huge_memory: return -EINVAL in folio split functions when THP is disabled
Subsystems (2)
PATCH v2 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. As large folios depend on CONFIG_THP, issue warning as this function should not be called without a large folio.
---
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 | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-)
include/linux/huge_mm.h +12 -4
@@ -588,22 +588,30 @@ static inline int split_huge_page_to_list_to_order(struct page *page, struct list_head *list, unsigned int new_order) { - return 0; + struct folio *folio = page_folio(page); + +VM_WARN_ON_ONCE_FOLIO (1 , folio); +return -EINVAL ; }static inline int split_huge_page (struct page *page) { -return 0 ; +struct folio *folio =page_folio (page); + +VM_WARN_ON_ONCE_FOLIO (1 , folio); +return -EINVAL ; }static inline int split_folio_to_list (struct folio *folio,struct list_head *list) { -return 0 ; +VM_WARN_ON_ONCE_FOLIO (1 , folio); +return -EINVAL ; }static inline int try_folio_split (struct folio *folio,struct page *page,struct list_head *list) { -return 0 ; +VM_WARN_ON_ONCE_FOLIO (1 , folio); +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. As large folios depend onCONFIG_THP, issue warning as this function should not be called without a large folio.Signed-off-by Pankaj Raghav <[email protected]>---
Signature · 3 lines
Cheers David / dhildenb
Quoted 13 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. As large folios depend onCONFIG_THP, issue warning as this function should not be called without a large folio.
Quoted 9 lines
Signed-off-by Pankaj Raghav <[email protected]>---
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 | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-)
Best Regards,
Yan, Zi
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 5 lines
Make all these functions return
-EINVALto indicate failure instead of
0. As large folios depend onCONFIG_THP, issue warning as this function should not be called without a large folio.Signed-off-by Pankaj Raghav <[email protected]>
Just curious, did you give BUILD_BUG() a try?
Signature · 1 line
Kiryl Shutsemau / Kirill A. Shutemov
Quoted 14 lines · Kiryl Shutsemau
Quoted 13 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. As large folios depend onCONFIG_THP, issue warning as this function should not be called without a large folio.
Quoted 6 lines
Quoted 1 line
Signed-off-by Pankaj Raghav <[email protected]>Acked-by Kiryl Shutsemau <[email protected]>Just curious, did you give
BUILD_BUG()a try?
Yes, I tried BUILD_BUG() but it actually fails the build because truncate_inode_partial_folio() calls try_folio_split().
It won't be called in runtime because there is a check for large folios before we call this function. So a runtime warning is better in this case.
--
Pankaj
Quoted 19 lines · Pankaj Raghav
Quoted 18 lines · Kiryl Shutsemau
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 5 lines
Make all these functions return
-EINVALto indicate failure instead of
0. As large folios depend onCONFIG_THP, issue warning as this function should not be called without a large folio.Signed-off-by Pankaj Raghav <[email protected]>Acked-by Kiryl Shutsemau <[email protected]>
Quoted 8 lines
Quoted 2 lines
Just curious, did you give
BUILD_BUG()a try?Yes, I tried
BUILD_BUG()but it actually fails the build becausetruncate_inode_partial_folio()callstry_folio_split().It won't be called in runtime because there is a check for large folios before we call this function. So a runtime warning is better in this case.
Right, folio_test_large() is not compiled out.
Signature · 3 lines
Cheers David / dhildenb
Quoted 19 lines · Pankaj Raghav
Quoted 18 lines · Kiryl Shutsemau
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 5 lines
Make all these functions return
-EINVALto indicate failure instead of
0. As large folios depend onCONFIG_THP, issue warning as this function should not be called without a large folio.Signed-off-by Pankaj Raghav <[email protected]>Acked-by Kiryl Shutsemau <[email protected]>
Quoted 8 lines
Quoted 2 lines
Just curious, did you give
BUILD_BUG()a try?Yes, I tried
BUILD_BUG()but it actually fails the build becausetruncate_inode_partial_folio()callstry_folio_split().It won't be called in runtime because there is a check for large folios before we call this function. So a runtime warning is better in this case.
If truncate_inode_partial_folio() shouldn't ever be calling on THP=n kernel, it also can be BUILD_BUG(). Just saying :P
Signature · 1 line
Kiryl Shutsemau / Kirill A. Shutemov
kernel test robot
build botkernel test robot noticed the following build warnings:
Hi Pankaj,
kernel test robot noticed the following build warnings:
[auto build test WARNING on 291634ccfd2820c09f6e8c4982c2dee8155d09ae]
url: https://github.com/intel-lab-lkp/linux/commits/Pankaj-Raghav-Samsung/huge_memory-return-EINVAL-in-folio-split-functions-when-THP-is-disabled/20250904-175359 base: 291634ccfd2820c09f6e8c4982c2dee8155d09ae patch link: https://lore.kernel.org/r/20250904095129.222316-1-kernel%40pankajraghav.com patch subject: [PATCH v2] huge_memory: return -EINVAL in folio split functions when THP is disabled config: i386-buildonly-randconfig-001-20250905 (https://download.01.org/0day-ci/archive/20250905/[email protected]/config) compiler: clang version 20.1.8 (https://github.com/llvm/llvm-project 87f0227cb60147a26a1eeb4fb06e3b505e9c7261) reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20250905/[email protected]/reproduce)
If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/
All warnings (new ones prefixed by >>):
In file included from arch/x86/kernel/asm-offsets.c:14:
In file included from include/linux/suspend.h:5:
In file included from include/linux/swap.h:9:
In file included from include/linux/memcontrol.h:21:
In file included from include/linux/mm.h:1081:
>> include/linux/huge_mm.h:591:16: warning: unused variable 'folio' [-Wunused-variable]
591 | struct folio *folio = page_folio(page);
| ^~~~~
include/linux/huge_mm.h:598:16: warning: unused variable 'folio' [-Wunused-variable]
598 | struct folio *folio = page_folio(page);
| ^~~~~ 2 warnings generated.
--
In file included from drivers/media/mc/mc-entity.c:16:
In file included from include/media/media-device.h:16:
In file included from include/linux/pci.h:2169:
In file included from arch/x86/include/asm/pci.h:5:
In file included from include/linux/mm.h:1081:
>> include/linux/huge_mm.h:591:16: warning: unused variable 'folio' [-Wunused-variable]
591 | struct folio *folio = page_folio(page);
| ^~~~~
include/linux/huge_mm.h:598:16: warning: unused variable 'folio' [-Wunused-variable]
598 | struct folio *folio = page_folio(page);
| ^~~~~ drivers/media/mc/mc-entity.c:18:27: warning: unused function 'intf_type' [-Wunused-function]
18 | static inline const char *intf_type(struct media_interface *intf)
| ^~~~~~~~~
drivers/media/mc/mc-entity.c:48:27: warning: unused function 'link_type_name' [-Wunused-function]
48 | static inline const char *link_type_name(struct media_link *link)
| ^~~~~~~~~~~~~~
4 warnings generated.
--
In file included from mc-entity.c:16:
In file included from include/media/media-device.h:16:
In file included from include/linux/pci.h:2169:
In file included from arch/x86/include/asm/pci.h:5:
In file included from include/linux/mm.h:1081:
>> include/linux/huge_mm.h:591:16: warning: unused variable 'folio' [-Wunused-variable]
591 | struct folio *folio = page_folio(page);
| ^~~~~
include/linux/huge_mm.h:598:16: warning: unused variable 'folio' [-Wunused-variable]
598 | struct folio *folio = page_folio(page);
| ^~~~~ mc-entity.c:18:27: warning: unused function 'intf_type' [-Wunused-function]
18 | static inline const char *intf_type(struct media_interface *intf)
| ^~~~~~~~~
mc-entity.c:48:27: warning: unused function 'link_type_name' [-Wunused-function]
48 | static inline const char *link_type_name(struct media_link *link)
| ^~~~~~~~~~~~~~
4 warnings generated.
--
In file included from arch/x86/kernel/asm-offsets.c:14:
In file included from include/linux/suspend.h:5:
In file included from include/linux/swap.h:9:
In file included from include/linux/memcontrol.h:21:
In file included from include/linux/mm.h:1081:
>> include/linux/huge_mm.h:591:16: warning: unused variable 'folio' [-Wunused-variable]
591 | struct folio *folio = page_folio(page);
| ^~~~~
include/linux/huge_mm.h:598:16: warning: unused variable 'folio' [-Wunused-variable]
598 | struct folio *folio = page_folio(page);
| ^~~~~2 warnings generated.
vim +/folio +591 include/linux/huge_mm.h
581
582 static inline bool
583 can_split_folio(struct folio *folio, int caller_pins, int *pextra_pins)
584 {
585 return false;
586 }
587 static inline int
588 split_huge_page_to_list_to_order(struct page *page, struct list_head *list,
589 unsigned int new_order)
590 {
> 591 struct folio *folio = page_folio(page);
592
593 VM_WARN_ON_ONCE_FOLIO(1, folio);
594 return -EINVAL;
595 }
596 static inline int split_huge_page(struct page *page)
597 {
598 struct folio *folio = page_folio(page);
599
600 VM_WARN_ON_ONCE_FOLIO(1, folio);
601 return -EINVAL;
602 }
603
Signature · 2 lines
0-DAY CI Kernel Test Service https://github.com/intel/lkp-tests/wiki
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 5 lines
Make all these functions return
-EINVALto indicate failure instead of
0. As large folios depend onCONFIG_THP, issue warning as this function should not be called without a large folio.Signed-off-by Pankaj Raghav <[email protected]>
Other than the trivially-fixable issue mentioned below this LGTM so:
On basis of those being fixed.
Quoted 13 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.include/linux/huge_mm.h | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-)include/linux/huge_mm.h +0
-0
include/linux/huge_mm.h static inline int · line 593
Quoted 7 lines
include/linux/huge_mm.h +3
-1@@ -588,22 +588,30 @@ static inline int split_huge_page_to_list_to_order(struct page *page, struct list_head *list, unsigned int new_order) { - return 0; + struct folio *folio = page_folio(page); + +VM_WARN_ON_ONCE_FOLIO (1 , folio);
As per the bot report, you should just do page_folio(page) here or otherwise when compiled out you'll get an unused var warning.
include/linux/huge_mm.h static inline int · line 598
Quoted 6 lines
include/linux/huge_mm.h +2
-1+return -EINVAL ; }static inline int split_huge_page (struct page *page) { -return 0 ; +struct folio *folio =page_folio (page);
Same as above!
Quoted 26 lines
include/linux/huge_mm.h +7
-2+ +VM_WARN_ON_ONCE_FOLIO (1 , folio); +return -EINVAL ; }static inline int split_folio_to_list (struct folio *folio,struct list_head *list) { -return 0 ; +VM_WARN_ON_ONCE_FOLIO (1 , folio); +return -EINVAL ; }static inline int try_folio_split (struct folio *folio,struct page *page,struct list_head *list) { -return 0 ; +VM_WARN_ON_ONCE_FOLIO (1 , folio); +return -EINVAL ; }static inline void deferred_split_folio (struct folio *folio,bool partially_mapped) {}base-commit:
291634ccfd2820c09f6e8c4982c2dee8155d09ae--
2.50.1