From nobody Sat May 4 00:15:19 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 1507569061412168.6440574691585; Mon, 9 Oct 2017 10:11:01 -0700 (PDT) Received: from localhost ([::1]:58951 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1e1bZt-0003rg-Hf for importer@patchew.org; Mon, 09 Oct 2017 13:10:53 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:46337) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1e1bVy-00014X-QX for qemu-devel@nongnu.org; Mon, 09 Oct 2017 13:06:51 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1e1bVt-0002Ao-Mj for qemu-devel@nongnu.org; Mon, 09 Oct 2017 13:06:50 -0400 Received: from mx0b-001b2d01.pphosted.com ([148.163.158.5]:54552 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 1e1bVt-00029s-HA for qemu-devel@nongnu.org; Mon, 09 Oct 2017 13:06:45 -0400 Received: from pps.filterd (m0098420.ppops.net [127.0.0.1]) by mx0b-001b2d01.pphosted.com (8.16.0.21/8.16.0.21) with SMTP id v99H4e5x083552 for ; Mon, 9 Oct 2017 13:06:41 -0400 Received: from e15.ny.us.ibm.com (e15.ny.us.ibm.com [129.33.205.205]) by mx0b-001b2d01.pphosted.com with ESMTP id 2dgc1m44r3-1 (version=TLSv1.2 cipher=AES256-SHA bits=256 verify=NOT) for ; Mon, 09 Oct 2017 13:06:41 -0400 Received: from localhost by e15.ny.us.ibm.com with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted for from ; Mon, 9 Oct 2017 13:06:40 -0400 Received: from b01cxnp23034.gho.pok.ibm.com (9.57.198.29) by e15.ny.us.ibm.com (146.89.104.202) with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted; Mon, 9 Oct 2017 13:06:37 -0400 Received: from b01ledav003.gho.pok.ibm.com (b01ledav003.gho.pok.ibm.com [9.57.199.108]) by b01cxnp23034.gho.pok.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id v99H6a0p41025780; Mon, 9 Oct 2017 17:06:36 GMT Received: from b01ledav003.gho.pok.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 17BE4B2046; Mon, 9 Oct 2017 13:03:54 -0400 (EDT) Received: from localhost (unknown [9.80.88.151]) by b01ledav003.gho.pok.ibm.com (Postfix) with ESMTP id D2690B2050; Mon, 9 Oct 2017 13:03:53 -0400 (EDT) From: Michael Roth To: qemu-devel@nongnu.org Date: Mon, 9 Oct 2017 12:06:05 -0500 X-Mailer: git-send-email 2.11.0 In-Reply-To: <20171009170607.4155-1-mdroth@linux.vnet.ibm.com> References: <20171009170607.4155-1-mdroth@linux.vnet.ibm.com> X-TM-AS-GCONF: 00 x-cbid: 17100917-0036-0000-0000-00000278314C X-IBM-SpamModules-Scores: X-IBM-SpamModules-Versions: BY=3.00007869; HX=3.00000241; KW=3.00000007; PH=3.00000004; SC=3.00000235; SDB=6.00928712; UDB=6.00467406; IPR=6.00708984; BA=6.00005630; NDR=6.00000001; ZLA=6.00000005; ZF=6.00000009; ZB=6.00000000; ZP=6.00000000; ZH=6.00000000; ZU=6.00000002; MB=3.00017460; XFM=3.00000015; UTC=2017-10-09 17:06:40 X-IBM-AV-DETECTION: SAVI=unused REMOTE=unused XFE=unused x-cbparentid: 17100917-0037-0000-0000-000042059B99 Message-Id: <20171009170607.4155-2-mdroth@linux.vnet.ibm.com> X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10432:, , definitions=2017-10-09_04:, , 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-1710090250 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 v2 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" , groug@kaod.org, armbru@redhat.com, alex.williamson@redhat.com, imammedo@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 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..0542e1879f 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 it's usage later on in the unplug pa= th + */ + 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 Sat May 4 00:15:19 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 1507569139842274.5946597907366; Mon, 9 Oct 2017 10:12:19 -0700 (PDT) Received: from localhost ([::1]:58955 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1e1bb1-0004XZ-4G for importer@patchew.org; Mon, 09 Oct 2017 13:12:03 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:46364) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1e1bW0-00014t-2W for qemu-devel@nongnu.org; Mon, 09 Oct 2017 13:06:52 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1e1bVu-0002BR-Tj for qemu-devel@nongnu.org; Mon, 09 Oct 2017 13:06:52 -0400 Received: from mx0a-001b2d01.pphosted.com ([148.163.156.1]:44342) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1e1bVu-0002B4-KQ for qemu-devel@nongnu.org; Mon, 09 Oct 2017 13:06:46 -0400 Received: from pps.filterd (m0098393.ppops.net [127.0.0.1]) by mx0a-001b2d01.pphosted.com (8.16.0.21/8.16.0.21) with SMTP id v99H4fpJ012206 for ; Mon, 9 Oct 2017 13:06:42 -0400 Received: from e13.ny.us.ibm.com (e13.ny.us.ibm.com [129.33.205.203]) by mx0a-001b2d01.pphosted.com with ESMTP id 2dgb0eqg82-1 (version=TLSv1.2 cipher=AES256-SHA bits=256 verify=NOT) for ; Mon, 09 Oct 2017 13:06:42 -0400 Received: from localhost by e13.ny.us.ibm.com with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted for from ; Mon, 9 Oct 2017 13:06:41 -0400 Received: from b01cxnp22034.gho.pok.ibm.com (9.57.198.24) by e13.ny.us.ibm.com (146.89.104.200) with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted; Mon, 9 Oct 2017 13:06:37 -0400 Received: from b01ledav004.gho.pok.ibm.com (b01ledav004.gho.pok.ibm.com [9.57.199.109]) by b01cxnp22034.gho.pok.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id v99H6bhJ51970186; Mon, 9 Oct 2017 17:06:37 GMT Received: from b01ledav004.gho.pok.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 30526112034; Mon, 9 Oct 2017 13:06:12 -0400 (EDT) Received: from localhost (unknown [9.80.88.151]) by b01ledav004.gho.pok.ibm.com (Postfix) with ESMTP id EB0F1112040; Mon, 9 Oct 2017 13:06:11 -0400 (EDT) From: Michael Roth To: qemu-devel@nongnu.org Date: Mon, 9 Oct 2017 12:06:06 -0500 X-Mailer: git-send-email 2.11.0 In-Reply-To: <20171009170607.4155-1-mdroth@linux.vnet.ibm.com> References: <20171009170607.4155-1-mdroth@linux.vnet.ibm.com> X-TM-AS-GCONF: 00 x-cbid: 17100917-0008-0000-0000-0000028E009A X-IBM-SpamModules-Scores: X-IBM-SpamModules-Versions: BY=3.00007869; HX=3.00000241; KW=3.00000007; PH=3.00000004; SC=3.00000235; SDB=6.00928713; UDB=6.00467406; IPR=6.00708984; BA=6.00005631; NDR=6.00000001; ZLA=6.00000005; ZF=6.00000009; ZB=6.00000000; ZP=6.00000000; ZH=6.00000000; ZU=6.00000002; MB=3.00017460; XFM=3.00000015; UTC=2017-10-09 17:06:39 X-IBM-AV-DETECTION: SAVI=unused REMOTE=unused XFE=unused x-cbparentid: 17100917-0009-0000-0000-000036F402FC Message-Id: <20171009170607.4155-3-mdroth@linux.vnet.ibm.com> X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10432:, , definitions=2017-10-09_04:, , 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-1710090250 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 v2 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, groug@kaod.org, armbru@redhat.com, alex.williamson@redhat.com, imammedo@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 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 0542e1879f..f7c66d9bd0 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 Sat May 4 00:15:19 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 1507568920271336.6472822646058; Mon, 9 Oct 2017 10:08:40 -0700 (PDT) Received: from localhost ([::1]:58938 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1e1bXe-00025V-F4 for importer@patchew.org; Mon, 09 Oct 2017 13:08:34 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:46383) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1e1bW2-000192-1K for qemu-devel@nongnu.org; Mon, 09 Oct 2017 13:06:58 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1e1bVw-0002Cu-Vz for qemu-devel@nongnu.org; Mon, 09 Oct 2017 13:06:54 -0400 Received: from mx0a-001b2d01.pphosted.com ([148.163.156.1]:42396) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1e1bVw-0002C5-N9 for qemu-devel@nongnu.org; Mon, 09 Oct 2017 13:06:48 -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 v99H4fbH144894 for ; Mon, 9 Oct 2017 13:06:43 -0400 Received: from e12.ny.us.ibm.com (e12.ny.us.ibm.com [129.33.205.202]) by mx0a-001b2d01.pphosted.com with ESMTP id 2dgaucquk1-1 (version=TLSv1.2 cipher=AES256-SHA bits=256 verify=NOT) for ; Mon, 09 Oct 2017 13:06:43 -0400 Received: from localhost by e12.ny.us.ibm.com with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted for from ; Mon, 9 Oct 2017 13:06:42 -0400 Received: from b01cxnp22034.gho.pok.ibm.com (9.57.198.24) by e12.ny.us.ibm.com (146.89.104.199) with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted; Mon, 9 Oct 2017 13:06:38 -0400 Received: from b01ledav005.gho.pok.ibm.com (b01ledav005.gho.pok.ibm.com [9.57.199.110]) by b01cxnp22034.gho.pok.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id v99H6bgx52887634; Mon, 9 Oct 2017 17:06:37 GMT Received: from b01ledav005.gho.pok.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id B0A8FAE03B; Mon, 9 Oct 2017 13:07:15 -0400 (EDT) Received: from localhost (unknown [9.80.88.151]) by b01ledav005.gho.pok.ibm.com (Postfix) with ESMTP id 775C7AE04B; Mon, 9 Oct 2017 13:07:15 -0400 (EDT) From: Michael Roth To: qemu-devel@nongnu.org Date: Mon, 9 Oct 2017 12:06:07 -0500 X-Mailer: git-send-email 2.11.0 In-Reply-To: <20171009170607.4155-1-mdroth@linux.vnet.ibm.com> References: <20171009170607.4155-1-mdroth@linux.vnet.ibm.com> X-TM-AS-GCONF: 00 x-cbid: 17100917-0048-0000-0000-000001F30085 X-IBM-SpamModules-Scores: X-IBM-SpamModules-Versions: BY=3.00007869; HX=3.00000241; KW=3.00000007; PH=3.00000004; SC=3.00000235; SDB=6.00928713; UDB=6.00467406; IPR=6.00708984; BA=6.00005631; NDR=6.00000001; ZLA=6.00000005; ZF=6.00000009; ZB=6.00000000; ZP=6.00000000; ZH=6.00000000; ZU=6.00000002; MB=3.00017460; XFM=3.00000015; UTC=2017-10-09 17:06:40 X-IBM-AV-DETECTION: SAVI=unused REMOTE=unused XFE=unused x-cbparentid: 17100917-0049-0000-0000-000042D102F1 Message-Id: <20171009170607.4155-4-mdroth@linux.vnet.ibm.com> X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10432:, , definitions=2017-10-09_04:, , 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-1710090250 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 v2 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, groug@kaod.org, armbru@redhat.com, alex.williamson@redhat.com, imammedo@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 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 f7c66d9bd0..8b7b8c3280 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