Skip to contentSkip to conversation

mm/huge_memory: cleanup __split_unmapped_folio()

PATCH Superseded Wei Yang 6 participants 26 messages Oct 16, 2025 – Oct 21, 2025

Subsystems (2)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Wei Yang

author
#0 lore raw

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

v2:
* merge patch 2-5

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

Wei Yang (2):
mm/huge_memory: cache folio attribute in __split_unmapped_folio() mm/huge_memory: Optimize and simplify __split_unmapped_folio() logic

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

PATCH v2 1/2 mm/huge_memory: cache folio attribute in __split_unmapped_folio()

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.

---
 mm/huge_memory.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
mm/huge_memory.c +4 -3
index 3c74227cc847..4b2d5a7e5c8e 100644
@@ -3527,6 +3527,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)
 {
+	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;
@@ -3534,7 +3535,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);
@@ -3551,7 +3552,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;
@@ -3603,7 +3604,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 v2 1/2
#2 lore raw

Re: [Patch v2 1/2] mm/huge_memory: cache folio attribute in __split_unmapped_folio()

Quoted 4 lines · Wei Yang

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

Quoted 8 lines

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

---

Acked-by on v2 1/2
#3 lore raw

Re: [Patch v2 1/2] mm/huge_memory: cache folio attribute in __split_unmapped_folio()

In reply to Wei Yang
Quoted 19 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.

---
  mm/huge_memory.c | 7 ++++---
  1 file changed, 4 insertions(+), 3 deletions(-)
mm/huge_memory.c +0 -0
index 3c74227cc847..4b2d5a7e5c8e 100644

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

Quoted 4 lines
mm/huge_memory.c +1 -0
@@ -3527,6 +3527,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)
  {
+	bool is_anon = folio_test_anon(folio);

Can be const.

Signature · 3 lines
Cheers

David / dhildenb
#4 lore raw

Re: [Patch v2 1/2] mm/huge_memory: cache folio attribute in __split_unmapped_folio()

In reply to Wei Yang

I don't like 'cache' here. You're not cache-ing anything, you're relying on an attribute remaining constant.

Something like 'avoid reinvoking folio_test_anon()' would be simpler.

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

---
 mm/huge_memory.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
mm/huge_memory.c +0 -0
index 3c74227cc847..4b2d5a7e5c8e 100644

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

Quoted 4 lines
mm/huge_memory.c +1 -0
@@ -3527,6 +3527,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)
 {
+	bool is_anon = folio_test_anon(folio);

As David said, make this const please.

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;
@@ -3534,7 +3535,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);
@@ -3551,7 +3552,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;
@@ -3603,7 +3604,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
#5 lore raw

Re: [Patch v2 1/2] mm/huge_memory: cache folio attribute in __split_unmapped_folio()

Quoted 5 lines · Lorenzo Stoakes

I don't like 'cache' here. You're not cache-ing anything, you're relying on an attribute remaining constant.

Something like 'avoid reinvoking folio_test_anon()' would be simpler.

Will adjust it.

Quoted 15 lines · Wei Yang
Quoted 14 lines

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.

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

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

Quoted 9 lines
mm/huge_memory.c +1 -0
index 3c74227cc847..4b2d5a7e5c8e 100644
@@ -3527,6 +3527,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)
 {
+	bool is_anon = folio_test_anon(folio);

As David said, make this const please.

Will do.

Quoted 34 lines
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;
@@ -3534,7 +3535,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);
@@ -3551,7 +3552,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;
@@ -3603,7 +3604,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

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

PATCH v2 2/2 mm/huge_memory: Optimize and simplify __split_unmapped_folio() logic

In reply to Wei Yang

Existing __split_unmapped_folio() code splits the given folio and update stats, but it is complicated to understand.

After simplification, __split_unmapped_folio() directly calculate and update the folio statistics upon a successful split:

* All resulting folios are @split_order.

* The number of new folios are calculated directly from @old_order and @split_order.

* The folio for the next split is identified as the one containing @split_at.

* An xas_try_split() error is returned directly without worrying about stats updates.

---
v2:
* merge patch 2-5
* retain start_order
* new_folios -> nr_new_folios
* add a comment at the end of the loop

---
 mm/huge_memory.c | 66 ++++++++++++++----------------------------------
 1 file changed, 19 insertions(+), 47 deletions(-)
mm/huge_memory.c +19 -47
index 4b2d5a7e5c8e..68e851f5fcb2 100644
@@ -3528,15 +3528,9 @@ static int __split_unmapped_folio(struct folio *folio, int new_order,
 		struct address_space *mapping, bool uniform_split)
 {
 	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 old_order = folio_order(folio);
+	int start_order = uniform_split ? new_order : old_order - 1;
 	int split_order;
-	int ret = 0;
-
-	if (is_anon)
-		mod_mthp_stat(order, MTHP_STAT_NR_ANON, -1);
 
 	folio_clear_has_hwpoisoned(folio);
 
@@ -3545,17 +3539,13 @@ 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);
-		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)
 			continue;
-		if (uniform_split && split_order != new_order)
-			continue;
 
 		if (mapping) {
 			/*
@@ -3568,49 +3558,31 @@ 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);
+		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);
 
-			__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);
 		}
 
 		/*
-		 * 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 uniform split, we have finished the job.
+		 * For non-uniform split, we assign folio to the one the one
+		 * containing @split_at and assign @old_order to @split_order.
 		 */
-		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)) {
-				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);
-		}
+		folio = page_folio(split_at);
+		old_order = split_order;
 	}
 
-	return ret;
+	return 0;
 }
 
 bool non_uniform_split_supported(struct folio *folio, unsigned int new_order,
Signature · 1 line
2.34.1
Reviewed-by on v2 2/2
#7 lore raw

Re: [Patch v2 2/2] mm/huge_memory: Optimize and simplify __split_unmapped_folio() logic

Quoted 9 lines

Existing __split_unmapped_folio() code splits the given folio and update stats, but it is complicated to understand.

After simplification, __split_unmapped_folio() directly calculate and update the folio statistics upon a successful split:

* All resulting folios are @split_order.

* The number of new folios are calculated directly from @old_order

and @split_order.

Quoted 1 line

* The folio for the next split is identified as the one containing

@split_at.

Quoted 1 line

* An xas_try_split() error is returned directly without worrying

about stats updates.

Quoted 3 lines

The simplification looks great and makes the logic much easier to follow. The new comment you added is also very helpful for understanding the difference between the two split modes.

LGTM.

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

Quoted 3 lines
mm/huge_memory.c +3 -0
+		 * For uniform split, we have finished the job.
+		 * For non-uniform split, we assign folio to the one the one
+		 * containing @split_at and assign @old_order to @split_order.

--
Best Regards,
wang lian

#8 lore raw

Re: [Patch v2 2/2] mm/huge_memory: Optimize and simplify __split_unmapped_folio() logic

In reply to Wei Yang
Quoted 4 lines · Wei Yang

Existing __split_unmapped_folio() code splits the given folio and update stats, but it is complicated to understand.

After simplification, __split_unmapped_folio() directly calculate and

s/calculate/calculates/

Quoted 1 line

update the folio statistics upon a successful split:

s/update/updates/

Quoted 5 lines

* All resulting folios are @split_order.

* The number of new folios are calculated directly from @old_order and @split_order.

That makes sense.

Quoted 4 lines

* The folio for the next split is identified as the one containing @split_at.

That as well.

Quoted 2 lines

* An xas_try_split() error is returned directly without worrying about stats updates.

Why is that change ok?

Quoted 11 lines

---
v2:
* merge patch 2-5
* retain start_order
* new_folios -> nr_new_folios
* add a comment at the end of the loop

Quoted 3 lines
---
  mm/huge_memory.c | 66 ++++++++++++++----------------------------------
  1 file changed, 19 insertions(+), 47 deletions(-)

Skimmed over the rest and LGTM, but it's all a bit complicated to understand.

Trusting on Zu Yan here :)

