* [PATCH v3 0/2] mm/mglru: deactivate anon folios into the oldest generation @ 2026-11-25 9:40 Ridong Chen 2026-11-25 9:40 ` [PATCH v3 1/2] mm/mglru: pass a seq range to lru_gen_add_folio() Ridong Chen 2026-11-25 9:40 ` [PATCH v3 2/2] mm/mglru: deactivate anon folios into the oldest generation Ridong Chen 0 siblings, 2 replies; 8+ messages in thread From: Ridong Chen @ 2026-11-25 9:40 UTC (permalink / raw) To: David Hildenbrand, Andrew Morton, Kairui Song, Johannes Weiner Cc: Liam R. Howlett, Vlastimil Babka, Mike Rapoport, Michal Hocko, Barry Song, Qi Zheng, Shakeel Butt, Axel Rasmussen, Yuanchu Xie, Wei Xu, Baoquan He, Baolin Wang, Yu Zhao, linux-mm, Ridong Chen, Ridong Chen From: Ridong Chen <chenridong@xiaomi.com> 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 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 v2: https://lore.kernel.org/linux-mm/20261119134740.514632-1-ridong.chen@linux.dev/ Link to v1: https://lore.kernel.org/linux-mm/20261112081928.3014946-1-ridong.chen@linux.dev/ 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 | 55 ++++++++++++++++++--------------------- mm/folio.c | 19 +++++++++++++- mm/vmscan.c | 12 ++++----- 3 files changed, 48 insertions(+), 38 deletions(-) base-commit: a036649714c3446abfdc36d0ae483046432b16c5 -- 2.34.1 ^ permalink raw reply [flat|nested] 8+ messages in thread
* [PATCH v3 1/2] mm/mglru: pass a seq range to lru_gen_add_folio() 2026-11-25 9:40 [PATCH v3 0/2] mm/mglru: deactivate anon folios into the oldest generation Ridong Chen @ 2026-11-25 9:40 ` Ridong Chen 2026-11-25 23:57 ` Barry Song 2026-11-25 9:40 ` [PATCH v3 2/2] mm/mglru: deactivate anon folios into the oldest generation Ridong Chen 1 sibling, 1 reply; 8+ messages in thread From: Ridong Chen @ 2026-11-25 9:40 UTC (permalink / raw) To: David Hildenbrand, Andrew Morton, Kairui Song, Johannes Weiner Cc: Liam R. Howlett, Vlastimil Babka, Mike Rapoport, Michal Hocko, Barry Song, Qi Zheng, Shakeel Butt, Axel Rasmussen, Yuanchu Xie, Wei Xu, Baoquan He, Baolin Wang, Yu Zhao, linux-mm, Ridong Chen, Ridong Chen From: Ridong Chen <chenridong@xiaomi.com> 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". With MGLRU, the tail of the LRU is the tail of the oldest generation, so when tail is true, lru_gen_add_folio() now picks min_seq[type] itself, and lru_gen_folio_seq() no longer needs the argument. This does not change behavior. The old reclaiming branch in lru_gen_folio_seq() also resulted in min_seq[type]. It was only skipped for active folios, and none of the callers that pass true add an active folio. max_seq has no user yet. The next patch uses it in lru_deactivate(). No functional change intended. Suggested-by: Kairui Song <kasong@tencent.com> Signed-off-by: Ridong Chen <chenridong@xiaomi.com> --- include/linux/mm_inline.h | 55 ++++++++++++++++++--------------------- mm/vmscan.c | 12 ++++----- 2 files changed, 30 insertions(+), 37 deletions(-) diff --git a/include/linux/mm_inline.h b/include/linux/mm_inline.h index 573b68ffbc63..148b230fb5c0 100644 --- a/include/linux/mm_inline.h +++ b/include/linux/mm_inline.h @@ -121,6 +121,10 @@ static inline int lru_get_gen_flags(unsigned long flags) return gen - 1; } +/* 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) @@ -353,8 +357,7 @@ static inline void lru_gen_update_size(struct lruvec *lruvec, int type, } static inline unsigned long lru_gen_folio_seq(const struct lruvec *lruvec, - const struct folio *folio, - bool reclaiming) + const struct folio *folio) { int age; bool workingset = lru_refs_is_workingset(folio_lru_refs(folio)); @@ -375,8 +378,6 @@ static inline unsigned long lru_gen_folio_seq(const struct lruvec *lruvec, */ if (folio_test_active(folio)) age = MIN_NR_GENS - workingset; - else if (reclaiming) - age = 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)))) @@ -387,8 +388,14 @@ static inline unsigned long lru_gen_folio_seq(const struct lruvec *lruvec, return max(READ_ONCE(lrugen->max_seq) - age + 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. With no limit and tail set, the folio is + * added to the tail of the oldest gen. + */ +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; @@ -406,9 +413,12 @@ static inline bool __lru_gen_add_folio(struct lruvec *lruvec, struct folio *foli if (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)); + if (tail) + seq = READ_ONCE(lrugen->min_seq[type]); + else + 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 */ @@ -422,7 +432,7 @@ static inline bool __lru_gen_add_folio(struct lruvec *lruvec, struct folio *foli __update_lru_size(lruvec, lru, zone, delta); /* 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]); @@ -430,17 +440,6 @@ static inline bool __lru_gen_add_folio(struct lruvec *lruvec, struct folio *foli return 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; @@ -495,13 +494,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; } @@ -595,7 +590,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), @@ -611,7 +606,7 @@ 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)) + if (lru_gen_add_folio(lruvec, folio, MIN_SEQ_UNBOUNDED, MAX_SEQ_UNBOUNDED, true)) return; update_lru_size(lruvec, lru, folio_zonenum(folio), diff --git a/mm/vmscan.c b/mm/vmscan.c index 81797b71228f..907d81185c36 100644 --- a/mm/vmscan.c +++ b/mm/vmscan.c @@ -2165,7 +2165,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); @@ -5722,7 +5723,7 @@ static bool fill_evictable(struct lruvec *lruvec) /* * 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) @@ -5736,11 +5737,8 @@ 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); + success = lru_gen_add_folio(lruvec, folio, MIN_SEQ_UNBOUNDED, + MAX_SEQ_UNBOUNDED, !active); VM_WARN_ON_ONCE(!success); if (!--remaining) -- 2.34.1 ^ permalink raw reply related [flat|nested] 8+ messages in thread
* Re: [PATCH v3 1/2] mm/mglru: pass a seq range to lru_gen_add_folio() 2026-11-25 9:40 ` [PATCH v3 1/2] mm/mglru: pass a seq range to lru_gen_add_folio() Ridong Chen @ 2026-11-25 23:57 ` Barry Song 2026-11-26 2:16 ` Ridong Chen 0 siblings, 1 reply; 8+ messages in thread From: Barry Song @ 2026-11-25 23:57 UTC (permalink / raw) To: Ridong Chen Cc: David Hildenbrand, Andrew Morton, Kairui Song, Johannes Weiner, Liam R. Howlett, Vlastimil Babka, Mike Rapoport, Michal Hocko, Qi Zheng, Shakeel Butt, Axel Rasmussen, Yuanchu Xie, Wei Xu, Baoquan He, Baolin Wang, Yu Zhao, linux-mm, Ridong Chen > +/* > + * Cap the seq to [min_seq, max_seq], see MIN_SEQ_UNBOUNDED and > + * MAX_SEQ_UNBOUNDED for no limit. With no limit and tail set, the folio is > + * added to the tail of the oldest gen. > + */ Another option is to make tail and gen orthogonal, so that tail isn't necessarily tied to the oldest gen. This would be similar to the deactivate path, except that we would only need to set tail here without changing gen. Would that approach work as well? > +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; > @@ -406,9 +413,12 @@ static inline bool __lru_gen_add_folio(struct lruvec *lruvec, struct folio *foli > if (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)); > + if (tail) > + seq = READ_ONCE(lrugen->min_seq[type]); > + else > + seq = lru_gen_folio_seq(lruvec, folio); Then we wouldn't need any if/else branches here. > + seq = max(seq, min(min_seq, lrugen->max_seq)); Thanks, Barry ^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [PATCH v3 1/2] mm/mglru: pass a seq range to lru_gen_add_folio() 2026-11-25 23:57 ` Barry Song @ 2026-11-26 2:16 ` Ridong Chen 2026-11-26 12:47 ` Barry Song 0 siblings, 1 reply; 8+ messages in thread From: Ridong Chen @ 2026-11-26 2:16 UTC (permalink / raw) To: Barry Song Cc: David Hildenbrand, Andrew Morton, Kairui Song, Johannes Weiner, Liam R. Howlett, Vlastimil Babka, Mike Rapoport, Michal Hocko, Qi Zheng, Shakeel Butt, Axel Rasmussen, Yuanchu Xie, Wei Xu, Baoquan He, Baolin Wang, Yu Zhao, linux-mm, Ridong Chen On 11/26/2026 7:57 AM, Barry Song wrote: >> +/* >> + * Cap the seq to [min_seq, max_seq], see MIN_SEQ_UNBOUNDED and >> + * MAX_SEQ_UNBOUNDED for no limit. With no limit and tail set, the folio is >> + * added to the tail of the oldest gen. >> + */ > > Another option is to make tail and gen orthogonal, so that tail > isn't necessarily tied to the oldest gen. This would be similar > to the deactivate path, except that we would only need to set > tail here without changing gen. > > Would that approach work as well? > Yes, that works for me, and it is cleaner. tail will only pick the list end, and the two callers that pass tail=true will pass min_seq[type] as max_seq to keep the folio in the oldest gen, like the deactivate path in patch 2. lru_gen_folio_seq() never returns a seq below min_seq[type], so this gives the same result as before. The changes on top of v3 would be something like: lru_gen_add_folio() drops the if/else: seq = lru_gen_folio_seq(lruvec, folio); seq = max(seq, min(min_seq, lrugen->max_seq)); seq = min(seq, max_seq); lruvec_add_folio_tail() is outside CONFIG_LRU_GEN, so it reads lrugen under the #ifdef: ``` #ifdef CONFIG_LRU_GEN int type = folio_is_file_lru(folio); unsigned long seq = READ_ONCE(lruvec->lrugen.min_seq[type]); if (lru_gen_add_folio(lruvec, folio, MIN_SEQ_UNBOUNDED, seq,true)) return; #endif ``` fill_evictable() caps only the inactive folios: ``` 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); ``` Patch 2 stays the same. Does this look good to you? If so, I will send v4 with it. >> +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; >> @@ -406,9 +413,12 @@ static inline bool __lru_gen_add_folio(struct lruvec *lruvec, struct folio *foli >> if (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)); >> + if (tail) >> + seq = READ_ONCE(lrugen->min_seq[type]); >> + else >> + seq = lru_gen_folio_seq(lruvec, folio); > > Then we wouldn't need any if/else branches here. > >> + seq = max(seq, min(min_seq, lrugen->max_seq)); > > Thanks, > Barry -- Best regards Ridong ^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [PATCH v3 1/2] mm/mglru: pass a seq range to lru_gen_add_folio() 2026-11-26 2:16 ` Ridong Chen @ 2026-11-26 12:47 ` Barry Song 0 siblings, 0 replies; 8+ messages in thread From: Barry Song @ 2026-11-26 12:47 UTC (permalink / raw) To: Ridong Chen Cc: David Hildenbrand, Andrew Morton, Kairui Song, Johannes Weiner, Liam R. Howlett, Vlastimil Babka, Mike Rapoport, Michal Hocko, Qi Zheng, Shakeel Butt, Axel Rasmussen, Yuanchu Xie, Wei Xu, Baoquan He, Baolin Wang, Yu Zhao, linux-mm, Ridong Chen On Thu, Nov 26, 2026 at 10:16 AM Ridong Chen <ridong.chen@linux.dev> wrote: > > > > On 11/26/2026 7:57 AM, Barry Song wrote: > >> +/* > >> + * Cap the seq to [min_seq, max_seq], see MIN_SEQ_UNBOUNDED and > >> + * MAX_SEQ_UNBOUNDED for no limit. With no limit and tail set, the folio is > >> + * added to the tail of the oldest gen. > >> + */ > > > > Another option is to make tail and gen orthogonal, so that tail > > isn't necessarily tied to the oldest gen. This would be similar > > to the deactivate path, except that we would only need to set > > tail here without changing gen. > > > > Would that approach work as well? > > > > Yes, that works for me, and it is cleaner. tail will only pick the > list end, and the two callers that pass tail=true will pass > min_seq[type] as max_seq to keep the folio in the oldest gen, like > the deactivate path in patch 2. lru_gen_folio_seq() never returns a > seq below min_seq[type], so this gives the same result as before. > > The changes on top of v3 would be something like: > > lru_gen_add_folio() drops the if/else: > > seq = lru_gen_folio_seq(lruvec, folio); > seq = max(seq, min(min_seq, lrugen->max_seq)); > seq = min(seq, max_seq); > > lruvec_add_folio_tail() is outside CONFIG_LRU_GEN, so it reads > lrugen under the #ifdef: > > ``` > #ifdef CONFIG_LRU_GEN > int type = folio_is_file_lru(folio); > unsigned long seq = READ_ONCE(lruvec->lrugen.min_seq[type]); > > if (lru_gen_add_folio(lruvec, folio, MIN_SEQ_UNBOUNDED, seq,true)) > return; > #endif > ``` > > fill_evictable() caps only the inactive folios: > > ``` > 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); > ``` > Patch 2 stays the same. > > Does this look good to you? If so, I will send v4 with it. Sounds good to me, thanks! I guess we can gradually move more code from include/linux/mm_inline.h into mm/vmscan.h such as lruvec_add_folio, lruvec_add_folio_tail etc. Thanks, Barry ^ permalink raw reply [flat|nested] 8+ messages in thread
* [PATCH v3 2/2] mm/mglru: deactivate anon folios into the oldest generation 2026-11-25 9:40 [PATCH v3 0/2] mm/mglru: deactivate anon folios into the oldest generation Ridong Chen 2026-11-25 9:40 ` [PATCH v3 1/2] mm/mglru: pass a seq range to lru_gen_add_folio() Ridong Chen @ 2026-11-25 9:40 ` Ridong Chen 2026-11-26 0:02 ` Barry Song 1 sibling, 1 reply; 8+ messages in thread From: Ridong Chen @ 2026-11-25 9:40 UTC (permalink / raw) To: David Hildenbrand, Andrew Morton, Kairui Song, Johannes Weiner Cc: Liam R. Howlett, Vlastimil Babka, Mike Rapoport, Michal Hocko, Barry Song, Qi Zheng, Shakeel Butt, Axel Rasmussen, Yuanchu Xie, Wei Xu, Baoquan He, Baolin Wang, Yu Zhao, linux-mm, Ridong Chen, Ridong Chen From: Ridong Chen <chenridong@xiaomi.com> 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 are placed into the oldest generation with their refs cleared, except the ones that have PG_reclaim set and are dirty or under writeback, which 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, and after it, B is moved to min_seq. Three runs on mm-new: A swapped out B (MADV_COLD) swapped out before 16384 pages 63 ~ 150 pages after 58 ~ 78 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. Fixes: 081488051d28 ("mm/mglru: fix underprotected page cache") Suggested-by: Barry Song <baohua@kernel.org> Signed-off-by: Ridong Chen <chenridong@xiaomi.com> --- mm/folio.c | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/mm/folio.c b/mm/folio.c index 7521680874c5..0220419612ff 100644 --- a/mm/folio.c +++ b/mm/folio.c @@ -525,6 +525,23 @@ 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 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 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); @@ -535,7 +552,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); -- 2.34.1 ^ permalink raw reply related [flat|nested] 8+ messages in thread
* Re: [PATCH v3 2/2] mm/mglru: deactivate anon folios into the oldest generation 2026-11-25 9:40 ` [PATCH v3 2/2] mm/mglru: deactivate anon folios into the oldest generation Ridong Chen @ 2026-11-26 0:02 ` Barry Song 2026-11-26 2:18 ` Ridong Chen 0 siblings, 1 reply; 8+ messages in thread From: Barry Song @ 2026-11-26 0:02 UTC (permalink / raw) To: Ridong Chen Cc: David Hildenbrand, Andrew Morton, Kairui Song, Johannes Weiner, Liam R. Howlett, Vlastimil Babka, Mike Rapoport, Michal Hocko, Qi Zheng, Shakeel Butt, Axel Rasmussen, Yuanchu Xie, Wei Xu, Baoquan He, Baolin Wang, Yu Zhao, linux-mm, Ridong Chen On Wed, Nov 25, 2026 at 5:41 PM Ridong Chen <ridong.chen@linux.dev> wrote: > > From: Ridong Chen <chenridong@xiaomi.com> > > 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 are placed into the > oldest generation with their refs cleared, except the ones that have > PG_reclaim set and are dirty or under writeback, which 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, and after it, B is moved to min_seq. > Three runs on mm-new: > > A swapped out B (MADV_COLD) swapped out > before 16384 pages 63 ~ 150 pages > after 58 ~ 78 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. > > Fixes: 081488051d28 ("mm/mglru: fix underprotected page cache") > Suggested-by: Barry Song <baohua@kernel.org> > Signed-off-by: Ridong Chen <chenridong@xiaomi.com> With a few minor nits. Reviewed-by: Barry Song <baohua@kernel.org> > --- > mm/folio.c | 19 ++++++++++++++++++- > 1 file changed, 18 insertions(+), 1 deletion(-) > > diff --git a/mm/folio.c b/mm/folio.c > index 7521680874c5..0220419612ff 100644 > --- a/mm/folio.c > +++ b/mm/folio.c > @@ -525,6 +525,23 @@ 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 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 void lruvec_add_deactivated_folio(struct lruvec *lruvec, struct folio *folio) > +{ > + lruvec_add_folio(lruvec, folio); > +} > +#endif I'm not sure whether compilers will inline them, but explicitly marking them as inline should be fine? > + ^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [PATCH v3 2/2] mm/mglru: deactivate anon folios into the oldest generation 2026-11-26 0:02 ` Barry Song @ 2026-11-26 2:18 ` Ridong Chen 0 siblings, 0 replies; 8+ messages in thread From: Ridong Chen @ 2026-11-26 2:18 UTC (permalink / raw) To: Barry Song Cc: David Hildenbrand, Andrew Morton, Kairui Song, Johannes Weiner, Liam R. Howlett, Vlastimil Babka, Mike Rapoport, Michal Hocko, Qi Zheng, Shakeel Butt, Axel Rasmussen, Yuanchu Xie, Wei Xu, Baoquan He, Baolin Wang, Yu Zhao, linux-mm, Ridong Chen On 11/26/2026 8:02 AM, Barry Song wrote: > On Wed, Nov 25, 2026 at 5:41 PM Ridong Chen <ridong.chen@linux.dev> wrote: >> >> From: Ridong Chen <chenridong@xiaomi.com> >> >> 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 are placed into the >> oldest generation with their refs cleared, except the ones that have >> PG_reclaim set and are dirty or under writeback, which 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, and after it, B is moved to min_seq. >> Three runs on mm-new: >> >> A swapped out B (MADV_COLD) swapped out >> before 16384 pages 63 ~ 150 pages >> after 58 ~ 78 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. >> >> Fixes: 081488051d28 ("mm/mglru: fix underprotected page cache") >> Suggested-by: Barry Song <baohua@kernel.org> >> Signed-off-by: Ridong Chen <chenridong@xiaomi.com> > > With a few minor nits. > > Reviewed-by: Barry Song <baohua@kernel.org> > >> --- >> mm/folio.c | 19 ++++++++++++++++++- >> 1 file changed, 18 insertions(+), 1 deletion(-) >> >> diff --git a/mm/folio.c b/mm/folio.c >> index 7521680874c5..0220419612ff 100644 >> --- a/mm/folio.c >> +++ b/mm/folio.c >> @@ -525,6 +525,23 @@ 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 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 void lruvec_add_deactivated_folio(struct lruvec *lruvec, struct folio *folio) >> +{ >> + lruvec_add_folio(lruvec, folio); >> +} >> +#endif > > I'm not sure whether compilers will inline them, but explicitly > marking them as inline should be fine? > Sure, will mark them inline in v4. Thanks for the review! -- Best regards Ridong ^ permalink raw reply [flat|nested] 8+ messages in thread
end of thread, other threads:[~2026-11-26 12:47 UTC | newest] Thread overview: 8+ messages (download: mbox.gz follow: Atom feed -- links below jump to the message on this page -- 2026-11-25 9:40 [PATCH v3 0/2] mm/mglru: deactivate anon folios into the oldest generation Ridong Chen 2026-11-25 9:40 ` [PATCH v3 1/2] mm/mglru: pass a seq range to lru_gen_add_folio() Ridong Chen 2026-11-25 23:57 ` Barry Song 2026-11-26 2:16 ` Ridong Chen 2026-11-26 12:47 ` Barry Song 2026-11-25 9:40 ` [PATCH v3 2/2] mm/mglru: deactivate anon folios into the oldest generation Ridong Chen 2026-11-26 0:02 ` Barry Song 2026-11-26 2:18 ` Ridong Chen
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox