Skip to contentSkip to conversation

virtio-blk: set req->state to MQ_RQ_COMPLETE after polling I/O is finished

PATCH Superseded Suwan Kim 4 participants 8 messages Dec 6, 2022 – Dec 12, 2022

Subsystems (3)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Suwan Kim

author
#0 lore raw

PATCH 1/2 virtio-blk: set req->state to MQ_RQ_COMPLETE after polling I/O is finished

Driver should set req->state to MQ_RQ_COMPLETE after it finishes to process req. But virtio-blk doesn't set MQ_RQ_COMPLETE after virtblk_poll() handles req and req->state still remains MQ_RQ_IN_FLIGHT. Fortunately so far there is no issue about it because blk_mq_end_request_batch() sets req->state to MQ_RQ_IDLE. This patch properly sets req->state after polling I/O is finished.

Fixes 4e0400525691 ("virtio-blk: support polling I/O")
---
 drivers/block/virtio_blk.c | 1 +
 1 file changed, 1 insertion(+)
drivers/block/virtio_blk.c +1 -0
index 19da5defd734..cf64d256787e 100644
@@ -839,6 +839,7 @@ static void virtblk_complete_batch(struct io_comp_batch *iob)
 	rq_list_for_each(&iob->req_list, req) {
 		virtblk_unmap_data(req, blk_mq_rq_to_pdu(req));
 		virtblk_cleanup_cmd(req);
+		blk_mq_set_request_complete(req);
 	}
 	blk_mq_end_request_batch(iob);
 }
Signature · 1 line
2.26.3
author
#1 lore raw

PATCH 2/2 virtio-blk: support completion batching for the IRQ path

This patch adds completion batching to the IRQ path. It reuses batch completion code of virtblk_poll(). It collects requests to io_comp_batch and processes them all at once. It can boost up the performance by 2%.

To validate the performance improvement and stabilty, I did fio test with 4 vCPU VM and 12 vCPU VM respectively. Both VMs have 8GB ram and the same number of HW queues as vCPU.
The fio cammad is as follows and I ran the fio 5 times and got IOPS average. (io_uring, randread, direct=1, bs=512, iodepth=64 numjobs=2,4)

Test result shows about 2% improvement.

4 vcpu VM | numjobs=2 | numjobs=4
-----------------------------------------------------------
fio without patch | 367.2K IOPS | 397.6K IOPS
-----------------------------------------------------------
fio with patch | 372.8K IOPS | 407.7K IOPS

12 vcpu VM | numjobs=2 | numjobs=4
-----------------------------------------------------------
fio without patch | 363.6K IOPS | 374.8K IOPS
-----------------------------------------------------------
fio with patch | 373.8K IOPS | 385.3K IOPS

---
 drivers/block/virtio_blk.c | 38 +++++++++++++++++++++++---------------
 1 file changed, 23 insertions(+), 15 deletions(-)
drivers/block/virtio_blk.c +23 -15
index cf64d256787e..48fcf745f007 100644
@@ -272,6 +272,18 @@ static inline void virtblk_request_done(struct request *req)
 	blk_mq_end_request(req, virtblk_result(vbr));
 }
 
