[RFC PATCH 3/7] sched/cache: Extract sched_cache_alloc_group() helper

From: Tim Chen

Date: Fri Aug 28 2026 - 18:26:05 EST


Extract sched_cache_alloc_group() into kernel/sched/cache_sched.c as a
shared helper.

Convert mm_init_sched() to use the new helper instead of open-coding the
allocation and initialization. This prepares for a second caller in the
upcoming prctl CREATE path.

No functional change.

Co-developed-by: Chen Yu <yu.c.chen@xxxxxxxxx>
Signed-off-by: Chen Yu <yu.c.chen@xxxxxxxxx>
Signed-off-by: Tim Chen <tim.c.chen@xxxxxxxxxxxxxxx>
---
include/linux/sched.h | 2 ++
kernel/sched/cache_sched.c | 46 ++++++++++++++++++++++++++++++++++++++
kernel/sched/fair.c | 32 ++------------------------
3 files changed, 50 insertions(+), 30 deletions(-)

diff --git a/include/linux/sched.h b/include/linux/sched.h
index e7253cb332fd..e25347aa2cc5 100644
--- a/include/linux/sched.h
+++ b/include/linux/sched.h
@@ -2406,6 +2406,8 @@ struct sched_cache_group {
void sched_cache_group_put(struct sched_cache_group *grp);
struct sched_cache_group *sched_cache_group_get(struct sched_cache_group *grp);
struct sched_cache_group *task_cache_group_get(struct task_struct *p);
+struct sched_cache_group *
+sched_cache_alloc_group(struct sched_cache_time __percpu *pcpu_sched);

#else

diff --git a/kernel/sched/cache_sched.c b/kernel/sched/cache_sched.c
index 99d07e1e067c..860209a47931 100644
--- a/kernel/sched/cache_sched.c
+++ b/kernel/sched/cache_sched.c
@@ -37,3 +37,49 @@ void sched_cache_group_put(struct sched_cache_group *grp)

call_rcu(&grp->rcu, sched_cache_group_free_rcu);
}
+
+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/fair.c b/kernel/sched/fair.c
index 281b4c896bf4..be1f3568c3aa 100644
--- a/kernel/sched/fair.c
+++ b/kernel/sched/fair.c
@@ -1575,40 +1575,12 @@ static void account_llc_dequeue(struct rq *rq, struct task_struct *p)
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;
+ struct sched_cache_group *grp = sched_cache_alloc_group(_pcpu_sched);

- grp = kzalloc_obj(*grp);
- if (!grp) {
- free_percpu(_pcpu_sched);
+ if (!grp)
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);
mm->sched_cache_grp = grp;
- /*
- * 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);
return 0;
}

--
2.32.0