All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v4 0/3] Some fixes on intel_iommu
@ 2023-06-15  3:26 Zhenzhong Duan
  2023-06-15  3:26 ` [PATCH v4 1/3] intel_iommu: Fix a potential issue in VFIO dirty page sync Zhenzhong Duan
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Zhenzhong Duan @ 2023-06-15  3:26 UTC (permalink / raw)
  To: qemu-devel
  Cc: mst, peterx, jasowang, pbonzini, richard.henderson, eduardo,
	marcel.apfelbaum, alex.williamson, kwankhede, cjia, yi.l.liu,
	chao.p.peng

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

-- 
2.34.1



^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH v4 1/3] intel_iommu: Fix a potential issue in VFIO dirty page sync
  2023-06-15  3:26 [PATCH v4 0/3] Some fixes on intel_iommu Zhenzhong Duan
@ 2023-06-15  3:26 ` Zhenzhong Duan
  2023-06-15  3:26 ` [PATCH v4 2/3] intel_iommu: Fix flag check in replay Zhenzhong Duan
  2023-06-15  3:26 ` [PATCH v4 3/3] intel_iommu: Fix address space unmap Zhenzhong Duan
  2 siblings, 0 replies; 4+ messages in thread
From: Zhenzhong Duan @ 2023-06-15  3:26 UTC (permalink / raw)
  To: qemu-devel
  Cc: mst, peterx, jasowang, pbonzini, richard.henderson, eduardo,
	marcel.apfelbaum, alex.williamson, kwankhede, cjia, yi.l.liu,
	chao.p.peng

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

Suggested-by: Peter Xu <peterx@redhat.com>
Signed-off-by: Zhenzhong Duan <zhenzhong.duan@intel.com>
Reviewed-by: Peter Xu <peterx@redhat.com>
---
 hw/i386/intel_iommu.c | 9 +++------
 1 file changed, 3 insertions(+), 6 deletions(-)

diff --git a/hw/i386/intel_iommu.c b/hw/i386/intel_iommu.c
index 94d52f4205d2..34af12f392f5 100644
--- a/hw/i386/intel_iommu.c
+++ b/hw/i386/intel_iommu.c
@@ -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" :
-- 
2.34.1



^ permalink raw reply related	[flat|nested] 4+ messages in thread

* [PATCH v4 2/3] intel_iommu: Fix flag check in replay
  2023-06-15  3:26 [PATCH v4 0/3] Some fixes on intel_iommu Zhenzhong Duan
  2023-06-15  3:26 ` [PATCH v4 1/3] intel_iommu: Fix a potential issue in VFIO dirty page sync Zhenzhong Duan
@ 2023-06-15  3:26 ` Zhenzhong Duan
  2023-06-15  3:26 ` [PATCH v4 3/3] intel_iommu: Fix address space unmap Zhenzhong Duan
  2 siblings, 0 replies; 4+ messages in thread
From: Zhenzhong Duan @ 2023-06-15  3:26 UTC (permalink / raw)
  To: qemu-devel
  Cc: mst, peterx, jasowang, pbonzini, richard.henderson, eduardo,
	marcel.apfelbaum, alex.williamson, kwankhede, cjia, yi.l.liu,
	chao.p.peng

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.

Signed-off-by: Zhenzhong Duan <zhenzhong.duan@intel.com>
Reviewed-by: Peter Xu <peterx@redhat.com>
---
 hw/i386/intel_iommu.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/hw/i386/intel_iommu.c b/hw/i386/intel_iommu.c
index 34af12f392f5..f046f8591335 100644
--- a/hw/i386/intel_iommu.c
+++ b/hw/i386/intel_iommu.c
@@ -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,
-- 
2.34.1



^ permalink raw reply related	[flat|nested] 4+ messages in thread

* [PATCH v4 3/3] intel_iommu: Fix address space unmap
  2023-06-15  3:26 [PATCH v4 0/3] Some fixes on intel_iommu Zhenzhong Duan
  2023-06-15  3:26 ` [PATCH v4 1/3] intel_iommu: Fix a potential issue in VFIO dirty page sync Zhenzhong Duan
  2023-06-15  3:26 ` [PATCH v4 2/3] intel_iommu: Fix flag check in replay Zhenzhong Duan
@ 2023-06-15  3:26 ` Zhenzhong Duan
  2 siblings, 0 replies; 4+ messages in thread
From: Zhenzhong Duan @ 2023-06-15  3:26 UTC (permalink / raw)
  To: qemu-devel
  Cc: mst, peterx, jasowang, pbonzini, richard.henderson, eduardo,
	marcel.apfelbaum, alex.williamson, kwankhede, cjia, yi.l.liu,
	chao.p.peng

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.

Signed-off-by: Zhenzhong Duan <zhenzhong.duan@intel.com>
Reviewed-by: Peter Xu <peterx@redhat.com>
---
 hw/i386/intel_iommu.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/hw/i386/intel_iommu.c b/hw/i386/intel_iommu.c
index f046f8591335..dcc334060cd6 100644
--- a/hw/i386/intel_iommu.c
+++ b/hw/i386/intel_iommu.c
@@ -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);
 }
 
-- 
2.34.1



^ permalink raw reply related	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2023-06-15  3:40 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-06-15  3:26 [PATCH v4 0/3] Some fixes on intel_iommu Zhenzhong Duan
2023-06-15  3:26 ` [PATCH v4 1/3] intel_iommu: Fix a potential issue in VFIO dirty page sync Zhenzhong Duan
2023-06-15  3:26 ` [PATCH v4 2/3] intel_iommu: Fix flag check in replay Zhenzhong Duan
2023-06-15  3:26 ` [PATCH v4 3/3] intel_iommu: Fix address space unmap Zhenzhong Duan

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.