Skip to contentSkip to conversation

QOM: container_get() removal

PATCH Superseded Peter Xu 5 participants 51 messages Nov 20, 2024 – Nov 21, 2024

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
author
#22 lore raw

PATCH 7/12 ppc/e500: Avoid abuse of container_get()

In reply to Peter Xu

container_get() is going to become strict on not allowing to return a non-container.

Switch the e500 user to use object_resolve_path_component() explicitly.

---
 hw/pci-host/ppce500.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
hw/pci-host/ppce500.c +2 -2
index b70631045a..65233b9e3f 100644
@@ -418,8 +418,8 @@ static const VMStateDescription vmstate_ppce500_pci = {
 static void e500_pcihost_bridge_realize(PCIDevice *d, Error **errp)
 {
     PPCE500PCIBridgeState *b = PPC_E500_PCI_BRIDGE(d);
-    PPCE500CCSRState *ccsr = CCSR(container_get(qdev_get_machine(),
-                                  "/e500-ccsr"));
+    PPCE500CCSRState *ccsr = CCSR(
+        object_resolve_path_component(qdev_get_machine(), "e500-ccsr"));
 
     memory_region_init_alias(&b->bar0, OBJECT(ccsr), "e500-pci-bar0", &ccsr->ccsr_space,
                              0, int128_get64(ccsr->ccsr_space.size));
Signature · 1 line
2.45.0
author
#14 lore raw

PATCH 5/12 ui/console: Explicitly create "/backend" container

In reply to Peter Xu

Follow the trend to explicitly create containers, do that for console.c on "/backend" container.

Cc Marc-André Lureau <[email protected]>
---
 ui/console.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
ui/console.c +2 -2
index 5165f17125..36f8c6debb 100644
@@ -1154,14 +1154,14 @@ DisplayState *init_displaystate(void)
 {
     gchar *name;
     QemuConsole *con;
+    Object *backend = container_create(object_get_root(), "backend");
 
     QTAILQ_FOREACH(con, &consoles, next) {
         /* Hook up into the qom tree here (not in object_new()), once
          * all QemuConsoles are created and the order / numbering
          * doesn't change any more */
         name = g_strdup_printf("console[%d]", con->index);
-        object_property_add_child(container_get(object_get_root(), "/backend"),
-                                  name, OBJECT(con));
+        object_property_add_child(backend, name, OBJECT(con));
         g_free(name);
     }
 
Signature · 1 line
2.45.0
author
#35 lore raw

PATCH 10/12 qom: Create system containers explicitly

In reply to Peter Xu

Always explicitly create QEMU system containers upfront.

Root containers will be created when trying to fetch the root object the 1st time. Machine sub-containers will be created only until machine is being initialized.

---
 hw/core/machine.c | 19 ++++++++++++++++---
 qom/object.c      | 16 +++++++++++++++-
 2 files changed, 31 insertions(+), 4 deletions(-)
hw/core/machine.c +16 -3
index a35c4a8fae..a184dbf8f0 100644
@@ -1193,14 +1193,27 @@ static void machine_class_base_init(ObjectClass *oc, void *data)
     }
 }
 
+const char *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++) {
+        container_create(machine, machine_containers[i]);
+    }
+}
+
 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");
-
+    qemu_create_machine_containers(obj);
     ms->dump_guest_core = true;
     ms->mem_merge = (QEMU_MADV_MERGEABLE != QEMU_MADV_INVALID);
     ms->enable_graphics = true;
qom/object.c +15 -1
index 214d6eb4c1..810e6f2bd9 100644
@@ -1734,12 +1734,26 @@ const char *object_property_get_type(Object *obj, const char *name, Error **errp
     return prop->type;
 }
 
