iommu/dma: Do not try to iommu_map a 0 length region in swiotlb
Subsystems (3)
PATCH 1/1 iommu/dma: Do not try to iommu_map a 0 length region in swiotlb
iommu_dma_iova_link_swiotlb() processes a mapping that is unaligned in three parts, the head, middle and trailer. If the middle is empty because there are no aligned pages it will call down to iommu_map() with a 0 size which the iommupt implementation will fail as illegal.
It then tries to do an error unwind and starts from the wrong spot corrupting the mapping so the eventual destruction triggers a WARN_ON.
Check for 0 length and avoid mapping and use offset not 0 as the starting point to unlink.
This is frequently triggered by using some kinds of thunderbolt NVMe drives that trigger forced SWIOTLB for unaligned memory. NVMe seems to pass in oddly aligned buffers for the passthrough commands from smartctl that hit this condition.
--- drivers/iommu/dma-iommu.c | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-)
This was discovered because iommupt errors on mapping length=0 instead of making it a NOP, so it is an became an issue since commit d6c65b0fd621 ("iommupt: Avoid rewalking during map") making it a regression this merge window.
drivers/iommu/dma-iommu.c +13 -6
@@ -1918,12 +1918,18 @@ static int iommu_dma_iova_link_swiotlb(struct device *dev,return 0 ; } +/* +* After removing the partial head and tail, there may be no aligned +* middle left to map. The tail still gets bounced below. +*/ size -= iova_end_pad; - error =__dma_iova_link (dev, addr + mapped, phys + mapped, size, dir, - attrs); -if (error) - goto out_unmap; - mapped += size; +if (size) { + error =__dma_iova_link (dev, addr + mapped, phys + mapped, + size, dir, attrs); +if (error) + goto out_unmap; + mapped += size; + }if (iova_end_pad) { error = iommu_dma_iova_bounce_and_link(dev, addr + mapped,
@@ -1936,7 +1942,8 @@ static int iommu_dma_iova_link_swiotlb(struct device *dev,return 0 ; out_unmap: -dma_iova_unlink (dev, state,0 , mapped, dir, attrs); +if (mapped) +dma_iova_unlink (dev, state, offset, mapped, dir, attrs);return error; }
base-commit: 4549871118cf616eecdd2d939f78e3b9e1dddc48
Signature · 1 line
2.43.0
Looks good:
Quoted 20 lines · Jason Gunthorpe
iommu_dma_iova_link_swiotlb()processes a mapping that is unaligned in three parts, the head, middle and trailer. If the middle is empty because there are no aligned pages it will call down toiommu_map()with a 0 size which the iommupt implementation will fail as illegal.It then tries to do an error unwind and starts from the wrong spot corrupting the mapping so the eventual destruction triggers a
WARN_ON.Check for 0 length and avoid mapping and use offset not 0 as the starting point to unlink.
This is frequently triggered by using some kinds of thunderbolt NVMe drives that trigger forced SWIOTLB for unaligned memory. NVMe seems to pass in oddly aligned buffers for the passthrough commands from smartctl that hit this condition.
Fixes 433a76207dcf ("dma-mapping: Implement link/unlink ranges API")Reported-by Mark Lord <[email protected]>Signed-off-by Jason Gunthorpe <[email protected]>
Quoted 9 lines
--- drivers/iommu/dma-iommu.c | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-)This was discovered because iommupt errors on mapping length=0 instead of making it a NOP, so it is an became an issue since commit
d6c65b0fd621("iommupt: Avoid rewalking during map") making it a regression this merge window.
Thanks,
Quoted 68 lines · Jason Gunthorpe
iommu_dma_iova_link_swiotlb()processes a mapping that is unaligned in three parts, the head, middle and trailer. If the middle is empty because there are no aligned pages it will call down toiommu_map()with a 0 size which the iommupt implementation will fail as illegal.It then tries to do an error unwind and starts from the wrong spot corrupting the mapping so the eventual destruction triggers a
WARN_ON.Check for 0 length and avoid mapping and use offset not 0 as the starting point to unlink.
This is frequently triggered by using some kinds of thunderbolt NVMe drives that trigger forced SWIOTLB for unaligned memory. NVMe seems to pass in oddly aligned buffers for the passthrough commands from smartctl that hit this condition.
Fixes 433a76207dcf ("dma-mapping: Implement link/unlink ranges API")Reported-by Mark Lord <[email protected]>Signed-off-by Jason Gunthorpe <[email protected]>---
drivers/iommu/dma-iommu.c| 19 +++++++++++++------1 file changed, 13 insertions(+), 6 deletions(-)This was discovered because iommupt errors on mapping length=0 instead of making it a NOP, so it is an became an issue since commit
d6c65b0fd621("iommupt: Avoid rewalking during map") making it a regression this merge window.drivers/iommu/dma-iommu.c +11
-5@@ -1918,12 +1918,18 @@ static int iommu_dma_iova_link_swiotlb(struct device *dev,return 0 ; } +/* +* After removing the partial head and tail, there may be no aligned +* middle left to map. The tail still gets bounced below. +*/ size -= iova_end_pad; - error =__dma_iova_link (dev, addr + mapped, phys + mapped, size, dir, - attrs); -if (error) - goto out_unmap; - mapped += size; +if (size) { + error =__dma_iova_link (dev, addr + mapped, phys + mapped, + size, dir, attrs); +if (error) + goto out_unmap; + mapped += size; + }if (iova_end_pad) { error = iommu_dma_iova_bounce_and_link(dev, addr + mapped,@@ -1936,7 +1942,8 @@ static int iommu_dma_iova_link_swiotlb(struct device *dev,return 0 ;
out_unmap:drivers/iommu/dma-iommu.c +2
-1-dma_iova_unlink (dev, state,0 , mapped, dir, attrs); +if (mapped) +dma_iova_unlink (dev, state, offset, mapped, dir, attrs);return error; }
Quoted 5 lines
base-commit:
4549871118cf616eecdd2d939f78e3b9e1dddc48Signature · 1 line
2.43.0
Marek Szyprowski
AppliedQuoted 16 lines · Jason Gunthorpe
iommu_dma_iova_link_swiotlb()processes a mapping that is unaligned in three parts, the head, middle and trailer. If the middle is empty because there are no aligned pages it will call down toiommu_map()with a 0 size which the iommupt implementation will fail as illegal.It then tries to do an error unwind and starts from the wrong spot corrupting the mapping so the eventual destruction triggers a
WARN_ON.Check for 0 length and avoid mapping and use offset not 0 as the starting point to unlink.
This is frequently triggered by using some kinds of thunderbolt NVMe drives that trigger forced SWIOTLB for unaligned memory. NVMe seems to pass in oddly aligned buffers for the passthrough commands from smartctl that hit this condition.
Quoted 4 lines
Fixes 433a76207dcf ("dma-mapping: Implement link/unlink ranges API")Reported-by Mark Lord <[email protected]>Signed-off-by Jason Gunthorpe <[email protected]>
Applied to dma-mapping-fixes, thanks!
Quoted 48 lines
--- drivers/iommu/dma-iommu.c | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-)This was discovered because iommupt errors on mapping length=0 instead of making it a NOP, so it is an became an issue since commit
d6c65b0fd621("iommupt: Avoid rewalking during map") making it a regression this merge window.drivers/iommu/dma-iommu.c +13
-6@@ -1918,12 +1918,18 @@ static int iommu_dma_iova_link_swiotlb(struct device *dev,return 0 ; } +/* +* After removing the partial head and tail, there may be no aligned +* middle left to map. The tail still gets bounced below. +*/ size -= iova_end_pad; - error =__dma_iova_link (dev, addr + mapped, phys + mapped, size, dir, - attrs); -if (error) - goto out_unmap; - mapped += size; +if (size) { + error =__dma_iova_link (dev, addr + mapped, phys + mapped, + size, dir, attrs); +if (error) + goto out_unmap; + mapped += size; + }if (iova_end_pad) { error = iommu_dma_iova_bounce_and_link(dev, addr + mapped,@@ -1936,7 +1942,8 @@ static int iommu_dma_iova_link_swiotlb(struct device *dev,return 0 ; out_unmap: -dma_iova_unlink (dev, state,0 , mapped, dir, attrs); +if (mapped) +dma_iova_unlink (dev, state, offset, mapped, dir, attrs);return error; }
Quoted 1 line
base-commit:
4549871118cf616eecdd2d939f78e3b9e1dddc48
Best regards
Signature · 2 lines
Marek Szyprowski, PhD Samsung R&D Institute Poland