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 12/13] selftests/rseq: Test the concurrency IDs with an io_uring SQPOLL thread
Date: Tue,  1 Dec 2026 19:28:00 -0500	[thread overview]
Message-ID: <32e29cac5aef20170afe774074529c30a973c94d.1796169827.git.mathieu.desnoyers@efficios.com> (raw)
In-Reply-To: <cover.1796169827.git.mathieu.desnoyers@efficios.com>

An io_uring SQPOLL thread is a user worker of the process that creates
the ring. When it starts, it calls set_cpus_allowed_ptr() with all
online CPUs, unless the ring pins it to one CPU with
IORING_SETUP_SQ_AFF. It counts as a user of the CIDs, so these CPUs
widen the allowed CPUs of the mm. A process limited by a cpuset then
gets CIDs for more CPUs than its threads may use.

Add the case "io_uring SQPOLL thread in a cpuset of 2 CPUs" to
mm_cid_bound_test. A process in a cpuset of two CPUs creates a ring with
an SQPOLL thread, then starts two worker threads. The main thread and
the two workers must read an mm_cid below 2. As in the other cases of
this test, that is a target: a miss is printed. The case fails only at
or above the number of possible CPUs.

Two details of the setup matter:
- The process is forked in the cpuset, not moved there. The allowed
  CPUs of an mm only grow, so a moved process keeps those it had.
- The ring is created before the main thread calls sched_setaffinity()
  to visit the CPUs. The SQPOLL thread would otherwise inherit the mask
  which sched_setaffinity() requested. set_cpus_allowed_ptr() keeps a
  task within that mask. So the thread would stay on the two CPUs and
  hide the widening.

The test declares its own copy of struct io_uring_params. The
tools/include of these tests hides the uapi stddef.h which
<linux/io_uring.h> needs. The case needs more than 2 CPUs, root, the
cgroup v2 cpuset controller and io_uring. It skips without them.

The case ran three times on ten guests with 3 to 64 CPUs. On tip
sched/urgent, the target was missed in 27 of the 30 runs. On the 64-CPU
guest:

  #    3 threads on 2 CPUs and SQPOLL: largest mm_cid 3 on CPU 1, above the CPU count target

It was met in all 30 runs after "sched/mmcid: Give no concurrency ID
to user workers", with a largest mm_cid of 1.

Signed-off-by: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
---
 .../selftests/rseq/mm_cid_bound_test.c        | 248 +++++++++++++++++-
 1 file changed, 247 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 9795cc030e7f..e39f0aafad31 100644
--- a/tools/testing/selftests/rseq/mm_cid_bound_test.c
+++ b/tools/testing/selftests/rseq/mm_cid_bound_test.c
@@ -33,16 +33,28 @@
  * IDs. The threads are then allowed on all CPUs. The kernel aims at giving
  * the IDs back to the tasks, so that three threads read an mm_cid below 3.
  * That is a target too: the case prints when it is missed.
+ *
+ * The io_uring case runs three threads and an io_uring SQPOLL thread in a
+ * cpuset of two CPUs. The SQPOLL thread is a user worker (PF_USER_WORKER).
+ * It never returns to user space and reads no mm_cid. It allows itself
+ * all online CPUs, and these must not widen the allowed CPUs of the mm.
+ * 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.
  */
 
 #define _GNU_SOURCE
 #include <errno.h>
+#include <fcntl.h>
+#include <limits.h>
 #include <pthread.h>
 #include <sched.h>
 #include <stdbool.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
+#include <sys/stat.h>
+#include <sys/syscall.h>
 #include <sys/wait.h>
 #include <unistd.h>
 
@@ -54,6 +66,29 @@
 #define WIDEN_THREADS	3
 #define WIDEN_MIN_CPUS	8
 #define WIDEN_SETTLE_US	20000
