[RFC patch v3 11/20] sched: Introduce per runqueue task LLC preference counter

From: Tim Chen
Date: Wed Jun 18 2025 - 14:23:45 EST


Each runqueue is assigned a static array, where each element indicates
the number of tasks preferring a particular LLC mapped to the
array index.

For example, rq->nr_pref_llc[3] = 2 signifies that there are 2 tasks on
this runqueue which prefer to run within LLC3 (indexed from 0 to MAX_LLC
across the entire system). With this information, the load balancer can
make better decisions to select the busiest runqueue and migrate tasks
to their preferred LLC domains.

Note: The static array could be converted to an xarray in the future.

Signed-off-by: Tim Chen <tim.c.chen@xxxxxxxxxxxxxxx>
---
kernel/sched/fair.c | 36 +++++++++++++++++++++++++++++++++++-
kernel/sched/sched.h | 1 +
2 files changed, 36 insertions(+), 1 deletion(-)

diff --git a/kernel/sched/fair.c b/kernel/sched/fair.c
index 88ff47194faa..ba62b445bbbb 100644
--- a/kernel/sched/fair.c
+++ b/kernel/sched/fair.c
@@ -1195,16 +1195,45 @@ static inline int llc_idx(int cpu)
return per_cpu(sd_llc_idx, cpu);
}

+static inline int pref_llc_idx(struct task_struct *p)
+{
+ return llc_idx(p->preferred_llc);
+}
+
static void account_llc_enqueue(struct rq *rq, struct task_struct *p)
{
+ int pref_llc;
+
rq->nr_llc_running += (p->preferred_llc != -1);
rq->nr_pref_llc_running += (p->preferred_llc == task_llc(p));
+
+ if (p->preferred_llc < 0)
+ return;
+
+ pref_llc = pref_llc_idx(p);
+ if (pref_llc < 0)
+ return;
+
+ ++rq->nr_pref_llc[pref_llc];
}

static void account_llc_dequeue(struct rq *rq, struct task_struct *p)
{
+ int pref_llc;
+
rq->nr_llc_running -= (p->preferred_llc != -1);
rq->nr_pref_llc_running -= (p->preferred_llc == task_llc(p));
+
+ if (p->preferred_llc < 0)
+ return;
+
+ pref_llc = pref_llc_idx(p);
+ if (pref_llc < 0)
+ return;
+
+ /* avoid negative counter */
+ if (rq->nr_pref_llc[pref_llc] > 0)
+ --rq->nr_pref_llc[pref_llc];
}

void mm_init_sched(struct mm_struct *mm, struct mm_sched __percpu *_pcpu_sched)
@@ -1417,8 +1446,13 @@ void init_sched_mm(struct task_struct *p)

void reset_llc_stats(struct rq *rq)
{
- if (rq->nr_llc_running)
+ int i;
+
+ if (rq->nr_llc_running) {
+ for (i = 0; i < MAX_LLC; ++i)
+ rq->nr_pref_llc[i] = 0;
rq->nr_llc_running = 0;
+ }

rq->nr_pref_llc_running = 0;
}
diff --git a/kernel/sched/sched.h b/kernel/sched/sched.h
index 6c83a71ac8ca..391ddc0195f8 100644
--- a/kernel/sched/sched.h
+++ b/kernel/sched/sched.h
@@ -1107,6 +1107,7 @@ struct rq {
#ifdef CONFIG_SCHED_CACHE
unsigned int nr_pref_llc_running;
unsigned int nr_llc_running;
+ unsigned int nr_pref_llc[MAX_LLC];
#endif
#ifdef CONFIG_NO_HZ_COMMON
#ifdef CONFIG_SMP
--
2.32.0