Skip to content

Commit 2b8e976

Browse files
isilenceaxboe
authored andcommitted
io_uring: user registered clockid for wait timeouts
Add a new registration opcode IORING_REGISTER_CLOCK, which allows the user to select which clock id it wants to use with CQ waiting timeouts. It only allows a subset of all posix clocks and currently supports CLOCK_MONOTONIC and CLOCK_BOOTTIME. Suggested-by: Lewis Baker <[email protected]> Signed-off-by: Pavel Begunkov <[email protected]> Link: https://lore.kernel.org/r/98f2bc8a3c36cdf8f0e6a275245e81e903459703.1723039801.git.asml.silence@gmail.com Signed-off-by: Jens Axboe <[email protected]>
1 parent d29cb37 commit 2b8e976

File tree

6 files changed

+56
-3
lines changed

6 files changed

+56
-3
lines changed

include/linux/io_uring_types.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,9 @@ struct io_ring_ctx {
239239
struct io_rings *rings;
240240
struct percpu_ref refs;
241241

242+
clockid_t clockid;
243+
enum tk_offsets clock_offset;
244+
242245
enum task_work_notify_mode notify_method;
243246
unsigned sq_thread_idle;
244247
} ____cacheline_aligned_in_smp;

include/uapi/linux/io_uring.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -596,6 +596,8 @@ enum io_uring_register_op {
596596
IORING_REGISTER_NAPI = 27,
597597
IORING_UNREGISTER_NAPI = 28,
598598

599+
IORING_REGISTER_CLOCK = 29,
600+
599601
/* this goes last */
600602
IORING_REGISTER_LAST,
601603

@@ -676,6 +678,11 @@ struct io_uring_restriction {
676678
__u32 resv2[3];
677679
};
678680

681+
struct io_uring_clock_register {
682+
__u32 clockid;
683+
__u32 __resv[3];
684+
};
685+
679686
struct io_uring_buf {
680687
__u64 addr;
681688
__u32 len;

io_uring/io_uring.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2377,7 +2377,8 @@ static inline int io_cqring_wait_schedule(struct io_ring_ctx *ctx,
23772377
ret = 0;
23782378
if (iowq->timeout == KTIME_MAX)
23792379
schedule();
2380-
else if (!schedule_hrtimeout(&iowq->timeout, HRTIMER_MODE_ABS))
2380+
else if (!schedule_hrtimeout_range_clock(&iowq->timeout, 0,
2381+
HRTIMER_MODE_ABS, ctx->clockid))
23812382
ret = -ETIME;
23822383
current->in_iowait = 0;
23832384
return ret;
@@ -2422,7 +2423,7 @@ static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events, u32 flags,
24222423

24232424
iowq.timeout = timespec64_to_ktime(ts);
24242425
if (!(flags & IORING_ENTER_ABS_TIMER))
2425-
iowq.timeout = ktime_add(iowq.timeout, ktime_get());
2426+
iowq.timeout = ktime_add(iowq.timeout, io_get_time(ctx));
24262427
}
24272428

24282429
if (sig) {
@@ -3424,6 +3425,9 @@ static __cold int io_uring_create(unsigned entries, struct io_uring_params *p,
34243425
if (!ctx)
34253426
return -ENOMEM;
34263427

3428+
ctx->clockid = CLOCK_MONOTONIC;
3429+
ctx->clock_offset = 0;
3430+
34273431
if ((ctx->flags & IORING_SETUP_DEFER_TASKRUN) &&
34283432
!(ctx->flags & IORING_SETUP_IOPOLL) &&
34293433
!(ctx->flags & IORING_SETUP_SQPOLL))

io_uring/io_uring.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -437,6 +437,14 @@ static inline bool io_file_can_poll(struct io_kiocb *req)
437437
return false;
438438
}
439439

440+
static inline ktime_t io_get_time(struct io_ring_ctx *ctx)
441+
{
442+
if (ctx->clockid == CLOCK_MONOTONIC)
443+
return ktime_get();
444+
445+
return ktime_get_with_offset(ctx->clock_offset);
446+
}
447+
440448
enum {
441449
IO_CHECK_CQ_OVERFLOW_BIT,
442450
IO_CHECK_CQ_DROPPED_BIT,

io_uring/napi.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@ void __io_napi_busy_loop(struct io_ring_ctx *ctx, struct io_wait_queue *iowq)
283283

284284
iowq->napi_busy_poll_dt = READ_ONCE(ctx->napi_busy_poll_dt);
285285
if (iowq->timeout != KTIME_MAX) {
286-
ktime_t dt = ktime_sub(iowq->timeout, ktime_get());
286+
ktime_t dt = ktime_sub(iowq->timeout, io_get_time(ctx));
287287

288288
iowq->napi_busy_poll_dt = min_t(u64, iowq->napi_busy_poll_dt, dt);
289289
}

io_uring/register.c

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,31 @@ static __cold int io_register_iowq_max_workers(struct io_ring_ctx *ctx,
335335
return ret;
336336
}
337337

338+
static int io_register_clock(struct io_ring_ctx *ctx,
339+
struct io_uring_clock_register __user *arg)
340+
{
341+
struct io_uring_clock_register reg;
342+
343+
if (copy_from_user(&reg, arg, sizeof(reg)))
344+
return -EFAULT;
345+
if (memchr_inv(&reg.__resv, 0, sizeof(reg.__resv)))
346+
return -EINVAL;
347+
348+
switch (reg.clockid) {
349+
case CLOCK_MONOTONIC:
350+
ctx->clock_offset = 0;
351+
break;
352+
case CLOCK_BOOTTIME:
353+
ctx->clock_offset = TK_OFFS_BOOT;
354+
break;
355+
default:
356+
return -EINVAL;
357+
}
358+
359+
ctx->clockid = reg.clockid;
360+
return 0;
361+
}
362+
338363
static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode,
339364
void __user *arg, unsigned nr_args)
340365
__releases(ctx->uring_lock)
@@ -511,6 +536,12 @@ static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode,
511536
break;
512537
ret = io_unregister_napi(ctx, arg);
513538
break;
539+
case IORING_REGISTER_CLOCK:
540+
ret = -EINVAL;
541+
if (!arg || nr_args)
542+
break;
543+
ret = io_register_clock(ctx, arg);
544+
break;
514545
default:
515546
ret = -EINVAL;
516547
break;

0 commit comments

Comments
 (0)