All of lore.kernel.org
 help / color / mirror / Atom feed
From: Jens Axboe <axboe@kernel.dk>
To: stable@vger.kernel.org
Cc: io-uring@vger.kernel.org, gregkh@linuxfoundation.org
Subject: [PATCH 6.12.y 06/15] io_uring: free the ring after a grace period
Date: Sat,  5 Dec 2026 23:10:58 +0000	[thread overview]
Message-ID: <20261205231107.4023506-7-axboe@kernel.dk> (raw)
In-Reply-To: <20261205231107.4023506-1-axboe@kernel.dk>

From: Chris Mason <mason@kernel.org>

Commit 0e433c705aff3846356f402e8b054d6e4ebbdbe0 upstream.

On !PREEMPT_RT, a task that removes its tctx node on exit or exec can
race with the exit work of a closed ring and cause a use-after-free of
the io_ring_ctx.

A mutex has to stay allocated until mutex_unlock() has returned. But
io_uring_del_tctx_node() holds no reference on the ring, only its node
on ->tctx_list keeps it, and io_ring_exit_work() frees the ring once it
has tctx_lock and finds the list empty. If the exit work has queued as
a waiter, it can get there while the unlock still needs the mutex:

    io_uring_del_tctx_node()        io_ring_exit_work()
    ========================        ===================
    mutex_lock(tctx_lock)
    list_del()
                                    mutex_lock(tctx_lock)
                                      lock wait_lock
                                      set MUTEX_FLAG_WAITERS
    mutex_unlock(tctx_lock)
      clear the owner
                                      __mutex_trylock() succeeds
                                      unlock wait_lock
                                    list_empty() is true
                                    mutex_unlock(tctx_lock)
                                    io_ring_ctx_free()
                                      kfree(ctx)
      lock wait_lock
      waiter = lock->first_waiter

Each node used to hold a file reference that was dropped after the
unlock. Commit eebd2e37e662 ("io_uring: don't take task ring-file
notes") removed it, and relies on the exit work to wait for the nodes.

The fix used here is to do the unlock inside an RCU read section, and to
free the io_ring_ctx only after a grace period.

Fixes: eebd2e37e662 ("io_uring: don't take task ring-file notes")
Signed-off-by: Chris Mason <mason@kernel.org>
Signed-off-by: Jens Axboe <axboe@kernel.dk>
---
 include/linux/io_uring_types.h | 2 ++
 io_uring/io_uring.c            | 2 +-
 io_uring/tctx.c                | 3 +++
 3 files changed, 6 insertions(+), 1 deletion(-)

diff --git a/include/linux/io_uring_types.h b/include/linux/io_uring_types.h
index 3fa179dd10b4..a06b75630222 100644
--- a/include/linux/io_uring_types.h
+++ b/include/linux/io_uring_types.h
@@ -406,6 +406,8 @@ struct io_ring_ctx {
 	struct llist_head		fallback_llist;
 	struct delayed_work		fallback_work;
 	struct work_struct		exit_work;
+	/* the ctx is freed after a grace period, see io_uring_del_tctx_node() */
+	struct rcu_head			rcu;
 	struct completion		ref_comp;
 
 	/* io-wq management, e.g. thread count */
diff --git a/io_uring/io_uring.c b/io_uring/io_uring.c
index 3e5201e19bb4..e61d1a124a27 100644
--- a/io_uring/io_uring.c
+++ b/io_uring/io_uring.c
@@ -2865,7 +2865,7 @@ static __cold void io_ring_ctx_free(struct io_ring_ctx *ctx)
 	kfree(ctx->cancel_table_locked.hbs);
 	xa_destroy(&ctx->io_bl_xa);
 	xa_destroy(&ctx->personalities);
-	kfree(ctx);
+	kfree_rcu(ctx, rcu);
 }
 
 static __cold void io_activate_pollwq_cb(struct callback_head *cb)
diff --git a/io_uring/tctx.c b/io_uring/tctx.c
index 63c97eec3038..9845bbd7551b 100644
--- a/io_uring/tctx.c
+++ b/io_uring/tctx.c
@@ -180,7 +180,10 @@ __cold void io_uring_del_tctx_node(unsigned long index)
 
 	mutex_lock(&node->ctx->tctx_lock);
 	list_del(&node->ctx_node);
+	/* keeps the ctx until the unlock is done, see io_ring_ctx_free() */
+	rcu_read_lock();
 	mutex_unlock(&node->ctx->tctx_lock);
+	rcu_read_unlock();
 
 	if (tctx->last == node->ctx)
 		tctx->last = NULL;
-- 
2.55.0


  parent reply	other threads:[~2026-12-05 23:11 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-05 23:10 [PATCH 6.12.y 00/15] io_uring fixes for 6.12-stable Jens Axboe
2026-12-05 23:10 ` [PATCH 6.12.y 01/15] io_uring/net: don't charge the folio offset to truesize for managed frags Jens Axboe
2026-12-05 23:10 ` [PATCH 6.12.y 02/15] io_uring/net: don't copy msg_name on a recvmsg retry Jens Axboe
2026-12-05 23:10 ` [PATCH 6.12.y 03/15] io_uring/kbuf: clear REQ_F_BL_NO_RECYCLE on buffer put Jens Axboe
2026-12-05 23:10 ` [PATCH 6.12.y 04/15] io_uring/tctx: add separate lock for list of tctx's in ctx Jens Axboe
2026-12-05 23:10 ` [PATCH 6.12.y 05/15] io_uring: free the task context after a grace period Jens Axboe
2026-12-05 23:10 ` Jens Axboe [this message]
2026-12-05 23:10 ` [PATCH 6.12.y 07/15] io_uring/filetable: don't install O_PATH files Jens Axboe
2026-12-05 23:11 ` [PATCH 6.12.y 08/15] io_uring: make ctx->timeout_lock a raw spinlock Jens Axboe
2026-12-05 23:11 ` [PATCH 6.12.y 09/15] io_uring/timeout: flush timeouts outside of the timeout lock Jens Axboe
2026-12-05 23:11 ` [PATCH 6.12.y 10/15] io_uring/cancel: always walk links under timeout_lock Jens Axboe
2026-12-05 23:11 ` [PATCH 6.12.y 11/15] io_uring/net: don't count the buffers of a send bundle Jens Axboe
2026-12-05 23:11 ` [PATCH 6.12.y 12/15] io_uring/net: restore the cmsg buffer on recvmsg retry Jens Axboe
2026-12-05 23:11 ` [PATCH 6.12.y 13/15] io_uring_poll: kill the no longer necessary barrier after poll_wait() Jens Axboe
2026-12-05 23:11 ` [PATCH 6.12.y 14/15] io_uring: close poll activation race when enabling rings Jens Axboe
2026-12-05 23:11 ` [PATCH 6.12.y 15/15] io_uring/cancel: arm local work wakeups only for submitter Jens Axboe

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261205231107.4023506-7-axboe@kernel.dk \
    --to=axboe@kernel.dk \
    --cc=gregkh@linuxfoundation.org \
    --cc=io-uring@vger.kernel.org \
    --cc=stable@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.