Commit 38ec010d authored by Joerg Roedel's avatar Joerg Roedel

iommu: Do more input validation in iommu_map_sg()

The IOMMU-API works on page boundarys, unlike the DMA-API
which can work with sub-page buffers. The sg->offset
field does not make sense on the IOMMU level, so force it to
be 0. Do some error-path consolidation while at it.
Signed-off-by: default avatarJoerg Roedel <jroedel@suse.de>
parent 315786eb
...@@ -1127,26 +1127,33 @@ EXPORT_SYMBOL_GPL(iommu_unmap); ...@@ -1127,26 +1127,33 @@ EXPORT_SYMBOL_GPL(iommu_unmap);
size_t default_iommu_map_sg(struct iommu_domain *domain, unsigned long iova, size_t default_iommu_map_sg(struct iommu_domain *domain, unsigned long iova,
struct scatterlist *sg, unsigned int nents, int prot) struct scatterlist *sg, unsigned int nents, int prot)
{ {
int ret; struct scatterlist *s;
size_t mapped = 0; size_t mapped = 0;
unsigned int i; unsigned int i;
struct scatterlist *s; int ret;
for_each_sg(sg, s, nents, i) { for_each_sg(sg, s, nents, i) {
phys_addr_t phys = page_to_phys(sg_page(s)); phys_addr_t phys = page_to_phys(sg_page(s));
size_t page_len = s->offset + s->length;
ret = iommu_map(domain, iova + mapped, phys, page_len, prot); /* We are mapping on page boundarys, so offset must be 0 */
if (ret) { if (s->offset)
/* undo mappings already done */ goto out_err;
iommu_unmap(domain, iova, mapped);
mapped = 0; ret = iommu_map(domain, iova + mapped, phys, s->length, prot);
break; if (ret)
} goto out_err;
mapped += page_len;
mapped += s->length;
} }
return mapped; return mapped;
out_err:
/* undo mappings already done */
iommu_unmap(domain, iova, mapped);
return 0;
} }
EXPORT_SYMBOL_GPL(default_iommu_map_sg); EXPORT_SYMBOL_GPL(default_iommu_map_sg);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment