hw/core: Unify hotplug decision logic
PATCH v4 1/1 hw/core: Unify hotplug decision logic
Commit 03fcbd9dc508 ("qdev: Check for the availability of a hotplug controller before adding a device") says:
Quoted 10 lines
The
qdev_unplug()function contains ag_assert(hotplug_ctrl)statement, so QEMU crashes when the user tries todevice_add+device_dela device that does not have a corresponding hotplug controller.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, here is no appropriate check available yet. 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.
However, it forgot to add the corresponding check to qdev_unplug().
Most checks are comon between the hot-plug and hot-unplug scenarios so extract them and share the implementation, saving some code and fixing the aforementioned bug.
---
Changes in v4:
- Rebased.
- Link to v3: https://lore.kernel.org/r/[email protected]
Changes in v3:
- Extracted checks common for hot-plug and hot-unplug into a function.
- Link to v2: https://lore.kernel.org/r/[email protected]
Changes in v2:
- Fixed indention.
- Link to v1: https://lore.kernel.org/r/[email protected]
--- include/hw/qdev-core.h | 3 ++- hw/core/qdev-hotplug.c | 42 +++++++++++++++++++++++++++++++++++++++++- system/qdev-monitor.c | 37 +++++-------------------------------- 3 files changed, 48 insertions(+), 34 deletions(-)
include/hw/qdev-core.h +2 -1
@@ -544,7 +544,8 @@ void qdev_set_legacy_instance_id(DeviceState *dev, int alias_id, int required_for_version);HotplugHandler *qdev_get_bus_hotplug_handler (DeviceState *dev);HotplugHandler *qdev_get_machine_hotplug_handler (DeviceState *dev); -bool qdev_hotplug_allowed (DeviceState *dev,Error **errp); +bool qdev_hotplug_allowed (DeviceState *dev,BusState *bus,Error **errp); +bool qdev_hotunplug_allowed (DeviceState *dev,Error **errp);/** * qdev_get_hotplug_handler() - Get handler responsible for device wiring
hw/core/qdev-hotplug.c +41 -1
65 lines not on this page · raw message
system/qdev-monitor.c +5 -32
@@ -263,8 +263,7 @@ static DeviceClass *qdev_get_device_class(const char **driver, Error **errp) } dc =DEVICE_CLASS (oc); -if (!dc->user_creatable || - (phase_check (PHASE_MACHINE_READY ) && !dc->hotpluggable)) { +if (!dc->user_creatable) {error_setg (errp,QERR_INVALID_PARAMETER_VALUE ,"driver" ,"a pluggable device type" );return NULL ;
@@ -675,11 +674,6 @@ DeviceState *qdev_device_add_from_qdict(const QDict *opts,return NULL ; } -if (phase_check (PHASE_MACHINE_READY ) && bus && !qbus_is_hotpluggable (bus)) { -error_setg (errp,"Bus '%s' does not support hotplugging" , bus->name); -return NULL ; - } -if (migration_is_running ()) {error_setg (errp,"device_add not allowed while migrating" );return NULL ;
@@ -689,17 +683,9 @@ DeviceState *qdev_device_add_from_qdict(const QDict *opts, dev =qdev_new (driver);/* Check whether the hotplug is allowed by the machine */ -if (phase_check (PHASE_MACHINE_READY )) { -if (!qdev_hotplug_allowed (dev, errp)) { - goto err_del_dev; - } - -if (!bus && !qdev_get_machine_hotplug_handler (dev)) { -/* No bus, no machine hotplug handler --> device is not hotpluggable */ -error_setg (errp,"Device '%s' can not be hotplugged on this machine" , - driver); - goto err_del_dev; - } +if (phase_check (PHASE_MACHINE_READY ) && + !qdev_hotplug_allowed (dev, bus, errp)) { + goto err_del_dev; }/*
@@ -902,24 +888,11 @@ static DeviceState *find_device_state(const char *id, bool use_generic_error,void qdev_unplug (DeviceState *dev,Error **errp) { -DeviceClass *dc =DEVICE_GET_CLASS (dev);HotplugHandler *hotplug_ctrl;HotplugHandlerClass *hdc;Error *local_err =NULL ; -if (qdev_unplug_blocked (dev, errp)) { -return ; - } - -if (dev->parent_bus && !qbus_is_hotpluggable (dev->parent_bus)) { -error_setg (errp,"Bus '%s' does not support hotplugging" , - dev->parent_bus->name); -return ; - } - -if (!dc->hotpluggable) { -error_setg (errp,"Device '%s' does not support hotplugging" , -object_get_typename (OBJECT (dev))); +if (!qdev_hotunplug_allowed (dev, errp)) {return ; }
---
base-commit: 38d0939b86e2eef6f6a622c6f1f7befda0146595
change-id: 20231202-bus-75a454c5d959
Best regards,
Signature · 1 line
Akihiko Odaki <[email protected]>