Skip to content

Commit

Permalink
io_uring: don't convert to jiffies for waiting on timeouts
Browse files Browse the repository at this point in the history
commit 2283396 upstream.

If an application calls io_uring_enter(2) with a timespec passed in,
convert that timespec to ktime_t rather than jiffies. The latter does
not provide the granularity the application may expect, and may in
fact provided different granularity on different systems, depending
on what the HZ value is configured at.

Turn the timespec into an absolute ktime_t, and use that with
schedule_hrtimeout() instead.

Link: axboe/liburing#531
Cc: stable@vger.kernel.org
Reported-by: Bob Chen <chenbo.chen@alibaba-inc.com>
Signed-off-by: Jens Axboe <axboe@kernel.dk>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
  • Loading branch information
axboe authored and gregkh committed Mar 2, 2022
1 parent 6b0d719 commit ac14631
Showing 1 changed file with 7 additions and 6 deletions.
13 changes: 7 additions & 6 deletions fs/io_uring.c
Expand Up @@ -7633,7 +7633,7 @@ static int io_run_task_work_sig(void)
/* when returns >0, the caller should retry */
static inline int io_cqring_wait_schedule(struct io_ring_ctx *ctx,
struct io_wait_queue *iowq,
signed long *timeout)
ktime_t timeout)
{
int ret;

Expand All @@ -7645,8 +7645,9 @@ static inline int io_cqring_wait_schedule(struct io_ring_ctx *ctx,
if (test_bit(0, &ctx->check_cq_overflow))
return 1;

*timeout = schedule_timeout(*timeout);
return !*timeout ? -ETIME : 1;
if (!schedule_hrtimeout(&timeout, HRTIMER_MODE_ABS))
return -ETIME;
return 1;
}

/*
Expand All @@ -7659,7 +7660,7 @@ static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events,
{
struct io_wait_queue iowq;
struct io_rings *rings = ctx->rings;
signed long timeout = MAX_SCHEDULE_TIMEOUT;
ktime_t timeout = KTIME_MAX;
int ret;

do {
Expand All @@ -7675,7 +7676,7 @@ static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events,

if (get_timespec64(&ts, uts))
return -EFAULT;
timeout = timespec64_to_jiffies(&ts);
timeout = ktime_add_ns(timespec64_to_ktime(ts), ktime_get_ns());
}

if (sig) {
Expand Down Expand Up @@ -7707,7 +7708,7 @@ static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events,
}
prepare_to_wait_exclusive(&ctx->cq_wait, &iowq.wq,
TASK_INTERRUPTIBLE);
ret = io_cqring_wait_schedule(ctx, &iowq, &timeout);
ret = io_cqring_wait_schedule(ctx, &iowq, timeout);
finish_wait(&ctx->cq_wait, &iowq.wq);
cond_resched();
} while (ret > 0);
Expand Down

0 comments on commit ac14631

Please sign in to comment.