Skip to contentSkip to conversation

mm/huge_memory: Modularize and simplify folio splitting paths

PATCH In review Wei Yang 3 participants 12 messages Nov 1, 2025 – Nov 7, 2025

Subsystems (2)
lore
Share & tools
lore

Wei Yang

author
#0 lore raw

cover mm/huge_memory: Modularize and simplify folio splitting paths

This two-patch series focuses on refactoring the core logic of folio splitting to improve code structure, clarity, and maintainability.

The first patch introduces a new consolidated helper function that groups all the required tasks involved in a folio split. The second patch then leverages this new helper to clearly separate the different execution paths for uniform and non-uniform splitting, which simplifies __split_unmapped_folio().

This series sets a better foundation for future optimizations and maintenance in the huge memory handling code.

Also selftests/split_huge_page_test pass.

Wei Yang (2):
mm/huge_memory: introduce __split_folio_and_update_stats() to
consolidate split task
mm/huge_memory: separate uniform/non uniform split logic in
__split_unmapped_folio()

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

PATCH 1/2 mm/huge_memory: introduce __split_folio_and_update_stats() to consolidate split task

The folio splitting process involves several related tasks that are executed together:

Adjusting memcg (memory control group) accounting. Updating page owner tracking.
Splitting the folio to the target size (new_order). Updating necessary folio statistics.

This commit introduces the new helper function, __split_folio_and_update_stats(), to gather all these tasks. This consolidation improves modularity and is a necessary preparation step for further cleanup and simplification of the surrounding folio splitting logic.

Cc Lorenzo Stoakes <[email protected]>
Cc Zi Yan <[email protected]>
---
 mm/huge_memory.c | 28 +++++++++++++++++-----------
 1 file changed, 17 insertions(+), 11 deletions(-)
mm/huge_memory.c +17 -11
index c37fe6ad0c96..abde0f1aa8ff 100644
@@ -3567,6 +3567,22 @@ static void __split_folio_to_order(struct folio *folio, int old_order,
 		ClearPageCompound(&folio->page);
 }
 
+static void __split_folio_and_update_stats(struct folio *folio, int old_order,
+		int new_order, bool is_anon)
+{
+	int nr_new_folios = 1UL << (old_order - new_order);
+
+	folio_split_memcg_refs(folio, old_order, new_order);
+	split_page_owner(&folio->page, old_order, new_order);
+	pgalloc_tag_split(folio, old_order, new_order);
+	__split_folio_to_order(folio, old_order, new_order);
+
+	if (is_anon) {
+		mod_mthp_stat(old_order, MTHP_STAT_NR_ANON, -1);
+		mod_mthp_stat(new_order, MTHP_STAT_NR_ANON, nr_new_folios);
+	}
+}
+
 /**
  * __split_unmapped_folio() - splits an unmapped @folio to lower order folios in
  * two ways: uniform split or non-uniform split.
@@ -3623,8 +3639,6 @@ static int __split_unmapped_folio(struct folio *folio, int new_order,
 	for (split_order = start_order;
 	     split_order >= new_order;
 	     split_order--) {
-		int nr_new_folios = 1UL << (old_order - split_order);
-
 		/* order-1 anonymous folio is not supported */
 		if (is_anon && split_order == 1)
 			continue;
