From: Pavel Begunkov <asml.silence@gmail.com>
To: Bui Quang Minh <minhquangbui99@gmail.com>, linux-kernel@vger.kernel.org
Cc: Jens Axboe <axboe@kernel.dk>,
io-uring@vger.kernel.org,
syzbot+3c750be01dab672c513d@syzkaller.appspotmail.com,
Li Zetao <lizetao1@huawei.com>
Subject: Re: [PATCH] io_uring: annotate sqd->thread access with data race in cancel path
Date: Sat, 11 Jan 2025 12:02:25 +0000 [thread overview]
Message-ID: <02961c66-19b2-4f55-b785-3c4a132a5da3@gmail.com> (raw)
In-Reply-To: <20250111105920.38083-1-minhquangbui99@gmail.com>
On 1/11/25 10:59, Bui Quang Minh wrote:
> The sqd->thread access in io_uring_cancel_generic is just for debug check
> so we can safely ignore the data race.
>
> The sqd->thread access in io_uring_try_cancel_requests is to check if the
> caller is the sq threadi with the check ctx->sq_data->thread == current. In
> case this is called in a task other than the sq thread, we expect the
> expression to be false. And in that case, the sq_data->thread read can race
> with the NULL write in the sq thread termination. However, the race will
> still make ctx->sq_data->thread == current be false, so we can safely
> ignore the data race.
>
> 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 | 15 ++++++++++++---
> 1 file changed, 12 insertions(+), 3 deletions(-)
>
> diff --git a/io_uring/io_uring.c b/io_uring/io_uring.c
> index ff691f37462c..b1a116620ae1 100644
> --- a/io_uring/io_uring.c
> +++ b/io_uring/io_uring.c
> @@ -3094,9 +3094,18 @@ static __cold bool io_uring_try_cancel_requests(struct io_ring_ctx *ctx,
> ret |= (cret != IO_WQ_CANCEL_NOTFOUND);
> }
>
> - /* SQPOLL thread does its own polling */
> + /*
> + * SQPOLL thread does its own polling
> + *
> + * We expect ctx->sq_data->thread == current to be false when
> + * this function is called on a task other than the sq thread.
> + * In that case, the sq_data->thread read can race with the
> + * NULL write in the sq thread termination. However, the race
> + * will still make ctx->sq_data->thread == current be false,
> + * so we can safely ignore the data race here.
> + */
> if ((!(ctx->flags & IORING_SETUP_SQPOLL) && cancel_all) ||
> - (ctx->sq_data && ctx->sq_data->thread == current)) {
> + (ctx->sq_data && data_race(ctx->sq_data->thread) == current)) {
> while (!wq_list_empty(&ctx->iopoll_list)) {
> io_iopoll_try_reap_events(ctx);
> ret = true;
data_race() is a hammer we don't want to use to just silence warnings,
it can hide real problems. The fact that it needs 6 lines of comments
to explain is also not a good sign.
Instead, you can pass a flag, i.e. io_uring_cancel_generic() will have
non zero sqd IFF it's the SQPOLL task.
> @@ -3142,7 +3151,7 @@ __cold void io_uring_cancel_generic(bool cancel_all, struct io_sq_data *sqd)
> s64 inflight;
> DEFINE_WAIT(wait);
>
> - WARN_ON_ONCE(sqd && sqd->thread != current);
It's not racing if it's the same thread, if it's not it'll trigger
the warning anyway, I don't think we care about this one.
> + WARN_ON_ONCE(sqd && data_race(sqd->thread) != current);
>
> if (!current->io_uring)
> return;
--
Pavel Begunkov
next prev parent reply other threads:[~2025-01-11 12:01 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-01-11 10:59 [PATCH] io_uring: annotate sqd->thread access with data race in cancel path Bui Quang Minh
2025-01-11 12:02 ` Pavel Begunkov [this message]
2025-01-11 13:57 ` Bui Quang Minh
2025-01-12 1:21 ` Pavel Begunkov
2025-01-12 9:36 ` Bui Quang Minh
2025-01-12 11:34 ` Pavel Begunkov
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=02961c66-19b2-4f55-b785-3c4a132a5da3@gmail.com \
--to=asml.silence@gmail.com \
--cc=axboe@kernel.dk \
--cc=io-uring@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=lizetao1@huawei.com \
--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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox