Skip to contentSkip to conversation

Some fixes on intel_iommu

PATCH New Zhenzhong Duan 1 participant 4 messages Jun 15, 2023

lore
Share & tools
lore
#0 lore raw

v4 cover Some fixes on intel_iommu

Hi All,

This patchset fixes a potential issue in VFIO dirty page sync and two trivial fixes for robust purpose.

Tested net card passthrough, ping/ssh pass
Tested DSA vdev passthrough, start dmatest then do live migration, pass. Checked the LM performance before and after patch, no explicit difference. (DSA vdev requires customed kernel and qemu)

v4:
Add Reviewed-by
Drop PATCH1 and PATCH5

v3:
updated PATCH2 to fix VFIO dirty sync in Peter suggested way. split PATCH4 in v2 to PATCH4 and PATCH5 as they target different issue. dropped PATCH3 in v2.
add Suggested-by.

Thanks

Zhenzhong Duan (3):
intel_iommu: Fix a potential issue in VFIO dirty page sync
intel_iommu: Fix flag check in replay
intel_iommu: Fix address space unmap

 hw/i386/intel_iommu.c | 13 +++++--------
 1 file changed, 5 insertions(+), 8 deletions(-)
Signature · 1 line
2.34.1
#1 lore raw

PATCH v4 1/3 intel_iommu: Fix a potential issue in VFIO dirty page sync

Peter Xu found a potential issue:

"The other thing is when I am looking at the new code I found that we actually extended the replay() to be used also in dirty tracking of vfio, in vfio_sync_dirty_bitmap(). For that maybe it's already broken if unmap_all() because afaiu log_sync() can be called in migration thread anytime during DMA so I think it means the device is prone to DMA with the IOMMU pgtable quickly erased and rebuilt here, which means the DMA could fail unexpectedly. Copy Alex, Kirti and Neo."

Fix it by replacing the unmap_all() to only evacuate the iova tree (keeping all host mappings untouched, IOW, don't notify UNMAP), and do a full resync in page walk which will notify all existing mappings as MAP. This way we don't interrupt with any existing mapping if there is (e.g. for the dirty sync case), meanwhile we keep sync too to latest (for moving a vfio device into an existing iommu group).

---
 hw/i386/intel_iommu.c | 9 +++------
 1 file changed, 3 insertions(+), 6 deletions(-)
hw/i386/intel_iommu.c +3 -6
index 94d52f4205d2..34af12f392f5 100644
@@ -3825,13 +3825,10 @@ static void vtd_iommu_replay(IOMMUMemoryRegion *iommu_mr, IOMMUNotifier *n)
     IntelIOMMUState *s = vtd_as->iommu_state;
     uint8_t bus_n = pci_bus_num(vtd_as->bus);
     VTDContextEntry ce;
+    DMAMap map = { .iova = 0, .size = HWADDR_MAX };
 
-    /*
-     * The replay can be triggered by either a invalidation or a newly
-     * created entry. No matter what, we release existing mappings
-     * (it means flushing caches for UNMAP-only registers).
-     */
-    vtd_address_space_unmap(vtd_as, n);
+    /* replay is protected by BQL, page walk will re-setup it safely */
+    iova_tree_remove(vtd_as->iova_tree, map);
 
     if (vtd_dev_to_context_entry(s, bus_n, vtd_as->devfn, &ce) == 0) {
         trace_vtd_replay_ce_valid(s->root_scalable ? "scalable mode" :
Signature · 1 line
2.34.1
#2 lore raw

PATCH v4 2/3 intel_iommu: Fix flag check in replay

In reply to Zhenzhong Duan

Replay doesn't notify registered notifiers but the one passed to it. So it's meaningless to check the registered notifier's synthetic flag.

There is no issue currently as all replay use cases have MAP flag set, but let's be robust.

---
 hw/i386/intel_iommu.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
hw/i386/intel_iommu.c +1 -1
index 34af12f392f5..f046f8591335 100644
@@ -3837,7 +3837,7 @@ static void vtd_iommu_replay(IOMMUMemoryRegion *iommu_mr, IOMMUNotifier *n)
                                   PCI_FUNC(vtd_as->devfn),
                                   vtd_get_domain_id(s, &ce, vtd_as->pasid),
                                   ce.hi, ce.lo);
-        if (vtd_as_has_map_notifier(vtd_as)) {
+        if (n->notifier_flags & IOMMU_NOTIFIER_MAP) {
             /* This is required only for MAP typed notifiers */
             vtd_page_walk_info info = {
                 .hook_fn = vtd_replay_hook,
Signature · 1 line
2.34.1
#3 lore raw

PATCH v4 3/3 intel_iommu: Fix address space unmap

In reply to Zhenzhong Duan

During address space unmap, corresponding IOVA tree entries are also removed. But DMAMap is set beyond notifier's scope by 1, so in theory there is possibility to remove a continuous entry above the notifier's scope but falling in adjacent notifier's scope.

There is no issue currently as no use cases allocate notifiers continuously, but let's be robust.

---
 hw/i386/intel_iommu.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
hw/i386/intel_iommu.c +1 -1
index f046f8591335..dcc334060cd6 100644
@@ -3791,7 +3791,7 @@ static void vtd_address_space_unmap(VTDAddressSpace *as, IOMMUNotifier *n)
                              n->start, size);
 
     map.iova = n->start;
-    map.size = size;
+    map.size = size - 1; /* Inclusive */
     iova_tree_remove(as->iova_tree, map);
 }
 
Signature · 1 line
2.34.1