Some trivial live update fixes
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
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.
--- hw/vfio/cpr-legacy.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
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; }
Signature · 1 line
2.34.1
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_mapin both outgoing and incoming QEMU, for different reasons. But it only setssaved_dma_mapin the target.Fix it by always setting
saved_dma_map.Fixes eba1f657cbb1 ("vfio/container: recover from unmap-all-vaddr failure")Suggested-by Steven Sistare <[email protected]>Signed-off-by Zhenzhong Duan <[email protected]>--- hw/vfio/cpr-legacy.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)hw/vfio/cpr-legacy.c +0
-0
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; }
PATCH v2 2/2 vfio/container: Fix vfio_container_post_load()
In reply to Zhenzhong DuanWhen 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.
--- 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
@@ -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
@@ -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
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_mapis restored multiple times, this made only first container work and remaining containers usingvioc->dma_maprestored by first container.Fix it by save and restore
vioc->dma_maplocally.saved_dma_mapin VFIOContainerCPR becomes useless and is removed.Fixes 7e9f21411302 ("vfio/container: restore DMA vaddr")Signed-off-by Zhenzhong Duan <[email protected]>--- 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@@ -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@@ -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 );
Cédric Le Goater
AppliedQuoted 16 lines · Zhenzhong Duan
Hi
These are trivial VFIO live update fixes in corner cases.
1) potential SIGSEGV when unmap-all-vaddr failed
2) potentialvfio_container_post_loadfailureThanks
ZhenzhongChangelog:
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: Fixvfio_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.