Skip to contentSkip to conversation

mm/huge_memory: use end_folio to terminate anonymous folio remapping

PATCH In review Wei Yang 4 participants 10 messages Dec 15, 2025 – Dec 20, 2025

Subsystems (2)
lore
Share & tools
lore

Wei Yang

author
#0 lore raw

PATCH v2 1/1 mm/huge_memory: use end_folio to terminate anonymous folio remapping

After splitting a large folio, it is necessary to remap the resulting anonymous folios.

The current implementation determines the end of the remapping process by counting the number of pages that have been processed.

Since the final folio in the sequence, end_folio, is already known and tracked, this commit refactors the remapping loop to leverage end_folio as the termination marker.

---
v2: move folio assignment in loop

---
 mm/huge_memory.c | 13 ++++---------
 1 file changed, 4 insertions(+), 9 deletions(-)
mm/huge_memory.c +4 -9
index 40cf59301c21..fe812d9c7807 100644
@@ -3423,20 +3423,15 @@ bool unmap_huge_pmd_locked(struct vm_area_struct *vma, unsigned long addr,
 	return __discard_anon_folio_pmd_locked(vma, addr, pmdp, folio);
 }
 
-static void remap_page(struct folio *folio, unsigned long nr, int flags)
+static void remap_page(struct folio *folio, struct folio *end_folio, int flags)
 {
-	int i = 0;
-
 	/* If unmap_folio() uses try_to_migrate() on file, remove this check */
 	if (!folio_test_anon(folio))
 		return;
-	for (;;) {
+	do {
 		remove_migration_ptes(folio, folio, RMP_LOCKED | flags);
-		i += folio_nr_pages(folio);
-		if (i >= nr)
-			break;
 		folio = folio_next(folio);
-	}
+	} while (folio != end_folio);
 }
 
 static void lru_add_split_folio(struct folio *folio, struct folio *new_folio,
@@ -4066,7 +4061,7 @@ static int __folio_split(struct folio *folio, unsigned int new_order,
 	if (!ret && is_anon && !folio_is_device_private(folio))
 		remap_flags = RMP_USE_SHARED_ZEROPAGE;
 
-	remap_page(folio, 1 << old_order, remap_flags);
+	remap_page(folio, end_folio, remap_flags);
 
 	/*
 	 * Unlock all after-split folios except the one containing
Signature · 1 line
2.34.1
#1 lore raw
Quoted 25 lines · Wei Yang

After splitting a large folio, it is necessary to remap the resulting anonymous folios.

The current implementation determines the end of the remapping process by counting the number of pages that have been processed.

Since the final folio in the sequence, end_folio, is already known and tracked, this commit refactors the remapping loop to leverage end_folio as the termination marker.

---
v2: move folio assignment in loop

---
 mm/huge_memory.c | 13 ++++---------
 1 file changed, 4 insertions(+), 9 deletions(-)
mm/huge_memory.c +0 -0
index 40cf59301c21..fe812d9c7807 100644

mm/huge_memory.c bool unmap_huge_pmd_locked(struct vm_area_struct *vma, unsigned long addr, · line 3427

Quoted 6 lines
mm/huge_memory.c +1 -1
@@ -3423,20 +3423,15 @@ bool unmap_huge_pmd_locked(struct vm_area_struct *vma, unsigned long addr,
        return __discard_anon_folio_pmd_locked(vma, addr, pmdp, folio);
 }
 
-static void remap_page(struct folio *folio, unsigned long nr, int flags)
+static void remap_page(struct folio *folio, struct folio *end_folio, int flags)
 {

Do we actually see an improvement in readability or performance? The existing code feels a bit more readable to me; nr is clearly the number of pages :-)

Thanks
Barry

author
#2 lore raw
Quoted 29 lines · Barry Song
Quoted 28 lines · Wei Yang

After splitting a large folio, it is necessary to remap the resulting anonymous folios.

The current implementation determines the end of the remapping process by counting the number of pages that have been processed.

Since the final folio in the sequence, end_folio, is already known and tracked, this commit refactors the remapping loop to leverage end_folio as the termination marker.

---
v2: move folio assignment in loop

---
 mm/huge_memory.c | 13 ++++---------
 1 file changed, 4 insertions(+), 9 deletions(-)
mm/huge_memory.c +0 -0
index 40cf59301c21..fe812d9c7807 100644
@@ -3423,20 +3423,15 @@ bool unmap_huge_pmd_locked(struct vm_area_struct *vma, unsigned long addr,
        return __discard_anon_folio_pmd_locked(vma, addr, pmdp, folio);
 }
 
Quoted 8 lines

mm/huge_memory.c bool unmap_huge_pmd_locked(struct vm_area_struct *vma, unsigned long addr, · line 3427

Quoted 3 lines
mm/huge_memory.c +1 -1
-static void remap_page(struct folio *folio, unsigned long nr, int flags)
+static void remap_page(struct folio *folio, struct folio *end_folio, int flags)
 {

Do we actually see an improvement in readability or performance? The existing code feels a bit more readable to me; nr is clearly the number of pages :-)

Hi, Barry,

Thanks for your review.

Currently end_folio has been used in __folio_split() and __folio_freeze_and_split_unmapped() as a termination marker. So continue to use end_folio here as a termination marker looks consistent and improves readability to me.

But yeah, this is my personal preference. If it is not the case, I am fine to keep as it is now. :-)

Quoted 2 lines

Thanks
Barry

Signature · 2 lines
Wei Yang
Help you, Help me
#3 lore raw
Quoted 40 lines · Wei Yang
Quoted 29 lines · Barry Song
Quoted 28 lines · Wei Yang

After splitting a large folio, it is necessary to remap the resulting anonymous folios.

The current implementation determines the end of the remapping process by counting the number of pages that have been processed.

Since the final folio in the sequence, end_folio, is already known and tracked, this commit refactors the remapping loop to leverage end_folio as the termination marker.

---
v2: move folio assignment in loop

---
 mm/huge_memory.c | 13 ++++---------
 1 file changed, 4 insertions(+), 9 deletions(-)
mm/huge_memory.c +0 -0
index 40cf59301c21..fe812d9c7807 100644
@@ -3423,20 +3423,15 @@ bool unmap_huge_pmd_locked(struct vm_area_struct *vma, unsigned long addr,
        return __discard_anon_folio_pmd_locked(vma, addr, pmdp, folio);
 }
 
Quoted 8 lines

mm/huge_memory.c bool unmap_huge_pmd_locked(struct vm_area_struct *vma, unsigned long addr, · line 3427

Quoted 3 lines
mm/huge_memory.c +1 -1
-static void remap_page(struct folio *folio, unsigned long nr, int flags)
+static void remap_page(struct folio *folio, struct folio *end_folio, int flags)
 {

Do we actually see an improvement in readability or performance? The existing code feels a bit more readable to me; nr is clearly the number of pages :-)

Quoted 8 lines

Hi, Barry,

Thanks for your review.

Currently end_folio has been used in __folio_split() and __folio_freeze_and_split_unmapped() as a termination marker. So continue to use end_folio here as a termination marker looks consistent and improves readability to me.

In both cases the variables are temporary, and they look fine within their own self-documented contexts.

Now that we’re crossing two functions in the context of remap_page(), this doesn’t seem to be the case, though.

Quoted 3 lines

But yeah, this is my personal preference. If it is not the case, I am fine to keep as it is now. :-)

I personally find the existing code clearer, but I’m also okay if others like your new approach more.

Thanks
Barry

author
#4 lore raw
Quoted 50 lines · Barry Song
Quoted 40 lines · Wei Yang
Quoted 29 lines · Barry Song
Quoted 28 lines · Wei Yang

After splitting a large folio, it is necessary to remap the resulting anonymous folios.

The current implementation determines the end of the remapping process by counting the number of pages that have been processed.

Since the final folio in the sequence, end_folio, is already known and tracked, this commit refactors the remapping loop to leverage end_folio as the termination marker.

---
v2: move folio assignment in loop

---
 mm/huge_memory.c | 13 ++++---------
 1 file changed, 4 insertions(+), 9 deletions(-)
mm/huge_memory.c +0 -0
index 40cf59301c21..fe812d9c7807 100644
@@ -3423,20 +3423,15 @@ bool unmap_huge_pmd_locked(struct vm_area_struct *vma, unsigned long addr,
        return __discard_anon_folio_pmd_locked(vma, addr, pmdp, folio);
 }
 
Quoted 8 lines

mm/huge_memory.c bool unmap_huge_pmd_locked(struct vm_area_struct *vma, unsigned long addr, · line 3427

Quoted 3 lines
mm/huge_memory.c +1 -1
-static void remap_page(struct folio *folio, unsigned long nr, int flags)
+static void remap_page(struct folio *folio, struct folio *end_folio, int flags)
 {

Do we actually see an improvement in readability or performance? The existing code feels a bit more readable to me; nr is clearly the number of pages :-)

Quoted 8 lines

Hi, Barry,

Thanks for your review.

Currently end_folio has been used in __folio_split() and __folio_freeze_and_split_unmapped() as a termination marker. So continue to use end_folio here as a termination marker looks consistent and improves readability to me.

Quoted 6 lines

In both cases the variables are temporary, and they look fine within their own self-documented contexts.

Now that we’re crossing two functions in the context of remap_page(), this doesn’t seem to be the case, though.

Thanks for your feedback.

Quoted 7 lines
Quoted 3 lines

But yeah, this is my personal preference. If it is not the case, I am fine to keep as it is now. :-)

I personally find the existing code clearer, but I’m also okay if others like your new approach more.

Thanks

Quoted 2 lines

Thanks
Barry

Signature · 2 lines
Wei Yang
Help you, Help me
#5 lore raw
In reply to Barry Song
Quoted 57 lines · Barry Song
Quoted 40 lines · Wei Yang
Quoted 29 lines · Barry Song
Quoted 28 lines · Wei Yang

After splitting a large folio, it is necessary to remap the resulting anonymous folios.

The current implementation determines the end of the remapping process by counting the number of pages that have been processed.

Since the final folio in the sequence, end_folio, is already known and tracked, this commit refactors the remapping loop to leverage end_folio as the termination marker.

---
v2: move folio assignment in loop

---
  mm/huge_memory.c | 13 ++++---------
  1 file changed, 4 insertions(+), 9 deletions(-)
mm/huge_memory.c +0 -0
index 40cf59301c21..fe812d9c7807 100644
@@ -3423,20 +3423,15 @@ bool unmap_huge_pmd_locked(struct vm_area_struct *vma, unsigned long addr,
         return __discard_anon_folio_pmd_locked(vma, addr, pmdp, folio);
  }
 
Quoted 8 lines

mm/huge_memory.c bool unmap_huge_pmd_locked(struct vm_area_struct *vma, unsigned long addr, · line 3427

Quoted 3 lines
mm/huge_memory.c +1 -1
-static void remap_page(struct folio *folio, unsigned long nr, int flags)
+static void remap_page(struct folio *folio, struct folio *end_folio, int flags)
  {

Do we actually see an improvement in readability or performance? The existing code feels a bit more readable to me; nr is clearly the number of pages :-)

Quoted 8 lines

Hi, Barry,

Thanks for your review.

Currently end_folio has been used in __folio_split() and __folio_freeze_and_split_unmapped() as a termination marker. So continue to use end_folio here as a termination marker looks consistent and improves readability to me.

In both cases the variables are temporary, and they look fine within their own self-documented contexts.

Now that we’re crossing two functions in the context of remap_page(), this doesn’t seem to be the case, though.

Quoted 1 line
Quoted 5 lines
Quoted 2 lines

But yeah, this is my personal preference. If it is not the case, I am fine to keep as it is now. :-)

I personally find the existing code clearer, but I’m also okay if others like your new approach more.

Me too. Well, the existing code could be cleaned up
* nr -> nr_pages
* remove i and subtract from nr_pages instead

probably more ... :)

Signature · 3 lines
Cheers

David
author
#6 lore raw
Quoted 61 lines · David Hildenbrand (Red Hat)
Quoted 60 lines · Barry Song
Quoted 40 lines · Wei Yang
Quoted 29 lines · Barry Song
Quoted 28 lines · Wei Yang

After splitting a large folio, it is necessary to remap the resulting anonymous folios.

The current implementation determines the end of the remapping process by counting the number of pages that have been processed.

Since the final folio in the sequence, end_folio, is already known and tracked, this commit refactors the remapping loop to leverage end_folio as the termination marker.

---
v2: move folio assignment in loop

---
  mm/huge_memory.c | 13 ++++---------
  1 file changed, 4 insertions(+), 9 deletions(-)
mm/huge_memory.c +0 -0
index 40cf59301c21..fe812d9c7807 100644
@@ -3423,20 +3423,15 @@ bool unmap_huge_pmd_locked(struct vm_area_struct *vma, unsigned long addr,
         return __discard_anon_folio_pmd_locked(vma, addr, pmdp, folio);
  }
 
Quoted 8 lines

mm/huge_memory.c bool unmap_huge_pmd_locked(struct vm_area_struct *vma, unsigned long addr, · line 3427

Quoted 3 lines
mm/huge_memory.c +1 -1
-static void remap_page(struct folio *folio, unsigned long nr, int flags)
+static void remap_page(struct folio *folio, struct folio *end_folio, int flags)
  {

Do we actually see an improvement in readability or performance? The existing code feels a bit more readable to me; nr is clearly the number of pages :-)

Quoted 8 lines

Hi, Barry,

Thanks for your review.

Currently end_folio has been used in __folio_split() and __folio_freeze_and_split_unmapped() as a termination marker. So continue to use end_folio here as a termination marker looks consistent and improves readability to me.

In both cases the variables are temporary, and they look fine within their own self-documented contexts.

Now that we’re crossing two functions in the context of remap_page(), this doesn’t seem to be the case, though.

Quoted 3 lines

But yeah, this is my personal preference. If it is not the case, I am fine to keep as it is now. :-)

Quoted 7 lines
Quoted 2 lines

I personally find the existing code clearer, but I’m also okay if others like your new approach more.

Me too. Well, the existing code could be cleaned up
* nr -> nr_pages
* remove i and subtract from nr_pages instead

Sorry for my poor taste :-)

Is this what you expect?

mm/huge_memory.c +3 -5
index 1db9cfb09533..b8ee33318a60 100644
@@ -3423,17 +3423,15 @@ bool unmap_huge_pmd_locked(struct vm_area_struct *vma, unsigned long addr,
 	return __discard_anon_folio_pmd_locked(vma, addr, pmdp, folio);
 }
 
-static void remap_page(struct folio *folio, unsigned long nr, int flags)
+static void remap_page(struct folio *folio, unsigned long nr_pages, int flags)
 {
-	int i = 0;
-
 	/* If unmap_folio() uses try_to_migrate() on file, remove this check */
 	if (!folio_test_anon(folio))
 		return;
 	for (;;) {
 		remove_migration_ptes(folio, folio, RMP_LOCKED | flags);
-		i += folio_nr_pages(folio);
-		if (i >= nr)
+		nr_pages -= folio_nr_pages(folio);
+		if (!nr_pages)
 			break;
 		folio = folio_next(folio);
 	}
Quoted 2 lines

probably more ... :)

Don't catch this. Sounds you have more idea on further cleanup?

Glad to hear your insight.

Quoted 4 lines
Signature · 3 lines
Cheers

David
Signature · 2 lines
Wei Yang
Help you, Help me
#7 lore raw
Quoted 87 lines · Wei Yang
Quoted 61 lines · David Hildenbrand (Red Hat)
Quoted 60 lines · Barry Song
Quoted 40 lines · Wei Yang
Quoted 29 lines · Barry Song
Quoted 28 lines · Wei Yang

After splitting a large folio, it is necessary to remap the resulting anonymous folios.

The current implementation determines the end of the remapping process by counting the number of pages that have been processed.

Since the final folio in the sequence, end_folio, is already known and tracked, this commit refactors the remapping loop to leverage end_folio as the termination marker.

---
v2: move folio assignment in loop

---
   mm/huge_memory.c | 13 ++++---------
   1 file changed, 4 insertions(+), 9 deletions(-)
mm/huge_memory.c +0 -0
index 40cf59301c21..fe812d9c7807 100644
@@ -3423,20 +3423,15 @@ bool unmap_huge_pmd_locked(struct vm_area_struct *vma, unsigned long addr,
          return __discard_anon_folio_pmd_locked(vma, addr, pmdp, folio);
   }
 
Quoted 8 lines

mm/huge_memory.c bool unmap_huge_pmd_locked(struct vm_area_struct *vma, unsigned long addr, · line 3427

Quoted 3 lines
mm/huge_memory.c +1 -1
-static void remap_page(struct folio *folio, unsigned long nr, int flags)
+static void remap_page(struct folio *folio, struct folio *end_folio, int flags)
   {

Do we actually see an improvement in readability or performance? The existing code feels a bit more readable to me; nr is clearly the number of pages :-)

Quoted 8 lines

Hi, Barry,

Thanks for your review.

Currently end_folio has been used in __folio_split() and __folio_freeze_and_split_unmapped() as a termination marker. So continue to use end_folio here as a termination marker looks consistent and improves readability to me.

In both cases the variables are temporary, and they look fine within their own self-documented contexts.

Now that we’re crossing two functions in the context of remap_page(), this doesn’t seem to be the case, though.

Quoted 3 lines

But yeah, this is my personal preference. If it is not the case, I am fine to keep as it is now. :-)

Quoted 7 lines
Quoted 2 lines

I personally find the existing code clearer, but I’m also okay if others like your new approach more.

Me too. Well, the existing code could be cleaned up
* nr -> nr_pages
* remove i and subtract from nr_pages instead

Sorry for my poor taste :-)

