Received: by 2002:a05:6a10:17d3:0:0:0:0 with SMTP id hz19csp3322626pxb; Tue, 20 Apr 2021 05:58:32 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzpb33gX+T0j4xz4X7O34wfO3/Zea5CsThbafS+sYsesKaTqwNoXfoSqjNGw/D1CCnLlNnJ X-Received: by 2002:a17:902:b28b:b029:ea:eda0:4d5e with SMTP id u11-20020a170902b28bb02900eaeda04d5emr27909730plr.68.1618923512044; Tue, 20 Apr 2021 05:58:32 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1618923512; cv=none; d=google.com; s=arc-20160816; b=y+GyvC/IQ5Vh1KKUX06t/jGLo7XlOHxGu6tWCSF5eECDpUD63syWztf7+bkNqzipMn J6ABHM6EJlBWWpEHPQmzQcLRGddlC3ldrtNMM4f1EX1aeRPwGWmsU8CnWvkdeH7aHSmC Z0Ez2AyFBl1RoUJzixG+BBwGp9P9CeqhSu9aTgDdz6ZhQ/0oD0dYXQ4YbLle5VD4mV23 f2H19rTB6EFIfSb/a0R9IF+rMxhdDzZuwhQXewDVop+FMlGvxPOvxf9NkS9N1SEx8apF tSmogbWhtD7vUXjSkIhl/VWM9AZl9CGd/GHH3UsRpmn3CJ8xoeQoYgNmsaOfHqwt4R7Z 7OSQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:message-id:date:subject:cc:to:from; bh=lOgfAlGy7wlfdFgWH6tuUKxLyDzIdw1vwukOS+cEKXU=; b=fSBwLvGHRbFYvE4OvskN2SF8DiaULmxHWXfRMBhVZYGNl64Q6G7D8Tu4chSn/BUY+P pvS352Howpbdn+9sZBn79NAHH5c2SQx2/Z64OT07nss1qFHD3OkR221BHSLWabq53LRX lhKj0sBHERITZEqkR7T+guwvhqwNJ+PM53qtcQMKDyRxz2TIkmmHXaPjRkqBy7rGNmLv Ic84H8MOJdZAN9z+NQCiayQGJxtsa16s+p2MOmAJqznAJTuMdiTZe2QEkDh67T32jpjY hX6mZfWM/Rlf0uRwoA1PQqAoaj60LAB4lZZ0NHoJgxUINtvBVCMcTHiyDCatemySU7Jm Cc/A== 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 g2si1977784pll.367.2021.04.20.05.58.19; Tue, 20 Apr 2021 05:58:32 -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 S232094AbhDTM6J (ORCPT + 99 others); Tue, 20 Apr 2021 08:58:09 -0400 Received: from foss.arm.com ([217.140.110.172]:34304 "EHLO foss.arm.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232026AbhDTM6I (ORCPT ); Tue, 20 Apr 2021 08:58:08 -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 E8C241478; Tue, 20 Apr 2021 05:57:36 -0700 (PDT) Received: from e120189.arm.com (unknown [10.57.57.251]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPA id 62DBF3F792; Tue, 20 Apr 2021 05:57:33 -0700 (PDT) From: Pierre.Gondois@arm.com To: linux-kernel@vger.kernel.org, xuewen.yan@unisoc.com Cc: Lukasz.Luba@arm.com, Vincent.Donnefort@arm.com, dietmar.eggemann@arm.com, qais.yousef@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, qperret@qperret.net Subject: [PATCH] sched/fair: Fix negative energy delta in find_energy_efficient_cpu() Date: Tue, 20 Apr 2021 13:56:04 +0100 Message-Id: <20210420125604.15796-1-Pierre.Gondois@arm.com> X-Mailer: git-send-email 2.17.1 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Pierre Gondois 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 perf_domain. In some cases, this leads to having a 'negative delta', i.e. placing the task in the perf_domain 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 perf_domain. 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 The patch also: a. groups the compute_energy() calls to lower the chances of having concurrent updates in between the calls b. skips the base_energy_pd computation if no CPU is available in a perf_domain Fixes: eb92692b2544d sched/fair: Speed-up energy-aware wake-up Reported-by: Xuewen Yan Suggested-by: Xuewen Yan Signed-off-by: Pierre Gondois --- kernel/sched/fair.c | 69 +++++++++++++++++++++++++-------------------- 1 file changed, 39 insertions(+), 30 deletions(-) diff --git a/kernel/sched/fair.c b/kernel/sched/fair.c index 0dba0ebc3657..577482aa8919 100644 --- a/kernel/sched/fair.c +++ b/kernel/sched/fair.c @@ -6594,8 +6594,8 @@ 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; @@ -6614,19 +6614,18 @@ static int find_energy_efficient_cpu(struct task_struct *p, int prev_cpu) if (!sd) goto fail; + target = prev_cpu; + sync_entity_load_avg(&p->se); if (!task_util_est(p)) - goto unlock; + goto fail; for (; pd; pd = pd->next) { unsigned long cur_delta, spare_cap, max_spare_cap = 0; + bool compute_prev_delta = false; unsigned long base_energy_pd; int max_spare_cap_cpu = -1; - /* Compute the 'base' energy of the pd, without @p */ - base_energy_pd = compute_energy(p, -1, pd); - base_energy += base_energy_pd; - for_each_cpu_and(cpu, perf_domain_span(pd), sched_domain_span(sd)) { if (!cpumask_test_cpu(cpu, p->cpus_ptr)) continue; @@ -6647,26 +6646,41 @@ static int find_energy_efficient_cpu(struct task_struct *p, int prev_cpu) if (!fits_capacity(util, cpu_cap)) continue; - /* Always use prev_cpu as a candidate. */ if (cpu == prev_cpu) { - prev_delta = compute_energy(p, prev_cpu, pd); - prev_delta -= base_energy_pd; - best_delta = min(best_delta, prev_delta); - } - - /* - * Find the CPU with the maximum spare capacity in - * the performance domain - */ - if (spare_cap > max_spare_cap) { + /* Always use prev_cpu as a candidate. */ + compute_prev_delta = true; + } else if (spare_cap > max_spare_cap) { + /* + * Find the CPU with the maximum spare capacity + * in the performance domain. + */ max_spare_cap = spare_cap; max_spare_cap_cpu = cpu; } } + if (max_spare_cap_cpu < 0 && !compute_prev_delta) + continue; + + /* Compute the 'base' energy of the pd, without @p */ + base_energy_pd = compute_energy(p, -1, pd); + base_energy += base_energy_pd; + + if (compute_prev_delta) { + prev_delta = compute_energy(p, prev_cpu, pd); + /* Prevent negative deltas and select prev_cpu */ + if (prev_delta < base_energy_pd) + goto fail; + prev_delta -= base_energy_pd; + best_delta = min(best_delta, prev_delta); + } + /* Evaluate the energy impact of using this CPU. */ - if (max_spare_cap_cpu >= 0 && max_spare_cap_cpu != prev_cpu) { + if (max_spare_cap_cpu >= 0) { cur_delta = compute_energy(p, max_spare_cap_cpu, pd); + /* Prevent negative deltas and select prev_cpu */ + if (cur_delta < base_energy_pd) + goto fail; cur_delta -= base_energy_pd; if (cur_delta < best_delta) { best_delta = cur_delta; @@ -6674,25 +6688,20 @@ 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. + * Pick the best CPU if: + * - prev_cpu cannot be used, or + * - it saves at least 6% of the energy used by prev_cpu */ - if (prev_delta == ULONG_MAX) - return best_energy_cpu; - - if ((prev_delta - best_delta) > ((prev_delta + base_energy) >> 4)) - return best_energy_cpu; - - return prev_cpu; + if ((prev_delta == ULONG_MAX) || + (prev_delta - best_delta) > ((prev_delta + base_energy) >> 4)) + target = best_energy_cpu; fail: rcu_read_unlock(); - return -1; + return target; } /* -- 2.17.1