Skip to contentSkip to conversation

qdev: Check for the availability of a hotplug controller before adding a device

PATCH Superseded Thomas Huth 3 participants 10 messages Oct 3, 2017 – Oct 5, 2017

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 qdev: Check for the availability of a hotplug controller before adding a device

The qdev_unplug() function contains a g_assert(hotplug_ctrl) statement, so QEMU crashes when the user tries to device_add + device_del a device that does not have a corresponding hotplug controller. This could be provoked for a couple of devices in the past (see commit 4c93950659487c7ad or 84ebd3e8c7d4fe955 for example). So devices clearly need a hotplug controller when they are suitable for device_add.
The code in qdev_device_add() already checks whether the bus has a proper hotplug controller, but for devices that do not have a corresponding bus, there is no appropriate check available. In that case we should check whether the machine itself provides a suitable hotplug controller and refuse to plug the device if none is available.

---
This is the follow-up patch from my earlier try "hw/core/qdev: Do not allow hot-plugging without hotplug controller" ... AFAICS the function qdev_device_add() is now the right spot to do the check.

 hw/core/qdev.c         | 28 ++++++++++++++++++++--------
 include/hw/qdev-core.h |  1 +
 qdev-monitor.c         |  9 +++++++++
 3 files changed, 30 insertions(+), 8 deletions(-)
hw/core/qdev.c +20 -8
index 606ab53..a953ec9 100644
@@ -253,19 +253,31 @@ void qdev_set_legacy_instance_id(DeviceState *dev, int alias_id,
     dev->alias_required_for_version = required_for_version;
 }
 
+HotplugHandler *qdev_get_machine_hotplug_handler(DeviceState *dev)
+{
+    MachineState *machine;
+    MachineClass *mc;
+    Object *m_obj = qdev_get_machine();
+
+    if (object_dynamic_cast(m_obj, TYPE_MACHINE)) {
+        machine = MACHINE(m_obj);
+        mc = MACHINE_GET_CLASS(machine);
+        if (mc->get_hotplug_handler) {
+            return mc->get_hotplug_handler(machine, dev);
+        }
+    }
+
+    return NULL;
+}
+
 HotplugHandler *qdev_get_hotplug_handler(DeviceState *dev)
 {
-    HotplugHandler *hotplug_ctrl = NULL;
+    HotplugHandler *hotplug_ctrl;
 
     if (dev->parent_bus && dev->parent_bus->hotplug_handler) {
         hotplug_ctrl = dev->parent_bus->hotplug_handler;
-    } else if (object_dynamic_cast(qdev_get_machine(), TYPE_MACHINE)) {
-        MachineState *machine = MACHINE(qdev_get_machine());
-        MachineClass *mc = MACHINE_GET_CLASS(machine);
-
-        if (mc->get_hotplug_handler) {
-            hotplug_ctrl = mc->get_hotplug_handler(machine, dev);
-        }
+    } else {
+        hotplug_ctrl = qdev_get_machine_hotplug_handler(dev);
     }
     return hotplug_ctrl;
 }
include/hw/qdev-core.h +1 -0
index 0891461..5aa536d 100644
@@ -285,6 +285,7 @@ DeviceState *qdev_try_create(BusState *bus, const char *name);
 void qdev_init_nofail(DeviceState *dev);
 void qdev_set_legacy_instance_id(DeviceState *dev, int alias_id,
                                  int required_for_version);
+HotplugHandler *qdev_get_machine_hotplug_handler(DeviceState *dev);
 HotplugHandler *qdev_get_hotplug_handler(DeviceState *dev);
 void qdev_unplug(DeviceState *dev, Error **errp);
 void qdev_simple_device_unplug_cb(HotplugHandler *hotplug_dev,
qdev-monitor.c +9 -0
index 8fd6df9..2891dde 100644
@@ -626,6 +626,15 @@ DeviceState *qdev_device_add(QemuOpts *opts, Error **errp)
         return NULL;
     }
 
