Skip to contentSkip to conversation

Minor ublk optimizations

PATCH Applied Caleb Sander Mateos 3 participants 9 messages Mar 28, 2025 – Mar 29, 2025

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

cover Minor ublk optimizations

A few cleanups on top of Ming's recent patch set that implemented ->queue_rqs() for ublk:
https://lore.kernel.org/linux-block/[email protected]/T/#u

Caleb Sander Mateos (5):
ublk: remove unused cmd argument to ublk_dispatch_req()
ublk: skip 1 NULL check in ublk_cmd_list_tw_cb() loop
ublk: get ubq from pdu in ublk_cmd_list_tw_cb()
ublk: avoid redundant io->cmd in ublk_queue_cmd_list()
ublk: store req in ublk_uring_cmd_pdu for ublk_cmd_tw_cb()

 drivers/block/ublk_drv.c | 33 ++++++++++++++++-----------------
 1 file changed, 16 insertions(+), 17 deletions(-)
Signature · 1 line
2.45.2
#1 lore raw

PATCH 1/5 ublk: remove unused cmd argument to ublk_dispatch_req()

ublk_dispatch_req() never uses its struct io_uring_cmd *cmd argument. Drop it so callers don't have to pass a value.

---
 drivers/block/ublk_drv.c | 7 ++-----
 1 file changed, 2 insertions(+), 5 deletions(-)
