Skip to contentSkip to conversation

vp_vdpa: harden the logic of set status

PATCH Superseded Longpeng(Mike) 3 participants 6 messages Nov 11, 2022 – Nov 14, 2022 virtio core

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH 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().

2. The driver MUST wait for a read of device_status to return 0 before reinitializing the device.

---
 drivers/vdpa/virtio_pci/vp_vdpa.c | 11 ++++++++++-
 1 file changed, 10 insertions(+), 1 deletion(-)
drivers/vdpa/virtio_pci/vp_vdpa.c +10 -1
index d448db0c4de3..d35fac5cde11 100644
@@ -212,8 +212,12 @@ static void vp_vdpa_set_status(struct vdpa_device *vdpa, u8 status)
 {
 	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);
+	u8 s;
+
+	/* We should never be setting status to 0. */
+	BUG_ON(status == 0);
 
+	s = vp_vdpa_get_status(vdpa);
 	if (status & VIRTIO_CONFIG_S_DRIVER_OK &&
 	    !(s & VIRTIO_CONFIG_S_DRIVER_OK)) {
 		vp_vdpa_request_irq(vp_vdpa);
@@ -229,6 +233,11 @@ static int vp_vdpa_reset(struct vdpa_device *vdpa)
 	u8 s = vp_vdpa_get_status(vdpa);
 
 	vp_modern_set_status(mdev, 0);
+	/* After writing 0 to device_status, the driver MUST wait for a read of
+	 * device_status to return 0 before reinitializing the device.
+	 */
+	while (vp_modern_get_status(mdev))
+		msleep(1);
 
 	if (s & VIRTIO_CONFIG_S_DRIVER_OK)
 		vp_vdpa_free_irq(vp_vdpa);
Signature · 1 line
2.23.0
#1 lore raw
Quoted 17 lines · Longpeng(Mike)

From: Longpeng <[email protected]>

1. We should not set status to 0 when invoking vp_vdpa_set_status().

2. The driver MUST wait for a read of device_status to return 0 before reinitializing the device.

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

1 file changed, 10 insertions(+), 1 deletion(-)
drivers/vdpa/virtio_pci/vp_vdpa.c +0 -0
index d448db0c4de3..d35fac5cde11 100644

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

Quoted 4 lines
drivers/vdpa/virtio_pci/vp_vdpa.c +0 -1
@@ -212,8 +212,12 @@ static void vp_vdpa_set_status(struct vdpa_device *vdpa, u8 status)
 {
 	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);

Is this change really needed?

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

Quoted 4 lines
drivers/vdpa/virtio_pci/vp_vdpa.c +4 -0
+	u8 s;
+
+	/* We should never be setting status to 0. */
+	BUG_ON(status == 0);

IMHO panicking the kernel seems excessive in this case, please use WARN_ON and maybe return earlier.

Quoted 6 lines
drivers/vdpa/virtio_pci/vp_vdpa.c +1 -0
+	s = vp_vdpa_get_status(vdpa);
 	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 int vp_vdpa_reset(struct vdpa_device *vdpa) · line 240

Quoted 8 lines
drivers/vdpa/virtio_pci/vp_vdpa.c +5 -0
@@ -229,6 +233,11 @@ static int vp_vdpa_reset(struct vdpa_device *vdpa)
 	u8 s = vp_vdpa_get_status(vdpa);
 
 	vp_modern_set_status(mdev, 0);
+	/* After writing 0 to device_status, the driver MUST wait for a read of
+	 * device_status to return 0 before reinitializing the device.
+	 */
+	while (vp_modern_get_status(mdev))
+		msleep(1);

Should we set a limit after which we give up? A malfunctioning device could keep us here forever.

Thanks,
Stefano

Quoted 6 lines
drivers/vdpa/virtio_pci/vp_vdpa.c +0 -0
 	if (s & VIRTIO_CONFIG_S_DRIVER_OK)
 		vp_vdpa_free_irq(vp_vdpa);
Signature · 1 line
2.23.0

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

#2 lore raw
Quoted 13 lines · Stefano Garzarella
Quoted 12 lines · Longpeng(Mike)

From: Longpeng <[email protected]>

1. We should not set status to 0 when invoking vp_vdpa_set_status().

2. The driver MUST wait for a read of device_status to return 0 before reinitializing the device.

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

1 file changed, 10 insertions(+), 1 deletion(-)
Quoted 13 lines

drivers/vdpa/virtio_pci/vp_vdpa.c static void vp_vdpa_set_status(struct vdpa_device · line 216

Quoted 10 lines
drivers/vdpa/virtio_pci/vp_vdpa.c +0 -1
index d448db0c4de3..d35fac5cde11 100644
@@ -212,8 +212,12 @@ static void vp_vdpa_set_status(struct vdpa_device 
 *vdpa, u8 status)
 {
     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);

Is this change really needed?

No need to get the status if we try to set status to 0 (trigger BUG).

Quoted 7 lines

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

Quoted 4 lines
drivers/vdpa/virtio_pci/vp_vdpa.c +4 -0
+    u8 s;
+
+    /* We should never be setting status to 0. */
+    BUG_ON(status == 0);

IMHO panicking the kernel seems excessive in this case, please use WARN_ON and maybe return earlier.

Um...I referenced the vp_reset/vp_set_status,

Quoted 8 lines
Quoted 8 lines
drivers/vdpa/virtio_pci/vp_vdpa.c +1 -0
+    s = vp_vdpa_get_status(vdpa);

    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
@@ -229,6 +233,11 @@ static int vp_vdpa_reset(struct vdpa_device *vdpa)
     u8 s = vp_vdpa_get_status(vdpa);
 
Quoted 10 lines
Quoted 7 lines
drivers/vdpa/virtio_pci/vp_vdpa.c +1 -0
     vp_modern_set_status(mdev, 0);
+    /* After writing 0 to device_status, the driver MUST wait for a 

read of
+     * device_status to return 0 before reinitializing the device. +     */
+    while (vp_modern_get_status(mdev))
+        msleep(1);

Should we set a limit after which we give up? A malfunctioning device could keep us here forever.

Yes, but the malfunctioning device maybe can not work anymore, how to handle it?

Quoted 11 lines

Thanks,
Stefano

Quoted 6 lines

    if (s & VIRTIO_CONFIG_S_DRIVER_OK)
        vp_vdpa_free_irq(vp_vdpa);

Signature · 1 line
2.23.0

.

#3 lore raw
Quoted 29 lines · Cloud Infrastructure Service Product Dept.)
Quoted 13 lines · Stefano Garzarella
Quoted 12 lines · Longpeng(Mike)

From: Longpeng <[email protected]>

1. We should not set status to 0 when invoking vp_vdpa_set_status().

2. The driver MUST wait for a read of device_status to return 0 before reinitializing the device.

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

1 file changed, 10 insertions(+), 1 deletion(-)
Quoted 13 lines

drivers/vdpa/virtio_pci/vp_vdpa.c static void vp_vdpa_set_status(struct · line 216

Quoted 10 lines
drivers/vdpa/virtio_pci/vp_vdpa.c +0 -1
index d448db0c4de3..d35fac5cde11 100644
@@ -212,8 +212,12 @@ static void vp_vdpa_set_status(struct 
 vdpa_device *vdpa, u8 status)
 {
     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);

Is this change really needed?

Quoted 2 lines

No need to get the status if we try to set status to 0 (trigger BUG).

Okay, but that's the case that should never happen, so IMHO we can leave it as it is.

Quoted 8 lines
Quoted 7 lines

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

Quoted 4 lines
drivers/vdpa/virtio_pci/vp_vdpa.c +4 -0
+    u8 s;
+
+    /* We should never be setting status to 0. */
+    BUG_ON(status == 0);

IMHO panicking the kernel seems excessive in this case, please use WARN_ON and maybe return earlier.

Um...I referenced the vp_reset/vp_set_status,

Ah I see, maybe it's an old code, because recently we always try to avoid BUG_ON().

Quoted 19 lines
Quoted 9 lines
Quoted 9 lines
drivers/vdpa/virtio_pci/vp_vdpa.c +1 -0
+    s = vp_vdpa_get_status(vdpa);

    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
@@ -229,6 +233,11 @@ static int vp_vdpa_reset(struct vdpa_device *vdpa)
     u8 s = vp_vdpa_get_status(vdpa);
 
Quoted 10 lines
Quoted 7 lines
drivers/vdpa/virtio_pci/vp_vdpa.c +1 -0
     vp_modern_set_status(mdev, 0);
+    /* After writing 0 to device_status, the driver MUST wait for 

a read of
+     * device_status to return 0 before reinitializing the device. +     */
+    while (vp_modern_get_status(mdev))
+        msleep(1);

Should we set a limit after which we give up? A malfunctioning device could keep us here forever.

Quoted 1 line

Yes, but the malfunctioning device maybe can not work anymore, how to handle it?

Maybe we should set the status to broken, but in this case we could just return an error if we couldn't reset it, how about that?

Thanks,
Stefano

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

#4 lore raw
Quoted 30 lines · Stefano Garzarella
Quoted 29 lines · Cloud Infrastructure Service Product Dept.)
Quoted 26 lines · Stefano Garzarella
Quoted 17 lines · Longpeng(Mike)

From: Longpeng <[email protected]>

1. We should not set status to 0 when invoking vp_vdpa_set_status().

2. The driver MUST wait for a read of device_status to return 0 before reinitializing the device.

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

1 file changed, 10 insertions(+), 1 deletion(-)
drivers/vdpa/virtio_pci/vp_vdpa.c +0 -0
index d448db0c4de3..d35fac5cde11 100644

drivers/vdpa/virtio_pci/vp_vdpa.c static void vp_vdpa_set_status(struct · line 216

Quoted 5 lines
drivers/vdpa/virtio_pci/vp_vdpa.c +0 -1
@@ -212,8 +212,12 @@ static void vp_vdpa_set_status(struct 
 vdpa_device *vdpa, u8 status)
 {
     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);

Is this change really needed?

Quoted 5 lines
Quoted 2 lines

No need to get the status if we try to set status to 0 (trigger BUG).

Okay, but that's the case that should never happen, so IMHO we can leave it as it is.

OK.

Quoted 11 lines
Quoted 8 lines
Quoted 7 lines

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

Quoted 4 lines
drivers/vdpa/virtio_pci/vp_vdpa.c +4 -0
+    u8 s;
+
+    /* We should never be setting status to 0. */
+    BUG_ON(status == 0);

IMHO panicking the kernel seems excessive in this case, please use WARN_ON and maybe return earlier.

Um...I referenced the vp_reset/vp_set_status,

Ah I see, maybe it's an old code, because recently we always try to avoid BUG_ON().

OK. The checkpatch.pl script also triggered a waring about it. I'll use WARN_ON in next version.

Quoted 19 lines
Quoted 19 lines
Quoted 19 lines
Quoted 11 lines
drivers/vdpa/virtio_pci/vp_vdpa.c +1 -0
+    s = vp_vdpa_get_status(vdpa);

    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 +1 -0
@@ -229,6 +233,11 @@ static int vp_vdpa_reset(struct vdpa_device *vdpa)
     u8 s = vp_vdpa_get_status(vdpa);
 
     vp_modern_set_status(mdev, 0);
+    /* After writing 0 to device_status, the driver MUST wait for a 
Quoted 5 lines

read of
+     * device_status to return 0 before reinitializing the device. +     */
+    while (vp_modern_get_status(mdev))
+        msleep(1);

Should we set a limit after which we give up? A malfunctioning device could keep us here forever.

Quoted 4 lines
Quoted 1 line

Yes, but the malfunctioning device maybe can not work anymore, how to handle it?

Maybe we should set the status to broken, but in this case we could just return an error if we couldn't reset it, how about that?

It can work, but it seems to violate the specification. Maybe we can also wait for other guys' suggestions and then decide how to handle the exception.

Quoted 4 lines

Thanks,
Stefano

.

#5 lore raw
Quoted 74 lines · Cloud Infrastructure Service Product Dept.)
Quoted 30 lines · Stefano Garzarella
Quoted 29 lines · Cloud Infrastructure Service Product Dept.)
Quoted 26 lines · Stefano Garzarella
Quoted 17 lines · Longpeng(Mike)

