Skip to contentSkip to conversation

virtio_blk: freeze the virtqueue layout at probe time

PATCH Applied Yunye Zhao 4 participants 4 messages Nov 2 – Nov 3

Subsystems (3)
lore
Share & tools
lore

Yunye Zhao

author
#0 lore raw

PATCH 1/1 virtio_blk: freeze the virtqueue layout at probe time

init_vq() reads the writable num_request_queues and poll_queues module parameters both at probe and when recreating virtqueues after suspend or reset. However, virtio-blk does not update the blk-mq hardware queue count or queue maps on restore.

Increasing poll_queues can therefore remove the completion callback from a virtqueue still used for interrupt-driven I/O, leaving requests uncompleted. Reducing the effective request queue count through num_request_queues can instead leave existing hardware contexts indexing beyond the reallocated vblk->vqs array.

Determine the virtqueue count and the split between interrupt-driven and polling queues once at probe, and reuse this layout when recreating the virtqueues. Changes to these module parameters then affect only subsequently probed devices.

Fixes 0989c41bed96 ("virtio-blk: add num_request_queues module parameter")

---
The functional fix was previously tested with QEMU 8.2.2 on 7be4bc52fd9e (v7.3-2415), covering:

- PCI FLR after changing poll_queues from 0 to 1 or num_request_queues from 0 to 1 on a device with four request queues.
- Normal and polled I/O across FLR with poll_queues=1 at probe, including a subsequent change of poll_queues to 0.
- Device suspend/resume with pm_test=devices and state=freeze (not a full s2idle cycle), plus full S3 and S4 suspend/resume cycles.

 drivers/block/virtio_blk.c | 71 +++++++++++++++++++++++++++-----------------
 1 file changed, 43 insertions(+), 28 deletions(-)
drivers/block/virtio_blk.c +43 -28
index c8bfbf8435ac..0df89f381047 100644
@@ -958,42 +958,54 @@ static void virtblk_config_changed(struct virtio_device *vdev)
 	queue_work(virtblk_wq, &vblk->config_work);
 }
 
+/* Keep the queue layout unchanged for the lifetime of the tag set. */
+static int virtblk_init_queue_layout(struct virtio_blk *vblk)
+{
+	struct virtio_device *vdev = vblk->vdev;
+	unsigned short num_vqs;
+	unsigned short num_poll_vqs;
+	int err;
+
+	err = virtio_cread_feature(vdev, VIRTIO_BLK_F_MQ,
+				   struct virtio_blk_config, num_queues,
+				   &num_vqs);
+	if (err)
+		num_vqs = 1;
+
+	if (!err && !num_vqs) {
+		dev_err(&vdev->dev, "MQ advertised but zero queues reported\n");
+		return -EINVAL;
+	}
+
+	num_vqs = blk_mq_num_possible_queues(
+			min_not_zero(num_request_queues, num_vqs));
+
+	num_poll_vqs = min_t(unsigned int, poll_queues, num_vqs - 1);
+
+	vblk->num_vqs = num_vqs;
+	vblk->io_queues[HCTX_TYPE_DEFAULT] = num_vqs - num_poll_vqs;
+	vblk->io_queues[HCTX_TYPE_READ] = 0;
+	vblk->io_queues[HCTX_TYPE_POLL] = num_poll_vqs;
+
+	dev_info(&vdev->dev, "%d/%d/%d default/read/poll queues\n",
+				vblk->io_queues[HCTX_TYPE_DEFAULT],
+				vblk->io_queues[HCTX_TYPE_READ],
+				vblk->io_queues[HCTX_TYPE_POLL]);
+
+	return 0;
+}
+
 static int init_vq(struct virtio_blk *vblk)
 {
 	int err;
 	unsigned short i;
 	struct virtqueue_info *vqs_info;
 	struct virtqueue **vqs;
-	unsigned short num_vqs;
-	unsigned short num_poll_vqs;
+	unsigned short num_vqs = vblk->num_vqs;
+	unsigned short num_poll_vqs = vblk->io_queues[HCTX_TYPE_POLL];
 	struct virtio_device *vdev = vblk->vdev;
 	struct irq_affinity desc = { 0, };
 
-	err = virtio_cread_feature(vdev, VIRTIO_BLK_F_MQ,
-				   struct virtio_blk_config, num_queues,
-				   &num_vqs);
-	if (err)
-		num_vqs = 1;
-
-	if (!err && !num_vqs) {
-		dev_err(&vdev->dev, "MQ advertised but zero queues reported\n");
-		return -EINVAL;
-	}
-
-	num_vqs = blk_mq_num_possible_queues(
-			min_not_zero(num_request_queues, num_vqs));
-
-	num_poll_vqs = min_t(unsigned int, poll_queues, num_vqs - 1);
-
-	vblk->io_queues[HCTX_TYPE_DEFAULT] = num_vqs - num_poll_vqs;
-	vblk->io_queues[HCTX_TYPE_READ] = 0;
-	vblk->io_queues[HCTX_TYPE_POLL] = num_poll_vqs;
-
-	dev_info(&vdev->dev, "%d/%d/%d default/read/poll queues\n",
-				vblk->io_queues[HCTX_TYPE_DEFAULT],
-				vblk->io_queues[HCTX_TYPE_READ],
-				vblk->io_queues[HCTX_TYPE_POLL]);
-
 	vblk->vqs = kmalloc_objs(*vblk->vqs, num_vqs);
 	if (!vblk->vqs)
 		return -ENOMEM;
@@ -1025,7 +1037,6 @@ static int init_vq(struct virtio_blk *vblk)
 		spin_lock_init(&vblk->vqs[i].lock);
 		vblk->vqs[i].vq = vqs[i];
 	}
