Skip to contentSkip to conversation

mm/huge_memory: simplify page tracking in remap_page() during folio split

PATCH In review Wei Yang 3 participants 3 messages Dec 22, 2025 – Dec 23, 2025

Subsystems (2)
lore
Share & tools
lore

Wei Yang

author
#0 lore raw

PATCH v3 1/1 mm/huge_memory: simplify page tracking in remap_page() during folio split

After splitting a large folio, the resulting anonymous folios must be remapped.

Currently, the code tracks progress by recording the number of processed pages in an index variable (e.g., @i) and comparing it against the total. This commit simplifies the logic by directly subtracting the processed pages from the remaining count. This approach is more straightforward and reduces the number of local variables.

Additionally, this commit renames the variable nr to nr_pages to improve code readability and self-documentation.

---
v3: still use nr to count pages but subtract it directly
v2: move folio assignment in loop

---
 mm/huge_memory.c | 8 +++-----
 1 file changed, 3 insertions(+), 5 deletions(-)
mm/huge_memory.c +3 -5
index 40cf59301c21..d51aff0b7838 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);
 	}
Signature · 1 line
2.34.1
maintainer
#1 lore raw
Quoted 27 lines · On Mon, 22 Dec 2025 13:45:31 +0000 Wei Yang <[email protected]> wrote:

After splitting a large folio, the resulting anonymous folios must be remapped.

Currently, the code tracks progress by recording the number of processed pages in an index variable (e.g., @i) and comparing it against the total. This commit simplifies the logic by directly subtracting the processed pages from the remaining count. This approach is more straightforward and reduces the number of local variables.

Additionally, this commit renames the variable nr to nr_pages to improve code readability and self-documentation.

...

mm/huge_memory.c +1 -3
@@ -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))

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

Quoted 10 lines
mm/huge_memory.c +2 -2
 		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);
 	}

This changes behavior when nr_pages was too large. It would be best to mention this and to describe why this is OK. Or to check for ((signed)nr_pages <= 0).

Also, isn't it a bit nasty to alter an incoming arg? I think it's best to treat all those as const - part of the function's calling environment. Yes, C permits this hack but that was a mistake!

#2 lore raw
Quoted 44 lines · Andrew Morton
Quoted 27 lines · On Mon, 22 Dec 2025 13:45:31 +0000 Wei Yang <[email protected]> wrote:

After splitting a large folio, the resulting anonymous folios must be remapped.

Currently, the code tracks progress by recording the number of processed pages in an index variable (e.g., @i) and comparing it against the total. This commit simplifies the logic by directly subtracting the processed pages from the remaining count. This approach is more straightforward and reduces the number of local variables.

Additionally, this commit renames the variable nr to nr_pages to improve code readability and self-documentation.

...

mm/huge_memory.c +1 -3
@@ -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))

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

Quoted 10 lines
mm/huge_memory.c +2 -2
  		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);
  	}

This changes behavior when nr_pages was too large. It would be best to mention this and to describe why this is OK. Or to check for ((signed)nr_pages <= 0).

Quoted 3 lines

Also, isn't it a bit nasty to alter an incoming arg? I think it's best to treat all those as const - part of the function's calling environment. Yes, C permits this hack but that was a mistake!

For small functions I think it's quite acceptable.

But yeah, for bigger functions it can happen too easily that one ends up reusing a now-modified value by mistake.

Signature · 3 lines
Cheers

David