Skip to contentSkip to conversation

iommu/dma-iommu.c: Convert to use vm_insert_range

PATCH Superseded Souptick Joarder 2 participants 4 messages Dec 6, 2018 – Dec 10, 2018

Subsystems (3)

v3 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

PATCH v3 6/9 iommu/dma-iommu.c: Convert to use vm_insert_range

Convert to use vm_insert_range() to map range of kernel memory to user vma.

---
 drivers/iommu/dma-iommu.c | 13 +++----------
 1 file changed, 3 insertions(+), 10 deletions(-)
drivers/iommu/dma-iommu.c +3 -10
index d1b0475..a2c65e2 100644
@@ -622,17 +622,10 @@ struct page **iommu_dma_alloc(struct device *dev, size_t size, gfp_t gfp,
 
 int iommu_dma_mmap(struct page **pages, size_t size, struct vm_area_struct *vma)
 {
-	unsigned long uaddr = vma->vm_start;
-	unsigned int i, count = PAGE_ALIGN(size) >> PAGE_SHIFT;
-	int ret = -ENXIO;
+	unsigned long count = PAGE_ALIGN(size) >> PAGE_SHIFT;
 
-	for (i = vma->vm_pgoff; i < count && uaddr < vma->vm_end; i++) {
-		ret = vm_insert_page(vma, uaddr, pages[i]);
-		if (ret)
-			break;
-		uaddr += PAGE_SIZE;
-	}
-	return ret;
+	return vm_insert_range(vma, vma->vm_start,
+				pages + vma->vm_pgoff, count);
 }
 
 static dma_addr_t __iommu_dma_map(struct device *dev, phys_addr_t phys,
Signature · 1 line
1.9.1
maintainer
#1 lore raw
Quoted 21 lines · Souptick Joarder

Convert to use vm_insert_range() to map range of kernel memory to user vma.

---
  drivers/iommu/dma-iommu.c | 13 +++----------
  1 file changed, 3 insertions(+), 10 deletions(-)
drivers/iommu/dma-iommu.c +1 -3
index d1b0475..a2c65e2 100644
@@ -622,17 +622,10 @@ struct page **iommu_dma_alloc(struct device *dev, size_t size, gfp_t gfp,
 
  int iommu_dma_mmap(struct page **pages, size_t size, struct vm_area_struct *vma)
  {
-	unsigned long uaddr = vma->vm_start;
-	unsigned int i, count = PAGE_ALIGN(size) >> PAGE_SHIFT;
-	int ret = -ENXIO;
+	unsigned long count = PAGE_ALIGN(size) >> PAGE_SHIFT;

drivers/iommu/dma-iommu.c struct page **iommu_dma_alloc(struct device *dev, size_t size, gfp_t gfp, · line 628

Quoted 10 lines
drivers/iommu/dma-iommu.c +2 -7
 
-	for (i = vma->vm_pgoff; i < count && uaddr < vma->vm_end; i++) {
-		ret = vm_insert_page(vma, uaddr, pages[i]);
-		if (ret)
-			break;
-		uaddr += PAGE_SIZE;
-	}
-	return ret;
+	return vm_insert_range(vma, vma->vm_start,
+				pages + vma->vm_pgoff, count);

You also need to adjust count to compensate for the pages skipped by vm_pgoff, otherwise you've got an out-of-bounds dereference triggered from userspace, which is pretty high up the "not good" scale (not to mention the entire call would then propagate -EFAULT back from vm_insert_page() and thus always appear to fail for nonzero offsets).

Robin.

Quoted 4 lines
drivers/iommu/dma-iommu.c +0 -0
  }
 
  static dma_addr_t __iommu_dma_map(struct device *dev, phys_addr_t phys,
#2 lore raw
Quoted 24 lines · Robin Murphy
Quoted 22 lines · Souptick Joarder

Convert to use vm_insert_range() to map range of kernel memory to user vma.

---
  drivers/iommu/dma-iommu.c | 13 +++----------
  1 file changed, 3 insertions(+), 10 deletions(-)
drivers/iommu/dma-iommu.c +1 -3
index d1b0475..a2c65e2 100644
@@ -622,17 +622,10 @@ struct page **iommu_dma_alloc(struct device *dev, size_t size, gfp_t gfp,
 
  int iommu_dma_mmap(struct page **pages, size_t size, struct vm_area_struct *vma)
  {
-     unsigned long uaddr = vma->vm_start;
-     unsigned int i, count = PAGE_ALIGN(size) >> PAGE_SHIFT;
-     int ret = -ENXIO;
+     unsigned long count = PAGE_ALIGN(size) >> PAGE_SHIFT;
 
Quoted 15 lines

drivers/iommu/dma-iommu.c struct page **iommu_dma_alloc(struct device *dev, size_t size, gfp_t gfp, · line 628

Quoted 9 lines
drivers/iommu/dma-iommu.c +2 -7
-     for (i = vma->vm_pgoff; i < count && uaddr < vma->vm_end; i++) {
-             ret = vm_insert_page(vma, uaddr, pages[i]);
-             if (ret)
-                     break;
-             uaddr += PAGE_SIZE;
-     }
-     return ret;
+     return vm_insert_range(vma, vma->vm_start,
+                             pages + vma->vm_pgoff, count);

You also need to adjust count to compensate for the pages skipped by vm_pgoff, otherwise you've got an out-of-bounds dereference triggered from userspace, which is pretty high up the "not good" scale (not to mention the entire call would then propagate -EFAULT back from vm_insert_page() and thus always appear to fail for nonzero offsets).

So this should something similar to ->

        return vm_insert_range(vma, vma->vm_start,
                                pages + vma->vm_pgoff, count - vma->vm_pgoff);
maintainer
#3 lore raw
Quoted 41 lines · Souptick Joarder
Quoted 24 lines · Robin Murphy
Quoted 22 lines · Souptick Joarder

Convert to use vm_insert_range() to map range of kernel memory to user vma.

---
   drivers/iommu/dma-iommu.c | 13 +++----------
   1 file changed, 3 insertions(+), 10 deletions(-)
drivers/iommu/dma-iommu.c +1 -3
index d1b0475..a2c65e2 100644
@@ -622,17 +622,10 @@ struct page **iommu_dma_alloc(struct device *dev, size_t size, gfp_t gfp,
 
   int iommu_dma_mmap(struct page **pages, size_t size, struct vm_area_struct *vma)
   {
-     unsigned long uaddr = vma->vm_start;
-     unsigned int i, count = PAGE_ALIGN(size) >> PAGE_SHIFT;
-     int ret = -ENXIO;
+     unsigned long count = PAGE_ALIGN(size) >> PAGE_SHIFT;
 
Quoted 15 lines

drivers/iommu/dma-iommu.c struct page **iommu_dma_alloc(struct device *dev, size_t size, gfp_t gfp, · line 628

Quoted 9 lines
drivers/iommu/dma-iommu.c +2 -7
-     for (i = vma->vm_pgoff; i < count && uaddr < vma->vm_end; i++) {
-             ret = vm_insert_page(vma, uaddr, pages[i]);
-             if (ret)
-                     break;
-             uaddr += PAGE_SIZE;
-     }
-     return ret;
+     return vm_insert_range(vma, vma->vm_start,
+                             pages + vma->vm_pgoff, count);

You also need to adjust count to compensate for the pages skipped by vm_pgoff, otherwise you've got an out-of-bounds dereference triggered from userspace, which is pretty high up the "not good" scale (not to mention the entire call would then propagate -EFAULT back from vm_insert_page() and thus always appear to fail for nonzero offsets).

Quoted 5 lines

So this should something similar to ->

         return vm_insert_range(vma, vma->vm_start,
                                 pages + vma->vm_pgoff, count - vma->vm_pgoff);

Yup, I think that looks appropriate.

Robin.