Linux-mm Archive on lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v2 00/13] sched/mmcid: fixes for execve, mode switches and affinity
@ 2026-12-02  0:27 Mathieu Desnoyers
  2026-12-02  0:27 ` [PATCH v2 01/13] sched/mmcid: Move the execve() hooks into exec_mmap() Mathieu Desnoyers
                   ` (13 more replies)
  0 siblings, 14 replies; 15+ messages in thread
From: Mathieu Desnoyers @ 2026-12-02  0:27 UTC (permalink / raw)
  To: tglx, peterz
  Cc: Mathieu Desnoyers, linux-kernel, 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, kmehltretter

Hi Thomas, Peter,

This series fixes bugs of the rseq concurrency IDs (mm_cid) in
execve(), in mode switches and in affinity changes. Either the tasks
own the CIDs of an mm (per task mode), or its CPUs do (per CPU mode).
The kernel switches between these modes as the number of users
changes. The series also adds selftests for mm_cid.

Four kernel patches come first. Patches 1 to 3 are fixes, marked for
stable. Patch 4 is an optimization. The kernel patches:

  1  Move the execve() hooks into exec_mmap(). A task whose execve()
     fails then no longer leaves and rejoins the CID management of its
     mm. So it no longer returns to user space with an mm_cid of
     0x80000000, leaks a CID, or loops in mm_get_cid().
  2  Start a new mm with no allowed CPUs. Each task adds its own when
     it joins. An affinity change made during execve() or fork() is
     then no longer lost, except in a short race which patch 3 closes.
  3  Take pi_lock in sched_mm_cid_fork() and sched_mm_cid_exit().
     mm_update_cpus_allowed() then uses p->mm only while the task is
     an active user of it. This fixes the use after free reported by
     Weiming Shi, and the same window in kthreads that switch borrowed
     mms.
  4  Give no CID to io_uring and vhost workers. An SQPOLL thread then
     no longer adds its CPUs to the mm, so a process in a cpuset keeps
     compact IDs.

Patch 1 is Thomas's exec_mmap() patch from the v1 thread. Patches 2
and 3 are his affinity patch, split in two as he asked. Karl
Mehltretter proposed the placement of patch 1. Thomas agreed that I
carry the patches with small fixes folded in, and with a Suggested-by:
tag. "Changes since v1" lists these fixes.

The selftests are all in tools/testing/selftests/rseq. Each mm_cid must
stay below the number of possible CPUs. That number is the only bound:
user space sizes its per-CID data by it. The kernel also aims at
smaller IDs: below max_cids, the thread count or the CPU count of the
process. It does not promise them. A missed target makes the per-CID
data less compact, and breaks nothing. So most cases only print a note
when they miss a target. Two cases fail on one, because the bug they
check shows only that way:
- case 2 of mm_cid_execve_test: a leaked CID;
- case 2 of mm_cid_exec_affinity_test: a lost affinity change.

The selftest patches:

   5  mm_cid_execve_test: the mm_cid after a failed execve(). Fails on
      tip, passes from patch 1 on.
   6  mm_cid_transition_test: each thread increments a counter indexed
      by its mm_cid, while the number of threads crosses the mode
      thresholds. Cases 1 and 2 fail on tip, pass from patch 1 on.
   7  mm_cid_bound_test: the largest mm_cid stays below the number of
      possible CPUs while the thread count rises and falls.
   8  mm_cid_exec_affinity_test: an affinity change during execve().
      Case 2 fails on tip, passes from patch 2 on.
   9  A case of mm_cid_bound_test: the IDs after the affinity is
      widened.
  10  mm_cid_window_test: two cases, each aimed at a narrow window of a
      mode switch. Case 1 fails on tip, passes from patch 1 on.
  11  A case of mm_cid_execve_test: a single thread must not leak a CID
      after an affinity change during a failed execve(). Fails on tip,
      passes from patch 1 on.
  12  A case of mm_cid_bound_test: an io_uring SQPOLL thread in a
      cpuset. Its target is met from patch 4 on.
  13  A case of mm_cid_bound_test: clone3() into a wider cpuset. It
      covers the recompute of pcpu_thrs in the join (patch 2).

Changes since v1
================