+static void virtblk_complete_batch(struct io_comp_batch *iob)
+{
+	struct request *req;
+
+	rq_list_for_each(&iob->req_list, req) {
+		virtblk_unmap_data(req, blk_mq_rq_to_pdu(req));
+		virtblk_cleanup_cmd(req);
+		blk_mq_set_request_complete(req);
+	}
+	blk_mq_end_request_batch(iob);
+}
+
 static void virtblk_done(struct virtqueue *vq)
 {
 	struct virtio_blk *vblk = vq->vdev->priv;
@@ -280,6 +292,7 @@ static void virtblk_done(struct virtqueue *vq)
 	struct virtblk_req *vbr;
 	unsigned long flags;
 	unsigned int len;
+	DEFINE_IO_COMP_BATCH(iob);
 
 	spin_lock_irqsave(&vblk->vqs[qid].lock, flags);
 	do {
@@ -287,7 +300,9 @@ static void virtblk_done(struct virtqueue *vq)
 		while ((vbr = virtqueue_get_buf(vblk->vqs[qid].vq, &len)) != NULL) {
 			struct request *req = blk_mq_rq_from_pdu(vbr);
 
-			if (likely(!blk_should_fake_timeout(req->q)))
+			if (likely(!blk_should_fake_timeout(req->q)) &&
+				!blk_mq_add_to_batch(req, &iob, vbr->status,
+							virtblk_complete_batch))
 				blk_mq_complete_request(req);
 			req_done = true;
 		}
@@ -295,9 +310,14 @@ static void virtblk_done(struct virtqueue *vq)
 			break;
 	} while (!virtqueue_enable_cb(vq));
 
-	/* In case queue is stopped waiting for more buffers. */
-	if (req_done)
+	if (req_done) {
+		if (!rq_list_empty(iob.req_list))
+			virtblk_complete_batch(&iob);
+
+		/* In case queue is stopped waiting for more buffers. */
 		blk_mq_start_stopped_hw_queues(vblk->disk->queue, true);
+	}
+
 	spin_unlock_irqrestore(&vblk->vqs[qid].lock, flags);
 }
 
@@ -832,18 +852,6 @@ static void virtblk_map_queues(struct blk_mq_tag_set *set)
 	}
 }
 
-static void virtblk_complete_batch(struct io_comp_batch *iob)
-{
-	struct request *req;
-
-	rq_list_for_each(&iob->req_list, req) {
-		virtblk_unmap_data(req, blk_mq_rq_to_pdu(req));
-		virtblk_cleanup_cmd(req);
-		blk_mq_set_request_complete(req);
-	}
-	blk_mq_end_request_batch(iob);
-}
-
 static int virtblk_poll(struct blk_mq_hw_ctx *hctx, struct io_comp_batch *iob)
 {
 	struct virtio_blk *vblk = hctx->queue->queuedata;
Signature · 1 line
2.26.3
#4 lore raw
In reply to Suwan Kim
Quoted 17 lines · Suwan Kim

Driver should set req->state to MQ_RQ_COMPLETE after it finishes to process req. But virtio-blk doesn't set MQ_RQ_COMPLETE after virtblk_poll() handles req and req->state still remains MQ_RQ_IN_FLIGHT. Fortunately so far there is no issue about it because blk_mq_end_request_batch() sets req->state to MQ_RQ_IDLE. This patch properly sets req->state after polling I/O is finished.

Fixes 4e0400525691 ("virtio-blk: support polling I/O")
---
 drivers/block/virtio_blk.c | 1 +
 1 file changed, 1 insertion(+)
drivers/block/virtio_blk.c +0 -0
index 19da5defd734..cf64d256787e 100644

drivers/block/virtio_blk.c static void virtblk_complete_batch(struct io_comp_batch *iob) · line 845

Quoted 7 lines
drivers/block/virtio_blk.c +1 -0
@@ -839,6 +839,7 @@ static void virtblk_complete_batch(struct io_comp_batch *iob)
 	rq_list_for_each(&iob->req_list, req) {
 		virtblk_unmap_data(req, blk_mq_rq_to_pdu(req));
 		virtblk_cleanup_cmd(req);
+		blk_mq_set_request_complete(req);
 	}
 	blk_mq_end_request_batch(iob);
 }

The doc comment for blk_mq_set_request_complete() mentions this being used in ->queue_rq(), but that's not the case here. Does the doc comment need to be updated if we're using the function in a different way?

I'm not familiar enough with the Linux block APIs, but this feels weird to me. Shouldn't blk_mq_end_request_batch(iob) take care of this for us? Why does it set the state to IDLE instead of COMPLETE?

I think Jens can confirm whether we really want all drivers that use polling and io_comp_batch to manually call blk_mq_set_request_complete().

Thanks,
Stefan

_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization

reviewer Reviewed-by on v1 2/2
#2 lore raw

Re: [PATCH 2/2] virtio-blk: support completion batching for the IRQ path

In reply to Suwan Kim
Quoted 24 lines · Suwan Kim

