public inbox for [email protected]
 help / color / mirror / Atom feed
* [PATCH] io_uring: annotate sqd->thread access with data race in cancel path
@ 2025-01-11 10:59 Bui Quang Minh
  2025-01-11 12:02 ` Pavel Begunkov
  0 siblings, 1 reply; 6+ messages in thread
From: Bui Quang Minh @ 2025-01-11 10:59 UTC (permalink / raw)
  To: linux-kernel
  Cc: Bui Quang Minh, Jens Axboe, Pavel Begunkov, io-uring,
	syzbot+3c750be01dab672c513d, Li Zetao

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: [email protected]
Reported-by: Li Zetao <[email protected]>
Signed-off-by: Bui Quang Minh <[email protected]>
---
 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;
@@ -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);
+	WARN_ON_ONCE(sqd && data_race(sqd->thread) != current);
 
 	if (!current->io_uring)
 		return;
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2025-01-12 11:33 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
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
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

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox