All of lore.kernel.org
 help / color / mirror / Atom feed
From: "Philippe Mathieu-Daudé" <philmd@oss.qualcomm.com>
To: qemu-devel@nongnu.org
Subject: [PULL 15/56] hw/qdev: Parent device before setting parent bus
Date: Sun, 16 Aug 2026 16:45:14 +0200	[thread overview]
Message-ID: <20260816144556.69009-16-philmd@oss.qualcomm.com> (raw)
In-Reply-To: <20260816144556.69009-1-philmd@oss.qualcomm.com>

From: Akihiko Odaki <odaki@rsg.ci.i.u-tokyo.ac.jp>

Commit 9940b2cfbc05 ("qdev: New qdev_new(), qdev_realize(), etc.") says
"device state 'no QOM parent, but plugged into bus' is dangerous". In
such a case, unrealizing the bus will hang in bus_unparent():

    while ((kid = QTAILQ_FIRST(&bus->children)) != NULL) {
        DeviceState *dev = kid->child;
        object_unparent(OBJECT(dev));
    }

object_unparent() does nothing when its argument has no QOM parent,
and the loop spins forever.

However, that commit did not completely eliminate such a situation.
When the device is not parented, device_set_realized() lets
/machine/unattached parent it, but it happens after setting parent bus.
Therefore, any failure between the two operations can leave the device
in a dangerous state.

qdev_realize() at least asserts that the device is not already realized
and prevents one realization failure pattern, but it is not
comprehensive. Besides, it will trip with a command line like the
following:

    qemu-system-x86_64 -M none -nodefaults -nographic \
        -device ipmi-bmc-sim,realized=on

Eliminate the dangerous state by ensuring that the device is parented
before calling qdev_set_parent_bus(). Also, stop asserting that the
device is not already realized in qdev_realize(); it is broken and
no longer serves any purpose.

Fixes: 9940b2cfbc05 ("qdev: New qdev_new(), qdev_realize(), etc.")
Signed-off-by: Akihiko Odaki <odaki@rsg.ci.i.u-tokyo.ac.jp>
Reviewed-by: Philippe Mathieu-Daudé <philmd@oss.qualcomm.com>
Signed-off-by: Philippe Mathieu-Daudé <philmd@oss.qualcomm.com>
Message-ID: <20260721-qdev-v3-14-d2e226fa002e@rsg.ci.i.u-tokyo.ac.jp>
---
 hw/core/qdev.c         | 51 ++++++++++++++++++++++++------------------
 tests/unit/test-qdev.c | 13 +++++++++++
 2 files changed, 42 insertions(+), 22 deletions(-)

