Linux io-uring development
 help / color / mirror / Atom feed
From: Artem Andreenko <miolini@gmail.com>
To: Jens Axboe <axboe@kernel.dk>, Pavel Begunkov <asml.silence@gmail.com>
Cc: io-uring@vger.kernel.org, Artem Andreenko <miolini@gmail.com>
Subject: [RFC PATCH 1/2] io_uring: snapshot clockid per wait and drop clock_offset
Date: Fri, 27 Nov 2026 13:20:03 -0800	[thread overview]
Message-ID: <20261127212001.18516-5-miolini@gmail.com> (raw)
In-Reply-To: <20261127212001.18516-4-miolini@gmail.com>

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)


  reply	other threads:[~2026-11-27 21:20 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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 [this message]
2026-11-27 21:20 ` [RFC PATCH 2/2] " Artem Andreenko

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=20261127212001.18516-5-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