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=iIJDUYKoZ1xZ9kDo3bxggVoxa962R0cqq/yir40NnzQ59PJtcE6yGXUu5wNdSpXWk1Lt TIlIdQlpCfAO16ALnyIF5EWcG0VYaQmDz8v6RNeN4HcjXeYTt1WWm2Qy6EYjziG1rQeMj fEYWbicBVPOqCsdQIAeTthnVBK0BfhlXlUpfuHOCSeTCSzyWkYMt7x9MIwlQrS2H1j3Qp gMXwVsdsLfHAL3fcZA5ObFt7mAn9gTFTus3/qisyePS6yLD1LNB57TBFau4B76prtGqSd Ihi3rhGGyTlCH0DnKmaTZi/lfqpD4UWpwFIHddU1FdUmxWfC4oV0BCZi8z9csF/mGJQ== 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=SNFvuKsZTOF8ShMRCFAmjmTF1hUvjKTdpO97kktiUEzL3tdK6zZr1dImN0FZsdmWJD2f DC8GMwfX6iTaxlZ/YzPcCPahholQBvhk3vohnzE4ko2t2sqnm5foOmxEwMIbYsK31BLGX TBtt9TeH+eCm4DUcvj3G8CCXZL0TMwGQjqCEIKSpNiA6f2aPZ7t4KYw6oKYfen3axjg1p BspeZ3xEsnAMNxvC+lLYbinVJyrhc70EYkKm5BoURGGt8AuomDM3gl3JGIxCqky2mttt0 RjS8XXYVya2y/odAV2yUtEHwEjoS190aMh/iN0KgL0R+yJ8oSr6lWkLswfMzAhNA66A== 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 53FAF606A327 for ; Sun, 29 Nov 2026 22:47:12 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 53FAF606A327 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, unprotected) 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-2f0d23553b1so6216405ad.2 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=p0A3wjIO0S1W3E6NGm4KuvTc0BQoxKi9fBoq+TvVTElO4G4tr1nkxtDTO2u9yyWG+j sWisPXx4DbHzCxOYF+5OjnxJdOJf7scST6KLjVNjTNP9TMZ0PR7T2uIq9FD3bkVJ1MyA 3y/KhgrWjBXBVLafabgb3YfZbh0XVMFHEOPOujVPVUTioC7BKFnUaPY0xKqQujTbk5eK 1cq55rtzSX2vX9kjXxjc/jrhP6jTiLB4r6kCHlQIN/RNsECU7leTja8gVPv2pOOfc4Xh X6uvoLCFu2WsU55NyJTllNBb/r16ZC1+DswWbGzahElPZUB2tVP8hK9H8vXwM6KX8KjY yTTw== X-Forwarded-Encrypted: i=1; AL+hErE+p56ASKPLjnbs1YYPlSlZ9gbWWLn43cW9SY65aTFgObS/bNNX/bO1JjcUnDqlHCWX25+DcDeaGaFG+Ac=@vger.kernel.org X-Gm-Message-State: APuU4Z8Mi/MjNzBk6FkGXTnbLxggcg8G2KMLo+24jDRvpgCPKKPYJjc0 mMGXCILGROil2oW0NbG8cljQ4W1PNZ9+Zd95vyTMiiR5BxjBJ66sCBtEQ+TxMjagkVGWaX4RT1f Y4TbiFzJAkEqIszE0bzk7QqIzhVz503RQVAEU09S1NkejxXyLpFRO X-Gm-Gg: AZFdTZcuaRpeiJbSlsWXJCv/v2DSr2M/XscbFpO1f378rzWVhnZSMvD5iGdWfIAe4vl ZiXgDTqI05xKd8ShMfQSD1xc6nNFmJhB6Z9b4cJCo9Od2s8LaO16jPy92KlB592EEhv6CwPP2qa FA1lrG1cMe+DcdeXrwhPSnTLTEzJnBA8OU6Irp6MFA4zOvz98VNFHO8RuUb2EjBaHp23LfWeurb XM/zBH1dcYSXf/SmeV5HEEfIFSjW1zPGAgaBsy6ZfGpCTBc39dCSuq8lPoNDhEC1Cpq8DTq7loQ ILJx8/Vo+pcP3MHBfvybbwWimGLMXxyxebHJ3WR/eFmjFYQAdQGjchBoV/0x7zIKLIOfK7vjXg1 k4sErd6FtXkNVqWY6Og== 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: linux-kernel@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