Re: [PATCH v5 17/17] mm/mglru: make folio_test_workingset() work based on folio LRU refs
From: KunWu Chan
Date: Sun Nov 22 2026 - 20:30:47 EST
On Sun, Nov 22, 2026 at 4:13 AM Kairui Song <ryncsn@xxxxxxxxx> wrote:
>
> On Fri, Nov 20, 2026 at 05:53:36PM +0800, KunWu Chan wrote:
> > On Thu, Nov 19, 2026 at 5:04 AM Kairui Song via B4 Relay
> > <devnull+kasong.tencent.com@xxxxxxxxxx> wrote:
> > >
> > > From: Kairui Song <kasong@xxxxxxxxxxx>
> > >
> > > folio_test_workingset() currently tests the PG_workingset bit, which
> > > is only the second lowest access bit of the folio LRU refs count now.
> > > Folios with refs 4 or 5 carry it in the LRU_REFS_MASK field
> > > and no longer have PG_workingset set, so the bit test misses them.
> > >
> > > folio_set_workingset() currently sets the PG_workingset bit with a
> > > plain set_bit(), which also over-promotes folios at a higher refs
> > > count: e.g. a folio at refs 4 is bumped to refs 6, advancing it
> > > toward LRU_REFS_MAX and an earlier generation promotion.
> > >
> > > Move the test into mm_inline.h as an inline helper based on
> > > folio_lru_refs(), checking refs >= LRU_REFS_WORKINGSET. The set
> > > stays a plain PG_workingset bit operation reserved for the
> > > classical LRU: under MGLRU the refs count is maintained by
> > > folio_inc_lru_refs(), and now triggers a debug WARN when called
> > > while MGLRU is fully on (the switching window is exempt, as the
> > > classical paths legitimately run alongside MGLRU then). The one
> > > PageWorkingset() user in erofs is converted to the folio helper.
> > >
> > > Under the classical LRU the refs count is not maintained, so the
> > > test is equivalent to the PG_workingset bit test, which is the old
> > > behavior.
> > >
> > > Tested-by: Zhao Zhenzhuo <zhaozhengzhuo@xxxxxxxxxxxxx>
> > > Signed-off-by: Kairui Song <kasong@xxxxxxxxxxx>
> > > ---
>
> ...
>
> > > +/*
> > > + * For the classical LRU only: with MGLRU the PG_workingset bit is
> > > + * part of the folio refs count maintained by folio_inc_lru_refs(),
> > > + * and a raw set would corrupt it. The switching window is exempt
> > > + * because the classical paths legitimately run alongside MGLRU then.
> > > + */
> > > +static __always_inline void folio_set_workingset(struct folio *folio)
> > > +{
> > > + VM_WARN_ON_ONCE(lru_gen_enabled() && !lru_gen_switching());
> >
> > Could shrink_active_list() still hit this WARN after the switch
> > to MGLRU completes? It can reschedule after isolating folios and
> > reach this setter after lru_switch is cleared.
>
> Hi Kunwu, thanks for the very careful review!
>
> Right I thought a lot about it after your reply, first your concern
> is right, thanks! I should just remove and replace this WARN with
> something else.
>
> And in theory, we should only call this for isolated folio (for CLRU
> it means a folio moving out of active workingset, isolation is always
> the first step), so this helper is fine - except, for the MADV cold
> case, it will call it on for a on-list folio, and will could skew
> the counter and if it raced with switching. Switching is so painful
> I hope we can get rid of it soon :/
>
> But, good thing is it's easy to fix, we just need (part of this belong
> to another patch, a trimmed diff just for reference):
>
> diff --git a/include/linux/mm_inline.h b/include/linux/mm_inline.h
> index 4f65867446dc..c936d1fadfbd 100644
> --- a/include/linux/mm_inline.h
> +++ b/include/linux/mm_inline.h
> @@ -261,8 +262,9 @@ static inline void __folio_set_lru_refs(struct folio *folio, unsigned int refs)
>
> do {
> new_flags = old_flags;
> - VM_WARN_ON_ONCE(lru_get_gen_flags(old_flags) != -1);
> lru_set_refs_flags(&new_flags, refs);
> + if (lru_get_gen_flags(old_flags) != -1)
> + break;
> if (new_flags == old_flags)
> break;
> } while (!try_cmpxchg(folio_flags(folio, 0), &old_flags, new_flags));
> @@ -569,16 +571,16 @@ static inline void folio_clear_lru_refs(struct folio *folio)
> */
> static __always_inline void folio_set_workingset(struct folio *folio)
> {
> - VM_WARN_ON_ONCE(lru_gen_enabled() && !lru_gen_switching());
> - set_bit(PG_workingset, folio_flags(folio, FOLIO_HEAD_PAGE));
> + unsigned long flags;
> +
> + flags = set_mask_bits(folio_flags(folio, 0), BIT(PG_workingset),
> + BIT(PG_workingset));
> + VM_WARN_ON_ONCE(lru_get_gen_flags(flags) != -1);
> }
>
> static __always_inline
> diff --git a/mm/madvise.c b/mm/madvise.c
> index bbbb729ed647..a73d89a2ecca 100644
> --- a/mm/madvise.c
> +++ b/mm/madvise.c
> @@ -374,11 +374,12 @@ static void madvise_cold_prep_folio(struct folio *folio)
> * 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);
> + __folio_set_lru_refs(folio, LRU_REFS_WORKINGSET);
> }
> }
Thanks, Kairui. That covers the shrink_active_list() case I was
worried about. With these changes folded into v5, feel free to
add my tag to 17/17.
Reviewed-by: Kunwu Chan <kunwu.chan@xxxxxxxxx>