Commit 709d9af776fe for kernel

commit 709d9af776febf3a0773727939742460a55e14e2
Author: Andrea Parri <parri.andrea@gmail.com>
Date:   Fri Dec 4 19:49:49 2026 +0100

    io_uring/cancel: arm local work wakeups only for submitter

    io_uring_try_cancel_requests() arms cq_wait_nr on every DEFER_TASKRUN
    ring it visits, so that io_req_local_work_add() wakes the caller.  The
    wakeup goes to submitter_task, which need not be the caller.  A
    cancelling task can visit a ring whose local work belongs to another
    task, and ring release and io_ring_exit_work() call in as well.

    A cancelling task can also visit a ring that is still disabled, where
    submitter_task is NULL.  A later ZCRX event then passes it to
    wake_up_state():

      BUG: kernel NULL pointer dereference, address: 00000000000008e4
      RIP: 0010:_raw_spin_lock_irqsave+0x1b/0x50
      Call Trace:
       try_to_wake_up+0x51/0x790
       io_req_local_work_add+0xbb/0x1b0
       zcrx_send_notif+0xf9/0x120

    On an enabled ring, the same arm can replace the submitter's cq_wait_nr
    batch threshold and cause an early wakeup.  If the submitter is not
    waiting on the ring, a later local work add can still find the count
    armed and wake the submitter from an unrelated interruptible sleep.

    Arm cq_wait_nr only when the caller is allowed to run the ring's local
    work.  The submitter-only rearm before cancellation sleeps already uses
    the same ownership check.

    ZCRX could skip disabled rings, as msg_ring does, but that leaves the
    wrong arm on enabled rings.  Work that ZCRX queues on a disabled ring
    waits there until the ring is enabled, or until io_ring_exit_work()
    drains it.

    ZCRX events are the only local work a disabled ring can receive, so the
    NULL pointer dereference needs commit 0719e10d826a ("io_uring/zcrx:
    notify user when out of buffers").  Older kernels only see the early
    wakeups.

    Tested in a 2-vCPU x86-64 KVM guest with a reproducer that execs with
    an io_uring request pending while ZCRX copy events target a disabled
    DEFER_TASKRUN ring.  v7.4-rc5 hit the NULL pointer dereference above.
    With this patch, 1000 runs completed.

    The bug was found with the help of an LLM, which was also used to write
    the fix, the changelog and the tests.

    Fixes: 360cd42c4e95 ("io_uring: optimise io_req_local_work_add")
    Cc: stable@vger.kernel.org
    Signed-off-by: Andrea Parri <parri.andrea@gmail.com>
    Link: https://patch.msgid.link/20261204184949.185716-1-parri.andrea@gmail.com
    Signed-off-by: Jens Axboe <axboe@kernel.dk>

diff --git a/io_uring/cancel.c b/io_uring/cancel.c
index 5b57d27225ac..e6be39161fbe 100644
--- a/io_uring/cancel.c
+++ b/io_uring/cancel.c
@@ -544,7 +544,8 @@ __cold bool io_uring_try_cancel_requests(struct io_ring_ctx *ctx,
 	bool ret = false;

 	/* set it so io_req_local_work_add() would wake us up */
-	if (ctx->flags & IORING_SETUP_DEFER_TASKRUN) {
+	if ((ctx->flags & IORING_SETUP_DEFER_TASKRUN) &&
+	    io_allowed_defer_tw_run(ctx)) {
 		atomic_set(&ctx->cq_wait_nr, 1);
 		smp_mb();
 	}