[RFC PATCH 2/7] sched/cache: Add thread count to cache group

From: Luo Gengkun

Date: Thu Nov 26 2026 - 08:16:27 EST


Every cache group belongs to an mm today, so get_nr_threads() is a
reasonable stand-in for the group's member count. Later patches in
this series will introduce cache groups that no mm owns, and
get_nr_threads() cannot tell whether such a group is valid.

So add an atomic nr_threads counter to struct sched_cache_group and
maintain it through fork, exec, exit and failed-fork cleanup.
invalid_llc_nr() switches to grp->nr_threads, an exact count of the
tasks that reference the group.

Also add a WARN_ON_ONCE in sched_cache_group_free_rcu() so any leak is
caught when the group is finally released: a group being freed has no
members left, so a non-zero count means an attach/detach pairing bug.

Signed-off-by: Luo Gengkun <luogengkun@xxxxxxxxxxxxxxx>
---
include/linux/sched.h | 1 +
kernel/sched/cache_sched.c | 59 ++++++++++++++++++++++++++++++++------
kernel/sched/cache_sched.h | 1 +
kernel/sched/fair.c | 14 ++++-----
4 files changed, 59 insertions(+), 16 deletions(-)

diff --git a/include/linux/sched.h b/include/linux/sched.h
index d35ae49a991f..3073ed421b58 100644
--- a/include/linux/sched.h
+++ b/include/linux/sched.h
@@ -2415,6 +2415,7 @@ struct sched_cache_group {
unsigned long footprint;
int cpu;
refcount_t refcnt;
+ atomic_t nr_threads;
struct rcu_head rcu;
} ____cacheline_aligned_in_smp;

diff --git a/kernel/sched/cache_sched.c b/kernel/sched/cache_sched.c
index c20ed59ba45b..e1562b6af0bc 100644
--- a/kernel/sched/cache_sched.c
+++ b/kernel/sched/cache_sched.c
@@ -20,6 +20,7 @@ static void sched_cache_group_free_rcu(struct rcu_head *rcu)
struct sched_cache_group *grp =
container_of(rcu, struct sched_cache_group, rcu);

+ WARN_ON_ONCE(atomic_read(&grp->nr_threads));
free_percpu(grp->pcpu_sched);
kfree(grp);
}
@@ -54,26 +55,60 @@ struct sched_cache_group *task_cache_group_get(struct task_struct *p)
return sched_cache_group_get(rcu_dereference(p->sched_cache_grp));
}

+/*
+ * Become a member of @grp and take one reference on it. The reference
+ * returned is the caller's: publishing @grp into a task's sched_cache_grp
+ * hands it to the field, and every other outcome gives it back through
+ * sched_cache_group_detach().
+ *
+ * Returns NULL, having taken nothing, when @grp is NULL or already being
+ * released.
+ */
+static struct sched_cache_group *
+sched_cache_group_attach(struct sched_cache_group *grp) __must_check;
+
+static struct sched_cache_group *
+sched_cache_group_attach(struct sched_cache_group *grp)
+{
+ if (!grp || !refcount_inc_not_zero(&grp->refcnt))
+ return NULL;
+
+ atomic_inc(&grp->nr_threads);
+ return grp;
+}
+
+void sched_cache_group_detach(struct sched_cache_group *grp)
+{
+ if (grp) {
+ atomic_dec(&grp->nr_threads);
+ sched_cache_group_put(grp);
+ }
+}
+
void sched_cache_fork(struct task_struct *p)
{
/*
* The child takes its own reference on the mm's cache group, separate
- * from the reference held by the mm. @p is not yet visible to readers,
- * so a plain initializing store is enough.
+ * from the reference held by the mm. Attaching takes that reference
+ * and the membership in one step, and the store publishes both: @p is
+ * not yet visible to readers, so a plain initializing store is enough.
*/
RCU_INIT_POINTER(p->sched_cache_grp,
- sched_cache_group_get(p->mm->sched_cache_grp));
+ sched_cache_group_attach(p->mm->sched_cache_grp));
}

void sched_cache_fork_cleanup(struct task_struct *p)
{
+ struct sched_cache_group *grp;
+
/*
* A fork that fails after sched_cache_fork() never reaches exit_mm(),
* so drop the reference here. @p never became visible, so there are no
* concurrent readers and the reference we hold keeps the group alive.
*/
- sched_cache_group_put(rcu_access_pointer(p->sched_cache_grp));
+ grp = rcu_access_pointer(p->sched_cache_grp);
RCU_INIT_POINTER(p->sched_cache_grp, NULL);
+ sched_cache_group_detach(grp);
}

void sched_cache_exec_mmap(struct task_struct *p, struct mm_struct *mm)
@@ -81,11 +116,16 @@ void sched_cache_exec_mmap(struct task_struct *p, struct mm_struct *mm)
struct sched_cache_group *old;