Is this what you expect?

mm/huge_memory.c +1 -3
index 1db9cfb09533..b8ee33318a60 100644
@@ -3423,17 +3423,15 @@ bool unmap_huge_pmd_locked(struct vm_area_struct *vma, unsigned long addr,
  	return __discard_anon_folio_pmd_locked(vma, addr, pmdp, folio);
  }
 
-static void remap_page(struct folio *folio, unsigned long nr, int flags)
+static void remap_page(struct folio *folio, unsigned long nr_pages, int flags)
  {
-	int i = 0;
-

mm/huge_memory.c bool unmap_huge_pmd_locked(struct vm_area_struct *vma, unsigned long addr, · line 3435

Quoted 10 lines
mm/huge_memory.c +2 -2
  	/* If unmap_folio() uses try_to_migrate() on file, remove this check */
  	if (!folio_test_anon(folio))
  		return;
  	for (;;) {
  		remove_migration_ptes(folio, folio, RMP_LOCKED | flags);
-		i += folio_nr_pages(folio);
-		if (i >= nr)
+		nr_pages -= folio_nr_pages(folio);
+		if (!nr_pages)
  			break;

Right, I think we would never underflow nr_pages.

Quoted 7 lines
mm/huge_memory.c +0 -0
  		folio = folio_next(folio);
  	}
Quoted 3 lines

probably more ... :)

Don't catch this. Sounds you have more idea on further cleanup?

I was rather wondering whether there could be more cleanups, but I have nothing concrete in mind.

Signature · 3 lines
Cheers

David
author
#8 lore raw
Quoted 91 lines · David Hildenbrand (Red Hat)
Quoted 90 lines · Wei Yang
Quoted 61 lines · David Hildenbrand (Red Hat)
Quoted 60 lines · Barry Song
Quoted 40 lines · Wei Yang
Quoted 29 lines · Barry Song
Quoted 28 lines · Wei Yang

After splitting a large folio, it is necessary to remap the resulting anonymous folios.

The current implementation determines the end of the remapping process by counting the number of pages that have been processed.

Since the final folio in the sequence, end_folio, is already known and tracked, this commit refactors the remapping loop to leverage end_folio as the termination marker.

---
v2: move folio assignment in loop

---
   mm/huge_memory.c | 13 ++++---------
   1 file changed, 4 insertions(+), 9 deletions(-)
mm/huge_memory.c +0 -0
index 40cf59301c21..fe812d9c7807 100644
@@ -3423,20 +3423,15 @@ bool unmap_huge_pmd_locked(struct vm_area_struct *vma, unsigned long addr,
          return __discard_anon_folio_pmd_locked(vma, addr, pmdp, folio);
   }
 
Quoted 8 lines

mm/huge_memory.c bool unmap_huge_pmd_locked(struct vm_area_struct *vma, unsigned long addr, · line 3427

Quoted 3 lines
mm/huge_memory.c +1 -1
-static void remap_page(struct folio *folio, unsigned long nr, int flags)
+static void remap_page(struct folio *folio, struct folio *end_folio, int flags)
   {

Do we actually see an improvement in readability or performance? The existing code feels a bit more readable to me; nr is clearly the number of pages :-)

Quoted 8 lines

Hi, Barry,

Thanks for your review.

Currently end_folio has been used in __folio_split() and __folio_freeze_and_split_unmapped() as a termination marker. So continue to use end_folio here as a termination marker looks consistent and improves readability to me.

In both cases the variables are temporary, and they look fine within their own self-documented contexts.

Now that we’re crossing two functions in the context of remap_page(), this doesn’t seem to be the case, though.

Quoted 3 lines

But yeah, this is my personal preference. If it is not the case, I am fine to keep as it is now. :-)

Quoted 7 lines
Quoted 2 lines

I personally find the existing code clearer, but I’m also okay if others like your new approach more.

Me too. Well, the existing code could be cleaned up
* nr -> nr_pages
* remove i and subtract from nr_pages instead

Sorry for my poor taste :-)

