[RFC PATCH 1/7] sched/cache: Move the cache group lifecycle to cache_sched.c

From: Luo Gengkun

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


The cache group's allocation, initialization, and reference lifecycle
currently lives in fair.c inside its CONFIG_SCHED_CACHE block, mixed
in with the wakeup and load balancing code that consumes it.

Move it into its own compilation unit, kernel/sched/cache_sched.c
(built into build_utility.o), to lay the groundwork for the rest of
this series.

The moved code is unchanged except for the allocator, which is split
out of mm_init_sched() as sched_cache_alloc_group() along with its
failure path. Export this constructor via the new
kernel/sched/cache_sched.h, while keeping the responsibility of
publishing the group to the mm inside mm_init_sched(). Also make
sched_cache_group_put() global, as task_cache_work() and
mm_destroy_sched() remain in fair.c and still call it.

No functional change intended.

Signed-off-by: Luo Gengkun <luogengkun@xxxxxxxxxxxxxxx>
---
kernel/sched/build_utility.c | 5 ++
kernel/sched/cache_sched.c | 155 +++++++++++++++++++++++++++++++++++
kernel/sched/cache_sched.h | 19 +++++
kernel/sched/fair.c | 133 +-----------------------------
4 files changed, 181 insertions(+), 131 deletions(-)
create mode 100644 kernel/sched/cache_sched.c
create mode 100644 kernel/sched/cache_sched.h

diff --git a/kernel/sched/build_utility.c b/kernel/sched/build_utility.c
index e2cf3b08d4e9..f7ea083de6a3 100644
--- a/kernel/sched/build_utility.c
+++ b/kernel/sched/build_utility.c
@@ -85,6 +85,11 @@

#include "topology.c"

