qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 0/5] qom: module loading cleanups
@ 2024-10-29 12:26 Paolo Bonzini
  2024-10-29 12:26 ` [PATCH 1/5] qom: remove unused function Paolo Bonzini
                   ` (5 more replies)
  0 siblings, 6 replies; 9+ messages in thread
From: Paolo Bonzini @ 2024-10-29 12:26 UTC (permalink / raw)
  To: qemu-devel; +Cc: armbru

While looking at possible Rust APIs for object_new/qdev_new (one of the
functions that rust/hw/char/pl011 uses via FFI) I noticed that the support
for modules in QOM APIs is quite random.

In particular, loading modules is done by object_initialize() and
qdev_new(), but not by object_new(); furthermore, the loading code is
duplicated between object_initialize() and module_object_class_by_name().
This series fixes that.

Patches 1-2 are small cleanups.

Patches 3-4 let object_new() use a module if the type is not present,
like object_initialize() already does.  To get there, all calls to
type_get_by_name() are changed to either type_get_by_name_noload() (the
old function) or type_get_or_load_by_name() (which looks at modules
like object_initialize() and module_object_class_by_name() do).

This is enough to let Rust code use a single function object_new() to create
QOM objects; the function can be wrapped to allow writing something like
"PL011State::new()" instead of object_new(TYPE_PL011) as *mut PL011State".

Patch 5 finally allows modularized user-creatable classes, which are
not allowed just because the code uses the non-module-friendly
object_class_by_name().  While there are no modularized user-creatable
classes right now, there is no real difference between them and
character devices (which do allow modularization).  This patch could
be left out if desired.

Paolo


Paolo Bonzini (5):
  qom: remove unused function
  qom: use object_new_with_class when possible
  qom: centralize module-loading functionality
  qom: let object_new use a module if the type is not present
  qom: allow user-creatable classes to be in modules

 include/qom/object.h    |  8 -----
 hw/core/qdev.c          | 21 ++---------
 qom/object.c            | 80 +++++++++++++++++++----------------------
 qom/object_interfaces.c |  4 +--
 qom/qom-qmp-cmds.c      |  4 +--
 5 files changed, 43 insertions(+), 74 deletions(-)

-- 
2.47.0



^ permalink raw reply	[flat|nested] 9+ messages in thread

* [PATCH 1/5] qom: remove unused function
  2024-10-29 12:26 [PATCH 0/5] qom: module loading cleanups Paolo Bonzini
@ 2024-10-29 12:26 ` Paolo Bonzini
  2024-10-29 12:35   ` Daniel P. Berrangé
  2024-10-29 12:26 ` [PATCH 2/5] qom: use object_new_with_class when possible Paolo Bonzini
                   ` (4 subsequent siblings)
  5 siblings, 1 reply; 9+ messages in thread
From: Paolo Bonzini @ 2024-10-29 12:26 UTC (permalink / raw)
  To: qemu-devel; +Cc: armbru, Dr. David Alan Gilbert

