[PATCH v2 05/13] selftests/rseq: Test the concurrency ID after a failed execve

From: Mathieu Desnoyers

Date: Tue Dec 01 2026 - 19:28:25 EST


A failed execve() returns to user space in the same mm. The mm_cid that
the task reads there must be a valid ID. No test checks this.

In per CPU mode, the CPUs own the CIDs of the mm, not the tasks. A task
that joins the CID management of the mm then gets no CID. It takes the
CID of its CPU when it is scheduled in. On tip sched/urgent, a failed
execve() makes the task leave the CID management of its mm and join it
again. The task then returns to user space before it is scheduled in
again, and reads an mm_cid of 0x80000000.

Add mm_cid_execve_test. Its main thread calls execve() on a file that no
binfmt handler recognises, while four other threads run. The call fails
with ENOEXEC before de_thread() kills the other threads. The main thread
reads its mm_cid after each call. The value must stay below the number
of possible CPUs, the only bound. The kernel also aims at a value below
the number of allowed CPUs. That is a target: the test prints a note
when it misses it, and does not fail.

Per CPU mode needs more threads than the mm has CIDs. The allowed CPUs
of an mm only grow. So the test narrows its affinity to two CPUs before
it forks the process under test. Five threads are then enough on any
machine.

On tip sched/urgent, the test fails on guests with 2 to 64 CPUs:

mm_cid 2147483648 (0x80000000) after a failed execve, round 0 of 100
not ok 1 mm_cid after a failed execve

It passes after "sched/mmcid: Move the execve() hooks into exec_mmap()".

Signed-off-by: Mathieu Desnoyers <mathieu.desnoyers@xxxxxxxxxxxx>
---
tools/testing/selftests/rseq/.gitignore | 1 +
tools/testing/selftests/rseq/Makefile | 3 +-
.../selftests/rseq/mm_cid_execve_test.c | 319 ++++++++++++++++++
3 files changed, 322 insertions(+), 1 deletion(-)
create mode 100644 tools/testing/selftests/rseq/mm_cid_execve_test.c

diff --git a/tools/testing/selftests/rseq/.gitignore b/tools/testing/selftests/rseq/.gitignore
index 9b6eb6c3922f..1d94aaf15f4e 100644
--- a/tools/testing/selftests/rseq/.gitignore
+++ b/tools/testing/selftests/rseq/.gitignore
@@ -9,6 +9,7 @@ param_test_compare_twice
param_test_mm_cid
param_test_mm_cid_benchmark
param_test_mm_cid_compare_twice
+mm_cid_execve_test
syscall_errors_test
slice_test
legacy_check
diff --git a/tools/testing/selftests/rseq/Makefile b/tools/testing/selftests/rseq/Makefile
index aba6317f6cb8..ee40f80c079c 100644
--- a/tools/testing/selftests/rseq/Makefile
+++ b/tools/testing/selftests/rseq/Makefile
@@ -19,7 +19,8 @@ LDLIBS += -lpthread -ldl
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
+ param_test_benchmark param_test_mm_cid_benchmark \
+ mm_cid_execve_test

