Skip to contentSkip to conversation

Don't treat all sysbus devices as hotpluggable

PATCH In review Peter Maydell 5 participants 15 messages Mar 25, 2021 – Apr 4, 2021

lore
Share & tools
lore
#0 lore raw

cover Don't treat all sysbus devices as hotpluggable

On the two machines which have the "platform bus" (ppc e500 and arm virt) we currently treat all TYPE_SYS_BUS_DEVICE devices as being hotpluggable in the device callbacks, and try to plug those devices into the platform bus. This is far too broad, because only a handful of devices are actually valid to plug into the platform bus. Moreover, if a device which is pluggable for some other reason (like a PCI device) happens to use a sysbus device internally as part of its implementation, the hotplug callback will incorrectly grab that sysbus device, probably resulting in an assertion failure.

Mostly PCI devices don't use sysbus devices internally, so the only case we've encountered so far is the not-valid-anyway
qemu-system-ppc64 -M ppce500 -device macio-oldworld
but we might create more in future.

This series restricts hotpluggability of sysbus devices on these platforms to those devices which are on the dynamic sysbus whitelist (which we were maintaining anyway). With it, the above ppc commandline stops asserting and instead fails cleanly with
qemu-system-ppc64: Device heathrow is not supported by this machine yet.

Patch 1 is an API doc improvement while I was in the header file anyway.

thanks
-- PMM

Peter Maydell (4):
include/hw/boards.h: Document machine_class_allow_dynamic_sysbus_dev()
machine: Provide a function to check the dynamic sysbus whitelist hw/arm/virt: Only try to add valid dynamic sysbus devices to platform
bus
hw/ppc/e500plat: Only try to add valid dynamic sysbus devices to
platform bus

 include/hw/boards.h | 38 ++++++++++++++++++++++++++++++++++++++
 hw/arm/virt.c       |  8 ++++++--
 hw/core/machine.c   | 21 ++++++++++++++++-----
 hw/ppc/e500plat.c   |  8 ++++++--
 4 files changed, 66 insertions(+), 9 deletions(-)
Signature · 1 line
2.20.1
#1 lore raw

PATCH 1/4 include/hw/boards.h: Document machine_class_allow_dynamic_sysbus_dev()

The function machine_class_allow_dynamic_sysbus_dev() is currently undocumented; add a doc comment.

---
 include/hw/boards.h | 14 ++++++++++++++
 1 file changed, 14 insertions(+)
include/hw/boards.h +14 -0
index 4a90549ad85..27106abc11d 100644
@@ -36,7 +36,21 @@ void machine_set_cpu_numa_node(MachineState *machine,
                                const CpuInstanceProperties *props,
                                Error **errp);
 
+/**
+ * machine_class_allow_dynamic_sysbus_dev: Add type to list of valid devices
+ * @mc: Machine class
+ * @type: type to whitelist (should be a subtype of TYPE_SYS_BUS_DEVICE)
+ *
+ * Add the QOM type @type to the list of devices of which are subtypes
+ * of TYPE_SYS_BUS_DEVICE but which are still permitted to be dynamically
+ * created (eg by the user on the command line with -device).
+ * By default if the user tries to create any devices on the command line
+ * that are subtypes of TYPE_SYS_BUS_DEVICE they will get an error message;
+ * for the special cases which are permitted for this machine model, the
+ * machine model class init code must call this function to whitelist them.
+ */
 void machine_class_allow_dynamic_sysbus_dev(MachineClass *mc, const char *type);
