Re: [PATCH v6 10/18] mm/mglru, gup: mark folios referenced via a fast helper

From: Vernon Yang

Date: Wed Nov 25 2026 - 08:47:45 EST


On Tue, Nov 24, 2026 at 02:53:02AM +0800, Kairui Song via B4 Relay wrote:
> From: Kairui Song <kasong@xxxxxxxxxxx>
>
> Introduce folio_inc_lru_refs_fast(): raise refs from 0 to
> LRU_REFS_REFERENCED via cmpxchg, or just set PG_referenced for the
> classical LRU as before. No counter update or promotion is needed,
> nonzero MGLRU refs are left unchanged.
>
> The classical path is taken for a folio that is off the gen lists while
> MGLRU is off, not on lru_gen_enabled() alone: while MGLRU is being
> turned off, a folio can still be on a gen list after the static key
> flips, and setting PG_referenced there would bump its refs count.
>
> That check uses lru_get_gen_flags(), so move the helper out of the
> CONFIG_LRU_GEN block, it is now built with the option off, where
> LRU_GEN_WIDTH is 0 and LRU_GEN_MASK is zero. Keep the decoded value in
> an int so the subtraction of the offset-by-one encoding cannot fold to
> ULONG_MAX before it happens to avoid -Woverflow error with CONFIG_WERROR.
>
> This helper only aims to be fast, it exists so the gup fast path can
> stay a fast path. The new helper doesn't account for refs above
> LRU_REFS_REFERENCED but that's fine for now as GUP never used that
> or anything similar for CLRU either.
>
> Convert the gup fast-path sites. No behavior change for classical LRU,
> no regression for MGLRU either. Just prepare for the following changes
> by providing a helper that follows the new convention.
>
> Tested-by: Zhao Zhenzhuo <zhaozhengzhuo@xxxxxxxxxxxxx>
> Reviewed-by: Kunwu Chan <kunwu.chan@xxxxxxxxx>
> Signed-off-by: Kairui Song <kasong@xxxxxxxxxxx>
> ---
> include/linux/mm_inline.h | 62 +++++++++++++++++++++++++++++++++++------------
> mm/gup.c | 6 ++---
> 2 files changed, 49 insertions(+), 19 deletions(-)
>
> diff --git a/include/linux/mm_inline.h b/include/linux/mm_inline.h
> index b15c3618a1f2..13c8b6bce349 100644
> --- a/include/linux/mm_inline.h
> +++ b/include/linux/mm_inline.h
> @@ -105,6 +105,22 @@ static __always_inline enum lru_list folio_lru_list(const struct folio *folio)
> return lru;
> }
>
> +/**
> + * lru_get_gen_flags - Return the LRU generation number from folio flags.
> + * @flags: folio flags
> + *
> + * Returns: A number between 0 and (MAX_NR_GENS - 1), inclusive. Returns
> + * -1 if the flags indicate the folio is off the list (e.g., isolated).
> + */
> +static inline int lru_get_gen_flags(unsigned long flags)
> +{
> + int gen = (int)((flags & LRU_GEN_MASK) >> LRU_GEN_PGOFF);
> +
> + /* The gen is stored offset by one, so zero means off-list */
> + VM_WARN_ON_ONCE(gen > MAX_NR_GENS);
> + return gen - 1;
> +}
> +
> #ifdef CONFIG_LRU_GEN
>
> static inline bool lru_gen_switching(void)
> @@ -168,22 +184,6 @@ static inline void lru_set_gen_flags(unsigned long *flags, int gen)
> *flags |= (gen + 1UL) << LRU_GEN_PGOFF;
> }
>
> -/**
> - * lru_get_gen_flags - Return the LRU generation number from folio flags.
> - * @flags: folio flags
> - *
> - * Returns: A number between 0 and (MAX_NR_GENS - 1), inclusive. Returns
> - * -1 if the flags indicate the folio is off the list (e.g., isolated).
> - */
> -static inline int lru_get_gen_flags(unsigned long flags)
> -{
> - int gen = ((flags & LRU_GEN_MASK) >> LRU_GEN_PGOFF) - 1;
> -
> - /* Exclude the legal -1 from the unsigned MAX_NR_GENS comparison */
> - VM_WARN_ON_ONCE(gen != -1 && gen >= MAX_NR_GENS);
> - return gen;
> -}
> -
> static inline int folio_lru_gen(const struct folio *folio)
> {
> return lru_get_gen_flags(READ_ONCE(*const_folio_flags(folio, 0)));
> @@ -483,6 +483,36 @@ static inline bool lru_gen_del_folio(struct lruvec *lruvec, struct folio *folio,
> }
> #endif /* CONFIG_LRU_GEN */
>
> +/**
> + * folio_inc_lru_refs_fast - Bump folio refs count without promotion.
> + * @folio: the folio
> + *
> + * Raise refs from 0 to LRU_REFS_REFERENCED and leave hotter folios
> + * untouched. For a folio off the gen lists with MGLRU off, a plain
> + * PG_referenced set.
> + */
> +static __always_inline void folio_inc_lru_refs_fast(struct folio *folio)
> +{
> + unsigned long new_flags, old_flags = READ_ONCE(*folio_flags(folio, 0));
> + unsigned int refs;
> +
> + do {
> + refs = lru_get_refs_flags(old_flags);
> + if (lru_get_gen_flags(old_flags) < 0 && !lru_gen_enabled()) {
> + /* For CLRU, just do folio_set_referenced() */
> + if (refs & LRU_REFS_REFERENCED)
> + break;
> + refs |= LRU_REFS_REFERENCED;
> + } else {
> + if (refs)
> + break;
> + refs = LRU_REFS_REFERENCED;
> + }
> + new_flags = old_flags;
> + lru_set_refs_flags(&new_flags, refs);
> + } while (!try_cmpxchg(folio_flags(folio, 0), &old_flags, new_flags));
> +}

The implementation code of this function looks a bit convoluted.
Would it be better to simplify it into the following code?

do {
bool clru = !lru_gen_enabled() && lru_get_gen_flags(old_flags) < 0;

refs = lru_get_refs_flags(old_flags);
if (clru ? (refs & LRU_REFS_REFERENCED) : refs)
break;
new_flags = old_flags;
lru_set_refs_flags(&new_flags, refs | LRU_REFS_REFERENCED);
} while (!try_cmpxchg(folio_flags(folio, 0), &old_flags, new_flags));

--
Cheers,
Vernon