Skip to contentSkip to conversation

virtio_blk: Fix device surprise removal

PATCH In review Parav Pandit 4 participants 13 messages Feb 17, 2024 – Feb 22, 2024

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

PATCH 1/1 virtio_blk: Fix device surprise removal

When the PCI device is surprise removed, requests won't complete from the device. These IOs are never completed and disk deletion hangs indefinitely.

Fix it by aborting the IOs which the device will never complete when the VQ is broken.

With this fix now fio completes swiftly.
An alternative of IO timeout has been considered, however when the driver knows about unresponsive block device, swiftly clearing them enables users and upper layers to react quickly.

Verified with multiple device unplug cycles with pending IOs in virtio used ring and some pending with device.

In future instead of VQ broken, a more elegant method can be used. At the moment the patch is kept to its minimal changes given its urgency to fix broken kernels.

Fixes 43bb40c5b926 ("virtio_pci: Support surprise removal of virtio pci device")
Reported-by [email protected]
---
 drivers/block/virtio_blk.c | 54 ++++++++++++++++++++++++++++++++++++++
 1 file changed, 54 insertions(+)
drivers/block/virtio_blk.c +54 -0
index 2bf14a0e2815..59b49899b229 100644
@@ -1562,10 +1562,64 @@ static int virtblk_probe(struct virtio_device *vdev)
 	return err;
 }
 
+static bool virtblk_cancel_request(struct request *rq, void *data)
+{
+	struct virtblk_req *vbr = blk_mq_rq_to_pdu(rq);
+
+	vbr->in_hdr.status = VIRTIO_BLK_S_IOERR;
+	if (blk_mq_request_started(rq) && !blk_mq_request_completed(rq))
+		blk_mq_complete_request(rq);
+
+	return true;
+}
+
+static void virtblk_cleanup_reqs(struct virtio_blk *vblk)
+{
+	struct virtio_blk_vq *blk_vq;
+	struct request_queue *q;
+	struct virtqueue *vq;
+	unsigned long flags;
+	int i;
+
+	vq = vblk->vqs[0].vq;
+	if (!virtqueue_is_broken(vq))
+		return;
+
+	q = vblk->disk->queue;
+	/* Block upper layer to not get any new requests */
+	blk_mq_quiesce_queue(q);
+
+	for (i = 0; i < vblk->num_vqs; i++) {
+		blk_vq = &vblk->vqs[i];
+
+		/* Synchronize with any ongoing virtblk_poll() which may be
+		 * completing the requests to uppper layer which has already
+		 * crossed the broken vq check.
+		 */
+		spin_lock_irqsave(&blk_vq->lock, flags);
+		spin_unlock_irqrestore(&blk_vq->lock, flags);
+	}
+
+	blk_sync_queue(q);
+
+	/* Complete remaining pending requests with error */
+	blk_mq_tagset_busy_iter(&vblk->tag_set, virtblk_cancel_request, vblk);
+	blk_mq_tagset_wait_completed_request(&vblk->tag_set);
+
+	/*
+	 * Unblock any pending dispatch I/Os before we destroy device. From
+	 * del_gendisk() -> __blk_mark_disk_dead(disk) will set GD_DEAD flag,
+	 * that will make sure any new I/O from bio_queue_enter() to fail.
+	 */
+	blk_mq_unquiesce_queue(q);
+}
+
 static void virtblk_remove(struct virtio_device *vdev)
 {
 	struct virtio_blk *vblk = vdev->priv;
 
+	virtblk_cleanup_reqs(vblk);
+
 	/* Make sure no work handler is accessing the device. */
 	flush_work(&vblk->config_work);
 
Signature · 1 line
2.34.1
#1 lore raw
Quoted 51 lines · Parav Pandit

When the PCI device is surprise removed, requests won't complete from the device. These IOs are never completed and disk deletion hangs indefinitely.

Fix it by aborting the IOs which the device will never complete when the VQ is broken.

With this fix now fio completes swiftly.
An alternative of IO timeout has been considered, however when the driver knows about unresponsive block device, swiftly clearing them enables users and upper layers to react quickly.

Verified with multiple device unplug cycles with pending IOs in virtio used ring and some pending with device.

In future instead of VQ broken, a more elegant method can be used. At the moment the patch is kept to its minimal changes given its urgency to fix broken kernels.

Fixes 43bb40c5b926 ("virtio_pci: Support surprise removal of virtio pci device")
Reported-by [email protected]
---
 drivers/block/virtio_blk.c | 54 ++++++++++++++++++++++++++++++++++++++
 1 file changed, 54 insertions(+)
drivers/block/virtio_blk.c +13 -0
index 2bf14a0e2815..59b49899b229 100644
@@ -1562,10 +1562,64 @@ static int virtblk_probe(struct virtio_device *vdev)
 	return err;
 }
 
