mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH] io_uring: only run task work once for SETUP_DEFER_TASKRUN
@ 2026-11-29 22:47 Caleb Sander Mateos
  0 siblings, 0 replies; only message in thread
From: Caleb Sander Mateos @ 2026-11-29 22:47 UTC (permalink / raw)
  To: Jens Axboe; +Cc: Caleb Sander Mateos, io-uring, linux-kernel

An io_uring_enter() syscall on an IORING_SETUP_DEFER_TASKRUN ring with
to_submit and IORING_ENTER_GETEVENTS set will execute up to
max(IO_LOCAL_TW_DEFAULT_MAX, min_events) task work items while the
uring_lock is held for io_submit_sqes(). This optimization was
implemented in commit 44f87745d5f2 ("io_uring: optimise locking for
local tw with submit_wait") to avoid acquiring the uring_lock mutex a
second time for the task work. However, io_cqring_wait() is still called
afterwards. It calls io_local_work_pending(), which loads the contended
mpscq tail again. And if any task work has been queued concurrently (or
is in the middle of the two-step mpscq push), io_run_local_work() is
called, which takes the uring_lock again to execute the task work.

Avoid the redundant mpscq check and uring_lock acquisition, which are
quite expensive on workloads with high task work volume. Pass a bool
tw_ran to io_cqring_wait() to indicate whether task work already ran.
Only running task work once can add latency to the small fraction of
task work items arriving shortly after, but returning faster from the
syscall provides latency savings for all other work on the thread.

Remove the comment in io_uring_enter() about io_run_local_work_locked()
errors. io_run_local_work_locked() can only fail in the warning case
that the current task isn't submitter_task, which has already been
checked by the earlier io_uring_add_tctx_node() call.

Signed-off-by: Caleb Sander Mateos <csander@purestorage.com>
---
 io_uring/io_uring.c | 11 +++++------
 io_uring/wait.c     |  4 ++--
 io_uring/wait.h     |  2 +-
 3 files changed, 8 insertions(+), 9 deletions(-)

diff --git a/io_uring/io_uring.c b/io_uring/io_uring.c
index 38de85e76b3d..5c21f0a0d21f 100644
--- a/io_uring/io_uring.c
+++ b/io_uring/io_uring.c
@@ -2868,10 +2868,11 @@ struct file *io_uring_ctx_get_file(unsigned int fd, bool registered)
 SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit,
 		u32, min_complete, u32, flags, const void __user *, argp,
 		size_t, argsz)
 {
 	struct io_ring_ctx *ctx;
+	bool tw_ran = false;
 	struct file *file;
 	long ret;
 
 	if (unlikely(flags & ~IORING_ENTER_FLAGS))
 		return -EINVAL;
@@ -2922,16 +2923,14 @@ SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit,
 			goto out;
 		}
 		if (flags & IORING_ENTER_GETEVENTS) {
 			if (ctx->int_flags & IO_RING_F_SYSCALL_IOPOLL)
 				goto iopoll_locked;
-			/*
-			 * Ignore errors, we'll soon call io_cqring_wait() and
-			 * it should handle ownership problems if any.
-			 */
-			if (ctx->flags & IORING_SETUP_DEFER_TASKRUN)
+			if (ctx->flags & IORING_SETUP_DEFER_TASKRUN) {
 				(void)io_run_local_work_locked(ctx, min_complete);
+				tw_ran = true;
+			}
 		}
 		mutex_unlock(&ctx->uring_lock);
 	}
 
 	if (flags & IORING_ENTER_GETEVENTS) {
@@ -2954,11 +2953,11 @@ SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit,
 			struct ext_arg ext_arg = { .argsz = argsz };
 
 			ret2 = io_get_ext_arg(ctx, flags, argp, &ext_arg);
 			if (likely(!ret2))
 				ret2 = io_cqring_wait(ctx, min_complete, flags,
-						      &ext_arg);
+						      &ext_arg, tw_ran);
 		}
 
 		if (!ret) {
 			ret = ret2;
 
diff --git a/io_uring/wait.c b/io_uring/wait.c
index 367d49d631ec..5ccaeba4d3c9 100644
--- a/io_uring/wait.c
+++ b/io_uring/wait.c
@@ -211,22 +211,22 @@ static inline int io_cqring_wait_schedule(struct io_ring_ctx *ctx,
 /*
  * Wait until events become available, if we don't already have some. The
  * application must reap them itself, as they reside on the shared cq ring.
  */
 int io_cqring_wait(struct io_ring_ctx *ctx, int min_events, u32 flags,
-		   struct ext_arg *ext_arg)
+		   struct ext_arg *ext_arg, bool tw_ran)
 {
 	struct io_wait_queue iowq;
 	struct io_rings *rings;
 	ktime_t start_time;
 	int ret, nr_wait;
 
 	min_events = min_t(int, min_events, ctx->cq_entries);
 
 	if (!io_allowed_run_tw(ctx))
 		return -EEXIST;
-	if (io_local_work_pending(ctx))
+	if (!tw_ran && io_local_work_pending(ctx))
 		io_run_local_work(ctx, min_events,
 				  max(IO_LOCAL_TW_DEFAULT_MAX, min_events));
 	io_run_task_work();
 
 	if (unlikely(test_bit(IO_CHECK_CQ_OVERFLOW_BIT, &ctx->check_cq)))
diff --git a/io_uring/wait.h b/io_uring/wait.h
index eb5ae8a6fc94..c7a4e5b85c44 100644
--- a/io_uring/wait.h
+++ b/io_uring/wait.h
@@ -37,11 +37,11 @@ struct ext_arg {
 	bool ts_set;
 	bool iowait;
 };
 
 int io_cqring_wait(struct io_ring_ctx *ctx, int min_events, u32 flags,
-		   struct ext_arg *ext_arg);
+		   struct ext_arg *ext_arg, bool tw_ran);
 int io_run_task_work_sig(struct io_ring_ctx *ctx);
 void io_cqring_do_overflow_flush(struct io_ring_ctx *ctx);
 void io_cqring_overflow_flush_locked(struct io_ring_ctx *ctx);
 
 static inline unsigned int __io_cqring_events(struct io_ring_ctx *ctx)

base-commit: 33e97a6c608064795dc5e36643a9cc66d98bfa99
-- 
2.56.1


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2026-11-29 22:47 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-29 22:47 [PATCH] io_uring: only run task work once for SETUP_DEFER_TASKRUN Caleb Sander Mateos

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®