From patchwork Fri Sep 1 13:03:09 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Vincent Guittot X-Patchwork-Id: 719669 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id B0DBACA0FE1 for ; Fri, 1 Sep 2023 13:03:24 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1345328AbjIANDZ (ORCPT ); Fri, 1 Sep 2023 09:03:25 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46740 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S242897AbjIANDY (ORCPT ); Fri, 1 Sep 2023 09:03:24 -0400 Received: from mail-wr1-x429.google.com (mail-wr1-x429.google.com [IPv6:2a00:1450:4864:20::429]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 74693E7E for ; Fri, 1 Sep 2023 06:03:20 -0700 (PDT) Received: by mail-wr1-x429.google.com with SMTP id ffacd0b85a97d-31c73c21113so1593859f8f.1 for ; Fri, 01 Sep 2023 06:03:20 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1693573399; x=1694178199; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=3XGN82il/1YBgFB1xd1FuzsdVFxN2VHyBDMO8DeBOOo=; b=nNqDXUqClQHvapuHdw3Gjfoyglv76AC9Lc6rrn940rgKTsmlXnnJEmEnAjUSw6bUou DNAKrDN3q5GlX1cGvqCzGRdQwvTyWyhF1FCPcliBuTrJGCRfT6jssXEPtooBROetF+Lv ItsGObdPc4hkAU7bmUzqPoAE4grR0OBCAnijEQQ722+k5rrq8StdBG6GxUdIXh6ZlK90 9rZAnpM4Yw/fevUyrFTz7h9NeTULpn3bvIenEtN1OizEN/GWXVSySlYIkuh6nPC8c9AQ xedsOtRm/MDJI/uUlLZmymtFajzcNWUOBrDePFXMTD6Cl1MVWsSPGJM7Uj/MI2KodxVP EpoQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1693573399; x=1694178199; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=3XGN82il/1YBgFB1xd1FuzsdVFxN2VHyBDMO8DeBOOo=; b=HhWs6TwLQo9QkCep/9+s7E7i2F9o8icnKrOhA11CH4iR7Ax20pUfIlmdkLpcHIdDlU aEP1+58YcsGIzvw0z9hzaaSjZbomfHpjEQOc3ZAbarnqm8XoSeoCJVAh4mUwf58EelJa qz2DLMUx057o2e6bkyBPrswaQEGJdy6IkYPs8c7URkt1frMErYw+AlNZW3ALlbaf/8ss 1C3s1xXxmHKQpaOMdPJjdhCpX8Ip9DReXzpiUb380QnRkTpl5RfmYPLkDdCOGDJb35jL v5MZt+k+lk5Ksr7L3gR4LIiA2rn+YxVyXrRTmOOONDCXHN2D5xBbs2w2tHruJZJsVwlK U4fQ== X-Gm-Message-State: AOJu0Yw8JdbAdTrf4HxQJk/HJqtPE0FiYW7dGBHuvD547IeG58xeGKY9 TreUZE146n1AyOiJpwuIcAma9A== X-Google-Smtp-Source: AGHT+IERLKj3sQli7kquAJo1LxhyV8dn8e2ozcL/AJpoVa8ta4GyQxSWCkp906EBGCj0hP1nYCxKOQ== X-Received: by 2002:adf:f282:0:b0:317:5d3d:c9df with SMTP id k2-20020adff282000000b003175d3dc9dfmr1892285wro.18.1693573397559; Fri, 01 Sep 2023 06:03:17 -0700 (PDT) Received: from vingu-book.. ([2a01:e0a:f:6020:e9bd:add1:d9ac:7b3e]) by smtp.gmail.com with ESMTPSA id i14-20020adfdece000000b003142e438e8csm5167452wrn.26.2023.09.01.06.03.16 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 01 Sep 2023 06:03:16 -0700 (PDT) From: Vincent Guittot To: linux@armlinux.org.uk, catalin.marinas@arm.com, will@kernel.org, paul.walmsley@sifive.com, palmer@dabbelt.com, aou@eecs.berkeley.edu, sudeep.holla@arm.com, gregkh@linuxfoundation.org, rafael@kernel.org, mingo@redhat.com, peterz@infradead.org, juri.lelli@redhat.com, dietmar.eggemann@arm.com, rostedt@goodmis.org, bsegall@google.com, mgorman@suse.de, bristot@redhat.com, vschneid@redhat.com, viresh.kumar@linaro.org, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, linux-riscv@lists.infradead.org, linux-pm@vger.kernel.org Cc: conor.dooley@microchip.com, suagrfillet@gmail.com, ajones@ventanamicro.com, lftan@kernel.org, Vincent Guittot Subject: [PATCH 1/4] sched: consolidate and cleanup access to CPU's max compute capacity Date: Fri, 1 Sep 2023 15:03:09 +0200 Message-Id: <20230901130312.247719-2-vincent.guittot@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230901130312.247719-1-vincent.guittot@linaro.org> References: <20230901130312.247719-1-vincent.guittot@linaro.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-pm@vger.kernel.org Remove struct rq cpu_capacity_orig field and use arch_scale_cpu_capacity() instead. Scheduler uses 3 methods to get access to the CPU's max compute capacity: - arch_scale_cpu_capacity(cpu) which is the default way to get CPU's capacity. - cpu_capacity_orig field which is periodically updated with arch_scale_cpu_capacity(). - capacity_orig_of(cpu) which encapsulates rq->cpu_capacity_orig There is no real need to save the value returned by arch_scale_cpu_capacity() in struct rq. arch_scale_cpu_capacity() returns: - either a per_cpu variable. - or a const value for systems which have only one capacity. Remove cpu_capacity_orig and use arch_scale_cpu_capacity() everywhere. No functional changes. some tests of Arm64: small SMP device (hikey): no noticeable changes HMP device (RB5): hackbench shows minor improvement (1-2%) large smp (thx2): hackbench and tbench shows minor improvement (1%) Signed-off-by: Vincent Guittot Reviewed-by: Dietmar Eggemann --- kernel/sched/core.c | 2 +- kernel/sched/cpudeadline.c | 2 +- kernel/sched/deadline.c | 4 ++-- kernel/sched/fair.c | 18 ++++++++---------- kernel/sched/rt.c | 2 +- kernel/sched/sched.h | 6 ------ kernel/sched/topology.c | 7 +++++-- 7 files changed, 18 insertions(+), 23 deletions(-) diff --git a/kernel/sched/core.c b/kernel/sched/core.c index efe3848978a0..6560392f2f83 100644 --- a/kernel/sched/core.c +++ b/kernel/sched/core.c @@ -10014,7 +10014,7 @@ void __init sched_init(void) #ifdef CONFIG_SMP rq->sd = NULL; rq->rd = NULL; - rq->cpu_capacity = rq->cpu_capacity_orig = SCHED_CAPACITY_SCALE; + rq->cpu_capacity = SCHED_CAPACITY_SCALE; rq->balance_callback = &balance_push_callback; rq->active_balance = 0; rq->next_balance = jiffies; diff --git a/kernel/sched/cpudeadline.c b/kernel/sched/cpudeadline.c index 57c92d751bcd..95baa12a1029 100644 --- a/kernel/sched/cpudeadline.c +++ b/kernel/sched/cpudeadline.c @@ -131,7 +131,7 @@ int cpudl_find(struct cpudl *cp, struct task_struct *p, if (!dl_task_fits_capacity(p, cpu)) { cpumask_clear_cpu(cpu, later_mask); - cap = capacity_orig_of(cpu); + cap = arch_scale_cpu_capacity(cpu); if (cap > max_cap || (cpu == task_cpu(p) && cap == max_cap)) { diff --git a/kernel/sched/deadline.c b/kernel/sched/deadline.c index 58b542bf2893..c57ef2e0db41 100644 --- a/kernel/sched/deadline.c +++ b/kernel/sched/deadline.c @@ -132,7 +132,7 @@ static inline unsigned long __dl_bw_capacity(const struct cpumask *mask) int i; for_each_cpu_and(i, mask, cpu_active_mask) - cap += capacity_orig_of(i); + cap += arch_scale_cpu_capacity(i); return cap; } @@ -144,7 +144,7 @@ static inline unsigned long __dl_bw_capacity(const struct cpumask *mask) static inline unsigned long dl_bw_capacity(int i) { if (!sched_asym_cpucap_active() && - capacity_orig_of(i) == SCHED_CAPACITY_SCALE) { + arch_scale_cpu_capacity(i) == SCHED_CAPACITY_SCALE) { return dl_bw_cpus(i) << SCHED_CAPACITY_SHIFT; } else { RCU_LOCKDEP_WARN(!rcu_read_lock_sched_held(), diff --git a/kernel/sched/fair.c b/kernel/sched/fair.c index 0b7445cd5af9..06d6d0dde48a 100644 --- a/kernel/sched/fair.c +++ b/kernel/sched/fair.c @@ -4690,7 +4690,7 @@ static inline void util_est_update(struct cfs_rq *cfs_rq, * To avoid overestimation of actual task utilization, skip updates if * we cannot grant there is idle time in this CPU. */ - if (task_util(p) > capacity_orig_of(cpu_of(rq_of(cfs_rq)))) + if (task_util(p) > arch_scale_cpu_capacity(cpu_of(rq_of(cfs_rq)))) return; /* @@ -4738,14 +4738,14 @@ static inline int util_fits_cpu(unsigned long util, return fits; /* - * We must use capacity_orig_of() for comparing against uclamp_min and + * We must use arch_scale_cpu_capacity() for comparing against uclamp_min and * uclamp_max. We only care about capacity pressure (by using * capacity_of()) for comparing against the real util. * * If a task is boosted to 1024 for example, we don't want a tiny * pressure to skew the check whether it fits a CPU or not. * - * Similarly if a task is capped to capacity_orig_of(little_cpu), it + * Similarly if a task is capped to arch_scale_cpu_capacity(little_cpu), it * should fit a little cpu even if there's some pressure. * * Only exception is for thermal pressure since it has a direct impact @@ -4757,7 +4757,7 @@ static inline int util_fits_cpu(unsigned long util, * For uclamp_max, we can tolerate a drop in performance level as the * goal is to cap the task. So it's okay if it's getting less. */ - capacity_orig = capacity_orig_of(cpu); + capacity_orig = arch_scale_cpu_capacity(cpu); capacity_orig_thermal = capacity_orig - arch_scale_thermal_pressure(cpu); /* @@ -7226,7 +7226,7 @@ select_idle_capacity(struct task_struct *p, struct sched_domain *sd, int target) * Look for the CPU with best capacity. */ else if (fits < 0) - cpu_cap = capacity_orig_of(cpu) - thermal_load_avg(cpu_rq(cpu)); + cpu_cap = arch_scale_cpu_capacity(cpu) - thermal_load_avg(cpu_rq(cpu)); /* * First, select CPU which fits better (-1 being better than 0). @@ -7468,7 +7468,7 @@ cpu_util(int cpu, struct task_struct *p, int dst_cpu, int boost) util = max(util, util_est); } - return min(util, capacity_orig_of(cpu)); + return min(util, arch_scale_cpu_capacity(cpu)); } unsigned long cpu_util_cfs(int cpu) @@ -9251,8 +9251,6 @@ static void update_cpu_capacity(struct sched_domain *sd, int cpu) unsigned long capacity = scale_rt_capacity(cpu); struct sched_group *sdg = sd->groups; - cpu_rq(cpu)->cpu_capacity_orig = arch_scale_cpu_capacity(cpu); - if (!capacity) capacity = 1; @@ -9328,7 +9326,7 @@ static inline int check_cpu_capacity(struct rq *rq, struct sched_domain *sd) { return ((rq->cpu_capacity * sd->imbalance_pct) < - (rq->cpu_capacity_orig * 100)); + (arch_scale_cpu_capacity(cpu_of(rq)) * 100)); } /* @@ -9339,7 +9337,7 @@ check_cpu_capacity(struct rq *rq, struct sched_domain *sd) static inline int check_misfit_status(struct rq *rq, struct sched_domain *sd) { return rq->misfit_task_load && - (rq->cpu_capacity_orig < rq->rd->max_cpu_capacity || + (arch_scale_cpu_capacity(rq->cpu) < rq->rd->max_cpu_capacity || check_cpu_capacity(rq, sd)); } diff --git a/kernel/sched/rt.c b/kernel/sched/rt.c index 0597ba0f85ff..8f4e8db6e234 100644 --- a/kernel/sched/rt.c +++ b/kernel/sched/rt.c @@ -515,7 +515,7 @@ static inline bool rt_task_fits_capacity(struct task_struct *p, int cpu) min_cap = uclamp_eff_value(p, UCLAMP_MIN); max_cap = uclamp_eff_value(p, UCLAMP_MAX); - cpu_cap = capacity_orig_of(cpu); + cpu_cap = arch_scale_cpu_capacity(cpu); return cpu_cap >= min(min_cap, max_cap); } diff --git a/kernel/sched/sched.h b/kernel/sched/sched.h index 3a01b7a2bf66..17ae151e90c0 100644 --- a/kernel/sched/sched.h +++ b/kernel/sched/sched.h @@ -1048,7 +1048,6 @@ struct rq { struct sched_domain __rcu *sd; unsigned long cpu_capacity; - unsigned long cpu_capacity_orig; struct balance_callback *balance_callback; @@ -2974,11 +2973,6 @@ static inline void cpufreq_update_util(struct rq *rq, unsigned int flags) {} #endif #ifdef CONFIG_SMP -static inline unsigned long capacity_orig_of(int cpu) -{ - return cpu_rq(cpu)->cpu_capacity_orig; -} - /** * enum cpu_util_type - CPU utilization type * @FREQUENCY_UTIL: Utilization used to select frequency diff --git a/kernel/sched/topology.c b/kernel/sched/topology.c index 05a5bc678c08..e6b0b6a8e60a 100644 --- a/kernel/sched/topology.c +++ b/kernel/sched/topology.c @@ -2479,12 +2479,15 @@ build_sched_domains(const struct cpumask *cpu_map, struct sched_domain_attr *att /* Attach the domains */ rcu_read_lock(); for_each_cpu(i, cpu_map) { + unsigned long capacity; + rq = cpu_rq(i); sd = *per_cpu_ptr(d.sd, i); + capacity = arch_scale_cpu_capacity(i); /* Use READ_ONCE()/WRITE_ONCE() to avoid load/store tearing: */ - if (rq->cpu_capacity_orig > READ_ONCE(d.rd->max_cpu_capacity)) - WRITE_ONCE(d.rd->max_cpu_capacity, rq->cpu_capacity_orig); + if (capacity > READ_ONCE(d.rd->max_cpu_capacity)) + WRITE_ONCE(d.rd->max_cpu_capacity, capacity); cpu_attach_domain(sd, d.rd, i); } From patchwork Fri Sep 1 13:03:10 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Vincent Guittot X-Patchwork-Id: 719842 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 5C715CA0FE6 for ; Fri, 1 Sep 2023 13:03:26 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1347782AbjIAND0 (ORCPT ); Fri, 1 Sep 2023 09:03:26 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46744 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1345792AbjIANDZ (ORCPT ); Fri, 1 Sep 2023 09:03:25 -0400 Received: from mail-wr1-x42f.google.com (mail-wr1-x42f.google.com [IPv6:2a00:1450:4864:20::42f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1524810CA for ; Fri, 1 Sep 2023 06:03:22 -0700 (PDT) Received: by mail-wr1-x42f.google.com with SMTP id ffacd0b85a97d-31de47996c8so1656794f8f.2 for ; Fri, 01 Sep 2023 06:03:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1693573400; x=1694178200; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=Lc2/PThdglNLGfS0qJb++8JNKia8ENWEPE++6QMdKJg=; b=isk+rIEjOm07QeYfpOewfO+4OY+UQBLieRIcXY1xvZd32o7aFvL5jTMcEa02LpNADG zGbc8gs3YcWL4TSNB3p60PdQkGRMdt1jrBJl8+Gqztf69LlFC+vLhkcrPEfM+JwVZ0hk MQCG1zAyYe1tYaBbezn5C0FowM1Y+kYY4tfYQLBQD7qsLMhKPe20XufaBtsa3N1rWT25 csPkkfq/BDw7MZMnnqkbLAs3CVqIOcS1sdwBUpaHmXDcrsSzESBHL4V6o2IcYSNbIlxm btkShGPr259KGYArv/KVi5Q1o2wSqYRUrCZJLXJmO78iZfK5dWjZPcf2lYhp+nx2GEI1 CbCw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1693573400; x=1694178200; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=Lc2/PThdglNLGfS0qJb++8JNKia8ENWEPE++6QMdKJg=; b=gvPNaN8MiY/jKMwj4xx4UIpo6ZYlw7PjxmCZKaioW/H8ZFrVnp98lQhZLoSJ+T/YN2 9CU4ZIpYj/rtimZZuRXFxysOoGDIgj76j/3Ed9fmKjz50/fiwD/7YQD9sPxxXboT80mw AouO7Mt1WlMeQXi3kx9TDHLJt/wTox//eiIY9BnKJMsTD0pKWdbW0H0HTyyIsw2rArT4 uTGTqvUDRAi4XnmhWHWLub5OBNmz+hgusYUVbirJhREjYoAN47e2Aq72epYHTNcvKVd4 z4J2WZIE/NSIWys2kj2x656qcAp8v8nIR0facQLXIp1QOpUhPkYRIv6M5GvBCQku+zHq DJOg== X-Gm-Message-State: AOJu0YzNcwUjFmhOc5XkpHxsqd8Cp1JMB/JWacUnyF2tbQ3NIcU86ayD nVxrNRfcr64+3DNqtaRnqPxxdA== X-Google-Smtp-Source: AGHT+IFhopL0OmPezFJfjy60UyNhSKg8ZqbBwJgAZmOJYjwD1j0DD1QYHLDKXKPgXXRmJOa5y8JoMw== X-Received: by 2002:adf:ecc6:0:b0:317:58e4:e941 with SMTP id s6-20020adfecc6000000b0031758e4e941mr1608494wro.33.1693573400353; Fri, 01 Sep 2023 06:03:20 -0700 (PDT) Received: from vingu-book.. ([2a01:e0a:f:6020:e9bd:add1:d9ac:7b3e]) by smtp.gmail.com with ESMTPSA id i14-20020adfdece000000b003142e438e8csm5167452wrn.26.2023.09.01.06.03.19 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 01 Sep 2023 06:03:19 -0700 (PDT) From: Vincent Guittot To: linux@armlinux.org.uk, catalin.marinas@arm.com, will@kernel.org, paul.walmsley@sifive.com, palmer@dabbelt.com, aou@eecs.berkeley.edu, sudeep.holla@arm.com, gregkh@linuxfoundation.org, rafael@kernel.org, mingo@redhat.com, peterz@infradead.org, juri.lelli@redhat.com, dietmar.eggemann@arm.com, rostedt@goodmis.org, bsegall@google.com, mgorman@suse.de, bristot@redhat.com, vschneid@redhat.com, viresh.kumar@linaro.org, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, linux-riscv@lists.infradead.org, linux-pm@vger.kernel.org Cc: conor.dooley@microchip.com, suagrfillet@gmail.com, ajones@ventanamicro.com, lftan@kernel.org, Vincent Guittot Subject: [PATCH 2/4] topology: add a new arch_scale_freq_reference Date: Fri, 1 Sep 2023 15:03:10 +0200 Message-Id: <20230901130312.247719-3-vincent.guittot@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230901130312.247719-1-vincent.guittot@linaro.org> References: <20230901130312.247719-1-vincent.guittot@linaro.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-pm@vger.kernel.org Create a new method to get a unique and fixed max frequency. Currently cpuinfo.max_freq or last item of performance domain are used as the max frequency when computing the frequency for a level of utilization but: - cpuinfo_max_freq can change at runtime. boost is one example of such change. - cpuinfo.max_freq and last item of the PD can be different leading to different results betwen cpufreq and energy model. We need to save the max frequency that has been used when computing the CPUs capacity and use this fixed and coherent value to convert between frequency and CPU's capacity. In fact, we already save the frequency that has been used when computing the capacity of each CPU. We extend the precision to save khZ instead of Mhz currently and we modify the type to be aligned with other variables used when converting frequency to capacity and the other way. Signed-off-by: Vincent Guittot --- arch/arm/include/asm/topology.h | 1 + arch/arm64/include/asm/topology.h | 1 + arch/riscv/include/asm/topology.h | 1 + drivers/base/arch_topology.c | 9 +++------ include/linux/arch_topology.h | 7 +++++++ 5 files changed, 13 insertions(+), 6 deletions(-) diff --git a/arch/arm/include/asm/topology.h b/arch/arm/include/asm/topology.h index c7d2510e5a78..853c4f81ba4a 100644 --- a/arch/arm/include/asm/topology.h +++ b/arch/arm/include/asm/topology.h @@ -13,6 +13,7 @@ #define arch_set_freq_scale topology_set_freq_scale #define arch_scale_freq_capacity topology_get_freq_scale #define arch_scale_freq_invariant topology_scale_freq_invariant +#define arch_scale_freq_ref topology_get_freq_ref #endif /* Replace task scheduler's default cpu-invariant accounting */ diff --git a/arch/arm64/include/asm/topology.h b/arch/arm64/include/asm/topology.h index 9fab663dd2de..a323b109b9c4 100644 --- a/arch/arm64/include/asm/topology.h +++ b/arch/arm64/include/asm/topology.h @@ -23,6 +23,7 @@ void update_freq_counters_refs(void); #define arch_set_freq_scale topology_set_freq_scale #define arch_scale_freq_capacity topology_get_freq_scale #define arch_scale_freq_invariant topology_scale_freq_invariant +#define arch_scale_freq_ref topology_get_freq_ref #ifdef CONFIG_ACPI_CPPC_LIB #define arch_init_invariance_cppc topology_init_cpu_capacity_cppc diff --git a/arch/riscv/include/asm/topology.h b/arch/riscv/include/asm/topology.h index e316ab3b77f3..61183688bdd5 100644 --- a/arch/riscv/include/asm/topology.h +++ b/arch/riscv/include/asm/topology.h @@ -9,6 +9,7 @@ #define arch_set_freq_scale topology_set_freq_scale #define arch_scale_freq_capacity topology_get_freq_scale #define arch_scale_freq_invariant topology_scale_freq_invariant +#define arch_scale_freq_ref topology_get_freq_ref /* Replace task scheduler's default cpu-invariant accounting */ #define arch_scale_cpu_capacity topology_get_cpu_scale diff --git a/drivers/base/arch_topology.c b/drivers/base/arch_topology.c index b741b5ba82bd..75fa67477a9d 100644 --- a/drivers/base/arch_topology.c +++ b/drivers/base/arch_topology.c @@ -26,7 +26,7 @@ static DEFINE_PER_CPU(struct scale_freq_data __rcu *, sft_data); static struct cpumask scale_freq_counters_mask; static bool scale_freq_invariant; -static DEFINE_PER_CPU(u32, freq_factor) = 1; +DEFINE_PER_CPU(unsigned long, freq_factor) = 1; static bool supports_scale_freq_counters(const struct cpumask *cpus) { @@ -183,10 +183,7 @@ void topology_update_thermal_pressure(const struct cpumask *cpus, cpu = cpumask_first(cpus); max_capacity = arch_scale_cpu_capacity(cpu); - max_freq = per_cpu(freq_factor, cpu); - - /* Convert to MHz scale which is used in 'freq_factor' */ - capped_freq /= 1000; + max_freq = arch_scale_freq_ref(cpu); /* * Handle properly the boost frequencies, which should simply clean @@ -411,7 +408,7 @@ init_cpu_capacity_callback(struct notifier_block *nb, cpumask_andnot(cpus_to_visit, cpus_to_visit, policy->related_cpus); for_each_cpu(cpu, policy->related_cpus) - per_cpu(freq_factor, cpu) = policy->cpuinfo.max_freq / 1000; + per_cpu(freq_factor, cpu) = policy->cpuinfo.max_freq; if (cpumask_empty(cpus_to_visit)) { topology_normalize_cpu_scale(); diff --git a/include/linux/arch_topology.h b/include/linux/arch_topology.h index a07b510e7dc5..7a2dba9c3dc0 100644 --- a/include/linux/arch_topology.h +++ b/include/linux/arch_topology.h @@ -27,6 +27,13 @@ static inline unsigned long topology_get_cpu_scale(int cpu) void topology_set_cpu_scale(unsigned int cpu, unsigned long capacity); +DECLARE_PER_CPU(unsigned long, freq_factor); + +static inline unsigned long topology_get_freq_ref(int cpu) +{ + return per_cpu(freq_factor, cpu); +} + DECLARE_PER_CPU(unsigned long, arch_freq_scale); static inline unsigned long topology_get_freq_scale(int cpu) From patchwork Fri Sep 1 13:03:11 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Vincent Guittot X-Patchwork-Id: 719668 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 732F7CA0FE9 for ; Fri, 1 Sep 2023 13:03:26 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1349622AbjIAND0 (ORCPT ); Fri, 1 Sep 2023 09:03:26 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46770 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1349596AbjIAND0 (ORCPT ); Fri, 1 Sep 2023 09:03:26 -0400 Received: from mail-wm1-x330.google.com (mail-wm1-x330.google.com [IPv6:2a00:1450:4864:20::330]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4BF06E7E for ; Fri, 1 Sep 2023 06:03:23 -0700 (PDT) Received: by mail-wm1-x330.google.com with SMTP id 5b1f17b1804b1-401b3ea0656so18976445e9.0 for ; Fri, 01 Sep 2023 06:03:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1693573402; x=1694178202; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=jBj2auLlUAXmJtm0JLBXBgA7sNg7bnkhJEMz0GdiTZI=; b=OpK4JaCciL5TRL2pawG/VmQ8Jdds1w2k8OeopaJ/tilLw+/YIJqEoh7PyKYrzSHBNP 1ed2IdIpZ/eI5oFahFSwshrTxJNvovEjUUrrFbHBWg+HoXHettVV5zZ44DMlcyabng8M 39ZqV5TYraxwOsvPuqUMnicMoXvIPAjseghcabVIw+f5UA3fOBa9ynuLukGnDp6BYEO5 RdG4gBzPb+hYTs5jz+5Ec5RmrcX/9Olow8iVhxp4NhW2KxOumnlUMLKD1KG6MCnsd2fk XceG41egOaQIT7RTvxmyXM/aZBGf99nJUzsxu9Ff1BcFRESd3Hw4VVcjCJ9eb+WMEjnG 4NYQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1693573402; x=1694178202; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=jBj2auLlUAXmJtm0JLBXBgA7sNg7bnkhJEMz0GdiTZI=; b=BUliRTvEJPLT1UBsmqnXj/qJ/Apact4mP0pGylx1dMA5cQxu+wDPBdQ4aScSh4aryC lFTYxtX/6uI1X0gSEFehQnF7DfmcYmyiOHntMLYOQhpvLHmyAV21/Z3TV0PgRLPzdcjV qJKHQ5b3UcysFrlE3OdcyBNpBBmFQsYT3igByvRe6NlXA19M5Vi88Io++nA0g1h8ib7n I/z4iJX6aa/ZmN9jvDwu1yP9bd+x1RCCERpQuIBfXsuCKy0gbvYqfOgvjHm9kpo/8m6m 7hwMedIanOjbPARzia48RbEAPiPCIS6rqXw1lIrHsmizIUMGNRtQLpXTGKpezWlFfoKf U7Sg== X-Gm-Message-State: AOJu0YwK7mMgsOzA/bCMunLkPVa/arSG6hOMCh0Qu2U4zm6IplGWSMxZ 9JbGyotlUttz2Y2JY3AQkDKgZg== X-Google-Smtp-Source: AGHT+IEQD39cpHWbAVr3Bfe0Bk2YZV7G5Wbn5InEVXtG214GUwMtcEYoELLPBmdXYyGWOssaBv1Vxw== X-Received: by 2002:a05:600c:234a:b0:401:bd95:2e02 with SMTP id 10-20020a05600c234a00b00401bd952e02mr1551706wmq.39.1693573401777; Fri, 01 Sep 2023 06:03:21 -0700 (PDT) Received: from vingu-book.. ([2a01:e0a:f:6020:e9bd:add1:d9ac:7b3e]) by smtp.gmail.com with ESMTPSA id i14-20020adfdece000000b003142e438e8csm5167452wrn.26.2023.09.01.06.03.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 01 Sep 2023 06:03:21 -0700 (PDT) From: Vincent Guittot To: linux@armlinux.org.uk, catalin.marinas@arm.com, will@kernel.org, paul.walmsley@sifive.com, palmer@dabbelt.com, aou@eecs.berkeley.edu, sudeep.holla@arm.com, gregkh@linuxfoundation.org, rafael@kernel.org, mingo@redhat.com, peterz@infradead.org, juri.lelli@redhat.com, dietmar.eggemann@arm.com, rostedt@goodmis.org, bsegall@google.com, mgorman@suse.de, bristot@redhat.com, vschneid@redhat.com, viresh.kumar@linaro.org, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, linux-riscv@lists.infradead.org, linux-pm@vger.kernel.org Cc: conor.dooley@microchip.com, suagrfillet@gmail.com, ajones@ventanamicro.com, lftan@kernel.org, Vincent Guittot Subject: [PATCH 3/4] cpufreq/schedutil: use a fixed reference frequency Date: Fri, 1 Sep 2023 15:03:11 +0200 Message-Id: <20230901130312.247719-4-vincent.guittot@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230901130312.247719-1-vincent.guittot@linaro.org> References: <20230901130312.247719-1-vincent.guittot@linaro.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-pm@vger.kernel.org cpuinfo_max_freq can change at runtime because of boost as example. This implies that the value could not be the same than the one that has been used when computing the capacity of a CPU. The new arch_scale_freq_ref() returns a fixed and coherent frequency reference that can be used when computing a frequency based on utilization. Use this arch_scale_freq_ref() when available and fallback to cpuinfo.max_freq otherwise. Signed-off-by: Vincent Guittot --- kernel/sched/cpufreq_schedutil.c | 29 +++++++++++++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) diff --git a/kernel/sched/cpufreq_schedutil.c b/kernel/sched/cpufreq_schedutil.c index 4492608b7d7f..9996ef429e2b 100644 --- a/kernel/sched/cpufreq_schedutil.c +++ b/kernel/sched/cpufreq_schedutil.c @@ -114,6 +114,31 @@ static void sugov_deferred_update(struct sugov_policy *sg_policy) } } +#ifdef arch_scale_freq_ref +/** + * arch_scale_freq_ref_policy - get the reference frequency of a given CPU that + * has been used to correlate frequency and compute capacity. + * @cpu: the CPU in question. + * + * Return: the reference CPU frequency. + */ +static __always_inline +unsigned long arch_scale_freq_ref_policy(struct cpufreq_policy *policy) +{ + return arch_scale_freq_ref(policy->cpu); +} +#else +static __always_inline +unsigned long arch_scale_freq_ref_policy(struct cpufreq_policy *policy) +{ + if (arch_scale_freq_invariant()) + return policy->cpuinfo.max_freq; + + + return policy->cur; +} +#endif + /** * get_next_freq - Compute a new frequency for a given cpufreq policy. * @sg_policy: schedutil policy object to compute the new frequency for. @@ -139,11 +164,11 @@ static void sugov_deferred_update(struct sugov_policy *sg_policy) static unsigned int get_next_freq(struct sugov_policy *sg_policy, unsigned long util, unsigned long max) { + unsigned int freq; struct cpufreq_policy *policy = sg_policy->policy; - unsigned int freq = arch_scale_freq_invariant() ? - policy->cpuinfo.max_freq : policy->cur; util = map_util_perf(util); + freq = arch_scale_freq_ref_policy(policy); freq = map_util_freq(util, freq, max); if (freq == sg_policy->cached_raw_freq && !sg_policy->need_freq_update) From patchwork Fri Sep 1 13:03:12 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Vincent Guittot X-Patchwork-Id: 719841 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 8D502CA0FE8 for ; Fri, 1 Sep 2023 13:03:31 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1349667AbjIANDc (ORCPT ); Fri, 1 Sep 2023 09:03:32 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46796 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1349642AbjIAND1 (ORCPT ); Fri, 1 Sep 2023 09:03:27 -0400 Received: from mail-wr1-x433.google.com (mail-wr1-x433.google.com [IPv6:2a00:1450:4864:20::433]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D9875E7E for ; Fri, 1 Sep 2023 06:03:24 -0700 (PDT) Received: by mail-wr1-x433.google.com with SMTP id ffacd0b85a97d-31c4d5bd69cso1659761f8f.3 for ; Fri, 01 Sep 2023 06:03:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1693573403; x=1694178203; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=V5aYZdSdgznaVi+JliScjcxNZSCQD8Th20BQ7P8lBSs=; b=hO6xfBmPKBgbuknFzUx9djhsOZtlMuq9HbhytH/CkX7n1a7K7Ppwgg3wSN8ElMwOn2 /FbUOQZdqcKdECl5sdrAJI50ai9l6fgV6X9+FiZXQyJ9rsO9k6M/q12SFwK/pS8XIg/I GY84P/+ugAHDXz5ZaSIiIpSCw0QOVMAm53MtNAfq+M4RvOHwwBOmhHB7L/21qrLWvheL ZqyCNDs+JcbhHxBeihuDCzU80XVQd74n5BJ1oblot4ZlKMW78OjjrsvGHwGMhixwGV5t VaPNz6qJyPgUbWrPKTDhNdOt1QsHiTXPMVxU9Ks/PqAYsLhu/NHB4i9cpjEjULOMufsa j2Gw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1693573403; x=1694178203; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=V5aYZdSdgznaVi+JliScjcxNZSCQD8Th20BQ7P8lBSs=; b=J3fb2vHJkntPtnVOLrmBIg15NISVinYW1zMpEhIfUkuypBqmsFLrzD8Uzu/frpAJtC 1aE+XZCnlkss7wz4UcomucEQleCUDgjsRuhTNwOcDPjr1FFLsalwyIh2eQRm1CBKaC6m 9cbFv8OzJ86CvOlNaVnLFJbAoh0MdekTXl7dHW8VjQ/bFEtlfXV0TqOZkislM+K3s93P Q3hNHug17CZ/Oawg9KK0TaxpNe7+I2d1bHg54d4bs14n0dhjbUdDOKxCbVatN6kQszwn T4YHvQLdQOKQQVfdYvSMDsWtD/vEDF4BPlVfFBbDbqsw4rxxMayqOTbkp/lWWqEPP7vZ xIMA== X-Gm-Message-State: AOJu0YzHotlET+fy+bjVP+2X9khtsZZOixJjos17IsCYCXjExiBDVzSQ CrDwm4HIuZEd5UtYOjP7ZuTT8w== X-Google-Smtp-Source: AGHT+IHWuDsPZBVeKEf+XTy2vPIdFmCGwE/gsGA3pylPfmUXuPXSa8EeuGjxUHsZSClfbNuuKWlS8Q== X-Received: by 2002:adf:e544:0:b0:31d:db92:31ae with SMTP id z4-20020adfe544000000b0031ddb9231aemr1715345wrm.44.1693573403268; Fri, 01 Sep 2023 06:03:23 -0700 (PDT) Received: from vingu-book.. ([2a01:e0a:f:6020:e9bd:add1:d9ac:7b3e]) by smtp.gmail.com with ESMTPSA id i14-20020adfdece000000b003142e438e8csm5167452wrn.26.2023.09.01.06.03.21 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 01 Sep 2023 06:03:22 -0700 (PDT) From: Vincent Guittot To: linux@armlinux.org.uk, catalin.marinas@arm.com, will@kernel.org, paul.walmsley@sifive.com, palmer@dabbelt.com, aou@eecs.berkeley.edu, sudeep.holla@arm.com, gregkh@linuxfoundation.org, rafael@kernel.org, mingo@redhat.com, peterz@infradead.org, juri.lelli@redhat.com, dietmar.eggemann@arm.com, rostedt@goodmis.org, bsegall@google.com, mgorman@suse.de, bristot@redhat.com, vschneid@redhat.com, viresh.kumar@linaro.org, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, linux-riscv@lists.infradead.org, linux-pm@vger.kernel.org Cc: conor.dooley@microchip.com, suagrfillet@gmail.com, ajones@ventanamicro.com, lftan@kernel.org, Vincent Guittot Subject: [PATCH 4/4] energy_model: use a fixed reference frequency Date: Fri, 1 Sep 2023 15:03:12 +0200 Message-Id: <20230901130312.247719-5-vincent.guittot@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230901130312.247719-1-vincent.guittot@linaro.org> References: <20230901130312.247719-1-vincent.guittot@linaro.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-pm@vger.kernel.org The last item of a performance domain is not always the performance point that has been used to compute CPU's capacity. This can lead to different target frequency compared with other part of the system like schedutil and would result in wrong energy estimation. a new arch_scale_freq_ref() is available to return a fixed and coherent frequency reference that can be used when computing the CPU's frequency for an level of utilization. Use this function when available or fallback to the last performance domain item otherwise. Signed-off-by: Vincent Guittot Reviewed-by: Lukasz Luba --- include/linux/energy_model.h | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/include/linux/energy_model.h b/include/linux/energy_model.h index b9caa01dfac4..7ee07be6928e 100644 --- a/include/linux/energy_model.h +++ b/include/linux/energy_model.h @@ -204,6 +204,20 @@ struct em_perf_state *em_pd_get_efficient_state(struct em_perf_domain *pd, return ps; } +#ifdef arch_scale_freq_ref +static __always_inline +unsigned long arch_scale_freq_ref_em(int cpu, struct em_perf_domain *pd) +{ + return arch_scale_freq_ref(cpu); +} +#else +static __always_inline +unsigned long arch_scale_freq_ref_em(int cpu, struct em_perf_domain *pd) +{ + return pd->table[pd->nr_perf_states - 1].frequency; +} +#endif + /** * em_cpu_energy() - Estimates the energy consumed by the CPUs of a * performance domain @@ -224,7 +238,7 @@ static inline unsigned long em_cpu_energy(struct em_perf_domain *pd, unsigned long max_util, unsigned long sum_util, unsigned long allowed_cpu_cap) { - unsigned long freq, scale_cpu; + unsigned long freq, ref_freq, scale_cpu; struct em_perf_state *ps; int cpu; @@ -241,11 +255,11 @@ static inline unsigned long em_cpu_energy(struct em_perf_domain *pd, */ cpu = cpumask_first(to_cpumask(pd->cpus)); scale_cpu = arch_scale_cpu_capacity(cpu); - ps = &pd->table[pd->nr_perf_states - 1]; + ref_freq = arch_scale_freq_ref_em(cpu, pd); max_util = map_util_perf(max_util); max_util = min(max_util, allowed_cpu_cap); - freq = map_util_freq(max_util, ps->frequency, scale_cpu); + freq = map_util_freq(max_util, ref_freq, scale_cpu); /* * Find the lowest performance state of the Energy Model above the