diff --git a/hw/core/qdev.c b/hw/core/qdev.c
index e2aab3d1fc6..0b0f2f47fa7 100644
--- a/hw/core/qdev.c
+++ b/hw/core/qdev.c
@@ -264,17 +264,43 @@ static void device_reset_child_foreach(Object *obj, ResettableChildCallback cb,
 
 bool qdev_realize(DeviceState *dev, BusState *bus, Error **errp)
 {
-    assert(!dev->realized && !dev->parent_bus);
+    static int unattached_count;
+    bool unattached_parent = false;
+
+    assert(!dev->parent_bus);
+
+    if (!OBJECT(dev)->parent) {
+        gchar *name = g_strdup_printf("device[%d]", unattached_count++);
+
+        object_property_add_child(machine_get_container("unattached"),
+                                  name, OBJECT(dev));
+        unattached_parent = true;
+        g_free(name);
+    }
 
     if (bus) {
         if (!qdev_set_parent_bus(dev, bus, errp)) {
-            return false;
+            goto fail;
         }
     } else {
         assert(!DEVICE_GET_CLASS(dev)->bus_type);
     }
 
-    return object_property_set_bool(OBJECT(dev), "realized", true, errp);
+    if (object_property_set_bool(OBJECT(dev), "realized", true, errp)) {
+        return true;
+    }
+
+fail:
+    if (unattached_parent) {
+        /*
+         * Beware, this doesn't just revert
+         * object_property_add_child(), it also runs bus_remove()!
+         */
+        object_unparent(OBJECT(dev));
+        unattached_count--;
+    }
+
+    return false;
 }
 
 bool qdev_realize_and_unref(DeviceState *dev, BusState *bus, Error **errp)
@@ -479,8 +505,6 @@ static void device_set_realized(Object *obj, bool value, Error **errp)
     BusState *bus;
     NamedClockList *ncl;
     Error *local_err = NULL;
-    bool unattached_parent = false;
-    static int unattached_count;
 
     if (dev->hotplugged && !dc->hotpluggable) {
         error_setg(errp, "Device '%s' does not support hotplugging",
@@ -493,15 +517,6 @@ static void device_set_realized(Object *obj, bool value, Error **errp)
             goto fail;
         }
 
-        if (!obj->parent) {
-            gchar *name = g_strdup_printf("device[%d]", unattached_count++);
-
-            object_property_add_child(machine_get_container("unattached"),
-                                      name, obj);
-            unattached_parent = true;
-            g_free(name);
-        }
-
         hotplug_ctrl = qdev_get_hotplug_handler(dev);
         if (hotplug_ctrl) {
             hotplug_handler_pre_plug(hotplug_ctrl, dev, &local_err);
@@ -627,14 +642,6 @@ post_realize_fail:
 
 fail:
     error_propagate(errp, local_err);
-    if (unattached_parent) {
-        /*
-         * Beware, this doesn't just revert
-         * object_property_add_child(), it also runs bus_remove()!
-         */
-        object_unparent(OBJECT(dev));
-        unattached_count--;
-    }
 }
 
 static bool device_get_hotpluggable(Object *obj, Error **errp)
diff --git a/tests/unit/test-qdev.c b/tests/unit/test-qdev.c
index 20eae38e03f..77c3eee7171 100644
--- a/tests/unit/test-qdev.c
+++ b/tests/unit/test-qdev.c
@@ -78,6 +78,16 @@ static void test_qdev_free_properties(void)
     object_unref(mt);
 }
 
+static void test_qdev_double_realization(void)
+{
+    MyDev *mt = STATIC_TYPE(object_new(TYPE_MY_DEV));
+
+    qdev_realize(DEVICE(mt), NULL, &error_fatal);
+    qdev_realize(DEVICE(mt), NULL, &error_fatal);
+    object_unparent(OBJECT(mt));
+    object_unref(OBJECT(mt));
+}
+
 
 int main(int argc, char **argv)
 {
@@ -90,6 +100,9 @@ int main(int argc, char **argv)
     g_test_add_func("/qdev/free-properties",
                     test_qdev_free_properties);
 
+    g_test_add_func("/qdev/double-realization",
+                    test_qdev_double_realization);
+
     g_test_run();
 
     return 0;
-- 
2.53.0



  parent reply	other threads:[~2026-08-16 14:50 UTC|newest]

Thread overview: 58+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-08-16 14:44 [PULL 00/56] Misc HW patches for 2026-08-16 Philippe Mathieu-Daudé
2026-08-16 14:45 ` [PULL 01/56] hw/qdev: Remove DEFINE_PROP_DMAADDR() and 'hw/qdev-dma.h' Philippe Mathieu-Daudé
2026-08-16 14:45 ` [PULL 02/56] hw/mem/nvdimm: fix "size" property typename Philippe Mathieu-Daudé
2026-08-16 14:45 ` [PULL 03/56] qdev: Make qdev_is_realized() take a const DeviceState * Philippe Mathieu-Daudé
2026-08-16 14:45 ` [PULL 04/56] hw/hyperv/balloon: Use qdev_is_realized() Philippe Mathieu-Daudé
2026-08-16 14:45 ` [PULL 05/56] hw/intc/apic: " Philippe Mathieu-Daudé
2026-08-16 14:45 ` [PULL 06/56] hw/mem/memory-device: " Philippe Mathieu-Daudé
2026-08-16 14:45 ` [PULL 07/56] hw/mem/pc-dimm: " Philippe Mathieu-Daudé
2026-08-16 14:45 ` [PULL 08/56] hw/nvram: " Philippe Mathieu-Daudé
2026-08-16 14:45 ` [PULL 09/56] hw/ppc/pnv_xscom: " Philippe Mathieu-Daudé
2026-08-16 14:45 ` [PULL 10/56] hw/vfio: " Philippe Mathieu-Daudé
2026-08-16 14:45 ` [PULL 11/56] hw/virtio/virtio-mem: " Philippe Mathieu-Daudé
2026-08-16 14:45 ` [PULL 12/56] hw/virtio/virtio-qmp: " Philippe Mathieu-Daudé
2026-08-16 14:45 ` [PULL 13/56] target/i386/cpu: " Philippe Mathieu-Daudé
2026-08-16 14:45 ` [PULL 14/56] target/s390x: " Philippe Mathieu-Daudé
2026-08-16 14:45 ` Philippe Mathieu-Daudé [this message]
2026-08-16 14:45 ` [PULL 16/56] hw/i386/pc: xen: reinstate the "xenfv" machine alias Philippe Mathieu-Daudé
2026-08-16 14:45 ` [PULL 17/56] hw/net/rtl8139: Fix handling of VLAN tags on incoming short packets Philippe Mathieu-Daudé
2026-08-16 14:45 ` [PULL 18/56] hw/net/rtl8139: Send whole of vlan-tagged packet when doing loopback Philippe Mathieu-Daudé
2026-08-16 14:45 ` [PULL 19/56] hw/block/pflash_cfi01: Restore ROMD mode after migration Philippe Mathieu-Daudé
2026-08-16 14:45 ` [PULL 20/56] hw/nvme: add SPDM_SOCKET Kconfig dependency Philippe Mathieu-Daudé
2026-08-16 14:45 ` [PULL 21/56] hw/cpu: Correct CPU_GET_CLASS() comment Philippe Mathieu-Daudé
2026-08-16 14:45 ` [PULL 22/56] hw/cpu: Include missing 'qemu/accel.h' header Philippe Mathieu-Daudé
2026-08-16 14:45 ` [PULL 23/56] hw/cpu: Move internal declarations to new 'cpu-internal.h' header Philippe Mathieu-Daudé
2026-08-16 14:45 ` [PULL 24/56] hw/cpu: Rename cpu_common_realizefn() -> cpu_exec_realize() Philippe Mathieu-Daudé
2026-08-16 14:45 ` [PULL 25/56] hw/cpu: Rename cpu_exec_realizefn() -> cpu_common_realize() Philippe Mathieu-Daudé
2026-08-16 14:45 ` [PULL 26/56] hw/cpu: Rename cpu_exec_unrealizefn() -> cpu_common_unrealize() Philippe Mathieu-Daudé
2026-08-16 14:45 ` [PULL 27/56] hw/cpu: Extract cpu_exec_realize() out of cpu_common_realizefn() Philippe Mathieu-Daudé
2026-08-16 14:45 ` [PULL 28/56] hw/cpu: Move system-specific cpu_exec_realize() to cpu-system.c Philippe Mathieu-Daudé
2026-08-16 14:45 ` [PULL 29/56] hw/nmi: Use object_child_foreach_recursive() in nmi_children() Philippe Mathieu-Daudé
2026-08-16 14:45 ` [PULL 30/56] hw/s390x/virtio-ccw: Always inject NMI to first CPU Philippe Mathieu-Daudé
2026-08-16 14:45 ` [PULL 31/56] hw/nmi: Remove @cpu_index argument from NMIClass::nmi_monitor_handler() Philippe Mathieu-Daudé
2026-08-16 14:45 ` [PULL 32/56] hw/nmi: Remove @cpu_index argument from nmi_inject() Philippe Mathieu-Daudé
2026-08-16 14:45 ` [PULL 33/56] hw/nmi: Rename nmi_monitor_handler() -> raise_nmi() Philippe Mathieu-Daudé
2026-08-16 14:45 ` [PULL 34/56] hw/nmi: Remove unused @errp argument from raise_nmi() Philippe Mathieu-Daudé
2026-08-16 14:45 ` [PULL 35/56] hw/nmi: Raise NMI line only once Philippe Mathieu-Daudé
2026-08-16 14:45 ` [PULL 36/56] hexagon: Remove unnecessary 'monitor/monitor.h' header Philippe Mathieu-Daudé
2026-08-16 14:45 ` [PULL 37/56] net/vhost-vdpa: Include missing 'qemu/iov.h' header Philippe Mathieu-Daudé
2026-08-16 14:45 ` [PULL 38/56] tests/unit: Include 'qemu/main-loop.h' header in test-util-sockets.c Philippe Mathieu-Daudé
2026-08-16 14:45 ` [PULL 39/56] qapi/qmp-dispatch: Include 'qemu/aio-wait.h' and 'monitor/monitor.h' Philippe Mathieu-Daudé
2026-08-16 14:45 ` [PULL 40/56] qapi/qmp-registry: Remove unnecessary 'monitor/monitor.h' header Philippe Mathieu-Daudé
2026-08-16 14:45 ` [PULL 41/56] migration/hmp-cmds: Include 'block/block-global-state.h' header Philippe Mathieu-Daudé
2026-08-16 14:45 ` [PULL 42/56] monitor: Include missing 'qemu/aio-wait.h' header Philippe Mathieu-Daudé
2026-08-16 14:45 ` [PULL 43/56] monitor: Include missing 'qemu/lockable.h' header Philippe Mathieu-Daudé
2026-08-16 14:45 ` [PULL 44/56] monitor: Include missing 'qemu/coroutine-core.h' header Philippe Mathieu-Daudé
2026-08-16 14:45 ` [PULL 45/56] monitor: Reduce inclusion of 'qapi/qapi-emit-events.h' header Philippe Mathieu-Daudé
2026-08-16 14:45 ` [PULL 46/56] monitor: Remove unnecessary 'block/block.h' header Philippe Mathieu-Daudé
2026-08-16 14:45 ` [PULL 47/56] system: Remove unnecessary 'monitor/monitor.h' header Philippe Mathieu-Daudé
2026-08-16 14:45 ` [PULL 48/56] system/dirtylimit: Extract HMP code to dirtylimit-hmp-cmds.c Philippe Mathieu-Daudé
2026-08-16 14:45 ` [PULL 49/56] system: Move qmp_inject_nmi() to hw/core/machine-qmp-cmds.c Philippe Mathieu-Daudé
2026-08-16 14:45 ` [PULL 50/56] system: Extract QMP memsave/pmemsave commands to physmem-qmp-cmds.c Philippe Mathieu-Daudé
2026-08-16 14:45 ` [PULL 51/56] system: Move runstate-related code from cpus.c to runstate.c Philippe Mathieu-Daudé
2026-08-16 14:45 ` [PULL 52/56] monitor: Rename MonitorHMP @mon -> @hmp Philippe Mathieu-Daudé
2026-08-16 14:45 ` [PULL 53/56] monitor: Better express monitor_read()'s opaque arg is of Monitor type Philippe Mathieu-Daudé
2026-08-16 14:45 ` [PULL 54/56] monitor: Replace container_of(MonitorHMP, parent_obj) -> MONITOR_HMP() Philippe Mathieu-Daudé
2026-08-16 14:45 ` [PULL 55/56] hw/elf_ops: defend against weird elf headers Philippe Mathieu-Daudé
2026-08-16 14:45 ` [PULL 56/56] hw/core/machine: Move EHCI migration compat properties to 11.1 Philippe Mathieu-Daudé
2026-08-16 17:26 ` [PULL 00/56] Misc HW patches for 2026-08-16 Richard Henderson

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260816144556.69009-16-philmd@oss.qualcomm.com \
    --to=philmd@oss.qualcomm.com \
    --cc=qemu-devel@nongnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.