- Thomas's two patches replace the v1 patches 1 and 3 to 5. These
  small fixes are folded in:
  - patch 1: the comment on the last user in sched_mm_cid_exit() loses
    its last sentence. It said that sched_mm_cid_after_execve() handles
    a stale transit after a failed execve(). With the hooks in
    exec_mmap(), a failed execve() no longer rejoins the mm.
  - patch 2: when a join widens the mm in per CPU mode, it also
    recomputes pcpu_thrs. A Spin model found a race which needs it.
  - patch 2: the comment above __mm_update_cpus_allowed() is reworded.
    It names its caller sched_mm_cid_add_user() and its return value.
    Patch 3 adds the active bit to it.
  - patch 3: the !CONFIG_SCHED_MM_CID stub of mm_update_cpus_allowed()
    gets its missing braces.
  - patch 3: comments on the new pi_lock rule, in the "Serialization
    rules" block and in mm_update_cpus_allowed().
- v1 patch 6, from Weiming Shi, is dropped: patch 3 fixes the same
  use after free. Patch 3 credits him with Reported-by: and a Closes:
  link. It also keeps the other Reported-by: of his v3 patch.
- v1 patch 2 is now patch 4, with no Fixes: and no Cc: stable. In my
  reply to the Sashiko review, I planned to also skip user workers in
  set_cpus_allowed_common(). This is no longer needed. With patch 4, a
  worker never joins, so it is never active. With patch 3,
  mm_update_cpus_allowed() ignores a task which is not active.
- v1 patches 7, 9, 10, 12, 14 and 15 are now patches 5 to 10.
- Case 2 of mm_cid_execve_test (v1 patch 8) is dropped. It tested a
  detail of the v1 implementation. With patch 1, a failed execve() no
  longer leaves the CID management of the mm. Case 2 is now the case
  of v1 patch 17 (patch 11).
- v1 patch 11 (a transition case with affinity changes by another
  process) found no bug, and is dropped.
- v1 patch 16 (mm_cid_teardown_test) is dropped. It aimed at the use
  after free which patch 3 fixes. A test cannot see that bug without
  KASAN, and this one did not reproduce it with KASAN.
