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

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