Add IORING_MOCK_CMD_POLL_WAKE to issue a controlled number of immediate wakeups and to prep another batch for the next poll() call. The next batch is issued after poll_wait() installs the waiter, allowing a test to inject a wakeup from inside a vfs_poll() recheck. Assisted-by: LLM Signed-off-by: Juanlu Herrero --- Commit 937d38dcacd6 ("io_uring/poll: avoid retry wakeup loss when dropping ownership") fixed a race where a poll wakeup could be lost between consuming IO_POLL_RETRY_FLAG and dropping request ownership, stalling the request until another wakeup arrived. Add a mock-file command that issues immediate wakeups and arms another batch for the next poll() call. This lets the liburing test trigger a wakeup from inside the vfs_poll() recheck and reproduce the race deterministically [1]. [1] https://github.com/axboe/liburing/compare/master...jlhe97:liburing:mock-poll-test --- include/uapi/linux/io_uring/mock_file.h | 9 +++++ io_uring/mock_file.c | 61 +++++++++++++++++++++++++++++++++ 2 files changed, 70 insertions(+) diff --git a/include/uapi/linux/io_uring/mock_file.h b/include/uapi/linux/io_uring/mock_file.h index debeee8e4527d..14518b8ecc363 100644 --- a/include/uapi/linux/io_uring/mock_file.h +++ b/include/uapi/linux/io_uring/mock_file.h @@ -9,6 +9,7 @@ enum { IORING_MOCK_FEAT_RW_NOWAIT, IORING_MOCK_FEAT_RW_ASYNC, IORING_MOCK_FEAT_POLL, + IORING_MOCK_FEAT_POLL_WAKE, IORING_MOCK_FEAT_END, }; @@ -38,6 +39,14 @@ enum { enum { IORING_MOCK_CMD_COPY_REGBUF, + IORING_MOCK_CMD_POLL_WAKE, +}; + +struct io_uring_mock_poll_wake { + __u32 events; + __u32 nr_wakeups; + __u32 nr_wakeups_on_poll; + __u32 __resv[5]; }; enum { diff --git a/io_uring/mock_file.c b/io_uring/mock_file.c index b318ed697998e..19f87a2567bb1 100644 --- a/io_uring/mock_file.c +++ b/io_uring/mock_file.c @@ -24,6 +24,9 @@ struct io_mock_file { u64 rw_delay_ns; bool pollable; struct wait_queue_head poll_wq; + spinlock_t poll_lock; + __poll_t poll_wake_events; + u32 poll_wake_count; }; #define IO_VALID_COPY_CMD_FLAGS IORING_MOCK_COPY_FROM @@ -90,11 +93,56 @@ static int io_cmd_copy_regbuf(struct io_uring_cmd *cmd, unsigned int issue_flags return ret ? ret : -EFAULT; } +#define IO_MOCK_MAX_WAKEUPS 1024 + +static int io_cmd_poll_wake(struct io_uring_cmd *cmd) +{ + const struct io_uring_sqe *sqe = cmd->sqe; + struct io_mock_file *mf = cmd->file->private_data; + struct io_uring_mock_poll_wake arg; + void __user *uarg; + u32 nr_wakeups; + + if (!mf->pollable) + return -EOPNOTSUPP; + if (sqe->ioprio || sqe->__pad1 || sqe->addr3 || sqe->file_index) + return -EINVAL; + if (READ_ONCE(sqe->len) != sizeof(arg)) + return -EINVAL; + + uarg = u64_to_user_ptr(READ_ONCE(sqe->addr)); + if (copy_from_user(&arg, uarg, sizeof(arg))) + return -EFAULT; + if (!arg.events || + arg.nr_wakeups > IO_MOCK_MAX_WAKEUPS || + arg.nr_wakeups_on_poll > IO_MOCK_MAX_WAKEUPS || + !mem_is_zero(arg.__resv, sizeof(arg.__resv))) + return -EINVAL; + + if (arg.nr_wakeups_on_poll) { + spin_lock(&mf->poll_lock); + if (mf->poll_wake_count) { + spin_unlock(&mf->poll_lock); + return -EBUSY; + } + mf->poll_wake_events = arg.events; + mf->poll_wake_count = arg.nr_wakeups_on_poll; + spin_unlock(&mf->poll_lock); + } + + nr_wakeups = arg.nr_wakeups; + while (nr_wakeups--) + wake_up_poll(&mf->poll_wq, arg.events); + return 0; +} + static int io_mock_cmd(struct io_uring_cmd *cmd, unsigned int issue_flags) { switch (cmd->cmd_op) { case IORING_MOCK_CMD_COPY_REGBUF: return io_cmd_copy_regbuf(cmd, issue_flags); + case IORING_MOCK_CMD_POLL_WAKE: + return io_cmd_poll_wake(cmd); } return -ENOTSUPP; } @@ -168,9 +216,21 @@ static loff_t io_mock_llseek(struct file *file, loff_t offset, int whence) static __poll_t io_mock_poll(struct file *file, struct poll_table_struct *pt) { struct io_mock_file *mf = file->private_data; + __poll_t wake_events = 0; + u32 nr_wakeups = 0; __poll_t mask = 0; poll_wait(file, &mf->poll_wq, pt); + spin_lock(&mf->poll_lock); + if (mf->poll_wake_count) { + nr_wakeups = mf->poll_wake_count; + wake_events = mf->poll_wake_events; + mf->poll_wake_count = 0; + } + spin_unlock(&mf->poll_lock); + + while (nr_wakeups--) + wake_up_poll(&mf->poll_wq, wake_events); mask |= EPOLLOUT | EPOLLWRNORM; mask |= EPOLLIN | EPOLLRDNORM; @@ -247,6 +307,7 @@ static int io_create_mock_file(struct io_uring_cmd *cmd, unsigned int issue_flag return -ENOMEM; init_waitqueue_head(&mf->poll_wq); + spin_lock_init(&mf->poll_lock); mf->size = mc.file_size; mf->rw_delay_ns = mc.rw_delay_ns; if (mc.flags & IORING_MOCK_CREATE_F_POLL) { --- base-commit: ecb6817b769fd7d71af2c9f446188649020b2e53 change-id: 20261020-mock-poll-kernel-3d2624fb7d28 Best regards, -- Juanlu Herrero