* [PATCH v2] iommu/dma: Skip iova_to_phys() on unmap when unneeded
@ 2026-12-02 15:33 Joe Damato
2026-12-02 16:36 ` Robin Murphy
0 siblings, 1 reply; 4+ messages in thread
From: Joe Damato @ 2026-12-02 15:33 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().
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@dama.to>
---
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@dama.to/
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
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH v2] iommu/dma: Skip iova_to_phys() on unmap when unneeded
2026-12-02 15:33 [PATCH v2] iommu/dma: Skip iova_to_phys() on unmap when unneeded Joe Damato
@ 2026-12-02 16:36 ` Robin Murphy
2026-12-02 17:50 ` Joe Damato
0 siblings, 1 reply; 4+ messages in thread
From: Robin Murphy @ 2026-12-02 16:36 UTC (permalink / raw)
To: Joe Damato, Joerg Roedel (AMD), Will Deacon; +Cc: iommu, linux-kernel
On 02/12/2026 3:33 pm, Joe Damato wrote:
> 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.
Thanks Joe!
Acked-by: Robin Murphy <robin.murphy@arm.com>
Although now I've made the mistake of remembering we have the
higher-level dma_dev_need_sync() thing as well, and starting to wonder
how much that now might effectively cover these conditions both here and
for iommu_dma_sync_single*()...
> Signed-off-by: Joe Damato <joe@dama.to>
> ---
> 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@dama.to/
>
> 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
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH v2] iommu/dma: Skip iova_to_phys() on unmap when unneeded
2026-12-02 16:36 ` Robin Murphy
@ 2026-12-02 17:50 ` Joe Damato
2026-12-03 16:38 ` Robin Murphy
0 siblings, 1 reply; 4+ messages in thread
From: Joe Damato @ 2026-12-02 17:50 UTC (permalink / raw)
To: Robin Murphy; +Cc: Joerg Roedel (AMD), Will Deacon, iommu, linux-kernel
On Wed, Dec 02, 2026 at 04:36:27PM +0000, Robin Murphy wrote:
> On 02/12/2026 3:33 pm, Joe Damato wrote:
> > 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.
>
> Thanks Joe!
>
> Acked-by: Robin Murphy <robin.murphy@arm.com>
>
> Although now I've made the mistake of remembering we have the higher-level
> dma_dev_need_sync() thing as well, and starting to wonder how much that now
> might effectively cover these conditions both here and for
> iommu_dma_sync_single*()...
I see what you mean. I did not know about that before, my bad.
Your call: I would be happy to send a followup cleaning up all of those sites
to use dma_dev_need_sync once the patch above is applied
or
If you prefer, I can send a v3 as a single patch that cleans up the existing
sites and inserts the call in unmap.
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH v2] iommu/dma: Skip iova_to_phys() on unmap when unneeded
2026-12-02 17:50 ` Joe Damato
@ 2026-12-03 16:38 ` Robin Murphy
0 siblings, 0 replies; 4+ messages in thread
From: Robin Murphy @ 2026-12-03 16:38 UTC (permalink / raw)
To: Joe Damato; +Cc: Joerg Roedel (AMD), Will Deacon, iommu, linux-kernel
On 2026-12-02 5:50 pm, Joe Damato wrote:
> On Wed, Dec 02, 2026 at 04:36:27PM +0000, Robin Murphy wrote:
>> On 02/12/2026 3:33 pm, Joe Damato wrote:
>>> 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.
>>
>> Thanks Joe!
>>
>> Acked-by: Robin Murphy <robin.murphy@arm.com>
>>
>> Although now I've made the mistake of remembering we have the higher-level
>> dma_dev_need_sync() thing as well, and starting to wonder how much that now
>> might effectively cover these conditions both here and for
>> iommu_dma_sync_single*()...
>
> I see what you mean. I did not know about that before, my bad.
>
> Your call: I would be happy to send a followup cleaning up all of those sites
> to use dma_dev_need_sync once the patch above is applied
Yeah, I think it makes sense to land this as-is as a targeted functional
improvement for unmaps following the internal sync logic, but if you do
fancy reasoning through whether the overlap with the external sync flag
really is 100% for all possible configurations, partial syncs, etc. then
that can be done as a more general cleanup of the whole lot on top.
(And otherwise, I'd probably end up taking a look at some point now
anyway, as it seems sufficiently adjacent to another DMA flag thing I
have in mind...)
Cheers,
Robin.
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2026-12-03 16:38 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-12-02 15:33 [PATCH v2] iommu/dma: Skip iova_to_phys() on unmap when unneeded Joe Damato
2026-12-02 16:36 ` Robin Murphy
2026-12-02 17:50 ` Joe Damato
2026-12-03 16:38 ` Robin Murphy
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®