io_uring: fix io may accumulation in poll mode
Subsystems (2)
cover io_uring: fix io may accumulation in poll mode
In the io_do_iopoll function, when the poll loop of iopoll_list ends, it is considered that the current req is the actual completed request. This may be reasonable for multi-queue ctx, but is problematic for single-queue ctx because the current request may not be done when the poll gets to the result. In this case, the completed io needs to wait for the first io on the chain to complete before notifying the user, which may cause io accumulation in the list.
Our modification plan is as follows: change io_wq_work_list to normal list so that the iopoll_list list in it can be removed and put into the comp_reqs list when the request is completed. This way each io is handled independently and all gets processed in time.
After modification, test with:
./t/io_uring -p1 -d128 -b4096 -s32 -c32 -F1 -B1 -R1 -X1 -n1 -P1 /dev/nvme6n1
base IOPS is 725K, patch IOPS is 782K.
./t/io_uring -p1 -d128 -b4096 -s32 -c1 -F1 -B1 -R1 -X1 -n1 -P1 /dev/nvme6n1
Base IOPS is 880k, patch IOPS is 895K.
Fengnan Chang (2):
blk-mq: delete task running check in blk_hctx_poll
io_uring: fix io may accumulation in poll mode
block/blk-mq.c | 2 -- include/linux/blk-mq.h | 1 + include/linux/blkdev.h | 1 + include/linux/io_uring_types.h | 8 +++--- io_uring/io_uring.c | 51 ++++++++++++++++------------------ io_uring/io_uring.h | 15 ++++------ io_uring/rw.c | 35 +++++++---------------- io_uring/slist.h | 9 ------ io_uring/sqpoll.c | 8 +++--- 9 files changed, 49 insertions(+), 81 deletions(-)
base-commit: 4941a17751c99e17422be743c02c923ad706f888
prerequisite-patch-id: 14d5e19968d0acb074ebcd62f073c330b0e34f36
prerequisite-patch-id: 1b5c18a564a4b4788f4992dc02d70c01f69c0e0e
Signature · 1 line
2.39.5 (Apple Git-154)
RFC PATCH 1/2 blk-mq: delete task running check in blk_hctx_poll
In blk_hctx_poll, it always check task is running or not, and return 1 if task is running, it's not reasonable for current caller, especially io_uring, which is always running and cause BLK_POLL_ONESHOT is set.
It looks like there has been this judgment for historical reasons, and in very early versions of this function the user would set the process state to TASK_UNINTERRUPTIBLE.
--- block/blk-mq.c | 2 -- 1 file changed, 2 deletions(-)
block/blk-mq.c +0 -2
@@ -5172,8 +5172,6 @@ static int blk_hctx_poll(struct request_queue *q, struct blk_mq_hw_ctx *hctx,if (signal_pending_state (state, current))__set_current_state (TASK_RUNNING ); -if (task_is_running (current)) -return 1 ;if (ret <0 || (flags &BLK_POLL_ONESHOT ))break ;
Signature · 1 line
2.39.5 (Apple Git-154)
Jens Axboe
maintainerRe: [RFC PATCH 1/2] blk-mq: delete task running check in blk_hctx_poll
Quoted 19 lines · Fengnan Chang
In
blk_hctx_poll, it always check task is running or not, and return 1 if task is running, it's not reasonable for current caller, especiallyio_uring, which is always running and causeBLK_POLL_ONESHOTis set.It looks like there has been this judgment for historical reasons, and in very early versions of this function the user would set the process state to
TASK_UNINTERRUPTIBLE.Signed-off-by Diangang Li <[email protected]>Signed-off-by Fengnan Chang <[email protected]>--- block/blk-mq.c | 2 -- 1 file changed, 2 deletions(-)block/blk-mq.c +0
-0
block/blk-mq.c static int blk_hctx_poll(struct request_queue *q, struct blk_mq_hw_ctx *hctx, · line 5177
Quoted 8 lines
block/blk-mq.c +0
-2@@ -5172,8 +5172,6 @@ static int blk_hctx_poll(struct request_queue *q, struct blk_mq_hw_ctx *hctx,if (signal_pending_state (state, current))__set_current_state (TASK_RUNNING ); -if (task_is_running (current)) -return 1 ;if (ret <0 || (flags &BLK_POLL_ONESHOT ))break ;
Heh yes, this is completely a leftover part from when polled IO was purely synchronous and the completion would wake it. Similarly, the __set_current_state(TASK_RUNNING) a bit further down needs to die as well.
I'll check your patch 2 tomorrow. Not loving the complete switch from the singly linked list to the double linked, that's going to slow down both addition and iteration of the req freelist. So I think something needs to change there, but haven't really gone deep with it yet.
Signature · 1 line
Jens Axboe
Jens Axboe
maintainerRe: [RFC PATCH 1/2] blk-mq: delete task running check in blk_hctx_poll
In reply to Fengnan ChangQuoted 19 lines · Fengnan Chang
In
blk_hctx_poll, it always check task is running or not, and return 1 if task is running, it's not reasonable for current caller, especiallyio_uring, which is always running and causeBLK_POLL_ONESHOTis set.It looks like there has been this judgment for historical reasons, and in very early versions of this function the user would set the process state to
TASK_UNINTERRUPTIBLE.Signed-off-by Diangang Li <[email protected]>Signed-off-by Fengnan Chang <[email protected]>--- block/blk-mq.c | 2 -- 1 file changed, 2 deletions(-)block/blk-mq.c +0
-0
block/blk-mq.c static int blk_hctx_poll(struct request_queue *q, struct blk_mq_hw_ctx *hctx, · line 5177
Quoted 8 lines
block/blk-mq.c +0
-2@@ -5172,8 +5172,6 @@ static int blk_hctx_poll(struct request_queue *q, struct blk_mq_hw_ctx *hctx,if (signal_pending_state (state, current))__set_current_state (TASK_RUNNING ); -if (task_is_running (current)) -return 1 ;if (ret <0 || (flags &BLK_POLL_ONESHOT ))break ;
Applied with the other task-is-running remnants killed, too.
Signature · 1 line
Jens Axboe
RFC PATCH 2/2 io_uring: fix io may accumulation in poll mode
In reply to Fengnan ChangIn the io_do_iopoll function, when the poll loop of iopoll_list ends, it is considered that the current req is the actual completed request. This may be reasonable for multi-queue ctx, but is problematic for single-queue ctx because the current request may not be done when the poll gets to the result. In this case, the completed io needs to wait for the first io on the chain to complete before notifying the user, which may cause io accumulation in the list.
Our modification plan is as follows: change io_wq_work_list to normal list so that the iopoll_list list in it can be removed and put into the comp_reqs list when the request is completed. This way each io is handled independently and all gets processed in time.
After modification, test with:
./t/io_uring -p1 -d128 -b4096 -s32 -c32 -F1 -B1 -R1 -X1 -n1 -P1 /dev/nvme6n1
base IOPS is 725K, patch IOPS is 782K.
./t/io_uring -p1 -d128 -b4096 -s32 -c1 -F1 -B1 -R1 -X1 -n1 -P1 /dev/nvme6n1
Base IOPS is 880k, patch IOPS is 895K.
--- include/linux/blk-mq.h | 1 + include/linux/blkdev.h | 1 + include/linux/io_uring_types.h | 8 +++--- io_uring/io_uring.c | 51 ++++++++++++++++------------------ io_uring/io_uring.h | 15 ++++------ io_uring/rw.c | 35 +++++++---------------- io_uring/slist.h | 9 ------ io_uring/sqpoll.c | 8 +++--- 8 files changed, 49 insertions(+), 79 deletions(-)
include/linux/blk-mq.h +1 -0
@@ -902,6 +902,7 @@ static inline bool blk_mq_add_to_batch(struct request *req,return false; iob->need_ts |=blk_mq_need_time_stamp (req);rq_list_add_tail (&iob->req_list, req); + iob->req_count++;return true; }
include/linux/blkdev.h +1 -0
@@ -1822,6 +1822,7 @@ void bdev_fput(struct file *bdev_file);struct io_comp_batch {struct rq_list req_list; +unsigned int req_count;bool need_ts;void (*complete)(struct io_comp_batch *); };
include/linux/io_uring_types.h +4 -4
@@ -235,9 +235,9 @@ struct io_submit_link {struct io_submit_state {/* inline/task_work completion list, under ->uring_lock */ -struct io_wq_work_node free_list; +struct list_head free_list;/* batch completion logic */ -struct io_wq_work_list compl_reqs; +struct list_head compl_reqs;struct io_submit_link link;bool plug_started;
@@ -317,7 +317,7 @@ struct io_ring_ctx {* manipulate the list, hence no extra locking is needed there. */ bool poll_multi_queue; -struct io_wq_work_list iopoll_list; +struct list_head iopoll_list;struct io_file_table file_table;struct io_rsrc_data buf_table;
@@ -695,7 +695,7 @@ struct io_kiocb {union {/* used by request caches, completion batching and iopoll */ -struct io_wq_work_node comp_list; +struct list_head comp_list;/* cache ->apoll->events */ __poll_t apoll_events; };
io_uring/io_uring.c +24 -27
167 lines not on this page · raw message
io_uring/io_uring.h +5 -10
@@ -215,7 +215,7 @@ static inline void io_req_task_work_add(struct io_kiocb *req)static inline void io_submit_flush_completions (struct io_ring_ctx *ctx) { -if (!wq_list_empty (&ctx->submit_state.compl_reqs) || +if (!list_empty (&ctx->submit_state.compl_reqs) || ctx->submit_state.cq_flush)__io_submit_flush_completions (ctx); }
@@ -502,7 +502,7 @@ static inline void io_req_complete_defer(struct io_kiocb *req)lockdep_assert_held (&req->ctx->uring_lock); -wq_list_add_tail (&req->comp_list, &state->compl_reqs); +list_add_tail (&req->comp_list, &state->compl_reqs); }static inline void io_commit_cqring_flush (struct io_ring_ctx *ctx)
@@ -521,25 +521,20 @@ static inline void io_get_task_refs(int nr)io_task_refs_refill (tctx); } -static inline bool io_req_cache_empty (struct io_ring_ctx *ctx) -{ -return !ctx->submit_state.free_list.next; -} -extern struct kmem_cache *req_cachep;static inline struct io_kiocb *io_extract_req (struct io_ring_ctx *ctx) {struct io_kiocb *req; - req = container_of(ctx->submit_state.free_list.next, struct io_kiocb, comp_list); -wq_stack_extract (&ctx->submit_state.free_list); + req = list_first_entry(&ctx->submit_state.free_list, struct io_kiocb, comp_list); +list_del_init (&req->comp_list);return req; }static inline bool io_alloc_req (struct io_ring_ctx *ctx,struct io_kiocb **req) { -if (unlikely (io_req_cache_empty (ctx))) { +if (unlikely (list_empty (&ctx->submit_state.free_list))) {if (!__io_alloc_req_refill (ctx))return false; }
io_uring/rw.c +10 -25
@@ -616,6 +616,11 @@ static void io_complete_rw_iopoll(struct kiocb *kiocb, long res)/* order with io_iopoll_complete() checking ->iopoll_completed */ smp_store_release (&req->iopoll_completed,1 ); + + req->cqe.flags =io_put_kbuf (req, req->cqe.res,NULL ); +if (req->opcode !=IORING_OP_URING_CMD ) +io_req_rw_cleanup (req,0 ); +list_move_tail (&req->comp_list, &req->ctx->submit_state.compl_reqs); }static inline void io_rw_done (struct io_kiocb *req,ssize_t ret)
@@ -1326,10 +1331,9 @@ static int io_uring_hybrid_poll(struct io_kiocb *req,int io_do_iopoll (struct io_ring_ctx *ctx,bool force_nonspin) { -struct io_wq_work_node *pos, *start, *prev; +struct io_kiocb *req, *next;unsigned int poll_flags =0 ;DEFINE_IO_COMP_BATCH (iob); -int nr_events =0 ;/* * Only spin for completions if we don't have multiple devices hanging
@@ -1338,8 +1342,7 @@ int io_do_iopoll(struct io_ring_ctx *ctx, bool force_nonspin)if (ctx->poll_multi_queue || force_nonspin) poll_flags |=BLK_POLL_ONESHOT ; -wq_list_for_each (pos, start, &ctx->iopoll_list) { - struct io_kiocb *req = container_of(pos, struct io_kiocb, comp_list); +list_for_each_entry (req, &ctx->iopoll_list, comp_list) {int ret;/*
@@ -1368,32 +1371,14 @@ int io_do_iopoll(struct io_ring_ctx *ctx, bool force_nonspin)if (!rq_list_empty (&iob.req_list)) iob.complete (&iob); -else if (!pos) +else if (list_entry_is_head (req, &ctx->iopoll_list, comp_list))return 0 ; - prev = start; -wq_list_for_each_resume (pos, prev) { - struct io_kiocb *req = container_of(pos, struct io_kiocb, comp_list); - -/* order with io_complete_rw_iopoll(), e.g. ->result updates */ -if (!smp_load_acquire (&req->iopoll_completed)) -break ; - nr_events++; - req->cqe.flags =io_put_kbuf (req, req->cqe.res,NULL ); -if (req->opcode !=IORING_OP_URING_CMD ) -io_req_rw_cleanup (req,0 ); - } -if (unlikely (!nr_events)) +if (unlikely (!iob.req_count))return 0 ; - pos = start ? start->next : ctx->iopoll_list.first; -wq_list_cut (&ctx->iopoll_list, prev, start); - -if (WARN_ON_ONCE (!wq_list_empty (&ctx->submit_state.compl_reqs))) -return 0 ; - ctx->submit_state.compl_reqs.first = pos;__io_submit_flush_completions (ctx); -return nr_events; +return iob.req_count; }void io_rw_cache_free (const void *entry)
io_uring/slist.h +0 -9
@@ -99,15 +99,6 @@ static inline void wq_list_del(struct io_wq_work_list *list,wq_list_cut (list, node, prev); } -static inline -struct io_wq_work_node *wq_stack_extract (struct io_wq_work_node *stack) -{ -struct io_wq_work_node *node = stack->next; - - stack->next = node->next; -return node; -} -static inline struct io_wq_work *wq_next_work (struct io_wq_work *work) {if (!work->list.next)
io_uring/sqpoll.c +4 -4
@@ -211,7 +211,7 @@ static int __io_sq_thread(struct io_ring_ctx *ctx, struct io_sq_data *sqd,if (cap_entries && to_submit >IORING_SQPOLL_CAP_ENTRIES_VALUE ) to_submit =IORING_SQPOLL_CAP_ENTRIES_VALUE ; -if (to_submit || !wq_list_empty (&ctx->iopoll_list)) { +if (to_submit || !list_empty (&ctx->iopoll_list)) {const struct cred *creds =NULL ;io_sq_start_worktime (ist);
@@ -220,7 +220,7 @@ static int __io_sq_thread(struct io_ring_ctx *ctx, struct io_sq_data *sqd, creds =override_creds (ctx->sq_creds);mutex_lock (&ctx->uring_lock); -if (!wq_list_empty (&ctx->iopoll_list)) +if (!list_empty (&ctx->iopoll_list))io_do_iopoll (ctx, true);/*
@@ -343,7 +343,7 @@ static int io_sq_thread(void *data)list_for_each_entry (ctx, &sqd->ctx_list, sqd_list) {int ret =__io_sq_thread (ctx, sqd, cap_entries, &ist); -if (!sqt_spin && (ret >0 || !wq_list_empty (&ctx->iopoll_list))) +if (!sqt_spin && (ret >0 || !list_empty (&ctx->iopoll_list))) sqt_spin = true; }if (io_sq_tw (&retry_list,IORING_TW_CAP_ENTRIES_VALUE ))
@@ -378,7 +378,7 @@ static int io_sq_thread(void *data)atomic_or (IORING_SQ_NEED_WAKEUP , &ctx->rings->sq_flags);if ((ctx->flags &IORING_SETUP_IOPOLL ) && - !wq_list_empty (&ctx->iopoll_list)) { + !list_empty (&ctx->iopoll_list)) { needs_sched = false;break ; }
Signature · 1 line
2.39.5 (Apple Git-154)
Jens Axboe
maintainerQuoted 17 lines · Fengnan Chang
In the
io_do_iopollfunction, when the poll loop ofiopoll_listends, it is considered that the current req is the actual completed request. This may be reasonable for multi-queue ctx, but is problematic for single-queue ctx because the current request may not be done when the poll gets to the result. In this case, the completed io needs to wait for the first io on the chain to complete before notifying the user, which may cause io accumulation in the list.
Our modification plan is as follows: changeio_wq_work_listto normal list so that theiopoll_listlist in it can be removed and put into thecomp_reqslist when the request is completed. This way each io is handled independently and all gets processed in time.After modification, test with:
./t/io_uring -p1 -d128 -b4096 -s32 -c32 -F1 -B1 -R1 -X1 -n1 -P1
/dev/nvme6n1
Quoted 6 lines
base IOPS is 725K, patch IOPS is 782K.
./t/io_uring -p1 -d128 -b4096 -s32 -c1 -F1 -B1 -R1 -X1 -n1 -P1
/dev/nvme6n1Base IOPS is 880k, patch IOPS is 895K.
A few notes on this:
1) Manipulating the list in io_complete_rw_iopoll() I don't think is necessarily safe. Yes generally this is invoked from the owning/polling task, but that's not guaranteed.
2) The patch doesn't apply to the current tree, must be an older version?
3) When hand-applied, it still throws a compile warning about an unused variable. Please don't send untested stuff...
4) Don't just blatantly bloat the io_kiocb. When you change from a singly to a doubly linked list, you're growing the io_kiocb size. You should be able to use a union with struct io_task_work for example. That's already 16b in size - win/win as you don't need to slow down the cache management as that can keep using the linkage it currently is using, and you're not bloating the io_kiocb.
5) The already mentioned point about the cache free list now being doubly linked. This is generally a _bad_ idea as removing and adding entries now need to touch other entries too. That's not very cache friendly.
#1 is kind of the big one, as it means you'll need to re-think how you do this. I do agree that the current approach isn't necessarily ideal as we don't process completions as quickly as we could, so I think there's merrit in continuing this work.
Signature · 1 line
Jens Axboe
Jens Axboe
maintainerQuoted 49 lines · Jens Axboe
Quoted 17 lines · Fengnan Chang
In the
io_do_iopollfunction, when the poll loop ofiopoll_listends, it is considered that the current req is the actual completed request. This may be reasonable for multi-queue ctx, but is problematic for single-queue ctx because the current request may not be done when the poll gets to the result. In this case, the completed io needs to wait for the first io on the chain to complete before notifying the user, which may cause io accumulation in the list.
Our modification plan is as follows: changeio_wq_work_listto normal list so that theiopoll_listlist in it can be removed and put into thecomp_reqslist when the request is completed. This way each io is handled independently and all gets processed in time.After modification, test with:
./t/io_uring -p1 -d128 -b4096 -s32 -c32 -F1 -B1 -R1 -X1 -n1 -P1
/dev/nvme6n1Quoted 6 lines
base IOPS is 725K, patch IOPS is 782K.
./t/io_uring -p1 -d128 -b4096 -s32 -c1 -F1 -B1 -R1 -X1 -n1 -P1
/dev/nvme6n1Base IOPS is 880k, patch IOPS is 895K.
A few notes on this:
1) Manipulating the list in
io_complete_rw_iopoll()I don't think is necessarily safe. Yes generally this is invoked from the owning/polling task, but that's not guaranteed.2) The patch doesn't apply to the current tree, must be an older version?
3) When hand-applied, it still throws a compile warning about an unused variable. Please don't send untested stuff...
4) Don't just blatantly bloat the
io_kiocb. When you change from a singly to a doubly linked list, you're growing theio_kiocbsize. You should be able to use a union with structio_task_workfor example. That's already 16b in size - win/win as you don't need to slow down the cache management as that can keep using the linkage it currently is using, and you're not bloating theio_kiocb.5) The already mentioned point about the cache free list now being doubly linked. This is generally a _bad_ idea as removing and adding entries now need to touch other entries too. That's not very cache friendly.
Quoted 4 lines
#1 is kind of the big one, as it means you'll need to re-think how you do this. I do agree that the current approach isn't necessarily ideal as we don't process completions as quickly as we could, so I think there's merrit in continuing this work.
Proof of concept below, entirely untested, at a conference. Basically does what I describe above, and retains the list manipulation logic on the iopoll side, rather than on the completion side. Can you take a look at this? And if it runs, can you do some numbers on that too?
include/linux/io_uring_types.h +11 -2
@@ -316,7 +316,7 @@ struct io_ring_ctx {* manipulate the list, hence no extra locking is needed there. */ bool poll_multi_queue; -struct io_wq_work_list iopoll_list; +struct list_head iopoll_list;struct io_file_table file_table;struct io_rsrc_data buf_table;
@@ -708,7 +708,16 @@ struct io_kiocb {atomic_t refs;bool cancel_seq_set; -struct io_task_work io_task_work; + +/* +* IOPOLL doesn't use task_work, so use the ->iopoll_node list +* entry to manage pending iopoll requests. +*/ +union { +struct io_task_work io_task_work; +struct list_head iopoll_node; + }; +union {/* * for polled requests, i.e. IORING_OP_POLL_ADD and async armed
io_uring/cancel.c +1 -1
@@ -534,7 +534,7 @@ __cold bool io_uring_try_cancel_requests(struct io_ring_ctx *ctx,/* SQPOLL thread does its own polling */ if ((!(ctx->flags &IORING_SETUP_SQPOLL ) && cancel_all) || is_sqpoll_thread) { -while (!wq_list_empty (&ctx->iopoll_list)) { +while (!list_empty (&ctx->iopoll_list)) {io_iopoll_try_reap_events (ctx); ret = true;cond_resched ();
io_uring/io_uring.c +8 -19
@@ -334,7 +334,7 @@ static __cold struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p)init_waitqueue_head (&ctx->poll_wq);spin_lock_init (&ctx->completion_lock);raw_spin_lock_init (&ctx->timeout_lock); -INIT_WQ_LIST (&ctx->iopoll_list); +INIT_LIST_HEAD (&ctx->iopoll_list);INIT_LIST_HEAD (&ctx->defer_list);INIT_LIST_HEAD (&ctx->timeout_list);INIT_LIST_HEAD (&ctx->ltimeout_list);
@@ -1561,7 +1561,7 @@ __cold void io_iopoll_try_reap_events(struct io_ring_ctx *ctx)return ;mutex_lock (&ctx->uring_lock); -while (!wq_list_empty (&ctx->iopoll_list)) { +while (!list_empty (&ctx->iopoll_list)) {/* let it sleep and repeat later if can't complete a request */ if (io_do_iopoll (ctx, true) ==0 )break ;
@@ -1626,21 +1626,18 @@ static int io_iopoll_check(struct io_ring_ctx *ctx, unsigned int min_events)* forever, while the workqueue is stuck trying to acquire the * very same mutex. */ -if (wq_list_empty (&ctx->iopoll_list) || -io_task_work_pending (ctx)) { +if (list_empty (&ctx->iopoll_list) ||io_task_work_pending (ctx)) {u32 tail = ctx->cached_cq_tail; (void )io_run_local_work_locked (ctx, min_events); -if (task_work_pending (current) || -wq_list_empty (&ctx->iopoll_list)) { +if (task_work_pending (current) ||list_empty (&ctx->iopoll_list)) {mutex_unlock (&ctx->uring_lock);io_run_task_work ();mutex_lock (&ctx->uring_lock); }/* some requests don't go through iopoll_list */ -if (tail != ctx->cached_cq_tail || -wq_list_empty (&ctx->iopoll_list)) +if (tail != ctx->cached_cq_tail ||list_empty (&ctx->iopoll_list))break ; } ret =io_do_iopoll (ctx, !min_events);
@@ -1683,25 +1680,17 @@ static void io_iopoll_req_issued(struct io_kiocb *req, unsigned int issue_flags)* how we do polling eventually, not spinning if we're on potentially * different devices. */ -if (wq_list_empty (&ctx->iopoll_list)) { +if (list_empty (&ctx->iopoll_list)) { ctx->poll_multi_queue = false; }else if (!ctx->poll_multi_queue) {struct io_kiocb *list_req; - list_req = container_of(ctx->iopoll_list.first, struct io_kiocb, - comp_list); + list_req = list_entry(ctx->iopoll_list.next, struct io_kiocb, iopoll_node);if (list_req->file != req->file) ctx->poll_multi_queue = true; } -/* -* For fast devices, IO may have already completed. If it has, add -* it to the front so we find it first. -*/ -if (READ_ONCE (req->iopoll_completed)) -wq_list_add_head (&req->comp_list, &ctx->iopoll_list); -else -wq_list_add_tail (&req->comp_list, &ctx->iopoll_list); +list_add_tail (&req->iopoll_node, &ctx->iopoll_list);if (unlikely (needs_lock)) {/*
io_uring/rw.c +8 -20
@@ -1315,9 +1315,9 @@ static int io_uring_hybrid_poll(struct io_kiocb *req,int io_do_iopoll (struct io_ring_ctx *ctx,bool force_nonspin) { -struct io_wq_work_node *pos, *start, *prev;unsigned int poll_flags =0 ;DEFINE_IO_COMP_BATCH (iob); +struct io_kiocb *req, *tmp;int nr_events =0 ;/*
@@ -1327,8 +1327,7 @@ int io_do_iopoll(struct io_ring_ctx *ctx, bool force_nonspin)if (ctx->poll_multi_queue || force_nonspin) poll_flags |=BLK_POLL_ONESHOT ; -wq_list_for_each (pos, start, &ctx->iopoll_list) { - struct io_kiocb *req = container_of(pos, struct io_kiocb, comp_list); +list_for_each_entry (req, &ctx->iopoll_list, iopoll_node) {int ret;/*
@@ -1357,31 +1356,20 @@ int io_do_iopoll(struct io_ring_ctx *ctx, bool force_nonspin)if (!rq_list_empty (&iob.req_list)) iob.complete (&iob); -else if (!pos) -return 0 ; - - prev = start; -wq_list_for_each_resume (pos, prev) { - struct io_kiocb *req = container_of(pos, struct io_kiocb, comp_list); +list_for_each_entry_safe (req, tmp, &ctx->iopoll_list, iopoll_node) {/* order with io_complete_rw_iopoll(), e.g. ->result updates */ if (!smp_load_acquire (&req->iopoll_completed)) -break ; +continue ; +list_del (&req->iopoll_node); +wq_list_add_tail (&req->comp_list, &ctx->submit_state.compl_reqs); nr_events++; req->cqe.flags =io_put_kbuf (req, req->cqe.res,NULL );if (req->opcode !=IORING_OP_URING_CMD )io_req_rw_cleanup (req,0 ); } -if (unlikely (!nr_events)) -return 0 ; - - pos = start ? start->next : ctx->iopoll_list.first; -wq_list_cut (&ctx->iopoll_list, prev, start); - -if (WARN_ON_ONCE (!wq_list_empty (&ctx->submit_state.compl_reqs))) -return 0 ; - ctx->submit_state.compl_reqs.first = pos; -__io_submit_flush_completions (ctx); +if (nr_events) +__io_submit_flush_completions (ctx);return nr_events; }
io_uring/sqpoll.c +4 -4
@@ -212,7 +212,7 @@ static int __io_sq_thread(struct io_ring_ctx *ctx, struct io_sq_data *sqd,if (cap_entries && to_submit >IORING_SQPOLL_CAP_ENTRIES_VALUE ) to_submit =IORING_SQPOLL_CAP_ENTRIES_VALUE ; -if (to_submit || !wq_list_empty (&ctx->iopoll_list)) { +if (to_submit || !list_empty (&ctx->iopoll_list)) {const struct cred *creds =NULL ;io_sq_start_worktime (ist);
@@ -221,7 +221,7 @@ static int __io_sq_thread(struct io_ring_ctx *ctx, struct io_sq_data *sqd, creds =override_creds (ctx->sq_creds);mutex_lock (&ctx->uring_lock); -if (!wq_list_empty (&ctx->iopoll_list)) +if (!list_empty (&ctx->iopoll_list))io_do_iopoll (ctx, true);/*
@@ -344,7 +344,7 @@ static int io_sq_thread(void *data)list_for_each_entry (ctx, &sqd->ctx_list, sqd_list) {int ret =__io_sq_thread (ctx, sqd, cap_entries, &ist); -if (!sqt_spin && (ret >0 || !wq_list_empty (&ctx->iopoll_list))) +if (!sqt_spin && (ret >0 || !list_empty (&ctx->iopoll_list))) sqt_spin = true; }if (io_sq_tw (&retry_list,IORING_TW_CAP_ENTRIES_VALUE ))
@@ -379,7 +379,7 @@ static int io_sq_thread(void *data)atomic_or (IORING_SQ_NEED_WAKEUP , &ctx->rings->sq_flags);if ((ctx->flags &IORING_SETUP_IOPOLL ) && - !wq_list_empty (&ctx->iopoll_list)) { + !list_empty (&ctx->iopoll_list)) { needs_sched = false;break ; }
Signature · 1 line
Jens Axboe
Quoted 49 lines · Jens Axboe
Quoted 48 lines · Jens Axboe
Quoted 17 lines · Fengnan Chang
In the
io_do_iopollfunction, when the poll loop ofiopoll_listends, it is considered that the current req is the actual completed request. This may be reasonable for multi-queue ctx, but is problematic for single-queue ctx because the current request may not be done when the poll gets to the result. In this case, the completed io needs to wait for the first io on the chain to complete before notifying the user, which may cause io accumulation in the list.
Our modification plan is as follows: changeio_wq_work_listto normal list so that theiopoll_listlist in it can be removed and put into thecomp_reqslist when the request is completed. This way each io is handled independently and all gets processed in time.After modification, test with:
./t/io_uring -p1 -d128 -b4096 -s32 -c32 -F1 -B1 -R1 -X1 -n1 -P1
/dev/nvme6n1Quoted 6 lines
base IOPS is 725K, patch IOPS is 782K.
./t/io_uring -p1 -d128 -b4096 -s32 -c1 -F1 -B1 -R1 -X1 -n1 -P1
/dev/nvme6n1Base IOPS is 880k, patch IOPS is 895K.
A few notes on this:
1) Manipulating the list in
io_complete_rw_iopoll()I don't think is necessarily safe. Yes generally this is invoked from the owning/polling task, but that's not guaranteed.2) The patch doesn't apply to the current tree, must be an older version?
3) When hand-applied, it still throws a compile warning about an unused variable. Please don't send untested stuff...
4) Don't just blatantly bloat the
io_kiocb. When you change from a singly to a doubly linked list, you're growing theio_kiocbsize. You should be able to use a union with structio_task_workfor example. That's already 16b in size - win/win as you don't need to slow down the cache management as that can keep using the linkage it currently is using, and you're not bloating theio_kiocb.5) The already mentioned point about the cache free list now being doubly linked. This is generally a _bad_ idea as removing and adding entries now need to touch other entries too. That's not very cache friendly.
Quoted 8 lines
Quoted 4 lines
#1 is kind of the big one, as it means you'll need to re-think how you do this. I do agree that the current approach isn't necessarily ideal as we don't process completions as quickly as we could, so I think there's merrit in continuing this work.
Proof of concept below, entirely untested, at a conference. Basically does what I describe above, and retains the list manipulation logic on the iopoll side, rather than on the completion side. Can you take a look at this? And if it runs, can you do some numbers on that too?
This patch works, and in my test case, the performance is identical to my patch. But there is a small problem, now looking for completed requests, always need to traverse the whole iopoll_list. this can be a bit inefficient in some cases, for example if the previous sent 128K io , the last io is 4K, the last io will be returned much earlier, this kind of scenario can not be verified in the current test. I'm not sure if it's a meaningful scenario.
Quoted 224 lines
include/linux/io_uring_types.h +11
-2@@ -316,7 +316,7 @@ struct io_ring_ctx {* manipulate the list, hence no extra locking is needed there. */ bool poll_multi_queue; -struct io_wq_work_list iopoll_list; +struct list_head iopoll_list;struct io_file_table file_table;struct io_rsrc_data buf_table;@@ -708,7 +708,16 @@ struct io_kiocb {atomic_t refs;bool cancel_seq_set; -struct io_task_work io_task_work; + +/* +* IOPOLL doesn't use task_work, so use the ->iopoll_node list +* entry to manage pending iopoll requests. +*/ +union { +struct io_task_work io_task_work; +struct list_head iopoll_node; + }; +union {/* * for polled requests, i.e. IORING_OP_POLL_ADD and async armed io_uring/cancel.c +1
-1@@ -534,7 +534,7 @@ __cold bool io_uring_try_cancel_requests(struct io_ring_ctx *ctx,/* SQPOLL thread does its own polling */ if ((!(ctx->flags &IORING_SETUP_SQPOLL ) && cancel_all) || is_sqpoll_thread) { -while (!wq_list_empty (&ctx->iopoll_list)) { +while (!list_empty (&ctx->iopoll_list)) {io_iopoll_try_reap_events (ctx); ret = true;cond_resched ();io_uring/io_uring.c +8
-19@@ -334,7 +334,7 @@ static __cold struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p)init_waitqueue_head (&ctx->poll_wq);spin_lock_init (&ctx->completion_lock);raw_spin_lock_init (&ctx->timeout_lock); -INIT_WQ_LIST (&ctx->iopoll_list); +INIT_LIST_HEAD (&ctx->iopoll_list);INIT_LIST_HEAD (&ctx->defer_list);INIT_LIST_HEAD (&ctx->timeout_list);INIT_LIST_HEAD (&ctx->ltimeout_list);@@ -1561,7 +1561,7 @@ __cold void io_iopoll_try_reap_events(struct io_ring_ctx *ctx)return ;mutex_lock (&ctx->uring_lock); -while (!wq_list_empty (&ctx->iopoll_list)) { +while (!list_empty (&ctx->iopoll_list)) {/* let it sleep and repeat later if can't complete a request */ if (io_do_iopoll (ctx, true) ==0 )break ;@@ -1626,21 +1626,18 @@ static int io_iopoll_check(struct io_ring_ctx *ctx, unsigned int min_events)* forever, while the workqueue is stuck trying to acquire the * very same mutex. */ -if (wq_list_empty (&ctx->iopoll_list) || -io_task_work_pending (ctx)) { +if (list_empty (&ctx->iopoll_list) ||io_task_work_pending (ctx)) {u32 tail = ctx->cached_cq_tail; (void )io_run_local_work_locked (ctx, min_events); -if (task_work_pending (current) || -wq_list_empty (&ctx->iopoll_list)) { +if (task_work_pending (current) ||list_empty (&ctx->iopoll_list)) {mutex_unlock (&ctx->uring_lock);io_run_task_work ();mutex_lock (&ctx->uring_lock); }/* some requests don't go through iopoll_list */ -if (tail != ctx->cached_cq_tail || -wq_list_empty (&ctx->iopoll_list)) +if (tail != ctx->cached_cq_tail ||list_empty (&ctx->iopoll_list))break ; } ret =io_do_iopoll (ctx, !min_events);@@ -1683,25 +1680,17 @@ static void io_iopoll_req_issued(struct io_kiocb *req, unsigned int issue_flags)* how we do polling eventually, not spinning if we're on potentially * different devices. */ -if (wq_list_empty (&ctx->iopoll_list)) { +if (list_empty (&ctx->iopoll_list)) { ctx->poll_multi_queue = false; }else if (!ctx->poll_multi_queue) {struct io_kiocb *list_req; - list_req = container_of(ctx->iopoll_list.first, struct io_kiocb, - comp_list); + list_req = list_entry(ctx->iopoll_list.next, struct io_kiocb, iopoll_node);if (list_req->file != req->file) ctx->poll_multi_queue = true; } -/* -* For fast devices, IO may have already completed. If it has, add -* it to the front so we find it first. -*/ -if (READ_ONCE (req->iopoll_completed)) -wq_list_add_head (&req->comp_list, &ctx->iopoll_list); -else -wq_list_add_tail (&req->comp_list, &ctx->iopoll_list); +list_add_tail (&req->iopoll_node, &ctx->iopoll_list);if (unlikely (needs_lock)) {/* io_uring/rw.c +8
-20@@ -1315,9 +1315,9 @@ static int io_uring_hybrid_poll(struct io_kiocb *req,int io_do_iopoll (struct io_ring_ctx *ctx,bool force_nonspin) { -struct io_wq_work_node *pos, *start, *prev;unsigned int poll_flags =0 ;DEFINE_IO_COMP_BATCH (iob); +struct io_kiocb *req, *tmp;int nr_events =0 ;/* @@ -1327,8 +1327,7 @@ int io_do_iopoll(struct io_ring_ctx *ctx, bool force_nonspin)if (ctx->poll_multi_queue || force_nonspin) poll_flags |=BLK_POLL_ONESHOT ; -wq_list_for_each (pos, start, &ctx->iopoll_list) { - struct io_kiocb *req = container_of(pos, struct io_kiocb, comp_list); +list_for_each_entry (req, &ctx->iopoll_list, iopoll_node) {int ret;/* @@ -1357,31 +1356,20 @@ int io_do_iopoll(struct io_ring_ctx *ctx, bool force_nonspin)if (!rq_list_empty (&iob.req_list)) iob.complete (&iob); -else if (!pos) -return 0 ; - - prev = start; -wq_list_for_each_resume (pos, prev) { - struct io_kiocb *req = container_of(pos, struct io_kiocb, comp_list); +list_for_each_entry_safe (req, tmp, &ctx->iopoll_list, iopoll_node) {/* order with io_complete_rw_iopoll(), e.g. ->result updates */ if (!smp_load_acquire (&req->iopoll_completed)) -break ; +continue ; +list_del (&req->iopoll_node); +wq_list_add_tail (&req->comp_list, &ctx->submit_state.compl_reqs); nr_events++; req->cqe.flags =io_put_kbuf (req, req->cqe.res,NULL );if (req->opcode !=IORING_OP_URING_CMD )io_req_rw_cleanup (req,0 ); } -if (unlikely (!nr_events)) -return 0 ; - - pos = start ? start->next : ctx->iopoll_list.first; -wq_list_cut (&ctx->iopoll_list, prev, start); - -if (WARN_ON_ONCE (!wq_list_empty (&ctx->submit_state.compl_reqs))) -return 0 ; - ctx->submit_state.compl_reqs.first = pos; -__io_submit_flush_completions (ctx); +if (nr_events) +__io_submit_flush_completions (ctx);return nr_events; }io_uring/sqpoll.c +4
-4@@ -212,7 +212,7 @@ static int __io_sq_thread(struct io_ring_ctx *ctx, struct io_sq_data *sqd,if (cap_entries && to_submit >IORING_SQPOLL_CAP_ENTRIES_VALUE ) to_submit =IORING_SQPOLL_CAP_ENTRIES_VALUE ; -if (to_submit || !wq_list_empty (&ctx->iopoll_list)) { +if (to_submit || !list_empty (&ctx->iopoll_list)) {const struct cred *creds =NULL ;io_sq_start_worktime (ist);@@ -221,7 +221,7 @@ static int __io_sq_thread(struct io_ring_ctx *ctx, struct io_sq_data *sqd, creds =override_creds (ctx->sq_creds);mutex_lock (&ctx->uring_lock); -if (!wq_list_empty (&ctx->iopoll_list)) +if (!list_empty (&ctx->iopoll_list))io_do_iopoll (ctx, true);/* @@ -344,7 +344,7 @@ static int io_sq_thread(void *data)list_for_each_entry (ctx, &sqd->ctx_list, sqd_list) {int ret =__io_sq_thread (ctx, sqd, cap_entries, &ist); -if (!sqt_spin && (ret >0 || !wq_list_empty (&ctx->iopoll_list))) +if (!sqt_spin && (ret >0 || !list_empty (&ctx->iopoll_list))) sqt_spin = true; }if (io_sq_tw (&retry_list,IORING_TW_CAP_ENTRIES_VALUE ))@@ -379,7 +379,7 @@ static int io_sq_thread(void *data)atomic_or (IORING_SQ_NEED_WAKEUP , &ctx->rings->sq_flags);if ((ctx->flags &IORING_SETUP_IOPOLL ) && - !wq_list_empty (&ctx->iopoll_list)) { + !list_empty (&ctx->iopoll_list)) { needs_sched = false;break ; }
Jens Axboe
maintainerQuoted 56 lines · Fengnan
? 2025/12/11 12:10, Jens Axboe ??:
Quoted 53 lines · Jens Axboe
Quoted 52 lines · Fengnan Chang
Quoted 17 lines
In the
io_do_iopollfunction, when the poll loop ofiopoll_listends, it is considered that the current req is the actual completed request. This may be reasonable for multi-queue ctx, but is problematic for single-queue ctx because the current request may not be done when the poll gets to the result. In this case, the completed io needs to wait for the first io on the chain to complete before notifying the user, which may cause io accumulation in the list.
Our modification plan is as follows: changeio_wq_work_listto normal list so that theiopoll_listlist in it can be removed and put into thecomp_reqslist when the request is completed. This way each io is handled independently and all gets processed in time.After modification, test with:
./t/io_uring -p1 -d128 -b4096 -s32 -c32 -F1 -B1 -R1 -X1 -n1 -P1
/dev/nvme6n1Quoted 6 lines
base IOPS is 725K, patch IOPS is 782K.
./t/io_uring -p1 -d128 -b4096 -s32 -c1 -F1 -B1 -R1 -X1 -n1 -P1
/dev/nvme6n1Base IOPS is 880k, patch IOPS is 895K.
A few notes on this:
1) Manipulating the list in
io_complete_rw_iopoll()I don't think is necessarily safe. Yes generally this is invoked from the owning/polling task, but that's not guaranteed.2) The patch doesn't apply to the current tree, must be an older version?
3) When hand-applied, it still throws a compile warning about an unused variable. Please don't send untested stuff...
4) Don't just blatantly bloat the
io_kiocb. When you change from a singly to a doubly linked list, you're growing theio_kiocbsize. You should be able to use a union with structio_task_workfor example. That's already 16b in size - win/win as you don't need to slow down the cache management as that can keep using the linkage it currently is using, and you're not bloating theio_kiocb.5) The already mentioned point about the cache free list now being doubly linked. This is generally a _bad_ idea as removing and adding entries now need to touch other entries too. That's not very cache friendly.
#1 is kind of the big one, as it means you'll need to re-think how you do this. I do agree that the current approach isn't necessarily ideal as we don't process completions as quickly as we could, so I think there's merrit in continuing this work.
Quoted 7 lines
Quoted 4 lines
Proof of concept below, entirely untested, at a conference. Basically does what I describe above, and retains the list manipulation logic on the iopoll side, rather than on the completion side. Can you take a look at this? And if it runs, can you do some numbers on that too?
This patch works, and in my test case, the performance is identical to my patch.
Good!
Quoted 6 lines
But there is a small problem, now looking for completed requests, always need to traverse the whole
iopoll_list. this can be a bit inefficient in some cases, for example if the previous sent 128K io , the last io is 4K, the last io will be returned much earlier, this kind of scenario can not be verified in the current test. I'm not sure if it's a meaningful scenario.
Not sure that's a big problem, you're just spinning to complete anyway. You could add your iob->nr_reqs or something, and break after finding those know have completed. That won't necessarily change anything, could still be the last one that completed. Would probably make more sense to pass in 'min_events' or similar and stop after that. But I think mostly tweaks that can be made after the fact. If you want to send out a new patch based on the one I sent, feel free to.
Signature · 1 line
Jens Axboe
Jens Axboe
maintainerQuoted 74 lines · Jens Axboe
Quoted 56 lines · Fengnan
? 2025/12/11 12:10, Jens Axboe ??:
Quoted 53 lines · Jens Axboe
Quoted 52 lines · Fengnan Chang
Quoted 17 lines
In the
io_do_iopollfunction, when the poll loop ofiopoll_listends, it is considered that the current req is the actual completed request. This may be reasonable for multi-queue ctx, but is problematic for single-queue ctx because the current request may not be done when the poll gets to the result. In this case, the completed io needs to wait for the first io on the chain to complete before notifying the user, which may cause io accumulation in the list.
Our modification plan is as follows: changeio_wq_work_listto normal list so that theiopoll_listlist in it can be removed and put into thecomp_reqslist when the request is completed. This way each io is handled independently and all gets processed in time.After modification, test with:
./t/io_uring -p1 -d128 -b4096 -s32 -c32 -F1 -B1 -R1 -X1 -n1 -P1
/dev/nvme6n1Quoted 6 lines
base IOPS is 725K, patch IOPS is 782K.
./t/io_uring -p1 -d128 -b4096 -s32 -c1 -F1 -B1 -R1 -X1 -n1 -P1
/dev/nvme6n1Base IOPS is 880k, patch IOPS is 895K.
A few notes on this:
1) Manipulating the list in
io_complete_rw_iopoll()I don't think is necessarily safe. Yes generally this is invoked from the owning/polling task, but that's not guaranteed.2) The patch doesn't apply to the current tree, must be an older version?
3) When hand-applied, it still throws a compile warning about an unused variable. Please don't send untested stuff...
4) Don't just blatantly bloat the
io_kiocb. When you change from a singly to a doubly linked list, you're growing theio_kiocbsize. You should be able to use a union with structio_task_workfor example. That's already 16b in size - win/win as you don't need to slow down the cache management as that can keep using the linkage it currently is using, and you're not bloating theio_kiocb.5) The already mentioned point about the cache free list now being doubly linked. This is generally a _bad_ idea as removing and adding entries now need to touch other entries too. That's not very cache friendly.
#1 is kind of the big one, as it means you'll need to re-think how you do this. I do agree that the current approach isn't necessarily ideal as we don't process completions as quickly as we could, so I think there's merrit in continuing this work.
Quoted 7 lines
Quoted 4 lines
Proof of concept below, entirely untested, at a conference. Basically does what I describe above, and retains the list manipulation logic on the iopoll side, rather than on the completion side. Can you take a look at this? And if it runs, can you do some numbers on that too?
This patch works, and in my test case, the performance is identical to my patch.
Good!
Quoted 6 lines
But there is a small problem, now looking for completed requests, always need to traverse the whole
iopoll_list. this can be a bit inefficient in some cases, for example if the previous sent 128K io , the last io is 4K, the last io will be returned much earlier, this kind of scenario can not be verified in the current test. I'm not sure if it's a meaningful scenario.
Quoted 7 lines
Not sure that's a big problem, you're just spinning to complete anyway. You could add your
iob->nr_reqsor something, and break after finding those know have completed. That won't necessarily change anything, could still be the last one that completed. Would probably make more sense to pass in 'min_events' or similar and stop after that. But I think mostly tweaks that can be made after the fact. If you want to send out a new patch based on the one I sent, feel free to.
Eg, something like this on top would do that. Like I mentioned earlier, you cannot do the list manipulation where you did it, it's not safe. You have to defer it to reaping time. If we could do it from the callback where we mark it complete, then that would surely make things more trivial and avoid iteration when not needed.
include/linux/blk-mq.h +1 -0
@@ -917,6 +917,7 @@ static inline bool blk_mq_add_to_batch(struct request *req,else if (iob->complete != complete)return false; iob->need_ts |=blk_mq_need_time_stamp (req); + iob->nr_reqs++;rq_list_add_tail (&iob->req_list, req);return true; }
include/linux/blkdev.h +1 -0
@@ -1821,6 +1821,7 @@ void bdev_fput(struct file *bdev_file);struct io_comp_batch {struct rq_list req_list;bool need_ts; +int nr_reqs;void (*complete)(struct io_comp_batch *); };
io_uring/rw.c +2 -0
@@ -1358,6 +1358,8 @@ int io_do_iopoll(struct io_ring_ctx *ctx, bool force_nonspin) iob.complete (&iob);list_for_each_entry_safe (req, tmp, &ctx->iopoll_list, iopoll_node) { +if (nr_events == iob.nr_reqs) +break ;/* order with io_complete_rw_iopoll(), e.g. ->result updates */ if (!smp_load_acquire (&req->iopoll_completed))continue ;
Signature · 1 line
Jens Axboe
Quoted 78 lines · Jens Axboe
Quoted 70 lines · Jens Axboe
Quoted 55 lines · Fengnan
? 2025/12/11 12:10, Jens Axboe ??:
Quoted 53 lines · Jens Axboe
Quoted 52 lines · Fengnan Chang
Quoted 17 lines
In the
io_do_iopollfunction, when the poll loop ofiopoll_listends, it is considered that the current req is the actual completed request. This may be reasonable for multi-queue ctx, but is problematic for single-queue ctx because the current request may not be done when the poll gets to the result. In this case, the completed io needs to wait for the first io on the chain to complete before notifying the user, which may cause io accumulation in the list.
Our modification plan is as follows: changeio_wq_work_listto normal list so that theiopoll_listlist in it can be removed and put into thecomp_reqslist when the request is completed. This way each io is handled independently and all gets processed in time.After modification, test with:
./t/io_uring -p1 -d128 -b4096 -s32 -c32 -F1 -B1 -R1 -X1 -n1 -P1
/dev/nvme6n1Quoted 6 lines
base IOPS is 725K, patch IOPS is 782K.
./t/io_uring -p1 -d128 -b4096 -s32 -c1 -F1 -B1 -R1 -X1 -n1 -P1
/dev/nvme6n1Base IOPS is 880k, patch IOPS is 895K.
A few notes on this:
1) Manipulating the list in
io_complete_rw_iopoll()I don't think is necessarily safe. Yes generally this is invoked from the owning/polling task, but that's not guaranteed.2) The patch doesn't apply to the current tree, must be an older version?
3) When hand-applied, it still throws a compile warning about an unused variable. Please don't send untested stuff...
4) Don't just blatantly bloat the
io_kiocb. When you change from a singly to a doubly linked list, you're growing theio_kiocbsize. You should be able to use a union with structio_task_workfor example. That's already 16b in size - win/win as you don't need to slow down the cache management as that can keep using the linkage it currently is using, and you're not bloating theio_kiocb.5) The already mentioned point about the cache free list now being doubly linked. This is generally a _bad_ idea as removing and adding entries now need to touch other entries too. That's not very cache friendly.
#1 is kind of the big one, as it means you'll need to re-think how you do this. I do agree that the current approach isn't necessarily ideal as we don't process completions as quickly as we could, so I think there's merrit in continuing this work.
Quoted 6 lines
Quoted 4 lines
Proof of concept below, entirely untested, at a conference. Basically does what I describe above, and retains the list manipulation logic on the iopoll side, rather than on the completion side. Can you take a look at this? And if it runs, can you do some numbers on that too?
This patch works, and in my test case, the performance is identical to my patch.
Good!
Quoted 6 lines
But there is a small problem, now looking for completed requests, always need to traverse the whole
iopoll_list. this can be a bit inefficient in some cases, for example if the previous sent 128K io , the last io is 4K, the last io will be returned much earlier, this kind of scenario can not be verified in the current test. I'm not sure if it's a meaningful scenario.Quoted 7 lines
Not sure that's a big problem, you're just spinning to complete anyway. You could add your
iob->nr_reqsor something, and break after finding those know have completed. That won't necessarily change anything, could still be the last one that completed. Would probably make more sense to pass in 'min_events' or similar and stop after that. But I think mostly tweaks that can be made after the fact. If you want to send out a new patch based on the one I sent, feel free to.
Quoted 5 lines
Eg, something like this on top would do that. Like I mentioned earlier, you cannot do the list manipulation where you did it, it's not safe. You have to defer it to reaping time. If we could do it from the callback where we mark it complete, then that would surely make things more trivial and avoid iteration when not needed.
Yes, it's not safe do the list manipulation in io_complete_rw_iopoll. It looks more reasonable with the following modifications. Your changes look good enough, but please give me more time, I'd like to do some more testing and rethink this.
Quoted 39 lines
include/linux/blk-mq.h +1
-0@@ -917,6 +917,7 @@ static inline bool blk_mq_add_to_batch(struct request *req,else if (iob->complete != complete)return false; iob->need_ts |=blk_mq_need_time_stamp (req); + iob->nr_reqs++;rq_list_add_tail (&iob->req_list, req);return true; }include/linux/blkdev.h +1
-0@@ -1821,6 +1821,7 @@ void bdev_fput(struct file *bdev_file);struct io_comp_batch {struct rq_list req_list;bool need_ts; +int nr_reqs;void (*complete)(struct io_comp_batch *); };io_uring/rw.c +2
-0@@ -1358,6 +1358,8 @@ int io_do_iopoll(struct io_ring_ctx *ctx, bool force_nonspin) iob.complete (&iob);list_for_each_entry_safe (req, tmp, &ctx->iopoll_list, iopoll_node) { +if (nr_events == iob.nr_reqs) +break ;/* order with io_complete_rw_iopoll(), e.g. ->result updates */ if (!smp_load_acquire (&req->iopoll_completed))continue ;
Jens Axboe
maintainerQuoted 87 lines · Fengnan Chang
? 2025/12/11 18:33, Jens Axboe ??:
Quoted 78 lines · Jens Axboe
Quoted 70 lines · Fengnan
Quoted 55 lines
? 2025/12/11 12:10, Jens Axboe ??:
Quoted 53 lines · Jens Axboe
Quoted 52 lines · Fengnan Chang
Quoted 17 lines
In the
io_do_iopollfunction, when the poll loop ofiopoll_listends, it is considered that the current req is the actual completed request. This may be reasonable for multi-queue ctx, but is problematic for single-queue ctx because the current request may not be done when the poll gets to the result. In this case, the completed io needs to wait for the first io on the chain to complete before notifying the user, which may cause io accumulation in the list.
Our modification plan is as follows: changeio_wq_work_listto normal list so that theiopoll_listlist in it can be removed and put into thecomp_reqslist when the request is completed. This way each io is handled independently and all gets processed in time.After modification, test with:
./t/io_uring -p1 -d128 -b4096 -s32 -c32 -F1 -B1 -R1 -X1 -n1 -P1
/dev/nvme6n1Quoted 6 lines
base IOPS is 725K, patch IOPS is 782K.
./t/io_uring -p1 -d128 -b4096 -s32 -c1 -F1 -B1 -R1 -X1 -n1 -P1
/dev/nvme6n1Base IOPS is 880k, patch IOPS is 895K.
A few notes on this:
1) Manipulating the list in
io_complete_rw_iopoll()I don't think is necessarily safe. Yes generally this is invoked from the owning/polling task, but that's not guaranteed.2) The patch doesn't apply to the current tree, must be an older version?
3) When hand-applied, it still throws a compile warning about an unused variable. Please don't send untested stuff...
4) Don't just blatantly bloat the
io_kiocb. When you change from a singly to a doubly linked list, you're growing theio_kiocbsize. You should be able to use a union with structio_task_workfor example. That's already 16b in size - win/win as you don't need to slow down the cache management as that can keep using the linkage it currently is using, and you're not bloating theio_kiocb.5) The already mentioned point about the cache free list now being doubly linked. This is generally a _bad_ idea as removing and adding entries now need to touch other entries too. That's not very cache friendly.
#1 is kind of the big one, as it means you'll need to re-think how you do this. I do agree that the current approach isn't necessarily ideal as we don't process completions as quickly as we could, so I think there's merrit in continuing this work.
Quoted 6 lines
Quoted 4 lines
Proof of concept below, entirely untested, at a conference. Basically does what I describe above, and retains the list manipulation logic on the iopoll side, rather than on the completion side. Can you take a look at this? And if it runs, can you do some numbers on that too?
This patch works, and in my test case, the performance is identical to my patch.
Good!
Quoted 6 lines
But there is a small problem, now looking for completed requests, always need to traverse the whole
iopoll_list. this can be a bit inefficient in some cases, for example if the previous sent 128K io , the last io is 4K, the last io will be returned much earlier, this kind of scenario can not be verified in the current test. I'm not sure if it's a meaningful scenario.Quoted 7 lines
Not sure that's a big problem, you're just spinning to complete anyway. You could add your
iob->nr_reqsor something, and break after finding those know have completed. That won't necessarily change anything, could still be the last one that completed. Would probably make more sense to pass in 'min_events' or similar and stop after that. But I think mostly tweaks that can be made after the fact. If you want to send out a new patch based on the one I sent, feel free to.Quoted 5 lines
Eg, something like this on top would do that. Like I mentioned earlier, you cannot do the list manipulation where you did it, it's not safe. You have to defer it to reaping time. If we could do it from the callback where we mark it complete, then that would surely make things more trivial and avoid iteration when not needed.
Quoted 4 lines
Yes, it's not safe do the list manipulation in
io_complete_rw_iopoll. It looks more reasonable with the following modifications. Your changes look good enough, but please give me more time, I'd like to do some more testing and rethink this.
Of course, there's no rush - we're in the merge window anyway, so this will be targeted for 6.20/7.0 either way.
Signature · 1 line
Jens Axboe
Quoted 78 lines · Jens Axboe
Quoted 70 lines · Jens Axboe
Quoted 55 lines · Fengnan
? 2025/12/11 12:10, Jens Axboe ??:
Quoted 53 lines · Jens Axboe
Quoted 52 lines · Fengnan Chang
Quoted 17 lines
In the
io_do_iopollfunction, when the poll loop ofiopoll_listends, it is considered that the current req is the actual completed request. This may be reasonable for multi-queue ctx, but is problematic for single-queue ctx because the current request may not be done when the poll gets to the result. In this case, the completed io needs to wait for the first io on the chain to complete before notifying the user, which may cause io accumulation in the list.
Our modification plan is as follows: changeio_wq_work_listto normal list so that theiopoll_listlist in it can be removed and put into thecomp_reqslist when the request is completed. This way each io is handled independently and all gets processed in time.After modification, test with:
./t/io_uring -p1 -d128 -b4096 -s32 -c32 -F1 -B1 -R1 -X1 -n1 -P1
/dev/nvme6n1Quoted 6 lines
base IOPS is 725K, patch IOPS is 782K.
./t/io_uring -p1 -d128 -b4096 -s32 -c1 -F1 -B1 -R1 -X1 -n1 -P1
/dev/nvme6n1Base IOPS is 880k, patch IOPS is 895K.
A few notes on this:
1) Manipulating the list in
io_complete_rw_iopoll()I don't think is necessarily safe. Yes generally this is invoked from the owning/polling task, but that's not guaranteed.2) The patch doesn't apply to the current tree, must be an older version?
3) When hand-applied, it still throws a compile warning about an unused variable. Please don't send untested stuff...
4) Don't just blatantly bloat the
io_kiocb. When you change from a singly to a doubly linked list, you're growing theio_kiocbsize. You should be able to use a union with structio_task_workfor example. That's already 16b in size - win/win as you don't need to slow down the cache management as that can keep using the linkage it currently is using, and you're not bloating theio_kiocb.5) The already mentioned point about the cache free list now being doubly linked. This is generally a _bad_ idea as removing and adding entries now need to touch other entries too. That's not very cache friendly.
#1 is kind of the big one, as it means you'll need to re-think how you do this. I do agree that the current approach isn't necessarily ideal as we don't process completions as quickly as we could, so I think there's merrit in continuing this work.
Quoted 6 lines
Quoted 4 lines
Proof of concept below, entirely untested, at a conference. Basically does what I describe above, and retains the list manipulation logic on the iopoll side, rather than on the completion side. Can you take a look at this? And if it runs, can you do some numbers on that too?
This patch works, and in my test case, the performance is identical to my patch.
Good!
Quoted 6 lines
But there is a small problem, now looking for completed requests, always need to traverse the whole
iopoll_list. this can be a bit inefficient in some cases, for example if the previous sent 128K io , the last io is 4K, the last io will be returned much earlier, this kind of scenario can not be verified in the current test. I'm not sure if it's a meaningful scenario.Quoted 7 lines
Not sure that's a big problem, you're just spinning to complete anyway. You could add your
iob->nr_reqsor something, and break after finding those know have completed. That won't necessarily change anything, could still be the last one that completed. Would probably make more sense to pass in 'min_events' or similar and stop after that. But I think mostly tweaks that can be made after the fact. If you want to send out a new patch based on the one I sent, feel free to.
Quoted 5 lines
Eg, something like this on top would do that. Like I mentioned earlier, you cannot do the list manipulation where you did it, it's not safe. You have to defer it to reaping time. If we could do it from the callback where we mark it complete, then that would surely make things more trivial and avoid iteration when not needed.
Oh, we can't add nr_events == iob.nr_reqs check, if blk_mq_add_to_batch add failed, completed IO will not add into iob, iob.nr_reqs will be 0, this may cause io hang.
Quoted 39 lines
include/linux/blk-mq.h +1
-0@@ -917,6 +917,7 @@ static inline bool blk_mq_add_to_batch(struct request *req,else if (iob->complete != complete)return false; iob->need_ts |=blk_mq_need_time_stamp (req); + iob->nr_reqs++;rq_list_add_tail (&iob->req_list, req);return true; }include/linux/blkdev.h +1
-0@@ -1821,6 +1821,7 @@ void bdev_fput(struct file *bdev_file);struct io_comp_batch {struct rq_list req_list;bool need_ts; +int nr_reqs;void (*complete)(struct io_comp_batch *); };io_uring/rw.c +2
-0@@ -1358,6 +1358,8 @@ int io_do_iopoll(struct io_ring_ctx *ctx, bool force_nonspin) iob.complete (&iob);list_for_each_entry_safe (req, tmp, &ctx->iopoll_list, iopoll_node) { +if (nr_events == iob.nr_reqs) +break ;/* order with io_complete_rw_iopoll(), e.g. ->result updates */ if (!smp_load_acquire (&req->iopoll_completed))continue ;
Jens Axboe
maintainerQuoted 3 lines · Fengnan Chang
Oh, we can't add
nr_events== iob.nr_reqs check, ifblk_mq_add_to_batchadd failed, completed IO will not add into iob, iob.nr_reqs will be 0, this may cause io hang.
Indeed, won't work as-is.
I do think we're probably making a bigger deal out of the full loop than necessary. At least I'd be perfectly happy with just the current patch, performance should be better there than we currently have it. Ideally we'd have just one loop for polling and catching the completed items, but that's a bit tricky with the batch completions.
Signature · 1 line
Jens Axboe
Quoted 11 lines · Jens Axboe
Quoted 3 lines · Fengnan Chang
Oh, we can't add
nr_events== iob.nr_reqs check, ifblk_mq_add_to_batchadd failed, completed IO will not add into iob, iob.nr_reqs will be 0, this may cause io hang.Indeed, won't work as-is.
I do think we're probably making a bigger deal out of the full loop than necessary. At least I'd be perfectly happy with just the current patch, performance should be better there than we currently have it. Ideally we'd have just one loop for polling and catching the completed items, but that's a bit tricky with the batch completions.
Yes, ideally one loop would be enough, but given that there are also multi_queue ctx, that doesn't seem to be possible.
Quoted 1 line
Jens Axboe
maintainerQuoted 9 lines · Fengnan Chang
? 2025/12/12 09:53, Jens Axboe ??:
Quoted 6 lines · Fengnan Chang
Quoted 3 lines
Oh, we can't add
nr_events== iob.nr_reqs check, ifblk_mq_add_to_batchadd failed, completed IO will not add into iob, iob.nr_reqs will be 0, this may cause io hang.Indeed, won't work as-is.
Quoted 8 lines
Quoted 5 lines
I do think we're probably making a bigger deal out of the full loop than necessary. At least I'd be perfectly happy with just the current patch, performance should be better there than we currently have it. Ideally we'd have just one loop for polling and catching the completed items, but that's a bit tricky with the batch completions.
Yes, ideally one loop would be enough, but given that there are also
multi_queuectx, that doesn't seem to be possible.
It's not removing the double loop, but the below could help _only_ iterate completed requests at the end. Rather than move items between the current list at the completion callback, have a separate list just for completed requests. Then we can simply iterate that, knowing all of them have completed. Gets rid of the ->iopoll_completed as well, and then we can move the poll_refs. Not really related at all, obviously this patch should be split into multiple pieces.
This uses a lockless list. But since the producer and consumer are generally the same task, that should not add any real overhead. On top of the previous one I sent. What do you think?
include/linux/io_uring_types.h +6 -4
@@ -317,6 +317,7 @@ struct io_ring_ctx {*/ bool poll_multi_queue;struct list_head iopoll_list; +struct llist_head iopoll_complete;struct io_file_table file_table;struct io_rsrc_data buf_table;
@@ -672,8 +673,9 @@ struct io_kiocb { };u8 opcode; -/* polled IO has completed */ -u8 iopoll_completed; + +bool cancel_seq_set; +/* * Can be either a fixed buffer index, or used with provided buffers. * For the latter, it points to the selected buffer ID.
@@ -700,6 +702,7 @@ struct io_kiocb {union {/* used by request caches, completion batching and iopoll */ struct io_wq_work_node comp_list; +struct llist_node iopoll_done_list;/* cache ->apoll->events */ __poll_t apoll_events; };
@@ -707,7 +710,7 @@ struct io_kiocb {struct io_rsrc_node *file_node;atomic_t refs; -bool cancel_seq_set; +atomic_t poll_refs;/* * IOPOLL doesn't use task_work, so use the ->iopoll_node list
@@ -734,7 +737,6 @@ struct io_kiocb {/* opcode allocated if it needs to store data for async defer */ void *async_data;/* linked requests, IFF REQ_F_HARDLINK or REQ_F_LINK are set */ -atomic_t poll_refs;struct io_kiocb *link;/* custom credentials, valid IFF REQ_F_CREDS is set */ const struct cred *creds;
io_uring/io_uring.c +1 -0
@@ -335,6 +335,7 @@ static __cold struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p)spin_lock_init (&ctx->completion_lock);raw_spin_lock_init (&ctx->timeout_lock);INIT_LIST_HEAD (&ctx->iopoll_list); +init_llist_head (&ctx->iopoll_complete);INIT_LIST_HEAD (&ctx->defer_list);INIT_LIST_HEAD (&ctx->timeout_list);INIT_LIST_HEAD (&ctx->ltimeout_list);
io_uring/rw.c +13 -18
@@ -604,8 +604,7 @@ static void io_complete_rw_iopoll(struct kiocb *kiocb, long res) req->cqe.res = res; } -/* order with io_iopoll_complete() checking ->iopoll_completed */ -smp_store_release (&req->iopoll_completed,1 ); +llist_add (&req->iopoll_done_list, &req->ctx->iopoll_complete); }static inline void io_rw_done (struct io_kiocb *req,ssize_t ret)
@@ -870,7 +869,6 @@ static int io_rw_init_file(struct io_kiocb *req, fmode_t mode, int rw_type)return -EOPNOTSUPP ; kiocb->private =NULL ; kiocb->ki_flags |=IOCB_HIPRI ; - req->iopoll_completed =0 ;if (ctx->flags &IORING_SETUP_HYBRID_IOPOLL ) {/* make sure every req only blocks once*/ req->flags &= ~REQ_F_IOPOLL_STATE ;
@@ -1317,7 +1315,8 @@ int io_do_iopoll(struct io_ring_ctx *ctx, bool force_nonspin) {unsigned int poll_flags =0 ;DEFINE_IO_COMP_BATCH (iob); -struct io_kiocb *req, *tmp; +struct llist_node *node; +struct io_kiocb *req;int nr_events =0 ;/*
@@ -1327,17 +1326,12 @@ int io_do_iopoll(struct io_ring_ctx *ctx, bool force_nonspin)if (ctx->poll_multi_queue || force_nonspin) poll_flags |=BLK_POLL_ONESHOT ; +/* +* Loop over uncompleted polled IO requests, and poll for them. +*/ list_for_each_entry (req, &ctx->iopoll_list, iopoll_node) {int ret; -/* -* Move completed and retryable entries to our local lists. -* If we find a request that requires polling, break out -* and complete those lists first, if we have entries there. -*/ -if (READ_ONCE (req->iopoll_completed)) -break ; -if (ctx->flags &IORING_SETUP_HYBRID_IOPOLL ) ret =io_uring_hybrid_poll (req, &iob, poll_flags);else
@@ -1349,24 +1343,25 @@ int io_do_iopoll(struct io_ring_ctx *ctx, bool force_nonspin) poll_flags |=BLK_POLL_ONESHOT ;/* iopoll may have completed current req */ -if (!rq_list_empty (&iob.req_list) || -READ_ONCE (req->iopoll_completed)) +if (!rq_list_empty (&iob.req_list))break ; }if (!rq_list_empty (&iob.req_list)) iob.complete (&iob); -list_for_each_entry_safe (req, tmp, &ctx->iopoll_list, iopoll_node) { -/* order with io_complete_rw_iopoll(), e.g. ->result updates */ -if (!smp_load_acquire (&req->iopoll_completed)) -continue ; + node =llist_del_all (&ctx->iopoll_complete); +while (node) { +struct llist_node *next = node->next; + + req = container_of(node, struct io_kiocb, iopoll_done_list);list_del (&req->iopoll_node);wq_list_add_tail (&req->comp_list, &ctx->submit_state.compl_reqs); nr_events++; req->cqe.flags =io_put_kbuf (req, req->cqe.res,NULL );if (req->opcode !=IORING_OP_URING_CMD )io_req_rw_cleanup (req,0 ); + node = next; }if (nr_events)__io_submit_flush_completions (ctx);
io_uring/uring_cmd.c +1 -3
@@ -159,8 +159,7 @@ void __io_uring_cmd_done(struct io_uring_cmd *ioucmd, s32 ret, u64 res2, }io_req_uring_cleanup (req, issue_flags);if (req->ctx->flags &IORING_SETUP_IOPOLL ) { -/* order with io_iopoll_req_issued() checking ->iopoll_complete */ -smp_store_release (&req->iopoll_completed,1 ); +llist_add (&req->iopoll_done_list, &req->ctx->iopoll_complete); }else if (issue_flags &IO_URING_F_COMPLETE_DEFER ) {if (WARN_ON_ONCE (issue_flags &IO_URING_F_UNLOCKED ))return ;
@@ -252,7 +251,6 @@ int io_uring_cmd(struct io_kiocb *req, unsigned int issue_flags)if (!file->f_op->uring_cmd_iopoll)return -EOPNOTSUPP ; issue_flags |=IO_URING_F_IOPOLL ; - req->iopoll_completed =0 ;if (ctx->flags &IORING_SETUP_HYBRID_IOPOLL ) {/* make sure every req only blocks once */ req->flags &= ~REQ_F_IOPOLL_STATE ;
Signature · 1 line
Jens Axboe
Jens Axboe
maintainerQuoted 27 lines · Jens Axboe
Quoted 9 lines · Fengnan Chang
? 2025/12/12 09:53, Jens Axboe ??:
Quoted 6 lines · Fengnan Chang
Quoted 3 lines
Oh, we can't add
nr_events== iob.nr_reqs check, ifblk_mq_add_to_batchadd failed, completed IO will not add into iob, iob.nr_reqs will be 0, this may cause io hang.Indeed, won't work as-is.
Quoted 8 lines
Quoted 5 lines
I do think we're probably making a bigger deal out of the full loop than necessary. At least I'd be perfectly happy with just the current patch, performance should be better there than we currently have it. Ideally we'd have just one loop for polling and catching the completed items, but that's a bit tricky with the batch completions.
Yes, ideally one loop would be enough, but given that there are also
multi_queuectx, that doesn't seem to be possible.It's not removing the double loop, but the below could help _only_ iterate completed requests at the end. Rather than move items between the current list at the completion callback, have a separate list just for completed requests. Then we can simply iterate that, knowing all of them have completed. Gets rid of the ->iopoll_completed as well, and then we can move the
poll_refs. Not really related at all, obviously this patch should be split into multiple pieces.
Quoted 3 lines
This uses a lockless list. But since the producer and consumer are generally the same task, that should not add any real overhead. On top of the previous one I sent. What do you think?
Ran some quick testing, as one interesting case is mixing slower and faster devices. Let's take this basic example:
t/io_uring -p1 -d128 -b512 -s32 -c32 -F1 -B1 -R1 -X1 -n1 -P1 -t1 -n2 /dev/nvme32n1 /dev/nvme1n1 /dev/nvme2n1 /dev/nvme3n1
where nvme32n1 is about 1.27M IOPS, and the other 3 do about 3.3M IOPS, and we poll 2 devices with each IO thread. With the current kernel, we get:
IOPS=5.18M, BW=2.53GiB/s, IOS/call=31/32
IOPS=5.19M, BW=2.53GiB/s, IOS/call=31/31
IOPS=5.17M, BW=2.53GiB/s, IOS/call=31/31
and with the two patches we get:
IOPS=6.54M, BW=3.19GiB/s, IOS/call=31/31
IOPS=6.52M, BW=3.18GiB/s, IOS/call=31/31
IOPS=6.52M, BW=3.18GiB/s, IOS/call=31/31
or about a 25% improvement. This is mostly due to the issue you highlighted, where you end up with later completions (that are done) being stuck behind waiting on a slower completion.
Note: obviously 1 thread driving multiple devices for polling could still be improved, and in fact it does improve if we simply change -c32 to something lower. The more important case is the one you identified, where different completion times on the same device will hold completions up. Multi device polling is just an interesting way to kind of emulate that, to an extent.
This effect is (naturally) also apparent in the completion latencies as well, particularly in the higher percentiles.
Ran peak testing too, and it's better all around than before.
Signature · 1 line
Jens Axboe
Quoted 58 lines · Jens Axboe
Quoted 24 lines · Jens Axboe
Quoted 8 lines · Fengnan Chang
? 2025/12/12 09:53, Jens Axboe ??:
Quoted 6 lines · Fengnan Chang
Quoted 3 lines
Oh, we can't add
nr_events== iob.nr_reqs check, ifblk_mq_add_to_batchadd failed, completed IO will not add into iob, iob.nr_reqs will be 0, this may cause io hang.Indeed, won't work as-is.
Quoted 7 lines
Quoted 5 lines
I do think we're probably making a bigger deal out of the full loop than necessary. At least I'd be perfectly happy with just the current patch, performance should be better there than we currently have it. Ideally we'd have just one loop for polling and catching the completed items, but that's a bit tricky with the batch completions.
Yes, ideally one loop would be enough, but given that there are also
multi_queuectx, that doesn't seem to be possible.It's not removing the double loop, but the below could help _only_ iterate completed requests at the end. Rather than move items between the current list at the completion callback, have a separate list just for completed requests. Then we can simply iterate that, knowing all of them have completed. Gets rid of the ->iopoll_completed as well, and then we can move the
poll_refs. Not really related at all, obviously this patch should be split into multiple pieces.Quoted 3 lines
This uses a lockless list. But since the producer and consumer are generally the same task, that should not add any real overhead. On top of the previous one I sent. What do you think?
Ran some quick testing, as one interesting case is mixing slower and faster devices. Let's take this basic example:
t/io_uring -p1 -d128 -b512 -s32 -c32 -F1 -B1 -R1 -X1 -n1 -P1 -t1 -n2
/dev/nvme32n1/dev/nvme1n1/dev/nvme2n1/dev/nvme3n1where nvme32n1 is about 1.27M IOPS, and the other 3 do about 3.3M IOPS, and we poll 2 devices with each IO thread. With the current kernel, we get:
IOPS=5.18M, BW=2.53GiB/s, IOS/call=31/32
IOPS=5.19M, BW=2.53GiB/s, IOS/call=31/31
IOPS=5.17M, BW=2.53GiB/s, IOS/call=31/31and with the two patches we get:
IOPS=6.54M, BW=3.19GiB/s, IOS/call=31/31
IOPS=6.52M, BW=3.18GiB/s, IOS/call=31/31
IOPS=6.52M, BW=3.18GiB/s, IOS/call=31/31or about a 25% improvement. This is mostly due to the issue you highlighted, where you end up with later completions (that are done) being stuck behind waiting on a slower completion.
Note: obviously 1 thread driving multiple devices for polling could still be improved, and in fact it does improve if we simply change -c32 to something lower. The more important case is the one you identified, where different completion times on the same device will hold completions up. Multi device polling is just an interesting way to kind of emulate that, to an extent.
Quoted 4 lines
This effect is (naturally) also apparent in the completion latencies as well, particularly in the higher percentiles.
Ran peak testing too, and it's better all around than before.
I love the patch, I had a similar thought, this addresses my concern, I simple tested it and the performance is a bit better than the previous performance.
base IOPS is 725K, previous IOPS is 782K, now 790k.
It looks like all the problems are solved,I 'll do more testing next week.
Quoted 1 line
Jens Axboe
maintainerQuoted 64 lines · Fengnan Chang
? 2025/12/12 16:58, Jens Axboe ??:
Quoted 61 lines · Jens Axboe
Quoted 24 lines · Fengnan Chang
Quoted 8 lines
? 2025/12/12 09:53, Jens Axboe ??:
Quoted 6 lines · Fengnan Chang
Quoted 3 lines
Oh, we can't add
nr_events== iob.nr_reqs check, ifblk_mq_add_to_batchadd failed, completed IO will not add into iob, iob.nr_reqs will be 0, this may cause io hang.Indeed, won't work as-is.
Quoted 7 lines
Quoted 5 lines
I do think we're probably making a bigger deal out of the full loop than necessary. At least I'd be perfectly happy with just the current patch, performance should be better there than we currently have it. Ideally we'd have just one loop for polling and catching the completed items, but that's a bit tricky with the batch completions.
Yes, ideally one loop would be enough, but given that there are also
multi_queuectx, that doesn't seem to be possible.It's not removing the double loop, but the below could help _only_ iterate completed requests at the end. Rather than move items between the current list at the completion callback, have a separate list just for completed requests. Then we can simply iterate that, knowing all of them have completed. Gets rid of the ->iopoll_completed as well, and then we can move the
poll_refs. Not really related at all, obviously this patch should be split into multiple pieces.Quoted 3 lines
This uses a lockless list. But since the producer and consumer are generally the same task, that should not add any real overhead. On top of the previous one I sent. What do you think?
Ran some quick testing, as one interesting case is mixing slower and faster devices. Let's take this basic example:
t/io_uring -p1 -d128 -b512 -s32 -c32 -F1 -B1 -R1 -X1 -n1 -P1 -t1 -n2
/dev/nvme32n1/dev/nvme1n1/dev/nvme2n1/dev/nvme3n1where nvme32n1 is about 1.27M IOPS, and the other 3 do about 3.3M IOPS, and we poll 2 devices with each IO thread. With the current kernel, we get:
IOPS=5.18M, BW=2.53GiB/s, IOS/call=31/32
IOPS=5.19M, BW=2.53GiB/s, IOS/call=31/31
IOPS=5.17M, BW=2.53GiB/s, IOS/call=31/31and with the two patches we get:
IOPS=6.54M, BW=3.19GiB/s, IOS/call=31/31
IOPS=6.52M, BW=3.18GiB/s, IOS/call=31/31
IOPS=6.52M, BW=3.18GiB/s, IOS/call=31/31or about a 25% improvement. This is mostly due to the issue you highlighted, where you end up with later completions (that are done) being stuck behind waiting on a slower completion.
Note: obviously 1 thread driving multiple devices for polling could still be improved, and in fact it does improve if we simply change -c32 to something lower. The more important case is the one you identified, where different completion times on the same device will hold completions up. Multi device polling is just an interesting way to kind of emulate that, to an extent.
This effect is (naturally) also apparent in the completion latencies as well, particularly in the higher percentiles.
Quoted 6 lines
Quoted 1 line
Ran peak testing too, and it's better all around than before.
I love the patch, I had a similar thought, this addresses my concern, I simple tested it and the performance is a bit better than the previous performance.
base IOPS is 725K, previous IOPS is 782K, now 790k.
It looks like all the problems are solved,I 'll do more testing next week.
Nice, thanks! FWIW, I put the patches in a branch here, just to have them somewhat organized and easier to iterate/test on:
https://git.kernel.org/pub/scm/linux/kernel/git/axboe/linux.git/log/?h=io_uring-iopoll
Signature · 1 line
Jens Axboe
Quoted 131 lines · Jens Axboe
Quoted 9 lines · Fengnan Chang
? 2025/12/12 09:53, Jens Axboe ??:
Quoted 6 lines · Fengnan Chang
Quoted 3 lines
Oh, we can't add
nr_events== iob.nr_reqs check, ifblk_mq_add_to_batchadd failed, completed IO will not add into iob, iob.nr_reqs will be 0, this may cause io hang.Indeed, won't work as-is.
Quoted 8 lines
Quoted 5 lines
I do think we're probably making a bigger deal out of the full loop than necessary. At least I'd be perfectly happy with just the current patch, performance should be better there than we currently have it. Ideally we'd have just one loop for polling and catching the completed items, but that's a bit tricky with the batch completions.
Yes, ideally one loop would be enough, but given that there are also
multi_queuectx, that doesn't seem to be possible.It's not removing the double loop, but the below could help _only_ iterate completed requests at the end. Rather than move items between the current list at the completion callback, have a separate list just for completed requests. Then we can simply iterate that, knowing all of them have completed. Gets rid of the ->iopoll_completed as well, and then we can move the
poll_refs. Not really related at all, obviously this patch should be split into multiple pieces.This uses a lockless list. But since the producer and consumer are generally the same task, that should not add any real overhead. On top of the previous one I sent. What do you think?
include/linux/io_uring_types.h +6
-4@@ -317,6 +317,7 @@ struct io_ring_ctx {*/ bool poll_multi_queue;struct list_head iopoll_list; +struct llist_head iopoll_complete;struct io_file_table file_table;struct io_rsrc_data buf_table;@@ -672,8 +673,9 @@ struct io_kiocb { };u8 opcode; -/* polled IO has completed */ -u8 iopoll_completed; + +bool cancel_seq_set; +/* * Can be either a fixed buffer index, or used with provided buffers. * For the latter, it points to the selected buffer ID. @@ -700,6 +702,7 @@ struct io_kiocb {union {/* used by request caches, completion batching and iopoll */ struct io_wq_work_node comp_list; +struct llist_node iopoll_done_list;/* cache ->apoll->events */ __poll_t apoll_events; };@@ -707,7 +710,7 @@ struct io_kiocb {struct io_rsrc_node *file_node;atomic_t refs; -bool cancel_seq_set; +atomic_t poll_refs;/* * IOPOLL doesn't use task_work, so use the ->iopoll_node list @@ -734,7 +737,6 @@ struct io_kiocb {/* opcode allocated if it needs to store data for async defer */ void *async_data;/* linked requests, IFF REQ_F_HARDLINK or REQ_F_LINK are set */ -atomic_t poll_refs;struct io_kiocb *link;/* custom credentials, valid IFF REQ_F_CREDS is set */ const struct cred *creds;io_uring/io_uring.c +1
-0@@ -335,6 +335,7 @@ static __cold struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p)spin_lock_init (&ctx->completion_lock);raw_spin_lock_init (&ctx->timeout_lock);INIT_LIST_HEAD (&ctx->iopoll_list); +init_llist_head (&ctx->iopoll_complete);INIT_LIST_HEAD (&ctx->defer_list);INIT_LIST_HEAD (&ctx->timeout_list);INIT_LIST_HEAD (&ctx->ltimeout_list);io_uring/rw.c +6
-4@@ -604,8 +604,7 @@ static void io_complete_rw_iopoll(struct kiocb *kiocb, long res) req->cqe.res = res; } -/* order with io_iopoll_complete() checking ->iopoll_completed */ -smp_store_release (&req->iopoll_completed,1 ); +llist_add (&req->iopoll_done_list, &req->ctx->iopoll_complete); }static inline void io_rw_done (struct io_kiocb *req,ssize_t ret)@@ -870,7 +869,6 @@ static int io_rw_init_file(struct io_kiocb *req, fmode_t mode, int rw_type)return -EOPNOTSUPP ; kiocb->private =NULL ; kiocb->ki_flags |=IOCB_HIPRI ; - req->iopoll_completed =0 ;if (ctx->flags &IORING_SETUP_HYBRID_IOPOLL ) {/* make sure every req only blocks once*/ req->flags &= ~REQ_F_IOPOLL_STATE ;@@ -1317,7 +1315,8 @@ int io_do_iopoll(struct io_ring_ctx *ctx, bool force_nonspin) {unsigned int poll_flags =0 ;DEFINE_IO_COMP_BATCH (iob); -struct io_kiocb *req, *tmp; +struct llist_node *node; +struct io_kiocb *req;int nr_events =0 ;/* @@ -1327,17 +1326,12 @@ int io_do_iopoll(struct io_ring_ctx *ctx, bool force_nonspin)if (ctx->poll_multi_queue || force_nonspin) poll_flags |=BLK_POLL_ONESHOT ; +/* +* Loop over uncompleted polled IO requests, and poll for them. +*/
io_uring/rw.c int io_do_iopoll(struct io_ring_ctx *ctx, bool force_nonspin) · line 1339
Quoted 10 lines
io_uring/rw.c +0
-7list_for_each_entry (req, &ctx->iopoll_list, iopoll_node) {int ret; -/* -* Move completed and retryable entries to our local lists. -* If we find a request that requires polling, break out -* and complete those lists first, if we have entries there. -*/ -if (READ_ONCE (req->iopoll_completed)) -break ;
Suggest keeping iopoll_completed here to avoid unnecessary subsequent polling and to process IRQ-completed requests promptly.
Quoted 58 lines
io_uring/rw.c +7
-7-if (ctx->flags &IORING_SETUP_HYBRID_IOPOLL ) ret =io_uring_hybrid_poll (req, &iob, poll_flags);else @@ -1349,24 +1343,25 @@ int io_do_iopoll(struct io_ring_ctx *ctx, bool force_nonspin) poll_flags |=BLK_POLL_ONESHOT ;/* iopoll may have completed current req */ -if (!rq_list_empty (&iob.req_list) || -READ_ONCE (req->iopoll_completed)) +if (!rq_list_empty (&iob.req_list))break ; }if (!rq_list_empty (&iob.req_list)) iob.complete (&iob); -list_for_each_entry_safe (req, tmp, &ctx->iopoll_list, iopoll_node) { -/* order with io_complete_rw_iopoll(), e.g. ->result updates */ -if (!smp_load_acquire (&req->iopoll_completed)) -continue ; + node =llist_del_all (&ctx->iopoll_complete); +while (node) { +struct llist_node *next = node->next; + + req = container_of(node, struct io_kiocb, iopoll_done_list);list_del (&req->iopoll_node);wq_list_add_tail (&req->comp_list, &ctx->submit_state.compl_reqs); nr_events++; req->cqe.flags =io_put_kbuf (req, req->cqe.res,NULL );if (req->opcode !=IORING_OP_URING_CMD )io_req_rw_cleanup (req,0 ); + node = next; }if (nr_events)__io_submit_flush_completions (ctx);io_uring/uring_cmd.c +1
-3@@ -159,8 +159,7 @@ void __io_uring_cmd_done(struct io_uring_cmd *ioucmd, s32 ret, u64 res2, }io_req_uring_cleanup (req, issue_flags);if (req->ctx->flags &IORING_SETUP_IOPOLL ) { -/* order with io_iopoll_req_issued() checking ->iopoll_complete */ -smp_store_release (&req->iopoll_completed,1 ); +llist_add (&req->iopoll_done_list, &req->ctx->iopoll_complete); }else if (issue_flags &IO_URING_F_COMPLETE_DEFER ) {if (WARN_ON_ONCE (issue_flags &IO_URING_F_UNLOCKED ))return ;@@ -252,7 +251,6 @@ int io_uring_cmd(struct io_kiocb *req, unsigned int issue_flags)if (!file->f_op->uring_cmd_iopoll)return -EOPNOTSUPP ; issue_flags |=IO_URING_F_IOPOLL ; - req->iopoll_completed =0 ;if (ctx->flags &IORING_SETUP_HYBRID_IOPOLL ) {/* make sure every req only blocks once */ req->flags &= ~REQ_F_IOPOLL_STATE ;
Jens Axboe
maintainerQuoted 10 lines · Diangang Li
Quoted 10 lines
io_uring/rw.c +3
-0@@ -1327,17 +1326,12 @@ int io_do_iopoll(struct io_ring_ctx *ctx, bool force_nonspin)if (ctx->poll_multi_queue || force_nonspin) poll_flags |=BLK_POLL_ONESHOT ; +/* +* Loop over uncompleted polled IO requests, and poll for them. +*/ list_for_each_entry (req, &ctx->iopoll_list, iopoll_node) {int ret;
Quoted 10 lines
io_uring/rw.c int io_do_iopoll(struct io_ring_ctx *ctx, bool force_nonspin) · line 1339
Quoted 7 lines
io_uring/rw.c +0
-7-/* -* Move completed and retryable entries to our local lists. -* If we find a request that requires polling, break out -* and complete those lists first, if we have entries there. -*/ -if (READ_ONCE (req->iopoll_completed)) -break ;Suggest keeping
iopoll_completedhere to avoid unnecessary subsequent polling and to process IRQ-completed requests promptly.
There should not be any IRQ completed requests in here. The block layer used to allow that, but that should no longer be the case. If it's a polled request, then it will by definition end up in a polled queue and need iopoll completion. Or it'll sit for a while and be completed by a timeout. If someone is still allowing polled IO with IRQ completions then that should be fixed, and there's no reason why we should try and catch those cases here. Will not happen with NVMe, for example.
For the other point, there's no way for ->iopoll_completed to be set before iob.complete(&iob) has been called to invoke the callback, which in turn sets it. Hence there's no way for us to end up in this loop and have ->iopoll_completed set.
So no, I don't think we need it, and honestly it wasn't even really needed the posted changes either.
Signature · 1 line
Jens Axboe
Quoted 22 lines · Jens Axboe
Quoted 10 lines · Diangang Li
Quoted 10 lines
io_uring/rw.c +3
-0@@ -1327,17 +1326,12 @@ int io_do_iopoll(struct io_ring_ctx *ctx, bool force_nonspin)if (ctx->poll_multi_queue || force_nonspin) poll_flags |=BLK_POLL_ONESHOT ; +/* +* Loop over uncompleted polled IO requests, and poll for them. +*/ list_for_each_entry (req, &ctx->iopoll_list, iopoll_node) {int ret;Quoted 10 lines
io_uring/rw.c int io_do_iopoll(struct io_ring_ctx *ctx, bool force_nonspin) · line 1339
Quoted 7 lines
io_uring/rw.c +0
-7-/* -* Move completed and retryable entries to our local lists. -* If we find a request that requires polling, break out -* and complete those lists first, if we have entries there. -*/ -if (READ_ONCE (req->iopoll_completed)) -break ;Suggest keeping
iopoll_completedhere to avoid unnecessary subsequent polling and to process IRQ-completed requests promptly.
Quoted 8 lines
There should not be any IRQ completed requests in here. The block layer used to allow that, but that should no longer be the case. If it's a polled request, then it will by definition end up in a polled queue and need iopoll completion. Or it'll sit for a while and be completed by a timeout. If someone is still allowing polled IO with IRQ completions then that should be fixed, and there's no reason why we should try and catch those cases here. Will not happen with NVMe, for example.
Hi Jens,
Before commit 958148a6ac06 ("block: check BLK_FEAT_POLL under q_usage_count"), if IORING_SETUP_IOPOLL was set, IO would still proceed even with zero poll queue. It would be redirected to default queues and still linked to ctx->iopoll_list. After that commit, these cases return BLK_STS_NOTSUPP.
While tests show the split path still exits: a split clears REQ_POLLED and sends the child to the default queue, yet the IO remains on iopoll_list. Is this the situation you’re referring to previously in our original patch with doubly-linked lists, that it's not necessarily safe to manipulating the list in io_complete_rw_iopoll?
Quoted 5 lines
For the other point, there's no way for ->iopoll_completed to be set before
iob.complete(&iob)has been called to invoke the callback, which in turn sets it. Hence there's no way for us to end up in this loop and have ->iopoll_completed set.
Also, blk_mq_add_to_batch may return false for various reasons, so it's possible that the IO may complete earlier before we end up poll loop.
Quoted 3 lines
So no, I don't think we need it, and honestly it wasn't even really needed the posted changes either.
Best,
Diangang
Quoted 158 lines · Jens Axboe
Quoted 9 lines · Fengnan Chang
? 2025/12/12 09:53, Jens Axboe ??:
Quoted 6 lines · Fengnan Chang
Quoted 3 lines
Oh, we can't add
nr_events== iob.nr_reqs check, ifblk_mq_add_to_batchadd failed, completed IO will not add into iob, iob.nr_reqs will be 0, this may cause io hang.Indeed, won't work as-is.
Quoted 8 lines
Quoted 5 lines
I do think we're probably making a bigger deal out of the full loop than necessary. At least I'd be perfectly happy with just the current patch, performance should be better there than we currently have it. Ideally we'd have just one loop for polling and catching the completed items, but that's a bit tricky with the batch completions.
Yes, ideally one loop would be enough, but given that there are also
multi_queuectx, that doesn't seem to be possible.It's not removing the double loop, but the below could help _only_ iterate completed requests at the end. Rather than move items between the current list at the completion callback, have a separate list just for completed requests. Then we can simply iterate that, knowing all of them have completed. Gets rid of the ->iopoll_completed as well, and then we can move the
poll_refs. Not really related at all, obviously this patch should be split into multiple pieces.This uses a lockless list. But since the producer and consumer are generally the same task, that should not add any real overhead. On top of the previous one I sent. What do you think?
include/linux/io_uring_types.h +6
-4@@ -317,6 +317,7 @@ struct io_ring_ctx {*/ bool poll_multi_queue;struct list_head iopoll_list; +struct llist_head iopoll_complete;struct io_file_table file_table;struct io_rsrc_data buf_table;@@ -672,8 +673,9 @@ struct io_kiocb { };u8 opcode; -/* polled IO has completed */ -u8 iopoll_completed; + +bool cancel_seq_set; +/* * Can be either a fixed buffer index, or used with provided buffers. * For the latter, it points to the selected buffer ID. @@ -700,6 +702,7 @@ struct io_kiocb {union {/* used by request caches, completion batching and iopoll */ struct io_wq_work_node comp_list; +struct llist_node iopoll_done_list;/* cache ->apoll->events */ __poll_t apoll_events; };@@ -707,7 +710,7 @@ struct io_kiocb {struct io_rsrc_node *file_node;atomic_t refs; -bool cancel_seq_set; +atomic_t poll_refs;/* * IOPOLL doesn't use task_work, so use the ->iopoll_node list @@ -734,7 +737,6 @@ struct io_kiocb {/* opcode allocated if it needs to store data for async defer */ void *async_data;/* linked requests, IFF REQ_F_HARDLINK or REQ_F_LINK are set */ -atomic_t poll_refs;struct io_kiocb *link;/* custom credentials, valid IFF REQ_F_CREDS is set */ const struct cred *creds;io_uring/io_uring.c +1
-0@@ -335,6 +335,7 @@ static __cold struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p)spin_lock_init (&ctx->completion_lock);raw_spin_lock_init (&ctx->timeout_lock);INIT_LIST_HEAD (&ctx->iopoll_list); +init_llist_head (&ctx->iopoll_complete);INIT_LIST_HEAD (&ctx->defer_list);INIT_LIST_HEAD (&ctx->timeout_list);INIT_LIST_HEAD (&ctx->ltimeout_list);io_uring/rw.c +7
-14@@ -604,8 +604,7 @@ static void io_complete_rw_iopoll(struct kiocb *kiocb, long res) req->cqe.res = res; } -/* order with io_iopoll_complete() checking ->iopoll_completed */ -smp_store_release (&req->iopoll_completed,1 ); +llist_add (&req->iopoll_done_list, &req->ctx->iopoll_complete); }static inline void io_rw_done (struct io_kiocb *req,ssize_t ret)@@ -870,7 +869,6 @@ static int io_rw_init_file(struct io_kiocb *req, fmode_t mode, int rw_type)return -EOPNOTSUPP ; kiocb->private =NULL ; kiocb->ki_flags |=IOCB_HIPRI ; - req->iopoll_completed =0 ;if (ctx->flags &IORING_SETUP_HYBRID_IOPOLL ) {/* make sure every req only blocks once*/ req->flags &= ~REQ_F_IOPOLL_STATE ;@@ -1317,7 +1315,8 @@ int io_do_iopoll(struct io_ring_ctx *ctx, bool force_nonspin) {unsigned int poll_flags =0 ;DEFINE_IO_COMP_BATCH (iob); -struct io_kiocb *req, *tmp; +struct llist_node *node; +struct io_kiocb *req;int nr_events =0 ;/* @@ -1327,17 +1326,12 @@ int io_do_iopoll(struct io_ring_ctx *ctx, bool force_nonspin)if (ctx->poll_multi_queue || force_nonspin) poll_flags |=BLK_POLL_ONESHOT ; +/* +* Loop over uncompleted polled IO requests, and poll for them. +*/ list_for_each_entry (req, &ctx->iopoll_list, iopoll_node) {int ret; -/* -* Move completed and retryable entries to our local lists. -* If we find a request that requires polling, break out -* and complete those lists first, if we have entries there. -*/ -if (READ_ONCE (req->iopoll_completed)) -break ; -if (ctx->flags &IORING_SETUP_HYBRID_IOPOLL ) ret =io_uring_hybrid_poll (req, &iob, poll_flags);else @@ -1349,24 +1343,25 @@ int io_do_iopoll(struct io_ring_ctx *ctx, bool force_nonspin) poll_flags |=BLK_POLL_ONESHOT ;/* iopoll may have completed current req */ -if (!rq_list_empty (&iob.req_list) || -READ_ONCE (req->iopoll_completed)) +if (!rq_list_empty (&iob.req_list))break ; }if (!rq_list_empty (&iob.req_list)) iob.complete (&iob);
io_uring/rw.c int io_do_iopoll(struct io_ring_ctx *ctx, bool force_nonspin) · line 1358
Quoted 10 lines
io_uring/rw.c +5
-4-list_for_each_entry_safe (req, tmp, &ctx->iopoll_list, iopoll_node) { -/* order with io_complete_rw_iopoll(), e.g. ->result updates */ -if (!smp_load_acquire (&req->iopoll_completed)) -continue ; + node =llist_del_all (&ctx->iopoll_complete); +while (node) { +struct llist_node *next = node->next; + + req = container_of(node, struct io_kiocb, iopoll_done_list);list_del (&req->iopoll_node);
Hi Jens,
We’ve identified one critical panic issue here.
[ 4504.422964] [ T63683] list_del corruption, ff2adc9b51d19a90->next is LIST_POISON1 (dead000000000100) [ 4504.422994] [ T63683] ------------[ cut here ]------------ [ 4504.422995] [ T63683] kernel BUG at lib/list_debug.c:56! [ 4504.423006] [ T63683] Oops: invalid opcode: 0000 [#1] SMP NOPTI [ 4504.423017] [ T63683] CPU: 38 UID: 0 PID: 63683 Comm: io_uring Kdump: loaded Tainted: G S E 6.19.0-rc1+ #1 PREEMPT(voluntary) [ 4504.423032] [ T63683] Tainted: [S]=CPU_OUT_OF_SPEC, [E]=UNSIGNED_MODULE [ 4504.423040] [ T63683] Hardware name: Inventec S520-A6/Nanping MLB, BIOS 01.01.01.06.03 03/03/2023 [ 4504.423050] [ T63683] RIP: 0010:__list_del_entry_valid_or_report+0x94/0x100 [ 4504.423064] [ T63683] Code: 89 fe 48 c7 c7 f0 78 87 b5 e8 38 07 ae ff 0f 0b 48 89 ef e8 6e 40 cd ff 48 89 ea 48 89 de 48 c7 c7 20 79 87 b5 e8 1c 07 ae ff <0f> 0b 4c 89 e7 e8 52 40 cd ff 4c 89 e2 48 89 de 48 c7 c7 58 79 87 [ 4504.423085] [ T63683] RSP: 0018:ff4efd9f3838fdb0 EFLAGS: 00010246 [ 4504.423093] [ T63683] RAX: 000000000000004e RBX: ff2adc9b51d19a90 RCX: 0000000000000027 [ 4504.423103] [ T63683] RDX: 0000000000000000 RSI: 0000000000000001 RDI: ff2add151cf99580 [ 4504.423112] [ T63683] RBP: dead000000000100 R08: 0000000000000000 R09: 0000000000000003 [ 4504.423120] [ T63683] R10: ff4efd9f3838fc60 R11: ff2add151cdfffe8 R12: dead000000000122 [ 4504.423130] [ T63683] R13: ff2adc9b51d19a00 R14: 0000000000000000 R15: 0000000000000000 [ 4504.423139] [ T63683] FS: 00007fae4f7ff6c0(0000) GS:ff2add15665f5000(0000) knlGS:0000000000000000 [ 4504.423148] [ T63683] CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033 [ 4504.423157] [ T63683] CR2: 000055aa8afe5000 CR3: 00000083037ee006 CR4: 0000000000773ef0 [ 4504.423166] [ T63683] PKRU: 55555554 [ 4504.423171] [ T63683] Call Trace: [ 4504.423178] [ T63683] <TASK> [ 4504.423184] [ T63683] io_do_iopoll+0x298/0x330 [ 4504.423193] [ T63683] ? asm_sysvec_apic_timer_interrupt+0x1a/0x20 [ 4504.423204] [ T63683] __do_sys_io_uring_enter+0x421/0x770 [ 4504.423214] [ T63683] do_syscall_64+0x67/0xf00 [ 4504.423223] [ T63683] entry_SYSCALL_64_after_hwframe+0x76/0x7e [ 4504.423232] [ T63683] RIP: 0033:0x55aa707e99c3
It can be reproduced in three ways:
- Running iopoll tests while switching the block scheduler
- A split IO scenario in iopoll (e.g., bs=512k with max_sectors_kb=256k)
- Multi poll queues with multi threads
All cases appear related to IO completions occurring outside the io_do_iopoll() loop. The root cause remains unclear.
Best,
Diangang
Quoted 32 lines
io_uring/rw.c +1
-0wq_list_add_tail (&req->comp_list, &ctx->submit_state.compl_reqs); nr_events++; req->cqe.flags =io_put_kbuf (req, req->cqe.res,NULL );if (req->opcode !=IORING_OP_URING_CMD )io_req_rw_cleanup (req,0 ); + node = next; }if (nr_events)__io_submit_flush_completions (ctx);io_uring/uring_cmd.c +1
-3@@ -159,8 +159,7 @@ void __io_uring_cmd_done(struct io_uring_cmd *ioucmd, s32 ret, u64 res2, }io_req_uring_cleanup (req, issue_flags);if (req->ctx->flags &IORING_SETUP_IOPOLL ) { -/* order with io_iopoll_req_issued() checking ->iopoll_complete */ -smp_store_release (&req->iopoll_completed,1 ); +llist_add (&req->iopoll_done_list, &req->ctx->iopoll_complete); }else if (issue_flags &IO_URING_F_COMPLETE_DEFER ) {if (WARN_ON_ONCE (issue_flags &IO_URING_F_UNLOCKED ))return ;@@ -252,7 +251,6 @@ int io_uring_cmd(struct io_kiocb *req, unsigned int issue_flags)if (!file->f_op->uring_cmd_iopoll)return -EOPNOTSUPP ; issue_flags |=IO_URING_F_IOPOLL ; - req->iopoll_completed =0 ;if (ctx->flags &IORING_SETUP_HYBRID_IOPOLL ) {/* make sure every req only blocks once */ req->flags &= ~REQ_F_IOPOLL_STATE ;
Jens Axboe
maintainerQuoted 47 lines · Diangang Li
Hi Jens,
We?ve identified one critical panic issue here.
[ 4504.422964] [ T63683] list_del corruption, ff2adc9b51d19a90->next is LIST_POISON1 (dead000000000100) [ 4504.422994] [ T63683] ------------[ cut here ]------------ [ 4504.422995] [ T63683] kernel BUG at lib/list_debug.c:56! [ 4504.423006] [ T63683] Oops: invalid opcode: 0000 [#1] SMP NOPTI [ 4504.423017] [ T63683] CPU: 38 UID: 0 PID: 63683 Comm: io_uring Kdump: loaded Tainted: G S E 6.19.0-rc1+ #1 PREEMPT(voluntary) [ 4504.423032] [ T63683] Tainted: [S]=CPU_OUT_OF_SPEC, [E]=UNSIGNED_MODULE [ 4504.423040] [ T63683] Hardware name: Inventec S520-A6/Nanping MLB, BIOS 01.01.01.06.03 03/03/2023 [ 4504.423050] [ T63683] RIP: 0010:__list_del_entry_valid_or_report+0x94/0x100 [ 4504.423064] [ T63683] Code: 89 fe 48 c7 c7 f0 78 87 b5 e8 38 07 ae ff 0f 0b 48 89 ef e8 6e 40 cd ff 48 89 ea 48 89 de 48 c7 c7 20 79 87 b5 e8 1c 07 ae ff <0f> 0b 4c 89 e7 e8 52 40 cd ff 4c 89 e2 48 89 de 48 c7 c7 58 79 87 [ 4504.423085] [ T63683] RSP: 0018:ff4efd9f3838fdb0 EFLAGS: 00010246 [ 4504.423093] [ T63683] RAX: 000000000000004e RBX: ff2adc9b51d19a90 RCX: 0000000000000027 [ 4504.423103] [ T63683] RDX: 0000000000000000 RSI: 0000000000000001 RDI: ff2add151cf99580 [ 4504.423112] [ T63683] RBP: dead000000000100 R08: 0000000000000000 R09: 0000000000000003 [ 4504.423120] [ T63683] R10: ff4efd9f3838fc60 R11: ff2add151cdfffe8 R12: dead000000000122 [ 4504.423130] [ T63683] R13: ff2adc9b51d19a00 R14: 0000000000000000 R15: 0000000000000000 [ 4504.423139] [ T63683] FS: 00007fae4f7ff6c0(0000) GS:ff2add15665f5000(0000) knlGS:0000000000000000 [ 4504.423148] [ T63683] CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033 [ 4504.423157] [ T63683] CR2: 000055aa8afe5000 CR3: 00000083037ee006 CR4: 0000000000773ef0 [ 4504.423166] [ T63683] PKRU: 55555554 [ 4504.423171] [ T63683] Call Trace: [ 4504.423178] [ T63683] <TASK> [ 4504.423184] [ T63683] io_do_iopoll+0x298/0x330 [ 4504.423193] [ T63683] ? asm_sysvec_apic_timer_interrupt+0x1a/0x20 [ 4504.423204] [ T63683] __do_sys_io_uring_enter+0x421/0x770 [ 4504.423214] [ T63683] do_syscall_64+0x67/0xf00 [ 4504.423223] [ T63683] entry_SYSCALL_64_after_hwframe+0x76/0x7e [ 4504.423232] [ T63683] RIP: 0033:0x55aa707e99c3
Quoted 7 lines
It can be reproduced in three ways:
- Running iopoll tests while switching the block scheduler
- A split IO scenario in iopoll (e.g., bs=512k with max_sectors_kb=256k)
- Multi poll queues with multi threadsAll cases appear related to IO completions occurring outside the
io_do_iopoll()loop. The root cause remains unclear.
Ah I see what it is - we can get multiple completions on the iopoll side, if you have multiple bio's per request. This didn't matter before the patch that uses a lockless list to collect them, as it just marked the request completed by writing to ->iopoll_complete and letting the reaper find them. But it matters with the llist change, as then we're adding the request to the llist more than once.
Signature · 1 line
Jens Axboe
Quoted 56 lines · Jens Axboe
Quoted 47 lines · Diangang Li
Hi Jens,
We?ve identified one critical panic issue here.
[ 4504.422964] [ T63683] list_del corruption, ff2adc9b51d19a90->next is LIST_POISON1 (dead000000000100) [ 4504.422994] [ T63683] ------------[ cut here ]------------ [ 4504.422995] [ T63683] kernel BUG at lib/list_debug.c:56! [ 4504.423006] [ T63683] Oops: invalid opcode: 0000 [#1] SMP NOPTI [ 4504.423017] [ T63683] CPU: 38 UID: 0 PID: 63683 Comm: io_uring Kdump: loaded Tainted: G S E 6.19.0-rc1+ #1 PREEMPT(voluntary) [ 4504.423032] [ T63683] Tainted: [S]=CPU_OUT_OF_SPEC, [E]=UNSIGNED_MODULE [ 4504.423040] [ T63683] Hardware name: Inventec S520-A6/Nanping MLB, BIOS 01.01.01.06.03 03/03/2023 [ 4504.423050] [ T63683] RIP: 0010:__list_del_entry_valid_or_report+0x94/0x100 [ 4504.423064] [ T63683] Code: 89 fe 48 c7 c7 f0 78 87 b5 e8 38 07 ae ff 0f 0b 48 89 ef e8 6e 40 cd ff 48 89 ea 48 89 de 48 c7 c7 20 79 87 b5 e8 1c 07 ae ff <0f> 0b 4c 89 e7 e8 52 40 cd ff 4c 89 e2 48 89 de 48 c7 c7 58 79 87 [ 4504.423085] [ T63683] RSP: 0018:ff4efd9f3838fdb0 EFLAGS: 00010246 [ 4504.423093] [ T63683] RAX: 000000000000004e RBX: ff2adc9b51d19a90 RCX: 0000000000000027 [ 4504.423103] [ T63683] RDX: 0000000000000000 RSI: 0000000000000001 RDI: ff2add151cf99580 [ 4504.423112] [ T63683] RBP: dead000000000100 R08: 0000000000000000 R09: 0000000000000003 [ 4504.423120] [ T63683] R10: ff4efd9f3838fc60 R11: ff2add151cdfffe8 R12: dead000000000122 [ 4504.423130] [ T63683] R13: ff2adc9b51d19a00 R14: 0000000000000000 R15: 0000000000000000 [ 4504.423139] [ T63683] FS: 00007fae4f7ff6c0(0000) GS:ff2add15665f5000(0000) knlGS:0000000000000000 [ 4504.423148] [ T63683] CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033 [ 4504.423157] [ T63683] CR2: 000055aa8afe5000 CR3: 00000083037ee006 CR4: 0000000000773ef0 [ 4504.423166] [ T63683] PKRU: 55555554 [ 4504.423171] [ T63683] Call Trace: [ 4504.423178] [ T63683] <TASK> [ 4504.423184] [ T63683] io_do_iopoll+0x298/0x330 [ 4504.423193] [ T63683] ? asm_sysvec_apic_timer_interrupt+0x1a/0x20 [ 4504.423204] [ T63683] __do_sys_io_uring_enter+0x421/0x770 [ 4504.423214] [ T63683] do_syscall_64+0x67/0xf00 [ 4504.423223] [ T63683] entry_SYSCALL_64_after_hwframe+0x76/0x7e [ 4504.423232] [ T63683] RIP: 0033:0x55aa707e99c3Quoted 7 lines
It can be reproduced in three ways:
- Running iopoll tests while switching the block scheduler
- A split IO scenario in iopoll (e.g., bs=512k with max_sectors_kb=256k)
- Multi poll queues with multi threadsAll cases appear related to IO completions occurring outside the
io_do_iopoll()loop. The root cause remains unclear.
Quoted 8 lines
Ah I see what it is - we can get multiple completions on the iopoll side, if you have multiple bio's per request. This didn't matter before the patch that uses a lockless list to collect them, as it just marked the request completed by writing to ->iopoll_complete and letting the reaper find them. But it matters with the llist change, as then we're adding the request to the llist more than once.
From e2f749299e3c76ef92d3edfd9f8f7fc9a029129a Mon Sep 17 00:00:00 2001
From: Diangang Li <[email protected]>
Date: Fri, 19 Dec 2025 10:14:33 +0800
Subject: [PATCH] io_uring: fix race between adding to ctx->iopoll_list and ctx->iopoll_complete
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Since commit 316693eb8aed ("io_uring: be smarter about handling IOPOLL completions") introduced ctx->iopoll_complete to cache polled completions, a request can be enqueued to ctx->iopoll_complete as part of a batched poll while it is still in the issuing path.
If the IO was submitted via io_wq_submit_work(), it may still be stuck in io_iopoll_req_issued() waiting for ctx->uring_lock, which is held by io_do_iopoll(). In this state, io_do_iopoll() may attempt to delete the request from ctx->iopoll_list before it has ever been linked, leading to a list_del() corruption.
Fix this by introducing an iopoll_state flag to mark whether the request has been inserted into ctx->iopoll_list. When io_do_iopoll() tries to unlink a request and the flag indicates it hasn’t been linked yet, skip the list_del() and just requeue the completion to ctx->iopoll_complete for later reap.
--- include/linux/io_uring_types.h | 1 + io_uring/io_uring.c | 1 + io_uring/rw.c | 7 +++++++ io_uring/uring_cmd.c | 1 + 4 files changed, 10 insertions(+)
include/linux/io_uring_types.h +1 -0
@@ -677,6 +677,7 @@ struct io_kiocb { };u8 opcode; +u8 iopoll_state;bool cancel_seq_set;
io_uring/io_uring.c +0 -0
@@ -1692,6 +1692,7 @@ static void io_iopoll_req_issued(struct io_kiocb
*req, unsigned int issue_flags)
}
list_add_tail(&req->iopoll_node, &ctx->iopoll_list);
+ smp_store_release(&req->iopoll_state, 1);
if (unlikely(needs_lock)) {
/*
io_uring/rw.c +0 -0
@@ -869,6 +869,7 @@ static int io_rw_init_file(struct io_kiocb *req,
fmode_t mode, int rw_type)
return -EOPNOTSUPP;
kiocb->private = NULL;
kiocb->ki_flags |= IOCB_HIPRI;
+ req->iopoll_state = 0;
if (ctx->flags & IORING_SETUP_HYBRID_IOPOLL) {
/* make sure every req only blocks once*/ req->flags &= ~REQ_F_IOPOLL_STATE;
@@ -1355,6 +1356,12 @@ int io_do_iopoll(struct io_ring_ctx *ctx, bool force_nonspin)
struct llist_node *next = node->next;
req = container_of(node, struct io_kiocb, iopoll_done_list);
+ if (!READ_ONCE(req->iopoll_state)) {
+ node->next = NULL;
+ llist_add(&req->iopoll_done_list, &ctx->iopoll_complete);
+ node = next;
+ continue;
+ }
list_del(&req->iopoll_node); wq_list_add_tail(&req->comp_list, &ctx->submit_state.compl_reqs); nr_events++;
io_uring/uring_cmd.c +0 -0
@@ -251,6 +251,7 @@ int io_uring_cmd(struct io_kiocb *req, unsigned int
issue_flags)
if (!file->f_op->uring_cmd_iopoll)
return -EOPNOTSUPP;
issue_flags |= IO_URING_F_IOPOLL;
+ req->iopoll_state = 0;
if (ctx->flags & IORING_SETUP_HYBRID_IOPOLL) {
/* make sure every req only blocks once */ req->flags &= ~REQ_F_IOPOLL_STATE;
Signature · 1 line
2.20.1
Quoted 170 lines · Diangang Li
Quoted 58 lines · Jens Axboe
Quoted 49 lines · Diangang Li
Hi Jens,
We?ve identified one critical panic issue here.
[ 4504.422964] [ T63683] list_del corruption, ff2adc9b51d19a90->next is LIST_POISON1 (dead000000000100) [ 4504.422994] [ T63683] ------------[ cut here ]------------ [ 4504.422995] [ T63683] kernel BUG at lib/list_debug.c:56! [ 4504.423006] [ T63683] Oops: invalid opcode: 0000 [#1] SMP NOPTI [ 4504.423017] [ T63683] CPU: 38 UID: 0 PID: 63683 Comm: io_uring Kdump: loaded Tainted: G S E 6.19.0-rc1+ #1 PREEMPT(voluntary) [ 4504.423032] [ T63683] Tainted: [S]=CPU_OUT_OF_SPEC, [E]=UNSIGNED_MODULE [ 4504.423040] [ T63683] Hardware name: Inventec S520-A6/Nanping MLB, BIOS 01.01.01.06.03 03/03/2023 [ 4504.423050] [ T63683] RIP: 0010:__list_del_entry_valid_or_report+0x94/0x100 [ 4504.423064] [ T63683] Code: 89 fe 48 c7 c7 f0 78 87 b5 e8 38 07 ae ff 0f 0b 48 89 ef e8 6e 40 cd ff 48 89 ea 48 89 de 48 c7 c7 20 79 87 b5 e8 1c 07 ae ff <0f> 0b 4c 89 e7 e8 52 40 cd ff 4c 89 e2 48 89 de 48 c7 c7 58 79 87 [ 4504.423085] [ T63683] RSP: 0018:ff4efd9f3838fdb0 EFLAGS: 00010246 [ 4504.423093] [ T63683] RAX: 000000000000004e RBX: ff2adc9b51d19a90 RCX: 0000000000000027 [ 4504.423103] [ T63683] RDX: 0000000000000000 RSI: 0000000000000001 RDI: ff2add151cf99580 [ 4504.423112] [ T63683] RBP: dead000000000100 R08: 0000000000000000 R09: 0000000000000003 [ 4504.423120] [ T63683] R10: ff4efd9f3838fc60 R11: ff2add151cdfffe8 R12: dead000000000122 [ 4504.423130] [ T63683] R13: ff2adc9b51d19a00 R14: 0000000000000000 R15: 0000000000000000 [ 4504.423139] [ T63683] FS: 00007fae4f7ff6c0(0000) GS:ff2add15665f5000(0000) knlGS:0000000000000000 [ 4504.423148] [ T63683] CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033 [ 4504.423157] [ T63683] CR2: 000055aa8afe5000 CR3: 00000083037ee006 CR4: 0000000000773ef0 [ 4504.423166] [ T63683] PKRU: 55555554 [ 4504.423171] [ T63683] Call Trace: [ 4504.423178] [ T63683] <TASK> [ 4504.423184] [ T63683] io_do_iopoll+0x298/0x330 [ 4504.423193] [ T63683] ? asm_sysvec_apic_timer_interrupt+0x1a/0x20 [ 4504.423204] [ T63683] __do_sys_io_uring_enter+0x421/0x770 [ 4504.423214] [ T63683] do_syscall_64+0x67/0xf00 [ 4504.423223] [ T63683] entry_SYSCALL_64_after_hwframe+0x76/0x7e [ 4504.423232] [ T63683] RIP: 0033:0x55aa707e99c3Quoted 7 lines
It can be reproduced in three ways:
- Running iopoll tests while switching the block scheduler
- A split IO scenario in iopoll (e.g., bs=512k with max_sectors_kb=256k)
- Multi poll queues with multi threadsAll cases appear related to IO completions occurring outside the
io_do_iopoll()loop. The root cause remains unclear.Quoted 8 lines
Ah I see what it is - we can get multiple completions on the iopoll side, if you have multiple bio's per request. This didn't matter before the patch that uses a lockless list to collect them, as it just marked the request completed by writing to ->iopoll_complete and letting the reaper find them. But it matters with the llist change, as then we're adding the request to the llist more than once.
From
e2f749299e3c76ef92d3edfd9f8f7fc9a029129aMon Sep 17 00:00:00 2001
From: Diangang Li <[email protected]>
Date: Fri, 19 Dec 2025 10:14:33 +0800
Subject: [PATCH]io_uring: fix race between adding toctx->iopoll_listandctx->iopoll_complete
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bitSince commit
316693eb8aed("io_uring: be smarter about handling IOPOLL completions") introducedctx->iopoll_completeto cache polled completions, a request can be enqueued toctx->iopoll_completeas part of a batched poll while it is still in the issuing path.If the IO was submitted via
io_wq_submit_work(), it may still be stuck inio_iopoll_req_issued()waiting forctx->uring_lock, which is held byio_do_iopoll(). In this state,io_do_iopoll()may attempt to delete the request fromctx->iopoll_listbefore it has ever been linked, leading to alist_del()corruption.Fix this by introducing an
iopoll_stateflag to mark whether the request has been inserted intoctx->iopoll_list. Whenio_do_iopoll()tries to unlink a request and the flag indicates it hasn’t been linked yet, skip thelist_del()and just requeue the completion toctx->iopoll_completefor later reap.Signed-off-by Diangang Li <[email protected]>Signed-off-by Fengnan Chang <[email protected]>--- include/linux/io_uring_types.h | 1 + io_uring/io_uring.c | 1 + io_uring/rw.c | 7 +++++++ io_uring/uring_cmd.c | 1 + 4 files changed, 10 insertions(+)include/linux/ renamed from include/linux/io_uring_types.h +0
-0
io_uring_types.h
index0f619c37dce4..aaf26911badb100644include/linux/io_uring_types.h +1
-0@@ -677,6 +677,7 @@ struct io_kiocb { }; u8 opcode; + u8 iopoll_state; bool cancel_seq_set;io_uring/io_uring.c +1
-0@@ -1692,6 +1692,7 @@ static void io_iopoll_req_issued(struct io_kiocb *req, unsigned int issue_flags) } list_add_tail(&req->iopoll_node, &ctx->iopoll_list); + smp_store_release(&req->iopoll_state, 1); if (unlikely(needs_lock)) {/*
io_uring/rw.c +1
-0@@ -869,6 +869,7 @@ static int io_rw_init_file(struct io_kiocb *req, fmode_t mode, int rw_type) return -EOPNOTSUPP; kiocb->private = NULL; kiocb->ki_flags |= IOCB_HIPRI; + req->iopoll_state = 0; if (ctx->flags & IORING_SETUP_HYBRID_IOPOLL) { /* make sure every req only blocks once*/
req->flags&= ~REQ_F_IOPOLL_STATE;io_uring/rw.c +6
-0@@ -1355,6 +1356,12 @@ int io_do_iopoll(struct io_ring_ctx *ctx, bool force_nonspin) struct llist_node *next = node->next; req = container_of(node, struct io_kiocb, iopoll_done_list); + if (!READ_ONCE(req->iopoll_state)) { + node->next = NULL; + llist_add(&req->iopoll_done_list, &ctx->iopoll_complete); + node = next; + continue; + } list_del(&req->iopoll_node); wq_list_add_tail(&req->comp_list, &ctx->submit_state.compl_reqs);nr_events++;
io_uring/uring_cmd.c +1
-0@@ -251,6 +251,7 @@ int io_uring_cmd(struct io_kiocb *req, unsigned int issue_flags) if (!file->f_op->uring_cmd_iopoll) return -EOPNOTSUPP; issue_flags |= IO_URING_F_IOPOLL; + req->iopoll_state = 0; if (ctx->flags & IORING_SETUP_HYBRID_IOPOLL) { /* make sure every req only blocks once */
Quoted 1 line
req->flags&= ~REQ_F_IOPOLL_STATE;
Hi Jens,
Regarding the analysis of this list_del corruption issue and the fix patch, do you have any other comments?
Best regards,
Diangang Li
Jens Axboe
maintainerQuoted 175 lines · Diangang Li
Quoted 170 lines · Diangang Li
Quoted 58 lines · Jens Axboe
Quoted 49 lines · Diangang Li
Hi Jens,
We?ve identified one critical panic issue here.
[ 4504.422964] [ T63683] list_del corruption, ff2adc9b51d19a90->next is LIST_POISON1 (dead000000000100) [ 4504.422994] [ T63683] ------------[ cut here ]------------ [ 4504.422995] [ T63683] kernel BUG at lib/list_debug.c:56! [ 4504.423006] [ T63683] Oops: invalid opcode: 0000 [#1] SMP NOPTI [ 4504.423017] [ T63683] CPU: 38 UID: 0 PID: 63683 Comm: io_uring Kdump: loaded Tainted: G S E 6.19.0-rc1+ #1 PREEMPT(voluntary) [ 4504.423032] [ T63683] Tainted: [S]=CPU_OUT_OF_SPEC, [E]=UNSIGNED_MODULE [ 4504.423040] [ T63683] Hardware name: Inventec S520-A6/Nanping MLB, BIOS 01.01.01.06.03 03/03/2023 [ 4504.423050] [ T63683] RIP: 0010:__list_del_entry_valid_or_report+0x94/0x100 [ 4504.423064] [ T63683] Code: 89 fe 48 c7 c7 f0 78 87 b5 e8 38 07 ae ff 0f 0b 48 89 ef e8 6e 40 cd ff 48 89 ea 48 89 de 48 c7 c7 20 79 87 b5 e8 1c 07 ae ff <0f> 0b 4c 89 e7 e8 52 40 cd ff 4c 89 e2 48 89 de 48 c7 c7 58 79 87 [ 4504.423085] [ T63683] RSP: 0018:ff4efd9f3838fdb0 EFLAGS: 00010246 [ 4504.423093] [ T63683] RAX: 000000000000004e RBX: ff2adc9b51d19a90 RCX: 0000000000000027 [ 4504.423103] [ T63683] RDX: 0000000000000000 RSI: 0000000000000001 RDI: ff2add151cf99580 [ 4504.423112] [ T63683] RBP: dead000000000100 R08: 0000000000000000 R09: 0000000000000003 [ 4504.423120] [ T63683] R10: ff4efd9f3838fc60 R11: ff2add151cdfffe8 R12: dead000000000122 [ 4504.423130] [ T63683] R13: ff2adc9b51d19a00 R14: 0000000000000000 R15: 0000000000000000 [ 4504.423139] [ T63683] FS: 00007fae4f7ff6c0(0000) GS:ff2add15665f5000(0000) knlGS:0000000000000000 [ 4504.423148] [ T63683] CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033 [ 4504.423157] [ T63683] CR2: 000055aa8afe5000 CR3: 00000083037ee006 CR4: 0000000000773ef0 [ 4504.423166] [ T63683] PKRU: 55555554 [ 4504.423171] [ T63683] Call Trace: [ 4504.423178] [ T63683] <TASK> [ 4504.423184] [ T63683] io_do_iopoll+0x298/0x330 [ 4504.423193] [ T63683] ? asm_sysvec_apic_timer_interrupt+0x1a/0x20 [ 4504.423204] [ T63683] __do_sys_io_uring_enter+0x421/0x770 [ 4504.423214] [ T63683] do_syscall_64+0x67/0xf00 [ 4504.423223] [ T63683] entry_SYSCALL_64_after_hwframe+0x76/0x7e [ 4504.423232] [ T63683] RIP: 0033:0x55aa707e99c3Quoted 7 lines
It can be reproduced in three ways:
- Running iopoll tests while switching the block scheduler
- A split IO scenario in iopoll (e.g., bs=512k with max_sectors_kb=256k)
- Multi poll queues with multi threadsAll cases appear related to IO completions occurring outside the
io_do_iopoll()loop. The root cause remains unclear.Quoted 8 lines
Ah I see what it is - we can get multiple completions on the iopoll side, if you have multiple bio's per request. This didn't matter before the patch that uses a lockless list to collect them, as it just marked the request completed by writing to ->iopoll_complete and letting the reaper find them. But it matters with the llist change, as then we're adding the request to the llist more than once.
From
e2f749299e3c76ef92d3edfd9f8f7fc9a029129aMon Sep 17 00:00:00 2001
From: Diangang Li <[email protected]>
Date: Fri, 19 Dec 2025 10:14:33 +0800
Subject: [PATCH]io_uring: fix race between adding toctx->iopoll_listandctx->iopoll_complete
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bitSince commit
316693eb8aed("io_uring: be smarter about handling IOPOLL completions") introducedctx->iopoll_completeto cache polled completions, a request can be enqueued toctx->iopoll_completeas part of a batched poll while it is still in the issuing path.If the IO was submitted via
io_wq_submit_work(), it may still be stuck inio_iopoll_req_issued()waiting forctx->uring_lock, which is held byio_do_iopoll(). In this state,io_do_iopoll()may attempt to delete the request fromctx->iopoll_listbefore it has ever been linked, leading to alist_del()corruption.Fix this by introducing an
iopoll_stateflag to mark whether the request has been inserted intoctx->iopoll_list. Whenio_do_iopoll()tries to unlink a request and the flag indicates it hasn?t been linked yet, skip thelist_del()and just requeue the completion toctx->iopoll_completefor later reap.Signed-off-by Diangang Li <[email protected]>Signed-off-by Fengnan Chang <[email protected]>--- include/linux/io_uring_types.h | 1 + io_uring/io_uring.c | 1 + io_uring/rw.c | 7 +++++++ io_uring/uring_cmd.c | 1 + 4 files changed, 10 insertions(+)include/linux/ renamed from include/linux/io_uring_types.h +0
-0
io_uring_types.h
index0f619c37dce4..aaf26911badb100644include/linux/io_uring_types.h +1
-0@@ -677,6 +677,7 @@ struct io_kiocb { };u8 opcode; +u8 iopoll_state;bool cancel_seq_set;io_uring/io_uring.c +1
-0@@ -1692,6 +1692,7 @@ static void io_iopoll_req_issued(struct io_kiocb *req, unsigned int issue_flags) }list_add_tail (&req->iopoll_node, &ctx->iopoll_list); +smp_store_release (&req->iopoll_state,1 );if (unlikely (needs_lock)) {/*io_uring/rw.c +1
-0@@ -869,6 +869,7 @@ static int io_rw_init_file(struct io_kiocb *req, fmode_t mode, int rw_type) return -EOPNOTSUPP; kiocb->private =NULL ; kiocb->ki_flags |=IOCB_HIPRI ; + req->iopoll_state =0 ;if (ctx->flags &IORING_SETUP_HYBRID_IOPOLL ) {/* make sure every req only blocks once*/ req->flags &= ~REQ_F_IOPOLL_STATE;io_uring/rw.c +6
-0@@ -1355,6 +1356,12 @@ int io_do_iopoll(struct io_ring_ctx *ctx, bool force_nonspin)struct llist_node *next = node->next; req = container_of(node, struct io_kiocb, iopoll_done_list); +if (!READ_ONCE (req->iopoll_state)) { + node->next =NULL ; +llist_add (&req->iopoll_done_list, &ctx->iopoll_complete); + node = next; +continue ; + }list_del (&req->iopoll_node);wq_list_add_tail (&req->comp_list, &ctx->submit_state.compl_reqs);nr_events++;io_uring/uring_cmd.c +1
-0@@ -251,6 +251,7 @@ int io_uring_cmd(struct io_kiocb *req, unsigned int issue_flags)if (!file->f_op->uring_cmd_iopoll)return -EOPNOTSUPP ; issue_flags |=IO_URING_F_IOPOLL ; + req->iopoll_state =0 ;if (ctx->flags &IORING_SETUP_HYBRID_IOPOLL ) {/* make sure every req only blocks once */ Quoted 1 line
req->flags &= ~REQ_F_IOPOLL_STATE;Hi Jens,
Quoted 2 lines
Regarding the analysis of this
list_delcorruption issue and the fix patch, do you have any other comments?
I just dropped the second part of the iopoll changes, so it's back to just this one:
I didn't have an immediately good idea for solving it without doing more locking and/or synchronization, and I wasn't convinced it was worth it.
I'll ponder it some more next week and pick it back up.
Signature · 1 line
Jens Axboe
Re: (subset) [RFC PATCH 0/2] io_uring: fix io may accumulation in poll mode
In reply to Fengnan ChangQuoted 12 lines · Fengnan Chang
In the
io_do_iopollfunction, when the poll loop ofiopoll_listends, it is considered that the current req is the actual completed request. This may be reasonable for multi-queue ctx, but is problematic for single-queue ctx because the current request may not be done when the poll gets to the result. In this case, the completed io needs to wait for the first io on the chain to complete before notifying the user, which may cause io accumulation in the list.
Our modification plan is as follows: changeio_wq_work_listto normal list so that theiopoll_listlist in it can be removed and put into thecomp_reqslist when the request is completed. This way each io is handled independently and all gets processed in time.
Quoted 1 line
[...]
Applied, thanks!
[1/2] blk-mq: delete task running check in blk_hctx_poll
(no commit info)
Best regards,
Signature · 1 line
Jens Axboe