Received: by 2002:a05:6a10:a852:0:0:0:0 with SMTP id d18csp3440234pxy; Tue, 4 May 2021 02:10:25 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzIGkOncnys90QHc5aEQEmiCXXE50WsTKkxOt3E7wU8q1o03B8T4ntYDJroWC7S8QcSa0Qx X-Received: by 2002:a63:f301:: with SMTP id l1mr21997539pgh.394.1620119424807; Tue, 04 May 2021 02:10:24 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1620119424; cv=none; d=google.com; s=arc-20160816; b=da92AnL6K5RRr3QYXU/vi9Z+z/+s3sgLGVxRtfp7bHWc+TkVYIqy1sABXEcpACQMe9 rQyvqImcS+bKBNMnCIV+PuFK63le+/jN7WVOQ1jvkhTP+iGYBE/a2DLc7c72SZpWxOnm INYnm71Evjl04yZm6LwmxL0/vTcQRunqEIoRi+or1FAbFdfXR/Z5C3z1mwKyLKFYJv/9 dioujjuTlYlA71YnZJ1dyx+RTM9BY402XtawyZvaXgdNrkvv6oUwFli7qFoL26qc9fX6 IIWDbLJPYuxVuVekrg57sg5cJkf1hGdYVH8rJdv1wcDzSsOBvEYjOYgX9SmHdI0cGxMA JEDA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:references:in-reply-to:message-id:date:subject :cc:to:from; bh=TbOryMag4sz96uObTPejOJ9T7a0t1JZCKiKvyoPunrI=; b=eekLPw5OUN9TE5rJE9nGwG6a2ipw5swEH9mXtARURx9nKaoJeiyjnQ6alqcjw6IK9a s5UvCcqoCkWwFZQged83v4ilXp/UilXuz2INGvCWkiV/0CVvwmNJrH4NeJg/8jI99aPN Uojk+dUC4CdbSfnG3pAbdDCv+t7P/w7vhUoyaRuIlX3wxLshGJj4EksyuMXFjKxOfIkN G7W99w6Fs77BE1hKi2Jh+zbPVr1hOu2ehzrTTD9Or1zVpR76I2MnFyM8J2diTfV8Xr4c jqSiBmadKoRisrGxY8W/k1z9gvXA5WEf1WSSaSqUQWBywLH90OVsxBe86QADtXzObEqs mrdQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id 89si3099367plb.254.2021.05.04.02.10.11; Tue, 04 May 2021 02:10:24 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230166AbhEDJJO (ORCPT + 99 others); Tue, 4 May 2021 05:09:14 -0400 Received: from foss.arm.com ([217.140.110.172]:55984 "EHLO foss.arm.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230113AbhEDJJL (ORCPT ); Tue, 4 May 2021 05:09:11 -0400 Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id E4E27106F; Tue, 4 May 2021 02:08:16 -0700 (PDT) Received: from e120189.arm.com (unknown [10.57.63.61]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPA id 9C95C3F73B; Tue, 4 May 2021 02:08:13 -0700 (PDT) From: Pierre Gondois To: linux-kernel@vger.kernel.org, xuewen.yan@unisoc.com, qperret@qperret.net, Lukasz.Luba@arm.com, dietmar.eggemann@arm.com Cc: Vincent.Donnefort@arm.com, Pierre Gondois , mingo@redhat.com, peterz@infradead.org, juri.lelli@redhat.com, vincent.guittot@linaro.org, rostedt@goodmis.org, bsegall@google.com, mgorman@suse.de, bristot@redhat.com Subject: [PATCH v3 2/2] sched/fair: Fix negative energy delta in find_energy_efficient_cpu() Date: Tue, 4 May 2021 10:07:43 +0100 Message-Id: <20210504090743.9688-3-Pierre.Gondois@arm.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20210504090743.9688-1-Pierre.Gondois@arm.com> References: <20210504090743.9688-1-Pierre.Gondois@arm.com> Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org find_energy_efficient_cpu() (feec()) searches the best energy CPU to place a task on. To do so, compute_energy() estimates the energy impact of placing the task on a CPU, based on CPU and task utilization signals. Utilization signals can be concurrently updated while evaluating a performance domain (pd). In some cases, this leads to having a 'negative delta', i.e. placing the task in the pd is seen as an energy gain. Thus, any further energy comparison is biased. In case of a 'negative delta', return prev_cpu since: 1. a 'negative delta' happens in less than 0.5% of feec() calls, on a Juno with 6 CPUs (4 little, 2 big) 2. it is unlikely to have two consecutive 'negative delta' for a task, so if the first call fails, feec() will correctly place the task in the next feec() call 3. EAS current behavior tends to select prev_cpu if the task doesn't raise the OPP of its current pd. prev_cpu is EAS's generic decision 4. prev_cpu should be preferred to returning an error code. In the latter case, select_idle_sibling() would do the placement, selecting a big (and not energy efficient) CPU. As 3., the task would potentially reside on the big CPU for a long time Reported-by: Xuewen Yan Suggested-by: Xuewen Yan Signed-off-by: Pierre Gondois Reviewed-by: Dietmar Eggemann Reviewed-by: Lukasz Luba --- kernel/sched/fair.c | 27 +++++++++++++++------------ 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/kernel/sched/fair.c b/kernel/sched/fair.c index fcb9595fc123..c20c6e1acf41 100644 --- a/kernel/sched/fair.c +++ b/kernel/sched/fair.c @@ -6594,15 +6594,15 @@ static int find_energy_efficient_cpu(struct task_struct *p, int prev_cpu) { unsigned long prev_delta = ULONG_MAX, best_delta = ULONG_MAX; struct root_domain *rd = cpu_rq(smp_processor_id())->rd; + int cpu, best_energy_cpu = prev_cpu, target = -1; unsigned long cpu_cap, util, base_energy = 0; - int cpu, best_energy_cpu = prev_cpu; struct sched_domain *sd; struct perf_domain *pd; rcu_read_lock(); pd = rcu_dereference(rd->pd); if (!pd || READ_ONCE(rd->overutilized)) - goto fail; + goto unlock; /* * Energy-aware wake-up happens on the lowest sched_domain starting @@ -6612,7 +6612,9 @@ static int find_energy_efficient_cpu(struct task_struct *p, int prev_cpu) while (sd && !cpumask_test_cpu(prev_cpu, sched_domain_span(sd))) sd = sd->parent; if (!sd) - goto fail; + goto unlock; + + target = prev_cpu; sync_entity_load_avg(&p->se); if (!task_util_est(p)) @@ -6667,6 +6669,8 @@ static int find_energy_efficient_cpu(struct task_struct *p, int prev_cpu) /* Evaluate the energy impact of using prev_cpu. */ if (compute_prev_delta) { prev_delta = compute_energy(p, prev_cpu, pd); + if (prev_delta < base_energy_pd) + goto unlock; prev_delta -= base_energy_pd; best_delta = min(best_delta, prev_delta); } @@ -6674,6 +6678,8 @@ static int find_energy_efficient_cpu(struct task_struct *p, int prev_cpu) /* Evaluate the energy impact of using max_spare_cap_cpu. */ if (max_spare_cap_cpu >= 0) { cur_delta = compute_energy(p, max_spare_cap_cpu, pd); + if (cur_delta < base_energy_pd) + goto unlock; cur_delta -= base_energy_pd; if (cur_delta < best_delta) { best_delta = cur_delta; @@ -6681,25 +6687,22 @@ static int find_energy_efficient_cpu(struct task_struct *p, int prev_cpu) } } } -unlock: rcu_read_unlock(); /* * Pick the best CPU if prev_cpu cannot be used, or if it saves at * least 6% of the energy used by prev_cpu. */ - if (prev_delta == ULONG_MAX) - return best_energy_cpu; + if ((prev_delta == ULONG_MAX) || + (prev_delta - best_delta) > ((prev_delta + base_energy) >> 4)) + target = best_energy_cpu; - if ((prev_delta - best_delta) > ((prev_delta + base_energy) >> 4)) - return best_energy_cpu; - - return prev_cpu; + return target; -fail: +unlock: rcu_read_unlock(); - return -1; + return target; } /* -- 2.17.1