mm/mglru: deactivate anon folios into the oldest generation
Subsystems (4)
v4 cover mm/mglru: deactivate anon folios into the oldest generation
From: Ridong Chen <[email protected]>
With MGLRU, folio_deactivate() puts anon folios that are not in the swap cache into max_seq - 1, the second youngest generation, so MADV_COLD and DAMOS_LRU_DEPRIO move them away from eviction instead of towards it.
Patch 1 lets lru_gen_add_folio() take a [min_seq, max_seq] range for the target generation and renames "reclaiming" to "tail", as Kairui suggested. No functional change intended.
Patch 2 uses it in lru_deactivate() to add the folio to the head of the oldest generation, which matches the classic LRU, as Barry suggested.
Changes in v4:
- Make tail only pick which end of the list the folio is added to, and let lruvec_add_folio_tail() and fill_evictable() pass min_seq[type] as max_seq to keep the folio in the oldest gen (Barry).
- Mark lruvec_add_deactivated_folio() inline (Barry).
- Rebase on the current mm-new, which no longer has the MGLRU-FG series.
Changes in v3:
- Add MIN_SEQ_UNBOUNDED and MAX_SEQ_UNBOUNDED for lru_gen_add_folio() instead of open coding -1UL (Barry). They are identity bounds, so the "!= -1UL" special cases are dropped.
- Rename lru_gen_add_deactivated_folio() to lruvec_add_deactivated_folio() and handle the classic LRU fallback inside it, so lru_deactivate() does not need to care (Barry).
- Rebase on the current mm-new, which has the MGLRU-FG series.
Changes in v2:
- Add the folio to the head of the oldest generation instead of its tail, so file folios keep their old position and anon folios are not put ahead of them (Barry).
- Build on top of the scanned_seq change in mm-new and add a seq range to lru_gen_add_folio() instead of open coding the move (Kairui).
- Keep the per-cpu batching in folio_deactivate().
Link to v3:
https://lore.kernel.org/linux-mm/[email protected]/
Link to v2:
https://lore.kernel.org/linux-mm/[email protected]/
Link to v1:
https://lore.kernel.org/linux-mm/[email protected]/
This series was developed with the help of an AI coding assistant (Claude).
Ridong Chen (2):
mm/mglru: pass a seq range to lru_gen_add_folio() mm/mglru: deactivate anon folios into the oldest generation
include/linux/mm_inline.h | 57 +++++++++++++++++++-------------------- mm/folio.c | 21 ++++++++++++++- mm/vmscan.c | 16 ++++++----- 3 files changed, 56 insertions(+), 38 deletions(-)
base-commit: 3d27e49eb905a5119e78fbe3d3890f376d15c728
Signature · 1 line
2.34.1
PATCH v4 1/2 mm/mglru: pass a seq range to lru_gen_add_folio()
From: Ridong Chen <[email protected]>
lru_gen_add_folio() and lru_gen_add_rejected_folio() are wrappers around __lru_gen_add_folio(), which can only raise the generation picked by lru_gen_folio_seq(), through scanned_seq. There is no way for a caller to lower it, e.g. to put a folio into the oldest generation without also adding it to the tail.
Let lru_gen_add_folio() take a range [min_seq, max_seq] that the target generation is capped to, and drop the two wrappers. MIN_SEQ_UNBOUNDED (0) and MAX_SEQ_UNBOUNDED (ULONG_MAX) mean no limit on that side. They leave the seq unchanged when capped to, so no special case is needed for them. move_folios_to_lru() passes scanned_seq + 1 as min_seq for rejected folios, as before.
Also rename "reclaiming" to "tail", and make it only pick which end of the list the folio is added to. The generation is now only decided by lru_gen_folio_seq() and the range, so lru_gen_folio_seq() no longer needs the argument. The two callers that add to the tail of the oldest generation, lruvec_add_folio_tail() and fill_evictable() for inactive folios, pass min_seq[type] as max_seq instead.
This does not change behavior. lru_gen_folio_seq() never returns a seq below min_seq[type], so capping it to min_seq[type] gives min_seq[type], which is what the old reclaiming branch resulted in. That branch was only skipped for active folios, and none of the callers that pass true add an active folio.
No functional change intended.
--- include/linux/mm_inline.h | 57 +++++++++++++++++++-------------------- mm/vmscan.c | 16 ++++++----- 2 files changed, 36 insertions(+), 37 deletions(-)
include/linux/mm_inline.h +27 -30
@@ -106,6 +106,10 @@ static __always_inline enum lru_list folio_lru_list(const struct folio *folio)return lru; } +/* no limit on that side of the seq range for lru_gen_add_folio() */ +#define MIN_SEQ_UNBOUNDED 0UL +#define MAX_SEQ_UNBOUNDED ULONG_MAX +#ifdef CONFIG_LRU_GEN static inline bool lru_gen_switching (void )
@@ -299,8 +303,7 @@ static inline void lru_gen_update_size(struct lruvec *lruvec, struct folio *foli }static inline unsigned long lru_gen_folio_seq (const struct lruvec *lruvec, -const struct folio *folio, -bool reclaiming) +const struct folio *folio) {int gen;int type =folio_is_file_lru (folio);
@@ -320,8 +323,6 @@ static inline unsigned long lru_gen_folio_seq(const struct lruvec *lruvec,*/ if (folio_test_active (folio)) gen =MIN_NR_GENS -folio_test_workingset (folio); -else if (reclaiming) - gen =MAX_NR_GENS ;else if ((!folio_is_file_lru (folio) && !folio_test_swapcache (folio)) || (folio_test_reclaim (folio) && (folio_test_dirty (folio) ||folio_test_writeback (folio))))
@@ -332,8 +333,14 @@ static inline unsigned long lru_gen_folio_seq(const struct lruvec *lruvec,return max (READ_ONCE (lrugen->max_seq) - gen +1 ,READ_ONCE (lrugen->min_seq[type])); } -static inline bool __lru_gen_add_folio (struct lruvec *lruvec,struct folio *folio, -unsigned long scanned_seq,bool reclaiming) +/* +* Cap the seq to [min_seq, max_seq], see MIN_SEQ_UNBOUNDED and +* MAX_SEQ_UNBOUNDED for no limit. tail only picks which end of the list +* the folio is added to. +*/ +static inline bool lru_gen_add_folio (struct lruvec *lruvec,struct folio *folio, +unsigned long min_seq,unsigned long max_seq, +bool tail) {unsigned long seq;unsigned long flags;
@@ -347,9 +354,9 @@ static inline bool __lru_gen_add_folio(struct lruvec *lruvec, struct folio *foliif (folio_test_unevictable (folio) || !lrugen->enabled)return false; - seq =lru_gen_folio_seq (lruvec, folio, reclaiming); -if (scanned_seq !=-1UL ) - seq =max (seq,min (scanned_seq +1 , lrugen->max_seq)); + seq =lru_gen_folio_seq (lruvec, folio); + seq =max (seq,min (min_seq, lrugen->max_seq)); + seq =min (seq, max_seq); gen =lru_gen_from_seq (seq); flags = (gen +1UL ) <<LRU_GEN_PGOFF ;/* see the comment on MIN_NR_GENS about PG_active */
@@ -357,7 +364,7 @@ static inline bool __lru_gen_add_folio(struct lruvec *lruvec, struct folio *folilru_gen_update_size (lruvec, folio,-1 , gen);/* for folio_rotate_reclaimable() */ -if (reclaiming) +if (tail)list_add_tail (&folio->lru, &lrugen->folios[gen][type][zone]);else list_add (&folio->lru, &lrugen->folios[gen][type][zone]);
@@ -365,17 +372,6 @@ static inline bool __lru_gen_add_folio(struct lruvec *lruvec, struct folio *folireturn true; } -static inline bool lru_gen_add_folio (struct lruvec *lruvec,struct folio *folio,bool reclaiming) -{ -return __lru_gen_add_folio (lruvec, folio,-1UL , reclaiming); -} - -static inline bool lru_gen_add_rejected_folio (struct lruvec *lruvec,struct folio *folio, -unsigned long scanned_seq) -{ -return __lru_gen_add_folio (lruvec, folio, scanned_seq, false); -} -static inline bool lru_gen_del_folio (struct lruvec *lruvec,struct folio *folio,bool reclaiming) {unsigned long flags;
@@ -429,13 +425,9 @@ static inline bool lru_gen_in_fault(void)return false; } -static inline bool lru_gen_add_folio (struct lruvec *lruvec,struct folio *folio,bool reclaiming) -{ -return false; -} - -static inline bool lru_gen_add_rejected_folio (struct lruvec *lruvec,struct folio *folio, -unsigned long scanned_seq) +static inline bool lru_gen_add_folio (struct lruvec *lruvec,struct folio *folio, +unsigned long min_seq,unsigned long max_seq, +bool tail) {return false; }
@@ -459,7 +451,7 @@ void lruvec_add_folio(struct lruvec *lruvec, struct folio *folio)VM_WARN_ON_ONCE_FOLIO (!folio_matches_lruvec (folio, lruvec), folio); -if (lru_gen_add_folio (lruvec, folio, false)) +if (lru_gen_add_folio (lruvec, folio,MIN_SEQ_UNBOUNDED ,MAX_SEQ_UNBOUNDED , false))return ; update_lru_size(lruvec, lru, folio_zonenum(folio),
@@ -475,8 +467,13 @@ void lruvec_add_folio_tail(struct lruvec *lruvec, struct folio *folio)VM_WARN_ON_ONCE_FOLIO (!folio_matches_lruvec (folio, lruvec), folio); -if (lru_gen_add_folio (lruvec, folio, true)) +#ifdef CONFIG_LRU_GEN +/* the tail of the LRU is the tail of the oldest gen */ +if (lru_gen_add_folio (lruvec, folio,MIN_SEQ_UNBOUNDED , +READ_ONCE (lruvec->lrugen.min_seq[folio_is_file_lru (folio)]), + true))return ; +#endif update_lru_size (lruvec, lru,folio_zonenum (folio),folio_nr_pages (folio));
mm/vmscan.c +9 -7
@@ -1916,7 +1916,8 @@ static unsigned int move_folios_to_lru(struct list_head *list, unsigned long seq !folio_test_referenced(folio) && folio_ref_count(folio) == folio_expected_ref_count(folio))lruvec_add_folio_tail (lruvec, folio); - else if (seq == -1UL || !lru_gen_add_rejected_folio(lruvec, folio, seq)) + else if (seq == -1UL || + !lru_gen_add_folio(lruvec, folio, seq + 1, MAX_SEQ_UNBOUNDED, false))lruvec_add_folio (lruvec, folio); nr_pages =folio_nr_pages (folio);
@@ -5515,10 +5516,11 @@ static bool fill_evictable(struct lruvec *lruvec)while (!list_empty (head)) {bool success;struct folio *folio; +unsigned long max_seq;/* * lru_gen_add_folio() uses list_add_tail() rather -* than list_add() when reclaiming is true. Match +* than list_add() when tail is true. Match * its ordering to avoid cold/hot inversion. */ if (active)
@@ -5532,11 +5534,11 @@ static bool fill_evictable(struct lruvec *lruvec)VM_WARN_ON_ONCE_FOLIO (folio_lru_gen (folio) !=-1 , folio);lruvec_del_folio (lruvec, folio); -/* -* Borrow reclaiming=true to place inactive folios in -* the older gens. -*/ - success =lru_gen_add_folio (lruvec, folio, !active); +/* inactive folios go to the tail of the oldest gen */ + max_seq = active ?MAX_SEQ_UNBOUNDED : +READ_ONCE (lruvec->lrugen.min_seq[type]); + success =lru_gen_add_folio (lruvec, folio,MIN_SEQ_UNBOUNDED , + max_seq, !active);VM_WARN_ON_ONCE (!success);if (!--remaining)
base-commit: 3d27e49eb905a5119e78fbe3d3890f376d15c728
Signature · 1 line
2.34.1
Re: [PATCH v4 1/2] mm/mglru: pass a seq range to lru_gen_add_folio()
Quoted 29 lines · Ridong Chen
From: Ridong Chen <[email protected]>
lru_gen_add_folio()andlru_gen_add_rejected_folio()are wrappers around__lru_gen_add_folio(), which can only raise the generation picked bylru_gen_folio_seq(), throughscanned_seq. There is no way for a caller to lower it, e.g. to put a folio into the oldest generation without also adding it to the tail.Let
lru_gen_add_folio()take a range [min_seq,max_seq] that the target generation is capped to, and drop the two wrappers.MIN_SEQ_UNBOUNDED(0) andMAX_SEQ_UNBOUNDED(ULONG_MAX) mean no limit on that side. They leave the seq unchanged when capped to, so no special case is needed for them.move_folios_to_lru()passesscanned_seq+ 1 asmin_seqfor rejected folios, as before.Also rename "reclaiming" to "tail", and make it only pick which end of the list the folio is added to. The generation is now only decided by
lru_gen_folio_seq()and the range, solru_gen_folio_seq()no longer needs the argument. The two callers that add to the tail of the oldest generation,lruvec_add_folio_tail()andfill_evictable()for inactive folios, pass min_seq[type] asmax_seqinstead.This does not change behavior.
lru_gen_folio_seq()never returns a seq below min_seq[type], so capping it to min_seq[type] gives min_seq[type], which is what the old reclaiming branch resulted in. That branch was only skipped for active folios, and none of the callers that pass true add an active folio.
Quoted 6 lines
No functional change intended.
Suggested-by Kairui Song <[email protected]>Suggested-by Barry Song <[email protected]>Signed-off-by Ridong Chen <[email protected]>---
+ mm-mglru-pass-a-seq-range-to-lru_gen_add_folio.patch added to mm-new branch
In reply to Ridong ChenThe patch titled Subject: mm/mglru: pass a seq range to lru_gen_add_folio() has been added to the -mm mm-new branch. Its filename is mm-mglru-pass-a-seq-range-to-lru_gen_add_folio.patch
The patch titled
Subject: mm/mglru: pass a seq range to lru_gen_add_folio()
has been added to the -mm mm-new branch. Its filename is
mm-mglru-pass-a-seq-range-to-lru_gen_add_folio.patch
This patch will shortly appear at
https://git.kernel.org/pub/scm/linux/kernel/git/akpm/25-new.git/tree/patches/mm-mglru-pass-a-seq-range-to-lru_gen_add_folio.patch
This patch will later appear in the mm-new branch at
git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm
Note, mm-new is a provisional staging ground for work-in-progress patches, and acceptance into mm-new is a notification for others take notice and to finish up reviews. Please do not hesitate to respond to review feedback and post updated versions to replace or incrementally fixup patches in mm-new.
The mm-new branch of mm.git is not included in linux-next
If a few days of testing in mm-new is successful, the patch will me moved into mm.git's mm-unstable branch, which is included in linux-next
Before you just go and hit "reply", please:
a) Consider who else should be cc'ed
b) Prefer to cc a suitable mailing list as well
c) Ideally: find the original patch on the mailing list and do a reply-to-all to that, adding suitable additional cc's
*** Remember to use Documentation/process/submit-checklist.rst when testing your code ***
The -mm tree is included into linux-next via various branches at git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm
and is updated there most days
------------------------------------------------------
From: Ridong Chen <[email protected]>
Subject: mm/mglru: pass a seq range to lru_gen_add_folio()
Date: Fri, 27 Nov 2026 11:11:09 +0800
Patch series "mm/mglru: deactivate anon folios into the oldest generation", v4.
With MGLRU, folio_deactivate() puts anon folios that are not in the swap cache into max_seq - 1, the second youngest generation, so MADV_COLD and DAMOS_LRU_DEPRIO move them away from eviction instead of towards it.
Patch 1 lets lru_gen_add_folio() take a [min_seq, max_seq] range for the target generation and renames "reclaiming" to "tail", as Kairui suggested. No functional change intended.
Patch 2 uses it in lru_deactivate() to add the folio to the head of the oldest generation, which matches the classic LRU, as Barry suggested.
This patch (of 2):
lru_gen_add_folio() and lru_gen_add_rejected_folio() are wrappers around __lru_gen_add_folio(), which can only raise the generation picked by lru_gen_folio_seq(), through scanned_seq. There is no way for a caller to lower it, e.g. to put a folio into the oldest generation without also adding it to the tail.
Let lru_gen_add_folio() take a range [min_seq, max_seq] that the target generation is capped to, and drop the two wrappers. MIN_SEQ_UNBOUNDED (0) and MAX_SEQ_UNBOUNDED (ULONG_MAX) mean no limit on that side. They leave the seq unchanged when capped to, so no special case is needed for them. move_folios_to_lru() passes scanned_seq + 1 as min_seq for rejected folios, as before.
Also rename "reclaiming" to "tail", and make it only pick which end of the list the folio is added to. The generation is now only decided by lru_gen_folio_seq() and the range, so lru_gen_folio_seq() no longer needs the argument. The two callers that add to the tail of the oldest generation, lruvec_add_folio_tail() and fill_evictable() for inactive folios, pass min_seq[type] as max_seq instead.
This does not change behavior. lru_gen_folio_seq() never returns a seq below min_seq[type], so capping it to min_seq[type] gives min_seq[type], which is what the old reclaiming branch resulted in. That branch was only skipped for active folios, and none of the callers that pass true add an active folio.
No functional change intended.
include/linux/mm_inline.h | 57 +++++++++++++++++------------------- mm/vmscan.c | 16 +++++----- 2 files changed, 36 insertions(+), 37 deletions(-)
include/linux/mm_inline.h renamed from include/linux/mm_inline.h~mm-mglru-pass-a-seq-range-to-lru_gen_add_folio +27 -30
@@ -106,6 +106,10 @@ static __always_inline enum lru_list folreturn lru; } +/* no limit on that side of the seq range for lru_gen_add_folio() */ +#define MIN_SEQ_UNBOUNDED 0UL +#define MAX_SEQ_UNBOUNDED ULONG_MAX +#ifdef CONFIG_LRU_GEN static inline bool lru_gen_switching (void )
@@ -299,8 +303,7 @@ static inline void lru_gen_update_size(s }static inline unsigned long lru_gen_folio_seq (const struct lruvec *lruvec, -const struct folio *folio, -bool reclaiming) +const struct folio *folio) {int gen;int type =folio_is_file_lru (folio);
@@ -320,8 +323,6 @@ static inline unsigned long lru_gen_foli*/ if (folio_test_active (folio)) gen =MIN_NR_GENS -folio_test_workingset (folio); -else if (reclaiming) - gen =MAX_NR_GENS ;else if ((!folio_is_file_lru (folio) && !folio_test_swapcache (folio)) || (folio_test_reclaim (folio) && (folio_test_dirty (folio) ||folio_test_writeback (folio))))
@@ -332,8 +333,14 @@ static inline unsigned long lru_gen_folireturn max (READ_ONCE (lrugen->max_seq) - gen +1 ,READ_ONCE (lrugen->min_seq[type])); } -static inline bool __lru_gen_add_folio (struct lruvec *lruvec,struct folio *folio, -unsigned long scanned_seq,bool reclaiming) +/* +* Cap the seq to [min_seq, max_seq], see MIN_SEQ_UNBOUNDED and +* MAX_SEQ_UNBOUNDED for no limit. tail only picks which end of the list +* the folio is added to. +*/ +static inline bool lru_gen_add_folio (struct lruvec *lruvec,struct folio *folio, +unsigned long min_seq,unsigned long max_seq, +bool tail) {unsigned long seq;unsigned long flags;
@@ -347,9 +354,9 @@ static inline bool __lru_gen_add_folio(sif (folio_test_unevictable (folio) || !lrugen->enabled)return false; - seq =lru_gen_folio_seq (lruvec, folio, reclaiming); -if (scanned_seq !=-1UL ) - seq =max (seq,min (scanned_seq +1 , lrugen->max_seq)); + seq =lru_gen_folio_seq (lruvec, folio); + seq =max (seq,min (min_seq, lrugen->max_seq)); + seq =min (seq, max_seq); gen =lru_gen_from_seq (seq); flags = (gen +1UL ) <<LRU_GEN_PGOFF ;/* see the comment on MIN_NR_GENS about PG_active */
@@ -357,7 +364,7 @@ static inline bool __lru_gen_add_folio(slru_gen_update_size (lruvec, folio,-1 , gen);/* for folio_rotate_reclaimable() */ -if (reclaiming) +if (tail)list_add_tail (&folio->lru, &lrugen->folios[gen][type][zone]);else list_add (&folio->lru, &lrugen->folios[gen][type][zone]);
@@ -365,17 +372,6 @@ static inline bool __lru_gen_add_folio(sreturn true; } -static inline bool lru_gen_add_folio (struct lruvec *lruvec,struct folio *folio,bool reclaiming) -{ -return __lru_gen_add_folio (lruvec, folio,-1UL , reclaiming); -} - -static inline bool lru_gen_add_rejected_folio (struct lruvec *lruvec,struct folio *folio, -unsigned long scanned_seq) -{ -return __lru_gen_add_folio (lruvec, folio, scanned_seq, false); -} -static inline bool lru_gen_del_folio (struct lruvec *lruvec,struct folio *folio,bool reclaiming) {unsigned long flags;
@@ -429,13 +425,9 @@ static inline bool lru_gen_in_fault(voidreturn false; } -static inline bool lru_gen_add_folio (struct lruvec *lruvec,struct folio *folio,bool reclaiming) -{ -return false; -} - -static inline bool lru_gen_add_rejected_folio (struct lruvec *lruvec,struct folio *folio, -unsigned long scanned_seq) +static inline bool lru_gen_add_folio (struct lruvec *lruvec,struct folio *folio, +unsigned long min_seq,unsigned long max_seq, +bool tail) {return false; }
@@ -459,7 +451,7 @@ void lruvec_add_folio(struct lruvec *lruVM_WARN_ON_ONCE_FOLIO (!folio_matches_lruvec (folio, lruvec), folio); -if (lru_gen_add_folio (lruvec, folio, false)) +if (lru_gen_add_folio (lruvec, folio,MIN_SEQ_UNBOUNDED ,MAX_SEQ_UNBOUNDED , false))return ; update_lru_size(lruvec, lru, folio_zonenum(folio),
@@ -475,8 +467,13 @@ void lruvec_add_folio_tail(struct lruvecVM_WARN_ON_ONCE_FOLIO (!folio_matches_lruvec (folio, lruvec), folio); -if (lru_gen_add_folio (lruvec, folio, true)) +#ifdef CONFIG_LRU_GEN +/* the tail of the LRU is the tail of the oldest gen */ +if (lru_gen_add_folio (lruvec, folio,MIN_SEQ_UNBOUNDED , +READ_ONCE (lruvec->lrugen.min_seq[folio_is_file_lru (folio)]), + true))return ; +#endif update_lru_size (lruvec, lru,folio_zonenum (folio),folio_nr_pages (folio));
mm/vmscan.c renamed from mm/vmscan.c~mm-mglru-pass-a-seq-range-to-lru_gen_add_folio +9 -7
@@ -1978,7 +1978,8 @@ static unsigned int move_folios_to_lru(s !folio_test_referenced(folio) && folio_ref_count(folio) == folio_expected_ref_count(folio))lruvec_add_folio_tail (lruvec, folio); - else if (seq == -1UL || !lru_gen_add_rejected_folio(lruvec, folio, seq)) + else if (seq == -1UL || + !lru_gen_add_folio(lruvec, folio, seq + 1, MAX_SEQ_UNBOUNDED, false))lruvec_add_folio (lruvec, folio); nr_pages =folio_nr_pages (folio);
@@ -5577,10 +5578,11 @@ static bool fill_evictable(struct lruvecwhile (!list_empty (head)) {bool success;struct folio *folio; +unsigned long max_seq;/* * lru_gen_add_folio() uses list_add_tail() rather -* than list_add() when reclaiming is true. Match +* than list_add() when tail is true. Match * its ordering to avoid cold/hot inversion. */ if (active)
@@ -5594,11 +5596,11 @@ static bool fill_evictable(struct lruvecVM_WARN_ON_ONCE_FOLIO (folio_lru_gen (folio) !=-1 , folio);lruvec_del_folio (lruvec, folio); -/* -* Borrow reclaiming=true to place inactive folios in -* the older gens. -*/ - success =lru_gen_add_folio (lruvec, folio, !active); +/* inactive folios go to the tail of the oldest gen */ + max_seq = active ?MAX_SEQ_UNBOUNDED : +READ_ONCE (lruvec->lrugen.min_seq[type]); + success =lru_gen_add_folio (lruvec, folio,MIN_SEQ_UNBOUNDED , + max_seq, !active);VM_WARN_ON_ONCE (!success);if (!--remaining)
_
Patches currently in -mm which might be from [email protected] are
mm-memcontrol-drop-unused-vmscan-tracepoint-include.patch mm-vmscan-move-vmscan-tracepoints-to-a-local-header.patch mm-vmscan-move-struct-scan_control-to-a-dedicated-header.patch mm-vmscan-pass-scan_control-to-the-vmscan-tracepoints.patch mm-mglru-add-tracepoint-for-scan_folios.patch mm-mglru-add-lruvec_gen_size-helper.patch mm-mglru-add-tracepoint-for-aging.patch mm-mglru-pass-a-seq-range-to-lru_gen_add_folio.patch mm-mglru-deactivate-anon-folios-into-the-oldest-generation.patch
Re: [PATCH v4 1/2] mm/mglru: pass a seq range to lru_gen_add_folio()
In reply to Ridong ChenQuoted 43 lines · Ridong Chen
From: Ridong Chen <[email protected]>
lru_gen_add_folio()andlru_gen_add_rejected_folio()are wrappers around__lru_gen_add_folio(), which can only raise the generation picked bylru_gen_folio_seq(), throughscanned_seq. There is no way for a caller to lower it, e.g. to put a folio into the oldest generation without also adding it to the tail.Let
lru_gen_add_folio()take a range [min_seq,max_seq] that the target generation is capped to, and drop the two wrappers.MIN_SEQ_UNBOUNDED(0) andMAX_SEQ_UNBOUNDED(ULONG_MAX) mean no limit on that side. They leave the seq unchanged when capped to, so no special case is needed for them.move_folios_to_lru()passesscanned_seq+ 1 asmin_seqfor rejected folios, as before.Also rename "reclaiming" to "tail", and make it only pick which end of the list the folio is added to. The generation is now only decided by
lru_gen_folio_seq()and the range, solru_gen_folio_seq()no longer needs the argument. The two callers that add to the tail of the oldest generation,lruvec_add_folio_tail()andfill_evictable()for inactive folios, pass min_seq[type] asmax_seqinstead.This does not change behavior.
lru_gen_folio_seq()never returns a seq below min_seq[type], so capping it to min_seq[type] gives min_seq[type], which is what the old reclaiming branch resulted in. That branch was only skipped for active folios, and none of the callers that pass true add an active folio.No functional change intended.
Suggested-by Kairui Song <[email protected]>Suggested-by Barry Song <[email protected]>Signed-off-by Ridong Chen <[email protected]>--- include/linux/mm_inline.h | 57 +++++++++++++++++++-------------------- mm/vmscan.c | 16 ++++++----- 2 files changed, 36 insertions(+), 37 deletions(-)include/linux/mm_inline.h +0
-0
include/linux/mm_inline.h static __always_inline enum lru_list folio_lru_list(const struct folio *folio) · line 112
Quoted 7 lines
include/linux/mm_inline.h +4
-0@@ -106,6 +106,10 @@ static __always_inline enum lru_list folio_lru_list(const struct folio *folio)return lru; } +/* no limit on that side of the seq range for lru_gen_add_folio() */ +#define MIN_SEQ_UNBOUNDED 0UL +#define MAX_SEQ_UNBOUNDED ULONG_MAX +
I think this should stay together with MIN_NR_GENS and MAX_NR_GENS and named MIN_SEQ_NUM and MAX_SEQ_NUM, they literally means that. But this is also fine, let's move them when we split the headers, right now MIN_NR_GENS and MAX_NR_GENS are in mmzone.h, which also seems a wrong place for MGLRU.
Quoted 23 lines
include/linux/mm_inline.h +1
-4#ifdef CONFIG_LRU_GEN static inline bool lru_gen_switching (void )@@ -299,8 +303,7 @@ static inline void lru_gen_update_size(struct lruvec *lruvec, struct folio *foli }static inline unsigned long lru_gen_folio_seq (const struct lruvec *lruvec, -const struct folio *folio, -bool reclaiming) +const struct folio *folio) {int gen;int type =folio_is_file_lru (folio);@@ -320,8 +323,6 @@ static inline unsigned long lru_gen_folio_seq(const struct lruvec *lruvec,*/ if (folio_test_active (folio)) gen =MIN_NR_GENS -folio_test_workingset (folio); -else if (reclaiming) - gen =MAX_NR_GENS ;else if ((!folio_is_file_lru (folio) && !folio_test_swapcache (folio)) || (folio_test_reclaim (folio) && (folio_test_dirty (folio) ||folio_test_writeback (folio))))
include/linux/mm_inline.h static inline unsigned long lru_gen_folio_seq(const struct lruvec *lruvec, · line 340
Quoted 10 lines
include/linux/mm_inline.h +5
-2@@ -332,8 +333,14 @@ static inline unsigned long lru_gen_folio_seq(const struct lruvec *lruvec,return max (READ_ONCE (lrugen->max_seq) - gen +1 ,READ_ONCE (lrugen->min_seq[type])); } -static inline bool __lru_gen_add_folio (struct lruvec *lruvec,struct folio *folio, -unsigned long scanned_seq,bool reclaiming) +/* +* Cap the seq to [min_seq, max_seq], see MIN_SEQ_UNBOUNDED and +* MAX_SEQ_UNBOUNDED for no limit. tail only picks which end of the list +* the folio is added to. +*/
Will be better if we add a proper kdoc here later.
Quoted 73 lines
include/linux/mm_inline.h +11
-23+static inline bool lru_gen_add_folio (struct lruvec *lruvec,struct folio *folio, +unsigned long min_seq,unsigned long max_seq, +bool tail) {unsigned long seq;unsigned long flags;@@ -347,9 +354,9 @@ static inline bool __lru_gen_add_folio(struct lruvec *lruvec, struct folio *foliif (folio_test_unevictable (folio) || !lrugen->enabled)return false; - seq =lru_gen_folio_seq (lruvec, folio, reclaiming); -if (scanned_seq !=-1UL ) - seq =max (seq,min (scanned_seq +1 , lrugen->max_seq)); + seq =lru_gen_folio_seq (lruvec, folio); + seq =max (seq,min (min_seq, lrugen->max_seq)); + seq =min (seq, max_seq); gen =lru_gen_from_seq (seq); flags = (gen +1UL ) <<LRU_GEN_PGOFF ;/* see the comment on MIN_NR_GENS about PG_active */ @@ -357,7 +364,7 @@ static inline bool __lru_gen_add_folio(struct lruvec *lruvec, struct folio *folilru_gen_update_size (lruvec, folio,-1 , gen);/* for folio_rotate_reclaimable() */ -if (reclaiming) +if (tail)list_add_tail (&folio->lru, &lrugen->folios[gen][type][zone]);else list_add (&folio->lru, &lrugen->folios[gen][type][zone]);@@ -365,17 +372,6 @@ static inline bool __lru_gen_add_folio(struct lruvec *lruvec, struct folio *folireturn true; } -static inline bool lru_gen_add_folio (struct lruvec *lruvec,struct folio *folio,bool reclaiming) -{ -return __lru_gen_add_folio (lruvec, folio,-1UL , reclaiming); -} - -static inline bool lru_gen_add_rejected_folio (struct lruvec *lruvec,struct folio *folio, -unsigned long scanned_seq) -{ -return __lru_gen_add_folio (lruvec, folio, scanned_seq, false); -} -static inline bool lru_gen_del_folio (struct lruvec *lruvec,struct folio *folio,bool reclaiming) {unsigned long flags;@@ -429,13 +425,9 @@ static inline bool lru_gen_in_fault(void)return false; } -static inline bool lru_gen_add_folio (struct lruvec *lruvec,struct folio *folio,bool reclaiming) -{ -return false; -} - -static inline bool lru_gen_add_rejected_folio (struct lruvec *lruvec,struct folio *folio, -unsigned long scanned_seq) +static inline bool lru_gen_add_folio (struct lruvec *lruvec,struct folio *folio, +unsigned long min_seq,unsigned long max_seq, +bool tail) {return false; }@@ -459,7 +451,7 @@ void lruvec_add_folio(struct lruvec *lruvec, struct folio *folio)VM_WARN_ON_ONCE_FOLIO (!folio_matches_lruvec (folio, lruvec), folio); -if (lru_gen_add_folio (lruvec, folio, false)) +if (lru_gen_add_folio (lruvec, folio,MIN_SEQ_UNBOUNDED ,MAX_SEQ_UNBOUNDED , false))return ; update_lru_size(lruvec, lru, folio_zonenum(folio),
include/linux/mm_inline.h void lruvec_add_folio_tail(struct lruvec *lruvec, struct folio *folio) · line 473
Quoted 8 lines
include/linux/mm_inline.h +4
-1@@ -475,8 +467,13 @@ void lruvec_add_folio_tail(struct lruvec *lruvec, struct folio *folio)VM_WARN_ON_ONCE_FOLIO (!folio_matches_lruvec (folio, lruvec), folio); -if (lru_gen_add_folio (lruvec, folio, true)) +#ifdef CONFIG_LRU_GEN +/* the tail of the LRU is the tail of the oldest gen */ + if (lru_gen_add_folio(lruvec, folio, MIN_SEQ_UNBOUNDED, + READ_ONCE(lruvec->lrugen.min_seq[folio_is_file_lru(folio)]),
Hmm, the new ifdef block and long opencoded READ_ONCE line doesn't look so nice.
Quoted 31 lines
mm/vmscan.c +4
-2@@ -1916,7 +1916,8 @@ static unsigned int move_folios_to_lru(struct list_head *list, unsigned long seq !folio_test_referenced(folio) && folio_ref_count(folio) == folio_expected_ref_count(folio))lruvec_add_folio_tail (lruvec, folio); - else if (seq == -1UL || !lru_gen_add_rejected_folio(lruvec, folio, seq)) + else if (seq == -1UL || + !lru_gen_add_folio(lruvec, folio, seq + 1, MAX_SEQ_UNBOUNDED, false))lruvec_add_folio (lruvec, folio); nr_pages =folio_nr_pages (folio);@@ -5515,10 +5516,11 @@ static bool fill_evictable(struct lruvec *lruvec)while (!list_empty (head)) {bool success;struct folio *folio; +unsigned long max_seq;/* * lru_gen_add_folio() uses list_add_tail() rather -* than list_add() when reclaiming is true. Match +* than list_add() when tail is true. Match * its ordering to avoid cold/hot inversion. */ if (active)@@ -5532,11 +5534,11 @@ static bool fill_evictable(struct lruvec *lruvec)VM_WARN_ON_ONCE_FOLIO (folio_lru_gen (folio) !=-1 , folio);lruvec_del_folio (lruvec, folio);
mm/vmscan.c static bool fill_evictable(struct lruvec *lruvec) · line 5541
Quoted 10 lines
mm/vmscan.c +5
-5-/* -* Borrow reclaiming=true to place inactive folios in -* the older gens. -*/ - success =lru_gen_add_folio (lruvec, folio, !active); +/* inactive folios go to the tail of the oldest gen */ + max_seq = active ?MAX_SEQ_UNBOUNDED : +READ_ONCE (lruvec->lrugen.min_seq[type]); + success =lru_gen_add_folio (lruvec, folio,MIN_SEQ_UNBOUNDED , + max_seq, !active);
I don't think we really care about the hotness with switching, this part may get simpler and finally dropped later.
mm/vmscan.c static bool fill_evictable(struct lruvec *lruvec) · line 5542
Quoted 1 line
mm/vmscan.c +0
-0VM_WARN_ON_ONCE (!success);
It still looks good though.
PATCH v4 2/2 mm/mglru: deactivate anon folios into the oldest generation
In reply to Ridong ChenFrom: Ridong Chen <[email protected]>
folio_deactivate() is used by MADV_COLD and DAMOS_LRU_DEPRIO to move a folio towards eviction. lru_deactivate() does that by deleting the folio from its LRU list, clearing PG_active and PG_referenced, and adding it back with lruvec_add_folio().
With MGLRU, lru_gen_folio_seq() places a swap-backed folio that is not in the swap cache, i.e. an anon or shmem folio, into max_seq - 1, regardless of its refs. So a folio that has aged into an older generation, e.g. min_seq + 1, is moved to a younger generation instead, while PGDEACTIVATE is still counted. Other folios go to an older generation with their refs cleared: the oldest one, or the second oldest if PG_workingset is set. The ones that have PG_reclaim set and are dirty or under writeback also go to max_seq - 1.
As a result, MADV_COLD can make anon memory less likely to be reclaimed. It can be reproduced with a single process in a memcg with swap enabled:
1) map two 64MB anon regions A and B, and fault in A;
2) age the memcg once through /sys/kernel/debug/lru_gen, then fault
in B, so that B is one generation younger than A;
3) apply MADV_COLD to B only, A is the control;
4) reclaim the size of one region from anon only:
echo "64M swappiness=max" > memory.reclaim; 5) count the swapped out pages of A and B from /proc/self/pagemap.
B is expected to be swapped out instead of A. Before this patch, B stays in its generation after MADV_COLD, so A is swapped out instead. After it, B is moved to min_seq. Three runs on mm-new with this patch:
A swapped out B (MADV_COLD) swapped out
0 ~ 190 pages 16384 pages
Cap the generation to min_seq[type] for MGLRU, so that the folio is added to the head of the oldest generation. This matches the classic LRU, which adds a deactivated folio to the head of the inactive list, and is where most file folios already go. Folios that have PG_reclaim set and are dirty or under writeback are now placed there too, as the classic LRU puts them on the inactive list. min_seq only advances under the LRU lock, which is held here.
--- mm/folio.c | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-)
mm/folio.c +20 -1
@@ -599,6 +599,25 @@ static void lru_deactivate_file(struct lruvec *lruvec, struct folio *folio) } } +#ifdef CONFIG_LRU_GEN +/* with MGLRU, add the folio to the head of the oldest generation */ +static inline void lruvec_add_deactivated_folio (struct lruvec *lruvec, +struct folio *folio) +{ +int type =folio_is_file_lru (folio); +unsigned long seq = lruvec->lrugen.min_seq[type]; + +if (!lru_gen_add_folio (lruvec, folio,MIN_SEQ_UNBOUNDED , seq, false)) +lruvec_add_folio (lruvec, folio); +} +#else +static inline void lruvec_add_deactivated_folio (struct lruvec *lruvec, +struct folio *folio) +{ +lruvec_add_folio (lruvec, folio); +} +#endif +static void lru_deactivate (struct lruvec *lruvec,struct folio *folio) {long nr_pages =folio_nr_pages (folio);
@@ -609,7 +628,7 @@ static void lru_deactivate(struct lruvec *lruvec, struct folio *folio)lruvec_del_folio (lruvec, folio);folio_clear_active (folio);folio_clear_referenced (folio); -lruvec_add_folio (lruvec, folio); +lruvec_add_deactivated_folio (lruvec, folio);__count_vm_events (PGDEACTIVATE , nr_pages);count_memcg_events (lruvec_memcg (lruvec),PGDEACTIVATE , nr_pages);
Signature · 1 line
2.34.1
+ mm-mglru-deactivate-anon-folios-into-the-oldest-generation.patch added to mm-new branch
The patch titled Subject: mm/mglru: deactivate anon folios into the oldest generation has been added to the -mm mm-new branch. Its filename is…
The patch titled
Subject: mm/mglru: deactivate anon folios into the oldest generation
has been added to the -mm mm-new branch. Its filename is
mm-mglru-deactivate-anon-folios-into-the-oldest-generation.patch
This patch will shortly appear at
https://git.kernel.org/pub/scm/linux/kernel/git/akpm/25-new.git/tree/patches/mm-mglru-deactivate-anon-folios-into-the-oldest-generation.patch
This patch will later appear in the mm-new branch at
git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm
Note, mm-new is a provisional staging ground for work-in-progress patches, and acceptance into mm-new is a notification for others take notice and to finish up reviews. Please do not hesitate to respond to review feedback and post updated versions to replace or incrementally fixup patches in mm-new.
The mm-new branch of mm.git is not included in linux-next
If a few days of testing in mm-new is successful, the patch will me moved into mm.git's mm-unstable branch, which is included in linux-next
Before you just go and hit "reply", please:
a) Consider who else should be cc'ed
b) Prefer to cc a suitable mailing list as well
c) Ideally: find the original patch on the mailing list and do a reply-to-all to that, adding suitable additional cc's
*** Remember to use Documentation/process/submit-checklist.rst when testing your code ***
The -mm tree is included into linux-next via various branches at git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm
and is updated there most days
------------------------------------------------------
From: Ridong Chen <[email protected]>
Subject: mm/mglru: deactivate anon folios into the oldest generation
Date: Fri, 27 Nov 2026 11:11:10 +0800
folio_deactivate() is used by MADV_COLD and DAMOS_LRU_DEPRIO to move a folio towards eviction. lru_deactivate() does that by deleting the folio from its LRU list, clearing PG_active and PG_referenced, and adding it back with lruvec_add_folio().
With MGLRU, lru_gen_folio_seq() places a swap-backed folio that is not in the swap cache, i.e. an anon or shmem folio, into max_seq - 1, regardless of its refs. So a folio that has aged into an older generation, e.g. min_seq + 1, is moved to a younger generation instead, while PGDEACTIVATE is still counted. Other folios go to an older generation with their refs
cleared: the oldest one, or the second oldest if PG_workingset is set. The ones that have PG_reclaim set and are dirty or under writeback also go to max_seq - 1.
As a result, MADV_COLD can make anon memory less likely to be reclaimed. It can be reproduced with a single process in a memcg with swap enabled:
1) map two 64MB anon regions A and B, and fault in A;
2) age the memcg once through /sys/kernel/debug/lru_gen, then fault
in B, so that B is one generation younger than A;
3) apply MADV_COLD to B only, A is the control;
4) reclaim the size of one region from anon only:
echo "64M swappiness=max" > memory.reclaim; 5) count the swapped out pages of A and B from /proc/self/pagemap.
B is expected to be swapped out instead of A. Before this patch, B stays in its generation after MADV_COLD, so A is swapped out instead. After it, B is moved to min_seq. Three runs on mm-new with this patch:
A swapped out B (MADV_COLD) swapped out
0 ~ 190 pages 16384 pages
Cap the generation to min_seq[type] for MGLRU, so that the folio is added to the head of the oldest generation. This matches the classic LRU, which adds a deactivated folio to the head of the inactive list, and is where most file folios already go. Folios that have PG_reclaim set and are dirty or under writeback are now placed there too, as the classic LRU puts them on the inactive list. min_seq only advances under the LRU lock, which is held here.
mm/folio.c | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-)
mm/folio.c renamed from mm/folio.c~mm-mglru-deactivate-anon-folios-into-the-oldest-generation +20 -1
@@ -599,6 +599,25 @@ static void lru_deactivate_file(struct l } } +#ifdef CONFIG_LRU_GEN +/* with MGLRU, add the folio to the head of the oldest generation */ +static inline void lruvec_add_deactivated_folio (struct lruvec *lruvec, +struct folio *folio) +{ +int type =folio_is_file_lru (folio); +unsigned long seq = lruvec->lrugen.min_seq[type]; + +if (!lru_gen_add_folio (lruvec, folio,MIN_SEQ_UNBOUNDED , seq, false)) +lruvec_add_folio (lruvec, folio); +} +#else +static inline void lruvec_add_deactivated_folio (struct lruvec *lruvec, +struct folio *folio) +{ +lruvec_add_folio (lruvec, folio); +} +#endif +static void lru_deactivate (struct lruvec *lruvec,struct folio *folio) {long nr_pages =folio_nr_pages (folio);
@@ -609,7 +628,7 @@ static void lru_deactivate(struct lruveclruvec_del_folio (lruvec, folio);folio_clear_active (folio);folio_clear_referenced (folio); -lruvec_add_folio (lruvec, folio); +lruvec_add_deactivated_folio (lruvec, folio);__count_vm_events (PGDEACTIVATE , nr_pages);count_memcg_events (lruvec_memcg (lruvec),PGDEACTIVATE , nr_pages);
_
Patches currently in -mm which might be from [email protected] are
mm-memcontrol-drop-unused-vmscan-tracepoint-include.patch mm-vmscan-move-vmscan-tracepoints-to-a-local-header.patch mm-vmscan-move-struct-scan_control-to-a-dedicated-header.patch mm-vmscan-pass-scan_control-to-the-vmscan-tracepoints.patch mm-mglru-add-tracepoint-for-scan_folios.patch mm-mglru-add-lruvec_gen_size-helper.patch mm-mglru-add-tracepoint-for-aging.patch mm-mglru-pass-a-seq-range-to-lru_gen_add_folio.patch mm-mglru-deactivate-anon-folios-into-the-oldest-generation.patch