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

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