drivers/block/ublk_drv.c +2 -5
index 355a59c78539..39efe443e235 100644
@@ -1183,11 +1183,10 @@ static inline void __ublk_abort_rq(struct ublk_queue *ubq,
 	else
 		blk_mq_end_request(rq, BLK_STS_IOERR);
 }
 
 static void ublk_dispatch_req(struct ublk_queue *ubq,
-			      struct io_uring_cmd *cmd,
 			      struct request *req,
 			      unsigned int issue_flags)
 {
 	int tag = req->tag;
 	struct ublk_io *io = &ubq->ios[tag];
@@ -1271,11 +1270,11 @@ static void ublk_cmd_tw_cb(struct io_uring_cmd *cmd,
 	struct ublk_queue *ubq = pdu->ubq;
 	int tag = pdu->tag;
 	struct request *req = blk_mq_tag_to_rq(
 		ubq->dev->tag_set.tags[ubq->q_id], tag);
 
-	ublk_dispatch_req(ubq, cmd, req, issue_flags);
+	ublk_dispatch_req(ubq, req, issue_flags);
 }
 
 static void ublk_queue_cmd(struct ublk_queue *ubq, struct request *rq)
 {
 	struct ublk_io *io = &ubq->ios[rq->tag];
@@ -1290,15 +1289,13 @@ static void ublk_cmd_list_tw_cb(struct io_uring_cmd *cmd,
 	struct request *rq = pdu->req_list;
 	struct ublk_queue *ubq = rq->mq_hctx->driver_data;
 	struct request *next;
 
 	while (rq) {
-		struct ublk_io *io = &ubq->ios[rq->tag];
-
 		next = rq->rq_next;
 		rq->rq_next = NULL;
-		ublk_dispatch_req(ubq, io->cmd, rq, issue_flags);
+		ublk_dispatch_req(ubq, rq, issue_flags);
 		rq = next;
 	}
 }
 
 static void ublk_queue_cmd_list(struct ublk_queue *ubq, struct rq_list *l)
Signature · 1 line
2.45.2
#2 lore raw

PATCH 2/5 ublk: skip 1 NULL check in ublk_cmd_list_tw_cb() loop

In reply to Caleb Sander Mateos

ublk_cmd_list_tw_cb() is always performed on a non-empty request list. So don't check whether rq is NULL on the first iteration of the loop, just on subsequent iterations.

---
 drivers/block/ublk_drv.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/block/ublk_drv.c +2 -2
index 39efe443e235..8b9780c0feab 100644
@@ -1288,16 +1288,16 @@ static void ublk_cmd_list_tw_cb(struct io_uring_cmd *cmd,
 	struct ublk_uring_cmd_pdu *pdu = ublk_get_uring_cmd_pdu(cmd);
 	struct request *rq = pdu->req_list;
 	struct ublk_queue *ubq = rq->mq_hctx->driver_data;
 	struct request *next;
 
-	while (rq) {
+	do {
 		next = rq->rq_next;
 		rq->rq_next = NULL;
 		ublk_dispatch_req(ubq, rq, issue_flags);
 		rq = next;
-	}
+	} while (rq);
 }
 
 static void ublk_queue_cmd_list(struct ublk_queue *ubq, struct rq_list *l)
 {
 	struct request *rq = rq_list_peek(l);
Signature · 1 line
2.45.2
#3 lore raw

PATCH 3/5 ublk: get ubq from pdu in ublk_cmd_list_tw_cb()

In reply to Caleb Sander Mateos

Save a few pointer dereferences by obtaining struct ublk_queue *ubq from the ublk_uring_cmd_pdu instead of the request's mq_hctx.

---
 drivers/block/ublk_drv.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/block/ublk_drv.c +1 -1
index 8b9780c0feab..9276d1fcc100 100644
@@ -1285,11 +1285,11 @@ static void ublk_queue_cmd(struct ublk_queue *ubq, struct request *rq)
 static void ublk_cmd_list_tw_cb(struct io_uring_cmd *cmd,
 		unsigned int issue_flags)
 {
 	struct ublk_uring_cmd_pdu *pdu = ublk_get_uring_cmd_pdu(cmd);
 	struct request *rq = pdu->req_list;
-	struct ublk_queue *ubq = rq->mq_hctx->driver_data;
+	struct ublk_queue *ubq = pdu->ubq;
 	struct request *next;
 
 	do {
 		next = rq->rq_next;
 		rq->rq_next = NULL;
Signature · 1 line
2.45.2
#4 lore raw

PATCH 4/5 ublk: avoid redundant io->cmd in ublk_queue_cmd_list()

In reply to Caleb Sander Mateos

ublk_queue_cmd_list() loads io->cmd twice. The intervening stores prevent the compiler from combining the loads. Since struct ublk_io *io is only used to compute io->cmd, replace the variable with io->cmd.

---
 drivers/block/ublk_drv.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
drivers/block/ublk_drv.c +3 -3
index 9276d1fcc100..23250471562a 100644
@@ -1299,16 +1299,16 @@ static void ublk_cmd_list_tw_cb(struct io_uring_cmd *cmd,
 }
 
 static void ublk_queue_cmd_list(struct ublk_queue *ubq, struct rq_list *l)
 {
 	struct request *rq = rq_list_peek(l);
-	struct ublk_io *io = &ubq->ios[rq->tag];
-	struct ublk_uring_cmd_pdu *pdu = ublk_get_uring_cmd_pdu(io->cmd);
+	struct io_uring_cmd *cmd = ubq->ios[rq->tag].cmd;
+	struct ublk_uring_cmd_pdu *pdu = ublk_get_uring_cmd_pdu(cmd);
 
 	pdu->req_list = rq;
 	rq_list_init(l);
-	io_uring_cmd_complete_in_task(io->cmd, ublk_cmd_list_tw_cb);
+	io_uring_cmd_complete_in_task(cmd, ublk_cmd_list_tw_cb);
 }
 
 static enum blk_eh_timer_return ublk_timeout(struct request *rq)
 {
 	struct ublk_queue *ubq = rq->mq_hctx->driver_data;
Signature · 1 line
2.45.2
#5 lore raw

PATCH 5/5 ublk: store req in ublk_uring_cmd_pdu for ublk_cmd_tw_cb()

In reply to Caleb Sander Mateos

Pass struct request *rq to ublk_cmd_tw_cb() through ublk_uring_cmd_pdu, mirroring how it works for ublk_cmd_list_tw_cb(). This saves some pointer dereferences, as well as the bounds check in blk_mq_tag_to_rq().

---
 drivers/block/ublk_drv.c | 16 +++++++++-------
 1 file changed, 9 insertions(+), 7 deletions(-)
drivers/block/ublk_drv.c +9 -7
index 23250471562a..466a23b89379 100644
@@ -87,11 +87,14 @@ struct ublk_uring_cmd_pdu {
 	 *
 	 * It should have been stored to request payload, but we do want
 	 * to avoid extra pre-allocation, and uring_cmd payload is always
 	 * free for us
 	 */
-	struct request *req_list;
+	union {
+		struct request *req;
+		struct request *req_list;
+	};
 
 	/*
 	 * The following two are valid in this cmd whole lifetime, and
 	 * setup in ublk uring_cmd handler
 	 */
@@ -1266,22 +1269,21 @@ static void ublk_dispatch_req(struct ublk_queue *ubq,
 static void ublk_cmd_tw_cb(struct io_uring_cmd *cmd,
 			   unsigned int issue_flags)
 {
 	struct ublk_uring_cmd_pdu *pdu = ublk_get_uring_cmd_pdu(cmd);
 	struct ublk_queue *ubq = pdu->ubq;
-	int tag = pdu->tag;
-	struct request *req = blk_mq_tag_to_rq(
-		ubq->dev->tag_set.tags[ubq->q_id], tag);
 
-	ublk_dispatch_req(ubq, req, issue_flags);
+	ublk_dispatch_req(ubq, pdu->req, issue_flags);
 }
 
 static void ublk_queue_cmd(struct ublk_queue *ubq, struct request *rq)
 {
-	struct ublk_io *io = &ubq->ios[rq->tag];
+	struct io_uring_cmd *cmd = ubq->ios[rq->tag].cmd;
+	struct ublk_uring_cmd_pdu *pdu = ublk_get_uring_cmd_pdu(cmd);
 
-	io_uring_cmd_complete_in_task(io->cmd, ublk_cmd_tw_cb);
+	pdu->req = rq;
+	io_uring_cmd_complete_in_task(cmd, ublk_cmd_tw_cb);
 }
 
 static void ublk_cmd_list_tw_cb(struct io_uring_cmd *cmd,
 		unsigned int issue_flags)
 {
Signature · 1 line
2.45.2
maintainer
#6 lore raw
In reply to Caleb Sander Mateos
Quoted 10 lines · Caleb Sander Mateos

A few cleanups on top of Ming's recent patch set that implemented ->queue_rqs() for ublk:
https://lore.kernel.org/linux-block/[email protected]/T/#u

Caleb Sander Mateos (5):
ublk: remove unused cmd argument to ublk_dispatch_req()
ublk: skip 1 NULL check in ublk_cmd_list_tw_cb() loop
ublk: get ubq from pdu in ublk_cmd_list_tw_cb()
ublk: avoid redundant io->cmd in ublk_queue_cmd_list()
ublk: store req in ublk_uring_cmd_pdu for ublk_cmd_tw_cb()

All look like reasonable cleanups to me.

Signature · 1 line
Jens Axboe
Reviewed-by on the v1 series
#7 lore raw
In reply to Caleb Sander Mateos
Quoted 11 lines · Caleb Sander Mateos

A few cleanups on top of Ming's recent patch set that implemented ->queue_rqs() for ublk:
https://lore.kernel.org/linux-block/[email protected]/T/#u

Caleb Sander Mateos (5):
ublk: remove unused cmd argument to ublk_dispatch_req()
ublk: skip 1 NULL check in ublk_cmd_list_tw_cb() loop
ublk: get ubq from pdu in ublk_cmd_list_tw_cb()
ublk: avoid redundant io->cmd in ublk_queue_cmd_list()
ublk: store req in ublk_uring_cmd_pdu for ublk_cmd_tw_cb()

Quoted 2 lines
 drivers/block/ublk_drv.c | 33 ++++++++++++++++-----------------
 1 file changed, 16 insertions(+), 17 deletions(-)

All are nice cleanup, and pass builtin ublk selftest.

Thanks,
Ming

maintainer Applied
#8 lore raw
In reply to Caleb Sander Mateos
Quoted 4 lines · Caleb Sander Mateos

A few cleanups on top of Ming's recent patch set that implemented ->queue_rqs() for ublk:
https://lore.kernel.org/linux-block/[email protected]/T/#u

Quoted 8 lines

Caleb Sander Mateos (5):
ublk: remove unused cmd argument to ublk_dispatch_req()
ublk: skip 1 NULL check in ublk_cmd_list_tw_cb() loop
ublk: get ubq from pdu in ublk_cmd_list_tw_cb()
ublk: avoid redundant io->cmd in ublk_queue_cmd_list()
ublk: store req in ublk_uring_cmd_pdu for ublk_cmd_tw_cb()

[...]

Applied, thanks!

[1/5] ublk: remove unused cmd argument to ublk_dispatch_req()

      commit: dfbce8b798fb848a42706e2e544b78b3db22aaae
[2/5] ublk: skip 1 NULL check in ublk_cmd_list_tw_cb() loop
      commit: 9d7fa99189709b80eb16094aad18f7e492b835de
[3/5] ublk: get ubq from pdu in ublk_cmd_list_tw_cb()
      commit: 6a87fc437a034e4be2a63d8dfd4d2985c6c574bc
[4/5] ublk: avoid redundant io->cmd in ublk_queue_cmd_list()
      commit: 108d8aecaeeb52f5fbe98ac94da534954db1da44
[5/5] ublk: store req in ublk_uring_cmd_pdu for ublk_cmd_tw_cb()
      commit: 00cfc05cf81f58b1bc2650e18228350a094b1f6d

Best regards,

Signature · 1 line
Jens Axboe