FAILED: patch "[PATCH] io_uring: include dying ring in task_work "should cancel"" failed to apply to 6.12-stable tree
Subsystems (2)
[email protected]
maintainerThe patch below does not apply to the 6.12-stable tree.
If someone wants it applied there, or to any other stable or longterm tree, then please email the backport, including the original git commit id to <[email protected]>.
To reproduce the conflict and resubmit, you may use the following commands:
git fetch https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/ linux-6.12.y
git checkout FETCH_HEAD
git cherry-pick -x 3539b1467e94336d5854ebf976d9627bfb65d6c3
# <resolve conflicts, build, test, etc.>
git commit -s
git send-email --to '<[email protected]>' --in-reply-to '2025092127-synthetic-squash-4d57@gregkh' --subject-prefix 'PATCH 6.12.y' HEAD^..
Possible dependencies:
thanks,
greg k-h
------------------ original commit in Linus's tree ------------------
From 3539b1467e94336d5854ebf976d9627bfb65d6c3 Mon Sep 17 00:00:00 2001
From: Jens Axboe <[email protected]>
Date: Thu, 18 Sep 2025 10:21:14 -0600
Subject: [PATCH] io_uring: include dying ring in task_work "should cancel"
state
When running task_work for an exiting task, rather than perform the issue retry attempt, the task_work is canceled. However, this isn't done for a ring that has been closed. This can lead to requests being successfully completed post the ring being closed, which is somewhat confusing and surprising to an application.
Rather than just check the task exit state, also include the ring ref state in deciding whether or not to terminate a given request when run from task_work.
io_uring/io_uring.c +4 -2
@@ -1406,8 +1406,10 @@ static void io_req_task_cancel(struct io_kiocb *req, io_tw_token_t tw)void io_req_task_submit (struct io_kiocb *req,io_tw_token_t tw) { -io_tw_lock (req->ctx, tw); -if (unlikely (io_should_terminate_tw ())) +struct io_ring_ctx *ctx = req->ctx; + +io_tw_lock (ctx, tw); +if (unlikely (io_should_terminate_tw (ctx)))io_req_defer_failed (req, -EFAULT );else if (req->flags &REQ_F_FORCE_ASYNC )io_queue_iowq (req);
io_uring/io_uring.h +2 -2
@@ -476,9 +476,9 @@ static inline bool io_allowed_run_tw(struct io_ring_ctx *ctx)* 2) PF_KTHREAD is set, in which case the invoker of the task_work is * our fallback task_work. */ -static inline bool io_should_terminate_tw (void ) +static inline bool io_should_terminate_tw (struct io_ring_ctx *ctx) { -return current->flags & (PF_KTHREAD |PF_EXITING ); +return (current->flags & (PF_KTHREAD |PF_EXITING )) ||percpu_ref_is_dying (&ctx->refs); }static inline void io_req_queue_tw_complete (struct io_kiocb *req,s32 res)
io_uring/poll.c +1 -1
@@ -224,7 +224,7 @@ static int io_poll_check_events(struct io_kiocb *req, io_tw_token_t tw) {int v; -if (unlikely (io_should_terminate_tw ())) +if (unlikely (io_should_terminate_tw (req->ctx)))return -ECANCELED ; do {
io_uring/timeout.c +1 -1
@@ -324,7 +324,7 @@ static void io_req_task_link_timeout(struct io_kiocb *req, io_tw_token_t tw)int ret;if (prev) { -if (!io_should_terminate_tw ()) { +if (!io_should_terminate_tw (req->ctx)) {struct io_cancel_data cd = { .ctx = req->ctx, .data = prev->cqe.user_data,
io_uring/uring_cmd.c +1 -1
@@ -118,7 +118,7 @@ static void io_uring_cmd_work(struct io_kiocb *req, io_tw_token_t tw) struct io_uring_cmd *ioucmd = io_kiocb_to_cmd(req, struct io_uring_cmd);unsigned int flags =IO_URING_F_COMPLETE_DEFER ; -if (io_should_terminate_tw ()) +if (io_should_terminate_tw (req->ctx)) flags |=IO_URING_F_TASK_DEAD ;/* task_work executor checks the deffered list completion */
Quoted 8 lines · On 9/21/25 6:32 AM, [email protected] wrote:
The patch below does not apply to the 6.12-stable tree.
If someone wants it applied there, or to any other stable or longterm tree, then please email the backport, including the original git commit id to <[email protected]>.To reproduce the conflict and resubmit, you may use the following commands:
Quoted 8 lines
git fetch https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/ linux-6.12.y
git checkoutFETCH_HEAD
git cherry-pick -x3539b1467e94336d5854ebf976d9627bfb65d6c3
# <resolve conflicts, build, test, etc.>
git commit -s
git send-email --to '<[email protected]>' --in-reply-to '2025092127-synthetic-squash-4d57@gregkh' --subject-prefix 'PATCH 6.12.y' HEAD^..Possible dependencies:
Here's this and the others marked for 6.12-stable, already prepared them last week as I knew I'd be traveling.
Signature · 1 line
Jens Axboe
From 17e7c0b7ee81de9706aa45d8cee1b1c2427eaf6b Mon Sep 17 00:00:00 2001
From: Jens Axboe <[email protected]>
Date: Thu, 18 Sep 2025 15:45:41 -0600
Subject: [PATCH 5/5] io_uring/kbuf: drop WARN_ON_ONCE() from incremental
length check
Partially based on commit 98b6fa62c84f2e129161e976a5b9b3cb4ccd117b upstream.
This can be triggered by userspace, so just drop it. The condition is appropriately handled.
--- io_uring/kbuf.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
io_uring/kbuf.h +1 -1
@@ -143,7 +143,7 @@ static inline bool io_kbuf_commit(struct io_kiocb *req,struct io_uring_buf *buf; buf =io_ring_head_to_buf (bl->buf_ring, bl->head, bl->mask); -if (WARN_ON_ONCE (len > buf->len)) +if (len > buf->len) len = buf->len; buf->len -= len;if (buf->len) {
Signature · 1 line
2.51.0
From 8a41ad655edfb6e052070133a43300fcaa9bfaab Mon Sep 17 00:00:00 2001
From: Jens Axboe <[email protected]>
Date: Thu, 18 Sep 2025 14:16:53 -0600
Subject: [PATCH 4/5] io_uring/msg_ring: kill alloc_cache for io_kiocb
allocations
Commit df8922afc37aa2111ca79a216653a629146763ad upstream.
A recent commit:
fc582cd26e88 ("io_uring/msg_ring: ensure io_kiocb freeing is deferred for RCU")
fixed an issue with not deferring freeing of io_kiocb structs that msg_ring allocates to after the current RCU grace period. But this only covers requests that don't end up in the allocation cache. If a request goes into the alloc cache, it can get reused before it is sane to do so. A recent syzbot report would seem to indicate that there's something there, however it may very well just be because of the KASAN poisoning that the alloc_cache handles manually.
Rather than attempt to make the alloc_cache sane for that use case, just drop the usage of the alloc_cache for msg_ring request payload data.
--- include/linux/io_uring_types.h | 3 --- io_uring/io_uring.c | 5 ----- io_uring/msg_ring.c | 24 ++---------------------- 3 files changed, 2 insertions(+), 30 deletions(-)
include/linux/io_uring_types.h +0 -3
@@ -400,9 +400,6 @@ struct io_ring_ctx {struct callback_head poll_wq_task_work;struct list_head defer_list; -struct io_alloc_cache msg_cache; -spinlock_t msg_lock; -#ifdef CONFIG_NET_RX_BUSY_POLL struct list_head napi_list;/* track busy poll napi_id */ spinlock_t napi_lock;/* napi_list lock */
io_uring/io_uring.c +0 -5
@@ -316,9 +316,6 @@ static __cold struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p) sizeof(struct io_async_rw)); ret |=io_alloc_cache_init (&ctx->uring_cache,IO_ALLOC_CACHE_MAX ,sizeof (struct uring_cache )); -spin_lock_init (&ctx->msg_lock); - ret |=io_alloc_cache_init (&ctx->msg_cache,IO_ALLOC_CACHE_MAX , -sizeof (struct io_kiocb )); ret |=io_futex_cache_init (ctx);if (ret) goto free_ref;
@@ -358,7 +355,6 @@ static __cold struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p)io_alloc_cache_free (&ctx->netmsg_cache, io_netmsg_cache_free);io_alloc_cache_free (&ctx->rw_cache, io_rw_cache_free);io_alloc_cache_free (&ctx->uring_cache, kfree); -io_alloc_cache_free (&ctx->msg_cache, io_msg_cache_free);io_futex_cache_free (ctx);kfree (ctx->cancel_table.hbs);kfree (ctx->cancel_table_locked.hbs);
@@ -2743,7 +2739,6 @@ static __cold void io_ring_ctx_free(struct io_ring_ctx *ctx)io_alloc_cache_free (&ctx->netmsg_cache, io_netmsg_cache_free);io_alloc_cache_free (&ctx->rw_cache, io_rw_cache_free);io_alloc_cache_free (&ctx->uring_cache, kfree); -io_alloc_cache_free (&ctx->msg_cache, io_msg_cache_free);io_futex_cache_free (ctx);io_destroy_buffers (ctx);mutex_unlock (&ctx->uring_lock);
io_uring/msg_ring.c +2 -22
@@ -11,7 +11,6 @@#include "io_uring.h" #include "rsrc.h" #include "filetable.h" -#include "alloc_cache.h" #include "msg_ring.h" /* All valid masks for MSG_RING */
@@ -76,13 +75,7 @@ static void io_msg_tw_complete(struct io_kiocb *req, struct io_tw_state *ts)struct io_ring_ctx *ctx = req->ctx;io_add_aux_cqe (ctx, req->cqe.user_data, req->cqe.res, req->cqe.flags); -if (spin_trylock (&ctx->msg_lock)) { -if (io_alloc_cache_put (&ctx->msg_cache, req)) - req =NULL ; -spin_unlock (&ctx->msg_lock); - } -if (req) -kfree_rcu (req, rcu_head); +kfree_rcu (req, rcu_head);percpu_ref_put (&ctx->refs); }
@@ -104,19 +97,6 @@ static int io_msg_remote_post(struct io_ring_ctx *ctx, struct io_kiocb *req,return 0 ; } -static struct io_kiocb *io_msg_get_kiocb (struct io_ring_ctx *ctx) -{ -struct io_kiocb *req =NULL ; - -if (spin_trylock (&ctx->msg_lock)) { - req =io_alloc_cache_get (&ctx->msg_cache); -spin_unlock (&ctx->msg_lock); -if (req) -return req; - } -return kmem_cache_alloc (req_cachep,GFP_KERNEL | __GFP_NOWARN | __GFP_ZERO); -} -static int io_msg_data_remote (struct io_kiocb *req) {struct io_ring_ctx *target_ctx = req->file->private_data;
@@ -124,7 +104,7 @@ static int io_msg_data_remote(struct io_kiocb *req)struct io_kiocb *target;u32 flags =0 ; - target =io_msg_get_kiocb (req->ctx); + target =kmem_cache_alloc (req_cachep,GFP_KERNEL | __GFP_NOWARN | __GFP_ZERO);if (unlikely (!target))return -ENOMEM ;
Signature · 1 line
2.51.0
From 32d72fba3a7dbe4c819a792c6277371902160eec Mon Sep 17 00:00:00 2001
From: Jens Axboe <[email protected]>
Date: Thu, 18 Sep 2025 11:27:06 -0600
Subject: [PATCH 2/5] io_uring: backport io_should_terminate_tw()
Parts of commit b6f58a3f4aa8dba424356c7a69388a81f4459300 upstream.
Backport io_should_terminate_tw() helper to judge whether task_work should be run or terminated.
--- io_uring/io_uring.c | 3 +-- io_uring/io_uring.h | 13 +++++++++++++ io_uring/poll.c | 3 +-- io_uring/timeout.c | 2 +- io_uring/uring_cmd.c | 2 +- 5 files changed, 17 insertions(+), 6 deletions(-)
io_uring/io_uring.c +1 -2
@@ -1359,8 +1359,7 @@ static void io_req_task_cancel(struct io_kiocb *req, struct io_tw_state *ts)void io_req_task_submit (struct io_kiocb *req,struct io_tw_state *ts) {io_tw_lock (req->ctx, ts); -/* req->task == current here, checking PF_EXITING is safe */ -if (unlikely (req->task->flags &PF_EXITING )) +if (unlikely (io_should_terminate_tw ()))io_req_defer_failed (req, -EFAULT );else if (req->flags &REQ_F_FORCE_ASYNC )io_queue_iowq (req);
io_uring/io_uring.h +13 -0
@@ -421,6 +421,19 @@ static inline bool io_allowed_run_tw(struct io_ring_ctx *ctx) ctx->submitter_task == current); } +/* +* Terminate the request if either of these conditions are true: +* +* 1) It's being executed by the original task, but that task is marked +* with PF_EXITING as it's exiting. +* 2) PF_KTHREAD is set, in which case the invoker of the task_work is +* our fallback task_work. +*/ +static inline bool io_should_terminate_tw (void ) +{ +return current->flags & (PF_KTHREAD |PF_EXITING ); +} +static inline void io_req_queue_tw_complete (struct io_kiocb *req,s32 res) {io_req_set_res (req, res,0 );
io_uring/poll.c +1 -2
@@ -265,8 +265,7 @@ static int io_poll_check_events(struct io_kiocb *req, struct io_tw_state *ts) {int v; -/* req->task == current here, checking PF_EXITING is safe */ -if (unlikely (req->task->flags &PF_EXITING )) +if (unlikely (io_should_terminate_tw ()))return -ECANCELED ; do {
io_uring/timeout.c +1 -1
@@ -303,7 +303,7 @@ static void io_req_task_link_timeout(struct io_kiocb *req, struct io_tw_state *tint ret = -ENOENT ;if (prev) { -if (!(req->task->flags &PF_EXITING )) { +if (!io_should_terminate_tw ()) {struct io_cancel_data cd = { .ctx = req->ctx, .data = prev->cqe.user_data,
io_uring/uring_cmd.c +1 -1
@@ -118,7 +118,7 @@ static void io_uring_cmd_work(struct io_kiocb *req, struct io_tw_state *ts) struct io_uring_cmd *ioucmd = io_kiocb_to_cmd(req, struct io_uring_cmd);unsigned int flags =IO_URING_F_COMPLETE_DEFER ; -if (current->flags & (PF_EXITING |PF_KTHREAD )) +if (io_should_terminate_tw ()) flags |=IO_URING_F_TASK_DEAD ;/* task_work executor checks the deffered list completion */
Signature · 1 line
2.51.0
From d61426de1cd739117889e8f876d1f8de548c6c92 Mon Sep 17 00:00:00 2001
From: Pavel Begunkov <[email protected]>
Date: Mon, 4 Nov 2024 16:12:04 +0000
Subject: [PATCH 1/5] io_uring/cmd: let cmds to know about dying task
Commit df3b8ca604f224eb4cd51669416ad4d607682273 upstream.
When the taks that submitted a request is dying, a task work for that request might get run by a kernel thread or even worse by a half dismantled task. We can't just cancel the task work without running the callback as the cmd might need to do some clean up, so pass a flag instead. If set, it's not safe to access any task resources and the callback is expected to cancel the cmd ASAP.
--- include/linux/io_uring_types.h | 1 + io_uring/uring_cmd.c | 6 +++++- 2 files changed, 6 insertions(+), 1 deletion(-)
include/linux/io_uring_types.h +1 -0
@@ -37,6 +37,7 @@ enum io_uring_cmd_flags {/* set when uring wants to cancel a previously issued command */ IO_URING_F_CANCEL = (1 <<11 ),IO_URING_F_COMPAT = (1 <<12 ), + IO_URING_F_TASK_DEAD = (1 << 13), }; struct io_wq_work_node {
io_uring/uring_cmd.c +5 -1
@@ -116,9 +116,13 @@ EXPORT_SYMBOL_GPL(io_uring_cmd_mark_cancelable);static void io_uring_cmd_work (struct io_kiocb *req,struct io_tw_state *ts) { struct io_uring_cmd *ioucmd = io_kiocb_to_cmd(req, struct io_uring_cmd); +unsigned int flags =IO_URING_F_COMPLETE_DEFER ; + +if (current->flags & (PF_EXITING |PF_KTHREAD )) + flags |=IO_URING_F_TASK_DEAD ;/* task_work executor checks the deffered list completion */ - ioucmd->task_work_cb (ioucmd,IO_URING_F_COMPLETE_DEFER ); + ioucmd->task_work_cb (ioucmd, flags); } void __io_uring_cmd_do_in_task(struct io_uring_cmd *ioucmd,
Signature · 1 line
2.51.0
From 29c99f5dee72c8a4477f38c7804e70fe3db0ab34 Mon Sep 17 00:00:00 2001
From: Jens Axboe <[email protected]>
Date: Thu, 18 Sep 2025 10:21:14 -0600
Subject: [PATCH 3/5] io_uring: include dying ring in task_work "should cancel"
state
Commit 3539b1467e94336d5854ebf976d9627bfb65d6c3 upstream.
When running task_work for an exiting task, rather than perform the issue retry attempt, the task_work is canceled. However, this isn't done for a ring that has been closed. This can lead to requests being successfully completed post the ring being closed, which is somewhat confusing and surprising to an application.
Rather than just check the task exit state, also include the ring ref state in deciding whether or not to terminate a given request when run from task_work.
--- io_uring/io_uring.c | 6 ++++-- io_uring/io_uring.h | 4 ++-- io_uring/poll.c | 2 +- io_uring/timeout.c | 2 +- io_uring/uring_cmd.c | 2 +- 5 files changed, 9 insertions(+), 7 deletions(-)
io_uring/io_uring.c +4 -2
@@ -1358,8 +1358,10 @@ static void io_req_task_cancel(struct io_kiocb *req, struct io_tw_state *ts)void io_req_task_submit (struct io_kiocb *req,struct io_tw_state *ts) { -io_tw_lock (req->ctx, ts); -if (unlikely (io_should_terminate_tw ())) +struct io_ring_ctx *ctx = req->ctx; + +io_tw_lock (ctx, ts); +if (unlikely (io_should_terminate_tw (ctx)))io_req_defer_failed (req, -EFAULT );else if (req->flags &REQ_F_FORCE_ASYNC )io_queue_iowq (req);
io_uring/io_uring.h +2 -2
@@ -429,9 +429,9 @@ static inline bool io_allowed_run_tw(struct io_ring_ctx *ctx)* 2) PF_KTHREAD is set, in which case the invoker of the task_work is * our fallback task_work. */ -static inline bool io_should_terminate_tw (void ) +static inline bool io_should_terminate_tw (struct io_ring_ctx *ctx) { -return current->flags & (PF_KTHREAD |PF_EXITING ); +return (current->flags & (PF_KTHREAD |PF_EXITING )) ||percpu_ref_is_dying (&ctx->refs); }static inline void io_req_queue_tw_complete (struct io_kiocb *req,s32 res)
io_uring/poll.c +1 -1
@@ -265,7 +265,7 @@ static int io_poll_check_events(struct io_kiocb *req, struct io_tw_state *ts) {int v; -if (unlikely (io_should_terminate_tw ())) +if (unlikely (io_should_terminate_tw (req->ctx)))return -ECANCELED ; do {
io_uring/timeout.c +1 -1
@@ -303,7 +303,7 @@ static void io_req_task_link_timeout(struct io_kiocb *req, struct io_tw_state *tint ret = -ENOENT ;if (prev) { -if (!io_should_terminate_tw ()) { +if (!io_should_terminate_tw (req->ctx)) {struct io_cancel_data cd = { .ctx = req->ctx, .data = prev->cqe.user_data,
io_uring/uring_cmd.c +1 -1
@@ -118,7 +118,7 @@ static void io_uring_cmd_work(struct io_kiocb *req, struct io_tw_state *ts) struct io_uring_cmd *ioucmd = io_kiocb_to_cmd(req, struct io_uring_cmd);unsigned int flags =IO_URING_F_COMPLETE_DEFER ; -if (io_should_terminate_tw ()) +if (io_should_terminate_tw (req->ctx)) flags |=IO_URING_F_TASK_DEAD ;/* task_work executor checks the deffered list completion */
Signature · 1 line
2.51.0
Greg KH
maintainerQuoted 16 lines · Jens Axboe
Quoted 15 lines · On 9/21/25 6:32 AM, [email protected] wrote:
The patch below does not apply to the 6.12-stable tree.
If someone wants it applied there, or to any other stable or longterm tree, then please email the backport, including the original git commit id to <[email protected]>.To reproduce the conflict and resubmit, you may use the following commands:
git fetch https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/ linux-6.12.y
git checkoutFETCH_HEAD
git cherry-pick -x3539b1467e94336d5854ebf976d9627bfb65d6c3
# <resolve conflicts, build, test, etc.>
git commit -s
git send-email --to '<[email protected]>' --in-reply-to '2025092127-synthetic-squash-4d57@gregkh' --subject-prefix 'PATCH 6.12.y' HEAD^..
Quoted 4 lines
Quoted 1 line
Possible dependencies:
Here's this and the others marked for 6.12-stable, already prepared them last week as I knew I'd be traveling.
Now queued up, thanks.
greg k-h
Greg KH
maintainerQuoted 16 lines · Jens Axboe
Quoted 15 lines · On 9/21/25 6:32 AM, [email protected] wrote:
The patch below does not apply to the 6.12-stable tree.
If someone wants it applied there, or to any other stable or longterm tree, then please email the backport, including the original git commit id to <[email protected]>.To reproduce the conflict and resubmit, you may use the following commands:
git fetch https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/ linux-6.12.y
git checkoutFETCH_HEAD
git cherry-pick -x3539b1467e94336d5854ebf976d9627bfb65d6c3
# <resolve conflicts, build, test, etc.>
git commit -s
git send-email --to '<[email protected]>' --in-reply-to '2025092127-synthetic-squash-4d57@gregkh' --subject-prefix 'PATCH 6.12.y' HEAD^..
Quoted 4 lines
Quoted 1 line
Possible dependencies:
Here's this and the others marked for 6.12-stable, already prepared them last week as I knew I'd be traveling.
As you have backported commit b6f58a3f4aa8 ("io_uring: move struct io_kiocb from task_struct to io_uring_task") does that mean we should also add commit 69a62e03f896 ("io_uring/msg_ring: don't leave potentially dangling ->tctx pointer") which claims to fix it?
I tried to apply it (same for 6.1 and 6.6), but it didn't apply cleanly.
thanks,
greg k-h
Jens Axboe
maintainerQuoted 19 lines · Greg KH
Quoted 18 lines · Jens Axboe
Quoted 16 lines · On 9/21/25 6:32 AM, [email protected] wrote:
The patch below does not apply to the 6.12-stable tree.
If someone wants it applied there, or to any other stable or longterm tree, then please email the backport, including the original git commit id to <[email protected]>.To reproduce the conflict and resubmit, you may use the following commands:
git fetch https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/ linux-6.12.y
git checkoutFETCH_HEAD
git cherry-pick -x3539b1467e94336d5854ebf976d9627bfb65d6c3
# <resolve conflicts, build, test, etc.>
git commit -s
git send-email --to '<[email protected]>' --in-reply-to '2025092127-synthetic-squash-4d57@gregkh' --subject-prefix 'PATCH 6.12.y' HEAD^..Possible dependencies:
Quoted 7 lines
Quoted 2 lines
Here's this and the others marked for 6.12-stable, already prepared them last week as I knew I'd be traveling.
As you have backported commit
b6f58a3f4aa8("io_uring: move structio_kiocbfromtask_structtoio_uring_task") does that mean we should also add commit69a62e03f896("io_uring/msg_ring: don't leave potentially dangling ->tctx pointer") which claims to fix it?
No, the b6f58a3f4aa8 commit backport just backports the io_should_terminate_tw() helper. That original commit should've likely been split upstream, which is what made the backport only partial. It doesn't backport the parts that moves from using task_struct to io_uring_task.
Signature · 1 line
Jens Axboe