[PATCH v3 05/10] sched: cpufreq: Remove magic 1.25 headroom from sugov_apply_dvfs_headroom()
From: Qais Yousef
Date: Thu Nov 26 2026 - 00:15:02 EST
Replace 1.25 headroom in sugov_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, or
TICK_USEC which is the max delay for util signal to change and promote
a cpufreq update; whichever is higher.
Add a new percpu dvfs_update_delay that can be cheaply accessed whenever
sugov_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. Some systems will experience
faster dvfs rampup (because of higher TICK or rate_limit_us), others
will experience slower rampup.
The impact on performance should not be visible if not for the black
hole effect of utilization invariance. A problem that will be addressed
in later patches.
CONST_DVFS_HEADROOM sched_feat allows reverting back to the old behavior
for easy backward compatibility.
Signed-off-by: Qais Yousef <qyousef@xxxxxxxxxxx>
---
kernel/sched/core.c | 1 +
kernel/sched/cpufreq_schedutil.c | 31 +++++++++++++++++++++++++------
kernel/sched/features.h | 6 ++++++
kernel/sched/sched.h | 9 +++++++++
4 files changed, 41 insertions(+), 6 deletions(-)
diff --git a/kernel/sched/core.c b/kernel/sched/core.c
index 5dd53cef22117..97d3cceba22c0 100644
--- a/kernel/sched/core.c
+++ b/kernel/sched/core.c
@@ -128,6 +128,7 @@ EXPORT_TRACEPOINT_SYMBOL_GPL(sched_dl_server_start_tp);
EXPORT_TRACEPOINT_SYMBOL_GPL(sched_dl_server_stop_tp);
DEFINE_PER_CPU_SHARED_ALIGNED(struct rq, runqueues);
+DEFINE_PER_CPU_READ_MOSTLY(u64, dvfs_update_delay);
DEFINE_PER_CPU(struct rnd_state, sched_rnd_state);
#ifdef CONFIG_SCHED_PROXY_EXEC
diff --git a/kernel/sched/cpufreq_schedutil.c b/kernel/sched/cpufreq_schedutil.c
index 4d781b394baa8..04d0182759609 100644
--- a/kernel/sched/cpufreq_schedutil.c
+++ b/kernel/sched/cpufreq_schedutil.c
@@ -239,13 +239,25 @@ static unsigned int get_next_freq(struct sugov_policy *sg_policy,
* 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 or TICK_USEC, whichever is higher.
*
- * At the moment it is a constant multiplication with 1.25.
+ * Ideally we should pick max(dvfs_updae_delay, min(curr.se.slice, TICK_USEC)),
+ * but reading curr.se.slice requires holding the rq_lock which is hard/not
+ * feasible.
+ *
+ * XXX: Should we provide headroom when the util is decaying?
*/
-static inline unsigned long sugov_apply_dvfs_headroom(unsigned long util)
+static inline unsigned long sugov_apply_dvfs_headroom(unsigned long util, int cpu)
{
- return util + (util >> 2);
+ u64 delay;
+
+ if (sched_feat(CONST_DVFS_HEADROOM))
+ return util + (util >> 2);
+
+ delay = max(TICK_USEC, per_cpu(dvfs_update_delay, cpu));
+
+ return approximate_util_avg(util, delay);
}
unsigned long sugov_effective_cpu_perf(int cpu, unsigned long actual,
@@ -253,7 +265,7 @@ unsigned long sugov_effective_cpu_perf(int cpu, unsigned long actual,
unsigned long max)
{
/* Add dvfs headroom to actual utilization */
- actual = sugov_apply_dvfs_headroom(actual);
+ actual = sugov_apply_dvfs_headroom(actual, cpu);
/* Actually we don't need to target the max performance */
if (actual < max)
max = actual;
@@ -642,15 +654,20 @@ 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) {
sugov_update_rate_limit_us(sg_policy);
+ for_each_cpu(cpu, sg_policy->policy->cpus)
+ per_cpu(dvfs_update_delay, cpu) = rate_limit_us;
+ }
+
return count;
}
@@ -913,6 +930,8 @@ 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;
}
/*
diff --git a/kernel/sched/features.h b/kernel/sched/features.h
index 8f0dee8fc4755..500ece3e341fe 100644
--- a/kernel/sched/features.h
+++ b/kernel/sched/features.h
@@ -142,3 +142,9 @@ SCHED_FEAT(LATENCY_WARN, false)
*/
SCHED_FEAT(NI_RANDOM, true)
SCHED_FEAT(NI_RATE, true)
+
+/*
+ * For backward compatibility. Use the constant 1.25 dvfs headroom in
+ * schedutil instead of the dynamic one.
+ */
+SCHED_FEAT(CONST_DVFS_HEADROOM, false)
diff --git a/kernel/sched/sched.h b/kernel/sched/sched.h
index f4e6e85d45a07..27c18701de35c 100644
--- a/kernel/sched/sched.h
+++ b/kernel/sched/sched.h
@@ -3806,6 +3806,15 @@ static inline void update_fits_capacity_threshold(int cpu)
rq->fits_capacity_threshold = approximate_util_avg(0, limit);
}
+/*
+ * 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);
+
/*
* Verify the fitness of task @p to run on @cpu taking into account the
* CPU original capacity and the runtime/deadline ratio of the task.
--
2.53.0