[PATCH 04/19] sched/fair: Introduce a static key to enable cache aware only for multi LLCs

From: Tim Chen

Date: Sat Oct 11 2025 - 14:18:50 EST


From: Chen Yu <yu.c.chen@xxxxxxxxx>

Enable cache-aware load balancing only if at least 1 NUMA node has
more than one LLC.

Suggested-by: Libo Chen <libo.chen@xxxxxxxxxx>
Suggested-by: Adam Li <adamli@xxxxxxxxxxxxxxxxxxxxxx>
Signed-off-by: Chen Yu <yu.c.chen@xxxxxxxxx>
Signed-off-by: Tim Chen <tim.c.chen@xxxxxxxxxxxxxxx>
---
kernel/sched/fair.c | 15 ++++++++++++---
kernel/sched/sched.h | 1 +
kernel/sched/topology.c | 14 ++++++++++++--
3 files changed, 25 insertions(+), 5 deletions(-)

diff --git a/kernel/sched/fair.c b/kernel/sched/fair.c
index cd080468ddc9..3d643449c48c 100644
--- a/kernel/sched/fair.c
+++ b/kernel/sched/fair.c
@@ -1208,6 +1208,14 @@ static s64 update_se(struct rq *rq, struct sched_entity *se)
__read_mostly unsigned int llc_overload_pct = 50;
__read_mostly unsigned int llc_imb_pct = 20;

+DEFINE_STATIC_KEY_FALSE(sched_cache_allowed);
+
+static inline bool sched_cache_enabled(void)
+{
+ return sched_feat(SCHED_CACHE) &&
+ static_branch_likely(&sched_cache_allowed);
+}
+
static int llc_id(int cpu)
{
if (cpu < 0)
@@ -1294,7 +1302,7 @@ void account_mm_sched(struct rq *rq, struct task_struct *p, s64 delta_exec)
struct mm_sched *pcpu_sched;
unsigned long epoch;

- if (!sched_feat(SCHED_CACHE))
+ if (!sched_cache_enabled())
return;

if (p->sched_class != &fair_sched_class)
@@ -1330,7 +1338,7 @@ static void task_tick_cache(struct rq *rq, struct task_struct *p)
struct callback_head *work = &p->cache_work;
struct mm_struct *mm = p->mm;

- if (!sched_feat(SCHED_CACHE))
+ if (!sched_cache_enabled())
return;

if (!mm || !mm->pcpu_sched)
@@ -10716,7 +10724,8 @@ static void record_sg_llc_stats(struct lb_env *env,
struct sched_domain *sd = env->sd->child;
struct sched_domain_shared *sd_share;

- if (!sched_feat(SCHED_CACHE) || env->idle == CPU_NEWLY_IDLE)
+ if (!sched_cache_enabled() ||
+ env->idle == CPU_NEWLY_IDLE)
return;

/* only care about sched domains spanning a LLC */
diff --git a/kernel/sched/sched.h b/kernel/sched/sched.h
index a52c96064b36..60f1e51685ec 100644
--- a/kernel/sched/sched.h
+++ b/kernel/sched/sched.h
@@ -2800,6 +2800,7 @@ extern unsigned int sysctl_numa_balancing_hot_threshold;
#ifdef CONFIG_SCHED_CACHE
extern unsigned int llc_overload_pct;
extern unsigned int llc_imb_pct;
+extern struct static_key_false sched_cache_allowed;
#endif

#ifdef CONFIG_SCHED_HRTICK
diff --git a/kernel/sched/topology.c b/kernel/sched/topology.c
index 6e2f54169e66..2675db980f70 100644
--- a/kernel/sched/topology.c
+++ b/kernel/sched/topology.c
@@ -2444,6 +2444,7 @@ static int
build_sched_domains(const struct cpumask *cpu_map, struct sched_domain_attr *attr)
{
enum s_alloc alloc_state = sa_none;
+ bool has_multi_llcs = false;
struct sched_domain *sd;
struct s_data d;
struct rq *rq = NULL;
@@ -2530,10 +2531,12 @@ build_sched_domains(const struct cpumask *cpu_map, struct sched_domain_attr *att
* between LLCs and memory channels.
*/
nr_llcs = sd->span_weight / child->span_weight;
- if (nr_llcs == 1)
+ if (nr_llcs == 1) {
imb = sd->span_weight >> 3;
- else
+ } else {
imb = nr_llcs;
+ has_multi_llcs = true;
+ }
imb = max(1U, imb);
sd->imb_numa_nr = imb;

@@ -2581,6 +2584,13 @@ build_sched_domains(const struct cpumask *cpu_map, struct sched_domain_attr *att
if (has_cluster)
static_branch_inc_cpuslocked(&sched_cluster_active);

+#ifdef CONFIG_SCHED_CACHE
+ if (has_multi_llcs) {
+ static_branch_enable_cpuslocked(&sched_cache_allowed);
+ pr_info("Cache aware load balance enabled.\n");
+ }
+#endif
+
if (rq && sched_debug_verbose)
pr_info("root domain span: %*pbl\n", cpumask_pr_args(cpu_map));

--
2.32.0