Signature · 3 lines
Cheers

David / dhildenb
reviewer
#9 lore raw

Re: [Patch v2 2/2] mm/huge_memory: Optimize and simplify __split_unmapped_folio() logic

Quoted 25 lines · David Hildenbrand
Quoted 4 lines · Wei Yang

Existing __split_unmapped_folio() code splits the given folio and update stats, but it is complicated to understand.

After simplification, __split_unmapped_folio() directly calculate and

s/calculate/calculates/

Quoted 1 line

update the folio statistics upon a successful split:

s/update/updates/

Quoted 5 lines

* All resulting folios are @split_order.

* The number of new folios are calculated directly from @old_order and @split_order.

That makes sense.

Quoted 4 lines

* The folio for the next split is identified as the one containing @split_at.

Quoted 6 lines

That as well.

Quoted 2 lines

* An xas_try_split() error is returned directly without worrying about stats updates.

Why is that change ok?

Before this, the code decreases 1 for the to-be-split folio before the split actually happens, so for a failed xas_try_split() the stats needs to be fixed up. Wei’s code updates stats after the split, removing the stats fixup for a xas_try_split() failure.

How about?

* Stats fixup is no longer needed for an xas_try_split() error, since originally stats was updated before an split happens.

Quoted 16 lines
Quoted 12 lines

---
v2:
* merge patch 2-5
* retain start_order
* new_folios -> nr_new_folios
* add a comment at the end of the loop

Quoted 3 lines
---
  mm/huge_memory.c | 66 ++++++++++++++----------------------------------
  1 file changed, 19 insertions(+), 47 deletions(-)
Quoted 3 lines

Skimmed over the rest and LGTM, but it's all a bit complicated to understand.

Trusting on Zu Yan here :)

;)

Best Regards,
Yan, Zi

#10 lore raw

Re: [Patch v2 2/2] mm/huge_memory: Optimize and simplify __split_unmapped_folio() logic

Quoted 32 lines · Zi Yan
Quoted 30 lines · David Hildenbrand
Quoted 4 lines · Wei Yang

Existing __split_unmapped_folio() code splits the given folio and update stats, but it is complicated to understand.

After simplification, __split_unmapped_folio() directly calculate and

s/calculate/calculates/

Quoted 1 line

update the folio statistics upon a successful split:

s/update/updates/

Quoted 5 lines

* All resulting folios are @split_order.

* The number of new folios are calculated directly from @old_order and @split_order.

That makes sense.

Quoted 4 lines

* The folio for the next split is identified as the one containing

    @split_at.

That as well.

Quoted 2 lines

* An xas_try_split() error is returned directly without worrying about stats updates.

Quoted 6 lines
Quoted 1 line

Why is that change ok?

Before this, the code decreases 1 for the to-be-split folio before the split actually happens, so for a failed xas_try_split() the stats needs to be fixed up. Wei’s code updates stats after the split, removing the stats fixup for a xas_try_split() failure.

Ah, that was not immediately clear to me.

Quoted 5 lines

How about?

* Stats fixup is no longer needed for an xas_try_split() error, since originally stats was updated before an split happens.

... since we now update the stats only after a successful split.

?

Quoted 17 lines
Quoted 17 lines
Quoted 16 lines

---
v2:
* merge patch 2-5
* retain start_order
* new_folios -> nr_new_folios
* add a comment at the end of the loop

---
   mm/huge_memory.c | 66 ++++++++++++++----------------------------------
   1 file changed, 19 insertions(+), 47 deletions(-)
Quoted 5 lines
Quoted 3 lines

Skimmed over the rest and LGTM, but it's all a bit complicated to understand.

Trusting on Zu Yan here :)

;)

"Zi" of course :)

Signature · 3 lines
Cheers

David / dhildenb
reviewer
#11 lore raw

Re: [Patch v2 2/2] mm/huge_memory: Optimize and simplify __split_unmapped_folio() logic

Quoted 43 lines · David Hildenbrand
Quoted 32 lines · Zi Yan
Quoted 30 lines · David Hildenbrand
Quoted 4 lines · Wei Yang

Existing __split_unmapped_folio() code splits the given folio and update stats, but it is complicated to understand.

After simplification, __split_unmapped_folio() directly calculate and

s/calculate/calculates/

Quoted 1 line

update the folio statistics upon a successful split:

s/update/updates/

Quoted 5 lines

* All resulting folios are @split_order.

* The number of new folios are calculated directly from @old_order and @split_order.

That makes sense.

Quoted 4 lines

* The folio for the next split is identified as the one containing

    @split_at.

That as well.

Quoted 2 lines

* An xas_try_split() error is returned directly without worrying about stats updates.

Quoted 6 lines
Quoted 1 line

Why is that change ok?

Before this, the code decreases 1 for the to-be-split folio before the split actually happens, so for a failed xas_try_split() the stats needs to be fixed up. Wei’s code updates stats after the split, removing the stats fixup for a xas_try_split() failure.

Ah, that was not immediately clear to me.

Quoted 1 line
Quoted 6 lines
Quoted 4 lines

How about?

* Stats fixup is no longer needed for an xas_try_split() error, since originally stats was updated before an split happens.

... since we now update the stats only after a successful split.

Sounds good to me.

Quoted 20 lines

?

Quoted 17 lines
Quoted 17 lines
Quoted 16 lines

---
v2:
* merge patch 2-5
* retain start_order
* new_folios -> nr_new_folios
* add a comment at the end of the loop

---
   mm/huge_memory.c | 66 ++++++++++++++----------------------------------
   1 file changed, 19 insertions(+), 47 deletions(-)
Quoted 12 lines
Quoted 5 lines
Quoted 3 lines

Skimmed over the rest and LGTM, but it's all a bit complicated to understand.

Trusting on Zu Yan here :)

;)

"Zi" of course :)

Signature · 3 lines
Cheers

David / dhildenb

Best Regards,
Yan, Zi

author
#12 lore raw

