History

io_uring/rw: add support for IORING_OP_READ_MULTISHOT

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.

Jens Axboe committed 3 years ago

Commit fc68fcd1 parent d2d778f4 files · +82 −1Browse files

Unified · Split

4 files
  1. include/uapi/linux/io_uring.h +1 −0
  2. io_uring/opdef.c +12 −1
  3. io_uring/rw.c +67 −0
  4. io_uring/rw.h +2 −0
include/uapi/linux/io_uring.h+1−0
@@ -240,6 +240,7 @@ enum io_uring_op { · 239 unmodified lines
240 IORING_OP_URING_CMD,240 IORING_OP_URING_CMD,
241 IORING_OP_SEND_ZC,241 IORING_OP_SEND_ZC,
242 IORING_OP_SENDMSG_ZC,242 IORING_OP_SENDMSG_ZC,
243 IORING_OP_READ_MULTISHOT,
243 244
244 /* this goes last, obviously */245 /* this goes last, obviously */
245 IORING_OP_LAST,246 IORING_OP_LAST,
io_uring/opdef.c+12−1
@@ -430,9 +430,17 @@ const struct io_issue_def io_issue_defs[] = { · 429 unmodified lines
430 .prep = io_eopnotsupp_prep,430 .prep = io_eopnotsupp_prep,
431#endif431#endif
432 },432 },
433 [IORING_OP_READ_MULTISHOT] = {
434 .needs_file = 1,
435 .unbound_nonreg_file = 1,
436 .pollin = 1,
437 .buffer_select = 1,
438 .audit_skip = 1,
439 .prep = io_read_mshot_prep,
440 .issue = io_read_mshot,
441 },
433};442};
434 443
435
436const struct io_cold_def io_cold_defs[] = {444const struct io_cold_def io_cold_defs[] = {
437 [IORING_OP_NOP] = {445 [IORING_OP_NOP] = {
438 .name = "NOP",446 .name = "NOP",
@@ -650,6 +658,9 @@ const struct io_cold_def io_cold_defs[] = { · 211 unmodified lines
650 .fail = io_sendrecv_fail,658 .fail = io_sendrecv_fail,
651#endif659#endif
652 },660 },
661 [IORING_OP_READ_MULTISHOT] = {
662 .name = "READ_MULTISHOT",
663 },
653};664};
654 665
655const char *io_uring_get_opcode(u8 opcode)666const char *io_uring_get_opcode(u8 opcode)
io_uring/rw.c+67−0
@@ -123,6 +123,22 @@ int io_prep_rw(struct io_kiocb *req, const struct io_uring_sqe *sqe) · 122 unmodified lines
123 return 0;123 return 0;
124}124}
125 125
126/*
127 * Multishot read is prepared just like a normal read/write request, only
128 * difference is that we set the MULTISHOT flag.
129 */
130int io_read_mshot_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
131{
132 int ret;
133
134 ret = io_prep_rw(req, sqe);
135 if (unlikely(ret))
136 return ret;
137
138 req->flags |= REQ_F_APOLL_MULTISHOT;
139 return 0;
140}
141
126void io_readv_writev_cleanup(struct io_kiocb *req)142void io_readv_writev_cleanup(struct io_kiocb *req)
127{143{
128 struct io_async_rw *io = req->async_data;144 struct io_async_rw *io = req->async_data;
@@ -869,6 +885,57 @@ int io_read(struct io_kiocb *req, unsigned int issue_flags) · 740 unmodified lines
869 return ret;885 return ret;
870}886}
871 887
888int io_read_mshot(struct io_kiocb *req, unsigned int issue_flags)
889{
890 unsigned int cflags = 0;
891 int ret;
892
893 /*
894 * Multishot MUST be used on a pollable file
895 */
896 if (!file_can_poll(req->file))
897 return -EBADFD;
898
899 ret = __io_read(req, issue_flags);
900
901 /*
902 * If we get -EAGAIN, recycle our buffer and just let normal poll
903 * handling arm it.
904 */
905 if (ret == -EAGAIN) {
906 io_kbuf_recycle(req, issue_flags);
907 return -EAGAIN;
908 }
909
910 /*
911 * Any successful return value will keep the multishot read armed.
912 */
913 if (ret > 0) {
914 /*
915 * Put our buffer and post a CQE. If we fail to post a CQE, then
916 * jump to the termination path. This request is then done.
917 */
918 cflags = io_put_kbuf(req, issue_flags);
919
920 if (io_fill_cqe_req_aux(req,
921 issue_flags & IO_URING_F_COMPLETE_DEFER,
922 ret, cflags | IORING_CQE_F_MORE)) {
923 if (issue_flags & IO_URING_F_MULTISHOT)
924 return IOU_ISSUE_SKIP_COMPLETE;
925 return -EAGAIN;
926 }
927 }
928
929 /*
930 * Either an error, or we've hit overflow posting the CQE. For any
931 * multishot request, hitting overflow will terminate it.
932 */
933 io_req_set_res(req, ret, cflags);
934 if (issue_flags & IO_URING_F_MULTISHOT)
935 return IOU_STOP_MULTISHOT;
936 return IOU_OK;
937}
938
872int io_write(struct io_kiocb *req, unsigned int issue_flags)939int io_write(struct io_kiocb *req, unsigned int issue_flags)
873{940{
874 struct io_rw *rw = io_kiocb_to_cmd(req, struct io_rw);941 struct io_rw *rw = io_kiocb_to_cmd(req, struct io_rw);
io_uring/rw.h+2−0
@@ -23,3 +23,5 @@ int io_writev_prep_async(struct io_kiocb *req); · 22 unmodified lines
23void io_readv_writev_cleanup(struct io_kiocb *req);23void io_readv_writev_cleanup(struct io_kiocb *req);
24void io_rw_fail(struct io_kiocb *req);24void io_rw_fail(struct io_kiocb *req);
25void io_req_rw_complete(struct io_kiocb *req, struct io_tw_state *ts);25void io_req_rw_complete(struct io_kiocb *req, struct io_tw_state *ts);
26int io_read_mshot_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe);
27int io_read_mshot(struct io_kiocb *req, unsigned int issue_flags);

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t