Skip to contentSkip to conversation

virtio: Fix error handling in virtio_bus_device_plugged

PATCH Superseded Fam Zheng 3 participants 5 messages Mar 17, 2017

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Fam Zheng

author
#0 lore raw

PATCH 1/1 virtio: Fix error handling in virtio_bus_device_plugged

For one thing we shouldn't continue if an error happened, for the other two steps failing can cause an abort() in error_setg because we reuse the same errp blindly.

Add error handling checks to fix both issues.

---
 hw/virtio/virtio-bus.c | 18 ++++++++++++++++--
 1 file changed, 16 insertions(+), 2 deletions(-)
hw/virtio/virtio-bus.c +16 -2
index a886011..ef76919 100644
@@ -25,6 +25,7 @@
 #include "qemu/osdep.h"
 #include "hw/hw.h"
 #include "qemu/error-report.h"
+#include "qapi/error.h"
 #include "hw/qdev.h"
 #include "hw/virtio/virtio-bus.h"
 #include "hw/virtio/virtio.h"
@@ -48,21 +49,34 @@ void virtio_bus_device_plugged(VirtIODevice *vdev, Error **errp)
     VirtioBusClass *klass = VIRTIO_BUS_GET_CLASS(bus);
     VirtioDeviceClass *vdc = VIRTIO_DEVICE_GET_CLASS(vdev);
     bool has_iommu = virtio_host_has_feature(vdev, VIRTIO_F_IOMMU_PLATFORM);
+    Error *local_err = NULL;
 
     DPRINTF("%s: plug device.\n", qbus->name);
 
     if (klass->pre_plugged != NULL) {
-        klass->pre_plugged(qbus->parent, errp);
+        klass->pre_plugged(qbus->parent, &local_err);
+        if (local_err) {
+            error_propagate(errp, local_err);
+            return;
+        }
     }
 
     /* Get the features of the plugged device. */
     assert(vdc->get_features != NULL);
     vdev->host_features = vdc->get_features(vdev, vdev->host_features,
-                                            errp);
+                                            &local_err);
+    if (local_err) {
+        error_propagate(errp, local_err);
+        return;
+    }
 
     if (klass->device_plugged != NULL) {
         klass->device_plugged(qbus->parent, errp);
     }
+    if (local_err) {
+        error_propagate(errp, local_err);
+        return;
+    }
 
     if (klass->get_dma_as != NULL && has_iommu) {
         virtio_add_feature(&vdev->host_features, VIRTIO_F_IOMMU_PLATFORM);
Signature · 1 line
2.9.3
Reviewed-by on v1 1/1
#1 lore raw
Quoted 10 lines · On Fri, 17 Mar 2017 19:33:53 +0800 Fam Zheng <[email protected]> wrote:

For one thing we shouldn't continue if an error happened, for the other two steps failing can cause an abort() in error_setg because we reuse the same errp blindly.

Add error handling checks to fix both issues.

---
 hw/virtio/virtio-bus.c | 18 ++++++++++++++++--
 1 file changed, 16 insertions(+), 2 deletions(-)
#2 lore raw
In reply to Fam Zheng
Quoted 42 lines · Fam Zheng

For one thing we shouldn't continue if an error happened, for the other two steps failing can cause an abort() in error_setg because we reuse the same errp blindly.

Add error handling checks to fix both issues.

---
 hw/virtio/virtio-bus.c | 18 ++++++++++++++++--
 1 file changed, 16 insertions(+), 2 deletions(-)
hw/virtio/virtio-bus.c +7 -1
index a886011..ef76919 100644
@@ -25,6 +25,7 @@
 #include "qemu/osdep.h"
 #include "hw/hw.h"
 #include "qemu/error-report.h"
+#include "qapi/error.h"
 #include "hw/qdev.h"
 #include "hw/virtio/virtio-bus.h"
 #include "hw/virtio/virtio.h"
@@ -48,21 +49,34 @@ void virtio_bus_device_plugged(VirtIODevice *vdev, Error **errp)
     VirtioBusClass *klass = VIRTIO_BUS_GET_CLASS(bus);
     VirtioDeviceClass *vdc = VIRTIO_DEVICE_GET_CLASS(vdev);
     bool has_iommu = virtio_host_has_feature(vdev, VIRTIO_F_IOMMU_PLATFORM);
+    Error *local_err = NULL;
 
     DPRINTF("%s: plug device.\n", qbus->name);
 
     if (klass->pre_plugged != NULL) {
-        klass->pre_plugged(qbus->parent, errp);
+        klass->pre_plugged(qbus->parent, &local_err);
+        if (local_err) {
+            error_propagate(errp, local_err);
+            return;
+        }
     }
 
     /* Get the features of the plugged device. */
     assert(vdc->get_features != NULL);

hw/virtio/virtio-bus.c void virtio_bus_device_plugged(VirtIODevice *vdev, Error **errp) · line 74

Quoted 10 lines
hw/virtio/virtio-bus.c +5 -1
     vdev->host_features = vdc->get_features(vdev, vdev->host_features,
-                                            errp);
+                                            &local_err);
+    if (local_err) {
+        error_propagate(errp, local_err);
+        return;
+    }
 
     if (klass->device_plugged != NULL) {
         klass->device_plugged(qbus->parent, errp);

Did you intend to change this errp to &local_err, as well? If not, then the body of the below hunk is unreachable, as local_err can never be non-null there.

Thanks,
drew

Quoted 12 lines
hw/virtio/virtio-bus.c +4 -0
     }
