huge_memory: return -EINVAL in folio split functions when THP is disabled
Subsystems (2)
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.
--- include/linux/huge_mm.h | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-)
include/linux/huge_mm.h +4 -4
@@ -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
Quoted 9 lines · Pankaj Raghav (Samsung)
From: Pankaj Raghav <[email protected]>
split_huge_page_to_list_[to_order](),
split_huge_page()andtry_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
-EINVALto 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
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()andtry_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
-EINVALto 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
David Hildenbrand
Acked-byQuoted 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()andtry_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
-EINVALto 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
Quoted 8 lines · Pankaj Raghav (Samsung)
From: Pankaj Raghav <[email protected]>
split_huge_page_to_list_[to_order](),
split_huge_page()andtry_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
-EINVALto 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
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()andtry_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
-EINVALto 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
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()andtry_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
-EINVALto 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
-EINVALwill 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
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
-EINVALwill do the right thing instead of returning 0, which means success.Okay, makes sense.
In THP=y case,
__folio_split()also returns-EINVALfor !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
@@ -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
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
-EINVALwill do the right thing instead of returning 0, which means success.Okay, makes sense.
In THP=y case,
__folio_split()also returns-EINVALfor !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
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
Quoted 16 lines
include/linux/huge_mm.h static inline int · line 593
Quoted 12 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.
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
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@@ -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