From nobody Thu May 2 17:14:59 2024 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.zohomail.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 1501119219144211.56017812288553; Wed, 26 Jul 2017 18:33:39 -0700 (PDT) Received: from localhost ([::1]:40599 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1daXgH-0003hz-8Z for importer@patchew.org; Wed, 26 Jul 2017 21:33:37 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:50873) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1daXeh-0002sm-Oz for qemu-devel@nongnu.org; Wed, 26 Jul 2017 21:32:00 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1daXec-00087d-RJ for qemu-devel@nongnu.org; Wed, 26 Jul 2017 21:31:59 -0400 Received: from mx0b-001b2d01.pphosted.com ([148.163.158.5]:40872 helo=mx0a-001b2d01.pphosted.com) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1daXec-000870-Lp for qemu-devel@nongnu.org; Wed, 26 Jul 2017 21:31:54 -0400 Received: from pps.filterd (m0098419.ppops.net [127.0.0.1]) by mx0b-001b2d01.pphosted.com (8.16.0.21/8.16.0.21) with SMTP id v6R1Sh45049041 for ; Wed, 26 Jul 2017 21:31:53 -0400 Received: from e32.co.us.ibm.com (e32.co.us.ibm.com [32.97.110.150]) by mx0b-001b2d01.pphosted.com with ESMTP id 2bxyxvgmjb-1 (version=TLSv1.2 cipher=AES256-SHA bits=256 verify=NOT) for ; Wed, 26 Jul 2017 21:31:53 -0400 Received: from localhost by e32.co.us.ibm.com with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted for from ; Wed, 26 Jul 2017 19:31:52 -0600 Received: from b03cxnp07028.gho.boulder.ibm.com (9.17.130.15) by e32.co.us.ibm.com (192.168.1.132) with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted; Wed, 26 Jul 2017 19:31:48 -0600 Received: from b03ledav004.gho.boulder.ibm.com (b03ledav004.gho.boulder.ibm.com [9.17.130.235]) by b03cxnp07028.gho.boulder.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id v6R1VmDv1048848; Wed, 26 Jul 2017 18:31:48 -0700 Received: from b03ledav004.gho.boulder.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 412CE78037; Wed, 26 Jul 2017 19:31:48 -0600 (MDT) Received: from localhost (unknown [9.80.84.170]) by b03ledav004.gho.boulder.ibm.com (Postfix) with ESMTP id F33B578038; Wed, 26 Jul 2017 19:31:47 -0600 (MDT) From: Michael Roth To: qemu-devel@nongnu.org Date: Wed, 26 Jul 2017 20:30:53 -0500 X-Mailer: git-send-email 2.7.4 In-Reply-To: <1501119055-4060-1-git-send-email-mdroth@linux.vnet.ibm.com> References: <1501119055-4060-1-git-send-email-mdroth@linux.vnet.ibm.com> X-TM-AS-GCONF: 00 x-cbid: 17072701-0004-0000-0000-000012A86B8C X-IBM-SpamModules-Scores: X-IBM-SpamModules-Versions: BY=3.00007432; HX=3.00000241; KW=3.00000007; PH=3.00000004; SC=3.00000214; SDB=6.00893408; UDB=6.00446643; IPR=6.00673572; BA=6.00005492; NDR=6.00000001; ZLA=6.00000005; ZF=6.00000009; ZB=6.00000000; ZP=6.00000000; ZH=6.00000000; ZU=6.00000002; MB=3.00016398; XFM=3.00000015; UTC=2017-07-27 01:31:50 X-IBM-AV-DETECTION: SAVI=unused REMOTE=unused XFE=unused x-cbparentid: 17072701-0005-0000-0000-0000806BECCE Message-Id: <1501119055-4060-2-git-send-email-mdroth@linux.vnet.ibm.com> X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10432:, , definitions=2017-07-26_13:, , signatures=0 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 spamscore=0 suspectscore=3 malwarescore=0 phishscore=0 adultscore=0 bulkscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.0.1-1706020000 definitions=main-1707270023 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x [generic] [fuzzy] X-Received-From: 148.163.158.5 Subject: [Qemu-devel] [PATCH for-2.10 1/3] qdev: store DeviceState's canonical path to use when unparenting 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: peter.maydell@linaro.org, "Michael S . Tsirkin" , groug@kaod.org, armbru@redhat.com, alex.williamson@redhat.com, pbonzini@redhat.com, david@gibson.dropbear.id.au 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" device_unparent(dev, ...) is called when a device is unparented, either directly, or as a result of a parent device being finalized, and handles some final cleanup for the device. Part of this includes emiting a DEVICE_DELETED QMP event to notify management, which includes the device's path in the composition tree as provided by object_get_canonical_path(). object_get_canonical_path() assumes the device is still connected to the machine/root container, and will assert otherwise, but in some situations this isn't the case: If the parent is finalized as a result of object_unparent(), it will still be attached to the composition tree at the time any children are unparented as a result of that same call to object_unparent(). However, in some cases, object_unparent() will complete without finalizing the parent device, due to lingering references that won't be released till some time later. One such example is if the parent has MemoryRegion children (which take a ref on their parent), who in turn have AddressSpace's (which take a ref on their regions), since those AddressSpaces get cleaned up asynchronously by the RCU thread. In this case qdev:device_unparent() may be called for a child Device that no longer has a path to the root/machine container, causing object_get_canonical_path() to assert. Fix this by storing the canonical path during realize() so the information will still be available for device_unparent() in such cases. Cc: Michael S. Tsirkin Cc: Paolo Bonzini Signed-off-by: Michael Roth Signed-off-by: Greg Kurz Signed-off-by: Michael Roth Tested-by: Eric Auger --- hw/core/qdev.c | 15 ++++++++++++--- include/hw/qdev-core.h | 1 + 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/hw/core/qdev.c b/hw/core/qdev.c index 606ab53..a64b35c 100644 --- a/hw/core/qdev.c +++ b/hw/core/qdev.c @@ -928,6 +928,12 @@ static void device_set_realized(Object *obj, bool valu= e, Error **errp) goto post_realize_fail; } =20 + /* always re-initialize since we clean up in device_unparent() ins= tead + * of unrealize() + */ + g_free(dev->canonical_path); + dev->canonical_path =3D object_get_canonical_path(OBJECT(dev)); + if (qdev_get_vmsd(dev)) { if (vmstate_register_with_alias_id(dev, -1, qdev_get_vmsd(dev)= , dev, dev->instance_id_alias, @@ -984,6 +990,7 @@ child_realize_fail: } =20 post_realize_fail: + g_free(dev->canonical_path); if (dc->unrealize) { dc->unrealize(dev, NULL); } @@ -1102,10 +1109,12 @@ static void device_unparent(Object *obj) =20 /* Only send event if the device had been completely realized */ if (dev->pending_deleted_event) { - gchar *path =3D object_get_canonical_path(OBJECT(dev)); + g_assert(dev->canonical_path); =20 - qapi_event_send_device_deleted(!!dev->id, dev->id, path, &error_ab= ort); - g_free(path); + qapi_event_send_device_deleted(!!dev->id, dev->id, dev->canonical_= path, + &error_abort); + g_free(dev->canonical_path); + dev->canonical_path =3D NULL; } =20 qemu_opts_del(dev->opts); diff --git a/include/hw/qdev-core.h b/include/hw/qdev-core.h index ae31728..9237b684 100644 --- a/include/hw/qdev-core.h +++ b/include/hw/qdev-core.h @@ -153,6 +153,7 @@ struct DeviceState { /*< public >*/ =20 const char *id; + char *canonical_path; bool realized; bool pending_deleted_event; QemuOpts *opts; --=20 2.7.4 From nobody Thu May 2 17:14:59 2024 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.zohomail.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 1501119219813658.0395342269719; Wed, 26 Jul 2017 18:33:39 -0700 (PDT) Received: from localhost ([::1]:40600 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1daXgH-0003iH-29 for importer@patchew.org; Wed, 26 Jul 2017 21:33:37 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:50877) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1daXei-0002sn-A5 for qemu-devel@nongnu.org; Wed, 26 Jul 2017 21:32:01 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1daXed-000884-D9 for qemu-devel@nongnu.org; Wed, 26 Jul 2017 21:32:00 -0400 Received: from mx0b-001b2d01.pphosted.com ([148.163.158.5]:40881 helo=mx0a-001b2d01.pphosted.com) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1daXed-000878-8F for qemu-devel@nongnu.org; Wed, 26 Jul 2017 21:31:55 -0400 Received: from pps.filterd (m0098419.ppops.net [127.0.0.1]) by mx0b-001b2d01.pphosted.com (8.16.0.21/8.16.0.21) with SMTP id v6R1Si4F049106 for ; Wed, 26 Jul 2017 21:31:54 -0400 Received: from e38.co.us.ibm.com (e38.co.us.ibm.com [32.97.110.159]) by mx0b-001b2d01.pphosted.com with ESMTP id 2bxyxvgmjp-1 (version=TLSv1.2 cipher=AES256-SHA bits=256 verify=NOT) for ; Wed, 26 Jul 2017 21:31:54 -0400 Received: from localhost by e38.co.us.ibm.com with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted for from ; Wed, 26 Jul 2017 19:31:53 -0600 Received: from b03cxnp07028.gho.boulder.ibm.com (9.17.130.15) by e38.co.us.ibm.com (192.168.1.138) with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted; Wed, 26 Jul 2017 19:31:49 -0600 Received: from b03ledav005.gho.boulder.ibm.com (b03ledav005.gho.boulder.ibm.com [9.17.130.236]) by b03cxnp07028.gho.boulder.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id v6R1VnMZ5439786; Wed, 26 Jul 2017 18:31:49 -0700 Received: from b03ledav005.gho.boulder.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 2072CBE039; Wed, 26 Jul 2017 19:31:49 -0600 (MDT) Received: from localhost (unknown [9.80.84.170]) by b03ledav005.gho.boulder.ibm.com (Postfix) with ESMTP id E21B7BE03A; Wed, 26 Jul 2017 19:31:48 -0600 (MDT) From: Michael Roth To: qemu-devel@nongnu.org Date: Wed, 26 Jul 2017 20:30:54 -0500 X-Mailer: git-send-email 2.7.4 In-Reply-To: <1501119055-4060-1-git-send-email-mdroth@linux.vnet.ibm.com> References: <1501119055-4060-1-git-send-email-mdroth@linux.vnet.ibm.com> X-TM-AS-GCONF: 00 x-cbid: 17072701-0028-0000-0000-000008186E34 X-IBM-SpamModules-Scores: X-IBM-SpamModules-Versions: BY=3.00007432; HX=3.00000241; KW=3.00000007; PH=3.00000004; SC=3.00000214; SDB=6.00893408; UDB=6.00446643; IPR=6.00673572; BA=6.00005492; NDR=6.00000001; ZLA=6.00000005; ZF=6.00000009; ZB=6.00000000; ZP=6.00000000; ZH=6.00000000; ZU=6.00000002; MB=3.00016398; XFM=3.00000015; UTC=2017-07-27 01:31:52 X-IBM-AV-DETECTION: SAVI=unused REMOTE=unused XFE=unused x-cbparentid: 17072701-0029-0000-0000-000036DD4F45 Message-Id: <1501119055-4060-3-git-send-email-mdroth@linux.vnet.ibm.com> X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10432:, , definitions=2017-07-26_13:, , signatures=0 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 spamscore=0 suspectscore=3 malwarescore=0 phishscore=0 adultscore=0 bulkscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.0.1-1706020000 definitions=main-1707270023 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x [generic] [fuzzy] X-Received-From: 148.163.158.5 Subject: [Qemu-devel] [PATCH for-2.10 2/3] Revert "qdev: Free QemuOpts when the QOM path goes away" 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: peter.maydell@linaro.org, groug@kaod.org, armbru@redhat.com, alex.williamson@redhat.com, pbonzini@redhat.com, david@gibson.dropbear.id.au 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" This reverts commit abed886ec60cf239a03515cf0b30fb11fa964c44. This patch originally addressed an issue where a DEVICE_DELETED event could be emitted (in device_unparent()) before a Device's QemuOpts were cleaned up (in device_finalize()), leading to a "duplicate ID" error if management attempted to immediately add a device with the same ID in response to the DEVICE_DELETED event. An alternative will be implemented in a subsequent patch where we defer the DEVICE_DELETED event until device_finalize(), which would also prevent the race, so we revert the original fix in preparation. Signed-off-by: Michael Roth Reviewed-by: Greg Kurz Tested-by: Eric Auger --- hw/core/qdev.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/hw/core/qdev.c b/hw/core/qdev.c index a64b35c..08c4061 100644 --- a/hw/core/qdev.c +++ b/hw/core/qdev.c @@ -1067,6 +1067,7 @@ static void device_finalize(Object *obj) NamedGPIOList *ngl, *next; =20 DeviceState *dev =3D DEVICE(obj); + qemu_opts_del(dev->opts); =20 QLIST_FOREACH_SAFE(ngl, &dev->gpios, node, next) { QLIST_REMOVE(ngl, node); @@ -1116,9 +1117,6 @@ static void device_unparent(Object *obj) g_free(dev->canonical_path); dev->canonical_path =3D NULL; } - - qemu_opts_del(dev->opts); - dev->opts =3D NULL; } =20 static void device_class_init(ObjectClass *class, void *data) --=20 2.7.4 From nobody Thu May 2 17:14:59 2024 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.zohomail.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 1501119220002297.8102225758689; Wed, 26 Jul 2017 18:33:40 -0700 (PDT) Received: from localhost ([::1]:40602 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1daXgI-0003iy-0V for importer@patchew.org; Wed, 26 Jul 2017 21:33:38 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:50892) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1daXei-0002st-UE for qemu-devel@nongnu.org; Wed, 26 Jul 2017 21:32:02 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1daXed-00088a-W9 for qemu-devel@nongnu.org; Wed, 26 Jul 2017 21:32:00 -0400 Received: from mx0b-001b2d01.pphosted.com ([148.163.158.5]:40950 helo=mx0a-001b2d01.pphosted.com) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1daXed-00088D-Rh for qemu-devel@nongnu.org; Wed, 26 Jul 2017 21:31:55 -0400 Received: from pps.filterd (m0098419.ppops.net [127.0.0.1]) by mx0b-001b2d01.pphosted.com (8.16.0.21/8.16.0.21) with SMTP id v6R1Siqm049105 for ; Wed, 26 Jul 2017 21:31:55 -0400 Received: from e31.co.us.ibm.com (e31.co.us.ibm.com [32.97.110.149]) by mx0b-001b2d01.pphosted.com with ESMTP id 2bxyxvgmkg-1 (version=TLSv1.2 cipher=AES256-SHA bits=256 verify=NOT) for ; Wed, 26 Jul 2017 21:31:55 -0400 Received: from localhost by e31.co.us.ibm.com with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted for from ; Wed, 26 Jul 2017 19:31:54 -0600 Received: from b03cxnp08025.gho.boulder.ibm.com (9.17.130.17) by e31.co.us.ibm.com (192.168.1.131) with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted; Wed, 26 Jul 2017 19:31:50 -0600 Received: from b03ledav006.gho.boulder.ibm.com (b03ledav006.gho.boulder.ibm.com [9.17.130.237]) by b03cxnp08025.gho.boulder.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id v6R1Vo6R64094416; Wed, 26 Jul 2017 18:31:50 -0700 Received: from b03ledav006.gho.boulder.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id F352DC6042; Wed, 26 Jul 2017 19:31:49 -0600 (MDT) Received: from localhost (unknown [9.80.84.170]) by b03ledav006.gho.boulder.ibm.com (Postfix) with ESMTP id C02BBC6037; Wed, 26 Jul 2017 19:31:49 -0600 (MDT) From: Michael Roth To: qemu-devel@nongnu.org Date: Wed, 26 Jul 2017 20:30:55 -0500 X-Mailer: git-send-email 2.7.4 In-Reply-To: <1501119055-4060-1-git-send-email-mdroth@linux.vnet.ibm.com> References: <1501119055-4060-1-git-send-email-mdroth@linux.vnet.ibm.com> X-TM-AS-GCONF: 00 x-cbid: 17072701-8235-0000-0000-00000C016FCB X-IBM-SpamModules-Scores: X-IBM-SpamModules-Versions: BY=3.00007432; HX=3.00000241; KW=3.00000007; PH=3.00000004; SC=3.00000214; SDB=6.00893408; UDB=6.00446643; IPR=6.00673572; BA=6.00005492; NDR=6.00000001; ZLA=6.00000005; ZF=6.00000009; ZB=6.00000000; ZP=6.00000000; ZH=6.00000000; ZU=6.00000002; MB=3.00016398; XFM=3.00000015; UTC=2017-07-27 01:31:53 X-IBM-AV-DETECTION: SAVI=unused REMOTE=unused XFE=unused x-cbparentid: 17072701-8236-0000-0000-00003CF12779 Message-Id: <1501119055-4060-4-git-send-email-mdroth@linux.vnet.ibm.com> X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10432:, , definitions=2017-07-26_13:, , signatures=0 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 spamscore=0 suspectscore=3 malwarescore=0 phishscore=0 adultscore=0 bulkscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.0.1-1706020000 definitions=main-1707270023 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x [generic] [fuzzy] X-Received-From: 148.163.158.5 Subject: [Qemu-devel] [PATCH for-2.10 3/3] qdev: defer DEVICE_DEL event until instance_finalize() 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: peter.maydell@linaro.org, groug@kaod.org, armbru@redhat.com, alex.williamson@redhat.com, pbonzini@redhat.com, david@gibson.dropbear.id.au 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" DEVICE_DEL is currently emitted when a Device is unparented, as opposed to when it is finalized. The main design motivation for this seems to be that after unparent()/unrealize(), the Device is no longer visible to the guest, and thus the operation is complete from the perspective of management. However, there are cases where remaining host-side cleanup is also pertinent to management. The is generally handled by treating these resources as aspects of the "backend", which can be managed via separate interfaces/events, such as blockdev_add/del, netdev_add/del, object_add/del, etc, but some devices do not have this level of compartmentalization, namely vfio-pci, and possibly to lend themselves well to it. In the case of vfio-pci, the "backend" cleanup happens as part of the finalization of the vfio-pci device itself, in particular the cleanup of the VFIO group FD. Failing to wait for this cleanup can result in tools like libvirt attempting to rebind the device to the host while it's still being used by VFIO, which can result in host crashes or other misbehavior depending on the host driver. Deferring DEVICE_DEL still affords us the ability to manage backends explicitly, while also addressing cases like vfio-pci's, so we implement that approach here. An alternative proposal involving having VFIO emit a separate event to denote completion of host-side cleanup was discussed, but the prevailing opinion seems to be that it is not worth the added complexity, and leaves the issue open for other Device implementations solve in the future. Signed-off-by: Michael Roth Reviewed-by: Greg Kurz Tested-by: Eric Auger --- hw/core/qdev.c | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/hw/core/qdev.c b/hw/core/qdev.c index 08c4061..d14acba 100644 --- a/hw/core/qdev.c +++ b/hw/core/qdev.c @@ -1067,7 +1067,6 @@ static void device_finalize(Object *obj) NamedGPIOList *ngl, *next; =20 DeviceState *dev =3D DEVICE(obj); - qemu_opts_del(dev->opts); =20 QLIST_FOREACH_SAFE(ngl, &dev->gpios, node, next) { QLIST_REMOVE(ngl, node); @@ -1078,6 +1077,18 @@ static void device_finalize(Object *obj) * here */ } + + /* Only send event if the device had been completely realized */ + if (dev->pending_deleted_event) { + g_assert(dev->canonical_path); + + qapi_event_send_device_deleted(!!dev->id, dev->id, dev->canonical_= path, + &error_abort); + g_free(dev->canonical_path); + dev->canonical_path =3D NULL; + } + + qemu_opts_del(dev->opts); } =20 static void device_class_base_init(ObjectClass *class, void *data) @@ -1107,16 +1118,6 @@ static void device_unparent(Object *obj) object_unref(OBJECT(dev->parent_bus)); dev->parent_bus =3D NULL; } - - /* Only send event if the device had been completely realized */ - if (dev->pending_deleted_event) { - g_assert(dev->canonical_path); - - qapi_event_send_device_deleted(!!dev->id, dev->id, dev->canonical_= path, - &error_abort); - g_free(dev->canonical_path); - dev->canonical_path =3D NULL; - } } =20 static void device_class_init(ObjectClass *class, void *data) --=20 2.7.4