Skip to contentSkip to conversation

mm/gup: Minor fix, cleanup and improvements

PATCH Applied Baoquan He 5 participants 40 messages Mar 31, 2025 – Apr 3, 2025

Subsystems (9)

v2 of 4 Applied Latest is v4 →

lore
Share & tools
lore

Baoquan He

author
#0 lore raw

v2 cover mm/gup: Minor fix, cleanup and improvements

These are made when I explore codes in mm/gup.c.

v1->v2:
- In patch 1, I carelessly copied the fault_in_readable() as fault_in_writeable(). Thanks to Yanjun for pointing it out.

- In patch 2, I copied the code in follow_page_pte() to __get_user_pages() directly w/o adjustment which is done but not merged to patch. That failed testing taken by lkp test robot, thanks for reporting.

Baoquan He (7):
mm/gup: fix wrongly calculated returned value in
fault_in_safe_writeable()
mm/gup: check if both GUP_GET and GUP_PIN are set in
__get_user_pages() earlier
mm/gup: Fix the outdated code comments above get_user_pages_unlocked() mm/gup: remove gup_fast_pgd_leaf() and clean up the relevant codes x86/mm: remove pgd_leaf definition in arch
x86/mm: remove p4d_leaf definition
mm/pgtable: remove unneeded pgd_devmap()

 arch/arm64/include/asm/pgtable.h             |  5 --
 arch/loongarch/include/asm/pgtable.h         |  1 -
 arch/powerpc/include/asm/book3s/64/pgtable.h |  5 --
 arch/riscv/include/asm/pgtable-64.h          |  5 --
 arch/x86/include/asm/pgtable.h               | 15 ----
 arch/x86/mm/pti.c                            |  4 +-
 include/linux/pgtable.h                      |  4 -
 mm/gup.c                                     | 79 ++++----------------
 8 files changed, 15 insertions(+), 103 deletions(-)
Signature · 1 line
2.41.0
author
#1 lore raw

PATCH v2 1/7 mm/gup: fix wrongly calculated returned value in fault_in_safe_writeable()

Not like fault_in_readable() or fault_in_writeable(), in fault_in_safe_writeable() local variable 'start' is increased page by page to loop till the whole address range is handled. However, it mistakenly calcalates the size of handled range with 'uaddr - start'.

Fix it here.

---
v1->v2:
- Fix a patch log typo caused by copy-and-paste error. Thanks to Yanjun.

 mm/gup.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
mm/gup.c +2 -2
index 855ab860f88b..73777b1de679 100644
@@ -2207,8 +2207,8 @@ size_t fault_in_safe_writeable(const char __user *uaddr, size_t size)
 	} while (start != end);
 	mmap_read_unlock(mm);
 
-	if (size > (unsigned long)uaddr - start)
-		return size - ((unsigned long)uaddr - start);
+	if (size > start - (unsigned long)uaddr)
+		return size - (start - (unsigned long)uaddr);
 	return 0;
 }
 EXPORT_SYMBOL(fault_in_safe_writeable);
Signature · 1 line
2.41.0
#2 lore raw

Re: [PATCH v2 1/7] mm/gup: fix wrongly calculated returned value in fault_in_safe_writeable()

Quoted 6 lines · Baoquan He

Not like fault_in_readable() or fault_in_writeable(), in fault_in_safe_writeable() local variable 'start' is increased page by page to loop till the whole address range is handled. However, it mistakenly calcalates the size of handled range with 'uaddr - start'.

Fix it here.

Fixes ? Do we know of user-visible effects?
Quoted 15 lines

---
v1->v2:
- Fix a patch log typo caused by copy-and-paste error. Thanks to Yanjun.

  mm/gup.c | 4 ++--
  1 file changed, 2 insertions(+), 2 deletions(-)
mm/gup.c +0 -0
index 855ab860f88b..73777b1de679 100644

mm/gup.c size_t fault_in_safe_writeable(const char __user *uaddr, size_t size) · line 2214

Quoted 10 lines
mm/gup.c +2 -2
@@ -2207,8 +2207,8 @@ size_t fault_in_safe_writeable(const char __user *uaddr, size_t size)
  	} while (start != end);
  	mmap_read_unlock(mm);
 
-	if (size > (unsigned long)uaddr - start)
-		return size - ((unsigned long)uaddr - start);
+	if (size > start - (unsigned long)uaddr)
+		return size - (start - (unsigned long)uaddr);
  	return 0;
  }
  EXPORT_SYMBOL(fault_in_safe_writeable);

Can we instead just use the uaddr and start variables like in fault_in_readable?

That is, turn "start" into a const and adjust uaddr instead.

(maybe we should also handle the types of these variables similar to as in fault_in_readable)

Signature · 3 lines
Cheers,

David / dhildenb
#3 lore raw

Re: [PATCH v2 1/7] mm/gup: fix wrongly calculated returned value in fault_in_safe_writeable()

Quoted 17 lines · David Hildenbrand

mm/gup.c size_t fault_in_safe_writeable(const char __user *uaddr, size_t size) · line 2213

Quoted 12 lines · Baoquan He
mm/gup.c +2 -2
@@ -2207,8 +2207,8 @@ size_t fault_in_safe_writeable(const char __user *uaddr, size_t size)
  	} while (start != end);
  	mmap_read_unlock(mm);
-	if (size > (unsigned long)uaddr - start)
-		return size - ((unsigned long)uaddr - start);
+	if (size > start - (unsigned long)uaddr)
+		return size - (start - (unsigned long)uaddr);
  	return 0;
  }
  EXPORT_SYMBOL(fault_in_safe_writeable);

Can we instead just use the uaddr and start variables like in fault_in_readable?

Quoted 1 line

That is, turn "start" into a const and adjust uaddr instead.

Yes, I think that would be much cleaner.

Otherwise, this looks good to me.

Signature · 2 lines
Oscar Salvador
SUSE Labs
author
#4 lore raw

Re: [PATCH v2 1/7] mm/gup: fix wrongly calculated returned value in fault_in_safe_writeable()

Quoted 18 lines · Oscar Salvador
Quoted 17 lines · David Hildenbrand

mm/gup.c size_t fault_in_safe_writeable(const char __user *uaddr, size_t size) · line 2213

Quoted 12 lines · Baoquan He
mm/gup.c +2 -2
@@ -2207,8 +2207,8 @@ size_t fault_in_safe_writeable(const char __user *uaddr, size_t size)
  	} while (start != end);
  	mmap_read_unlock(mm);
-	if (size > (unsigned long)uaddr - start)
-		return size - ((unsigned long)uaddr - start);
+	if (size > start - (unsigned long)uaddr)
+		return size - (start - (unsigned long)uaddr);
  	return 0;
  }
  EXPORT_SYMBOL(fault_in_safe_writeable);

Can we instead just use the uaddr and start variables like in fault_in_readable?

Quoted 5 lines
Quoted 1 line

That is, turn "start" into a const and adjust uaddr instead.

Yes, I think that would be much cleaner.

Otherwise, this looks good to me.

Will change in v3 as both of you suggested, thanks for reviewing this v2 series.

author
#5 lore raw

Re: [PATCH v2 1/7] mm/gup: fix wrongly calculated returned value in fault_in_safe_writeable()

In reply to David Hildenbrand
Quoted 9 lines · David Hildenbrand
Quoted 6 lines · Baoquan He

Not like fault_in_readable() or fault_in_writeable(), in fault_in_safe_writeable() local variable 'start' is increased page by page to loop till the whole address range is handled. However, it mistakenly calcalates the size of handled range with 'uaddr - start'.

Fix it here.

Fixes ? Do we know of user-visible effects?

I believe it should impact, while I didn't hear of any complaint. Yeah, it's worth to have one Fixes.

Quoted 29 lines
Quoted 16 lines

---
v1->v2:
- Fix a patch log typo caused by copy-and-paste error. Thanks to Yanjun.

  mm/gup.c | 4 ++--
  1 file changed, 2 insertions(+), 2 deletions(-)
mm/gup.c +0 -0
index 855ab860f88b..73777b1de679 100644

mm/gup.c size_t fault_in_safe_writeable(const char __user *uaddr, size_t size) · line 2213

