Skip to contentSkip to conversation

mm/hmm: two bug fixes for hmm_range_fault()

PATCH Applied Ralph Campbell 4 participants 12 messages Aug 23, 2019 – Aug 27, 2019

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

cover mm/hmm: two bug fixes for hmm_range_fault()

I have been working on converting Jerome's hmm_dummy driver and self tests into a stand-alone set of tests to be included in tools/testing/selftests/vm and came across these two bug fixes in the process. The tests aren't quite ready to be posted as a patch. I'm posting the fixes now since I thought they shouldn't wait. They should probably have a fixes line but with all the HMM changes, I wasn't sure exactly which commit to use.

These are based on top of Jason's latest hmm branch.

Ralph Campbell (2):
mm/hmm: hmm_range_fault() NULL pointer bug
mm/hmm: hmm_range_fault() infinite loop

 mm/hmm.c | 16 ++++++++++++----
 1 file changed, 12 insertions(+), 4 deletions(-)
Signature · 6 lines
2.20.1

_______________________________________________
Nouveau mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/nouveau
#1 lore raw

PATCH 1/2 mm/hmm: hmm_range_fault() NULL pointer bug

Although hmm_range_fault() calls find_vma() to make sure that a vma exists before calling walk_page_range(), hmm_vma_walk_hole() can still be called with walk->vma == NULL if the start and end address are not contained within the vma range.

hmm_range_fault() /* calls find_vma() but no range check */
walk_page_range() /* calls find_vma(), sets walk->vma = NULL */
__walk_page_range()

    walk_pgd_range()
     walk_p4d_range()
      walk_pud_range()
       hmm_vma_walk_hole()
        hmm_vma_walk_hole_()
         hmm_vma_do_fault()
          handle_mm_fault(vma=0)
---
 mm/hmm.c | 13 +++++++++----
 1 file changed, 9 insertions(+), 4 deletions(-)
