[PATCH 3/5] qdev: Make device_set_realized() always safe in tests

Peter Xu posted 5 patches 4 days, 20 hours ago
[PATCH 3/5] qdev: Make device_set_realized() always safe in tests
Posted by Peter Xu 4 days, 20 hours ago
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, which may not be expected when developing the test.

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 single test case, or for any future test
cases when some device needs to be realized before the machine is present.

This shouldn't affect anything else when QEMU runs as an emulator, as that
always relies on a real machine being created before realizing any devices.
It's because if "/machine" is wrongly created as a container, it'll fail
QEMU very soon later on qemu_create_machine() trying to create the real
machine, conflicting with the "/machine" container.

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 5f13111b77..eff297e584 100644
--- a/hw/core/qdev.c
+++ b/hw/core/qdev.c
@@ -475,9 +475,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
Re: [PATCH 3/5] qdev: Make device_set_realized() always safe in tests
Posted by Daniel P. Berrangé 4 days, 8 hours ago
On Mon, Nov 18, 2024 at 05:13:28PM -0500, Peter Xu wrote:
> 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, which may not be expected when developing the test.
> 
> 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 single test case, or for any future test
> cases when some device needs to be realized before the machine is present.
> 
> This shouldn't affect anything else when QEMU runs as an emulator, as that
> always relies on a real machine being created before realizing any devices.
> It's because if "/machine" is wrongly created as a container, it'll fail
> QEMU very soon later on qemu_create_machine() trying to create the real
> machine, conflicting with the "/machine" container.
> 
> 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 5f13111b77..eff297e584 100644
> --- a/hw/core/qdev.c
> +++ b/hw/core/qdev.c
> @@ -475,9 +475,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();
> +            }

IMHO modifying the qdev.c code to workaround limitations of the test suite
is not a nice approach. Even if it is more work, I'd say it is better to
properly stub a /machine object in the test case, so that it complies with
expectations of qdev.c

> +
> +            object_property_add_child(container_get(root, "/unattached"),
>                                        name, obj);
>              unattached_parent = true;
>              g_free(name);

With regards,
Daniel
-- 
|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|
Re: [PATCH 3/5] qdev: Make device_set_realized() always safe in tests
Posted by Peter Xu 3 days, 21 hours ago
On Tue, Nov 19, 2024 at 09:46:35AM +0000, Daniel P. Berrangé wrote:
> On Mon, Nov 18, 2024 at 05:13:28PM -0500, Peter Xu wrote:
> > 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, which may not be expected when developing the test.
> > 
> > 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 single test case, or for any future test
> > cases when some device needs to be realized before the machine is present.
> > 
> > This shouldn't affect anything else when QEMU runs as an emulator, as that
> > always relies on a real machine being created before realizing any devices.
> > It's because if "/machine" is wrongly created as a container, it'll fail
> > QEMU very soon later on qemu_create_machine() trying to create the real
> > machine, conflicting with the "/machine" container.
> > 
> > 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 5f13111b77..eff297e584 100644
> > --- a/hw/core/qdev.c
> > +++ b/hw/core/qdev.c
> > @@ -475,9 +475,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();
> > +            }
> 
> IMHO modifying the qdev.c code to workaround limitations of the test suite
> is not a nice approach. Even if it is more work, I'd say it is better to
> properly stub a /machine object in the test case, so that it complies with
> expectations of qdev.c

Yeah I can give it a shot.

Meanwhile I just noticed that the assertion I added in the last patch may
be too strict, considering that "/machine" is actually not a container
object itself..  I think no test crashed because all such users used
qdev_get_machine() as the 1st parameter to container_get() to start the
walk, then the container_get() won't walk the "/machine" object itself, but
anything afterwards.

I still think it's possible some other objects got to be used as a
container even if it's not TYPE_CONTAINER, like the machine object.

So maybe.. what we really want is not "assert everything is a container",
as fundamentally every object "can" be a container itself.. supporting
childs in the properties.  What we really need might be that we never try
to silently create containers where it shouldn't..

I'll need to rethink about the series a bit.

-- 
Peter Xu