[Qemu-devel] [RFC PATCH 4/6] sysbus: Fix latent bug with onboard devices

Markus Armbruster posted 6 patches 6 years, 8 months ago
Maintainers: "Michael S. Tsirkin" <mst@redhat.com>, Marcel Apfelbaum <marcel.apfelbaum@gmail.com>, Paolo Bonzini <pbonzini@redhat.com>, Max Reitz <mreitz@redhat.com>, "Andreas Färber" <afaerber@suse.de>, Richard Henderson <rth@twiddle.net>, Kevin Wolf <kwolf@redhat.com>, Eduardo Habkost <ehabkost@redhat.com>
There is a newer version of this series
[Qemu-devel] [RFC PATCH 4/6] sysbus: Fix latent bug with onboard devices
Posted by Markus Armbruster 6 years, 8 months ago
The first call of sysbus_get_default() creates the main system bus and
stores it in QOM as "/machine/unattached/sysbus".  This must not
happen before main() creates "/machine", or else container_get() would
"helpfully" create it as "container" object, and the real creation of
"/machine" would later abort with "attempt to add duplicate property
'machine' to object (type 'container')".  Has been that way ever since
we wired up busses in QOM (commit f968fc6892d, v1.2.0).

I believe the bug is latent.  I got it to bite by trying to
qdev_create() a sysbus device from a machine's .instance_init()
method.

The fix is obvious: store the main system bus in QOM right after
creating "/machine".

Signed-off-by: Markus Armbruster <armbru@redhat.com>
---
 hw/core/sysbus.c | 3 ---
 vl.c             | 4 ++++
 2 files changed, 4 insertions(+), 3 deletions(-)

diff --git a/hw/core/sysbus.c b/hw/core/sysbus.c
index 9f9edbcab9..307cf90a51 100644
--- a/hw/core/sysbus.c
+++ b/hw/core/sysbus.c
@@ -357,9 +357,6 @@ static void main_system_bus_create(void)
     qbus_create_inplace(main_system_bus, system_bus_info.instance_size,
                         TYPE_SYSTEM_BUS, NULL, "main-system-bus");
     OBJECT(main_system_bus)->free = g_free;
-    object_property_add_child(container_get(qdev_get_machine(),
-                                            "/unattached"),
-                              "sysbus", OBJECT(main_system_bus), NULL);
 }
 
 BusState *sysbus_get_default(void)
diff --git a/vl.c b/vl.c
index e3fdce410f..6ce3d2d448 100644
--- a/vl.c
+++ b/vl.c
@@ -3990,6 +3990,10 @@ int main(int argc, char **argv, char **envp)
     }
     object_property_add_child(object_get_root(), "machine",
                               OBJECT(current_machine), &error_abort);
