Skip to contentSkip to conversation

mm/huge_memory: cleanup __split_unmapped_folio()

PATCH Applied Wei Yang 5 participants 24 messages Oct 21, 2025 – Oct 31, 2025

Subsystems (2)
lore
Share & tools
lore

Wei Yang

author
#0 lore raw

v3 cover mm/huge_memory: cleanup __split_unmapped_folio()

This short patch series cleans up and optimizes the internal logic of the __split_unmapped_folio() function.

The goal is to improve clarity and efficiency by eliminating redundant checks, caching stable attribute values, and simplifying the iteration logic used for updating folio statistics.

These changes make the code easier to follow and maintain.

The split_huge_page_test selftest pass.

v3:
* only merge 4&5 in v1
* refine the comment

v2:
* merge patch 2-5
* http://lkml.kernel.org/r/[email protected]

v1:
* http://lkml.kernel.org/r/[email protected]

Wei Yang (4):
mm/huge_memory: avoid reinvoking folio_test_anon() mm/huge_memory: update folio stat after successful split mm/huge_memory: optimize and simplify folio stat update after split mm/huge_memory: optimize old_order derivation during folio splitting

 mm/huge_memory.c | 70 +++++++++++++++---------------------------------
 1 file changed, 21 insertions(+), 49 deletions(-)
Signature · 1 line
2.34.1
author
#1 lore raw

PATCH v3 1/4 mm/huge_memory: avoid reinvoking folio_test_anon()

During the execution of __split_unmapped_folio(), the folio's anon/!anon attribute is invariant (not expected to change).

Therefore, it is safe and more efficient to retrieve this attribute once at the start and reuse it throughout the function.

---
v3:
* adjust subject
* add const to variable is_anon

---
 mm/huge_memory.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
