Improve folio split related functions
Subsystems (4)
Zi Yan
reviewerv4 cover Improve folio split related functions
Hi all,
This patchset improves several folio split related functions to avoid future misuse. The changes are:
1. Consolidated folio splittable checks by moving truncated folio check, huge zero folio check, and writeback folio check into folio_split_supported(). Changed the function return type. Renamed it to folio_check_splittable() for clarification.
2. Replaced can_split_folio() with open coded folio_expected_ref_count() and folio_ref_count() and introduced folio_cache_ref_count().
3. Changed min_order_for_split() to always return an order.
4. Fixed folio split stats counting.
Motivation
===
This is based on Wei's observation[1] and solves several potential issues:
1. Dereferencing NULL folio->mapping in try_folio_split_to_order() if it is called on truncated folios.
2. Not handling of negative return value of min_order_for_split() in mm/memory-failure.c
There is no bug in the current code.
The code is based on latest mm-new with V2 reverted and can replace V2 cleanly on mm-new branch.
Changelog
===
From V3[4]:
1. Reordered conditions when checking truncated shmem or to-be-anon folios.
2. Removed expected_refs.
From V2[3]:
1. Removed "bool warns" parameter from folio_check_splittable().
2. Removed all warnings in folio_check_splittable() and added a single warning in its caller, __folio_split() instead.
3. Spelled out in the comment in folio_check_splittable() that folios without a mapping in the swapcache can be shmem or to-be-anon folios.
4. Renamed folio_cache_references to folio_cache_ref_count.
5. Removed extra_pins variable.
6. Replaced folio_expected_ref_count() with folio_cache_ref_count() for folio_ref_unfreeze() uses in __folio_freeze_and_split_unmapped(), since they are equivalent at those call sites.
From RFC[2]:
1. Renamed folio_split_supported() to folio_check_splittable(), changed its return type from bool to int to return error code directly, and added kernel-doc.
2. Moved truncated folio check, zero huge folio check, and writeback check in folio_check_splittable().
3. Changed zero huge folio check's error number from -EBUSY to -EINVAL.
4. Replaced can_split_folio() with open code.
5. Changed min_order_for_split() to return 0 for truncated folio instead of -EBUSY and added kernel-doc.
6. Fixed folio split stats counting.
Comments and feedbacks are welcome.
Zi Yan (4):
mm/huge_memory: change folio_split_supported() to
folio_check_splittable()
mm/huge_memory: replace can_split_folio() with direct refcount
calculation
mm/huge_memory: make min_order_for_split() always return an order mm/huge_memory: fix folio split stats counting
include/linux/huge_mm.h | 13 ++-- mm/huge_memory.c | 165 ++++++++++++++++++++++------------------ mm/vmscan.c | 3 +- 3 files changed, 98 insertions(+), 83 deletions(-)
Signature · 1 line
2.51.0
Zi Yan
reviewerPATCH v4 1/4 mm/huge_memory: change folio_split_supported() to folio_check_splittable()
folio_split_supported() used in try_folio_split_to_order() requires folio->mapping to be non NULL, but current try_folio_split_to_order() does not check it. There is no issue in the current code, since try_folio_split_to_order() is only used in truncate_inode_partial_folio(), where folio->mapping is not NULL.
To prevent future misuse, move folio->mapping NULL check (i.e., folio is truncated) into folio_split_supported(). Since folio->mapping NULL check returns -EBUSY and folio_split_supported() == false means -EINVAL, change folio_split_supported() return type from bool to int and return error numbers accordingly. Rename folio_split_supported() to folio_check_splittable() to match the return type change.
While at it, move is_huge_zero_folio() check and folio_test_writeback() check into folio_check_splittable() and add kernel-doc.
Remove all warnings inside folio_check_splittable() and give warnings in __folio_split() instead, so that bool warns parameter can be removed.
--- include/linux/huge_mm.h | 6 ++-- mm/huge_memory.c | 76 +++++++++++++++++++++++------------------ 2 files changed, 46 insertions(+), 36 deletions(-)
include/linux/huge_mm.h +3 -3
@@ -375,8 +375,8 @@ int __split_huge_page_to_list_to_order(struct page *page, struct list_head *listint folio_split_unmapped (struct folio *folio,unsigned int new_order);int min_order_for_split (struct folio *folio);int split_folio_to_list (struct folio *folio,struct list_head *list); -bool folio_split_supported (struct folio *folio,unsigned int new_order, -enum split_type split_type,bool warns); +int folio_check_splittable (struct folio *folio,unsigned int new_order, +enum split_type split_type);int folio_split (struct folio *folio,unsigned int new_order,struct page *page,struct list_head *list);
@@ -407,7 +407,7 @@ static inline int split_huge_page_to_order(struct page *page, unsigned int new_ostatic inline int try_folio_split_to_order (struct folio *folio,struct page *page,unsigned int new_order) { -if (!folio_split_supported (folio, new_order,SPLIT_TYPE_NON_UNIFORM ,/* warns= */ false)) +if (folio_check_splittable (folio, new_order,SPLIT_TYPE_NON_UNIFORM ))return split_huge_page_to_order (&folio->page, new_order);return folio_split (folio, new_order, page,NULL ); }
mm/huge_memory.c +43 -33
117 lines not on this page · raw message
Signature · 1 line
2.51.0
Zi Yan
reviewerPATCH v4 2/4 mm/huge_memory: replace can_split_folio() with direct refcount calculation
In reply to Zi Yancan_split_folio() is just a refcount comparison, making sure only the split caller holds an extra pin. Open code it with folio_expected_ref_count() != folio_ref_count() - 1. For the extra_pins used by folio_ref_freeze(), add folio_cache_ref_count() to calculate it. Also replace folio_expected_ref_count() with folio_cache_ref_count() used by folio_ref_unfreeze(), since they are returning the same values when a folio is frozen and folio_cache_ref_count() does not have unnecessary folio_mapcount() in its implementation.
--- include/linux/huge_mm.h | 1 - mm/huge_memory.c | 52 ++++++++++++++++------------------------- mm/vmscan.c | 3 ++- 3 files changed, 22 insertions(+), 34 deletions(-)
include/linux/huge_mm.h +0 -1
@@ -369,7 +369,6 @@ enum split_type { SPLIT_TYPE_NON_UNIFORM, }; -bool can_split_folio (struct folio *folio,int caller_pins,int *pextra_pins);int __split_huge_page_to_list_to_order (struct page *page,struct list_head *list,unsigned int new_order);int folio_split_unmapped (struct folio *folio,unsigned int new_order);
mm/huge_memory.c +20 -32
129 lines not on this page · raw message
mm/vmscan.c +2 -1
@@ -1284,7 +1284,8 @@ static unsigned int shrink_folio_list(struct list_head *folio_list, goto keep_locked;if (folio_test_large (folio)) {/* cannot split folio, skip it */ -if (!can_split_folio (folio,1 ,NULL )) +if (folio_expected_ref_count (folio) != +folio_ref_count (folio) -1 ) goto activate_locked;/* * Split partially mapped folios right away.
Signature · 1 line
2.51.0
Zi Yan
reviewerPATCH v4 3/4 mm/huge_memory: make min_order_for_split() always return an order
In reply to Zi Yanmin_order_for_split() returns -EBUSY when the folio is truncated and cannot be split. In commit 77008e1b2ef7 ("mm/huge_memory: do not change split_huge_page*() target order silently"), memory_failure() does not handle it and pass -EBUSY to try_to_split_thp_page() directly. try_to_split_thp_page() returns -EINVAL since -EBUSY becomes 0xfffffff0 as new_order is unsigned int in __folio_split() and this large new_order is rejected as an invalid input. The code does not cause a bug. soft_offline_in_use_page() also uses min_order_for_split() but it always passes 0 as new_order for split.
Fix it by making min_order_for_split() always return an order. When the given folio is truncated, namely folio->mapping == NULL, return 0 and let a subsequent split function handle the situation and return -EBUSY.
Add kernel-doc to min_order_for_split() to clarify its use.
--- include/linux/huge_mm.h | 6 +++--- mm/huge_memory.c | 25 +++++++++++++++++++------ 2 files changed, 22 insertions(+), 9 deletions(-)
include/linux/huge_mm.h +3 -3
@@ -372,7 +372,7 @@ enum split_type {int __split_huge_page_to_list_to_order (struct page *page,struct list_head *list,unsigned int new_order);int folio_split_unmapped (struct folio *folio,unsigned int new_order); -int min_order_for_split (struct folio *folio); +unsigned int min_order_for_split (struct folio *folio);int split_folio_to_list (struct folio *folio,struct list_head *list);int folio_check_splittable (struct folio *folio,unsigned int new_order,enum split_type split_type);
@@ -630,10 +630,10 @@ static inline int split_huge_page(struct page *page)return -EINVAL ; } -static inline int min_order_for_split (struct folio *folio) +static inline unsigned int min_order_for_split (struct folio *folio) {VM_WARN_ON_ONCE_FOLIO (1 , folio); -return -EINVAL ; +return 0 ; }static inline int split_folio_to_list (struct folio *folio,struct list_head *list)
mm/huge_memory.c +19 -6
@@ -4219,16 +4219,29 @@ int folio_split(struct folio *folio, unsigned int new_order, SPLIT_TYPE_NON_UNIFORM); } -int min_order_for_split (struct folio *folio) +/** +* min_order_for_split() - get the minimum order @folio can be split to +* @folio: folio to split +* +* min_order_for_split() tells the minimum order @folio can be split to. +* If a file-backed folio is truncated, 0 will be returned. Any subsequent +* split attempt should get -EBUSY from split checking code. +* +* Return: @folio's minimum order for split +*/ +unsigned int min_order_for_split (struct folio *folio) {if (folio_test_anon (folio))return 0 ; -if (!folio->mapping) { -if (folio_test_pmd_mappable (folio)) -count_vm_event (THP_SPLIT_PAGE_FAILED ); -return -EBUSY ; - } +/* +* If the folio got truncated, we don't know the previous mapping and +* consequently the old min order. But it doesn't matter, as any split +* attempt will immediately fail with -EBUSY as the folio cannot get +* split until freed. +*/ +if (!folio->mapping) +return 0 ;return mapping_min_folio_order (folio->mapping); }
Signature · 1 line
2.51.0
Zi Yan
reviewerPATCH v4 4/4 mm/huge_memory: fix folio split stats counting
In reply to Zi YanThe "return <error code>" statements for error checks at the beginning of __folio_split() skip necessary count_vm_event() and count_mthp_stat() at the end of the function. Fix these by replacing them with "ret = <error code>; goto out;".
--- mm/huge_memory.c | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-)
mm/huge_memory.c +9 -5
@@ -3942,16 +3942,20 @@ static int __folio_split(struct folio *folio, unsigned int new_order,VM_WARN_ON_ONCE_FOLIO (!folio_test_locked (folio), folio);VM_WARN_ON_ONCE_FOLIO (!folio_test_large (folio), folio); -if (folio !=page_folio (split_at) || folio !=page_folio (lock_at)) -return -EINVAL ; +if (folio !=page_folio (split_at) || folio !=page_folio (lock_at)) { + ret = -EINVAL ; + goto out; + } -if (new_order >= old_order) -return -EINVAL ; +if (new_order >= old_order) { + ret = -EINVAL ; + goto out; + } ret =folio_check_splittable (folio, new_order, split_type);if (ret) {VM_WARN_ONCE (ret == -EINVAL ,"Tried to split an unsplittable folio" ); -return ret; + goto out; }if (is_anon) {
Signature · 1 line
2.51.0