This patch adds completion batching to the IRQ path. It reuses batch completion code of virtblk_poll(). It collects requests to io_comp_batch and processes them all at once. It can boost up the performance by 2%.

To validate the performance improvement and stabilty, I did fio test with 4 vCPU VM and 12 vCPU VM respectively. Both VMs have 8GB ram and the same number of HW queues as vCPU.
The fio cammad is as follows and I ran the fio 5 times and got IOPS average. (io_uring, randread, direct=1, bs=512, iodepth=64 numjobs=2,4)

Test result shows about 2% improvement.

4 vcpu VM | numjobs=2 | numjobs=4
-----------------------------------------------------------
fio without patch | 367.2K IOPS | 397.6K IOPS
-----------------------------------------------------------
fio with patch | 372.8K IOPS | 407.7K IOPS

12 vcpu VM | numjobs=2 | numjobs=4
-----------------------------------------------------------
fio without patch | 363.6K IOPS | 374.8K IOPS
-----------------------------------------------------------
fio with patch | 373.8K IOPS | 385.3K IOPS

Quoted 4 lines
---
 drivers/block/virtio_blk.c | 38 +++++++++++++++++++++++---------------
 1 file changed, 23 insertions(+), 15 deletions(-)

Cool, thanks for doing this!

Quoted 43 lines
drivers/block/virtio_blk.c +16 -1
index cf64d256787e..48fcf745f007 100644
@@ -272,6 +272,18 @@ static inline void virtblk_request_done(struct request *req)
 	blk_mq_end_request(req, virtblk_result(vbr));
 }
 
+static void virtblk_complete_batch(struct io_comp_batch *iob)
+{
+	struct request *req;
+
+	rq_list_for_each(&iob->req_list, req) {
+		virtblk_unmap_data(req, blk_mq_rq_to_pdu(req));
+		virtblk_cleanup_cmd(req);
+		blk_mq_set_request_complete(req);
+	}
+	blk_mq_end_request_batch(iob);
+}
+
 static void virtblk_done(struct virtqueue *vq)
 {
 	struct virtio_blk *vblk = vq->vdev->priv;
@@ -280,6 +292,7 @@ static void virtblk_done(struct virtqueue *vq)
 	struct virtblk_req *vbr;
 	unsigned long flags;
 	unsigned int len;
+	DEFINE_IO_COMP_BATCH(iob);
 
 	spin_lock_irqsave(&vblk->vqs[qid].lock, flags);
 	do {
@@ -287,7 +300,9 @@ static void virtblk_done(struct virtqueue *vq)
 		while ((vbr = virtqueue_get_buf(vblk->vqs[qid].vq, &len)) != NULL) {
 			struct request *req = blk_mq_rq_from_pdu(vbr);
 
-			if (likely(!blk_should_fake_timeout(req->q)))
+			if (likely(!blk_should_fake_timeout(req->q)) &&
+				!blk_mq_add_to_batch(req, &iob, vbr->status,
+							virtblk_complete_batch))
 				blk_mq_complete_request(req);
 			req_done = true;
 		}

drivers/block/virtio_blk.c static void virtblk_done(struct virtqueue *vq) · line 315

Quoted 8 lines
drivers/block/virtio_blk.c +3 -2
@@ -295,9 +310,14 @@ static void virtblk_done(struct virtqueue *vq)
 			break;
 	} while (!virtqueue_enable_cb(vq));
 
-	/* In case queue is stopped waiting for more buffers. */
-	if (req_done)
+	if (req_done) {
+		if (!rq_list_empty(iob.req_list))
+			virtblk_complete_batch(&iob);

A little optimization to avoid the indirect call: iob.complete(&iob) :). Not sure if it's good style to do that but it works in this case because we know it can only be virtblk_complete_batch().

_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization

maintainer
#5 lore raw
In reply to Stefan Hajnoczi
Quoted 13 lines · Stefan Hajnoczi
Quoted 12 lines · Suwan Kim

Driver should set req->state to MQ_RQ_COMPLETE after it finishes to process req. But virtio-blk doesn't set MQ_RQ_COMPLETE after virtblk_poll() handles req and req->state still remains MQ_RQ_IN_FLIGHT. Fortunately so far there is no issue about it because blk_mq_end_request_batch() sets req->state to MQ_RQ_IDLE. This patch properly sets req->state after polling I/O is finished.

Fixes 4e0400525691 ("virtio-blk: support polling I/O")
---
 drivers/block/virtio_blk.c | 1 +
 1 file changed, 1 insertion(+)
Quoted 16 lines

drivers/block/virtio_blk.c static void virtblk_complete_batch(struct io_comp_batch *iob) · line 845

Quoted 12 lines
drivers/block/virtio_blk.c +1 -0
index 19da5defd734..cf64d256787e 100644
@@ -839,6 +839,7 @@ static void virtblk_complete_batch(struct io_comp_batch *iob)
 	rq_list_for_each(&iob->req_list, req) {
 		virtblk_unmap_data(req, blk_mq_rq_to_pdu(req));
 		virtblk_cleanup_cmd(req);
+		blk_mq_set_request_complete(req);
 	}
 	blk_mq_end_request_batch(iob);
 }

