[RFC PATCH 3/7] sched/cache: Introduce the task affinity group
From: Luo Gengkun
Date: Thu Nov 26 2026 - 08:16:44 EST
A cache group belongs to an mm today, and it is what gathers all the
tasks of a thread group together. But it cannot gather tasks from
different mms: two processes talking through a pipe, for example, have
data affinity, yet still cannot be gathered together. To address this
problem, add the affinity group: it belongs to no mm, so it can gather
tasks from different mms.
An mm's group and an affinity group are the two kinds of cache group,
told apart by the new type field. An affinity group adds two members:
- parent, used only by affinity groups: when two affinity groups
merge, the loser points to the winner
- rank, used only by affinity groups: when two affinity groups merge,
rank decides the winner
Submitting sched_cache_hint(x, y) is all it takes to gather x and y
into one affinity group. An example:
before after
[ A ] [ B ] [ A ]----->[ B ]
/ \ / \ / \ / \
t1 t2 t3 t4 t1 t2 t3 t4
This commit only sets A->parent to B; updating the sched_cache_grp of
the tasks in A to follow the link is implemented in the next commit.
Signed-off-by: Luo Gengkun <luogengkun@xxxxxxxxxxxxxxx>
---
include/linux/sched.h | 19 +++
kernel/sched/cache_sched.c | 280 ++++++++++++++++++++++++++++++++++++-
2 files changed, 296 insertions(+), 3 deletions(-)
diff --git a/include/linux/sched.h b/include/linux/sched.h
index 3073ed421b58..de270443cffa 100644
--- a/include/linux/sched.h
+++ b/include/linux/sched.h
@@ -2406,6 +2406,16 @@ struct sched_cache_time {
unsigned long epoch;
};
+/*
+ * The kind of a cache group. An mm's group and an affinity group are the
+ * only kinds there are, and only an affinity group ever carries a merge
+ * link: the type tells them apart.
+ */
+enum cache_group_type {
+ CACHE_GROUP_MM = 0,
+ CACHE_GROUP_AFFINITY,
+};
+
struct sched_cache_group {
struct sched_cache_time __percpu *pcpu_sched;
raw_spinlock_t lock;
@@ -2416,6 +2426,9 @@ struct sched_cache_group {
int cpu;
refcount_t refcnt;
atomic_t nr_threads;
+ enum cache_group_type type;
+ struct sched_cache_group __rcu *parent;
+ unsigned int rank;
struct rcu_head rcu;
} ____cacheline_aligned_in_smp;
@@ -2427,6 +2440,8 @@ void sched_cache_fork_cleanup(struct task_struct *p);
void sched_cache_exec_mmap(struct task_struct *p, struct mm_struct *mm);
void sched_cache_exit_mm(struct task_struct *p);
+int sched_cache_hint(struct task_struct *t1, struct task_struct *t2);
+
#else
struct sched_cache_group { };
@@ -2436,6 +2451,10 @@ static inline void sched_cache_fork_cleanup(struct task_struct *p) { }
static inline void sched_cache_exec_mmap(struct task_struct *p, struct mm_struct *mm) { }
static inline void sched_cache_exit_mm(struct task_struct *p) { }
+static inline int sched_cache_hint(struct task_struct *t1, struct task_struct *t2)
+{
+ return -EOPNOTSUPP;
+}
#endif
#ifndef MODULE
diff --git a/kernel/sched/cache_sched.c b/kernel/sched/cache_sched.c
index e1562b6af0bc..52106c3ac049 100644
--- a/kernel/sched/cache_sched.c
+++ b/kernel/sched/cache_sched.c
@@ -1,5 +1,6 @@
// SPDX-License-Identifier: GPL-2.0-only
#include "sched.h"
+#include "cache_sched.h"
#define rcu_deref_sched_cache_grp(tsk) \
rcu_dereference_check((tsk)->sched_cache_grp, (tsk) == current)
@@ -9,8 +10,14 @@ static struct sched_cache_group *sched_cache_replace_grp(struct task_struct *p,
{
struct sched_cache_group *old;
+ /*
+ * A hint on another CPU may change the field concurrently. cmpxchg
+ * makes sure @old is the group this call actually replaced: that is
+ * the one to detach, and a concurrent swap is left intact.
+ */
old = rcu_deref_sched_cache_grp(p);
- rcu_assign_pointer(p->sched_cache_grp, new);
+ do {
+ } while (!try_cmpxchg(&p->sched_cache_grp, &old, new));
return old;
}
@@ -19,10 +26,15 @@ static void sched_cache_group_free_rcu(struct rcu_head *rcu)
{
struct sched_cache_group *grp =
container_of(rcu, struct sched_cache_group, rcu);
+ struct sched_cache_group *parent;
WARN_ON_ONCE(atomic_read(&grp->nr_threads));
+ /* Only affinity groups hold it; for an mm's group this is NULL. */
+ parent = rcu_access_pointer(grp->parent);
free_percpu(grp->pcpu_sched);
kfree(grp);
+
+ sched_cache_group_put(parent);
}
void sched_cache_group_put(struct sched_cache_group *grp)
@@ -120,8 +132,7 @@ void sched_cache_exec_mmap(struct task_struct *p, struct mm_struct *mm)
* 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.
+ * membership and the reference it was published with.
*/
old = sched_cache_replace_grp(p,
sched_cache_group_attach(mm->sched_cache_grp));
@@ -194,3 +205,266 @@ sched_cache_alloc_group(struct sched_cache_time __percpu *_pcpu_sched)
sched_cache_group_init(grp, _pcpu_sched);
return grp;
}
+
+/*
+ * Follow the merge links up to the root of @grp, which is where the
+ * accounting lives. Walking needs no lock of its own: the caller's
+ * reference on @grp keeps it alive, and every live affinity group holds a
+ * reference on its parent, so the chain cannot be freed under the walk.
+ */
+static struct sched_cache_group *scg_find_root(struct sched_cache_group *grp)
+{
+ struct sched_cache_group *parent;
+
+ while ((parent = rcu_dereference(grp->parent)))
+ grp = parent;
+
+ return grp;
+}
+
+/*
+ * Allocate an affinity group: a fresh group typed CACHE_GROUP_AFFINITY,
+ * never an mm's. Returns with one reference, the caller's. Sleeps, so
+ * call it with no lock held.
+ */
+static struct sched_cache_group *scg_affinity_alloc(void)
+{
+ struct sched_cache_time __percpu *pcpu_sched;
+ struct sched_cache_group *grp;
+
+ pcpu_sched = alloc_percpu(struct sched_cache_time);
+ if (!pcpu_sched)
+ return NULL;
+
+ /* Frees @pcpu_sched itself when it fails. */
+ grp = sched_cache_alloc_group(pcpu_sched);
+ if (!grp)
+ return NULL;
+
+ grp->type = CACHE_GROUP_AFFINITY;
+
+ return grp;
+}
+
+/*
+ * Whether @p can be put into an affinity group. A kernel thread has no mm
+ * to pin, and a task on its way out of one - the last exec or exit - is past
+ * the point where an affinity move means anything.
+ *
+ * Both are racy reads from another CPU: a stale read only lets a hint
+ * through, and the cmpxchg on the field settles that race like any other.
+ * What the checks buy is skipping an affinity allocation and swap for a task
+ * that is already gone.
+ */
+static bool scg_can_attach(struct task_struct *p)
+{
+ return !(p->flags & (PF_KTHREAD | PF_EXITING)) &&
+ !p->in_execve && p->mm;
+}
+
+/*
+ * The task's entry into its affinity group: the group it accounts in. A
+ * task outside any affinity group accounts in its mm's group; first contact
+ * switches it to a fresh affinity group here, so membership and accounting
+ * are one fact in one field.
+ *
+ * The switch mirrors sched_cache_replace_grp(): exec and exit race it from
+ * @t's own context, and two hints may race each other, so whoever wins the
+ * cmpxchg owns the attach and the detach of what it installed and replaced.
+ */
+static struct sched_cache_group *
+scg_get_or_alloc_affinity(struct task_struct *t)
+{
+ struct sched_cache_group *c, *new;
+
+ scoped_guard(rcu) {
+ c = rcu_dereference(t->sched_cache_grp);
+ /* The type is stamped before publication and never changes. */
+ if (c && (READ_ONCE(c->type) == CACHE_GROUP_AFFINITY)) {
+ if (!sched_cache_group_get(c))
+ return ERR_PTR(-EAGAIN);
+ return c; /* already an affinity group */
+ }
+ }
+
+ if (!scg_can_attach(t))
+ return ERR_PTR(-ESRCH);
+
+ new = scg_affinity_alloc(); /* sleepable, outside any lock */
+ if (!new)
+ return ERR_PTR(-ENOMEM);
+
+ scoped_guard(rcu) {
+ c = rcu_dereference(t->sched_cache_grp);
+ if (!c) {
+ /* Exit took the group away while we were allocating. */
+ sched_cache_group_put(new);
+ return ERR_PTR(-ESRCH);
+ }
+ if (READ_ONCE(c->type) == CACHE_GROUP_AFFINITY) {
+ /* Another hint won the first contact while we allocated. */
+ if (!sched_cache_group_get(c)) {
+ sched_cache_group_put(new);
+ return ERR_PTR(-EAGAIN);
+ }
+ sched_cache_group_put(new);
+ return c;
+ }
+
+ /*
+ * First contact: switch the accounting to the fresh affinity
+ * group. A lost cmpxchg means @t moved on after the read
+ * above (exec installed a new mm's group, exit cleared it):
+ * drop the hint, not the task.
+ *
+ * Two references are in play: the allocator's, which the
+ * caller owns, and the field's. Attaching takes the second
+ * and counts the membership in the same step.
+ */
+ if (!sched_cache_group_attach(new)) {
+ /* Fresh from the allocator, so this cannot happen. */
+ sched_cache_group_put(new);
+ return ERR_PTR(-EAGAIN);
+ }
+ if (!try_cmpxchg(&t->sched_cache_grp, &c, new)) {
+ sched_cache_group_detach(new); /* the field's */
+ sched_cache_group_put(new); /* the allocator's */
+ return ERR_PTR(-EAGAIN);
+ }
+
+ sched_cache_group_detach(c);
+ }
+
+ return new; /* the allocator's reference */
+}
+
+/*
+ * Put one root under the other; every member keeps converging to whatever
+ * its root is now.
+ *
+ * The child's reference on the root keeps the chain up while the members
+ * that cached the child are still converging through it; it is dropped only
+ * in the child's free callback.
+ *
+ * The lock is the groups' own, shared with the tick's queueing, which runs
+ * from interrupt context - hence the IRQ-disabled take. The two groups are
+ * locked in address order, the only nesting this lock ever sees.
+ */
+static int scg_merge(struct sched_cache_group *a, struct sched_cache_group *b)
+{
+ int ret = 0;
+ struct sched_cache_group *root, *child;
+ unsigned long flags;
+
+ if (a == b)
+ return 0; /* same affinity group already */
+
+ /* Merge links exist only between affinity groups, never an mm's. */
+ if (WARN_ON_ONCE(a->type != CACHE_GROUP_AFFINITY ||
+ b->type != CACHE_GROUP_AFFINITY))
+ return -EINVAL;
+
+ if (a > b)
+ swap(a, b);
+
+ raw_spin_lock_irqsave(&a->lock, flags);
+ raw_spin_lock_nested(&b->lock, SINGLE_DEPTH_NESTING);
+
+ if (rcu_dereference_protected(a->parent, 1) != NULL ||
+ rcu_dereference_protected(b->parent, 1) != NULL) {
+ ret = -EAGAIN;
+ goto out;
+ }
+ root = a->rank < b->rank ? b : a;
+ child = root == a ? b : a;
+ if (root->rank == child->rank)
+ root->rank++;
+
+ /*
+ * The child's per-CPU runtime history dies with the group - the
+ * merged affinity group's occupancy starts over from what accrues on
+ * the root. These are advisory statistics, not accounting of record;
+ * keeping the sums continuous would mean walking the child's
+ * per-CPU array under both locks, for numbers that converge again
+ * within an epoch anyway.
+ */
+ refcount_inc(&root->refcnt); /* child owns one ref on root */
+ rcu_assign_pointer(child->parent, root);
+
+out:
+ raw_spin_unlock(&b->lock);
+ raw_spin_unlock_irqrestore(&a->lock, flags);
+
+ return ret;
+}
+
+/*
+ * Connect @t1 and @t2: they are accounted in one group from here on. The
+ * caller's references are enough, no tasks are pinned.
+ *
+ * Two threads of one mm need nothing of this only while neither is in an
+ * affinity group: their accounting already meets in the mm's group. Once
+ * one of them joins an affinity group the pair is split, and the hint has
+ * real work to do.
+ */
+int sched_cache_hint(struct task_struct *t1, struct task_struct *t2)
+{
+ struct sched_cache_group *c1, *c2;
+ struct mm_struct *mm1;
+ int ret = 0;
+
+ if (!sched_cache_enabled())
+ return -EOPNOTSUPP;
+
+ if (t1 == t2)
+ return 0;
+
+ /*
+ * The common same-mm case is two threads outside any affinity group:
+ * they share the mm's group already, so skip the lock. These reads
+ * are racy and may skip a pair that just joined an affinity group;
+ * hints are advisory, and whatever still observes the relation
+ * submits again.
+ */
+ mm1 = READ_ONCE(t1->mm);
+ if (mm1 && mm1 == READ_ONCE(t2->mm)) {
+ struct sched_cache_group *g1;
+
+ rcu_read_lock();
+ g1 = rcu_dereference(t1->sched_cache_grp);
+ if (g1 && g1 == rcu_dereference(t2->sched_cache_grp) &&
+ READ_ONCE(g1->type) != CACHE_GROUP_AFFINITY) {
+ rcu_read_unlock();
+ return 0;
+ }
+ rcu_read_unlock();
+ }
+
+ if (!scg_can_attach(t1) || !scg_can_attach(t2))
+ return -ESRCH;
+
+ for (;;) {
+ c1 = scg_get_or_alloc_affinity(t1);
+ if (IS_ERR(c1)) {
+ ret = PTR_ERR(c1);
+ break;
+ }
+ c2 = scg_get_or_alloc_affinity(t2);
+ if (IS_ERR(c2)) {
+ sched_cache_group_put(c1);
+ ret = PTR_ERR(c2);
+ break;
+ }
+ rcu_read_lock();
+ ret = scg_merge(scg_find_root(c1), scg_find_root(c2));
+ rcu_read_unlock();
+
+ sched_cache_group_put(c1);
+ sched_cache_group_put(c2);
+
+ if (ret != -EAGAIN)
+ return ret;
+ cond_resched();
+ }
+ return ret;
+}
--
2.34.1