Skip to content

Commit f7b32e7

Browse files
isilenceaxboe
authored andcommitted
io_uring: fix mutex_unlock with unreferenced ctx
Callers of mutex_unlock() have to make sure that the mutex stays alive for the whole duration of the function call. For io_uring that means that the following pattern is not valid unless we ensure that the context outlives the mutex_unlock() call. mutex_lock(&ctx->uring_lock); req_put(req); // typically via io_req_task_submit() mutex_unlock(&ctx->uring_lock); Most contexts are fine: io-wq pins requests, syscalls hold the file, task works are taking ctx references and so on. However, the task work fallback path doesn't follow the rule. Cc: <[email protected]> Fixes: 04fc6c8 ("io_uring: save ctx put/get for task_work submit") Reported-by: Jann Horn <[email protected]> Signed-off-by: Pavel Begunkov <[email protected]> Link: https://lore.kernel.org/io-uring/CAG48ez3xSoYb+45f1RLtktROJrpiDQ1otNvdR+YLQf7m+Krj5Q@mail.gmail.com/ Signed-off-by: Jens Axboe <[email protected]>
1 parent 73363c2 commit f7b32e7

File tree

1 file changed

+3
-6
lines changed

1 file changed

+3
-6
lines changed

io_uring/io_uring.c

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -271,13 +271,15 @@ static __cold void io_fallback_req_func(struct work_struct *work)
271271
struct io_kiocb *req, *tmp;
272272
struct io_tw_state ts = { .locked = true, };
273273

274+
percpu_ref_get(&ctx->refs);
274275
mutex_lock(&ctx->uring_lock);
275276
llist_for_each_entry_safe(req, tmp, node, io_task_work.node)
276277
req->io_task_work.func(req, &ts);
277278
if (WARN_ON_ONCE(!ts.locked))
278279
return;
279280
io_submit_flush_completions(ctx);
280281
mutex_unlock(&ctx->uring_lock);
282+
percpu_ref_put(&ctx->refs);
281283
}
282284

283285
static int io_alloc_hash_table(struct io_hash_table *table, unsigned bits)
@@ -3146,12 +3148,7 @@ static __cold void io_ring_exit_work(struct work_struct *work)
31463148
init_completion(&exit.completion);
31473149
init_task_work(&exit.task_work, io_tctx_exit_cb);
31483150
exit.ctx = ctx;
3149-
/*
3150-
* Some may use context even when all refs and requests have been put,
3151-
* and they are free to do so while still holding uring_lock or
3152-
* completion_lock, see io_req_task_submit(). Apart from other work,
3153-
* this lock/unlock section also waits them to finish.
3154-
*/
3151+
31553152
mutex_lock(&ctx->uring_lock);
31563153
while (!list_empty(&ctx->tctx_list)) {
31573154
WARN_ON_ONCE(time_after(jiffies, timeout));

0 commit comments

Comments
 (0)