Skip to contentSkip to conversation

s390x/pci: re-fetch DMA slot shadow count from VFIO upon IOMMU disable

PATCH Superseded Omar Elghoul 2 participants 2 messages Oct 7 – Oct 15

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 s390x/pci: re-fetch DMA slot shadow count from VFIO upon IOMMU disable

s390_pci_iommu_disable() destroys the I/O TLB and unpins the host's mappings, but it doesn't reset the available DMA slot counter back to the VFIO-advertised limit. This is problematic when an IOMMU is disabled and re-enabled while reusing the container (e.g. during a guest reboot), which may cause the RPCIT path to fail with DMA slot exhaustion errors, manifesting as unexplained ENOMEMs in the guest.

Fix it by refactoring s390_pci_update_dma_avail() to take a pointer to S390PCIBusDevice instead of the plain file descriptor. Call it in s390_pci_iommu_disable() after the host mappings have been unpinned.

Further print a warning if the VFIO-advertised counter doesn't match the in-use shadow counter, to reduce the likelihood of introducing new similar DMA accounting bugs in the future.

Fixes 37fa32de70 ("s390x/pci: Honor DMA limits set by vfio")
---
 hw/s390x/s390-pci-bus.c          | 19 +++++++++++++++++++
 hw/s390x/s390-pci-vfio-stubs.c   |  2 +-
 hw/s390x/s390-pci-vfio.c         | 18 ++++++++++++------
 include/hw/s390x/s390-pci-vfio.h |  2 +-
 4 files changed, 33 insertions(+), 8 deletions(-)
hw/s390x/s390-pci-bus.c +19 -0
index 2eb4e8cec4..0da9f67f42 100644
@@ -762,6 +762,8 @@ void s390_pci_iommu_direct_map_enable(S390PCIIOMMU *iommu)
 
 void s390_pci_iommu_disable(S390PCIIOMMU *iommu)
 {
+    unsigned int dma_avail;
+
     iommu->enabled = false;
     g_hash_table_remove_all(iommu->iotlb);
     if (iommu->dm_mr) {
@@ -774,6 +776,23 @@ void s390_pci_iommu_disable(S390PCIIOMMU *iommu)
                                     MEMORY_REGION(&iommu->iommu_mr));
         object_unparent(OBJECT(&iommu->iommu_mr));
     }
+
+    /*
+     * update shadow counter from vfio after having unpinned all host mappings.
+     * if this value mismatches our previous shadow count, then we likely have
+     * DMA remapping, unmapping, or accounting bugs
+     */
+    if (iommu->pbdev && s390_pci_update_dma_avail(iommu->pbdev, &dma_avail)) {
+        if (iommu->max_dma_limit != dma_avail) {
+            warn_report("zpci: DMA limit %u from vfio != %" PRIu64 " shadowed",
+                        dma_avail, iommu->max_dma_limit);
+            iommu->max_dma_limit = dma_avail;
+        }
+
+        if (iommu->dma_limit) {
+            iommu->dma_limit->avail = dma_avail;
+        }
+    }
 }
 
 static void s390_pci_iommu_free(S390pciState *s, PCIBus *bus, int32_t devfn)
hw/s390x/s390-pci-vfio-stubs.c +1 -1
index d9882b7aad..da41c75602 100644
@@ -7,7 +7,7 @@
 #include "qemu/osdep.h"
 #include "hw/s390x/s390-pci-vfio.h"
 
-bool s390_pci_update_dma_avail(int fd, unsigned int *avail)
+bool s390_pci_update_dma_avail(S390PCIBusDevice *pbdev, unsigned int *avail)
 {
     return false;
 }
hw/s390x/s390-pci-vfio.c +12 -6
index db6de00bd2..caa3d88bc1 100644
@@ -29,13 +29,23 @@
  * limiting DMA requests, false otherwise.  The current available count read
  * from vfio is returned in avail.
  */
