Blame SOURCES/kvm-intel-iommu-only-do-page-walk-for-MAP-notifiers.patch

383d26
From b6bc2d692f4f3c41b61c0917d4709e91827c8432 Mon Sep 17 00:00:00 2001
383d26
From: Peter Xu <peterx@redhat.com>
383d26
Date: Mon, 3 Sep 2018 04:52:36 +0200
383d26
Subject: [PATCH 21/29] intel-iommu: only do page walk for MAP notifiers
383d26
383d26
RH-Author: Peter Xu <peterx@redhat.com>
383d26
Message-id: <20180903045241.6456-5-peterx@redhat.com>
383d26
Patchwork-id: 82024
383d26
O-Subject: [RHEL-7.6 qemu-kvm-rhev PATCH 4/9] intel-iommu: only do page walk for MAP notifiers
383d26
Bugzilla: 1623859
383d26
RH-Acked-by: Xiao Wang <jasowang@redhat.com>
383d26
RH-Acked-by: Auger Eric <eric.auger@redhat.com>
383d26
RH-Acked-by: Michael S. Tsirkin <mst@redhat.com>
383d26
383d26
For UNMAP-only IOMMU notifiers, we don't need to walk the page tables.
383d26
Fasten that procedure by skipping the page table walk.  That should
383d26
boost performance for UNMAP-only notifiers like vhost.
383d26
383d26
CC: QEMU Stable <qemu-stable@nongnu.org>
383d26
Signed-off-by: Peter Xu <peterx@redhat.com>
383d26
Reviewed-by: Michael S. Tsirkin <mst@redhat.com>
383d26
Signed-off-by: Michael S. Tsirkin <mst@redhat.com>
383d26
(cherry picked from commit 4f8a62a933a79094e44bc1b16b63bb23e62d67b4)
383d26
Signed-off-by: Peter Xu <peterx@redhat.com>
383d26
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
383d26
---
383d26
 hw/i386/intel_iommu.c         | 44 ++++++++++++++++++++++++++++++++++++++-----
383d26
 include/hw/i386/intel_iommu.h |  2 ++
383d26
 2 files changed, 41 insertions(+), 5 deletions(-)
383d26
383d26
diff --git a/hw/i386/intel_iommu.c b/hw/i386/intel_iommu.c
383d26
index 8d4069d..38ccc74 100644
383d26
--- a/hw/i386/intel_iommu.c
383d26
+++ b/hw/i386/intel_iommu.c
383d26
@@ -138,6 +138,12 @@ static inline void vtd_iommu_unlock(IntelIOMMUState *s)
383d26
     qemu_mutex_unlock(&s->iommu_lock);
383d26
 }
383d26
 
383d26
+/* Whether the address space needs to notify new mappings */
383d26
+static inline gboolean vtd_as_has_map_notifier(VTDAddressSpace *as)
383d26
+{
383d26
+    return as->notifier_flags & IOMMU_NOTIFIER_MAP;
383d26
+}
383d26
+
383d26
 /* GHashTable functions */
383d26
 static gboolean vtd_uint64_equal(gconstpointer v1, gconstpointer v2)
