Skip to content

Commit 7cdaf58

Browse files
Xiaoguang Wangaxboe
authored andcommitted
io_uring: avoid whole io_wq_work copy for requests completed inline
If requests can be submitted and completed inline, we don't need to initialize whole io_wq_work in io_init_req(), which is an expensive operation, add a new 'REQ_F_WORK_INITIALIZED' to determine whether io_wq_work is initialized and add a helper io_req_init_async(), users must call io_req_init_async() for the first time touching any members of io_wq_work. I use /dev/nullb0 to evaluate performance improvement in my physical machine: modprobe null_blk nr_devices=1 completion_nsec=0 sudo taskset -c 60 fio -name=fiotest -filename=/dev/nullb0 -iodepth=128 -thread -rw=read -ioengine=io_uring -direct=1 -bs=4k -size=100G -numjobs=1 -time_based -runtime=120 before this patch: Run status group 0 (all jobs): READ: bw=724MiB/s (759MB/s), 724MiB/s-724MiB/s (759MB/s-759MB/s), io=84.8GiB (91.1GB), run=120001-120001msec With this patch: Run status group 0 (all jobs): READ: bw=761MiB/s (798MB/s), 761MiB/s-761MiB/s (798MB/s-798MB/s), io=89.2GiB (95.8GB), run=120001-120001msec About 5% improvement. Signed-off-by: Xiaoguang Wang <[email protected]> Signed-off-by: Jens Axboe <[email protected]>
1 parent c5b8562 commit 7cdaf58

File tree

2 files changed

+36
-9
lines changed

2 files changed

+36
-9
lines changed

fs/io-wq.h

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -93,11 +93,6 @@ struct io_wq_work {
9393
pid_t task_pid;
9494
};
9595

96-
#define INIT_IO_WORK(work) \
97-
do { \
98-
*(work) = (struct io_wq_work){}; \
99-
} while (0) \
100-
10196
static inline struct io_wq_work *wq_next_work(struct io_wq_work *work)
10297
{
10398
if (!work->list.next)

fs/io_uring.c

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -542,6 +542,7 @@ enum {
542542
REQ_F_BUFFER_SELECTED_BIT,
543543
REQ_F_NO_FILE_TABLE_BIT,
544544
REQ_F_QUEUE_TIMEOUT_BIT,
545+
REQ_F_WORK_INITIALIZED_BIT,
545546

546547
/* not a real bit, just to check we're not overflowing the space */
547548
__REQ_F_LAST_BIT,
@@ -599,6 +600,8 @@ enum {
599600
REQ_F_NO_FILE_TABLE = BIT(REQ_F_NO_FILE_TABLE_BIT),
600601
/* needs to queue linked timeout */
601602
REQ_F_QUEUE_TIMEOUT = BIT(REQ_F_QUEUE_TIMEOUT_BIT),
603+
/* io_wq_work is initialized */
604+
REQ_F_WORK_INITIALIZED = BIT(REQ_F_WORK_INITIALIZED_BIT),
602605
};
603606

604607
struct async_poll {
@@ -911,6 +914,19 @@ EXPORT_SYMBOL(io_uring_get_socket);
911914

912915
static void io_file_put_work(struct work_struct *work);
913916

917+
/*
918+
* Note: must call io_req_init_async() for the first time you
919+
* touch any members of io_wq_work.
920+
*/
921+
static inline void io_req_init_async(struct io_kiocb *req)
922+
{
923+
if (req->flags & REQ_F_WORK_INITIALIZED)
924+
return;
925+
926+
memset(&req->work, 0, sizeof(req->work));
927+
req->flags |= REQ_F_WORK_INITIALIZED;
928+
}
929+
914930
static inline bool io_async_submit(struct io_ring_ctx *ctx)
915931
{
916932
return ctx->flags & IORING_SETUP_SQPOLL;
@@ -1037,6 +1053,9 @@ static inline void io_req_work_grab_env(struct io_kiocb *req,
10371053

10381054
static inline void io_req_work_drop_env(struct io_kiocb *req)
10391055
{
1056+
if (!(req->flags & REQ_F_WORK_INITIALIZED))
1057+
return;
1058+
10401059
if (req->work.mm) {
10411060
mmdrop(req->work.mm);
10421061
req->work.mm = NULL;
@@ -2785,8 +2804,14 @@ static int __io_splice_prep(struct io_kiocb *req,
27852804
return ret;
27862805
req->flags |= REQ_F_NEED_CLEANUP;
27872806

2788-
if (!S_ISREG(file_inode(sp->file_in)->i_mode))
2807+
if (!S_ISREG(file_inode(sp->file_in)->i_mode)) {
2808+
/*
2809+
* Splice operation will be punted aync, and here need to
2810+
* modify io_wq_work.flags, so initialize io_wq_work firstly.
2811+
*/
2812+
io_req_init_async(req);
27892813
req->work.flags |= IO_WQ_WORK_UNBOUND;
2814+
}
27902815

27912816
return 0;
27922817
}
@@ -3372,8 +3397,10 @@ static int io_close_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
33723397
{
33733398
/*
33743399
* If we queue this for async, it must not be cancellable. That would
3375-
* leave the 'file' in an undeterminate state.
3400+
* leave the 'file' in an undeterminate state, and here need to modify
3401+
* io_wq_work.flags, so initialize io_wq_work firstly.
33763402
*/
3403+
io_req_init_async(req);
33773404
req->work.flags |= IO_WQ_WORK_NO_CANCEL;
33783405

33793406
if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
@@ -4851,6 +4878,8 @@ static int io_req_defer_prep(struct io_kiocb *req,
48514878
if (!sqe)
48524879
return 0;
48534880

4881+
io_req_init_async(req);
4882+
48544883
if (io_op_defs[req->opcode].file_table) {
48554884
ret = io_grab_files(req);
48564885
if (unlikely(ret))
@@ -5505,7 +5534,8 @@ static void __io_queue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe)
55055534
again:
55065535
linked_timeout = io_prep_linked_timeout(req);
55075536

5508-
if (req->work.creds && req->work.creds != current_cred()) {
5537+
if ((req->flags & REQ_F_WORK_INITIALIZED) && req->work.creds &&
5538+
req->work.creds != current_cred()) {
55095539
if (old_creds)
55105540
revert_creds(old_creds);
55115541
if (old_creds == req->work.creds)
@@ -5528,6 +5558,8 @@ static void __io_queue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe)
55285558
goto exit;
55295559
}
55305560
punt:
5561+
io_req_init_async(req);
5562+
55315563
if (io_op_defs[req->opcode].file_table) {
55325564
ret = io_grab_files(req);
55335565
if (ret)
@@ -5780,7 +5812,6 @@ static int io_init_req(struct io_ring_ctx *ctx, struct io_kiocb *req,
57805812
refcount_set(&req->refs, 2);
57815813
req->task = NULL;
57825814
req->result = 0;
5783-
INIT_IO_WORK(&req->work);
57845815

57855816
if (unlikely(req->opcode >= IORING_OP_LAST))
57865817
return -EINVAL;
@@ -5802,6 +5833,7 @@ static int io_init_req(struct io_ring_ctx *ctx, struct io_kiocb *req,
58025833

58035834
id = READ_ONCE(sqe->personality);
58045835
if (id) {
5836+
io_req_init_async(req);
58055837
req->work.creds = idr_find(&ctx->personality_idr, id);
58065838
if (unlikely(!req->work.creds))
58075839
return -EINVAL;

0 commit comments

Comments
 (0)