Skip to contentSkip to conversation

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

PATCH In review Omar Elghoul 2 participants 3 messages Oct 19

lore
Share & tools
lore
#0 lore raw

PATCH v2 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 37fa32de7073 ("s390x/pci: Honor DMA limits set by vfio")

---
v1 -> v2: rebase on latest master, ensure that the device is a vfio-pci device before calling vfio-specific code.

---
 hw/s390x/s390-pci-bus.c          | 21 +++++++++++++++++++++
 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, 35 insertions(+), 8 deletions(-)
hw/s390x/s390-pci-bus.c +21 -0
index 2724b27ec2..9938191c4d 100644
@@ -825,6 +825,8 @@ void s390_pci_iommu_direct_map_enable(S390PCIBusDevice *pbdev)
 void s390_pci_iommu_disable(S390PCIBusDevice *pbdev)
 {
     S390PCIIOMMU *iommu = pbdev->iommu;
+    unsigned int dma_avail;
+
     pbdev->iommu_enabled = false;
     g_hash_table_remove_all(pbdev->iotlb);
     if (pbdev->dm_mr) {
@@ -837,6 +839,25 @@ void s390_pci_iommu_disable(S390PCIBusDevice *pbdev)
                                     MEMORY_REGION(&pbdev->iommu_mr));
         object_unparent(OBJECT(&pbdev->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 (object_dynamic_cast(OBJECT(pbdev->pdev), "vfio-pci")) {
+        if (s390_pci_update_dma_avail(pbdev, &dma_avail)) {
+            if (pbdev->max_dma_limit != dma_avail) {
+                warn_report("DMA limit %u from vfio != %" PRIu64 " shadowed",
+                            dma_avail, pbdev->max_dma_limit);
+                pbdev->max_dma_limit = dma_avail;
+            }
+
+            if (pbdev->dma_limit) {
+                pbdev->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 9fc84ca135..5e0782ac0e 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 e65e2be473..f9a454ef7d 100644
@@ -30,13 +30,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->vbasedev.group || !vpdev->vbasedev.group->container) {
+        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
@@ -69,16 +79,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 c7886b63ea..25d9e74f94 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 6 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

Maybe something like 'doesn't match the previously-stashed limit' ?

Quoted 29 lines

similar DMA accounting bugs in the future.

Fixes 37fa32de7073 ("s390x/pci: Honor DMA limits set by vfio")

---
v1 -> v2: rebase on latest master, ensure that the device is a vfio-pci device before calling vfio-specific code.

---
 hw/s390x/s390-pci-bus.c          | 21 +++++++++++++++++++++
 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, 35 insertions(+), 8 deletions(-)
hw/s390x/s390-pci-bus.c +2 -0
index 2724b27ec2..9938191c4d 100644
@@ -825,6 +825,8 @@ void s390_pci_iommu_direct_map_enable(S390PCIBusDevice *pbdev)
 void s390_pci_iommu_disable(S390PCIBusDevice *pbdev)
 {
     S390PCIIOMMU *iommu = pbdev->iommu;
+    unsigned int dma_avail;
+
     pbdev->iommu_enabled = false;
     g_hash_table_remove_all(pbdev->iotlb);
     if (pbdev->dm_mr) {

hw/s390x/s390-pci-bus.c void s390_pci_iommu_disable(S390PCIBusDevice *pbdev) · line 846

Quoted 8 lines
hw/s390x/s390-pci-bus.c +5 -0
@@ -837,6 +839,25 @@ void s390_pci_iommu_disable(S390PCIBusDevice *pbdev)
                                     MEMORY_REGION(&pbdev->iommu_mr));
         object_unparent(OBJECT(&pbdev->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

comment nit: This is related to the commit message -- we aren't looking at the previous shadow count here (dma_limit->avail) but rather at the previously stashed limit (max_dma_limit) to make sure the vfio count got back to the value we expected.

hw/s390x/s390-pci-bus.c void s390_pci_iommu_disable(S390PCIBusDevice *pbdev) · line 848

Quoted 2 lines
hw/s390x/s390-pci-bus.c +2 -0
+     */
+    if (object_dynamic_cast(OBJECT(pbdev->pdev), "vfio-pci")) {

Could you just check for if (pbev->dma_avail) here?

We should only have that if s390_pci_start_dma_count() was called successfully, and that should only happen for a vfio-pci device.

hw/s390x/s390-pci-bus.c void s390_pci_iommu_disable(S390PCIBusDevice *pbdev) · line 856

Quoted 8 lines
hw/s390x/s390-pci-bus.c +8 -0
+        if (s390_pci_update_dma_avail(pbdev, &dma_avail)) {
+            if (pbdev->max_dma_limit != dma_avail) {
+                warn_report("DMA limit %u from vfio != %" PRIu64 " shadowed",
+                            dma_avail, pbdev->max_dma_limit);
+                pbdev->max_dma_limit = dma_avail;
+            }
+
+            if (pbdev->dma_limit) {

Then you also don't need this check.

Quoted 5 lines
hw/s390x/s390-pci-bus.c +4 -0
+                pbdev->dma_limit->avail = dma_avail;
+            }
+        }
+    }
 }
#2 lore raw
Quoted 8 lines · Matthew Rosato
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
Quoted 6 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

Maybe something like 'doesn't match the previously-stashed limit' ?

Sure.

Quoted 44 lines
Quoted 30 lines

similar DMA accounting bugs in the future.

Fixes 37fa32de7073 ("s390x/pci: Honor DMA limits set by vfio")

---
v1 -> v2: rebase on latest master, ensure that the device is a vfio-pci device before calling vfio-specific code.

---
  hw/s390x/s390-pci-bus.c          | 21 +++++++++++++++++++++
  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, 35 insertions(+), 8 deletions(-)
hw/s390x/s390-pci-bus.c +2 -0
index 2724b27ec2..9938191c4d 100644
@@ -825,6 +825,8 @@ void s390_pci_iommu_direct_map_enable(S390PCIBusDevice *pbdev)
  void s390_pci_iommu_disable(S390PCIBusDevice *pbdev)
  {
      S390PCIIOMMU *iommu = pbdev->iommu;
+    unsigned int dma_avail;
+
      pbdev->iommu_enabled = false;
      g_hash_table_remove_all(pbdev->iotlb);
      if (pbdev->dm_mr) {

hw/s390x/s390-pci-bus.c void s390_pci_iommu_disable(S390PCIBusDevice *pbdev) · line 846

Quoted 8 lines
hw/s390x/s390-pci-bus.c +5 -0
@@ -837,6 +839,25 @@ void s390_pci_iommu_disable(S390PCIBusDevice *pbdev)
                                      MEMORY_REGION(&pbdev->iommu_mr));
          object_unparent(OBJECT(&pbdev->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

comment nit: This is related to the commit message -- we aren't looking at the previous shadow count here (dma_limit->avail) but rather at the previously stashed limit (max_dma_limit) to make sure the vfio count got back to the value we expected.

Quoted 7 lines

hw/s390x/s390-pci-bus.c void s390_pci_iommu_disable(S390PCIBusDevice *pbdev) · line 848

Quoted 2 lines
hw/s390x/s390-pci-bus.c +2 -0
+     */
+    if (object_dynamic_cast(OBJECT(pbdev->pdev), "vfio-pci")) {

Could you just check for if (pbev->dma_avail) here?

We should only have that if s390_pci_start_dma_count() was called successfully, and that should only happen for a vfio-pci device.

That works too, I just thought it'd make sense to explicitly check for the "vfio-pci" cast since the same file already does that in another place.

Quoted 17 lines

hw/s390x/s390-pci-bus.c void s390_pci_iommu_disable(S390PCIBusDevice *pbdev) · line 856

Quoted 9 lines
hw/s390x/s390-pci-bus.c +8 -0
+        if (s390_pci_update_dma_avail(pbdev, &dma_avail)) {
+            if (pbdev->max_dma_limit != dma_avail) {
+                warn_report("DMA limit %u from vfio != %" PRIu64 " shadowed",
+                            dma_avail, pbdev->max_dma_limit);
+                pbdev->max_dma_limit = dma_avail;
+            }
+
+            if (pbdev->dma_limit) {

Then you also don't need this check.

Quoted 5 lines
hw/s390x/s390-pci-bus.c +4 -0
+                pbdev->dma_limit->avail = dma_avail;
+            }
+        }
+    }
  }