Skip to contentSkip to conversation

hw/arm/bcm283x: Fix crash with device_add bcm2837 on unsupported machines

PATCH In review Thomas Huth 5 participants 33 messages Jul 9, 2018 – Jul 16, 2018

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 hw/arm/bcm283x: Fix crash with device_add bcm2837 on unsupported machines

When trying to "device_add bcm2837" on a machine that is not suitable for this device, you can quickly crash QEMU afterwards, e.g. with "info qtree":

echo "{'execute':'qmp_capabilities'} {'execute':'device_add', " \
"'arguments':{'driver':'bcm2837'}} {'execute': 'human-monitor-command', " \
"'arguments': {'command-line': 'info qtree'}}" | \
aarch64-softmmu/qemu-system-aarch64 -M integratorcp,accel=qtest -S -qmp stdio

{"QMP": {"version": {"qemu": {"micro": 50, "minor": 12, "major": 2},
"package": "build-all"}, "capabilities": []}}
{"return": {}}
{"error": {"class": "GenericError", "desc": "Device 'bcm2837' can not be
hotplugged on this machine"}}
Segmentation fault (core dumped)

The problem is that qdev_set_parent_bus() from instance_init adds a link to the child devices which is not valid anymore after the device init failed. Thus the qdev_set_parent_bus() must rather be done in the realize function instead.

---
 hw/arm/bcm2835_peripherals.c | 25 ++++++++++++++-----------
 1 file changed, 14 insertions(+), 11 deletions(-)
hw/arm/bcm2835_peripherals.c +14 -11
index 6be7660..4724a29 100644
@@ -43,22 +43,18 @@ static void bcm2835_peripherals_init(Object *obj)
     /* Interrupt Controller */
     object_initialize(&s->ic, sizeof(s->ic), TYPE_BCM2835_IC);
     object_property_add_child(obj, "ic", OBJECT(&s->ic), NULL);
-    qdev_set_parent_bus(DEVICE(&s->ic), sysbus_get_default());
 
     /* UART0 */
     s->uart0 = SYS_BUS_DEVICE(object_new("pl011"));
     object_property_add_child(obj, "uart0", OBJECT(s->uart0), NULL);
-    qdev_set_parent_bus(DEVICE(s->uart0), sysbus_get_default());
 
     /* AUX / UART1 */
     object_initialize(&s->aux, sizeof(s->aux), TYPE_BCM2835_AUX);
     object_property_add_child(obj, "aux", OBJECT(&s->aux), NULL);
-    qdev_set_parent_bus(DEVICE(&s->aux), sysbus_get_default());
 
     /* Mailboxes */
     object_initialize(&s->mboxes, sizeof(s->mboxes), TYPE_BCM2835_MBOX);
     object_property_add_child(obj, "mbox", OBJECT(&s->mboxes), NULL);
-    qdev_set_parent_bus(DEVICE(&s->mboxes), sysbus_get_default());
 
     object_property_add_const_link(OBJECT(&s->mboxes), "mbox-mr",
                                    OBJECT(&s->mbox_mr), &error_abort);
@@ -68,7 +64,6 @@ static void bcm2835_peripherals_init(Object *obj)
     object_property_add_child(obj, "fb", OBJECT(&s->fb), NULL);
     object_property_add_alias(obj, "vcram-size", OBJECT(&s->fb), "vcram-size",
                               &error_abort);
-    qdev_set_parent_bus(DEVICE(&s->fb), sysbus_get_default());
 
     object_property_add_const_link(OBJECT(&s->fb), "dma-mr",
                                    OBJECT(&s->gpu_bus_mr), &error_abort);
@@ -78,7 +73,6 @@ static void bcm2835_peripherals_init(Object *obj)
     object_property_add_child(obj, "property", OBJECT(&s->property), NULL);
     object_property_add_alias(obj, "board-rev", OBJECT(&s->property),
                               "board-rev", &error_abort);
-    qdev_set_parent_bus(DEVICE(&s->property), sysbus_get_default());
 
     object_property_add_const_link(OBJECT(&s->property), "fb",
                                    OBJECT(&s->fb), &error_abort);
@@ -88,22 +82,18 @@ static void bcm2835_peripherals_init(Object *obj)
     /* Random Number Generator */
     object_initialize(&s->rng, sizeof(s->rng), TYPE_BCM2835_RNG);
     object_property_add_child(obj, "rng", OBJECT(&s->rng), NULL);
-    qdev_set_parent_bus(DEVICE(&s->rng), sysbus_get_default());
 
     /* Extended Mass Media Controller */
     object_initialize(&s->sdhci, sizeof(s->sdhci), TYPE_SYSBUS_SDHCI);
     object_property_add_child(obj, "sdhci", OBJECT(&s->sdhci), NULL);
-    qdev_set_parent_bus(DEVICE(&s->sdhci), sysbus_get_default());
 
     /* SDHOST */
     object_initialize(&s->sdhost, sizeof(s->sdhost), TYPE_BCM2835_SDHOST);
     object_property_add_child(obj, "sdhost", OBJECT(&s->sdhost), NULL);
-    qdev_set_parent_bus(DEVICE(&s->sdhost), sysbus_get_default());
 
     /* DMA Channels */
     object_initialize(&s->dma, sizeof(s->dma), TYPE_BCM2835_DMA);
     object_property_add_child(obj, "dma", OBJECT(&s->dma), NULL);
-    qdev_set_parent_bus(DEVICE(&s->dma), sysbus_get_default());
 
     object_property_add_const_link(OBJECT(&s->dma), "dma-mr",
                                    OBJECT(&s->gpu_bus_mr), &error_abort);
@@ -111,7 +101,6 @@ static void bcm2835_peripherals_init(Object *obj)
     /* GPIO */
     object_initialize(&s->gpio, sizeof(s->gpio), TYPE_BCM2835_GPIO);
     object_property_add_child(obj, "gpio", OBJECT(&s->gpio), NULL);
-    qdev_set_parent_bus(DEVICE(&s->gpio), sysbus_get_default());
 
     object_property_add_const_link(OBJECT(&s->gpio), "sdbus-sdhci",
                                    OBJECT(&s->sdhci.sdbus), &error_abort);
@@ -126,8 +115,22 @@ static void bcm2835_peripherals_realize(DeviceState *dev, Error **errp)
     MemoryRegion *ram;
     Error *err = NULL;
     uint64_t ram_size, vcram_size;
+    BusState *sysbus;
     int n;
 
+    sysbus = sysbus_get_default();
+    qdev_set_parent_bus(DEVICE(&s->ic), sysbus);
+    qdev_set_parent_bus(DEVICE(s->uart0), sysbus);
+    qdev_set_parent_bus(DEVICE(&s->aux), sysbus);
+    qdev_set_parent_bus(DEVICE(&s->mboxes), sysbus);
+    qdev_set_parent_bus(DEVICE(&s->fb), sysbus);
+    qdev_set_parent_bus(DEVICE(&s->property), sysbus);
+    qdev_set_parent_bus(DEVICE(&s->rng), sysbus);
+    qdev_set_parent_bus(DEVICE(&s->sdhci), sysbus);
+    qdev_set_parent_bus(DEVICE(&s->sdhost), sysbus);
+    qdev_set_parent_bus(DEVICE(&s->dma), sysbus);
+    qdev_set_parent_bus(DEVICE(&s->gpio), sysbus);
+
     obj = object_property_get_link(OBJECT(dev), "ram", &err);
     if (obj == NULL) {
         error_setg(errp, "%s: required ram link not found: %s",
Signature · 1 line
1.8.3.1
#1 lore raw
Quoted 7 lines · Thomas Huth

When trying to "device_add bcm2837" on a machine that is not suitable for this device, you can quickly crash QEMU afterwards, e.g. with "info qtree":

echo "{'execute':'qmp_capabilities'} {'execute':'device_add', " \
"'arguments':{'driver':'bcm2837'}} {'execute': 'human-monitor-command', " \
"'arguments': {'command-line': 'info qtree'}}" | \
aarch64-softmmu/qemu-system-aarch64 -M integratorcp,accel=qtest -S -qmp stdio

Interesting, how did you find this bug?

Running "info qtree" and other queries on device-crash-test sounds like a good idea. I will add it to my TODO list.

Quoted 14 lines

{"QMP": {"version": {"qemu": {"micro": 50, "minor": 12, "major": 2},
"package": "build-all"}, "capabilities": []}}
{"return": {}}
{"error": {"class": "GenericError", "desc": "Device 'bcm2837' can not be
hotplugged on this machine"}}
Segmentation fault (core dumped)

The problem is that qdev_set_parent_bus() from instance_init adds a link to the child devices which is not valid anymore after the device init failed. Thus the qdev_set_parent_bus() must rather be done in the realize function instead.

[...]

Signature · 1 line
Eduardo
#2 lore raw
Quoted 10 lines · Eduardo Habkost
Quoted 7 lines · Thomas Huth

When trying to "device_add bcm2837" on a machine that is not suitable for this device, you can quickly crash QEMU afterwards, e.g. with "info qtree":

echo "{'execute':'qmp_capabilities'} {'execute':'device_add', " \
"'arguments':{'driver':'bcm2837'}} {'execute': 'human-monitor-command', " \
"'arguments': {'command-line': 'info qtree'}}" | \
aarch64-softmmu/qemu-system-aarch64 -M integratorcp,accel=qtest -S -qmp stdio

Interesting, how did you find this bug?

I was running some tests with an enhanced version of this patch applied:

http://lists.gnu.org/archive/html/qemu-devel/2018-03/msg05033.html

Thomas

#3 lore raw
In reply to Thomas Huth
Quoted 15 lines · Thomas Huth

When trying to "device_add bcm2837" on a machine that is not suitable for this device, you can quickly crash QEMU afterwards, e.g. with "info qtree":

echo "{'execute':'qmp_capabilities'} {'execute':'device_add', " \
"'arguments':{'driver':'bcm2837'}} {'execute': 'human-monitor-command', " \
"'arguments': {'command-line': 'info qtree'}}" | \
aarch64-softmmu/qemu-system-aarch64 -M integratorcp,accel=qtest -S -qmp stdio

{"QMP": {"version": {"qemu": {"micro": 50, "minor": 12, "major": 2},
"package": "build-all"}, "capabilities": []}}
{"return": {}}
{"error": {"class": "GenericError", "desc": "Device 'bcm2837' can not be
hotplugged on this machine"}}
Segmentation fault (core dumped)

Quoted 4 lines

The problem is that qdev_set_parent_bus() from instance_init adds a link to the child devices which is not valid anymore after the device init failed. Thus the qdev_set_parent_bus() must rather be done in the realize function instead.

Yuck. The real problem here is that we're still requiring the code that creates these QOM devices to manually set the parent in the first place. It's not surprising that we don't get it right (either parenting in the wrong place or not at all). I'd much rather see us fix that properly than keep papering over places where we get it wrong.

Also, "device_add bcm2837" is just nonsensical: there's no way to create an SoC device like this with device_add. We should not allow it (for this and all the other devices that device_add will never work with)...

thanks
-- PMM

#4 lore raw
Quoted 21 lines · Peter Maydell
Quoted 15 lines · Thomas Huth

When trying to "device_add bcm2837" on a machine that is not suitable for this device, you can quickly crash QEMU afterwards, e.g. with "info qtree":

echo "{'execute':'qmp_capabilities'} {'execute':'device_add', " \
"'arguments':{'driver':'bcm2837'}} {'execute': 'human-monitor-command', " \
"'arguments': {'command-line': 'info qtree'}}" | \
aarch64-softmmu/qemu-system-aarch64 -M integratorcp,accel=qtest -S -qmp stdio

{"QMP": {"version": {"qemu": {"micro": 50, "minor": 12, "major": 2},
"package": "build-all"}, "capabilities": []}}
{"return": {}}
{"error": {"class": "GenericError", "desc": "Device 'bcm2837' can not be
hotplugged on this machine"}}
Segmentation fault (core dumped)

Quoted 4 lines

The problem is that qdev_set_parent_bus() from instance_init adds a link to the child devices which is not valid anymore after the device init failed. Thus the qdev_set_parent_bus() must rather be done in the realize function instead.

Quoted 6 lines

Yuck. The real problem here is that we're still requiring the code that creates these QOM devices to manually set the parent in the first place. It's not surprising that we don't get it right (either parenting in the wrong place or not at all). I'd much rather see us fix that properly than keep papering over places where we get it wrong.

Sorry, I'm still not an expert in all this QOM stuff yet ... so what do you exactly recommend to do instead?

Quoted 4 lines

Also, "device_add bcm2837" is just nonsensical: there's no way to create an SoC device like this with device_add. We should not allow it (for this and all the other devices that device_add will never work with)...

I'm fine if we additionally add a user_creatable = false here, but the problem then still persists, e.g. with :

echo "{'execute':'qmp_capabilities'} " \
"{'execute':'device-list-properties', 'arguments': " \
"{'typename':'bcm2837'}} {'execute': 'human-monitor-command', " \
"'arguments': {'command-line': 'info qtree'}}" | valgrind -q \
aarch64-softmmu/qemu-system-aarch64 -M integratorcp -S -qmp stdio

The same problem also exists for some other devices which have already been marked with user_creatable = false, e.g.:

echo "{'execute':'qmp_capabilities'} " \
"{'execute':'device-list-properties', " \
"'arguments':{'typename':'macio-newworld'}} " \
"{'execute': 'human-monitor-command', 'arguments': " \
"{'command-line': 'info qtree'}}" | valgrind -q \
ppc64-softmmu/qemu-system-ppc64 -M mac99 -S -qmp stdio

Hmm, maybe we need a qtest that first executes "info qtree", then runs 'device-list-properties' for all devices and finally checks "info qtree" again ... since 'device-list-properties' should not change the qtree, the output of the "info qtree" should be the same. Currently this is not the case :-/

Thomas

#5 lore raw
Quoted 29 lines · Thomas Huth
Quoted 21 lines · Peter Maydell
Quoted 15 lines · Thomas Huth

When trying to "device_add bcm2837" on a machine that is not suitable for this device, you can quickly crash QEMU afterwards, e.g. with "info qtree":

echo "{'execute':'qmp_capabilities'} {'execute':'device_add', " \
"'arguments':{'driver':'bcm2837'}} {'execute': 'human-monitor-command', " \
"'arguments': {'command-line': 'info qtree'}}" | \
aarch64-softmmu/qemu-system-aarch64 -M integratorcp,accel=qtest -S -qmp stdio

{"QMP": {"version": {"qemu": {"micro": 50, "minor": 12, "major": 2},
"package": "build-all"}, "capabilities": []}}
{"return": {}}
{"error": {"class": "GenericError", "desc": "Device 'bcm2837' can not be
hotplugged on this machine"}}
Segmentation fault (core dumped)

Quoted 4 lines

The problem is that qdev_set_parent_bus() from instance_init adds a link to the child devices which is not valid anymore after the device init failed. Thus the qdev_set_parent_bus() must rather be done in the realize function instead.

Quoted 6 lines

Yuck. The real problem here is that we're still requiring the code that creates these QOM devices to manually set the parent in the first place. It's not surprising that we don't get it right (either parenting in the wrong place or not at all). I'd much rather see us fix that properly than keep papering over places where we get it wrong.

Quoted 2 lines

Sorry, I'm still not an expert in all this QOM stuff yet ... so what do you exactly recommend to do instead?

I'm not clear either, but I don't think that what we're currently doing can be right.

thanks
-- PMM

#6 lore raw
Quoted 30 lines · Peter Maydell
Quoted 29 lines · Thomas Huth
Quoted 27 lines · Peter Maydell
Quoted 15 lines · Thomas Huth

When trying to "device_add bcm2837" on a machine that is not suitable for this device, you can quickly crash QEMU afterwards, e.g. with "info qtree":

echo "{'execute':'qmp_capabilities'} {'execute':'device_add', " \
"'arguments':{'driver':'bcm2837'}} {'execute': 'human-monitor-command', " \
"'arguments': {'command-line': 'info qtree'}}" | \
aarch64-softmmu/qemu-system-aarch64 -M integratorcp,accel=qtest -S -qmp stdio

{"QMP": {"version": {"qemu": {"micro": 50, "minor": 12, "major": 2},
"package": "build-all"}, "capabilities": []}}
{"return": {}}
{"error": {"class": "GenericError", "desc": "Device 'bcm2837' can not be
hotplugged on this machine"}}
Segmentation fault (core dumped)

Quoted 4 lines

The problem is that qdev_set_parent_bus() from instance_init adds a link to the child devices which is not valid anymore after the device init failed. Thus the qdev_set_parent_bus() must rather be done in the realize function instead.

Yuck. The real problem here is that we're still requiring the code that creates these QOM devices to manually set the parent in the first place. It's not surprising that we don't get it right (either parenting in the wrong place or not at all). I'd much rather see us fix that properly than keep papering over places where we get it wrong.

Quoted 5 lines
Quoted 2 lines

Sorry, I'm still not an expert in all this QOM stuff yet ... so what do you exactly recommend to do instead?

I'm not clear either, but I don't think that what we're currently doing can be right.

Hm, ok, so how to continue here now? Shall we at least mark the bcm2836/7 devices with user_creatable=false, so that users can not crash their QEMU so easily with device_add? The problem with introspection via device-list-properties would still continue to exist, but I think that's less likely used in practice... otherwise we could still move the qdev_set_parent_bus() calls to the realize() function instead, and just add a big fat FIXME comment in front of the code block, so that we remember to clean that up one day...

Thomas

#7 lore raw
Quoted 37 lines · Thomas Huth
Quoted 30 lines · Peter Maydell
Quoted 29 lines · Thomas Huth
Quoted 27 lines · Peter Maydell
Quoted 15 lines · Thomas Huth

When trying to "device_add bcm2837" on a machine that is not suitable for this device, you can quickly crash QEMU afterwards, e.g. with "info qtree":

echo "{'execute':'qmp_capabilities'} {'execute':'device_add', " \
"'arguments':{'driver':'bcm2837'}} {'execute': 'human-monitor-command', " \
"'arguments': {'command-line': 'info qtree'}}" | \
aarch64-softmmu/qemu-system-aarch64 -M integratorcp,accel=qtest -S -qmp stdio

{"QMP": {"version": {"qemu": {"micro": 50, "minor": 12, "major": 2},
"package": "build-all"}, "capabilities": []}}
{"return": {}}
{"error": {"class": "GenericError", "desc": "Device 'bcm2837' can not be
hotplugged on this machine"}}
Segmentation fault (core dumped)

Quoted 4 lines

The problem is that qdev_set_parent_bus() from instance_init adds a link to the child devices which is not valid anymore after the device init failed. Thus the qdev_set_parent_bus() must rather be done in the realize function instead.

Yuck. The real problem here is that we're still requiring the code that creates these QOM devices to manually set the parent in the first place. It's not surprising that we don't get it right (either parenting in the wrong place or not at all). I'd much rather see us fix that properly than keep papering over places where we get it wrong.

Quoted 5 lines
Quoted 2 lines

Sorry, I'm still not an expert in all this QOM stuff yet ... so what do you exactly recommend to do instead?

I'm not clear either, but I don't think that what we're currently doing can be right.

Quoted 8 lines

Hm, ok, so how to continue here now? Shall we at least mark the bcm2836/7 devices with user_creatable=false, so that users can not crash their QEMU so easily with device_add? The problem with introspection via device-list-properties would still continue to exist, but I think that's less likely used in practice... otherwise we could still move the qdev_set_parent_bus() calls to the realize() function instead, and just add a big fat FIXME comment in front of the code block, so that we remember to clean that up one day...

Crashing device-list-properties should be a blocker bug, IMO.

Moving to realize is not the best solution, but I would prefer to do that in 3.0 instead of leaving the device-list-properties crash unfixed.

Another solution is to reintroduce DeviceClass::cannot_destroy_with_object_finalize_yet (commit 08f00df4f4b8b4e38ad620477cc90cf5f73832d9), and set cannot_destroy_with_object_finalize_yet=true on bcm2837.

Signature · 1 line
Eduardo
#8 lore raw
Quoted 47 lines · Eduardo Habkost
Quoted 37 lines · Thomas Huth
Quoted 30 lines · Peter Maydell
Quoted 29 lines · Thomas Huth
Quoted 27 lines · Peter Maydell
Quoted 15 lines · Thomas Huth

When trying to "device_add bcm2837" on a machine that is not suitable for this device, you can quickly crash QEMU afterwards, e.g. with "info qtree":

echo "{'execute':'qmp_capabilities'} {'execute':'device_add', " \
"'arguments':{'driver':'bcm2837'}} {'execute': 'human-monitor-command', " \
"'arguments': {'command-line': 'info qtree'}}" | \
aarch64-softmmu/qemu-system-aarch64 -M integratorcp,accel=qtest -S -qmp stdio

{"QMP": {"version": {"qemu": {"micro": 50, "minor": 12, "major": 2},
"package": "build-all"}, "capabilities": []}}
{"return": {}}
{"error": {"class": "GenericError", "desc": "Device 'bcm2837' can not be
hotplugged on this machine"}}
Segmentation fault (core dumped)

Quoted 4 lines

The problem is that qdev_set_parent_bus() from instance_init adds a link to the child devices which is not valid anymore after the device init failed. Thus the qdev_set_parent_bus() must rather be done in the realize function instead.

Yuck. The real problem here is that we're still requiring the code that creates these QOM devices to manually set the parent in the first place. It's not surprising that we don't get it right (either parenting in the wrong place or not at all). I'd much rather see us fix that properly than keep papering over places where we get it wrong.

Quoted 5 lines
Quoted 2 lines

Sorry, I'm still not an expert in all this QOM stuff yet ... so what do you exactly recommend to do instead?

I'm not clear either, but I don't think that what we're currently doing can be right.

Quoted 8 lines

Hm, ok, so how to continue here now? Shall we at least mark the bcm2836/7 devices with user_creatable=false, so that users can not crash their QEMU so easily with device_add? The problem with introspection via device-list-properties would still continue to exist, but I think that's less likely used in practice... otherwise we could still move the qdev_set_parent_bus() calls to the realize() function instead, and just add a big fat FIXME comment in front of the code block, so that we remember to clean that up one day...

Quoted 5 lines

Crashing device-list-properties should be a blocker bug, IMO.

Moving to realize is not the best solution, but I would prefer to do that in 3.0 instead of leaving the device-list-properties crash unfixed.

I would like to see the crash fixed too. But I'd like to see it fixed:
(a) by having clear documentation about how the QOM
system works, what you should do in init and what you should do in realize, when and why you need to manually parent objects, etc
(b) as far as possible making our APIs for doing this
easy to use correctly and difficult to use wrongly. At the moment we have APIs that are far too easy to misuse, which means we will continue to get bugs like this and spend a lot of time on one-off fixes for them.

In particular I don't understand why we need to manually parent these objects at all.

thanks
-- PMM

#9 lore raw
Quoted 11 lines · Peter Maydell
Quoted 10 lines · Eduardo Habkost
Quoted 8 lines · Thomas Huth

Hm, ok, so how to continue here now? Shall we at least mark the bcm2836/7 devices with user_creatable=false, so that users can not crash their QEMU so easily with device_add? The problem with introspection via device-list-properties would still continue to exist, but I think that's less likely used in practice... otherwise we could still move the qdev_set_parent_bus() calls to the realize() function instead, and just add a big fat FIXME comment in front of the code block, so that we remember to clean that up one day...

Quoted 1 line
Quoted 1 line

Crashing device-list-properties should be a blocker bug, IMO.

Seconded.

Quoted 16 lines
Quoted 3 lines

Moving to realize is not the best solution, but I would prefer to do that in 3.0 instead of leaving the device-list-properties crash unfixed.

I would like to see the crash fixed too. But I'd like to see it fixed:
(a) by having clear documentation about how the QOM
system works, what you should do in init and what you should do in realize, when and why you need to manually parent objects, etc
(b) as far as possible making our APIs for doing this
easy to use correctly and difficult to use wrongly. At the moment we have APIs that are far too easy to misuse, which means we will continue to get bugs like this and spend a lot of time on one-off fixes for them.

Quoted 2 lines

In particular I don't understand why we need to manually parent these objects at all.

I want both (a) and (b) as badly as anyone, but we should not hold any particular crash bug hostage to get them.

#10 lore raw
Quoted 33 lines · Markus Armbruster
Quoted 11 lines · Peter Maydell
Quoted 10 lines · Eduardo Habkost
Quoted 8 lines · Thomas Huth

Hm, ok, so how to continue here now? Shall we at least mark the bcm2836/7 devices with user_creatable=false, so that users can not crash their QEMU so easily with device_add? The problem with introspection via device-list-properties would still continue to exist, but I think that's less likely used in practice... otherwise we could still move the qdev_set_parent_bus() calls to the realize() function instead, and just add a big fat FIXME comment in front of the code block, so that we remember to clean that up one day...

Quoted 1 line
Quoted 1 line

Crashing device-list-properties should be a blocker bug, IMO.

Seconded.

Quoted 16 lines
Quoted 3 lines

Moving to realize is not the best solution, but I would prefer to do that in 3.0 instead of leaving the device-list-properties crash unfixed.

I would like to see the crash fixed too. But I'd like to see it fixed:
(a) by having clear documentation about how the QOM
system works, what you should do in init and what you should do in realize, when and why you need to manually parent objects, etc
(b) as far as possible making our APIs for doing this
easy to use correctly and difficult to use wrongly. At the moment we have APIs that are far too easy to misuse, which means we will continue to get bugs like this and spend a lot of time on one-off fixes for them.

Quoted 5 lines
Quoted 2 lines

In particular I don't understand why we need to manually parent these objects at all.

I want both (a) and (b) as badly as anyone, but we should not hold any particular crash bug hostage to get them.

Without at least (a) I can't review this patch or any other patch that fixes this kind of crash bug...

thanks
-- PMM

#11 lore raw
Quoted 34 lines · Peter Maydell
Quoted 33 lines · Markus Armbruster
Quoted 11 lines · Peter Maydell
Quoted 10 lines · Eduardo Habkost
Quoted 8 lines · Thomas Huth

Hm, ok, so how to continue here now? Shall we at least mark the bcm2836/7 devices with user_creatable=false, so that users can not crash their QEMU so easily with device_add? The problem with introspection via device-list-properties would still continue to exist, but I think that's less likely used in practice... otherwise we could still move the qdev_set_parent_bus() calls to the realize() function instead, and just add a big fat FIXME comment in front of the code block, so that we remember to clean that up one day...

Quoted 1 line
Quoted 1 line

Crashing device-list-properties should be a blocker bug, IMO.

Seconded.

Quoted 16 lines
Quoted 3 lines

Moving to realize is not the best solution, but I would prefer to do that in 3.0 instead of leaving the device-list-properties crash unfixed.

I would like to see the crash fixed too. But I'd like to see it fixed:
(a) by having clear documentation about how the QOM
system works, what you should do in init and what you should do in realize, when and why you need to manually parent objects, etc
(b) as far as possible making our APIs for doing this
easy to use correctly and difficult to use wrongly. At the moment we have APIs that are far too easy to misuse, which means we will continue to get bugs like this and spend a lot of time on one-off fixes for them.

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

In particular I don't understand why we need to manually parent these objects at all.

I want both (a) and (b) as badly as anyone, but we should not hold any particular crash bug hostage to get them.

Without at least (a) I can't review this patch or any other patch that fixes this kind of crash bug...

Fair point. But if Paolo can, and vouches for a fix, then we shouldn't hold the fix hostage.

#12 lore raw
In reply to Markus Armbruster
Quoted 13 lines · Markus Armbruster
Quoted 11 lines · Peter Maydell
Quoted 10 lines · Eduardo Habkost
Quoted 8 lines · Thomas Huth

Hm, ok, so how to continue here now? Shall we at least mark the bcm2836/7 devices with user_creatable=false, so that users can not crash their QEMU so easily with device_add? The problem with introspection via device-list-properties would still continue to exist, but I think that's less likely used in practice... otherwise we could still move the qdev_set_parent_bus() calls to the realize() function instead, and just add a big fat FIXME comment in front of the code block, so that we remember to clean that up one day...

Quoted 3 lines
Quoted 1 line
Quoted 1 line

Crashing device-list-properties should be a blocker bug, IMO.

Seconded.

Well, maybe I should then not suggest to add a hmp("info qtree") below the hmp("info qom-tree") in test_one_device() of tests/device-introspect-test.c ... otherwise we'll be quite busy in the next weeks...

Thomas

#13 lore raw
Quoted 14 lines · Thomas Huth
Quoted 13 lines · Markus Armbruster
Quoted 11 lines · Peter Maydell
Quoted 10 lines · Eduardo Habkost
Quoted 8 lines · Thomas Huth

Hm, ok, so how to continue here now? Shall we at least mark the bcm2836/7 devices with user_creatable=false, so that users can not crash their QEMU so easily with device_add? The problem with introspection via device-list-properties would still continue to exist, but I think that's less likely used in practice... otherwise we could still move the qdev_set_parent_bus() calls to the realize() function instead, and just add a big fat FIXME comment in front of the code block, so that we remember to clean that up one day...

Quoted 8 lines
Quoted 3 lines
Quoted 1 line
Quoted 1 line

Crashing device-list-properties should be a blocker bug, IMO.

Seconded.

Well, maybe I should then not suggest to add a hmp("info qtree") below the hmp("info qom-tree") in test_one_device() of tests/device-introspect-test.c ... otherwise we'll be quite busy in the next weeks...

If we can't fix these bugs in time, we can bring back cannot_destroy_with_object_finalize_yet, as Eduardo mentioned upthread. Would be sad, but sad beats crash.

#14 lore raw
Quoted 20 lines · Markus Armbruster
Quoted 18 lines · Thomas Huth
Quoted 16 lines · Markus Armbruster
Quoted 11 lines · Peter Maydell
Quoted 10 lines · Eduardo Habkost
Quoted 8 lines · Thomas Huth

Hm, ok, so how to continue here now? Shall we at least mark the bcm2836/7 devices with user_creatable=false, so that users can not crash their QEMU so easily with device_add? The problem with introspection via device-list-properties would still continue to exist, but I think that's less likely used in practice... otherwise we could still move the qdev_set_parent_bus() calls to the realize() function instead, and just add a big fat FIXME comment in front of the code block, so that we remember to clean that up one day...

Quoted 1 line
Quoted 1 line

Crashing device-list-properties should be a blocker bug, IMO.

Seconded.

Quoted 8 lines
Quoted 4 lines

Well, maybe I should then not suggest to add a hmp("info qtree") below the hmp("info qom-tree") in test_one_device() of tests/device-introspect-test.c ... otherwise we'll be quite busy in the next weeks...

If we can't fix these bugs in time, we can bring back cannot_destroy_with_object_finalize_yet, as Eduardo mentioned upthread. Would be sad, but sad beats crash.

...but are they actually interesting crashes? Nobody is ever going to actually start emulation of an integratorcp machine and then try to add a bcm2837 device via the QMP interface, except if they're deliberately doing exhaustive testing. They're bugs, sure, but they're not bugs I can ever see any real user possibly tripping over, so we don't necessarily need to go to any particular lengths to fix them for 3.0 if that's painful.

thanks
-- PMM

#15 lore raw
Quoted 26 lines · Peter Maydell
Quoted 25 lines · Markus Armbruster
Quoted 22 lines · Thomas Huth
Quoted 13 lines · Markus Armbruster
Quoted 11 lines · Peter Maydell
Quoted 10 lines · Eduardo Habkost
Quoted 8 lines · Thomas Huth

Hm, ok, so how to continue here now? Shall we at least mark the bcm2836/7 devices with user_creatable=false, so that users can not crash their QEMU so easily with device_add? The problem with introspection via device-list-properties would still continue to exist, but I think that's less likely used in practice... otherwise we could still move the qdev_set_parent_bus() calls to the realize() function instead, and just add a big fat FIXME comment in front of the code block, so that we remember to clean that up one day...

Quoted 3 lines
Quoted 1 line
Quoted 1 line

Crashing device-list-properties should be a blocker bug, IMO.

Seconded.

Well, maybe I should then not suggest to add a hmp("info qtree") below the hmp("info qom-tree") in test_one_device() of tests/device-introspect-test.c ... otherwise we'll be quite busy in the next weeks...

Quoted 8 lines
Quoted 3 lines

If we can't fix these bugs in time, we can bring back cannot_destroy_with_object_finalize_yet, as Eduardo mentioned upthread. Would be sad, but sad beats crash.

...but are they actually interesting crashes? Nobody is ever going to actually start emulation of an integratorcp machine and then try to add a bcm2837 device via the QMP interface, except if they're deliberately doing exhaustive testing.

It's not "device_add" that is a real problem here (otherwise we could simply use user_creatable=false which we likely should do for this device anyway), but rather the "device-list-properties" QMP command, which also works for devices that are marked as user_creatable=false.

I think it's valid that an upper layer tool scans all devices for their properties. But since nobody complained about crashes in the past here already, it seems like no upper layer tool is currently doing this. So I agree with you that this should not be a blocker for the 3.0 release.

Thomas

#16 lore raw
Quoted 41 lines · Thomas Huth
Quoted 26 lines · Peter Maydell
Quoted 25 lines · Markus Armbruster
Quoted 22 lines · Thomas Huth
Quoted 13 lines · Markus Armbruster
Quoted 11 lines · Peter Maydell
Quoted 10 lines · Eduardo Habkost
Quoted 8 lines · Thomas Huth

Hm, ok, so how to continue here now? Shall we at least mark the bcm2836/7 devices with user_creatable=false, so that users can not crash their QEMU so easily with device_add? The problem with introspection via device-list-properties would still continue to exist, but I think that's less likely used in practice... otherwise we could still move the qdev_set_parent_bus() calls to the realize() function instead, and just add a big fat FIXME comment in front of the code block, so that we remember to clean that up one day...

Quoted 3 lines
Quoted 1 line
Quoted 1 line

Crashing device-list-properties should be a blocker bug, IMO.

Seconded.

Well, maybe I should then not suggest to add a hmp("info qtree") below the hmp("info qom-tree") in test_one_device() of tests/device-introspect-test.c ... otherwise we'll be quite busy in the next weeks...

Quoted 8 lines
Quoted 3 lines

If we can't fix these bugs in time, we can bring back cannot_destroy_with_object_finalize_yet, as Eduardo mentioned upthread. Would be sad, but sad beats crash.

...but are they actually interesting crashes? Nobody is ever going to actually start emulation of an integratorcp machine and then try to add a bcm2837 device via the QMP interface, except if they're deliberately doing exhaustive testing.

It's not "device_add" that is a real problem here (otherwise we could simply use user_creatable=false which we likely should do for this device anyway), but rather the "device-list-properties" QMP command, which also works for devices that are marked as user_creatable=false.

Quoted 4 lines

I think it's valid that an upper layer tool scans all devices for their properties. But since nobody complained about crashes in the past here already, it seems like no upper layer tool is currently doing this. So I agree with you that this should not be a blocker for the 3.0 release.

Fixing the crashes by bringing back cannot_destroy_with_object_finalize_yet would be a bit of a cop out, but it would also be so easy that there's really no excuse for leaving them unfixed.

#17 lore raw
In reply to Peter Maydell
Quoted 10 lines · Peter Maydell
Quoted 8 lines
Quoted 6 lines

Yuck. The real problem here is that we're still requiring the code that creates these QOM devices to manually set the parent in the first place. It's not surprising that we don't get it right (either parenting in the wrong place or not at all). I'd much rather see us fix that properly than keep papering over places where we get it wrong.

Sorry, I'm still not an expert in all this QOM stuff yet ... so what do you exactly recommend to do instead?

I'm not clear either, but I don't think that what we're currently doing can be right.

Well, in theory it should work... I sent the expected flow in another email.

Paolo

#18 lore raw
Quoted 7 lines · Paolo Bonzini
Quoted 6 lines · Peter Maydell
Quoted 6 lines
Quoted 6 lines

Yuck. The real problem here is that we're still requiring the code that creates these QOM devices to manually set the parent in the first place. It's not surprising that we don't get it right (either parenting in the wrong place or not at all). I'd much rather see us fix that properly than keep papering over places where we get it wrong.

Quoted 6 lines
Quoted 4 lines
Quoted 2 lines

Sorry, I'm still not an expert in all this QOM stuff yet ... so what do you exactly recommend to do instead?

I'm not clear either, but I don't think that what we're currently doing can be right.

Well, in theory it should work... I sent the expected flow in another email.

Something that just came to my mind:

bcm2836_init() creates the TYPE_BCM2835_PERIPHERALS object with object_initialize(). This creates one reference to the object already. Then the object is linked to its parent with object_property_add_child(), which creates another reference to the object. But where are the two references correctly destroyed again? One is certainly destroyed by device_unparent later, but the initial one? Could it be that we are simply lacking one object_unref() after the object_property_add_child() here?

Thomas

#19 lore raw
Quoted 17 lines · Thomas Huth
Quoted 7 lines · Paolo Bonzini
Quoted 6 lines · Peter Maydell
Quoted 6 lines
Quoted 6 lines

Yuck. The real problem here is that we're still requiring the code that creates these QOM devices to manually set the parent in the first place. It's not surprising that we don't get it right (either parenting in the wrong place or not at all). I'd much rather see us fix that properly than keep papering over places where we get it wrong.

Quoted 6 lines
Quoted 4 lines
Quoted 2 lines

Sorry, I'm still not an expert in all this QOM stuff yet ... so what do you exactly recommend to do instead?

I'm not clear either, but I don't think that what we're currently doing can be right.

Well, in theory it should work... I sent the expected flow in another email.

Something that just came to my mind:

Quoted 8 lines

bcm2836_init() creates the TYPE_BCM2835_PERIPHERALS object with object_initialize(). This creates one reference to the object already. Then the object is linked to its parent with object_property_add_child(), which creates another reference to the object. But where are the two references correctly destroyed again? One is certainly destroyed by device_unparent later, but the initial one? Could it be that we are simply lacking one object_unref() after the object_property_add_child() here?

This seems to be true, but I'm confused about the reference counting model, here:

What exactly guarantees there will be no other references to (e.g.) &s->control when s is freed?

We know the references added by object_initialize(), object_property_add_child() and qdev_set_parent_bus() will be dropped, but what about other code calling object_ref()?

Signature · 1 line
Eduardo
#20 lore raw
Quoted 7 lines · Eduardo Habkost

What exactly guarantees there will be no other references to (e.g.) &s->control when s is freed?

We know the references added by object_initialize(), object_property_add_child() and qdev_set_parent_bus() will be dropped, but what about other code calling object_ref()?

That would be a bug. This is in fact the reason why memory_region_ref/unref exists---to take the reference on the "outer" device object rather than the contained memory region object. It's not pretty though.

I've thought of generalizing the pattern to Object (object_ref adds a reference to the container rather than the contained object, and finalize takes care of finalizing the contained object too), but I'm a bit wary of doing it since it would complicate things further and (except for MemoryRegions) it hasn't been a problem in practice.

Paolo

#21 lore raw
In reply to Thomas Huth
Quoted 2 lines · Thomas Huth
Quoted 1 line · Peter Maydell

On 9 July 2018 at 22:03, Thomas Huth <[email protected]> wrote:

[...]

Quoted 4 lines

Hmm, maybe we need a qtest that first executes "info qtree", then runs 'device-list-properties' for all devices and finally checks "info qtree" again ... since 'device-list-properties' should not change the qtree, the output of the "info qtree" should be the same.

Lovely idea.

Quoted 2 lines

Currently this is not
the case :-/

Less than lovely, but I can't claim to be surprised. As Peter remarked elsewhere in this thread, QOM is underdocumented, and is too easy to misuse.

On documentation: we sorely lack documentation tying everything together. GDK-Doc function comments alone can't cut it for something as complex as QOM.

Even with adequate documentation, bad examples in the source will continue to multiply. We'll have to get rid of them.

#22 lore raw
In reply to Thomas Huth
Quoted 5 lines · Thomas Huth

The problem is that qdev_set_parent_bus() from instance_init adds a link to the child devices which is not valid anymore after the device init failed. Thus the qdev_set_parent_bus() must rather be done in the realize function instead.

The theoretical behavior should be:

- realize fails

- object_unparent is called on the device that failed to realize (see qdev_device_add). object_unparent calls device_unparent

- after device_unparent finishes, the last reference to the device has been dropped and the device is freed

- object finalization releases all properties

- this includes child properties, so for each child device object_unparent is called

- again device_unparent is called (for the child) and this removes the child from the bus.

Paolo

#23 lore raw
Quoted 8 lines · Paolo Bonzini
Quoted 5 lines · Thomas Huth

The problem is that qdev_set_parent_bus() from instance_init adds a link to the child devices which is not valid anymore after the device init failed. Thus the qdev_set_parent_bus() must rather be done in the realize function instead.

The theoretical behavior should be:

It's not clear below where you expect
qdev_set_parent_bus(..., sysbus_get_default())
to be called (if it should be called at all).

I don't know where it should be called, but I'm absolutely sure instance_init is not the right place.

Quoted 18 lines

- realize fails

- object_unparent is called on the device that failed to realize (see qdev_device_add). object_unparent calls device_unparent

- after device_unparent finishes, the last reference to the device has been dropped and the device is freed

- object finalization releases all properties

- this includes child properties, so for each child device object_unparent is called

- again device_unparent is called (for the child) and this removes the child from the bus.

Paolo

Signature · 1 line
Eduardo
#24 lore raw
Quoted 7 lines · Eduardo Habkost
Quoted 1 line

The theoretical behavior should be:

It's not clear below where you expect
qdev_set_parent_bus(..., sysbus_get_default())
to be called (if it should be called at all).

I don't know where it should be called, but I'm absolutely sure instance_init is not the right place.

I think instance_init is fine to call qdev_set_parent_bus on contained devices. Why do you say it's not?

Thanks,

Paolo

#25 lore raw
Quoted 11 lines · Paolo Bonzini
Quoted 7 lines · Eduardo Habkost
Quoted 1 line

The theoretical behavior should be:

It's not clear below where you expect
qdev_set_parent_bus(..., sysbus_get_default())
to be called (if it should be called at all).

I don't know where it should be called, but I'm absolutely sure instance_init is not the right place.

I think instance_init is fine to call qdev_set_parent_bus on contained devices. Why do you say it's not?

Because object_unref(object_new(...)) is not supposed to affect QEMU global state at all.

Signature · 1 line
Eduardo
#26 lore raw
Quoted 7 lines · Eduardo Habkost
Quoted 6 lines · Paolo Bonzini
Quoted 5 lines · Eduardo Habkost
Quoted 1 line

The theoretical behavior should be:

It's not clear below where you expect
qdev_set_parent_bus(..., sysbus_get_default())
to be called (if it should be called at all).

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

I don't know where it should be called, but I'm absolutely sure instance_init is not the right place.

I think instance_init is fine to call qdev_set_parent_bus on contained devices. Why do you say it's not?

Because object_unref(object_new(...)) is not supposed to affect QEMU global state at all.

It should not affect it. Any changes to the global state done by instance_init are immediately undone when object_unref destroys the child properties of the object.

Thanks,

Paolo

#27 lore raw
Quoted 14 lines · Paolo Bonzini
Quoted 13 lines · Eduardo Habkost
Quoted 11 lines · Paolo Bonzini
Quoted 7 lines · Eduardo Habkost
Quoted 1 line

The theoretical behavior should be:

It's not clear below where you expect
qdev_set_parent_bus(..., sysbus_get_default())
to be called (if it should be called at all).

I don't know where it should be called, but I'm absolutely sure instance_init is not the right place.

I think instance_init is fine to call qdev_set_parent_bus on contained devices. Why do you say it's not?

Quoted 6 lines
Quoted 2 lines

Because object_unref(object_new(...)) is not supposed to affect QEMU global state at all.

It should not affect it. Any changes to the global state done by instance_init are immediately undone when object_unref destroys the child properties of the object.

I would prefer if it didn't, but not a big deal as long as all QOM code is protected by the BQL (it is, right?).

If we get rid of object_new() in qmp_device_list_properties(), then most of the restrictions on instance_init can go away, anyway.

Signature · 1 line
Eduardo
#28 lore raw
Quoted 22 lines · Eduardo Habkost
Quoted 14 lines · Paolo Bonzini
Quoted 13 lines · Eduardo Habkost
Quoted 11 lines · Paolo Bonzini
Quoted 7 lines · Eduardo Habkost
Quoted 1 line

The theoretical behavior should be:

It's not clear below where you expect
qdev_set_parent_bus(..., sysbus_get_default())
to be called (if it should be called at all).

I don't know where it should be called, but I'm absolutely sure instance_init is not the right place.

I think instance_init is fine to call qdev_set_parent_bus on contained devices. Why do you say it's not?

Quoted 6 lines
Quoted 2 lines

Because object_unref(object_new(...)) is not supposed to affect QEMU global state at all.

It should not affect it. Any changes to the global state done by instance_init are immediately undone when object_unref destroys the child properties of the object.

Quoted 6 lines

I would prefer if it didn't, but not a big deal as long as all QOM code is protected by the BQL (it is, right?).

If we get rid of object_new() in qmp_device_list_properties(), then most of the restrictions on instance_init can go away, anyway.

How could we get rid of object_new()? As long as we create properties in code, we need to run the code to find the properties.

#29 lore raw
Quoted 27 lines · Markus Armbruster
Quoted 25 lines · Eduardo Habkost
Quoted 14 lines · Paolo Bonzini
Quoted 13 lines · Eduardo Habkost
Quoted 11 lines · Paolo Bonzini
Quoted 7 lines · Eduardo Habkost
Quoted 1 line

The theoretical behavior should be:

It's not clear below where you expect
qdev_set_parent_bus(..., sysbus_get_default())
to be called (if it should be called at all).

I don't know where it should be called, but I'm absolutely sure instance_init is not the right place.

I think instance_init is fine to call qdev_set_parent_bus on contained devices. Why do you say it's not?

Quoted 6 lines
Quoted 2 lines

Because object_unref(object_new(...)) is not supposed to affect QEMU global state at all.

It should not affect it. Any changes to the global state done by instance_init are immediately undone when object_unref destroys the child properties of the object.

I would prefer if it didn't, but not a big deal as long as all QOM code is protected by the BQL (it is, right?).

Quoted 6 lines
Quoted 3 lines

If we get rid of object_new() in qmp_device_list_properties(), then most of the restrictions on instance_init can go away, anyway.

How could we get rid of object_new()? As long as we create properties in code, we need to run the code to find the properties.

By stopping registering properties at instance_init, and making them introspectable at the class object.

Signature · 1 line
Eduardo
#30 lore raw
In reply to Paolo Bonzini
Quoted 10 lines · Paolo Bonzini
Quoted 5 lines · Thomas Huth

The problem is that qdev_set_parent_bus() from instance_init adds a link to the child devices which is not valid anymore after the device init failed. Thus the qdev_set_parent_bus() must rather be done in the realize function instead.

The theoretical behavior should be:

- realize fails

In this case, the failure is before realize is attempted, qdev_device_add() already stop with "Device '%s' can not be hotplugged on this machine".

Quoted 2 lines

- object_unparent is called on the device that failed to realize (see qdev_device_add). object_unparent calls device_unparent

Hmm, are you sure? I can see that object_unparent calls device_unparent indirectly for the *child* nodes of the device, but not for the device itself...

Thomas

#31 lore raw
Quoted 5 lines · Thomas Huth
Quoted 2 lines

- realize fails

In this case, the failure is before realize is attempted, qdev_device_add() already stop with "Device '%s' can not be hotplugged on this machine".

Still, object_unparent is called by qdev_device_add in the error path, and it should work the same way (in a nutshell, recursive unparent when child properties are deleted, and finalization of the contained objects as the last reference is dropped).

Quoted 5 lines
Quoted 2 lines

- object_unparent is called on the device that failed to realize (see qdev_device_add). object_unparent calls device_unparent

Hmm, are you sure? I can see that object_unparent calls device_unparent indirectly for the *child* nodes of the device, but not for the device itself...

object_unparent -> object_property_del_child ->
object_finalize_child_property -> device_unparent

I think you're on the right track, after object_property_add_child you need to drop the reference to the object. For example qmp_device_add does it after qdev_device_add returns a device successfully (just an example---I understand it is not the case with bcm283x). In that case the call to object_property_add_child is in qdev_set_id.

Paolo

#32 lore raw

On 11.07.2018 22:15, Paolo Bonzini wrote:
[...]

Quoted 2 lines

I think you're on the right track, after object_property_add_child you need to drop the reference to the object.

Yes, that's the issue indeed! The child objects get properly cleaned up once I add the object_unref() after the object_property_add_child(), and then the crash does not occur anymore. I'll check the other spots, then I'll write a patch...

Thomas