Skip to contentSkip to conversation

Cleanups and fixup for gup

PATCH Applied Miaohe Lin 5 participants 15 messages Aug 7, 2021 – Aug 9, 2021

Subsystems (2)
lore
Share & tools
lore

Miaohe Lin

author
#0 lore raw

cover Cleanups and fixup for gup

Hi all,
This series contains cleanups to remove unneeded variable, useless BUG_ON and use helper to improve readability. Also we fix a potential pgmap refcnt leak. More details can be found in the respective changelogs. Thanks!

Miaohe Lin (5):
mm: gup: remove set but unused local variable major
mm: gup: remove unneed local variable orig_refs
mm: gup: remove useless BUG_ON in __get_user_pages()
mm: gup: fix potential pgmap refcnt leak in __gup_device_huge()
mm: gup: use helper PAGE_ALIGNED in populate_vma_page_range()

 mm/gup.c | 16 ++++++----------
 1 file changed, 6 insertions(+), 10 deletions(-)
Signature · 1 line
2.23.0
author
#1 lore raw

PATCH 1/5 mm: gup: remove set but unused local variable major

Since commit a2beb5f1efed ("mm: clean up the last pieces of page fault accountings"), the local variable major is unused. Remove it.

---
 mm/gup.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
mm/gup.c +1 -2
index 77150624f77a..430495cb1b91 100644
@@ -1276,7 +1276,7 @@ int fixup_user_fault(struct mm_struct *mm,
 		     bool *unlocked)
 {
 	struct vm_area_struct *vma;
-	vm_fault_t ret, major = 0;
+	vm_fault_t ret;
 
 	address = untagged_addr(address);
 
@@ -1296,7 +1296,6 @@ int fixup_user_fault(struct mm_struct *mm,
 		return -EINTR;
 
 	ret = handle_mm_fault(vma, address, fault_flags, NULL);
-	major |= ret & VM_FAULT_MAJOR;
 	if (ret & VM_FAULT_ERROR) {
 		int err = vm_fault_to_errno(ret, 0);
 
Signature · 1 line
2.23.0
Reviewed-by on v1 1/5
#2 lore raw

Re: [PATCH 1/5] mm: gup: remove set but unused local variable major

Quoted 23 lines · Miaohe Lin

Since commit a2beb5f1efed ("mm: clean up the last pieces of page fault accountings"), the local variable major is unused. Remove it.

---
  mm/gup.c | 3 +--
  1 file changed, 1 insertion(+), 2 deletions(-)
mm/gup.c +1 -1
index 77150624f77a..430495cb1b91 100644
@@ -1276,7 +1276,7 @@ int fixup_user_fault(struct mm_struct *mm,
  		     bool *unlocked)
  {
  	struct vm_area_struct *vma;
-	vm_fault_t ret, major = 0;
+	vm_fault_t ret;
 
  	address = untagged_addr(address);
 

mm/gup.c int fixup_user_fault(struct mm_struct *mm, · line 1301

Quoted 7 lines
mm/gup.c +0 -1
@@ -1296,7 +1296,6 @@ int fixup_user_fault(struct mm_struct *mm,
  		return -EINTR;
 
  	ret = handle_mm_fault(vma, address, fault_flags, NULL);
-	major |= ret & VM_FAULT_MAJOR;
  	if (ret & VM_FAULT_ERROR) {
  		int err = vm_fault_to_errno(ret, 0);
 
Signature · 3 lines
Thanks,

David / dhildenb
author
#3 lore raw

PATCH 2/5 mm: gup: remove unneed local variable orig_refs

In reply to Miaohe Lin

Remove unneed local variable orig_refs since refs is unchanged now.

---
 mm/gup.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
mm/gup.c +1 -3
index 430495cb1b91..d6056dcab02c 100644
@@ -117,8 +117,6 @@ __maybe_unused struct page *try_grab_compound_head(struct page *page,
 	if (flags & FOLL_GET)
 		return try_get_compound_head(page, refs);
 	else if (flags & FOLL_PIN) {
-		int orig_refs = refs;
-
 		/*
 		 * Can't do FOLL_LONGTERM + FOLL_PIN gup fast path if not in a
 		 * right zone, so fail and let the caller fall back to the slow
@@ -150,7 +148,7 @@ __maybe_unused struct page *try_grab_compound_head(struct page *page,
 			page_ref_add(page, refs * (GUP_PIN_COUNTING_BIAS - 1));
 
 		mod_node_page_state(page_pgdat(page), NR_FOLL_PIN_ACQUIRED,
-				    orig_refs);
+				    refs);
 
 		return page;
 	}
Signature · 1 line
2.23.0
Reviewed-by on v1 2/5
#4 lore raw

Re: [PATCH 2/5] mm: gup: remove unneed local variable orig_refs

Quoted 22 lines · Miaohe Lin

Remove unneed local variable orig_refs since refs is unchanged now.

---
  mm/gup.c | 4 +---
  1 file changed, 1 insertion(+), 3 deletions(-)
mm/gup.c +0 -2
index 430495cb1b91..d6056dcab02c 100644
@@ -117,8 +117,6 @@ __maybe_unused struct page *try_grab_compound_head(struct page *page,
  	if (flags & FOLL_GET)
  		return try_get_compound_head(page, refs);
  	else if (flags & FOLL_PIN) {
-		int orig_refs = refs;
-
  		/*
  		 * Can't do FOLL_LONGTERM + FOLL_PIN gup fast path if not in a
  		 * right zone, so fail and let the caller fall back to the slow

mm/gup.c __maybe_unused struct page *try_grab_compound_head(struct page *page, · line 154

Quoted 8 lines
mm/gup.c +1 -1
@@ -150,7 +148,7 @@ __maybe_unused struct page *try_grab_compound_head(struct page *page,
  			page_ref_add(page, refs * (GUP_PIN_COUNTING_BIAS - 1));
 
  		mod_node_page_state(page_pgdat(page), NR_FOLL_PIN_ACQUIRED,
-				    orig_refs);
+				    refs);
 
  		return page;
  	}
Signature · 3 lines
Thanks,

David / dhildenb
author
#5 lore raw

PATCH 3/5 mm: gup: remove useless BUG_ON in __get_user_pages()

In reply to Miaohe Lin

Indeed, this BUG_ON couldn't catch anything useful. We are sure ret == 0 here because we would already bail out if ret != 0 and ret is untouched till here.

---
 mm/gup.c | 1 -
 1 file changed, 1 deletion(-)
mm/gup.c +0 -1
index d6056dcab02c..d7e4507de6b1 100644
@@ -1149,7 +1149,6 @@ static long __get_user_pages(struct mm_struct *mm,
 					 * We must stop here.
 					 */
 					BUG_ON(gup_flags & FOLL_NOWAIT);
-					BUG_ON(ret != 0);
 					goto out;
 				}
 				continue;
Signature · 1 line
2.23.0
Reviewed-by on v1 3/5
#6 lore raw

Re: [PATCH 3/5] mm: gup: remove useless BUG_ON in __get_user_pages()

Quoted 15 lines · Miaohe Lin

Indeed, this BUG_ON couldn't catch anything useful. We are sure ret == 0 here because we would already bail out if ret != 0 and ret is untouched till here.

---
  mm/gup.c | 1 -
  1 file changed, 1 deletion(-)
mm/gup.c +0 -0
index d6056dcab02c..d7e4507de6b1 100644

mm/gup.c static long __get_user_pages(struct mm_struct *mm, · line 1154

Quoted 7 lines
mm/gup.c +0 -1
@@ -1149,7 +1149,6 @@ static long __get_user_pages(struct mm_struct *mm,
  					 * We must stop here.
  					 */
  					BUG_ON(gup_flags & FOLL_NOWAIT);
-					BUG_ON(ret != 0);
  					goto out;
  				}
  				continue;
Signature · 3 lines
Thanks,

David / dhildenb
author
#7 lore raw

PATCH 4/5 mm: gup: fix potential pgmap refcnt leak in __gup_device_huge()

In reply to Miaohe Lin

When failed to try_grab_page, put_dev_pagemap() is missed. So pgmap refcnt will leak in this case. Also we remove the check for pgmap against NULL as it's also checked inside the put_dev_pagemap().

Fixes 3faa52c03f44 ("mm/gup: track FOLL_PIN pages")
---
 mm/gup.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
mm/gup.c +2 -2
index d7e4507de6b1..8c89e614d4aa 100644
@@ -2253,14 +2253,14 @@ static int __gup_device_huge(unsigned long pfn, unsigned long addr,
 		pages[*nr] = page;
 		if (unlikely(!try_grab_page(page, flags))) {
 			undo_dev_pagemap(nr, nr_start, flags, pages);
+			put_dev_pagemap(pgmap);
 			return 0;
 		}
 		(*nr)++;
 		pfn++;
 	} while (addr += PAGE_SIZE, addr != end);
 
-	if (pgmap)
-		put_dev_pagemap(pgmap);
+	put_dev_pagemap(pgmap);
 	return 1;
 }
 
Signature · 1 line
2.23.0
maintainer
#8 lore raw

Re: [PATCH 4/5] mm: gup: fix potential pgmap refcnt leak in __gup_device_huge()

Quoted 14 lines · On Sat, 7 Aug 2021 17:36:19 +0800 Miaohe Lin <[email protected]> wrote:

When failed to try_grab_page, put_dev_pagemap() is missed. So pgmap refcnt will leak in this case. Also we remove the check for pgmap against NULL as it's also checked inside the put_dev_pagemap().

...

mm/gup.c +1 -0
@@ -2253,14 +2253,14 @@ static int __gup_device_huge(unsigned long pfn, unsigned long addr,
 		pages[*nr] = page;
 		if (unlikely(!try_grab_page(page, flags))) {
 			undo_dev_pagemap(nr, nr_start, flags, pages);
+			put_dev_pagemap(pgmap);
 			return 0;

mm/gup.c static int __gup_device_huge(unsigned long pfn, unsigned long addr, · line 2265

Quoted 10 lines
mm/gup.c +1 -2
 		}
 		(*nr)++;
 		pfn++;
 	} while (addr += PAGE_SIZE, addr != end);
 
-	if (pgmap)
-		put_dev_pagemap(pgmap);
+	put_dev_pagemap(pgmap);
 	return 1;
 }

We can simplify further, and remove the troublesome multiple return points?

mm/gup.c renamed from mm/gup.c~mm-gup-fix-potential-pgmap-refcnt-leak-in-__gup_device_huge-fix +2 -3
@@ -2247,14 +2247,13 @@ static int __gup_device_huge(unsigned lo
 		pgmap = get_dev_pagemap(pfn, pgmap);
 		if (unlikely(!pgmap)) {
 			undo_dev_pagemap(nr, nr_start, flags, pages);
-			return 0;
+			break;
 		}
 		SetPageReferenced(page);
 		pages[*nr] = page;
 		if (unlikely(!try_grab_page(page, flags))) {
 			undo_dev_pagemap(nr, nr_start, flags, pages);
-			put_dev_pagemap(pgmap);
-			return 0;
+			break;
 		}
 		(*nr)++;
 		pfn++;

_

maintainer
#9 lore raw

Re: [PATCH 4/5] mm: gup: fix potential pgmap refcnt leak in __gup_device_huge()

Quoted 2 lines · On Sat, 7 Aug 2021 11:41:12 -0700 Andrew Morton <[email protected]>…

We can simplify further, and remove the troublesome multiple return points?

oops.

mm/gup.c renamed from mm/gup.c~mm-gup-fix-potential-pgmap-refcnt-leak-in-__gup_device_huge-fix-fix +4 -1
@@ -2240,6 +2240,7 @@ static int __gup_device_huge(unsigned lo
 {
 	int nr_start = *nr;
 	struct dev_pagemap *pgmap = NULL;
+	int ret = 1;
 
 	do {
 		struct page *page = pfn_to_page(pfn);
@@ -2247,12 +2248,14 @@ static int __gup_device_huge(unsigned lo
 		pgmap = get_dev_pagemap(pfn, pgmap);
 		if (unlikely(!pgmap)) {
 			undo_dev_pagemap(nr, nr_start, flags, pages);
+			ret = 0;
 			break;
 		}
 		SetPageReferenced(page);
 		pages[*nr] = page;
 		if (unlikely(!try_grab_page(page, flags))) {
 			undo_dev_pagemap(nr, nr_start, flags, pages);
+			ret = 0;
 			break;
 		}
 		(*nr)++;
@@ -2260,7 +2263,7 @@ static int __gup_device_huge(unsigned lo
 	} while (addr += PAGE_SIZE, addr != end);
 
 	put_dev_pagemap(pgmap);
-	return 1;
+	return ret;
 }
 
 static int __gup_device_huge_pmd(pmd_t orig, pmd_t *pmdp, unsigned long addr,

Not sure if it's worth bothering, really...

reviewer Reviewed-by
#10 lore raw

Re: [PATCH 4/5] mm: gup: fix potential pgmap refcnt leak in __gup_device_huge()

Quoted 6 lines · Andrew Morton
Quoted 2 lines · On Sat, 7 Aug 2021 11:41:12 -0700 Andrew Morton <[email protected]>…

We can simplify further, and remove the troublesome multiple return points?

oops.

I sent a reviewed by to the "+" fixup email, but just realized that that did not hit the main
mailing list. So:

For the end result of these stacked fixes to this file:

thanks,

Signature · 3 lines
John Hubbard

NVIDIA
Quoted 38 lines
mm/gup.c renamed from mm/gup.c~mm-gup-fix-potential-pgmap-refcnt-leak-in-__gup_device_huge-fix-fix +4 -1
@@ -2240,6 +2240,7 @@ static int __gup_device_huge(unsigned lo
  {
  	int nr_start = *nr;
  	struct dev_pagemap *pgmap = NULL;
+	int ret = 1;
 
  	do {
  		struct page *page = pfn_to_page(pfn);
@@ -2247,12 +2248,14 @@ static int __gup_device_huge(unsigned lo
  		pgmap = get_dev_pagemap(pfn, pgmap);
  		if (unlikely(!pgmap)) {
  			undo_dev_pagemap(nr, nr_start, flags, pages);
+			ret = 0;
  			break;
  		}
  		SetPageReferenced(page);
  		pages[*nr] = page;
  		if (unlikely(!try_grab_page(page, flags))) {
  			undo_dev_pagemap(nr, nr_start, flags, pages);
+			ret = 0;
  			break;
  		}
  		(*nr)++;
@@ -2260,7 +2263,7 @@ static int __gup_device_huge(unsigned lo
  	} while (addr += PAGE_SIZE, addr != end);
 
  	put_dev_pagemap(pgmap);
-	return 1;
+	return ret;
  }
 
  static int __gup_device_huge_pmd(pmd_t orig, pmd_t *pmdp, unsigned long addr,

Not sure if it's worth bothering, really...

author
#11 lore raw

PATCH 5/5 mm: gup: use helper PAGE_ALIGNED in populate_vma_page_range()

In reply to Miaohe Lin

Use helper PAGE_ALIGNED to check if address is aligned to PAGE_SIZE. Minor readability improvement.

---
 mm/gup.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
mm/gup.c +2 -2
index 8c89e614d4aa..802a3deb50cd 100644
@@ -1471,8 +1471,8 @@ long populate_vma_page_range(struct vm_area_struct *vma,
 	unsigned long nr_pages = (end - start) / PAGE_SIZE;
 	int gup_flags;
 
-	VM_BUG_ON(start & ~PAGE_MASK);
-	VM_BUG_ON(end   & ~PAGE_MASK);
+	VM_BUG_ON(!PAGE_ALIGNED(start));
+	VM_BUG_ON(!PAGE_ALIGNED(end));
 	VM_BUG_ON_VMA(start < vma->vm_start, vma);
 	VM_BUG_ON_VMA(end   > vma->vm_end, vma);
 	mmap_assert_locked(mm);
Signature · 1 line
2.23.0
Reviewed-by on v1 5/5
#12 lore raw

Re: [PATCH 5/5] mm: gup: use helper PAGE_ALIGNED in populate_vma_page_range()

Quoted 14 lines · Miaohe Lin

Use helper PAGE_ALIGNED to check if address is aligned to PAGE_SIZE. Minor readability improvement.

---
  mm/gup.c | 4 ++--
  1 file changed, 2 insertions(+), 2 deletions(-)
mm/gup.c +0 -0
index 8c89e614d4aa..802a3deb50cd 100644

mm/gup.c long populate_vma_page_range(struct vm_area_struct *vma, · line 1478

Quoted 10 lines
mm/gup.c +2 -2
@@ -1471,8 +1471,8 @@ long populate_vma_page_range(struct vm_area_struct *vma,
  	unsigned long nr_pages = (end - start) / PAGE_SIZE;
  	int gup_flags;
 
-	VM_BUG_ON(start & ~PAGE_MASK);
-	VM_BUG_ON(end   & ~PAGE_MASK);
+	VM_BUG_ON(!PAGE_ALIGNED(start));
+	VM_BUG_ON(!PAGE_ALIGNED(end));
  	VM_BUG_ON_VMA(start < vma->vm_start, vma);
  	VM_BUG_ON_VMA(end   > vma->vm_end, vma);
  	mmap_assert_locked(mm);

Making it look more like faultin_vma_page_range(), nice :)

Signature · 3 lines
Thanks,

David / dhildenb
reviewer Reviewed-by on the v1 series
#13 lore raw
In reply to Miaohe Lin
Quoted 13 lines · Miaohe Lin

Hi all,
This series contains cleanups to remove unneeded variable, useless BUG_ON and use helper to improve readability. Also we fix a potential pgmap refcnt leak. More details can be found in the respective changelogs. Thanks!

Miaohe Lin (5):
mm: gup: remove set but unused local variable major
mm: gup: remove unneed local variable orig_refs
mm: gup: remove useless BUG_ON in __get_user_pages()
mm: gup: fix potential pgmap refcnt leak in __gup_device_huge()
mm: gup: use helper PAGE_ALIGNED in populate_vma_page_range()

Quoted 3 lines
  mm/gup.c | 16 ++++++----------
  1 file changed, 6 insertions(+), 10 deletions(-)

For the series:

thanks,

Signature · 2 lines
John Hubbard
NVIDIA
Reviewed-by on the v1 series
#14 lore raw
In reply to Miaohe Lin
Quoted 13 lines · On Sat, 7 Aug 2021 17:36:15 +0800 Miaohe Lin <[email protected]> wrote:

Hi all,
This series contains cleanups to remove unneeded variable, useless BUG_ON and use helper to improve readability. Also we fix a potential pgmap refcnt leak. More details can be found in the respective changelogs. Thanks!

Miaohe Lin (5):
mm: gup: remove set but unused local variable major
mm: gup: remove unneed local variable orig_refs
mm: gup: remove useless BUG_ON in __get_user_pages()
mm: gup: fix potential pgmap refcnt leak in __gup_device_huge()
mm: gup: use helper PAGE_ALIGNED in populate_vma_page_range()

Quoted 3 lines
 mm/gup.c | 16 ++++++----------
 1 file changed, 6 insertions(+), 10 deletions(-)

Whole series (including the fixup for the return value):