All of lore.kernel.org
 help / color / mirror / Atom feed
From: Jens Axboe <axboe@kernel.dk>
To: io-uring@vger.kernel.org
Cc: Jens Axboe <axboe@kernel.dk>
Subject: [PATCH 04/10] io_uring: free the ring from an RCU work
Date: Wed, 25 Nov 2026 11:43:13 -0700	[thread overview]
Message-ID: <20261125184439.58077-5-axboe@kernel.dk> (raw)
In-Reply-To: <20261125184439.58077-1-axboe@kernel.dk>

The ring is currently freed in two steps: a synchronize_rcu() in
io_ring_exit_work() for DEFER_TASKRUN and SQPOLL rings, which pairs with
the RCU read sections in the task_work add paths, and a kfree_rcu() of
the ctx itself, which pairs with the one in io_uring_del_tctx_node().

The kfree_rcu() alone can't replace the synchronize_rcu(). The task_work
add paths also look at the ring memory (->rings and ->rings_rcu, for the
SQ flags) and at ->submitter_task, and io_ring_ctx_free() releases both
before it gets to the free of the ctx.

Instead of waiting for a grace period in the exit work, queue the whole
io_ring_ctx_free() as an RCU work, and make its final free a plain
kfree(). That is process context, as io_ring_ctx_free() needs, and
everything it tears down now happens after a grace period. This covers
the readers in the task_work add paths for all rings, and the unlock in
io_uring_del_tctx_node(), and removes both the synchronize_rcu() and the
rcu_head in the ctx.

Signed-off-by: Jens Axboe <axboe@kernel.dk>
---
 include/linux/io_uring_types.h |  4 ++--
 io_uring/io_uring.c            | 23 +++++++++++++++++------
 io_uring/tctx.c                |  2 +-
 io_uring/tw.c                  |  2 +-
 4 files changed, 21 insertions(+), 10 deletions(-)

diff --git a/include/linux/io_uring_types.h b/include/linux/io_uring_types.h
index 1b85872bb7ea..7a835113eac3 100644
--- a/include/linux/io_uring_types.h
+++ b/include/linux/io_uring_types.h
@@ -517,8 +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;
+	/* the ctx is freed after a grace period, see io_ring_exit_work() */
+	struct rcu_work			free_rwork;
 	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 06ff99921871..6808229edf07 100644
--- a/io_uring/io_uring.c
+++ b/io_uring/io_uring.c
@@ -2368,7 +2368,15 @@ 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_rcu(ctx, rcu);
+	kfree(ctx);
+}
+
+static __cold void io_ring_ctx_free_work(struct work_struct *work)
+{
+	struct io_ring_ctx *ctx = container_of(to_rcu_work(work),
+					       struct io_ring_ctx, free_rwork);
+
+	io_ring_ctx_free(ctx);
 }
 
 static __cold void io_activate_pollwq_cb(struct callback_head *cb)
@@ -2660,11 +2668,14 @@ static __cold void io_ring_exit_work(struct work_struct *work)
 	spin_lock(&ctx->completion_lock);
 	spin_unlock(&ctx->completion_lock);
 
-	/* pairs with the RCU read sections in the task_work add paths */
-	if (ctx->flags & (IORING_SETUP_DEFER_TASKRUN | IORING_SETUP_SQPOLL))
-		synchronize_rcu();
-
-	io_ring_ctx_free(ctx);
+	/*
+	 * Free the ring after a grace period. This pairs with the RCU read
+	 * sections in the task_work add paths, which look at the rings and
+	 * the submitter task, and with the one in io_uring_del_tctx_node(),
+	 * which may still be unlocking ->tctx_lock.
+	 */
+	INIT_RCU_WORK(&ctx->free_rwork, io_ring_ctx_free_work);
+	queue_rcu_work(iou_wq, &ctx->free_rwork);
 }
 
 static __cold void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx)
diff --git a/io_uring/tctx.c b/io_uring/tctx.c
index c6b7f5647f79..508a72fdfc1b 100644
--- a/io_uring/tctx.c
+++ b/io_uring/tctx.c
@@ -231,7 +231,7 @@ __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() */
+	/* keeps the ctx until the unlock is done, see io_ring_exit_work() */
 	rcu_read_lock();
 	mutex_unlock(&node->ctx->tctx_lock);
 	rcu_read_unlock();
diff --git a/io_uring/tw.c b/io_uring/tw.c
index b4a9e28ce779..41b3b9422d41 100644
--- a/io_uring/tw.c
+++ b/io_uring/tw.c
@@ -184,7 +184,7 @@ void io_req_local_work_add(struct io_kiocb *req, unsigned flags)
 	struct io_ring_ctx *ctx = req->ctx;
 	int nr_wait;
 
-	/* pairs with synchronize_rcu() in io_ring_exit_work() */
+	/* pairs with the grace period before the free in io_ring_exit_work() */
 	guard(rcu)();
 
 	/*
-- 
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 ` [PATCH 03/10] io_uring: free the ring after a grace period Jens Axboe
2026-11-25 18:43 ` Jens Axboe [this message]
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-5-axboe@kernel.dk \
    --to=axboe@kernel.dk \
    --cc=io-uring@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.