Skip to contentSkip to conversation

spapr: introduce SPAPR_MACHINE_NOASSERT()

PATCH In review Greg Kurz 3 participants 5 messages Oct 8, 2017 – Oct 9, 2017

lore
Share & tools
lore

Greg Kurz

author
#0 lore raw

PATCH v2 1/2 spapr: introduce SPAPR_MACHINE_NOASSERT()

A spapr-cpu-core device can only be plugged into a pseries machine. This is checked in spapr_cpu_core_realize() with object_dynamic_cast() instead of the usual SPAPR_MACHINE() macro because we don't want QEMU to abort.

This patch moves the gory details to a SPAPR_MACHINE_NOASSERT() macro that acts like the SPAPR_MACHINE() one, except it returns NULL instead of aborting if its argument doesn't point to a pseries machine type.

This is done for two reasons:
- it makes the code nicer
- it may be used by other pseries-specific devices like PHBs for example

---
 hw/ppc/spapr_cpu_core.c |    7 +++----
 include/hw/ppc/spapr.h  |    3 +++
 2 files changed, 6 insertions(+), 4 deletions(-)
hw/ppc/spapr_cpu_core.c +3 -4
index 37beb56e8b18..5fde07614218 100644
@@ -199,7 +199,7 @@ error:
 
 static void spapr_cpu_core_realize(DeviceState *dev, Error **errp)
 {
-    sPAPRMachineState *spapr;
+    sPAPRMachineState *spapr = SPAPR_MACHINE_NOASSERT(qdev_get_machine());
     sPAPRCPUCore *sc = SPAPR_CPU_CORE(OBJECT(dev));
     sPAPRCPUCoreClass *scc = SPAPR_CPU_CORE_GET_CLASS(OBJECT(dev));
     CPUCore *cc = CPU_CORE(OBJECT(dev));
@@ -209,9 +209,8 @@ static void spapr_cpu_core_realize(DeviceState *dev, Error **errp)
     void *obj;
     int i, j;
 
-    spapr = (sPAPRMachineState *) qdev_get_machine();
-    if (!object_dynamic_cast((Object *) spapr, TYPE_SPAPR_MACHINE)) {
-        error_setg(errp, "spapr-cpu-core needs a pseries machine");
+    if (!spapr) {
+        error_setg(errp, TYPE_SPAPR_CPU_CORE " needs a pseries machine");
         return;
     }
 
include/hw/ppc/spapr.h +3 -0
index c1b365f56431..4933da8083df 100644
@@ -43,6 +43,9 @@ typedef struct sPAPRMachineClass sPAPRMachineClass;
 #define SPAPR_MACHINE_CLASS(klass) \
     OBJECT_CLASS_CHECK(sPAPRMachineClass, klass, TYPE_SPAPR_MACHINE)
 
+#define SPAPR_MACHINE_NOASSERT(obj) \
+    ((sPAPRMachineState *) object_dynamic_cast(obj, TYPE_SPAPR_MACHINE))
+
 typedef enum {
     SPAPR_RESIZE_HPT_DEFAULT = 0,
     SPAPR_RESIZE_HPT_DISABLED,
author
#1 lore raw

PATCH v2 2/2 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 using the SPAPR_MACHINE_NOASSERT() macro in papr_phb_realize().

---
v2: - use new SPAPR_MACHINE_NOASSERT() macro

---
 hw/ppc/spapr_pci.c |    7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
hw/ppc/spapr_pci.c +6 -1
index 5049ced4e8b4..4d6a91ebb9c2 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 = SPAPR_MACHINE_NOASSERT(qdev_get_machine());
     SysBusDevice *s = SYS_BUS_DEVICE(dev);
     sPAPRPHBState *sphb = SPAPR_PCI_HOST_BRIDGE(s);
     PCIHostState *phb = PCI_HOST_BRIDGE(s);
@@ -1519,6 +1519,11 @@ static void spapr_phb_realize(DeviceState *dev, Error **errp)
     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;
+    }
+
     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 48 lines · Greg Kurz

A spapr-cpu-core device can only be plugged into a pseries machine. This is checked in spapr_cpu_core_realize() with object_dynamic_cast() instead of the usual SPAPR_MACHINE() macro because we don't want QEMU to abort.

This patch moves the gory details to a SPAPR_MACHINE_NOASSERT() macro that acts like the SPAPR_MACHINE() one, except it returns NULL instead of aborting if its argument doesn't point to a pseries machine type.

This is done for two reasons:
- it makes the code nicer
- it may be used by other pseries-specific devices like PHBs for example

---
 hw/ppc/spapr_cpu_core.c |    7 +++----
 include/hw/ppc/spapr.h  |    3 +++
 2 files changed, 6 insertions(+), 4 deletions(-)
hw/ppc/spapr_cpu_core.c +3 -4
index 37beb56e8b18..5fde07614218 100644
@@ -199,7 +199,7 @@ error:
 
 static void spapr_cpu_core_realize(DeviceState *dev, Error **errp)
 {
-    sPAPRMachineState *spapr;
+    sPAPRMachineState *spapr = SPAPR_MACHINE_NOASSERT(qdev_get_machine());
     sPAPRCPUCore *sc = SPAPR_CPU_CORE(OBJECT(dev));
     sPAPRCPUCoreClass *scc = SPAPR_CPU_CORE_GET_CLASS(OBJECT(dev));
     CPUCore *cc = CPU_CORE(OBJECT(dev));
@@ -209,9 +209,8 @@ static void spapr_cpu_core_realize(DeviceState *dev, Error **errp)
     void *obj;
     int i, j;
 
-    spapr = (sPAPRMachineState *) qdev_get_machine();
-    if (!object_dynamic_cast((Object *) spapr, TYPE_SPAPR_MACHINE)) {
-        error_setg(errp, "spapr-cpu-core needs a pseries machine");
+    if (!spapr) {
+        error_setg(errp, TYPE_SPAPR_CPU_CORE " needs a pseries machine");
         return;
     }
 
include/hw/ppc/spapr.h +0 -0
index c1b365f56431..4933da8083df 100644

include/hw/ppc/spapr.h typedef struct sPAPRMachineClass sPAPRMachineClass; · line 48

Quoted 6 lines
include/hw/ppc/spapr.h +3 -0
@@ -43,6 +43,9 @@ typedef struct sPAPRMachineClass sPAPRMachineClass;
 #define SPAPR_MACHINE_CLASS(klass) \
     OBJECT_CLASS_CHECK(sPAPRMachineClass, klass, TYPE_SPAPR_MACHINE)
 
+#define SPAPR_MACHINE_NOASSERT(obj) \
+    ((sPAPRMachineState *) object_dynamic_cast(obj, TYPE_SPAPR_MACHINE))
+

I don't think this is a great idea. Doing things with pointers that might not be of the right type should be obvious, not hidden under macros. An opencoded call to object_dynamic_cast is how the rest of the codebase does this; it's a bit of a weird way to write "isinstance()" but there you go. If we want to improve the way we write this sort of thing we should do so as a general improvement to the QOM APIs and conventions, not just a single thing in SPAPR code.

thanks
-- PMM

#3 lore raw
Quoted 52 lines · Peter Maydell
Quoted 51 lines · Greg Kurz

A spapr-cpu-core device can only be plugged into a pseries machine. This is checked in spapr_cpu_core_realize() with object_dynamic_cast() instead of the usual SPAPR_MACHINE() macro because we don't want QEMU to abort.

This patch moves the gory details to a SPAPR_MACHINE_NOASSERT() macro that acts like the SPAPR_MACHINE() one, except it returns NULL instead of aborting if its argument doesn't point to a pseries machine type.

This is done for two reasons:
- it makes the code nicer
- it may be used by other pseries-specific devices like PHBs for example

---
 hw/ppc/spapr_cpu_core.c |    7 +++----
 include/hw/ppc/spapr.h  |    3 +++
 2 files changed, 6 insertions(+), 4 deletions(-)
hw/ppc/spapr_cpu_core.c +3 -4
index 37beb56e8b18..5fde07614218 100644
@@ -199,7 +199,7 @@ error:
 
 static void spapr_cpu_core_realize(DeviceState *dev, Error **errp)
 {
-    sPAPRMachineState *spapr;
+    sPAPRMachineState *spapr = SPAPR_MACHINE_NOASSERT(qdev_get_machine());
     sPAPRCPUCore *sc = SPAPR_CPU_CORE(OBJECT(dev));
     sPAPRCPUCoreClass *scc = SPAPR_CPU_CORE_GET_CLASS(OBJECT(dev));
     CPUCore *cc = CPU_CORE(OBJECT(dev));
@@ -209,9 +209,8 @@ static void spapr_cpu_core_realize(DeviceState *dev, Error **errp)
     void *obj;
     int i, j;
 
-    spapr = (sPAPRMachineState *) qdev_get_machine();
-    if (!object_dynamic_cast((Object *) spapr, TYPE_SPAPR_MACHINE)) {
-        error_setg(errp, "spapr-cpu-core needs a pseries machine");
+    if (!spapr) {
+        error_setg(errp, TYPE_SPAPR_CPU_CORE " needs a pseries machine");
         return;
     }
 
include/hw/ppc/spapr.h +0 -0
index c1b365f56431..4933da8083df 100644
@@ -43,6 +43,9 @@ typedef struct sPAPRMachineClass sPAPRMachineClass;
 #define SPAPR_MACHINE_CLASS(klass) \
     OBJECT_CLASS_CHECK(sPAPRMachineClass, klass, TYPE_SPAPR_MACHINE)
 
Quoted 12 lines

include/hw/ppc/spapr.h typedef struct sPAPRMachineClass sPAPRMachineClass; · line 48

Quoted 3 lines
include/hw/ppc/spapr.h +3 -0
+#define SPAPR_MACHINE_NOASSERT(obj) \
+    ((sPAPRMachineState *) object_dynamic_cast(obj, TYPE_SPAPR_MACHINE))
+

I don't think this is a great idea. Doing things with pointers that might not be of the right type should be obvious, not hidden under macros. An opencoded call to object_dynamic_cast is how the rest of the codebase does this; it's a bit of a weird way to write "isinstance()" but there you go. If we want to improve the way we write this sort of thing we should do so as a general improvement to the QOM APIs and conventions, not just a single thing in SPAPR code.

Yeah, I tend to agree. Sorry, the original advice I gave on not using object_dynamic_cast() directly was bogus - I didn't think it through clearly.

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
#4 lore raw
Quoted 53 lines · On Mon, 9 Oct 2017 10:26:38 +1100 David Gibson <[email protected]>…
Quoted 52 lines · Peter Maydell
Quoted 51 lines · Greg Kurz

A spapr-cpu-core device can only be plugged into a pseries machine. This is checked in spapr_cpu_core_realize() with object_dynamic_cast() instead of the usual SPAPR_MACHINE() macro because we don't want QEMU to abort.

This patch moves the gory details to a SPAPR_MACHINE_NOASSERT() macro that acts like the SPAPR_MACHINE() one, except it returns NULL instead of aborting if its argument doesn't point to a pseries machine type.

This is done for two reasons:
- it makes the code nicer
- it may be used by other pseries-specific devices like PHBs for example

---
 hw/ppc/spapr_cpu_core.c |    7 +++----
 include/hw/ppc/spapr.h  |    3 +++
 2 files changed, 6 insertions(+), 4 deletions(-)
hw/ppc/spapr_cpu_core.c +3 -4
index 37beb56e8b18..5fde07614218 100644
@@ -199,7 +199,7 @@ error:
 
 static void spapr_cpu_core_realize(DeviceState *dev, Error **errp)
 {
-    sPAPRMachineState *spapr;
+    sPAPRMachineState *spapr = SPAPR_MACHINE_NOASSERT(qdev_get_machine());
     sPAPRCPUCore *sc = SPAPR_CPU_CORE(OBJECT(dev));
     sPAPRCPUCoreClass *scc = SPAPR_CPU_CORE_GET_CLASS(OBJECT(dev));
     CPUCore *cc = CPU_CORE(OBJECT(dev));
@@ -209,9 +209,8 @@ static void spapr_cpu_core_realize(DeviceState *dev, Error **errp)
     void *obj;
     int i, j;
 
-    spapr = (sPAPRMachineState *) qdev_get_machine();
-    if (!object_dynamic_cast((Object *) spapr, TYPE_SPAPR_MACHINE)) {
-        error_setg(errp, "spapr-cpu-core needs a pseries machine");
+    if (!spapr) {
+        error_setg(errp, TYPE_SPAPR_CPU_CORE " needs a pseries machine");
         return;
     }
 
include/hw/ppc/spapr.h +0 -0
index c1b365f56431..4933da8083df 100644
@@ -43,6 +43,9 @@ typedef struct sPAPRMachineClass sPAPRMachineClass;
 #define SPAPR_MACHINE_CLASS(klass) \
     OBJECT_CLASS_CHECK(sPAPRMachineClass, klass, TYPE_SPAPR_MACHINE)
 
Quoted 13 lines
Quoted 4 lines

include/hw/ppc/spapr.h typedef struct sPAPRMachineClass sPAPRMachineClass; · line 48

Quoted 3 lines
include/hw/ppc/spapr.h +3 -0
+#define SPAPR_MACHINE_NOASSERT(obj) \
+    ((sPAPRMachineState *) object_dynamic_cast(obj, TYPE_SPAPR_MACHINE))
+  
Quoted 8 lines

I don't think this is a great idea. Doing things with pointers that might not be of the right type should be obvious, not hidden under macros. An opencoded call to object_dynamic_cast is how the rest of the codebase does this; it's a bit of a weird way to write "isinstance()" but there you go. If we want to improve the way we write this sort of thing we should do so as a general improvement to the QOM APIs and conventions, not just a single thing in SPAPR code.

Ok, I agree.

Quoted 4 lines

Yeah, I tend to agree. Sorry, the original advice I gave on not using object_dynamic_cast() directly was bogus - I didn't think it through clearly.

Then maybe you can apply the previous version or do you have another suggestion ?