Re: [PATCH RFC v3 15/17] mm/khugepaged: check folio referenced state via LRU refs under MGLRU
From: Zi Yan
Date: Sat Oct 03 2026 - 22:11:56 EST
On Sat Oct 3, 2026 at 8:55 AM EDT, 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 a higher refs count no longer have the bit set.
> folio_pte_referenced() testing the raw bit misses hot folios at
> refs >= 2, 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.
Why not reuse the smaps helper function? You can rename the helper to
check_folio_referenced() for general use.
>
> 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 913086eaf17b..be18381c6efd 100644
> --- a/mm/khugepaged.c
> +++ b/mm/khugepaged.c
> @@ -586,7 +586,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 (lru_gen_enabled() ? folio_lru_refs(folio) : folio_test_referenced(folio))
> return true;
> /* ... or the PTE mapping was recently used */
> return pte_young(pteval) || mmu_notifier_test_young(vma->vm_mm, addr);
--
Best Regards,
Yan, Zi