From: Zhenzhong Duan <zhenzhong.duan@intel.com>
To: qemu-devel@nongnu.org
Cc: mst@redhat.com, peterx@redhat.com, jasowang@redhat.com,
pbonzini@redhat.com, richard.henderson@linaro.org,
eduardo@habkost.net, marcel.apfelbaum@gmail.com,
alex.williamson@redhat.com, clg@redhat.com, david@redhat.com,
philmd@linaro.org, kwankhede@nvidia.com, cjia@nvidia.com,
yi.l.liu@intel.com, chao.p.peng@intel.com
Subject: [PATCH v3 2/5] intel_iommu: Fix a potential issue in VFIO dirty page sync
Date: Thu, 8 Jun 2023 17:52:28 +0800 [thread overview]
Message-ID: <20230608095231.225450-3-zhenzhong.duan@intel.com> (raw)
In-Reply-To: <20230608095231.225450-1-zhenzhong.duan@intel.com>
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>
---
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
next prev parent reply other threads:[~2023-06-08 10:06 UTC|newest]
Thread overview: 30+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-06-08 9:52 [PATCH v3 0/5] Optimize UNMAP call and bug fix Zhenzhong Duan
2023-06-08 9:52 ` [PATCH v3 1/5] util: Add iova_tree_foreach_range_data Zhenzhong Duan
2023-06-08 9:52 ` Zhenzhong Duan [this message]
2023-06-08 13:42 ` [PATCH v3 2/5] intel_iommu: Fix a potential issue in VFIO dirty page sync Peter Xu
2023-06-08 9:52 ` [PATCH v3 3/5] intel_iommu: Fix flag check in replay Zhenzhong Duan
2023-06-08 13:43 ` Peter Xu
2023-06-08 9:52 ` [PATCH v3 4/5] intel_iommu: Fix address space unmap Zhenzhong Duan
2023-06-08 13:48 ` Peter Xu
2023-06-09 3:31 ` Duan, Zhenzhong
2023-06-09 13:36 ` Peter Xu
2023-06-13 2:32 ` Duan, Zhenzhong
2023-06-08 9:52 ` [PATCH v3 5/5] intel_iommu: Optimize out some unnecessary UNMAP calls Zhenzhong Duan
2023-06-08 14:05 ` Peter Xu
2023-06-08 14:11 ` Jason Gunthorpe
2023-06-08 15:40 ` Peter Xu
2023-06-08 16:27 ` Jason Gunthorpe
2023-06-08 19:53 ` Peter Xu
2023-06-09 1:00 ` Jason Gunthorpe
2023-06-09 5:49 ` Duan, Zhenzhong
2023-06-09 21:26 ` Peter Xu
2023-06-13 2:37 ` Duan, Zhenzhong
2023-06-14 9:47 ` Duan, Zhenzhong
2023-06-09 4:03 ` Duan, Zhenzhong
2023-06-09 3:41 ` Duan, Zhenzhong
2023-06-08 20:34 ` Peter Xu
2023-06-09 4:01 ` Duan, Zhenzhong
2023-06-14 9:38 ` Duan, Zhenzhong
2023-06-14 12:51 ` Peter Xu
2023-06-08 15:53 ` [PATCH v3 0/5] Optimize UNMAP call and bug fix Peter Xu
2023-06-09 3:32 ` Duan, Zhenzhong
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20230608095231.225450-3-zhenzhong.duan@intel.com \
--to=zhenzhong.duan@intel.com \
--cc=alex.williamson@redhat.com \
--cc=chao.p.peng@intel.com \
--cc=cjia@nvidia.com \
--cc=clg@redhat.com \
--cc=david@redhat.com \
--cc=eduardo@habkost.net \
--cc=jasowang@redhat.com \
--cc=kwankhede@nvidia.com \
--cc=marcel.apfelbaum@gmail.com \
--cc=mst@redhat.com \
--cc=pbonzini@redhat.com \
--cc=peterx@redhat.com \
--cc=philmd@linaro.org \
--cc=qemu-devel@nongnu.org \
--cc=richard.henderson@linaro.org \
--cc=yi.l.liu@intel.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).