Skip to contentSkip to conversation

huge_memory: return -EINVAL in folio split functions when THP is disabled

PATCH Superseded Pankaj Raghav (Samsung) 3 participants 11 messages Sep 2, 2025

Subsystems (2)

v1 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 huge_memory: return -EINVAL in folio split functions when THP is disabled

From: Pankaj Raghav <[email protected]>

split_huge_page_to_list_[to_order](), split_huge_page() and try_folio_split() return 0 on success and error codes on failure.

When THP is disabled, these functions return 0 indicating success even though an error code should be returned as it is not possible to split a folio when THP is disabled.

Make all these functions return -EINVAL to indicate failure instead of 0.

This issue was discovered while experimenting enabling large folios without THP and found that returning 0 in these functions is resulting in undefined behavior in truncate operations. This change fixes the issue.

Fixes c010d47f107f ("mm: thp: split huge page to any lower order pages")
---
 include/linux/huge_mm.h | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
include/linux/huge_mm.h +4 -4
index 29ef70022da1..48c4f91c5b13 100644
@@ -588,22 +588,22 @@ static inline int
 split_huge_page_to_list_to_order(struct page *page, struct list_head *list,
 		unsigned int new_order)
 {
-	return 0;
+	return -EINVAL;
 }
 static inline int split_huge_page(struct page *page)
 {
-	return 0;
+	return -EINVAL;
 }
 
 static inline int split_folio_to_list(struct folio *folio, struct list_head *list)
 {
-	return 0;
+	return -EINVAL;
 }
 
 static inline int try_folio_split(struct folio *folio, struct page *page,
 		struct list_head *list)
 {
-	return 0;
+	return -EINVAL;
 }
 
 static inline void deferred_split_folio(struct folio *folio, bool partially_mapped) {}

base-commit: 291634ccfd2820c09f6e8c4982c2dee8155d09ae

Signature · 1 line
2.50.1
#1 lore raw
Quoted 9 lines · Pankaj Raghav (Samsung)

From: Pankaj Raghav <[email protected]>

split_huge_page_to_list_[to_order](), split_huge_page() and try_folio_split() return 0 on success and error codes on failure.

When THP is disabled, these functions return 0 indicating success even though an error code should be returned as it is not possible to split a folio when THP is disabled.

Quoted 6 lines

Make all these functions return -EINVAL to indicate failure instead of 0.

This issue was discovered while experimenting enabling large folios without THP and found that returning 0 in these functions is resulting in undefined behavior in truncate operations. This change fixes the issue.

Currently large folios that could be split are impossible without THP, so why should this be a fix?

Signature · 3 lines
Cheers

David / dhildenb
#2 lore raw
Quoted 10 lines · David Hildenbrand
Quoted 9 lines · Pankaj Raghav (Samsung)

From: Pankaj Raghav <[email protected]>

split_huge_page_to_list_[to_order](), split_huge_page() and try_folio_split() return 0 on success and error codes on failure.

When THP is disabled, these functions return 0 indicating success even though an error code should be returned as it is not possible to split a folio when THP is disabled.

Quoted 9 lines
Quoted 6 lines

Make all these functions return -EINVAL to indicate failure instead of 0.

This issue was discovered while experimenting enabling large folios without THP and found that returning 0 in these functions is resulting in undefined behavior in truncate operations. This change fixes the issue.

Currently large folios that could be split are impossible without THP, so why should this be a fix?

I was debating with myself whether it will classify as a fix or not. I have had people tell me that I should mention it as fix even though I didn't feel like it ;)

But I agree with you, we can't hit this issue in the upstream kernel. I can remove the fixes tag in the next version.

--
Pankaj

#3 lore raw
Quoted 19 lines · Pankaj Raghav
Quoted 17 lines · David Hildenbrand
Quoted 15 lines · Pankaj Raghav (Samsung)

From: Pankaj Raghav <[email protected]>

split_huge_page_to_list_[to_order](), split_huge_page() and try_folio_split() return 0 on success and error codes on failure.

When THP is disabled, these functions return 0 indicating success even though an error code should be returned as it is not possible to split a folio when THP is disabled.

Make all these functions return -EINVAL to indicate failure instead of 0.

This issue was discovered while experimenting enabling large folios without THP and found that returning 0 in these functions is resulting in undefined behavior in truncate operations. This change fixes the issue.

Quoted 7 lines
Quoted 2 lines

Currently large folios that could be split are impossible without THP, so why should this be a fix?

I was debating with myself whether it will classify as a fix or not. I have had people tell me that I should mention it as fix even though I didn't feel like it ;)

But I agree with you, we can't hit this issue in the upstream kernel. I can remove the fixes tag in the next version.

No need to resend, I think Andrew can just drop the comment.

I'll not that I assume with any large folio support (except unsplittable things like hugetlb), we would require splitting support.

So calling these functions would rather indicate a bug I think.

Fine with making them return -EINVAL.

With the Fixes: tag dropped

Signature · 3 lines
Cheers

