io_uring/rw: add support for IORING_OP_READ_MULTISHOT
authorJens Axboe <axboe@kernel.dk>
Mon, 11 Sep 2023 19:35:42 +0000 (13:35 -0600)
committerJens Axboe <axboe@kernel.dk>
Thu, 21 Sep 2023 18:02:30 +0000 (12:02 -0600)
This behaves like IORING_OP_READ, except:

1) It only supports pollable files (eg pipes, sockets, etc). Note that
   for sockets, you probably want to use recv/recvmsg with multishot
   instead.

2) It supports multishot mode, meaning it will repeatedly trigger a
   read and fill a buffer when data is available. This allows similar
   use to recv/recvmsg but on non-sockets, where a single request will
   repeatedly post a CQE whenever data is read from it.

3) Because of #2, it must be used with provided buffers. This is
   uniformly true across any request type that supports multishot and
   transfers data, with the reason being that it's obviously not
   possible to pass in a single buffer for the data, as multiple reads
   may very well trigger before an application has a chance to process
   previous CQEs and the data passed from them.

Reviewed-by: Gabriel Krisman Bertazi <krisman@suse.de>
Signed-off-by: Jens Axboe <axboe@kernel.dk>
include/uapi/linux/io_uring.h
io_uring/opdef.c
io_uring/rw.c
io_uring/rw.h

index 8e61f8b7c2ced1221179e3ada854f4bbba73f36f..d127948b0d8a8a5c21b02154c95ef5d336154764 100644 (file)
@@ -240,6 +240,7 @@ enum io_uring_op {
        IORING_OP_URING_CMD,
        IORING_OP_SEND_ZC,
        IORING_OP_SENDMSG_ZC,
+       IORING_OP_READ_MULTISHOT,
 
        /* this goes last, obviously */
        IORING_OP_LAST,
index f4090406550070c520f1d55c1bc19a62f0172f37..a3fb1f9b3998e40a75d85db7eea5504347219a38 100644 (file)
@@ -430,9 +430,17 @@ const struct io_issue_def io_issue_defs[] = {
                .prep                   = io_eopnotsupp_prep,
 #endif
        },
+       [IORING_OP_READ_MULTISHOT] = {
+               .needs_file             = 1,
+               .unbound_nonreg_file    = 1,
+               .pollin                 = 1,
+               .buffer_select          = 1,
+               .audit_skip             = 1,
+               .prep                   = io_read_mshot_prep,
+               .issue                  = io_read_mshot,
+       },
 };
 
-
 const struct io_cold_def io_cold_defs[] = {
        [IORING_OP_NOP] = {
                .name                   = "NOP",
@@ -650,6 +658,9 @@ const struct io_cold_def io_cold_defs[] = {
                .fail                   = io_sendrecv_fail,
 #endif
        },
+       [IORING_OP_READ_MULTISHOT] = {
+               .name                   = "READ_MULTISHOT",
+       },
 };
 
 const char *io_uring_get_opcode(u8 opcode)
index 83ae911c2868cafa7a996ea54ae7bd298205c5c5..ec0cc38ea6824a2a8b6f29841cd08ead84137e6a 100644 (file)
@@ -123,6 +123,22 @@ int io_prep_rw(struct io_kiocb *req, const struct io_uring_sqe *sqe)
        return 0;
 }
 
+/*
+ * Multishot read is prepared just like a normal read/write request, only
+ * difference is that we set the MULTISHOT flag.
+ */
+int io_read_mshot_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
+{
+       int ret;
+
+       ret = io_prep_rw(req, sqe);
+       if (unlikely(ret))
+               return ret;
+
+       req->flags |= REQ_F_APOLL_MULTISHOT;
+       return 0;
+}
+
 void io_readv_writev_cleanup(struct io_kiocb *req)
 {
        struct io_async_rw *io = req->async_data;
@@ -869,6 +885,57 @@ int io_read(struct io_kiocb *req, unsigned int issue_flags)
        return ret;
 }
 
+int io_read_mshot(struct io_kiocb *req, unsigned int issue_flags)
+{
+       unsigned int cflags = 0;
+       int ret;
+
+       /*
+        * Multishot MUST be used on a pollable file
+        */
+       if (!file_can_poll(req->file))
+               return -EBADFD;
+
+       ret = __io_read(req, issue_flags);
+
+       /*
+        * If we get -EAGAIN, recycle our buffer and just let normal poll
+        * handling arm it.
+        */
+       if (ret == -EAGAIN) {
+               io_kbuf_recycle(req, issue_flags);
+               return -EAGAIN;
+       }
+
+       /*
+        * Any successful return value will keep the multishot read armed.
+        */
+       if (ret > 0) {
+               /*
+                * Put our buffer and post a CQE. If we fail to post a CQE, then
+                * jump to the termination path. This request is then done.
+                */
+               cflags = io_put_kbuf(req, issue_flags);
+
+               if (io_fill_cqe_req_aux(req,
+                                       issue_flags & IO_URING_F_COMPLETE_DEFER,
+                                       ret, cflags | IORING_CQE_F_MORE)) {
+                       if (issue_flags & IO_URING_F_MULTISHOT)
+                               return IOU_ISSUE_SKIP_COMPLETE;
+                       return -EAGAIN;
+               }
+       }
+
+       /*
+        * Either an error, or we've hit overflow posting the CQE. For any
+        * multishot request, hitting overflow will terminate it.
+        */
+       io_req_set_res(req, ret, cflags);
+       if (issue_flags & IO_URING_F_MULTISHOT)
+               return IOU_STOP_MULTISHOT;
+       return IOU_OK;
+}
+
 int io_write(struct io_kiocb *req, unsigned int issue_flags)
 {
        struct io_rw *rw = io_kiocb_to_cmd(req, struct io_rw);
index 4b89f9659366a0ead3af7b749edd443a36289039..c5aed03d42a4d14182a57318ad9fc7cd8cc0e829 100644 (file)
@@ -23,3 +23,5 @@ int io_writev_prep_async(struct io_kiocb *req);
 void io_readv_writev_cleanup(struct io_kiocb *req);
 void io_rw_fail(struct io_kiocb *req);
 void io_req_rw_complete(struct io_kiocb *req, struct io_tw_state *ts);
+int io_read_mshot_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe);
+int io_read_mshot(struct io_kiocb *req, unsigned int issue_flags);