Skip to contentSkip to conversation

spapr_pci: fail gracefully with non-pseries machine types

PATCH Superseded Greg Kurz 3 participants 7 messages Oct 6, 2017 – Oct 11, 2017

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Greg Kurz

author
#0 lore raw

PATCH 1/1 spapr_pci: fail gracefully with non-pseries machine types

QEMU currently crashes when the user tries to add a spapr-pci-host-bridge on a non-pseries machine:

$ qemu-system-ppc64 -M ppce500 -device spapr-pci-host-bridge,index=1
hw/ppc/spapr_pci.c:1535:spapr_phb_realize:
Object 0x1003dacae60 is not an instance of type spapr-machine
Aborted (core dumped)

The same thing happens with the deprecated but still available child type spapr-pci-vfio-host-bridge.

Fix both by checking the machine type with object_dynamic_cast().

---
 hw/ppc/spapr_pci.c |    8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
hw/ppc/spapr_pci.c +7 -1
index 5049ced4e8b4..9e85106f51f8 100644
@@ -1507,7 +1507,7 @@ static void spapr_pci_unplug_request(HotplugHandler *plug_handler,
 
 static void spapr_phb_realize(DeviceState *dev, Error **errp)
 {
-    sPAPRMachineState *spapr = SPAPR_MACHINE(qdev_get_machine());
+    sPAPRMachineState *spapr;
     SysBusDevice *s = SYS_BUS_DEVICE(dev);
     sPAPRPHBState *sphb = SPAPR_PCI_HOST_BRIDGE(s);
     PCIHostState *phb = PCI_HOST_BRIDGE(s);
@@ -1519,6 +1519,12 @@ static void spapr_phb_realize(DeviceState *dev, Error **errp)
     const unsigned windows_supported =
         sphb->ddw_enabled ? SPAPR_PCI_DMA_MAX_WINDOWS : 1;
 
+    spapr = (sPAPRMachineState *) qdev_get_machine();
+    if (!object_dynamic_cast((Object *) spapr, TYPE_SPAPR_MACHINE)) {
+        error_setg(errp, TYPE_SPAPR_PCI_HOST_BRIDGE " needs a pseries machine");
+        return;
+    }
+
     if (sphb->index != (uint32_t)-1) {
         sPAPRMachineClass *smc = SPAPR_MACHINE_GET_CLASS(spapr);
         Error *local_err = NULL;
Reviewed-by on v1 1/1
#1 lore raw
Quoted 8 lines · Greg Kurz

QEMU currently crashes when the user tries to add a spapr-pci-host-bridge on a non-pseries machine:

$ qemu-system-ppc64 -M ppce500 -device spapr-pci-host-bridge,index=1
hw/ppc/spapr_pci.c:1535:spapr_phb_realize:
Object 0x1003dacae60 is not an instance of type spapr-machine
Aborted (core dumped)
Quoted 7 lines

The same thing happens with the deprecated but still available child type spapr-pci-vfio-host-bridge.

Fix both by checking the machine type with object_dynamic_cast().

---

Quoted 31 lines
  hw/ppc/spapr_pci.c |    8 +++++++-
  1 file changed, 7 insertions(+), 1 deletion(-)
hw/ppc/spapr_pci.c +7 -1
index 5049ced4e8b4..9e85106f51f8 100644
@@ -1507,7 +1507,7 @@ static void spapr_pci_unplug_request(HotplugHandler *plug_handler,
 
  static void spapr_phb_realize(DeviceState *dev, Error **errp)
  {
-    sPAPRMachineState *spapr = SPAPR_MACHINE(qdev_get_machine());
+    sPAPRMachineState *spapr;
      SysBusDevice *s = SYS_BUS_DEVICE(dev);
      sPAPRPHBState *sphb = SPAPR_PCI_HOST_BRIDGE(s);
      PCIHostState *phb = PCI_HOST_BRIDGE(s);
@@ -1519,6 +1519,12 @@ static void spapr_phb_realize(DeviceState *dev, Error **errp)
      const unsigned windows_supported =
          sphb->ddw_enabled ? SPAPR_PCI_DMA_MAX_WINDOWS : 1;
 
+    spapr = (sPAPRMachineState *) qdev_get_machine();
+    if (!object_dynamic_cast((Object *) spapr, TYPE_SPAPR_MACHINE)) {
+        error_setg(errp, TYPE_SPAPR_PCI_HOST_BRIDGE " needs a pseries machine");
+        return;
+    }
+
      if (sphb->index != (uint32_t)-1) {
          sPAPRMachineClass *smc = SPAPR_MACHINE_GET_CLASS(spapr);
          Error *local_err = NULL;
#2 lore raw
In reply to Greg Kurz
Quoted 35 lines · Greg Kurz

QEMU currently crashes when the user tries to add a spapr-pci-host-bridge on a non-pseries machine:

$ qemu-system-ppc64 -M ppce500 -device spapr-pci-host-bridge,index=1
hw/ppc/spapr_pci.c:1535:spapr_phb_realize:
Object 0x1003dacae60 is not an instance of type spapr-machine
Aborted (core dumped)

The same thing happens with the deprecated but still available child type spapr-pci-vfio-host-bridge.

Fix both by checking the machine type with object_dynamic_cast().

---
 hw/ppc/spapr_pci.c |    8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
hw/ppc/spapr_pci.c +1 -1
index 5049ced4e8b4..9e85106f51f8 100644
@@ -1507,7 +1507,7 @@ static void spapr_pci_unplug_request(HotplugHandler *plug_handler,
 
 static void spapr_phb_realize(DeviceState *dev, Error **errp)
 {
-    sPAPRMachineState *spapr = SPAPR_MACHINE(qdev_get_machine());
+    sPAPRMachineState *spapr;
     SysBusDevice *s = SYS_BUS_DEVICE(dev);
     sPAPRPHBState *sphb = SPAPR_PCI_HOST_BRIDGE(s);
     PCIHostState *phb = PCI_HOST_BRIDGE(s);

hw/ppc/spapr_pci.c static void spapr_phb_realize(DeviceState *dev, Error **errp) · line 1526

Quoted 8 lines
hw/ppc/spapr_pci.c +5 -0
@@ -1519,6 +1519,12 @@ static void spapr_phb_realize(DeviceState *dev, Error **errp)
     const unsigned windows_supported =
         sphb->ddw_enabled ? SPAPR_PCI_DMA_MAX_WINDOWS : 1;
 
+    spapr = (sPAPRMachineState *) qdev_get_machine();
+    if (!object_dynamic_cast((Object *) spapr, TYPE_SPAPR_MACHINE)) {
+        error_setg(errp, TYPE_SPAPR_PCI_HOST_BRIDGE " needs a pseries machine");
+        return;
+    }

This is slightly clunky. You could instead use OBJECT_CHECK in the initializer, then just if (!spapr) here.

Quoted 5 lines
hw/ppc/spapr_pci.c +1 -0
+
     if (sphb->index != (uint32_t)-1) {
         sPAPRMachineClass *smc = SPAPR_MACHINE_GET_CLASS(spapr);
         Error *local_err = NULL;
Signature · 4 lines
David Gibson			| I'll have my music baroque, and my code
david AT gibson.dropbear.id.au	| minimalist, thank you.  NOT _the_ _other_
				| _way_ _around_!
http://www.ozlabs.org/~dgibson
author
#3 lore raw
Quoted 39 lines · On Sat, 7 Oct 2017 16:19:25 +1100 David Gibson <[email protected]>…
Quoted 38 lines · Greg Kurz

QEMU currently crashes when the user tries to add a spapr-pci-host-bridge on a non-pseries machine:

$ qemu-system-ppc64 -M ppce500 -device spapr-pci-host-bridge,index=1
hw/ppc/spapr_pci.c:1535:spapr_phb_realize:
Object 0x1003dacae60 is not an instance of type spapr-machine
Aborted (core dumped)

The same thing happens with the deprecated but still available child type spapr-pci-vfio-host-bridge.

Fix both by checking the machine type with object_dynamic_cast().

---
 hw/ppc/spapr_pci.c |    8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
hw/ppc/spapr_pci.c +1 -1
index 5049ced4e8b4..9e85106f51f8 100644
@@ -1507,7 +1507,7 @@ static void spapr_pci_unplug_request(HotplugHandler *plug_handler,
 
 static void spapr_phb_realize(DeviceState *dev, Error **errp)
 {
-    sPAPRMachineState *spapr = SPAPR_MACHINE(qdev_get_machine());
+    sPAPRMachineState *spapr;
     SysBusDevice *s = SYS_BUS_DEVICE(dev);
     sPAPRPHBState *sphb = SPAPR_PCI_HOST_BRIDGE(s);
     PCIHostState *phb = PCI_HOST_BRIDGE(s);
@@ -1519,6 +1519,12 @@ static void spapr_phb_realize(DeviceState *dev, Error **errp)
     const unsigned windows_supported =
         sphb->ddw_enabled ? SPAPR_PCI_DMA_MAX_WINDOWS : 1;
 
Quoted 9 lines

hw/ppc/spapr_pci.c static void spapr_phb_realize(DeviceState *dev, Error **errp) · line 1526

Quoted 5 lines
hw/ppc/spapr_pci.c +5 -0
+    spapr = (sPAPRMachineState *) qdev_get_machine();
+    if (!object_dynamic_cast((Object *) spapr, TYPE_SPAPR_MACHINE)) {
+        error_setg(errp, TYPE_SPAPR_PCI_HOST_BRIDGE " needs a pseries machine");
+        return;
+    }  

This is slightly clunky. You could instead use OBJECT_CHECK in the initializer, then just if (!spapr) here.

Unless I'm missing something, OBJECT_CHECK() is precisely what we're removing with this patch to avoid the runtime assert...

#define SPAPR_MACHINE(obj) \
OBJECT_CHECK(sPAPRMachineState, (obj), TYPE_SPAPR_MACHINE)

Quoted 7 lines
Quoted 5 lines
hw/ppc/spapr_pci.c +1 -0
+
     if (sphb->index != (uint32_t)-1) {
         sPAPRMachineClass *smc = SPAPR_MACHINE_GET_CLASS(spapr);
         Error *local_err = NULL;
author
#4 lore raw
Quoted 52 lines · On Sat, 7 Oct 2017 10:06:00 +0200 Greg Kurz <[email protected]> wrote:
Quoted 39 lines · On Sat, 7 Oct 2017 16:19:25 +1100 David Gibson <[email protected]>…
Quoted 38 lines · Greg Kurz

QEMU currently crashes when the user tries to add a spapr-pci-host-bridge on a non-pseries machine:

$ qemu-system-ppc64 -M ppce500 -device spapr-pci-host-bridge,index=1
hw/ppc/spapr_pci.c:1535:spapr_phb_realize:
Object 0x1003dacae60 is not an instance of type spapr-machine
Aborted (core dumped)

The same thing happens with the deprecated but still available child type spapr-pci-vfio-host-bridge.

Fix both by checking the machine type with object_dynamic_cast().

---
 hw/ppc/spapr_pci.c |    8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
hw/ppc/spapr_pci.c +1 -1
index 5049ced4e8b4..9e85106f51f8 100644
@@ -1507,7 +1507,7 @@ static void spapr_pci_unplug_request(HotplugHandler *plug_handler,
 
 static void spapr_phb_realize(DeviceState *dev, Error **errp)
 {
-    sPAPRMachineState *spapr = SPAPR_MACHINE(qdev_get_machine());
+    sPAPRMachineState *spapr;
     SysBusDevice *s = SYS_BUS_DEVICE(dev);
     sPAPRPHBState *sphb = SPAPR_PCI_HOST_BRIDGE(s);
     PCIHostState *phb = PCI_HOST_BRIDGE(s);
@@ -1519,6 +1519,12 @@ static void spapr_phb_realize(DeviceState *dev, Error **errp)
     const unsigned windows_supported =
         sphb->ddw_enabled ? SPAPR_PCI_DMA_MAX_WINDOWS : 1;
 
Quoted 9 lines

hw/ppc/spapr_pci.c static void spapr_phb_realize(DeviceState *dev, Error **errp) · line 1526

Quoted 5 lines
hw/ppc/spapr_pci.c +5 -0
+    spapr = (sPAPRMachineState *) qdev_get_machine();
+    if (!object_dynamic_cast((Object *) spapr, TYPE_SPAPR_MACHINE)) {
+        error_setg(errp, TYPE_SPAPR_PCI_HOST_BRIDGE " needs a pseries machine");
+        return;
+    }    

This is slightly clunky. You could instead use OBJECT_CHECK in the initializer, then just if (!spapr) here.

Quoted 6 lines

Unless I'm missing something, OBJECT_CHECK() is precisely what we're removing with this patch to avoid the runtime assert...

#define SPAPR_MACHINE(obj) \
OBJECT_CHECK(sPAPRMachineState, (obj), TYPE_SPAPR_MACHINE)

Since we already do this in spapr-cpu-core, I guess I'll hide the ugliness in some macro. I'll send another patch.

Quoted 8 lines
Quoted 7 lines
Quoted 5 lines
hw/ppc/spapr_pci.c +1 -0
+
     if (sphb->index != (uint32_t)-1) {
         sPAPRMachineClass *smc = SPAPR_MACHINE_GET_CLASS(spapr);
         Error *local_err = NULL;
author
#5 lore raw
In reply to David Gibson
Quoted 39 lines · On Sat, 7 Oct 2017 16:19:25 +1100 David Gibson <[email protected]>…
Quoted 38 lines · Greg Kurz

QEMU currently crashes when the user tries to add a spapr-pci-host-bridge on a non-pseries machine:

$ qemu-system-ppc64 -M ppce500 -device spapr-pci-host-bridge,index=1
hw/ppc/spapr_pci.c:1535:spapr_phb_realize:
Object 0x1003dacae60 is not an instance of type spapr-machine
Aborted (core dumped)

The same thing happens with the deprecated but still available child type spapr-pci-vfio-host-bridge.

Fix both by checking the machine type with object_dynamic_cast().

---
 hw/ppc/spapr_pci.c |    8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
hw/ppc/spapr_pci.c +1 -1
index 5049ced4e8b4..9e85106f51f8 100644
@@ -1507,7 +1507,7 @@ static void spapr_pci_unplug_request(HotplugHandler *plug_handler,
 
 static void spapr_phb_realize(DeviceState *dev, Error **errp)
 {
-    sPAPRMachineState *spapr = SPAPR_MACHINE(qdev_get_machine());
+    sPAPRMachineState *spapr;
     SysBusDevice *s = SYS_BUS_DEVICE(dev);
     sPAPRPHBState *sphb = SPAPR_PCI_HOST_BRIDGE(s);
     PCIHostState *phb = PCI_HOST_BRIDGE(s);
@@ -1519,6 +1519,12 @@ static void spapr_phb_realize(DeviceState *dev, Error **errp)
     const unsigned windows_supported =
         sphb->ddw_enabled ? SPAPR_PCI_DMA_MAX_WINDOWS : 1;
 
Quoted 9 lines

hw/ppc/spapr_pci.c static void spapr_phb_realize(DeviceState *dev, Error **errp) · line 1526

Quoted 5 lines
hw/ppc/spapr_pci.c +5 -0
+    spapr = (sPAPRMachineState *) qdev_get_machine();
+    if (!object_dynamic_cast((Object *) spapr, TYPE_SPAPR_MACHINE)) {
+        error_setg(errp, TYPE_SPAPR_PCI_HOST_BRIDGE " needs a pseries machine");
+        return;
+    }  

This is slightly clunky. You could instead use OBJECT_CHECK in the initializer, then just if (!spapr) here.

The negative review on v2 seem to indicate we have an agreement on using object_dynamic_cast() explicitly. So we have two possibilities, either the "clunky" v1, or possibly:

- sPAPRMachineState *spapr = SPAPR_MACHINE(qdev_get_machine());
+ /* We don't use SPAPR_MACHINE() in order to exit gracefully if the user
+ * tries to add a PHB to a non-pseries machine.
+ */
+ sPAPRMachineState *spapr =
+ (sPAPRMachineState *) object_dynamic_cast(qdev_get_machine(),

+                                                  TYPE_SPAPR_MACHINE);
     SysBusDevice *s = SYS_BUS_DEVICE(dev);
     sPAPRPHBState *sphb = SPAPR_PCI_HOST_BRIDGE(s);
     PCIHostState *phb = PCI_HOST_BRIDGE(s);
     char *namebuf;
     int i;
     PCIBus *bus;
     uint64_t msi_window_size = 4096;
     sPAPRTCETable *tcet;
     const unsigned windows_supported =
         sphb->ddw_enabled ? SPAPR_PCI_DMA_MAX_WINDOWS : 1;

+ if (!spapr) {
+ error_setg(errp, TYPE_SPAPR_PCI_HOST_BRIDGE " needs a pseries machine");
+ return;
+ }
+

Which one do you prefer ? I'll also change spapr_cpu_core_realize() if needed.

Quoted 7 lines
Quoted 5 lines
hw/ppc/spapr_pci.c +1 -0
+
     if (sphb->index != (uint32_t)-1) {
         sPAPRMachineClass *smc = SPAPR_MACHINE_GET_CLASS(spapr);
         Error *local_err = NULL;
#6 lore raw
Quoted 80 lines · Greg Kurz
Quoted 39 lines · On Sat, 7 Oct 2017 16:19:25 +1100 David Gibson <[email protected]>…
Quoted 38 lines · Greg Kurz

QEMU currently crashes when the user tries to add a spapr-pci-host-bridge on a non-pseries machine:

$ qemu-system-ppc64 -M ppce500 -device spapr-pci-host-bridge,index=1
hw/ppc/spapr_pci.c:1535:spapr_phb_realize:
Object 0x1003dacae60 is not an instance of type spapr-machine
Aborted (core dumped)

The same thing happens with the deprecated but still available child type spapr-pci-vfio-host-bridge.

Fix both by checking the machine type with object_dynamic_cast().

---
 hw/ppc/spapr_pci.c |    8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
hw/ppc/spapr_pci.c +1 -1
index 5049ced4e8b4..9e85106f51f8 100644
@@ -1507,7 +1507,7 @@ static void spapr_pci_unplug_request(HotplugHandler *plug_handler,
 
 static void spapr_phb_realize(DeviceState *dev, Error **errp)
 {
-    sPAPRMachineState *spapr = SPAPR_MACHINE(qdev_get_machine());
+    sPAPRMachineState *spapr;
     SysBusDevice *s = SYS_BUS_DEVICE(dev);
     sPAPRPHBState *sphb = SPAPR_PCI_HOST_BRIDGE(s);
     PCIHostState *phb = PCI_HOST_BRIDGE(s);
@@ -1519,6 +1519,12 @@ static void spapr_phb_realize(DeviceState *dev, Error **errp)
     const unsigned windows_supported =
         sphb->ddw_enabled ? SPAPR_PCI_DMA_MAX_WINDOWS : 1;
 
Quoted 9 lines

hw/ppc/spapr_pci.c static void spapr_phb_realize(DeviceState *dev, Error **errp) · line 1526

Quoted 5 lines
hw/ppc/spapr_pci.c +5 -0
+    spapr = (sPAPRMachineState *) qdev_get_machine();
+    if (!object_dynamic_cast((Object *) spapr, TYPE_SPAPR_MACHINE)) {
+        error_setg(errp, TYPE_SPAPR_PCI_HOST_BRIDGE " needs a pseries machine");
+        return;
+    }  

This is slightly clunky. You could instead use OBJECT_CHECK in the initializer, then just if (!spapr) here.

The negative review on v2 seem to indicate we have an agreement on using object_dynamic_cast() explicitly. So we have two possibilities, either the "clunky" v1, or possibly:

(quoted diff) +11 -1
-    sPAPRMachineState *spapr = SPAPR_MACHINE(qdev_get_machine());
+    /* We don't use SPAPR_MACHINE() in order to exit gracefully if the user
+     * tries to add a PHB to a non-pseries machine.
+     */
+    sPAPRMachineState *spapr =
+        (sPAPRMachineState *) object_dynamic_cast(qdev_get_machine(),
+                                                  TYPE_SPAPR_MACHINE);
     SysBusDevice *s = SYS_BUS_DEVICE(dev);
     sPAPRPHBState *sphb = SPAPR_PCI_HOST_BRIDGE(s);
     PCIHostState *phb = PCI_HOST_BRIDGE(s);
     char *namebuf;
     int i;
     PCIBus *bus;
     uint64_t msi_window_size = 4096;
     sPAPRTCETable *tcet;
     const unsigned windows_supported =
         sphb->ddw_enabled ? SPAPR_PCI_DMA_MAX_WINDOWS : 1;
 
+    if (!spapr) {
+        error_setg(errp, TYPE_SPAPR_PCI_HOST_BRIDGE " needs a pseries machine");
+        return;
+    }
+
Quoted 2 lines

Which one do you prefer ? I'll also change spapr_cpu_core_realize() if needed.

Second one looks good to me.

Quoted 9 lines
Quoted 8 lines
Quoted 6 lines
hw/ppc/spapr_pci.c +1 -0
+
     if (sphb->index != (uint32_t)-1) {
         sPAPRMachineClass *smc = SPAPR_MACHINE_GET_CLASS(spapr);
         Error *local_err = NULL;
Signature · 4 lines
David Gibson			| I'll have my music baroque, and my code
david AT gibson.dropbear.id.au	| minimalist, thank you.  NOT _the_ _other_
				| _way_ _around_!
http://www.ozlabs.org/~dgibson