mm/hmm.c +9 -4
index fc05c8fe78b4..29371485fe94 100644
@@ -229,6 +229,9 @@ static int hmm_vma_do_fault(struct mm_walk *walk, unsigned long addr,
 	struct vm_area_struct *vma = walk->vma;
 	vm_fault_t ret;
 
+	if (!vma)
+		goto err;
+
 	if (hmm_vma_walk->flags & HMM_FAULT_ALLOW_RETRY)
 		flags |= FAULT_FLAG_ALLOW_RETRY;
 	if (write_fault)
@@ -239,12 +242,14 @@ static int hmm_vma_do_fault(struct mm_walk *walk, unsigned long addr,
 		/* Note, handle_mm_fault did up_read(&mm->mmap_sem)) */
 		return -EAGAIN;
 	}
-	if (ret & VM_FAULT_ERROR) {
-		*pfn = range->values[HMM_PFN_ERROR];
-		return -EFAULT;
-	}
+	if (ret & VM_FAULT_ERROR)
+		goto err;
 
 	return -EBUSY;
+
+err:
+	*pfn = range->values[HMM_PFN_ERROR];
+	return -EFAULT;
 }
 
 static int hmm_pfns_bad(unsigned long addr,
Signature · 1 line
2.20.1
Reviewed-by on v1 1/2
#2 lore raw

Re: [PATCH 1/2] mm/hmm: hmm_range_fault() NULL pointer bug

Quoted 4 lines · Ralph Campbell

Although hmm_range_fault() calls find_vma() to make sure that a vma exists before calling walk_page_range(), hmm_vma_walk_hole() can still be called with walk->vma == NULL if the start and end address are not contained within the vma range.

Should we convert to walk_vma_range instead? Or keep walk_page_range but drop searching the vma ourselves?

Except for that the patch looks good to me:

_______________________________________________
Nouveau mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/nouveau

#3 lore raw

Re: [PATCH 1/2] mm/hmm: hmm_range_fault() NULL pointer bug

Quoted 6 lines · Christoph Hellwig
Quoted 4 lines · Ralph Campbell

Although hmm_range_fault() calls find_vma() to make sure that a vma exists before calling walk_page_range(), hmm_vma_walk_hole() can still be called with walk->vma == NULL if the start and end address are not contained within the vma range.

Quoted 7 lines

Should we convert to walk_vma_range instead? Or keep walk_page_range but drop searching the vma ourselves?

Except for that the patch looks good to me:

I think keeping the call to walk_page_range() makes sense. Jason is hoping to be able to snapshot a range with & without vmas and have the pfns[] filled with empty/valid entries as appropriate.

I plan to repost my patch changing hmm_range_fault() to use walk.test_walk which will remove the call to find_vma(). Jason had some concerns about testing it so that's why I have been working on some HMM self tests before resending it.
_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx

#4 lore raw

Re: [PATCH 1/2] mm/hmm: hmm_range_fault() NULL pointer bug

Quoted 16 lines · Ralph Campbell
Quoted 6 lines · Christoph Hellwig
Quoted 4 lines · Ralph Campbell

Although hmm_range_fault() calls find_vma() to make sure that a vma exists before calling walk_page_range(), hmm_vma_walk_hole() can still be called with walk->vma == NULL if the start and end address are not contained within the vma range.

Quoted 7 lines

Should we convert to walk_vma_range instead? Or keep walk_page_range but drop searching the vma ourselves?

Except for that the patch looks good to me:

Quoted 8 lines

I think keeping the call to walk_page_range() makes sense. Jason is hoping to be able to snapshot a range with & without vmas and have the pfns[] filled with empty/valid entries as appropriate.

I plan to repost my patch changing hmm_range_fault() to use walk.test_walk which will remove the call to find_vma(). Jason had some concerns about testing it so that's why I have been working on some HMM self tests before resending it.

I'm really excited to see tests for hmm_range_fault()!

Did you find this bug with the tests??

Jason

#5 lore raw

Re: [PATCH 1/2] mm/hmm: hmm_range_fault() NULL pointer bug

Quoted 26 lines · Jason Gunthorpe
Quoted 16 lines · Ralph Campbell
Quoted 6 lines · Christoph Hellwig
Quoted 4 lines · Ralph Campbell

Although hmm_range_fault() calls find_vma() to make sure that a vma exists before calling walk_page_range(), hmm_vma_walk_hole() can still be called with walk->vma == NULL if the start and end address are not contained within the vma range.

Quoted 7 lines

Should we convert to walk_vma_range instead? Or keep walk_page_range but drop searching the vma ourselves?

Except for that the patch looks good to me:

Quoted 8 lines

I think keeping the call to walk_page_range() makes sense. Jason is hoping to be able to snapshot a range with & without vmas and have the pfns[] filled with empty/valid entries as appropriate.

I plan to repost my patch changing hmm_range_fault() to use walk.test_walk which will remove the call to find_vma(). Jason had some concerns about testing it so that's why I have been working on some HMM self tests before resending it.

Quoted 6 lines

I'm really excited to see tests for hmm_range_fault()!

Did you find this bug with the tests??

Jason

Yes, I found both bugs with the tests.
I started with Jerome's hmm_dummy driver and user level test code. Hopefully I can send it out this week.

#6 lore raw

PATCH 2/2 mm/hmm: hmm_range_fault() infinite loop

In reply to Ralph Campbell

Normally, callers to handle_mm_fault() are supposed to check the vma->vm_flags first. hmm_range_fault() checks for VM_READ but doesn't check for VM_WRITE if the caller requests a page to be faulted in with write permission (via the hmm_range.pfns[] value).
If the vma is write protected, this can result in an infinite loop:
hmm_range_fault()

    walk_page_range()
      ...
      hmm_vma_walk_hole()
        hmm_vma_walk_hole_()
          hmm_vma_do_fault()
            handle_mm_fault(FAULT_FLAG_WRITE)
            /* returns VM_FAULT_WRITE */
          /* returns -EBUSY */
        /* returns -EBUSY */
      /* returns -EBUSY */
    /* loops on -EBUSY and range->valid */

Prevent this by checking for vma->vm_flags & VM_WRITE before calling handle_mm_fault().

---
 mm/hmm.c | 3 +++
 1 file changed, 3 insertions(+)
mm/hmm.c +3 -0
index 29371485fe94..4882b83aeccb 100644
@@ -292,6 +292,9 @@ static int hmm_vma_walk_hole_(unsigned long addr, unsigned long end,
 	hmm_vma_walk->last = addr;
 	i = (addr - range->start) >> PAGE_SHIFT;
 
+	if (write_fault && walk->vma && !(walk->vma->vm_flags & VM_WRITE))
+		return -EPERM;
+
 	for (; addr < end; addr += PAGE_SIZE, i++) {
 		pfns[i] = range->values[HMM_PFN_NONE];
 		if (fault || write_fault) {
Signature · 6 lines
2.20.1

_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx
Reviewed-by on v1 2/2
#7 lore raw

Re: [PATCH 2/2] mm/hmm: hmm_range_fault() infinite loop

Quoted 17 lines · Ralph Campbell

Normally, callers to handle_mm_fault() are supposed to check the vma->vm_flags first. hmm_range_fault() checks for VM_READ but doesn't check for VM_WRITE if the caller requests a page to be faulted in with write permission (via the hmm_range.pfns[] value).
If the vma is write protected, this can result in an infinite loop:
hmm_range_fault()

    walk_page_range()
      ...
      hmm_vma_walk_hole()
        hmm_vma_walk_hole_()
          hmm_vma_do_fault()
            handle_mm_fault(FAULT_FLAG_WRITE)
            /* returns VM_FAULT_WRITE */
          /* returns -EBUSY */
        /* returns -EBUSY */
      /* returns -EBUSY */
    /* loops on -EBUSY and range->valid */
Quoted 4 lines

Prevent this by checking for vma->vm_flags & VM_WRITE before calling handle_mm_fault().

Looks good,

#8 lore raw

Re: [PATCH 2/2] mm/hmm: hmm_range_fault() infinite loop

In reply to Ralph Campbell

mm/hmm.c static int hmm_vma_walk_hole_(unsigned long addr, unsigned long end, · line 296

Quoted 13 lines · Ralph Campbell
 mm/hmm.c | 3 +++
 1 file changed, 3 insertions(+)
mm/hmm.c +3 -0
index 29371485fe94..4882b83aeccb 100644
+++ b/mm/hmm.c
@@ -292,6 +292,9 @@ static int hmm_vma_walk_hole_(unsigned long addr, unsigned long end,
 	hmm_vma_walk->last = addr;
 	i = (addr - range->start) >> PAGE_SHIFT;
 
+	if (write_fault && walk->vma && !(walk->vma->vm_flags & VM_WRITE))
+		return -EPERM;

Can walk->vma be NULL here? hmm_vma_do_fault() touches it unconditionally.

Jason

#9 lore raw

Re: [PATCH 2/2] mm/hmm: hmm_range_fault() infinite loop

Quoted 13 lines · Jason Gunthorpe
Quoted 11 lines · Ralph Campbell
  mm/hmm.c | 3 +++
  1 file changed, 3 insertions(+)
mm/hmm.c +1 -0
index 29371485fe94..4882b83aeccb 100644
+++ b/mm/hmm.c
@@ -292,6 +292,9 @@ static int hmm_vma_walk_hole_(unsigned long addr, unsigned long end,
  	hmm_vma_walk->last = addr;
  	i = (addr - range->start) >> PAGE_SHIFT;
 
Quoted 8 lines

mm/hmm.c static int hmm_vma_walk_hole_(unsigned long addr, unsigned long end, · line 296

Quoted 2 lines
mm/hmm.c +2 -0
+	if (write_fault && walk->vma && !(walk->vma->vm_flags & VM_WRITE))
+		return -EPERM;

Can walk->vma be NULL here? hmm_vma_do_fault() touches it unconditionally.

Jason

walk->vma can be NULL. hmm_vma_do_fault() no longer touches it unconditionally, that is what the preceding patch fixes. I suppose I could change hmm_vma_walk_hole_() to check for NULL and fill in the pfns[] array, I just chose to handle it in hmm_vma_do_fault().
_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx

Reviewed-by
#10 lore raw

Re: [PATCH 2/2] mm/hmm: hmm_range_fault() infinite loop

Quoted 20 lines · Ralph Campbell
Quoted 18 lines · Jason Gunthorpe

mm/hmm.c static int hmm_vma_walk_hole_(unsigned long addr, unsigned long end, · line 295

Quoted 12 lines · Ralph Campbell
  mm/hmm.c | 3 +++
  1 file changed, 3 insertions(+)
mm/hmm.c +3 -0
index 29371485fe94..4882b83aeccb 100644
+++ b/mm/hmm.c
@@ -292,6 +292,9 @@ static int hmm_vma_walk_hole_(unsigned long addr, unsigned long end,
  	hmm_vma_walk->last = addr;
  	i = (addr - range->start) >> PAGE_SHIFT;
+	if (write_fault && walk->vma && !(walk->vma->vm_flags & VM_WRITE))
+		return -EPERM;

Can walk->vma be NULL here? hmm_vma_do_fault() touches it unconditionally.

Quoted 7 lines
Quoted 2 lines

Jason

walk->vma can be NULL. hmm_vma_do_fault() no longer touches it unconditionally, that is what the preceding patch fixes. I suppose I could change hmm_vma_walk_hole_() to check for NULL and fill in the pfns[] array, I just chose to handle it in hmm_vma_do_fault().

Okay, yes maybe long term it would be clearer to do the vma null check closer to the start of the callback, but this is a good enough bug fix

Jason

#11 lore raw
In reply to Ralph Campbell
Quoted 8 lines · Ralph Campbell

I have been working on converting Jerome's hmm_dummy driver and self tests into a stand-alone set of tests to be included in tools/testing/selftests/vm and came across these two bug fixes in the process. The tests aren't quite ready to be posted as a patch. I'm posting the fixes now since I thought they shouldn't wait. They should probably have a fixes line but with all the HMM changes, I wasn't sure exactly which commit to use.

Quoted 5 lines

These are based on top of Jason's latest hmm branch.

Ralph Campbell (2):
mm/hmm: hmm_range_fault() NULL pointer bug
mm/hmm: hmm_range_fault() infinite loop

Applied to hmm.git

Thanks,
Jason