hw/core: Unify hotplug decision logic
v5 cover hw/core: Unify hotplug decision logic
Akihiko's v4 patch was doing too many things at once to my taste, so I split it to follow dumbly each steps.
https://lore.kernel.org/qemu-devel/[email protected]/
Akihiko Odaki (6):
hw/qdev: Pass bus argument to qdev_hotplug_allowed() hw/qdev: Factor qdev_hotunplug_allowed() out
hw/qdev: Introduce qdev_hotplug_unplug_allowed_common() hw/qdev: Check DevClass::hotpluggable in hotplug_unplug_allowed_common hw/qdev: Check qbus_is_hotpluggable in hotplug_unplug_allowed_common hw/qdev: Check machine_hotplug_handler in
hotplug_unplug_allowed_common
include/hw/qdev-core.h | 3 ++- hw/core/qdev-hotplug.c | 42 +++++++++++++++++++++++++++++++++++++++++- system/qdev-monitor.c | 37 +++++-------------------------------- 3 files changed, 48 insertions(+), 34 deletions(-)
Signature · 1 line
2.47.1
PATCH v5 1/6 hw/qdev: Pass bus argument to qdev_hotplug_allowed()
From: Akihiko Odaki <[email protected]>
In preparation of checking the parent bus is hot(un)pluggable in a few commits, pass a 'bus' argument to qdev_hotplug_allowed().
--- include/hw/qdev-core.h | 2 +- hw/core/qdev-hotplug.c | 2 +- system/qdev-monitor.c | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-)
include/hw/qdev-core.h +1 -1
@@ -544,7 +544,7 @@ 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);/** * qdev_get_hotplug_handler() - Get handler responsible for device wiring
hw/core/qdev-hotplug.c +1 -1
@@ -30,7 +30,7 @@ HotplugHandler *qdev_get_machine_hotplug_handler(DeviceState *dev)return NULL ; } -bool qdev_hotplug_allowed (DeviceState *dev,Error **errp) +bool qdev_hotplug_allowed (DeviceState *dev,BusState *bus,Error **errp) {MachineState *machine;MachineClass *mc;
system/qdev-monitor.c +1 -1
@@ -690,7 +690,7 @@ DeviceState *qdev_device_add_from_qdict(const QDict *opts,/* Check whether the hotplug is allowed by the machine */ if (phase_check (PHASE_MACHINE_READY )) { -if (!qdev_hotplug_allowed (dev, errp)) { +if (!qdev_hotplug_allowed (dev, bus, errp)) { goto err_del_dev; }
Signature · 1 line
2.47.1
PATCH v5 2/6 hw/qdev: Factor qdev_hotunplug_allowed() out
In reply to Philippe Mathieu-DaudéFrom: Akihiko Odaki <[email protected]>
Factor qdev_hotunplug_allowed() out of qdev_unplug(). Start checking the device is not blocked.
--- include/hw/qdev-core.h | 1 + hw/core/qdev-hotplug.c | 5 +++++ system/qdev-monitor.c | 2 +- 3 files changed, 7 insertions(+), 1 deletion(-)
include/hw/qdev-core.h +1 -0
@@ -545,6 +545,7 @@ void qdev_set_legacy_instance_id(DeviceState *dev, int alias_id,HotplugHandler *qdev_get_bus_hotplug_handler (DeviceState *dev);HotplugHandler *qdev_get_machine_hotplug_handler (DeviceState *dev);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 +5 -0
@@ -47,6 +47,11 @@ bool qdev_hotplug_allowed(DeviceState *dev, BusState *bus, Error **errp)return true; } +bool qdev_hotunplug_allowed (DeviceState *dev,Error **errp) +{ +return !qdev_unplug_blocked (dev, errp); +} +HotplugHandler *qdev_get_bus_hotplug_handler (DeviceState *dev) {if (dev->parent_bus) {
system/qdev-monitor.c +1 -1
@@ -907,7 +907,7 @@ void qdev_unplug(DeviceState *dev, Error **errp)HotplugHandlerClass *hdc;Error *local_err =NULL ; -if (qdev_unplug_blocked (dev, errp)) { +if (!qdev_hotunplug_allowed (dev, errp)) {return ; }
Signature · 1 line
2.47.1
PATCH v5 3/6 hw/qdev: Introduce qdev_hotplug_unplug_allowed_common()
In reply to Philippe Mathieu-DaudéFrom: Akihiko Odaki <[email protected]>
Introduce qdev_hotplug_unplug_allowed_common() to hold common code between checking hot-plug/unplug is allowed.
--- hw/core/qdev-hotplug.c | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-)
hw/core/qdev-hotplug.c +12 -1
@@ -30,12 +30,22 @@ HotplugHandler *qdev_get_machine_hotplug_handler(DeviceState *dev)return NULL ; } +static bool qdev_hotplug_unplug_allowed_common (DeviceState *dev,BusState *bus, +Error **errp) +{ +return true; +} +bool qdev_hotplug_allowed (DeviceState *dev,BusState *bus,Error **errp) {MachineState *machine;MachineClass *mc;Object *m_obj =qdev_get_machine (); +if (!qdev_hotplug_unplug_allowed_common (dev, bus, errp)) { +return false; + } +if (object_dynamic_cast (m_obj,TYPE_MACHINE )) { machine =MACHINE (m_obj); mc =MACHINE_GET_CLASS (machine);
@@ -49,7 +59,8 @@ bool qdev_hotplug_allowed(DeviceState *dev, BusState *bus, Error **errp)bool qdev_hotunplug_allowed (DeviceState *dev,Error **errp) { -return !qdev_unplug_blocked (dev, errp); +return !qdev_unplug_blocked (dev, errp) && +qdev_hotplug_unplug_allowed_common (dev, dev->parent_bus, errp); }HotplugHandler *qdev_get_bus_hotplug_handler (DeviceState *dev)
Signature · 1 line
2.47.1
PATCH v5 4/6 hw/qdev: Check DevClass::hotpluggable in hotplug_unplug_allowed_common
In reply to Philippe Mathieu-DaudéFrom: Akihiko Odaki <[email protected]>
Check the same code once in the common helper.
--- hw/core/qdev-hotplug.c | 9 +++++++++ system/qdev-monitor.c | 10 +--------- 2 files changed, 10 insertions(+), 9 deletions(-)
hw/core/qdev-hotplug.c +9 -0
@@ -12,6 +12,7 @@#include "qemu/osdep.h" #include "hw/qdev-core.h" #include "hw/boards.h" +#include "qapi/error.h" HotplugHandler *qdev_get_machine_hotplug_handler (DeviceState *dev) {
@@ -33,6 +34,14 @@ HotplugHandler *qdev_get_machine_hotplug_handler(DeviceState *dev)static bool qdev_hotplug_unplug_allowed_common (DeviceState *dev,BusState *bus,Error **errp) { +DeviceClass *dc =DEVICE_GET_CLASS (dev); + +if (!dc->hotpluggable) { +error_setg (errp,"Device '%s' does not support hotplugging" , +object_get_typename (OBJECT (dev))); +return false; + } +return true; }
system/qdev-monitor.c +1 -9
@@ -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 ;
@@ -902,7 +901,6 @@ 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 ;
@@ -917,12 +915,6 @@ void qdev_unplug(DeviceState *dev, Error **errp)return ; } -if (!dc->hotpluggable) { -error_setg (errp,"Device '%s' does not support hotplugging" , -object_get_typename (OBJECT (dev))); -return ; - } -if (migration_is_running () && !dev->allow_unplug_during_migration) {error_setg (errp,"device_del not allowed while migrating" );return ;
Signature · 1 line
2.47.1
PATCH v5 5/6 hw/qdev: Check qbus_is_hotpluggable in hotplug_unplug_allowed_common
In reply to Philippe Mathieu-DaudéFrom: Akihiko Odaki <[email protected]>
Check the same code once in the common helper.
--- hw/core/qdev-hotplug.c | 8 ++++++++ system/qdev-monitor.c | 11 ----------- 2 files changed, 8 insertions(+), 11 deletions(-)
hw/core/qdev-hotplug.c +8 -0
@@ -42,6 +42,14 @@ static bool qdev_hotplug_unplug_allowed_common(DeviceState *dev, BusState *bus,return false; } +if (bus) { +if (!qbus_is_hotpluggable (bus)) { +error_setg (errp,"Bus '%s' does not support hotplugging" , + bus->name); +return false; + } + } +return true; }
system/qdev-monitor.c +0 -11
@@ -674,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 ;
@@ -909,12 +904,6 @@ void qdev_unplug(DeviceState *dev, Error **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 (migration_is_running () && !dev->allow_unplug_during_migration) {error_setg (errp,"device_del not allowed while migrating" );return ;
Signature · 1 line
2.47.1
PATCH v5 6/6 hw/qdev: Check machine_hotplug_handler in hotplug_unplug_allowed_common
In reply to Philippe Mathieu-DaudéFrom: Akihiko Odaki <[email protected]>
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().
Check the machine hotplug handler once in the common qdev_hotplug_unplug_allowed_common() helper so both hotplug and hot-unplug path are covered.
--- hw/core/qdev-hotplug.c | 7 +++++++ system/qdev-monitor.c | 14 +++----------- 2 files changed, 10 insertions(+), 11 deletions(-)
hw/core/qdev-hotplug.c +7 -0
@@ -48,6 +48,13 @@ static bool qdev_hotplug_unplug_allowed_common(DeviceState *dev, BusState *bus, bus->name);return false; } + }else { +if (!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" , +object_get_typename (OBJECT (dev))); +return false; + } }return true;
system/qdev-monitor.c +3 -11
@@ -683,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, bus, 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; }/*
Signature · 1 line
2.47.1
Quoted 4 lines · Philippe Mathieu-Daudé
Akihiko's v4 patch was doing too many things at once to my taste, so I split it to follow dumbly each steps.
https://lore.kernel.org/qemu-devel/[email protected]/
Quoted 8 lines
Akihiko Odaki (6):
hw/qdev: Pass bus argument toqdev_hotplug_allowed()hw/qdev: Factorqdev_hotunplug_allowed()out
hw/qdev: Introduceqdev_hotplug_unplug_allowed_common()hw/qdev: CheckDevClass::hotpluggableinhotplug_unplug_allowed_commonhw/qdev: Checkqbus_is_hotpluggableinhotplug_unplug_allowed_commonhw/qdev: Checkmachine_hotplug_handlerin
hotplug_unplug_allowed_common
Akihiko, are you OK with this split?
Quoted 3 lines · On Fri, 10 Jan 2025 10:19:02 +0100 Philippe Mathieu-Daudé <[email protected]>…
Akihiko's v4 patch was doing too many things at once to my taste, so I split it to follow dumbly each steps.
https://lore.kernel.org/qemu-devel/[email protected]/
refactoring in some cases would lead to device being created and then deleted, where as before it we were bailing out earlier without creating a device.
I guess there is no much harm in that overhead and we get consolidated handling instead of scattered checks.
Just make checkpatch happy, with that
Quoted 15 lines
Akihiko Odaki (6):
hw/qdev: Pass bus argument toqdev_hotplug_allowed()hw/qdev: Factorqdev_hotunplug_allowed()out
hw/qdev: Introduceqdev_hotplug_unplug_allowed_common()hw/qdev: CheckDevClass::hotpluggableinhotplug_unplug_allowed_commonhw/qdev: Checkqbus_is_hotpluggableinhotplug_unplug_allowed_commonhw/qdev: Checkmachine_hotplug_handlerin
hotplug_unplug_allowed_commoninclude/hw/qdev-core.h | 3 ++- hw/core/qdev-hotplug.c | 42 +++++++++++++++++++++++++++++++++++++++++- system/qdev-monitor.c | 37 +++++-------------------------------- 3 files changed, 48 insertions(+), 34 deletions(-)
Quoted 8 lines · Philippe Mathieu-Daudé
Akihiko Odaki (6):
hw/qdev: Pass bus argument toqdev_hotplug_allowed()hw/qdev: Factorqdev_hotunplug_allowed()out
hw/qdev: Introduceqdev_hotplug_unplug_allowed_common()hw/qdev: CheckDevClass::hotpluggableinhotplug_unplug_allowed_commonhw/qdev: Checkqbus_is_hotpluggableinhotplug_unplug_allowed_commonhw/qdev: Checkmachine_hotplug_handlerin
hotplug_unplug_allowed_common
Series queued.