383d26
 {
383d26
@@ -1436,14 +1442,36 @@ static void vtd_iotlb_page_invalidate_notify(IntelIOMMUState *s,
383d26
     VTDAddressSpace *vtd_as;
383d26
     VTDContextEntry ce;
383d26
     int ret;
383d26
+    hwaddr size = (1 << am) * VTD_PAGE_SIZE;
383d26
 
383d26
     QLIST_FOREACH(vtd_as, &(s->vtd_as_with_notifiers), next) {
383d26
         ret = vtd_dev_to_context_entry(s, pci_bus_num(vtd_as->bus),
383d26
                                        vtd_as->devfn, &ce);
383d26
         if (!ret && domain_id == VTD_CONTEXT_ENTRY_DID(ce.hi)) {
383d26
-            vtd_page_walk(&ce, addr, addr + (1 << am) * VTD_PAGE_SIZE,
383d26
-                          vtd_page_invalidate_notify_hook,
383d26
-                          (void *)&vtd_as->iommu, true, s->aw_bits);
383d26
+            if (vtd_as_has_map_notifier(vtd_as)) {
383d26
+                /*
383d26
+                 * As long as we have MAP notifications registered in
383d26
+                 * any of our IOMMU notifiers, we need to sync the
383d26
+                 * shadow page table.
383d26
+                 */
383d26
+                vtd_page_walk(&ce, addr, addr + size,
383d26
+                              vtd_page_invalidate_notify_hook,
383d26
+                              (void *)&vtd_as->iommu, true, s->aw_bits);
383d26
+            } else {
383d26
+                /*
383d26
+                 * For UNMAP-only notifiers, we don't need to walk the
383d26
+                 * page tables.  We just deliver the PSI down to
383d26
+                 * invalidate caches.
383d26
+                 */
383d26
+                IOMMUTLBEntry entry = {
383d26
+                    .target_as = &address_space_memory,
383d26
+                    .iova = addr,
383d26
+                    .translated_addr = 0,
383d26
+                    .addr_mask = size - 1,
383d26
+                    .perm = IOMMU_NONE,
383d26
+                };
383d26
+                memory_region_notify_iommu(&vtd_as->iommu, entry);
383d26
+            }
383d26
         }
383d26
     }
383d26
 }
383d26
@@ -2383,6 +2411,9 @@ static void vtd_iommu_notify_flag_changed(IOMMUMemoryRegion *iommu,
383d26
         exit(1);
383d26
     }
383d26
 
383d26
+    /* Update per-address-space notifier flags */
383d26
+    vtd_as->notifier_flags = new;
383d26
+
383d26
     if (old == IOMMU_NOTIFIER_NONE) {
383d26
         QLIST_INSERT_HEAD(&s->vtd_as_with_notifiers, vtd_as, next);
383d26
     } else if (new == IOMMU_NOTIFIER_NONE) {
383d26
@@ -2891,8 +2922,11 @@ static void vtd_iommu_replay(IOMMUMemoryRegion *iommu_mr, IOMMUNotifier *n)
383d26
                                   PCI_FUNC(vtd_as->devfn),
383d26
                                   VTD_CONTEXT_ENTRY_DID(ce.hi),
383d26
                                   ce.hi, ce.lo);
383d26
-        vtd_page_walk(&ce, 0, ~0ULL, vtd_replay_hook, (void *)n, false,
383d26
-                      s->aw_bits);
383d26
+        if (vtd_as_has_map_notifier(vtd_as)) {
383d26
+            /* This is required only for MAP typed notifiers */
383d26
+            vtd_page_walk(&ce, 0, ~0ULL, vtd_replay_hook, (void *)n, false,
383d26
+                          s->aw_bits);
383d26
+        }
383d26
     } else {
383d26
         trace_vtd_replay_ce_invalid(bus_n, PCI_SLOT(vtd_as->devfn),
383d26
                                     PCI_FUNC(vtd_as->devfn));
383d26
diff --git a/include/hw/i386/intel_iommu.h b/include/hw/i386/intel_iommu.h
383d26
index 016e74b..156f35e 100644
383d26
--- a/include/hw/i386/intel_iommu.h
383d26
+++ b/include/hw/i386/intel_iommu.h
383d26
@@ -93,6 +93,8 @@ struct VTDAddressSpace {
383d26
     IntelIOMMUState *iommu_state;
383d26
     VTDContextCacheEntry context_cache_entry;
383d26
     QLIST_ENTRY(VTDAddressSpace) next;
383d26
+    /* Superset of notifier flags that this address space has */
383d26
+    IOMMUNotifierFlag notifier_flags;
383d26
 };
383d26
 
383d26
 struct VTDBus {
383d26
-- 
383d26
1.8.3.1
383d26