]> git.ipfire.org Git - thirdparty/linux.git/commitdiff
io_uring/cmd: allow multishot polled commands
authorPavel Begunkov <asml.silence@gmail.com>
Mon, 16 Jun 2025 09:46:27 +0000 (10:46 +0100)
committerJens Axboe <axboe@kernel.dk>
Mon, 23 Jun 2025 15:00:12 +0000 (09:00 -0600)
Some commands like timestamping in the next patch can make use of
multishot polling, i.e. REQ_F_APOLL_MULTISHOT. Add support for that,
which is condensed in a single helper called io_cmd_poll_multishot().

The user who wants to continue with a request in a multishot mode must
call the function, and only if it returns 0 the user is free to proceed.
Apart from normal terminal errors, it can also end up with -EIOCBQUEUED,
in which case the user must forward it to the core io_uring. It's
forbidden to use task work while the request is executing in a multishot
mode.

The API is not foolproof, hence it's not exported to modules nor exposed
in public headers.

Signed-off-by: Pavel Begunkov <asml.silence@gmail.com>
Link: https://lore.kernel.org/r/bcf97c31659662c72b69fc8fcdf2a88cfc16e430.1750065793.git.asml.silence@gmail.com
Signed-off-by: Jens Axboe <axboe@kernel.dk>
io_uring/uring_cmd.c
io_uring/uring_cmd.h

index 9ad0ea5398c2e9234af996907c1cb9cd059e728b..02cec6231831965239225e185970c6207af0d053 100644 (file)
@@ -12,6 +12,7 @@
 #include "alloc_cache.h"
 #include "rsrc.h"
 #include "uring_cmd.h"
+#include "poll.h"
 
 void io_cmd_cache_free(const void *entry)
 {
@@ -136,6 +137,9 @@ void __io_uring_cmd_do_in_task(struct io_uring_cmd *ioucmd,
 {
        struct io_kiocb *req = cmd_to_io_kiocb(ioucmd);
 
+       if (WARN_ON_ONCE(req->flags & REQ_F_APOLL_MULTISHOT))
+               return;
+
        ioucmd->task_work_cb = task_work_cb;
        req->io_task_work.func = io_uring_cmd_work;
        __io_req_task_work_add(req, flags);
@@ -158,6 +162,9 @@ void io_uring_cmd_done(struct io_uring_cmd *ioucmd, ssize_t ret, u64 res2,
 {
        struct io_kiocb *req = cmd_to_io_kiocb(ioucmd);
 
+       if (WARN_ON_ONCE(req->flags & REQ_F_APOLL_MULTISHOT))
+               return;
+
        io_uring_cmd_del_cancelable(ioucmd, issue_flags);
 
        if (ret < 0)
@@ -305,3 +312,19 @@ void io_uring_cmd_issue_blocking(struct io_uring_cmd *ioucmd)
 
        io_req_queue_iowq(req);
 }
+
+int io_cmd_poll_multishot(struct io_uring_cmd *cmd,
+                         unsigned int issue_flags, __poll_t mask)
+{
+       struct io_kiocb *req = cmd_to_io_kiocb(cmd);
+       int ret;
+
+       if (likely(req->flags & REQ_F_APOLL_MULTISHOT))
+               return 0;
+
+       req->flags |= REQ_F_APOLL_MULTISHOT;
+       mask &= ~EPOLLONESHOT;
+
+       ret = io_arm_apoll(req, issue_flags, mask);
+       return ret == IO_APOLL_OK ? -EIOCBQUEUED : -ECANCELED;
+}
index a6dad47afc6bfecc9613835c9441bf1fd642c460..50a6ccb831dfdb1cf76425afaa6e2dab123de1bd 100644 (file)
@@ -18,3 +18,6 @@ bool io_uring_try_cancel_uring_cmd(struct io_ring_ctx *ctx,
                                   struct io_uring_task *tctx, bool cancel_all);
 
 void io_cmd_cache_free(const void *entry);
+
+int io_cmd_poll_multishot(struct io_uring_cmd *cmd,
+                         unsigned int issue_flags, __poll_t mask);