@@ -3645,15 +3659,7 @@ static int __split_unmapped_folio(struct folio *folio, int new_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);
-			mod_mthp_stat(split_order, MTHP_STAT_NR_ANON, nr_new_folios);
-		}
+		__split_folio_and_update_stats(folio, old_order, split_order, is_anon);
 		/*
 		 * If uniform split, the process is complete.
 		 * If non-uniform, continue splitting the folio at @split_at
Signature · 1 line
2.34.1
#2 lore raw

Re: [PATCH 1/2] mm/huge_memory: introduce __split_folio_and_update_stats() to consolidate split task

Quoted 26 lines · Wei Yang

The folio splitting process involves several related tasks that are executed together:

Adjusting memcg (memory control group) accounting. Updating page owner tracking.
Splitting the folio to the target size (new_order). Updating necessary folio statistics.

This commit introduces the new helper function, __split_folio_and_update_stats(), to gather all these tasks. This consolidation improves modularity and is a necessary preparation step for further cleanup and simplification of the surrounding folio splitting logic.

Cc Lorenzo Stoakes <[email protected]>
Cc Zi Yan <[email protected]>
---
  mm/huge_memory.c | 28 +++++++++++++++++-----------
  1 file changed, 17 insertions(+), 11 deletions(-)
mm/huge_memory.c +0 -0
index c37fe6ad0c96..abde0f1aa8ff 100644

mm/huge_memory.c static void __split_folio_to_order(struct folio *folio, int old_order, · line 3571

Quoted 5 lines
mm/huge_memory.c +2 -0
@@ -3567,6 +3567,22 @@ static void __split_folio_to_order(struct folio *folio, int old_order,
  		ClearPageCompound(&folio->page);
  }
 
+static void __split_folio_and_update_stats(struct folio *folio, int old_order,
+		int new_order, bool is_anon)

Is there any good reason we want to pass things like "is_anon" instead of just querying it in that helper?

mm/huge_memory.c static void __split_folio_to_order(struct folio *folio, int old_order, · line 3573

Quoted 2 lines
mm/huge_memory.c +2 -0
+{
+	int nr_new_folios = 1UL << (old_order - new_order);

Could be const.

Signature · 3 lines
Cheers

David
#3 lore raw

Re: [PATCH 1/2] mm/huge_memory: introduce __split_folio_and_update_stats() to consolidate split task

Quoted 37 lines · David Hildenbrand (Red Hat)
Quoted 26 lines · Wei Yang

The folio splitting process involves several related tasks that are executed together:

Adjusting memcg (memory control group) accounting. Updating page owner tracking.
Splitting the folio to the target size (new_order). Updating necessary folio statistics.

This commit introduces the new helper function, __split_folio_and_update_stats(), to gather all these tasks. This consolidation improves modularity and is a necessary preparation step for further cleanup and simplification of the surrounding folio splitting logic.

Cc Lorenzo Stoakes <[email protected]>
Cc Zi Yan <[email protected]>
---
   mm/huge_memory.c | 28 +++++++++++++++++-----------
   1 file changed, 17 insertions(+), 11 deletions(-)
mm/huge_memory.c +0 -0
index c37fe6ad0c96..abde0f1aa8ff 100644

mm/huge_memory.c static void __split_folio_to_order(struct folio *folio, int old_order, · line 3571

Quoted 5 lines
mm/huge_memory.c +2 -0
@@ -3567,6 +3567,22 @@ static void __split_folio_to_order(struct folio *folio, int old_order,
   		ClearPageCompound(&folio->page);
   }
 
+static void __split_folio_and_update_stats(struct folio *folio, int old_order,
+		int new_order, bool is_anon)

Is there any good reason we want to pass things like "is_anon" instead of just querying it in that helper?

Quoted 6 lines

mm/huge_memory.c static void __split_folio_to_order(struct folio *folio, int old_order, · line 3573

Quoted 2 lines
mm/huge_memory.c +2 -0
+{
+	int nr_new_folios = 1UL << (old_order - new_order);

Could be const.

Looking at it again, and that we are not reusing this function on patch #2, I am not sure factoring this out is really helpful at this point?

Signature · 3 lines
Cheers

David
#4 lore raw

Re: [PATCH 1/2] mm/huge_memory: introduce __split_folio_and_update_stats() to consolidate split task

Quoted 45 lines · David Hildenbrand (Red Hat)
Quoted 37 lines · David Hildenbrand (Red Hat)
Quoted 26 lines · Wei Yang

The folio splitting process involves several related tasks that are executed together:

Adjusting memcg (memory control group) accounting. Updating page owner tracking.
Splitting the folio to the target size (new_order). Updating necessary folio statistics.

This commit introduces the new helper function, __split_folio_and_update_stats(), to gather all these tasks. This consolidation improves modularity and is a necessary preparation step for further cleanup and simplification of the surrounding folio splitting logic.

Cc Lorenzo Stoakes <[email protected]>
Cc Zi Yan <[email protected]>
---
    mm/huge_memory.c | 28 +++++++++++++++++-----------
    1 file changed, 17 insertions(+), 11 deletions(-)
mm/huge_memory.c +0 -0
index c37fe6ad0c96..abde0f1aa8ff 100644

mm/huge_memory.c static void __split_folio_to_order(struct folio *folio, int old_order, · line 3571

Quoted 5 lines
mm/huge_memory.c +2 -0
@@ -3567,6 +3567,22 @@ static void __split_folio_to_order(struct folio *folio, int old_order,
    		ClearPageCompound(&folio->page);
    }
 
+static void __split_folio_and_update_stats(struct folio *folio, int old_order,
+		int new_order, bool is_anon)

Is there any good reason we want to pass things like "is_anon" instead of just querying it in that helper?

Quoted 6 lines

mm/huge_memory.c static void __split_folio_to_order(struct folio *folio, int old_order, · line 3573

Quoted 2 lines
mm/huge_memory.c +2 -0
+{
+	int nr_new_folios = 1UL << (old_order - new_order);

Could be const.

Quoted 2 lines

Looking at it again, and that we are not reusing this function on patch #2, I am not sure factoring this out is really helpful at this point?

Ignore that, I was briefly confused by patch #2 :)

Signature · 3 lines
Cheers

David
author
#5 lore raw

Re: [PATCH 1/2] mm/huge_memory: introduce __split_folio_and_update_stats() to consolidate split task

Quoted 45 lines · David Hildenbrand (Red Hat)
Quoted 44 lines · David Hildenbrand (Red Hat)
Quoted 42 lines · David Hildenbrand (Red Hat)
Quoted 26 lines · Wei Yang

The folio splitting process involves several related tasks that are executed together:

Adjusting memcg (memory control group) accounting. Updating page owner tracking.
Splitting the folio to the target size (new_order). Updating necessary folio statistics.

This commit introduces the new helper function, __split_folio_and_update_stats(), to gather all these tasks. This consolidation improves modularity and is a necessary preparation step for further cleanup and simplification of the surrounding folio splitting logic.

Cc Lorenzo Stoakes <[email protected]>
Cc Zi Yan <[email protected]>
---
    mm/huge_memory.c | 28 +++++++++++++++++-----------
    1 file changed, 17 insertions(+), 11 deletions(-)
mm/huge_memory.c +0 -0
index c37fe6ad0c96..abde0f1aa8ff 100644

mm/huge_memory.c static void __split_folio_to_order(struct folio *folio, int old_order, · line 3570

Quoted 4 lines
mm/huge_memory.c +2 -0
@@ -3567,6 +3567,22 @@ static void __split_folio_to_order(struct folio *folio, int old_order,
    		ClearPageCompound(&folio->page);
    }
+static void __split_folio_and_update_stats(struct folio *folio, int old_order,
+		int new_order, bool is_anon)

Is there any good reason we want to pass things like "is_anon" instead of just querying it in that helper?

mm/huge_memory.c static void __split_folio_to_order(struct folio *folio, int old_order, · line 3573

Quoted 2 lines
mm/huge_memory.c +2 -0
+{
+	int nr_new_folios = 1UL << (old_order - new_order);

Could be const.

Quoted 5 lines
Quoted 2 lines

Looking at it again, and that we are not reusing this function on patch #2, I am not sure factoring this out is really helpful at this point?

Ignore that, I was briefly confused by patch #2 :)

Hi, David

Would you mind be more specific on which part confuse you?

Quoted 4 lines
Signature · 3 lines
Cheers

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

Re: [PATCH 1/2] mm/huge_memory: introduce __split_folio_and_update_stats() to consolidate split task

In reply to David Hildenbrand (Red Hat)
Quoted 22 lines · David Hildenbrand (Red Hat)
Quoted 21 lines · Wei Yang

The folio splitting process involves several related tasks that are executed together:

Adjusting memcg (memory control group) accounting. Updating page owner tracking.
Splitting the folio to the target size (new_order). Updating necessary folio statistics.

This commit introduces the new helper function, __split_folio_and_update_stats(), to gather all these tasks. This consolidation improves modularity and is a necessary preparation step for further cleanup and simplification of the surrounding folio splitting logic.

Cc Lorenzo Stoakes <[email protected]>
Cc Zi Yan <[email protected]>
---
  mm/huge_memory.c | 28 +++++++++++++++++-----------
  1 file changed, 17 insertions(+), 11 deletions(-)
Quoted 13 lines

mm/huge_memory.c static void __split_folio_to_order(struct folio *folio, int old_order, · line 3570

Quoted 9 lines
mm/huge_memory.c +2 -0
index c37fe6ad0c96..abde0f1aa8ff 100644
@@ -3567,6 +3567,22 @@ static void __split_folio_to_order(struct folio *folio, int old_order,
  		ClearPageCompound(&folio->page);
  }
+static void __split_folio_and_update_stats(struct folio *folio, int old_order,
+		int new_order, bool is_anon)

Is there any good reason we want to pass things like "is_anon" instead of just querying it in that helper?

The reason is during split, folio's attribute will not change. In __split_unmapped_folio() it is queried and set as const.

Otherwise we need to query it on each split.

Quoted 6 lines

mm/huge_memory.c static void __split_folio_to_order(struct folio *folio, int old_order, · line 3573

Quoted 3 lines
mm/huge_memory.c +2 -0
+{
+	int nr_new_folios = 1UL << (old_order - new_order);

Could be const.

Will add it.

Quoted 5 lines
Signature · 4 lines
-- 
Cheers

David
Signature · 2 lines
Wei Yang
Help you, Help me
#7 lore raw

Re: [PATCH 1/2] mm/huge_memory: introduce __split_folio_and_update_stats() to consolidate split task

Quoted 37 lines · Wei Yang
Quoted 22 lines · David Hildenbrand (Red Hat)
Quoted 21 lines · Wei Yang

The folio splitting process involves several related tasks that are executed together:

Adjusting memcg (memory control group) accounting. Updating page owner tracking.
Splitting the folio to the target size (new_order). Updating necessary folio statistics.

This commit introduces the new helper function, __split_folio_and_update_stats(), to gather all these tasks. This consolidation improves modularity and is a necessary preparation step for further cleanup and simplification of the surrounding folio splitting logic.

Cc Lorenzo Stoakes <[email protected]>
Cc Zi Yan <[email protected]>
---
   mm/huge_memory.c | 28 +++++++++++++++++-----------
   1 file changed, 17 insertions(+), 11 deletions(-)
Quoted 13 lines

mm/huge_memory.c static void __split_folio_to_order(struct folio *folio, int old_order, · line 3570

Quoted 9 lines
mm/huge_memory.c +2 -0
index c37fe6ad0c96..abde0f1aa8ff 100644
@@ -3567,6 +3567,22 @@ static void __split_folio_to_order(struct folio *folio, int old_order,
   		ClearPageCompound(&folio->page);
   }
+static void __split_folio_and_update_stats(struct folio *folio, int old_order,
+		int new_order, bool is_anon)

Is there any good reason we want to pass things like "is_anon" instead of just querying it in that helper?

Quoted 4 lines

The reason is during split, folio's attribute will not change. In __split_unmapped_folio() it is queried and set as const.

Otherwise we need to query it on each split.

These checks are extremely cheap. Do we really care?

Signature · 3 lines
Cheers

David
author
#8 lore raw

Re: [PATCH 1/2] mm/huge_memory: introduce __split_folio_and_update_stats() to consolidate split task

Quoted 38 lines · David Hildenbrand (Red Hat)
Quoted 37 lines · Wei Yang
Quoted 35 lines · David Hildenbrand (Red Hat)
Quoted 26 lines · Wei Yang

The folio splitting process involves several related tasks that are executed together:

Adjusting memcg (memory control group) accounting. Updating page owner tracking.
Splitting the folio to the target size (new_order). Updating necessary folio statistics.

This commit introduces the new helper function, __split_folio_and_update_stats(), to gather all these tasks. This consolidation improves modularity and is a necessary preparation step for further cleanup and simplification of the surrounding folio splitting logic.

Cc Lorenzo Stoakes <[email protected]>
Cc Zi Yan <[email protected]>
---
   mm/huge_memory.c | 28 +++++++++++++++++-----------
   1 file changed, 17 insertions(+), 11 deletions(-)
mm/huge_memory.c +0 -0
index c37fe6ad0c96..abde0f1aa8ff 100644

mm/huge_memory.c static void __split_folio_to_order(struct folio *folio, int old_order, · line 3570

Quoted 4 lines
mm/huge_memory.c +2 -0
@@ -3567,6 +3567,22 @@ static void __split_folio_to_order(struct folio *folio, int old_order,
   		ClearPageCompound(&folio->page);
   }
+static void __split_folio_and_update_stats(struct folio *folio, int old_order,
+		int new_order, bool is_anon)

Is there any good reason we want to pass things like "is_anon" instead of just querying it in that helper?

Quoted 7 lines
Quoted 4 lines

The reason is during split, folio's attribute will not change. In __split_unmapped_folio() it is queried and set as const.

Otherwise we need to query it on each split.

These checks are extremely cheap. Do we really care?

Ok, will remove it.

Quoted 4 lines
Signature · 3 lines
Cheers

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

PATCH 2/2 mm/huge_memory: separate uniform/non uniform split logic in __split_unmapped_folio()

In reply to Wei Yang

By utilizing the newly introduced __split_folio_and_update_stats() helper function, we can now clearly separate the logic for uniform and non-uniform folio splitting within __split_unmapped_folio().

This refactoring greatly simplifies the code by creating two distinct execution paths:

* Uniform Split: Directly calls __split_folio_and_update_stats() once to achieve the @new_order in a single operation.

* Non-Uniform Split: Continues to use a loop to iteratively split the folio to a single lower order at a time, eventually reaching the @new_order.

This separation improves code clarity and maintainability.

Cc Lorenzo Stoakes <[email protected]>
Cc Zi Yan <[email protected]>
---
 mm/huge_memory.c | 31 ++++++++++++++++---------------
 1 file changed, 16 insertions(+), 15 deletions(-)
mm/huge_memory.c +16 -15
index abde0f1aa8ff..c4fb84cedbe0 100644
@@ -3629,14 +3629,20 @@ static int __split_unmapped_folio(struct folio *folio, int new_order,
 {
 	const bool is_anon = folio_test_anon(folio);
 	int old_order = folio_order(folio);
-	int start_order = uniform_split ? new_order : old_order - 1;
 	int split_order;
 
+	/* For uniform split, folio is split to new_order directly. */
+	if (uniform_split) {
+		if (mapping)
+			xas_split(xas, folio, old_order);
+		__split_folio_and_update_stats(folio, old_order, new_order, is_anon);
+		return 0;
+	}
+
 	/*
-	 * split to new_order one order at a time. For uniform split,
-	 * folio is split to new_order directly.
+	 * For non-uniform, split to new_order one order at a time.
 	 */
