io-wq workers started after the cpuset of a task changes, or after the task moves to another cgroup, run on the CPUs the task had when its io-wq was created: the kernel reads the cpuset only then. The kernel fix is "io_uring/io-wq: apply the current cpuset to new workers". Add a test that starts a worker on one CPU, moves the task to another CPU, once by changing its cpuset and once by moving it to another cgroup, starts three more workers, and checks that all of them run on the new CPU only. It needs root and cgroup v2 with the cpuset controller, and skips otherwise. Signed-off-by: Mikhail Malyshev --- The kernel fix: https://lore.kernel.org/io-uring/20261111162705.1150983-1-mike.malyshev@gmail.com/ On io_uring-7.4 under QEMU/KVM (x86_64, 8 CPUs, cgroup v2) the test fails without the kernel fix, at its first subtest (the cpuset change), and passes with it; the same on 6.18.y. Without root it skips. The test was written with Claude Code (Anthropic, Claude Opus 5.5) together with the kernel fix; I have reviewed it. test/Makefile | 1 + test/iowq-cpuset.c | 330 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 331 insertions(+) create mode 100644 test/iowq-cpuset.c diff --git a/test/Makefile b/test/Makefile index 8771aa50..a4249ecd 100644 --- a/test/Makefile +++ b/test/Makefile @@ -159,6 +159,7 @@ test_srcs := \ io-wq-exit.c \ io-wq-unused-exit.c \ iowait.c \ + iowq-cpuset.c \ kallsyms.c \ large-resize.c \ lfs-openat.c \ diff --git a/test/iowq-cpuset.c b/test/iowq-cpuset.c new file mode 100644 index 00000000..49657655 --- /dev/null +++ b/test/iowq-cpuset.c @@ -0,0 +1,330 @@ +/* SPDX-License-Identifier: MIT */ +/* + * Description: test that io-wq workers started after the cpuset of the task + * changes, or after the task moves to another cgroup, run on + * the CPUs of the cpuset the task is in now, not on those it + * had when its io-wq was created. Needs root and cgroup v2 + * with the cpuset controller. + */ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "liburing.h" +#include "helpers.h" + +#define CG_ROOT "/sys/fs/cgroup" +#define NR_PARKED 4 + +static char cg_a[64], cg_b[64], fifo[64]; +static int cpu_old, cpu_new, parked; + +static int write_str(const char *path, const char *val) +{ + int fd, ret; + + fd = open(path, O_WRONLY); + if (fd < 0) + return -errno; + ret = write(fd, val, strlen(val)); + ret = ret < 0 ? -errno : 0; + close(fd); + return ret; +} + +static int set_cpu(const char *cg, int cpu) +{ + char path[128], val[16]; + + snprintf(path, sizeof(path), "%s/cpuset.cpus", cg); + snprintf(val, sizeof(val), "%d", cpu); + return write_str(path, val); +} + +static int join(const char *cg) +{ + char path[128], val[16]; + + snprintf(path, sizeof(path), "%s/cgroup.procs", cg); + snprintf(val, sizeof(val), "%d", getpid()); + return write_str(path, val); +} + +/* + * Count the io-wq workers of this process, and check that each of them may + * run on @cpu only. Returns the count, or -1 if a worker is elsewhere. + */ +static int check_workers(int cpu, int report) +{ + char want[32]; + struct dirent *de; + int nr = 0, ret = 0; + DIR *dir; + + snprintf(want, sizeof(want), "iou-wrk-%d", getpid()); + dir = opendir("/proc/self/task"); + if (!dir) + return -1; + while ((de = readdir(dir)) != NULL) { + char path[300], comm[64]; + cpu_set_t set; + ssize_t len; + int fd; + + if (de->d_name[0] == '.') + continue; + snprintf(path, sizeof(path), "/proc/self/task/%s/comm", + de->d_name); + fd = open(path, O_RDONLY); + if (fd < 0) + continue; + len = read(fd, comm, sizeof(comm) - 1); + close(fd); + if (len <= 0) + continue; + comm[len] = '\0'; + comm[strcspn(comm, "\n")] = '\0'; + if (strcmp(comm, want)) + continue; + if (sched_getaffinity(atoi(de->d_name), sizeof(set), &set)) + continue; + nr++; + if (CPU_COUNT(&set) != 1 || !CPU_ISSET(cpu, &set)) { + if (report) + fprintf(stderr, "worker %s: on %d CPU(s)%s, " + "want CPU %d only\n", de->d_name, + CPU_COUNT(&set), CPU_ISSET(cpu, &set) ? + "" : " without it", cpu); + ret = -1; + } + } + closedir(dir); + return ret ? ret : nr; +} + +/* + * Queue an open of the fifo, forced to io-wq. With no writer it blocks + * there, so each one queued while the others wait needs a worker of its + * own. Wait for that worker to show up. + */ +static int park(struct io_uring *ring, int cpu) +{ + struct io_uring_sqe *sqe; + int i, before; + + before = check_workers(cpu, 0); + sqe = io_uring_get_sqe(ring); + io_uring_prep_openat(sqe, AT_FDCWD, fifo, O_RDONLY, 0); + sqe->flags |= IOSQE_ASYNC; + if (io_uring_submit(ring) != 1) + return T_EXIT_FAIL; + parked++; + for (i = 0; i < 100; i++) { + int nr = check_workers(cpu, 0); + + if (nr < 0 || nr > before) + break; + usleep(10000); + } + return T_EXIT_PASS; +} + +static void release(struct io_uring *ring) +{ + struct io_uring_cqe *cqe; + int fd, i; + + fd = open(fifo, O_WRONLY | O_NONBLOCK); + for (i = 0; i < parked; i++) { + if (io_uring_wait_cqe(ring, &cqe)) + break; + if (cqe->res >= 0) + close(cqe->res); + io_uring_cqe_seen(ring, cqe); + } + if (fd >= 0) + close(fd); +} + +/* + * Start a worker on cpu_old, then move the task to cpu_new: change the + * cpuset, or move to the other cgroup. The workers started after that, and + * the one cpuset moved, must all be on cpu_new. + */ +static int test(int move) +{ + struct io_uring ring; + int i, ret; + + if (join(cg_a)) + return T_EXIT_SKIP; + ret = io_uring_queue_init(8, &ring, 0); + if (ret) { + fprintf(stderr, "queue init: %d\n", ret); + return T_EXIT_FAIL; + } + ret = park(&ring, cpu_old); + if (ret) + goto out; + if (check_workers(cpu_old, 1) != 1) { + fprintf(stderr, "no worker on CPU %d\n", cpu_old); + ret = T_EXIT_FAIL; + goto out; + } + + if (move) + ret = join(cg_b); + else + ret = set_cpu(cg_a, cpu_new); + if (ret) { + fprintf(stderr, "moving to CPU %d: %d\n", cpu_new, ret); + ret = T_EXIT_FAIL; + goto out; + } + + for (i = 1; i < NR_PARKED; i++) { + ret = park(&ring, cpu_new); + if (ret) + goto out; + } + ret = check_workers(cpu_new, 1); + if (ret != NR_PARKED) { + if (ret >= 0) + fprintf(stderr, "%d workers, want %d\n", ret, + NR_PARKED); + ret = T_EXIT_FAIL; + } else { + ret = T_EXIT_PASS; + } +out: + release(&ring); + io_uring_queue_exit(&ring); + return ret; +} + +static int run(int move) +{ + int status; + pid_t pid; + + if (set_cpu(cg_a, cpu_old) || set_cpu(cg_b, cpu_new)) + return T_EXIT_SKIP; + /* a child of its own, so it gets an io-wq of its own */ + pid = fork(); + if (pid < 0) + return T_EXIT_FAIL; + if (!pid) + exit(test(move)); + if (waitpid(pid, &status, 0) < 0 || !WIFEXITED(status)) + return T_EXIT_FAIL; + return WEXITSTATUS(status); +} + +static int setup(void) +{ + char buf[256] = ""; + struct statfs sfs; + cpu_set_t set; + int fd, i; + ssize_t len; + + if (geteuid()) + return T_EXIT_SKIP; + if (statfs(CG_ROOT, &sfs) || sfs.f_type != CGROUP2_SUPER_MAGIC) + return T_EXIT_SKIP; + fd = open(CG_ROOT "/cgroup.controllers", O_RDONLY); + if (fd < 0) + return T_EXIT_SKIP; + len = read(fd, buf, sizeof(buf) - 1); + close(fd); + if (len <= 0 || !strstr(buf, "cpuset")) + return T_EXIT_SKIP; + if (write_str(CG_ROOT "/cgroup.subtree_control", "+cpuset")) + return T_EXIT_SKIP; + + if (sched_getaffinity(0, sizeof(set), &set)) + return T_EXIT_SKIP; + cpu_old = cpu_new = -1; + for (i = 0; i < CPU_SETSIZE; i++) { + if (!CPU_ISSET(i, &set)) + continue; + if (cpu_old < 0) { + cpu_old = i; + } else { + cpu_new = i; + break; + } + } + if (cpu_new < 0) + return T_EXIT_SKIP; + + snprintf(cg_a, sizeof(cg_a), CG_ROOT "/iowq-cpuset-a.%d", getpid()); + snprintf(cg_b, sizeof(cg_b), CG_ROOT "/iowq-cpuset-b.%d", getpid()); + if (mkdir(cg_a, 0755) || mkdir(cg_b, 0755)) + return T_EXIT_SKIP; + + strcpy(buf, "/tmp/iowq-cpuset.XXXXXX"); + if (!mkdtemp(buf)) + return T_EXIT_SKIP; + snprintf(fifo, sizeof(fifo), "%s/fifo", buf); + if (mkfifo(fifo, 0600)) + return T_EXIT_SKIP; + return T_EXIT_PASS; +} + +static void cleanup(void) +{ + int i; + + if (fifo[0]) { + unlink(fifo); + *strrchr(fifo, '/') = '\0'; + rmdir(fifo); + } + /* the workers of the children may take a moment to exit */ + for (i = 0; i < 100; i++) { + if (cg_a[0] && rmdir(cg_a) && errno == EBUSY) { + usleep(10000); + continue; + } + if (cg_b[0] && rmdir(cg_b) && errno == EBUSY) { + usleep(10000); + continue; + } + break; + } +} + +int main(int argc, char *argv[]) +{ + int ret; + + if (argc > 1) + return T_EXIT_SKIP; + + ret = setup(); + if (ret) + goto out; + + ret = run(0); + if (ret) { + if (ret != T_EXIT_SKIP) + fprintf(stderr, "cpuset change failed\n"); + goto out; + } + ret = run(1); + if (ret && ret != T_EXIT_SKIP) + fprintf(stderr, "cgroup move failed\n"); +out: + cleanup(); + return ret; +} base-commit: 67bc5dcc7dc31b81bc623074887e2a231ede3b54 -- 2.43.0