[PATCH] monitor: Tidy up find_device_state()

Markus Armbruster posted 1 patch 2 years, 7 months ago
Test checkpatch passed
Patches applied successfully (tree, apply log)
git fetch https://github.com/patchew-project/qemu tags/patchew/20210916111707.84999-1-armbru@redhat.com
Maintainers: "Daniel P. Berrangé" <berrange@redhat.com>, Eduardo Habkost <ehabkost@redhat.com>, Paolo Bonzini <pbonzini@redhat.com>
softmmu/qdev-monitor.c | 13 +++++--------
1 file changed, 5 insertions(+), 8 deletions(-)
[PATCH] monitor: Tidy up find_device_state()
Posted by Markus Armbruster 2 years, 7 months ago
Commit 6287d827d4 "monitor: allow device_del to accept QOM paths"
extended find_device_state() to accept QOM paths in addition to qdev
IDs.  This added a checked conversion to TYPE_DEVICE at the end, which
duplicates the check done for the qdev ID case earlier, except it sets
a *different* error: GenericError "ID is not a hotpluggable device"
when passed a QOM path, and DeviceNotFound "Device 'ID' not found"
when passed a qdev ID.  Fortunately, the latter won't happen as long
as we add only devices to /machine/peripheral/.

Earlier, commit b6cc36abb2 "qdev: device_del: Search for to be
unplugged device in 'peripheral' container" rewrote the lookup by qdev
ID to use QOM instead of qdev_find_recursive(), so it can handle
buss-less devices.  It does so by constructing an absolute QOM path.
Works, but object_resolve_path_component() is easier.  Switching to it
also gets rid of the unclean duplication described above.

While there, avoid converting to TYPE_DEVICE twice, first to check
whether it's possible, and then for real.

Signed-off-by: Markus Armbruster <armbru@redhat.com>
---
 softmmu/qdev-monitor.c | 13 +++++--------
 1 file changed, 5 insertions(+), 8 deletions(-)

