From: Jens Axboe <axboe@kernel.dk>
To: stable@vger.kernel.org
Cc: io-uring@vger.kernel.org, gregkh@linuxfoundation.org
Subject: [PATCH 6.1.y 4/9] io_uring: free the ring after a grace period
Date: Sat, 5 Dec 2026 23:10:07 +0000 [thread overview]
Message-ID: <20261205231012.3977993-5-axboe@kernel.dk> (raw)
In-Reply-To: <20261205231012.3977993-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 93d9e8130b5a..8bee77acb384 100644
--- a/include/linux/io_uring_types.h
+++ b/include/linux/io_uring_types.h
@@ -351,6 +351,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 75e92f581f61..dc2cac09cbe6 100644
--- a/io_uring/io_uring.c
+++ b/io_uring/io_uring.c
@@ -2805,7 +2805,7 @@ static __cold void io_ring_ctx_free(struct io_ring_ctx *ctx)
kfree(ctx->io_bl);
xa_destroy(&ctx->io_bl_xa);
xa_destroy(&ctx->personalities);
- kfree(ctx);
+ kfree_rcu(ctx, rcu);
}
static __poll_t io_uring_poll(struct file *file, poll_table *wait)
diff --git a/io_uring/tctx.c b/io_uring/tctx.c
index daba0a2c4ec2..85d670a52c51 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
next prev parent reply other threads:[~2026-12-05 23:10 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-12-05 23:10 [PATCH 6.1.y 0/9] io_uring fixes for 6.1-stable Jens Axboe
2026-12-05 23:10 ` [PATCH 6.1.y 1/9] io_uring/net: don't copy msg_name on a recvmsg retry Jens Axboe
2026-12-05 23:10 ` [PATCH 6.1.y 2/9] io_uring/tctx: add separate lock for list of tctx's in ctx Jens Axboe
2026-12-05 23:10 ` [PATCH 6.1.y 3/9] 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.1.y 5/9] io_uring/filetable: don't install O_PATH files Jens Axboe
2026-12-05 23:10 ` [PATCH 6.1.y 6/9] io_uring: make ctx->timeout_lock a raw spinlock Jens Axboe
2026-12-05 23:10 ` [PATCH 6.1.y 7/9] io_uring/timeout: flush timeouts outside of the timeout lock Jens Axboe
2026-12-05 23:10 ` [PATCH 6.1.y 8/9] io_uring/cancel: always walk links under timeout_lock Jens Axboe
2026-12-05 23:10 ` [PATCH 6.1.y 9/9] 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=20261205231012.3977993-5-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.