[PATCH RFC v2 3/7] qdev: Make device_set_realized() be fully prepared with !machine

Peter Xu posted 7 patches 3 weeks, 3 days ago
[PATCH RFC v2 3/7] qdev: Make device_set_realized() be fully prepared with !machine
Posted by Peter Xu 3 weeks, 3 days ago
We're going to enable qdev_get_machine() to work even before machine is
created.  Make device_set_realized() be prepared with it.

Currently, a device can be realized even before machine is created, but
only in one of QEMU's qtest, test-global-qdev-props.c.

Right now, the test_static_prop_subprocess() test (which creates one simple
object without machine created) will internally make "/machine" to be a
container.  Now explicitly support that case when there's no real
"/machine" object around, then unattached devices will be put under
root ("/") rather than "/machine".  Mostly only for this test case, or for
any future test cases only.

Note that this shouldn't affect anything else that relies on a real machine
being there but only unit tests like mentioned, because if "/machine" is
created as a container as of now, it'll fail QEMU very soon later on
qemu_create_machine() trying to create the real machine, seeing that
there's a conflict.

Signed-off-by: Peter Xu <peterx@redhat.com>
---
 hw/core/qdev.c | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)

diff --git a/hw/core/qdev.c b/hw/core/qdev.c
index db36f54d91..5c83f48b33 100644
--- a/hw/core/qdev.c
+++ b/hw/core/qdev.c
@@ -490,9 +490,17 @@ static void device_set_realized(Object *obj, bool value, Error **errp)
 
         if (!obj->parent) {
             gchar *name = g_strdup_printf("device[%d]", unattached_count++);
+            Object *root = qdev_get_machine();
 
-            object_property_add_child(container_get(qdev_get_machine(),
-                                                    "/unattached"),
+            /*
+             * We could have qdev test cases trying to realize() a device
+             * without machine created.  In that case we use the root.
+             */
+            if (!root) {
+                root = object_get_root();
+            }
+
+            object_property_add_child(container_get(root, "/unattached"),
                                       name, obj);
             unattached_parent = true;
             g_free(name);
-- 
2.45.0