vfio: relax the vIOMMU check
v5 cover vfio: relax the vIOMMU check
Hi,
This series relax the vIOMMU check and allows live migration with vIOMMU without VFs using device dirty tracking. It's rewritten based on first 4 patches of [1] from Joao.
Currently what block us is the lack of dirty bitmap query with iommufd before unmap. By adding that query and handle some corner case we can relax the check.
Based on vfio-next branch:
patch1-2: add dirty bitmap query with iommufd patch3: a ranaming cleanup patch4-5: unmap_bitmap optimization patch6-7: fixes to avoid losing dirty pages patch8: add a blocker if VM memory is really quite large for unmap_bitmap patch9: relax vIOMMU check
We used [2] to test, it contains dom_switch series + this series + nesting series. I included nesting series just because I'd like to confirm the two patches optimizing out dirty tracking for readonly pages work.
We tested VM live migration (running QAT workload in VM) with QAT device passthrough, below matrix configs with guest config 'iommu=pt' and 'iommu=nopt':
1.Scalable mode vIOMMU + IOMMUFD cdev mode
2.Scalable mode vIOMMU + legacy VFIO mode
3.legacy mode vIOMMU + IOMMUFD cdev mode
4.legacy mode vIOMMU + legacy VFIO mode
The QAT workload is a user level app that utilizes VFIO to control QAT device.
Thanks
Zhenzhong
[1] https://github.com/jpemartins/qemu/commits/vfio-migration-viommu/
[2] https://github.com/yiliu1765/qemu/tree/liuyi/zhenzhong/iommufd_nesting.v8.DS_LM.wip
Changelog:
v5:
- drop the patch checking iommu_dirty_tracking (Avihai, Joao)
- pass iotlb info to unmap_bitmap when switch out of system AS
v4:
- bypass memory size check for device dirty tracking as it's unrelated (Avihai)
- split vfio_device_dirty_pages_disabled() helper out as a separate patch
- add a patch to fix minor error on checking vbasedev->iommu_dirty_tracking
v3:
- return bitmap query failure to fail migration (Avihai)
- refine patch7, set IOMMUFD backend 'dirty_pgsizes' and 'max_dirty_bitmap_size' (Cedric)
- refine patch7, calculate memory limit instead of hardcode 8TB (Liuyi)
- refine commit log (Cedric, Liuyi)
v2:
- add backend_flag parameter to pass DIRTY_BITMAP_NO_CLEAR (Joao, Cedric)
- add a cleanup patch to rename vfio_dma_unmap_bitmap (Cedric)
- add blocker if unmap_bitmap limit check fail (Liuyi)
Joao Martins (1):
vfio: Add a backend_flag parameter to
vfio_contianer_query_dirty_bitmap()
Zhenzhong Duan (8):
vfio/iommufd: Add framework code to support getting dirty bitmap
before unmap
vfio/iommufd: Query dirty bitmap before DMA unmap vfio/container-legacy: rename vfio_dma_unmap_bitmap() to
vfio_legacy_dma_unmap_get_dirty_bitmap()
vfio/iommufd: Add IOMMU_HWPT_GET_DIRTY_BITMAP_NO_CLEAR flag support
intel_iommu: Fix unmap_bitmap failure with legacy VFIO backend vfio/listener: Construct iotlb entry when unmap memory address space vfio/migration: Add migration blocker if VM memory is too large to
cause unmap_bitmap failure
vfio/migration: Allow live migration with vIOMMU without VFs using
device dirty tracking
include/hw/vfio/vfio-container.h | 8 +++-- include/hw/vfio/vfio-device.h | 10 ++++++ include/system/iommufd.h | 2 +- backends/iommufd.c | 5 +-- hw/i386/intel_iommu.c | 42 +++++++++++++++++++++++++ hw/vfio-user/container.c | 5 +-- hw/vfio/container-legacy.c | 15 +++++---- hw/vfio/container.c | 20 ++++++------ hw/vfio/device.c | 6 ++++ hw/vfio/iommufd.c | 53 +++++++++++++++++++++++++++++--- hw/vfio/listener.c | 21 ++++++++++--- hw/vfio/migration.c | 40 ++++++++++++++++++++++-- backends/trace-events | 2 +- hw/vfio/trace-events | 2 +- 14 files changed, 194 insertions(+), 37 deletions(-)
Signature · 1 line
2.47.1
PATCH v5 1/9 vfio/iommufd: Add framework code to support getting dirty bitmap before unmap
Currently we support device and iommu dirty tracking, device dirty tracking is preferred.
Add the framework code in iommufd_cdev_unmap() to choose either device or iommu dirty tracking, just like vfio_legacy_dma_unmap_one().
--- hw/vfio/iommufd.c | 34 +++++++++++++++++++++++++++++++--- 1 file changed, 31 insertions(+), 3 deletions(-)
hw/vfio/iommufd.c +31 -3
@@ -61,14 +61,42 @@ static int iommufd_cdev_unmap(const VFIOContainer *bcontainer, IOMMUTLBEntry *iotlb, bool unmap_all) {const VFIOIOMMUFDContainer *container =VFIO_IOMMU_IOMMUFD (bcontainer); +IOMMUFDBackend *be = container->be; +uint32_t ioas_id = container->ioas_id; +bool need_dirty_sync = false; +Error *local_err =NULL ; +int ret;if (unmap_all) { size =UINT64_MAX ; } -/* TODO: Handle dma_unmap_bitmap with iotlb args (migration) */ -return iommufd_backend_unmap_dma (container->be, - container->ioas_id, iova, size); +if (iotlb &&vfio_container_dirty_tracking_is_started (bcontainer)) { +if (!vfio_container_devices_dirty_tracking_is_supported (bcontainer) && + bcontainer->dirty_pages_supported) { +/* TODO: query dirty bitmap before DMA unmap */ +return iommufd_backend_unmap_dma (be, ioas_id, iova, size); + } + + need_dirty_sync = true; + } + + ret =iommufd_backend_unmap_dma (be, ioas_id, iova, size); +if (ret) { +return ret; + } + +if (need_dirty_sync) { + ret =vfio_container_query_dirty_bitmap (bcontainer, iova, size, + iotlb->translated_addr, + &local_err); +if (ret) { +error_report_err (local_err); +return ret; + } + } + +return 0 ; }static bool iommufd_cdev_kvm_device_add (VFIODevice *vbasedev,Error **errp)
Signature · 1 line
2.47.1
PATCH v5 2/9 vfio/iommufd: Query dirty bitmap before DMA unmap
In reply to Zhenzhong DuanWhen an existing mapping is unmapped, there could already be dirty bits which need to be recorded before unmap.
If query dirty bitmap fails, we still need to do unmapping or else there is stale mapping and it's risky to guest.
--- hw/vfio/iommufd.c | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-)
hw/vfio/iommufd.c +16 -3
@@ -65,7 +65,7 @@ static int iommufd_cdev_unmap(const VFIOContainer *bcontainer,uint32_t ioas_id = container->ioas_id;bool need_dirty_sync = false;Error *local_err =NULL ; -int ret; +int ret, unmap_ret;if (unmap_all) { size =UINT64_MAX ;
@@ -74,8 +74,21 @@ static int iommufd_cdev_unmap(const VFIOContainer *bcontainer,if (iotlb &&vfio_container_dirty_tracking_is_started (bcontainer)) {if (!vfio_container_devices_dirty_tracking_is_supported (bcontainer) && bcontainer->dirty_pages_supported) { -/* TODO: query dirty bitmap before DMA unmap */ -return iommufd_backend_unmap_dma (be, ioas_id, iova, size); + ret =vfio_container_query_dirty_bitmap (bcontainer, iova, size, + iotlb->translated_addr, + &local_err); +if (ret) { +error_report_err (local_err); + } +/* Unmap stale mapping even if query dirty bitmap fails */ + unmap_ret =iommufd_backend_unmap_dma (be, ioas_id, iova, size); + +/* +* If dirty tracking fails, return the failure to VFIO core to +* fail the migration, or else there will be dirty pages missed +* to be migrated. +*/ +return unmap_ret ? : ret; } need_dirty_sync = true;
Signature · 1 line
2.47.1
PATCH v5 3/9 vfio/container-legacy: rename vfio_dma_unmap_bitmap() to vfio_legacy_dma_unmap_get_dirty_bitmap()
In reply to Zhenzhong DuanThis is to follow naming style in container-legacy.c to have low level functions with vfio_legacy_ prefix.
No functional changes.
--- hw/vfio/container-legacy.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-)
hw/vfio/container-legacy.c +6 -4
@@ -68,9 +68,10 @@ static int vfio_ram_block_discard_disable(VFIOLegacyContainer *container, } } -static int vfio_dma_unmap_bitmap (const VFIOLegacyContainer *container, -hwaddr iova,uint64_t size, -IOMMUTLBEntry *iotlb) +static int +vfio_legacy_dma_unmap_get_dirty_bitmap (const VFIOLegacyContainer *container, +hwaddr iova,uint64_t size, +IOMMUTLBEntry *iotlb) {const VFIOContainer *bcontainer =VFIO_IOMMU (container);struct vfio_iommu_type1_dma_unmap *unmap;
@@ -141,7 +142,8 @@ static int vfio_legacy_dma_unmap_one(const VFIOLegacyContainer *container,if (iotlb &&vfio_container_dirty_tracking_is_started (bcontainer)) {if (!vfio_container_devices_dirty_tracking_is_supported (bcontainer) && bcontainer->dirty_pages_supported) { -return vfio_dma_unmap_bitmap (container, iova, size, iotlb); +return vfio_legacy_dma_unmap_get_dirty_bitmap (container, iova, size, + iotlb); } need_dirty_sync = true;
Signature · 1 line
2.47.1
PATCH v5 4/9 vfio: Add a backend_flag parameter to vfio_contianer_query_dirty_bitmap()
In reply to Zhenzhong DuanFrom: Joao Martins <[email protected]>
This new parameter will be used in following patch, currently 0 is passed.
--- include/hw/vfio/vfio-container.h | 8 ++++++-- hw/vfio-user/container.c | 5 +++-- hw/vfio/container-legacy.c | 5 +++-- hw/vfio/container.c | 15 +++++++++------ hw/vfio/iommufd.c | 7 ++++--- hw/vfio/listener.c | 6 +++--- hw/vfio/trace-events | 2 +- 7 files changed, 29 insertions(+), 19 deletions(-)
include/hw/vfio/vfio-container.h +6 -2
@@ -99,7 +99,9 @@ bool vfio_container_devices_dirty_tracking_is_supported( const VFIOContainer *bcontainer);int vfio_container_query_dirty_bitmap (const VFIOContainer *bcontainer,uint64_t iova,uint64_t size, -hwaddr translated_addr,Error **errp); +uint64_t backend_flag, +hwaddr translated_addr, +Error **errp);GList *vfio_container_get_iova_ranges (const VFIOContainer *bcontainer);
@@ -253,12 +255,14 @@ struct VFIOIOMMUClass {* @vbmap: #VFIOBitmap internal bitmap structure * @iova: iova base address * @size: size of iova range +* @backend_flag: flags for backend, opaque to upper layer container * @errp: pointer to Error*, to store an error if it happens. * * Returns zero to indicate success and negative for error. */ int (*query_dirty_bitmap)(const VFIOContainer *bcontainer, -VFIOBitmap *vbmap,hwaddr iova,hwaddr size,Error **errp); +VFIOBitmap *vbmap,hwaddr iova,hwaddr size, +uint64_t backend_flag,Error **errp);/* PCI specific */ int (*pci_hot_reset)(VFIODevice *vbasedev,bool single);
hw/vfio-user/container.c +3 -2
@@ -162,8 +162,9 @@ vfio_user_set_dirty_page_tracking(const VFIOContainer *bcontainer, }static int vfio_user_query_dirty_bitmap (const VFIOContainer *bcontainer, -VFIOBitmap *vbmap,hwaddr iova, -hwaddr size,Error **errp) +VFIOBitmap *vbmap,hwaddr iova, +hwaddr size,uint64_t backend_flag, +Error **errp) {error_setg_errno (errp,ENOTSUP ,"Not supported" );return -ENOTSUP ;
hw/vfio/container-legacy.c +3 -2
@@ -154,7 +154,7 @@ static int vfio_legacy_dma_unmap_one(const VFIOLegacyContainer *container, }if (need_dirty_sync) { - ret =vfio_container_query_dirty_bitmap (bcontainer, iova, size, + ret =vfio_container_query_dirty_bitmap (bcontainer, iova, size,0 , iotlb->translated_addr, &local_err);if (ret) {error_report_err (local_err);
@@ -255,7 +255,8 @@ vfio_legacy_set_dirty_page_tracking(const VFIOContainer *bcontainer, }static int vfio_legacy_query_dirty_bitmap (const VFIOContainer *bcontainer, -VFIOBitmap *vbmap,hwaddr iova,hwaddr size,Error **errp) +VFIOBitmap *vbmap,hwaddr iova,hwaddr size, +uint64_t backend_flag,Error **errp) {const VFIOLegacyContainer *container =VFIO_IOMMU_LEGACY (bcontainer);struct vfio_iommu_type1_dirty_bitmap *dbitmap;
hw/vfio/container.c +9 -6
@@ -213,13 +213,13 @@ static int vfio_device_dma_logging_report(VFIODevice *vbasedev, hwaddr iova,static int vfio_container_iommu_query_dirty_bitmap (const VFIOContainer *bcontainer,VFIOBitmap *vbmap,hwaddr iova, -hwaddr size,Error **errp) +hwaddr size,uint64_t backend_flag,Error **errp) {VFIOIOMMUClass *vioc =VFIO_IOMMU_GET_CLASS (bcontainer);g_assert (vioc->query_dirty_bitmap);return vioc->query_dirty_bitmap (bcontainer, vbmap, iova, size, - errp); + backend_flag, errp); } static int vfio_container_devices_query_dirty_bitmap(
@@ -247,7 +247,9 @@ static int vfio_container_devices_query_dirty_bitmap(int vfio_container_query_dirty_bitmap (const VFIOContainer *bcontainer,uint64_t iova,uint64_t size, -hwaddr translated_addr,Error **errp) +uint64_t backend_flag, +hwaddr translated_addr, +Error **errp) {bool all_device_dirty_tracking =vfio_container_devices_dirty_tracking_is_supported (bcontainer);
@@ -274,7 +276,7 @@ int vfio_container_query_dirty_bitmap(const VFIOContainer *bcontainer, errp); }else { ret =vfio_container_iommu_query_dirty_bitmap (bcontainer, &vbmap, iova, size, - errp); + backend_flag, errp); }if (ret) {
@@ -285,8 +287,9 @@ int vfio_container_query_dirty_bitmap(const VFIOContainer *bcontainer, translated_addr, vbmap.pages); -trace_vfio_container_query_dirty_bitmap (iova, size, vbmap.size, - translated_addr, dirty_pages); +trace_vfio_container_query_dirty_bitmap (iova, size, backend_flag, + vbmap.size, translated_addr, + dirty_pages); out:g_free (vbmap.bitmap);
hw/vfio/iommufd.c +4 -3
@@ -74,7 +74,7 @@ static int iommufd_cdev_unmap(const VFIOContainer *bcontainer,if (iotlb &&vfio_container_dirty_tracking_is_started (bcontainer)) {if (!vfio_container_devices_dirty_tracking_is_supported (bcontainer) && bcontainer->dirty_pages_supported) { - ret =vfio_container_query_dirty_bitmap (bcontainer, iova, size, + ret =vfio_container_query_dirty_bitmap (bcontainer, iova, size,0 , iotlb->translated_addr, &local_err);if (ret) {
@@ -100,7 +100,7 @@ static int iommufd_cdev_unmap(const VFIOContainer *bcontainer, }if (need_dirty_sync) { - ret =vfio_container_query_dirty_bitmap (bcontainer, iova, size, + ret =vfio_container_query_dirty_bitmap (bcontainer, iova, size,0 , iotlb->translated_addr, &local_err);if (ret) {
@@ -216,7 +216,8 @@ err:static int iommufd_query_dirty_bitmap (const VFIOContainer *bcontainer,VFIOBitmap *vbmap,hwaddr iova, -hwaddr size,Error **errp) +hwaddr size,uint64_t backend_flag, +Error **errp) {VFIOIOMMUFDContainer *container =VFIO_IOMMU_IOMMUFD (bcontainer);unsigned long page_size =qemu_real_host_page_size ();
hw/vfio/listener.c +3 -3
@@ -1083,7 +1083,7 @@ static void vfio_iommu_map_dirty_notify(IOMMUNotifier *n, IOMMUTLBEntry *iotlb) translated_addr =memory_region_get_ram_addr (mr) + xlat; ret =vfio_container_query_dirty_bitmap (bcontainer, iova, iotlb->addr_mask +1 , - translated_addr, &local_err); +0 , translated_addr, &local_err);if (ret) {error_prepend (&local_err, "vfio_iommu_map_dirty_notify(%p, 0x%"HWADDR_PRIx", "
@@ -1119,7 +1119,7 @@ static int vfio_ram_discard_query_dirty_bitmap(MemoryRegionSection *section,* Sync the whole mapped region (spanning multiple individual mappings) * in one go. */ - ret =vfio_container_query_dirty_bitmap (vrdl->bcontainer, iova, size, + ret =vfio_container_query_dirty_bitmap (vrdl->bcontainer, iova, size,0 , translated_addr, &local_err);if (ret) {error_report_err (local_err);
@@ -1204,7 +1204,7 @@ static int vfio_sync_dirty_bitmap(VFIOContainer *bcontainer,return vfio_container_query_dirty_bitmap (bcontainer,REAL_HOST_PAGE_ALIGN (section->offset_within_address_space), -int128_get64 (section->size), translated_addr, errp); +int128_get64 (section->size),0 , translated_addr, errp); } static void vfio_listener_log_sync(MemoryListener *listener,
hw/vfio/trace-events +1 -1
@@ -105,7 +105,7 @@ vfio_device_dirty_tracking_start(int nr_ranges, uint64_t min32, uint64_t max32, vfio_iommu_map_dirty_notify(uint64_t iova_start, uint64_t iova_end) "iommu dirty @ 0x%"PRIx64" - 0x%"PRIx64 # container.c -vfio_container_query_dirty_bitmap(uint64_t iova, uint64_t size, uint64_t bitmap_size, uint64_t translated_addr, uint64_t dirty_pages) "iova=0x%"PRIx64" size= 0x%"PRIx64" bitmap_size=0x%"PRIx64" gpa=0x%"PRIx64" dirty_pages=%"PRIu64 +vfio_container_query_dirty_bitmap(uint64_t iova, uint64_t size, uint64_t backend_flag, uint64_t bitmap_size, uint64_t translated_addr, uint64_t dirty_pages) "iova=0x%"PRIx64" size=0x%"PRIx64" backend_flag=0x%"PRIx64" bitmap_size=0x%"PRIx64" gpa=0x%"PRIx64" dirty_pages=%"PRIu64 # container-legacy.c vfio_container_disconnect(int fd) "close container->fd=%d"
Signature · 1 line
2.47.1
PATCH v5 5/9 vfio/iommufd: Add IOMMU_HWPT_GET_DIRTY_BITMAP_NO_CLEAR flag support
In reply to Zhenzhong DuanPass IOMMU_HWPT_GET_DIRTY_BITMAP_NO_CLEAR when doing the last dirty bitmap query right before unmap, no PTEs flushes. This accelerates the query without issue because unmap will tear down the mapping anyway.
--- include/system/iommufd.h | 2 +- backends/iommufd.c | 5 +++-- hw/vfio/iommufd.c | 5 +++-- backends/trace-events | 2 +- 4 files changed, 8 insertions(+), 6 deletions(-)
include/system/iommufd.h +1 -1
@@ -64,7 +64,7 @@ bool iommufd_backend_set_dirty_tracking(IOMMUFDBackend *be, uint32_t hwpt_id,bool iommufd_backend_get_dirty_bitmap (IOMMUFDBackend *be,uint32_t hwpt_id,uint64_t iova,ram_addr_t size,uint64_t page_size,uint64_t *data, -Error **errp); +uint64_t flags,Error **errp); bool iommufd_backend_invalidate_cache(IOMMUFDBackend *be, uint32_t id, uint32_t data_type, uint32_t entry_len, uint32_t *entry_num, void *data,
backends/iommufd.c +3 -2
@@ -361,7 +361,7 @@ bool iommufd_backend_get_dirty_bitmap(IOMMUFDBackend *be,uint32_t hwpt_id, uint64_t iova, ram_addr_t size, uint64_t page_size, uint64_t *data, - Error **errp) + uint64_t flags, Error **errp) { int ret; struct iommu_hwpt_get_dirty_bitmap get_dirty_bitmap = {
@@ -371,11 +371,12 @@ bool iommufd_backend_get_dirty_bitmap(IOMMUFDBackend *be, .length = size, .page_size = page_size, .data = (uintptr_t)data, + .flags = flags, }; ret =ioctl (be->fd,IOMMU_HWPT_GET_DIRTY_BITMAP , &get_dirty_bitmap);trace_iommufd_backend_get_dirty_bitmap (be->fd, hwpt_id, iova, size, - page_size, ret ? errno :0 ); + flags, page_size, ret ? errno :0 );if (ret) {error_setg_errno (errp, errno, "IOMMU_HWPT_GET_DIRTY_BITMAP (iova: 0x%"HWADDR_PRIx
hw/vfio/iommufd.c +3 -2
@@ -74,7 +74,8 @@ static int iommufd_cdev_unmap(const VFIOContainer *bcontainer,if (iotlb &&vfio_container_dirty_tracking_is_started (bcontainer)) {if (!vfio_container_devices_dirty_tracking_is_supported (bcontainer) && bcontainer->dirty_pages_supported) { - ret =vfio_container_query_dirty_bitmap (bcontainer, iova, size,0 , + ret =vfio_container_query_dirty_bitmap (bcontainer, iova, size, +IOMMU_HWPT_GET_DIRTY_BITMAP_NO_CLEAR , iotlb->translated_addr, &local_err);if (ret) {
@@ -231,7 +232,7 @@ static int iommufd_query_dirty_bitmap(const VFIOContainer *bcontainer,if (!iommufd_backend_get_dirty_bitmap (container->be, hwpt->hwpt_id, iova, size, page_size, (uint64_t *)vbmap->bitmap, - errp)) { + backend_flag, errp)) {return -EINVAL ; } }
backends/trace-events +1 -1
@@ -19,5 +19,5 @@ iommufd_backend_alloc_ioas(int iommufd, uint32_t ioas) " iommufd=%d ioas=%d" iommufd_backend_alloc_hwpt(int iommufd, uint32_t dev_id, uint32_t pt_id, uint32_t flags, uint32_t hwpt_type, uint32_t len, uint64_t data_ptr, uint32_t out_hwpt_id, int ret) " iommufd=%d dev_id=%u pt_id=%u flags=0x%x hwpt_type=%u len=%u data_ptr=0x%"PRIx64" out_hwpt=%u (%d)" iommufd_backend_free_id(int iommufd, uint32_t id, int ret) " iommufd=%d id=%d (%d)" iommufd_backend_set_dirty(int iommufd, uint32_t hwpt_id, bool start, int ret) " iommufd=%d hwpt=%u enable=%d (%d)" -iommufd_backend_get_dirty_bitmap(int iommufd, uint32_t hwpt_id, uint64_t iova, uint64_t size, uint64_t page_size, int ret) " iommufd=%d hwpt=%u iova=0x%"PRIx64" size=0x%"PRIx64" page_size=0x%"PRIx64" (%d)" +iommufd_backend_get_dirty_bitmap(int iommufd, uint32_t hwpt_id, uint64_t iova, uint64_t size, uint64_t flags, uint64_t page_size, int ret) " iommufd=%d hwpt=%u iova=0x%"PRIx64" size=0x%"PRIx64" flags=0x%"PRIx64" page_size=0x%"PRIx64" (%d)" iommufd_backend_invalidate_cache(int iommufd, uint32_t id, uint32_t data_type, uint32_t entry_len, uint32_t entry_num, uint32_t done_num, uint64_t data_ptr, int ret) " iommufd=%d id=%u data_type=%u entry_len=%u entry_num=%u done_num=%u data_ptr=0x%"PRIx64" (%d)"
Signature · 1 line
2.47.1
PATCH v5 6/9 intel_iommu: Fix unmap_bitmap failure with legacy VFIO backend
In reply to Zhenzhong DuanIf a VFIO device in guest switches from IOMMU domain to block domain, vtd_address_space_unmap() is called to unmap whole address space.
If that happens during migration, migration fails with legacy VFIO backend as below:
Status: failed (vfio_container_dma_unmap(0x561bbbd92d90, 0x100000000000, 0x100000000000) = -7 (Argument list too long))
Because legacy VFIO limits maximum bitmap size to 256MB which maps to 8TB on 4K page system, when 16TB sized UNMAP notification is sent, unmap_bitmap ioctl fails. Normally such large UNMAP notification come from IOVA range rather than system memory.
Apart from that, vtd_address_space_unmap() sends UNMAP notification with translated_addr = 0, because there is no valid translated_addr for unmapping a whole iommu memory region. This breaks dirty tracking no matter which VFIO backend is used.
Fix them all by iterating over DMAMap list to unmap each range with active mapping when migration is active. If migration is not active, unmapping the whole address space in one go is optimal.
--- hw/i386/intel_iommu.c | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+)
hw/i386/intel_iommu.c +42 -0
@@ -37,6 +37,7 @@#include "system/system.h" #include "hw/i386/apic_internal.h" #include "kvm/kvm_i386.h" +#include "migration/misc.h" #include "migration/vmstate.h" #include "trace.h"
@@ -4695,6 +4696,42 @@ static void vtd_dev_unset_iommu_device(PCIBus *bus, void *opaque, int devfn)vtd_iommu_unlock (s); } +/* +* Unmapping a large range in one go is not optimal during migration because +* a large dirty bitmap needs to be allocated while there may be only small +* mappings, iterate over DMAMap list to unmap each range with active mapping. +*/ +static void vtd_address_space_unmap_in_migration (VTDAddressSpace *as, +IOMMUNotifier *n) +{ +const DMAMap *map; +const DMAMap target = { + .iova = n->start, + .size = n->end, + }; +IOVATree *tree = as->iova_tree; + +/* +* DMAMap is created during IOMMU page table sync, it's either 4KB or huge +* page size and always a power of 2 in size. So the range of DMAMap could +* be used for UNMAP notification directly. +*/ +while ((map =iova_tree_find (tree, &target))) { +IOMMUTLBEvent event; + + event.type =IOMMU_NOTIFIER_UNMAP ; + event.entry.iova = map->iova; + event.entry.addr_mask = map->size; + event.entry.target_as = &address_space_memory; + event.entry.perm =IOMMU_NONE ; +/* This field is needed to set dirty bigmap */ + event.entry.translated_addr = map->translated_addr; +memory_region_notify_iommu_one (n, &event); + +iova_tree_remove (tree, *map); + } +} +/* Unmap the whole range in the notifier's scope. */ static void vtd_address_space_unmap (VTDAddressSpace *as,IOMMUNotifier *n) {
@@ -4704,6 +4741,11 @@ static void vtd_address_space_unmap(VTDAddressSpace *as, IOMMUNotifier *n)IntelIOMMUState *s = as->iommu_state;DMAMap map; +if (migration_is_running ()) { +vtd_address_space_unmap_in_migration (as, n); +return ; + } +/* * Note: all the codes in this function has a assumption that IOVA * bits are no more than VTD_MGAW bits (which is restricted by
Signature · 1 line
2.47.1
RE: [PATCH v5 6/9] intel_iommu: Fix unmap_bitmap failure with legacy VFIO backend
Quoted 104 lines
-----Original Message-----
From: Duan, Zhenzhong <[email protected]>
Subject: [PATCH v5 6/9]intel_iommu: Fixunmap_bitmapfailure with legacy VFIO backendIf a VFIO device in guest switches from IOMMU domain to block domain,
vtd_address_space_unmap()is called to unmap whole address space.If that happens during migration, migration fails with legacy VFIO backend as below:
Status: failed (
vfio_container_dma_unmap(0x561bbbd92d90, 0x100000000000, 0x100000000000)= -7 (Argument list too long))Because legacy VFIO limits maximum bitmap size to 256MB which maps to 8TB on
4K page system, when 16TB sized UNMAP notification is sent,unmap_bitmapioctl fails. Normally such large UNMAP notification come from IOVA range rather than system memory.Apart from that,
vtd_address_space_unmap()sends UNMAP notification withtranslated_addr= 0, because there is no validtranslated_addrfor unmapping a whole iommu memory region. This breaks dirty tracking no matter which VFIO
backend is used.Fix them all by iterating over DMAMap list to unmap each range with active mapping when migration is active. If migration is not active, unmapping the whole address space in one go is optimal.
Signed-off-by Zhenzhong Duan <[email protected]>Reviewed-by Yi Liu <[email protected]>Tested-by Giovannio Cabiddu <[email protected]>Tested-by Rohith S R <[email protected]>---
hw/i386/intel_iommu.c| 42
++++++++++++++++++++++++++++++++++++++++++1 file changed, 42 insertions(+)hw/i386/intel_iommu.c +3
-0@@ -37,6 +37,7 @@#include "system/system.h" #include "hw/i386/apic_internal.h" #include "kvm/kvm_i386.h" +#include "migration/misc.h" #include "migration/vmstate.h" #include "trace.h" @@ -4695,6 +4696,42 @@ static void vtd_dev_unset_iommu_device(PCIBus *bus, void *opaque, int devfn)vtd_iommu_unlock (s); } +/* +* Unmapping a large range in one go is not optimal during migration because
+ * a large dirty bitmap needs to be allocated while there may be only small
+ * mappings, iterate over DMAMap list to unmap each range with active mapping.
+ */hw/i386/intel_iommu.c +2
-0+static void vtd_address_space_unmap_in_migration (VTDAddressSpace *as, + IOMMUNotifier*n)
hw/i386/intel_iommu.c +10
-0+{ +const DMAMap *map; +const DMAMap target = { + .iova = n->start, + .size = n->end, + }; +IOVATree *tree = as->iova_tree; + +/* +* DMAMap is created during IOMMU page table sync, it's either 4KB or huge
+ * page size and always a power of 2 in size. So the range of DMAMap
couldhw/i386/intel_iommu.c +18
-0+* be used for UNMAP notification directly. +*/ +while ((map =iova_tree_find (tree, &target))) { +IOMMUTLBEvent event; + + event.type =IOMMU_NOTIFIER_UNMAP ; + event.entry.iova = map->iova; + event.entry.addr_mask = map->size; + event.entry.target_as = &address_space_memory; + event.entry.perm =IOMMU_NONE ; +/* This field is needed to set dirty bigmap */ + event.entry.translated_addr = map->translated_addr; +memory_region_notify_iommu_one (n, &event); + +iova_tree_remove (tree, *map); + } +} +/* Unmap the whole range in the notifier's scope. */ static void
vtd_address_space_unmap(VTDAddressSpace *as, IOMMUNotifier *n)
{
hw/i386/intel_iommu.c static void · line 4745
Quoted 5 lines
hw/i386/intel_iommu.c +1
-0@@ -4704,6 +4741,11 @@ static void vtd_address_space_unmap(VTDAddressSpace *as, IOMMUNotifier *n)IntelIOMMUState *s = as->iommu_state;DMAMap map; +if (migration_is_running ()) {
Hmm, I just realized it may be better to check global_dirty_tracking instead, because dirty rate/limit QMP also need it.
Zhenzhong
Quoted 9 lines
hw/i386/intel_iommu.c +4
-0+vtd_address_space_unmap_in_migration (as, n); +return ; + } +/* * Note: all the codes in this function has a assumption that IOVA * bits are no more than
VTD_MGAWbits (which is restricted by
--
2.47.1
PATCH v5 7/9 vfio/listener: Construct iotlb entry when unmap memory address space
In reply to Zhenzhong DuanIf a VFIO device in guest switches from passthrough(PT) domain to block domain, the whole memory address space is unmapped, but we passed a NULL iotlb entry to unmap_bitmap, then bitmap query didn't happen and we lost dirty pages.
By constructing an iotlb entry with iova = gpa for unmap_bitmap, it can set dirty bits correctly.
For IOMMU address space, we still send NULL iotlb because VFIO don't know the actual mappings in guest. It's vIOMMU's responsibility to send actual unmapping notifications, e.g., vtd_address_space_unmap_in_migration()
--- hw/vfio/listener.c | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-)
hw/vfio/listener.c +14 -1
@@ -713,14 +713,27 @@ static void vfio_listener_region_del(MemoryListener *listener,if (try_unmap) {bool unmap_all = false; +IOMMUTLBEntry entry = {}, *iotlb =NULL ;if (int128_eq (llsize,int128_2_64 ())) {assert (!iova); unmap_all = true; llsize =int128_zero (); } + +/* +* Fake an IOTLB entry for identity mapping which is needed by dirty +* tracking. In fact, in unmap_bitmap, only translated_addr field is +* used to set dirty bitmap. +*/ +if (!memory_region_is_iommu (section->mr)) { + entry.iova = iova; + entry.translated_addr = iova; + iotlb = &entry; + } + ret =vfio_container_dma_unmap (bcontainer, iova,int128_get64 (llsize), -NULL , unmap_all); + iotlb, unmap_all);if (ret) { error_report("vfio_container_dma_unmap(%p, 0x%"HWADDR_PRIx", " "0x%"HWADDR_PRIx") = %d (%s)",
Signature · 1 line
2.47.1
Re: [PATCH v5 7/9] vfio/listener: Construct iotlb entry when unmap memory address space
Quoted 4 lines · Zhenzhong Duan
If a VFIO device in guest switches from passthrough(PT) domain to block domain, the whole memory address space is unmapped, but we passed a NULL iotlb entry to
unmap_bitmap, then bitmap query didn't happen and we lost dirty pages.
this is a good catch. :) Have you observed problem in testing or just identified it with patch iteration?
Quoted 23 lines
By constructing an iotlb entry with iova = gpa for
unmap_bitmap, it can set dirty bits correctly.For IOMMU address space, we still send NULL iotlb because VFIO don't know the actual mappings in guest. It's vIOMMU's responsibility to send actual unmapping notifications, e.g.,
vtd_address_space_unmap_in_migration()Signed-off-by Zhenzhong Duan <[email protected]>Tested-by Giovannio Cabiddu <[email protected]>--- hw/vfio/listener.c | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-)hw/vfio/listener.c +1
-0@@ -713,14 +713,27 @@ static void vfio_listener_region_del(MemoryListener *listener,if (try_unmap) {bool unmap_all = false; +IOMMUTLBEntry entry = {}, *iotlb =NULL ;
hw/vfio/listener.c static void vfio_listener_region_del(MemoryListener *listener, · line 727
Quoted 10 lines
hw/vfio/listener.c +5
-0if (int128_eq (llsize,int128_2_64 ())) {assert (!iova); unmap_all = true; llsize =int128_zero (); } + +/* +* Fake an IOTLB entry for identity mapping which is needed by dirty +* tracking. In fact, in unmap_bitmap, only translated_addr field is +* used to set dirty bitmap.
Just say sync dirty is needed per unmap. So you may add a check in_migration as well. If not in migration, it is no needed to do it.
hw/vfio/listener.c static void vfio_listener_region_del(MemoryListener *listener, · line 734
Quoted 7 lines
hw/vfio/listener.c +7
-0+*/ +if (!memory_region_is_iommu (section->mr)) { + entry.iova = iova; + entry.translated_addr = iova; + iotlb = &entry; + } +
While, I'm still wondering how to deal with iommu MR case. Let's see a scenario first. When switching from DMA domain to PT, QEMU will switch to PT. This shall trigger the vfio_listener_region_del() and unregister the iommu notifier. This means vIOMMU side needs to do unmap prior to switching AS. If not, the iommu notifier is gone when vIOMMU wants to unmap with an IOTLBEvent. For virtual intel_iommu, it is calling vtd_address_space_unmap_in_migration() prior to calling vtd_switch_address_space(). So I think you need to tweak the intel_iommu a bit to suit the order requirement. :)
BTW. should the iommu MRs even go to this try_unmap branch? I think for such MRs, it relies on the vIOMMU to unmap explicitly (hence trigger the vfio_iommu_map_notify()).
hw/vfio/listener.c static void vfio_listener_region_del(MemoryListener *listener, · line 739
Quoted 6 lines
hw/vfio/listener.c +1
-1ret =vfio_container_dma_unmap (bcontainer, iova,int128_get64 (llsize), -NULL , unmap_all); + iotlb, unmap_all);if (ret) { error_report("vfio_container_dma_unmap(%p, 0x%"HWADDR_PRIx", " "0x%"HWADDR_PRIx") = %d (%s)",
Regards,
Yi Liu
RE: [PATCH v5 7/9] vfio/listener: Construct iotlb entry when unmap memory address space
Quoted 8 lines
-----Original Message-----
From: Liu, Yi L <[email protected]>
Subject: Re: [PATCH v5 7/9] vfio/listener: Construct iotlb entry when unmap memory address spaceOn 2025/11/6 12:20, Zhenzhong Duan wrote:
Quoted 8 lines
Quoted 2 lines
If a VFIO device in guest switches from passthrough(PT) domain to block domain, the whole memory address space is unmapped, but we passed a
NULL
Quoted 2 lines
iotlb entry to
unmap_bitmap, then bitmap query didn't happen and we lost dirty pages.this is a good catch. :) Have you observed problem in testing or just identified it with patch iteration?
Patch iteration.
Quoted 34 lines
Quoted 7 lines
By constructing an iotlb entry with iova = gpa for
unmap_bitmap, it can set dirty bits correctly.For IOMMU address space, we still send NULL iotlb because VFIO don't know the actual mappings in guest. It's vIOMMU's responsibility to send actual unmapping notifications, e.g.,
vtd_address_space_unmap_in_migration()hw/vfio/listener.c static void
Quoted 12 lines
Signed-off-by Zhenzhong Duan <[email protected]>Tested-by Giovannio Cabiddu <[email protected]>--- hw/vfio/listener.c | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-)hw/vfio/listener.c +0
-0@@ -713,14 +713,27 @@ static voidvfio_listener_region_del(MemoryListener *listener,
hw/vfio/listener.c static void vfio_listener_region_del(MemoryListener *listener,
Quoted 13 lines
hw/vfio/listener.c +4
-0if (try_unmap) {bool unmap_all = false; +IOMMUTLBEntry entry = {}, *iotlb =NULL ;if (int128_eq (llsize,int128_2_64 ())) {assert (!iova); unmap_all = true; llsize =int128_zero (); } + +/* +* Fake an IOTLB entry for identity mapping which is needed by
Quoted 7 lines
dirty
hw/vfio/listener.c static void · line 725
Quoted 1 line
hw/vfio/listener.c +1
-0+* tracking. In fact, in unmap_bitmap, only translated_addr field is
hw/vfio/listener.c static void vfio_listener_region_del(MemoryListener *listener, · line 727
Quoted 1 line
hw/vfio/listener.c +1
-0+* used to set dirty bitmap. Just say sync dirty is needed per unmap. So you may add a check
in_migrationas well. If not in migration, it is no needed to do it.
Dirty tracking is not only for migration, but also ditry rate/limit. So a non-null iotlb is always passed for ram MR. That iotlb pointer is used only when vfio_container_dirty_tracking_is_started() return true.
I can add a check on global_dirty_tracking if you prefer to add a check.
Quoted 9 lines
hw/vfio/listener.c static void vfio_listener_region_del(MemoryListener *listener, · line 734
Quoted 8 lines
hw/vfio/listener.c +7
-0+*/ +if (!memory_region_is_iommu (section->mr)) { + entry.iova = iova; + entry.translated_addr = iova; + iotlb = &entry; + } +
Quoted 9 lines
While, I'm still wondering how to deal with iommu MR case. Let's see a scenario first. When switching from DMA domain to PT, QEMU will switch to PT. This shall trigger the
vfio_listener_region_del()and unregister the iommu notifier. This means vIOMMU side needs to do unmap prior to switching AS. If not, the iommu notifier is gone when vIOMMU wants to unmap with an IOTLBEvent. For virtualintel_iommu, it is callingvtd_address_space_unmap_in_migration()prior to callingvtd_switch_address_space(). So I think you need to tweak theintel_iommua bit to suit the order requirement. :)
VTD doesn't support switching from DMA domain to PT atomically, so switches to block domain in between, see intel_iommu_attach_device() in kernel.
So with this sequence is DMA->block->PT domain, we have guaranteed the order you shared? See vtd_pasid_cache_sync_locked().
Quoted 4 lines
BTW. should the iommu MRs even go to this
try_unmapbranch? I think for such MRs, it relies on the vIOMMU to unmap explicitly (hence trigger thevfio_iommu_map_notify()).
Yes, it's unnecessary, but it's hard for VFIO to distinguish if try_unmap is due to domain switch or a real unmap. I think it's harmless because the second try_unmap unmaps nothing.
Thanks
Zhenzhong
Quoted 12 lines
hw/vfio/listener.c static void · line 734
Quoted 2 lines
hw/vfio/listener.c +0
-0ret = vfio_container_dma_unmap(bcontainer, iova,
int128_get64(llsize),hw/vfio/listener.c static void vfio_listener_region_del(MemoryListener *listener,
Quoted 4 lines
hw/vfio/listener.c +1
-1- NULL, unmap_all); + iotlb, unmap_all);if (ret) { error_report("vfio_container_dma_unmap(%p,0x%"HWADDR_PRIx", "
hw/vfio/listener.c static void vfio_listener_region_del(MemoryListener *listener, · line 739
Quoted 1 line
hw/vfio/listener.c +0
-0"0x%"HWADDR_PRIx") = %d (%s)",Regards,
Yi Liu
Yi Liu
Reviewed-byRe: [PATCH v5 7/9] vfio/listener: Construct iotlb entry when unmap memory address space
Quoted 58 lines · Zhenzhong
Quoted 8 lines
-----Original Message-----
From: Liu, Yi L <[email protected]>
Subject: Re: [PATCH v5 7/9] vfio/listener: Construct iotlb entry when unmap memory address spaceOn 2025/11/6 12:20, Zhenzhong Duan wrote:
Quoted 8 lines
Quoted 2 lines
If a VFIO device in guest switches from passthrough(PT) domain to block domain, the whole memory address space is unmapped, but we passed a
NULL
Quoted 2 lines
iotlb entry to
unmap_bitmap, then bitmap query didn't happen and we lost dirty pages.this is a good catch. :) Have you observed problem in testing or just identified it with patch iteration?
Patch iteration.
Quoted 39 lines
Quoted 7 lines
By constructing an iotlb entry with iova = gpa for
unmap_bitmap, it can set dirty bits correctly.For IOMMU address space, we still send NULL iotlb because VFIO don't know the actual mappings in guest. It's vIOMMU's responsibility to send actual unmapping notifications, e.g.,
vtd_address_space_unmap_in_migration()hw/vfio/listener.c static void
Quoted 12 lines
Signed-off-by Zhenzhong Duan <[email protected]>Tested-by Giovannio Cabiddu <[email protected]>--- hw/vfio/listener.c | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-)hw/vfio/listener.c +0
-0@@ -713,14 +713,27 @@ static voidvfio_listener_region_del(MemoryListener *listener,
hw/vfio/listener.c static void vfio_listener_region_del(MemoryListener *listener,
Quoted 13 lines
hw/vfio/listener.c +4
-0if (try_unmap) {bool unmap_all = false; +IOMMUTLBEntry entry = {}, *iotlb =NULL ;if (int128_eq (llsize,int128_2_64 ())) {assert (!iova); unmap_all = true; llsize =int128_zero (); } + +/* +* Fake an IOTLB entry for identity mapping which is needed by dirty
hw/vfio/listener.c static void · line 725
Quoted 1 line
hw/vfio/listener.c +1
-0+* tracking. In fact, in unmap_bitmap, only translated_addr field is
Quoted 1 line
hw/vfio/listener.c +1
-0+* used to set dirty bitmap.
Quoted 8 lines
Quoted 2 lines
Just say sync dirty is needed per unmap. So you may add a check
in_migrationas well. If not in migration, it is no needed to do it.Dirty tracking is not only for migration, but also ditry rate/limit. So a non-null iotlb is always passed for ram MR. That iotlb pointer is used only when
vfio_container_dirty_tracking_is_started()return true.I can add a check on
global_dirty_trackingif you prefer to add a check.
yeah, this would be helpful.
Quoted 20 lines
Quoted 10 lines
hw/vfio/listener.c static void vfio_listener_region_del(MemoryListener *listener, · line 734
Quoted 9 lines
hw/vfio/listener.c +7
-0+*/ +if (!memory_region_is_iommu (section->mr)) { + entry.iova = iova; + entry.translated_addr = iova; + iotlb = &entry; + } +Quoted 9 lines
While, I'm still wondering how to deal with iommu MR case. Let's see a scenario first. When switching from DMA domain to PT, QEMU will switch to PT. This shall trigger the
vfio_listener_region_del()and unregister the iommu notifier. This means vIOMMU side needs to do unmap prior to switching AS. If not, the iommu notifier is gone when vIOMMU wants to unmap with an IOTLBEvent. For virtualintel_iommu, it is callingvtd_address_space_unmap_in_migration()prior to callingvtd_switch_address_space(). So I think you need to tweak theintel_iommua bit to suit the order requirement. :)
Quoted 5 lines
VTD doesn't support switching from DMA domain to PT atomically, so switches to block domain in between, see
intel_iommu_attach_device()in kernel.So with this sequence is
DMA->block->PTdomain, we have guaranteed the order you shared? Seevtd_pasid_cache_sync_locked().
I see. So guest helps it. This might be a bit weak since we rely on guest behavior. I think you may add a TODO or add comment to note it.
BTW. I think the subject can be refined since the real purpose is to make tracking dirty pages in the unmap happen in region_del.
vfio/listener: Add missing dirty tracking in region_del
with this and the prior check, this patch looks good to me.
Quoted 8 lines
Quoted 4 lines
BTW. should the iommu MRs even go to this
try_unmapbranch? I think for such MRs, it relies on the vIOMMU to unmap explicitly (hence trigger thevfio_iommu_map_notify()).Yes, it's unnecessary, but it's hard for VFIO to distinguish if
try_unmapis due to domain switch or a real unmap. I think it's harmless because the secondtry_unmapunmaps nothing.
hmmm. can a unmap path go to region_del()? Not quite get the second try_unmap, do you mean when vIOMMU unmaps everything via vfio_iommu_map_notify() and then switch AS which triggers the region_del and this try_unmap branch?
Regards,
Yi Liu
RE: [PATCH v5 7/9] vfio/listener: Construct iotlb entry when unmap memory address space
Quoted 104 lines
-----Original Message-----
From: Liu, Yi L <[email protected]>
Subject: Re: [PATCH v5 7/9] vfio/listener: Construct iotlb entry when unmap memory address spaceQuoted 5 lines · Zhenzhong
Quoted 5 lines
-----Original Message-----
From: Liu, Yi L <[email protected]>
Subject: Re: [PATCH v5 7/9] vfio/listener: Construct iotlb entry whenunmap
Quoted 7 lines
Quoted 7 lines
memory address space
Quoted 2 lines · Zhenzhong Duan
If a VFIO device in guest switches from passthrough(PT) domain to block domain, the whole memory address space is unmapped, but we passed a
NULL
Quoted 1 line
iotlb entry to
unmap_bitmap, then bitmap query didn't happen and welost
Quoted 41 lines
Quoted 4 lines
Quoted 1 line
dirty pages.
this is a good catch. :) Have you observed problem in testing or just identified it with patch iteration?
Patch iteration.
Quoted 34 lines
Quoted 7 lines
By constructing an iotlb entry with iova = gpa for
unmap_bitmap, it can set dirty bits correctly.For IOMMU address space, we still send NULL iotlb because VFIO don't know the actual mappings in guest. It's vIOMMU's responsibility to send actual unmapping notifications, e.g.,
vtd_address_space_unmap_in_migration()hw/vfio/listener.c static void
Quoted 12 lines
Signed-off-by Zhenzhong Duan <[email protected]>Tested-by Giovannio Cabiddu <[email protected]>--- hw/vfio/listener.c | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-)hw/vfio/listener.c +0
-0@@ -713,14 +713,27 @@ static voidvfio_listener_region_del(MemoryListener *listener,
Quoted 13 lines
hw/vfio/listener.c +4
-0if (try_unmap) {bool unmap_all = false; +IOMMUTLBEntry entry = {}, *iotlb =NULL ;if (int128_eq (llsize,int128_2_64 ())) {assert (!iova); unmap_all = true; llsize =int128_zero (); } + +/* +* Fake an IOTLB entry for identity mapping which is needed by
Quoted 2 lines
Quoted 2 lines
dirty
Quoted 1 line
hw/vfio/listener.c +1
-0+* tracking. In fact, in unmap_bitmap, only translated_addr field
Quoted 7 lines
Quoted 5 lines
is
hw/vfio/listener.c static void vfio_listener_region_del(MemoryListener *listener, · line 727
Quoted 1 line
hw/vfio/listener.c +1
-0+* used to set dirty bitmap. Just say sync dirty is needed per unmap. So you may add a check
in_migrationas well. If not in migration, it is no needed to do it.Dirty tracking is not only for migration, but also ditry rate/limit. So a
non-null iotlb
Quoted 4 lines
is always passed for ram MR. That iotlb pointer is used only when
vfio_container_dirty_tracking_is_started()return true.I can add a check on
global_dirty_trackingif you prefer to add a check.yeah, this would be helpful.
Quoted 20 lines
Quoted 10 lines
hw/vfio/listener.c static void vfio_listener_region_del(MemoryListener *listener, · line 734
Quoted 9 lines
hw/vfio/listener.c +7
-0+*/ +if (!memory_region_is_iommu (section->mr)) { + entry.iova = iova; + entry.translated_addr = iova; + iotlb = &entry; + } +Quoted 9 lines
While, I'm still wondering how to deal with iommu MR case. Let's see a scenario first. When switching from DMA domain to PT, QEMU will switch to PT. This shall trigger the
vfio_listener_region_del()and unregister the iommu notifier. This means vIOMMU side needs to do unmap prior to switching AS. If not, the iommu notifier is gone when vIOMMU wants to unmap with an IOTLBEvent. For virtualintel_iommu, it is callingvtd_address_space_unmap_in_migration()prior to callingvtd_switch_address_space(). So I think you need to tweak theintel_iommua bit to suit the order requirement. :)Quoted 1 line
VTD doesn't support switching from DMA domain to PT atomically, so
switches
Quoted 8 lines
Quoted 3 lines
to block domain in between, see
intel_iommu_attach_device()in kernel.So with this sequence is
DMA->block->PTdomain, we have guaranteed theorder
Quoted 1 line
you shared? See
vtd_pasid_cache_sync_locked().I see. So guest helps it. This might be a bit weak since we rely on guest behavior. I think you may add a TODO or add comment to note it.
Make sense, will add.
Quoted 5 lines
BTW. I think the subject can be refined since the real purpose is to make tracking dirty pages in the unmap happen in
region_del.vfio/listener: Add missing dirty tracking in
region_del
Will do.
Quoted 12 lines
with this and the prior check, this patch looks good to me.
Reviewed-by Yi Liu <[email protected]>Quoted 6 lines
Quoted 4 lines
BTW. should the iommu MRs even go to this
try_unmapbranch? I think for such MRs, it relies on the vIOMMU to unmap explicitly (hence trigger thevfio_iommu_map_notify()).Yes, it's unnecessary, but it's hard for VFIO to distinguish if
try_unmapis dueto
Quoted 8 lines
Quoted 1 line
domain switch or a real unmap. I think it's harmless because the second
try_unmapQuoted 1 line
unmaps nothing.
hmmm. can a unmap path go to
region_del()? Not quite get the secondtry_unmap, do you mean when vIOMMU unmaps everything viavfio_iommu_map_notify()and then switch AS which triggers theregion_deland thistry_unmapbranch?
Yes, vfio_iommu_map_notify() is first try_unmap, unmap in region_del is the second try_unmap.
Thanks
Zhenzhong
PATCH v5 8/9 vfio/migration: Add migration blocker if VM memory is too large to cause unmap_bitmap failure
In reply to Zhenzhong DuanWith default config, kernel VFIO IOMMU type1 driver limits dirty bitmap to 256MB for unmap_bitmap ioctl so the maximum guest memory region is no more than 8TB size for the ioctl to succeed.
Be conservative here to limit total guest memory to max value supported by unmap_bitmap ioctl or else add a migration blocker. IOMMUFD backend doesn't have such limit, one can use it if there is a need to migrate such large VM.
--- hw/vfio/migration.c | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+)
hw/vfio/migration.c +34 -0
@@ -16,6 +16,7 @@#include <sys/ioctl.h> #include "system/runstate.h" +#include "hw/boards.h" #include "hw/vfio/vfio-device.h" #include "hw/vfio/vfio-migration.h" #include "migration/misc.h"
@@ -1152,6 +1153,32 @@ static bool vfio_viommu_preset(VFIODevice *vbasedev)return vbasedev->bcontainer->space->as != &address_space_memory; } +static bool vfio_dirty_tracking_exceed_limit (VFIODevice *vbasedev) +{ +VFIOContainer *bcontainer = vbasedev->bcontainer; +uint64_t max_size, page_size; + +if (!bcontainer->dirty_pages_supported) { +return false; + } + +/* +* VFIO IOMMU type1 driver has limitation of bitmap size on unmap_bitmap +* ioctl(), calculate the limit and compare with guest memory size to +* catch dirty tracking failure early. +* +* This limit is 8TB with default kernel and QEMU config, we are a bit +* conservative here as VM memory layout may be nonconsecutive or VM +* can run with vIOMMU enabled so the limitation could be relaxed. One +* can also switch to use IOMMUFD backend if there is a need to migrate +* large VM. +*/ + page_size =1 <<ctz64 (bcontainer->dirty_pgsizes); + max_size = bcontainer->max_dirty_bitmap_size *BITS_PER_BYTE * page_size; + +return current_machine->ram_size > max_size; +} +/* * Return true when either migration initialized or blocker registered. * Currently only return false when adding blocker fails which will
@@ -1193,6 +1220,13 @@ bool vfio_migration_realize(VFIODevice *vbasedev, Error **errp) goto add_blocker; } +if (vfio_dirty_tracking_exceed_limit (vbasedev)) { +error_setg (&err,"%s: Migration is currently not supported with " +"large memory VM due to dirty tracking limitation in " +"backend" , vbasedev->name); + goto add_blocker; + } +warn_report ("%s: VFIO device doesn't support device and " "IOMMU dirty tracking" , vbasedev->name); }
Signature · 1 line
2.47.1
Re: [PATCH v5 8/9] vfio/migration: Add migration blocker if VM memory is too large to cause unmap_bitmap failure
Quoted 4 lines · Zhenzhong Duan
With default config, kernel VFIO IOMMU type1 driver limits dirty bitmap to 256MB for
unmap_bitmapioctl so the maximum guest memory region is no more than 8TB size for the ioctl to succeed.
Quoted 8 lines
Be conservative here to limit total guest memory to max value supported by
unmap_bitmapioctl or else add a migration blocker. IOMMUFD backend doesn't have such limit, one can use it if there is a need to migrate such large VM.Suggested-by Yi Liu <[email protected]>Signed-off-by Zhenzhong Duan <[email protected]>---
Quoted 62 lines
hw/vfio/migration.c | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+)hw/vfio/migration.c +34
-0@@ -16,6 +16,7 @@#include <sys/ioctl.h> #include "system/runstate.h" +#include "hw/boards.h" #include "hw/vfio/vfio-device.h" #include "hw/vfio/vfio-migration.h" #include "migration/misc.h" @@ -1152,6 +1153,32 @@ static bool vfio_viommu_preset(VFIODevice *vbasedev)return vbasedev->bcontainer->space->as != &address_space_memory; } +static bool vfio_dirty_tracking_exceed_limit (VFIODevice *vbasedev) +{ +VFIOContainer *bcontainer = vbasedev->bcontainer; +uint64_t max_size, page_size; + +if (!bcontainer->dirty_pages_supported) { +return false; + } + +/* +* VFIO IOMMU type1 driver has limitation of bitmap size on unmap_bitmap +* ioctl(), calculate the limit and compare with guest memory size to +* catch dirty tracking failure early. +* +* This limit is 8TB with default kernel and QEMU config, we are a bit +* conservative here as VM memory layout may be nonconsecutive or VM +* can run with vIOMMU enabled so the limitation could be relaxed. One +* can also switch to use IOMMUFD backend if there is a need to migrate +* large VM. +*/ + page_size =1 <<ctz64 (bcontainer->dirty_pgsizes); + max_size = bcontainer->max_dirty_bitmap_size *BITS_PER_BYTE * page_size; + +return current_machine->ram_size > max_size; +} +/* * Return true when either migration initialized or blocker registered. * Currently only return false when adding blocker fails which will @@ -1193,6 +1220,13 @@ bool vfio_migration_realize(VFIODevice *vbasedev, Error **errp) goto add_blocker; } +if (vfio_dirty_tracking_exceed_limit (vbasedev)) { +error_setg (&err,"%s: Migration is currently not supported with " +"large memory VM due to dirty tracking limitation in " +"backend" , vbasedev->name); + goto add_blocker; + } +warn_report ("%s: VFIO device doesn't support device and " "IOMMU dirty tracking" , vbasedev->name); }
PATCH v5 9/9 vfio/migration: Allow live migration with vIOMMU without VFs using device dirty tracking
In reply to Zhenzhong DuanCommit e46883204c38 ("vfio/migration: Block migration with vIOMMU") introduces a migration blocker when vIOMMU is enabled, because we need to calculate the IOVA ranges for device dirty tracking. But this is unnecessary for iommu dirty tracking.
Limit the vfio_viommu_preset() check to those devices which use device dirty tracking. This allows live migration with VFIO devices which use iommu dirty tracking.
--- include/hw/vfio/vfio-device.h | 10 ++++++++++ hw/vfio/container.c | 5 +---- hw/vfio/device.c | 6 ++++++ hw/vfio/migration.c | 6 +++--- 4 files changed, 20 insertions(+), 7 deletions(-)
include/hw/vfio/vfio-device.h +10 -0
@@ -148,6 +148,16 @@ bool vfio_device_irq_set_signaling(VFIODevice *vbasedev, int index, int subindexvoid vfio_device_reset_handler (void *opaque);bool vfio_device_is_mdev (VFIODevice *vbasedev); +/** +* vfio_device_dirty_pages_disabled: Check if device dirty tracking will be +* used for a VFIO device +* +* @vbasedev: The VFIODevice to transform +* +* Return: true if either @vbasedev doesn't support device dirty tracking or +* is forcedly disabled from command line, otherwise false. +*/ +bool vfio_device_dirty_pages_disabled (VFIODevice *vbasedev);bool vfio_device_hiod_create_and_realize (VFIODevice *vbasedev,const char *typename,Error **errp); bool vfio_device_attach(char *name, VFIODevice *vbasedev,
hw/vfio/container.c +1 -4
@@ -178,10 +178,7 @@ bool vfio_container_devices_dirty_tracking_is_supported(VFIODevice *vbasedev; QLIST_FOREACH(vbasedev, &bcontainer->device_list, container_next) { -if (vbasedev->device_dirty_page_tracking ==ON_OFF_AUTO_OFF ) { -return false; - } -if (!vbasedev->dirty_pages_supported) { +if (vfio_device_dirty_pages_disabled (vbasedev)) {return false; } }
hw/vfio/device.c +6 -0
@@ -411,6 +411,12 @@ bool vfio_device_is_mdev(VFIODevice *vbasedev)return subsys && (strcmp (subsys,"/sys/bus/mdev" ) ==0 ); } +bool vfio_device_dirty_pages_disabled (VFIODevice *vbasedev) +{ +return (!vbasedev->dirty_pages_supported || + vbasedev->device_dirty_page_tracking ==ON_OFF_AUTO_OFF ); +} +bool vfio_device_hiod_create_and_realize (VFIODevice *vbasedev,const char *typename,Error **errp) {
hw/vfio/migration.c +3 -3
@@ -1210,8 +1210,7 @@ bool vfio_migration_realize(VFIODevice *vbasedev, Error **errp)return !vfio_block_migration (vbasedev, err, errp); } -if ((!vbasedev->dirty_pages_supported || - vbasedev->device_dirty_page_tracking ==ON_OFF_AUTO_OFF ) && +if (vfio_device_dirty_pages_disabled (vbasedev) && !vbasedev->iommu_dirty_tracking) {if (vbasedev->enable_migration ==ON_OFF_AUTO_AUTO ) { error_setg(&err,
@@ -1236,7 +1235,8 @@ bool vfio_migration_realize(VFIODevice *vbasedev, Error **errp) goto out_deinit; } -if (vfio_viommu_preset (vbasedev)) { +if (!vfio_device_dirty_pages_disabled (vbasedev) && +vfio_viommu_preset (vbasedev)) {error_setg (&err,"%s: Migration is currently not supported " "with vIOMMU enabled" , vbasedev->name); goto add_blocker;
Signature · 1 line
2.47.1
Hi All,
Kindly ping😊, any more comments?
Thanks
Zhenzhong
Quoted 109 lines
-----Original Message-----
From: Duan, Zhenzhong <[email protected]>
Subject: [PATCH v5 0/9] vfio: relax the vIOMMU checkHi,
This series relax the vIOMMU check and allows live migration with vIOMMU without VFs using device dirty tracking. It's rewritten based on first 4 patches of [1] from Joao.
Currently what block us is the lack of dirty bitmap query with iommufd before unmap. By adding that query and handle some corner case we can relax the check.
Based on vfio-next branch:
patch1-2: add dirty bitmap query with iommufd patch3: a ranaming cleanup patch4-5: unmap_bitmap optimization patch6-7: fixes to avoid losing dirty pages patch8: add a blocker if VM memory is really quite large for unmap_bitmap patch9: relax vIOMMU checkWe used [2] to test, it contains
dom_switchseries + this series + nesting series. I included nesting series just because I'd like to confirm the two patches optimizing out dirty tracking for readonly pages work.We tested VM live migration (running QAT workload in VM) with QAT device passthrough, below matrix configs with guest config 'iommu=pt' and 'iommu=nopt':
1.Scalable mode vIOMMU + IOMMUFD cdev mode
2.Scalable mode vIOMMU + legacy VFIO mode
3.legacy mode vIOMMU + IOMMUFD cdev mode
4.legacy mode vIOMMU + legacy VFIO modeThe QAT workload is a user level app that utilizes VFIO to control QAT device.
Thanks
Zhenzhong[1] https://github.com/jpemartins/qemu/commits/vfio-migration-viommu/
[2]
https://github.com/yiliu1765/qemu/tree/liuyi/zhenzhong/iommufd_nesting.v
8.DS_LM.wipChangelog:
v5:
- drop the patch checkingiommu_dirty_tracking(Avihai, Joao)
- pass iotlb info tounmap_bitmapwhen switch out of system ASv4:
- bypass memory size check for device dirty tracking as it's unrelated (Avihai)
- splitvfio_device_dirty_pages_disabled()helper out as a separate patch
- add a patch to fix minor error on checkingvbasedev->iommu_dirty_trackingv3:
- return bitmap query failure to fail migration (Avihai)
- refine patch7, set IOMMUFD backend 'dirty_pgsizes' and 'max_dirty_bitmap_size' (Cedric)
- refine patch7, calculate memory limit instead of hardcode 8TB (Liuyi)
- refine commit log (Cedric, Liuyi)v2:
- addbackend_flagparameter to passDIRTY_BITMAP_NO_CLEAR(Joao, Cedric)
- add a cleanup patch to renamevfio_dma_unmap_bitmap(Cedric)
- add blocker ifunmap_bitmaplimit check fail (Liuyi)Joao Martins (1):
vfio: Add abackend_flagparameter to
vfio_contianer_query_dirty_bitmap()Zhenzhong Duan (8):
vfio/iommufd: Add framework code to support getting dirty bitmap
before unmap
vfio/iommufd: Query dirty bitmap before DMA unmap vfio/container-legacy: renamevfio_dma_unmap_bitmap()to
vfio_legacy_dma_unmap_get_dirty_bitmap()
vfio/iommufd: AddIOMMU_HWPT_GET_DIRTY_BITMAP_NO_CLEARflag
support
intel_iommu: Fixunmap_bitmapfailure with legacy VFIO backend vfio/listener: Construct iotlb entry when unmap memory address space vfio/migration: Add migration blocker if VM memory is too large to
causeunmap_bitmapfailure
vfio/migration: Allow live migration with vIOMMU without VFs using
device dirty trackinginclude/hw/vfio/vfio-container.h | 8 +++-- include/hw/vfio/vfio-device.h | 10 ++++++ include/system/iommufd.h | 2 +- backends/iommufd.c | 5 +-- hw/i386/intel_iommu.c | 42 +++++++++++++++++++++++++ hw/vfio-user/container.c | 5 +-- hw/vfio/container-legacy.c | 15 +++++---- hw/vfio/container.c | 20 ++++++------ hw/vfio/device.c | 6 ++++ hw/vfio/iommufd.c | 53 +++++++++++++++++++++++++++++--- hw/vfio/listener.c | 21 ++++++++++--- hw/vfio/migration.c | 40 ++++++++++++++++++++++-- backends/trace-events | 2 +- hw/vfio/trace-events | 2 +- 14 files changed, 194 insertions(+), 37 deletions(-)--
2.47.1
Quoted 3 lines · Zhenzhong
Hi All,
Kindly ping😊, any more comments?
Yes ! These are for QEMU 11.0 and it would be nice to have some Acks on the remaining patches.
Thanks,
C.