From: Zhenzhong Duan <zhenzhong.duan@intel.com>
If there are multiple containers and unmap-all fails for some of them, we
need to remap vaddr for the other containers for which unmap-all succeeded.
When ram discard is enabled, we should only remap populated parts in a
section instead of the whole section.
Fixes: eba1f657cbb1 ("vfio/container: recover from unmap-all-vaddr failure")
Signed-off-by: Zhenzhong Duan <zhenzhong.duan@intel.com>
Reviewed-by: Steven Sistare <steven.sistare@oracle.com>
Reviewed-by: David Hildenbrand <david@redhat.com>
Link: https://lore.kernel.org/qemu-devel/20250928085432.40107-2-zhenzhong.duan@intel.com
Signed-off-by: Cédric Le Goater <clg@redhat.com>
---
include/hw/vfio/vfio-cpr.h | 2 +-
hw/vfio/cpr-legacy.c | 20 +++++++++++++++-----
hw/vfio/listener.c | 4 ++--
3 files changed, 18 insertions(+), 8 deletions(-)
diff --git a/include/hw/vfio/vfio-cpr.h b/include/hw/vfio/vfio-cpr.h
index 81f4e24e229ef35f5b14582ce6e58415e0ebf3df..4606da500a7969b6519b054280464608ae624bb9 100644
--- a/include/hw/vfio/vfio-cpr.h
+++ b/include/hw/vfio/vfio-cpr.h
@@ -68,7 +68,7 @@ bool vfio_cpr_container_match(struct VFIOLegacyContainer *container,
void vfio_cpr_giommu_remap(struct VFIOContainer *bcontainer,
MemoryRegionSection *section);
-bool vfio_cpr_ram_discard_register_listener(
+bool vfio_cpr_ram_discard_replay_populated(
struct VFIOContainer *bcontainer, MemoryRegionSection *section);
void vfio_cpr_save_vector_fd(struct VFIOPCIDevice *vdev, const char *name,
diff --git a/hw/vfio/cpr-legacy.c b/hw/vfio/cpr-legacy.c
index 80af7469d06b2f15a705a97c4b5ddd25a8044115..b4581e8f859323c1c45560f7d345b34f1ed9bd85 100644
--- a/hw/vfio/cpr-legacy.c
+++ b/hw/vfio/cpr-legacy.c
@@ -228,22 +228,32 @@ void vfio_cpr_giommu_remap(VFIOContainer *bcontainer,
memory_region_iommu_replay(giommu->iommu_mr, &giommu->n);
}
+static int vfio_cpr_rdm_remap(MemoryRegionSection *section, void *opaque)
+{
+ RamDiscardListener *rdl = opaque;
+
+ return rdl->notify_populate(rdl, section);
+}
+
/*
* In old QEMU, VFIO_DMA_UNMAP_FLAG_VADDR may fail on some mapping after
* succeeding for others, so the latter have lost their vaddr. Call this
- * to restore vaddr for a section with a RamDiscardManager.
+ * to restore vaddr for populated parts in a section with a RamDiscardManager.
*
- * The ram discard listener already exists. Call its populate function
+ * The ram discard listener already exists. Call its replay_populated function
* directly, which calls vfio_legacy_cpr_dma_map.
*/
-bool vfio_cpr_ram_discard_register_listener(VFIOContainer *bcontainer,
- MemoryRegionSection *section)
+bool vfio_cpr_ram_discard_replay_populated(VFIOContainer *bcontainer,
+ MemoryRegionSection *section)
{
+ RamDiscardManager *rdm = memory_region_get_ram_discard_manager(section->mr);
VFIORamDiscardListener *vrdl =
vfio_find_ram_discard_listener(bcontainer, section);
g_assert(vrdl);
- return vrdl->listener.notify_populate(&vrdl->listener, section) == 0;
+ return ram_discard_manager_replay_populated(rdm, section,
+ vfio_cpr_rdm_remap,
+ &vrdl->listener) == 0;
}
int vfio_cpr_group_get_device_fd(int d, const char *name)
diff --git a/hw/vfio/listener.c b/hw/vfio/listener.c
index c6bb58f5209a4c8eb67deac41a89c317fa391447..1b6e5065a3267ab08d2d3fea3c5b4965a80947e8 100644
--- a/hw/vfio/listener.c
+++ b/hw/vfio/listener.c
@@ -577,8 +577,8 @@ void vfio_container_region_add(VFIOContainer *bcontainer,
if (!vfio_ram_discard_register_listener(bcontainer, section, &err)) {
goto fail;
}
- } else if (!vfio_cpr_ram_discard_register_listener(bcontainer,
- section)) {
+ } else if (!vfio_cpr_ram_discard_replay_populated(bcontainer,
+ section)) {
error_setg(&err,
"vfio_cpr_ram_discard_register_listener for %s failed",
memory_region_name(section->mr));
--
2.51.0