s390x/pci: fix DMA slot leak on I/O TLB entry replacement
PATCH 1/1 s390x/pci: fix DMA slot leak on I/O TLB entry replacement
When s390_pci_update_iotlb() does a mapping update for an IOVA that has an already-existing TLB entry with different permissions or translated address, it unmaps then remaps it. However, at the end of the map path, it unconditionally decrements the available DMA slot counter without a corresponding increment in the intermediate unmap branch.
This causes the DMA slot count to be decremented on every remapping of an active IOVA, leading to a permanent DMA slot leak. This remapping without a prior invalidation and sync is not seen today in well-behaved guests but is allowed by the architecture. Fix it by only decrementing available DMA slots when inserting a brand new mapping.
--- hw/s390x/s390-pci-inst.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
hw/s390x/s390-pci-inst.c +3 -1
@@ -665,6 +665,9 @@ static uint32_t s390_pci_update_iotlb(S390PCIIOMMU *iommu,memory_region_notify_iommu (&iommu->iommu_mr,0 , event); event.type =IOMMU_NOTIFIER_MAP ; event.entry.perm = entry->perm; + }else { +/* only new mappings consume DMA slots */ +dec_dma_avail (iommu); } cache =g_new (S390IOTLBEntry,1 );
@@ -673,7 +676,6 @@ static uint32_t s390_pci_update_iotlb(S390PCIIOMMU *iommu, cache->len =TARGET_PAGE_SIZE ; cache->perm = entry->perm;g_hash_table_replace (iommu->iotlb, &cache->iova, cache); -dec_dma_avail (iommu); }/*
Signature · 1 line
2.55.0
Quoted 24 lines · Omar Elghoul
When
s390_pci_update_iotlb()does a mapping update for an IOVA that has an already-existing TLB entry with different permissions or translated address, it unmaps then remaps it. However, at the end of the map path, it unconditionally decrements the available DMA slot counter without a corresponding increment in the intermediate unmap branch.This causes the DMA slot count to be decremented on every remapping of an active IOVA, leading to a permanent DMA slot leak. This remapping without a prior invalidation and sync is not seen today in well-behaved guests but is allowed by the architecture. Fix it by only decrementing available DMA slots when inserting a brand new mapping.
Fixes 37fa32de70 ("s390x/pci: Honor DMA limits set by vfio")Signed-off-by Omar Elghoul <[email protected]>--- hw/s390x/s390-pci-inst.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)hw/s390x/s390-pci-inst.c +0
-0
hw/s390x/s390-pci-inst.c static uint32_t s390_pci_update_iotlb(S390PCIIOMMU *iommu, · line 671
Quoted 7 lines
hw/s390x/s390-pci-inst.c +3
-0@@ -665,6 +665,9 @@ static uint32_t s390_pci_update_iotlb(S390PCIIOMMU *iommu,memory_region_notify_iommu (&iommu->iommu_mr,0 , event); event.type =IOMMU_NOTIFIER_MAP ; event.entry.perm = entry->perm; + }else { +/* only new mappings consume DMA slots */ +dec_dma_avail (iommu); }
To me the else statement here makes it a little more confusing. I think it would be easier to read, if we increment above when do the unmap in the intermediate step. That way we associate incrementing the available entries with unmap and decrementing it with map operation. But I guess this avoids an additional function call for the same result.
Quoted 10 lines
hw/s390x/s390-pci-inst.c +0
-1cache =g_new (S390IOTLBEntry,1 );@@ -673,7 +676,6 @@ static uint32_t s390_pci_update_iotlb(S390PCIIOMMU *iommu, cache->len =TARGET_PAGE_SIZE ; cache->perm = entry->perm;g_hash_table_replace (iommu->iotlb, &cache->iova, cache); -dec_dma_avail (iommu); }/*
Matthew Rosato
Reviewed-byQuoted 31 lines · Farhan Ali
Quoted 29 lines · Omar Elghoul
When
s390_pci_update_iotlb()does a mapping update for an IOVA that has an already-existing TLB entry with different permissions or translated address, it unmaps then remaps it. However, at the end of the map path, it unconditionally decrements the available DMA slot counter without a corresponding increment in the intermediate unmap branch.This causes the DMA slot count to be decremented on every remapping of an active IOVA, leading to a permanent DMA slot leak. This remapping without a prior invalidation and sync is not seen today in well-behaved guests but is allowed by the architecture. Fix it by only decrementing available DMA slots when inserting a brand new mapping.
Fixes 37fa32de70 ("s390x/pci: Honor DMA limits set by vfio")Signed-off-by Omar Elghoul <[email protected]>---
hw/s390x/s390-pci-inst.c| 4 +++-1 file changed, 3 insertions(+), 1 deletion(-)hw/s390x/s390-pci-inst.c +1
-0@@ -665,6 +665,9 @@ static uint32_t s390_pci_update_iotlb(S390PCIIOMMU *iommu, memory_region_notify_iommu(&iommu->iommu_mr, 0, event); event.type = IOMMU_NOTIFIER_MAP; event.entry.perm = entry->perm; + } else {
Quoted 8 lines
hw/s390x/s390-pci-inst.c static uint32_t s390_pci_update_iotlb(S390PCIIOMMU *iommu, · line 671
Quoted 3 lines
hw/s390x/s390-pci-inst.c +2
-0+ /* only new mappings consume DMA slots */ + dec_dma_avail(iommu); }To me the else statement here makes it a little more confusing. I think it would be easier to read, if we increment above when do the unmap in the intermediate step. That way we associate incrementing the available entries with unmap and decrementing it with map operation. But I guess this avoids an additional function call for the same result.
So first, for the code itself:
As to the readability vs adding an intermediate increment -- maybe if you add another small comment it would help with readability?
Something like:
if (cache) {
/* valid->valid transitions re-use a DMA slot */
...
} else {
/* invalid->valid transitions consume a new DMA slot */
...
}Note I did tweak your wording a little bit.
You could even add a comment in the straight unmap case if you think it would also be helpful e.g.
/* valid->invalid transitions will release a DMA slot */
I think this makes it very clear when the counter should be going up/down/neutral.
Thanks,
Matt
Quoted 50 lines · Matthew Rosato
Quoted 31 lines · Farhan Ali
Quoted 29 lines · Omar Elghoul
When
s390_pci_update_iotlb()does a mapping update for an IOVA that has an already-existing TLB entry with different permissions or translated address, it unmaps then remaps it. However, at the end of the map path, it unconditionally decrements the available DMA slot counter without a corresponding increment in the intermediate unmap branch.This causes the DMA slot count to be decremented on every remapping of an active IOVA, leading to a permanent DMA slot leak. This remapping without a prior invalidation and sync is not seen today in well-behaved guests but is allowed by the architecture. Fix it by only decrementing available DMA slots when inserting a brand new mapping.
Fixes 37fa32de70 ("s390x/pci: Honor DMA limits set by vfio")Signed-off-by Omar Elghoul <[email protected]>--- hw/s390x/s390-pci-inst.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)hw/s390x/s390-pci-inst.c +1
-0@@ -665,6 +665,9 @@ static uint32_t s390_pci_update_iotlb(S390PCIIOMMU *iommu, memory_region_notify_iommu(&iommu->iommu_mr, 0, event); event.type = IOMMU_NOTIFIER_MAP; event.entry.perm = entry->perm; + } else {Quoted 8 lines
hw/s390x/s390-pci-inst.c static uint32_t s390_pci_update_iotlb(S390PCIIOMMU *iommu, · line 671
Quoted 3 lines
hw/s390x/s390-pci-inst.c +2
-0+ /* only new mappings consume DMA slots */ + dec_dma_avail(iommu); }To me the else statement here makes it a little more confusing. I think it would be easier to read, if we increment above when do the unmap in the intermediate step. That way we associate incrementing the available entries with unmap and decrementing it with map operation. But I guess this avoids an additional function call for the same result.
So first, for the code itself:
Reviewed-by Matthew Rosato <[email protected]>As to the readability vs adding an intermediate increment -- maybe if you add another small comment it would help with readability?
Something like:
Quoted 7 lines
if (cache) {
/* valid->valid transitions re-use a DMA slot */
...
} else {
/* invalid->valid transitions consume a new DMA slot */
...
}That's a great suggestion, I will post a v2 with more comments.
Thanks