[PATCH v2 07/13] qom: Create system containers explicitly

Peter Xu posted 13 patches 1 day, 19 hours ago
[PATCH v2 07/13] qom: Create system containers explicitly
Posted by Peter Xu 1 day, 19 hours ago
Always explicitly create QEMU system containers upfront.

Root containers will be created when trying to fetch the root object the
1st time.  They are:

  /objects
  /chardevs
  /backend

Machine sub-containers will be created only until machine is being
initialized.  They are:

  /machine/unattached
  /machine/peripheral
  /machine/peripheral-anon

Signed-off-by: Peter Xu <peterx@redhat.com>
---
 hw/core/machine.c |  3 ---
 qom/object.c      | 24 +++++++++++++++++++++++-
 system/vl.c       | 16 ++++++++++++++++
 3 files changed, 39 insertions(+), 4 deletions(-)

diff --git a/hw/core/machine.c b/hw/core/machine.c
index a35c4a8fae..a72c001c3d 100644
--- a/hw/core/machine.c
+++ b/hw/core/machine.c
@@ -1198,9 +1198,6 @@ static void machine_initfn(Object *obj)
     MachineState *ms = MACHINE(obj);
     MachineClass *mc = MACHINE_GET_CLASS(obj);
 
-    container_get(obj, "/peripheral");
-    container_get(obj, "/peripheral-anon");
-
     ms->dump_guest_core = true;
     ms->mem_merge = (QEMU_MADV_MERGEABLE != QEMU_MADV_INVALID);
     ms->enable_graphics = true;
diff --git a/qom/object.c b/qom/object.c
index 214d6eb4c1..2fb0b8418e 100644
--- a/qom/object.c
+++ b/qom/object.c
@@ -1734,12 +1734,34 @@ const char *object_property_get_type(Object *obj, const char *name, Error **errp
     return prop->type;
 }
 
+static const char *const root_containers[] = {
+    "chardevs",
+    "objects",
+    "backend"
+};
+
+static Object *object_root_initialize(void)
+{
+    Object *root = object_new(TYPE_CONTAINER);
+    int i;
+
+    /*
+     * Create all QEMU system containers.  "machine" and its sub-containers
+     * are only created when machine initializes (qemu_create_machine()).
+     */
+    for (i = 0; i < ARRAY_SIZE(root_containers); i++) {
+        object_property_add_new_container(root, root_containers[i]);
+    }
+
+    return root;
+}
+
 Object *object_get_root(void)
 {
     static Object *root;
 
     if (!root) {
-        root = object_new(TYPE_CONTAINER);
+        root = object_root_initialize();
     }
 
     return root;
diff --git a/system/vl.c b/system/vl.c
index 3bb8f2db9a..15e35162c6 100644
--- a/system/vl.c
+++ b/system/vl.c
@@ -2112,6 +2112,21 @@ static void parse_memory_options(void)
     loc_pop(&loc);
 }
 