+static bool virtblk_cancel_request(struct request *rq, void *data)
+{
+	struct virtblk_req *vbr = blk_mq_rq_to_pdu(rq);
+
+	vbr->in_hdr.status = VIRTIO_BLK_S_IOERR;
+	if (blk_mq_request_started(rq) && !blk_mq_request_completed(rq))
+		blk_mq_complete_request(rq);
+
+	return true;
+}
+
+static void virtblk_cleanup_reqs(struct virtio_blk *vblk)
+{

drivers/block/virtio_blk.c static int virtblk_probe(struct virtio_device *vdev) · line 1587

Quoted 10 lines
drivers/block/virtio_blk.c +10 -0
+	struct virtio_blk_vq *blk_vq;
+	struct request_queue *q;
+	struct virtqueue *vq;
+	unsigned long flags;
+	int i;
+
+	vq = vblk->vqs[0].vq;
+	if (!virtqueue_is_broken(vq))
+		return;
+

What if the surprise happens after the above check?

Thanks,
Ming

#2 lore raw

Hi Ming,

Quoted 62 lines

From: Ming Lei <[email protected]>
Sent: Sunday, February 18, 2024 6:57 PM

drivers/block/virtio_blk.c static int virtblk_probe(struct virtio_device

Quoted 39 lines · Parav Pandit

When the PCI device is surprise removed, requests won't complete from the device. These IOs are never completed and disk deletion hangs indefinitely.

Fix it by aborting the IOs which the device will never complete when the VQ is broken.

With this fix now fio completes swiftly.
An alternative of IO timeout has been considered, however when the driver knows about unresponsive block device, swiftly clearing them enables users and upper layers to react quickly.

Verified with multiple device unplug cycles with pending IOs in virtio used ring and some pending with device.

In future instead of VQ broken, a more elegant method can be used. At the moment the patch is kept to its minimal changes given its urgency to fix broken kernels.

Fixes 43bb40c5b926 ("virtio_pci: Support surprise removal of virtio

pci device")

Closes:
https://lore.kernel.org/virtualization/c45dd68698cd47238c55fb73ca9b474
[email protected]/

---
 drivers/block/virtio_blk.c | 54
++++++++++++++++++++++++++++++++++++++
 1 file changed, 54 insertions(+)
drivers/block/virtio_blk.c +0 -0
index 2bf14a0e2815..59b49899b229 100644
@@ -1562,10 +1562,64 @@ static int virtblk_probe(struct virtio_device

*vdev)

Quoted 18 lines
drivers/block/virtio_blk.c +15 -0
 	return err;
 }
 
+static bool virtblk_cancel_request(struct request *rq, void *data) {
+	struct virtblk_req *vbr = blk_mq_rq_to_pdu(rq);
+
+	vbr->in_hdr.status = VIRTIO_BLK_S_IOERR;
+	if (blk_mq_request_started(rq) && !blk_mq_request_completed(rq))
+		blk_mq_complete_request(rq);
+
+	return true;
+}
+
+static void virtblk_cleanup_reqs(struct virtio_blk *vblk) {
+	struct virtio_blk_vq *blk_vq;
+	struct request_queue *q;
+	struct virtqueue *vq;
+	unsigned long flags;
Quoted 10 lines

drivers/block/virtio_blk.c static int virtblk_probe(struct virtio_device *vdev) · line 1587

Quoted 6 lines
drivers/block/virtio_blk.c +6 -0
+	int i;
+
+	vq = vblk->vqs[0].vq;
+	if (!virtqueue_is_broken(vq))
+		return;
+

What if the surprise happens after the above check?

In that small timing window, the race still exists.

I think, blk_mq_quiesce_queue(q); should move up before cleanup_reqs() regardless of surprise case along with other below changes.

Additionally, for non-surprise case, better to have a graceful timeout to complete already queued requests.
In absence of timeout scheme for this regression, shall we only complete the requests which the device has already completed (instead of waiting for the grace time)? There was past work from Chaitanaya, for the graceful timeout.

The sequence for the fix I have in mind is:
1. quiesce the queue
2. complete all requests which has completed, with its status
3. stop the transport (queues)
4. complete remaining pending requests with error status

This should work regardless of surprise case.
An additional/optional graceful timeout on non-surprise case can be helpful for #2.

WDYT?

Quoted 2 lines

Thanks,
Ming

#3 lore raw
Quoted 88 lines · Parav Pandit

Hi Ming,

Quoted 62 lines

From: Ming Lei <[email protected]>
Sent: Sunday, February 18, 2024 6:57 PM

drivers/block/virtio_blk.c static int virtblk_probe(struct virtio_device

Quoted 39 lines · Parav Pandit

When the PCI device is surprise removed, requests won't complete from the device. These IOs are never completed and disk deletion hangs indefinitely.

Fix it by aborting the IOs which the device will never complete when the VQ is broken.

With this fix now fio completes swiftly.
An alternative of IO timeout has been considered, however when the driver knows about unresponsive block device, swiftly clearing them enables users and upper layers to react quickly.

Verified with multiple device unplug cycles with pending IOs in virtio used ring and some pending with device.

In future instead of VQ broken, a more elegant method can be used. At the moment the patch is kept to its minimal changes given its urgency to fix broken kernels.

Fixes 43bb40c5b926 ("virtio_pci: Support surprise removal of virtio

pci device")

Closes:
https://lore.kernel.org/virtualization/c45dd68698cd47238c55fb73ca9b474
[email protected]/

---
 drivers/block/virtio_blk.c | 54
++++++++++++++++++++++++++++++++++++++
 1 file changed, 54 insertions(+)
drivers/block/virtio_blk.c +0 -0
index 2bf14a0e2815..59b49899b229 100644
@@ -1562,10 +1562,64 @@ static int virtblk_probe(struct virtio_device

*vdev)

Quoted 18 lines
drivers/block/virtio_blk.c +15 -0
 	return err;
 }
 
+static bool virtblk_cancel_request(struct request *rq, void *data) {
+	struct virtblk_req *vbr = blk_mq_rq_to_pdu(rq);
+
+	vbr->in_hdr.status = VIRTIO_BLK_S_IOERR;
+	if (blk_mq_request_started(rq) && !blk_mq_request_completed(rq))
+		blk_mq_complete_request(rq);
+
+	return true;
+}
+
+static void virtblk_cleanup_reqs(struct virtio_blk *vblk) {
+	struct virtio_blk_vq *blk_vq;
+	struct request_queue *q;
+	struct virtqueue *vq;
+	unsigned long flags;
Quoted 10 lines

drivers/block/virtio_blk.c static int virtblk_probe(struct virtio_device *vdev) · line 1587

Quoted 6 lines
drivers/block/virtio_blk.c +6 -0
+	int i;
+
+	vq = vblk->vqs[0].vq;
+	if (!virtqueue_is_broken(vq))
+		return;
+

What if the surprise happens after the above check?

In that small timing window, the race still exists.

I think, blk_mq_quiesce_queue(q); should move up before cleanup_reqs() regardless of surprise case along with other below changes.

Additionally, for non-surprise case, better to have a graceful timeout to complete already queued requests.
In absence of timeout scheme for this regression, shall we only complete the requests which the device has already completed (instead of waiting for the grace time)? There was past work from Chaitanaya, for the graceful timeout.

The sequence for the fix I have in mind is:
1. quiesce the queue
2. complete all requests which has completed, with its status
3. stop the transport (queues)
4. complete remaining pending requests with error status

Quoted 4 lines

This should work regardless of surprise case.
An additional/optional graceful timeout on non-surprise case can be helpful for #2.

WDYT?

All this is unnecessarily hard for drivers... I am thinking maybe after we set broken we should go ahead and invoke all callbacks. The issue is really interrupt handling core is not making it easy for us - we must disable real interrupts if we do, and in the past we failed to do it. See e.g.

commit eb4cecb453a19b34d5454b49532e09e9cb0c1529

Author: Jason Wang <[email protected]>
Date:   Wed Mar 23 11:15:24 2022 +0800

Revert "virtio_pci: harden MSI-X interrupts"

This reverts commit 9e35276a5344f74d4a3600fc4100b3dd251d5c56. Issue were reported for the drivers that are using affinity managed IRQ where manually toggling IRQ status is not expected. And we forget to enable the interrupts in the restore path as well.

In the future, we will rework on the interrupt hardening.

Fixes 9e35276a5344 ("virtio_pci: harden MSI-X interrupts")

If someone can figure out a way to make toggling interrupt state play nice with affinity managed interrupts, that would solve a host of issues I feel.

Quoted 2 lines
Quoted 2 lines

Thanks,
Ming

#4 lore raw
Quoted 99 lines

From: Michael S. Tsirkin <[email protected]>
Sent: Monday, February 19, 2024 1:45 PM

Quoted 77 lines · Parav Pandit

Hi Ming,

Quoted 65 lines

From: Ming Lei <[email protected]>
Sent: Sunday, February 18, 2024 6:57 PM

Quoted 41 lines · Parav Pandit

When the PCI device is surprise removed, requests won't complete from the device. These IOs are never completed and disk deletion hangs indefinitely.

Fix it by aborting the IOs which the device will never complete when the VQ is broken.

With this fix now fio completes swiftly.
An alternative of IO timeout has been considered, however when the driver knows about unresponsive block device, swiftly clearing them enables users and upper layers to react quickly.

Verified with multiple device unplug cycles with pending IOs in virtio used ring and some pending with device.

In future instead of VQ broken, a more elegant method can be used. At the moment the patch is kept to its minimal changes given its urgency to fix broken kernels.

Fixes 43bb40c5b926 ("virtio_pci: Support surprise removal of

virtio pci device")

Closes:
https://lore.kernel.org/virtualization/c45dd68698cd47238c55fb73ca9
b474
[email protected]/

---
 drivers/block/virtio_blk.c | 54
++++++++++++++++++++++++++++++++++++++
 1 file changed, 54 insertions(+)
(quoted diff) +0 -0

b/drivers/block/virtio_blk.c index 2bf14a0e2815..59b49899b229 100644

drivers/block/virtio_blk.c +0 -0
@@ -1562,10 +1562,64 @@ static int virtblk_probe(struct
Quoted 1 line

virtio_device

*vdev)

Quoted 18 lines
drivers/block/virtio_blk.c +15 -0
 	return err;
 }
 
+static bool virtblk_cancel_request(struct request *rq, void *data) {
+	struct virtblk_req *vbr = blk_mq_rq_to_pdu(rq);
+
+	vbr->in_hdr.status = VIRTIO_BLK_S_IOERR;
+	if (blk_mq_request_started(rq) && !blk_mq_request_completed(rq))
+		blk_mq_complete_request(rq);
+
+	return true;
+}
+
+static void virtblk_cleanup_reqs(struct virtio_blk *vblk) {
+	struct virtio_blk_vq *blk_vq;
+	struct request_queue *q;
+	struct virtqueue *vq;
+	unsigned long flags;
Quoted 10 lines

drivers/block/virtio_blk.c static int virtblk_probe(struct virtio_device *vdev) · line 1587

Quoted 6 lines
drivers/block/virtio_blk.c +6 -0
+	int i;
+
+	vq = vblk->vqs[0].vq;
+	if (!virtqueue_is_broken(vq))
+		return;
+

What if the surprise happens after the above check?

Quoted 3 lines

In that small timing window, the race still exists.

I think, blk_mq_quiesce_queue(q); should move up before cleanup_reqs()

regardless of surprise case along with other below changes.

Quoted 2 lines

Additionally, for non-surprise case, better to have a graceful timeout to

complete already queued requests.

Quoted 1 line

In absence of timeout scheme for this regression, shall we only complete the

requests which the device has already completed (instead of waiting for the grace time)?

Quoted 8 lines

There was past work from Chaitanaya, for the graceful timeout.

The sequence for the fix I have in mind is:
1. quiesce the queue
2. complete all requests which has completed, with its status 3. stop the transport (queues) 4. complete remaining pending requests with error status

Quoted 8 lines
Quoted 2 lines

This should work regardless of surprise case.
An additional/optional graceful timeout on non-surprise case can be helpful

for #2.

Quoted 2 lines

WDYT?

All this is unnecessarily hard for drivers... I am thinking maybe after we set broken we should go ahead and invoke all callbacks.

Yes, #2 is about invoking the callbacks.

The issue is not with setting the flag broken. As Ming pointed, the issue is : we may miss setting the broken.

Without graceful time out it is straight forward code, just rearrangement of APIs in this patch with existing code.

The question is : it is really if we really care for that grace period when the device or driver is already on its exit path and VQ is not broken. If we don't wait for the request in progress, is it ok?

Quoted 30 lines

interrupt handling core is not making it easy for us - we must disable real interrupts if we do, and in the past we failed to do it.
See e.g.

commit eb4cecb453a19b34d5454b49532e09e9cb0c1529

Author: Jason Wang <[email protected]>
Date:   Wed Mar 23 11:15:24 2022 +0800

Revert "virtio_pci: harden MSI-X interrupts"

This reverts commit 9e35276a5344f74d4a3600fc4100b3dd251d5c56.
Issue
were reported for the drivers that are using affinity managed IRQ where manually toggling IRQ status is not expected. And we forget to enable the interrupts in the restore path as well.

In the future, we will rework on the interrupt hardening.

Fixes 9e35276a5344 ("virtio_pci: harden MSI-X interrupts")

If someone can figure out a way to make toggling interrupt state play nice with affinity managed interrupts, that would solve a host of issues I feel.

Quoted 2 lines
Quoted 2 lines

Thanks,
Ming

#5 lore raw
Quoted 103 lines · Parav Pandit
Quoted 103 lines

From: Michael S. Tsirkin <[email protected]>
Sent: Monday, February 19, 2024 1:45 PM

Quoted 77 lines · Parav Pandit

Hi Ming,

Quoted 65 lines

From: Ming Lei <[email protected]>
Sent: Sunday, February 18, 2024 6:57 PM

Quoted 41 lines · Parav Pandit

When the PCI device is surprise removed, requests won't complete from the device. These IOs are never completed and disk deletion hangs indefinitely.

Fix it by aborting the IOs which the device will never complete when the VQ is broken.

With this fix now fio completes swiftly.
An alternative of IO timeout has been considered, however when the driver knows about unresponsive block device, swiftly clearing them enables users and upper layers to react quickly.

Verified with multiple device unplug cycles with pending IOs in virtio used ring and some pending with device.

In future instead of VQ broken, a more elegant method can be used. At the moment the patch is kept to its minimal changes given its urgency to fix broken kernels.

Fixes 43bb40c5b926 ("virtio_pci: Support surprise removal of

virtio pci device")

Closes:
https://lore.kernel.org/virtualization/c45dd68698cd47238c55fb73ca9
b474
[email protected]/

---
 drivers/block/virtio_blk.c | 54
++++++++++++++++++++++++++++++++++++++
 1 file changed, 54 insertions(+)
(quoted diff) +0 -0

b/drivers/block/virtio_blk.c index 2bf14a0e2815..59b49899b229 100644

drivers/block/virtio_blk.c +0 -0
@@ -1562,10 +1562,64 @@ static int virtblk_probe(struct
Quoted 1 line

virtio_device

*vdev)

Quoted 18 lines
drivers/block/virtio_blk.c +15 -0
 	return err;
 }
 
+static bool virtblk_cancel_request(struct request *rq, void *data) {
+	struct virtblk_req *vbr = blk_mq_rq_to_pdu(rq);
+
+	vbr->in_hdr.status = VIRTIO_BLK_S_IOERR;
+	if (blk_mq_request_started(rq) && !blk_mq_request_completed(rq))
+		blk_mq_complete_request(rq);
+
+	return true;
+}
+
+static void virtblk_cleanup_reqs(struct virtio_blk *vblk) {
+	struct virtio_blk_vq *blk_vq;
+	struct request_queue *q;
+	struct virtqueue *vq;
+	unsigned long flags;
Quoted 10 lines

drivers/block/virtio_blk.c static int virtblk_probe(struct virtio_device *vdev) · line 1587

Quoted 6 lines
drivers/block/virtio_blk.c +6 -0
+	int i;
+
+	vq = vblk->vqs[0].vq;
+	if (!virtqueue_is_broken(vq))
+		return;
+

What if the surprise happens after the above check?

Quoted 3 lines

In that small timing window, the race still exists.

I think, blk_mq_quiesce_queue(q); should move up before cleanup_reqs()

regardless of surprise case along with other below changes.

Quoted 2 lines

Additionally, for non-surprise case, better to have a graceful timeout to

complete already queued requests.

Quoted 1 line

In absence of timeout scheme for this regression, shall we only complete the

requests which the device has already completed (instead of waiting for the grace time)?

Quoted 10 lines

There was past work from Chaitanaya, for the graceful timeout.

The sequence for the fix I have in mind is:
1. quiesce the queue
2. complete all requests which has completed, with its status 3. stop the transport (queues) 4. complete remaining pending requests with error status

This should work regardless of surprise case.
An additional/optional graceful timeout on non-surprise case can be helpful

for #2.

Quoted 1 line
Quoted 8 lines
Quoted 4 lines
Quoted 1 line

WDYT?

All this is unnecessarily hard for drivers... I am thinking maybe after we set broken we should go ahead and invoke all callbacks.

Yes, #2 is about invoking the callbacks.

The issue is not with setting the flag broken. As Ming pointed, the issue is : we may miss setting the broken.

So if we did get callbacks, we'd be able to test broken flag in the callback.

Quoted 5 lines

Without graceful time out it is straight forward code, just rearrangement of APIs in this patch with existing code.

The question is : it is really if we really care for that grace period when the device or driver is already on its exit path and VQ is not broken. If we don't wait for the request in progress, is it ok?

If we are talking about physical hardware, it seems quite possible that removal triggers then user gets impatient and yanks the card out.

Quoted 30 lines
Quoted 30 lines

interrupt handling core is not making it easy for us - we must disable real interrupts if we do, and in the past we failed to do it.
See e.g.

commit eb4cecb453a19b34d5454b49532e09e9cb0c1529

Author: Jason Wang <[email protected]>
Date:   Wed Mar 23 11:15:24 2022 +0800

Revert "virtio_pci: harden MSI-X interrupts"

This reverts commit 9e35276a5344f74d4a3600fc4100b3dd251d5c56.
Issue
were reported for the drivers that are using affinity managed IRQ where manually toggling IRQ status is not expected. And we forget to enable the interrupts in the restore path as well.

In the future, we will rework on the interrupt hardening.

Fixes 9e35276a5344 ("virtio_pci: harden MSI-X interrupts")

If someone can figure out a way to make toggling interrupt state play nice with affinity managed interrupts, that would solve a host of issues I feel.

Quoted 2 lines
Quoted 2 lines

Thanks,
Ming

#6 lore raw
Quoted 119 lines

From: Michael S. Tsirkin <[email protected]>
Sent: Monday, February 19, 2024 4:17 PM

Quoted 54 lines · Parav Pandit
Quoted 54 lines

From: Michael S. Tsirkin <[email protected]>
Sent: Monday, February 19, 2024 1:45 PM

Quoted 50 lines · Parav Pandit

Hi Ming,

Quoted 48 lines

From: Ming Lei <[email protected]>
Sent: Sunday, February 18, 2024 6:57 PM

Quoted 42 lines · Parav Pandit

When the PCI device is surprise removed, requests won't complete from the device. These IOs are never completed and disk deletion hangs indefinitely.

Fix it by aborting the IOs which the device will never complete when the VQ is broken.

With this fix now fio completes swiftly.
An alternative of IO timeout has been considered, however when the driver knows about unresponsive block device, swiftly clearing them enables users and upper layers to react quickly.

Verified with multiple device unplug cycles with pending IOs in virtio used ring and some pending with device.

In future instead of VQ broken, a more elegant method can be used. At the moment the patch is kept to its minimal changes given its urgency to fix broken kernels.

Fixes 43bb40c5b926 ("virtio_pci: Support surprise removal of

virtio pci device")

Closes:
https://lore.kernel.org/virtualization/c45dd68698cd47238c55fb7
3ca9
b474
[email protected]/

---
 drivers/block/virtio_blk.c | 54
++++++++++++++++++++++++++++++++++++++
 1 file changed, 54 insertions(+)
(quoted diff) +0 -0

b/drivers/block/virtio_blk.c index 2bf14a0e2815..59b49899b229 100644

drivers/block/virtio_blk.c +0 -0
@@ -1562,10 +1562,64 @@ static int virtblk_probe(struct
Quoted 1 line

virtio_device

*vdev)

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

drivers/block/virtio_blk.c static int virtblk_probe(struct virtio_device *vdev)

Quoted 8 lines
drivers/block/virtio_blk.c +5 -0
 	return err;
 }
 
+static bool virtblk_cancel_request(struct request *rq, void *data) {
+	struct virtblk_req *vbr = blk_mq_rq_to_pdu(rq);
+
+	vbr->in_hdr.status = VIRTIO_BLK_S_IOERR;
+	if (blk_mq_request_started(rq) &&

!blk_mq_request_completed(rq))

Quoted 51 lines
Quoted 50 lines
Quoted 20 lines
Quoted 10 lines
Quoted 10 lines
drivers/block/virtio_blk.c +10 -0
+		blk_mq_complete_request(rq);
+
+	return true;
+}
+
+static void virtblk_cleanup_reqs(struct virtio_blk *vblk) {
+	struct virtio_blk_vq *blk_vq;
+	struct request_queue *q;
+	struct virtqueue *vq;
+	unsigned long flags;
Quoted 10 lines

drivers/block/virtio_blk.c static int virtblk_probe(struct virtio_device *vdev) · line 1587

Quoted 6 lines
drivers/block/virtio_blk.c +6 -0
+	int i;
+
+	vq = vblk->vqs[0].vq;
+	if (!virtqueue_is_broken(vq))
+		return;
+

What if the surprise happens after the above check?

Quoted 4 lines

In that small timing window, the race still exists.

I think, blk_mq_quiesce_queue(q); should move up before cleanup_reqs()

regardless of surprise case along with other below changes.

Quoted 3 lines

Additionally, for non-surprise case, better to have a graceful timeout to

complete already queued requests.

Quoted 2 lines

In absence of timeout scheme for this regression, shall we only complete the

requests which the device has already completed (instead of waiting for the grace time)?

Quoted 11 lines

There was past work from Chaitanaya, for the graceful timeout.

The sequence for the fix I have in mind is:
1. quiesce the queue
2. complete all requests which has completed, with its status 3. stop the transport (queues) 4. complete remaining pending requests with error status

This should work regardless of surprise case.
An additional/optional graceful timeout on non-surprise case can be helpful

for #2.

Quoted 2 lines

WDYT?

All this is unnecessarily hard for drivers... I am thinking maybe after we set broken we should go ahead and invoke all callbacks.

Quoted 8 lines
Quoted 3 lines

Yes, #2 is about invoking the callbacks.

The issue is not with setting the flag broken. As Ming pointed, the issue is :

we may miss setting the broken.

So if we did get callbacks, we'd be able to test broken flag in the callback.

Yes, getting callbacks is fine.
But when the device is surprise removed, we wont get the callbacks and completions are missed.

Quoted 11 lines
Quoted 1 line

Without graceful time out it is straight forward code, just rearrangement of

APIs in this patch with existing code.

Quoted 2 lines

The question is : it is really if we really care for that grace period when the

device or driver is already on its exit path and VQ is not broken.

Quoted 2 lines

If we don't wait for the request in progress, is it ok?

If we are talking about physical hardware, it seems quite possible that removal triggers then user gets impatient and yanks the card out.

Yes, regardless of surprise or not, completing the remaining IOs is just good enough.
Device is anyway on its exit path, so completing 10 commands vs 12, does not make a lot of difference with extra complexity of timeout.

So better to not complicate the driver, at least not when adding Fixes tag patch.

Quoted 32 lines
Quoted 18 lines
Quoted 18 lines

interrupt handling core is not making it easy for us - we must disable real interrupts if we do, and in the past we failed to do it. See e.g.

commit eb4cecb453a19b34d5454b49532e09e9cb0c1529

Author: Jason Wang <[email protected]>
Date:   Wed Mar 23 11:15:24 2022 +0800

Revert "virtio_pci: harden MSI-X interrupts"

This reverts commit 9e35276a5344f74d4a3600fc4100b3dd251d5c56.
Issue
were reported for the drivers that are using affinity managed IRQ where manually toggling IRQ status is not expected. And we forget to enable the interrupts in the restore path as well.

Quoted 8 lines
Quoted 8 lines

In the future, we will rework on the interrupt hardening.

Fixes 9e35276a5344 ("virtio_pci: harden MSI-X interrupts")

If someone can figure out a way to make toggling interrupt state play nice with affinity managed interrupts, that would solve a host of

issues I feel.

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

Thanks,
Ming

#7 lore raw
Quoted 121 lines · Parav Pandit
Quoted 121 lines

From: Michael S. Tsirkin <[email protected]>
Sent: Monday, February 19, 2024 4:17 PM

Quoted 54 lines · Parav Pandit
Quoted 54 lines

From: Michael S. Tsirkin <[email protected]>
Sent: Monday, February 19, 2024 1:45 PM

Quoted 50 lines · Parav Pandit

Hi Ming,

Quoted 48 lines

From: Ming Lei <[email protected]>
Sent: Sunday, February 18, 2024 6:57 PM

Quoted 42 lines · Parav Pandit

When the PCI device is surprise removed, requests won't complete from the device. These IOs are never completed and disk deletion hangs indefinitely.

Fix it by aborting the IOs which the device will never complete when the VQ is broken.

With this fix now fio completes swiftly.
An alternative of IO timeout has been considered, however when the driver knows about unresponsive block device, swiftly clearing them enables users and upper layers to react quickly.

Verified with multiple device unplug cycles with pending IOs in virtio used ring and some pending with device.

In future instead of VQ broken, a more elegant method can be used. At the moment the patch is kept to its minimal changes given its urgency to fix broken kernels.

Fixes 43bb40c5b926 ("virtio_pci: Support surprise removal of

virtio pci device")

Closes:
https://lore.kernel.org/virtualization/c45dd68698cd47238c55fb7
3ca9
b474
[email protected]/

---
 drivers/block/virtio_blk.c | 54
++++++++++++++++++++++++++++++++++++++
 1 file changed, 54 insertions(+)
(quoted diff) +0 -0

b/drivers/block/virtio_blk.c index 2bf14a0e2815..59b49899b229 100644

drivers/block/virtio_blk.c +0 -0
@@ -1562,10 +1562,64 @@ static int virtblk_probe(struct
Quoted 1 line

virtio_device

*vdev)

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

drivers/block/virtio_blk.c

Quoted 8 lines
drivers/block/virtio_blk.c +5 -0
 	return err;
 }
 
+static bool virtblk_cancel_request(struct request *rq, void *data) {
+	struct virtblk_req *vbr = blk_mq_rq_to_pdu(rq);
+
+	vbr->in_hdr.status = VIRTIO_BLK_S_IOERR;
+	if (blk_mq_request_started(rq) &&

!blk_mq_request_completed(rq))

Quoted 53 lines
Quoted 44 lines
Quoted 20 lines
Quoted 10 lines
Quoted 10 lines
drivers/block/virtio_blk.c +10 -0
+		blk_mq_complete_request(rq);
+
+	return true;
+}
+
+static void virtblk_cleanup_reqs(struct virtio_blk *vblk) {
+	struct virtio_blk_vq *blk_vq;
+	struct request_queue *q;
+	struct virtqueue *vq;
+	unsigned long flags;
Quoted 10 lines

drivers/block/virtio_blk.c

Quoted 6 lines
drivers/block/virtio_blk.c +6 -0
+	int i;
+
+	vq = vblk->vqs[0].vq;
+	if (!virtqueue_is_broken(vq))
+		return;
+

What if the surprise happens after the above check?

Quoted 4 lines

In that small timing window, the race still exists.

I think, blk_mq_quiesce_queue(q); should move up before cleanup_reqs()

regardless of surprise case along with other below changes.

Quoted 3 lines

Additionally, for non-surprise case, better to have a graceful timeout to

complete already queued requests.

Quoted 2 lines

In absence of timeout scheme for this regression, shall we only complete the

requests which the device has already completed (instead of waiting for the grace time)?

Quoted 11 lines

There was past work from Chaitanaya, for the graceful timeout.

The sequence for the fix I have in mind is:
1. quiesce the queue
2. complete all requests which has completed, with its status 3. stop the transport (queues) 4. complete remaining pending requests with error status

This should work regardless of surprise case.
An additional/optional graceful timeout on non-surprise case can be helpful

Quoted 6 lines

for #2.

Quoted 2 lines

WDYT?

All this is unnecessarily hard for drivers... I am thinking maybe after we set broken we should go ahead and invoke all callbacks.

Yes, #2 is about invoking the callbacks.

Quoted 8 lines
Quoted 6 lines
Quoted 1 line

The issue is not with setting the flag broken. As Ming pointed, the issue is :

we may miss setting the broken.

So if we did get callbacks, we'd be able to test broken flag in the callback.

Yes, getting callbacks is fine.
But when the device is surprise removed, we wont get the callbacks and completions are missed.

exactly and then we should trigger them ourselves.

Quoted 48 lines
Quoted 11 lines
Quoted 1 line

Without graceful time out it is straight forward code, just rearrangement of

APIs in this patch with existing code.

Quoted 2 lines

The question is : it is really if we really care for that grace period when the

device or driver is already on its exit path and VQ is not broken.

Quoted 2 lines

If we don't wait for the request in progress, is it ok?

If we are talking about physical hardware, it seems quite possible that removal triggers then user gets impatient and yanks the card out.

Yes, regardless of surprise or not, completing the remaining IOs is just good enough.
Device is anyway on its exit path, so completing 10 commands vs 12, does not make a lot of difference with extra complexity of timeout.

So better to not complicate the driver, at least not when adding Fixes tag patch.

Quoted 32 lines
Quoted 18 lines
Quoted 18 lines

interrupt handling core is not making it easy for us - we must disable real interrupts if we do, and in the past we failed to do it. See e.g.

commit eb4cecb453a19b34d5454b49532e09e9cb0c1529

Author: Jason Wang <[email protected]>
Date:   Wed Mar 23 11:15:24 2022 +0800

Revert "virtio_pci: harden MSI-X interrupts"

This reverts commit 9e35276a5344f74d4a3600fc4100b3dd251d5c56.
Issue
were reported for the drivers that are using affinity managed IRQ where manually toggling IRQ status is not expected. And we forget to enable the interrupts in the restore path as well.

Quoted 8 lines
Quoted 8 lines

In the future, we will rework on the interrupt hardening.

Fixes 9e35276a5344 ("virtio_pci: harden MSI-X interrupts")

If someone can figure out a way to make toggling interrupt state play nice with affinity managed interrupts, that would solve a host of

issues I feel.

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

Thanks,
Ming

#8 lore raw
In reply to Parav Pandit
Quoted 70 lines · Parav Pandit

When the PCI device is surprise removed, requests won't complete from the device. These IOs are never completed and disk deletion hangs indefinitely.

Fix it by aborting the IOs which the device will never complete when the VQ is broken.

With this fix now fio completes swiftly.
An alternative of IO timeout has been considered, however when the driver knows about unresponsive block device, swiftly clearing them enables users and upper layers to react quickly.

Verified with multiple device unplug cycles with pending IOs in virtio used ring and some pending with device.

In future instead of VQ broken, a more elegant method can be used. At the moment the patch is kept to its minimal changes given its urgency to fix broken kernels.

Fixes 43bb40c5b926 ("virtio_pci: Support surprise removal of virtio pci device")
Reported-by [email protected]
---
 drivers/block/virtio_blk.c | 54 ++++++++++++++++++++++++++++++++++++++
 1 file changed, 54 insertions(+)
drivers/block/virtio_blk.c +32 -0
index 2bf14a0e2815..59b49899b229 100644
@@ -1562,10 +1562,64 @@ static int virtblk_probe(struct virtio_device *vdev)
 	return err;
 }
 
+static bool virtblk_cancel_request(struct request *rq, void *data)
+{
+	struct virtblk_req *vbr = blk_mq_rq_to_pdu(rq);
+
+	vbr->in_hdr.status = VIRTIO_BLK_S_IOERR;
+	if (blk_mq_request_started(rq) && !blk_mq_request_completed(rq))
+		blk_mq_complete_request(rq);
+
+	return true;
+}
+
+static void virtblk_cleanup_reqs(struct virtio_blk *vblk)
+{
+	struct virtio_blk_vq *blk_vq;
+	struct request_queue *q;
+	struct virtqueue *vq;
+	unsigned long flags;
+	int i;
+
+	vq = vblk->vqs[0].vq;
+	if (!virtqueue_is_broken(vq))
+		return;
+
+	q = vblk->disk->queue;
+	/* Block upper layer to not get any new requests */
+	blk_mq_quiesce_queue(q);
+
+	for (i = 0; i < vblk->num_vqs; i++) {
+		blk_vq = &vblk->vqs[i];
+
+		/* Synchronize with any ongoing virtblk_poll() which may be
+		 * completing the requests to uppper layer which has already

drivers/block/virtio_blk.c static int virtblk_probe(struct virtio_device *vdev) · line 1606

Quoted 10 lines
drivers/block/virtio_blk.c +10 -0
+		 * crossed the broken vq check.
+		 */
+		spin_lock_irqsave(&blk_vq->lock, flags);
+		spin_unlock_irqrestore(&blk_vq->lock, flags);
+	}
+
+	blk_sync_queue(q);
+
+	/* Complete remaining pending requests with error */
+	blk_mq_tagset_busy_iter(&vblk->tag_set, virtblk_cancel_request, vblk);

Interrupts can still occur here. What prevents the race between virtblk_cancel_request() and virtblk_request_done()?

Quoted 22 lines
drivers/block/virtio_blk.c +12 -0
+	blk_mq_tagset_wait_completed_request(&vblk->tag_set);
+
+	/*
+	 * Unblock any pending dispatch I/Os before we destroy device. From
+	 * del_gendisk() -> __blk_mark_disk_dead(disk) will set GD_DEAD flag,
+	 * that will make sure any new I/O from bio_queue_enter() to fail.
+	 */
+	blk_mq_unquiesce_queue(q);
+}
+
 static void virtblk_remove(struct virtio_device *vdev)
 {
 	struct virtio_blk *vblk = vdev->priv;
 
+	virtblk_cleanup_reqs(vblk);
+
 	/* Make sure no work handler is accessing the device. */
 	flush_work(&vblk->config_work);
 
Signature · 1 line
2.34.1
#9 lore raw
Quoted 86 lines

From: Stefan Hajnoczi <[email protected]>
Sent: Wednesday, February 21, 2024 3:35 AM
To: Parav Pandit <[email protected]>

drivers/block/virtio_blk.c static int virtblk_probe(struct virtio_device

Quoted 39 lines · Parav Pandit

When the PCI device is surprise removed, requests won't complete from the device. These IOs are never completed and disk deletion hangs indefinitely.

Fix it by aborting the IOs which the device will never complete when the VQ is broken.

With this fix now fio completes swiftly.
An alternative of IO timeout has been considered, however when the driver knows about unresponsive block device, swiftly clearing them enables users and upper layers to react quickly.

Verified with multiple device unplug cycles with pending IOs in virtio used ring and some pending with device.

In future instead of VQ broken, a more elegant method can be used. At the moment the patch is kept to its minimal changes given its urgency to fix broken kernels.

Fixes 43bb40c5b926 ("virtio_pci: Support surprise removal of virtio

pci device")

Closes:
https://lore.kernel.org/virtualization/c45dd68698cd47238c55fb73ca9b474
[email protected]/

---
 drivers/block/virtio_blk.c | 54
++++++++++++++++++++++++++++++++++++++
 1 file changed, 54 insertions(+)
drivers/block/virtio_blk.c +0 -0
index 2bf14a0e2815..59b49899b229 100644
@@ -1562,10 +1562,64 @@ static int virtblk_probe(struct virtio_device

*vdev)

Quoted 39 lines
drivers/block/virtio_blk.c +36 -0
 	return err;
 }
 
+static bool virtblk_cancel_request(struct request *rq, void *data) {
+	struct virtblk_req *vbr = blk_mq_rq_to_pdu(rq);
+
+	vbr->in_hdr.status = VIRTIO_BLK_S_IOERR;
+	if (blk_mq_request_started(rq) && !blk_mq_request_completed(rq))
+		blk_mq_complete_request(rq);
+
+	return true;
+}
+
+static void virtblk_cleanup_reqs(struct virtio_blk *vblk) {
+	struct virtio_blk_vq *blk_vq;
+	struct request_queue *q;
+	struct virtqueue *vq;
+	unsigned long flags;
+	int i;
+
+	vq = vblk->vqs[0].vq;
+	if (!virtqueue_is_broken(vq))
+		return;
+
+	q = vblk->disk->queue;
+	/* Block upper layer to not get any new requests */
+	blk_mq_quiesce_queue(q);
+
+	for (i = 0; i < vblk->num_vqs; i++) {
+		blk_vq = &vblk->vqs[i];
+
+		/* Synchronize with any ongoing virtblk_poll() which may be
+		 * completing the requests to uppper layer which has already
+		 * crossed the broken vq check.
+		 */
+		spin_lock_irqsave(&blk_vq->lock, flags);
+		spin_unlock_irqrestore(&blk_vq->lock, flags);
+	}
+
Quoted 9 lines

drivers/block/virtio_blk.c static int virtblk_probe(struct virtio_device *vdev)

Quoted 5 lines
drivers/block/virtio_blk.c +5 -0
+	blk_sync_queue(q);
+
+	/* Complete remaining pending requests with error */
+	blk_mq_tagset_busy_iter(&vblk->tag_set, virtblk_cancel_request,
+vblk);

Interrupts can still occur here. What prevents the race between virtblk_cancel_request() and virtblk_request_done()?

The PCI device which generates the interrupt is already removed so interrupt shouldn't arrive when executing cancel_request. (This is ignoring the race that Ming pointed out. I am preparing the v1 that eliminates such condition.)

If there was ongoing virtblk_request_done() is synchronized by the for loop above.

Quoted 22 lines

drivers/block/virtio_blk.c static int virtblk_probe(struct virtio_device *vdev)

Quoted 5 lines
drivers/block/virtio_blk.c +5 -0
+	blk_mq_tagset_wait_completed_request(&vblk->tag_set);
+
+	/*
+	 * Unblock any pending dispatch I/Os before we destroy device. From
+	 * del_gendisk() -> __blk_mark_disk_dead(disk) will set GD_DEAD

flag,

Quoted 16 lines
drivers/block/virtio_blk.c +7 -1
+	 * that will make sure any new I/O from bio_queue_enter() to fail.
+	 */
+	blk_mq_unquiesce_queue(q);
+}
+
 static void virtblk_remove(struct virtio_device *vdev)  {
 	struct virtio_blk *vblk = vdev->priv;
 
+	virtblk_cleanup_reqs(vblk);
+
 	/* Make sure no work handler is accessing the device. */
 	flush_work(&vblk->config_work);
 
--

2.34.1

#10 lore raw
Quoted 95 lines · Parav Pandit
Quoted 86 lines

From: Stefan Hajnoczi <[email protected]>
Sent: Wednesday, February 21, 2024 3:35 AM
To: Parav Pandit <[email protected]>

drivers/block/virtio_blk.c static int virtblk_probe(struct virtio_device

Quoted 39 lines · Parav Pandit

When the PCI device is surprise removed, requests won't complete from the device. These IOs are never completed and disk deletion hangs indefinitely.

Fix it by aborting the IOs which the device will never complete when the VQ is broken.

With this fix now fio completes swiftly.
An alternative of IO timeout has been considered, however when the driver knows about unresponsive block device, swiftly clearing them enables users and upper layers to react quickly.

Verified with multiple device unplug cycles with pending IOs in virtio used ring and some pending with device.

In future instead of VQ broken, a more elegant method can be used. At the moment the patch is kept to its minimal changes given its urgency to fix broken kernels.

Fixes 43bb40c5b926 ("virtio_pci: Support surprise removal of virtio

pci device")

Closes:
https://lore.kernel.org/virtualization/c45dd68698cd47238c55fb73ca9b474
[email protected]/

---
 drivers/block/virtio_blk.c | 54
++++++++++++++++++++++++++++++++++++++
 1 file changed, 54 insertions(+)
drivers/block/virtio_blk.c +0 -0
index 2bf14a0e2815..59b49899b229 100644
@@ -1562,10 +1562,64 @@ static int virtblk_probe(struct virtio_device

*vdev)

Quoted 39 lines
drivers/block/virtio_blk.c +36 -0
 	return err;
 }
 
+static bool virtblk_cancel_request(struct request *rq, void *data) {
+	struct virtblk_req *vbr = blk_mq_rq_to_pdu(rq);
+
+	vbr->in_hdr.status = VIRTIO_BLK_S_IOERR;
+	if (blk_mq_request_started(rq) && !blk_mq_request_completed(rq))
+		blk_mq_complete_request(rq);
+
+	return true;
+}
+
+static void virtblk_cleanup_reqs(struct virtio_blk *vblk) {
+	struct virtio_blk_vq *blk_vq;
+	struct request_queue *q;
+	struct virtqueue *vq;
+	unsigned long flags;
+	int i;
+
+	vq = vblk->vqs[0].vq;
+	if (!virtqueue_is_broken(vq))
+		return;
+
+	q = vblk->disk->queue;
+	/* Block upper layer to not get any new requests */
+	blk_mq_quiesce_queue(q);
+
+	for (i = 0; i < vblk->num_vqs; i++) {
+		blk_vq = &vblk->vqs[i];
+
+		/* Synchronize with any ongoing virtblk_poll() which may be
+		 * completing the requests to uppper layer which has already
+		 * crossed the broken vq check.
+		 */
+		spin_lock_irqsave(&blk_vq->lock, flags);
+		spin_unlock_irqrestore(&blk_vq->lock, flags);
+	}
+
Quoted 9 lines

drivers/block/virtio_blk.c static int virtblk_probe(struct virtio_device *vdev)

Quoted 5 lines
drivers/block/virtio_blk.c +5 -0
+	blk_sync_queue(q);
+
+	/* Complete remaining pending requests with error */
+	blk_mq_tagset_busy_iter(&vblk->tag_set, virtblk_cancel_request,
+vblk);

Interrupts can still occur here. What prevents the race between virtblk_cancel_request() and virtblk_request_done()?

Quoted 4 lines

The PCI device which generates the interrupt is already removed so interrupt shouldn't arrive when executing cancel_request. (This is ignoring the race that Ming pointed out. I am preparing the v1 that eliminates such condition.)

If there was ongoing virtblk_request_done() is synchronized by the for loop above.

Ah, I see now that:

+if (!virtqueue_is_broken(vq))
+ return;

relates to:

static void virtio_pci_remove(struct pci_dev *pci_dev)

{
	struct virtio_pci_device *vp_dev = pci_get_drvdata(pci_dev);
	struct device *dev = get_device(&vp_dev->vdev.dev);

	/*
	 * Device is marked broken on surprise removal so that virtio upper
	 * layers can abort any ongoing operation.
	 */
	if (!pci_device_is_present(pci_dev))
		virtio_break_device(&vp_dev->vdev);

Please rename virtblk_cleanup_reqs() to virtblk_cleanup_broken_device() or similar so it's clear that this function only applies when the device is broken? For example, it won't handle ACPI hot unplug requests because the device will still be present.

Thanks,
Stefan

Quoted 25 lines
Quoted 24 lines

drivers/block/virtio_blk.c static int virtblk_probe(struct virtio_device *vdev)

Quoted 7 lines
drivers/block/virtio_blk.c +5 -0
+	blk_mq_tagset_wait_completed_request(&vblk->tag_set);
+
+	/*
+	 * Unblock any pending dispatch I/Os before we destroy device. From
+	 * del_gendisk() -> __blk_mark_disk_dead(disk) will set GD_DEAD

flag,

Quoted 16 lines
drivers/block/virtio_blk.c +7 -1
+	 * that will make sure any new I/O from bio_queue_enter() to fail.
+	 */
+	blk_mq_unquiesce_queue(q);
+}
+
 static void virtblk_remove(struct virtio_device *vdev)  {
 	struct virtio_blk *vblk = vdev->priv;
 
+	virtblk_cleanup_reqs(vblk);
+
 	/* Make sure no work handler is accessing the device. */
 	flush_work(&vblk->config_work);
 
--

2.34.1

#11 lore raw
Quoted 109 lines · Stefan Hajnoczi
Quoted 95 lines · Parav Pandit
Quoted 86 lines

From: Stefan Hajnoczi <[email protected]>
Sent: Wednesday, February 21, 2024 3:35 AM
To: Parav Pandit <[email protected]>

drivers/block/virtio_blk.c static int virtblk_probe(struct virtio_device

Quoted 39 lines · Parav Pandit

When the PCI device is surprise removed, requests won't complete from the device. These IOs are never completed and disk deletion hangs indefinitely.

Fix it by aborting the IOs which the device will never complete when the VQ is broken.

With this fix now fio completes swiftly.
An alternative of IO timeout has been considered, however when the driver knows about unresponsive block device, swiftly clearing them enables users and upper layers to react quickly.

Verified with multiple device unplug cycles with pending IOs in virtio used ring and some pending with device.

In future instead of VQ broken, a more elegant method can be used. At the moment the patch is kept to its minimal changes given its urgency to fix broken kernels.

Fixes 43bb40c5b926 ("virtio_pci: Support surprise removal of virtio

pci device")

Closes:
https://lore.kernel.org/virtualization/c45dd68698cd47238c55fb73ca9b474
[email protected]/

---
 drivers/block/virtio_blk.c | 54
++++++++++++++++++++++++++++++++++++++
 1 file changed, 54 insertions(+)
drivers/block/virtio_blk.c +0 -0
index 2bf14a0e2815..59b49899b229 100644
@@ -1562,10 +1562,64 @@ static int virtblk_probe(struct virtio_device

*vdev)

Quoted 39 lines
drivers/block/virtio_blk.c +36 -0
 	return err;
 }
 
+static bool virtblk_cancel_request(struct request *rq, void *data) {
+	struct virtblk_req *vbr = blk_mq_rq_to_pdu(rq);
+
+	vbr->in_hdr.status = VIRTIO_BLK_S_IOERR;
+	if (blk_mq_request_started(rq) && !blk_mq_request_completed(rq))
+		blk_mq_complete_request(rq);
+
+	return true;
+}
+
+static void virtblk_cleanup_reqs(struct virtio_blk *vblk) {
+	struct virtio_blk_vq *blk_vq;
+	struct request_queue *q;
+	struct virtqueue *vq;
+	unsigned long flags;
+	int i;
+
+	vq = vblk->vqs[0].vq;
+	if (!virtqueue_is_broken(vq))
+		return;
+
+	q = vblk->disk->queue;
+	/* Block upper layer to not get any new requests */
+	blk_mq_quiesce_queue(q);
+
+	for (i = 0; i < vblk->num_vqs; i++) {
+		blk_vq = &vblk->vqs[i];
+
+		/* Synchronize with any ongoing virtblk_poll() which may be
+		 * completing the requests to uppper layer which has already
+		 * crossed the broken vq check.
+		 */
+		spin_lock_irqsave(&blk_vq->lock, flags);
+		spin_unlock_irqrestore(&blk_vq->lock, flags);
+	}
+
Quoted 9 lines

drivers/block/virtio_blk.c static int virtblk_probe(struct virtio_device *vdev)

Quoted 5 lines
drivers/block/virtio_blk.c +5 -0
+	blk_sync_queue(q);
+
+	/* Complete remaining pending requests with error */
+	blk_mq_tagset_busy_iter(&vblk->tag_set, virtblk_cancel_request,
+vblk);

Interrupts can still occur here. What prevents the race between virtblk_cancel_request() and virtblk_request_done()?

Quoted 4 lines

The PCI device which generates the interrupt is already removed so interrupt shouldn't arrive when executing cancel_request. (This is ignoring the race that Ming pointed out. I am preparing the v1 that eliminates such condition.)

If there was ongoing virtblk_request_done() is synchronized by the for loop above.

Ah, I see now that:

drivers/block/virtio_blk.c +2 -0
+if (!virtqueue_is_broken(vq))
+    return;

relates to:

static void virtio_pci_remove(struct pci_dev *pci_dev)

Quoted 10 lines
{
	struct virtio_pci_device *vp_dev = pci_get_drvdata(pci_dev);
	struct device *dev = get_device(&vp_dev->vdev.dev);

	/*
	 * Device is marked broken on surprise removal so that virtio upper
	 * layers can abort any ongoing operation.
	 */
	if (!pci_device_is_present(pci_dev))
		virtio_break_device(&vp_dev->vdev);

It's not 100% reliable though. We did it opportunistically but if you suddenly want to rely on it then you need to also synchronize callbacks.

Quoted 33 lines

Please rename virtblk_cleanup_reqs() to virtblk_cleanup_broken_device() or similar so it's clear that this function only applies when the device is broken? For example, it won't handle ACPI hot unplug requests because the device will still be present.

Thanks,
Stefan

Quoted 25 lines
Quoted 24 lines

drivers/block/virtio_blk.c static int virtblk_probe(struct virtio_device *vdev)

Quoted 7 lines
drivers/block/virtio_blk.c +5 -0
+	blk_mq_tagset_wait_completed_request(&vblk->tag_set);
+
+	/*
+	 * Unblock any pending dispatch I/Os before we destroy device. From
+	 * del_gendisk() -> __blk_mark_disk_dead(disk) will set GD_DEAD

flag,

Quoted 16 lines
drivers/block/virtio_blk.c +7 -1
+	 * that will make sure any new I/O from bio_queue_enter() to fail.
+	 */
+	blk_mq_unquiesce_queue(q);
+}
+
 static void virtblk_remove(struct virtio_device *vdev)  {
 	struct virtio_blk *vblk = vdev->priv;
 
+	virtblk_cleanup_reqs(vblk);
+
 	/* Make sure no work handler is accessing the device. */
 	flush_work(&vblk->config_work);
 
--

2.34.1

#12 lore raw
In reply to Parav Pandit
Quoted 95 lines · Parav Pandit
Quoted 86 lines

From: Stefan Hajnoczi <[email protected]>
Sent: Wednesday, February 21, 2024 3:35 AM
To: Parav Pandit <[email protected]>

drivers/block/virtio_blk.c static int virtblk_probe(struct virtio_device

Quoted 39 lines · Parav Pandit

When the PCI device is surprise removed, requests won't complete from the device. These IOs are never completed and disk deletion hangs indefinitely.

Fix it by aborting the IOs which the device will never complete when the VQ is broken.

With this fix now fio completes swiftly.
An alternative of IO timeout has been considered, however when the driver knows about unresponsive block device, swiftly clearing them enables users and upper layers to react quickly.

Verified with multiple device unplug cycles with pending IOs in virtio used ring and some pending with device.

In future instead of VQ broken, a more elegant method can be used. At the moment the patch is kept to its minimal changes given its urgency to fix broken kernels.

Fixes 43bb40c5b926 ("virtio_pci: Support surprise removal of virtio

pci device")

Closes:
https://lore.kernel.org/virtualization/c45dd68698cd47238c55fb73ca9b474
[email protected]/

---
 drivers/block/virtio_blk.c | 54
++++++++++++++++++++++++++++++++++++++
 1 file changed, 54 insertions(+)
drivers/block/virtio_blk.c +0 -0
index 2bf14a0e2815..59b49899b229 100644
@@ -1562,10 +1562,64 @@ static int virtblk_probe(struct virtio_device

*vdev)

Quoted 39 lines
drivers/block/virtio_blk.c +36 -0
 	return err;
 }
 
+static bool virtblk_cancel_request(struct request *rq, void *data) {
+	struct virtblk_req *vbr = blk_mq_rq_to_pdu(rq);
+
+	vbr->in_hdr.status = VIRTIO_BLK_S_IOERR;
+	if (blk_mq_request_started(rq) && !blk_mq_request_completed(rq))
+		blk_mq_complete_request(rq);
+
+	return true;
+}
+
+static void virtblk_cleanup_reqs(struct virtio_blk *vblk) {
+	struct virtio_blk_vq *blk_vq;
+	struct request_queue *q;
+	struct virtqueue *vq;
+	unsigned long flags;
+	int i;
+
+	vq = vblk->vqs[0].vq;
+	if (!virtqueue_is_broken(vq))
+		return;
+
+	q = vblk->disk->queue;
+	/* Block upper layer to not get any new requests */
+	blk_mq_quiesce_queue(q);
+
+	for (i = 0; i < vblk->num_vqs; i++) {
+		blk_vq = &vblk->vqs[i];
+
+		/* Synchronize with any ongoing virtblk_poll() which may be
+		 * completing the requests to uppper layer which has already
+		 * crossed the broken vq check.
+		 */
+		spin_lock_irqsave(&blk_vq->lock, flags);
+		spin_unlock_irqrestore(&blk_vq->lock, flags);
+	}
+
Quoted 9 lines

drivers/block/virtio_blk.c static int virtblk_probe(struct virtio_device *vdev)

Quoted 5 lines
drivers/block/virtio_blk.c +5 -0
+	blk_sync_queue(q);
+
+	/* Complete remaining pending requests with error */
+	blk_mq_tagset_busy_iter(&vblk->tag_set, virtblk_cancel_request,
+vblk);

Interrupts can still occur here. What prevents the race between virtblk_cancel_request() and virtblk_request_done()?

Quoted 5 lines

The PCI device which generates the interrupt is already removed so interrupt shouldn't arrive when executing cancel_request. (This is ignoring the race that Ming pointed out. I am preparing the v1 that eliminates such condition.)

If there was ongoing virtblk_request_done() is synchronized by the for loop above.

Yes, this works, but I feel this is very subtle. This is why I am asking whether we should instead call virtio_synchronize_cbs and then just invoke all the callbacks one last time from virtio core?

Quoted 22 lines
Quoted 22 lines

drivers/block/virtio_blk.c static int virtblk_probe(struct virtio_device *vdev)

Quoted 5 lines
drivers/block/virtio_blk.c +5 -0
+	blk_mq_tagset_wait_completed_request(&vblk->tag_set);
+
+	/*
+	 * Unblock any pending dispatch I/Os before we destroy device. From
+	 * del_gendisk() -> __blk_mark_disk_dead(disk) will set GD_DEAD

flag,

Quoted 16 lines
drivers/block/virtio_blk.c +7 -1
+	 * that will make sure any new I/O from bio_queue_enter() to fail.
+	 */
+	blk_mq_unquiesce_queue(q);
+}
+
 static void virtblk_remove(struct virtio_device *vdev)  {
 	struct virtio_blk *vblk = vdev->priv;
 
+	virtblk_cleanup_reqs(vblk);
+
 	/* Make sure no work handler is accessing the device. */
 	flush_work(&vblk->config_work);
 
--

2.34.1