Skip to contentSkip to conversation

mm/hugetlb: Refactor unmap_ref_private() to take folio instead of page

PATCH Superseded [email protected] 5 participants 16 messages Apr 18, 2025 – Apr 25, 2025

Subsystems (2)

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/4 mm/hugetlb: Refactor unmap_ref_private() to take folio instead of page

From: Fan Ni <[email protected]>

The function unmap_ref_private() has only user, which passes in &folio->page. Let it take folio directly.

---
v2:
Picked up Reviewed-by tags;

v1:
https://lore.kernel.org/linux-mm/aAHUluy7T32ZlYg7@debian/T/#mbf9f3e8b49497755b414e1887b2376b3902ffb76

---
 mm/hugetlb.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
mm/hugetlb.c +4 -4
index ccc4f08f8481..b5d1ac8290a7 100644
@@ -6064,7 +6064,7 @@ void unmap_hugepage_range(struct vm_area_struct *vma, unsigned long start,
  * same region.
  */
 static void unmap_ref_private(struct mm_struct *mm, struct vm_area_struct *vma,
-			      struct page *page, unsigned long address)
+			      struct folio *folio, unsigned long address)
 {
 	struct hstate *h = hstate_vma(vma);
 	struct vm_area_struct *iter_vma;
@@ -6108,7 +6108,8 @@ static void unmap_ref_private(struct mm_struct *mm, struct vm_area_struct *vma,
 		 */
 		if (!is_vma_resv_set(iter_vma, HPAGE_RESV_OWNER))
 			unmap_hugepage_range(iter_vma, address,
-					     address + huge_page_size(h), page, 0);
+					     address + huge_page_size(h),
+					     folio_page(folio, 0), 0);
 	}
 	i_mmap_unlock_write(mapping);
 }
@@ -6231,8 +6232,7 @@ static vm_fault_t hugetlb_wp(struct folio *pagecache_folio,
 			hugetlb_vma_unlock_read(vma);
 			mutex_unlock(&hugetlb_fault_mutex_table[hash]);
 
-			unmap_ref_private(mm, vma, &old_folio->page,
-					vmf->address);
+			unmap_ref_private(mm, vma, old_folio, vmf->address);
 
 			mutex_lock(&hugetlb_fault_mutex_table[hash]);
 			hugetlb_vma_lock_read(vma);
Signature · 1 line
2.47.2
#1 lore raw

PATCH v2 2/4 mm/hugetlb: Refactor unmap_hugepage_range() to take folio instead of page

From: Fan Ni <[email protected]>

The function unmap_hugepage_range() has two kinds of users:
1) unmap_ref_private(), which passes in the head page of a folio. Since unmap_ref_private() already takes folio and there are no other uses of the folio struct in the function, it is natural for unmap_hugepage_range() to take folio also.
2) All other uses, which pass in NULL pointer.

In both cases, we can pass in folio. Refactor unmap_hugepage_range() to take folio.

---
v2:
Picked up Reviewed-by tags;

v1:
https://lore.kernel.org/linux-mm/aAHUluy7T32ZlYg7@debian/T/#m7ba630d783e08ae708453279579093d481c6e721

---
 include/linux/hugetlb.h | 2 +-
 mm/hugetlb.c            | 7 ++++---
 2 files changed, 5 insertions(+), 4 deletions(-)
