Skip to contentSkip to conversation

mm/hugetlb: Let unmap_hugepage_range() and

PATCH Superseded [email protected] 5 participants 22 messages Apr 28, 2025 – May 5, 2025

Subsystems (2)

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

v3 cover mm/hugetlb: Let unmap_hugepage_range() and

From: Fan Ni <[email protected]>

Changes compared to v2,

Patch 1:
1) Update the commit log subject;
2) Use &folio->page instead of folio_page(folio) in unmap_ref_private() when calling unmap_hugepage_range();

Patch 2:
1) Update the declaration of unmap_hugepage_range() in hugetlb.h;
2) Use &folio->page instead of folio_page(folio) in unmap_hugepage_range() when calling __unmap_hugepage_range();

Patch 3:
1) Update the declaration of __unmap_hugepage_range() in hugetlb.h;
2) Rename ref_folio to folio;
3) compare folio instead of page in __unmap_hugepage_range() when folio is provided when calling __unmap_hugepage_range();

Patch 4:
1) Pass folio size instead of huge_page_size() when calling tlb_remove_page_size() by Matthew;
2) Update the processing inside __unmap_hugepage_range() when folio is provided as sugguested by David Hildenbrand;
3) Since there is some functional change in this patch, we do not pick up the tags;

v2:
https://lore.kernel.org/linux-mm/[email protected]

Fan Ni (4):
mm/hugetlb: Pass folio instead of page to unmap_ref_private() mm/hugetlb: Refactor unmap_hugepage_range() to take folio instead of
page
mm/hugetlb: Refactor __unmap_hugepage_range() to take folio instead of
page
mm/hugetlb: Convert use of struct page to folio in
__unmap_hugepage_range()

 include/linux/hugetlb.h |  8 ++++----
 mm/hugetlb.c            | 39 +++++++++++++++++++++------------------
 2 files changed, 25 insertions(+), 22 deletions(-)
Signature · 1 line
2.47.2
#1 lore raw

PATCH v3 1/4 mm/hugetlb: Pass folio instead of page to unmap_ref_private()

From: Fan Ni <[email protected]>

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

---
 mm/hugetlb.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
mm/hugetlb.c +4 -4
index e287d8050b40..b1268e7ca1f6 100644
@@ -6039,7 +6039,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;
@@ -6083,7 +6083,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, 0);
 	}
 	i_mmap_unlock_write(mapping);
 }
@@ -6206,8 +6207,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
Reviewed-by on v3 1/4
#3 lore raw

Re: [PATCH v3 1/4] mm/hugetlb: Pass folio instead of page to unmap_ref_private()

In reply to [email protected]
Quoted 8 lines · On Mon, Apr 28, 2025 at 10:11:44AM -0700, [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.

maintainer Reviewed-by on v3 1/4
#4 lore raw

Re: [PATCH v3 1/4] mm/hugetlb: Pass folio instead of page to unmap_ref_private()

In reply to [email protected]
Quoted 8 lines · On Mon, Apr 28, 2025 at 10:11:44AM -0700, [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 41 lines
---
 mm/hugetlb.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
mm/hugetlb.c +4 -4
index e287d8050b40..b1268e7ca1f6 100644
@@ -6039,7 +6039,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;
@@ -6083,7 +6083,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, 0);
 	}
 	i_mmap_unlock_write(mapping);
 }
@@ -6206,8 +6207,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
Signature · 2 lines
Oscar Salvador
SUSE Labs
#5 lore raw

PATCH v3 2/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_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.

---
 include/linux/hugetlb.h | 4 ++--
 mm/hugetlb.c            | 7 ++++---
 2 files changed, 6 insertions(+), 5 deletions(-)
include/linux/hugetlb.h +2 -2
index a57bed83c657..83d85cbb4284 100644
@@ -128,8 +128,8 @@ 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 *,
-			  zap_flags_t);
+			  unsigned long start, unsigned long end,
+			  struct folio *, zap_flags_t);
 void __unmap_hugepage_range(struct mmu_gather *tlb,
 			  struct vm_area_struct *vma,
 			  unsigned long start, unsigned long end,
mm/hugetlb.c +4 -3
index b1268e7ca1f6..7601e3d344bc 100644
@@ -6014,7 +6014,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 *folio,
 			  zap_flags_t zap_flags)
 {
 	struct mmu_notifier_range range;
@@ -6026,7 +6026,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, zap_flags);
 
 	mmu_notifier_invalidate_range_end(&range);
 	tlb_finish_mmu(&tlb);
