qemu-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Qemu-devel] [PATCH] memory: do not do out of bound notification


From: Paolo Bonzini
Subject: Re: [Qemu-devel] [PATCH] memory: do not do out of bound notification
Date: Thu, 20 Jun 2019 10:35:29 +0200
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.6.1

On 20/06/19 06:02, Peter Xu wrote:
> Seems workable, to be explicit - we can even cut it into chunks with
> different size to be efficient.

Yes, this is not hard (completely untested):

diff --git a/hw/i386/intel_iommu.c b/hw/i386/intel_iommu.c
index 44b1231157..541538bc6c 100644
--- a/hw/i386/intel_iommu.c
+++ b/hw/i386/intel_iommu.c
@@ -3388,39 +3388,34 @@ static void vtd_address_space_unmap(VTDAddressSpace 
*as, IOMMUNotifier *n)
     }
 
     assert(start <= end);
-    size = end - start;
+    while (end > start) {
+        size = end - start;
+        /* Only keep the lowest bit of either size or start.  */
+        size = MIN(size & -size, start & -start);
+        /* Should not happen, but limit to address width too just in case */
+        size = MIN(size, 1ULL << s->aw_bits);
 
-    if (ctpop64(size) != 1) {
-        /*
-         * This size cannot format a correct mask. Let's enlarge it to
-         * suite the minimum available mask.
-         */
-        int n = 64 - clz64(size);
-        if (n > s->aw_bits) {
-            /* should not happen, but in case it happens, limit it */
-            n = s->aw_bits;
-        }
-        size = 1ULL << n;
-    }
+        assert((start & (size - 1)) == 0);
 
-    entry.target_as = &address_space_memory;
-    /* Adjust iova for the size */
-    entry.iova = n->start & ~(size - 1);
-    /* This field is meaningless for unmap */
-    entry.translated_addr = 0;
-    entry.perm = IOMMU_NONE;
-    entry.addr_mask = size - 1;
+        entry.target_as = &address_space_memory;
+        entry.iova = start;
+        /* This field is meaningless for unmap */
+        entry.translated_addr = 0;
+        entry.perm = IOMMU_NONE;
+        entry.addr_mask = size - 1;
 
-    trace_vtd_as_unmap_whole(pci_bus_num(as->bus),
-                             VTD_PCI_SLOT(as->devfn),
-                             VTD_PCI_FUNC(as->devfn),
-                             entry.iova, size);
+        trace_vtd_as_unmap_whole(pci_bus_num(as->bus),
+                                 VTD_PCI_SLOT(as->devfn),
+                                 VTD_PCI_FUNC(as->devfn),
+                                 entry.iova, size);
 
-    map.iova = entry.iova;
-    map.size = entry.addr_mask;
-    iova_tree_remove(as->iova_tree, &map);
+        map.iova = entry.iova;
+        map.size = entry.addr_mask;
+        iova_tree_remove(as->iova_tree, &map);
 
-    memory_region_notify_one(n, &entry);
+        memory_region_notify_one(n, &entry);
+        start += size;
+    }
 }
 
 static void vtd_address_space_unmap_all(IntelIOMMUState *s)


Yan,

if something like this works for you, let me know and I will submit it
as a proper patch.

Paolo



reply via email to

[Prev in Thread] Current Thread [Next in Thread]