include/linux/hugetlb.h +1 -1
index a57bed83c657..b7699f35c87f 100644
@@ -128,7 +128,7 @@ int move_hugetlb_page_tables(struct vm_area_struct *vma,
 int copy_hugetlb_page_range(struct mm_struct *, struct mm_struct *,
 			    struct vm_area_struct *, struct vm_area_struct *);
 void unmap_hugepage_range(struct vm_area_struct *,
-			  unsigned long, unsigned long, struct page *,
+			  unsigned long, unsigned long, struct folio *folio,
 			  zap_flags_t);
 void __unmap_hugepage_range(struct mmu_gather *tlb,
 			  struct vm_area_struct *vma,
mm/hugetlb.c +4 -3
index b5d1ac8290a7..3181dbe0c4bb 100644
@@ -6039,7 +6039,7 @@ void __hugetlb_zap_end(struct vm_area_struct *vma,
 }
 
 void unmap_hugepage_range(struct vm_area_struct *vma, unsigned long start,
-			  unsigned long end, struct page *ref_page,
+			  unsigned long end, struct folio *ref_folio,
 			  zap_flags_t zap_flags)
 {
 	struct mmu_notifier_range range;
@@ -6051,7 +6051,8 @@ void unmap_hugepage_range(struct vm_area_struct *vma, unsigned long start,
 	mmu_notifier_invalidate_range_start(&range);
 	tlb_gather_mmu(&tlb, vma->vm_mm);
 
-	__unmap_hugepage_range(&tlb, vma, start, end, ref_page, zap_flags);
+	__unmap_hugepage_range(&tlb, vma, start, end,
+			       folio_page(ref_folio, 0), zap_flags);
 
 	mmu_notifier_invalidate_range_end(&range);
 	tlb_finish_mmu(&tlb);
@@ -6109,7 +6110,7 @@ static void unmap_ref_private(struct mm_struct *mm, struct vm_area_struct *vma,
 		if (!is_vma_resv_set(iter_vma, HPAGE_RESV_OWNER))
 			unmap_hugepage_range(iter_vma, address,
 					     address + huge_page_size(h),
-					     folio_page(folio, 0), 0);
+					     folio, 0);
 	}
 	i_mmap_unlock_write(mapping);
 }
Signature · 1 line
2.47.2
Acked-by on v2 2/4
#2 lore raw

Re: [PATCH v2 2/4] mm/hugetlb: Refactor unmap_hugepage_range() to take folio instead of page

Quoted 31 lines · On 18.04.25 18:57, [email protected] wrote:

From: Fan Ni <[email protected]>

The function unmap_hugepage_range() has two kinds of users:
1) unmap_ref_private(), which passes in the head page of a folio. Since unmap_ref_private() already takes folio and there are no other uses of the folio struct in the function, it is natural for unmap_hugepage_range() to take folio also.
2) All other uses, which pass in NULL pointer.

In both cases, we can pass in folio. Refactor unmap_hugepage_range() to take folio.

---
v2:
Picked up Reviewed-by tags;

v1:
https://lore.kernel.org/linux-mm/aAHUluy7T32ZlYg7@debian/T/#m7ba630d783e08ae708453279579093d481c6e721

---
  include/linux/hugetlb.h | 2 +-
  mm/hugetlb.c            | 7 ++++---
  2 files changed, 5 insertions(+), 4 deletions(-)
include/linux/hugetlb.h +0 -0
index a57bed83c657..b7699f35c87f 100644

include/linux/hugetlb.h int move_hugetlb_page_tables(struct vm_area_struct *vma, · line 132

Quoted 6 lines
include/linux/hugetlb.h +1 -1
@@ -128,7 +128,7 @@ int move_hugetlb_page_tables(struct vm_area_struct *vma,
  int copy_hugetlb_page_range(struct mm_struct *, struct mm_struct *,
  			    struct vm_area_struct *, struct vm_area_struct *);
  void unmap_hugepage_range(struct vm_area_struct *,
-			  unsigned long, unsigned long, struct page *,
+			  unsigned long, unsigned long, struct folio *folio,
  			  zap_flags_t);

Note that no parameters are using names. While I am not a big fan of that in general, we should keep it consistent.

Either name all or none.

Signature · 3 lines
Cheers,

David / dhildenb
#3 lore raw

Re: [PATCH v2 2/4] mm/hugetlb: Refactor unmap_hugepage_range() to take folio instead of page

In reply to [email protected]
Quoted 44 lines · On 18.04.25 18:57, [email protected] wrote:

From: Fan Ni <[email protected]>

The function unmap_hugepage_range() has two kinds of users:
1) unmap_ref_private(), which passes in the head page of a folio. Since unmap_ref_private() already takes folio and there are no other uses of the folio struct in the function, it is natural for unmap_hugepage_range() to take folio also.
2) All other uses, which pass in NULL pointer.

In both cases, we can pass in folio. Refactor unmap_hugepage_range() to take folio.

---
v2:
Picked up Reviewed-by tags;

v1:
https://lore.kernel.org/linux-mm/aAHUluy7T32ZlYg7@debian/T/#m7ba630d783e08ae708453279579093d481c6e721

---
  include/linux/hugetlb.h | 2 +-
  mm/hugetlb.c            | 7 ++++---
  2 files changed, 5 insertions(+), 4 deletions(-)
