1b1826
From daa0c48addc50413b79612d9e7251a9cbf35af48 Mon Sep 17 00:00:00 2001
1b1826
From: Alex Williamson <alex.williamson@redhat.com>
1b1826
Date: Mon, 20 Nov 2017 16:21:44 +0100
1b1826
Subject: [PATCH] vfio/pci: Only mmap >= TARGET_PAGE_SIZE regions
1b1826
1b1826
RH-Author: Alex Williamson <alex.williamson@redhat.com>
1b1826
Message-id: <20171120162044.30263.60064.stgit@gimli.home>
1b1826
Patchwork-id: 77755
1b1826
O-Subject: [RHEL-7.4.z qemu-kvm PATCH] vfio/pci: Only mmap >= TARGET_PAGE_SIZE regions
1b1826
Bugzilla: 1515110
1b1826
RH-Acked-by: Paolo Bonzini <pbonzini@redhat.com>
1b1826
RH-Acked-by: Thomas Huth <thuth@redhat.com>
1b1826
RH-Acked-by: Auger Eric <eric.auger@redhat.com>
1b1826
1b1826
Upstream Status: RHEL-only (very small subset of db0da029a185)
1b1826
Tested: Teradici USB assignment
1b1826
1b1826
Upstream kernel commit 05f0c03fbac1 ('vfio-pci: Allow to mmap sub-page
1b1826
MMIO BARs if the mmio page is exclusive') [RHEL-7.4 390f15a45024] allows
1b1826
vfio-pci to expose the VFIO_REGION_INFO_FLAG_MMAP flag, indicating the
1b1826
region can be mmap'd, for sub-page PCI BARs iff the BAR is page aligned
1b1826
and the remainder of the page can be reserved to ensure that it's not
1b1826
used for other purposes.  Unfortunately QEMU versions prior to v2.6.0
1b1826
blindly accept the MMAP flag with no special handling of these sub-page
1b1826
mmaps.  This went unnoticed upstream, but was inadvertently fixed by
1b1826
commit db0da029a185 ('vfio: Generalize region support') which ensures
1b1826
that the region size is a multiple of page size.  This returns us to
1b1826
the previous behavior where sub-page regions are not mmap'd, even though
1b1826
the kernel now allows it.  This QEMU commit has since been picked up in
1b1826
qemu-kvm with the backport of the above as a33e922436f7.  qemu-kvm-rhev
1b1826
has had this support since RHEL-7.3.  Furthermore, upstream commit
1b1826
95251725e335 ('vfio: Add support for mmapping sub-page MMIO BARs')
1b1826
allows QEMU to fully make use of these sub-page mmaps.  qemu-kvm-rhev
1b1826
acquired this capability in the RHEL-7.4 rebase.
1b1826
1b1826
Here we extract only the portion of db0da029a185 which excludes sub-page
1b1826
regions from being mmap'd.
1b1826
1b1826
Signed-off-by: Alex Williamson <alex.williamson@redhat.com>
1b1826
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
1b1826
---
1b1826
 hw/misc/vfio.c | 3 ++-
1b1826
 1 file changed, 2 insertions(+), 1 deletion(-)
1b1826
1b1826
diff --git a/hw/misc/vfio.c b/hw/misc/vfio.c
1b1826
index 4fdc09a..adfefec 100644
1b1826
--- a/hw/misc/vfio.c
1b1826
+++ b/hw/misc/vfio.c
1b1826
@@ -2576,7 +2576,8 @@ static int vfio_mmap_bar(VFIOBAR *bar, MemoryRegion *mem, MemoryRegion *submem,
1b1826
 {
1b1826
     int ret = 0;
1b1826
 
1b1826
-    if (VFIO_ALLOW_MMAP && size && bar->flags & VFIO_REGION_INFO_FLAG_MMAP) {
1b1826
+    if (VFIO_ALLOW_MMAP && size && bar->flags & VFIO_REGION_INFO_FLAG_MMAP &&
1b1826
+        !(size & ~TARGET_PAGE_MASK)) {
1b1826
         int prot = 0;
1b1826
 
1b1826
         if (bar->flags & VFIO_REGION_INFO_FLAG_READ) {
1b1826
-- 
1b1826
1.8.3.1
1b1826