Blame SOURCES/kvm-memory-Skip-bad-range-assertion-if-notifier-is-DEVIO.patch

c687bc
From 8c5154729effda3f762bfb8224f9c61dab8b2986 Mon Sep 17 00:00:00 2001
c687bc
From: eperezma <eperezma@redhat.com>
c687bc
Date: Tue, 12 Jan 2021 14:36:38 -0500
c687bc
Subject: [PATCH 14/17] memory: Skip bad range assertion if notifier is
c687bc
 DEVIOTLB_UNMAP type
c687bc
MIME-Version: 1.0
c687bc
Content-Type: text/plain; charset=UTF-8
c687bc
Content-Transfer-Encoding: 8bit
c687bc
c687bc
RH-Author: eperezma <eperezma@redhat.com>
c687bc
Message-id: <20210112143638.374060-14-eperezma@redhat.com>
c687bc
Patchwork-id: 100606
c687bc
O-Subject: [RHEL-8.4.0 qemu-kvm PATCH v2 13/13] memory: Skip bad range assertion if notifier is DEVIOTLB_UNMAP type
c687bc
Bugzilla: 1843852
c687bc
RH-Acked-by: Xiao Wang <jasowang@redhat.com>
c687bc
RH-Acked-by: Peter Xu <peterx@redhat.com>
c687bc
RH-Acked-by: Auger Eric <eric.auger@redhat.com>
c687bc
c687bc
Device IOTLB invalidations can unmap arbitrary ranges, eiter outside of
c687bc
the memory region or even [0, ~0ULL] for all the space. The assertion
c687bc
could be hit by a guest, and rhel7 guest effectively hit it.
c687bc
c687bc
Signed-off-by: Eugenio Pérez <eperezma@redhat.com>
c687bc
Reviewed-by: Peter Xu <peterx@redhat.com>
c687bc
Reviewed-by: Juan Quintela <quintela@redhat.com>
c687bc
Acked-by: Jason Wang <jasowang@redhat.com>
c687bc
Message-Id: <20201116165506.31315-6-eperezma@redhat.com>
c687bc
Reviewed-by: Michael S. Tsirkin <mst@redhat.com>
c687bc
Signed-off-by: Michael S. Tsirkin <mst@redhat.com>
c687bc
(cherry picked from commit 1804857f19f612f6907832e35599cdb51d4ec764)
c687bc
Signed-off-by: Eugenio Pérez <eperezma@redhat.com>
c687bc
Signed-off-by: Danilo C. L. de Paula <ddepaula@redhat.com>
c687bc
---
c687bc
 memory.c | 11 +++++++++--
c687bc
 1 file changed, 9 insertions(+), 2 deletions(-)
c687bc
c687bc
diff --git a/memory.c b/memory.c
c687bc
index 3bd99b8ac4a..5a4a80842d7 100644
c687bc
--- a/memory.c
c687bc
+++ b/memory.c
c687bc
@@ -1916,6 +1916,7 @@ void memory_region_notify_iommu_one(IOMMUNotifier *notifier,
c687bc
 {
c687bc
     IOMMUTLBEntry *entry = &event->entry;
c687bc
     hwaddr entry_end = entry->iova + entry->addr_mask;
c687bc
+    IOMMUTLBEntry tmp = *entry;
c687bc
 
c687bc
     if (event->type == IOMMU_NOTIFIER_UNMAP) {
c687bc
         assert(entry->perm == IOMMU_NONE);
c687bc
@@ -1929,10 +1930,16 @@ void memory_region_notify_iommu_one(IOMMUNotifier *notifier,
c687bc
         return;
c687bc
     }
c687bc
 
c687bc
-    assert(entry->iova >= notifier->start && entry_end <= notifier->end);
c687bc
+    if (notifier->notifier_flags & IOMMU_NOTIFIER_DEVIOTLB_UNMAP) {
c687bc
+        /* Crop (iova, addr_mask) to range */
c687bc
+        tmp.iova = MAX(tmp.iova, notifier->start);
c687bc
+        tmp.addr_mask = MIN(entry_end, notifier->end) - tmp.iova;
c687bc
+    } else {
c687bc
+        assert(entry->iova >= notifier->start && entry_end <= notifier->end);
c687bc
+    }
c687bc
 
c687bc
     if (event->type & notifier->notifier_flags) {
c687bc
-        notifier->notify(notifier, entry);
c687bc
+        notifier->notify(notifier, &tmp);
c687bc
     }
c687bc
 }
c687bc
 
c687bc
-- 
c687bc
2.27.0
c687bc