An epoll waiter can hang with completions pending when polling a disabled SINGLE_ISSUER ring races with IORING_REGISTER_ENABLE_RINGS: CPU0 CPU1 ---- ---- io_activate_pollwq() submitter_task == NULL submitter_task = current wq_has_sleeper() == false poll_wait() Neither side retries the activation. The activation flag remains clear, so completion and deferred task work paths skip poll_wq wakeups. Cure this by registering the poll waiter before trying to activate poll_wq. The full barrier in poll_wait() pairs with the barrier in wq_has_sleeper(), which ensures that either the poller observes submitter_task or the ring enable path observes the waiter. Tested in a 2-vCPU x86-64 KVM guest with a reproducer that races EPOLL_CTL_ADD on a disabled ring against IORING_REGISTER_ENABLE_RINGS. v7.4-rc5 missed the wakeup in each of three runs, at iterations 177, 505 and 26826. With this patch, three runs of 100000 iterations passed. The bug was found with the help of an LLM, which was also used to write the fix, the changelog and the tests. Fixes: bca39f390585 ("io_uring: add lazy poll_wq activation") Cc: stable@vger.kernel.org Signed-off-by: Andrea Parri --- io_register_enable_rings() stores ->submitter_task and io_activate_pollwq() loads it without a common lock. Commit bcd4c95737d1 ("io_uring/msg_ring: drop unnecessary submitter_task checks") made the store plain because the msg_ring readers check IORING_SETUP_R_DISABLED first; io_activate_pollwq() does not. The barriers in poll_wait() and wq_has_sleeper() provide the ordering, but the accesses themselves stay plain, as they were before this patch, to keep the fix and its stable backport small. I can send a follow-up that marks them with WRITE_ONCE() and READ_ONCE() if you want it. Reproducer below, for x86-64 (it spins on the pause instruction). It needs liburing, virtme-ng and QEMU with KVM. Build the kernel with: vng --kconfig --skip-modules \ --configitem CONFIG_IO_URING=y \ --configitem CONFIG_EPOLL=y make -j$(nproc) LOCALVERSION=-poll-fixed bzImage Save the program below as /tmp/poll-activation.c, then build and run it in a guest: cc -O2 -Wall -Wextra -Werror -pthread /tmp/poll-activation.c \ -luring -o /tmp/poll-activation vng --skip-modules --run ./arch/x86/boot/bzImage \ --cpus 2 --memory 2G --rodir=/tmp --append panic=-1 \ --exec 'uname -r; /tmp/poll-activation 100000' Each iteration, a poller thread on CPU 1 adds the disabled ring to an epoll set while the main thread on CPU 0 enables the ring after a delay that varies with the iteration. The main thread then submits a NOP. Once the NOP is submitted, the poller waits up to 250 ms in epoll_wait() and the main thread checks that it reported EPOLLIN. Without this patch the program printed: PASS: watch before enable and deferred work after enable FAIL: missed EPOLLIN at iteration 177 with a CQE pending With it: 7.4.0-rc5-poll-fixed PASS: watch before enable and deferred work after enable PASS: 100000 concurrent epoll add / ring enable iterations /* SPDX-License-Identifier: GPL-2.0-only */ #define _GNU_SOURCE #include #include #include #include #include #include #include #include #include #include #include static pthread_barrier_t barrier; static struct io_uring ring; static int epfd; static int ready; static int stop; static struct epoll_event in = { .events = EPOLLIN }; static void check(const char *op, int ret) { if (ret < 0) { fprintf(stderr, "%s: %s\n", op, strerror(-ret)); exit(2); } } static void expect(const char *op, int actual, int expected) { if (actual != expected) { fprintf(stderr, "%s: got %d, expected %d\n", op, actual, expected); exit(2); } } static void pin_cpu(int cpu) { cpu_set_t set; CPU_ZERO(&set); CPU_SET(cpu, &set); check("pthread_setaffinity_np", -pthread_setaffinity_np(pthread_self(), sizeof(set), &set)); } static void add_watch(void) { if (epoll_ctl(epfd, EPOLL_CTL_ADD, ring.ring_fd, &in)) check("EPOLL_CTL_ADD", -errno); } static int wait_for_ready(int timeout) { struct epoll_event event; int ret = epoll_wait(epfd, &event, 1, timeout); if (ret < 0) check("epoll_wait", -errno); if (ret) expect("epoll readiness", event.events, EPOLLIN); return ret; } static void *poller(void *arg) { (void)arg; pin_cpu(1); for (;;) { pthread_barrier_wait(&barrier); if (stop) return NULL; add_watch(); pthread_barrier_wait(&barrier); /* Start the timeout only once the CQE exists. */ pthread_barrier_wait(&barrier); ready = wait_for_ready(250); pthread_barrier_wait(&barrier); } } static void setup(void) { check("io_uring_queue_init", io_uring_queue_init(8, &ring, IORING_SETUP_R_DISABLED | IORING_SETUP_SINGLE_ISSUER | IORING_SETUP_DEFER_TASKRUN)); epfd = epoll_create1(EPOLL_CLOEXEC); if (epfd < 0) check("epoll_create1", -errno); } static void teardown(void) { close(epfd); io_uring_queue_exit(&ring); } static void submit_nop(void) { struct io_uring_sqe *sqe = io_uring_get_sqe(&ring); if (!sqe) exit(2); io_uring_prep_nop(sqe); expect("io_uring_submit", io_uring_submit(&ring), 1); } static void reap(int expected) { struct io_uring_cqe *cqe; check("io_uring_peek_cqe", io_uring_peek_cqe(&ring, &cqe)); expect("CQE result", cqe->res, expected); io_uring_cqe_seen(&ring, cqe); } static void smoke_tests(void) { int fd = eventfd(0, EFD_CLOEXEC); struct io_uring_sqe *sqe; if (fd < 0) check("eventfd", -errno); setup(); add_watch(); check("io_uring_enable_rings", io_uring_enable_rings(&ring)); submit_nop(); expect("watch before enable", wait_for_ready(500), 1); reap(0); teardown(); setup(); check("io_uring_enable_rings", io_uring_enable_rings(&ring)); add_watch(); sqe = io_uring_get_sqe(&ring); if (!sqe) exit(2); io_uring_prep_poll_add(sqe, fd, POLLIN); expect("submit poll request", io_uring_submit(&ring), 1); if (eventfd_write(fd, 1)) check("eventfd_write", -errno); expect("deferred work readiness", wait_for_ready(500), 1); check("io_uring_get_events", io_uring_get_events(&ring)); reap(POLLIN); teardown(); close(fd); puts("PASS: watch before enable and deferred work after enable"); } int main(int argc, char **argv) { int iterations = argc > 1 ? atoi(argv[1]) : 20000; pthread_t thread; int i; setlinebuf(stdout); smoke_tests(); pin_cpu(0); check("pthread_barrier_init", -pthread_barrier_init(&barrier, NULL, 2)); check("pthread_create", -pthread_create(&thread, NULL, poller, NULL)); for (i = 0; i < iterations; i++) { setup(); pthread_barrier_wait(&barrier); for (int delay = 0; delay < i % 256; delay++) __asm__ volatile ("pause" ::: "memory"); check("io_uring_enable_rings", io_uring_enable_rings(&ring)); pthread_barrier_wait(&barrier); submit_nop(); pthread_barrier_wait(&barrier); pthread_barrier_wait(&barrier); if (!ready) { if (epoll_ctl(epfd, EPOLL_CTL_MOD, ring.ring_fd, &in)) check("EPOLL_CTL_MOD", -errno); expect("readiness after EPOLL_CTL_MOD", wait_for_ready(0), 1); reap(0); printf("FAIL: missed EPOLLIN at iteration %d with a CQE pending\n", i + 1); break; } reap(0); teardown(); } stop = 1; pthread_barrier_wait(&barrier); check("pthread_join", -pthread_join(thread, NULL)); if (i != iterations) { teardown(); return 1; } printf("PASS: %d concurrent epoll add / ring enable iterations\n", i); return 0; } io_uring/io_uring.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/io_uring/io_uring.c b/io_uring/io_uring.c index d8491f86e4808..a3caadbeb384b 100644 --- a/io_uring/io_uring.c +++ b/io_uring/io_uring.c @@ -2423,13 +2423,14 @@ static __poll_t io_uring_poll(struct file *file, poll_table *wait) struct io_ring_ctx *ctx = file->private_data; __poll_t mask = 0; - if (unlikely(!(data_race(ctx->int_flags) & IO_RING_F_POLL_ACTIVATED))) - io_activate_pollwq(ctx); /* - * provides mb() which pairs with barrier from wq_has_sleeper - * call in io_commit_cqring + * The poll_wait() barrier pairs with wq_has_sleeper() on the CQ + * wakeup and ring enable paths. */ poll_wait(file, &ctx->poll_wq, wait); + /* A stale flag only causes a redundant activation attempt. */ + if (unlikely(!(data_race(ctx->int_flags) & IO_RING_F_POLL_ACTIVATED))) + io_activate_pollwq(ctx); rcu_read_lock(); -- 2.53.0