Queued patches for 6.6-stable
Subsystems (2)
Jens Axboe
maintainerHi,
Like the 6.1 series, but for 6.6-stable.
Please apply!
Signature · 1 line
Jens Axboe
From 28fbb29b23a9fc14463fe1093c673c28bea7446f Mon Sep 17 00:00:00 2001
From: Jens Axboe <[email protected]>
Date: Fri, 16 Oct 2026 09:15:13 -0600
Subject: [PATCH 27/27] io_uring/poll: don't loop on HUP with multiple
references
Commit cf1d5c9d335e2c673811da641531da82cb01393a upstream.
io_poll_check_events() drops down to one reference when a multishot request sees HUP with more than one, so that the loop goes around once more. It did so by subtracting one less than it had. With the cmpxchg that only takes the local count down: the exchange fails, the next pass reads the unchanged count, and goes through the same motions again. The loop then only ends once io_poll_issue() stops the request.
Drop the extra references from the counter instead, and loop once more without trying the exchange. The next pass sees one reference, like it did before, and ends unless a wake up came in.
--- io_uring/poll.c | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-)
io_uring/poll.c +13 -4
@@ -258,12 +258,14 @@ static inline void io_poll_execute(struct io_kiocb *req, int res,*/ static int io_poll_check_events (struct io_kiocb *req,struct io_tw_state *ts) { +bool again;int v;if (unlikely (io_should_terminate_tw (req->ctx)))return -ECANCELED ; do { + again = false; v = atomic_read(&req->poll_refs); if (unlikely(v != 1)) {
@@ -325,10 +327,17 @@ static int io_poll_check_events(struct io_kiocb *req, struct io_tw_state *ts) }else {int ret; -/* multiple refs and HUP, ensure we loop once more */ +/* +* Multiple refs and HUP, ensure we loop once more. Drop +* down to one ref now, so the next pass ends unless a +* wake up came in. +*/ if ((req->cqe.res & (POLLHUP |POLLRDHUP )) && - (v &IO_POLL_REF_MASK ) !=1 ) - v--; + (v &IO_POLL_REF_MASK ) !=1 ) { +atomic_sub ((v &IO_POLL_REF_MASK ) -1 , + &req->poll_refs); + again = true; + } ret =io_poll_issue (req, ts);if (ret ==IOU_STOP_MULTISHOT )return IOU_POLL_REMOVE_POLL_USE_RES ;
@@ -346,7 +355,7 @@ static int io_poll_check_events(struct io_kiocb *req, struct io_tw_state *ts)* task_work while we were executing it. */ v &=IO_POLL_REF_MASK ; - }while (atomic_cmpxchg (&req->poll_refs, v,0 ) != v); + }while (again ||atomic_cmpxchg (&req->poll_refs, v,0 ) != v);return IOU_POLL_NO_ACTION ; }
Signature · 1 line
2.56.0
From cc1b2a8bb0f239f12a07cc31949afac4660415c5 Mon Sep 17 00:00:00 2001
From: Jens Axboe <[email protected]>
Date: Thu, 15 Oct 2026 16:38:45 -0600
Subject: [PATCH 26/27] io_uring/timeout: don't lose a cancel as io-wq arms a
timeout
Commit 89f193dc3ef462059387e9691bf6af4b1808379a upstream.
An IORING_OP_TIMEOUT run by io-wq, because of IOSQE_ASYNC, has the same problem as poll, futex and waitid. io_wq_submit_work() looks at IO_WQ_WORK_CANCEL once, before it issues the request. A cancel in between flags the work, gets -EALREADY, and searches ->timeout_list in vain, while the worker then adds the request and starts the timer:
io-wq worker io_try_cancel()
io_wq_submit_work()
IO_WQ_WORK_CANCEL isn't set
io_async_cancel_one()
sets IO_WQ_WORK_CANCEL = -EALREADY
io_timeout_cancel() = -ENOENT
io_timeout()
adds the request to ->timeout_list
hrtimer_start()
= IOU_ISSUE_SKIP_COMPLETE
Nobody looks at the flag again, and the timeout runs its course. At ring exit, io_uring_try_cancel_requests() cancels io-wq work and then kills the timeouts the same way, so an exiting task can get stuck for the full duration of such a timeout.
Look at the flag under ->timeout_lock, which the search takes as well, and fail the request if it is set. If the search came first, the flag is visible. If it comes later, it finds the request on the list.
[ 6.6: carries the __io_wq_cancel_seen()/io_wq_cancel_seen() helpers that
the futex/waitid fix 5cb049d2203d brought upstream; work->flags is not atomic and ->timeout_lock isn't raw here ]
--- io_uring/io_uring.h | 23 +++++++++++++++++++++++ io_uring/timeout.c | 7 +++++++ 2 files changed, 30 insertions(+)
io_uring/io_uring.h +23 -0
@@ -259,6 +259,29 @@ static inline void io_ring_submit_lock(struct io_ring_ctx *ctx,lockdep_assert_held (&ctx->uring_lock); } +/* +* For a handler that is run by io-wq and parks the request on a list where +* cancellations find it, rather than completing it. Whoever tried to cancel +* the request before that found it running. It has flagged the work item, +* has searched the list in vain, and won't come back. To be called with +* the lock the search takes held: if the search came first, the flag is +* visible. For most lists that is ->uring_lock, timeouts use ->timeout_lock. +*/ +static inline bool __io_wq_cancel_seen (struct io_kiocb *req, +unsigned int issue_flags) +{ +return (issue_flags &IO_URING_F_IOWQ ) && + (req->work.flags &IO_WQ_WORK_CANCEL ); +} + +static inline bool io_wq_cancel_seen (struct io_kiocb *req, +unsigned int issue_flags) +{ +lockdep_assert_held (&req->ctx->uring_lock); + +return __io_wq_cancel_seen (req, issue_flags); +} +static inline void io_commit_cqring (struct io_ring_ctx *ctx) {/* order cqe stores with ring update */
io_uring/timeout.c +7 -0
@@ -609,6 +609,13 @@ int io_timeout(struct io_kiocb *req, unsigned int issue_flags)spin_lock_irq (&ctx->timeout_lock); +/* the list is searched under ->timeout_lock, see io_wq_cancel_seen() */ +if (__io_wq_cancel_seen (req, issue_flags)) { +spin_unlock_irq (&ctx->timeout_lock); +io_req_task_queue_fail (req, -ECANCELED ); +return IOU_ISSUE_SKIP_COMPLETE ; + } +/* * sqe->off holds how many events that need to occur for this * timeout event to be satisfied. If it isn't set, then this is
Signature · 1 line
2.56.0
From b08314423916a45259a47f2502015e64098a815a Mon Sep 17 00:00:00 2001
From: Jens Axboe <[email protected]>
Date: Thu, 15 Oct 2026 14:25:00 -0600
Subject: [PATCH 25/27] io_uring/timeout: don't flush with the sequence behind
the last flush
Commit 85f85d573ffa7772e1ad4504d0cec5113a5c5cf1 upstream.
io_flush_timeouts() measures the events since the last flush as the distance of the current sequence from ->cq_last_tm_flush. A fired timeout bumps ->cq_timeouts before its CQE is posted, so until it is the sequence can be behind the last flush, and the distance wraps:
last flush: seq 100, cq_last_tm_flush = 100
Y: IORING_OP_TIMEOUT, off 50, target_seq 150 pure timeout A fires, cq_timeouts = 1 task_work posts A's CQE, cached_cq_tail = 101 pure timeout B fires before the flush, cq_timeouts = 2 io_flush_timeouts(): seq = 99, events_got = 0xffffffff
Y, and every sequence timeout behind it, is ended with -ETIME although no event happened. Two timeouts expiring close together is all it takes, a completion with IOSQE_CQE_SKIP_SUCCESS after a single one does it too.
Nothing but those in flight CQEs separates the two, so count from the last flush when the sequence is behind it, as io_timeout() does.
--- io_uring/timeout.c | 8 ++++++++ 1 file changed, 8 insertions(+)
io_uring/timeout.c +8 -0
@@ -116,6 +116,14 @@ __cold void io_flush_timeouts(struct io_ring_ctx *ctx)spin_lock_irq (&ctx->timeout_lock); seq = ctx->cached_cq_tail -atomic_read (&ctx->cq_timeouts); +/* +* 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; +list_for_each_entry_safe (timeout, tmp, &ctx->timeout_list, list) {struct io_kiocb *req =cmd_to_io_kiocb (timeout);u32 events_needed, events_got;
Signature · 1 line
2.56.0
From 970910e62df15cd68bb0fb4bd3c917836019db25 Mon Sep 17 00:00:00 2001
From: Jens Axboe <[email protected]>
Date: Thu, 15 Oct 2026 14:18:22 -0600
Subject: [PATCH 24/27] io_uring/timeout: don't put a sequence target behind
the last flush
Commit fdfce905a58edea053ee7b68b5bd4348dd1b36e0 upstream.
io_timeout_fn() and io_kill_timeout() bump ->cq_timeouts when a timeout fires, but its CQE is only posted later from task_work. Until then the sequence io_timeout() computes is one below the last flush per fired timeout. Now that ->cq_last_tm_flush is kept while sequence timeouts are pending, a timeout armed in that window with a small offset gets a target behind the base. Its distance from the base wraps, and it's never completed by events, only by its timer. With DEFER_TASKRUN that window is as wide as the application makes it, as submission runs before the local task_work.
Nothing but those in flight CQEs separates the two, so use the last flush as the base for the target when the sequence is behind it.
--- io_uring/timeout.c | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-)
io_uring/timeout.c +9 -1
@@ -612,7 +612,6 @@ int io_timeout(struct io_kiocb *req, unsigned int issue_flags) } tail =data_race (ctx->cached_cq_tail) -atomic_read (&ctx->cq_timeouts); - timeout->target_seq = tail + off;/* * Update the last seq here in case io_flush_timeouts() hasn't, it only
@@ -626,6 +625,15 @@ 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; +/* * Insertion sort, ensuring the first entry in the list is always * the one we need first.
Signature · 1 line
2.56.0
From e8014cce7fd738a8be4f9516f0022a3067794d48 Mon Sep 17 00:00:00 2001
From: Juanlu Herrero <[email protected]>
Date: Tue, 13 Oct 2026 15:37:20 -0700
Subject: [PATCH 23/27] io_uring/poll: avoid retry wakeup loss when dropping
ownership
Commit 937d38dcacd675dc0572d6c9b6743960df3128ed upstream.
If a wakeup sets IO_POLL_RETRY_FLAG between the owning task consuming the existing instance of IO_POLL_RETRY_FLAG and the loop check of io_poll_check_events(), then we stall on processing the request until an unrelated wakeup comes along.
Using atomic_cmpxchg instead of atomic_sub_return to guarantee both the count v and poll_refs are 0 and no wake-ups are missed.
--- io_uring/poll.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
io_uring/poll.c +2 -2
@@ -345,8 +345,8 @@ static int io_poll_check_events(struct io_kiocb *req, struct io_tw_state *ts)* Release all references, retry if someone tried to restart * task_work while we were executing it. */ - }while (atomic_sub_return (v &IO_POLL_REF_MASK , &req->poll_refs) & -IO_POLL_REF_MASK ); + v &=IO_POLL_REF_MASK ; + }while (atomic_cmpxchg (&req->poll_refs, v,0 ) != v);return IOU_POLL_NO_ACTION ; }
Signature · 1 line
2.56.0
From 4b776dabb5c719698881452460bfc4dfceeccab1 Mon Sep 17 00:00:00 2001
From: Josef Bacik <[email protected]>
Date: Thu, 15 Oct 2026 19:31:07 +0000
Subject: [PATCH 22/27] io_uring: look up the file before queueing to io-wq
Commit 98bc044d5a5374f8d3605fe55a8e2df6d6fc3f82 upstream.
io_prep_async_work() decides by the file of a request what kind of work it is for io-wq. I/O on regular files and block devices is bounded work, limited to min(SQ entries, 4 * CPUs) workers, for anything else that may block indefinitely the limit is RLIMIT_NPROC. Writes to a regular file are hashed by the inode, so that they are run one after the other instead of having a bunch of workers fight for the inode lock.
Since the file is only assigned when a request is issued, a request that is queued without an attempt to issue it, REQ_F_FORCE_ASYNC, doesn't have one at that point. io_prep_async_work() takes that for "not a regular file or block device":
- Reads and writes end up as unbound work. 32 reads with IOSQE_ASYNC from a slow block device on a ring with 4 SQ entries get 32 workers, not 4.
- Writes aren't hashed. Four buffered writes to one file get four workers, three of which do nothing but wait for the lock.
The late assignment is for the benefit of links, where a request may use a fixed file that one of its predecessors installs. By the time a request gets to io_queue_iowq() these are done, it is as good as issued. Assign the file there.
io_assign_file() moves up and is forced inline. With a third caller the compiler may stop inlining it, and every request that is issued would pay for a call.
IORING_OP_SPLICE is left as it is, unbound and not hashed. The file of the request is where it writes to, which says nothing about how long it
takes: a splice from an empty pipe or a socket into a regular file waits for its input. As bounded work, four of these on a ring with 4 SQ entries keep all other bounded work of the task from running, and as hashed work one of them blocks all writes to that file.
REQ_F_FORCE_ASYNC is not only set for IOSQE_ASYNC. What changes, and brings back what was the case before the file assignment was deferred:
- Requests with IOSQE_ASYNC or IOSQE_IO_DRAIN, and all requests that are submitted while a drain is active: reads and writes on regular files and block devices count against the limit for bounded workers, including the one set with IORING_REGISTER_IOWQ_MAX_WORKERS, so fewer of them run at the same time.
- Of these, writes to one regular file are run one at a time, unless they are O_DIRECT and the filesystem has FOP_DIO_PARALLEL_WRITE.
- IORING_OP_FALLOCATE and IORING_OP_FTRUNCATE, which always go to io-wq, are run one at a time per file, and in turn with these writes.
- The descriptor or the fixed file slot is looked up when the request is queued, not when a worker gets around to it, so closing or replacing it in between no longer has an effect on the request.
--- io_uring/io_uring.c | 43 +++++++++++++++++++++++++++++-------------- 1 file changed, 29 insertions(+), 14 deletions(-)
io_uring/io_uring.c +29 -14
66 lines not on this page · raw message
Signature · 1 line
2.56.0
From b3a03402d178dd4ef14a8499f07e36d38e19dd8a Mon Sep 17 00:00:00 2001
From: Josef Bacik <[email protected]>
Date: Thu, 15 Oct 2026 19:31:05 +0000
Subject: [PATCH 21/27] io_uring/timeout: no ABS update of multishot timeouts
Commit b088ae34d4da730dc647261b893a6eb622fe5e1f upstream.
IORING_TIMEOUT_MULTISHOT can't be combined with IORING_TIMEOUT_ABS, prep rejects that, as the request is armed again with the same value each time it fires. IORING_TIMEOUT_UPDATE doesn't check and takes an absolute time for a multishot timeout. The first expiry is as asked for, but io_timeout_complete() then arms the timer with ->mode of the original request, i.e. it uses the absolute time as the interval. With CLOCK_MONOTONIC the next expiry is an uptime away, with CLOCK_REALTIME more than fifty years.
Fail such an update with -EINVAL, and do so before the timeout is touched.
--- io_uring/timeout.c | 38 ++++++++++++++++++++++++++------------ 1 file changed, 26 insertions(+), 12 deletions(-)
io_uring/timeout.c +26 -12
@@ -257,22 +257,29 @@ static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer)return HRTIMER_NORESTART ; } +static struct io_kiocb *io_timeout_find (struct io_ring_ctx *ctx, +struct io_cancel_data *cd) + __must_hold(&ctx->timeout_lock) +{ +struct io_timeout *timeout; + +list_for_each_entry (timeout, &ctx->timeout_list, list) { +struct io_kiocb *req =cmd_to_io_kiocb (timeout); + +if (io_cancel_req_match (req, cd)) +return req; + } +return NULL ; +} +static struct io_kiocb *io_timeout_extract (struct io_ring_ctx *ctx,struct io_cancel_data *cd) __must_hold(&ctx->timeout_lock) { +struct io_kiocb *req =io_timeout_find (ctx, cd);struct io_timeout *timeout;struct io_timeout_data *io; -struct io_kiocb *req =NULL ; -list_for_each_entry (timeout, &ctx->timeout_list, list) { -struct io_kiocb *tmp =cmd_to_io_kiocb (timeout); - -if (io_cancel_req_match (tmp, cd)) { - req = tmp; -break ; - } - }if (!req)return ERR_PTR (-ENOENT );
@@ -404,14 +411,21 @@ static int io_timeout_update(struct io_ring_ctx *ctx, __u64 user_data, __must_hold(&ctx->timeout_lock) {struct io_cancel_data cd = { .ctx = ctx, .data = user_data, }; -struct io_kiocb *req =io_timeout_extract (ctx, &cd); +struct io_kiocb *req =io_timeout_find (ctx, &cd); struct io_timeout *timeout = io_kiocb_to_cmd(req, struct io_timeout);struct io_timeout_data *data; -if (IS_ERR (req)) -return PTR_ERR (req); +if (!req) +return -ENOENT ; data = req->async_data; +/* multishot requests only make sense with rel values */ +if ((data->flags &IORING_TIMEOUT_MULTISHOT ) && mode ==HRTIMER_MODE_ABS ) +return -EINVAL ; + + req =io_timeout_extract (ctx, &cd); +if (IS_ERR (req)) +return PTR_ERR (req);/* for a multishot request this isn't a sequence, keep the repeats */ if (!(data->flags &IORING_TIMEOUT_MULTISHOT )) timeout->off =0 ;/* noseq */
Signature · 1 line
2.56.0
From d2f0f4117c1dbe18c4b6026b674360c570776872 Mon Sep 17 00:00:00 2001
From: Josef Bacik <[email protected]>
Date: Thu, 15 Oct 2026 19:31:03 +0000
Subject: [PATCH 20/27] io_uring: don't pass restart error codes to userspace
Commit e1a64bc64897f4c2fe460922c345f2ed8f889b5f upstream.
-ERESTARTSYS and friends are instructions for the system call exit code, which either restarts the call or turns them into -EINTR. They are not supposed to be seen by applications. A request isn't a system call, nobody looks at what it returns before that is put into a CQE.
The read/write and the networking opcodes convert these codes to -EINTR by hand. The others don't. An io-wq worker that is blocked in the open of a FIFO, a tty or some other device, or in a splice from an empty pipe, and that gets interrupted because the request is cancelled, completes it with -512:
IORING_OP_OPENAT, IOSQE_ASYNC // FIFO without a writer
IORING_OP_ASYNC_CANCEL // -EALREADY, worker is signalled
CQE of the open: res == -512
There are more candidates, like whatever a file system or driver returns from ->flush(), ->fsync(), ->fallocate() or ->uring_cmd(), or breaking a lease or delegation.
Take care of it in io_issue_sqe(), which all of them go through. The helper is moved from rw.c as is. The cost is a compare and a branch that is not taken per request.
--- io_uring/io_uring.c | 8 +++++++- io_uring/io_uring.h | 18 ++++++++++++++++++ io_uring/rw.c | 18 ------------------ 3 files changed, 25 insertions(+), 19 deletions(-)
io_uring/io_uring.c +7 -1
@@ -1906,6 +1906,9 @@ static int io_issue_sqe(struct io_kiocb *req, unsigned int issue_flags) }if (ret ==IOU_OK ) { +/* not all handlers take care of the internal restart codes */ +if (unlikely (req->cqe.res <= -ERESTARTSYS )) + req->cqe.res =io_fixup_restart_res (req->cqe.res);if (issue_flags &IO_URING_F_COMPLETE_DEFER )io_req_complete_defer (req);else
@@ -1914,8 +1917,11 @@ static int io_issue_sqe(struct io_kiocb *req, unsigned int issue_flags)return 0 ; } -if (ret !=IOU_ISSUE_SKIP_COMPLETE ) +if (ret !=IOU_ISSUE_SKIP_COMPLETE ) { +if (unlikely (ret <= -ERESTARTSYS )) + ret =io_fixup_restart_res (ret);return ret; + }/* If the op doesn't have a file, we're not polling for it */ if ((req->ctx->flags &IORING_SETUP_IOPOLL ) && def->iopoll_queue)
io_uring/io_uring.h +18 -0
@@ -193,6 +193,24 @@ static __always_inline bool io_fill_cqe_req(struct io_ring_ctx *ctx,return true; } +static inline ssize_t io_fixup_restart_res (ssize_t ret) +{ +switch (ret) { +case -ERESTARTSYS : +case -ERESTARTNOINTR : +case -ERESTARTNOHAND : +case -ERESTART_RESTARTBLOCK : +/* +* We can't just restart the syscall, since previously +* submitted sqes may already be in progress. Just fail +* this IO with EINTR. +*/ +return -EINTR ; +default : +return ret; + } +} +static inline void req_set_fail (struct io_kiocb *req) { req->flags |=REQ_F_FAIL ;
io_uring/rw.c +0 -18
@@ -140,24 +140,6 @@ void io_readv_writev_cleanup(struct io_kiocb *req)kfree (io->free_iovec); } -static inline ssize_t io_fixup_restart_res (ssize_t ret) -{ -switch (ret) { -case -ERESTARTSYS : -case -ERESTARTNOINTR : -case -ERESTARTNOHAND : -case -ERESTART_RESTARTBLOCK : -/* -* We can't just restart the syscall, since previously -* submitted sqes may already be in progress. Just fail -* this IO with EINTR. -*/ -return -EINTR ; -default : -return ret; - } -} -static inline void io_rw_done (struct kiocb *kiocb,ssize_t ret) {/* IO was queued async, completion will happen later */
Signature · 1 line
2.56.0
From 9bb34c8bc62914f4bfc24804a4b53bec87274fd4 Mon Sep 17 00:00:00 2001
From: Josef Bacik <[email protected]>
Date: Thu, 15 Oct 2026 19:31:01 +0000
Subject: [PATCH 19/27] io_uring/sqpoll: let the thread sleep on disabled rings
Commit d492dcbb3b81c1c2d0aabef41fa1307093f3a556 upstream.
__io_sq_thread() doesn't submit anything as long as the ring has IORING_SETUP_R_DISABLED set. The check that keeps the SQPOLL thread from going to sleep while the SQ ring holds entries doesn't know about that. If an application fills in SQEs first and enables the ring later, the thread finds that there is work each time it is about to sleep, goes around, doesn't submit it, and so on. It uses up a CPU until the ring gets enabled.
Entries in the SQ ring of a disabled ring are no reason to stay awake. io_register_enable_rings() clears the flag and then wakes the thread, and the flag is looked at after prepare_to_wait(), so the thread can't miss that.
--- io_uring/sqpoll.c | 7 +++++++ 1 file changed, 7 insertions(+)
io_uring/sqpoll.c +7 -0
@@ -300,6 +300,13 @@ static int io_sq_thread(void *data)*/ smp_mb__after_atomic (); +/* +* Nothing gets submitted before the ring is +* enabled, which comes with a wake up. +*/ +if (READ_ONCE (ctx->flags) &IORING_SETUP_R_DISABLED ) +continue ; +if (io_sqring_entries (ctx)) { needs_sched = false;break ;
Signature · 1 line
2.56.0
From aee6183ac16edfd5baeaff4a42183defc04f87fc Mon Sep 17 00:00:00 2001
From: Josef Bacik <[email protected]>
Date: Thu, 15 Oct 2026 19:30:54 +0000
Subject: [PATCH 18/27] io_uring/timeout: keep repeat count on multishot update
Commit 3dc2a32cd356ee382aa4a8c7d844c53b2c0ad83b upstream.
IORING_TIMEOUT_UPDATE clears ->off of the timeout it updates, to turn a timeout that waits for a number of completions into a pure one. For a multishot timeout ->off isn't a sequence though, it tells whether the number of repeats is limited: io_timeout_finish() only looks at ->repeats if ->off is set.
A multishot timeout that was set up to fire N times hence fires forever once its interval has been updated. Leave ->off alone for those, they are never treated as sequence timeouts anyway.
--- io_uring/timeout.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
io_uring/timeout.c +3 -1
@@ -411,8 +411,10 @@ static int io_timeout_update(struct io_ring_ctx *ctx, __u64 user_data,if (IS_ERR (req))return PTR_ERR (req); - timeout->off =0 ;/* noseq */ data = req->async_data; +/* for a multishot request this isn't a sequence, keep the repeats */ +if (!(data->flags &IORING_TIMEOUT_MULTISHOT )) + timeout->off =0 ;/* noseq */ data->ts = *ts;list_add_tail (&timeout->list, &ctx->timeout_list);
Signature · 1 line
2.56.0
From f53abf9c5fc7d8b49078446de7605814a7e4231e Mon Sep 17 00:00:00 2001
From: Josef Bacik <[email protected]>
Date: Thu, 15 Oct 2026 19:30:53 +0000
Subject: [PATCH 17/27] io_uring/timeout: don't skip over reached sequences
Commit c6c0b3eba88c3b13ca11ea112404982e86d58f7a upstream.
io_flush_timeouts() copes with the wrap of the CQ sequence by looking at the distance from ->cq_last_tm_flush. That relies on the target of each pending timeout being at or ahead of the last flush.
io_timeout() breaks that. It sets ->cq_last_tm_flush to the current sequence, on the assumption that nothing has been posted since the last flush. That isn't true. A multishot request posts its CQEs right when it is issued or retried, but they are committed, and the timeouts flushed, at the end of the submission or task_work run:
X: IORING_OP_TIMEOUT, off == 1 target_seq == N + 1
io_submit_sqes()
multishot recv, posts two CQEs sequence == N + 2
Y: IORING_OP_TIMEOUT, off == 100 ->cq_last_tm_flush = N + 2 io_flush_timeouts()
X: events_needed = N + 1 - (N + 2) // 0xffffffff
events_got = 0 // not yet
X isn't completed although the completions it was waiting for have happened, only its timer can end it now. And as the list is sorted and the walk ends at the first timeout that isn't due, it gets in the way of the ones behind it. The insertion sort has the same problem, it measures the distance of the pending targets from the current sequence.
->cq_last_tm_flush only needs to be brought up to date if no timeout is waiting for a sequence, as io_flush_timeouts() isn't called before the first one is set up. Otherwise it is current enough. Timeouts with a sequence come first in the list, so looking at the first entry will do. Use the last flush as the base for the insertion sort as well.
--- io_uring/timeout.c | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-)
io_uring/timeout.c +14 -7
@@ -579,8 +579,9 @@ int io_timeout(struct io_kiocb *req, unsigned int issue_flags) struct io_timeout *timeout = io_kiocb_to_cmd(req, struct io_timeout);struct io_ring_ctx *ctx = req->ctx;struct io_timeout_data *data = req->async_data; +struct io_timeout *first;struct list_head *entry; -u32 tail, off = timeout->off; +u32 tail, base, off = timeout->off;spin_lock_irq (&ctx->timeout_lock);
@@ -597,11 +598,17 @@ int io_timeout(struct io_kiocb *req, unsigned int issue_flags) tail =data_race (ctx->cached_cq_tail) -atomic_read (&ctx->cq_timeouts); timeout->target_seq = tail + off; -/* Update the last seq here in case io_flush_timeouts() hasn't. -* This is safe because ->completion_lock is held, and submissions -* and completions are never mixed in the same ->completion_lock section. +/* +* Update the last seq here in case io_flush_timeouts() hasn't, it only +* gets called once sequences are in use. Leave it alone if there are +* timeouts waiting for one. CQEs may have been posted since the last +* flush, and a target that has been reached must not fall behind it. */ - ctx->cq_last_tm_flush = tail; + first = list_first_entry_or_null(&ctx->timeout_list, struct io_timeout, + list); +if (!first ||io_is_timeout_noseq (cmd_to_io_kiocb (first))) + ctx->cq_last_tm_flush = tail; + base = ctx->cq_last_tm_flush;/* * Insertion sort, ensuring the first entry in the list is always
@@ -613,8 +620,8 @@ int io_timeout(struct io_kiocb *req, unsigned int issue_flags)if (io_is_timeout_noseq (nxt))continue ; -/* nxt.seq is behind @tail, otherwise would've been completed */ -if (off >= nextt->target_seq - tail) +/* no target is behind the last flush, see io_flush_timeouts() */ +if (timeout->target_seq - base >= nextt->target_seq - base)break ; } add:
Signature · 1 line
2.56.0
From 63aa51337d95030cd2833e7626e6b4a0edc88fd3 Mon Sep 17 00:00:00 2001
From: Josef Bacik <[email protected]>
Date: Thu, 15 Oct 2026 19:30:45 +0000
Subject: [PATCH 16/27] io_uring/poll: drop stale double poll entry on rearm
Commit f2dbe87968a3aef74a11c025776d293b80964d3f upstream.
A file that uses two wait queues for poll, e.g. a tty, gets a second io_poll entry, which IORING_OP_POLL_ADD keeps in ->async_data until the request is freed. io_poll_add() can run more than once for a request
though: IORING_OP_POLL_REMOVE with one of the update flags disarms and rearms it, and io_poll_task_func() reissues it if a wake up turns out to be spurious. The second poll_wait() of the new round then finds *poll_ptr set, with ->head cleared by the disarm, and __io_queue_proc() fails what it takes for a third wait queue with -EINVAL.
For an update that means that the poll request is completed with -ECANCELED, while the update itself reports success. For a spurious wake up, e.g. tcsetattr() on the polled tty, the poll request fails with -EINVAL.
Free the old entry and clear the poll flags before arming, which is what io_arm_apoll() does for internal polling. All entries have been removed from their wait queues at that point.
--- io_uring/poll.c | 12 ++++++++++++ 1 file changed, 12 insertions(+)
io_uring/poll.c +12 -0
@@ -983,6 +983,18 @@ int io_poll_add(struct io_kiocb *req, unsigned int issue_flags)if (req->ctx->flags & (IORING_SETUP_SQPOLL |IORING_SETUP_SINGLE_ISSUER )) req->flags |=REQ_F_HASH_LOCKED ; +/* +* A poll update or a spurious wake up arms the request again. Forget +* the entries of the previous round like io_arm_apoll() does, a stale +* second entry would be taken for a third wait queue. +*/ +if (req_has_async_data (req)) { +kfree (req->async_data); + req->async_data =NULL ; + req->flags &= ~REQ_F_ASYNC_DATA ; + } + req->flags &= ~(REQ_F_SINGLE_POLL |REQ_F_DOUBLE_POLL ); + ret =__io_arm_poll_handler (req, poll, &ipt, poll->events, issue_flags);if (ret >0 ) {io_req_set_res (req, ipt.result_mask,0 );
Signature · 1 line
2.56.0
From 7419d530be710951f118e8537e3a5439be2ffc34 Mon Sep 17 00:00:00 2001
From: Josef Bacik <[email protected]>
Date: Thu, 15 Oct 2026 19:30:44 +0000
Subject: [PATCH 15/27] io_uring/wait: look at all CQ waiters on wake up
Commit 356f92f2412686f822383b784fa102924407457a upstream.
io_wake_function() returns -1 if the waiter it is called for doesn't have enough events yet, which makes __wake_up_common() stop walking the wait queue. That assumes that the waiters behind it can't be satisfied either. Each waiter has its own target though, computed from the min_complete it passed in:
thread A: io_uring_enter(GETEVENTS, min_complete = 10) // queued first thread B: io_uring_enter(GETEVENTS, min_complete = 1)
one CQE is posted: A isn't woken, and B isn't even considered
B keeps sleeping with the event it asked for sitting in the CQ ring, until A's demand is met or a timeout or signal ends the wait.
Return 0 to carry on with the next waiter. Only waiters whose condition is met are woken, like before.
--- io_uring/io_uring.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
io_uring/io_uring.c +2 -1
@@ -2531,7 +2531,8 @@ static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode,*/ if (io_should_wake (iowq) ||io_has_work (iowq->ctx))return autoremove_wake_function (curr, mode, wake_flags, key); -return -1 ; +/* keep going, a waiter further down may be content with less */ +return 0 ; }int io_run_task_work_sig (struct io_ring_ctx *ctx)
Signature · 1 line
2.56.0
From 15e5c0007771517c03d8a9f905b80282072fa40d Mon Sep 17 00:00:00 2001
From: Josef Bacik <[email protected]>
Date: Thu, 15 Oct 2026 19:30:43 +0000
Subject: [PATCH 14/27] io_uring: wake ring pollers for local task_work
Commit b88902d8745a701d24275d012f0830e3628e2a22 upstream.
With IORING_SETUP_DEFER_TASKRUN completions are queued as local work, and only become CQEs when the submitter enters the kernel to wait for events. For an application that finds out via poll() or epoll on the ring fd when it is time to do that, io_uring_poll() reports EPOLLIN if local work is pending.
That only helps if the work is there at the time of the call. When DEFER_TASKRUN was added, io_req_local_work_add() did io_cqring_wake(), which took care of tasks sleeping in poll() as well. Then polling got a wait queue of its own, ->poll_wq, and the wake up in io_req_local_work_add() was replaced by waking the submitter directly. Since then ->poll_wq is only woken when CQEs are committed, which for such a ring is done by the very task that is waiting to be told to:
submitter completion
poll(ring_fd)
io_uring_poll() // nothing yet
schedule()
io_req_local_work_add()
// ->cq_wait_nr: nobody waits// sleeps on
Wake ->poll_wq when the first entry is put on the work list. Like for the eventfd, that must not be done right away if the work is queued from a wake up handler, as the chain of wake ups could get back to the wait queue whose lock is held. Use an RCU callback in that case, with a reference to the ring.
--- include/linux/io_uring_types.h | 3 +++ io_uring/io_uring.c | 28 ++++++++++++++++++++++++++++ 2 files changed, 31 insertions(+)
include/linux/io_uring_types.h +3 -0
@@ -362,6 +362,9 @@ struct io_ring_ctx {bool iowq_limits_set;struct callback_head poll_wq_task_work; +/* deferred ->poll_wq wake up for local task_work */ +struct rcu_head poll_wq_rcu; +atomic_t poll_wq_rcu_pending;struct list_head defer_list;unsigned sq_thread_idle;/* protected by ->completion_lock */
io_uring/io_uring.c +28 -0
@@ -1273,6 +1273,31 @@ void tctx_task_work(struct callback_head *cb)trace_io_uring_task_work_run (tctx, count,1 ); } +static void io_poll_wq_wake_rcu (struct rcu_head *rcu) +{ + struct io_ring_ctx *ctx = container_of(rcu, struct io_ring_ctx, + poll_wq_rcu); + +atomic_set (&ctx->poll_wq_rcu_pending,0 ); +io_poll_wq_wake (ctx); +percpu_ref_put (&ctx->refs); +} + +/* +* io_uring_poll() reports pending local work as EPOLLIN, as it takes a trip to +* the kernel to turn it into CQEs. Let those know who are asleep already. +* This may be called from a wake up handler with some wait queue lock held, +* and this wake up could lead back to it. Do it from a clean context. +*/ +static void io_local_work_wake_poll (struct io_ring_ctx *ctx) +{ +if (!wq_has_sleeper (&ctx->poll_wq) || +atomic_xchg (&ctx->poll_wq_rcu_pending,1 )) +return ; +percpu_ref_get (&ctx->refs); +call_rcu_hurry (&ctx->poll_wq_rcu, io_poll_wq_wake_rcu); +} +static inline void io_req_local_work_add (struct io_kiocb *req,unsigned flags) {struct io_ring_ctx *ctx = req->ctx;
@@ -1310,6 +1335,9 @@ static inline void io_req_local_work_add(struct io_kiocb *req, unsigned flags)atomic_or (IORING_SQ_TASKRUN , &ctx->rings->sq_flags);if (ctx->has_evfd)io_eventfd_signal (ctx, flags &IOU_F_TWQ_IN_WAKE ); +/* whoever sets the flag wakes ->poll_wq afterwards */ +if (ctx->poll_activated) +io_local_work_wake_poll (ctx); } nr_wait =atomic_read (&ctx->cq_wait_nr);
Signature · 1 line
2.56.0
From 03d6ddf5e4c2015f62009b1017eddae08e03a049 Mon Sep 17 00:00:00 2001
From: Josef Bacik <[email protected]>
Date: Thu, 15 Oct 2026 19:30:42 +0000
Subject: [PATCH 13/27] io_uring/cancel: let CANCEL_ALL find parked requests
Commit fe6ca13c98b1223f6005f32913ebd32e4df64b77 upstream.
A cancellation with IORING_ASYNC_CANCEL_ALL or _ANY goes on until it finds nothing more. A request may stay where it was found for a while after it has been cancelled, so each one that matches is stamped with the sequence number of the cancellation, and is skipped from then on.
io_cancel_cb() does so for io-wq work as well. There is no need to:
pending work is taken off the list, and the workers are looked at once per call. And it does harm. A request that is run by io-wq may be parked while it still is the current work of the worker: poll gets armed for it, or it is put on ->futex_list or ->waitid_list.
io-wq worker io_try_cancel()
io_wq_submit_work()
io_arm_poll_handler()
io_poll_add_hash()
io_async_cancel_one()
io_cancel_cb()
stamps the request
sets IO_WQ_WORK_CANCEL = -EALREADY
io_poll_cancel()
skips the request
= -ENOENT
io_assign_current_work(NULL)
The flag comes too late for the worker to see it, and the search that would have found the request takes it for done. The cancellation returns, and the request stays.
With a multishot poll, IOSQE_ASYNC, and IORING_OP_ASYNC_CANCEL with IORING_ASYNC_CANCEL_ALL up to 63us later, that happens to 4 of 1,000 requests (4 CPUs, KVM).
Take the stamp off again if the work matches. If the request is found on one of the lists it gets a new one, and io_cancel_cb() doesn't match it a second time.
__io_async_cancel() tries the io-wq of the caller twice. So far the stamp has kept it from counting work that is running there the second time. Skip it instead, so that the number that is returned stays the same.
--- io_uring/cancel.c | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-)
io_uring/cancel.c +14 -4
@@ -68,7 +68,16 @@ static bool io_cancel_cb(struct io_wq_work *work, void *data) struct io_kiocb *req = container_of(work, struct io_kiocb, work);struct io_cancel_data *cd = data; -return io_cancel_req_match (req, cd); +if (!io_cancel_req_match (req, cd)) +return false; +/* +* Pending work is gone once it has matched. Work that is running may +* park the request where the same cancellation looks next. Don't make +* it skip the request as one that it has dealt with. +*/ +if (cd->flags & (IORING_ASYNC_CANCEL_ALL |IORING_ASYNC_CANCEL_ANY )) + req->cancel_seq_set = false; +return true; } static int io_async_cancel_one(struct io_uring_task *tctx,
@@ -177,9 +186,10 @@ static int __io_async_cancel(struct io_cancel_data *cd,io_ring_submit_lock (ctx, issue_flags); ret = -ENOENT ;list_for_each_entry (node, &ctx->tctx_list, ctx_node) { -struct io_uring_task *tctx = node->task->io_uring; - - ret =io_async_cancel_one (tctx, cd); +/* has been done above, don't count what is running there */ +if (all && node->task->io_uring == tctx) +continue ; + ret =io_async_cancel_one (node->task->io_uring, cd);if (ret != -ENOENT ) {if (!all)break ;
Signature · 1 line
2.56.0
From d9355e4d39c2bded35d05ef56b7d5a79b9498c2d Mon Sep 17 00:00:00 2001
From: Jens Axboe <[email protected]>
Date: Sun, 28 Jan 2024 20:11:55 -0700
Subject: [PATCH 12/27] io_uring/cancel: don't default to setting
req->work.cancel_seq
Commit 521223d7c229f83915619f888c99e952f24dc39f upstream.
Just leave it unset by default, avoiding dipping into the last cacheline (which is otherwise untouched) for the fast path of using poll to drive networked traffic. Add a flag that tells us if the sequence is valid or not, and then we can defer actually assigning the flag and sequence until someone runs cancelations.
--- include/linux/io_uring_types.h | 3 ++- io_uring/cancel.c | 3 +-- io_uring/cancel.h | 10 ++++++++++ io_uring/io_uring.c | 2 +- io_uring/poll.c | 6 +----- 5 files changed, 15 insertions(+), 9 deletions(-)
include/linux/io_uring_types.h +2 -1
@@ -576,7 +576,7 @@ struct io_kiocb {__poll_t apoll_events; };atomic_t refs; -atomic_t poll_refs; +bool cancel_seq_set;struct io_task_work io_task_work;unsigned nr_tw;/* for polled requests, i.e. IORING_OP_POLL_ADD and async armed poll */
@@ -586,6 +586,7 @@ struct io_kiocb {/* opcode allocated if it needs to store data for async defer */ void *async_data;/* linked requests, IFF REQ_F_HARDLINK or REQ_F_LINK are set */ +atomic_t poll_refs;struct io_kiocb *link;/* custom credentials, valid IFF REQ_F_CREDS is set */ const struct cred *creds;
io_uring/cancel.c +1 -2
@@ -56,9 +56,8 @@ bool io_cancel_req_match(struct io_kiocb *req, struct io_cancel_data *cd)return false;if (cd->flags &IORING_ASYNC_CANCEL_ALL ) { check_seq: -if (cd->seq == req->work.cancel_seq) +if (io_cancel_match_sequence (req, cd->seq))return false; - req->work.cancel_seq = cd->seq; }return true;
io_uring/cancel.h +10 -0
@@ -22,3 +22,13 @@ void init_hash_table(struct io_hash_table *table, unsigned size);int io_sync_cancel (struct io_ring_ctx *ctx,void __user *arg);bool io_cancel_req_match (struct io_kiocb *req,struct io_cancel_data *cd); + +static inline bool io_cancel_match_sequence (struct io_kiocb *req,int sequence) +{ +if (req->cancel_seq_set && sequence == req->work.cancel_seq) +return true; + + req->cancel_seq_set = true; + req->work.cancel_seq = sequence; +return false; +}
io_uring/io_uring.c +1 -1
@@ -434,7 +434,6 @@ static void io_prep_async_work(struct io_kiocb *req) req->work.list.next =NULL ; req->work.flags =0 ; - req->work.cancel_seq =atomic_read (&ctx->cancel_seq);if (req->flags &REQ_F_FORCE_ASYNC ) req->work.flags |=IO_WQ_WORK_CONCURRENT ;
@@ -2164,6 +2163,7 @@ static int io_init_req(struct io_ring_ctx *ctx, struct io_kiocb *req, req->file =NULL ; req->rsrc_node =NULL ; req->task = current; + req->cancel_seq_set = false;if (unlikely (opcode >=IORING_OP_LAST )) { req->opcode =0 ;
io_uring/poll.c +1 -5
@@ -585,10 +585,7 @@ static int __io_arm_poll_handler(struct io_kiocb *req, struct io_poll_table *ipt, __poll_t mask, unsigned issue_flags) { - struct io_ring_ctx *ctx = req->ctx; - INIT_HLIST_NODE(&req->hash_node); - req->work.cancel_seq =atomic_read (&ctx->cancel_seq);io_init_poll_iocb (poll, mask); poll->file = req->file; req->apoll_events = poll->events;
@@ -826,9 +823,8 @@ static struct io_kiocb *io_poll_find(struct io_ring_ctx *ctx, bool poll_only,if (poll_only && req->opcode !=IORING_OP_POLL_ADD )continue ;if (cd->flags &IORING_ASYNC_CANCEL_ALL ) { -if (cd->seq == req->work.cancel_seq) +if (io_cancel_match_sequence (req, cd->seq))continue ; - req->work.cancel_seq = cd->seq; } *out_bucket = hb;return req;
Signature · 1 line
2.56.0
From 52303d203afe5b4bbd7a754176df5a5ae11d638b Mon Sep 17 00:00:00 2001
From: Josef Bacik <[email protected]>
Date: Thu, 15 Oct 2026 19:30:41 +0000
Subject: [PATCH 11/27] io_uring/cancel: recognize "no timeout" on 32-bit
Commit 3774e4d4c8533da2caae95556c4d635c7c4f45b8 upstream.
IORING_REGISTER_SYNC_CANCEL waits without a limit if both fields of io_uring_sync_cancel_reg::timeout are -1. io_sync_cancel() compares them with -1UL. They are of type long long. With a long of 32 bits -1UL is 4294967295, which is what it stays when it is converted for the comparison, and the fields are never equal to it.
So { -1, -1 } is taken for a timeout. ktime_set() takes the nanoseconds as an unsigned long, so what becomes of it is -1s + 4294967295ns = 3.29s.
If the request is being run by io-wq and can't be interrupted, the call returns -ETIME after that time, rather than waiting for the request to be gone. On i386, with a buffered read from a null_blk device that takes 4s to complete a request, IOSQE_ASYNC, that is what each call does.
--- io_uring/cancel.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
io_uring/cancel.c +1 -1
@@ -308,7 +308,7 @@ int io_sync_cancel(struct io_ring_ctx *ctx, void __user *arg)if (ret != -EALREADY ) goto out; -if (sc.timeout.tv_sec !=-1UL || sc.timeout.tv_nsec !=-1UL ) { +if (sc.timeout.tv_sec !=-1 || sc.timeout.tv_nsec !=-1 ) {struct timespec64 ts = { .tv_sec = sc.timeout.tv_sec, .tv_nsec = sc.timeout.tv_nsec
Signature · 1 line
2.56.0
From c6399523941f29ab9fc8f685d8678c5267d245c0 Mon Sep 17 00:00:00 2001
From: Josef Bacik <[email protected]>
Date: Thu, 15 Oct 2026 19:30:40 +0000
Subject: [PATCH 10/27] io_uring/cancel: don't forget that the work was running
Commit 8aa0e5db4845fb5ac11abe40d7b1cac0656c3ee3 upstream.
If the request to be cancelled is being run by io-wq, io_async_cancel_one() flags the work, signals the worker and returns -EALREADY. Since commit ccbf726171b7 ("io_uring: perform poll removal even if async work removal is successful") io_try_cancel() goes on regardless, as poll may be armed for the request. If it isn't, and nothing else turns up, the result is the -ENOENT of the last search.
__io_async_cancel() makes up for that. On -ENOENT it tries the io-wq of every task that uses the ring, and that of the caller is one of them. So the work is found a second time, and the result is -EALREADY after all.
Unless the worker is done with it by then. If the work is flagged before io_wq_submit_work() looks at IO_WQ_WORK_CANCEL, all the worker does is to queue the request to be failed by task work. Then it isn't anywhere to be found until the task work has run, and
- IORING_REGISTER_SYNC_CANCEL returns -ENOENT,
- IORING_OP_ASYNC_CANCEL completes with -ENOENT,
for a request that they have just cancelled, and that completes with -ECANCELED.
With a read of an empty pipe, IOSQE_ASYNC, and a sync cancel right behind it, 1 to 3 of 1,000,000 calls return -ENOENT, and 50 to 110 with IORING_SETUP_DEFER_TASKRUN (4 CPUs, KVM). Nothing is written to the pipe, and all of these reads end with -ECANCELED.
Remember that the work was found. IORING_OP_ASYNC_CANCEL completes with -EALREADY, as it does if the worker is a bit slower. For IORING_REGISTER_SYNC_CANCEL it means to try again, which ends with 0.
The result of a linked timeout, which calls io_try_cancel() by itself, and the number that is returned for IORING_ASYNC_CANCEL_ALL and _ANY don't change.
--- io_uring/cancel.c | 13 ++++++++++++- io_uring/cancel.h | 2 ++ 2 files changed, 14 insertions(+), 1 deletion(-)
io_uring/cancel.c +12 -1
@@ -114,6 +114,8 @@ int io_try_cancel(struct io_uring_task *tctx, struct io_cancel_data *cd,*/ if (!ret)return 0 ; +if (ret == -EALREADY ) + cd->running = true; ret =io_poll_cancel (ctx, cd, issue_flags);if (ret != -ENOENT )
@@ -162,6 +164,7 @@ static int __io_async_cancel(struct io_cancel_data *cd,struct io_tctx_node *node;int ret, nr =0 ; + cd->running = false; do { ret = io_try_cancel(tctx, cd, issue_flags); if (ret == -ENOENT)
@@ -185,7 +188,15 @@ static int __io_async_cancel(struct io_cancel_data *cd, } }io_ring_submit_unlock (ctx, issue_flags); -return all ? nr : ret; +if (all) +return nr; +/* +* The worker may be done with the work that was found running, and +* flagged, above. That isn't the same as not having found it. +*/ +if (ret == -ENOENT && cd->running) + ret = -EALREADY ; +return ret; }int io_async_cancel (struct io_kiocb *req,unsigned int issue_flags)
io_uring/cancel.h +2 -0
@@ -9,6 +9,8 @@ struct io_cancel_data {u8 opcode;u32 flags;int seq; +/* io_try_cancel() has found it being run by io-wq, and flagged it */ +bool running; };int io_async_cancel_prep (struct io_kiocb *req,const struct io_uring_sqe *sqe);
Signature · 1 line
2.56.0
From 97036bf54d303469983f619b7bbe663e0001c7c3 Mon Sep 17 00:00:00 2001
From: Josef Bacik <[email protected]>
Date: Thu, 15 Oct 2026 19:30:38 +0000
Subject: [PATCH 09/27] io_uring/poll: don't lose a cancel as io-wq arms poll
Commit 6d2d4c2530e95ff6ddeb98195d49d99dbca14369 upstream.
A request on a pollable file that was forced to io-wq is issued without blocking there. If that ends in -EAGAIN, io_wq_submit_work() arms poll for it and returns. It looks at IO_WQ_WORK_CANCEL once, before it issues the request.
Somebody who wants to cancel the request in between finds it running:
io-wq worker io_try_cancel()
io_wq_submit_work()
IO_WQ_WORK_CANCEL isn't set
io_issue_sqe() = -EAGAIN
io_async_cancel_one()
sets IO_WQ_WORK_CANCEL signals the worker = -EALREADY
io_poll_cancel() = -ENOENT
...
= -ENOENT
io_arm_poll_handler() = IO_APOLL_OK
return
Nobody looks at the flag again, and the signal doesn't matter to a worker that doesn't sleep. The request is armed as if nothing had happened:
- a linked timeout completes with -ENOENT, and the request it was meant to limit waits for as long as it takes.
- IORING_OP_ASYNC_CANCEL returns -EALREADY, and the request stays.
- IORING_REGISTER_SYNC_CANCEL waits for the request to complete if it gets -EALREADY. Since commit 5623eb1ed035 ("io_uring/tctx: add separate lock for list of tctx's in ctx") it doesn't sleep but spins, so its next attempt finds the armed poll.
The same goes for a multishot poll, recv or accept that is armed from io-wq.
With a read of an empty pipe, IOSQE_ASYNC, and a linked timeout of 5ms or a cancel in the same submission, in a guest that is slow enough (TCG, KASAN), the request is lost in 21 of 24 rounds of 40 or 100 attempts.
While it arms poll from io-wq, __io_arm_poll_handler() owns the request. Look at the flag once the request is in the hash table. The table is searched and the request is added with ->uring_lock held, and the flag is set before the search. So either the search finds the request, or it came first and the flag is seen. With the request marked as cancelled, the task work that is queued, right away or because ownership can't be released, fails it with -ECANCELED.
What the canceller is told doesn't change. In this window a linked timeout still completes with -ENOENT rather than -ETIME, a cancel request with -EALREADY, and one with IORING_ASYNC_CANCEL_ALL or _ANY with a count that doesn't include the request. But the request is cancelled, so what is new is a request that completes with -ECANCELED after such a result.
This doesn't help in two cases:
- IORING_ASYNC_CANCEL_ALL and _ANY, if the work is flagged after the test added here, while io-wq still has it as the current work of the worker. io_cancel_cb() stamps it with the sequence of the cancel, so the search of the hash table on behalf of the same cancel takes it for done and skips it.
- requests that are parked elsewhere by io-wq, like IORING_OP_FUTEX_WAIT and IORING_OP_WAITID. They have the same problem.
--- io_uring/poll.c | 11 +++++++++++ 1 file changed, 11 insertions(+)
io_uring/poll.c +11 -0
@@ -644,6 +644,17 @@ static int __io_arm_poll_handler(struct io_kiocb *req,io_poll_add_hash (req); +/* +* Whoever tried to cancel the request while io-wq was running it has +* flagged the work item. If it searched the hash table before the +* request got there it found nothing, and it won't come back. Both +* the search and the insertion are done with the lock of the hash +* table held, and the flag is set before the search. If the search +* came first, the flag is visible by now. +*/ +if (ipt->owning && (req->work.flags &IO_WQ_WORK_CANCEL )) +io_poll_mark_cancelled (req); +if (mask && (poll->events &EPOLLET ) &&io_poll_can_finish_inline (req, ipt)) {__io_poll_execute (req, mask,0 );
Signature · 1 line
2.56.0
From cd80dc16e2d726727389396b8958b95fdf7868e7 Mon Sep 17 00:00:00 2001
From: Josef Bacik <[email protected]>
Date: Thu, 15 Oct 2026 19:30:37 +0000
Subject: [PATCH 08/27] io_uring/io-wq: let cancel find picked up hashed work
Commit e9efeb4fc97b0879512a3c3b995ed448f2487e12 upstream.
io_get_next_work() takes all the work items that share a hash off the work list in one go, and the worker runs them one after the other. Only the one it is working on is visible as ->cur_work. The others are neither on the list nor current, so io_wq_cancel_cb() can't find them.
IORING_OP_ASYNC_CANCEL and IORING_REGISTER_SYNC_CANCEL return -ENOENT for a request that has not even been started, e.g. a buffered write queued behind another write to the same file that is blocked, and the request is carried out. The cancellation done for a task or ring that goes away gets there in the end, as it keeps trying, but it has to wait for each item to become current.
Keep a pointer to the remainder of the chain next to ->cur_work, and have io_wq_worker_cancel() look at it. The items on it belong to the worker, which needs ->lock to move on to the next one. Matching work is flagged with IO_WQ_WORK_CANCEL and fails with -ECANCELED when its turn comes. The worker isn't signalled for it, that would interrupt the unrelated work it is busy with. As the request isn't gone yet, it is reported like running work, with -EALREADY.
--- io_uring/io-wq.c | 38 +++++++++++++++++++++++++++++--------- 1 file changed, 29 insertions(+), 9 deletions(-)
io_uring/io-wq.c +29 -9
@@ -54,6 +54,7 @@ struct io_worker {struct io_wq *wq;struct io_wq_work *cur_work; +/* rest of the hashed chain ->cur_work was taken off the list with */ struct io_wq_work *next_work;raw_spinlock_t lock;
@@ -536,6 +537,17 @@ static struct io_wq_work *io_get_next_work(struct io_wq_acct *acct,return NULL ; } +static void __io_assign_current_work (struct io_worker *worker, +struct io_wq_work *work) +{ +raw_spin_lock (&worker->lock); + worker->cur_work = work; +/* stays valid while in between two items of a chain */ +if (work) + worker->next_work =wq_next_work (work); +raw_spin_unlock (&worker->lock); +} +static void io_assign_current_work (struct io_worker *worker,struct io_wq_work *work) {
@@ -544,10 +556,7 @@ static void io_assign_current_work(struct io_worker *worker,cond_resched (); } -raw_spin_lock (&worker->lock); - worker->cur_work = work; - worker->next_work =NULL ; -raw_spin_unlock (&worker->lock); +__io_assign_current_work (worker, work); }/*
@@ -578,9 +587,7 @@ static void io_worker_handle_work(struct io_wq_acct *acct,* work list, but isn't yet discoverable as the * current work item for this worker. */ -raw_spin_lock (&worker->lock); - worker->next_work = work; -raw_spin_unlock (&worker->lock); +__io_assign_current_work (worker, work); }raw_spin_unlock (&acct->lock);
@@ -1083,15 +1090,28 @@ static bool __io_wq_worker_cancel(struct io_worker *worker,static bool io_wq_worker_cancel (struct io_worker *worker,void *data) {struct io_cb_cancel_data *match = data; +struct io_wq_work *work;/* * Hold the lock to avoid ->cur_work going out of scope, caller * may dereference the passed in work. */ raw_spin_lock (&worker->lock); -if (__io_wq_worker_cancel (worker, match, worker->cur_work) || -__io_wq_worker_cancel (worker, match, worker->next_work)) +if (__io_wq_worker_cancel (worker, match, worker->cur_work)) match->nr_running++; +/* +* Hashed work is taken off the work list a chain at a time, what the +* worker hasn't started yet can only be found here. It doesn't move on +* without the lock. Just mark the work, it fails once it is its turn. +*/ +for (work = worker->next_work; work; work =wq_next_work (work)) { +if (match->nr_running && !match->cancel_all) +break ; +if (match->fn (work, match->data)) { + work->flags |=IO_WQ_WORK_CANCEL ; + match->nr_running++; + } + }raw_spin_unlock (&worker->lock);return match->nr_running && !match->cancel_all;
Signature · 1 line
2.56.0
From 6f79ecd4e9f58bb7d3fd25a41497037ecfdc9091 Mon Sep 17 00:00:00 2001
From: Josef Bacik <[email protected]>
Date: Thu, 15 Oct 2026 19:30:36 +0000
Subject: [PATCH 07/27] io_uring/io-wq: don't exit with a creation pending
Commit e231d2e8f80d3649ecd3851b7f2abdfbe606c04b upstream.
A worker that is the last one to go to sleep while work is pending asks for another worker. io_wq_dec_running() bumps ->nr_running on behalf of the new worker and queues create_worker_cb() as task_work for the owner of the io-wq. Until that has run, everybody else takes it that a worker is running which is going to look after the work list: io_wq_enqueue() doesn't create one, and other workers that go to sleep don't find themselves to be the last.
If the worker that made the request exits, io_worker_exit() cancels the task_work, and io_worker_cancel_cb() drops ->nr_running without a look at the work list:
worker A worker B io_wq_enqueue()
blocks, work is pending
nr_running++
queues create_worker_cb()
wakes, handles the work
idle timeout, decides to exit
picks up work 1
queues work 2 B is on the free list
still: wakes B
work 1 blocks
nr_running 2 -> 1io_worker_exit()
io_worker_cancel_cb()
nr_running 1 -> 0
Work 2 has to wait for work 1, although there is room for more workers. It takes an owner that doesn't get around to run its task_work for the time of the idle timeout, less if the worker exits because its affinity was changed or the io-wq is marked exit-on-idle.
Let the worker stay around until its request has been dealt with. Only the worker itself sets ->create_state, so it can't be set behind its back once it has decided to leave.
--- io_uring/io-wq.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-)
io_uring/io-wq.c +5 -1
@@ -708,8 +708,12 @@ static int io_wq_worker(void *data)/* * Last sleep timed out. Exit if we're not the last worker, * or if someone modified our affinity. +* Not as long as a request of ours for another worker is +* pending, though. It is accounted as a running worker, which +* others rely on, and io_worker_exit() would cancel it. */ -if (last_timeout && (exit_mask || acct->nr_workers >1 )) { +if (last_timeout && (exit_mask || acct->nr_workers >1 ) && + !test_bit (0 , &worker->create_state)) { acct->nr_workers--;if (test_and_clear_bit (IO_WORKER_F_FREE , &worker->flags))hlist_nulls_del_init_rcu (&worker->nulls_node);
Signature · 1 line
2.56.0
From ecd7590110211606baeaa749770e136ede21dc1e Mon Sep 17 00:00:00 2001
From: Josef Bacik <[email protected]>
Date: Thu, 15 Oct 2026 19:30:35 +0000
Subject: [PATCH 06/27] io_uring/io-wq: don't strand work when a worker exits
Commit ad41f325be1271b22d99353819c90f0d5e45acc8 upstream.
A worker exits if its idle timeout fired and it isn't the last one, if its affinity was changed, or if the wq is marked exit-on-idle. It decides so after it found the work list empty, and until io_worker_exit() is through it stays on the free list and in ->nr_running:
worker io_wq_enqueue()io_acct_run_queue() == false
queues work
io_acct_activate_free_worker()
wake_up_process(worker)
// found one, nothing to createacct->nr_workers--
breakio_worker_exit()
Nobody is going to look at the work. If other workers exist, it has to wait until one of them finishes what it is blocked on or hits its idle timeout. If not, which is possible for the affinity and the exit-on-idle case, it sits there until something else is queued. For exit-on-idle it takes a task that closes its last ring and sets up a new one while the workers are on their way out.
Make the worker take itself off the free list when it gives up its ->nr_workers slot, then leave ->nr_running, and only then take the final look at the work list. That look and queueing work are both done under acct->lock, so either the worker sees the work and stays, or the enqueue side sees that the worker is gone and wakes or creates another one. If it wants to stay and finds that its slot has been taken, a worker has been created in its place, which is going to look at the work list, and it can leave after all.
--- io_uring/io-wq.c | 37 ++++++++++++++++++++++++++++++++++++- 1 file changed, 36 insertions(+), 1 deletion(-)
io_uring/io-wq.c +36 -1
@@ -648,6 +648,36 @@ static void io_worker_handle_work(struct io_wq_acct *acct, }while (1 ); } +/* +* The worker has decided to exit, and has given up its ->nr_workers slot and +* its place on the free list. io_wq_enqueue() may have picked it from that +* list, or seen it running, after it last looked at the work list. Leave +* ->nr_running as well and check again. Returns false if the worker has to +* stay. +*/ +static bool io_worker_retire (struct io_wq *wq,struct io_wq_acct *acct, +struct io_worker *worker) +{ +/* turns io_wq_dec_running() and the scheduler hooks into no-ops */ +clear_bit (IO_WORKER_F_UP , &worker->flags); +atomic_dec (&acct->nr_running); +if (!io_acct_run_queue (acct)) +return true; +raw_spin_unlock (&acct->lock); + +raw_spin_lock (&wq->lock); +if (acct->nr_workers >= acct->max_workers) { +/* a worker was created in our place, the work is its business */ +raw_spin_unlock (&wq->lock); +return true; + } + acct->nr_workers++; +raw_spin_unlock (&wq->lock); +atomic_inc (&acct->nr_running); +set_bit (IO_WORKER_F_UP , &worker->flags); +return false; +} +static int io_wq_worker (void *data) {struct io_worker *worker = data;
@@ -681,9 +711,14 @@ static int io_wq_worker(void *data)*/ if (last_timeout && (exit_mask || acct->nr_workers >1 )) { acct->nr_workers--; +if (test_and_clear_bit (IO_WORKER_F_FREE , &worker->flags)) +hlist_nulls_del_init_rcu (&worker->nulls_node);raw_spin_unlock (&wq->lock);__set_current_state (TASK_RUNNING ); -break ; +if (io_worker_retire (wq, acct, worker)) +break ; + last_timeout = false; +continue ; } last_timeout = false;__io_worker_idle (wq, worker);
Signature · 1 line
2.56.0
From cb06ee4c2172a8e6b8c590a5e0d3252962a9735c Mon Sep 17 00:00:00 2001
From: Josef Bacik <[email protected]>
Date: Thu, 15 Oct 2026 19:30:30 +0000
Subject: [PATCH 05/27] io_uring: don't flood a cancelling task with task_work
Commit 22e643e61b9b6299b42ff7d88bab48bccada8a5d upstream.
io_ring_exit_work() asks each task which has used the ring to remove its tctx node, by task_work, and repeats that until the list is empty. io_tctx_exit_cb() doesn't touch the node while the task is in io_uring_cancel_generic(), which removes all the nodes at its end. So the work item queues the callback again as soon as it has completed, for nothing.
That is more than a waste of time. task_work_run() only returns when it finds the list empty. If the rings are released from inside the cancellation, because requests of other rings held the last references to their files, up to 64 work items keep sending callbacks to the one task which is running them. It never finds the list empty, never gets back to the cancellation, the nodes stay, and so it goes on: a task with 60 pairs of rings which poll each other ran 280000 callbacks in 5 seconds, 20 seconds after exit_group(). It ends when all the senders happen to be late at the same time. Until then the task keeps a CPU and the work items busy.
An exiting task can't be killed meanwhile. execve() cancels the requests in the same way, so a live task can get stuck there as well, for minutes. If it takes more than five, the work items run into the WARN_ON_ONCE(time_after(jiffies, timeout)) of io_ring_exit_work(). No privilege is needed for any of this.
If the callback has met a task in cancellation, wait a little before it is sent again. Nobody has to wake the work item up for that: either the node is gone by then, or it is the turn of the next round.
--- io_uring/io_uring.c | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-)
io_uring/io_uring.c +11 -1
@@ -3169,6 +3169,7 @@ struct io_tctx_exit {struct callback_head task_work;struct completion completion;struct io_ring_ctx *ctx; +bool in_cancel; };static __cold void io_tctx_exit_cb (struct callback_head *cb)
@@ -3183,7 +3184,8 @@ static __cold void io_tctx_exit_cb(struct callback_head *cb)* tctx can be NULL if the queueing of this task_work raced with * work cancelation off the exec path. */ -if (tctx && !atomic_read (&tctx->in_cancel)) + work->in_cancel = tctx &&atomic_read (&tctx->in_cancel); +if (tctx && !work->in_cancel)io_uring_del_tctx_node ((unsigned long )work->ctx);complete (&work->completion); }
@@ -3277,6 +3279,14 @@ static __cold void io_ring_exit_work(struct work_struct *work)* wait is marked as interruptible. */ wait_for_completion_interruptible (&exit.completion); +/* +* The task removes the node when it is done with its +* cancellation. Give it the time for that: it doesn't get back +* to it as long as it finds new task_work each time it has run +* the old one, and we may be one of many who send it. +*/ +if (exit.in_cancel) +schedule_timeout_idle (HZ /20 );mutex_lock (&ctx->uring_lock); }mutex_unlock (&ctx->uring_lock);
Signature · 1 line
2.56.0
From 01859e1310e9c1e5871cdb0694479868137e4e77 Mon Sep 17 00:00:00 2001
From: Josef Bacik <[email protected]>
Date: Thu, 15 Oct 2026 19:30:29 +0000
Subject: [PATCH 04/27] io_uring/io-wq: don't hide a link from cancellations
Commit 436d48a2ed13112d2d27085325838e5bce6b4507 upstream.
A task that exits cancels its tracked requests and waits until they are gone. io_uring_cancel_generic() looks everywhere a request can be, and goes to sleep if it found nothing to cancel. It is woken up each time one of the requests of the task is completed by task work or freed.
When io-wq is done with a request that has a link, there is a time in which the next request of the link can't be found:
io_worker_handle_work()
io_assign_current_work(worker, NULL)
io_wq_free_work(work)
io_req_find_next() // takes the link off the request
io_free_req() // task work for the task
io_assign_current_work(worker, linked)
io_run_task_work()
cond_resched()
worker->cur_work = linked
It is not pending, it is not the current work of a worker, and it is not yet on the list that it is about to be parked on. If the pass that the exiting task makes because of io_free_req() falls into that time, it finds nothing and the task goes to sleep. The worker then runs the link, which IO_WQ_BIT_EXIT doesn't prevent for bounded work. If that parks the request nothing is completed or freed, and nobody wakes up the task. If it is the process that exits it can't be killed any more either.
NOP, IOSQE_ASYNC | IOSQE_IO_LINK
FUTEX_WAIT, FUTEX2_PRIVATE
exit of the thread, or of the process, up to 63us later
With four of these per round 5 to 8 of 10,000 rounds hang (4 CPUs, KVM):
io_uring_cancel_generic
do_exit
Commit f8b632e89a10 ("io_uring: tighten task exit cancellations") is about the same gap, and makes the task look again for whatever happens before io_free_req().
Free the request once the link is the current work of the worker, or pending. Whoever is woken up by that can find the link, until it is parked where cancellations look as well. To do so split io_wq_free_work() in two.
--- io_uring/io-wq.c | 22 ++++++++++++++++++---- io_uring/io-wq.h | 4 +++- io_uring/io_uring.c | 28 ++++++++++++++++++++-------- io_uring/io_uring.h | 3 ++- io_uring/tctx.c | 1 + 5 files changed, 44 insertions(+), 14 deletions(-)
io_uring/io-wq.c +18 -4
@@ -99,6 +99,7 @@ enum {struct io_wq {unsigned long state; +put_work_fn *put_work;free_work_fn *free_work;io_wq_work_fn *do_work;
@@ -595,8 +596,9 @@ static void io_worker_handle_work(struct io_wq_acct *acct,/* handle a whole dependent link */ do { bool do_kill = test_bit(IO_WQ_BIT_EXIT, &wq->state); - struct io_wq_work *next_hashed, *linked; + struct io_wq_work *next_hashed, *linked, *done = work; unsigned int hash = io_get_work_hash(work); + bool last; next_hashed = wq_next_work(work);
@@ -612,7 +614,7 @@ static void io_worker_handle_work(struct io_wq_acct *acct, wq->do_work (work);io_assign_current_work (worker,NULL ); - linked = wq->free_work (work); + last = wq->put_work (work, &linked); work = next_hashed;if (!work && linked && !io_wq_is_hashed (linked)) { work = linked;
@@ -621,6 +623,13 @@ static void io_worker_handle_work(struct io_wq_acct *acct,io_assign_current_work (worker, work);if (linked)io_wq_enqueue (wq, linked); +/* +* Not before the link is current or pending work again. +* This may be the last time that a task is woken up +* which exits and looks for requests to cancel. +*/ +if (last) + wq->free_work (done);if (hash !=-1U && !next_hashed) {/* serialize hash clear with wake_up() */
@@ -921,9 +930,13 @@ static bool io_wq_worker_wake(struct io_worker *worker, void *data)static void io_run_cancel (struct io_wq_work *work,struct io_wq *wq) {do { +struct io_wq_work *linked; + work->flags |=IO_WQ_WORK_CANCEL ; wq->do_work (work); - work = wq->free_work (work); +if (wq->put_work (work, &linked)) + wq->free_work (work); + work = linked; }while (work); }
@@ -1176,7 +1189,7 @@ struct io_wq *io_wq_create(unsigned bounded, struct io_wq_data *data)int ret, i;struct io_wq *wq; -if (WARN_ON_ONCE (!data->free_work || !data->do_work)) +if (WARN_ON_ONCE (!data->put_work || !data->free_work || !data->do_work))return ERR_PTR (-EINVAL );if (WARN_ON_ONCE (!bounded))return ERR_PTR (-EINVAL );
@@ -1187,6 +1200,7 @@ struct io_wq *io_wq_create(unsigned bounded, struct io_wq_data *data)refcount_inc (&data->hash->refs); wq->hash = data->hash; + wq->put_work = data->put_work; wq->free_work = data->free_work; wq->do_work = data->do_work;
io_uring/io-wq.h +3 -1
@@ -21,7 +21,8 @@ enum io_wq_cancel { IO_WQ_CANCEL_NOTFOUND, /* work not found */ }; -typedef struct io_wq_work *(free_work_fn )(struct io_wq_work *); +typedef bool (put_work_fn )(struct io_wq_work *,struct io_wq_work **); +typedef void (free_work_fn )(struct io_wq_work *);typedef void (io_wq_work_fn )(struct io_wq_work *);struct io_wq_hash {
@@ -40,6 +41,7 @@ struct io_wq_data {struct io_wq_hash *hash;struct task_struct *task;io_wq_work_fn *do_work; +put_work_fn *put_work;free_work_fn *free_work; };
io_uring/io_uring.c +20 -8
@@ -1904,17 +1904,29 @@ int io_poll_issue(struct io_kiocb *req, struct io_tw_state *ts) IO_URING_F_COMPLETE_DEFER); } -struct io_wq_work *io_wq_free_work (struct io_wq_work *work) +/* +* io-wq is done with @work. Returns true if it held the last reference, and +* hands out the next request of the link then. The caller has to make sure +* that cancellations can find the latter before it calls io_wq_free_work(). +*/ +bool io_wq_put_work (struct io_wq_work *work,struct io_wq_work **linked) { struct io_kiocb *req = container_of(work, struct io_kiocb, work);struct io_kiocb *nxt =NULL ; -if (req_ref_put_and_test_atomic (req)) { -if (req->flags &IO_REQ_LINK_FLAGS ) - nxt =io_req_find_next (req); -io_free_req (req); - } -return nxt ? &nxt->work :NULL ; + *linked =NULL ; +if (!req_ref_put_and_test_atomic (req)) +return false; +if (req->flags &IO_REQ_LINK_FLAGS ) + nxt =io_req_find_next (req); +if (nxt) + *linked = &nxt->work; +return true; +} + +void io_wq_free_work (struct io_wq_work *work) +{ +io_free_req (container_of(work ,struct io_kiocb ,work )); }void io_wq_submit_work (struct io_wq_work *work)
@@ -1925,7 +1937,7 @@ void io_wq_submit_work(struct io_wq_work *work)bool needs_poll = false;int ret =0 , err = -ECANCELED ; -/* one will be dropped by ->io_wq_free_work() after returning to io-wq */ +/* one will be dropped by io_wq_put_work() after returning to io-wq */ if (!(req->flags &REQ_F_REFCOUNT ))__io_req_set_refcount (req,2 );else
io_uring/io_uring.h +2 -1
@@ -91,7 +91,8 @@ int io_do_iopoll(struct io_ring_ctx *ctx, bool force_nonspin);void __io_submit_flush_completions (struct io_ring_ctx *ctx);int io_req_prep_async (struct io_kiocb *req); -struct io_wq_work *io_wq_free_work (struct io_wq_work *work); +bool io_wq_put_work (struct io_wq_work *work,struct io_wq_work **linked); +void io_wq_free_work (struct io_wq_work *work);void io_wq_submit_work (struct io_wq_work *work);void io_free_req (struct io_kiocb *req);
io_uring/tctx.c +1 -0
@@ -35,6 +35,7 @@ static struct io_wq *io_init_wq_offload(struct io_ring_ctx *ctx, data.hash = hash; data.task = task; + data.put_work = io_wq_put_work; data.free_work = io_wq_free_work; data.do_work = io_wq_submit_work;
Signature · 1 line
2.56.0
From 219fae866dc549b17e665bfed2da35de4b54c77d Mon Sep 17 00:00:00 2001
From: Josef Bacik <[email protected]>
Date: Thu, 15 Oct 2026 19:30:28 +0000
Subject: [PATCH 03/27] io_uring/io-wq: don't lose the signal that ends work
Commit 63c103279a95e82280092f2f4232e91e8117a252 upstream.
When a thread exits, only its tracked requests are cancelled. Then its io-wq is torn down. io_wq_exit_workers() signals every worker, to get it out of an interruptible sleep, and waits for all of them to exit. Before they do, they go through the work that is left. Unbounded work is cancelled. Bounded work is run, as it is expected to end by itself.
Not all of it does. An open has no file yet, so it counts as bounded, and the open of a FIFO waits for the other end. That is what the signal is for. But the workers are signalled once, and a worker that hasn't started the work by then loses the signal:
io_wq_exit_workers() io_worker_handle_work()
io_wq_worker_wake()
set TIF_NOTIFY_SIGNAL
io_assign_current_work()
io_run_task_work()
clear TIF_NOTIFY_SIGNAL
io_wq_submit_work()
fifo_open()
wait_for_partner()
wait_for_completion_timeout()The thread never gets out of do_exit():
IORING_OP_OPENAT of a FIFO without a writer, IOSQE_ASYNC exit of the thread that has submitted it
hangs in more than 9 of 10 attempts (4 CPUs, KVM), until the process is killed.
Set TIF_NOTIFY_SIGNAL for bounded work that is started with IO_WQ_BIT_EXIT set. The bit is set before the workers are signalled, and it is tested after the flag has been cleared, with clear_notify_signal() providing the barrier. So either the flag stays, or the bit is seen.
Work that is started while the io-wq exits is treated like work that is being run at that time: it isn't interrupted unless it waits interruptibly.
--- io_uring/io-wq.c | 7 +++++++ 1 file changed, 7 insertions(+)
io_uring/io-wq.c +7 -0
@@ -600,8 +600,15 @@ static void io_worker_handle_work(struct io_wq_acct *acct, next_hashed =wq_next_work (work); +/* +* io_wq_exit_workers() signals us once, so that we +* don't sleep for ever. If that was before this work +* became the current one, the signal is gone. +*/ if (unlikely (do_kill) && (work->flags &IO_WQ_WORK_UNBOUND )) work->flags |=IO_WQ_WORK_CANCEL ; +else if (unlikely (do_kill)) +set_notify_signal (current); wq->do_work (work);io_assign_current_work (worker,NULL );
Signature · 1 line
2.56.0
From 61b62006844b0b3ac097be2ab8340e112e83bb9d Mon Sep 17 00:00:00 2001
From: Josef Bacik <[email protected]>
Date: Thu, 15 Oct 2026 19:30:27 +0000
Subject: [PATCH 02/27] io_uring/wait: don't spin on a stuck overflow list
Commit 34f5a664c34d3cb62f6bec7f4156754594a7d25b upstream.
As long as IO_CHECK_CQ_OVERFLOW_BIT is set, io_cqring_wait_schedule() returns right away, before it gets to look at signals or arms the timeout, for io_cqring_wait() to flush the overflow list. If that doesn't empty the list, and the CQ ring doesn't hold the number of events the caller wants, the loop starts over, with the same result. The task spins in the kernel, and can't be killed.
The assumption is that this can't happen: if CQEs can't be flushed, the ring is full, and min_events is capped to the size of the ring. But:
- With IORING_SETUP_CQE_MIXED a 32b CQE needs two contiguous entries. It is stuck on the list if a single one is free, and then the ring holds less than cq_entries events. A single threaded application that asks for as many events as the ring has entries never gets out of io_uring_enter().
- Whether the ring is full is up to the CQ head, which the application can write to. While one thread waits for events on an empty ring, another one can move the head back so that the ring looks full and have a CQE overflow. The waiter can't be stopped any more, not even by SIGKILL.
Only the application can make room in the CQ ring. Return to it if the list could not be flushed.
--- io_uring/io_uring.c | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-)
io_uring/io_uring.c +11 -2
@@ -2641,9 +2641,18 @@ static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events, check_cq =READ_ONCE (ctx->check_cq);if (unlikely (check_cq)) { -/* let the caller flush overflows, retry */ -if (check_cq &BIT (IO_CHECK_CQ_OVERFLOW_BIT )) +if (check_cq &BIT (IO_CHECK_CQ_OVERFLOW_BIT )) {io_cqring_do_overflow_flush (ctx); +/* +* No room for what has overflown. It takes the +* application to change that, so return to it. +*/ +if (test_bit (IO_CHECK_CQ_OVERFLOW_BIT , + &ctx->check_cq)) { + ret =0 ; +break ; + } + }if (check_cq &BIT (IO_CHECK_CQ_DROPPED_BIT )) { ret = -EBADR ;break ;
Signature · 1 line
2.56.0
From e389a18a7b9d270033968f0d2833a072599fbdfe Mon Sep 17 00:00:00 2001
From: Christian Brauner <[email protected]>
Date: Mon, 21 Sep 2026 15:44:53 +0200
Subject: [PATCH 01/27] io-wq: order the exit bit against worker creation task
work
Commit 0e5663856b69219ef2f02e04bac9f5c5be19718a upstream.
io_wq_exit_workers() cancels task work for queued worker creation with task_work_cancel_match(). That has a plain load of task->task_works.
io_queue_worker_create() adds the new entry and tests IO_WQ_BIT_EXIT to cancel it if the workqueue is already on its way out.
That must be ordered. task_work_add() has a full barrier via cmpxchg(). But io_wq_exit_start() sets the bit with set_bit() which doesn't have any memory ordering.
So afaict, on weakly ordered architectures the exiting task may load task_works before the store of the bit is visible. The other side tests the bit before the store is visible as well. So the entry remains queued with worker_refs and the exiting task waits on worker_done indefinitely.
If the task still has rings then io_uring_del_tctx_node() provides the barrier via test_and_set_bit() in io_wq_set_exit_on_idle(). When it has closed all rings though that barrier is gone.
Add the barrier after the set_bit().
--- io_uring/io-wq.c | 2 ++ 1 file changed, 2 insertions(+)
io_uring/io-wq.c +2 -0
@@ -1236,6 +1236,8 @@ static bool io_task_work_match(struct callback_head *cb, void *data)void io_wq_exit_start (struct io_wq *wq) {set_bit (IO_WQ_BIT_EXIT , &wq->state); +/* Pairs with task_work_add() in io_queue_worker_create(). */ +smp_mb__after_atomic (); }static void io_wq_cancel_tw_create (struct io_wq *wq)
Signature · 1 line
2.56.0