From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=158.69.130.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796171287; b=AHAemPILfYP2QdhU6Vs8bGIwFIdqY+A+BXr1kvr4TFz1wQx77slmzJCZG18WA8XhJmNG 0SZXLDMRHYBbJrSGOZKfzdWXC6NdFt30FbLGtYQMWXQuep/pEqeoanU5wq9i2IOzGDRYW W3fOFBZMcXH5rZBC/h+oj//uFAi6vYWQnvg4FMYdQsEYT+dIR1ZGcm2QoXmXaqCh4/L1P xMolL5Vc27i8XBfhnNAhs8Pw14QqXk92HHRsQPe+hWRFq3BGKp5oARkzt6p/w3DhM6vEK 8MJs6RsBwJpnPiM6WABPC0Z/68whl6xyEwKTAbcLbs//kRwB9DgxZhdKdGffNRGJhWQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796171287; h=DMARC-Filter:DKIM-Signature:Received:From:To:Cc:Subject:Date: Message-ID:X-Mailer:MIME-Version:Content-Transfer-Encoding; bh=oshO+eAnYibWiaLJaqErK1guw/8zhCCXsqUitvXyMug=; b=LpiLMw22IlRUUKpwxK9DfOtojz7BGOxYdxp5K9fTBl2aLotonut9+MlV5o4RCpm3TBO5 cLhgUmULVq9jD7Mr1wc4MbHPA3Vr/ZsBiu1nvN38J6KZTleaP3NFBL48ffbREypTqvOGD mny/vgIlyFBhn00q4MYg2Bp8OASAeJseN8xDvt1clAzbBkBbzMFXATOeHrtuvyhdTAXYh JcI/WqmgXJ2nQB6czKPXQt1OL5RrnYT9j1NWHae0xRbUXHnX+X1OmF+IyHK+/iDXNDVKh MzBy/vhFkbhNYv2yDroigrAf5nHnXRMeDYP9C7Fqpfh5VWim+bomTKA455vxVgYD6/A== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=efficios.com; spf=pass smtp.mailfrom=efficios.com; dkim=pass header.d=efficios.com header.i=@efficios.com header.a=rsa-sha256 header.s=smtpout1 header.b=AxWzZu3R; arc=none smtp.remote-ip=158.69.130.18 Received: from smtpout.efficios.com (smtpout.efficios.com [158.69.130.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B30DC606A322; Wed, 2 Dec 2026 00:28:06 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org B30DC606A322 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=efficios.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=efficios.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=efficios.com header.i=@efficios.com header.a=rsa-sha256 header.s=smtpout1 header.b=AxWzZu3R DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=efficios.com; s=smtpout1; t=1796171285; bh=oshO+eAnYibWiaLJaqErK1guw/8zhCCXsqUitvXyMug=; h=From:To:Cc:Subject:Date:From; b=AxWzZu3RG+Zr1RqfO+1CbkfNmPVEjcfhiVCF9DVCySMBW7flMF/XCMvsrf+KlyQGd rJdSFOz84y7aDxK5ftkrOWg1zzRPVEaZSLymgFhhmOEtyRUb2Lq7RhctaSBqesNOZV FIhBLjRJXVOOHjX2/cCY4f5boVnilZgDRVGgoIWeuOf4+kS5l9S2ASA5OIXu4AYlUB Qn86mCxVtI1ws39BZCl1LsAq3cKkHZTC5xGZLjZT1i+okTHIjldHi0vEScrD4LNwW/ KxzsLyY0qrMpEyRT5HXUzE9S37QfEeawanLsb7TunVZWvDOpXXTQk8pisVbLTVCqCj /sGSuHmUdQANA== Received: from thinkos.internal.efficios.com (unknown [IPv6:2606:6d00:100:4000:cacb:9855:de1f:ded2]) by smtpout.efficios.com (Postfix) with ESMTPSA id 4jfJWT240MzZ5r; Tue, 01 Dec 2026 19:28:05 -0500 (EST) From: Mathieu Desnoyers To: tglx@kernel.org, peterz@infradead.org Cc: Mathieu Desnoyers , 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 Subject: [PATCH v2 00/13] sched/mmcid: fixes for execve, mode switches and affinity Date: Tue, 1 Dec 2026 19:27:48 -0500 Message-ID: X-Mailer: git-send-email 2.47.3 X-Mailing-List: linux-kselftest@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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