Skip to contentSkip to conversation

io-wq: cancelation race fix and small cleanup in io-wq

PATCH Applied Gabriel Krisman Bertazi 2 participants 5 messages Apr 16, 2024 – Apr 17, 2024 io_uring

lore
Share & tools
lore
#0 lore raw

cover io-wq: cancelation race fix and small cleanup in io-wq

Hi Jens,

Two small fixes to the wq path, closing a small race of cancelation with wq work removal for execution.

Thank you,

Gabriel Krisman Bertazi (2):
io-wq: write next_work before dropping acct_lock
io-wq: Drop intermediate step between pending list and active work

 io_uring/io-wq.c | 20 ++++++++++----------
 1 file changed, 10 insertions(+), 10 deletions(-)
Signature · 1 line
2.44.0
#1 lore raw

PATCH 1/2 io-wq: write next_work before dropping acct_lock

Commit 361aee450c6e ("io-wq: add intermediate work step between pending list and active work") closed a race between a cancellation and the work being removed from the wq for execution. To ensure the request is always reachable by the cancellation, we need to move it within the wq lock, which also synchronizes the cancellation. But commit 42abc95f05bf ("io-wq: decouple work_list protection from the big wqe->lock") replaced the wq lock here and accidentally reintroduced the race by releasing the acct_lock too early.

In other words:

        worker                |     cancellation
work = io_get_next_work()     |
raw_spin_unlock(&acct->lock); |
			      |
                              | io_acct_cancel_pending_work
                              | io_wq_worker_cancel()

worker->next_work = work

Using acct_lock is still enough since we synchronize on it on io_acct_cancel_pending_work.

Fixes 42abc95f05bf ("io-wq: decouple work_list protection from the big wqe->lock")
---
 io_uring/io-wq.c | 13 ++++++++-----
 1 file changed, 8 insertions(+), 5 deletions(-)
io_uring/io-wq.c +8 -5
index 522196dfb0ff..318ed067dbf6 100644
@@ -564,10 +564,7 @@ static void io_worker_handle_work(struct io_wq_acct *acct,
 		 * clear the stalled flag.
 		 */
 		work = io_get_next_work(acct, worker);
-		raw_spin_unlock(&acct->lock);
 		if (work) {
-			__io_worker_busy(wq, worker);
-
 			/*
 			 * Make sure cancelation can find this, even before
 			 * it becomes the active work. That avoids a window
@@ -578,9 +575,15 @@ static void io_worker_handle_work(struct io_wq_acct *acct,
 			raw_spin_lock(&worker->lock);
 			worker->next_work = work;
 			raw_spin_unlock(&worker->lock);
-		} else {
-			break;
 		}
+
+		raw_spin_unlock(&acct->lock);
+
+		if (!work)
+			break;
+
+		__io_worker_busy(wq, worker);
+
 		io_assign_current_work(worker, work);
 		__set_current_state(TASK_RUNNING);
 
Signature · 1 line
2.44.0
#2 lore raw

PATCH 2/2 io-wq: Drop intermediate step between pending list and active work

In reply to Gabriel Krisman Bertazi

next_work is only used to make the work visible for cancellation. Instead, we can just directly write to cur_work before dropping the acct_lock and avoid the extra hop.

---
 io_uring/io-wq.c | 7 ++-----
 1 file changed, 2 insertions(+), 5 deletions(-)
io_uring/io-wq.c +2 -5
index 318ed067dbf6..d7fc6f6d4477 100644
@@ -51,7 +51,6 @@ struct io_worker {
 	struct io_wq *wq;
 
 	struct io_wq_work *cur_work;
-	struct io_wq_work *next_work;
 	raw_spinlock_t lock;
 
 	struct completion ref_done;
@@ -539,7 +538,6 @@ static void io_assign_current_work(struct io_worker *worker,
 
 	raw_spin_lock(&worker->lock);
 	worker->cur_work = work;
-	worker->next_work = NULL;
 	raw_spin_unlock(&worker->lock);
 }
 
@@ -573,7 +571,7 @@ static void io_worker_handle_work(struct io_wq_acct *acct,
 			 * current work item for this worker.
 			 */
 			raw_spin_lock(&worker->lock);
-			worker->next_work = work;
+			worker->cur_work = work;
 			raw_spin_unlock(&worker->lock);
 		}
 
@@ -1008,8 +1006,7 @@ static bool io_wq_worker_cancel(struct io_worker *worker, void *data)
 	 * may dereference the passed in work.
 	 */
 	raw_spin_lock(&worker->lock);
-	if (__io_wq_worker_cancel(worker, match, worker->cur_work) ||
-	    __io_wq_worker_cancel(worker, match, worker->next_work))
+	if (__io_wq_worker_cancel(worker, match, worker->cur_work))
 		match->nr_running++;
 	raw_spin_unlock(&worker->lock);
 
Signature · 1 line
2.44.0
maintainer
#3 lore raw
In reply to Gabriel Krisman Bertazi
Quoted 4 lines · Gabriel Krisman Bertazi

Hi Jens,

Two small fixes to the wq path, closing a small race of cancelation with wq work removal for execution.

Looks good, and cleans it up nicely. Thanks!

Signature · 1 line
Jens Axboe
maintainer Applied
#4 lore raw
In reply to Gabriel Krisman Bertazi
Quoted 10 lines · Gabriel Krisman Bertazi

Two small fixes to the wq path, closing a small race of cancelation with wq work removal for execution.

Thank you,

Gabriel Krisman Bertazi (2):
io-wq: write next_work before dropping acct_lock
io-wq: Drop intermediate step between pending list and active work

[...]

Applied, thanks!

[1/2] io-wq: write next_work before dropping acct_lock

      commit: 068c27e32e51e94e4a9eb30ae85f4097a3602980
[2/2] io-wq: Drop intermediate step between pending list and active work
      commit: 24c3fc5c75c5b9d471783b4a4958748243828613

Best regards,

Signature · 1 line
Jens Axboe