+
 /*
  * Checks that backend isn't used, preps it for exclusive usage and
  * returns migratable MemoryRegion provided by backend.
Signature · 1 line
2.20.1
Reviewed-by on v1 1/4
#2 lore raw

Re: [PATCH for-6.0 1/4] include/hw/boards.h: Document machine_class_allow_dynamic_sysbus_dev()

Hi Peter,

Quoted 13 lines · Peter Maydell

The function machine_class_allow_dynamic_sysbus_dev() is currently undocumented; add a doc comment.

---
 include/hw/boards.h | 14 ++++++++++++++
 1 file changed, 14 insertions(+)
include/hw/boards.h +0 -0
index 4a90549ad85..27106abc11d 100644

include/hw/boards.h void machine_set_cpu_numa_node(MachineState *machine, · line 40

Quoted 5 lines
include/hw/boards.h +2 -0
@@ -36,7 +36,21 @@ void machine_set_cpu_numa_node(MachineState *machine,
                                const CpuInstanceProperties *props,
                                Error **errp);
 
+/**
+ * machine_class_allow_dynamic_sysbus_dev: Add type to list of valid devices

nit: s/of valid devices/of dynamically instantiable sysbus devices ?

Quoted 7 lines
include/hw/boards.h +6 -0
+ * @mc: Machine class
+ * @type: type to whitelist (should be a subtype of TYPE_SYS_BUS_DEVICE)
+ *
+ * Add the QOM type @type to the list of devices of which are subtypes
+ * of TYPE_SYS_BUS_DEVICE but which are still permitted to be dynamically
+ * created (eg by the user on the command line with -device).

include/hw/boards.h void machine_set_cpu_numa_node(MachineState *machine, · line 56

Quoted 10 lines
include/hw/boards.h +6 -0
+ * By default if the user tries to create any devices on the command line
+ * that are subtypes of TYPE_SYS_BUS_DEVICE they will get an error message;
+ * for the special cases which are permitted for this machine model, the
+ * machine model class init code must call this function to whitelist them.
+ */
 void machine_class_allow_dynamic_sysbus_dev(MachineClass *mc, const char *type);
+
 /*
  * Checks that backend isn't used, preps it for exclusive usage and
  * returns migratable MemoryRegion provided by backend.

Besides

Thanks

Eric

#3 lore raw

Re: [PATCH for-6.0 1/4] include/hw/boards.h: Document machine_class_allow_dynamic_sysbus_dev()

Quoted 20 lines · Auger Eric

Hi Peter,

Quoted 16 lines · Peter Maydell

The function machine_class_allow_dynamic_sysbus_dev() is currently undocumented; add a doc comment.

---
 include/hw/boards.h | 14 ++++++++++++++
 1 file changed, 14 insertions(+)
include/hw/boards.h +0 -0
index 4a90549ad85..27106abc11d 100644
@@ -36,7 +36,21 @@ void machine_set_cpu_numa_node(MachineState *machine,
                                const CpuInstanceProperties *props,
                                Error **errp);
 
Quoted 3 lines

include/hw/boards.h void machine_set_cpu_numa_node(MachineState *machine, · line 40

Quoted 2 lines
include/hw/boards.h +2 -0
+/**
+ * machine_class_allow_dynamic_sysbus_dev: Add type to list of valid devices

nit: s/of valid devices/of dynamically instantiable sysbus devices ?

I was trying to keep the summary line to be one line, which doesn't give much space for nuance with a function name this long...

-- PMM

#4 lore raw

Re: [PATCH for-6.0 1/4] include/hw/boards.h: Document machine_class_allow_dynamic_sysbus_dev()

Hi Peter,

Quoted 21 lines · Peter Maydell
Quoted 20 lines · Auger Eric

Hi Peter,

Quoted 16 lines · Peter Maydell

The function machine_class_allow_dynamic_sysbus_dev() is currently undocumented; add a doc comment.

---
 include/hw/boards.h | 14 ++++++++++++++
 1 file changed, 14 insertions(+)
include/hw/boards.h +0 -0
index 4a90549ad85..27106abc11d 100644
@@ -36,7 +36,21 @@ void machine_set_cpu_numa_node(MachineState *machine,
                                const CpuInstanceProperties *props,
                                Error **errp);
 
Quoted 6 lines
Quoted 3 lines

include/hw/boards.h void machine_set_cpu_numa_node(MachineState *machine, · line 40

Quoted 2 lines
include/hw/boards.h +2 -0
+/**
+ * machine_class_allow_dynamic_sysbus_dev: Add type to list of valid devices

nit: s/of valid devices/of dynamically instantiable sysbus devices ?

I was trying to keep the summary line to be one line, which doesn't give much space for nuance with a function name this long...

OK no worries

Thanks

Eric

Quoted 4 lines

-- PMM

#5 lore raw

PATCH 2/4 machine: Provide a function to check the dynamic sysbus whitelist

In reply to Peter Maydell

Provide a new function dynamic_sysbus_dev_allowed() which checks the per-machine whitelist of dynamic sysbus devices and returns a boolean result indicating whether the device is whitelisted. We can use this in the implementation of validate_sysbus_device(), but we will also need it so that machine hotplug callbacks can validate devices rather than assuming that any sysbus device might be hotpluggable into the platform bus.

---
 include/hw/boards.h | 24 ++++++++++++++++++++++++
 hw/core/machine.c   | 21 ++++++++++++++++-----
 2 files changed, 40 insertions(+), 5 deletions(-)
include/hw/boards.h +24 -0
index 27106abc11d..609112a4e1a 100644
@@ -51,6 +51,30 @@ void machine_set_cpu_numa_node(MachineState *machine,
  */
 void machine_class_allow_dynamic_sysbus_dev(MachineClass *mc, const char *type);
 
