Skip to content

Commit 59efbac

Browse files
axboeopsiff
authored andcommitted
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 <krisman@suse.de> Signed-off-by: Jens Axboe <axboe@kernel.dk> Conflicts: io_uring/rw.c (cherry picked from commit fc68fcd) Signed-off-by: Wentao Guan <guanwentao@uniontech.com>
1 parent 6371c03 commit 59efbac

4 files changed

Lines changed: 82 additions & 1 deletion

File tree

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
@@ -431,9 +431,17 @@ const struct io_issue_def io_issue_defs[] = {
431431
.prep = io_eopnotsupp_prep,
432432
#endif
433433
},
434+
[IORING_OP_READ_MULTISHOT] = {
435+
.needs_file = 1,
436+
.unbound_nonreg_file = 1,
437+
.pollin = 1,
438+
.buffer_select = 1,
439+
.audit_skip = 1,
440+
.prep = io_read_mshot_prep,
441+
.issue = io_read_mshot,
442+
},
434443
};
435444

436-
437445
const struct io_cold_def io_cold_defs[] = {
438446
[IORING_OP_NOP] = {
439447
.name = "NOP",
@@ -651,6 +659,9 @@ const struct io_cold_def io_cold_defs[] = {
651659
.fail = io_sendrecv_fail,
652660
#endif
653661
},
662+
[IORING_OP_READ_MULTISHOT] = {
663+
.name = "READ_MULTISHOT",
664+
},
654665
};
655666

656667
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
@@ -133,6 +133,22 @@ int io_prep_rw(struct io_kiocb *req, const struct io_uring_sqe *sqe)
133133
return 0;
134134
}
135135

136+
/*
137+
* Multishot read is prepared just like a normal read/write request, only
138+
* difference is that we set the MULTISHOT flag.
139+
*/
140+
int io_read_mshot_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
141+
{
142+
int ret;
143+
144+
ret = io_prep_rw(req, sqe);
145+
if (unlikely(ret))
146+
return ret;
147+
148+
req->flags |= REQ_F_APOLL_MULTISHOT;
149+
return 0;
150+
}
151+
136152
void io_readv_writev_cleanup(struct io_kiocb *req)
137153
{
138154
struct io_async_rw *io = req->async_data;
@@ -890,6 +906,57 @@ int io_read(struct io_kiocb *req, unsigned int issue_flags)
890906
return ret;
891907
}
892908

909+
int io_read_mshot(struct io_kiocb *req, unsigned int issue_flags)
910+
{
911+
unsigned int cflags = 0;
912+
int ret;
913+
914+
/*
915+
* Multishot MUST be used on a pollable file
916+
*/
917+
if (!file_can_poll(req->file))
918+
return -EBADFD;
919+
920+
ret = __io_read(req, issue_flags);
921+
922+
/*
923+
* If we get -EAGAIN, recycle our buffer and just let normal poll
924+
* handling arm it.
925+
*/
926+
if (ret == -EAGAIN) {
927+
io_kbuf_recycle(req, issue_flags);
928+
return -EAGAIN;
929+
}
930+
931+
/*
932+
* Any successful return value will keep the multishot read armed.
933+
*/
934+
if (ret > 0) {
935+
/*
936+
* Put our buffer and post a CQE. If we fail to post a CQE, then
937+
* jump to the termination path. This request is then done.
938+
*/
939+
cflags = io_put_kbuf(req, issue_flags);
940+
941+
if (io_fill_cqe_req_aux(req,
942+
issue_flags & IO_URING_F_COMPLETE_DEFER,
943+
ret, cflags | IORING_CQE_F_MORE)) {
944+
if (issue_flags & IO_URING_F_MULTISHOT)
945+
return IOU_ISSUE_SKIP_COMPLETE;
946+
return -EAGAIN;
947+
}
948+
}
949+
950+
/*
951+
* Either an error, or we've hit overflow posting the CQE. For any
952+
* multishot request, hitting overflow will terminate it.
953+
*/
954+
io_req_set_res(req, ret, cflags);
955+
if (issue_flags & IO_URING_F_MULTISHOT)
956+
return IOU_STOP_MULTISHOT;
957+
return IOU_OK;
958+
}
959+
893960
static bool io_kiocb_start_write(struct io_kiocb *req, struct kiocb *kiocb)
894961
{
895962
struct inode *inode;

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)