Add support for batched min timeout
Jens Axboe
maintainerv3 cover Add support for batched min timeout
Hi,
After somewhat of a lengthy delay from v2, here's v3 of this patchset. We've now validated this actually makes sense and helps for production workloads where there's a big gap between the peak and slow hours in terms of load. Having a min time enables the same settings to be used for peak hours (guaranteeing minimum response time), while avoiding excessive context switches during lulls.
For a full description, see the v2 posting:
https://lore.kernel.org/io-uring/[email protected]/
As before, there's a liburing branch with added test cases, it can be found here:
https://git.kernel.dk/cgit/liburing/log/?h=min-wait
The patches are on top of for-6.12/io_uring, and with David Wei's new iowait feat and enter flag added to avoid conflicts with that.
Changes since v2:
- Rebase on current 6.12 tree, which was mostly centered around Pavel's rework of the timeout handling and ABS_TIME additions
- Since there's now one more argument for io_cqring_wait(), wrap those output arguments in a struct ext_arg.
include/uapi/linux/io_uring.h | 3 +- io_uring/io_uring.c | 187 +++++++++++++++++++++++++--------- io_uring/io_uring.h | 4 + 3 files changed, 147 insertions(+), 47 deletions(-)
Signature · 1 line
Jens Axboe
Jens Axboe
maintainerPATCH v3 1/5 io_uring: encapsulate extraneous wait flags into a separate struct
Rather than need to pass in 2 or 3 separate arguments, add a struct to encapsulate the timeout and sigset_t parts of waiting. In preparation for adding another argument for waiting.
--- io_uring/io_uring.c | 45 ++++++++++++++++++++++++--------------------- 1 file changed, 24 insertions(+), 21 deletions(-)
io_uring/io_uring.c +24 -21
@@ -2384,13 +2384,18 @@ static inline int io_cqring_wait_schedule(struct io_ring_ctx *ctx,return ret; } +struct ext_arg { +size_t argsz; +struct __kernel_timespec __user *ts; +const sigset_t __user *sig; +}; +/* * 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. */ static int io_cqring_wait (struct io_ring_ctx *ctx,int min_events,u32 flags, -const sigset_t __user *sig,size_t sigsz, -struct __kernel_timespec __user *uts) +struct ext_arg *ext_arg) {struct io_wait_queue iowq;struct io_rings *rings = ctx->rings;
@@ -2416,10 +2421,10 @@ static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events, u32 flags, iowq.timeout =KTIME_MAX ; iowq.no_iowait = flags &IORING_ENTER_NO_IOWAIT ; -if (uts) { +if (ext_arg->ts) {struct timespec64 ts; -if (get_timespec64 (&ts, uts)) +if (get_timespec64 (&ts, ext_arg->ts))return -EFAULT ; iowq.timeout =timespec64_to_ktime (ts);
@@ -2427,14 +2432,14 @@ static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events, u32 flags, iowq.timeout =ktime_add (iowq.timeout,io_get_time (ctx)); } -if (sig) { +if (ext_arg->sig) {#ifdef CONFIG_COMPAT if (in_compat_syscall ()) - ret =set_compat_user_sigmask ((const compat_sigset_t __user *)sig, - sigsz); + ret =set_compat_user_sigmask ((const compat_sigset_t __user *)ext_arg->sig, + ext_arg->argsz); else#endif - ret =set_user_sigmask (sig, sigsz); + ret =set_user_sigmask (ext_arg->sig, ext_arg->argsz);if (ret)return ret;
@@ -3113,9 +3118,8 @@ static int io_validate_ext_arg(unsigned flags, const void __user *argp, size_t areturn 0 ; } -static int io_get_ext_arg (unsigned flags,const void __user *argp,size_t *argsz, -struct __kernel_timespec __user **ts, -const sigset_t __user **sig) +static int io_get_ext_arg (unsigned flags,const void __user *argp, +struct ext_arg *ext_arg) {struct io_uring_getevents_arg arg;
@@ -3124,8 +3128,8 @@ static int io_get_ext_arg(unsigned flags, const void __user *argp, size_t *argsz* is just a pointer to the sigset_t. */ if (!(flags &IORING_ENTER_EXT_ARG )) { - *sig = (const sigset_t __user *) argp; - *ts =NULL ; + ext_arg->sig = (const sigset_t __user *) argp; + ext_arg->ts =NULL ;return 0 ; }
@@ -3133,15 +3137,15 @@ static int io_get_ext_arg(unsigned flags, const void __user *argp, size_t *argsz* EXT_ARG is set - ensure we agree on the size of it and copy in our * timespec and sigset_t pointers if good. */ -if (*argsz !=sizeof (arg)) +if (ext_arg->argsz !=sizeof (arg))return -EINVAL ;if (copy_from_user (&arg, argp,sizeof (arg)))return -EFAULT ;if (arg.pad)return -EINVAL ; - *sig =u64_to_user_ptr (arg.sigmask); - *argsz = arg.sigmask_sz; - *ts =u64_to_user_ptr (arg.ts); + ext_arg->sig =u64_to_user_ptr (arg.sigmask); + ext_arg->argsz = arg.sigmask_sz; + ext_arg->ts =u64_to_user_ptr (arg.ts);return 0 ; }
@@ -3247,15 +3251,14 @@ SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit, }mutex_unlock (&ctx->uring_lock); }else { -const sigset_t __user *sig; -struct __kernel_timespec __user *ts; +struct ext_arg ext_arg = { .argsz = argsz }; - ret2 =io_get_ext_arg (flags, argp, &argsz, &ts, &sig); + ret2 =io_get_ext_arg (flags, argp, &ext_arg);if (likely (!ret2)) { min_complete =min (min_complete, ctx->cq_entries); ret2 =io_cqring_wait (ctx, min_complete, flags, - sig, argsz, ts); + &ext_arg); } }
Signature · 1 line
2.43.0
Jens Axboe
maintainerPATCH v3 2/5 io_uring: move schedule wait logic into helper
In reply to Jens AxboeIn preparation for expanding how we handle waits, move the actual schedule and schedule_timeout() handling into a helper.
--- io_uring/io_uring.c | 37 +++++++++++++++++++++---------------- 1 file changed, 21 insertions(+), 16 deletions(-)
io_uring/io_uring.c +21 -16
@@ -2350,22 +2350,10 @@ static bool current_pending_io(void)return percpu_counter_read_positive (&tctx->inflight); } -/* when returns >0, the caller should retry */ -static inline int io_cqring_wait_schedule (struct io_ring_ctx *ctx, -struct io_wait_queue *iowq) +static int __io_cqring_wait_schedule (struct io_ring_ctx *ctx, +struct io_wait_queue *iowq) { -int ret; - -if (unlikely (READ_ONCE (ctx->check_cq))) -return 1 ; -if (unlikely (!llist_empty (&ctx->work_llist))) -return 1 ; -if (unlikely (test_thread_flag (TIF_NOTIFY_SIGNAL ))) -return 1 ; -if (unlikely (task_sigpending (current))) -return -EINTR ; -if (unlikely (io_should_wake (iowq))) -return 0 ; +int ret =0 ;/* * Mark us as being in io_wait if we have pending requests, so cpufreq
@@ -2374,7 +2362,6 @@ static inline int io_cqring_wait_schedule(struct io_ring_ctx *ctx,*/ if (!iowq->no_iowait &¤t_pending_io ()) current->in_iowait =1 ; - ret =0 ;if (iowq->timeout ==KTIME_MAX )schedule (); else if (!schedule_hrtimeout_range_clock(&iowq->timeout, 0,
@@ -2384,6 +2371,24 @@ static inline int io_cqring_wait_schedule(struct io_ring_ctx *ctx,return ret; } +/* If this returns > 0, the caller should retry */ +static inline int io_cqring_wait_schedule (struct io_ring_ctx *ctx, +struct io_wait_queue *iowq) +{ +if (unlikely (READ_ONCE (ctx->check_cq))) +return 1 ; +if (unlikely (!llist_empty (&ctx->work_llist))) +return 1 ; +if (unlikely (test_thread_flag (TIF_NOTIFY_SIGNAL ))) +return 1 ; +if (unlikely (task_sigpending (current))) +return -EINTR ; +if (unlikely (io_should_wake (iowq))) +return 0 ; + +return __io_cqring_wait_schedule (ctx, iowq); +} +struct ext_arg {size_t argsz;struct __kernel_timespec __user *ts;
Signature · 1 line
2.43.0
Jens Axboe
maintainerPATCH v3 3/5 io_uring: implement our own schedule timeout handling
In reply to Jens AxboeIn preparation for having two distinct timeouts and avoid waking the task if we don't need to.
--- io_uring/io_uring.c | 41 ++++++++++++++++++++++++++++++++++++----- io_uring/io_uring.h | 2 ++ 2 files changed, 38 insertions(+), 5 deletions(-)
io_uring/io_uring.c +36 -5
@@ -2322,7 +2322,7 @@ static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode,* Cannot safely flush overflowed CQEs from here, ensure we wake up * the task, and the next invocation will do it. */ -if (io_should_wake (iowq) ||io_has_work (iowq->ctx)) +if (io_should_wake (iowq) ||io_has_work (iowq->ctx) || iowq->hit_timeout)return autoremove_wake_function (curr, mode, wake_flags, key);return -1 ; }
@@ -2350,6 +2350,38 @@ static bool current_pending_io(void)return percpu_counter_read_positive (&tctx->inflight); } +static enum hrtimer_restart io_cqring_timer_wakeup (struct hrtimer *timer) +{ + struct io_wait_queue *iowq = container_of(timer, struct io_wait_queue, t); +struct io_ring_ctx *ctx = iowq->ctx; + +WRITE_ONCE (iowq->hit_timeout,1 ); +if (ctx->flags &IORING_SETUP_DEFER_TASKRUN ) +wake_up_process (ctx->submitter_task); +else +io_cqring_wake (ctx); +return HRTIMER_NORESTART ; +} + +static int io_cqring_schedule_timeout (struct io_wait_queue *iowq, +clockid_t clock_id) +{ + iowq->hit_timeout =0 ; +hrtimer_init_on_stack (&iowq->t, clock_id,HRTIMER_MODE_ABS ); + iowq->t.function = io_cqring_timer_wakeup; +hrtimer_set_expires_range_ns (&iowq->t, iowq->timeout,0 ); +hrtimer_start_expires (&iowq->t,HRTIMER_MODE_ABS ); + +if (!READ_ONCE (iowq->hit_timeout)) +schedule (); + +hrtimer_cancel (&iowq->t); +destroy_hrtimer_on_stack (&iowq->t); +__set_current_state (TASK_RUNNING ); + +return READ_ONCE (iowq->hit_timeout) ? -ETIME :0 ; +} +static int __io_cqring_wait_schedule (struct io_ring_ctx *ctx,struct io_wait_queue *iowq) {
@@ -2362,11 +2394,10 @@ static int __io_cqring_wait_schedule(struct io_ring_ctx *ctx,*/ if (!iowq->no_iowait &¤t_pending_io ()) current->in_iowait =1 ; -if (iowq->timeout ==KTIME_MAX ) +if (iowq->timeout !=KTIME_MAX ) + ret =io_cqring_schedule_timeout (iowq, ctx->clockid); +else schedule (); -else if (!schedule_hrtimeout_range_clock (&iowq->timeout,0 , -HRTIMER_MODE_ABS , ctx->clockid)) - ret = -ETIME ; current->in_iowait =0 ;return ret; }
io_uring/io_uring.h +2 -0
@@ -40,7 +40,9 @@ struct io_wait_queue {struct io_ring_ctx *ctx;unsigned cq_tail;unsigned nr_timeouts; +int hit_timeout;ktime_t timeout; +struct hrtimer t;#ifdef CONFIG_NET_RX_BUSY_POLL ktime_t napi_busy_poll_dt;bool napi_prefer_busy_poll;
Signature · 1 line
2.43.0
Jens Axboe
maintainerPATCH v3 4/5 io_uring: add support for batch wait timeout
In reply to Jens AxboeWaiting for events with io_uring has two knobs that can be set:
1) The number of events to wake for
2) The timeout associated with the event
Waiting will abort when either of those conditions are met, as expected.
This adds support for a third event, which is associated with the number of events to wait for. Applications generally like to handle batches of completions, and right now they'd set a number of events to wait for and the timeout for that. If no events have been received but the timeout triggers, control is returned to the application and it can wait again. However, if the application doesn't have anything to do until events are reaped, then it's possible to make this waiting more efficient.
For example, the application may have a latency time of 50 usecs and wanting to handle a batch of 8 requests at the time. If it uses 50 usecs as the timeout, then it'll be doing 20K context switches per second even if nothing is happening.
This introduces the notion of min batch wait time. If the min batch wait time expires, then we'll return to userspace if we have any events at all. If none are available, the general wait time is applied. Any request arriving after the min batch wait time will cause waiting to stop and return control to the application.
--- io_uring/io_uring.c | 75 +++++++++++++++++++++++++++++++++++++++------ io_uring/io_uring.h | 2 ++ 2 files changed, 67 insertions(+), 10 deletions(-)
io_uring/io_uring.c +65 -10
@@ -2363,13 +2363,62 @@ static enum hrtimer_restart io_cqring_timer_wakeup(struct hrtimer *timer)return HRTIMER_NORESTART ; } +/* +* Doing min_timeout portion. If we saw any timeouts, events, or have work, +* wake up. If not, and we have a normal timeout, switch to that and keep +* sleeping. +*/ +static enum hrtimer_restart io_cqring_min_timer_wakeup (struct hrtimer *timer) +{ + struct io_wait_queue *iowq = container_of(timer, struct io_wait_queue, t); +struct io_ring_ctx *ctx = iowq->ctx; + +/* no general timeout, or shorter, we are done */ +if (iowq->timeout ==KTIME_MAX || +ktime_after (iowq->min_timeout, iowq->timeout)) + goto out_wake; +/* work we may need to run, wake function will see if we need to wake */ +if (io_has_work (ctx)) + goto out_wake; +/* got events since we started waiting, min timeout is done */ +if (iowq->cq_min_tail !=READ_ONCE (ctx->rings->cq.tail)) + goto out_wake; +/* if we have any events and min timeout expired, we're done */ +if (io_cqring_events (ctx)) + goto out_wake; + +/* +* If using deferred task_work running and application is waiting on +* more than one request, ensure we reset it now where we are switching +* to normal sleeps. Any request completion post min_wait should wake +* the task and return. +*/ +if (ctx->flags &IORING_SETUP_DEFER_TASKRUN ) +atomic_set (&ctx->cq_wait_nr,1 ); + + iowq->t.function = io_cqring_timer_wakeup; +hrtimer_set_expires (timer, iowq->timeout); +return HRTIMER_RESTART ; +out_wake: +return io_cqring_timer_wakeup (timer); +} +static int io_cqring_schedule_timeout (struct io_wait_queue *iowq, -clockid_t clock_id) +clockid_t clock_id,ktime_t start_time) { +ktime_t timeout; + iowq->hit_timeout =0 ;hrtimer_init_on_stack (&iowq->t, clock_id,HRTIMER_MODE_ABS ); - iowq->t.function = io_cqring_timer_wakeup; -hrtimer_set_expires_range_ns (&iowq->t, iowq->timeout,0 ); +if (iowq->min_timeout) { + timeout =ktime_add_ns (iowq->min_timeout, start_time); + iowq->t.function = io_cqring_min_timer_wakeup; + }else { + timeout = iowq->timeout; + iowq->t.function = io_cqring_timer_wakeup; + } + +hrtimer_set_expires_range_ns (&iowq->t, timeout,0 );hrtimer_start_expires (&iowq->t,HRTIMER_MODE_ABS );if (!READ_ONCE (iowq->hit_timeout))
@@ -2383,7 +2432,8 @@ static int io_cqring_schedule_timeout(struct io_wait_queue *iowq, }static int __io_cqring_wait_schedule (struct io_ring_ctx *ctx, -struct io_wait_queue *iowq) +struct io_wait_queue *iowq, +ktime_t start_time) {int ret =0 ;
@@ -2394,8 +2444,8 @@ static int __io_cqring_wait_schedule(struct io_ring_ctx *ctx,*/ if (!iowq->no_iowait &¤t_pending_io ()) current->in_iowait =1 ; -if (iowq->timeout !=KTIME_MAX ) - ret =io_cqring_schedule_timeout (iowq, ctx->clockid); +if (iowq->timeout !=KTIME_MAX || iowq->min_timeout !=KTIME_MAX ) + ret =io_cqring_schedule_timeout (iowq, ctx->clockid, start_time);else schedule (); current->in_iowait =0 ;
@@ -2404,7 +2454,8 @@ static int __io_cqring_wait_schedule(struct io_ring_ctx *ctx,/* If this returns > 0, the caller should retry */ static inline int io_cqring_wait_schedule (struct io_ring_ctx *ctx, -struct io_wait_queue *iowq) +struct io_wait_queue *iowq, +ktime_t start_time) {if (unlikely (READ_ONCE (ctx->check_cq)))return 1 ;
@@ -2417,7 +2468,7 @@ static inline int io_cqring_wait_schedule(struct io_ring_ctx *ctx,if (unlikely (io_should_wake (iowq)))return 0 ; -return __io_cqring_wait_schedule (ctx, iowq); +return __io_cqring_wait_schedule (ctx, iowq, start_time); }struct ext_arg {
@@ -2435,6 +2486,7 @@ static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events, u32 flags, {struct io_wait_queue iowq;struct io_rings *rings = ctx->rings; +ktime_t start_time;int ret;if (!io_allowed_run_tw (ctx))
@@ -2453,9 +2505,12 @@ static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events, u32 flags,INIT_LIST_HEAD (&iowq.wq.entry); iowq.ctx = ctx; iowq.nr_timeouts =atomic_read (&ctx->cq_timeouts); + iowq.cq_min_tail =READ_ONCE (ctx->rings->cq.tail); iowq.cq_tail =READ_ONCE (ctx->rings->cq.head) + min_events; + iowq.min_timeout =0 ; iowq.timeout =KTIME_MAX ; iowq.no_iowait = flags &IORING_ENTER_NO_IOWAIT ; + start_time =io_get_time (ctx);if (ext_arg->ts) {struct timespec64 ts;
@@ -2465,7 +2520,7 @@ static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events, u32 flags, iowq.timeout =timespec64_to_ktime (ts);if (!(flags &IORING_ENTER_ABS_TIMER )) - iowq.timeout =ktime_add (iowq.timeout,io_get_time (ctx)); + iowq.timeout =ktime_add (iowq.timeout, start_time); }if (ext_arg->sig) {
@@ -2496,7 +2551,7 @@ static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events, u32 flags, TASK_INTERRUPTIBLE); } - ret =io_cqring_wait_schedule (ctx, &iowq); + ret =io_cqring_wait_schedule (ctx, &iowq, start_time);__set_current_state (TASK_RUNNING );atomic_set (&ctx->cq_wait_nr,IO_CQ_WAKE_INIT );
io_uring/io_uring.h +2 -0
@@ -39,8 +39,10 @@ struct io_wait_queue {struct wait_queue_entry wq;struct io_ring_ctx *ctx;unsigned cq_tail; +unsigned cq_min_tail;unsigned nr_timeouts;int hit_timeout; +ktime_t min_timeout;ktime_t timeout;struct hrtimer t;#ifdef CONFIG_NET_RX_BUSY_POLL
Signature · 1 line
2.43.0
Jens Axboe
maintainerPATCH v3 5/5 io_uring: wire up min batch wake timeout
In reply to Jens AxboeExpose min_wait_usec in io_uring_getevents_arg, replacing the pad member that is currently in there. The value is in usecs, which is explained in the name as well.
Note that if min_wait_usec and a normal timeout is used in conjunction, the normal timeout is still relative to the base time. For example, if min_wait_usec is set to 100 and the normal timeout is 1000, the max total time waited is still 1000. This also means that if the normal timeout is shorter than min_wait_usec, then only the min_wait_usec will take effect.
See previous commit for an explanation of how this works.
IORING_FEAT_MIN_TIMEOUT is added as a feature flag for this, as applications doing submit_and_wait_timeout() style operations will generally not see the -EINVAL from the wait side as they return the number of IOs submitted. Only if no IOs are submitted will the -EINVAL bubble back up to the application.
--- include/uapi/linux/io_uring.h | 3 ++- io_uring/io_uring.c | 9 +++++---- 2 files changed, 7 insertions(+), 5 deletions(-)
include/uapi/linux/io_uring.h +2 -1
@@ -545,6 +545,7 @@ struct io_uring_params {#define IORING_FEAT_REG_REG_RING (1U << 13)#define IORING_FEAT_RECVSEND_BUNDLE (1U << 14)#define IORING_FEAT_IOWAIT_TOGGLE (1U << 15) +#define IORING_FEAT_MIN_TIMEOUT (1U << 16)/* * io_uring_register(2) opcodes and arguments
@@ -768,7 +769,7 @@ enum io_uring_register_restriction_op {struct io_uring_getevents_arg {__u64 sigmask;__u32 sigmask_sz; -__u32 pad; +__u32 min_wait_usec;__u64 ts; };
io_uring/io_uring.c +5 -4
@@ -2475,6 +2475,7 @@ struct ext_arg {size_t argsz;struct __kernel_timespec __user *ts;const sigset_t __user *sig; +ktime_t min_time; };/*
@@ -2507,7 +2508,7 @@ static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events, u32 flags, iowq.nr_timeouts =atomic_read (&ctx->cq_timeouts); iowq.cq_min_tail =READ_ONCE (ctx->rings->cq.tail); iowq.cq_tail =READ_ONCE (ctx->rings->cq.head) + min_events; - iowq.min_timeout =0 ; + iowq.min_timeout = ext_arg->min_time; iowq.timeout =KTIME_MAX ; iowq.no_iowait = flags &IORING_ENTER_NO_IOWAIT ; start_time =io_get_time (ctx);
@@ -3232,8 +3233,7 @@ static int io_get_ext_arg(unsigned flags, const void __user *argp,return -EINVAL ;if (copy_from_user (&arg, argp,sizeof (arg)))return -EFAULT ; -if (arg.pad) -return -EINVAL ; + ext_arg->min_time = arg.min_wait_usec *NSEC_PER_USEC ; ext_arg->sig =u64_to_user_ptr (arg.sigmask); ext_arg->argsz = arg.sigmask_sz; ext_arg->ts =u64_to_user_ptr (arg.ts);
@@ -3634,7 +3634,8 @@ static __cold int io_uring_create(unsigned entries, struct io_uring_params *p,IORING_FEAT_EXT_ARG |IORING_FEAT_NATIVE_WORKERS |IORING_FEAT_RSRC_TAGS |IORING_FEAT_CQE_SKIP |IORING_FEAT_LINKED_FILE |IORING_FEAT_REG_REG_RING | -IORING_FEAT_RECVSEND_BUNDLE |IORING_FEAT_IOWAIT_TOGGLE ; +IORING_FEAT_RECVSEND_BUNDLE |IORING_FEAT_IOWAIT_TOGGLE | +IORING_FEAT_MIN_TIMEOUT ;if (copy_to_user (params, p,sizeof (*p))) { ret = -EFAULT ;
Signature · 1 line
2.43.0