The number of concurrency IDs of an mm follows the CPUs its threads are allowed on. Another process can widen the affinity of a task while the task is in execve(). The new mm must then include the new CPUs, or its IDs are no longer compact. No test checks this. On tip sched/urgent, the change is lost when it lands between mm_init_cid() and the join of the new mm. In this sequence, the parent is the test process and the child is single-threaded. CPU 0 and CPU 1 stand for the first two CPUs the test may use. parent child ------ ----- sched_setaffinity(self, CPU 0) fork() (allowed on CPU 0 only) sched_setaffinity(self, CPU 1) execve() bprm_execve() ... exec_mmap() mm_init_cid(new mm) nr_cpus_allowed = 1 sched_setaffinity(child, all CPUs) set_cpus_allowed_common() mm_update_cpus_allowed(new mm) users == 0: return sched_mm_cid_after_execve() sched_mm_cid_fork() users = 1 The new mm keeps one allowed CPU, so max_cids is 1. Two threads then switch it to per CPU mode, and it does not switch back. Each CPU keeps the CID it took. Add mm_cid_exec_affinity_test. It runs this sequence up to 60 times, with a different delay before the parent widens the affinity. The new program runs one thread per CPU, then keeps two. Both must read an mm_cid below 2. The header comment of the test has the details. A first case allows all CPUs from the start. It passes on tip sched/urgent and with each kernel patch of this series. Both cases are skipped with fewer than 4 CPUs. The second case fails on tip sched/urgent, on every guest with 4 to 64 CPUs. On the 64-CPU guest: mm_cid 1 and 63 with 2 threads, largest spinner mm_cid 63 with 64 threads # attempt 4, 148 us delay before the affinity change not ok 2 affinity widened by another process during execve With "sched/mmcid: Move the execve() hooks into exec_mmap()" alone, the case still fails at times: on one 16-CPU guest in each of three runs. That patch moves sched_mm_cid_after_execve() into exec_mmap(), a few lines after mm_init_cid(). The change can still land in between. The case passes on every guest after "sched/mmcid: Add the allowed CPUs of each joining task to the mm". Signed-off-by: Mathieu Desnoyers --- tools/testing/selftests/rseq/.gitignore | 1 + tools/testing/selftests/rseq/Makefile | 2 +- .../rseq/mm_cid_exec_affinity_test.c | 288 ++++++++++++++++++ 3 files changed, 290 insertions(+), 1 deletion(-) create mode 100644 tools/testing/selftests/rseq/mm_cid_exec_affinity_test.c diff --git a/tools/testing/selftests/rseq/.gitignore b/tools/testing/selftests/rseq/.gitignore index ff63ada38d5f..4c02ff8dccca 100644 --- a/tools/testing/selftests/rseq/.gitignore +++ b/tools/testing/selftests/rseq/.gitignore @@ -12,6 +12,7 @@ param_test_mm_cid_compare_twice mm_cid_bound_test mm_cid_execve_test mm_cid_transition_test +mm_cid_exec_affinity_test syscall_errors_test slice_test legacy_check diff --git a/tools/testing/selftests/rseq/Makefile b/tools/testing/selftests/rseq/Makefile index b643ce9cb585..006e2e281ca8 100644 --- a/tools/testing/selftests/rseq/Makefile +++ b/tools/testing/selftests/rseq/Makefile @@ -21,7 +21,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_transition_test \ - mm_cid_bound_test + mm_cid_bound_test mm_cid_exec_affinity_test TEST_GEN_PROGS_EXTENDED = librseq.so \ param_test \ diff --git a/tools/testing/selftests/rseq/mm_cid_exec_affinity_test.c b/tools/testing/selftests/rseq/mm_cid_exec_affinity_test.c new file mode 100644 index 000000000000..3aa82f141df4 --- /dev/null +++ b/tools/testing/selftests/rseq/mm_cid_exec_affinity_test.c @@ -0,0 +1,288 @@ +// SPDX-License-Identifier: LGPL-2.1 +/* + * The concurrency IDs after another process widens the affinity of a task + * during its execve(2). + * + * The kernel sizes the ID range of an mm from the CPUs which its threads + * are allowed on. A task in execve(2) cannot change its own affinity, but + * another task can. The new mm must count the CPUs of that change. + * + * A child allowed on one CPU runs this test again with execve(2). The + * parent runs on another CPU. It widens the affinity of the child to all + * CPUs after a delay, which starts when the child is about to call + * execve(2). The delay differs on each attempt, so that some changes land + * during the execve(2). + * + * The new program waits until it is allowed on all CPUs. It runs one + * thread per CPU: the main thread and spinners. It lets them spread. Then + * it stops all spinners but the one with the largest mm_cid. A thread + * whose mm_cid is not below max_cids takes a free one below it when it is + * scheduled in. With two threads, max_cids is 2, so both must read an + * mm_cid below 2. If the new mm missed the change, it counts one CPU and + * stays in per CPU mode. The kept spinner can then keep its large mm_cid. + * A first case runs the same program with all CPUs allowed from the + * start. + * + * The new program does not change its own affinity. Such a change would + * add its CPUs to the mm, and hide a lost change. + */ + +#define _GNU_SOURCE +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "rseq.h" +#include "kselftest.h" + +#define MIN_CPUS 4 +#define MAX_SPINNERS 64 +#define ATTEMPTS 60 +#define MAX_DELAY_US 1500 +#define SPREAD_US 50000 +#define SETTLE_US 50000 +#define WIDE_WAIT_US 5000000 + +#define CHILD_PASS 0 +#define CHILD_FAIL 1 +#define CHILD_ERROR 2 + +struct spinner { + pthread_t thread; + unsigned int cid; + int stop; +}; + +static struct spinner spinners[MAX_SPINNERS]; +static int nap; + +static void *spinner_fn(void *arg) +{ + struct spinner *s = arg; + + if (rseq_register_current_thread()) + abort(); + while (!RSEQ_READ_ONCE(s->stop)) { + RSEQ_WRITE_ONCE(s->cid, rseq_current_mm_cid()); + /* Sleep, so that the next read follows a schedule in. */ + if (RSEQ_READ_ONCE(nap)) + usleep(1000); + } + rseq_unregister_current_thread(); + return NULL; +} + +/* Stop @s and join it. Return 0, or -1 after reporting a join error. */ +static int stop_spinner(struct spinner *s) +{ + int err; + + RSEQ_WRITE_ONCE(s->stop, 1); + err = pthread_join(s->thread, NULL); + if (err) { + fprintf(stderr, "pthread_join: %s\n", strerror(err)); + return -1; + } + return 0; +} + +/* + * The program under test, after the execve(2). Wait for @want allowed + * CPUs, then check the IDs of two threads. Return a CHILD_* status. + */ +static int check_main(int want) +{ + unsigned int spread_cid = 0, cid, own; + int nr = want < MAX_SPINNERS ? want : MAX_SPINNERS; + int i, err, started = 0, keep = 0, waited = 0, failed = 0; + cpu_set_t mask; + + for (;;) { + if (sched_getaffinity(0, sizeof(mask), &mask)) + return CHILD_ERROR; + if (CPU_COUNT(&mask) == want) + break; + if (waited >= WIDE_WAIT_US) { + fprintf(stderr, "allowed on %d CPUs, expected %d\n", + CPU_COUNT(&mask), want); + return CHILD_ERROR; + } + usleep(100); + waited += 100; + } + if (rseq_register_current_thread()) + return CHILD_ERROR; + + /* One thread per CPU, this one included. */ + for (i = 0; i < nr - 1; i++) { + err = pthread_create(&spinners[i].thread, NULL, spinner_fn, &spinners[i]); + if (err) { + fprintf(stderr, "pthread_create: %s\n", strerror(err)); + break; + } + started++; + } + if (started) + usleep(SPREAD_US); + + /* Keep the thread with the largest mm_cid. */ + for (i = 0; i < started; i++) { + cid = RSEQ_READ_ONCE(spinners[i].cid); + if (cid >= spread_cid) { + spread_cid = cid; + keep = i; + } + } + for (i = 0; i < started; i++) { + if (i != keep && stop_spinner(&spinners[i])) + failed = 1; + } + if (failed || started < nr - 1) { + if (started) + stop_spinner(&spinners[keep]); + return CHILD_ERROR; + } + + RSEQ_WRITE_ONCE(nap, 1); + usleep(SETTLE_US); + cid = RSEQ_READ_ONCE(spinners[keep].cid); + own = rseq_current_mm_cid(); + if (stop_spinner(&spinners[keep])) + return CHILD_ERROR; + + if (cid >= 2 || own >= 2) { + fprintf(stderr, + "mm_cid %u and %u with 2 threads, largest spinner mm_cid %u with %d threads\n", + own, cid, spread_cid, nr); + return CHILD_FAIL; + } + return CHILD_PASS; +} + +static void busy_wait_us(int us) +{ + struct timespec start, now; + + clock_gettime(CLOCK_MONOTONIC, &start); + do { + clock_gettime(CLOCK_MONOTONIC, &now); + } while ((now.tv_sec - start.tv_sec) * 1000000L + + (now.tv_nsec - start.tv_nsec) / 1000 < us); +} + +/* + * Run the program under test in a new process that starts on the CPUs in + * @start. Widen its affinity to @all after @delay_us, unless @delay_us is + * negative. Run on the CPUs in @beside meanwhile. Return a CHILD_* status. + */ +static int run_attempt(const cpu_set_t *start, const cpu_set_t *beside, + const cpu_set_t *all, int delay_us) +{ + char want[16], arg0[] = "mm_cid_exec_affinity_test", arg1[] = "check"; + char *const av[] = { arg0, arg1, want, NULL }; + int go[2], status = 0; + pid_t child; + char c = 0; + + snprintf(want, sizeof(want), "%d", CPU_COUNT(all)); + if (pipe2(go, O_CLOEXEC)) + ksft_exit_fail_msg("pipe2: %s\n", strerror(errno)); + if (sched_setaffinity(0, sizeof(*start), start)) + ksft_exit_fail_msg("sched_setaffinity: %s\n", strerror(errno)); + child = fork(); + if (!child) { + if (write(go[1], &c, 1) != 1) + _exit(CHILD_ERROR); + execv("/proc/self/exe", av); + _exit(CHILD_ERROR); + } + if (child < 0) + ksft_exit_fail_msg("fork: %s\n", strerror(errno)); + /* On the CPU of the child, the change would wait for its execve(2). */ + if (sched_setaffinity(0, sizeof(*beside), beside)) + ksft_exit_fail_msg("sched_setaffinity: %s\n", strerror(errno)); + close(go[1]); + if (read(go[0], &c, 1) == 1 && delay_us >= 0) { + busy_wait_us(delay_us); + /* The child is in execve(2), or about to enter or leave it. */ + if (sched_setaffinity(child, sizeof(*all), all)) + ksft_print_msg("sched_setaffinity on the child: %s\n", strerror(errno)); + } + close(go[0]); + 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)); + return WEXITSTATUS(status); +} + +int main(int argc, char **argv) +{ + cpu_set_t all, one, beside; + int status, i, cpu; + + if (argc == 3 && !strcmp(argv[1], "check")) + return check_main(atoi(argv[2])); + + 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)); + if (CPU_COUNT(&all) < MIN_CPUS) { + ksft_test_result_skip("all CPUs allowed before execve: fewer than %d CPUs\n", + MIN_CPUS); + ksft_test_result_skip("affinity widened during execve: fewer than %d CPUs\n", + MIN_CPUS); + ksft_finished(); + } + + /* The child starts on the first CPU, the parent runs on the second. */ + CPU_ZERO(&one); + CPU_ZERO(&beside); + for (cpu = 0; cpu < CPU_SETSIZE; cpu++) { + if (!CPU_ISSET(cpu, &all)) + continue; + if (!CPU_COUNT(&one)) { + CPU_SET(cpu, &one); + } else { + CPU_SET(cpu, &beside); + break; + } + } + + status = run_attempt(&all, &all, &all, -1); + if (status == CHILD_ERROR) + ksft_exit_fail_msg("the process under test failed to run\n"); + ksft_test_result(status == CHILD_PASS, "all CPUs allowed before execve\n"); + + for (i = 0; i < ATTEMPTS; i++) { + status = run_attempt(&one, &beside, &all, (i * 37) % MAX_DELAY_US); + if (status == CHILD_ERROR) + ksft_exit_fail_msg("the process under test failed to run\n"); + if (status != CHILD_PASS) { + ksft_print_msg("attempt %d, %d us delay before the affinity change\n", + i, (i * 37) % MAX_DELAY_US); + break; + } + } + ksft_test_result(status == CHILD_PASS, + "affinity widened by another process during execve\n"); + ksft_finished(); +} -- 2.47.3