Skip to contentSkip to conversation

ublk: build batch from IOs in same io_ring_ctx and io task

PATCH Applied Ming Lei 3 participants 3 messages Jun 25, 2025 – Jun 26, 2025

Subsystems (2)
lore
Share & tools
lore

Ming Lei

author
#0 lore raw

PATCH v2 1/1 ublk: build batch from IOs in same io_ring_ctx and io task

ublk_queue_cmd_list() dispatches the whole batch list by scheduling task work via the tail request's io_uring_cmd, this way is fine even though more than one io_ring_ctx are involved for this batch since it is just one running context.

However, the task work handler ublk_cmd_list_tw_cb() takes issue_flags of tail uring_cmd's io_ring_ctx for completing all commands. This way is wrong if any uring_cmd is issued from different io_ring_ctx.

Fixes it by always building batch IOs from same io_ring_ctx and io task because ublk_dispatch_req() does validate task context, and IO needs to be aborted in case of running from fallback task work context.

For typical per-queue or per-io daemon implementation, this way shouldn't make difference from performance viewpoint, because single io_ring_ctx is taken in each daemon for normal use case.

Fixes d796cea7b9f3 ("ublk: implement ->queue_rqs()")

---
V2:
- build batch list from same io_uring_ctx and io task(Caleb Sander Mateos)

 drivers/block/ublk_drv.c | 11 ++++++++++-
 1 file changed, 10 insertions(+), 1 deletion(-)
drivers/block/ublk_drv.c +10 -1
index 3566d7c36b8d..d441d3259edb 100644
@@ -1423,6 +1423,14 @@ static blk_status_t ublk_queue_rq(struct blk_mq_hw_ctx *hctx,
 	return BLK_STS_OK;
 }
 
+static inline bool ublk_belong_to_same_batch(const struct ublk_io *io,
+					     const struct ublk_io *io2)
+{
+	return (io_uring_cmd_ctx_handle(io->cmd) ==
+		io_uring_cmd_ctx_handle(io2->cmd)) &&
+		(io->task == io2->task);
+}
+
 static void ublk_queue_rqs(struct rq_list *rqlist)
 {
 	struct rq_list requeue_list = { };
@@ -1434,7 +1442,8 @@ static void ublk_queue_rqs(struct rq_list *rqlist)
 		struct ublk_queue *this_q = req->mq_hctx->driver_data;
 		struct ublk_io *this_io = &this_q->ios[req->tag];
 
-		if (io && io->task != this_io->task && !rq_list_empty(&submit_list))
+		if (io && !ublk_belong_to_same_batch(io, this_io) &&
+				!rq_list_empty(&submit_list))
 			ublk_queue_cmd_list(io, &submit_list);
 		io = this_io;
 
Signature · 1 line
2.47.1
maintainer Applied
#1 lore raw
Quoted 10 lines · Ming Lei

ublk_queue_cmd_list() dispatches the whole batch list by scheduling task work via the tail request's io_uring_cmd, this way is fine even though more than one io_ring_ctx are involved for this batch since it is just one running context.

However, the task work handler ublk_cmd_list_tw_cb() takes issue_flags of tail uring_cmd's io_ring_ctx for completing all commands. This way is wrong if any uring_cmd is issued from different io_ring_ctx.

[...]

Applied, thanks!

[1/1] ublk: build batch from IOs in same io_ring_ctx and io task

      commit: 524346e9d79f63a6e5aaa645140da3d1ec7a8a0f

Best regards,

Signature · 1 line
Jens Axboe
Reviewed-by on v2 1/1
#2 lore raw
In reply to Ming Lei
Quoted 14 lines · Ming Lei

ublk_queue_cmd_list() dispatches the whole batch list by scheduling task work via the tail request's io_uring_cmd, this way is fine even though more than one io_ring_ctx are involved for this batch since it is just one running context.

However, the task work handler ublk_cmd_list_tw_cb() takes issue_flags of tail uring_cmd's io_ring_ctx for completing all commands. This way is wrong if any uring_cmd is issued from different io_ring_ctx.

Fixes it by always building batch IOs from same io_ring_ctx and io task because ublk_dispatch_req() does validate task context, and IO needs to be aborted in case of running from fallback task work context.

Quoted 6 lines

For typical per-queue or per-io daemon implementation, this way shouldn't make difference from performance viewpoint, because single io_ring_ctx is taken in each daemon for normal use case.

Fixes d796cea7b9f3 ("ublk: implement ->queue_rqs()")