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

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