+#ifdef CONFIG_SCHED_CACHE
+# include "cache_sched.h"
+# include "cache_sched.c"
+#endif
+
#ifdef CONFIG_SCHED_CORE
# include "core_sched.c"
#endif
diff --git a/kernel/sched/cache_sched.c b/kernel/sched/cache_sched.c
new file mode 100644
index 000000000000..c20ed59ba45b
--- /dev/null
+++ b/kernel/sched/cache_sched.c
@@ -0,0 +1,155 @@
+// SPDX-License-Identifier: GPL-2.0-only
+#include "sched.h"
+
+#define rcu_deref_sched_cache_grp(tsk) \
+ rcu_dereference_check((tsk)->sched_cache_grp, (tsk) == current)
+
+static struct sched_cache_group *sched_cache_replace_grp(struct task_struct *p,
+ struct sched_cache_group *new)
+{
+ struct sched_cache_group *old;
+
+ old = rcu_deref_sched_cache_grp(p);
+ rcu_assign_pointer(p->sched_cache_grp, new);
+
+ return old;
+}
+
+static void sched_cache_group_free_rcu(struct rcu_head *rcu)
+{
+ struct sched_cache_group *grp =
+ container_of(rcu, struct sched_cache_group, rcu);
+
+ free_percpu(grp->pcpu_sched);
+ kfree(grp);
+}
+
+void sched_cache_group_put(struct sched_cache_group *grp)
+{
+ if (!grp || !refcount_dec_and_test(&grp->refcnt))
+ return;
+
+ call_rcu(&grp->rcu, sched_cache_group_free_rcu);
+}
+
+DEFINE_FREE(sched_cache_group_put, struct sched_cache_group *,
+ sched_cache_group_put(_T));
+
+struct sched_cache_group *sched_cache_group_get(struct sched_cache_group *grp)
+{
+ /*
+ * refcount_inc_not_zero() is the acquire primitive for lockless
+ * (RCU) lookups; plain refcount_inc() would scribble the count if
+ * it already reached zero. Return NULL in that case.
+ */
+ if (grp && !refcount_inc_not_zero(&grp->refcnt))
+ grp = NULL;
+
+ return grp;
+}
+
+struct sched_cache_group *task_cache_group_get(struct task_struct *p)
+{
+ guard(rcu)();
+ return sched_cache_group_get(rcu_dereference(p->sched_cache_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.
+ */
+ RCU_INIT_POINTER(p->sched_cache_grp,
+ sched_cache_group_get(p->mm->sched_cache_grp));
+}
+
+void sched_cache_fork_cleanup(struct task_struct *p)
+{
+ /*
+ * 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));
+ RCU_INIT_POINTER(p->sched_cache_grp, NULL);
+}
+
+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.
+ */
+ old = sched_cache_replace_grp(p, sched_cache_group_get(mm->sched_cache_grp));
+ sched_cache_group_put(old);
+}
+
+void sched_cache_exit_mm(struct task_struct *p)
+{
+ struct sched_cache_group *grp = sched_cache_replace_grp(p, NULL);
+
+#ifdef CONFIG_NUMA_BALANCING
+ /*
+ * Subtract this task's footprint from the group before dropping the
+ * reference, so the group footprint converges as its threads exit.
+ * Unlocked for performance; clamp to avoid underflow.
+ */
+ if (grp && p->total_numa_faults) {
+ unsigned long fp = READ_ONCE(grp->footprint);
+ unsigned long sub = min(fp, p->total_numa_faults);
+
+ WRITE_ONCE(grp->footprint, fp - sub);
+ }
+#endif
+ sched_cache_group_put(grp);
+}
+
+static void sched_cache_group_init(struct sched_cache_group *grp,
+ struct sched_cache_time __percpu *_pcpu_sched)
+{
+ unsigned long epoch = 0;
+ int i;
+
+ for_each_possible_cpu(i) {
+ struct sched_cache_time *pcpu_sched = per_cpu_ptr(_pcpu_sched, i);
+ struct rq *rq = cpu_rq(i);
+
+ pcpu_sched->runtime = 0;
+ /* a slightly stale cpu epoch is acceptible */
+ pcpu_sched->epoch = rq->cpu_epoch;
+ epoch = rq->cpu_epoch;
+ }
+
+ raw_spin_lock_init(&grp->lock);
+ grp->epoch = epoch;
+ grp->cpu = -1;
+ grp->next_scan = jiffies;
+ grp->nr_running_avg = 0;
+ grp->footprint = 0;
+ refcount_set(&grp->refcnt, 1);
+ /*
+ * The update to grp->pcpu_sched should not be reordered
+ * before initialization to grp's other fields, in case
+ * the readers may get invalid mm_sched_epoch, etc.
+ */
+ smp_store_release(&grp->pcpu_sched, _pcpu_sched);
+}
+
+struct sched_cache_group *
+sched_cache_alloc_group(struct sched_cache_time __percpu *_pcpu_sched)
+{
+ struct sched_cache_group *grp;
+
+ grp = kzalloc_obj(*grp);
+ if (!grp) {
+ free_percpu(_pcpu_sched);
+ return NULL;
+ }
+
+ sched_cache_group_init(grp, _pcpu_sched);
+ return grp;
+}
diff --git a/kernel/sched/cache_sched.h b/kernel/sched/cache_sched.h
new file mode 100644
index 000000000000..4d0febe5fee1
--- /dev/null
+++ b/kernel/sched/cache_sched.h
@@ -0,0 +1,19 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/*
+ * Cache-aware scheduling: the interface between fair.c and the cache
+ * group's own compilation unit, kernel/sched/cache_sched.c.
+ */
+#ifndef _KERNEL_SCHED_CACHE_SCHED_H
+#define _KERNEL_SCHED_CACHE_SCHED_H
+
+#include <linux/sched.h>
+
+#ifdef CONFIG_SCHED_CACHE
+
+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);
+
+#endif /* CONFIG_SCHED_CACHE */
+
+#endif /* _KERNEL_SCHED_CACHE_SCHED_H */
diff --git a/kernel/sched/fair.c b/kernel/sched/fair.c
index 57360f5cdde4..29c6be227ef8 100644
--- a/kernel/sched/fair.c
+++ b/kernel/sched/fair.c
@@ -60,6 +60,7 @@
#include "sched.h"
#include "stats.h"
#include "autogroup.h"
+#include "cache_sched.h"

/*
* The initial- and re-scaling of tunables is configurable
@@ -1652,39 +1653,13 @@ int mm_init_sched(struct mm_struct *mm,
struct sched_cache_time __percpu *_pcpu_sched)
{
struct sched_cache_group *grp;
- unsigned long epoch = 0;
- int i;

- grp = kzalloc_obj(*grp);
+ grp = sched_cache_alloc_group(_pcpu_sched);
if (!grp) {
- free_percpu(_pcpu_sched);
mm->sched_cache_grp = NULL;
return -ENOMEM;
}

- for_each_possible_cpu(i) {
- struct sched_cache_time *pcpu_sched = per_cpu_ptr(_pcpu_sched, i);
- struct rq *rq = cpu_rq(i);
-
- pcpu_sched->runtime = 0;
- /* a slightly stale cpu epoch is acceptible */
- pcpu_sched->epoch = rq->cpu_epoch;
- epoch = rq->cpu_epoch;
- }
-
- raw_spin_lock_init(&grp->lock);
- grp->epoch = epoch;
- grp->cpu = -1;
- grp->next_scan = jiffies;
- grp->nr_running_avg = 0;
- grp->footprint = 0;
- refcount_set(&grp->refcnt, 1);
- /*
- * The update to grp->pcpu_sched should not be reordered
- * before initialization to grp's other fields, in case
- * the readers may get invalid mm_sched_epoch, etc.
- */
- smp_store_release(&grp->pcpu_sched, _pcpu_sched);
/*
* Publish the group last. Not every reader qualifies it by
* grp->pcpu_sched - can_migrate_llc_task() only checks that the
@@ -1696,113 +1671,9 @@ int mm_init_sched(struct mm_struct *mm,
return 0;
}

-static void sched_cache_group_free_rcu(struct rcu_head *rcu)
-{
- struct sched_cache_group *grp =
- container_of(rcu, struct sched_cache_group, rcu);
-
- free_percpu(grp->pcpu_sched);
- kfree(grp);
-}
-
-static void sched_cache_group_put(struct sched_cache_group *grp)
-{
- if (!grp || !refcount_dec_and_test(&grp->refcnt))
- return;
-
- call_rcu(&grp->rcu, sched_cache_group_free_rcu);
-}
-
DEFINE_FREE(sched_cache_group_put, struct sched_cache_group *,
sched_cache_group_put(_T));

-#define rcu_deref_sched_cache_grp(tsk) \
- rcu_dereference_check((tsk)->sched_cache_grp, (tsk) == current)
-
-static struct sched_cache_group *sched_cache_replace_grp(struct task_struct *p,
- struct sched_cache_group *new)
-{
- struct sched_cache_group *old;
-
- old = rcu_deref_sched_cache_grp(p);
- rcu_assign_pointer(p->sched_cache_grp, new);
-
- return old;
-}
-
-struct sched_cache_group *sched_cache_group_get(struct sched_cache_group *grp)
-{
- /*
- * refcount_inc_not_zero() is the acquire primitive for lockless
- * (RCU) lookups; plain refcount_inc() would scribble the count if
- * it already reached zero. Return NULL in that case.
- */
- if (grp && !refcount_inc_not_zero(&grp->refcnt))
- grp = NULL;
-
- return grp;
-}
-
-struct sched_cache_group *task_cache_group_get(struct task_struct *p)
-{
- guard(rcu)();
- return sched_cache_group_get(rcu_dereference(p->sched_cache_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.
- */
- RCU_INIT_POINTER(p->sched_cache_grp,
- sched_cache_group_get(p->mm->sched_cache_grp));
-}
-
-void sched_cache_fork_cleanup(struct task_struct *p)
-{
- /*
- * 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));
- RCU_INIT_POINTER(p->sched_cache_grp, NULL);
-}
-
-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.
- */
- old = sched_cache_replace_grp(p, sched_cache_group_get(mm->sched_cache_grp));
- sched_cache_group_put(old);
-}
-
-void sched_cache_exit_mm(struct task_struct *p)
-{
- struct sched_cache_group *grp = sched_cache_replace_grp(p, NULL);
-
-#ifdef CONFIG_NUMA_BALANCING
- /*
- * Subtract this task's footprint from the group before dropping the
- * reference, so the group footprint converges as its threads exit.
- * Unlocked for performance; clamp to avoid underflow.
- */
- if (grp && p->total_numa_faults) {
- unsigned long fp = READ_ONCE(grp->footprint);
- unsigned long sub = min(fp, p->total_numa_faults);
-
- WRITE_ONCE(grp->footprint, fp - sub);
- }
-#endif
- sched_cache_group_put(grp);
-}
-
void mm_destroy_sched(struct mm_struct *mm)
{
sched_cache_group_put(mm->sched_cache_grp);
--
2.34.1