+/**
+ * device_is_dynamic_sysbus: test whether device is a dynamic sysbus device
+ * @mc: Machine class
+ * @dev: device to check
+ *
+ * Returns: true if @dev is a sysbus device on the machine's whitelist
+ * of dynamically pluggable sysbus devices; otherwise false.
+ *
+ * This function checks whether @dev is a valid dynamic sysbus device,
+ * by first confirming that it is a sysbus device and then checking it
+ * against the whitelist of permitted dynamic sysbus devices which has
+ * been set up by the machine using machine_class_allow_dynamic_sysbus_dev().
+ *
+ * It is valid to call this with something that is not a subclass of
+ * TYPE_SYS_BUS_DEVICE; the function will return false in this case.
+ * This allows hotplug callback functions to be written as:
+ *     if (device_is_dynamic_sysbus(mc, dev)) {
+ *         handle dynamic sysbus case;
+ *     } else if (some other kind of hotplug) {
+ *         handle that;
+ *     }
+ */
+bool device_is_dynamic_sysbus(MachineClass *mc, DeviceState *dev);
+
 /*
  * Checks that backend isn't used, preps it for exclusive usage and
  * returns migratable MemoryRegion provided by backend.
hw/core/machine.c +16 -5
index 9935c6ddd56..8d97094736a 100644
@@ -529,20 +529,31 @@ void machine_class_allow_dynamic_sysbus_dev(MachineClass *mc, const char *type)
     QAPI_LIST_PREPEND(mc->allowed_dynamic_sysbus_devices, g_strdup(type));
 }
 
-static void validate_sysbus_device(SysBusDevice *sbdev, void *opaque)
+bool device_is_dynamic_sysbus(MachineClass *mc, DeviceState *dev)
 {
-    MachineState *machine = opaque;
-    MachineClass *mc = MACHINE_GET_CLASS(machine);
     bool allowed = false;
     strList *wl;
+    Object *obj = OBJECT(dev);
+
+    if (!object_dynamic_cast(obj, TYPE_SYS_BUS_DEVICE)) {
+        return false;
+    }
 
     for (wl = mc->allowed_dynamic_sysbus_devices;
          !allowed && wl;
          wl = wl->next) {
-        allowed |= !!object_dynamic_cast(OBJECT(sbdev), wl->value);
+        allowed |= !!object_dynamic_cast(obj, wl->value);
     }
 
-    if (!allowed) {
+    return allowed;
+}
+
+static void validate_sysbus_device(SysBusDevice *sbdev, void *opaque)
+{
+    MachineState *machine = opaque;
+    MachineClass *mc = MACHINE_GET_CLASS(machine);
+
+    if (!device_is_dynamic_sysbus(mc, DEVICE(sbdev))) {
         error_report("Option '-device %s' cannot be handled by this machine",
                      object_class_get_name(object_get_class(OBJECT(sbdev))));
         exit(1);
Signature · 1 line
2.20.1
Reviewed-by on v1 2/4
#6 lore raw

Re: [PATCH for-6.0 2/4] machine: Provide a function to check the dynamic sysbus whitelist

Hi Peter,

Quoted 9 lines · Peter Maydell

Provide a new function dynamic_sysbus_dev_allowed() which checks the per-machine whitelist of dynamic sysbus devices and returns a boolean result indicating whether the device is whitelisted. We can use this in the implementation of validate_sysbus_device(), but we will also need it so that machine hotplug callbacks can validate devices rather than assuming that any sysbus device might be hotpluggable into the platform bus.

Thanks

Eric

Quoted 82 lines
---
 include/hw/boards.h | 24 ++++++++++++++++++++++++
 hw/core/machine.c   | 21 ++++++++++++++++-----
 2 files changed, 40 insertions(+), 5 deletions(-)
include/hw/boards.h +24 -0
index 27106abc11d..609112a4e1a 100644
@@ -51,6 +51,30 @@ void machine_set_cpu_numa_node(MachineState *machine,
  */
 void machine_class_allow_dynamic_sysbus_dev(MachineClass *mc, const char *type);
 
