From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.214.227 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795992433; b=z6OTep5hdF87tZn+78nE9z4ujZeNEEBjd4KbVki2MuWCTrmP71DrvEFhj9//mpRHL8gp QXrMqZi5U3ch2ki3n2yGEjhz1f2Oy6BjocG2OH0QgZ7f/WpIXNjpMz6HMmoublo80ioUc VFZi2FELPHCPTN2sx4lM9Q2vAv2Y4qhZLRiJpS3c8Bi/0U1qzYyvAFKPlgvkeLJ2sqgpN KtbEeOCb0Pn+uxU5vhCONxxHGtOPb+dQH4DgTOm9fR7xN9+v+6SDHG9TbYWy7VjoHOnt4 /CsHBOZyX0arpsSfJVSZB8jYBSVyBvodCWdWcp9cJodT9axEL2CLUZWJeXW+BiK4CDA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795992433; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Gm-Gg:X-Received:Received: X-Relaying-Domain:Received:Received:From:To:Cc:Subject:Date: Message-ID:X-Mailer:MIME-Version:Content-Transfer-Encoding; bh=gNDKmichvZUMf4ew/KRMneFKRFd8Z+9pE79W77w3CH0=; b=knRAUqpSkSg33KuS3srbxgF7qagelYcomiPhCPMipZjiO3JOthCGReoKASQfY4HRrfXZ 1E/97mZ7D+luX+4rxuLQ73OBS/kYDZdp+rEj2QQFSUI/5S8gEVY76Yl0H4kp1NV4gY7bZ QVKDfSXuiDr3F5FUEitvzODbMekgf23Zz5UHPkWE9fyVwaszdlspqGIylEzjYdUvs5MCh kvAvOEpqqdmp5N5RRE58LSDOw7fh78Afq9k3Tae/MZooZVUogYrptH3VaDAp+g10DaBpF 2tNJdd+MQrefETXt1994meCNaRAa3KrsCuo/f0ENOLsjf+CUxhVhSvEwJWIMi3CgbdQ== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=purestorage.com; spf=pass smtp.mailfrom=purestorage.com; dkim=pass header.d=purestorage.com header.i=@purestorage.com header.a=rsa-sha256 header.s=google2022 header.b=PDZM+c7A; arc=none smtp.remote-ip=209.85.214.227 Received: from mail-pl1-f227.google.com (mail-pl1-f227.google.com [209.85.214.227]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 5404C606A329 for ; Sun, 29 Nov 2026 22:47:12 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 5404C606A329 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=purestorage.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=purestorage.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=purestorage.com header.i=@purestorage.com header.a=rsa-sha256 header.s=google2022 header.b=PDZM+c7A Received: by mail-pl1-f227.google.com with SMTP id d9443c01a7336-2eb33b5c067so9810575ad.1 for ; Sun, 29 Nov 2026 14:47:12 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=purestorage.com; s=google2022; t=1795992431; x=1796597231; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to:content-type; bh=gNDKmichvZUMf4ew/KRMneFKRFd8Z+9pE79W77w3CH0=; b=PDZM+c7AxL8yIwcg8A/cDM/VG7esppZkpk3akz8/Q7Snt11GvexLgY41POf1Px8GrJ Kky+G9vFn1nELspKsNThKwBnxlex/Y4YCX+3ZypBFqTj3qav2pZWvqCbGrGi6C3yuizF 7A4ho0efmj+JMX7IZdnqV9OkgJA94Pq9A4lhIvWaUrraDf6i7MP3mwAmcCLquI3gzwl9 aulJT47fl8For4H+61Tdb+yTI38At92Tv4vxmEiTqaKs+9wYwy9dHRo4BuQ9oiI7tp4l phcGNo0STacF/dphXD8r+iMs++HjdS1pOnrbrm5nCLKHjPEBjyiE76c85uYf4nM/6vJA Zk8A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1795992431; x=1796597231; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=gNDKmichvZUMf4ew/KRMneFKRFd8Z+9pE79W77w3CH0=; b=pe18ge0QcSHTmVSLm0pwHuIMFa9g0totgix9mbGLH5J5Gs6tqQwtsEwNQDcKQ6qnJ8 xRnxmunOLHoFIkquLt8QLVCrd5qafcuNpks0xeWiaz8oW8KrEXSd0Y7BkkOK4fLHSTCo 6z3Ls3Y0cOy1uYMjeXLII3vYXR8bfNoW7e0ydGHOdJZnHDzcfOgKBeyqOWh/pKIiwzc+ LPQJlxPlPdpqv6H4mmxwWKdnZEhP7Ryf4iwDjPUrv5FLWbB2eCqHXnsBso0STX7Gy4N5 SyonM2WfxtjNkwwbrRJ9IRtkZkNWMbkbR8/SUVjC5HghpC/cVivBg6FdPvb1f/t9ldMK Z14Q== X-Forwarded-Encrypted: i=1; AL+hErG2MjeD4npJ+R4disrOIMeHH1xTiS7ApQWlqkqZL5vJDC/+D67N2YUic5DUFydSkW4Hp7z7esTT2g==@vger.kernel.org X-Gm-Message-State: APuU4Z8aEzs4Ew4rJ1h7h8VLjrk16g0oDMIQm5H/lZKFNIL8vRsjfAuc j5WvEgIVXqcYdoopiHzOnCj9+1Oa3N4wgUCDSGRopIpzIhiNrlzZQKzdDkUHWNSZGl76lzXQ09x m11optYQwaMNLF/DtruIMamhCOPpG8x3oSpo1j7i3Bi5M3dLxrcaK X-Gm-Gg: AZFdTZfZbXS6c4nY8NR0ITOYS1sulFNGgvm8dUjfy39Om1WlZMKmF6scNdhFCuS7kYH VEpxFhiaw3yw190OY+UOY8KC4sM0gHXZ2cMWKC7hnELZu6bXYeJbYdeImgyalaVs30hS/JXqFDm mXUdsuAbzZGvvaqQhQOVNkfThuV7UMrfERX1yECJH/ntC3LRpZniK9iJfcIZ7GziE1Pvcbon+Lz czpFbM79t7K0m4LYUVaCk60XXrA6iN6THvg5wMB5+mi2qgORZlNQj/tJzuieHXWVwnl5b/SKhDP G+jJwpoiP9B8NP1BKCn9woM+/KLOF6VLbRfUhsM64OHH2Rdl3EZkFHYTIoRSNAuS+sw35iMU/cM ht6vgFTmsoS5kOu36Rw== X-Received: by 2002:a17:90b:2701:b0:3b3:ab75:c947 with SMTP id 98e67ed59e1d1-3b41c7b60aamr46946885a91.1.1795992431510; Sun, 29 Nov 2026 14:47:11 -0800 (PST) Received: from c7-smtp-2026.dev.purestorage.com ([2620:125:9017:12:36:3:6:0]) by smtp-relay.gmail.com with ESMTPS id 98e67ed59e1d1-3b411775fa6sm21950592a91.1.2026.11.29.14.47.11 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 29 Nov 2026 14:47:11 -0800 (PST) X-Relaying-Domain: purestorage.com Received: from dev-csander.dev.purestorage.com (dev-csander.dev.purestorage.com [10.17.2.4]) by c7-smtp-2026.dev.purestorage.com (Postfix) with ESMTP id 9B5804014C; Sun, 29 Nov 2026 15:47:10 -0700 (MST) Received: by dev-csander.dev.purestorage.com (Postfix, from userid 1557716354) id 945DBE402D8; Sun, 29 Nov 2026 15:47:10 -0700 (MST) From: Caleb Sander Mateos To: Jens Axboe Cc: Caleb Sander Mateos , io-uring@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH] io_uring: only run task work once for SETUP_DEFER_TASKRUN Date: Sun, 29 Nov 2026 15:47:04 -0700 Message-ID: <20261129224706.822216-1-csander@purestorage.com> X-Mailer: git-send-email 2.56.1 X-Mailing-List: io-uring@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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 --- 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