[PATCH 2/2] io_uring: clear IO_RING_F_DRAIN_ACTIVE when all drains have finished
From: Zhiqi Zhang
Date: Tue Nov 17 2026 - 03:50:57 EST
io_queue_deferred() only sets ctx->drain_req for standalone drain
requests, leaving ctx->drain_req NULL when a linked drain request is
dispatched. If a non-drain request is waiting in ->defer_list behind
the linked drain:
1. P0 in flight
2. Linked drain [D0 (IOSQE_IO_DRAIN | IOSQE_IO_LINK), D1] queued
3. Non-drain P1 (e.g. POLL_ADD) queued to ->defer_list
4. P0 completes, [D0, D1] is dispatched with ctx->drain_req == NULL
5. [D0, D1] completes and frees; drain_done stays false
6. io_queue_deferred() pops P1 (non-drain) and empties ->defer_list
Because drain_done is false, __io_submit_flush_completions() leaves
IO_RING_F_DRAIN_ACTIVE set permanently after all drain requests have
finished. Any subsequent non-drain submission (e.g. a WRITE or cancel
targeting P1) is forced through io_drain_req() and deadlocks behind P1.
The same happens for a standalone io-wq drain request if its CQE is
flushed while P1 stays deferred awaiting the worker's final reference
drop, because ctx->drain_req is already cleared before the later flush
that frees the request and starts P1.
When io_queue_deferred() observes ctx->nr_req_allocated ==
ctx->nr_drained before dequeuing (nothing outside ->defer_list is
alive), all prior drains have completed and been freed. If the loop
drains ->defer_list without starting another REQ_F_IO_DRAIN request,
clear IO_RING_F_DRAIN_ACTIVE. That also subsumes the !drain clearing in
io_drain_req().
Fixes: ecb6817b769f ("io_uring: end the drain when the drain request completes")
Cc: stable@xxxxxxxxxxxxxxx
Signed-off-by: Zhiqi Zhang <zhiqizhangg@xxxxxxxxx>
---
io_uring/io_uring.c | 11 ++++++-----
1 file changed, 6 insertions(+), 5 deletions(-)
diff --git a/io_uring/io_uring.c b/io_uring/io_uring.c
index 4e8b93a584b4..1fb2b9bb601f 100644
--- a/io_uring/io_uring.c
+++ b/io_uring/io_uring.c
@@ -498,11 +498,13 @@ unsigned io_linked_nr(struct io_kiocb *req)
static __cold noinline bool io_queue_deferred(struct io_ring_ctx *ctx)
{
bool drain_seen = false, first = true, drain_queued = false;
+ bool nothing_alive;
lockdep_assert_held(&ctx->uring_lock);
__io_req_caches_free(ctx);
/* nothing outside of ->defer_list is alive */
- if (ctx->nr_req_allocated == ctx->nr_drained)
+ nothing_alive = ctx->nr_req_allocated == ctx->nr_drained;
+ if (nothing_alive)
ctx->drain_req = NULL;
while (!list_empty(&ctx->defer_list)) {
@@ -525,6 +527,9 @@ static __cold noinline bool io_queue_deferred(struct io_ring_ctx *ctx)
kfree(de);
first = false;
}
+
+ if (nothing_alive && !drain_queued)
+ ctx->int_flags &= ~IO_RING_F_DRAIN_ACTIVE;
return drain_queued;
}
@@ -1473,7 +1478,6 @@ static __cold void io_drain_req(struct io_kiocb *req)
struct io_ring_ctx *ctx = req->ctx;
struct io_defer_entry *de;
bool drain_next = false;
- bool drain;
de = kmalloc_obj(*de, GFP_KERNEL_ACCOUNT);
if (!de) {
@@ -1506,7 +1510,6 @@ static __cold void io_drain_req(struct io_kiocb *req)
if (unlikely(drain_next))
ctx->int_flags |= IO_RING_F_DRAIN_NEXT;
req->flags |= REQ_F_FORCE_ASYNC;
- drain = req->flags & REQ_F_IO_DRAIN;
io_prep_async_link(req);
trace_io_uring_defer(req);
@@ -1515,8 +1518,6 @@ static __cold void io_drain_req(struct io_kiocb *req)
ctx->nr_drained += io_linked_nr(req);
list_add_tail(&de->list, &ctx->defer_list);
io_queue_deferred(ctx);
- if (!drain && list_empty(&ctx->defer_list))
- ctx->int_flags &= ~IO_RING_F_DRAIN_ACTIVE;
}
#define REQ_ISSUE_SLOW_FLAGS (REQ_F_CREDS | REQ_F_ARM_LTIMEOUT)
--
2.43.0