From: Longpeng <[email protected]>

1. We should not set status to 0 when invoking vp_vdpa_set_status().

2. The driver MUST wait for a read of device_status to return 0 before reinitializing the device.

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

1 file changed, 10 insertions(+), 1 deletion(-)
drivers/vdpa/virtio_pci/vp_vdpa.c +0 -0
index d448db0c4de3..d35fac5cde11 100644

drivers/vdpa/virtio_pci/vp_vdpa.c static void vp_vdpa_set_status(struct · line 216

Quoted 5 lines
drivers/vdpa/virtio_pci/vp_vdpa.c +0 -1
@@ -212,8 +212,12 @@ static void vp_vdpa_set_status(struct 
 vdpa_device *vdpa, u8 status)
 {
     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);

Is this change really needed?

Quoted 5 lines
Quoted 2 lines

No need to get the status if we try to set status to 0 (trigger BUG).

Okay, but that's the case that should never happen, so IMHO we can leave it as it is.

OK.

Quoted 11 lines
Quoted 8 lines
Quoted 7 lines

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

Quoted 4 lines
drivers/vdpa/virtio_pci/vp_vdpa.c +4 -0
+    u8 s;
+
+    /* We should never be setting status to 0. */
+    BUG_ON(status == 0);

IMHO panicking the kernel seems excessive in this case, please use WARN_ON and maybe return earlier.

Um...I referenced the vp_reset/vp_set_status,

Ah I see, maybe it's an old code, because recently we always try to avoid BUG_ON().

OK. The checkpatch.pl script also triggered a waring about it. I'll use WARN_ON in next version.

Quoted 20 lines
Quoted 20 lines
Quoted 20 lines
Quoted 12 lines
drivers/vdpa/virtio_pci/vp_vdpa.c +1 -0
+    s = vp_vdpa_get_status(vdpa);

    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 +1 -0
@@ -229,6 +233,11 @@ static int vp_vdpa_reset(struct vdpa_device 
 *vdpa)
     u8 s = vp_vdpa_get_status(vdpa);
 
     vp_modern_set_status(mdev, 0);
+    /* After writing 0 to device_status, the driver MUST wait for 
Quoted 5 lines

a read of
+     * device_status to return 0 before reinitializing the device. +     */
+    while (vp_modern_get_status(mdev))
+        msleep(1);

Should we set a limit after which we give up? A malfunctioning device could keep us here forever.

Quoted 5 lines
Quoted 4 lines
Quoted 1 line

Yes, but the malfunctioning device maybe can not work anymore, how to handle it?

Maybe we should set the status to broken, but in this case we could just return an error if we couldn't reset it, how about that?

It can work, but it seems to violate the specification. Maybe we can also wait for other guys' suggestions and then decide how to handle the exception.

Need more thought but it's not an issue that is introduced in this patch, we can do optimization on top.

Probably a warning plus FAILED. Then at least the device can DOS the driver which is good for hardening as well.

Thanks

Quoted 6 lines
Quoted 5 lines

Thanks,
Stefano

.

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