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=1796171289; b=fOS0HZ/4QZAy50cXcIf1soyaecOqlC46o430zs25H3UVnRck/R55M3rkn7209STgTkQ1 mOeRZOPxL9Qv+J5hew7tHSapOi8GzeXNd9nrjS5r5VuS7o8m6TcG10ismNVlVGRlvmZcI Ea65D7yQcxxKw+LLOkyHUSst+oYUzNDJl0wJeFgThTTJMwBlexIo/LzVko1efIzCpYeRw pmA5dFGJlg/mIEDUmmx5tkJz+x3nBY8Q+roP2XwUsJG1HmmtvMHC6ZOA2dr5AU/xwXwLy MevpI5gGIp/guQ4UCcsM45DV9Aa3JEh4iKxESpfYI9jX8HMxkln4iXFXQODxf9eBBVQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796171289; h=DMARC-Filter:DKIM-Signature:Received:From:To:Cc:Subject:Date: Message-ID:X-Mailer:In-Reply-To:References:MIME-Version: Content-Transfer-Encoding; bh=TJtAhvjI307Ik0ovnbXU0pAOM3PystaZujYkgxk9l+Y=; b=ggpNqh8zBBNwR9uGGsYV3MBWShxaFkeCV1GDzGXP7mAO8tyWoyusjR6XFR0je2MjZZgX pNyufpYXaP01W9OIGVwjBy0yKBn9wpO2uMEC1zlhSXLhKRoatVfkwKizpLSLlJOW6lshC WPrgF2TIXATpPMrUQFWpwiteah2NuhSWH6vYk/VZg6pIhYckCojtsrso8eN5EeSNHHCVU DtcZY6isHyc2yr8/euDcEqUHYA/L4OLjgmXp/sNi4pD5Eo9/X51V6VgH6iwqocK0IYbis TYHEhQA1R7lFq0wXZhmY7DbYMRZanQ/zdibvkGuK28xeQ+Qlby3dM2dJXmVxmzM+5VA== 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=cGKYU12R; 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)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 823E4606A697; Wed, 2 Dec 2026 00:28:08 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 823E4606A697 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=cGKYU12R DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=efficios.com; s=smtpout1; t=1796171287; bh=TJtAhvjI307Ik0ovnbXU0pAOM3PystaZujYkgxk9l+Y=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=cGKYU12Rh1r6jLq1rtd0bDRkXRFIIcIjHmfTotKxVrbLW9rosG9C+dH+2YzV2iign 3DjVdRoCGifbtHzk9CoRiq0sLKr38nz0YurFBZBVkQPa+Z88vx1ff5YvQjsxwXEs2d UYwHtdTl1k6vtwEC5S+MM4LzsAw+fuyvDGxmV4S/PeSdHIvaeH7ommC8cFsKbSXLw6 xUiK6laSd6o/i2x5KQvbxHCMDMTjBzyQv+wbdwUGXshD7NsQbXVVi1QuX7o/CQk4E3 /cc1Mk9d42Hnu8JOTWFqpJfPPwv9Xwb4IwABtXiKZp2yer//5feI3IiLCku2vs+9Rk 3QGzCA/AmJNxA== Received: from thinkos.internal.efficios.com (unknown [IPv6:2606:6d00:100:4000:cacb:9855:de1f:ded2]) by smtpout.efficios.com (Postfix) with ESMTPSA id 4jfJWW3r1bzYyn; Tue, 01 Dec 2026 19:28:07 -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 06/13] selftests/rseq: Test the concurrency IDs across ownership mode switches Date: Tue, 1 Dec 2026 19:27:54 -0500 Message-ID: <78fe7589108626d422ef63f519997a3c6b06facb.1796169827.git.mathieu.desnoyers@efficios.com> X-Mailer: git-send-email 2.47.3 In-Reply-To: References: X-Mailing-List: linux-kernel@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 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 --- 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 +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#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