qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
* [Qemu-devel] [PATCH] qdev_try_create(): Assert that devices we put onto the system bus are SysBusDevices
@ 2016-03-16 14:11 Peter Maydell
  2016-03-16 14:14 ` Andreas Färber
  0 siblings, 1 reply; 4+ messages in thread
From: Peter Maydell @ 2016-03-16 14:11 UTC (permalink / raw)
  To: qemu-devel; +Cc: Thomas Hanson, Andreas Färber

If qdev_try_create() is passed NULL for the bus, it will automatically
put the newly created device onto the default system bus. However
if the device is not actually a SysBusDevice then this will result
in later crashes (for instance when running the monitor "info qtree"
command) because code reasonably assumes that all devices on the system
bus are system bus devices.

Generally the mistake is that the calling code should create the
object with object_new(TYPE_FOO) rather than qdev_create(NULL, TYPE_FOO);
see commit 6749695eaaf346c1 for an example of fixing this bug.

Assert in qdev_try_create() if the device isn't suitable to put on
the system bus, so that this mistake results in failure earlier
and more reliably.

Signed-off-by: Peter Maydell <peter.maydell@linaro.org>
---
This needs to go in after http://patchwork.ozlabs.org/patch/597716/
as otherwise the bug fixed by that patch will become a 'make check'
failure.

 hw/core/qdev.c | 7 +++++++
 1 file changed, 7 insertions(+)

diff --git a/hw/core/qdev.c b/hw/core/qdev.c
index db41aa1..fb7db86 100644
--- a/hw/core/qdev.c
+++ b/hw/core/qdev.c
@@ -35,6 +35,7 @@
 #include "qemu/error-report.h"
 #include "hw/hotplug.h"
 #include "hw/boards.h"
+#include "hw/sysbus.h"
 #include "qapi-event.h"
 
 int qdev_hotplug = 0;
@@ -161,6 +162,12 @@ DeviceState *qdev_try_create(BusState *bus, const char *type)
     }
 
     if (!bus) {
+        /* Assert that the device really is a SysBusDevice before
+         * we put it onto the sysbus. (Non-sysbus devices which aren't
+         * being put onto a bus should be created with object_new(TYPE_FOO),
+         * not qdev_create(NULL, TYPE_FOO).)
+         */
+        SYS_BUS_DEVICE(dev);
         bus = sysbus_get_default();
     }
 
-- 
1.9.1

^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [Qemu-devel] [PATCH] qdev_try_create(): Assert that devices we put onto the system bus are SysBusDevices
  2016-03-16 14:11 [Qemu-devel] [PATCH] qdev_try_create(): Assert that devices we put onto the system bus are SysBusDevices Peter Maydell
@ 2016-03-16 14:14 ` Andreas Färber
  2016-03-16 14:25   ` Peter Maydell
  0 siblings, 1 reply; 4+ messages in thread
From: Andreas Färber @ 2016-03-16 14:14 UTC (permalink / raw)
  To: Peter Maydell, qemu-devel; +Cc: Thomas Hanson

Am 16.03.2016 um 15:11 schrieb Peter Maydell:
> If qdev_try_create() is passed NULL for the bus, it will automatically
> put the newly created device onto the default system bus. However
> if the device is not actually a SysBusDevice then this will result
> in later crashes (for instance when running the monitor "info qtree"
> command) because code reasonably assumes that all devices on the system
> bus are system bus devices.
> 
> Generally the mistake is that the calling code should create the
> object with object_new(TYPE_FOO) rather than qdev_create(NULL, TYPE_FOO);
> see commit 6749695eaaf346c1 for an example of fixing this bug.
> 
> Assert in qdev_try_create() if the device isn't suitable to put on
> the system bus, so that this mistake results in failure earlier
> and more reliably.
> 
> Signed-off-by: Peter Maydell <peter.maydell@linaro.org>
> ---
> This needs to go in after http://patchwork.ozlabs.org/patch/597716/
> as otherwise the bug fixed by that patch will become a 'make check'
> failure.

Looks strange, but okay,

Reviewed-by: Andreas Färber <afaerber@suse.de>

Through whose queue?

Thanks,
Andreas

-- 
SUSE Linux GmbH, Maxfeldstr. 5, 90409 Nürnberg, Germany
GF: Felix Imendörffer, Jane Smithard, Graham Norton; HRB 21284 (AG Nürnberg)

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [Qemu-devel] [PATCH] qdev_try_create(): Assert that devices we put onto the system bus are SysBusDevices
  2016-03-16 14:14 ` Andreas Färber
