Skip to contentSkip to conversation

don't reorder requests passed to ->queue_rqs

PATCH In review Christoph Hellwig 7 participants 26 messages Nov 13, 2024 – Nov 18, 2024

Subsystems (6)
lore
Share & tools
lore

Christoph Hellwig

author maintainer
#0 lore raw

Hi Jens,

currently blk-mq reorders requests when adding them to the plug because the request list can't do efficient tail appends. When the plug is directly issued using ->queue_rqs that means reordered requests are passed to the driver, which can lead to very bad I/O patterns when not corrected, especially on rotational devices (e.g. NVMe HDD) or when using zone append.

This series first adds two easily backportable workarounds to reverse the reording in the virtio_blk and nvme-pci ->queue_rq implementations similar to what the non-queue_rqs path does, and then adds a rq_list type that allows for efficient tail insertions and uses that to fix the reordering for real and then does the same for I/O completions as well.

Diffstat:

 block/blk-core.c              |    6 +-
 block/blk-merge.c             |    2 
 block/blk-mq.c                |   42 ++++++++---------
 block/blk-mq.h                |    2 
 drivers/block/null_blk/main.c |    9 +--
 drivers/block/virtio_blk.c    |   53 ++++++++++------------
 drivers/nvme/host/apple.c     |    2 
 drivers/nvme/host/pci.c       |   46 ++++++++-----------
 include/linux/blk-mq.h        |   99 ++++++++++++++++++++----------------------
 include/linux/blkdev.h        |   11 +++-
 io_uring/rw.c                 |    4 -
 11 files changed, 133 insertions(+), 143 deletions(-)
author maintainer
#1 lore raw

PATCH 1/6 nvme-pci: reverse request order in nvme_queue_rqs

blk_mq_flush_plug_list submits requests in the reverse order that they were submitted, which leads to a rather suboptimal I/O pattern especially in rotational devices. Fix this by rewriting nvme_queue_rqs so that it always pops the requests from the passed in request list, and then adds them to the head of a local submit list. This actually simplifies the code a bit as it removes the complicated list splicing, at the cost of extra updates of the rq_next pointer. As that should be cache hot anyway it should be an easy price to pay.

Fixes d62cbcf62f2f ("nvme: add support for mq_ops->queue_rqs()")
---
 drivers/nvme/host/pci.c | 39 +++++++++++++++++----------------------
 1 file changed, 17 insertions(+), 22 deletions(-)