@@ -6084,7 +6085,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, 0);
+					     folio, 0);
 	}
 	i_mmap_unlock_write(mapping);
 }
Signature · 1 line
2.47.2
Acked-by on v3 2/4
#6 lore raw

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

Quoted 9 lines · On 28.04.25 19:11, [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.

Quoted 7 lines

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

---

Signature · 3 lines
Cheers,

David / dhildenb
maintainer Reviewed-by on v3 2/4
#7 lore raw

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

In reply to [email protected]
Quoted 9 lines · On Mon, Apr 28, 2025 at 10:11:45AM -0700, [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.

Quoted 6 lines

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

Signature · 2 lines
Oscar Salvador
SUSE Labs
#8 lore raw

PATCH v3 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.

---
 include/linux/hugetlb.h |  4 ++--
 mm/hugetlb.c            | 10 +++++-----
 2 files changed, 7 insertions(+), 7 deletions(-)
include/linux/hugetlb.h +2 -2
index 83d85cbb4284..3a07a60c8cd9 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 *, 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 *folio,
 			zap_flags_t zap_flags)
 {
 	BUG();
mm/hugetlb.c +5 -5
index 7601e3d344bc..6696206d556e 100644
@@ -5808,7 +5808,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 *folio, zap_flags_t zap_flags)
 {
 	struct mm_struct *mm = vma->vm_mm;
 	unsigned long address;
@@ -5885,8 +5885,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 (folio) {
+			if (page_folio(page) != folio) {
 				spin_unlock(ptl);
 				continue;
 			}
@@ -5952,7 +5952,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 (folio)
 			break;
 	}
 	tlb_end_vma(tlb, vma);
@@ -6027,7 +6027,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, zap_flags);
+			       folio, zap_flags);
 
 	mmu_notifier_invalidate_range_end(&range);
 	tlb_finish_mmu(&tlb);
Signature · 1 line
2.47.2
Acked-by on v3 3/4
#9 lore raw

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