-	vblk->num_vqs = num_vqs;
 
 out:
 	kfree(vqs);
@@ -1470,6 +1481,10 @@ static int virtblk_probe(struct virtio_device *vdev)
 
 	INIT_WORK(&vblk->config_work, virtblk_config_changed_work);
 
+	err = virtblk_init_queue_layout(vblk);
+	if (err)
+		goto out_free_vblk;
+
 	err = init_vq(vblk);
 	if (err)
 		goto out_free_vblk;

base-commit: 7be4bc52fd9e0e55dc9a2e0232b89427d2c4b614

Signature · 1 line
2.19.1.6.gb485710b
#1 lore raw
Sashiko has reviewed this patch and found no issues.
Quoted 11 lines

init_vq() reads the writable num_request_queues and poll_queues module parameters both at probe and when recreating virtqueues after suspend or reset. However, virtio-blk does not update the blk-mq hardware queue count or queue maps on restore.

Increasing poll_queues can therefore remove the completion callback from a virtqueue still used for interrupt-driven I/O, leaving requests uncompleted. Reducing the effective request queue count through num_request_queues can instead leave existing hardware contexts indexing beyond the reallocated vblk->vqs array.

Quoted 7 lines

Determine the virtqueue count and the split between interrupt-driven and polling queues once at probe, and reuse this layout when recreating the virtqueues. Changes to these module parameters then affect only subsequently probed devices.

Fixes 0989c41bed96 ("virtio-blk: add num_request_queues module parameter")

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
reviewer Reviewed-by on v1 1/1
#2 lore raw
In reply to Yunye Zhao
Quoted 22 lines · Yunye Zhao

init_vq() reads the writable num_request_queues and poll_queues module parameters both at probe and when recreating virtqueues after suspend or reset. However, virtio-blk does not update the blk-mq hardware queue count or queue maps on restore.

Increasing poll_queues can therefore remove the completion callback from a virtqueue still used for interrupt-driven I/O, leaving requests uncompleted. Reducing the effective request queue count through num_request_queues can instead leave existing hardware contexts indexing beyond the reallocated vblk->vqs array.

Determine the virtqueue count and the split between interrupt-driven and polling queues once at probe, and reuse this layout when recreating the virtqueues. Changes to these module parameters then affect only subsequently probed devices.

Fixes 0989c41bed96 ("virtio-blk: add num_request_queues module parameter")

---
The functional fix was previously tested with QEMU 8.2.2 on 7be4bc52fd9e (v7.3-2415), covering:

Quoted 6 lines

- PCI FLR after changing poll_queues from 0 to 1 or num_request_queues from 0 to 1 on a device with four request queues.
- Normal and polled I/O across FLR with poll_queues=1 at probe, including a subsequent change of poll_queues to 0.
- Device suspend/resume with pm_test=devices and state=freeze (not a full s2idle cycle), plus full S3 and S4 suspend/resume cycles.

I noticed that drivers/nvme/host/ allows the queues to change:
1. The hardware queue count can be different and the tagset is updated with blk_mq_update_nr_hw_queues().
2. The module parameters are refreshed in nvme_setup_io_queues() and are allowed to change across power management operations.

Please consider implementing more flexibile restore behavior in virtio_blk that can toleration hardware queue count changes and module parameter changes instead though.

However, this patch looks correct to me and can be merged IMO.

maintainer Applied
#3 lore raw
In reply to Yunye Zhao
Quoted 5 lines · Yunye Zhao

init_vq() reads the writable num_request_queues and poll_queues module parameters both at probe and when recreating virtqueues after suspend or reset. However, virtio-blk does not update the blk-mq hardware queue count or queue maps on restore.

Quoted 7 lines

Increasing poll_queues can therefore remove the completion callback from a virtqueue still used for interrupt-driven I/O, leaving requests uncompleted. Reducing the effective request queue count through num_request_queues can instead leave existing hardware contexts indexing beyond the reallocated vblk->vqs array.

[...]

Applied, thanks!

[1/1] virtio_blk: freeze the virtqueue layout at probe time

      commit: d53c1a12ff1cfdb12770ddcfdf5603915d0ba0d8

Best regards,

Signature · 1 line
Jens Axboe