Is this what you expect?

mm/huge_memory.c +1 -3
index 1db9cfb09533..b8ee33318a60 100644
@@ -3423,17 +3423,15 @@ bool unmap_huge_pmd_locked(struct vm_area_struct *vma, unsigned long addr,
  	return __discard_anon_folio_pmd_locked(vma, addr, pmdp, folio);
  }
-static void remap_page(struct folio *folio, unsigned long nr, int flags)
+static void remap_page(struct folio *folio, unsigned long nr_pages, int flags)
  {
-	int i = 0;
-
  	/* If unmap_folio() uses try_to_migrate() on file, remove this check */
  	if (!folio_test_anon(folio))
  		return;
  	for (;;) {
Quoted 9 lines

mm/huge_memory.c bool unmap_huge_pmd_locked(struct vm_area_struct *vma, unsigned long addr, · line 3435

Quoted 6 lines
mm/huge_memory.c +2 -2
  		remove_migration_ptes(folio, folio, RMP_LOCKED | flags);
-		i += folio_nr_pages(folio);
-		if (i >= nr)
+		nr_pages -= folio_nr_pages(folio);
+		if (!nr_pages)
  			break;

Right, I think we would never underflow nr_pages.

Thanks. Well prepare v3 with this.

Quoted 15 lines
Quoted 7 lines
mm/huge_memory.c +0 -0
  		folio = folio_next(folio);
  	}
 
Quoted 2 lines

probably more ... :)

Don't catch this. Sounds you have more idea on further cleanup?

I was rather wondering whether there could be more cleanups, but I have nothing concrete in mind.

Signature · 3 lines
Cheers

David
Signature · 2 lines
Wei Yang
Help you, Help me
#9 lore raw
In reply to Wei Yang
Quoted 24 lines

After splitting a large folio, it is necessary to remap the resulting anonymous folios.

The current implementation determines the end of the remapping process by counting the number of pages that have been processed.

Since the final folio in the sequence, end_folio, is already known and tracked, this commit refactors the remapping loop to leverage end_folio as the termination marker.

---
v2: move folio assignment in loop

---
 mm/huge_memory.c | 13 ++++---------
 1 file changed, 4 insertions(+), 9 deletions(-)
mm/huge_memory.c +0 -0
index 40cf59301c21..fe812d9c7807 100644

mm/huge_memory.c bool unmap_huge_pmd_locked(struct vm_area_struct *vma, unsigned long addr, · line 3427

Quoted 6 lines
mm/huge_memory.c +1 -1
@@ -3423,20 +3423,15 @@ bool unmap_huge_pmd_locked(struct vm_area_struct *vma, unsigned long addr,
        return __discard_anon_folio_pmd_locked(vma, addr, pmdp, folio);
 }
 
-static void remap_page(struct folio *folio, unsigned long nr, int flags)
+static void remap_page(struct folio *folio, struct folio *end_folio, int flags)
 {

Reviewed v1. The end_folio logic in __folio_split is clear, but I agree with Barry that nr_pages is sufficiently intuitive. :-)

--
Best Regards,
wang lian