-	for (split_order = start_order;
+	for (split_order = old_order - 1;
 	     split_order >= new_order;
 	     split_order--) {
 		/* order-1 anonymous folio is not supported */
@@ -3649,21 +3655,16 @@ static int __split_unmapped_folio(struct folio *folio, int new_order,
 			 * irq is disabled to allocate enough memory, whereas
 			 * non-uniform split can handle ENOMEM.
 			 */
-			if (uniform_split)
-				xas_split(xas, folio, old_order);
-			else {
-				xas_set_order(xas, folio->index, split_order);
-				xas_try_split(xas, folio, old_order);
-				if (xas_error(xas))
-					return xas_error(xas);
-			}
+			xas_set_order(xas, folio->index, split_order);
+			xas_try_split(xas, folio, old_order);
+			if (xas_error(xas))
+				return xas_error(xas);
 		}
 
 		__split_folio_and_update_stats(folio, old_order, split_order, is_anon);
 		/*
-		 * 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.
+		 * 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;
Signature · 1 line
2.34.1
author
#10 lore raw
In reply to Wei Yang
Quoted 9 lines · Wei Yang

This two-patch series focuses on refactoring the core logic of folio splitting to improve code structure, clarity, and maintainability.

The first patch introduces a new consolidated helper function that groups all the required tasks involved in a folio split. The second patch then leverages this new helper to clearly separate the different execution paths for uniform and non-uniform splitting, which simplifies __split_unmapped_folio().

Quoted 4 lines

This series sets a better foundation for future optimizations and maintenance in the huge memory handling code.

Also selftests/split_huge_page_test pass.

Hi, Lorenzo , Zi

This patch set is based on your suggestion from [1].

Not sure you still like it?

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

Quoted 12 lines

Wei Yang (2):
mm/huge_memory: introduce __split_folio_and_update_stats() to
consolidate split task
mm/huge_memory: separate uniform/non uniform split logic in
__split_unmapped_folio()

mm/huge_memory.c | 59 +++++++++++++++++++++++++++---------------------

1 file changed, 33 insertions(+), 26 deletions(-)
Signature · 1 line
2.34.1
Signature · 2 lines
Wei Yang
Help you, Help me
reviewer
#11 lore raw
Quoted 15 lines · Wei Yang
Quoted 9 lines · Wei Yang

This two-patch series focuses on refactoring the core logic of folio splitting to improve code structure, clarity, and maintainability.

The first patch introduces a new consolidated helper function that groups all the required tasks involved in a folio split. The second patch then leverages this new helper to clearly separate the different execution paths for uniform and non-uniform splitting, which simplifies __split_unmapped_folio().

Quoted 4 lines

This series sets a better foundation for future optimizations and maintenance in the huge memory handling code.

Also selftests/split_huge_page_test pass.

Quoted 8 lines

Hi, Lorenzo , Zi

This patch set is based on your suggestion from [1].

Not sure you still like it?

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

Can you address David’s concern and update the patches? I will take a look then.

Thanks.

Quoted 7 lines
Quoted 7 lines

Wei Yang (2):
mm/huge_memory: introduce __split_folio_and_update_stats() to
consolidate split task
mm/huge_memory: separate uniform/non uniform split logic in
__split_unmapped_folio()

Quoted 9 lines
Quoted 5 lines

mm/huge_memory.c | 59 +++++++++++++++++++++++++++---------------------

1 file changed, 33 insertions(+), 26 deletions(-)
Signature · 1 line
2.34.1
Signature · 2 lines
Wei Yang
Help you, Help me

Best Regards,
Yan, Zi