Linux io-uring development
 help / color / mirror / Atom feed
From: Jens Axboe <axboe@kernel.dk>
To: io-uring@vger.kernel.org
Cc: Chris Mason <mason@kernel.org>, Jens Axboe <axboe@kernel.dk>
Subject: [PATCH 03/10] io_uring: free the ring after a grace period
Date: Wed, 25 Nov 2026 11:43:12 -0700	[thread overview]
Message-ID: <20261125184439.58077-4-axboe@kernel.dk> (raw)
In-Reply-To: <20261125184439.58077-1-axboe@kernel.dk>

From: Chris Mason <mason@kernel.org>

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 1468bcb02893..1b85872bb7ea 100644
--- a/include/linux/io_uring_types.h
+++ b/include/linux/io_uring_types.h
@@ -517,6 +517,8 @@ struct io_ring_ctx {
 
 	/* ctx exit and cancelation */
 	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 be5a4f9505e0..06ff99921871 100644
--- a/io_uring/io_uring.c
+++ b/io_uring/io_uring.c
@@ -2368,7 +2368,7 @@ static __cold void io_ring_ctx_free(struct io_ring_ctx *ctx)
 	xa_destroy(&ctx->io_bl_xa);
 	xa_destroy(&ctx->hpage_acct);
 	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 b4fecea3ea47..c6b7f5647f79 100644
--- a/io_uring/tctx.c
+++ b/io_uring/tctx.c
@@ -231,7 +231,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-11-25 18:44 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-25 18:43 [PATCHSET 0/10] Various fixes Jens Axboe
2026-11-25 18:43 ` [PATCH 01/10] io_uring/net: don't copy msg_name on a recvmsg retry Jens Axboe
2026-11-25 18:43 ` [PATCH 02/10] io_uring/kbuf: clear REQ_F_BL_NO_RECYCLE on buffer put Jens Axboe
2026-11-25 18:43 ` Jens Axboe [this message]
2026-11-25 18:43 ` [PATCH 04/10] io_uring: free the ring from an RCU work Jens Axboe
2026-11-25 18:43 ` [PATCH 05/10] io_uring/filetable: don't install O_PATH files Jens Axboe
2026-11-25 18:43 ` [PATCH 06/10] io_uring/timeout: hold timeout_lock for the link splice Jens Axboe
2026-11-25 18:43 ` [PATCH 07/10] io_uring/cancel: always walk links under timeout_lock Jens Axboe
2026-11-25 18:43 ` [PATCH 08/10] io_uring/net: don't count the buffers of a send bundle Jens Axboe
2026-11-25 18:43 ` [PATCH 09/10] io_uring: drop cached task refs on ring release Jens Axboe
2026-11-25 18:43 ` [PATCH 10/10] io_uring/net: restore the cmsg buffer on recvmsg retry 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=20261125184439.58077-4-axboe@kernel.dk \
    --to=axboe@kernel.dk \
    --cc=io-uring@vger.kernel.org \
    --cc=mason@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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox