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
Subject: [PATCH v2 06/13] selftests/rseq: Test the concurrency IDs across ownership mode switches
Date: Tue, 1 Dec 2026 19:27:54 -0500 [thread overview]
Message-ID: <78fe7589108626d422ef63f519997a3c6b06facb.1796169827.git.mathieu.desnoyers@efficios.com> (raw)
In-Reply-To: <cover.1796169827.git.mathieu.desnoyers@efficios.com>
Either the tasks or the CPUs own the concurrency IDs (CIDs) of an mm.
This is its ownership mode. The kernel switches to per CPU mode when
the mm has more users than CIDs. It switches back to per task mode when
the user count drops below a lower threshold. An affinity change that
widens the allowed CPUs of the mm raises that threshold.
No test switches the mode back and forth while threads use their CIDs.
Add mm_cid_transition_test. Its worker threads count in an array
indexed by their mm_cid, with no lock. Two workers that run with the
same mm_cid at the same time can lose counts. One worker also fails
execve() in a loop, and another calls vfork() in a loop. The test has
three cases:
- Case 1 creates and ends 1 to 16 workers per round, on at most four
CPUs. 16 workers exceed the CIDs on any machine.
- Case 2 does the same on all CPUs.
- Case 3 starts three workers on two CPUs, with no execve() worker. The
main thread then widens its own affinity to all CPUs. This can switch
the mm back to per task mode while the workers count.
A case fails when counts are lost, or when an mm_cid is not below the
number of possible CPUs.
On tip sched/urgent, a thread reads an mm_cid of 0x80000000. Case 1
fails on guests with 2 to 64 CPUs:
thread count changes: mm_cid 2147483648 (0x80000000), 8 possible CPUs
not ok 1 thread count changes on at most 4 CPUs
Case 2 fails on the same guests, except those with 18 CPUs or more
(here 48 and 64).
There, the mm has at least as many CIDs as users, and stays in per task
mode.
Both pass after "sched/mmcid: Move the execve() hooks into
exec_mmap()". Case 3 passes on tip sched/urgent, and with each of
patches 1 to 4 added in turn.
Signed-off-by: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
---
tools/testing/selftests/rseq/.gitignore | 1 +
tools/testing/selftests/rseq/Makefile | 2 +-
.../selftests/rseq/mm_cid_transition_test.c | 388 ++++++++++++++++++
3 files changed, 390 insertions(+), 1 deletion(-)
create mode 100644 tools/testing/selftests/rseq/mm_cid_transition_test.c
diff --git a/tools/testing/selftests/rseq/.gitignore b/tools/testing/selftests/rseq/.gitignore
index 1d94aaf15f4e..e8afb37121d7 100644
--- a/tools/testing/selftests/rseq/.gitignore
+++ b/tools/testing/selftests/rseq/.gitignore
@@ -10,6 +10,7 @@ param_test_mm_cid
param_test_mm_cid_benchmark
param_test_mm_cid_compare_twice
mm_cid_execve_test
+mm_cid_transition_test
syscall_errors_test
slice_test
legacy_check
diff --git a/tools/testing/selftests/rseq/Makefile b/tools/testing/selftests/rseq/Makefile
index ee40f80c079c..ac32d6af3b99 100644
--- a/tools/testing/selftests/rseq/Makefile
+++ b/tools/testing/selftests/rseq/Makefile
@@ -20,7 +20,7 @@ OVERRIDE_TARGETS = 1
TEST_GEN_PROGS = basic_test basic_percpu_ops_test basic_percpu_ops_mm_cid_test \
param_test_benchmark param_test_mm_cid_benchmark \
- mm_cid_execve_test
+ mm_cid_execve_test mm_cid_transition_test
TEST_GEN_PROGS_EXTENDED = librseq.so \
param_test \
diff --git a/tools/testing/selftests/rseq/mm_cid_transition_test.c b/tools/testing/selftests/rseq/mm_cid_transition_test.c
new file mode 100644
index 000000000000..eef1a0922ca4
--- /dev/null
+++ b/tools/testing/selftests/rseq/mm_cid_transition_test.c
@@ -0,0 +1,388 @@
+// SPDX-License-Identifier: LGPL-2.1
+/*
+ * Check the concurrency IDs while the kernel switches their ownership
+ * mode.
+ *
+ * The tasks own the IDs of an mm while there are few threads. The CPUs
+ * own them once there are more threads than IDs. The kernel switches mode
+ * when the thread count crosses a threshold. An affinity change that
+ * widens the allowed CPUs can also switch it back to per task mode.
+ *
+ * The main thread starts worker threads. It stops them after a while and
+ * waits for them to exit. Each worker does this:
+ * - It counts in an array indexed by its mm_cid, in an rseq critical
+ * section with no lock. Two workers that run with the same mm_cid at
+ * the same time can lose counts.
+ * - Except in case 3, worker 0 also fails execve(2) in a loop. Before
+ * "sched/mmcid: Move the execve() hooks into exec_mmap()", each failure
+ * left the CID management of the mm and joined it again.
+ * - Worker 1 also calls vfork(2) in a loop. The child is a user of the
+ * CIDs of the mm until it exits.
+ *
+ * Cases 1 and 2 run rounds of 1 to 16 workers, on at most four CPUs and on
+ * all CPUs. Case 3 starts three workers on two CPUs. The main thread then
+ * widens its own affinity while the workers count.
+ *
+ * The test fails when counts are lost, or when an mm_cid is not below the
+ * number of possible CPUs. A kernel that loops in mm_get_cid() shows as a
+ * timeout.
+ */
+
+#define _GNU_SOURCE
+#include <errno.h>
+#include <limits.h>
+#include <pthread.h>
+#include <sched.h>
+#include <stdbool.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/stat.h>
+#include <sys/wait.h>
+#include <unistd.h>
+
+#include "rseq.h"
+#include "kselftest.h"
+
+#define MAX_CIDS CPU_SETSIZE
+#define MAX_WORKERS 16
+#define ROUNDS 200
+#define WIDEN_RUNS 100
+#define ROUND_US 500
+#define MAX_TRIES 1000000
+
+#define CHILD_PASS 0
+#define CHILD_FAIL 1
+#define CHILD_ERROR 2
+
+struct worker {
+ pthread_t thread;
+ intptr_t counted;
+ unsigned int bad_cid;
+ bool execs;
+ bool vforks;
+};
+
+static intptr_t counts[MAX_CIDS];
+static struct worker workers[MAX_WORKERS];
+static int stop;
+static int nr_possible;
+static cpu_set_t all_cpus;
+static char exec_path[PATH_MAX];
+
+/*
+ * Add one to the count of the current mm_cid. Record an mm_cid not below
+ * the possible CPUs in @w instead.
+ */
+static void count_one(struct worker *w)
+{
+ for (;;) {
+ unsigned int cid = rseq_current_mm_cid();
+
+ if (cid >= (unsigned int)nr_possible || cid >= MAX_CIDS) {
+ w->bad_cid = cid;
+ return;
+ }
+ if (!rseq_addv(RSEQ_MO_RELAXED, RSEQ_PERCPU_MM_CID, &counts[cid], 1, cid)) {
+ w->counted++;
+ return;
+ }
+ }
+}
+
+static int join_failed;
+
+/* Join @t. Report an error and set join_failed if that fails. */
+static void join_thread(pthread_t t)
+{
+ int err = pthread_join(t, NULL);
+
+ if (err) {
+ fprintf(stderr, "pthread_join: %s\n", strerror(err));
+ RSEQ_WRITE_ONCE(join_failed, 1);
+ }
+}
+
+/* Remove @path. Return 0, or -1 after reporting the error. */
+static int remove_file(const char *path)
+{
+ if (unlink(path)) {
+ fprintf(stderr, "unlink %s: %s\n", path, strerror(errno));
+ return -1;
+ }
+ return 0;
+}
+
+static void *worker_fn(void *arg)
+{
+ struct worker *w = arg;
+ char *const av[] = { exec_path, NULL };
+ int i;
+
+ if (rseq_register_current_thread())
+ abort();
+ while (!RSEQ_READ_ONCE(stop) && w->bad_cid == UINT_MAX) {
+ for (i = 0; i < 100; i++)
+ count_one(w);
+ if (w->execs && execve(exec_path, av, NULL) == -1 && errno != ENOEXEC) {
+ fprintf(stderr, "execve: %s, expected ENOEXEC\n", strerror(errno));
+ abort();
+ }
+ if (w->vforks) {
+ pid_t pid = vfork();
+
+ if (!pid)
+ _exit(0);
+ if (pid < 0 || waitpid(pid, NULL, 0) != pid) {
+ fprintf(stderr, "vfork: %s\n", strerror(errno));
+ abort();
+ }
+ }
+ }
+ rseq_unregister_current_thread();
+ return NULL;
+}
+
+/*
+ * Run @nr workers for a while. Unless @widen_us is negative, widen the
+ * affinity of this thread to all CPUs @widen_us after the start. Return 0,
+ * or -1 on an error.
+ */
+static int run_round(int nr, int widen_us)
+{
+ int i, started = 0, widened = 1;
+
+ 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)
+ break;
+ started++;
+ }
+ if (widen_us >= 0) {
+ usleep(widen_us);
+ /* The union of allowed CPUs of the mm grows. */
+ if (sched_setaffinity(0, sizeof(all_cpus), &all_cpus)) {
+ fprintf(stderr, "sched_setaffinity: %s\n", strerror(errno));
+ widened = 0;
+ }
+ }
+ usleep(ROUND_US);
+ RSEQ_WRITE_ONCE(stop, 1);
+ for (i = 0; i < started; i++)
+ join_thread(workers[i].thread);
+ return started == nr && widened && !RSEQ_READ_ONCE(join_failed) ? 0 : -1;
+}
+
+/*
+ * Compare the sum of the array with the counts of the workers, and report
+ * any mm_cid not below the possible CPUs. Return a CHILD_* status.
+ */
+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++) {
+ counted += workers[i].counted;
+ if (workers[i].bad_cid != UINT_MAX) {
+ fprintf(stderr, "%s: mm_cid %u (0x%x), %d possible CPUs\n",
+ what, workers[i].bad_cid, workers[i].bad_cid, nr_possible);
+ ret = CHILD_FAIL;
+ }
+ }
+ for (i = 0; i < MAX_CIDS; i++)
+ total += counts[i];
+ if (total != counted) {
+ fprintf(stderr, "%s: %ld counts lost out of %ld: two threads ran with one mm_cid\n",
+ what, (long)(counted - total), (long)counted);
+ ret = CHILD_FAIL;
+ }
+ return ret;
+}
+
+static void init_workers(void)
+{
+ int i;
+
+ for (i = 0; i < MAX_WORKERS; i++)
+ workers[i].bad_cid = UINT_MAX;
+}
+
+/*
+ * Create a file in @dir that no binfmt handler recognises, and leave its
+ * path in exec_path. execve(2) on it fails with ENOEXEC in bprm_execve(),
+ * before the other threads are killed. Return 0, or -1.
+ */
+static int make_unknown_format_file(const char *dir)
+{
+ static const char junk[] = "not a binary of any known format\n";
+ char *const av[] = { exec_path, NULL };
+ bool written;
+ int fd;
+
+ snprintf(exec_path, sizeof(exec_path), "%s/mm_cid_transition_test.XXXXXX", dir);
+ fd = mkstemp(exec_path);
+ if (fd < 0)
+ goto fail;
+ written = write(fd, junk, sizeof(junk) - 1) == (ssize_t)sizeof(junk) - 1 &&
+ !fchmod(fd, 0755);
+ if (close(fd) || !written)
+ goto unlink;
+ if (execve(exec_path, av, NULL) == -1 && errno == ENOEXEC)
+ return 0;
+unlink:
+ remove_file(exec_path);
+fail:
+ exec_path[0] = '\0';
+ return -1;
+}
+
+/* Rounds of 1 to MAX_WORKERS threads: the count crosses both thresholds. */
+static int run_child_rounds(int unused)
+{
+ int round, ret;
+
+ init_workers();
+ /* 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");
+ for (round = 0; round < ROUNDS; round++) {
+ if (run_round(1 + (round * 7) % MAX_WORKERS, -1)) {
+ ret = CHILD_ERROR;
+ goto out;
+ }
+ }
+ ret = check_counts("thread count changes");
+out:
+ if (exec_path[0] && remove_file(exec_path) && ret == CHILD_PASS)
+ ret = CHILD_ERROR;
+ return ret;
+}
+
+/* Run one round that widens the allowed CPUs @delay_us after its start. */
+static int run_child_widen(int delay_us)
+{
+ init_workers();
+ if (run_round(3, delay_us))
+ return CHILD_ERROR;
+ return check_counts("affinity change");
+}
+
+static int read_nr_possible(void)
+{
+ int first, last, total = 0;
+ char sep;
+ FILE *f;
+
+ f = fopen("/sys/devices/system/cpu/possible", "r");
+ if (!f)
+ return -1;
+ /* A list of ranges: "0-63" or "0,2-5". */
+ while (fscanf(f, "%d", &first) == 1) {
+ last = first;
+ sep = '\n';
+ if (fscanf(f, "%c", &sep) == 1 && sep == '-') {
+ if (fscanf(f, "%d", &last) != 1)
+ break;
+ if (fscanf(f, "%c", &sep) != 1)
+ sep = '\n';
+ }
+ total += last - first + 1;
+ if (sep != ',')
+ break;
+ }
+ fclose(f);
+ return total;
+}
+
+/* Keep the first @nr CPUs of @src in @dst. */
+static int narrow_cpus(cpu_set_t *dst, const cpu_set_t *src, int nr)
+{
+ int cpu, taken = 0;
+
+ CPU_ZERO(dst);
+ for (cpu = 0; cpu < CPU_SETSIZE && taken < nr; cpu++) {
+ if (!CPU_ISSET(cpu, src))
+ continue;
+ CPU_SET(cpu, dst);
+ taken++;
+ }
+ return taken;
+}
+
+/*
+ * Run @fn(@arg) in a new process and return its exit status. The affinity
+ * is @narrow when its mm is created. The union of allowed CPUs of an mm
+ * starts there and only grows.
+ */
+static int run_under_test(int (*fn)(int), int arg, const cpu_set_t *narrow)
+{
+ int status = 0;
+ pid_t child;
+
+ if (sched_setaffinity(0, sizeof(*narrow), narrow))
+ ksft_exit_skip("sched_setaffinity: %s\n", strerror(errno));
+ child = fork();
+ if (!child)
+ _exit(fn(arg));
+ if (child < 0)
+ ksft_exit_fail_msg("fork: %s\n", strerror(errno));
+ if (sched_setaffinity(0, sizeof(all_cpus), &all_cpus))
+ ksft_exit_fail_msg("sched_setaffinity: %s\n", strerror(errno));
+ if (waitpid(child, &status, 0) != child)
+ ksft_exit_fail_msg("waitpid: %s\n", strerror(errno));
+ if (!WIFEXITED(status))
+ ksft_exit_fail_msg("the process under test was killed by signal %d\n",
+ WTERMSIG(status));
+ if (WEXITSTATUS(status) > CHILD_FAIL)
+ ksft_exit_fail_msg("the process under test failed to run\n");
+ return WEXITSTATUS(status);
+}
+
+int main(void)
+{
+ cpu_set_t narrow;
+ int status, i;
+
+ ksft_print_header();
+ ksft_set_plan(3);
+
+ if (!rseq_available())
+ ksft_exit_skip("rseq is unavailable\n");
+ if (rseq_register_current_thread())
+ ksft_exit_fail_msg("rseq_register_current_thread: %s\n", strerror(errno));
+ if (!rseq_mm_cid_available())
+ ksft_exit_skip("the rseq mm_cid field is unavailable\n");
+ if (sched_getaffinity(0, sizeof(all_cpus), &all_cpus))
+ ksft_exit_fail_msg("sched_getaffinity: %s\n", strerror(errno));
+ nr_possible = read_nr_possible();
+ if (nr_possible <= 0)
+ ksft_exit_skip("cannot read the possible CPUs\n");
+
+ narrow_cpus(&narrow, &all_cpus, 4);
+ status = run_under_test(run_child_rounds, 0, &narrow);
+ ksft_test_result(status == CHILD_PASS, "thread count changes on at most 4 CPUs\n");
+
+ status = run_under_test(run_child_rounds, 0, &all_cpus);
+ ksft_test_result(status == CHILD_PASS, "thread count changes on all CPUs\n");
+
+ narrow_cpus(&narrow, &all_cpus, 2);
+ for (i = 0; i < WIDEN_RUNS; i++) {
+ status = run_under_test(run_child_widen, (i * 13) % ROUND_US, &narrow);
+ if (status != CHILD_PASS)
+ break;
+ }
+ ksft_test_result(status == CHILD_PASS, "affinity change widens the allowed CPUs\n");
+ ksft_finished();
+}
--
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 ` [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 ` Mathieu Desnoyers [this message]
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=78fe7589108626d422ef63f519997a3c6b06facb.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=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®