Skip to content

Commit fc68fcd

Browse files
committed
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. Reviewed-by: Gabriel Krisman Bertazi <[email protected]> Signed-off-by: Jens Axboe <[email protected]>
1 parent d2d778f commit fc68fcd

File tree

4 files changed

+82
-1
lines changed

4 files changed

+82
-1
lines changed

include/uapi/linux/io_uring.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,7 @@ enum io_uring_op {
240240
IORING_OP_URING_CMD,
241241
IORING_OP_SEND_ZC,
242242
IORING_OP_SENDMSG_ZC,
243+
IORING_OP_READ_MULTISHOT,
243244

244245
/* this goes last, obviously */
245246
IORING_OP_LAST,

io_uring/opdef.c

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -430,9 +430,17 @@ const struct io_issue_def io_issue_defs[] = {
430430
.prep = io_eopnotsupp_prep,
431431
#endif
432432
},
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+
},
433442
};
434443

435-
436444
const struct io_cold_def io_cold_defs[] = {
437445
[IORING_OP_NOP] = {
438446
.name = "NOP",
@@ -650,6 +658,9 @@ const struct io_cold_def io_cold_defs[] = {
650658
.fail = io_sendrecv_fail,
651659
#endif
652660
},
661+
[IORING_OP_READ_MULTISHOT] = {
662+
.name = "READ_MULTISHOT",
663+
},
653664
};
654665

655666
const char *io_uring_get_opcode(u8 opcode)

io_uring/rw.c

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,22 @@ int io_prep_rw(struct io_kiocb *req, const struct io_uring_sqe *sqe)
123123
return 0;
124124
}
125125

126+
/*
127+
* Multishot read is prepared just like a normal read/write request, only
128+
* difference is that we set the MULTISHOT flag.
129+
*/
130+
int 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+
126142
void io_readv_writev_cleanup(struct io_kiocb *req)
127143
{
128144
struct io_async_rw *io = req->async_data;
@@ -869,6 +885,57 @@ int io_read(struct io_kiocb *req, unsigned int issue_flags)
869885
return ret;
870886
}
871887

888+
int 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+
872939
int io_write(struct io_kiocb *req, unsigned int issue_flags)
873940
{
874941
struct io_rw *rw = io_kiocb_to_cmd(req, struct io_rw);

io_uring/rw.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,5 @@ int io_writev_prep_async(struct io_kiocb *req);
2323
void io_readv_writev_cleanup(struct io_kiocb *req);
2424
void io_rw_fail(struct io_kiocb *req);
2525
void io_req_rw_complete(struct io_kiocb *req, struct io_tw_state *ts);
26+
int io_read_mshot_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe);
27+
int io_read_mshot(struct io_kiocb *req, unsigned int issue_flags);

0 commit comments

Comments
 (0)