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 13/17] selftests/rseq: Test concurrent thread creation and forced migration
Date: Tue, 17 Nov 2026 11:17:13 -0500 [thread overview]
Message-ID: <d4aabd0faed808d6acd1a2a59e88ec98546a17f8.1794932121.git.mathieu.desnoyers@efficios.com> (raw)
In-Reply-To: <cover.1794932121.git.mathieu.desnoyers@efficios.com>
Two fixes that followed the rework of the concurrency IDs point at cases
no test covers:
- commit b2e48c429ec5 ("sched/mmcid: Prevent CID stalls due to
concurrent forks") fixed two tasks that join an mm at the same
time;
- commit 4327fb13fa47 ("sched/mmcid: Prevent live lock on task to CPU
mode transition") fixed a task that migrates while the mm switches
to per CPU mode.
In mm_cid_transition_test only the main thread creates threads, and no
thread is moved to another CPU while the thread count changes.
Add two cases. Both repeat the rounds of 1 to 16 threads on at most
four CPUs:
- In the first one, most threads create a thread of their own when
they start. Several threads then join the mm at the same time.
- In the second one, another process pins every thread to one CPU
after another in a loop. The threads migrate while the mode
switches in both directions.
Both cases pass on guests with 2 to 64 CPUs, with and without the
previous fixes.
Signed-off-by: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
---
.../selftests/rseq/mm_cid_transition_test.c | 173 ++++++++++++------
1 file changed, 121 insertions(+), 52 deletions(-)
diff --git a/tools/testing/selftests/rseq/mm_cid_transition_test.c b/tools/testing/selftests/rseq/mm_cid_transition_test.c
index fa26be14950c..ccedec71fd6d 100644
--- a/tools/testing/selftests/rseq/mm_cid_transition_test.c
+++ b/tools/testing/selftests/rseq/mm_cid_transition_test.c
@@ -24,6 +24,11 @@
* and widens every thread in a loop, while the threads count, call
* vfork(2) and then exit with their process.
*
+ * Two more cases repeat the rounds. In one, most threads create a thread
+ * of their own, so that several threads join the mm at the same time. In
+ * the other, another process moves every thread from CPU to CPU, so that
+ * threads migrate while the mode switches.
+ *
* 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.
@@ -55,6 +60,7 @@
#define WIDEN_RUNS 100
#define ROUND_US 500
#define MAX_TRIES 1000000
+#define MAX_SETS 4
#define CHILD_PASS 0
#define CHILD_FAIL 1
@@ -66,10 +72,16 @@ struct worker {
unsigned int bad_cid;
bool execs;
bool vforks;
+ struct worker *buddy;
};
+/* run_child_rounds() flags */
+#define NESTED 1
+#define MIGRATED 2
+
static intptr_t counts[MAX_CIDS];
-static struct worker workers[MAX_WORKERS];
+static struct worker workers[2 * MAX_WORKERS];
+static bool nested;
static int stop;
static int nr_possible;
static cpu_set_t all_cpus;
@@ -115,14 +127,34 @@ static int remove_file(const char *path)
return 0;
}
+static void *worker_fn(void *arg);
+
+/* Start the thread of @w. Return 0 or an error number. */
+static int create_worker(struct worker *w)
+{
+ int err, tries = 0;
+
+ /* clone(CLONE_FS) fails with EAGAIN while a thread is in execve. */
+ do {
+ err = pthread_create(&w->thread, NULL, worker_fn, w);
+ if (err == EAGAIN)
+ sched_yield();
+ } while (err == EAGAIN && ++tries < MAX_TRIES);
+ return err;
+}
+
static void *worker_fn(void *arg)
{
struct worker *w = arg;
char *const av[] = { exec_path, NULL };
+ bool has_buddy = false;
int i;
if (rseq_register_current_thread())
abort();
+ /* Other workers do the same now: several threads join at once. */
+ if (w->buddy)
+ has_buddy = !create_worker(w->buddy);
while (!RSEQ_READ_ONCE(stop) && w->bad_cid == UINT_MAX) {
for (i = 0; i < 100; i++)
count_one(w);
@@ -141,6 +173,8 @@ static void *worker_fn(void *arg)
}
}
}
+ if (has_buddy)
+ join_thread(w->buddy->thread);
rseq_unregister_current_thread();
return NULL;
}
@@ -152,17 +186,11 @@ static int run_round(int nr, int widen_us)
RSEQ_WRITE_ONCE(stop, 0);
for (i = 0; i < nr; i++) {
- int err, tries = 0;
-
workers[i].execs = i == 0 && exec_path[0];
workers[i].vforks = i == 1;
- /* clone(CLONE_FS) fails with EAGAIN while a thread is in execve. */
- do {
- err = pthread_create(&workers[i].thread, NULL, worker_fn, &workers[i]);
- if (err == EAGAIN)
- sched_yield();
- } while (err == EAGAIN && ++tries < MAX_TRIES);
- if (err)
+ /* The two workers above keep to their own loop. */
+ workers[i].buddy = nested && i > 1 ? &workers[MAX_WORKERS + i] : NULL;
+ if (create_worker(&workers[i]))
break;
started++;
}
@@ -187,7 +215,7 @@ static int check_counts(const char *what)
intptr_t counted = 0, total = 0;
int i, ret = CHILD_PASS;
- for (i = 0; i < MAX_WORKERS; i++) {
+ for (i = 0; i < 2 * MAX_WORKERS; i++) {
counted += workers[i].counted;
if (workers[i].bad_cid != UINT_MAX) {
fprintf(stderr, "%s: mm_cid %u (0x%x), %d possible CPUs\n",
@@ -209,7 +237,7 @@ static void init_workers(void)
{
int i;
- for (i = 0; i < MAX_WORKERS; i++)
+ for (i = 0; i < 2 * MAX_WORKERS; i++)
workers[i].bad_cid = UINT_MAX;
}
@@ -242,12 +270,76 @@ static int make_unknown_format_file(const char *dir)
return -1;
}
-/* Rounds of 1 to MAX_WORKERS threads: the count crosses both thresholds. */
-static int run_child_rounds(int unused)
+/*
+ * Change the affinity of every thread of @pid in a loop, from another
+ * process. Each change takes the next of the @nr_sets CPU sets in @sets.
+ * Start after @delay_us and run until @pid exits.
+ */
+static void remote_setter(pid_t pid, int delay_us, const cpu_set_t *sets, int nr_sets)
{
- int round, ret;
+ unsigned int next = 0;
+ char path[64];
+
+ /* Die with the process under test, not before its exit. */
+ if (prctl(PR_SET_PDEATHSIG, SIGKILL)) {
+ fprintf(stderr, "prctl(PR_SET_PDEATHSIG): %s\n", strerror(errno));
+ _exit(1);
+ }
+ 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(sets[0]),
+ &sets[next++ % nr_sets]) &&
+ errno != ESRCH) {
+ fprintf(stderr, "sched_setaffinity: %s\n", strerror(errno));
+ _exit(1);
+ }
+ }
+ closedir(dir);
+ }
+ _exit(0);
+}
+
+/*
+ * Rounds of 1 to MAX_WORKERS threads: the count crosses both thresholds.
+ * With NESTED in @flags, most threads create one more thread. With
+ * MIGRATED, another process moves the threads between the allowed CPUs.
+ */
+static int run_child_rounds(int flags)
+{
+ static cpu_set_t sets[MAX_SETS];
+ int round, ret, cpu, nr_sets = 0;
+ pid_t self = getpid(), setter;
+ cpu_set_t allowed;
init_workers();
+ nested = flags & NESTED;
+ if (flags & MIGRATED) {
+ if (sched_getaffinity(0, sizeof(allowed), &allowed))
+ return CHILD_ERROR;
+ /* One set per allowed CPU: the allowed CPUs of the mm stay. */
+ for (cpu = 0; cpu < CPU_SETSIZE && nr_sets < MAX_SETS; cpu++) {
+ if (!CPU_ISSET(cpu, &allowed))
+ continue;
+ CPU_ZERO(&sets[nr_sets]);
+ CPU_SET(cpu, &sets[nr_sets++]);
+ }
+ setter = fork();
+ if (setter < 0)
+ return CHILD_ERROR;
+ if (!setter)
+ remote_setter(self, 0, sets, nr_sets);
+ }
/* Without the file the rounds run with no execve. */
if (make_unknown_format_file("/tmp") && make_unknown_format_file("."))
fprintf(stderr, "no unknown format file: the rounds run without execve\n");
@@ -273,53 +365,23 @@ 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;
+ cpu_set_t sets[3];
- if (sched_getaffinity(0, sizeof(narrow), &narrow))
+ /* Two wide changes for a narrow one, so that each thread gets both. */
+ if (sched_getaffinity(0, sizeof(sets[0]), &sets[0]))
return CHILD_ERROR;
+ sets[1] = all_cpus;
+ sets[2] = all_cpus;
init_workers();
setter = fork();
if (setter < 0)
return CHILD_ERROR;
if (!setter)
- remote_setter(self, delay_us, &narrow);
+ remote_setter(self, delay_us, sets, 3);
if (run_round(3, -1))
return CHILD_ERROR;
/* The setter keeps running while this process exits. */
@@ -403,7 +465,7 @@ int main(void)
int status, i;
ksft_print_header();
- ksft_set_plan(4);
+ ksft_set_plan(6);
if (!rseq_available())
ksft_exit_skip("rseq is unavailable\n");
@@ -438,5 +500,12 @@ int main(void)
break;
}
ksft_test_result(status == CHILD_PASS, "affinity changed by another process\n");
+
+ narrow_cpus(&narrow, &all_cpus, MAX_SETS);
+ status = run_under_test(run_child_rounds, NESTED, &narrow);
+ ksft_test_result(status == CHILD_PASS, "threads created by several threads at once\n");
+
+ status = run_under_test(run_child_rounds, MIGRATED, &narrow);
+ ksft_test_result(status == CHILD_PASS, "threads migrated 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 ` [PATCH 11/17] selftests/rseq: Test affinity changes made by another process Mathieu Desnoyers
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 ` Mathieu Desnoyers [this message]
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=d4aabd0faed808d6acd1a2a59e88ec98546a17f8.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®