+static Object *object_root_initialize(void)
+{
+    Object *root = object_new(TYPE_CONTAINER);
+
+    /*
+     * Create all QEMU system containers.  "machine" and its sub-containers
+     * are only created when machine initializes (qemu_create_machine()).
+     */
+    container_create(root, "chardevs");
+    container_create(root, "objects");
+
+    return root;
+}
+
 Object *object_get_root(void)
 {
     static Object *root;
 
     if (!root) {
-        root = object_new(TYPE_CONTAINER);
+        root = object_root_initialize();
     }
 
     return root;
Signature · 1 line
2.45.0
author
#1 lore raw

PATCH 1/12 qom: Add TYPE_CONTAINER macro

In reply to Peter Xu

Provide a macro for the container type across QEMU source tree, rather than hard code it every time.

---
 include/qom/object.h | 1 +
 hw/arm/stellaris.c   | 2 +-
 qom/container.c      | 4 ++--
 qom/object.c         | 4 ++--
 4 files changed, 6 insertions(+), 5 deletions(-)
include/qom/object.h +1 -0
index 43c135984a..3ba370ce9b 100644
@@ -26,6 +26,7 @@ typedef struct InterfaceClass InterfaceClass;
 typedef struct InterfaceInfo InterfaceInfo;
 
 #define TYPE_OBJECT "object"
+#define TYPE_CONTAINER "container"
 
 typedef struct ObjectProperty ObjectProperty;
 
hw/arm/stellaris.c +1 -1
index 376746251e..6d518b9cde 100644
@@ -1053,7 +1053,7 @@ static void stellaris_init(MachineState *ms, stellaris_board_info *board)
     flash_size = (((board->dc0 & 0xffff) + 1) << 1) * 1024;
     sram_size = ((board->dc0 >> 18) + 1) * 1024;
 
-    soc_container = object_new("container");
+    soc_container = object_new(TYPE_CONTAINER);
     object_property_add_child(OBJECT(ms), "soc", soc_container);
 
     /* Flash programming is done via the SCU, so pretend it is ROM.  */
qom/container.c +2 -2
index 455e8410c6..cfec92a944 100644
@@ -15,7 +15,7 @@
 #include "qemu/module.h"
 
 static const TypeInfo container_info = {
-    .name          = "container",
+    .name          = TYPE_CONTAINER,
     .parent        = TYPE_OBJECT,
 };
 
@@ -37,7 +37,7 @@ Object *container_get(Object *root, const char *path)
     for (i = 1; parts[i] != NULL; i++, obj = child) {
         child = object_resolve_path_component(obj, parts[i]);
         if (!child) {
-            child = object_new("container");
+            child = object_new(TYPE_CONTAINER);
             object_property_add_child(obj, parts[i], child);
             object_unref(child);
         }
qom/object.c +2 -2
index 9edc06d391..214d6eb4c1 100644
@@ -1739,7 +1739,7 @@ Object *object_get_root(void)
     static Object *root;
 
     if (!root) {
-        root = object_new("container");
+        root = object_new(TYPE_CONTAINER);
     }
 
     return root;
@@ -1755,7 +1755,7 @@ Object *object_get_internal_root(void)
     static Object *internal_root;
 
     if (!internal_root) {
-        internal_root = object_new("container");
+        internal_root = object_new(TYPE_CONTAINER);
     }
 
     return internal_root;
Signature · 1 line
2.45.0
author
#48 lore raw

PATCH 12/12 qom: Drop container_get()

In reply to Peter Xu

Now we should be ready to always create containers upfront, meanwhile we have explicit helpers to fetch either:

- Root containers (object_get_container()), or
- Machine containers (machine_get_container()).

Change all rest container_get() users to use the explicit & fast version of container lookup helpers, finally remove container_get().

---
 include/qom/object.h  | 12 ------------
 backends/cryptodev.c  |  4 ++--
 chardev/char.c        |  2 +-
 hw/core/gpio.c        |  3 +--
 hw/core/qdev.c        |  3 +--
 hw/core/sysbus.c      |  4 ++--
 hw/i386/pc.c          |  4 ++--
 qom/container.c       | 23 -----------------------
 qom/object.c          |  2 +-
 scsi/pr-manager.c     |  4 ++--
 system/ioport.c       |  2 +-
 system/memory.c       |  2 +-
 system/qdev-monitor.c |  6 +++---
 system/vl.c           |  3 +--
 ui/dbus-chardev.c     |  2 +-
 15 files changed, 19 insertions(+), 57 deletions(-)
include/qom/object.h +0 -12
index 87b13f9681..c6e7a7fc08 100644
@@ -2031,18 +2031,6 @@ int object_child_foreach(Object *obj, int (*fn)(Object *child, void *opaque),
 int object_child_foreach_recursive(Object *obj,
                                    int (*fn)(Object *child, void *opaque),
                                    void *opaque);
-/**
- * container_get:
- * @root: root of the #path, e.g., object_get_root()
- * @path: path to the container
- *
- * Return a container object whose path is @path.  Create more containers
- * along the path if necessary.
- *
- * Returns: the container object.
- */
-Object *container_get(Object *root, const char *path);
-
 
 /**
  * container_create:
backends/cryptodev.c +2 -2
index d8bd2a1ae6..263de4913b 100644
@@ -97,7 +97,7 @@ static int qmp_query_cryptodev_foreach(Object *obj, void *data)
 QCryptodevInfoList *qmp_query_cryptodev(Error **errp)
 {
     QCryptodevInfoList *list = NULL;
-    Object *objs = container_get(object_get_root(), "/objects");
+    Object *objs = object_get_container("objects");
 
     object_child_foreach(objs, qmp_query_cryptodev_foreach, &list);
 
@@ -557,7 +557,7 @@ static void cryptodev_backend_stats_cb(StatsResultList **result,
     switch (target) {
     case STATS_TARGET_CRYPTODEV:
     {
-        Object *objs = container_get(object_get_root(), "/objects");
+        Object *objs = object_get_container("objects");
         StatsArgs stats_args;
         stats_args.result.stats = result;
         stats_args.names = names;
chardev/char.c +1 -1
index a1722aa076..22fc5f7f76 100644
@@ -48,7 +48,7 @@
 
 Object *get_chardevs_root(void)
 {
-    return container_get(object_get_root(), "/chardevs");
+    return object_get_container("chardevs");
 }
 
 static void chr_be_event(Chardev *s, QEMUChrEvent event)
hw/core/gpio.c +1 -2
index 80d07a6ec9..6e32a8eec6 100644
@@ -121,8 +121,7 @@ void qdev_connect_gpio_out_named(DeviceState *dev, const char *name, int n,
                                      name ? name : "unnamed-gpio-out", n);
     if (input_pin && !OBJECT(input_pin)->parent) {
         /* We need a name for object_property_set_link to work */
-        object_property_add_child(container_get(qdev_get_machine(),
-                                                "/unattached"),
+        object_property_add_child(machine_get_container("unattached"),
                                   "non-qdev-gpio[*]", OBJECT(input_pin));
     }
     object_property_set_link(OBJECT(dev), propname,
hw/core/qdev.c +1 -2
index 6cb4fe4691..8051961fe5 100644
@@ -476,8 +476,7 @@ static void device_set_realized(Object *obj, bool value, Error **errp)
         if (!obj->parent) {
             gchar *name = g_strdup_printf("device[%d]", unattached_count++);
 
-            object_property_add_child(container_get(qdev_get_machine(),
-                                                    "/unattached"),
+            object_property_add_child(machine_get_container("unattached"),
                                       name, obj);
             unattached_parent = true;
             g_free(name);
hw/core/sysbus.c +2 -2
index e64d99c8ed..9355849ff0 100644
@@ -65,9 +65,9 @@ void foreach_dynamic_sysbus_device(FindSysbusDeviceFunc *func, void *opaque)
     };
 
     /* Loop through all sysbus devices that were spawned outside the machine */
-    container = container_get(qdev_get_machine(), "/peripheral");
+    container = machine_get_container("peripheral");
     find_sysbus_device(container, &find);
-    container = container_get(qdev_get_machine(), "/peripheral-anon");
+    container = machine_get_container("peripheral-anon");
     find_sysbus_device(container, &find);
 }
 
hw/i386/pc.c +2 -2
index 317aaca25a..b8ec2506e1 100644
@@ -460,7 +460,7 @@ static int check_fdc(Object *obj, void *opaque)
 }
 
 static const char * const fdc_container_path[] = {
-    "/unattached", "/peripheral", "/peripheral-anon"
+    "unattached", "peripheral", "peripheral-anon"
 };
 
 /*
@@ -474,7 +474,7 @@ static ISADevice *pc_find_fdc0(void)
     CheckFdcState state = { 0 };
 
     for (i = 0; i < ARRAY_SIZE(fdc_container_path); i++) {
-        container = container_get(qdev_get_machine(), fdc_container_path[i]);
+        container = machine_get_container(fdc_container_path[i]);
         object_child_foreach(container, check_fdc, &state);
     }
 
qom/container.c +0 -23
index da657754a4..5e36fb8773 100644
@@ -38,27 +38,4 @@ Object *container_create(Object *obj, const char *name)
     return child;
 }
 
-Object *container_get(Object *root, const char *path)
-{
-    Object *obj, *child;
-    char **parts;
-    int i;
-
-    parts = g_strsplit(path, "/", 0);
-    assert(parts != NULL && parts[0] != NULL && !parts[0][0]);
-    obj = root;
-
-    for (i = 1; parts[i] != NULL; i++, obj = child) {
-        child = object_resolve_path_component(obj, parts[i]);
-        if (!child) {
-            child = container_create(obj, parts[i]);
-        }
-    }
-
-    g_strfreev(parts);
-
-    return obj;
-}
-
-
 type_init(container_register_types)
qom/object.c +1 -1
index cf66803a6a..aeb04c483c 100644
@@ -1771,7 +1771,7 @@ Object *object_get_root(void)
 
 Object *object_get_objects_root(void)
 {
-    return container_get(object_get_root(), "/objects");
+    return object_get_container("objects");
 }
 
 Object *object_get_internal_root(void)
scsi/pr-manager.c +2 -2
index fb5fc29730..1977d99ce0 100644
@@ -21,7 +21,7 @@
 #include "qemu/module.h"
 #include "qapi/qapi-commands-block.h"
 
-#define PR_MANAGER_PATH     "/objects"
+#define PR_MANAGER_PATH     "objects"
 
 typedef struct PRManagerData {
     PRManager *pr_mgr;
@@ -135,7 +135,7 @@ PRManagerInfoList *qmp_query_pr_managers(Error **errp)
 {
     PRManagerInfoList *head = NULL;
     PRManagerInfoList **prev = &head;
-    Object *container = container_get(object_get_root(), PR_MANAGER_PATH);
+    Object *container = object_get_container(PR_MANAGER_PATH);
 
     object_child_foreach(container, query_one_pr_manager, &prev);
     return head;
system/ioport.c +1 -1
index fd551d0375..55c2a75239 100644
@@ -258,7 +258,7 @@ static void portio_list_add_1(PortioList *piolist,
     object_ref(&mrpio->mr);
     object_unparent(OBJECT(&mrpio->mr));
     if (!piolist->owner) {
-        owner = container_get(qdev_get_machine(), "/unattached");
+        owner = machine_get_container("unattached");
     } else {
         owner = piolist->owner;
     }
system/memory.c +1 -1
index 85f6834cb3..fba351b030 100644
@@ -1238,7 +1238,7 @@ static void memory_region_do_init(MemoryRegion *mr,
         char *name_array = g_strdup_printf("%s[*]", escaped_name);
 
         if (!owner) {
-            owner = container_get(qdev_get_machine(), "/unattached");
+            owner = machine_get_container("unattached");
         }
 
         object_property_add_child(owner, name_array, OBJECT(mr));
system/qdev-monitor.c +3 -3
index 4c09b38ffb..4d46af2c8d 100644
@@ -348,7 +348,7 @@ static Object *qdev_get_peripheral(void)
     static Object *dev;
 
     if (dev == NULL) {
-        dev = container_get(qdev_get_machine(), "/peripheral");
+        dev = machine_get_container("peripheral");
     }
 
     return dev;
@@ -359,7 +359,7 @@ static Object *qdev_get_peripheral_anon(void)
     static Object *dev;
 
     if (dev == NULL) {
-        dev = container_get(qdev_get_machine(), "/peripheral-anon");
+        dev = machine_get_container("peripheral-anon");
     }
 
     return dev;
@@ -1085,7 +1085,7 @@ static GSList *qdev_build_hotpluggable_device_list(Object *peripheral)
 static void peripheral_device_del_completion(ReadLineState *rs,
                                              const char *str)
 {
-    Object *peripheral = container_get(qdev_get_machine(), "/peripheral");
+    Object *peripheral = machine_get_container("peripheral");
     GSList *list, *item;
 
     list = qdev_build_hotpluggable_device_list(peripheral);
system/vl.c +1 -2
index 3bb8f2db9a..822f7ff656 100644
@@ -2120,8 +2120,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));
-    object_property_add_child(container_get(OBJECT(current_machine),
-                                            "/unattached"),
+    object_property_add_child(machine_get_container("unattached"),
                               "sysbus", OBJECT(sysbus_get_default()));
 
     if (machine_class->minimum_page_bits) {
ui/dbus-chardev.c +1 -1
index 1d3a7122a1..bf061cbc93 100644
@@ -106,7 +106,7 @@ dbus_chardev_init(DBusDisplay *dpy)
     dpy->notifier.notify = dbus_display_on_notify;
     dbus_display_notifier_add(&dpy->notifier);
 
-    object_child_foreach(container_get(object_get_root(), "/chardevs"),
+    object_child_foreach(object_get_container("chardevs"),
                          dbus_display_chardev_foreach, dpy);
 }
 
Signature · 1 line
2.45.0
author
#28 lore raw

PATCH 8/12 qdev: Make qdev_get_machine() not use container_get()

In reply to Peter Xu

Currently, qdev_get_machine() has a slight misuse on container_get(), as the helper says "get a container" but in reality the goal is to get the machine object. It is still a "container" but not strictly.

Note that it _may_ get a container (at "/machine") in our current unit test of test-qdev-global-props.c before all these changes, but it's probably unexpected and worked by accident.

Switch to an explicit object_resolve_path_component(), with a side benefit that qdev_get_machine() can happen a lot, and we don't need to split the string ("/machine") every time. This also paves way for making the helper container_get() never try to return a non-container at all.

---
 hw/core/qdev.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
hw/core/qdev.c +7 -1
index 5f13111b77..c869c47a27 100644
@@ -817,7 +817,13 @@ Object *qdev_get_machine(void)
     static Object *dev;
 
     if (dev == NULL) {
-        dev = container_get(object_get_root(), "/machine");
+        /*
+         * NOTE: when the machine is not yet created, this helper will
+         * also keep the cached object untouched and return NULL.  The next
+         * invoke of the helper will try to look for the machine again.
+         * It'll only cache the pointer when it's found the first time.
+         */
+        dev = object_resolve_path_component(object_get_root(), "machine");
     }
 
     return dev;
Signature · 1 line
2.45.0
author
#11 lore raw

PATCH 4/12 tests: Explicitly create containers in test_qom_partial_path()

In reply to Peter Xu

Drop one use of container_get(), instead switch to the explicit function to create a container.

---
 tests/unit/check-qom-proplist.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tests/unit/check-qom-proplist.c +1 -1
index 79d4a8b89d..21b1e167ca 100644
@@ -610,7 +610,7 @@ static void test_dummy_delchild(void)
 static void test_qom_partial_path(void)
 {
     Object *root  = object_get_objects_root();
-    Object *cont1 = container_get(root, "/cont1");
+    Object *cont1 = container_create(root, "cont1");
     Object *obj1  = object_new(TYPE_DUMMY);
     Object *obj2a = object_new(TYPE_DUMMY);
     Object *obj2b = object_new(TYPE_DUMMY);
Signature · 1 line
2.45.0
author
#8 lore raw

PATCH 3/12 tests: Fix test-qdev-global-props on anonymous qdev realize()

In reply to Peter Xu

test-qdev-global-props creates a few subprocesses and test things based on qdev realize(). One thing was overlooked since the start, that anonymous creations of qdev (then realize() the device) requires the machine object's presence, as all these devices need to be attached to QOM tree, by default to path "/machine/unattached".

The test didn't crash simply because container_get() has an implicit semantic to silently create any missing container, hence what happened here is container_get() (when running these tests) will try to create containers at QOM path "/machine" on the fly. That's probably unexpected by the test, but worked like charm before.

We're going to fix device_set_realized() soon, but before that make the test case prepared, by creating the machine object on its own.

---
 tests/unit/test-qdev-global-props.c | 21 +++++++++++++++++++++
 1 file changed, 21 insertions(+)
tests/unit/test-qdev-global-props.c +21 -0
index c8862cac5f..b2a1598f8e 100644
@@ -72,6 +72,25 @@ static const TypeInfo subclass_type = {
     .parent = TYPE_STATIC_PROPS,
 };
 
+/*
+ * Initialize a fake machine, being prepared for future tests.
+ *
+ * All the tests later (even if to be run in subprocesses.. which will
+ * inherit the global states of the parent process) will try to create qdev
+ * and realize the device.
+ *
+ * Realization of such anonymous qdev (with no parent object) requires both
+ * the machine object and its "unattached" container to be at least present.
+ */
+static void test_init_machine(void)
+{
+    /* This is a fake machine - it doesn't need to be a machine object */
+    Object *machine = container_create(object_get_root(), "machine");
+
+    /* This container must exist for anonymous qdevs to realize() */
+    container_create(machine, "unattached");
+}
+
 /* Test simple static property setting to default value */
 static void test_static_prop_subprocess(void)
 {
@@ -295,6 +314,8 @@ int main(int argc, char **argv)
     type_register_static(&nohotplug_type);
     type_register_static(&nondevice_type);
 
+    test_init_machine();
+
     g_test_add_func("/qdev/properties/static/default/subprocess",
                     test_static_prop_subprocess);
     g_test_add_func("/qdev/properties/static/default",
Signature · 1 line
2.45.0
author
#4 lore raw

PATCH 2/12 qom: New container_create()

In reply to Peter Xu

To move towards explicit creations of containers, starting that by providing a helper for creating container objects.

---
 include/qom/object.h | 12 ++++++++++++
 qom/container.c      | 18 +++++++++++++++---
 2 files changed, 27 insertions(+), 3 deletions(-)
include/qom/object.h +12 -0
index 3ba370ce9b..41ef53241e 100644
@@ -2033,6 +2033,18 @@ int object_child_foreach_recursive(Object *obj,
  */
 Object *container_get(Object *root, const char *path);
 
+
+/**
+ * container_create:
+ * @root: root of the object to create the new container
+ * @name: name of the new container
+ *
+ * Create a container object under @root with @name.
+ *
+ * Returns: the newly created container object.
+ */
+Object *container_create(Object *root, const char *name);
+
 /**
  * object_property_help:
  * @name: the name of the property
qom/container.c +15 -3
index cfec92a944..da657754a4 100644
@@ -24,6 +24,20 @@ static void container_register_types(void)
     type_register_static(&container_info);
 }
 
+Object *container_create(Object *obj, const char *name)
+{
+    Object *child = object_new(TYPE_CONTAINER);
+
+    object_property_add_child(obj, name, child);
+    /*
+     * Simplify the caller by always drop the refcount directly here, as
+     * containers are normally never destroyed after created anyway.
+     */
+    object_unref(child);
+
+    return child;
+}
+
 Object *container_get(Object *root, const char *path)
 {
     Object *obj, *child;
@@ -37,9 +51,7 @@ Object *container_get(Object *root, const char *path)
     for (i = 1; parts[i] != NULL; i++, obj = child) {
         child = object_resolve_path_component(obj, parts[i]);
         if (!child) {
-            child = object_new(TYPE_CONTAINER);
-            object_property_add_child(obj, parts[i], child);
-            object_unref(child);
+            child = container_create(obj, parts[i]);
         }
     }
 
Signature · 1 line
2.45.0

Peter Xu

author
#0 lore raw

cover QOM: container_get() removal

This series is not for 9.2, but for 10.0. It is intended to replace this previous patchset:

[PATCH 0/5] QOM: Enforce container_get() to operate on containers only
https://lore.kernel.org/r/[email protected]

Since it's a different patchset, the versioning starts from v1.

The series is about container_get() and its removal. Meanwhile after the whole series applied, all the containers will be created explicitly. No lookup code will implicitly create container anymore.

In general, container_get() is a flaky interface in a few things. Firstly, some of the users use it without getting a container object. Secondly, it can implicitly create containers on the fly.

As Dan (mostly) pointed out, we don't have a super complicated container layout. It looks like this:

/objects
/chardevs
/backend (conditional, ui/ only)
/dr-connector (conditional, ppc/ only)
/machine
/peripheral
/peripheral-anon
/unattached

This series create these containers explicitly.

There's a side benefit of dropping container_get(), which is to avoid complicated string operations, as container_get() is never used in more than one depth. It means switching to object_resolve_path_component() should make existing code even tiny little faster.

To achieve this, some test needs to be fixed first. For that, "tests: Fix test-qdev-global-props on anonymous qdev realize()".

Comments welcomed, thanks.

Peter Xu (12):
qom: Add TYPE_CONTAINER macro
qom: New container_create()
tests: Fix test-qdev-global-props on anonymous qdev realize()
tests: Explicitly create containers in test_qom_partial_path() ui/console: Explicitly create "/backend" container hw/ppc: Explicitly create the drc container ppc/e500: Avoid abuse of container_get()
qdev: Make qdev_get_machine() not use container_get()
qdev: Add machine_get_container()
qom: Create system containers explicitly
qom: Add object_get_container()
qom: Drop container_get()

 include/hw/qdev-core.h              | 10 ++++++++
 include/qom/object.h                | 25 +++++++++++++-----
 backends/cryptodev.c                |  4 +--
 chardev/char.c                      |  2 +-
 hw/arm/stellaris.c                  |  2 +-
 hw/core/gpio.c                      |  3 +--
 hw/core/machine.c                   | 19 +++++++++++---
 hw/core/qdev.c                      | 28 +++++++++++++++++---
 hw/core/sysbus.c                    |  4 +--
 hw/i386/pc.c                        |  4 +--
 hw/pci-host/ppce500.c               |  4 +--
 hw/ppc/spapr_drc.c                  | 40 +++++++++++++++++++++--------
 qom/container.c                     | 31 ++++++++--------------
 qom/object.c                        | 30 +++++++++++++++++++---
 scsi/pr-manager.c                   |  4 +--
 system/ioport.c                     |  2 +-
 system/memory.c                     |  2 +-
 system/qdev-monitor.c               |  6 ++---
 system/vl.c                         |  3 +--
 tests/unit/check-qom-proplist.c     |  2 +-
 tests/unit/test-qdev-global-props.c | 21 +++++++++++++++
 ui/console.c                        |  4 +--
 ui/dbus-chardev.c                   |  2 +-
 23 files changed, 180 insertions(+), 72 deletions(-)
Signature · 1 line
2.45.0
author
#18 lore raw

PATCH 6/12 hw/ppc: Explicitly create the drc container

QEMU will start to not rely on implicit creations of containers soon. Make PPC drc devices follow by explicitly create the container whenever a drc device is realized, dropping container_get() calls.

No functional change intended.

Cc Nicholas Piggin <[email protected]>
Cc Daniel Henrique Barboza <[email protected]>
Cc Harsh Prateek Bora <[email protected]>
---
 hw/ppc/spapr_drc.c | 40 ++++++++++++++++++++++++++++++----------
 1 file changed, 30 insertions(+), 10 deletions(-)
hw/ppc/spapr_drc.c +30 -10
index 1484e3209d..3d6ef26b38 100644
@@ -27,7 +27,7 @@
 #include "sysemu/reset.h"
 #include "trace.h"
 
-#define DRC_CONTAINER_PATH "/dr-connector"
+#define DRC_CONTAINER_PATH "dr-connector"
 #define DRC_INDEX_TYPE_SHIFT 28
 #define DRC_INDEX_ID_MASK ((1ULL << DRC_INDEX_TYPE_SHIFT) - 1)
 
@@ -514,6 +514,26 @@ static const VMStateDescription vmstate_spapr_drc = {
     }
 };
 
+static GOnce drc_container_created = G_ONCE_INIT;
+
+static gpointer drc_container_create(gpointer unused G_GNUC_UNUSED)
+{
+    container_create(object_get_root(), DRC_CONTAINER_PATH);
+    return NULL;
+}
+
+static Object *drc_container_get(void)
+{
+    return object_resolve_path_component(
+        object_get_root(), DRC_CONTAINER_PATH);
+}
+
+/* TODO: create the container in an ppc init function */
+static void drc_container_create_once(void)
+{
+    g_once(&drc_container_created, drc_container_create, NULL);
+}
+
 static void drc_realize(DeviceState *d, Error **errp)
 {
     SpaprDrc *drc = SPAPR_DR_CONNECTOR(d);
@@ -521,6 +541,9 @@ static void drc_realize(DeviceState *d, Error **errp)
     Object *root_container;
     const char *child_name;
 
+    /* Whenever a DRC device is realized, create the container */
+    drc_container_create_once();
+
     trace_spapr_drc_realize(spapr_drc_index(drc));
     /* NOTE: we do this as part of realize/unrealize due to the fact
      * that the guest will communicate with the DRC via RTAS calls
@@ -529,7 +552,7 @@ static void drc_realize(DeviceState *d, Error **errp)
      * inaccessible by the guest, since lookups rely on this path
      * existing in the composition tree
      */
-    root_container = container_get(object_get_root(), DRC_CONTAINER_PATH);
+    root_container = drc_container_get();
     child_name = object_get_canonical_path_component(OBJECT(drc));
     trace_spapr_drc_realize_child(spapr_drc_index(drc), child_name);
     object_property_add_alias(root_container, link_name,
@@ -543,12 +566,10 @@ static void drc_unrealize(DeviceState *d)
 {
     SpaprDrc *drc = SPAPR_DR_CONNECTOR(d);
     g_autofree gchar *name = g_strdup_printf("%x", spapr_drc_index(drc));
-    Object *root_container;
 
     trace_spapr_drc_unrealize(spapr_drc_index(drc));
     vmstate_unregister(VMSTATE_IF(drc), &vmstate_spapr_drc, drc);
-    root_container = container_get(object_get_root(), DRC_CONTAINER_PATH);
-    object_property_del(root_container, name);
+    object_property_del(drc_container_get(), name);
 }
 
 SpaprDrc *spapr_dr_connector_new(Object *owner, const char *type,
@@ -796,9 +817,8 @@ static const TypeInfo spapr_drc_pmem_info = {
 SpaprDrc *spapr_drc_by_index(uint32_t index)
 {
     Object *obj;
-    g_autofree gchar *name = g_strdup_printf("%s/%x", DRC_CONTAINER_PATH,
-                                             index);
-    obj = object_resolve_path(name, NULL);
+    g_autofree gchar *name = g_strdup_printf("%x", index);
+    obj = object_resolve_path_component(drc_container_get(), name);
 
     return !obj ? NULL : SPAPR_DR_CONNECTOR(obj);
 }
@@ -860,7 +880,7 @@ int spapr_dt_drc(void *fdt, int offset, Object *owner, uint32_t drc_type_mask)
     /* aliases for all DRConnector objects will be rooted in QOM
      * composition tree at DRC_CONTAINER_PATH
      */
-    root_container = container_get(object_get_root(), DRC_CONTAINER_PATH);
+    root_container = drc_container_get();
 
     object_property_iter_init(&iter, root_container);
     while ((prop = object_property_iter_next(&iter))) {
@@ -953,7 +973,7 @@ void spapr_drc_reset_all(SpaprMachineState *spapr)
     ObjectProperty *prop;
     ObjectPropertyIterator iter;
 
-    drc_container = container_get(object_get_root(), DRC_CONTAINER_PATH);
+    drc_container = drc_container_get();
 restart:
     object_property_iter_init(&iter, drc_container);
     while ((prop = object_property_iter_next(&iter))) {
Signature · 1 line
2.45.0
author
#31 lore raw

PATCH 9/12 qdev: Add machine_get_container()

In reply to Peter Xu

Add a helper to fetch machine containers. Add some sanity check around.

---
 include/hw/qdev-core.h | 10 ++++++++++
 hw/core/qdev.c         | 17 +++++++++++++++++
 2 files changed, 27 insertions(+)
include/hw/qdev-core.h +10 -0
index 5be9844412..38edfb1b54 100644
@@ -996,6 +996,16 @@ const char *qdev_fw_name(DeviceState *dev);
 void qdev_assert_realized_properly(void);
 Object *qdev_get_machine(void);
 
+/**
+ * machine_get_container:
+ * @name: The name of container to lookup
+ *
+ * Get a container of the machine (QOM path "/machine/XXX").
+ *
+ * Returns: the machine container object.
+ */
+Object *machine_get_container(const char *name);
+
 /**
  * qdev_get_human_name() - Return a human-readable name for a device
  * @dev: The device. Must be a valid and non-NULL pointer.
hw/core/qdev.c +17 -0
index c869c47a27..6cb4fe4691 100644
@@ -829,6 +829,23 @@ Object *qdev_get_machine(void)
     return dev;
 }
 
+Object *machine_get_container(const char *name)
+{
+    Object *container, *machine;
+
+    /*
+     * NOTE: nobody should call this _before_ machine is created.  If it
+     * happens, it's a programming error.
+     */
+    machine = qdev_get_machine();
+    assert(machine);
+
+    container = object_resolve_path_component(machine, name);
+    assert(object_dynamic_cast(container, TYPE_CONTAINER));
+
+    return container;
+}
+
 char *qdev_get_human_name(DeviceState *dev)
 {
     g_assert(dev != NULL);
Signature · 1 line
2.45.0
author
#45 lore raw

PATCH 11/12 qom: Add object_get_container()

In reply to Peter Xu

Add a helper to fetch a root container (under object_get_root()). Sanity check on the type of the object.

---
 include/qom/object.h | 10 ++++++++++
 qom/object.c         | 10 ++++++++++
 2 files changed, 20 insertions(+)
include/qom/object.h +10 -0
index 41ef53241e..87b13f9681 100644
@@ -1524,6 +1524,16 @@ const char *object_property_get_type(Object *obj, const char *name,
  */
 Object *object_get_root(void);
 
+/**
+ * object_get_container:
+ * @name: the name of container to lookup
+ *
+ * Lookup a root level container.
+ *
+ * Returns: the container with @name.
+ */
+Object *object_get_container(const char *name);
+
 
 /**
  * object_get_objects_root:
qom/object.c +10 -0
index 810e6f2bd9..cf66803a6a 100644
@@ -1748,6 +1748,16 @@ static Object *object_root_initialize(void)
     return root;
 }
 
+Object *object_get_container(const char *name)
+{
+    Object *container;
+
+    container = object_resolve_path_component(object_get_root(), name);
+    assert(object_dynamic_cast(container, TYPE_CONTAINER));
+
+    return container;
+}
+
 Object *object_get_root(void)
 {
     static Object *root;
Signature · 1 line
2.45.0
Reviewed-by on v1 10/12
#36 lore raw

Re: [PATCH 10/12] qom: Create system containers explicitly

In reply to Peter Xu
Quoted 17 lines · Peter Xu

Always explicitly create QEMU system containers upfront.

Root containers will be created when trying to fetch the root object the 1st time. Machine sub-containers will be created only until machine is being initialized.

---
  hw/core/machine.c | 19 ++++++++++++++++---
  qom/object.c      | 16 +++++++++++++++-
  2 files changed, 31 insertions(+), 4 deletions(-)
hw/core/machine.c +0 -0
index a35c4a8fae..a184dbf8f0 100644

hw/core/machine.c static void machine_class_base_init(ObjectClass *oc, void *data) · line 1196

Quoted 4 lines
hw/core/machine.c +1 -0
@@ -1193,14 +1193,27 @@ static void machine_class_base_init(ObjectClass *oc, void *data)
      }
  }
 
+const char *machine_containers[] = {
   const char *const machine_containers[] = {

hw/core/machine.c static void machine_class_base_init(ObjectClass *oc, void *data) · line 1200

Quoted 4 lines
hw/core/machine.c +4 -0
+    "unattached",
+    "peripheral",
+    "peripheral-anon"
+};
#50 lore raw
In reply to Peter Xu
Quoted 5 lines · Peter Xu

Peter Xu (12):
qom: Add TYPE_CONTAINER macro
qom: New container_create()
tests: Fix test-qdev-global-props on anonymous qdev realize()
tests: Explicitly create containers in test_qom_partial_path()

Quoted 8 lines

ui/console: Explicitly create "/backend" container hw/ppc: Explicitly create the drc container ppc/e500: Avoid abuse of container_get()
qdev: Make qdev_get_machine() not use container_get()
qdev: Add machine_get_container()
qom: Create system containers explicitly
qom: Add object_get_container()
qom: Drop container_get()

Maybe split the last patch in 3, reordering as:

- qom: Create system containers explicitly
- qdev: Make qdev_get_machine() not use container_get()
- qdev: Add machine_get_container()
- qdev: Use machine_get_container()
(part 1 of 'qom: Drop container_get()')
- qom: Add object_get_container()
- qom: Use object_get_container()
(part 2 of 'qom: Drop container_get()')
- qom: Drop container_get()
(part 3 of 'qom: Drop container_get()')

Reviewed-by on v1 5/12
#15 lore raw

Re: [PATCH 05/12] ui/console: Explicitly create "/backend" container

In reply to Peter Xu
Quoted 8 lines · Peter Xu

Follow the trend to explicitly create containers, do that for console.c on "/backend" container.

Cc Marc-André Lureau <[email protected]>
---
  ui/console.c | 4 ++--
  1 file changed, 2 insertions(+), 2 deletions(-)
Reviewed-by on v1 3/12
#9 lore raw

Re: [PATCH 03/12] tests: Fix test-qdev-global-props on anonymous qdev realize()

In reply to Peter Xu
Quoted 12 lines · Peter Xu

test-qdev-global-props creates a few subprocesses and test things based on qdev realize(). One thing was overlooked since the start, that anonymous creations of qdev (then realize() the device) requires the machine object's presence, as all these devices need to be attached to QOM tree, by default to path "/machine/unattached".

The test didn't crash simply because container_get() has an implicit semantic to silently create any missing container, hence what happened here is container_get() (when running these tests) will try to create containers at QOM path "/machine" on the fly. That's probably unexpected by the test, but worked like charm before.

Quoted 7 lines

We're going to fix device_set_realized() soon, but before that make the test case prepared, by creating the machine object on its own.

---
  tests/unit/test-qdev-global-props.c | 21 +++++++++++++++++++++
  1 file changed, 21 insertions(+)
Reviewed-by on v1 1/12
#2 lore raw

Re: [PATCH 01/12] qom: Add TYPE_CONTAINER macro

In reply to Peter Xu
Quoted 10 lines · Peter Xu

Provide a macro for the container type across QEMU source tree, rather than hard code it every time.

---
  include/qom/object.h | 1 +
  hw/arm/stellaris.c   | 2 +-
  qom/container.c      | 4 ++--
  qom/object.c         | 4 ++--
  4 files changed, 6 insertions(+), 5 deletions(-)
#19 lore raw

Re: [PATCH 06/12] hw/ppc: Explicitly create the drc container

In reply to Peter Xu

Hi Peter,

Quoted 40 lines · Peter Xu

QEMU will start to not rely on implicit creations of containers soon. Make PPC drc devices follow by explicitly create the container whenever a drc device is realized, dropping container_get() calls.

No functional change intended.

Cc Nicholas Piggin <[email protected]>
Cc Daniel Henrique Barboza <[email protected]>
Cc Harsh Prateek Bora <[email protected]>
---
  hw/ppc/spapr_drc.c | 40 ++++++++++++++++++++++++++++++----------
  1 file changed, 30 insertions(+), 10 deletions(-)
hw/ppc/spapr_drc.c +20 -0
+static GOnce drc_container_created = G_ONCE_INIT;
+
+static gpointer drc_container_create(gpointer unused G_GNUC_UNUSED)
+{
+    container_create(object_get_root(), DRC_CONTAINER_PATH);
+    return NULL;
+}
+
+static Object *drc_container_get(void)
+{
+    return object_resolve_path_component(
+        object_get_root(), DRC_CONTAINER_PATH);
+}
+
+/* TODO: create the container in an ppc init function */
+static void drc_container_create_once(void)
+{
+    g_once(&drc_container_created, drc_container_create, NULL);
+}
+
  static void drc_realize(DeviceState *d, Error **errp)
  {
      SpaprDrc *drc = SPAPR_DR_CONNECTOR(d);

hw/ppc/spapr_drc.c static void drc_realize(DeviceState *d, Error **errp) · line 545

Quoted 5 lines
hw/ppc/spapr_drc.c +2 -0
@@ -521,6 +541,9 @@ static void drc_realize(DeviceState *d, Error **errp)
      Object *root_container;
      const char *child_name;
 
+    /* Whenever a DRC device is realized, create the container */
+    drc_container_create_once();

Can't we just create it once in spapr_dr_connector_class_init(), removing the G_ONCE_INIT need?

Quoted 12 lines
hw/ppc/spapr_drc.c +1 -1
      trace_spapr_drc_realize(spapr_drc_index(drc));
      /* NOTE: we do this as part of realize/unrealize due to the fact
       * that the guest will communicate with the DRC via RTAS calls
@@ -529,7 +552,7 @@ static void drc_realize(DeviceState *d, Error **errp)
       * inaccessible by the guest, since lookups rely on this path
       * existing in the composition tree
       */
-    root_container = container_get(object_get_root(), DRC_CONTAINER_PATH);
+    root_container = drc_container_get();
      child_name = object_get_canonical_path_component(OBJECT(drc));
      trace_spapr_drc_realize_child(spapr_drc_index(drc), child_name);
      object_property_add_alias(root_container, link_name,
#23 lore raw

Re: [PATCH 07/12] ppc/e500: Avoid abuse of container_get()

In reply to Peter Xu
Quoted 17 lines · Peter Xu

container_get() is going to become strict on not allowing to return a non-container.

Switch the e500 user to use object_resolve_path_component() explicitly.

---
  hw/pci-host/ppce500.c | 4 ++--
  1 file changed, 2 insertions(+), 2 deletions(-)
hw/pci-host/ppce500.c +0 -0
index b70631045a..65233b9e3f 100644

hw/pci-host/ppce500.c static const VMStateDescription vmstate_ppce500_pci = { · line 422

Quoted 7 lines
hw/pci-host/ppce500.c +2 -2
@@ -418,8 +418,8 @@ static const VMStateDescription vmstate_ppce500_pci = {
  static void e500_pcihost_bridge_realize(PCIDevice *d, Error **errp)
  {
      PPCE500PCIBridgeState *b = PPC_E500_PCI_BRIDGE(d);
-    PPCE500CCSRState *ccsr = CCSR(container_get(qdev_get_machine(),
-                                  "/e500-ccsr"));
+    PPCE500CCSRState *ccsr = CCSR(
+        object_resolve_path_component(qdev_get_machine(), "e500-ccsr"));

why not simply use :

      CCSR(object_resolve_path("/machine/e500-ccsr", NULL));

Thanks,

C.

Quoted 3 lines
hw/pci-host/ppce500.c +0 -0
      memory_region_init_alias(&b->bar0, OBJECT(ccsr), "e500-pci-bar0", &ccsr->ccsr_space,
                               0, int128_get64(ccsr->ccsr_space.size));
Reviewed-by
#24 lore raw

Re: [PATCH 07/12] ppc/e500: Avoid abuse of container_get()

Quoted 29 lines · Cédric Le Goater
Quoted 17 lines · Peter Xu

container_get() is going to become strict on not allowing to return a non-container.

Switch the e500 user to use object_resolve_path_component() explicitly.

---
  hw/pci-host/ppce500.c | 4 ++--

  1 file changed, 2 insertions(+), 2 deletions(-)
hw/pci-host/ppce500.c +0 -0
index b70631045a..65233b9e3f 100644

hw/pci-host/ppce500.c static const VMStateDescription vmstate_ppce500_pci = { · line 422

Quoted 7 lines
hw/pci-host/ppce500.c +2 -2
@@ -418,8 +418,8 @@ static const VMStateDescription vmstate_ppce500_pci = {
   static void e500_pcihost_bridge_realize(PCIDevice *d, Error **errp)
   {
       PPCE500PCIBridgeState *b = PPC_E500_PCI_BRIDGE(d);
-    PPCE500CCSRState *ccsr = CCSR(container_get(qdev_get_machine(),
-                                  "/e500-ccsr"));
+    PPCE500CCSRState *ccsr = CCSR(
+        object_resolve_path_component(qdev_get_machine(), "e500-ccsr"));

why not simply use :

Quoted 1 line

      CCSR(object_resolve_path("/machine/e500-ccsr", NULL));

I guess we want to avoid the absolute paths. If so,

We might want to convert these lookups to object_resolve_path_component too, not in this patchset.

hw/i386/acpi-build.c:    host = PCI_HOST_BRIDGE(object_resolve_path("/machine/i440fx", NULL));
hw/i386/acpi-build.c:        host = PCI_HOST_BRIDGE(object_resolve_path("/machine/q35", NULL));
target/i386/kvm/kvm.c:        (MemoryRegion *) object_resolve_path("/machine/smram", NULL);
target/i386/tcg/sysemu/tcg-cpu.c:        (MemoryRegion *) object_resolve_path("/machine/smram", NULL);

Thanks,

C.

Reviewed-by on v1 1/12
#3 lore raw

Re: [PATCH 01/12] qom: Add TYPE_CONTAINER macro

In reply to Peter Xu
Quoted 10 lines · Peter Xu

Provide a macro for the container type across QEMU source tree, rather than hard code it every time.

---
 include/qom/object.h | 1 +
 hw/arm/stellaris.c   | 2 +-
 qom/container.c      | 4 ++--
 qom/object.c         | 4 ++--
 4 files changed, 6 insertions(+), 5 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 :|
Reviewed-by on v1 2/12
#5 lore raw

Re: [PATCH 02/12] qom: New container_create()

In reply to Peter Xu
Quoted 8 lines · Peter Xu

To move towards explicit creations of containers, starting that by providing a helper for creating container objects.

---
 include/qom/object.h | 12 ++++++++++++
 qom/container.c      | 18 +++++++++++++++---
 2 files changed, 27 insertions(+), 3 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 :|
Reviewed-by on v1 3/12
#10 lore raw

Re: [PATCH 03/12] tests: Fix test-qdev-global-props on anonymous qdev realize()

In reply to Peter Xu
Quoted 12 lines · Peter Xu

test-qdev-global-props creates a few subprocesses and test things based on qdev realize(). One thing was overlooked since the start, that anonymous creations of qdev (then realize() the device) requires the machine object's presence, as all these devices need to be attached to QOM tree, by default to path "/machine/unattached".

The test didn't crash simply because container_get() has an implicit semantic to silently create any missing container, hence what happened here is container_get() (when running these tests) will try to create containers at QOM path "/machine" on the fly. That's probably unexpected by the test, but worked like charm before.

Quoted 7 lines

We're going to fix device_set_realized() soon, but before that make the test case prepared, by creating the machine object on its own.

---
 tests/unit/test-qdev-global-props.c | 21 +++++++++++++++++++++
 1 file changed, 21 insertions(+)

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 :|
Reviewed-by on v1 4/12
#13 lore raw

Re: [PATCH 04/12] tests: Explicitly create containers in test_qom_partial_path()

In reply to Peter Xu
Quoted 7 lines · Peter Xu

Drop one use of container_get(), instead switch to the explicit function to create a container.

---
 tests/unit/check-qom-proplist.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

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 :|
#29 lore raw

Re: [PATCH 08/12] qdev: Make qdev_get_machine() not use container_get()

In reply to Peter Xu
Quoted 23 lines · Peter Xu

Currently, qdev_get_machine() has a slight misuse on container_get(), as the helper says "get a container" but in reality the goal is to get the machine object. It is still a "container" but not strictly.

Note that it _may_ get a container (at "/machine") in our current unit test of test-qdev-global-props.c before all these changes, but it's probably unexpected and worked by accident.

Switch to an explicit object_resolve_path_component(), with a side benefit that qdev_get_machine() can happen a lot, and we don't need to split the string ("/machine") every time. This also paves way for making the helper container_get() never try to return a non-container at all.

---
 hw/core/qdev.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
hw/core/qdev.c +0 -0
index 5f13111b77..c869c47a27 100644

hw/core/qdev.c Object *qdev_get_machine(void) · line 825

Quoted 10 lines
hw/core/qdev.c +6 -1
@@ -817,7 +817,13 @@ Object *qdev_get_machine(void)
     static Object *dev;
 
     if (dev == NULL) {
-        dev = container_get(object_get_root(), "/machine");
+        /*
+         * NOTE: when the machine is not yet created, this helper will
+         * also keep the cached object untouched and return NULL.  The next
+         * invoke of the helper will try to look for the machine again.
+         * It'll only cache the pointer when it's found the first time.
+         */

This smells like a recipe for subtle bugs. I think I'd consider it a code flaw if something called qdev_get_machine() in a scenario where the machine does not yet exist.

hw/core/qdev.c Object *qdev_get_machine(void) · line 827

Quoted 2 lines
hw/core/qdev.c +1 -0
+        dev = object_resolve_path_component(object_get_root(), "machine");
     }

IOW, I think we should assert that dev != NULL to ensure we immediately diagnose the flawed sequence of calls.

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 :|
#33 lore raw

Re: [PATCH 09/12] qdev: Add machine_get_container()

In reply to Peter Xu
Quoted 37 lines · Peter Xu

Add a helper to fetch machine containers. Add some sanity check around.

---
 include/hw/qdev-core.h | 10 ++++++++++
 hw/core/qdev.c         | 17 +++++++++++++++++
 2 files changed, 27 insertions(+)
include/hw/qdev-core.h +10 -0
index 5be9844412..38edfb1b54 100644
@@ -996,6 +996,16 @@ const char *qdev_fw_name(DeviceState *dev);
 void qdev_assert_realized_properly(void);
 Object *qdev_get_machine(void);
 
+/**
+ * machine_get_container:
+ * @name: The name of container to lookup
+ *
+ * Get a container of the machine (QOM path "/machine/XXX").
+ *
+ * Returns: the machine container object.
+ */
+Object *machine_get_container(const char *name);
+
 /**
  * qdev_get_human_name() - Return a human-readable name for a device
  * @dev: The device. Must be a valid and non-NULL pointer.
hw/core/qdev.c +0 -0
index c869c47a27..6cb4fe4691 100644
@@ -829,6 +829,23 @@ Object *qdev_get_machine(void)
     return dev;
 }
 

hw/core/qdev.c Object *qdev_get_machine(void) · line 841

Quoted 10 lines
hw/core/qdev.c +10 -0
+Object *machine_get_container(const char *name)
+{
+    Object *container, *machine;
+
+    /*
+     * NOTE: nobody should call this _before_ machine is created.  If it
+     * happens, it's a programming error.
+     */
+    machine = qdev_get_machine();
+    assert(machine);

IMHO this assert should be in qdev_get_machine() itself any premature call of it is a programming error.

Quoted 13 lines
hw/core/qdev.c +7 -0
+
+    container = object_resolve_path_component(machine, name);
+    assert(object_dynamic_cast(container, TYPE_CONTAINER));
+
+    return container;
+}
+
 char *qdev_get_human_name(DeviceState *dev)
 {
     g_assert(dev != NULL);
Signature · 1 line
2.45.0

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 :|
#16 lore raw

Re: [PATCH 05/12] ui/console: Explicitly create "/backend" container

In reply to Peter Xu
Quoted 14 lines · Peter Xu

Follow the trend to explicitly create containers, do that for console.c on "/backend" container.

Cc Marc-André Lureau <[email protected]>
---
 ui/console.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
ui/console.c +0 -0
index 5165f17125..36f8c6debb 100644

ui/console.c DisplayState *init_displaystate(void) · line 1157

Quoted 4 lines
ui/console.c +1 -0
@@ -1154,14 +1154,14 @@ DisplayState *init_displaystate(void)
 {
     gchar *name;
     QemuConsole *con;
+    Object *backend = container_create(object_get_root(), "backend");

What's the rationale for keeping this in the console code ?

I'd consider this to be a similar situation to '/chardevs' and '/objects', and be a common container rather than UI specific. IOW, be created by your later patch.

ui/console.c DisplayState *init_displaystate(void) · line 1166

Quoted 11 lines
ui/console.c +1 -2
     QTAILQ_FOREACH(con, &consoles, next) {
         /* Hook up into the qom tree here (not in object_new()), once
          * all QemuConsoles are created and the order / numbering
          * doesn't change any more */
         name = g_strdup_printf("console[%d]", con->index);
-        object_property_add_child(container_get(object_get_root(), "/backend"),
-                                  name, OBJECT(con));
+        object_property_add_child(backend, name, OBJECT(con));
         g_free(name);
     }

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 :|
Reviewed-by on v1 7/12
#27 lore raw

Re: [PATCH 07/12] ppc/e500: Avoid abuse of container_get()

In reply to Peter Xu
Quoted 11 lines · Peter Xu

container_get() is going to become strict on not allowing to return a non-container.

Switch the e500 user to use object_resolve_path_component() explicitly.

---
 hw/pci-host/ppce500.c | 4 ++--
 1 file changed, 2 insertions(+), 2 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 :|
#37 lore raw

Re: [PATCH 10/12] qom: Create system containers explicitly

In reply to Peter Xu
Quoted 21 lines · Peter Xu

Always explicitly create QEMU system containers upfront.

Root containers will be created when trying to fetch the root object the 1st time. Machine sub-containers will be created only until machine is being initialized.

---
 hw/core/machine.c | 19 ++++++++++++++++---
 qom/object.c      | 16 +++++++++++++++-
 2 files changed, 31 insertions(+), 4 deletions(-)
qom/object.c +0 -0
index 214d6eb4c1..810e6f2bd9 100644
@@ -1734,12 +1734,26 @@ const char *object_property_get_type(Object *obj, const char *name, Error **errp
     return prop->type;
 }
 

qom/object.c const char *object_property_get_type(Object *obj, const char *name, Error **errp · line 1746

Quoted 10 lines
qom/object.c +10 -0
+static Object *object_root_initialize(void)
+{
+    Object *root = object_new(TYPE_CONTAINER);
+
+    /*
+     * Create all QEMU system containers.  "machine" and its sub-containers
+     * are only created when machine initializes (qemu_create_machine()).
+     */
+    container_create(root, "chardevs");
+    container_create(root, "objects");

This is where I would expect 'backend' to have been created rather than ui/console.c, though you could potentially make a case to create it from the machine function, snice console stuff can't be used outside of the machine context, while chardevs/objects can be used in qemu-img/qemu-nbd, etc

qom/object.c const char *object_property_get_type(Object *obj, const char *name, Error **errp · line 1749

Quoted 3 lines
qom/object.c +3 -0
+
+    return root;
+}

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 :|
Reviewed-by on v1 11/12
#47 lore raw

Re: [PATCH 11/12] qom: Add object_get_container()

In reply to Peter Xu
Quoted 8 lines · Peter Xu

Add a helper to fetch a root container (under object_get_root()). Sanity check on the type of the object.

---
 include/qom/object.h | 10 ++++++++++
 qom/object.c         | 10 ++++++++++
 2 files changed, 20 insertions(+)

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 :|
Reviewed-by on v1 12/12
#49 lore raw

Re: [PATCH 12/12] qom: Drop container_get()

In reply to Peter Xu

Subject says drop container_get, actual commit does alot more.

Suggest splitting the conversion to (object/machine)_get_container, from the dropping of container_get.

Quoted 21 lines · Peter Xu

Now we should be ready to always create containers upfront, meanwhile we have explicit helpers to fetch either:

- Root containers (object_get_container()), or
- Machine containers (machine_get_container()).

Change all rest container_get() users to use the explicit & fast version of container lookup helpers, finally remove container_get().

---
 include/qom/object.h  | 12 ------------
 backends/cryptodev.c  |  4 ++--
 chardev/char.c        |  2 +-
 hw/core/gpio.c        |  3 +--
 hw/core/qdev.c        |  3 +--
 hw/core/sysbus.c      |  4 ++--
 hw/i386/pc.c          |  4 ++--
 qom/container.c       | 23 -----------------------
Quoted 8 lines
 qom/object.c          |  2 +-
 scsi/pr-manager.c     |  4 ++--
 system/ioport.c       |  2 +-
 system/memory.c       |  2 +-
 system/qdev-monitor.c |  6 +++---
 system/vl.c           |  3 +--
 ui/dbus-chardev.c     |  2 +-
 15 files changed, 19 insertions(+), 57 deletions(-)
Quoted 279 lines
include/qom/object.h +0 -12
index 87b13f9681..c6e7a7fc08 100644
@@ -2031,18 +2031,6 @@ int object_child_foreach(Object *obj, int (*fn)(Object *child, void *opaque),
 int object_child_foreach_recursive(Object *obj,
                                    int (*fn)(Object *child, void *opaque),
                                    void *opaque);
-/**
- * container_get:
- * @root: root of the #path, e.g., object_get_root()
- * @path: path to the container
- *
- * Return a container object whose path is @path.  Create more containers
- * along the path if necessary.
- *
- * Returns: the container object.
- */
-Object *container_get(Object *root, const char *path);
-
 
 /**
  * container_create:
backends/cryptodev.c +2 -2
index d8bd2a1ae6..263de4913b 100644
@@ -97,7 +97,7 @@ static int qmp_query_cryptodev_foreach(Object *obj, void *data)
 QCryptodevInfoList *qmp_query_cryptodev(Error **errp)
 {
     QCryptodevInfoList *list = NULL;
-    Object *objs = container_get(object_get_root(), "/objects");
+    Object *objs = object_get_container("objects");
 
     object_child_foreach(objs, qmp_query_cryptodev_foreach, &list);
 
@@ -557,7 +557,7 @@ static void cryptodev_backend_stats_cb(StatsResultList **result,
     switch (target) {
     case STATS_TARGET_CRYPTODEV:
     {
-        Object *objs = container_get(object_get_root(), "/objects");
+        Object *objs = object_get_container("objects");
         StatsArgs stats_args;
         stats_args.result.stats = result;
         stats_args.names = names;
chardev/char.c +1 -1
index a1722aa076..22fc5f7f76 100644
@@ -48,7 +48,7 @@
 
 Object *get_chardevs_root(void)
 {
-    return container_get(object_get_root(), "/chardevs");
+    return object_get_container("chardevs");
 }
 
 static void chr_be_event(Chardev *s, QEMUChrEvent event)
hw/core/gpio.c +1 -2
index 80d07a6ec9..6e32a8eec6 100644
@@ -121,8 +121,7 @@ void qdev_connect_gpio_out_named(DeviceState *dev, const char *name, int n,
                                      name ? name : "unnamed-gpio-out", n);
     if (input_pin && !OBJECT(input_pin)->parent) {
         /* We need a name for object_property_set_link to work */
-        object_property_add_child(container_get(qdev_get_machine(),
-                                                "/unattached"),
+        object_property_add_child(machine_get_container("unattached"),
                                   "non-qdev-gpio[*]", OBJECT(input_pin));
     }
     object_property_set_link(OBJECT(dev), propname,
hw/core/qdev.c +1 -2
index 6cb4fe4691..8051961fe5 100644
@@ -476,8 +476,7 @@ static void device_set_realized(Object *obj, bool value, Error **errp)
         if (!obj->parent) {
             gchar *name = g_strdup_printf("device[%d]", unattached_count++);
 
-            object_property_add_child(container_get(qdev_get_machine(),
-                                                    "/unattached"),
+            object_property_add_child(machine_get_container("unattached"),
                                       name, obj);
             unattached_parent = true;
             g_free(name);
hw/core/sysbus.c +2 -2
index e64d99c8ed..9355849ff0 100644
@@ -65,9 +65,9 @@ void foreach_dynamic_sysbus_device(FindSysbusDeviceFunc *func, void *opaque)
     };
 
     /* Loop through all sysbus devices that were spawned outside the machine */
-    container = container_get(qdev_get_machine(), "/peripheral");
+    container = machine_get_container("peripheral");
     find_sysbus_device(container, &find);
-    container = container_get(qdev_get_machine(), "/peripheral-anon");
+    container = machine_get_container("peripheral-anon");
     find_sysbus_device(container, &find);
 }
 
hw/i386/pc.c +2 -2
index 317aaca25a..b8ec2506e1 100644
@@ -460,7 +460,7 @@ static int check_fdc(Object *obj, void *opaque)
 }
 
 static const char * const fdc_container_path[] = {
-    "/unattached", "/peripheral", "/peripheral-anon"
+    "unattached", "peripheral", "peripheral-anon"
 };
 
 /*
@@ -474,7 +474,7 @@ static ISADevice *pc_find_fdc0(void)
     CheckFdcState state = { 0 };
 
     for (i = 0; i < ARRAY_SIZE(fdc_container_path); i++) {
-        container = container_get(qdev_get_machine(), fdc_container_path[i]);
+        container = machine_get_container(fdc_container_path[i]);
         object_child_foreach(container, check_fdc, &state);
     }
 
qom/container.c +0 -23
index da657754a4..5e36fb8773 100644
@@ -38,27 +38,4 @@ Object *container_create(Object *obj, const char *name)
     return child;
 }
 
-Object *container_get(Object *root, const char *path)
-{
-    Object *obj, *child;
-    char **parts;
-    int i;
-
-    parts = g_strsplit(path, "/", 0);
-    assert(parts != NULL && parts[0] != NULL && !parts[0][0]);
-    obj = root;
-
-    for (i = 1; parts[i] != NULL; i++, obj = child) {
-        child = object_resolve_path_component(obj, parts[i]);
-        if (!child) {
-            child = container_create(obj, parts[i]);
-        }
-    }
-
-    g_strfreev(parts);
-
-    return obj;
-}
-
-
 type_init(container_register_types)
qom/object.c +1 -1
index cf66803a6a..aeb04c483c 100644
@@ -1771,7 +1771,7 @@ Object *object_get_root(void)
 
 Object *object_get_objects_root(void)
 {
-    return container_get(object_get_root(), "/objects");
+    return object_get_container("objects");
 }
 
 Object *object_get_internal_root(void)
scsi/pr-manager.c +2 -2
index fb5fc29730..1977d99ce0 100644
@@ -21,7 +21,7 @@
 #include "qemu/module.h"
 #include "qapi/qapi-commands-block.h"
 
-#define PR_MANAGER_PATH     "/objects"
+#define PR_MANAGER_PATH     "objects"
 
 typedef struct PRManagerData {
     PRManager *pr_mgr;
@@ -135,7 +135,7 @@ PRManagerInfoList *qmp_query_pr_managers(Error **errp)
 {
     PRManagerInfoList *head = NULL;
     PRManagerInfoList **prev = &head;
-    Object *container = container_get(object_get_root(), PR_MANAGER_PATH);
+    Object *container = object_get_container(PR_MANAGER_PATH);
 
     object_child_foreach(container, query_one_pr_manager, &prev);
     return head;
system/ioport.c +1 -1
index fd551d0375..55c2a75239 100644
@@ -258,7 +258,7 @@ static void portio_list_add_1(PortioList *piolist,
     object_ref(&mrpio->mr);
     object_unparent(OBJECT(&mrpio->mr));
     if (!piolist->owner) {
-        owner = container_get(qdev_get_machine(), "/unattached");
+        owner = machine_get_container("unattached");
     } else {
         owner = piolist->owner;
     }
system/memory.c +1 -1
index 85f6834cb3..fba351b030 100644
@@ -1238,7 +1238,7 @@ static void memory_region_do_init(MemoryRegion *mr,
         char *name_array = g_strdup_printf("%s[*]", escaped_name);
 
         if (!owner) {
-            owner = container_get(qdev_get_machine(), "/unattached");
+            owner = machine_get_container("unattached");
         }
 
         object_property_add_child(owner, name_array, OBJECT(mr));
system/qdev-monitor.c +3 -3
index 4c09b38ffb..4d46af2c8d 100644
@@ -348,7 +348,7 @@ static Object *qdev_get_peripheral(void)
     static Object *dev;
 
     if (dev == NULL) {
-        dev = container_get(qdev_get_machine(), "/peripheral");
+        dev = machine_get_container("peripheral");
     }
 
     return dev;
@@ -359,7 +359,7 @@ static Object *qdev_get_peripheral_anon(void)
     static Object *dev;
 
     if (dev == NULL) {
-        dev = container_get(qdev_get_machine(), "/peripheral-anon");
+        dev = machine_get_container("peripheral-anon");
     }
 
     return dev;
@@ -1085,7 +1085,7 @@ static GSList *qdev_build_hotpluggable_device_list(Object *peripheral)
 static void peripheral_device_del_completion(ReadLineState *rs,
                                              const char *str)
 {
-    Object *peripheral = container_get(qdev_get_machine(), "/peripheral");
+    Object *peripheral = machine_get_container("peripheral");
     GSList *list, *item;
 
     list = qdev_build_hotpluggable_device_list(peripheral);
system/vl.c +1 -2
index 3bb8f2db9a..822f7ff656 100644
@@ -2120,8 +2120,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));
-    object_property_add_child(container_get(OBJECT(current_machine),
-                                            "/unattached"),
+    object_property_add_child(machine_get_container("unattached"),
                               "sysbus", OBJECT(sysbus_get_default()));
 
     if (machine_class->minimum_page_bits) {
ui/dbus-chardev.c +1 -1
index 1d3a7122a1..bf061cbc93 100644
@@ -106,7 +106,7 @@ dbus_chardev_init(DBusDisplay *dpy)
     dpy->notifier.notify = dbus_display_on_notify;
     dbus_display_notifier_add(&dpy->notifier);
 
-    object_child_foreach(container_get(object_get_root(), "/chardevs"),
+    object_child_foreach(object_get_container("chardevs"),
                          dbus_display_chardev_foreach, dpy);
 }
 
Signature · 1 line
2.45.0

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 :|
#38 lore raw

Re: [PATCH 10/12] qom: Create system containers explicitly

In reply to Daniel P. Berrangé
Quoted 22 lines · Daniel P. Berrangé
Quoted 21 lines · Peter Xu

Always explicitly create QEMU system containers upfront.

Root containers will be created when trying to fetch the root object the 1st time. Machine sub-containers will be created only until machine is being initialized.

---
  hw/core/machine.c | 19 ++++++++++++++++---
  qom/object.c      | 16 +++++++++++++++-
  2 files changed, 31 insertions(+), 4 deletions(-)
qom/object.c +0 -0
index 214d6eb4c1..810e6f2bd9 100644
@@ -1734,12 +1734,26 @@ const char *object_property_get_type(Object *obj, const char *name, Error **errp
      return prop->type;
  }
 
Quoted 16 lines

qom/object.c const char *object_property_get_type(Object *obj, const char *name, Error **errp · line 1746

Quoted 10 lines
qom/object.c +10 -0
+static Object *object_root_initialize(void)
+{
+    Object *root = object_new(TYPE_CONTAINER);
+
+    /*
+     * Create all QEMU system containers.  "machine" and its sub-containers
+     * are only created when machine initializes (qemu_create_machine()).
+     */
+    container_create(root, "chardevs");
+    container_create(root, "objects");

This is where I would expect 'backend' to have been created rather than ui/console.c, though you could potentially make a case to create it from the machine function, snice console stuff can't be used outside of the machine context, while chardevs/objects can be used in qemu-img/qemu-nbd, etc

What about creating "backend" container in qemu_create_machine()?

#6 lore raw

Re: [PATCH 02/12] qom: New container_create()

In reply to Peter Xu
Quoted 14 lines · Peter Xu

To move towards explicit creations of containers, starting that by providing a helper for creating container objects.

---
 include/qom/object.h | 12 ++++++++++++
 qom/container.c      | 18 +++++++++++++++---
 2 files changed, 27 insertions(+), 3 deletions(-)
include/qom/object.h +0 -0
index 3ba370ce9b..41ef53241e 100644

include/qom/object.h int object_child_foreach_recursive(Object *obj, · line 2040

Quoted 8 lines
include/qom/object.h +5 -0
@@ -2033,6 +2033,18 @@ int object_child_foreach_recursive(Object *obj,
  */
 Object *container_get(Object *root, const char *path);
 
+
+/**
+ * container_create:
+ * @root: root of the object to create the new container
+ * @name: name of the new container

Is this the name of the property of @root to hold the new container? Peeking ahead to the implementation... yes.

include/qom/object.h int object_child_foreach_recursive(Object *obj, · line 2046

Quoted 6 lines
include/qom/object.h +6 -0
+ *
+ * Create a container object under @root with @name.
+ *
+ * Returns: the newly created container object.
+ */
+Object *container_create(Object *root, const char *name);

No function in this file is named like FOO_create(). Hmm.

Compare:

/**
* object_property_try_add_child:
* @obj: the object to add a property to
* @name: the name of the property
* @child: the child object
* @errp: pointer to error object
*
* Child properties form the composition tree. All objects need to be a child
* of another object. Objects can only be a child of one object. *
* There is no way for a child to determine what its parent is. It is not
* a bidirectional relationship. This is by design.

Aside: this is nonsense. While you're not supposed to simply use obj->parent (it's documented as private), you can still get the child's canonical path with object_get_canonical_path(), split off its last component to get the parent's canonical path, then use object_resolve_path() to get the parent.

*
* The value of a child property as a C string will be the child object's
* canonical path. It can be retrieved using object_property_get_str().
* The child object itself can be retrieved using object_property_get_link(). *
* Returns: The newly added property on success, or %NULL on failure. */

What about

/**
* object_property_add_new_container:
* @obj: the parent object
* @name: the name of the parent object's property to add *
* Add a newly created container object to a parent object. *
* Returns: the newly created container object. Its reference count
* is 1, and the reference is owned by the parent object. */

Quoted 12 lines
include/qom/object.h +1 -0
+
 /**
  * object_property_help:
  * @name: the name of the property
qom/container.c +0 -0
index cfec92a944..da657754a4 100644
@@ -24,6 +24,20 @@ static void container_register_types(void)
     type_register_static(&container_info);
 }
 

qom/container.c static void container_register_types(void) · line 36

Quoted 10 lines
qom/container.c +10 -0
+Object *container_create(Object *obj, const char *name)
+{
+    Object *child = object_new(TYPE_CONTAINER);
+
+    object_property_add_child(obj, name, child);
+    /*
+     * Simplify the caller by always drop the refcount directly here, as
+     * containers are normally never destroyed after created anyway.
+     */
+    object_unref(child);

Do we still need the comment if we document the reference count in the function comment?

Quoted 17 lines
qom/container.c +5 -3
+
+    return child;
+}
+
 Object *container_get(Object *root, const char *path)
 {
     Object *obj, *child;
@@ -37,9 +51,7 @@ Object *container_get(Object *root, const char *path)
     for (i = 1; parts[i] != NULL; i++, obj = child) {
         child = object_resolve_path_component(obj, parts[i]);
         if (!child) {
-            child = object_new(TYPE_CONTAINER);
-            object_property_add_child(obj, parts[i], child);
-            object_unref(child);
+            child = container_create(obj, parts[i]);
         }
     }
#34 lore raw

Re: [PATCH 09/12] qdev: Add machine_get_container()

In reply to Peter Xu
Quoted 13 lines · Peter Xu

Add a helper to fetch machine containers. Add some sanity check around.

---
 include/hw/qdev-core.h | 10 ++++++++++
 hw/core/qdev.c         | 17 +++++++++++++++++
 2 files changed, 27 insertions(+)
include/hw/qdev-core.h +0 -0
index 5be9844412..38edfb1b54 100644

include/hw/qdev-core.h const char *qdev_fw_name(DeviceState *dev); · line 1003

Quoted 8 lines
include/hw/qdev-core.h +5 -0
@@ -996,6 +996,16 @@ const char *qdev_fw_name(DeviceState *dev);
 void qdev_assert_realized_properly(void);
 Object *qdev_get_machine(void);
 
+/**
+ * machine_get_container:
+ * @name: The name of container to lookup
+ *
+ * Get a container of the machine (QOM path "/machine/XXX").

Suggest "/machine/NAME" here.

Quoted 8 lines
include/hw/qdev-core.h +5 -0
+ *
+ * Returns: the machine container object.
+ */
+Object *machine_get_container(const char *name);
+
 /**
  * qdev_get_human_name() - Return a human-readable name for a device
  * @dev: The device. Must be a valid and non-NULL pointer.

[...]

#43 lore raw

Re: [PATCH 10/12] qom: Create system containers explicitly

In reply to Peter Xu
Quoted 4 lines · Peter Xu

Always explicitly create QEMU system containers upfront.

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

Which ones are affected?

Not a fan of creating stuff on first use, unless there may not be any use. But no worse than before.

Quoted 31 lines

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

---
 hw/core/machine.c | 19 ++++++++++++++++---
 qom/object.c      | 16 +++++++++++++++-
 2 files changed, 31 insertions(+), 4 deletions(-)
hw/core/machine.c +14 -0
index a35c4a8fae..a184dbf8f0 100644
@@ -1193,14 +1193,27 @@ static void machine_class_base_init(ObjectClass *oc, void *data)
     }
 }
 
+const char *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++) {
+        container_create(machine, machine_containers[i]);
+    }
+}

hw/core/machine.c static void machine_class_base_init(ObjectClass *oc, void *data) · line 1216

Quoted 10 lines
hw/core/machine.c +2 -3
+
 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");
-
+    qemu_create_machine_containers(obj);

We now additionally create "/machine/unattached" here.

Quoted 22 lines
hw/core/machine.c +0 -0
     ms->dump_guest_core = true;
     ms->mem_merge = (QEMU_MADV_MERGEABLE != QEMU_MADV_INVALID);
     ms->enable_graphics = true;
qom/object.c +11 -0
index 214d6eb4c1..810e6f2bd9 100644
@@ -1734,12 +1734,26 @@ const char *object_property_get_type(Object *obj, const char *name, Error **errp
     return prop->type;
 }
 
+static Object *object_root_initialize(void)
+{
+    Object *root = object_new(TYPE_CONTAINER);
+
+    /*
+     * Create all QEMU system containers.  "machine" and its sub-containers
+     * are only created when machine initializes (qemu_create_machine()).
+     */
+    container_create(root, "chardevs");
+    container_create(root, "objects");
+

qom/object.c const char *object_property_get_type(Object *obj, const char *name, Error **errp · line 1756

Quoted 10 lines
qom/object.c +4 -1
+    return root;
+}
+
 Object *object_get_root(void)
 {
     static Object *root;
 
     if (!root) {
-        root = object_new(TYPE_CONTAINER);
+        root = object_root_initialize();

We now additonally create "/chardevs" and "/objects" here, not just "/".

Quoted 3 lines
qom/object.c +0 -0
     }
 
     return root;
author
#7 lore raw

Re: [PATCH 02/12] qom: New container_create()

In reply to Markus Armbruster
Quoted 68 lines · Markus Armbruster
Quoted 14 lines · Peter Xu

To move towards explicit creations of containers, starting that by providing a helper for creating container objects.

---
 include/qom/object.h | 12 ++++++++++++
 qom/container.c      | 18 +++++++++++++++---
 2 files changed, 27 insertions(+), 3 deletions(-)
include/qom/object.h +0 -0
index 3ba370ce9b..41ef53241e 100644

include/qom/object.h int object_child_foreach_recursive(Object *obj, · line 2040

Quoted 8 lines
include/qom/object.h +5 -0
@@ -2033,6 +2033,18 @@ int object_child_foreach_recursive(Object *obj,
  */
 Object *container_get(Object *root, const char *path);
 
+
+/**
+ * container_create:
+ * @root: root of the object to create the new container
+ * @name: name of the new container

Is this the name of the property of @root to hold the new container? Peeking ahead to the implementation... yes.

include/qom/object.h int object_child_foreach_recursive(Object *obj, · line 2046

Quoted 6 lines
include/qom/object.h +6 -0
+ *
+ * Create a container object under @root with @name.
+ *
+ * Returns: the newly created container object.
+ */
+Object *container_create(Object *root, const char *name);

No function in this file is named like FOO_create(). Hmm.

Compare:

/**
* object_property_try_add_child:
* @obj: the object to add a property to
* @name: the name of the property
* @child: the child object
* @errp: pointer to error object
*
* Child properties form the composition tree. All objects need to be a child
* of another object. Objects can only be a child of one object. *
* There is no way for a child to determine what its parent is. It is not
* a bidirectional relationship. This is by design.

Aside: this is nonsense. While you're not supposed to simply use obj->parent (it's documented as private), you can still get the child's canonical path with object_get_canonical_path(), split off its last component to get the parent's canonical path, then use object_resolve_path() to get the parent.

*
* The value of a child property as a C string will be the child object's
* canonical path. It can be retrieved using object_property_get_str().
* The child object itself can be retrieved using object_property_get_link(). *
* Returns: The newly added property on success, or %NULL on failure. */

What about

Quoted 10 lines

/**
* object_property_add_new_container:
* @obj: the parent object
* @name: the name of the parent object's property to add *
* Add a newly created container object to a parent object. *
* Returns: the newly created container object. Its reference count
* is 1, and the reference is owned by the parent object. */

Sure, this may indeed align better with the rest function names.

Quoted 13 lines
Quoted 13 lines
include/qom/object.h +1 -0
+
 /**
  * object_property_help:
  * @name: the name of the property
qom/container.c +0 -0
index cfec92a944..da657754a4 100644
@@ -24,6 +24,20 @@ static void container_register_types(void)
     type_register_static(&container_info);
 }
 
Quoted 13 lines

qom/container.c static void container_register_types(void) · line 36

Quoted 10 lines
qom/container.c +10 -0
+Object *container_create(Object *obj, const char *name)
+{
+    Object *child = object_new(TYPE_CONTAINER);
+
+    object_property_add_child(obj, name, child);
+    /*
+     * Simplify the caller by always drop the refcount directly here, as
+     * containers are normally never destroyed after created anyway.
+     */
+    object_unref(child);

Do we still need the comment if we document the reference count in the function comment?

Probably not. I'll drop this comment while taking above suggestion.

Thanks,

Signature · 1 line
Peter Xu
author
#17 lore raw

Re: [PATCH 05/12] ui/console: Explicitly create "/backend" container

In reply to Daniel P. Berrangé
Quoted 22 lines · Daniel P. Berrangé
Quoted 14 lines · Peter Xu

Follow the trend to explicitly create containers, do that for console.c on "/backend" container.

Cc Marc-André Lureau <[email protected]>
---
 ui/console.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
ui/console.c +0 -0
index 5165f17125..36f8c6debb 100644

ui/console.c DisplayState *init_displaystate(void) · line 1157

Quoted 4 lines
ui/console.c +1 -0
@@ -1154,14 +1154,14 @@ DisplayState *init_displaystate(void)
 {
     gchar *name;
     QemuConsole *con;
+    Object *backend = container_create(object_get_root(), "backend");

What's the rationale for keeping this in the console code ?

Quoted 3 lines

I'd consider this to be a similar situation to '/chardevs' and '/objects', and be a common container rather than UI specific. IOW, be created by your later patch.

I was trying to be careful on always create then on demand like before. E.g. I was thinking maybe this container shouldn't exist when there's no display at all.

But looks like init_displaystate() is indeed always invoked for system code.. so yeah, perhaps I can move it over too, and drop this patch (below will be part of last patch to use objects_get_container() instead, or part of its splits).

Quoted 22 lines

ui/console.c DisplayState *init_displaystate(void) · line 1166

Quoted 12 lines
ui/console.c +1 -2
     QTAILQ_FOREACH(con, &consoles, next) {
         /* Hook up into the qom tree here (not in object_new()), once
          * all QemuConsoles are created and the order / numbering
          * doesn't change any more */
         name = g_strdup_printf("console[%d]", con->index);
-        object_property_add_child(container_get(object_get_root(), "/backend"),
-                                  name, OBJECT(con));
+        object_property_add_child(backend, name, OBJECT(con));
         g_free(name);
     }

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 :|
Signature · 1 line
Peter Xu
author
#20 lore raw

Re: [PATCH 06/12] hw/ppc: Explicitly create the drc container

In reply to Philippe Mathieu-Daudé
Quoted 42 lines · Philippe Mathieu-Daudé

Hi Peter,

Quoted 39 lines · Peter Xu

QEMU will start to not rely on implicit creations of containers soon. Make PPC drc devices follow by explicitly create the container whenever a drc device is realized, dropping container_get() calls.

No functional change intended.

Cc Nicholas Piggin <[email protected]>
Cc Daniel Henrique Barboza <[email protected]>
Cc Harsh Prateek Bora <[email protected]>
---
  hw/ppc/spapr_drc.c | 40 ++++++++++++++++++++++++++++++----------
  1 file changed, 30 insertions(+), 10 deletions(-)
hw/ppc/spapr_drc.c +20 -0
+static GOnce drc_container_created = G_ONCE_INIT;
+
+static gpointer drc_container_create(gpointer unused G_GNUC_UNUSED)
+{
+    container_create(object_get_root(), DRC_CONTAINER_PATH);
+    return NULL;
+}
+
+static Object *drc_container_get(void)
+{
+    return object_resolve_path_component(
+        object_get_root(), DRC_CONTAINER_PATH);
+}
+
+/* TODO: create the container in an ppc init function */
+static void drc_container_create_once(void)
+{
+    g_once(&drc_container_created, drc_container_create, NULL);
+}
+
  static void drc_realize(DeviceState *d, Error **errp)
  {
      SpaprDrc *drc = SPAPR_DR_CONNECTOR(d);
Quoted 8 lines

hw/ppc/spapr_drc.c static void drc_realize(DeviceState *d, Error **errp) · line 544

Quoted 5 lines
hw/ppc/spapr_drc.c +2 -0
@@ -521,6 +541,9 @@ static void drc_realize(DeviceState *d, Error **errp)
      Object *root_container;
      const char *child_name;
+    /* Whenever a DRC device is realized, create the container */
+    drc_container_create_once();

Can't we just create it once in spapr_dr_connector_class_init(), removing the G_ONCE_INIT need?

IIUC it's a matter of whether there's case where we have this file compiled in, but never create any DRC device. When that happens, the patch can change the QEMU qom-tree slightly, in that there'll be an empty drc container while we used to not have it.

I'm trying to be on the safe side in case something would detect the container's existance to know whether drc devices are present. lazy create it in realize() is the safest way to behave 100% identical like before, considering my ppc knowledge is merely zero (even if I have drc tests covered in ppc64's qtest..).

However I also doubt whether it matters much. It'll be great if I can get an ACK from anyone familiar with this device (Phil, are you?), then I can move it over.

Quoted 14 lines
Quoted 13 lines
hw/ppc/spapr_drc.c +1 -1
      trace_spapr_drc_realize(spapr_drc_index(drc));
      /* NOTE: we do this as part of realize/unrealize due to the fact
       * that the guest will communicate with the DRC via RTAS calls
@@ -529,7 +552,7 @@ static void drc_realize(DeviceState *d, Error **errp)
       * inaccessible by the guest, since lookups rely on this path
       * existing in the composition tree
       */
-    root_container = container_get(object_get_root(), DRC_CONTAINER_PATH);
+    root_container = drc_container_get();
      child_name = object_get_canonical_path_component(OBJECT(drc));
      trace_spapr_drc_realize_child(spapr_drc_index(drc), child_name);
      object_property_add_alias(root_container, link_name,
Signature · 1 line
Peter Xu
author
#25 lore raw

Re: [PATCH 07/12] ppc/e500: Avoid abuse of container_get()

In reply to Cédric Le Goater
Quoted 30 lines · Cédric Le Goater
Quoted 29 lines · Cédric Le Goater
Quoted 17 lines · Peter Xu

container_get() is going to become strict on not allowing to return a non-container.

Switch the e500 user to use object_resolve_path_component() explicitly.

---
  hw/pci-host/ppce500.c | 4 ++--

  1 file changed, 2 insertions(+), 2 deletions(-)
hw/pci-host/ppce500.c +0 -0
index b70631045a..65233b9e3f 100644

hw/pci-host/ppce500.c static const VMStateDescription vmstate_ppce500_pci = { · line 422

Quoted 7 lines
hw/pci-host/ppce500.c +2 -2
@@ -418,8 +418,8 @@ static const VMStateDescription vmstate_ppce500_pci = {
   static void e500_pcihost_bridge_realize(PCIDevice *d, Error **errp)
   {
       PPCE500PCIBridgeState *b = PPC_E500_PCI_BRIDGE(d);
-    PPCE500CCSRState *ccsr = CCSR(container_get(qdev_get_machine(),
-                                  "/e500-ccsr"));
+    PPCE500CCSRState *ccsr = CCSR(
+        object_resolve_path_component(qdev_get_machine(), "e500-ccsr"));

why not simply use :

Quoted 4 lines
Quoted 1 line

      CCSR(object_resolve_path("/machine/e500-ccsr", NULL));

I guess we want to avoid the absolute paths. If so,

It wasn't my intention, but what you said actually makes sense to me to avoid hard-coded "/machine" if possible.

OTOH, object_resolve_path_component() was actually tiny little faster when we know the depth of the path.

Quoted 11 lines

We might want to convert these lookups to object_resolve_path_component too, not in this patchset.

hw/i386/acpi-build.c:    host = PCI_HOST_BRIDGE(object_resolve_path("/machine/i440fx", NULL));
hw/i386/acpi-build.c:        host = PCI_HOST_BRIDGE(object_resolve_path("/machine/q35", NULL));
target/i386/kvm/kvm.c:        (MemoryRegion *) object_resolve_path("/machine/smram", NULL);
target/i386/tcg/sysemu/tcg-cpu.c:        (MemoryRegion *) object_resolve_path("/machine/smram", NULL);

Sounds reasonable to me to use the same style. I'll stick with this patch as of now in the current series.

Thanks,

Signature · 1 line
Peter Xu
author
#30 lore raw

Re: [PATCH 08/12] qdev: Make qdev_get_machine() not use container_get()

In reply to Daniel P. Berrangé
Quoted 26 lines · Daniel P. Berrangé
Quoted 25 lines · Peter Xu

Currently, qdev_get_machine() has a slight misuse on container_get(), as the helper says "get a container" but in reality the goal is to get the machine object. It is still a "container" but not strictly.

Note that it _may_ get a container (at "/machine") in our current unit test of test-qdev-global-props.c before all these changes, but it's probably unexpected and worked by accident.

Switch to an explicit object_resolve_path_component(), with a side benefit that qdev_get_machine() can happen a lot, and we don't need to split the string ("/machine") every time. This also paves way for making the helper container_get() never try to return a non-container at all.

---
 hw/core/qdev.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
hw/core/qdev.c +0 -0
index 5f13111b77..c869c47a27 100644
@@ -817,7 +817,13 @@ Object *qdev_get_machine(void)
     static Object *dev;
 
Quoted 12 lines

hw/core/qdev.c Object *qdev_get_machine(void) · line 825

Quoted 8 lines
hw/core/qdev.c +6 -1
     if (dev == NULL) {
-        dev = container_get(object_get_root(), "/machine");
+        /*
+         * NOTE: when the machine is not yet created, this helper will
+         * also keep the cached object untouched and return NULL.  The next
+         * invoke of the helper will try to look for the machine again.
+         * It'll only cache the pointer when it's found the first time.
+         */

This smells like a recipe for subtle bugs. I think I'd consider it a code flaw if something called qdev_get_machine() in a scenario where the machine does not yet exist.

Returning NULL to catch such bug isn't that bad either if it appears, IMHO. It'll crash on the null reference instead.

I did this not only because I used to allow this return NULL (in my other patchset to enable singleton for vIOMMUs, then whoever wants to do that for singleton doesn't need yet another qdev patch..), but also I think qemu is complex enough, so for such frequently used global helper it won't surprise me that users will start to detect NULL soon anyway.

For example, I learned only recently that migration_is_active() can be invoked before migration object is initialized.. and it needs to keep working like that..

Quoted 6 lines

hw/core/qdev.c Object *qdev_get_machine(void) · line 827

Quoted 3 lines
hw/core/qdev.c +1 -0
+        dev = object_resolve_path_component(object_get_root(), "machine");
     }

IOW, I think we should assert that dev != NULL to ensure we immediately diagnose the flawed sequence of calls.

I'd confess indeed the current qemu can assert that. OK, let me switch.

Signature · 1 line
Peter Xu
#21 lore raw

Re: [PATCH 06/12] hw/ppc: Explicitly create the drc container

In reply to Peter Xu
Quoted 52 lines · Peter Xu
Quoted 42 lines · Philippe Mathieu-Daudé

Hi Peter,

Quoted 39 lines · Peter Xu

QEMU will start to not rely on implicit creations of containers soon. Make PPC drc devices follow by explicitly create the container whenever a drc device is realized, dropping container_get() calls.

No functional change intended.

Cc Nicholas Piggin <[email protected]>
Cc Daniel Henrique Barboza <[email protected]>
Cc Harsh Prateek Bora <[email protected]>
---
   hw/ppc/spapr_drc.c | 40 ++++++++++++++++++++++++++++++----------
   1 file changed, 30 insertions(+), 10 deletions(-)
hw/ppc/spapr_drc.c +20 -0
+static GOnce drc_container_created = G_ONCE_INIT;
+
+static gpointer drc_container_create(gpointer unused G_GNUC_UNUSED)
+{
+    container_create(object_get_root(), DRC_CONTAINER_PATH);
+    return NULL;
+}
+
+static Object *drc_container_get(void)
+{
+    return object_resolve_path_component(
+        object_get_root(), DRC_CONTAINER_PATH);
+}
+
+/* TODO: create the container in an ppc init function */
+static void drc_container_create_once(void)
+{
+    g_once(&drc_container_created, drc_container_create, NULL);
+}
+
   static void drc_realize(DeviceState *d, Error **errp)
   {
       SpaprDrc *drc = SPAPR_DR_CONNECTOR(d);
Quoted 8 lines

hw/ppc/spapr_drc.c static void drc_realize(DeviceState *d, Error **errp) · line 544

Quoted 5 lines
hw/ppc/spapr_drc.c +2 -0
@@ -521,6 +541,9 @@ static void drc_realize(DeviceState *d, Error **errp)
       Object *root_container;
       const char *child_name;
+    /* Whenever a DRC device is realized, create the container */
+    drc_container_create_once();

Can't we just create it once in spapr_dr_connector_class_init(), removing the G_ONCE_INIT need?

Quoted 8 lines

IIUC it's a matter of whether there's case where we have this file compiled in, but never create any DRC device. When that happens, the patch can change the QEMU qom-tree slightly, in that there'll be an empty drc container while we used to not have it.

I'm trying to be on the safe side in case something would detect the container's existance to know whether drc devices are present. lazy create it in realize() is the safest way to behave 100% identical like before,

Class singleton must be taken care in class_init, not once in the first instance realize(). I'd rather fix this bad QOM pattern.

Quoted 6 lines

considering my ppc knowledge is merely zero (even if I have drc tests covered in ppc64's qtest..).

However I also doubt whether it matters much. It'll be great if I can get an ACK from anyone familiar with this device (Phil, are you?), then I can move it over.

I'm not, but having to care about G_ONCE_INIT seems cumbersome.

Quoted 16 lines
Quoted 15 lines
Quoted 14 lines
hw/ppc/spapr_drc.c +1 -1
       trace_spapr_drc_realize(spapr_drc_index(drc));
       /* NOTE: we do this as part of realize/unrealize due to the fact
        * that the guest will communicate with the DRC via RTAS calls
@@ -529,7 +552,7 @@ static void drc_realize(DeviceState *d, Error **errp)
        * inaccessible by the guest, since lookups rely on this path
        * existing in the composition tree
        */
-    root_container = container_get(object_get_root(), DRC_CONTAINER_PATH);
+    root_container = drc_container_get();
       child_name = object_get_canonical_path_component(OBJECT(drc));
       trace_spapr_drc_realize_child(spapr_drc_index(drc), child_name);
       object_property_add_alias(root_container, link_name,
author
#39 lore raw

Re: [PATCH 10/12] qom: Create system containers explicitly

In reply to Philippe Mathieu-Daudé
Quoted 30 lines · Philippe Mathieu-Daudé
Quoted 29 lines · Daniel P. Berrangé
Quoted 28 lines · Peter Xu

Always explicitly create QEMU system containers upfront.

Root containers will be created when trying to fetch the root object the 1st time. Machine sub-containers will be created only until machine is being initialized.

---
  hw/core/machine.c | 19 ++++++++++++++++---
  qom/object.c      | 16 +++++++++++++++-
  2 files changed, 31 insertions(+), 4 deletions(-)
qom/object.c +8 -0
index 214d6eb4c1..810e6f2bd9 100644
@@ -1734,12 +1734,26 @@ const char *object_property_get_type(Object *obj, const char *name, Error **errp
      return prop->type;
  }
+static Object *object_root_initialize(void)
+{
+    Object *root = object_new(TYPE_CONTAINER);
+
+    /*
+     * Create all QEMU system containers.  "machine" and its sub-containers
+     * are only created when machine initializes (qemu_create_machine()).
+     */
Quoted 8 lines
Quoted 8 lines

qom/object.c const char *object_property_get_type(Object *obj, const char *name, Error **errp · line 1746

Quoted 2 lines
qom/object.c +2 -0
+    container_create(root, "chardevs");
+    container_create(root, "objects");

This is where I would expect 'backend' to have been created rather than ui/console.c, though you could potentially make a case to create it from the machine function, snice console stuff can't be used outside of the machine context, while chardevs/objects can be used in qemu-img/qemu-nbd, etc

Would it hurt if we do it altogether here even if it won't be used in qemu-img/qemu-nbd?

IMHO we should either make it simple (assuming empty containers won't hurt there..), or we should just leave "backend" to ui/ code, so we don't assume which binary is using the ui code: whoever uses it will create the container.

Quoted 2 lines

What about creating "backend" container in qemu_create_machine()?

I remember I started with that but it didn't work. IIRC that's because machine_initfn() (or somewhere around the init code) requires the containers to present, hence it's too late even if we create the containers right after this line:

    current_machine = MACHINE(object_new_with_class(OBJECT_CLASS(machine_class)));
Signature · 1 line
Peter Xu
#26 lore raw

Re: [PATCH 07/12] ppc/e500: Avoid abuse of container_get()

In reply to Peter Xu
Quoted 31 lines · Peter Xu
Quoted 30 lines · Cédric Le Goater
Quoted 29 lines · Cédric Le Goater
Quoted 17 lines · Peter Xu

container_get() is going to become strict on not allowing to return a non-container.

Switch the e500 user to use object_resolve_path_component() explicitly.

---
   hw/pci-host/ppce500.c | 4 ++--
   1 file changed, 2 insertions(+), 2 deletions(-)
hw/pci-host/ppce500.c +0 -0
index b70631045a..65233b9e3f 100644

hw/pci-host/ppce500.c static const VMStateDescription vmstate_ppce500_pci = { · line 422

Quoted 7 lines
hw/pci-host/ppce500.c +2 -2
@@ -418,8 +418,8 @@ static const VMStateDescription vmstate_ppce500_pci = {
   static void e500_pcihost_bridge_realize(PCIDevice *d, Error **errp)
   {
       PPCE500PCIBridgeState *b = PPC_E500_PCI_BRIDGE(d);
-    PPCE500CCSRState *ccsr = CCSR(container_get(qdev_get_machine(),
-                                  "/e500-ccsr"));
+    PPCE500CCSRState *ccsr = CCSR(
+        object_resolve_path_component(qdev_get_machine(), "e500-ccsr"));

why not simply use :

Quoted 7 lines
Quoted 4 lines
Quoted 1 line
        CCSR(object_resolve_path("/machine/e500-ccsr", NULL));

I guess we want to avoid the absolute paths. If so,

It wasn't my intention, but what you said actually makes sense to me to avoid hard-coded "/machine" if possible.

Long term (heterogeneous emulation & dynamic machines in mind) I'm unsure about qdev_get_machine() future. Not to take care now, but I'm expecting it to evolve. So not using hardcoded path is better IMHO.

Quoted 21 lines

OTOH, object_resolve_path_component() was actually tiny little faster when we know the depth of the path.

Quoted 11 lines

We might want to convert these lookups to object_resolve_path_component too, not in this patchset.

hw/i386/acpi-build.c:    host = PCI_HOST_BRIDGE(object_resolve_path("/machine/i440fx", NULL));
hw/i386/acpi-build.c:        host = PCI_HOST_BRIDGE(object_resolve_path("/machine/q35", NULL));
target/i386/kvm/kvm.c:        (MemoryRegion *) object_resolve_path("/machine/smram", NULL);
target/i386/tcg/sysemu/tcg-cpu.c:        (MemoryRegion *) object_resolve_path("/machine/smram", NULL);

Sounds reasonable to me to use the same style. I'll stick with this patch as of now in the current series.

Thanks,

author
#44 lore raw

Re: [PATCH 10/12] qom: Create system containers explicitly

In reply to Markus Armbruster
Quoted 8 lines · Markus Armbruster
Quoted 4 lines · Peter Xu

Always explicitly create QEMU system containers upfront.

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

Which ones are affected?

I updated the commit message to this:

qom: Create system containers explicitly

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

Note that I also added "backend" per request from Dan, as of now.

Quoted 3 lines

Not a fan of creating stuff on first use, unless there may not be any use. But no worse than before.

True.. since that can be a separate question to answer, I avoided going too far to dig into which binaries may use containers, which may not.

Thanks,

Signature · 1 line
Peter Xu
#40 lore raw

Re: [PATCH 10/12] qom: Create system containers explicitly

In reply to Peter Xu
Quoted 48 lines · Peter Xu
Quoted 30 lines · Philippe Mathieu-Daudé
Quoted 29 lines · Daniel P. Berrangé
Quoted 28 lines · Peter Xu

Always explicitly create QEMU system containers upfront.

Root containers will be created when trying to fetch the root object the 1st time. Machine sub-containers will be created only until machine is being initialized.

---
   hw/core/machine.c | 19 ++++++++++++++++---
   qom/object.c      | 16 +++++++++++++++-
   2 files changed, 31 insertions(+), 4 deletions(-)
qom/object.c +8 -0
index 214d6eb4c1..810e6f2bd9 100644
@@ -1734,12 +1734,26 @@ const char *object_property_get_type(Object *obj, const char *name, Error **errp
       return prop->type;
   }
+static Object *object_root_initialize(void)
+{
+    Object *root = object_new(TYPE_CONTAINER);
+
+    /*
+     * Create all QEMU system containers.  "machine" and its sub-containers
+     * are only created when machine initializes (qemu_create_machine()).
+     */
Quoted 8 lines
Quoted 8 lines

qom/object.c const char *object_property_get_type(Object *obj, const char *name, Error **errp · line 1746

Quoted 2 lines
qom/object.c +2 -0
+    container_create(root, "chardevs");
+    container_create(root, "objects");

This is where I would expect 'backend' to have been created rather than ui/console.c, though you could potentially make a case to create it from the machine function, snice console stuff can't be used outside of the machine context, while chardevs/objects can be used in qemu-img/qemu-nbd, etc

Would it hurt if we do it altogether here even if it won't be used in qemu-img/qemu-nbd?

IMHO we should either make it simple (assuming empty containers won't hurt there..), or we should just leave "backend" to ui/ code, so we don't assume which binary is using the ui code: whoever uses it will create the container.

Quoted 1 line
Quoted 8 lines
Quoted 1 line

What about creating "backend" container in qemu_create_machine()?

I remember I started with that but it didn't work. IIRC that's because machine_initfn() (or somewhere around the init code) requires the containers to present, hence it's too late even if we create the containers right after this line:

     current_machine = MACHINE(object_new_with_class(OBJECT_CLASS(machine_class)));

So qemu_create_machine_containers() really belongs to qemu_create_machine() =)

author
#41 lore raw

Re: [PATCH 10/12] qom: Create system containers explicitly

Quoted 56 lines · Philippe Mathieu-Daudé
Quoted 55 lines · Peter Xu
Quoted 30 lines · Philippe Mathieu-Daudé
Quoted 29 lines · Daniel P. Berrangé
Quoted 28 lines · Peter Xu

Always explicitly create QEMU system containers upfront.

Root containers will be created when trying to fetch the root object the 1st time. Machine sub-containers will be created only until machine is being initialized.

---
   hw/core/machine.c | 19 ++++++++++++++++---
   qom/object.c      | 16 +++++++++++++++-
   2 files changed, 31 insertions(+), 4 deletions(-)
qom/object.c +8 -0
index 214d6eb4c1..810e6f2bd9 100644
@@ -1734,12 +1734,26 @@ const char *object_property_get_type(Object *obj, const char *name, Error **errp
       return prop->type;
   }
+static Object *object_root_initialize(void)
+{
+    Object *root = object_new(TYPE_CONTAINER);
+
+    /*
+     * Create all QEMU system containers.  "machine" and its sub-containers
+     * are only created when machine initializes (qemu_create_machine()).
+     */
Quoted 8 lines
Quoted 8 lines

qom/object.c const char *object_property_get_type(Object *obj, const char *name, Error **errp · line 1745

Quoted 2 lines
qom/object.c +2 -0
+    container_create(root, "chardevs");
+    container_create(root, "objects");

This is where I would expect 'backend' to have been created rather than ui/console.c, though you could potentially make a case to create it from the machine function, snice console stuff can't be used outside of the machine context, while chardevs/objects can be used in qemu-img/qemu-nbd, etc

Would it hurt if we do it altogether here even if it won't be used in qemu-img/qemu-nbd?

IMHO we should either make it simple (assuming empty containers won't hurt there..), or we should just leave "backend" to ui/ code, so we don't assume which binary is using the ui code: whoever uses it will create the container.

Quoted 2 lines

What about creating "backend" container in qemu_create_machine()?

I remember I started with that but it didn't work. IIRC that's because machine_initfn() (or somewhere around the init code) requires the containers to present, hence it's too late even if we create the containers right after this line:

Quoted 4 lines
Quoted 1 line
     current_machine = MACHINE(object_new_with_class(OBJECT_CLASS(machine_class)));

So qemu_create_machine_containers() really belongs to qemu_create_machine() =)

Frankly, I don't immediately get this line..

But when I was trying again just to check my memory, I can't see anything crash anymore, moving things over.

So while I'll test some more, I can switch to that if I cannot reproduce any issue with it. That's:

===8<===

hw/core/machine.c +0 -16
index ed613ec4cb..a72c001c3d 100644
@@ -1193,27 +1193,11 @@ static void machine_class_base_init(ObjectClass *oc, void *data)
     }
 }
 
-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 machine_initfn(Object *obj)
 {
     MachineState *ms = MACHINE(obj);
     MachineClass *mc = MACHINE_GET_CLASS(obj);
 
-    qemu_create_machine_containers(obj);
     ms->dump_guest_core = true;
     ms->mem_merge = (QEMU_MADV_MERGEABLE != QEMU_MADV_INVALID);
     ms->enable_graphics = true;
system/vl.c +16 -0
index 822f7ff656..cdc0b6e10c 100644
@@ -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(machine_get_container("unattached"),
                               "sysbus", OBJECT(sysbus_get_default()));
 
Signature · 1 line
Peter Xu
#42 lore raw

Re: [PATCH 10/12] qom: Create system containers explicitly

Quoted 57 lines · Peter Xu
Quoted 56 lines · Philippe Mathieu-Daudé
Quoted 55 lines · Peter Xu
Quoted 30 lines · Philippe Mathieu-Daudé
Quoted 29 lines · Daniel P. Berrangé
Quoted 28 lines · Peter Xu

Always explicitly create QEMU system containers upfront.

Root containers will be created when trying to fetch the root object the 1st time. Machine sub-containers will be created only until machine is being initialized.

---
    hw/core/machine.c | 19 ++++++++++++++++---
    qom/object.c      | 16 +++++++++++++++-
    2 files changed, 31 insertions(+), 4 deletions(-)
qom/object.c +8 -0
index 214d6eb4c1..810e6f2bd9 100644
@@ -1734,12 +1734,26 @@ const char *object_property_get_type(Object *obj, const char *name, Error **errp
        return prop->type;
    }
+static Object *object_root_initialize(void)
+{
+    Object *root = object_new(TYPE_CONTAINER);
+
+    /*
+     * Create all QEMU system containers.  "machine" and its sub-containers
+     * are only created when machine initializes (qemu_create_machine()).
+     */
Quoted 8 lines
Quoted 8 lines

qom/object.c const char *object_property_get_type(Object *obj, const char *name, Error **errp · line 1745

Quoted 2 lines
qom/object.c +2 -0
+    container_create(root, "chardevs");
+    container_create(root, "objects");

This is where I would expect 'backend' to have been created rather than ui/console.c, though you could potentially make a case to create it from the machine function, snice console stuff can't be used outside of the machine context, while chardevs/objects can be used in qemu-img/qemu-nbd, etc

Would it hurt if we do it altogether here even if it won't be used in qemu-img/qemu-nbd?

IMHO we should either make it simple (assuming empty containers won't hurt there..), or we should just leave "backend" to ui/ code, so we don't assume which binary is using the ui code: whoever uses it will create the container.

Quoted 2 lines

What about creating "backend" container in qemu_create_machine()?

I remember I started with that but it didn't work. IIRC that's because machine_initfn() (or somewhere around the init code) requires the containers to present, hence it's too late even if we create the containers right after this line:

Quoted 6 lines
Quoted 4 lines
Quoted 1 line
      current_machine = MACHINE(object_new_with_class(OBJECT_CLASS(machine_class)));

So qemu_create_machine_containers() really belongs to qemu_create_machine() =)

Frankly, I don't immediately get this line..

"machine_initfn requires the containers to be present" -> machine_initfn isn't the place to create them, it has to be before or after. Since it can't be before, the "after" place is qemu_create_machine_containers(). Sorry for not being very clear :/

Quoted 69 lines

But when I was trying again just to check my memory, I can't see anything crash anymore, moving things over.

So while I'll test some more, I can switch to that if I cannot reproduce any issue with it. That's:

===8<===

hw/core/machine.c +0 -16
index ed613ec4cb..a72c001c3d 100644
@@ -1193,27 +1193,11 @@ static void machine_class_base_init(ObjectClass *oc, void *data)
      }
  }
 
-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 machine_initfn(Object *obj)
  {
      MachineState *ms = MACHINE(obj);
      MachineClass *mc = MACHINE_GET_CLASS(obj);
 
-    qemu_create_machine_containers(obj);
      ms->dump_guest_core = true;
      ms->mem_merge = (QEMU_MADV_MERGEABLE != QEMU_MADV_INVALID);
      ms->enable_graphics = true;
system/vl.c +15 -0
index 822f7ff656..cdc0b6e10c 100644
@@ -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);

system/vl.c static void qemu_create_machine(QDict *qdict) · line 2141

Quoted 7 lines
system/vl.c +1 -0
@@ -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(machine_get_container("unattached"),
                                "sysbus", OBJECT(sysbus_get_default()));
 

Yes, this is exactly what I was thinking of / expecting :)