[RFC PATCH 4/7] sched/cache: Converge task accounting to the affinity root
From: Luo Gengkun
Date: Thu Nov 26 2026 - 08:16:35 EST
Merging two affinity groups only sets the loser's parent: the tasks of
the loser keep accounting in their old group. Add
sched_grp_refresh(), which walks a task's merge links up to the
root and switches the task to it, so the entire affinity group ends up
accounted in a single group.
The convergence is lazy, triggered at the task's next accounting: a
task outside any affinity group is untouched, while a task whose
affinity group has not moved pays only a type read and a parent read.
account_mm_sched() converges before accounting, and task_cache_work()
does the same; the walk and switch happen at most once per member per
merge.
Signed-off-by: Luo Gengkun <luogengkun@xxxxxxxxxxxxxxx>
---
include/linux/sched.h | 3 +++
kernel/sched/cache_sched.c | 54 ++++++++++++++++++++++++++++++++++++++
kernel/sched/fair.c | 16 +++++++++++
3 files changed, 73 insertions(+)
diff --git a/include/linux/sched.h b/include/linux/sched.h
index de270443cffa..769e2304f617 100644
--- a/include/linux/sched.h
+++ b/include/linux/sched.h
@@ -2442,6 +2442,9 @@ void sched_cache_exit_mm(struct task_struct *p);
int sched_cache_hint(struct task_struct *t1, struct task_struct *t2);
+struct sched_cache_group *sched_grp_refresh(struct task_struct *p,
+ struct sched_cache_group *grp);
+
#else
struct sched_cache_group { };
diff --git a/kernel/sched/cache_sched.c b/kernel/sched/cache_sched.c
index 52106c3ac049..c5fce997c114 100644
--- a/kernel/sched/cache_sched.c
+++ b/kernel/sched/cache_sched.c
@@ -468,3 +468,57 @@ int sched_cache_hint(struct task_struct *t1, struct task_struct *t2)
}
return ret;
}
+
+/*
+ * Converge @p to the group its affinity group accounts in, and return the
+ * group the caller should account in. A task outside any affinity group
+ * keeps its group, i.e. the default thread-group behaviour.
+ *
+ * @p is always the task running on the rq being accounted, but the caller
+ * need not run on that CPU: a remote wakeup's enqueue or a bandwidth
+ * unthrottle calls account_mm_sched() on rq->curr under that rq's lock, so
+ * this can converge @p while @p itself is in exec_mmap() or exit_mm() on
+ * its own CPU. The move is therefore a cmpxchg, mirrored by
+ * sched_cache_replace_grp() and scg_get_or_alloc_affinity(): whoever
+ * loses drops the reference it just took and converges again at the next
+ * accounting.
+ */
+struct sched_cache_group *sched_grp_refresh(struct task_struct *p,
+ struct sched_cache_group *grp)
+{
+ struct sched_cache_group *new_root;
+
+ /* Outside any affinity group: keeps the mm's group, nothing to do. */
+ if (!grp || READ_ONCE(grp->type) != CACHE_GROUP_AFFINITY)
+ return grp;
+
+ scoped_guard(rcu) {
+ /* Still the root: the common case. */
+ if (!rcu_dereference(grp->parent))
+ return grp;
+
+ /* The group we account in was merged away: follow the links. */
+ new_root = scg_find_root(grp);
+ /*
+ * Pinned by the reference @p holds on @grp: it keeps the whole
+ * chain above alive, and an RCU-pending @grp holds its edge on
+ * the parent until its free callback, so attaching cannot fail
+ * here.
+ */
+ if (!sched_cache_group_attach(new_root))
+ return grp;
+ /*
+ * Move @p's reference to the new root. Losing means @p moved
+ * on (exec installed a new mm's group, exit cleared it) after
+ * the caller read @grp: give back the membership and the
+ * reference just taken, the winner's is its own.
+ */
+ if (!try_cmpxchg(&p->sched_cache_grp, &grp, new_root)) {
+ sched_cache_group_detach(new_root);
+ return grp;
+ }
+ sched_cache_group_detach(grp); /* nr_threads + the field's reference */
+ }
+
+ return new_root;
+}
diff --git a/kernel/sched/fair.c b/kernel/sched/fair.c
index f5610cbc76ec..5be8a7c7076e 100644
--- a/kernel/sched/fair.c
+++ b/kernel/sched/fair.c
@@ -1788,6 +1788,14 @@ void account_mm_sched(struct rq *rq, struct task_struct *p, s64 delta_exec)
if (p->flags & PF_KTHREAD || !grp || !grp->pcpu_sched)
return;
+ /*
+ * Converge to the group the task's affinity group accounts in
+ * before accounting into it; a no-op outside any affinity group.
+ */
+ grp = sched_grp_refresh(p, grp);
+ if (!grp || !grp->pcpu_sched)
+ return;
+
pcpu_sched = per_cpu_ptr(grp->pcpu_sched, cpu_of(rq));
scoped_guard (raw_spinlock, &rq->cpu_epoch_lock) {
@@ -1912,6 +1920,7 @@ static inline void update_avg_scale(u64 *avg, u64 sample)
static void task_cache_work(struct callback_head *work)
{
struct sched_cache_group *grp __free(sched_cache_group_put) = NULL;
+ struct sched_cache_group *new_grp;
cpumask_var_t cpus __free(free_cpumask_var) = CPUMASK_VAR_NULL;
int cpu, m_a_cpu = -1, nr_running = 0, curr_cpu;
unsigned long next_scan, now = jiffies;
@@ -1934,6 +1943,13 @@ static void task_cache_work(struct callback_head *work)
grp = task_cache_group_get(p);
if (!grp)
return;
+ new_grp = sched_grp_refresh(p, grp);
+ if (new_grp != grp) {
+ sched_cache_group_put(grp);
+ grp = sched_cache_group_get(new_grp);
+ if (!grp)
+ return;
+ }
next_scan = READ_ONCE(grp->next_scan);
if (time_before(now, next_scan))
--
2.34.1