In per CPU mode, the CIDs go back to the tasks once the user count of the mm drops below pcpu_thrs. This threshold grows with the allowed CPUs of the mm. So mm_update_cpus_allowed() recomputes it when an affinity change widens the mm. clone3(CLONE_INTO_CGROUP) sets the CPUs of the new task in cpuset_fork(), before the task joins in sched_mm_cid_fork(). Since "sched/mmcid: Serialize joins and exits with affinity changes by pi_lock", mm_update_cpus_allowed() ignores this change: the task has not joined the mm yet. The join then widens the mm, and recomputes pcpu_thrs (see "sched/mmcid: Add the allowed CPUs of each joining task to the mm"). No later affinity change can do it, because the mm is already wide. Add the case "join from a wider cpuset" to mm_cid_bound_test. The diagram shows it on a guest with 64 CPUs, with a join which does not recompute pcpu_thrs. T0 is the main thread of the process under test, forked in a cpuset of CPUs 0 and 1. T1 and T2 are its other threads. C is a task which shares the mm of T0, created in a cpuset of all CPUs. T0 T1, T2 C -------------------------------------------- ------------- ------------------------- pthread_create(T1), pthread_create(T2) sched_mm_cid_fork(T2): 3 users > max_cids 2: per CPU mode, pcpu_thrs 2 tour(): visits CPUs 0 and 1 tour(): same sched_setaffinity(all CPUs): the cpuset keeps T0 on CPUs 0 and 1 clone3(CLONE_VM | CLONE_VFORK | CLONE_INTO_CGROUP) cpuset_fork(C): set_cpus_allowed_ptr(C, CPUs 0-63) mm_update_cpus_allowed(C): C not active, return sched_mm_cid_fork(C): nr_cpus_allowed 2 -> 64, 4 users >= pcpu_thrs 2: stays in per CPU mode waits for C (CLONE_VFORK) exit() sched_mm_cid_exit(C): 3 users >= pcpu_thrs 2: stays in per CPU mode writes 0 to cgroup.procs of the wide cpuset set_cpus_allowed_ptr(T0, T1, T2) mm_update_cpus_allowed(): mm has 64 CPUs, return tour(): visits CPUs 0 to 63 tour(): same The CPUs keep the IDs, so the threads read mm_cid values up to 63. With the recompute, the join sets pcpu_thrs to 32, and the mm goes back to per task mode. The target is then an mm_cid below 3. As in the "widened from 2 CPUs" case, a missed target only prints a note. The case fails only on an mm_cid at or above the number of possible CPUs. The case needs at least 8 CPUs, root and the cgroup v2 cpuset controller. C runs on the stack of T0, as after vfork(2), so it must not return or call a function. So clone3() and exit() are issued from inline assembly, on x86-64 and arm64 only. The arm64 code was only compile- tested. The target is met on x86-64 guests with 8 to 64 CPUs, on tip sched/urgent and after each kernel patch of this series. On tip, the affinity change in cpuset_fork() still recomputes pcpu_thrs. To check that the case catches a join without the recompute, the recompute was removed from a kernel with the four kernel patches. The largest mm_cid was then the CPU count minus one, on guests with 8, 16 and 64 CPUs: # 3 threads after a join on 64 CPUs: largest mm_cid 63 on CPU 63, above the thread count target Signed-off-by: Mathieu Desnoyers --- .../selftests/rseq/mm_cid_bound_test.c | 245 +++++++++++++++++- 1 file changed, 244 insertions(+), 1 deletion(-) diff --git a/tools/testing/selftests/rseq/mm_cid_bound_test.c b/tools/testing/selftests/rseq/mm_cid_bound_test.c index e39f0aafad31..f6b554969cdc 100644 --- a/tools/testing/selftests/rseq/mm_cid_bound_test.c +++ b/tools/testing/selftests/rseq/mm_cid_bound_test.c @@ -41,6 +41,17 @@ * The CPUs then keep owning the IDs, and the three threads read an mm_cid * below 2. That is a target as well. The case needs root and the * cgroup v2 cpuset controller. + * + * The join case starts three threads in a cpuset of two CPUs, so the CPUs + * own the IDs. The main thread then creates a task with + * clone3(CLONE_VM | CLONE_VFORK | CLONE_INTO_CGROUP), into a cpuset of + * all CPUs. The kernel gives the new task all CPUs before it joins the mm + * in sched_mm_cid_fork(). So the join widens the allowed CPUs of the mm. + * The new task exits at once. The three threads then move to the cpuset + * of all CPUs. This adds no CPU to the mm: the join already added them. + * The kernel aims at giving the IDs back to the tasks, as in the widening + * case. The case prints when an mm_cid is not below 3. The case needs + * root and the cgroup v2 cpuset controller. */ #define _GNU_SOURCE @@ -49,6 +60,7 @@ #include #include #include +#include #include #include #include @@ -69,6 +81,29 @@ #define URING_CPUS 2 #define CHILD_SKIP 32 +#ifndef __NR_clone3 +#define __NR_clone3 435 +#endif +#ifndef CLONE_INTO_CGROUP +#define CLONE_INTO_CGROUP 0x200000000ULL +#endif + +/* struct clone_args of , up to its cgroup field. */ +struct clone_args_v2 { + unsigned long long flags; + unsigned long long pidfd; + unsigned long long child_tid; + unsigned long long parent_tid; + unsigned long long exit_signal; + unsigned long long stack; + unsigned long long stack_size; + unsigned long long tls; + unsigned long long set_tid; + unsigned long long set_tid_size; + unsigned long long cgroup; +}; +_Static_assert(sizeof(struct clone_args_v2) == 88, "struct clone_args"); + /* * struct io_uring_params of . The tools/include of * these tests hides the uapi stddef.h that this header needs. @@ -368,6 +403,121 @@ static int run_child_uring(void) return ret; } +static int write_file_at(int dirfd, const char *name, const char *val); + +/* + * Create a task that shares the mm, in the cgroup of the directory + * @cgfd, and wait until it exits. The task runs on the stack of the + * caller, as after vfork(2), so it exits at once without a function + * call. Return its pid, or -errno. + */ +static long clone_vm_into_cgroup(int cgfd) +{ + struct clone_args_v2 args = { + .flags = CLONE_VM | CLONE_VFORK | CLONE_INTO_CGROUP, + .exit_signal = SIGCHLD, + .cgroup = cgfd, + }; +#if defined(__x86_64__) + long ret; + + asm volatile ("syscall\n\t" + "test %%rax, %%rax\n\t" + "jnz 1f\n\t" + "mov %[nr_exit], %%eax\n\t" + "xor %%edi, %%edi\n\t" + "syscall\n\t" + "1:" + : "=a" (ret) + : "0" ((long)__NR_clone3), "D" (&args), "S" (sizeof(args)), + [nr_exit] "i" (__NR_exit) + : "rcx", "r11", "memory"); + return ret; +#elif defined(__aarch64__) + register long x0 asm("x0") = (long)&args; + register long x1 asm("x1") = sizeof(args); + register long x8 asm("x8") = __NR_clone3; + + asm volatile ("svc #0\n\t" + "cbnz x0, 1f\n\t" + "mov x8, %[nr_exit]\n\t" + "svc #0\n\t" + "1:" + : "+r" (x0), "+r" (x8) + : "r" (x1), [nr_exit] "i" (__NR_exit) + : "memory"); + return x0; +#else + (void)args; + return -EOPNOTSUPP; +#endif +} + +/* + * The process under test of the join case. It starts on two CPUs, in + * a cpuset. @widefd is the directory of a cgroup of all CPUs. Return a + * mask of the OVER_* values, or CHILD_SKIP without clone3(). + */ +static int run_child_join(int widefd) +{ + int nr_workers = 0, ret = 0, cpu, status; + unsigned int cid; + long pid; + + if (sched_getaffinity(0, sizeof(allowed), &allowed)) + return CHILD_ERROR; + nr_allowed = CPU_COUNT(&allowed); + + if (set_workers(&nr_workers, WIDEN_THREADS - 1)) + return CHILD_ERROR; + measure(nr_workers, &cpu); + cid = measure(nr_workers, &cpu); + ksft_print_msg("%4d threads on %d CPUs: largest mm_cid %u on CPU %d\n", + WIDEN_THREADS, nr_allowed, cid, cpu); + + /* + * The new task inherits the affinity which tour() requested last: + * the two CPUs. The kernel would intersect it with the wide cpuset, + * and the new task would not widen the mm. So request all CPUs. The + * cpuset still keeps this thread on the two CPUs. + */ + if (sched_setaffinity(0, sizeof(all_cpus), &all_cpus)) { + ret = CHILD_ERROR; + goto out; + } + pid = clone_vm_into_cgroup(widefd); + if (pid < 0) { + ksft_print_msg("clone3: %s\n", strerror(-pid)); + ret = CHILD_SKIP; + goto out; + } + if (waitpid(pid, &status, 0) != pid) { + ret = CHILD_ERROR; + goto out; + } + + /* Move the process into the wide cgroup of the new task. */ + if (write_file_at(widefd, "cgroup.procs", "0")) { + ret = CHILD_ERROR; + goto out; + } + allowed = all_cpus; + nr_allowed = CPU_COUNT(&allowed); + measure(nr_workers, &cpu); + cid = measure(nr_workers, &cpu); + if (cid >= (unsigned int)nr_possible) + ret |= OVER_POSSIBLE; + if (cid >= WIDEN_THREADS) + ret |= OVER_STRICT; + ksft_print_msg("%4d threads after a join on %d CPUs: largest mm_cid %u on CPU %d%s\n", + WIDEN_THREADS, nr_allowed, cid, cpu, + ret & OVER_STRICT ? ", above the thread count target" : ""); +out: + if (set_workers(&nr_workers, 0)) + ret |= CHILD_ERROR; + return ret; +} + /* Keep the first @nr CPUs of @src in @dst. */ static int narrow_cpus(cpu_set_t *dst, const cpu_set_t *src, int nr) { @@ -516,6 +666,21 @@ static int write_file(const char *dir, const char *name, const char *val) return ret; } +/* Write @val to the file @name of the directory fd @dirfd. Return 0 or -1. */ +static int write_file_at(int dirfd, const char *name, const char *val) +{ + ssize_t len = strlen(val); + int fd, ret = 0; + + fd = openat(dirfd, name, O_WRONLY); + if (fd < 0) + return -1; + if (write(fd, val, len) != len) + ret = -1; + close(fd); + return ret; +} + /* Read the file @name of @dir into @buf. Return 0 or -1. */ static int read_file(const char *dir, const char *name, char *buf, size_t size) { @@ -658,13 +823,90 @@ static void test_uring(const cpu_set_t *all) ksft_test_result(!(WEXITSTATUS(status) & OVER_POSSIBLE), "%s\n", name); } +static void test_join(const cpu_set_t *all) +{ + const char *name = "join from a wider cpuset: below the possible CPUs"; + char root[PATH_MAX], cg[PATH_MAX + 32] = "", wide[PATH_MAX + 32]; + bool added = false; + int status = 0, widefd; + pid_t child; + + /* With fewer CPUs the kernel keeps the CPUs as owners of the IDs. */ + if (CPU_COUNT(all) < WIDEN_MIN_CPUS || nr_possible < WIDEN_MIN_CPUS) { + ksft_test_result_skip("%s: fewer than %d CPUs\n", name, WIDEN_MIN_CPUS); + return; + } + if (find_cgroup2(root, sizeof(root))) { + ksft_test_result_skip("%s: no cgroup v2\n", name); + return; + } + if (make_cpuset(root, cg, sizeof(cg), all, &added)) { + ksft_print_msg("cpuset of 2 CPUs: %s\n", strerror(errno)); + if (cg[0]) + rmdir(cg); + if (added) + write_file(root, "cgroup.subtree_control", "-cpuset"); + ksft_test_result_skip("%s: no cgroup v2 cpuset\n", name); + return; + } + /* A cpuset with no CPUs of its own has all the CPUs of the root. */ + snprintf(wide, sizeof(wide), "%s/mm_cid_bound_test_wide", root); + if (mkdir(wide, 0755) && errno != EEXIST) { + ksft_print_msg("mkdir %s: %s\n", wide, strerror(errno)); + remove_cpuset(root, cg, added); + ksft_exit_fail_msg("cannot make a cpuset of all CPUs\n"); + } + widefd = open(wide, O_RDONLY | O_DIRECTORY); + if (widefd < 0) { + ksft_print_msg("open %s: %s\n", wide, strerror(errno)); + rmdir(wide); + remove_cpuset(root, cg, added); + ksft_exit_fail_msg("cannot make a cpuset of all CPUs\n"); + } + all_cpus = *all; + + child = fork(); + if (!child) { + pid_t pid; + + if (write_file(cg, "cgroup.procs", "0")) + _exit(CHILD_ERROR); + /* A new mm starts with the two CPUs of the cpuset. */ + pid = fork(); + if (!pid) + _exit(run_child_join(widefd)); + if (pid < 0 || waitpid(pid, &status, 0) != pid || + !WIFEXITED(status)) + _exit(CHILD_ERROR); + _exit(WEXITSTATUS(status)); + } + if (child < 0) + ksft_exit_fail_msg("fork: %s\n", strerror(errno)); + if (waitpid(child, &status, 0) != child) + ksft_exit_fail_msg("waitpid: %s\n", strerror(errno)); + close(widefd); + if (rmdir(wide)) + ksft_print_msg("rmdir %s: %s\n", wide, strerror(errno)); + remove_cpuset(root, cg, added); + if (!WIFEXITED(status) || (WEXITSTATUS(status) & CHILD_ERROR)) + ksft_exit_fail_msg("the process under test failed to run\n"); + if (WEXITSTATUS(status) & CHILD_SKIP) { + ksft_test_result_skip("%s: no clone3() into a cgroup\n", name); + return; + } + /* The thread count is a target. Only the bound decides the result. */ + if (WEXITSTATUS(status) & OVER_STRICT) + ksft_print_msg("join from a wider cpuset: above the thread count target\n"); + ksft_test_result(!(WEXITSTATUS(status) & OVER_POSSIBLE), "%s\n", name); +} + int main(void) { cpu_set_t all; int nr_all; ksft_print_header(); - ksft_set_plan(5); + ksft_set_plan(6); if (!rseq_available()) ksft_exit_skip("rseq is unavailable\n"); @@ -685,5 +927,6 @@ int main(void) test_ramp(&all, nr_all, "all CPUs"); test_widen(&all); test_uring(&all); + test_join(&all); ksft_finished(); } -- 2.47.3