Linux io-uring development
 help / color / mirror / Atom feed
From: Artem Andreenko <miolini@gmail.com>
To: Pavel Begunkov <asml.silence@gmail.com>, Jens Axboe <axboe@kernel.dk>
Cc: io-uring@vger.kernel.org, Artem Andreenko <miolini@gmail.com>
Subject: [RFC PATCH] io_uring/loop: add timeout support for BPF event loops
Date: Fri, 27 Nov 2026 12:25:02 -0800	[thread overview]
Message-ID: <20261127202501.14163-2-miolini@gmail.com> (raw)

Currently io_loop_wait() sleeps unconditionally until lp->cq_wait_idx is
reached, local work arrives, or a signal is pending. This makes it awkward
for BPF loop_step programs to implement adaptive min_wait, batching
deadlines, or periodic ticks without allocating and canceling
IORING_OP_TIMEOUT requests (which also pollute the CQ ring on expiry).

Extend struct iou_loop_params with:
- @flags: input control flags (currently IOU_LOOP_F_TIMEOUT_ABS; unknown
  bits fail the loop with -EINVAL).
- @timeout_ns: optional wait timeout in nanoseconds (0 with
  IOU_LOOP_F_TIMEOUT_ABS unset keeps the existing infinite-wait path).
- @wait_res: read-only output status populated by the kernel (0 on event
  wakeup or no sleep, -ETIME when the wait expired due to @timeout_ns).

Wait expiration uses schedule_hrtimeout_range_clock() against a snapshot
of ctx->clockid taken under uring_lock (with timens_ktime_to_host() for
absolute timestamps), only after checking for pending local work and
ready CQEs so the non-sleeping fast path does no extra clock reads.

Tested on arm64 QEMU with CONFIG_IO_URING_BPF_OPS=y, PROVE_LOCKING, and
DEBUG_ATOMIC_SLEEP covering relative timeouts, early CQE wakeups, future
and already-expired absolute timeouts, wait_res reset across iterations,
and invalid flag rejection.

Signed-off-by: Artem Andreenko <miolini@gmail.com>
---
Tool-assisted content note (per Documentation/process/generated-content.rst):
An AI coding assistant (Google Antigravity / Gemini) assisted with initial
source exploration, drafting the patch/changelog, and writing the standalone
arm64 QEMU BPF test harness. All changes were reviewed, audited for locking
and verifier constraints, built with W=1 (BPF_OPS=y and =n), and runtime-tested
under PROVE_LOCKING and DEBUG_ATOMIC_SLEEP.

Open questions for RFC feedback:
1. Should @timeout_ns / @flags persist across loop_step() calls (matching
   @cq_wait_idx) or be reset to 0 after each wait?
2. Is read-only @wait_res (-ETIME / 0) preferred over a status flag, and
   where would you prefer the selftest to live (tools/testing/selftests/bpf/
   or liburing)?

 io_uring/bpf-ops.c |  2 +-
 io_uring/loop.c    | 36 +++++++++++++++++++++++++++++++++++-
 io_uring/loop.h    | 22 ++++++++++++++++++++++
 3 files changed, 58 insertions(+), 2 deletions(-)

