| Seq | Timestamp | Type | Name | Duration |
|---|---|---|---|---|
| 0/0 | 2026/10/16 00:51 | flow | patch-triage |
3mArgs:null Results: {
"EnableConfigs": [
"IO_URING",
"FUTEX",
"NET",
"NET_RX_BUSY_POLL",
"PAGE_POOL",
"INET",
"IO_URING_ZCRX"
],
"FocusSymbols": [
"io_async_cancel",
"io_sync_cancel",
"io_futex_wait",
"io_futexv_wait",
"io_waitid",
"io_timeout",
"io_poll_add",
"io_pipe",
"io_queue_iowq",
"io_wq_put_work"
],
"KMSANReasoning": "The patch series addresses concurrency, cancellation, task work synchronization, and bounds handling across several io_uring submodules:\n1. Struct and variable initialization: New struct fields (such as `running` in `struct io_cancel_data`, `next_work` in `struct io_worker`, `in_cancel` in `struct io_tctx_exit`, and `poll_wq_rcu` / `poll_wq_rcu_pending` in `struct io_ring_ctx`) are either allocated with `kzalloc_obj`, cleared via designated struct initializers (`= { ... }`), or explicitly initialized prior to being read.\n2. User-space copies and info-leaks: In `io_pipe_fixed()`, `copy_to_user()` copies `fds` (an `int[2]` with no padding, both elements fully initialized), and the patch only modifies the locking scope around the copy. In `io_zcrx_queue_cqe()`, casting to `u64` prevents integer truncation/overflow while `__pad` remains zeroed.\n3. Memory safety and ring handling: Changes in `fdinfo.c` and `register.c` correct CQE32 and SQE128 index calculations and wrap-around bounds checks in shared ring buffers. Any indexing or sizing bugs would result in out-of-bounds access or data corruption, which are directly detected by KASAN. Concurrency, memory barrier, and locking adjustments (e.g. in `mpscq.h`, `io-wq.c`, `waitid.c`) are monitored by LOCKDEP.\n\nBecause no uninitialized stack/heap reads, uninitialized conditional branches, or uninitialized user-space info-leaks are introduced, a dedicated KMSAN session is not justified.",
"NeedsKMSAN": false,
"Reasoning": "The patch set modifies core io_uring execution logic across multiple subsystems including request cancellation (io-wq cancel races and running flag tracking), futex wait preparation and cancellation, waitid cancellation, timeout sequence logic and update conversions, network recv buffer recycling on partial transfers, fixed pipe error unwinding, and ring resizing for mixed/128b SQEs and 32b CQEs. All these code paths are directly reachable from userspace via io_uring syscalls in standard virtualized environments and present significant synchronization, cancellation, and state management complexities that warrant fuzzing.",
"WorthFuzzing": true
} |
| 1/1 | 2026/10/16 00:51 | action | read-patch-diff |
0mArgs:null Results: {
"PatchDiff": "commit 48577bc0265d4180886ef9a5bafa875e849d186b\nAuthor: syz-cluster \u003ctriage@syzkaller.com\u003e\nDate: Fri Oct 16 00:51:24 2026 +0000\n\n syz-cluster: applied patch under review\n\ndiff --git a/include/linux/io_uring_types.h b/include/linux/io_uring_types.h\nindex 50970b382a9ab..3e00012cbfd40 100644\n--- a/include/linux/io_uring_types.h\n+++ b/include/linux/io_uring_types.h\n@@ -520,6 +520,9 @@ struct io_ring_ctx {\n \tu32\t\t\t\tiowq_limits[2];\n \n \tstruct callback_head\t\tpoll_wq_task_work;\n+\t/* deferred -\u003epoll_wq wake up for local task_work */\n+\tstruct rcu_head\t\t\tpoll_wq_rcu;\n+\tatomic_t\t\t\tpoll_wq_rcu_pending;\n \tstruct list_head\t\tdefer_list;\n \tunsigned\t\t\tnr_drained;\n \ndiff --git a/io_uring/cancel.c b/io_uring/cancel.c\nindex 9a92a6aa2bccd..6f7a6cf356c27 100644\n--- a/io_uring/cancel.c\n+++ b/io_uring/cancel.c\n@@ -72,7 +72,16 @@ static bool io_cancel_cb(struct io_wq_work *work, void *data)\n \tstruct io_kiocb *req = container_of(work, struct io_kiocb, work);\n \tstruct io_cancel_data *cd = data;\n \n-\treturn io_cancel_req_match(req, cd);\n+\tif (!io_cancel_req_match(req, cd))\n+\t\treturn false;\n+\t/*\n+\t * Pending work is gone once it has matched. Work that is running may\n+\t * park the request where the same cancellation looks next. Don't make\n+\t * it skip the request as one that it has dealt with.\n+\t */\n+\tif (cd-\u003eflags \u0026 (IORING_ASYNC_CANCEL_ALL | IORING_ASYNC_CANCEL_ANY))\n+\t\treq-\u003ecancel_seq_set = false;\n+\treturn true;\n }\n \n static int io_async_cancel_one(struct io_uring_task *tctx,\n@@ -117,6 +126,8 @@ int io_try_cancel(struct io_uring_task *tctx, struct io_cancel_data *cd,\n \t */\n \tif (!ret)\n \t\treturn 0;\n+\tif (ret == -EALREADY)\n+\t\tcd-\u003erunning = true;\n \n \tret = io_poll_cancel(ctx, cd, issue_flags);\n \tif (ret != -ENOENT)\n@@ -180,6 +191,7 @@ static int __io_async_cancel(struct io_cancel_data *cd,\n \tstruct io_tctx_node *node;\n \tint ret, nr = 0;\n \n+\tcd-\u003erunning = false;\n \tdo {\n \t\tret = io_try_cancel(tctx, cd, issue_flags);\n \t\tif (ret == -ENOENT)\n@@ -195,6 +207,9 @@ static int __io_async_cancel(struct io_cancel_data *cd,\n \tmutex_lock(\u0026ctx-\u003etctx_lock);\n \tret = -ENOENT;\n \tlist_for_each_entry(node, \u0026ctx-\u003etctx_list, ctx_node) {\n+\t\t/* has been done above, don't count what is running there */\n+\t\tif (all \u0026\u0026 node-\u003etask-\u003eio_uring == tctx)\n+\t\t\tcontinue;\n \t\tret = io_async_cancel_one(node-\u003etask-\u003eio_uring, cd);\n \t\tif (ret != -ENOENT) {\n \t\t\tif (!all)\n@@ -204,7 +219,15 @@ static int __io_async_cancel(struct io_cancel_data *cd,\n \t}\n \tmutex_unlock(\u0026ctx-\u003etctx_lock);\n \tio_ring_submit_unlock(ctx, issue_flags);\n-\treturn all ? nr : ret;\n+\tif (all)\n+\t\treturn nr;\n+\t/*\n+\t * The worker may be done with the work that was found running, and\n+\t * flagged, above. That isn't the same as not having found it.\n+\t */\n+\tif (ret == -ENOENT \u0026\u0026 cd-\u003erunning)\n+\t\tret = -EALREADY;\n+\treturn ret;\n }\n \n int io_async_cancel(struct io_kiocb *req, unsigned int issue_flags)\n@@ -308,7 +331,7 @@ int io_sync_cancel(struct io_ring_ctx *ctx, void __user *arg)\n \tif (ret != -EALREADY)\n \t\tgoto out;\n \n-\tif (sc.timeout.tv_sec != -1UL || sc.timeout.tv_nsec != -1UL) {\n+\tif (sc.timeout.tv_sec != -1 || sc.timeout.tv_nsec != -1) {\n \t\tstruct timespec64 ts = {\n \t\t\t.tv_sec\t\t= sc.timeout.tv_sec,\n \t\t\t.tv_nsec\t= sc.timeout.tv_nsec\n@@ -694,8 +717,18 @@ __cold void io_uring_cancel_generic(bool cancel_all, struct io_sq_data *sqd)\n \t\tio_uring_drop_tctx_refs(current);\n \t\t/* only skip the sleep for local work this task may run */\n \t\txa_for_each(\u0026tctx-\u003exa, index, node) {\n-\t\t\tif (io_local_work_pending(node-\u003ectx) \u0026\u0026\n-\t\t\t io_allowed_defer_tw_run(node-\u003ectx))\n+\t\t\tif (!(node-\u003ectx-\u003eflags \u0026 IORING_SETUP_DEFER_TASKRUN) ||\n+\t\t\t !io_allowed_defer_tw_run(node-\u003ectx))\n+\t\t\t\tcontinue;\n+\t\t\t/*\n+\t\t\t * The wake up io_uring_try_cancel_requests() asked for\n+\t\t\t * may have been issued while we were still running. Ask\n+\t\t\t * again now that it can't be missed.\n+\t\t\t */\n+\t\t\tatomic_set(\u0026node-\u003ectx-\u003ecq_wait_nr, 1);\n+\t\t\t/* pairs with the xchg() in mpscq_push() */\n+\t\t\tsmp_mb();\n+\t\t\tif (io_local_work_pending(node-\u003ectx))\n \t\t\t\tgoto end_wait;\n \t\t}\n \t\t/*\ndiff --git a/io_uring/cancel.h b/io_uring/cancel.h\nindex e49713a1bc669..484fa5e7ffaaf 100644\n--- a/io_uring/cancel.h\n+++ b/io_uring/cancel.h\n@@ -11,6 +11,8 @@ struct io_cancel_data {\n \tu8 opcode;\n \tu32 flags;\n \tint seq;\n+\t/* io_try_cancel() has found it being run by io-wq, and flagged it */\n+\tbool running;\n };\n \n int io_async_cancel_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe);\ndiff --git a/io_uring/fdinfo.c b/io_uring/fdinfo.c\nindex c362f3a81bdce..46e64b0413605 100644\n--- a/io_uring/fdinfo.c\n+++ b/io_uring/fdinfo.c\n@@ -154,20 +154,26 @@ static void __io_uring_show_fdinfo(struct io_ring_ctx *ctx, struct seq_file *m)\n \tcq_entries = min(cq_tail - cq_head, ctx-\u003ecq_entries);\n \tfor (i = 0; i \u003c cq_entries; i++) {\n \t\tstruct io_uring_cqe *cqe;\n-\t\tbool cqe32 = false;\n+\t\tbool cqe32 = false, two_entries = false;\n \t\tbool is_last_cqarray_slot = (cq_head \u0026 cq_mask) == cq_mask;\n \n-\t\tcqe = \u0026r-\u003ecqes[(cq_head \u0026 cq_mask)];\n-\t\t/*\n-\t\t * Userspace can manipulate the last cqarray slot to have\n-\t\t * IORING_CQE_F_32 set, which would cause the second half of\n-\t\t * that CQE to be read out of bounds.\n-\t\t * Ignore the flag for the last cqarray slot, which can't\n-\t\t * legitimately have the flag set.\n-\t\t */\n-\t\tif ((cqe-\u003eflags \u0026 IORING_CQE_F_32 || ctx-\u003eflags \u0026 IORING_SETUP_CQE32) \u0026\u0026\n-\t\t !is_last_cqarray_slot)\n+\t\tif (ctx-\u003eflags \u0026 IORING_SETUP_CQE32) {\n+\t\t\t/* all entries have twice the size */\n+\t\t\tcqe = \u0026r-\u003ecqes[(cq_head \u0026 cq_mask) \u003c\u003c 1];\n \t\t\tcqe32 = true;\n+\t\t} else {\n+\t\t\tcqe = \u0026r-\u003ecqes[(cq_head \u0026 cq_mask)];\n+\t\t\t/*\n+\t\t\t * Userspace can manipulate the last cqarray slot to\n+\t\t\t * have IORING_CQE_F_32 set, which would cause the second\n+\t\t\t * half of that CQE to be read out of bounds.\n+\t\t\t * Ignore the flag for the last cqarray slot, which can't\n+\t\t\t * legitimately have the flag set.\n+\t\t\t */\n+\t\t\ttwo_entries = (cqe-\u003eflags \u0026 IORING_CQE_F_32) \u0026\u0026\n+\t\t\t\t !is_last_cqarray_slot;\n+\t\t\tcqe32 = two_entries;\n+\t\t}\n \t\tseq_printf(m, \"%5u: user_data:%llu, res:%d, flags:%x\",\n \t\t\t cq_head \u0026 cq_mask, cqe-\u003euser_data, cqe-\u003eres,\n \t\t\t cqe-\u003eflags);\n@@ -176,7 +182,7 @@ static void __io_uring_show_fdinfo(struct io_ring_ctx *ctx, struct seq_file *m)\n \t\t\t\t\tcqe-\u003ebig_cqe[0], cqe-\u003ebig_cqe[1]);\n \t\tseq_printf(m, \"\\n\");\n \t\tcq_head++;\n-\t\tif (cqe32) {\n+\t\tif (two_entries) {\n \t\t\tcq_head++;\n \t\t\ti++;\n \t\t}\ndiff --git a/io_uring/futex.c b/io_uring/futex.c\nindex eaee14242a3a3..9203998654ffb 100644\n--- a/io_uring/futex.c\n+++ b/io_uring/futex.c\n@@ -152,6 +152,24 @@ int io_futex_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)\n \treturn 0;\n }\n \n+/*\n+ * A wait on a private futex does not block the submitter. Even a single\n+ * threaded task can go on and have the mm private hash installed by clone()\n+ * or prctl(), which does not pick up waiters from the global hash. Make\n+ * sure the private hash exists before the first waiter is queued.\n+ */\n+static int io_futex_prep_private(struct io_kiocb *req)\n+{\n+\tint ret = futex_hash_allocate_default();\n+\n+\tif (unlikely(ret))\n+\t\treturn ret;\n+\n+\t/* inflight tracking only needed for mm private hash */\n+\tio_req_track_inflight(req);\n+\treturn 0;\n+}\n+\n int io_futex_wait_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)\n {\n \tstruct io_futex *iof = io_kiocb_to_cmd(req, struct io_futex);\n@@ -161,9 +179,8 @@ int io_futex_wait_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)\n \tif (unlikely(ret))\n \t\treturn ret;\n \n-\t/* inflight tracking only needed for mm private hash */\n \tif (!(iof-\u003efutex_flags \u0026 FLAGS_SHARED))\n-\t\tio_req_track_inflight(req);\n+\t\treturn io_futex_prep_private(req);\n \treturn 0;\n }\n \n@@ -213,10 +230,13 @@ int io_futexv_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)\n \t\treturn ret;\n \t}\n \n-\t/* inflight tracking only needed for mm private hash */\n \tfor (i = 0; i \u003c iof-\u003efutex_nr; i++) {\n \t\tif (!(ifd-\u003efutexv[i].w.flags \u0026 FLAGS_SHARED)) {\n-\t\t\tio_req_track_inflight(req);\n+\t\t\tret = io_futex_prep_private(req);\n+\t\t\tif (ret) {\n+\t\t\t\tkfree(ifd);\n+\t\t\t\treturn ret;\n+\t\t\t}\n \t\t\tbreak;\n \t\t}\n \t}\n@@ -283,6 +303,8 @@ int io_futexv_wait(struct io_kiocb *req, unsigned int issue_flags)\n \t\t */\n \t\t__set_current_state(TASK_RUNNING);\n \t\thlist_add_head(\u0026req-\u003ehash_node, \u0026ctx-\u003efutex_list);\n+\t\tif (io_wq_cancel_seen(req, issue_flags))\n+\t\t\t__io_futex_cancel(req);\n \t} else {\n \t\tiof-\u003efutexv_unqueued = 1;\n \t\tif (woken != -1)\n@@ -323,6 +345,8 @@ int io_futex_wait(struct io_kiocb *req, unsigned int issue_flags)\n \t\t\t \u0026ifd-\u003eq, NULL, NULL);\n \tif (!ret) {\n \t\thlist_add_head(\u0026req-\u003ehash_node, \u0026ctx-\u003efutex_list);\n+\t\tif (io_wq_cancel_seen(req, issue_flags))\n+\t\t\t__io_futex_cancel(req);\n \t\tio_ring_submit_unlock(ctx, issue_flags);\n \n \t\treturn IOU_ISSUE_SKIP_COMPLETE;\ndiff --git a/io_uring/io-wq.c b/io_uring/io-wq.c\nindex 97523ccc7a714..dfdc78612783a 100644\n--- a/io_uring/io-wq.c\n+++ b/io_uring/io-wq.c\n@@ -56,6 +56,8 @@ struct io_worker {\n \tstruct io_wq_acct *acct;\n \n \tstruct io_wq_work *cur_work;\n+\t/* rest of the hashed chain -\u003ecur_work was taken off the list with */\n+\tstruct io_wq_work *next_work;\n \traw_spinlock_t lock;\n \n \tstruct completion ref_done;\n@@ -467,11 +469,9 @@ static void io_wq_dec_running(struct io_worker *worker)\n \n \tif (!atomic_dec_and_test(\u0026acct-\u003enr_running))\n \t\treturn;\n-\tif (!worker-\u003ecur_work)\n-\t\treturn;\n \tif (!io_acct_run_queue(acct))\n \t\treturn;\n-\tif (io_wq_hash_defer(worker-\u003ecur_work, acct)) {\n+\tif (worker-\u003ecur_work \u0026\u0026 io_wq_hash_defer(worker-\u003ecur_work, acct)) {\n \t\traw_spin_unlock(\u0026acct-\u003elock);\n \t\treturn;\n \t}\n@@ -583,6 +583,17 @@ static struct io_wq_work *io_get_next_work(struct io_wq_acct *acct,\n \treturn NULL;\n }\n \n+static void __io_assign_current_work(struct io_worker *worker,\n+\t\t\t\t struct io_wq_work *work)\n+{\n+\traw_spin_lock(\u0026worker-\u003elock);\n+\tworker-\u003ecur_work = work;\n+\t/* stays valid while in between two items of a chain */\n+\tif (work)\n+\t\tworker-\u003enext_work = wq_next_work(work);\n+\traw_spin_unlock(\u0026worker-\u003elock);\n+}\n+\n static void io_assign_current_work(struct io_worker *worker,\n \t\t\t\t struct io_wq_work *work)\n {\n@@ -591,9 +602,7 @@ static void io_assign_current_work(struct io_worker *worker,\n \t\tcond_resched();\n \t}\n \n-\traw_spin_lock(\u0026worker-\u003elock);\n-\tworker-\u003ecur_work = work;\n-\traw_spin_unlock(\u0026worker-\u003elock);\n+\t__io_assign_current_work(worker, work);\n }\n \n /*\n@@ -625,9 +634,7 @@ static void io_worker_handle_work(struct io_wq_acct *acct,\n \t\t\t * work list, but isn't yet discoverable as the\n \t\t\t * current work item for this worker.\n \t\t\t */\n-\t\t\traw_spin_lock(\u0026worker-\u003elock);\n-\t\t\tworker-\u003ecur_work = work;\n-\t\t\traw_spin_unlock(\u0026worker-\u003elock);\n+\t\t\t__io_assign_current_work(worker, work);\n \t\t}\n \n \t\traw_spin_unlock(\u0026acct-\u003elock);\n@@ -646,25 +653,32 @@ static void io_worker_handle_work(struct io_wq_acct *acct,\n \t\t/* handle a whole dependent link */\n \t\tdo {\n \t\t\tbool do_kill = test_bit(IO_WQ_BIT_EXIT, \u0026wq-\u003estate);\n-\t\t\tstruct io_wq_work *next_hashed, *linked;\n+\t\t\tstruct io_wq_work *next_hashed, *linked, *done = work;\n \t\t\tunsigned int work_flags = atomic_read(\u0026work-\u003eflags);\n \t\t\tunsigned int hash = __io_wq_is_hashed(work_flags)\n \t\t\t\t? __io_get_work_hash(work_flags)\n \t\t\t\t: -1U;\n \t\t\tstruct io_kiocb *req;\n+\t\t\tbool last;\n \n \t\t\tnext_hashed = wq_next_work(work);\n \n-\t\t\tif (do_kill \u0026\u0026\n-\t\t\t (work_flags \u0026 IO_WQ_WORK_UNBOUND))\n+\t\t\t/*\n+\t\t\t * io_wq_exit_workers() signals us once, so that we\n+\t\t\t * don't sleep for ever. If that was before this work\n+\t\t\t * became the current one, the signal is gone.\n+\t\t\t */\n+\t\t\tif (do_kill \u0026\u0026 (work_flags \u0026 IO_WQ_WORK_UNBOUND))\n \t\t\t\tatomic_or(IO_WQ_WORK_CANCEL, \u0026work-\u003eflags);\n+\t\t\telse if (do_kill)\n+\t\t\t\tset_notify_signal(current);\n \t\t\treq = container_of(work, struct io_kiocb, work);\n \t\t\tkcov_remote_start_common(req-\u003ectx-\u003ekcov_handle);\n \t\t\tio_wq_submit_work(work);\n \t\t\tkcov_remote_stop();\n \t\t\tio_assign_current_work(worker, NULL);\n \n-\t\t\tlinked = io_wq_free_work(work);\n+\t\t\tlast = io_wq_put_work(work, \u0026linked);\n \t\t\twork = next_hashed;\n \t\t\tif (!work \u0026\u0026 linked \u0026\u0026 !io_wq_is_hashed(linked)) {\n \t\t\t\twork = linked;\n@@ -673,6 +687,13 @@ static void io_worker_handle_work(struct io_wq_acct *acct,\n \t\t\tio_assign_current_work(worker, work);\n \t\t\tif (linked)\n \t\t\t\tio_wq_enqueue(wq, linked);\n+\t\t\t/*\n+\t\t\t * Not before the link is current or pending work again.\n+\t\t\t * This may be the last time that a task is woken up\n+\t\t\t * which exits and looks for requests to cancel.\n+\t\t\t */\n+\t\t\tif (last)\n+\t\t\t\tio_wq_free_work(done);\n \n \t\t\tif (hash != -1U \u0026\u0026 !next_hashed) {\n \t\t\t\t/* serialize hash clear with wake_up() */\n@@ -691,6 +712,35 @@ static void io_worker_handle_work(struct io_wq_acct *acct,\n \t} while (1);\n }\n \n+/*\n+ * The worker has decided to exit, and has given up its -\u003enr_workers slot and\n+ * its place on the free list. io_wq_enqueue() may have picked it from that\n+ * list, or seen it running, after it last looked at the work list. Leave\n+ * -\u003enr_running as well and check again. Returns false if the worker has to\n+ * stay.\n+ */\n+static bool io_worker_retire(struct io_wq_acct *acct, struct io_worker *worker)\n+{\n+\t/* turns io_wq_dec_running() and the scheduler hooks into no-ops */\n+\tclear_bit(IO_WORKER_F_UP, \u0026worker-\u003eflags);\n+\tatomic_dec(\u0026acct-\u003enr_running);\n+\tif (!io_acct_run_queue(acct))\n+\t\treturn true;\n+\traw_spin_unlock(\u0026acct-\u003elock);\n+\n+\traw_spin_lock(\u0026acct-\u003eworkers_lock);\n+\tif (acct-\u003enr_workers \u003e= acct-\u003emax_workers) {\n+\t\t/* a worker was created in our place, the work is its business */\n+\t\traw_spin_unlock(\u0026acct-\u003eworkers_lock);\n+\t\treturn true;\n+\t}\n+\tacct-\u003enr_workers++;\n+\traw_spin_unlock(\u0026acct-\u003eworkers_lock);\n+\tatomic_inc(\u0026acct-\u003enr_running);\n+\tset_bit(IO_WORKER_F_UP, \u0026worker-\u003eflags);\n+\treturn false;\n+}\n+\n static int io_wq_worker(void *data)\n {\n \tstruct io_worker *worker = data;\n@@ -724,19 +774,41 @@ static int io_wq_worker(void *data)\n \t\t * idle-exit, drop the worker as well. This is used to avoid\n \t\t * keeping io-wq workers around for tasks that no longer have\n \t\t * any active io_uring instances.\n+\t\t * Not as long as a request of ours for another worker is\n+\t\t * pending, though. It is accounted as a running worker, which\n+\t\t * others rely on, and io_worker_exit() would cancel it.\n \t\t */\n-\t\tif ((last_timeout \u0026\u0026 (exit_mask || acct-\u003enr_workers \u003e 1)) ||\n-\t\t test_bit(IO_WQ_BIT_EXIT_ON_IDLE, \u0026wq-\u003estate)) {\n+\t\tif (((last_timeout \u0026\u0026 (exit_mask || acct-\u003enr_workers \u003e 1)) ||\n+\t\t test_bit(IO_WQ_BIT_EXIT_ON_IDLE, \u0026wq-\u003estate)) \u0026\u0026\n+\t\t !test_bit(0, \u0026worker-\u003ecreate_state)) {\n \t\t\tacct-\u003enr_workers--;\n+\t\t\tif (test_and_clear_bit(IO_WORKER_F_FREE, \u0026worker-\u003eflags))\n+\t\t\t\thlist_nulls_del_init_rcu(\u0026worker-\u003enulls_node);\n \t\t\traw_spin_unlock(\u0026acct-\u003eworkers_lock);\n \t\t\t__set_current_state(TASK_RUNNING);\n-\t\t\tbreak;\n+\t\t\tif (io_worker_retire(acct, worker))\n+\t\t\t\tbreak;\n+\t\t\tlast_timeout = false;\n+\t\t\tcontinue;\n \t\t}\n \t\tlast_timeout = false;\n \t\t__io_worker_idle(acct, worker);\n \t\traw_spin_unlock(\u0026acct-\u003eworkers_lock);\n \t\tif (io_run_task_work())\n \t\t\tcontinue;\n+\t\t/*\n+\t\t * io_wq_enqueue() leaves new work to us for as long as we are\n+\t\t * accounted as running. Get out of -\u003enr_running before taking\n+\t\t * the last look at the work list, rather than when scheduling\n+\t\t * out. No need to ask for another worker then, we can do it.\n+\t\t */\n+\t\tclear_bit(IO_WORKER_F_RUNNING, \u0026worker-\u003eflags);\n+\t\tatomic_dec(\u0026acct-\u003enr_running);\n+\t\tif (io_acct_run_queue(acct)) {\n+\t\t\tio_wq_worker_running(current);\n+\t\t\tio_worker_handle_work(acct, worker);\n+\t\t\tcontinue;\n+\t\t}\n \t\tret = schedule_timeout(WORKER_IDLE_TIMEOUT);\n \t\tif (signal_pending(current)) {\n \t\t\tstruct ksignal ksig;\n@@ -1002,9 +1074,13 @@ void io_wq_set_exit_on_idle(struct io_wq *wq, bool enable)\n static void io_run_cancel(struct io_wq_work *work, struct io_wq *wq)\n {\n \tdo {\n+\t\tstruct io_wq_work *linked;\n+\n \t\tatomic_or(IO_WQ_WORK_CANCEL, \u0026work-\u003eflags);\n \t\tio_wq_submit_work(work);\n-\t\twork = io_wq_free_work(work);\n+\t\tif (io_wq_put_work(work, \u0026linked))\n+\t\t\tio_wq_free_work(work);\n+\t\twork = linked;\n \t} while (work);\n }\n \n@@ -1112,6 +1188,7 @@ static bool __io_wq_worker_cancel(struct io_worker *worker,\n static bool io_wq_worker_cancel(struct io_worker *worker, void *data)\n {\n \tstruct io_cb_cancel_data *match = data;\n+\tstruct io_wq_work *work;\n \n \t/*\n \t * Hold the lock to avoid -\u003ecur_work going out of scope, caller\n@@ -1120,6 +1197,19 @@ static bool io_wq_worker_cancel(struct io_worker *worker, void *data)\n \traw_spin_lock(\u0026worker-\u003elock);\n \tif (__io_wq_worker_cancel(worker, match, worker-\u003ecur_work))\n \t\tmatch-\u003enr_running++;\n+\t/*\n+\t * Hashed work is taken off the work list a chain at a time, what the\n+\t * worker hasn't started yet can only be found here. It doesn't move on\n+\t * without the lock. Just mark the work, it fails once it is its turn.\n+\t */\n+\tfor (work = worker-\u003enext_work; work; work = wq_next_work(work)) {\n+\t\tif (match-\u003enr_running \u0026\u0026 !match-\u003ecancel_all)\n+\t\t\tbreak;\n+\t\tif (match-\u003efn(work, match-\u003edata)) {\n+\t\t\tatomic_or(IO_WQ_WORK_CANCEL, \u0026work-\u003eflags);\n+\t\t\tmatch-\u003enr_running++;\n+\t\t}\n+\t}\n \traw_spin_unlock(\u0026worker-\u003elock);\n \n \treturn match-\u003enr_running \u0026\u0026 !match-\u003ecancel_all;\ndiff --git a/io_uring/io_uring.c b/io_uring/io_uring.c\nindex 0c91d466d8259..e6569c2049172 100644\n--- a/io_uring/io_uring.c\n+++ b/io_uring/io_uring.c\n@@ -407,17 +407,46 @@ static void io_prep_async_link(struct io_kiocb *req)\n \t}\n }\n \n+/* Called for every request that is issued: keep it inline */\n+static __always_inline bool io_assign_file(struct io_kiocb *req,\n+\t\t\t\t\t const struct io_issue_def *def,\n+\t\t\t\t\t unsigned int issue_flags)\n+{\n+\tif (req-\u003efile || !def-\u003eneeds_file)\n+\t\treturn true;\n+\n+\tif (req-\u003eflags \u0026 REQ_F_FIXED_FILE)\n+\t\treq-\u003efile = io_file_get_fixed(req, req-\u003ecqe.fd, issue_flags);\n+\telse\n+\t\treq-\u003efile = io_file_get_normal(req, req-\u003ecqe.fd);\n+\n+\treturn !!req-\u003efile;\n+}\n+\n void io_queue_iowq(struct io_kiocb *req)\n {\n \tstruct io_uring_task *tctx = req-\u003etctx;\n \n \tBUG_ON(!tctx);\n+\tlockdep_assert_held(\u0026req-\u003ectx-\u003euring_lock);\n \n \tif ((current-\u003eflags \u0026 PF_KTHREAD) || !tctx-\u003eio_wq) {\n \t\tio_req_task_queue_fail(req, -ECANCELED);\n \t\treturn;\n \t}\n \n+\t/*\n+\t * The file is looked up as late as possible, as it may be the result\n+\t * of an earlier request of the link. This one is up now, and what kind\n+\t * of work it is depends on the file. If that fails, leave the error\n+\t * handling to the worker.\n+\t *\n+\t * Not for a splice: whatever it writes to, it may wait for its input\n+\t * for ever. It has to remain unbound work that isn't hashed.\n+\t */\n+\tif (req-\u003eopcode != IORING_OP_SPLICE)\n+\t\tio_assign_file(req, \u0026io_issue_defs[req-\u003eopcode], 0);\n+\n \t/* init -\u003ework of the whole link before punting */\n \tio_prep_async_link(req);\n \n@@ -729,6 +758,13 @@ bool io_cqe_cache_refill(struct io_ring_ctx *ctx, bool overflow, bool cqe32)\n \tif (!overflow \u0026\u0026 (ctx-\u003echeck_cq \u0026 BIT(IO_CHECK_CQ_OVERFLOW_BIT)))\n \t\treturn false;\n \n+\t/*\n+\t * A single entry may be left if a 32b CQE is asked for. It must not be\n+\t * handed out later on if this fails, as the CQE is going to overflow.\n+\t */\n+\tctx-\u003ecqe_cached = NULL;\n+\tctx-\u003ecqe_sentinel = NULL;\n+\n \t/*\n \t * Post dummy CQE if a 32b CQE is needed and there's only room for a\n \t * 16b CQE before the ring wraps.\n@@ -1390,20 +1426,6 @@ static __cold void io_drain_req(struct io_kiocb *req)\n \t\tctx-\u003eint_flags \u0026= ~IO_RING_F_DRAIN_ACTIVE;\n }\n \n-static bool io_assign_file(struct io_kiocb *req, const struct io_issue_def *def,\n-\t\t\t unsigned int issue_flags)\n-{\n-\tif (req-\u003efile || !def-\u003eneeds_file)\n-\t\treturn true;\n-\n-\tif (req-\u003eflags \u0026 REQ_F_FIXED_FILE)\n-\t\treq-\u003efile = io_file_get_fixed(req, req-\u003ecqe.fd, issue_flags);\n-\telse\n-\t\treq-\u003efile = io_file_get_normal(req, req-\u003ecqe.fd);\n-\n-\treturn !!req-\u003efile;\n-}\n-\n #define REQ_ISSUE_SLOW_FLAGS\t(REQ_F_CREDS | REQ_F_ARM_LTIMEOUT)\n \n static inline int __io_issue_sqe(struct io_kiocb *req,\n@@ -1450,6 +1472,9 @@ static int io_issue_sqe(struct io_kiocb *req, unsigned int issue_flags)\n \tret = __io_issue_sqe(req, issue_flags, def);\n \n \tif (ret == IOU_COMPLETE) {\n+\t\t/* not all handlers take care of the internal restart codes */\n+\t\tif (unlikely(req-\u003ecqe.res \u003c= -ERESTARTSYS))\n+\t\t\treq-\u003ecqe.res = io_fixup_restart_res(req-\u003ecqe.res);\n \t\tif (issue_flags \u0026 IO_URING_F_COMPLETE_DEFER)\n \t\t\tio_req_complete_defer(req);\n \t\telse\n@@ -1464,6 +1489,8 @@ static int io_issue_sqe(struct io_kiocb *req, unsigned int issue_flags)\n \t\tif (req-\u003eflags \u0026 REQ_F_IOPOLL)\n \t\t\tio_iopoll_req_issued(req, issue_flags);\n \t}\n+\tif (unlikely(ret \u003c= -ERESTARTSYS))\n+\t\tret = io_fixup_restart_res(ret);\n \treturn ret;\n }\n \n@@ -1486,28 +1513,40 @@ int io_poll_issue(struct io_kiocb *req, io_tw_token_t tw)\n \treturn ret;\n }\n \n-struct io_wq_work *io_wq_free_work(struct io_wq_work *work)\n+/*\n+ * io-wq is done with @work. Returns true if it held the last reference, and\n+ * hands out the next request of the link then. The caller has to make sure\n+ * that cancellations can find the latter before it calls io_wq_free_work().\n+ */\n+bool io_wq_put_work(struct io_wq_work *work, struct io_wq_work **linked)\n {\n \tstruct io_kiocb *req = container_of(work, struct io_kiocb, work);\n \tstruct io_kiocb *nxt = NULL;\n \n-\tif (req_ref_put_and_test_atomic(req)) {\n-\t\t/*\n-\t\t * Only continue the link from here if the completion has been\n-\t\t * posted. If it's still pending, io_free_req() completes the\n-\t\t * request and the next link follows from there, after the CQE.\n-\t\t */\n-\t\tif ((req-\u003eflags \u0026 IO_REQ_LINK_FLAGS) \u0026\u0026\n-\t\t (req-\u003eflags \u0026 REQ_F_CQE_SKIP)) {\n-\t\t\tstruct io_ring_ctx *ctx = req-\u003ectx;\n+\t*linked = NULL;\n+\tif (!req_ref_put_and_test_atomic(req))\n+\t\treturn false;\n \n-\t\t\tmutex_lock(\u0026ctx-\u003euring_lock);\n-\t\t\tnxt = io_req_find_next(req);\n-\t\t\tmutex_unlock(\u0026ctx-\u003euring_lock);\n-\t\t}\n-\t\tio_free_req(req);\n+\t/*\n+\t * Only continue the link from here if the completion has been posted.\n+\t * If it's still pending, io_free_req() completes the request and the\n+\t * next link follows from there, after the CQE.\n+\t */\n+\tif ((req-\u003eflags \u0026 IO_REQ_LINK_FLAGS) \u0026\u0026 (req-\u003eflags \u0026 REQ_F_CQE_SKIP)) {\n+\t\tstruct io_ring_ctx *ctx = req-\u003ectx;\n+\n+\t\tmutex_lock(\u0026ctx-\u003euring_lock);\n+\t\tnxt = io_req_find_next(req);\n+\t\tmutex_unlock(\u0026ctx-\u003euring_lock);\n \t}\n-\treturn nxt ? \u0026nxt-\u003ework : NULL;\n+\tif (nxt)\n+\t\t*linked = \u0026nxt-\u003ework;\n+\treturn true;\n+}\n+\n+void io_wq_free_work(struct io_wq_work *work)\n+{\n+\tio_free_req(container_of(work, struct io_kiocb, work));\n }\n \n void io_wq_submit_work(struct io_wq_work *work)\n@@ -1518,7 +1557,7 @@ void io_wq_submit_work(struct io_wq_work *work)\n \tbool needs_poll = false;\n \tint ret = 0, err = -ECANCELED;\n \n-\t/* one will be dropped by io_wq_free_work() after returning to io-wq */\n+\t/* one will be dropped by io_wq_put_work() after returning to io-wq */\n \tif (!(req-\u003eflags \u0026 REQ_F_REFCOUNT))\n \t\t__io_req_set_refcount(req, 2);\n \telse\n@@ -1799,17 +1838,27 @@ static int io_init_req(struct io_ring_ctx *ctx, struct io_kiocb *req,\n \t\t * A 128b op on a non-128b SQ requires mixed SQE support as\n \t\t * well as 2 contiguous entries.\n \t\t */\n-\t\tif (!(ctx-\u003eflags \u0026 IORING_SETUP_SQE_MIXED) || *left \u003c 2 ||\n+\t\tif (!(ctx-\u003eflags \u0026 IORING_SETUP_SQE_MIXED) ||\n \t\t (unsigned)(sqe - ctx-\u003esq_sqes) \u003e= ctx-\u003esq_entries - 1)\n \t\t\treturn io_init_fail_req(req, -EINVAL);\n+\t\tif (*left \u003e= 2) {\n+\t\t\t/*\n+\t\t\t * A 128b operation on a mixed SQ uses two entries, so\n+\t\t\t * we have to increment the head and cached refs, and\n+\t\t\t * decrement what's left.\n+\t\t\t */\n+\t\t\tcurrent-\u003eio_uring-\u003ecached_refs++;\n+\t\t\t(*left)--;\n+\t\t} else if (!(ctx-\u003eflags \u0026 IORING_SETUP_SQPOLL) ||\n+\t\t\t !__io_sqring_entries(ctx)) {\n+\t\t\treturn io_init_fail_req(req, -EINVAL);\n+\t\t}\n \t\t/*\n-\t\t * A 128b operation on a mixed SQ uses two entries, so we have\n-\t\t * to increment the head and cached refs, and decrement what's\n-\t\t * left.\n+\t\t * else: the SQPOLL thread caps the number of entries it submits\n+\t\t * in one go, which is none of the application's business. Take\n+\t\t * the 2nd entry on top.\n \t\t */\n-\t\tcurrent-\u003eio_uring-\u003ecached_refs++;\n \t\tctx-\u003ecached_sq_head++;\n-\t\t(*left)--;\n \t}\n \n \tif (unlikely(sqe_flags \u0026 ~SQE_COMMON_FLAGS)) {\n@@ -2336,6 +2385,7 @@ struct io_tctx_exit {\n \tstruct callback_head\t\ttask_work;\n \tstruct completion\t\tcompletion;\n \tstruct io_ring_ctx\t\t*ctx;\n+\tbool\t\t\t\tin_cancel;\n };\n \n static __cold void io_tctx_exit_cb(struct callback_head *cb)\n@@ -2350,7 +2400,8 @@ static __cold void io_tctx_exit_cb(struct callback_head *cb)\n \t * tctx can be NULL if the queueing of this task_work raced with\n \t * work cancelation off the exec path.\n \t */\n-\tif (tctx \u0026\u0026 !atomic_read(\u0026tctx-\u003ein_cancel))\n+\twork-\u003ein_cancel = tctx \u0026\u0026 atomic_read(\u0026tctx-\u003ein_cancel);\n+\tif (tctx \u0026\u0026 !work-\u003ein_cancel)\n \t\tio_uring_del_tctx_node((unsigned long)work-\u003ectx);\n \tcomplete(\u0026work-\u003ecompletion);\n }\n@@ -2518,6 +2569,14 @@ static __cold void io_ring_exit_work(struct work_struct *work)\n \t\t * wait is marked as interruptible.\n \t\t */\n \t\twait_for_completion_interruptible(\u0026exit.completion);\n+\t\t/*\n+\t\t * The task removes the node when it is done with its\n+\t\t * cancellation. Give it the time for that: it doesn't get back\n+\t\t * to it as long as it finds new task_work each time it has run\n+\t\t * the old one, and we may be one of many who send it.\n+\t\t */\n+\t\tif (exit.in_cancel)\n+\t\t\tschedule_timeout_idle(HZ / 20);\n \t\tmutex_lock(\u0026ctx-\u003euring_lock);\n \t\tmutex_lock(\u0026ctx-\u003etctx_lock);\n \t}\ndiff --git a/io_uring/io_uring.h b/io_uring/io_uring.h\nindex 109261722ca74..203bf019e9319 100644\n--- a/io_uring/io_uring.h\n+++ b/io_uring/io_uring.h\n@@ -204,7 +204,8 @@ int io_do_iopoll(struct io_ring_ctx *ctx, bool force_nonspin);\n __cold void io_iopoll_try_reap_events(struct io_ring_ctx *ctx);\n void __io_submit_flush_completions(struct io_ring_ctx *ctx);\n \n-struct io_wq_work *io_wq_free_work(struct io_wq_work *work);\n+bool io_wq_put_work(struct io_wq_work *work, struct io_wq_work **linked);\n+void io_wq_free_work(struct io_wq_work *work);\n void io_wq_submit_work(struct io_wq_work *work);\n \n void io_free_req(struct io_kiocb *req);\n@@ -318,6 +319,24 @@ static __always_inline bool io_fill_cqe_req(struct io_ring_ctx *ctx,\n \treturn true;\n }\n \n+static inline ssize_t io_fixup_restart_res(ssize_t ret)\n+{\n+\tswitch (ret) {\n+\tcase -ERESTARTSYS:\n+\tcase -ERESTARTNOINTR:\n+\tcase -ERESTARTNOHAND:\n+\tcase -ERESTART_RESTARTBLOCK:\n+\t\t/*\n+\t\t * We can't just restart the syscall, since previously\n+\t\t * submitted sqes may already be in progress. Just fail\n+\t\t * this IO with EINTR.\n+\t\t */\n+\t\treturn -EINTR;\n+\tdefault:\n+\t\treturn ret;\n+\t}\n+}\n+\n static inline void *io_uring_alloc_async_data(struct io_alloc_cache *cache,\n \t\t\t\t\t struct io_kiocb *req)\n {\n@@ -380,6 +399,23 @@ static inline void io_ring_submit_lock(struct io_ring_ctx *ctx,\n \tlockdep_assert_held(\u0026ctx-\u003euring_lock);\n }\n \n+/*\n+ * For a handler that is run by io-wq and parks the request on a list where\n+ * cancellations find it, rather than completing it. Whoever tried to cancel\n+ * the request before that found it running. It has flagged the work item,\n+ * has searched the list in vain, and won't come back. To be called with\n+ * -\u003euring_lock held, which the search needs as well, once the request is on\n+ * the list: if the search came first, the flag is visible.\n+ */\n+static inline bool io_wq_cancel_seen(struct io_kiocb *req,\n+\t\t\t\t unsigned int issue_flags)\n+{\n+\tlockdep_assert_held(\u0026req-\u003ectx-\u003euring_lock);\n+\n+\treturn (issue_flags \u0026 IO_URING_F_IOWQ) \u0026\u0026\n+\t (atomic_read(\u0026req-\u003ework.flags) \u0026 IO_WQ_WORK_CANCEL);\n+}\n+\n static inline void io_commit_cqring(struct io_ring_ctx *ctx)\n {\n \t/* order cqe stores with ring update */\ndiff --git a/io_uring/kbuf.c b/io_uring/kbuf.c\nindex 6e2facf34bfa6..c9641b6ccc8ff 100644\n--- a/io_uring/kbuf.c\n+++ b/io_uring/kbuf.c\n@@ -204,7 +204,7 @@ static struct io_br_sel io_ring_buffer_select(struct io_kiocb *req, size_t *len,\n \tif (unlikely(tail == head))\n \t\treturn sel;\n \n-\tif (head + 1 == tail)\n+\tif ((__u16)(head + 1) == tail)\n \t\treq-\u003eflags |= REQ_F_BL_EMPTY;\n \n \tbuf = io_ring_head_to_buf(br, head, bl-\u003emask);\ndiff --git a/io_uring/loop.c b/io_uring/loop.c\nindex 3d472b7827fac..2340459164a1a 100644\n--- a/io_uring/loop.c\n+++ b/io_uring/loop.c\n@@ -10,12 +10,6 @@ static inline int io_loop_nr_cqes(const struct io_ring_ctx *ctx,\n \treturn lp-\u003ecq_wait_idx - READ_ONCE(ctx-\u003erings-\u003ecq.tail);\n }\n \n-static inline void io_loop_wait_start(struct io_ring_ctx *ctx, unsigned nr_wait)\n-{\n-\tatomic_set(\u0026ctx-\u003ecq_wait_nr, nr_wait);\n-\tset_current_state(TASK_INTERRUPTIBLE);\n-}\n-\n static inline void io_loop_wait_finish(struct io_ring_ctx *ctx)\n {\n \t__set_current_state(TASK_RUNNING);\n@@ -25,7 +19,7 @@ static inline void io_loop_wait_finish(struct io_ring_ctx *ctx)\n static void io_loop_wait(struct io_ring_ctx *ctx, struct iou_loop_params *lp,\n \t\t\t unsigned nr_wait)\n {\n-\tio_loop_wait_start(ctx, nr_wait);\n+\tio_cq_wait_arm(ctx, nr_wait);\n \n \tif (unlikely(io_local_work_pending(ctx) ||\n \t\t io_loop_nr_cqes(ctx, lp) \u003c= 0) ||\ndiff --git a/io_uring/mpscq.h b/io_uring/mpscq.h\nindex f910526766fd8..da9474497cc8e 100644\n--- a/io_uring/mpscq.h\n+++ b/io_uring/mpscq.h\n@@ -25,9 +25,10 @@\n * its predecessor. In between, the tail end of the queue is not yet reachable\n * from the head. mpscq_pop() detects this and returns NULL, while mpscq_empty()\n * reports false. The consumer must not treat such a NULL as \"queue empty\" - it\n- * should retry later. The window is two instructions wide, but a producer can\n- * be preempted inside it, so the consumer must not spin on it while holding\n- * resources the producer might need to make progress.\n+ * should retry later. The window is two instructions wide and the producer\n+ * can't be preempted inside it, but it can be interrupted, so the consumer must\n+ * not spin on it while holding resources the producer might need to make\n+ * progress.\n *\n * The consumer side only supports a single consumer at a time, callers must\n * provide their own serialization for it. The stub node is what allows the\n@@ -69,6 +70,12 @@ static inline bool mpscq_push(struct mpscq *q, struct llist_node *node)\n \tstruct llist_node *prev;\n \n \tnode-\u003enext = NULL;\n+\t/*\n+\t * All the consumer can do about a node that isn't linked yet is to try\n+\t * again. If it is what preempted us, and we don't get to run as long\n+\t * as it does, that never ends.\n+\t */\n+\tpreempt_disable();\n \t/*\n \t * xchg() implies a full barrier, so the initialization of the\n \t * entry (including -\u003enext above) is visible before the node can\n@@ -77,6 +84,7 @@ static inline bool mpscq_push(struct mpscq *q, struct llist_node *node)\n \t */\n \tprev = xchg(\u0026q-\u003etail, node);\n \tWRITE_ONCE(prev-\u003enext, node);\n+\tpreempt_enable();\n \treturn prev == \u0026q-\u003estub;\n }\n \ndiff --git a/io_uring/napi.c b/io_uring/napi.c\nindex ca1c814fa73a2..7e698720cd51a 100644\n--- a/io_uring/napi.c\n+++ b/io_uring/napi.c\n@@ -6,7 +6,7 @@\n #ifdef CONFIG_NET_RX_BUSY_POLL\n \n /* Timeout for cleanout of stale entries. */\n-#define NAPI_TIMEOUT\t\t(60 * SEC_CONVERSION)\n+#define NAPI_TIMEOUT\t\t(60 * HZ)\n \n struct io_napi_entry {\n \tunsigned int\t\tnapi_id;\n@@ -378,6 +378,8 @@ void __io_napi_busy_loop(struct io_ring_ctx *ctx, struct io_wait_queue *iowq)\n \tif (iowq-\u003etimeout != KTIME_MAX) {\n \t\tktime_t dt = ktime_sub(iowq-\u003etimeout, io_get_time(ctx));\n \n+\t\t/* an absolute timeout may have passed already */\n+\t\tdt = max_t(ktime_t, dt, 0);\n \t\tiowq-\u003enapi_busy_poll_dt = min_t(u64, iowq-\u003enapi_busy_poll_dt, dt);\n \t}\n \ndiff --git a/io_uring/net.c b/io_uring/net.c\nindex f5f4f3ce6ef35..b1bf7dbec5d27 100644\n--- a/io_uring/net.c\n+++ b/io_uring/net.c\n@@ -480,6 +480,24 @@ static int io_bundle_nbufs(struct io_async_msghdr *kmsg, int ret)\n \treturn nbufs;\n }\n \n+/*\n+ * A receive that is retried after a partial transfer keeps writing to what it\n+ * has mapped. io_net_kbuf_recyle() only commits what was filled so far, which\n+ * takes a buffer out of circulation if it's a single one that is consumed as a\n+ * whole. The tail of an incrementally consumed buffer, or the yet unused\n+ * buffers of a bundle, would be up for grabs. They can't be committed in\n+ * advance either, the CQE has no way to tell if they end up not being used.\n+ */\n+static bool io_net_kbuf_can_retry(struct io_kiocb *req,\n+\t\t\t\t struct io_buffer_list *bl,\n+\t\t\t\t struct io_async_msghdr *kmsg)\n+{\n+\t/* not ring provided, or committed at selection time */\n+\tif (!(req-\u003eflags \u0026 REQ_F_BUFFERS_COMMIT))\n+\t\treturn true;\n+\treturn !(bl-\u003eflags \u0026 IOBL_INC) \u0026\u0026 iter_is_ubuf(\u0026kmsg-\u003emsg.msg_iter);\n+}\n+\n static int io_net_kbuf_recyle(struct io_kiocb *req, struct io_buffer_list *bl,\n \t\t\t struct io_async_msghdr *kmsg, int len)\n {\n@@ -1091,7 +1109,8 @@ int io_recvmsg(struct io_kiocb *req, unsigned int issue_flags)\n \t\t\tio_kbuf_recycle(req, sel.buf_list, issue_flags);\n \t\t\treturn IOU_RETRY;\n \t\t}\n-\t\tif (ret \u003e 0 \u0026\u0026 io_net_retry(sock, flags)) {\n+\t\tif (ret \u003e 0 \u0026\u0026 io_net_retry(sock, flags) \u0026\u0026\n+\t\t io_net_kbuf_can_retry(req, sel.buf_list, kmsg)) {\n \t\t\tsr-\u003edone_io += ret;\n \t\t\treturn io_net_kbuf_recyle(req, sel.buf_list, kmsg, ret);\n \t\t}\n@@ -1252,7 +1271,8 @@ int io_recv(struct io_kiocb *req, unsigned int issue_flags)\n \t\t\tio_kbuf_recycle(req, sel.buf_list, issue_flags);\n \t\t\treturn IOU_RETRY;\n \t\t}\n-\t\tif (ret \u003e 0 \u0026\u0026 io_net_retry(sock, flags)) {\n+\t\tif (ret \u003e 0 \u0026\u0026 io_net_retry(sock, flags) \u0026\u0026\n+\t\t io_net_kbuf_can_retry(req, sel.buf_list, kmsg)) {\n \t\t\tsr-\u003elen -= ret;\n \t\t\tsr-\u003ebuf += ret;\n \t\t\tsr-\u003edone_io += ret;\ndiff --git a/io_uring/openclose.c b/io_uring/openclose.c\nindex c71242915dade..24cf3ce2e2bc3 100644\n--- a/io_uring/openclose.c\n+++ b/io_uring/openclose.c\n@@ -375,13 +375,16 @@ static int io_pipe_fixed(struct io_kiocb *req, struct file **files,\n \tfds[1] = alloc_slot ? ret : slot - 1;\n \tfiles[1] = NULL;\n \n-\tio_ring_submit_unlock(ctx, issue_flags);\n-\n-\tif (!copy_to_user(p-\u003efds, fds, sizeof(fds)))\n+\t/*\n+\t * Hang on to the lock. If this fails, the slots have to be cleared, and\n+\t * they must still hold what was put there.\n+\t */\n+\tif (!copy_to_user(p-\u003efds, fds, sizeof(fds))) {\n+\t\tio_ring_submit_unlock(ctx, issue_flags);\n \t\treturn 0;\n+\t}\n \n \tret = -EFAULT;\n-\tio_ring_submit_lock(ctx, issue_flags);\n err:\n \tif (fds[0] != -1)\n \t\tio_fixed_fd_remove(ctx, fds[0]);\ndiff --git a/io_uring/poll.c b/io_uring/poll.c\nindex 5447a7c24dce6..7dcb904e8fc3e 100644\n--- a/io_uring/poll.c\n+++ b/io_uring/poll.c\n@@ -617,6 +617,18 @@ static int __io_arm_poll_handler(struct io_kiocb *req,\n \n \tio_poll_add_hash(req, issue_flags);\n \n+\t/*\n+\t * Whoever tried to cancel the request while io-wq was running it has\n+\t * flagged the work item. If it searched the hash table before the\n+\t * request got there it found nothing, and it won't come back. Both\n+\t * the search and the insertion are done with -\u003euring_lock held, and\n+\t * the flag is set before the search. If the search came first,\n+\t * the flag is visible by now.\n+\t */\n+\tif (ipt-\u003eowning \u0026\u0026\n+\t (atomic_read(\u0026req-\u003ework.flags) \u0026 IO_WQ_WORK_CANCEL))\n+\t\tio_poll_mark_cancelled(req);\n+\n \tif (mask \u0026\u0026 (poll-\u003eevents \u0026 EPOLLET) \u0026\u0026\n \t io_poll_can_finish_inline(req, ipt)) {\n \t\t__io_poll_execute(req, mask, 0);\n@@ -906,6 +918,15 @@ int io_poll_add(struct io_kiocb *req, unsigned int issue_flags)\n \n \tipt.pt._qproc = io_poll_queue_proc;\n \n+\t/*\n+\t * A poll update or a spurious wake up arms the request again. Forget\n+\t * the entries of the previous round like io_arm_apoll() does, a stale\n+\t * second entry would be taken for a third wait queue.\n+\t */\n+\tif (req_has_async_data(req))\n+\t\tio_req_async_data_free(req);\n+\treq-\u003eflags \u0026= ~(REQ_F_SINGLE_POLL | REQ_F_DOUBLE_POLL);\n+\n \tret = __io_arm_poll_handler(req, poll, \u0026ipt, poll-\u003eevents, issue_flags);\n \tif (ret \u003e 0) {\n \t\tio_req_set_res(req, ipt.result_mask, 0);\ndiff --git a/io_uring/register.c b/io_uring/register.c\nindex dab3eb4258dff..2ce36623e25a6 100644\n--- a/io_uring/register.c\n+++ b/io_uring/register.c\n@@ -496,6 +496,17 @@ static void io_register_free_rings(struct io_ring_ctx *ctx,\n \t\t\t IORING_SETUP_CQE32 | IORING_SETUP_NO_MMAP | \\\n \t\t\t IORING_SETUP_CQE_MIXED | IORING_SETUP_SQE_MIXED)\n \n+static bool io_sqe_is_128(struct io_ring_ctx *ctx,\n+\t\t\t const struct io_uring_sqe *sqe)\n+{\n+\tu8 opcode = READ_ONCE(sqe-\u003eopcode);\n+\n+\tif (!(ctx-\u003eflags \u0026 IORING_SETUP_SQE_MIXED) || opcode \u003e= IORING_OP_LAST)\n+\t\treturn false;\n+\topcode = array_index_nospec(opcode, IORING_OP_LAST);\n+\treturn io_issue_defs[opcode].is_128;\n+}\n+\n static int io_register_resize_rings(struct io_ring_ctx *ctx, void __user *arg)\n {\n \tstruct io_ctx_config config;\n@@ -505,6 +516,8 @@ static int io_register_resize_rings(struct io_ring_ctx *ctx, void __user *arg)\n \tstruct io_uring_params *p = \u0026config.p;\n \tstruct io_rings_layout *rl = \u0026config.layout;\n \tu32 *o_sq_array, *n_sq_array = NULL;\n+\tbool cqe32_tail = false;\n+\tunsigned int sqe128_src = 0;\n \tint ret;\n \n \tmemset(\u0026config, 0, sizeof(config));\n@@ -603,13 +616,20 @@ static int io_register_resize_rings(struct io_ring_ctx *ctx, void __user *arg)\n \told_head = READ_ONCE(o.rings-\u003esq.head);\n \tif (tail - old_head \u003e p-\u003esq_entries)\n \t\tgoto overflow;\n-\tfor (i = old_head; i \u003c tail; i++) {\n+\tfor (i = old_head; i != tail; i++) {\n+\t\tbool sqe128_tail = sqe128_src;\n \t\tunsigned int dst, src;\n \t\tsize_t sq_size;\n \n \t\tdst = i \u0026 (p-\u003esq_entries - 1);\n \t\tsrc = i \u0026 (ctx-\u003esq_entries - 1);\n-\t\tif (n_sq_array) {\n+\t\tif (sqe128_tail) {\n+\t\t\t/* it follows the first half, whatever the array says */\n+\t\t\tsrc = sqe128_src;\n+\t\t\tsqe128_src = 0;\n+\t\t\tif (n_sq_array)\n+\t\t\t\tWRITE_ONCE(n_sq_array[dst], dst);\n+\t\t} else if (n_sq_array) {\n \t\t\tsrc = READ_ONCE(o_sq_array[src]);\n \t\t\tif (unlikely(src \u003e= ctx-\u003esq_entries)) {\n \t\t\t\tWRITE_ONCE(n_sq_array[dst], UINT_MAX);\n@@ -618,6 +638,19 @@ static int io_register_resize_rings(struct io_ring_ctx *ctx, void __user *arg)\n \t\t\tWRITE_ONCE(n_sq_array[dst], dst);\n \t\t}\n \n+\t\t/*\n+\t\t * A 128b SQE on a SQE_MIXED ring takes two contiguous entries\n+\t\t * and must not wrap. A smaller ring may place it on the last\n+\t\t * entry, fail the resize if that happens.\n+\t\t */\n+\t\tif (!sqe128_tail \u0026\u0026 i + 1 != tail \u0026\u0026\n+\t\t src \u003c ctx-\u003esq_entries - 1 \u0026\u0026\n+\t\t io_sqe_is_128(ctx, \u0026o.sq_sqes[src])) {\n+\t\t\tif (dst == p-\u003esq_entries - 1)\n+\t\t\t\tgoto overflow;\n+\t\t\tsqe128_src = src + 1;\n+\t\t}\n+\n \t\tsq_size = sizeof(struct io_uring_sqe);\n \t\tif (ctx-\u003eflags \u0026 IORING_SETUP_SQE128) {\n \t\t\tdst \u003c\u003c= 1;\n@@ -640,7 +673,7 @@ static int io_register_resize_rings(struct io_ring_ctx *ctx, void __user *arg)\n \t\tret = -EOVERFLOW;\n \t\tgoto out;\n \t}\n-\tfor (i = old_head; i \u003c tail; i++) {\n+\tfor (i = old_head; i != tail; i++) {\n \t\tunsigned index, dst_mask, src_mask;\n \t\tsize_t cq_size;\n \n@@ -654,6 +687,21 @@ static int io_register_resize_rings(struct io_ring_ctx *ctx, void __user *arg)\n \t\t\tsrc_mask = (ctx-\u003ecq_entries \u003c\u003c 1) - 1;\n \t\t\tdst_mask = (p-\u003ecq_entries \u003c\u003c 1) - 1;\n \t\t}\n+\t\t/*\n+\t\t * A 32b CQE on a CQE_MIXED ring takes two contiguous entries\n+\t\t * and must not wrap. A smaller ring may place it on the last\n+\t\t * entry, fail the resize if that happens.\n+\t\t */\n+\t\tif (ctx-\u003eflags \u0026 IORING_SETUP_CQE_MIXED) {\n+\t\t\tif (cqe32_tail) {\n+\t\t\t\tcqe32_tail = false;\n+\t\t\t} else if (READ_ONCE(o.rings-\u003ecqes[index \u0026 src_mask].flags) \u0026\n+\t\t\t\t IORING_CQE_F_32) {\n+\t\t\t\tif ((index \u0026 dst_mask) == dst_mask)\n+\t\t\t\t\tgoto overflow;\n+\t\t\t\tcqe32_tail = true;\n+\t\t\t}\n+\t\t}\n \t\tmemcpy(\u0026n.rings-\u003ecqes[index \u0026 dst_mask], \u0026o.rings-\u003ecqes[index \u0026 src_mask], cq_size);\n \t}\n \tWRITE_ONCE(n.rings-\u003ecq.head, old_head);\ndiff --git a/io_uring/rw.c b/io_uring/rw.c\nindex d33b925a848e5..a65693e393695 100644\n--- a/io_uring/rw.c\n+++ b/io_uring/rw.c\n@@ -613,24 +613,6 @@ static void io_complete_rw_iopoll(struct kiocb *kiocb, long res)\n \tsmp_store_release(\u0026req-\u003eiopoll_completed, 1);\n }\n \n-static inline ssize_t io_fixup_restart_res(ssize_t ret)\n-{\n-\tswitch (ret) {\n-\tcase -ERESTARTSYS:\n-\tcase -ERESTARTNOINTR:\n-\tcase -ERESTARTNOHAND:\n-\tcase -ERESTART_RESTARTBLOCK:\n-\t\t/*\n-\t\t * We can't just restart the syscall, since previously\n-\t\t * submitted sqes may already be in progress. Just fail\n-\t\t * this IO with EINTR.\n-\t\t */\n-\t\treturn -EINTR;\n-\tdefault:\n-\t\treturn ret;\n-\t}\n-}\n-\n static inline void io_rw_done(struct io_kiocb *req, ssize_t ret)\n {\n \tstruct io_rw *rw = io_kiocb_to_cmd(req, struct io_rw);\ndiff --git a/io_uring/sqpoll.c b/io_uring/sqpoll.c\nindex ad42e8eb10029..ee97808a065a9 100644\n--- a/io_uring/sqpoll.c\n+++ b/io_uring/sqpoll.c\n@@ -385,6 +385,13 @@ static int io_sq_thread(void *data)\n \t\t\t\t */\n \t\t\t\tsmp_mb__after_atomic();\n \n+\t\t\t\t/*\n+\t\t\t\t * Nothing gets submitted before the ring is\n+\t\t\t\t * enabled, which comes with a wake up.\n+\t\t\t\t */\n+\t\t\t\tif (READ_ONCE(ctx-\u003eflags) \u0026 IORING_SETUP_R_DISABLED)\n+\t\t\t\t\tcontinue;\n+\n \t\t\t\tif (io_sqring_entries(ctx)) {\n \t\t\t\t\tneeds_sched = false;\n \t\t\t\t\tbreak;\ndiff --git a/io_uring/timeout.c b/io_uring/timeout.c\nindex 9c239c0a17155..b219dfdf7af5a 100644\n--- a/io_uring/timeout.c\n+++ b/io_uring/timeout.c\n@@ -321,22 +321,29 @@ static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer)\n \treturn HRTIMER_NORESTART;\n }\n \n-static struct io_kiocb *io_timeout_extract(struct io_ring_ctx *ctx,\n-\t\t\t\t\t struct io_cancel_data *cd)\n+static struct io_kiocb *io_timeout_find(struct io_ring_ctx *ctx,\n+\t\t\t\t\tstruct io_cancel_data *cd)\n \t__must_hold(\u0026ctx-\u003etimeout_lock)\n {\n \tstruct io_timeout *timeout;\n-\tstruct io_timeout_data *io;\n-\tstruct io_kiocb *req = NULL;\n \n \tlist_for_each_entry(timeout, \u0026ctx-\u003etimeout_list, list) {\n-\t\tstruct io_kiocb *tmp = cmd_to_io_kiocb(timeout);\n+\t\tstruct io_kiocb *req = cmd_to_io_kiocb(timeout);\n \n-\t\tif (io_cancel_req_match(tmp, cd)) {\n-\t\t\treq = tmp;\n-\t\t\tbreak;\n-\t\t}\n+\t\tif (io_cancel_req_match(req, cd))\n+\t\t\treturn req;\n \t}\n+\treturn NULL;\n+}\n+\n+static struct io_kiocb *io_timeout_extract(struct io_ring_ctx *ctx,\n+\t\t\t\t\t struct io_cancel_data *cd)\n+\t__must_hold(\u0026ctx-\u003etimeout_lock)\n+{\n+\tstruct io_kiocb *req = io_timeout_find(ctx, cd);\n+\tstruct io_timeout *timeout;\n+\tstruct io_timeout_data *io;\n+\n \tif (!req)\n \t\treturn ERR_PTR(-ENOENT);\n \n@@ -386,6 +393,9 @@ static void io_req_task_link_timeout(struct io_tw_req tw_req, io_tw_token_t tw)\n \t\t\t};\n \n \t\t\tret = io_try_cancel(req-\u003etctx, \u0026cd, 0);\n+\t\t\t/* a number of requests for futex and waitid */\n+\t\t\tif (ret \u003e 0)\n+\t\t\t\tret = 0;\n \t\t} else {\n \t\t\tret = -ECANCELED;\n \t\t}\n@@ -458,6 +468,8 @@ static int io_linked_timeout_update(struct io_ring_ctx *ctx, __u64 user_data,\n \tio = req-\u003easync_data;\n \tif (hrtimer_try_to_cancel(\u0026io-\u003etimer) == -1)\n \t\treturn -EALREADY;\n+\tif (mode == HRTIMER_MODE_ABS)\n+\t\tts = timens_ktime_to_host(io_timeout_get_clock(io), ts);\n \thrtimer_setup(\u0026io-\u003etimer, io_link_timeout_fn, io_timeout_get_clock(io), mode);\n \thrtimer_start(\u0026io-\u003etimer, ts, mode);\n \treturn 0;\n@@ -468,15 +480,26 @@ static int io_timeout_update(struct io_ring_ctx *ctx, __u64 user_data,\n \t__must_hold(\u0026ctx-\u003etimeout_lock)\n {\n \tstruct io_cancel_data cd = { .ctx = ctx, .data = user_data, };\n-\tstruct io_kiocb *req = io_timeout_extract(ctx, \u0026cd);\n+\tstruct io_kiocb *req = io_timeout_find(ctx, \u0026cd);\n \tstruct io_timeout *timeout = io_kiocb_to_cmd(req, struct io_timeout);\n \tstruct io_timeout_data *data;\n \n-\tif (IS_ERR(req))\n-\t\treturn PTR_ERR(req);\n+\tif (!req)\n+\t\treturn -ENOENT;\n \n-\ttimeout-\u003eoff = 0; /* noseq */\n \tdata = req-\u003easync_data;\n+\t/* multishot requests only make sense with rel values */\n+\tif ((data-\u003eflags \u0026 IORING_TIMEOUT_MULTISHOT) \u0026\u0026 mode == HRTIMER_MODE_ABS)\n+\t\treturn -EINVAL;\n+\n+\treq = io_timeout_extract(ctx, \u0026cd);\n+\tif (IS_ERR(req))\n+\t\treturn PTR_ERR(req);\n+\t/* for a multishot request this isn't a sequence, keep the repeats */\n+\tif (!(data-\u003eflags \u0026 IORING_TIMEOUT_MULTISHOT))\n+\t\ttimeout-\u003eoff = 0; /* noseq */\n+\tif (mode == HRTIMER_MODE_ABS)\n+\t\ttime = timens_ktime_to_host(io_timeout_get_clock(data), time);\n \tdata-\u003etime = time;\n \n \tlist_add_tail(\u0026timeout-\u003elist, \u0026ctx-\u003etimeout_list);\n@@ -509,7 +532,12 @@ int io_timeout_remove_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)\n \t\t\t\t IORING_TIMEOUT_ABS |\n \t\t\t\t IORING_TIMEOUT_IMMEDIATE_ARG))\n \t\t\treturn -EINVAL;\n-\t\tret = io_parse_user_time(\u0026tr-\u003etime, READ_ONCE(sqe-\u003eaddr2), tr-\u003eflags);\n+\t\t/*\n+\t\t * The clock is that of the timeout to update, an absolute time\n+\t\t * is converted to the host's view once that has been found.\n+\t\t */\n+\t\tret = io_parse_user_time(\u0026tr-\u003etime, READ_ONCE(sqe-\u003eaddr2),\n+\t\t\t\t\t tr-\u003eflags \u0026 ~IORING_TIMEOUT_ABS);\n \t\tif (ret)\n \t\t\treturn ret;\n \t} else if (tr-\u003eflags) {\n@@ -645,8 +673,9 @@ int io_timeout(struct io_kiocb *req, unsigned int issue_flags)\n \tstruct io_timeout *timeout = io_kiocb_to_cmd(req, struct io_timeout);\n \tstruct io_ring_ctx *ctx = req-\u003ectx;\n \tstruct io_timeout_data *data = req-\u003easync_data;\n+\tstruct io_timeout *first;\n \tstruct list_head *entry;\n-\tu32 tail, off = timeout-\u003eoff;\n+\tu32 tail, base, off = timeout-\u003eoff;\n \n \traw_spin_lock_irq(\u0026ctx-\u003etimeout_lock);\n \n@@ -663,11 +692,17 @@ int io_timeout(struct io_kiocb *req, unsigned int issue_flags)\n \ttail = data_race(ctx-\u003ecached_cq_tail) - atomic_read(\u0026ctx-\u003ecq_timeouts);\n \ttimeout-\u003etarget_seq = tail + off;\n \n-\t/* Update the last seq here in case io_flush_timeouts() hasn't.\n-\t * This is safe because -\u003ecompletion_lock is held, and submissions\n-\t * and completions are never mixed in the same -\u003ecompletion_lock section.\n+\t/*\n+\t * Update the last seq here in case io_flush_timeouts() hasn't, it only\n+\t * gets called once sequences are in use. Leave it alone if there are\n+\t * timeouts waiting for one. CQEs may have been posted since the last\n+\t * flush, and a target that has been reached must not fall behind it.\n \t */\n-\tctx-\u003ecq_last_tm_flush = tail;\n+\tfirst = list_first_entry_or_null(\u0026ctx-\u003etimeout_list, struct io_timeout,\n+\t\t\t\t\t list);\n+\tif (!first || io_is_timeout_noseq(cmd_to_io_kiocb(first)))\n+\t\tctx-\u003ecq_last_tm_flush = tail;\n+\tbase = ctx-\u003ecq_last_tm_flush;\n \n \t/*\n \t * Insertion sort, ensuring the first entry in the list is always\n@@ -679,8 +714,8 @@ int io_timeout(struct io_kiocb *req, unsigned int issue_flags)\n \n \t\tif (io_is_timeout_noseq(nxt))\n \t\t\tcontinue;\n-\t\t/* nxt.seq is behind @tail, otherwise would've been completed */\n-\t\tif (off \u003e= nextt-\u003etarget_seq - tail)\n+\t\t/* no target is behind the last flush, see io_flush_timeouts() */\n+\t\tif (timeout-\u003etarget_seq - base \u003e= nextt-\u003etarget_seq - base)\n \t\t\tbreak;\n \t}\n add:\ndiff --git a/io_uring/tw.c b/io_uring/tw.c\nindex f9affd68dd263..0b5066f972245 100644\n--- a/io_uring/tw.c\n+++ b/io_uring/tw.c\n@@ -148,6 +148,37 @@ static void io_ctx_mark_taskrun(struct io_ring_ctx *ctx)\n \t}\n }\n \n+static void io_poll_wq_wake_rcu(struct rcu_head *rcu)\n+{\n+\tstruct io_ring_ctx *ctx = container_of(rcu, struct io_ring_ctx,\n+\t\t\t\t\t poll_wq_rcu);\n+\n+\tatomic_set(\u0026ctx-\u003epoll_wq_rcu_pending, 0);\n+\tio_poll_wq_wake(ctx);\n+\tpercpu_ref_put(\u0026ctx-\u003erefs);\n+}\n+\n+/*\n+ * io_uring_poll() reports pending local work as EPOLLIN, as it takes a trip to\n+ * the kernel to turn it into CQEs. Let those know who are asleep already.\n+ */\n+static void io_local_work_wake_poll(struct io_ring_ctx *ctx, unsigned int flags)\n+{\n+\tif (!(flags \u0026 IOU_F_TWQ_IN_WAKE)) {\n+\t\tio_poll_wq_wake(ctx);\n+\t\treturn;\n+\t}\n+\t/*\n+\t * Called from a wake up handler with some wait queue lock held, this\n+\t * one could lead back to it. Do it from a clean context.\n+\t */\n+\tif (!wq_has_sleeper(\u0026ctx-\u003epoll_wq) ||\n+\t atomic_xchg(\u0026ctx-\u003epoll_wq_rcu_pending, 1))\n+\t\treturn;\n+\tpercpu_ref_get(\u0026ctx-\u003erefs);\n+\tcall_rcu_hurry(\u0026ctx-\u003epoll_wq_rcu, io_poll_wq_wake_rcu);\n+}\n+\n void io_req_local_work_add(struct io_kiocb *req, unsigned flags)\n {\n \tstruct io_ring_ctx *ctx = req-\u003ectx;\n@@ -165,7 +196,7 @@ void io_req_local_work_add(struct io_kiocb *req, unsigned flags)\n \n \t/*\n \t * The xchg() in mpscq_push() implies a full barrier, which pairs with\n-\t * the barrier in set_current_state() on the io_cqring_wait() side. This\n+\t * the barrier in io_cq_wait_arm() on the io_cqring_wait() side. This\n \t * ensures that either we see the updated -\u003ecq_wait_nr, or waiters going\n \t * to sleep will observe the work added to the list, which is similar to\n \t * the wait/wake task state sync.\n@@ -174,6 +205,9 @@ void io_req_local_work_add(struct io_kiocb *req, unsigned flags)\n \t\tio_ctx_mark_taskrun(ctx);\n \t\tif (data_race(ctx-\u003eint_flags) \u0026 IO_RING_F_HAS_EVFD)\n \t\t\tio_eventfd_signal(ctx, false, flags \u0026 IOU_F_TWQ_IN_WAKE);\n+\t\t/* whoever sets the flag wakes -\u003epoll_wq afterwards */\n+\t\tif (data_race(ctx-\u003eint_flags) \u0026 IO_RING_F_POLL_ACTIVATED)\n+\t\t\tio_local_work_wake_poll(ctx, flags);\n \t}\n \n \t/*\ndiff --git a/io_uring/uring_cmd.c b/io_uring/uring_cmd.c\nindex 73b606899833f..b5486b56ee0c7 100644\n--- a/io_uring/uring_cmd.c\n+++ b/io_uring/uring_cmd.c\n@@ -87,7 +87,7 @@ static void io_uring_cmd_del_cancelable(struct io_uring_cmd *cmd,\n \n \tcmd-\u003eflags \u0026= ~IORING_URING_CMD_CANCELABLE;\n \tio_ring_submit_lock(ctx, issue_flags);\n-\thlist_del(\u0026req-\u003ehash_node);\n+\thlist_del_init(\u0026req-\u003ehash_node);\n \tio_ring_submit_unlock(ctx, issue_flags);\n }\n \ndiff --git a/io_uring/wait.c b/io_uring/wait.c\nindex a48523c2f356b..b7a0292767c7c 100644\n--- a/io_uring/wait.c\n+++ b/io_uring/wait.c\n@@ -26,7 +26,8 @@ static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode,\n \t */\n \tif (io_should_wake(iowq) || io_has_work(iowq-\u003ectx))\n \t\treturn autoremove_wake_function(curr, mode, wake_flags, key);\n-\treturn -1;\n+\t/* keep going, a waiter further down may be content with less */\n+\treturn 0;\n }\n \n int io_run_task_work_sig(struct io_ring_ctx *ctx)\n@@ -76,9 +77,16 @@ static enum hrtimer_restart io_cqring_min_timer_wakeup(struct hrtimer *timer)\n \tif (iowq-\u003etimeout == KTIME_MAX ||\n \t ktime_compare(iowq-\u003emin_timeout, iowq-\u003etimeout) \u003e= 0)\n \t\tgoto out_wake;\n-\t/* work we may need to run, wake function will see if we need to wake */\n-\tif (io_has_work(ctx))\n-\t\tgoto out_wake;\n+\t/*\n+\t * Work we need to run. It isn't known if that is going to post a CQE,\n+\t * so this isn't a timeout. Carry on with the normal one if it doesn't.\n+\t */\n+\tif (io_has_work(ctx)) {\n+\t\tiowq-\u003emin_timeout = 0;\n+\t\tiowq-\u003ecq_tail = iowq-\u003ecq_min_tail + 1;\n+\t\twake_up_process(iowq-\u003ewq.private);\n+\t\treturn HRTIMER_NORESTART;\n+\t}\n \t/* got events since we started waiting, min timeout is done */\n \tscoped_guard(rcu) {\n \t\tstruct io_rings *rings = io_get_rings(ctx);\n@@ -257,8 +265,7 @@ int io_cqring_wait(struct io_ring_ctx *ctx, int min_events, u32 flags,\n \t\tunsigned long check_cq;\n \n \t\tif (ctx-\u003eflags \u0026 IORING_SETUP_DEFER_TASKRUN) {\n-\t\t\tatomic_set(\u0026ctx-\u003ecq_wait_nr, nr_wait);\n-\t\t\tset_current_state(TASK_INTERRUPTIBLE);\n+\t\t\tio_cq_wait_arm(ctx, nr_wait);\n \t\t} else {\n \t\t\tprepare_to_wait_exclusive(\u0026ctx-\u003ecq_wait, \u0026iowq.wq,\n \t\t\t\t\t\t\tTASK_INTERRUPTIBLE);\n@@ -291,9 +298,18 @@ int io_cqring_wait(struct io_ring_ctx *ctx, int min_events, u32 flags,\n \n \t\tcheck_cq = READ_ONCE(ctx-\u003echeck_cq);\n \t\tif (unlikely(check_cq)) {\n-\t\t\t/* let the caller flush overflows, retry */\n-\t\t\tif (check_cq \u0026 BIT(IO_CHECK_CQ_OVERFLOW_BIT))\n+\t\t\tif (check_cq \u0026 BIT(IO_CHECK_CQ_OVERFLOW_BIT)) {\n \t\t\t\tio_cqring_do_overflow_flush(ctx);\n+\t\t\t\t/*\n+\t\t\t\t * No room for what has overflown. It takes the\n+\t\t\t\t * application to change that, so return to it.\n+\t\t\t\t */\n+\t\t\t\tif (test_bit(IO_CHECK_CQ_OVERFLOW_BIT,\n+\t\t\t\t\t \u0026ctx-\u003echeck_cq)) {\n+\t\t\t\t\tret = 0;\n+\t\t\t\t\tbreak;\n+\t\t\t\t}\n+\t\t\t}\n \t\t\tif (check_cq \u0026 BIT(IO_CHECK_CQ_DROPPED_BIT)) {\n \t\t\t\tret = -EBADR;\n \t\t\t\tbreak;\ndiff --git a/io_uring/wait.h b/io_uring/wait.h\nindex b7b9c46b1b013..eb5ae8a6fc942 100644\n--- a/io_uring/wait.h\n+++ b/io_uring/wait.h\n@@ -14,6 +14,21 @@\n */\n #define IO_CQ_WAKE_INIT\t\t(-1)\n \n+/*\n+ * Start a wait cycle of the current task. The add that uses up -\u003ecq_wait_nr\n+ * may be one that queued its entry long ago and only now gets to look at the\n+ * count. As nobody else wakes us after that, it has to find us sleeping, hence\n+ * the task state must be visible before the count is. The barrier pairs with\n+ * the xchg() in mpscq_push(), see io_req_local_work_add().\n+ */\n+static inline void io_cq_wait_arm(struct io_ring_ctx *ctx, int nr_wait)\n+{\n+\t__set_current_state(TASK_INTERRUPTIBLE);\n+\tatomic_set_release(\u0026ctx-\u003ecq_wait_nr, nr_wait);\n+\t/* order the above against looking at the work list */\n+\tsmp_mb();\n+}\n+\n struct ext_arg {\n \tsize_t argsz;\n \tstruct timespec64 ts;\ndiff --git a/io_uring/waitid.c b/io_uring/waitid.c\nindex 2b756017423f0..2ba2369681d0e 100644\n--- a/io_uring/waitid.c\n+++ b/io_uring/waitid.c\n@@ -265,12 +265,18 @@ static int io_waitid_wait(struct wait_queue_entry *wait, unsigned mode,\n \t\treturn 0;\n \n \tlist_del_init(\u0026wait-\u003eentry);\n-\tsmp_store_release(\u0026iw-\u003ehead, NULL);\n \n-\t/* cancel is in progress */\n+\t/*\n+\t * Cancel or issue is in progress. Leave -\u003ehead alone, so that whoever\n+\t * owns the request has to take the wait queue lock, which we hold,\n+\t * before it can complete and free it.\n+\t */\n \tif (atomic_fetch_inc(\u0026iw-\u003erefs) \u0026 IO_WAITID_REF_MASK)\n \t\treturn 1;\n \n+\t/* pairs with the smp_load_acquire() in io_waitid_remove_wq() */\n+\tsmp_store_release(\u0026iw-\u003ehead, NULL);\n+\n \treq-\u003eio_task_work.func = io_waitid_cb;\n \t__io_req_task_work_add(req, IOU_F_TWQ_IN_WAKE);\n \treturn 1;\n@@ -343,6 +349,8 @@ int io_waitid(struct io_kiocb *req, unsigned int issue_flags)\n \t\t * a waitqueue callback, or if someone cancels it.\n \t\t */\n \t\tif (!io_waitid_drop_issue_ref(req)) {\n+\t\t\tif (io_wq_cancel_seen(req, issue_flags))\n+\t\t\t\t__io_waitid_cancel(req, true);\n \t\t\tio_ring_submit_unlock(ctx, issue_flags);\n \t\t\treturn IOU_ISSUE_SKIP_COMPLETE;\n \t\t}\ndiff --git a/io_uring/zcrx.c b/io_uring/zcrx.c\nindex cc9df7e64a407..6d7abb1636e7f 100644\n--- a/io_uring/zcrx.c\n+++ b/io_uring/zcrx.c\n@@ -1645,7 +1645,7 @@ static bool io_zcrx_queue_cqe(struct io_kiocb *req, struct net_iov *niov,\n \t\tcqe-\u003eflags |= IORING_CQE_F_32;\n \n \tarea = io_zcrx_iov_to_area(niov);\n-\toffset = off + (net_iov_idx(niov) \u003c\u003c ifq-\u003eniov_shift);\n+\toffset = off + ((u64)net_iov_idx(niov) \u003c\u003c ifq-\u003eniov_shift);\n \trcqe = (struct io_uring_zcrx_cqe *)(cqe + 1);\n \trcqe-\u003eoff = offset + zcrx_area_id_to_token(area-\u003earea_id);\n \trcqe-\u003e__pad = 0;\n"
} |
| 2/1 | 2026/10/16 00:51 | agent | patch-evaluator |
1mModel:core Args: null Results: {
"EnableConfigs": [
"IO_URING",
"FUTEX",
"NET",
"NET_RX_BUSY_POLL",
"PAGE_POOL",
"INET",
"IO_URING_ZCRX"
],
"FocusSymbols": [
"io_async_cancel",
"io_sync_cancel",
"io_futex_wait",
"io_futexv_wait",
"io_waitid",
"io_timeout",
"io_poll_add",
"io_pipe",
"io_queue_iowq",
"io_wq_put_work"
],
"Reasoning": "The patch set modifies core io_uring execution logic across multiple subsystems including request cancellation (io-wq cancel races and running flag tracking), futex wait preparation and cancellation, waitid cancellation, timeout sequence logic and update conversions, network recv buffer recycling on partial transfers, fixed pipe error unwinding, and ring resizing for mixed/128b SQEs and 32b CQEs. All these code paths are directly reachable from userspace via io_uring syscalls in standard virtualized environments and present significant synchronization, cancellation, and state management complexities that warrant fuzzing.",
"WorthFuzzing": true
}Instruction: You are an expert Linux kernel maintainer and security engineer.
Your job is to review a provided patch series and evaluate whether it warrants fuzzing with syzkaller.
IMPORTANT: The changes have ALREADY been applied and committed as the HEAD commit in
your workspace. Do NOT rely on internal assumptions. You must actively use your code access
tools to inspect the actual source code, callers, and surrounding context.
================================================================================
1. CORE TRIAGE PHILOSOPHY
================================================================================
The goal of patch fuzzing is to discover crashes, regressions, exposed latent bugs,
and newly triggered assertions introduced by the patch series.
- REACHABILITY IS THE PRIMARY GATE:
Fuzzing can only discover bugs in code that can actually execute in standard virtualized
environments (GCE or QEMU, utilizing software-emulated devices like USB gadgets, netdev, tun/tap).
If the modified code is structurally unreachable (see Section 2), it MUST NOT be fuzzed,
regardless of whether it adds assertions or complex logic.
- DO NOT BLINDLY TRUST "NO FUNCTIONAL CHANGE" (NFCI) OR "REFACTORING" CLAIMS:
Patch authors routinely label changes as "cleanups", "refactorings", or state
"No functional change intended". Do NOT take these claims at face value.
Code refactorings that rearrange logic, introduce helper functions, or alter state management
in core subsystems frequently introduce subtle semantic shifts or uncover latent kernel bugs.
If reachable executable code is modified or refactored, it MUST be fuzzed.
- NEW OR MODIFIED ASSERTIONS IN REACHABLE CODE MUST BE FUZZED:
When a patch introduces or modifies runtime checks or assertions (e.g., WARN_ON*, VM_WARN_ON*,
BUG_ON*, lockdep_assert*) in reachable code paths, it enforces new or stricter invariants.
Even if the author believes the invariant always holds, fuzzing is essential to verify whether
an unusual sequence of operations can violate it.
================================================================================
2. WHEN TO RETURN WorthFuzzing=false (NEGATIVE CRITERIA)
================================================================================
Return WorthFuzzing=false ONLY IF all modified code falls strictly into one or more of these categories:
- Non-kernel and non-executable changes:
* Modifications to Documentation/, comments, or spelling fixes.
* User-space directories, self-tests, samples, or scripts (e.g., tools/, samples/, scripts/, usr/)
that do not affect the compiled kernel image (vmlinux) or kernel modules.
* Purely decorative logging (e.g., message strings in pr_err, printk, dev_info) or tracepoints
that do not alter control flow or data structures.
* Build system or Kconfig changes that do not alter compiled C logic.
- Structurally unreachable hardware:
* Vendor-specific PCIe switches, SmartNICs, or GPU drivers (e.g., mlxsw, pds_core, qed,
ionic, amdgpu) requiring physical ASIC/PCIe cards not emulated in standard QEMU.
- Unreachable execution paths:
* Driver teardown callbacks (.remove, .shutdown, pci_unregister_driver) executed only during
physical PCI hot-unplug or manual sysfs driver unbinding.
* Code paths exclusive to architectures other than the target architecture.
================================================================================
3. WHEN TO RETURN WorthFuzzing=true (POSITIVE CRITERIA)
================================================================================
Return WorthFuzzing=true whenever the patch touches reachable executable code, including:
- Core Subsystems:
* Any logic modifications in memory management (mm/), synchronization/locking (kernel/locking/),
BPF, scheduler, core networking, VFS, or syscall handling.
- Refactorings and Code Cleanups:
* Any restructuring of reachable data structures, helper abstractions, or algorithm flows.
- Runtime Assertions and Defensive Checks:
* Any introduction or alteration of assertions (WARN_ON*, VM_WARN_ON*, BUG_ON*, etc.) in reachable paths.
- Reachable Drivers and Protocols:
* Drivers accessible via virtual buses (virtio, USB gadget, loopback, netlink, binder, sockets, etc.).
================================================================================
4. EXTRACTING FocusSymbols (PREVENTING DILUTION)
================================================================================
When WorthFuzzing=true, you must extract specific kernel functions into FocusSymbols to guide the fuzzer:
- AVOID UBIQUITOUS LIFECYCLE HOT-PATHS:
Do NOT list generic, ubiquitous functions called by almost every program in the corpus
(including, but not limited to: general memory allocators and deallocators, page fault
and trap handlers, or core synchronization primitives; this is not an exhaustive list).
Listing ubiquitous functions causes the fuzzer to classify thousands of unrelated tests as "focused",
which severely dilutes fuzzing effort away from the actual changes.
- TARGET SPECIFIC FEATURE LOGIC AND ENTRYPOINTS:
List functions that specifically implement the logic being added or altered, or direct API entrypoints
for the subsystem feature under review.
- HANDLING STATIC INLINE FUNCTIONS IN HEADERS (.h):
Compiler-inlined static functions (such as static inlines in mm/*.h or include/linux/*.h) lack
distinct symbol addresses in vmlinux and cannot be targeted directly by symbol coverage filters.
If the changes are primarily in static inline helpers, identify non-static, feature-specific caller
functions in .c files that exercise them (avoiding ubiquitous lifecycle wrappers).
================================================================================
5. IDENTIFYING EnableConfigs
================================================================================
Identify any specific CONFIG_ options required to properly compile and reach the modified code:
- Inspect Kconfig files and #ifdef guards; do not make assumptions.
- Check "depends on" lines in Kconfig to include any non-standard parent subsystem configs needed.
- Strip any 'CONFIG_' prefix (e.g., return "NET_IPV4" instead of "CONFIG_NET_IPV4").
Prefer calling several tools at the same time to save round-trips.
Use set-results tool to provide results of the analysis.
It must be called exactly once before the final reply.
Ignore results of this tool.
Prompt: Target architecture: amd64
For your convenience, here is the diff of the changes:
commit 48577bc0265d4180886ef9a5bafa875e849d186b
Author: syz-cluster <triage@syzkaller.com>
Date: Fri Oct 16 00:51:24 2026 +0000
syz-cluster: applied patch under review
diff --git a/include/linux/io_uring_types.h b/include/linux/io_uring_types.h
index 50970b382a9ab..3e00012cbfd40 100644
--- a/include/linux/io_uring_types.h
+++ b/include/linux/io_uring_types.h
@@ -520,6 +520,9 @@ struct io_ring_ctx {
u32 iowq_limits[2];
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 nr_drained;
diff --git a/io_uring/cancel.c b/io_uring/cancel.c
index 9a92a6aa2bccd..6f7a6cf356c27 100644
--- a/io_uring/cancel.c
+++ b/io_uring/cancel.c
@@ -72,7 +72,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,
@@ -117,6 +126,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)
@@ -180,6 +191,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)
@@ -195,6 +207,9 @@ static int __io_async_cancel(struct io_cancel_data *cd,
mutex_lock(&ctx->tctx_lock);
ret = -ENOENT;
list_for_each_entry(node, &ctx->tctx_list, ctx_node) {
+ /* 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)
@@ -204,7 +219,15 @@ static int __io_async_cancel(struct io_cancel_data *cd,
}
mutex_unlock(&ctx->tctx_lock);
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)
@@ -308,7 +331,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
@@ -694,8 +717,18 @@ __cold void io_uring_cancel_generic(bool cancel_all, struct io_sq_data *sqd)
io_uring_drop_tctx_refs(current);
/* only skip the sleep for local work this task may run */
xa_for_each(&tctx->xa, index, node) {
- if (io_local_work_pending(node->ctx) &&
- io_allowed_defer_tw_run(node->ctx))
+ if (!(node->ctx->flags & IORING_SETUP_DEFER_TASKRUN) ||
+ !io_allowed_defer_tw_run(node->ctx))
+ continue;
+ /*
+ * The wake up io_uring_try_cancel_requests() asked for
+ * may have been issued while we were still running. Ask
+ * again now that it can't be missed.
+ */
+ atomic_set(&node->ctx->cq_wait_nr, 1);
+ /* pairs with the xchg() in mpscq_push() */
+ smp_mb();
+ if (io_local_work_pending(node->ctx))
goto end_wait;
}
/*
diff --git a/io_uring/cancel.h b/io_uring/cancel.h
index e49713a1bc669..484fa5e7ffaaf 100644
--- a/io_uring/cancel.h
+++ b/io_uring/cancel.h
@@ -11,6 +11,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);
diff --git a/io_uring/fdinfo.c b/io_uring/fdinfo.c
index c362f3a81bdce..46e64b0413605 100644
--- a/io_uring/fdinfo.c
+++ b/io_uring/fdinfo.c
@@ -154,20 +154,26 @@ static void __io_uring_show_fdinfo(struct io_ring_ctx *ctx, struct seq_file *m)
cq_entries = min(cq_tail - cq_head, ctx->cq_entries);
for (i = 0; i < cq_entries; i++) {
struct io_uring_cqe *cqe;
- bool cqe32 = false;
+ bool cqe32 = false, two_entries = false;
bool is_last_cqarray_slot = (cq_head & cq_mask) == cq_mask;
- cqe = &r->cqes[(cq_head & cq_mask)];
- /*
- * Userspace can manipulate the last cqarray slot to have
- * IORING_CQE_F_32 set, which would cause the second half of
- * that CQE to be read out of bounds.
- * Ignore the flag for the last cqarray slot, which can't
- * legitimately have the flag set.
- */
- if ((cqe->flags & IORING_CQE_F_32 || ctx->flags & IORING_SETUP_CQE32) &&
- !is_last_cqarray_slot)
+ if (ctx->flags & IORING_SETUP_CQE32) {
+ /* all entries have twice the size */
+ cqe = &r->cqes[(cq_head & cq_mask) << 1];
cqe32 = true;
+ } else {
+ cqe = &r->cqes[(cq_head & cq_mask)];
+ /*
+ * Userspace can manipulate the last cqarray slot to
+ * have IORING_CQE_F_32 set, which would cause the second
+ * half of that CQE to be read out of bounds.
+ * Ignore the flag for the last cqarray slot, which can't
+ * legitimately have the flag set.
+ */
+ two_entries = (cqe->flags & IORING_CQE_F_32) &&
+ !is_last_cqarray_slot;
+ cqe32 = two_entries;
+ }
seq_printf(m, "%5u: user_data:%llu, res:%d, flags:%x",
cq_head & cq_mask, cqe->user_data, cqe->res,
cqe->flags);
@@ -176,7 +182,7 @@ static void __io_uring_show_fdinfo(struct io_ring_ctx *ctx, struct seq_file *m)
cqe->big_cqe[0], cqe->big_cqe[1]);
seq_printf(m, "\n");
cq_head++;
- if (cqe32) {
+ if (two_entries) {
cq_head++;
i++;
}
diff --git a/io_uring/futex.c b/io_uring/futex.c
index eaee14242a3a3..9203998654ffb 100644
--- a/io_uring/futex.c
+++ b/io_uring/futex.c
@@ -152,6 +152,24 @@ int io_futex_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
return 0;
}
+/*
+ * A wait on a private futex does not block the submitter. Even a single
+ * threaded task can go on and have the mm private hash installed by clone()
+ * or prctl(), which does not pick up waiters from the global hash. Make
+ * sure the private hash exists before the first waiter is queued.
+ */
+static int io_futex_prep_private(struct io_kiocb *req)
+{
+ int ret = futex_hash_allocate_default();
+
+ if (unlikely(ret))
+ return ret;
+
+ /* inflight tracking only needed for mm private hash */
+ io_req_track_inflight(req);
+ return 0;
+}
+
int io_futex_wait_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
{
struct io_futex *iof = io_kiocb_to_cmd(req, struct io_futex);
@@ -161,9 +179,8 @@ int io_futex_wait_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
if (unlikely(ret))
return ret;
- /* inflight tracking only needed for mm private hash */
if (!(iof->futex_flags & FLAGS_SHARED))
- io_req_track_inflight(req);
+ return io_futex_prep_private(req);
return 0;
}
@@ -213,10 +230,13 @@ int io_futexv_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
return ret;
}
- /* inflight tracking only needed for mm private hash */
for (i = 0; i < iof->futex_nr; i++) {
if (!(ifd->futexv[i].w.flags & FLAGS_SHARED)) {
- io_req_track_inflight(req);
+ ret = io_futex_prep_private(req);
+ if (ret) {
+ kfree(ifd);
+ return ret;
+ }
break;
}
}
@@ -283,6 +303,8 @@ int io_futexv_wait(struct io_kiocb *req, unsigned int issue_flags)
*/
__set_current_state(TASK_RUNNING);
hlist_add_head(&req->hash_node, &ctx->futex_list);
+ if (io_wq_cancel_seen(req, issue_flags))
+ __io_futex_cancel(req);
} else {
iof->futexv_unqueued = 1;
if (woken != -1)
@@ -323,6 +345,8 @@ int io_futex_wait(struct io_kiocb *req, unsigned int issue_flags)
&ifd->q, NULL, NULL);
if (!ret) {
hlist_add_head(&req->hash_node, &ctx->futex_list);
+ if (io_wq_cancel_seen(req, issue_flags))
+ __io_futex_cancel(req);
io_ring_submit_unlock(ctx, issue_flags);
return IOU_ISSUE_SKIP_COMPLETE;
diff --git a/io_uring/io-wq.c b/io_uring/io-wq.c
index 97523ccc7a714..dfdc78612783a 100644
--- a/io_uring/io-wq.c
+++ b/io_uring/io-wq.c
@@ -56,6 +56,8 @@ struct io_worker {
struct io_wq_acct *acct;
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;
struct completion ref_done;
@@ -467,11 +469,9 @@ static void io_wq_dec_running(struct io_worker *worker)
if (!atomic_dec_and_test(&acct->nr_running))
return;
- if (!worker->cur_work)
- return;
if (!io_acct_run_queue(acct))
return;
- if (io_wq_hash_defer(worker->cur_work, acct)) {
+ if (worker->cur_work && io_wq_hash_defer(worker->cur_work, acct)) {
raw_spin_unlock(&acct->lock);
return;
}
@@ -583,6 +583,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)
{
@@ -591,9 +602,7 @@ static void io_assign_current_work(struct io_worker *worker,
cond_resched();
}
- raw_spin_lock(&worker->lock);
- worker->cur_work = work;
- raw_spin_unlock(&worker->lock);
+ __io_assign_current_work(worker, work);
}
/*
@@ -625,9 +634,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->cur_work = work;
- raw_spin_unlock(&worker->lock);
+ __io_assign_current_work(worker, work);
}
raw_spin_unlock(&acct->lock);
@@ -646,25 +653,32 @@ 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 work_flags = atomic_read(&work->flags);
unsigned int hash = __io_wq_is_hashed(work_flags)
? __io_get_work_hash(work_flags)
: -1U;
struct io_kiocb *req;
+ bool last;
next_hashed = wq_next_work(work);
- if (do_kill &&
- (work_flags & IO_WQ_WORK_UNBOUND))
+ /*
+ * 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 (do_kill && (work_flags & IO_WQ_WORK_UNBOUND))
atomic_or(IO_WQ_WORK_CANCEL, &work->flags);
+ else if (do_kill)
+ set_notify_signal(current);
req = container_of(work, struct io_kiocb, work);
kcov_remote_start_common(req->ctx->kcov_handle);
io_wq_submit_work(work);
kcov_remote_stop();
io_assign_current_work(worker, NULL);
- linked = io_wq_free_work(work);
+ last = io_wq_put_work(work, &linked);
work = next_hashed;
if (!work && linked && !io_wq_is_hashed(linked)) {
work = linked;
@@ -673,6 +687,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)
+ io_wq_free_work(done);
if (hash != -1U && !next_hashed) {
/* serialize hash clear with wake_up() */
@@ -691,6 +712,35 @@ 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_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(&acct->workers_lock);
+ if (acct->nr_workers >= acct->max_workers) {
+ /* a worker was created in our place, the work is its business */
+ raw_spin_unlock(&acct->workers_lock);
+ return true;
+ }
+ acct->nr_workers++;
+ raw_spin_unlock(&acct->workers_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;
@@ -724,19 +774,41 @@ static int io_wq_worker(void *data)
* idle-exit, drop the worker as well. This is used to avoid
* keeping io-wq workers around for tasks that no longer have
* any active io_uring instances.
+ * 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)) ||
- test_bit(IO_WQ_BIT_EXIT_ON_IDLE, &wq->state)) {
+ if (((last_timeout && (exit_mask || acct->nr_workers > 1)) ||
+ test_bit(IO_WQ_BIT_EXIT_ON_IDLE, &wq->state)) &&
+ !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);
raw_spin_unlock(&acct->workers_lock);
__set_current_state(TASK_RUNNING);
- break;
+ if (io_worker_retire(acct, worker))
+ break;
+ last_timeout = false;
+ continue;
}
last_timeout = false;
__io_worker_idle(acct, worker);
raw_spin_unlock(&acct->workers_lock);
if (io_run_task_work())
continue;
+ /*
+ * io_wq_enqueue() leaves new work to us for as long as we are
+ * accounted as running. Get out of ->nr_running before taking
+ * the last look at the work list, rather than when scheduling
+ * out. No need to ask for another worker then, we can do it.
+ */
+ clear_bit(IO_WORKER_F_RUNNING, &worker->flags);
+ atomic_dec(&acct->nr_running);
+ if (io_acct_run_queue(acct)) {
+ io_wq_worker_running(current);
+ io_worker_handle_work(acct, worker);
+ continue;
+ }
ret = schedule_timeout(WORKER_IDLE_TIMEOUT);
if (signal_pending(current)) {
struct ksignal ksig;
@@ -1002,9 +1074,13 @@ void io_wq_set_exit_on_idle(struct io_wq *wq, bool enable)
static void io_run_cancel(struct io_wq_work *work, struct io_wq *wq)
{
do {
+ struct io_wq_work *linked;
+
atomic_or(IO_WQ_WORK_CANCEL, &work->flags);
io_wq_submit_work(work);
- work = io_wq_free_work(work);
+ if (io_wq_put_work(work, &linked))
+ io_wq_free_work(work);
+ work = linked;
} while (work);
}
@@ -1112,6 +1188,7 @@ 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
@@ -1120,6 +1197,19 @@ static bool io_wq_worker_cancel(struct io_worker *worker, void *data)
raw_spin_lock(&worker->lock);
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)) {
+ atomic_or(IO_WQ_WORK_CANCEL, &work->flags);
+ match->nr_running++;
+ }
+ }
raw_spin_unlock(&worker->lock);
return match->nr_running && !match->cancel_all;
diff --git a/io_uring/io_uring.c b/io_uring/io_uring.c
index 0c91d466d8259..e6569c2049172 100644
--- a/io_uring/io_uring.c
+++ b/io_uring/io_uring.c
@@ -407,17 +407,46 @@ static void io_prep_async_link(struct io_kiocb *req)
}
}
+/* Called for every request that is issued: keep it inline */
+static __always_inline bool io_assign_file(struct io_kiocb *req,
+ const struct io_issue_def *def,
+ unsigned int issue_flags)
+{
+ if (req->file || !def->needs_file)
+ return true;
+
+ if (req->flags & REQ_F_FIXED_FILE)
+ req->file = io_file_get_fixed(req, req->cqe.fd, issue_flags);
+ else
+ req->file = io_file_get_normal(req, req->cqe.fd);
+
+ return !!req->file;
+}
+
void io_queue_iowq(struct io_kiocb *req)
{
struct io_uring_task *tctx = req->tctx;
BUG_ON(!tctx);
+ lockdep_assert_held(&req->ctx->uring_lock);
if ((current->flags & PF_KTHREAD) || !tctx->io_wq) {
io_req_task_queue_fail(req, -ECANCELED);
return;
}
+ /*
+ * The file is looked up as late as possible, as it may be the result
+ * of an earlier request of the link. This one is up now, and what kind
+ * of work it is depends on the file. If that fails, leave the error
+ * handling to the worker.
+ *
+ * Not for a splice: whatever it writes to, it may wait for its input
+ * for ever. It has to remain unbound work that isn't hashed.
+ */
+ if (req->opcode != IORING_OP_SPLICE)
+ io_assign_file(req, &io_issue_defs[req->opcode], 0);
+
/* init ->work of the whole link before punting */
io_prep_async_link(req);
@@ -729,6 +758,13 @@ bool io_cqe_cache_refill(struct io_ring_ctx *ctx, bool overflow, bool cqe32)
if (!overflow && (ctx->check_cq & BIT(IO_CHECK_CQ_OVERFLOW_BIT)))
return false;
+ /*
+ * A single entry may be left if a 32b CQE is asked for. It must not be
+ * handed out later on if this fails, as the CQE is going to overflow.
+ */
+ ctx->cqe_cached = NULL;
+ ctx->cqe_sentinel = NULL;
+
/*
* Post dummy CQE if a 32b CQE is needed and there's only room for a
* 16b CQE before the ring wraps.
@@ -1390,20 +1426,6 @@ static __cold void io_drain_req(struct io_kiocb *req)
ctx->int_flags &= ~IO_RING_F_DRAIN_ACTIVE;
}
-static bool io_assign_file(struct io_kiocb *req, const struct io_issue_def *def,
- unsigned int issue_flags)
-{
- if (req->file || !def->needs_file)
- return true;
-
- if (req->flags & REQ_F_FIXED_FILE)
- req->file = io_file_get_fixed(req, req->cqe.fd, issue_flags);
- else
- req->file = io_file_get_normal(req, req->cqe.fd);
-
- return !!req->file;
-}
-
#define REQ_ISSUE_SLOW_FLAGS (REQ_F_CREDS | REQ_F_ARM_LTIMEOUT)
static inline int __io_issue_sqe(struct io_kiocb *req,
@@ -1450,6 +1472,9 @@ static int io_issue_sqe(struct io_kiocb *req, unsigned int issue_flags)
ret = __io_issue_sqe(req, issue_flags, def);
if (ret == IOU_COMPLETE) {
+ /* 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
@@ -1464,6 +1489,8 @@ static int io_issue_sqe(struct io_kiocb *req, unsigned int issue_flags)
if (req->flags & REQ_F_IOPOLL)
io_iopoll_req_issued(req, issue_flags);
}
+ if (unlikely(ret <= -ERESTARTSYS))
+ ret = io_fixup_restart_res(ret);
return ret;
}
@@ -1486,28 +1513,40 @@ int io_poll_issue(struct io_kiocb *req, io_tw_token_t tw)
return ret;
}
-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)) {
- /*
- * Only continue the link from here if the completion has been
- * posted. If it's still pending, io_free_req() completes the
- * request and the next link follows from there, after the CQE.
- */
- if ((req->flags & IO_REQ_LINK_FLAGS) &&
- (req->flags & REQ_F_CQE_SKIP)) {
- struct io_ring_ctx *ctx = req->ctx;
+ *linked = NULL;
+ if (!req_ref_put_and_test_atomic(req))
+ return false;
- mutex_lock(&ctx->uring_lock);
- nxt = io_req_find_next(req);
- mutex_unlock(&ctx->uring_lock);
- }
- io_free_req(req);
+ /*
+ * Only continue the link from here if the completion has been posted.
+ * If it's still pending, io_free_req() completes the request and the
+ * next link follows from there, after the CQE.
+ */
+ if ((req->flags & IO_REQ_LINK_FLAGS) && (req->flags & REQ_F_CQE_SKIP)) {
+ struct io_ring_ctx *ctx = req->ctx;
+
+ mutex_lock(&ctx->uring_lock);
+ nxt = io_req_find_next(req);
+ mutex_unlock(&ctx->uring_lock);
}
- return nxt ? &nxt->work : NULL;
+ 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)
@@ -1518,7 +1557,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
@@ -1799,17 +1838,27 @@ static int io_init_req(struct io_ring_ctx *ctx, struct io_kiocb *req,
* A 128b op on a non-128b SQ requires mixed SQE support as
* well as 2 contiguous entries.
*/
- if (!(ctx->flags & IORING_SETUP_SQE_MIXED) || *left < 2 ||
+ if (!(ctx->flags & IORING_SETUP_SQE_MIXED) ||
(unsigned)(sqe - ctx->sq_sqes) >= ctx->sq_entries - 1)
return io_init_fail_req(req, -EINVAL);
+ if (*left >= 2) {
+ /*
+ * A 128b operation on a mixed SQ uses two entries, so
+ * we have to increment the head and cached refs, and
+ * decrement what's left.
+ */
+ current->io_uring->cached_refs++;
+ (*left)--;
+ } else if (!(ctx->flags & IORING_SETUP_SQPOLL) ||
+ !__io_sqring_entries(ctx)) {
+ return io_init_fail_req(req, -EINVAL);
+ }
/*
- * A 128b operation on a mixed SQ uses two entries, so we have
- * to increment the head and cached refs, and decrement what's
- * left.
+ * else: the SQPOLL thread caps the number of entries it submits
+ * in one go, which is none of the application's business. Take
+ * the 2nd entry on top.
*/
- current->io_uring->cached_refs++;
ctx->cached_sq_head++;
- (*left)--;
}
if (unlikely(sqe_flags & ~SQE_COMMON_FLAGS)) {
@@ -2336,6 +2385,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)
@@ -2350,7 +2400,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);
}
@@ -2518,6 +2569,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_lock(&ctx->tctx_lock);
}
diff --git a/io_uring/io_uring.h b/io_uring/io_uring.h
index 109261722ca74..203bf019e9319 100644
--- a/io_uring/io_uring.h
+++ b/io_uring/io_uring.h
@@ -204,7 +204,8 @@ int io_do_iopoll(struct io_ring_ctx *ctx, bool force_nonspin);
__cold void io_iopoll_try_reap_events(struct io_ring_ctx *ctx);
void __io_submit_flush_completions(struct io_ring_ctx *ctx);
-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);
@@ -318,6 +319,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 *io_uring_alloc_async_data(struct io_alloc_cache *cache,
struct io_kiocb *req)
{
@@ -380,6 +399,23 @@ 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
+ * ->uring_lock held, which the search needs as well, once the request is on
+ * the list: if the search came first, the flag is visible.
+ */
+static inline bool io_wq_cancel_seen(struct io_kiocb *req,
+ unsigned int issue_flags)
+{
+ lockdep_assert_held(&req->ctx->uring_lock);
+
+ return (issue_flags & IO_URING_F_IOWQ) &&
+ (atomic_read(&req->work.flags) & IO_WQ_WORK_CANCEL);
+}
+
static inline void io_commit_cqring(struct io_ring_ctx *ctx)
{
/* order cqe stores with ring update */
diff --git a/io_uring/kbuf.c b/io_uring/kbuf.c
index 6e2facf34bfa6..c9641b6ccc8ff 100644
--- a/io_uring/kbuf.c
+++ b/io_uring/kbuf.c
@@ -204,7 +204,7 @@ static struct io_br_sel io_ring_buffer_select(struct io_kiocb *req, size_t *len,
if (unlikely(tail == head))
return sel;
- if (head + 1 == tail)
+ if ((__u16)(head + 1) == tail)
req->flags |= REQ_F_BL_EMPTY;
buf = io_ring_head_to_buf(br, head, bl->mask);
diff --git a/io_uring/loop.c b/io_uring/loop.c
index 3d472b7827fac..2340459164a1a 100644
--- a/io_uring/loop.c
+++ b/io_uring/loop.c
@@ -10,12 +10,6 @@ static inline int io_loop_nr_cqes(const struct io_ring_ctx *ctx,
return lp->cq_wait_idx - READ_ONCE(ctx->rings->cq.tail);
}
-static inline void io_loop_wait_start(struct io_ring_ctx *ctx, unsigned nr_wait)
-{
- atomic_set(&ctx->cq_wait_nr, nr_wait);
- set_current_state(TASK_INTERRUPTIBLE);
-}
-
static inline void io_loop_wait_finish(struct io_ring_ctx *ctx)
{
__set_current_state(TASK_RUNNING);
@@ -25,7 +19,7 @@ static inline void io_loop_wait_finish(struct io_ring_ctx *ctx)
static void io_loop_wait(struct io_ring_ctx *ctx, struct iou_loop_params *lp,
unsigned nr_wait)
{
- io_loop_wait_start(ctx, nr_wait);
+ io_cq_wait_arm(ctx, nr_wait);
if (unlikely(io_local_work_pending(ctx) ||
io_loop_nr_cqes(ctx, lp) <= 0) ||
diff --git a/io_uring/mpscq.h b/io_uring/mpscq.h
index f910526766fd8..da9474497cc8e 100644
--- a/io_uring/mpscq.h
+++ b/io_uring/mpscq.h
@@ -25,9 +25,10 @@
* its predecessor. In between, the tail end of the queue is not yet reachable
* from the head. mpscq_pop() detects this and returns NULL, while mpscq_empty()
* reports false. The consumer must not treat such a NULL as "queue empty" - it
- * should retry later. The window is two instructions wide, but a producer can
- * be preempted inside it, so the consumer must not spin on it while holding
- * resources the producer might need to make progress.
+ * should retry later. The window is two instructions wide and the producer
+ * can't be preempted inside it, but it can be interrupted, so the consumer must
+ * not spin on it while holding resources the producer might need to make
+ * progress.
*
* The consumer side only supports a single consumer at a time, callers must
* provide their own serialization for it. The stub node is what allows the
@@ -69,6 +70,12 @@ static inline bool mpscq_push(struct mpscq *q, struct llist_node *node)
struct llist_node *prev;
node->next = NULL;
+ /*
+ * All the consumer can do about a node that isn't linked yet is to try
+ * again. If it is what preempted us, and we don't get to run as long
+ * as it does, that never ends.
+ */
+ preempt_disable();
/*
* xchg() implies a full barrier, so the initialization of the
* entry (including ->next above) is visible before the node can
@@ -77,6 +84,7 @@ static inline bool mpscq_push(struct mpscq *q, struct llist_node *node)
*/
prev = xchg(&q->tail, node);
WRITE_ONCE(prev->next, node);
+ preempt_enable();
return prev == &q->stub;
}
diff --git a/io_uring/napi.c b/io_uring/napi.c
index ca1c814fa73a2..7e698720cd51a 100644
--- a/io_uring/napi.c
+++ b/io_uring/napi.c
@@ -6,7 +6,7 @@
#ifdef CONFIG_NET_RX_BUSY_POLL
/* Timeout for cleanout of stale entries. */
-#define NAPI_TIMEOUT (60 * SEC_CONVERSION)
+#define NAPI_TIMEOUT (60 * HZ)
struct io_napi_entry {
unsigned int napi_id;
@@ -378,6 +378,8 @@ void __io_napi_busy_loop(struct io_ring_ctx *ctx, struct io_wait_queue *iowq)
if (iowq->timeout != KTIME_MAX) {
ktime_t dt = ktime_sub(iowq->timeout, io_get_time(ctx));
+ /* an absolute timeout may have passed already */
+ dt = max_t(ktime_t, dt, 0);
iowq->napi_busy_poll_dt = min_t(u64, iowq->napi_busy_poll_dt, dt);
}
diff --git a/io_uring/net.c b/io_uring/net.c
index f5f4f3ce6ef35..b1bf7dbec5d27 100644
--- a/io_uring/net.c
+++ b/io_uring/net.c
@@ -480,6 +480,24 @@ static int io_bundle_nbufs(struct io_async_msghdr *kmsg, int ret)
return nbufs;
}
+/*
+ * A receive that is retried after a partial transfer keeps writing to what it
+ * has mapped. io_net_kbuf_recyle() only commits what was filled so far, which
+ * takes a buffer out of circulation if it's a single one that is consumed as a
+ * whole. The tail of an incrementally consumed buffer, or the yet unused
+ * buffers of a bundle, would be up for grabs. They can't be committed in
+ * advance either, the CQE has no way to tell if they end up not being used.
+ */
+static bool io_net_kbuf_can_retry(struct io_kiocb *req,
+ struct io_buffer_list *bl,
+ struct io_async_msghdr *kmsg)
+{
+ /* not ring provided, or committed at selection time */
+ if (!(req->flags & REQ_F_BUFFERS_COMMIT))
+ return true;
+ return !(bl->flags & IOBL_INC) && iter_is_ubuf(&kmsg->msg.msg_iter);
+}
+
static int io_net_kbuf_recyle(struct io_kiocb *req, struct io_buffer_list *bl,
struct io_async_msghdr *kmsg, int len)
{
@@ -1091,7 +1109,8 @@ int io_recvmsg(struct io_kiocb *req, unsigned int issue_flags)
io_kbuf_recycle(req, sel.buf_list, issue_flags);
return IOU_RETRY;
}
- if (ret > 0 && io_net_retry(sock, flags)) {
+ if (ret > 0 && io_net_retry(sock, flags) &&
+ io_net_kbuf_can_retry(req, sel.buf_list, kmsg)) {
sr->done_io += ret;
return io_net_kbuf_recyle(req, sel.buf_list, kmsg, ret);
}
@@ -1252,7 +1271,8 @@ int io_recv(struct io_kiocb *req, unsigned int issue_flags)
io_kbuf_recycle(req, sel.buf_list, issue_flags);
return IOU_RETRY;
}
- if (ret > 0 && io_net_retry(sock, flags)) {
+ if (ret > 0 && io_net_retry(sock, flags) &&
+ io_net_kbuf_can_retry(req, sel.buf_list, kmsg)) {
sr->len -= ret;
sr->buf += ret;
sr->done_io += ret;
diff --git a/io_uring/openclose.c b/io_uring/openclose.c
index c71242915dade..24cf3ce2e2bc3 100644
--- a/io_uring/openclose.c
+++ b/io_uring/openclose.c
@@ -375,13 +375,16 @@ static int io_pipe_fixed(struct io_kiocb *req, struct file **files,
fds[1] = alloc_slot ? ret : slot - 1;
files[1] = NULL;
- io_ring_submit_unlock(ctx, issue_flags);
-
- if (!copy_to_user(p->fds, fds, sizeof(fds)))
+ /*
+ * Hang on to the lock. If this fails, the slots have to be cleared, and
+ * they must still hold what was put there.
+ */
+ if (!copy_to_user(p->fds, fds, sizeof(fds))) {
+ io_ring_submit_unlock(ctx, issue_flags);
return 0;
+ }
ret = -EFAULT;
- io_ring_submit_lock(ctx, issue_flags);
err:
if (fds[0] != -1)
io_fixed_fd_remove(ctx, fds[0]);
diff --git a/io_uring/poll.c b/io_uring/poll.c
index 5447a7c24dce6..7dcb904e8fc3e 100644
--- a/io_uring/poll.c
+++ b/io_uring/poll.c
@@ -617,6 +617,18 @@ static int __io_arm_poll_handler(struct io_kiocb *req,
io_poll_add_hash(req, issue_flags);
+ /*
+ * 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 ->uring_lock held, and
+ * the flag is set before the search. If the search came first,
+ * the flag is visible by now.
+ */
+ if (ipt->owning &&
+ (atomic_read(&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);
@@ -906,6 +918,15 @@ int io_poll_add(struct io_kiocb *req, unsigned int issue_flags)
ipt.pt._qproc = io_poll_queue_proc;
+ /*
+ * 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))
+ io_req_async_data_free(req);
+ 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);
diff --git a/io_uring/register.c b/io_uring/register.c
index dab3eb4258dff..2ce36623e25a6 100644
--- a/io_uring/register.c
+++ b/io_uring/register.c
@@ -496,6 +496,17 @@ static void io_register_free_rings(struct io_ring_ctx *ctx,
IORING_SETUP_CQE32 | IORING_SETUP_NO_MMAP | \
IORING_SETUP_CQE_MIXED | IORING_SETUP_SQE_MIXED)
+static bool io_sqe_is_128(struct io_ring_ctx *ctx,
+ const struct io_uring_sqe *sqe)
+{
+ u8 opcode = READ_ONCE(sqe->opcode);
+
+ if (!(ctx->flags & IORING_SETUP_SQE_MIXED) || opcode >= IORING_OP_LAST)
+ return false;
+ opcode = array_index_nospec(opcode, IORING_OP_LAST);
+ return io_issue_defs[opcode].is_128;
+}
+
static int io_register_resize_rings(struct io_ring_ctx *ctx, void __user *arg)
{
struct io_ctx_config config;
@@ -505,6 +516,8 @@ static int io_register_resize_rings(struct io_ring_ctx *ctx, void __user *arg)
struct io_uring_params *p = &config.p;
struct io_rings_layout *rl = &config.layout;
u32 *o_sq_array, *n_sq_array = NULL;
+ bool cqe32_tail = false;
+ unsigned int sqe128_src = 0;
int ret;
memset(&config, 0, sizeof(config));
@@ -603,13 +616,20 @@ static int io_register_resize_rings(struct io_ring_ctx *ctx, void __user *arg)
old_head = READ_ONCE(o.rings->sq.head);
if (tail - old_head > p->sq_entries)
goto overflow;
- for (i = old_head; i < tail; i++) {
+ for (i = old_head; i != tail; i++) {
+ bool sqe128_tail = sqe128_src;
unsigned int dst, src;
size_t sq_size;
dst = i & (p->sq_entries - 1);
src = i & (ctx->sq_entries - 1);
- if (n_sq_array) {
+ if (sqe128_tail) {
+ /* it follows the first half, whatever the array says */
+ src = sqe128_src;
+ sqe128_src = 0;
+ if (n_sq_array)
+ WRITE_ONCE(n_sq_array[dst], dst);
+ } else if (n_sq_array) {
src = READ_ONCE(o_sq_array[src]);
if (unlikely(src >= ctx->sq_entries)) {
WRITE_ONCE(n_sq_array[dst], UINT_MAX);
@@ -618,6 +638,19 @@ static int io_register_resize_rings(struct io_ring_ctx *ctx, void __user *arg)
WRITE_ONCE(n_sq_array[dst], dst);
}
+ /*
+ * A 128b SQE on a SQE_MIXED ring takes two contiguous entries
+ * and must not wrap. A smaller ring may place it on the last
+ * entry, fail the resize if that happens.
+ */
+ if (!sqe128_tail && i + 1 != tail &&
+ src < ctx->sq_entries - 1 &&
+ io_sqe_is_128(ctx, &o.sq_sqes[src])) {
+ if (dst == p->sq_entries - 1)
+ goto overflow;
+ sqe128_src = src + 1;
+ }
+
sq_size = sizeof(struct io_uring_sqe);
if (ctx->flags & IORING_SETUP_SQE128) {
dst <<= 1;
@@ -640,7 +673,7 @@ static int io_register_resize_rings(struct io_ring_ctx *ctx, void __user *arg)
ret = -EOVERFLOW;
goto out;
}
- for (i = old_head; i < tail; i++) {
+ for (i = old_head; i != tail; i++) {
unsigned index, dst_mask, src_mask;
size_t cq_size;
@@ -654,6 +687,21 @@ static int io_register_resize_rings(struct io_ring_ctx *ctx, void __user *arg)
src_mask = (ctx->cq_entries << 1) - 1;
dst_mask = (p->cq_entries << 1) - 1;
}
+ /*
+ * A 32b CQE on a CQE_MIXED ring takes two contiguous entries
+ * and must not wrap. A smaller ring may place it on the last
+ * entry, fail the resize if that happens.
+ */
+ if (ctx->flags & IORING_SETUP_CQE_MIXED) {
+ if (cqe32_tail) {
+ cqe32_tail = false;
+ } else if (READ_ONCE(o.rings->cqes[index & src_mask].flags) &
+ IORING_CQE_F_32) {
+ if ((index & dst_mask) == dst_mask)
+ goto overflow;
+ cqe32_tail = true;
+ }
+ }
memcpy(&n.rings->cqes[index & dst_mask], &o.rings->cqes[index & src_mask], cq_size);
}
WRITE_ONCE(n.rings->cq.head, old_head);
diff --git a/io_uring/rw.c b/io_uring/rw.c
index d33b925a848e5..a65693e393695 100644
--- a/io_uring/rw.c
+++ b/io_uring/rw.c
@@ -613,24 +613,6 @@ static void io_complete_rw_iopoll(struct kiocb *kiocb, long res)
smp_store_release(&req->iopoll_completed, 1);
}
-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 io_kiocb *req, ssize_t ret)
{
struct io_rw *rw = io_kiocb_to_cmd(req, struct io_rw);
diff --git a/io_uring/sqpoll.c b/io_uring/sqpoll.c
index ad42e8eb10029..ee97808a065a9 100644
--- a/io_uring/sqpoll.c
+++ b/io_uring/sqpoll.c
@@ -385,6 +385,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;
diff --git a/io_uring/timeout.c b/io_uring/timeout.c
index 9c239c0a17155..b219dfdf7af5a 100644
--- a/io_uring/timeout.c
+++ b/io_uring/timeout.c
@@ -321,22 +321,29 @@ static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer)
return HRTIMER_NORESTART;
}
-static struct io_kiocb *io_timeout_extract(struct io_ring_ctx *ctx,
- struct io_cancel_data *cd)
+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;
- 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);
+ struct io_kiocb *req = cmd_to_io_kiocb(timeout);
- if (io_cancel_req_match(tmp, cd)) {
- req = tmp;
- break;
- }
+ 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;
+
if (!req)
return ERR_PTR(-ENOENT);
@@ -386,6 +393,9 @@ static void io_req_task_link_timeout(struct io_tw_req tw_req, io_tw_token_t tw)
};
ret = io_try_cancel(req->tctx, &cd, 0);
+ /* a number of requests for futex and waitid */
+ if (ret > 0)
+ ret = 0;
} else {
ret = -ECANCELED;
}
@@ -458,6 +468,8 @@ static int io_linked_timeout_update(struct io_ring_ctx *ctx, __u64 user_data,
io = req->async_data;
if (hrtimer_try_to_cancel(&io->timer) == -1)
return -EALREADY;
+ if (mode == HRTIMER_MODE_ABS)
+ ts = timens_ktime_to_host(io_timeout_get_clock(io), ts);
hrtimer_setup(&io->timer, io_link_timeout_fn, io_timeout_get_clock(io), mode);
hrtimer_start(&io->timer, ts, mode);
return 0;
@@ -468,15 +480,26 @@ 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;
- timeout->off = 0; /* noseq */
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 */
+ if (mode == HRTIMER_MODE_ABS)
+ time = timens_ktime_to_host(io_timeout_get_clock(data), time);
data->time = time;
list_add_tail(&timeout->list, &ctx->timeout_list);
@@ -509,7 +532,12 @@ int io_timeout_remove_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
IORING_TIMEOUT_ABS |
IORING_TIMEOUT_IMMEDIATE_ARG))
return -EINVAL;
- ret = io_parse_user_time(&tr->time, READ_ONCE(sqe->addr2), tr->flags);
+ /*
+ * The clock is that of the timeout to update, an absolute time
+ * is converted to the host's view once that has been found.
+ */
+ ret = io_parse_user_time(&tr->time, READ_ONCE(sqe->addr2),
+ tr->flags & ~IORING_TIMEOUT_ABS);
if (ret)
return ret;
} else if (tr->flags) {
@@ -645,8 +673,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;
raw_spin_lock_irq(&ctx->timeout_lock);
@@ -663,11 +692,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
@@ -679,8 +714,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:
diff --git a/io_uring/tw.c b/io_uring/tw.c
index f9affd68dd263..0b5066f972245 100644
--- a/io_uring/tw.c
+++ b/io_uring/tw.c
@@ -148,6 +148,37 @@ static void io_ctx_mark_taskrun(struct io_ring_ctx *ctx)
}
}
+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.
+ */
+static void io_local_work_wake_poll(struct io_ring_ctx *ctx, unsigned int flags)
+{
+ if (!(flags & IOU_F_TWQ_IN_WAKE)) {
+ io_poll_wq_wake(ctx);
+ return;
+ }
+ /*
+ * Called from a wake up handler with some wait queue lock held, this
+ * one could lead back to it. Do it from a clean context.
+ */
+ 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);
+}
+
void io_req_local_work_add(struct io_kiocb *req, unsigned flags)
{
struct io_ring_ctx *ctx = req->ctx;
@@ -165,7 +196,7 @@ void io_req_local_work_add(struct io_kiocb *req, unsigned flags)
/*
* The xchg() in mpscq_push() implies a full barrier, which pairs with
- * the barrier in set_current_state() on the io_cqring_wait() side. This
+ * the barrier in io_cq_wait_arm() on the io_cqring_wait() side. This
* ensures that either we see the updated ->cq_wait_nr, or waiters going
* to sleep will observe the work added to the list, which is similar to
* the wait/wake task state sync.
@@ -174,6 +205,9 @@ void io_req_local_work_add(struct io_kiocb *req, unsigned flags)
io_ctx_mark_taskrun(ctx);
if (data_race(ctx->int_flags) & IO_RING_F_HAS_EVFD)
io_eventfd_signal(ctx, false, flags & IOU_F_TWQ_IN_WAKE);
+ /* whoever sets the flag wakes ->poll_wq afterwards */
+ if (data_race(ctx->int_flags) & IO_RING_F_POLL_ACTIVATED)
+ io_local_work_wake_poll(ctx, flags);
}
/*
diff --git a/io_uring/uring_cmd.c b/io_uring/uring_cmd.c
index 73b606899833f..b5486b56ee0c7 100644
--- a/io_uring/uring_cmd.c
+++ b/io_uring/uring_cmd.c
@@ -87,7 +87,7 @@ static void io_uring_cmd_del_cancelable(struct io_uring_cmd *cmd,
cmd->flags &= ~IORING_URING_CMD_CANCELABLE;
io_ring_submit_lock(ctx, issue_flags);
- hlist_del(&req->hash_node);
+ hlist_del_init(&req->hash_node);
io_ring_submit_unlock(ctx, issue_flags);
}
diff --git a/io_uring/wait.c b/io_uring/wait.c
index a48523c2f356b..b7a0292767c7c 100644
--- a/io_uring/wait.c
+++ b/io_uring/wait.c
@@ -26,7 +26,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)
@@ -76,9 +77,16 @@ static enum hrtimer_restart io_cqring_min_timer_wakeup(struct hrtimer *timer)
if (iowq->timeout == KTIME_MAX ||
ktime_compare(iowq->min_timeout, iowq->timeout) >= 0)
goto out_wake;
- /* work we may need to run, wake function will see if we need to wake */
- if (io_has_work(ctx))
- goto out_wake;
+ /*
+ * Work we need to run. It isn't known if that is going to post a CQE,
+ * so this isn't a timeout. Carry on with the normal one if it doesn't.
+ */
+ if (io_has_work(ctx)) {
+ iowq->min_timeout = 0;
+ iowq->cq_tail = iowq->cq_min_tail + 1;
+ wake_up_process(iowq->wq.private);
+ return HRTIMER_NORESTART;
+ }
/* got events since we started waiting, min timeout is done */
scoped_guard(rcu) {
struct io_rings *rings = io_get_rings(ctx);
@@ -257,8 +265,7 @@ int io_cqring_wait(struct io_ring_ctx *ctx, int min_events, u32 flags,
unsigned long check_cq;
if (ctx->flags & IORING_SETUP_DEFER_TASKRUN) {
- atomic_set(&ctx->cq_wait_nr, nr_wait);
- set_current_state(TASK_INTERRUPTIBLE);
+ io_cq_wait_arm(ctx, nr_wait);
} else {
prepare_to_wait_exclusive(&ctx->cq_wait, &iowq.wq,
TASK_INTERRUPTIBLE);
@@ -291,9 +298,18 @@ int io_cqring_wait(struct io_ring_ctx *ctx, int min_events, u32 flags,
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;
diff --git a/io_uring/wait.h b/io_uring/wait.h
index b7b9c46b1b013..eb5ae8a6fc942 100644
--- a/io_uring/wait.h
+++ b/io_uring/wait.h
@@ -14,6 +14,21 @@
*/
#define IO_CQ_WAKE_INIT (-1)
+/*
+ * Start a wait cycle of the current task. The add that uses up ->cq_wait_nr
+ * may be one that queued its entry long ago and only now gets to look at the
+ * count. As nobody else wakes us after that, it has to find us sleeping, hence
+ * the task state must be visible before the count is. The barrier pairs with
+ * the xchg() in mpscq_push(), see io_req_local_work_add().
+ */
+static inline void io_cq_wait_arm(struct io_ring_ctx *ctx, int nr_wait)
+{
+ __set_current_state(TASK_INTERRUPTIBLE);
+ atomic_set_release(&ctx->cq_wait_nr, nr_wait);
+ /* order the above against looking at the work list */
+ smp_mb();
+}
+
struct ext_arg {
size_t argsz;
struct timespec64 ts;
diff --git a/io_uring/waitid.c b/io_uring/waitid.c
index 2b756017423f0..2ba2369681d0e 100644
--- a/io_uring/waitid.c
+++ b/io_uring/waitid.c
@@ -265,12 +265,18 @@ static int io_waitid_wait(struct wait_queue_entry *wait, unsigned mode,
return 0;
list_del_init(&wait->entry);
- smp_store_release(&iw->head, NULL);
- /* cancel is in progress */
+ /*
+ * Cancel or issue is in progress. Leave ->head alone, so that whoever
+ * owns the request has to take the wait queue lock, which we hold,
+ * before it can complete and free it.
+ */
if (atomic_fetch_inc(&iw->refs) & IO_WAITID_REF_MASK)
return 1;
+ /* pairs with the smp_load_acquire() in io_waitid_remove_wq() */
+ smp_store_release(&iw->head, NULL);
+
req->io_task_work.func = io_waitid_cb;
__io_req_task_work_add(req, IOU_F_TWQ_IN_WAKE);
return 1;
@@ -343,6 +349,8 @@ int io_waitid(struct io_kiocb *req, unsigned int issue_flags)
* a waitqueue callback, or if someone cancels it.
*/
if (!io_waitid_drop_issue_ref(req)) {
+ if (io_wq_cancel_seen(req, issue_flags))
+ __io_waitid_cancel(req, true);
io_ring_submit_unlock(ctx, issue_flags);
return IOU_ISSUE_SKIP_COMPLETE;
}
diff --git a/io_uring/zcrx.c b/io_uring/zcrx.c
index cc9df7e64a407..6d7abb1636e7f 100644
--- a/io_uring/zcrx.c
+++ b/io_uring/zcrx.c
@@ -1645,7 +1645,7 @@ static bool io_zcrx_queue_cqe(struct io_kiocb *req, struct net_iov *niov,
cqe->flags |= IORING_CQE_F_32;
area = io_zcrx_iov_to_area(niov);
- offset = off + (net_iov_idx(niov) << ifq->niov_shift);
+ offset = off + ((u64)net_iov_idx(niov) << ifq->niov_shift);
rcqe = (struct io_uring_zcrx_cqe *)(cqe + 1);
rcqe->off = offset + zcrx_area_id_to_token(area->area_id);
rcqe->__pad = 0;
|
| 3/2 | 2026/10/16 00:51 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 24491 output: 34 thoughts: 420 Thoughts: **Analyzing a Kernel Patch: io_uring Deep Dive** Okay, here's a diff applied to the kernel HEAD. Let's dig in. The first thing that jumps out is that this is io_uring code, a core subsystem. Bingo! This is prime real estate for fuzzing, especially given how heavily syzkaller targets io_uring. This is a very relevant place to look for potential bugs. Looking at the file list, we've got a comprehensive spread across the io_uring directory: headers, cancel operations, file descriptor info, futex interactions, workqueue integration, the core implementation, kbuf management, the event loop, various concurrency structures, NAPI integration, networking components, open/close handling, polling mechanisms, registration, read/write functions, SQPOLL, timeouts, task waking, wait queues, waitid, and zero copy receive. The whole shebang. Absolutely, this code is reachable. We're talking about direct syscalls: `sys_io_uring_setup`, `sys_io_uring_enter`, and `sys_io_uring_register`. These are fully accessible in a standard QEMU environment. This is definitely `WorthFuzzing = true`. Now, let's figure out the configuration dependencies. I need to check the Kconfig to see what modules and options enable this functionality. Then, I'll need to figure out what symbols are interesting to fuzz. I'll need to run a grepper or read the file to find the important symbols. |
| 4/2 | 2026/10/16 00:51 | tool | grepper |
0mArgs:{
"Expression": "config IO_URING",
"PathPrefixes": [
"io_uring/",
"init/"
]
}Results: {
"Output": "init/Kconfig=1957=config AIO\n--\ninit/Kconfig-1964-\ninit/Kconfig:1965:config IO_URING\ninit/Kconfig-1966-\tbool \"Enable IO uring support\" if EXPERT\n--\ninit/Kconfig=1974=config GCOV_PROFILE_URING\n--\ninit/Kconfig-1986-\ninit/Kconfig:1987:config IO_URING_MOCK_FILE\ninit/Kconfig-1988-\ttristate \"Enable io_uring mock files (Experimental)\" if EXPERT\n--\nio_uring/Kconfig-5-\nio_uring/Kconfig:6:config IO_URING_ZCRX\nio_uring/Kconfig-7-\tdef_bool y\n--\nio_uring/Kconfig-12-\nio_uring/Kconfig:13:config IO_URING_BPF\nio_uring/Kconfig-14-\tdef_bool y\n--\nio_uring/Kconfig-17-\nio_uring/Kconfig:18:config IO_URING_BPF_OPS\nio_uring/Kconfig-19-\tdef_bool y\n"
} |
| 5/2 | 2026/10/16 00:51 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 25565 output: 33 thoughts: 24 Thoughts: Okay, here's my take on those thoughts, tailored for an expert audience: **Diving into Kconfig Configuration for io_uring and Initialization** Alright, my internal radar's pinging. I need to take a look at the Kconfig configurations for a potential interaction or dependency. My first port of call will be init/Kconfig, specifically around line 1965 – something there seems relevant. I'm assuming there's an issue with how the kernel is initialized, or a flag that can be set during initialization, as I want to enable io_uring support. Then I'll pivot to io_uring/Kconfig and scrutinize its definitions to identify all dependencies or optional features related to io_uring. I need to see how the two areas interact. I'm expecting some configuration options related to resource allocation, perhaps interrupt handling, or memory management that could influence io_uring's behavior during the early boot process. This feels like debugging a race condition or a configuration conflict waiting to happen. The goal is to verify that these configs are set correctly for my use case. |
| 6/2 | 2026/10/16 00:52 | tool | read-file |
0mArgs:{
"File": "init/Kconfig",
"FirstLine": 1960,
"LineCount": 20
}Results: {
"Contents": "1960:\t\thelp\n1961:\t\t This option enables POSIX asynchronous I/O which may by used\n1962:\t\t by some high performance threaded applications. Disabling\n1963:\t\t this option saves about 7k.\n1964:\t\n1965:\tconfig IO_URING\n1966:\t\tbool \"Enable IO uring support\" if EXPERT\n1967:\t\tselect IO_WQ\n1968:\t\tdefault y\n1969:\t\thelp\n1970:\t\t This option enables support for the io_uring interface, enabling\n1971:\t\t applications to submit and complete IO through submission and\n1972:\t\t completion rings that are shared between the kernel and application.\n1973:\t\n1974:\tconfig GCOV_PROFILE_URING\n1975:\t\tbool \"Enable GCOV profiling on the io_uring subsystem\"\n1976:\t\tdepends on IO_URING \u0026\u0026 GCOV_KERNEL\n1977:\t\thelp\n1978:\t\t Enable GCOV profiling on the io_uring subsystem, to facilitate\n1979:\t\t code coverage testing.\n"
} |
| 7/2 | 2026/10/16 00:52 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 26149 output: 32 thoughts: 12 Thoughts: Okay, let's dive into it. Here's how I'd approach this, thinking as an expert: **Investigating io_uring/Kconfig** Alright, time to get to the heart of the matter. My first instinct, given what we're likely discussing, is to jump straight into `io_uring/Kconfig`. It’s the configuration file, the gatekeeper. It holds the core definitions, the dependencies, and the options that determine *how* `io_uring` is built and integrated into the system. This file's a vital piece of understanding how the kernel's going to work. |
| 8/2 | 2026/10/16 00:52 | tool | read-file |
0mArgs:{
"File": "io_uring/Kconfig",
"FirstLine": 1,
"LineCount": 40
}Results: {
"Contents": " 1:\t# SPDX-License-Identifier: GPL-2.0-only\n 2:\t#\n 3:\t# io_uring configuration\n 4:\t#\n 5:\t\n 6:\tconfig IO_URING_ZCRX\n 7:\t\tdef_bool y\n 8:\t\tdepends on IO_URING\n 9:\t\tdepends on PAGE_POOL\n 10:\t\tdepends on INET\n 11:\t\tdepends on NET_RX_BUSY_POLL\n 12:\t\n 13:\tconfig IO_URING_BPF\n 14:\t\tdef_bool y\n 15:\t\tdepends on BPF\n 16:\t\tdepends on NET\n 17:\t\n 18:\tconfig IO_URING_BPF_OPS\n 19:\t\tdef_bool y\n 20:\t\tdepends on IO_URING\n 21:\t\tdepends on BPF_SYSCALL \u0026\u0026 BPF_JIT \u0026\u0026 DEBUG_INFO_BTF\n"
} |
| 9/2 | 2026/10/16 00:52 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 26571 output: 31 thoughts: 18 Thoughts: Here's my take on those thoughts, tailored for an expert audience: **Diving into the Build Process of io_uring** Alright, let's get down to brass tacks. I need to understand the build process for `io_uring`, and the `Makefile` is the obvious place to start. I'm going to crack open `io_uring/Makefile` to dissect the exact build sequence. Specifically, I'm thinking about how the object files are created, the linking dependencies, any compiler flags employed, and how this all meshes with the kernel's build system, since it's ultimately a core component. Any clever optimizations or workarounds for specific architectures that are used here are of particular interest. I'm hoping it's well-structured and doesn't employ any unusual tricks that will necessitate a deep dive into obscure build systems. My goal is to rapidly grasp the build logic and identify any potential gotchas or areas ripe for optimization or modification for my specific project needs. |
| 10/2 | 2026/10/16 00:52 | tool | read-file |
0mArgs:{
"File": "io_uring/Makefile",
"FirstLine": 1,
"LineCount": 40
}Results: {
"Contents": " 1:\t# SPDX-License-Identifier: GPL-2.0\n 2:\t#\n 3:\t# Makefile for io_uring\n 4:\t\n 5:\tifdef CONFIG_GCOV_PROFILE_URING\n 6:\tGCOV_PROFILE := y\n 7:\tendif\n 8:\t\n 9:\tobj-$(CONFIG_IO_URING)\t\t+= io_uring.o opdef.o kbuf.o rsrc.o notif.o \\\n 10:\t\t\t\t\t\ttctx.o filetable.o rw.o poll.o \\\n 11:\t\t\t\t\t\ttw.o wait.o eventfd.o uring_cmd.o \\\n 12:\t\t\t\t\t\topenclose.o sqpoll.o xattr.o nop.o \\\n 13:\t\t\t\t\t\tfs.o splice.o sync.o msg_ring.o \\\n 14:\t\t\t\t\t\tadvise.o openclose.o statx.o timeout.o \\\n 15:\t\t\t\t\t\tcancel.o waitid.o register.o \\\n 16:\t\t\t\t\t\ttruncate.o memmap.o alloc_cache.o \\\n 17:\t\t\t\t\t\tquery.o loop.o\n 18:\t\n 19:\tobj-$(CONFIG_IO_URING_ZCRX)\t+= zcrx.o\n 20:\tobj-$(CONFIG_IO_WQ)\t\t+= io-wq.o\n 21:\tobj-$(CONFIG_FUTEX)\t\t+= futex.o\n 22:\tobj-$(CONFIG_EPOLL)\t\t+= epoll.o\n 23:\tobj-$(CONFIG_NET_RX_BUSY_POLL)\t+= napi.o\n 24:\tobj-$(CONFIG_NET) += net.o cmd_net.o\n 25:\tobj-$(CONFIG_PROC_FS) += fdinfo.o\n 26:\tobj-$(CONFIG_IO_URING_MOCK_FILE) += mock_file.o\n 27:\tobj-$(CONFIG_IO_URING_BPF) += bpf_filter.o\n 28:\tobj-$(CONFIG_IO_URING_BPF_OPS) += bpf-ops.o\n 29:\tobj-$(CONFIG_DMA_SHARED_BUFFER) += dma-buf.o\n"
} |
| 11/2 | 2026/10/16 00:52 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 27378 output: 32 thoughts: 1779 Thoughts: **Analyzing the Impact of this Commit for Fuzzing Focus** Alright, let's break down this commit and figure out the best approach for fuzzing, given that we want to provide good guidance for the fuzzer. First, I'm checking which configurations are touched: `IO_URING` (the base, also selects `IO_WQ`), `FUTEX`, `NET_RX_BUSY_POLL`, `NET`, `PROC_FS`, and `IO_URING_ZCRX` (which has dependencies on `PAGE_POOL`, `INET`, and `NET_RX_BUSY_POLL`). This tells us the general area of the changes. I'm then scanning the diff for additional configuration dependencies. I've spotted a `#ifdef CONFIG_NET_RX_BUSY_POLL` in `io_uring/napi.c` and `obj-$(CONFIG_IO_URING_ZCRX) += zcrx.o` in `io_uring/zcrx.c`. This confirms that those configurations are relevant. Now, let's identify the changed functions across all the files. I've got a comprehensive list of all the functions modified in the diff. This is crucial for determining *where* the changes are. From the guidance, I know to avoid ubiquitous functions (allocators, synchronization primitives, etc.) because they'll pollute the focus, diluting the fuzzing effort. Instead, I need to pinpoint feature-specific logic or API entrypoints. In case of inline static functions, I have to find the non-static callers. So, I'm going through the list and looking for non-static functions that seem to be specific to the io_uring feature set. The candidates that stand out are: * `io_register_resize_rings` * `io_sync_cancel` * `io_async_cancel` * `io_futex_wait` / `io_futexv_wait` * `io_timeout` / `io_timeout_update` * `io_waitid` * `io_cqring_wait` * `io_poll_add` * `io_queue_iowq` * `io_wq_worker` * `io_uring_cancel_generic` * `io_pipe_fixed` I'm verifying that these aren't static; specifically, checking the code for `io_register_resize_rings`. It's non-static, so the fuzzer can target it. |
| 12/2 | 2026/10/16 00:52 | tool | grepper |
0mArgs:{
"Expression": "io_register_resize_rings",
"PathPrefixes": [
"io_uring/"
]
}Results: {
"Output": "io_uring/register.c=499=static bool io_sqe_is_128(struct io_ring_ctx *ctx,\n--\nio_uring/register.c-509-\nio_uring/register.c:510:static int io_register_resize_rings(struct io_ring_ctx *ctx, void __user *arg)\nio_uring/register.c-511-{\n--\nio_uring/register.c=800=static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode,\n--\nio_uring/register.c-1004-\t\t\tbreak;\nio_uring/register.c:1005:\t\tret = io_register_resize_rings(ctx, arg);\nio_uring/register.c-1006-\t\tbreak;\n"
} |
| 13/2 | 2026/10/16 00:52 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 29965 output: 30 thoughts: 739 Thoughts: **Analyzing Function Visibility in the Kernel Code** Okay, so I'm digging into this kernel code and need to understand the visibility of the functions. My initial thought was that `io_register_resize_rings` being static could be inlined in a non-LTO kernel. However, I want to confirm which functions are actually non-static within the modified sections. Let's begin by systematically checking the modified functions, starting with those found in the diff. First up is `cancel.c`, where I find `io_try_cancel`, `io_async_cancel`, `io_sync_cancel`, and `io_uring_cancel_generic`. All are non-static. Moving on, I see `io_futex_wait` and `io_futexv_wait` are also non-static. The pattern is continuing, so I'll quickly confirm `io_poll_add`, `io_timeout`, and `io_waitid`, all non-static. Then I see `io_cqring_wait`, `io_queue_iowq`, `io_wq_submit_work`, `io_wq_free_work`, `io_wq_put_work`, and `io_req_local_work_add` – they're all non-static as well. Next, I'll focus on `io_timeout.c`. I already noted `io_timeout`, but I need to make sure I don't miss anything, so I confirm that `io_timeout_remove_prep` is non-static too. Now, let's turn to `io_uring/net.c`. A quick check shows that `io_recvmsg` and `io_recv` are non-static. Finally, I'll switch over to `io_uring/openclose.c`. I need to figure out what's calling `io_pipe_fixed`, so I'll grep for that. |
| 14/2 | 2026/10/16 00:52 | tool | grepper |
0mArgs:{
"Expression": "io_pipe_fixed",
"PathPrefixes": [
"io_uring/"
]
}Results: {
"Output": "io_uring/openclose.c=326=int io_pipe_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)\n--\nio_uring/openclose.c-342-\nio_uring/openclose.c:343:static int io_pipe_fixed(struct io_kiocb *req, struct file **files,\nio_uring/openclose.c-344-\t\t\t unsigned int issue_flags)\n--\nio_uring/openclose.c=426=int io_pipe(struct io_kiocb *req, unsigned int issue_flags)\n--\nio_uring/openclose.c-436-\tif (!!p-\u003efile_slot)\nio_uring/openclose.c:437:\t\tret = io_pipe_fixed(req, files, issue_flags);\nio_uring/openclose.c-438-\telse\n"
} |
| 15/2 | 2026/10/16 00:52 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 31389 output: 275 thoughts: 1406 Thoughts: **My Analysis of IO_uring Changes for Fuzzing** Okay, let's break this down. First, I need to identify the key functions involved and how they're called. I'm focusing on io_uring, and I see that `io_pipe` is a non-static caller of `io_pipe_fixed`. For the ring resize, `io_register_resize_rings` is called by `__io_uring_register`, which in turn leads to `sys_io_uring_register`. Now, I'm looking at `io-wq.c`. `io_wq_worker` is a static thread function, typically created with `kthread_run` or `create_io_worker`. The question is, is `io_wq_worker` in the symbol table? Given its nature, it likely *is*, unless inlined. But non-static symbols are guaranteed to be. The important thing is to select a focused set of symbols for fuzzing. I need to avoid anything ubiquitous, like allocators or schedulers, that would dilute the fuzzing effort. I've compiled a list of function names which seem to be highly relevant: `io_try_cancel`, `io_async_cancel`, `io_sync_cancel`, `io_futex_wait`, `io_futexv_wait`, `io_waitid`, `io_timeout`, `io_poll_add`, `io_cqring_wait`, and `io_pipe`. `io_cqring_wait` is the primary wait path for io_uring completion. It is specific to io_uring, and it seems like a valid choice despite being a common point. The rule is to avoid ubiquitous lifecycle hot-paths, and target specific feature logic and entrypoints. The functions I've selected, like `io_async_cancel`, `io_sync_cancel`, `io_futex_wait`, `io_futexv_wait`, `io_waitid`, `io_timeout`, `io_poll_add`, and `io_pipe` are all specific entrypoints to the features which have been changed. Also `io_queue_iowq`, `io_wq_put_work` (for work queue handling) and `io_req_local_work_add` (local task_work). I need to confirm the required configurations. I'm seeing: `CONFIG_IO_URING`, `CONFIG_FUTEX` (for futex.c), `CONFIG_NET` (for net.c), `CONFIG_NET_RX_BUSY_POLL` (for napi.c), and `CONFIG_IO_URING_ZCRX` (for zcrx.c). Let's double-check if `CONFIG_IO_URING` requires anything else. Based on the `init/Kconfig` file, it *selects* `IO_WQ` by default. And in `io_uring/Kconfig` `IO_URING_ZCRX` depends on `IO_URING`, `PAGE_POOL`, `INET`, and `NET_RX_BUSY_POLL`. So I will include: "IO_URING", "FUTEX", "NET", "NET_RX_BUSY_POLL", "PAGE_POOL", "INET" and "IO_URING_ZCRX". This patch touches multiple areas, like cancellation, futex handling, waitid cancellation, timeouts, ring waits and overflows, network receive buffer recycling, pipe error handling, and ring resize logic. All of this code is accessible through io_uring syscalls, which confirms that fuzzing is necessary. This is especially important for testing race conditions, lifecycle regressions, and cancellation corner cases. Finally, let's sanity check my focus symbols one last time: `io_async_cancel`, `io_sync_cancel`, `io_futex_wait`, `io_futexv_wait`, `io_waitid`, `io_timeout`, `io_poll_add`, `io_pipe`, `io_queue_iowq`, and `io_wq_put_work`. They're all non-static, directly involved in the changes, and specific to those features. Alright, that covers it. I'm ready to call `set-results`. |
| 16/2 | 2026/10/16 00:52 | tool | set-results |
0mArgs:{
"EnableConfigs": [
"IO_URING",
"FUTEX",
"NET",
"NET_RX_BUSY_POLL",
"PAGE_POOL",
"INET",
"IO_URING_ZCRX"
],
"FocusSymbols": [
"io_async_cancel",
"io_sync_cancel",
"io_futex_wait",
"io_futexv_wait",
"io_waitid",
"io_timeout",
"io_poll_add",
"io_pipe",
"io_queue_iowq",
"io_wq_put_work"
],
"Reasoning": "The patch set modifies core io_uring execution logic across multiple subsystems including request cancellation (io-wq cancel races and running flag tracking), futex wait preparation and cancellation, waitid cancellation, timeout sequence logic and update conversions, network recv buffer recycling on partial transfers, fixed pipe error unwinding, and ring resizing for mixed/128b SQEs and 32b CQEs. All these code paths are directly reachable from userspace via io_uring syscalls in standard virtualized environments and present significant synchronization, cancellation, and state management complexities that warrant fuzzing.",
"WorthFuzzing": true
}Results: {
"EnableConfigs": [
"IO_URING",
"FUTEX",
"NET",
"NET_RX_BUSY_POLL",
"PAGE_POOL",
"INET",
"IO_URING_ZCRX"
],
"FocusSymbols": [
"io_async_cancel",
"io_sync_cancel",
"io_futex_wait",
"io_futexv_wait",
"io_waitid",
"io_timeout",
"io_poll_add",
"io_pipe",
"io_queue_iowq",
"io_wq_put_work"
],
"Reasoning": "The patch set modifies core io_uring execution logic across multiple subsystems including request cancellation (io-wq cancel races and running flag tracking), futex wait preparation and cancellation, waitid cancellation, timeout sequence logic and update conversions, network recv buffer recycling on partial transfers, fixed pipe error unwinding, and ring resizing for mixed/128b SQEs and 32b CQEs. All these code paths are directly reachable from userspace via io_uring syscalls in standard virtualized environments and present significant synchronization, cancellation, and state management complexities that warrant fuzzing.",
"WorthFuzzing": true
} |
| 17/1 | 2026/10/16 00:52 | action | If |
2mArgs:{
"WorthFuzzing": true
}Results: null |
| 18/2 | 2026/10/16 00:52 | agent | kmsan-evaluator |
2mModel:core Args: null Results: {
"KMSANReasoning": "The patch series addresses concurrency, cancellation, task work synchronization, and bounds handling across several io_uring submodules:\n1. Struct and variable initialization: New struct fields (such as `running` in `struct io_cancel_data`, `next_work` in `struct io_worker`, `in_cancel` in `struct io_tctx_exit`, and `poll_wq_rcu` / `poll_wq_rcu_pending` in `struct io_ring_ctx`) are either allocated with `kzalloc_obj`, cleared via designated struct initializers (`= { ... }`), or explicitly initialized prior to being read.\n2. User-space copies and info-leaks: In `io_pipe_fixed()`, `copy_to_user()` copies `fds` (an `int[2]` with no padding, both elements fully initialized), and the patch only modifies the locking scope around the copy. In `io_zcrx_queue_cqe()`, casting to `u64` prevents integer truncation/overflow while `__pad` remains zeroed.\n3. Memory safety and ring handling: Changes in `fdinfo.c` and `register.c` correct CQE32 and SQE128 index calculations and wrap-around bounds checks in shared ring buffers. Any indexing or sizing bugs would result in out-of-bounds access or data corruption, which are directly detected by KASAN. Concurrency, memory barrier, and locking adjustments (e.g. in `mpscq.h`, `io-wq.c`, `waitid.c`) are monitored by LOCKDEP.\n\nBecause no uninitialized stack/heap reads, uninitialized conditional branches, or uninitialized user-space info-leaks are introduced, a dedicated KMSAN session is not justified.",
"NeedsKMSAN": false
}Instruction: You are an expert Linux kernel security engineer specializing in kernel memory
error detectors (KASAN and KMSAN). Your job is to review the provided patch series and
determine if the code changes justify spawning a dedicated KMSAN (KernelMemorySanitizer)
fuzzing session in addition to standard KASAN fuzzing.
CRITICAL DISTINCTION BETWEEN KASAN AND KMSAN:
- Standard KASAN kernel builds (upstream-apparmor-kasan.config) already enable
a comprehensive suite of debugging tools and sanitizers, including KASAN
(out-of-bounds accesses, use-after-free, double free, invalid free), LOCKDEP
(locking bugs and deadlocks), UB-sanitizers, and memory corruption checks.
- KMSAN (KernelMemorySanitizer) detects reads of UNINITIALIZED memory (stack, heap,
or page allocations) and kernel-to-user memory info-leaks.
Rule: THERE IS NO SENSE IN RUNNING A KMSAN SESSION IF A BUG CAN BE CAUGHT BY KASAN,
LOCKDEP, OR OTHER STANDARD BUG DETECTORS.
A dedicated KMSAN fuzzing session incurs significant resource costs. You must ONLY
set NeedsKMSAN=true if the code changes introduce or expose UNINITIALIZED MEMORY risks
that are detected ONLY by KMSAN.
Look holistically at the patch series and surrounding code. Even if no direct
uninitialized field accesses or new buffer allocations are added in the diff itself,
a patch may alter control flow, bounds checking, or data length calculations in ways
that change how the rest of the code operates on existing buffers (e.g. allowing
uninitialized stack/heap memory to be read, copied to user space, or used in control
flow). Do not hesitate to use your code access tools to inspect the surrounding code,
called functions, and callers.
Set NeedsKMSAN=true ONLY IF the patch introduces or modifies:
1. Kernel structures sent to user space (via copy_to_user, put_user, netlink skb
attributes, ioctl output arguments, socket options, or BPF buffers) where fields
or structure padding might not be fully initialized/zeroed.
2. Conditional logic or branching that depends on potentially uninitialized variables
or struct fields.
3. Allocation or initialization of complex data structures where uninitialized fields
could be read later in reachable code paths.
4. Bounds checks, lengths, or logic in a way that allows surrounding code to access
uninitialized bytes of existing buffers.
Set NeedsKMSAN=false IF:
- The code changes primarily risk out-of-bounds access, array overflows, NULL pointer
dereferences, locking deadlocks, or use-after-free bugs (these are already caught
by KASAN, LOCKDEP, or standard bug detectors).
- All stack/heap structures touched or introduced by the patch are fully zeroed
or initialized (e.g. using = {0}, memset, kzalloc) before being read or copied.
- The patch does not introduce any risk of uninitialized memory usage or info-leaks.
Use your code access tools to inspect the surrounding code if necessary, then provide
detailed KMSANReasoning contrasting KASAN vs KMSAN applicability for this patch.
Prefer calling several tools at the same time to save round-trips.
Use set-results tool to provide results of the analysis.
It must be called exactly once before the final reply.
Ignore results of this tool.
Prompt: Target architecture: amd64
For your convenience, here is the diff of the changes:
commit 48577bc0265d4180886ef9a5bafa875e849d186b
Author: syz-cluster <triage@syzkaller.com>
Date: Fri Oct 16 00:51:24 2026 +0000
syz-cluster: applied patch under review
diff --git a/include/linux/io_uring_types.h b/include/linux/io_uring_types.h
index 50970b382a9ab..3e00012cbfd40 100644
--- a/include/linux/io_uring_types.h
+++ b/include/linux/io_uring_types.h
@@ -520,6 +520,9 @@ struct io_ring_ctx {
u32 iowq_limits[2];
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 nr_drained;
diff --git a/io_uring/cancel.c b/io_uring/cancel.c
index 9a92a6aa2bccd..6f7a6cf356c27 100644
--- a/io_uring/cancel.c
+++ b/io_uring/cancel.c
@@ -72,7 +72,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,
@@ -117,6 +126,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)
@@ -180,6 +191,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)
@@ -195,6 +207,9 @@ static int __io_async_cancel(struct io_cancel_data *cd,
mutex_lock(&ctx->tctx_lock);
ret = -ENOENT;
list_for_each_entry(node, &ctx->tctx_list, ctx_node) {
+ /* 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)
@@ -204,7 +219,15 @@ static int __io_async_cancel(struct io_cancel_data *cd,
}
mutex_unlock(&ctx->tctx_lock);
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)
@@ -308,7 +331,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
@@ -694,8 +717,18 @@ __cold void io_uring_cancel_generic(bool cancel_all, struct io_sq_data *sqd)
io_uring_drop_tctx_refs(current);
/* only skip the sleep for local work this task may run */
xa_for_each(&tctx->xa, index, node) {
- if (io_local_work_pending(node->ctx) &&
- io_allowed_defer_tw_run(node->ctx))
+ if (!(node->ctx->flags & IORING_SETUP_DEFER_TASKRUN) ||
+ !io_allowed_defer_tw_run(node->ctx))
+ continue;
+ /*
+ * The wake up io_uring_try_cancel_requests() asked for
+ * may have been issued while we were still running. Ask
+ * again now that it can't be missed.
+ */
+ atomic_set(&node->ctx->cq_wait_nr, 1);
+ /* pairs with the xchg() in mpscq_push() */
+ smp_mb();
+ if (io_local_work_pending(node->ctx))
goto end_wait;
}
/*
diff --git a/io_uring/cancel.h b/io_uring/cancel.h
index e49713a1bc669..484fa5e7ffaaf 100644
--- a/io_uring/cancel.h
+++ b/io_uring/cancel.h
@@ -11,6 +11,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);
diff --git a/io_uring/fdinfo.c b/io_uring/fdinfo.c
index c362f3a81bdce..46e64b0413605 100644
--- a/io_uring/fdinfo.c
+++ b/io_uring/fdinfo.c
@@ -154,20 +154,26 @@ static void __io_uring_show_fdinfo(struct io_ring_ctx *ctx, struct seq_file *m)
cq_entries = min(cq_tail - cq_head, ctx->cq_entries);
for (i = 0; i < cq_entries; i++) {
struct io_uring_cqe *cqe;
- bool cqe32 = false;
+ bool cqe32 = false, two_entries = false;
bool is_last_cqarray_slot = (cq_head & cq_mask) == cq_mask;
- cqe = &r->cqes[(cq_head & cq_mask)];
- /*
- * Userspace can manipulate the last cqarray slot to have
- * IORING_CQE_F_32 set, which would cause the second half of
- * that CQE to be read out of bounds.
- * Ignore the flag for the last cqarray slot, which can't
- * legitimately have the flag set.
- */
- if ((cqe->flags & IORING_CQE_F_32 || ctx->flags & IORING_SETUP_CQE32) &&
- !is_last_cqarray_slot)
+ if (ctx->flags & IORING_SETUP_CQE32) {
+ /* all entries have twice the size */
+ cqe = &r->cqes[(cq_head & cq_mask) << 1];
cqe32 = true;
+ } else {
+ cqe = &r->cqes[(cq_head & cq_mask)];
+ /*
+ * Userspace can manipulate the last cqarray slot to
+ * have IORING_CQE_F_32 set, which would cause the second
+ * half of that CQE to be read out of bounds.
+ * Ignore the flag for the last cqarray slot, which can't
+ * legitimately have the flag set.
+ */
+ two_entries = (cqe->flags & IORING_CQE_F_32) &&
+ !is_last_cqarray_slot;
+ cqe32 = two_entries;
+ }
seq_printf(m, "%5u: user_data:%llu, res:%d, flags:%x",
cq_head & cq_mask, cqe->user_data, cqe->res,
cqe->flags);
@@ -176,7 +182,7 @@ static void __io_uring_show_fdinfo(struct io_ring_ctx *ctx, struct seq_file *m)
cqe->big_cqe[0], cqe->big_cqe[1]);
seq_printf(m, "\n");
cq_head++;
- if (cqe32) {
+ if (two_entries) {
cq_head++;
i++;
}
diff --git a/io_uring/futex.c b/io_uring/futex.c
index eaee14242a3a3..9203998654ffb 100644
--- a/io_uring/futex.c
+++ b/io_uring/futex.c
@@ -152,6 +152,24 @@ int io_futex_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
return 0;
}
+/*
+ * A wait on a private futex does not block the submitter. Even a single
+ * threaded task can go on and have the mm private hash installed by clone()
+ * or prctl(), which does not pick up waiters from the global hash. Make
+ * sure the private hash exists before the first waiter is queued.
+ */
+static int io_futex_prep_private(struct io_kiocb *req)
+{
+ int ret = futex_hash_allocate_default();
+
+ if (unlikely(ret))
+ return ret;
+
+ /* inflight tracking only needed for mm private hash */
+ io_req_track_inflight(req);
+ return 0;
+}
+
int io_futex_wait_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
{
struct io_futex *iof = io_kiocb_to_cmd(req, struct io_futex);
@@ -161,9 +179,8 @@ int io_futex_wait_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
if (unlikely(ret))
return ret;
- /* inflight tracking only needed for mm private hash */
if (!(iof->futex_flags & FLAGS_SHARED))
- io_req_track_inflight(req);
+ return io_futex_prep_private(req);
return 0;
}
@@ -213,10 +230,13 @@ int io_futexv_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
return ret;
}
- /* inflight tracking only needed for mm private hash */
for (i = 0; i < iof->futex_nr; i++) {
if (!(ifd->futexv[i].w.flags & FLAGS_SHARED)) {
- io_req_track_inflight(req);
+ ret = io_futex_prep_private(req);
+ if (ret) {
+ kfree(ifd);
+ return ret;
+ }
break;
}
}
@@ -283,6 +303,8 @@ int io_futexv_wait(struct io_kiocb *req, unsigned int issue_flags)
*/
__set_current_state(TASK_RUNNING);
hlist_add_head(&req->hash_node, &ctx->futex_list);
+ if (io_wq_cancel_seen(req, issue_flags))
+ __io_futex_cancel(req);
} else {
iof->futexv_unqueued = 1;
if (woken != -1)
@@ -323,6 +345,8 @@ int io_futex_wait(struct io_kiocb *req, unsigned int issue_flags)
&ifd->q, NULL, NULL);
if (!ret) {
hlist_add_head(&req->hash_node, &ctx->futex_list);
+ if (io_wq_cancel_seen(req, issue_flags))
+ __io_futex_cancel(req);
io_ring_submit_unlock(ctx, issue_flags);
return IOU_ISSUE_SKIP_COMPLETE;
diff --git a/io_uring/io-wq.c b/io_uring/io-wq.c
index 97523ccc7a714..dfdc78612783a 100644
--- a/io_uring/io-wq.c
+++ b/io_uring/io-wq.c
@@ -56,6 +56,8 @@ struct io_worker {
struct io_wq_acct *acct;
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;
struct completion ref_done;
@@ -467,11 +469,9 @@ static void io_wq_dec_running(struct io_worker *worker)
if (!atomic_dec_and_test(&acct->nr_running))
return;
- if (!worker->cur_work)
- return;
if (!io_acct_run_queue(acct))
return;
- if (io_wq_hash_defer(worker->cur_work, acct)) {
+ if (worker->cur_work && io_wq_hash_defer(worker->cur_work, acct)) {
raw_spin_unlock(&acct->lock);
return;
}
@@ -583,6 +583,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)
{
@@ -591,9 +602,7 @@ static void io_assign_current_work(struct io_worker *worker,
cond_resched();
}
- raw_spin_lock(&worker->lock);
- worker->cur_work = work;
- raw_spin_unlock(&worker->lock);
+ __io_assign_current_work(worker, work);
}
/*
@@ -625,9 +634,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->cur_work = work;
- raw_spin_unlock(&worker->lock);
+ __io_assign_current_work(worker, work);
}
raw_spin_unlock(&acct->lock);
@@ -646,25 +653,32 @@ 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 work_flags = atomic_read(&work->flags);
unsigned int hash = __io_wq_is_hashed(work_flags)
? __io_get_work_hash(work_flags)
: -1U;
struct io_kiocb *req;
+ bool last;
next_hashed = wq_next_work(work);
- if (do_kill &&
- (work_flags & IO_WQ_WORK_UNBOUND))
+ /*
+ * 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 (do_kill && (work_flags & IO_WQ_WORK_UNBOUND))
atomic_or(IO_WQ_WORK_CANCEL, &work->flags);
+ else if (do_kill)
+ set_notify_signal(current);
req = container_of(work, struct io_kiocb, work);
kcov_remote_start_common(req->ctx->kcov_handle);
io_wq_submit_work(work);
kcov_remote_stop();
io_assign_current_work(worker, NULL);
- linked = io_wq_free_work(work);
+ last = io_wq_put_work(work, &linked);
work = next_hashed;
if (!work && linked && !io_wq_is_hashed(linked)) {
work = linked;
@@ -673,6 +687,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)
+ io_wq_free_work(done);
if (hash != -1U && !next_hashed) {
/* serialize hash clear with wake_up() */
@@ -691,6 +712,35 @@ 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_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(&acct->workers_lock);
+ if (acct->nr_workers >= acct->max_workers) {
+ /* a worker was created in our place, the work is its business */
+ raw_spin_unlock(&acct->workers_lock);
+ return true;
+ }
+ acct->nr_workers++;
+ raw_spin_unlock(&acct->workers_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;
@@ -724,19 +774,41 @@ static int io_wq_worker(void *data)
* idle-exit, drop the worker as well. This is used to avoid
* keeping io-wq workers around for tasks that no longer have
* any active io_uring instances.
+ * 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)) ||
- test_bit(IO_WQ_BIT_EXIT_ON_IDLE, &wq->state)) {
+ if (((last_timeout && (exit_mask || acct->nr_workers > 1)) ||
+ test_bit(IO_WQ_BIT_EXIT_ON_IDLE, &wq->state)) &&
+ !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);
raw_spin_unlock(&acct->workers_lock);
__set_current_state(TASK_RUNNING);
- break;
+ if (io_worker_retire(acct, worker))
+ break;
+ last_timeout = false;
+ continue;
}
last_timeout = false;
__io_worker_idle(acct, worker);
raw_spin_unlock(&acct->workers_lock);
if (io_run_task_work())
continue;
+ /*
+ * io_wq_enqueue() leaves new work to us for as long as we are
+ * accounted as running. Get out of ->nr_running before taking
+ * the last look at the work list, rather than when scheduling
+ * out. No need to ask for another worker then, we can do it.
+ */
+ clear_bit(IO_WORKER_F_RUNNING, &worker->flags);
+ atomic_dec(&acct->nr_running);
+ if (io_acct_run_queue(acct)) {
+ io_wq_worker_running(current);
+ io_worker_handle_work(acct, worker);
+ continue;
+ }
ret = schedule_timeout(WORKER_IDLE_TIMEOUT);
if (signal_pending(current)) {
struct ksignal ksig;
@@ -1002,9 +1074,13 @@ void io_wq_set_exit_on_idle(struct io_wq *wq, bool enable)
static void io_run_cancel(struct io_wq_work *work, struct io_wq *wq)
{
do {
+ struct io_wq_work *linked;
+
atomic_or(IO_WQ_WORK_CANCEL, &work->flags);
io_wq_submit_work(work);
- work = io_wq_free_work(work);
+ if (io_wq_put_work(work, &linked))
+ io_wq_free_work(work);
+ work = linked;
} while (work);
}
@@ -1112,6 +1188,7 @@ 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
@@ -1120,6 +1197,19 @@ static bool io_wq_worker_cancel(struct io_worker *worker, void *data)
raw_spin_lock(&worker->lock);
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)) {
+ atomic_or(IO_WQ_WORK_CANCEL, &work->flags);
+ match->nr_running++;
+ }
+ }
raw_spin_unlock(&worker->lock);
return match->nr_running && !match->cancel_all;
diff --git a/io_uring/io_uring.c b/io_uring/io_uring.c
index 0c91d466d8259..e6569c2049172 100644
--- a/io_uring/io_uring.c
+++ b/io_uring/io_uring.c
@@ -407,17 +407,46 @@ static void io_prep_async_link(struct io_kiocb *req)
}
}
+/* Called for every request that is issued: keep it inline */
+static __always_inline bool io_assign_file(struct io_kiocb *req,
+ const struct io_issue_def *def,
+ unsigned int issue_flags)
+{
+ if (req->file || !def->needs_file)
+ return true;
+
+ if (req->flags & REQ_F_FIXED_FILE)
+ req->file = io_file_get_fixed(req, req->cqe.fd, issue_flags);
+ else
+ req->file = io_file_get_normal(req, req->cqe.fd);
+
+ return !!req->file;
+}
+
void io_queue_iowq(struct io_kiocb *req)
{
struct io_uring_task *tctx = req->tctx;
BUG_ON(!tctx);
+ lockdep_assert_held(&req->ctx->uring_lock);
if ((current->flags & PF_KTHREAD) || !tctx->io_wq) {
io_req_task_queue_fail(req, -ECANCELED);
return;
}
+ /*
+ * The file is looked up as late as possible, as it may be the result
+ * of an earlier request of the link. This one is up now, and what kind
+ * of work it is depends on the file. If that fails, leave the error
+ * handling to the worker.
+ *
+ * Not for a splice: whatever it writes to, it may wait for its input
+ * for ever. It has to remain unbound work that isn't hashed.
+ */
+ if (req->opcode != IORING_OP_SPLICE)
+ io_assign_file(req, &io_issue_defs[req->opcode], 0);
+
/* init ->work of the whole link before punting */
io_prep_async_link(req);
@@ -729,6 +758,13 @@ bool io_cqe_cache_refill(struct io_ring_ctx *ctx, bool overflow, bool cqe32)
if (!overflow && (ctx->check_cq & BIT(IO_CHECK_CQ_OVERFLOW_BIT)))
return false;
+ /*
+ * A single entry may be left if a 32b CQE is asked for. It must not be
+ * handed out later on if this fails, as the CQE is going to overflow.
+ */
+ ctx->cqe_cached = NULL;
+ ctx->cqe_sentinel = NULL;
+
/*
* Post dummy CQE if a 32b CQE is needed and there's only room for a
* 16b CQE before the ring wraps.
@@ -1390,20 +1426,6 @@ static __cold void io_drain_req(struct io_kiocb *req)
ctx->int_flags &= ~IO_RING_F_DRAIN_ACTIVE;
}
-static bool io_assign_file(struct io_kiocb *req, const struct io_issue_def *def,
- unsigned int issue_flags)
-{
- if (req->file || !def->needs_file)
- return true;
-
- if (req->flags & REQ_F_FIXED_FILE)
- req->file = io_file_get_fixed(req, req->cqe.fd, issue_flags);
- else
- req->file = io_file_get_normal(req, req->cqe.fd);
-
- return !!req->file;
-}
-
#define REQ_ISSUE_SLOW_FLAGS (REQ_F_CREDS | REQ_F_ARM_LTIMEOUT)
static inline int __io_issue_sqe(struct io_kiocb *req,
@@ -1450,6 +1472,9 @@ static int io_issue_sqe(struct io_kiocb *req, unsigned int issue_flags)
ret = __io_issue_sqe(req, issue_flags, def);
if (ret == IOU_COMPLETE) {
+ /* 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
@@ -1464,6 +1489,8 @@ static int io_issue_sqe(struct io_kiocb *req, unsigned int issue_flags)
if (req->flags & REQ_F_IOPOLL)
io_iopoll_req_issued(req, issue_flags);
}
+ if (unlikely(ret <= -ERESTARTSYS))
+ ret = io_fixup_restart_res(ret);
return ret;
}
@@ -1486,28 +1513,40 @@ int io_poll_issue(struct io_kiocb *req, io_tw_token_t tw)
return ret;
}
-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)) {
- /*
- * Only continue the link from here if the completion has been
- * posted. If it's still pending, io_free_req() completes the
- * request and the next link follows from there, after the CQE.
- */
- if ((req->flags & IO_REQ_LINK_FLAGS) &&
- (req->flags & REQ_F_CQE_SKIP)) {
- struct io_ring_ctx *ctx = req->ctx;
+ *linked = NULL;
+ if (!req_ref_put_and_test_atomic(req))
+ return false;
- mutex_lock(&ctx->uring_lock);
- nxt = io_req_find_next(req);
- mutex_unlock(&ctx->uring_lock);
- }
- io_free_req(req);
+ /*
+ * Only continue the link from here if the completion has been posted.
+ * If it's still pending, io_free_req() completes the request and the
+ * next link follows from there, after the CQE.
+ */
+ if ((req->flags & IO_REQ_LINK_FLAGS) && (req->flags & REQ_F_CQE_SKIP)) {
+ struct io_ring_ctx *ctx = req->ctx;
+
+ mutex_lock(&ctx->uring_lock);
+ nxt = io_req_find_next(req);
+ mutex_unlock(&ctx->uring_lock);
}
- return nxt ? &nxt->work : NULL;
+ 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)
@@ -1518,7 +1557,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
@@ -1799,17 +1838,27 @@ static int io_init_req(struct io_ring_ctx *ctx, struct io_kiocb *req,
* A 128b op on a non-128b SQ requires mixed SQE support as
* well as 2 contiguous entries.
*/
- if (!(ctx->flags & IORING_SETUP_SQE_MIXED) || *left < 2 ||
+ if (!(ctx->flags & IORING_SETUP_SQE_MIXED) ||
(unsigned)(sqe - ctx->sq_sqes) >= ctx->sq_entries - 1)
return io_init_fail_req(req, -EINVAL);
+ if (*left >= 2) {
+ /*
+ * A 128b operation on a mixed SQ uses two entries, so
+ * we have to increment the head and cached refs, and
+ * decrement what's left.
+ */
+ current->io_uring->cached_refs++;
+ (*left)--;
+ } else if (!(ctx->flags & IORING_SETUP_SQPOLL) ||
+ !__io_sqring_entries(ctx)) {
+ return io_init_fail_req(req, -EINVAL);
+ }
/*
- * A 128b operation on a mixed SQ uses two entries, so we have
- * to increment the head and cached refs, and decrement what's
- * left.
+ * else: the SQPOLL thread caps the number of entries it submits
+ * in one go, which is none of the application's business. Take
+ * the 2nd entry on top.
*/
- current->io_uring->cached_refs++;
ctx->cached_sq_head++;
- (*left)--;
}
if (unlikely(sqe_flags & ~SQE_COMMON_FLAGS)) {
@@ -2336,6 +2385,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)
@@ -2350,7 +2400,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);
}
@@ -2518,6 +2569,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_lock(&ctx->tctx_lock);
}
diff --git a/io_uring/io_uring.h b/io_uring/io_uring.h
index 109261722ca74..203bf019e9319 100644
--- a/io_uring/io_uring.h
+++ b/io_uring/io_uring.h
@@ -204,7 +204,8 @@ int io_do_iopoll(struct io_ring_ctx *ctx, bool force_nonspin);
__cold void io_iopoll_try_reap_events(struct io_ring_ctx *ctx);
void __io_submit_flush_completions(struct io_ring_ctx *ctx);
-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);
@@ -318,6 +319,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 *io_uring_alloc_async_data(struct io_alloc_cache *cache,
struct io_kiocb *req)
{
@@ -380,6 +399,23 @@ 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
+ * ->uring_lock held, which the search needs as well, once the request is on
+ * the list: if the search came first, the flag is visible.
+ */
+static inline bool io_wq_cancel_seen(struct io_kiocb *req,
+ unsigned int issue_flags)
+{
+ lockdep_assert_held(&req->ctx->uring_lock);
+
+ return (issue_flags & IO_URING_F_IOWQ) &&
+ (atomic_read(&req->work.flags) & IO_WQ_WORK_CANCEL);
+}
+
static inline void io_commit_cqring(struct io_ring_ctx *ctx)
{
/* order cqe stores with ring update */
diff --git a/io_uring/kbuf.c b/io_uring/kbuf.c
index 6e2facf34bfa6..c9641b6ccc8ff 100644
--- a/io_uring/kbuf.c
+++ b/io_uring/kbuf.c
@@ -204,7 +204,7 @@ static struct io_br_sel io_ring_buffer_select(struct io_kiocb *req, size_t *len,
if (unlikely(tail == head))
return sel;
- if (head + 1 == tail)
+ if ((__u16)(head + 1) == tail)
req->flags |= REQ_F_BL_EMPTY;
buf = io_ring_head_to_buf(br, head, bl->mask);
diff --git a/io_uring/loop.c b/io_uring/loop.c
index 3d472b7827fac..2340459164a1a 100644
--- a/io_uring/loop.c
+++ b/io_uring/loop.c
@@ -10,12 +10,6 @@ static inline int io_loop_nr_cqes(const struct io_ring_ctx *ctx,
return lp->cq_wait_idx - READ_ONCE(ctx->rings->cq.tail);
}
-static inline void io_loop_wait_start(struct io_ring_ctx *ctx, unsigned nr_wait)
-{
- atomic_set(&ctx->cq_wait_nr, nr_wait);
- set_current_state(TASK_INTERRUPTIBLE);
-}
-
static inline void io_loop_wait_finish(struct io_ring_ctx *ctx)
{
__set_current_state(TASK_RUNNING);
@@ -25,7 +19,7 @@ static inline void io_loop_wait_finish(struct io_ring_ctx *ctx)
static void io_loop_wait(struct io_ring_ctx *ctx, struct iou_loop_params *lp,
unsigned nr_wait)
{
- io_loop_wait_start(ctx, nr_wait);
+ io_cq_wait_arm(ctx, nr_wait);
if (unlikely(io_local_work_pending(ctx) ||
io_loop_nr_cqes(ctx, lp) <= 0) ||
diff --git a/io_uring/mpscq.h b/io_uring/mpscq.h
index f910526766fd8..da9474497cc8e 100644
--- a/io_uring/mpscq.h
+++ b/io_uring/mpscq.h
@@ -25,9 +25,10 @@
* its predecessor. In between, the tail end of the queue is not yet reachable
* from the head. mpscq_pop() detects this and returns NULL, while mpscq_empty()
* reports false. The consumer must not treat such a NULL as "queue empty" - it
- * should retry later. The window is two instructions wide, but a producer can
- * be preempted inside it, so the consumer must not spin on it while holding
- * resources the producer might need to make progress.
+ * should retry later. The window is two instructions wide and the producer
+ * can't be preempted inside it, but it can be interrupted, so the consumer must
+ * not spin on it while holding resources the producer might need to make
+ * progress.
*
* The consumer side only supports a single consumer at a time, callers must
* provide their own serialization for it. The stub node is what allows the
@@ -69,6 +70,12 @@ static inline bool mpscq_push(struct mpscq *q, struct llist_node *node)
struct llist_node *prev;
node->next = NULL;
+ /*
+ * All the consumer can do about a node that isn't linked yet is to try
+ * again. If it is what preempted us, and we don't get to run as long
+ * as it does, that never ends.
+ */
+ preempt_disable();
/*
* xchg() implies a full barrier, so the initialization of the
* entry (including ->next above) is visible before the node can
@@ -77,6 +84,7 @@ static inline bool mpscq_push(struct mpscq *q, struct llist_node *node)
*/
prev = xchg(&q->tail, node);
WRITE_ONCE(prev->next, node);
+ preempt_enable();
return prev == &q->stub;
}
diff --git a/io_uring/napi.c b/io_uring/napi.c
index ca1c814fa73a2..7e698720cd51a 100644
--- a/io_uring/napi.c
+++ b/io_uring/napi.c
@@ -6,7 +6,7 @@
#ifdef CONFIG_NET_RX_BUSY_POLL
/* Timeout for cleanout of stale entries. */
-#define NAPI_TIMEOUT (60 * SEC_CONVERSION)
+#define NAPI_TIMEOUT (60 * HZ)
struct io_napi_entry {
unsigned int napi_id;
@@ -378,6 +378,8 @@ void __io_napi_busy_loop(struct io_ring_ctx *ctx, struct io_wait_queue *iowq)
if (iowq->timeout != KTIME_MAX) {
ktime_t dt = ktime_sub(iowq->timeout, io_get_time(ctx));
+ /* an absolute timeout may have passed already */
+ dt = max_t(ktime_t, dt, 0);
iowq->napi_busy_poll_dt = min_t(u64, iowq->napi_busy_poll_dt, dt);
}
diff --git a/io_uring/net.c b/io_uring/net.c
index f5f4f3ce6ef35..b1bf7dbec5d27 100644
--- a/io_uring/net.c
+++ b/io_uring/net.c
@@ -480,6 +480,24 @@ static int io_bundle_nbufs(struct io_async_msghdr *kmsg, int ret)
return nbufs;
}
+/*
+ * A receive that is retried after a partial transfer keeps writing to what it
+ * has mapped. io_net_kbuf_recyle() only commits what was filled so far, which
+ * takes a buffer out of circulation if it's a single one that is consumed as a
+ * whole. The tail of an incrementally consumed buffer, or the yet unused
+ * buffers of a bundle, would be up for grabs. They can't be committed in
+ * advance either, the CQE has no way to tell if they end up not being used.
+ */
+static bool io_net_kbuf_can_retry(struct io_kiocb *req,
+ struct io_buffer_list *bl,
+ struct io_async_msghdr *kmsg)
+{
+ /* not ring provided, or committed at selection time */
+ if (!(req->flags & REQ_F_BUFFERS_COMMIT))
+ return true;
+ return !(bl->flags & IOBL_INC) && iter_is_ubuf(&kmsg->msg.msg_iter);
+}
+
static int io_net_kbuf_recyle(struct io_kiocb *req, struct io_buffer_list *bl,
struct io_async_msghdr *kmsg, int len)
{
@@ -1091,7 +1109,8 @@ int io_recvmsg(struct io_kiocb *req, unsigned int issue_flags)
io_kbuf_recycle(req, sel.buf_list, issue_flags);
return IOU_RETRY;
}
- if (ret > 0 && io_net_retry(sock, flags)) {
+ if (ret > 0 && io_net_retry(sock, flags) &&
+ io_net_kbuf_can_retry(req, sel.buf_list, kmsg)) {
sr->done_io += ret;
return io_net_kbuf_recyle(req, sel.buf_list, kmsg, ret);
}
@@ -1252,7 +1271,8 @@ int io_recv(struct io_kiocb *req, unsigned int issue_flags)
io_kbuf_recycle(req, sel.buf_list, issue_flags);
return IOU_RETRY;
}
- if (ret > 0 && io_net_retry(sock, flags)) {
+ if (ret > 0 && io_net_retry(sock, flags) &&
+ io_net_kbuf_can_retry(req, sel.buf_list, kmsg)) {
sr->len -= ret;
sr->buf += ret;
sr->done_io += ret;
diff --git a/io_uring/openclose.c b/io_uring/openclose.c
index c71242915dade..24cf3ce2e2bc3 100644
--- a/io_uring/openclose.c
+++ b/io_uring/openclose.c
@@ -375,13 +375,16 @@ static int io_pipe_fixed(struct io_kiocb *req, struct file **files,
fds[1] = alloc_slot ? ret : slot - 1;
files[1] = NULL;
- io_ring_submit_unlock(ctx, issue_flags);
-
- if (!copy_to_user(p->fds, fds, sizeof(fds)))
+ /*
+ * Hang on to the lock. If this fails, the slots have to be cleared, and
+ * they must still hold what was put there.
+ */
+ if (!copy_to_user(p->fds, fds, sizeof(fds))) {
+ io_ring_submit_unlock(ctx, issue_flags);
return 0;
+ }
ret = -EFAULT;
- io_ring_submit_lock(ctx, issue_flags);
err:
if (fds[0] != -1)
io_fixed_fd_remove(ctx, fds[0]);
diff --git a/io_uring/poll.c b/io_uring/poll.c
index 5447a7c24dce6..7dcb904e8fc3e 100644
--- a/io_uring/poll.c
+++ b/io_uring/poll.c
@@ -617,6 +617,18 @@ static int __io_arm_poll_handler(struct io_kiocb *req,
io_poll_add_hash(req, issue_flags);
+ /*
+ * 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 ->uring_lock held, and
+ * the flag is set before the search. If the search came first,
+ * the flag is visible by now.
+ */
+ if (ipt->owning &&
+ (atomic_read(&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);
@@ -906,6 +918,15 @@ int io_poll_add(struct io_kiocb *req, unsigned int issue_flags)
ipt.pt._qproc = io_poll_queue_proc;
+ /*
+ * 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))
+ io_req_async_data_free(req);
+ 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);
diff --git a/io_uring/register.c b/io_uring/register.c
index dab3eb4258dff..2ce36623e25a6 100644
--- a/io_uring/register.c
+++ b/io_uring/register.c
@@ -496,6 +496,17 @@ static void io_register_free_rings(struct io_ring_ctx *ctx,
IORING_SETUP_CQE32 | IORING_SETUP_NO_MMAP | \
IORING_SETUP_CQE_MIXED | IORING_SETUP_SQE_MIXED)
+static bool io_sqe_is_128(struct io_ring_ctx *ctx,
+ const struct io_uring_sqe *sqe)
+{
+ u8 opcode = READ_ONCE(sqe->opcode);
+
+ if (!(ctx->flags & IORING_SETUP_SQE_MIXED) || opcode >= IORING_OP_LAST)
+ return false;
+ opcode = array_index_nospec(opcode, IORING_OP_LAST);
+ return io_issue_defs[opcode].is_128;
+}
+
static int io_register_resize_rings(struct io_ring_ctx *ctx, void __user *arg)
{
struct io_ctx_config config;
@@ -505,6 +516,8 @@ static int io_register_resize_rings(struct io_ring_ctx *ctx, void __user *arg)
struct io_uring_params *p = &config.p;
struct io_rings_layout *rl = &config.layout;
u32 *o_sq_array, *n_sq_array = NULL;
+ bool cqe32_tail = false;
+ unsigned int sqe128_src = 0;
int ret;
memset(&config, 0, sizeof(config));
@@ -603,13 +616,20 @@ static int io_register_resize_rings(struct io_ring_ctx *ctx, void __user *arg)
old_head = READ_ONCE(o.rings->sq.head);
if (tail - old_head > p->sq_entries)
goto overflow;
- for (i = old_head; i < tail; i++) {
+ for (i = old_head; i != tail; i++) {
+ bool sqe128_tail = sqe128_src;
unsigned int dst, src;
size_t sq_size;
dst = i & (p->sq_entries - 1);
src = i & (ctx->sq_entries - 1);
- if (n_sq_array) {
+ if (sqe128_tail) {
+ /* it follows the first half, whatever the array says */
+ src = sqe128_src;
+ sqe128_src = 0;
+ if (n_sq_array)
+ WRITE_ONCE(n_sq_array[dst], dst);
+ } else if (n_sq_array) {
src = READ_ONCE(o_sq_array[src]);
if (unlikely(src >= ctx->sq_entries)) {
WRITE_ONCE(n_sq_array[dst], UINT_MAX);
@@ -618,6 +638,19 @@ static int io_register_resize_rings(struct io_ring_ctx *ctx, void __user *arg)
WRITE_ONCE(n_sq_array[dst], dst);
}
+ /*
+ * A 128b SQE on a SQE_MIXED ring takes two contiguous entries
+ * and must not wrap. A smaller ring may place it on the last
+ * entry, fail the resize if that happens.
+ */
+ if (!sqe128_tail && i + 1 != tail &&
+ src < ctx->sq_entries - 1 &&
+ io_sqe_is_128(ctx, &o.sq_sqes[src])) {
+ if (dst == p->sq_entries - 1)
+ goto overflow;
+ sqe128_src = src + 1;
+ }
+
sq_size = sizeof(struct io_uring_sqe);
if (ctx->flags & IORING_SETUP_SQE128) {
dst <<= 1;
@@ -640,7 +673,7 @@ static int io_register_resize_rings(struct io_ring_ctx *ctx, void __user *arg)
ret = -EOVERFLOW;
goto out;
}
- for (i = old_head; i < tail; i++) {
+ for (i = old_head; i != tail; i++) {
unsigned index, dst_mask, src_mask;
size_t cq_size;
@@ -654,6 +687,21 @@ static int io_register_resize_rings(struct io_ring_ctx *ctx, void __user *arg)
src_mask = (ctx->cq_entries << 1) - 1;
dst_mask = (p->cq_entries << 1) - 1;
}
+ /*
+ * A 32b CQE on a CQE_MIXED ring takes two contiguous entries
+ * and must not wrap. A smaller ring may place it on the last
+ * entry, fail the resize if that happens.
+ */
+ if (ctx->flags & IORING_SETUP_CQE_MIXED) {
+ if (cqe32_tail) {
+ cqe32_tail = false;
+ } else if (READ_ONCE(o.rings->cqes[index & src_mask].flags) &
+ IORING_CQE_F_32) {
+ if ((index & dst_mask) == dst_mask)
+ goto overflow;
+ cqe32_tail = true;
+ }
+ }
memcpy(&n.rings->cqes[index & dst_mask], &o.rings->cqes[index & src_mask], cq_size);
}
WRITE_ONCE(n.rings->cq.head, old_head);
diff --git a/io_uring/rw.c b/io_uring/rw.c
index d33b925a848e5..a65693e393695 100644
--- a/io_uring/rw.c
+++ b/io_uring/rw.c
@@ -613,24 +613,6 @@ static void io_complete_rw_iopoll(struct kiocb *kiocb, long res)
smp_store_release(&req->iopoll_completed, 1);
}
-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 io_kiocb *req, ssize_t ret)
{
struct io_rw *rw = io_kiocb_to_cmd(req, struct io_rw);
diff --git a/io_uring/sqpoll.c b/io_uring/sqpoll.c
index ad42e8eb10029..ee97808a065a9 100644
--- a/io_uring/sqpoll.c
+++ b/io_uring/sqpoll.c
@@ -385,6 +385,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;
diff --git a/io_uring/timeout.c b/io_uring/timeout.c
index 9c239c0a17155..b219dfdf7af5a 100644
--- a/io_uring/timeout.c
+++ b/io_uring/timeout.c
@@ -321,22 +321,29 @@ static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer)
return HRTIMER_NORESTART;
}
-static struct io_kiocb *io_timeout_extract(struct io_ring_ctx *ctx,
- struct io_cancel_data *cd)
+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;
- 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);
+ struct io_kiocb *req = cmd_to_io_kiocb(timeout);
- if (io_cancel_req_match(tmp, cd)) {
- req = tmp;
- break;
- }
+ 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;
+
if (!req)
return ERR_PTR(-ENOENT);
@@ -386,6 +393,9 @@ static void io_req_task_link_timeout(struct io_tw_req tw_req, io_tw_token_t tw)
};
ret = io_try_cancel(req->tctx, &cd, 0);
+ /* a number of requests for futex and waitid */
+ if (ret > 0)
+ ret = 0;
} else {
ret = -ECANCELED;
}
@@ -458,6 +468,8 @@ static int io_linked_timeout_update(struct io_ring_ctx *ctx, __u64 user_data,
io = req->async_data;
if (hrtimer_try_to_cancel(&io->timer) == -1)
return -EALREADY;
+ if (mode == HRTIMER_MODE_ABS)
+ ts = timens_ktime_to_host(io_timeout_get_clock(io), ts);
hrtimer_setup(&io->timer, io_link_timeout_fn, io_timeout_get_clock(io), mode);
hrtimer_start(&io->timer, ts, mode);
return 0;
@@ -468,15 +480,26 @@ 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;
- timeout->off = 0; /* noseq */
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 */
+ if (mode == HRTIMER_MODE_ABS)
+ time = timens_ktime_to_host(io_timeout_get_clock(data), time);
data->time = time;
list_add_tail(&timeout->list, &ctx->timeout_list);
@@ -509,7 +532,12 @@ int io_timeout_remove_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
IORING_TIMEOUT_ABS |
IORING_TIMEOUT_IMMEDIATE_ARG))
return -EINVAL;
- ret = io_parse_user_time(&tr->time, READ_ONCE(sqe->addr2), tr->flags);
+ /*
+ * The clock is that of the timeout to update, an absolute time
+ * is converted to the host's view once that has been found.
+ */
+ ret = io_parse_user_time(&tr->time, READ_ONCE(sqe->addr2),
+ tr->flags & ~IORING_TIMEOUT_ABS);
if (ret)
return ret;
} else if (tr->flags) {
@@ -645,8 +673,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;
raw_spin_lock_irq(&ctx->timeout_lock);
@@ -663,11 +692,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
@@ -679,8 +714,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:
diff --git a/io_uring/tw.c b/io_uring/tw.c
index f9affd68dd263..0b5066f972245 100644
--- a/io_uring/tw.c
+++ b/io_uring/tw.c
@@ -148,6 +148,37 @@ static void io_ctx_mark_taskrun(struct io_ring_ctx *ctx)
}
}
+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.
+ */
+static void io_local_work_wake_poll(struct io_ring_ctx *ctx, unsigned int flags)
+{
+ if (!(flags & IOU_F_TWQ_IN_WAKE)) {
+ io_poll_wq_wake(ctx);
+ return;
+ }
+ /*
+ * Called from a wake up handler with some wait queue lock held, this
+ * one could lead back to it. Do it from a clean context.
+ */
+ 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);
+}
+
void io_req_local_work_add(struct io_kiocb *req, unsigned flags)
{
struct io_ring_ctx *ctx = req->ctx;
@@ -165,7 +196,7 @@ void io_req_local_work_add(struct io_kiocb *req, unsigned flags)
/*
* The xchg() in mpscq_push() implies a full barrier, which pairs with
- * the barrier in set_current_state() on the io_cqring_wait() side. This
+ * the barrier in io_cq_wait_arm() on the io_cqring_wait() side. This
* ensures that either we see the updated ->cq_wait_nr, or waiters going
* to sleep will observe the work added to the list, which is similar to
* the wait/wake task state sync.
@@ -174,6 +205,9 @@ void io_req_local_work_add(struct io_kiocb *req, unsigned flags)
io_ctx_mark_taskrun(ctx);
if (data_race(ctx->int_flags) & IO_RING_F_HAS_EVFD)
io_eventfd_signal(ctx, false, flags & IOU_F_TWQ_IN_WAKE);
+ /* whoever sets the flag wakes ->poll_wq afterwards */
+ if (data_race(ctx->int_flags) & IO_RING_F_POLL_ACTIVATED)
+ io_local_work_wake_poll(ctx, flags);
}
/*
diff --git a/io_uring/uring_cmd.c b/io_uring/uring_cmd.c
index 73b606899833f..b5486b56ee0c7 100644
--- a/io_uring/uring_cmd.c
+++ b/io_uring/uring_cmd.c
@@ -87,7 +87,7 @@ static void io_uring_cmd_del_cancelable(struct io_uring_cmd *cmd,
cmd->flags &= ~IORING_URING_CMD_CANCELABLE;
io_ring_submit_lock(ctx, issue_flags);
- hlist_del(&req->hash_node);
+ hlist_del_init(&req->hash_node);
io_ring_submit_unlock(ctx, issue_flags);
}
diff --git a/io_uring/wait.c b/io_uring/wait.c
index a48523c2f356b..b7a0292767c7c 100644
--- a/io_uring/wait.c
+++ b/io_uring/wait.c
@@ -26,7 +26,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)
@@ -76,9 +77,16 @@ static enum hrtimer_restart io_cqring_min_timer_wakeup(struct hrtimer *timer)
if (iowq->timeout == KTIME_MAX ||
ktime_compare(iowq->min_timeout, iowq->timeout) >= 0)
goto out_wake;
- /* work we may need to run, wake function will see if we need to wake */
- if (io_has_work(ctx))
- goto out_wake;
+ /*
+ * Work we need to run. It isn't known if that is going to post a CQE,
+ * so this isn't a timeout. Carry on with the normal one if it doesn't.
+ */
+ if (io_has_work(ctx)) {
+ iowq->min_timeout = 0;
+ iowq->cq_tail = iowq->cq_min_tail + 1;
+ wake_up_process(iowq->wq.private);
+ return HRTIMER_NORESTART;
+ }
/* got events since we started waiting, min timeout is done */
scoped_guard(rcu) {
struct io_rings *rings = io_get_rings(ctx);
@@ -257,8 +265,7 @@ int io_cqring_wait(struct io_ring_ctx *ctx, int min_events, u32 flags,
unsigned long check_cq;
if (ctx->flags & IORING_SETUP_DEFER_TASKRUN) {
- atomic_set(&ctx->cq_wait_nr, nr_wait);
- set_current_state(TASK_INTERRUPTIBLE);
+ io_cq_wait_arm(ctx, nr_wait);
} else {
prepare_to_wait_exclusive(&ctx->cq_wait, &iowq.wq,
TASK_INTERRUPTIBLE);
@@ -291,9 +298,18 @@ int io_cqring_wait(struct io_ring_ctx *ctx, int min_events, u32 flags,
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;
diff --git a/io_uring/wait.h b/io_uring/wait.h
index b7b9c46b1b013..eb5ae8a6fc942 100644
--- a/io_uring/wait.h
+++ b/io_uring/wait.h
@@ -14,6 +14,21 @@
*/
#define IO_CQ_WAKE_INIT (-1)
+/*
+ * Start a wait cycle of the current task. The add that uses up ->cq_wait_nr
+ * may be one that queued its entry long ago and only now gets to look at the
+ * count. As nobody else wakes us after that, it has to find us sleeping, hence
+ * the task state must be visible before the count is. The barrier pairs with
+ * the xchg() in mpscq_push(), see io_req_local_work_add().
+ */
+static inline void io_cq_wait_arm(struct io_ring_ctx *ctx, int nr_wait)
+{
+ __set_current_state(TASK_INTERRUPTIBLE);
+ atomic_set_release(&ctx->cq_wait_nr, nr_wait);
+ /* order the above against looking at the work list */
+ smp_mb();
+}
+
struct ext_arg {
size_t argsz;
struct timespec64 ts;
diff --git a/io_uring/waitid.c b/io_uring/waitid.c
index 2b756017423f0..2ba2369681d0e 100644
--- a/io_uring/waitid.c
+++ b/io_uring/waitid.c
@@ -265,12 +265,18 @@ static int io_waitid_wait(struct wait_queue_entry *wait, unsigned mode,
return 0;
list_del_init(&wait->entry);
- smp_store_release(&iw->head, NULL);
- /* cancel is in progress */
+ /*
+ * Cancel or issue is in progress. Leave ->head alone, so that whoever
+ * owns the request has to take the wait queue lock, which we hold,
+ * before it can complete and free it.
+ */
if (atomic_fetch_inc(&iw->refs) & IO_WAITID_REF_MASK)
return 1;
+ /* pairs with the smp_load_acquire() in io_waitid_remove_wq() */
+ smp_store_release(&iw->head, NULL);
+
req->io_task_work.func = io_waitid_cb;
__io_req_task_work_add(req, IOU_F_TWQ_IN_WAKE);
return 1;
@@ -343,6 +349,8 @@ int io_waitid(struct io_kiocb *req, unsigned int issue_flags)
* a waitqueue callback, or if someone cancels it.
*/
if (!io_waitid_drop_issue_ref(req)) {
+ if (io_wq_cancel_seen(req, issue_flags))
+ __io_waitid_cancel(req, true);
io_ring_submit_unlock(ctx, issue_flags);
return IOU_ISSUE_SKIP_COMPLETE;
}
diff --git a/io_uring/zcrx.c b/io_uring/zcrx.c
index cc9df7e64a407..6d7abb1636e7f 100644
--- a/io_uring/zcrx.c
+++ b/io_uring/zcrx.c
@@ -1645,7 +1645,7 @@ static bool io_zcrx_queue_cqe(struct io_kiocb *req, struct net_iov *niov,
cqe->flags |= IORING_CQE_F_32;
area = io_zcrx_iov_to_area(niov);
- offset = off + (net_iov_idx(niov) << ifq->niov_shift);
+ offset = off + ((u64)net_iov_idx(niov) << ifq->niov_shift);
rcqe = (struct io_uring_zcrx_cqe *)(cqe + 1);
rcqe->off = offset + zcrx_area_id_to_token(area->area_id);
rcqe->__pad = 0;
|
| 19/3 | 2026/10/16 00:52 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 23703 output: 34 thoughts: 2265 Thoughts: **KMSAN Evaluation for IO_uring Patch Series** Okay, let's break this down. The goal is to determine if this patch series warrants a dedicated KMSAN fuzzing session. That's a serious resource commitment, so we need to be very diligent. My approach is to meticulously analyze the changes, file by file, and identify potential areas where *uninitialized memory* could be introduced or exposed. KASAN and friends are excellent, but KMSAN shines when it comes to catching reads of uninitialized memory. So, that's my focus here. 1. `include/linux/io_uring_types.h`: Adding new fields to `io_ring_ctx` is worth a quick look, just to ensure that if these new fields are ever sent to userspace, they're properly initialized. Not a major red flag, but worth the check later when considering a potential `copy_to_user`. 2. `io_uring/cancel.c`, `io_uring/cancel.h`: Seems primarily focused on cancellation logic, flags, and `running` state. This seems more about thread safety and synchronization than exposing uninitialized memory. Still, I will need to be particularly careful if there are any `running` fields exposed to user space. 3. `io_uring/fdinfo.c`: This one's the standout, and this is where my alarm bells are ringing! We are handling `fdinfo` printing and reading CQEs for file descriptor information. The use of `seq_printf` and interaction with the user-visible CQ ring buffer (`r->cqes[...]`) needs to be inspected. *Specifically* the `__io_uring_show_fdinfo` function. Are we potentially outputting uninitialized kernel memory? This is where a lot of time will be spent. 4. `io_uring/futex.c`: The addition of `io_futex_prep_private` with `futex_hash_allocate_default` warrants scrutiny. We need to verify that whatever gets allocated is initialized before it's used. The same with `io_futexv_wait` and `io_futex_wait` - any potential data that is read back from them. 5. `io_uring/io-wq.c`: This change is around worker handling. It seems more about worker management than introducing uninitialized reads or writes. However, I have to examine the `next_work` tracking and `io_worker_retire` for any possible issues. 6. `io_uring/io_uring.c`, `io_uring/io_uring.h`: Changes to request initialization, CQE cache, and restart logic are good areas to scrutinize for uninitialized reads. `io_init_req` looks suspicious with the mixed SQE checks and head/cached_refs logic. 7. `io_uring/kbuf.c`: The overflow wrap in the cast here seems fine. 8. `io_uring/loop.c`, `io_uring/mpscq.h`: `preempt_disable()` / `preempt_enable()` around `xchg` and `WRITE_ONCE(prev->next, node);` is a concurrency thing but not an initialization issue, looks safe. 9. `io_uring/napi.c`: NAPI timeout change, safe. 10. `io_uring/net.c`: `io_net_kbuf_can_retry`. The retry path, need to check if there is uninitialized memory when handling partial receive retries. 11. `io_uring/openclose.c`: `io_pipe_fixed` - the "don't drop `ctx->uring_lock` before `copy_to_user(p->fds, fds, sizeof(fds))`" change is a synchronization fix, but that `copy_to_user` call is a MUST-CHECK. I must confirm that `fds` are initialized before that call! 12. `io_uring/poll.c`, `io_uring/register.c`: Looks fine, but still has to be check. 13. `io_uring/rw.c`: Looks safe now, the static was removed. 14. `io_uring/sqpoll.c`: The `IORING_SETUP_R_DISABLED` check is safe. 15. `io_uring/timeout.c`, `io_uring/tw.c`, `io_uring/uring_cmd.c`: Fine. 16. `io_uring/wait.c`, `io_uring/wait.h`: Return of 0 is safe. 17. `io_uring/waitid.c`, `io_uring/zcr |
| 20/3 | 2026/10/16 00:52 | tool | grepper |
0mArgs:{
"Expression": "io_pipe_fixed",
"PathPrefixes": [
"io_uring/openclose.c"
]
}Results: {
"Output": "io_uring/openclose.c=326=int io_pipe_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)\n--\nio_uring/openclose.c-342-\nio_uring/openclose.c:343:static int io_pipe_fixed(struct io_kiocb *req, struct file **files,\nio_uring/openclose.c-344-\t\t\t unsigned int issue_flags)\n--\nio_uring/openclose.c=426=int io_pipe(struct io_kiocb *req, unsigned int issue_flags)\n--\nio_uring/openclose.c-436-\tif (!!p-\u003efile_slot)\nio_uring/openclose.c:437:\t\tret = io_pipe_fixed(req, files, issue_flags);\nio_uring/openclose.c-438-\telse\n"
} |
| 21/3 | 2026/10/16 00:52 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 27263 output: 36 thoughts: 22 Thoughts: Here's my attempt at summarizing the thought process, tailored for an expert audience: **Analyzing `io_pipe_fixed` - Lines 343-400** Okay, I'm diving into the `io_pipe_fixed` module, specifically focusing on lines 343 through 400. My immediate goal is to understand how a particular function or set of functions is handling I/O operations and resource management within this region of code. Considering the 'fixed' in the name, I anticipate this segment likely deals with a *specific* issue. Let me start by reviewing the logic and identifying the key data structures and function calls, looking for hints. I'll pay attention to any error checking or error handling to look for any logic errors, or subtle edge-case handling. I'll need to figure out the inputs and outputs of the function, and look for how it handles buffer management to prevent buffer overflows or memory leaks. I want to see how data is being transferred, what locks are in place for concurrency, and how it deals with potential blocking operations, looking for optimization opportunities. I'll also try to infer the specific scenario this code addresses, because the name suggests this might be designed to tackle a problem with how the I/O pipe handles data. Let's see if there is any indication of data corruption, which would then point to a possible bug. |
| 22/3 | 2026/10/16 00:53 | tool | read-file |
0mArgs:{
"File": "io_uring/openclose.c",
"FirstLine": 343,
"LineCount": 60
}Results: {
"Contents": " 343:\tstatic int io_pipe_fixed(struct io_kiocb *req, struct file **files,\n 344:\t\t\t\t unsigned int issue_flags)\n 345:\t{\n 346:\t\tstruct io_pipe *p = io_kiocb_to_cmd(req, struct io_pipe);\n 347:\t\tstruct io_ring_ctx *ctx = req-\u003ectx;\n 348:\t\tbool alloc_slot;\n 349:\t\tint ret, fds[2] = { -1, -1 };\n 350:\t\tint slot = p-\u003efile_slot;\n 351:\t\n 352:\t\tif (p-\u003eflags \u0026 O_CLOEXEC)\n 353:\t\t\treturn -EINVAL;\n 354:\t\n 355:\t\talloc_slot = slot == IORING_FILE_INDEX_ALLOC;\n 356:\t\n 357:\t\tio_ring_submit_lock(ctx, issue_flags);\n 358:\t\n 359:\t\tret = __io_fixed_fd_install(ctx, files[0], slot);\n 360:\t\tif (ret \u003c 0)\n 361:\t\t\tgoto err;\n 362:\t\tfds[0] = alloc_slot ? ret : slot - 1;\n 363:\t\tfiles[0] = NULL;\n 364:\t\n 365:\t\t/*\n 366:\t\t * If a specific slot is given, next one will be used for\n 367:\t\t * the write side.\n 368:\t\t */\n 369:\t\tif (!alloc_slot)\n 370:\t\t\tslot++;\n 371:\t\n 372:\t\tret = __io_fixed_fd_install(ctx, files[1], slot);\n 373:\t\tif (ret \u003c 0)\n 374:\t\t\tgoto err;\n 375:\t\tfds[1] = alloc_slot ? ret : slot - 1;\n 376:\t\tfiles[1] = NULL;\n 377:\t\n 378:\t\t/*\n 379:\t\t * Hang on to the lock. If this fails, the slots have to be cleared, and\n 380:\t\t * they must still hold what was put there.\n 381:\t\t */\n 382:\t\tif (!copy_to_user(p-\u003efds, fds, sizeof(fds))) {\n 383:\t\t\tio_ring_submit_unlock(ctx, issue_flags);\n 384:\t\t\treturn 0;\n 385:\t\t}\n 386:\t\n 387:\t\tret = -EFAULT;\n 388:\terr:\n 389:\t\tif (fds[0] != -1)\n 390:\t\t\tio_fixed_fd_remove(ctx, fds[0]);\n 391:\t\tif (fds[1] != -1)\n 392:\t\t\tio_fixed_fd_remove(ctx, fds[1]);\n 393:\t\tio_ring_submit_unlock(ctx, issue_flags);\n 394:\t\treturn ret;\n 395:\t}\n 396:\t\n 397:\tstatic int io_pipe_fd(struct io_kiocb *req, struct file **files)\n 398:\t{\n 399:\t\tstruct io_pipe *p = io_kiocb_to_cmd(req, struct io_pipe);\n 400:\t\tint ret, fds[2] = { -1, -1 };\n 401:\t\n 402:\t\tret = __get_unused_fd_flags(p-\u003eflags, p-\u003enofile);\n"
} |
| 23/3 | 2026/10/16 00:53 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 28549 output: 36 thoughts: 165 Thoughts: **Analyzing Memory Initialization in io_uring** Okay, so I'm looking at this code and trying to suss out if there are any lurking memory initialization issues, specifically related to how file descriptors are being handled. In `io_pipe_fixed`, the `fds` array is declared and *both* elements are explicitly initialized to -1. Then, later those values are overwritten. The critical part is that the `copy_to_user` call is copying a well-defined `int[2]` with no padding; both `int` elements will have been initialized. I'm satisfied that this isn't a problem, since the patch only moved `io_ring_submit_unlock` and didn't touch the initialization logic. That's a relief. Now, I want to take a look at `io_register_resize_rings` in `io_uring/register.c`. I need to inspect the code more closely between lines 496 and 710 to ensure there's no subtle issue that I've missed. I'll focus on how ring buffers are allocated and registered, as those operations are often a source of memory-related bugs. |
| 24/3 | 2026/10/16 00:53 | tool | read-file |
0mArgs:{
"File": "io_uring/register.c",
"FirstLine": 595,
"LineCount": 100
}Results: {
"Contents": " 595:\t\t * mmap from seeing them, as we'll unmap them. Any attempt to mmap\n 596:\t\t * existing rings beyond this point will fail. Not that it could proceed\n 597:\t\t * at this point anyway, as the io_uring mmap side needs go grab the\n 598:\t\t * ctx-\u003emmap_lock as well. Likewise, hold the completion lock over the\n 599:\t\t * duration of the actual swap.\n 600:\t\t */\n 601:\t\tmutex_lock(\u0026ctx-\u003emmap_lock);\n 602:\t\tspin_lock(\u0026ctx-\u003ecompletion_lock);\n 603:\t\to.rings = ctx-\u003erings;\n 604:\t\tctx-\u003erings = NULL;\n 605:\t\to.sq_sqes = ctx-\u003esq_sqes;\n 606:\t\tctx-\u003esq_sqes = NULL;\n 607:\t\to_sq_array = ctx-\u003esq_array;\n 608:\t\tif (!(ctx-\u003eflags \u0026 IORING_SETUP_NO_SQARRAY))\n 609:\t\t\tn_sq_array = (u32 *)((char *)n.rings + rl-\u003esq_array_offset);\n 610:\t\n 611:\t\t/*\n 612:\t\t * Now copy SQ and CQ entries, if any. If either of the destination\n 613:\t\t * rings can't hold what is already there, then fail the operation.\n 614:\t\t */\n 615:\t\ttail = READ_ONCE(o.rings-\u003esq.tail);\n 616:\t\told_head = READ_ONCE(o.rings-\u003esq.head);\n 617:\t\tif (tail - old_head \u003e p-\u003esq_entries)\n 618:\t\t\tgoto overflow;\n 619:\t\tfor (i = old_head; i != tail; i++) {\n 620:\t\t\tbool sqe128_tail = sqe128_src;\n 621:\t\t\tunsigned int dst, src;\n 622:\t\t\tsize_t sq_size;\n 623:\t\n 624:\t\t\tdst = i \u0026 (p-\u003esq_entries - 1);\n 625:\t\t\tsrc = i \u0026 (ctx-\u003esq_entries - 1);\n 626:\t\t\tif (sqe128_tail) {\n 627:\t\t\t\t/* it follows the first half, whatever the array says */\n 628:\t\t\t\tsrc = sqe128_src;\n 629:\t\t\t\tsqe128_src = 0;\n 630:\t\t\t\tif (n_sq_array)\n 631:\t\t\t\t\tWRITE_ONCE(n_sq_array[dst], dst);\n 632:\t\t\t} else if (n_sq_array) {\n 633:\t\t\t\tsrc = READ_ONCE(o_sq_array[src]);\n 634:\t\t\t\tif (unlikely(src \u003e= ctx-\u003esq_entries)) {\n 635:\t\t\t\t\tWRITE_ONCE(n_sq_array[dst], UINT_MAX);\n 636:\t\t\t\t\tcontinue;\n 637:\t\t\t\t}\n 638:\t\t\t\tWRITE_ONCE(n_sq_array[dst], dst);\n 639:\t\t\t}\n 640:\t\n 641:\t\t\t/*\n 642:\t\t\t * A 128b SQE on a SQE_MIXED ring takes two contiguous entries\n 643:\t\t\t * and must not wrap. A smaller ring may place it on the last\n 644:\t\t\t * entry, fail the resize if that happens.\n 645:\t\t\t */\n 646:\t\t\tif (!sqe128_tail \u0026\u0026 i + 1 != tail \u0026\u0026\n 647:\t\t\t src \u003c ctx-\u003esq_entries - 1 \u0026\u0026\n 648:\t\t\t io_sqe_is_128(ctx, \u0026o.sq_sqes[src])) {\n 649:\t\t\t\tif (dst == p-\u003esq_entries - 1)\n 650:\t\t\t\t\tgoto overflow;\n 651:\t\t\t\tsqe128_src = src + 1;\n 652:\t\t\t}\n 653:\t\n 654:\t\t\tsq_size = sizeof(struct io_uring_sqe);\n 655:\t\t\tif (ctx-\u003eflags \u0026 IORING_SETUP_SQE128) {\n 656:\t\t\t\tdst \u003c\u003c= 1;\n 657:\t\t\t\tsrc \u003c\u003c= 1;\n 658:\t\t\t\tsq_size \u003c\u003c= 1;\n 659:\t\t\t}\n 660:\t\t\tmemcpy(\u0026n.sq_sqes[dst], \u0026o.sq_sqes[src], sq_size);\n 661:\t\t}\n 662:\t\tWRITE_ONCE(n.rings-\u003esq.head, old_head);\n 663:\t\tWRITE_ONCE(n.rings-\u003esq.tail, tail);\n 664:\t\n 665:\t\ttail = READ_ONCE(o.rings-\u003ecq.tail);\n 666:\t\told_head = READ_ONCE(o.rings-\u003ecq.head);\n 667:\t\tif (tail - old_head \u003e p-\u003ecq_entries) {\n 668:\toverflow:\n 669:\t\t\t/* restore old rings, and return -EOVERFLOW via cleanup path */\n 670:\t\t\tctx-\u003erings = o.rings;\n 671:\t\t\tctx-\u003esq_sqes = o.sq_sqes;\n 672:\t\t\tto_free = \u0026n;\n 673:\t\t\tret = -EOVERFLOW;\n 674:\t\t\tgoto out;\n 675:\t\t}\n 676:\t\tfor (i = old_head; i != tail; i++) {\n 677:\t\t\tunsigned index, dst_mask, src_mask;\n 678:\t\t\tsize_t cq_size;\n 679:\t\n 680:\t\t\tindex = i;\n 681:\t\t\tcq_size = sizeof(struct io_uring_cqe);\n 682:\t\t\tsrc_mask = ctx-\u003ecq_entries - 1;\n 683:\t\t\tdst_mask = p-\u003ecq_entries - 1;\n 684:\t\t\tif (ctx-\u003eflags \u0026 IORING_SETUP_CQE32) {\n 685:\t\t\t\tindex \u003c\u003c= 1;\n 686:\t\t\t\tcq_size \u003c\u003c= 1;\n 687:\t\t\t\tsrc_mask = (ctx-\u003ecq_entries \u003c\u003c 1) - 1;\n 688:\t\t\t\tdst_mask = (p-\u003ecq_entries \u003c\u003c 1) - 1;\n 689:\t\t\t}\n 690:\t\t\t/*\n 691:\t\t\t * A 32b CQE on a CQE_MIXED ring takes two contiguous entries\n 692:\t\t\t * and must not wrap. A smaller ring may place it on the last\n 693:\t\t\t * entry, fail the resize if that happens.\n 694:\t\t\t */\n"
} |
| 25/3 | 2026/10/16 00:53 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 30742 output: 35 thoughts: 23 Thoughts: Okay, let's dive into this. Here's my take on those lines of code: **Analyzing `register.c` - Lines 690-720** Alright, I'm heading down into `register.c` now, specifically focusing on lines 690 through 720. My initial thought is to quickly identify the surrounding context. What's happening in this section? What function or process is it a part of? I'm already anticipating that this block likely handles some aspect of registration, maybe related to user accounts, device authentication, or module initialization. I’ll be looking for keywords like "validation," "permission," "authentication," or similar terms that would provide clues to its intended function. I'll be paying close attention to any conditional statements, like `if` or `else` blocks, which would indicate different paths of execution based on specific conditions. Are there any error checks in there? Specific return codes or error messages? That will give me an insight into the error handling implemented. I'll need to know whether exceptions are handled correctly, or if that part requires a review. Also, I'll be looking for any loops. This could be checking for a correct key, or iterating over a list of users, or similar such operations. I'll be particularly watchful for any potential security vulnerabilities, like buffer overflows or race conditions, given the nature of registration processes. It's likely that a key or password is being verified here. I'll analyze any function calls within this block. Knowing where those other functions are defined and what they do is crucial to understanding the entire logic. I'll need to know the parameters and how they are handled, and how those functions might affect the current code block. Overall, I'm expecting this section to be relatively low-level and fairly specific. I'm keen to isolate any relevant information, and determine whether it follows accepted best practices. |
| 26/3 | 2026/10/16 00:53 | tool | read-file |
0mArgs:{
"File": "io_uring/register.c",
"FirstLine": 690,
"LineCount": 40
}Results: {
"Contents": " 690:\t\t\t/*\n 691:\t\t\t * A 32b CQE on a CQE_MIXED ring takes two contiguous entries\n 692:\t\t\t * and must not wrap. A smaller ring may place it on the last\n 693:\t\t\t * entry, fail the resize if that happens.\n 694:\t\t\t */\n 695:\t\t\tif (ctx-\u003eflags \u0026 IORING_SETUP_CQE_MIXED) {\n 696:\t\t\t\tif (cqe32_tail) {\n 697:\t\t\t\t\tcqe32_tail = false;\n 698:\t\t\t\t} else if (READ_ONCE(o.rings-\u003ecqes[index \u0026 src_mask].flags) \u0026\n 699:\t\t\t\t\t IORING_CQE_F_32) {\n 700:\t\t\t\t\tif ((index \u0026 dst_mask) == dst_mask)\n 701:\t\t\t\t\t\tgoto overflow;\n 702:\t\t\t\t\tcqe32_tail = true;\n 703:\t\t\t\t}\n 704:\t\t\t}\n 705:\t\t\tmemcpy(\u0026n.rings-\u003ecqes[index \u0026 dst_mask], \u0026o.rings-\u003ecqes[index \u0026 src_mask], cq_size);\n 706:\t\t}\n 707:\t\tWRITE_ONCE(n.rings-\u003ecq.head, old_head);\n 708:\t\tWRITE_ONCE(n.rings-\u003ecq.tail, tail);\n 709:\t\t/* invalidate cached cqe refill */\n 710:\t\tctx-\u003ecqe_cached = ctx-\u003ecqe_sentinel = NULL;\n 711:\t\n 712:\t\tWRITE_ONCE(n.rings-\u003esq_dropped, READ_ONCE(o.rings-\u003esq_dropped));\n 713:\t\tatomic_set(\u0026n.rings-\u003esq_flags, atomic_read(\u0026o.rings-\u003esq_flags));\n 714:\t\tWRITE_ONCE(n.rings-\u003ecq_flags, READ_ONCE(o.rings-\u003ecq_flags));\n 715:\t\tWRITE_ONCE(n.rings-\u003ecq_overflow, READ_ONCE(o.rings-\u003ecq_overflow));\n 716:\t\n 717:\t\t/* all done, store old pointers and assign new ones */\n 718:\t\tif (n_sq_array)\n 719:\t\t\tctx-\u003esq_array = n_sq_array;\n 720:\t\n 721:\t\tctx-\u003esq_entries = p-\u003esq_entries;\n 722:\t\tctx-\u003ecq_entries = p-\u003ecq_entries;\n 723:\t\n 724:\t\t/*\n 725:\t\t * Just mark any flag we may have missed and that the application\n 726:\t\t * should act on unconditionally. Worst case it'll be an extra\n 727:\t\t * syscall.\n 728:\t\t */\n 729:\t\tatomic_or(IORING_SQ_TASKRUN | IORING_SQ_NEED_WAKEUP, \u0026n.rings-\u003esq_flags);\n"
} |
| 27/3 | 2026/10/16 00:53 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 31962 output: 35 thoughts: 55 Thoughts: Here's my analysis of what I'm thinking: **Examining Ring Buffer Allocation in io_uring** Alright, let's dive into this ring buffer allocation business. My immediate concern is how `n.rings` and `n.sq_sqes` were actually allocated within the `io_register_resize_rings` function. Understanding this is crucial for performance tuning and potential debugging down the line. To get a clear picture, I'll need to dissect the code. My next step is to head straight to the source. Specifically, I need to get intimately familiar with lines 510 through 594 in `io_uring/register.c`. That chunk should give me the details I'm after, revealing the nitty-gritty of the memory allocation process for these core structures. I need to understand the constraints and potential areas for optimization within that code block. |
| 28/3 | 2026/10/16 00:53 | tool | read-file |
0mArgs:{
"File": "io_uring/register.c",
"FirstLine": 515,
"LineCount": 80
}Results: {
"Contents": " 515:\t\tunsigned i, tail, old_head;\n 516:\t\tstruct io_uring_params *p = \u0026config.p;\n 517:\t\tstruct io_rings_layout *rl = \u0026config.layout;\n 518:\t\tu32 *o_sq_array, *n_sq_array = NULL;\n 519:\t\tbool cqe32_tail = false;\n 520:\t\tunsigned int sqe128_src = 0;\n 521:\t\tint ret;\n 522:\t\n 523:\t\tmemset(\u0026config, 0, sizeof(config));\n 524:\t\n 525:\t\t/* limited to DEFER_TASKRUN for now */\n 526:\t\tif (!(ctx-\u003eflags \u0026 IORING_SETUP_DEFER_TASKRUN))\n 527:\t\t\treturn -EINVAL;\n 528:\t\tif (copy_from_user(p, arg, sizeof(*p)))\n 529:\t\t\treturn -EFAULT;\n 530:\t\tif (p-\u003eflags \u0026 ~RESIZE_FLAGS)\n 531:\t\t\treturn -EINVAL;\n 532:\t\n 533:\t\t/* properties that are always inherited */\n 534:\t\tp-\u003eflags |= (ctx-\u003eflags \u0026 COPY_FLAGS);\n 535:\t\n 536:\t\tret = io_prepare_config(\u0026config);\n 537:\t\tif (unlikely(ret))\n 538:\t\t\treturn ret;\n 539:\t\n 540:\t\tmemset(\u0026rd, 0, sizeof(rd));\n 541:\t\trd.size = PAGE_ALIGN(rl-\u003erings_size);\n 542:\t\tif (p-\u003eflags \u0026 IORING_SETUP_NO_MMAP) {\n 543:\t\t\trd.user_addr = p-\u003ecq_off.user_addr;\n 544:\t\t\trd.flags |= IORING_MEM_REGION_TYPE_USER;\n 545:\t\t}\n 546:\t\tret = io_create_region(NULL, \u0026n.ring_region, \u0026rd, IORING_OFF_CQ_RING);\n 547:\t\tif (ret)\n 548:\t\t\treturn ret;\n 549:\t\n 550:\t\tn.rings = io_region_get_ptr(\u0026n.ring_region);\n 551:\t\n 552:\t\t/*\n 553:\t\t * At this point n.rings is shared with userspace, just like o.rings\n 554:\t\t * is as well. While we don't expect userspace to modify it while\n 555:\t\t * a resize is in progress, and it's most likely that userspace will\n 556:\t\t * shoot itself in the foot if it does, we can't always assume good\n 557:\t\t * intent... Use read/write once helpers from here on to indicate the\n 558:\t\t * shared nature of it.\n 559:\t\t */\n 560:\t\tWRITE_ONCE(n.rings-\u003esq_ring_mask, p-\u003esq_entries - 1);\n 561:\t\tWRITE_ONCE(n.rings-\u003ecq_ring_mask, p-\u003ecq_entries - 1);\n 562:\t\tWRITE_ONCE(n.rings-\u003esq_ring_entries, p-\u003esq_entries);\n 563:\t\tWRITE_ONCE(n.rings-\u003ecq_ring_entries, p-\u003ecq_entries);\n 564:\t\n 565:\t\tif (copy_to_user(arg, p, sizeof(*p))) {\n 566:\t\t\tio_register_free_rings(ctx, \u0026n);\n 567:\t\t\treturn -EFAULT;\n 568:\t\t}\n 569:\t\n 570:\t\tmemset(\u0026rd, 0, sizeof(rd));\n 571:\t\trd.size = PAGE_ALIGN(rl-\u003esq_size);\n 572:\t\tif (p-\u003eflags \u0026 IORING_SETUP_NO_MMAP) {\n 573:\t\t\trd.user_addr = p-\u003esq_off.user_addr;\n 574:\t\t\trd.flags |= IORING_MEM_REGION_TYPE_USER;\n 575:\t\t}\n 576:\t\tret = io_create_region(NULL, \u0026n.sq_region, \u0026rd, IORING_OFF_SQES);\n 577:\t\tif (ret) {\n 578:\t\t\tio_register_free_rings(ctx, \u0026n);\n 579:\t\t\treturn ret;\n 580:\t\t}\n 581:\t\tn.sq_sqes = io_region_get_ptr(\u0026n.sq_region);\n 582:\t\n 583:\t\t/*\n 584:\t\t * If using SQPOLL, park the thread\n 585:\t\t */\n 586:\t\tif (ctx-\u003esq_data) {\n 587:\t\t\tmutex_unlock(\u0026ctx-\u003euring_lock);\n 588:\t\t\tio_sq_thread_park(ctx-\u003esq_data);\n 589:\t\t\tmutex_lock(\u0026ctx-\u003euring_lock);\n 590:\t\t}\n 591:\t\n 592:\t\t/*\n 593:\t\t * We'll do the swap. Grab the ctx-\u003emmap_lock, which will exclude\n 594:\t\t * any new mmap's on the ring fd. Clear out existing mappings to prevent\n"
} |
| 29/3 | 2026/10/16 00:53 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 33611 output: 30 thoughts: 1916 Thoughts: **Analysis of Potential Uninitialized Memory Issues in io_uring Patch**
Okay, let's break this down. My initial concern is always uninitialized memory, which often leads to security vulnerabilities. Let's see if this patch touches any sensitive areas.
First, the note about `p` being `config.p` is good; it's being properly initialized with `memset` and `copy_from_user` before further field settings. The patch doesn't modify this, so we're good there.
Now, `register.c` and mixed 128/32-byte SQEs/CQEs are being handled correctly with `memcpy`. `n.sq_sqes` and `n.rings` are allocated with `io_create_region()`, which uses zeroed memory. We're safe as long as the size calculations are right. The original rings `o.rings` and `o.sq_sqes` are user-accessible which is already handled in previous code.
Moving on to `io_uring/fdinfo.c`. The main change here is to correctly handle the `IORING_SETUP_CQE32` flag. The old logic had an off-by-one in the `cqe` pointer calculation, and also introduced the `two_entries` flag that correctly handled `CQE_MIXED`. The new code handles the different CQE sizes and potential "double entries" with the IORING_CQE_F_32 flag properly. It seems to have fixed a previous bug, and the indexing into the shared ring buffer, populated by kernel or mapped by userspace is now being done correctly.
In `zcrx.c`, the fix for `net_iov_idx` integer overflow using `u64` prevents an issue where buffer offsets could become incorrect, and therefore prevent out-of-bounds writes. The code explicitly clears `rcqe->__pad` to ensure no stale data remains, which is important. This is a good fix.
The addition of `io_net_kbuf_can_retry` in `io_uring/net.c` is about network receive handling and the retrying mechanism. It seems to correctly manage buffer re-use based on `REQ_F_BUFFERS_COMMIT` and `IOBL_INC` flags. It is also good to check if buffers are user-space buffers or not. This is related to buffer tracking and how it will be reused.
Finally, in `io_uring/timeout.c`, the `io_cancel_data` struct is being used. Here, the initialization of `cd` with `{ .ctx = ctx, .data = user_data, };` is where I have to be careful. The patch added a `running` field, which isn't initialized explicitly in all cases. I'm going to scan all uses of `struct io_cancel_data` across `io_uring/` to make sure the compiler doesn't introduce a potential leak. This is a critical point; uninitialized `running` could introduce an exploitable race condition or information leak if it's subsequently read before being properly set.
In summary, most of the changes seem to address either bug fixes or functionality improvements with appropriate attention to initialization, memory allocation, and copy operations. However, the `io_cancel_data` struct's initialization requires careful review to ensure `running` is always properly handled, and I need to do a full scan of the codebase.
|
| 30/3 | 2026/10/16 00:53 | tool | grepper |
0mArgs:{
"Expression": "struct io_cancel_data",
"PathPrefixes": [
"io_uring"
]
}Results: {
"Output": "io_uring/cancel.c=24=struct io_cancel {\n--\nio_uring/cancel.c-38- */\nio_uring/cancel.c:39:bool io_cancel_req_match(struct io_kiocb *req, struct io_cancel_data *cd)\nio_uring/cancel.c-40-{\n--\nio_uring/cancel.c=70=static bool io_cancel_cb(struct io_wq_work *work, void *data)\n--\nio_uring/cancel.c-72-\tstruct io_kiocb *req = container_of(work, struct io_kiocb, work);\nio_uring/cancel.c:73:\tstruct io_cancel_data *cd = data;\nio_uring/cancel.c-74-\n--\nio_uring/cancel.c=87=static int io_async_cancel_one(struct io_uring_task *tctx,\nio_uring/cancel.c:88:\t\t\t struct io_cancel_data *cd)\nio_uring/cancel.c-89-{\n--\nio_uring/cancel.c-113-\nio_uring/cancel.c:114:int io_try_cancel(struct io_uring_task *tctx, struct io_cancel_data *cd,\nio_uring/cancel.c-115-\t\t unsigned issue_flags)\n--\nio_uring/cancel.c=151=int io_async_cancel_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)\n--\nio_uring/cancel.c-184-\nio_uring/cancel.c:185:static int __io_async_cancel(struct io_cancel_data *cd,\nio_uring/cancel.c-186-\t\t\t struct io_uring_task *tctx,\n--\nio_uring/cancel.c=233=int io_async_cancel(struct io_kiocb *req, unsigned int issue_flags)\n--\nio_uring/cancel.c-235-\tstruct io_cancel *cancel = io_kiocb_to_cmd(req, struct io_cancel);\nio_uring/cancel.c:236:\tstruct io_cancel_data cd = {\nio_uring/cancel.c-237-\t\t.ctx\t= req-\u003ectx,\n--\nio_uring/cancel.c=270=static int __io_sync_cancel(struct io_uring_task *tctx,\nio_uring/cancel.c:271:\t\t\t struct io_cancel_data *cd, int fd)\nio_uring/cancel.c-272-{\n--\nio_uring/cancel.c=291=int io_sync_cancel(struct io_ring_ctx *ctx, void __user *arg)\n--\nio_uring/cancel.c-293-{\nio_uring/cancel.c:294:\tstruct io_cancel_data cd = {\nio_uring/cancel.c-295-\t\t.ctx\t= ctx,\n--\nio_uring/cancel.c=380=bool io_cancel_remove_all(struct io_ring_ctx *ctx, struct io_uring_task *tctx,\n--\nio_uring/cancel.c-400-\nio_uring/cancel.c:401:int io_cancel_remove(struct io_ring_ctx *ctx, struct io_cancel_data *cd,\nio_uring/cancel.c-402-\t\t unsigned int issue_flags, struct hlist_head *list,\n--\nio_uring/cancel.h-6-\nio_uring/cancel.h:7:struct io_cancel_data {\nio_uring/cancel.h-8-\tstruct io_ring_ctx *ctx;\n--\nio_uring/cancel.h=19=int io_async_cancel(struct io_kiocb *req, unsigned int issue_flags);\nio_uring/cancel.h-20-\nio_uring/cancel.h:21:int io_try_cancel(struct io_uring_task *tctx, struct io_cancel_data *cd,\nio_uring/cancel.h-22-\t\t unsigned int issue_flags);\n--\nio_uring/cancel.h=24=int io_sync_cancel(struct io_ring_ctx *ctx, void __user *arg);\nio_uring/cancel.h:25:bool io_cancel_req_match(struct io_kiocb *req, struct io_cancel_data *cd);\nio_uring/cancel.h-26-bool io_match_task_safe(struct io_kiocb *head, struct io_uring_task *tctx,\n--\nio_uring/cancel.h=29=bool io_cancel_remove_all(struct io_ring_ctx *ctx, struct io_uring_task *tctx,\n--\nio_uring/cancel.h-31-\t\t\t bool (*cancel)(struct io_kiocb *));\nio_uring/cancel.h:32:int io_cancel_remove(struct io_ring_ctx *ctx, struct io_cancel_data *cd,\nio_uring/cancel.h-33-\t\t unsigned int issue_flags, struct hlist_head *list,\n--\nio_uring/futex.c=92=static bool __io_futex_cancel(struct io_kiocb *req)\n--\nio_uring/futex.c-114-\nio_uring/futex.c:115:int io_futex_cancel(struct io_ring_ctx *ctx, struct io_cancel_data *cd,\nio_uring/futex.c-116-\t\t unsigned int issue_flags)\n--\nio_uring/futex.h=10=int io_futex_wake(struct io_kiocb *req, unsigned int issue_flags);\n--\nio_uring/futex.h-12-#if defined(CONFIG_FUTEX)\nio_uring/futex.h:13:int io_futex_cancel(struct io_ring_ctx *ctx, struct io_cancel_data *cd,\nio_uring/futex.h-14-\t\t unsigned int issue_flags);\n--\nio_uring/futex.h=20=static inline int io_futex_cancel(struct io_ring_ctx *ctx,\nio_uring/futex.h:21:\t\t\t\t struct io_cancel_data *cd,\nio_uring/futex.h-22-\t\t\t\t unsigned int issue_flags)\n--\nio_uring/poll.c=771=static struct io_kiocb *io_poll_find(struct io_ring_ctx *ctx, bool poll_only,\nio_uring/poll.c:772:\t\t\t\t struct io_cancel_data *cd)\nio_uring/poll.c-773-{\n--\nio_uring/poll.c=792=static struct io_kiocb *io_poll_file_find(struct io_ring_ctx *ctx,\nio_uring/poll.c:793:\t\t\t\t\t struct io_cancel_data *cd)\nio_uring/poll.c-794-{\n--\nio_uring/poll.c=810=static int io_poll_disarm(struct io_kiocb *req)\n--\nio_uring/poll.c-820-\nio_uring/poll.c:821:static int __io_poll_cancel(struct io_ring_ctx *ctx, struct io_cancel_data *cd)\nio_uring/poll.c-822-{\n--\nio_uring/poll.c-837-\nio_uring/poll.c:838:int io_poll_cancel(struct io_ring_ctx *ctx, struct io_cancel_data *cd,\nio_uring/poll.c-839-\t\t unsigned issue_flags)\n--\nio_uring/poll.c=938=int io_poll_remove(struct io_kiocb *req, unsigned int issue_flags)\n--\nio_uring/poll.c-941-\tstruct io_ring_ctx *ctx = req-\u003ectx;\nio_uring/poll.c:942:\tstruct io_cancel_data cd = { .ctx = ctx, .data = poll_update-\u003eold_user_data, };\nio_uring/poll.c-943-\tstruct io_kiocb *preq;\n--\nio_uring/poll.h=39=int io_poll_remove(struct io_kiocb *req, unsigned int issue_flags);\nio_uring/poll.h-40-\nio_uring/poll.h:41:struct io_cancel_data;\nio_uring/poll.h:42:int io_poll_cancel(struct io_ring_ctx *ctx, struct io_cancel_data *cd,\nio_uring/poll.h-43-\t\t unsigned issue_flags);\n--\nio_uring/timeout.c=324=static struct io_kiocb *io_timeout_find(struct io_ring_ctx *ctx,\nio_uring/timeout.c:325:\t\t\t\t\tstruct io_cancel_data *cd)\nio_uring/timeout.c-326-\t__must_hold(\u0026ctx-\u003etimeout_lock)\n--\nio_uring/timeout.c=339=static struct io_kiocb *io_timeout_extract(struct io_ring_ctx *ctx,\nio_uring/timeout.c:340:\t\t\t\t\t struct io_cancel_data *cd)\nio_uring/timeout.c-341-\t__must_hold(\u0026ctx-\u003etimeout_lock)\n--\nio_uring/timeout.c-357-\nio_uring/timeout.c:358:int io_timeout_cancel(struct io_ring_ctx *ctx, struct io_cancel_data *cd)\nio_uring/timeout.c-359-\t__must_hold(\u0026ctx-\u003ecompletion_lock)\n--\nio_uring/timeout.c=373=static void io_req_task_link_timeout(struct io_tw_req tw_req, io_tw_token_t tw)\n--\nio_uring/timeout.c-389-\t\tif (!tw.cancel) {\nio_uring/timeout.c:390:\t\t\tstruct io_cancel_data cd = {\nio_uring/timeout.c-391-\t\t\t\t.ctx\t\t= req-\u003ectx,\n--\nio_uring/timeout.c=478=static int io_timeout_update(struct io_ring_ctx *ctx, __u64 user_data,\n--\nio_uring/timeout.c-481-{\nio_uring/timeout.c:482:\tstruct io_cancel_data cd = { .ctx = ctx, .data = user_data, };\nio_uring/timeout.c-483-\tstruct io_kiocb *req = io_timeout_find(ctx, \u0026cd);\n--\nio_uring/timeout.c=560=int io_timeout_remove(struct io_kiocb *req, unsigned int issue_flags)\n--\nio_uring/timeout.c-566-\tif (!(tr-\u003eflags \u0026 IORING_TIMEOUT_UPDATE)) {\nio_uring/timeout.c:567:\t\tstruct io_cancel_data cd = { .ctx = ctx, .data = tr-\u003eaddr, };\nio_uring/timeout.c-568-\n--\nio_uring/timeout.h=11=__cold void io_flush_timeouts(struct io_ring_ctx *ctx);\nio_uring/timeout.h:12:struct io_cancel_data;\nio_uring/timeout.h:13:int io_timeout_cancel(struct io_ring_ctx *ctx, struct io_cancel_data *cd);\nio_uring/timeout.h-14-__cold unsigned int io_timeouts_armed(struct io_ring_ctx *ctx,\n--\nio_uring/waitid.c=175=static bool io_waitid_cancel_nocopy_cb(struct io_kiocb *req)\n--\nio_uring/waitid.c-179-\nio_uring/waitid.c:180:int io_waitid_cancel(struct io_ring_ctx *ctx, struct io_cancel_data *cd,\nio_uring/waitid.c-181-\t\t unsigned int issue_flags)\n--\nio_uring/waitid.h=11=int io_waitid(struct io_kiocb *req, unsigned int issue_flags);\nio_uring/waitid.h:12:int io_waitid_cancel(struct io_ring_ctx *ctx, struct io_cancel_data *cd,\nio_uring/waitid.h-13-\t\t unsigned int issue_flags);\n"
} |
| 31/3 | 2026/10/16 00:53 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 39419 output: 31 thoughts: 202 Thoughts: Okay, let me break this down.
**Analyzing io_cancel_data and io_ring_ctx in the context of recent changes**
Right, so I'm seeing a consistent use of designated initializers when declaring `struct io_cancel_data`. It's all done like: `struct io_cancel_data cd = { .ctx = ..., ... };` In C99, that's crucial because it implicitly zeros out anything not explicitly set. That's good practice. I note that in `__io_async_cancel`, `cd->running = false;` is explicitly set *before* the call to `io_try_cancel`. No surprises there, but it's important to verify. The `running` flag being explicitly set to `false` and being set *before* calling `io_try_cancel` is standard and important in async environments.
Now, let's look at the changes to `io_ring_ctx`. I see the addition of these fields:
```c
struct io_ring_ctx {
...
+ /* deferred ->poll_wq wake up for local task_work */
+ struct rcu_head poll_wq_rcu;
+ atomic_t poll_wq_rcu_pending;
```
Interesting. These are obviously related to deferred wakeup of `poll_wq` for local `task_work`. And I see that `io_ring_ctx` is allocated via `io_ring_ctx_alloc`. I need to review how this allocation works to understand the implications of these new fields, and how their initialization is handled. Let's see how `io_ring_ctx` is allocated.
|
| 32/3 | 2026/10/16 00:53 | tool | grepper |
0mArgs:{
"Expression": "io_ring_ctx_alloc",
"PathPrefixes": [
"io_uring"
]
}Results: {
"Output": "io_uring/io_uring.c=216=static void io_free_alloc_caches(struct io_ring_ctx *ctx)\n--\nio_uring/io_uring.c-225-\nio_uring/io_uring.c:226:static __cold struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p)\nio_uring/io_uring.c-227-{\n--\nio_uring/io_uring.c=3163=static __cold int io_uring_create(struct io_ctx_config *config)\n--\nio_uring/io_uring.c-3174-\nio_uring/io_uring.c:3175:\tctx = io_ring_ctx_alloc(p);\nio_uring/io_uring.c-3176-\tif (!ctx)\n"
} |
| 33/3 | 2026/10/16 00:53 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 40238 output: 37 thoughts: 13 Thoughts: Okay, here's my attempt at summarizing the thought process, tailored for an expert audience, and written in the first person: **Diving into `io_ring_ctx_alloc`** Alright, let's get into the heart of this. The goal is to fully understand `io_ring_ctx_alloc`. I need to see how the core context for an io_uring instance is constructed. This function is the foundation; everything else builds upon it. I'm expecting to see memory allocation, initialization of data structures, and perhaps some setup related to the kernel interaction. I'll be looking specifically at: * **Memory Management:** How is the memory for the context, including the submission queue (SQ), completion queue (CQ), and other internal structures, allocated? Is it using `kmalloc`, `vmalloc`, or a custom allocator? I need to understand the constraints and potential overhead involved. I'll pay close attention to the use of any specific flags passed to allocation routines. * **Data Structure Initialization:** What specific data structures within the `io_ring_ctx` are populated? What values are they initialized to? Are there any obvious initial states or configurations that will have a performance impact later on? I'm keen to see if any lock initialization is present here. * **Kernel Interaction Setup:** Does this function establish any initial interaction with the kernel, perhaps by registering the io_uring instance? Does it handle any security-related aspects or capabilities checks during the creation of the ring? Are there any checks for kernel version compatibility or feature availability? * **Error Handling:** What error conditions are accounted for? What resources are cleaned up if an error occurs during allocation or initialization? I want to see how robustly this function handles potential failures to prevent resource leaks and ensure a stable environment. I'll be looking for any hints about potential performance bottlenecks, particularly regarding memory contention or locking. I need to understand the design choices, and how the internals are exposed. This could be where the rubber meets the road. |
| 34/3 | 2026/10/16 00:53 | tool | read-file |
0mArgs:{
"File": "io_uring/io_uring.c",
"FirstLine": 226,
"LineCount": 20
}Results: {
"Contents": " 226:\tstatic __cold struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p)\n 227:\t{\n 228:\t\tstruct io_ring_ctx *ctx;\n 229:\t\tint hash_bits;\n 230:\t\tbool ret;\n 231:\t\n 232:\t\tctx = kzalloc_obj(*ctx);\n 233:\t\tif (!ctx)\n 234:\t\t\treturn NULL;\n 235:\t\n 236:\t\txa_init(\u0026ctx-\u003eio_bl_xa);\n 237:\t\txa_init(\u0026ctx-\u003ehpage_acct);\n 238:\t\n 239:\t\t/*\n 240:\t\t * Use 5 bits less than the max cq entries, that should give us around\n 241:\t\t * 32 entries per hash list if totally full and uniformly spread, but\n 242:\t\t * don't keep too many buckets to not overconsume memory.\n 243:\t\t */\n 244:\t\thash_bits = ilog2(p-\u003ecq_entries) - 5;\n 245:\t\thash_bits = clamp(hash_bits, 1, 8);\n"
} |
| 35/3 | 2026/10/16 00:53 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 41035 output: 42 thoughts: 157 Thoughts: **My Initial Thoughts on io_worker Initialization** Okay, so I'm diving into this code, and the first thing I see is `ctx = kzalloc_obj(*ctx);`. That's straightforward; `kzalloc_obj` uses `kzalloc`, so `ctx` is definitely initialized to all zeros. That’s good, a clean slate. Then I see `poll_wq_rcu` and `poll_wq_rcu_pending` being set to zero initially – as expected. Now, let's turn our attention to the `io_worker` structure. Specifically, I'm curious about how the `io_worker` is allocated, so I'm going to look more closely at the definition of `io_worker`. Okay, let's see, I can see `cur_work` and `next_work` declared here. Aha! Now I understand `next_work`'s purpose: it holds the rest of the hashed chain that `cur_work` was removed from. |
| 36/3 | 2026/10/16 00:53 | tool | grepper |
0mArgs:{
"Expression": "kzalloc.*io_worker|create_io_worker",
"PathPrefixes": [
"io_uring/io-wq.c"
]
}Results: {
"Output": "io_uring/io-wq.c=142=struct io_cb_cancel_data {\n--\nio_uring/io-wq.c-149-\nio_uring/io-wq.c:150:static bool create_io_worker(struct io_wq *wq, struct io_wq_acct *acct);\nio_uring/io-wq.c-151-static void io_wq_dec_running(struct io_worker *worker);\n--\nio_uring/io-wq.c=329=static bool io_wq_create_worker(struct io_wq *wq, struct io_wq_acct *acct)\n--\nio_uring/io-wq.c-346-\tatomic_inc(\u0026wq-\u003eworker_refs);\nio_uring/io-wq.c:347:\treturn create_io_worker(wq, acct);\nio_uring/io-wq.c-348-}\n--\nio_uring/io-wq.c=357=static void create_worker_cb(struct callback_head *cb)\n--\nio_uring/io-wq.c-382-\tif (do_create) {\nio_uring/io-wq.c:383:\t\tcreate_io_worker(wq, acct);\nio_uring/io-wq.c-384-\t} else {\n--\nio_uring/io-wq.c=967=static void io_workqueue_create(struct work_struct *work)\n--\nio_uring/io-wq.c-976-\nio_uring/io-wq.c:977:static bool create_io_worker(struct io_wq *wq, struct io_wq_acct *acct)\nio_uring/io-wq.c-978-{\n"
} |
| 37/3 | 2026/10/16 00:53 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 41904 output: 37 thoughts: 11 Thoughts: Okay, here's my interpretation of that thought process, tailored for an expert audience, and written in the first person: **Analyzing the `create_io_worker` Function** Alright, let's dive right in. The instruction's clear: I need to scrutinize the `create_io_worker` function. Considering its name, I'm already anticipating this will be a critical piece of infrastructure for any asynchronous or parallel I/O operation. I expect this function handles the initialization of either a thread or process dedicated to I/O-bound tasks, which is the cornerstone for achieving non-blocking operations. I will be looking closely for how it manages resource allocation, thread/process lifecycle, and inter-process communication if the design is multi-process. I'm also interested in how it handles error conditions and potential blocking situations. My mental checklist already includes concerns about thread safety, potential race conditions, and appropriate locking mechanisms, depending on the implementation details. I also want to observe if there's any clever use of OS-specific primitives here, such as epoll or kqueue, as this could have a significant performance impact. I'll be looking to see if any context is passed in, that might inform the design or operation of this worker. |
| 38/3 | 2026/10/16 00:54 | tool | read-file |
0mArgs:{
"File": "io_uring/io-wq.c",
"FirstLine": 977,
"LineCount": 30
}Results: {
"Contents": " 977:\tstatic bool create_io_worker(struct io_wq *wq, struct io_wq_acct *acct)\n 978:\t{\n 979:\t\tstruct io_worker *worker;\n 980:\t\tstruct task_struct *tsk;\n 981:\t\n 982:\t\t__set_current_state(TASK_RUNNING);\n 983:\t\n 984:\t\tworker = kzalloc_obj(*worker);\n 985:\t\tif (!worker) {\n 986:\tfail:\n 987:\t\t\tatomic_dec(\u0026acct-\u003enr_running);\n 988:\t\t\traw_spin_lock(\u0026acct-\u003eworkers_lock);\n 989:\t\t\tacct-\u003enr_workers--;\n 990:\t\t\traw_spin_unlock(\u0026acct-\u003eworkers_lock);\n 991:\t\t\tio_worker_ref_put(wq);\n 992:\t\t\treturn false;\n 993:\t\t}\n 994:\t\n 995:\t\trefcount_set(\u0026worker-\u003eref, 1);\n 996:\t\tworker-\u003ewq = wq;\n 997:\t\tworker-\u003eacct = acct;\n 998:\t\traw_spin_lock_init(\u0026worker-\u003elock);\n 999:\t\tinit_completion(\u0026worker-\u003eref_done);\n1000:\t\n1001:\t\ttsk = create_io_thread(io_wq_worker, worker, NUMA_NO_NODE);\n1002:\t\tif (!IS_ERR(tsk)) {\n1003:\t\t\tio_init_new_worker(wq, acct, worker, tsk);\n1004:\t\t} else if (!io_should_retry_thread(worker, PTR_ERR(tsk))) {\n1005:\t\t\tkfree(worker);\n1006:\t\t\tgoto fail;\n"
} |
| 39/3 | 2026/10/16 00:54 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 42692 output: 36 thoughts: 137 Thoughts: Okay, here's my breakdown of what's going through my mind right now. I'm focusing on that `io_uring` context, as requested:
**Deconstructing the `io_tctx_exit` Structure**
Alright, so I've got this new `worker` object, freshly allocated using `kzalloc_obj`. That means it's *guaranteed* to be completely zeroed out. Excellent! That takes care of any potential initialization headaches, and I know that `next_work` is automatically set to `NULL`. Good.
Now, I need to understand this `io_tctx_exit` structure within the `io_uring.c` context. I'm going to quickly reference the structure definition itself:
```c
struct io_tctx_exit {
struct callback_head task_work;
struct completion completion;
struct io_ring_ctx *ctx;
bool in_cancel;
};
```
Okay, so I see the components: `task_work`, `completion`, `ctx`, and `in_cancel`. Now, the key question is, where is this `io_tctx_exit` used? Where is it allocated? To answer this, I'm going to look at the function `io_ring_exit_work`. I have a feeling that function will give me insight into how the struct is instantiated. I'll make sure to note where the struct itself is allocated, and what the use case is.
|
| 40/3 | 2026/10/16 00:54 | tool | grepper |
0mArgs:{
"Expression": "io_tctx_exit",
"PathPrefixes": [
"io_uring/io_uring.c"
]
}Results: {
"Output": "io_uring/io_uring.c=2345=static __poll_t io_uring_poll(struct file *file, poll_table *wait)\n--\nio_uring/io_uring.c-2383-\nio_uring/io_uring.c:2384:struct io_tctx_exit {\nio_uring/io_uring.c-2385-\tstruct callback_head\t\ttask_work;\n--\nio_uring/io_uring.c-2390-\nio_uring/io_uring.c:2391:static __cold void io_tctx_exit_cb(struct callback_head *cb)\nio_uring/io_uring.c-2392-{\nio_uring/io_uring.c-2393-\tstruct io_uring_task *tctx = current-\u003eio_uring;\nio_uring/io_uring.c:2394:\tstruct io_tctx_exit *work;\nio_uring/io_uring.c-2395-\nio_uring/io_uring.c:2396:\twork = container_of(cb, struct io_tctx_exit, task_work);\nio_uring/io_uring.c-2397-\t/*\n--\nio_uring/io_uring.c=2518=static __cold void io_ring_exit_work(struct work_struct *work)\n--\nio_uring/io_uring.c-2522-\tunsigned long interval = HZ / 20;\nio_uring/io_uring.c:2523:\tstruct io_tctx_exit exit;\nio_uring/io_uring.c-2524-\tstruct io_tctx_node *node;\n--\nio_uring/io_uring.c-2547-\tinit_completion(\u0026exit.completion);\nio_uring/io_uring.c:2548:\tinit_task_work(\u0026exit.task_work, io_tctx_exit_cb);\nio_uring/io_uring.c-2549-\texit.ctx = ctx;\n"
} |
| 41/3 | 2026/10/16 00:54 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 43716 output: 38 thoughts: 115 Thoughts: Okay, let's dive into this `io_ring_exit_work` function. I need to be absolutely sure about how this is handling context exits, particularly regarding potential race conditions or uninitialized memory reads. **Analyzing `io_ring_exit_work` for Context Exit Handling** Right, so I'm looking at `struct io_tctx_exit exit;` and how it's used. We have `init_completion(&exit.completion)` and `init_task_work(&exit.task_work, io_tctx_exit_cb)`, standard initializations. And, of course, `exit.ctx = ctx;` to associate the exit structure with the context. But here's the critical point I need to verify: I'm concerned about the initialization, or lack thereof, and potential uninitialized reads of `exit.in_cancel`. This variable is critical and I have to confirm how it is used. I need to read the code in and around lines 2520 to 2580, I have a feeling there might be a subtle bug that's being masked. Let me jump into the code and see. |
| 42/3 | 2026/10/16 00:54 | tool | read-file |
0mArgs:{
"File": "io_uring/io_uring.c",
"FirstLine": 2520,
"LineCount": 65
}Results: {
"Contents": "2520:\t\tstruct io_ring_ctx *ctx = container_of(work, struct io_ring_ctx, exit_work);\n2521:\t\tunsigned long timeout = jiffies + IO_URING_EXIT_WAIT_MAX;\n2522:\t\tunsigned long interval = HZ / 20;\n2523:\t\tstruct io_tctx_exit exit;\n2524:\t\tstruct io_tctx_node *node;\n2525:\t\tint ret;\n2526:\t\n2527:\t\tdo {\n2528:\t\t\tio_ring_ctx_cancel(ctx);\n2529:\t\n2530:\t\t\tif (WARN_ON_ONCE(time_after(jiffies, timeout))) {\n2531:\t\t\t\t/* there is little hope left, don't run it too often */\n2532:\t\t\t\tinterval = HZ * 60;\n2533:\t\t\t}\n2534:\t\t\t/*\n2535:\t\t\t * This is really an uninterruptible wait, as it has to be\n2536:\t\t\t * complete. But it's also run from a kworker, which doesn't\n2537:\t\t\t * take signals, so it's fine to make it interruptible. This\n2538:\t\t\t * avoids scenarios where we knowingly can wait much longer\n2539:\t\t\t * on completions, for example if someone does a SIGSTOP on\n2540:\t\t\t * a task that needs to finish task_work to make this loop\n2541:\t\t\t * complete. That's a synthetic situation that should not\n2542:\t\t\t * cause a stuck task backtrace, and hence a potential panic\n2543:\t\t\t * on stuck tasks if that is enabled.\n2544:\t\t\t */\n2545:\t\t} while (!wait_for_completion_interruptible_timeout(\u0026ctx-\u003eref_comp, interval));\n2546:\t\n2547:\t\tinit_completion(\u0026exit.completion);\n2548:\t\tinit_task_work(\u0026exit.task_work, io_tctx_exit_cb);\n2549:\t\texit.ctx = ctx;\n2550:\t\n2551:\t\tmutex_lock(\u0026ctx-\u003euring_lock);\n2552:\t\tmutex_lock(\u0026ctx-\u003etctx_lock);\n2553:\t\twhile (!list_empty(\u0026ctx-\u003etctx_list)) {\n2554:\t\t\tWARN_ON_ONCE(time_after(jiffies, timeout));\n2555:\t\n2556:\t\t\tnode = list_first_entry(\u0026ctx-\u003etctx_list, struct io_tctx_node,\n2557:\t\t\t\t\t\tctx_node);\n2558:\t\t\t/* don't spin on a single task if cancellation failed */\n2559:\t\t\tlist_rotate_left(\u0026ctx-\u003etctx_list);\n2560:\t\t\tret = task_work_add(node-\u003etask, \u0026exit.task_work, TWA_SIGNAL);\n2561:\t\t\tif (WARN_ON_ONCE(ret))\n2562:\t\t\t\tcontinue;\n2563:\t\n2564:\t\t\tmutex_unlock(\u0026ctx-\u003etctx_lock);\n2565:\t\t\tmutex_unlock(\u0026ctx-\u003euring_lock);\n2566:\t\t\t/*\n2567:\t\t\t * See comment above for\n2568:\t\t\t * wait_for_completion_interruptible_timeout() on why this\n2569:\t\t\t * wait is marked as interruptible.\n2570:\t\t\t */\n2571:\t\t\twait_for_completion_interruptible(\u0026exit.completion);\n2572:\t\t\t/*\n2573:\t\t\t * The task removes the node when it is done with its\n2574:\t\t\t * cancellation. Give it the time for that: it doesn't get back\n2575:\t\t\t * to it as long as it finds new task_work each time it has run\n2576:\t\t\t * the old one, and we may be one of many who send it.\n2577:\t\t\t */\n2578:\t\t\tif (exit.in_cancel)\n2579:\t\t\t\tschedule_timeout_idle(HZ / 20);\n2580:\t\t\tmutex_lock(\u0026ctx-\u003euring_lock);\n2581:\t\t\tmutex_lock(\u0026ctx-\u003etctx_lock);\n2582:\t\t}\n2583:\t\tmutex_unlock(\u0026ctx-\u003etctx_lock);\n2584:\t\tmutex_unlock(\u0026ctx-\u003euring_lock);\n"
} |
| 43/3 | 2026/10/16 00:54 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 45229 output: 34 thoughts: 833 Thoughts: **Analyzing Potential Race Conditions in io_uring Task Completion**
Okay, let's dig into this io_uring completion flow. Specifically, I'm focusing on lines 2560-2578 and trying to understand a possible race condition. The core concern is the interaction between `wait_for_completion_interruptible(&exit.completion)` and the execution of `exit.task_work`.
My initial thought is: what if `wait_for_completion_interruptible` returns prematurely, *before* `exit.task_work` has actually run? Remember that `io_ring_exit_work` is handled by a kworker thread. Crucially, I have to remember that line 2536 explicitly states that kworkers do *not* take signals. So, could an interruption by signal cause an early return? No. The code clearly addresses that.
Then, a new possibility emerges: what if `task_work_add` fails, returning an error? Line 2561 reveals that `if (WARN_ON_ONCE(ret)) continue;` handles non-zero return values by skipping the wait. Okay, so that's covered.
When `task_work_add` succeeds, the designated `io_tctx_exit_cb` callback is run. Now, let's figure out what `io_tctx_exit_cb` *actually* does. Looking at lines 2391-2407, the crucial part is the assignment `work->in_cancel = tctx && atomic_read(&tctx->in_cancel);` and then, the call to `complete(&work->completion);`.
Here's the key: `work->in_cancel` is ALWAYS assigned! And then, the completion is signaled. This means that `exit.in_cancel` is set in the callback before `complete()` wakes up `wait_for_completion_interruptible()`. I can't find a scenario where this won't happen.
So, the critical question is whether `exit.in_cancel` could be read uninitialized. If `wait_for_completion_interruptible` returns prematurely without `complete(&work->completion)`, it can't. Kthreads generally don't get signals, and even if interrupted (like if frozen), `reinit_completion` isn't called. But I recall that each loop uses the completion, not re-initializing. No, when it completes, `in_cancel` was already set.
I am re-evaluating whether the kthread *could* be interrupted. I know that workqueue threads have signals blocked, but I have to remember that SIGKILL could come in during process exit. However, I believe kworker isn't affected by that.
Finally, I think I need to cross-check how cancellations are handled using `io_sync_cancel` in `io_uring/cancel.c`, especially with respect to the timeout handling in lines like
```c
- 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
```
I am going to check what `sc` is defined as.
|
| 44/3 | 2026/10/16 00:54 | tool | grepper |
0mArgs:{
"Expression": "struct io_uring_sync_cancel_reg",
"PathPrefixes": [
"io_uring"
]
}Results: {
"Output": "io_uring/cancel.c=291=int io_sync_cancel(struct io_ring_ctx *ctx, void __user *arg)\n--\nio_uring/cancel.c-298-\tktime_t timeout = KTIME_MAX;\nio_uring/cancel.c:299:\tstruct io_uring_sync_cancel_reg sc;\nio_uring/cancel.c-300-\tstruct file *file = NULL;\n"
} |
| 45/3 | 2026/10/16 00:54 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 46969 output: 35 thoughts: 21 Thoughts: Okay, let's dive into this. Here's my take on those lines of code, presented as my own thinking: **Examining the `io_sync_cancel` Implementation** Alright, I need to get a handle on what's happening within `io_sync_cancel` between lines 291 and 340. I know this is a critical section for canceling asynchronous I/O operations, so I need to pay close attention to the mechanics. First, I expect to see the code handling the cancellation itself – the process of signaling or otherwise interrupting the ongoing I/O. I'm looking for how it interacts with the underlying kernel structures and any associated completion queues or waiting threads. I want to see if it handles errors gracefully and ensures data consistency, especially considering this is a multi-threaded environment. I suspect there will be some locking involved to prevent race conditions during cancellation. I want to identify where the code handles the case of a request being already completed or in the process of completion, and how it manages the resources associated with the cancelled operation – such as freeing buffers or notifying the calling application. Understanding the specific control flow within these lines will clarify the nuances of the cancellation procedure. |
| 46/3 | 2026/10/16 00:54 | tool | read-file |
0mArgs:{
"File": "io_uring/cancel.c",
"FirstLine": 291,
"LineCount": 50
}Results: {
"Contents": " 291:\tint io_sync_cancel(struct io_ring_ctx *ctx, void __user *arg)\n 292:\t\t__must_hold(\u0026ctx-\u003euring_lock)\n 293:\t{\n 294:\t\tstruct io_cancel_data cd = {\n 295:\t\t\t.ctx\t= ctx,\n 296:\t\t\t.seq\t= atomic_inc_return(\u0026ctx-\u003ecancel_seq),\n 297:\t\t};\n 298:\t\tktime_t timeout = KTIME_MAX;\n 299:\t\tstruct io_uring_sync_cancel_reg sc;\n 300:\t\tstruct file *file = NULL;\n 301:\t\tDEFINE_WAIT(wait);\n 302:\t\tint ret, i;\n 303:\t\n 304:\t\tif (copy_from_user(\u0026sc, arg, sizeof(sc)))\n 305:\t\t\treturn -EFAULT;\n 306:\t\tif (sc.flags \u0026 ~CANCEL_FLAGS)\n 307:\t\t\treturn -EINVAL;\n 308:\t\tfor (i = 0; i \u003c ARRAY_SIZE(sc.pad); i++)\n 309:\t\t\tif (sc.pad[i])\n 310:\t\t\t\treturn -EINVAL;\n 311:\t\tfor (i = 0; i \u003c ARRAY_SIZE(sc.pad2); i++)\n 312:\t\t\tif (sc.pad2[i])\n 313:\t\t\t\treturn -EINVAL;\n 314:\t\n 315:\t\tcd.data = sc.addr;\n 316:\t\tcd.flags = sc.flags;\n 317:\t\tcd.opcode = sc.opcode;\n 318:\t\n 319:\t\t/* we can grab a normal file descriptor upfront */\n 320:\t\tif ((cd.flags \u0026 IORING_ASYNC_CANCEL_FD) \u0026\u0026\n 321:\t\t !(cd.flags \u0026 IORING_ASYNC_CANCEL_FD_FIXED)) {\n 322:\t\t\tfile = fget(sc.fd);\n 323:\t\t\tif (!file)\n 324:\t\t\t\treturn -EBADF;\n 325:\t\t\tcd.file = file;\n 326:\t\t}\n 327:\t\n 328:\t\tret = __io_sync_cancel(current-\u003eio_uring, \u0026cd, sc.fd);\n 329:\t\n 330:\t\t/* found something, done! */\n 331:\t\tif (ret != -EALREADY)\n 332:\t\t\tgoto out;\n 333:\t\n 334:\t\tif (sc.timeout.tv_sec != -1 || sc.timeout.tv_nsec != -1) {\n 335:\t\t\tstruct timespec64 ts = {\n 336:\t\t\t\t.tv_sec\t\t= sc.timeout.tv_sec,\n 337:\t\t\t\t.tv_nsec\t= sc.timeout.tv_nsec\n 338:\t\t\t};\n 339:\t\n 340:\t\t\ttimeout = ktime_add_ns(timespec64_to_ktime(ts), ktime_get_ns());\n"
} |
| 47/3 | 2026/10/16 00:54 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 48077 output: 31 thoughts: 929 Thoughts: **Analyzing a Potential Bug in `io_sync_cancel` and Related Timeout Handling** Okay, so I'm digging into this `io_sync_cancel` function, and the first thing that jumps out is this user copy: `copy_from_user(&sc, arg, sizeof(sc))`. I need to understand what's happening with `sc.timeout.tv_sec`. What's the type of that field? Let's check `struct __kernel_timespec`. Ah, right, it's `int64_t tv_sec; long long tv_nsec;`. That's important. Now I see the potential issue. There's a comparison `sc.timeout.tv_sec != -1UL`. On 64-bit platforms, that `UL` (unsigned long) is going to represent 0xffffffffffffffff, which is fine. But on 32-bit platforms, `-1UL` becomes 0xffffffff, which is a 32-bit value. The `int64_t` is 64-bit, so this comparison wasn't correctly checking if the value was `-1`. The comparison could have been buggy on 32-bit platforms! A value of `0xffffffff` might have resulted in a false positive. By changing it to `-1`, the code is properly sign-extending the comparison to a 64-bit value, ensuring the comparison works as intended across architectures. So it wasn't uninitialized memory as I initially thought. Now, let's move on to `io_timeout_update` and `io_timeout_remove_prep` and see what they're doing. I see in `io_timeout_update` function in `io_timeout.c`, that `io_timeout_find` has been replaced with `io_timeout_extract` function. Also, there is now an extra check for `multishot requests` which only makes sense with `rel` values and `HRTIMER_MODE_ABS` is being returned if the `data->flags` are set with `IORING_TIMEOUT_MULTISHOT`. After that the `req` variable is initialized again. If the request isn't multishot `timeout->off` is assigned `0`. If the mode is absolute, then the value is converted to the host's view. Finally, the `data->time` is assigned the `time` parameter and added to the list. Now looking at `io_timeout_remove_prep` if the flags aren't `IORING_TIMEOUT_ABS` or `IORING_TIMEOUT_IMMEDIATE_ARG`, an `EINVAL` is returned. Inside, the clock is that of the timeout to update, an absolute time is converted to the host's view once that has been found. Then, let's see what `io_parse_user_time` is doing. |
| 48/3 | 2026/10/16 00:54 | tool | grepper |
0mArgs:{
"Expression": "io_parse_user_time",
"PathPrefixes": [
"io_uring"
]
}Results: {
"Output": "io_uring/timeout.c=39=static clockid_t io_flags_to_clock(unsigned flags)\n--\nio_uring/timeout.c-54-\nio_uring/timeout.c:55:static int io_parse_user_time(ktime_t *time, u64 arg, unsigned flags)\nio_uring/timeout.c-56-{\n--\nio_uring/timeout.c=511=int io_timeout_remove_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)\n--\nio_uring/timeout.c-538-\t\t */\nio_uring/timeout.c:539:\t\tret = io_parse_user_time(\u0026tr-\u003etime, READ_ONCE(sqe-\u003eaddr2),\nio_uring/timeout.c-540-\t\t\t\t\t tr-\u003eflags \u0026 ~IORING_TIMEOUT_ABS);\n--\nio_uring/timeout.c=589=static int __io_timeout_prep(struct io_kiocb *req,\n--\nio_uring/timeout.c-637-\nio_uring/timeout.c:638:\tret = io_parse_user_time(\u0026data-\u003etime, READ_ONCE(sqe-\u003eaddr), flags);\nio_uring/timeout.c-639-\tif (ret)\n"
} |
| 49/3 | 2026/10/16 00:54 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 50002 output: 34 thoughts: 13 Thoughts: Okay, here's my attempt at summarizing the thought process, speaking as though it's my own internal dialogue, assuming I'm an expert in this field and presented as a fulsome summary of the thought process: **Diving into `io_parse_user_time`** Alright, let's get into this `io_parse_user_time` function. I see it's calling for my attention, so let's dig in and figure out what the intent is. Based on the name alone, I'm expecting a function related to parsing user-provided time input, which typically would involve date, time, and possibly timezone information. My initial question is how robust is this implementation? Is it relying on standard libraries? Rolling its own parsing routines? How is it handling different time formats and potential errors? That's what I want to ascertain first. I need to understand its error handling. What kind of validation is being done? Does it provide informative error messages or will it just crash? Ideally, it should catch invalid formats, out-of-range values, and ambiguous time zones. I'll need to look for exception handling, or explicit error codes. I'm thinking about edge cases. Does it account for leap seconds or timezone transitions? Does it handle ambiguous times, like when a daylight savings time change causes a time to occur twice or be skipped? What about different locales and their varying date/time formats? I'm hoping it's designed with internationalization in mind, but I'll have to see if there is any indication of it when inspecting this. Functionality wise, how does it deal with relative times? Does it handle inputs like "now", "yesterday", or "in 2 hours"? Those features add a lot of complexity, and it'll depend on whether the intent is for a simple absolute time parser, or one that could work with relative times. I also need to see if it supports multiple timezones or assumes a single default timezone. Finally, I'll pay attention to the return values. Is it returning a structured time object, like a `struct tm` or an equivalent, some kind of timestamp, or perhaps a string representation? The return type will affect how other parts of the system interact with the parsed time information. And importantly, are the return values checked rigorously in the surrounding code? No point in robust parsing if the results aren't properly validated before use. |
| 50/3 | 2026/10/16 00:54 | tool | read-file |
0mArgs:{
"File": "io_uring/timeout.c",
"FirstLine": 55,
"LineCount": 35
}Results: {
"Contents": " 55:\tstatic int io_parse_user_time(ktime_t *time, u64 arg, unsigned flags)\n 56:\t{\n 57:\t\tstruct timespec64 ts;\n 58:\t\n 59:\t\tif (flags \u0026 IORING_TIMEOUT_IMMEDIATE_ARG) {\n 60:\t\t\t*time = ns_to_ktime(arg);\n 61:\t\t\tif (*time \u003c 0)\n 62:\t\t\t\treturn -EINVAL;\n 63:\t\t\tgoto out;\n 64:\t\t}\n 65:\t\n 66:\t\tif (get_timespec64(\u0026ts, u64_to_user_ptr(arg)))\n 67:\t\t\treturn -EFAULT;\n 68:\t\tif (ts.tv_sec \u003c 0 || ts.tv_nsec \u003c 0)\n 69:\t\t\treturn -EINVAL;\n 70:\t\t*time = timespec64_to_ktime(ts);\n 71:\tout:\n 72:\t\tif (flags \u0026 IORING_TIMEOUT_ABS)\n 73:\t\t\t*time = timens_ktime_to_host(io_flags_to_clock(flags), *time);\n 74:\t\treturn 0;\n 75:\t}\n 76:\t\n 77:\tstatic struct io_kiocb *__io_disarm_linked_timeout(struct io_kiocb *req,\n 78:\t\t\t\t\t\t\t struct io_kiocb *link);\n 79:\t\n 80:\tstatic inline bool io_is_timeout_noseq(struct io_kiocb *req)\n 81:\t{\n 82:\t\tstruct io_timeout *timeout = io_kiocb_to_cmd(req, struct io_timeout);\n 83:\t\tstruct io_timeout_data *data = req-\u003easync_data;\n 84:\t\n 85:\t\treturn !timeout-\u003eoff || data-\u003eflags \u0026 IORING_TIMEOUT_MULTISHOT;\n 86:\t}\n 87:\t\n 88:\tstatic inline void io_put_req(struct io_kiocb *req)\n 89:\t{\n"
} |
| 51/3 | 2026/10/16 00:54 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 51108 output: 35 thoughts: 67 Thoughts: Here's my summary, as I see it: **Digging into Time Handling in the IO Subsystem** Alright, so I'm looking at how this system handles timeouts, and it seems pretty straightforward at first glance. The `io_parse_user_time` function directly populates the `*time` pointer we pass it, returning an error if there's a problem with the input, or if it encounters an EFAULT. That's standard error handling. The interesting part is how this time value, once parsed, gets used. I see `&tr->time` being passed to `io_timeout_remove_prep`. This suggests `tr->time` is likely a time structure that holds either an absolute or relative timeout value, depending on how this is designed to operate. My next step is clearly to go deep on `io_timeout_remove_prep` and `io_timeout_remove`. I need to understand how the parsed time gets integrated into the timeout management lifecycle – how it's prepared, and then how it's ultimately removed. Knowing how the time is stored and manipulated in these functions will expose the potential for synchronization issues, race conditions, or logic errors that I need to be aware of. Let's see what these functions reveal. |
| 52/3 | 2026/10/16 00:54 | tool | read-file |
0mArgs:{
"File": "io_uring/timeout.c",
"FirstLine": 520,
"LineCount": 55
}Results: {
"Contents": " 520:\t\tif (sqe-\u003ebuf_index || sqe-\u003elen || sqe-\u003esplice_fd_in)\n 521:\t\t\treturn -EINVAL;\n 522:\t\n 523:\t\ttr-\u003eltimeout = false;\n 524:\t\ttr-\u003eaddr = READ_ONCE(sqe-\u003eaddr);\n 525:\t\ttr-\u003eflags = READ_ONCE(sqe-\u003etimeout_flags);\n 526:\t\tif (tr-\u003eflags \u0026 IORING_TIMEOUT_UPDATE_MASK) {\n 527:\t\t\tif (hweight32(tr-\u003eflags \u0026 IORING_TIMEOUT_CLOCK_MASK) \u003e 1)\n 528:\t\t\t\treturn -EINVAL;\n 529:\t\t\tif (tr-\u003eflags \u0026 IORING_LINK_TIMEOUT_UPDATE)\n 530:\t\t\t\ttr-\u003eltimeout = true;\n 531:\t\t\tif (tr-\u003eflags \u0026 ~(IORING_TIMEOUT_UPDATE_MASK |\n 532:\t\t\t\t\t IORING_TIMEOUT_ABS |\n 533:\t\t\t\t\t IORING_TIMEOUT_IMMEDIATE_ARG))\n 534:\t\t\t\treturn -EINVAL;\n 535:\t\t\t/*\n 536:\t\t\t * The clock is that of the timeout to update, an absolute time\n 537:\t\t\t * is converted to the host's view once that has been found.\n 538:\t\t\t */\n 539:\t\t\tret = io_parse_user_time(\u0026tr-\u003etime, READ_ONCE(sqe-\u003eaddr2),\n 540:\t\t\t\t\t\t tr-\u003eflags \u0026 ~IORING_TIMEOUT_ABS);\n 541:\t\t\tif (ret)\n 542:\t\t\t\treturn ret;\n 543:\t\t} else if (tr-\u003eflags) {\n 544:\t\t\t/* timeout removal doesn't support flags */\n 545:\t\t\treturn -EINVAL;\n 546:\t\t}\n 547:\t\n 548:\t\treturn 0;\n 549:\t}\n 550:\t\n 551:\tstatic inline enum hrtimer_mode io_translate_timeout_mode(unsigned int flags)\n 552:\t{\n 553:\t\treturn (flags \u0026 IORING_TIMEOUT_ABS) ? HRTIMER_MODE_ABS\n 554:\t\t\t\t\t\t : HRTIMER_MODE_REL;\n 555:\t}\n 556:\t\n 557:\t/*\n 558:\t * Remove or update an existing timeout command\n 559:\t */\n 560:\tint io_timeout_remove(struct io_kiocb *req, unsigned int issue_flags)\n 561:\t{\n 562:\t\tstruct io_timeout_rem *tr = io_kiocb_to_cmd(req, struct io_timeout_rem);\n 563:\t\tstruct io_ring_ctx *ctx = req-\u003ectx;\n 564:\t\tint ret;\n 565:\t\n 566:\t\tif (!(tr-\u003eflags \u0026 IORING_TIMEOUT_UPDATE)) {\n 567:\t\t\tstruct io_cancel_data cd = { .ctx = ctx, .data = tr-\u003eaddr, };\n 568:\t\n 569:\t\t\tspin_lock(\u0026ctx-\u003ecompletion_lock);\n 570:\t\t\tret = io_timeout_cancel(ctx, \u0026cd);\n 571:\t\t\tspin_unlock(\u0026ctx-\u003ecompletion_lock);\n 572:\t\t} else {\n 573:\t\t\tenum hrtimer_mode mode = io_translate_timeout_mode(tr-\u003eflags);\n 574:\t\n"
} |
| 53/3 | 2026/10/16 00:54 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 52376 output: 408 thoughts: 3388 Thoughts: **Summary of Patch Analysis** Alright, let's break this down. I've gone through the code changes meticulously, focusing on whether these modifications introduce any risks that KMSAN is uniquely equipped to catch. From what I've seen, all the fundamental building blocks—`tr` values, `io_init_req`'s ring management logic for mixed SQEs (128b), CQE cache refill procedures, `io_futex_prep_private`'s error handling, the poll handler re-arming, synchronization with `io_waitid_wait`, and wake queue callbacks—are working as intended. The crucial thing is to see whether something's going to leak uninitialized memory to user space, or whether we are reading garbage. The `io_init_req` change specifically addresses how 128-byte SQEs are handled in mixed SQ rings. The code meticulously manages the `cached_refs` and ring indices. The other change makes the `cached_cqe` pointer to be cleared, which is important for preventing incorrect reuse if CQE refill fails, and avoids problems by handling CQE32. The `io_futex_prep_private` change ensures that private futexes are handled correctly, and the `io_poll_add` modification carefully cleans up old asynchronous data and flags before re-arming the poll handler. I've also checked out the synchronization around `io_waitid_wait` and its interaction with cancel/issue, and the wake queue callback return value. Now, considering the KMSAN criteria, there aren't any new kernel structures being sent to user space where uninitialized fields are a concern. The `copy_to_user` calls (like in `openclose.c` or resize in `register.c`) either deal with fully initialized data, or rely on zero-initialization. All of the changes are zeroed or explicitly set. We're not seeing new opportunities for user-space info leaks. In some cases, we're fixing possible integer overflows like that in zcrx.c with a clear fix by casting to a u64. Crucially, all added fields or modifications to existing structures (`io_cancel_data.running`, `io_worker.next_work`, etc.) are either part of zero-initialized structures or set to known values before use. The most dangerous cases are handled through standard kernel debugging. Potential bugs would most likely be related to concurrency, race conditions, or access violations, not uninitialized memory reads. Therefore, given my expert understanding of the code's behavior, I am confident that there is no risk of uninitialized memory usage or info-leaks, and thus, KMSAN is not needed for this set of changes. Standard KASAN, LOCKDEP, and other bug detectors should be sufficient. |
| 54/3 | 2026/10/16 00:55 | tool | set-results |
0mArgs:{
"KMSANReasoning": "The patch series addresses concurrency, cancellation, task work synchronization, and bounds handling across several io_uring submodules:\n1. Struct and variable initialization: New struct fields (such as `running` in `struct io_cancel_data`, `next_work` in `struct io_worker`, `in_cancel` in `struct io_tctx_exit`, and `poll_wq_rcu` / `poll_wq_rcu_pending` in `struct io_ring_ctx`) are either allocated with `kzalloc_obj`, cleared via designated struct initializers (`= { ... }`), or explicitly initialized prior to being read.\n2. User-space copies and info-leaks: In `io_pipe_fixed()`, `copy_to_user()` copies `fds` (an `int[2]` with no padding, both elements fully initialized), and the patch only modifies the locking scope around the copy. In `io_zcrx_queue_cqe()`, casting to `u64` prevents integer truncation/overflow while `__pad` remains zeroed.\n3. Memory safety and ring handling: Changes in `fdinfo.c` and `register.c` correct CQE32 and SQE128 index calculations and wrap-around bounds checks in shared ring buffers. Any indexing or sizing bugs would result in out-of-bounds access or data corruption, which are directly detected by KASAN. Concurrency, memory barrier, and locking adjustments (e.g. in `mpscq.h`, `io-wq.c`, `waitid.c`) are monitored by LOCKDEP.\n\nBecause no uninitialized stack/heap reads, uninitialized conditional branches, or uninitialized user-space info-leaks are introduced, a dedicated KMSAN session is not justified.",
"NeedsKMSAN": false
}Results: {
"KMSANReasoning": "The patch series addresses concurrency, cancellation, task work synchronization, and bounds handling across several io_uring submodules:\n1. Struct and variable initialization: New struct fields (such as `running` in `struct io_cancel_data`, `next_work` in `struct io_worker`, `in_cancel` in `struct io_tctx_exit`, and `poll_wq_rcu` / `poll_wq_rcu_pending` in `struct io_ring_ctx`) are either allocated with `kzalloc_obj`, cleared via designated struct initializers (`= { ... }`), or explicitly initialized prior to being read.\n2. User-space copies and info-leaks: In `io_pipe_fixed()`, `copy_to_user()` copies `fds` (an `int[2]` with no padding, both elements fully initialized), and the patch only modifies the locking scope around the copy. In `io_zcrx_queue_cqe()`, casting to `u64` prevents integer truncation/overflow while `__pad` remains zeroed.\n3. Memory safety and ring handling: Changes in `fdinfo.c` and `register.c` correct CQE32 and SQE128 index calculations and wrap-around bounds checks in shared ring buffers. Any indexing or sizing bugs would result in out-of-bounds access or data corruption, which are directly detected by KASAN. Concurrency, memory barrier, and locking adjustments (e.g. in `mpscq.h`, `io-wq.c`, `waitid.c`) are monitored by LOCKDEP.\n\nBecause no uninitialized stack/heap reads, uninitialized conditional branches, or uninitialized user-space info-leaks are introduced, a dedicated KMSAN session is not justified.",
"NeedsKMSAN": false
} |
| Total Calls | Total Tokens | Avg Tokens | Total Duration (Seconds) | Avg Duration (Seconds) |
|---|
| Total Calls | Total Duration (Seconds) | Avg Duration (Seconds) |
|---|