+    if (local_err) {
+        error_propagate(errp, local_err);
+        return;
+    }
 
     if (klass->get_dma_as != NULL && has_iommu) {
         virtio_add_feature(&vdev->host_features, VIRTIO_F_IOMMU_PLATFORM);
Signature · 1 line
2.9.3
#3 lore raw
Quoted 13 lines · On Fri, 17 Mar 2017 13:18:16 +0100 Andrew Jones <[email protected]> wrote:
Quoted 12 lines · Fam Zheng

For one thing we shouldn't continue if an error happened, for the other two steps failing can cause an abort() in error_setg because we reuse the same errp blindly.

Add error handling checks to fix both issues.

---
 hw/virtio/virtio-bus.c | 18 ++++++++++++++++--
 1 file changed, 16 insertions(+), 2 deletions(-)
Quoted 6 lines
Quoted 2 lines
     if (klass->device_plugged != NULL) {
         klass->device_plugged(qbus->parent, errp);

Did you intend to change this errp to &local_err, as well? If not, then the body of the below hunk is unreachable, as local_err can never be non-null there.

Oh, I managed to overlook this.

So my r-b applies if this is changed to use local_err :)

Quoted 18 lines

Thanks,
drew

Quoted 12 lines
     }
hw/virtio/virtio-bus.c +4 -0
+    if (local_err) {
+        error_propagate(errp, local_err);
+        return;
+    }
 
     if (klass->get_dma_as != NULL && has_iommu) {
         virtio_add_feature(&vdev->host_features, VIRTIO_F_IOMMU_PLATFORM);
Signature · 1 line
2.9.3
author
#4 lore raw
In reply to Andrew Jones
Quoted 51 lines · Andrew Jones
Quoted 50 lines · Fam Zheng

For one thing we shouldn't continue if an error happened, for the other two steps failing can cause an abort() in error_setg because we reuse the same errp blindly.

Add error handling checks to fix both issues.

---
 hw/virtio/virtio-bus.c | 18 ++++++++++++++++--
 1 file changed, 16 insertions(+), 2 deletions(-)
hw/virtio/virtio-bus.c +12 -2
index a886011..ef76919 100644
@@ -25,6 +25,7 @@
 #include "qemu/osdep.h"
 #include "hw/hw.h"
 #include "qemu/error-report.h"
+#include "qapi/error.h"
 #include "hw/qdev.h"
 #include "hw/virtio/virtio-bus.h"
 #include "hw/virtio/virtio.h"
@@ -48,21 +49,34 @@ void virtio_bus_device_plugged(VirtIODevice *vdev, Error **errp)
     VirtioBusClass *klass = VIRTIO_BUS_GET_CLASS(bus);
     VirtioDeviceClass *vdc = VIRTIO_DEVICE_GET_CLASS(vdev);
     bool has_iommu = virtio_host_has_feature(vdev, VIRTIO_F_IOMMU_PLATFORM);
+    Error *local_err = NULL;
 
     DPRINTF("%s: plug device.\n", qbus->name);
 
     if (klass->pre_plugged != NULL) {
-        klass->pre_plugged(qbus->parent, errp);
+        klass->pre_plugged(qbus->parent, &local_err);
+        if (local_err) {
+            error_propagate(errp, local_err);
+            return;
+        }
     }
 
     /* Get the features of the plugged device. */
     assert(vdc->get_features != NULL);
     vdev->host_features = vdc->get_features(vdev, vdev->host_features,
-                                            errp);
+                                            &local_err);
+    if (local_err) {
+        error_propagate(errp, local_err);
+        return;
+    }
 
Quoted 6 lines

hw/virtio/virtio-bus.c void virtio_bus_device_plugged(VirtIODevice *vdev, Error **errp) · line 74

Quoted 2 lines
hw/virtio/virtio-bus.c +0 -0
     if (klass->device_plugged != NULL) {
         klass->device_plugged(qbus->parent, errp);

Did you intend to change this errp to &local_err, as well? If not, then the body of the below hunk is unreachable, as local_err can never be non-null there.

Yes, this is an overlook.

Fam

Quoted 16 lines

Thanks,
drew

Quoted 12 lines
hw/virtio/virtio-bus.c +4 -0
     }
+    if (local_err) {
+        error_propagate(errp, local_err);
+        return;
+    }
 
     if (klass->get_dma_as != NULL && has_iommu) {
         virtio_add_feature(&vdev->host_features, VIRTIO_F_IOMMU_PLATFORM);
Signature · 1 line
2.9.3