Re: [PATCH] mm/mglru: deactivate anon folios into the oldest generation
From: Kairui Song
Date: Sun Nov 15 2026 - 22:18:23 EST
On Mon, Nov 16, 2026 at 9:48 AM Ridong Chen <ridong.chen@xxxxxxxxx> wrote:
> On 11/16/2026 8:28 AM, Barry Song wrote:
> > For example, something along these lines:
> >
> > lru_gen_deactivate_folio()
> > {
> > int type = folio_is_file_lru(folio);
> > int zone = folio_zonenum(folio);
> > int gen = lru_gen_from_seq(lrugen->min_seq[type]);
> >
> > if (folio_lru_gen(folio) < 0)
> > return false;
> >
> > folio_clear_active(folio);
> > folio_clear_referenced(folio);
> >
> > list_move(&folio->lru, &lrugen->folios[gen][type][zone]);
> > }
> >
>
> I think that works, and it is better than what this patch does. It
> puts both anon and file folios at the head of the oldest generation,
> which matches the classic LRU, and it avoids the two problems I
> mentioned (too aggressive for anon, and moving file folios from the
> head to the tail).
>
> A plain list_move() is not enough though. We also need to:
>
> - update the gen bits in folio->flags with a cmpxchg loop, as
> folio_inc_gen() does, since folio_update_gen() can change them
> locklessly from the page table walk;
> - fix up lrugen->nr_pages and the LRU sizes. lru_gen_update_size()
> warns on an active -> inactive move ("demotion requires
> isolation"), so this would be done as a deletion from old_gen
> followed by an addition to new_gen.
>
> min_seq only advances under the LRU lock, so it is stable here.
> Something like:
>
> static bool lru_gen_deactivate_folio(struct lruvec *lruvec,
> struct folio *folio)
> {
> int type = folio_is_file_lru(folio);
> int zone = folio_zonenum(folio);
> struct lru_gen_folio *lrugen = &lruvec->lrugen;
> int new_gen = lru_gen_from_seq(lrugen->min_seq[type]);
> unsigned long new_flags, old_flags = READ_ONCE(folio->flags.f);
> int old_gen;
>
> do {
> old_gen = ((old_flags & LRU_GEN_MASK) >> LRU_GEN_PGOFF) - 1;
> if (old_gen < 0)
> return false;
> new_flags = old_flags & ~LRU_GEN_MASK;
> new_flags |= (new_gen + 1UL) << LRU_GEN_PGOFF;
> } while (!try_cmpxchg(&folio->flags.f, &old_flags, new_flags));
>
> if (old_gen != new_gen) {
> lru_gen_update_size(lruvec, folio, old_gen, -1);
> lru_gen_update_size(lruvec, folio, -1, new_gen);
> }
> list_move(&folio->lru, &lrugen->folios[new_gen][type][zone]);
>
> return true;
> }
>
>
> > Right now, lru_deactivate() seems unnecessarily complicated,
> > incurring additional CPU overhead and LRU lock contention:
> >
> > 1. Remove the folio from the LRU list.
> > 2. Add the folio to the LRU cache.
> > 3. Drain the LRU cache.
> > 4. Add the folio back to the LRU list.
> >
> > Why do we need all these intermediate steps instead of moving
> > the folio directly in a single operation, given that we're
> > already manipulating the LRU list?
> >
> > Am I missing something?
> >
> I don't think the batch itself is the problem. The folio stays on the
> LRU while it sits in the per-cpu batch; only when the batch is
> drained do we take the LRU lock once for up to a batch of folios and
> do del + add under it. So the batch reduces lock contention rather
> than adding to it. What the in-place move saves is the del/add pair
> inside the lock.
>
> So I plan to keep the batching and, in lru_deactivate(), call
> lru_gen_deactivate_folio() instead of lruvec_del_folio() +
> lruvec_add_folio() when MGLRU is enabled. I'll test it and send v2
> if that sounds good to you.
Right, keeping the batch sounds like a good idea.
And I think we can make this cleaner if we build it on top of:
https://lore.kernel.org/linux-mm/CAMgjq7A1dWJ6DoF6+CxfjysxjzAChWrdk=z_JvtLMyFWdoYTyw@xxxxxxxxxxxxxx/
By having a "lru_gen_add_folio(lruvec, folio, min_seq, max_seq, tail)"
lru_gen_add_folio can cap the target window by the given [min_seq,
max_seq], -1 means no limitation. Avoid a lot of redundant code, and
we still have just one unifed place to add a folio into the gens
(except lazy promotion), I think.