From: Zhenzhong Duan <zhenzhong.duan@intel.com>
To: qemu-devel@nongnu.org
Cc: mst@redhat.com, peterx@redhat.com, jasowang@redhat.com,
marcel.apfelbaum@gmail.com, pbonzini@redhat.com,
richard.henderson@linaro.org, eduardo@habkost.net,
yi.l.liu@intel.com, chao.p.peng@intel.com
Subject: [PATCH] intel_iommu: Optimize out some unnecessary UNMAP calls
Date: Tue, 23 May 2023 16:07:02 +0800 [thread overview]
Message-ID: <20230523080702.179363-1-zhenzhong.duan@intel.com> (raw)
Commit 63b88968f1 ("intel-iommu: rework the page walk logic") adds logic
to record mapped IOVA ranges so we only need to send MAP or UNMAP when
necessary. But there are still a few corner cases of unnecessary UNMAP.
One is address space switch. During switching to iommu address space,
all the original mappings have been dropped by VFIO memory listener,
we don't need to unmap again in replay. The other is invalidation,
we only need to unmap when there are recorded mapped IOVA ranges,
presuming most of OSes allocating IOVA range continuously,
ex. on x86, linux sets up mapping from 0xffffffff downwards.
Signed-off-by: Zhenzhong Duan <zhenzhong.duan@intel.com>
---
Tested on x86 with a net card passed or hotpluged to kvm guest,
ping/ssh pass.
hw/i386/intel_iommu.c | 31 ++++++++++++++-----------------
1 file changed, 14 insertions(+), 17 deletions(-)
diff --git a/hw/i386/intel_iommu.c b/hw/i386/intel_iommu.c
index 94d52f4205d2..6afd6428aaaa 100644
--- a/hw/i386/intel_iommu.c
+++ b/hw/i386/intel_iommu.c
@@ -3743,6 +3743,7 @@ static void vtd_address_space_unmap(VTDAddressSpace *as, IOMMUNotifier *n)
hwaddr start = n->start;
hwaddr end = n->end;
IntelIOMMUState *s = as->iommu_state;
+ IOMMUTLBEvent event;
DMAMap map;
/*
@@ -3762,22 +3763,25 @@ static void vtd_address_space_unmap(VTDAddressSpace *as, IOMMUNotifier *n)
assert(start <= end);
size = remain = end - start + 1;
+ event.type = IOMMU_NOTIFIER_UNMAP;
+ event.entry.target_as = &address_space_memory;
+ event.entry.perm = IOMMU_NONE;
+ /* This field is meaningless for unmap */
+ event.entry.translated_addr = 0;
+
while (remain >= VTD_PAGE_SIZE) {
- IOMMUTLBEvent event;
uint64_t mask = dma_aligned_pow2_mask(start, end, s->aw_bits);
uint64_t size = mask + 1;
assert(size);
- event.type = IOMMU_NOTIFIER_UNMAP;
- event.entry.iova = start;
- event.entry.addr_mask = mask;
- 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);
+ map.iova = start;
+ map.size = size;
+ if (iova_tree_find(as->iova_tree, &map)) {
+ event.entry.iova = start;
+ event.entry.addr_mask = mask;
+ memory_region_notify_iommu_one(n, &event);
+ }
start += size;
remain -= size;
@@ -3826,13 +3830,6 @@ static void vtd_iommu_replay(IOMMUMemoryRegion *iommu_mr, IOMMUNotifier *n)
uint8_t bus_n = pci_bus_num(vtd_as->bus);
VTDContextEntry ce;
- /*
- * 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);
-
if (vtd_dev_to_context_entry(s, bus_n, vtd_as->devfn, &ce) == 0) {
trace_vtd_replay_ce_valid(s->root_scalable ? "scalable mode" :
"legacy mode",
--
2.34.1
next reply other threads:[~2023-05-23 8:19 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-05-23 8:07 Zhenzhong Duan [this message]
2023-05-24 16:58 ` [PATCH] intel_iommu: Optimize out some unnecessary UNMAP calls Peter Xu
2023-05-25 11:29 ` Duan, Zhenzhong
2023-05-25 13:53 ` Peter Xu
2023-05-26 6:22 ` Duan, Zhenzhong
2023-05-26 6:28 ` Jason Wang
2023-05-26 8:44 ` Liu, Yi L
2023-05-29 17:00 ` Jason Gunthorpe
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=20230523080702.179363-1-zhenzhong.duan@intel.com \
--to=zhenzhong.duan@intel.com \
--cc=chao.p.peng@intel.com \
--cc=eduardo@habkost.net \
--cc=jasowang@redhat.com \
--cc=marcel.apfelbaum@gmail.com \
--cc=mst@redhat.com \
--cc=pbonzini@redhat.com \
--cc=peterx@redhat.com \
--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).