* [PATCH 0/2] iommu/dma: Skip iova_to_phys() on unmap for coherent devices
@ 2026-11-06 17:57 Joe Damato
2026-11-06 17:57 ` [PATCH 1/2] iommu/dma: Add helper for coherent devs without swiotlb Joe Damato
` (2 more replies)
0 siblings, 3 replies; 6+ messages in thread
From: Joe Damato @ 2026-11-06 17:57 UTC (permalink / raw)
Cc: Joe Damato, iommu, Joerg Roedel (AMD), linux-kernel, Robin Murphy,
Will Deacon
Greetings:
iommu_dma_unmap_phys() calls iommu_iova_to_phys() on every unmap, which
walks the IOMMU page table. The physical address it returns is only used
for devices that are non-coherent or use a swiotlb.
For a coherent device that does not use swiotlb, the lookup is not
needed. iommu_dma_sync_single_for_cpu() and
iommu_dma_sync_single_for_device() already skip it for these devices, as
does __iommu_dma_iova_unlink() for the dma_iova_link() API.
Patch 1 factors the check out into a helper. Patch 2 uses the helper to
skip the lookup in iommu_dma_unmap_phys().
I split this into two patches because I thought it would make review and
bisect easier, but I can make them a single patch if needed.
One behavior change: for these devices the WARN_ON(!phys) in
iommu_dma_unmap_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.
I tested 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: 119-143 ns per TX packet (~0.5% of busy
CPU) before, 0 after
AMD EPYC 9334 (Genoa), AMD IOMMU:
- iova_to_phys() on unmap: ~10 ns per TX packet (~0.1% of busy CPU)
before, 0 after
So, the performance improvement is small, but measureable. The proposed
change removes redundant work in the unmap, though, making it match the map
path.
Thanks,
Joe
Joe Damato (2):
iommu/dma: Add helper for coherent devs without swiotlb
iommu/dma: Skip iova_to_phys() on unmap when unneeded
drivers/iommu/dma-iommu.c | 14 +++++++++++---
1 file changed, 11 insertions(+), 3 deletions(-)
base-commit: f66469d9905b39d2c260a15d7d6db450f4db67c1
--
2.53.0-Meta
^ permalink raw reply [flat|nested] 6+ messages in thread* [PATCH 1/2] iommu/dma: Add helper for coherent devs without swiotlb 2026-11-06 17:57 [PATCH 0/2] iommu/dma: Skip iova_to_phys() on unmap for coherent devices Joe Damato @ 2026-11-06 17:57 ` Joe Damato 2026-12-01 20:30 ` Robin Murphy 2026-11-06 17:57 ` [PATCH 2/2] iommu/dma: Skip iova_to_phys() on unmap when unneeded Joe Damato 2026-12-01 17:48 ` [PATCH 0/2] iommu/dma: Skip iova_to_phys() on unmap for coherent devices Joe Damato 2 siblings, 1 reply; 6+ messages in thread From: Joe Damato @ 2026-11-06 17:57 UTC (permalink / raw) To: Robin Murphy, Joerg Roedel (AMD), Will Deacon Cc: Joe Damato, iommu, linux-kernel Factor out some repeated logic which checks if a device is coherent and if there is a swiotlb. Replace repeated instances of this logic with a call to the helper instead. Signed-off-by: Joe Damato <joe@dama.to> --- drivers/iommu/dma-iommu.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/drivers/iommu/dma-iommu.c b/drivers/iommu/dma-iommu.c index 78742aeba932..a25ee287a5ac 100644 --- a/drivers/iommu/dma-iommu.c +++ b/drivers/iommu/dma-iommu.c @@ -628,6 +628,12 @@ static bool dev_use_sg_swiotlb(struct device *dev, struct scatterlist *sg, return false; } +static bool dev_is_coherent_no_swiotlb(struct device *dev, size_t size, + enum dma_data_direction dir) +{ + return dev_is_dma_coherent(dev) && !dev_use_swiotlb(dev, size, dir); +} + /** * iommu_dma_init_options - Initialize dma-iommu options * @options: The options to be initialized @@ -1103,7 +1109,7 @@ void iommu_dma_sync_single_for_cpu(struct device *dev, dma_addr_t dma_handle, { phys_addr_t phys; - if (dev_is_dma_coherent(dev) && !dev_use_swiotlb(dev, size, dir)) + if (dev_is_coherent_no_swiotlb(dev, size, dir)) return; phys = iommu_iova_to_phys(iommu_get_dma_domain(dev), dma_handle); @@ -1120,7 +1126,7 @@ void iommu_dma_sync_single_for_device(struct device *dev, dma_addr_t dma_handle, { phys_addr_t phys; - if (dev_is_dma_coherent(dev) && !dev_use_swiotlb(dev, size, dir)) + if (dev_is_coherent_no_swiotlb(dev, size, dir)) return; phys = iommu_iova_to_phys(iommu_get_dma_domain(dev), dma_handle); -- 2.53.0-Meta ^ permalink raw reply related [flat|nested] 6+ messages in thread
* Re: [PATCH 1/2] iommu/dma: Add helper for coherent devs without swiotlb 2026-11-06 17:57 ` [PATCH 1/2] iommu/dma: Add helper for coherent devs without swiotlb Joe Damato @ 2026-12-01 20:30 ` Robin Murphy 2026-12-02 15:18 ` Joe Damato 0 siblings, 1 reply; 6+ messages in thread From: Robin Murphy @ 2026-12-01 20:30 UTC (permalink / raw) To: Joe Damato, Joerg Roedel (AMD), Will Deacon; +Cc: iommu, linux-kernel On 2026-11-06 5:57 pm, Joe Damato wrote: > Factor out some repeated logic which checks if a device is coherent and > if there is a swiotlb. > > Replace repeated instances of this logic with a call to the helper > instead. dev_use_swiotlb() itself already contains a frankly confusing number of levels of indirection. I guess I can buy the argument for patch #2, but please just make it the trivial 2-liner that doesn't obfuscate the conditions (and thus correlates even more clearly to those same conditions in the map path) i.e.: @@ -1280,7 +1280,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; } Thanks, Robin. > > Signed-off-by: Joe Damato <joe@dama.to> > --- > drivers/iommu/dma-iommu.c | 10 ++++++++-- > 1 file changed, 8 insertions(+), 2 deletions(-) > > diff --git a/drivers/iommu/dma-iommu.c b/drivers/iommu/dma-iommu.c > index 78742aeba932..a25ee287a5ac 100644 > --- a/drivers/iommu/dma-iommu.c > +++ b/drivers/iommu/dma-iommu.c > @@ -628,6 +628,12 @@ static bool dev_use_sg_swiotlb(struct device *dev, struct scatterlist *sg, > return false; > } > > +static bool dev_is_coherent_no_swiotlb(struct device *dev, size_t size, > + enum dma_data_direction dir) > +{ > + return dev_is_dma_coherent(dev) && !dev_use_swiotlb(dev, size, dir); > +} > + > /** > * iommu_dma_init_options - Initialize dma-iommu options > * @options: The options to be initialized > @@ -1103,7 +1109,7 @@ void iommu_dma_sync_single_for_cpu(struct device *dev, dma_addr_t dma_handle, > { > phys_addr_t phys; > > - if (dev_is_dma_coherent(dev) && !dev_use_swiotlb(dev, size, dir)) > + if (dev_is_coherent_no_swiotlb(dev, size, dir)) > return; > > phys = iommu_iova_to_phys(iommu_get_dma_domain(dev), dma_handle); > @@ -1120,7 +1126,7 @@ void iommu_dma_sync_single_for_device(struct device *dev, dma_addr_t dma_handle, > { > phys_addr_t phys; > > - if (dev_is_dma_coherent(dev) && !dev_use_swiotlb(dev, size, dir)) > + if (dev_is_coherent_no_swiotlb(dev, size, dir)) > return; > > phys = iommu_iova_to_phys(iommu_get_dma_domain(dev), dma_handle); ^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH 1/2] iommu/dma: Add helper for coherent devs without swiotlb 2026-12-01 20:30 ` Robin Murphy @ 2026-12-02 15:18 ` Joe Damato 0 siblings, 0 replies; 6+ messages in thread From: Joe Damato @ 2026-12-02 15:18 UTC (permalink / raw) To: Robin Murphy; +Cc: Joerg Roedel (AMD), Will Deacon, iommu, linux-kernel On Tue, Dec 01, 2026 at 08:30:38PM +0000, Robin Murphy wrote: > On 2026-11-06 5:57 pm, Joe Damato wrote: > > Factor out some repeated logic which checks if a device is coherent and > > if there is a swiotlb. > > > > Replace repeated instances of this logic with a call to the helper > > instead. > > dev_use_swiotlb() itself already contains a frankly confusing number of > levels of indirection. I guess I can buy the argument for patch #2, but > please just make it the trivial 2-liner that doesn't obfuscate the > conditions (and thus correlates even more clearly to those same > conditions in the map path) i.e.: > > @@ -1280,7 +1280,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; > } OK, will do. I'll respin this as a single patch. Thanks a lot for the review, I appreciate your time! ^ permalink raw reply [flat|nested] 6+ messages in thread
* [PATCH 2/2] iommu/dma: Skip iova_to_phys() on unmap when unneeded 2026-11-06 17:57 [PATCH 0/2] iommu/dma: Skip iova_to_phys() on unmap for coherent devices Joe Damato 2026-11-06 17:57 ` [PATCH 1/2] iommu/dma: Add helper for coherent devs without swiotlb Joe Damato @ 2026-11-06 17:57 ` Joe Damato 2026-12-01 17:48 ` [PATCH 0/2] iommu/dma: Skip iova_to_phys() on unmap for coherent devices Joe Damato 2 siblings, 0 replies; 6+ messages in thread From: Joe Damato @ 2026-11-06 17:57 UTC (permalink / raw) To: Robin Murphy, Joerg Roedel (AMD), Will Deacon Cc: Joe Damato, iommu, linux-kernel 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(). Signed-off-by: Joe Damato <joe@dama.to> --- drivers/iommu/dma-iommu.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/drivers/iommu/dma-iommu.c b/drivers/iommu/dma-iommu.c index a25ee287a5ac..c4a5fac4f7cb 100644 --- a/drivers/iommu/dma-iommu.c +++ b/drivers/iommu/dma-iommu.c @@ -1260,9 +1260,11 @@ dma_addr_t iommu_dma_map_phys(struct device *dev, phys_addr_t phys, size_t size, void iommu_dma_unmap_phys(struct device *dev, dma_addr_t dma_handle, size_t size, enum dma_data_direction dir, unsigned long attrs) { + unsigned long skip_phys; phys_addr_t phys; - if (attrs & (DMA_ATTR_MMIO | DMA_ATTR_REQUIRE_COHERENT)) { + skip_phys = attrs & (DMA_ATTR_MMIO | DMA_ATTR_REQUIRE_COHERENT); + if (skip_phys || dev_is_coherent_no_swiotlb(dev, size, dir)) { __iommu_dma_unmap(dev, dma_handle, size); return; } -- 2.53.0-Meta ^ permalink raw reply related [flat|nested] 6+ messages in thread
* Re: [PATCH 0/2] iommu/dma: Skip iova_to_phys() on unmap for coherent devices 2026-11-06 17:57 [PATCH 0/2] iommu/dma: Skip iova_to_phys() on unmap for coherent devices Joe Damato 2026-11-06 17:57 ` [PATCH 1/2] iommu/dma: Add helper for coherent devs without swiotlb Joe Damato 2026-11-06 17:57 ` [PATCH 2/2] iommu/dma: Skip iova_to_phys() on unmap when unneeded Joe Damato @ 2026-12-01 17:48 ` Joe Damato 2 siblings, 0 replies; 6+ messages in thread From: Joe Damato @ 2026-12-01 17:48 UTC (permalink / raw) To: iommu, Joerg Roedel (AMD), linux-kernel, Robin Murphy, Will Deacon On Fri, Nov 06, 2026 at 09:57:41AM -0800, Joe Damato wrote: > Greetings: > > iommu_dma_unmap_phys() calls iommu_iova_to_phys() on every unmap, which > walks the IOMMU page table. The physical address it returns is only used > for devices that are non-coherent or use a swiotlb. > > For a coherent device that does not use swiotlb, the lookup is not > needed. iommu_dma_sync_single_for_cpu() and > iommu_dma_sync_single_for_device() already skip it for these devices, as > does __iommu_dma_iova_unlink() for the dma_iova_link() API. > > Patch 1 factors the check out into a helper. Patch 2 uses the helper to > skip the lookup in iommu_dma_unmap_phys(). > > I split this into two patches because I thought it would make review and > bisect easier, but I can make them a single patch if needed. > > One behavior change: for these devices the WARN_ON(!phys) in > iommu_dma_unmap_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. > > I tested 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: 119-143 ns per TX packet (~0.5% of busy > CPU) before, 0 after > > AMD EPYC 9334 (Genoa), AMD IOMMU: > > - iova_to_phys() on unmap: ~10 ns per TX packet (~0.1% of busy CPU) > before, 0 after > > So, the performance improvement is small, but measureable. The proposed > change removes redundant work in the unmap, though, making it match the map > path. > > Thanks, > Joe > > Joe Damato (2): > iommu/dma: Add helper for coherent devs without swiotlb > iommu/dma: Skip iova_to_phys() on unmap when unneeded > > drivers/iommu/dma-iommu.c | 14 +++++++++++--- > 1 file changed, 11 insertions(+), 3 deletions(-) > > > base-commit: f66469d9905b39d2c260a15d7d6db450f4db67c1 Hi: Just wanted to ping on the above. Sorry, I think my git settings were a little off and I realized my cover letter had no To line. Not sure if I need to resend the series or not, but I haven't heard anything in a few weeks. Thanks, Joe ^ permalink raw reply [flat|nested] 6+ messages in thread
end of thread, other threads:[~2026-12-02 15:18 UTC | newest] Thread overview: 6+ messages (download: mbox.gz follow: Atom feed -- links below jump to the message on this page -- 2026-11-06 17:57 [PATCH 0/2] iommu/dma: Skip iova_to_phys() on unmap for coherent devices Joe Damato 2026-11-06 17:57 ` [PATCH 1/2] iommu/dma: Add helper for coherent devs without swiotlb Joe Damato 2026-12-01 20:30 ` Robin Murphy 2026-12-02 15:18 ` Joe Damato 2026-11-06 17:57 ` [PATCH 2/2] iommu/dma: Skip iova_to_phys() on unmap when unneeded Joe Damato 2026-12-01 17:48 ` [PATCH 0/2] iommu/dma: Skip iova_to_phys() on unmap for coherent devices Joe Damato
This is an external index of several public inboxes, see mirroring instructions on how to clone and mirror all data and code used by this external index.