io_uring/io-wq: Fix a small time window for reading work->flags
PATCH 1/1 io_uring/io-wq: Fix a small time window for reading work->flags
There is a small time window that is modified by other tasks after reading work->flags. It is changed to read before use, which is more in line with the semantics of atoms.
--- io_uring/io-wq.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-)
io_uring/io-wq.c +2 -3
@@ -932,7 +932,6 @@ static bool io_wq_work_match_item(struct io_wq_work *work, void *data)void io_wq_enqueue (struct io_wq *wq,struct io_wq_work *work) {struct io_wq_acct *acct =io_work_get_acct (wq, work); -unsigned int work_flags =atomic_read (&work->flags);struct io_cb_cancel_data match = { .fn = io_wq_work_match_item, .data = work,
@@ -945,7 +944,7 @@ void io_wq_enqueue(struct io_wq *wq, struct io_wq_work *work)* been marked as one that should not get executed, cancel it here. */ if (test_bit (IO_WQ_BIT_EXIT , &wq->state) || - (work_flags &IO_WQ_WORK_CANCEL )) { + (atomic_read (&work->flags) &IO_WQ_WORK_CANCEL )) {io_run_cancel (work, wq);return ; }
@@ -959,7 +958,7 @@ void io_wq_enqueue(struct io_wq *wq, struct io_wq_work *work) do_create = !io_wq_activate_free_worker (wq, acct);rcu_read_unlock (); -if (do_create && ((work_flags &IO_WQ_WORK_CONCURRENT ) || +if (do_create && ((atomic_read (&work->flags) &IO_WQ_WORK_CONCURRENT ) || !atomic_read (&acct->nr_running))) {bool did_create;
Signature · 1 line
2.33.0
Quoted 2 lines · lizetao
There is a small time window that is modified by other tasks after reading
work->flags. It is changed to read before use, which is more
Can you elaborate on what races with what? I don't immediately see any race here.
Quoted 37 lines
in line with the semantics of atoms.
Fixes 3474d1b93f89 ("io_uring/io-wq: make io_wq_work flags atomic")Signed-off-by Li Zetao <[email protected]>--- io_uring/io-wq.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-)io_uring/io-wq.c +2
-3@@ -932,7 +932,6 @@ static bool io_wq_work_match_item(struct io_wq_work *work, void *data)void io_wq_enqueue (struct io_wq *wq,struct io_wq_work *work) {struct io_wq_acct *acct =io_work_get_acct (wq, work); -unsigned int work_flags =atomic_read (&work->flags);struct io_cb_cancel_data match = { .fn = io_wq_work_match_item, .data = work,@@ -945,7 +944,7 @@ void io_wq_enqueue(struct io_wq *wq, struct io_wq_work *work)* been marked as one that should not get executed, cancel it here. */ if (test_bit (IO_WQ_BIT_EXIT , &wq->state) || - (work_flags &IO_WQ_WORK_CANCEL )) { + (atomic_read (&work->flags) &IO_WQ_WORK_CANCEL )) {io_run_cancel (work, wq);return ; }@@ -959,7 +958,7 @@ void io_wq_enqueue(struct io_wq *wq, struct io_wq_work *work) do_create = !io_wq_activate_free_worker (wq, acct);rcu_read_unlock (); -if (do_create && ((work_flags &IO_WQ_WORK_CONCURRENT ) || +if (do_create && ((atomic_read (&work->flags) &IO_WQ_WORK_CONCURRENT ) || !atomic_read (&acct->nr_running))) {bool did_create;
Signature · 1 line
Pavel Begunkov
Hi,
Quoted 6 lines
-----Original Message-----
From: Pavel Begunkov <[email protected]>
Sent: Wednesday, January 15, 2025 12:22 AM
To: lizetao <[email protected]>; Jens Axboe <[email protected]>
Cc: [email protected]
Subject: Re: [PATCH]io_uring/io-wq: Fix a small time window for reading work-
Quoted 8 lines
Quoted 1 line
flags
Quoted 2 lines · lizetao
There is a small time window that is modified by other tasks after reading
work->flags. It is changed to read before use, which is moreCan you elaborate on what races with what? I don't immediately see any race here.
There is such a race context:
worker process io_worker_handle_work: IORING_OP_ASYNC_CANCEL io_wq_enqueue __io_wq_worker_cancel work_flags = atomic_read(&work->flags); // no IO_WQ_WORK_CANCEL atomic_or(IO_WQ_WORK_CANCEL, &work->flags); if (work_flags & IO_WQ_WORK_CANCEL) // false
There seems to be a small time window here, resulting in the latest flags not being used.
Quoted 36 lines
io_uring/io-wq.c static bool io_wq_work_match_item(struct
Quoted 13 lines
in line with the semantics of atoms.
Fixes 3474d1b93f89 ("io_uring/io-wq: make io_wq_work flags atomic")Signed-off-by Li Zetao <[email protected]>--- io_uring/io-wq.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-)io_uring/io-wq.c index renamed from io_uring/io-wq.c +0
-0
a38f36b68060..75096e77b1fe100644io_uring/io-wq.c +0
-0@@ -932,7 +932,6 @@ static bool io_wq_work_match_item(struct
io_wq_work*work, void *data)io_uring/io-wq.c void io_wq_enqueue(struct io_wq *wq, struct
Quoted 8 lines
io_uring/io-wq.c +0
-1void io_wq_enqueue (struct io_wq *wq,struct io_wq_work *work) {struct io_wq_acct *acct =io_work_get_acct (wq, work); -unsigned int work_flags =atomic_read (&work->flags);struct io_cb_cancel_data match = { .fn = io_wq_work_match_item, .data = work,@@ -945,7 +944,7 @@ void io_wq_enqueue(struct io_wq *wq, struct
io_wq_work*work)io_uring/io-wq.c void io_wq_enqueue(struct io_wq *wq, struct
Quoted 9 lines
io_uring/io-wq.c +1
-1* been marked as one that should not get executed, cancel it here. */ if (test_bit (IO_WQ_BIT_EXIT , &wq->state) || - (work_flags &IO_WQ_WORK_CANCEL )) { + (atomic_read (&work->flags) &IO_WQ_WORK_CANCEL )) {io_run_cancel (work, wq);return ; }@@ -959,7 +958,7 @@ void io_wq_enqueue(struct io_wq *wq, struct
io_wq_work*work)Quoted 3 lines
io_uring/io-wq.c +0
-0do_create = !io_wq_activate_free_worker (wq, acct);rcu_read_unlock ();
Quoted 9 lines
io_uring/io-wq.c void io_wq_enqueue(struct io_wq *wq, struct io_wq_work *work) · line 963
Quoted 6 lines
io_uring/io-wq.c +2
-1-if (do_create && ((work_flags &IO_WQ_WORK_CONCURRENT ) || +if (do_create && ((atomic_read (&work->flags) & +IO_WQ_WORK_CONCURRENT ) || !atomic_read (&acct->nr_running))) {bool did_create;--
Pavel Begunkov
---
Li Zetao
Quoted 17 lines · lizetao
Hi,
Quoted 6 lines
-----Original Message-----
From: Pavel Begunkov <[email protected]>
Sent: Wednesday, January 15, 2025 12:22 AM
To: lizetao <[email protected]>; Jens Axboe <[email protected]>
Cc: [email protected]
Subject: Re: [PATCH]io_uring/io-wq: Fix a small time window for reading work-Quoted 8 lines
Quoted 1 line
flags
Quoted 2 lines · lizetao
There is a small time window that is modified by other tasks after reading
work->flags. It is changed to read before use, which is moreCan you elaborate on what races with what? I don't immediately see any race here.
Quoted 7 lines
There is such a race context:
worker process io_worker_handle_work: IORING_OP_ASYNC_CANCEL io_wq_enqueue __io_wq_worker_cancel work_flags = atomic_read(&work->flags); // no IO_WQ_WORK_CANCEL atomic_or(IO_WQ_WORK_CANCEL, &work->flags);
^^^
That can't happen, the request is not discoverable via iowq yet.
Quoted 1 line
if (
work_flags&IO_WQ_WORK_CANCEL) // false
This check is for requests that came with the flag already set.
Signature · 1 line
Pavel Begunkov