Linux io-uring development
 help / color / mirror / Atom feed
* [RFC PATCH 0/2] io_uring: add CLOCK_TAI support for waits and timeouts
@ 2026-11-27 21:20 Artem Andreenko
  2026-11-27 21:20 ` [RFC PATCH 1/2] io_uring: snapshot clockid per wait and drop clock_offset Artem Andreenko
  2026-11-27 21:20 ` [RFC PATCH 2/2] io_uring: add CLOCK_TAI support for waits and timeouts Artem Andreenko
  0 siblings, 2 replies; 3+ messages in thread
From: Artem Andreenko @ 2026-11-27 21:20 UTC (permalink / raw)
  To: Jens Axboe, Pavel Begunkov; +Cc: io-uring, Artem Andreenko, linux-api

This series adds CLOCK_TAI support to io_uring for both
IORING_REGISTER_CLOCK (ring wait timers) and IORING_OP_TIMEOUT /
IORING_OP_LINK_TIMEOUT (via IORING_TIMEOUT_TAI).

5G NR gNodeB DU/CU radio-slot loops, O-RAN 7.2x fronthaul, and TSN
workloads anchor periodic activations and TX deadlines to PTP-steered
CLOCK_TAI (matching SO_TXTIME, sch_etf, and bpf_ktime_get_tai_ns()).
Adding CLOCK_TAI directly to io_uring avoids per-slot userspace clock
conversion or separate timerfd(CLOCK_TAI) syscalls.

Patch 1/2 removes the redundant ctx->clock_offset field (deriving the
timekeeper offset from ctx->clockid) and snapshots iowq.clockid once per
io_cqring_wait() cycle, fixing a pre-existing data race between
IORING_REGISTER_CLOCK and lockless waiters (which becomes severe once
CLOCK_TAI and CLOCK_MONOTONIC differ by ~56 years).

Patch 2/2 enables CLOCK_TAI in IORING_REGISTER_CLOCK and adds
IORING_TIMEOUT_TAI (bit 8) to IORING_TIMEOUT_CLOCK_MASK.

Tool-assisted content note (per Documentation/process/generated-content.rst):
An AI coding assistant (Google Antigravity / Gemini) assisted with source
analysis, drafting the patches/changelogs, and writing the standalone
arm64 QEMU test harness. Both patches were built with W=1 and runtime-tested
on arm64 QEMU with PROVE_LOCKING, DEBUG_ATOMIC_SLEEP, and a 37-second
ADJ_TAI offset.

Open question for RFC feedback:
Should IORING_REGISTER_CLOCK(CLOCK_TAI) and IORING_TIMEOUT_TAI be split
into separate patches or kept together as in Patch 2/2?

Artem Andreenko (2):
  io_uring: snapshot clockid per wait and drop clock_offset
  io_uring: add CLOCK_TAI support for waits and timeouts

 include/linux/io_uring_types.h |  1 -
 include/uapi/linux/io_uring.h  |  5 ++++-
 io_uring/io_uring.c            |  1 -
 io_uring/io_uring.h            | 13 +++++++++----
 io_uring/napi.c                |  3 ++-
 io_uring/register.c            |  6 ++----
 io_uring/timeout.c             |  2 ++
 io_uring/wait.c                |  9 +++++----
 8 files changed, 24 insertions(+), 16 deletions(-)


base-commit: 822ba3a89c7896b26e4beb70e2ebffb881924dfe
-- 
2.54.0 (Apple Git-157)


^ permalink raw reply	[flat|nested] 3+ messages in thread

* [RFC PATCH 1/2] io_uring: snapshot clockid per wait and drop clock_offset
  2026-11-27 21:20 [RFC PATCH 0/2] io_uring: add CLOCK_TAI support for waits and timeouts Artem Andreenko
@ 2026-11-27 21:20 ` Artem Andreenko
  2026-11-27 21:20 ` [RFC PATCH 2/2] io_uring: add CLOCK_TAI support for waits and timeouts Artem Andreenko
  1 sibling, 0 replies; 3+ messages in thread
From: Artem Andreenko @ 2026-11-27 21:20 UTC (permalink / raw)
  To: Jens Axboe, Pavel Begunkov; +Cc: io-uring, Artem Andreenko

