public inbox for io-uring@vger.kernel.org
 help / color / mirror / Atom feed
From: pgray <hi@pgray.dev>
To: Jens Axboe <axboe@kernel.dk>
Cc: Pavel Begunkov <asml.silence@gmail.com>,
	David Wei <dw@davidwei.uk>,
	io-uring@vger.kernel.org, linux-kernel@vger.kernel.org,
	pgray <hi@pgray.dev>
Subject: [PATCH] io_uring/tw: avoid local task work starvation
Date: Wed, 30 Sep 2026 13:15:35 +0000	[thread overview]
Message-ID: <20260930131446.90741-1-hi@pgray.dev> (raw)

IO_LOCAL_TW_DEFAULT_MAX (20) is too small for massive backlogs

this patch fixes by:
* not counting passive completions against budget
* add a ceiling of 512: IO_LOCAL_TW_COMPL_MAX
* clearing IORING_SQ_TASKRUN when local queue empty

Link: https://github.com/axboe/liburing/issues/1645
Fixes: f46b9cdb22f7 ("io_uring: limit local tw done")

Signed-off-by: pgray <hi@pgray.dev>
---
 io_uring/tw.c | 21 ++++++++++++++++-----
 1 file changed, 16 insertions(+), 5 deletions(-)

diff --git a/io_uring/tw.c b/io_uring/tw.c
index f573bcc3af6a..236ed01fcfaa 100644
--- a/io_uring/tw.c
+++ b/io_uring/tw.c
@@ -16,6 +16,8 @@
 #include "wait.h"
 #include "mpscq.h"
 
+#define IO_LOCAL_TW_COMPL_MAX		512
+
 static void ctx_flush_and_put(struct io_ring_ctx *ctx, io_tw_token_t tw)
 {
 	if (!ctx)
@@ -287,17 +289,23 @@ static int __io_run_local_work_loop(struct io_ring_ctx *ctx,
 				    int events)
 {
 	int ret = 0;
+	int nr_events = 0;
 
-	while (ret < events) {
+	while (nr_events < events && ret < IO_LOCAL_TW_COMPL_MAX) {
 		struct llist_node *node = mpscq_pop(&ctx->work_list, &ctx->work_head);
 		struct io_kiocb *req;
 
 		if (!node)
 			break;
 		req = container_of(node, struct io_kiocb, io_task_work.node);
-		INDIRECT_CALL_2(req->io_task_work.func,
-				io_poll_task_func, io_req_rw_complete,
-				(struct io_tw_req){req}, tw);
+		if (req->io_task_work.func == io_req_task_complete) {
+			io_req_task_complete((struct io_tw_req){req}, tw);
+		} else {
+			INDIRECT_CALL_2(req->io_task_work.func,
+					io_poll_task_func, io_req_rw_complete,
+					(struct io_tw_req){req}, tw);
+			nr_events++;
+		}
 		ret++;
 	}
 
@@ -342,8 +350,11 @@ int io_run_local_work_locked(struct io_ring_ctx *ctx, int min_events)
 {
 	struct io_tw_state ts = {};
 
-	if (!io_local_work_pending(ctx))
+	if (!io_local_work_pending(ctx)) {
+		if (ctx->flags & IORING_SETUP_TASKRUN_FLAG)
+			atomic_andnot(IORING_SQ_TASKRUN, &ctx->rings->sq_flags);
 		return 0;
+	}
 	return __io_run_local_work(ctx, ts, min_events,
 					max(IO_LOCAL_TW_DEFAULT_MAX, min_events));
 }
-- 
2.55.0



                 reply	other threads:[~2026-09-30 13:15 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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=20260930131446.90741-1-hi@pgray.dev \
    --to=hi@pgray.dev \
    --cc=asml.silence@gmail.com \
    --cc=axboe@kernel.dk \
    --cc=dw@davidwei.uk \
    --cc=io-uring@vger.kernel.org \
    --cc=linux-kernel@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