mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
To: tglx@kernel.org, peterz@infradead.org
Cc: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>,
	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 13/13] selftests/rseq: Test the concurrency IDs after clone3() into a wider cpuset
Date: Tue,  1 Dec 2026 19:28:01 -0500	[thread overview]
Message-ID: <0c7160b638a20db172db86336764eec6c19951ed.1796169827.git.mathieu.desnoyers@efficios.com> (raw)
In-Reply-To: <cover.1796169827.git.mathieu.desnoyers@efficios.com>

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 <mathieu.desnoyers@efficios.com>
---
 .../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 <limits.h>
 #include <pthread.h>
 #include <sched.h>
+#include <signal.h>
 #include <stdbool.h>
 #include <stdio.h>
 #include <stdlib.h>
@@ -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 <linux/sched.h>, 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 <linux/io_uring.h>. 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


  parent reply	other threads:[~2026-12-02  0:28 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-02  0:27 [PATCH v2 00/13] sched/mmcid: fixes for execve, mode switches and affinity Mathieu Desnoyers
2026-12-02  0:27 ` [PATCH v2 01/13] sched/mmcid: Move the execve() hooks into exec_mmap() Mathieu Desnoyers
2026-12-02  0:27 ` [PATCH v2 02/13] sched/mmcid: Add the allowed CPUs of each joining task to the mm Mathieu Desnoyers
2026-12-02  0:27 ` [PATCH v2 03/13] sched/mmcid: Serialize joins and exits with affinity changes by pi_lock Mathieu Desnoyers
2026-12-02  0:27 ` [PATCH v2 04/13] sched/mmcid: Give no concurrency ID to user workers Mathieu Desnoyers
2026-12-02  0:27 ` [PATCH v2 05/13] selftests/rseq: Test the concurrency ID after a failed execve Mathieu Desnoyers
2026-12-02  0:27 ` [PATCH v2 06/13] selftests/rseq: Test the concurrency IDs across ownership mode switches Mathieu Desnoyers
2026-12-02  0:27 ` [PATCH v2 07/13] selftests/rseq: Test the upper bound of the concurrency IDs Mathieu Desnoyers
2026-12-02  0:27 ` [PATCH v2 08/13] selftests/rseq: Test an affinity change by another process during execve Mathieu Desnoyers
2026-12-02  0:27 ` [PATCH v2 09/13] selftests/rseq: Test the concurrency IDs after the affinity is widened Mathieu Desnoyers
2026-12-02  0:27 ` [PATCH v2 10/13] selftests/rseq: Test two race windows of the concurrency ID mode switches Mathieu Desnoyers
2026-12-02  0:27 ` [PATCH v2 11/13] selftests/rseq: Test the last user after an affinity change during execve Mathieu Desnoyers
2026-12-02  0:28 ` [PATCH v2 12/13] selftests/rseq: Test the concurrency IDs with an io_uring SQPOLL thread Mathieu Desnoyers
2026-12-02  0:28 ` Mathieu Desnoyers [this message]
2026-12-02  2:49 ` [PATCH v2 00/13] sched/mmcid: fixes for execve, mode switches and affinity Karl Mehltretter

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=0c7160b638a20db172db86336764eec6c19951ed.1796169827.git.mathieu.desnoyers@efficios.com \
    --to=mathieu.desnoyers@efficios.com \
    --cc=bestswngs@gmail.com \
    --cc=boqun@kernel.org \
    --cc=brauner@kernel.org \
    --cc=bsegall@google.com \
    --cc=dietmar.eggemann@arm.com \
    --cc=jack@suse.cz \
    --cc=juri.lelli@redhat.com \
    --cc=kees@kernel.org \
    --cc=kmehltretter@gmail.com \
    --cc=kprateek.nayak@amd.com \
    --cc=linux-fsdevel@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-kselftest@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=mgorman@suse.de \
    --cc=mingo@redhat.com \
    --cc=oleg@redhat.com \
    --cc=paulmck@kernel.org \
    --cc=peterz@infradead.org \
    --cc=rostedt@goodmis.org \
    --cc=shuah@kernel.org \
    --cc=tglx@kernel.org \
    --cc=vincent.guittot@linaro.org \
    --cc=viro@zeniv.linux.org.uk \
    --cc=vschneid@redhat.com \
    --cc=xmei5@asu.edu \
    --cc=xujiakai24@mails.ucas.ac.cn \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®