From nobody Tue Feb 10 06:25:12 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zoho.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1488294472606427.6997915970027; Tue, 28 Feb 2017 07:07:52 -0800 (PST) Received: from localhost ([::1]:33732 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1cijNT-0003ZD-73 for importer@patchew.org; Tue, 28 Feb 2017 10:07:47 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:47637) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1cijBP-0001VE-5I for qemu-devel@nongnu.org; Tue, 28 Feb 2017 09:55:20 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1cijBO-0005dY-7x for qemu-devel@nongnu.org; Tue, 28 Feb 2017 09:55:19 -0500 Received: from orth.archaic.org.uk ([2001:8b0:1d0::2]:48690) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1cijBM-0005cl-1K; Tue, 28 Feb 2017 09:55:16 -0500 Received: from pm215 by orth.archaic.org.uk with local (Exim 4.84_2) (envelope-from ) id 1cijBI-0003ID-U9; Tue, 28 Feb 2017 14:55:12 +0000 From: Peter Maydell To: qemu-arm@nongnu.org, qemu-devel@nongnu.org Date: Tue, 28 Feb 2017 14:55:08 +0000 Message-Id: <1488293711-14195-2-git-send-email-peter.maydell@linaro.org> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1488293711-14195-1-git-send-email-peter.maydell@linaro.org> References: <1488293711-14195-1-git-send-email-peter.maydell@linaro.org> X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 2001:8b0:1d0::2 Subject: [Qemu-devel] [PATCH 1/4] qdev: Have qdev_set_parent_bus() handle devices already on a bus X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Paolo Bonzini , Clement Deschamps , patches@linaro.org Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" 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 Reviewed-by: Igor Mammedov --- 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) =20 void qdev_set_parent_bus(DeviceState *dev, BusState *bus) { + bool replugging =3D dev->parent_bus !=3D 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 =3D bus; object_ref(OBJECT(bus)); bus_add_child(bus, dev); + if (replugging) { + object_unref(OBJECT(dev)); + } } =20 /* Create a new device. This only initializes the device state --=20 2.7.4