TEST_GEN_PROGS_EXTENDED = librseq.so \
param_test \
diff --git a/tools/testing/selftests/rseq/mm_cid_execve_test.c b/tools/testing/selftests/rseq/mm_cid_execve_test.c
new file mode 100644
index 000000000000..1a27494297bc
--- /dev/null
+++ b/tools/testing/selftests/rseq/mm_cid_execve_test.c
@@ -0,0 +1,319 @@
+// SPDX-License-Identifier: LGPL-2.1
+/*
+ * Check the concurrency ID that a thread reads after a failed execve(2).
+ *
+ * A failed execve(2) returns to user space in the same mm. The other
+ * threads still use CIDs of that mm. The mm_cid that the task reads must
+ * be a valid ID: below the number of possible CPUs. The kernel also aims
+ * at an ID below the number of allowed CPUs. That is a target: the test
+ * prints a note when it is missed.
+ *
+ * Before "sched/mmcid: Move the execve() hooks into exec_mmap()", the task
+ * left the CID management of the mm in execve(2) and joined it again on
+ * failure. In per CPU mode, it then returned to user space with no CID, and
+ * read 0x80000000.
+ *
+ * Per CPU mode needs more threads than the mm has CIDs. The number of CIDs
+ * follows the union of the allowed CPUs of the threads, which only grows.
+ * The affinity is therefore narrowed before the fork(2) that creates the
+ * process under test. A few threads are then enough on any machine.
+ */
+
+#define _GNU_SOURCE
+#include <errno.h>
+#include <limits.h>
+#include <pthread.h>
+#include <sched.h>
+#include <stdbool.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 NARROW_CPUS 2
+#define THREADS 4
+#define ROUNDS 100
+
+/* Exit status of the process under test. */
+#define CHILD_PASS 0
+#define CHILD_FAIL 1
+#define CHILD_SKIP 2
+#define CHILD_ERROR 3
+
+static int stop;
+static int nr_possible;
+
+/*
+ * Stay alive as a user of the CIDs of the mm until stop is set. A thread
+ * needs no rseq area for that.
+ */
+static void *waiter(void *arg)
+{
+ while (!RSEQ_READ_ONCE(stop))
+ (void)sched_yield();
+ return NULL;
+}
+
+/* Join @t. Return 0, or the error number after reporting it. */
+static int join_thread(pthread_t t)
+{
+ int err = pthread_join(t, NULL);
+
+ if (err)
+ fprintf(stderr, "pthread_join: %s\n", strerror(err));
+ return err;
+}
+
+/* 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;
+}
+
+/*
+ * Return the directory of the test's executable, stored in @buf. Return "."
+ * if it cannot be read.
+ */
+static const char *test_dir(char *buf, size_t len)
+{
+ ssize_t n = readlink("/proc/self/exe", buf, len - 1);
+ char *slash;
+
+ if (n <= 0)
+ return ".";
+ buf[n] = '\0';
+ slash = strrchr(buf, '/');
+ if (!slash)
+ return ".";
+ *slash = '\0';
+ return slash == buf ? "/" : buf;
+}
+
+/*
+ * Create a file that no binfmt handler recognises, and leave its path in
+ * @path. execve(2) on it fails with ENOEXEC in bprm_execve(), before
+ * begin_new_exec() kills the other threads. A path that does not exist
+ * would fail before bprm_execve(). Before "sched/mmcid: Move the execve()
+ * hooks into exec_mmap()", bprm_execve() called the CID hooks.
+ *
+ * The directory must allow execution, so try the directory of the test
+ * first, then /tmp. Run execve(2) once to check for ENOEXEC. Return 0 on
+ * success, or -1.
+ */
+static int make_unknown_format_file(char *path, size_t len)
+{
+ char self[PATH_MAX];
+ const char *const dirs[] = { test_dir(self, sizeof(self)), "/tmp" };
+ static const char junk[] = "not a binary of any known format\n";
+ unsigned int i;
+
+ for (i = 0; i < ARRAY_SIZE(dirs); i++) {
+ char *const av[] = { path, NULL };
+ bool written;
+ int fd;
+
+ snprintf(path, len, "%s/mm_cid_execve_test.XXXXXX", dirs[i]);
+ fd = mkstemp(path);
+ if (fd < 0)
+ continue;
+ written = write(fd, junk, sizeof(junk) - 1) == (ssize_t)sizeof(junk) - 1 &&
+ !fchmod(fd, 0755);
+ /* An open file would make execve(2) fail with ETXTBSY. */
+ if (close(fd) || !written) {
+ remove_file(path);
+ continue;
+ }
+ if (execve(path, av, NULL) == -1 && errno == ENOEXEC)
+ return 0;
+ remove_file(path);
+ }
+ return -1;
+}
+
+/*
+ * The process under test. Start THREADS threads on a narrow set of CPUs.
+ * Then fail execve(2) in a loop and read the mm_cid after each call.
+ * Return a CHILD_* status.
+ */
+static int run_child(void)
+{
+ pthread_t thread[THREADS];
+ char path[PATH_MAX];
+ unsigned int nr_allowed, worst = 0;
+ cpu_set_t mask;
+ int i, err = 0, started = 0, ret = CHILD_PASS;
+
+ /* The kernel aims at IDs below the allowed CPUs: a target. */
+ if (sched_getaffinity(0, sizeof(mask), &mask)) {
+ fprintf(stderr, "sched_getaffinity: %s\n", strerror(errno));
+ return CHILD_ERROR;
+ }
+ nr_allowed = CPU_COUNT(&mask);
+
+ if (make_unknown_format_file(path, sizeof(path))) {
+ fprintf(stderr, "no directory to run an unknown format file from: %s\n",
+ strerror(errno));
+ return CHILD_SKIP;
+ }
+
+ for (i = 0; i < THREADS; i++) {
+ err = pthread_create(&thread[i], NULL, waiter, NULL);
+ if (err)
+ break;
+ started++;
+ }
+ if (started < THREADS) {
+ fprintf(stderr, "pthread_create: %s\n", strerror(err));
+ ret = CHILD_ERROR;
+ goto out;
+ }
+
+ for (i = 0; i < ROUNDS; i++) {
+ char *const av[] = { path, NULL };
+ unsigned int cid;
+
+ if (!execve(path, av, NULL)) {
+ fprintf(stderr, "execve(%s) succeeded, expected ENOEXEC\n", path);
+ ret = CHILD_ERROR;
+ goto out;
+ }
+ if (errno != ENOEXEC) {
+ fprintf(stderr, "execve(%s): %s, expected ENOEXEC\n",
+ path, strerror(errno));
+ ret = CHILD_ERROR;
+ goto out;
+ }
+ cid = rseq_current_mm_cid();
+ if (cid > worst)
+ worst = cid;
+ /* The number of possible CPUs is the only bound. */
+ if (cid >= (unsigned int)nr_possible) {
+ fprintf(stderr, "mm_cid %u (0x%x) after a failed execve, round %d of %d\n",
+ cid, cid, i, ROUNDS);
+ ret = CHILD_FAIL;
+ goto out;
+ }
+ }
+ fprintf(stderr, "%d threads, %d failed execve, largest mm_cid %u, %d possible CPUs%s\n",
+ started + 1, ROUNDS, worst, nr_possible,
+ worst >= nr_allowed ? ", reaches the number of allowed CPUs" : "");
+out:
+ RSEQ_WRITE_ONCE(stop, 1);
+ for (i = 0; i < started; i++) {
+ if (join_thread(thread[i]) && ret == CHILD_PASS)
+ ret = CHILD_ERROR;
+ }
+ if (remove_file(path) && ret == CHILD_PASS)
+ ret = CHILD_ERROR;
+ return ret;
+}
+
+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;
+}
+
+int main(void)
+{
+ cpu_set_t allowed, narrow;
+ int status = 0;
+ pid_t child;
+
+ ksft_print_header();
+ ksft_set_plan(1);
+
+ if (!rseq_available())
+ ksft_exit_skip("rseq is unavailable\n");
+ /* glibc before 2.35 registers no rseq area. */
+ 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(allowed), &allowed))
+ 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");
+ if (!narrow_cpus(&narrow, &allowed, NARROW_CPUS))
+ ksft_exit_skip("no CPU to run on\n");
+
+ /*
+ * Narrow before the fork(2). The mm of the child starts with these
+ * allowed CPUs, and a later narrowing does not shrink them.
+ */
+ if (sched_setaffinity(0, sizeof(narrow), &narrow))
+ ksft_exit_skip("sched_setaffinity: %s\n", strerror(errno));
+ child = fork();
+ if (!child)
+ _exit(run_child());
+ if (child < 0)
+ ksft_exit_fail_msg("fork: %s\n", strerror(errno));
+ if (sched_setaffinity(0, sizeof(allowed), &allowed))
+ 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));
+
+ switch (WEXITSTATUS(status)) {
+ case CHILD_PASS:
+ ksft_test_result_pass("mm_cid after a failed execve\n");
+ break;
+ case CHILD_SKIP:
+ ksft_test_result_skip("mm_cid after a failed execve\n");
+ break;
+ case CHILD_FAIL:
+ ksft_test_result_fail("mm_cid after a failed execve\n");
+ break;
+ default:
+ ksft_exit_fail_msg("the process under test failed to run\n");
+ }
+ ksft_finished();
+}
--
2.47.3