Skip to contentSkip to conversation

vdpa/vp_vdpa: Wait for device reset to complete

PATCH In review Yuho Choi 3 participants 3 messages Nov 16 virtio core

lore
Share & tools
lore

Yuho Choi

author
#0 lore raw

PATCH v2 1/1 vdpa/vp_vdpa: Wait for device reset to complete

vp_vdpa_reset() writes 0 to device_status and returns success right away. The virtio specification requires the driver to wait until a read of device_status returns 0 before reinitializing the device, and virtio_pci_modern's vp_reset() does exactly that: the read-back flushes the posted status write and any in-flight device writes, including MSI-X messages.

Without it, vhost-vdpa (or virtio-vdpa) can go on to tear down the IOTLB/IOMMU mappings or reprogram the queues while the device has not yet finished its reset, and may still be DMAing to the old virtqueue addresses. The MSI-X vectors are also freed while an interrupt from the old configuration may still be in flight.

Poll device_status after writing 0, in the same way as vp_reset(), before freeing the IRQs and returning.

Fixes 64b9f64f80a6 ("vdpa: introduce virtio pci driver")

---
v2:
- Drop Cc: stable, as this has not been reproduced.
- Rebased on current mainline. No code change.

v1: https://lore.kernel.org/r/[email protected]

Like vp_reset(), the new loop has no bound, so a surprise-removed device that reads back all ones would keep it spinning (the Sashiko review of v1 pointed this out). I kept it the same as vp_reset() rather than handling that case only here.

Compile-tested only (x86_64 defconfig + VP_VDPA, W=1).

 drivers/vdpa/virtio_pci/vp_vdpa.c | 4 ++++
 1 file changed, 4 insertions(+)
drivers/vdpa/virtio_pci/vp_vdpa.c +4 -0
index 646efd9384fb..eba82ff253ea 100644
@@ -8,6 +8,7 @@
  * Based on virtio_pci_modern.c.
  */
 
+#include <linux/delay.h>
 #include <linux/interrupt.h>
 #include <linux/module.h>
 #include <linux/pci.h>
@@ -236,6 +237,9 @@ static int vp_vdpa_reset(struct vdpa_device *vdpa)
 	u8 s = vp_vdpa_get_status(vdpa);
 
 	vp_modern_set_status(mdev, 0);
+	/* Wait for the reset to complete */
+	while (vp_modern_get_status(mdev))
+		fsleep(1000);
 
 	if (s & VIRTIO_CONFIG_S_DRIVER_OK)
 		vp_vdpa_free_irq(vp_vdpa);

base-commit: 067c5c24cef23d913b67eaaf71ae67e7169a6620

Signature · 1 line
2.43.0
#1 lore raw
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [High] vdpa/vp_vdpa: infinite loop in vp_vdpa_reset() on surprise removal or fault --

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [High] vdpa/vp_vdpa: infinite loop in vp_vdpa_reset() on surprise removal or fault
--

commit 877ae67f265800ae2b653bd6932dd2f6fa55c959
Author: Yuho Choi <[email protected]>

vdpa/vp_vdpa: Wait for device reset to complete

This commit polls device_status in vp_vdpa_reset() until it reads back 0 before freeing the MSI-X IRQs and returning.

Quoted 4 lines
drivers/vdpa/virtio_pci/vp_vdpa.c +0 -0
index 646efd9384fba..eba82ff253ea3 100644

[ ... ]

drivers/vdpa/virtio_pci/vp_vdpa.c static int vp_vdpa_reset(struct vdpa_device *vdpa) · line 242

Quoted 7 lines
drivers/vdpa/virtio_pci/vp_vdpa.c +3 -0
@@ -236,6 +237,9 @@ static int vp_vdpa_reset(struct vdpa_device *vdpa)
 	u8 s = vp_vdpa_get_status(vdpa);
 
 	vp_modern_set_status(mdev, 0);
+	/* Wait for the reset to complete */
+	while (vp_modern_get_status(mdev))
+		fsleep(1000);

[Severity: High]
Can this loop sleep forever if the PCI device is surprise-removed or fails to reset?

On PCI hot-unplug, vp_vdpa_remove() unregisters the vDPA device, which triggers vdpa_reset() -> vp_vdpa_reset() while MMIO reads from cfg->device_status return 0xff on every iteration. Without a timeout or a check for pci_device_is_present(mdev->pci_dev), vp_vdpa_reset() hangs indefinitely while holding vdpa->cf_lock and blocks vp_vdpa_free_irq() and device teardown.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
#2 lore raw
In reply to Yuho Choi
Quoted 23 lines · Yuho Choi

vp_vdpa_reset() writes 0 to device_status and returns success right away. The virtio specification requires the driver to wait until a read of device_status returns 0 before reinitializing the device, and virtio_pci_modern's vp_reset() does exactly that: the read-back flushes the posted status write and any in-flight device writes, including MSI-X messages.

Without it, vhost-vdpa (or virtio-vdpa) can go on to tear down the IOTLB/IOMMU mappings or reprogram the queues while the device has not yet finished its reset, and may still be DMAing to the old virtqueue addresses. The MSI-X vectors are also freed while an interrupt from the old configuration may still be in flight.

Poll device_status after writing 0, in the same way as vp_reset(), before freeing the IRQs and returning.

Fixes 64b9f64f80a6 ("vdpa: introduce virtio pci driver")

---
v2:
- Drop Cc: stable, as this has not been reproduced.
- Rebased on current mainline. No code change.

Quoted 8 lines

v1: https://lore.kernel.org/r/[email protected]

Like vp_reset(), the new loop has no bound, so a surprise-removed device that reads back all ones would keep it spinning (the Sashiko review of v1 pointed this out). I kept it the same as vp_reset() rather than handling that case only here.

Compile-tested only (x86_64 defconfig + VP_VDPA, W=1).

If you can't bother testing, why should I bother merging?

Quoted 29 lines
 drivers/vdpa/virtio_pci/vp_vdpa.c | 4 ++++
 1 file changed, 4 insertions(+)
drivers/vdpa/virtio_pci/vp_vdpa.c +4 -0
index 646efd9384fb..eba82ff253ea 100644
@@ -8,6 +8,7 @@
  * Based on virtio_pci_modern.c.
  */
 
+#include <linux/delay.h>
 #include <linux/interrupt.h>
 #include <linux/module.h>
 #include <linux/pci.h>
@@ -236,6 +237,9 @@ static int vp_vdpa_reset(struct vdpa_device *vdpa)
 	u8 s = vp_vdpa_get_status(vdpa);
 
 	vp_modern_set_status(mdev, 0);
+	/* Wait for the reset to complete */
+	while (vp_modern_get_status(mdev))
+		fsleep(1000);
 
 	if (s & VIRTIO_CONFIG_S_DRIVER_OK)
 		vp_vdpa_free_irq(vp_vdpa);

base-commit: 067c5c24cef23d913b67eaaf71ae67e7169a6620

Signature · 1 line
2.43.0