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

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