-bool s390_pci_update_dma_avail(int fd, unsigned int *avail)
+bool s390_pci_update_dma_avail(S390PCIBusDevice *pbdev, unsigned int *avail)
 {
+    VFIOPCIDevice *vpdev;
     uint32_t argsz = sizeof(struct vfio_iommu_type1_info);
     g_autofree struct vfio_iommu_type1_info *info = g_malloc0(argsz);
+    int fd;
 
+    assert(pbdev);
     assert(avail);
 
+    vpdev = VFIO_PCI_DEVICE(pbdev->pdev);
+    if (!vpdev || !vpdev->vbasedev.group) {
+        return false;
+    }
+
+    fd = vpdev->vbasedev.group->container->fd;
+
     /*
      * If the specified argsz is not large enough to contain all capabilities
      * it will be updated upon return from the ioctl.  Retry until we have
@@ -68,16 +78,12 @@ S390PCIDMACount *s390_pci_start_dma_count(S390pciState *s,
 
     assert(vpdev);
 
-    if (!vpdev->vbasedev.group) {
+    if (!s390_pci_update_dma_avail(pbdev, &avail)) {
         return NULL;
     }
 
     id = vpdev->vbasedev.group->container->fd;
 
-    if (!s390_pci_update_dma_avail(id, &avail)) {
-        return NULL;
-    }
-
     QTAILQ_FOREACH(cnt, &s->zpci_dma_limit, link) {
         if (cnt->id  == id) {
             cnt->users++;
include/hw/s390x/s390-pci-vfio.h +1 -1
index f7d6149daf..c11d5781d2 100644
@@ -14,7 +14,7 @@
 
 #include "hw/s390x/s390-pci-bus.h"
 
-bool s390_pci_update_dma_avail(int fd, unsigned int *avail);
+bool s390_pci_update_dma_avail(S390PCIBusDevice *pbdev, unsigned int *avail);
 S390PCIDMACount *s390_pci_start_dma_count(S390pciState *s,
                                           S390PCIBusDevice *pbdev);
 void s390_pci_end_dma_count(S390pciState *s, S390PCIDMACount *cnt);
Signature · 1 line
2.56.0
#1 lore raw
Quoted 7 lines · Omar Elghoul

s390_pci_iommu_disable() destroys the I/O TLB and unpins the host's mappings, but it doesn't reset the available DMA slot counter back to the VFIO-advertised limit. This is problematic when an IOMMU is disabled and re-enabled while reusing the container (e.g. during a guest reboot), which may cause the RPCIT path to fail with DMA slot exhaustion errors, manifesting as unexplained ENOMEMs in the guest.

Quoted 8 lines

Fix it by refactoring s390_pci_update_dma_avail() to take a pointer to S390PCIBusDevice instead of the plain file descriptor. Call it in s390_pci_iommu_disable() after the host mappings have been unpinned.

Further print a warning if the VFIO-advertised counter doesn't match the in-use shadow counter, to reduce the likelihood of introducing new similar DMA accounting bugs in the future.

Thanks for digging into this. Unfortunately it looks like this won't apply after recent merges into master, can you please rebase for v2?

Also, one issue...

[...]

hw/s390x/s390-pci-vfio.c line 42

Quoted 12 lines
hw/s390x/s390-pci-vfio.c +5 -1
-bool s390_pci_update_dma_avail(int fd, unsigned int *avail)
+bool s390_pci_update_dma_avail(S390PCIBusDevice *pbdev, unsigned int *avail)
 {
+    VFIOPCIDevice *vpdev;
     uint32_t argsz = sizeof(struct vfio_iommu_type1_info);
     g_autofree struct vfio_iommu_type1_info *info = g_malloc0(argsz);
+    int fd;
 
+    assert(pbdev);
     assert(avail);
 
+    vpdev = VFIO_PCI_DEVICE(pbdev->pdev);

We can reach this for devices that are not associated with a vfio-pci device (e.g. virtio-pci) and results in

/usr/src/qemu/hw/vfio/pci.h:123:VFIO_PCI_DEVICE: Object 0x2aa2d8a1410 is not an instance of type vfio-pci-device

2026-10-15 16:44:18.851+0000: shutting down, reason=crashed

Thanks,
Matt