[PATCH v3 04/10] sched/fair: Remove magic hardcoded margin in fits_capacity()
From: Qais Yousef
Date: Thu Nov 26 2026 - 00:14:57 EST
Replace hardcoded margin value in fits_capacity() with better dynamic
logic.
80% margin is a magic value that has served its purpose for now, but it
no longer fits the variety of systems that exist today. If a system is
over powered specifically, this 80% will mean we leave a lot of capacity
unused before we decide to upmigrate on HMP system.
On many systems the little cores are under powered and the ability to
migrate faster away from them is desired.
Redefine misfit migration to mean the utilization threshold at which the
task would become misfit at the next tick event assuming it becomes an
always running task.
To calculate this threshold, we use the new approximate_util_avg()
function to find out the threshold, based on arch_scale_cpu_capacity()
the task will be misfit if it continues to run for a TICK_USEC which is
our worst case scenario for when misfit migration will kick in.
Signed-off-by: Qais Yousef <qyousef@xxxxxxxxxxx>
---
kernel/sched/core.c | 1 +
kernel/sched/debug.c | 10 +++++++++-
kernel/sched/fair.c | 15 +++++++++------
kernel/sched/sched.h | 26 ++++++++++++++++++++++++++
kernel/sched/topology.c | 1 +
5 files changed, 46 insertions(+), 7 deletions(-)
diff --git a/kernel/sched/core.c b/kernel/sched/core.c
index ea89471d4743c..5dd53cef22117 100644
--- a/kernel/sched/core.c
+++ b/kernel/sched/core.c
@@ -9071,6 +9071,7 @@ void __init sched_init(void)
RCU_INIT_POINTER(rq->sd, NULL);
RCU_INIT_POINTER(rq->rd, NULL);
rq->cpu_capacity = SCHED_CAPACITY_SCALE;
+ rq->fits_capacity_threshold = SCHED_CAPACITY_SCALE;
rq->balance_callback = &balance_push_callback;
rq->active_balance = 0;
rq->next_balance = jiffies;
diff --git a/kernel/sched/debug.c b/kernel/sched/debug.c
index 869bea87d2ce0..94e65208807dd 100644
--- a/kernel/sched/debug.c
+++ b/kernel/sched/debug.c
@@ -736,7 +736,8 @@ static __init void debugfs_cpu_init(void)
static __init int sched_init_debug(void)
{
- struct dentry __maybe_unused *numa, *llc;
+ struct dentry __maybe_unused *numa, *llc, *fct;
+ int cpu;
debugfs_sched = debugfs_create_dir("sched", NULL);
@@ -785,6 +786,13 @@ static __init int sched_init_debug(void)
&llc_imb_pct);
#endif
+ fct = debugfs_create_dir("fits_capacity_threshold", debugfs_sched);
+ for_each_possible_cpu(cpu) {
+ char name[16] = {};
+ sprintf(name, "cpu%d", cpu);
+ debugfs_create_ulong(name, 0644, fct, &cpu_rq(cpu)->fits_capacity_threshold);
+ }
+
debugfs_create_file("debug", 0444, debugfs_sched, NULL, &sched_debug_fops);
#ifdef CONFIG_FAIR_GROUP_SCHED
diff --git a/kernel/sched/fair.c b/kernel/sched/fair.c
index 731d78155e654..e92b923b769ac 100644
--- a/kernel/sched/fair.c
+++ b/kernel/sched/fair.c
@@ -103,11 +103,15 @@ int __weak arch_asym_cpu_priority(int cpu)
}
/*
- * The margin used when comparing utilization with CPU capacity.
- *
- * (default: ~20%)
+ * fits_capacity() must ensure that a task will not be 'stuck' on a CPU with
+ * lower capacity for too long. This threshold is the util value at which if
+ * a task becomes always busy it could miss misfit migration load balance
+ * event. So we consider a task is misfit before it reaches this point.
*/
-#define fits_capacity(cap, max) ((cap) * 1280 < (max) * 1024)
+static inline bool fits_capacity(unsigned long util, int cpu)
+{
+ return util < cpu_rq(cpu)->fits_capacity_threshold;
+}
/*
* The margin used when comparing CPU capacities.
@@ -6213,14 +6217,13 @@ static inline int util_fits_cpu(unsigned long util,
unsigned long uclamp_max,
int cpu)
{
- unsigned long capacity = capacity_of(cpu);
unsigned long capacity_orig;
bool fits, uclamp_max_fits;
/*
* Check if the real util fits without any uclamp boost/cap applied.
*/
- fits = fits_capacity(util, capacity);
+ fits = fits_capacity(util, cpu);
if (!uclamp_is_used())
return fits;
diff --git a/kernel/sched/sched.h b/kernel/sched/sched.h
index 50cf07c364727..f4e6e85d45a07 100644
--- a/kernel/sched/sched.h
+++ b/kernel/sched/sched.h
@@ -1324,6 +1324,7 @@ struct rq {
unsigned char nohz_idle_balance;
unsigned char idle_balance;
+ unsigned long fits_capacity_threshold;
unsigned long misfit_task_load;
/* For active balancing */
@@ -3780,6 +3781,31 @@ 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);
+static inline void update_fits_capacity_threshold(int cpu)
+{
+ struct rq *rq = cpu_rq(cpu);
+ u64 limit;
+
+ /*
+ * Calculate the util at which the task must be considered a misfit.
+ *
+ * We must ensure that a task experiences the same ramp-up time to
+ * reach max performance point of the system regardless of the CPU it
+ * is running on (due to invariance, time will stretch and task will
+ * take longer to achieve the same util value compared to a task
+ * running on a big CPU) and a delay in misfit migration which depends
+ * on TICK doesn't end up hurting it as it can happen after we would
+ * have crossed this threshold.
+ *
+ * If capacity is too small that limit is less than TICK_USEC, this is
+ * bad configuration and we'd have to skip this CPU always from
+ * performance point of view.
+ */
+ limit = approximate_runtime(arch_scale_cpu_capacity(cpu));
+ limit = limit > TICK_USEC ? limit - TICK_USEC : 0;
+ rq->fits_capacity_threshold = approximate_util_avg(0, limit);
+}
+
/*
* 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.
diff --git a/kernel/sched/topology.c b/kernel/sched/topology.c
index 99878da03bce7..53ba488955a28 100644
--- a/kernel/sched/topology.c
+++ b/kernel/sched/topology.c
@@ -842,6 +842,7 @@ cpu_attach_domain(struct sched_domain *sd, struct root_domain *rd, int cpu)
destroy_sched_domains(tmp);
update_top_cache_domain(cpu);
+ update_fits_capacity_threshold(cpu);
}
struct s_data {
--
2.53.0