Re: [Patch v2 2/2] mm/huge_memory: Optimize and simplify __split_unmapped_folio() logic

In reply to Wei Yang
Quoted 8 lines · Wei Yang

Existing __split_unmapped_folio() code splits the given folio and update stats, but it is complicated to understand.

After simplification, __split_unmapped_folio() directly calculate and update the folio statistics upon a successful split:

* All resulting folios are @split_order.

Quoted 8 lines

* The number of new folios are calculated directly from @old_order and @split_order.

* The folio for the next split is identified as the one containing @split_at.

* An xas_try_split() error is returned directly without worrying about stats updates.

@Andrew

From David and Zi comment, there is little change in change log.

Would you mind helping update it? Below is the updated version.

---

Existing __split_unmapped_folio() code splits the given folio and updates stats, but it is complicated to understand.

After simplification, __split_unmapped_folio() directly calculates and updates the folio statistics upon a successful split:

* All resulting folios are @split_order.

* The number of new folios are calculated directly from @old_order and @split_order.

* The folio for the next split is identified as the one containing @split_at.

* Stats fixup is no longer needed for an xas_try_split() error, since since we now update the stats only after a successful split.

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

Re: [Patch v2 2/2] mm/huge_memory: Optimize and simplify __split_unmapped_folio() logic

In reply to Wei Yang
Quoted 8 lines · Wei Yang

Existing __split_unmapped_folio() code splits the given folio and update stats, but it is complicated to understand.

After simplification, __split_unmapped_folio() directly calculate and update the folio statistics upon a successful split:

* All resulting folios are @split_order.

Quoted 8 lines

* The number of new folios are calculated directly from @old_order and @split_order.

* The folio for the next split is identified as the one containing @split_at.

* An xas_try_split() error is returned directly without worrying about stats updates.

You seem to be doing two things at once, a big refactoring where you move stuff about AND changing functionality.

Can we split this out please? It makes review so much harder.

Quoted 82 lines

---
v2:
* merge patch 2-5
* retain start_order
* new_folios -> nr_new_folios
* add a comment at the end of the loop

---
 mm/huge_memory.c | 66 ++++++++++++++----------------------------------
 1 file changed, 19 insertions(+), 47 deletions(-)
mm/huge_memory.c +12 -23
index 4b2d5a7e5c8e..68e851f5fcb2 100644
@@ -3528,15 +3528,9 @@ static int __split_unmapped_folio(struct folio *folio, int new_order,
 		struct address_space *mapping, bool uniform_split)
 {
 	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 old_order = folio_order(folio);
+	int start_order = uniform_split ? new_order : old_order - 1;
 	int split_order;
-	int ret = 0;
-
-	if (is_anon)
-		mod_mthp_stat(order, MTHP_STAT_NR_ANON, -1);
 
 	folio_clear_has_hwpoisoned(folio);
 
@@ -3545,17 +3539,13 @@ 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);
-		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)
 			continue;
-		if (uniform_split && split_order != new_order)
-			continue;
 
 		if (mapping) {
 			/*
@@ -3568,49 +3558,31 @@ 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);
+		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);
 
-			__split_folio_to_order(folio, old_order, split_order);
+		if (is_anon) {
+			mod_mthp_stat(old_order, MTHP_STAT_NR_ANON, -1);

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

Quoted 10 lines
mm/huge_memory.c +2 -5
+			mod_mthp_stat(split_order, MTHP_STAT_NR_ANON, nr_new_folios);
 		}
 
 		/*
-		 * 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 uniform split, we have finished the job.

Finsihed what job? This is unclear.

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

Quoted 1 line
mm/huge_memory.c +1 -0
+		 * For non-uniform split, we assign folio to the one the one

'To the one the one' you're duplicating that, and I have no idea what 'the one' means?

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

Quoted 1 line
mm/huge_memory.c +1 -0
+		 * containing @split_at and assign @old_order to @split_order.

Now you're just describing code, and why are you making it kdoc-like in a non-kdoc comment?

I mean you're now unconditionally assigning folio to page_folio(split_at) and reassigning split-order to old_order so you really need to be clearer about what you mean here, given there is no e.g.:

if (is uniform split)

	break;

Something simpler would probably work better here.

Quoted 32 lines
mm/huge_memory.c +3 -20
 		 */
-		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)) {
-				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);
-		}
+		folio = page_folio(split_at);
+		old_order = split_order;
 	}
 
-	return ret;
+	return 0;
 }
 
 bool non_uniform_split_supported(struct folio *folio, unsigned int new_order,
--

2.34.1

reviewer
#14 lore raw

Re: [Patch v2 2/2] mm/huge_memory: Optimize and simplify __split_unmapped_folio() logic

Quoted 12 lines · Lorenzo Stoakes
Quoted 11 lines · Wei Yang

Existing __split_unmapped_folio() code splits the given folio and update stats, but it is complicated to understand.

After simplification, __split_unmapped_folio() directly calculate and update the folio statistics upon a successful split:

* All resulting folios are @split_order.

* The number of new folios are calculated directly from @old_order and @split_order.

Quoted 8 lines
Quoted 5 lines

* The folio for the next split is identified as the one containing @split_at.

* An xas_try_split() error is returned directly without worrying about stats updates.

You seem to be doing two things at once, a big refactoring where you move stuff about AND changing functionality.

No function change is done in this patchset. The wording might be confusing here, it should be read like:

After simplification, __split_unmapped_folio() directly calculate and update the folio statistics upon a successful split, so An xas_try_split() error is returned directly without worrying about stats updates.

David suggested a change[1] to make it clear:
Stats fixup is no longer needed for an xas_try_split() error, since we now update the stats only after a successful split.

[1] https://lore.kernel.org/linux-mm/[email protected]/

Quoted 2 lines

Can we split this out please? It makes review so much harder.

I asked Wei to use a single patch for this change, since the original code was complicated due to the initial implementation. After my recent change (first commit 6c7de9c83)[1], __split_unmmaped_folio() can be simplified like Wei did here.

[1] https://lore.kernel.org/all/[email protected]/

Quoted 142 lines
Quoted 83 lines

---
v2:
* merge patch 2-5
* retain start_order
* new_folios -> nr_new_folios
* add a comment at the end of the loop

---
 mm/huge_memory.c | 66 ++++++++++++++----------------------------------
 1 file changed, 19 insertions(+), 47 deletions(-)
mm/huge_memory.c +12 -23
index 4b2d5a7e5c8e..68e851f5fcb2 100644
@@ -3528,15 +3528,9 @@ static int __split_unmapped_folio(struct folio *folio, int new_order,
 		struct address_space *mapping, bool uniform_split)
 {
 	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 old_order = folio_order(folio);
+	int start_order = uniform_split ? new_order : old_order - 1;
 	int split_order;
-	int ret = 0;
-
-	if (is_anon)
-		mod_mthp_stat(order, MTHP_STAT_NR_ANON, -1);
 
 	folio_clear_has_hwpoisoned(folio);
 
@@ -3545,17 +3539,13 @@ 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);
-		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)
 			continue;
