From: Jens Axboe <axboe@kernel.dk>
To: stable@vger.kernel.org
Subject: [PATCH 6.1.y 7/8] io_uring/sqpoll: don't wait for the thread to exit
Date: Wed, 4 Nov 2026 11:43:27 -0700 [thread overview]
Message-ID: <20261104184328.516206-8-axboe@kernel.dk> (raw)
In-Reply-To: <20261104184328.516206-1-axboe@kernel.dk>
From: Chris Mason <mason@kernel.org>
Commit 360395e8dbf9d23579f74b84a1af11190813b0f8 upstream.
io_sq_thread_stop() wakes the SQPOLL thread and waits for it to exit.
The thread belongs to the thread group that created the ring, so a
SIGSTOP or the cgroup freezer holds it in get_signal(), and the wake up
doesn't change that. io_ring_exit_work() then sleeps in that wait until
someone continues or kills the process. iou_exit has 64 slots, so 64
closed SQPOLL rings of a stopped process keep every other ring from
being freed, for everyone.
Don't wait. Set the stop bit, wake the thread, and let it exit when it
gets to run again. The sqd can now outlive its last ring, so give it a
second refcount with one reference for the rings and one for the thread,
and free it with the last one. The thread can't reach a freed ring,
rings leave ctx_list before dropping their reference, and the io-wq hash
is refcounted.
Fixes: 16efa4fce3b7 ("io_uring: allow IO worker threads to be frozen")
Signed-off-by: Chris Mason <mason@kernel.org>
Signed-off-by: Jens Axboe <axboe@kernel.dk>
[axboe: 6.1 has a single exit path in io_sq_thread(), drop the thread's
free_refs share there]
---
io_uring/sqpoll.c | 28 +++++++++++++++++++++++-----
io_uring/sqpoll.h | 3 ++-
2 files changed, 25 insertions(+), 6 deletions(-)
diff --git a/io_uring/sqpoll.c b/io_uring/sqpoll.c
index adc01f65464b..2cbe580d76af 100644
--- a/io_uring/sqpoll.c
+++ b/io_uring/sqpoll.c
@@ -52,6 +52,24 @@ void io_sq_thread_park(struct io_sq_data *sqd)
wake_up_process(sqd->thread);
}
+/*
+ * The sqd has two holders: its rings, as one, and its thread. Neither waits
+ * for the other to let go. The one that lets go last frees the sqd.
+ */
+static void io_sq_data_release(struct io_sq_data *sqd)
+{
+ if (refcount_dec_and_test(&sqd->free_refs))
+ kfree(sqd);
+}
+
+/*
+ * Ask the thread to exit, and don't wait for it. A thread that is stopped or
+ * frozen exits only when a user lets it run again, and the exit work of the
+ * ring would keep its place in the workqueue until then. All rings have left
+ * ctx_list by now and have no request left, so the thread can reach none of
+ * them. What it still uses is its own or counted: the sqd by free_refs, the
+ * hash of its io-wq by hash->refs.
+ */
void io_sq_thread_stop(struct io_sq_data *sqd)
{
WARN_ON_ONCE(sqd->thread == current);
@@ -62,7 +80,6 @@ void io_sq_thread_stop(struct io_sq_data *sqd)
if (sqd->thread)
wake_up_process(sqd->thread);
mutex_unlock(&sqd->lock);
- wait_for_completion(&sqd->exited);
}
void io_put_sq_data(struct io_sq_data *sqd)
@@ -71,7 +88,7 @@ void io_put_sq_data(struct io_sq_data *sqd)
WARN_ON_ONCE(atomic_read(&sqd->park_pending));
io_sq_thread_stop(sqd);
- kfree(sqd);
+ io_sq_data_release(sqd);
}
}
@@ -153,10 +170,10 @@ static struct io_sq_data *io_get_sq_data(struct io_uring_params *p,
atomic_set(&sqd->park_pending, 0);
refcount_set(&sqd->refs, 1);
+ refcount_set(&sqd->free_refs, 2);
INIT_LIST_HEAD(&sqd->ctx_list);
mutex_init(&sqd->lock);
init_waitqueue_head(&sqd->wait);
- init_completion(&sqd->exited);
return sqd;
}
@@ -327,7 +344,7 @@ static int io_sq_thread(void *data)
io_run_task_work();
mutex_unlock(&sqd->lock);
- complete(&sqd->exited);
+ io_sq_data_release(sqd);
do_exit(0);
}
@@ -448,7 +465,8 @@ __cold int io_sq_offload_create(struct io_ring_ctx *ctx,
put_task_struct(task_to_put);
return 0;
err_sqpoll:
- complete(&ctx->sq_data->exited);
+ /* no thread was made: let go of its share */
+ io_sq_data_release(ctx->sq_data);
err:
io_sq_thread_finish(ctx);
if (task_to_put)
diff --git a/io_uring/sqpoll.h b/io_uring/sqpoll.h
index 36245f1afa5e..3e41db84014b 100644
--- a/io_uring/sqpoll.h
+++ b/io_uring/sqpoll.h
@@ -2,6 +2,8 @@
struct io_sq_data {
refcount_t refs;
+ /* one for all the rings, one for the thread: the last put frees */
+ refcount_t free_refs;
atomic_t park_pending;
struct mutex lock;
@@ -17,7 +19,6 @@ struct io_sq_data {
pid_t task_tgid;
unsigned long state;
- struct completion exited;
};
int io_sq_offload_create(struct io_ring_ctx *ctx, struct io_uring_params *p);
--
2.56.0
next prev parent reply other threads:[~2026-11-04 18:43 UTC|newest]
Thread overview: 12+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-04 18:43 [PATCH 6.1.y 0/8] io_uring fixes for 6.1-stable Jens Axboe
2026-11-04 18:43 ` [PATCH 6.1.y 1/8] io_uring/net: keep recvmsg buffer select length within MAX_RW_COUNT Jens Axboe
2026-11-04 18:43 ` [PATCH 6.1.y 2/8] io_uring/net: don't retry a non-blocking connect() that fails with -EAGAIN Jens Axboe
2026-11-04 18:43 ` [PATCH 6.1.y 3/8] io_uring/io-wq: no new workers once the exit has begun Jens Axboe
2026-11-04 18:43 ` [PATCH 6.1.y 4/8] io_uring/rsrc: charge buffer arrays and nodes to memcg Jens Axboe
2026-11-04 18:43 ` [PATCH 6.1.y 5/8] io_uring: charge tctx nodes and personalities Jens Axboe
2026-11-04 18:43 ` [PATCH 6.1.y 6/8] io_uring: free XArray nodes left by a failed store Jens Axboe
2026-11-04 18:43 ` Jens Axboe [this message]
2026-11-04 18:43 ` [PATCH 6.1.y 8/8] io_uring/timeout: count cancelled timeouts in cq_timeouts Jens Axboe
2026-11-04 20:51 ` [PATCH 6.1.y 0/8] io_uring fixes for 6.1-stable Greg KH
2026-11-04 20:53 ` Jens Axboe
2026-11-04 21:00 ` Greg KH
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=20261104184328.516206-8-axboe@kernel.dk \
--to=axboe@kernel.dk \
--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