Skip to contentSkip to conversation

vfio/container: Fix container object destruction

PATCH In review Cédric Le Goater 3 participants 3 messages Nov 15, 2024 – Nov 18, 2024

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 vfio/container: Fix container object destruction

When commit 96b7af4388b3 intoduced a .instance_finalize() handler, it did not take into account that the container was not necessarily inserted into the container list of the address space. Hence, if the container object is destroyed, by calling object_unref() for example, before vfio_address_space_insert() is called, QEMU may crash when removing the container from the list as done in vfio_container_instance_finalize(). This was seen with an SEV-SNP guest for which discarding of RAM fails.

To resolve this issue, use the safe version of QLIST_REMOVE().

Cc Zhenzhong Duan <[email protected]>
Cc Eric Auger <[email protected]>
Fixes 96b7af4388b3 ("vfio/container: Move vfio_container_destroy() to an instance_finalize() handler")

---

Changes in v2:

- use the safe version of QLIST_REMOVE() instead of calling vfio_address_space_insert() earlier.

 hw/vfio/container-base.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
hw/vfio/container-base.c +1 -1
index 809b15767425a48f2404b08fc409ee5684af2094..6f86c37d971ec38426dacd471bca837c0d0df806 100644
@@ -103,7 +103,7 @@ static void vfio_container_instance_finalize(Object *obj)
     VFIOContainerBase *bcontainer = VFIO_IOMMU(obj);
     VFIOGuestIOMMU *giommu, *tmp;
 
-    QLIST_REMOVE(bcontainer, next);
+    QLIST_SAFE_REMOVE(bcontainer, next);
 
     QLIST_FOREACH_SAFE(giommu, &bcontainer->giommu_list, giommu_next, tmp) {
         memory_region_unregister_iommu_notifier(
Signature · 1 line
2.47.0
Reviewed-by on v2 1/1
#1 lore raw
Quoted 16 lines

-----Original Message-----
From: Cédric Le Goater <[email protected]>
Sent: Friday, November 15, 2024 4:35 PM
Subject: [PATCH v2] vfio/container: Fix container object destruction

When commit 96b7af4388b3 intoduced a .instance_finalize() handler, it did not take into account that the container was not necessarily inserted into the container list of the address space. Hence, if the container object is destroyed, by calling object_unref() for example, before vfio_address_space_insert() is called, QEMU may crash when removing the container from the list as done in vfio_container_instance_finalize(). This was seen with an SEV-SNP guest for which discarding of RAM fails.

Quoted 7 lines

To resolve this issue, use the safe version of QLIST_REMOVE().

Cc Zhenzhong Duan <[email protected]>
Cc Eric Auger <[email protected]>
Fixes 96b7af4388b3 ("vfio/container: Move vfio_container_destroy() to an

instance_finalize() handler")

Thanks
Zhenzhong

Reviewed-by on v2 1/1
#2 lore raw
In reply to Cédric Le Goater

Hi Cédric,

Quoted 9 lines · Cédric Le Goater

When commit 96b7af4388b3 intoduced a .instance_finalize() handler, it did not take into account that the container was not necessarily inserted into the container list of the address space. Hence, if the container object is destroyed, by calling object_unref() for example, before vfio_address_space_insert() is called, QEMU may crash when removing the container from the list as done in vfio_container_instance_finalize(). This was seen with an SEV-SNP guest for which discarding of RAM fails.

Quoted 6 lines

To resolve this issue, use the safe version of QLIST_REMOVE().

Cc Zhenzhong Duan <[email protected]>
Cc Eric Auger <[email protected]>
Fixes 96b7af4388b3 ("vfio/container: Move vfio_container_destroy() to an instance_finalize() handler")

Eric

Quoted 23 lines

---

Changes in v2:

- use the safe version of QLIST_REMOVE() instead of calling vfio_address_space_insert() earlier.

 hw/vfio/container-base.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
hw/vfio/container-base.c +1 -1
index 809b15767425a48f2404b08fc409ee5684af2094..6f86c37d971ec38426dacd471bca837c0d0df806 100644
@@ -103,7 +103,7 @@ static void vfio_container_instance_finalize(Object *obj)
     VFIOContainerBase *bcontainer = VFIO_IOMMU(obj);
     VFIOGuestIOMMU *giommu, *tmp;
 
-    QLIST_REMOVE(bcontainer, next);
+    QLIST_SAFE_REMOVE(bcontainer, next);
 
     QLIST_FOREACH_SAFE(giommu, &bcontainer->giommu_list, giommu_next, tmp) {
         memory_region_unregister_iommu_notifier(