mm: avoid large folio splits when swap is unavailable
Subsystems (8)
v5 cover mm: avoid large folio splits when swap is unavailable
This is RFC v5 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 version keeps that behavior, but makes folio_alloc_swap() classify the failure. The function sees both the swap allocation result and the memcg swap charge result, so callers only split when a smaller folio might still be swapped out.
Patch #1 adds page_counter_margin(), a small helper that computes the minimum remaining chargeable space across a page_counter hierarchy.
Patch #2 lets folio_alloc_swap() distinguish large-folio swap allocation failures:
- -E2BIG: splitting may let smaller folios make progress
- -ENOSPC: no global swap space is available
- -ENOMEM: splitting is not expected to help, including memcg swap charge failures with no remaining swap 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 previously split 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.
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 | 16 ++++++++++---- mm/memcontrol.c | 41 ++++++++++++++++++++++++++++++------ mm/page_counter.c | 20 ++++++++++++++++++ mm/shmem.c | 7 +++--- mm/swapfile.c | 32 +++++++++++++++++++++------- mm/vmscan.c | 7 +++++- 7 files changed, 101 insertions(+), 23 deletions(-)
base-commit: c73b725a57f276a3702ca213bde78fca029bc619
Signature · 1 line
2.47.3
RFC PATCH v5 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
Signature · 1 line
2.47.3
Re: [RFC PATCH v5 1/4] mm: add page_counter_margin()
Quoted 5 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 7 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]>---
Signature · 3 lines
Cheers, David
RFC PATCH v5 2/4 mm: distinguish large folio swap allocation failures
In reply to Xueyuan Chenfolio_alloc_swap() reports most allocation failures with a generic negative error code. Reclaim cannot tell whether splitting a large folio could make progress or whether there is no backing space at all.
Keep the global free swap count and the remaining hierarchical memcg swap margin as separate inputs. The memcg charge path reports only its own margin; folio_alloc_swap() combines the two layers when classifying an allocation failure.
Return -E2BIG for large folios when a smaller allocation might still fit, -ENOSPC when no global swap space is available, and -ENOMEM when the failure is not helped by splitting.
For early large-folio rejections, check global and memcg swap availability instead of returning -E2BIG unconditionally. On a memcg charge failure, swap slot allocation has already succeeded, so use the remaining memcg margin to decide whether a smaller charge might fit.
This only refines folio_alloc_swap() return codes. The reclaim callers are updated separately.
--- include/linux/swap.h | 16 ++++++++++++---- mm/memcontrol.c | 32 +++++++++++++++++++++++++++++++- mm/swapfile.c | 32 ++++++++++++++++++++++++-------- 3 files changed, 67 insertions(+), 13 deletions(-)
include/linux/swap.h +12 -4
@@ -509,12 +509,13 @@ static inline void folio_throttle_swaprate(struct folio *folio, gfp_t gfp)#endif #if defined(CONFIG_MEMCG ) && defined(CONFIG_SWAP ) -int __mem_cgroup_try_charge_swap (struct folio *folio); -static inline int mem_cgroup_try_charge_swap (struct folio *folio) +int __mem_cgroup_try_charge_swap (struct folio *folio,long *swap_margin); +static inline int mem_cgroup_try_charge_swap (struct folio *folio, +long *swap_margin) {if (mem_cgroup_disabled ())return 0 ; -return __mem_cgroup_try_charge_swap (folio); +return __mem_cgroup_try_charge_swap (folio, swap_margin); }extern void __mem_cgroup_uncharge_swap (unsigned short id,unsigned int nr_pages);
@@ -525,10 +526,12 @@ 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 -static inline int mem_cgroup_try_charge_swap (struct folio *folio) +static inline int mem_cgroup_try_charge_swap (struct folio *folio, +long *swap_margin) {return 0 ; }
@@ -538,6 +541,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 +31 -1
@@ -5592,12 +5592,13 @@ int __init mem_cgroup_init(void)/** * __mem_cgroup_try_charge_swap - try charging swap space for a folio * @folio: folio being added to swap +* @swap_margin: remaining memcg swap margin if allocation or charge fails * * Try to charge @folio's memcg for the swap space at folio->swap. * * Returns 0 on success, -ENOMEM on failure. */ -int __mem_cgroup_try_charge_swap (struct folio *folio) +int __mem_cgroup_try_charge_swap (struct folio *folio,long *swap_margin) {unsigned int nr_pages =folio_nr_pages (folio);struct swap_cluster_info *ci;
@@ -5616,6 +5617,7 @@ int __mem_cgroup_try_charge_swap(struct folio *folio)rcu_read_lock (); memcg =obj_cgroup_memcg (objcg);if (!folio_test_swapcache (folio)) { + *swap_margin =page_counter_margin (&memcg->swap);memcg_memory_event (memcg,MEMCG_SWAP_FAIL );rcu_read_unlock ();return 0 ;
@@ -5629,6 +5631,7 @@ int __mem_cgroup_try_charge_swap(struct folio *folio) !page_counter_try_charge(&memcg->swap, nr_pages, &counter)) {memcg_memory_event (memcg,MEMCG_SWAP_MAX );memcg_memory_event (memcg,MEMCG_SWAP_FAIL ); + *swap_margin =page_counter_margin (counter);mem_cgroup_private_id_put (memcg, nr_pages);return -ENOMEM ; }
@@ -5676,6 +5679,33 @@ 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) +{ +long swap_margin =PAGE_COUNTER_MAX ; +struct mem_cgroup *memcg; +struct obj_cgroup *objcg; + +if (mem_cgroup_disabled () ||do_memsw_account ()) +return swap_margin; + + objcg =folio_objcg (folio); +if (!objcg) +return swap_margin; + +rcu_read_lock (); + memcg =obj_cgroup_memcg (objcg); + swap_margin =page_counter_margin (&memcg->swap); +rcu_read_unlock (); + +return swap_margin; +} +bool mem_cgroup_swap_full (struct folio *folio) {struct mem_cgroup *memcg;
mm/swapfile.c +24 -8
@@ -1735,23 +1735,28 @@ 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) {unsigned int order =folio_order (folio);unsigned int size =1 << order; +long swap_margin =PAGE_COUNTER_MAX ;VM_BUG_ON_FOLIO (!folio_test_locked (folio), folio);VM_BUG_ON_FOLIO (!folio_test_uptodate (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 ; +if (!IS_ENABLED (CONFIG_THP_SWAP )) { + swap_margin =mem_cgroup_get_folio_swap_margin (folio); + goto failed; + }/* * Allocation size should never exceed cluster size
@@ -1759,7 +1764,8 @@ int folio_alloc_swap(struct folio *folio)*/ if (size >SWAPFILE_CLUSTER ) {VM_WARN_ON_ONCE (1 ); -return -EINVAL ; + swap_margin =mem_cgroup_get_folio_swap_margin (folio); + goto failed; } }
@@ -1775,13 +1781,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_margin))) {swap_cache_del_folio (folio); +return order && swap_margin >0 ? -E2BIG : -ENOMEM ; + }if (unlikely (!folio_test_swapcache (folio))) -return -ENOMEM ; + goto failed;return 0 ; + +failed: +if (get_nr_swap_pages () <=0 ) +return -ENOSPC ; +if (swap_margin <=0 ) +return -ENOMEM ; + +return order ? -E2BIG : -ENOMEM ; }/**
Signature · 1 line
2.47.3
Re: [RFC PATCH v5 2/4] mm: distinguish large folio swap allocation failures
Quoted 35 lines · Xueyuan Chen
folio_alloc_swap()reports most allocation failures with a generic negative error code. Reclaim cannot tell whether splitting a large folio could make progress or whether there is no backing space at all.Keep the global free swap count and the remaining hierarchical memcg swap margin as separate inputs. The memcg charge path reports only its own margin;
folio_alloc_swap()combines the two layers when classifying an allocation failure.Return
-E2BIGfor large folios when a smaller allocation might still fit,-ENOSPCwhen no global swap space is available, and-ENOMEMwhen the failure is not helped by splitting.For early large-folio rejections, check global and memcg swap availability instead of returning
-E2BIGunconditionally. On a memcg charge failure, swap slot allocation has already succeeded, so use the remaining memcg margin to decide whether a smaller charge might fit.This only refines
folio_alloc_swap()return codes. The reclaim callers are updated separately.Suggested-by Barry Song <[email protected]>Suggested-by Youngjun Park <[email protected]>Signed-off-by Xueyuan Chen <[email protected]>--- include/linux/swap.h | 16 ++++++++++++---- mm/memcontrol.c | 32 +++++++++++++++++++++++++++++++- mm/swapfile.c | 32 ++++++++++++++++++++++++-------- 3 files changed, 67 insertions(+), 13 deletions(-)include/linux/swap.h +0
-0
include/linux/swap.h static inline void folio_throttle_swaprate(struct folio *folio, gfp_t gfp) · line 514
Quoted 8 lines
include/linux/swap.h +3
-2@@ -509,12 +509,13 @@ static inline void folio_throttle_swaprate(struct folio *folio, gfp_t gfp)#endif #if defined(CONFIG_MEMCG ) && defined(CONFIG_SWAP ) -int __mem_cgroup_try_charge_swap (struct folio *folio); -static inline int mem_cgroup_try_charge_swap (struct folio *folio) +int __mem_cgroup_try_charge_swap (struct folio *folio,long *swap_margin); +static inline int mem_cgroup_try_charge_swap (struct folio *folio, +long *swap_margin)
Nit: two tab indent
[...]
include/linux/swap.h static inline void folio_throttle_swaprate(struct folio *folio, gfp_t gfp) · line 514
Quoted 3 lines
include/linux/swap.h +2
-1-static inline int mem_cgroup_try_charge_swap (struct folio *folio) +static inline int mem_cgroup_try_charge_swap (struct folio *folio, +long *swap_margin)
Nothing else jumped at me
Signature · 3 lines
Cheers, David
Re: [RFC PATCH v5 2/4] mm: distinguish large folio swap allocation failures
In reply to Xueyuan ChenQuoted 21 lines · Xueyuan Chen
folio_alloc_swap()reports most allocation failures with a generic negative error code. Reclaim cannot tell whether splitting a large folio could make progress or whether there is no backing space at all.Keep the global free swap count and the remaining hierarchical memcg swap margin as separate inputs. The memcg charge path reports only its own margin;
folio_alloc_swap()combines the two layers when classifying an allocation failure.Return
-E2BIGfor large folios when a smaller allocation might still fit,-ENOSPCwhen no global swap space is available, and-ENOMEMwhen the failure is not helped by splitting.For early large-folio rejections, check global and memcg swap availability instead of returning
-E2BIGunconditionally. On a memcg charge failure, swap slot allocation has already succeeded, so use the remaining memcg margin to decide whether a smaller charge might fit.This only refines
folio_alloc_swap()return codes. The reclaim callers are updated separately.
Quoted 9 lines
Suggested-by Barry Song <[email protected]>Suggested-by Youngjun Park <[email protected]>Signed-off-by Xueyuan Chen <[email protected]>--- include/linux/swap.h | 16 ++++++++++++---- mm/memcontrol.c | 32 +++++++++++++++++++++++++++++++- mm/swapfile.c | 32 ++++++++++++++++++++++++-------- 3 files changed, 67 insertions(+), 13 deletions(-)
Hello Xueyuan,
Thanks for the patch!
include/linux/swap.h static inline void folio_throttle_swaprate(struct folio *folio, gfp_t gfp) · line 514
Quoted 13 lines
include/linux/swap.h +3
-2@@ -509,12 +509,13 @@ static inline void folio_throttle_swaprate(struct folio *folio, gfp_t gfp)#endif #if defined(CONFIG_MEMCG ) && defined(CONFIG_SWAP ) -int __mem_cgroup_try_charge_swap (struct folio *folio); -static inline int mem_cgroup_try_charge_swap (struct folio *folio) +int __mem_cgroup_try_charge_swap (struct folio *folio,long *swap_margin); +static inline int mem_cgroup_try_charge_swap (struct folio *folio, +long *swap_margin)
Am I the only one that feel this returning argument is a bit ugly? See below..
Quoted 17 lines
include/linux/swap.h +17
-0+/** +* 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) +{ +long swap_margin =PAGE_COUNTER_MAX ; +struct mem_cgroup *memcg; +struct obj_cgroup *objcg; + +if (mem_cgroup_disabled () ||do_memsw_account ()) +return swap_margin; + + objcg =folio_objcg (folio); +if (!objcg)
include/linux/swap.h static inline void folio_throttle_swaprate(struct folio *folio, gfp_t gfp)
Quoted 10 lines
include/linux/swap.h +10
-0+return swap_margin; + +rcu_read_lock (); + memcg =obj_cgroup_memcg (objcg); + swap_margin =page_counter_margin (&memcg->swap); +rcu_read_unlock (); + +return swap_margin; +} +
Will is be good if we just always check the margin use this helper on alloc failure? Alloc failure should be a rather cold path I think?
Quoted 65 lines
bool
mem_cgroup_swap_full(struct folio *folio)
{include/linux/swap.h +0
-0struct mem_cgroup *memcg;mm/swapfile.c +16
-8@@ -1735,23 +1735,28 @@ 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) {unsigned int order =folio_order (folio);unsigned int size =1 << order; +long swap_margin =PAGE_COUNTER_MAX ;VM_BUG_ON_FOLIO (!folio_test_locked (folio), folio);VM_BUG_ON_FOLIO (!folio_test_uptodate (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 ; +if (!IS_ENABLED (CONFIG_THP_SWAP )) { + swap_margin =mem_cgroup_get_folio_swap_margin (folio); + goto failed; + }/* * Allocation size should never exceed cluster size @@ -1759,7 +1764,8 @@ int folio_alloc_swap(struct folio *folio)*/ if (size >SWAPFILE_CLUSTER ) {VM_WARN_ON_ONCE (1 ); -return -EINVAL ; + swap_margin =mem_cgroup_get_folio_swap_margin (folio); + goto failed; } }@@ -1775,13 +1781,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_margin))) {swap_cache_del_folio (folio); +return order && swap_margin >0 ? -E2BIG : -ENOMEM ; + }if (unlikely (!folio_test_swapcache (folio))) -return -ENOMEM ; + goto failed;
mm/swapfile.c int folio_alloc_swap(struct folio *folio) · line 1801
Quoted 10 lines
mm/swapfile.c +8
-0return 0 ; + +failed: +if (get_nr_swap_pages () <=0 ) +return -ENOSPC ; +if (swap_margin <=0 ) +return -ENOMEM ; + +return order ? -E2BIG : -ENOMEM ; }
How do you think if we apply this on top of this? (Not tested) Should be no behavior change but outside the existing races, the margin read moves from charge time to failure classification time, a small TOCTOU, which the original also has but in a different way.
include/linux/swap.h +4 -6
@@ -490,13 +490,12 @@ static inline void folio_throttle_swaprate(struct folio *folio, gfp_t gfp)#endif #if defined(CONFIG_MEMCG ) && defined(CONFIG_SWAP ) -int __mem_cgroup_try_charge_swap (struct folio *folio,long *swap_margin); -static inline int mem_cgroup_try_charge_swap (struct folio *folio, -long *swap_margin) +int __mem_cgroup_try_charge_swap (struct folio *folio); +static inline int mem_cgroup_try_charge_swap (struct folio *folio) {if (mem_cgroup_disabled ())return 0 ; -return __mem_cgroup_try_charge_swap (folio, swap_margin); +return __mem_cgroup_try_charge_swap (folio); }extern void __mem_cgroup_uncharge_swap (unsigned short id,unsigned int nr_pages);
@@ -511,8 +510,7 @@ 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 -static inline int mem_cgroup_try_charge_swap (struct folio *folio, -long *swap_margin) +static inline int mem_cgroup_try_charge_swap (struct folio *folio) {return 0 ; }
mm/memcontrol.c +1 -4
@@ -5650,13 +5650,12 @@ int __init mem_cgroup_init(void)/** * __mem_cgroup_try_charge_swap - try charging swap space for a folio * @folio: folio being added to swap -* @swap_margin: remaining memcg swap margin if allocation or charge fails * * Try to charge @folio's memcg for the swap space at folio->swap. * * Returns 0 on success, -ENOMEM on failure. */ -int __mem_cgroup_try_charge_swap (struct folio *folio,long *swap_margin) +int __mem_cgroup_try_charge_swap (struct folio *folio) {unsigned int nr_pages =folio_nr_pages (folio);struct swap_cluster_info *ci;
@@ -5675,7 +5674,6 @@ int __mem_cgroup_try_charge_swap(struct folio *folio, long *swap_margin)rcu_read_lock (); memcg =obj_cgroup_memcg (objcg);if (!folio_test_swapcache (folio)) { - *swap_margin =page_counter_margin (&memcg->swap);memcg_memory_event (memcg,MEMCG_SWAP_FAIL );rcu_read_unlock ();return 0 ;
@@ -5689,7 +5687,6 @@ int __mem_cgroup_try_charge_swap(struct folio *folio, long *swap_margin) !page_counter_try_charge(&memcg->swap, nr_pages, &counter)) {memcg_memory_event (memcg,MEMCG_SWAP_MAX );memcg_memory_event (memcg,MEMCG_SWAP_FAIL ); - *swap_margin =page_counter_margin (counter);mem_cgroup_private_id_put (memcg, nr_pages);return -ENOMEM ; }
mm/swapfile.c +3 -9
@@ -1756,7 +1756,6 @@ int folio_alloc_swap(struct folio *folio) {unsigned int order =folio_order (folio);unsigned int size =1 << order; -long swap_margin =PAGE_COUNTER_MAX ;VM_BUG_ON_FOLIO (!folio_test_locked (folio), folio);VM_BUG_ON_FOLIO (!folio_test_uptodate (folio), folio);
@@ -1766,10 +1765,8 @@ int folio_alloc_swap(struct folio *folio)* Reject large allocation when THP_SWAP is disabled. Check below * whether splitting and retrying can make progress. */ -if (!IS_ENABLED (CONFIG_THP_SWAP )) { - swap_margin =mem_cgroup_get_folio_swap_margin (folio); +if (!IS_ENABLED (CONFIG_THP_SWAP )) goto failed; - }/* * Allocation size should never exceed cluster size
@@ -1777,7 +1774,6 @@ int folio_alloc_swap(struct folio *folio)*/ if (size >SWAPFILE_CLUSTER ) {VM_WARN_ON_ONCE (1 ); - swap_margin =mem_cgroup_get_folio_swap_margin (folio); goto failed; } }
@@ -1794,10 +1790,8 @@ 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, &swap_margin))) { +if (unlikely (mem_cgroup_try_charge_swap (folio)))swap_cache_del_folio (folio); -return order && swap_margin >0 ? -E2BIG : -ENOMEM ; - }if (unlikely (!folio_test_swapcache (folio))) goto failed;
@@ -1807,7 +1801,7 @@ int folio_alloc_swap(struct folio *folio) failed:if (get_nr_swap_pages () <=0 )return -ENOSPC ; -if (swap_margin <=0 ) +if (mem_cgroup_get_folio_swap_margin (folio) <=0 )return -ENOMEM ;return order ? -E2BIG : -ENOMEM ;
Re: [RFC PATCH v5 2/4] mm: distinguish large folio swap allocation failures
Quoted 43 lines · Kairui Song
Quoted 21 lines · Xueyuan Chen
folio_alloc_swap()reports most allocation failures with a generic negative error code. Reclaim cannot tell whether splitting a large folio could make progress or whether there is no backing space at all.Keep the global free swap count and the remaining hierarchical memcg swap margin as separate inputs. The memcg charge path reports only its own margin;
folio_alloc_swap()combines the two layers when classifying an allocation failure.Return
-E2BIGfor large folios when a smaller allocation might still fit,-ENOSPCwhen no global swap space is available, and-ENOMEMwhen the failure is not helped by splitting.For early large-folio rejections, check global and memcg swap availability instead of returning
-E2BIGunconditionally. On a memcg charge failure, swap slot allocation has already succeeded, so use the remaining memcg margin to decide whether a smaller charge might fit.This only refines
folio_alloc_swap()return codes. The reclaim callers are updated separately.Quoted 9 lines
Suggested-by Barry Song <[email protected]>Suggested-by Youngjun Park <[email protected]>Signed-off-by Xueyuan Chen <[email protected]>--- include/linux/swap.h | 16 ++++++++++++---- mm/memcontrol.c | 32 +++++++++++++++++++++++++++++++- mm/swapfile.c | 32 ++++++++++++++++++++++++-------- 3 files changed, 67 insertions(+), 13 deletions(-)Hello Xueyuan,
Thanks for the patch!
Quoted 7 lines
include/linux/swap.h +0
-0@@ -509,12 +509,13 @@ static inline void folio_throttle_swaprate(struct folio *folio, gfp_t gfp)#endif
Quoted 8 lines
include/linux/swap.h static inline void folio_throttle_swaprate(struct folio *folio, gfp_t gfp) · line 514
Quoted 6 lines
include/linux/swap.h +3
-2#if defined(CONFIG_MEMCG ) && defined(CONFIG_SWAP ) -int __mem_cgroup_try_charge_swap (struct folio *folio); -static inline int mem_cgroup_try_charge_swap (struct folio *folio) +int __mem_cgroup_try_charge_swap (struct folio *folio,long *swap_margin); +static inline int mem_cgroup_try_charge_swap (struct folio *folio, +long *swap_margin)Am I the only one that feel this returning argument is a bit ugly? See below..
No :)
Johannes Weiner
maintainerRe: [RFC PATCH v5 2/4] mm: distinguish large folio swap allocation failures
In reply to Kairui SongQuoted 4 lines · Kairui Song
Am I the only one that feel this returning argument is a bit ugly? See below..
Will is be good if we just always check the margin use this helper on alloc failure? Alloc failure should be a rather cold path I think?
Works for me!
Sorry, I may have been the main instigator of that complication.
Barry Song
reviewerRe: [RFC PATCH v5 2/4] mm: distinguish large folio swap allocation failures
In reply to Kairui SongQuoted 75 lines · Kairui Song
Quoted 21 lines · Xueyuan Chen
folio_alloc_swap()reports most allocation failures with a generic negative error code. Reclaim cannot tell whether splitting a large folio could make progress or whether there is no backing space at all.Keep the global free swap count and the remaining hierarchical memcg swap margin as separate inputs. The memcg charge path reports only its own margin;
folio_alloc_swap()combines the two layers when classifying an allocation failure.Return
-E2BIGfor large folios when a smaller allocation might still fit,-ENOSPCwhen no global swap space is available, and-ENOMEMwhen the failure is not helped by splitting.For early large-folio rejections, check global and memcg swap availability instead of returning
-E2BIGunconditionally. On a memcg charge failure, swap slot allocation has already succeeded, so use the remaining memcg margin to decide whether a smaller charge might fit.This only refines
folio_alloc_swap()return codes. The reclaim callers are updated separately.Quoted 9 lines
Suggested-by Barry Song <[email protected]>Suggested-by Youngjun Park <[email protected]>Signed-off-by Xueyuan Chen <[email protected]>--- include/linux/swap.h | 16 ++++++++++++---- mm/memcontrol.c | 32 +++++++++++++++++++++++++++++++- mm/swapfile.c | 32 ++++++++++++++++++++++++-------- 3 files changed, 67 insertions(+), 13 deletions(-)Hello Xueyuan,
Thanks for the patch!
include/linux/swap.h static inline void folio_throttle_swaprate(struct folio *folio, gfp_t gfp) · line 514
Quoted 13 lines
include/linux/swap.h +3
-2@@ -509,12 +509,13 @@ static inline void folio_throttle_swaprate(struct folio *folio, gfp_t gfp)#endif #if defined(CONFIG_MEMCG ) && defined(CONFIG_SWAP ) -int __mem_cgroup_try_charge_swap (struct folio *folio); -static inline int mem_cgroup_try_charge_swap (struct folio *folio) +int __mem_cgroup_try_charge_swap (struct folio *folio,long *swap_margin); +static inline int mem_cgroup_try_charge_swap (struct folio *folio, +long *swap_margin)Am I the only one that feel this returning argument is a bit ugly? See below..
Quoted 22 lines
include/linux/swap.h +22
-0+/** +* 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) +{ +long swap_margin =PAGE_COUNTER_MAX ; +struct mem_cgroup *memcg; +struct obj_cgroup *objcg; + +if (mem_cgroup_disabled () ||do_memsw_account ()) +return swap_margin; + + objcg =folio_objcg (folio); +if (!objcg) +return swap_margin; + +rcu_read_lock (); + memcg =obj_cgroup_memcg (objcg); + swap_margin =page_counter_margin (&memcg->swap);
Quoted 9 lines
include/linux/swap.h
Quoted 5 lines
include/linux/swap.h +5
-0+rcu_read_unlock (); + +return swap_margin; +} +Will is be good if we just always check the margin use this helper on alloc failure? Alloc failure should be a rather cold path I think?
The only disadvantage is that we don't get the margin together with the charging failure. If this happens infrequently, it may not matter.
Re: [RFC PATCH v5 2/4] mm: distinguish large folio swap allocation failures
In reply to Kairui SongQuoted 1 line · Kairui Song <[email protected]> 于2026年8月7日周五 16:29写道:
[...]
Quoted 12 lines
Hello Xueyuan,
Thanks for the patch!
Quoted 7 lines
include/linux/swap.h +0
-0@@ -509,12 +509,13 @@ static inline void folio_throttle_swaprate(struct folio *folio, gfp_t gfp)#endif
Quoted 8 lines
include/linux/swap.h static inline void folio_throttle_swaprate(struct folio *folio, gfp_t gfp) · line 514
Quoted 6 lines
include/linux/swap.h +3
-2#if defined(CONFIG_MEMCG ) && defined(CONFIG_SWAP ) -int __mem_cgroup_try_charge_swap (struct folio *folio); -static inline int mem_cgroup_try_charge_swap (struct folio *folio) +int __mem_cgroup_try_charge_swap (struct folio *folio,long *swap_margin); +static inline int mem_cgroup_try_charge_swap (struct folio *folio, +long *swap_margin)Am I the only one that feel this returning argument is a bit ugly? See below..
Hi Kairui,
Yes, it does look a bit odd :P
Quoted 97 lines
Quoted 18 lines
include/linux/swap.h +17
-0+/** +* 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) +{ +long swap_margin =PAGE_COUNTER_MAX ; +struct mem_cgroup *memcg; +struct obj_cgroup *objcg; + +if (mem_cgroup_disabled () ||do_memsw_account ()) +return swap_margin; + + objcg =folio_objcg (folio); +if (!objcg)include/linux/swap.h static inline void folio_throttle_swaprate(struct folio *folio, gfp_t gfp)
Quoted 10 lines
include/linux/swap.h +10
-0+return swap_margin; + +rcu_read_lock (); + memcg =obj_cgroup_memcg (objcg); + swap_margin =page_counter_margin (&memcg->swap); +rcu_read_unlock (); + +return swap_margin; +} +Will is be good if we just always check the margin use this helper on alloc failure? Alloc failure should be a rather cold path I think?
Quoted 65 lines
bool
mem_cgroup_swap_full(struct folio *folio){ struct mem_cgroup *memcg;mm/swapfile.c +16
-8@@ -1735,23 +1735,28 @@ 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) {unsigned int order =folio_order (folio);unsigned int size =1 << order; +long swap_margin =PAGE_COUNTER_MAX ;VM_BUG_ON_FOLIO (!folio_test_locked (folio), folio);VM_BUG_ON_FOLIO (!folio_test_uptodate (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 ; +if (!IS_ENABLED (CONFIG_THP_SWAP )) { + swap_margin =mem_cgroup_get_folio_swap_margin (folio); + goto failed; + }/* * Allocation size should never exceed cluster size @@ -1759,7 +1764,8 @@ int folio_alloc_swap(struct folio *folio)*/ if (size >SWAPFILE_CLUSTER ) {VM_WARN_ON_ONCE (1 ); -return -EINVAL ; + swap_margin =mem_cgroup_get_folio_swap_margin (folio); + goto failed; } }@@ -1775,13 +1781,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_margin))) {swap_cache_del_folio (folio); +return order && swap_margin >0 ? -E2BIG : -ENOMEM ; + }if (unlikely (!folio_test_swapcache (folio))) -return -ENOMEM ; + goto failed;
Quoted 15 lines
mm/swapfile.c int folio_alloc_swap(struct folio *folio) · line 1801
Quoted 10 lines
mm/swapfile.c +8
-0return 0 ; + +failed: +if (get_nr_swap_pages () <=0 ) +return -ENOSPC ; +if (swap_margin <=0 ) +return -ENOMEM ; + +return order ? -E2BIG : -ENOMEM ; }How do you think if we apply this on top of this? (Not tested) Should be no behavior change but outside the existing races, the margin read moves from charge time to failure classification time, a small TOCTOU, which the original also has but in a different way.
Thanks a lot for the review and suggestion. I'll simplify the code along the lines of your diff in the next version. :)
Quoted 121 lines
include/linux/swap.h +4
-6@@ -490,13 +490,12 @@ static inline void folio_throttle_swaprate(struct folio *folio, gfp_t gfp)#endif #if defined(CONFIG_MEMCG ) && defined(CONFIG_SWAP ) -int __mem_cgroup_try_charge_swap (struct folio *folio,long *swap_margin); -static inline int mem_cgroup_try_charge_swap (struct folio *folio, -long *swap_margin) +int __mem_cgroup_try_charge_swap (struct folio *folio); +static inline int mem_cgroup_try_charge_swap (struct folio *folio) {if (mem_cgroup_disabled ())return 0 ; -return __mem_cgroup_try_charge_swap (folio, swap_margin); +return __mem_cgroup_try_charge_swap (folio); }extern void __mem_cgroup_uncharge_swap (unsigned short id,unsigned int nr_pages);@@ -511,8 +510,7 @@ 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 -static inline int mem_cgroup_try_charge_swap (struct folio *folio, -long *swap_margin) +static inline int mem_cgroup_try_charge_swap (struct folio *folio) {return 0 ; }mm/memcontrol.c +1
-4@@ -5650,13 +5650,12 @@ int __init mem_cgroup_init(void)/** * __mem_cgroup_try_charge_swap - try charging swap space for a folio * @folio: folio being added to swap -* @swap_margin: remaining memcg swap margin if allocation or charge fails * * Try to charge @folio's memcg for the swap space at folio->swap. * * Returns 0 on success, -ENOMEM on failure. */ -int __mem_cgroup_try_charge_swap (struct folio *folio,long *swap_margin) +int __mem_cgroup_try_charge_swap (struct folio *folio) {unsigned int nr_pages =folio_nr_pages (folio);struct swap_cluster_info *ci;@@ -5675,7 +5674,6 @@ int __mem_cgroup_try_charge_swap(struct folio *folio, long *swap_margin)rcu_read_lock (); memcg =obj_cgroup_memcg (objcg);if (!folio_test_swapcache (folio)) { - *swap_margin =page_counter_margin (&memcg->swap);memcg_memory_event (memcg,MEMCG_SWAP_FAIL );rcu_read_unlock ();return 0 ;@@ -5689,7 +5687,6 @@ int __mem_cgroup_try_charge_swap(struct folio *folio, long *swap_margin) !page_counter_try_charge(&memcg->swap, nr_pages, &counter)) {memcg_memory_event (memcg,MEMCG_SWAP_MAX );memcg_memory_event (memcg,MEMCG_SWAP_FAIL ); - *swap_margin =page_counter_margin (counter);mem_cgroup_private_id_put (memcg, nr_pages);return -ENOMEM ; }mm/swapfile.c +3
-9@@ -1756,7 +1756,6 @@ int folio_alloc_swap(struct folio *folio) {unsigned int order =folio_order (folio);unsigned int size =1 << order; -long swap_margin =PAGE_COUNTER_MAX ;VM_BUG_ON_FOLIO (!folio_test_locked (folio), folio);VM_BUG_ON_FOLIO (!folio_test_uptodate (folio), folio);@@ -1766,10 +1765,8 @@ int folio_alloc_swap(struct folio *folio)* Reject large allocation when THP_SWAP is disabled. Check below * whether splitting and retrying can make progress. */ -if (!IS_ENABLED (CONFIG_THP_SWAP )) { - swap_margin =mem_cgroup_get_folio_swap_margin (folio); +if (!IS_ENABLED (CONFIG_THP_SWAP )) goto failed; - }/* * Allocation size should never exceed cluster size @@ -1777,7 +1774,6 @@ int folio_alloc_swap(struct folio *folio)*/ if (size >SWAPFILE_CLUSTER ) {VM_WARN_ON_ONCE (1 ); - swap_margin =mem_cgroup_get_folio_swap_margin (folio); goto failed; } }@@ -1794,10 +1790,8 @@ 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, &swap_margin))) { +if (unlikely (mem_cgroup_try_charge_swap (folio)))swap_cache_del_folio (folio); -return order && swap_margin >0 ? -E2BIG : -ENOMEM ; - }if (unlikely (!folio_test_swapcache (folio))) goto failed;@@ -1807,7 +1801,7 @@ int folio_alloc_swap(struct folio *folio) failed:if (get_nr_swap_pages () <=0 )return -ENOSPC ; -if (swap_margin <=0 ) +if (mem_cgroup_get_folio_swap_margin (folio) <=0 )return -ENOMEM ;return order ? -E2BIG : -ENOMEM ;
Barry Song
reviewerRe: [RFC PATCH v5 2/4] mm: distinguish large folio swap allocation failures
In reply to Xueyuan ChenQuoted 22 lines · Xueyuan Chen
folio_alloc_swap()reports most allocation failures with a generic negative error code. Reclaim cannot tell whether splitting a large folio could make progress or whether there is no backing space at all.Keep the global free swap count and the remaining hierarchical memcg swap margin as separate inputs. The memcg charge path reports only its own margin;
folio_alloc_swap()combines the two layers when classifying an allocation failure.Return
-E2BIGfor large folios when a smaller allocation might still fit,-ENOSPCwhen no global swap space is available, and-ENOMEMwhen the failure is not helped by splitting.For early large-folio rejections, check global and memcg swap availability instead of returning
-E2BIGunconditionally. On a memcg charge failure, swap slot allocation has already succeeded, so use the remaining memcg margin to decide whether a smaller charge might fit.This only refines
folio_alloc_swap()return codes. The reclaim callers are updated separately.
[...]
mm/swapfile.c int folio_alloc_swap(struct folio *folio) · line 1796
Quoted 4 lines
mm/swapfile.c +4
-0+ +failed: +if (get_nr_swap_pages () <=0 ) +return -ENOSPC ;
I wonder if we can do this earlier when swap is full or disabled. We could exit immediately and avoid trying many redundant paths. Have you tried this?
mm/swapfile.c int folio_alloc_swap(struct folio *folio) · line 1800
Quoted 4 lines
mm/swapfile.c +4
-0+if (swap_margin <=0 ) +return -ENOMEM ; + +return order ? -E2BIG : -ENOMEM ;
Best Regards
Barry
Re: [RFC PATCH v5 2/4] mm: distinguish large folio swap allocation failures
Quoted 25 lines · Barry Song
Quoted 22 lines · Xueyuan Chen
folio_alloc_swap()reports most allocation failures with a generic negative error code. Reclaim cannot tell whether splitting a large folio could make progress or whether there is no backing space at all.Keep the global free swap count and the remaining hierarchical memcg swap margin as separate inputs. The memcg charge path reports only its own margin;
folio_alloc_swap()combines the two layers when classifying an allocation failure.Return
-E2BIGfor large folios when a smaller allocation might still fit,-ENOSPCwhen no global swap space is available, and-ENOMEMwhen the failure is not helped by splitting.For early large-folio rejections, check global and memcg swap availability instead of returning
-E2BIGunconditionally. On a memcg charge failure, swap slot allocation has already succeeded, so use the remaining memcg margin to decide whether a smaller charge might fit.This only refines
folio_alloc_swap()return codes. The reclaim callers are updated separately.[...]
Quoted 9 lines
mm/swapfile.c int folio_alloc_swap(struct folio *folio) · line 1796
Quoted 4 lines
mm/swapfile.c +4
-0+ +failed: +if (get_nr_swap_pages () <=0 ) +return -ENOSPC ;I wonder if we can do this earlier when swap is full or disabled. We could exit immediately and avoid trying many redundant paths. Have you tried this?
Hi Barry,
Regarding moving the check earlier, I currently have two concerns:
1. It would add an extra get_nr_swap_pages() atomic read to every successful allocation path.
2. If the failure-time check is removed, the earlier snapshot could become stale while the allocation is attempted. The failure path could then return -E2BIG even if global swap has since been exhausted.
Thanks,
Xueyuan
Quoted 7 lines
mm/swapfile.c int folio_alloc_swap(struct folio *folio) · line 1800
Quoted 4 lines
mm/swapfile.c +4
-0+if (swap_margin <=0 ) +return -ENOMEM ; + +return order ? -E2BIG : -ENOMEM ;Best Regards
Barry
RFC PATCH v5 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
Re: [RFC PATCH v5 3/4] mm/vmscan: avoid pointless large folio splits without swap
Quoted 10 lines · Xueyuan Chen
From: "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
-E2BIGfromfolio_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.
Quoted 5 lines
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.
Reported-by Nanzhe Zhao <[email protected]>
Do you have a link to the report?
I assume this is not Fixes-worthy.
Quoted 25 lines
Signed-off-by Barry Song (Xiaomi) <[email protected]>Signed-off-by Xueyuan Chen <[email protected]>--- mm/vmscan.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-)mm/vmscan.c +3
-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);
mm/vmscan.c static unsigned int shrink_folio_list(struct list_head *folio_list, · line 1296
Quoted 10 lines
mm/vmscan.c +3
-0+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 · 3 lines
Cheers, David
Barry Song
reviewerRe: [RFC PATCH v5 3/4] mm/vmscan: avoid pointless large folio splits without swap
Quoted 12 lines · David Hildenbrand (Arm)
Quoted 10 lines · Xueyuan Chen
From: "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
-E2BIGfromfolio_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.
Quoted 7 lines
Quoted 5 lines
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.
Reported-by Nanzhe Zhao <[email protected]>Do you have a link to the report?
No. Nanzhe mentioned that to me verbally.
Quoted 3 lines
I assume this is not Fixes-worthy.
[...]
Quoted 7 lines
--
Cheers,David
Re: [RFC PATCH v5 3/4] mm/vmscan: avoid pointless large folio splits without swap
In reply to Xueyuan ChenQuoted 10 lines · Xueyuan Chen
From: "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
-E2BIGfromfolio_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.
Quoted 8 lines
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.
Reported-by Nanzhe Zhao <[email protected]>Signed-off-by Barry Song (Xiaomi) <[email protected]>Signed-off-by Xueyuan Chen <[email protected]>---
LGTM. Feel free to add:
RFC PATCH v5 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
Re: [RFC PATCH v5 4/4] mm/shmem: split large folios only on -E2BIG
Quoted 5 lines · Xueyuan Chen
shmem_writeout()currently splits a large folio on everyfolio_alloc_swap()failure. With the refined return-value contract, only-E2BIGindicates that splitting might allow smaller folios to be swapped out.
Quoted 7 lines
Enter the split fallback only for
-E2BIG. For-ENOSPCand-ENOMEM, redirty and reactivate the folio as before.Suggested-by Baolin Wang <[email protected]>Reviewed-by Baolin Wang <[email protected]>Signed-off-by Xueyuan Chen <[email protected]>---
Signature · 3 lines
Cheers, David
Re: [RFC PATCH v5 4/4] mm/shmem: split large folios only on -E2BIG
In reply to Xueyuan ChenQuoted 6 lines · Xueyuan Chen
shmem_writeout()currently splits a large folio on everyfolio_alloc_swap()failure. With the refined return-value contract, only-E2BIGindicates that splitting might allow smaller folios to be swapped out.
Quoted 7 lines
Enter the split fallback only for
-E2BIG. For-ENOSPCand-ENOMEM, redirty and reactivate the folio as before.Suggested-by Baolin Wang <[email protected]>Reviewed-by Baolin Wang <[email protected]>Signed-off-by Xueyuan Chen <[email protected]>---
LGTM,