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(-)
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.
Cc: qemu-stable@nongnu.org
Fixes: 37fa32de7073 ("s390x/pci: Honor DMA limits set by vfio")
Signed-off-by: Omar Elghoul <oelghoul@linux.ibm.com>
---
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(-)
diff --git a/hw/s390x/s390-pci-bus.c b/hw/s390x/s390-pci-bus.c
index 2724b27ec2..9938191c4d 100644
--- a/hw/s390x/s390-pci-bus.c
+++ b/hw/s390x/s390-pci-bus.c
@@ -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)
diff --git a/hw/s390x/s390-pci-vfio-stubs.c b/hw/s390x/s390-pci-vfio-stubs.c
index 9fc84ca135..5e0782ac0e 100644
--- a/hw/s390x/s390-pci-vfio-stubs.c
+++ b/hw/s390x/s390-pci-vfio-stubs.c
@@ -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;
}
diff --git a/hw/s390x/s390-pci-vfio.c b/hw/s390x/s390-pci-vfio.c
index e65e2be473..f9a454ef7d 100644
--- a/hw/s390x/s390-pci-vfio.c
+++ b/hw/s390x/s390-pci-vfio.c
@@ -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++;
diff --git a/include/hw/s390x/s390-pci-vfio.h b/include/hw/s390x/s390-pci-vfio.h
index c7886b63ea..25d9e74f94 100644
--- a/include/hw/s390x/s390-pci-vfio.h
+++ b/include/hw/s390x/s390-pci-vfio.h
@@ -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);
--
2.56.0
On 10/19/26 11:37 AM, Omar Elghoul wrote:
> 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
Maybe something like 'doesn't match the previously-stashed limit' ?
> similar DMA accounting bugs in the future.
>
> Cc: qemu-stable@nongnu.org
> Fixes: 37fa32de7073 ("s390x/pci: Honor DMA limits set by vfio")
> Signed-off-by: Omar Elghoul <oelghoul@linux.ibm.com>
> ---
> 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(-)
>
> diff --git a/hw/s390x/s390-pci-bus.c b/hw/s390x/s390-pci-bus.c
> index 2724b27ec2..9938191c4d 100644
> --- a/hw/s390x/s390-pci-bus.c
> +++ b/hw/s390x/s390-pci-bus.c
> @@ -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
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.
> + */
> + 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.
> + 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.
> + pbdev->dma_limit->avail = dma_avail;
> + }
> + }
> + }
> }
On 10/19/26 3:18 PM, Matthew Rosato wrote:
> On 10/19/26 11:37 AM, Omar Elghoul wrote:
>> 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
>
> Maybe something like 'doesn't match the previously-stashed limit' ?
Sure.
>
>> similar DMA accounting bugs in the future.
>>
>> Cc: qemu-stable@nongnu.org
>> Fixes: 37fa32de7073 ("s390x/pci: Honor DMA limits set by vfio")
>> Signed-off-by: Omar Elghoul <oelghoul@linux.ibm.com>
>> ---
>> 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(-)
>>
>> diff --git a/hw/s390x/s390-pci-bus.c b/hw/s390x/s390-pci-bus.c
>> index 2724b27ec2..9938191c4d 100644
>> --- a/hw/s390x/s390-pci-bus.c
>> +++ b/hw/s390x/s390-pci-bus.c
>> @@ -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
>
> 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.
>
>> + */
>> + 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.
>
>> + 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.
>
>> + pbdev->dma_limit->avail = dma_avail;
>> + }
>> + }
>> + }
>> }
© 2016 - 2026 Red Hat, Inc.