include/linux/hugetlb.h +1 -1
index a57bed83c657..b7699f35c87f 100644
@@ -128,7 +128,7 @@ int move_hugetlb_page_tables(struct vm_area_struct *vma,
  int copy_hugetlb_page_range(struct mm_struct *, struct mm_struct *,
  			    struct vm_area_struct *, struct vm_area_struct *);
  void unmap_hugepage_range(struct vm_area_struct *,
-			  unsigned long, unsigned long, struct page *,
+			  unsigned long, unsigned long, struct folio *folio,
  			  zap_flags_t);
  void __unmap_hugepage_range(struct mmu_gather *tlb,
  			  struct vm_area_struct *vma,
mm/hugetlb.c +0 -0
index b5d1ac8290a7..3181dbe0c4bb 100644

mm/hugetlb.c void __hugetlb_zap_end(struct vm_area_struct *vma, · line 6043

Quoted 6 lines
mm/hugetlb.c +1 -1
@@ -6039,7 +6039,7 @@ void __hugetlb_zap_end(struct vm_area_struct *vma,
  }
 
  void unmap_hugepage_range(struct vm_area_struct *vma, unsigned long start,
-			  unsigned long end, struct page *ref_page,
+			  unsigned long end, struct folio *ref_folio,
  			  zap_flags_t zap_flags)

Oh, and now I realize that you call it "folio" above and "ref_folio" here.

Can we just rename it to "folio" consistently or is there a reason to use the weird "ref_" prefix?

Signature · 3 lines
Cheers,

David / dhildenb
#4 lore raw

Re: [PATCH v2 2/4] mm/hugetlb: Refactor unmap_hugepage_range() to take folio instead of page

In reply to [email protected]

include/linux/hugetlb.h int move_hugetlb_page_tables(struct vm_area_struct *vma, · line 131

Quoted 3 lines · On Fri, Apr 18, 2025 at 09:57:40AM -0700, [email protected] wrote:

void unmap_hugepage_range(struct vm_area_struct *,

include/linux/hugetlb.h +1 -1
-			  unsigned long, unsigned long, struct page *,
+			  unsigned long, unsigned long, struct folio *folio,

I'm fine with leaving the vma and folio unnamed, but it is a crime against our fellow programmers to leave the two 'unsigned long's unnamed. What the hell are they?

mm/hugetlb.c void __hugetlb_zap_end(struct vm_area_struct *vma, · line 6042

Quoted 3 lines

void unmap_hugepage_range(struct vm_area_struct *vma, unsigned long start,

mm/hugetlb.c +1 -1
-			  unsigned long end, struct page *ref_page,
+			  unsigned long end, struct folio *ref_folio,

... start and end. I'd happily see a patch which only named those parameters and left the struct folio unnamed.

mm/hugetlb.c void unmap_hugepage_range(struct vm_area_struct *vma, unsigned long start, · line 6055

Quoted 3 lines
mm/hugetlb.c +2 -1
-	__unmap_hugepage_range(&tlb, vma, start, end, ref_page, zap_flags);
+	__unmap_hugepage_range(&tlb, vma, start, end,
+			       folio_page(ref_folio, 0), zap_flags);

I do not like this. Why should we pass in the first page here? It seems to me that this is just "Now we will call a function which still takes a struct page", and we *SHOULD* use &folio->page here to indicate that we just haven't done the conversion yet.

#5 lore raw

PATCH v2 3/4 mm/hugetlb: Refactor __unmap_hugepage_range() to take folio instead of page

In reply to [email protected]

From: Fan Ni <[email protected]>

The function __unmap_hugepage_range() has two kinds of users:
1) unmap_hugepage_range(), which passes in the head page of a folio. Since unmap_hugepage_range() already takes folio and there are no other uses of the folio struct in the function, it is natural for __unmap_hugepage_range() to take folio also.
2) All other uses, which pass in NULL pointer.

In both cases, we can pass in folio. Refactor __unmap_hugepage_range() to take folio.

---
v2: fixed issue mentioned here:
https://lore.kernel.org/linux-mm/aAHUluy7T32ZlYg7@debian/T/#m2b9cc1743e1907e52658815b297b9d249474f387

v1:
https://lore.kernel.org/linux-mm/aAHUluy7T32ZlYg7@debian/T/#m738e9e7f7d7fe4aab6b21782b4658dd65abf8fc4
---

 include/linux/hugetlb.h |  4 ++--
 mm/hugetlb.c            | 10 +++++-----
 2 files changed, 7 insertions(+), 7 deletions(-)
include/linux/hugetlb.h +2 -2
index b7699f35c87f..ebaf95231934 100644
@@ -133,7 +133,7 @@ void unmap_hugepage_range(struct vm_area_struct *,
 void __unmap_hugepage_range(struct mmu_gather *tlb,
 			  struct vm_area_struct *vma,
 			  unsigned long start, unsigned long end,
-			  struct page *ref_page, zap_flags_t zap_flags);
+			  struct folio *ref_folio, zap_flags_t zap_flags);
 void hugetlb_report_meminfo(struct seq_file *);
 int hugetlb_report_node_meminfo(char *buf, int len, int nid);
 void hugetlb_show_meminfo_node(int nid);
@@ -452,7 +452,7 @@ static inline long hugetlb_change_protection(
 
 static inline void __unmap_hugepage_range(struct mmu_gather *tlb,
 			struct vm_area_struct *vma, unsigned long start,
-			unsigned long end, struct page *ref_page,
+			unsigned long end, struct folio *ref_folio,
 			zap_flags_t zap_flags)
 {
 	BUG();
mm/hugetlb.c +5 -5
index 3181dbe0c4bb..7d280ab23784 100644
@@ -5833,7 +5833,7 @@ int move_hugetlb_page_tables(struct vm_area_struct *vma,
 
 void __unmap_hugepage_range(struct mmu_gather *tlb, struct vm_area_struct *vma,
 			    unsigned long start, unsigned long end,
-			    struct page *ref_page, zap_flags_t zap_flags)
+			    struct folio *ref_folio, zap_flags_t zap_flags)
 {
 	struct mm_struct *mm = vma->vm_mm;
 	unsigned long address;
@@ -5910,8 +5910,8 @@ void __unmap_hugepage_range(struct mmu_gather *tlb, struct vm_area_struct *vma,
 		 * page is being unmapped, not a range. Ensure the page we
 		 * are about to unmap is the actual page of interest.
 		 */
-		if (ref_page) {
-			if (page != ref_page) {
+		if (ref_folio) {
+			if (page != folio_page(ref_folio, 0)) {
 				spin_unlock(ptl);
 				continue;
 			}
@@ -5977,7 +5977,7 @@ void __unmap_hugepage_range(struct mmu_gather *tlb, struct vm_area_struct *vma,
 		/*
 		 * Bail out after unmapping reference page if supplied
 		 */
-		if (ref_page)
+		if (ref_folio)
 			break;
 	}
 	tlb_end_vma(tlb, vma);
@@ -6052,7 +6052,7 @@ void unmap_hugepage_range(struct vm_area_struct *vma, unsigned long start,
 	tlb_gather_mmu(&tlb, vma->vm_mm);
 
 	__unmap_hugepage_range(&tlb, vma, start, end,
-			       folio_page(ref_folio, 0), zap_flags);
+			       ref_folio, zap_flags);
 
 	mmu_notifier_invalidate_range_end(&range);
 	tlb_finish_mmu(&tlb);
Signature · 1 line
2.47.2
#6 lore raw

Re: [PATCH v2 3/4] mm/hugetlb: Refactor __unmap_hugepage_range() to take folio instead of page

Quoted 39 lines · On 18.04.25 18:57, [email protected] wrote:

From: Fan Ni <[email protected]>

The function __unmap_hugepage_range() has two kinds of users:
1) unmap_hugepage_range(), which passes in the head page of a folio. Since unmap_hugepage_range() already takes folio and there are no other uses of the folio struct in the function, it is natural for __unmap_hugepage_range() to take folio also.
2) All other uses, which pass in NULL pointer.

In both cases, we can pass in folio. Refactor __unmap_hugepage_range() to take folio.

---
v2: fixed issue mentioned here:
https://lore.kernel.org/linux-mm/aAHUluy7T32ZlYg7@debian/T/#m2b9cc1743e1907e52658815b297b9d249474f387

v1:
https://lore.kernel.org/linux-mm/aAHUluy7T32ZlYg7@debian/T/#m738e9e7f7d7fe4aab6b21782b4658dd65abf8fc4
---

  include/linux/hugetlb.h |  4 ++--
  mm/hugetlb.c            | 10 +++++-----
  2 files changed, 7 insertions(+), 7 deletions(-)
include/linux/hugetlb.h +1 -1
index b7699f35c87f..ebaf95231934 100644
@@ -133,7 +133,7 @@ void unmap_hugepage_range(struct vm_area_struct *,
  void __unmap_hugepage_range(struct mmu_gather *tlb,
  			  struct vm_area_struct *vma,
  			  unsigned long start, unsigned long end,
-			  struct page *ref_page, zap_flags_t zap_flags);
+			  struct folio *ref_folio, zap_flags_t zap_flags);
  void hugetlb_report_meminfo(struct seq_file *);
  int hugetlb_report_node_meminfo(char *buf, int len, int nid);
  void hugetlb_show_meminfo_node(int nid);

include/linux/hugetlb.h static inline long hugetlb_change_protection( · line 457

Quoted 7 lines
include/linux/hugetlb.h +1 -1
@@ -452,7 +452,7 @@ static inline long hugetlb_change_protection(
 
  static inline void __unmap_hugepage_range(struct mmu_gather *tlb,
  			struct vm_area_struct *vma, unsigned long start,
-			unsigned long end, struct page *ref_page,
+			unsigned long end, struct folio *ref_folio,
  			zap_flags_t zap_flags)
  {

Same comment as for the previous patch, can we just call this "folio" instead of "ref_folio" if we are already touching it?

Signature · 3 lines
Cheers,

David / dhildenb
#7 lore raw

Re: [PATCH v2 3/4] mm/hugetlb: Refactor __unmap_hugepage_range() to take folio instead of page

In reply to [email protected]

mm/hugetlb.c void __unmap_hugepage_range(struct mmu_gather *tlb, struct vm_area_struct *vma, · line 5914

Quoted 8 lines · On Fri, Apr 18, 2025 at 09:57:41AM -0700, [email protected] wrote:
mm/hugetlb.c +2 -2
@@ -5910,8 +5910,8 @@ void __unmap_hugepage_range(struct mmu_gather *tlb, struct vm_area_struct *vma,
 		 * page is being unmapped, not a range. Ensure the page we
 		 * are about to unmap is the actual page of interest.
 		 */
-		if (ref_page) {
-			if (page != ref_page) {
+		if (ref_folio) {
+			if (page != folio_page(ref_folio, 0)) {

I think this is actually wrong. Shouldn't it be:

			if (page_folio(page) != ref_folio)

so that it doesn't matter which page in the folio we found; we just need to know if the page belongs to the same folio or not?

#8 lore raw

PATCH v2 4/4 mm/hugetlb: Convert use of struct page to folio in __unmap_hugepage_range()

In reply to [email protected]

From: Fan Ni <[email protected]>

In __unmap_hugepage_range(), the "page" pointer always points to the first page of a huge page, which guarantees there is a folio associating with it. Convert the "page" pointer to use folio.

---
This is a new patch added to the series based on the discussion here:
https://lore.kernel.org/linux-mm/aAHUluy7T32ZlYg7@debian/T/#m2b9cc1743e1907e52658815b297b9d249474f387

---
 mm/hugetlb.c | 18 +++++++++---------
 1 file changed, 9 insertions(+), 9 deletions(-)
mm/hugetlb.c +9 -9
index 7d280ab23784..8177a3fe47d7 100644
@@ -5840,7 +5840,7 @@ void __unmap_hugepage_range(struct mmu_gather *tlb, struct vm_area_struct *vma,
 	pte_t *ptep;
 	pte_t pte;
 	spinlock_t *ptl;
-	struct page *page;
+	struct folio *folio;
 	struct hstate *h = hstate_vma(vma);
 	unsigned long sz = huge_page_size(h);
 	bool adjust_reservation = false;
@@ -5904,14 +5904,14 @@ void __unmap_hugepage_range(struct mmu_gather *tlb, struct vm_area_struct *vma,
 			continue;
 		}
 
-		page = pte_page(pte);
+		folio = page_folio(pte_page(pte));
 		/*
 		 * If a reference page is supplied, it is because a specific
 		 * page is being unmapped, not a range. Ensure the page we
 		 * are about to unmap is the actual page of interest.
 		 */
 		if (ref_folio) {
-			if (page != folio_page(ref_folio, 0)) {
+			if (folio != ref_folio) {
 				spin_unlock(ptl);
 				continue;
 			}
@@ -5926,7 +5926,7 @@ void __unmap_hugepage_range(struct mmu_gather *tlb, struct vm_area_struct *vma,
 		pte = huge_ptep_get_and_clear(mm, address, ptep, sz);
 		tlb_remove_huge_tlb_entry(h, tlb, ptep, address);
 		if (huge_pte_dirty(pte))
-			set_page_dirty(page);
+			folio_mark_dirty(folio);
 		/* Leave a uffd-wp pte marker if needed */
 		if (huge_pte_uffd_wp(pte) &&
 		    !(zap_flags & ZAP_FLAG_DROP_MARKER))
@@ -5934,7 +5934,7 @@ void __unmap_hugepage_range(struct mmu_gather *tlb, struct vm_area_struct *vma,
 					make_pte_marker(PTE_MARKER_UFFD_WP),
 					sz);
 		hugetlb_count_sub(pages_per_huge_page(h), mm);
-		hugetlb_remove_rmap(page_folio(page));
+		hugetlb_remove_rmap(folio);
 
 		/*
 		 * Restore the reservation for anonymous page, otherwise the
@@ -5943,8 +5943,8 @@ void __unmap_hugepage_range(struct mmu_gather *tlb, struct vm_area_struct *vma,
 		 * reservation bit.
 		 */
 		if (!h->surplus_huge_pages && __vma_private_lock(vma) &&
-		    folio_test_anon(page_folio(page))) {
-			folio_set_hugetlb_restore_reserve(page_folio(page));
+		    folio_test_anon(folio)) {
+			folio_set_hugetlb_restore_reserve(folio);
 			/* Reservation to be adjusted after the spin lock */
 			adjust_reservation = true;
 		}
@@ -5968,12 +5968,12 @@ void __unmap_hugepage_range(struct mmu_gather *tlb, struct vm_area_struct *vma,
 				 * count will not be incremented by free_huge_folio.
 				 * Act as if we consumed the reservation.
 				 */
-				folio_clear_hugetlb_restore_reserve(page_folio(page));
+				folio_clear_hugetlb_restore_reserve(folio);
 			else if (rc)
 				vma_add_reservation(h, vma, address);
 		}
 
-		tlb_remove_page_size(tlb, page, huge_page_size(h));
+		tlb_remove_page_size(tlb, folio_page(folio, 0), huge_page_size(h));
 		/*
 		 * Bail out after unmapping reference page if supplied
 		 */
Signature · 1 line
2.47.2
Reviewed-by on v2 4/4
#9 lore raw

Re: [PATCH v2 4/4] mm/hugetlb: Convert use of struct page to folio in __unmap_hugepage_range()

Quoted 78 lines · On 4/18/25 12:57 PM, [email protected] wrote:

From: Fan Ni <[email protected]>

In __unmap_hugepage_range(), the "page" pointer always points to the first page of a huge page, which guarantees there is a folio associating with it. Convert the "page" pointer to use folio.

---
This is a new patch added to the series based on the discussion here:
https://lore.kernel.org/linux-mm/aAHUluy7T32ZlYg7@debian/T/#m2b9cc1743e1907e52658815b297b9d249474f387

---
  mm/hugetlb.c | 18 +++++++++---------
  1 file changed, 9 insertions(+), 9 deletions(-)
mm/hugetlb.c +7 -8
index 7d280ab23784..8177a3fe47d7 100644
@@ -5840,7 +5840,7 @@ void __unmap_hugepage_range(struct mmu_gather *tlb, struct vm_area_struct *vma,
  	pte_t *ptep;
  	pte_t pte;
  	spinlock_t *ptl;
-	struct page *page;
+	struct folio *folio;
  	struct hstate *h = hstate_vma(vma);
  	unsigned long sz = huge_page_size(h);
  	bool adjust_reservation = false;
@@ -5904,14 +5904,14 @@ void __unmap_hugepage_range(struct mmu_gather *tlb, struct vm_area_struct *vma,
  			continue;
  		}
 
-		page = pte_page(pte);
+		folio = page_folio(pte_page(pte));
  		/*
  		 * If a reference page is supplied, it is because a specific
  		 * page is being unmapped, not a range. Ensure the page we
  		 * are about to unmap is the actual page of interest.
  		 */
  		if (ref_folio) {
-			if (page != folio_page(ref_folio, 0)) {
+			if (folio != ref_folio) {
  				spin_unlock(ptl);
  				continue;
  			}
@@ -5926,7 +5926,7 @@ void __unmap_hugepage_range(struct mmu_gather *tlb, struct vm_area_struct *vma,
  		pte = huge_ptep_get_and_clear(mm, address, ptep, sz);
  		tlb_remove_huge_tlb_entry(h, tlb, ptep, address);
  		if (huge_pte_dirty(pte))
-			set_page_dirty(page);
+			folio_mark_dirty(folio);
  		/* Leave a uffd-wp pte marker if needed */
  		if (huge_pte_uffd_wp(pte) &&
  		    !(zap_flags & ZAP_FLAG_DROP_MARKER))
@@ -5934,7 +5934,7 @@ void __unmap_hugepage_range(struct mmu_gather *tlb, struct vm_area_struct *vma,
  					make_pte_marker(PTE_MARKER_UFFD_WP),
  					sz);
  		hugetlb_count_sub(pages_per_huge_page(h), mm);
-		hugetlb_remove_rmap(page_folio(page));
+		hugetlb_remove_rmap(folio);
 
  		/*
  		 * Restore the reservation for anonymous page, otherwise the
@@ -5943,8 +5943,8 @@ void __unmap_hugepage_range(struct mmu_gather *tlb, struct vm_area_struct *vma,
  		 * reservation bit.
  		 */
  		if (!h->surplus_huge_pages && __vma_private_lock(vma) &&
-		    folio_test_anon(page_folio(page))) {
-			folio_set_hugetlb_restore_reserve(page_folio(page));
+		    folio_test_anon(folio)) {
+			folio_set_hugetlb_restore_reserve(folio);
  			/* Reservation to be adjusted after the spin lock */
  			adjust_reservation = true;
  		}
@@ -5968,12 +5968,12 @@ void __unmap_hugepage_range(struct mmu_gather *tlb, struct vm_area_struct *vma,
  				 * count will not be incremented by free_huge_folio.
  				 * Act as if we consumed the reservation.
  				 */
-				folio_clear_hugetlb_restore_reserve(page_folio(page));

mm/hugetlb.c void __unmap_hugepage_range(struct mmu_gather *tlb, struct vm_area_struct *vma, · line 5979

Quoted 10 lines
mm/hugetlb.c +2 -1
+				folio_clear_hugetlb_restore_reserve(folio);
  			else if (rc)
  				vma_add_reservation(h, vma, address);
  		}
 
-		tlb_remove_page_size(tlb, page, huge_page_size(h));
+		tlb_remove_page_size(tlb, folio_page(folio, 0), huge_page_size(h));
  		/*
  		 * Bail out after unmapping reference page if supplied
  		 */
#10 lore raw

Re: [PATCH v2 4/4] mm/hugetlb: Convert use of struct page to folio in __unmap_hugepage_range()

In reply to [email protected]
Quoted 34 lines · On 18.04.25 18:57, [email protected] wrote:

From: Fan Ni <[email protected]>

In __unmap_hugepage_range(), the "page" pointer always points to the first page of a huge page, which guarantees there is a folio associating with it. Convert the "page" pointer to use folio.

---
This is a new patch added to the series based on the discussion here:
https://lore.kernel.org/linux-mm/aAHUluy7T32ZlYg7@debian/T/#m2b9cc1743e1907e52658815b297b9d249474f387

---
  mm/hugetlb.c | 18 +++++++++---------
  1 file changed, 9 insertions(+), 9 deletions(-)
mm/hugetlb.c +2 -2
index 7d280ab23784..8177a3fe47d7 100644
@@ -5840,7 +5840,7 @@ void __unmap_hugepage_range(struct mmu_gather *tlb, struct vm_area_struct *vma,
  	pte_t *ptep;
  	pte_t pte;
  	spinlock_t *ptl;
-	struct page *page;
+	struct folio *folio;
  	struct hstate *h = hstate_vma(vma);
  	unsigned long sz = huge_page_size(h);
  	bool adjust_reservation = false;
@@ -5904,14 +5904,14 @@ void __unmap_hugepage_range(struct mmu_gather *tlb, struct vm_area_struct *vma,
  			continue;
  		}
 
-		page = pte_page(pte);
+		folio = page_folio(pte_page(pte));
  		/*

mm/hugetlb.c void __unmap_hugepage_range(struct mmu_gather *tlb, struct vm_area_struct *vma, · line 5917

Quoted 10 lines
mm/hugetlb.c +1 -1
  		 * If a reference page is supplied, it is because a specific
  		 * page is being unmapped, not a range. Ensure the page we
  		 * are about to unmap is the actual page of interest.
  		 */
  		if (ref_folio) {
-			if (page != folio_page(ref_folio, 0)) {
+			if (folio != ref_folio) {
  				spin_unlock(ptl);
  				continue;
  			}

What about something like (keeping in mind that I suggest renaming "ref_folio" -> "folio" in previous patches)

const bool folio_provided = !!folio;

...

if (folio_provided) {
	if (folio != page_folio(pte_page(pte))) {
		spin_unlock(ptl);
		continue;
	}
	...
} else {
	folio = page_folio(pte_page(pte);
}

...

if (folio_supplied)

	break;

...

Signature · 3 lines
Cheers,

David / dhildenb
#11 lore raw

Re: [PATCH v2 4/4] mm/hugetlb: Convert use of struct page to folio in __unmap_hugepage_range()

In reply to [email protected]

mm/hugetlb.c void __unmap_hugepage_range(struct mmu_gather *tlb, struct vm_area_struct *vma, · line 5976

Quoted 3 lines · On Fri, Apr 18, 2025 at 09:57:42AM -0700, [email protected] wrote:
mm/hugetlb.c +1 -1
-		tlb_remove_page_size(tlb, page, huge_page_size(h));
+		tlb_remove_page_size(tlb, folio_page(folio, 0), huge_page_size(h));

Surely this should be:

		tlb_remove_page_size(tlb, folio_page(folio, 0), folio_size(folio));
maintainer
#12 lore raw

Re: [PATCH v2 4/4] mm/hugetlb: Convert use of struct page to folio in __unmap_hugepage_range()

Quoted 7 lines · On Wed, 23 Apr 2025 04:22:42 +0100 Matthew Wilcox <[email protected]> wrote:

mm/hugetlb.c void __unmap_hugepage_range(struct mmu_gather *tlb, struct vm_area_struct *vma, · line 5976

Quoted 3 lines · On Fri, Apr 18, 2025 at 09:57:42AM -0700, [email protected] wrote:
mm/hugetlb.c +1 -1
-		tlb_remove_page_size(tlb, page, huge_page_size(h));
+		tlb_remove_page_size(tlb, folio_page(folio, 0), huge_page_size(h));

Surely this should be:

		tlb_remove_page_size(tlb, folio_page(folio, 0), folio_size(folio));

Thanks, I removed this version of the series from mm.git's mm-new branch.

Acked-by on v2 1/4
#13 lore raw
In reply to [email protected]
Quoted 8 lines · On 18.04.25 18:57, [email protected] wrote:

From: Fan Ni <[email protected]>

The function unmap_ref_private() has only user, which passes in &folio->page. Let it take folio directly.

Quoted 7 lines

---
v2:
Picked up Reviewed-by tags;

v1:
https://lore.kernel.org/linux-mm/aAHUluy7T32ZlYg7@debian/T/#mbf9f3e8b49497755b414e1887b2376b3902ffb76
---

Signature · 3 lines
Cheers,

David / dhildenb
#14 lore raw
In reply to [email protected]

With regard to the Subject: line, you're not "refactoring". You're passing a folio to the function.

mm/hugetlb.c static void unmap_ref_private(struct mm_struct *mm, struct vm_area_struct *vma, · line 6112

Quoted 7 lines
mm/hugetlb.c +2 -1
@@ -6108,7 +6108,8 @@ static void unmap_ref_private(struct mm_struct *mm, struct vm_area_struct *vma,
 		 */
 		if (!is_vma_resv_set(iter_vma, HPAGE_RESV_OWNER))
 			unmap_hugepage_range(iter_vma, address,
-					     address + huge_page_size(h), page, 0);
+					     address + huge_page_size(h),
+					     folio_page(folio, 0), 0);

As previously, use &folio->page here.

author
#15 lore raw
Quoted 3 lines · Matthew Wilcox

With regard to the Subject: line, you're not "refactoring". You're passing a folio to the function.

Hi Matthew,

Thanks for the review.

Are you suggesting something like "Passing folio instead of page to unmap_ref_private()"?

We do not change the behavior of the function, but only the interface, it is kind of "refactoring" to me.

Fan

Quoted 12 lines

mm/hugetlb.c static void unmap_ref_private(struct mm_struct *mm, struct vm_area_struct *vma, · line 6112

Quoted 8 lines
mm/hugetlb.c +2 -1
@@ -6108,7 +6108,8 @@ static void unmap_ref_private(struct mm_struct *mm, struct vm_area_struct *vma,
 		 */
 		if (!is_vma_resv_set(iter_vma, HPAGE_RESV_OWNER))
 			unmap_hugepage_range(iter_vma, address,
-					     address + huge_page_size(h), page, 0);
+					     address + huge_page_size(h),
+					     folio_page(folio, 0), 0);

As previously, use &folio->page here.