[PATCH v2 02/13] sched/mmcid: Add the allowed CPUs of each joining task to the mm
From: Mathieu Desnoyers
Date: Tue Dec 01 2026 - 19:28:55 EST
max_cids, the CID range of an mm, depends on the number of CPUs its
tasks may run on. The mm keeps the union of the allowed CPUs of its
tasks. This set only grows. mm_init_cid() copies the allowed CPUs of
one task into a new mm. A task becomes a user of the mm when it joins
it in sched_mm_cid_fork(). Later affinity changes reach the mm through
mm_update_cpus_allowed(). Since
commit b0c3d51b54f8 ("sched/mmcid: Provide precomputed maximal value"),
it does nothing for an mm with no user.
So a change made between the copy and the join is lost. In this
sequence, task C runs on CPU 0 only and calls execve(). Its parent P
widens the affinity of C to CPUs 0-63:
P (parent) C (in execve())
exec_mmap()
tsk->mm = new mm
mm_init_cid(new mm, C)
allowed CPUs of mm = 0
nr_cpus_allowed = 1
sched_setaffinity(C, CPUs 0-63)
set_cpus_allowed_common()
C->cpus_mask = CPUs 0-63
mm_update_cpus_allowed(new mm)
mm_cid.users == 0: return
sched_mm_cid_after_execve()
sched_mm_cid_fork()
mm_cid.users = 1
nr_cpus_allowed stays 1
max_cids and pcpu_thrs of the new mm then derive from one allowed CPU.
So the mm switches to per CPU mode as soon as it has two threads. It
switches back only below pcpu_thrs users, which is 1, so it stays in
per CPU mode. There, each CPU keeps the CID it took. The CIDs stay
below the number of possible CPUs, but they are no longer compact.
Per-CID data in user space then spreads over more memory. In testing on
a 64-CPU guest, C ran one thread per CPU, then stopped all but two.
They read mm_cid 1 and 63, instead of values below 2.
A fork() child has the same window, between dup_mm() and
sched_mm_cid_fork(). clone3() with CLONE_INTO_CGROUP into a cpuset with
more CPUs hits it: cpuset_fork() changes the affinity of the child
there. In testing on tip sched/urgent, a process in a cpuset of two
CPUs created a child this way, into a cpuset of all CPUs. The child ran
one thread per CPU, then stopped all but two. In 43 of 80 attempts on
guests with 4 to 64 CPUs, one of the two read an mm_cid of 2 or more.
A child created by a parent already in the cpuset of all CPUs never
did.
Fix this by starting a new mm with no allowed CPUs. Each task adds its
own in sched_mm_cid_add_user() when it joins. The new mm of execve()
comes from mm_alloc(), which already calls mm_init_cid(). So
exec_mmap() no longer calls it.
When the join widens the mm in per CPU mode, it also recomputes
pcpu_thrs, as mm_update_cpus_allowed() does. Otherwise a race leaves a
stale threshold. An affinity change can store the new CPUs of a task
just before the task joins. The join adds them to the mm. The update
then finds nothing to add, and returns before its own recompute. The
mm would stay in per CPU mode too long. A Spin model of the CID
management found this race.
With this change, mm_cid_exec_affinity_test of this series passes on
guests with 4 to 64 CPUs. In the clone3() test above, no attempt read
an mm_cid of 2 or more, with this patch alone on tip sched/urgent.
"sched/mmcid: Serialize joins and exits with affinity changes by
pi_lock" relies on this change. It no longer updates the mm of a task
that has not joined yet, so the join must add the CPUs of the task.
Fixes: b0c3d51b54f8 ("sched/mmcid: Provide precomputed maximal value")
Suggested-by: Thomas Gleixner <tglx@xxxxxxxxxx>
Signed-off-by: Mathieu Desnoyers <mathieu.desnoyers@xxxxxxxxxxxx>
Cc: stable@xxxxxxxxxxxxxxx
---
fs/exec.c | 1 -
include/linux/mm_types.h | 5 ++--
kernel/sched/core.c | 52 ++++++++++++++++++++++++++++++----------
3 files changed, 42 insertions(+), 16 deletions(-)
diff --git a/fs/exec.c b/fs/exec.c
index d188f014871d..20dea1509726 100644
--- a/fs/exec.c
+++ b/fs/exec.c
@@ -892,7 +892,6 @@ static int exec_mmap(struct linux_binprm *bprm)
tsk->active_mm = mm;
tsk->mm = mm;
sched_cache_exec_mmap(tsk, mm);
- mm_init_cid(mm, tsk);
exec_state = task_exec_state_replace(tsk, exec_state);
/*
* This prevents preemption while active_mm is being loaded and
diff --git a/include/linux/mm_types.h b/include/linux/mm_types.h
index 1ed35d258d2e..dd2ecb900c45 100644
--- a/include/linux/mm_types.h
+++ b/include/linux/mm_types.h
@@ -1624,14 +1624,14 @@ static inline unsigned long *mm_cidmask(struct mm_struct *mm)
return (unsigned long *)cid_bitmap;
}
-void mm_init_cid(struct mm_struct *mm, struct task_struct *p);
+void mm_init_cid(struct mm_struct *mm);
static inline int mm_alloc_cid_noprof(struct mm_struct *mm, struct task_struct *p)
{
mm->mm_cid.pcpu = alloc_percpu_noprof(struct mm_cid_pcpu);
if (!mm->mm_cid.pcpu)
return -ENOMEM;
- mm_init_cid(mm, p);
+ mm_init_cid(mm);
return 0;
}
# define mm_alloc_cid(...) alloc_hooks(mm_alloc_cid_noprof(__VA_ARGS__))
@@ -1651,7 +1651,6 @@ static inline unsigned int mm_cid_size(void)
/* Use 2 * NR_CPUS as worse case for static allocation. */
# define MM_CID_STATIC_SIZE (2 * sizeof(cpumask_t))
#else /* CONFIG_SCHED_MM_CID */
-static inline void mm_init_cid(struct mm_struct *mm, struct task_struct *p) { }
static inline int mm_alloc_cid(struct mm_struct *mm, struct task_struct *p) { return 0; }
static inline void mm_destroy_cid(struct mm_struct *mm) { }
static inline unsigned int mm_cid_size(void)
diff --git a/kernel/sched/core.c b/kernel/sched/core.c
index 3ad27670d67f..80239800c566 100644
--- a/kernel/sched/core.c
+++ b/kernel/sched/core.c
@@ -10889,26 +10889,45 @@ static bool mm_update_max_cids(struct mm_struct *mm)
return true;
}
-static inline void mm_update_cpus_allowed(struct mm_struct *mm, const struct cpumask *affmsk)
+/*
+ * Add @affmsk to the allowed CPUs of @mm. Return true if the set grew.
+ * Called from mm_update_cpus_allowed() on an affinity change, and from
+ * sched_mm_cid_add_user() when a task joins the MM CID management.
+ */
+static inline bool __mm_update_cpus_allowed(struct mm_struct *mm, const struct cpumask *affmsk)
{
- struct cpumask *mm_allowed;
- struct mm_mm_cid *mc;
+ struct cpumask *mm_allowed = mm_cpus_allowed(mm);
+ struct mm_mm_cid *mc = &mm->mm_cid;
unsigned int weight;
- if (!mm || !READ_ONCE(mm->mm_cid.users))
- return;
+ lockdep_assert_held(&mc->lock);
+
/*
* mm::mm_cid::mm_cpus_allowed is the superset of each threads
* allowed CPUs mask which means it can only grow.
*/
- mc = &mm->mm_cid;
- guard(raw_spinlock)(&mc->lock);
- mm_allowed = mm_cpus_allowed(mm);
weight = cpumask_weighted_or(mm_allowed, mm_allowed, affmsk);
if (weight == mc->nr_cpus_allowed)
- return;
+ return false;
WRITE_ONCE(mc->nr_cpus_allowed, weight);
+ return true;
+}
+
+static inline void mm_update_cpus_allowed(struct mm_struct *mm, const struct cpumask *affmsk)
+{
+ struct mm_mm_cid *mc;
+
+ if (!mm || !READ_ONCE(mm->mm_cid.users))
+ return;
+
+ mc = &mm->mm_cid;
+ guard(raw_spinlock)(&mc->lock);
+
+ /* Apply the update and check whether deferred work if required */
+ if (!__mm_update_cpus_allowed(mm, affmsk))
+ return;
+
__mm_update_max_cids(mc);
if (!cid_on_cpu(mc->mode))
return;
@@ -11035,6 +11054,15 @@ static void mm_cid_fixup_tasks_to_cpus(void)
static bool sched_mm_cid_add_user(struct task_struct *t, struct mm_struct *mm)
{
lockdep_assert_held(&mm->mm_cid.lock);
+ /*
+ * Update allowed CPUs to take any affinity change, which happened
+ * before sched_mm_cid_fork() acquired the locks, into account.
+ * In per CPU mode, also recompute pcpu_thrs for the wider set, as
+ * mm_update_cpus_allowed() does.
+ */
+ if (__mm_update_cpus_allowed(mm, &t->cpus_mask) &&
+ cid_on_cpu(mm->mm_cid.mode))
+ mm->mm_cid.pcpu_thrs = mm_cid_calc_pcpu_thrs(&mm->mm_cid);
t->mm_cid.active = 1;
hlist_add_head(&t->mm_cid.node, &mm->mm_cid.user_list);
@@ -11226,11 +11254,11 @@ static void mm_cid_irq_work(struct irq_work *work)
schedule_work(&mm->mm_cid.work);
}
-void mm_init_cid(struct mm_struct *mm, struct task_struct *p)
+void mm_init_cid(struct mm_struct *mm)
{
mm->mm_cid.max_cids = 0;
mm->mm_cid.mode = 0;
- mm->mm_cid.nr_cpus_allowed = p->nr_cpus_allowed;
+ mm->mm_cid.nr_cpus_allowed = 0;
mm->mm_cid.users = 0;
mm->mm_cid.pcpu_thrs = 0;
mm->mm_cid.update_deferred = 0;
@@ -11239,7 +11267,7 @@ void mm_init_cid(struct mm_struct *mm, struct task_struct *p)
mm->mm_cid.irq_work = IRQ_WORK_INIT_HARD(mm_cid_irq_work);
INIT_WORK(&mm->mm_cid.work, mm_cid_work_fn);
INIT_HLIST_HEAD(&mm->mm_cid.user_list);
- cpumask_copy(mm_cpus_allowed(mm), &p->cpus_mask);
+ cpumask_clear(mm_cpus_allowed(mm));
bitmap_zero(mm_cidmask(mm), num_possible_cpus());
}
#else /* CONFIG_SCHED_MM_CID */
--
2.47.3