Skip to content

Commit a388477

Browse files
yunfeimmjoergroedel
authored andcommitted
iommu/dma: Fix iova map result check bug
The data type of the return value of the iommu_map_sg_atomic is ssize_t, but the data type of iova size is size_t, e.g. one is int while the other is unsigned int. When iommu_map_sg_atomic return value is compared with iova size, it will force the signed int to be converted to unsigned int, if iova map fails and iommu_map_sg_atomic return error code is less than 0, then (ret < iova_len) is false, which will to cause not do free iova, and the master can still successfully get the iova of map fail, which is not expected. Therefore, we need to check the return value of iommu_map_sg_atomic in two cases according to whether it is less than 0. Fixes: ad8f36e ("iommu: return full error code from iommu_map_sg[_atomic]()") Signed-off-by: Yunfei Wang <[email protected]> Cc: <[email protected]> # 5.15.* Reviewed-by: Robin Murphy <[email protected]> Reviewed-by: Miles Chen <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Joerg Roedel <[email protected]>
1 parent e8ae0e1 commit a388477

File tree

1 file changed

+4
-3
lines changed

1 file changed

+4
-3
lines changed

drivers/iommu/dma-iommu.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -776,6 +776,7 @@ static struct page **__iommu_dma_alloc_noncontiguous(struct device *dev,
776776
unsigned int count, min_size, alloc_sizes = domain->pgsize_bitmap;
777777
struct page **pages;
778778
dma_addr_t iova;
779+
ssize_t ret;
779780

780781
if (static_branch_unlikely(&iommu_deferred_attach_enabled) &&
781782
iommu_deferred_attach(dev, domain))
@@ -813,8 +814,8 @@ static struct page **__iommu_dma_alloc_noncontiguous(struct device *dev,
813814
arch_dma_prep_coherent(sg_page(sg), sg->length);
814815
}
815816

816-
if (iommu_map_sg_atomic(domain, iova, sgt->sgl, sgt->orig_nents, ioprot)
817-
< size)
817+
ret = iommu_map_sg_atomic(domain, iova, sgt->sgl, sgt->orig_nents, ioprot);
818+
if (ret < 0 || ret < size)
818819
goto out_free_sg;
819820

820821
sgt->sgl->dma_address = iova;
@@ -1214,7 +1215,7 @@ static int iommu_dma_map_sg(struct device *dev, struct scatterlist *sg,
12141215
* implementation - it knows better than we do.
12151216
*/
12161217
ret = iommu_map_sg_atomic(domain, iova, sg, nents, prot);
1217-
if (ret < iova_len)
1218+
if (ret < 0 || ret < iova_len)
12181219
goto out_free_iova;
12191220

12201221
return __finalise_sg(dev, sg, nents, iova);

0 commit comments

Comments
 (0)