[PATCH] io_uring/timeout: account for timeouts whose CQE is not posted yet
From: Yichong Chen
Date: Wed Nov 25 2026 - 00:43:49 EST
The sequence of non-timeout CQEs is derived from ->cached_cq_tail minus
->cq_timeouts, but a timeout bumps the latter when it fires, is killed or
is cancelled, which is ahead of its CQE being posted. Reading the sequence
in between comes out low, and a target counted from such a reading is
reached that much early: a counted timeout completes with 0 while nothing
but the already posted CQEs has happened, and it can also be satisfied by
timeouts posted before it was armed. Keep track of the timeouts whose CQE
is still in flight and add them back, so that the sequence is exact
whenever it is read.
Fixes: c6c0b3eba88c ("io_uring/timeout: don't skip over reached sequences")
Signed-off-by: Yichong Chen <chenyichong@xxxxxxxxxxxxx>
---
include/linux/io_uring_types.h | 1 +
io_uring/timeout.c | 53 ++++++++++++++++++++++++++++++------------
2 files changed, 39 insertions(+), 15 deletions(-)
diff --git a/include/linux/io_uring_types.h b/include/linux/io_uring_types.h
index 1468bcb..b947c72 100644
--- a/include/linux/io_uring_types.h
+++ b/include/linux/io_uring_types.h
@@ -457,6 +457,7 @@ struct io_ring_ctx {
unsigned long check_cq;
atomic_t cq_wait_nr;
atomic_t cq_timeouts;
+ atomic_t cq_timeouts_pending;
struct wait_queue_head cq_wait;
} ____cacheline_aligned_in_smp;
diff --git a/io_uring/timeout.c b/io_uring/timeout.c
index db78059..f662c0c 100644
--- a/io_uring/timeout.c
+++ b/io_uring/timeout.c
@@ -114,6 +114,7 @@ static void io_timeout_complete(struct io_tw_req tw_req, io_tw_token_t tw)
struct io_timeout_data *data = req->async_data;
struct io_ring_ctx *ctx = req->ctx;
+ atomic_dec(&ctx->cq_timeouts_pending);
if (!io_timeout_finish(timeout, data)) {
if (!tw.cancel &&
io_req_post_cqe(req, -ETIME, IORING_CQE_F_MORE)) {
@@ -129,6 +130,18 @@ static void io_timeout_complete(struct io_tw_req tw_req, io_tw_token_t tw)
io_req_task_complete(tw_req, tw);
}
+static void io_timeout_complete_killed(struct io_tw_req tw_req, io_tw_token_t tw)
+{
+ atomic_dec(&tw_req.req->ctx->cq_timeouts_pending);
+ io_req_task_complete(tw_req, tw);
+}
+
+static void io_timeout_complete_cancelled(struct io_tw_req tw_req, io_tw_token_t tw)
+{
+ atomic_dec(&tw_req.req->ctx->cq_timeouts_pending);
+ io_req_task_queue_fail(tw_req.req, -ECANCELED);
+}
+
static __cold bool io_flush_killed_timeouts(struct list_head *list, int err)
{
if (list_empty(list))
@@ -143,7 +156,9 @@ static __cold bool io_flush_killed_timeouts(struct list_head *list, int err)
req = cmd_to_io_kiocb(timeout);
if (err)
req_set_fail(req);
- io_req_queue_tw_complete(req, err);
+ io_req_set_res(req, err, 0);
+ req->io_task_work.func = io_timeout_complete_killed;
+ io_req_task_work_add(req);
}
return true;
@@ -159,10 +174,23 @@ static void io_kill_timeout(struct io_kiocb *req, struct list_head *list)
atomic_set(&req->ctx->cq_timeouts,
atomic_read(&req->ctx->cq_timeouts) + 1);
+ atomic_inc(&req->ctx->cq_timeouts_pending);
list_move_tail(&timeout->list, list);
}
}
+/*
+ * Number of CQEs posted that were not timeouts, i.e. the sequence counted
+ * timeouts are ordered by. A timeout bumps ->cq_timeouts when it fires,
+ * before its CQE is posted, so subtract the ones still in flight - a
+ * reading taken then is otherwise low by that much.
+ */
+static u32 io_timeout_seq(struct io_ring_ctx *ctx)
+{
+ return READ_ONCE(ctx->cached_cq_tail) - atomic_read(&ctx->cq_timeouts) +
+ atomic_read(&ctx->cq_timeouts_pending);
+}
+
__cold void io_flush_timeouts(struct io_ring_ctx *ctx)
{
struct io_timeout *timeout, *tmp;
@@ -170,13 +198,8 @@ __cold void io_flush_timeouts(struct io_ring_ctx *ctx)
u32 seq;
raw_spin_lock_irq(&ctx->timeout_lock);
- seq = READ_ONCE(ctx->cached_cq_tail) - atomic_read(&ctx->cq_timeouts);
+ seq = io_timeout_seq(ctx);
- /*
- * A fired timeout bumps ->cq_timeouts before its CQE is posted, which
- * leaves the sequence behind the last flush until it is. No event has
- * happened then, see io_timeout().
- */
if ((s32)(seq - ctx->cq_last_tm_flush) < 0)
seq = ctx->cq_last_tm_flush;
@@ -319,6 +342,7 @@ static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer)
list_del_init(&timeout->list);
atomic_set(&ctx->cq_timeouts,
atomic_read(&ctx->cq_timeouts) + 1);
+ atomic_inc(&ctx->cq_timeouts_pending);
raw_spin_unlock_irqrestore(&ctx->timeout_lock, flags);
if (!(data->flags & IORING_TIMEOUT_ETIME_SUCCESS))
@@ -371,14 +395,18 @@ int io_timeout_cancel(struct io_ring_ctx *ctx, struct io_cancel_data *cd)
raw_spin_lock_irq(&ctx->timeout_lock);
req = io_timeout_extract(ctx, cd);
- if (!IS_ERR(req))
+ if (!IS_ERR(req)) {
atomic_set(&ctx->cq_timeouts,
atomic_read(&ctx->cq_timeouts) + 1);
+ atomic_inc(&ctx->cq_timeouts_pending);
+ }
raw_spin_unlock_irq(&ctx->timeout_lock);
if (IS_ERR(req))
return PTR_ERR(req);
- io_req_task_queue_fail(req, -ECANCELED);
+ io_req_set_res(req, -ECANCELED, 0);
+ req->io_task_work.func = io_timeout_complete_cancelled;
+ io_req_task_work_add(req);
return 0;
}
@@ -708,7 +736,7 @@ int io_timeout(struct io_kiocb *req, unsigned int issue_flags)
goto add;
}
- tail = data_race(ctx->cached_cq_tail) - atomic_read(&ctx->cq_timeouts);
+ tail = io_timeout_seq(ctx);
/*
* Update the last seq here in case io_flush_timeouts() hasn't, it only
@@ -722,11 +750,6 @@ int io_timeout(struct io_kiocb *req, unsigned int issue_flags)
ctx->cq_last_tm_flush = tail;
base = ctx->cq_last_tm_flush;
- /*
- * A fired timeout bumps ->cq_timeouts before its CQE is posted, which
- * leaves the sequence behind the last flush until it is. Nothing else
- * separates the two then, so count from the flush.
- */
if ((s32)(tail - base) < 0)
tail = base;
timeout->target_seq = tail + off;