qom: module loading cleanups
cover qom: module loading cleanups
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(-)
Signature · 1 line
2.47.0
PATCH 1/5 qom: remove unused function
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.
--- include/qom/object.h | 8 -------- qom/object.c | 8 -------- 2 files changed, 16 deletions(-)
include/qom/object.h +0 -8
@@ -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
qom/object.c +0 -8
@@ -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);
Signature · 1 line
2.47.0
Re: [PATCH 1/5] qom: remove unused function
Quoted 11 lines · Paolo Bonzini
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 <[email protected]>Signed-off-by Paolo Bonzini <[email protected]>--- include/qom/object.h | 8 -------- qom/object.c | 8 -------- 2 files changed, 16 deletions(-)
With regards,
Daniel
Signature · 3 lines
|: 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 :|
PATCH 2/5 qom: use object_new_with_class when possible
In reply to Paolo BonziniA 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.
--- hw/core/qdev.c | 5 +++-- qom/object_interfaces.c | 2 +- qom/qom-qmp-cmds.c | 2 +- 3 files changed, 5 insertions(+), 4 deletions(-)
hw/core/qdev.c +3 -2
@@ -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
qom/object_interfaces.c +1 -1
@@ -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;
qom/qom-qmp-cmds.c +1 -1
@@ -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))) {
Signature · 1 line
2.47.0
Re: [PATCH 2/5] qom: use object_new_with_class when possible
Quoted 10 lines · Paolo Bonzini
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 <[email protected]>--- hw/core/qdev.c | 5 +++-- qom/object_interfaces.c | 2 +- qom/qom-qmp-cmds.c | 2 +- 3 files changed, 5 insertions(+), 4 deletions(-)
PATCH 3/5 qom: centralize module-loading functionality
In reply to Paolo BonziniPut 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.
--- qom/object.c | 72 ++++++++++++++++++++++++++-------------------------- 1 file changed, 36 insertions(+), 36 deletions(-)
qom/object.c +36 -36
@@ -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 *typevoid 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)
Signature · 1 line
2.47.0
PATCH 4/5 qom: let object_new use a module if the type is not present
In reply to Paolo Bonziniobject_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.
--- hw/core/qdev.c | 16 ---------------- qom/object.c | 2 +- 2 files changed, 1 insertion(+), 17 deletions(-)
hw/core/qdev.c +0 -16
@@ -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)); }
qom/object.c +1 -1
@@ -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); }
Signature · 1 line
2.47.0
PATCH 5/5 qom: allow user-creatable classes to be in modules
In reply to Paolo BonziniThere 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.
--- qom/object_interfaces.c | 2 +- qom/qom-qmp-cmds.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-)
qom/object_interfaces.c +1 -1
@@ -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 ;
qom/qom-qmp-cmds.c +1 -1
@@ -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);
Signature · 1 line
2.47.0
Quoted 6 lines · Paolo Bonzini
Paolo Bonzini (5):
qom: remove unused function
qom: useobject_new_with_classwhen possible
qom: centralize module-loading functionality
qom: letobject_newuse a module if the type is not present
qom: allow user-creatable classes to be in modules
r~