[PATCH v7 11/19] mm/mglru, smaps: convert to LRU refs based operations for MGLRU

From: Kairui Song via B4 Relay

Date: Tue Dec 01 2026 - 15:20:04 EST


From: Kairui Song <kasong@xxxxxxxxxxx>

PG_referenced is only the lowest bit of the folio LRU refs count
(LRU_REFS_FLAGS) now, and MGLRU never kept it stable: promotion used to
clear it, and now it toggles as the count grows. But with the LRU
referenced count as a stable API now, we can use two helpers to provide
in-kernel users a stable and sane reading of the referenced status of
an LRU managed folio.

Add folio_test_lru_refs() and folio_clear_lru_refs(), which cover the
PG_young page-idle hint and use the refs count when MGLRU is enabled and
PG_referenced otherwise, and switch the smaps paths over to them.

Convert smaps as the first user of them. MGLRU now reports every folio
with a nonzero count, so the reading no longer flickers and clearing no
longer results in an ambiguous status.

The classical LRU case is unchanged, and its internals keep using the
raw PG_referenced helpers.

Reviewed-by: Kunwu Chan <kunwu.chan@xxxxxxxxx>
Tested-by: Andrew Morton <akpm@xxxxxxxxxxxxxxxxxxxx>
Acked-by: Zi Yan <ziy@xxxxxxxxxx>
Signed-off-by: Kairui Song <kasong@xxxxxxxxxxx>
---
fs/proc/task_mmu.c | 8 +++-----
include/linux/mm_inline.h | 35 +++++++++++++++++++++++++++++++++++
2 files changed, 38 insertions(+), 5 deletions(-)

diff --git a/fs/proc/task_mmu.c b/fs/proc/task_mmu.c
index 0d1c941790b0..44a347bc65d2 100644
--- a/fs/proc/task_mmu.c
+++ b/fs/proc/task_mmu.c
@@ -890,7 +890,7 @@ static void smaps_account(struct mem_size_stats *mss, struct page *page,

mss->resident += size;
/* Accumulate the size in pages that have been accessed. */
- if (young || folio_test_young(folio) || folio_test_referenced(folio))
+ if (young || folio_test_lru_refs(folio))
mss->referenced += size;

/*
@@ -1693,8 +1693,7 @@ static int clear_refs_pte_range(pmd_t *pmd, unsigned long addr,

/* Clear accessed and referenced bits. */
pmdp_test_and_clear_young(vma, addr, pmd);
- folio_test_clear_young(folio);
- folio_clear_referenced(folio);
+ folio_clear_lru_refs(folio);
out:
spin_unlock(ptl);
return 0;
@@ -1722,8 +1721,7 @@ static int clear_refs_pte_range(pmd_t *pmd, unsigned long addr,

/* Clear accessed and referenced bits. */
ptep_test_and_clear_young(vma, addr, pte);
- folio_test_clear_young(folio);
- folio_clear_referenced(folio);
+ folio_clear_lru_refs(folio);
}
pte_unmap_unlock(pte - 1, ptl);
cond_resched();
diff --git a/include/linux/mm_inline.h b/include/linux/mm_inline.h
index dde3fc6e7c0c..d3f0c29ac340 100644
--- a/include/linux/mm_inline.h
+++ b/include/linux/mm_inline.h
@@ -9,6 +9,7 @@
#include <linux/string.h>
#include <linux/userfaultfd_k.h>
#include <linux/leafops.h>
+#include <linux/page_idle.h>

/**
* folio_is_file_lru - Should the folio be on a file LRU or anon LRU?
@@ -524,6 +525,40 @@ static __always_inline void folio_inc_lru_refs_fast(struct folio *folio)
} while (!try_cmpxchg(folio_flags(folio, 0), &old_flags, new_flags));
}

+/**
+ * folio_test_lru_refs - Test if an LRU managed folio has been referenced.
+ * @folio: The folio to test.
+ *
+ * Returns true if the MGLRU refs count is nonzero, or PG_referenced is set,
+ * which is also correct for the non-MGLRU case, or if PG_young is set. Kept
+ * as a separate helper, to avoid splattering the lru_gen_enabled() static key.
+ */
+static inline bool folio_test_lru_refs(const struct folio *folio)
+{
+ if (folio_test_young(folio))
+ return true;
+ if (lru_gen_enabled())
+ return folio_lru_refs(folio) > 0;
+ return folio_test_referenced(folio);
+}
+
+/**
+ * folio_clear_lru_refs - Clear the referenced state of an LRU managed folio.
+ * @folio: The folio to clear.
+ *
+ * Reset the MGLRU refs count to zero, or clear PG_referenced, leaving
+ * PG_workingset alone for the non-MGLRU case, and clear PG_young. Kept as a
+ * separate helper just like folio_test_lru_refs().
+ */
+static inline void folio_clear_lru_refs(struct folio *folio)
+{
+ folio_test_clear_young(folio);
+ if (lru_gen_enabled())
+ folio_set_lru_refs(folio, 0);
+ else
+ folio_clear_referenced(folio);
+}
+
static __always_inline
void lruvec_add_folio(struct lruvec *lruvec, struct folio *folio)
{

--
2.56.1