Skip to content

Commit e677edb

Browse files
committed
io_uring: fix race between timeout flush and removal
io_flush_timeouts() assumes the timeout isn't in progress of triggering or being removed/canceled, so it unconditionally removes it from the timeout list and attempts to cancel it. Leave it on the list and let the normal timeout cancelation take care of it. Cc: stable@vger.kernel.org # 5.5+ Signed-off-by: Jens Axboe <axboe@kernel.dk>
1 parent 4cdd158 commit e677edb

File tree

1 file changed

+3
-4
lines changed

1 file changed

+3
-4
lines changed

fs/io_uring.c

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1736,12 +1736,11 @@ static __cold void io_flush_timeouts(struct io_ring_ctx *ctx)
17361736
__must_hold(&ctx->completion_lock)
17371737
{
17381738
u32 seq = ctx->cached_cq_tail - atomic_read(&ctx->cq_timeouts);
1739+
struct io_kiocb *req, *tmp;
17391740

17401741
spin_lock_irq(&ctx->timeout_lock);
1741-
while (!list_empty(&ctx->timeout_list)) {
1742+
list_for_each_entry_safe(req, tmp, &ctx->timeout_list, timeout.list) {
17421743
u32 events_needed, events_got;
1743-
struct io_kiocb *req = list_first_entry(&ctx->timeout_list,
1744-
struct io_kiocb, timeout.list);
17451744

17461745
if (io_is_timeout_noseq(req))
17471746
break;
@@ -1758,7 +1757,6 @@ static __cold void io_flush_timeouts(struct io_ring_ctx *ctx)
17581757
if (events_got < events_needed)
17591758
break;
17601759

1761-
list_del_init(&req->timeout.list);
17621760
io_kill_timeout(req, 0);
17631761
}
17641762
ctx->cq_last_tm_flush = seq;
@@ -6628,6 +6626,7 @@ static int io_timeout_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
66286626
if (data->ts.tv_sec < 0 || data->ts.tv_nsec < 0)
66296627
return -EINVAL;
66306628

6629+
INIT_LIST_HEAD(&req->timeout.list);
66316630
data->mode = io_translate_timeout_mode(flags);
66326631
hrtimer_init(&data->timer, io_timeout_get_clock(data), data->mode);
66336632

0 commit comments

Comments
 (0)