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=1796171290; b=1+d2rp7YNxVKrpj9P3V3DHcuUHFkXxpknraC4YBg9Tz3lsiKjenK+zv1de5CVMe+pCKo hzdJHLTnjAVLbY+ZnKs5oCrzh2nx7zEwC9wUn1CSeBK/c/u85XZd1b8qGXkJEDYr5uDnQ qcXgBpl6mLnqn8H1XT9iV+qqr2jRPpwnbrOIBrZw0W4lZpjQD4kpFbYZ+dy/0iGrUBEVw RklE2p0vuhdmLUMCDp0ulVwxlKw4F5PRv5aBMY6j07xZXAbr76dgok8htZBdvgySBAY8h QfGjHcxyMIFBfJMGQTEwa89BiPZ5gwUvwkmzQoZCzTObDg1TX7A93PpVlsWiYt1y9nA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796171290; 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=Mk1ZZrXaCC2k2joW5tf3sBHhs4Qyji9ZL63iP3eUQ+M=; b=yFeoTweBjJZGcNkpD1Kc8EaL1/j7b4D3dA+eXMt9UcdfGIfttyRxxEiqoC/rm6jK4Ypr jLSSLsupwx/RXwI/rrK0/oHcvc7b+LjxZV5LWmhJhVoyo3fuP8KuBF5mg2+/3aYFWQFBI CKu3c7lBQcXnR2nD/hC2NW5KCSVHdyHUp64RWUVt97TYNph9n3ItMzs6tGBGS2yykh8F3 pdCPzmDxedT4ALmW+Y9bOvgk7gNmNRXWzLdUQzGdxItV90oYZYi8GnUFDs0cGKXNKBCu4 hvngKZbEh97QmgZOPentw5lzFbBbMx6qM6clxVddfdaT4zgI+fuqwnClpB/RZvPr+gw== 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=xM8clBpj; 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 BB44760A2600; Wed, 2 Dec 2026 00:28:09 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org BB44760A2600 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=xM8clBpj DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=efficios.com; s=smtpout1; t=1796171289; bh=Mk1ZZrXaCC2k2joW5tf3sBHhs4Qyji9ZL63iP3eUQ+M=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=xM8clBpj0neTOuwzbdHCpdCvkekAaU8DqgaHISUJ0DVGK2DuNEyWOT/7fStvqpFZf hXiBHLyP3sPLJhDfNSCQzd4osWljvSYfekdNC9qoikgyEVPh9JXFeu9a9p9gF30VPW I4uGzZS+2cyvydOWVHFsnfyl7RR97mb4oFEF0ZDMTzI45PizOqRT3xOguYgyWvlaEo 4Omn+920Ur8HdoqmxmFfTxm3TxJL+hjPwbGBGdAzz1rP+GcitcD5ybdtvBkMnMzMsD uOTMcTrZVJwp15ckw3AEO/7hrHQZyYj8+6EgGO8L81o5t6ajDBeNQ4KC9H+qo7CNQf t9CQgi2hR87fQ== Received: from thinkos.internal.efficios.com (unknown [IPv6:2606:6d00:100:4000:cacb:9855:de1f:ded2]) by smtpout.efficios.com (Postfix) with ESMTPSA id 4jfJWX6CsczYkL; Tue, 01 Dec 2026 19:28:08 -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 10/13] selftests/rseq: Test two race windows of the concurrency ID mode switches Date: Tue, 1 Dec 2026 19:27:58 -0500 Message-ID: <212cdc1e4b36069d1fb0129e8179b5350df329d8.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 Two bugs of the concurrency ID mode switches show only when a task is scheduled inside a narrow window. The header comment of the new test draws each sequence as a diagram: - Case 1: a failed execve() rejoins the mm and switches it to per CPU mode. This needs a kernel where a failed execve() leaves the mm. - Case 2: a thread migrates while the mm switches to per CPU mode. This is the sequence of commit 4327fb13fa47 ("sched/mmcid: Prevent live lock on task to CPU mode transition"). Without a delay in the kernel, a test reaches these windows only by chance. Add mm_cid_window_test, with one case for each window. Each case makes 40 attempts, each with another delay. Case 1 runs on 2 CPUs. It fails on an mm_cid at or above the number of possible CPUs, or when the failed execve() takes more than 100ms. An mm_cid of 2 or more only gets a note, as in mm_cid_execve_test. Case 2 needs 3 to 256 CPUs, all possible CPUs allowed. It fails when the moving thread has not moved after 2s. On tip sched/urgent, case 1 fails on guests with 2 to 64 CPUs. It fails on another bug of the same rejoin, which mm_cid_execve_test also finds. Unless the task is scheduled out in the window, it returns to user space with no ID. Its mm_cid is 0x80000000, printed in decimal: mm_cid 2147483648 after a failed execve, 2 possible CPUs not ok 1 failed execve switches to per CPU mode Case 1 passes after "sched/mmcid: Move the execve() hooks into exec_mmap()". A failed execve() then no longer leaves the mm. Case 1 stays as a guard: if the rejoin comes back, it fails on 0x80000000 again. Case 2 passes on tip sched/urgent and after each of patches 1 to 4. A kernel that makes a thread sleep in the window hits it on every attempt. The test header names the places. Case 2 was checked this way, on a kernel with patches 1 to 4 and two changes: - The switch to per CPU mode skipped its transit phase. This undoes the fix of the commit above. - sched_mm_cid_fork() slept 3ms in fsleep(), after the mode change and before mm_cid_fixup_tasks_to_cpus(). Case 2 then hung eight guests with 3 to 16 CPUs. RCU stall reports showed a test thread looping in __schedule(). With the fsleep() alone, case 2 passed on the same eight guests. Signed-off-by: Mathieu Desnoyers --- tools/testing/selftests/rseq/.gitignore | 1 + tools/testing/selftests/rseq/Makefile | 3 +- .../selftests/rseq/mm_cid_window_test.c | 525 ++++++++++++++++++ 3 files changed, 528 insertions(+), 1 deletion(-) create mode 100644 tools/testing/selftests/rseq/mm_cid_window_test.c diff --git a/tools/testing/selftests/rseq/.gitignore b/tools/testing/selftests/rseq/.gitignore index 4c02ff8dccca..0065f51f53ab 100644 --- a/tools/testing/selftests/rseq/.gitignore +++ b/tools/testing/selftests/rseq/.gitignore @@ -13,6 +13,7 @@ mm_cid_bound_test mm_cid_execve_test mm_cid_transition_test mm_cid_exec_affinity_test +mm_cid_window_test syscall_errors_test slice_test legacy_check diff --git a/tools/testing/selftests/rseq/Makefile b/tools/testing/selftests/rseq/Makefile index 006e2e281ca8..d72502fc7652 100644 --- a/tools/testing/selftests/rseq/Makefile +++ b/tools/testing/selftests/rseq/Makefile @@ -21,7 +21,8 @@ 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_transition_test \ - mm_cid_bound_test mm_cid_exec_affinity_test + mm_cid_bound_test mm_cid_exec_affinity_test \ + mm_cid_window_test TEST_GEN_PROGS_EXTENDED = librseq.so \ param_test \ diff --git a/tools/testing/selftests/rseq/mm_cid_window_test.c b/tools/testing/selftests/rseq/mm_cid_window_test.c new file mode 100644 index 000000000000..32527fd3015f --- /dev/null +++ b/tools/testing/selftests/rseq/mm_cid_window_test.c @@ -0,0 +1,525 @@ +// SPDX-License-Identifier: LGPL-2.1 +/* + * Two race windows of the concurrency ID mode switches. + * + * Each case drives its threads through one sequence in which the kernel + * used to loop in mm_get_cid(), looking for a free ID. The window is + * narrow, so an attempt rarely lands in it. Each attempt uses another + * delay. On a kernel with the bug, an attempt that misses the window + * passes. A kernel that loops shows as a timeout. + * + * Case 1 needs a kernel where a failed execve(2) leaves the mm and + * rejoins it, as before "sched/mmcid: Move the execve() hooks into + * exec_mmap()". The process runs on two CPUs, in per task mode. A and B + * are its two threads, on CPU 0 and CPU 1. C is the vfork(2) child of B, + * so it shares the mm. The mm uses two IDs, 0 and 1. On a machine with + * two possible CPUs, these are the only IDs. + * + * CPU 0 CPU 1 + * + * A: execve(2) + * sched_mm_cid_before_execve() + * drops the ID of A + * B: vfork(2) + * sched_mm_cid_fork(C) + * C takes the free ID + * B: waits for C + * C: spins, never schedules + * A: execve(2) fails with ELOOP + * sched_mm_cid_after_execve() + * sched_mm_cid_fork(A): 3 users, + * sets MM_CID_ONCPU | MM_CID_TRANSIT + * A: is scheduled out, and in again + * mm_cid_schedin() + * mm_get_cid() loops: B and C + * hold both IDs + * A: mm_cid_fixup_tasks_to_cpus() + * is never reached + * + * Only that fixup could free an ID, and it runs in A. With more possible + * CPUs, mm_get_cid() does not loop. It takes an ID of 2 or more. That ID is + * below the number of possible CPUs, the only bound, so the case prints a + * note and passes. + * + * Before that change, sched_mm_cid_fork() gives A no ID in per CPU mode. + * Unless A is scheduled out in the window, it returns to user space with + * no ID and reads an mm_cid of 0x80000000. Case 1 then fails on that + * value. Since that change, a failed execve(2) stays in the mm and the + * sequence cannot happen. Case 1 stays as a guard against a return of the + * rejoin. + * + * Case 2 is the sequence of commit 4327fb13fa47 ("sched/mmcid: Prevent + * live lock on task to CPU mode transition"). The diagram shows a kernel + * without the transit phase which that commit added. The process is + * allowed on all possible CPUs, so the mm has one ID per CPU. M is its + * main thread, on CPU 0. T1 and T2 are two of the threads which M starts, + * one on each other CPU. + * + * CPU 0 CPU 1 CPU 2 + * + * M: starts one spinning + * thread on each other + * CPU: every ID is taken + * T1: spins T2: spins + * M: pthread_create() + * sched_mm_cid_fork(): + * one user too many, + * switches to per CPU + * mode + * T1: usleep(), then + * mm_cid_schedin(): + * CPU 1 takes the + * ID of T1 + * T1: sched_setaffinity() + * to CPU 2 + * T2: preempted, + * keeps its ID + * T1: mm_cid_schedin() + * mm_get_cid() + * loops, with the + * rq lock held + * M: mm_cid_fixup_tasks_to_cpus() + * waits for the rq lock + * of CPU 2 + * + * With that commit, T1 drops its ID when it leaves CPU 1. mm_get_cid() on + * CPU 2 then takes it. + * + * A kernel with an added sleep inside the window hits it on every + * attempt. The thread which switches the mode, A in case 1 and M in case + * 2, would sleep in these places: + * - in sched_mm_cid_fork(), after the mode change and before + * mm_cid_fixup_tasks_to_cpus() (both cases); + * - in sched_mm_cid_after_execve(), before sched_mm_cid_fork(), on a + * kernel where a failed execve(2) rejoins the mm (case 1). + */ + +#define _GNU_SOURCE +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "rseq.h" +#include "kselftest.h" + +#define ATTEMPTS 40 +#define MAX_DELAY_US 400 +#define SPIN_MS 300 +#define SLOW_MS 100 +#define MAX_SPINNERS 256 +#define MOVE_WAIT_MS 2000 + +#define CHILD_PASS 0 +#define CHILD_FAIL 1 +#define CHILD_SKIP 2 +#define CHILD_ERROR 3 + +static int go, done, moved, ready, stop; +static int pin_failed; +static int delay_us; +static int nr_possible; +static int cpus[MAX_SPINNERS], nr_cpus; +static char exec_path[PATH_MAX]; + +static long long now_us(void) +{ + struct timespec ts; + + clock_gettime(CLOCK_MONOTONIC, &ts); + return ts.tv_sec * 1000000LL + ts.tv_nsec / 1000; +} + +static void busy_wait_us(int us) +{ + long long end = now_us() + us; + + while (now_us() < end) + ; +} + +/* 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; +} + +static int pin(int cpu) +{ + cpu_set_t set; + + CPU_ZERO(&set); + CPU_SET(cpu, &set); + return sched_setaffinity(0, sizeof(set), &set); +} + +/* Fill @cpus with the allowed CPUs. Return their number. */ +static int read_allowed_cpus(void) +{ + cpu_set_t set; + int cpu; + + if (sched_getaffinity(0, sizeof(set), &set)) + return -1; + nr_cpus = 0; + for (cpu = 0; cpu < CPU_SETSIZE && nr_cpus < MAX_SPINNERS; cpu++) { + if (CPU_ISSET(cpu, &set)) + cpus[nr_cpus++] = cpu; + } + return CPU_COUNT(&set); +} + +/* + * Create a script that names itself as its interpreter. execve(2) on it + * follows the interpreter a few times and fails with ELOOP. On a kernel + * where a failed execve(2) rejoins the mm, that keeps the task out of its + * mm longer than a file of unknown format. Return 0, or -1 if execve(2) on + * it does not fail with ELOOP. + */ +static int make_loop_script(void) +{ + char *const av[] = { exec_path, NULL }; + FILE *f; + + snprintf(exec_path, sizeof(exec_path), "/tmp/mm_cid_window_test.%d", getpid()); + f = fopen(exec_path, "w"); + if (!f) + return -1; + fprintf(f, "#!%s\n", exec_path); + if (fclose(f) || chmod(exec_path, 0755)) + goto unlink; + if (execve(exec_path, av, NULL) == -1 && errno == ELOOP) + return 0; +unlink: + remove_file(exec_path); + return -1; +} + +/* Run thread B of case 1: call vfork(2) after a delay and wait for C. */ +static void *vfork_thread(void *arg) +{ + pid_t pid; + + if (pin(cpus[1])) { + RSEQ_WRITE_ONCE(pin_failed, 1); + return NULL; + } + while (!RSEQ_READ_ONCE(go)) + ; + busy_wait_us(delay_us); + pid = vfork(); + if (!pid) { + long long end = now_us() + SPIN_MS * 1000; + + /* Hold an ID on this CPU without scheduling. */ + while (!RSEQ_READ_ONCE(done) && now_us() < end) + ; + _exit(0); + } + if (pid < 0 || waitpid(pid, NULL, 0) != pid) { + fprintf(stderr, "vfork: %s\n", strerror(errno)); + abort(); + } + return NULL; +} + +/* + * Run one attempt of case 1 in a new mm on two CPUs. Return a CHILD_* + * status. + */ +static int run_child_join(int us) +{ + char *const av[] = { exec_path, NULL }; + long long start, end; + unsigned int cid; + pthread_t b; + int err, exec_errno; + + delay_us = us; + if (read_allowed_cpus() != 2 || pin(cpus[0])) + return CHILD_ERROR; + if (make_loop_script()) { + fprintf(stderr, "cannot run a script from /tmp\n"); + return CHILD_SKIP; + } + if (pthread_create(&b, NULL, vfork_thread, NULL)) { + remove_file(exec_path); + return CHILD_ERROR; + } + usleep(200); + start = now_us(); + RSEQ_WRITE_ONCE(go, 1); + execve(exec_path, av, NULL); + exec_errno = errno; + end = now_us(); + cid = rseq_current_mm_cid(); + RSEQ_WRITE_ONCE(done, 1); + err = join_thread(b); + if (remove_file(exec_path) || err) + return CHILD_ERROR; + if (exec_errno != ELOOP) { + fprintf(stderr, "execve(%s): %s, expected ELOOP\n", + exec_path, strerror(exec_errno)); + return CHILD_ERROR; + } + + /* B did not run its part if it could not pin itself. */ + if (pin_failed) { + fprintf(stderr, "a thread could not pin itself\n"); + return CHILD_ERROR; + } + /* The number of possible CPUs is the only bound. Two is a target. */ + if (cid >= (unsigned int)nr_possible) { + fprintf(stderr, "mm_cid %u after a failed execve, %d possible CPUs\n", + cid, nr_possible); + return CHILD_FAIL; + } + if (cid >= 2) + fprintf(stderr, + "mm_cid %u after a failed execve, reaches the 2 allowed CPUs\n", + cid); + if (end - start > SLOW_MS * 1000) { + fprintf(stderr, "the failed execve took %lld ms\n", (end - start) / 1000); + return CHILD_FAIL; + } + return CHILD_PASS; +} + +/* Spin on the CPU cpus[@arg], as a thread of case 2. Thread 1 also moves. */ +static void *spinner(void *arg) +{ + int nr = (long)arg; + + if (pin(cpus[nr])) { + RSEQ_WRITE_ONCE(pin_failed, 1); + return NULL; + } + __atomic_add_fetch(&ready, 1, __ATOMIC_RELAXED); + while (!RSEQ_READ_ONCE(stop)) { + if (nr != 1 || !RSEQ_READ_ONCE(go) || RSEQ_READ_ONCE(moved)) + continue; + busy_wait_us(delay_us); + /* Schedule out and in on this CPU, with the new mode set. */ + usleep(1000); + /* Move to the CPU of thread 2, which still holds its own ID. */ + if (pin(cpus[2])) + RSEQ_WRITE_ONCE(pin_failed, 1); + RSEQ_WRITE_ONCE(moved, 1); + } + return NULL; +} + +static void *idler(void *arg) +{ + while (!RSEQ_READ_ONCE(stop)) + usleep(1000); + return NULL; +} + +/* + * Run one attempt of case 2 in a new mm on all CPUs. Return a CHILD_* + * status. + */ +static int run_child_migrate(int us) +{ + static pthread_t threads[MAX_SPINNERS]; + int i, started = 0, waited = 0, ret = CHILD_PASS; + pthread_t extra; + + delay_us = us; + if (read_allowed_cpus() < 3 || pin(cpus[0])) + return CHILD_ERROR; + for (i = 1; i < nr_cpus; i++) { + if (pthread_create(&threads[i], NULL, spinner, (void *)(long)i)) + break; + started++; + } + if (started < nr_cpus - 1) { + ret = CHILD_ERROR; + goto out; + } + /* + * Wait until every thread runs on its CPU and holds an ID. A thread + * whose pin failed sets pin_failed and never gets ready. + */ + while (RSEQ_READ_ONCE(ready) < started && !RSEQ_READ_ONCE(pin_failed)) + usleep(100); + if (RSEQ_READ_ONCE(pin_failed)) + goto out; + usleep(1000); + RSEQ_WRITE_ONCE(go, 1); + if (pthread_create(&extra, NULL, idler, NULL)) { + ret = CHILD_ERROR; + goto out; + } + while (!RSEQ_READ_ONCE(moved) && waited++ < MOVE_WAIT_MS) + usleep(1000); + if (!RSEQ_READ_ONCE(moved)) { + fprintf(stderr, "the moving thread did not move\n"); + ret = CHILD_FAIL; + } + RSEQ_WRITE_ONCE(stop, 1); + if (join_thread(extra) && ret == CHILD_PASS) + ret = CHILD_ERROR; +out: + RSEQ_WRITE_ONCE(stop, 1); + for (i = 1; i <= started; i++) { + if (join_thread(threads[i]) && ret == CHILD_PASS) + ret = CHILD_ERROR; + } + /* The sequence did not run as written if a thread could not pin itself. */ + if (pin_failed) { + fprintf(stderr, "a thread could not pin itself\n"); + 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; +} + +/* + * Run @fn(@arg) in a new process that starts on the CPUs in @start, and + * return its exit status. The new process dies with this one. + */ +static int run_under_test(int (*fn)(int), int arg, const cpu_set_t *start, + const cpu_set_t *all) +{ + int status = 0; + pid_t child; + + if (sched_setaffinity(0, sizeof(*start), start)) + ksft_exit_fail_msg("sched_setaffinity: %s\n", strerror(errno)); + child = fork(); + if (!child) { + if (prctl(PR_SET_PDEATHSIG, SIGKILL, 0, 0, 0)) { + fprintf(stderr, "prctl(PR_SET_PDEATHSIG): %s\n", strerror(errno)); + _exit(CHILD_ERROR); + } + _exit(fn(arg)); + } + if (child < 0) + ksft_exit_fail_msg("fork: %s\n", strerror(errno)); + if (sched_setaffinity(0, sizeof(*all), all)) + 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_ERROR) + ksft_exit_fail_msg("the process under test failed to run\n"); + return WEXITSTATUS(status); +} + +static void report(int status, const char *name) +{ + if (status == CHILD_SKIP) + ksft_test_result_skip("%s\n", name); + else + ksft_test_result(status == CHILD_PASS, "%s\n", name); +} + +int main(void) +{ + const char *join = "failed execve switches to per CPU mode"; + const char *migrate = "thread migrates during the switch to per CPU mode"; + cpu_set_t all, two; + int status, i, cpu, nr_all; + + ksft_print_header(); + ksft_set_plan(2); + + 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), &all)) + ksft_exit_fail_msg("sched_getaffinity: %s\n", strerror(errno)); + nr_all = CPU_COUNT(&all); + nr_possible = read_nr_possible(); + if (nr_possible <= 0) + ksft_exit_skip("cannot read the possible CPUs\n"); + + if (nr_all < 2) { + ksft_test_result_skip("%s: fewer than 2 CPUs\n", join); + } else { + CPU_ZERO(&two); + for (cpu = 0; cpu < CPU_SETSIZE && CPU_COUNT(&two) < 2; cpu++) { + if (CPU_ISSET(cpu, &all)) + CPU_SET(cpu, &two); + } + for (i = 0; i < ATTEMPTS; i++) { + status = run_under_test(run_child_join, (i * 37) % MAX_DELAY_US, + &two, &all); + if (status != CHILD_PASS) + break; + } + report(status, join); + } + + /* Every ID is taken only when one thread spins on each possible CPU. */ + if (nr_all < 3 || nr_all > MAX_SPINNERS || nr_all != nr_possible) { + ksft_test_result_skip("%s: needs 3 to %d CPUs, all of them allowed\n", + migrate, MAX_SPINNERS); + } else { + for (i = 0; i < ATTEMPTS; i++) { + status = run_under_test(run_child_migrate, (i * 37) % MAX_DELAY_US, + &all, &all); + if (status != CHILD_PASS) + break; + } + report(status, migrate); + } + ksft_finished(); +} -- 2.47.3