+#define URING_CPUS	2
+#define CHILD_SKIP	32
+
+/*
+ * struct io_uring_params of <linux/io_uring.h>. The tools/include of
+ * these tests hides the uapi stddef.h that this header needs.
+ */
+#define IORING_SETUP_SQPOLL	(1U << 1)
+struct uring_params {
+	unsigned int	sq_entries;
+	unsigned int	cq_entries;
+	unsigned int	flags;
+	unsigned int	sq_thread_cpu;
+	unsigned int	sq_thread_idle;
+	unsigned int	features;
+	unsigned int	wq_fd;
+	unsigned int	resv[3];
+	unsigned int	sq_off[8];
+	unsigned long long sq_user_addr;
+	unsigned int	cq_off[8];
+	unsigned long long cq_user_addr;
+};
+_Static_assert(sizeof(struct uring_params) == 120, "struct io_uring_params");
 
 /*
  * Exit status of the process under test: a mask of the bound and of the
@@ -282,6 +317,57 @@ static int run_child_widen(void)
 	return ret;
 }
 
+/*
+ * The process under test of the io_uring case. It starts on two CPUs.
+ * Return a mask of the OVER_* values, or CHILD_SKIP without io_uring.
+ */
+static int run_child_uring(void)
+{
+	struct uring_params params;
+	int nr_workers = 0, ret = 0, cpu, fd;
+	unsigned int cid;
+
+	if (sched_getaffinity(0, sizeof(allowed), &allowed))
+		return CHILD_ERROR;
+	nr_allowed = CPU_COUNT(&allowed);
+
+	/*
+	 * Create the ring before tour() calls sched_setaffinity(). The SQPOLL
+	 * thread inherits the mask which sched_setaffinity() requested, and
+	 * its set_cpus_allowed_ptr() stays within it. It would then stay on
+	 * the two CPUs and not widen the mm.
+	 */
+	memset(&params, 0, sizeof(params));
+	params.flags = IORING_SETUP_SQPOLL;
+	params.sq_thread_idle = 1000;
+	fd = syscall(__NR_io_uring_setup, 4, &params);
+	if (fd < 0) {
+		ksft_print_msg("io_uring_setup: %s\n", strerror(errno));
+		return CHILD_SKIP;
+	}
+	if (set_workers(&nr_workers, WIDEN_THREADS - 1)) {
+		ret = CHILD_ERROR;
+		goto out;
+	}
+	measure(nr_workers, &cpu);
+	/* The kernel changes the owners of the IDs from a work item. */
+	usleep(WIDEN_SETTLE_US);
+	measure(nr_workers, &cpu);
+	cid = measure(nr_workers, &cpu);
+	if (cid >= (unsigned int)nr_possible)
+		ret |= OVER_POSSIBLE;
+	if (cid >= URING_CPUS)
+		ret |= OVER_STRICT;
+	ksft_print_msg("%4d threads on %d CPUs and SQPOLL: largest mm_cid %u on CPU %d%s\n",
+		       WIDEN_THREADS, nr_allowed, cid, cpu,
+		       ret & OVER_STRICT ? ", above the CPU count target" : "");
+out:
+	if (set_workers(&nr_workers, 0))
+		ret |= CHILD_ERROR;
+	close(fd);
+	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)
 {
@@ -413,13 +499,172 @@ static void test_widen(const cpu_set_t *all)
 	ksft_test_result(!(WEXITSTATUS(status) & OVER_POSSIBLE), "%s\n", name);
 }
 
