Skip to contentSkip to conversation

Some trivial live update fixes

PATCH Applied Zhenzhong Duan 3 participants 6 messages Jun 27, 2025 – Jun 28, 2025

lore
Share & tools
lore
#0 lore raw

v2 cover Some trivial live update fixes

Hi

These are trivial VFIO live update fixes in corner cases.

1) potential SIGSEGV when unmap-all-vaddr failed
2) potential vfio_container_post_load failure

Thanks
Zhenzhong

Changelog:
v2:
- drop patch1,2 in v1 as they are merged
- squashed "local save" parts of patch3 into patch4 (Steve)
- s/DMA_MAP_FUNC/dma_map_fn (Steve)

Zhenzhong Duan (2):
vfio/container: Fix potential SIGSEGV when recover from
unmap-all-vaddr failure
vfio/container: Fix vfio_container_post_load()

 include/hw/vfio/vfio-cpr.h |  7 ++++---
 hw/vfio/cpr-legacy.c       | 23 +++++++++--------------
 2 files changed, 13 insertions(+), 17 deletions(-)
Signature · 1 line
2.34.1
#1 lore raw

PATCH v2 1/2 vfio/container: Fix potential SIGSEGV when recover from unmap-all-vaddr failure

CPR overrides then restores dma_map in both outgoing and incoming QEMU, for different reasons. But it only sets saved_dma_map in the target.

Fix it by always setting saved_dma_map.

Fixes eba1f657cbb1 ("vfio/container: recover from unmap-all-vaddr failure")
---
 hw/vfio/cpr-legacy.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
hw/vfio/cpr-legacy.c +2 -2
index a84c3247b7..0a5d1bd480 100644
@@ -180,9 +180,9 @@ bool vfio_legacy_cpr_register_container(VFIOContainer *container, Error **errp)
     vmstate_register(NULL, -1, &vfio_container_vmstate, container);
 
     /* During incoming CPR, divert calls to dma_map. */
+    VFIOIOMMUClass *vioc = VFIO_IOMMU_GET_CLASS(bcontainer);
+    container->cpr.saved_dma_map = vioc->dma_map;
     if (cpr_is_incoming()) {
-        VFIOIOMMUClass *vioc = VFIO_IOMMU_GET_CLASS(bcontainer);
-        container->cpr.saved_dma_map = vioc->dma_map;
         vioc->dma_map = vfio_legacy_cpr_dma_map;
     }
 
Signature · 1 line
2.34.1
Reviewed-by on v2 1/2
#2 lore raw

Re: [PATCH v2 1/2] vfio/container: Fix potential SIGSEGV when recover from unmap-all-vaddr failure

Quoted 17 lines · Zhenzhong Duan

CPR overrides then restores dma_map in both outgoing and incoming QEMU, for different reasons. But it only sets saved_dma_map in the target.

Fix it by always setting saved_dma_map.

Fixes eba1f657cbb1 ("vfio/container: recover from unmap-all-vaddr failure")
---
  hw/vfio/cpr-legacy.c | 4 ++--
  1 file changed, 2 insertions(+), 2 deletions(-)
hw/vfio/cpr-legacy.c +0 -0
index a84c3247b7..0a5d1bd480 100644

hw/vfio/cpr-legacy.c bool vfio_legacy_cpr_register_container(VFIOContainer *container, Error **errp) · line 187

Quoted 10 lines
hw/vfio/cpr-legacy.c +2 -2
@@ -180,9 +180,9 @@ bool vfio_legacy_cpr_register_container(VFIOContainer *container, Error **errp)
      vmstate_register(NULL, -1, &vfio_container_vmstate, container);
 
      /* During incoming CPR, divert calls to dma_map. */
+    VFIOIOMMUClass *vioc = VFIO_IOMMU_GET_CLASS(bcontainer);
+    container->cpr.saved_dma_map = vioc->dma_map;
      if (cpr_is_incoming()) {
-        VFIOIOMMUClass *vioc = VFIO_IOMMU_GET_CLASS(bcontainer);
-        container->cpr.saved_dma_map = vioc->dma_map;
          vioc->dma_map = vfio_legacy_cpr_dma_map;
      }  
#3 lore raw

PATCH v2 2/2 vfio/container: Fix vfio_container_post_load()

In reply to Zhenzhong Duan

When there are multiple VFIO containers, vioc->dma_map is restored multiple times, this made only first container work and remaining containers using vioc->dma_map restored by first container.

Fix it by save and restore vioc->dma_map locally. saved_dma_map in VFIOContainerCPR becomes useless and is removed.

Fixes 7e9f21411302 ("vfio/container: restore DMA vaddr")
---
 include/hw/vfio/vfio-cpr.h |  7 ++++---
 hw/vfio/cpr-legacy.c       | 23 +++++++++--------------
 2 files changed, 13 insertions(+), 17 deletions(-)