+static const char *const machine_containers[] = {
+    "unattached",
+    "peripheral",
+    "peripheral-anon"
+};
+
+static void qemu_create_machine_containers(Object *machine)
+{
+    int i;
+
+    for (i = 0; i < ARRAY_SIZE(machine_containers); i++) {
+        object_property_add_new_container(machine, machine_containers[i]);
+    }
+}
+
 static void qemu_create_machine(QDict *qdict)
 {
     MachineClass *machine_class = select_machine(qdict, &error_fatal);
@@ -2120,6 +2135,7 @@ static void qemu_create_machine(QDict *qdict)
     current_machine = MACHINE(object_new_with_class(OBJECT_CLASS(machine_class)));
     object_property_add_child(object_get_root(), "machine",
                               OBJECT(current_machine));
+    qemu_create_machine_containers(OBJECT(current_machine));
     object_property_add_child(container_get(OBJECT(current_machine),
                                             "/unattached"),
                               "sysbus", OBJECT(sysbus_get_default()));
-- 
2.45.0
Re: [PATCH v2 07/13] qom: Create system containers explicitly
Posted by Philippe Mathieu-Daudé 1 day, 18 hours ago
On 21/11/24 20:21, Peter Xu wrote:
> Always explicitly create QEMU system containers upfront.
> 
> Root containers will be created when trying to fetch the root object the
> 1st time.  They are:
> 
>    /objects
>    /chardevs
>    /backend
> 
> Machine sub-containers will be created only until machine is being
> initialized.  They are:
> 
>    /machine/unattached
>    /machine/peripheral
>    /machine/peripheral-anon
> 
> Signed-off-by: Peter Xu <peterx@redhat.com>
> ---
>   hw/core/machine.c |  3 ---
>   qom/object.c      | 24 +++++++++++++++++++++++-
>   system/vl.c       | 16 ++++++++++++++++
>   3 files changed, 39 insertions(+), 4 deletions(-)
> 
> diff --git a/hw/core/machine.c b/hw/core/machine.c
> index a35c4a8fae..a72c001c3d 100644
> --- a/hw/core/machine.c
> +++ b/hw/core/machine.c
> @@ -1198,9 +1198,6 @@ static void machine_initfn(Object *obj)
>       MachineState *ms = MACHINE(obj);
>       MachineClass *mc = MACHINE_GET_CLASS(obj);
>   
> -    container_get(obj, "/peripheral");
> -    container_get(obj, "/peripheral-anon");
> -
>       ms->dump_guest_core = true;
>       ms->mem_merge = (QEMU_MADV_MERGEABLE != QEMU_MADV_INVALID);
>       ms->enable_graphics = true;
> diff --git a/qom/object.c b/qom/object.c
> index 214d6eb4c1..2fb0b8418e 100644
> --- a/qom/object.c
> +++ b/qom/object.c
> @@ -1734,12 +1734,34 @@ const char *object_property_get_type(Object *obj, const char *name, Error **errp
>       return prop->type;
>   }
>   
> +static const char *const root_containers[] = {
> +    "chardevs",
> +    "objects",
> +    "backend"
> +};
> +
> +static Object *object_root_initialize(void)
> +{

(root_containers[] can be declared here)

> +    Object *root = object_new(TYPE_CONTAINER);
> +    int i;
> +
> +    /*
> +     * Create all QEMU system containers.  "machine" and its sub-containers
> +     * are only created when machine initializes (qemu_create_machine()).

I'm not sure this comment is helpful. Regardless,

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

> +     */
> +    for (i = 0; i < ARRAY_SIZE(root_containers); i++) {
> +        object_property_add_new_container(root, root_containers[i]);
> +    }
> +
> +    return root;
> +}

> +
>   Object *object_get_root(void)
>   {
>       static Object *root;
>   
>       if (!root) {
> -        root = object_new(TYPE_CONTAINER);
> +        root = object_root_initialize();
>       }
>   
>       return root;
> diff --git a/system/vl.c b/system/vl.c
> index 3bb8f2db9a..15e35162c6 100644
> --- a/system/vl.c
> +++ b/system/vl.c
> @@ -2112,6 +2112,21 @@ static void parse_memory_options(void)
>       loc_pop(&loc);
>   }
>   
> +static const char *const machine_containers[] = {
> +    "unattached",
> +    "peripheral",
> +    "peripheral-anon"
> +};
> +
> +static void qemu_create_machine_containers(Object *machine)
> +{

(ditto machine_containers[])

> +    int i;
> +
> +    for (i = 0; i < ARRAY_SIZE(machine_containers); i++) {
> +        object_property_add_new_container(machine, machine_containers[i]);
> +    }
> +}
> +
>   static void qemu_create_machine(QDict *qdict)
>   {
>       MachineClass *machine_class = select_machine(qdict, &error_fatal);
> @@ -2120,6 +2135,7 @@ static void qemu_create_machine(QDict *qdict)
>       current_machine = MACHINE(object_new_with_class(OBJECT_CLASS(machine_class)));
>       object_property_add_child(object_get_root(), "machine",
>                                 OBJECT(current_machine));
> +    qemu_create_machine_containers(OBJECT(current_machine));
>       object_property_add_child(container_get(OBJECT(current_machine),
>                                               "/unattached"),
>                                 "sysbus", OBJECT(sysbus_get_default()));