@ 2016-03-16 14:25   ` Peter Maydell
  2016-03-16 16:17     ` Paolo Bonzini
  0 siblings, 1 reply; 4+ messages in thread
From: Peter Maydell @ 2016-03-16 14:25 UTC (permalink / raw)
  To: Andreas Färber; +Cc: Thomas Hanson, QEMU Developers

On 16 March 2016 at 14:14, Andreas Färber <afaerber@suse.de> wrote:
> Am 16.03.2016 um 15:11 schrieb Peter Maydell:
>> If qdev_try_create() is passed NULL for the bus, it will automatically
>> put the newly created device onto the default system bus. However
>> if the device is not actually a SysBusDevice then this will result
>> in later crashes (for instance when running the monitor "info qtree"
>> command) because code reasonably assumes that all devices on the system
>> bus are system bus devices.
>>
>> Generally the mistake is that the calling code should create the
>> object with object_new(TYPE_FOO) rather than qdev_create(NULL, TYPE_FOO);
>> see commit 6749695eaaf346c1 for an example of fixing this bug.
>>
>> Assert in qdev_try_create() if the device isn't suitable to put on
>> the system bus, so that this mistake results in failure earlier
>> and more reliably.
>>
>> Signed-off-by: Peter Maydell <peter.maydell@linaro.org>
>> ---
>> This needs to go in after http://patchwork.ozlabs.org/patch/597716/
>> as otherwise the bug fixed by that patch will become a 'make check'
>> failure.
>
> Looks strange, but okay,

You mean the way we use what looks like a cast macro and ignore
the result? Yeah, I thought that was a little odd-looking. Happy
to do it some other way if you have an alternative suggestion.

> Reviewed-by: Andreas Färber <afaerber@suse.de>
>
> Through whose queue?

I'm happy to take it via the target-arm queue, since I was planning
to take the sd.c fix that way and this one needs to go after it,
if that works for you.

thanks
-- PMM

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [Qemu-devel] [PATCH] qdev_try_create(): Assert that devices we put onto the system bus are SysBusDevices
  2016-03-16 14:25   ` Peter Maydell
@ 2016-03-16 16:17     ` Paolo Bonzini
  0 siblings, 0 replies; 4+ messages in thread
From: Paolo Bonzini @ 2016-03-16 16:17 UTC (permalink / raw)
  To: Peter Maydell, Andreas Färber; +Cc: Thomas Hanson, QEMU Developers



On 16/03/2016 15:25, Peter Maydell wrote:
> > Looks strange, but okay,
> You mean the way we use what looks like a cast macro and ignore
> the result? Yeah, I thought that was a little odd-looking. Happy
> to do it some other way if you have an alternative suggestion.

Perhaps this:

g_assert(object_dynamic_cast(OBJECT(dev), TYPE_SYS_BUS_DEVICE) != NULL);

or just add a (void) in front of the cast.

Paolo

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2016-03-16 16:18 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2016-03-16 14:11 [Qemu-devel] [PATCH] qdev_try_create(): Assert that devices we put onto the system bus are SysBusDevices Peter Maydell
2016-03-16 14:14 ` Andreas Färber
2016-03-16 14:25   ` Peter Maydell
2016-03-16 16:17     ` Paolo Bonzini

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).