]> git.hungrycats.org Git - linux/commitdiff
io_uring: free the task context after a grace period
authorChris Mason <mason@kernel.org>
Wed, 4 Nov 2026 18:44:43 +0000 (11:44 -0700)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Fri, 6 Nov 2026 17:10:53 +0000 (12:10 -0500)
Commit 9f5315d3f4368590c9fe06d4313b7247470bcf15 upstream.

A task other than the owner can free a request, io_ring_exit_work()
running the local task_work of a DEFER_TASKRUN ring for example, or
io_do_iopoll() reaping completions. io_put_task() then takes the else
branch:

    percpu_counter_sub(&tctx->inflight, 1);
    if (unlikely(atomic_read(&tctx->in_cancel)))
            wake_up(&tctx->wait);
    put_task_struct(tctx->task);

The first line is what lets the owner leave io_uring_cancel_generic(),
and on exec it frees the tctx right after. Nothing holds that off for
the remaining three lines, the task reference only covers a tctx freed
along with the task_struct. It used to work out because
io_uring_del_tctx_node() took the uring_lock that the freeing side holds
across io_put_task(), until that moved to tctx_lock.

Free the tctx with kfree_rcu(), and enter an RCU read section in
io_put_task() before the count is dropped.

Fixes: 5623eb1ed035 ("io_uring/tctx: add separate lock for list of tctx's in ctx")
Signed-off-by: Chris Mason <mason@kernel.org>
Signed-off-by: Jens Axboe <axboe@kernel.dk>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
include/linux/io_uring_types.h
io_uring/io_uring.c
io_uring/tctx.c

index c5edf6246a4b4a3199275b0f5fc3fcc56a6bb311..1c85ce0e471d2172abcbadeb16844fb6cc934237 100644 (file)
@@ -161,6 +161,9 @@ struct io_uring_task {
        /* drains ->task_list once the task can no longer run task_work */
        struct work_struct              fallback_work;
 
+       /* the tctx is freed after a grace period, see io_put_task() */
+       struct rcu_head                 rcu;
+
        struct { /* task_work */
                struct mpscq            task_list;
                struct callback_head    task_work;
index 880be1fed3a66b0bab4114b8dc6d610d01887855..c8aa6ca4a7cc4ea10ef0cd52bb1d1c6d81d0f2bd 100644 (file)
@@ -634,6 +634,8 @@ static inline void io_put_task(struct io_kiocb *req)
        if (likely(tctx->task == current)) {
                tctx->cached_refs++;
        } else {
+               guard(rcu)();
+
                percpu_counter_sub(&tctx->inflight, 1);
                if (unlikely(atomic_read(&tctx->in_cancel)))
                        wake_up(&tctx->wait);
index 291e30e00e450e9475d36434b2d1fc2f5aedbfb7..b4fecea3ea479718c07eaad28dddc6f55681d0d4 100644 (file)
@@ -64,7 +64,7 @@ void io_uring_free_tctx(struct task_struct *tsk)
                WARN_ON_ONCE(tctx->cached_refs);
 
                percpu_counter_destroy(&tctx->inflight);
-               kfree(tctx);
+               kfree_rcu(tctx, rcu);
                tsk->io_uring = NULL;
        }
 }