diff --git a/io_uring/bpf-ops.c b/io_uring/bpf-ops.c
index 6c2a587fb..b7cfe7cd6 100644
--- a/io_uring/bpf-ops.c
+++ b/io_uring/bpf-ops.c
@@ -107,7 +107,7 @@ static int bpf_io_btf_struct_access(struct bpf_verifier_log *log,
 	const struct btf_type *t = btf_type_by_id(reg->btf, reg->btf_id);
 
 	if (t == loop_params_type) {
-		if (off + size <= offsetofend(struct iou_loop_params, cq_wait_idx))
+		if (off + size <= offsetofend(struct iou_loop_params, timeout_ns))
 			return SCALAR_VALUE;
 	}
 
diff --git a/io_uring/loop.c b/io_uring/loop.c
index 234045916..a5de19dda 100644
--- a/io_uring/loop.c
+++ b/io_uring/loop.c
@@ -1,4 +1,7 @@
 /* SPDX-License-Identifier: GPL-2.0 */
+#include <linux/hrtimer.h>
+#include <linux/time_namespace.h>
+
 #include "io_uring.h"
 #include "wait.h"
 #include "loop.h"
@@ -19,6 +22,12 @@ static inline void io_loop_wait_finish(struct io_ring_ctx *ctx)
 static void io_loop_wait(struct io_ring_ctx *ctx, struct iou_loop_params *lp,
 			 unsigned nr_wait)
 {
+	enum hrtimer_mode mode = HRTIMER_MODE_REL;
+	clockid_t clockid = ctx->clockid;
+	bool has_timeout;
+	ktime_t expires;
+	int ret = -EINTR;
+
 	io_cq_wait_arm(ctx, nr_wait);
 
 	if (unlikely(io_local_work_pending(ctx) ||
@@ -28,10 +37,31 @@ static void io_loop_wait(struct io_ring_ctx *ctx, struct iou_loop_params *lp,
 		return;
 	}
 
+	has_timeout = lp->timeout_ns || (lp->flags & IOU_LOOP_F_TIMEOUT_ABS);
+	if (has_timeout) {
+		expires = ns_to_ktime(min_t(u64, lp->timeout_ns, KTIME_MAX));
+		if (lp->flags & IOU_LOOP_F_TIMEOUT_ABS) {
+			expires = timens_ktime_to_host(clockid, expires);
+			if (ktime_compare(io_get_time(ctx), expires) >= 0) {
+				io_loop_wait_finish(ctx);
+				lp->wait_res = -ETIME;
+				return;
+			}
+			mode = HRTIMER_MODE_ABS;
+		}
+	}
+
 	mutex_unlock(&ctx->uring_lock);
-	schedule();
+	if (has_timeout)
+		ret = schedule_hrtimeout_range_clock(&expires, 0, mode,
+						     clockid);
+	else
+		schedule();
 	io_loop_wait_finish(ctx);
 	mutex_lock(&ctx->uring_lock);
+
+	if (!ret)
+		lp->wait_res = -ETIME;
 }
 
 static int __io_run_loop(struct io_ring_ctx *ctx)
@@ -50,6 +80,10 @@ static int __io_run_loop(struct io_ring_ctx *ctx)
 		if (step_res != IOU_LOOP_CONTINUE)
 			return -EINVAL;
 
+		if (unlikely(lp.flags & ~IOU_LOOP_F_MASK))
+			return -EINVAL;
+
+		lp.wait_res = 0;
 		nr_wait = io_loop_nr_cqes(ctx, &lp);
 		if (nr_wait > 0)
 			io_loop_wait(ctx, &lp, nr_wait);
diff --git a/io_uring/loop.h b/io_uring/loop.h
index 4dd4fb3ae..39bacc389 100644
--- a/io_uring/loop.h
+++ b/io_uring/loop.h
@@ -4,12 +4,34 @@
 
 #include <linux/io_uring_types.h>
 
+enum {
+	IOU_LOOP_F_TIMEOUT_ABS		= 1U << 0,
+};
+
+#define IOU_LOOP_F_MASK			IOU_LOOP_F_TIMEOUT_ABS
+
 struct iou_loop_params {
 	/*
 	 * The CQE index to wait for. Only serves as a hint and can still be
 	 * woken up earlier.
 	 */
 	__u32			cq_wait_idx;
+	/*
+	 * Wait control flags (IOU_LOOP_F_*).
+	 */
+	__u32			flags;
+	/*
+	 * Optional wait timeout in nanoseconds. Zero with
+	 * IOU_LOOP_F_TIMEOUT_ABS unset disables the timeout. Persists across
+	 * loop_step() iterations until modified by the BPF program.
+	 */
+	__u64			timeout_ns;
+	/*
+	 * Output status from the previous wait (read-only for BPF):
+	 * 0 if woken by events/work or no wait occurred, -ETIME if the wait
+	 * expired due to @timeout_ns.
+	 */
+	__s32			wait_res;
 };
 
 enum {
-- 
2.54.0 (Apple Git-157)


                 reply	other threads:[~2026-11-27 20:25 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=20261127202501.14163-2-miolini@gmail.com \
    --to=miolini@gmail.com \
    --cc=asml.silence@gmail.com \
    --cc=axboe@kernel.dk \
    --cc=io-uring@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