Quoted 9 lines · On 28.04.25 19:11, [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.

Quoted 5 lines

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

---

Signature · 3 lines
Cheers,

David / dhildenb
maintainer Reviewed-by on v3 3/4
#10 lore raw

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

In reply to [email protected]
Quoted 9 lines · On Mon, Apr 28, 2025 at 10:11:46AM -0700, [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.

Quoted 4 lines

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

But:

mm/hugetlb.c int move_hugetlb_page_tables(struct vm_area_struct *vma, · line 5811

Quoted 4 lines

void __unmap_hugepage_range(struct mmu_gather *tlb, struct vm_area_struct *vma,

mm/hugetlb.c +1 -1
 			    unsigned long start, unsigned long end,
-			    struct page *ref_page, zap_flags_t zap_flags)
+			    struct folio *folio, zap_flags_t zap_flags)

I think we are kinda losing information here. ref_ was a good hint and...

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

Quoted 10 lines
mm/hugetlb.c +2 -2
 	struct mm_struct *mm = vma->vm_mm;
 	unsigned long address;
@@ -5885,8 +5885,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 (folio) {
+			if (page_folio(page) != folio) {

You have to update the comment above, since we are not passing a reference page anymore but a folio.

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

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

Quoted 19 lines · Oscar Salvador
Quoted 9 lines · On Mon, Apr 28, 2025 at 10:11:46AM -0700, [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.

Quoted 4 lines

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

But:

Quoted 7 lines

mm/hugetlb.c int move_hugetlb_page_tables(struct vm_area_struct *vma, · line 5811

Quoted 4 lines

void __unmap_hugepage_range(struct mmu_gather *tlb, struct vm_area_struct *vma,

mm/hugetlb.c +1 -1
 			    unsigned long start, unsigned long end,
-			    struct page *ref_page, zap_flags_t zap_flags)
+			    struct folio *folio, zap_flags_t zap_flags)

I think we are kinda losing information here. ref_ was a good hint and...

Hi Oscar,

Thanks for the feedback.
Since the sugguested change here is minor and does not affect the function, and we do not have a aligned opinion here.
https://lore.kernel.org/linux-mm/[email protected]/
I will leave it as it is until there are more pushes for the change.

Quoted 14 lines

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

Quoted 11 lines
mm/hugetlb.c +2 -2
 	struct mm_struct *mm = vma->vm_mm;
 	unsigned long address;
@@ -5885,8 +5885,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 (folio) {
+			if (page_folio(page) != folio) {

You have to update the comment above, since we are not passing a reference page anymore but a folio.

Will update in the next version. Thanks.

Fan

Quoted 7 lines
Signature · 3 lines
-- 
Oscar Salvador
SUSE Labs
Signature · 1 line
Fan Ni
#12 lore raw

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

Quoted 28 lines · Fan Ni
Quoted 19 lines · Oscar Salvador
Quoted 9 lines · On Mon, Apr 28, 2025 at 10:11:46AM -0700, [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.

Quoted 4 lines

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

But:

Quoted 7 lines

mm/hugetlb.c int move_hugetlb_page_tables(struct vm_area_struct *vma, · line 5811

Quoted 4 lines

void __unmap_hugepage_range(struct mmu_gather *tlb, struct vm_area_struct *vma,
unsigned long start, unsigned long end,

mm/hugetlb.c +1 -1
-			    struct page *ref_page, zap_flags_t zap_flags)
+			    struct folio *folio, zap_flags_t zap_flags)

I think we are kinda losing information here. ref_ was a good hint and...

Quoted 7 lines

Hi Oscar,

Thanks for the feedback.
Since the sugguested change here is minor and does not affect the function, and we do not have a aligned opinion here.
https://lore.kernel.org/linux-mm/[email protected]/
I will leave it as it is until there are more pushes for the change.

I don't think we're losing any information. :) Especially if nowhere else in the kernel we use the term "ref_page" or "ref_folio". And things like put_ref_page(), free_unref_folios() ... talk about dropping references not "this is the reference folio".

"folio_to_unmap" might be clearer, but then, I think we should just use a single folio variable in that function.

This function might benefit from kerneldoc, though :)

Signature · 3 lines
Cheers,

David / dhildenb
#13 lore raw

PATCH v3 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.

---
 mm/hugetlb.c | 24 +++++++++++++-----------
 1 file changed, 13 insertions(+), 11 deletions(-)
mm/hugetlb.c +13 -11
index 6696206d556e..293c2afa724b 100644
@@ -5815,12 +5815,12 @@ 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 hstate *h = hstate_vma(vma);
 	unsigned long sz = huge_page_size(h);
 	bool adjust_reservation = false;
 	unsigned long last_addr_mask;
 	bool force_flush = false;
+	const bool folio_provided = !!folio;
 
 	WARN_ON(!is_vm_hugetlb_page(vma));
 	BUG_ON(start & ~huge_page_mask(h));
@@ -5879,14 +5879,13 @@ void __unmap_hugepage_range(struct mmu_gather *tlb, struct vm_area_struct *vma,
 			continue;
 		}
 
-		page = 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 (folio) {
-			if (page_folio(page) != folio) {
+		if (folio_provided) {
+			if (folio != page_folio(pte_page(pte))) {
 				spin_unlock(ptl);
 				continue;
 			}
@@ -5896,12 +5895,14 @@ void __unmap_hugepage_range(struct mmu_gather *tlb, struct vm_area_struct *vma,
 			 * looking like data was lost
 			 */
 			set_vma_resv_flags(vma, HPAGE_RESV_UNMAPPED);
+		} else {
+			folio = page_folio(pte_page(pte));
 		}
 
 		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))
@@ -5909,7 +5910,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
@@ -5918,8 +5919,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;
 		}
@@ -5943,16 +5944,17 @@ 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),
+				     folio_size(folio));
 		/*
 		 * Bail out after unmapping reference page if supplied
 		 */
-		if (folio)
+		if (folio_provided)
 			break;
 	}
 	tlb_end_vma(tlb, vma);
Signature · 1 line
2.47.2
Acked-by on v3 4/4
#14 lore raw

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

Quoted 16 lines · On 28.04.25 19:11, [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.

---
  mm/hugetlb.c | 24 +++++++++++++-----------
  1 file changed, 13 insertions(+), 11 deletions(-)
mm/hugetlb.c +0 -0
index 6696206d556e..293c2afa724b 100644

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

Quoted 10 lines
mm/hugetlb.c +1 -1
@@ -5815,12 +5815,12 @@ 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 hstate *h = hstate_vma(vma);
  	unsigned long sz = huge_page_size(h);
  	bool adjust_reservation = false;
  	unsigned long last_addr_mask;
  	bool force_flush = false;
+	const bool folio_provided = !!folio;

I would but that all the way to the top.

Quoted 7 lines
mm/hugetlb.c +1 -1
  		}
@@ -5943,16 +5944,17 @@ 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);

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

Quoted 10 lines
mm/hugetlb.c +2 -1
  			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),
+				     folio_size(folio));
  		/*
  		 * Bail out after unmapping reference page if supplied
  		 */