+    /* In case we don't have a bus, there must be a machine hotplug handler */
+    if (qdev_hotplug && !bus && !qdev_get_machine_hotplug_handler(dev)) {
+        error_setg(errp, "Device '%s' can not be hotplugged on this machine",
+                   driver);
+        object_unparent(OBJECT(dev));
+        object_unref(OBJECT(dev));
+        return NULL;
+    }
+
     dev->opts = opts;
     object_property_set_bool(OBJECT(dev), true, "realized", &err);
     if (err != NULL) {
Signature · 1 line
1.8.3.1
#1 lore raw
Quoted 84 lines · Thomas Huth

The qdev_unplug() function contains a g_assert(hotplug_ctrl) statement, so QEMU crashes when the user tries to device_add + device_del a device that does not have a corresponding hotplug controller. This could be provoked for a couple of devices in the past (see commit 4c93950659487c7ad or 84ebd3e8c7d4fe955 for example). So devices clearly need a hotplug controller when they are suitable for device_add.
The code in qdev_device_add() already checks whether the bus has a proper hotplug controller, but for devices that do not have a corresponding bus, there is no appropriate check available. In that case we should check whether the machine itself provides a suitable hotplug controller and refuse to plug the device if none is available.

---
This is the follow-up patch from my earlier try "hw/core/qdev: Do not allow hot-plugging without hotplug controller" ... AFAICS the function qdev_device_add() is now the right spot to do the check.

 hw/core/qdev.c         | 28 ++++++++++++++++++++--------
 include/hw/qdev-core.h |  1 +
 qdev-monitor.c         |  9 +++++++++
 3 files changed, 30 insertions(+), 8 deletions(-)
hw/core/qdev.c +20 -8
index 606ab53..a953ec9 100644
@@ -253,19 +253,31 @@ void qdev_set_legacy_instance_id(DeviceState *dev, int alias_id,
     dev->alias_required_for_version = required_for_version;
 }
 
+HotplugHandler *qdev_get_machine_hotplug_handler(DeviceState *dev)
+{
+    MachineState *machine;
+    MachineClass *mc;
+    Object *m_obj = qdev_get_machine();
+
+    if (object_dynamic_cast(m_obj, TYPE_MACHINE)) {
+        machine = MACHINE(m_obj);
+        mc = MACHINE_GET_CLASS(machine);
+        if (mc->get_hotplug_handler) {
+            return mc->get_hotplug_handler(machine, dev);
+        }
+    }
+
+    return NULL;
+}
+
 HotplugHandler *qdev_get_hotplug_handler(DeviceState *dev)
 {
-    HotplugHandler *hotplug_ctrl = NULL;
+    HotplugHandler *hotplug_ctrl;
 
     if (dev->parent_bus && dev->parent_bus->hotplug_handler) {
         hotplug_ctrl = dev->parent_bus->hotplug_handler;
-    } else if (object_dynamic_cast(qdev_get_machine(), TYPE_MACHINE)) {
-        MachineState *machine = MACHINE(qdev_get_machine());
-        MachineClass *mc = MACHINE_GET_CLASS(machine);
-
-        if (mc->get_hotplug_handler) {
-            hotplug_ctrl = mc->get_hotplug_handler(machine, dev);
-        }
+    } else {
+        hotplug_ctrl = qdev_get_machine_hotplug_handler(dev);
     }
     return hotplug_ctrl;
 }
include/hw/qdev-core.h +1 -0
index 0891461..5aa536d 100644
@@ -285,6 +285,7 @@ DeviceState *qdev_try_create(BusState *bus, const char *name);
 void qdev_init_nofail(DeviceState *dev);
 void qdev_set_legacy_instance_id(DeviceState *dev, int alias_id,
                                  int required_for_version);
+HotplugHandler *qdev_get_machine_hotplug_handler(DeviceState *dev);
 HotplugHandler *qdev_get_hotplug_handler(DeviceState *dev);
 void qdev_unplug(DeviceState *dev, Error **errp);
 void qdev_simple_device_unplug_cb(HotplugHandler *hotplug_dev,
qdev-monitor.c +0 -0
index 8fd6df9..2891dde 100644

qdev-monitor.c DeviceState *qdev_device_add(QemuOpts *opts, Error **errp) · line 633

Quoted 8 lines
qdev-monitor.c +5 -0
@@ -626,6 +626,15 @@ DeviceState *qdev_device_add(QemuOpts *opts, Error **errp)
         return NULL;
     }
 
+    /* In case we don't have a bus, there must be a machine hotplug handler */
+    if (qdev_hotplug && !bus && !qdev_get_machine_hotplug_handler(dev)) {
+        error_setg(errp, "Device '%s' can not be hotplugged on this machine",
+                   driver);
+        object_unparent(OBJECT(dev));

Isn't it better to check qdev_get_machine_hotplug_handler() earlier (before the qdev_set_parent_bus() and qdev_set_id() lines), so object_unparent() isn't necessary?

(We probably don't need to call object_unparent() here, already, because bus is NULL. But moving the check before the "if (bus) qdev_set_parent_bus()" statement would make this more obvious).

I would prefer to eventually make MachineClass::get_hotplug_handler() get a typename or DeviceClass* argument instead of DeviceState*, so we don't even create the device object. But I don't think it's a requirement for this bug fix.

Quoted 10 lines
qdev-monitor.c +4 -0
+        object_unref(OBJECT(dev));
+        return NULL;
+    }
+
     dev->opts = opts;
     object_property_set_bool(OBJECT(dev), true, "realized", &err);
     if (err != NULL) {
Signature · 1 line
1.8.3.1
Signature · 1 line
Eduardo
#6 lore raw
In reply to Thomas Huth
Quoted 84 lines · On Tue, 3 Oct 2017 18:46:02 +0200 Thomas Huth <[email protected]> wrote:

The qdev_unplug() function contains a g_assert(hotplug_ctrl) statement, so QEMU crashes when the user tries to device_add + device_del a device that does not have a corresponding hotplug controller. This could be provoked for a couple of devices in the past (see commit 4c93950659487c7ad or 84ebd3e8c7d4fe955 for example). So devices clearly need a hotplug controller when they are suitable for device_add.
The code in qdev_device_add() already checks whether the bus has a proper hotplug controller, but for devices that do not have a corresponding bus, there is no appropriate check available. In that case we should check whether the machine itself provides a suitable hotplug controller and refuse to plug the device if none is available.

---
This is the follow-up patch from my earlier try "hw/core/qdev: Do not allow hot-plugging without hotplug controller" ... AFAICS the function qdev_device_add() is now the right spot to do the check.

 hw/core/qdev.c         | 28 ++++++++++++++++++++--------
 include/hw/qdev-core.h |  1 +
 qdev-monitor.c         |  9 +++++++++
 3 files changed, 30 insertions(+), 8 deletions(-)
hw/core/qdev.c +20 -8
index 606ab53..a953ec9 100644
@@ -253,19 +253,31 @@ void qdev_set_legacy_instance_id(DeviceState *dev, int alias_id,
     dev->alias_required_for_version = required_for_version;
 }
 
+HotplugHandler *qdev_get_machine_hotplug_handler(DeviceState *dev)
+{
+    MachineState *machine;
+    MachineClass *mc;
+    Object *m_obj = qdev_get_machine();
+
+    if (object_dynamic_cast(m_obj, TYPE_MACHINE)) {
+        machine = MACHINE(m_obj);
+        mc = MACHINE_GET_CLASS(machine);
+        if (mc->get_hotplug_handler) {
+            return mc->get_hotplug_handler(machine, dev);
+        }
+    }
+
+    return NULL;
+}
+
 HotplugHandler *qdev_get_hotplug_handler(DeviceState *dev)
 {
-    HotplugHandler *hotplug_ctrl = NULL;
+    HotplugHandler *hotplug_ctrl;
 
     if (dev->parent_bus && dev->parent_bus->hotplug_handler) {
         hotplug_ctrl = dev->parent_bus->hotplug_handler;
-    } else if (object_dynamic_cast(qdev_get_machine(), TYPE_MACHINE)) {
-        MachineState *machine = MACHINE(qdev_get_machine());
-        MachineClass *mc = MACHINE_GET_CLASS(machine);
-
-        if (mc->get_hotplug_handler) {
-            hotplug_ctrl = mc->get_hotplug_handler(machine, dev);
-        }
+    } else {
+        hotplug_ctrl = qdev_get_machine_hotplug_handler(dev);
     }
     return hotplug_ctrl;
 }
include/hw/qdev-core.h +1 -0
index 0891461..5aa536d 100644
@@ -285,6 +285,7 @@ DeviceState *qdev_try_create(BusState *bus, const char *name);
 void qdev_init_nofail(DeviceState *dev);
 void qdev_set_legacy_instance_id(DeviceState *dev, int alias_id,
                                  int required_for_version);
+HotplugHandler *qdev_get_machine_hotplug_handler(DeviceState *dev);
 HotplugHandler *qdev_get_hotplug_handler(DeviceState *dev);
 void qdev_unplug(DeviceState *dev, Error **errp);
 void qdev_simple_device_unplug_cb(HotplugHandler *hotplug_dev,
qdev-monitor.c +0 -0
index 8fd6df9..2891dde 100644

qdev-monitor.c DeviceState *qdev_device_add(QemuOpts *opts, Error **errp) · line 630

Quoted 5 lines
qdev-monitor.c +2 -0
@@ -626,6 +626,15 @@ DeviceState *qdev_device_add(QemuOpts *opts, Error **errp)
         return NULL;
     }
 
+    /* In case we don't have a bus, there must be a machine hotplug handler */
+    if (qdev_hotplug && !bus && !qdev_get_machine_hotplug_handler(dev)) {

current machine hotplug handler serves both cold and hot-plug so in reality it's just 'plug' handler.

Is there a point -device/device_add devices on board that doesn't have 'hotplug' handler that would wire device up properly?

Do we have such devices?

Quoted 10 lines
qdev-monitor.c +7 -0
+        error_setg(errp, "Device '%s' can not be hotplugged on this machine",
+                   driver);
+        object_unparent(OBJECT(dev));
+        object_unref(OBJECT(dev));
+        return NULL;
+    }
+
     dev->opts = opts;
     object_property_set_bool(OBJECT(dev), true, "realized", &err);
     if (err != NULL) {
#7 lore raw
Quoted 90 lines · Igor Mammedov
Quoted 87 lines · On Tue, 3 Oct 2017 18:46:02 +0200 Thomas Huth <[email protected]> wrote:

The qdev_unplug() function contains a g_assert(hotplug_ctrl) statement, so QEMU crashes when the user tries to device_add + device_del a device that does not have a corresponding hotplug controller. This could be provoked for a couple of devices in the past (see commit 4c93950659487c7ad or 84ebd3e8c7d4fe955 for example). So devices clearly need a hotplug controller when they are suitable for device_add.
The code in qdev_device_add() already checks whether the bus has a proper hotplug controller, but for devices that do not have a corresponding bus, there is no appropriate check available. In that case we should check whether the machine itself provides a suitable hotplug controller and refuse to plug the device if none is available.

---
This is the follow-up patch from my earlier try "hw/core/qdev: Do not allow hot-plugging without hotplug controller" ... AFAICS the function qdev_device_add() is now the right spot to do the check.

 hw/core/qdev.c         | 28 ++++++++++++++++++++--------
 include/hw/qdev-core.h |  1 +
 qdev-monitor.c         |  9 +++++++++
 3 files changed, 30 insertions(+), 8 deletions(-)
hw/core/qdev.c +20 -8
index 606ab53..a953ec9 100644
@@ -253,19 +253,31 @@ void qdev_set_legacy_instance_id(DeviceState *dev, int alias_id,
     dev->alias_required_for_version = required_for_version;
 }
 
+HotplugHandler *qdev_get_machine_hotplug_handler(DeviceState *dev)
+{
+    MachineState *machine;
+    MachineClass *mc;
+    Object *m_obj = qdev_get_machine();
+
+    if (object_dynamic_cast(m_obj, TYPE_MACHINE)) {
+        machine = MACHINE(m_obj);
+        mc = MACHINE_GET_CLASS(machine);
+        if (mc->get_hotplug_handler) {
+            return mc->get_hotplug_handler(machine, dev);
+        }
+    }
+
+    return NULL;
+}
+
 HotplugHandler *qdev_get_hotplug_handler(DeviceState *dev)
 {
-    HotplugHandler *hotplug_ctrl = NULL;
+    HotplugHandler *hotplug_ctrl;
 
     if (dev->parent_bus && dev->parent_bus->hotplug_handler) {
         hotplug_ctrl = dev->parent_bus->hotplug_handler;
-    } else if (object_dynamic_cast(qdev_get_machine(), TYPE_MACHINE)) {
-        MachineState *machine = MACHINE(qdev_get_machine());
-        MachineClass *mc = MACHINE_GET_CLASS(machine);
-
-        if (mc->get_hotplug_handler) {
-            hotplug_ctrl = mc->get_hotplug_handler(machine, dev);
-        }
+    } else {
+        hotplug_ctrl = qdev_get_machine_hotplug_handler(dev);
     }
     return hotplug_ctrl;
 }
include/hw/qdev-core.h +1 -0
index 0891461..5aa536d 100644
@@ -285,6 +285,7 @@ DeviceState *qdev_try_create(BusState *bus, const char *name);
 void qdev_init_nofail(DeviceState *dev);
 void qdev_set_legacy_instance_id(DeviceState *dev, int alias_id,
                                  int required_for_version);
+HotplugHandler *qdev_get_machine_hotplug_handler(DeviceState *dev);
 HotplugHandler *qdev_get_hotplug_handler(DeviceState *dev);
 void qdev_unplug(DeviceState *dev, Error **errp);
 void qdev_simple_device_unplug_cb(HotplugHandler *hotplug_dev,
qdev-monitor.c +0 -0
index 8fd6df9..2891dde 100644
@@ -626,6 +626,15 @@ DeviceState *qdev_device_add(QemuOpts *opts, Error **errp)
         return NULL;
     }
 
Quoted 7 lines

qdev-monitor.c DeviceState *qdev_device_add(QemuOpts *opts, Error **errp) · line 630

Quoted 2 lines
qdev-monitor.c +2 -0
+    /* In case we don't have a bus, there must be a machine hotplug handler */
+    if (qdev_hotplug && !bus && !qdev_get_machine_hotplug_handler(dev)) {

current machine hotplug handler serves both cold and hot-plug so in reality it's just 'plug' handler.

Is there a point -device/device_add devices on board that doesn't have 'hotplug' handler that would wire device up properly?

Sorry, I did not get that question ... do you mean whether there's any code that uses qdev_device_add() to add a device without hotplug controller? I don't think so. It's currently only used by qmp_device_add() for the QMP/HMP command, in vl.c for -device and in the USB code for xen-usb host device. So this function currently really only makes sense for devices that have a hotplug controller.

Thomas

#8 lore raw
Quoted 99 lines · Thomas Huth
Quoted 90 lines · Igor Mammedov
Quoted 87 lines · On Tue, 3 Oct 2017 18:46:02 +0200 Thomas Huth <[email protected]> wrote:

The qdev_unplug() function contains a g_assert(hotplug_ctrl) statement, so QEMU crashes when the user tries to device_add + device_del a device that does not have a corresponding hotplug controller. This could be provoked for a couple of devices in the past (see commit 4c93950659487c7ad or 84ebd3e8c7d4fe955 for example). So devices clearly need a hotplug controller when they are suitable for device_add.
The code in qdev_device_add() already checks whether the bus has a proper hotplug controller, but for devices that do not have a corresponding bus, there is no appropriate check available. In that case we should check whether the machine itself provides a suitable hotplug controller and refuse to plug the device if none is available.

---
This is the follow-up patch from my earlier try "hw/core/qdev: Do not allow hot-plugging without hotplug controller" ... AFAICS the function qdev_device_add() is now the right spot to do the check.

 hw/core/qdev.c         | 28 ++++++++++++++++++++--------
 include/hw/qdev-core.h |  1 +
 qdev-monitor.c         |  9 +++++++++
 3 files changed, 30 insertions(+), 8 deletions(-)
hw/core/qdev.c +20 -8
index 606ab53..a953ec9 100644
@@ -253,19 +253,31 @@ void qdev_set_legacy_instance_id(DeviceState *dev, int alias_id,
     dev->alias_required_for_version = required_for_version;
 }
 
+HotplugHandler *qdev_get_machine_hotplug_handler(DeviceState *dev)
+{
+    MachineState *machine;
+    MachineClass *mc;
+    Object *m_obj = qdev_get_machine();
+
+    if (object_dynamic_cast(m_obj, TYPE_MACHINE)) {
+        machine = MACHINE(m_obj);
+        mc = MACHINE_GET_CLASS(machine);
+        if (mc->get_hotplug_handler) {
+            return mc->get_hotplug_handler(machine, dev);
+        }
+    }
+
+    return NULL;
+}
+
 HotplugHandler *qdev_get_hotplug_handler(DeviceState *dev)
 {
-    HotplugHandler *hotplug_ctrl = NULL;
+    HotplugHandler *hotplug_ctrl;
 
     if (dev->parent_bus && dev->parent_bus->hotplug_handler) {
         hotplug_ctrl = dev->parent_bus->hotplug_handler;
-    } else if (object_dynamic_cast(qdev_get_machine(), TYPE_MACHINE)) {
-        MachineState *machine = MACHINE(qdev_get_machine());
-        MachineClass *mc = MACHINE_GET_CLASS(machine);
-
-        if (mc->get_hotplug_handler) {
-            hotplug_ctrl = mc->get_hotplug_handler(machine, dev);
-        }
+    } else {
+        hotplug_ctrl = qdev_get_machine_hotplug_handler(dev);
     }
     return hotplug_ctrl;
 }
include/hw/qdev-core.h +1 -0
index 0891461..5aa536d 100644
@@ -285,6 +285,7 @@ DeviceState *qdev_try_create(BusState *bus, const char *name);
 void qdev_init_nofail(DeviceState *dev);
 void qdev_set_legacy_instance_id(DeviceState *dev, int alias_id,
                                  int required_for_version);
+HotplugHandler *qdev_get_machine_hotplug_handler(DeviceState *dev);
 HotplugHandler *qdev_get_hotplug_handler(DeviceState *dev);
 void qdev_unplug(DeviceState *dev, Error **errp);
 void qdev_simple_device_unplug_cb(HotplugHandler *hotplug_dev,
qdev-monitor.c +0 -0
index 8fd6df9..2891dde 100644
@@ -626,6 +626,15 @@ DeviceState *qdev_device_add(QemuOpts *opts, Error **errp)
         return NULL;
     }
 
Quoted 7 lines

qdev-monitor.c DeviceState *qdev_device_add(QemuOpts *opts, Error **errp) · line 630

Quoted 2 lines
qdev-monitor.c +2 -0
+    /* In case we don't have a bus, there must be a machine hotplug handler */
+    if (qdev_hotplug && !bus && !qdev_get_machine_hotplug_handler(dev)) {

current machine hotplug handler serves both cold and hot-plug so in reality it's just 'plug' handler.

Is there a point -device/device_add devices on board that doesn't have 'hotplug' handler that would wire device up properly?

Quoted 6 lines

Sorry, I did not get that question ... do you mean whether there's any code that uses qdev_device_add() to add a device without hotplug controller? I don't think so. It's currently only used by qmp_device_add() for the QMP/HMP command, in vl.c for -device and in the USB code for xen-usb host device. So this function currently really only makes sense for devices that have a hotplug controller.

I assume you are talking only about hotpluggable devices. With -device, qdev_device_add() is also used for devices that don't have a hotplug controller, but this is supposed to be true only for non-hotpluggable devices.

Signature · 1 line
Eduardo
#9 lore raw
Quoted 107 lines · Eduardo Habkost
Quoted 99 lines · Thomas Huth
Quoted 90 lines · Igor Mammedov
Quoted 87 lines · On Tue, 3 Oct 2017 18:46:02 +0200 Thomas Huth <[email protected]> wrote:

The qdev_unplug() function contains a g_assert(hotplug_ctrl) statement, so QEMU crashes when the user tries to device_add + device_del a device that does not have a corresponding hotplug controller. This could be provoked for a couple of devices in the past (see commit 4c93950659487c7ad or 84ebd3e8c7d4fe955 for example). So devices clearly need a hotplug controller when they are suitable for device_add.
The code in qdev_device_add() already checks whether the bus has a proper hotplug controller, but for devices that do not have a corresponding bus, there is no appropriate check available. In that case we should check whether the machine itself provides a suitable hotplug controller and refuse to plug the device if none is available.

---
This is the follow-up patch from my earlier try "hw/core/qdev: Do not allow hot-plugging without hotplug controller" ... AFAICS the function qdev_device_add() is now the right spot to do the check.

 hw/core/qdev.c         | 28 ++++++++++++++++++++--------
 include/hw/qdev-core.h |  1 +
 qdev-monitor.c         |  9 +++++++++
 3 files changed, 30 insertions(+), 8 deletions(-)
hw/core/qdev.c +20 -8
index 606ab53..a953ec9 100644
@@ -253,19 +253,31 @@ void qdev_set_legacy_instance_id(DeviceState *dev, int alias_id,
     dev->alias_required_for_version = required_for_version;
 }
 
+HotplugHandler *qdev_get_machine_hotplug_handler(DeviceState *dev)
+{
+    MachineState *machine;
+    MachineClass *mc;
+    Object *m_obj = qdev_get_machine();
+
+    if (object_dynamic_cast(m_obj, TYPE_MACHINE)) {
+        machine = MACHINE(m_obj);
+        mc = MACHINE_GET_CLASS(machine);
+        if (mc->get_hotplug_handler) {
+            return mc->get_hotplug_handler(machine, dev);
+        }
+    }
+
+    return NULL;
+}
+
 HotplugHandler *qdev_get_hotplug_handler(DeviceState *dev)
 {
-    HotplugHandler *hotplug_ctrl = NULL;
+    HotplugHandler *hotplug_ctrl;
 
     if (dev->parent_bus && dev->parent_bus->hotplug_handler) {
         hotplug_ctrl = dev->parent_bus->hotplug_handler;
-    } else if (object_dynamic_cast(qdev_get_machine(), TYPE_MACHINE)) {
-        MachineState *machine = MACHINE(qdev_get_machine());
-        MachineClass *mc = MACHINE_GET_CLASS(machine);
-
-        if (mc->get_hotplug_handler) {
-            hotplug_ctrl = mc->get_hotplug_handler(machine, dev);
-        }
+    } else {
+        hotplug_ctrl = qdev_get_machine_hotplug_handler(dev);
     }
     return hotplug_ctrl;
 }
include/hw/qdev-core.h +1 -0
index 0891461..5aa536d 100644
@@ -285,6 +285,7 @@ DeviceState *qdev_try_create(BusState *bus, const char *name);
 void qdev_init_nofail(DeviceState *dev);
 void qdev_set_legacy_instance_id(DeviceState *dev, int alias_id,
                                  int required_for_version);
+HotplugHandler *qdev_get_machine_hotplug_handler(DeviceState *dev);
 HotplugHandler *qdev_get_hotplug_handler(DeviceState *dev);
 void qdev_unplug(DeviceState *dev, Error **errp);
 void qdev_simple_device_unplug_cb(HotplugHandler *hotplug_dev,
qdev-monitor.c +0 -0
index 8fd6df9..2891dde 100644
@@ -626,6 +626,15 @@ DeviceState *qdev_device_add(QemuOpts *opts, Error **errp)
         return NULL;
     }
 
Quoted 7 lines

qdev-monitor.c DeviceState *qdev_device_add(QemuOpts *opts, Error **errp) · line 630

Quoted 2 lines
qdev-monitor.c +2 -0
+    /* In case we don't have a bus, there must be a machine hotplug handler */
+    if (qdev_hotplug && !bus && !qdev_get_machine_hotplug_handler(dev)) {

current machine hotplug handler serves both cold and hot-plug so in reality it's just 'plug' handler.

Is there a point -device/device_add devices on board that doesn't have 'hotplug' handler that would wire device up properly?

Quoted 6 lines

Sorry, I did not get that question ... do you mean whether there's any code that uses qdev_device_add() to add a device without hotplug controller? I don't think so. It's currently only used by qmp_device_add() for the QMP/HMP command, in vl.c for -device and in the USB code for xen-usb host device. So this function currently really only makes sense for devices that have a hotplug controller.

Quoted 4 lines

I assume you are talking only about hotpluggable devices. With -device, qdev_device_add() is also used for devices that don't have a hotplug controller, but this is supposed to be true only for non-hotpluggable devices.

Right, the cold-pluggable devices should be fine because of the "qdev_hotplug" check, forgot to mention that, sorry.

Thomas

#2 lore raw
In reply to Eduardo Habkost
Quoted 98 lines · On Tue, 3 Oct 2017 15:49:46 -0300 Eduardo Habkost <[email protected]> wrote:
Quoted 84 lines · Thomas Huth

The qdev_unplug() function contains a g_assert(hotplug_ctrl) statement, so QEMU crashes when the user tries to device_add + device_del a device that does not have a corresponding hotplug controller. This could be provoked for a couple of devices in the past (see commit 4c93950659487c7ad or 84ebd3e8c7d4fe955 for example). So devices clearly need a hotplug controller when they are suitable for device_add.
The code in qdev_device_add() already checks whether the bus has a proper hotplug controller, but for devices that do not have a corresponding bus, there is no appropriate check available. In that case we should check whether the machine itself provides a suitable hotplug controller and refuse to plug the device if none is available.

---
This is the follow-up patch from my earlier try "hw/core/qdev: Do not allow hot-plugging without hotplug controller" ... AFAICS the function qdev_device_add() is now the right spot to do the check.

 hw/core/qdev.c         | 28 ++++++++++++++++++++--------
 include/hw/qdev-core.h |  1 +
 qdev-monitor.c         |  9 +++++++++
 3 files changed, 30 insertions(+), 8 deletions(-)
hw/core/qdev.c +20 -8
index 606ab53..a953ec9 100644
@@ -253,19 +253,31 @@ void qdev_set_legacy_instance_id(DeviceState *dev, int alias_id,
     dev->alias_required_for_version = required_for_version;
 }
 
+HotplugHandler *qdev_get_machine_hotplug_handler(DeviceState *dev)
+{
+    MachineState *machine;
+    MachineClass *mc;
+    Object *m_obj = qdev_get_machine();
+
+    if (object_dynamic_cast(m_obj, TYPE_MACHINE)) {
+        machine = MACHINE(m_obj);
+        mc = MACHINE_GET_CLASS(machine);
+        if (mc->get_hotplug_handler) {
+            return mc->get_hotplug_handler(machine, dev);
+        }
+    }
+
+    return NULL;
+}
+
 HotplugHandler *qdev_get_hotplug_handler(DeviceState *dev)
 {
-    HotplugHandler *hotplug_ctrl = NULL;
+    HotplugHandler *hotplug_ctrl;
 
     if (dev->parent_bus && dev->parent_bus->hotplug_handler) {
         hotplug_ctrl = dev->parent_bus->hotplug_handler;
-    } else if (object_dynamic_cast(qdev_get_machine(), TYPE_MACHINE)) {
-        MachineState *machine = MACHINE(qdev_get_machine());
-        MachineClass *mc = MACHINE_GET_CLASS(machine);
-
-        if (mc->get_hotplug_handler) {
-            hotplug_ctrl = mc->get_hotplug_handler(machine, dev);
-        }
+    } else {
+        hotplug_ctrl = qdev_get_machine_hotplug_handler(dev);
     }
     return hotplug_ctrl;
 }
include/hw/qdev-core.h +1 -0
index 0891461..5aa536d 100644
@@ -285,6 +285,7 @@ DeviceState *qdev_try_create(BusState *bus, const char *name);
 void qdev_init_nofail(DeviceState *dev);
 void qdev_set_legacy_instance_id(DeviceState *dev, int alias_id,
                                  int required_for_version);
+HotplugHandler *qdev_get_machine_hotplug_handler(DeviceState *dev);
 HotplugHandler *qdev_get_hotplug_handler(DeviceState *dev);
 void qdev_unplug(DeviceState *dev, Error **errp);
 void qdev_simple_device_unplug_cb(HotplugHandler *hotplug_dev,
qdev-monitor.c +0 -0
index 8fd6df9..2891dde 100644

qdev-monitor.c DeviceState *qdev_device_add(QemuOpts *opts, Error **errp) · line 633

Quoted 8 lines
qdev-monitor.c +5 -0
@@ -626,6 +626,15 @@ DeviceState *qdev_device_add(QemuOpts *opts, Error **errp)
         return NULL;
     }
 
+    /* In case we don't have a bus, there must be a machine hotplug handler */
+    if (qdev_hotplug && !bus && !qdev_get_machine_hotplug_handler(dev)) {
+        error_setg(errp, "Device '%s' can not be hotplugged on this machine",
+                   driver);
+        object_unparent(OBJECT(dev));  

Isn't it better to check qdev_get_machine_hotplug_handler() earlier (before the qdev_set_parent_bus() and qdev_set_id() lines), so object_unparent() isn't necessary?

Quoted 3 lines

(We probably don't need to call object_unparent() here, already, because bus is NULL. But moving the check before the "if (bus) qdev_set_parent_bus()" statement would make this more obvious).

it might be bus or bus-less device, so making check before qdev_set_parent_bus() should be simpler.

Quoted 5 lines

I would prefer to eventually make MachineClass::get_hotplug_handler() get a typename or DeviceClass* argument instead of DeviceState*, so we don't even create the device object. But I don't think it's a requirement for this bug fix.

choice of hotplug handler might theoretically depend on plugged device instance (over-engineered? as far as I recall none does it so far)

Quoted 5 lines

qdev-monitor.c DeviceState *qdev_device_add(QemuOpts *opts, Error **errp) · line 636

Quoted 5 lines
qdev-monitor.c +3 -0
+        object_unref(OBJECT(dev));
+        return NULL;
+    }

wrt error exit path, I'd rework error path in qdev_device_add() in separate patch first to look like it is in device_set_realized() and then just jump to appropriate label from here.

Quoted 8 lines
Quoted 7 lines
qdev-monitor.c +1 -0
+
     dev->opts = opts;
     object_property_set_bool(OBJECT(dev), true, "realized", &err);
     if (err != NULL) {
Signature · 1 line
1.8.3.1
#3 lore raw
Quoted 101 lines · Igor Mammedov
Quoted 98 lines · On Tue, 3 Oct 2017 15:49:46 -0300 Eduardo Habkost <[email protected]> wrote:
Quoted 84 lines · Thomas Huth

The qdev_unplug() function contains a g_assert(hotplug_ctrl) statement, so QEMU crashes when the user tries to device_add + device_del a device that does not have a corresponding hotplug controller. This could be provoked for a couple of devices in the past (see commit 4c93950659487c7ad or 84ebd3e8c7d4fe955 for example). So devices clearly need a hotplug controller when they are suitable for device_add.
The code in qdev_device_add() already checks whether the bus has a proper hotplug controller, but for devices that do not have a corresponding bus, there is no appropriate check available. In that case we should check whether the machine itself provides a suitable hotplug controller and refuse to plug the device if none is available.

---
This is the follow-up patch from my earlier try "hw/core/qdev: Do not allow hot-plugging without hotplug controller" ... AFAICS the function qdev_device_add() is now the right spot to do the check.

 hw/core/qdev.c         | 28 ++++++++++++++++++++--------
 include/hw/qdev-core.h |  1 +
 qdev-monitor.c         |  9 +++++++++
 3 files changed, 30 insertions(+), 8 deletions(-)
hw/core/qdev.c +20 -8
index 606ab53..a953ec9 100644
@@ -253,19 +253,31 @@ void qdev_set_legacy_instance_id(DeviceState *dev, int alias_id,
     dev->alias_required_for_version = required_for_version;
 }
 
+HotplugHandler *qdev_get_machine_hotplug_handler(DeviceState *dev)
+{
+    MachineState *machine;
+    MachineClass *mc;
+    Object *m_obj = qdev_get_machine();
+
+    if (object_dynamic_cast(m_obj, TYPE_MACHINE)) {
+        machine = MACHINE(m_obj);
+        mc = MACHINE_GET_CLASS(machine);
+        if (mc->get_hotplug_handler) {
+            return mc->get_hotplug_handler(machine, dev);
+        }
+    }
+
+    return NULL;
+}
+
 HotplugHandler *qdev_get_hotplug_handler(DeviceState *dev)
 {
-    HotplugHandler *hotplug_ctrl = NULL;
+    HotplugHandler *hotplug_ctrl;
 
     if (dev->parent_bus && dev->parent_bus->hotplug_handler) {
         hotplug_ctrl = dev->parent_bus->hotplug_handler;
-    } else if (object_dynamic_cast(qdev_get_machine(), TYPE_MACHINE)) {
-        MachineState *machine = MACHINE(qdev_get_machine());
-        MachineClass *mc = MACHINE_GET_CLASS(machine);
-
-        if (mc->get_hotplug_handler) {
-            hotplug_ctrl = mc->get_hotplug_handler(machine, dev);
-        }
+    } else {
+        hotplug_ctrl = qdev_get_machine_hotplug_handler(dev);
     }
     return hotplug_ctrl;
 }
include/hw/qdev-core.h +1 -0
index 0891461..5aa536d 100644
@@ -285,6 +285,7 @@ DeviceState *qdev_try_create(BusState *bus, const char *name);
 void qdev_init_nofail(DeviceState *dev);
 void qdev_set_legacy_instance_id(DeviceState *dev, int alias_id,
                                  int required_for_version);
+HotplugHandler *qdev_get_machine_hotplug_handler(DeviceState *dev);
 HotplugHandler *qdev_get_hotplug_handler(DeviceState *dev);
 void qdev_unplug(DeviceState *dev, Error **errp);
 void qdev_simple_device_unplug_cb(HotplugHandler *hotplug_dev,
qdev-monitor.c +0 -0
index 8fd6df9..2891dde 100644

qdev-monitor.c DeviceState *qdev_device_add(QemuOpts *opts, Error **errp) · line 633

Quoted 8 lines
qdev-monitor.c +5 -0
@@ -626,6 +626,15 @@ DeviceState *qdev_device_add(QemuOpts *opts, Error **errp)
         return NULL;
     }
 
+    /* In case we don't have a bus, there must be a machine hotplug handler */
+    if (qdev_hotplug && !bus && !qdev_get_machine_hotplug_handler(dev)) {
+        error_setg(errp, "Device '%s' can not be hotplugged on this machine",
+                   driver);
+        object_unparent(OBJECT(dev));  

Isn't it better to check qdev_get_machine_hotplug_handler() earlier (before the qdev_set_parent_bus() and qdev_set_id() lines), so object_unparent() isn't necessary?

Quoted 5 lines
Quoted 3 lines

(We probably don't need to call object_unparent() here, already, because bus is NULL. But moving the check before the "if (bus) qdev_set_parent_bus()" statement would make this more obvious).

it might be bus or bus-less device, so making check before qdev_set_parent_bus() should be simpler.

The check for devices that have a bus is already done earlier in this function ("if (qdev_hotplug && bus && !qbus_is_hotpluggable(bus))") ... but yes, I'll move the new check for bus-less devices a little bit earlier so that the unparent is not necessary anymore.

Quoted 7 lines
Quoted 5 lines

I would prefer to eventually make MachineClass::get_hotplug_handler() get a typename or DeviceClass* argument instead of DeviceState*, so we don't even create the device object. But I don't think it's a requirement for this bug fix.

choice of hotplug handler might theoretically depend on plugged device instance (over-engineered? as far as I recall none does it so far)

Yes, currently we might be able to do it with the typename only ... but that seems to be a rather big rework right now, and we might indeed need a real device instance later again, so I'd prefer to rather not do that rework right now.

Quoted 6 lines
Quoted 3 lines

qdev-monitor.c DeviceState *qdev_device_add(QemuOpts *opts, Error **errp) · line 636

Quoted 3 lines
qdev-monitor.c +3 -0
+        object_unref(OBJECT(dev));
+        return NULL;
+    }

wrt error exit path, I'd rework error path in qdev_device_add() in separate patch first to look like it is in device_set_realized() and then just jump to appropriate label from here.

Ok, I can have a try whether that looks better.

Thomas

#4 lore raw
Quoted 118 lines · On Thu, 5 Oct 2017 11:00:29 +0200 Thomas Huth <[email protected]> wrote:
Quoted 101 lines · Igor Mammedov
Quoted 98 lines · On Tue, 3 Oct 2017 15:49:46 -0300 Eduardo Habkost <[email protected]> wrote:
Quoted 84 lines · Thomas Huth

The qdev_unplug() function contains a g_assert(hotplug_ctrl) statement, so QEMU crashes when the user tries to device_add + device_del a device that does not have a corresponding hotplug controller. This could be provoked for a couple of devices in the past (see commit 4c93950659487c7ad or 84ebd3e8c7d4fe955 for example). So devices clearly need a hotplug controller when they are suitable for device_add.
The code in qdev_device_add() already checks whether the bus has a proper hotplug controller, but for devices that do not have a corresponding bus, there is no appropriate check available. In that case we should check whether the machine itself provides a suitable hotplug controller and refuse to plug the device if none is available.

---
This is the follow-up patch from my earlier try "hw/core/qdev: Do not allow hot-plugging without hotplug controller" ... AFAICS the function qdev_device_add() is now the right spot to do the check.

 hw/core/qdev.c         | 28 ++++++++++++++++++++--------
 include/hw/qdev-core.h |  1 +
 qdev-monitor.c         |  9 +++++++++
 3 files changed, 30 insertions(+), 8 deletions(-)
hw/core/qdev.c +20 -8
index 606ab53..a953ec9 100644
@@ -253,19 +253,31 @@ void qdev_set_legacy_instance_id(DeviceState *dev, int alias_id,
     dev->alias_required_for_version = required_for_version;
 }
 
+HotplugHandler *qdev_get_machine_hotplug_handler(DeviceState *dev)
+{
+    MachineState *machine;
+    MachineClass *mc;
+    Object *m_obj = qdev_get_machine();
+
+    if (object_dynamic_cast(m_obj, TYPE_MACHINE)) {
+        machine = MACHINE(m_obj);
+        mc = MACHINE_GET_CLASS(machine);
+        if (mc->get_hotplug_handler) {
+            return mc->get_hotplug_handler(machine, dev);
+        }
+    }
+
+    return NULL;
+}
+
 HotplugHandler *qdev_get_hotplug_handler(DeviceState *dev)
 {
-    HotplugHandler *hotplug_ctrl = NULL;
+    HotplugHandler *hotplug_ctrl;
 
     if (dev->parent_bus && dev->parent_bus->hotplug_handler) {
         hotplug_ctrl = dev->parent_bus->hotplug_handler;
-    } else if (object_dynamic_cast(qdev_get_machine(), TYPE_MACHINE)) {
-        MachineState *machine = MACHINE(qdev_get_machine());
-        MachineClass *mc = MACHINE_GET_CLASS(machine);
-
-        if (mc->get_hotplug_handler) {
-            hotplug_ctrl = mc->get_hotplug_handler(machine, dev);
-        }
+    } else {
+        hotplug_ctrl = qdev_get_machine_hotplug_handler(dev);
     }
     return hotplug_ctrl;
 }
include/hw/qdev-core.h +1 -0
index 0891461..5aa536d 100644
@@ -285,6 +285,7 @@ DeviceState *qdev_try_create(BusState *bus, const char *name);
 void qdev_init_nofail(DeviceState *dev);
 void qdev_set_legacy_instance_id(DeviceState *dev, int alias_id,
                                  int required_for_version);
+HotplugHandler *qdev_get_machine_hotplug_handler(DeviceState *dev);
 HotplugHandler *qdev_get_hotplug_handler(DeviceState *dev);
 void qdev_unplug(DeviceState *dev, Error **errp);
 void qdev_simple_device_unplug_cb(HotplugHandler *hotplug_dev,
qdev-monitor.c +0 -0
index 8fd6df9..2891dde 100644

qdev-monitor.c DeviceState *qdev_device_add(QemuOpts *opts, Error **errp) · line 633

Quoted 8 lines
qdev-monitor.c +5 -0
@@ -626,6 +626,15 @@ DeviceState *qdev_device_add(QemuOpts *opts, Error **errp)
         return NULL;
     }
 
+    /* In case we don't have a bus, there must be a machine hotplug handler */
+    if (qdev_hotplug && !bus && !qdev_get_machine_hotplug_handler(dev)) {
+        error_setg(errp, "Device '%s' can not be hotplugged on this machine",
+                   driver);
+        object_unparent(OBJECT(dev));    

Isn't it better to check qdev_get_machine_hotplug_handler() earlier (before the qdev_set_parent_bus() and qdev_set_id() lines), so object_unparent() isn't necessary?

Quoted 5 lines
Quoted 3 lines

(We probably don't need to call object_unparent() here, already, because bus is NULL. But moving the check before the "if (bus) qdev_set_parent_bus()" statement would make this more obvious).

it might be bus or bus-less device, so making check before qdev_set_parent_bus() should be simpler.

The check for devices that have a bus is already done earlier in this function ("if (qdev_hotplug && bus && !qbus_is_hotpluggable(bus))") ... but yes, I'll move the new check for bus-less devices a little bit earlier so that the unparent is not necessary anymore.

Quoted 5 lines
Quoted 5 lines

I would prefer to eventually make MachineClass::get_hotplug_handler() get a typename or DeviceClass* argument instead of DeviceState*, so we don't even create the device object. But I don't think it's a requirement for this bug fix.

Quoted 7 lines
Quoted 2 lines

choice of hotplug handler might theoretically depend on plugged device instance (over-engineered? as far as I recall none does it so far)

Yes, currently we might be able to do it with the typename only ... but that seems to be a rather big rework right now, and we might indeed need a real device instance later again, so I'd prefer to rather not do that rework right now.

it was just remark why it uses 'dev' and a call for an action.

Quoted 9 lines
Quoted 7 lines
Quoted 4 lines

qdev-monitor.c DeviceState *qdev_device_add(QemuOpts *opts, Error **errp) · line 636

Quoted 4 lines
qdev-monitor.c +3 -0
+        object_unref(OBJECT(dev));
+        return NULL;
+    }  

wrt error exit path, I'd rework error path in qdev_device_add() in separate patch first to look like it is in device_set_realized() and then just jump to appropriate label from here.

Ok, I can have a try whether that looks better.

the function already has couple error exit point that duplicate cleanup steps, so it probably would read better with cleanup

Quoted 3 lines

Thomas

#5 lore raw
Quoted 2 lines · On Thu, 5 Oct 2017 11:12:41 +0200 Igor Mammedov <[email protected]> wrote:

On Thu, 5 Oct 2017 11:00:29 +0200
Thomas Huth <[email protected]> wrote:

[...]

Quoted 5 lines
Quoted 4 lines

Yes, currently we might be able to do it with the typename only ... but that seems to be a rather big rework right now, and we might indeed need a real device instance later again, so I'd prefer to rather not do that rework right now.

it was just remark why it uses 'dev' and a call for an action.

forgot the most important word

s/a call/NOT a call/

[...]