include/hw/vfio/vfio-cpr.h +4 -3
index 8bf85b9f4e..dbb2a16b7a 100644
@@ -16,14 +16,15 @@ struct VFIOContainer;
 struct VFIOContainerBase;
 struct VFIOGroup;
 
+typedef int (*dma_map_fn)(const struct VFIOContainerBase *bcontainer,
+                          hwaddr iova, ram_addr_t size, void *vaddr,
+                          bool readonly, MemoryRegion *mr);
+
 typedef struct VFIOContainerCPR {
     Error *blocker;
     bool vaddr_unmapped;
     NotifierWithReturn transfer_notifier;
     MemoryListener remap_listener;
-    int (*saved_dma_map)(const struct VFIOContainerBase *bcontainer,
-                         hwaddr iova, ram_addr_t size,
-                         void *vaddr, bool readonly, MemoryRegion *mr);
 } VFIOContainerCPR;
 
 typedef struct VFIODeviceCPR {
hw/vfio/cpr-legacy.c +9 -14
index 0a5d1bd480..1216717546 100644
@@ -99,20 +99,21 @@ static int vfio_container_post_load(void *opaque, int version_id)
 {
     VFIOContainer *container = opaque;
     VFIOContainerBase *bcontainer = &container->bcontainer;
-    VFIOGroup *group;
+    VFIOIOMMUClass *vioc = VFIO_IOMMU_GET_CLASS(bcontainer);
+    dma_map_fn saved_dma_map = vioc->dma_map;
     Error *local_err = NULL;
 
+    /* During incoming CPR, divert calls to dma_map. */
+    vioc->dma_map = vfio_legacy_cpr_dma_map;
+
     if (!vfio_listener_register(bcontainer, &local_err)) {
         error_report_err(local_err);
         return -1;
     }
 
-    QLIST_FOREACH(group, &container->group_list, container_next) {
-        VFIOIOMMUClass *vioc = VFIO_IOMMU_GET_CLASS(bcontainer);
+    /* Restore original dma_map function */
+    vioc->dma_map = saved_dma_map;
 
-        /* Restore original dma_map function */
-        vioc->dma_map = container->cpr.saved_dma_map;
-    }
     return 0;
 }
 
@@ -148,6 +149,7 @@ static int vfio_cpr_fail_notifier(NotifierWithReturn *notifier,
          */
 
         VFIOIOMMUClass *vioc = VFIO_IOMMU_GET_CLASS(bcontainer);
+        dma_map_fn saved_dma_map = vioc->dma_map;
         vioc->dma_map = vfio_legacy_cpr_dma_map;
 
         container->cpr.remap_listener = (MemoryListener) {
@@ -158,7 +160,7 @@ static int vfio_cpr_fail_notifier(NotifierWithReturn *notifier,
                                  bcontainer->space->as);
         memory_listener_unregister(&container->cpr.remap_listener);
         container->cpr.vaddr_unmapped = false;
-        vioc->dma_map = container->cpr.saved_dma_map;
+        vioc->dma_map = saved_dma_map;
     }
     return 0;
 }
@@ -179,13 +181,6 @@ bool vfio_legacy_cpr_register_container(VFIOContainer *container, Error **errp)
 
     vmstate_register(NULL, -1, &vfio_container_vmstate, container);
 
-    /* During incoming CPR, divert calls to dma_map. */
-    VFIOIOMMUClass *vioc = VFIO_IOMMU_GET_CLASS(bcontainer);
-    container->cpr.saved_dma_map = vioc->dma_map;
-    if (cpr_is_incoming()) {
-        vioc->dma_map = vfio_legacy_cpr_dma_map;
-    }
-
     migration_add_notifier_mode(&container->cpr.transfer_notifier,
                                 vfio_cpr_fail_notifier,
                                 MIG_MODE_CPR_TRANSFER);
Signature · 1 line
2.34.1
Reviewed-by on v2 2/2
#4 lore raw

Re: [PATCH v2 2/2] vfio/container: Fix vfio_container_post_load()

Quoted 90 lines · Zhenzhong Duan

When there are multiple VFIO containers, vioc->dma_map is restored multiple times, this made only first container work and remaining containers using vioc->dma_map restored by first container.

Fix it by save and restore vioc->dma_map locally. saved_dma_map in VFIOContainerCPR becomes useless and is removed.

Fixes 7e9f21411302 ("vfio/container: restore DMA vaddr")
---
  include/hw/vfio/vfio-cpr.h |  7 ++++---
  hw/vfio/cpr-legacy.c       | 23 +++++++++--------------
  2 files changed, 13 insertions(+), 17 deletions(-)
include/hw/vfio/vfio-cpr.h +4 -3
index 8bf85b9f4e..dbb2a16b7a 100644
@@ -16,14 +16,15 @@ struct VFIOContainer;
  struct VFIOContainerBase;
  struct VFIOGroup;
 
+typedef int (*dma_map_fn)(const struct VFIOContainerBase *bcontainer,
+                          hwaddr iova, ram_addr_t size, void *vaddr,
+                          bool readonly, MemoryRegion *mr);
+
  typedef struct VFIOContainerCPR {
      Error *blocker;
      bool vaddr_unmapped;
      NotifierWithReturn transfer_notifier;
      MemoryListener remap_listener;
-    int (*saved_dma_map)(const struct VFIOContainerBase *bcontainer,
-                         hwaddr iova, ram_addr_t size,
-                         void *vaddr, bool readonly, MemoryRegion *mr);
  } VFIOContainerCPR;
 
  typedef struct VFIODeviceCPR {
hw/vfio/cpr-legacy.c +9 -7
index 0a5d1bd480..1216717546 100644
@@ -99,20 +99,21 @@ static int vfio_container_post_load(void *opaque, int version_id)
  {
      VFIOContainer *container = opaque;
      VFIOContainerBase *bcontainer = &container->bcontainer;
-    VFIOGroup *group;
+    VFIOIOMMUClass *vioc = VFIO_IOMMU_GET_CLASS(bcontainer);
+    dma_map_fn saved_dma_map = vioc->dma_map;
      Error *local_err = NULL;
 
+    /* During incoming CPR, divert calls to dma_map. */
+    vioc->dma_map = vfio_legacy_cpr_dma_map;
+
      if (!vfio_listener_register(bcontainer, &local_err)) {
          error_report_err(local_err);
          return -1;
      }
 
-    QLIST_FOREACH(group, &container->group_list, container_next) {
-        VFIOIOMMUClass *vioc = VFIO_IOMMU_GET_CLASS(bcontainer);
+    /* Restore original dma_map function */
+    vioc->dma_map = saved_dma_map;
 
-        /* Restore original dma_map function */
-        vioc->dma_map = container->cpr.saved_dma_map;
-    }
      return 0;
  }
 
@@ -148,6 +149,7 @@ static int vfio_cpr_fail_notifier(NotifierWithReturn *notifier,
           */
 
          VFIOIOMMUClass *vioc = VFIO_IOMMU_GET_CLASS(bcontainer);
+        dma_map_fn saved_dma_map = vioc->dma_map;
          vioc->dma_map = vfio_legacy_cpr_dma_map;
 
          container->cpr.remap_listener = (MemoryListener) {
@@ -158,7 +160,7 @@ static int vfio_cpr_fail_notifier(NotifierWithReturn *notifier,
                                   bcontainer->space->as);
          memory_listener_unregister(&container->cpr.remap_listener);
          container->cpr.vaddr_unmapped = false;
-        vioc->dma_map = container->cpr.saved_dma_map;
+        vioc->dma_map = saved_dma_map;
      }
      return 0;
  }
@@ -179,13 +181,6 @@ bool vfio_legacy_cpr_register_container(VFIOContainer *container, Error **errp)
 
      vmstate_register(NULL, -1, &vfio_container_vmstate, container);
 

hw/vfio/cpr-legacy.c bool vfio_legacy_cpr_register_container(VFIOContainer *container, Error **errp) · line 186

Quoted 10 lines
hw/vfio/cpr-legacy.c +0 -7
-    /* During incoming CPR, divert calls to dma_map. */
-    VFIOIOMMUClass *vioc = VFIO_IOMMU_GET_CLASS(bcontainer);
-    container->cpr.saved_dma_map = vioc->dma_map;
-    if (cpr_is_incoming()) {
-        vioc->dma_map = vfio_legacy_cpr_dma_map;
-    }
-
      migration_add_notifier_mode(&container->cpr.transfer_notifier,
                                  vfio_cpr_fail_notifier,
                                  MIG_MODE_CPR_TRANSFER);
#5 lore raw
In reply to Zhenzhong Duan
Quoted 16 lines · Zhenzhong Duan

Hi

These are trivial VFIO live update fixes in corner cases.

1) potential SIGSEGV when unmap-all-vaddr failed
2) potential vfio_container_post_load failure

Thanks
Zhenzhong

Changelog:
v2:
- drop patch1,2 in v1 as they are merged
- squashed "local save" parts of patch3 into patch4 (Steve)
- s/DMA_MAP_FUNC/dma_map_fn (Steve)

Quoted 9 lines

Zhenzhong Duan (2):
vfio/container: Fix potential SIGSEGV when recover from
unmap-all-vaddr failure
vfio/container: Fix vfio_container_post_load()

  include/hw/vfio/vfio-cpr.h |  7 ++++---
  hw/vfio/cpr-legacy.c       | 23 +++++++++--------------
  2 files changed, 13 insertions(+), 17 deletions(-)

Applied to vfio-next.

Thanks,

C.