[PATCH v2 12/13] selftests/rseq: Test the concurrency IDs with an io_uring SQPOLL thread
From: Mathieu Desnoyers
Date: Tue Dec 01 2026 - 19:28:57 EST
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@xxxxxxxxxxxx>
---
.../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(¶ms, 0, sizeof(params));
+ params.flags = IORING_SETUP_SQPOLL;
+ params.sq_thread_idle = 1000;
+ fd = syscall(__NR_io_uring_setup, 4, ¶ms);
+ 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