/*
- * Acquire the new reference before publishing the pointer, then drop
- * the old one. @p is current and the only writer of its own pointer.
+ * Attaching takes the new group's reference and its membership before
+ * the store publishes it, so a reader that finds it in the field sees
+ * a group @p is already counted in. The store then hands over that
+ * reference and the old group is detached, which drops both its
+ * membership and the reference it was published with. @p is current
+ * and the only writer of its own pointer.
*/
- old = sched_cache_replace_grp(p, sched_cache_group_get(mm->sched_cache_grp));
- sched_cache_group_put(old);
+ old = sched_cache_replace_grp(p,
+ sched_cache_group_attach(mm->sched_cache_grp));
+ sched_cache_group_detach(old);
}

void sched_cache_exit_mm(struct task_struct *p)
@@ -105,7 +145,7 @@ void sched_cache_exit_mm(struct task_struct *p)
WRITE_ONCE(grp->footprint, fp - sub);
}
#endif
- sched_cache_group_put(grp);
+ sched_cache_group_detach(grp);
}

static void sched_cache_group_init(struct sched_cache_group *grp,
@@ -131,6 +171,7 @@ static void sched_cache_group_init(struct sched_cache_group *grp,
grp->nr_running_avg = 0;
grp->footprint = 0;
refcount_set(&grp->refcnt, 1);
+ atomic_set(&grp->nr_threads, 0);
/*
* The update to grp->pcpu_sched should not be reordered
* before initialization to grp's other fields, in case
diff --git a/kernel/sched/cache_sched.h b/kernel/sched/cache_sched.h
index 4d0febe5fee1..c73595ad6a9a 100644
--- a/kernel/sched/cache_sched.h
+++ b/kernel/sched/cache_sched.h
@@ -13,6 +13,7 @@
struct sched_cache_group *
sched_cache_alloc_group(struct sched_cache_time __percpu *_pcpu_sched);
void sched_cache_group_put(struct sched_cache_group *grp);
+void sched_cache_group_detach(struct sched_cache_group *grp);

#endif /* CONFIG_SCHED_CACHE */

diff --git a/kernel/sched/fair.c b/kernel/sched/fair.c
index 29c6be227ef8..f5610cbc76ec 100644
--- a/kernel/sched/fair.c
+++ b/kernel/sched/fair.c
@@ -1527,17 +1527,17 @@ static bool exceed_llc_capacity(struct sched_cache_group *grp, int cpu)
return false;
}

-static bool invalid_llc_nr(struct sched_cache_group *grp, struct task_struct *p,
- int cpu)
+static bool invalid_llc_nr(struct sched_cache_group *grp, int cpu)
{
int scale;

- if (get_nr_threads(p) <= 1)
+ /* Skip if the cache group has at most one member. */
+ if (atomic_read(&grp->nr_threads) <= 1)
return true;

/*
* Scale the number of 'cores' in a LLC by llc_aggr_tolerance
- * and compare it to the task's active threads.
+ * and compare it to the group's active threads.
*/
scale = get_sched_cache_scale(1);
if (scale == INT_MAX)
@@ -1802,7 +1802,7 @@ void account_mm_sched(struct rq *rq, struct task_struct *p, s64 delta_exec)
* its preferred state.
*/
if ((long)(epoch - READ_ONCE(grp->epoch)) > llc_epoch_affinity_timeout ||
- invalid_llc_nr(grp, p, cpu_of(rq)) ||
+ invalid_llc_nr(grp, cpu_of(rq)) ||
exceed_llc_capacity(grp, cpu_of(rq))) {
if (READ_ONCE(grp->cpu) != -1)
WRITE_ONCE(grp->cpu, -1);
@@ -1946,7 +1946,7 @@ static void task_cache_work(struct callback_head *work)
return;

curr_cpu = task_cpu(p);
- if (invalid_llc_nr(grp, p, curr_cpu) ||
+ if (invalid_llc_nr(grp, curr_cpu) ||
exceed_llc_capacity(grp, curr_cpu)) {
if (READ_ONCE(grp->cpu) != -1)
WRITE_ONCE(grp->cpu, -1);
@@ -10830,7 +10830,7 @@ static enum llc_mig can_migrate_llc_task(struct lb_env *env,
return mig_unrestricted;

/* skip cache aware load balance for too many threads */
- if (invalid_llc_nr(grp, p, dst_cpu) ||
+ if (invalid_llc_nr(grp, dst_cpu) ||
exceed_llc_capacity(grp, dst_cpu)) {
if (READ_ONCE(grp->cpu) != -1)
WRITE_ONCE(grp->cpu, -1);
--
2.34.1