* [PATCH 01/17] sched/mmcid: Give a CID to a task that joins its mm while running
[not found] <cover.1794932121.git.mathieu.desnoyers@efficios.com>
@ 2026-11-17 16:17 ` Mathieu Desnoyers
2026-11-17 16:17 ` [PATCH 02/17] sched/mmcid: Give no concurrency ID to user workers Mathieu Desnoyers
` (4 subsequent siblings)
5 siblings, 0 replies; 7+ messages in thread
From: Mathieu Desnoyers @ 2026-11-17 16:17 UTC (permalink / raw)
To: tglx, peterz
Cc: linux-kernel, Mathieu Desnoyers, mingo, juri.lelli,
vincent.guittot, dietmar.eggemann, rostedt, bsegall, mgorman,
vschneid, kprateek.nayak, paulmck, boqun, shuah, kees, viro,
brauner, jack, oleg, bestswngs, xmei5, xujiakai24, linux-mm,
linux-fsdevel, linux-kselftest, stable
sched_mm_cid_fork() makes a task a user of the CIDs of its mm. In per
CPU mode the new user gets no CID there. It takes the CID of its CPU
when it is scheduled in.
This relies on the new user not running yet. That holds for a fork()
child. A successful execve() joins a new mm as its first user, and the
first user gets a CID directly.
A failed execve() is missed. bprm_execve() leaves the mm in
sched_mm_cid_before_execve(). Its error path rejoins the same mm in
sched_mm_cid_after_execve(). If the execve() fails before
begin_new_exec() kills the other threads, they are still users of the
mm. The mm can then be in per CPU mode. The task is running, so it
returns to user space with no schedule in. Its CID is still
MM_CID_UNSET, and rseq_force_update() makes the kernel write it to the
rseq area. User space reads an mm_cid of 0x80000000 until its next
context switch. A program that indexes per-CID data with it would read
out of bounds.
An execve() of a file that no binfmt handler recognises fails in that
window. In testing, with 20 threads on a machine with 8 CPUs:
mm_cid 2147483648 (0x80000000) after a failed execve, round 0
Fix this by giving a running joiner its CID in sched_mm_cid_fork(). It
takes the CID a schedule in would give it, under the runqueue lock.
The switch back to per task mode has the same problem. Since
commit 7dbc307b6c72 ("sched/mmcid: Fix affinity-triggered switch back to
per-task mode"), a join can do the switch that an affinity change
queued. For a running joiner:
1. sched_mm_cid_fork() runs mm_cid_fixup_cpus_to_tasks().
2. The joiner is preempted. It takes a CID at its schedule in.
3. sched_mm_cid_fork() allocates a CID for the new user. That
overwrites the CID of step 2.
The bit of the first CID stays set in the bitmap, with no owner. In
testing, three threads on two CPUs then read an mm_cid of 3 or more.
Leave that allocation to a joiner that is not running. A running joiner
takes its CID as above if it has none.
Fixes: 653fda7ae73d ("sched/mmcid: Switch over to the new mechanism")
Signed-off-by: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
Cc: stable@vger.kernel.org
---
kernel/sched/core.c | 43 +++++++++++++++++++++++++++++++++++--------
1 file changed, 35 insertions(+), 8 deletions(-)
diff --git a/kernel/sched/core.c b/kernel/sched/core.c
index 1a088d4576f5..f2a981d418eb 100644
--- a/kernel/sched/core.c
+++ b/kernel/sched/core.c
@@ -10986,10 +10986,19 @@ static bool sched_mm_cid_add_user(struct task_struct *t, struct mm_struct *mm)
return mm_update_max_cids(mm);
}
+/*
+ * Make @t a user of the CIDs of its mm. Called on fork(2) and on
+ * execve(2). A failed execve(2) rejoins the mm it left.
+ *
+ * In per CPU mode a new user gets no CID here. It takes the CID of its
+ * CPU when it is scheduled in. A task that joins while it is running
+ * returns to user space before that, so it takes its CID here. This is
+ * the case of a failed execve(2) when the mm has other users.
+ */
static void sched_mm_cid_fork(struct task_struct *t)
{
struct mm_struct *mm = t->mm;
- bool percpu;
+ bool percpu = false, flip = false;
if (!mm)
return;
@@ -11012,10 +11021,14 @@ static void sched_mm_cid_fork(struct task_struct *t)
if (!sched_mm_cid_add_user(t, mm)) {
if (!cid_on_cpu(mm->mm_cid.mode))
t->mm_cid.cid = mm_get_cid(mm);
- return;
+ break;
}
- /* Handle the mode change and transfer current's CID */
+ /*
+ * Handle the mode change and transfer current's CID. A
+ * running joiner has no CID yet. It takes one below.
+ */
+ flip = true;
percpu = cid_on_cpu(mm->mm_cid.mode);
if (!percpu)
mm_cid_transit_to_task(current, pcp);
@@ -11023,11 +11036,25 @@ static void sched_mm_cid_fork(struct task_struct *t)
mm_cid_transit_to_cpu(current, pcp);
}
- if (percpu) {
- mm_cid_fixup_tasks_to_cpus();
- } else {
- mm_cid_fixup_cpus_to_tasks(mm);
- t->mm_cid.cid = mm_get_cid(mm);
+ if (flip) {
+ if (percpu) {
+ mm_cid_fixup_tasks_to_cpus();
+ } else {
+ mm_cid_fixup_cpus_to_tasks(mm);
+ /*
+ * A running joiner may have taken a CID at a
+ * schedule in. If not, it takes one below.
+ */
+ if (t != current)
+ t->mm_cid.cid = mm_get_cid(mm);
+ }
+ }
+
+ /* A running joiner with no CID takes one now, see above. */
+ if (t == current) {
+ guard(task_rq_lock)(t);
+ if (t->mm_cid.cid == MM_CID_UNSET)
+ mm_cid_schedin(t);
}
}
--
2.47.3
^ permalink raw reply related [flat|nested] 7+ messages in thread* [PATCH 02/17] sched/mmcid: Give no concurrency ID to user workers
[not found] <cover.1794932121.git.mathieu.desnoyers@efficios.com>
2026-11-17 16:17 ` [PATCH 01/17] sched/mmcid: Give a CID to a task that joins its mm while running Mathieu Desnoyers
@ 2026-11-17 16:17 ` Mathieu Desnoyers
2026-11-29 21:54 ` Thomas Gleixner
2026-11-17 16:17 ` [PATCH 03/17] sched/mmcid: Reset stale state when the first user joins an mm Mathieu Desnoyers
` (3 subsequent siblings)
5 siblings, 1 reply; 7+ messages in thread
From: Mathieu Desnoyers @ 2026-11-17 16:17 UTC (permalink / raw)
To: tglx, peterz
Cc: linux-kernel, Mathieu Desnoyers, mingo, juri.lelli,
vincent.guittot, dietmar.eggemann, rostedt, bsegall, mgorman,
vschneid, kprateek.nayak, paulmck, boqun, shuah, kees, viro,
brauner, jack, oleg, bestswngs, xmei5, xujiakai24, linux-mm,
linux-fsdevel, linux-kselftest, stable
The workers of io_uring and vhost are PF_USER_WORKER tasks. They are
threads of the process they work for, created with CLONE_VM. They only
run kernel code: copy_process() gives them a kernel function that ends
in do_exit(). They never return to user space and have no rseq area.
sched_mm_cid_fork() still makes them users of the CIDs of the mm, like
every task with an mm. A worker takes a CID whenever it runs. It also
counts as a user for max_cids and for the switch to per CPU mode.
Nothing reports the CID of a worker. But it is one CID fewer for the
threads of the process, which then get larger ones. The added users can
also switch the mm to per CPU mode. A process with io_uring workers
would then read mm_cid values at or above its number of threads. This
was found by reading the code, and has no test.
Fix this by not making a user worker a user of the CIDs. Kernel threads
are already left out, as they have no mm.
Fixes: af7f588d8f73 ("sched: Introduce per-memory-map concurrency ID")
Signed-off-by: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
Cc: stable@vger.kernel.org
---
kernel/sched/core.c | 6 +++++-
1 file changed, 5 insertions(+), 1 deletion(-)
diff --git a/kernel/sched/core.c b/kernel/sched/core.c
index f2a981d418eb..7e213e88ac28 100644
--- a/kernel/sched/core.c
+++ b/kernel/sched/core.c
@@ -11000,7 +11000,11 @@ static void sched_mm_cid_fork(struct task_struct *t)
struct mm_struct *mm = t->mm;
bool percpu = false, flip = false;
- if (!mm)
+ /*
+ * A user worker (io_uring, vhost) never returns to user space and
+ * has no rseq area. It needs no CID.
+ */
+ if (!mm || (t->flags & PF_USER_WORKER))
return;
WARN_ON_ONCE(t->mm_cid.cid != MM_CID_UNSET);
--
2.47.3
^ permalink raw reply related [flat|nested] 7+ messages in thread* Re: [PATCH 02/17] sched/mmcid: Give no concurrency ID to user workers
2026-11-17 16:17 ` [PATCH 02/17] sched/mmcid: Give no concurrency ID to user workers Mathieu Desnoyers
@ 2026-11-29 21:54 ` Thomas Gleixner
0 siblings, 0 replies; 7+ messages in thread
From: Thomas Gleixner @ 2026-11-29 21:54 UTC (permalink / raw)
To: Mathieu Desnoyers, peterz
Cc: linux-kernel, Mathieu Desnoyers, mingo, juri.lelli,
vincent.guittot, dietmar.eggemann, rostedt, bsegall, mgorman,
vschneid, kprateek.nayak, paulmck, boqun, shuah, kees, viro,
brauner, jack, oleg, bestswngs, xmei5, xujiakai24, linux-mm,
linux-fsdevel, linux-kselftest, stable
On Tue, Nov 17 2026 at 11:17, Mathieu Desnoyers wrote:
> The workers of io_uring and vhost are PF_USER_WORKER tasks. They are
> threads of the process they work for, created with CLONE_VM. They only
> run kernel code: copy_process() gives them a kernel function that ends
> in do_exit(). They never return to user space and have no rseq area.
>
> sched_mm_cid_fork() still makes them users of the CIDs of the mm, like
> every task with an mm. A worker takes a CID whenever it runs. It also
> counts as a user for max_cids and for the switch to per CPU mode.
>
> Nothing reports the CID of a worker. But it is one CID fewer for the
> threads of the process, which then get larger ones. The added users can
> also switch the mm to per CPU mode. A process with io_uring workers
> would then read mm_cid values at or above its number of threads. This
> was found by reading the code, and has no test.
>
> Fix this by not making a user worker a user of the CIDs. Kernel threads
> are already left out, as they have no mm.
>
> Fixes: af7f588d8f73 ("sched: Introduce per-memory-map concurrency ID")
That's not a fix. That's an optimization and has zero impact on
correctness.
Thanks,
tglx
^ permalink raw reply [flat|nested] 7+ messages in thread
* [PATCH 03/17] sched/mmcid: Reset stale state when the first user joins an mm
[not found] <cover.1794932121.git.mathieu.desnoyers@efficios.com>
2026-11-17 16:17 ` [PATCH 01/17] sched/mmcid: Give a CID to a task that joins its mm while running Mathieu Desnoyers
2026-11-17 16:17 ` [PATCH 02/17] sched/mmcid: Give no concurrency ID to user workers Mathieu Desnoyers
@ 2026-11-17 16:17 ` Mathieu Desnoyers
2026-11-17 16:17 ` [PATCH 04/17] sched/mmcid: Let a running joiner take its CID after the fixups Mathieu Desnoyers
` (2 subsequent siblings)
5 siblings, 0 replies; 7+ messages in thread
From: Mathieu Desnoyers @ 2026-11-17 16:17 UTC (permalink / raw)
To: tglx, peterz
Cc: linux-kernel, Mathieu Desnoyers, mingo, juri.lelli,
vincent.guittot, dietmar.eggemann, rostedt, bsegall, mgorman,
vschneid, kprateek.nayak, paulmck, boqun, shuah, kees, viro,
brauner, jack, oleg, bestswngs, xmei5, xujiakai24, linux-mm,
linux-fsdevel, linux-kselftest, stable
sched_mm_cid_exit() ignores the mode change when the last user of an mm
leaves. Its comment says there is nothing to do. That holds for an exit:
the mm goes away. It holds for a successful execve() too, which joins a
new mm.
A failed execve() is missed. It leaves the mm in
sched_mm_cid_before_execve() and rejoins the same mm in
sched_mm_cid_after_execve(). If the last user left in per CPU mode,
MM_CID_TRANSIT stays set and the other CPUs still own their CIDs.
In the diagram below, the machine has two CPUs. T is the main thread
of a process. The four threads it creates only wait. H is another
process. It stays on CPU 1.
CPU 0 CPU 1
T: creates four threads
the mm switches to per CPU mode
T: runs on CPU 1
CPU 1 owns a CID
T: runs on CPU 0 again
CPU 0 owns a CID
T: joins the four threads
T is the only user left
T: execve()
sched_mm_cid_before_execve()
T leaves as the last user
drops the CID of CPU 0
MM_CID_TRANSIT is set
CPU 1 still owns its CID
H: sched_setaffinity(T, CPU 1)
T: migrates to CPU 1
execve() fails
sched_mm_cid_after_execve()
sched_mm_cid_fork(T)
T is the first user
mm_get_cid() succeeds
writes that CID to the
storage of CPU 1
the CID CPU 1 owned keeps
its bit, with no owner
The mm has one CID fewer from then on. Once every remaining CID is
taken, mm_get_cid() loops forever with interrupts disabled. On a guest
with two CPUs, the next pthread_create() of T does not return:
rcu: INFO: rcu_preempt detected stalls on CPUs/tasks:
RIP: 0010:sched_mm_cid_fork+0x1dd/0x660
copy_process+0x2339/0x2ca0
kernel_clone+0xc3/0x3c0
__do_sys_clone3+0xe2/0x130
Per CPU mode lasts down to one user when mm_cid_calc_pcpu_thrs() returns
1. It does with two or three possible CPUs. It also does on any machine
when the mm is allowed a single CPU. The lockup was reproduced in the
first case only.
Fix this in sched_mm_cid_fork(), when the first user joins an mm whose
mode is not 0. Clear the mode, the CID of every CPU and the CID bitmap,
and take the allowed CPUs of the task. A new mm has mode 0, so this
does nothing for it.
sched_mm_cid_exit() stays as it is. The last user of an mm that goes
away runs no fixup.
Also check the user count again under the lock in
mm_update_cpus_allowed(). An affinity change can race with the last
user leaving. It would change the allowed CPUs of an mm with no user.
With this patch, in the same test, T keeps CID 0 after the failed
execve() and its next pthread_create() returns.
[ mathieu: wrote the changelog from the thread; the code is Thomas's
draft, placed on the current sched_mm_cid_fork() ]
Fixes: 653fda7ae73d ("sched/mmcid: Switch over to the new mechanism")
Originally-by: Thomas Gleixner <tglx@kernel.org>
Signed-off-by: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
Cc: stable@vger.kernel.org
Link: https://lore.kernel.org/all/87mrs8bhp4.ffs@fw13/
---
kernel/sched/core.c | 32 ++++++++++++++++++++++++++++++++
1 file changed, 32 insertions(+)
diff --git a/kernel/sched/core.c b/kernel/sched/core.c
index 7e213e88ac28..ab68e8605eed 100644
--- a/kernel/sched/core.c
+++ b/kernel/sched/core.c
@@ -10847,6 +10847,10 @@ static inline void mm_update_cpus_allowed(struct mm_struct *mm, const struct cpu
*/
mc = &mm->mm_cid;
guard(raw_spinlock)(&mc->lock);
+ /* Might have raced with exit() */
+ if (!mm->mm_cid.users)
+ return;
+
mm_allowed = mm_cpus_allowed(mm);
weight = cpumask_weighted_or(mm_allowed, mm_allowed, affmsk);
if (weight == mc->nr_cpus_allowed)
@@ -10986,6 +10990,33 @@ static bool sched_mm_cid_add_user(struct task_struct *t, struct mm_struct *mm)
return mm_update_max_cids(mm);
}
+/*
+ * sched_mm_cid_exit() does not handle the mode switch for the last user
+ * because for normal exit() that's a pointless exercise. But if the
+ * process which issued execve() was multithreaded and the last exiting
+ * thread switched back from per CPU mode then this leaves stale state
+ * around. The indicator for that is mm_cid::mode having MM_CID_TRANSIT
+ * set.
+ */
+static void sched_mm_cid_cleanup_execve(struct task_struct *t, struct mm_struct *mm)
+{
+ if (likely(!mm->mm_cid.mode))
+ return;
+
+ WARN_ON_ONCE(mm->mm_cid.mode & MM_CID_ONCPU);
+ mm->mm_cid.mode = 0;
+
+ unsigned int cpu;
+
+ for_each_possible_cpu(cpu)
+ per_cpu(mm->mm_cid.pcpu->cid, cpu) = 0;
+
+ bitmap_zero(mm_cidmask(mm), num_possible_cpus());
+
+ mm->mm_cid.nr_cpus_allowed = t->nr_cpus_allowed;
+ cpumask_copy(mm_cpus_allowed(mm), &t->cpus_mask);
+}
+
/*
* Make @t a user of the CIDs of its mm. Called on fork(2) and on
* execve(2). A failed execve(2) rejoins the mm it left.
@@ -11015,6 +11046,7 @@ static void sched_mm_cid_fork(struct task_struct *t)
/* First user ? */
if (!mm->mm_cid.users) {
+ sched_mm_cid_cleanup_execve(t, mm);
sched_mm_cid_add_user(t, mm);
t->mm_cid.cid = mm_get_cid(mm);
/* Required for execve() */
--
2.47.3
^ permalink raw reply related [flat|nested] 7+ messages in thread* [PATCH 04/17] sched/mmcid: Let a running joiner take its CID after the fixups
[not found] <cover.1794932121.git.mathieu.desnoyers@efficios.com>
` (2 preceding siblings ...)
2026-11-17 16:17 ` [PATCH 03/17] sched/mmcid: Reset stale state when the first user joins an mm Mathieu Desnoyers
@ 2026-11-17 16:17 ` Mathieu Desnoyers
2026-11-17 16:17 ` [PATCH 05/17] sched/mmcid: Take the allowed CPUs of the first user when it joins Mathieu Desnoyers
2026-11-17 16:17 ` [PATCH 06/17] sched/mmcid: Synchronize MM teardown with affinity updates Mathieu Desnoyers
5 siblings, 0 replies; 7+ messages in thread
From: Mathieu Desnoyers @ 2026-11-17 16:17 UTC (permalink / raw)
To: tglx, peterz
Cc: linux-kernel, Mathieu Desnoyers, mingo, juri.lelli,
vincent.guittot, dietmar.eggemann, rostedt, bsegall, mgorman,
vschneid, kprateek.nayak, paulmck, boqun, shuah, kees, viro,
brauner, jack, oleg, bestswngs, xmei5, xujiakai24, linux-mm,
linux-fsdevel, linux-kselftest, stable
The switch to per CPU mode relies on the new user not running yet. The
comment above the code says so: the new user is not visible in the
system, and the task that forks it runs the fixup.
A failed execve() is missed. It rejoins its mm in
sched_mm_cid_after_execve(): the new user is the running task, and it
runs the fixup itself. If it is preempted before the fixup is done,
mm_get_cid() can loop forever at its schedule in, with the runqueue
lock held.
In the diagram below, the machine has two possible CPUs. A and B are
threads of one process, which is in per task mode with two CIDs. C is
the vfork() child of B. A holds mm_cid.mutex from sched_mm_cid_fork(A)
on.
CPU 0 CPU 1
A: execve()
sched_mm_cid_before_execve()
A leaves, users = 1
B: vfork()
sched_mm_cid_fork(C)
users = 2
C takes the last CID
A: execve() fails
sched_mm_cid_after_execve()
sched_mm_cid_fork(A)
users = 3, switch to per CPU mode
A is active and has no CID
A is preempted
A is scheduled in
mm_cid_schedin(A)
mm_get_cid() loops:
B and C hold both CIDs
C: exit()
sched_mm_cid_exit()
waits for mm_cid.mutex,
which A holds
Nothing ends the loop. Only the fixup could free a CID, and A is the
task that runs it.
A guest with two CPUs hangs on this sequence when a SCHED_FIFO task
wakes every 50us on CPU 0 to preempt A. CPU 0 stays in __schedule() and
CPU 1 in queued_spin_lock_slowpath(). With this change the same guest
completes 20000 attempts.
Fix this by keeping a running joiner inactive until the fixups are done.
A schedule in gives it no CID before that. sched_mm_cid_fork() then
marks it active, and it takes its CID under the runqueue lock.
Fixes: 653fda7ae73d ("sched/mmcid: Switch over to the new mechanism")
Signed-off-by: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
Cc: stable@vger.kernel.org
---
kernel/sched/core.c | 20 +++++++++++++-------
1 file changed, 13 insertions(+), 7 deletions(-)
diff --git a/kernel/sched/core.c b/kernel/sched/core.c
index ab68e8605eed..510cd775723b 100644
--- a/kernel/sched/core.c
+++ b/kernel/sched/core.c
@@ -11054,7 +11054,16 @@ static void sched_mm_cid_fork(struct task_struct *t)
return;
}
- if (!sched_mm_cid_add_user(t, mm)) {
+ flip = sched_mm_cid_add_user(t, mm);
+ /*
+ * A running joiner takes no CID at a schedule in before the
+ * fixups are done. The users it joins can hold every CID
+ * until then, and the fixups are its own work.
+ */
+ if (t == current)
+ t->mm_cid.active = 0;
+
+ if (!flip) {
if (!cid_on_cpu(mm->mm_cid.mode))
t->mm_cid.cid = mm_get_cid(mm);
break;
@@ -11064,7 +11073,6 @@ static void sched_mm_cid_fork(struct task_struct *t)
* Handle the mode change and transfer current's CID. A
* running joiner has no CID yet. It takes one below.
*/
- flip = true;
percpu = cid_on_cpu(mm->mm_cid.mode);
if (!percpu)
mm_cid_transit_to_task(current, pcp);
@@ -11077,18 +11085,16 @@ static void sched_mm_cid_fork(struct task_struct *t)
mm_cid_fixup_tasks_to_cpus();
} else {
mm_cid_fixup_cpus_to_tasks(mm);
- /*
- * A running joiner may have taken a CID at a
- * schedule in. If not, it takes one below.
- */
+ /* A running joiner takes its CID below. */
if (t != current)
t->mm_cid.cid = mm_get_cid(mm);
}
}
- /* A running joiner with no CID takes one now, see above. */
+ /* The running joiner takes part from here on, see above. */
if (t == current) {
guard(task_rq_lock)(t);
+ t->mm_cid.active = 1;
if (t->mm_cid.cid == MM_CID_UNSET)
mm_cid_schedin(t);
}
--
2.47.3
^ permalink raw reply related [flat|nested] 7+ messages in thread* [PATCH 05/17] sched/mmcid: Take the allowed CPUs of the first user when it joins
[not found] <cover.1794932121.git.mathieu.desnoyers@efficios.com>
` (3 preceding siblings ...)
2026-11-17 16:17 ` [PATCH 04/17] sched/mmcid: Let a running joiner take its CID after the fixups Mathieu Desnoyers
@ 2026-11-17 16:17 ` Mathieu Desnoyers
2026-11-17 16:17 ` [PATCH 06/17] sched/mmcid: Synchronize MM teardown with affinity updates Mathieu Desnoyers
5 siblings, 0 replies; 7+ messages in thread
From: Mathieu Desnoyers @ 2026-11-17 16:17 UTC (permalink / raw)
To: tglx, peterz
Cc: linux-kernel, Mathieu Desnoyers, mingo, juri.lelli,
vincent.guittot, dietmar.eggemann, rostedt, bsegall, mgorman,
vschneid, kprateek.nayak, paulmck, boqun, shuah, kees, viro,
brauner, jack, oleg, bestswngs, xmei5, xujiakai24, linux-mm,
linux-fsdevel, linux-kselftest, stable
The CIDs of an mm are sized from the CPUs its threads are allowed on.
mm_init_cid() copies the allowed CPUs of the task into a new mm. Later
changes reach the mm through mm_update_cpus_allowed(). It uses the mm
the task points to, and ignores an mm with no user.
An affinity change between mm_init_cid() and the join of the first user
is lost. In this example, P is a task allowed on one CPU, and H is
another task:
P H
execve()
exec_mmap()
mm_init_cid()
copies the allowed CPUs of P
sched_setaffinity(P, all CPUs)
mm_update_cpus_allowed()
the new mm has no user:
ignored
sched_mm_cid_after_execve()
P joins the new mm, which
counts one allowed CPU
From reading the code, a fork() child has the same window between
dup_mm() and sched_mm_cid_fork().
The new mm then stays sized for the CPUs allowed at that time. A
process that starts on one CPU and is widened to 64 CPUs during its
execve() switches to per CPU mode with its second thread. In testing,
two threads of such a process read mm_cid 0 and 63. The IDs stay below
the number of possible CPUs. They are no longer compact.
Fix this by adding the allowed CPUs of the first user to the mm when it
joins. Hold t->pi_lock for that, as an affinity change holds it. A
concurrent change is then in t->cpus_mask already, or it finds the user
and updates the mm itself.
Fixes: b0c3d51b54f8 ("sched/mmcid: Provide precomputed maximal value")
Signed-off-by: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
Cc: stable@vger.kernel.org
---
kernel/sched/core.c | 65 +++++++++++++++++++++++++++++++--------------
1 file changed, 45 insertions(+), 20 deletions(-)
diff --git a/kernel/sched/core.c b/kernel/sched/core.c
index 510cd775723b..456b76e823b3 100644
--- a/kernel/sched/core.c
+++ b/kernel/sched/core.c
@@ -10833,24 +10833,17 @@ 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)
+static void __mm_update_cpus_allowed(struct mm_struct *mm, const struct cpumask *affmsk)
{
+ struct mm_mm_cid *mc = &mm->mm_cid;
struct cpumask *mm_allowed;
- struct mm_mm_cid *mc;
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);
- /* Might have raced with exit() */
- if (!mm->mm_cid.users)
- return;
-
mm_allowed = mm_cpus_allowed(mm);
weight = cpumask_weighted_or(mm_allowed, mm_allowed, affmsk);
if (weight == mc->nr_cpus_allowed)
@@ -10876,6 +10869,20 @@ static inline void mm_update_cpus_allowed(struct mm_struct *mm, const struct cpu
irq_work_queue(&mc->irq_work);
}
+static inline void mm_update_cpus_allowed(struct mm_struct *mm, const struct cpumask *affmsk)
+{
+ /* The first user takes the allowed CPUs of its task when it joins */
+ if (!mm || !READ_ONCE(mm->mm_cid.users))
+ return;
+
+ guard(raw_spinlock)(&mm->mm_cid.lock);
+ /* Might have raced with exit() */
+ if (!mm->mm_cid.users)
+ return;
+
+ __mm_update_cpus_allowed(mm, affmsk);
+}
+
static inline void mm_cid_complete_transit(struct mm_struct *mm, unsigned int mode)
{
/*
@@ -11017,6 +11024,28 @@ static void sched_mm_cid_cleanup_execve(struct task_struct *t, struct mm_struct
cpumask_copy(mm_cpus_allowed(mm), &t->cpus_mask);
}
+/*
+ * Make @t the first user of the CIDs of its mm.
+ *
+ * mm_init_cid() copied the allowed CPUs of @t before @t was a user. An
+ * affinity change since then did not reach the mm. Add the allowed CPUs
+ * of @t again. Hold t->pi_lock, which every affinity change takes. A
+ * concurrent change is then in t->cpus_mask already, or it finds the
+ * user.
+ */
+static void sched_mm_cid_add_first_user(struct task_struct *t, struct mm_struct *mm)
+{
+ guard(raw_spinlock_irq)(&t->pi_lock);
+ guard(raw_spinlock)(&mm->mm_cid.lock);
+
+ sched_mm_cid_cleanup_execve(t, mm);
+ sched_mm_cid_add_user(t, mm);
+ __mm_update_cpus_allowed(mm, &t->cpus_mask);
+ t->mm_cid.cid = mm_get_cid(mm);
+ /* Required for execve() */
+ __this_cpu_write(mm->mm_cid.pcpu->cid, t->mm_cid.cid);
+}
+
/*
* Make @t a user of the CIDs of its mm. Called on fork(2) and on
* execve(2). A failed execve(2) rejoins the mm it left.
@@ -11041,19 +11070,15 @@ static void sched_mm_cid_fork(struct task_struct *t)
WARN_ON_ONCE(t->mm_cid.cid != MM_CID_UNSET);
guard(mutex)(&mm->mm_cid.mutex);
+ /* The mutex keeps other users from joining or leaving */
+ if (!mm->mm_cid.users) {
+ sched_mm_cid_add_first_user(t, mm);
+ return;
+ }
+
scoped_guard(raw_spinlock_irq, &mm->mm_cid.lock) {
struct mm_cid_pcpu *pcp = this_cpu_ptr(mm->mm_cid.pcpu);
- /* First user ? */
- if (!mm->mm_cid.users) {
- sched_mm_cid_cleanup_execve(t, mm);
- sched_mm_cid_add_user(t, mm);
- t->mm_cid.cid = mm_get_cid(mm);
- /* Required for execve() */
- pcp->cid = t->mm_cid.cid;
- return;
- }
-
flip = sched_mm_cid_add_user(t, mm);
/*
* A running joiner takes no CID at a schedule in before the
--
2.47.3
^ permalink raw reply related [flat|nested] 7+ messages in thread* [PATCH 06/17] sched/mmcid: Synchronize MM teardown with affinity updates
[not found] <cover.1794932121.git.mathieu.desnoyers@efficios.com>
` (4 preceding siblings ...)
2026-11-17 16:17 ` [PATCH 05/17] sched/mmcid: Take the allowed CPUs of the first user when it joins Mathieu Desnoyers
@ 2026-11-17 16:17 ` Mathieu Desnoyers
5 siblings, 0 replies; 7+ messages in thread
From: Mathieu Desnoyers @ 2026-11-17 16:17 UTC (permalink / raw)
To: tglx, peterz
Cc: linux-kernel, Mathieu Desnoyers, mingo, juri.lelli,
vincent.guittot, dietmar.eggemann, rostedt, bsegall, mgorman,
vschneid, kprateek.nayak, paulmck, boqun, shuah, kees, viro,
brauner, jack, oleg, bestswngs, xmei5, xujiakai24, linux-mm,
linux-fsdevel, linux-kselftest, stable, co+cab19e2ebe4d535a
From: Weiming Shi <bestswngs@gmail.com>
set_cpus_allowed_common() passes a remote task's p->mm to
mm_update_cpus_allowed() while holding p->pi_lock, but that lock did not
stabilize the pointer against MM replacement. A same-owner
sched_setaffinity() caller can therefore retain the old pointer while exec
replaces and frees it. On architectures which shoot down rather than
refcount lazy TLB MMs, exit_mm() and kthread_unuse_mm() have the same
lifetime gap.
BUG: KASAN: slab-use-after-free in set_cpus_allowed_common
Read of size 4 at addr ffff888123155e4c by task poc/5132
Call Trace:
set_cpus_allowed_common (kernel/sched/core.c:10835)
set_cpus_allowed_fair (kernel/sched/fair.c:10046)
__set_cpus_allowed_ptr_locked (kernel/sched/core.c:3224)
__set_cpus_allowed_ptr (kernel/sched/core.c:3258)
__sched_setaffinity (kernel/sched/syscalls.c:1161)
sched_setaffinity (kernel/sched/syscalls.c:1239)
__x64_sys_sched_setaffinity (kernel/sched/syscalls.c:1275)
do_syscall_64 (arch/x86/entry/syscall_64.c:84)
entry_SYSCALL_64_after_hwframe (arch/x86/entry/entry_64.S:121)
Freed by task 5127:
kmem_cache_free (mm/slub.c:6757)
__mmput (kernel/fork.c:1204)
mmput (kernel/fork.c:1215)
setup_new_exec (fs/exec.c:1395)
load_elf_binary (fs/binfmt_elf.c:1024)
bprm_execve (fs/exec.c:1865)
do_execveat_common.isra.0 (fs/exec.c:1963)
__x64_sys_execveat (fs/exec.c:2047)
do_syscall_64 (arch/x86/entry/syscall_64.c:84)
entry_SYSCALL_64_after_hwframe (arch/x86/entry/entry_64.S:121)
Kernel panic - not syncing: KASAN: panic_on_warn set ...
Affinity paths hold p->pi_lock across the p->mm load and its complete use.
After publishing a replacement or NULL p->mm, take and release that same
lock before dropping the old MM reference. For an updater that is already
using the old pointer, the ordering is:
affinity updater MM teardown
---------------- -----------
raw_spin_lock(&p->pi_lock)
mm = p->mm /* old_mm */
p->mm = new_mm or NULL
raw_spin_lock(&p->pi_lock)
/* waits */
mm_update_cpus_allowed(mm, mask)
raw_spin_unlock(&p->pi_lock) /* release */
/* acquire succeeds */
raw_spin_unlock(&p->pi_lock)
...
mmput/mmdrop(old_mm)
The teardown-side acquire cannot complete until an updater ordered before
it has released p->pi_lock, so old_mm remains alive throughout that
updater's access. If teardown is ordered first instead, its unlock is a
release that orders the preceding p->mm store before a later updater's lock
acquire. The later updater therefore observes new_mm or NULL. An updater
that acquired the lock before the store but has not loaded p->mm yet is
still ordered before the teardown-side acquisition; it may observe either
pointer, but teardown waits for its complete use before dropping old_mm.
All runtime callers of set_cpus_allowed_common() hold p->pi_lock across
this access. The runqueue lock is not part of the p->mm lifetime ordering,
so the grace period only needs p->pi_lock. Place it after publication and
before the old reference drop in exec_mmap(), exit_mm(), and
kthread_unuse_mm(). The last two paths matter on architectures which shoot
down rather than refcount lazy TLB MMs.
This change was developed with assistance from an LLM.
Fixes: 7e019dcc470f ("sched: Improve cache locality of RSEQ concurrency IDs for intermittent workloads")
Cc: stable@vger.kernel.org
Reported-by: <co+cab19e2ebe4d535a@bugs.sh>
Signed-off-by: Weiming Shi <bestswngs@gmail.com>
Signed-off-by: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
Link: https://lore.kernel.org/all/20261110183809.3308868-1-bestswngs@gmail.com/
---
fs/exec.c | 1 +
include/linux/sched.h | 2 ++
kernel/exit.c | 1 +
kernel/kthread.c | 1 +
kernel/sched/core.c | 9 +++++++++
5 files changed, 14 insertions(+)
diff --git a/fs/exec.c b/fs/exec.c
index 8d2b14e1d455..3196421cb81d 100644
--- a/fs/exec.c
+++ b/fs/exec.c
@@ -905,6 +905,7 @@ static int exec_mmap(struct linux_binprm *bprm)
BUG_ON(active_mm != old_mm);
/* Defer teardown to setup_new_exec(), outside the exec locks. */
bprm->old_mm = old_mm;
+ sched_mm_cid_mm_barrier(tsk);
} else {
mmdrop_lazy_tlb(active_mm);
}
diff --git a/include/linux/sched.h b/include/linux/sched.h
index e4f6323d7855..8da4b88f434c 100644
--- a/include/linux/sched.h
+++ b/include/linux/sched.h
@@ -2360,6 +2360,7 @@ static __always_inline void alloc_tag_restore(struct alloc_tag *tag, struct allo
#ifdef CONFIG_SCHED_MM_CID
void sched_mm_cid_before_execve(struct task_struct *t);
void sched_mm_cid_after_execve(struct task_struct *t);
+void sched_mm_cid_mm_barrier(struct task_struct *t);
void sched_mm_cid_exit(struct task_struct *t);
static __always_inline int task_mm_cid(struct task_struct *t)
{
@@ -2368,6 +2369,7 @@ static __always_inline int task_mm_cid(struct task_struct *t)
#else
static inline void sched_mm_cid_before_execve(struct task_struct *t) { }
static inline void sched_mm_cid_after_execve(struct task_struct *t) { }
+static inline void sched_mm_cid_mm_barrier(struct task_struct *t) { }
static inline void sched_mm_cid_exit(struct task_struct *t) { }
static __always_inline int task_mm_cid(struct task_struct *t)
{
diff --git a/kernel/exit.c b/kernel/exit.c
index d5e7bc2af804..5f9979d6216c 100644
--- a/kernel/exit.c
+++ b/kernel/exit.c
@@ -582,6 +582,7 @@ static void exit_mm(void)
local_irq_enable();
task_unlock(current);
mmap_read_unlock(mm);
+ sched_mm_cid_mm_barrier(current);
mm_update_next_owner(mm);
mmput(mm);
if (test_thread_flag(TIF_MEMDIE))
diff --git a/kernel/kthread.c b/kernel/kthread.c
index fdb8fb07f40b..e7b393cfdcee 100644
--- a/kernel/kthread.c
+++ b/kernel/kthread.c
@@ -1682,6 +1682,7 @@ void kthread_unuse_mm(struct mm_struct *mm)
enter_lazy_tlb(mm, tsk);
local_irq_enable();
task_unlock(tsk);
+ sched_mm_cid_mm_barrier(tsk);
mmdrop(mm);
}
diff --git a/kernel/sched/core.c b/kernel/sched/core.c
index 456b76e823b3..c4475344f6f2 100644
--- a/kernel/sched/core.c
+++ b/kernel/sched/core.c
@@ -11229,6 +11229,15 @@ void sched_mm_cid_after_execve(struct task_struct *t)
sched_mm_cid_fork(t);
}
+/*
+ * Pair with set_cpus_allowed_common()'s p->pi_lock critical section. Call
+ * after publishing p->mm and before dropping the old MM reference.
+ */
+void sched_mm_cid_mm_barrier(struct task_struct *t)
+{
+ guard(raw_spinlock_irq)(&t->pi_lock);
+}
+
static void mm_cid_work_fn(struct work_struct *work)
{
struct mm_struct *mm = container_of(work, struct mm_struct, mm_cid.work);
--
2.47.3
^ permalink raw reply related [flat|nested] 7+ messages in thread