From 82a697a50f8f32ec0aed8374b3fd4174b2902d5c Mon Sep 17 00:00:00 2001 From: Xiaoguang Wang Date: Tue, 17 Nov 2020 14:45:47 +0800 Subject: [PATCH 2225/2944] io_uring: only wake up sq thread while current task is in io worker context task #31449212 commit 07d9bdbd936a1d67735819535a3ad8cca278a157 upstream If IORING_SETUP_SQPOLL is enabled, sqes are either handled in sq thread task context or in io worker task context. If current task context is sq thread, we don't need to check whether should wake up sq thread. io_iopoll_req_issued() calls wq_has_sleeper(), which has smp_mb() memory barrier, before this patch, perf shows obvious overhead: Samples: 481K of event 'cycles', Event count (approx.): 299807382878 Overhead Comma Shared Object Symbol 3.69% :9630 [kernel.vmlinux] [k] io_issue_sqe With this patch, perf shows: Samples: 482K of event 'cycles', Event count (approx.): 299929547283 Overhead Comma Shared Object Symbol 0.70% :4015 [kernel.vmlinux] [k] io_issue_sqe Signed-off-by: Xiaoguang Wang Signed-off-by: Jens Axboe Reviewed-by: Joseph Qi --- fs/io_uring.c | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/fs/io_uring.c b/fs/io_uring.c index 0bc5c01..9e02614 100644 --- a/fs/io_uring.c +++ b/fs/io_uring.c @@ -2171,7 +2171,7 @@ static void io_complete_rw_iopoll(struct kiocb *kiocb, long res, long res2) * find it from a io_iopoll_getevents() thread before the issuer is done * accessing the kiocb cookie. */ -static void io_iopoll_req_issued(struct io_kiocb *req) +static void io_iopoll_req_issued(struct io_kiocb *req, bool in_async) { struct io_ring_ctx *ctx = req->ctx; @@ -2200,7 +2200,12 @@ static void io_iopoll_req_issued(struct io_kiocb *req) else list_add_tail(&req->list, &ctx->poll_list); - if ((ctx->flags & IORING_SETUP_SQPOLL) && + /* + * If IORING_SETUP_SQPOLL is enabled, sqes are either handled in sq thread + * task context or in io worker task context. If current task context is + * sq thread, we don't need to check whether should wake up sq thread. + */ + if (in_async && (ctx->flags & IORING_SETUP_SQPOLL) && wq_has_sleeper(ctx->sqo_wait)) wake_up(ctx->sqo_wait); } @@ -5782,7 +5787,7 @@ static int io_issue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe, if (in_async) mutex_lock(&ctx->uring_lock); - io_iopoll_req_issued(req); + io_iopoll_req_issued(req, in_async); if (in_async) mutex_unlock(&ctx->uring_lock); -- 1.8.3.1