[PATCH 0/2] io_uring: fix stuck IO_RING_F_DRAIN_ACTIVE and IO_RING_F_DRAIN_NEXT

From: Zhiqi Zhang

Date: Tue Nov 17 2026 - 03:50:46 EST


This two-patch series fixes two state-lifecycle bugs in io_uring request
draining that can leave IO_RING_F_DRAIN_ACTIVE or IO_RING_F_DRAIN_NEXT
permanently set on a ring, causing subsequent unrelated non-drain
submissions to be redirected through io_drain_req() and deadlock behind
in-flight requests.

Patch 1/2 stops touching IO_RING_F_DRAIN_ACTIVE and IO_RING_F_DRAIN_NEXT
inside io_init_req() and defers drain activation until io_drain_req()
has validated the request/link and allocated its struct io_defer_entry.
This prevents failed SQEs (restriction, ioprio, IOPOLL, personality,
def->prep(), BPF filters), failed links, and -ENOMEM in io_drain_req()
from leaking active drain state.

Patch 2/2 ensures IO_RING_F_DRAIN_ACTIVE is cleared in io_queue_deferred()
when all outside requests have completed and been freed
(ctx->nr_req_allocated == ctx->nr_drained) and ->defer_list drains
without dispatching another REQ_F_IO_DRAIN request. This fixes the
permanent drain stall after linked drain requests (where ctx->drain_req
stays NULL) or io-wq drain requests whose CQE flushed while a trailing
non-drain request remained deferred awaiting the worker's last reference
drop.

Base commit: 666539eecdf4 ("Merge tag 'assoc-array-fixes-v2'") (v7.4-rc3+).
Note that linux-next converts direct req->flags updates in
io_uring/io_uring.c to io_req_set_flags(), which touches overlapping
lines if forward-ported.

Testing:
- Built arm64 kernel with CONFIG_IO_URING=y, CONFIG_KASAN=y,
CONFIG_PROVE_LOCKING=y, CONFIG_UBSAN=y, and CONFIG_DEBUG_ATOMIC_SLEEP=y,
plus W=1 and alternate config (IO_URING_BPF=n, COMPAT=n, KASAN=n) with
zero new warnings.
- Verified in QEMU with a standalone syscall test suite across default,
IORING_SETUP_SUBMIT_ALL, IORING_SETUP_SINGLE_ISSUER |
IORING_SETUP_DEFER_TASKRUN, and IORING_SETUP_SQPOLL rings:
- Unpatched 666539eecdf4: 20/32 deadlock failures (all 5 deadlock
reproducers across all 4 ring modes).
- With 1/2 only: 16/16 init/link failure cases pass; linked-drain
cleanup still fails (4/32 failures).
- With 1/2 + 2/2: 0/44 failures across all cases, including
restriction/personality/prep failures, post-midlink failed SQEs,
consecutive drain ordering, and io-wq drains, with no KASAN/UBSAN/
lockdep warnings.

AI assistance:
- I found the bugs, wrote the reproducer and test cases, reviewed the
code changes, and tested them locally.
- An AI coding assistant wrote the fix changes in 1/2 and 2/2 and helped
draft the commit messages and cover letter.

Zhiqi Zhang (2):
io_uring: avoid activating drain for failed requests or links
io_uring: clear IO_RING_F_DRAIN_ACTIVE when all drains have finished

io_uring/io_uring.c | 107 +++++++++++++++++++++++++-------------------
1 file changed, 61 insertions(+), 46 deletions(-)


base-commit: 666539eecdf4db623f5e3c3e55cf9f2dfad24aa4
--
2.43.0