From: lizetao <lizetao1@huawei.com>
To: Bui Quang Minh <minhquangbui99@gmail.com>,
"linux-kernel@vger.kernel.org" <linux-kernel@vger.kernel.org>
Cc: Jens Axboe <axboe@kernel.dk>,
Pavel Begunkov <asml.silence@gmail.com>,
"io-uring@vger.kernel.org" <io-uring@vger.kernel.org>,
"syzbot+3c750be01dab672c513d@syzkaller.appspotmail.com"
<syzbot+3c750be01dab672c513d@syzkaller.appspotmail.com>
Subject: RE: [PATCH] io_uring: simplify the SQPOLL thread check when cancelling requests
Date: Mon, 13 Jan 2025 07:49:05 +0000 [thread overview]
Message-ID: <e9e5bda7706244598c977c8cfaeaca38@huawei.com> (raw)
In-Reply-To: <20250112143358.49671-1-minhquangbui99@gmail.com>
> -----Original Message-----
> From: Bui Quang Minh <minhquangbui99@gmail.com>
> Sent: Sunday, January 12, 2025 10:34 PM
> To: linux-kernel@vger.kernel.org
> Cc: Bui Quang Minh <minhquangbui99@gmail.com>; Jens Axboe
> <axboe@kernel.dk>; Pavel Begunkov <asml.silence@gmail.com>; io-
> uring@vger.kernel.org;
> syzbot+3c750be01dab672c513d@syzkaller.appspotmail.com; lizetao
> <lizetao1@huawei.com>
> Subject: [PATCH] io_uring: simplify the SQPOLL thread check when cancelling
> requests
>
> In io_uring_try_cancel_requests, we check whether sq_data->thread ==
> current to determine if the function is called by the SQPOLL thread to do iopoll
> when IORING_SETUP_SQPOLL is set. This check can race with the SQPOLL
> thread termination.
>
> io_uring_cancel_generic is used in 2 places: io_uring_cancel_generic and
> io_ring_exit_work. In io_uring_cancel_generic, we have the information
> whether the current is SQPOLL thread already. In io_ring_exit_work, in case
> the SQPOLL thread reaches this path, we don't need to iopoll and leave that for
> io_uring_cancel_generic to handle.
>
> So to avoid the racy check, this commit adds a boolean flag to
> io_uring_try_cancel_requests to determine if we need to do iopoll inside the
> function and only sets this flag in io_uring_cancel_generic when the current is
> SQPOLL thread.
>
> Reported-by: syzbot+3c750be01dab672c513d@syzkaller.appspotmail.com
> Reported-by: Li Zetao <lizetao1@huawei.com>
> Signed-off-by: Bui Quang Minh <minhquangbui99@gmail.com>
> ---
> io_uring/io_uring.c | 21 +++++++++++++++------
> 1 file changed, 15 insertions(+), 6 deletions(-)
>
> diff --git a/io_uring/io_uring.c b/io_uring/io_uring.c index
> ff691f37462c..f28ea1254143 100644
> --- a/io_uring/io_uring.c
> +++ b/io_uring/io_uring.c
> @@ -143,7 +143,8 @@ struct io_defer_entry {
>
> static bool io_uring_try_cancel_requests(struct io_ring_ctx *ctx,
> struct io_uring_task *tctx,
> - bool cancel_all);
> + bool cancel_all,
> + bool force_iopoll);
>
> static void io_queue_sqe(struct io_kiocb *req);
>
> @@ -2898,7 +2899,12 @@ static __cold void io_ring_exit_work(struct
> work_struct *work)
> if (ctx->flags & IORING_SETUP_DEFER_TASKRUN)
> io_move_task_work_from_local(ctx);
>
> - while (io_uring_try_cancel_requests(ctx, NULL, true))
> + /*
> + * Even if SQPOLL thread reaches this path, don't force
> + * iopoll here, let the io_uring_cancel_generic handle
> + * it.
> + */
> + while (io_uring_try_cancel_requests(ctx, NULL, true, false))
> cond_resched();
>
> if (ctx->sq_data) {
> @@ -3066,7 +3072,8 @@ static __cold bool io_uring_try_cancel_iowq(struct
> io_ring_ctx *ctx)
>
> static __cold bool io_uring_try_cancel_requests(struct io_ring_ctx *ctx,
> struct io_uring_task *tctx,
> - bool cancel_all)
> + bool cancel_all,
> + bool force_iopoll)
> {
> struct io_task_cancel cancel = { .tctx = tctx, .all = cancel_all, };
> enum io_wq_cancel cret;
> @@ -3096,7 +3103,7 @@ static __cold bool
> io_uring_try_cancel_requests(struct io_ring_ctx *ctx,
>
> /* SQPOLL thread does its own polling */
> if ((!(ctx->flags & IORING_SETUP_SQPOLL) && cancel_all) ||
> - (ctx->sq_data && ctx->sq_data->thread == current)) {
> + force_iopoll) {
> while (!wq_list_empty(&ctx->iopoll_list)) {
> io_iopoll_try_reap_events(ctx);
> ret = true;
> @@ -3169,13 +3176,15 @@ __cold void io_uring_cancel_generic(bool
> cancel_all, struct io_sq_data *sqd)
> continue;
> loop |= io_uring_try_cancel_requests(node-
> >ctx,
> current->io_uring,
> - cancel_all);
> + cancel_all,
> + false);
> }
> } else {
> list_for_each_entry(ctx, &sqd->ctx_list, sqd_list)
> loop |= io_uring_try_cancel_requests(ctx,
> current-
> >io_uring,
> - cancel_all);
> + cancel_all,
> + true);
> }
>
> if (loop) {
> --
> 2.43.0
>
Reviewed-by: Li Zetao<lizetao1@huawei.com>
---
Li Zetao
prev parent reply other threads:[~2025-01-13 7:49 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-01-12 14:33 [PATCH] io_uring: simplify the SQPOLL thread check when cancelling requests Bui Quang Minh
2025-01-12 15:45 ` lizetao
2025-01-12 16:14 ` Bui Quang Minh
2025-01-12 21:15 ` Pavel Begunkov
2025-01-13 4:40 ` lizetao
2025-01-13 15:33 ` Bui Quang Minh
2025-01-13 7:49 ` lizetao [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=e9e5bda7706244598c977c8cfaeaca38@huawei.com \
--to=lizetao1@huawei.com \
--cc=asml.silence@gmail.com \
--cc=axboe@kernel.dk \
--cc=io-uring@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=minhquangbui99@gmail.com \
--cc=syzbot+3c750be01dab672c513d@syzkaller.appspotmail.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.