diff mbox series

[1/4] qdev: Have qdev_set_parent_bus() handle devices already on a bus

Message ID 1488293711-14195-2-git-send-email-peter.maydell@linaro.org
State Superseded
Headers show
Series handle reparenting of SD cards for rpi2 | expand

Commit Message

Peter Maydell Feb. 28, 2017, 2:55 p.m. UTC
Instead of qdev_set_parent_bus() silently doing the wrong
thing if it's handed a device that's already on a bus,
have it remove the device from the old bus and add it to
the new one. This is useful for the raspi2 sdcard.

Signed-off-by: Peter Maydell <peter.maydell@linaro.org>

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

-- 
2.7.4

Comments

Igor Mammedov Feb. 28, 2017, 3:55 p.m. UTC | #1
On Tue, 28 Feb 2017 14:55:08 +0000
Peter Maydell <peter.maydell@linaro.org> wrote:

> Instead of qdev_set_parent_bus() silently doing the wrong

> thing if it's handed a device that's already on a bus,

> have it remove the device from the old bus and add it to

> the new one. This is useful for the raspi2 sdcard.

> 

> Signed-off-by: Peter Maydell <peter.maydell@linaro.org>

Reviewed-by: Igor Mammedov <imammedo@redhat.com>


> ---

>  hw/core/qdev.c | 14 ++++++++++++++

>  1 file changed, 14 insertions(+)

> 

> diff --git a/hw/core/qdev.c b/hw/core/qdev.c

> index 06ba02e..923e626 100644

> --- a/hw/core/qdev.c

> +++ b/hw/core/qdev.c

> @@ -102,9 +102,23 @@ static void bus_add_child(BusState *bus, DeviceState *child)

>  

>  void qdev_set_parent_bus(DeviceState *dev, BusState *bus)

>  {

> +    bool replugging = dev->parent_bus != NULL;

> +

> +    if (replugging) {

> +        /* Keep a reference to the device while it's not plugged into

> +         * any bus, to avoid it potentially evaporating when it is

> +         * dereffed in bus_remove_child().

> +         */

> +        object_ref(OBJECT(dev));

> +        bus_remove_child(dev->parent_bus, dev);

> +        object_unref(OBJECT(dev->parent_bus));

> +    }

>      dev->parent_bus = bus;

>      object_ref(OBJECT(bus));

>      bus_add_child(bus, dev);

> +    if (replugging) {

> +        object_unref(OBJECT(dev));

> +    }

>  }

>  

>  /* Create a new device.  This only initializes the device state
diff mbox series

Patch

diff --git a/hw/core/qdev.c b/hw/core/qdev.c
index 06ba02e..923e626 100644
--- a/hw/core/qdev.c
+++ b/hw/core/qdev.c
@@ -102,9 +102,23 @@  static void bus_add_child(BusState *bus, DeviceState *child)
 
 void qdev_set_parent_bus(DeviceState *dev, BusState *bus)
 {
+    bool replugging = dev->parent_bus != NULL;
+
+    if (replugging) {
+        /* Keep a reference to the device while it's not plugged into
+         * any bus, to avoid it potentially evaporating when it is
+         * dereffed in bus_remove_child().
+         */
+        object_ref(OBJECT(dev));
+        bus_remove_child(dev->parent_bus, dev);
+        object_unref(OBJECT(dev->parent_bus));
+    }
     dev->parent_bus = bus;
     object_ref(OBJECT(bus));
     bus_add_child(bus, dev);
+    if (replugging) {
+        object_unref(OBJECT(dev));
+    }
 }
 
 /* Create a new device.  This only initializes the device state