+/* Write @val to the file @name of the directory @dir. Return 0 or -1. */
+static int write_file(const char *dir, const char *name, const char *val)
+{
+	char path[PATH_MAX];
+	ssize_t len = strlen(val);
+	int fd, ret = 0;
+
+	snprintf(path, sizeof(path), "%s/%s", dir, name);
+	fd = open(path, 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)
+{
+	char path[PATH_MAX];
+	ssize_t len;
+	int fd;
+
+	snprintf(path, sizeof(path), "%s/%s", dir, name);
+	fd = open(path, O_RDONLY);
+	if (fd < 0)
+		return -1;
+	len = read(fd, buf, size - 1);
+	close(fd);
+	if (len < 0)
+		return -1;
+	buf[len] = '\0';
+	return 0;
+}
+
+/* Find the mount point of cgroup v2 in @root. Return 0 or -1. */
+static int find_cgroup2(char *root, size_t size)
+{
+	char line[PATH_MAX + 128], dir[PATH_MAX], type[64];
+	FILE *f = fopen("/proc/self/mounts", "r");
+	int ret = -1;
+
+	while (f && ret && fgets(line, sizeof(line), f)) {
+		if (sscanf(line, "%*s %4095s %63s", dir, type) == 2 &&
+		    !strcmp(type, "cgroup2")) {
+			snprintf(root, size, "%s", dir);
+			ret = 0;
+		}
+	}
+	if (f)
+		fclose(f);
+	return ret;
+}
+
+/*
+ * Make the cpuset @cg of two CPUs of @all, under the cgroup v2 root
+ * @root. Set *@added if the cpuset controller had to be enabled. Return
+ * 0, or -1 if the system has no such cpuset.
+ */
+static int make_cpuset(const char *root, char *cg, size_t size,
+		       const cpu_set_t *all, bool *added)
+{
+	char buf[256], cpus[32];
+	cpu_set_t two;
+	int cpu, n = 0;
+
+	*added = false;
+	if (read_file(root, "cgroup.controllers", buf, sizeof(buf)) ||
+	    !strstr(buf, "cpuset"))
+		return -1;
+	if (read_file(root, "cgroup.subtree_control", buf, sizeof(buf)))
+		return -1;
+	if (!strstr(buf, "cpuset")) {
+		if (write_file(root, "cgroup.subtree_control", "+cpuset"))
+			return -1;
+		*added = true;
+	}
+	snprintf(cg, size, "%s/mm_cid_bound_test", root);
+	if (mkdir(cg, 0755) && errno != EEXIST)
+		return -1;
+	narrow_cpus(&two, all, URING_CPUS);
+	for (cpu = 0; cpu < CPU_SETSIZE; cpu++) {
+		if (CPU_ISSET(cpu, &two))
+			n += snprintf(cpus + n, sizeof(cpus) - n, n ? ",%d" : "%d", cpu);
+	}
+	return write_file(cg, "cpuset.cpus", cpus);
+}
+
+static void remove_cpuset(const char *root, const char *cg, bool added)
+{
+	if (rmdir(cg))
+		ksft_print_msg("rmdir %s: %s\n", cg, strerror(errno));
+	if (added && write_file(root, "cgroup.subtree_control", "-cpuset"))
+		ksft_print_msg("the cpuset controller stays enabled\n");
+}
+
+static void test_uring(const cpu_set_t *all)
+{
+	const char *name = "io_uring SQPOLL thread in a cpuset of 2 CPUs: below the possible CPUs";
+	char root[PATH_MAX], cg[PATH_MAX + 32] = "";
+	bool added = false;
+	int status = 0;
+	pid_t child;
+
+	/* No CPU outside the cpuset: the SQPOLL thread cannot widen the mm. */
+	if (CPU_COUNT(all) <= URING_CPUS) {
+		ksft_test_result_skip("%s: needs more than %d CPUs\n", name, URING_CPUS);
+		return;
+	}
+	/* A cpuset, not sched_setaffinity(): see run_child_uring(). */
+	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;
+	}
+	child = fork();
+	if (!child) {
+		pid_t pid;
+
+		if (write_file(cg, "cgroup.procs", "0"))
+			_exit(CHILD_ERROR);
+		/*
+		 * The allowed CPUs of an mm only grow, so this one keeps all
+		 * CPUs. A new mm starts with the two CPUs of the cpuset.
+		 */
+		pid = fork();
+		if (!pid)
+			_exit(run_child_uring());
+		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));
+	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 io_uring\n", name);
+		return;
+	}
+	/* The CPU count is a target. Only the bound decides the result. */
+	if (WEXITSTATUS(status) & OVER_STRICT)
+		ksft_print_msg("SQPOLL in a cpuset of 2 CPUs: above the CPU 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(4);
+	ksft_set_plan(5);
 
 	if (!rseq_available())
 		ksft_exit_skip("rseq is unavailable\n");
@@ -439,5 +684,6 @@ int main(void)
 	test_ramp(&all, 4, "4 CPUs");
 	test_ramp(&all, nr_all, "all CPUs");
 	test_widen(&all);
+	test_uring(&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 ` Mathieu Desnoyers [this message]
2026-12-02  0:28 ` [PATCH v2 13/13] selftests/rseq: Test the concurrency IDs after clone3() into a wider cpuset Mathieu Desnoyers
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=32e29cac5aef20170afe774074529c30a973c94d.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®