David / dhildenb
#4 lore raw
In reply to Pankaj Raghav (Samsung)
Quoted 8 lines · Pankaj Raghav (Samsung)

From: Pankaj Raghav <[email protected]>

split_huge_page_to_list_[to_order](), split_huge_page() and try_folio_split() return 0 on success and error codes on failure.

When THP is disabled, these functions return 0 indicating success even though an error code should be returned as it is not possible to split a folio when THP is disabled.

Other view is that the page is already split therefore nop.

Quoted 6 lines

Make all these functions return -EINVAL to indicate failure instead of 0.

This issue was discovered while experimenting enabling large folios without THP and found that returning 0 in these functions is resulting in undefined behavior in truncate operations. This change fixes the issue.

Could you elaborate on the undefined behaviour? I don't see it.

If you argue that this code should not be reachable on !THP config, add WARN() there. But I don't see a value.

Signature · 1 line
  Kiryl Shutsemau / Kirill A. Shutemov
#5 lore raw
Quoted 15 lines · Kiryl Shutsemau
Quoted 8 lines · Pankaj Raghav (Samsung)

From: Pankaj Raghav <[email protected]>

split_huge_page_to_list_[to_order](), split_huge_page() and try_folio_split() return 0 on success and error codes on failure.

When THP is disabled, these functions return 0 indicating success even though an error code should be returned as it is not possible to split a folio when THP is disabled.

Other view is that the page is already split therefore nop.

Quoted 3 lines

Make all these functions return -EINVAL to indicate failure instead of 0.

Quoted 8 lines
Quoted 3 lines

This issue was discovered while experimenting enabling large folios without THP and found that returning 0 in these functions is resulting in undefined behavior in truncate operations. This change fixes the issue.

Could you elaborate on the undefined behaviour? I don't see it.

If you argue that this code should not be reachable on !THP config, add WARN() there. But I don't see a value.

Little bit of context:

I started investigating what it takes to remove large folio dependency on THP[1][2]

I have some non-upstream changes which enables Large block size (therefore it uses large folios) on systems with !CONFIG_THP.

I was hitting a weird stale content read error and finally ended up with this fix.

I thought this is a self-contained patch that can already be upstream. My argument is not that this should not be reachable, but returning -EINVAL will do the right thing instead of returning 0, which means success.

I hope it clarifies a bit. Let me know what you think.

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

#6 lore raw
Quoted 30 lines · Pankaj Raghav
Quoted 15 lines · Kiryl Shutsemau
Quoted 8 lines · Pankaj Raghav (Samsung)

From: Pankaj Raghav <[email protected]>

split_huge_page_to_list_[to_order](), split_huge_page() and try_folio_split() return 0 on success and error codes on failure.

When THP is disabled, these functions return 0 indicating success even though an error code should be returned as it is not possible to split a folio when THP is disabled.

Other view is that the page is already split therefore nop.

Quoted 3 lines

Make all these functions return -EINVAL to indicate failure instead of 0.

Quoted 8 lines
Quoted 3 lines

This issue was discovered while experimenting enabling large folios without THP and found that returning 0 in these functions is resulting in undefined behavior in truncate operations. This change fixes the issue.

Could you elaborate on the undefined behaviour? I don't see it.

If you argue that this code should not be reachable on !THP config, add WARN() there. But I don't see a value.

Little bit of context:

I started investigating what it takes to remove large folio dependency on THP[1][2]

Quoted 8 lines

I have some non-upstream changes which enables Large block size (therefore it uses large folios) on systems with !CONFIG_THP.

I was hitting a weird stale content read error and finally ended up with this fix.

I thought this is a self-contained patch that can already be upstream. My argument is not that this should not be reachable, but returning -EINVAL will do the right thing instead of returning 0, which means success.

Okay, makes sense.

In THP=y case, __folio_split() also returns -EINVAL for !large folios, but it is not very explicit:

	if (new_order >= folio_order(folio))
		return -EINVAL;

In THP=y, we also issue warning:

	VM_WARN_ON_ONCE_FOLIO(!folio_test_locked(folio), folio);

Makes sense to do the same here for THP=n. It might help to catch cases we do not see with THP=y, like getting non-THP large folios here.

Signature · 1 line
  Kiryl Shutsemau / Kirill A. Shutemov
#7 lore raw
Quoted 11 lines
Quoted 5 lines

I was hitting a weird stale content read error and finally ended up with this fix.

I thought this is a self-contained patch that can already be upstream. My argument is not that this should not be reachable, but returning -EINVAL will do the right thing instead of returning 0, which means success.

Okay, makes sense.

In THP=y case, __folio_split() also returns -EINVAL for !large folios, but it is not very explicit:

Quoted 7 lines
	if (new_order >= folio_order(folio))
		return -EINVAL;

In THP=y, we also issue warning:

	VM_WARN_ON_ONCE_FOLIO(!folio_test_locked(folio), folio);

You mean:

VM_WARN_ON_ONCE_FOLIO(!folio_test_large(folio), folio);

