Re: [RFC PATCH 0/1] mm/mglru: MADV_RANDOM page cache never ages and thrashes the oldest generation

From: Kairui Song

Date: Thu Nov 26 2026 - 21:55:15 EST


On Sun, Nov 22, 2026 at 5:00 AM Maxim Muzafarov <maxmuzaf@xxxxxxxxx> wrote:
>
> Hi Tianyi, Kairui,
>
> Sorry for the late reply. There was a lot of new information for me
> to digest.
>
> I've checked the reproducer you provided. It tests a slightly
> different scenario from the one I mentioned in my first email.
> However, I managed to adapt it [1] to my case and tested everything
> with the same setup (x86-64 VM), and I also reproduced your results.
>
> The key difference [1] is that I added two new knobs:
> - SCAN_PASSES (default 1, your value), how many times each scan page
> is touched
> - SCAN_MB (default 96, your value), the cold scan size in MiB
>
> This is important because 300 MiB scan forces reclaim to reach the
> pages during the first pass, which clears their accessed bits and
> evicts some of them. The second pass then gives the scan pages a
> second visible access. The reproducer from my first email also reads
> the cold data twice (--loops=2).
>
> So the idea is to get the cold data promoted to newer generations
> and watch whether the workload recovers while the oldest generation
> is thrashing.
>
> With a 300 MiB scan and 2 passes, neither MGLRU nor MGLRU-FG5 can
> recover. FG5 handles the 96 MiB scans and the 300 MiB single pass
> scan.
>
> The Patch
> ---------
>
> Kairui,

Hi Maxim, thanks for looking into this.

I reproduced your cases on latest FG (7.4.0-rc3, 1 GiB
memcg, x86-64). Base FG fails 4/5 seeds: early p50 280-344 ms,
late p50 364-375 ms, .vec 9-19 MiB, scan 288 MiB resident.

Refautl ditance below seems fix the problem just fine as we
expected.

>
> On Mon, Nov 02, 2026 at 02:02:33AM +0800, Kairui Song wrote:
> > This code snip:
> > + /* Set refault as active. */
> > + if (distance < total / 2) {
> > + folio_set_active(folio);
> [...]
> > That way, even a random folio will be put to the head directly instead
> > of keep thrasing on tail. And placing on head or newer gen will
> > trigger aging.
>
> As I understand it, the refault distance series will break this loop
> on tail. However, will the loop survive when the hot random set
> is much bigger than half of memory? E.g. when its refault
> distances exceed total / 2 and refaults land in the oldest
> generation again? Should we in this case use the aging logic from
> this patch as a fallback?

Yes, that's expected when the hot random set is much larger than memory:
`distance >= tail` keeps part of it in the oldest generation, and that is
intentional (MRU-like). It's not part of your case, but it is a real future
case.

> Anyway I'd be happy to test the refault distance implementation as
> part of this work when it's ready.
>
> I also rebased the patch onto mm-new (25fee5b1762b) [2] and on top
> of the MGLRU-FG5 branch [3]. The patch applies unchanged to both.
>
> Please, let me know if I need to send v2 of the patch to this thread.

Let's wait a bit: the per-generation counter could trigger unnecessary aging.
If the active working set is well protected in the newer generations,
constant eviction of rarely used folios should not trigger aging; otherwise
it may push the working set out of memory and cause a regression.

> The full results are in the gist [5].

Thanks! I also tested the refault distance design below, and it looks good:

distance = lru_distance(lruvec, token, LRU_GEN_EVICT_BITS,
lru_gen_bucket_order[type]);
tail = lru_gen_tail_size(lruvec, type); /* oldest MIN_NR_GENS */

if (distance < tail) {
/* It would still be resident if it had been kept. */
folio_set_active(folio);
__folio_set_lru_refs(folio, min(refs, LRU_REFS_PROTECTED));
}

I also swept the promotion threshold: tail, total/2, and total/4 all fix
this case; gating on refs does not (refs are 0 for MADV_RANDOM).

Results (300 MiB / 2 passes, 1 GiB memcg; base/RD 5 seeds, aging patch 2):

early p50 late p50 .vec late
Base FG 280-344 ms 364-375 ms 9-19 MiB
FG + RD 5.1-5.4 ms 2.7-2.9 ms 117.2 MiB
Your patch 7-51 ms 2.9-4.3 ms 117.2 MiB

And your tests covered the other parts too, so I think FG + RD is the right
approach. I'll keep this case covered and Cc you on update.