io_register_clock() updates ctx->clock_offset and ctx->clockid under
ctx->uring_lock, while io_cqring_wait() and __io_napi_busy_loop() read
them without holding uring_lock. A concurrent IORING_REGISTER_CLOCK call
can cause a waiter to observe mismatched clockid and clock_offset values
or switch clocks mid-wait.

Since the timekeeper offset is a direct 1:1 function of ctx->clockid,
drop ctx->clock_offset from struct io_ring_ctx, pass clockid directly to
io_get_time(), update ctx->clockid with WRITE_ONCE(), and snapshot
iowq.clockid = READ_ONCE(ctx->clockid) once per io_cqring_wait() cycle so
start_time, timens conversion, NAPI busy polling, and hrtimer setup all
use a consistent clock.

Signed-off-by: Artem Andreenko <miolini@gmail.com>
---
 include/linux/io_uring_types.h |  1 -
 io_uring/io_uring.c            |  1 -
 io_uring/io_uring.h            | 11 +++++++----
 io_uring/napi.c                |  3 ++-
 io_uring/register.c            |  5 +----
 io_uring/wait.c                |  9 +++++----
 6 files changed, 15 insertions(+), 15 deletions(-)

diff --git a/include/linux/io_uring_types.h b/include/linux/io_uring_types.h
index 1468bcb02..3ffece450 100644
--- a/include/linux/io_uring_types.h
+++ b/include/linux/io_uring_types.h
@@ -347,7 +347,6 @@ struct io_ring_ctx {
 		struct percpu_ref	refs;
 
 		clockid_t		clockid;
-		enum tk_offsets		clock_offset;
 
 		enum task_work_notify_mode	notify_method;
 		unsigned			sq_thread_idle;
diff --git a/io_uring/io_uring.c b/io_uring/io_uring.c
index be5a4f950..e44bf416b 100644
--- a/io_uring/io_uring.c
+++ b/io_uring/io_uring.c
@@ -3252,7 +3252,6 @@ static __cold int io_uring_create(struct io_ctx_config *config)
 		return -ENOMEM;
 
 	ctx->clockid = CLOCK_MONOTONIC;
-	ctx->clock_offset = 0;
 
 	if (!(ctx->flags & IORING_SETUP_NO_SQARRAY))
 		static_branch_deferred_inc(&io_key_has_sqarray);
diff --git a/io_uring/io_uring.h b/io_uring/io_uring.h
index c8592f4ef..b2808222e 100644
--- a/io_uring/io_uring.h
+++ b/io_uring/io_uring.h
@@ -134,6 +134,7 @@ struct io_wait_queue {
 	unsigned cq_min_tail;
 	unsigned nr_timeouts;
 	int hit_timeout;
+	clockid_t clockid;
 	ktime_t min_timeout;
 	ktime_t timeout;
 	struct hrtimer t;
@@ -578,12 +579,14 @@ static inline bool io_is_uring_cmd(const struct io_kiocb *req)
 	       req->opcode == IORING_OP_URING_CMD128;
 }
 
-static inline ktime_t io_get_time(struct io_ring_ctx *ctx)
+static inline ktime_t io_get_time(clockid_t clockid)
 {
-	if (ctx->clockid == CLOCK_MONOTONIC)
+	switch (clockid) {
+	case CLOCK_BOOTTIME:
+		return ktime_get_boottime();
+	default:
 		return ktime_get();
-
-	return ktime_get_with_offset(ctx->clock_offset);
+	}
 }
 
 enum {
diff --git a/io_uring/napi.c b/io_uring/napi.c
index 0724c6087..2871b416a 100644
--- a/io_uring/napi.c
+++ b/io_uring/napi.c
@@ -367,7 +367,8 @@ void __io_napi_busy_loop(struct io_ring_ctx *ctx, struct io_wait_queue *iowq)
 
 	iowq->napi_busy_poll_dt = READ_ONCE(ctx->napi_busy_poll_dt);
 	if (iowq->timeout != KTIME_MAX) {
-		ktime_t dt = ktime_sub(iowq->timeout, io_get_time(ctx));
+		ktime_t dt = ktime_sub(iowq->timeout,
+				       io_get_time(iowq->clockid));
 
 		/* an absolute timeout may have passed already */
 		dt = max_t(ktime_t, dt, 0);
diff --git a/io_uring/register.c b/io_uring/register.c
index 62f492e64..802cfd6a9 100644
--- a/io_uring/register.c
+++ b/io_uring/register.c
@@ -452,16 +452,13 @@ static int io_register_clock(struct io_ring_ctx *ctx,
 
 	switch (reg.clockid) {
 	case CLOCK_MONOTONIC:
-		ctx->clock_offset = 0;
-		break;
 	case CLOCK_BOOTTIME:
-		ctx->clock_offset = TK_OFFS_BOOT;
 		break;
 	default:
 		return -EINVAL;
 	}
 
-	ctx->clockid = reg.clockid;
+	WRITE_ONCE(ctx->clockid, reg.clockid);
 	return 0;
 }
 
diff --git a/io_uring/wait.c b/io_uring/wait.c
index 367d49d63..584f98b1a 100644
--- a/io_uring/wait.c
+++ b/io_uring/wait.c
@@ -170,7 +170,7 @@ static int __io_cqring_wait_schedule(struct io_ring_ctx *ctx,
 	 * only fire to return -ETIME. NAPI busy polling gets here like that.
 	 */
 	if (iowq->timeout != KTIME_MAX &&
-	    ktime_compare(io_get_time(ctx), iowq->timeout) >= 0)
+	    ktime_compare(io_get_time(iowq->clockid), iowq->timeout) >= 0)
 		return -ETIME;
 
 	/*
@@ -181,7 +181,7 @@ static int __io_cqring_wait_schedule(struct io_ring_ctx *ctx,
 	if (ext_arg->iowait && current_pending_io())
 		current->in_iowait = 1;
 	if (iowq->timeout != KTIME_MAX || iowq->min_timeout)
-		ret = io_cqring_schedule_timeout(iowq, ctx->clockid, start_time);
+		ret = io_cqring_schedule_timeout(iowq, iowq->clockid, start_time);
 	else
 		schedule();
 	current->in_iowait = 0;
@@ -250,14 +250,15 @@ int io_cqring_wait(struct io_ring_ctx *ctx, int min_events, u32 flags,
 	rings = NULL;
 	iowq.nr_timeouts = atomic_read(&ctx->cq_timeouts);
 	iowq.hit_timeout = 0;
+	iowq.clockid = READ_ONCE(ctx->clockid);
 	iowq.min_timeout = ext_arg->min_time;
 	iowq.timeout = KTIME_MAX;
-	start_time = io_get_time(ctx);
+	start_time = io_get_time(iowq.clockid);
 
 	if (ext_arg->ts_set) {
 		iowq.timeout = timespec64_to_ktime(ext_arg->ts);
 		if (flags & IORING_ENTER_ABS_TIMER)
-			iowq.timeout = timens_ktime_to_host(ctx->clockid,
+			iowq.timeout = timens_ktime_to_host(iowq.clockid,
 							    iowq.timeout);
 		else
 			iowq.timeout = ktime_add(iowq.timeout, start_time);
-- 
2.54.0 (Apple Git-157)


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* [RFC PATCH 2/2] io_uring: add CLOCK_TAI support for waits and timeouts
  2026-11-27 21:20 [RFC PATCH 0/2] io_uring: add CLOCK_TAI support for waits and timeouts Artem Andreenko
  2026-11-27 21:20 ` [RFC PATCH 1/2] io_uring: snapshot clockid per wait and drop clock_offset Artem Andreenko
@ 2026-11-27 21:20 ` Artem Andreenko
  1 sibling, 0 replies; 3+ messages in thread
From: Artem Andreenko @ 2026-11-27 21:20 UTC (permalink / raw)
  To: Jens Axboe, Pavel Begunkov; +Cc: io-uring, Artem Andreenko, linux-api

Time-sensitive workloads such as 5G NR gNodeB DU/CU radio-slot loops,
O-RAN 7.2x fronthaul, and TSN applications schedule periodic activations
and packet launchtimes on PTP-synchronized CLOCK_TAI rather than
CLOCK_MONOTONIC (which drifts relative to GNSS/PTP grandmasters).

Allow CLOCK_TAI in IORING_REGISTER_CLOCK (via ktime_get_clocktai() and
HRTIMER_BASE_TAI) and add IORING_TIMEOUT_TAI (bit 8) to
IORING_TIMEOUT_CLOCK_MASK so IORING_OP_TIMEOUT and
IORING_OP_LINK_TIMEOUT can arm CLOCK_TAI deadlines directly.

Tested on arm64 QEMU with PROVE_LOCKING and DEBUG_ATOMIC_SLEEP after
setting a 37-second ADJ_TAI offset, verifying IORING_REGISTER_CLOCK,
future and already-expired absolute io_uring_enter waits,
IORING_OP_TIMEOUT with IORING_TIMEOUT_TAI, and conflicting clock-flag
rejection.

Signed-off-by: Artem Andreenko <miolini@gmail.com>
---
 include/uapi/linux/io_uring.h | 5 ++++-
 io_uring/io_uring.h           | 2 ++
 io_uring/register.c           | 1 +
 io_uring/timeout.c            | 2 ++
 4 files changed, 9 insertions(+), 1 deletion(-)

diff --git a/include/uapi/linux/io_uring.h b/include/uapi/linux/io_uring.h
index cf1afd11b..af86553b3 100644
--- a/include/uapi/linux/io_uring.h
+++ b/include/uapi/linux/io_uring.h
@@ -357,7 +357,10 @@ enum io_uring_op {
 #define IORING_TIMEOUT_ETIME_SUCCESS	(1U << 5)
 #define IORING_TIMEOUT_MULTISHOT	(1U << 6)
 #define IORING_TIMEOUT_IMMEDIATE_ARG	(1U << 7)
-#define IORING_TIMEOUT_CLOCK_MASK	(IORING_TIMEOUT_BOOTTIME | IORING_TIMEOUT_REALTIME)
+#define IORING_TIMEOUT_TAI		(1U << 8)
+#define IORING_TIMEOUT_CLOCK_MASK	(IORING_TIMEOUT_BOOTTIME | \
+					 IORING_TIMEOUT_REALTIME | \
+					 IORING_TIMEOUT_TAI)
 #define IORING_TIMEOUT_UPDATE_MASK	(IORING_TIMEOUT_UPDATE | IORING_LINK_TIMEOUT_UPDATE)
 /*
  * sqe->splice_flags
diff --git a/io_uring/io_uring.h b/io_uring/io_uring.h
index b2808222e..b8c2bee11 100644
--- a/io_uring/io_uring.h
+++ b/io_uring/io_uring.h
@@ -584,6 +584,8 @@ static inline ktime_t io_get_time(clockid_t clockid)
 	switch (clockid) {
 	case CLOCK_BOOTTIME:
 		return ktime_get_boottime();
+	case CLOCK_TAI:
+		return ktime_get_clocktai();
 	default:
 		return ktime_get();
 	}
diff --git a/io_uring/register.c b/io_uring/register.c
index 802cfd6a9..38495369f 100644
--- a/io_uring/register.c
+++ b/io_uring/register.c
@@ -453,6 +453,7 @@ static int io_register_clock(struct io_ring_ctx *ctx,
 	switch (reg.clockid) {
 	case CLOCK_MONOTONIC:
 	case CLOCK_BOOTTIME:
+	case CLOCK_TAI:
 		break;
 	default:
 		return -EINVAL;
diff --git a/io_uring/timeout.c b/io_uring/timeout.c
index db78059cf..43a6afd6d 100644
--- a/io_uring/timeout.c
+++ b/io_uring/timeout.c
@@ -43,6 +43,8 @@ static clockid_t io_flags_to_clock(unsigned flags)
 		return CLOCK_BOOTTIME;
 	case IORING_TIMEOUT_REALTIME:
 		return CLOCK_REALTIME;
+	case IORING_TIMEOUT_TAI:
+		return CLOCK_TAI;
 	default:
 		/* can't happen, vetted at prep time */
 		WARN_ON_ONCE(1);
-- 
2.54.0 (Apple Git-157)


^ permalink raw reply related	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2026-11-27 21:20 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-27 21:20 [RFC PATCH 0/2] io_uring: add CLOCK_TAI support for waits and timeouts Artem Andreenko
2026-11-27 21:20 ` [RFC PATCH 1/2] io_uring: snapshot clockid per wait and drop clock_offset Artem Andreenko
2026-11-27 21:20 ` [RFC PATCH 2/2] io_uring: add CLOCK_TAI support for waits and timeouts Artem Andreenko

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