From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=158.69.130.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796171290; b=AMZ1UxneL4A/mWQJyXI51tZPy1sjOD50rxZf84y3/kvTO5juM+fpTfKycyVTQ+zjfNZB V1UyY9kkzz+iyu9zhQ8QO+g1AK22LP4HSNPC1gFRtWJFWvvv5iIvEWvxox4H2YMz2P8b7 oaAKzxwVsbHoNsSBVLwylv1frC9QJ3BFLc5+DG8F4utBjrHlo0+1JS5Qr7lepdgh9PAao iBb0L8FECmPVOiX0FubxHBofRZDgpwg1t2oWJe83eE8a6Z6pXzBohmSZQoSA8MK3YAqUu oKWFsQMS+/hEledjRhSGOpABORTEejjPnje9VC7LSFVohh46bwOXyPPPBKDPrBTIvyw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796171290; h=DMARC-Filter:DKIM-Signature:Received:From:To:Cc:Subject:Date: Message-ID:X-Mailer:In-Reply-To:References:MIME-Version: Content-Transfer-Encoding; bh=fbGl+gmshhXzQ5PJTQbepKguQzr2rSA1OhQdxzWMB+8=; b=mnFTkhhFsmElqNsUNZCicPJ4q/kQ2+6tprt+qNg3Mv+0rJrmKHEz+NALM8ZSp1E1OQtZ jBv1DbtGy8KtmLrJ2YW6hko7L6tkWPpUDBbXDKJe0KXEBh1TUlymj7h/OlCYpbsoFjY0N bfiVw2RGj/+QeG91DfMz6Bv+vC7I51gVEMcoPs0b89i9QrhiV9xhcqcJGD7uxqrAvgzuU KXIK4b4bQreHkLtJVz6FAKzaaswAl1XGOhzdi2OnqksRleDhPPXmdwzUqxrp1gjxYfSIg tf4PkvTL9fhmG8BgY0BKked11Jj+3oQwfu2EX8D23cVT2qM21RvqHB7kXCCf4NCqOMg== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=efficios.com; spf=pass smtp.mailfrom=efficios.com; dkim=pass header.d=efficios.com header.i=@efficios.com header.a=rsa-sha256 header.s=smtpout1 header.b="Less/gyn"; arc=none smtp.remote-ip=158.69.130.18 Received: from smtpout.efficios.com (smtpout.efficios.com [158.69.130.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 6AAF7606A333; Wed, 2 Dec 2026 00:28:10 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 6AAF7606A333 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=efficios.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=efficios.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=efficios.com header.i=@efficios.com header.a=rsa-sha256 header.s=smtpout1 header.b=Less/gyn DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=efficios.com; s=smtpout1; t=1796171289; bh=fbGl+gmshhXzQ5PJTQbepKguQzr2rSA1OhQdxzWMB+8=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Less/gyn7/6638Iw5SrkAhZ4jUZf6Bz6TQkvJ+D/TQ9ZJBcvYpEgHfXM/pZxeDJoX CxCOm+wK/BjKPHS7vCmgz0yfrNJaUGROUdQqbFQqzZPFy/KpAJTf40sr8w3UBODt6b 9tJEvzso7r3ItRezoExqau0zFTCpFHgnKR3+PsxwD9MaCu5MF/xRJ3K8Fxu/DrLJC7 cR6QXnJh4qVlph0dgnoFUHuBjFrdMoyeRnfygtZyiXfeaTDPPZIwRDVJBr72g55U/D TnIOvZ4YyeLiqzDMMS29SRd6cjM+ryfGakR87RP64zXgW61KLQ3HwSYixDivv/hx/L aQfasehthC6Pw== Received: from thinkos.internal.efficios.com (unknown [IPv6:2606:6d00:100:4000:cacb:9855:de1f:ded2]) by smtpout.efficios.com (Postfix) with ESMTPSA id 4jfJWY3LxNzZ6b; Tue, 01 Dec 2026 19:28:09 -0500 (EST) From: Mathieu Desnoyers To: tglx@kernel.org, peterz@infradead.org Cc: Mathieu Desnoyers , 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 Message-ID: <32e29cac5aef20170afe774074529c30a973c94d.1796169827.git.mathieu.desnoyers@efficios.com> X-Mailer: git-send-email 2.47.3 In-Reply-To: References: X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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 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 --- .../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 +#include +#include #include #include #include #include #include #include +#include +#include #include #include @@ -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 . 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