Skip to content

Commit bfe68a2

Browse files
isilenceaxboe
authored andcommitted
io_uring: off timeouts based only on completions
Offset timeouts wait not for sqe->off non-timeout CQEs, but rather sqe->off + number of prior inflight requests. Wait exactly for sqe->off non-timeout completions Reported-by: Jens Axboe <[email protected]> Signed-off-by: Pavel Begunkov <[email protected]> Signed-off-by: Jens Axboe <[email protected]>
1 parent 360428f commit bfe68a2

File tree

1 file changed

+14
-51
lines changed

1 file changed

+14
-51
lines changed

fs/io_uring.c

Lines changed: 14 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -394,7 +394,8 @@ struct io_timeout {
394394
struct file *file;
395395
u64 addr;
396396
int flags;
397-
u32 count;
397+
u32 off;
398+
u32 target_seq;
398399
};
399400

400401
struct io_rw {
@@ -1124,8 +1125,10 @@ static void io_flush_timeouts(struct io_ring_ctx *ctx)
11241125

11251126
if (req->flags & REQ_F_TIMEOUT_NOSEQ)
11261127
break;
1127-
if (__req_need_defer(req))
1128+
if (req->timeout.target_seq != ctx->cached_cq_tail
1129+
- atomic_read(&ctx->cq_timeouts))
11281130
break;
1131+
11291132
list_del_init(&req->list);
11301133
io_kill_timeout(req);
11311134
}
@@ -4660,20 +4663,8 @@ static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer)
46604663
* We could be racing with timeout deletion. If the list is empty,
46614664
* then timeout lookup already found it and will be handling it.
46624665
*/
4663-
if (!list_empty(&req->list)) {
4664-
struct io_kiocb *prev;
4665-
4666-
/*
4667-
* Adjust the reqs sequence before the current one because it
4668-
* will consume a slot in the cq_ring and the cq_tail
4669-
* pointer will be increased, otherwise other timeout reqs may
4670-
* return in advance without waiting for enough wait_nr.
4671-
*/
4672-
prev = req;
4673-
list_for_each_entry_continue_reverse(prev, &ctx->timeout_list, list)
4674-
prev->sequence++;
4666+
if (!list_empty(&req->list))
46754667
list_del_init(&req->list);
4676-
}
46774668

46784669
io_cqring_fill_event(req, -ETIME);
46794670
io_commit_cqring(ctx);
@@ -4765,7 +4756,7 @@ static int io_timeout_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
47654756
if (flags & ~IORING_TIMEOUT_ABS)
47664757
return -EINVAL;
47674758

4768-
req->timeout.count = off;
4759+
req->timeout.off = off;
47694760

47704761
if (!req->io && io_alloc_async_ctx(req))
47714762
return -ENOMEM;
@@ -4789,67 +4780,39 @@ static int io_timeout_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
47894780
static int io_timeout(struct io_kiocb *req)
47904781
{
47914782
struct io_ring_ctx *ctx = req->ctx;
4792-
struct io_timeout_data *data;
4783+
struct io_timeout_data *data = &req->io->timeout;
47934784
struct list_head *entry;
4794-
unsigned span = 0;
4795-
u32 count = req->timeout.count;
4796-
u32 seq = req->sequence;
4785+
u32 tail, off = req->timeout.off;
47974786

4798-
data = &req->io->timeout;
47994787
spin_lock_irq(&ctx->completion_lock);
48004788

48014789
/*
48024790
* sqe->off holds how many events that need to occur for this
48034791
* timeout event to be satisfied. If it isn't set, then this is
48044792
* a pure timeout request, sequence isn't used.
48054793
*/
4806-
if (!count) {
4794+
if (!off) {
48074795
req->flags |= REQ_F_TIMEOUT_NOSEQ;
48084796
entry = ctx->timeout_list.prev;
48094797
goto add;
48104798
}
48114799

4812-
req->sequence = seq + count;
4800+
tail = ctx->cached_cq_tail - atomic_read(&ctx->cq_timeouts);
4801+
req->timeout.target_seq = tail + off;
48134802

48144803
/*
48154804
* Insertion sort, ensuring the first entry in the list is always
48164805
* the one we need first.
48174806
*/
48184807
list_for_each_prev(entry, &ctx->timeout_list) {
48194808
struct io_kiocb *nxt = list_entry(entry, struct io_kiocb, list);
4820-
unsigned nxt_seq;
4821-
long long tmp, tmp_nxt;
4822-
u32 nxt_offset = nxt->timeout.count;
48234809

48244810
if (nxt->flags & REQ_F_TIMEOUT_NOSEQ)
48254811
continue;
4826-
4827-
/*
4828-
* Since seq + count can overflow, use type long
4829-
* long to store it.
4830-
*/
4831-
tmp = (long long)seq + count;
4832-
nxt_seq = nxt->sequence - nxt_offset;
4833-
tmp_nxt = (long long)nxt_seq + nxt_offset;
4834-
4835-
/*
4836-
* cached_sq_head may overflow, and it will never overflow twice
4837-
* once there is some timeout req still be valid.
4838-
*/
4839-
if (seq < nxt_seq)
4840-
tmp += UINT_MAX;
4841-
4842-
if (tmp > tmp_nxt)
4812+
/* nxt.seq is behind @tail, otherwise would've been completed */
4813+
if (off >= nxt->timeout.target_seq - tail)
48434814
break;
4844-
4845-
/*
4846-
* Sequence of reqs after the insert one and itself should
4847-
* be adjusted because each timeout req consumes a slot.
4848-
*/
4849-
span++;
4850-
nxt->sequence++;
48514815
}
4852-
req->sequence -= span;
48534816
add:
48544817
list_add(&req->list, entry);
48554818
data->timer.function = io_timeout_fn;

0 commit comments

Comments
 (0)