drivers/nvme/host/pci.c +17 -22
index 4b9fda0b1d9a..c66ea2c23690 100644
@@ -904,9 +904,10 @@ static blk_status_t nvme_queue_rq(struct blk_mq_hw_ctx *hctx,
 
 static void nvme_submit_cmds(struct nvme_queue *nvmeq, struct request **rqlist)
 {
+	struct request *req;
+
 	spin_lock(&nvmeq->sq_lock);
-	while (!rq_list_empty(*rqlist)) {
-		struct request *req = rq_list_pop(rqlist);
+	while ((req = rq_list_pop(rqlist))) {
 		struct nvme_iod *iod = blk_mq_rq_to_pdu(req);
 
 		nvme_sq_copy_cmd(nvmeq, &iod->cmd);
@@ -931,31 +932,25 @@ static bool nvme_prep_rq_batch(struct nvme_queue *nvmeq, struct request *req)
 
 static void nvme_queue_rqs(struct request **rqlist)
 {
-	struct request *req, *next, *prev = NULL;
+	struct request *submit_list = NULL;
 	struct request *requeue_list = NULL;
+	struct request **requeue_lastp = &requeue_list;
+	struct nvme_queue *nvmeq = NULL;
+	struct request *req;
 
-	rq_list_for_each_safe(rqlist, req, next) {
-		struct nvme_queue *nvmeq = req->mq_hctx->driver_data;
-
-		if (!nvme_prep_rq_batch(nvmeq, req)) {
-			/* detach 'req' and add to remainder list */
-			rq_list_move(rqlist, &requeue_list, req, prev);
-
-			req = prev;
-			if (!req)
-				continue;
-		}
+	while ((req = rq_list_pop(rqlist))) {
+		if (nvmeq && nvmeq != req->mq_hctx->driver_data)
+			nvme_submit_cmds(nvmeq, &submit_list);
+		nvmeq = req->mq_hctx->driver_data;
 
-		if (!next || req->mq_hctx != next->mq_hctx) {
-			/* detach rest of list, and submit */
-			req->rq_next = NULL;
-			nvme_submit_cmds(nvmeq, rqlist);
-			*rqlist = next;
-			prev = NULL;
-		} else
-			prev = req;
+		if (nvme_prep_rq_batch(nvmeq, req))
+			rq_list_add(&submit_list, req); /* reverse order */
+		else
+			rq_list_add_tail(&requeue_lastp, req);
 	}
 
+	if (nvmeq)
+		nvme_submit_cmds(nvmeq, &submit_list);
 	*rqlist = requeue_list;
 }
 
Signature · 1 line
2.45.2
maintainer Reviewed-by on v1 1/6
#2 lore raw

Re: [PATCH 1/6] nvme-pci: reverse request order in nvme_queue_rqs

Quoted 8 lines · Christoph Hellwig

blk_mq_flush_plug_list submits requests in the reverse order that they were submitted, which leads to a rather suboptimal I/O pattern especially in rotational devices. Fix this by rewriting nvme_queue_rqs so that it always pops the requests from the passed in request list, and then adds them to the head of a local submit list. This actually simplifies the code a bit as it removes the complicated list splicing, at the cost of extra updates of the rq_next pointer. As that should be cache hot anyway it should be an easy price to pay.

Looks good.

author maintainer
#3 lore raw

PATCH 2/6 virtio_blk: reverse request order in virtio_queue_rqs

In reply to Christoph Hellwig

blk_mq_flush_plug_list submits requests in the reverse order that they were submitted, which leads to a rather suboptimal I/O pattern especially in rotational devices. Fix this by rewriting nvme_queue_rqs so that it always pops the requests from the passed in request list, and then adds them to the head of a local submit list. This actually simplifies the code a bit as it removes the complicated list splicing, at the cost of extra updates of the rq_next pointer. As that should be cache hot anyway it should be an easy price to pay.

Fixes 0e9911fa768f ("virtio-blk: support mq_ops->queue_rqs()")
---
 drivers/block/virtio_blk.c | 46 +++++++++++++++++---------------------
 1 file changed, 21 insertions(+), 25 deletions(-)
drivers/block/virtio_blk.c +21 -25
index 0e99a4714928..b25f7c06a28e 100644
@@ -471,18 +471,18 @@ static bool virtblk_prep_rq_batch(struct request *req)
 	return virtblk_prep_rq(req->mq_hctx, vblk, req, vbr) == BLK_STS_OK;
 }
 
-static bool virtblk_add_req_batch(struct virtio_blk_vq *vq,
+static void virtblk_add_req_batch(struct virtio_blk_vq *vq,
 					struct request **rqlist)
 {
+	struct request *req;
 	unsigned long flags;
-	int err;
 	bool kick;
 
 	spin_lock_irqsave(&vq->lock, flags);
 
-	while (!rq_list_empty(*rqlist)) {
-		struct request *req = rq_list_pop(rqlist);
+	while ((req = rq_list_pop(rqlist))) {
 		struct virtblk_req *vbr = blk_mq_rq_to_pdu(req);
+		int err;
 
 		err = virtblk_add_req(vq->vq, vbr);
 		if (err) {
@@ -495,37 +495,33 @@ static bool virtblk_add_req_batch(struct virtio_blk_vq *vq,
 	kick = virtqueue_kick_prepare(vq->vq);
 	spin_unlock_irqrestore(&vq->lock, flags);
 
-	return kick;
+	if (kick)
+		virtqueue_notify(vq->vq);
 }
 
 static void virtio_queue_rqs(struct request **rqlist)
 {
-	struct request *req, *next, *prev = NULL;
+	struct request *submit_list = NULL;
 	struct request *requeue_list = NULL;
+	struct request **requeue_lastp = &requeue_list;
+	struct virtio_blk_vq *vq = NULL;
+	struct request *req;
 
-	rq_list_for_each_safe(rqlist, req, next) {
-		struct virtio_blk_vq *vq = get_virtio_blk_vq(req->mq_hctx);
-		bool kick;
-
-		if (!virtblk_prep_rq_batch(req)) {
-			rq_list_move(rqlist, &requeue_list, req, prev);
-			req = prev;
-			if (!req)
-				continue;
-		}
+	while ((req = rq_list_pop(rqlist))) {
+		struct virtio_blk_vq *this_vq = get_virtio_blk_vq(req->mq_hctx);
 
-		if (!next || req->mq_hctx != next->mq_hctx) {
-			req->rq_next = NULL;
-			kick = virtblk_add_req_batch(vq, rqlist);
-			if (kick)
-				virtqueue_notify(vq->vq);
+		if (vq && vq != this_vq)
+			virtblk_add_req_batch(vq, &submit_list);
+		vq = this_vq;
 
-			*rqlist = next;
-			prev = NULL;
-		} else
-			prev = req;
+		if (virtblk_prep_rq_batch(req))
+			rq_list_add(&submit_list, req); /* reverse order */
+		else
+			rq_list_add_tail(&requeue_lastp, req);
 	}
 
+	if (vq)
+		virtblk_add_req_batch(vq, &submit_list);
 	*rqlist = requeue_list;
 }
 
Signature · 1 line
2.45.2
maintainer
#4 lore raw

Re: [PATCH 2/6] virtio_blk: reverse request order in virtio_queue_rqs

Quoted 1 line · Christoph Hellwig

in rotational devices. Fix this by rewriting nvme_queue_rqs so that it

You copied this commit message from the previous one for the nvme driver. This message should say "virtio_queue_rqs".

maintainer
#5 lore raw

Re: [PATCH 2/6] virtio_blk: reverse request order in virtio_queue_rqs

Quoted 5 lines · Keith Busch
Quoted 1 line · Christoph Hellwig

in rotational devices. Fix this by rewriting nvme_queue_rqs so that it

You copied this commit message from the previous one for the nvme driver. This message should say "virtio_queue_rqs".

I fixed it up. As well as the annoying "two spaces after a period", that should just go away.

Signature · 1 line
Jens Axboe
maintainer Acked-by on v1 2/6
#6 lore raw

Re: [PATCH 2/6] virtio_blk: reverse request order in virtio_queue_rqs

In reply to Christoph Hellwig
Quoted 86 lines · Christoph Hellwig

blk_mq_flush_plug_list submits requests in the reverse order that they were submitted, which leads to a rather suboptimal I/O pattern especially in rotational devices. Fix this by rewriting nvme_queue_rqs so that it always pops the requests from the passed in request list, and then adds them to the head of a local submit list. This actually simplifies the code a bit as it removes the complicated list splicing, at the cost of extra updates of the rq_next pointer. As that should be cache hot anyway it should be an easy price to pay.

Fixes 0e9911fa768f ("virtio-blk: support mq_ops->queue_rqs()")
---
 drivers/block/virtio_blk.c | 46 +++++++++++++++++---------------------
 1 file changed, 21 insertions(+), 25 deletions(-)
drivers/block/virtio_blk.c +15 -25
index 0e99a4714928..b25f7c06a28e 100644
@@ -471,18 +471,18 @@ static bool virtblk_prep_rq_batch(struct request *req)
 	return virtblk_prep_rq(req->mq_hctx, vblk, req, vbr) == BLK_STS_OK;
 }
 
-static bool virtblk_add_req_batch(struct virtio_blk_vq *vq,
+static void virtblk_add_req_batch(struct virtio_blk_vq *vq,
 					struct request **rqlist)
 {
+	struct request *req;
 	unsigned long flags;
-	int err;
 	bool kick;
 
 	spin_lock_irqsave(&vq->lock, flags);
 
-	while (!rq_list_empty(*rqlist)) {
-		struct request *req = rq_list_pop(rqlist);
+	while ((req = rq_list_pop(rqlist))) {
 		struct virtblk_req *vbr = blk_mq_rq_to_pdu(req);
+		int err;
 
 		err = virtblk_add_req(vq->vq, vbr);
 		if (err) {
@@ -495,37 +495,33 @@ static bool virtblk_add_req_batch(struct virtio_blk_vq *vq,
 	kick = virtqueue_kick_prepare(vq->vq);
 	spin_unlock_irqrestore(&vq->lock, flags);
 
-	return kick;
+	if (kick)
+		virtqueue_notify(vq->vq);
 }
 
 static void virtio_queue_rqs(struct request **rqlist)
 {
-	struct request *req, *next, *prev = NULL;
+	struct request *submit_list = NULL;
 	struct request *requeue_list = NULL;
+	struct request **requeue_lastp = &requeue_list;
+	struct virtio_blk_vq *vq = NULL;
+	struct request *req;
 
-	rq_list_for_each_safe(rqlist, req, next) {
-		struct virtio_blk_vq *vq = get_virtio_blk_vq(req->mq_hctx);
-		bool kick;
-
-		if (!virtblk_prep_rq_batch(req)) {
-			rq_list_move(rqlist, &requeue_list, req, prev);
-			req = prev;
-			if (!req)
-				continue;
-		}
+	while ((req = rq_list_pop(rqlist))) {
+		struct virtio_blk_vq *this_vq = get_virtio_blk_vq(req->mq_hctx);
 
-		if (!next || req->mq_hctx != next->mq_hctx) {
-			req->rq_next = NULL;
-			kick = virtblk_add_req_batch(vq, rqlist);
-			if (kick)
-				virtqueue_notify(vq->vq);
+		if (vq && vq != this_vq)
+			virtblk_add_req_batch(vq, &submit_list);
+		vq = this_vq;
 
-			*rqlist = next;
-			prev = NULL;
-		} else
-			prev = req;

drivers/block/virtio_blk.c static bool virtblk_add_req_batch(struct virtio_blk_vq *vq, · line 526

Quoted 10 lines
drivers/block/virtio_blk.c +6 -0
+		if (virtblk_prep_rq_batch(req))
+			rq_list_add(&submit_list, req); /* reverse order */
+		else
+			rq_list_add_tail(&requeue_lastp, req);
 	}
 
+	if (vq)
+		virtblk_add_req_batch(vq, &submit_list);
 	*rqlist = requeue_list;
 }

looks ok from virtio POV

Quoted 2 lines
Signature · 1 line
2.45.2
author maintainer
#7 lore raw

PATCH 3/6 block: remove rq_list_move

In reply to Christoph Hellwig

Unused now.

---
 include/linux/blk-mq.h | 17 -----------------
 1 file changed, 17 deletions(-)
include/linux/blk-mq.h +0 -17
index a28264442948..ad26a41d13f9 100644
@@ -268,23 +268,6 @@ static inline unsigned short req_get_ioprio(struct request *req)
 #define rq_list_next(rq)	(rq)->rq_next
 #define rq_list_empty(list)	((list) == (struct request *) NULL)
 
-/**
- * rq_list_move() - move a struct request from one list to another
- * @src: The source list @rq is currently in
- * @dst: The destination list that @rq will be appended to
- * @rq: The request to move
- * @prev: The request preceding @rq in @src (NULL if @rq is the head)
- */
-static inline void rq_list_move(struct request **src, struct request **dst,
-				struct request *rq, struct request *prev)
-{
-	if (prev)
-		prev->rq_next = rq->rq_next;
-	else
-		*src = rq->rq_next;
-	rq_list_add(dst, rq);
-}
-
 /**
  * enum blk_eh_timer_return - How the timeout handler should proceed
  * @BLK_EH_DONE: The block driver completed the command or will complete it at
Signature · 1 line
2.45.2
author maintainer
#8 lore raw

PATCH 4/6 block: add a rq_list type

In reply to Christoph Hellwig

Replace the semi-open coded request list helpers with a proper rq_list type that mirrors the bio_list and has head and tail pointers. Besides better type safety this actually allows to insert at the tail of the list, which will be useful soon.

---
 block/blk-core.c              |  6 +--
 block/blk-merge.c             |  2 +-
 block/blk-mq.c                | 40 ++++++++--------
 block/blk-mq.h                |  2 +-
 drivers/block/null_blk/main.c |  9 ++--
 drivers/block/virtio_blk.c    | 13 +++---
 drivers/nvme/host/apple.c     |  2 +-
 drivers/nvme/host/pci.c       | 15 +++---
 include/linux/blk-mq.h        | 88 +++++++++++++++++++++--------------
 include/linux/blkdev.h        | 11 +++--
 io_uring/rw.c                 |  4 +-
 11 files changed, 104 insertions(+), 88 deletions(-)
block/blk-core.c +3 -3
index 0387172e8259..666efe8fa202 100644
@@ -1120,8 +1120,8 @@ void blk_start_plug_nr_ios(struct blk_plug *plug, unsigned short nr_ios)
 		return;
 
 	plug->cur_ktime = 0;
-	plug->mq_list = NULL;
-	plug->cached_rq = NULL;
+	rq_list_init(&plug->mq_list);
+	rq_list_init(&plug->cached_rqs);
 	plug->nr_ios = min_t(unsigned short, nr_ios, BLK_MAX_REQUEST_COUNT);
 	plug->rq_count = 0;
 	plug->multiple_queues = false;
@@ -1217,7 +1217,7 @@ void __blk_flush_plug(struct blk_plug *plug, bool from_schedule)
 	 * queue for cached requests, we don't want a blocked task holding
 	 * up a queue freeze/quiesce event.
 	 */
-	if (unlikely(!rq_list_empty(plug->cached_rq)))
+	if (unlikely(!rq_list_empty(&plug->cached_rqs)))
 		blk_mq_free_plug_rqs(plug);
 
 	plug->cur_ktime = 0;
block/blk-merge.c +1 -1
index df36f83f3738..e0b28e9298c9 100644
@@ -1179,7 +1179,7 @@ bool blk_attempt_plug_merge(struct request_queue *q, struct bio *bio,
 	struct blk_plug *plug = current->plug;
 	struct request *rq;
 
-	if (!plug || rq_list_empty(plug->mq_list))
+	if (!plug || rq_list_empty(&plug->mq_list))
 		return false;
 
 	rq_list_for_each(&plug->mq_list, rq) {
block/blk-mq.c +19 -21
index 3c6cadba75e3..ff0b819e35fc 100644
@@ -478,7 +478,7 @@ __blk_mq_alloc_requests_batch(struct blk_mq_alloc_data *data)
 		prefetch(tags->static_rqs[tag]);
 		tag_mask &= ~(1UL << i);
 		rq = blk_mq_rq_ctx_init(data, tags, tag);
-		rq_list_add(data->cached_rq, rq);
+		rq_list_add_head(data->cached_rqs, rq);
 		nr++;
 	}
 	if (!(data->rq_flags & RQF_SCHED_TAGS))
@@ -487,7 +487,7 @@ __blk_mq_alloc_requests_batch(struct blk_mq_alloc_data *data)
 	percpu_ref_get_many(&data->q->q_usage_counter, nr - 1);
 	data->nr_tags -= nr;
 
-	return rq_list_pop(data->cached_rq);
+	return rq_list_pop(data->cached_rqs);
 }
 
 static struct request *__blk_mq_alloc_requests(struct blk_mq_alloc_data *data)
@@ -584,7 +584,7 @@ static struct request *blk_mq_rq_cache_fill(struct request_queue *q,
 		.flags		= flags,
 		.cmd_flags	= opf,
 		.nr_tags	= plug->nr_ios,
-		.cached_rq	= &plug->cached_rq,
+		.cached_rqs	= &plug->cached_rqs,
 	};
 	struct request *rq;
 
@@ -609,14 +609,14 @@ static struct request *blk_mq_alloc_cached_request(struct request_queue *q,
 	if (!plug)
 		return NULL;
 
-	if (rq_list_empty(plug->cached_rq)) {
+	if (rq_list_empty(&plug->cached_rqs)) {
 		if (plug->nr_ios == 1)
 			return NULL;
 		rq = blk_mq_rq_cache_fill(q, plug, opf, flags);
 		if (!rq)
 			return NULL;
 	} else {
-		rq = rq_list_peek(&plug->cached_rq);
+		rq = rq_list_peek(&plug->cached_rqs);
 		if (!rq || rq->q != q)
 			return NULL;
 
@@ -625,7 +625,7 @@ static struct request *blk_mq_alloc_cached_request(struct request_queue *q,
 		if (op_is_flush(rq->cmd_flags) != op_is_flush(opf))
 			return NULL;
 
-		plug->cached_rq = rq_list_next(rq);
+		rq_list_pop(&plug->cached_rqs);
 		blk_mq_rq_time_init(rq, blk_time_get_ns());
 	}
 
@@ -802,7 +802,7 @@ void blk_mq_free_plug_rqs(struct blk_plug *plug)
 {
 	struct request *rq;
 
-	while ((rq = rq_list_pop(&plug->cached_rq)) != NULL)
+	while ((rq = rq_list_pop(&plug->cached_rqs)) != NULL)
 		blk_mq_free_request(rq);
 }
 
@@ -1392,8 +1392,7 @@ static void blk_add_rq_to_plug(struct blk_plug *plug, struct request *rq)
 	 */
 	if (!plug->has_elevator && (rq->rq_flags & RQF_SCHED_TAGS))
 		plug->has_elevator = true;
-	rq->rq_next = NULL;
-	rq_list_add(&plug->mq_list, rq);
+	rq_list_add_head(&plug->mq_list, rq);
 	plug->rq_count++;
 }
 
@@ -2785,7 +2784,7 @@ static void blk_mq_plug_issue_direct(struct blk_plug *plug)
 	blk_status_t ret = BLK_STS_OK;
 
 	while ((rq = rq_list_pop(&plug->mq_list))) {
-		bool last = rq_list_empty(plug->mq_list);
+		bool last = rq_list_empty(&plug->mq_list);
 
 		if (hctx != rq->mq_hctx) {
 			if (hctx) {
@@ -2828,8 +2827,7 @@ static void blk_mq_dispatch_plug_list(struct blk_plug *plug, bool from_sched)
 {
 	struct blk_mq_hw_ctx *this_hctx = NULL;
 	struct blk_mq_ctx *this_ctx = NULL;
-	struct request *requeue_list = NULL;
-	struct request **requeue_lastp = &requeue_list;
+	struct rq_list requeue_list = {};
 	unsigned int depth = 0;
 	bool is_passthrough = false;
 	LIST_HEAD(list);
@@ -2843,12 +2841,12 @@ static void blk_mq_dispatch_plug_list(struct blk_plug *plug, bool from_sched)
 			is_passthrough = blk_rq_is_passthrough(rq);
 		} else if (this_hctx != rq->mq_hctx || this_ctx != rq->mq_ctx ||
 			   is_passthrough != blk_rq_is_passthrough(rq)) {
-			rq_list_add_tail(&requeue_lastp, rq);
+			rq_list_add_tail(&requeue_list, rq);
 			continue;
 		}
 		list_add(&rq->queuelist, &list);
 		depth++;
-	} while (!rq_list_empty(plug->mq_list));
+	} while (!rq_list_empty(&plug->mq_list));
 
 	plug->mq_list = requeue_list;
 	trace_block_unplug(this_hctx->queue, depth, !from_sched);
@@ -2903,19 +2901,19 @@ void blk_mq_flush_plug_list(struct blk_plug *plug, bool from_schedule)
 		if (q->mq_ops->queue_rqs) {
 			blk_mq_run_dispatch_ops(q,
 				__blk_mq_flush_plug_list(q, plug));
-			if (rq_list_empty(plug->mq_list))
+			if (rq_list_empty(&plug->mq_list))
 				return;
 		}
 
 		blk_mq_run_dispatch_ops(q,
 				blk_mq_plug_issue_direct(plug));
-		if (rq_list_empty(plug->mq_list))
+		if (rq_list_empty(&plug->mq_list))
 			return;
 	}
 
 	do {
 		blk_mq_dispatch_plug_list(plug, from_schedule);
-	} while (!rq_list_empty(plug->mq_list));
+	} while (!rq_list_empty(&plug->mq_list));
 }
 
 static void blk_mq_try_issue_list_directly(struct blk_mq_hw_ctx *hctx,
@@ -2980,7 +2978,7 @@ static struct request *blk_mq_get_new_requests(struct request_queue *q,
 	if (plug) {
 		data.nr_tags = plug->nr_ios;
 		plug->nr_ios = 1;
-		data.cached_rq = &plug->cached_rq;
+		data.cached_rqs = &plug->cached_rqs;
 	}
 
 	rq = __blk_mq_alloc_requests(&data);
@@ -3003,7 +3001,7 @@ static struct request *blk_mq_peek_cached_request(struct blk_plug *plug,
 
 	if (!plug)
 		return NULL;
-	rq = rq_list_peek(&plug->cached_rq);
+	rq = rq_list_peek(&plug->cached_rqs);
 	if (!rq || rq->q != q)
 		return NULL;
 	if (type != rq->mq_hctx->type &&
@@ -3017,14 +3015,14 @@ static struct request *blk_mq_peek_cached_request(struct blk_plug *plug,
 static void blk_mq_use_cached_rq(struct request *rq, struct blk_plug *plug,
 		struct bio *bio)
 {
-	WARN_ON_ONCE(rq_list_peek(&plug->cached_rq) != rq);
+	if (rq_list_pop(&plug->cached_rqs) != rq)
+		WARN_ON_ONCE(1);
 
 	/*
 	 * If any qos ->throttle() end up blocking, we will have flushed the
 	 * plug and hence killed the cached_rq list as well. Pop this entry
 	 * before we throttle.
 	 */
-	plug->cached_rq = rq_list_next(rq);
 	rq_qos_throttle(rq->q, bio);
 
 	blk_mq_rq_time_init(rq, blk_time_get_ns());
block/blk-mq.h +1 -1
index f4ac1af77a26..89a20fffa4b1 100644
@@ -155,7 +155,7 @@ struct blk_mq_alloc_data {
 
 	/* allocate multiple requests/tags in one go */
 	unsigned int nr_tags;
-	struct request **cached_rq;
+	struct rq_list *cached_rqs;
 
 	/* input & output parameter */
 	struct blk_mq_ctx *ctx;
drivers/block/null_blk/main.c +4 -5
index 2f0431e42c49..3c3d8d200abb 100644
@@ -1638,10 +1638,9 @@ static blk_status_t null_queue_rq(struct blk_mq_hw_ctx *hctx,
 	return BLK_STS_OK;
 }
 
-static void null_queue_rqs(struct request **rqlist)
+static void null_queue_rqs(struct rq_list *rqlist)
 {
-	struct request *requeue_list = NULL;
-	struct request **requeue_lastp = &requeue_list;
+	struct rq_list requeue_list = {};
 	struct blk_mq_queue_data bd = { };
 	blk_status_t ret;
 
@@ -1651,8 +1650,8 @@ static void null_queue_rqs(struct request **rqlist)
 		bd.rq = rq;
 		ret = null_queue_rq(rq->mq_hctx, &bd);
 		if (ret != BLK_STS_OK)
-			rq_list_add_tail(&requeue_lastp, rq);
-	} while (!rq_list_empty(*rqlist));
+			rq_list_add_tail(&requeue_list, rq);
+	} while (!rq_list_empty(rqlist));
 
 	*rqlist = requeue_list;
 }
drivers/block/virtio_blk.c +6 -7
index b25f7c06a28e..a19f24c19140 100644
@@ -472,7 +472,7 @@ static bool virtblk_prep_rq_batch(struct request *req)
 }
 
 static void virtblk_add_req_batch(struct virtio_blk_vq *vq,
-					struct request **rqlist)
+		struct rq_list *rqlist)
 {
 	struct request *req;
 	unsigned long flags;
@@ -499,11 +499,10 @@ static void virtblk_add_req_batch(struct virtio_blk_vq *vq,
 		virtqueue_notify(vq->vq);
 }
 
-static void virtio_queue_rqs(struct request **rqlist)
+static void virtio_queue_rqs(struct rq_list *rqlist)
 {
-	struct request *submit_list = NULL;
-	struct request *requeue_list = NULL;
-	struct request **requeue_lastp = &requeue_list;
+	struct rq_list submit_list = { };
+	struct rq_list requeue_list = { };
 	struct virtio_blk_vq *vq = NULL;
 	struct request *req;
 
@@ -515,9 +514,9 @@ static void virtio_queue_rqs(struct request **rqlist)
 		vq = this_vq;
 
 		if (virtblk_prep_rq_batch(req))
-			rq_list_add(&submit_list, req); /* reverse order */
+			rq_list_add_head(&submit_list, req); /* reverse order */
 		else
-			rq_list_add_tail(&requeue_lastp, req);
+			rq_list_add_tail(&requeue_list, req);
 	}
 
 	if (vq)
drivers/nvme/host/apple.c +1 -1
index b1387dc459a3..7cd1102a8d2c 100644
@@ -649,7 +649,7 @@ static bool apple_nvme_handle_cq(struct apple_nvme_queue *q, bool force)
 
 	found = apple_nvme_poll_cq(q, &iob);
 
-	if (!rq_list_empty(iob.req_list))
+	if (!rq_list_empty(&iob.req_list))
 		apple_nvme_complete_batch(&iob);
 
 	return found;
drivers/nvme/host/pci.c +7 -8
index c66ea2c23690..c3a00a13946d 100644
@@ -902,7 +902,7 @@ static blk_status_t nvme_queue_rq(struct blk_mq_hw_ctx *hctx,
 	return BLK_STS_OK;
 }
 
-static void nvme_submit_cmds(struct nvme_queue *nvmeq, struct request **rqlist)
+static void nvme_submit_cmds(struct nvme_queue *nvmeq, struct rq_list *rqlist)
 {
 	struct request *req;
 
@@ -930,11 +930,10 @@ static bool nvme_prep_rq_batch(struct nvme_queue *nvmeq, struct request *req)
 	return nvme_prep_rq(nvmeq->dev, req) == BLK_STS_OK;
 }
 
-static void nvme_queue_rqs(struct request **rqlist)
+static void nvme_queue_rqs(struct rq_list *rqlist)
 {
-	struct request *submit_list = NULL;
-	struct request *requeue_list = NULL;
-	struct request **requeue_lastp = &requeue_list;
+	struct rq_list submit_list = { };
+	struct rq_list requeue_list = { };
 	struct nvme_queue *nvmeq = NULL;
 	struct request *req;
 
@@ -944,9 +943,9 @@ static void nvme_queue_rqs(struct request **rqlist)
 		nvmeq = req->mq_hctx->driver_data;
 
 		if (nvme_prep_rq_batch(nvmeq, req))
-			rq_list_add(&submit_list, req); /* reverse order */
+			rq_list_add_head(&submit_list, req); /* reverse order */
 		else
-			rq_list_add_tail(&requeue_lastp, req);
+			rq_list_add_tail(&requeue_list, req);
 	}
 
 	if (nvmeq)
@@ -1078,7 +1077,7 @@ static irqreturn_t nvme_irq(int irq, void *data)
 	DEFINE_IO_COMP_BATCH(iob);
 
 	if (nvme_poll_cq(nvmeq, &iob)) {
-		if (!rq_list_empty(iob.req_list))
+		if (!rq_list_empty(&iob.req_list))
 			nvme_pci_complete_batch(&iob);
 		return IRQ_HANDLED;
 	}
include/linux/blk-mq.h +52 -36

110 lines not on this page · raw message

include/linux/blkdev.h +8 -3
index 65f37ae70712..ce8b65503ff0 100644
@@ -1006,6 +1006,11 @@ extern void blk_put_queue(struct request_queue *);
 void blk_mark_disk_dead(struct gendisk *disk);
 
 #ifdef CONFIG_BLOCK
+struct rq_list {
+	struct request *head;
+	struct request *tail;
+};
+
 /*
  * blk_plug permits building a queue of related requests by holding the I/O
  * fragments for a short period. This allows merging of sequential requests
@@ -1018,10 +1023,10 @@ void blk_mark_disk_dead(struct gendisk *disk);
  * blk_flush_plug() is called.
  */
 struct blk_plug {
-	struct request *mq_list; /* blk-mq requests */
+	struct rq_list mq_list; /* blk-mq requests */
 
 	/* if ios_left is > 1, we can batch tag/rq allocations */
-	struct request *cached_rq;
+	struct rq_list cached_rqs;
 	u64 cur_ktime;
 	unsigned short nr_ios;
 
@@ -1683,7 +1688,7 @@ int bdev_thaw(struct block_device *bdev);
 void bdev_fput(struct file *bdev_file);
 
 struct io_comp_batch {
-	struct request *req_list;
+	struct rq_list req_list;
 	bool need_ts;
 	void (*complete)(struct io_comp_batch *);
 };
io_uring/rw.c +2 -2
index 354c4e175654..9daef985543e 100644
@@ -1160,12 +1160,12 @@ 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) ||
+		if (!rq_list_empty(&iob.req_list) ||
 		    READ_ONCE(req->iopoll_completed))
 			break;
 	}
 
-	if (!rq_list_empty(iob.req_list))
+	if (!rq_list_empty(&iob.req_list))
 		iob.complete(&iob);
 	else if (!pos)
 		return 0;
Signature · 1 line
2.45.2
#9 lore raw

Re: [PATCH 4/6] block: add a rq_list type

Hi Christoph,

Quoted 30 lines · Christoph Hellwig
include/linux/blkdev.h +7 -2
index 65f37ae70712..ce8b65503ff0 100644
@@ -1006,6 +1006,11 @@ extern void blk_put_queue(struct request_queue *);
 void blk_mark_disk_dead(struct gendisk *disk);
 
 #ifdef CONFIG_BLOCK
+struct rq_list {
+	struct request *head;
+	struct request *tail;
+};
+
 /*
  * blk_plug permits building a queue of related requests by holding the I/O
  * fragments for a short period. This allows merging of sequential requests
@@ -1018,10 +1023,10 @@ void blk_mark_disk_dead(struct gendisk *disk);
  * blk_flush_plug() is called.
  */
 struct blk_plug {
-	struct request *mq_list; /* blk-mq requests */
+	struct rq_list mq_list; /* blk-mq requests */
 
 	/* if ios_left is > 1, we can batch tag/rq allocations */
-	struct request *cached_rq;
+	struct rq_list cached_rqs;
 	u64 cur_ktime;
 	unsigned short nr_ios;
 

include/linux/blkdev.h int bdev_thaw(struct block_device *bdev); · line 1691

Quoted 5 lines
include/linux/blkdev.h +1 -1
@@ -1683,7 +1688,7 @@ int bdev_thaw(struct block_device *bdev);
 void bdev_fput(struct file *bdev_file);
 
 struct io_comp_batch {
-	struct request *req_list;
+	struct rq_list req_list;

This change as commit a3396b99990d ("block: add a rq_list type") in next-20241114 causes errors when CONFIG_BLOCK is disabled because the definition of 'struct rq_list' is under CONFIG_BLOCK. Should it be moved out?

include/linux/blkdev.h +1 -1
index 00212e96261a..a1fd0ddce5cf 100644
@@ -1006,12 +1006,12 @@ extern void blk_put_queue(struct request_queue *);
 
 void blk_mark_disk_dead(struct gendisk *disk);
 
-#ifdef CONFIG_BLOCK
 struct rq_list {
 	struct request *head;
 	struct request *tail;
 };
 
+#ifdef CONFIG_BLOCK
 /*
  * blk_plug permits building a queue of related requests by holding the I/O
  * fragments for a short period. This allows merging of sequential requests
Quoted 3 lines
include/linux/blkdev.h +0 -0
 	bool need_ts;
 	void (*complete)(struct io_comp_batch *);
 };
author maintainer Reviewed-by
#10 lore raw

Re: [PATCH 4/6] block: add a rq_list type

Quoted 4 lines · Nathan Chancellor

This change as commit a3396b99990d ("block: add a rq_list type") in next-20241114 causes errors when CONFIG_BLOCK is disabled because the definition of 'struct rq_list' is under CONFIG_BLOCK. Should it be moved out?

Yes, this looks good:

maintainer
#11 lore raw

Re: [PATCH 4/6] block: add a rq_list type

In reply to Nathan Chancellor
Quoted 53 lines · Nathan Chancellor

Hi Christoph,

Quoted 30 lines · Christoph Hellwig
include/linux/blkdev.h +7 -2
index 65f37ae70712..ce8b65503ff0 100644
@@ -1006,6 +1006,11 @@ extern void blk_put_queue(struct request_queue *);
 void blk_mark_disk_dead(struct gendisk *disk);
 
 #ifdef CONFIG_BLOCK
+struct rq_list {
+	struct request *head;
+	struct request *tail;
+};
+
 /*
  * blk_plug permits building a queue of related requests by holding the I/O
  * fragments for a short period. This allows merging of sequential requests
@@ -1018,10 +1023,10 @@ void blk_mark_disk_dead(struct gendisk *disk);
  * blk_flush_plug() is called.
  */
 struct blk_plug {
-	struct request *mq_list; /* blk-mq requests */
+	struct rq_list mq_list; /* blk-mq requests */
 
 	/* if ios_left is > 1, we can batch tag/rq allocations */
-	struct request *cached_rq;
+	struct rq_list cached_rqs;
 	u64 cur_ktime;
 	unsigned short nr_ios;
 

include/linux/blkdev.h int bdev_thaw(struct block_device *bdev); · line 1691

Quoted 5 lines
include/linux/blkdev.h +1 -1
@@ -1683,7 +1688,7 @@ int bdev_thaw(struct block_device *bdev);
 void bdev_fput(struct file *bdev_file);
 
 struct io_comp_batch {
-	struct request *req_list;
+	struct rq_list req_list;

This change as commit a3396b99990d ("block: add a rq_list type") in next-20241114 causes errors when CONFIG_BLOCK is disabled because the definition of 'struct rq_list' is under CONFIG_BLOCK. Should it be moved out?

include/linux/blkdev.h +0 -0
index 00212e96261a..a1fd0ddce5cf 100644
@@ -1006,12 +1006,12 @@ extern void blk_put_queue(struct request_queue *);
 
 void blk_mark_disk_dead(struct gendisk *disk);
 

include/linux/blkdev.h extern void blk_put_queue(struct request_queue *); · line 1017

Quoted 10 lines
include/linux/blkdev.h +1 -1
-#ifdef CONFIG_BLOCK
 struct rq_list {
 	struct request *head;
 	struct request *tail;
 };
 
+#ifdef CONFIG_BLOCK
 /*
  * blk_plug permits building a queue of related requests by holding the I/O
  * fragments for a short period. This allows merging of sequential requests

Fix looks fine, but I can't apply a patch that hasn't been signed off. Please send one, or I'll just have to sort it out manually as we're really close to this code shipping.

Signature · 1 line
Jens Axboe
maintainer
#12 lore raw

Re: [PATCH 4/6] block: add a rq_list type

Quoted 59 lines · Jens Axboe
Quoted 58 lines · Nathan Chancellor

Hi Christoph,

Quoted 30 lines · Christoph Hellwig
include/linux/blkdev.h +7 -2
index 65f37ae70712..ce8b65503ff0 100644
@@ -1006,6 +1006,11 @@ extern void blk_put_queue(struct request_queue *);
 void blk_mark_disk_dead(struct gendisk *disk);
 
 #ifdef CONFIG_BLOCK
+struct rq_list {
+	struct request *head;
+	struct request *tail;
+};
+
 /*
  * blk_plug permits building a queue of related requests by holding the I/O
  * fragments for a short period. This allows merging of sequential requests
@@ -1018,10 +1023,10 @@ void blk_mark_disk_dead(struct gendisk *disk);
  * blk_flush_plug() is called.
  */
 struct blk_plug {
-	struct request *mq_list; /* blk-mq requests */
+	struct rq_list mq_list; /* blk-mq requests */
 
 	/* if ios_left is > 1, we can batch tag/rq allocations */
-	struct request *cached_rq;
+	struct rq_list cached_rqs;
 	u64 cur_ktime;
 	unsigned short nr_ios;
 

include/linux/blkdev.h int bdev_thaw(struct block_device *bdev); · line 1691

Quoted 5 lines
include/linux/blkdev.h +1 -1
@@ -1683,7 +1688,7 @@ int bdev_thaw(struct block_device *bdev);
 void bdev_fput(struct file *bdev_file);
 
 struct io_comp_batch {
-	struct request *req_list;
+	struct rq_list req_list;

This change as commit a3396b99990d ("block: add a rq_list type") in next-20241114 causes errors when CONFIG_BLOCK is disabled because the definition of 'struct rq_list' is under CONFIG_BLOCK. Should it be moved out?

include/linux/blkdev.h +0 -1
index 00212e96261a..a1fd0ddce5cf 100644
@@ -1006,12 +1006,12 @@ extern void blk_put_queue(struct request_queue *);
 
 void blk_mark_disk_dead(struct gendisk *disk);
 
-#ifdef CONFIG_BLOCK
 struct rq_list {
 	struct request *head;
 	struct request *tail;
 };
 
Quoted 9 lines

include/linux/blkdev.h extern void blk_put_queue(struct request_queue *); · line 1017

Quoted 5 lines
include/linux/blkdev.h +1 -0
+#ifdef CONFIG_BLOCK
 /*
  * blk_plug permits building a queue of related requests by holding the I/O
  * fragments for a short period. This allows merging of sequential requests

Fix looks fine, but I can't apply a patch that hasn't been signed off. Please send one, or I'll just have to sort it out manually as we're really close to this code shipping.

I fixed it up myself, it's too close to me sending out 6.13 changes to let it linger:

https://git.kernel.dk/cgit/linux/commit/?h=for-6.13/block&id=957860cbc1dc89f79f2acc193470224e350dfd03

Signature · 1 line
Jens Axboe
#13 lore raw

Re: [PATCH 4/6] block: add a rq_list type

Quoted 9 lines · Jens Axboe
Quoted 3 lines · Jens Axboe

Fix looks fine, but I can't apply a patch that hasn't been signed off. Please send one, or I'll just have to sort it out manually as we're really close to this code shipping.

I fixed it up myself, it's too close to me sending out 6.13 changes to let it linger:

https://git.kernel.dk/cgit/linux/commit/?h=for-6.13/block&id=957860cbc1dc89f79f2acc193470224e350dfd03

Thanks, I was going to send a patch amidst my travels today but by the time I got to it, I saw you had already tackled it on your branch.

Cheers,
Nathan

author maintainer
#14 lore raw

PATCH 5/6 block: don't reorder requests in blk_add_rq_to_plug

In reply to Christoph Hellwig

Add requests to the tail of the list instead of the front so that they are queued up in submission order.

Remove the re-reordering in blk_mq_dispatch_plug_list, virtio_queue_rqs and nvme_queue_rqs now that the list is ordered as expected.

---
 block/blk-mq.c             | 4 ++--
 drivers/block/virtio_blk.c | 2 +-
 drivers/nvme/host/pci.c    | 2 +-
 3 files changed, 4 insertions(+), 4 deletions(-)
block/blk-mq.c +2 -2
index ff0b819e35fc..270cfd9fc6b0 100644
@@ -1392,7 +1392,7 @@ static void blk_add_rq_to_plug(struct blk_plug *plug, struct request *rq)
 	 */
 	if (!plug->has_elevator && (rq->rq_flags & RQF_SCHED_TAGS))
 		plug->has_elevator = true;
-	rq_list_add_head(&plug->mq_list, rq);
+	rq_list_add_tail(&plug->mq_list, rq);
 	plug->rq_count++;
 }
 
@@ -2844,7 +2844,7 @@ static void blk_mq_dispatch_plug_list(struct blk_plug *plug, bool from_sched)
 			rq_list_add_tail(&requeue_list, rq);
 			continue;
 		}
-		list_add(&rq->queuelist, &list);
+		list_add_tail(&rq->queuelist, &list);
 		depth++;
 	} while (!rq_list_empty(&plug->mq_list));
 
drivers/block/virtio_blk.c +1 -1
index a19f24c19140..c0cdba71f436 100644
@@ -514,7 +514,7 @@ static void virtio_queue_rqs(struct rq_list *rqlist)
 		vq = this_vq;
 
 		if (virtblk_prep_rq_batch(req))
-			rq_list_add_head(&submit_list, req); /* reverse order */
+			rq_list_add_tail(&submit_list, req);
 		else
 			rq_list_add_tail(&requeue_list, req);
 	}
drivers/nvme/host/pci.c +1 -1
index c3a00a13946d..39ba1d0c287d 100644
@@ -943,7 +943,7 @@ static void nvme_queue_rqs(struct rq_list *rqlist)
 		nvmeq = req->mq_hctx->driver_data;
 
 		if (nvme_prep_rq_batch(nvmeq, req))
-			rq_list_add_head(&submit_list, req); /* reverse order */
+			rq_list_add_tail(&submit_list, req);
 		else
 			rq_list_add_tail(&requeue_list, req);
 	}
Signature · 1 line
2.45.2
author maintainer
#15 lore raw

PATCH 6/6 block: don't reorder requests in blk_mq_add_to_batch

In reply to Christoph Hellwig

LIFO ordering for batched completions is a bit unexpected and also defeats some merging optimizations in e.g. the XFS buffered write code. Now that we can easily add the request to the tail of the list do that.

---
 include/linux/blk-mq.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
include/linux/blk-mq.h +1 -1
index c61e04365677..c596e0e4cb75 100644
@@ -884,7 +884,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);
-	rq_list_add_head(&iob->req_list, req);
+	rq_list_add_tail(&iob->req_list, req);
 	return true;
 }
 
Signature · 1 line
2.45.2
#16 lore raw
In reply to Christoph Hellwig
Quoted 7 lines · Christoph Hellwig

currently blk-mq reorders requests when adding them to the plug because the request list can't do efficient tail appends. When the plug is directly issued using ->queue_rqs that means reordered requests are passed to the driver, which can lead to very bad I/O patterns when not corrected, especially on rotational devices (e.g. NVMe HDD) or when using zone append.

Quoted 6 lines

This series first adds two easily backportable workarounds to reverse the reording in the virtio_blk and nvme-pci ->queue_rq implementations similar to what the non-queue_rqs path does, and then adds a rq_list type that allows for efficient tail insertions and uses that to fix the reordering for real and then does the same for I/O completions as well.

Hi Christoph,

Could something like the patch below replace this patch series? I don't have a strong opinion about which approach to select. I'm sharing this patch because this is what I came up while looking into
how to support QD>1 for zoned devices with a storage controller that
preserves the request order (UFS).

Thanks,

Bart.

block: Make the plugging mechanism preserve the request order

Requests are added to the front of the plug list and dispatching happens in list order. Hence, dispatching happens in reverse order. Dispatch in order by reversing the plug list before dispatching. This patch is a modified version of a patch from Jens
(https://lore.kernel.org/linux-block/[email protected]/).

block/blk-mq.c +18 -0
index 3533bd808072..bf2ea421b2e8 100644
@@ -2868,6 +2868,22 @@ static void blk_mq_dispatch_plug_list(struct blk_plug *plug, bool from_sched)
 	percpu_ref_put(&this_hctx->queue->q_usage_counter);
 }
 
+/* See also llist_reverse_order(). */
+static void blk_plug_reverse_order(struct blk_plug *plug)
+{
+	struct request *rq = plug->mq_list, *new_head = NULL;
+
+	while (rq) {
+		struct request *tmp = rq;
+
+		rq = rq->rq_next;
+		tmp->rq_next = new_head;
+		new_head = tmp;
+	}
+
+	plug->mq_list = new_head;
+}
+
 void blk_mq_flush_plug_list(struct blk_plug *plug, bool from_schedule)
 {
 	struct request *rq;
@@ -2885,6 +2901,8 @@ void blk_mq_flush_plug_list(struct blk_plug *plug, bool from_schedule)
 	depth = plug->rq_count;
 	plug->rq_count = 0;
 
+	blk_plug_reverse_order(plug);
+
 	if (!plug->multiple_queues && !plug->has_elevator && !from_schedule) {
 		struct request_queue *q;
 
maintainer
#17 lore raw
Quoted 16 lines · Bart Van Assche
Quoted 7 lines · Christoph Hellwig

currently blk-mq reorders requests when adding them to the plug because the request list can't do efficient tail appends. When the plug is directly issued using ->queue_rqs that means reordered requests are passed to the driver, which can lead to very bad I/O patterns when not corrected, especially on rotational devices (e.g. NVMe HDD) or when using zone append.

Quoted 6 lines

This series first adds two easily backportable workarounds to reverse the reording in the virtio_blk and nvme-pci ->queue_rq implementations similar to what the non-queue_rqs path does, and then adds a rq_list type that allows for efficient tail insertions and uses that to fix the reordering for real and then does the same for I/O completions as well.

Quoted 4 lines

Hi Christoph,

Could something like the patch below replace this patch series? I don't have a strong opinion about which approach to select.

I mean it obviously could, but it'd be a terrible way to go as we're now iterating the full list just to reverse it...

Signature · 1 line
Jens Axboe
maintainer Applied
#18 lore raw
In reply to Christoph Hellwig
Quoted 8 lines · Christoph Hellwig

currently blk-mq reorders requests when adding them to the plug because the request list can't do efficient tail appends. When the plug is directly issued using ->queue_rqs that means reordered requests are passed to the driver, which can lead to very bad I/O patterns when not corrected, especially on rotational devices (e.g. NVMe HDD) or when using zone append.

[...]

Applied, thanks!

[1/6] nvme-pci: reverse request order in nvme_queue_rqs

      commit: beadf0088501d9dcf2454b05d90d5d31ea3ba55f
[2/6] virtio_blk: reverse request order in virtio_queue_rqs
      commit: a982a5637e91fd4491da4bb8eb79cde3fed2300e
[3/6] block: remove rq_list_move
      commit: dd53d238d4ca7ff1a0e10eb0205e842560f1394d
[4/6] block: add a rq_list type
      commit: dc56e2a78ba20c472c419c6f2ed8fb7d0c95ad90
[5/6] block: don't reorder requests in blk_add_rq_to_plug
      commit: 05634083a304eb3d4b5733fe8d6e2fd0c1041ca8
[6/6] block: don't reorder requests in blk_mq_add_to_batch
      commit: 65550dea43815d01f45d7ece38e458855d5e2962

Best regards,

Signature · 1 line
Jens Axboe
Reviewed-by
#19 lore raw
In reply to Christoph Hellwig
Quoted 9 lines · Christoph Hellwig

Hi Jens,

currently blk-mq reorders requests when adding them to the plug because the request list can't do efficient tail appends. When the plug is directly issued using ->queue_rqs that means reordered requests are passed to the driver, which can lead to very bad I/O patterns when not corrected, especially on rotational devices (e.g. NVMe HDD) or when using zone append.

Quoted 6 lines

This series first adds two easily backportable workarounds to reverse the reording in the virtio_blk and nvme-pci ->queue_rq implementations similar to what the non-queue_rqs path does, and then adds a rq_list type that allows for efficient tail insertions and uses that to fix the reordering for real and then does the same for I/O completions as well.

Looks good to me. I ran the quick performance numbers [1].

-ck

fio randread iouring workload :-

IOPS :-
-------
nvme-orig:           Average IOPS: 72,690
nvme-new-no-reorder: Average IOPS: 72,580

BW :-
-------
nvme-orig:           Average BW: 283.9 MiB/s
nvme-new-no-reorder: Average BW: 283.4 MiB/s

IOPS/BW :-

nvme-orig-10.fio:  read: IOPS=72.9k, BW=285MiB/s (299MB/s)(16.7GiB/60004msec)
nvme-orig-1.fio:  read: IOPS=72.7k, BW=284MiB/s (298MB/s)(16.6GiB/60004msec) nvme-orig-2.fio:  read: IOPS=73.0k, BW=285MiB/s (299MB/s)(16.7GiB/60004msec) nvme-orig-3.fio:  read: IOPS=73.3k, BW=286MiB/s (300MB/s)(16.8GiB/60003msec) nvme-orig-4.fio:  read: IOPS=72.5k, BW=283MiB/s (297MB/s)(16.6GiB/60003msec) nvme-orig-5.fio:  read: IOPS=72.4k, BW=283MiB/s (297MB/s)(16.6GiB/60004msec) nvme-orig-6.fio:  read: IOPS=72.9k, BW=285MiB/s (299MB/s)(16.7GiB/60003msec) nvme-orig-7.fio:  read: IOPS=72.3k, BW=282MiB/s (296MB/s)(16.5GiB/60004msec) nvme-orig-8.fio:  read: IOPS=72.4k, BW=283MiB/s (296MB/s)(16.6GiB/60003msec) nvme-orig-9.fio:  read: IOPS=72.5k, BW=283MiB/s (297MB/s)(16.6GiB/60004msec) nvme (nvme-6.13) #
nvme (nvme-6.13) # grep BW nvme-new-no-reorder-*fio nvme-new-no-reorder-10.fio:  read: IOPS=72.5k, BW=283MiB/s (297MB/s)(16.6GiB/60004msec)
nvme-new-no-reorder-1.fio:  read: IOPS=72.5k, BW=283MiB/s (297MB/s)(16.6GiB/60004msec)
nvme-new-no-reorder-2.fio:  read: IOPS=72.5k, BW=283MiB/s (297MB/s)(16.6GiB/60003msec)
nvme-new-no-reorder-3.fio:  read: IOPS=71.7k, BW=280MiB/s (294MB/s)(16.4GiB/60005msec)
nvme-new-no-reorder-4.fio:  read: IOPS=72.5k, BW=283MiB/s (297MB/s)(16.6GiB/60004msec)
nvme-new-no-reorder-5.fio:  read: IOPS=72.6k, BW=284MiB/s (298MB/s)(16.6GiB/60003msec)
nvme-new-no-reorder-6.fio:  read: IOPS=73.3k, BW=286MiB/s (300MB/s)(16.8GiB/60003msec)
nvme-new-no-reorder-7.fio:  read: IOPS=72.8k, BW=284MiB/s (298MB/s)(16.7GiB/60003msec)
nvme-new-no-reorder-8.fio:  read: IOPS=73.2k, BW=286MiB/s (300MB/s)(16.7GiB/60004msec)
nvme-new-no-reorder-9.fio:  read: IOPS=72.2k, BW=282MiB/s (296MB/s)(16.5GiB/60005msec)

maintainer
#20 lore raw
Quoted 27 lines · Chaitanya Kulkarni
Quoted 9 lines · Christoph Hellwig

Hi Jens,

currently blk-mq reorders requests when adding them to the plug because the request list can't do efficient tail appends. When the plug is directly issued using ->queue_rqs that means reordered requests are passed to the driver, which can lead to very bad I/O patterns when not corrected, especially on rotational devices (e.g. NVMe HDD) or when using zone append.

Quoted 6 lines

This series first adds two easily backportable workarounds to reverse the reording in the virtio_blk and nvme-pci ->queue_rq implementations similar to what the non-queue_rqs path does, and then adds a rq_list type that allows for efficient tail insertions and uses that to fix the reordering for real and then does the same for I/O completions as well.

Looks good to me. I ran the quick performance numbers [1].

-ck

fio randread iouring workload :-

IOPS :-
-------

Quoted 7 lines
nvme-orig:           Average IOPS: 72,690
nvme-new-no-reorder: Average IOPS: 72,580

BW :-
-------

nvme-orig:           Average BW: 283.9 MiB/s
nvme-new-no-reorder: Average BW: 283.4 MiB/s

Thanks for testing, but you can't verify any kind of perf change with that kind of setup. I'll be willing to bet that it'll be 1-2% drop at higher rates, which is substantial. But the reordering is a problem, not just for zoned devices, which is why I chose to merge this.

Signature · 1 line
Jens Axboe
#21 lore raw
Quoted 17 lines · Jens Axboe
Quoted 10 lines

Looks good to me. I ran the quick performance numbers [1].

-ck

fio randread iouring workload :-

IOPS :-
-------

Quoted 7 lines
nvme-orig:           Average IOPS: 72,690
nvme-new-no-reorder: Average IOPS: 72,580

BW :-
-------

nvme-orig:           Average BW: 283.9 MiB/s
nvme-new-no-reorder: Average BW: 283.4 MiB/s
Quoted 6 lines

Thanks for testing, but you can't verify any kind of perf change with that kind of setup. I'll be willing to bet that it'll be 1-2% drop at higher rates, which is substantial. But the reordering is a problem, not just for zoned devices, which is why I chose to merge this.

-- Jens Axboe

Agree with you. My intention was to test it, since it was touching NVMe, I thought sharing results will help either way with io_uring? but no intention to stop this patchset and make an argument against it (if at all) for potential drop :).

-ck

maintainer
#22 lore raw
Quoted 23 lines · Chaitanya Kulkarni
Quoted 22 lines · Jens Axboe
Quoted 10 lines

Looks good to me. I ran the quick performance numbers [1].

-ck

fio randread iouring workload :-

IOPS :-
-------

Quoted 7 lines
nvme-orig:           Average IOPS: 72,690
nvme-new-no-reorder: Average IOPS: 72,580

BW :-
-------

nvme-orig:           Average BW: 283.9 MiB/s
nvme-new-no-reorder: Average BW: 283.4 MiB/s

Thanks for testing, but you can't verify any kind of perf change with that kind of setup. I'll be willing to bet that it'll be 1-2% drop at higher rates, which is substantial. But the reordering is a problem, not just for zoned devices, which is why I chose to merge this.

Quoted 6 lines
Quoted 1 line

-- Jens Axboe

Agree with you. My intention was to test it, since it was touching NVMe, I thought sharing results will help either way with io_uring? but no intention to stop this patchset and make an argument against it (if at all) for potential drop :).

Oh all good, and like I said, the testing is appreciated! The functional testing is definitely useful.

Signature · 1 line
Jens Axboe
author maintainer
#23 lore raw
In reply to Jens Axboe
Quoted 4 lines · Jens Axboe

Thanks for testing, but you can't verify any kind of perf change with that kind of setup. I'll be willing to bet that it'll be 1-2% drop at higher rates, which is substantial. But the reordering is a problem, not just for zoned devices, which is why I chose to merge this.

So I did not see any variation, but I also don't have access to a really beefy setup right now. If there is a degradation it probably is that touching rq_next for each request actually has an effect if the list is big enough and they aren't cache hot any more. I can cook up a patch that goes back to the scheme currently used upstream in nvme and virtio that just cuts of the list at a hctx change instead of moving the requests one by one now that the block layer doesn't mess up the order.

maintainer
#24 lore raw
Quoted 10 lines · Christoph Hellwig
Quoted 4 lines · Jens Axboe

Thanks for testing, but you can't verify any kind of perf change with that kind of setup. I'll be willing to bet that it'll be 1-2% drop at higher rates, which is substantial. But the reordering is a problem, not just for zoned devices, which is why I chose to merge this.

So I did not see any variation, but I also don't have access to a really beefy setup right now. If there is a degradation it probably is that touching rq_next for each request actually has an effect if the list is big enough and they aren't cache hot any more. I can cook up a patch

Exactly.

Quoted 3 lines

that goes back to the scheme currently used upstream in nvme and virtio that just cuts of the list at a hctx change instead of moving the requests one by one now that the block layer doesn't mess up the order.

I think that would be useful. I can test.

Signature · 1 line
Jens Axboe
author maintainer
#25 lore raw
Quoted 5 lines · Jens Axboe
Quoted 3 lines

that goes back to the scheme currently used upstream in nvme and virtio that just cuts of the list at a hctx change instead of moving the requests one by one now that the block layer doesn't mess up the order.

I think that would be useful. I can test.

I played with this a bit, and I really hate keeping prev for the unlinking. So this version uses a slightly different approach:

‐ a first pass that just rolls through all requests and does the prep
checks. If they fail that is recorded in a new field in the iod (could also become a request flag)
- the submission loop inside sq_lock becomes a bit more complex as it handles breaking out of the inner loop for hctx changes, and to move the previously failed commands to the resubmit list.

But this completely gets rid of the prev tracking and all list manipulation except for the final list_pop.

drivers/nvme/host/pci.c +28 -29
index 5f2e3ad2cc52..3470dae73a8c 100644
@@ -233,6 +233,7 @@ struct nvme_iod {
 	struct nvme_request req;
 	struct nvme_command cmd;
 	bool aborted;
+	bool batch_failed;
 	s8 nr_allocations;	/* PRP list pool allocations. 0 means small
 				   pool in use */
 	unsigned int dma_len;	/* length of single DMA segment mapping */
@@ -843,6 +844,7 @@ static blk_status_t nvme_prep_rq(struct nvme_dev *dev, struct request *req)
 	blk_status_t ret;
 
 	iod->aborted = false;
+	iod->batch_failed = false;
 	iod->nr_allocations = -1;
 	iod->sgt.nents = 0;
 
@@ -904,54 +906,51 @@ static blk_status_t nvme_queue_rq(struct blk_mq_hw_ctx *hctx,
 	return BLK_STS_OK;
 }
 
-static void nvme_submit_cmds(struct nvme_queue *nvmeq, struct rq_list *rqlist)
+static void nvme_submit_cmds(struct rq_list *rqlist,
+		struct rq_list *requeue_list)
 {
-	struct request *req;
+	struct request *req = rq_list_peek(rqlist);
+	struct nvme_queue *nvmeq = req->mq_hctx->driver_data;
 
 	spin_lock(&nvmeq->sq_lock);
 	while ((req = rq_list_pop(rqlist))) {
 		struct nvme_iod *iod = blk_mq_rq_to_pdu(req);
 
-		nvme_sq_copy_cmd(nvmeq, &iod->cmd);
+		if (iod->batch_failed)
+			rq_list_add_tail(requeue_list, req);
+		else
+			nvme_sq_copy_cmd(nvmeq, &iod->cmd);
+
+		if (!req->rq_next || req->mq_hctx != req->rq_next->mq_hctx)
+			break;
 	}
 	nvme_write_sq_db(nvmeq, true);
 	spin_unlock(&nvmeq->sq_lock);
 }
 
-static bool nvme_prep_rq_batch(struct nvme_queue *nvmeq, struct request *req)
-{
-	/*
-	 * We should not need to do this, but we're still using this to
-	 * ensure we can drain requests on a dying queue.
-	 */
-	if (unlikely(!test_bit(NVMEQ_ENABLED, &nvmeq->flags)))
-		return false;
-	if (unlikely(!nvme_check_ready(&nvmeq->dev->ctrl, req, true)))
-		return false;
-
-	return nvme_prep_rq(nvmeq->dev, req) == BLK_STS_OK;
-}
-
 static void nvme_queue_rqs(struct rq_list *rqlist)
 {
-	struct rq_list submit_list = { };
 	struct rq_list requeue_list = { };
-	struct nvme_queue *nvmeq = NULL;
 	struct request *req;
 
-	while ((req = rq_list_pop(rqlist))) {
-		if (nvmeq && nvmeq != req->mq_hctx->driver_data)
-			nvme_submit_cmds(nvmeq, &submit_list);
-		nvmeq = req->mq_hctx->driver_data;
+	rq_list_for_each(rqlist, req) {
+		struct nvme_queue *nvmeq = req->mq_hctx->driver_data;
+		struct nvme_iod *iod = blk_mq_rq_to_pdu(req);
 
-		if (nvme_prep_rq_batch(nvmeq, req))
-			rq_list_add_tail(&submit_list, req);
-		else
-			rq_list_add_tail(&requeue_list, req);
+		/*
+		 * We should not need to do this, but we're still using this to
+		 * ensure we can drain requests on a dying queue.
+		 */
+		if (unlikely(!test_bit(NVMEQ_ENABLED, &nvmeq->flags)) ||
+		    unlikely(!nvme_check_ready(&nvmeq->dev->ctrl, req, true)) ||
+		    unlikely(nvme_prep_rq(nvmeq->dev, req) != BLK_STS_OK))
+			iod->batch_failed = true;
 	}
 
-	if (nvmeq)
-		nvme_submit_cmds(nvmeq, &submit_list);
+	do {
+		nvme_submit_cmds(rqlist, &requeue_list);
+	} while (!rq_list_empty(rqlist));
+
 	*rqlist = requeue_list;
 }