Quoted 3 lines

Makes sense to do the same here for THP=n. It might help to catch cases we do not see with THP=y, like getting non-THP large folios here.

Yeah, I think that is a good idea. Something like this:

include/linux/huge_mm.h +8 -0
index 48c4f91c5b13..4ddf9e87db91 100644
@@ -588,21 +588,29 @@ static inline int
 split_huge_page_to_list_to_order(struct page *page, struct list_head *list,
                unsigned int new_order)
 {
+       struct folio *folio = page_folio(page);
+
+       VM_WARN_ON_ONCE_FOLIO(!folio_test_large(folio), folio);
        return -EINVAL;
 }
 static inline int split_huge_page(struct page *page)
 {
+       struct folio *folio = page_folio(page);
+
+       VM_WARN_ON_ONCE_FOLIO(!folio_test_large(folio), folio);
        return -EINVAL;
 }
 
 static inline int split_folio_to_list(struct folio *folio, struct list_head *list)
 {
+       VM_WARN_ON_ONCE_FOLIO(!folio_test_large(folio), folio);
        return -EINVAL;
 }
 
 static inline int try_folio_split(struct folio *folio, struct page *page,
                struct list_head *list)
 {
+       VM_WARN_ON_ONCE_FOLIO(!folio_test_large(folio), folio);
        return -EINVAL;
 }
 

--
Pankaj

#8 lore raw
Quoted 14 lines · Pankaj Raghav
Quoted 14 lines
Quoted 5 lines

I was hitting a weird stale content read error and finally ended up with this fix.

I thought this is a self-contained patch that can already be upstream. My argument is not that this should not be reachable, but returning -EINVAL will do the right thing instead of returning 0, which means success.

Okay, makes sense.

In THP=y case, __folio_split() also returns -EINVAL for !large folios, but it is not very explicit:

	if (new_order >= folio_order(folio))
		return -EINVAL;
Quoted 7 lines
Quoted 4 lines

In THP=y, we also issue warning:

	VM_WARN_ON_ONCE_FOLIO(!folio_test_locked(folio), folio);

You mean:

VM_WARN_ON_ONCE_FOLIO(!folio_test_large(folio), folio);

Yeah, copied wrong line.

Quoted 11 lines
Quoted 3 lines

Makes sense to do the same here for THP=n. It might help to catch cases we do not see with THP=y, like getting non-THP large folios here.

Yeah, I think that is a good idea. Something like this:

include/linux/huge_mm.h +0 -0
index 48c4f91c5b13..4ddf9e87db91 100644

include/linux/huge_mm.h static inline int · line 593

Quoted 6 lines
include/linux/huge_mm.h +3 -0
@@ -588,21 +588,29 @@ static inline int
 split_huge_page_to_list_to_order(struct page *page, struct list_head *list,
                unsigned int new_order)
 {
+       struct folio *folio = page_folio(page);
+
+       VM_WARN_ON_ONCE_FOLIO(!folio_test_large(folio), folio);

No. Make it unconditional. The point is we don't expect to see any splitable folios, so no reason to get here at all.

You can try to use BUILD_BUG(), but it can be too messy.

Signature · 1 line
  Kiryl Shutsemau / Kirill A. Shutemov
#9 lore raw
Quoted 16 lines

include/linux/huge_mm.h static inline int · line 593

Quoted 12 lines
include/linux/huge_mm.h +3 -0
index 48c4f91c5b13..4ddf9e87db91 100644
@@ -588,21 +588,29 @@ static inline int
 split_huge_page_to_list_to_order(struct page *page, struct list_head *list,
                unsigned int new_order)
 {
+       struct folio *folio = page_folio(page);
+
+       VM_WARN_ON_ONCE_FOLIO(!folio_test_large(folio), folio);

No. Make it unconditional. The point is we don't expect to see any splitable folios, so no reason to get here at all.

Got it.

Just one question though, in a future world where we remove the dependency between large folios and THP, then we can revert back this change to do a conditional WARN_ON?

--
Pankaj

#10 lore raw
Quoted 17 lines · Pankaj Raghav (Samsung)
Quoted 16 lines

include/linux/huge_mm.h static inline int · line 593

Quoted 12 lines
include/linux/huge_mm.h +3 -0
index 48c4f91c5b13..4ddf9e87db91 100644
@@ -588,21 +588,29 @@ static inline int
  split_huge_page_to_list_to_order(struct page *page, struct list_head *list,
                 unsigned int new_order)
  {
+       struct folio *folio = page_folio(page);
+
+       VM_WARN_ON_ONCE_FOLIO(!folio_test_large(folio), folio);

No. Make it unconditional. The point is we don't expect to see any splitable folios, so no reason to get here at all.

Quoted 5 lines

Got it.

Just one question though, in a future world where we remove the dependency between large folios and THP, then we can revert back this change to do a conditional WARN_ON?

I think we would never expect to get called to split something that is small. Calling code should be fixed.

Signature · 3 lines
Cheers

David / dhildenb