Blame SOURCES/kvm-s390x-pci-shrink-DMA-aperture-to-be-bound-by-vfio-DM.patch

97168e
From a0b6c21b555566eb6bc38643269d14c82dfd0226 Mon Sep 17 00:00:00 2001
97168e
From: Matthew Rosato <mjrosato@linux.ibm.com>
97168e
Date: Fri, 28 Oct 2022 15:47:58 -0400
97168e
Subject: [PATCH 10/11] s390x/pci: shrink DMA aperture to be bound by vfio DMA
97168e
 limit
97168e
MIME-Version: 1.0
97168e
Content-Type: text/plain; charset=UTF-8
97168e
Content-Transfer-Encoding: 8bit
97168e
97168e
RH-Author: Cédric Le Goater <clg@redhat.com>
97168e
RH-MergeRequest: 250: s390x/pci: reset ISM passthrough devices on shutdown and system reset
97168e
RH-Bugzilla: 2163713
97168e
RH-Acked-by: Thomas Huth <thuth@redhat.com>
97168e
RH-Acked-by: Cornelia Huck <cohuck@redhat.com>
97168e
RH-Acked-by: Miroslav Rezanina <mrezanin@redhat.com>
97168e
RH-Commit: [3/4] aa241dd250ad5e696b67c87dddc31ee5aaee9c0e
97168e
97168e
Currently, s390x-pci performs accounting against the vfio DMA
97168e
limit and triggers the guest to clean up mappings when the limit
97168e
is reached. Let's go a step further and also limit the size of
97168e
the supported DMA aperture reported to the guest based upon the
97168e
initial vfio DMA limit reported for the container (if less than
97168e
than the size reported by the firmware/host zPCI layer).  This
97168e
avoids processing sections of the guest DMA table during global
97168e
refresh that, for common use cases, will never be used anway, and
97168e
makes exhausting the vfio DMA limit due to mismatch between guest
97168e
aperture size and host limit far less likely and more indicitive
97168e
of an error.
97168e
97168e
Signed-off-by: Matthew Rosato <mjrosato@linux.ibm.com>
97168e
Message-Id: <20221028194758.204007-4-mjrosato@linux.ibm.com>
97168e
Reviewed-by: Eric Farman <farman@linux.ibm.com>
97168e
Signed-off-by: Thomas Huth <thuth@redhat.com>
97168e
(cherry picked from commit df202e3ff3fccb49868e08f20d0bda86cb953fbe)
97168e
Signed-off-by: Cédric Le Goater <clg@redhat.com>
97168e
---
97168e
 hw/s390x/s390-pci-vfio.c        | 11 +++++++++++
97168e
 include/hw/s390x/s390-pci-bus.h |  1 +
97168e
 2 files changed, 12 insertions(+)
97168e
97168e
diff --git a/hw/s390x/s390-pci-vfio.c b/hw/s390x/s390-pci-vfio.c
97168e
index 2aefa508a0..99806e2a84 100644
97168e
--- a/hw/s390x/s390-pci-vfio.c
97168e
+++ b/hw/s390x/s390-pci-vfio.c
97168e
@@ -84,6 +84,7 @@ S390PCIDMACount *s390_pci_start_dma_count(S390pciState *s,
97168e
     cnt->users = 1;
97168e
     cnt->avail = avail;
97168e
     QTAILQ_INSERT_TAIL(&s->zpci_dma_limit, cnt, link);
97168e
+    pbdev->iommu->max_dma_limit = avail;
97168e
     return cnt;
97168e
 }
97168e
 
97168e
@@ -103,6 +104,7 @@ static void s390_pci_read_base(S390PCIBusDevice *pbdev,
97168e
     struct vfio_info_cap_header *hdr;
97168e
     struct vfio_device_info_cap_zpci_base *cap;
97168e
     VFIOPCIDevice *vpci =  container_of(pbdev->pdev, VFIOPCIDevice, pdev);
97168e
+    uint64_t vfio_size;
97168e
 
97168e
     hdr = vfio_get_device_info_cap(info, VFIO_DEVICE_INFO_CAP_ZPCI_BASE);
97168e
 
97168e
@@ -122,6 +124,15 @@ static void s390_pci_read_base(S390PCIBusDevice *pbdev,
97168e
     /* The following values remain 0 until we support other FMB formats */
97168e
     pbdev->zpci_fn.fmbl = 0;
97168e
     pbdev->zpci_fn.pft = 0;
97168e
+
97168e
+    /*
97168e
+     * If appropriate, reduce the size of the supported DMA aperture reported
97168e
+     * to the guest based upon the vfio DMA limit.
97168e
+     */
97168e
+    vfio_size = pbdev->iommu->max_dma_limit << TARGET_PAGE_BITS;
97168e
+    if (vfio_size < (cap->end_dma - cap->start_dma + 1)) {
97168e
+        pbdev->zpci_fn.edma = cap->start_dma + vfio_size - 1;
97168e
+    }
97168e
 }
97168e
 
97168e
 static bool get_host_fh(S390PCIBusDevice *pbdev, struct vfio_device_info *info,
97168e
diff --git a/include/hw/s390x/s390-pci-bus.h b/include/hw/s390x/s390-pci-bus.h
97168e
index 0605fcea24..1c46e3a269 100644
97168e
--- a/include/hw/s390x/s390-pci-bus.h
97168e
+++ b/include/hw/s390x/s390-pci-bus.h
97168e
@@ -278,6 +278,7 @@ struct S390PCIIOMMU {
97168e
     uint64_t g_iota;
97168e
     uint64_t pba;
97168e
     uint64_t pal;
97168e
+    uint64_t max_dma_limit;
97168e
     GHashTable *iotlb;
97168e
     S390PCIDMACount *dma_limit;
97168e
 };
97168e
-- 
97168e
2.37.3
97168e