I wonder if we want to adjust that comment while at it.

/* If we were instructed to unmap a specific folio, we're done. */

Signature · 3 lines
Cheers,

David / dhildenb
maintainer Reviewed-by on v3 4/4
#15 lore raw

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

In reply to [email protected]
Quoted 16 lines · On Mon, Apr 28, 2025 at 10:11:47AM -0700, [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.

---
 mm/hugetlb.c | 24 +++++++++++++-----------
 1 file changed, 13 insertions(+), 11 deletions(-)
mm/hugetlb.c +0 -0
index 6696206d556e..293c2afa724b 100644

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

Quoted 10 lines
mm/hugetlb.c +1 -1
@@ -5815,12 +5815,12 @@ 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 hstate *h = hstate_vma(vma);
 	unsigned long sz = huge_page_size(h);
 	bool adjust_reservation = false;
 	unsigned long last_addr_mask;
 	bool force_flush = false;
+	const bool folio_provided = !!folio;

This might be me just nitpicking but:

Why not rename folio to ref_folio o provided_folio and do

        struct folio *folio = ref_folio;

I think it is more natural than the boolean.

Also you need to update the comment that references the 'reference page' and the one David mentioned.

Anyway,

Signature · 2 lines
Oscar Salvador
SUSE Labs
author
#17 lore raw
Quoted 5 lines · Andrew Morton
Quoted 1 line · On Mon, 28 Apr 2025 10:11:43 -0700 [email protected] wrote:

Subject: [PATCH v3 0/4] mm/hugetlb: Let unmap_hugepage_range() and

What was intended here?

Since I have changes in each patch in this series, I thought it may be better to have a cover letter to summarize all the changes in one place.

Should I just mention the changes in each patch separately?

Fan

#18 lore raw
Quoted 11 lines · Fan Ni
Quoted 5 lines · Andrew Morton
Quoted 1 line · On Mon, 28 Apr 2025 10:11:43 -0700 [email protected] wrote:

Subject: [PATCH v3 0/4] mm/hugetlb: Let unmap_hugepage_range() and

What was intended here?

Since I have changes in each patch in this series, I thought it may be better to have a cover letter to summarize all the changes in one place.

Should I just mention the changes in each patch separately?

The subject got trimmed, I think Andrew is asking what the real subject is supposed to be

"mm/hugetlb: Let unmap_hugepage_range() and ... " ?

Signature · 3 lines
Cheers,

David / dhildenb
author
#19 lore raw
Quoted 8 lines · David Hildenbrand
Quoted 7 lines · Fan Ni
Quoted 5 lines · Andrew Morton
Quoted 1 line · On Mon, 28 Apr 2025 10:11:43 -0700 [email protected] wrote:

Subject: [PATCH v3 0/4] mm/hugetlb: Let unmap_hugepage_range() and

What was intended here?

Quoted 7 lines
Quoted 4 lines

Since I have changes in each patch in this series, I thought it may be better to have a cover letter to summarize all the changes in one place.

Should I just mention the changes in each patch separately?

The subject got trimmed, I think Andrew is asking what the real subject is supposed to be

Oh. I see. Thanks.

The subject is

mm/hugetlb: Let unmap_hugepage_range() and several related functions take folio instead of page

Fan

Quoted 8 lines

"mm/hugetlb: Let unmap_hugepage_range() and ... " ?

Signature · 3 lines
Cheers,

David / dhildenb
#20 lore raw
In reply to [email protected]
Quoted 1 line · On Mon, Apr 28, 2025 at 10:11:43AM -0700, [email protected] wrote:

From: Fan Ni <[email protected]>

When you're sending v2, send the same cover letter as you did for the first one; don't make people go and look at v1 for the cover letter.

Put what was changed in v2 after it, so they have some idea what to look at when reviewing.

Quoted 45 lines

Changes compared to v2,

Patch 1:
1) Update the commit log subject;
2) Use &folio->page instead of folio_page(folio) in unmap_ref_private() when calling unmap_hugepage_range();

