Re: [PATCH v5 06/17] mm/mglru: frequency guided workingset promotion (MGLRU-FG)
From: KunWu Chan
Date: Fri Nov 20 2026 - 11:21:04 EST
On Fri, Nov 20, 2026 at 5:36 PM Kairui Song <ryncsn@xxxxxxxxx> wrote:
>
> On Fri, Nov 20, 2026 at 4:09 PM KunWu Chan <kunwu.chan@xxxxxxxxx> wrote:
> > > +/**
> > > + * folio_inc_lru_refs - Update the folio's LRU refs and lazily promote it.
> > > + * @folio: The folio being accessed.
> > > + * @flags: The type of ref: FD access, or mapped access optionally with
> > > + * executable access.
> > > + *
> > > + * Used by both cache access (flags == 0) and page table access (flags ==
> > > + * LRU_REF_MAPPED, optionally with LRU_REF_EXEC).
> > > + * Context: The caller must keep the folio alive by a reference or the page
> > > + * table lock of a mapped folio.
> > > + */
> > > +void folio_inc_lru_refs(struct folio *folio, unsigned int flags)
> > > +{
> > > + int max_gen;
> > > + int type, refs, old_gen, gen;
> > > + unsigned long new_flags, old_flags, max_seq;
> > > + struct lru_gen_folio *lrugen;
> > > + struct lruvec *lruvec = NULL;
> > > +
> > > + type = folio_is_file_lru(folio);
> > > + old_flags = READ_ONCE(*folio_flags(folio, 0));
> > > + do {
> > > + new_flags = old_flags;
> > > + old_gen = lru_get_gen_flags(old_flags);
> > > + refs = lru_get_refs_flags(old_flags) + 1;
> > > + gen = old_gen;
> > > + if (old_gen < 0)
> > > + goto out;
> > > + /*
> > > + * Lock the lruvec if the folio is on-list. We are already
> > > + * doing lazy promotion so in theory we don't need this,
> > > + * but for now, concurrent aging would still corrupt the
> > > + * size counters. This is a temporary limitation and
> > > + * will be lifted very soon, so the lock here is not a
> > > + * performance concern.
> > > + */
> > > + if (!lruvec) {
> > > + lruvec = lruvec_live_lock_irq(folio_lruvec(folio));
> > > + lrugen = &lruvec->lrugen;
> > > }
> >
> > Can we do the lookup under RCU and preserve IRQ state here?
> > filemap_read() can reach folio_lruvec() without RCU held. On non-RT,
> > lruvec_unlock_irq() can re-enable IRQs while KVM's hva_to_pfn_retry()
> > still holds gpc->lock.
>
> Thanks for the careful review!
>
> We can, but not in this commit, and in a later patch we did convert to
> RCU convention, so this lock is just an intermediate state because it
> may race with aging hence race with the active / inactive number
> update. In a later commit we changed how inactive/active is reported
> for MGLRU so this is no longer needed. We are basically lockless after
> this whole series (I mean, RCU lock only).
>
> Perhaps I should add a TODO in the comment here so reviewers won't got confused.
Thanks, Kairui. Keeping the temporary lock for the accounting
makes sense to me.
Kunwu