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

From: Kairui Song

Date: Mon Nov 30 2026 - 13:02:40 EST


On Wed, Nov 25, 2026 at 6:03 PM Barry Song <baohua@xxxxxxxxxx> wrote:
>
> 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?

Hi Barry,

The local stats do move in bulk, and they're moved after the lists:
memcg_reparent_objcgs() does the per-node lru_gen_reparent_memcg(),
then reparent_state_local(). reparent_v2_lruvec_state_local(), which
reparents NR_LRU_BASE + i for every LRU list (plus the two slab items)
via reparent_memcg_lruvec_state_local(); v1 does the same through
reparent_memcg1_lruvec_state_local(). I also added a fix for that in
this series, and the comment above that function addresses exactly
your case, without it "the ancestor would only receive the negative
deltas ... and its local stats would permanently underflow".

The reparenting race might look ugly before this series, but there
will no longer be any race after this commit as the generation number
is no longer bound to active/inactive counting.

The old conditional __update_lru_size() pair wasn't moving totals, it
only re-bucketed pages whose active/inactive meaning differed between
the child's and the parent's gen windows. With refs-based accounting
the bucket is a property of the folio, not of the lruvec, so it no
longer changes when the folio moves to the parent, the child's local
counter already holds the right bucket and bulk transfer is enough.
Nothing needs to be worried or walked per folio.

And here for the lockless updater: it resolves the live ancestor under
RCU, so its -inactive/+active pair lands on the parent while the
child's counter still holds the folio, and the later bulk transfer
cancels it exactly. That's what patch 3's raw signed read keeps
honest.

I'll send a v7 shortly and add some comment or commit message on these
too. Thanks for the review!