Received: by 2002:a05:7412:8d10:b0:f3:1519:9f41 with SMTP id bj16csp2026684rdb; Thu, 7 Dec 2023 16:24:57 -0800 (PST) X-Google-Smtp-Source: AGHT+IFx4Oj6JoG6Y/K5Vi02OfBlbgmEJCwr9P2doPA9kAfpGkQoA8On/A1ADnn1jfRWPzaDg/Ek X-Received: by 2002:a05:6358:63a7:b0:170:33e4:70d0 with SMTP id k39-20020a05635863a700b0017033e470d0mr5364151rwh.46.1701995096951; Thu, 07 Dec 2023 16:24:56 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1701995096; cv=none; d=google.com; s=arc-20160816; b=gO8Hf8Y0WTpFsbXBBUEIv9kvl8oxQdZt+CxSUeldc65bEh3l+2hfWpGTO4wRcK5EVC XcFyOJqOEBKpIrEKcTcszhi6cmnWFB7HOLdvRheGz8k3nZF8eA+eAqP1k06lGBZR4Bg0 wqAekx0tvhMQyfr5Pa3xGUTG+KXN+qB6dCt6p0tMv43bFGyXk1GUvWKPXl2RG5Z+Tl/o b+lGDzKxZJUdaRUavof3xj1M3wJgctPfN7UFSy5RqvIz2iV1HU+28lyct7HkJ+1hZC2e ljIzsYhulgHt1v8NJPaZ0viY7iOR7X5veC15fciE3zH3HcCSuIpPaBu8bE3In0jzSEy/ TB1g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=0nwlq0zhFu5FAStt4KBjg7mmh2j1Bzk1RkbICiq0xto=; fh=yvV4jsQVDgaqc4q7qs1OXQfyZz4GAcMjFnMHO0nsM7c=; b=upmDF6K7nxewJSoextgh0g9fmFqcfk3625BoZZvDHAI1AHE6+QuCHKRpuA531BQYll moCS/U7OtgSPzaCcRlW2R69IKtIgefjAGbdLvtqjSDCNOuOQIBroASnkMdrCFgy6CKRM SNF3g5jQ0AyCMkYHAHgTGTejgZV/prcgbCLVH5ZkbuyU/1QeTd7sVOIeNBQQ5t0lHZ9s RGHlBtiJbo2VLyCSZWPcUB6sKL3+zstPSBtPnGoPMk2UpNCPguxzDfvec2tKymVKSudT S6cqrtBgq2GbBReG3HrbNoUZSZbSx+JiYIVlLny8S0rWx0+NcMcOWitPazCuxiImL3aM 9hYA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@layalina-io.20230601.gappssmtp.com header.s=20230601 header.b=OMNtVLE8; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:2 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from agentk.vger.email (agentk.vger.email. [2620:137:e000::3:2]) by mx.google.com with ESMTPS id s1-20020a056a0008c100b006c6930e7540si540133pfu.121.2023.12.07.16.24.56 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 07 Dec 2023 16:24:56 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:2 as permitted sender) client-ip=2620:137:e000::3:2; Authentication-Results: mx.google.com; dkim=pass header.i=@layalina-io.20230601.gappssmtp.com header.s=20230601 header.b=OMNtVLE8; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:2 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by agentk.vger.email (Postfix) with ESMTP id BA5F7801C915; Thu, 7 Dec 2023 16:24:50 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at agentk.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235331AbjLHAYW (ORCPT + 99 others); Thu, 7 Dec 2023 19:24:22 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46890 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1444202AbjLHAYM (ORCPT ); Thu, 7 Dec 2023 19:24:12 -0500 Received: from mail-wr1-x436.google.com (mail-wr1-x436.google.com [IPv6:2a00:1450:4864:20::436]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 471811990 for ; Thu, 7 Dec 2023 16:24:17 -0800 (PST) Received: by mail-wr1-x436.google.com with SMTP id ffacd0b85a97d-3335397607dso1868493f8f.1 for ; Thu, 07 Dec 2023 16:24:17 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=layalina-io.20230601.gappssmtp.com; s=20230601; t=1701995056; x=1702599856; 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=0nwlq0zhFu5FAStt4KBjg7mmh2j1Bzk1RkbICiq0xto=; b=OMNtVLE8/L55t5RGpgoTuYD5hxcyHPgVChQ3+c9sHAS8QuIcAzROMrVPzL2nH5P74V O6EkctcoMVX8q1HOxPP1CqZZXp+gAQ5PwhBzs2T1AdK3yyGZ7DrplrlwwlCj7Tut+O/K kG/k03zv8NL9cxG1nylXz05v1lMjSlQ2YffIumgm5HAwedQf6AeQxlXyF9ZeHf9OcEk8 dSNToUI4qfS0/Co6X6FgQQidon1j2XdH86XESsCn0P/lePbMhxxstB+3bj0B8Ofcobww sehJjTlBxV8lOU7E0U0lqgcvAbfk7N0G7+CesMsVsj1aYwmCNI5BiN1D8Wq51s0GfIuQ GTaA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1701995056; x=1702599856; 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=0nwlq0zhFu5FAStt4KBjg7mmh2j1Bzk1RkbICiq0xto=; b=Uofr9N2ltuBE4SK1NW+JyJMqWIE76z5eJMYeNDEm8bKwiPWv1RDrbyoPspvU3yAcyd TnA6pa2psH9RhmI9zyH1P603Gpya+bLPrV3mlSYIq5S0ziAhPaLt4Nv5wIBAZ1R16NLS J/qpdoCs8XZwNvqYPvG5I6C2hPj3ApfGJzsETHjdv2EHo01Ugx+5EpdPDs/eJviGz9az Fb/+7hRZy5mLLIk1iLVeyebu2/nPRyiId3VwoyTTVn1Xx2vpaEhlmB7TfJ3mx/2YLikq 7m+dNshXOk47SMAtOYD5/ILNzgO3ZCSajJzKmmEOnAY1ZzCmTkRFzUdN9BdPc6uN0hBv RzAA== X-Gm-Message-State: AOJu0Yy1+UEP10aSmC2nGqMEJNwxzJR9ieEEpRAev67kI72spiIU3E4w bf/aV2T5YncTqPoCYyEoZNh6+w== X-Received: by 2002:a05:600c:45cf:b0:401:bdd7:49ae with SMTP id s15-20020a05600c45cf00b00401bdd749aemr1943026wmo.18.1701995055118; Thu, 07 Dec 2023 16:24:15 -0800 (PST) Received: from airbuntu.. (host109-153-232-45.range109-153.btcentralplus.com. [109.153.232.45]) by smtp.gmail.com with ESMTPSA id u17-20020a05600c19d100b0040c1c269264sm3339653wmq.40.2023.12.07.16.24.14 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 07 Dec 2023 16:24:14 -0800 (PST) From: Qais Yousef To: Ingo Molnar , Peter Zijlstra , "Rafael J. Wysocki" , Viresh Kumar , Vincent Guittot , Dietmar Eggemann Cc: linux-kernel@vger.kernel.org, linux-pm@vger.kernel.org, Lukasz Luba , Wei Wang , Rick Yiu , Chung-Kai Mei , Qais Yousef Subject: [PATCH v2 6/8] sched: cpufreq: Remove magic 1.25 headroom from apply_dvfs_headroom() Date: Fri, 8 Dec 2023 00:23:40 +0000 Message-Id: <20231208002342.367117-7-qyousef@layalina.io> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20231208002342.367117-1-qyousef@layalina.io> References: <20231208002342.367117-1-qyousef@layalina.io> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-0.8 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,SPF_HELO_NONE, SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on agentk.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (agentk.vger.email [0.0.0.0]); Thu, 07 Dec 2023 16:24:51 -0800 (PST) Replace 1.25 headroom in apply_dvfs_headroom() with better dynamic logic. Instead of the magical 1.25 headroom, use the new approximate_util_avg() to provide headroom based on the dvfs_update_delay; which is the period at which the cpufreq governor will send DVFS updates to the hardware. Add a new percpu dvfs_update_delay that can be cheaply accessed whenever apply_dvfs_headroom() is called. We expect cpufreq governors that rely on util to drive its DVFS logic/algorithm to populate these percpu variables. schedutil is the only such governor at the moment. The behavior of schedutil will change as the headroom will be less than 1.25 for most systems as the rate_limit_us is usually short. Signed-off-by: Qais Yousef (Google) --- kernel/sched/core.c | 1 + kernel/sched/cpufreq_schedutil.c | 13 +++++++++++-- kernel/sched/sched.h | 18 ++++++++++++++---- 3 files changed, 26 insertions(+), 6 deletions(-) diff --git a/kernel/sched/core.c b/kernel/sched/core.c index db4be4921e7f..b4a1c8ea9e12 100644 --- a/kernel/sched/core.c +++ b/kernel/sched/core.c @@ -116,6 +116,7 @@ EXPORT_TRACEPOINT_SYMBOL_GPL(sched_update_nr_running_tp); EXPORT_TRACEPOINT_SYMBOL_GPL(sched_compute_energy_tp); DEFINE_PER_CPU_SHARED_ALIGNED(struct rq, runqueues); +DEFINE_PER_CPU_READ_MOSTLY(u64, dvfs_update_delay); #ifdef CONFIG_SCHED_DEBUG /* diff --git a/kernel/sched/cpufreq_schedutil.c b/kernel/sched/cpufreq_schedutil.c index 79c3b96dc02c..1d4d6025c15f 100644 --- a/kernel/sched/cpufreq_schedutil.c +++ b/kernel/sched/cpufreq_schedutil.c @@ -157,7 +157,7 @@ unsigned long sugov_effective_cpu_perf(int cpu, unsigned long actual, unsigned long max) { /* Add dvfs headroom to actual utilization */ - actual = apply_dvfs_headroom(actual); + actual = apply_dvfs_headroom(actual, cpu); /* Actually we don't need to target the max performance */ if (actual < max) max = actual; @@ -535,15 +535,21 @@ rate_limit_us_store(struct gov_attr_set *attr_set, const char *buf, size_t count struct sugov_tunables *tunables = to_sugov_tunables(attr_set); struct sugov_policy *sg_policy; unsigned int rate_limit_us; + int cpu; if (kstrtouint(buf, 10, &rate_limit_us)) return -EINVAL; tunables->rate_limit_us = rate_limit_us; - list_for_each_entry(sg_policy, &attr_set->policy_list, tunables_hook) + list_for_each_entry(sg_policy, &attr_set->policy_list, tunables_hook) { + sg_policy->freq_update_delay_ns = rate_limit_us * NSEC_PER_USEC; + for_each_cpu(cpu, sg_policy->policy->cpus) + per_cpu(dvfs_update_delay, cpu) = rate_limit_us; + } + return count; } @@ -824,6 +830,9 @@ static int sugov_start(struct cpufreq_policy *policy) memset(sg_cpu, 0, sizeof(*sg_cpu)); sg_cpu->cpu = cpu; sg_cpu->sg_policy = sg_policy; + + per_cpu(dvfs_update_delay, cpu) = sg_policy->tunables->rate_limit_us; + cpufreq_add_update_util_hook(cpu, &sg_cpu->update_util, uu); } return 0; diff --git a/kernel/sched/sched.h b/kernel/sched/sched.h index 2de64f59853c..bbece0eb053a 100644 --- a/kernel/sched/sched.h +++ b/kernel/sched/sched.h @@ -3005,6 +3005,15 @@ unsigned long sugov_effective_cpu_perf(int cpu, unsigned long actual, unsigned long approximate_util_avg(unsigned long util, u64 delta); u64 approximate_runtime(unsigned long util); +/* + * Any governor that relies on util signal to drive DVFS, must populate these + * percpu dvfs_update_delay variables. + * + * It should describe the rate/delay at which the governor sends DVFS freq + * update to the hardware in us. + */ +DECLARE_PER_CPU_READ_MOSTLY(u64, dvfs_update_delay); + /* * DVFS decision are made at discrete points. If CPU stays busy, the util will * continue to grow, which means it could need to run at a higher frequency @@ -3014,13 +3023,14 @@ u64 approximate_runtime(unsigned long util); * to run at adequate performance point. * * This function provides enough headroom to provide adequate performance - * assuming the CPU continues to be busy. + * assuming the CPU continues to be busy. This headroom is based on the + * dvfs_update_delay of the cpufreq governor. * - * At the moment it is a constant multiplication with 1.25. + * XXX: Should we provide headroom when the util is decaying? */ -static inline unsigned long apply_dvfs_headroom(unsigned long util) +static inline unsigned long apply_dvfs_headroom(unsigned long util, int cpu) { - return util + (util >> 2); + return approximate_util_avg(util, per_cpu(dvfs_update_delay, cpu)); } /* -- 2.34.1