-		if (uniform_split && split_order != new_order)
-			continue;
 
 		if (mapping) {
 			/*
@@ -3568,49 +3558,31 @@ 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);
+		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);
 
-			__split_folio_to_order(folio, old_order, split_order);
+		if (is_anon) {
+			mod_mthp_stat(old_order, MTHP_STAT_NR_ANON, -1);

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

Quoted 10 lines
mm/huge_memory.c +2 -5
+			mod_mthp_stat(split_order, MTHP_STAT_NR_ANON, nr_new_folios);
 		}
 
 		/*
-		 * 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 uniform split, we have finished the job.

Finsihed what job? This is unclear.

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

Quoted 1 line
mm/huge_memory.c +1 -0
+		 * For non-uniform split, we assign folio to the one the one

'To the one the one' you're duplicating that, and I have no idea what 'the one' means?

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

Quoted 1 line
mm/huge_memory.c +1 -0
+		 * containing @split_at and assign @old_order to @split_order.

Now you're just describing code, and why are you making it kdoc-like in a non-kdoc comment?

I mean you're now unconditionally assigning folio to page_folio(split_at) and reassigning split-order to old_order so you really need to be clearer about what you mean here, given there is no e.g.:

if (is uniform split)

	break;

Something simpler would probably work better here.

Quoted 27 lines
mm/huge_memory.c +3 -19
 		 */
-		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)) {
-				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);
-		}
+		folio = page_folio(split_at);
+		old_order = split_order;
 	}
 
-	return ret;
+	return 0;
 }
 
Quoted 5 lines
Quoted 5 lines
mm/huge_memory.c +0 -1
 bool non_uniform_split_supported(struct folio *folio, unsigned int new_order,
--

2.34.1

--
Best Regards,
Yan, Zi

reviewer
#15 lore raw

Re: [Patch v2 2/2] mm/huge_memory: Optimize and simplify __split_unmapped_folio() logic

Quoted 40 lines · Zi Yan
Quoted 12 lines · Lorenzo Stoakes
Quoted 11 lines · Wei Yang

Existing __split_unmapped_folio() code splits the given folio and update stats, but it is complicated to understand.

After simplification, __split_unmapped_folio() directly calculate and update the folio statistics upon a successful split:

* All resulting folios are @split_order.

* The number of new folios are calculated directly from @old_order and @split_order.

Quoted 8 lines
Quoted 5 lines

* The folio for the next split is identified as the one containing @split_at.

* An xas_try_split() error is returned directly without worrying about stats updates.

You seem to be doing two things at once, a big refactoring where you move stuff about AND changing functionality.

No function change is done in this patchset. The wording might be confusing here, it should be read like:

After simplification, __split_unmapped_folio() directly calculate and update the folio statistics upon a successful split, so An xas_try_split() error is returned directly without worrying about stats updates.

David suggested a change[1] to make it clear:
Stats fixup is no longer needed for an xas_try_split() error, since we now update the stats only after a successful split.

[1] https://lore.kernel.org/linux-mm/[email protected]/

Quoted 2 lines

Can we split this out please? It makes review so much harder.

Quoted 123 lines

I asked Wei to use a single patch for this change, since the original code was complicated due to the initial implementation. After my recent change (first commit 6c7de9c83)[1], __split_unmmaped_folio() can be simplified like Wei did here.

[1] https://lore.kernel.org/all/[email protected]/

Quoted 114 lines
Quoted 83 lines

---
v2:
* merge patch 2-5
* retain start_order
* new_folios -> nr_new_folios
* add a comment at the end of the loop

---
 mm/huge_memory.c | 66 ++++++++++++++----------------------------------
 1 file changed, 19 insertions(+), 47 deletions(-)
mm/huge_memory.c +12 -23
index 4b2d5a7e5c8e..68e851f5fcb2 100644
@@ -3528,15 +3528,9 @@ static int __split_unmapped_folio(struct folio *folio, int new_order,
 		struct address_space *mapping, bool uniform_split)
 {
 	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 old_order = folio_order(folio);
+	int start_order = uniform_split ? new_order : old_order - 1;
 	int split_order;
-	int ret = 0;
-
-	if (is_anon)
-		mod_mthp_stat(order, MTHP_STAT_NR_ANON, -1);
 
 	folio_clear_has_hwpoisoned(folio);
 
@@ -3545,17 +3539,13 @@ 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);
-		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)
 			continue;
-		if (uniform_split && split_order != new_order)
-			continue;
 
 		if (mapping) {
 			/*
@@ -3568,49 +3558,31 @@ 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);
+		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);
 
-			__split_folio_to_order(folio, old_order, split_order);
+		if (is_anon) {
+			mod_mthp_stat(old_order, MTHP_STAT_NR_ANON, -1);

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

Quoted 10 lines
mm/huge_memory.c +2 -5
+			mod_mthp_stat(split_order, MTHP_STAT_NR_ANON, nr_new_folios);
 		}
 
 		/*
-		 * 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 uniform split, we have finished the job.

Finsihed what job? This is unclear.

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

Quoted 1 line
mm/huge_memory.c +1 -0
+		 * For non-uniform split, we assign folio to the one the one

'To the one the one' you're duplicating that, and I have no idea what 'the one' means?

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

Quoted 1 line
mm/huge_memory.c +1 -0
+		 * containing @split_at and assign @old_order to @split_order.

Now you're just describing code, and why are you making it kdoc-like in a non-kdoc comment?

I mean you're now unconditionally assigning folio to page_folio(split_at) and reassigning split-order to old_order so you really need to be clearer about what you mean here, given there is no e.g.:

if (is uniform split)

	break;

Something simpler would probably work better here.

Maybe, when uniform_split is true, for loop will only be done once, since start_order == new_order. This break is almost a no op.

Quoted 28 lines
Quoted 28 lines
Quoted 28 lines
mm/huge_memory.c +3 -19
 		 */
-		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)) {
-				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);
-		}
+		folio = page_folio(split_at);
+		old_order = split_order;
 	}
 
-	return ret;
+	return 0;
 }
 
