From: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
To: tglx@kernel.org, peterz@infradead.org
Cc: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>,
linux-kernel@vger.kernel.org, mingo@redhat.com,
juri.lelli@redhat.com, vincent.guittot@linaro.org,
dietmar.eggemann@arm.com, rostedt@goodmis.org,
bsegall@google.com, mgorman@suse.de, vschneid@redhat.com,
kprateek.nayak@amd.com, paulmck@kernel.org, boqun@kernel.org,
shuah@kernel.org, kees@kernel.org, viro@zeniv.linux.org.uk,
brauner@kernel.org, jack@suse.cz, oleg@redhat.com,
bestswngs@gmail.com, xmei5@asu.edu, xujiakai24@mails.ucas.ac.cn,
linux-mm@kvack.org, linux-fsdevel@vger.kernel.org,
linux-kselftest@vger.kernel.org, kmehltretter@gmail.com,
stable@vger.kernel.org
Subject: [PATCH v2 01/13] sched/mmcid: Move the execve() hooks into exec_mmap()
Date: Tue, 1 Dec 2026 19:27:49 -0500 [thread overview]
Message-ID: <be70a178db85ef74c1c8366e235ed43ecfeac546.1796169827.git.mathieu.desnoyers@efficios.com> (raw)
In-Reply-To: <cover.1796169827.git.mathieu.desnoyers@efficios.com>
bprm_execve() makes the task leave the CID management of its mm at the
start of execve(). The task joins again at the end: the new mm on
success, the old one on failure.
sched_mm_cid_fork() handles the join. It is written for a fork() child,
which does not run yet. In per CPU mode the child gets no CID there. It
takes the CID of its CPU when it is scheduled in. When the join
switches the mode, the forking task runs the fixups. These are
mm_cid_fixup_tasks_to_cpus() and mm_cid_fixup_cpus_to_tasks(). A
successful execve() fits this design. The task is the first user of a
new mm, and the first user gets its CID at once.
A failed execve() does not fit. The task rejoins its old mm while it
runs. A failure before de_thread() leaves the other threads alive. The
CPUs can also still own CIDs of the old mm. This has three effects:
1. In per CPU mode, the task returns to user space with no CID. It
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.
mm_cid_execve_test of this series found it.
2. Per CPU mode can last down to one user. This happens with two or
three possible CPUs, or when the mm counts one allowed CPU. The mm
can count one allowed CPU while its task runs on other CPUs.
mm_update_cpus_allowed() ignores an affinity change while the mm has
no user, as inside a failed execve(). When that last user leaves,
sched_mm_cid_exit() frees only the CID of its own CPU. Other CPUs
keep theirs. The rejoin then takes a new CID, and a CID leaks. Case 2
of mm_cid_execve_test of this series shows it: a single thread reads
mm_cid 1. The comment above run_child_widened() in that test has the
sequence as a diagram. On an earlier tip, a guest with two CPUs
locked up after such a leak. This was not reproduced on tip
sched/urgent.
3. The rejoin can switch the mm to per CPU mode. If the other users
hold every CID, only the fixups can free one. The task runs them
itself, and it can be preempted before them. A and B are two threads
on CPUs 0 and 1 of a machine with two possible CPUs. The mm is in
per task mode. C is the vfork() child of B:
CPU 0 CPU 1
A: execve()
sched_mm_cid_before_execve(A)
users 2 -> 1, frees the CID of A
B: vfork()
sched_mm_cid_fork(C)
users 1 -> 2, C takes a CID
B and C hold both CIDs
A: execve() fails
sched_mm_cid_after_execve(A)
sched_mm_cid_fork(A)
users 2 -> 3, switch to per CPU mode
A: is preempted before
mm_cid_fixup_tasks_to_cpus()
A: is scheduled in again
mm_cid_schedin(A)
no CID on A or on CPU 0
mm_get_cid() loops
A model in the Spin model checker found this. A reproducer makes a
SCHED_FIFO task wake every 50us to preempt A. On tip sched/urgent,
it hung each of four guests with two CPUs. On a fifth guest, the
QEMU monitor showed a CPU in mm_get_cid() under __schedule().
Fix this by moving both hooks into exec_mmap(). The task leaves the old
mm after mm_exit_exec_release(). By then, de_thread() has killed the
other threads. Another process may still share the old mm: a vfork()
parent, or a task created with CLONE_VM and without CLONE_THREAD. The
leave is then an ordinary exit of one user. The task joins the new mm
once it is installed.
In between, only down_write_killable() and mmap_read_lock_killable()
can fail, and only with a pending SIGKILL. The task then dies. A tracer
cannot cancel a SIGKILL. It could cancel the SIGSEGV which
bprm_execve() forces on other failures past the point of no return. So
a task never rejoins its old mm. The last user no longer needs to free
the CID of its CPU in sched_mm_cid_exit().
Karl Mehltretter found that earlier versions of this change broke under
ptrace and with vfork(). He proposed the placement in exec_mmap().
With this change, mm_cid_execve_test, mm_cid_transition_test and
mm_cid_window_test no longer read 0x80000000 on guests with 2 to 64
CPUs. Case 2 of mm_cid_execve_test no longer leaks a CID. The
SCHED_FIFO reproducer completed 20000 attempts on each of four guests
with two CPUs.
Fixes: 653fda7ae73d ("sched/mmcid: Switch over to the new mechanism")
Suggested-by: Thomas Gleixner <tglx@kernel.org>
Suggested-by: Karl Mehltretter <kmehltretter@gmail.com>
Signed-off-by: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
Cc: stable@vger.kernel.org
Link: https://lore.kernel.org/all/awyrCYBrJS-CuBE8@gmail.com/
---
fs/exec.c | 13 ++++++++++---
kernel/sched/core.c | 12 ++++++------
2 files changed, 16 insertions(+), 9 deletions(-)
diff --git a/fs/exec.c b/fs/exec.c
index 8d2b14e1d455..d188f014871d 100644
--- a/fs/exec.c
+++ b/fs/exec.c
@@ -858,6 +858,15 @@ static int exec_mmap(struct linux_binprm *bprm)
/* Clean up futexes and release the mm */
mm_exit_exec_release(tsk, old_mm);
+ /*
+ * The below lock acquisitions can only fail due to a SIGKILL. In
+ * that case the task will die and even PTRACE won't be able to
+ * resurrect it. So this is the safest place to leave MM CID
+ * management as there is no way that this will go back to user
+ * space.
+ */
+ sched_mm_cid_before_execve(current);
+
ret = down_write_killable(&tsk->signal->exec_update_lock);
if (ret)
return ret;
@@ -908,6 +917,7 @@ static int exec_mmap(struct linux_binprm *bprm)
} else {
mmdrop_lazy_tlb(active_mm);
}
+ sched_mm_cid_after_execve(current);
futex_exec_done(tsk);
return 0;
}
@@ -1853,7 +1863,6 @@ static int bprm_execve(struct linux_binprm *bprm)
*/
check_unsafe_exec(bprm);
current->in_execve = 1;
- sched_mm_cid_before_execve(current);
sched_exec();
@@ -1866,7 +1875,6 @@ static int bprm_execve(struct linux_binprm *bprm)
if (retval < 0)
goto out;
- sched_mm_cid_after_execve(current);
rseq_execve(current);
/* execve succeeded */
current->in_execve = 0;
@@ -1885,7 +1893,6 @@ static int bprm_execve(struct linux_binprm *bprm)
if (bprm->point_of_no_return && !fatal_signal_pending(current))
force_fatal_sig(SIGSEGV);
- sched_mm_cid_after_execve(current);
rseq_force_update();
current->in_execve = 0;
diff --git a/kernel/sched/core.c b/kernel/sched/core.c
index 07ec834d54fd..3ad27670d67f 100644
--- a/kernel/sched/core.c
+++ b/kernel/sched/core.c
@@ -11060,7 +11060,7 @@ static void sched_mm_cid_fork(struct task_struct *t)
if (!mm->mm_cid.users) {
sched_mm_cid_add_user(t, mm);
t->mm_cid.cid = mm_get_cid(mm);
- /* Required for execve() */
+ /* Required for execve() as the task is running */
pcp->cid = t->mm_cid.cid;
return;
}
@@ -11158,10 +11158,10 @@ void sched_mm_cid_exit(struct task_struct *t)
}
/* Last user */
scoped_guard(raw_spinlock_irq, &mm->mm_cid.lock) {
- /* Required across execve() */
- if (t == current)
- mm_cid_transit_to_task(t, this_cpu_ptr(mm->mm_cid.pcpu));
- /* Ignore mode change. There is nothing to do. */
+ /*
+ * Ignore mode change. There is nothing to do as the MM
+ * is going away in the common case.
+ */
sched_mm_cid_remove_user(t);
}
}
@@ -11178,7 +11178,7 @@ void sched_mm_cid_exit(struct task_struct *t)
cancel_work_sync(&mm->mm_cid.work);
}
-/* Deactivate MM CID allocation across execve() */
+/* Deactivate MM CID allocation across execve() switching the MM */
void sched_mm_cid_before_execve(struct task_struct *t)
{
sched_mm_cid_exit(t);
--
2.47.3
next prev parent reply other threads:[~2026-12-02 0:28 UTC|newest]
Thread overview: 14+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-12-02 0:27 [PATCH v2 00/13] sched/mmcid: fixes for execve, mode switches and affinity Mathieu Desnoyers
2026-12-02 0:27 ` Mathieu Desnoyers [this message]
2026-12-02 0:27 ` [PATCH v2 02/13] sched/mmcid: Add the allowed CPUs of each joining task to the mm Mathieu Desnoyers
2026-12-02 0:27 ` [PATCH v2 03/13] sched/mmcid: Serialize joins and exits with affinity changes by pi_lock Mathieu Desnoyers
2026-12-02 0:27 ` [PATCH v2 04/13] sched/mmcid: Give no concurrency ID to user workers Mathieu Desnoyers
2026-12-02 0:27 ` [PATCH v2 05/13] selftests/rseq: Test the concurrency ID after a failed execve Mathieu Desnoyers
2026-12-02 0:27 ` [PATCH v2 06/13] selftests/rseq: Test the concurrency IDs across ownership mode switches Mathieu Desnoyers
2026-12-02 0:27 ` [PATCH v2 07/13] selftests/rseq: Test the upper bound of the concurrency IDs Mathieu Desnoyers
2026-12-02 0:27 ` [PATCH v2 08/13] selftests/rseq: Test an affinity change by another process during execve Mathieu Desnoyers
2026-12-02 0:27 ` [PATCH v2 09/13] selftests/rseq: Test the concurrency IDs after the affinity is widened Mathieu Desnoyers
2026-12-02 0:27 ` [PATCH v2 10/13] selftests/rseq: Test two race windows of the concurrency ID mode switches Mathieu Desnoyers
2026-12-02 0:27 ` [PATCH v2 11/13] selftests/rseq: Test the last user after an affinity change during execve Mathieu Desnoyers
2026-12-02 0:28 ` [PATCH v2 12/13] selftests/rseq: Test the concurrency IDs with an io_uring SQPOLL thread Mathieu Desnoyers
2026-12-02 0:28 ` [PATCH v2 13/13] selftests/rseq: Test the concurrency IDs after clone3() into a wider cpuset Mathieu Desnoyers
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=be70a178db85ef74c1c8366e235ed43ecfeac546.1796169827.git.mathieu.desnoyers@efficios.com \
--to=mathieu.desnoyers@efficios.com \
--cc=bestswngs@gmail.com \
--cc=boqun@kernel.org \
--cc=brauner@kernel.org \
--cc=bsegall@google.com \
--cc=dietmar.eggemann@arm.com \
--cc=jack@suse.cz \
--cc=juri.lelli@redhat.com \
--cc=kees@kernel.org \
--cc=kmehltretter@gmail.com \
--cc=kprateek.nayak@amd.com \
--cc=linux-fsdevel@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-kselftest@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=mgorman@suse.de \
--cc=mingo@redhat.com \
--cc=oleg@redhat.com \
--cc=paulmck@kernel.org \
--cc=peterz@infradead.org \
--cc=rostedt@goodmis.org \
--cc=shuah@kernel.org \
--cc=stable@vger.kernel.org \
--cc=tglx@kernel.org \
--cc=vincent.guittot@linaro.org \
--cc=viro@zeniv.linux.org.uk \
--cc=vschneid@redhat.com \
--cc=xmei5@asu.edu \
--cc=xujiakai24@mails.ucas.ac.cn \
/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®