From: Jens Axboe <axboe@kernel.dk>
To: stable@vger.kernel.org
Cc: io-uring@vger.kernel.org, gregkh@linuxfoundation.org
Subject: [PATCH 6.18.y 06/14] io_uring: free the ring after a grace period
Date: Sat, 5 Dec 2026 23:11:39 +0000 [thread overview]
Message-ID: <20261205231147.4095031-7-axboe@kernel.dk> (raw)
In-Reply-To: <20261205231147.4095031-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 bdb0dc928c6e..a866cbb92620 100644
--- a/include/linux/io_uring_types.h
+++ b/include/linux/io_uring_types.h
@@ -447,6 +447,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 fb4528bc3aac..d300026c74bf 100644
--- a/io_uring/io_uring.c
+++ b/io_uring/io_uring.c
@@ -3068,7 +3068,7 @@ static __cold void io_ring_ctx_free(struct io_ring_ctx *ctx)
kvfree(ctx->cancel_table.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 c7b08c9a1657..602b5af32053 100644
--- a/io_uring/tctx.c
+++ b/io_uring/tctx.c
@@ -186,7 +186,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
next prev parent reply other threads:[~2026-12-05 23:12 UTC|newest]
Thread overview: 15+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-12-05 23:11 [PATCH 6.18.y 00/14] io_uring fixes for 6.18-stable Jens Axboe
2026-12-05 23:11 ` [PATCH 6.18.y 01/14] io_uring/net: don't charge the folio offset to truesize for managed frags Jens Axboe
2026-12-05 23:11 ` [PATCH 6.18.y 02/14] io_uring/net: don't copy msg_name on a recvmsg retry Jens Axboe
2026-12-05 23:11 ` [PATCH 6.18.y 03/14] io_uring/kbuf: clear REQ_F_BL_NO_RECYCLE on buffer put Jens Axboe
2026-12-05 23:11 ` [PATCH 6.18.y 04/14] io_uring/tctx: add separate lock for list of tctx's in ctx Jens Axboe
2026-12-05 23:11 ` [PATCH 6.18.y 05/14] io_uring: free the task context after a grace period Jens Axboe
2026-12-05 23:11 ` Jens Axboe [this message]
2026-12-05 23:11 ` [PATCH 6.18.y 07/14] io_uring/filetable: don't install O_PATH files Jens Axboe
2026-12-05 23:11 ` [PATCH 6.18.y 08/14] io_uring/cancel: always walk links under timeout_lock Jens Axboe
2026-12-05 23:11 ` [PATCH 6.18.y 09/14] io_uring/net: don't count the buffers of a send bundle Jens Axboe
2026-12-05 23:11 ` [PATCH 6.18.y 10/14] io_uring/net: restore the cmsg buffer on recvmsg retry Jens Axboe
2026-12-05 23:11 ` [PATCH 6.18.y 11/14] io_uring: close poll activation race when enabling rings Jens Axboe
2026-12-05 23:11 ` [PATCH 6.18.y 12/14] io_uring/cancel: arm local work wakeups only for submitter Jens Axboe
2026-12-05 23:11 ` [PATCH 6.18.y 13/14] io_uring/zcrx: cache fallback availability in zcrx ctx Jens Axboe
2026-12-05 23:11 ` [PATCH 6.18.y 14/14] io_uring/zcrx: don't sync dma-bufs 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=20261205231147.4095031-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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox