Skip to contentSkip to conversation

vfio: relax the vIOMMU check

PATCH Superseded Zhenzhong Duan 4 participants 36 messages Sep 10, 2025 – Oct 17, 2025

v1 of 6 Superseded Latest is v6 →

lore
Share & tools
lore
#0 lore raw

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 in patch2 plus an extra optimization in patch3 that let us simply read the dirty bit (without clearing it), then an optimization in patch4 to handle a corner case of domain switch in guest, finally we relax the check in patch5.

We tested VM live migration (running QAT workload in VM) with QAT device passthrough, below matrix configs:
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

[1] https://github.com/jpemartins/qemu/commits/vfio-migration-viommu/

Thanks
Zhenzhong

Zhenzhong Duan (5):
vfio/iommufd: Add framework code to support getting dirty bitmap
before unmap
vfio/iommufd: Query dirty bitmap before DMA unmap vfio/iommufd: Add IOMMU_HWPT_GET_DIRTY_BITMAP_NO_CLEAR flag support
intel_iommu: Optimize unmap_bitmap during migration vfio/migration: Allow live migration with vIOMMU without VFs using
device dirty tracking

 hw/vfio/vfio-iommufd.h        |  1 +
 include/hw/vfio/vfio-device.h | 10 ++++++
 include/system/iommufd.h      |  2 +-
 backends/iommufd.c            |  5 +--
 hw/i386/intel_iommu.c         | 42 ++++++++++++++++++++++
 hw/vfio/container-base.c      |  5 +--
 hw/vfio/device.c              |  6 ++++
 hw/vfio/iommufd.c             | 66 +++++++++++++++++++++++++++++------
 hw/vfio/migration.c           |  6 ++--
 backends/trace-events         |  2 +-
 10 files changed, 123 insertions(+), 22 deletions(-)
Signature · 1 line
2.47.1
#1 lore raw

PATCH 1/5 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_one() to choose either device or iommu dirty tracking, just like vfio_legacy_dma_unmap_one().

---
 hw/vfio/iommufd.c | 56 +++++++++++++++++++++++++++++++++++++----------
 1 file changed, 45 insertions(+), 11 deletions(-)
hw/vfio/iommufd.c +45 -11
index 48c590b6a9..b5d6e54c45 100644
@@ -58,33 +58,67 @@ static int iommufd_cdev_map_file(const VFIOContainerBase *bcontainer,
                                         iova, size, fd, start, readonly);
 }
 
-static int iommufd_cdev_unmap(const VFIOContainerBase *bcontainer,
-                              hwaddr iova, ram_addr_t size,
-                              IOMMUTLBEntry *iotlb, bool unmap_all)
+static int iommufd_cdev_unmap_one(const VFIOContainerBase *bcontainer,
+                                  hwaddr iova, ram_addr_t size,
+                                  IOMMUTLBEntry *iotlb)
 {
     const VFIOIOMMUFDContainer *container =
         container_of(bcontainer, VFIOIOMMUFDContainer, bcontainer);
+    bool need_dirty_sync = false;
+    Error *local_err = NULL;
+    IOMMUFDBackend *be = container->be;
+    uint32_t ioas_id = container->ioas_id;
+    int ret;
+
+    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 int iommufd_cdev_unmap(const VFIOContainerBase *bcontainer,
+                              hwaddr iova, ram_addr_t size,
+                              IOMMUTLBEntry *iotlb, bool unmap_all)
+{
     /* unmap in halves */
     if (unmap_all) {
         Int128 llsize = int128_rshift(int128_2_64(), 1);
         int ret;
 
-        ret = iommufd_backend_unmap_dma(container->be, container->ioas_id,
-                                        0, int128_get64(llsize));
+        ret = iommufd_cdev_unmap_one(bcontainer, 0, int128_get64(llsize),
+                                     iotlb);
 
         if (ret == 0) {
-            ret = iommufd_backend_unmap_dma(container->be, container->ioas_id,
-                                            int128_get64(llsize),
-                                            int128_get64(llsize));
+            ret = iommufd_cdev_unmap_one(bcontainer, int128_get64(llsize),
+                                         int128_get64(llsize), iotlb);
         }
 
         return ret;
     }
 
-    /* TODO: Handle dma_unmap_bitmap with iotlb args (migration) */
-    return iommufd_backend_unmap_dma(container->be,
-                                     container->ioas_id, iova, size);
+    return iommufd_cdev_unmap_one(bcontainer, iova, size, iotlb);
 }
 
 static bool iommufd_cdev_kvm_device_add(VFIODevice *vbasedev, Error **errp)
Signature · 1 line
2.47.1
#2 lore raw

Re: [PATCH 1/5] vfio/iommufd: Add framework code to support getting dirty bitmap before unmap

Hello Zhenzhong

Quoted 5 lines · Zhenzhong Duan

Currently we support device and iommu dirty tracking, device dirty tracking is preferred.

Add the framework code in iommufd_cdev_unmap_one() to choose either device or iommu dirty tracking, just like vfio_legacy_dma_unmap_one().

I wonder if commit 567d7d3e6be5 ("vfio/common: Work around kernel overflow bug in DMA unmap") could be removed now to make the code common to both VFIO IOMMU Type1 and IOMMUFD backends.

I asked Alex and Peter in another thread.

Thanks,

C.

Quoted 90 lines
---
  hw/vfio/iommufd.c | 56 +++++++++++++++++++++++++++++++++++++----------
  1 file changed, 45 insertions(+), 11 deletions(-)
hw/vfio/iommufd.c +44 -11
index 48c590b6a9..b5d6e54c45 100644
@@ -58,33 +58,67 @@ static int iommufd_cdev_map_file(const VFIOContainerBase *bcontainer,
                                          iova, size, fd, start, readonly);
  }
 
-static int iommufd_cdev_unmap(const VFIOContainerBase *bcontainer,
-                              hwaddr iova, ram_addr_t size,
-                              IOMMUTLBEntry *iotlb, bool unmap_all)
+static int iommufd_cdev_unmap_one(const VFIOContainerBase *bcontainer,
+                                  hwaddr iova, ram_addr_t size,
+                                  IOMMUTLBEntry *iotlb)
  {
      const VFIOIOMMUFDContainer *container =
          container_of(bcontainer, VFIOIOMMUFDContainer, bcontainer);
+    bool need_dirty_sync = false;
+    Error *local_err = NULL;
+    IOMMUFDBackend *be = container->be;
+    uint32_t ioas_id = container->ioas_id;
+    int ret;
+
+    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 int iommufd_cdev_unmap(const VFIOContainerBase *bcontainer,
+                              hwaddr iova, ram_addr_t size,
+                              IOMMUTLBEntry *iotlb, bool unmap_all)
+{
      /* unmap in halves */
      if (unmap_all) {
          Int128 llsize = int128_rshift(int128_2_64(), 1);
          int ret;
 
-        ret = iommufd_backend_unmap_dma(container->be, container->ioas_id,
-                                        0, int128_get64(llsize));
+        ret = iommufd_cdev_unmap_one(bcontainer, 0, int128_get64(llsize),
+                                     iotlb);
 
          if (ret == 0) {
-            ret = iommufd_backend_unmap_dma(container->be, container->ioas_id,
-                                            int128_get64(llsize),
-                                            int128_get64(llsize));
+            ret = iommufd_cdev_unmap_one(bcontainer, int128_get64(llsize),
+                                         int128_get64(llsize), iotlb);
          }
 
          return ret;
      }
 
-    /* TODO: Handle dma_unmap_bitmap with iotlb args (migration) */
-    return iommufd_backend_unmap_dma(container->be,
-                                     container->ioas_id, iova, size);
+    return iommufd_cdev_unmap_one(bcontainer, iova, size, iotlb);
  }
 
  static bool iommufd_cdev_kvm_device_add(VFIODevice *vbasedev, Error **errp)
author Reviewed-by Dan Carpenter Reviewed-by Peter Xu Tested-by Peter Xu Reviewed-by Cornelia Huck
#3 lore raw

RE: [PATCH 1/5] vfio/iommufd: Add framework code to support getting dirty bitmap before unmap

Hi Cedric,

Quoted 11 lines

-----Original Message-----
From: Cédric Le Goater <[email protected]>
Subject: Re: [PATCH 1/5] vfio/iommufd: Add framework code to support getting dirty bitmap before unmap

Hello Zhenzhong

Quoted 3 lines · Zhenzhong Duan

Currently we support device and iommu dirty tracking, device dirty tracking is preferred.

Quoted 6 lines
Quoted 2 lines

Add the framework code in iommufd_cdev_unmap_one() to choose either device or iommu dirty tracking, just like vfio_legacy_dma_unmap_one().

I wonder if commit 567d7d3e6be5 ("vfio/common: Work around kernel overflow bug in DMA unmap") could be removed now to make the code common to both VFIO IOMMU Type1 and IOMMUFD backends.

I am not clear if there is other reason to keep the workaround, but the original kernel issue had been fixed with below commit:

commit 58fec830fc19208354895d9832785505046d6c01

Author: Alex Williamson <[email protected]>
Date:   Mon Jan 7 22:13:22 2019 -0700

vfio/type1: Fix unmap overflow off-by-one

The below referenced commit adds a test for integer overflow, but in doing so prevents the unmap ioctl from ever including the last page of the address space. Subtract one to compare to the last address of the unmap to avoid the overflow and wrap-around.

Quoted 2 lines

I asked Alex and Peter in another thread.

Just curious on the answer, may I ask which thread?

btw: I just found unmapping in halves seems unnecessary as both backends of kernel side support unmap_all now.

    if (unmap_all) {
        /* The unmap ioctl doesn't accept a full 64-bit span. */
        Int128 llsize = int128_rshift(int128_2_64(), 1);

        ret = vfio_legacy_dma_unmap_one(bcontainer, 0, int128_get64(llsize),
                                        iotlb);

        if (ret == 0) {
            ret = vfio_legacy_dma_unmap_one(bcontainer, int128_get64(llsize),
                                            int128_get64(llsize), iotlb);
        }

    } else {
        ret = vfio_legacy_dma_unmap_one(bcontainer, iova, size, iotlb);
    }

Thanks
Zhenzhong

#4 lore raw

Re: [PATCH 1/5] vfio/iommufd: Add framework code to support getting dirty bitmap before unmap

Quoted 46 lines · Zhenzhong

Hi Cedric,

Quoted 11 lines

-----Original Message-----
From: Cédric Le Goater <[email protected]>
Subject: Re: [PATCH 1/5] vfio/iommufd: Add framework code to support getting dirty bitmap before unmap

Hello Zhenzhong

Quoted 3 lines · Zhenzhong Duan

Currently we support device and iommu dirty tracking, device dirty tracking is preferred.

Quoted 6 lines
Quoted 2 lines

Add the framework code in iommufd_cdev_unmap_one() to choose either device or iommu dirty tracking, just like vfio_legacy_dma_unmap_one().

I wonder if commit 567d7d3e6be5 ("vfio/common: Work around kernel overflow bug in DMA unmap") could be removed now to make the code common to both VFIO IOMMU Type1 and IOMMUFD backends.

I am not clear if there is other reason to keep the workaround, but the original kernel issue had been fixed with below commit:

commit 58fec830fc19208354895d9832785505046d6c01

Author: Alex Williamson <[email protected]>
Date:   Mon Jan 7 22:13:22 2019 -0700

vfio/type1: Fix unmap overflow off-by-one

The below referenced commit adds a test for integer overflow, but in doing so prevents the unmap ioctl from ever including the last page of the address space. Subtract one to compare to the last address of the unmap to avoid the overflow and wrap-around.

Quoted 1 line
Quoted 3 lines
Quoted 1 line

I asked Alex and Peter in another thread.

Just curious on the answer, may I ask which thread?

According to Alex, the QEMU workaround can be removed :

https://lore.kernel.org/qemu-devel/[email protected]/

Quoted 17 lines

btw: I just found unmapping in halves seems unnecessary as both backends of kernel side support unmap_all now.

     if (unmap_all) {
         /* The unmap ioctl doesn't accept a full 64-bit span. */
         Int128 llsize = int128_rshift(int128_2_64(), 1);

         ret = vfio_legacy_dma_unmap_one(bcontainer, 0, int128_get64(llsize),
                                         iotlb);

         if (ret == 0) {
             ret = vfio_legacy_dma_unmap_one(bcontainer, int128_get64(llsize),
                                             int128_get64(llsize), iotlb);
         }

     } else {
         ret = vfio_legacy_dma_unmap_one(bcontainer, iova, size, iotlb);
     }

Good. So we can simply both backends it seems.

Thanks,

C.

#5 lore raw

RE: [PATCH 1/5] vfio/iommufd: Add framework code to support getting dirty bitmap before unmap

Quoted 83 lines

-----Original Message-----
From: Cédric Le Goater <[email protected]>
Subject: Re: [PATCH 1/5] vfio/iommufd: Add framework code to support getting dirty bitmap before unmap

Quoted 14 lines · Zhenzhong

Hi Cedric,

Quoted 12 lines

-----Original Message-----
From: Cédric Le Goater <[email protected]>
Subject: Re: [PATCH 1/5] vfio/iommufd: Add framework code to support getting dirty bitmap before unmap

Hello Zhenzhong

Quoted 4 lines · Zhenzhong Duan

Currently we support device and iommu dirty tracking, device dirty tracking is preferred.

Add the framework code in iommufd_cdev_unmap_one() to choose

either

Quoted 7 lines
Quoted 5 lines
Quoted 1 line

device or iommu dirty tracking, just like vfio_legacy_dma_unmap_one().

I wonder if commit 567d7d3e6be5 ("vfio/common: Work around kernel overflow bug in DMA unmap") could be removed now to make the code common to both VFIO IOMMU Type1 and IOMMUFD backends.

I am not clear if there is other reason to keep the workaround, but the

original

Quoted 10 lines

kernel issue had been fixed with below commit:

commit 58fec830fc19208354895d9832785505046d6c01

Author: Alex Williamson <[email protected]>
Date:   Mon Jan 7 22:13:22 2019 -0700

vfio/type1: Fix unmap overflow off-by-one

The below referenced commit adds a test for integer overflow, but in doing so prevents the unmap ioctl from ever including the last page

of

Quoted 1 line

the address space. Subtract one to compare to the last address of

the

Quoted 14 lines

unmap to avoid the overflow and wrap-around.

Quoted 1 line
Quoted 3 lines
Quoted 1 line

I asked Alex and Peter in another thread.

Just curious on the answer, may I ask which thread?

According to Alex, the QEMU workaround can be removed :

https://lore.kernel.org/qemu-devel/20250919102447.748e17fe.alex.williams
[email protected]/

Quoted 1 line

btw: I just found unmapping in halves seems unnecessary as both backends

of kernel side support unmap_all now.

Quoted 6 lines
     if (unmap_all) {
         /* The unmap ioctl doesn't accept a full 64-bit span. */
         Int128 llsize = int128_rshift(int128_2_64(), 1);

         ret = vfio_legacy_dma_unmap_one(bcontainer, 0,

int128_get64(llsize),

Quoted 4 lines
                                         iotlb);

         if (ret == 0) {
             ret = vfio_legacy_dma_unmap_one(bcontainer,

int128_get64(llsize),

Quoted 1 line
                                             int128_get64(llsize),

iotlb);

Quoted 8 lines
Quoted 4 lines
         }

     } else {
         ret = vfio_legacy_dma_unmap_one(bcontainer, iova, size,

iotlb);

Quoted 1 line
     }

Good. So we can simply both backends it seems.

Will you handle them or not? I mean the workaround removing and unmapping_all optimization.

Thanks
Zhenzhong

#6 lore raw

Re: [PATCH 1/5] vfio/iommufd: Add framework code to support getting dirty bitmap before unmap

Quoted 83 lines · Zhenzhong
Quoted 83 lines

-----Original Message-----
From: Cédric Le Goater <[email protected]>
Subject: Re: [PATCH 1/5] vfio/iommufd: Add framework code to support getting dirty bitmap before unmap

Quoted 14 lines · Zhenzhong

Hi Cedric,

Quoted 12 lines

-----Original Message-----
From: Cédric Le Goater <[email protected]>
Subject: Re: [PATCH 1/5] vfio/iommufd: Add framework code to support getting dirty bitmap before unmap

Hello Zhenzhong

Quoted 4 lines · Zhenzhong Duan

Currently we support device and iommu dirty tracking, device dirty tracking is preferred.

Add the framework code in iommufd_cdev_unmap_one() to choose

either

Quoted 7 lines
Quoted 5 lines
Quoted 1 line

device or iommu dirty tracking, just like vfio_legacy_dma_unmap_one().

I wonder if commit 567d7d3e6be5 ("vfio/common: Work around kernel overflow bug in DMA unmap") could be removed now to make the code common to both VFIO IOMMU Type1 and IOMMUFD backends.

I am not clear if there is other reason to keep the workaround, but the

original

Quoted 10 lines

kernel issue had been fixed with below commit:

commit 58fec830fc19208354895d9832785505046d6c01

Author: Alex Williamson <[email protected]>
Date:   Mon Jan 7 22:13:22 2019 -0700

vfio/type1: Fix unmap overflow off-by-one

The below referenced commit adds a test for integer overflow, but in doing so prevents the unmap ioctl from ever including the last page

of

Quoted 1 line

the address space. Subtract one to compare to the last address of

the

Quoted 14 lines

unmap to avoid the overflow and wrap-around.

Quoted 1 line
Quoted 3 lines
Quoted 1 line

I asked Alex and Peter in another thread.

Just curious on the answer, may I ask which thread?

According to Alex, the QEMU workaround can be removed :

https://lore.kernel.org/qemu-devel/20250919102447.748e17fe.alex.williams
[email protected]/

Quoted 1 line

btw: I just found unmapping in halves seems unnecessary as both backends

of kernel side support unmap_all now.

Quoted 6 lines
      if (unmap_all) {
          /* The unmap ioctl doesn't accept a full 64-bit span. */
          Int128 llsize = int128_rshift(int128_2_64(), 1);

          ret = vfio_legacy_dma_unmap_one(bcontainer, 0,

int128_get64(llsize),

Quoted 4 lines
                                          iotlb);

          if (ret == 0) {
              ret = vfio_legacy_dma_unmap_one(bcontainer,

int128_get64(llsize),

Quoted 1 line
                                              int128_get64(llsize),

iotlb);

Quoted 8 lines
Quoted 8 lines
Quoted 4 lines
          }

      } else {
          ret = vfio_legacy_dma_unmap_one(bcontainer, iova, size,

iotlb);

Quoted 1 line
      }

Good. So we can simply both backends it seems.

*ify

Quoted 2 lines

Will you handle them or not? I mean the workaround removing and unmapping_all optimization.

I can revert 567d7d3e6be5 ("vfio/common: Work around kernel overflow bug in DMA unmap") but, AFAICT, the "unmap DMAs in halves" method (see 1b296c3def4b) should be kept.

Thanks,

C.

#7 lore raw

RE: [PATCH 1/5] vfio/iommufd: Add framework code to support getting dirty bitmap before unmap

Quoted 111 lines

-----Original Message-----
From: Cédric Le Goater <[email protected]>
Subject: Re: [PATCH 1/5] vfio/iommufd: Add framework code to support getting dirty bitmap before unmap

Quoted 17 lines · Zhenzhong
Quoted 17 lines

-----Original Message-----
From: Cédric Le Goater <[email protected]>
Subject: Re: [PATCH 1/5] vfio/iommufd: Add framework code to support getting dirty bitmap before unmap

Quoted 9 lines · Zhenzhong

Hi Cedric,

Quoted 7 lines

-----Original Message-----
From: Cédric Le Goater <[email protected]>
Subject: Re: [PATCH 1/5] vfio/iommufd: Add framework code to support getting dirty bitmap before unmap

Hello Zhenzhong

Quoted 7 lines
Quoted 7 lines
Quoted 5 lines
Quoted 5 lines · Zhenzhong Duan
Quoted 4 lines

Currently we support device and iommu dirty tracking, device dirty tracking is preferred.

Add the framework code in iommufd_cdev_unmap_one() to choose

either

Quoted 1 line
Quoted 1 line
Quoted 1 line

device or iommu dirty tracking, just like

vfio_legacy_dma_unmap_one().

Quoted 9 lines
Quoted 9 lines
Quoted 6 lines
Quoted 4 lines

I wonder if commit 567d7d3e6be5 ("vfio/common: Work around kernel overflow bug in DMA unmap") could be removed now to make the code common to both VFIO IOMMU Type1 and IOMMUFD backends.

I am not clear if there is other reason to keep the workaround, but the

original

Quoted 2 lines

kernel issue had been fixed with below commit:

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

commit 58fec830fc19208354895d9832785505046d6c01

Author: Alex Williamson <[email protected]>
Date:   Mon Jan 7 22:13:22 2019 -0700

vfio/type1: Fix unmap overflow off-by-one

The below referenced commit adds a test for integer overflow,

but in

Quoted 1 line
Quoted 1 line
Quoted 1 line

doing so prevents the unmap ioctl from ever including the last

page

Quoted 2 lines
Quoted 2 lines

of

Quoted 1 line

the address space. Subtract one to compare to the last address

of

Quoted 4 lines
Quoted 4 lines

the

Quoted 3 lines

unmap to avoid the overflow and wrap-around.

Fixes 71a7d3d78e3c ("vfio/type1: silence integer overflow

warning")

Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
Quoted 1 line
Quoted 7 lines
Quoted 7 lines
Quoted 3 lines
Quoted 1 line

I asked Alex and Peter in another thread.

Just curious on the answer, may I ask which thread?

According to Alex, the QEMU workaround can be removed :

https://lore.kernel.org/qemu-devel/20250919102447.748e17fe.alex.williams

Quoted 3 lines
Quoted 3 lines

[email protected]/

Quoted 1 line

btw: I just found unmapping in halves seems unnecessary as both

backends

Quoted 6 lines
Quoted 6 lines

of kernel side support unmap_all now.

Quoted 5 lines
      if (unmap_all) {
          /* The unmap ioctl doesn't accept a full 64-bit span. */
          Int128 llsize = int128_rshift(int128_2_64(), 1);
Quoted 8 lines
Quoted 8 lines
Quoted 1 line

ret = vfio_legacy_dma_unmap_one(bcontainer, 0,

int128_get64(llsize),

Quoted 4 lines
                                          iotlb);

          if (ret == 0) {
              ret = vfio_legacy_dma_unmap_one(bcontainer,

int128_get64(llsize),

Quoted 1 line

int128_get64(llsize),

Quoted 9 lines
Quoted 9 lines

iotlb);

Quoted 4 lines
          }

      } else {
          ret = vfio_legacy_dma_unmap_one(bcontainer, iova, size,

iotlb);

Quoted 1 line
      }

Good. So we can simply both backends it seems.

*ify

Quoted 1 line
Quoted 6 lines
Quoted 1 line

Will you handle them or not? I mean the workaround removing and

unmapping_all optimization.

I can revert 567d7d3e6be5 ("vfio/common: Work around kernel overflow bug in DMA unmap") but, AFAICT, the "unmap DMAs in halves" method (see 1b296c3def4b) should be kept.

OK, let me take the unmap_all part. For example, for iommufd, it can be simplified to:

hw/vfio/iommufd.c +2 -13
@@ -67,19 +67,8 @@ static int iommufd_cdev_unmap(const VFIOContainerBase *bcontainer,
 
     /* unmap in halves */
     if (unmap_all) {
-        Int128 llsize = int128_rshift(int128_2_64(), 1);
-        int ret;
-
-        ret = iommufd_backend_unmap_dma(container->be, container->ioas_id,
-                                        0, int128_get64(llsize));
-
-        if (ret == 0) {
-            ret = iommufd_backend_unmap_dma(container->be, container->ioas_id,
-                                            int128_get64(llsize),
-                                            int128_get64(llsize));
-        }
-
-        return ret;
+        assert(!iova && !size);
+        size = UINT64_MAX;
     }
 
     /* TODO: Handle dma_unmap_bitmap with iotlb args (migration) */

Thanks
Zhenzhong

#8 lore raw

PATCH 2/5 vfio/iommufd: Query dirty bitmap before DMA unmap

In reply to Zhenzhong Duan

When a 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 | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
hw/vfio/iommufd.c +7 -1
index b5d6e54c45..0057488ce9 100644
@@ -73,7 +73,13 @@ static int iommufd_cdev_unmap_one(const VFIOContainerBase *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 */
+            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 */
             return iommufd_backend_unmap_dma(be, ioas_id, iova, size);
         }
 
Signature · 1 line
2.47.1
#9 lore raw

PATCH 3/5 vfio/iommufd: Add IOMMU_HWPT_GET_DIRTY_BITMAP_NO_CLEAR flag support

In reply to Zhenzhong Duan

Pass 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.

Add a new element dirty_tracking_flags in VFIOIOMMUFDContainer to be used for the flags of iommufd dirty tracking. Currently it is set to either IOMMU_HWPT_GET_DIRTY_BITMAP_NO_CLEAR or 0 based on the scenario.

---
 hw/vfio/vfio-iommufd.h   | 1 +
 include/system/iommufd.h | 2 +-
 backends/iommufd.c       | 5 +++--
 hw/vfio/iommufd.c        | 6 +++++-
 backends/trace-events    | 2 +-
 5 files changed, 11 insertions(+), 5 deletions(-)
hw/vfio/vfio-iommufd.h +1 -0
index 07ea0f4304..e0af241c75 100644
@@ -26,6 +26,7 @@ typedef struct VFIOIOMMUFDContainer {
     VFIOContainerBase bcontainer;
     IOMMUFDBackend *be;
     uint32_t ioas_id;
+    uint64_t dirty_tracking_flags;
     QLIST_HEAD(, VFIOIOASHwpt) hwpt_list;
 } VFIOIOMMUFDContainer;
 
include/system/iommufd.h +1 -1
index c9c72ffc45..63898e7b0d 100644
@@ -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
index 2a33c7ab0b..3c4f6157e2 100644
@@ -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 +5 -1
index 0057488ce9..c897aa6b17 100644
@@ -62,7 +62,7 @@ static int iommufd_cdev_unmap_one(const VFIOContainerBase *bcontainer,
                                   hwaddr iova, ram_addr_t size,
                                   IOMMUTLBEntry *iotlb)
 {
-    const VFIOIOMMUFDContainer *container =
+    VFIOIOMMUFDContainer *container =
         container_of(bcontainer, VFIOIOMMUFDContainer, bcontainer);
     bool need_dirty_sync = false;
     Error *local_err = NULL;
@@ -73,9 +73,12 @@ static int iommufd_cdev_unmap_one(const VFIOContainerBase *bcontainer,
     if (iotlb && vfio_container_dirty_tracking_is_started(bcontainer)) {
         if (!vfio_container_devices_dirty_tracking_is_supported(bcontainer) &&
             bcontainer->dirty_pages_supported) {
+            container->dirty_tracking_flags =
+                                      IOMMU_HWPT_GET_DIRTY_BITMAP_NO_CLEAR;
             ret = vfio_container_query_dirty_bitmap(bcontainer, iova, size,
                                                     iotlb->translated_addr,
                                                     &local_err);
+            container->dirty_tracking_flags = 0;
             if (ret) {
                 error_report_err(local_err);
             }
@@ -248,6 +251,7 @@ static int iommufd_query_dirty_bitmap(const VFIOContainerBase *bcontainer,
         if (!iommufd_backend_get_dirty_bitmap(container->be, hwpt->hwpt_id,
                                               iova, size, page_size,
                                               (uint64_t *)vbmap->bitmap,
+                                              container->dirty_tracking_flags,
                                               errp)) {
             return -EINVAL;
         }
backends/trace-events +1 -1
index 56132d3fd2..e1992ba12f 100644
@@ -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
#10 lore raw

Re: [PATCH 3/5] vfio/iommufd: Add IOMMU_HWPT_GET_DIRTY_BITMAP_NO_CLEAR flag support

Quoted 88 lines · Zhenzhong Duan

Pass 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.

Add a new element dirty_tracking_flags in VFIOIOMMUFDContainer to be used for the flags of iommufd dirty tracking. Currently it is set to either IOMMU_HWPT_GET_DIRTY_BITMAP_NO_CLEAR or 0 based on the scenario.

---
 hw/vfio/vfio-iommufd.h   | 1 +
 include/system/iommufd.h | 2 +-
 backends/iommufd.c       | 5 +++--
 hw/vfio/iommufd.c        | 6 +++++-
 backends/trace-events    | 2 +-
 5 files changed, 11 insertions(+), 5 deletions(-)
hw/vfio/vfio-iommufd.h +1 -0
index 07ea0f4304..e0af241c75 100644
@@ -26,6 +26,7 @@ typedef struct VFIOIOMMUFDContainer {
     VFIOContainerBase bcontainer;
     IOMMUFDBackend *be;
     uint32_t ioas_id;
+    uint64_t dirty_tracking_flags;
     QLIST_HEAD(, VFIOIOASHwpt) hwpt_list;
 } VFIOIOMMUFDContainer;
 
include/system/iommufd.h +1 -1
index c9c72ffc45..63898e7b0d 100644
@@ -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
index 2a33c7ab0b..3c4f6157e2 100644
@@ -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 +1 -1
index 0057488ce9..c897aa6b17 100644
@@ -62,7 +62,7 @@ static int iommufd_cdev_unmap_one(const VFIOContainerBase *bcontainer,
                                   hwaddr iova, ram_addr_t size,
                                   IOMMUTLBEntry *iotlb)
 {
-    const VFIOIOMMUFDContainer *container =
+    VFIOIOMMUFDContainer *container =
         container_of(bcontainer, VFIOIOMMUFDContainer, bcontainer);
     bool need_dirty_sync = false;
     Error *local_err = NULL;

hw/vfio/iommufd.c static int iommufd_cdev_unmap_one(const VFIOContainerBase *bcontainer, · line 81

Quoted 9 lines
hw/vfio/iommufd.c +3 -0
@@ -73,9 +73,12 @@ static int iommufd_cdev_unmap_one(const VFIOContainerBase *bcontainer,
     if (iotlb && vfio_container_dirty_tracking_is_started(bcontainer)) {
         if (!vfio_container_devices_dirty_tracking_is_supported(bcontainer) &&
             bcontainer->dirty_pages_supported) {
+            container->dirty_tracking_flags =
+                                      IOMMU_HWPT_GET_DIRTY_BITMAP_NO_CLEAR;
             ret = vfio_container_query_dirty_bitmap(bcontainer, iova, size,
                                                     iotlb->translated_addr,
                                                     &local_err);
+            container->dirty_tracking_flags = 0;

Why not changing vfio_container_query_dirty_bitmap to pass a flags too, like the original patches? This is a little unnecssary odd style to pass a flag via container structure rather and then clearing.

Part of the reason the original series had a VFIO_GET_DIRTY_NO_FLUSH for generic container abstraction was to not mix IOMMUFD UAPI specifics into base container API. Then in getting a VFIO_GET_DIRTY_NO_FLUSH, then type1 backend could just ignore the flag, while IOMMUFD translates it to IOMMU_HWPT_GET_DIRTY_BITMAP_NO_CLEAR

Quoted 22 lines
hw/vfio/iommufd.c +1 -0
             if (ret) {
                 error_report_err(local_err);
             }
@@ -248,6 +251,7 @@ static int iommufd_query_dirty_bitmap(const VFIOContainerBase *bcontainer,
         if (!iommufd_backend_get_dirty_bitmap(container->be, hwpt->hwpt_id,
                                               iova, size, page_size,
                                               (uint64_t *)vbmap->bitmap,
+                                              container->dirty_tracking_flags,
                                               errp)) {
             return -EINVAL;
         }
backends/trace-events +1 -1
index 56132d3fd2..e1992ba12f 100644
@@ -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)"
#11 lore raw

RE: [PATCH 3/5] vfio/iommufd: Add IOMMU_HWPT_GET_DIRTY_BITMAP_NO_CLEAR flag support

Hi Joao,

Quoted 128 lines

-----Original Message-----
From: Joao Martins <[email protected]>
Subject: Re: [PATCH 3/5] vfio/iommufd: Add
IOMMU_HWPT_GET_DIRTY_BITMAP_NO_CLEAR flag support

Quoted 1 line · Zhenzhong Duan

Pass IOMMU_HWPT_GET_DIRTY_BITMAP_NO_CLEAR when doing the last

dirty

include/system/iommufd.h bool

Quoted 38 lines

bitmap query right before unmap, no PTEs flushes. This accelerates the query without issue because unmap will tear down the mapping anyway.

Add a new element dirty_tracking_flags in VFIOIOMMUFDContainer to be used for the flags of iommufd dirty tracking. Currently it is set to either IOMMU_HWPT_GET_DIRTY_BITMAP_NO_CLEAR or 0 based on the scenario.

---
 hw/vfio/vfio-iommufd.h   | 1 +
 include/system/iommufd.h | 2 +-
 backends/iommufd.c       | 5 +++--
 hw/vfio/iommufd.c        | 6 +++++-
 backends/trace-events    | 2 +-
 5 files changed, 11 insertions(+), 5 deletions(-)
hw/vfio/vfio-iommufd.h +1 -0
index 07ea0f4304..e0af241c75 100644
@@ -26,6 +26,7 @@ typedef struct VFIOIOMMUFDContainer {
     VFIOContainerBase bcontainer;
     IOMMUFDBackend *be;
     uint32_t ioas_id;
+    uint64_t dirty_tracking_flags;
     QLIST_HEAD(, VFIOIOASHwpt) hwpt_list;
 } VFIOIOMMUFDContainer;
 
include/system/iommufd.h +0 -0
index c9c72ffc45..63898e7b0d 100644
@@ -64,7 +64,7 @@ bool

iommufd_backend_set_dirty_tracking(IOMMUFDBackend *be, uint32_t hwpt_id,

include/system/iommufd.h bool · line 64

Quoted 1 line
include/system/iommufd.h +0 -0
 bool iommufd_backend_get_dirty_bitmap(IOMMUFDBackend *be,

uint32_t hwpt_id,

include/system/iommufd.h bool · line 65

Quoted 1 line
include/system/iommufd.h +0 -0
                                       uint64_t iova, ram_addr_t

size,

include/system/iommufd.h bool · line 66

Quoted 1 line
include/system/iommufd.h +0 -0
                                       uint64_t page_size,

uint64_t *data,

include/system/iommufd.h bool · line 67

Quoted 2 lines
include/system/iommufd.h +1 -1
-                                      Error **errp);
+                                      uint64_t flags, Error

**errp);

include/system/iommufd.h bool · line 68

Quoted 1 line
include/system/iommufd.h +0 -0
 bool iommufd_backend_invalidate_cache(IOMMUFDBackend *be,

uint32_t id,

include/system/iommufd.h bool · line 69

Quoted 1 line
include/system/iommufd.h +0 -0
                                       uint32_t data_type,

uint32_t entry_len,

include/system/iommufd.h bool · line 70

Quoted 1 line
include/system/iommufd.h +0 -0
                                       uint32_t *entry_num, void

*data,

backends/iommufd.c bool

Quoted 5 lines
backends/iommufd.c +0 -0
index 2a33c7ab0b..3c4f6157e2 100644
@@ -361,7 +361,7 @@ bool

iommufd_backend_get_dirty_bitmap(IOMMUFDBackend *be,

backends/iommufd.c bool · line 362

Quoted 2 lines
backends/iommufd.c +0 -0
                                       uint32_t hwpt_id,
                                       uint64_t iova, ram_addr_t

size,

backends/iommufd.c bool · line 363

Quoted 1 line
backends/iommufd.c +0 -0
                                       uint64_t page_size,

uint64_t *data,

backends/iommufd.c bool

Quoted 6 lines
backends/iommufd.c +1 -1
-                                      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,

backends/iommufd.c bool iommufd_backend_get_dirty_bitmap(IOMMUFDBackend *be,

Quoted 7 lines
backends/iommufd.c +1 -0
         .length = size,
         .page_size = page_size,
         .data = (uintptr_t)data,
+        .flags = flags,
     };
 
     ret = ioctl(be->fd, IOMMU_HWPT_GET_DIRTY_BITMAP,

&get_dirty_bitmap);

backends/iommufd.c bool · line 378

Quoted 1 line
backends/iommufd.c +0 -0
     trace_iommufd_backend_get_dirty_bitmap(be->fd, hwpt_id, iova,

size,

backends/iommufd.c bool · line 378

Quoted 1 line
backends/iommufd.c +0 -1
-                                           page_size, ret ? errno :

0);

backends/iommufd.c bool · line 379

Quoted 1 line
backends/iommufd.c +1 -0
+                                           flags, page_size, ret ?

errno : 0);

backends/iommufd.c bool iommufd_backend_get_dirty_bitmap(IOMMUFDBackend *be,

Quoted 3 lines
backends/iommufd.c +0 -0
     if (ret) {
         error_setg_errno(errp, errno,
                          "IOMMU_HWPT_GET_DIRTY_BITMAP

(iova: 0x%"HWADDR_PRIx

hw/vfio/iommufd.c static int iommufd_cdev_unmap_one(const

Quoted 5 lines
hw/vfio/iommufd.c +0 -0
index 0057488ce9..c897aa6b17 100644
@@ -62,7 +62,7 @@ static int iommufd_cdev_unmap_one(const

VFIOContainerBase *bcontainer,

hw/vfio/iommufd.c static int iommufd_cdev_unmap_one(const VFIOContainerBase *bcontainer,

Quoted 6 lines
hw/vfio/iommufd.c +1 -1
                                   hwaddr iova, ram_addr_t size,
                                   IOMMUTLBEntry *iotlb)
 {
-    const VFIOIOMMUFDContainer *container =
+    VFIOIOMMUFDContainer *container =
         container_of(bcontainer, VFIOIOMMUFDContainer,

bcontainer);

hw/vfio/iommufd.c static int iommufd_cdev_unmap_one(const

Quoted 3 lines
hw/vfio/iommufd.c +0 -0
     bool need_dirty_sync = false;
     Error *local_err = NULL;
@@ -73,9 +73,12 @@ static int iommufd_cdev_unmap_one(const

VFIOContainerBase *bcontainer,

hw/vfio/iommufd.c static int iommufd_cdev_unmap_one(const · line 74

Quoted 2 lines
hw/vfio/iommufd.c +0 -0
     if (iotlb && vfio_container_dirty_tracking_is_started(bcontainer)) {
         if

(!vfio_container_devices_dirty_tracking_is_supported(bcontainer) &&

hw/vfio/iommufd.c static int iommufd_cdev_unmap_one(const VFIOContainerBase *bcontainer,

Quoted 3 lines
hw/vfio/iommufd.c +2 -0
             bcontainer->dirty_pages_supported) {
+            container->dirty_tracking_flags =
+

IOMMU_HWPT_GET_DIRTY_BITMAP_NO_CLEAR;

hw/vfio/iommufd.c static int iommufd_cdev_unmap_one(const · line 78

Quoted 1 line
hw/vfio/iommufd.c +0 -0
             ret = vfio_container_query_dirty_bitmap(bcontainer, iova,

size,

Quoted 1 line

iotlb->translated_addr,

Quoted 1 line
Quoted 7 lines

&local_err);

Quoted 1 line

+ container->dirty_tracking_flags = 0;

Why not changing vfio_container_query_dirty_bitmap to pass a flags too, like the
original patches? This is a little unnecssary odd style to pass a flag via container structure rather and then clearing.

Just want to be simpler, original patch introduced a new parameter to almost all variants of *_query_dirty_bitmap() while the flags parameter is only used by IOMMUFD backend when doing unmap_bitmap. Currently we already have three backends, legacy VFIO, IOMMUFD and VFIO-user, only IOMMUFD need the flag.

I take container->dirty_tracking_flags as a notification mechanism, so set it before vfio_container_query_dirty_bitmap() and clear it thereafter. Maybe clearing it in iommufd_query_dirty_bitmap() is easier to be acceptable?

Quoted 9 lines

Part of the reason the original series had a VFIO_GET_DIRTY_NO_FLUSH for generic
container abstraction was to not mix IOMMUFD UAPI specifics into base container
API. Then in getting a VFIO_GET_DIRTY_NO_FLUSH, then type1 backend could just
ignore the flag, while IOMMUFD translates it to IOMMU_HWPT_GET_DIRTY_BITMAP_NO_CLEAR

I did port original patch https://github.com/yiliu1765/qemu/commit/99f83595d79d2e4170c9e456cf1a7b9521bd4f80
But it looks complex to have 'flags' parameter everywhere.

Thanks
Zhenzhong

#12 lore raw

Re: [PATCH 3/5] vfio/iommufd: Add IOMMU_HWPT_GET_DIRTY_BITMAP_NO_CLEAR flag support

Quoted 157 lines · Zhenzhong

Hi Joao,

Quoted 128 lines

-----Original Message-----
From: Joao Martins <[email protected]>
Subject: Re: [PATCH 3/5] vfio/iommufd: Add
IOMMU_HWPT_GET_DIRTY_BITMAP_NO_CLEAR flag support

Quoted 1 line · Zhenzhong Duan

Pass IOMMU_HWPT_GET_DIRTY_BITMAP_NO_CLEAR when doing the last

dirty

include/system/iommufd.h bool

Quoted 38 lines

bitmap query right before unmap, no PTEs flushes. This accelerates the query without issue because unmap will tear down the mapping anyway.

Add a new element dirty_tracking_flags in VFIOIOMMUFDContainer to be used for the flags of iommufd dirty tracking. Currently it is set to either IOMMU_HWPT_GET_DIRTY_BITMAP_NO_CLEAR or 0 based on the scenario.

---
  hw/vfio/vfio-iommufd.h   | 1 +
  include/system/iommufd.h | 2 +-
  backends/iommufd.c       | 5 +++--
  hw/vfio/iommufd.c        | 6 +++++-
  backends/trace-events    | 2 +-
  5 files changed, 11 insertions(+), 5 deletions(-)
hw/vfio/vfio-iommufd.h +1 -0
index 07ea0f4304..e0af241c75 100644
@@ -26,6 +26,7 @@ typedef struct VFIOIOMMUFDContainer {
      VFIOContainerBase bcontainer;
      IOMMUFDBackend *be;
      uint32_t ioas_id;
+    uint64_t dirty_tracking_flags;
      QLIST_HEAD(, VFIOIOASHwpt) hwpt_list;
  } VFIOIOMMUFDContainer;
 
include/system/iommufd.h +0 -0
index c9c72ffc45..63898e7b0d 100644
@@ -64,7 +64,7 @@ bool

iommufd_backend_set_dirty_tracking(IOMMUFDBackend *be, uint32_t hwpt_id,

include/system/iommufd.h bool · line 64

Quoted 1 line
include/system/iommufd.h +0 -0
  bool iommufd_backend_get_dirty_bitmap(IOMMUFDBackend *be,

uint32_t hwpt_id,

include/system/iommufd.h bool · line 65

Quoted 1 line
include/system/iommufd.h +0 -0
                                        uint64_t iova, ram_addr_t

size,

include/system/iommufd.h bool · line 66

Quoted 1 line
include/system/iommufd.h +0 -0
                                        uint64_t page_size,

uint64_t *data,

include/system/iommufd.h bool · line 67

Quoted 2 lines
include/system/iommufd.h +1 -1
-                                      Error **errp);
+                                      uint64_t flags, Error

**errp);

include/system/iommufd.h bool · line 68

Quoted 1 line
include/system/iommufd.h +0 -0
  bool iommufd_backend_invalidate_cache(IOMMUFDBackend *be,

uint32_t id,

include/system/iommufd.h bool · line 69

Quoted 1 line
include/system/iommufd.h +0 -0
                                        uint32_t data_type,

uint32_t entry_len,

include/system/iommufd.h bool · line 70

Quoted 1 line
include/system/iommufd.h +0 -0
                                        uint32_t *entry_num, void

*data,

backends/iommufd.c bool

Quoted 5 lines
backends/iommufd.c +0 -0
index 2a33c7ab0b..3c4f6157e2 100644
@@ -361,7 +361,7 @@ bool

iommufd_backend_get_dirty_bitmap(IOMMUFDBackend *be,

backends/iommufd.c bool · line 362

Quoted 2 lines
backends/iommufd.c +0 -0
                                        uint32_t hwpt_id,
                                        uint64_t iova, ram_addr_t

size,

backends/iommufd.c bool · line 363

Quoted 1 line
backends/iommufd.c +0 -0
                                        uint64_t page_size,

uint64_t *data,

backends/iommufd.c bool

Quoted 6 lines
backends/iommufd.c +1 -1
-                                      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,

backends/iommufd.c bool iommufd_backend_get_dirty_bitmap(IOMMUFDBackend *be,

Quoted 7 lines
backends/iommufd.c +1 -0
          .length = size,
          .page_size = page_size,
          .data = (uintptr_t)data,
+        .flags = flags,
      };
 
      ret = ioctl(be->fd, IOMMU_HWPT_GET_DIRTY_BITMAP,

&get_dirty_bitmap);

backends/iommufd.c bool · line 378

Quoted 1 line
backends/iommufd.c +0 -0
      trace_iommufd_backend_get_dirty_bitmap(be->fd, hwpt_id, iova,

size,

backends/iommufd.c bool · line 378

Quoted 1 line
backends/iommufd.c +0 -1
-                                           page_size, ret ? errno :

0);

backends/iommufd.c bool · line 379

Quoted 1 line
backends/iommufd.c +1 -0
+                                           flags, page_size, ret ?

errno : 0);

backends/iommufd.c bool iommufd_backend_get_dirty_bitmap(IOMMUFDBackend *be,

Quoted 3 lines
backends/iommufd.c +0 -0
      if (ret) {
          error_setg_errno(errp, errno,
                           "IOMMU_HWPT_GET_DIRTY_BITMAP

(iova: 0x%"HWADDR_PRIx

hw/vfio/iommufd.c static int iommufd_cdev_unmap_one(const

Quoted 5 lines
hw/vfio/iommufd.c +0 -0
index 0057488ce9..c897aa6b17 100644
@@ -62,7 +62,7 @@ static int iommufd_cdev_unmap_one(const

VFIOContainerBase *bcontainer,

hw/vfio/iommufd.c static int iommufd_cdev_unmap_one(const VFIOContainerBase *bcontainer,

Quoted 6 lines
hw/vfio/iommufd.c +1 -1
                                    hwaddr iova, ram_addr_t size,
                                    IOMMUTLBEntry *iotlb)
  {
-    const VFIOIOMMUFDContainer *container =
+    VFIOIOMMUFDContainer *container =
          container_of(bcontainer, VFIOIOMMUFDContainer,

bcontainer);

hw/vfio/iommufd.c static int iommufd_cdev_unmap_one(const

Quoted 3 lines
hw/vfio/iommufd.c +0 -0
      bool need_dirty_sync = false;
      Error *local_err = NULL;
@@ -73,9 +73,12 @@ static int iommufd_cdev_unmap_one(const

VFIOContainerBase *bcontainer,

hw/vfio/iommufd.c static int iommufd_cdev_unmap_one(const · line 74

Quoted 2 lines
hw/vfio/iommufd.c +0 -0
      if (iotlb && vfio_container_dirty_tracking_is_started(bcontainer)) {
          if

(!vfio_container_devices_dirty_tracking_is_supported(bcontainer) &&

hw/vfio/iommufd.c static int iommufd_cdev_unmap_one(const VFIOContainerBase *bcontainer,

Quoted 3 lines
hw/vfio/iommufd.c +2 -0
              bcontainer->dirty_pages_supported) {
+            container->dirty_tracking_flags =
+

IOMMU_HWPT_GET_DIRTY_BITMAP_NO_CLEAR;

hw/vfio/iommufd.c static int iommufd_cdev_unmap_one(const · line 78

Quoted 1 line
hw/vfio/iommufd.c +0 -0
              ret = vfio_container_query_dirty_bitmap(bcontainer, iova,

size,

Quoted 1 line

iotlb->translated_addr,

Quoted 1 line
Quoted 7 lines

&local_err);

Quoted 1 line

+ container->dirty_tracking_flags = 0;

Why not changing vfio_container_query_dirty_bitmap to pass a flags too, like the
original patches? This is a little unnecssary odd style to pass a flag via container structure rather and then clearing.

Just want to be simpler, original patch introduced a new parameter to almost all variants of *_query_dirty_bitmap() while the flags parameter is only used by IOMMUFD backend when doing unmap_bitmap. Currently we already have three backends, legacy VFIO, IOMMUFD and VFIO-user, only IOMMUFD need the flag.

I take container->dirty_tracking_flags as a notification mechanism, so set it before vfio_container_query_dirty_bitmap() and clear it thereafter. Maybe clearing it in iommufd_query_dirty_bitmap() is easier to be acceptable?

Quoted 9 lines

Part of the reason the original series had a VFIO_GET_DIRTY_NO_FLUSH for generic
container abstraction was to not mix IOMMUFD UAPI specifics into base container
API. Then in getting a VFIO_GET_DIRTY_NO_FLUSH, then type1 backend could just
ignore the flag, while IOMMUFD translates it to IOMMU_HWPT_GET_DIRTY_BITMAP_NO_CLEAR

Quoted 2 lines

I did port original patch https://github.com/yiliu1765/qemu/commit/99f83595d79d2e4170c9e456cf1a7b9521bd4f80
But it looks complex to have 'flags' parameter everywhere.

I think I would prefer like Joao to avoid caching information if possible but I haven't check closely the mess it would introduce in the code. Let me check.

Thanks,

C.

#13 lore raw

Re: [PATCH 3/5] vfio/iommufd: Add IOMMU_HWPT_GET_DIRTY_BITMAP_NO_CLEAR flag support

Quoted 159 lines · Cédric Le Goater
Quoted 158 lines · Zhenzhong

Hi Joao,

Quoted 128 lines

-----Original Message-----
From: Joao Martins <[email protected]>
Subject: Re: [PATCH 3/5] vfio/iommufd: Add
IOMMU_HWPT_GET_DIRTY_BITMAP_NO_CLEAR flag support

Quoted 1 line · Zhenzhong Duan

Pass IOMMU_HWPT_GET_DIRTY_BITMAP_NO_CLEAR when doing the last

dirty

include/system/iommufd.h bool

Quoted 38 lines

bitmap query right before unmap, no PTEs flushes. This accelerates the query without issue because unmap will tear down the mapping anyway.

Add a new element dirty_tracking_flags in VFIOIOMMUFDContainer to be used for the flags of iommufd dirty tracking. Currently it is set to either IOMMU_HWPT_GET_DIRTY_BITMAP_NO_CLEAR or 0 based on the scenario.

---
  hw/vfio/vfio-iommufd.h   | 1 +
  include/system/iommufd.h | 2 +-
  backends/iommufd.c       | 5 +++--
  hw/vfio/iommufd.c        | 6 +++++-
  backends/trace-events    | 2 +-

  5 files changed, 11 insertions(+), 5 deletions(-)
hw/vfio/vfio-iommufd.h +1 -0
index 07ea0f4304..e0af241c75 100644
@@ -26,6 +26,7 @@ typedef struct VFIOIOMMUFDContainer {
       VFIOContainerBase bcontainer;
       IOMMUFDBackend *be;
       uint32_t ioas_id;
+    uint64_t dirty_tracking_flags;
       QLIST_HEAD(, VFIOIOASHwpt) hwpt_list;
   } VFIOIOMMUFDContainer;
 
include/system/iommufd.h +0 -0
index c9c72ffc45..63898e7b0d 100644
@@ -64,7 +64,7 @@ bool

iommufd_backend_set_dirty_tracking(IOMMUFDBackend *be, uint32_t hwpt_id,

include/system/iommufd.h bool · line 64

Quoted 1 line
include/system/iommufd.h +0 -0
   bool iommufd_backend_get_dirty_bitmap(IOMMUFDBackend *be,

uint32_t hwpt_id,

Quoted 1 line

                                        uint64_t iova, ram_addr_t

size,

Quoted 1 line

                                        uint64_t page_size,

uint64_t *data,

Quoted 2 lines

-                                      Error **errp); +                                      uint64_t flags, Error

**errp);

Quoted 1 line

  bool iommufd_backend_invalidate_cache(IOMMUFDBackend *be,

uint32_t id,

Quoted 1 line

                                        uint32_t data_type,

uint32_t entry_len,

Quoted 1 line

                                        uint32_t *entry_num, void

*data,

backends/iommufd.c bool

Quoted 5 lines
backends/iommufd.c +0 -0
index 2a33c7ab0b..3c4f6157e2 100644
@@ -361,7 +361,7 @@ bool

iommufd_backend_get_dirty_bitmap(IOMMUFDBackend *be,

Quoted 2 lines
backends/iommufd.c +0 -0
                                         uint32_t hwpt_id,

                                        uint64_t iova, ram_addr_t

size,

Quoted 1 line

                                        uint64_t page_size,

uint64_t *data,

backends/iommufd.c bool

Quoted 6 lines

-                                      Error **errp)
+                                      uint64_t flags, Error **errp) {
      int ret;
      struct iommu_hwpt_get_dirty_bitmap get_dirty_bitmap = {

backends/iommufd.c +0 -0
@@ -371,11 +371,12 @@ bool

iommufd_backend_get_dirty_bitmap(IOMMUFDBackend *be,

backends/iommufd.c bool iommufd_backend_get_dirty_bitmap(IOMMUFDBackend *be,

Quoted 7 lines
backends/iommufd.c +1 -0
           .length = size,
           .page_size = page_size,
           .data = (uintptr_t)data,
+        .flags = flags,
       };
 
       ret = ioctl(be->fd, IOMMU_HWPT_GET_DIRTY_BITMAP,

&get_dirty_bitmap);

backends/iommufd.c bool · line 378

Quoted 1 line
backends/iommufd.c +0 -0
       trace_iommufd_backend_get_dirty_bitmap(be->fd, hwpt_id, iova,

size,

backends/iommufd.c bool · line 378

Quoted 1 line
backends/iommufd.c +0 -1
-                                           page_size, ret ? errno :

0);

backends/iommufd.c bool · line 379

Quoted 1 line
backends/iommufd.c +1 -0
+                                           flags, page_size, ret ?

errno : 0);

Quoted 3 lines
backends/iommufd.c +0 -0
       if (ret) {
           error_setg_errno(errp, errno,

                           "IOMMU_HWPT_GET_DIRTY_BITMAP

(iova: 0x%"HWADDR_PRIx

hw/vfio/iommufd.c static int iommufd_cdev_unmap_one(const

Quoted 5 lines
hw/vfio/iommufd.c +0 -0
index 0057488ce9..c897aa6b17 100644
@@ -62,7 +62,7 @@ static int iommufd_cdev_unmap_one(const

VFIOContainerBase *bcontainer,

hw/vfio/iommufd.c static int iommufd_cdev_unmap_one(const VFIOContainerBase *bcontainer,

Quoted 6 lines
hw/vfio/iommufd.c +1 -1
                                     hwaddr iova, ram_addr_t size,
                                     IOMMUTLBEntry *iotlb)
   {
-    const VFIOIOMMUFDContainer *container =
+    VFIOIOMMUFDContainer *container =
           container_of(bcontainer, VFIOIOMMUFDContainer,

bcontainer);

hw/vfio/iommufd.c static int iommufd_cdev_unmap_one(const

Quoted 3 lines
hw/vfio/iommufd.c +0 -0
       bool need_dirty_sync = false;
       Error *local_err = NULL;
@@ -73,9 +73,12 @@ static int iommufd_cdev_unmap_one(const

VFIOContainerBase *bcontainer,

Quoted 2 lines
hw/vfio/iommufd.c +0 -0
       if (iotlb && vfio_container_dirty_tracking_is_started(bcontainer)) {

          if

(!vfio_container_devices_dirty_tracking_is_supported(bcontainer) &&

Quoted 3 lines

              bcontainer->dirty_pages_supported) { +            container->dirty_tracking_flags = +

IOMMU_HWPT_GET_DIRTY_BITMAP_NO_CLEAR;

Quoted 1 line

              ret = vfio_container_query_dirty_bitmap(bcontainer, iova,

size,

Quoted 1 line

iotlb->translated_addr,

Quoted 1 line
Quoted 7 lines

&local_err);

Quoted 1 line

+            container->dirty_tracking_flags = 0;

Why not changing vfio_container_query_dirty_bitmap to pass a flags too, like the
original patches? This is a little unnecssary odd style to pass a flag via container structure rather and then clearing.

Just want to be simpler, original patch introduced a new parameter to almost all variants of *_query_dirty_bitmap() while the flags parameter is only used by IOMMUFD backend when doing unmap_bitmap. Currently we already have three backends, legacy VFIO, IOMMUFD and VFIO-user, only IOMMUFD need the flag.

I take container->dirty_tracking_flags as a notification mechanism, so set it before
vfio_container_query_dirty_bitmap() and clear it thereafter. Maybe clearing it in iommufd_query_dirty_bitmap() is easier to be acceptable?

Quoted 9 lines

Part of the reason the original series had a VFIO_GET_DIRTY_NO_FLUSH for generic
container abstraction was to not mix IOMMUFD UAPI specifics into base container
API. Then in getting a VFIO_GET_DIRTY_NO_FLUSH, then type1 backend could just
ignore the flag, while IOMMUFD translates it to IOMMU_HWPT_GET_DIRTY_BITMAP_NO_CLEAR

Quoted 7 lines
Quoted 3 lines

I did port original patch https://github.com/yiliu1765/qemu/
commit/99f83595d79d2e4170c9e456cf1a7b9521bd4f80 But it looks complex to have 'flags' parameter everywhere.

I think I would prefer like Joao to avoid caching information if possible but I haven't check closely the mess it would introduce in the code. Let me check.

My recollection was that it wasn't that much churn added as this series is already doing the most of the churn. But I am checking how the code would look like to properly respond to his suggestion on why he changing it towards structuref field.

#14 lore raw

Re: [PATCH 3/5] vfio/iommufd: Add IOMMU_HWPT_GET_DIRTY_BITMAP_NO_CLEAR flag support

Quoted 168 lines · Joao Martins
Quoted 159 lines · Cédric Le Goater
Quoted 158 lines · Zhenzhong

Hi Joao,

Quoted 128 lines

-----Original Message-----
From: Joao Martins <[email protected]>
Subject: Re: [PATCH 3/5] vfio/iommufd: Add
IOMMU_HWPT_GET_DIRTY_BITMAP_NO_CLEAR flag support

Quoted 1 line · Zhenzhong Duan

Pass IOMMU_HWPT_GET_DIRTY_BITMAP_NO_CLEAR when doing the last

dirty

include/system/iommufd.h bool

Quoted 38 lines

bitmap query right before unmap, no PTEs flushes. This accelerates the query without issue because unmap will tear down the mapping anyway.

Add a new element dirty_tracking_flags in VFIOIOMMUFDContainer to be used for the flags of iommufd dirty tracking. Currently it is set to either IOMMU_HWPT_GET_DIRTY_BITMAP_NO_CLEAR or 0 based on the scenario.

---
  hw/vfio/vfio-iommufd.h   | 1 +
  include/system/iommufd.h | 2 +-
  backends/iommufd.c       | 5 +++--
  hw/vfio/iommufd.c        | 6 +++++-
  backends/trace-events    | 2 +-

  5 files changed, 11 insertions(+), 5 deletions(-)
hw/vfio/vfio-iommufd.h +1 -0
index 07ea0f4304..e0af241c75 100644
@@ -26,6 +26,7 @@ typedef struct VFIOIOMMUFDContainer {
       VFIOContainerBase bcontainer;
       IOMMUFDBackend *be;
       uint32_t ioas_id;
+    uint64_t dirty_tracking_flags;
       QLIST_HEAD(, VFIOIOASHwpt) hwpt_list;
   } VFIOIOMMUFDContainer;
 
include/system/iommufd.h +0 -0
index c9c72ffc45..63898e7b0d 100644
@@ -64,7 +64,7 @@ bool

iommufd_backend_set_dirty_tracking(IOMMUFDBackend *be, uint32_t hwpt_id,

include/system/iommufd.h bool · line 64

Quoted 1 line
include/system/iommufd.h +0 -0
   bool iommufd_backend_get_dirty_bitmap(IOMMUFDBackend *be,

uint32_t hwpt_id,

Quoted 1 line

                                        uint64_t iova, ram_addr_t

size,

Quoted 1 line

                                        uint64_t page_size,

uint64_t *data,

Quoted 2 lines

-                                      Error **errp); +                                      uint64_t flags, Error

**errp);

Quoted 1 line

  bool iommufd_backend_invalidate_cache(IOMMUFDBackend *be,

uint32_t id,

Quoted 1 line

                                        uint32_t data_type,

uint32_t entry_len,

Quoted 1 line

                                        uint32_t *entry_num, void

*data,

backends/iommufd.c bool

Quoted 5 lines
backends/iommufd.c +0 -0
index 2a33c7ab0b..3c4f6157e2 100644
@@ -361,7 +361,7 @@ bool

iommufd_backend_get_dirty_bitmap(IOMMUFDBackend *be,

Quoted 2 lines
backends/iommufd.c +0 -0
                                         uint32_t hwpt_id,

                                        uint64_t iova, ram_addr_t

size,

Quoted 1 line

                                        uint64_t page_size,

uint64_t *data,

backends/iommufd.c bool

Quoted 6 lines

-                                      Error **errp)
+                                      uint64_t flags, Error **errp) {
      int ret;
      struct iommu_hwpt_get_dirty_bitmap get_dirty_bitmap = {

backends/iommufd.c +0 -0
@@ -371,11 +371,12 @@ bool

iommufd_backend_get_dirty_bitmap(IOMMUFDBackend *be,

backends/iommufd.c bool · line 377

Quoted 7 lines
backends/iommufd.c +1 -0
           .length = size,
           .page_size = page_size,
           .data = (uintptr_t)data,
+        .flags = flags,
       };
 
       ret = ioctl(be->fd, IOMMU_HWPT_GET_DIRTY_BITMAP,

&get_dirty_bitmap);

backends/iommufd.c bool · line 378

Quoted 1 line
backends/iommufd.c +0 -0
       trace_iommufd_backend_get_dirty_bitmap(be->fd, hwpt_id, iova,

size,

backends/iommufd.c bool · line 378

Quoted 1 line
backends/iommufd.c +0 -1
-                                           page_size, ret ? errno :

0);

backends/iommufd.c bool · line 379

Quoted 1 line
backends/iommufd.c +1 -0
+                                           flags, page_size, ret ?

errno : 0);

Quoted 3 lines
backends/iommufd.c +0 -0
       if (ret) {
           error_setg_errno(errp, errno,

                           "IOMMU_HWPT_GET_DIRTY_BITMAP

(iova: 0x%"HWADDR_PRIx

hw/vfio/iommufd.c static int iommufd_cdev_unmap_one(const

Quoted 5 lines
hw/vfio/iommufd.c +0 -0
index 0057488ce9..c897aa6b17 100644
@@ -62,7 +62,7 @@ static int iommufd_cdev_unmap_one(const

VFIOContainerBase *bcontainer,

hw/vfio/iommufd.c static int iommufd_cdev_unmap_one(const · line 66

Quoted 6 lines
hw/vfio/iommufd.c +1 -1
                                     hwaddr iova, ram_addr_t size,
                                     IOMMUTLBEntry *iotlb)
   {
-    const VFIOIOMMUFDContainer *container =
+    VFIOIOMMUFDContainer *container =
           container_of(bcontainer, VFIOIOMMUFDContainer,

bcontainer);

hw/vfio/iommufd.c static int iommufd_cdev_unmap_one(const

Quoted 3 lines
hw/vfio/iommufd.c +0 -0
       bool need_dirty_sync = false;
       Error *local_err = NULL;
@@ -73,9 +73,12 @@ static int iommufd_cdev_unmap_one(const

VFIOContainerBase *bcontainer,

Quoted 2 lines
hw/vfio/iommufd.c +0 -0
       if (iotlb && vfio_container_dirty_tracking_is_started(bcontainer)) {

          if

(!vfio_container_devices_dirty_tracking_is_supported(bcontainer) &&

Quoted 3 lines

              bcontainer->dirty_pages_supported) { +            container->dirty_tracking_flags = +

IOMMU_HWPT_GET_DIRTY_BITMAP_NO_CLEAR;

Quoted 1 line

              ret = vfio_container_query_dirty_bitmap(bcontainer, iova,

size,

Quoted 1 line

iotlb->translated_addr,

Quoted 1 line
Quoted 7 lines

&local_err);

Quoted 1 line

+            container->dirty_tracking_flags = 0;

Why not changing vfio_container_query_dirty_bitmap to pass a flags too, like the
original patches? This is a little unnecssary odd style to pass a flag via container structure rather and then clearing.

Just want to be simpler, original patch introduced a new parameter to almost all variants of *_query_dirty_bitmap() while the flags parameter is only used by IOMMUFD backend when doing unmap_bitmap. Currently we already have three backends, legacy VFIO, IOMMUFD and VFIO-user, only IOMMUFD need the flag.

I take container->dirty_tracking_flags as a notification mechanism, so set it before
vfio_container_query_dirty_bitmap() and clear it thereafter. Maybe clearing it in iommufd_query_dirty_bitmap() is easier to be acceptable?

Quoted 9 lines

Part of the reason the original series had a VFIO_GET_DIRTY_NO_FLUSH for generic
container abstraction was to not mix IOMMUFD UAPI specifics into base container
API. Then in getting a VFIO_GET_DIRTY_NO_FLUSH, then type1 backend could just
ignore the flag, while IOMMUFD translates it to IOMMU_HWPT_GET_DIRTY_BITMAP_NO_CLEAR

Quoted 7 lines
Quoted 3 lines

I did port original patch https://github.com/yiliu1765/qemu/
commit/99f83595d79d2e4170c9e456cf1a7b9521bd4f80 But it looks complex to have 'flags' parameter everywhere.

I think I would prefer like Joao to avoid caching information if possible but I haven't check closely the mess it would introduce in the code. Let me check.

Quoted 4 lines

My recollection was that it wasn't that much churn added as this series is already doing the most of the churn. But I am checking how the code would look like to properly respond to his suggestion on why he changing it towards structuref field.

The churn should be similar to this patch:

https://github.com/jpemartins/qemu/commit/5e1f11075299a5fa9564a26788dc9cc1717e297c

It's mostly an interface parameter addition of flags.
But there's now a few more callsites and I suppose that's the extra churn. But I don't think vfio-user would need to change. See snip below.

I've pushed this here https://github.com/jpemartins/qemu/commits/relax-viommu-lm but showing the series wide changes for discussion. The thing I didn't do was have an intermediate 'backend agnostic' flag thingie like the original one
(https://github.com/jpemartins/qemu/commit/1ef8abc896ae69be8896a68705fe4a87204709e0) with VFIO_GET_DIRTY_NO_FLUSH

hw/vfio/container-base.c +7 -8
index 56304978e1e8..2fe08e010405 100644
@@ -211,17 +211,16 @@ static int vfio_device_dma_logging_report(VFIODevice *vbasedev, hwaddr iova,
 }
 
 static int vfio_container_iommu_query_dirty_bitmap(const VFIOContainerBase *bcontainer,
-                   VFIOBitmap *vbmap, hwaddr iova, hwaddr size, Error **errp)
+                   VFIOBitmap *vbmap, hwaddr iova, hwaddr size, uint64_t flags, 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);
+    return vioc->query_dirty_bitmap(bcontainer, vbmap, iova, size, flags, errp);
 }
 
 static int vfio_container_devices_query_dirty_bitmap(const VFIOContainerBase *bcontainer,
-                 VFIOBitmap *vbmap, hwaddr iova, hwaddr size, Error **errp)
+                 VFIOBitmap *vbmap, hwaddr iova, hwaddr size, uint64_t flags, Error **errp)
 {
     VFIODevice *vbasedev;
     int ret;
@@ -243,7 +242,7 @@ static int vfio_container_devices_query_dirty_bitmap(const VFIOContainerBase *bc
 }
 
 int vfio_container_query_dirty_bitmap(const VFIOContainerBase *bcontainer, uint64_t iova,
-                          uint64_t size, ram_addr_t ram_addr, Error **errp)
+                          uint64_t size, uint64_t flags, ram_addr_t ram_addr, Error **errp)
 {
     bool all_device_dirty_tracking =
         vfio_container_devices_dirty_tracking_is_supported(bcontainer);
@@ -267,10 +266,10 @@ int vfio_container_query_dirty_bitmap(const VFIOContainerBase *bcontainer, uint6
 
     if (all_device_dirty_tracking) {
         ret = vfio_container_devices_query_dirty_bitmap(bcontainer, &vbmap, iova, size,
-                                                        errp);
+                                                        flags, errp);
     } else {
         ret = vfio_container_iommu_query_dirty_bitmap(bcontainer, &vbmap, iova, size,
-                                                     errp);
+                                                      flags, errp);
     }
 
     if (ret) {
@@ -280,7 +279,7 @@ int vfio_container_query_dirty_bitmap(const VFIOContainerBase *bcontainer, uint6
     dirty_pages = cpu_physical_memory_set_dirty_lebitmap(vbmap.bitmap, ram_addr,
                                                          vbmap.pages);
 
-    trace_vfio_container_query_dirty_bitmap(iova, size, vbmap.size, ram_addr,
+    trace_vfio_container_query_dirty_bitmap(iova, size, flags, vbmap.size, ram_addr,
                                             dirty_pages);
 out:
     g_free(vbmap.bitmap);
hw/vfio/container.c +3 -2
index 030c6d3f89cf..86c4d06298f7 100644
@@ -169,7 +169,7 @@ static int vfio_legacy_dma_unmap_one(const VFIOContainerBase *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) {
             error_report_err(local_err);
@@ -267,7 +267,8 @@ vfio_legacy_set_dirty_page_tracking(const VFIOContainerBase *bcontainer,
 }
 
 static int vfio_legacy_query_dirty_bitmap(const VFIOContainerBase *bcontainer,
-                      VFIOBitmap *vbmap, hwaddr iova, hwaddr size, Error **errp)
+                      VFIOBitmap *vbmap, hwaddr iova, hwaddr size, uint64_t flags,
+                      Error **errp)
 {
     const VFIOContainer *container = VFIO_IOMMU_LEGACY(bcontainer);
     struct vfio_iommu_type1_dirty_bitmap *dbitmap;
hw/vfio/iommufd.c +4 -7
index b82597de9116..c1c8755c6d7c 100644
@@ -73,12 +73,10 @@ static int iommufd_cdev_unmap_one(const VFIOContainerBase *bcontainer,
     if (iotlb && vfio_container_dirty_tracking_is_started(bcontainer)) {
         if (!vfio_container_devices_dirty_tracking_is_supported(bcontainer) &&
             bcontainer->dirty_pages_supported) {
-            container->dirty_tracking_flags =
-                                      IOMMU_HWPT_GET_DIRTY_BITMAP_NO_CLEAR;
             ret = vfio_container_query_dirty_bitmap(bcontainer, iova, size,
+                                                    IOMMU_HWPT_GET_DIRTY_BITMAP_NO_CLEAR,
                                                     iotlb->translated_addr,
                                                     &local_err);
-            container->dirty_tracking_flags = 0;
             if (ret) {
                 error_report_err(local_err);
             }
@@ -95,7 +93,7 @@ static int iommufd_cdev_unmap_one(const VFIOContainerBase *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) {
@@ -235,7 +233,7 @@ err:
 
 static int iommufd_query_dirty_bitmap(const VFIOContainerBase *bcontainer,
                                       VFIOBitmap *vbmap, hwaddr iova,
-                                      hwaddr size, Error **errp)
+                                      hwaddr size, uint64_t flags, Error **errp)
 {
     VFIOIOMMUFDContainer *container = container_of(bcontainer,
                                                    VFIOIOMMUFDContainer,
@@ -251,8 +249,7 @@ static int iommufd_query_dirty_bitmap(const VFIOContainerBase *bcontainer,
         if (!iommufd_backend_get_dirty_bitmap(container->be, hwpt->hwpt_id,
                                               iova, size, page_size,
                                               (uint64_t *)vbmap->bitmap,
-                                              container->dirty_tracking_flags,
-                                              errp)) {
+                                              flags, errp)) {
             return -EINVAL;
         }
     }
hw/vfio/listener.c +3 -3
index e09383316598..27cbb45282ae 100644
@@ -1082,7 +1082,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", "
@@ -1118,7 +1118,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, ram_addr,
+    ret = vfio_container_query_dirty_bitmap(vrdl->bcontainer, iova, size, 0, ram_addr,
                                 &local_err);
     if (ret) {
         error_report_err(local_err);
@@ -1203,7 +1203,7 @@ static int vfio_sync_dirty_bitmap(VFIOContainerBase *bcontainer,
 
     return vfio_container_query_dirty_bitmap(bcontainer,
                    REAL_HOST_PAGE_ALIGN(section->offset_within_address_space),
-                                 int128_get64(section->size), ram_addr, errp);
+                                 int128_get64(section->size), 0, ram_addr, errp);
 }
 
 static void vfio_listener_log_sync(MemoryListener *listener,
hw/vfio/trace-events +0 -0
index e3d571f8c845..ee18dafdb2ae 100644
@@ -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-base.c
-vfio_container_query_dirty_bitmap(uint64_t iova, uint64_t size, uint64_t bitmap_size, uint64_t
start, uint64_t dirty_pages) "
iova=0x%"PRIx64" size= 0x%"PRIx64" bitmap_size=0x%"PRIx64" start=0x%"PRIx64" dirty_pages=%"PRIu64
+vfio_container_query_dirty_bitmap(uint64_t iova, uint64_t size, uint64_t flags, uint64_t
bitmap_size, uint64_t start, uint64_
t dirty_pages) "iova=0x%"PRIx64" size= 0x%"PRIx64" flags= 0x%"PRIx64" bitmap_size=0x%"PRIx64"
start=0x%"PRIx64" dirty_pages=%"
PRIu64
 # container.c
 vfio_container_disconnect(int fd) "close container->fd=%d"
hw/vfio/vfio-iommufd.h +0 -1
index e0af241c75cf..07ea0f430496 100644
@@ -26,7 +26,6 @@ typedef struct VFIOIOMMUFDContainer {
     VFIOContainerBase bcontainer;
     IOMMUFDBackend *be;
     uint32_t ioas_id;
-    uint64_t dirty_tracking_flags;
     QLIST_HEAD(, VFIOIOASHwpt) hwpt_list;
 } VFIOIOMMUFDContainer;
include/hw/vfio/vfio-container-base.h +4 -2
index acbd48a18a3a..e88b690cf423 100644
@@ -98,7 +98,7 @@ bool vfio_container_dirty_tracking_is_started(
 bool vfio_container_devices_dirty_tracking_is_supported(
     const VFIOContainerBase *bcontainer);
 int vfio_container_query_dirty_bitmap(const VFIOContainerBase *bcontainer,
-    uint64_t iova, uint64_t size, ram_addr_t ram_addr, Error **errp);
+    uint64_t iova, uint64_t size, uint64_t flags, ram_addr_t ram_addr, Error **errp);
 
 GList *vfio_container_get_iova_ranges(const VFIOContainerBase *bcontainer);
 
@@ -252,12 +252,14 @@ struct VFIOIOMMUClass {
      * @vbmap: #VFIOBitmap internal bitmap structure
      * @iova: iova base address
      * @size: size of iova range
+     * @flags: flags to the dirty tracking query
      * @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 VFIOContainerBase *bcontainer,
-                VFIOBitmap *vbmap, hwaddr iova, hwaddr size, Error **errp);
+                VFIOBitmap *vbmap, hwaddr iova, hwaddr size, uint64_t flags,
+                Error **errp);
     /* PCI specific */
     int (*pci_hot_reset)(VFIODevice *vbasedev, bool single);
 
#15 lore raw

RE: [PATCH 3/5] vfio/iommufd: Add IOMMU_HWPT_GET_DIRTY_BITMAP_NO_CLEAR flag support

Quoted 212 lines

-----Original Message-----
From: Joao Martins <[email protected]>
Subject: Re: [PATCH 3/5] vfio/iommufd: Add
IOMMU_HWPT_GET_DIRTY_BITMAP_NO_CLEAR flag support

Quoted 11 lines · Joao Martins
Quoted 10 lines · Cédric Le Goater
Quoted 9 lines · Zhenzhong

Hi Joao,

Quoted 7 lines

-----Original Message-----
From: Joao Martins <[email protected]>
Subject: Re: [PATCH 3/5] vfio/iommufd: Add
IOMMU_HWPT_GET_DIRTY_BITMAP_NO_CLEAR flag support

Quoted 1 line · Zhenzhong Duan

Pass IOMMU_HWPT_GET_DIRTY_BITMAP_NO_CLEAR when doing the

last

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

dirty

Quoted 2 lines

bitmap query right before unmap, no PTEs flushes. This accelerates the query without issue because unmap will tear down the mapping

anyway.

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

Add a new element dirty_tracking_flags in VFIOIOMMUFDContainer to be used for the flags of iommufd dirty tracking. Currently it is set to either IOMMU_HWPT_GET_DIRTY_BITMAP_NO_CLEAR or 0

based on

Quoted 34 lines
Quoted 34 lines
Quoted 34 lines
Quoted 34 lines

include/system/iommufd.h bool

Quoted 32 lines

the scenario.

---
  hw/vfio/vfio-iommufd.h   | 1 +
  include/system/iommufd.h | 2 +-
  backends/iommufd.c       | 5 +++--
  hw/vfio/iommufd.c        | 6 +++++-
  backends/trace-events    | 2 +-

  5 files changed, 11 insertions(+), 5 deletions(-)
hw/vfio/vfio-iommufd.h +1 -0
index 07ea0f4304..e0af241c75 100644
@@ -26,6 +26,7 @@ typedef struct VFIOIOMMUFDContainer {
       VFIOContainerBase bcontainer;
       IOMMUFDBackend *be;
       uint32_t ioas_id;
+    uint64_t dirty_tracking_flags;
       QLIST_HEAD(, VFIOIOASHwpt) hwpt_list;
   } VFIOIOMMUFDContainer;
 
include/system/iommufd.h +0 -0
index c9c72ffc45..63898e7b0d 100644
@@ -64,7 +64,7 @@ bool

iommufd_backend_set_dirty_tracking(IOMMUFDBackend *be, uint32_t hwpt_id,

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

include/system/iommufd.h bool · line 64

Quoted 1 line
include/system/iommufd.h +0 -0
   bool iommufd_backend_get_dirty_bitmap(IOMMUFDBackend *be,

uint32_t hwpt_id,

Quoted 1 line

                                        uint64_t

iova, ram_addr_t

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

size,

Quoted 1 line

                                        uint64_t

page_size,

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

uint64_t *data,

Quoted 1 line

-                                      Error

**errp);

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

+                                      uint64_t

flags, Error

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

**errp);

Quoted 1 line

  bool iommufd_backend_invalidate_cache(IOMMUFDBackend *be,

uint32_t id,

Quoted 1 line

                                        uint32_t

data_type,

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

uint32_t entry_len,

Quoted 1 line

                                        uint32_t

*entry_num, void

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

*data,

backends/iommufd.c bool

Quoted 5 lines
backends/iommufd.c +0 -0
index 2a33c7ab0b..3c4f6157e2 100644
@@ -361,7 +361,7 @@ bool

iommufd_backend_get_dirty_bitmap(IOMMUFDBackend *be,

Quoted 1 line

                                        uint32_t

hwpt_id,

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

                                        uint64_t

iova, ram_addr_t

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

size,

Quoted 1 line

                                        uint64_t

page_size,

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

uint64_t *data,

Quoted 2 lines

-                                      Error **errp) +                                      uint64_t

flags, Error **errp)

Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines

backends/iommufd.c bool

Quoted 4 lines

  {
      int ret;
      struct iommu_hwpt_get_dirty_bitmap get_dirty_bitmap = {

backends/iommufd.c +0 -0
@@ -371,11 +371,12 @@ bool

iommufd_backend_get_dirty_bitmap(IOMMUFDBackend *be,

Quoted 6 lines
backends/iommufd.c +1 -0
           .length = size,
           .page_size = page_size,
           .data = (uintptr_t)data,
+        .flags = flags,
       };
 
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

backends/iommufd.c bool · line 377

Quoted 1 line
backends/iommufd.c +0 -0
       ret = ioctl(be->fd, IOMMU_HWPT_GET_DIRTY_BITMAP,

&get_dirty_bitmap);

Quoted 1 line
backends/iommufd.c +0 -0
       trace_iommufd_backend_get_dirty_bitmap(be->fd, hwpt_id,

iova,

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

size,

Quoted 1 line

-                                           page_size, ret ? errno :

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

0);

Quoted 1 line

+                                           flags,

page_size, ret ?

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

errno : 0);

Quoted 3 lines

      if (ret) {
          error_setg_errno(errp, errno,
                           "IOMMU_HWPT_GET_DIRTY_

BITMAP

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

(iova: 0x%"HWADDR_PRIx

hw/vfio/iommufd.c static int iommufd_cdev_unmap_one(const

Quoted 5 lines
hw/vfio/iommufd.c +0 -0
index 0057488ce9..c897aa6b17 100644
@@ -62,7 +62,7 @@ static int iommufd_cdev_unmap_one(const

VFIOContainerBase *bcontainer,

Quoted 1 line
hw/vfio/iommufd.c +0 -0
                                     hwaddr iova,

ram_addr_t size,

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

                                    IOMMUTLBEntr

y *iotlb)

Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 4 lines

  {
-    const VFIOIOMMUFDContainer *container =
+    VFIOIOMMUFDContainer *container = container_of(bcontainer, VFIOIOMMUFDContainer,

bcontainer);

hw/vfio/iommufd.c static int iommufd_cdev_unmap_one(const

Quoted 3 lines

      bool need_dirty_sync = false;
      Error *local_err = NULL;

hw/vfio/iommufd.c +0 -0
@@ -73,9 +73,12 @@ static int iommufd_cdev_unmap_one(const

VFIOContainerBase *bcontainer,

Quoted 1 line

      if (iotlb &&

vfio_container_dirty_tracking_is_started(bcontainer)) {

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 1 line

          if

(!vfio_container_devices_dirty_tracking_is_supported(bcontainer) &&

Quoted 3 lines

              bcontainer->dirty_pages_supported) { +            container->dirty_tracking_flags = +

IOMMU_HWPT_GET_DIRTY_BITMAP_NO_CLEAR;

Quoted 1 line

              ret =

vfio_container_query_dirty_bitmap(bcontainer, iova,

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

size,

Quoted 1 line

iotlb->translated_addr,

Quoted 1 line

&local_err);

Quoted 1 line

+            container->dirty_tracking_flags = 0;

Why not changing vfio_container_query_dirty_bitmap to pass a flags

too, like

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 3 lines

the
original patches? This is a little unnecssary odd style to pass a flag via container structure rather and then clearing.

Just want to be simpler, original patch introduced a new parameter to

almost all

Quoted 1 line
Quoted 1 line
Quoted 1 line

variants of *_query_dirty_bitmap() while the flags parameter is only used

by

Quoted 1 line
Quoted 1 line
Quoted 1 line

IOMMUFD backend when doing unmap_bitmap. Currently we already

have three

Quoted 1 line
Quoted 1 line
Quoted 1 line

backends, legacy VFIO, IOMMUFD and VFIO-user, only IOMMUFD need

the flag.

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

I take container->dirty_tracking_flags as a notification mechanism, so set

it

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

before
vfio_container_query_dirty_bitmap() and clear it thereafter. Maybe

clearing it in

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

iommufd_query_dirty_bitmap() is easier to be acceptable?

Quoted 2 lines

Part of the reason the original series had a VFIO_GET_DIRTY_NO_FLUSH

for

Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
Quoted 7 lines

generic
container abstraction was to not mix IOMMUFD UAPI specifics into base container
API. Then in getting a VFIO_GET_DIRTY_NO_FLUSH, then type1 backend could just
ignore the flag, while IOMMUFD translates it to IOMMU_HWPT_GET_DIRTY_BITMAP_NO_CLEAR

I did port original patch https://github.com/yiliu1765/qemu/
commit/99f83595d79d2e4170c9e456cf1a7b9521bd4f80 But it looks complex to have 'flags' parameter everywhere.

Quoted 7 lines
Quoted 4 lines

I think I would prefer like Joao to avoid caching information if possible but I haven't check closely the mess it would introduce in the code. Let me check.

My recollection was that it wasn't that much churn added as this series is already doing the most of the churn. But I am checking how the code would

look

Quoted 2 lines

like to properly respond to his suggestion on why he changing it towards structuref field.

Quoted 6 lines

The churn should be similar to this patch:

https://github.com/jpemartins/qemu/commit/5e1f11075299a5fa9564a26788
dc9cc1717e297c

It's mostly an interface parameter addition of flags.

Yes, it's a general interface parameter but it's only used by IOMMUFD. That's my only argument.

Quoted 3 lines

But there's now a few more callsites and I suppose that's the extra churn. But I don't think
vfio-user would need to change. See snip below.

vfio-user need same change as vfio-legacy though they don't use 'flags' parameter, or else there is build error:

../hw/vfio-user/container.c:340:30: error: assignment to ‘int (*)(const VFIOContainerBase *, VFIOBitmap *, hwaddr, hwaddr, uint64_t, Error **)’ {aka ‘int (*)(const VFIOContainerBase *, VFIOBitmap *, long unsigned int, long unsigned int, long unsigned int, Error **)’} from incompatible pointer type ‘int (*)(const VFIOContainerBase *, VFIOBitmap *, hwaddr, hwaddr, Error **)’ {aka ‘int (*)(const VFIOContainerBase *, VFIOBitmap *, long unsigned int, long unsigned int, Error **)’} [-Werror=incompatible-pointer-types]
340 | vioc->query_dirty_bitmap = vfio_user_query_dirty_bitmap;

Quoted 35 lines

I've pushed this here
https://github.com/jpemartins/qemu/commits/relax-viommu-lm but showing the
series wide changes for discussion. The thing I didn't do was have an intermediate 'backend
agnostic' flag thingie like the original one
(https://github.com/jpemartins/qemu/commit/1ef8abc896ae69be8896a6870
5fe4a87204709e0) with
VFIO_GET_DIRTY_NO_FLUSH

hw/vfio/container-base.c +0 -0
index 56304978e1e8..2fe08e010405 100644
@@ -211,17 +211,16 @@ static int
 vfio_device_dma_logging_report(VFIODevice *vbasedev, hwaddr iova,
 }
 

static int vfio_container_iommu_query_dirty_bitmap(const VFIOContainerBase *bcontainer,
- VFIOBitmap *vbmap, hwaddr iova, hwaddr size, Error
**errp)
+ VFIOBitmap *vbmap, hwaddr iova, hwaddr size,
uint64_t flags, Error **errp)

{
    VFIOIOMMUClass *vioc = VFIO_IOMMU_GET_CLASS(bcontainer);

    g_assert(vioc->query_dirty_bitmap);
hw/vfio/container-base.c +1 -2
-    return vioc->query_dirty_bitmap(bcontainer, vbmap, iova, size,
-                                               errp);
+    return vioc->query_dirty_bitmap(bcontainer, vbmap, iova, size, flags,

errp);
}

Quoted 6 lines

static int vfio_container_devices_query_dirty_bitmap(const VFIOContainerBase *bcontainer,
- VFIOBitmap *vbmap, hwaddr iova, hwaddr size, Error
**errp)
+ VFIOBitmap *vbmap, hwaddr iova, hwaddr size,
uint64_t flags, Error **errp)

I think we can drop 'flags' here as you did in your old patch?

Quoted 24 lines
{
    VFIODevice *vbasedev;
    int ret;
hw/vfio/container-base.c +0 -0
@@ -243,7 +242,7 @@ static int

vfio_container_devices_query_dirty_bitmap(const VFIOContainerBase *bc }

int vfio_container_query_dirty_bitmap(const VFIOContainerBase *bcontainer, uint64_t iova,
- uint64_t size, ram_addr_t ram_addr, Error
**errp)
+ uint64_t size, uint64_t flags, ram_addr_t
ram_addr, Error **errp)
{
bool all_device_dirty_tracking =
vfio_container_devices_dirty_tracking_is_supported(bcontainer);

hw/vfio/container-base.c +0 -0
@@ -267,10 +266,10 @@ int vfio_container_query_dirty_bitmap(const

VFIOContainerBase *bcontainer, uint6

if (all_device_dirty_tracking) {
ret = vfio_container_devices_query_dirty_bitmap(bcontainer,
&vbmap, iova, size,

hw/vfio/container-base.c +1 -1
-                                                        errp);
+                                                        flags,
Quoted 1 line

errp);

Same here.

Thanks
Zhenzhong

#16 lore raw

Re: [PATCH 3/5] vfio/iommufd: Add IOMMU_HWPT_GET_DIRTY_BITMAP_NO_CLEAR flag support

Quoted 212 lines · Zhenzhong
Quoted 212 lines

-----Original Message-----
From: Joao Martins <[email protected]>
Subject: Re: [PATCH 3/5] vfio/iommufd: Add
IOMMU_HWPT_GET_DIRTY_BITMAP_NO_CLEAR flag support

Quoted 11 lines · Joao Martins
Quoted 10 lines · Cédric Le Goater
Quoted 9 lines · Zhenzhong

Hi Joao,

Quoted 7 lines

-----Original Message-----
From: Joao Martins <[email protected]>
Subject: Re: [PATCH 3/5] vfio/iommufd: Add
IOMMU_HWPT_GET_DIRTY_BITMAP_NO_CLEAR flag support

Quoted 1 line · Zhenzhong Duan

Pass IOMMU_HWPT_GET_DIRTY_BITMAP_NO_CLEAR when doing the

last

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

dirty

Quoted 2 lines

bitmap query right before unmap, no PTEs flushes. This accelerates the query without issue because unmap will tear down the mapping

anyway.

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

Add a new element dirty_tracking_flags in VFIOIOMMUFDContainer to be used for the flags of iommufd dirty tracking. Currently it is set to either IOMMU_HWPT_GET_DIRTY_BITMAP_NO_CLEAR or 0

based on

Quoted 34 lines
Quoted 34 lines
Quoted 34 lines
Quoted 34 lines

include/system/iommufd.h bool

Quoted 32 lines

the scenario.

---
  hw/vfio/vfio-iommufd.h   | 1 +
  include/system/iommufd.h | 2 +-
  backends/iommufd.c       | 5 +++--
  hw/vfio/iommufd.c        | 6 +++++-
  backends/trace-events    | 2 +-

  5 files changed, 11 insertions(+), 5 deletions(-)
hw/vfio/vfio-iommufd.h +1 -0
index 07ea0f4304..e0af241c75 100644
@@ -26,6 +26,7 @@ typedef struct VFIOIOMMUFDContainer {
       VFIOContainerBase bcontainer;
       IOMMUFDBackend *be;
       uint32_t ioas_id;
+    uint64_t dirty_tracking_flags;
       QLIST_HEAD(, VFIOIOASHwpt) hwpt_list;
   } VFIOIOMMUFDContainer;
 
include/system/iommufd.h +0 -0
index c9c72ffc45..63898e7b0d 100644
@@ -64,7 +64,7 @@ bool

iommufd_backend_set_dirty_tracking(IOMMUFDBackend *be, uint32_t hwpt_id,

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

include/system/iommufd.h bool · line 64

Quoted 1 line
include/system/iommufd.h +0 -0
   bool iommufd_backend_get_dirty_bitmap(IOMMUFDBackend *be,

uint32_t hwpt_id,

Quoted 1 line

                                        uint64_t

iova, ram_addr_t

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

size,

Quoted 1 line

                                        uint64_t

page_size,

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

uint64_t *data,

Quoted 1 line

-                                      Error

**errp);

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

+                                      uint64_t

flags, Error

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

**errp);

Quoted 1 line

  bool iommufd_backend_invalidate_cache(IOMMUFDBackend *be,

uint32_t id,

Quoted 1 line

                                        uint32_t

data_type,

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

uint32_t entry_len,

Quoted 1 line

                                        uint32_t

*entry_num, void

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

*data,

backends/iommufd.c bool

Quoted 5 lines
backends/iommufd.c +0 -0
index 2a33c7ab0b..3c4f6157e2 100644
@@ -361,7 +361,7 @@ bool

iommufd_backend_get_dirty_bitmap(IOMMUFDBackend *be,

Quoted 1 line

                                        uint32_t

hwpt_id,

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

                                        uint64_t

iova, ram_addr_t

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

size,

Quoted 1 line

                                        uint64_t

page_size,

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

uint64_t *data,

Quoted 2 lines

-                                      Error **errp) +                                      uint64_t

flags, Error **errp)

Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines

backends/iommufd.c bool

Quoted 4 lines

  {
      int ret;
      struct iommu_hwpt_get_dirty_bitmap get_dirty_bitmap = {

backends/iommufd.c +0 -0
@@ -371,11 +371,12 @@ bool

iommufd_backend_get_dirty_bitmap(IOMMUFDBackend *be,

Quoted 6 lines
backends/iommufd.c +1 -0
           .length = size,
           .page_size = page_size,
           .data = (uintptr_t)data,
+        .flags = flags,
       };
 
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

backends/iommufd.c bool · line 377

Quoted 1 line
backends/iommufd.c +0 -0
       ret = ioctl(be->fd, IOMMU_HWPT_GET_DIRTY_BITMAP,

&get_dirty_bitmap);

Quoted 1 line
backends/iommufd.c +0 -0
       trace_iommufd_backend_get_dirty_bitmap(be->fd, hwpt_id,

iova,

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

size,

Quoted 1 line

-                                           page_size, ret ? errno :

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

0);

Quoted 1 line

+                                           flags,

page_size, ret ?

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

errno : 0);

Quoted 3 lines

      if (ret) {
          error_setg_errno(errp, errno,
                           "IOMMU_HWPT_GET_DIRTY_

BITMAP

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

(iova: 0x%"HWADDR_PRIx

hw/vfio/iommufd.c static int iommufd_cdev_unmap_one(const

Quoted 5 lines
hw/vfio/iommufd.c +0 -0
index 0057488ce9..c897aa6b17 100644
@@ -62,7 +62,7 @@ static int iommufd_cdev_unmap_one(const

VFIOContainerBase *bcontainer,

Quoted 1 line
hw/vfio/iommufd.c +0 -0
                                     hwaddr iova,

ram_addr_t size,

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

                                    IOMMUTLBEntr

y *iotlb)

Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 4 lines

  {
-    const VFIOIOMMUFDContainer *container =
+    VFIOIOMMUFDContainer *container = container_of(bcontainer, VFIOIOMMUFDContainer,

bcontainer);

hw/vfio/iommufd.c static int iommufd_cdev_unmap_one(const

Quoted 3 lines

      bool need_dirty_sync = false;
      Error *local_err = NULL;

hw/vfio/iommufd.c +0 -0
@@ -73,9 +73,12 @@ static int iommufd_cdev_unmap_one(const

VFIOContainerBase *bcontainer,

Quoted 1 line

      if (iotlb &&

vfio_container_dirty_tracking_is_started(bcontainer)) {

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 1 line

          if

(!vfio_container_devices_dirty_tracking_is_supported(bcontainer) &&

Quoted 3 lines

              bcontainer->dirty_pages_supported) { +            container->dirty_tracking_flags = +

IOMMU_HWPT_GET_DIRTY_BITMAP_NO_CLEAR;

Quoted 1 line

              ret =

vfio_container_query_dirty_bitmap(bcontainer, iova,

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

size,

Quoted 1 line

iotlb->translated_addr,

Quoted 1 line

&local_err);

Quoted 1 line

+            container->dirty_tracking_flags = 0;

Why not changing vfio_container_query_dirty_bitmap to pass a flags

too, like

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 3 lines

the
original patches? This is a little unnecssary odd style to pass a flag via container structure rather and then clearing.

Just want to be simpler, original patch introduced a new parameter to

almost all

Quoted 1 line
Quoted 1 line
Quoted 1 line

variants of *_query_dirty_bitmap() while the flags parameter is only used

by

Quoted 1 line
Quoted 1 line
Quoted 1 line

IOMMUFD backend when doing unmap_bitmap. Currently we already

have three

Quoted 1 line
Quoted 1 line
Quoted 1 line

backends, legacy VFIO, IOMMUFD and VFIO-user, only IOMMUFD need

the flag.

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

I take container->dirty_tracking_flags as a notification mechanism, so set

it

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

before
vfio_container_query_dirty_bitmap() and clear it thereafter. Maybe

clearing it in

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

iommufd_query_dirty_bitmap() is easier to be acceptable?

Quoted 2 lines

Part of the reason the original series had a VFIO_GET_DIRTY_NO_FLUSH

for

Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
Quoted 7 lines

generic
container abstraction was to not mix IOMMUFD UAPI specifics into base container
API. Then in getting a VFIO_GET_DIRTY_NO_FLUSH, then type1 backend could just
ignore the flag, while IOMMUFD translates it to IOMMU_HWPT_GET_DIRTY_BITMAP_NO_CLEAR

I did port original patch https://github.com/yiliu1765/qemu/
commit/99f83595d79d2e4170c9e456cf1a7b9521bd4f80 But it looks complex to have 'flags' parameter everywhere.

Quoted 7 lines
Quoted 4 lines

I think I would prefer like Joao to avoid caching information if possible but I haven't check closely the mess it would introduce in the code. Let me check.

My recollection was that it wasn't that much churn added as this series is already doing the most of the churn. But I am checking how the code would

look

Quoted 2 lines

like to properly respond to his suggestion on why he changing it towards structuref field.

The churn should be similar to this patch:

Quoted 8 lines
Quoted 4 lines

https://github.com/jpemartins/qemu/commit/5e1f11075299a5fa9564a26788
dc9cc1717e297c

It's mostly an interface parameter addition of flags.

Yes, it's a general interface parameter but it's only used by IOMMUFD. That's my only argument.

You could use that argument for anything that's not common and unique to each backend. type1 dirty tracking is effectively "frozen" in UAPI (IIRC) so I won't expect flags. There's device dirty tracking, IOMMUFD dirty tracking and vfio-user dirty tracking. While I am not anticipating any new thing here (at least in the needs I have ahead, can't speak for other companies/contributors); but you could see the flags as future proofing it.

The ugly part of the alternative is that it sort of limits the interface to be single-user only as nobody call into IOMMUFD with the dirty_tracking_flags if it's called concurrently for some other purpose than unmap. We sort of use the container data structure as a argument storage for a single call as opposed to store some intermediary state.

I mean I agree with you that there's churn, but it's essentially a argument change that's only churn-y because there's 3 users.

Quoted 10 lines
Quoted 3 lines

But there's now a few more callsites and I suppose that's the extra churn. But I don't think
vfio-user would need to change. See snip below.

vfio-user need same change as vfio-legacy though they don't use 'flags' parameter, or else there is build error:

../hw/vfio-user/container.c:340:30: error: assignment to ‘int (*)(const VFIOContainerBase *, VFIOBitmap *, hwaddr, hwaddr, uint64_t, Error **)’ {aka ‘int (*)(const VFIOContainerBase *, VFIOBitmap *, long unsigned int, long unsigned int, long unsigned int, Error **)’} from incompatible pointer type ‘int (*)(const VFIOContainerBase *, VFIOBitmap *, hwaddr, hwaddr, Error **)’ {aka ‘int (*)(const VFIOContainerBase *, VFIOBitmap *, long unsigned int, long unsigned int, Error **)’} [-Werror=incompatible-pointer-types]
340 | vioc->query_dirty_bitmap = vfio_user_query_dirty_bitmap;

yes -- I didn't have VFIO_USER compiled in probably why I didn't notice it.

Quoted 11 lines
Quoted 11 lines

I've pushed this here
https://github.com/jpemartins/qemu/commits/relax-viommu-lm but showing the
series wide changes for discussion. The thing I didn't do was have an intermediate 'backend
agnostic' flag thingie like the original one
(https://github.com/jpemartins/qemu/commit/1ef8abc896ae69be8896a6870
5fe4a87204709e0) with
VFIO_GET_DIRTY_NO_FLUSH

Instead of a flags, an alternative would be to have a 'bool unmap' which then IOMMUFD can translate to IOMMU_HWPT_GET_DIRTY_BITMAP_NO_CLEAR.

That's a bit cleaner as to avoid leaking backend UAPIs into the API but it limits to 'unmap' case.

Quoted 24 lines
Quoted 24 lines
hw/vfio/container-base.c +0 -0
index 56304978e1e8..2fe08e010405 100644
@@ -211,17 +211,16 @@ static int
 vfio_device_dma_logging_report(VFIODevice *vbasedev, hwaddr iova,
 }
 

static int vfio_container_iommu_query_dirty_bitmap(const VFIOContainerBase *bcontainer,
- VFIOBitmap *vbmap, hwaddr iova, hwaddr size, Error
**errp)
+ VFIOBitmap *vbmap, hwaddr iova, hwaddr size,
uint64_t flags, Error **errp)

{
    VFIOIOMMUClass *vioc = VFIO_IOMMU_GET_CLASS(bcontainer);

    g_assert(vioc->query_dirty_bitmap);
hw/vfio/container-base.c +1 -2
-    return vioc->query_dirty_bitmap(bcontainer, vbmap, iova, size,
-                                               errp);
+    return vioc->query_dirty_bitmap(bcontainer, vbmap, iova, size, flags,

errp);
}

Quoted 9 lines
Quoted 6 lines

static int vfio_container_devices_query_dirty_bitmap(const VFIOContainerBase *bcontainer,
- VFIOBitmap *vbmap, hwaddr iova, hwaddr size, Error
**errp)
+ VFIOBitmap *vbmap, hwaddr iova, hwaddr size,
uint64_t flags, Error **errp)

I think we can drop 'flags' here as you did in your old patch?

Yeah

Quoted 19 lines
Quoted 19 lines
{
    VFIODevice *vbasedev;
    int ret;
hw/vfio/container-base.c +0 -0
@@ -243,7 +242,7 @@ static int

vfio_container_devices_query_dirty_bitmap(const VFIOContainerBase *bc }

int vfio_container_query_dirty_bitmap(const VFIOContainerBase *bcontainer, uint64_t iova,
- uint64_t size, ram_addr_t ram_addr, Error
**errp)
+ uint64_t size, uint64_t flags, ram_addr_t
ram_addr, Error **errp)
{
bool all_device_dirty_tracking =
vfio_container_devices_dirty_tracking_is_supported(bcontainer);

hw/vfio/container-base.c +0 -0
@@ -267,10 +266,10 @@ int vfio_container_query_dirty_bitmap(const

VFIOContainerBase *bcontainer, uint6

Quoted 8 lines
Quoted 6 lines

if (all_device_dirty_tracking) {
ret = vfio_container_devices_query_dirty_bitmap(bcontainer,
&vbmap, iova, size,

hw/vfio/container-base.c +1 -1
-                                                        errp);
+                                                        flags,

errp);

Same here.

/me nods

#17 lore raw

RE: [PATCH 3/5] vfio/iommufd: Add IOMMU_HWPT_GET_DIRTY_BITMAP_NO_CLEAR flag support

Quoted 255 lines

-----Original Message-----
From: Joao Martins <[email protected]>
Subject: Re: [PATCH 3/5] vfio/iommufd: Add
IOMMU_HWPT_GET_DIRTY_BITMAP_NO_CLEAR flag support

Quoted 10 lines · Zhenzhong
Quoted 10 lines

-----Original Message-----
From: Joao Martins <[email protected]>
Subject: Re: [PATCH 3/5] vfio/iommufd: Add
IOMMU_HWPT_GET_DIRTY_BITMAP_NO_CLEAR flag support

Quoted 4 lines · Joao Martins
Quoted 3 lines · Cédric Le Goater
Quoted 2 lines · Zhenzhong

Hi Joao,

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

-----Original Message-----
From: Joao Martins <[email protected]>
Subject: Re: [PATCH 3/5] vfio/iommufd: Add
IOMMU_HWPT_GET_DIRTY_BITMAP_NO_CLEAR flag support

Quoted 1 line · Zhenzhong Duan

Pass IOMMU_HWPT_GET_DIRTY_BITMAP_NO_CLEAR when doing

the

Quoted 3 lines
Quoted 3 lines

last

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

dirty

Quoted 1 line

bitmap query right before unmap, no PTEs flushes. This accelerates

the

Quoted 4 lines
Quoted 4 lines
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

query without issue because unmap will tear down the mapping

anyway.

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

Add a new element dirty_tracking_flags in VFIOIOMMUFDContainer

to

Quoted 30 lines
Quoted 30 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

be used for the flags of iommufd dirty tracking. Currently it is set to either IOMMU_HWPT_GET_DIRTY_BITMAP_NO_CLEAR or 0

based on

Quoted 27 lines
Quoted 27 lines
Quoted 27 lines
Quoted 27 lines
Quoted 27 lines

the scenario.

---
  hw/vfio/vfio-iommufd.h   | 1 +
  include/system/iommufd.h | 2 +-
  backends/iommufd.c       | 5 +++--
  hw/vfio/iommufd.c        | 6 +++++-
  backends/trace-events    | 2 +-

  5 files changed, 11 insertions(+), 5 deletions(-)
hw/vfio/vfio-iommufd.h +1 -0
index 07ea0f4304..e0af241c75 100644
@@ -26,6 +26,7 @@ typedef struct VFIOIOMMUFDContainer {
       VFIOContainerBase bcontainer;
       IOMMUFDBackend *be;
       uint32_t ioas_id;
+    uint64_t dirty_tracking_flags;
       QLIST_HEAD(, VFIOIOASHwpt) hwpt_list;
   } VFIOIOMMUFDContainer;
 
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

include/system/iommufd.h bool

Quoted 5 lines
include/system/iommufd.h +0 -0
index c9c72ffc45..63898e7b0d 100644
@@ -64,7 +64,7 @@ bool

iommufd_backend_set_dirty_tracking(IOMMUFDBackend *be,

uint32_t

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

hwpt_id,

Quoted 1 line

  bool iommufd_backend_get_dirty_bitmap(IOMMUFDBackend

*be,

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

uint32_t hwpt_id,

Quoted 1 line

                                        uint64_

t

Quoted 3 lines
Quoted 3 lines

iova, ram_addr_t

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

size,

Quoted 1 line

                                        uint64_

t

Quoted 8 lines
Quoted 8 lines

page_size,

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

uint64_t *data,

Quoted 1 line

-                                      Error

**errp);

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

+                                      uint64_t

flags, Error

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

**errp);

Quoted 1 line

  bool iommufd_backend_invalidate_cache(IOMMUFDBackend

*be,

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

uint32_t id,

Quoted 1 line

                                        uint32_

t

Quoted 3 lines
Quoted 3 lines

data_type,

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

uint32_t entry_len,

Quoted 1 line

                                        uint32_

t

Quoted 9 lines
Quoted 9 lines

*entry_num, void

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

*data,

backends/iommufd.c bool

Quoted 5 lines
backends/iommufd.c +0 -0
index 2a33c7ab0b..3c4f6157e2 100644
@@ -361,7 +361,7 @@ bool

iommufd_backend_get_dirty_bitmap(IOMMUFDBackend *be,

Quoted 1 line

                                        uint32_

t

Quoted 2 lines
Quoted 2 lines

hwpt_id,

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

                                        uint64_

t

Quoted 3 lines
Quoted 3 lines

iova, ram_addr_t

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

size,

Quoted 1 line

                                        uint64_

t

Quoted 3 lines
Quoted 3 lines

page_size,

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

uint64_t *data,

Quoted 1 line

-                                      Error

**errp)

Quoted 13 lines
Quoted 13 lines
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

+                                      uint64_t

flags, Error **errp)

Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines

backends/iommufd.c bool

Quoted 4 lines

  {
      int ret;
      struct iommu_hwpt_get_dirty_bitmap get_dirty_bitmap = {

backends/iommufd.c +0 -0
@@ -371,11 +371,12 @@ bool

iommufd_backend_get_dirty_bitmap(IOMMUFDBackend *be,

Quoted 6 lines
backends/iommufd.c +1 -0
           .length = size,
           .page_size = page_size,
           .data = (uintptr_t)data,
+        .flags = flags,
       };
 
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

backends/iommufd.c bool · line 377

Quoted 1 line
backends/iommufd.c +0 -0
       ret = ioctl(be->fd, IOMMU_HWPT_GET_DIRTY_BITMAP,

&get_dirty_bitmap);

Quoted 1 line
backends/iommufd.c +0 -0
       trace_iommufd_backend_get_dirty_bitmap(be->fd,

hwpt_id,

Quoted 4 lines
Quoted 4 lines

iova,

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

size,

Quoted 1 line

-                                           page_size,

Quoted 3 lines
Quoted 3 lines

ret ? errno :

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

0);

Quoted 1 line

+                                           flags,

Quoted 5 lines
Quoted 5 lines

page_size, ret ?

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

errno : 0);

Quoted 3 lines

      if (ret) {
          error_setg_errno(errp, errno,
                           "IOMMU_HWPT_GET_DIRT

Y_

Quoted 11 lines
Quoted 11 lines

BITMAP

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

(iova: 0x%"HWADDR_PRIx

hw/vfio/iommufd.c static int iommufd_cdev_unmap_one(const

Quoted 5 lines
hw/vfio/iommufd.c +0 -0
index 0057488ce9..c897aa6b17 100644
@@ -62,7 +62,7 @@ static int iommufd_cdev_unmap_one(const

VFIOContainerBase *bcontainer,

Quoted 1 line
hw/vfio/iommufd.c +0 -0
                                     hwaddr iova,

ram_addr_t size,

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

                                    IOMMUTLBE

ntr

Quoted 28 lines
Quoted 28 lines

y *iotlb)

Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 4 lines

  {
-    const VFIOIOMMUFDContainer *container =
+    VFIOIOMMUFDContainer *container = container_of(bcontainer, VFIOIOMMUFDContainer,

bcontainer);

hw/vfio/iommufd.c static int iommufd_cdev_unmap_one(const

Quoted 3 lines

      bool need_dirty_sync = false;
      Error *local_err = NULL;

hw/vfio/iommufd.c +0 -0
@@ -73,9 +73,12 @@ static int iommufd_cdev_unmap_one(const

VFIOContainerBase *bcontainer,

Quoted 1 line

      if (iotlb &&

vfio_container_dirty_tracking_is_started(bcontainer)) {

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 1 line

          if

(!vfio_container_devices_dirty_tracking_is_supported(bcontainer) &&

Quoted 3 lines

              bcontainer->dirty_pages_supported) { +            container->dirty_tracking_flags = +

IOMMU_HWPT_GET_DIRTY_BITMAP_NO_CLEAR;

Quoted 1 line

              ret =

vfio_container_query_dirty_bitmap(bcontainer, iova,

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

size,

Quoted 1 line

iotlb->translated_addr,

Quoted 1 line

&local_err);

Quoted 1 line

+            container->dirty_tracking_flags = 0;

Why not changing vfio_container_query_dirty_bitmap to pass a flags

Quoted 8 lines
Quoted 8 lines

too, like

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 3 lines

the
original patches? This is a little unnecssary odd style to pass a flag via container structure rather and then clearing.

Just want to be simpler, original patch introduced a new parameter to

almost all

Quoted 1 line
Quoted 1 line
Quoted 1 line

variants of *_query_dirty_bitmap() while the flags parameter is only

used

Quoted 7 lines
Quoted 7 lines

by

Quoted 1 line
Quoted 1 line
Quoted 1 line

IOMMUFD backend when doing unmap_bitmap. Currently we already

have three

Quoted 1 line
Quoted 1 line
Quoted 1 line

backends, legacy VFIO, IOMMUFD and VFIO-user, only IOMMUFD need

the flag.

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

I take container->dirty_tracking_flags as a notification mechanism, so

set

Quoted 8 lines
Quoted 8 lines

it

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

before
vfio_container_query_dirty_bitmap() and clear it thereafter. Maybe

clearing it in

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

iommufd_query_dirty_bitmap() is easier to be acceptable?

Quoted 2 lines

Part of the reason the original series had a

VFIO_GET_DIRTY_NO_FLUSH

Quoted 3 lines
Quoted 3 lines

for

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

generic
container abstraction was to not mix IOMMUFD UAPI specifics into

base

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

container
API. Then in getting a VFIO_GET_DIRTY_NO_FLUSH, then type1

backend

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 3 lines

could just
ignore the flag, while IOMMUFD translates it to
IOMMU_HWPT_GET_DIRTY_BITMAP_NO_CLEAR

I did port original patch https://github.com/yiliu1765/qemu/
commit/99f83595d79d2e4170c9e456cf1a7b9521bd4f80 But it looks complex to have 'flags' parameter everywhere.

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 4 lines

I think I would prefer like Joao to avoid caching information if possible but I haven't check closely the mess it would introduce in the code. Let me check.

My recollection was that it wasn't that much churn added as this series is already doing the most of the churn. But I am checking how the code

would

Quoted 7 lines
Quoted 7 lines

look

Quoted 2 lines

like to properly respond to his suggestion on why he changing it towards structuref field.

The churn should be similar to this patch:

https://github.com/jpemartins/qemu/commit/5e1f11075299a5fa9564a26788

Quoted 7 lines
Quoted 3 lines

dc9cc1717e297c

It's mostly an interface parameter addition of flags.

Yes, it's a general interface parameter but it's only used by IOMMUFD. That's my only argument.

Quoted 8 lines

You could use that argument for anything that's not common and unique to each
backend. type1 dirty tracking is effectively "frozen" in UAPI (IIRC) so I won't expect flags. There's device dirty tracking, IOMMUFD dirty tracking and vfio-user dirty tracking. While I am not anticipating any new thing here (at least in the needs I have ahead, can't speak for other companies/contributors);
but you could see the flags as future proofing it.

Yes, agree it's future proofing.

Quoted 7 lines

The ugly part of the alternative is that it sort of limits the interface to be single-user only as nobody call into IOMMUFD with the dirty_tracking_flags if it's called concurrently for some other purpose than unmap. We sort of use the
container data structure as a argument storage for a single call as opposed to store some intermediary state.

dirty_tracking_flags only take effect during the unmap path except you take it for other purpose. But yes, I made the change based on the fact that unmap path is protected by BQL, if the concurrent access is allowed in future, this change will break.

Thanks
Zhenzhong

#18 lore raw

RE: [PATCH 3/5] vfio/iommufd: Add IOMMU_HWPT_GET_DIRTY_BITMAP_NO_CLEAR flag support

Quoted 13 lines

-----Original Message-----
From: Duan, Zhenzhong
Subject: RE: [PATCH 3/5] vfio/iommufd: Add
IOMMU_HWPT_GET_DIRTY_BITMAP_NO_CLEAR flag support

Quoted 2 lines

The ugly part of the alternative is that it sort of limits the interface to be single-user only as nobody call into IOMMUFD with the dirty_tracking_flags

if

Quoted 3 lines

it's called concurrently for some other purpose than unmap. We sort of use the
container data structure as a argument storage for a single call as opposed

to

Quoted 8 lines
Quoted 1 line

store some intermediary state.

dirty_tracking_flags only take effect during the unmap path except you take it for other purpose. But yes, I made the change based on the fact that unmap path
is protected by BQL, if the concurrent access is allowed in future, this change will
break.

I found I misunderstood your comment above. Yes, I planned dirty_tracking_flags only for unmap usage, not for others. I didn't take it as argument but a notification mechanism only for unmap_all path optimization.

Thanks
Zhenzhong

#19 lore raw

RE: [PATCH 3/5] vfio/iommufd: Add IOMMU_HWPT_GET_DIRTY_BITMAP_NO_CLEAR flag support

In reply to Cédric Le Goater
Quoted 184 lines

-----Original Message-----
From: Cédric Le Goater <[email protected]>
Subject: Re: [PATCH 3/5] vfio/iommufd: Add
IOMMU_HWPT_GET_DIRTY_BITMAP_NO_CLEAR flag support

Quoted 9 lines · Zhenzhong

Hi Joao,

Quoted 7 lines

-----Original Message-----
From: Joao Martins <[email protected]>
Subject: Re: [PATCH 3/5] vfio/iommufd: Add
IOMMU_HWPT_GET_DIRTY_BITMAP_NO_CLEAR flag support

Quoted 1 line · Zhenzhong Duan

Pass IOMMU_HWPT_GET_DIRTY_BITMAP_NO_CLEAR when doing the

last

Quoted 7 lines
Quoted 7 lines

dirty

Quoted 6 lines

bitmap query right before unmap, no PTEs flushes. This accelerates the query without issue because unmap will tear down the mapping anyway.

Add a new element dirty_tracking_flags in VFIOIOMMUFDContainer to be used for the flags of iommufd dirty tracking. Currently it is set to either IOMMU_HWPT_GET_DIRTY_BITMAP_NO_CLEAR or 0 based

on

Quoted 34 lines
Quoted 34 lines

include/system/iommufd.h bool

Quoted 32 lines

the scenario.

---
  hw/vfio/vfio-iommufd.h   | 1 +
  include/system/iommufd.h | 2 +-
  backends/iommufd.c       | 5 +++--
  hw/vfio/iommufd.c        | 6 +++++-
  backends/trace-events    | 2 +-
  5 files changed, 11 insertions(+), 5 deletions(-)
hw/vfio/vfio-iommufd.h +1 -0
index 07ea0f4304..e0af241c75 100644
@@ -26,6 +26,7 @@ typedef struct VFIOIOMMUFDContainer {
      VFIOContainerBase bcontainer;
      IOMMUFDBackend *be;
      uint32_t ioas_id;
+    uint64_t dirty_tracking_flags;
      QLIST_HEAD(, VFIOIOASHwpt) hwpt_list;
  } VFIOIOMMUFDContainer;
 
include/system/iommufd.h +0 -0
index c9c72ffc45..63898e7b0d 100644
@@ -64,7 +64,7 @@ bool

iommufd_backend_set_dirty_tracking(IOMMUFDBackend *be, uint32_t hwpt_id,

Quoted 3 lines
Quoted 3 lines

include/system/iommufd.h bool · line 64

Quoted 1 line
include/system/iommufd.h +0 -0
  bool iommufd_backend_get_dirty_bitmap(IOMMUFDBackend *be,

uint32_t hwpt_id,

Quoted 1 line
include/system/iommufd.h +0 -0
                                        uint64_t iova,

ram_addr_t

Quoted 11 lines
Quoted 11 lines

size,

include/system/iommufd.h bool · line 66

Quoted 1 line
include/system/iommufd.h +0 -0
                                        uint64_t page_size,

uint64_t *data,

include/system/iommufd.h bool · line 67

Quoted 2 lines
include/system/iommufd.h +1 -1
-                                      Error **errp);
+                                      uint64_t flags, Error

**errp);

include/system/iommufd.h bool · line 68

Quoted 1 line
include/system/iommufd.h +0 -0
  bool iommufd_backend_invalidate_cache(IOMMUFDBackend *be,

uint32_t id,

include/system/iommufd.h bool · line 69

Quoted 1 line
include/system/iommufd.h +0 -0
                                        uint32_t data_type,

uint32_t entry_len,

Quoted 1 line
include/system/iommufd.h +0 -0
                                        uint32_t *entry_num,

void

Quoted 9 lines
Quoted 9 lines

*data,

backends/iommufd.c bool

Quoted 5 lines
backends/iommufd.c +0 -0
index 2a33c7ab0b..3c4f6157e2 100644
@@ -361,7 +361,7 @@ bool

iommufd_backend_get_dirty_bitmap(IOMMUFDBackend *be,

Quoted 2 lines
backends/iommufd.c +0 -0
                                        uint32_t hwpt_id,
                                        uint64_t iova,

ram_addr_t

Quoted 5 lines
Quoted 5 lines

size,

backends/iommufd.c bool · line 363

Quoted 1 line
backends/iommufd.c +0 -0
                                        uint64_t page_size,

uint64_t *data,

Quoted 2 lines
backends/iommufd.c +1 -1
-                                      Error **errp)
+                                      uint64_t flags, Error

**errp)

Quoted 11 lines
Quoted 11 lines

backends/iommufd.c bool

Quoted 4 lines
backends/iommufd.c +0 -0
  {
      int ret;
      struct iommu_hwpt_get_dirty_bitmap get_dirty_bitmap = {
@@ -371,11 +371,12 @@ bool

iommufd_backend_get_dirty_bitmap(IOMMUFDBackend *be,

Quoted 6 lines
backends/iommufd.c +1 -0
          .length = size,
          .page_size = page_size,
          .data = (uintptr_t)data,
+        .flags = flags,
      };
 
Quoted 3 lines
Quoted 3 lines

backends/iommufd.c bool · line 377

Quoted 1 line
backends/iommufd.c +0 -0
      ret = ioctl(be->fd, IOMMU_HWPT_GET_DIRTY_BITMAP,

&get_dirty_bitmap);

Quoted 1 line
backends/iommufd.c +0 -0
      trace_iommufd_backend_get_dirty_bitmap(be->fd, hwpt_id,

iova,

Quoted 2 lines
Quoted 2 lines

size,

Quoted 1 line
backends/iommufd.c +0 -1
-                                           page_size, ret ?

errno :

Quoted 2 lines
Quoted 2 lines

0);

Quoted 1 line
backends/iommufd.c +1 -0
+                                           flags, page_size,

ret ?

Quoted 4 lines
Quoted 4 lines

errno : 0);

Quoted 3 lines
backends/iommufd.c +0 -0
      if (ret) {
          error_setg_errno(errp, errno,
                           "IOMMU_HWPT_GET_DIRTY_BITMAP
Quoted 8 lines
Quoted 8 lines

(iova: 0x%"HWADDR_PRIx

hw/vfio/iommufd.c static int iommufd_cdev_unmap_one(const

Quoted 5 lines
hw/vfio/iommufd.c +0 -0
index 0057488ce9..c897aa6b17 100644
@@ -62,7 +62,7 @@ static int iommufd_cdev_unmap_one(const

VFIOContainerBase *bcontainer,

Quoted 1 line
hw/vfio/iommufd.c +0 -0
                                    hwaddr iova, ram_addr_t

size,

Quoted 11 lines
Quoted 11 lines

hw/vfio/iommufd.c static int iommufd_cdev_unmap_one(const VFIOContainerBase *bcontainer,

Quoted 5 lines
hw/vfio/iommufd.c +1 -1
                                    IOMMUTLBEntry *iotlb)
  {
-    const VFIOIOMMUFDContainer *container =
+    VFIOIOMMUFDContainer *container =
          container_of(bcontainer, VFIOIOMMUFDContainer,

bcontainer);

hw/vfio/iommufd.c static int iommufd_cdev_unmap_one(const

Quoted 3 lines
hw/vfio/iommufd.c +0 -0
      bool need_dirty_sync = false;
      Error *local_err = NULL;
@@ -73,9 +73,12 @@ static int iommufd_cdev_unmap_one(const

VFIOContainerBase *bcontainer,

Quoted 1 line
hw/vfio/iommufd.c +0 -0
      if (iotlb && vfio_container_dirty_tracking_is_started(bcontainer))

{

Quoted 7 lines
Quoted 7 lines

hw/vfio/iommufd.c static int iommufd_cdev_unmap_one(const · line 74

Quoted 1 line
hw/vfio/iommufd.c +0 -0
          if

(!vfio_container_devices_dirty_tracking_is_supported(bcontainer) &&

hw/vfio/iommufd.c static int iommufd_cdev_unmap_one(const VFIOContainerBase *bcontainer,

Quoted 3 lines
hw/vfio/iommufd.c +2 -0
              bcontainer->dirty_pages_supported) {
+            container->dirty_tracking_flags =
+

IOMMU_HWPT_GET_DIRTY_BITMAP_NO_CLEAR;

Quoted 1 line
hw/vfio/iommufd.c +0 -0
              ret = vfio_container_query_dirty_bitmap(bcontainer,

iova,

Quoted 8 lines
Quoted 8 lines

size,

Quoted 1 line

iotlb->translated_addr,

Quoted 1 line

&local_err);

Quoted 1 line

+ container->dirty_tracking_flags = 0;

Why not changing vfio_container_query_dirty_bitmap to pass a flags too,

like

Quoted 5 lines
Quoted 3 lines

the
original patches? This is a little unnecssary odd style to pass a flag via container structure rather and then clearing.

Just want to be simpler, original patch introduced a new parameter to

almost all

Quoted 2 lines

variants of *_query_dirty_bitmap() while the flags parameter is only used by IOMMUFD backend when doing unmap_bitmap. Currently we already have

three

Quoted 1 line

backends, legacy VFIO, IOMMUFD and VFIO-user, only IOMMUFD need the

flag.

Quoted 2 lines

I take container->dirty_tracking_flags as a notification mechanism, so set it

before

Quoted 1 line

vfio_container_query_dirty_bitmap() and clear it thereafter. Maybe clearing

it in

Quoted 4 lines

iommufd_query_dirty_bitmap() is easier to be acceptable?

Quoted 2 lines

Part of the reason the original series had a VFIO_GET_DIRTY_NO_FLUSH

for

Quoted 8 lines
Quoted 7 lines

generic
container abstraction was to not mix IOMMUFD UAPI specifics into base container
API. Then in getting a VFIO_GET_DIRTY_NO_FLUSH, then type1 backend could just
ignore the flag, while IOMMUFD translates it to IOMMU_HWPT_GET_DIRTY_BITMAP_NO_CLEAR

Quoted 7 lines
Quoted 1 line

I did port original patch

https://github.com/yiliu1765/qemu/commit/99f83595d79d2e4170c9e456cf1
a7b9521bd4f80

Quoted 1 line

But it looks complex to have 'flags' parameter everywhere.

I think I would prefer like Joao to avoid caching information if possible but I haven't check closely the mess it would introduce in the code. Let me check.

OK, waiting for your finial decision, I'm not that eager on a simpler change, I can cherry-pick Joao's change if you prefer it.

Thanks
Zhenzhong

#20 lore raw

RE: [PATCH 3/5] vfio/iommufd: Add IOMMU_HWPT_GET_DIRTY_BITMAP_NO_CLEAR flag support

In reply to Cédric Le Goater

Hi Cédric,

Quoted 182 lines

-----Original Message-----
From: Cédric Le Goater <[email protected]>
Subject: Re: [PATCH 3/5] vfio/iommufd: Add
IOMMU_HWPT_GET_DIRTY_BITMAP_NO_CLEAR flag support

Quoted 9 lines · Zhenzhong

Hi Joao,

Quoted 7 lines

-----Original Message-----
From: Joao Martins <[email protected]>
Subject: Re: [PATCH 3/5] vfio/iommufd: Add
IOMMU_HWPT_GET_DIRTY_BITMAP_NO_CLEAR flag support

Quoted 1 line · Zhenzhong Duan

Pass IOMMU_HWPT_GET_DIRTY_BITMAP_NO_CLEAR when doing the

last

Quoted 7 lines
Quoted 7 lines

dirty

Quoted 6 lines

bitmap query right before unmap, no PTEs flushes. This accelerates the query without issue because unmap will tear down the mapping anyway.

Add a new element dirty_tracking_flags in VFIOIOMMUFDContainer to be used for the flags of iommufd dirty tracking. Currently it is set to either IOMMU_HWPT_GET_DIRTY_BITMAP_NO_CLEAR or 0 based

on

Quoted 34 lines
Quoted 34 lines

include/system/iommufd.h bool

Quoted 32 lines

the scenario.

---
  hw/vfio/vfio-iommufd.h   | 1 +
  include/system/iommufd.h | 2 +-
  backends/iommufd.c       | 5 +++--
  hw/vfio/iommufd.c        | 6 +++++-
  backends/trace-events    | 2 +-
  5 files changed, 11 insertions(+), 5 deletions(-)
hw/vfio/vfio-iommufd.h +1 -0
index 07ea0f4304..e0af241c75 100644
@@ -26,6 +26,7 @@ typedef struct VFIOIOMMUFDContainer {
      VFIOContainerBase bcontainer;
      IOMMUFDBackend *be;
      uint32_t ioas_id;
+    uint64_t dirty_tracking_flags;
      QLIST_HEAD(, VFIOIOASHwpt) hwpt_list;
  } VFIOIOMMUFDContainer;
 
include/system/iommufd.h +0 -0
index c9c72ffc45..63898e7b0d 100644
@@ -64,7 +64,7 @@ bool

iommufd_backend_set_dirty_tracking(IOMMUFDBackend *be, uint32_t hwpt_id,

Quoted 3 lines
Quoted 3 lines

include/system/iommufd.h bool · line 64

Quoted 1 line
include/system/iommufd.h +0 -0
  bool iommufd_backend_get_dirty_bitmap(IOMMUFDBackend *be,

uint32_t hwpt_id,

Quoted 1 line
include/system/iommufd.h +0 -0
                                        uint64_t iova,

ram_addr_t

Quoted 11 lines
Quoted 11 lines

size,

include/system/iommufd.h bool · line 66

Quoted 1 line
include/system/iommufd.h +0 -0
                                        uint64_t page_size,

uint64_t *data,

include/system/iommufd.h bool · line 67

Quoted 2 lines
include/system/iommufd.h +1 -1
-                                      Error **errp);
+                                      uint64_t flags, Error

**errp);

include/system/iommufd.h bool · line 68

Quoted 1 line
include/system/iommufd.h +0 -0
  bool iommufd_backend_invalidate_cache(IOMMUFDBackend *be,

uint32_t id,

include/system/iommufd.h bool · line 69

Quoted 1 line
include/system/iommufd.h +0 -0
                                        uint32_t data_type,

uint32_t entry_len,

Quoted 1 line
include/system/iommufd.h +0 -0
                                        uint32_t *entry_num,

void

Quoted 9 lines
Quoted 9 lines

*data,

backends/iommufd.c bool

Quoted 5 lines
backends/iommufd.c +0 -0
index 2a33c7ab0b..3c4f6157e2 100644
@@ -361,7 +361,7 @@ bool

iommufd_backend_get_dirty_bitmap(IOMMUFDBackend *be,

Quoted 2 lines
backends/iommufd.c +0 -0
                                        uint32_t hwpt_id,
                                        uint64_t iova,

ram_addr_t

Quoted 5 lines
Quoted 5 lines

size,

backends/iommufd.c bool · line 363

Quoted 1 line
backends/iommufd.c +0 -0
                                        uint64_t page_size,

uint64_t *data,

Quoted 2 lines
backends/iommufd.c +1 -1
-                                      Error **errp)
+                                      uint64_t flags, Error

**errp)

Quoted 11 lines
Quoted 11 lines

backends/iommufd.c bool

Quoted 4 lines
backends/iommufd.c +0 -0
  {
      int ret;
      struct iommu_hwpt_get_dirty_bitmap get_dirty_bitmap = {
@@ -371,11 +371,12 @@ bool

iommufd_backend_get_dirty_bitmap(IOMMUFDBackend *be,

Quoted 6 lines
backends/iommufd.c +1 -0
          .length = size,
          .page_size = page_size,
          .data = (uintptr_t)data,
+        .flags = flags,
      };
 
Quoted 3 lines
Quoted 3 lines

backends/iommufd.c bool · line 377

Quoted 1 line
backends/iommufd.c +0 -0
      ret = ioctl(be->fd, IOMMU_HWPT_GET_DIRTY_BITMAP,

&get_dirty_bitmap);

Quoted 1 line
backends/iommufd.c +0 -0
      trace_iommufd_backend_get_dirty_bitmap(be->fd, hwpt_id,

iova,

Quoted 2 lines
Quoted 2 lines

size,

Quoted 1 line
backends/iommufd.c +0 -1
-                                           page_size, ret ?

errno :

Quoted 2 lines
Quoted 2 lines

0);

Quoted 1 line
backends/iommufd.c +1 -0
+                                           flags, page_size,

ret ?

Quoted 4 lines
Quoted 4 lines

errno : 0);

Quoted 3 lines
backends/iommufd.c +0 -0
      if (ret) {
          error_setg_errno(errp, errno,
                           "IOMMU_HWPT_GET_DIRTY_BITMAP
Quoted 8 lines
Quoted 8 lines

(iova: 0x%"HWADDR_PRIx

hw/vfio/iommufd.c static int iommufd_cdev_unmap_one(const

Quoted 5 lines
hw/vfio/iommufd.c +0 -0
index 0057488ce9..c897aa6b17 100644
@@ -62,7 +62,7 @@ static int iommufd_cdev_unmap_one(const

VFIOContainerBase *bcontainer,

Quoted 1 line
hw/vfio/iommufd.c +0 -0
                                    hwaddr iova, ram_addr_t

size,

Quoted 11 lines
Quoted 11 lines

hw/vfio/iommufd.c static int iommufd_cdev_unmap_one(const VFIOContainerBase *bcontainer,

Quoted 5 lines
hw/vfio/iommufd.c +1 -1
                                    IOMMUTLBEntry *iotlb)
  {
-    const VFIOIOMMUFDContainer *container =
+    VFIOIOMMUFDContainer *container =
          container_of(bcontainer, VFIOIOMMUFDContainer,

bcontainer);

hw/vfio/iommufd.c static int iommufd_cdev_unmap_one(const

Quoted 3 lines
hw/vfio/iommufd.c +0 -0
      bool need_dirty_sync = false;
      Error *local_err = NULL;
@@ -73,9 +73,12 @@ static int iommufd_cdev_unmap_one(const

VFIOContainerBase *bcontainer,

Quoted 1 line
hw/vfio/iommufd.c +0 -0
      if (iotlb && vfio_container_dirty_tracking_is_started(bcontainer))

{

Quoted 7 lines
Quoted 7 lines

hw/vfio/iommufd.c static int iommufd_cdev_unmap_one(const · line 74

Quoted 1 line
hw/vfio/iommufd.c +0 -0
          if

(!vfio_container_devices_dirty_tracking_is_supported(bcontainer) &&

hw/vfio/iommufd.c static int iommufd_cdev_unmap_one(const VFIOContainerBase *bcontainer,

Quoted 3 lines
hw/vfio/iommufd.c +2 -0
              bcontainer->dirty_pages_supported) {
+            container->dirty_tracking_flags =
+

IOMMU_HWPT_GET_DIRTY_BITMAP_NO_CLEAR;

Quoted 1 line
hw/vfio/iommufd.c +0 -0
              ret = vfio_container_query_dirty_bitmap(bcontainer,

iova,

Quoted 8 lines
Quoted 8 lines

size,

Quoted 1 line

iotlb->translated_addr,

Quoted 1 line

&local_err);

Quoted 1 line

+ container->dirty_tracking_flags = 0;

Why not changing vfio_container_query_dirty_bitmap to pass a flags too,

like

Quoted 5 lines
Quoted 3 lines

the
original patches? This is a little unnecssary odd style to pass a flag via container structure rather and then clearing.

Just want to be simpler, original patch introduced a new parameter to

almost all

Quoted 2 lines

variants of *_query_dirty_bitmap() while the flags parameter is only used by IOMMUFD backend when doing unmap_bitmap. Currently we already have

three

Quoted 1 line

backends, legacy VFIO, IOMMUFD and VFIO-user, only IOMMUFD need the

flag.

Quoted 2 lines

I take container->dirty_tracking_flags as a notification mechanism, so set it

before

Quoted 1 line

vfio_container_query_dirty_bitmap() and clear it thereafter. Maybe clearing

it in

Quoted 4 lines

iommufd_query_dirty_bitmap() is easier to be acceptable?

Quoted 2 lines

Part of the reason the original series had a VFIO_GET_DIRTY_NO_FLUSH

for

Quoted 8 lines
Quoted 7 lines

generic
container abstraction was to not mix IOMMUFD UAPI specifics into base container
API. Then in getting a VFIO_GET_DIRTY_NO_FLUSH, then type1 backend could just
ignore the flag, while IOMMUFD translates it to IOMMU_HWPT_GET_DIRTY_BITMAP_NO_CLEAR

Quoted 7 lines
Quoted 1 line

I did port original patch

https://github.com/yiliu1765/qemu/commit/99f83595d79d2e4170c9e456cf1
a7b9521bd4f80

Quoted 1 line

But it looks complex to have 'flags' parameter everywhere.

I think I would prefer like Joao to avoid caching information if possible but I haven't check closely the mess it would introduce in the code. Let me check.

Kindly ping. Just in case you have further comments on this.

Thanks
Zhenzhong

#21 lore raw

Re: [PATCH 3/5] vfio/iommufd: Add IOMMU_HWPT_GET_DIRTY_BITMAP_NO_CLEAR flag support

Hello Zhenzhong, Joao,

Quoted 185 lines · Zhenzhong

Hi Cédric,

Quoted 183 lines

-----Original Message-----
From: Cédric Le Goater <[email protected]>
Subject: Re: [PATCH 3/5] vfio/iommufd: Add
IOMMU_HWPT_GET_DIRTY_BITMAP_NO_CLEAR flag support

Quoted 9 lines · Zhenzhong

Hi Joao,

Quoted 7 lines

-----Original Message-----
From: Joao Martins <[email protected]>
Subject: Re: [PATCH 3/5] vfio/iommufd: Add
IOMMU_HWPT_GET_DIRTY_BITMAP_NO_CLEAR flag support

Quoted 1 line · Zhenzhong Duan

Pass IOMMU_HWPT_GET_DIRTY_BITMAP_NO_CLEAR when doing the

last

Quoted 7 lines
Quoted 7 lines

dirty

Quoted 6 lines

bitmap query right before unmap, no PTEs flushes. This accelerates the query without issue because unmap will tear down the mapping anyway.

Add a new element dirty_tracking_flags in VFIOIOMMUFDContainer to be used for the flags of iommufd dirty tracking. Currently it is set to either IOMMU_HWPT_GET_DIRTY_BITMAP_NO_CLEAR or 0 based

on

Quoted 34 lines
Quoted 34 lines

include/system/iommufd.h bool

Quoted 32 lines

the scenario.

---
   hw/vfio/vfio-iommufd.h   | 1 +
   include/system/iommufd.h | 2 +-
   backends/iommufd.c       | 5 +++--
   hw/vfio/iommufd.c        | 6 +++++-
   backends/trace-events    | 2 +-
   5 files changed, 11 insertions(+), 5 deletions(-)
hw/vfio/vfio-iommufd.h +1 -0
index 07ea0f4304..e0af241c75 100644
@@ -26,6 +26,7 @@ typedef struct VFIOIOMMUFDContainer {
       VFIOContainerBase bcontainer;
       IOMMUFDBackend *be;
       uint32_t ioas_id;
+    uint64_t dirty_tracking_flags;
       QLIST_HEAD(, VFIOIOASHwpt) hwpt_list;
   } VFIOIOMMUFDContainer;
 
include/system/iommufd.h +0 -0
index c9c72ffc45..63898e7b0d 100644
@@ -64,7 +64,7 @@ bool

iommufd_backend_set_dirty_tracking(IOMMUFDBackend *be, uint32_t hwpt_id,

Quoted 3 lines
Quoted 3 lines

include/system/iommufd.h bool · line 64

Quoted 1 line
include/system/iommufd.h +0 -0
   bool iommufd_backend_get_dirty_bitmap(IOMMUFDBackend *be,

uint32_t hwpt_id,

Quoted 1 line
include/system/iommufd.h +0 -0
                                         uint64_t iova,

ram_addr_t

Quoted 11 lines
Quoted 11 lines

size,

include/system/iommufd.h bool · line 66

Quoted 1 line
include/system/iommufd.h +0 -0
                                         uint64_t page_size,

uint64_t *data,

include/system/iommufd.h bool · line 67

Quoted 2 lines
include/system/iommufd.h +1 -1
-                                      Error **errp);
+                                      uint64_t flags, Error

**errp);

include/system/iommufd.h bool · line 68

Quoted 1 line
include/system/iommufd.h +0 -0
   bool iommufd_backend_invalidate_cache(IOMMUFDBackend *be,

uint32_t id,

include/system/iommufd.h bool · line 69

Quoted 1 line
include/system/iommufd.h +0 -0
                                         uint32_t data_type,

uint32_t entry_len,

Quoted 1 line
include/system/iommufd.h +0 -0
                                         uint32_t *entry_num,

void

Quoted 9 lines
Quoted 9 lines

*data,

backends/iommufd.c bool

Quoted 5 lines
backends/iommufd.c +0 -0
index 2a33c7ab0b..3c4f6157e2 100644
@@ -361,7 +361,7 @@ bool

iommufd_backend_get_dirty_bitmap(IOMMUFDBackend *be,

Quoted 2 lines
backends/iommufd.c +0 -0
                                         uint32_t hwpt_id,
                                         uint64_t iova,

ram_addr_t

Quoted 5 lines
Quoted 5 lines

size,

backends/iommufd.c bool · line 363

Quoted 1 line
backends/iommufd.c +0 -0
                                         uint64_t page_size,

uint64_t *data,

Quoted 2 lines
backends/iommufd.c +1 -1
-                                      Error **errp)
+                                      uint64_t flags, Error

**errp)

Quoted 11 lines
Quoted 11 lines

backends/iommufd.c bool

Quoted 4 lines
backends/iommufd.c +0 -0
   {
       int ret;
       struct iommu_hwpt_get_dirty_bitmap get_dirty_bitmap = {
@@ -371,11 +371,12 @@ bool

iommufd_backend_get_dirty_bitmap(IOMMUFDBackend *be,

Quoted 6 lines
backends/iommufd.c +1 -0
           .length = size,
           .page_size = page_size,
           .data = (uintptr_t)data,
+        .flags = flags,
       };
 
Quoted 3 lines
Quoted 3 lines

backends/iommufd.c bool · line 377

Quoted 1 line
backends/iommufd.c +0 -0
       ret = ioctl(be->fd, IOMMU_HWPT_GET_DIRTY_BITMAP,

&get_dirty_bitmap);

Quoted 1 line
backends/iommufd.c +0 -0
       trace_iommufd_backend_get_dirty_bitmap(be->fd, hwpt_id,

iova,

Quoted 2 lines
Quoted 2 lines

size,

Quoted 1 line
backends/iommufd.c +0 -1
-                                           page_size, ret ?

errno :

Quoted 2 lines
Quoted 2 lines

0);

Quoted 1 line
backends/iommufd.c +1 -0
+                                           flags, page_size,

ret ?

Quoted 4 lines
Quoted 4 lines

errno : 0);

Quoted 3 lines
backends/iommufd.c +0 -0
       if (ret) {
           error_setg_errno(errp, errno,
                            "IOMMU_HWPT_GET_DIRTY_BITMAP
Quoted 8 lines
Quoted 8 lines

(iova: 0x%"HWADDR_PRIx

hw/vfio/iommufd.c static int iommufd_cdev_unmap_one(const

Quoted 5 lines
hw/vfio/iommufd.c +0 -0
index 0057488ce9..c897aa6b17 100644
@@ -62,7 +62,7 @@ static int iommufd_cdev_unmap_one(const

VFIOContainerBase *bcontainer,

Quoted 1 line
hw/vfio/iommufd.c +0 -0
                                     hwaddr iova, ram_addr_t

size,

Quoted 11 lines
Quoted 11 lines

hw/vfio/iommufd.c static int iommufd_cdev_unmap_one(const VFIOContainerBase *bcontainer,

Quoted 5 lines
hw/vfio/iommufd.c +1 -1
                                     IOMMUTLBEntry *iotlb)
   {
-    const VFIOIOMMUFDContainer *container =
+    VFIOIOMMUFDContainer *container =
           container_of(bcontainer, VFIOIOMMUFDContainer,

bcontainer);

hw/vfio/iommufd.c static int iommufd_cdev_unmap_one(const

Quoted 3 lines
hw/vfio/iommufd.c +0 -0
       bool need_dirty_sync = false;
       Error *local_err = NULL;
@@ -73,9 +73,12 @@ static int iommufd_cdev_unmap_one(const

VFIOContainerBase *bcontainer,

Quoted 1 line
hw/vfio/iommufd.c +0 -0
       if (iotlb && vfio_container_dirty_tracking_is_started(bcontainer))

{

Quoted 7 lines
Quoted 7 lines

hw/vfio/iommufd.c static int iommufd_cdev_unmap_one(const · line 74

Quoted 1 line
hw/vfio/iommufd.c +0 -0
           if

(!vfio_container_devices_dirty_tracking_is_supported(bcontainer) &&

hw/vfio/iommufd.c static int iommufd_cdev_unmap_one(const VFIOContainerBase *bcontainer,

Quoted 3 lines
hw/vfio/iommufd.c +2 -0
               bcontainer->dirty_pages_supported) {
+            container->dirty_tracking_flags =
+

IOMMU_HWPT_GET_DIRTY_BITMAP_NO_CLEAR;

Quoted 1 line
hw/vfio/iommufd.c +0 -0
               ret = vfio_container_query_dirty_bitmap(bcontainer,

iova,

Quoted 8 lines
Quoted 8 lines

size,

Quoted 1 line

iotlb->translated_addr,

Quoted 1 line

&local_err);

Quoted 1 line

+ container->dirty_tracking_flags = 0;

Why not changing vfio_container_query_dirty_bitmap to pass a flags too,

like

Quoted 5 lines
Quoted 3 lines

the
original patches? This is a little unnecssary odd style to pass a flag via container structure rather and then clearing.

Just want to be simpler, original patch introduced a new parameter to

almost all

Quoted 2 lines

variants of *_query_dirty_bitmap() while the flags parameter is only used by IOMMUFD backend when doing unmap_bitmap. Currently we already have

three

Quoted 1 line

backends, legacy VFIO, IOMMUFD and VFIO-user, only IOMMUFD need the

flag.

Quoted 2 lines

I take container->dirty_tracking_flags as a notification mechanism, so set it

before

Quoted 1 line

vfio_container_query_dirty_bitmap() and clear it thereafter. Maybe clearing

it in

Quoted 4 lines

iommufd_query_dirty_bitmap() is easier to be acceptable?

Quoted 2 lines

Part of the reason the original series had a VFIO_GET_DIRTY_NO_FLUSH

for

Quoted 9 lines
Quoted 7 lines

generic
container abstraction was to not mix IOMMUFD UAPI specifics into base container
API. Then in getting a VFIO_GET_DIRTY_NO_FLUSH, then type1 backend could just
ignore the flag, while IOMMUFD translates it to IOMMU_HWPT_GET_DIRTY_BITMAP_NO_CLEAR

I did port original patch

Quoted 8 lines
Quoted 6 lines

https://github.com/yiliu1765/qemu/commit/99f83595d79d2e4170c9e456cf1
a7b9521bd4f80

Quoted 1 line

But it looks complex to have 'flags' parameter everywhere.

I think I would prefer like Joao to avoid caching information if possible but I haven't check closely the mess it would introduce in the code. Let me check.

Kindly ping. Just in case you have further comments on this.

Regarding the whole series,

* [1/5] vfio/iommufd: Add framework code to support getting dirty bitmap before unmap

Needs refactoring. iommufd_cdev_unmap_one() seems superfluous now ?

* [2/5] vfio/iommufd: Query dirty bitmap before DMA unmap

Looks OK.

In an extra patch, could we rename 'vfio_dma_unmap_bitmap()' to 'vfio_legacy_dma_unmap_get_dirty_bitmap()' ? Helps (me) remember what it does.

* [3/5] vfio/iommufd: Add IOMMU_HWPT_GET_DIRTY_BITMAP_NO_CLEAR flag support

Sorry, I lost track of the discussion between you and Joao regarding the new '->dirty_tracking_flags' attribute.

I'd prefer adding a 'backend_flag' parameter, even if it’s currently only used by IOMMUFD. Since we’re not going to redefine all possible IOMMU backend flags, we can treat it as an opaque parameter for the upper container layer and document it as such.

Is that ok for you ? A bit more churn for you but Joao did provide some parts.

* [4/5] intel_iommu: Optimize unmap_bitmap during migration

Ack by Clément may be ?

* [5/5] vfio/migration: Allow live migration with vIOMMU without VFs using device dirty tracking

ok.

Thanks,

C.

#22 lore raw

RE: [PATCH 3/5] vfio/iommufd: Add IOMMU_HWPT_GET_DIRTY_BITMAP_NO_CLEAR flag support

Quoted 11 lines

-----Original Message-----
From: Cédric Le Goater <[email protected]>
Regarding the whole series,

* [1/5] vfio/iommufd: Add framework code to support getting dirty bitmap before unmap

Needs refactoring. iommufd_cdev_unmap_one() seems superfluous
now ?

Yes.

Quoted 8 lines

* [2/5] vfio/iommufd: Query dirty bitmap before DMA unmap

Looks OK.

In an extra patch, could we rename 'vfio_dma_unmap_bitmap()' to 'vfio_legacy_dma_unmap_get_dirty_bitmap()' ? Helps (me) remember what it does.

Sure

Quoted 7 lines

* [3/5] vfio/iommufd: Add IOMMU_HWPT_GET_DIRTY_BITMAP_NO_CLEAR flag support

Sorry, I lost track of the discussion between you and Joao regarding the new '->dirty_tracking_flags' attribute.

Quoted 7 lines

I'd prefer adding a 'backend_flag' parameter, even if it’s currently only used by IOMMUFD. Since we’re not going to redefine all possible IOMMU backend flags, we can treat it as an opaque parameter for the upper container layer and document it as such.

Is that ok for you ? A bit more churn for you but Joao did provide some parts.

Sure, will cherry-pick Joao's change.

Quoted 4 lines

* [4/5] intel_iommu: Optimize unmap_bitmap during migration

Ack by Clément may be ?

Yes

Thanks
Zhenzhong

Quoted 10 lines

* [5/5] vfio/migration: Allow live migration with vIOMMU without VFs using device dirty tracking

ok.

Thanks,

C.

#23 lore raw

PATCH 4/5 intel_iommu: Optimize unmap_bitmap during migration

In reply to Zhenzhong Duan

If 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.

There is no such limitation with iommufd backend, but it's still not optimal to allocate large bitmap.

Optimize it 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
index 83c5e44413..6876dae727 100644
@@ -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"
 
@@ -4423,6 +4424,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 meaningless for unmap */
+        event.entry.translated_addr = 0;
+        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)
 {
@@ -4432,6 +4469,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
#24 lore raw

Re: [PATCH 4/5] intel_iommu: Optimize unmap_bitmap during migration

Quoted 7 lines · Zhenzhong Duan

If 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))

this should be a giant and busy VM. right? Is a fix tag needed by the way?

Quoted 45 lines

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.

There is no such limitation with iommufd backend, but it's still not optimal to allocate large bitmap.

Optimize it 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 +12 -0
index 83c5e44413..6876dae727 100644
@@ -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"
 
@@ -4423,6 +4424,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,

hw/i386/intel_iommu.c static void vtd_dev_unset_iommu_device(PCIBus *bus, void *opaque, int devfn) · line 4447

Quoted 10 lines
hw/i386/intel_iommu.c +10 -0
+        .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))) {

how about an empty iova_tree? If guest has not mapped anything for the
device, the tree is empty. And it is fine to not unmap anyting. While,
if the device is attached to an identify domain, the iova_tree is empty
as well. Are we sure that we need not to unmap anything here? It looks
the answer is yes. But I'm suspecting the unmap failure will happen in the vfio side? If yes, need to consider a complete fix. :)

Quoted 19 lines
hw/i386/intel_iommu.c +15 -0
+        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 meaningless for unmap */
+        event.entry.translated_addr = 0;
+        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 vtd_address_space_unmap(VTDAddressSpace *as, IOMMUNotifier *n) · line 4472

Quoted 4 lines
hw/i386/intel_iommu.c +1 -0
@@ -4432,6 +4469,11 @@ static void vtd_address_space_unmap(VTDAddressSpace *as, IOMMUNotifier *n)
      IntelIOMMUState *s = as->iommu_state;
      DMAMap map;
 
+    if (migration_is_running()) {

If the range is not big enough, it is still better to unmap in one-go. right? If so, might add a check on the range here to go to the iova_tee iteration conditionally.

hw/i386/intel_iommu.c static void vtd_address_space_unmap(VTDAddressSpace *as, IOMMUNotifier *n) · line 4479

Quoted 7 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_MGAW bits (which is restricted by

Regards,
Yi Liu

#25 lore raw

RE: [PATCH 4/5] intel_iommu: Optimize unmap_bitmap during migration

Quoted 11 lines

-----Original Message-----
From: Liu, Yi L <[email protected]>
Subject: Re: [PATCH 4/5] intel_iommu: Optimize unmap_bitmap during migration

Quoted 3 lines · Zhenzhong Duan

If a VFIO device in guest switches from IOMMU domain to block domain, vtd_address_space_unmap() is called to unmap whole address space.

Quoted 7 lines
Quoted 4 lines

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))

this should be a giant and busy VM. right? Is a fix tag needed by the way?

VM size is unrelated, it's not a bug, just current code doesn't work well with migration.

When device switches from IOMMU domain to block domain, the whole iommu memory region is disabled, this trigger the unmap on the whole iommu memory region, no matter how many or how large the mappings are in the iommu MR.

Quoted 67 lines
Quoted 3 lines

Because legacy VFIO limits maximum bitmap size to 256MB which maps to

8TB on

Quoted 1 line

4K page system, when 16TB sized UNMAP notification is sent,

unmap_bitmap

Quoted 5 lines

ioctl fails.

There is no such limitation with iommufd backend, but it's still not optimal to allocate large bitmap.

Quoted 8 lines

Optimize it 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

++++++++++++++++++++++++++++++++++++++++++

hw/i386/intel_iommu.c static void

Quoted 15 lines
  1 file changed, 42 insertions(+)
hw/i386/intel_iommu.c +1 -0
index 83c5e44413..6876dae727 100644
@@ -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"
 
@@ -4423,6 +4424,42 @@ static void

vtd_dev_unset_iommu_device(PCIBus *bus, void *opaque, int devfn)

hw/i386/intel_iommu.c static void vtd_dev_unset_iommu_device(PCIBus *bus, void *opaque, int devfn)

Quoted 5 lines
hw/i386/intel_iommu.c +2 -0
      vtd_iommu_unlock(s);
  }
 
+/*
+ * Unmapping a large range in one go is not optimal during migration

because

hw/i386/intel_iommu.c static void · line 4430

Quoted 2 lines
hw/i386/intel_iommu.c +2 -0
+ * 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 static void · line 4432

Quoted 2 lines
hw/i386/intel_iommu.c +2 -0
+ */
+static void vtd_address_space_unmap_in_migration(VTDAddressSpace

*as,

hw/i386/intel_iommu.c static void · line 4433

Quoted 1 line
hw/i386/intel_iommu.c +1 -0
+

IOMMUNotifier *n)

hw/i386/intel_iommu.c static void vtd_dev_unset_iommu_device(PCIBus *bus, void *opaque, int devfn)

Quoted 10 lines
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

hw/i386/intel_iommu.c static void · line 4444

Quoted 1 line
hw/i386/intel_iommu.c +1 -0
+     * page size and always a power of 2 in size. So the range of

DMAMap could

hw/i386/intel_iommu.c static void vtd_dev_unset_iommu_device(PCIBus *bus, void *opaque, int devfn) · line 4447

Quoted 3 lines
hw/i386/intel_iommu.c +3 -0
+     * be used for UNMAP notification directly.
+     */
+    while ((map = iova_tree_find(tree, &target))) {
Quoted 6 lines

how about an empty iova_tree? If guest has not mapped anything for the device, the tree is empty. And it is fine to not unmap anyting. While, if the device is attached to an identify domain, the iova_tree is empty as well. Are we sure that we need not to unmap anything here? It looks the answer is yes. But I'm suspecting the unmap failure will happen in the vfio side? If yes, need to consider a complete fix. :)

Not get what failure will happen, could you elaborate?
In case of identity domain, IOMMU memory region is disabled, no iommu notifier will ever be triggered. vfio_listener monitors memory address space, if any memory region is disabled, vfio_listener will catch it and do dirty tracking.

Quoted 22 lines
Quoted 8 lines
hw/i386/intel_iommu.c +7 -0
+        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;

hw/i386/intel_iommu.c static void vtd_dev_unset_iommu_device(PCIBus *bus, void *opaque, int devfn)

Quoted 10 lines
hw/i386/intel_iommu.c +8 -0
+        /* This field is meaningless for unmap */
+        event.entry.translated_addr = 0;
+        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

Quoted 2 lines
hw/i386/intel_iommu.c +0 -0
  {
@@ -4432,6 +4469,11 @@ static void

vtd_address_space_unmap(VTDAddressSpace *as, IOMMUNotifier *n)

Quoted 8 lines

hw/i386/intel_iommu.c static void vtd_address_space_unmap(VTDAddressSpace *as, IOMMUNotifier *n) · line 4472

Quoted 4 lines
hw/i386/intel_iommu.c +1 -0
      IntelIOMMUState *s = as->iommu_state;
      DMAMap map;
 
+    if (migration_is_running()) {

If the range is not big enough, it is still better to unmap in one-go. right? If so, might add a check on the range here to go to the iova_tee iteration conditionally.

We don't want to ditry track IOVA holes between IOVA ranges because it's time consuming and useless work. The hole may be large depending on guest behavior. Meanwhile the time iterating on iova_tree is trivial. So we prefer tracking the exact iova ranges that may be dirty actually.

Thanks
Zhenzhong

#26 lore raw

Re: [PATCH 4/5] intel_iommu: Optimize unmap_bitmap during migration

Quoted 17 lines · Zhenzhong
Quoted 17 lines

-----Original Message-----
From: Liu, Yi L <[email protected]>
Subject: Re: [PATCH 4/5] intel_iommu: Optimize unmap_bitmap during migration

Quoted 7 lines · Zhenzhong Duan

If 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))

Quoted 7 lines
Quoted 1 line

this should be a giant and busy VM. right? Is a fix tag needed by the way?

VM size is unrelated, it's not a bug, just current code doesn't work well with migration.

When device switches from IOMMU domain to block domain, the whole iommu memory region is disabled, this trigger the unmap on the whole iommu memory region,

I got this part.

Quoted 1 line

no matter how many or how large the mappings are in the iommu MR.

hmmm. A more explicit question: does this error happen with 4G VM memory as well?

Quoted 74 lines
Quoted 67 lines
Quoted 3 lines

Because legacy VFIO limits maximum bitmap size to 256MB which maps to

8TB on

Quoted 1 line

4K page system, when 16TB sized UNMAP notification is sent,

unmap_bitmap

Quoted 5 lines

ioctl fails.

There is no such limitation with iommufd backend, but it's still not optimal to allocate large bitmap.

Quoted 8 lines

Optimize it 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

++++++++++++++++++++++++++++++++++++++++++

hw/i386/intel_iommu.c static void

Quoted 15 lines
   1 file changed, 42 insertions(+)
hw/i386/intel_iommu.c +1 -0
index 83c5e44413..6876dae727 100644
@@ -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"
 
@@ -4423,6 +4424,42 @@ static void

vtd_dev_unset_iommu_device(PCIBus *bus, void *opaque, int devfn)

hw/i386/intel_iommu.c static void vtd_dev_unset_iommu_device(PCIBus *bus, void *opaque, int devfn)

Quoted 5 lines
hw/i386/intel_iommu.c +2 -0
       vtd_iommu_unlock(s);
   }
 
+/*
+ * Unmapping a large range in one go is not optimal during migration

because

hw/i386/intel_iommu.c static void · line 4430

Quoted 2 lines
hw/i386/intel_iommu.c +2 -0
+ * 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 static void · line 4432

Quoted 2 lines
hw/i386/intel_iommu.c +2 -0
+ */
+static void vtd_address_space_unmap_in_migration(VTDAddressSpace

*as,

hw/i386/intel_iommu.c static void · line 4433

Quoted 1 line
hw/i386/intel_iommu.c +1 -0
+

IOMMUNotifier *n)

hw/i386/intel_iommu.c static void vtd_dev_unset_iommu_device(PCIBus *bus, void *opaque, int devfn)

Quoted 10 lines
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

hw/i386/intel_iommu.c static void · line 4444

Quoted 1 line
hw/i386/intel_iommu.c +1 -0
+     * page size and always a power of 2 in size. So the range of

DMAMap could

hw/i386/intel_iommu.c static void vtd_dev_unset_iommu_device(PCIBus *bus, void *opaque, int devfn) · line 4447

Quoted 3 lines
hw/i386/intel_iommu.c +3 -0
+     * be used for UNMAP notification directly.
+     */
+    while ((map = iova_tree_find(tree, &target))) {
Quoted 6 lines

how about an empty iova_tree? If guest has not mapped anything for the device, the tree is empty. And it is fine to not unmap anyting. While, if the device is attached to an identify domain, the iova_tree is empty as well. Are we sure that we need not to unmap anything here? It looks the answer is yes. But I'm suspecting the unmap failure will happen in the vfio side? If yes, need to consider a complete fix. :)

Quoted 4 lines

Not get what failure will happen, could you elaborate?
In case of identity domain, IOMMU memory region is disabled, no iommu notifier will ever be triggered. vfio_listener monitors memory address space, if any memory region is disabled, vfio_listener will catch it and do dirty tracking.

My question comes from the reason why DMA unmap fails. It is due to a big range is given to kernel while kernel does not support. So if VFIO gives a big range as well, it should fail as well. And this is possible when guest (a VM with large size memory) switches from identify domain to a paging domain. In this case, vfio_listener will unmap all the system MRs. And it can be a big range if VM size is big enough.

Quoted 25 lines
Quoted 25 lines
Quoted 8 lines
hw/i386/intel_iommu.c +7 -0
+        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;

hw/i386/intel_iommu.c static void vtd_dev_unset_iommu_device(PCIBus *bus, void *opaque, int devfn)

Quoted 10 lines
hw/i386/intel_iommu.c +8 -0
+        /* This field is meaningless for unmap */
+        event.entry.translated_addr = 0;
+        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

Quoted 2 lines
hw/i386/intel_iommu.c +0 -0
   {
@@ -4432,6 +4469,11 @@ static void

vtd_address_space_unmap(VTDAddressSpace *as, IOMMUNotifier *n)

Quoted 3 lines
hw/i386/intel_iommu.c +0 -0
       IntelIOMMUState *s = as->iommu_state;
       DMAMap map;
 
Quoted 8 lines
Quoted 5 lines

hw/i386/intel_iommu.c static void vtd_address_space_unmap(VTDAddressSpace *as, IOMMUNotifier *n) · line 4472

Quoted 1 line
hw/i386/intel_iommu.c +1 -0
+    if (migration_is_running()) {

If the range is not big enough, it is still better to unmap in one-go. right? If so, might add a check on the range here to go to the iova_tee iteration conditionally.

We don't want to ditry track IOVA holes between IOVA ranges because it's time consuming and useless work. The hole may be large depending on guest behavior. Meanwhile the time iterating on iova_tree is trivial. So we prefer tracking the exact iova ranges that may be dirty actually.

I see. So this is the optimization. And it also WA the above DMA unmap issue as well. right? If so, you may want to call out in the commit message.

Regards,
Yi Liu

#27 lore raw

RE: [PATCH 4/5] intel_iommu: Optimize unmap_bitmap during migration

Quoted 34 lines

-----Original Message-----
From: Liu, Yi L <[email protected]>
Subject: Re: [PATCH 4/5] intel_iommu: Optimize unmap_bitmap during migration

Quoted 14 lines · Zhenzhong
Quoted 14 lines

-----Original Message-----
From: Liu, Yi L <[email protected]>
Subject: Re: [PATCH 4/5] intel_iommu: Optimize unmap_bitmap during migration

Quoted 6 lines · Zhenzhong Duan

If 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:

Quoted 6 lines
Quoted 4 lines
Quoted 1 line

Status: failed (vfio_container_dma_unmap(0x561bbbd92d90,

0x100000000000, 0x100000000000) = -7 (Argument list too long))

this should be a giant and busy VM. right? Is a fix tag needed by the way?

VM size is unrelated, it's not a bug, just current code doesn't work well with

migration.

Quoted 2 lines

When device switches from IOMMU domain to block domain, the whole

iommu

Quoted 1 line

memory region is disabled, this trigger the unmap on the whole iommu

memory

Quoted 8 lines
Quoted 1 line

region,

I got this part.

Quoted 1 line

no matter how many or how large the mappings are in the iommu MR.

hmmm. A more explicit question: does this error happen with 4G VM memory as well?

Coincidently, I remember QAT team reported this issue just with 4G VM memory.

Quoted 85 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

Because legacy VFIO limits maximum bitmap size to 256MB which maps

to

Quoted 9 lines
Quoted 9 lines

8TB on

Quoted 1 line

4K page system, when 16TB sized UNMAP notification is sent,

unmap_bitmap

Quoted 6 lines

ioctl fails.

There is no such limitation with iommufd backend, but it's still not optimal to allocate large bitmap.

Optimize it by iterating over DMAMap list to unmap each range with

active

Quoted 1 line
Quoted 1 line
Quoted 1 line

mapping when migration is active. If migration is not active, unmapping

the

Quoted 23 lines
Quoted 23 lines
Quoted 6 lines

whole address space in one go is optimal.

---
   hw/i386/intel_iommu.c | 42

++++++++++++++++++++++++++++++++++++++++++

hw/i386/intel_iommu.c static void

Quoted 15 lines
   1 file changed, 42 insertions(+)
hw/i386/intel_iommu.c +1 -0
index 83c5e44413..6876dae727 100644
@@ -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"
 
@@ -4423,6 +4424,42 @@ static void

vtd_dev_unset_iommu_device(PCIBus *bus, void *opaque, int devfn)

Quoted 7 lines
Quoted 7 lines

hw/i386/intel_iommu.c static void vtd_dev_unset_iommu_device(PCIBus *bus, void *opaque, int devfn)

Quoted 5 lines
hw/i386/intel_iommu.c +2 -0
       vtd_iommu_unlock(s);
   }
 
+/*
+ * Unmapping a large range in one go is not optimal during migration

because

Quoted 1 line
hw/i386/intel_iommu.c +1 -0
+ * a large dirty bitmap needs to be allocated while there may be only

small

Quoted 7 lines
Quoted 7 lines

hw/i386/intel_iommu.c static void · line 4430

Quoted 1 line
hw/i386/intel_iommu.c +1 -0
+ * mappings, iterate over DMAMap list to unmap each range with active

mapping.

hw/i386/intel_iommu.c static void · line 4432

Quoted 2 lines
hw/i386/intel_iommu.c +2 -0
+ */
+static void vtd_address_space_unmap_in_migration(VTDAddressSpace

*as,

hw/i386/intel_iommu.c static void · line 4433

Quoted 1 line
hw/i386/intel_iommu.c +1 -0
+

IOMMUNotifier *n)

Quoted 10 lines
Quoted 10 lines

hw/i386/intel_iommu.c static void vtd_dev_unset_iommu_device(PCIBus *bus, void *opaque, int devfn)

Quoted 10 lines
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

Quoted 14 lines
Quoted 7 lines

or huge

hw/i386/intel_iommu.c static void · line 4444

Quoted 1 line
hw/i386/intel_iommu.c +1 -0
+     * page size and always a power of 2 in size. So the range of

DMAMap could

hw/i386/intel_iommu.c static void vtd_dev_unset_iommu_device(PCIBus *bus, void *opaque, int devfn) · line 4447

Quoted 3 lines
hw/i386/intel_iommu.c +3 -0
+     * be used for UNMAP notification directly.
+     */
+    while ((map = iova_tree_find(tree, &target))) {
Quoted 6 lines

how about an empty iova_tree? If guest has not mapped anything for the device, the tree is empty. And it is fine to not unmap anyting. While, if the device is attached to an identify domain, the iova_tree is empty as well. Are we sure that we need not to unmap anything here? It looks the answer is yes. But I'm suspecting the unmap failure will happen in the vfio side? If yes, need to consider a complete fix. :)

Quoted 3 lines

Not get what failure will happen, could you elaborate?
In case of identity domain, IOMMU memory region is disabled, no iommu notifier will ever be triggered. vfio_listener monitors memory address

space,

Quoted 1 line

if any memory region is disabled, vfio_listener will catch it and do dirty

Quoted 8 lines

tracking.

My question comes from the reason why DMA unmap fails. It is due to a big range is given to kernel while kernel does not support. So if VFIO gives a big range as well, it should fail as well. And this is possible when guest (a VM with large size memory) switches from identify domain to a paging domain. In this case, vfio_listener will unmap all the system MRs. And it can be a big range if VM size is big enough.

Got you point. Yes, currently vfio_type1 driver limits unmap_bitmap to 8TB size. If guest memory is large enough and lead to a memory region of more than 8TB size, unmap_bitmap will fail. It's a rare case to live migrate VM with more than 8TB memory, instead of fixing it in qemu with complex change, I'd suggest to bump below MACRO value to enlarge the limit in kernel, or switch to use iommufd which doesn't have such limit.

/*
* Input argument of number of bits to bitmap_set() is unsigned integer, which
* further casts to signed integer for unaligned multi-bit operation,
* __bitmap_set().
* Then maximum bitmap size supported is 2^31 bits divided by 2^3 bits/byte,
* that is 2^28 (256 MB) which maps to 2^31 * 2^12 = 2^43 (8TB) on 4K page
* system.
*/
#define DIRTY_BITMAP_PAGES_MAX ((u64)INT_MAX)
#define DIRTY_BITMAP_SIZE_MAX DIRTY_BITMAP_BYTES(DIRTY_BITMAP_PAGES_MAX)

Quoted 33 lines
Quoted 26 lines
Quoted 26 lines
Quoted 9 lines
hw/i386/intel_iommu.c +7 -0
+        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;

hw/i386/intel_iommu.c static void vtd_dev_unset_iommu_device(PCIBus *bus, void *opaque, int devfn)

Quoted 10 lines
hw/i386/intel_iommu.c +8 -0
+        /* This field is meaningless for unmap */
+        event.entry.translated_addr = 0;
+        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

Quoted 2 lines
hw/i386/intel_iommu.c +0 -0
   {
@@ -4432,6 +4469,11 @@ static void

vtd_address_space_unmap(VTDAddressSpace *as, IOMMUNotifier *n)

Quoted 3 lines
hw/i386/intel_iommu.c +0 -0
       IntelIOMMUState *s = as->iommu_state;
       DMAMap map;
 
Quoted 7 lines
Quoted 5 lines

hw/i386/intel_iommu.c static void vtd_address_space_unmap(VTDAddressSpace *as, IOMMUNotifier *n) · line 4472

Quoted 1 line
hw/i386/intel_iommu.c +1 -0
+    if (migration_is_running()) {

If the range is not big enough, it is still better to unmap in one-go. right? If so, might add a check on the range here to go to the iova_tee iteration conditionally.

We don't want to ditry track IOVA holes between IOVA ranges because it's

Quoted 8 lines

time consuming and useless work. The hole may be large depending on guest behavior.

Quoted 1 line

Meanwhile the time iterating on iova_tree is trivial. So we prefer tracking

the exact iova ranges that may be dirty actually.

I see. So this is the optimization. And it also WA the above DMA unmap issue as well. right? If so, you may want to call out in the commit message.

Yes, the main purpose of this patch is to fix the unmap_bitmap issue, then the optimization. I'll rephrase the description and subject.

Thanks
Zhenzhong

#28 lore raw

Re: [PATCH 4/5] intel_iommu: Optimize unmap_bitmap during migration

Quoted 36 lines · Zhenzhong
Quoted 36 lines

-----Original Message-----
From: Liu, Yi L <[email protected]>
Subject: Re: [PATCH 4/5] intel_iommu: Optimize unmap_bitmap during migration

Quoted 14 lines · Zhenzhong
Quoted 14 lines

-----Original Message-----
From: Liu, Yi L <[email protected]>
Subject: Re: [PATCH 4/5] intel_iommu: Optimize unmap_bitmap during migration

Quoted 6 lines · Zhenzhong Duan

If 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:

Quoted 6 lines
Quoted 4 lines
Quoted 1 line

Status: failed (vfio_container_dma_unmap(0x561bbbd92d90,

0x100000000000, 0x100000000000) = -7 (Argument list too long))

this should be a giant and busy VM. right? Is a fix tag needed by the way?

VM size is unrelated, it's not a bug, just current code doesn't work well with

migration.

Quoted 2 lines

When device switches from IOMMU domain to block domain, the whole

iommu

Quoted 1 line

memory region is disabled, this trigger the unmap on the whole iommu

memory

Quoted 1 line

region,

Quoted 8 lines
Quoted 6 lines

I got this part.

Quoted 1 line

no matter how many or how large the mappings are in the iommu MR.

hmmm. A more explicit question: does this error happen with 4G VM memory as well?

Coincidently, I remember QAT team reported this issue just with 4G VM memory.

ok. this might happen with legacy vIOMMU as guest triggers map/unmap. It can be a large range. But it's still not clear to me how can guest map a range more than 4G if VM only has 4G memory.

Quoted 95 lines
Quoted 86 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

Because legacy VFIO limits maximum bitmap size to 256MB which maps

to

Quoted 9 lines
Quoted 9 lines

8TB on

Quoted 1 line

4K page system, when 16TB sized UNMAP notification is sent,

unmap_bitmap

Quoted 6 lines

ioctl fails.

There is no such limitation with iommufd backend, but it's still not optimal to allocate large bitmap.

Optimize it by iterating over DMAMap list to unmap each range with

active

Quoted 1 line
Quoted 1 line
Quoted 1 line

mapping when migration is active. If migration is not active, unmapping

the

Quoted 23 lines
Quoted 23 lines
Quoted 6 lines

whole address space in one go is optimal.

---
    hw/i386/intel_iommu.c | 42

++++++++++++++++++++++++++++++++++++++++++

hw/i386/intel_iommu.c static void

Quoted 15 lines
    1 file changed, 42 insertions(+)
hw/i386/intel_iommu.c +1 -0
index 83c5e44413..6876dae727 100644
@@ -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"
 
@@ -4423,6 +4424,42 @@ static void

vtd_dev_unset_iommu_device(PCIBus *bus, void *opaque, int devfn)

Quoted 7 lines
Quoted 7 lines

hw/i386/intel_iommu.c static void vtd_dev_unset_iommu_device(PCIBus *bus, void *opaque, int devfn)

Quoted 5 lines
hw/i386/intel_iommu.c +2 -0
        vtd_iommu_unlock(s);
    }
 
+/*
+ * Unmapping a large range in one go is not optimal during migration

because

Quoted 1 line
hw/i386/intel_iommu.c +1 -0
+ * a large dirty bitmap needs to be allocated while there may be only

small

Quoted 7 lines
Quoted 7 lines

hw/i386/intel_iommu.c static void · line 4430

Quoted 1 line
hw/i386/intel_iommu.c +1 -0
+ * mappings, iterate over DMAMap list to unmap each range with active

mapping.

hw/i386/intel_iommu.c static void · line 4432

Quoted 2 lines
hw/i386/intel_iommu.c +2 -0
+ */
+static void vtd_address_space_unmap_in_migration(VTDAddressSpace

*as,

hw/i386/intel_iommu.c static void · line 4433

Quoted 1 line
hw/i386/intel_iommu.c +1 -0
+

IOMMUNotifier *n)

Quoted 10 lines
Quoted 10 lines

hw/i386/intel_iommu.c static void vtd_dev_unset_iommu_device(PCIBus *bus, void *opaque, int devfn)

Quoted 10 lines
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

Quoted 14 lines
Quoted 7 lines

or huge

hw/i386/intel_iommu.c static void · line 4444

Quoted 1 line
hw/i386/intel_iommu.c +1 -0
+     * page size and always a power of 2 in size. So the range of

DMAMap could

hw/i386/intel_iommu.c static void vtd_dev_unset_iommu_device(PCIBus *bus, void *opaque, int devfn) · line 4447

Quoted 3 lines
hw/i386/intel_iommu.c +3 -0
+     * be used for UNMAP notification directly.
+     */
+    while ((map = iova_tree_find(tree, &target))) {
Quoted 6 lines

how about an empty iova_tree? If guest has not mapped anything for the device, the tree is empty. And it is fine to not unmap anyting. While, if the device is attached to an identify domain, the iova_tree is empty as well. Are we sure that we need not to unmap anything here? It looks the answer is yes. But I'm suspecting the unmap failure will happen in the vfio side? If yes, need to consider a complete fix. :)

Quoted 3 lines

Not get what failure will happen, could you elaborate?
In case of identity domain, IOMMU memory region is disabled, no iommu notifier will ever be triggered. vfio_listener monitors memory address

space,

Quoted 1 line

if any memory region is disabled, vfio_listener will catch it and do dirty

Quoted 8 lines

tracking.

My question comes from the reason why DMA unmap fails. It is due to a big range is given to kernel while kernel does not support. So if VFIO gives a big range as well, it should fail as well. And this is possible when guest (a VM with large size memory) switches from identify domain to a paging domain. In this case, vfio_listener will unmap all the system MRs. And it can be a big range if VM size is big enough.

Quoted 5 lines

Got you point. Yes, currently vfio_type1 driver limits unmap_bitmap to 8TB size. If guest memory is large enough and lead to a memory region of more than 8TB size, unmap_bitmap will fail. It's a rare case to live migrate VM with more than 8TB memory, instead of fixing it in qemu with complex change, I'd suggest to bump below MACRO value to enlarge the limit in kernel, or switch to use iommufd which doesn't have such limit.

This limit shall not affect the usage of device dirty tracking. right? If yes, add something to tell user use iommufd backend is better. e.g if memory size is bigger than the limit of vfio iommu type1's dirty bitmap limit (query cap_mig.max_dirty_bitmap_size), then fail user if user wants migration capability.

Quoted 47 lines

/*
* Input argument of number of bits to bitmap_set() is unsigned integer, which
* further casts to signed integer for unaligned multi-bit operation,
* __bitmap_set().
* Then maximum bitmap size supported is 2^31 bits divided by 2^3 bits/byte,
* that is 2^28 (256 MB) which maps to 2^31 * 2^12 = 2^43 (8TB) on 4K page
* system.
*/
#define DIRTY_BITMAP_PAGES_MAX ((u64)INT_MAX)
#define DIRTY_BITMAP_SIZE_MAX DIRTY_BITMAP_BYTES(DIRTY_BITMAP_PAGES_MAX)

Quoted 36 lines
Quoted 26 lines
Quoted 26 lines
Quoted 9 lines
hw/i386/intel_iommu.c +7 -0
+        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;

hw/i386/intel_iommu.c static void vtd_dev_unset_iommu_device(PCIBus *bus, void *opaque, int devfn)

Quoted 10 lines
hw/i386/intel_iommu.c +8 -0
+        /* This field is meaningless for unmap */
+        event.entry.translated_addr = 0;
+        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

Quoted 2 lines
hw/i386/intel_iommu.c +0 -0
    {
@@ -4432,6 +4469,11 @@ static void

vtd_address_space_unmap(VTDAddressSpace *as, IOMMUNotifier *n)

Quoted 3 lines
hw/i386/intel_iommu.c +0 -0
        IntelIOMMUState *s = as->iommu_state;
        DMAMap map;
 
Quoted 7 lines
Quoted 5 lines

hw/i386/intel_iommu.c static void vtd_address_space_unmap(VTDAddressSpace *as, IOMMUNotifier *n) · line 4472

Quoted 1 line
hw/i386/intel_iommu.c +1 -0
+    if (migration_is_running()) {

If the range is not big enough, it is still better to unmap in one-go. right? If so, might add a check on the range here to go to the iova_tee iteration conditionally.

We don't want to ditry track IOVA holes between IOVA ranges because it's

time consuming and useless work. The hole may be large depending on guest behavior.

Quoted 1 line

Meanwhile the time iterating on iova_tree is trivial. So we prefer tracking

Quoted 8 lines
Quoted 5 lines

the exact iova ranges that may be dirty actually.

I see. So this is the optimization. And it also WA the above DMA unmap issue as well. right? If so, you may want to call out in the commit message.

Yes, the main purpose of this patch is to fix the unmap_bitmap issue, then the optimization. I'll rephrase the description and subject.

yes. The commit message gives me the impression this is bug fix. While subject is optimization. BTW. perhaps call it as an optimization is clearer since this smells more like an optimization. For fix, I guess you may need to consider the vfio_listener as well.

Regards,
Yi Liu

#29 lore raw

RE: [PATCH 4/5] intel_iommu: Optimize unmap_bitmap during migration

Quoted 53 lines

-----Original Message-----
From: Liu, Yi L <[email protected]>
Subject: Re: [PATCH 4/5] intel_iommu: Optimize unmap_bitmap during migration

Quoted 10 lines · Zhenzhong
Quoted 10 lines

-----Original Message-----
From: Liu, Yi L <[email protected]>
Subject: Re: [PATCH 4/5] intel_iommu: Optimize unmap_bitmap during migration

Quoted 2 lines · Zhenzhong
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

-----Original Message-----
From: Liu, Yi L <[email protected]>
Subject: Re: [PATCH 4/5] intel_iommu: Optimize unmap_bitmap during migration

Quoted 1 line · Zhenzhong Duan

If a VFIO device in guest switches from IOMMU domain to block

domain,

Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 6 lines

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))

this should be a giant and busy VM. right? Is a fix tag needed by the

way?

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

VM size is unrelated, it's not a bug, just current code doesn't work well

with

Quoted 5 lines
Quoted 5 lines

migration.

Quoted 2 lines

When device switches from IOMMU domain to block domain, the whole

iommu

Quoted 1 line

memory region is disabled, this trigger the unmap on the whole iommu

Quoted 8 lines
Quoted 8 lines

memory

Quoted 1 line

region,

I got this part.

Quoted 1 line

no matter how many or how large the mappings are in the iommu MR.

hmmm. A more explicit question: does this error happen with 4G VM

memory

Quoted 8 lines
Quoted 3 lines
Quoted 1 line

as well?

Coincidently, I remember QAT team reported this issue just with 4G VM

memory.

ok. this might happen with legacy vIOMMU as guest triggers map/unmap. It can be a large range. But it's still not clear to me how can guest map a range more than 4G if VM only has 4G memory.

It happens when guest switch from DMA domain to block domain, below sequence is triggered:

vtd_context_device_invalidate
vtd_address_space_sync
vtd_address_space_unmap

You can see the whole iommu address space is unmapped, it's unrelated to actual mapping in guest.

Quoted 110 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

Because legacy VFIO limits maximum bitmap size to 256MB which

maps

Quoted 7 lines
Quoted 7 lines

to

Quoted 6 lines
Quoted 6 lines

8TB on

Quoted 1 line

4K page system, when 16TB sized UNMAP notification is sent,

unmap_bitmap

Quoted 3 lines

ioctl fails.

There is no such limitation with iommufd backend, but it's still not

optimal

Quoted 5 lines
Quoted 5 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

to allocate large bitmap.

Optimize it by iterating over DMAMap list to unmap each range with

active

Quoted 1 line
Quoted 1 line
Quoted 1 line

mapping when migration is active. If migration is not active,

unmapping

Quoted 27 lines
Quoted 27 lines

the

Quoted 26 lines
Quoted 26 lines
Quoted 6 lines

whole address space in one go is optimal.

---
    hw/i386/intel_iommu.c | 42

++++++++++++++++++++++++++++++++++++++++++

hw/i386/intel_iommu.c static void

Quoted 15 lines
    1 file changed, 42 insertions(+)
hw/i386/intel_iommu.c +1 -0
index 83c5e44413..6876dae727 100644
@@ -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"
 
@@ -4423,6 +4424,42 @@ static void

vtd_dev_unset_iommu_device(PCIBus *bus, void *opaque, int devfn)

Quoted 3 lines
hw/i386/intel_iommu.c +0 -0
        vtd_iommu_unlock(s);
    }
 
Quoted 6 lines
Quoted 6 lines
Quoted 4 lines
Quoted 4 lines

hw/i386/intel_iommu.c static void · line 4428

Quoted 2 lines
hw/i386/intel_iommu.c +2 -0
+/*
+ * Unmapping a large range in one go is not optimal during migration

because

Quoted 1 line
hw/i386/intel_iommu.c +1 -0
+ * a large dirty bitmap needs to be allocated while there may be only

small

Quoted 1 line
Quoted 1 line
Quoted 1 line
hw/i386/intel_iommu.c +1 -0
+ * mappings, iterate over DMAMap list to unmap each range with

active

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

mapping.

Quoted 2 lines
hw/i386/intel_iommu.c +2 -0
+ */
+static void

vtd_address_space_unmap_in_migration(VTDAddressSpace

Quoted 14 lines
Quoted 14 lines
Quoted 13 lines
Quoted 13 lines

*as,

hw/i386/intel_iommu.c static void · line 4433

Quoted 1 line
hw/i386/intel_iommu.c +1 -0
+

IOMMUNotifier *n)

hw/i386/intel_iommu.c static void vtd_dev_unset_iommu_device(PCIBus *bus, void *opaque, int devfn)

Quoted 10 lines
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

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

or huge

hw/i386/intel_iommu.c static void · line 4444

Quoted 1 line
hw/i386/intel_iommu.c +1 -0
+     * page size and always a power of 2 in size. So the range of

DMAMap could

hw/i386/intel_iommu.c static void vtd_dev_unset_iommu_device(PCIBus *bus, void *opaque, int devfn) · line 4447

Quoted 3 lines
hw/i386/intel_iommu.c +3 -0
+     * be used for UNMAP notification directly.
+     */
+    while ((map = iova_tree_find(tree, &target))) {

how about an empty iova_tree? If guest has not mapped anything for

the

Quoted 13 lines
Quoted 13 lines
Quoted 9 lines
Quoted 5 lines

device, the tree is empty. And it is fine to not unmap anyting. While, if the device is attached to an identify domain, the iova_tree is empty as well. Are we sure that we need not to unmap anything here? It looks the answer is yes. But I'm suspecting the unmap failure will happen in the vfio side? If yes, need to consider a complete fix. :)

Not get what failure will happen, could you elaborate?
In case of identity domain, IOMMU memory region is disabled, no iommu notifier will ever be triggered. vfio_listener monitors memory address

space,

Quoted 1 line

if any memory region is disabled, vfio_listener will catch it and do dirty

tracking.

Quoted 8 lines
Quoted 6 lines

My question comes from the reason why DMA unmap fails. It is due to a big range is given to kernel while kernel does not support. So if VFIO gives a big range as well, it should fail as well. And this is possible when guest (a VM with large size memory) switches from identify domain to a paging domain. In this case, vfio_listener will unmap all the system MRs. And it can be a big range if VM size is big enough.

Got you point. Yes, currently vfio_type1 driver limits unmap_bitmap to 8TB

size.

Quoted 1 line

If guest memory is large enough and lead to a memory region of more than

8TB size,

Quoted 1 line

unmap_bitmap will fail. It's a rare case to live migrate VM with more than

8TB memory,

Quoted 1 line

instead of fixing it in qemu with complex change, I'd suggest to bump below

MACRO

Quoted 8 lines
Quoted 1 line

value to enlarge the limit in kernel, or switch to use iommufd which doesn't

have such limit.

This limit shall not affect the usage of device dirty tracking. right? If yes, add something to tell user use iommufd backend is better. e.g if memory size is bigger than the limit of vfio iommu type1's dirty bitmap limit (query cap_mig.max_dirty_bitmap_size), then fail user if user wants migration capability.

Do you mean just dirty tracking instead of migration, like dirtyrate? In that case, there is error print as above, I think that's enough as a hint?

I guess you mean to add a migration blocker if limit is reached? It's hard because the limit is only helpful for identity domain, DMA domain in guest doesn't have such limit, and we can't know guest's choice of domain type of each VFIO device attached.

Quoted 60 lines
Quoted 3 lines

/*
* Input argument of number of bits to bitmap_set() is unsigned integer,

which

Quoted 3 lines

* further casts to signed integer for unaligned multi-bit operation,
* __bitmap_set().
* Then maximum bitmap size supported is 2^31 bits divided by 2^3

bits/byte,

Quoted 1 line

* that is 2^28 (256 MB) which maps to 2^31 * 2^12 = 2^43 (8TB) on 4K

page

Quoted 4 lines

* system.
*/
#define DIRTY_BITMAP_PAGES_MAX ((u64)INT_MAX)
#define DIRTY_BITMAP_SIZE_MAX

DIRTY_BITMAP_BYTES(DIRTY_BITMAP_PAGES_MAX)

Quoted 27 lines
Quoted 27 lines
Quoted 27 lines
Quoted 27 lines
Quoted 10 lines
hw/i386/intel_iommu.c +7 -0
+        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;

hw/i386/intel_iommu.c static void vtd_dev_unset_iommu_device(PCIBus *bus, void *opaque, int devfn)

Quoted 10 lines
hw/i386/intel_iommu.c +8 -0
+        /* This field is meaningless for unmap */
+        event.entry.translated_addr = 0;
+        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

Quoted 2 lines
hw/i386/intel_iommu.c +0 -0
    {
@@ -4432,6 +4469,11 @@ static void

vtd_address_space_unmap(VTDAddressSpace *as, IOMMUNotifier *n)

Quoted 3 lines
hw/i386/intel_iommu.c +0 -0
        IntelIOMMUState *s = as->iommu_state;
        DMAMap map;
 
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 5 lines

hw/i386/intel_iommu.c static void vtd_address_space_unmap(VTDAddressSpace *as, IOMMUNotifier *n) · line 4472

Quoted 1 line
hw/i386/intel_iommu.c +1 -0
+    if (migration_is_running()) {

If the range is not big enough, it is still better to unmap in one-go. right? If so, might add a check on the range here to go to the iova_tee iteration conditionally.

We don't want to ditry track IOVA holes between IOVA ranges because

it's

Quoted 1 line
Quoted 1 line

time consuming and useless work. The hole may be large depending on

guest

Quoted 8 lines
Quoted 7 lines

behavior.

Quoted 1 line

Meanwhile the time iterating on iova_tree is trivial. So we prefer tracking

the exact iova ranges that may be dirty actually.

I see. So this is the optimization. And it also WA the above DMA unmap issue as well. right? If so, you may want to call out in the commit message.

Quoted 8 lines
Quoted 1 line

Yes, the main purpose of this patch is to fix the unmap_bitmap issue, then

the optimization.

Quoted 1 line

I'll rephrase the description and subject.

yes. The commit message gives me the impression this is bug fix. While subject is optimization. BTW. perhaps call it as an optimization is clearer since this smells more like an optimization. For fix, I guess you may need to consider the vfio_listener as well.

Do you have any idea to fix it with vfio_listener?

My understanding is, it's hard to fix this issue from vfio core, because vfio_listener doesn't know the mapping details in the guest, only vIOMMU cached them through DMAMap.

Thanks
Zhenzhong

#30 lore raw

Re: [PATCH 4/5] intel_iommu: Optimize unmap_bitmap during migration

Quoted 62 lines · Zhenzhong
Quoted 53 lines

-----Original Message-----
From: Liu, Yi L <[email protected]>
Subject: Re: [PATCH 4/5] intel_iommu: Optimize unmap_bitmap during migration

Quoted 10 lines · Zhenzhong
Quoted 10 lines

-----Original Message-----
From: Liu, Yi L <[email protected]>
Subject: Re: [PATCH 4/5] intel_iommu: Optimize unmap_bitmap during migration

Quoted 2 lines · Zhenzhong
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

-----Original Message-----
From: Liu, Yi L <[email protected]>
Subject: Re: [PATCH 4/5] intel_iommu: Optimize unmap_bitmap during migration

Quoted 1 line · Zhenzhong Duan

If a VFIO device in guest switches from IOMMU domain to block

domain,

Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 6 lines

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))

this should be a giant and busy VM. right? Is a fix tag needed by the

way?

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

VM size is unrelated, it's not a bug, just current code doesn't work well

with

Quoted 5 lines
Quoted 5 lines

migration.

Quoted 2 lines

When device switches from IOMMU domain to block domain, the whole

iommu

Quoted 1 line

memory region is disabled, this trigger the unmap on the whole iommu

Quoted 8 lines
Quoted 8 lines

memory

Quoted 1 line

region,

I got this part.

Quoted 1 line

no matter how many or how large the mappings are in the iommu MR.

hmmm. A more explicit question: does this error happen with 4G VM

memory

Quoted 8 lines
Quoted 3 lines
Quoted 1 line

as well?

Coincidently, I remember QAT team reported this issue just with 4G VM

memory.

ok. this might happen with legacy vIOMMU as guest triggers map/unmap. It can be a large range. But it's still not clear to me how can guest map a range more than 4G if VM only has 4G memory.

Quoted 7 lines

It happens when guest switch from DMA domain to block domain, below sequence is triggered:

vtd_context_device_invalidate
vtd_address_space_sync
vtd_address_space_unmap

You can see the whole iommu address space is unmapped, it's unrelated to actual mapping in guest.

got it.

Quoted 113 lines
Quoted 113 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

Because legacy VFIO limits maximum bitmap size to 256MB which

maps

Quoted 7 lines
Quoted 7 lines

to

Quoted 6 lines
Quoted 6 lines

8TB on

Quoted 1 line

4K page system, when 16TB sized UNMAP notification is sent,

unmap_bitmap

Quoted 3 lines

ioctl fails.

There is no such limitation with iommufd backend, but it's still not

optimal

Quoted 5 lines
Quoted 5 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

to allocate large bitmap.

Optimize it by iterating over DMAMap list to unmap each range with

active

Quoted 1 line
Quoted 1 line
Quoted 1 line

mapping when migration is active. If migration is not active,

unmapping

Quoted 27 lines
Quoted 27 lines

the

Quoted 26 lines
Quoted 26 lines
Quoted 6 lines

whole address space in one go is optimal.

---
     hw/i386/intel_iommu.c | 42

++++++++++++++++++++++++++++++++++++++++++

hw/i386/intel_iommu.c static void

Quoted 15 lines
     1 file changed, 42 insertions(+)
hw/i386/intel_iommu.c +1 -0
index 83c5e44413..6876dae727 100644
@@ -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"
 
@@ -4423,6 +4424,42 @@ static void

vtd_dev_unset_iommu_device(PCIBus *bus, void *opaque, int devfn)

Quoted 3 lines
hw/i386/intel_iommu.c +0 -0
         vtd_iommu_unlock(s);
     }
 
Quoted 6 lines
Quoted 6 lines
Quoted 4 lines
Quoted 4 lines

hw/i386/intel_iommu.c static void · line 4428

Quoted 2 lines
hw/i386/intel_iommu.c +2 -0
+/*
+ * Unmapping a large range in one go is not optimal during migration

because

Quoted 1 line
hw/i386/intel_iommu.c +1 -0
+ * a large dirty bitmap needs to be allocated while there may be only

small

Quoted 1 line
Quoted 1 line
Quoted 1 line
hw/i386/intel_iommu.c +1 -0
+ * mappings, iterate over DMAMap list to unmap each range with

active

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

mapping.

Quoted 2 lines
hw/i386/intel_iommu.c +2 -0
+ */
+static void

vtd_address_space_unmap_in_migration(VTDAddressSpace

Quoted 14 lines
Quoted 14 lines
Quoted 13 lines
Quoted 13 lines

*as,

hw/i386/intel_iommu.c static void · line 4433

Quoted 1 line
hw/i386/intel_iommu.c +1 -0
+

IOMMUNotifier *n)

hw/i386/intel_iommu.c static void · line 4443

Quoted 10 lines
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

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

or huge

hw/i386/intel_iommu.c static void · line 4444

Quoted 1 line
hw/i386/intel_iommu.c +1 -0
+     * page size and always a power of 2 in size. So the range of

DMAMap could

hw/i386/intel_iommu.c static void · line 4447

Quoted 3 lines
hw/i386/intel_iommu.c +3 -0
+     * be used for UNMAP notification directly.
+     */
+    while ((map = iova_tree_find(tree, &target))) {

how about an empty iova_tree? If guest has not mapped anything for

the

Quoted 13 lines
Quoted 13 lines
Quoted 9 lines
Quoted 5 lines

device, the tree is empty. And it is fine to not unmap anyting. While, if the device is attached to an identify domain, the iova_tree is empty as well. Are we sure that we need not to unmap anything here? It looks the answer is yes. But I'm suspecting the unmap failure will happen in the vfio side? If yes, need to consider a complete fix. :)

Not get what failure will happen, could you elaborate?
In case of identity domain, IOMMU memory region is disabled, no iommu notifier will ever be triggered. vfio_listener monitors memory address

space,

Quoted 1 line

if any memory region is disabled, vfio_listener will catch it and do dirty

tracking.

Quoted 8 lines
Quoted 6 lines

My question comes from the reason why DMA unmap fails. It is due to a big range is given to kernel while kernel does not support. So if VFIO gives a big range as well, it should fail as well. And this is possible when guest (a VM with large size memory) switches from identify domain to a paging domain. In this case, vfio_listener will unmap all the system MRs. And it can be a big range if VM size is big enough.

Got you point. Yes, currently vfio_type1 driver limits unmap_bitmap to 8TB

size.

Quoted 1 line

If guest memory is large enough and lead to a memory region of more than

8TB size,

Quoted 1 line

unmap_bitmap will fail. It's a rare case to live migrate VM with more than

8TB memory,

Quoted 1 line

instead of fixing it in qemu with complex change, I'd suggest to bump below

MACRO

Quoted 1 line

value to enlarge the limit in kernel, or switch to use iommufd which doesn't

have such limit.

Quoted 8 lines
Quoted 5 lines

This limit shall not affect the usage of device dirty tracking. right? If yes, add something to tell user use iommufd backend is better. e.g if memory size is bigger than the limit of vfio iommu type1's dirty bitmap limit (query cap_mig.max_dirty_bitmap_size), then fail user if user wants migration capability.

Do you mean just dirty tracking instead of migration, like dirtyrate? In that case, there is error print as above, I think that's enough as a hint?

it's not related to diryrate.

Quoted 4 lines

I guess you mean to add a migration blocker if limit is reached? It's hard because the limit is only helpful for identity domain, DMA domain in guest doesn't have such limit, and we can't know guest's choice of domain type of each VFIO device attached.

I meant a blocker to boot QEMU if there is limit. something like below:

	if (VM memory > 8TB && legacy_container_backend && migration_enabled)
		fail the VM boot.
Quoted 62 lines
Quoted 62 lines
Quoted 3 lines

/*
* Input argument of number of bits to bitmap_set() is unsigned integer,

which

Quoted 3 lines

* further casts to signed integer for unaligned multi-bit operation,
* __bitmap_set().
* Then maximum bitmap size supported is 2^31 bits divided by 2^3

bits/byte,

Quoted 1 line

* that is 2^28 (256 MB) which maps to 2^31 * 2^12 = 2^43 (8TB) on 4K

page

Quoted 4 lines

* system.
*/
#define DIRTY_BITMAP_PAGES_MAX ((u64)INT_MAX)
#define DIRTY_BITMAP_SIZE_MAX

DIRTY_BITMAP_BYTES(DIRTY_BITMAP_PAGES_MAX)

Quoted 27 lines
Quoted 27 lines
Quoted 27 lines
Quoted 27 lines
Quoted 10 lines
hw/i386/intel_iommu.c +7 -0
+        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;

hw/i386/intel_iommu.c static void · line 4464

Quoted 10 lines
hw/i386/intel_iommu.c +8 -0
+        /* This field is meaningless for unmap */
+        event.entry.translated_addr = 0;
+        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

Quoted 2 lines
hw/i386/intel_iommu.c +0 -0
     {
@@ -4432,6 +4469,11 @@ static void

vtd_address_space_unmap(VTDAddressSpace *as, IOMMUNotifier *n)

Quoted 3 lines
hw/i386/intel_iommu.c +0 -0
         IntelIOMMUState *s = as->iommu_state;
         DMAMap map;
 
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 5 lines

hw/i386/intel_iommu.c static void · line 4472

Quoted 1 line
hw/i386/intel_iommu.c +1 -0
+    if (migration_is_running()) {

If the range is not big enough, it is still better to unmap in one-go. right? If so, might add a check on the range here to go to the iova_tee iteration conditionally.

We don't want to ditry track IOVA holes between IOVA ranges because

it's

Quoted 1 line
Quoted 1 line

time consuming and useless work. The hole may be large depending on

guest

Quoted 9 lines
Quoted 7 lines

behavior.

Quoted 1 line

Meanwhile the time iterating on iova_tree is trivial. So we prefer tracking

the exact iova ranges that may be dirty actually.

I see. So this is the optimization. And it also WA the above DMA unmap issue as well. right? If so, you may want to call out in the commit message.

Yes, the main purpose of this patch is to fix the unmap_bitmap issue, then

the optimization.

Quoted 8 lines
Quoted 6 lines
Quoted 1 line

I'll rephrase the description and subject.

yes. The commit message gives me the impression this is bug fix. While subject is optimization. BTW. perhaps call it as an optimization is clearer since this smells more like an optimization. For fix, I guess you may need to consider the vfio_listener as well.

Do you have any idea to fix it with vfio_listener?

no good idea.

Quoted 2 lines

My understanding is, it's hard to fix this issue from vfio core, because vfio_listener doesn't know the mapping details in the guest, only vIOMMU cached them through DMAMap.

yes. that's why I suggest above fix/WA to avoid it.

Regards,
Yi Liu

#31 lore raw

RE: [PATCH 4/5] intel_iommu: Optimize unmap_bitmap during migration

Quoted 224 lines

-----Original Message-----
From: Liu, Yi L <[email protected]>
Subject: Re: [PATCH 4/5] intel_iommu: Optimize unmap_bitmap during migration

Quoted 15 lines · Zhenzhong
Quoted 15 lines

-----Original Message-----
From: Liu, Yi L <[email protected]>
Subject: Re: [PATCH 4/5] intel_iommu: Optimize unmap_bitmap during migration

Quoted 7 lines · Zhenzhong
Quoted 7 lines

-----Original Message-----
From: Liu, Yi L <[email protected]>
Subject: Re: [PATCH 4/5] intel_iommu: Optimize unmap_bitmap during migration

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines · Zhenzhong
Quoted 5 lines
Quoted 5 lines

-----Original Message-----
From: Liu, Yi L <[email protected]>
Subject: Re: [PATCH 4/5] intel_iommu: Optimize unmap_bitmap

during

Quoted 6 lines
Quoted 6 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

migration

Quoted 1 line · Zhenzhong Duan

If a VFIO device in guest switches from IOMMU domain to block

domain,

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

vtd_address_space_unmap() is called to unmap whole address

space.

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 5 lines

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))

Quoted 8 lines
Quoted 8 lines
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

this should be a giant and busy VM. right? Is a fix tag needed by the

way?

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

VM size is unrelated, it's not a bug, just current code doesn't work well

with

Quoted 3 lines
Quoted 3 lines

migration.

Quoted 2 lines

When device switches from IOMMU domain to block domain, the

whole

Quoted 13 lines
Quoted 13 lines
Quoted 10 lines
Quoted 10 lines

iommu

Quoted 1 line

memory region is disabled, this trigger the unmap on the whole iommu

memory

Quoted 1 line

region,

I got this part.

Quoted 1 line

no matter how many or how large the mappings are in the iommu MR.

hmmm. A more explicit question: does this error happen with 4G VM

memory

Quoted 2 lines
Quoted 1 line

as well?

Quoted 8 lines
Quoted 6 lines
Quoted 1 line

Coincidently, I remember QAT team reported this issue just with 4G VM

memory.

ok. this might happen with legacy vIOMMU as guest triggers map/unmap. It can be a large range. But it's still not clear to me how can guest map a range more than 4G if VM only has 4G memory.

It happens when guest switch from DMA domain to block domain, below

sequence is triggered:

Quoted 6 lines

vtd_context_device_invalidate
vtd_address_space_sync
vtd_address_space_unmap

You can see the whole iommu address space is unmapped, it's unrelated to

actual mapping in guest.

got it.

Quoted 43 lines
Quoted 43 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

Because legacy VFIO limits maximum bitmap size to 256MB which

maps

Quoted 7 lines
Quoted 7 lines

to

Quoted 6 lines
Quoted 6 lines

8TB on

Quoted 1 line

4K page system, when 16TB sized UNMAP notification is sent,

unmap_bitmap

Quoted 3 lines

ioctl fails.

There is no such limitation with iommufd backend, but it's still not

optimal

Quoted 5 lines
Quoted 5 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

to allocate large bitmap.

Optimize it by iterating over DMAMap list to unmap each range with

active

Quoted 1 line
Quoted 1 line
Quoted 1 line

mapping when migration is active. If migration is not active,

unmapping

Quoted 22 lines
Quoted 22 lines

the

Quoted 21 lines
Quoted 21 lines
Quoted 6 lines

whole address space in one go is optimal.

---
     hw/i386/intel_iommu.c | 42

++++++++++++++++++++++++++++++++++++++++++

Quoted 14 lines
     1 file changed, 42 insertions(+)
hw/i386/intel_iommu.c +1 -0
index 83c5e44413..6876dae727 100644
@@ -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"
 
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

hw/i386/intel_iommu.c static void

Quoted 1 line
hw/i386/intel_iommu.c +0 -0
@@ -4423,6 +4424,42 @@ static void

vtd_dev_unset_iommu_device(PCIBus *bus, void *opaque, int devfn)

Quoted 5 lines
hw/i386/intel_iommu.c +2 -0
         vtd_iommu_unlock(s);
     }
 
+/*
+ * Unmapping a large range in one go is not optimal during

migration

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

because

Quoted 1 line
hw/i386/intel_iommu.c +1 -0
+ * a large dirty bitmap needs to be allocated while there may be

only

Quoted 10 lines
Quoted 10 lines
Quoted 2 lines
Quoted 2 lines

small

Quoted 1 line
Quoted 1 line
Quoted 1 line
hw/i386/intel_iommu.c +1 -0
+ * mappings, iterate over DMAMap list to unmap each range with

active

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

mapping.

Quoted 2 lines
hw/i386/intel_iommu.c +2 -0
+ */
+static void

vtd_address_space_unmap_in_migration(VTDAddressSpace

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

*as,

hw/i386/intel_iommu.c static void · line 4433

Quoted 1 line
hw/i386/intel_iommu.c +1 -0
+

IOMMUNotifier *n)

Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines

hw/i386/intel_iommu.c static void · line 4443

Quoted 10 lines
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

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

4KB

Quoted 7 lines
Quoted 7 lines

or huge

hw/i386/intel_iommu.c static void · line 4444

Quoted 1 line
hw/i386/intel_iommu.c +1 -0
+     * page size and always a power of 2 in size. So the range of

DMAMap could

Quoted 3 lines
hw/i386/intel_iommu.c +3 -0
+     * be used for UNMAP notification directly.
+     */
+    while ((map = iova_tree_find(tree, &target))) {
Quoted 5 lines
Quoted 5 lines
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

how about an empty iova_tree? If guest has not mapped anything for

the

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

device, the tree is empty. And it is fine to not unmap anyting. While, if the device is attached to an identify domain, the iova_tree is empty as well. Are we sure that we need not to unmap anything here? It

looks

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 2 lines

the answer is yes. But I'm suspecting the unmap failure will happen in the vfio side? If yes, need to consider a complete fix. :)

Not get what failure will happen, could you elaborate? In case of identity domain, IOMMU memory region is disabled, no

iommu

Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 1 line

notifier will ever be triggered. vfio_listener monitors memory address

space,

Quoted 1 line

if any memory region is disabled, vfio_listener will catch it and do dirty

tracking.

My question comes from the reason why DMA unmap fails. It is due to a big range is given to kernel while kernel does not support. So if VFIO gives a big range as well, it should fail as well. And this is possible when guest (a VM with large size memory) switches from

identify

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 2 lines

domain to a paging domain. In this case, vfio_listener will unmap all the system MRs. And it can be a big range if VM size is big enough.

Got you point. Yes, currently vfio_type1 driver limits unmap_bitmap to

8TB

Quoted 2 lines
Quoted 2 lines

size.

Quoted 1 line

If guest memory is large enough and lead to a memory region of more

than

Quoted 4 lines
Quoted 4 lines

8TB size,

Quoted 1 line

unmap_bitmap will fail. It's a rare case to live migrate VM with more than

8TB memory,

Quoted 1 line

instead of fixing it in qemu with complex change, I'd suggest to bump

below

Quoted 2 lines
Quoted 2 lines

MACRO

Quoted 1 line

value to enlarge the limit in kernel, or switch to use iommufd which

doesn't

Quoted 10 lines
Quoted 7 lines

have such limit.

This limit shall not affect the usage of device dirty tracking. right? If yes, add something to tell user use iommufd backend is better. e.g if memory size is bigger than the limit of vfio iommu type1's dirty bitmap limit (query cap_mig.max_dirty_bitmap_size), then fail user if user wants migration capability.

Do you mean just dirty tracking instead of migration, like dirtyrate? In that case, there is error print as above, I think that's enough as a hint?

it's not related to diryrate.

Quoted 4 lines

I guess you mean to add a migration blocker if limit is reached? It's hard because the limit is only helpful for identity domain, DMA domain in guest doesn't have such limit, and we can't know guest's choice of domain type of each VFIO device attached.

Quoted 5 lines

I meant a blocker to boot QEMU if there is limit. something like below:

	if (VM memory > 8TB && legacy_container_backend &&

migration_enabled)
fail the VM boot.

OK, will add below to vfio_migration_realize() with an extra patch:

if (!vbasedev->iommufd && current_machine->ram_size > 8 * TiB) {
/*
* The 8TB comes from default kernel and QEMU config, it may be
* conservative here as VM can use large page or run with vIOMMU
* so the limitation may be relaxed. But 8TB is already quite
* large for live migration. One can also switch to use IOMMUFD
* backend if there is a need to migrate large VM.
*/
error_setg(&err, "%s: Migration is currently not supported "
"with large memory VM with approximately 8TB memory " "due to limitation in VFIO type1 driver", vbasedev->name);
goto add_blocker;
}

Thanks
Zhenzhong

#32 lore raw

Re: [PATCH 4/5] intel_iommu: Optimize unmap_bitmap during migration

Quoted 57 lines · Zhenzhong
Quoted 57 lines
Quoted 6 lines
Quoted 6 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

how about an empty iova_tree? If guest has not mapped anything for

the

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

device, the tree is empty. And it is fine to not unmap anyting. While, if the device is attached to an identify domain, the iova_tree is empty as well. Are we sure that we need not to unmap anything here? It

looks

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 2 lines

the answer is yes. But I'm suspecting the unmap failure will happen in the vfio side? If yes, need to consider a complete fix. :)

Not get what failure will happen, could you elaborate? In case of identity domain, IOMMU memory region is disabled, no

iommu

Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 1 line

notifier will ever be triggered. vfio_listener monitors memory address

space,

Quoted 1 line

if any memory region is disabled, vfio_listener will catch it and do dirty

tracking.

My question comes from the reason why DMA unmap fails. It is due to a big range is given to kernel while kernel does not support. So if VFIO gives a big range as well, it should fail as well. And this is possible when guest (a VM with large size memory) switches from

identify

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 2 lines

domain to a paging domain. In this case, vfio_listener will unmap all the system MRs. And it can be a big range if VM size is big enough.

Got you point. Yes, currently vfio_type1 driver limits unmap_bitmap to

8TB

Quoted 2 lines
Quoted 2 lines

size.

Quoted 1 line

If guest memory is large enough and lead to a memory region of more

than

Quoted 4 lines
Quoted 4 lines

8TB size,

Quoted 1 line

unmap_bitmap will fail. It's a rare case to live migrate VM with more than

8TB memory,

Quoted 1 line

instead of fixing it in qemu with complex change, I'd suggest to bump

below

Quoted 2 lines
Quoted 2 lines

MACRO

Quoted 1 line

value to enlarge the limit in kernel, or switch to use iommufd which

doesn't

Quoted 10 lines
Quoted 7 lines

have such limit.

This limit shall not affect the usage of device dirty tracking. right? If yes, add something to tell user use iommufd backend is better. e.g if memory size is bigger than the limit of vfio iommu type1's dirty bitmap limit (query cap_mig.max_dirty_bitmap_size), then fail user if user wants migration capability.

Do you mean just dirty tracking instead of migration, like dirtyrate? In that case, there is error print as above, I think that's enough as a hint?

it's not related to diryrate.

Quoted 4 lines

I guess you mean to add a migration blocker if limit is reached? It's hard because the limit is only helpful for identity domain, DMA domain in guest doesn't have such limit, and we can't know guest's choice of domain type of each VFIO device attached.

Quoted 7 lines
Quoted 5 lines

I meant a blocker to boot QEMU if there is limit. something like below:

	if (VM memory > 8TB && legacy_container_backend &&

migration_enabled)
fail the VM boot.

OK, will add below to vfio_migration_realize() with an extra patch:

yeah, let's see Alex and Cedric's feedback.

Quoted 8 lines

if (!vbasedev->iommufd && current_machine->ram_size > 8 * TiB) {
/*
* The 8TB comes from default kernel and QEMU config, it may be
* conservative here as VM can use large page or run with vIOMMU
* so the limitation may be relaxed. But 8TB is already quite
* large for live migration. One can also switch to use IOMMUFD
* backend if there is a need to migrate large VM. */

instead of hard code 8TB. May convert cap_mig.max_dirty_bitmap_size to memory size. :)

Quoted 8 lines
         error_setg(&err, "%s: Migration is currently not supported "
                    "with large memory VM with approximately 8TB memory "
                    "due to limitation in VFIO type1 driver", vbasedev->name);
         goto add_blocker;
     }

Thanks
Zhenzhong

#33 lore raw

Re: [PATCH 4/5] intel_iommu: Optimize unmap_bitmap during migration

Quoted 77 lines · Yi Liu
Quoted 57 lines · Zhenzhong
Quoted 57 lines
Quoted 6 lines
Quoted 6 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

how about an empty iova_tree? If guest has not mapped anything for

the

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

device, the tree is empty. And it is fine to not unmap anyting. While, if the device is attached to an identify domain, the iova_tree is empty as well. Are we sure that we need not to unmap anything here? It

looks

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 2 lines

the answer is yes. But I'm suspecting the unmap failure will happen in the vfio side? If yes, need to consider a complete fix. :)

Not get what failure will happen, could you elaborate? In case of identity domain, IOMMU memory region is disabled, no

iommu

Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 1 line

notifier will ever be triggered. vfio_listener monitors memory address

space,

Quoted 1 line

if any memory region is disabled, vfio_listener will catch it and do dirty

tracking.

My question comes from the reason why DMA unmap fails. It is due to a big range is given to kernel while kernel does not support. So if VFIO gives a big range as well, it should fail as well. And this is possible when guest (a VM with large size memory) switches from

identify

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 2 lines

domain to a paging domain. In this case, vfio_listener will unmap all the system MRs. And it can be a big range if VM size is big enough.

Got you point. Yes, currently vfio_type1 driver limits unmap_bitmap to

8TB

Quoted 2 lines
Quoted 2 lines

size.

Quoted 1 line

If guest memory is large enough and lead to a memory region of more

than

Quoted 4 lines
Quoted 4 lines

8TB size,

Quoted 1 line

unmap_bitmap will fail. It's a rare case to live migrate VM with more than

8TB memory,

Quoted 1 line

instead of fixing it in qemu with complex change, I'd suggest to bump

below

Quoted 2 lines
Quoted 2 lines

MACRO

Quoted 1 line

value to enlarge the limit in kernel, or switch to use iommufd which

doesn't

Quoted 10 lines
Quoted 7 lines

have such limit.

This limit shall not affect the usage of device dirty tracking. right? If yes, add something to tell user use iommufd backend is better. e.g if memory size is bigger than the limit of vfio iommu type1's dirty bitmap limit (query cap_mig.max_dirty_bitmap_size), then fail user if user wants migration capability.

Do you mean just dirty tracking instead of migration, like dirtyrate? In that case, there is error print as above, I think that's enough as a hint?

it's not related to diryrate.

Quoted 4 lines

I guess you mean to add a migration blocker if limit is reached? It's hard because the limit is only helpful for identity domain, DMA domain in guest doesn't have such limit, and we can't know guest's choice of domain type of each VFIO device attached.

Quoted 7 lines
Quoted 5 lines

I meant a blocker to boot QEMU if there is limit. something like below:

    if (VM memory > 8TB && legacy_container_backend && migration_enabled)
        fail the VM boot.

OK, will add below to vfio_migration_realize() with an extra patch:

yeah, let's see Alex and Cedric's feedback.

Quoted 8 lines

     if (!vbasedev->iommufd && current_machine->ram_size > 8 * TiB) { /*
          * The 8TB comes from default kernel and QEMU config, it may be
          * conservative here as VM can use large page or run with vIOMMU
          * so the limitation may be relaxed. But 8TB is already quite
          * large for live migration. One can also switch to use IOMMUFD
          * backend if there is a need to migrate large VM. */

Quoted 2 lines

instead of hard code 8TB. May convert cap_mig.max_dirty_bitmap_size to memory size. :)

yes. It would reflect better that it's a VFIO dirty tracking limitation.

Zhenzhong,

Soft freeze is w45. I plan to send a PR next week, w43, and I will be out w44. I will have some (limited) time to address more changes on w45.

Thanks,

C.

#34 lore raw

RE: [PATCH 4/5] intel_iommu: Optimize unmap_bitmap during migration

Quoted 103 lines

-----Original Message-----
From: Cédric Le Goater <[email protected]>
Subject: Re: [PATCH 4/5] intel_iommu: Optimize unmap_bitmap during migration

Quoted 3 lines · Yi Liu
Quoted 2 lines · Zhenzhong
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

how about an empty iova_tree? If guest has not mapped anything

for

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

the

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

device, the tree is empty. And it is fine to not unmap anyting.

While,

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

if the device is attached to an identify domain, the iova_tree is

empty

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

as well. Are we sure that we need not to unmap anything here? It

looks

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

the answer is yes. But I'm suspecting the unmap failure will

happen in

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 1 line

the vfio side? If yes, need to consider a complete fix. :)

Not get what failure will happen, could you elaborate? In case of identity domain, IOMMU memory region is disabled, no

iommu

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

notifier will ever be triggered. vfio_listener monitors memory

address

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

space,

Quoted 1 line

if any memory region is disabled, vfio_listener will catch it and do

dirty

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

tracking.

My question comes from the reason why DMA unmap fails. It is due

to

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

a big range is given to kernel while kernel does not support. So if VFIO gives a big range as well, it should fail as well. And this is possible when guest (a VM with large size memory) switches from

identify

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 2 lines

domain to a paging domain. In this case, vfio_listener will unmap all the system MRs. And it can be a big range if VM size is big enough.

Got you point. Yes, currently vfio_type1 driver limits unmap_bitmap

to

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

8TB

Quoted 2 lines
Quoted 2 lines

size.

Quoted 1 line

If guest memory is large enough and lead to a memory region of

more

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

than

Quoted 2 lines
Quoted 2 lines

8TB size,

Quoted 1 line

unmap_bitmap will fail. It's a rare case to live migrate VM with more

than

Quoted 14 lines
Quoted 14 lines
Quoted 14 lines
Quoted 2 lines
Quoted 2 lines

8TB memory,

Quoted 1 line

instead of fixing it in qemu with complex change, I'd suggest to bump

below

Quoted 2 lines
Quoted 2 lines

MACRO

Quoted 1 line

value to enlarge the limit in kernel, or switch to use iommufd which

doesn't

Quoted 8 lines
Quoted 7 lines

have such limit.

This limit shall not affect the usage of device dirty tracking. right? If yes, add something to tell user use iommufd backend is better. e.g if memory size is bigger than the limit of vfio iommu type1's dirty bitmap limit (query cap_mig.max_dirty_bitmap_size), then fail user if user wants migration capability.

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 2 lines

Do you mean just dirty tracking instead of migration, like dirtyrate? In that case, there is error print as above, I think that's enough as a hint?

it's not related to diryrate.

Quoted 2 lines

I guess you mean to add a migration blocker if limit is reached? It's hard because the limit is only helpful for identity domain, DMA domain in

guest

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

doesn't have such limit, and we can't know guest's choice of domain

type

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 1 line

of each VFIO device attached.

I meant a blocker to boot QEMU if there is limit. something like below:

Quoted 9 lines
Quoted 5 lines
Quoted 3 lines

    if (VM memory > 8TB && legacy_container_backend && migration_enabled)
        fail the VM boot.

OK, will add below to vfio_migration_realize() with an extra patch:

yeah, let's see Alex and Cedric's feedback.

Quoted 1 line

     if (!vbasedev->iommufd && current_machine->ram_size > 8 * TiB)

{

Quoted 2 lines
Quoted 2 lines

         /*
          * The 8TB comes from default kernel and QEMU config, it

may be

Quoted 1 line
Quoted 1 line

          * conservative here as VM can use large page or run with

vIOMMU

Quoted 2 lines
Quoted 2 lines

          * so the limitation may be relaxed. But 8TB is already quite
          * large for live migration. One can also switch to use

IOMMUFD

Quoted 5 lines
Quoted 2 lines

          * backend if there is a need to migrate large VM. */

instead of hard code 8TB. May convert cap_mig.max_dirty_bitmap_size to memory size. :)

Quoted 7 lines

yes. It would reflect better that it's a VFIO dirty tracking limitation.

Zhenzhong,

Soft freeze is w45. I plan to send a PR next week, w43, and I will be out w44. I will have some (limited) time to address more changes on w45.

Got it, I'll send a new version soon.

Thanks
Zhenzhong

#35 lore raw

PATCH 5/5 vfio/migration: Allow live migration with vIOMMU without VFs using device dirty tracking

In reply to Zhenzhong Duan

Commit 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.

Introduce a helper vfio_device_dirty_pages_disabled() to facilicate it.

---
 include/hw/vfio/vfio-device.h | 10 ++++++++++
 hw/vfio/container-base.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
index 6e4d5ccdac..0c663a49d5 100644
@@ -148,6 +148,16 @@ bool vfio_device_irq_set_signaling(VFIODevice *vbasedev, int index, int subindex
 
 void 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-base.c +1 -4
index 56304978e1..39c88812f8 100644
@@ -177,10 +177,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
index 52a1996dc4..d0975e0e40 100644
@@ -400,6 +400,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
index 4c06e3db93..810a5cb157 100644
@@ -1183,8 +1183,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,
@@ -1202,7 +1201,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