iommu/amd: Unmap all mapped pages in error path of map_sg

This change “iommu/amd: Unmap all mapped pages in error path of map_sg” in Linux kernel is authored by Jerry Snitselaar <jsnitsel [at]> on Sat Jan 19 10:38:05 2019 -0700.

iommu/amd: Unmap all mapped pages in error path of map_sg

In the error path of map_sg there is an incorrect if condition
for breaking out of the loop that searches the scatterlist
for mapped pages to unmap. Instead of breaking out of the
loop once all the pages that were mapped have been unmapped,
it will break out of the loop after it has unmapped 1 page.
Fix the condition, so it breaks out of the loop only after
all the mapped pages have been unmapped.

Fixes: 80187fd39dcb ("iommu/amd: Optimize map_sg and unmap_sg")
Cc: Joerg Roedel <>
Signed-off-by: Jerry Snitselaar <>
Signed-off-by: Joerg Roedel <>

This Linux change may have been applied to various maintained Linux releases and you can find Linux releases including commit f1724c0.

There are 2 lines of Linux source code added/deleted in this change. Code changes to Linux kernel are as follows.

 drivers/iommu/amd_iommu.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/iommu/amd_iommu.c b/drivers/iommu/amd_iommu.c
index 418df8f..843b825 100644
--- a/drivers/iommu/amd_iommu.c
+++ b/drivers/iommu/amd_iommu.c
@@ -2617,7 +2617,7 @@ static int map_sg(struct device *dev, struct scatterlist *sglist,
 			bus_addr  = address + s->dma_address + (j << PAGE_SHIFT);
 			iommu_unmap_page(domain, bus_addr, PAGE_SIZE);
-			if (--mapped_pages)
+			if (--mapped_pages == 0)
 				goto out_free_iova;

The commit for this change in Linux stable tree is f1724c0 (patch).

Leave a Reply

Your email address will not be published. Required fields are marked *