io_uring: annotate sqd->thread access with data race in cancel path
PATCH 1/1 io_uring: annotate sqd->thread access with data race in cancel path
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.
--- io_uring/io_uring.c | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-)
io_uring/io_uring.c +12 -3
@@ -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 ;
Signature · 1 line
2.43.0
Quoted 33 lines · Bui Quang Minh
The
sqd->threadaccess inio_uring_cancel_genericis just for debug check so we can safely ignore the data race.The
sqd->threadaccess inio_uring_try_cancel_requestsis to check if the caller is the sq threadi with the checkctx->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, thesq_data->threadread can race with the NULL write in the sq thread termination. However, the race will still makectx->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(-)io_uring/io_uring.c +6
-1@@ -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
io_uring/io_uring.c static __cold bool io_uring_try_cancel_requests(struct io_ring_ctx *ctx, · line 3111
Quoted 10 lines
io_uring/io_uring.c +5
-1+* 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.
io_uring/io_uring.c __cold void io_uring_cancel_generic(bool cancel_all, struct io_sq_data *sqd) · line 3145
Quoted 5 lines
io_uring/io_uring.c +0
-1@@ -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.
Quoted 4 lines
io_uring/io_uring.c +1
-0+WARN_ON_ONCE (sqd &&data_race (sqd->thread) != current);if (!current->io_uring)return ;
Signature · 1 line
Pavel Begunkov
Quoted 49 lines · Pavel Begunkov
Quoted 37 lines · Bui Quang Minh
The
sqd->threadaccess inio_uring_cancel_genericis just for debug check so we can safely ignore the data race.The
sqd->threadaccess inio_uring_try_cancel_requestsis to check if the caller is the sq threadi with the checkctx->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, thesq_data->threadread can race with the NULL write in the sq thread termination. However, the race will still makectx->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(-)io_uring/io_uring.c +10
-1@@ -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. +*/ Quoted 6 lines
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.
Quoted 2 lines
Instead, you can pass a flag, i.e.
io_uring_cancel_generic()will have non zero sqd IFF it's the SQPOLL task.
At first, I think of using READ_ONCE here and WRITE_ONCE in the sq thread termination to avoid the data race. What do you think about this approach?
Your proposed approach sounds good too.
Quoted 12 lines
io_uring/io_uring.c __cold void io_uring_cancel_generic(bool · line 3145
Quoted 5 lines
io_uring/io_uring.c +0
-1@@ -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.
Quoted 3 lines
io_uring/io_uring.c +1
-0+ WARN_ON_ONCE(sqd && data_race(sqd->thread) != current); if (!current->io_uring) return;
Oh, thanks. I will remove this.
Thanks,
Quang Minh.
Quoted 49 lines · Bui Quang Minh
Quoted 48 lines · Pavel Begunkov
Quoted 36 lines · Bui Quang Minh
The
sqd->threadaccess inio_uring_cancel_genericis just for debug check so we can safely ignore the data race.The
sqd->threadaccess inio_uring_try_cancel_requestsis to check if the caller is the sq threadi with the checkctx->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, thesq_data->threadread can race with the NULL write in the sq thread termination. However, the race will still makectx->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(-)io_uring/io_uring.c +10
-1@@ -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. +*/ Quoted 6 lines
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.
Quoted 4 lines
Quoted 2 lines
Instead, you can pass a flag, i.e.
io_uring_cancel_generic()will have non zero sqd IFF it's the SQPOLL task.At first, I think of using
READ_ONCEhere andWRITE_ONCEin the sq thread termination to avoid the data race. What do you think about this approach?
Same thing, that'd be complicating synchronisation when there shouldn't be any races in the first place. Having no races is easier than wrapping them into READ_ONCE and keeping in mind what that's even fine.
Btw, the line you're changing doesn't even look right. SQPOLL clears sqd->task right before starting with cancellations, so sounds like it's mindlessly comparing NULL == current.
Signature · 1 line
Pavel Begunkov
Quoted 55 lines · Pavel Begunkov
Quoted 54 lines · Bui Quang Minh
Quoted 52 lines · Pavel Begunkov
Quoted 37 lines · Bui Quang Minh
The
sqd->threadaccess inio_uring_cancel_genericis just for debug check so we can safely ignore the data race.The
sqd->threadaccess inio_uring_try_cancel_requestsis to check if the caller is the sq threadi with the checkctx->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, thesq_data->threadread can race with the NULL write in the sq thread termination. However, the race will still makectx->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(-)io_uring/io_uring.c +10
-1@@ -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. +*/ Quoted 7 lines
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.
Quoted 6 lines
Quoted 1 line
At first, I think of using
READ_ONCEhere andWRITE_ONCEin the sq thread termination to avoid the data race. What do you think about this approach?Same thing, that'd be complicating synchronisation when there shouldn't be any races in the first place. Having no races is easier than wrapping them into
READ_ONCEand keeping in mind what that's even fine.
Okay, I'll send another patch with a new flag for the cancel path.
Quoted 3 lines
Btw, the line you're changing doesn't even look right. SQPOLL clears
sqd->taskright before starting with cancellations, so sounds like it's mindlessly comparing NULL == current.
Hmm, I think it's correct but quite easy to get confused here. In the io_sq_thread, we explicitly call io_uring_cancel_generic before setting sqd->thread = NULL. The later io_uring_cancel_generic call in do_exit actually does nothing as we already set the task_struct->io_uring to NULL in the previous call.
Thanks,
Quang Minh.
Quoted 61 lines · Bui Quang Minh
Quoted 53 lines · Pavel Begunkov
Quoted 52 lines · Bui Quang Minh
Quoted 50 lines · Pavel Begunkov
Quoted 36 lines · Bui Quang Minh
The
sqd->threadaccess inio_uring_cancel_genericis just for debug check so we can safely ignore the data race.The
sqd->threadaccess inio_uring_try_cancel_requestsis to check if the caller is the sq threadi with the checkctx->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, thesq_data->threadread can race with the NULL write in the sq thread termination. However, the race will still makectx->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(-)io_uring/io_uring.c +10
-1@@ -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. +*/ Quoted 6 lines
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.Quoted 6 lines
Quoted 1 line
At first, I think of using
READ_ONCEhere andWRITE_ONCEin the sq thread termination to avoid the data race. What do you think about this approach?Same thing, that'd be complicating synchronisation when there shouldn't be any races in the first place. Having no races is easier than wrapping them into
READ_ONCEand keeping in mind what that's even fine.
Quoted 7 lines
Okay, I'll send another patch with a new flag for the cancel path.
Quoted 3 lines
Btw, the line you're changing doesn't even look right. SQPOLL clears
sqd->taskright before starting with cancellations, so sounds like it's mindlessly comparing NULL == current.Hmm, I think it's correct but quite easy to get confused here. In the
io_sq_thread, we explicitly callio_uring_cancel_genericbefore settingsqd->thread= NULL. The laterio_uring_cancel_genericcall indo_exitactually does nothing as we already set thetask_struct->io_uringto NULL in the previous call.
Yeah, you're right, mixed it up with normal user task cancellation, which happen in the exit path.
Signature · 1 line
Pavel Begunkov