The doc comment for blk_mq_set_request_complete() mentions this being used in ->queue_rq(), but that's not the case here. Does the doc comment need to be updated if we're using the function in a different way?

Looks like it's a bit outdated...

Quoted 7 lines

I'm not familiar enough with the Linux block APIs, but this feels weird to me. Shouldn't blk_mq_end_request_batch(iob) take care of this for us? Why does it set the state to IDLE instead of COMPLETE?

I think Jens can confirm whether we really want all drivers that use polling and io_comp_batch to manually call blk_mq_set_request_complete().

Should not be a need to call blk_mq_set_request_complete() directly in the driver for this.

Signature · 1 line
Jens Axboe
#6 lore raw
Quoted 5 lines · Jens Axboe
Quoted 3 lines

The doc comment for blk_mq_set_request_complete() mentions this being used in ->queue_rq(), but that's not the case here. Does the doc comment need to be updated if we're using the function in a different way?

Looks like it's a bit outdated...

I think the comment is still entirely correct.

Quoted 11 lines
Quoted 8 lines

I'm not familiar enough with the Linux block APIs, but this feels weird to me. Shouldn't blk_mq_end_request_batch(iob) take care of this for us? Why does it set the state to IDLE instead of COMPLETE?

I think Jens can confirm whether we really want all drivers that use polling and io_comp_batch to manually call blk_mq_set_request_complete().

Should not be a need to call blk_mq_set_request_complete() directly in the driver for this.

Exactly. Polling or not, drivers should go through the normal completion interface, that is blk_mq_complete_request or the lower-level options blk_mq_complete_request_remote and blk_mq_complete_request_direct.

author
#7 lore raw
Quoted 18 lines · Christoph Hellwig
Quoted 5 lines · Jens Axboe
Quoted 3 lines

The doc comment for blk_mq_set_request_complete() mentions this being used in ->queue_rq(), but that's not the case here. Does the doc comment need to be updated if we're using the function in a different way?

Looks like it's a bit outdated...

I think the comment is still entirely correct.

Quoted 9 lines
Quoted 8 lines

I'm not familiar enough with the Linux block APIs, but this feels weird to me. Shouldn't blk_mq_end_request_batch(iob) take care of this for us? Why does it set the state to IDLE instead of COMPLETE?

I think Jens can confirm whether we really want all drivers that use polling and io_comp_batch to manually call blk_mq_set_request_complete().

Quoted 6 lines
Quoted 2 lines

Should not be a need to call blk_mq_set_request_complete() directly in the driver for this.

Exactly. Polling or not, drivers should go through the normal completion interface, that is blk_mq_complete_request or the lower-level options blk_mq_complete_request_remote and blk_mq_complete_request_direct.

Hi all,

Thanks for the comment. It was the wrong use of the function... I will use blk_mq_complete_request_remote() instead of blk_mq_set_request_complete() and send next version soon.

Regards,
Suwan Kim

author
#3 lore raw

Re: [PATCH 2/2] virtio-blk: support completion batching for the IRQ path

