Re: [PATCH v6 13/18] mm/khugepaged: convert to LRU refs based operations for MGLRU
From: Kairui Song
Date: Wed Nov 25 2026 - 08:52:13 EST
On Wed, Nov 25, 2026 at 6:11 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 MGLRU, PG_referenced is only the lowest bit of the folio LRU refs
> > count, so folios with nonzero even refs no longer have the bit set.
> > folio_pte_referenced() testing the raw bit misses hot folios at
> > refs 2, 4 or 6, under-counting the referenced folios of a candidate range
> > and aborting otherwise good collapses with SCAN_LACK_REFERENCED_PAGE.
> >
> > Test the refs count directly when MGLRU is enabled, mirroring the
> > smaps conversion. The classical LRU keeps the plain bit test.
> >
> > Tested-by: Zhao Zhenzhuo <zhaozhengzhuo@xxxxxxxxxxxxx>
> > Reviewed-by: Kunwu Chan <kunwu.chan@xxxxxxxxx>
> > Tested-by: Andrew Morton <akpm@xxxxxxxxxxxxxxxxxxxx>
> > Signed-off-by: Kairui Song <kasong@xxxxxxxxxxx>
> > ---
> > mm/khugepaged.c | 4 +++-
> > 1 file changed, 3 insertions(+), 1 deletion(-)
> >
> > diff --git a/mm/khugepaged.c b/mm/khugepaged.c
> > index 2dc4245814df..5e768481ac22 100644
> > --- a/mm/khugepaged.c
> > +++ b/mm/khugepaged.c
> > @@ -579,7 +579,9 @@ static bool folio_pte_referenced(struct folio *folio,
> > struct vm_area_struct *vma, unsigned long addr, pte_t pteval)
> > {
> > /* The folio was referenced previously ... */
> > - if (folio_test_young(folio) || folio_test_referenced(folio))
> > + if (folio_test_young(folio))
> > + return true;
> > + if (folio_check_lru_refs(folio))
> > return true;
>
> Why separate lines?
I thought separating them would make the git log easier to track if we
changed one of them again in the future. I can change it back if it's
not helpful.
> Also, is there some way to abstract this that core-mm has to worry less about
> the details?
Let's just move folio_test_young into folio_check_lru_refs() then,
PG_young is page idles's hint for LRU, so doing that makes sense.
And with FG, I think we will soon be able to retire PG_young
completely, page idle's feed back can just call
folio_inc_lru_refs(folio, LRU_REF_MAPPED), save one page flag. But
can't do that yet, need to cover the CLRU case.