+    object_property_add_child(container_get(OBJECT(current_machine),
+                                            "/unattached"),
+                              "sysbus", OBJECT(sysbus_get_default()),
+                              NULL);
 
     if (machine_class->minimum_page_bits) {
         if (!set_preferred_target_page_bits(machine_class->minimum_page_bits)) {
-- 
2.17.2


Re: [Qemu-devel] [RFC PATCH 4/6] sysbus: Fix latent bug with onboard devices
Posted by Marc-André Lureau 6 years, 8 months ago
Hi

On Mon, Feb 25, 2019 at 7:40 PM Markus Armbruster <armbru@redhat.com> wrote:
>
> The first call of sysbus_get_default() creates the main system bus and
> stores it in QOM as "/machine/unattached/sysbus".  This must not
> happen before main() creates "/machine", or else container_get() would
> "helpfully" create it as "container" object, and the real creation of
> "/machine" would later abort with "attempt to add duplicate property
> 'machine' to object (type 'container')".  Has been that way ever since
> we wired up busses in QOM (commit f968fc6892d, v1.2.0).
>
> I believe the bug is latent.  I got it to bite by trying to
> qdev_create() a sysbus device from a machine's .instance_init()
> method.
>
> The fix is obvious: store the main system bus in QOM right after
> creating "/machine".
>
> Signed-off-by: Markus Armbruster <armbru@redhat.com>

makes sense to me, but I might miss some subtleties..

Reviewed-by: Marc-André Lureau <marcandre.lureau@redhat.com>

> ---
>  hw/core/sysbus.c | 3 ---
>  vl.c             | 4 ++++
>  2 files changed, 4 insertions(+), 3 deletions(-)
>
> diff --git a/hw/core/sysbus.c b/hw/core/sysbus.c
> index 9f9edbcab9..307cf90a51 100644
> --- a/hw/core/sysbus.c
> +++ b/hw/core/sysbus.c
> @@ -357,9 +357,6 @@ static void main_system_bus_create(void)
>      qbus_create_inplace(main_system_bus, system_bus_info.instance_size,
>                          TYPE_SYSTEM_BUS, NULL, "main-system-bus");
>      OBJECT(main_system_bus)->free = g_free;
> -    object_property_add_child(container_get(qdev_get_machine(),
> -                                            "/unattached"),
> -                              "sysbus", OBJECT(main_system_bus), NULL);
>  }
>
>  BusState *sysbus_get_default(void)
> diff --git a/vl.c b/vl.c
> index e3fdce410f..6ce3d2d448 100644
> --- a/vl.c
> +++ b/vl.c
> @@ -3990,6 +3990,10 @@ int main(int argc, char **argv, char **envp)
>      }
>      object_property_add_child(object_get_root(), "machine",
>                                OBJECT(current_machine), &error_abort);
> +    object_property_add_child(container_get(OBJECT(current_machine),
> +                                            "/unattached"),
> +                              "sysbus", OBJECT(sysbus_get_default()),
> +                              NULL);
>
>      if (machine_class->minimum_page_bits) {
>          if (!set_preferred_target_page_bits(machine_class->minimum_page_bits)) {
> --
> 2.17.2
>
>


-- 
Marc-André Lureau

Re: [Qemu-devel] [RFC PATCH 4/6] sysbus: Fix latent bug with onboard devices
Posted by Philippe Mathieu-Daudé 6 years, 8 months ago
On 2/25/19 7:37 PM, Markus Armbruster wrote:
> The first call of sysbus_get_default() creates the main system bus and
> stores it in QOM as "/machine/unattached/sysbus".  This must not
> happen before main() creates "/machine", or else container_get() would
> "helpfully" create it as "container" object, and the real creation of
> "/machine" would later abort with "attempt to add duplicate property
> 'machine' to object (type 'container')".  Has been that way ever since
> we wired up busses in QOM (commit f968fc6892d, v1.2.0).
> 
> I believe the bug is latent.  I got it to bite by trying to
> qdev_create() a sysbus device from a machine's .instance_init()
> method.
> 
> The fix is obvious: store the main system bus in QOM right after
> creating "/machine".
> 
> Signed-off-by: Markus Armbruster <armbru@redhat.com>
> ---
>  hw/core/sysbus.c | 3 ---
>  vl.c             | 4 ++++
>  2 files changed, 4 insertions(+), 3 deletions(-)
> 
> diff --git a/hw/core/sysbus.c b/hw/core/sysbus.c
> index 9f9edbcab9..307cf90a51 100644
> --- a/hw/core/sysbus.c
> +++ b/hw/core/sysbus.c
> @@ -357,9 +357,6 @@ static void main_system_bus_create(void)
>      qbus_create_inplace(main_system_bus, system_bus_info.instance_size,
>                          TYPE_SYSTEM_BUS, NULL, "main-system-bus");
>      OBJECT(main_system_bus)->free = g_free;
> -    object_property_add_child(container_get(qdev_get_machine(),
> -                                            "/unattached"),
> -                              "sysbus", OBJECT(main_system_bus), NULL);
>  }
>  
>  BusState *sysbus_get_default(void)
> diff --git a/vl.c b/vl.c
> index e3fdce410f..6ce3d2d448 100644
> --- a/vl.c
> +++ b/vl.c
> @@ -3990,6 +3990,10 @@ int main(int argc, char **argv, char **envp)
>      }
>      object_property_add_child(object_get_root(), "machine",
>                                OBJECT(current_machine), &error_abort);
> +    object_property_add_child(container_get(OBJECT(current_machine),
> +                                            "/unattached"),
> +                              "sysbus", OBJECT(sysbus_get_default()),
> +                              NULL);

If SYSTEM_BUS doesn't exist, sysbus_get_default() creates it.

Reviewed-by: Philippe Mathieu-Daudé <philmd@redhat.com>

>  
>      if (machine_class->minimum_page_bits) {
>          if (!set_preferred_target_page_bits(machine_class->minimum_page_bits)) {
> 

Re: [Qemu-devel] [RFC PATCH 4/6] sysbus: Fix latent bug with onboard devices
Posted by Thomas Huth 6 years, 8 months ago
On 25/02/2019 19.37, Markus Armbruster wrote:
> The first call of sysbus_get_default() creates the main system bus and
> stores it in QOM as "/machine/unattached/sysbus".  This must not
> happen before main() creates "/machine", or else container_get() would
> "helpfully" create it as "container" object, and the real creation of
> "/machine" would later abort with "attempt to add duplicate property
> 'machine' to object (type 'container')".  Has been that way ever since
> we wired up busses in QOM (commit f968fc6892d, v1.2.0).
> 
> I believe the bug is latent.  I got it to bite by trying to
> qdev_create() a sysbus device from a machine's .instance_init()
> method.
> 
> The fix is obvious: store the main system bus in QOM right after
> creating "/machine".
> 
> Signed-off-by: Markus Armbruster <armbru@redhat.com>
> ---
>  hw/core/sysbus.c | 3 ---
>  vl.c             | 4 ++++
>  2 files changed, 4 insertions(+), 3 deletions(-)
> 
> diff --git a/hw/core/sysbus.c b/hw/core/sysbus.c
> index 9f9edbcab9..307cf90a51 100644
> --- a/hw/core/sysbus.c
> +++ b/hw/core/sysbus.c
> @@ -357,9 +357,6 @@ static void main_system_bus_create(void)
>      qbus_create_inplace(main_system_bus, system_bus_info.instance_size,
>                          TYPE_SYSTEM_BUS, NULL, "main-system-bus");
>      OBJECT(main_system_bus)->free = g_free;
> -    object_property_add_child(container_get(qdev_get_machine(),
> -                                            "/unattached"),
> -                              "sysbus", OBJECT(main_system_bus), NULL);
>  }
>  
>  BusState *sysbus_get_default(void)
> diff --git a/vl.c b/vl.c
> index e3fdce410f..6ce3d2d448 100644
> --- a/vl.c
> +++ b/vl.c
> @@ -3990,6 +3990,10 @@ int main(int argc, char **argv, char **envp)
>      }
>      object_property_add_child(object_get_root(), "machine",
>                                OBJECT(current_machine), &error_abort);
> +    object_property_add_child(container_get(OBJECT(current_machine),
> +                                            "/unattached"),
> +                              "sysbus", OBJECT(sysbus_get_default()),
> +                              NULL);
>  
>      if (machine_class->minimum_page_bits) {
>          if (!set_preferred_target_page_bits(machine_class->minimum_page_bits)) {
> 

Looks right. Especially, a device should also not add itself to a
parent, so this definitely should not be done in sysbus.c

Reviewed-by: Thomas Huth <thuth@redhat.com>


PS: Not directly related to your patch, but in a separate patch we
should also object_unref(current_machine) here to drop the superfluous
second reference to current_machine after we added it as a child of the
root object.

Re: [Qemu-devel] [RFC PATCH 4/6] sysbus: Fix latent bug with onboard devices
Posted by Markus Armbruster 6 years, 8 months ago
Thomas Huth <thuth@redhat.com> writes:

> On 25/02/2019 19.37, Markus Armbruster wrote:
>> The first call of sysbus_get_default() creates the main system bus and
>> stores it in QOM as "/machine/unattached/sysbus".  This must not
>> happen before main() creates "/machine", or else container_get() would
>> "helpfully" create it as "container" object, and the real creation of
>> "/machine" would later abort with "attempt to add duplicate property
>> 'machine' to object (type 'container')".  Has been that way ever since
>> we wired up busses in QOM (commit f968fc6892d, v1.2.0).
>> 
>> I believe the bug is latent.  I got it to bite by trying to
>> qdev_create() a sysbus device from a machine's .instance_init()
>> method.
>> 
>> The fix is obvious: store the main system bus in QOM right after
>> creating "/machine".
>> 
>> Signed-off-by: Markus Armbruster <armbru@redhat.com>
>> ---
>>  hw/core/sysbus.c | 3 ---
>>  vl.c             | 4 ++++
>>  2 files changed, 4 insertions(+), 3 deletions(-)
>> 
>> diff --git a/hw/core/sysbus.c b/hw/core/sysbus.c
>> index 9f9edbcab9..307cf90a51 100644
>> --- a/hw/core/sysbus.c
>> +++ b/hw/core/sysbus.c
>> @@ -357,9 +357,6 @@ static void main_system_bus_create(void)
>>      qbus_create_inplace(main_system_bus, system_bus_info.instance_size,
>>                          TYPE_SYSTEM_BUS, NULL, "main-system-bus");
>>      OBJECT(main_system_bus)->free = g_free;
>> -    object_property_add_child(container_get(qdev_get_machine(),
>> -                                            "/unattached"),
>> -                              "sysbus", OBJECT(main_system_bus), NULL);
>>  }
>>  
>>  BusState *sysbus_get_default(void)
>> diff --git a/vl.c b/vl.c
>> index e3fdce410f..6ce3d2d448 100644
>> --- a/vl.c
>> +++ b/vl.c
>> @@ -3990,6 +3990,10 @@ int main(int argc, char **argv, char **envp)
>>      }
>>      object_property_add_child(object_get_root(), "machine",
>>                                OBJECT(current_machine), &error_abort);
>> +    object_property_add_child(container_get(OBJECT(current_machine),
>> +                                            "/unattached"),
>> +                              "sysbus", OBJECT(sysbus_get_default()),
>> +                              NULL);
>>  
>>      if (machine_class->minimum_page_bits) {
>>          if (!set_preferred_target_page_bits(machine_class->minimum_page_bits)) {
>> 
>
> Looks right. Especially, a device should also not add itself to a
> parent, so this definitely should not be done in sysbus.c
>
> Reviewed-by: Thomas Huth <thuth@redhat.com>
>
>
> PS: Not directly related to your patch, but in a separate patch we
> should also object_unref(current_machine) here to drop the superfluous
> second reference to current_machine after we added it as a child of the
> root object.

Just for cleanliness.  Makes sense.

Thanks!

Re: [Qemu-devel] [RFC PATCH 4/6] sysbus: Fix latent bug with onboard devices
Posted by Thomas Huth 6 years, 8 months ago
On 05/03/2019 07.54, Markus Armbruster wrote:
> Thomas Huth <thuth@redhat.com> writes:
> 
>> On 25/02/2019 19.37, Markus Armbruster wrote:
[...]
>>> diff --git a/vl.c b/vl.c
>>> index e3fdce410f..6ce3d2d448 100644
>>> --- a/vl.c
>>> +++ b/vl.c
>>> @@ -3990,6 +3990,10 @@ int main(int argc, char **argv, char **envp)
>>>      }
>>>      object_property_add_child(object_get_root(), "machine",
>>>                                OBJECT(current_machine), &error_abort);
[...]
>>
>> PS: Not directly related to your patch, but in a separate patch we
>> should also object_unref(current_machine) here to drop the superfluous
>> second reference to current_machine after we added it as a child of the
>> root object.
> 
> Just for cleanliness.  Makes sense.

Not only for cleanliness ... there is a TODO at the very end of vl.c
which we should fix one day, and for that we'd need the unref here.

 Thomas