- v1 patch 13 is dropped. Its two transition cases aimed at two past
  fixes. With each fix undone, its case did not fail. The fixes:
  - commit b2e48c429ec5 ("sched/mmcid: Prevent CID stalls due to
    concurrent forks");
  - commit 4327fb13fa47 ("sched/mmcid: Prevent live lock on task to CPU
    mode transition").
- Case 1 of mm_cid_execve_test and case 1 of mm_cid_window_test now
  fail only on an mm_cid at or above the number of possible CPUs. In
  v1 they failed at the number of allowed CPUs. That value now only
  gets a note.
- New: the io_uring SQPOLL case (patch 12). The Sashiko review of v1
  led to it.
- New: the clone3() case (patch 13).
- The selftest changelogs now name the patch that makes each failing
  case pass.

Testing
=======

The five tests have 15 cases. They ran on twelve x86-64 QEMU guests
with lockdep and 2 to 64 CPUs. Some cases skip on the small guests.
They ran three times on tip, and three times after each kernel patch.
No guest reported a kernel warning. The table shows the results of the
list above as counts. It gives each failing case, with the number of
guests where it failed in each of the three runs. "execve 1" is case 1
of mm_cid_execve_test.

  kernel             failing cases
  tip                execve 1 (12, 12, 12), execve 2 (7, 10, 9),
                     transition 1 (12, 12, 12),
                     transition 2 (10, 10, 10),
                     exec_affinity 2 (8, 8, 8: all the guests that
                     run it), window 1 (12, 12, 12)
  + patch 1          exec_affinity 2 (1, 1, 1: a guest with 16 CPUs)
  + patches 1-2      none
  + patches 1-3      none
  + patches 1-4      none

Some changelogs describe more checks:
- patch 1: a reproducer of the loop in mm_get_cid();
- patch 2: a clone3() test on tip, which is not in the series;
- patches 10 and 13: runs on kernels with a fix undone.

A Spin model checked the CID management with patches 1 to 3. It
models the mm_cid code of kernel/sched/core.c and kernel/sched/sched.h,
and its hooks in fork, exit and exec_mmap(). Each lock section is one
step. In the model:
- tasks fork, exit and call execve();
- the CPUs switch tasks at any time;
- another task changes the affinity of any task.

The model is small. 2 or 3 CPUs run 2 to 4 tasks. The number of
possible CPUs, 2 to 8, sets the size of the CID space and of the CPU
masks. 35 searches visited every state. They cover:
- 2 CPUs and 3 tasks, with one affinity change, or two without
  execve();
- 2 CPUs and 4 tasks, without affinity changes;
- 3 CPUs and 2 tasks;
- 3 CPUs and 3 tasks, without execve() and without affinity changes.

No state they reach violates these properties:

  - The CIDs are unique and below the number of possible CPUs.
  - mm_get_cid() never loops.
  - No affinity change is lost.
  - The affinity update never reads a freed mm.

Larger searches ran out of memory, among them 3 CPUs with 3 tasks
and execve(), and two affinity changes with execve(). They found
no violation in the states they visited. With one of patches 1 to 3
removed, the model reaches the bugs which that patch fixes.

The Spin model does not cover memory ordering. Five herd7 tests
checked the active check of patch 3 under the kernel memory model.
The two which model the code pass. Each of the other three changes
one thing, and fails:
- two drop pi_lock from the join or the leave;
- one sets the task active before exec_mmap() stores the new p->mm.

Neither the model nor the herd7 tests are part of the series.

How this was developed
======================

An LLM coding assistant worked under my direction. It wrote the
changelogs, the tests, the code of patch 4 and the small fixes folded
into patches 1 to 3. It also wrote the Spin model and the herd7
tests. I reviewed all of it. The rest of the code of patches 1 to 3 is
Thomas's.

Based on tip sched/urgent, edf8ee401674.

Links:
- v1: https://lore.kernel.org/all/cover.1794932121.git.mathieu.desnoyers@efficios.com/
- Thomas's exec_mmap() patch:
  https://lore.kernel.org/all/87ecc2qs9i.ffs@fw13/
- Thomas's affinity patch:
  https://lore.kernel.org/all/87v75fqnsi.ffs@fw13/
- Karl's proposal to move the hooks into exec_mmap():
  https://lore.kernel.org/all/awyrCYBrJS-CuBE8@gmail.com/
- Weiming Shi, "[PATCH v3] sched/mmcid: Synchronize MM teardown with
  affinity updates":
  https://lore.kernel.org/all/20261110183809.3308868-1-bestswngs@gmail.com/

Mathieu Desnoyers (13):
  sched/mmcid: Move the execve() hooks into exec_mmap()
  sched/mmcid: Add the allowed CPUs of each joining task to the mm
  sched/mmcid: Serialize joins and exits with affinity changes by
    pi_lock
  sched/mmcid: Give no concurrency ID to user workers
  selftests/rseq: Test the concurrency ID after a failed execve
  selftests/rseq: Test the concurrency IDs across ownership mode
    switches
  selftests/rseq: Test the upper bound of the concurrency IDs
  selftests/rseq: Test an affinity change by another process during
    execve
  selftests/rseq: Test the concurrency IDs after the affinity is widened
  selftests/rseq: Test two race windows of the concurrency ID mode
    switches
  selftests/rseq: Test the last user after an affinity change during
    execve
  selftests/rseq: Test the concurrency IDs with an io_uring SQPOLL
    thread
  selftests/rseq: Test the concurrency IDs after clone3() into a wider
    cpuset

 fs/exec.c                                     |  14 +-
 include/linux/mm_types.h                      |   5 +-
 kernel/sched/core.c                           | 106 +-
 tools/testing/selftests/rseq/.gitignore       |   5 +
 tools/testing/selftests/rseq/Makefile         |   5 +-
 .../selftests/rseq/mm_cid_bound_test.c        | 932 ++++++++++++++++++
 .../rseq/mm_cid_exec_affinity_test.c          | 288 ++++++
 .../selftests/rseq/mm_cid_execve_test.c       | 618 ++++++++++++
 .../selftests/rseq/mm_cid_transition_test.c   | 388 ++++++++
 .../selftests/rseq/mm_cid_window_test.c       | 525 ++++++++++
 10 files changed, 2853 insertions(+), 33 deletions(-)
 create mode 100644 tools/testing/selftests/rseq/mm_cid_bound_test.c
 create mode 100644 tools/testing/selftests/rseq/mm_cid_exec_affinity_test.c
 create mode 100644 tools/testing/selftests/rseq/mm_cid_execve_test.c
 create mode 100644 tools/testing/selftests/rseq/mm_cid_transition_test.c
 create mode 100644 tools/testing/selftests/rseq/mm_cid_window_test.c


base-commit: edf8ee40167473828c1ae20ab9c6442f2e66dded
-- 
2.47.3



^ permalink raw reply	[flat|nested] 15+ messages in thread

end of thread, other threads:[~2026-12-02  2:49 UTC | newest]

Thread overview: 15+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
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 ` [PATCH v2 01/13] sched/mmcid: Move the execve() hooks into exec_mmap() Mathieu Desnoyers
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
2026-12-02  2:49 ` [PATCH v2 00/13] sched/mmcid: fixes for execve, mode switches and affinity Karl Mehltretter

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox