Skip to contentSkip to conversation

vp_vdpa: harden the logic of set status

PATCH Superseded Longpeng(Mike) 3 participants 4 messages Dec 6, 2022 – Dec 7, 2022 virtio core

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 vp_vdpa: harden the logic of set status

From: Longpeng <[email protected]>

1. We should not set status to 0 when invoking vp_vdpa_set_status(), trigger a warning in that case.

2. The driver MUST wait for a read of device_status to return 0 before reinitializing the device. But we also don't want to keep us in an infinite loop forever, so wait for 5s if we try to reset the device.

---
Changes v1->v2:
- use WARN_ON instead of BUG_ON. [Stefano]
- use "warning + failed" instead of "infinite loop". [Jason, Stefano]
- use usleep_range instead of msleep (checkpatch). [Longpeng]

---
 drivers/vdpa/virtio_pci/vp_vdpa.c | 22 ++++++++++++++++++++++
 1 file changed, 22 insertions(+)
drivers/vdpa/virtio_pci/vp_vdpa.c +22 -0
index 13701c2a1963..a2d3b13ac646 100644
@@ -214,6 +214,9 @@ static void vp_vdpa_set_status(struct vdpa_device *vdpa, u8 status)
 	struct virtio_pci_modern_device *mdev = vp_vdpa_to_mdev(vp_vdpa);
 	u8 s = vp_vdpa_get_status(vdpa);
 
+	/* We should never be setting status to 0. */
+	WARN_ON(status == 0);
+
 	if (status & VIRTIO_CONFIG_S_DRIVER_OK &&
 	    !(s & VIRTIO_CONFIG_S_DRIVER_OK)) {
 		vp_vdpa_request_irq(vp_vdpa);
@@ -222,14 +225,33 @@ static void vp_vdpa_set_status(struct vdpa_device *vdpa, u8 status)
 	vp_modern_set_status(mdev, status);
 }
 
+#define VP_VDPA_RESET_TMOUT_MS 5000 /* 5s */
+
 static int vp_vdpa_reset(struct vdpa_device *vdpa, bool clear)
 {
 	struct vp_vdpa *vp_vdpa = vdpa_to_vp(vdpa);
 	struct virtio_pci_modern_device *mdev = vp_vdpa_to_mdev(vp_vdpa);
 	u8 s = vp_vdpa_get_status(vdpa);
+	unsigned long timeout;
 
 	vp_modern_set_status(mdev, 0);
 
+	/*
+	 * As the virtio v1.1 spec (4.1.4.3.2) says: After writing 0 to
+	 * device_status, the driver MUST wait for a read of device_status
+	 * to return 0 before reinitializing the device.
+	 * To avoid keep us here forever, we only wait for 5 seconds.
+	 */
+	timeout = jiffies + msecs_to_jiffies(VP_VDPA_RESET_TMOUT_MS);
+	while (vp_modern_get_status(mdev)) {
+		usleep_range(1000, 1500);
+		if (time_after(jiffies, timeout)) {
+			dev_err(&mdev->pci_dev->dev,
+				"vp_vdpa: fail to reset device\n");
+			return -ETIMEDOUT;
+		}
+	}
+
 	if (s & VIRTIO_CONFIG_S_DRIVER_OK)
 		vp_vdpa_free_irq(vp_vdpa);
 
Signature · 1 line
2.23.0
#1 lore raw
Quoted 45 lines · Longpeng(Mike)

From: Longpeng <[email protected]>

1. We should not set status to 0 when invoking vp_vdpa_set_status(), trigger a warning in that case.

2. The driver MUST wait for a read of device_status to return 0 before reinitializing the device. But we also don't want to keep us in an infinite loop forever, so wait for 5s if we try to reset the device.

---
Changes v1->v2:
- use WARN_ON instead of BUG_ON. [Stefano]
- use "warning + failed" instead of "infinite loop". [Jason, Stefano]
- use usleep_range instead of msleep (checkpatch). [Longpeng]

---
 drivers/vdpa/virtio_pci/vp_vdpa.c | 22 ++++++++++++++++++++++
 1 file changed, 22 insertions(+)
drivers/vdpa/virtio_pci/vp_vdpa.c +5 -0
index 13701c2a1963..a2d3b13ac646 100644
@@ -214,6 +214,9 @@ static void vp_vdpa_set_status(struct vdpa_device *vdpa, u8 status)
        struct virtio_pci_modern_device *mdev = vp_vdpa_to_mdev(vp_vdpa);
        u8 s = vp_vdpa_get_status(vdpa);
 
+       /* We should never be setting status to 0. */
+       WARN_ON(status == 0);
+
        if (status & VIRTIO_CONFIG_S_DRIVER_OK &&
            !(s & VIRTIO_CONFIG_S_DRIVER_OK)) {
                vp_vdpa_request_irq(vp_vdpa);
@@ -222,14 +225,33 @@ static void vp_vdpa_set_status(struct vdpa_device *vdpa, u8 status)
        vp_modern_set_status(mdev, status);
 }
 
+#define VP_VDPA_RESET_TMOUT_MS 5000 /* 5s */
+
 static int vp_vdpa_reset(struct vdpa_device *vdpa, bool clear)
 {
        struct vp_vdpa *vp_vdpa = vdpa_to_vp(vdpa);
        struct virtio_pci_modern_device *mdev = vp_vdpa_to_mdev(vp_vdpa);

drivers/vdpa/virtio_pci/vp_vdpa.c static void vp_vdpa_set_status(struct vdpa_device *vdpa, u8 status) · line 243

Quoted 10 lines
drivers/vdpa/virtio_pci/vp_vdpa.c +6 -0
        u8 s = vp_vdpa_get_status(vdpa);
+       unsigned long timeout;
 
        vp_modern_set_status(mdev, 0);
 
+       /*
+        * As the virtio v1.1 spec (4.1.4.3.2) says: After writing 0 to
+        * device_status, the driver MUST wait for a read of device_status
+        * to return 0 before reinitializing the device.
+        * To avoid keep us here forever, we only wait for 5 seconds.

s/keep/keeping/

drivers/vdpa/virtio_pci/vp_vdpa.c static void vp_vdpa_set_status(struct vdpa_device *vdpa, u8 status) · line 252

Quoted 9 lines
drivers/vdpa/virtio_pci/vp_vdpa.c +9 -0
+        */
+       timeout = jiffies + msecs_to_jiffies(VP_VDPA_RESET_TMOUT_MS);
+       while (vp_modern_get_status(mdev)) {
+               usleep_range(1000, 1500);
+               if (time_after(jiffies, timeout)) {
+                       dev_err(&mdev->pci_dev->dev,
+                               "vp_vdpa: fail to reset device\n");
+                       return -ETIMEDOUT;
+               }

Any chance to use readx_poll_timeout() here?

Thanks

Quoted 8 lines
drivers/vdpa/virtio_pci/vp_vdpa.c +2 -1
+       }
+
        if (s & VIRTIO_CONFIG_S_DRIVER_OK)
                vp_vdpa_free_irq(vp_vdpa);
 
--

2.23.0

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

#2 lore raw
In reply to Longpeng(Mike)
Quoted 35 lines · Longpeng(Mike)

From: Longpeng <[email protected]>

1. We should not set status to 0 when invoking vp_vdpa_set_status(), trigger a warning in that case.

2. The driver MUST wait for a read of device_status to return 0 before reinitializing the device. But we also don't want to keep us in an infinite loop forever, so wait for 5s if we try to reset the device.

---
Changes v1->v2:
- use WARN_ON instead of BUG_ON. [Stefano]
- use "warning + failed" instead of "infinite loop". [Jason, Stefano]
- use usleep_range instead of msleep (checkpatch). [Longpeng]

---
drivers/vdpa/virtio_pci/vp_vdpa.c | 22 ++++++++++++++++++++++

1 file changed, 22 insertions(+)
drivers/vdpa/virtio_pci/vp_vdpa.c +3 -0
index 13701c2a1963..a2d3b13ac646 100644
@@ -214,6 +214,9 @@ static void vp_vdpa_set_status(struct vdpa_device *vdpa, u8 status)
 	struct virtio_pci_modern_device *mdev = vp_vdpa_to_mdev(vp_vdpa);
 	u8 s = vp_vdpa_get_status(vdpa);
 
+	/* We should never be setting status to 0. */
+	WARN_ON(status == 0);
+
 	if (status & VIRTIO_CONFIG_S_DRIVER_OK &&
 	    !(s & VIRTIO_CONFIG_S_DRIVER_OK)) {
 		vp_vdpa_request_irq(vp_vdpa);

drivers/vdpa/virtio_pci/vp_vdpa.c static void vp_vdpa_set_status(struct vdpa_device *vdpa, u8 status) · line 228

Quoted 4 lines
drivers/vdpa/virtio_pci/vp_vdpa.c +1 -0
@@ -222,14 +225,33 @@ static void vp_vdpa_set_status(struct vdpa_device *vdpa, u8 status)
 	vp_modern_set_status(mdev, status);
 }
 
+#define VP_VDPA_RESET_TMOUT_MS 5000 /* 5s */

What about moving this define on top of this file? Near the other macro.

And I would use TIMEOUT entirely.

drivers/vdpa/virtio_pci/vp_vdpa.c static void vp_vdpa_set_status(struct vdpa_device *vdpa, u8 status) · line 240

Quoted 12 lines
drivers/vdpa/virtio_pci/vp_vdpa.c +4 -0
+
 static int vp_vdpa_reset(struct vdpa_device *vdpa, bool clear)
 {
 	struct vp_vdpa *vp_vdpa = vdpa_to_vp(vdpa);
 	struct virtio_pci_modern_device *mdev = vp_vdpa_to_mdev(vp_vdpa);
 	u8 s = vp_vdpa_get_status(vdpa);
+	unsigned long timeout;
 
 	vp_modern_set_status(mdev, 0);
 
+	/*
+	 * As the virtio v1.1 spec (4.1.4.3.2) says: After writing 0 to

I think we can refer to the lates v1.2 (the section should be the same).

Quoted 20 lines
drivers/vdpa/virtio_pci/vp_vdpa.c +14 -0
+	 * device_status, the driver MUST wait for a read of device_status
+	 * to return 0 before reinitializing the device.
+	 * To avoid keep us here forever, we only wait for 5 seconds.
+	 */
+	timeout = jiffies + msecs_to_jiffies(VP_VDPA_RESET_TMOUT_MS);
+	while (vp_modern_get_status(mdev)) {
+		usleep_range(1000, 1500);
+		if (time_after(jiffies, timeout)) {
+			dev_err(&mdev->pci_dev->dev,
+				"vp_vdpa: fail to reset device\n");
+			return -ETIMEDOUT;
+		}
+	}
+
 	if (s & VIRTIO_CONFIG_S_DRIVER_OK)
 		vp_vdpa_free_irq(vp_vdpa);
 
Signature · 1 line
2.23.0

The rest LGTM!

Thanks,
Stefano

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

#3 lore raw
Quoted 41 lines · Stefano Garzarella
Quoted 40 lines · Longpeng(Mike)

From: Longpeng <[email protected]>

1. We should not set status to 0 when invoking vp_vdpa_set_status(), trigger a warning in that case.

2. The driver MUST wait for a read of device_status to return 0 before reinitializing the device. But we also don't want to keep us in an infinite loop forever, so wait for 5s if we try to reset the device.

---
Changes v1->v2:
 - use WARN_ON instead of BUG_ON. [Stefano]
 - use "warning + failed" instead of "infinite loop". [Jason, Stefano]
 - use usleep_range instead of msleep (checkpatch). [Longpeng]

---
drivers/vdpa/virtio_pci/vp_vdpa.c | 22 ++++++++++++++++++++++

1 file changed, 22 insertions(+)
drivers/vdpa/virtio_pci/vp_vdpa.c +3 -0
index 13701c2a1963..a2d3b13ac646 100644
@@ -214,6 +214,9 @@ static void vp_vdpa_set_status(struct vdpa_device 
 *vdpa, u8 status)
     struct virtio_pci_modern_device *mdev = vp_vdpa_to_mdev(vp_vdpa);
     u8 s = vp_vdpa_get_status(vdpa);
 
+    /* We should never be setting status to 0. */
+    WARN_ON(status == 0);
+

    if (status & VIRTIO_CONFIG_S_DRIVER_OK &&
        !(s & VIRTIO_CONFIG_S_DRIVER_OK)) {
        vp_vdpa_request_irq(vp_vdpa);

drivers/vdpa/virtio_pci/vp_vdpa.c +0 -0
@@ -222,14 +225,33 @@ static void vp_vdpa_set_status(struct 
 vdpa_device *vdpa, u8 status)
     vp_modern_set_status(mdev, status);
 }
 
Quoted 7 lines

drivers/vdpa/virtio_pci/vp_vdpa.c static void vp_vdpa_set_status(struct vdpa_device *vdpa, u8 status) · line 228

Quoted 1 line
drivers/vdpa/virtio_pci/vp_vdpa.c +1 -0
+#define VP_VDPA_RESET_TMOUT_MS 5000 /* 5s */

What about moving this define on top of this file? Near the other macro.

And I would use TIMEOUT entirely.

Okay.

Quoted 8 lines
Quoted 8 lines
drivers/vdpa/virtio_pci/vp_vdpa.c +2 -0
+
 static int vp_vdpa_reset(struct vdpa_device *vdpa, bool clear)
 {
     struct vp_vdpa *vp_vdpa = vdpa_to_vp(vdpa);
     struct virtio_pci_modern_device *mdev = vp_vdpa_to_mdev(vp_vdpa);
     u8 s = vp_vdpa_get_status(vdpa);
+    unsigned long timeout;
 
Quoted 7 lines

drivers/vdpa/virtio_pci/vp_vdpa.c static void vp_vdpa_set_status(struct vdpa_device *vdpa, u8 status) · line 240

Quoted 4 lines
drivers/vdpa/virtio_pci/vp_vdpa.c +2 -0
     vp_modern_set_status(mdev, 0);
 
+    /*
+     * As the virtio v1.1 spec (4.1.4.3.2) says: After writing 0 to

I think we can refer to the lates v1.2 (the section should be the same).

Okay, will do in next version, thanks.

Quoted 28 lines
Quoted 20 lines
drivers/vdpa/virtio_pci/vp_vdpa.c +14 -0
+     * device_status, the driver MUST wait for a read of device_status
+     * to return 0 before reinitializing the device.
+     * To avoid keep us here forever, we only wait for 5 seconds.
+     */
+    timeout = jiffies + msecs_to_jiffies(VP_VDPA_RESET_TMOUT_MS);
+    while (vp_modern_get_status(mdev)) {
+        usleep_range(1000, 1500);
+        if (time_after(jiffies, timeout)) {
+            dev_err(&mdev->pci_dev->dev,
+                "vp_vdpa: fail to reset device\n");
+            return -ETIMEDOUT;
+        }
+    }
+
     if (s & VIRTIO_CONFIG_S_DRIVER_OK)
         vp_vdpa_free_irq(vp_vdpa);
Signature · 2 lines
-- 
2.23.0

The rest LGTM!

Thanks,
Stefano

.