From: Luo Gengkun <luogengkun@huaweicloud.com>
To: Peter Zijlstra <peterz@infradead.org>,
Ingo Molnar <mingo@redhat.com>,
Juri Lelli <juri.lelli@redhat.com>,
Vincent Guittot <vincent.guittot@linaro.org>,
Tim Chen <tim.c.chen@linux.intel.com>,
Chen Yu <yu.c.chen@intel.com>,
K Prateek Nayak <kprateek.nayak@amd.com>
Cc: Dietmar Eggemann <dietmar.eggemann@arm.com>,
Steven Rostedt <rostedt@goodmis.org>,
Ben Segall <bsegall@google.com>, Mel Gorman <mgorman@suse.de>,
Valentin Schneider <vschneid@redhat.com>,
linux-kernel@vger.kernel.org
Subject: [RFC PATCH 4/7] sched/cache: Converge task accounting to the affinity root
Date: Thu, 26 Nov 2026 13:48:25 +0000 [thread overview]
Message-ID: <cf5eaad0ece8894dfabc6498875b599ebff34899.1795691926.git.luogengkun2@huawei.com> (raw)
In-Reply-To: <cover.1795691926.git.luogengkun2@huawei.com>
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@huaweicloud.com>
---
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
next prev parent reply other threads:[~2026-11-26 13:16 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-26 13:48 [RFC PATCH 0/7] sched/cache: Introduce per-task affinity group via BPF detector Luo Gengkun
2026-11-26 13:48 ` [RFC PATCH 1/7] sched/cache: Move the cache group lifecycle to cache_sched.c Luo Gengkun
2026-11-26 13:48 ` [RFC PATCH 2/7] sched/cache: Add thread count to cache group Luo Gengkun
2026-11-26 13:48 ` [RFC PATCH 3/7] sched/cache: Introduce the task affinity group Luo Gengkun
2026-11-26 13:48 ` Luo Gengkun [this message]
2026-11-26 13:48 ` [RFC PATCH 5/7] sched/cache: Add the per-CPU hint queue Luo Gengkun
2026-11-26 13:48 ` [RFC PATCH 6/7] sched/cache: Add BPF-facing hint submitters Luo Gengkun
2026-11-26 13:48 ` [RFC PATCH 7/7] samples/bpf: Add pipe-based cache hint example Luo Gengkun
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=cf5eaad0ece8894dfabc6498875b599ebff34899.1795691926.git.luogengkun2@huawei.com \
--to=luogengkun@huaweicloud.com \
--cc=bsegall@google.com \
--cc=dietmar.eggemann@arm.com \
--cc=juri.lelli@redhat.com \
--cc=kprateek.nayak@amd.com \
--cc=linux-kernel@vger.kernel.org \
--cc=mgorman@suse.de \
--cc=mingo@redhat.com \
--cc=peterz@infradead.org \
--cc=rostedt@goodmis.org \
--cc=tim.c.chen@linux.intel.com \
--cc=vincent.guittot@linaro.org \
--cc=vschneid@redhat.com \
--cc=yu.c.chen@intel.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®