From: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
To: tglx@kernel.org, peterz@infradead.org
Cc: linux-kernel@vger.kernel.org,
Mathieu Desnoyers <mathieu.desnoyers@efficios.com>,
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
Subject: [PATCH 11/17] selftests/rseq: Test affinity changes made by another process
Date: Tue, 17 Nov 2026 11:17:11 -0500 [thread overview]
Message-ID: <926187ef9c1ed8c1b197cb7fa854339c01ee5e6b.1794932121.git.mathieu.desnoyers@efficios.com> (raw)
In-Reply-To: <cover.1794932121.git.mathieu.desnoyers@efficios.com>
The mm_cid tests change the affinity of a thread from inside its own
process. Another process can change it too, with sched_setaffinity() on
a thread ID. The kernel then updates the allowed CPUs of an mm that is
not the mm of the caller. The threads of that mm can exit or call
vfork() at the same time, and the whole process can exit. No test
covers this.
Add a case to mm_cid_transition_test. A process starts on two CPUs with
three counting threads. Another process narrows and widens the affinity
of each of these threads in a loop. It goes on while the process under
test exits. The case runs 100 times, and the changes start after a
different delay each time.
The case passes on guests with 2 to 64 CPUs. It adds coverage: it
found no bug.
Signed-off-by: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
---
.../selftests/rseq/mm_cid_transition_test.c | 69 ++++++++++++++++++-
1 file changed, 68 insertions(+), 1 deletion(-)
diff --git a/tools/testing/selftests/rseq/mm_cid_transition_test.c b/tools/testing/selftests/rseq/mm_cid_transition_test.c
index 7c80c222bd96..fa26be14950c 100644
--- a/tools/testing/selftests/rseq/mm_cid_transition_test.c
+++ b/tools/testing/selftests/rseq/mm_cid_transition_test.c
@@ -20,21 +20,28 @@
* A second case starts on two CPUs and widens the affinity of a thread
* while the others count.
*
+ * A third case leaves the affinity changes to another process. It narrows
+ * and widens every thread in a loop, while the threads count, call
+ * vfork(2) and then exit with their process.
+ *
* The test fails when counts are lost, or when an mm_cid is not below the
* number of possible CPUs. A kernel that loops looking for a free ID
* shows as a timeout.
*/
#define _GNU_SOURCE
+#include <dirent.h>
#include <errno.h>
#include <limits.h>
#include <pthread.h>
#include <sched.h>
+#include <signal.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
+#include <sys/prctl.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <unistd.h>
@@ -266,6 +273,59 @@ static int run_child_widen(int delay_us)
return check_counts("affinity change");
}
+/*
+ * Narrow and widen the affinity of every thread of @pid in a loop, from
+ * another process. Start after @delay_us and run until @pid exits.
+ */
+static void remote_setter(pid_t pid, int delay_us, const cpu_set_t *narrow)
+{
+ bool widen = true;
+ char path[64];
+
+ /* Die with the process under test, not before its exit. */
+ prctl(PR_SET_PDEATHSIG, SIGKILL);
+ snprintf(path, sizeof(path), "/proc/%d/task", pid);
+ usleep(delay_us);
+ while (getppid() == pid) {
+ DIR *dir = opendir(path);
+ struct dirent *de;
+
+ if (!dir)
+ break;
+ while ((de = readdir(dir))) {
+ pid_t tid = atoi(de->d_name);
+
+ /* A thread can exit first: ESRCH is expected. */
+ if (tid > 0)
+ sched_setaffinity(tid, sizeof(all_cpus),
+ widen ? &all_cpus : narrow);
+ }
+ closedir(dir);
+ widen = !widen;
+ }
+ _exit(0);
+}
+
+/* Run one round while another process changes the affinity of the threads. */
+static int run_child_remote(int delay_us)
+{
+ pid_t self = getpid(), setter;
+ cpu_set_t narrow;
+
+ if (sched_getaffinity(0, sizeof(narrow), &narrow))
+ return CHILD_ERROR;
+ init_workers();
+ setter = fork();
+ if (setter < 0)
+ return CHILD_ERROR;
+ if (!setter)
+ remote_setter(self, delay_us, &narrow);
+ if (run_round(3, -1))
+ return CHILD_ERROR;
+ /* The setter keeps running while this process exits. */
+ return check_counts("affinity change by another process");
+}
+
static int read_nr_possible(void)
{
int first, last, total = 0;
@@ -343,7 +403,7 @@ int main(void)
int status, i;
ksft_print_header();
- ksft_set_plan(3);
+ ksft_set_plan(4);
if (!rseq_available())
ksft_exit_skip("rseq is unavailable\n");
@@ -371,5 +431,12 @@ int main(void)
break;
}
ksft_test_result(status == CHILD_PASS, "affinity change widens the allowed CPUs\n");
+
+ for (i = 0; i < WIDEN_RUNS; i++) {
+ status = run_under_test(run_child_remote, (i * 13) % ROUND_US, &narrow);
+ if (status != CHILD_PASS)
+ break;
+ }
+ ksft_test_result(status == CHILD_PASS, "affinity changed by another process\n");
ksft_finished();
}
--
2.47.3
next prev parent reply other threads:[~2026-11-17 16:17 UTC|newest]
Thread overview: 18+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-17 16:17 [PATCH 00/17] sched/mmcid: fixes for execve, mode switches and affinity Mathieu Desnoyers
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 ` [PATCH 03/17] sched/mmcid: Reset stale state when the first user joins an mm Mathieu Desnoyers
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 ` [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
2026-11-17 16:17 ` [PATCH 07/17] selftests/rseq: Test the concurrency ID after a failed execve Mathieu Desnoyers
2026-11-17 16:17 ` [PATCH 08/17] selftests/rseq: Test the concurrency ID after the last user fails execve Mathieu Desnoyers
2026-11-17 16:17 ` [PATCH 09/17] selftests/rseq: Test the concurrency IDs across ownership mode switches Mathieu Desnoyers
2026-11-17 16:17 ` [PATCH 10/17] selftests/rseq: Test the upper bound of the concurrency IDs Mathieu Desnoyers
2026-11-17 16:17 ` Mathieu Desnoyers [this message]
2026-11-17 16:17 ` [PATCH 12/17] selftests/rseq: Test an affinity change by another process during execve Mathieu Desnoyers
2026-11-17 16:17 ` [PATCH 13/17] selftests/rseq: Test concurrent thread creation and forced migration Mathieu Desnoyers
2026-11-17 16:17 ` [PATCH 14/17] selftests/rseq: Test the concurrency IDs after the affinity is widened Mathieu Desnoyers
2026-11-17 16:17 ` [PATCH 15/17] selftests/rseq: Test two race windows of the concurrency ID mode switches Mathieu Desnoyers
2026-11-17 16:17 ` [PATCH 16/17] selftests/rseq: Test affinity changes while a task replaces or drops its mm Mathieu Desnoyers
2026-11-17 16:17 ` [PATCH 17/17] selftests/rseq: Test the last user after an affinity change during execve 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=926187ef9c1ed8c1b197cb7fa854339c01ee5e6b.1794932121.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=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=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®