Patch 2:
1) Update the declaration of unmap_hugepage_range() in hugetlb.h;
2) Use &folio->page instead of folio_page(folio) in unmap_hugepage_range() when calling __unmap_hugepage_range();

Patch 3:
1) Update the declaration of __unmap_hugepage_range() in hugetlb.h;
2) Rename ref_folio to folio;
3) compare folio instead of page in __unmap_hugepage_range() when folio is provided when calling __unmap_hugepage_range();

Patch 4:
1) Pass folio size instead of huge_page_size() when calling tlb_remove_page_size() by Matthew;
2) Update the processing inside __unmap_hugepage_range() when folio is provided as sugguested by David Hildenbrand;
3) Since there is some functional change in this patch, we do not pick up the tags;

v2:
https://lore.kernel.org/linux-mm/[email protected]

Fan Ni (4):
mm/hugetlb: Pass folio instead of page to unmap_ref_private() mm/hugetlb: Refactor unmap_hugepage_range() to take folio instead of
page
mm/hugetlb: Refactor __unmap_hugepage_range() to take folio instead of
page
mm/hugetlb: Convert use of struct page to folio in
__unmap_hugepage_range()

 include/linux/hugetlb.h |  8 ++++----
 mm/hugetlb.c            | 39 +++++++++++++++++++++------------------
 2 files changed, 25 insertions(+), 22 deletions(-)
Signature · 1 line
2.47.2
author
#21 lore raw
Quoted 8 lines · Matthew Wilcox
Quoted 1 line · On Mon, Apr 28, 2025 at 10:11:43AM -0700, [email protected] wrote:

From: Fan Ni <[email protected]>

When you're sending v2, send the same cover letter as you did for the first one; don't make people go and look at v1 for the cover letter.

Put what was changed in v2 after it, so they have some idea what to look at when reviewing.

Hi Matthew,

Thanks for the instruction. The previous version of the series (v2) did not have a cover letter, so I only attached a link here.

I will keep the rule in my mind for future sending.

Fan

Quoted 46 lines
Quoted 46 lines

Changes compared to v2,

Patch 1:
1) Update the commit log subject;
2) Use &folio->page instead of folio_page(folio) in unmap_ref_private() when calling unmap_hugepage_range();

Patch 2:
1) Update the declaration of unmap_hugepage_range() in hugetlb.h;
2) Use &folio->page instead of folio_page(folio) in unmap_hugepage_range() when calling __unmap_hugepage_range();

Patch 3:
1) Update the declaration of __unmap_hugepage_range() in hugetlb.h;
2) Rename ref_folio to folio;
3) compare folio instead of page in __unmap_hugepage_range() when folio is provided when calling __unmap_hugepage_range();

Patch 4:
1) Pass folio size instead of huge_page_size() when calling tlb_remove_page_size() by Matthew;
2) Update the processing inside __unmap_hugepage_range() when folio is provided as sugguested by David Hildenbrand;
3) Since there is some functional change in this patch, we do not pick up the tags;

v2:
https://lore.kernel.org/linux-mm/[email protected]

Fan Ni (4):
mm/hugetlb: Pass folio instead of page to unmap_ref_private() mm/hugetlb: Refactor unmap_hugepage_range() to take folio instead of
page
mm/hugetlb: Refactor __unmap_hugepage_range() to take folio instead of
page
mm/hugetlb: Convert use of struct page to folio in
__unmap_hugepage_range()

 include/linux/hugetlb.h |  8 ++++----
 mm/hugetlb.c            | 39 +++++++++++++++++++++------------------
 2 files changed, 25 insertions(+), 22 deletions(-)
Signature · 1 line
2.47.2