Quoted 10 lines
Quoted 5 lines
Quoted 5 lines
mm/huge_memory.c +0 -1
 bool non_uniform_split_supported(struct folio *folio, unsigned int new_order,
--

2.34.1

--
Best Regards,
Yan, Zi

--
Best Regards,
Yan, Zi

#16 lore raw

Re: [Patch v2 2/2] mm/huge_memory: Optimize and simplify __split_unmapped_folio() logic

In reply to Zi Yan
Quoted 17 lines · Zi Yan
Quoted 15 lines · Lorenzo Stoakes
Quoted 14 lines · Wei Yang

Existing __split_unmapped_folio() code splits the given folio and update stats, but it is complicated to understand.

After simplification, __split_unmapped_folio() directly calculate and update the folio statistics upon a successful split:

* All resulting folios are @split_order.

* The number of new folios are calculated directly from @old_order and @split_order.

* The folio for the next split is identified as the one containing @split_at.

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

* An xas_try_split() error is returned directly without worrying about stats updates.

You seem to be doing two things at once, a big refactoring where you move stuff about AND changing functionality.

No function change is done in this patchset. The wording might be confusing here, it should be read like:

I made this assessment based on David saying 'why are you making this change?' repeatedly.

That doesn't sound like a refactoring, but if you're claiming these are all invalid...

Quoted 8 lines

After simplification, __split_unmapped_folio() directly calculate and update the folio statistics upon a successful split, so An xas_try_split() error is returned directly without worrying about stats updates.

David suggested a change[1] to make it clear:
Stats fixup is no longer needed for an xas_try_split() error, since we now update the stats only after a successful split.

Right isn't that a substantive change?

Quoted 7 lines

[1] https://lore.kernel.org/linux-mm/[email protected]/

Quoted 2 lines

Can we split this out please? It makes review so much harder.

Quoted 9 lines

I asked Wei to use a single patch for this change, since the original code was complicated due to the initial implementation. After my recent change (first commit 6c7de9c83)[1], __split_unmmaped_folio() can be simplified like Wei did here.

[1] https://lore.kernel.org/all/[email protected]/

I mean I am not sure why it is so problematic to split up changes so we dont' do a bunch of complicated/not complicated things all at once which is harder to review, more bug prone and makes bisecting harder.

I've done series which consist of a _series_ of 'no functional change' patches becuase they logically fell out that way.

But if you're really confident this is functional-only and is a simple and sensible change I can take another look.

Thanks, Lorenzo

#17 lore raw

Re: [Patch v2 2/2] mm/huge_memory: Optimize and simplify __split_unmapped_folio() logic

In reply to Zi Yan
Quoted 38 lines · Zi Yan
Quoted 12 lines · Lorenzo Stoakes
Quoted 11 lines · Wei Yang

Existing __split_unmapped_folio() code splits the given folio and update stats, but it is complicated to understand.

After simplification, __split_unmapped_folio() directly calculate and update the folio statistics upon a successful split:

* All resulting folios are @split_order.

* The number of new folios are calculated directly from @old_order and @split_order.

Quoted 8 lines
Quoted 5 lines

* The folio for the next split is identified as the one containing @split_at.

* An xas_try_split() error is returned directly without worrying about stats updates.

You seem to be doing two things at once, a big refactoring where you move stuff about AND changing functionality.

No function change is done in this patchset. The wording might be confusing here, it should be read like:

After simplification, __split_unmapped_folio() directly calculate and update the folio statistics upon a successful split, so An xas_try_split() error is returned directly without worrying about stats updates.

David suggested a change[1] to make it clear:
Stats fixup is no longer needed for an xas_try_split() error, since we now update the stats only after a successful split.

[1] https://lore.kernel.org/linux-mm/[email protected]/

Quoted 1 line
Quoted 6 lines
Quoted 1 line

Can we split this out please? It makes review so much harder.

I asked Wei to use a single patch for this change, since the original code was complicated due to the initial implementation. After my recent change (first commit 6c7de9c83)[1], __split_unmmaped_folio() can be simplified like Wei did here.

I think it was too much split, agreed.

This patch was a bit hard for me to review, not sure if there would have been a better way to split some stuff out more logically.

Most changes just complicated way.

Not sure if splitting out the stats update change or the calculation of the number of folios could have been easily done.

Signature · 3 lines
Cheers

David / dhildenb
#18 lore raw

Re: [Patch v2 2/2] mm/huge_memory: Optimize and simplify __split_unmapped_folio() logic

Quoted 48 lines · David Hildenbrand
Quoted 38 lines · Zi Yan
Quoted 12 lines · Lorenzo Stoakes
Quoted 11 lines · Wei Yang

Existing __split_unmapped_folio() code splits the given folio and update stats, but it is complicated to understand.

After simplification, __split_unmapped_folio() directly calculate and update the folio statistics upon a successful split:

* All resulting folios are @split_order.

* The number of new folios are calculated directly from @old_order and @split_order.

Quoted 8 lines
Quoted 5 lines

* The folio for the next split is identified as the one containing @split_at.

* An xas_try_split() error is returned directly without worrying about stats updates.

You seem to be doing two things at once, a big refactoring where you move stuff about AND changing functionality.

No function change is done in this patchset. The wording might be confusing here, it should be read like:

After simplification, __split_unmapped_folio() directly calculate and update the folio statistics upon a successful split, so An xas_try_split() error is returned directly without worrying about stats updates.

David suggested a change[1] to make it clear:
Stats fixup is no longer needed for an xas_try_split() error, since we now update the stats only after a successful split.

[1] https://lore.kernel.org/linux-mm/[email protected]/

Quoted 1 line
Quoted 6 lines
Quoted 1 line

Can we split this out please? It makes review so much harder.

I asked Wei to use a single patch for this change, since the original code was complicated due to the initial implementation. After my recent change (first commit 6c7de9c83)[1], __split_unmmaped_folio() can be simplified like Wei did here.

I think it was too much split, agreed.

Quoted 7 lines

This patch was a bit hard for me to review, not sure if there would have been a better way to split some stuff out more logically.

Most changes just complicated way.

Not sure if splitting out the stats update change or the calculation of the number of folios could have been easily done.

I mean as I said to Wei/Zi, my objection is both moving code around and making complicated changes that require you to really dig in to figure them out _at the same time_.

It's perfectly feasible to send separate patches for non-functional changes, I've done that myself, each building on the next to make it clear what's going on.

I'm a little surprised at the oppositon to that...

But if you're convinced there's no other way this could be done (bit surprising as you're asking 'why this change' several times here) I guess I'll sit down and try to decode it.

reviewer
#19 lore raw

Re: [Patch v2 2/2] mm/huge_memory: Optimize and simplify __split_unmapped_folio() logic

Quoted 65 lines · Lorenzo Stoakes
Quoted 48 lines · David Hildenbrand
Quoted 38 lines · Zi Yan
Quoted 12 lines · Lorenzo Stoakes
Quoted 11 lines · Wei Yang

Existing __split_unmapped_folio() code splits the given folio and update stats, but it is complicated to understand.

After simplification, __split_unmapped_folio() directly calculate and update the folio statistics upon a successful split:

* All resulting folios are @split_order.

* The number of new folios are calculated directly from @old_order and @split_order.

Quoted 8 lines
Quoted 5 lines

* The folio for the next split is identified as the one containing @split_at.

* An xas_try_split() error is returned directly without worrying about stats updates.

You seem to be doing two things at once, a big refactoring where you move stuff about AND changing functionality.

No function change is done in this patchset. The wording might be confusing here, it should be read like:

After simplification, __split_unmapped_folio() directly calculate and update the folio statistics upon a successful split, so An xas_try_split() error is returned directly without worrying about stats updates.

David suggested a change[1] to make it clear:
Stats fixup is no longer needed for an xas_try_split() error, since we now update the stats only after a successful split.

[1] https://lore.kernel.org/linux-mm/[email protected]/

Quoted 1 line
Quoted 6 lines
Quoted 1 line

Can we split this out please? It makes review so much harder.

I asked Wei to use a single patch for this change, since the original code was complicated due to the initial implementation. After my recent change (first commit 6c7de9c83)[1], __split_unmmaped_folio() can be simplified like Wei did here.

I think it was too much split, agreed.

Quoted 7 lines

This patch was a bit hard for me to review, not sure if there would have been a better way to split some stuff out more logically.

Most changes just complicated way.

Not sure if splitting out the stats update change or the calculation of the number of folios could have been easily done.

I mean as I said to Wei/Zi, my objection is both moving code around and making complicated changes that require you to really dig in to figure them out _at the same time_.

It's perfectly feasible to send separate patches for non-functional changes, I've done that myself, each building on the next to make it clear what's going on.

Quoted 5 lines

I'm a little surprised at the oppositon to that...

But if you're convinced there's no other way this could be done (bit surprising as you're asking 'why this change' several times here) I guess I'll sit down and try to decode it.

Let me explain the changes in details below, so that we might find a good way of splitting it up for an easy review:

Quoted 6 lines
mm/huge_memory.c +0 -0
index 4b2d5a7e5c8e..68e851f5fcb2 100644
@@ -3528,15 +3528,9 @@ static int __split_unmapped_folio(struct folio *folio, int new_order,
 		struct address_space *mapping, bool uniform_split)

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

Quoted 10 lines
mm/huge_memory.c +2 -5
 {
 	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 old_order = folio_order(folio);
+	int start_order = uniform_split ? new_order : old_order - 1;
 	int split_order;
-	int ret = 0;

These remove unused variables and do a order->old_order rename.

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

Quoted 3 lines
mm/huge_memory.c +0 -3
-
-	if (is_anon)
-		mod_mthp_stat(order, MTHP_STAT_NR_ANON, -1);

In the original __split_unmapped_folio() implementation, I probably moved the stats in split_huge_page_to_list_to_order() (now __folio_split())[1] in to __split_unmapped_folio() and needed to handle xas_try_split() failures, so left mod_mthp_stat(order, ..., -1) here. It should have been put together with mod_mthp_stat(new_order, ..., 1 << (order - new_order).

[1] https://elixir.bootlin.com/linux/v6.13/source/mm/huge_memory.c#L3622

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

Quoted 9 lines
mm/huge_memory.c +1 -1
 	folio_clear_has_hwpoisoned(folio);
 
@@ -3545,17 +3539,13 @@ 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;

stop_split was used to handle xas_try_split() failure, since the stats separation I had in the initial code, I needed to add the -1 stats back. Now with Wei's change, subtracting old folio and increment new folios are done together, stop_split is no longer needed.

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

Quoted 5 lines
mm/huge_memory.c +1 -3
 	     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);

stats for counting new after-split folios.

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

Quoted 6 lines
mm/huge_memory.c +0 -2
 		/* order-1 anonymous folio is not supported */
 		if (is_anon && split_order == 1)
 			continue;
-		if (uniform_split && split_order != new_order)
-			continue;

split_order is always new_order when uniform_split is true, so it is useless code.

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

Quoted 13 lines
mm/huge_memory.c +2 -4
 		if (mapping) {
 			/*
@@ -3568,49 +3558,31 @@ 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);

xas_error() was not returned immediately because the -1 for old folio needed to be fixed later. Now since the stats is not changed upfront, it can simply return.

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

Quoted 13 lines
mm/huge_memory.c +4 -5
 			}
 		}
 
-		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);
+		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);
 
-			__split_folio_to_order(folio, old_order, split_order);

No more stop_split, so all these are unconditional.

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

Quoted 4 lines
mm/huge_memory.c +3 -0
+		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);
 		}

Update the stats when the actual split is done, much better than the original one, which assumes the split would happen and recovers when it does not.

Quoted 19 lines
mm/huge_memory.c +3 -13
 		/*
-		 * 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 uniform split, we have finished the job.
+		 * For non-uniform split, we assign folio to the one the one
+		 * containing @split_at and assign @old_order to @split_order.
 		 */
-		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.

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

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

This loop was needed to update after-split folios in the xarray and unfreeze folios, but since last refactor, neither is here. And the stats update can be done in one shot instead of the originally one by one method. So this hunk can be removed completely.

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

Quoted 2 lines
mm/huge_memory.c +2 -0
+		folio = page_folio(split_at);
+		old_order = split_order;

These two are for non-uniform split, where the code moves to split next folio after it splits the current one into half. And the old_order needs to be updated, since the folio has been split.

For uniform split, the for loop is done once, these two effectively does nothing, since the for loop ends.

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

Quoted 4 lines
mm/huge_memory.c +1 -1
 	}
 
-	return ret;
+	return 0;

xas_try_split() failure is handled above, code should always succeed at this point.

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

Quoted 3 lines
mm/huge_memory.c +0 -0
 }
 
 bool non_uniform_split_supported(struct folio *folio, unsigned int new_order,

--
Best Regards,
Yan, Zi

#20 lore raw

Re: [Patch v2 2/2] mm/huge_memory: Optimize and simplify __split_unmapped_folio() logic

Quoted 2 lines · Zi Yan

Let me explain the changes in details below, so that we might find a good way of splitting it up for an easy review:

Thanks, much appreciated!

Looking through it seems to me there should be 3 patches:

- Getting rid of the trailing loop
- Stats fixups
- Remaining trivial cleanups

Do you agree?

Each can have a commit message that describes in detail what's happening and why it's ok.

This is also useful for bisection purposes.

Quoted 135 lines
Quoted 7 lines
mm/huge_memory.c +0 -0
index 4b2d5a7e5c8e..68e851f5fcb2 100644
@@ -3528,15 +3528,9 @@ static int __split_unmapped_folio(struct folio *folio, int new_order,
 		struct address_space *mapping, bool uniform_split)

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

Quoted 10 lines
mm/huge_memory.c +2 -5
 {
 	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 old_order = folio_order(folio);
+	int start_order = uniform_split ? new_order : old_order - 1;
 	int split_order;
-	int ret = 0;

These remove unused variables and do a order->old_order rename.

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

Quoted 3 lines
mm/huge_memory.c +0 -3
-
-	if (is_anon)
-		mod_mthp_stat(order, MTHP_STAT_NR_ANON, -1);

In the original __split_unmapped_folio() implementation, I probably moved the stats in split_huge_page_to_list_to_order() (now __folio_split())[1] in to __split_unmapped_folio() and needed to handle xas_try_split() failures, so left mod_mthp_stat(order, ..., -1) here. It should have been put together with mod_mthp_stat(new_order, ..., 1 << (order - new_order).

[1] https://elixir.bootlin.com/linux/v6.13/source/mm/huge_memory.c#L3622

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

Quoted 9 lines
mm/huge_memory.c +1 -1
 	folio_clear_has_hwpoisoned(folio);
 
@@ -3545,17 +3539,13 @@ 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;

stop_split was used to handle xas_try_split() failure, since the stats separation I had in the initial code, I needed to add the -1 stats back. Now with Wei's change, subtracting old folio and increment new folios are done together, stop_split is no longer needed.

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

Quoted 5 lines
mm/huge_memory.c +1 -3
 	     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);

stats for counting new after-split folios.

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

Quoted 6 lines
mm/huge_memory.c +0 -2
 		/* order-1 anonymous folio is not supported */
 		if (is_anon && split_order == 1)
 			continue;
-		if (uniform_split && split_order != new_order)
-			continue;

split_order is always new_order when uniform_split is true, so it is useless code.

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

Quoted 13 lines
mm/huge_memory.c +2 -4
 		if (mapping) {
 			/*
@@ -3568,49 +3558,31 @@ 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);

xas_error() was not returned immediately because the -1 for old folio needed to be fixed later. Now since the stats is not changed upfront, it can simply return.

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

Quoted 13 lines
mm/huge_memory.c +4 -5
 			}
 		}
 
-		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);
+		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);
 
-			__split_folio_to_order(folio, old_order, split_order);

No more stop_split, so all these are unconditional.

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

Quoted 4 lines
mm/huge_memory.c +3 -0
+		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);
 		}

Update the stats when the actual split is done, much better than the original one, which assumes the split would happen and recovers when it does not.

Quoted 26 lines
mm/huge_memory.c +3 -20
 		/*
-		 * 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 uniform split, we have finished the job.
+		 * For non-uniform split, we assign folio to the one the one
+		 * containing @split_at and assign @old_order to @split_order.
 		 */
-		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)) {
-				folio = new_folio;
-				if (split_order != new_order && !stop_split)
-					continue;
-			}
-			if (is_anon)
Quoted 8 lines

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

Quoted 3 lines
mm/huge_memory.c +0 -3
-				mod_mthp_stat(folio_order(new_folio),
-					      MTHP_STAT_NR_ANON, 1);
-		}

This loop was needed to update after-split folios in the xarray and unfreeze folios, but since last refactor, neither is here. And the stats update can be done in one shot instead of the originally one by one method. So this hunk can be removed completely.

Which refactor in particular? This is the bit that really needs separating as it's the most confusing and begs the question 'why can we avoid doing this'?

Quoted 27 lines

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

Quoted 3 lines
mm/huge_memory.c +2 -0
+		folio = page_folio(split_at);
+		old_order = split_order;

These two are for non-uniform split, where the code moves to split next folio after it splits the current one into half. And the old_order needs to be updated, since the folio has been split.

For uniform split, the for loop is done once, these two effectively does nothing, since the for loop ends.

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

Quoted 4 lines
mm/huge_memory.c +1 -1
 	}
 
-	return ret;
+	return 0;

xas_try_split() failure is handled above, code should always succeed at this point.

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

Quoted 3 lines
mm/huge_memory.c +0 -0
 }
 
 bool non_uniform_split_supported(struct folio *folio, unsigned int new_order,

--
Best Regards,
Yan, Zi

reviewer
#21 lore raw

Re: [Patch v2 2/2] mm/huge_memory: Optimize and simplify __split_unmapped_folio() logic

Quoted 6 lines · Lorenzo Stoakes
Quoted 2 lines · Zi Yan

Let me explain the changes in details below, so that we might find a good way of splitting it up for an easy review:

Thanks, much appreciated!

Quoted 7 lines

Looking through it seems to me there should be 3 patches:

- Getting rid of the trailing loop
- Stats fixups
- Remaining trivial cleanups

Do you agree?

Sounds good to me. Thanks for taking a look. Let's see how Wei feels about this.

Quoted 150 lines

Each can have a commit message that describes in detail what's happening and why it's ok.

This is also useful for bisection purposes.

Quoted 135 lines
Quoted 7 lines
mm/huge_memory.c +0 -0
index 4b2d5a7e5c8e..68e851f5fcb2 100644
@@ -3528,15 +3528,9 @@ static int __split_unmapped_folio(struct folio *folio, int new_order,
 		struct address_space *mapping, bool uniform_split)

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

Quoted 10 lines
mm/huge_memory.c +2 -5
 {
 	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 old_order = folio_order(folio);
+	int start_order = uniform_split ? new_order : old_order - 1;
 	int split_order;
-	int ret = 0;

These remove unused variables and do a order->old_order rename.

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

Quoted 3 lines
mm/huge_memory.c +0 -3
-
-	if (is_anon)
-		mod_mthp_stat(order, MTHP_STAT_NR_ANON, -1);

In the original __split_unmapped_folio() implementation, I probably moved the stats in split_huge_page_to_list_to_order() (now __folio_split())[1] in to __split_unmapped_folio() and needed to handle xas_try_split() failures, so left mod_mthp_stat(order, ..., -1) here. It should have been put together with mod_mthp_stat(new_order, ..., 1 << (order - new_order).

[1] https://elixir.bootlin.com/linux/v6.13/source/mm/huge_memory.c#L3622

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

Quoted 9 lines
mm/huge_memory.c +1 -1
 	folio_clear_has_hwpoisoned(folio);
 
@@ -3545,17 +3539,13 @@ 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;

stop_split was used to handle xas_try_split() failure, since the stats separation I had in the initial code, I needed to add the -1 stats back. Now with Wei's change, subtracting old folio and increment new folios are done together, stop_split is no longer needed.

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

Quoted 5 lines
mm/huge_memory.c +1 -3
 	     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);

stats for counting new after-split folios.

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

Quoted 6 lines
mm/huge_memory.c +0 -2
 		/* order-1 anonymous folio is not supported */
 		if (is_anon && split_order == 1)
 			continue;
-		if (uniform_split && split_order != new_order)
-			continue;

split_order is always new_order when uniform_split is true, so it is useless code.

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

Quoted 13 lines
mm/huge_memory.c +2 -4
 		if (mapping) {
 			/*
@@ -3568,49 +3558,31 @@ 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);

xas_error() was not returned immediately because the -1 for old folio needed to be fixed later. Now since the stats is not changed upfront, it can simply return.

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

Quoted 13 lines
mm/huge_memory.c +4 -5
 			}
 		}
 
-		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);
+		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);
 
-			__split_folio_to_order(folio, old_order, split_order);

No more stop_split, so all these are unconditional.

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

Quoted 4 lines
mm/huge_memory.c +3 -0
+		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);
 		}

Update the stats when the actual split is done, much better than the original one, which assumes the split would happen and recovers when it does not.

Quoted 26 lines
mm/huge_memory.c +3 -20
 		/*
-		 * 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 uniform split, we have finished the job.
+		 * For non-uniform split, we assign folio to the one the one
+		 * containing @split_at and assign @old_order to @split_order.
 		 */
-		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)) {
-				folio = new_folio;
-				if (split_order != new_order && !stop_split)
-					continue;
-			}
-			if (is_anon)
Quoted 8 lines

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

Quoted 3 lines
mm/huge_memory.c +0 -3
-				mod_mthp_stat(folio_order(new_folio),
-					      MTHP_STAT_NR_ANON, 1);
-		}

This loop was needed to update after-split folios in the xarray and unfreeze folios, but since last refactor, neither is here. And the stats update can be done in one shot instead of the originally one by one method. So this hunk can be removed completely.

Quoted 3 lines

Which refactor in particular? This is the bit that really needs separating as it's the most confusing and begs the question 'why can we avoid doing this'?

Patch 1(https://lore.kernel.org/linux-mm/[email protected]/) in the __folio_split() clean up series.

Quoted 19 lines
Quoted 19 lines

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

Quoted 3 lines
mm/huge_memory.c +2 -0
+		folio = page_folio(split_at);
+		old_order = split_order;

These two are for non-uniform split, where the code moves to split next folio after it splits the current one into half. And the old_order needs to be updated, since the folio has been split.

For uniform split, the for loop is done once, these two effectively does nothing, since the for loop ends.

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

Quoted 4 lines
mm/huge_memory.c +1 -1
 	}
 
-	return ret;
+	return 0;

xas_try_split() failure is handled above, code should always succeed at this point.

Quoted 8 lines
Quoted 8 lines

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

Quoted 3 lines
mm/huge_memory.c +0 -0
 }
 
 bool non_uniform_split_supported(struct folio *folio, unsigned int new_order,

--
Best Regards,
Yan, Zi

--
Best Regards,
Yan, Zi

author
#22 lore raw

Re: [Patch v2 2/2] mm/huge_memory: Optimize and simplify __split_unmapped_folio() logic

In reply to Lorenzo Stoakes
Quoted 10 lines · Lorenzo Stoakes
Quoted 2 lines · Zi Yan

Let me explain the changes in details below, so that we might find a good way of splitting it up for an easy review:

Thanks, much appreciated!

Looking through it seems to me there should be 3 patches:

- Getting rid of the trailing loop
- Stats fixups

Hmm... I don't get how to arrange in this order.

Remove the trailing loop before stats fixup means we still do mod_mthp_stat() in two places? And we keep the stop_split and fixup the stats on stop_split?

If this is what you mean, it looks complicated the change.

Quoted 1 line

- Remaining trivial cleanups

My suggestion is switch 1 and 2:

- Stats fixups
- Getting rid of the trailing loop
- Remaining trivial cleanups

We first update stats only on success split, so that we could get rid of the stop_split. Then we could get rid of the trailing loop in a clean way.

And this my v1 with patch 4&5 merged.

Quoted 7 lines

Do you agree?

Each can have a commit message that describes in detail what's happening and why it's ok.

This is also useful for bisection purposes.

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

Re: [Patch v2 2/2] mm/huge_memory: Optimize and simplify __split_unmapped_folio() logic

Quoted 19 lines · Wei Yang
Quoted 10 lines · Lorenzo Stoakes
Quoted 2 lines · Zi Yan

Let me explain the changes in details below, so that we might find a good way of splitting it up for an easy review:

Thanks, much appreciated!

Looking through it seems to me there should be 3 patches:

- Getting rid of the trailing loop
- Stats fixups

Hmm... I don't get how to arrange in this order.

Remove the trailing loop before stats fixup means we still do mod_mthp_stat() in two places? And we keep the stop_split and fixup the stats on stop_split?

If this is what you mean, it looks complicated the change.

Quoted 7 lines
Quoted 1 line

- Remaining trivial cleanups

My suggestion is switch 1 and 2:

- Stats fixups
- Getting rid of the trailing loop
- Remaining trivial cleanups

Yup this works too!

Quoted 11 lines

We first update stats only on success split, so that we could get rid of the stop_split. Then we could get rid of the trailing loop in a clean way.

And this my v1 with patch 4&5 merged.

Quoted 5 lines

Do you agree?

Each can have a commit message that describes in detail what's happening and why it's ok.

Quoted 7 lines
Quoted 2 lines

This is also useful for bisection purposes.

--
Wei Yang
Help you, Help me

Thanks, Lorenzo

author
#24 lore raw

Re: [Patch v2 2/2] mm/huge_memory: Optimize and simplify __split_unmapped_folio() logic

In reply to Lorenzo Stoakes
Quoted 52 lines · Lorenzo Stoakes
Quoted 51 lines · David Hildenbrand
Quoted 38 lines · Zi Yan
Quoted 12 lines · Lorenzo Stoakes
Quoted 11 lines · Wei Yang

Existing __split_unmapped_folio() code splits the given folio and update stats, but it is complicated to understand.

After simplification, __split_unmapped_folio() directly calculate and update the folio statistics upon a successful split:

* All resulting folios are @split_order.

* The number of new folios are calculated directly from @old_order and @split_order.

Quoted 8 lines
Quoted 5 lines

* The folio for the next split is identified as the one containing @split_at.

* An xas_try_split() error is returned directly without worrying about stats updates.

You seem to be doing two things at once, a big refactoring where you move stuff about AND changing functionality.

No function change is done in this patchset. The wording might be confusing here, it should be read like:

After simplification, __split_unmapped_folio() directly calculate and update the folio statistics upon a successful split, so An xas_try_split() error is returned directly without worrying about stats updates.

David suggested a change[1] to make it clear:
Stats fixup is no longer needed for an xas_try_split() error, since we now update the stats only after a successful split.

[1] https://lore.kernel.org/linux-mm/[email protected]/

Quoted 1 line
Quoted 6 lines
Quoted 1 line

Can we split this out please? It makes review so much harder.

I asked Wei to use a single patch for this change, since the original code was complicated due to the initial implementation. After my recent change (first commit 6c7de9c83)[1], __split_unmmaped_folio() can be simplified like Wei did here.

I think it was too much split, agreed.

This patch was a bit hard for me to review, not sure if there would have been a better way to split some stuff out more logically.

Quoted 8 lines
Quoted 4 lines

Most changes just complicated way.

Not sure if splitting out the stats update change or the calculation of the number of folios could have been easily done.

I mean as I said to Wei/Zi, my objection is both moving code around and making complicated changes that require you to really dig in to figure them out _at the same time_.

Is this version better for review?

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

Quoted 10 lines

It's perfectly feasible to send separate patches for non-functional changes, I've done that myself, each building on the next to make it clear what's going on.

I'm a little surprised at the oppositon to that...

But if you're convinced there's no other way this could be done (bit surprising as you're asking 'why this change' several times here) I guess I'll sit down and try to decode it.

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

Re: [Patch v2 2/2] mm/huge_memory: Optimize and simplify __split_unmapped_folio() logic

Quoted 5 lines · Wei Yang

Is this version better for review?

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

No, this was _too_ divided :) I will look at Zi's reply and dig in when I get a chance.

Sorry for holding this up but I think important to try to get this right!

Cheers, Lorenzo