mm: avoid large folio splits when swap is unavailable
Subsystems (8)
v7 cover mm: avoid large folio splits when swap is unavailable
This is v7 of Barry's original RFC patch, "mm: Avoiding split large folios if swap has no space":
https://lore.kernel.org/r/[email protected]
Barry's RFC showed the no-swap case with MADV_PAGEOUT on 16KB mTHP: the large-folio split counter increased by 1024 even though no swapout progress was possible. Skipping the split in that case kept the counter at 0.
This series makes folio_alloc_swap() classify failures according to whether splitting a large folio might allow swapout to make progress. Callers can then avoid destroying the large folio when neither global swap availability nor the folio's memcg swap hierarchy has capacity for even a smaller folio.
Patch #1 adds page_counter_margin(), a small helper that computes the minimum remaining chargeable space across a page_counter hierarchy.
Patch #2 establishes the folio_alloc_swap() return-value contract:
- -E2BIG: splitting may let smaller folios make progress
- -ENOSPC: no global swap space is available
- -ENOMEM: splitting is not expected to help, including when the folio's memcg swap hierarchy has no remaining capacity
Patch #3 makes vmscan split a large folio only when folio_alloc_swap() returns -E2BIG. Other failures keep the existing activation path and avoid destroying the large folio when no smaller part can be backed by swap either.
Patch #4 applies the same contract to shmem_writeout(), which currently splits a large folio on every folio_alloc_swap() failure. It now enters the split fallback only on -E2BIG; other failures redirty and reactivate the folio as before.
Testing:
With a 1GB anonymous mapping backed by 16KB mTHPs and memory.swap.max=0, the patch reduced the median latency of 30 process_madvise(MADV_PAGEOUT) runs from 743.8 ms to 181.7 ms, while the number of large-folio splits per run dropped from 65536 to 0. Neither kernel swapped out any pages.
I also ran DaCapo h2 under swap pressure and found no statistically significant change in wall time or CPU time. The overall benefit appears minor and workload-dependent.
v6 -> v7:
- Add test results and clarify that the overall benefit is minor. No code changes.
- https://lore.kernel.org/r/[email protected]
RFC v5 -> v6:
- Simplify Patch #2's failure handling following Kairui Song's review, avoiding additional plumbing through the memcg charge path.
- https://lore.kernel.org/r/[email protected]
RFC v4 -> RFC v5:
- Fix Patch #3 to jump to activate_locked, not activate_locked_split, when folio_alloc_swap() fails with an error other than -E2BIG. The folio has not been split in that case, so activate_locked_split would incorrectly adjust nr_scanned and nr_pages as if tail pages had been split out.
- https://lore.kernel.org/r/[email protected]
RFC v3 -> RFC v4:
- Keep global swap availability and memcg swap margin separate, following feedback from Barry Song, Johannes Weiner, and Youngjun Park.
- Handle early rejections and memcg charge failures with the refined folio_alloc_swap() return-value contract.
- Apply the requested vmscan and shmem condition layout changes.
- https://lore.kernel.org/r/[email protected]
RFC v2 -> RFC v3:
- Use Johannes Weiner's original page_counter_margin() patch and preserve his authorship. Move the mem_cgroup_get_nr_swap_pages() conversion into Patch #1 so the helper addition remains a pure refactoring.
- Add Patch #4 to make shmem_writeout() split large folios only on -E2BIG, as suggested by Baolin Wang.
- https://lore.kernel.org/r/[email protected]
RFC v1 -> RFC v2:
- Split the RFC into helper, swap allocation, and vmscan patches.
- Add page_counter_margin() and use it for hierarchical memcg swap capacity checks.
- Make folio_alloc_swap() return -E2BIG only when a smaller folio may still be swapped out.
- Return -ENOSPC for no global swap space and -ENOMEM when splitting is not expected to help, including memcg swap exhaustion.
- Make vmscan split large folios only on -E2BIG from folio_alloc_swap().
Barry Song (Xiaomi) (1):
mm/vmscan: avoid pointless large folio splits without swap
Johannes Weiner (1):
mm: add page_counter_margin()
Xueyuan Chen (2):
mm: distinguish large folio swap allocation failures mm/shmem: split large folios only on -E2BIG
include/linux/page_counter.h | 1 + include/linux/swap.h | 6 ++++++ mm/memcontrol.c | 32 ++++++++++++++++++++++++++------ mm/page_counter.c | 20 ++++++++++++++++++++ mm/shmem.c | 7 ++++--- mm/swapfile.c | 26 +++++++++++++++++++------- mm/vmscan.c | 7 ++++++- 7 files changed, 82 insertions(+), 17 deletions(-)
base-commit: c73b725a57f276a3702ca213bde78fca029bc619
Signature · 1 line
2.47.3
PATCH v7 1/4 mm: add page_counter_margin()
From: Johannes Weiner <[email protected]>
mem_cgroup_get_nr_swap_pages() open-codes the remaining capacity across the memcg swap counter hierarchy.
Add page_counter_margin() to return the minimum usable space from a page counter to the root, and use it in mem_cgroup_get_nr_swap_pages(). This is a pure refactoring with no intended behavior change.
--- include/linux/page_counter.h | 1 + mm/memcontrol.c | 9 +++------ mm/page_counter.c | 20 ++++++++++++++++++++ 3 files changed, 24 insertions(+), 6 deletions(-)
include/linux/page_counter.h +1 -0
@@ -68,6 +68,7 @@ static inline unsigned long page_counter_read(struct page_counter *counter)return atomic_long_read (&counter->usage); } +long page_counter_margin (struct page_counter *counter);void page_counter_cancel (struct page_counter *counter,unsigned long nr_pages);void page_counter_charge (struct page_counter *counter,unsigned long nr_pages); bool page_counter_try_charge(struct page_counter *counter,
mm/memcontrol.c +3 -6
@@ -5670,12 +5670,9 @@ long mem_cgroup_get_nr_swap_pages(struct mem_cgroup *memcg) {long nr_swap_pages =get_nr_swap_pages (); -if (mem_cgroup_disabled () ||do_memsw_account ()) -return nr_swap_pages; -for (; !mem_cgroup_is_root (memcg); memcg =parent_mem_cgroup (memcg)) - nr_swap_pages =min_t (long, nr_swap_pages, -READ_ONCE (memcg->swap.max) - -page_counter_read (&memcg->swap)); +if (!mem_cgroup_disabled () && !do_memsw_account ()) + nr_swap_pages =min (nr_swap_pages,page_counter_margin (&memcg->swap)); +return nr_swap_pages; }
mm/page_counter.c +20 -0
@@ -46,6 +46,26 @@ static void propagate_protected_usage(struct page_counter *c, } } +/** +* page_counter_margin - remaining usable space within hierarchical limits +* @counter: counter +* +* Return: The minimum value of max minus usage across @counter and all of +* its ancestors. The value may be negative during a concurrent charge. +*/ +long page_counter_margin (struct page_counter *counter) +{ +long margin =PAGE_COUNTER_MAX ; + +do { +long m =READ_ONCE (counter->max) -page_counter_read (counter); + + margin =min (margin, m); + }while ((counter = counter->parent)); + +return margin; +} +/** * page_counter_cancel - take pages out of the local counter * @counter: counter
base-commit: c73b725a57f276a3702ca213bde78fca029bc619
Signature · 1 line
2.47.3
Re: [PATCH v7 1/4] mm: add page_counter_margin()
Quoted 6 lines · Xueyuan Chen
From: Johannes Weiner <[email protected]>
mem_cgroup_get_nr_swap_pages()open-codes the remaining capacity across the memcg swap counter hierarchy.
Quoted 8 lines
Add
page_counter_margin()to return the minimum usable space from a page counter to the root, and use it inmem_cgroup_get_nr_swap_pages(). This is a pure refactoring with no intended behavior change.Signed-off-by Johannes Weiner <[email protected]>Signed-off-by Xueyuan Chen <[email protected]>Reviewed-by David Hildenbrand (Arm) <[email protected]>---
Thanks for working over the weekend. I've been waiting for this resending for a while. :-). LGTM,
PATCH v7 2/4 mm: distinguish large folio swap allocation failures
In reply to Xueyuan Chenfolio_alloc_swap() reports most failures with generic negative error codes. Reclaim callers consequently cannot tell whether splitting a large folio could make progress, or whether no swap space is available for even a single page.
Classify failures using both the global free swap count and the remaining capacity in the folio's memcg swap hierarchy. Return -ENOSPC when global swap space is exhausted, -ENOMEM when splitting cannot overcome the failure, and -E2BIG for a large folio when allocating or charging a smaller folio might still succeed.
Use this classification for all folio_alloc_swap() failure paths, including capability rejection, swap slot allocation failure, and memcg swap charge failure. Callers are updated separately to split large folios only on -E2BIG.
--- include/linux/swap.h | 6 ++++++ mm/memcontrol.c | 23 +++++++++++++++++++++++ mm/swapfile.c | 26 +++++++++++++++++++------- 3 files changed, 48 insertions(+), 7 deletions(-)
include/linux/swap.h +6 -0
@@ -525,6 +525,7 @@ static inline void mem_cgroup_uncharge_swap(unsigned short id, unsigned int nr_p __mem_cgroup_uncharge_swap(id, nr_pages); } +long mem_cgroup_get_folio_swap_margin (struct folio *folio);extern long mem_cgroup_get_nr_swap_pages (struct mem_cgroup *memcg);extern bool mem_cgroup_swap_full (struct folio *folio);#else
@@ -538,6 +539,11 @@ static inline void mem_cgroup_uncharge_swap(unsigned short id, { } +static inline long mem_cgroup_get_folio_swap_margin (struct folio *folio) +{ +return PAGE_COUNTER_MAX ; +} +static inline long mem_cgroup_get_nr_swap_pages (struct mem_cgroup *memcg) {return get_nr_swap_pages ();
mm/memcontrol.c +23 -0
@@ -5676,6 +5676,29 @@ long mem_cgroup_get_nr_swap_pages(struct mem_cgroup *memcg)return nr_swap_pages; } +/** +* mem_cgroup_get_folio_swap_margin - get a folio's memcg swap margin +* @folio: folio whose memcg margin is queried +* +* Return: Remaining chargeable pages in the folio's memcg hierarchy. +*/ +long mem_cgroup_get_folio_swap_margin (struct folio *folio) +{ +struct mem_cgroup *memcg; +long margin; + +if (mem_cgroup_disabled () ||do_memsw_account () || + !folio_memcg_charged (folio)) +return PAGE_COUNTER_MAX ; + +rcu_read_lock (); + memcg =folio_memcg (folio); + margin =page_counter_margin (&memcg->swap); +rcu_read_unlock (); + +return margin; +} +bool mem_cgroup_swap_full (struct folio *folio) {struct mem_cgroup *memcg;
mm/swapfile.c +19 -7
@@ -1735,7 +1735,9 @@ static int swap_dup_entries_cluster(struct swap_info_struct *si,* swap cache. * * Context: Caller needs to hold the folio lock. -* Return: Whether the folio was added to the swap cache. +* Return: %0 on success, %-E2BIG if splitting the folio might allow swapout, +* %-ENOSPC if no global swap space is available, or %-ENOMEM if splitting +* would not help. */ int folio_alloc_swap (struct folio *folio) {
@@ -1747,11 +1749,11 @@ int folio_alloc_swap(struct folio *folio)if (order) {/* -* Reject large allocation when THP_SWAP is disabled, -* the caller should split the folio and try again. +* Reject large allocation when THP_SWAP is disabled. Check below +* whether splitting and retrying can make progress. */ if (!IS_ENABLED (CONFIG_THP_SWAP )) -return -EAGAIN ; + goto failed;/* * Allocation size should never exceed cluster size
@@ -1759,7 +1761,7 @@ int folio_alloc_swap(struct folio *folio)*/ if (size >SWAPFILE_CLUSTER ) {VM_WARN_ON_ONCE (1 ); -return -EINVAL ; + goto failed; } }
@@ -1775,13 +1777,23 @@ int folio_alloc_swap(struct folio *folio) }/* Need to call this even if allocation failed, for MEMCG_SWAP_FAIL. */ -if (unlikely (mem_cgroup_try_charge_swap (folio))) +if (unlikely (mem_cgroup_try_charge_swap (folio))) {swap_cache_del_folio (folio); + goto failed; + }if (unlikely (!folio_test_swapcache (folio))) -return -ENOMEM ; + goto failed;return 0 ; + +failed: +if (get_nr_swap_pages () <=0 ) +return -ENOSPC ; +if (mem_cgroup_get_folio_swap_margin (folio) <=0 ) +return -ENOMEM ; + +return order ? -E2BIG : -ENOMEM ; }/**
Signature · 1 line
2.47.3
PATCH v7 3/4 mm/vmscan: avoid pointless large folio splits without swap
In reply to Xueyuan ChenFrom: "Barry Song (Xiaomi)" <[email protected]>
When swap is disabled, exhausted, or unavailable due to memcg swap limits, splitting a large anonymous folio cannot make swapout progress. The fallback only destroys the large folio and inflates split statistics.
Use -E2BIG from folio_alloc_swap() as the explicit signal that splitting the folio might allow swapout of smaller pieces. For other allocation failures, keep the existing activation path and avoid the split.
This preserves the split fallback for fragmented or partially available swap, while avoiding it when there is no backing space for any part of the folio.
--- mm/vmscan.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-)
mm/vmscan.c +6 -1
@@ -1263,6 +1263,8 @@ static unsigned int shrink_folio_list(struct list_head *folio_list,*/ if (folio_test_anon (folio) &&folio_test_swapbacked (folio) && !folio_test_swapcache (folio)) { +int ret; +if (!(sc->gfp_mask & __GFP_IO)) goto keep_locked;if (folio_maybe_dma_pinned (folio))
@@ -1281,11 +1283,14 @@ static unsigned int shrink_folio_list(struct list_head *folio_list, split_folio_to_list(folio, folio_list)) goto activate_locked; } -if (folio_alloc_swap (folio)) { + ret =folio_alloc_swap (folio); +if (ret) {int __maybe_unused order =folio_order (folio);if (!folio_test_large (folio)) goto activate_locked_split; +if (ret != -E2BIG ) + goto activate_locked;/* Fallback to swap normal pages */ if (split_folio_to_list (folio, folio_list)) goto activate_locked;
Signature · 1 line
2.47.3
PATCH v7 4/4 mm/shmem: split large folios only on -E2BIG
In reply to Xueyuan Chenshmem_writeout() currently splits a large folio on every folio_alloc_swap() failure. With the refined return-value contract, only -E2BIG indicates that splitting might allow smaller folios to be swapped out.
Enter the split fallback only for -E2BIG. For -ENOSPC and -ENOMEM, redirty and reactivate the folio as before.
--- mm/shmem.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-)
mm/shmem.c +4 -3
@@ -1608,7 +1608,7 @@ int shmem_writeout(struct swap_io_ctx *ctx, struct folio *folio,struct shmem_inode_info *info =SHMEM_I (inode);struct shmem_sb_info *sbinfo =SHMEM_SB (inode->i_sb);pgoff_t index; -int nr_pages; +int nr_pages, ret;bool split = false;if ((info->flags &SHMEM_F_LOCKED ) || sbinfo->noswap)
@@ -1689,7 +1689,8 @@ int shmem_writeout(struct swap_io_ctx *ctx, struct folio *folio,folio_mark_uptodate (folio); } -if (!folio_alloc_swap (folio)) { + ret =folio_alloc_swap (folio); +if (!ret) {bool first_swapped =shmem_recalc_inode (inode,0 , nr_pages);int error;
@@ -1742,7 +1743,7 @@ int shmem_writeout(struct swap_io_ctx *ctx, struct folio *folio,swap_cache_del_folio (folio); goto redirty; } -if (nr_pages >1 ) +if (nr_pages >1 && ret == -E2BIG ) goto try_split; redirty:folio_mark_dirty (folio);
Signature · 1 line
2.47.3
Andrew Morton
maintainerQuoted 10 lines · On Sun, 30 Aug 2026 12:29:16 +0800 Xueyuan Chen <[email protected]>…
This is v7 of Barry's original RFC patch, "mm: Avoiding split large folios if swap has no space":
https://lore.kernel.org/r/[email protected]
Barry's RFC showed the no-swap case with
MADV_PAGEOUTon 16KB mTHP: the large-folio split counter increased by 1024 even though no swapout progress was possible. Skipping the split in that case kept the counter at 0.
Quoted 5 lines
This series makes
folio_alloc_swap()classify failures according to whether splitting a large folio might allow swapout to make progress. Callers can then avoid destroying the large folio when neither global swap availability nor the folio's memcg swap hierarchy has capacity for even a smaller folio.
Thanks. Sashiko had a question which didn't occur against v6:
https://sashiko.dev/#/patchset/[email protected]
Quoted 19 lines · Andrew Morton
Quoted 10 lines · On Sun, 30 Aug 2026 12:29:16 +0800 Xueyuan Chen <[email protected]>…
This is v7 of Barry's original RFC patch, "mm: Avoiding split large folios if swap has no space":
https://lore.kernel.org/r/[email protected]
Barry's RFC showed the no-swap case with
MADV_PAGEOUTon 16KB mTHP: the large-folio split counter increased by 1024 even though no swapout progress was possible. Skipping the split in that case kept the counter at 0.Quoted 5 lines
This series makes
folio_alloc_swap()classify failures according to whether splitting a large folio might allow swapout to make progress. Callers can then avoid destroying the large folio when neither global swap availability nor the folio's memcg swap hierarchy has capacity for even a smaller folio.
Quoted 4 lines
Thanks. Sashiko had a question which didn't occur against v6:
Hi Andrew,
I think the Sashiko link points to a different series:
[PATCH 0/8] mm: remove page_swap_entry()
The Sashiko page for this v7 reports "No issues found":
https://sashiko.dev/#/patchset/[email protected]
Did you intend to link a different question?
Thanks,
Xueyuan