Re: [PATCH v6 17/18] mm/mglru: folio LRU refs based active/inactive number accounting

From: Barry Song

Date: Wed Nov 25 2026 - 05:03:16 EST


On Tue, Nov 24, 2026 at 2:53 AM Kairui Song via B4 Relay
<devnull+kasong.tencent.com@xxxxxxxxxx> wrote:
>
> From: Kairui Song <kasong@xxxxxxxxxxx>
>
> Currently the active/inactive LRU sizes exported through /proc/vmstat
> and memory.stat are derived from the generation window: folios in the
> two newest generations are accounted as active, the rest as inactive.
> This causes several problems:
>
> - Unmapped file folios can stay in older gens despite repeated use,
> so active files can be under-reported, unlike classical LRU.
>
> - The reading is jumpy: on aging, the second-newest gen, previously
> active, becomes inactive all at once, causing a huge drop of active
> folios without any change in use.
>
> - The reading is reversed on swapless machines. When swapping is
> disabled or unavailable, MGLRU ages anon by moving the gens directly
> without walking or touching any folio, to reduce the overhead. So
> inactive anon folios can suddenly count as active, or vice versa,
> despite no change in their hotness.
>
> With frequency guided promotion, hotness is already tracked per folio by
> its referenced count, so we can do folio level accounting instead: a
> folio is active once its refs reach LRU_REFS_ACTIVATED (currently
> LRU_REFS_PROTECTED).
>
> On reaching this threshold, folios are accounted to the active part.
> On forced per-folio aging or PID protection, refs are capped at
> LRU_REFS_WORKINGSET, dropping active folios to the inactive part.
> Note that on swapless machines anon refs are not reset by aging (the
> shortcut skips the per-folio walk), but the counts no longer oscillate
> as the generation indices wrap.
>
> The accounting is now per folio and lruvec independent, so no size
> adjustment is needed when reparenting a dying memcg: the hierarchical
> stats keep the counts in the parent automatically.

I'm still struggling to understand whether the local LRU accounting
is correct here. But if there is a bug, it wasn't introduced by
this patch.

[...]

> @@ -4743,7 +4823,6 @@ static void __lru_gen_reparent_memcg(struct lruvec *child_lruvec, struct lruvec
> int zone, int type)
> {
> struct lru_gen_folio *child_lrugen, *parent_lrugen;
> - enum lru_list lru = type * LRU_INACTIVE_FILE;
> int gen;
>
> child_lrugen = &child_lruvec->lrugen;
> @@ -4756,8 +4835,6 @@ static void __lru_gen_reparent_memcg(struct lruvec *child_lruvec, struct lruvec
> */
> for (gen = 0; gen < MAX_NR_GENS; gen++) {
> long nr_pages = atomic_long_read(&child_lrugen->nr_pages[gen][type][zone]);
> - int child_lru_active = lru_gen_is_active(child_lruvec, gen) ? LRU_ACTIVE : 0;
> - int parent_lru_active = lru_gen_is_active(parent_lruvec, gen) ? LRU_ACTIVE : 0;
>
> /* Assuming that child pages are colder than parent pages */
> list_splice_tail_init(&child_lrugen->folios[gen][type][zone],
> @@ -4765,11 +4842,6 @@ static void __lru_gen_reparent_memcg(struct lruvec *child_lruvec, struct lruvec
>
> atomic_long_set(&child_lrugen->nr_pages[gen][type][zone], 0);
> atomic_long_add(nr_pages, &parent_lrugen->nr_pages[gen][type][zone]);
> -
> - if (lru_gen_is_active(child_lruvec, gen) != lru_gen_is_active(parent_lruvec, gen)) {
> - __update_lru_size(child_lruvec, lru + child_lru_active, zone, -nr_pages);
> - __update_lru_size(parent_lruvec, lru + parent_lru_active, zone, nr_pages);
> - }
> }
> }
>

The old code also skips updating the local stats when
lru_gen_is_active(child_lruvec, gen) ==
lru_gen_is_active(parent_lruvec, gen), as we skip
__update_lru_size() in that case.

But we do have some code in mm/workingset.c that relies on the
local stats:

if (sc->memcg && shadow_nodes.memcg_aware) {
struct lruvec *lruvec;
int i;

mem_cgroup_flush_stats_ratelimited(sc->memcg);
lruvec = mem_cgroup_lruvec(sc->memcg, NODE_DATA(sc->nid));
for (pages = 0, i = 0; i < NR_LRU_LISTS; i++)
pages += lruvec_page_state_local(lruvec,
NR_LRU_BASE + i);
pages += lruvec_page_state_local(
lruvec, NR_SLAB_RECLAIMABLE_B) >> PAGE_SHIFT;
pages += lruvec_page_state_local(
lruvec, NR_SLAB_UNRECLAIMABLE_B) >> PAGE_SHIFT;
}

So unless the local stats are transferred elsewhere during memcg
reparenting, this seems to be a pre-existing bug.

It would be relatively straightforward to fix in the old code.
With the new code, would we need to account for active/inactive
pages on a folio-by-folio basis during reparenting, or is there
still a way to transfer the stats in bulk?

Thanks,
Barry