In reply to Stefan Hajnoczi
Quoted 88 lines · Stefan Hajnoczi
Quoted 24 lines · Suwan Kim

This patch adds completion batching to the IRQ path. It reuses batch completion code of virtblk_poll(). It collects requests to io_comp_batch and processes them all at once. It can boost up the performance by 2%.

To validate the performance improvement and stabilty, I did fio test with 4 vCPU VM and 12 vCPU VM respectively. Both VMs have 8GB ram and the same number of HW queues as vCPU.
The fio cammad is as follows and I ran the fio 5 times and got IOPS average. (io_uring, randread, direct=1, bs=512, iodepth=64 numjobs=2,4)

Test result shows about 2% improvement.

4 vcpu VM | numjobs=2 | numjobs=4
-----------------------------------------------------------
fio without patch | 367.2K IOPS | 397.6K IOPS
-----------------------------------------------------------
fio with patch | 372.8K IOPS | 407.7K IOPS

12 vcpu VM | numjobs=2 | numjobs=4
-----------------------------------------------------------
fio without patch | 363.6K IOPS | 374.8K IOPS
-----------------------------------------------------------
fio with patch | 373.8K IOPS | 385.3K IOPS

Quoted 4 lines
---
 drivers/block/virtio_blk.c | 38 +++++++++++++++++++++++---------------
 1 file changed, 23 insertions(+), 15 deletions(-)

Cool, thanks for doing this!

Quoted 43 lines
drivers/block/virtio_blk.c +16 -1
index cf64d256787e..48fcf745f007 100644
@@ -272,6 +272,18 @@ static inline void virtblk_request_done(struct request *req)
 	blk_mq_end_request(req, virtblk_result(vbr));
 }
 
+static void virtblk_complete_batch(struct io_comp_batch *iob)
+{
+	struct request *req;
+
+	rq_list_for_each(&iob->req_list, req) {
+		virtblk_unmap_data(req, blk_mq_rq_to_pdu(req));
+		virtblk_cleanup_cmd(req);
+		blk_mq_set_request_complete(req);
+	}
+	blk_mq_end_request_batch(iob);
+}
+
 static void virtblk_done(struct virtqueue *vq)
 {
 	struct virtio_blk *vblk = vq->vdev->priv;
@@ -280,6 +292,7 @@ static void virtblk_done(struct virtqueue *vq)
 	struct virtblk_req *vbr;
 	unsigned long flags;
 	unsigned int len;
+	DEFINE_IO_COMP_BATCH(iob);
 
 	spin_lock_irqsave(&vblk->vqs[qid].lock, flags);
 	do {
@@ -287,7 +300,9 @@ static void virtblk_done(struct virtqueue *vq)
 		while ((vbr = virtqueue_get_buf(vblk->vqs[qid].vq, &len)) != NULL) {
 			struct request *req = blk_mq_rq_from_pdu(vbr);
 
-			if (likely(!blk_should_fake_timeout(req->q)))
+			if (likely(!blk_should_fake_timeout(req->q)) &&
+				!blk_mq_add_to_batch(req, &iob, vbr->status,
+							virtblk_complete_batch))
 				blk_mq_complete_request(req);
 			req_done = true;
 		}

drivers/block/virtio_blk.c static void virtblk_done(struct virtqueue *vq) · line 315

Quoted 8 lines
drivers/block/virtio_blk.c +3 -2
@@ -295,9 +310,14 @@ static void virtblk_done(struct virtqueue *vq)
 			break;
 	} while (!virtqueue_enable_cb(vq));
 
-	/* In case queue is stopped waiting for more buffers. */
-	if (req_done)
+	if (req_done) {
+		if (!rq_list_empty(iob.req_list))
+			virtblk_complete_batch(&iob);

A little optimization to avoid the indirect call: iob.complete(&iob) :). Not sure if it's good style to do that but it works in this case because we know it can only be virtblk_complete_batch().

Quoted 1 line

Hi Stefan,

Thanks for the comment!
It also needs to use blk_mq_complete_request_remote() instead of blk_mq_set_request_complete()
I will resend it with the modification of patch #1 and then please review it again.

Regards,
Suwan Kim