[PATCH v2] iommu/dma: Skip iova_to_phys() on unmap when unneeded

From: Joe Damato

Date: Wed Dec 02 2026 - 10:33:47 EST


iommu_dma_unmap_phys() looks up the physical address of every mapping
with iommu_iova_to_phys() before unmapping it.

This is unneeded for a coherent device that does not use a swiotlb.
Skipping this saves a page table walk.

iommu_dma_sync_single_for_cpu() already skips the walk for these
devices, so do the same in iommu_dma_unmap_phys().

For these devices the WARN_ON(!phys) is no longer reached, so unmapping
an IOVA that is not mapped no longer warns there. CONFIG_DMA_API_DEBUG
still catches this class of driver bug.

Signed-off-by: Joe Damato <joe@xxxxxxx>
---
v2:
- Squash into one patch and drop the helper; open-code the condition
as Robin suggested.
- Rebase on iommu/core.

v1: https://lore.kernel.org/20261106175745.3362057-1-joe@xxxxxxx/

Measured on prod hosts serving TX heavy network traffic, which unmaps TX
buffers on completion:

- Intel Xeon Gold 6138 (Skylake), VT-d: iova_to_phys() on unmap was
119-143 ns per TX packet (~0.5% of busy CPU) before, 0 after.
- AMD EPYC 9334 (Genoa), AMD IOMMU: ~10 ns per TX packet (~0.1% of busy
CPU) before, 0 after.

drivers/iommu/dma-iommu.c | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/drivers/iommu/dma-iommu.c b/drivers/iommu/dma-iommu.c
index 7c5abb77f4d6..80448fe533bf 100644
--- a/drivers/iommu/dma-iommu.c
+++ b/drivers/iommu/dma-iommu.c
@@ -1256,7 +1256,8 @@ void iommu_dma_unmap_phys(struct device *dev, dma_addr_t dma_handle,
{
phys_addr_t phys;

- if (attrs & (DMA_ATTR_MMIO | DMA_ATTR_REQUIRE_COHERENT)) {
+ if ((attrs & (DMA_ATTR_MMIO | DMA_ATTR_REQUIRE_COHERENT)) ||
+ (dev_is_dma_coherent(dev) && !dev_use_swiotlb(dev, size, dir))) {
__iommu_dma_unmap(dev, dma_handle, size);
return;
}

base-commit: a0aa8306adbb522f3f2a26393cc1e12a1836f084
--
2.53.1-Meta