Skip to contentSkip to conversation

mm/huge_memory: use end_folio to terminate anonymous folio remapping

PATCH Superseded Wei Yang 3 participants 4 messages Nov 27, 2025

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Wei Yang

author
#0 lore raw

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

---
 mm/huge_memory.c | 14 ++++----------
 1 file changed, 4 insertions(+), 10 deletions(-)
mm/huge_memory.c +4 -10
index 553bfa961fce..afc7b2f91e99 100644
@@ -3414,20 +3414,14 @@ 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 = folio_next(folio)) != end_folio);
 }
 
 static void lru_add_split_folio(struct folio *folio, struct folio *new_folio,
@@ -4066,7 +4060,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

Sorry Wei,

I know this is small, but can we not be doing these kinds of fiddly patches late in -rc7 please?

Thanks, Lorenzo

Quoted 57 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.

---
 mm/huge_memory.c | 14 ++++----------
 1 file changed, 4 insertions(+), 10 deletions(-)
mm/huge_memory.c +4 -11
index 553bfa961fce..afc7b2f91e99 100644
@@ -3414,20 +3414,14 @@ 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 = folio_next(folio)) != end_folio);
 }
 
 static void lru_add_split_folio(struct folio *folio, struct folio *new_folio,
@@ -4066,7 +4060,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
--

2.34.1

#2 lore raw
In reply to Wei Yang
Quoted 31 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.

---
  mm/huge_memory.c | 14 ++++----------
  1 file changed, 4 insertions(+), 10 deletions(-)
mm/huge_memory.c +1 -3
index 553bfa961fce..afc7b2f91e99 100644
@@ -3414,20 +3414,14 @@ 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))

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

Quoted 10 lines
mm/huge_memory.c +2 -6
  		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 = folio_next(folio)) != end_folio);

Rather ugly.

Nothing lost by just keeping "folio = folio_next(folio);" in the loop and focusing on what your patch documents.

But as Lorenzo said, the time for such cleanups is after most of the current stuff was merged in the upcoming merge window.

Signature · 3 lines
Cheers

David
author
#3 lore raw
Quoted 44 lines · David Hildenbrand (Red Hat)
Quoted 30 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.

---
  mm/huge_memory.c | 14 ++++----------
  1 file changed, 4 insertions(+), 10 deletions(-)
mm/huge_memory.c +1 -3
index 553bfa961fce..afc7b2f91e99 100644
@@ -3414,20 +3414,14 @@ 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))

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

Quoted 10 lines
mm/huge_memory.c +2 -6
  		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 = folio_next(folio)) != end_folio);

Rather ugly.

Quoted 5 lines

Nothing lost by just keeping "folio = folio_next(folio);" in the loop and focusing on what your patch documents.

But as Lorenzo said, the time for such cleanups is after most of the current stuff was merged in the upcoming merge window.

Sure.

Quoted 5 lines
Signature · 4 lines
-- 
Cheers

David
Signature · 2 lines
Wei Yang
Help you, Help me