+/**
+ * device_is_dynamic_sysbus: test whether device is a dynamic sysbus device
+ * @mc: Machine class
+ * @dev: device to check
+ *
+ * Returns: true if @dev is a sysbus device on the machine's whitelist
+ * of dynamically pluggable sysbus devices; otherwise false.
+ *
+ * This function checks whether @dev is a valid dynamic sysbus device,
+ * by first confirming that it is a sysbus device and then checking it
+ * against the whitelist of permitted dynamic sysbus devices which has
+ * been set up by the machine using machine_class_allow_dynamic_sysbus_dev().
+ *
+ * It is valid to call this with something that is not a subclass of
+ * TYPE_SYS_BUS_DEVICE; the function will return false in this case.
+ * This allows hotplug callback functions to be written as:
+ *     if (device_is_dynamic_sysbus(mc, dev)) {
+ *         handle dynamic sysbus case;
+ *     } else if (some other kind of hotplug) {
+ *         handle that;
+ *     }
+ */
+bool device_is_dynamic_sysbus(MachineClass *mc, DeviceState *dev);
+
 /*
  * Checks that backend isn't used, preps it for exclusive usage and
  * returns migratable MemoryRegion provided by backend.
hw/core/machine.c +16 -5
index 9935c6ddd56..8d97094736a 100644
@@ -529,20 +529,31 @@ void machine_class_allow_dynamic_sysbus_dev(MachineClass *mc, const char *type)
     QAPI_LIST_PREPEND(mc->allowed_dynamic_sysbus_devices, g_strdup(type));
 }
 
-static void validate_sysbus_device(SysBusDevice *sbdev, void *opaque)
+bool device_is_dynamic_sysbus(MachineClass *mc, DeviceState *dev)
 {
-    MachineState *machine = opaque;
-    MachineClass *mc = MACHINE_GET_CLASS(machine);
     bool allowed = false;
     strList *wl;
+    Object *obj = OBJECT(dev);
+
+    if (!object_dynamic_cast(obj, TYPE_SYS_BUS_DEVICE)) {
+        return false;
+    }
 
     for (wl = mc->allowed_dynamic_sysbus_devices;
          !allowed && wl;
          wl = wl->next) {
-        allowed |= !!object_dynamic_cast(OBJECT(sbdev), wl->value);
+        allowed |= !!object_dynamic_cast(obj, wl->value);
     }
 
-    if (!allowed) {
+    return allowed;
+}
+
+static void validate_sysbus_device(SysBusDevice *sbdev, void *opaque)
+{
+    MachineState *machine = opaque;
+    MachineClass *mc = MACHINE_GET_CLASS(machine);
+
+    if (!device_is_dynamic_sysbus(mc, DEVICE(sbdev))) {
         error_report("Option '-device %s' cannot be handled by this machine",
                      object_class_get_name(object_get_class(OBJECT(sbdev))));
         exit(1);
#7 lore raw

PATCH 3/4 hw/arm/virt: Only try to add valid dynamic sysbus devices to platform bus

In reply to Peter Maydell

The virt machine device plug callback currently calls platform_bus_link_device() for any sysbus device. This is overly broad, because platform_bus_link_device() will unconditionally grab the IRQs and MMIOs of the device it is passed, whether it was intended for the platform bus or not. Restrict hotpluggability of sysbus devices to only those devices on the dynamic sysbus whitelist.

We were mostly getting away with this because the board creates the platform bus as the last device it creates, and so the hotplug callback did not do anything for all the sysbus devices created by the board itself. However if the user plugged in a device which itself uses a sysbus device internally we would have mishandled this and probably asserted.

---
 hw/arm/virt.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
hw/arm/virt.c +6 -2
index aa2bbd14e09..8625152a735 100644
@@ -2443,7 +2443,9 @@ static void virt_machine_device_plug_cb(HotplugHandler *hotplug_dev,
     VirtMachineState *vms = VIRT_MACHINE(hotplug_dev);
 
     if (vms->platform_bus_dev) {
-        if (object_dynamic_cast(OBJECT(dev), TYPE_SYS_BUS_DEVICE)) {
+        MachineClass *mc = MACHINE_GET_CLASS(vms);
+
+        if (device_is_dynamic_sysbus(mc, dev)) {
             platform_bus_link_device(PLATFORM_BUS_DEVICE(vms->platform_bus_dev),
                                      SYS_BUS_DEVICE(dev));
         }
@@ -2527,7 +2529,9 @@ static void virt_machine_device_unplug_cb(HotplugHandler *hotplug_dev,
 static HotplugHandler *virt_machine_get_hotplug_handler(MachineState *machine,
                                                         DeviceState *dev)
 {
-    if (object_dynamic_cast(OBJECT(dev), TYPE_SYS_BUS_DEVICE) ||
+    MachineClass *mc = MACHINE_GET_CLASS(machine);
+
+    if (device_is_dynamic_sysbus(mc, dev) ||
        (object_dynamic_cast(OBJECT(dev), TYPE_PC_DIMM))) {
         return HOTPLUG_HANDLER(machine);
     }
Signature · 1 line
2.20.1
Reviewed-by on v1 3/4
#8 lore raw

Re: [PATCH for-6.0 3/4] hw/arm/virt: Only try to add valid dynamic sysbus devices to platform bus

Hi Peter,

Quoted 7 lines · Peter Maydell

The virt machine device plug callback currently calls platform_bus_link_device() for any sysbus device. This is overly broad, because platform_bus_link_device() will unconditionally grab the IRQs and MMIOs of the device it is passed, whether it was intended for the platform bus or not. Restrict hotpluggability of sysbus devices to only those devices on the dynamic sysbus whitelist.

Quoted 8 lines

We were mostly getting away with this because the board creates the platform bus as the last device it creates, and so the hotplug callback did not do anything for all the sysbus devices created by the board itself. However if the user plugged in a device which itself uses a sysbus device internally we would have mishandled this and probably asserted.

Thanks

Eric

Quoted 31 lines
---
 hw/arm/virt.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
hw/arm/virt.c +6 -2
index aa2bbd14e09..8625152a735 100644
@@ -2443,7 +2443,9 @@ static void virt_machine_device_plug_cb(HotplugHandler *hotplug_dev,
     VirtMachineState *vms = VIRT_MACHINE(hotplug_dev);
 
     if (vms->platform_bus_dev) {
-        if (object_dynamic_cast(OBJECT(dev), TYPE_SYS_BUS_DEVICE)) {
+        MachineClass *mc = MACHINE_GET_CLASS(vms);
+
+        if (device_is_dynamic_sysbus(mc, dev)) {
             platform_bus_link_device(PLATFORM_BUS_DEVICE(vms->platform_bus_dev),
                                      SYS_BUS_DEVICE(dev));
         }
@@ -2527,7 +2529,9 @@ static void virt_machine_device_unplug_cb(HotplugHandler *hotplug_dev,
 static HotplugHandler *virt_machine_get_hotplug_handler(MachineState *machine,
                                                         DeviceState *dev)
 {
-    if (object_dynamic_cast(OBJECT(dev), TYPE_SYS_BUS_DEVICE) ||
+    MachineClass *mc = MACHINE_GET_CLASS(machine);
+
+    if (device_is_dynamic_sysbus(mc, dev) ||
        (object_dynamic_cast(OBJECT(dev), TYPE_PC_DIMM))) {
         return HOTPLUG_HANDLER(machine);
     }
#9 lore raw

PATCH 4/4 hw/ppc/e500plat: Only try to add valid dynamic sysbus devices to platform bus

In reply to Peter Maydell

The e500plat machine device plug callback currently calls platform_bus_link_device() for any sysbus device. This is overly broad, because platform_bus_link_device() will unconditionally grab the IRQs and MMIOs of the device it is passed, whether it was intended for the platform bus or not. Restrict hotpluggability of sysbus devices to only those devices on the dynamic sysbus whitelist.

We were mostly getting away with this because the board creates the platform bus as the last device it creates, and so the hotplug callback did not do anything for all the sysbus devices created by the board itself. However if the user plugged in a device which itself uses a sysbus device internally we would have mishandled this and probably asserted. An example of this is:
qemu-system-ppc64 -M ppce500 -device macio-oldworld

This isn't a sensible command because the macio-oldworld device is really specific to the 'g3beige' machine, but we now fail with a reasonable error message rather than asserting:
qemu-system-ppc64: Device heathrow is not supported by this machine yet.

---
 hw/ppc/e500plat.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
hw/ppc/e500plat.c +6 -2
index bddd5e7c48f..fc911bbb7bd 100644
@@ -48,7 +48,9 @@ static void e500plat_machine_device_plug_cb(HotplugHandler *hotplug_dev,
     PPCE500MachineState *pms = PPCE500_MACHINE(hotplug_dev);
 
     if (pms->pbus_dev) {
-        if (object_dynamic_cast(OBJECT(dev), TYPE_SYS_BUS_DEVICE)) {
+        MachineClass *mc = MACHINE_GET_CLASS(pms);
+
+        if (device_is_dynamic_sysbus(mc, dev)) {
             platform_bus_link_device(pms->pbus_dev, SYS_BUS_DEVICE(dev));
         }
     }
@@ -58,7 +60,9 @@ static
 HotplugHandler *e500plat_machine_get_hotpug_handler(MachineState *machine,
                                                     DeviceState *dev)
 {
-    if (object_dynamic_cast(OBJECT(dev), TYPE_SYS_BUS_DEVICE)) {
+    MachineClass *mc = MACHINE_GET_CLASS(machine);
+
+    if (device_is_dynamic_sysbus(mc, dev)) {
         return HOTPLUG_HANDLER(machine);
     }
 
Signature · 1 line
2.20.1
Acked-by on v1 4/4
#10 lore raw

Re: [PATCH for-6.0 4/4] hw/ppc/e500plat: Only try to add valid dynamic sysbus devices to platform bus

Quoted 15 lines · Peter Maydell

The e500plat machine device plug callback currently calls platform_bus_link_device() for any sysbus device. This is overly broad, because platform_bus_link_device() will unconditionally grab the IRQs and MMIOs of the device it is passed, whether it was intended for the platform bus or not. Restrict hotpluggability of sysbus devices to only those devices on the dynamic sysbus whitelist.

We were mostly getting away with this because the board creates the platform bus as the last device it creates, and so the hotplug callback did not do anything for all the sysbus devices created by the board itself. However if the user plugged in a device which itself uses a sysbus device internally we would have mishandled this and probably asserted. An example of this is:
qemu-system-ppc64 -M ppce500 -device macio-oldworld

Quoted 6 lines

This isn't a sensible command because the macio-oldworld device is really specific to the 'g3beige' machine, but we now fail with a reasonable error message rather than asserting:
qemu-system-ppc64: Device heathrow is not supported by this machine yet.

Quoted 30 lines
---
 hw/ppc/e500plat.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
hw/ppc/e500plat.c +6 -2
index bddd5e7c48f..fc911bbb7bd 100644
@@ -48,7 +48,9 @@ static void e500plat_machine_device_plug_cb(HotplugHandler *hotplug_dev,
     PPCE500MachineState *pms = PPCE500_MACHINE(hotplug_dev);
 
     if (pms->pbus_dev) {
-        if (object_dynamic_cast(OBJECT(dev), TYPE_SYS_BUS_DEVICE)) {
+        MachineClass *mc = MACHINE_GET_CLASS(pms);
+
+        if (device_is_dynamic_sysbus(mc, dev)) {
             platform_bus_link_device(pms->pbus_dev, SYS_BUS_DEVICE(dev));
         }
     }
@@ -58,7 +60,9 @@ static
 HotplugHandler *e500plat_machine_get_hotpug_handler(MachineState *machine,
                                                     DeviceState *dev)
 {
-    if (object_dynamic_cast(OBJECT(dev), TYPE_SYS_BUS_DEVICE)) {
+    MachineClass *mc = MACHINE_GET_CLASS(machine);
+
+    if (device_is_dynamic_sysbus(mc, dev)) {
         return HOTPLUG_HANDLER(machine);
     }
 
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
Reviewed-by on v1 4/4
#11 lore raw

Re: [PATCH for-6.0 4/4] hw/ppc/e500plat: Only try to add valid dynamic sysbus devices to platform bus

In reply to Peter Maydell
Quoted 15 lines · Peter Maydell

The e500plat machine device plug callback currently calls platform_bus_link_device() for any sysbus device. This is overly broad, because platform_bus_link_device() will unconditionally grab the IRQs and MMIOs of the device it is passed, whether it was intended for the platform bus or not. Restrict hotpluggability of sysbus devices to only those devices on the dynamic sysbus whitelist.

We were mostly getting away with this because the board creates the platform bus as the last device it creates, and so the hotplug callback did not do anything for all the sysbus devices created by the board itself. However if the user plugged in a device which itself uses a sysbus device internally we would have mishandled this and probably asserted. An example of this is:
qemu-system-ppc64 -M ppce500 -device macio-oldworld

Quoted 6 lines

This isn't a sensible command because the macio-oldworld device is really specific to the 'g3beige' machine, but we now fail with a reasonable error message rather than asserting:
qemu-system-ppc64: Device heathrow is not supported by this machine yet.

Eric

Quoted 31 lines
---
 hw/ppc/e500plat.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
hw/ppc/e500plat.c +6 -2
index bddd5e7c48f..fc911bbb7bd 100644
@@ -48,7 +48,9 @@ static void e500plat_machine_device_plug_cb(HotplugHandler *hotplug_dev,
     PPCE500MachineState *pms = PPCE500_MACHINE(hotplug_dev);
 
     if (pms->pbus_dev) {
-        if (object_dynamic_cast(OBJECT(dev), TYPE_SYS_BUS_DEVICE)) {
+        MachineClass *mc = MACHINE_GET_CLASS(pms);
+
+        if (device_is_dynamic_sysbus(mc, dev)) {
             platform_bus_link_device(pms->pbus_dev, SYS_BUS_DEVICE(dev));
         }
     }
@@ -58,7 +60,9 @@ static
 HotplugHandler *e500plat_machine_get_hotpug_handler(MachineState *machine,
                                                     DeviceState *dev)
 {
-    if (object_dynamic_cast(OBJECT(dev), TYPE_SYS_BUS_DEVICE)) {
+    MachineClass *mc = MACHINE_GET_CLASS(machine);
+
+    if (device_is_dynamic_sysbus(mc, dev)) {
         return HOTPLUG_HANDLER(machine);
     }
 
Reviewed-by on the v1 series
#12 lore raw
In reply to Peter Maydell
Quoted 10 lines · Peter Maydell

On the two machines which have the "platform bus" (ppc e500 and arm virt) we currently treat all TYPE_SYS_BUS_DEVICE devices as being hotpluggable in the device callbacks, and try to plug those devices into the platform bus. This is far too broad, because only a handful of devices are actually valid to plug into the platform bus. Moreover, if a device which is pluggable for some other reason (like a PCI device) happens to use a sysbus device internally as part of its implementation, the hotplug callback will incorrectly grab that sysbus device, probably resulting in an assertion failure.

Quoted 7 lines

Mostly PCI devices don't use sysbus devices internally, so the only case we've encountered so far is the not-valid-anyway
qemu-system-ppc64 -M ppce500 -device macio-oldworld
but we might create more in future.

This series restricts hotpluggability of sysbus devices on these platforms to those devices which are on the dynamic sysbus whitelist

s/whitelist/allowlist/g ?

Quoted 6 lines

include/hw/boards.h: Document machine_class_allow_dynamic_sysbus_dev()
machine: Provide a function to check the dynamic sysbus whitelist hw/arm/virt: Only try to add valid dynamic sysbus devices to platform
bus
hw/ppc/e500plat: Only try to add valid dynamic sysbus devices to
platform bus

Otherwise,

r~

Reviewed-by on the v1 series
#13 lore raw
In reply to Peter Maydell
Quoted 35 lines · Peter Maydell

On the two machines which have the "platform bus" (ppc e500 and arm virt) we currently treat all TYPE_SYS_BUS_DEVICE devices as being hotpluggable in the device callbacks, and try to plug those devices into the platform bus. This is far too broad, because only a handful of devices are actually valid to plug into the platform bus. Moreover, if a device which is pluggable for some other reason (like a PCI device) happens to use a sysbus device internally as part of its implementation, the hotplug callback will incorrectly grab that sysbus device, probably resulting in an assertion failure.

Mostly PCI devices don't use sysbus devices internally, so the only case we've encountered so far is the not-valid-anyway
qemu-system-ppc64 -M ppce500 -device macio-oldworld
but we might create more in future.

This series restricts hotpluggability of sysbus devices on these platforms to those devices which are on the dynamic sysbus whitelist (which we were maintaining anyway). With it, the above ppc commandline stops asserting and instead fails cleanly with
qemu-system-ppc64: Device heathrow is not supported by this machine yet.

Patch 1 is an API doc improvement while I was in the header file anyway.

thanks
-- PMM

Peter Maydell (4):
include/hw/boards.h: Document machine_class_allow_dynamic_sysbus_dev()
machine: Provide a function to check the dynamic sysbus whitelist hw/arm/virt: Only try to add valid dynamic sysbus devices to platform
bus
hw/ppc/e500plat: Only try to add valid dynamic sysbus devices to
platform bus

Quoted 5 lines
  include/hw/boards.h | 38 ++++++++++++++++++++++++++++++++++++++
  hw/arm/virt.c       |  8 ++++++--
  hw/core/machine.c   | 21 ++++++++++++++++-----
  hw/ppc/e500plat.c   |  8 ++++++--
  4 files changed, 66 insertions(+), 9 deletions(-)

Looks good to me having been poking around the code earlier today so:

ATB,

Mark.

#14 lore raw
In reply to Peter Maydell
Quoted 10 lines · Peter Maydell

On the two machines which have the "platform bus" (ppc e500 and arm virt) we currently treat all TYPE_SYS_BUS_DEVICE devices as being hotpluggable in the device callbacks, and try to plug those devices into the platform bus. This is far too broad, because only a handful of devices are actually valid to plug into the platform bus. Moreover, if a device which is pluggable for some other reason (like a PCI device) happens to use a sysbus device internally as part of its implementation, the hotplug callback will incorrectly grab that sysbus device, probably resulting in an assertion failure.

I'm taking this into target-arm.next for 6.0 (with minor commit message and comment text tweaks to use 'allowlist' as suggested by rth).

thanks
-- PMM