mm/huge_memory.c +4 -3
index 370ecfd6a182..6d82df4a88dc 100644
@@ -3595,6 +3595,7 @@ static int __split_unmapped_folio(struct folio *folio, int new_order,
 		struct page *split_at, struct xa_state *xas,
 		struct address_space *mapping, bool uniform_split)
 {
+	const bool is_anon = folio_test_anon(folio);
 	int order = folio_order(folio);
 	int start_order = uniform_split ? new_order : order - 1;
 	bool stop_split = false;
@@ -3602,7 +3603,7 @@ static int __split_unmapped_folio(struct folio *folio, int new_order,
 	int split_order;
 	int ret = 0;
 
-	if (folio_test_anon(folio))
+	if (is_anon)
 		mod_mthp_stat(order, MTHP_STAT_NR_ANON, -1);
 
 	folio_clear_has_hwpoisoned(folio);
@@ -3619,7 +3620,7 @@ static int __split_unmapped_folio(struct folio *folio, int new_order,
 		struct folio *new_folio;
 
 		/* order-1 anonymous folio is not supported */
-		if (folio_test_anon(folio) && split_order == 1)
+		if (is_anon && split_order == 1)
 			continue;
 		if (uniform_split && split_order != new_order)
 			continue;
@@ -3671,7 +3672,7 @@ static int __split_unmapped_folio(struct folio *folio, int new_order,
 				if (split_order != new_order && !stop_split)
 					continue;
 			}
-			if (folio_test_anon(new_folio))
+			if (is_anon)
 				mod_mthp_stat(folio_order(new_folio),
 					      MTHP_STAT_NR_ANON, 1);
 		}
Signature · 1 line
2.34.1
Reviewed-by on v3 1/4
#2 lore raw

Re: [Patch v3 1/4] mm/huge_memory: avoid reinvoking folio_test_anon()

Quoted 5 lines · Wei Yang

During the execution of __split_unmapped_folio(), the folio's anon/!anon attribute is invariant (not expected to change).

Therefore, it is safe and more efficient to retrieve this attribute once at the start and reuse it throughout the function.

Thanks!

Quoted 4 lines

LGTM, so:

Quoted 25 lines
Cc Baolin Wang <[email protected]>
Cc Dev Jain <[email protected]>
Cc Lance Yang <[email protected]>
Cc Liam Howlett <[email protected]>
Cc Lorenzo Stoakes <[email protected]>
Cc Mariano Pache <[email protected]>
Cc Ryan Roberts <[email protected]>

---
v3:
* adjust subject
* add const to variable is_anon

---
 mm/huge_memory.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
mm/huge_memory.c +0 -0
index 370ecfd6a182..6d82df4a88dc 100644

mm/huge_memory.c static int __split_unmapped_folio(struct folio *folio, int new_order, · line 3598

Quoted 4 lines
mm/huge_memory.c +1 -0
@@ -3595,6 +3595,7 @@ static int __split_unmapped_folio(struct folio *folio, int new_order,
 		struct page *split_at, struct xa_state *xas,
 		struct address_space *mapping, bool uniform_split)
 {
+	const bool is_anon = folio_test_anon(folio);

Thanks for making const!

Quoted 34 lines
mm/huge_memory.c +3 -4
 	int order = folio_order(folio);
 	int start_order = uniform_split ? new_order : order - 1;
 	bool stop_split = false;
@@ -3602,7 +3603,7 @@ static int __split_unmapped_folio(struct folio *folio, int new_order,
 	int split_order;
 	int ret = 0;
 
-	if (folio_test_anon(folio))
+	if (is_anon)
 		mod_mthp_stat(order, MTHP_STAT_NR_ANON, -1);
 
 	folio_clear_has_hwpoisoned(folio);
@@ -3619,7 +3620,7 @@ static int __split_unmapped_folio(struct folio *folio, int new_order,
 		struct folio *new_folio;
 
 		/* order-1 anonymous folio is not supported */
-		if (folio_test_anon(folio) && split_order == 1)
+		if (is_anon && split_order == 1)
 			continue;
 		if (uniform_split && split_order != new_order)
 			continue;
@@ -3671,7 +3672,7 @@ static int __split_unmapped_folio(struct folio *folio, int new_order,
 				if (split_order != new_order && !stop_split)
 					continue;
 			}
-			if (folio_test_anon(new_folio))
+			if (is_anon)
 				mod_mthp_stat(folio_order(new_folio),
 					      MTHP_STAT_NR_ANON, 1);
 		}
--

2.34.1

author
#3 lore raw

PATCH v3 2/4 mm/huge_memory: update folio stat after successful split

In reply to Wei Yang

The current implementation complicates this process:

* It iterates over the resulting new folios.
* It uses a flag (@stop_split) to conditionally skip updating the stat for the folio at @split_at during the loop.
* It then attempts to update the skipped stat on a subsequent failure path.

This logic is unnecessarily hard to follow.

This commit refactors the code to update the folio statistics only after a successful split. This makes the logic much cleaner and sets the stage for further simplification of the stat-handling code.

---
 mm/huge_memory.c | 44 +++++++++++---------------------------------
 1 file changed, 11 insertions(+), 33 deletions(-)
mm/huge_memory.c +11 -33
index 6d82df4a88dc..b9a38dba8eb8 100644
@@ -3598,13 +3598,8 @@ static int __split_unmapped_folio(struct folio *folio, int new_order,
 	const bool is_anon = folio_test_anon(folio);
 	int order = folio_order(folio);
 	int start_order = uniform_split ? new_order : order - 1;
-	bool stop_split = false;
 	struct folio *next;
 	int split_order;
-	int ret = 0;
-
-	if (is_anon)
-		mod_mthp_stat(order, MTHP_STAT_NR_ANON, -1);
 
 	folio_clear_has_hwpoisoned(folio);
 
@@ -3613,7 +3608,7 @@ static int __split_unmapped_folio(struct folio *folio, int new_order,
 	 * folio is split to new_order directly.
 	 */
 	for (split_order = start_order;
-	     split_order >= new_order && !stop_split;
+	     split_order >= new_order;
 	     split_order--) {
 		struct folio *end_folio = folio_next(folio);
 		int old_order = folio_order(folio);
@@ -3636,49 +3631,32 @@ static int __split_unmapped_folio(struct folio *folio, int new_order,
 			else {
 				xas_set_order(xas, folio->index, split_order);
 				xas_try_split(xas, folio, old_order);
-				if (xas_error(xas)) {
-					ret = xas_error(xas);
-					stop_split = true;
-				}
+				if (xas_error(xas))
+					return xas_error(xas);
 			}
 		}
 
-		if (!stop_split) {
-			folio_split_memcg_refs(folio, old_order, split_order);
-			split_page_owner(&folio->page, old_order, split_order);
-			pgalloc_tag_split(folio, old_order, split_order);
-
-			__split_folio_to_order(folio, old_order, split_order);
-		}
+		folio_split_memcg_refs(folio, old_order, split_order);
+		split_page_owner(&folio->page, old_order, split_order);
+		pgalloc_tag_split(folio, old_order, split_order);
+		__split_folio_to_order(folio, old_order, split_order);
 
+		if (is_anon)
+			mod_mthp_stat(old_order, MTHP_STAT_NR_ANON, -1);
 		/*
 		 * Iterate through after-split folios and update folio stats.
-		 * But in buddy allocator like split, the folio
-		 * containing the specified page is skipped until its order
-		 * is new_order, since the folio will be worked on in next
-		 * iteration.
 		 */
 		for (new_folio = folio; new_folio != end_folio; new_folio = next) {
 			next = folio_next(new_folio);
-			/*
-			 * for buddy allocator like split, new_folio containing
-			 * @split_at page could be split again, thus do not
-			 * change stats yet. Wait until new_folio's order is
-			 * @new_order or stop_split is set to true by the above
-			 * xas_split() failure.
-			 */
-			if (new_folio == page_folio(split_at)) {
+			if (new_folio == page_folio(split_at))
 				folio = new_folio;
-				if (split_order != new_order && !stop_split)
-					continue;
-			}
 			if (is_anon)
 				mod_mthp_stat(folio_order(new_folio),
 					      MTHP_STAT_NR_ANON, 1);
 		}
 	}
 
-	return ret;
+	return 0;
 }
 
 bool non_uniform_split_supported(struct folio *folio, unsigned int new_order,
Signature · 1 line
2.34.1
Acked-by on v3 2/4
#4 lore raw

Re: [Patch v3 2/4] mm/huge_memory: update folio stat after successful split

Quoted 10 lines · Wei Yang

The current implementation complicates this process:

* It iterates over the resulting new folios.
* It uses a flag (@stop_split) to conditionally skip updating the stat for the folio at @split_at during the loop.
* It then attempts to update the skipped stat on a subsequent failure path.

This logic is unnecessarily hard to follow.

Quoted 8 lines

This commit refactors the code to update the folio statistics only after a successful split. This makes the logic much cleaner and sets the stage for further simplification of the stat-handling code.

---

This was easier to digest. I hope I didn't miss anything (it's late ...).

Signature · 3 lines
Cheers

David / dhildenb
reviewer
#5 lore raw

Re: [Patch v3 2/4] mm/huge_memory: update folio stat after successful split

In reply to Wei Yang
Quoted 14 lines · Wei Yang

The current implementation complicates this process:

* It iterates over the resulting new folios.
* It uses a flag (@stop_split) to conditionally skip updating the stat for the folio at @split_at during the loop.
* It then attempts to update the skipped stat on a subsequent failure path.

This logic is unnecessarily hard to follow.

This commit refactors the code to update the folio statistics only after a successful split. This makes the logic much cleaner and sets the stage for further simplification of the stat-handling code.

Quoted 7 lines
---
 mm/huge_memory.c | 44 +++++++++++---------------------------------
 1 file changed, 11 insertions(+), 33 deletions(-)

LGTM. Reviewed-by: Zi Yan <[email protected]>

--
Best Regards,
Yan, Zi

Reviewed-by on v3 2/4
#6 lore raw

Re: [Patch v3 2/4] mm/huge_memory: update folio stat after successful split

In reply to Wei Yang
Quoted 10 lines

The current implementation complicates this process:

* It iterates over the resulting new folios.
* It uses a flag (@stop_split) to conditionally skip updating the stat for the folio at @split_at during the loop.
* It then attempts to update the skipped stat on a subsequent failure path.

This logic is unnecessarily hard to follow.

Quoted 7 lines

This commit refactors the code to update the folio statistics only after a successful split. This makes the logic much cleaner and sets the stage for further simplification of the stat-handling code.

---

LGTM.

--
Best Regards,
wang lian

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

Re: [Patch v3 2/4] mm/huge_memory: update folio stat after successful split

In reply to Wei Yang
Quoted 8 lines · Wei Yang

The current implementation complicates this process:

* It iterates over the resulting new folios.
* It uses a flag (@stop_split) to conditionally skip updating the stat for the folio at @split_at during the loop.
* It then attempts to update the skipped stat on a subsequent failure path.

Quoted 7 lines

This logic is unnecessarily hard to follow.

This commit refactors the code to update the folio statistics only after a successful split. This makes the logic much cleaner and sets the stage for further simplification of the stat-handling code.

LGTM, thanks for separating these! So:

Quoted 70 lines
Cc Zi Yan <[email protected]>
Cc wang lian <[email protected]>
---
 mm/huge_memory.c | 44 +++++++++++---------------------------------
 1 file changed, 11 insertions(+), 33 deletions(-)
mm/huge_memory.c +9 -22
index 6d82df4a88dc..b9a38dba8eb8 100644
@@ -3598,13 +3598,8 @@ static int __split_unmapped_folio(struct folio *folio, int new_order,
 	const bool is_anon = folio_test_anon(folio);
 	int order = folio_order(folio);
 	int start_order = uniform_split ? new_order : order - 1;
-	bool stop_split = false;
 	struct folio *next;
 	int split_order;
-	int ret = 0;
-
-	if (is_anon)
-		mod_mthp_stat(order, MTHP_STAT_NR_ANON, -1);
 
 	folio_clear_has_hwpoisoned(folio);
 
@@ -3613,7 +3608,7 @@ static int __split_unmapped_folio(struct folio *folio, int new_order,
 	 * folio is split to new_order directly.
 	 */
 	for (split_order = start_order;
-	     split_order >= new_order && !stop_split;
+	     split_order >= new_order;
 	     split_order--) {
 		struct folio *end_folio = folio_next(folio);
 		int old_order = folio_order(folio);
@@ -3636,49 +3631,32 @@ static int __split_unmapped_folio(struct folio *folio, int new_order,
 			else {
 				xas_set_order(xas, folio->index, split_order);
 				xas_try_split(xas, folio, old_order);
-				if (xas_error(xas)) {
-					ret = xas_error(xas);
-					stop_split = true;
-				}
+				if (xas_error(xas))
+					return xas_error(xas);
 			}
 		}
 
-		if (!stop_split) {
-			folio_split_memcg_refs(folio, old_order, split_order);
-			split_page_owner(&folio->page, old_order, split_order);
-			pgalloc_tag_split(folio, old_order, split_order);
-
-			__split_folio_to_order(folio, old_order, split_order);
-		}
+		folio_split_memcg_refs(folio, old_order, split_order);
+		split_page_owner(&folio->page, old_order, split_order);
+		pgalloc_tag_split(folio, old_order, split_order);
+		__split_folio_to_order(folio, old_order, split_order);
 
+		if (is_anon)
+			mod_mthp_stat(old_order, MTHP_STAT_NR_ANON, -1);
 		/*
 		 * Iterate through after-split folios and update folio stats.
-		 * But in buddy allocator like split, the folio
-		 * containing the specified page is skipped until its order
-		 * is new_order, since the folio will be worked on in next
-		 * iteration.
 		 */
 		for (new_folio = folio; new_folio != end_folio; new_folio = next) {
 			next = folio_next(new_folio);
-			/*

mm/huge_memory.c static int __split_unmapped_folio(struct folio *folio, int new_order, · line 3672

Quoted 10 lines
mm/huge_memory.c +1 -8
-			 * for buddy allocator like split, new_folio containing
-			 * @split_at page could be split again, thus do not
-			 * change stats yet. Wait until new_folio's order is
-			 * @new_order or stop_split is set to true by the above
-			 * xas_split() failure.
-			 */
-			if (new_folio == page_folio(split_at)) {
+			if (new_folio == page_folio(split_at))
 				folio = new_folio;
-				if (split_order != new_order && !stop_split)

OK I guess we don't need this as in !uniform_split case we use xas_set_order() to set the order, then try to split, and if an error arose we already would have handled, so split_order == new_order is guaranteed at this point, right?

Quoted 17 lines
mm/huge_memory.c +1 -4
-					continue;
-			}
 			if (is_anon)
 				mod_mthp_stat(folio_order(new_folio),
 					      MTHP_STAT_NR_ANON, 1);
 		}
 	}
 
-	return ret;
+	return 0;
 }
 
 bool non_uniform_split_supported(struct folio *folio, unsigned int new_order,
--

2.34.1

author
#8 lore raw

Re: [Patch v3 2/4] mm/huge_memory: update folio stat after successful split

On Fri, Oct 24, 2025 at 03:32:42PM +0100, Lorenzo Stoakes wrote:
[...]

Quoted 44 lines
Quoted 44 lines
mm/huge_memory.c +8 -23
@@ -3636,49 +3631,32 @@ static int __split_unmapped_folio(struct folio *folio, int new_order,
 			else {
 				xas_set_order(xas, folio->index, split_order);
 				xas_try_split(xas, folio, old_order);
-				if (xas_error(xas)) {
-					ret = xas_error(xas);
-					stop_split = true;
-				}
+				if (xas_error(xas))
+					return xas_error(xas);
 			}
 		}
 
-		if (!stop_split) {
-			folio_split_memcg_refs(folio, old_order, split_order);
-			split_page_owner(&folio->page, old_order, split_order);
-			pgalloc_tag_split(folio, old_order, split_order);
-
-			__split_folio_to_order(folio, old_order, split_order);
-		}
+		folio_split_memcg_refs(folio, old_order, split_order);
+		split_page_owner(&folio->page, old_order, split_order);
+		pgalloc_tag_split(folio, old_order, split_order);
+		__split_folio_to_order(folio, old_order, split_order);
 
+		if (is_anon)
+			mod_mthp_stat(old_order, MTHP_STAT_NR_ANON, -1);
 		/*
 		 * Iterate through after-split folios and update folio stats.
-		 * But in buddy allocator like split, the folio
-		 * containing the specified page is skipped until its order
-		 * is new_order, since the folio will be worked on in next
-		 * iteration.
 		 */
 		for (new_folio = folio; new_folio != end_folio; new_folio = next) {
 			next = folio_next(new_folio);
-			/*
-			 * for buddy allocator like split, new_folio containing
-			 * @split_at page could be split again, thus do not
-			 * change stats yet. Wait until new_folio's order is
-			 * @new_order or stop_split is set to true by the above
-			 * xas_split() failure.
-			 */
-			if (new_folio == page_folio(split_at)) {
Quoted 9 lines

mm/huge_memory.c static int __split_unmapped_folio(struct folio *folio, int new_order, · line 3672

Quoted 3 lines
mm/huge_memory.c +1 -1
+			if (new_folio == page_folio(split_at))
 				folio = new_folio;
-				if (split_order != new_order && !stop_split)

OK I guess we don't need this as in !uniform_split case we use xas_set_order() to set the order, then try to split, and if an error arose we already would have handled, so split_order == new_order is guaranteed at this point, right?

Sorry for the late reply, I was on travel.

In non-uniform splitting, @split_order represents an intermediate step, with @new_order as the final target size.

The check for split_order != new_order is necessary for the final split iteration.

If this check is absent, the logic that updates folio statistics for the newly split folios can be incorrectly bypassed when the !stop_split condition is met.

Not sure I get your point correctly.

Quoted 17 lines
Quoted 17 lines
mm/huge_memory.c +1 -4
-					continue;
-			}
 			if (is_anon)
 				mod_mthp_stat(folio_order(new_folio),
 					      MTHP_STAT_NR_ANON, 1);
 		}
 	}
 
-	return ret;
+	return 0;
 }
 
 bool non_uniform_split_supported(struct folio *folio, unsigned int new_order,
--

2.34.1

Signature · 2 lines
Wei Yang
Help you, Help me
author
#9 lore raw

PATCH v3 3/4 mm/huge_memory: optimize and simplify folio stat update after split

In reply to Wei Yang

The loop executed after a successful folio split currently has two combined responsibilities:

* updating statistics for the new folios
* determining the folio for the next split iteration.

This commit refactors the logic to directly calculate and update folio statistics, eliminating the need for the iteration step.

We can do this because all necessary information is already available:

* All resulting new folios have the same order, which is @split_order.
* The exact number of new folios can be calculated directly using @old_order and @split_order.
* The folio for the subsequent split is simply the one containing

    @split_at.

By leveraging this knowledge, we can achieve the stat update more cleanly and efficiently without the looping logic.

---
 mm/huge_memory.c | 18 ++++--------------
 1 file changed, 4 insertions(+), 14 deletions(-)
mm/huge_memory.c +4 -14
index b9a38dba8eb8..093b3ffb180f 100644
@@ -3598,7 +3598,6 @@ static int __split_unmapped_folio(struct folio *folio, int new_order,
 	const bool is_anon = folio_test_anon(folio);
 	int order = folio_order(folio);
 	int start_order = uniform_split ? new_order : order - 1;
-	struct folio *next;
 	int split_order;
 
 	folio_clear_has_hwpoisoned(folio);
@@ -3610,9 +3609,8 @@ static int __split_unmapped_folio(struct folio *folio, int new_order,
 	for (split_order = start_order;
 	     split_order >= new_order;
 	     split_order--) {
-		struct folio *end_folio = folio_next(folio);
 		int old_order = folio_order(folio);
-		struct folio *new_folio;
+		int nr_new_folios = 1UL << (old_order - split_order);
 
 		/* order-1 anonymous folio is not supported */
 		if (is_anon && split_order == 1)
@@ -3641,19 +3639,11 @@ static int __split_unmapped_folio(struct folio *folio, int new_order,
 		pgalloc_tag_split(folio, old_order, split_order);
 		__split_folio_to_order(folio, old_order, split_order);
 
-		if (is_anon)
+		if (is_anon) {
 			mod_mthp_stat(old_order, MTHP_STAT_NR_ANON, -1);
-		/*
-		 * Iterate through after-split folios and update folio stats.
-		 */
-		for (new_folio = folio; new_folio != end_folio; new_folio = next) {
-			next = folio_next(new_folio);
-			if (new_folio == page_folio(split_at))
-				folio = new_folio;
-			if (is_anon)
-				mod_mthp_stat(folio_order(new_folio),
-					      MTHP_STAT_NR_ANON, 1);
+			mod_mthp_stat(split_order, MTHP_STAT_NR_ANON, nr_new_folios);
 		}
+		folio = page_folio(split_at);
 	}
 
 	return 0;
Signature · 1 line
2.34.1
Acked-by on v3 3/4
#10 lore raw

Re: [Patch v3 3/4] mm/huge_memory: optimize and simplify folio stat update after split

Quoted 17 lines · Wei Yang

The loop executed after a successful folio split currently has two combined responsibilities:

* updating statistics for the new folios
* determining the folio for the next split iteration.

This commit refactors the logic to directly calculate and update folio statistics, eliminating the need for the iteration step.

We can do this because all necessary information is already available:

* All resulting new folios have the same order, which is @split_order.
* The exact number of new folios can be calculated directly using @old_order and @split_order.
* The folio for the subsequent split is simply the one containing

     @split_at.
Quoted 7 lines

By leveraging this knowledge, we can achieve the stat update more cleanly and efficiently without the looping logic.

---

Signature · 3 lines
Cheers

David / dhildenb
reviewer
#11 lore raw

Re: [Patch v3 3/4] mm/huge_memory: optimize and simplify folio stat update after split

In reply to Wei Yang
Quoted 20 lines · Wei Yang

The loop executed after a successful folio split currently has two combined responsibilities:

* updating statistics for the new folios
* determining the folio for the next split iteration.

This commit refactors the logic to directly calculate and update folio statistics, eliminating the need for the iteration step.

We can do this because all necessary information is already available:

* All resulting new folios have the same order, which is @split_order.
* The exact number of new folios can be calculated directly using @old_order and @split_order.
* The folio for the subsequent split is simply the one containing

    @split_at.

By leveraging this knowledge, we can achieve the stat update more cleanly and efficiently without the looping logic.

Quoted 7 lines
---
 mm/huge_memory.c | 18 ++++--------------
 1 file changed, 4 insertions(+), 14 deletions(-)

LGTM. Reviewed-by: Zi Yan <[email protected]>

--
Best Regards,
Yan, Zi

Reviewed-by on v3 3/4
#12 lore raw

Re: [Patch v3 3/4] mm/huge_memory: optimize and simplify folio stat update after split

In reply to Wei Yang
Quoted 17 lines

The loop executed after a successful folio split currently has two combined responsibilities:

* updating statistics for the new folios
* determining the folio for the next split iteration.

This commit refactors the logic to directly calculate and update folio statistics, eliminating the need for the iteration step.

We can do this because all necessary information is already available:

* All resulting new folios have the same order, which is @split_order.
* The exact number of new folios can be calculated directly using @old_order and @split_order.
* The folio for the subsequent split is simply the one containing @split_at.

Quoted 6 lines

By leveraging this knowledge, we can achieve the stat update more cleanly and efficiently without the looping logic.

---

LGTM.

--
Best Regards,
wang lian

Reviewed-by on v3 3/4
#13 lore raw

Re: [Patch v3 3/4] mm/huge_memory: optimize and simplify folio stat update after split

In reply to Wei Yang
Quoted 11 lines · Wei Yang

The loop executed after a successful folio split currently has two combined responsibilities:

* updating statistics for the new folios
* determining the folio for the next split iteration.

This commit refactors the logic to directly calculate and update folio statistics, eliminating the need for the iteration step.

We can do this because all necessary information is already available:

Quoted 9 lines

* All resulting new folios have the same order, which is @split_order.
* The exact number of new folios can be calculated directly using @old_order and @split_order.
* The folio for the subsequent split is simply the one containing

    @split_at.

By leveraging this knowledge, we can achieve the stat update more cleanly and efficiently without the looping logic.

Thanks for this + previous commit's great commit messages, much appreciated!

Quoted 1 line

This separation makes a really big difference, much appreciated.

LGTM, so:

Quoted 56 lines
Cc Zi Yan <[email protected]>
Cc wang lian <[email protected]>
---
 mm/huge_memory.c | 18 ++++--------------
 1 file changed, 4 insertions(+), 14 deletions(-)
mm/huge_memory.c +4 -15
index b9a38dba8eb8..093b3ffb180f 100644
@@ -3598,7 +3598,6 @@ static int __split_unmapped_folio(struct folio *folio, int new_order,
 	const bool is_anon = folio_test_anon(folio);
 	int order = folio_order(folio);
 	int start_order = uniform_split ? new_order : order - 1;
-	struct folio *next;
 	int split_order;
 
 	folio_clear_has_hwpoisoned(folio);
@@ -3610,9 +3609,8 @@ static int __split_unmapped_folio(struct folio *folio, int new_order,
 	for (split_order = start_order;
 	     split_order >= new_order;
 	     split_order--) {
-		struct folio *end_folio = folio_next(folio);
 		int old_order = folio_order(folio);
-		struct folio *new_folio;
+		int nr_new_folios = 1UL << (old_order - split_order);
 
 		/* order-1 anonymous folio is not supported */
 		if (is_anon && split_order == 1)
@@ -3641,19 +3639,11 @@ static int __split_unmapped_folio(struct folio *folio, int new_order,
 		pgalloc_tag_split(folio, old_order, split_order);
 		__split_folio_to_order(folio, old_order, split_order);
 
-		if (is_anon)
+		if (is_anon) {
 			mod_mthp_stat(old_order, MTHP_STAT_NR_ANON, -1);
-		/*
-		 * Iterate through after-split folios and update folio stats.
-		 */
-		for (new_folio = folio; new_folio != end_folio; new_folio = next) {
-			next = folio_next(new_folio);
-			if (new_folio == page_folio(split_at))
-				folio = new_folio;
-			if (is_anon)
-				mod_mthp_stat(folio_order(new_folio),
-					      MTHP_STAT_NR_ANON, 1);
+			mod_mthp_stat(split_order, MTHP_STAT_NR_ANON, nr_new_folios);
 		}
+		folio = page_folio(split_at);
 	}
 
 	return 0;
--

2.34.1

author
#14 lore raw

PATCH v3 4/4 mm/huge_memory: optimize old_order derivation during folio splitting

In reply to Wei Yang

Folio splitting requires both the folio's original order (@old_order) and the new target order (@split_order).

In the current implementation, @old_order is repeatedly retrieved using folio_order().

However, for every iteration after the first, the folio being split is the result of the previous split, meaning its order is already known to be equal to the previous iteration's @split_order.

This commit optimizes the logic:

* Instead of calling folio_order(), we now set @old_order directly to the value of @split_order from the previous iteration.

This change avoids unnecessary function calls and simplifies the loop setup.

Also it removes a check for non-existent case, since for uniform splitting we only do split when @split_order == @new_order.

---
 mm/huge_memory.c | 13 ++++++++-----
 1 file changed, 8 insertions(+), 5 deletions(-)
mm/huge_memory.c +8 -5
index 093b3ffb180f..a4fa8b0e5b5a 100644
@@ -3596,8 +3596,8 @@ static int __split_unmapped_folio(struct folio *folio, int new_order,
 		struct address_space *mapping, bool uniform_split)
 {
 	const bool is_anon = folio_test_anon(folio);
-	int order = folio_order(folio);
-	int start_order = uniform_split ? new_order : order - 1;
+	int old_order = folio_order(folio);
+	int start_order = uniform_split ? new_order : old_order - 1;
 	int split_order;
 
 	folio_clear_has_hwpoisoned(folio);
@@ -3609,14 +3609,11 @@ static int __split_unmapped_folio(struct folio *folio, int new_order,
 	for (split_order = start_order;
 	     split_order >= new_order;
 	     split_order--) {
-		int old_order = folio_order(folio);
 		int nr_new_folios = 1UL << (old_order - split_order);
 
 		/* order-1 anonymous folio is not supported */
 		if (is_anon && split_order == 1)
 			continue;
-		if (uniform_split && split_order != new_order)
-			continue;
 
 		if (mapping) {
 			/*
@@ -3643,7 +3640,13 @@ static int __split_unmapped_folio(struct folio *folio, int new_order,
 			mod_mthp_stat(old_order, MTHP_STAT_NR_ANON, -1);
 			mod_mthp_stat(split_order, MTHP_STAT_NR_ANON, nr_new_folios);
 		}
+		/*
+		 * If uniform split, the process is complete.
+		 * If non-uniform, continue splitting the folio at @split_at
+		 * as long as the next @split_order is >= @new_order.
+		 */
 		folio = page_folio(split_at);
+		old_order = split_order;
 	}
 
 	return 0;
Signature · 1 line
2.34.1
Acked-by on v3 4/4
#15 lore raw

Re: [Patch v3 4/4] mm/huge_memory: optimize old_order derivation during folio splitting

Quoted 18 lines · Wei Yang

Folio splitting requires both the folio's original order (@old_order) and the new target order (@split_order).

In the current implementation, @old_order is repeatedly retrieved using folio_order().

However, for every iteration after the first, the folio being split is the result of the previous split, meaning its order is already known to be equal to the previous iteration's @split_order.

This commit optimizes the logic:

* Instead of calling folio_order(), we now set @old_order directly to the value of @split_order from the previous iteration.

This change avoids unnecessary function calls and simplifies the loop setup.

Quoted 7 lines

Also it removes a check for non-existent case, since for uniform splitting we only do split when @split_order == @new_order.

---

Signature · 3 lines
Cheers

David / dhildenb
reviewer
#16 lore raw

Re: [Patch v3 4/4] mm/huge_memory: optimize old_order derivation during folio splitting

In reply to Wei Yang
Quoted 21 lines · Wei Yang

Folio splitting requires both the folio's original order (@old_order) and the new target order (@split_order).

In the current implementation, @old_order is repeatedly retrieved using folio_order().

However, for every iteration after the first, the folio being split is the result of the previous split, meaning its order is already known to be equal to the previous iteration's @split_order.

This commit optimizes the logic:

* Instead of calling folio_order(), we now set @old_order directly to the value of @split_order from the previous iteration.

This change avoids unnecessary function calls and simplifies the loop setup.

Also it removes a check for non-existent case, since for uniform splitting we only do split when @split_order == @new_order.

Quoted 7 lines
---
 mm/huge_memory.c | 13 ++++++++-----
 1 file changed, 8 insertions(+), 5 deletions(-)

LGTM. Thanks. Reviewed-by: Zi Yan <[email protected]>

--
Best Regards,
Yan, Zi

Reviewed-by on v3 4/4
#17 lore raw

Re: [Patch v3 4/4] mm/huge_memory: optimize old_order derivation during folio splitting

In reply to Wei Yang
Quoted 19 lines

Folio splitting requires both the folio's original order (@old_order) and the new target order (@split_order).

In the current implementation, @old_order is repeatedly retrieved using folio_order().

However, for every iteration after the first, the folio being split is the result of the previous split, meaning its order is already known to be equal to the previous iteration's @split_order.

This commit optimizes the logic:

* Instead of calling folio_order(), we now set @old_order directly to the value of @split_order from the previous iteration.

This change avoids unnecessary function calls and simplifies the loop setup.

Quoted 6 lines

Also it removes a check for non-existent case, since for uniform splitting we only do split when @split_order == @new_order.

---

LGTM.

--
Best Regards,
wang lian

Reviewed-by on v3 4/4
#18 lore raw

Re: [Patch v3 4/4] mm/huge_memory: optimize old_order derivation during folio splitting

In reply to Wei Yang
Quoted 15 lines · Wei Yang

Folio splitting requires both the folio's original order (@old_order) and the new target order (@split_order).

In the current implementation, @old_order is repeatedly retrieved using folio_order().

However, for every iteration after the first, the folio being split is the result of the previous split, meaning its order is already known to be equal to the previous iteration's @split_order.

This commit optimizes the logic:

* Instead of calling folio_order(), we now set @old_order directly to the value of @split_order from the previous iteration.

Quoted 7 lines

This change avoids unnecessary function calls and simplifies the loop setup.

Also it removes a check for non-existent case, since for uniform splitting we only do split when @split_order == @new_order.

Thanks the separation makes a HUGE difference, much appreciated. So:

Quoted 22 lines
Cc Zi Yan <[email protected]>
Cc wang lian <[email protected]>
---
 mm/huge_memory.c | 13 ++++++++-----
 1 file changed, 8 insertions(+), 5 deletions(-)
mm/huge_memory.c +2 -2
index 093b3ffb180f..a4fa8b0e5b5a 100644
@@ -3596,8 +3596,8 @@ static int __split_unmapped_folio(struct folio *folio, int new_order,
 		struct address_space *mapping, bool uniform_split)
 {
 	const bool is_anon = folio_test_anon(folio);
-	int order = folio_order(folio);
-	int start_order = uniform_split ? new_order : order - 1;
+	int old_order = folio_order(folio);
+	int start_order = uniform_split ? new_order : old_order - 1;
 	int split_order;
 
 	folio_clear_has_hwpoisoned(folio);

mm/huge_memory.c static int __split_unmapped_folio(struct folio *folio, int new_order, · line 3610

Quoted 2 lines
mm/huge_memory.c +0 -0
@@ -3609,14 +3609,11 @@ static int __split_unmapped_folio(struct folio *folio, int new_order,
 	for (split_order = start_order;
 	     split_order >= new_order;

A thought for the future - now things are simplified, it might be nice to just separate out the core of this loop and have the uniform split just call the split out function directly, and the non-uniform one do the loop.

As it's a bit gross in the uniform case we just let split_order go to new_order
- 1 to exit the loop.

BUT - let's please save that for another patch :)

This all looks fine.

Quoted 30 lines
mm/huge_memory.c +6 -4
 	     split_order--) {
-		int old_order = folio_order(folio);
 		int nr_new_folios = 1UL << (old_order - split_order);
 
 		/* order-1 anonymous folio is not supported */
 		if (is_anon && split_order == 1)
 			continue;
-		if (uniform_split && split_order != new_order)
-			continue;
 
 		if (mapping) {
 			/*
@@ -3643,7 +3640,13 @@ static int __split_unmapped_folio(struct folio *folio, int new_order,
 			mod_mthp_stat(old_order, MTHP_STAT_NR_ANON, -1);
 			mod_mthp_stat(split_order, MTHP_STAT_NR_ANON, nr_new_folios);
 		}
+		/*
+		 * If uniform split, the process is complete.
+		 * If non-uniform, continue splitting the folio at @split_at
+		 * as long as the next @split_order is >= @new_order.
+		 */
 		folio = page_folio(split_at);
+		old_order = split_order;
 	}
 
 	return 0;
--

2.34.1

reviewer
#19 lore raw

Re: [Patch v3 4/4] mm/huge_memory: optimize old_order derivation during folio splitting

Quoted 57 lines · Lorenzo Stoakes
Quoted 15 lines · Wei Yang

Folio splitting requires both the folio's original order (@old_order) and the new target order (@split_order).

In the current implementation, @old_order is repeatedly retrieved using folio_order().

However, for every iteration after the first, the folio being split is the result of the previous split, meaning its order is already known to be equal to the previous iteration's @split_order.

This commit optimizes the logic:

* Instead of calling folio_order(), we now set @old_order directly to the value of @split_order from the previous iteration.

Quoted 7 lines

This change avoids unnecessary function calls and simplifies the loop setup.

Also it removes a check for non-existent case, since for uniform splitting we only do split when @split_order == @new_order.

Thanks the separation makes a HUGE difference, much appreciated. So:

Quoted 22 lines
Cc Zi Yan <[email protected]>
Cc wang lian <[email protected]>
---
 mm/huge_memory.c | 13 ++++++++-----
 1 file changed, 8 insertions(+), 5 deletions(-)
mm/huge_memory.c +2 -2
index 093b3ffb180f..a4fa8b0e5b5a 100644
@@ -3596,8 +3596,8 @@ static int __split_unmapped_folio(struct folio *folio, int new_order,
 		struct address_space *mapping, bool uniform_split)
 {
 	const bool is_anon = folio_test_anon(folio);
-	int order = folio_order(folio);
-	int start_order = uniform_split ? new_order : order - 1;
+	int old_order = folio_order(folio);
+	int start_order = uniform_split ? new_order : old_order - 1;
 	int split_order;
 
 	folio_clear_has_hwpoisoned(folio);

mm/huge_memory.c static int __split_unmapped_folio(struct folio *folio, int new_order, · line 3610

Quoted 2 lines
mm/huge_memory.c +0 -0
@@ -3609,14 +3609,11 @@ static int __split_unmapped_folio(struct folio *folio, int new_order,
 	for (split_order = start_order;
 	     split_order >= new_order;

A thought for the future - now things are simplified, it might be nice to just separate out the core of this loop and have the uniform split just call the split out function directly, and the non-uniform one do the loop.

Quoted 2 lines

As it's a bit gross in the uniform case we just let split_order go to new_order
- 1 to exit the loop.

Yeah, something like:

if (uniform_split) {
	if (mapping)
		xas_split(xas, folio, old_order);
	split_folio_to_order(...);
	return 0;
}

for () {
...
split_folio_to_order(...);
...
}

where split_folio_to_order(...) just
split memcg, split page owner, pgalloc_tag_split, __split_folio_to_order, and stats update

Quoted 2 lines

BUT - let's please save that for another patch :)

I agree.

Quoted 28 lines

This all looks fine.

Quoted 25 lines
mm/huge_memory.c +6 -3
 	     split_order--) {
-		int old_order = folio_order(folio);
 		int nr_new_folios = 1UL << (old_order - split_order);
 
 		/* order-1 anonymous folio is not supported */
 		if (is_anon && split_order == 1)
 			continue;
-		if (uniform_split && split_order != new_order)
-			continue;
 
 		if (mapping) {
 			/*
@@ -3643,7 +3640,13 @@ static int __split_unmapped_folio(struct folio *folio, int new_order,
 			mod_mthp_stat(old_order, MTHP_STAT_NR_ANON, -1);
 			mod_mthp_stat(split_order, MTHP_STAT_NR_ANON, nr_new_folios);
 		}
+		/*
+		 * If uniform split, the process is complete.
+		 * If non-uniform, continue splitting the folio at @split_at
+		 * as long as the next @split_order is >= @new_order.
+		 */
 		folio = page_folio(split_at);
+		old_order = split_order;
 	}
 
Quoted 5 lines
Quoted 5 lines
mm/huge_memory.c +0 -1
 	return 0;
--

2.34.1

--
Best Regards,
Yan, Zi

#20 lore raw

Re: [Patch v3 4/4] mm/huge_memory: optimize old_order derivation during folio splitting

Quoted 41 lines · Zi Yan
Quoted 23 lines
Quoted 16 lines
mm/huge_memory.c +2 -2
index 093b3ffb180f..a4fa8b0e5b5a 100644
@@ -3596,8 +3596,8 @@ static int __split_unmapped_folio(struct folio *folio, int new_order,
 		struct address_space *mapping, bool uniform_split)
 {
 	const bool is_anon = folio_test_anon(folio);
-	int order = folio_order(folio);
-	int start_order = uniform_split ? new_order : order - 1;
+	int old_order = folio_order(folio);
+	int start_order = uniform_split ? new_order : old_order - 1;
 	int split_order;
 
 	folio_clear_has_hwpoisoned(folio);

mm/huge_memory.c static int __split_unmapped_folio(struct folio *folio, int new_order, · line 3610

Quoted 2 lines
mm/huge_memory.c +0 -0
@@ -3609,14 +3609,11 @@ static int __split_unmapped_folio(struct folio *folio, int new_order,
 	for (split_order = start_order;
 	     split_order >= new_order;

A thought for the future - now things are simplified, it might be nice to just separate out the core of this loop and have the uniform split just call the split out function directly, and the non-uniform one do the loop.

Quoted 2 lines

As it's a bit gross in the uniform case we just let split_order go to new_order
- 1 to exit the loop.

Yeah, something like:

if (uniform_split) {
	if (mapping)
		xas_split(xas, folio, old_order);
	split_folio_to_order(...);
	return 0;
}

for () {
...
split_folio_to_order(...);
...
}

Quoted 3 lines

where split_folio_to_order(...) just
split memcg, split page owner, pgalloc_tag_split, __split_folio_to_order, and stats update

Yeah exactly :)

Quoted 5 lines
Quoted 3 lines

BUT - let's please save that for another patch :)

I agree.

Yes, let's land this first and that can be a follow up!

Thanks again for your help on this series and sorry if I was a little too grumpy before :) I cut down caffeine drastically recently and you know... it's hard ;)

Cheers, Lorenzo

author
#21 lore raw

Re: [Patch v3 4/4] mm/huge_memory: optimize old_order derivation during folio splitting

In reply to Zi Yan
Quoted 1 line · Zi Yan

On 24 Oct 2025, at 10:46, Lorenzo Stoakes wrote:

[...]

Quoted 41 lines
Quoted 23 lines
Quoted 16 lines
mm/huge_memory.c +2 -2
index 093b3ffb180f..a4fa8b0e5b5a 100644
@@ -3596,8 +3596,8 @@ static int __split_unmapped_folio(struct folio *folio, int new_order,
 		struct address_space *mapping, bool uniform_split)
 {
 	const bool is_anon = folio_test_anon(folio);
-	int order = folio_order(folio);
-	int start_order = uniform_split ? new_order : order - 1;
+	int old_order = folio_order(folio);
+	int start_order = uniform_split ? new_order : old_order - 1;
 	int split_order;
 
 	folio_clear_has_hwpoisoned(folio);

mm/huge_memory.c static int __split_unmapped_folio(struct folio *folio, int new_order, · line 3610

Quoted 2 lines
mm/huge_memory.c +0 -0
@@ -3609,14 +3609,11 @@ static int __split_unmapped_folio(struct folio *folio, int new_order,
 	for (split_order = start_order;
 	     split_order >= new_order;

A thought for the future - now things are simplified, it might be nice to just separate out the core of this loop and have the uniform split just call the split out function directly, and the non-uniform one do the loop.

Quoted 2 lines

As it's a bit gross in the uniform case we just let split_order go to new_order
- 1 to exit the loop.

Yeah, something like:

if (uniform_split) {
	if (mapping)
		xas_split(xas, folio, old_order);
	split_folio_to_order(...);
	return 0;
}

for () {
...
split_folio_to_order(...);
...
}

Quoted 4 lines

where split_folio_to_order(...) just
split memcg, split page owner, pgalloc_tag_split, __split_folio_to_order, and stats update

This looks reasonable, while I found we already have split_folio_to_order().

Quoted 42 lines
Quoted 2 lines

BUT - let's please save that for another patch :)

I agree.

Quoted 28 lines

This all looks fine.

Quoted 25 lines
mm/huge_memory.c +6 -3
 	     split_order--) {
-		int old_order = folio_order(folio);
 		int nr_new_folios = 1UL << (old_order - split_order);
 
 		/* order-1 anonymous folio is not supported */
 		if (is_anon && split_order == 1)
 			continue;
-		if (uniform_split && split_order != new_order)
-			continue;
 
 		if (mapping) {
 			/*
@@ -3643,7 +3640,13 @@ static int __split_unmapped_folio(struct folio *folio, int new_order,
 			mod_mthp_stat(old_order, MTHP_STAT_NR_ANON, -1);
 			mod_mthp_stat(split_order, MTHP_STAT_NR_ANON, nr_new_folios);
 		}
+		/*
+		 * If uniform split, the process is complete.
+		 * If non-uniform, continue splitting the folio at @split_at
+		 * as long as the next @split_order is >= @new_order.
+		 */
 		folio = page_folio(split_at);
+		old_order = split_order;
 	}
 
Quoted 5 lines
Quoted 5 lines
mm/huge_memory.c +0 -1
 	return 0;
--

2.34.1

--
Best Regards,
Yan, Zi

Signature · 2 lines
Wei Yang
Help you, Help me
reviewer
#22 lore raw

Re: [Patch v3 4/4] mm/huge_memory: optimize old_order derivation during folio splitting

Quoted 44 lines · Wei Yang
Quoted 1 line · Zi Yan

On 24 Oct 2025, at 10:46, Lorenzo Stoakes wrote:

[...]

Quoted 41 lines
Quoted 23 lines
Quoted 16 lines
mm/huge_memory.c +2 -2
index 093b3ffb180f..a4fa8b0e5b5a 100644
@@ -3596,8 +3596,8 @@ static int __split_unmapped_folio(struct folio *folio, int new_order,
 		struct address_space *mapping, bool uniform_split)
 {
 	const bool is_anon = folio_test_anon(folio);
-	int order = folio_order(folio);
-	int start_order = uniform_split ? new_order : order - 1;
+	int old_order = folio_order(folio);
+	int start_order = uniform_split ? new_order : old_order - 1;
 	int split_order;
 
 	folio_clear_has_hwpoisoned(folio);

mm/huge_memory.c static int __split_unmapped_folio(struct folio *folio, int new_order, · line 3610

Quoted 2 lines
mm/huge_memory.c +0 -0
@@ -3609,14 +3609,11 @@ static int __split_unmapped_folio(struct folio *folio, int new_order,
 	for (split_order = start_order;
 	     split_order >= new_order;

A thought for the future - now things are simplified, it might be nice to just separate out the core of this loop and have the uniform split just call the split out function directly, and the non-uniform one do the loop.

Quoted 2 lines

As it's a bit gross in the uniform case we just let split_order go to new_order
- 1 to exit the loop.

Yeah, something like:

if (uniform_split) {
	if (mapping)
		xas_split(xas, folio, old_order);
	split_folio_to_order(...);
	return 0;
}

for () {
...
split_folio_to_order(...);
...
}

Quoted 6 lines
Quoted 4 lines

where split_folio_to_order(...) just
split memcg, split page owner, pgalloc_tag_split, __split_folio_to_order, and stats update

This looks reasonable, while I found we already have split_folio_to_order().

Then, we just need a new name, like __split_folio_and_update_stats(). I am bad at naming. Feel free to come up with a better one. :)

Quoted 33 lines
Quoted 33 lines
Quoted 3 lines

BUT - let's please save that for another patch :)

I agree.

Quoted 28 lines

This all looks fine.

Quoted 25 lines
mm/huge_memory.c +6 -3
 	     split_order--) {
-		int old_order = folio_order(folio);
 		int nr_new_folios = 1UL << (old_order - split_order);
 
 		/* order-1 anonymous folio is not supported */
 		if (is_anon && split_order == 1)
 			continue;
-		if (uniform_split && split_order != new_order)
-			continue;
 
 		if (mapping) {
 			/*
@@ -3643,7 +3640,13 @@ static int __split_unmapped_folio(struct folio *folio, int new_order,
 			mod_mthp_stat(old_order, MTHP_STAT_NR_ANON, -1);
 			mod_mthp_stat(split_order, MTHP_STAT_NR_ANON, nr_new_folios);
 		}
+		/*
+		 * If uniform split, the process is complete.
+		 * If non-uniform, continue splitting the folio at @split_at
+		 * as long as the next @split_order is >= @new_order.
+		 */
 		folio = page_folio(split_at);
+		old_order = split_order;
 	}
 
Quoted 14 lines
Quoted 10 lines
Quoted 5 lines
Quoted 5 lines
mm/huge_memory.c +0 -1
 	return 0;
--

2.34.1

--
Best Regards,
Yan, Zi

Signature · 2 lines
Wei Yang
Help you, Help me

--
Best Regards,
Yan, Zi

author
#23 lore raw

Re: [Patch v3 4/4] mm/huge_memory: optimize old_order derivation during folio splitting

Quoted 51 lines · Zi Yan
Quoted 49 lines · Wei Yang
Quoted 1 line · Zi Yan

On 24 Oct 2025, at 10:46, Lorenzo Stoakes wrote:

[...]

Quoted 45 lines
Quoted 23 lines
Quoted 16 lines
mm/huge_memory.c +2 -2
index 093b3ffb180f..a4fa8b0e5b5a 100644
@@ -3596,8 +3596,8 @@ static int __split_unmapped_folio(struct folio *folio, int new_order,
 		struct address_space *mapping, bool uniform_split)
 {
 	const bool is_anon = folio_test_anon(folio);
-	int order = folio_order(folio);
-	int start_order = uniform_split ? new_order : order - 1;
+	int old_order = folio_order(folio);
+	int start_order = uniform_split ? new_order : old_order - 1;
 	int split_order;
 
 	folio_clear_has_hwpoisoned(folio);

mm/huge_memory.c static int __split_unmapped_folio(struct folio *folio, int new_order, · line 3610

Quoted 2 lines
mm/huge_memory.c +0 -0
@@ -3609,14 +3609,11 @@ static int __split_unmapped_folio(struct folio *folio, int new_order,
 	for (split_order = start_order;
 	     split_order >= new_order;

A thought for the future - now things are simplified, it might be nice to just separate out the core of this loop and have the uniform split just call the split out function directly, and the non-uniform one do the loop.

Quoted 2 lines

As it's a bit gross in the uniform case we just let split_order go to new_order
- 1 to exit the loop.

Yeah, something like:

if (uniform_split) {
	if (mapping)
		xas_split(xas, folio, old_order);
	split_folio_to_order(...);
	return 0;
}

for () {
...
split_folio_to_order(...);
...
}

where split_folio_to_order(...) just
split memcg, split page owner, pgalloc_tag_split, __split_folio_to_order, and stats update

Quoted 5 lines
Quoted 1 line

This looks reasonable, while I found we already have split_folio_to_order().

Then, we just need a new name, like __split_folio_and_update_stats(). I am bad at naming. Feel free to come up with a better one. :)

Ok, let me have a try ... not good at it neither.

Signature · 2 lines
Wei Yang
Help you, Help me