From nobody Mon Apr 29 00:22:47 2024 Delivered-To: importer@patchew.org Received-SPF: temperror (zoho.com: Error in retrieving data from DNS) 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=temperror (zoho.com: Error in retrieving data from DNS) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (208.118.235.17 [208.118.235.17]) by mx.zohomail.com with SMTPS id 150819274431342.75799568227421; Mon, 16 Oct 2017 15:25:44 -0700 (PDT) Received: from localhost ([::1]:35486 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1e4Dp8-0003m9-OL for importer@patchew.org; Mon, 16 Oct 2017 18:25:26 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:43941) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1e4DnX-0002Tc-IO for qemu-devel@nongnu.org; Mon, 16 Oct 2017 18:23:48 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1e4DnS-0002HR-CQ for qemu-devel@nongnu.org; Mon, 16 Oct 2017 18:23:47 -0400 Received: from mx0a-001b2d01.pphosted.com ([148.163.156.1]:38644) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1e4DnS-0002F2-32 for qemu-devel@nongnu.org; Mon, 16 Oct 2017 18:23:42 -0400 Received: from pps.filterd (m0098410.ppops.net [127.0.0.1]) by mx0a-001b2d01.pphosted.com (8.16.0.21/8.16.0.21) with SMTP id v9GMJfDK029261 for ; Mon, 16 Oct 2017 18:23:37 -0400 Received: from e31.co.us.ibm.com (e31.co.us.ibm.com [32.97.110.149]) by mx0a-001b2d01.pphosted.com with ESMTP id 2dn1u1h0sr-1 (version=TLSv1.2 cipher=AES256-SHA bits=256 verify=NOT) for ; Mon, 16 Oct 2017 18:23:37 -0400 Received: from localhost by e31.co.us.ibm.com with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted for from ; Mon, 16 Oct 2017 16:23:36 -0600 Received: from b03cxnp08026.gho.boulder.ibm.com (9.17.130.18) by e31.co.us.ibm.com (192.168.1.131) with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted; Mon, 16 Oct 2017 16:23:33 -0600 Received: from b03ledav004.gho.boulder.ibm.com (b03ledav004.gho.boulder.ibm.com [9.17.130.235]) by b03cxnp08026.gho.boulder.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id v9GMNWLf60686356; Mon, 16 Oct 2017 15:23:32 -0700 Received: from b03ledav004.gho.boulder.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 92DB778038; Mon, 16 Oct 2017 16:23:32 -0600 (MDT) Received: from localhost (unknown [9.80.108.22]) by b03ledav004.gho.boulder.ibm.com (Postfix) with ESMTP id 62A677803F; Mon, 16 Oct 2017 16:23:32 -0600 (MDT) From: Michael Roth To: qemu-devel@nongnu.org Date: Mon, 16 Oct 2017 17:23:13 -0500 X-Mailer: git-send-email 2.11.0 In-Reply-To: <20171016222315.407-1-mdroth@linux.vnet.ibm.com> References: <20171016222315.407-1-mdroth@linux.vnet.ibm.com> X-TM-AS-GCONF: 00 x-cbid: 17101622-8235-0000-0000-00000C6BB1EC X-IBM-SpamModules-Scores: X-IBM-SpamModules-Versions: BY=3.00007901; HX=3.00000241; KW=3.00000007; PH=3.00000004; SC=3.00000236; SDB=6.00932130; UDB=6.00469373; IPR=6.00712420; BA=6.00005641; NDR=6.00000001; ZLA=6.00000005; ZF=6.00000009; ZB=6.00000000; ZP=6.00000000; ZH=6.00000000; ZU=6.00000002; MB=3.00017565; XFM=3.00000015; UTC=2017-10-16 22:23:35 X-IBM-AV-DETECTION: SAVI=unused REMOTE=unused XFE=unused x-cbparentid: 17101622-8236-0000-0000-00003E10DE4A Message-Id: <20171016222315.407-2-mdroth@linux.vnet.ibm.com> X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10432:, , definitions=2017-10-16_07:, , 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-1707230000 definitions=main-1710160313 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x [generic] [fuzzy] X-Received-From: 148.163.156.1 Subject: [Qemu-devel] [PATCH v3 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, ehabkost@redhat.com, "Michael S . Tsirkin" , armbru@redhat.com, groug@kaod.org, alex.williamson@redhat.com, pbonzini@redhat.com, imammedo@redhat.com, david@gibson.dropbear.id.au Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_6 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 Reviewed-by: David Gibson --- hw/core/qdev.c | 16 +++++++++++++--- include/hw/qdev-core.h | 1 + 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/hw/core/qdev.c b/hw/core/qdev.c index 606ab53c42..ebf9d822b5 100644 --- a/hw/core/qdev.c +++ b/hw/core/qdev.c @@ -928,6 +928,13 @@ static void device_set_realized(Object *obj, bool valu= e, Error **errp) goto post_realize_fail; } =20 + /* + * always free/re-initialize here since the value cannot be cleane= d up + * in device_unrealize due to its usage later on in the unplug path + */ + 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 +991,7 @@ child_realize_fail: } =20 post_realize_fail: + g_free(dev->canonical_path); if (dc->unrealize) { dc->unrealize(dev, NULL); } @@ -1102,10 +1110,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 089146197f..0a71bf83f0 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.11.0 From nobody Mon Apr 29 00:22:47 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 1508192873404744.8025784669125; Mon, 16 Oct 2017 15:27:53 -0700 (PDT) Received: from localhost ([::1]:35500 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1e4DrL-0006CH-Mt for importer@patchew.org; Mon, 16 Oct 2017 18:27:43 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:43964) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1e4DnY-0002Tp-Ro for qemu-devel@nongnu.org; Mon, 16 Oct 2017 18:23:49 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1e4DnT-0002Is-N6 for qemu-devel@nongnu.org; Mon, 16 Oct 2017 18:23:48 -0400 Received: from mx0a-001b2d01.pphosted.com ([148.163.156.1]:55380) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1e4DnT-0002Hq-DF for qemu-devel@nongnu.org; Mon, 16 Oct 2017 18:23:43 -0400 Received: from pps.filterd (m0098404.ppops.net [127.0.0.1]) by mx0a-001b2d01.pphosted.com (8.16.0.21/8.16.0.21) with SMTP id v9GMNa7x031119 for ; Mon, 16 Oct 2017 18:23:38 -0400 Received: from e37.co.us.ibm.com (e37.co.us.ibm.com [32.97.110.158]) by mx0a-001b2d01.pphosted.com with ESMTP id 2dn4jy9y60-1 (version=TLSv1.2 cipher=AES256-SHA bits=256 verify=NOT) for ; Mon, 16 Oct 2017 18:23:38 -0400 Received: from localhost by e37.co.us.ibm.com with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted for from ; Mon, 16 Oct 2017 16:23:36 -0600 Received: from b03cxnp08027.gho.boulder.ibm.com (9.17.130.19) by e37.co.us.ibm.com (192.168.1.137) with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted; Mon, 16 Oct 2017 16:23:34 -0600 Received: from b03ledav005.gho.boulder.ibm.com (b03ledav005.gho.boulder.ibm.com [9.17.130.236]) by b03cxnp08027.gho.boulder.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id v9GMNXEZ62455980; Mon, 16 Oct 2017 15:23:33 -0700 Received: from b03ledav005.gho.boulder.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 99672BE039; Mon, 16 Oct 2017 16:23:33 -0600 (MDT) Received: from localhost (unknown [9.80.108.22]) by b03ledav005.gho.boulder.ibm.com (Postfix) with ESMTP id 67E8CBE038; Mon, 16 Oct 2017 16:23:33 -0600 (MDT) From: Michael Roth To: qemu-devel@nongnu.org Date: Mon, 16 Oct 2017 17:23:14 -0500 X-Mailer: git-send-email 2.11.0 In-Reply-To: <20171016222315.407-1-mdroth@linux.vnet.ibm.com> References: <20171016222315.407-1-mdroth@linux.vnet.ibm.com> X-TM-AS-GCONF: 00 x-cbid: 17101622-0024-0000-0000-00001758AA66 X-IBM-SpamModules-Scores: X-IBM-SpamModules-Versions: BY=3.00007901; HX=3.00000241; KW=3.00000007; PH=3.00000004; SC=3.00000236; SDB=6.00932130; UDB=6.00469374; IPR=6.00712420; BA=6.00005641; NDR=6.00000001; ZLA=6.00000005; ZF=6.00000009; ZB=6.00000000; ZP=6.00000000; ZH=6.00000000; ZU=6.00000002; MB=3.00017565; XFM=3.00000015; UTC=2017-10-16 22:23:36 X-IBM-AV-DETECTION: SAVI=unused REMOTE=unused XFE=unused x-cbparentid: 17101622-0025-0000-0000-00004D217FD0 Message-Id: <20171016222315.407-3-mdroth@linux.vnet.ibm.com> X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10432:, , definitions=2017-10-16_07:, , 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-1707230000 definitions=main-1710160313 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x [generic] [fuzzy] X-Received-From: 148.163.156.1 Subject: [Qemu-devel] [PATCH v3 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, ehabkost@redhat.com, armbru@redhat.com, groug@kaod.org, alex.williamson@redhat.com, pbonzini@redhat.com, imammedo@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 Reviewed-by: David Gibson --- 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 ebf9d822b5..0aa66a2e3c 100644 --- a/hw/core/qdev.c +++ b/hw/core/qdev.c @@ -1068,6 +1068,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); @@ -1117,9 +1118,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.11.0 From nobody Mon Apr 29 00:22:47 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 (208.118.235.17 [208.118.235.17]) by mx.zohomail.com with SMTPS id 1508192749327420.34268890263263; Mon, 16 Oct 2017 15:25:49 -0700 (PDT) Received: from localhost ([::1]:35487 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1e4DpA-0003rQ-O6 for importer@patchew.org; Mon, 16 Oct 2017 18:25:28 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:43940) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1e4DnX-0002Tb-I6 for qemu-devel@nongnu.org; Mon, 16 Oct 2017 18:23:48 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1e4DnS-0002Hk-Mf for qemu-devel@nongnu.org; Mon, 16 Oct 2017 18:23:47 -0400 Received: from mx0b-001b2d01.pphosted.com ([148.163.158.5]:53502 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 1e4DnS-0002H9-Gl for qemu-devel@nongnu.org; Mon, 16 Oct 2017 18:23:42 -0400 Received: from pps.filterd (m0098416.ppops.net [127.0.0.1]) by mx0b-001b2d01.pphosted.com (8.16.0.21/8.16.0.21) with SMTP id v9GMJYfh017935 for ; Mon, 16 Oct 2017 18:23:39 -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 2dn26mfsuv-1 (version=TLSv1.2 cipher=AES256-SHA bits=256 verify=NOT) for ; Mon, 16 Oct 2017 18:23:39 -0400 Received: from localhost by e31.co.us.ibm.com with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted for from ; Mon, 16 Oct 2017 16:23:38 -0600 Received: from b03cxnp07029.gho.boulder.ibm.com (9.17.130.16) by e31.co.us.ibm.com (192.168.1.131) with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted; Mon, 16 Oct 2017 16:23:34 -0600 Received: from b03ledav006.gho.boulder.ibm.com (b03ledav006.gho.boulder.ibm.com [9.17.130.237]) by b03cxnp07029.gho.boulder.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id v9GMNYoN60031090; Mon, 16 Oct 2017 15:23:34 -0700 Received: from b03ledav006.gho.boulder.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 6C43BC6042; Mon, 16 Oct 2017 16:23:34 -0600 (MDT) Received: from localhost (unknown [9.80.108.22]) by b03ledav006.gho.boulder.ibm.com (Postfix) with ESMTP id 3BA51C603E; Mon, 16 Oct 2017 16:23:34 -0600 (MDT) From: Michael Roth To: qemu-devel@nongnu.org Date: Mon, 16 Oct 2017 17:23:15 -0500 X-Mailer: git-send-email 2.11.0 In-Reply-To: <20171016222315.407-1-mdroth@linux.vnet.ibm.com> References: <20171016222315.407-1-mdroth@linux.vnet.ibm.com> X-TM-AS-GCONF: 00 x-cbid: 17101622-8235-0000-0000-00000C6BB1EF X-IBM-SpamModules-Scores: X-IBM-SpamModules-Versions: BY=3.00007901; HX=3.00000241; KW=3.00000007; PH=3.00000004; SC=3.00000236; SDB=6.00932130; UDB=6.00469373; IPR=6.00712420; BA=6.00005641; NDR=6.00000001; ZLA=6.00000005; ZF=6.00000009; ZB=6.00000000; ZP=6.00000000; ZH=6.00000000; ZU=6.00000002; MB=3.00017565; XFM=3.00000015; UTC=2017-10-16 22:23:37 X-IBM-AV-DETECTION: SAVI=unused REMOTE=unused XFE=unused x-cbparentid: 17101622-8236-0000-0000-00003E10DE50 Message-Id: <20171016222315.407-4-mdroth@linux.vnet.ibm.com> X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10432:, , definitions=2017-10-16_07:, , 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-1707230000 definitions=main-1710160313 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 v3 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, ehabkost@redhat.com, armbru@redhat.com, groug@kaod.org, alex.williamson@redhat.com, pbonzini@redhat.com, imammedo@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 to solve in the future. Signed-off-by: Michael Roth Reviewed-by: Greg Kurz Tested-by: Eric Auger Reviewed-by: David Gibson --- 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 0aa66a2e3c..441bed2419 100644 --- a/hw/core/qdev.c +++ b/hw/core/qdev.c @@ -1068,7 +1068,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); @@ -1079,6 +1078,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) @@ -1108,16 +1119,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.11.0