Quoted 9 lines
mm/gup.c +2 -2
@@ -2207,8 +2207,8 @@ size_t fault_in_safe_writeable(const char __user *uaddr, size_t size)
  	} while (start != end);
  	mmap_read_unlock(mm);
-	if (size > (unsigned long)uaddr - start)
-		return size - ((unsigned long)uaddr - start);
+	if (size > start - (unsigned long)uaddr)
+		return size - (start - (unsigned long)uaddr);
  	return 0;
  }
  EXPORT_SYMBOL(fault_in_safe_writeable);

Can we instead just use the uaddr and start variables like in fault_in_readable?

Quoted 1 line

That is, turn "start" into a const and adjust uaddr instead.

Sounds good to me, that makes these similar blocks own consistent code style. Will change in v3. Thanks for reviewing.

Quoted 9 lines

(maybe we should also handle the types of these variables similar to as in fault_in_readable)

Signature · 3 lines
Cheers,

David / dhildenb
author
#6 lore raw

PATCH v2 2/7 mm/gup: check if both GUP_GET and GUP_PIN are set in __get_user_pages() earlier

In reply to Baoquan He

In __get_user_pages(), it will traverse page table and take a reference to the page the given user address corresponds to if GUP_GET or GUP_PIN is et. However, it's not supported both GUP_GET and GUP_PIN are set. This check should be done earlier, but not doing it till entering into follow_page_pte() and failed.

Here move the checking to the beginning of __get_user_pages().

---
v1->v2:
- Fix code bug caused by copy-and-paste error, this is reported by lkp test robot.

 mm/gup.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)
