mm/huge_memory: clean up and speed up large folio splitting
Subsystems (6)
cover mm/huge_memory: clean up and speed up large folio splitting
Hi all,
This series slghtly cleans up the large folio split path and reduces its overhead: uniformly splitting PMD sized file folios to order 0 is over 20% faster.
I realize this might not be the best time for pushing more optimizations, but I am posting this as an early RFC as it adds two helpers, folio_prev() and xas_prev_canonical(), and uses them to walk the after-split folios in reverse, saving a few local variables, avoiding a for-loop pitfall [1], and publishing the folios with a single Xarray walk.
Walking backwards also changes the order the pieces are put on the LRU or the reclaim list: they are now in ascending order instead of descending, which is the actually intended order. The effect was not very measurable though.
New code is mostly the two helpers and an Xarray selftest. The change to mm/huge_memory.c is LOC-neutral.
Worth noting (not part of the series): I found these useful while preparation for supporting all kinds of folio splitting folios in the swap cache. I also tried to get rid of lruvec lock for the splitting, or at least decouple the lock and reduce lock scope of it. It turns out, not cleanly doable at this moment, and the main blocker is: MGLRU. MGLRU will read a folio's size without holding a ref, solely depending on the LRU lock to stabilize. To improve this, another rework of MGLRU's reclaim progress is needed (which will also cover the problem fount by the MGLRU-FG series that MGLRU sometime have a higher contention than CLRU on reclaim isolation).
Testing
=======
Split overhead on a 32 vCPU x86-64 KVM guest, 31 GiB RAM, 4K base pages, 2M THP. Split a 1 GiB shmem file (512 PMD-sized folios) uniformly to order 0 through /sys/kernel/debug/split_huge_pages and time that single write with bash's time. Medians over 12 interleaved rounds per kernel
Test script:
mount -t tmpfs -o size=2G,huge=always tmpfs /mnt fallocate -l 1G /mnt/big
time echo /mnt/big,0x0,0x40000,0 > /sys/kernel/debug/split_huge_pages rm -f /mnt/big
umount /mnt
Result:
Before After
real 17 ms 13 ms (-24%) sys 17 ms 13 ms (-24%)
End to end test showed no regression.
---
Kairui Song (8):
mm: add folio_prev()
xarray: add xas_prev_canonical()
mm/huge_memory: avoid an unnecessary folio size reading mm/huge_memory: walk the after-split file folios in reverse order mm/huge_memory: publish split folios by walking backwards mm/huge_memory: rearrange split routine handling for better optimization mm/huge_memory: walk the after-split anon folios in reverse order mm/huge_memory: walk the folios reversely for the release loop
include/linux/mm.h | 23 +++++++++++ include/linux/xarray.h | 49 +++++++++++++++++++++++ lib/test_xarray.c | 97 +++++++++++++++++++++++++++++++++++++++++++++ lib/xarray.c | 8 ---- mm/huge_memory.c | 105 +++++++++++++++++++++++++------------------------ 5 files changed, 222 insertions(+), 60 deletions(-)
---
base-commit: fae273e48c398c56b819151a607a6c8792dadef0
change-id: 20260915-swap-thp-split-0f86b554b42e
Best regards,
Signature · 1 line
Kairui Song <[email protected]>
RFC PATCH 1/8 mm: add folio_prev()
This is similar to folio_next(). But to get the head of the previous folio, the compound info of the current folio isn't helpful. So here just retrieve the previous page frame's compound info instead.
This creates a nice side effect that we don't require the current folio to be still valid as we don't need any info from it and @folio is never dereferenced, just pointer arithmetic.
The caller has to ensure the previous folio is legal though, as there is no reliable way to tell from @folio whether one exists.
--- include/linux/mm.h | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+)
include/linux/mm.h +23 -0
@@ -3076,6 +3076,29 @@ static inline struct folio *folio_next(struct folio *folio)return (struct folio *)folio_page (folio,folio_nr_pages (folio)); } +/** +* folio_prev - Move to the previous physical folio. +* @folio: The folio we're currently operating on. +* +* The inverse of folio_next(). Use it to move from one folio to the +* previous one in physically contiguous memory which may span more than +* one folio. Do not use it if the memory is only virtually contiguous. +* This is the folio equivalent to writing ``page--``. +* +* The caller has to ensure the previous folio is legal; there is no way +* to tell from @folio whether one exists. @folio itself is never +* dereferenced, only the page preceding it, so it may be a sentinel +* pointer instead of a folio. +* +* Context: We assume that the folios are refcounted and/or locked at a +* higher level and do not adjust the reference counts. +* Return: The previous struct folio. +*/ +static inline struct folio *folio_prev (struct folio *folio) +{ +return page_folio (&folio->page -1 ); +} +/** * folio_shift - The size of the memory described by this folio. * @folio: The folio.
Signature · 1 line
2.56.1
RFC PATCH 2/8 xarray: add xas_prev_canonical()
In reply to Kairui Songxas_prev() moves one index at a time, so stepping backwards into a multi-index entry can stop on a sibling slot. The new xas_prev_canonical() steps back over an entry as a whole: it leaves the state on the slot holding the entry and on the first index that entry covers, so xas_get_order() also reports its order.
A cursor already inside an entry reports it instead of stepping off.
Cover it in the test suite too.
--- include/linux/xarray.h | 49 +++++++++++++++++++++++++ lib/test_xarray.c | 97 ++++++++++++++++++++++++++++++++++++++++++++++++++ lib/xarray.c | 8 ----- 3 files changed, 146 insertions(+), 8 deletions(-)
include/linux/xarray.h +49 -0
@@ -1502,6 +1502,15 @@ static inline bool xas_top(struct xa_node *node)return node <=XAS_RESTART ; } +/* Move the index either forwards (find) or backwards (sibling slot) */ +static inline void xas_move_index (struct xa_state *xas,unsigned long offset) +{ +unsigned int shift = xas->xa_node->shift; + + xas->xa_index &= ~XA_CHUNK_MASK << shift; + xas->xa_index += offset << shift; +} +/** * xas_reset() - Reset an XArray operation state. * @xas: XArray operation state.
@@ -1883,6 +1892,46 @@ static inline void *xas_prev(struct xa_state *xas)return xa_entry (xas->xa, node, xas->xa_offset); } +/** +* xas_prev_canonical() - Move iterator to the previous entry, onto the slot +* which stores it. +* @xas: XArray operation state. +* +* Same as xas_prev(), but it steps back over a multi-index entry as a +* whole: the state is left on the slot which stores the entry and on its +* first index, so xas_get_order() reports its order. A cursor already +* inside an entry reports that entry instead of stepping off it. +* +* Context: Any context. Hold the xa_lock or the RCU lock across the +* whole walk; xas_pause() is forward-only. +* Return: The entry at the new position, or %NULL (empty slot, error +* state, or the end of the walk). The result may be an internal entry, +* which needs xas_retry(). +*/ +static inline void *xas_prev_canonical (struct xa_state *xas) +{ +void *entry =NULL ; + +/* step back unless @xas is already inside an entry */ +if (!xas_is_node (xas) || + xas->xa_offset >=XA_CHUNK_SIZE || + xas->xa_offset == ((xas->xa_index >> xas->xa_node->shift) & +XA_CHUNK_MASK )) + entry =xas_prev (xas); +if (!xas_is_node (xas)) +return entry; +if (xa_is_sibling (entry)) + xas->xa_offset =xa_to_sibling (entry); + + entry =xa_entry (xas->xa, xas->xa_node, xas->xa_offset); +/* a concurrent store or split can leave a sibling or a node here */ +if (xa_is_sibling (entry) || + (xas->xa_node->shift &&xa_is_node (entry))) +return XA_RETRY_ENTRY ; +xas_move_index (xas, xas->xa_offset); +return entry; +} +/** * xas_next() - Move state to next index. * @xas: XArray operation state.
lib/test_xarray.c +97 -0
109 lines not on this page · raw message
lib/xarray.c +0 -8
@@ -152,14 +152,6 @@ static void xas_set_offset(struct xa_state *xas) xas->xa_offset =get_offset (xas->xa_index, xas->xa_node); } -/* move the index either forwards (find) or backwards (sibling slot) */ -static void xas_move_index (struct xa_state *xas,unsigned long offset) -{ -unsigned int shift = xas->xa_node->shift; - xas->xa_index &= ~XA_CHUNK_MASK << shift; - xas->xa_index += offset << shift; -} -static void xas_next_offset (struct xa_state *xas) { xas->xa_offset++;
Signature · 1 line
2.56.1
[email protected]
AI reviewRe: [PATCH RFC 2/8] xarray: add xas_prev_canonical()
Thank you for your contribution! Sashiko AI review found 2 potential issues to consider: - [Medium] xarray: xas_prev_canonical() corrupts xa_index on XA_RETRY_ENTRY - [Medium] xarray…
Thank you for your contribution! Sashiko AI review found 2 potential issues to consider:
- [Medium] xarray: xas_prev_canonical() corrupts xa_index on XA_RETRY_ENTRY
- [Medium] xarray: xas_prev_canonical() steps off non-leaf canonical slot on split
--
commit b768624f5f5a261ca8a1796cf3f8c4a5e5deca0d
Author: Kairui Song <[email protected]>
xarray: add xas_prev_canonical()
This commit adds xas_prev_canonical() to step backwards over multi-index XArray entries onto the canonical slot and first index covered by the entry, along with test cases in the XArray test suite.
Quoted 4 lines
include/linux/xarray.h +0
-0
[ ... ]
Quoted 20 lines
include/linux/xarray.h +16
-0@@ -1883,6 +1892,46 @@ static inline void *xas_prev(struct xa_state *xas)return xa_entry (xas->xa, node, xas->xa_offset); } +/** +* xas_prev_canonical() - Move iterator to the previous entry, onto the slot +* which stores it. +* @xas: XArray operation state. +* +* Same as xas_prev(), but it steps back over a multi-index entry as a +* whole: the state is left on the slot which stores the entry and on its +* first index, so xas_get_order() reports its order. A cursor already +* inside an entry reports that entry instead of stepping off it. +* +* Context: Any context. Hold the xa_lock or the RCU lock across the +* whole walk; xas_pause() is forward-only. +* Return: The entry at the new position, or %NULL (empty slot, error +* state, or the end of the walk). The result may be an internal entry, +* which needs xas_retry(). +*/
include/linux/xarray.h static inline void *xas_prev(struct xa_state *xas) · line 1920
Quoted 10 lines
include/linux/xarray.h +10
-0+static inline void *xas_prev_canonical (struct xa_state *xas) +{ +void *entry =NULL ; + +/* step back unless @xas is already inside an entry */ +if (!xas_is_node (xas) || + xas->xa_offset >=XA_CHUNK_SIZE || + xas->xa_offset == ((xas->xa_index >> xas->xa_node->shift) & +XA_CHUNK_MASK )) + entry =xas_prev (xas);
[Severity: Medium]
Does shifting xas->xa_index right by xas->xa_node->shift step off an entry when the cursor is at a non-initial index inside the canonical slot of an internal node?
When an entry has order >= XA_CHUNK_SHIFT (xas->xa_node->shift > 0), the canonical slot itself covers (1UL << xas->xa_node->shift) indices. Discarding the lower shift bits makes xas->xa_offset equal ((xas->xa_index >> xas->xa_node->shift) & XA_CHUNK_MASK) for every index within that canonical slot, so xas_prev_canonical() calls xas_prev(xas) even when xas->xa_index is past the first index of the entry:
CPU0 CPU1xas_load(&xas)
/* order-6 entry at [128, 191],
xas_index = 150, shift = 6,
xa_offset = 2 */
xas_split()
/* splits slot 2 into child node */
xas_prev_canonical(&xas)
/* (150 >> 6) & 63 == 2 */
xas_prev(&xas)
__xas_prev(&xas)
xas->xa_index--; /* 149 */
/* descends into child node
at 149 instead of retrying
at 150 */ <-- skips entry at 150Preserving the lower shift bits the way xas_sibling() does (comparing xas->xa_index & ((XA_CHUNK_SIZE << node->shift) - 1) against (unsigned long)xas->xa_offset << node->shift) would avoid calling xas_prev() when the cursor is already inside the canonical slot.
include/linux/xarray.h static inline void *xas_prev(struct xa_state *xas) · line 1933
Quoted 13 lines
include/linux/xarray.h +13
-0+if (!xas_is_node (xas)) +return entry; +if (xa_is_sibling (entry)) + xas->xa_offset =xa_to_sibling (entry); + + entry =xa_entry (xas->xa, xas->xa_node, xas->xa_offset); +/* a concurrent store or split can leave a sibling or a node here */ +if (xa_is_sibling (entry) || + (xas->xa_node->shift &&xa_is_node (entry))) +return XA_RETRY_ENTRY ; +xas_move_index (xas, xas->xa_offset); +return entry; +}
[Severity: Medium]
Can xas_prev_canonical() move xas->xa_index backwards before returning XA_RETRY_ENTRY, or lose an XA_RETRY_ENTRY returned by xas_prev()?
During a lockless RCU walk, xas_retry() calls xas_reset(), which sets xas->xa_node = XAS_RESTART and relies on xas->xa_index remaining at the index that needs to be retried so the next __xas_prev() call reloads that same index. Here, if xas_prev() returns a sibling entry and the canonical slot at xas->xa_offset has been overwritten with XA_RETRY_ENTRY by a concurrent xas_free_nodes() or xas_shrink(), XA_RETRY_ENTRY is neither a sibling nor a node, so execution calls xas_move_index(xas, xas->xa_offset) before returning XA_RETRY_ENTRY:
CPU0 CPU1xas_prev_canonical(&xas)
entry = xas_prev(&xas)
/* lands on sibling slot,
returns xa_mk_sibling(off) */
xas->xa_offset = off;
xas_free_nodes()
/* overwrites slot[off] with
XA_RETRY_ENTRY */
entry = xa_entry(..., off);
/* entry is XA_RETRY_ENTRY */ xas_move_index(&xas, off); <-- lowers xas->xa_index
return XA_RETRY_ENTRY;xas_retry(&xas, entry)
xas_reset(&xas);xas_prev_canonical(&xas)
__xas_prev(&xas)
xas_load(&xas) <-- reloads at lowered xa_index,
skipping intermediate entries
In addition, if xas_prev() itself returns XA_RETRY_ENTRY (for example via __xas_prev() -> xas_load() -> xas_descend() when a sibling in an internal node was concurrently split into a child node), xas_prev_canonical() unconditionally overwrites entry with xa_entry() and loses the retry if that canonical slot was concurrently replaced with a normal entry.
Checking xa_is_retry(entry) after xas_prev() and before xas_move_index() would preserve both the retry entry and xas->xa_index.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
RFC PATCH 3/8 mm/huge_memory: avoid an unnecessary folio size reading
In reply to Kairui SongWe already have that in the local variable, a trivial cleanup.
--- mm/huge_memory.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-)
mm/huge_memory.c +1 -3
@@ -4287,9 +4287,7 @@ static int __folio_freeze_split_file(struct folio *folio,/* compute next before the folio can be freed below */ next =folio_next (new_folio); -folio_ref_unfreeze (new_folio, -folio_nr_pages (new_folio) +1 ); - +folio_ref_unfreeze (new_folio, nr_pages +1 );lru_add_split_folio (folio, new_folio, lruvec, list);/* Add the new folio to the page cache. */
Signature · 1 line
2.56.1
RFC PATCH 4/8 mm/huge_memory: walk the after-split file folios in reverse order
In reply to Kairui SongUse the new folio_prev() to walk the after-split range backwards. The @folio is still unfrozen after the loop, so it remains the last folio to be unfrozen and stays frozen until every page cache entry that used to point at it has been repointed to an after-split folio.
This improves the code in two ways: A local variable is saved, and this is also the order the LRU wants. lru_add_split_folio() inserts each sub-folio with list_add_tail(), just before @folio on the LRU, or at the tail of @list. The LRU is drained from the tail, so with the reverse walk, @folio goes first, then next to it is the first sub folio, and the pieces are reclaimed in ascending order, where the forward walk left them descending which is not the correct order.
And even if the folio is isolated, almost all split callers drain the list from the tail (lru_to_folio() is head->prev). With s1 .. s4 the four sub-folios in ascending address order and @folio holding the first pages of the range, we have:
For a folio on the LRU (list == NULL), reading head to tail:
Before (forward): [ .. s1 s2 s3 s4 folio ] reclaimed: folio s4 s3 s2 s1 After (reverse): [ .. s4 s3 s2 s1 folio ] reclaimed: folio s1 s2 s3 s4
For a folio the caller isolated off the LRU, reading head to tail:
Before (forward): [ .. s1 s2 s3 s4 ] consumed: s4 s3 s2 s1
After (reverse): [ .. s4 s3 s2 s1 ] consumed: s1 s2 s3 s4
Migration is the one @list user that walks its list from the head; that list is only a work queue and the order carries no meaning there. We may unify that part too if needed.
The effect is not very measurable in real workloads but we can get it right while reducing stack usage, so let's do that.
This also helps avoid a for loop pitfall mentioned by Zi Yan [1]:folio_next() needs the folio it is given to stay alive until the increment, folio_prev() does not. So the cached next pointer and its comment can go.
The anon helper still walks forward, and will be converted separately.
--- mm/huge_memory.c | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-)
mm/huge_memory.c +11 -12
@@ -4187,7 +4187,7 @@ static int __folio_freeze_split_file(struct folio *folio,XA_STATE (xas, &mapping->i_pages, folio->index);struct folio *end_folio =folio_next (folio);struct mem_cgroup *memcg, *old_memcg; -struct folio *new_folio, *next; +struct folio *new_folio;int nr_shmem_dropped =0 ;unsigned int min_order;struct lruvec *lruvec;
@@ -4273,20 +4273,19 @@ static int __folio_freeze_split_file(struct folio *folio, ret =__split_frozen_folio (folio, new_order, split_at, &xas, split_type);/* -* Unfreeze after-split folios and put them back to the right -* list. @folio should be kept frozen until page cache -* entries are updated with all the other after-split folios -* to prevent others seeing stale page cache entries. -* As a result, new_folio starts from the next folio of -* @folio. +* Unfreeze the after-split folios and put them back on the right +* list, walking the range in reverse so @folio is unfrozen last. +* It stays frozen until then, so a parallel lookup cannot +* folio_try_get() @folio and see a stale page cache entry. +* +* Reverse is also the reclaim order: sub-folios are inserted right +* before @folio, or at the end of @list, and the LRU is drained +* from the tail, so the last one added goes first. */ -for (new_folio =folio_next (folio); new_folio != end_folio; - new_folio = next) { +for (new_folio =folio_prev (end_folio); new_folio != folio; + new_folio =folio_prev (new_folio)) {unsigned long nr_pages =folio_nr_pages (new_folio); -/* compute next before the folio can be freed below */ - next =folio_next (new_folio); -folio_ref_unfreeze (new_folio, nr_pages +1 );lru_add_split_folio (folio, new_folio, lruvec, list);
Signature · 1 line
2.56.1
RFC PATCH 5/8 mm/huge_memory: publish split folios by walking backwards
In reply to Kairui Song__xa_store() builds a fresh XA_STATE per after-split folio, so a uniform order-9 to order-0 split does 511 root walks, which is slow and on the hot path for file folio splitting.
So instead, just use one XA_STATE iterator, and step back folio by folio using the new xas_prev_canonical() helper: each iteration is much cheaper, and publishing costs only one full descent walk.
Also simplify the code by unifying the post-processing of past EOF folios. We may store a few more folios than necessary, but the store is much cheaper now, the truncate path is unlikely, and it makes the folio removal convention in a mapping more consistent: the folio being removed always matches the content in the mapping now.
--- mm/huge_memory.c | 35 +++++++++++++++++++---------------- 1 file changed, 19 insertions(+), 16 deletions(-)
mm/huge_memory.c +19 -16
@@ -4187,7 +4187,7 @@ static int __folio_freeze_split_file(struct folio *folio,XA_STATE (xas, &mapping->i_pages, folio->index);struct folio *end_folio =folio_next (folio);struct mem_cgroup *memcg, *old_memcg; -struct folio *new_folio; +struct folio *new_folio, *old_folio;int nr_shmem_dropped =0 ;unsigned int min_order;struct lruvec *lruvec;
@@ -4282,28 +4282,31 @@ static int __folio_freeze_split_file(struct folio *folio,* before @folio, or at the end of @list, and the LRU is drained * from the tail, so the last one added goes first. */ +xas_set_order (&xas, folio->index + old_nr_pages -1 ,0 ); + old_folio =xas_prev_canonical (&xas);for (new_folio =folio_prev (end_folio); new_folio != folio; new_folio =folio_prev (new_folio)) {unsigned long nr_pages =folio_nr_pages (new_folio);folio_ref_unfreeze (new_folio, nr_pages +1 );lru_add_split_folio (folio, new_folio, lruvec, list); - -/* Add the new folio to the page cache. */ -if (new_folio->index < end) { -__xa_store (&mapping->i_pages, new_folio->index, - new_folio,0 ); -continue ; +/* Mapping is split, but content is still old folio */ +WARN_ON_ONCE (xas_get_order (&xas) !=folio_order (new_folio)); +WARN_ON_ONCE (old_folio != folio); +/* order 0 store: siblings keep resolving to this slot */ +xas_store (&xas, new_folio); +/* Step off now as the unlink below can free the xa_node */ + old_folio =xas_prev_canonical (&xas); +if (new_folio->index >= end) { +/* Drop folio beyond EOF: ->index >= end */ +if (shmem_mapping (mapping)) + nr_shmem_dropped += nr_pages; +else if (folio_test_clear_dirty (new_folio)) +folio_account_cleaned (new_folio, +inode_to_wb (mapping->host)); +__filemap_remove_folio (new_folio,NULL ); +folio_put_refs (new_folio, nr_pages); } - -/* Drop folio beyond EOF: ->index >= end */ -if (shmem_mapping (mapping)) - nr_shmem_dropped += nr_pages; -else if (folio_test_clear_dirty (new_folio)) -folio_account_cleaned ( - new_folio,inode_to_wb (mapping->host)); -__filemap_remove_folio (new_folio,NULL ); -folio_put_refs (new_folio, nr_pages); }/*
Signature · 1 line
2.56.1
RFC PATCH 6/8 mm/huge_memory: rearrange split routine handling for better optimization
In reply to Kairui SongAnonymous folios never use a mapping or an xarray, so combine the is_anon and xas conditions into a single if/else-if chain. The compiler cannot do this on its own because the xarray split calls sit between the two conditions and the anon mTHP stat update comes after the split helpers.
./scripts/bloat-o-meter on mm/huge_memory.o (x86-64 KVM test config) shows __split_frozen_folio() shrinking from 1077 to 996 bytes.
--- mm/huge_memory.c | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-)
mm/huge_memory.c +9 -9
@@ -3929,13 +3929,17 @@ static int __split_frozen_folio(struct folio *folio, int new_order,for (split_order = start_order; split_order >= new_order; split_order--) { -int nr_new_folios =1UL << (old_order - split_order); +if (is_anon) { +int nr_new_folios =1UL << (old_order - split_order); -/* order-1 anonymous folio is not supported */ -if (is_anon && split_order ==1 ) -continue ; +/* order-1 anonymous folio is not supported */ +if (split_order ==1 ) +continue ; -if (xas) { +/* Split can't fail at this point, update counters now */ +mod_mthp_stat (old_order,MTHP_STAT_NR_ANON ,-1 ); +mod_mthp_stat (split_order,MTHP_STAT_NR_ANON , nr_new_folios); + }else if (xas) {/* * uniform split has xas_split_alloc() called before * irq is disabled to allocate enough memory, whereas
@@ -3960,10 +3964,6 @@ static int __split_frozen_folio(struct folio *folio, int new_order,pgalloc_tag_split (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); - }/* * If uniform split, the process is complete. * If non-uniform, continue splitting the folio at @split_at
Signature · 1 line
2.56.1
RFC PATCH 7/8 mm/huge_memory: walk the after-split anon folios in reverse order
In reply to Kairui SongJust like the file folio, walk the after-split range with folio_prev() so the pieces are queued in ascending reclaim order where the forward walk left them descending, and the cached "next" goes away. All of its users either expects an ascending or do not care.
--- mm/huge_memory.c | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-)
mm/huge_memory.c +9 -9
@@ -4076,7 +4076,7 @@ static int __folio_freeze_split_anon(struct folio *folio,struct folio *end_folio =folio_next (folio);struct swap_cluster_info *ci =NULL ;const int old_order =folio_order (folio); -struct folio *new_folio, *next; +struct folio *new_folio;struct anon_vma *anon_vma =NULL ;enum ttu_flags ttu_flags =0 ;struct lruvec *lruvec;
@@ -4137,15 +4137,15 @@ static int __folio_freeze_split_anon(struct folio *folio, ret =__split_frozen_folio (folio, new_order, split_at,NULL , split_type);/* -* Unfreeze the after-split folios and put them back to the right -* place. Keep the head @folio frozen until the end: sub entries -* in swap cache must be updated first, so a concurrent -* swap_cache_get_folio() cannot return the head folio for a sub -* entry (folio_try_get() will fail on the head @folio until unfreeze). +* Unfreeze the after-split folios and put them back, keeping @folio +* frozen until every sub entry in the swap cache is repointed, so +* that a racing swap_cache_get_folio() cannot folio_try_get() the +* head. Backward is also the reclaim order: the pieces queue just +* before @folio on the LRU, or at the tail of @list, both drained +* from the tail. */ -for (new_folio =folio_next (folio); new_folio != end_folio; - new_folio = next) { - next =folio_next (new_folio); +for (new_folio =folio_prev (end_folio); new_folio != folio; + new_folio =folio_prev (new_folio)) {zone_device_private_split_cb (folio, new_folio);folio_ref_unfreeze (new_folio,folio_swapcache_ref_count (new_folio) +1 );
Signature · 1 line
2.56.1
RFC PATCH 8/8 mm/huge_memory: walk the folios reversely for the release loop
In reply to Kairui SongThe release loop unlocks and puts every after-split folio except the one holding @lock_at. Walk it backwards with folio_prev() instead of forwards with folio_next().
This also helps avoid a for loop pitfall mentioned by Zi Yan [1]:folio_next() needs the folio it is given to stay alive until the increment, folio_prev() does not. So the cached next pointer can go, and the folio that was just put is never read again.
Same folios visited, same work on each, only the direction changes.
--- mm/huge_memory.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-)
mm/huge_memory.c +5 -4
@@ -4366,7 +4366,7 @@ static int __folio_split(struct folio *folio, unsigned int new_order,const bool is_anon =folio_test_anon (folio);const bool is_swapcache =folio_test_swapcache (folio);int old_order =folio_order (folio); -struct folio *new_folio, *next; +struct folio *new_folio, *lock_folio;int ret;VM_WARN_ON_ONCE_FOLIO (!folio_test_locked (folio), folio);
@@ -4399,9 +4399,10 @@ static int __folio_split(struct folio *folio, unsigned int new_order,* Unlock all after-split folios except the one containing * @lock_at page. If @folio is not split, it will be kept locked. */ -for (new_folio = folio; new_folio != end_folio; new_folio = next) { - next =folio_next (new_folio); -if (new_folio ==page_folio (lock_at)) + lock_folio =page_folio (lock_at); +for (new_folio =folio_prev (end_folio); new_folio != folio; + new_folio =folio_prev (new_folio)) { +if (new_folio == lock_folio)continue ;/*
Signature · 1 line
2.56.1
[email protected]
AI reviewRe: [PATCH RFC 8/8] mm/huge_memory: walk the folios reversely for the release loop
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [High] mm/huge_memory: __folio_split() skips unlocking and putting first sub-folio --
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [High] mm/huge_memory: __folio_split() skips unlocking and putting first sub-folio
--
commit 896336ac7b9114a702349806a3b2f69599192c04
Author: Kairui Song <[email protected]>
mm/huge_memory: walk the folios reversely for the release loop
This commit changes the after-split folio release loop in __folio_split() to iterate backwards with folio_prev() instead of forwards with folio_next().
Quoted 15 lines
mm/huge_memory.c +1
-1@@ -4366,7 +4366,7 @@ static int __folio_split(struct folio *folio, unsigned int new_order,const bool is_anon =folio_test_anon (folio);const bool is_swapcache =folio_test_swapcache (folio);int old_order =folio_order (folio); -struct folio *new_folio, *next; +struct folio *new_folio, *lock_folio;int ret;VM_WARN_ON_ONCE_FOLIO (!folio_test_locked (folio), folio);@@ -4399,9 +4399,10 @@ static int __folio_split(struct folio *folio, unsigned int new_order,* Unlock all after-split folios except the one containing
mm/huge_memory.c static int __folio_split(struct folio *folio, unsigned int new_order, · line 4406
Quoted 10 lines
mm/huge_memory.c +4
-3* @lock_at page. If @folio is not split, it will be kept locked. */ -for (new_folio = folio; new_folio != end_folio; new_folio = next) { - next =folio_next (new_folio); -if (new_folio ==page_folio (lock_at)) + lock_folio =page_folio (lock_at); +for (new_folio =folio_prev (end_folio); new_folio != folio; + new_folio =folio_prev (new_folio)) { +if (new_folio == lock_folio)continue ;
[Severity: High]
Does stopping the loop at new_folio != folio skip unlocking and putting @folio when @lock_at belongs to a non-head sub-folio (lock_folio != folio)?
__split_huge_page_to_list_to_order() passes its @page argument as @lock_at:
mm/huge_memory.c:__split_huge_page_to_list_to_order() {
...
return __folio_split(folio, new_order, &folio->page, page, list,
SPLIT_TYPE_UNIFORM);
}When a caller such as try_to_merge_one_page() -> split_huge_page() splits a large folio on a tail page outside the first sub-folio and then switches to page_folio(page), the first sub-folio (@folio) is never visited by this loop and remains permanently locked and referenced, leaking @folio (and its swap entry if unmapped in swapcache) and hanging any task that later waits on its folio lock.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=8
David Hildenbrand (Arm)
maintainerQuoted 5 lines · On 11/13/26 22:26, Kairui Song via B4 Relay wrote:
Hi all,
This series slghtly cleans up the large folio split path and reduces its overhead: uniformly splitting PMD sized file folios to order 0 is over 20% faster.
I expect (well, and I have) some fixes in that area, so we better hold this off for the next week.
Signature · 3 lines
Cheers, David
Quoted 10 lines · David Hildenbrand (Arm)
Quoted 5 lines · On 11/13/26 22:26, Kairui Song via B4 Relay wrote:
Hi all,
This series slghtly cleans up the large folio split path and reduces its overhead: uniformly splitting PMD sized file folios to order 0 is over 20% faster.
I expect (well, and I have) some fixes in that area, so we better hold this off for the next week.
Yeah, that is totally understandable. I mainly wanted to gather feedback on the two xarray / folio helpers at the moment, in case anyone has concerns about that approach. I can also help review or test the upcoming fixes.