Skip to contentSkip to conversation

virtio-rng: fix old guest kernel hang

PATCH Superseded Pankaj Gupta 2 participants 5 messages Jun 27, 2018

v1 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

cover virtio-rng: fix old guest kernel hang

virtio-rng device causing old guest kernels(2.6.32) to hang on latest qemu. The driver attempts to read from the virtio-rng device too early in it's initialization. Qemu detects guest is not ready and returns, resulting in hang. Below patches fix this.

Stefan Hajnoczi(1):
virtio-rng: process pending requests when driver is ready

Pankaj Gupta(1):
virtio: Set status early in VirtIODevice parent object

 virtio-rng.c |   13 +++++++++++++
 virtio.c     |    3 ++-
 2 files changed, 15 insertions(+), 1 deletion(-)
Signature · 1 line
2.14.3
#1 lore raw

PATCH 1/2 virtio-rng: process pending requests when driver is ready

virtio-rng device causing old guest kernels(2.6.32) to hang on latest qemu. The driver attempts to read from the virtio-rng device too early in it's initialization. Qemu detects guest is not ready and returns, resulting in hang.

Fix is to handle pending request when guest is running and driver status is set to 'VIRTIO_CONFIG_S_DRIVER_OK'.

---
 hw/virtio/virtio-rng.c | 13 +++++++++++++
 1 file changed, 13 insertions(+)
hw/virtio/virtio-rng.c +13 -0
index 289bbcac03..49e2d0c10a 100644
@@ -156,6 +156,18 @@ static void check_rate_limit(void *opaque)
     vrng->activate_timer = true;
 }
 
+static void virtio_rng_set_status(VirtIODevice *vdev, uint8_t status)
+{
+    VirtIORNG *vrng = VIRTIO_RNG(vdev);
+
+    if (!vdev->vm_running) {
+        return;
+    }
+
+    /* Something changed, try to process buffers */
+    virtio_rng_process(vrng);
+}
+
 static void virtio_rng_device_realize(DeviceState *dev, Error **errp)
 {
     VirtIODevice *vdev = VIRTIO_DEVICE(dev);
@@ -261,6 +273,7 @@ static void virtio_rng_class_init(ObjectClass *klass, void *data)
     vdc->realize = virtio_rng_device_realize;
     vdc->unrealize = virtio_rng_device_unrealize;
     vdc->get_features = get_features;
+    vdc->set_status = virtio_rng_set_status;
 }
 
 static const TypeInfo virtio_rng_info = {
Signature · 1 line
2.14.3
#2 lore raw

PATCH 2/2 virtio: Set status early in VirtIODevice parent object

In reply to Pankaj Gupta

To process pending requests for driver status 'VIRTIO_CONFIG_S_DRIVER_OK', virtio-rng 'set_status' calls 'is_guest_ready' function. This checks if virtqueue is ready and status is set to 'VIRTIO_CONFIG_S_DRIVER_OK'.

This call is made before new status is updated in VirtIODevice parent object and calling function uses old status value.

---
 hw/virtio/virtio.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
hw/virtio/virtio.c +2 -1
index d4e4d98b59..37dc59a686 100644
@@ -1154,10 +1154,11 @@ int virtio_set_status(VirtIODevice *vdev, uint8_t val)
             }
         }
     }
+    vdev->status = val;
+
     if (k->set_status) {
         k->set_status(vdev, val);
     }
-    vdev->status = val;
     return 0;
 }
 
Signature · 1 line
2.14.3
#3 lore raw

Re: [Qemu-devel] [PATCH 2/2] virtio: Set status early in VirtIODevice parent object

Quoted 17 lines · Pankaj Gupta

To process pending requests for driver status 'VIRTIO_CONFIG_S_DRIVER_OK', virtio-rng 'set_status' calls 'is_guest_ready' function. This checks if virtqueue is ready and status is set to 'VIRTIO_CONFIG_S_DRIVER_OK'.

This call is made before new status is updated in VirtIODevice parent object and calling function uses old status value.

---
 hw/virtio/virtio.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
hw/virtio/virtio.c +0 -0
index d4e4d98b59..37dc59a686 100644

hw/virtio/virtio.c int virtio_set_status(VirtIODevice *vdev, uint8_t val) · line 1160

Quoted 9 lines
hw/virtio/virtio.c +2 -1
@@ -1154,10 +1154,11 @@ int virtio_set_status(VirtIODevice *vdev, uint8_t val)
             }
         }
     }
+    vdev->status = val;
+
     if (k->set_status) {
         k->set_status(vdev, val);
     }
-    vdev->status = val;

This breaks existing ->set_status() callbacks that rely on vdev->status containing the old value. Have you audited them?

For example, virtio_net_set_status -> virtio_net_vnet_endian_status uses vdev->status!

It may be easier to modify virtio-rng.c so that the old status value isn't used.

Stefan

#4 lore raw

Re: [Qemu-devel] [PATCH 2/2] virtio: Set status early in VirtIODevice parent object

Quoted 26 lines · Pankaj Gupta
Quoted 23 lines

To process pending requests for driver status 'VIRTIO_CONFIG_S_DRIVER_OK', virtio-rng 'set_status' calls 'is_guest_ready' function. This checks if virtqueue is ready and status is set to 'VIRTIO_CONFIG_S_DRIVER_OK'.

This call is made before new status is updated in VirtIODevice parent object
and calling function uses old status value.

---
 hw/virtio/virtio.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
hw/virtio/virtio.c +1 -0
index d4e4d98b59..37dc59a686 100644
@@ -1154,10 +1154,11 @@ int virtio_set_status(VirtIODevice *vdev, uint8_t
 val)
             }
         }
     }
+    vdev->status = val;
Quoted 8 lines

hw/virtio/virtio.c int virtio_set_status(VirtIODevice *vdev, uint8_t val) · line 1160

Quoted 5 lines
hw/virtio/virtio.c +1 -1
+
     if (k->set_status) {
         k->set_status(vdev, val);
     }
-    vdev->status = val;

This breaks existing ->set_status() callbacks that rely on vdev->status containing the old value. Have you audited them?

I did not audit them all. I thought that's the way it should be base object first get updated. But you are right, it can break things where old status is used.

Quoted 6 lines

For example, virtio_net_set_status -> virtio_net_vnet_endian_status uses vdev->status!

It may be easier to modify virtio-rng.c so that the old status value isn't used.

Just updating status only for virtio_rng_set_status looks okay? This will avoid multiple copies of status as different functions are using it.

I will send a V2.

+static void virtio_rng_set_status(VirtIODevice *vdev, uint8_t status) +{
+ VirtIORNG *vrng = VIRTIO_RNG(vdev);
+
+ if (!vdev->vm_running) {
+ return;
+ }
+ vdev->status = status;
+
+ /* Something changed, try to process buffers */
+ virtio_rng_process(vrng);
+}
+

Thanks,
Pankaj

Quoted 3 lines

Stefan