Re: [PATCH v6 14/18] mm/madvise: adapt for LRU refs based operations for MGLRU

From: Kairui Song

Date: Tue Dec 01 2026 - 13:29:04 EST


On Wed, Nov 25, 2026 at 6:10 PM David Hildenbrand (Arm)
<david@xxxxxxxxxx> wrote:
>
> On 11/23/26 19:53, Kairui Song via B4 Relay wrote:
> > From: Kairui Song <kasong@xxxxxxxxxxx>
> >
> > For the active/inactive LRU, madvise clears PG_referenced so that one more
> > access is not enough to reactivate a folio, and keeps PG_workingset on a
> > folio demoted out of the active list so its refault is still accounted as
> > a workingset refault (PSI).
> >
> > MGLRU keeps that history in the folio's refs count instead, which now
> > lives in PG_referenced, PG_workingset and LRU_REFS_MASK, so writing to
> > those bits directly corrupts the count. The two hints want different
> > things:
> >
> > - MADV_COLD resets the count in folio_deactivate(), which also requests
> > deactivation.
> > - MADV_PAGEOUT clears non-workingset refs and caps the rest at
> > LRU_REFS_WORKINGSET after isolation, before handing the folio to
> > reclaim_pages(). The hint marks the folio as cold, so the
> > eviction shadow records the capped count and does not inflate the
> > eviction/refault counters of the hot tiers that drive aging, while
> > folios at or above the workingset threshold keep their PSI
> > classification.
> >
> > PG_young is cleared for both LRUs as before, so the referenced walk does
> > not count stale history as a recent access.
> >
> > Reviewed-by: Kunwu Chan <kunwu.chan@xxxxxxxxx>
> > Tested-by: Andrew Morton <akpm@xxxxxxxxxxxxxxxxxxxx>
> > Signed-off-by: Kairui Song <kasong@xxxxxxxxxxx>
> > ---
> > mm/madvise.c | 54 ++++++++++++++++++++++++++++++++++++++++--------------
> > 1 file changed, 40 insertions(+), 14 deletions(-)
> >
> > diff --git a/mm/madvise.c b/mm/madvise.c
> > index a36cfe98dd81..7e562e78d52a 100644
> > --- a/mm/madvise.c
> > +++ b/mm/madvise.c
> > @@ -360,6 +360,42 @@ static inline int madvise_folio_pte_batch(unsigned long addr, unsigned long end,
> > FPB_MERGE_YOUNG_DIRTY);
> > }
> >
> > +static void madvise_cold_prep_folio(struct folio *folio)
> > +{
> > + /*
> > + * VM couldn't reclaim the folio unless we clear PG_young.
> > + * As a side effect, idle-page tracking loses recent referenced history.
> > + */
> > + folio_test_clear_young(folio);
> > +
> > + /*
> > + * For the active/inactive LRU, a folio demoted out of the active
> > + * list should have PG_workingset so its refault is still accounted
> > + * as a workingset refault, and the referenced bit always needs to
> > + * be cleared even for inactive folios. For MGLRU, MADV_COLD clears
> > + * refs in folio_deactivate(); MADV_PAGEOUT handles refs after isolation.
> > + */
> > + if (!lru_gen_enabled()) {
> > + folio_clear_lru_refs(folio);
> > + if (folio_test_active(folio))
> > + folio_set_workingset(folio);
> > + }
> > +}
>
> These are all not things that madvise.c should have to worry about. Can we have
> some call into folio/reclaim code that will take care of this?

I tried many times on this and found it's increadibly hard to do so
without changing the behavior of CLRU globally, as the whole if
(folio_test_active(folio)) then folio_set_workingset(folio) here is
actually a "workaround" for CLRU, and we have two LRU...

I managed to reduce the code left in madvise here a little bit,
though. I think we can clean this part further, later. It will be part
of the work to better seperate CLRU with MGLRU, or making MGLRU the
default one.