mm/gup.c +5 -5
index 73777b1de679..f9bce14ed3cd 100644
@@ -847,11 +847,6 @@ static struct page *follow_page_pte(struct vm_area_struct *vma,
 	pte_t *ptep, pte;
 	int ret;
 
-	/* FOLL_GET and FOLL_PIN are mutually exclusive. */
-	if (WARN_ON_ONCE((flags & (FOLL_PIN | FOLL_GET)) ==
-			 (FOLL_PIN | FOLL_GET)))
-		return ERR_PTR(-EINVAL);
-
 	ptep = pte_offset_map_lock(mm, pmd, address, &ptl);
 	if (!ptep)
 		return no_page_table(vma, flags, address);
@@ -1434,6 +1429,11 @@ static long __get_user_pages(struct mm_struct *mm,
 
 	VM_BUG_ON(!!pages != !!(gup_flags & (FOLL_GET | FOLL_PIN)));
 
+	/* FOLL_GET and FOLL_PIN are mutually exclusive. */
+	if (WARN_ON_ONCE((gup_flags & (FOLL_PIN | FOLL_GET)) ==
+			 (FOLL_PIN | FOLL_GET)))
+		return -EINVAL;
+
 	do {
 		struct page *page;
 		unsigned int page_increm;
Signature · 1 line
2.41.0
#7 lore raw

Re: [PATCH v2 2/7] mm/gup: check if both GUP_GET and GUP_PIN are set in __get_user_pages() earlier

Quoted 34 lines · Baoquan He

In __get_user_pages(), it will traverse page table and take a reference to the page the given user address corresponds to if GUP_GET or GUP_PIN is et. However, it's not supported both GUP_GET and GUP_PIN are set. This check should be done earlier, but not doing it till entering into follow_page_pte() and failed.

Here move the checking to the beginning of __get_user_pages().

---
v1->v2:
- Fix code bug caused by copy-and-paste error, this is reported by lkp test robot.

  mm/gup.c | 10 +++++-----
  1 file changed, 5 insertions(+), 5 deletions(-)
mm/gup.c +0 -5
index 73777b1de679..f9bce14ed3cd 100644
@@ -847,11 +847,6 @@ static struct page *follow_page_pte(struct vm_area_struct *vma,
  	pte_t *ptep, pte;
  	int ret;
 
-	/* FOLL_GET and FOLL_PIN are mutually exclusive. */
-	if (WARN_ON_ONCE((flags & (FOLL_PIN | FOLL_GET)) ==
-			 (FOLL_PIN | FOLL_GET)))
-		return ERR_PTR(-EINVAL);
-
  	ptep = pte_offset_map_lock(mm, pmd, address, &ptl);
  	if (!ptep)
  		return no_page_table(vma, flags, address);

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

Quoted 8 lines
mm/gup.c +5 -0
@@ -1434,6 +1429,11 @@ static long __get_user_pages(struct mm_struct *mm,
 
  	VM_BUG_ON(!!pages != !!(gup_flags & (FOLL_GET | FOLL_PIN)));
 
+	/* FOLL_GET and FOLL_PIN are mutually exclusive. */
+	if (WARN_ON_ONCE((gup_flags & (FOLL_PIN | FOLL_GET)) ==
+			 (FOLL_PIN | FOLL_GET)))
+		return -EINVAL;
+

We already have that check in is_valid_gup_args(), that catches all external users that could possibly mess this up.

So we can just convert that into a VM_WARN_ON_ONCE(), and while doing that, we should convert the VM_BUG_ON() above to a VM_WARN_ON_ONCE() as well.

Signature · 3 lines
Cheers,

David / dhildenb
author
#8 lore raw

Re: [PATCH v2 2/7] mm/gup: check if both GUP_GET and GUP_PIN are set in __get_user_pages() earlier

Quoted 35 lines · David Hildenbrand
Quoted 34 lines · Baoquan He

In __get_user_pages(), it will traverse page table and take a reference to the page the given user address corresponds to if GUP_GET or GUP_PIN is et. However, it's not supported both GUP_GET and GUP_PIN are set. This check should be done earlier, but not doing it till entering into follow_page_pte() and failed.

Here move the checking to the beginning of __get_user_pages().

---
v1->v2:
- Fix code bug caused by copy-and-paste error, this is reported by lkp test robot.

  mm/gup.c | 10 +++++-----
  1 file changed, 5 insertions(+), 5 deletions(-)
mm/gup.c +0 -5
index 73777b1de679..f9bce14ed3cd 100644
@@ -847,11 +847,6 @@ static struct page *follow_page_pte(struct vm_area_struct *vma,
  	pte_t *ptep, pte;
  	int ret;
-	/* FOLL_GET and FOLL_PIN are mutually exclusive. */
-	if (WARN_ON_ONCE((flags & (FOLL_PIN | FOLL_GET)) ==
-			 (FOLL_PIN | FOLL_GET)))
-		return ERR_PTR(-EINVAL);
-
  	ptep = pte_offset_map_lock(mm, pmd, address, &ptl);
  	if (!ptep)
  		return no_page_table(vma, flags, address);
@@ -1434,6 +1429,11 @@ static long __get_user_pages(struct mm_struct *mm,
  	VM_BUG_ON(!!pages != !!(gup_flags & (FOLL_GET | FOLL_PIN)));
Quoted 8 lines

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

Quoted 5 lines
mm/gup.c +5 -0
+	/* FOLL_GET and FOLL_PIN are mutually exclusive. */
+	if (WARN_ON_ONCE((gup_flags & (FOLL_PIN | FOLL_GET)) ==
+			 (FOLL_PIN | FOLL_GET)))
+		return -EINVAL;
+

We already have that check in is_valid_gup_args(), that catches all external users that could possibly mess this up.

Right.

Quoted 3 lines

So we can just convert that into a VM_WARN_ON_ONCE(), and while doing that, we should convert the VM_BUG_ON() above to a VM_WARN_ON_ONCE() as well.

Sounds great to me, will put below change into this patch of v3 as suggested. Thanks.

mm/gup.c +2 -2
index 9e4ed09c578b..d551da9549b1 100644
@@ -1427,10 +1427,10 @@ static long __get_user_pages(struct mm_struct *mm,
 
 	start = untagged_addr_remote(mm, start);
 
-	VM_BUG_ON(!!pages != !!(gup_flags & (FOLL_GET | FOLL_PIN)));
+	VM_WARN_ON_ONCE(!!pages != !!(gup_flags & (FOLL_GET | FOLL_PIN)));
 
 	/* FOLL_GET and FOLL_PIN are mutually exclusive. */
-	if (WARN_ON_ONCE((gup_flags & (FOLL_PIN | FOLL_GET)) ==
+	if (VM_WARN_ON_ONCE((gup_flags & (FOLL_PIN | FOLL_GET)) ==
 			 (FOLL_PIN | FOLL_GET)))
 		return -EINVAL;
 
#9 lore raw

Re: [PATCH v2 2/7] mm/gup: check if both GUP_GET and GUP_PIN are set in __get_user_pages() earlier

Quoted 59 lines · Baoquan He
Quoted 35 lines · David Hildenbrand
Quoted 34 lines · Baoquan He

In __get_user_pages(), it will traverse page table and take a reference to the page the given user address corresponds to if GUP_GET or GUP_PIN is et. However, it's not supported both GUP_GET and GUP_PIN are set. This check should be done earlier, but not doing it till entering into follow_page_pte() and failed.

Here move the checking to the beginning of __get_user_pages().

---
v1->v2:
- Fix code bug caused by copy-and-paste error, this is reported by lkp test robot.

   mm/gup.c | 10 +++++-----
   1 file changed, 5 insertions(+), 5 deletions(-)
mm/gup.c +0 -5
index 73777b1de679..f9bce14ed3cd 100644
@@ -847,11 +847,6 @@ static struct page *follow_page_pte(struct vm_area_struct *vma,
   	pte_t *ptep, pte;
   	int ret;
-	/* FOLL_GET and FOLL_PIN are mutually exclusive. */
-	if (WARN_ON_ONCE((flags & (FOLL_PIN | FOLL_GET)) ==
-			 (FOLL_PIN | FOLL_GET)))
-		return ERR_PTR(-EINVAL);
-
   	ptep = pte_offset_map_lock(mm, pmd, address, &ptl);
   	if (!ptep)
   		return no_page_table(vma, flags, address);
@@ -1434,6 +1429,11 @@ static long __get_user_pages(struct mm_struct *mm,
   	VM_BUG_ON(!!pages != !!(gup_flags & (FOLL_GET | FOLL_PIN)));
Quoted 8 lines

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

Quoted 5 lines
mm/gup.c +5 -0
+	/* FOLL_GET and FOLL_PIN are mutually exclusive. */
+	if (WARN_ON_ONCE((gup_flags & (FOLL_PIN | FOLL_GET)) ==
+			 (FOLL_PIN | FOLL_GET)))
+		return -EINVAL;
+

We already have that check in is_valid_gup_args(), that catches all external users that could possibly mess this up.

Right.

Quoted 3 lines

So we can just convert that into a VM_WARN_ON_ONCE(), and while doing that, we should convert the VM_BUG_ON() above to a VM_WARN_ON_ONCE() as well.

Sounds great to me, will put below change into this patch of v3 as suggested. Thanks.

mm/gup.c +0 -0
index 9e4ed09c578b..d551da9549b1 100644

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

Quoted 10 lines
mm/gup.c +2 -2
@@ -1427,10 +1427,10 @@ static long __get_user_pages(struct mm_struct *mm,
 
  	start = untagged_addr_remote(mm, start);
 
-	VM_BUG_ON(!!pages != !!(gup_flags & (FOLL_GET | FOLL_PIN)));
+	VM_WARN_ON_ONCE(!!pages != !!(gup_flags & (FOLL_GET | FOLL_PIN)));
 
  	/* FOLL_GET and FOLL_PIN are mutually exclusive. */
-	if (WARN_ON_ONCE((gup_flags & (FOLL_PIN | FOLL_GET)) ==
+	if (VM_WARN_ON_ONCE((gup_flags & (FOLL_PIN | FOLL_GET)) ==
  			 (FOLL_PIN | FOLL_GET)))

That won't work (or shouldn't work :) ). Just drop the handling, it's a sanity check we don't expect to ever trigger, just like the old VM_BUG_ON above.

Signature · 3 lines
Cheers,

David / dhildenb
author
#10 lore raw

Re: [PATCH v2 2/7] mm/gup: check if both GUP_GET and GUP_PIN are set in __get_user_pages() earlier

Quoted 3 lines · David Hildenbrand
Quoted 2 lines · Baoquan He
Quoted 1 line · David Hildenbrand

On 31.03.25 10:13, Baoquan He wrote:

...snip...

Quoted 12 lines
Quoted 12 lines
Quoted 2 lines

We already have that check in is_valid_gup_args(), that catches all external users that could possibly mess this up.

Right.

Quoted 3 lines

So we can just convert that into a VM_WARN_ON_ONCE(), and while doing that, we should convert the VM_BUG_ON() above to a VM_WARN_ON_ONCE() as well.

Sounds great to me, will put below change into this patch of v3 as suggested. Thanks.

Quoted 16 lines

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

Quoted 12 lines
mm/gup.c +2 -2
index 9e4ed09c578b..d551da9549b1 100644
@@ -1427,10 +1427,10 @@ static long __get_user_pages(struct mm_struct *mm,
  	start = untagged_addr_remote(mm, start);
-	VM_BUG_ON(!!pages != !!(gup_flags & (FOLL_GET | FOLL_PIN)));
+	VM_WARN_ON_ONCE(!!pages != !!(gup_flags & (FOLL_GET | FOLL_PIN)));
  	/* FOLL_GET and FOLL_PIN are mutually exclusive. */
-	if (WARN_ON_ONCE((gup_flags & (FOLL_PIN | FOLL_GET)) ==
+	if (VM_WARN_ON_ONCE((gup_flags & (FOLL_PIN | FOLL_GET)) ==
  			 (FOLL_PIN | FOLL_GET)))

That won't work (or shouldn't work :) ). Just drop the handling, it's a sanity check we don't expect to ever trigger, just like the old VM_BUG_ON above.

Ah, I misunderstood it. Will add below change into this patch of v3.

mm/gup.c +3 -6
index 9e4ed09c578b..a8a10a8ebf14 100644
@@ -1427,12 +1427,9 @@ static long __get_user_pages(struct mm_struct *mm,
 
 	start = untagged_addr_remote(mm, start);
 
-	VM_BUG_ON(!!pages != !!(gup_flags & (FOLL_GET | FOLL_PIN)));
-
-	/* FOLL_GET and FOLL_PIN are mutually exclusive. */
-	if (WARN_ON_ONCE((gup_flags & (FOLL_PIN | FOLL_GET)) ==
-			 (FOLL_PIN | FOLL_GET)))
-		return -EINVAL;
+	VM_WARN_ON_ONCE(!!pages != !!(gup_flags & (FOLL_GET | FOLL_PIN)));
+	VM_WARN_ON_ONCE((gup_flags & (FOLL_PIN | FOLL_GET)) ==
+			(FOLL_PIN | FOLL_GET));
 
 	do {
 		struct page *page;
author
#11 lore raw

PATCH v2 3/7 mm/gup: Fix the outdated code comments above get_user_pages_unlocked()

In reply to Baoquan He

Since commit f0818f472d8d ("mm: gup: add get_user_pages_locked and get_user_pages_unlocked"), get_user_pages() doesn't need to have mmap_lock held anymore. It calls __get_user_pages_locked() which can acquire and drop the mmap_lock internaly.

Hence remove the incorrect code comments now.

---
 mm/gup.c | 16 +++-------------
 1 file changed, 3 insertions(+), 13 deletions(-)
mm/gup.c +3 -13
index f9bce14ed3cd..a15317cf6641 100644
@@ -2702,19 +2702,9 @@ long get_user_pages(unsigned long start, unsigned long nr_pages,
 EXPORT_SYMBOL(get_user_pages);
 
 /*
- * get_user_pages_unlocked() is suitable to replace the form:
- *
- *      mmap_read_lock(mm);
- *      get_user_pages(mm, ..., pages, NULL);
- *      mmap_read_unlock(mm);
- *
- *  with:
- *
- *      get_user_pages_unlocked(mm, ..., pages);
- *
- * It is functionally equivalent to get_user_pages_fast so
- * get_user_pages_fast should be used instead if specific gup_flags
- * (e.g. FOLL_FORCE) are not required.
+ * get_user_pages_unlocked() is functionally equivalent to
+ * get_user_pages_fast so get_user_pages_fast should be used instead
+ * if specific gup_flags (e.g. FOLL_FORCE) are not required.
  */
 long get_user_pages_unlocked(unsigned long start, unsigned long nr_pages,
 			     struct page **pages, unsigned int gup_flags)
Signature · 1 line
2.41.0
#12 lore raw

Re: [PATCH v2 3/7] mm/gup: Fix the outdated code comments above get_user_pages_unlocked()

Quoted 4 lines · Baoquan He

Since commit f0818f472d8d ("mm: gup: add get_user_pages_locked and get_user_pages_unlocked"), get_user_pages() doesn't need to have mmap_lock held anymore. It calls __get_user_pages_locked() which can acquire and drop the mmap_lock internaly.

s/internaly/internally/

But your statement is wrong. get_user_pages() must be called with the mmap_lock held, because it sets "int locked = 1;" when calling __get_user_pages_locked().

Signature · 3 lines
Cheers,

David / dhildenb
author
#13 lore raw

Re: [PATCH v2 3/7] mm/gup: Fix the outdated code comments above get_user_pages_unlocked()

Quoted 11 lines · David Hildenbrand
Quoted 4 lines · Baoquan He

Since commit f0818f472d8d ("mm: gup: add get_user_pages_locked and get_user_pages_unlocked"), get_user_pages() doesn't need to have mmap_lock held anymore. It calls __get_user_pages_locked() which can acquire and drop the mmap_lock internaly.

s/internaly/internally/

But your statement is wrong. get_user_pages() must be called with the mmap_lock held, because it sets "int locked = 1;" when calling __get_user_pages_locked().

You are right. I missed that line. Oscar pointed that out in v1 thread. I will drop this patch. Thanks.

#14 lore raw

Re: [PATCH v2 3/7] mm/gup: Fix the outdated code comments above get_user_pages_unlocked()

In reply to Baoquan He
Quoted 6 lines · Baoquan He

Since commit f0818f472d8d ("mm: gup: add get_user_pages_locked and get_user_pages_unlocked"), get_user_pages() doesn't need to have mmap_lock held anymore. It calls __get_user_pages_locked() which can acquire and drop the mmap_lock internaly.

Hence remove the incorrect code comments now.

Uhm, I think this one should be dropped according to

https://lore.kernel.org/linux-mm/[email protected]/T/#m1aa161016ab98a6d85bcb657a729e01cb98763ec

?

Signature · 2 lines
Oscar Salvador
SUSE Labs
author
#15 lore raw

Re: [PATCH v2 3/7] mm/gup: Fix the outdated code comments above get_user_pages_unlocked()

Quoted 9 lines · Oscar Salvador
Quoted 6 lines · Baoquan He

Since commit f0818f472d8d ("mm: gup: add get_user_pages_locked and get_user_pages_unlocked"), get_user_pages() doesn't need to have mmap_lock held anymore. It calls __get_user_pages_locked() which can acquire and drop the mmap_lock internaly.

Hence remove the incorrect code comments now.

Uhm, I think this one should be dropped according to

Yeah, this v2 series was sent earlier than your comment in v1. Sorry about the mess.

Quoted 10 lines

https://lore.kernel.org/linux-mm/[email protected]/T/#m1aa161016ab98a6d85bcb657a729e01cb98763ec

?

Signature · 2 lines
Oscar Salvador
SUSE Labs
author
#16 lore raw

PATCH v2 4/7 mm/gup: remove gup_fast_pgd_leaf() and clean up the relevant codes

In reply to Baoquan He

In the current kernel, only pud huge page is supported in some architectures. P4d and pgd huge pages haven't been supported yet. And in mm/gup.c, there's no pgd huge page handling in the follow_page_mask() code path. Hence it doesn't make sense to have gup_fast_pgd_leaf() in gup_fast code path.

Here remove gup_fast_pgd_leaf() and clean up the relevant codes.

---
 mm/gup.c | 49 +++----------------------------------------------
 1 file changed, 3 insertions(+), 46 deletions(-)
mm/gup.c +3 -46
index a15317cf6641..58cdc5605a4a 100644
@@ -3168,46 +3168,6 @@ static int gup_fast_pud_leaf(pud_t orig, pud_t *pudp, unsigned long addr,
 	return 1;
 }
 
-static int gup_fast_pgd_leaf(pgd_t orig, pgd_t *pgdp, unsigned long addr,
-		unsigned long end, unsigned int flags, struct page **pages,
-		int *nr)
-{
-	int refs;
-	struct page *page;
-	struct folio *folio;
-
-	if (!pgd_access_permitted(orig, flags & FOLL_WRITE))
-		return 0;
-
-	BUILD_BUG_ON(pgd_devmap(orig));
-
-	page = pgd_page(orig);
-	refs = record_subpages(page, PGDIR_SIZE, addr, end, pages + *nr);
-
-	folio = try_grab_folio_fast(page, refs, flags);
-	if (!folio)
-		return 0;
-
-	if (unlikely(pgd_val(orig) != pgd_val(*pgdp))) {
-		gup_put_folio(folio, refs, flags);
-		return 0;
-	}
-
-	if (!pgd_write(orig) && gup_must_unshare(NULL, flags, &folio->page)) {
-		gup_put_folio(folio, refs, flags);
-		return 0;
-	}
-
-	if (!gup_fast_folio_allowed(folio, flags)) {
-		gup_put_folio(folio, refs, flags);
-		return 0;
-	}
-
-	*nr += refs;
-	folio_set_referenced(folio);
-	return 1;
-}
-
 static int gup_fast_pmd_range(pud_t *pudp, pud_t pud, unsigned long addr,
 		unsigned long end, unsigned int flags, struct page **pages,
 		int *nr)
@@ -3302,12 +3262,9 @@ static void gup_fast_pgd_range(unsigned long addr, unsigned long end,
 		next = pgd_addr_end(addr, end);
 		if (pgd_none(pgd))
 			return;
-		if (unlikely(pgd_leaf(pgd))) {
-			if (!gup_fast_pgd_leaf(pgd, pgdp, addr, next, flags,
-					       pages, nr))
-				return;
-		} else if (!gup_fast_p4d_range(pgdp, pgd, addr, next, flags,
-					       pages, nr))
+		BUILD_BUG_ON(pgd_leaf(pgd));
+		if (!gup_fast_p4d_range(pgdp, pgd, addr, next, flags,
+					pages, nr))
 			return;
 	} while (pgdp++, addr = next, addr != end);
 }
Signature · 1 line
2.41.0
Acked-by on v2 4/7
#17 lore raw

Re: [PATCH v2 4/7] mm/gup: remove gup_fast_pgd_leaf() and clean up the relevant codes

Quoted 5 lines · Baoquan He

In the current kernel, only pud huge page is supported in some architectures. P4d and pgd huge pages haven't been supported yet. And in mm/gup.c, there's no pgd huge page handling in the follow_page_mask() code path. Hence it doesn't make sense to have gup_fast_pgd_leaf() in gup_fast code path.

I wonder if that was in place to handle (prepare for) very large hugetlb folios. Until a while ago, follow_page_mask() did not have to handle these hugetlb folios.

But I assume it never got used.

Quoted 75 lines

Here remove gup_fast_pgd_leaf() and clean up the relevant codes.

---
  mm/gup.c | 49 +++----------------------------------------------
  1 file changed, 3 insertions(+), 46 deletions(-)
mm/gup.c +3 -46
index a15317cf6641..58cdc5605a4a 100644
@@ -3168,46 +3168,6 @@ static int gup_fast_pud_leaf(pud_t orig, pud_t *pudp, unsigned long addr,
  	return 1;
  }
 
-static int gup_fast_pgd_leaf(pgd_t orig, pgd_t *pgdp, unsigned long addr,
-		unsigned long end, unsigned int flags, struct page **pages,
-		int *nr)
-{
-	int refs;
-	struct page *page;
-	struct folio *folio;
-
-	if (!pgd_access_permitted(orig, flags & FOLL_WRITE))
-		return 0;
-
-	BUILD_BUG_ON(pgd_devmap(orig));
-
-	page = pgd_page(orig);
-	refs = record_subpages(page, PGDIR_SIZE, addr, end, pages + *nr);
-
-	folio = try_grab_folio_fast(page, refs, flags);
-	if (!folio)
-		return 0;
-
-	if (unlikely(pgd_val(orig) != pgd_val(*pgdp))) {
-		gup_put_folio(folio, refs, flags);
-		return 0;
-	}
-
-	if (!pgd_write(orig) && gup_must_unshare(NULL, flags, &folio->page)) {
-		gup_put_folio(folio, refs, flags);
-		return 0;
-	}
-
-	if (!gup_fast_folio_allowed(folio, flags)) {
-		gup_put_folio(folio, refs, flags);
-		return 0;
-	}
-
-	*nr += refs;
-	folio_set_referenced(folio);
-	return 1;
-}
-
  static int gup_fast_pmd_range(pud_t *pudp, pud_t pud, unsigned long addr,
  		unsigned long end, unsigned int flags, struct page **pages,
  		int *nr)
@@ -3302,12 +3262,9 @@ static void gup_fast_pgd_range(unsigned long addr, unsigned long end,
  		next = pgd_addr_end(addr, end);
  		if (pgd_none(pgd))
  			return;
-		if (unlikely(pgd_leaf(pgd))) {
-			if (!gup_fast_pgd_leaf(pgd, pgdp, addr, next, flags,
-					       pages, nr))
-				return;
-		} else if (!gup_fast_p4d_range(pgdp, pgd, addr, next, flags,
-					       pages, nr))
+		BUILD_BUG_ON(pgd_leaf(pgd));
+		if (!gup_fast_p4d_range(pgdp, pgd, addr, next, flags,
+					pages, nr))
  			return;
  	} while (pgdp++, addr = next, addr != end);
  }
Signature · 3 lines
Cheers,

David / dhildenb
Reviewed-by on v2 4/7
#18 lore raw

Re: [PATCH v2 4/7] mm/gup: remove gup_fast_pgd_leaf() and clean up the relevant codes

In reply to Baoquan He
Quoted 9 lines · Baoquan He

In the current kernel, only pud huge page is supported in some architectures. P4d and pgd huge pages haven't been supported yet. And in mm/gup.c, there's no pgd huge page handling in the follow_page_mask() code path. Hence it doesn't make sense to have gup_fast_pgd_leaf() in gup_fast code path.

Here remove gup_fast_pgd_leaf() and clean up the relevant codes.

Signature · 2 lines
Oscar Salvador
SUSE Labs
author
#19 lore raw

PATCH v2 5/7 x86/mm: remove pgd_leaf definition in arch

In reply to Baoquan He

pgd huge page is not supported yet, let's use the generic definition in linux/pgtable.h.

And also update the BUILD_BUG_ON() checking for pgd_leaf() in pti_user_pagetable_walk_p4d() because pgd_leaf() returns boolean value.

---
 arch/x86/include/asm/pgtable.h | 3 ---
 arch/x86/mm/pti.c              | 2 +-
 2 files changed, 1 insertion(+), 4 deletions(-)
arch/x86/include/asm/pgtable.h +0 -3
index 7bd6bd6df4a1..5f4fcc0eea17 100644
@@ -1472,9 +1472,6 @@ static inline bool pgdp_maps_userspace(void *__ptr)
 	return (((ptr & ~PAGE_MASK) / sizeof(pgd_t)) < PGD_KERNEL_START);
 }
 
-#define pgd_leaf	pgd_leaf
-static inline bool pgd_leaf(pgd_t pgd) { return false; }
-
 #ifdef CONFIG_MITIGATION_PAGE_TABLE_ISOLATION
 /*
  * All top-level MITIGATION_PAGE_TABLE_ISOLATION page tables are order-1 pages
arch/x86/mm/pti.c +1 -1
index 5f0d579932c6..c2e1de40136f 100644
@@ -185,7 +185,7 @@ static p4d_t *pti_user_pagetable_walk_p4d(unsigned long address)
 
 		set_pgd(pgd, __pgd(_KERNPG_TABLE | __pa(new_p4d_page)));
 	}
-	BUILD_BUG_ON(pgd_leaf(*pgd) != 0);
+	BUILD_BUG_ON(pgd_leaf(*pgd));
 
 	return p4d_offset(pgd, address);
 }
Signature · 1 line
2.41.0
merge bot Applied
#20 lore raw

[tip: x86/mm] x86/mm: Simplify the pgd_leaf() and p4d_leaf() checks a bit

The following commit has been merged into the x86/mm branch of tip:

The following commit has been merged into the x86/mm branch of tip:

Commit-ID:     e62c7a4bd1d1e99e1ddd947cb526f833992c5bd4
Gitweb:        https://git.kernel.org/tip/e62c7a4bd1d1e99e1ddd947cb526f833992c5bd4
Author:        Baoquan He <[email protected]>
AuthorDate:    Mon, 31 Mar 2025 16:13:25 +08:00
Committer:     Ingo Molnar <[email protected]>
CommitterDate: Mon, 31 Mar 2025 12:08:17 +02:00

x86/mm: Simplify the pgd_leaf() and p4d_leaf() checks a bit

The functions return bool, simplify the checks.

[ mingo: Split off from two other patches. ]

---
 arch/x86/mm/pti.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
arch/x86/mm/pti.c +2 -2
index 5f0d579..1902998 100644
@@ -185,7 +185,7 @@ static p4d_t *pti_user_pagetable_walk_p4d(unsigned long address)
 
 		set_pgd(pgd, __pgd(_KERNPG_TABLE | __pa(new_p4d_page)));
 	}
-	BUILD_BUG_ON(pgd_leaf(*pgd) != 0);
+	BUILD_BUG_ON(pgd_leaf(*pgd));
 
 	return p4d_offset(pgd, address);
 }
@@ -206,7 +206,7 @@ static pmd_t *pti_user_pagetable_walk_pmd(unsigned long address)
 	if (!p4d)
 		return NULL;
 
-	BUILD_BUG_ON(p4d_leaf(*p4d) != 0);
+	BUILD_BUG_ON(p4d_leaf(*p4d));
 	if (p4d_none(*p4d)) {
 		unsigned long new_pud_page = __get_free_page(gfp);
 		if (WARN_ON_ONCE(!new_pud_page))
merge bot Applied
#21 lore raw

[tip: x86/mm] x86/mm: Remove the arch-specific pgd_leaf() definition

In reply to Baoquan He
The following commit has been merged into the x86/mm branch of tip:

The following commit has been merged into the x86/mm branch of tip:

Commit-ID:     65525a3787ff082575fe29381d4dc8fd535634f2
Gitweb:        https://git.kernel.org/tip/65525a3787ff082575fe29381d4dc8fd535634f2
Author:        Baoquan He <[email protected]>
AuthorDate:    Mon, 31 Mar 2025 16:13:25 +08:00
Committer:     Ingo Molnar <[email protected]>
CommitterDate: Mon, 31 Mar 2025 12:08:17 +02:00

x86/mm: Remove the arch-specific pgd_leaf() definition

PGD huge pages are not supported yet, let's use the generic definition in <linux/pgtable.h>.

[ mingo: Cleaned up the changelog. ]

---
 arch/x86/include/asm/pgtable.h | 3 ---
 1 file changed, 3 deletions(-)
arch/x86/include/asm/pgtable.h +0 -3
index 7bd6bd6..5f4fcc0 100644
@@ -1472,9 +1472,6 @@ static inline bool pgdp_maps_userspace(void *__ptr)
 	return (((ptr & ~PAGE_MASK) / sizeof(pgd_t)) < PGD_KERNEL_START);
 }
 
-#define pgd_leaf	pgd_leaf
-static inline bool pgd_leaf(pgd_t pgd) { return false; }
-
 #ifdef CONFIG_MITIGATION_PAGE_TABLE_ISOLATION
 /*
  * All top-level MITIGATION_PAGE_TABLE_ISOLATION page tables are order-1 pages
Acked-by on v2 5/7
#22 lore raw

Re: [PATCH v2 5/7] x86/mm: remove pgd_leaf definition in arch

In reply to Baoquan He
Quoted 9 lines · Baoquan He

pgd huge page is not supported yet, let's use the generic definition in linux/pgtable.h.

And also update the BUILD_BUG_ON() checking for pgd_leaf() in pti_user_pagetable_walk_p4d() because pgd_leaf() returns boolean value.

---

Signature · 3 lines
Cheers,

David / dhildenb
Reviewed-by on v2 5/7
#23 lore raw

Re: [PATCH v2 5/7] x86/mm: remove pgd_leaf definition in arch

In reply to Baoquan He
Quoted 8 lines · Baoquan He

pgd huge page is not supported yet, let's use the generic definition in linux/pgtable.h.

And also update the BUILD_BUG_ON() checking for pgd_leaf() in pti_user_pagetable_walk_p4d() because pgd_leaf() returns boolean value.

I have been carrying a sort of this patch in my tree as well for quite some time now, and I think that Christophe also sent it some time ago, so glad someone finally pushed it

https://patchwork.kernel.org/project/linux-mm/patch/[email protected]/

Signature · 2 lines
Oscar Salvador
SUSE Labs
author
#24 lore raw

Re: [PATCH v2 5/7] x86/mm: remove pgd_leaf definition in arch

Quoted 7 lines · Oscar Salvador
Quoted 6 lines · Baoquan He

pgd huge page is not supported yet, let's use the generic definition in linux/pgtable.h.

And also update the BUILD_BUG_ON() checking for pgd_leaf() in pti_user_pagetable_walk_p4d() because pgd_leaf() returns boolean value.

Quoted 8 lines
Quoted 2 lines

I have been carrying a sort of this patch in my tree as well for quite some time now, and I think that Christophe also sent it some time ago, so glad someone finally pushed it

https://patchwork.kernel.org/project/linux-mm/patch/[email protected]/

Oops, I even commented in Christophe's patch thread to ask why the patchset hasn't been merged yet. Later when I focused on mm/gup.c code reading, I forgot that posting completely. Hope you don't mind I grab you and Christophe's credit since Ingo has picked it into x86/tip tree.

[PATCH v3 1/5] arch/x86: Drop own definition of pgd,p4d_leaf

Quoted 8 lines
Signature · 2 lines
Oscar Salvador
SUSE Labs
merge bot Applied
#25 lore raw

[tip: x86/mm] x86/mm: Simplify the pgd_leaf() and p4d_leaf() checks a bit

In reply to Baoquan He
The following commit has been merged into the x86/mm branch of tip:

The following commit has been merged into the x86/mm branch of tip:

Commit-ID:     4a490be3cb59c71dc84a363a251d466e2886ecd2
Gitweb:        https://git.kernel.org/tip/4a490be3cb59c71dc84a363a251d466e2886ecd2
Author:        Baoquan He <[email protected]>
AuthorDate:    Mon, 31 Mar 2025 16:13:25 +08:00
Committer:     Ingo Molnar <[email protected]>
CommitterDate: Tue, 01 Apr 2025 22:47:02 +02:00

x86/mm: Simplify the pgd_leaf() and p4d_leaf() checks a bit

The functions return bool, simplify the checks.

[ mingo: Split off from two other patches. ]

---
 arch/x86/mm/pti.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
arch/x86/mm/pti.c +2 -2
index 5f0d579..1902998 100644
@@ -185,7 +185,7 @@ static p4d_t *pti_user_pagetable_walk_p4d(unsigned long address)
 
 		set_pgd(pgd, __pgd(_KERNPG_TABLE | __pa(new_p4d_page)));
 	}
-	BUILD_BUG_ON(pgd_leaf(*pgd) != 0);
+	BUILD_BUG_ON(pgd_leaf(*pgd));
 
 	return p4d_offset(pgd, address);
 }
@@ -206,7 +206,7 @@ static pmd_t *pti_user_pagetable_walk_pmd(unsigned long address)
 	if (!p4d)
 		return NULL;
 
-	BUILD_BUG_ON(p4d_leaf(*p4d) != 0);
+	BUILD_BUG_ON(p4d_leaf(*p4d));
 	if (p4d_none(*p4d)) {
 		unsigned long new_pud_page = __get_free_page(gfp);
 		if (WARN_ON_ONCE(!new_pud_page))
merge bot Applied
#26 lore raw

[tip: x86/mm] x86/mm: Remove the arch-specific pgd_leaf() definition

In reply to Baoquan He
The following commit has been merged into the x86/mm branch of tip:

The following commit has been merged into the x86/mm branch of tip:

Commit-ID:     b0510ac74e189442dde8799c1b212bd106f2300c
Gitweb:        https://git.kernel.org/tip/b0510ac74e189442dde8799c1b212bd106f2300c
Author:        Baoquan He <[email protected]>
AuthorDate:    Mon, 31 Mar 2025 16:13:25 +08:00
Committer:     Ingo Molnar <[email protected]>
CommitterDate: Tue, 01 Apr 2025 22:46:51 +02:00

x86/mm: Remove the arch-specific pgd_leaf() definition

PGD huge pages are not supported yet, let's use the generic definition in <linux/pgtable.h>.

[ mingo: Cleaned up the changelog. ]

---
 arch/x86/include/asm/pgtable.h | 3 ---
 1 file changed, 3 deletions(-)
arch/x86/include/asm/pgtable.h +0 -3
index 7bd6bd6..5f4fcc0 100644
@@ -1472,9 +1472,6 @@ static inline bool pgdp_maps_userspace(void *__ptr)
 	return (((ptr & ~PAGE_MASK) / sizeof(pgd_t)) < PGD_KERNEL_START);
 }
 
-#define pgd_leaf	pgd_leaf
-static inline bool pgd_leaf(pgd_t pgd) { return false; }
-
 #ifdef CONFIG_MITIGATION_PAGE_TABLE_ISOLATION
 /*
  * All top-level MITIGATION_PAGE_TABLE_ISOLATION page tables are order-1 pages
merge bot Applied
#27 lore raw

[tip: x86/mm] x86/mm: Simplify the pgd_leaf() and p4d_leaf() checks a bit

In reply to Baoquan He
The following commit has been merged into the x86/mm branch of tip:

The following commit has been merged into the x86/mm branch of tip:

Commit-ID:     2b00d9031e42eabc8d32847d231ef48b8be0373d
Gitweb:        https://git.kernel.org/tip/2b00d9031e42eabc8d32847d231ef48b8be0373d
Author:        Baoquan He <[email protected]>
AuthorDate:    Mon, 31 Mar 2025 16:13:25 +08:00
Committer:     Ingo Molnar <[email protected]>
CommitterDate: Tue, 01 Apr 2025 22:48:56 +02:00

x86/mm: Simplify the pgd_leaf() and p4d_leaf() checks a bit

The functions return bool, simplify the checks.

[ mingo: Split off from two other patches. ]

---
 arch/x86/mm/pti.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
arch/x86/mm/pti.c +2 -2
index 5f0d579..1902998 100644
@@ -185,7 +185,7 @@ static p4d_t *pti_user_pagetable_walk_p4d(unsigned long address)
 
 		set_pgd(pgd, __pgd(_KERNPG_TABLE | __pa(new_p4d_page)));
 	}
-	BUILD_BUG_ON(pgd_leaf(*pgd) != 0);
+	BUILD_BUG_ON(pgd_leaf(*pgd));
 
 	return p4d_offset(pgd, address);
 }
@@ -206,7 +206,7 @@ static pmd_t *pti_user_pagetable_walk_pmd(unsigned long address)
 	if (!p4d)
 		return NULL;
 
-	BUILD_BUG_ON(p4d_leaf(*p4d) != 0);
+	BUILD_BUG_ON(p4d_leaf(*p4d));
 	if (p4d_none(*p4d)) {
 		unsigned long new_pud_page = __get_free_page(gfp);
 		if (WARN_ON_ONCE(!new_pud_page))
author
#28 lore raw

PATCH v2 6/7 x86/mm: remove p4d_leaf definition

In reply to Baoquan He

There's no p4d huge page support yet, let's use the generic definition.

And also update the BUILD_BUG_ON() in pti_user_pagetable_walk_pmd() because p4d_leaf() returns boolean value.

---
 arch/x86/include/asm/pgtable.h | 7 -------
 arch/x86/mm/pti.c              | 2 +-
 2 files changed, 1 insertion(+), 8 deletions(-)
arch/x86/include/asm/pgtable.h +0 -7
index 5f4fcc0eea17..5ddba366d3b4 100644
@@ -292,13 +292,6 @@ static inline unsigned long pgd_pfn(pgd_t pgd)
 	return (pgd_val(pgd) & PTE_PFN_MASK) >> PAGE_SHIFT;
 }
 
-#define p4d_leaf p4d_leaf
-static inline bool p4d_leaf(p4d_t p4d)
-{
-	/* No 512 GiB pages yet */
-	return 0;
-}
-
 #define pte_page(pte)	pfn_to_page(pte_pfn(pte))
 
 #define pmd_leaf pmd_leaf
arch/x86/mm/pti.c +1 -1
index c2e1de40136f..190299834011 100644
@@ -206,7 +206,7 @@ static pmd_t *pti_user_pagetable_walk_pmd(unsigned long address)
 	if (!p4d)
 		return NULL;
 
-	BUILD_BUG_ON(p4d_leaf(*p4d) != 0);
+	BUILD_BUG_ON(p4d_leaf(*p4d));
 	if (p4d_none(*p4d)) {
 		unsigned long new_pud_page = __get_free_page(gfp);
 		if (WARN_ON_ONCE(!new_pud_page))
Signature · 1 line
2.41.0
#29 lore raw

Re: [PATCH v2 6/7] x86/mm: remove p4d_leaf definition

arch/x86/include/asm/pgtable.h static inline unsigned long pgd_pfn(pgd_t pgd) · line 300

Quoted 12 lines · Baoquan He

There's no p4d huge page support yet, let's use the generic definition.

And also update the BUILD_BUG_ON() in pti_user_pagetable_walk_pmd() because p4d_leaf() returns boolean value.

arch/x86/include/asm/pgtable.h +0 -6
-#define p4d_leaf p4d_leaf
-static inline bool p4d_leaf(p4d_t p4d)
-{
-	/* No 512 GiB pages yet */
-	return 0;
-}

This comment was also incorrect I believe:

1 PTE entry on x86-64 covers 4K virtual memory, 512 PTE entries make up a 4K pagetable page, and each level of paging adds another level of 512 pagetable entries:

- level 0: 4K pages
- level 1: 512x 4K = 2MB 'large' pages
- level 2: 512x 2MB = 1GB 'huge' pages
- level 3: 512x 1GB = 512GB 'PGD' pages
- level 4: 512x 512GB = 256TB 'P4D' pages

So the above comment should have said '256 TB' pages, unless there's some naming weirdness I missed.

Thanks,

Ingo

author
#30 lore raw

Re: [PATCH v2 6/7] x86/mm: remove p4d_leaf definition

Quoted 7 lines · Ingo Molnar
Quoted 4 lines · Baoquan He

There's no p4d huge page support yet, let's use the generic definition.

And also update the BUILD_BUG_ON() in pti_user_pagetable_walk_pmd() because p4d_leaf() returns boolean value.

Thanks a lot for cleaning up the patch logs and rearranging x86 patches into tip tree.

Quoted 15 lines

arch/x86/include/asm/pgtable.h static inline unsigned long pgd_pfn(pgd_t pgd) · line 300

Quoted 8 lines
arch/x86/include/asm/pgtable.h +0 -6
-#define p4d_leaf p4d_leaf
-static inline bool p4d_leaf(p4d_t p4d)
-{
-	/* No 512 GiB pages yet */
-	return 0;
-}

This comment was also incorrect I believe:

1 PTE entry on x86-64 covers 4K virtual memory, 512 PTE entries make up a 4K pagetable page, and each level of paging adds another level of 512 pagetable entries:

Quoted 8 lines

- level 0: 4K pages
- level 1: 512x 4K = 2MB 'large' pages
- level 2: 512x 2MB = 1GB 'huge' pages
- level 3: 512x 1GB = 512GB 'PGD' pages
- level 4: 512x 512GB = 256TB 'P4D' pages

So the above comment should have said '256 TB' pages, unless there's some naming weirdness I missed.

Hmm, there could be misunderstanding here. In 5-level paging, PGD is the highest level, P4D is the next level of PGD. You may have reversed their order. So one P4D entry maps one PUD table which is 512 x 1GB. Then the mentioned comment seems correct to me.

#31 lore raw

Re: [PATCH v2 6/7] x86/mm: remove p4d_leaf definition

Quoted 6 lines · Baoquan He
Quoted 2 lines

So the above comment should have said '256 TB' pages, unless there's some naming weirdness I missed.

Hmm, there could be misunderstanding here. In 5-level paging, PGD is the highest level, P4D is the next level of PGD. You may have reversed their order.

Erm, yes indeed I flipped those two, so the correct table should be:

- level 0: 4K pages
- level 1: 512x 4K = 2MB 'large' pages
- level 2: 512x 2MB = 1GB 'huge' pages
- level 3: 512x 1GB = 512GB 'P4D' pages
- level 4: 512x 512GB = 256TB 'PGD' pages

I'm wondering whether 512GB pages will be called 'terapages'. ;-)

Thanks,

Ingo

author
#32 lore raw

Re: [PATCH v2 6/7] x86/mm: remove p4d_leaf definition

Quoted 12 lines · Ingo Molnar
Quoted 6 lines · Baoquan He
Quoted 2 lines

So the above comment should have said '256 TB' pages, unless there's some naming weirdness I missed.

Hmm, there could be misunderstanding here. In 5-level paging, PGD is the highest level, P4D is the next level of PGD. You may have reversed their order.

Erm, yes indeed I flipped those two, so the correct table should be:

Quoted 7 lines

- level 0: 4K pages
- level 1: 512x 4K = 2MB 'large' pages
- level 2: 512x 2MB = 1GB 'huge' pages
- level 3: 512x 1GB = 512GB 'P4D' pages
- level 4: 512x 512GB = 256TB 'PGD' pages

I'm wondering whether 512GB pages will be called 'terapages'. ;-)

Forgot replying to this one.

With my understanding, MM usually don't name large page of different size a specific name, just call them hugepage + size, e.g 2M huge page, or 1G huge page. Sometime when we build mapping for them, we will call them PMD hugepage or PUD hugepage.

I remember in x86 ARCH direct mapping is built with invocation of init_mem_mapping(), and the mapping to 2M or 1G directly is called large page mapping or PMD/PUD huge page mapping. They are similar.

Hope other MM people can correct me if I am wrong.

merge bot Applied
#33 lore raw

[tip: x86/mm] x86/mm: Remove the arch-specific p4d_leaf() definition

In reply to Baoquan He
The following commit has been merged into the x86/mm branch of tip:

The following commit has been merged into the x86/mm branch of tip:

Commit-ID:     3532c1f79ffcc57d14b8387a67b01797cc0e8be0
Gitweb:        https://git.kernel.org/tip/3532c1f79ffcc57d14b8387a67b01797cc0e8be0
Author:        Baoquan He <[email protected]>
AuthorDate:    Mon, 31 Mar 2025 16:13:26 +08:00
Committer:     Ingo Molnar <[email protected]>
CommitterDate: Mon, 31 Mar 2025 12:08:17 +02:00

x86/mm: Remove the arch-specific p4d_leaf() definition

P4D huge pages are not supported yet, let's use the generic definition in <linux/pgtable.h>.

[ mingo: Cleaned up the changelog. ]

---
 arch/x86/include/asm/pgtable.h | 7 -------
 1 file changed, 7 deletions(-)
arch/x86/include/asm/pgtable.h +0 -7
index 5f4fcc0..5ddba36 100644
@@ -292,13 +292,6 @@ static inline unsigned long pgd_pfn(pgd_t pgd)
 	return (pgd_val(pgd) & PTE_PFN_MASK) >> PAGE_SHIFT;
 }
 
-#define p4d_leaf p4d_leaf
-static inline bool p4d_leaf(p4d_t p4d)
-{
-	/* No 512 GiB pages yet */
-	return 0;
-}
-
 #define pte_page(pte)	pfn_to_page(pte_pfn(pte))
 
 #define pmd_leaf pmd_leaf
Reviewed-by on v2 6/7
#34 lore raw

Re: [PATCH v2 6/7] x86/mm: remove p4d_leaf definition

In reply to Baoquan He
Quoted 7 lines · Baoquan He

There's no p4d huge page support yet, let's use the generic definition.

And also update the BUILD_BUG_ON() in pti_user_pagetable_walk_pmd() because p4d_leaf() returns boolean value.

Signature · 2 lines
Oscar Salvador
SUSE Labs
merge bot Applied
#35 lore raw

[tip: x86/mm] x86/mm: Remove the arch-specific p4d_leaf() definition

In reply to Baoquan He
The following commit has been merged into the x86/mm branch of tip:

The following commit has been merged into the x86/mm branch of tip:

Commit-ID:     06e6e99da66cfca1ebac52c09c3a025ef5ccf959
Gitweb:        https://git.kernel.org/tip/06e6e99da66cfca1ebac52c09c3a025ef5ccf959
Author:        Baoquan He <[email protected]>
AuthorDate:    Mon, 31 Mar 2025 16:13:26 +08:00
Committer:     Ingo Molnar <[email protected]>
CommitterDate: Tue, 01 Apr 2025 22:47:02 +02:00

x86/mm: Remove the arch-specific p4d_leaf() definition

P4D huge pages are not supported yet, let's use the generic definition in <linux/pgtable.h>.

[ mingo: Cleaned up the changelog. ]

---
 arch/x86/include/asm/pgtable.h | 7 -------
 1 file changed, 7 deletions(-)
arch/x86/include/asm/pgtable.h +0 -7
index 5f4fcc0..5ddba36 100644
@@ -292,13 +292,6 @@ static inline unsigned long pgd_pfn(pgd_t pgd)
 	return (pgd_val(pgd) & PTE_PFN_MASK) >> PAGE_SHIFT;
 }
 
-#define p4d_leaf p4d_leaf
-static inline bool p4d_leaf(p4d_t p4d)
-{
-	/* No 512 GiB pages yet */
-	return 0;
-}
-
 #define pte_page(pte)	pfn_to_page(pte_pfn(pte))
 
 #define pmd_leaf pmd_leaf
merge bot Applied
#36 lore raw

[tip: x86/mm] x86/mm: Remove the arch-specific p4d_leaf() definition

In reply to Baoquan He
The following commit has been merged into the x86/mm branch of tip:

The following commit has been merged into the x86/mm branch of tip:

Commit-ID:     c083eff324edd73eb23f4bd3f40f388a3e7c2cd2
Gitweb:        https://git.kernel.org/tip/c083eff324edd73eb23f4bd3f40f388a3e7c2cd2
Author:        Baoquan He <[email protected]>
AuthorDate:    Mon, 31 Mar 2025 16:13:26 +08:00
Committer:     Ingo Molnar <[email protected]>
CommitterDate: Tue, 01 Apr 2025 22:48:51 +02:00

x86/mm: Remove the arch-specific p4d_leaf() definition

P4D huge pages are not supported yet, let's use the generic definition in <linux/pgtable.h>.

[ mingo: Cleaned up the changelog. ]

---
 arch/x86/include/asm/pgtable.h | 7 -------
 1 file changed, 7 deletions(-)
arch/x86/include/asm/pgtable.h +0 -7
index 5f4fcc0..5ddba36 100644
@@ -292,13 +292,6 @@ static inline unsigned long pgd_pfn(pgd_t pgd)
 	return (pgd_val(pgd) & PTE_PFN_MASK) >> PAGE_SHIFT;
 }
 
-#define p4d_leaf p4d_leaf
-static inline bool p4d_leaf(p4d_t p4d)
-{
-	/* No 512 GiB pages yet */
-	return 0;
-}
-
 #define pte_page(pte)	pfn_to_page(pte_pfn(pte))
 
 #define pmd_leaf pmd_leaf
author
#37 lore raw

PATCH v2 7/7 mm/pgtable: remove unneeded pgd_devmap()

In reply to Baoquan He

There's no user of pgd_devmap() now, remove it from all ARCH-es and linux/pgtable.h.

---
 arch/arm64/include/asm/pgtable.h             | 5 -----
 arch/loongarch/include/asm/pgtable.h         | 1 -
 arch/powerpc/include/asm/book3s/64/pgtable.h | 5 -----
 arch/riscv/include/asm/pgtable-64.h          | 5 -----
 arch/x86/include/asm/pgtable.h               | 5 -----
 include/linux/pgtable.h                      | 4 ----
 6 files changed, 25 deletions(-)
arch/arm64/include/asm/pgtable.h +0 -5
index 84f05f781a70..e0cab581edc9 100644
@@ -1230,11 +1230,6 @@ static inline int pud_devmap(pud_t pud)
 {
 	return 0;
 }
-
-static inline int pgd_devmap(pgd_t pgd)
-{
-	return 0;
-}
 #endif
 
 #ifdef CONFIG_PAGE_TABLE_CHECK
arch/loongarch/include/asm/pgtable.h +0 -1
index da346733a1da..d9b04296d9f5 100644
@@ -615,7 +615,6 @@ static inline long pmd_protnone(pmd_t pmd)
 
 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
 #define pud_devmap(pud)		(0)
-#define pgd_devmap(pgd)		(0)
 #endif /* CONFIG_TRANSPARENT_HUGEPAGE */
 
 /*
arch/powerpc/include/asm/book3s/64/pgtable.h +0 -5
index 6d98e6f08d4d..0da1c8d7f778 100644
@@ -1405,11 +1405,6 @@ static inline int pud_devmap(pud_t pud)
 {
 	return pte_devmap(pud_pte(pud));
 }
-
-static inline int pgd_devmap(pgd_t pgd)
-{
-	return 0;
-}
 #endif /* CONFIG_TRANSPARENT_HUGEPAGE */
 
 #define __HAVE_ARCH_PTEP_MODIFY_PROT_TRANSACTION
arch/riscv/include/asm/pgtable-64.h +0 -5
index 0897dd99ab8d..c6da4c8354a3 100644
@@ -411,11 +411,6 @@ static inline int pud_devmap(pud_t pud)
 {
 	return 0;
 }
-
-static inline int pgd_devmap(pgd_t pgd)
-{
-	return 0;
-}
 #endif
 
 #endif /* _ASM_RISCV_PGTABLE_64_H */
arch/x86/include/asm/pgtable.h +0 -5
index 5ddba366d3b4..3d6e78af525a 100644
@@ -361,11 +361,6 @@ static inline pud_t pud_mkspecial(pud_t pud)
 	return pud_set_flags(pud, _PAGE_SPECIAL);
 }
 #endif	/* CONFIG_ARCH_SUPPORTS_PUD_PFNMAP */
-
-static inline int pgd_devmap(pgd_t pgd)
-{
-	return 0;
-}
 #endif
 #endif /* CONFIG_TRANSPARENT_HUGEPAGE */
 
include/linux/pgtable.h +0 -4
index 94d267d02372..9d2dd5f71443 100644
@@ -1615,10 +1615,6 @@ static inline int pud_devmap(pud_t pud)
 {
 	return 0;
 }
-static inline int pgd_devmap(pgd_t pgd)
-{
-	return 0;
-}
 #endif
 
 #if !defined(CONFIG_TRANSPARENT_HUGEPAGE) || \
Signature · 1 line
2.41.0
author
#39 lore raw

Re: [PATCH v2 7/7] mm/pgtable: remove unneeded pgd_devmap()

Quoted 7 lines · David Hildenbrand
Quoted 2 lines · Baoquan He

There's no user of pgd_devmap() now, remove it from all ARCH-es and linux/pgtable.h.

Drop this patch, Alistair will remove all that devmap stuff soon.

https://lore.kernel.org/linux-mm/42a318bcbb65931958e52ce4b1334f3d012cbd6f.1736488799.git-series.apopple@nvidia.com/

Thanks for telling, will drop it.

Quoted 6 lines
Signature · 4 lines
-- 
Cheers,

David / dhildenb