Re: [PATCH v9 09/16] mm/mglru: Avoid placing rejected folios below populated gen
From: Kairui Song
Date: Sun Nov 15 2026 - 10:16:34 EST
On Sun, Nov 15, 2026 at 4:08 AM Barry Song (Xiaomi) <baohua@xxxxxxxxxx> wrote:
>
> We used to scan only the oldest generation in scan_folios(). In
> evict_folios(), we checked whether
> lru_gen_folio_seq(lruvec, folio, false) == min_seq[type]. If so, the
> folio was marked active and moved to younger generations.
>
> This was itself buggy, as folios in generation min_seq[type] - 1
> would not be marked active by folio_set_active(), potentially causing
> a cold/hot inversion.
>
> Besides this issue, the old approach also differs from the current
> behavior, where we can scan fewer than two generations.
> Now that we can scan younger generations, including the youngest
> generation, this no longer prevents rejected folios from being placed
> in a generation below or at the populated generation. Such rejected
> folios would then be scanned again and again, resulting in extremely
> low `pg_steal/pg_scan` ratios. For example:
> pgsteal_file: 2312605
> pgscan_file: 36465693
> reclamation efficiency: 6.34%
>
> We pass `scanned_seq` to `move_folios_to_lru()` and make sure we either
> promote the folio or place it in the youngest generation when the
> populated generation is already the youngest one.
> Also, folio_set_active() is no longer needed.
>
> With this change, the results become:
> pgsteal_file: 2683815
> pgscan_file: 4224244
> reclamation efficiency: 63.53%
That's a really nice improvement! And I have seen the rejected folio
got inserted to wrong gen many times, this will help to fix that too.
>
> The sys time for building the kernel also decreased from 4m30.532s to
> 3m50.188s while swappiness==1.
>
> Signed-off-by: Barry Song (Xiaomi) <baohua@xxxxxxxxxx>
> Reviewed-by: Kunwu Chan <kunwu.chan@xxxxxxxxx>
> ---
> include/linux/mm_inline.h | 22 +++++++++++++++++++++-
> mm/vmscan.c | 24 ++++++++++++------------
> 2 files changed, 33 insertions(+), 13 deletions(-)
>
> diff --git a/include/linux/mm_inline.h b/include/linux/mm_inline.h
> index ab69b9930893..f6f4bc9a704f 100644
> --- a/include/linux/mm_inline.h
> +++ b/include/linux/mm_inline.h
> @@ -332,7 +332,8 @@ 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, bool reclaiming)
> +static inline bool __lru_gen_add_folio(struct lruvec *lruvec, struct folio *folio,
> + unsigned long scanned_seq, bool reclaiming)
A little nitpick: I think the code will be cleaner if we use "long
min_seq" and get rid of __lru_gen_add_folio, caller of
lru_gen_add_folio just use -1 as min_seq if they don't have a specific
gen limitation, but we can just leave this as it is now to avoid more
chore update of this one.
...
> @@ -4991,7 +4995,7 @@ static int evict_folios(unsigned long nr_to_scan, struct lruvec *lruvec,
>
> tier = get_tier_idx(lruvec, type);
> scanned = scan_folios(nr_to_scan, lruvec, sc,
> - type, tier, &list, &isolated);
> + type, tier, &list, &isolated, &scanned_seq);
> nr_isolated = isolated;
> if (nr_isolated)
> __mod_node_page_state(pgdat, NR_ISOLATED_ANON + type,
> @@ -5017,8 +5021,6 @@ static int evict_folios(unsigned long nr_to_scan, struct lruvec *lruvec,
> type ? LRU_INACTIVE_FILE : LRU_INACTIVE_ANON);
>
> list_for_each_entry_safe_reverse(folio, next, &list, lru) {
> - DEFINE_MIN_SEQ(lruvec);
> -
> /* move_folios_to_lru() culls unevictable folios via folio_putback_lru() */
> if (!folio_evictable(folio))
> continue;
> @@ -5037,11 +5039,9 @@ static int evict_folios(unsigned long nr_to_scan, struct lruvec *lruvec,
> * so this effectively promotes them by at least one generation.
> */
> folio_set_lru_refs(folio, 0);
> - if (lru_gen_folio_seq(lruvec, folio, false) == min_seq[type])
> - folio_set_active(folio);
Nice, this part is causing trouble indeed.
> }
>
> - move_folios_to_lru(&list, false);
> + move_folios_to_lru(&list, scanned_seq, false);
>
> walk = current->reclaim_state->mm_walk;
> if (walk && walk->batched) {
> --
> 2.39.3 (Apple Git-146)
>
Thanks! Looks good to me.
Reviewed-by: Kairui Song <kasong@xxxxxxxxxxx>