The function has been unused since commit 4fa28f23906 ("ppc/pnv:
Instantiate cores separately", 2019-12-17).  The idea was that
you could use it to build an array of objects via pointer
arithmetic, but no one is doing it anymore.

Cc: Dr. David Alan Gilbert <dave@treblig.org>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
---
 include/qom/object.h | 8 --------
 qom/object.c         | 8 --------
 2 files changed, 16 deletions(-)

diff --git a/include/qom/object.h b/include/qom/object.h
index 2af9854675c..43c135984a6 100644
--- a/include/qom/object.h
+++ b/include/qom/object.h
@@ -2032,14 +2032,6 @@ int object_child_foreach_recursive(Object *obj,
  */
 Object *container_get(Object *root, const char *path);
 
-/**
- * object_type_get_instance_size:
- * @typename: Name of the Type whose instance_size is required
- *
- * Returns the instance_size of the given @typename.
- */
-size_t object_type_get_instance_size(const char *typename);
-
 /**
  * object_property_help:
  * @name: the name of the property
diff --git a/qom/object.c b/qom/object.c
index 11424cf4711..8b269414488 100644
--- a/qom/object.c
+++ b/qom/object.c
@@ -262,14 +262,6 @@ static size_t type_object_get_align(TypeImpl *ti)
     return 0;
 }
 
-size_t object_type_get_instance_size(const char *typename)
-{
-    TypeImpl *type = type_get_by_name(typename);
-
-    g_assert(type != NULL);
-    return type_object_get_size(type);
-}
-
 static bool type_is_ancestor(TypeImpl *type, TypeImpl *target_type)
 {
     assert(target_type);
-- 
2.47.0



^ permalink raw reply related	[flat|nested] 9+ messages in thread

* [PATCH 2/5] qom: use object_new_with_class when possible
  2024-10-29 12:26 [PATCH 0/5] qom: module loading cleanups Paolo Bonzini
  2024-10-29 12:26 ` [PATCH 1/5] qom: remove unused function Paolo Bonzini
@ 2024-10-29 12:26 ` Paolo Bonzini
  2024-10-29 19:51   ` Philippe Mathieu-Daudé
  2024-10-29 12:26 ` [PATCH 3/5] qom: centralize module-loading functionality Paolo Bonzini
                   ` (3 subsequent siblings)
  5 siblings, 1 reply; 9+ messages in thread
From: Paolo Bonzini @ 2024-10-29 12:26 UTC (permalink / raw)
  To: qemu-devel; +Cc: armbru

A small optimization/code simplification, that also makes it clear that
we won't look for a type in a not-loaded-yet module---the module will
have been loaded by a call to module_object_class_by_name(), if present.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
---
 hw/core/qdev.c          | 5 +++--
 qom/object_interfaces.c | 2 +-
 qom/qom-qmp-cmds.c      | 2 +-
 3 files changed, 5 insertions(+), 4 deletions(-)

diff --git a/hw/core/qdev.c b/hw/core/qdev.c
index 17c454334c6..5f13111b77c 100644
--- a/hw/core/qdev.c
+++ b/hw/core/qdev.c
@@ -151,10 +151,11 @@ DeviceState *qdev_new(const char *name)
 
 DeviceState *qdev_try_new(const char *name)
 {
-    if (!module_object_class_by_name(name)) {
+    ObjectClass *oc = module_object_class_by_name(name);
+    if (!oc) {
         return NULL;
     }
-    return DEVICE(object_new(name));
+    return DEVICE(object_new_with_class(oc));
 }
 
 static QTAILQ_HEAD(, DeviceListener) device_listeners
diff --git a/qom/object_interfaces.c b/qom/object_interfaces.c
index e0833c8bfe4..1f2aa133066 100644
--- a/qom/object_interfaces.c
+++ b/qom/object_interfaces.c
@@ -108,7 +108,7 @@ Object *user_creatable_add_type(const char *type, const char *id,
     }
 
     assert(qdict);
-    obj = object_new(type);
+    obj = object_new_with_class(klass);
     object_set_properties_from_qdict(obj, qdict, v, &local_err);
     if (local_err) {
         goto out;
diff --git a/qom/qom-qmp-cmds.c b/qom/qom-qmp-cmds.c
index e91a2353472..69a8e17aa80 100644
--- a/qom/qom-qmp-cmds.c
+++ b/qom/qom-qmp-cmds.c
@@ -141,7 +141,7 @@ ObjectPropertyInfoList *qmp_device_list_properties(const char *typename,
         return NULL;
     }
 
-    obj = object_new(typename);
+    obj = object_new_with_class(klass);
 
     object_property_iter_init(&iter, obj);
     while ((prop = object_property_iter_next(&iter))) {
-- 
2.47.0



^ permalink raw reply related	[flat|nested] 9+ messages in thread

* [PATCH 3/5] qom: centralize module-loading functionality
  2024-10-29 12:26 [PATCH 0/5] qom: module loading cleanups Paolo Bonzini
  2024-10-29 12:26 ` [PATCH 1/5] qom: remove unused function Paolo Bonzini
  2024-10-29 12:26 ` [PATCH 2/5] qom: use object_new_with_class when possible Paolo Bonzini
@ 2024-10-29 12:26 ` Paolo Bonzini
  2024-10-29 12:26 ` [PATCH 4/5] qom: let object_new use a module if the type is not present Paolo Bonzini
                   ` (2 subsequent siblings)
  5 siblings, 0 replies; 9+ messages in thread
From: Paolo Bonzini @ 2024-10-29 12:26 UTC (permalink / raw)
  To: qemu-devel; +Cc: armbru

Put together the common code of object_initialize() and
module_object_class_by_name() into a function that supports
Error **.  Rename the existing function type_get_by_name() to
clarify that it will only look at defined types; this is often
okay within object.c to look at the parents, but not outside it.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
---
 qom/object.c | 72 ++++++++++++++++++++++++++--------------------------
 1 file changed, 36 insertions(+), 36 deletions(-)

diff --git a/qom/object.c b/qom/object.c
index 8b269414488..29155c64639 100644
--- a/qom/object.c
+++ b/qom/object.c
@@ -195,7 +195,7 @@ void type_register_static_array(const TypeInfo *infos, int nr_infos)
     }
 }
 
-static TypeImpl *type_get_by_name(const char *name)
+static TypeImpl *type_get_by_name_noload(const char *name)
 {
     if (name == NULL) {
         return NULL;
@@ -204,10 +204,32 @@ static TypeImpl *type_get_by_name(const char *name)
     return type_table_lookup(name);
 }
 
+static TypeImpl *type_get_or_load_by_name(const char *name, Error **errp)
+{
+    TypeImpl *type = type_get_by_name_noload(name);
+
+#ifdef CONFIG_MODULES
+    if (!type) {
+        int rv = module_load_qom(name, errp);
+        if (rv > 0) {
+            type = type_get_by_name_noload(name);
+        } else {
+            error_prepend(errp, "could not load a module for type '%s'", name);
+            return NULL;
+        }
+    }
+#endif
+    if (!type) {
+        error_setg(errp, "unknown type '%s'", name);
+    }
+
+    return type;
+}
+
 static TypeImpl *type_get_parent(TypeImpl *type)
 {
     if (!type->parent_type && type->parent) {
-        type->parent_type = type_get_by_name(type->parent);
+        type->parent_type = type_get_by_name_noload(type->parent);
         if (!type->parent_type) {
             fprintf(stderr, "Type '%s' is missing its parent '%s'\n",
                     type->name, type->parent);
@@ -363,7 +385,7 @@ static void type_initialize(TypeImpl *ti)
         }
 
         for (i = 0; i < ti->num_interfaces; i++) {
-            TypeImpl *t = type_get_by_name(ti->interfaces[i].typename);
+            TypeImpl *t = type_get_by_name_noload(ti->interfaces[i].typename);
             if (!t) {
                 error_report("missing interface '%s' for object '%s'",
                              ti->interfaces[i].typename, parent->name);
@@ -557,23 +579,7 @@ static void object_initialize_with_type(Object *obj, size_t size, TypeImpl *type
 
 void object_initialize(void *data, size_t size, const char *typename)
 {
-    TypeImpl *type = type_get_by_name(typename);
-
-#ifdef CONFIG_MODULES
-    if (!type) {
-        int rv = module_load_qom(typename, &error_fatal);
-        if (rv > 0) {
-            type = type_get_by_name(typename);
-        } else {
-            error_report("missing object type '%s'", typename);
-            exit(1);
-        }
-    }
-#endif
-    if (!type) {
-        error_report("missing object type '%s'", typename);
-        abort();
-    }
+    TypeImpl *type = type_get_or_load_by_name(typename, &error_fatal);
 
     object_initialize_with_type(data, size, type);
 }
@@ -784,7 +790,7 @@ Object *object_new_with_class(ObjectClass *klass)
 
 Object *object_new(const char *typename)
 {
-    TypeImpl *ti = type_get_by_name(typename);
+    TypeImpl *ti = type_get_by_name_noload(typename);
 
     return object_new_with_type(ti);
 }
@@ -957,7 +963,7 @@ ObjectClass *object_class_dynamic_cast(ObjectClass *class,
         return class;
     }
 
-    target_type = type_get_by_name(typename);
+    target_type = type_get_by_name_noload(typename);
     if (!target_type) {
         /* target class type unknown, so fail the cast */
         return NULL;
@@ -1055,7 +1061,7 @@ const char *object_class_get_name(ObjectClass *klass)
 
 ObjectClass *object_class_by_name(const char *typename)
 {
-    TypeImpl *type = type_get_by_name(typename);
+    TypeImpl *type = type_get_by_name_noload(typename);
 
     if (!type) {
         return NULL;
@@ -1068,21 +1074,15 @@ ObjectClass *object_class_by_name(const char *typename)
 
 ObjectClass *module_object_class_by_name(const char *typename)
 {
-    ObjectClass *oc;
+    TypeImpl *type = type_get_or_load_by_name(typename, NULL);
 
-    oc = object_class_by_name(typename);
-#ifdef CONFIG_MODULES
-    if (!oc) {
-        Error *local_err = NULL;
-        int rv = module_load_qom(typename, &local_err);
-        if (rv > 0) {
-            oc = object_class_by_name(typename);
-        } else if (rv < 0) {
-            error_report_err(local_err);
-        }
+    if (!type) {
+        return NULL;
     }
-#endif
-    return oc;
+
+    type_initialize(type);
+
+    return type->class;
 }
 
 ObjectClass *object_class_get_parent(ObjectClass *class)
-- 
2.47.0



^ permalink raw reply related	[flat|nested] 9+ messages in thread

* [PATCH 4/5] qom: let object_new use a module if the type is not present
  2024-10-29 12:26 [PATCH 0/5] qom: module loading cleanups Paolo Bonzini
                   ` (2 preceding siblings ...)
  2024-10-29 12:26 ` [PATCH 3/5] qom: centralize module-loading functionality Paolo Bonzini
@ 2024-10-29 12:26 ` Paolo Bonzini
  2024-10-29 12:26 ` [PATCH 5/5] qom: allow user-creatable classes to be in modules Paolo Bonzini
  2024-10-29 17:05 ` [PATCH 0/5] qom: module loading cleanups Richard Henderson
  5 siblings, 0 replies; 9+ messages in thread
From: Paolo Bonzini @ 2024-10-29 12:26 UTC (permalink / raw)
  To: qemu-devel; +Cc: armbru

object_initialize() can use modules (it was added there because
virtio-gpu-device is a child device of virtio-gpu-pci; commit
64f7aece8ea, "object_initialize: try module load", 2020-09-15).
object_new() cannot; make things consistent.

qdev_new() is now just a simple wrapper that returns DeviceState.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
---
 hw/core/qdev.c | 16 ----------------
 qom/object.c   |  2 +-
 2 files changed, 1 insertion(+), 17 deletions(-)

diff --git a/hw/core/qdev.c b/hw/core/qdev.c
index db36f54d914..17c454334c6 100644
--- a/hw/core/qdev.c
+++ b/hw/core/qdev.c
@@ -146,22 +146,6 @@ bool qdev_set_parent_bus(DeviceState *dev, BusState *bus, Error **errp)
 
 DeviceState *qdev_new(const char *name)
 {
-    ObjectClass *oc = object_class_by_name(name);
-#ifdef CONFIG_MODULES
-    if (!oc) {
-        int rv = module_load_qom(name, &error_fatal);
-        if (rv > 0) {
-            oc = object_class_by_name(name);
-        } else {
-            error_report("could not find a module for type '%s'", name);
-            exit(1);
-        }
-    }
-#endif
-    if (!oc) {
-        error_report("unknown type '%s'", name);
-        abort();
-    }
     return DEVICE(object_new(name));
 }
 
diff --git a/qom/object.c b/qom/object.c
index 29155c64639..9edc06d391f 100644
--- a/qom/object.c
+++ b/qom/object.c
@@ -790,7 +790,7 @@ Object *object_new_with_class(ObjectClass *klass)
 
 Object *object_new(const char *typename)
 {
-    TypeImpl *ti = type_get_by_name_noload(typename);
+    TypeImpl *ti = type_get_or_load_by_name(typename, &error_fatal);
 
     return object_new_with_type(ti);
 }
-- 
2.47.0



^ permalink raw reply related	[flat|nested] 9+ messages in thread

* [PATCH 5/5] qom: allow user-creatable classes to be in modules
  2024-10-29 12:26 [PATCH 0/5] qom: module loading cleanups Paolo Bonzini
                   ` (3 preceding siblings ...)
  2024-10-29 12:26 ` [PATCH 4/5] qom: let object_new use a module if the type is not present Paolo Bonzini
@ 2024-10-29 12:26 ` Paolo Bonzini
  2024-10-29 17:05 ` [PATCH 0/5] qom: module loading cleanups Richard Henderson
  5 siblings, 0 replies; 9+ messages in thread
From: Paolo Bonzini @ 2024-10-29 12:26 UTC (permalink / raw)
  To: qemu-devel; +Cc: armbru

There is no real reason to make user-creatable classes different
from other backends in this respect.  This also allows modularized
character devices to be treated by qom-list-properties just like
builtin ones.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
---
 qom/object_interfaces.c | 2 +-
 qom/qom-qmp-cmds.c      | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)

diff --git a/qom/object_interfaces.c b/qom/object_interfaces.c
index 1f2aa133066..1a6f29c053e 100644
--- a/qom/object_interfaces.c
+++ b/qom/object_interfaces.c
@@ -90,7 +90,7 @@ Object *user_creatable_add_type(const char *type, const char *id,
         return NULL;
     }
 
-    klass = object_class_by_name(type);
+    klass = module_object_class_by_name(type);
     if (!klass) {
         error_setg(errp, "invalid object type: %s", type);
         return NULL;
diff --git a/qom/qom-qmp-cmds.c b/qom/qom-qmp-cmds.c
index 69a8e17aa80..46e4562300c 100644
--- a/qom/qom-qmp-cmds.c
+++ b/qom/qom-qmp-cmds.c
@@ -186,7 +186,7 @@ ObjectPropertyInfoList *qmp_qom_list_properties(const char *typename,
     ObjectPropertyIterator iter;
     ObjectPropertyInfoList *prop_list = NULL;
 
-    klass = object_class_by_name(typename);
+    klass = module_object_class_by_name(typename);
     if (klass == NULL) {
         error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
                   "Class '%s' not found", typename);
-- 
2.47.0



^ permalink raw reply related	[flat|nested] 9+ messages in thread

* Re: [PATCH 1/5] qom: remove unused function
  2024-10-29 12:26 ` [PATCH 1/5] qom: remove unused function Paolo Bonzini
@ 2024-10-29 12:35   ` Daniel P. Berrangé
  0 siblings, 0 replies; 9+ messages in thread
From: Daniel P. Berrangé @ 2024-10-29 12:35 UTC (permalink / raw)
  To: Paolo Bonzini; +Cc: qemu-devel, armbru, Dr. David Alan Gilbert

On Tue, Oct 29, 2024 at 01:26:05PM +0100, Paolo Bonzini wrote:
> The function has been unused since commit 4fa28f23906 ("ppc/pnv:
> Instantiate cores separately", 2019-12-17).  The idea was that
> you could use it to build an array of objects via pointer
> arithmetic, but no one is doing it anymore.
> 
> Cc: Dr. David Alan Gilbert <dave@treblig.org>
> Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
> ---
>  include/qom/object.h | 8 --------
>  qom/object.c         | 8 --------
>  2 files changed, 16 deletions(-)

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


With 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 :|



^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH 0/5] qom: module loading cleanups
  2024-10-29 12:26 [PATCH 0/5] qom: module loading cleanups Paolo Bonzini
                   ` (4 preceding siblings ...)
  2024-10-29 12:26 ` [PATCH 5/5] qom: allow user-creatable classes to be in modules Paolo Bonzini
@ 2024-10-29 17:05 ` Richard Henderson
  5 siblings, 0 replies; 9+ messages in thread
From: Richard Henderson @ 2024-10-29 17:05 UTC (permalink / raw)
  To: Paolo Bonzini, qemu-devel; +Cc: armbru

On 10/29/24 12:26, Paolo Bonzini wrote:
> Paolo Bonzini (5):
>    qom: remove unused function
>    qom: use object_new_with_class when possible
>    qom: centralize module-loading functionality
>    qom: let object_new use a module if the type is not present
>    qom: allow user-creatable classes to be in modules

Reviewed-by: Richard Henderson <richard.henderson@linaro.org>

r~


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH 2/5] qom: use object_new_with_class when possible
  2024-10-29 12:26 ` [PATCH 2/5] qom: use object_new_with_class when possible Paolo Bonzini
@ 2024-10-29 19:51   ` Philippe Mathieu-Daudé
  0 siblings, 0 replies; 9+ messages in thread
From: Philippe Mathieu-Daudé @ 2024-10-29 19:51 UTC (permalink / raw)
  To: Paolo Bonzini, qemu-devel; +Cc: armbru

On 29/10/24 09:26, Paolo Bonzini wrote:
> A small optimization/code simplification, that also makes it clear that
> we won't look for a type in a not-loaded-yet module---the module will
> have been loaded by a call to module_object_class_by_name(), if present.
> 
> Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
> ---
>   hw/core/qdev.c          | 5 +++--
>   qom/object_interfaces.c | 2 +-
>   qom/qom-qmp-cmds.c      | 2 +-
>   3 files changed, 5 insertions(+), 4 deletions(-)

Reviewed-by: Philippe Mathieu-Daudé <philmd@linaro.org>



^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2024-10-29 19:52 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-10-29 12:26 [PATCH 0/5] qom: module loading cleanups Paolo Bonzini
2024-10-29 12:26 ` [PATCH 1/5] qom: remove unused function Paolo Bonzini
2024-10-29 12:35   ` Daniel P. Berrangé
2024-10-29 12:26 ` [PATCH 2/5] qom: use object_new_with_class when possible Paolo Bonzini
2024-10-29 19:51   ` Philippe Mathieu-Daudé
2024-10-29 12:26 ` [PATCH 3/5] qom: centralize module-loading functionality Paolo Bonzini
2024-10-29 12:26 ` [PATCH 4/5] qom: let object_new use a module if the type is not present Paolo Bonzini
2024-10-29 12:26 ` [PATCH 5/5] qom: allow user-creatable classes to be in modules Paolo Bonzini
2024-10-29 17:05 ` [PATCH 0/5] qom: module loading cleanups Richard Henderson

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).