diff --git a/softmmu/qdev-monitor.c b/softmmu/qdev-monitor.c
index a304754ab9..d1ab3c25fb 100644
--- a/softmmu/qdev-monitor.c
+++ b/softmmu/qdev-monitor.c
@@ -831,16 +831,12 @@ void qmp_device_add(QDict *qdict, QObject **ret_data, Error **errp)
 static DeviceState *find_device_state(const char *id, Error **errp)
 {
     Object *obj;
+    DeviceState *dev;
 
     if (id[0] == '/') {
         obj = object_resolve_path(id, NULL);
     } else {
-        char *root_path = object_get_canonical_path(qdev_get_peripheral());
-        char *path = g_strdup_printf("%s/%s", root_path, id);
-
-        g_free(root_path);
-        obj = object_resolve_path_type(path, TYPE_DEVICE, NULL);
-        g_free(path);
+        obj = object_resolve_path_component(qdev_get_peripheral(), id);
     }
 
     if (!obj) {
@@ -849,12 +845,13 @@ static DeviceState *find_device_state(const char *id, Error **errp)
         return NULL;
     }
 
-    if (!object_dynamic_cast(obj, TYPE_DEVICE)) {
+    dev = (DeviceState *)object_dynamic_cast(obj, TYPE_DEVICE);
+    if (!dev) {
         error_setg(errp, "%s is not a hotpluggable device", id);
         return NULL;
     }
 
-    return DEVICE(obj);
+    return dev;
 }
 
 void qdev_unplug(DeviceState *dev, Error **errp)
-- 
2.31.1


Re: [PATCH] monitor: Tidy up find_device_state()
Posted by Daniel P. Berrangé 2 years, 7 months ago
On Thu, Sep 16, 2021 at 01:17:07PM +0200, Markus Armbruster wrote:
> Commit 6287d827d4 "monitor: allow device_del to accept QOM paths"
> extended find_device_state() to accept QOM paths in addition to qdev
> IDs.  This added a checked conversion to TYPE_DEVICE at the end, which
> duplicates the check done for the qdev ID case earlier, except it sets
> a *different* error: GenericError "ID is not a hotpluggable device"
> when passed a QOM path, and DeviceNotFound "Device 'ID' not found"
> when passed a qdev ID.  Fortunately, the latter won't happen as long
> as we add only devices to /machine/peripheral/.
> 
> Earlier, commit b6cc36abb2 "qdev: device_del: Search for to be
> unplugged device in 'peripheral' container" rewrote the lookup by qdev
> ID to use QOM instead of qdev_find_recursive(), so it can handle
> buss-less devices.  It does so by constructing an absolute QOM path.
> Works, but object_resolve_path_component() is easier.  Switching to it
> also gets rid of the unclean duplication described above.
> 
> While there, avoid converting to TYPE_DEVICE twice, first to check
> whether it's possible, and then for real.
> 
> Signed-off-by: Markus Armbruster <armbru@redhat.com>
> ---
>  softmmu/qdev-monitor.c | 13 +++++--------
>  1 file changed, 5 insertions(+), 8 deletions(-)

Reviewed-by: Daniel P. Berrangé <berrange@redhat.com>


Regards,
Daniel
-- 
|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|


Re: [PATCH] monitor: Tidy up find_device_state()
Posted by Markus Armbruster 2 years, 6 months ago
Ping?

Markus Armbruster <armbru@redhat.com> writes:

> Commit 6287d827d4 "monitor: allow device_del to accept QOM paths"
> extended find_device_state() to accept QOM paths in addition to qdev
> IDs.  This added a checked conversion to TYPE_DEVICE at the end, which
> duplicates the check done for the qdev ID case earlier, except it sets
> a *different* error: GenericError "ID is not a hotpluggable device"
> when passed a QOM path, and DeviceNotFound "Device 'ID' not found"
> when passed a qdev ID.  Fortunately, the latter won't happen as long
> as we add only devices to /machine/peripheral/.
>
> Earlier, commit b6cc36abb2 "qdev: device_del: Search for to be
> unplugged device in 'peripheral' container" rewrote the lookup by qdev
> ID to use QOM instead of qdev_find_recursive(), so it can handle
> buss-less devices.  It does so by constructing an absolute QOM path.
> Works, but object_resolve_path_component() is easier.  Switching to it
> also gets rid of the unclean duplication described above.
>
> While there, avoid converting to TYPE_DEVICE twice, first to check
> whether it's possible, and then for real.
>
> Signed-off-by: Markus Armbruster <armbru@redhat.com>
> ---
>  softmmu/qdev-monitor.c | 13 +++++--------
>  1 file changed, 5 insertions(+), 8 deletions(-)
>
> diff --git a/softmmu/qdev-monitor.c b/softmmu/qdev-monitor.c
> index a304754ab9..d1ab3c25fb 100644
> --- a/softmmu/qdev-monitor.c
> +++ b/softmmu/qdev-monitor.c
> @@ -831,16 +831,12 @@ void qmp_device_add(QDict *qdict, QObject **ret_data, Error **errp)
>  static DeviceState *find_device_state(const char *id, Error **errp)
>  {
>      Object *obj;
> +    DeviceState *dev;
>  
>      if (id[0] == '/') {
>          obj = object_resolve_path(id, NULL);
>      } else {
> -        char *root_path = object_get_canonical_path(qdev_get_peripheral());
> -        char *path = g_strdup_printf("%s/%s", root_path, id);
> -
> -        g_free(root_path);
> -        obj = object_resolve_path_type(path, TYPE_DEVICE, NULL);
> -        g_free(path);
> +        obj = object_resolve_path_component(qdev_get_peripheral(), id);
>      }
>  
>      if (!obj) {
> @@ -849,12 +845,13 @@ static DeviceState *find_device_state(const char *id, Error **errp)
>          return NULL;
>      }
>  
> -    if (!object_dynamic_cast(obj, TYPE_DEVICE)) {
> +    dev = (DeviceState *)object_dynamic_cast(obj, TYPE_DEVICE);
> +    if (!dev) {
>          error_setg(errp, "%s is not a hotpluggable device", id);
>          return NULL;
>      }
>  
> -    return DEVICE(obj);
> +    return dev;
>  }
>  
>  void qdev_unplug(DeviceState *dev, Error **errp)


Re: [PATCH] monitor: Tidy up find_device_state()
Posted by Damien Hedde 2 years, 6 months ago

On 9/16/21 13:17, Markus Armbruster wrote:
> Commit 6287d827d4 "monitor: allow device_del to accept QOM paths"
> extended find_device_state() to accept QOM paths in addition to qdev
> IDs.  This added a checked conversion to TYPE_DEVICE at the end, which
> duplicates the check done for the qdev ID case earlier, except it sets
> a *different* error: GenericError "ID is not a hotpluggable device"
> when passed a QOM path, and DeviceNotFound "Device 'ID' not found"
> when passed a qdev ID.  Fortunately, the latter won't happen as long
> as we add only devices to /machine/peripheral/.
> 
> Earlier, commit b6cc36abb2 "qdev: device_del: Search for to be
> unplugged device in 'peripheral' container" rewrote the lookup by qdev
> ID to use QOM instead of qdev_find_recursive(), so it can handle
> buss-less devices.  It does so by constructing an absolute QOM path.
> Works, but object_resolve_path_component() is easier.  Switching to it
> also gets rid of the unclean duplication described above.
> 
> While there, avoid converting to TYPE_DEVICE twice, first to check
> whether it's possible, and then for real.
> 
> Signed-off-by: Markus Armbruster <armbru@redhat.com>

Reviewed-by: Damien Hedde <damien.hedde@greensocs.com>
> ---
>   softmmu/qdev-monitor.c | 13 +++++--------
>   1 file changed, 5 insertions(+), 8 deletions(-)
> 
> diff --git a/softmmu/qdev-monitor.c b/softmmu/qdev-monitor.c
> index a304754ab9..d1ab3c25fb 100644
> --- a/softmmu/qdev-monitor.c
> +++ b/softmmu/qdev-monitor.c
> @@ -831,16 +831,12 @@ void qmp_device_add(QDict *qdict, QObject **ret_data, Error **errp)
>   static DeviceState *find_device_state(const char *id, Error **errp)
>   {
>       Object *obj;
> +    DeviceState *dev;
>   
>       if (id[0] == '/') {
>           obj = object_resolve_path(id, NULL);
>       } else {
> -        char *root_path = object_get_canonical_path(qdev_get_peripheral());
> -        char *path = g_strdup_printf("%s/%s", root_path, id);
> -
> -        g_free(root_path);
> -        obj = object_resolve_path_type(path, TYPE_DEVICE, NULL);
> -        g_free(path);
> +        obj = object_resolve_path_component(qdev_get_peripheral(), id);
>       }
>   
>       if (!obj) {
> @@ -849,12 +845,13 @@ static DeviceState *find_device_state(const char *id, Error **errp)
>           return NULL;
>       }
>   
> -    if (!object_dynamic_cast(obj, TYPE_DEVICE)) {
> +    dev = (DeviceState *)object_dynamic_cast(obj, TYPE_DEVICE);
> +    if (!dev) {
>           error_setg(errp, "%s is not a hotpluggable device", id);
>           return NULL;
>       }
>   
> -    return DEVICE(obj);
> +    return dev;
>   }
>   
>   void qdev_unplug(DeviceState *dev, Error **errp)
> 

Re: [PATCH] monitor: Tidy up find_device_state()
Posted by Paolo Bonzini 2 years, 6 months ago
On 16/09/21 13:17, Markus Armbruster wrote:
> Commit 6287d827d4 "monitor: allow device_del to accept QOM paths"
> extended find_device_state() to accept QOM paths in addition to qdev
> IDs.  This added a checked conversion to TYPE_DEVICE at the end, which
> duplicates the check done for the qdev ID case earlier, except it sets
> a *different* error: GenericError "ID is not a hotpluggable device"
> when passed a QOM path, and DeviceNotFound "Device 'ID' not found"
> when passed a qdev ID.  Fortunately, the latter won't happen as long
> as we add only devices to /machine/peripheral/.
> 
> Earlier, commit b6cc36abb2 "qdev: device_del: Search for to be
> unplugged device in 'peripheral' container" rewrote the lookup by qdev
> ID to use QOM instead of qdev_find_recursive(), so it can handle
> buss-less devices.  It does so by constructing an absolute QOM path.
> Works, but object_resolve_path_component() is easier.  Switching to it
> also gets rid of the unclean duplication described above.
> 
> While there, avoid converting to TYPE_DEVICE twice, first to check
> whether it's possible, and then for real.
> 
> Signed-off-by: Markus Armbruster <armbru@redhat.com>
> ---
>   softmmu/qdev-monitor.c | 13 +++++--------
>   1 file changed, 5 insertions(+), 8 deletions(-)

Queued, thanks.

Paolo

> diff --git a/softmmu/qdev-monitor.c b/softmmu/qdev-monitor.c
> index a304754ab9..d1ab3c25fb 100644
> --- a/softmmu/qdev-monitor.c
> +++ b/softmmu/qdev-monitor.c
> @@ -831,16 +831,12 @@ void qmp_device_add(QDict *qdict, QObject **ret_data, Error **errp)
>   static DeviceState *find_device_state(const char *id, Error **errp)
>   {
>       Object *obj;
> +    DeviceState *dev;
>   
>       if (id[0] == '/') {
>           obj = object_resolve_path(id, NULL);
>       } else {
> -        char *root_path = object_get_canonical_path(qdev_get_peripheral());
> -        char *path = g_strdup_printf("%s/%s", root_path, id);
> -
> -        g_free(root_path);
> -        obj = object_resolve_path_type(path, TYPE_DEVICE, NULL);
> -        g_free(path);
> +        obj = object_resolve_path_component(qdev_get_peripheral(), id);
>       }
>   
>       if (!obj) {
> @@ -849,12 +845,13 @@ static DeviceState *find_device_state(const char *id, Error **errp)
>           return NULL;
>       }
>   
> -    if (!object_dynamic_cast(obj, TYPE_DEVICE)) {
> +    dev = (DeviceState *)object_dynamic_cast(obj, TYPE_DEVICE);
> +    if (!dev) {
>           error_setg(errp, "%s is not a hotpluggable device", id);
>           return NULL;
>       }
>   
> -    return DEVICE(obj);
> +    return dev;
>   }
>   
>   void qdev_unplug(DeviceState *dev, Error **errp)
>