| Seq | Timestamp | Type | Name | Duration |
|---|---|---|---|---|
| 0/0 | 2026/10/15 23:41 | flow | patch-triage |
2mArgs:null Results: {
"EnableConfigs": [
"LRU_GEN",
"LRU_GEN_ENABLED",
"MEMCG"
],
"FocusSymbols": [
"folio_inc_lru_refs",
"folio_reset_lru_refs",
"folio_inc_gen",
"inc_min_seq",
"inc_max_seq",
"evict_folios",
"sort_folio",
"lru_gen_refault",
"lru_gen_eviction",
"lru_gen_seq_write",
"madvise_cold_or_pageout_pte_range",
"clear_refs_pte_range"
],
"KMSANReasoning": "The patch series reworks the Multi-Gen LRU (MGLRU) referenced count and tiering tracking, storing the reference count across PG_referenced, PG_workingset, and LRU_REFS bits in folio-\u003eflags, while updating page reclamation, migration, madvise, and compaction paths.\n\nKMSAN vs KASAN Applicability Analysis:\n1. Uninitialized Memory / Info-Leaks (KMSAN):\n - The changes do not introduce or modify any structures or buffers copied to user space (e.g., via copy_to_user, put_user, netlink, or ioctl). User-facing interfaces like smaps and lru_gen_seq_show only read formatted integers.\n - All newly introduced and modified functions (such as folio_inc_lru_refs, folio_reset_lru_refs, folio_inc_lru_refs_walk, __folio_inc_gen) explicitly initialize all local variables before use.\n - The new field `nr_activated` in `struct lru_gen_mm_walk` is zeroed either via `kzalloc_obj` in `set_mm_walk` or as part of the zero-initialized `pgdat-\u003emm_walk`, and is asserted to be zero via `memchr_inv` in `clear_mm_walk`.\n - Eviction shadow packing/unpacking handles integer tokens and booleans without exposing uninitialized memory.\n\n2. Memory Safety and Concurrency (KASAN / Standard Detectors):\n - The primary risks introduced by this patch series involve lockless atomic cmpxchg operations on folio flags, memory ordering (smp_rmb/smp_store_release), list splicing under RCU / lru_lock, and LRU size accounting (`__update_lru_size`).\n - Any potential regressions (such as use-after-free, double-free, out-of-bounds access, race conditions, or locking issues) are covered by KASAN, LOCKDEP, and kernel page/VM debugging assertions (e.g., VM_WARN_ON_ONCE_FOLIO).\n\nBecause no uninitialized memory risks or kernel-to-user information leaks are introduced, a dedicated KMSAN fuzzing session is not warranted.",
"NeedsKMSAN": false,
"Reasoning": "The patch modifies core memory management code by overhauling the Multi-Gen LRU (MGLRU) referenced count tracking, workingset protection, and generation promotion algorithms. It introduces new lockless generation advancement (folio_inc_lru_refs, folio_reset_lru_refs), modifies tier classification, refactors eviction sorting (sort_folio, evict_folios), and updates refault handling. These changes execute directly in standard virtualized environments under memory reclaim, page faults, madvise(MADV_COLD/MADV_PAGEOUT), and MGLRU sysfs controls (/sys/kernel/mm/lru_gen).",
"WorthFuzzing": true
} |
| 1/1 | 2026/10/15 23:41 | action | read-patch-diff |
0mArgs:null Results: {
"PatchDiff": "commit 5e17e2951caa17b8a656b8370b8228f83a262680\nAuthor: syz-cluster \u003ctriage@syzkaller.com\u003e\nDate: Thu Oct 15 23:41:47 2026 +0000\n\n syz-cluster: applied patch under review\n\ndiff --git a/Documentation/admin-guide/mm/multigen_lru.rst b/Documentation/admin-guide/mm/multigen_lru.rst\nindex 9cb54b4ff5d9d..808d2e97b3f30 100644\n--- a/Documentation/admin-guide/mm/multigen_lru.rst\n+++ b/Documentation/admin-guide/mm/multigen_lru.rst\n@@ -149,8 +149,8 @@ generations less than or equal to ``min_gen_nr``.\n ``- memcg_id node_id min_gen_nr [swappiness [nr_to_reclaim]]``\n \n ``min_gen_nr`` should be less than ``max_gen_nr-1``, since\n-``max_gen_nr`` and ``max_gen_nr-1`` are not fully aged (equivalent to\n-the active list) and therefore cannot be evicted. ``swappiness``\n+``max_gen_nr`` and ``max_gen_nr-1`` are not fully aged and therefore\n+cannot be evicted. ``swappiness``\n overrides the default value in ``/proc/sys/vm/swappiness`` and the valid\n range is [0-200, max], with max being exclusively used for the reclamation\n of anonymous memory. ``nr_to_reclaim`` limits the number of pages to evict.\ndiff --git a/Documentation/mm/multigen_lru.rst b/Documentation/mm/multigen_lru.rst\nindex 52ed5092022f1..ec8744c1f42ca 100644\n--- a/Documentation/mm/multigen_lru.rst\n+++ b/Documentation/mm/multigen_lru.rst\n@@ -95,16 +95,19 @@ at most ``MAX_NR_GENS`` generations. The gen counter stores a value\n within ``[1, MAX_NR_GENS]`` while a page is on one of\n ``lrugen-\u003efolios[]``; otherwise it stores zero.\n \n-Each generation is divided into multiple tiers. A page accessed ``N``\n-times through file descriptors is in tier ``order_base_2(N)``. Unlike\n-generations, tiers do not have dedicated ``lrugen-\u003efolios[]``. In\n-contrast to moving across generations, which requires the LRU lock,\n-moving across tiers only involves atomic operations on\n-``folio-\u003eflags`` and therefore has a negligible cost. A feedback loop\n-modeled after the PID controller monitors refaults over all the tiers\n-from anon and file types and decides which tiers from which types to\n-evict or protect. The desired effect is to balance refault percentages\n-between anon and file types proportional to the swappiness level.\n+Each generation is divided into multiple tiers. A page's tier is\n+derived from its referenced count, which counts the accesses collected\n+from both channels above and is capped at ``LRU_REFS_MAX``: pages\n+accessed at most once are in the lowest tier, and the tiers above hold\n+pages accessed at least twice. Unlike generations, tiers do not have\n+dedicated ``lrugen-\u003efolios[]``. In contrast to moving across\n+generations, which requires the LRU lock, updating the referenced\n+count only involves atomic operations on ``folio-\u003eflags`` and\n+therefore has a negligible cost. A feedback loop modeled after the PID\n+controller monitors refaults over all the tiers from anon and file\n+types and decides which tiers from which types to evict or protect.\n+The desired effect is to balance refault percentages between anon and\n+file types proportional to the swappiness level.\n \n There are two conceptually independent procedures: the aging and the\n eviction. They form a closed-loop system, i.e., the page reclaim.\n@@ -122,25 +125,31 @@ and calls ``walk_page_range()`` with each ``mm_struct`` on this list\n to scan PTEs, and after each iteration, it increments ``max_seq``. For\n the latter, when the eviction walks the rmap and finds a young PTE,\n the aging scans the adjacent PTEs. For both, on finding a young PTE,\n-the aging clears the accessed bit and updates the gen counter of the\n-page mapped by this PTE to ``(max_seq%MAX_NR_GENS)+1``.\n+the aging clears the accessed bit and bumps the referenced count of\n+the page mapped by this PTE. A second access promotes the page by\n+updating its gen counter to ``(max_seq%MAX_NR_GENS)+1``; the first\n+access only moves a page one generation forward if it is at the tail\n+of its LRU, i.e., in one of the two oldest generations of its type,\n+so that the next time it becomes the oldest one, its accessed bit\n+reflects whether it has been used again. Executable file pages are\n+promoted on their first access, since reclaiming them causes typical\n+I/O thrashing.\n \n Eviction\n --------\n The eviction consumes old generations. Given an ``lruvec``, it\n increments ``min_seq`` when ``lrugen-\u003efolios[]`` indexed by\n-``min_seq%MAX_NR_GENS`` becomes empty. To select a type and a tier to\n-evict from, it first compares ``min_seq[]`` to select the older type.\n-If both types are equally old, it selects the one whose first tier has\n-a lower refault percentage. The first tier contains single-use\n-unmapped clean pages, which are the best bet. The eviction sorts a\n-page according to its gen counter if the aging has found this page\n-accessed through page tables and updated its gen counter. It also\n-moves a page to the next generation, i.e., ``min_seq+1``, if this page\n-was accessed multiple times through file descriptors and the feedback\n-loop has detected outlying refaults from the tier this page is in. To\n-this end, the feedback loop uses the first tier as the baseline, for\n-the reason stated earlier.\n+``min_seq%MAX_NR_GENS`` becomes empty. To select a type to evict from,\n+it first compares ``min_seq[]`` to select the older type. If both\n+types are equally old, it compares the refault percentages of all the\n+tiers of each type proportional to the swappiness level. To select a\n+tier to protect, it compares the refault percentage of each tier\n+against that of the tiers below it. The lowest tier contains pages\n+accessed at most once, which are the best bet. The eviction sorts a\n+page according to its gen counter if the aging or an access has\n+updated its gen counter. It also moves a page to the next generation,\n+i.e., ``min_seq+1``, if the feedback loop has detected outlying\n+refaults from the tier this page is in.\n \n Working set protection\n ----------------------\ndiff --git a/fs/btrfs/compression.c b/fs/btrfs/compression.c\nindex c62b5148d5ac1..57d24413265e3 100644\n--- a/fs/btrfs/compression.c\n+++ b/fs/btrfs/compression.c\n@@ -8,6 +8,7 @@\n #include \u003clinux/file.h\u003e\n #include \u003clinux/fs.h\u003e\n #include \u003clinux/pagemap.h\u003e\n+#include \u003clinux/mm_inline.h\u003e\n #include \u003clinux/folio_batch.h\u003e\n #include \u003clinux/highmem.h\u003e\n #include \u003clinux/kthread.h\u003e\ndiff --git a/fs/erofs/zdata.c b/fs/erofs/zdata.c\nindex e981e371d6c29..34ee41e1ccdc9 100644\n--- a/fs/erofs/zdata.c\n+++ b/fs/erofs/zdata.c\n@@ -6,6 +6,7 @@\n */\n #include \"compress.h\"\n #include \u003clinux/psi.h\u003e\n+#include \u003clinux/mm_inline.h\u003e\n #include \u003clinux/cpuhotplug.h\u003e\n #include \u003ctrace/events/erofs.h\u003e\n \n@@ -1713,7 +1714,7 @@ static void z_erofs_submit_queue(struct z_erofs_frontend *f,\n \t\t\t\tDBG_BUGON(bvec.bv_len \u003c sb-\u003es_blocksize);\n \t\t\t}\n \n-\t\t\tif (unlikely(PageWorkingset(bvec.bv_page)) \u0026\u0026\n+\t\t\tif (unlikely(folio_test_workingset(page_folio(bvec.bv_page))) \u0026\u0026\n \t\t\t !memstall) {\n \t\t\t\tpsi_memstall_enter(\u0026pflags);\n \t\t\t\tmemstall = 1;\ndiff --git a/fs/proc/task_mmu.c b/fs/proc/task_mmu.c\nindex 81c464139f8f8..55f3a35fd3aff 100644\n--- a/fs/proc/task_mmu.c\n+++ b/fs/proc/task_mmu.c\n@@ -888,7 +888,7 @@ static void smaps_account(struct mem_size_stats *mss, struct page *page,\n \n \tmss-\u003eresident += size;\n \t/* Accumulate the size in pages that have been accessed. */\n-\tif (young || folio_test_young(folio) || folio_test_referenced(folio))\n+\tif (young || folio_test_young(folio) || folio_check_lru_refs(folio))\n \t\tmss-\u003ereferenced += size;\n \n \t/*\n@@ -1685,7 +1685,7 @@ static int clear_refs_pte_range(pmd_t *pmd, unsigned long addr,\n \t\t/* Clear accessed and referenced bits. */\n \t\tpmdp_test_and_clear_young(vma, addr, pmd);\n \t\tfolio_test_clear_young(folio);\n-\t\tfolio_clear_referenced(folio);\n+\t\tfolio_clear_lru_refs(folio);\n out:\n \t\tspin_unlock(ptl);\n \t\treturn 0;\n@@ -1714,7 +1714,7 @@ static int clear_refs_pte_range(pmd_t *pmd, unsigned long addr,\n \t\t/* Clear accessed and referenced bits. */\n \t\tptep_test_and_clear_young(vma, addr, pte);\n \t\tfolio_test_clear_young(folio);\n-\t\tfolio_clear_referenced(folio);\n+\t\tfolio_clear_lru_refs(folio);\n \t}\n \tpte_unmap_unlock(pte - 1, ptl);\n \tcond_resched();\ndiff --git a/include/linux/memcontrol.h b/include/linux/memcontrol.h\nindex 74110a324f9e2..829ab776ab449 100644\n--- a/include/linux/memcontrol.h\n+++ b/include/linux/memcontrol.h\n@@ -1535,6 +1535,45 @@ static inline void lruvec_lock_irq(struct lruvec *lruvec)\n \tspin_lock_irq(\u0026lruvec-\u003elru_lock);\n }\n \n+/**\n+ * folio_lruvec_live_get - get a live lruvec for a folio under RCU\n+ * @folio: the folio\n+ *\n+ * Computes @folio's lruvec and walks up to the nearest live ancestor\n+ * if the folio's memcg is dying. Paired with folio_lruvec_live_put().\n+ * The result may be stale: RCU keeps it alive but does not pin @folio\n+ * to it. That is fine as the counters are fixed up on reparenting.\n+ *\n+ * Return: the live lruvec, with rcu_read_lock held.\n+ */\n+static inline struct lruvec *folio_lruvec_live_get(struct folio *folio)\n+{\n+#ifdef CONFIG_MEMCG\n+\tstruct lruvec *lruvec;\n+\tstruct pglist_data *pgdat;\n+\tstruct mem_cgroup *memcg;\n+\n+\trcu_read_lock();\n+\tlruvec = folio_lruvec(folio);\n+\tpgdat = lruvec_pgdat(lruvec);\n+\tmemcg = lruvec_memcg(lruvec);\n+\twhile (unlikely(memcg \u0026\u0026 css_is_dying(\u0026memcg-\u003ecss))) {\n+\t\tmemcg = parent_mem_cgroup(memcg);\n+\t\tlruvec = mem_cgroup_lruvec(memcg, pgdat);\n+\t}\n+\treturn lruvec;\n+#else\n+\treturn folio_lruvec(folio);\n+#endif\n+}\n+\n+static inline void folio_lruvec_live_put(struct lruvec *lruvec)\n+{\n+#ifdef CONFIG_MEMCG\n+\trcu_read_unlock();\n+#endif\n+}\n+\n static inline struct lruvec *lruvec_live_lock_irq(struct lruvec *lruvec)\n {\n #ifdef CONFIG_MEMCG\ndiff --git a/include/linux/mm_inline.h b/include/linux/mm_inline.h\nindex ab69b9930893f..ae50f9880da89 100644\n--- a/include/linux/mm_inline.h\n+++ b/include/linux/mm_inline.h\n@@ -47,11 +47,10 @@ static __always_inline void __update_lru_size(struct lruvec *lruvec,\n {\n \tstruct pglist_data *pgdat = lruvec_pgdat(lruvec);\n \n-\tlockdep_assert_held(\u0026lruvec-\u003elru_lock);\n \tWARN_ON_ONCE(nr_pages != (int)nr_pages);\n \n \tmod_lruvec_state(lruvec, NR_LRU_BASE + lru, nr_pages);\n-\t__mod_zone_page_state(\u0026pgdat-\u003enode_zones[zid],\n+\tmod_zone_page_state(\u0026pgdat-\u003enode_zones[zid],\n \t\t\t\tNR_ZONE_LRU_BASE + lru, nr_pages);\n }\n \n@@ -106,6 +105,22 @@ static __always_inline enum lru_list folio_lru_list(const struct folio *folio)\n \treturn lru;\n }\n \n+/**\n+ * lru_get_gen_flags - Return the LRU generation number from folio flags.\n+ * @flags: folio flags\n+ *\n+ * Returns: A number between 0 and (MAX_NR_GENS - 1), inclusive. Returns\n+ * -1 if the flags indicate the folio is off the list (e.g., isolated).\n+ */\n+static inline int lru_get_gen_flags(unsigned long flags)\n+{\n+\tint gen = ((flags \u0026 LRU_GEN_MASK) \u003e\u003e LRU_GEN_PGOFF) - 1;\n+\n+\t/* Exclude the legal -1 from the unsigned MAX_NR_GENS comparison */\n+\tVM_WARN_ON_ONCE(gen != -1 \u0026\u0026 gen \u003e= MAX_NR_GENS);\n+\treturn gen;\n+}\n+\n #ifdef CONFIG_LRU_GEN\n \n static inline bool lru_gen_switching(void)\n@@ -145,12 +160,13 @@ static inline int lru_hist_from_seq(unsigned long seq)\n \treturn seq % NR_HIST_GENS;\n }\n \n-static inline int lru_tier_from_refs(int refs, bool workingset)\n+static inline int lru_tier_from_refs(unsigned int refs)\n {\n-\tVM_WARN_ON_ONCE(refs \u003e BIT(LRU_REFS_WIDTH));\n-\n-\t/* see the comment on MAX_NR_TIERS */\n-\treturn workingset ? MAX_NR_TIERS - 1 : order_base_2(refs);\n+\tBUILD_BUG_ON(fls(LRU_REFS_MAX - 1) \u003e MAX_NR_TIERS - 1);\n+\tVM_WARN_ON_ONCE(refs \u003e LRU_REFS_MAX);\n+\tif (refs \u003c LRU_REFS_WORKINGSET)\n+\t\treturn 0;\n+\treturn fls(refs - 1);\n }\n \n /**\n@@ -167,42 +183,36 @@ static inline void lru_set_gen_flags(unsigned long *flags, int gen)\n \t*flags |= (gen + 1UL) \u003c\u003c LRU_GEN_PGOFF;\n }\n \n-/**\n- * lru_get_gen_flags - Return the LRU generation number from folio flags.\n- * @flags: folio flags\n- *\n- * Returns: A number between 0 and (MAX_NR_GENS - 1), inclusive. Returns\n- * -1 if the flags indicate the folio is off the list (e.g., isolated).\n- */\n-static inline int lru_get_gen_flags(unsigned long flags)\n+static inline int folio_lru_gen(const struct folio *folio)\n {\n-\tint gen = ((flags \u0026 LRU_GEN_MASK) \u003e\u003e LRU_GEN_PGOFF) - 1;\n-\n-\t/* Exclude the legal -1 from the unsigned MAX_NR_GENS comparison */\n-\tVM_WARN_ON_ONCE(gen != -1 \u0026\u0026 gen \u003e= MAX_NR_GENS);\n-\treturn gen;\n+\treturn lru_get_gen_flags(READ_ONCE(*const_folio_flags(folio, 0)));\n }\n+#endif\n \n /**\n * lru_set_refs_flags - Set the LRU referenced count to folio flags.\n * @flags: pointer to the folio flags\n * @refs: referenced / access count number, between 0 and LRU_REFS_MAX, inclusive.\n *\n- * For MGLRU, PG_referenced holds the first ref, and the extra bits hold the\n- * remaining refs. For classical LRU the extra bits are not used, so it can\n- * also be seen as the refs count never exceeds 1. In both cases, refs == 1\n- * means PG_referenced is set and the extra bits are zero, and refs == 0 means\n- * PG_referenced and the extra bits are all unset.\n+ * For MGLRU, PG_referenced, PG_workingset are used as the lower two bits of\n+ * refs counter, and extra bits hold the remaining higher bits. For classical\n+ * LRU the extra bits are not used, and the two flags has no direct\n+ * relationship with each other, but this helper can still be used to sync\n+ * them. For both cases, refs == 0 means these two flags and the extra bits\n+ * are all unset. And refs == 1 / 2 / 3 means PG_referenced and PG_workingset\n+ * are set in an bit order way, which is more meaningful for MGLRU though.\n */\n static inline void lru_set_refs_flags(unsigned long *flags, unsigned int refs)\n {\n \tVM_WARN_ON_ONCE(refs \u003e LRU_REFS_MAX);\n-\tBUILD_BUG_ON(LRU_REFS_MAX != (LRU_REFS_MASK \u003e\u003e LRU_REFS_PGOFF) + 1);\n-\n+\tBUILD_BUG_ON(LRU_REFS_MASK \u0026 (BIT(PG_referenced) | BIT(PG_workingset)));\n \t*flags \u0026= ~LRU_REFS_FLAGS;\n-\tif (!refs)\n-\t\treturn;\n-\t*flags |= (BIT(PG_referenced) | ((refs - 1UL) \u003c\u003c LRU_REFS_PGOFF));\n+\tif (refs \u0026 BIT(0))\n+\t\t*flags |= BIT(PG_referenced);\n+\tif (refs \u0026 BIT(1))\n+\t\t*flags |= BIT(PG_workingset);\n+\tif (LRU_REFS_WIDTH)\n+\t\t*flags |= ((unsigned long)refs \u003e\u003e 2) \u003c\u003c LRU_REFS_PGOFF;\n }\n \n /**\n@@ -213,13 +223,13 @@ static inline void lru_set_refs_flags(unsigned long *flags, unsigned int refs)\n */\n static inline int lru_get_refs_flags(unsigned long flags)\n {\n-\tif (!(flags \u0026 BIT(PG_referenced)))\n-\t\treturn 0;\n-\t/*\n-\t * Return the total number of accesses including PG_referenced. Also see\n-\t * the comment on LRU_REFS_FLAGS.\n-\t */\n-\treturn ((flags \u0026 LRU_REFS_MASK) \u003e\u003e LRU_REFS_PGOFF) + 1;\n+\tint refs;\n+\n+\t/* Return the total number of accesses. See the comment above MAX_NR_TIERS. */\n+\trefs = (flags \u0026 BIT(PG_referenced)) ? BIT(0) : 0;\n+\trefs += (flags \u0026 BIT(PG_workingset)) ? BIT(1) : 0;\n+\trefs += ((flags \u0026 LRU_REFS_MASK) \u003e\u003e LRU_REFS_PGOFF) \u003c\u003c 2;\n+\treturn refs;\n }\n \n static inline int folio_lru_refs(const struct folio *folio)\n@@ -227,38 +237,99 @@ static inline int folio_lru_refs(const struct folio *folio)\n \treturn lru_get_refs_flags(READ_ONCE(*const_folio_flags(folio, 0)));\n }\n \n-static inline void folio_set_lru_refs(struct folio *folio, unsigned int refs)\n+/**\n+ * __folio_set_lru_refs - Set a folio's LRU refs.\n+ * @folio: the folio\n+ * @refs: the new referenced count (0 .. LRU_REFS_MAX)\n+ *\n+ * Set the folio's LRU refs. The folio must be off the LRU list (e.g.,\n+ * isolated), or use folio_inc_lru_refs or folio_reset_lru_refs instead.\n+ */\n+static inline void __folio_set_lru_refs(struct folio *folio, unsigned int refs)\n {\n \tunsigned long new_flags, old_flags = READ_ONCE(*folio_flags(folio, 0));\n \n \tdo {\n \t\tnew_flags = old_flags;\n+\t\tVM_WARN_ON_ONCE(lru_get_gen_flags(old_flags) != -1);\n \t\tlru_set_refs_flags(\u0026new_flags, refs);\n \t} while (!try_cmpxchg(folio_flags(folio, 0), \u0026old_flags, new_flags));\n }\n \n-static inline int folio_lru_gen(const struct folio *folio)\n+#ifdef CONFIG_LRU_GEN\n+void folio_inc_lru_refs(struct folio *folio, unsigned int flags);\n+bool folio_reset_lru_refs(struct folio *folio);\n+#else\n+static inline void folio_inc_lru_refs(struct folio *folio, unsigned int flags)\n {\n-\treturn lru_get_gen_flags(READ_ONCE(*const_folio_flags(folio, 0)));\n+\t/* Should not be called with !CONFIG_LRU_GEN */\n+\tWARN_ON_ONCE(1);\n }\n \n-static inline bool lru_gen_is_active(const struct lruvec *lruvec, int gen)\n+static inline bool folio_reset_lru_refs(struct folio *folio)\n {\n-\tunsigned long max_seq = lruvec-\u003elrugen.max_seq;\n+\treturn false;\n+}\n+#endif\n \n-\tVM_WARN_ON_ONCE(gen \u003e= MAX_NR_GENS);\n+/**\n+ * folio_migrate_lru_refs - copy the reference state to a new folio\n+ * @new: the destination folio\n+ * @old: the source folio\n+ *\n+ * Transfer the reference state to @new during migration: the MGLRU\n+ * refs count, or PG_referenced and PG_workingset (bits 0-1 of the\n+ * encoding) for the active/inactive LRU.\n+ */\n+static inline void folio_migrate_lru_refs(struct folio *new, const struct folio *old)\n+{\n+\tVM_WARN_ON_ONCE_FOLIO(folio_test_lru(old), old);\n+\tVM_WARN_ON_ONCE_FOLIO(folio_test_lru(new), new);\n+\t__folio_set_lru_refs(new, folio_lru_refs(old));\n+}\n \n-\t/* see the comment on MIN_NR_GENS */\n-\treturn gen == lru_gen_from_seq(max_seq) || gen == lru_gen_from_seq(max_seq - 1);\n+/**\n+ * folio_test_workingset - Test if a folio is in the workingset.\n+ * @folio: the folio\n+ *\n+ * A folio is workingset when its LRU refs count reaches\n+ * LRU_REFS_WORKINGSET. Under the classical LRU the extra refs counter\n+ * are always zero so this is just testing the PG_workingset bit.\n+ * NOTE: folio_set_workingset() must not be used under MGLRU, as the\n+ * folio refs are tracked by folio_inc_lru_refs() with rules.\n+ *\n+ * Return: true if the folio is workingset.\n+ */\n+static __always_inline bool folio_test_workingset(const struct folio *folio)\n+{\n+\treturn folio_lru_refs(folio) \u003e= LRU_REFS_WORKINGSET;\n }\n \n-static inline void lru_gen_update_size(struct lruvec *lruvec, struct folio *folio,\n-\t\t\t\t int old_gen, int new_gen)\n+#ifdef CONFIG_LRU_GEN\n+static inline bool lru_refs_is_active(unsigned int refs)\n+{\n+\tVM_WARN_ON_ONCE(refs \u003e LRU_REFS_MAX);\n+\treturn refs \u003e= LRU_REFS_ACTIVATED;\n+}\n+\n+static inline bool lru_gen_at_tail(struct lruvec *lruvec, int type, int gen)\n+{\n+\tunsigned long min_seq = READ_ONCE(lruvec-\u003elrugen.min_seq[type]);\n+\n+\t/*\n+\t * Consider a folio is at the tail of its LRU when it is within\n+\t * MIN_NR_GENS of the oldest generation of its own type\n+\t */\n+\tBUILD_BUG_ON(MIN_NR_GENS != 2);\n+\treturn gen == lru_gen_from_seq(min_seq) ||\n+\t gen == lru_gen_from_seq(min_seq + 1);\n+}\n+\n+static inline void lru_gen_update_size(struct lruvec *lruvec, int type,\n+\t\tstruct folio *folio, int old_gen, int new_gen)\n {\n-\tint type = folio_is_file_lru(folio);\n \tint zone = folio_zonenum(folio);\n \tint delta = folio_nr_pages(folio);\n-\tenum lru_list lru = type * LRU_INACTIVE_FILE;\n \tstruct lru_gen_folio *lrugen = \u0026lruvec-\u003elrugen;\n \n \tVM_WARN_ON_ONCE(old_gen != -1 \u0026\u0026 old_gen \u003e= MAX_NR_GENS);\n@@ -266,36 +337,9 @@ static inline void lru_gen_update_size(struct lruvec *lruvec, struct folio *foli\n \tVM_WARN_ON_ONCE(old_gen == -1 \u0026\u0026 new_gen == -1);\n \n \tif (old_gen \u003e= 0)\n-\t\tWRITE_ONCE(lrugen-\u003enr_pages[old_gen][type][zone],\n-\t\t\t lrugen-\u003enr_pages[old_gen][type][zone] - delta);\n+\t\tatomic_long_sub(delta, \u0026lrugen-\u003enr_pages[old_gen][type][zone]);\n \tif (new_gen \u003e= 0)\n-\t\tWRITE_ONCE(lrugen-\u003enr_pages[new_gen][type][zone],\n-\t\t\t lrugen-\u003enr_pages[new_gen][type][zone] + delta);\n-\n-\t/* addition */\n-\tif (old_gen \u003c 0) {\n-\t\tif (lru_gen_is_active(lruvec, new_gen))\n-\t\t\tlru += LRU_ACTIVE;\n-\t\t__update_lru_size(lruvec, lru, zone, delta);\n-\t\treturn;\n-\t}\n-\n-\t/* deletion */\n-\tif (new_gen \u003c 0) {\n-\t\tif (lru_gen_is_active(lruvec, old_gen))\n-\t\t\tlru += LRU_ACTIVE;\n-\t\t__update_lru_size(lruvec, lru, zone, -delta);\n-\t\treturn;\n-\t}\n-\n-\t/* promotion */\n-\tif (!lru_gen_is_active(lruvec, old_gen) \u0026\u0026 lru_gen_is_active(lruvec, new_gen)) {\n-\t\t__update_lru_size(lruvec, lru, zone, -delta);\n-\t\t__update_lru_size(lruvec, lru + LRU_ACTIVE, zone, delta);\n-\t}\n-\n-\t/* demotion requires isolation, e.g., lru_deactivate_fn() */\n-\tVM_WARN_ON_ONCE(lru_gen_is_active(lruvec, old_gen) \u0026\u0026 !lru_gen_is_active(lruvec, new_gen));\n+\t\tatomic_long_add(delta, \u0026lrugen-\u003enr_pages[new_gen][type][zone]);\n }\n \n static inline unsigned long lru_gen_folio_seq(const struct lruvec *lruvec,\n@@ -303,23 +347,24 @@ static inline unsigned long lru_gen_folio_seq(const struct lruvec *lruvec,\n \t\t\t\t\t bool reclaiming)\n {\n \tint gen;\n+\tint refs = folio_lru_refs(folio);\n \tint type = folio_is_file_lru(folio);\n \tconst struct lru_gen_folio *lrugen = \u0026lruvec-\u003elrugen;\n \n \t/*\n-\t * +-----------------------------------+-----------------------------------+\n-\t * | Accessed through page tables and | Accessed through file descriptors |\n-\t * | promoted by folio_update_gen() | and protected by folio_inc_gen() |\n-\t * +-----------------------------------+-----------------------------------+\n-\t * | PG_active (set while isolated) | |\n-\t * +-----------------+-----------------+-----------------+-----------------+\n-\t * | PG_workingset | PG_referenced | PG_workingset | LRU_REFS_FLAGS |\n-\t * +-----------------------------------+-----------------------------------+\n-\t * |\u003c---------- MIN_NR_GENS ----------\u003e| |\n-\t * |\u003c---------------------------- MAX_NR_GENS ----------------------------\u003e|\n+\t * +------------------------------------------+------------------------------------------+\n+\t * | Accessed through page tables and | Accessed through file descriptors |\n+\t * | promoted by folio_inc_lru_refs_walk() | protected by folio_inc_lru_refs/inc_gen |\n+\t * +------------------------------------------+------------------------------------------+\n+\t * | PG_active (set at isolation or refault) | |\n+\t * +--------------------+---------------------+--------------------+---------------------+\n+\t * | LRU_REFS_MAX | LRU_REFS_WORKINGSET | LRU_REFS_MAX | LRU_REFS_WORKINGSET |\n+\t * +------------------------------------------+------------------------------------------+\n+\t * |\u003c-------------- MIN_NR_GENS -------------\u003e| |\n+\t * |\u003c----------------------------------- MAX_NR_GENS -----------------------------------\u003e|\n \t */\n \tif (folio_test_active(folio))\n-\t\tgen = MIN_NR_GENS - folio_test_workingset(folio);\n+\t\tgen = MIN_NR_GENS - (refs \u003e= LRU_REFS_WORKINGSET);\n \telse if (reclaiming)\n \t\tgen = MAX_NR_GENS;\n \telse if ((!folio_is_file_lru(folio) \u0026\u0026 !folio_test_swapcache(folio)) ||\n@@ -327,7 +372,7 @@ static inline unsigned long lru_gen_folio_seq(const struct lruvec *lruvec,\n \t\t (folio_test_dirty(folio) || folio_test_writeback(folio))))\n \t\tgen = MIN_NR_GENS;\n \telse\n-\t\tgen = MAX_NR_GENS - (folio_test_workingset(folio) || folio_test_referenced(folio));\n+\t\tgen = MAX_NR_GENS - (refs \u003e= LRU_REFS_WORKINGSET);\n \n \treturn max(READ_ONCE(lrugen-\u003emax_seq) - gen + 1, READ_ONCE(lrugen-\u003emin_seq[type]));\n }\n@@ -336,12 +381,15 @@ static inline bool lru_gen_add_folio(struct lruvec *lruvec, struct folio *folio,\n {\n \tunsigned long seq;\n \tunsigned long flags;\n-\tint gen = folio_lru_gen(folio);\n+\tint gen, refs;\n \tint type = folio_is_file_lru(folio);\n \tint zone = folio_zonenum(folio);\n+\tint delta = folio_nr_pages(folio);\n+\tenum lru_list lru = type * LRU_INACTIVE_FILE;\n \tstruct lru_gen_folio *lrugen = \u0026lruvec-\u003elrugen;\n \n-\tVM_WARN_ON_ONCE_FOLIO(gen != -1, folio);\n+\tBUILD_BUG_ON(BIT(LRU_GEN_WIDTH - 1) != MAX_NR_GENS);\n+\tVM_WARN_ON_ONCE_FOLIO(folio_lru_gen(folio) != -1, folio);\n \n \tif (folio_test_unevictable(folio) || !lrugen-\u003eenabled)\n \t\treturn false;\n@@ -350,9 +398,15 @@ static inline bool lru_gen_add_folio(struct lruvec *lruvec, struct folio *folio,\n \tgen = lru_gen_from_seq(seq);\n \tflags = (gen + 1UL) \u003c\u003c LRU_GEN_PGOFF;\n \t/* see the comment on MIN_NR_GENS about PG_active */\n-\tset_mask_bits(folio_flags(folio, 0), LRU_GEN_MASK | BIT(PG_active), flags);\n+\tflags = set_mask_bits(folio_flags(folio, 0), LRU_GEN_MASK | BIT(PG_active), flags);\n+\t/* use the refs from the atomic snapshot to avoid raced update */\n+\trefs = lru_get_refs_flags(flags);\n+\n+\tlru_gen_update_size(lruvec, type, folio, -1, gen);\n+\tif (lru_refs_is_active(refs))\n+\t\tlru += LRU_ACTIVE;\n+\t__update_lru_size(lruvec, lru, zone, delta);\n \n-\tlru_gen_update_size(lruvec, folio, -1, gen);\n \t/* for folio_rotate_reclaimable() */\n \tif (reclaiming)\n \t\tlist_add_tail(\u0026folio-\u003elru, \u0026lrugen-\u003efolios[gen][type][zone]);\n@@ -365,39 +419,38 @@ static inline bool lru_gen_add_folio(struct lruvec *lruvec, struct folio *folio,\n static inline bool lru_gen_del_folio(struct lruvec *lruvec, struct folio *folio, bool reclaiming)\n {\n \tunsigned long flags;\n-\tint gen = folio_lru_gen(folio);\n+\tint gen, refs;\n+\tint type = folio_is_file_lru(folio);\n+\tint zone = folio_zonenum(folio);\n+\tint delta = folio_nr_pages(folio);\n+\tenum lru_list lru = folio_is_file_lru(folio) * LRU_INACTIVE_FILE;\n+\tunsigned long max_seq = READ_ONCE(lruvec-\u003elrugen.max_seq);\n \n+\tflags = set_mask_bits(folio_flags(folio, 0), LRU_GEN_MASK, 0);\n+\tgen = lru_get_gen_flags(flags);\n+\trefs = lru_get_refs_flags(flags);\n \tif (gen \u003c 0)\n \t\treturn false;\n \n \tVM_WARN_ON_ONCE_FOLIO(folio_test_active(folio), folio);\n \tVM_WARN_ON_ONCE_FOLIO(folio_test_unevictable(folio), folio);\n \n-\t/* for folio_migrate_flags() */\n-\tflags = !reclaiming \u0026\u0026 lru_gen_is_active(lruvec, gen) ? BIT(PG_active) : 0;\n-\tflags = set_mask_bits(folio_flags(folio, 0), LRU_GEN_MASK, flags);\n-\tgen = ((flags \u0026 LRU_GEN_MASK) \u003e\u003e LRU_GEN_PGOFF) - 1;\n+\t/*\n+\t * See the comment in lru_gen_folio_seq. For migration, compaction,\n+\t * or any other isolation of a hot folio, try best to retain its gen\n+\t * info. Ideally we would keep the full gen info.\n+\t */\n+\tif (!reclaiming \u0026\u0026 ((max_seq - gen) % MAX_NR_GENS) \u003c MIN_NR_GENS)\n+\t\tfolio_set_active(folio);\n \n-\tlru_gen_update_size(lruvec, folio, gen, -1);\n+\tlru_gen_update_size(lruvec, type, folio, gen, -1);\n+\tif (lru_refs_is_active(refs))\n+\t\tlru += LRU_ACTIVE;\n+\t__update_lru_size(lruvec, lru, zone, -delta);\n \tlist_del(\u0026folio-\u003elru);\n \n \treturn true;\n }\n-\n-/**\n- * folio_migrate_lru_refs - copy the reference state to a new folio\n- * @new: the destination folio\n- * @old: the source folio\n- *\n- * Transfer the reference state to @new during migration: the MGLRU\n- * refs count, including PG_referenced, or just PG_referenced for the\n- * active/inactive LRU.\n- */\n-static inline void folio_migrate_lru_refs(struct folio *new, const struct folio *old)\n-{\n-\tBUILD_BUG_ON(LRU_REFS_MASK \u0026 BIT(PG_referenced));\n-\tfolio_set_lru_refs(new, folio_lru_refs(old));\n-}\n #else /* !CONFIG_LRU_GEN */\n \n static inline bool lru_gen_enabled(void)\n@@ -424,13 +477,76 @@ static inline bool lru_gen_del_folio(struct lruvec *lruvec, struct folio *folio,\n {\n \treturn false;\n }\n+#endif /* CONFIG_LRU_GEN */\n \n-static inline void folio_migrate_lru_refs(struct folio *new, const struct folio *old)\n+/**\n+ * folio_inc_lru_refs_fast - Bump folio refs count without promotion.\n+ * @folio: the folio\n+ *\n+ * Raise refs from 0 to LRU_REFS_REFERENCED and leave hotter folios\n+ * untouched. For the classical LRU, a plain PG_referenced set.\n+ */\n+static __always_inline void folio_inc_lru_refs_fast(struct folio *folio)\n {\n-\tif (folio_test_referenced(old))\n-\t\tfolio_set_referenced(new);\n+\tunsigned long new_flags, old_flags;\n+\n+\tif (!lru_gen_enabled()) {\n+\t\tfolio_set_referenced(folio);\n+\t\treturn;\n+\t}\n+\n+\tBUILD_BUG_ON(LRU_REFS_REFERENCED \u003e= LRU_REFS_ACTIVATED);\n+\told_flags = READ_ONCE(*folio_flags(folio, 0));\n+\tdo {\n+\t\tif (lru_get_refs_flags(old_flags))\n+\t\t\tbreak;\n+\t\tnew_flags = old_flags;\n+\t\tlru_set_refs_flags(\u0026new_flags, LRU_REFS_REFERENCED);\n+\t} while (!try_cmpxchg(folio_flags(folio, 0), \u0026old_flags, new_flags));\n+}\n+\n+/**\n+ * folio_check_lru_refs - Test if an LRU managed folio has been referenced.\n+ * @folio: The folio to test.\n+ *\n+ * Returns true if the MGLRU refs count is nonzero, or PG_referenced is set,\n+ * which is also correct for the non-MGLRU case. Kept as a separate helper,\n+ * to avoid splattering the lru_gen_enabled() static key.\n+ */\n+static inline bool folio_check_lru_refs(const struct folio *folio)\n+{\n+\tif (lru_gen_enabled())\n+\t\treturn folio_lru_refs(folio) \u003e 0;\n+\treturn folio_test_referenced(folio);\n+}\n+\n+/**\n+ * folio_clear_lru_refs - Clear the referenced state of an LRU managed folio.\n+ * @folio: The folio to clear.\n+ *\n+ * Reset the MGLRU refs count to zero, or clear PG_referenced, leaving\n+ * PG_workingset alone for the non-MGLRU case. Kept as a separate helper\n+ * just like folio_check_lru_refs().\n+ */\n+static inline void folio_clear_lru_refs(struct folio *folio)\n+{\n+\tif (lru_gen_enabled())\n+\t\tfolio_reset_lru_refs(folio);\n+\telse\n+\t\tfolio_clear_referenced(folio);\n+}\n+\n+/*\n+ * For the classical LRU only: with MGLRU the PG_workingset bit is\n+ * part of the folio refs count maintained by folio_inc_lru_refs(),\n+ * and a raw set would corrupt it. The switching window is exempt\n+ * because the classical paths legitimately run alongside MGLRU then.\n+ */\n+static __always_inline void folio_set_workingset(struct folio *folio)\n+{\n+\tVM_WARN_ON_ONCE(lru_gen_enabled() \u0026\u0026 !lru_gen_switching());\n+\tset_bit(PG_workingset, folio_flags(folio, FOLIO_HEAD_PAGE));\n }\n-#endif /* CONFIG_LRU_GEN */\n \n static __always_inline\n void lruvec_add_folio(struct lruvec *lruvec, struct folio *folio)\ndiff --git a/include/linux/mmzone.h b/include/linux/mmzone.h\nindex 3b96d6c7123b4..f50449d102423 100644\n--- a/include/linux/mmzone.h\n+++ b/include/linux/mmzone.h\n@@ -434,15 +434,15 @@ enum lruvec_flags {\n * least twice before handing this folio over to the eviction. The first check\n * clears the accessed bit from the initial fault; the second check makes sure\n * this folio hasn't been used since then. This process, AKA second chance,\n- * requires a minimum of two generations, hence MIN_NR_GENS. And to maintain ABI\n- * compatibility with the active/inactive LRU, e.g., /proc/vmstat, these two\n- * generations are considered active; the rest of generations, if they exist,\n- * are considered inactive. See lru_gen_is_active().\n+ * requires a minimum of two generations, hence MIN_NR_GENS.\n+ *\n+ * Active/inactive is per-folio based on the referenced count;\n+ * see the comment above MAX_NR_TIERS.\n *\n * PG_active is always cleared while a folio is on one of lrugen-\u003efolios[] so\n- * that the sliding window needs not to worry about it. And it's set again when\n- * a folio considered active is isolated for non-reclaiming purposes, e.g.,\n- * migration. See lru_gen_add_folio() and lru_gen_del_folio().\n+ * that the sliding window needs not to worry about it. It is set on a\n+ * fault-driven refault (see lru_gen_refault), or isolation (see\n+ * lru_gen_del_folio).\n *\n * MAX_NR_GENS is set to 4 so that the multi-gen LRU can support twice the\n * number of categories of the active/inactive LRU when keeping track of\n@@ -453,57 +453,128 @@ enum lruvec_flags {\n #define MAX_NR_GENS\t\t4U\n \n /*\n- * Each generation is divided into multiple tiers. A folio accessed N times\n- * through file descriptors is in tier order_base_2(N). A folio in the first\n- * tier (N=0,1) is marked by PG_referenced unless it was faulted in through page\n- * tables or read ahead. A folio in the last tier (MAX_NR_TIERS-1) is marked by\n- * PG_workingset. A folio in any other tier (1\u003cN\u003c5) between the first and last\n- * is marked by additional bits of LRU_REFS_WIDTH in folio-\u003eflags.\n+ * Each generation is divided into multiple tiers. A folio's referenced\n+ * count maps to a tier as shown below:\n *\n- * In contrast to moving across generations which requires the LRU lock, moving\n- * across tiers only involves atomic operations on folio-\u003eflags and therefore\n- * has a negligible cost in the buffered access path. In the eviction path,\n- * comparisons of refaulted/(evicted+protected) from the first tier and the rest\n- * infer whether folios accessed multiple times through file descriptors are\n- * statistically hot and thus worth protecting.\n+ * MGLRU (frequency guidance)\n+ * Refs Tier |- Refs: how many times (at least) a folio has been referenced.\n+ * 0 0 |- Mostly cold pages, readahead, etc. [1]\n+ * 1 0 |= LRU_REFS_REFERENCED: Used at least once. [2]\n+ * -WORKINGSET-+|- Pages beyond are workingset and never fall below this floor. [3]\n+ * 2 1\u003c-+|= LRU_REFS_WORKINGSET: Classical workingset, accessed twice, protected. [4]\n+ * 3 2 |- LRU_REFS_PROTECTED: Protected workingset, promoted pages capped at here. [5]\n+ * 4 2 |\n+ * 5 3 |- The tier here is MAX_NR_TIERS - 1\n+ * 6 3 |\n+ * 7 3 |= LRU_REFS_MAX: Promotion candidate. [6]\n+ * -PROMOTION-\u003e-/\n *\n- * MAX_NR_TIERS is set to 4 so that the multi-gen LRU can support twice the\n- * number of categories of the active/inactive LRU when keeping track of\n- * accesses through file descriptors. This uses MAX_NR_TIERS-2 spare bits in\n- * folio-\u003eflags, masked by LRU_REFS_MASK.\n+ * Ideally each tier holds folios of similar access patterns: lower tiers\n+ * are less important and evicted faster. A page's reference count and\n+ * tier are capped when it changes generation, preventing it from\n+ * dominating the new generation based on old-generation access history.\n+ * Generation ordering already ensures a newer-gen page is hotter than an\n+ * older-gen one regardless of tier.\n+ *\n+ * Refs tracks accesses from two sources: page table (lazily collected by\n+ * the page table aging walk or rmap eviction lookup) and file descriptors\n+ * (by folio_mark_accessed). Page table accesses are weighted heavier\n+ * because the accessed bit is sticky (undercounts repeated accesses),\n+ * passively collected, and page faults are generally more important as\n+ * userspace does not expect a memory access to block on reclaim. Both\n+ * access types increment refs by one; the result is capped at\n+ * LRU_REFS_PROTECTED on promotion or deferral, or LRU_REFS_MAX otherwise.\n+ *\n+ * 1. Tier is fls(N-1) for N \u003e 1, 0 otherwise. Folios with zero\n+ * accesses (refs == 0) are generally cold, e.g. readahead folios.\n+ *\n+ * Freshly allocated folios start with refs == 0; faulted and mapped\n+ * folios have their page table access bit set, so the first page table\n+ * access check always sets LRU_REFS_REFERENCED. That is the second\n+ * chance described above MIN_NR_GENS: it moves the folio one\n+ * generation forward if it is at the tail of its LRU, i.e., in one\n+ * of the two oldest generations of its type.\n+ *\n+ * 2. Folios accessed once stay on tier 0: one-time usage does not\n+ * qualify for protection. A second access advances the folio,\n+ * aligning with classical LRU's use-twice threshold. A second page\n+ * table access promotes to the latest gen; file access only defers\n+ * eviction from the tail gens of its type.\n+ *\n+ * 3. Folios accessed at least twice are considered workingset. This\n+ * mostly aligns with classical LRU: at least one I/O is saved by\n+ * keeping them in memory. Folios at or above this level never fall\n+ * below tier 1 (the workingset floor), so tier 0 stays a clean tier\n+ * for cold cache while tier 1 serves as the fallback line for\n+ * actually reused or historically hot folios.\n+ *\n+ * Folios refaulted through a page fault at refs 1 will enter the second\n+ * newest gen, so faulting will be protected better.\n+ *\n+ * 4. Starting from tier 1, PID protection sacrifices lower tiers to\n+ * protect higher tiers by comparing refault rates for long-term\n+ * accuracy, and caps higher refs to this value. Since PID protection\n+ * bypasses page table lookup and clearing, when a further eviction\n+ * attempt occurs after PID loosens, the folio's page table access is\n+ * rechecked and a referenced folio is put back with refs capped to\n+ * this value as well. This also gives folios a fair opportunity to be\n+ * promoted by file access again.\n+ *\n+ * Folios refaulted through a page fault at tier 1 or above are activated\n+ * and enter the newest gen. Non fault page will enter second oldest gen,\n+ * driving aging and workingset shifting.\n+ *\n+ * 5. Pages beyond the ordinary workingset tier form new tiers for the\n+ * PID controller to protect differently. Folios at or above this\n+ * level are capped at LRU_REFS_PROTECTED on promotion or deferral,\n+ * and at LRU_REFS_WORKINGSET under PID protection in the oldest\n+ * generation, where they represent a historical workingset.\n+ *\n+ * 6. Folios that reach LRU_REFS_MAX are advanced to the next generation\n+ * on further access, with refs capped to LRU_REFS_PROTECTED. This\n+ * gives them a fair start for advancement to an even newer generation\n+ * while keeping hot folios distinguishable.\n+ *\n+ * Tiering uses PG_referenced and PG_workingset as the lower two bits,\n+ * and the bits masked by LRU_REFS_MASK as the higher bits, so the refs\n+ * count ranges from 0 to LRU_REFS_MAX. A folio is on the workingset\n+ * tier once accessed at least twice, which is more consistent with the\n+ * classical LRU.\n+ *\n+ * A folio's referenced count never goes backwards except upon gen\n+ * increase as described above, or when explicitly reset by\n+ * folio_reset_lru_refs(). Refault of a reclaimed folio restores\n+ * its referenced count, capped at LRU_REFS_PROTECTED, which aligns with\n+ * promotion. Page table refaults of previous workingset folios send\n+ * them to the latest gen, driving aging faster.\n+ *\n+ * For the active/inactive LRU ABI (/proc/vmstat), a folio is active\n+ * if its referenced count reaches LRU_REFS_ACTIVATED. The threshold\n+ * currently set to LRU_REFS_PROTECTED, which is tier 2.\n+ *\n+ * MAX_NR_TIERS is set to 4 so that the multi-gen LRU can support twice\n+ * the number of categories of the active/inactive LRU.\n */\n #define MAX_NR_TIERS\t\t4U\n #define LRU_TIER_MIN\t\t0U\n #define LRU_TIER_MAX\t\t(MAX_NR_TIERS - 1)\n \n+/* Flags for folio_inc_lru_refs() */\n+#define LRU_REF_MAPPED\t\t0x1U\n+#define LRU_REF_EXEC\t\t0x2U\n+#define LRU_REF_FORCE\t\t0x4U\n+\n+#define LRU_REFS_REFERENCED\t0x1\n+#define LRU_REFS_WORKINGSET\t0x2\n+#define LRU_REFS_PROTECTED\t0x3\n+#define LRU_REFS_ACTIVATED\tLRU_REFS_PROTECTED\n+\n #ifndef __GENERATING_BOUNDS_H\n \n #define LRU_GEN_MASK\t\t((BIT(LRU_GEN_WIDTH) - 1) \u003c\u003c LRU_GEN_PGOFF)\n #define LRU_REFS_MASK\t\t((BIT(LRU_REFS_WIDTH) - 1) \u003c\u003c LRU_REFS_PGOFF)\n-#define LRU_REFS_MAX\t\tBIT(LRU_REFS_WIDTH)\n-\n-/*\n- * For folios accessed multiple times through file descriptors,\n- * lru_gen_inc_refs() sets additional bits of LRU_REFS_WIDTH in folio-\u003eflags\n- * after PG_referenced, then PG_workingset after LRU_REFS_WIDTH. After all its\n- * bits are set, i.e., LRU_REFS_FLAGS|BIT(PG_workingset), a folio is lazily\n- * promoted into the second oldest generation in the eviction path. And when\n- * folio_inc_gen() does that, it clears LRU_REFS_FLAGS so that\n- * lru_gen_inc_refs() can start over. Note that for this case, LRU_REFS_MASK is\n- * only valid when PG_referenced is set.\n- *\n- * For folios accessed multiple times through page tables, folio_update_gen()\n- * from a page table walk or lru_gen_set_refs() from a rmap walk sets\n- * PG_referenced after the accessed bit is cleared for the first time.\n- * Thereafter, those two paths set PG_workingset and promote folios to the\n- * youngest generation. Like folio_inc_gen(), folio_update_gen() also clears\n- * PG_referenced. Note that for this case, LRU_REFS_MASK is not used.\n- *\n- * For both cases above, after PG_workingset is set on a folio, it remains until\n- * this folio is either reclaimed, or \"deactivated\" by lru_gen_clear_refs(). It\n- * can be set again if lru_gen_test_recent() returns true upon a refault.\n- */\n-#define LRU_REFS_FLAGS\t\t(LRU_REFS_MASK | BIT(PG_referenced))\n+#define LRU_REFS_FLAGS\t\t(LRU_REFS_MASK | BIT(PG_referenced) | BIT(PG_workingset))\n+#define LRU_REFS_MAX\t\t(BIT(LRU_REFS_WIDTH + 2) - 1)\n \n struct lruvec;\n struct page_vma_mapped_walk;\n@@ -554,7 +625,7 @@ struct lru_gen_folio {\n \t/* the multi-gen LRU lists, lazily sorted on eviction */\n \tstruct list_head folios[MAX_NR_GENS][ANON_AND_FILE][MAX_NR_ZONES];\n \t/* the multi-gen LRU sizes, eventually consistent */\n-\tlong nr_pages[MAX_NR_GENS][ANON_AND_FILE][MAX_NR_ZONES];\n+\tatomic_long_t nr_pages[MAX_NR_GENS][ANON_AND_FILE][MAX_NR_ZONES];\n \t/* the exponential moving average of refaulted */\n \tunsigned long avg_refaulted[ANON_AND_FILE][MAX_NR_TIERS];\n \t/* the exponential moving average of evicted+protected */\n@@ -605,6 +676,8 @@ struct lru_gen_mm_walk {\n \tunsigned long seq;\n \t/* the next address within an mm to scan */\n \tunsigned long next_addr;\n+\t/* to batch activated pages */\n+\tint nr_activated[ANON_AND_FILE][MAX_NR_ZONES];\n \t/* to batch promoted pages */\n \tint nr_pages[MAX_NR_GENS][ANON_AND_FILE][MAX_NR_ZONES];\n \t/* to batch the mm stats */\ndiff --git a/include/linux/page-flags.h b/include/linux/page-flags.h\nindex b0ddc652e76cc..f6625dc042dab 100644\n--- a/include/linux/page-flags.h\n+++ b/include/linux/page-flags.h\n@@ -549,8 +549,6 @@ PAGEFLAG(LRU, lru, PF_HEAD) __CLEARPAGEFLAG(LRU, lru, PF_HEAD)\n FOLIO_FLAG(active, FOLIO_HEAD_PAGE)\n \t__FOLIO_CLEAR_FLAG(active, FOLIO_HEAD_PAGE)\n \tFOLIO_TEST_CLEAR_FLAG(active, FOLIO_HEAD_PAGE)\n-PAGEFLAG(Workingset, workingset, PF_HEAD)\n-\tTESTCLEARFLAG(Workingset, workingset, PF_HEAD)\n PAGEFLAG(Checked, checked, PF_NO_COMPOUND)\t /* Used by some filesystems */\n \n /* Xen */\ndiff --git a/kernel/bounds.c b/kernel/bounds.c\nindex 02b619eb61065..06a034713b5dd 100644\n--- a/kernel/bounds.c\n+++ b/kernel/bounds.c\n@@ -25,7 +25,7 @@ int main(void)\n \tDEFINE(SPINLOCK_SIZE, sizeof(spinlock_t));\n #ifdef CONFIG_LRU_GEN\n \tDEFINE(LRU_GEN_WIDTH, order_base_2(MAX_NR_GENS + 1));\n-\tDEFINE(__LRU_REFS_WIDTH, MAX_NR_TIERS - 2);\n+\tDEFINE(__LRU_REFS_WIDTH, MAX_NR_TIERS - 3);\n #else\n \tDEFINE(LRU_GEN_WIDTH, 0);\n \tDEFINE(__LRU_REFS_WIDTH, 0);\ndiff --git a/mm/damon/paddr.c b/mm/damon/paddr.c\nindex 2cfdc356b4157..6412ff2396b3f 100644\n--- a/mm/damon/paddr.c\n+++ b/mm/damon/paddr.c\n@@ -279,7 +279,8 @@ static unsigned long damon_pa_pageout(struct damon_region *r,\n \t\telse\n \t\t\t*sz_filter_passed += folio_size(folio) / addr_unit;\n \n-\t\tfolio_clear_referenced(folio);\n+\t\t/* The regions is cold, better drop the hotness info */\n+\t\tfolio_clear_lru_refs(folio);\n \t\tfolio_test_clear_young(folio);\n \t\tif (!folio_isolate_lru(folio))\n \t\t\tgoto put_folio;\ndiff --git a/mm/filemap.c b/mm/filemap.c\nindex b74bc1e5015c6..49e827058e147 100644\n--- a/mm/filemap.c\n+++ b/mm/filemap.c\n@@ -21,6 +21,7 @@\n #include \u003clinux/gfp.h\u003e\n #include \u003clinux/mm.h\u003e\n #include \u003clinux/swap.h\u003e\n+#include \u003clinux/mm_inline.h\u003e\n #include \u003clinux/leafops.h\u003e\n #include \u003clinux/syscalls.h\u003e\n #include \u003clinux/mman.h\u003e\ndiff --git a/mm/folio.c b/mm/folio.c\nindex 35e242b48870b..9b9a5fe568c98 100644\n--- a/mm/folio.c\n+++ b/mm/folio.c\n@@ -273,7 +273,6 @@ static void lru_activate(struct lruvec *lruvec, struct folio *folio)\n \tif (folio_test_active(folio) || folio_test_unevictable(folio))\n \t\treturn;\n \n-\n \tlruvec_del_folio(lruvec, folio);\n \tfolio_set_active(folio);\n \tlruvec_add_folio(lruvec, folio);\n@@ -350,66 +349,6 @@ static void __lru_cache_activate_folio(struct folio *folio)\n \tlocal_unlock(\u0026cpu_fbatches.lock);\n }\n \n-#ifdef CONFIG_LRU_GEN\n-\n-static void lru_gen_inc_refs(struct folio *folio)\n-{\n-\tunsigned long new_flags, old_flags = READ_ONCE(*folio_flags(folio, 0));\n-\tint refs;\n-\n-\tif (folio_test_unevictable(folio))\n-\t\treturn;\n-\n-\t/* see the comment on LRU_REFS_FLAGS */\n-\tif (!folio_lru_refs(folio)) {\n-\t\tfolio_set_lru_refs(folio, 1);\n-\t\treturn;\n-\t}\n-\n-\tdo {\n-\t\tnew_flags = old_flags;\n-\t\trefs = lru_get_refs_flags(old_flags);\n-\t\tif (refs == LRU_REFS_MAX) {\n-\t\t\tif (!folio_test_workingset(folio))\n-\t\t\t\tfolio_set_workingset(folio);\n-\t\t\treturn;\n-\t\t}\n-\t\tlru_set_refs_flags(\u0026new_flags, refs + 1);\n-\t} while (!try_cmpxchg(folio_flags(folio, 0), \u0026old_flags, new_flags));\n-}\n-\n-static bool lru_gen_clear_refs(struct folio *folio)\n-{\n-\tint gen = folio_lru_gen(folio);\n-\tint type = folio_is_file_lru(folio);\n-\tunsigned long seq;\n-\n-\tif (gen \u003c 0)\n-\t\treturn true;\n-\n-\tfolio_set_lru_refs(folio, 0);\n-\tfolio_clear_workingset(folio);\n-\n-\trcu_read_lock();\n-\tseq = READ_ONCE(folio_lruvec(folio)-\u003elrugen.min_seq[type]);\n-\trcu_read_unlock();\n-\t/* whether can do without shuffling under the LRU lock */\n-\treturn gen == lru_gen_from_seq(seq);\n-}\n-\n-#else /* !CONFIG_LRU_GEN */\n-\n-static void lru_gen_inc_refs(struct folio *folio)\n-{\n-}\n-\n-static bool lru_gen_clear_refs(struct folio *folio)\n-{\n-\treturn false;\n-}\n-\n-#endif /* CONFIG_LRU_GEN */\n-\n /**\n * folio_mark_accessed - Mark a folio as having seen activity.\n * @folio: The folio to mark.\n@@ -428,7 +367,8 @@ void folio_mark_accessed(struct folio *folio)\n \tif (folio_test_dropbehind(folio))\n \t\treturn;\n \tif (lru_gen_enabled()) {\n-\t\tlru_gen_inc_refs(folio);\n+\t\tif (!folio_test_unevictable(folio))\n+\t\t\tfolio_inc_lru_refs(folio, 0);\n \t\treturn;\n \t}\n \n@@ -474,21 +414,6 @@ void folio_add_lru(struct folio *folio)\n \t\t\tfolio_test_unevictable(folio), folio);\n \tVM_BUG_ON_FOLIO(folio_test_lru(folio), folio);\n \n-\t/*\n-\t * For refaulted workingset folios, set PG_active so they\n-\t * can be added to active generations.\n-\t * For prefaulted file folios, folio_mark_accessed() sets\n-\t * PG_referenced so lru_gen_folio_seq() places them into\n-\t * the second oldest generation.\n-\t */\n-\tif (lru_gen_enabled() \u0026\u0026 !folio_test_unevictable(folio) \u0026\u0026\n-\t lru_gen_in_fault() \u0026\u0026 !(current-\u003eflags \u0026 PF_MEMALLOC)) {\n-\t\tif (folio_test_workingset(folio))\n-\t\t\tfolio_set_active(folio);\n-\t\telse if (!folio_test_referenced(folio))\n-\t\t\tfolio_mark_accessed(folio);\n-\t}\n-\n \tfolio_batch_add_and_move(folio, lru_add);\n }\n EXPORT_SYMBOL(folio_add_lru);\n@@ -600,7 +525,7 @@ static void lru_lazyfree(struct lruvec *lruvec, struct folio *folio)\n \tlruvec_del_folio(lruvec, folio);\n \tfolio_clear_active(folio);\n \tif (lru_gen_enabled())\n-\t\tlru_gen_clear_refs(folio);\n+\t\t__folio_set_lru_refs(folio, 0);\n \telse\n \t\tfolio_clear_referenced(folio);\n \t/*\n@@ -673,7 +598,7 @@ void deactivate_file_folio(struct folio *folio)\n \tif (folio_test_unevictable(folio) || !folio_test_lru(folio))\n \t\treturn;\n \n-\tif (lru_gen_enabled() \u0026\u0026 lru_gen_clear_refs(folio))\n+\tif (lru_gen_enabled() \u0026\u0026 !folio_reset_lru_refs(folio))\n \t\treturn;\n \n \tfolio_batch_add_and_move(folio, lru_deactivate_file);\n@@ -692,7 +617,7 @@ void folio_deactivate(struct folio *folio)\n \tif (folio_test_unevictable(folio) || !folio_test_lru(folio))\n \t\treturn;\n \n-\tif (lru_gen_enabled() ? lru_gen_clear_refs(folio) : !folio_test_active(folio))\n+\tif (lru_gen_enabled() ? !folio_reset_lru_refs(folio) : !folio_test_active(folio))\n \t\treturn;\n \n \tfolio_batch_add_and_move(folio, lru_deactivate);\ndiff --git a/mm/gup.c b/mm/gup.c\nindex f166acf794e30..b3f563d411923 100644\n--- a/mm/gup.c\n+++ b/mm/gup.c\n@@ -2896,7 +2896,7 @@ static unsigned long gup_fast_pte_range(pmd_t pmd, pmd_t *pmdp,\n \t\t\tgup_put_folio(folio, 1, flags);\n \t\t\tgoto pte_unmap;\n \t\t}\n-\t\tfolio_set_referenced(folio);\n+\t\tfolio_inc_lru_refs_fast(folio);\n \t\tpages[nr_pages] = page;\n \t\tnr_pages++;\n \t} while (ptep++, addr += PAGE_SIZE, addr != end);\n@@ -2965,7 +2965,7 @@ static unsigned long gup_fast_pmd_leaf(pmd_t orig, pmd_t *pmdp,\n \n \tfor (i = 0; i \u003c nr_pages; i++)\n \t\t*(pages++) = page++;\n-\tfolio_set_referenced(folio);\n+\tfolio_inc_lru_refs_fast(folio);\n \treturn nr_pages;\n }\n \n@@ -3007,7 +3007,7 @@ static unsigned long gup_fast_pud_leaf(pud_t orig, pud_t *pudp,\n \n \tfor (i = 0; i \u003c nr_pages; i++)\n \t\t*(pages++) = page++;\n-\tfolio_set_referenced(folio);\n+\tfolio_inc_lru_refs_fast(folio);\n \treturn nr_pages;\n }\n \ndiff --git a/mm/huge_memory.c b/mm/huge_memory.c\nindex bd44d58e86845..1e2b6448c0102 100644\n--- a/mm/huge_memory.c\n+++ b/mm/huge_memory.c\n@@ -3093,8 +3093,8 @@ static void __split_huge_pud_locked(struct vm_area_struct *vma, pud_t *pud,\n \n \tif (!folio_test_dirty(folio) \u0026\u0026 pud_dirty(old_pud))\n \t\tfolio_mark_dirty(folio);\n-\tif (!folio_test_referenced(folio) \u0026\u0026 pud_young(old_pud))\n-\t\tfolio_set_referenced(folio);\n+\tif (pud_young(old_pud) \u0026\u0026 vma_has_recency(vma))\n+\t\tfolio_mark_accessed(folio);\n \tfolio_remove_rmap_pud(folio, page, vma);\n \tadd_mm_counter(vma-\u003evm_mm, mm_counter_file(folio),\n \t\t-HPAGE_PUD_NR);\n@@ -3216,8 +3216,8 @@ static void __split_huge_pmd_locked(struct vm_area_struct *vma, pmd_t *pmd,\n \t\t\tfolio = page_folio(page);\n \t\t\tif (!folio_test_dirty(folio) \u0026\u0026 pmd_dirty(old_pmd))\n \t\t\t\tfolio_mark_dirty(folio);\n-\t\t\tif (!folio_test_referenced(folio) \u0026\u0026 pmd_young(old_pmd))\n-\t\t\t\tfolio_set_referenced(folio);\n+\t\t\tif (pmd_young(old_pmd) \u0026\u0026 vma_has_recency(vma))\n+\t\t\t\tfolio_mark_accessed(folio);\n \t\t\tfolio_remove_rmap_pmd(folio, page, vma);\n \t\t\tadd_mm_counter(mm, mm_counter_file(folio), -HPAGE_PMD_NR);\n \t\t\tfolio_put(folio);\ndiff --git a/mm/khugepaged.c b/mm/khugepaged.c\nindex ef5cd6fda34f7..5332b4c51fec7 100644\n--- a/mm/khugepaged.c\n+++ b/mm/khugepaged.c\n@@ -579,7 +579,9 @@ static bool folio_pte_referenced(struct folio *folio,\n \t\tstruct vm_area_struct *vma, unsigned long addr, pte_t pteval)\n {\n \t/* The folio was referenced previously ... */\n-\tif (folio_test_young(folio) || folio_test_referenced(folio))\n+\tif (folio_test_young(folio))\n+\t\treturn true;\n+\tif (folio_check_lru_refs(folio))\n \t\treturn true;\n \t/* ... or the PTE mapping was recently used */\n \treturn pte_young(pteval) || mmu_notifier_test_young(vma-\u003evm_mm, addr);\ndiff --git a/mm/madvise.c b/mm/madvise.c\nindex 75b409e97dbf2..184611fdd6f9d 100644\n--- a/mm/madvise.c\n+++ b/mm/madvise.c\n@@ -360,6 +360,45 @@ static inline int madvise_folio_pte_batch(unsigned long addr, unsigned long end,\n \t\t\t\t FPB_MERGE_YOUNG_DIRTY);\n }\n \n+static void madvise_cold_prep_folio(struct folio *folio)\n+{\n+\t/*\n+\t * VM couldn't reclaim the folio unless we clear PG_young.\n+\t * As a side effect, it makes confuse idle-page tracking\n+\t * because they will miss recent referenced history.\n+\t */\n+\tfolio_test_clear_young(folio);\n+\n+\t/*\n+\t * For the active/inactive LRU, a folio demoted out of the active\n+\t * list should have PG_workingset so its refault is still accounted\n+\t * as a workingset refault, and the referenced bit always needs to\n+\t * be cleared even for inactive folios. Nothing to do for MGLRU as\n+\t * folio_deactivate() clears the refs count through\n+\t * folio_reset_lru_refs().\n+\t */\n+\tif (!lru_gen_enabled()) {\n+\t\tfolio_clear_referenced(folio);\n+\t\tif (folio_test_active(folio))\n+\t\t\tfolio_set_workingset(folio);\n+\t}\n+}\n+\n+static void madvise_pageout_prep_folio(struct folio *folio)\n+{\n+\tint refs = folio_lru_refs(folio);\n+\n+\t/*\n+\t * For MGLRU, drop the count below LRU_REFS_WORKINGSET and cap\n+\t * the rest at it: MADV is\n+\t * suggesting the folio is actually cold, so the eviction shadow\n+\t * should not record its hotness in the high tier counters, but\n+\t * the workingset classification is preserved for PSI.\n+\t */\n+\tif (lru_gen_enabled())\n+\t\t__folio_set_lru_refs(folio, refs \u003e= LRU_REFS_WORKINGSET ? LRU_REFS_WORKINGSET : 0);\n+}\n+\n static int madvise_cold_or_pageout_pte_range(pmd_t *pmd,\n \t\t\t\tunsigned long addr, unsigned long end,\n \t\t\t\tstruct mm_walk *walk)\n@@ -434,12 +473,10 @@ static int madvise_cold_or_pageout_pte_range(pmd_t *pmd,\n \t\t\ttlb_remove_pmd_tlb_entry(tlb, pmd, addr);\n \t\t}\n \n-\t\tfolio_clear_referenced(folio);\n-\t\tfolio_test_clear_young(folio);\n-\t\tif (folio_test_active(folio))\n-\t\t\tfolio_set_workingset(folio);\n+\t\tmadvise_cold_prep_folio(folio);\n \t\tif (pageout) {\n \t\t\tif (folio_isolate_lru(folio)) {\n+\t\t\t\tmadvise_pageout_prep_folio(folio);\n \t\t\t\tif (folio_test_unevictable(folio))\n \t\t\t\t\tfolio_putback_lru(folio);\n \t\t\t\telse\n@@ -544,18 +581,10 @@ static int madvise_cold_or_pageout_pte_range(pmd_t *pmd,\n \t\t\ttlb_remove_tlb_entries(tlb, pte, nr, addr);\n \t\t}\n \n-\t\t/*\n-\t\t * We are deactivating a folio for accelerating reclaiming.\n-\t\t * VM couldn't reclaim the folio unless we clear PG_young.\n-\t\t * As a side effect, it makes confuse idle-page tracking\n-\t\t * because they will miss recent referenced history.\n-\t\t */\n-\t\tfolio_clear_referenced(folio);\n-\t\tfolio_test_clear_young(folio);\n-\t\tif (folio_test_active(folio))\n-\t\t\tfolio_set_workingset(folio);\n+\t\tmadvise_cold_prep_folio(folio);\n \t\tif (pageout) {\n \t\t\tif (folio_isolate_lru(folio)) {\n+\t\t\t\tmadvise_pageout_prep_folio(folio);\n \t\t\t\tif (folio_test_unevictable(folio))\n \t\t\t\t\tfolio_putback_lru(folio);\n \t\t\t\telse\ndiff --git a/mm/migrate.c b/mm/migrate.c\nindex 7bdcdb57652f8..5eefd8547f70e 100644\n--- a/mm/migrate.c\n+++ b/mm/migrate.c\n@@ -786,8 +786,6 @@ void folio_migrate_flags(struct folio *newfolio, struct folio *folio)\n \t\tfolio_set_active(newfolio);\n \t} else if (folio_test_clear_unevictable(folio))\n \t\tfolio_set_unevictable(newfolio);\n-\tif (folio_test_workingset(folio))\n-\t\tfolio_set_workingset(newfolio);\n \tif (folio_test_checked(folio))\n \t\tfolio_set_checked(newfolio);\n \t/*\ndiff --git a/mm/page_io.c b/mm/page_io.c\nindex c6824fcd483e0..540ae8caa932d 100644\n--- a/mm/page_io.c\n+++ b/mm/page_io.c\n@@ -16,6 +16,7 @@\n #include \u003clinux/gfp.h\u003e\n #include \u003clinux/pagemap.h\u003e\n #include \u003clinux/swap.h\u003e\n+#include \u003clinux/mm_inline.h\u003e\n #include \u003clinux/bio.h\u003e\n #include \u003clinux/swapops.h\u003e\n #include \u003clinux/writeback.h\u003e\ndiff --git a/mm/vmscan.c b/mm/vmscan.c\nindex daa7e37c2236a..e604b0893847e 100644\n--- a/mm/vmscan.c\n+++ b/mm/vmscan.c\n@@ -931,38 +931,257 @@ enum folio_references {\n };\n \n #ifdef CONFIG_LRU_GEN\n+/******************************************************************************\n+ * Referenced count feedback\n+ ******************************************************************************/\n+\n /*\n- * Only used on a mapped folio in the eviction (rmap walk) path, where promotion\n- * needs to be done by taking the folio off the LRU list and then adding it back\n- * with PG_active set. In contrast, the aging (page table walk) path uses\n- * folio_update_gen().\n+ * The folio_inc_lru_refs{_*} helpers below collect the referenced info\n+ * (hotness) from other parts, including the page table walker, the rmap walk\n+ * upon eviction, the rmap lookaround, and file descriptors\n+ * (folio_mark_accessed).\n+ *\n+ * Page table accesses use refs from either source. The first reference\n+ * advances tail-gen folios by one generation; a second promotes to the\n+ * newest. Executable file folios promote on their first access to avoid\n+ * I/O thrashing. Isolated rmap accesses use evict_folios() for putback.\n+ *\n+ * Promotion changes folio-\u003eflags; sort_folio() or inc_min_seq() moves the\n+ * folio later. Young PTEs found during unmapping use folio_mark_accessed()\n+ * and follow the file descriptor rules below.\n+ *\n+ * File descriptor accesses do not promote. They only defer eviction from\n+ * the tail gens of their type, and only once the folio is a workingset\n+ * folio (LRU_REFS_WORKINGSET), leaving the rest to PID protection. Page\n+ * table accesses are treated more generously because the accessed bit is\n+ * sticky (it under-counts repeated accesses) and because a page fault is\n+ * more costly than file descriptor I/O.\n+ *\n+ * PID protection operates on tier \u003e 0 folios. The one proactive promotion\n+ * outside of it and the page table path is the overflow case where the\n+ * referenced count exceeds LRU_REFS_MAX, which means the folio is hotter\n+ * than everything else in its generation.\n+ *\n+ * Whenever a folio changes generation here its referenced count is capped at\n+ * LRU_REFS_PROTECTED, so it starts at or below the protected tier regardless\n+ * of its old-generation access history. PID protection (folio_inc_gen) caps\n+ * at LRU_REFS_WORKINGSET independently.\n+ *\n+ * folio_inc_lru_refs() retries with LRU_REF_FORCE if the aging recycled the\n+ * gen index it was about to store: newest gen and an extra reference.\n */\n-static bool lru_gen_set_refs(struct folio *folio, const vma_flags_t *vma_flags)\n-{\n-\t/* see the comment on LRU_REFS_FLAGS */\n-\tif (!folio_test_referenced(folio) \u0026\u0026 !folio_test_workingset(folio)) {\n-\t\t/* Activate file-backed executable folios after first usage. */\n-\t\tif (is_exec_file_folio(folio, vma_flags)) {\n-\t\t\tfolio_set_workingset(folio);\n-\t\t\tfolio_set_lru_refs(folio, 0);\n-\t\t\treturn true;\n+\n+/*\n+ * Update the folio's lru refs indicator. The caller doesn't need to hold\n+ * the folio lock, isolate the folio, or hold the lruvec lock. Used by both\n+ * cache access (flags == 0) and page table access (LRU_REF_MAPPED,\n+ * optionally with LRU_REF_EXEC).\n+ */\n+void folio_inc_lru_refs(struct folio *folio, unsigned int flags)\n+{\n+\tint max_gen;\n+\tint file, old_refs, refs, old_gen, gen;\n+\tunsigned long new_flags, old_flags, max_seq;\n+\tlong nr_pages = folio_nr_pages(folio);\n+\tstruct lru_gen_folio *lrugen;\n+\tstruct lruvec *lruvec = NULL;\n+\tbool aged = false;\n+\n+retry:\n+\told_flags = READ_ONCE(*folio_flags(folio, 0));\n+\tdo {\n+\t\tnew_flags = old_flags;\n+\t\told_gen = lru_get_gen_flags(old_flags);\n+\t\told_refs = lru_get_refs_flags(old_flags);\n+\t\trefs = old_refs + 1;\n+\t\tgen = old_gen;\n+\t\tfile = folio_flags_is_file_lru(\u0026old_flags);\n+\t\tif (old_gen \u003c 0)\n+\t\t\tgoto out;\n+\t\tif (!lruvec) {\n+\t\t\tlruvec = folio_lruvec_live_get(folio);\n+\t\t\tlrugen = \u0026lruvec-\u003elrugen;\n+\t\t}\n+\t\t/*\n+\t\t * Read the flags before max_seq, paired with the store of a\n+\t\t * gen, which samples max_seq first.\n+\t\t */\n+\t\tsmp_rmb();\n+\t\tmax_seq = READ_ONCE(lrugen-\u003emax_seq);\n+\t\tmax_gen = lru_gen_from_seq(max_seq);\n+\t\tif (old_gen == max_gen)\n+\t\t\tgoto out;\n+\t\tif (flags \u0026 (LRU_REF_MAPPED | LRU_REF_EXEC | LRU_REF_FORCE)) {\n+\t\t\tif (refs \u003e LRU_REFS_REFERENCED ||\n+\t\t\t flags \u0026 (LRU_REF_EXEC | LRU_REF_FORCE))\n+\t\t\t\tgen = max_gen;\n+\t\t\t/* First access only defers eviction of a tail gen */\n+\t\t\telse if (lru_gen_at_tail(lruvec, file, old_gen))\n+\t\t\t\tgen = (old_gen + 1) % MAX_NR_GENS;\n+\t\t\trefs = min(refs, LRU_REFS_PROTECTED);\n+\t\t} else if (refs \u003e LRU_REFS_MAX) {\n+\t\t\t/* LRU refs counting overflow, bump the gen */\n+\t\t\tgen = (old_gen + 1) % MAX_NR_GENS;\n+\t\t\trefs = LRU_REFS_PROTECTED;\n+\t\t} else if (lru_gen_at_tail(lruvec, file, old_gen) \u0026\u0026 refs \u003e= LRU_REFS_WORKINGSET) {\n+\t\t\t/* Defer eviction of just accessed workingset */\n+\t\t\tgen = (old_gen + 1) % MAX_NR_GENS;\n+\t\t\trefs = min(refs, LRU_REFS_PROTECTED);\n \t\t}\n+out:\n+\t\trefs = min(refs, LRU_REFS_MAX);\n+\t\tlru_set_refs_flags(\u0026new_flags, refs);\n+\t\tif (gen != old_gen)\n+\t\t\tlru_set_gen_flags(\u0026new_flags, gen);\n+\t\tif (new_flags == old_flags)\n+\t\t\tbreak;\n+\t} while (!try_cmpxchg(folio_flags(folio, 0), \u0026old_flags, new_flags));\n \n-\t\tfolio_set_lru_refs(folio, 1);\n-\t\treturn false;\n+\tif (gen != old_gen) {\n+\t\t/*\n+\t\t * Gen-index reuse can fool cmpxchg(). Retry locklessly, accepting\n+\t\t * an extra reference to avoid folio_activate() latency.\n+\t\t */\n+\t\tif (unlikely(READ_ONCE(lrugen-\u003emax_seq) != max_seq)) {\n+\t\t\tflags = LRU_REF_FORCE;\n+\t\t\taged = true;\n+\t\t}\n+\t\tlru_gen_update_size(lruvec, file, folio, old_gen, gen);\n \t}\n \n-\t/* Promote on second access */\n-\tif (folio_lru_refs(folio) \u003e 1) {\n-\t\tfolio_set_workingset(folio);\n-\t\tfolio_set_lru_refs(folio, 0);\n-\t} else {\n-\t\tfolio_mark_accessed(folio);\n+\tif (lru_refs_is_active(old_refs) != lru_refs_is_active(refs) \u0026\u0026 gen \u003e= 0) {\n+\t\tenum lru_list lru = file * LRU_INACTIVE_FILE;\n+\n+\t\t__update_lru_size(lruvec, lru + lru_refs_is_active(old_refs),\n+\t\t\t\t folio_zonenum(folio), -nr_pages);\n+\t\t__update_lru_size(lruvec, lru + lru_refs_is_active(refs),\n+\t\t\t\t folio_zonenum(folio), nr_pages);\n \t}\n-\treturn true;\n+\tif (aged) {\n+\t\taged = false;\n+\t\tgoto retry;\n+\t}\n+\tif (lruvec)\n+\t\tfolio_lruvec_live_put(lruvec);\n+}\n+\n+/*\n+ * Reset the folio's lru refs indicator. The caller doesn't need to hold\n+ * the folio lock, isolate the folio, or hold the lruvec lock.\n+ */\n+bool folio_reset_lru_refs(struct folio *folio)\n+{\n+\tint type, gen, refs;\n+\tunsigned long seq, new_flags, old_flags;\n+\tlong nr_pages = folio_nr_pages(folio);\n+\tstruct lruvec *lruvec;\n+\tbool reset = false;\n+\n+\told_flags = READ_ONCE(*folio_flags(folio, 0));\n+\tdo {\n+\t\tnew_flags = old_flags;\n+\t\trefs = lru_get_refs_flags(old_flags);\n+\t\ttype = folio_flags_is_file_lru(\u0026old_flags);\n+\t\tgen = lru_get_gen_flags(old_flags);\n+\t\tif (!refs)\n+\t\t\tbreak;\n+\t\tlru_set_refs_flags(\u0026new_flags, 0);\n+\t} while (!try_cmpxchg(folio_flags(folio, 0), \u0026old_flags, new_flags));\n+\n+\tif (gen \u003e= 0) {\n+\t\tlruvec = folio_lruvec_live_get(folio);\n+\n+\t\t/* If the folio is on list, caller might also want to demote it */\n+\t\tseq = READ_ONCE(lruvec-\u003elrugen.min_seq[type]);\n+\t\treset = (gen != lru_gen_from_seq(seq));\n+\t\tif (lru_refs_is_active(refs)) {\n+\t\t\t__update_lru_size(lruvec, type * LRU_FILE + LRU_ACTIVE,\n+\t\t\t\t\t folio_zonenum(folio), -nr_pages);\n+\t\t\t__update_lru_size(lruvec, type * LRU_FILE,\n+\t\t\t\t\t folio_zonenum(folio), nr_pages);\n+\t\t}\n+\n+\t\tfolio_lruvec_live_put(lruvec);\n+\t}\n+\n+\treturn reset;\n+}\n+\n+/*\n+ * Update the folio's lru refs indicator during a page table walk or the\n+ * look-around. max_seq can be stale as neither holds the LRU lock.\n+ *\n+ * Returns the old generation, or -1 if the folio is off the LRU list.\n+ * Old LRU refs, and updated gen and LRU refs info by the successful\n+ * cmpxchg are all stored by returning arguments.\n+ */\n+static int folio_inc_lru_refs_walk(struct folio *folio, struct lruvec *lruvec,\n+\t\t\t\t const vma_flags_t *vma_flags, int *new_gen,\n+\t\t\t\t int *old_refs, int *new_refs, int *type)\n+{\n+\tunsigned long new_flags, old_flags = READ_ONCE(*folio_flags(folio, 0));\n+\tint refs, old_gen, max_gen;\n+\tunsigned long max_seq;\n+\n+\tdo {\n+\t\tmax_seq = READ_ONCE(lruvec-\u003elrugen.max_seq);\n+\t\tmax_gen = lru_gen_from_seq(max_seq);\n+\t\told_gen = lru_get_gen_flags(old_flags);\n+\t\trefs = lru_get_refs_flags(old_flags) + 1;\n+\t\t*type = folio_flags_is_file_lru(\u0026old_flags);\n+\t\tnew_flags = old_flags;\n+\n+\t\tif (old_gen \u003e= 0 \u0026\u0026 old_gen != max_gen) {\n+\t\t\t*new_refs = min(refs, LRU_REFS_PROTECTED);\n+\t\t\t/* Promote second page table access or executable */\n+\t\t\tif (refs \u003e LRU_REFS_REFERENCED || is_exec_file_folio(folio, vma_flags))\n+\t\t\t\t*new_gen = max_gen;\n+\t\t\t/* First access only defers eviction of a tail gen */\n+\t\t\telse if (lru_gen_at_tail(lruvec, *type, old_gen))\n+\t\t\t\t*new_gen = (old_gen + 1) % MAX_NR_GENS;\n+\t\t\telse\n+\t\t\t\t*new_gen = old_gen;\n+\t\t\tlru_set_gen_flags(\u0026new_flags, *new_gen);\n+\t\t\tlru_set_refs_flags(\u0026new_flags, *new_refs);\n+\t\t} else {\n+\t\t\t*new_gen = old_gen;\n+\t\t\t*new_refs = min(refs, LRU_REFS_MAX);\n+\t\t\tlru_set_refs_flags(\u0026new_flags, *new_refs);\n+\t\t}\n+\t\tif (new_flags == old_flags)\n+\t\t\tbreak;\n+\t} while (!try_cmpxchg(folio_flags(folio, 0), \u0026old_flags, new_flags));\n+\n+\t*old_refs = lru_get_refs_flags(old_flags);\n+\n+\treturn old_gen;\n+}\n+\n+/*\n+ * Update the lru refs indicator of an isolated folio, only used on\n+ * mapped folios upon the final eviction.\n+ *\n+ * Increments the refs count (capped at LRU_REFS_PROTECTED, evict_folios()\n+ * then caps it at LRU_REFS_WORKINGSET). Returns true if the caller should\n+ * activate the folio (second access or executable), false to put it back\n+ * for a second chance.\n+ */\n+static bool folio_inc_lru_refs_isolated(struct folio *folio, const vma_flags_t *vma_flags)\n+{\n+\tunsigned long new_flags, old_flags = READ_ONCE(*folio_flags(folio, 0));\n+\tint refs;\n+\n+\tdo {\n+\t\tnew_flags = old_flags;\n+\t\trefs = lru_get_refs_flags(old_flags) + 1;\n+\t\tlru_set_refs_flags(\u0026new_flags, min(refs, LRU_REFS_PROTECTED));\n+\t} while (!try_cmpxchg(folio_flags(folio, 0), \u0026old_flags, new_flags));\n+\n+\t/* Promote second page table access or executable */\n+\treturn refs \u003e LRU_REFS_REFERENCED || is_exec_file_folio(folio, vma_flags);\n }\n #else\n-static bool lru_gen_set_refs(struct folio *folio, const vma_flags_t *vma_flags)\n+static bool folio_inc_lru_refs_isolated(struct folio *folio, const vma_flags_t *vma_flags)\n {\n \treturn false;\n }\n@@ -997,7 +1216,8 @@ static enum folio_references folio_check_references(struct folio *folio,\n \t\tif (!referenced_ptes)\n \t\t\treturn FOLIOREF_RECLAIM;\n \n-\t\treturn lru_gen_set_refs(folio, \u0026vma_flags) ? FOLIOREF_ACTIVATE : FOLIOREF_KEEP;\n+\t\treturn folio_inc_lru_refs_isolated(folio, \u0026vma_flags) ?\n+\t\t FOLIOREF_ACTIVATE : FOLIOREF_KEEP;\n \t}\n \n \treferenced_folio = folio_test_clear_referenced(folio);\n@@ -3298,9 +3518,9 @@ static bool iterate_mm_list_nowalk(struct lruvec *lruvec, unsigned long seq)\n * P term over the generations previously evicted, using the smoothing factor\n * 1/2; the D term isn't supported.\n *\n- * The setpoint (SP) is always the first tier of one type; the process variable\n- * (PV) is either any tier of the other type or any other tier of the same\n- * type.\n+ * To select a type, the setpoint (SP) is all tiers of one type and the process\n+ * variable (PV) is all tiers of the other type. To select a tier to protect,\n+ * the SP is the tiers below it and the PV is the tier itself.\n *\n * The error is the difference between the SP and the PV; the correction is to\n * turn off protection when SP\u003ePV or turn on protection when SP\u003cPV.\n@@ -3388,50 +3608,16 @@ static bool positive_ctrl_err(struct ctrl_pos *sp, struct ctrl_pos *pv)\n * the aging\n ******************************************************************************/\n \n-/* promote pages accessed through page tables */\n-static int folio_update_gen(struct folio *folio, int new_gen, int *type,\n-\t\t\t const vma_flags_t *vma_flags)\n-{\n-\tunsigned long new_flags, old_flags = READ_ONCE(*folio_flags(folio, 0));\n-\tint old_gen;\n-\n-\t/*\n-\t * See the comment on LRU_REFS_FLAGS, and activate file-backed\n-\t * executable folios after first usage to avoid typical IO\n-\t * thrashing from reclaiming.\n-\t */\n-\tif (!folio_test_referenced(folio) \u0026\u0026 !folio_test_workingset(folio) \u0026\u0026\n-\t !is_exec_file_folio(folio, vma_flags)) {\n-\t\tfolio_set_lru_refs(folio, 1);\n-\t\treturn -1;\n-\t}\n-\n-\tdo {\n-\t\told_gen = lru_get_gen_flags(old_flags);\n-\t\tnew_flags = old_flags;\n-\n-\t\t/* lru_gen_del_folio() has isolated this page? */\n-\t\tif (old_gen \u003c 0)\n-\t\t\tbreak;\n-\n-\t\tlru_set_gen_flags(\u0026new_flags, new_gen);\n-\t\tlru_set_refs_flags(\u0026new_flags, 0);\n-\t\tnew_flags |= BIT(PG_workingset);\n-\t} while (!try_cmpxchg(folio_flags(folio, 0), \u0026old_flags, new_flags));\n-\n-\t*type = folio_flags_is_file_lru(\u0026old_flags);\n-\treturn old_gen;\n-}\n-\n-static int __folio_inc_gen(struct folio *folio, int old_gen, bool *increased)\n+static int __folio_inc_gen(struct lruvec *lruvec, struct folio *folio,\n+\t\tint old_gen, bool *increased)\n {\n \tunsigned long new_flags, old_flags = READ_ONCE(*folio_flags(folio, 0));\n-\tint new_gen;\n+\tint file, refs, old_refs, new_gen;\n \n \tdo {\n \t\tnew_gen = lru_get_gen_flags(old_flags);\n \n-\t\t/* folio_update_gen() has promoted this page? */\n+\t\t/* folio_inc_lru_refs() has promoted this page? */\n \t\tif (new_gen \u003e= 0 \u0026\u0026 new_gen != old_gen) {\n \t\t\tif (increased)\n \t\t\t\t*increased = false;\n@@ -3440,43 +3626,68 @@ static int __folio_inc_gen(struct folio *folio, int old_gen, bool *increased)\n \n \t\tnew_flags = old_flags;\n \t\tnew_gen = (old_gen + 1) % MAX_NR_GENS;\n-\n+\t\told_refs = lru_get_refs_flags(old_flags);\n+\t\tfile = folio_flags_is_file_lru(\u0026old_flags);\n+\t\trefs = min(old_refs, LRU_REFS_WORKINGSET);\n \t\tlru_set_gen_flags(\u0026new_flags, new_gen);\n-\t\tlru_set_refs_flags(\u0026new_flags, 0);\n+\t\tlru_set_refs_flags(\u0026new_flags, refs);\n \t} while (!try_cmpxchg(folio_flags(folio, 0), \u0026old_flags, new_flags));\n \n+\t/* Refs capped below PROTECTED: demote if previously active. */\n+\tif (lru_refs_is_active(old_refs) != lru_refs_is_active(refs)) {\n+\t\tenum lru_list lru = file * LRU_INACTIVE_FILE;\n+\t\tint nr_pages = folio_nr_pages(folio);\n+\n+\t\t__update_lru_size(lruvec, lru + lru_refs_is_active(old_refs),\n+\t\t\t\t folio_zonenum(folio), -nr_pages);\n+\t\t__update_lru_size(lruvec, lru + lru_refs_is_active(refs),\n+\t\t\t\t folio_zonenum(folio), nr_pages);\n+\t}\n+\n \tif (increased)\n \t\t*increased = true;\n \treturn new_gen;\n }\n \n-/* protect pages accessed multiple times through file descriptors */\n+/*\n+ * Force bump a folio's generation. Used for PID protection or to skip a\n+ * folio from a zone ineligible for the current reclaim.\n+ */\n static int folio_inc_gen(struct lruvec *lruvec, struct folio *folio)\n {\n+\tbool gen_increased;\n \tint type = folio_is_file_lru(folio);\n \tstruct lru_gen_folio *lrugen = \u0026lruvec-\u003elrugen;\n \tint new_gen, old_gen = lru_gen_from_seq(lrugen-\u003emin_seq[type]);\n-\tbool gen_increased;\n \n-\tnew_gen = __folio_inc_gen(folio, old_gen, \u0026gen_increased);\n+\tnew_gen = __folio_inc_gen(lruvec, folio, old_gen, \u0026gen_increased);\n \tif (gen_increased)\n-\t\tlru_gen_update_size(lruvec, folio, old_gen, new_gen);\n+\t\tlru_gen_update_size(lruvec, type, folio, old_gen, new_gen);\n+\n \treturn new_gen;\n }\n \n static void update_batch_size(struct lru_gen_mm_walk *walk, struct folio *folio,\n-\t\t\t int old_gen, int new_gen, int type)\n+\t\t\t int old_gen, int new_gen, int old_refs, int new_refs, int type)\n {\n \tint zone = folio_zonenum(folio);\n \tint delta = folio_nr_pages(folio);\n \n-\tVM_WARN_ON_ONCE(old_gen \u003e= MAX_NR_GENS);\n-\tVM_WARN_ON_ONCE(new_gen \u003e= MAX_NR_GENS);\n-\n-\twalk-\u003ebatched++;\n+\t/* gen counter update */\n+\tif (old_gen != new_gen) {\n+\t\tVM_WARN_ON_ONCE(old_gen \u003e= MAX_NR_GENS);\n+\t\tVM_WARN_ON_ONCE(new_gen \u003e= MAX_NR_GENS);\n+\t\twalk-\u003ebatched++;\n+\t\twalk-\u003enr_pages[old_gen][type][zone] -= delta;\n+\t\twalk-\u003enr_pages[new_gen][type][zone] += delta;\n+\t}\n \n-\twalk-\u003enr_pages[old_gen][type][zone] -= delta;\n-\twalk-\u003enr_pages[new_gen][type][zone] += delta;\n+\t/* active/inactive counter update */\n+\tif (lru_refs_is_active(old_refs) != lru_refs_is_active(new_refs)) {\n+\t\twalk-\u003enr_activated[type][zone] +=\n+\t\t\tlru_refs_is_active(new_refs) ? delta : -delta;\n+\t\twalk-\u003ebatched++;\n+\t}\n }\n \n static void reset_batch_size(struct lru_gen_mm_walk *walk)\n@@ -3488,19 +3699,28 @@ static void reset_batch_size(struct lru_gen_mm_walk *walk)\n \twalk-\u003ebatched = 0;\n \n \tfor_each_gen_type_zone(gen, type, zone) {\n-\t\tenum lru_list lru = type * LRU_INACTIVE_FILE;\n \t\tint delta = walk-\u003enr_pages[gen][type][zone];\n \n \t\tif (!delta)\n \t\t\tcontinue;\n \n \t\twalk-\u003enr_pages[gen][type][zone] = 0;\n-\t\tWRITE_ONCE(lrugen-\u003enr_pages[gen][type][zone],\n-\t\t\t lrugen-\u003enr_pages[gen][type][zone] + delta);\n+\t\tatomic_long_add(delta, \u0026lrugen-\u003enr_pages[gen][type][zone]);\n+\t}\n+\n+\t/* apply batched active/inactive updates */\n+\tfor (type = 0; type \u003c ANON_AND_FILE; type++) {\n+\t\tfor (zone = 0; zone \u003c MAX_NR_ZONES; zone++) {\n+\t\t\tenum lru_list lru = type * LRU_INACTIVE_FILE;\n+\t\t\tint delta = walk-\u003enr_activated[type][zone];\n \n-\t\tif (lru_gen_is_active(lruvec, gen))\n-\t\t\tlru += LRU_ACTIVE;\n-\t\t__update_lru_size(lruvec, lru, zone, delta);\n+\t\t\tif (delta) {\n+\t\t\t\t__update_lru_size(lruvec, lru + LRU_ACTIVE,\n+\t\t\t\t\t\t zone, delta);\n+\t\t\t\t__update_lru_size(lruvec, lru, zone, -delta);\n+\t\t\t\twalk-\u003enr_activated[type][zone] = 0;\n+\t\t\t}\n+\t\t}\n \t}\n \n \tlruvec_unlock_irq(lruvec);\n@@ -3655,26 +3875,27 @@ static bool suitable_to_scan(int total, int young)\n static void walk_update_folio(struct lru_gen_mm_walk *walk, struct vm_area_struct *vma,\n \t\tstruct lruvec *lruvec, struct folio *folio, bool dirty)\n {\n-\tint new_gen, old_gen, type;\n+\tint new_gen, old_gen, old_refs, new_refs, type;\n+\tunsigned int flags = LRU_REF_MAPPED;\n \n \tif (!folio)\n \t\treturn;\n \n-\tnew_gen = lru_gen_from_seq(READ_ONCE(lruvec-\u003elrugen.max_seq));\n-\n \tif (dirty \u0026\u0026 !folio_test_dirty(folio) \u0026\u0026\n \t !(folio_test_anon(folio) \u0026\u0026 folio_test_swapbacked(folio) \u0026\u0026\n \t !folio_test_swapcache(folio)))\n \t\tfolio_mark_dirty(folio);\n \n \tif (walk) {\n-\t\told_gen = folio_update_gen(folio, new_gen, \u0026type, \u0026vma-\u003eflags);\n-\t\tif (old_gen \u003e= 0 \u0026\u0026 old_gen != new_gen)\n-\t\t\tupdate_batch_size(walk, folio, old_gen, new_gen, type);\n-\t} else if (lru_gen_set_refs(folio, \u0026vma-\u003eflags)) {\n-\t\told_gen = folio_lru_gen(folio);\n-\t\tif (old_gen \u003e= 0 \u0026\u0026 old_gen != new_gen)\n-\t\t\tfolio_activate(folio);\n+\t\told_gen = folio_inc_lru_refs_walk(folio, lruvec, \u0026vma-\u003eflags,\n+\t\t\t\t\t\t \u0026new_gen, \u0026old_refs, \u0026new_refs, \u0026type);\n+\t\tif (old_gen \u003e= 0)\n+\t\t\tupdate_batch_size(walk, folio, old_gen, new_gen,\n+\t\t\t\t\t old_refs, new_refs, type);\n+\t} else {\n+\t\tif (is_exec_file_folio(folio, \u0026vma-\u003eflags))\n+\t\t\tflags |= LRU_REF_EXEC;\n+\t\tfolio_inc_lru_refs(folio, flags);\n \t}\n }\n \n@@ -4029,6 +4250,7 @@ static void clear_mm_walk(void)\n \n \tVM_WARN_ON_ONCE(walk \u0026\u0026 memchr_inv(walk-\u003enr_pages, 0, sizeof(walk-\u003enr_pages)));\n \tVM_WARN_ON_ONCE(walk \u0026\u0026 memchr_inv(walk-\u003emm_stats, 0, sizeof(walk-\u003emm_stats)));\n+\tVM_WARN_ON_ONCE(walk \u0026\u0026 memchr_inv(walk-\u003enr_activated, 0, sizeof(walk-\u003enr_activated)));\n \n \tcurrent-\u003ereclaim_state-\u003emm_walk = NULL;\n \n@@ -4067,8 +4289,6 @@ static bool inc_min_seq(struct lruvec *lruvec, int type, int swappiness)\n \t\tgoto done;\n \n \tVM_WARN_ON_ONCE(get_nr_gens(lruvec, type) != MAX_NR_GENS);\n-\tVM_WARN_ON_ONCE(lru_gen_is_active(lruvec, old_gen) !=\n-\t\t\tlru_gen_is_active(lruvec, target_gen));\n \t/* prevent cold/hot inversion if the type is evictable */\n \tfor (zone = 0; zone \u003c MAX_NR_ZONES; zone++) {\n \t\tstruct list_head *target_list = \u0026lrugen-\u003efolios[target_gen][type][zone];\n@@ -4081,7 +4301,7 @@ static bool inc_min_seq(struct lruvec *lruvec, int type, int swappiness)\n \t\t\tstruct folio *folio = list_entry(pos, struct folio, lru);\n \t\t\tlong nr_pages = folio_nr_pages(folio);\n \t\t\tint refs = folio_lru_refs(folio);\n-\t\t\tbool workingset = folio_test_workingset(folio);\n+\t\t\tint tier = lru_tier_from_refs(refs);\n \t\t\tbool gen_increased;\n \n \t\t\tVM_WARN_ON_ONCE_FOLIO(folio_test_unevictable(folio), folio);\n@@ -4091,7 +4311,7 @@ static bool inc_min_seq(struct lruvec *lruvec, int type, int swappiness)\n \n \t\t\tprefetchw_next_lru_folio(folio, head);\n \t\t\tpos = pos-\u003enext;\n-\t\t\tnew_gen = __folio_inc_gen(folio, old_gen, \u0026gen_increased);\n+\t\t\tnew_gen = __folio_inc_gen(lruvec, folio, old_gen, \u0026gen_increased);\n \t\t\t/*\n \t\t\t * If gen_increased is false, this is a promotion. Put folios\n \t\t\t * at the head of the promoted gen. Otherwise, put them at\n@@ -4101,13 +4321,8 @@ static bool inc_min_seq(struct lruvec *lruvec, int type, int swappiness)\n \t\t\t\tdelta += nr_pages;\n \t\t\t\tbatch_end = \u0026folio-\u003elru;\n \n-\t\t\t\t/* don't count the workingset being lazily promoted */\n-\t\t\t\tif (refs + workingset != BIT(LRU_REFS_WIDTH) + 1) {\n-\t\t\t\t\tint tier = lru_tier_from_refs(refs, workingset);\n-\n-\t\t\t\t\tWRITE_ONCE(lrugen-\u003eprotected[hist][type][tier],\n-\t\t\t\t\t\t lrugen-\u003eprotected[hist][type][tier] + nr_pages);\n-\t\t\t\t}\n+\t\t\t\tWRITE_ONCE(lrugen-\u003eprotected[hist][type][tier],\n+\t\t\t\t\t lrugen-\u003eprotected[hist][type][tier] + nr_pages);\n \t\t\t} else {\n \t\t\t\tflush_lru_batch(head, \u0026batch_end, target_list);\n \t\t\t\tlist_move(\u0026folio-\u003elru, \u0026lrugen-\u003efolios[new_gen][type][zone]);\n@@ -4116,11 +4331,8 @@ static bool inc_min_seq(struct lruvec *lruvec, int type, int swappiness)\n \t\t\t\tbreak;\n \t\t}\n \t\tflush_lru_batch(head, \u0026batch_end, target_list);\n-\n-\t\tWRITE_ONCE(lrugen-\u003enr_pages[old_gen][type][zone],\n-\t\t\t lrugen-\u003enr_pages[old_gen][type][zone] - delta);\n-\t\tWRITE_ONCE(lrugen-\u003enr_pages[target_gen][type][zone],\n-\t\t\t lrugen-\u003enr_pages[target_gen][type][zone] + delta);\n+\t\tatomic_long_sub(delta, \u0026lrugen-\u003enr_pages[old_gen][type][zone]);\n+\t\tatomic_long_add(delta, \u0026lrugen-\u003enr_pages[target_gen][type][zone]);\n \t\tif (!remaining)\n \t\t\treturn false;\n \t}\n@@ -4186,8 +4398,7 @@ static void try_to_inc_min_seq(struct lruvec *lruvec, int swappiness)\n static bool inc_max_seq(struct lruvec *lruvec, unsigned long seq, int swappiness)\n {\n \tbool success;\n-\tint prev, next;\n-\tint type, zone;\n+\tint type, next;\n \tstruct lru_gen_folio *lrugen = \u0026lruvec-\u003elrugen;\n restart:\n \tif (seq \u003c READ_ONCE(lrugen-\u003emax_seq))\n@@ -4213,32 +4424,10 @@ static bool inc_max_seq(struct lruvec *lruvec, unsigned long seq, int swappiness\n \t\tgoto restart;\n \t}\n \n-\t/*\n-\t * Update the active/inactive LRU sizes for compatibility. Both sides of\n-\t * the current max_seq need to be covered, since max_seq+1 can overlap\n-\t * with min_seq[LRU_GEN_ANON] if swapping is constrained. And if they do\n-\t * overlap, cold/hot inversion happens.\n-\t */\n-\tprev = lru_gen_from_seq(lrugen-\u003emax_seq - 1);\n-\tnext = lru_gen_from_seq(lrugen-\u003emax_seq + 1);\n-\n-\tfor (type = 0; type \u003c ANON_AND_FILE; type++) {\n-\t\tfor (zone = 0; zone \u003c MAX_NR_ZONES; zone++) {\n-\t\t\tenum lru_list lru = type * LRU_INACTIVE_FILE;\n-\t\t\tlong delta = lrugen-\u003enr_pages[prev][type][zone] -\n-\t\t\t\t lrugen-\u003enr_pages[next][type][zone];\n-\n-\t\t\tif (!delta)\n-\t\t\t\tcontinue;\n-\n-\t\t\t__update_lru_size(lruvec, lru, zone, delta);\n-\t\t\t__update_lru_size(lruvec, lru + LRU_ACTIVE, zone, -delta);\n-\t\t}\n-\t}\n-\n \tfor (type = 0; type \u003c ANON_AND_FILE; type++)\n \t\treset_ctrl_pos(lruvec, type, false);\n \n+\tnext = lru_gen_from_seq(lrugen-\u003emax_seq + 1);\n \tWRITE_ONCE(lrugen-\u003etimestamps[next], jiffies);\n \t/* make sure preceding modifications appear */\n \tsmp_store_release(\u0026lrugen-\u003emax_seq, lrugen-\u003emax_seq + 1);\n@@ -4344,7 +4533,8 @@ static unsigned long lruvec_evictable_size(struct lruvec *lruvec, int swappiness\n \t\tfor (seq = min_seq[type]; seq \u003c= max_seq; seq++) {\n \t\t\tgen = lru_gen_from_seq(seq);\n \t\t\tfor (zone = 0; zone \u003c MAX_NR_ZONES; zone++)\n-\t\t\t\ttotal += max(READ_ONCE(lrugen-\u003enr_pages[gen][type][zone]), 0L);\n+\t\t\t\ttotal += max(atomic_long_read(\u0026lrugen-\u003enr_pages[gen][type][zone]),\n+\t\t\t\t\t 0L);\n \t\t}\n \t}\n \n@@ -4756,30 +4946,22 @@ static void __lru_gen_reparent_memcg(struct lruvec *child_lruvec, struct lruvec\n \t\t\t\t int zone, int type)\n {\n \tstruct lru_gen_folio *child_lrugen, *parent_lrugen;\n-\tenum lru_list lru = type * LRU_INACTIVE_FILE;\n \tint i;\n \n \tchild_lrugen = \u0026child_lruvec-\u003elrugen;\n \tparent_lrugen = \u0026parent_lruvec-\u003elrugen;\n \n-\tfor (i = 0; i \u003c get_nr_gens(child_lruvec, type); i++) {\n+\t/* Lockless promotion may leave folios outside the child's window */\n+\tfor (i = 0; i \u003c MAX_NR_GENS; i++) {\n \t\tint gen = lru_gen_from_seq(child_lrugen-\u003emax_seq - i);\n-\t\tlong nr_pages = child_lrugen-\u003enr_pages[gen][type][zone];\n-\t\tint child_lru_active = lru_gen_is_active(child_lruvec, gen) ? LRU_ACTIVE : 0;\n-\t\tint parent_lru_active = lru_gen_is_active(parent_lruvec, gen) ? LRU_ACTIVE : 0;\n+\t\tlong nr_pages = atomic_long_read(\u0026child_lrugen-\u003enr_pages[gen][type][zone]);\n \n \t\t/* Assuming that child pages are colder than parent pages */\n \t\tlist_splice_tail_init(\u0026child_lrugen-\u003efolios[gen][type][zone],\n \t\t\t\t \u0026parent_lrugen-\u003efolios[gen][type][zone]);\n \n-\t\tWRITE_ONCE(child_lrugen-\u003enr_pages[gen][type][zone], 0);\n-\t\tWRITE_ONCE(parent_lrugen-\u003enr_pages[gen][type][zone],\n-\t\t\t parent_lrugen-\u003enr_pages[gen][type][zone] + nr_pages);\n-\n-\t\tif (lru_gen_is_active(child_lruvec, gen) != lru_gen_is_active(parent_lruvec, gen)) {\n-\t\t\t__update_lru_size(child_lruvec, lru + child_lru_active, zone, -nr_pages);\n-\t\t\t__update_lru_size(parent_lruvec, lru + parent_lru_active, zone, nr_pages);\n-\t\t}\n+\t\tatomic_long_set(\u0026child_lrugen-\u003enr_pages[gen][type][zone], 0);\n+\t\tatomic_long_add(nr_pages, \u0026parent_lrugen-\u003enr_pages[gen][type][zone]);\n \t}\n }\n \n@@ -4825,8 +5007,7 @@ static bool sort_folio(struct lruvec *lruvec, struct folio *folio, struct scan_c\n \tint zone = folio_zonenum(folio);\n \tint delta = folio_nr_pages(folio);\n \tint refs = folio_lru_refs(folio);\n-\tbool workingset = folio_test_workingset(folio);\n-\tint tier = lru_tier_from_refs(refs, workingset);\n+\tint tier = lru_tier_from_refs(refs);\n \tstruct lru_gen_folio *lrugen = \u0026lruvec-\u003elrugen;\n \n \tVM_WARN_ON_ONCE_FOLIO(gen \u003e= MAX_NR_GENS, folio);\n@@ -4842,17 +5023,15 @@ static bool sort_folio(struct lruvec *lruvec, struct folio *folio, struct scan_c\n \t}\n \n \t/* protected */\n-\tif (tier \u003e tier_idx || refs + workingset == BIT(LRU_REFS_WIDTH) + 1) {\n+\tif (tier \u003e tier_idx) {\n+\t\tint hist = lru_hist_from_seq(lrugen-\u003emin_seq[type]);\n+\n \t\tgen = folio_inc_gen(lruvec, folio);\n \t\tlist_move(\u0026folio-\u003elru, \u0026lrugen-\u003efolios[gen][type][zone]);\n \n-\t\t/* don't count the workingset being lazily promoted */\n-\t\tif (refs + workingset != BIT(LRU_REFS_WIDTH) + 1) {\n-\t\t\tint hist = lru_hist_from_seq(lrugen-\u003emin_seq[type]);\n+\t\tWRITE_ONCE(lrugen-\u003eprotected[hist][type][tier],\n+\t\t\t lrugen-\u003eprotected[hist][type][tier] + delta);\n \n-\t\t\tWRITE_ONCE(lrugen-\u003eprotected[hist][type][tier],\n-\t\t\t\t lrugen-\u003eprotected[hist][type][tier] + delta);\n-\t\t}\n \t\treturn true;\n \t}\n \n@@ -4880,10 +5059,6 @@ static bool isolate_folio(struct lruvec *lruvec, struct folio *folio, struct sca\n \t\treturn false;\n \t}\n \n-\t/* see the comment on LRU_REFS_FLAGS */\n-\tif (!folio_test_referenced(folio))\n-\t\tfolio_set_lru_refs(folio, 0);\n-\n \tsuccess = lru_gen_del_folio(lruvec, folio, true);\n \tVM_WARN_ON_ONCE_FOLIO(!success, folio);\n \n@@ -4971,13 +5146,14 @@ static int get_tier_idx(struct lruvec *lruvec, int type)\n \tstruct ctrl_pos sp, pv;\n \n \t/*\n-\t * To leave a margin for fluctuations, use a larger gain factor (2:3).\n-\t * This value is chosen because any other tier would have at least twice\n-\t * as many refaults as the first tier.\n+\t * To leave a margin for fluctuations, use a larger gain factor (1:2).\n+\t * Stop at the first tier whose refault rate is clearly worse than\n+\t * that of the cumulative mass of the tiers below it; the PID\n+\t * protects the tiers above it.\n \t */\n-\tread_ctrl_pos(lruvec, type, LRU_TIER_MIN, LRU_TIER_MIN, 2, \u0026sp);\n \tfor (tier = LRU_TIER_MIN + 1; tier \u003c= LRU_TIER_MAX; tier++) {\n-\t\tread_ctrl_pos(lruvec, type, tier, tier, 3, \u0026pv);\n+\t\tread_ctrl_pos(lruvec, type, LRU_TIER_MIN, tier - 1, 1, \u0026sp);\n+\t\tread_ctrl_pos(lruvec, type, tier, tier, 2, \u0026pv);\n \t\tif (!positive_ctrl_err(\u0026sp, \u0026pv))\n \t\t\tbreak;\n \t}\n@@ -5119,12 +5295,13 @@ static int evict_folios(unsigned long nr_to_scan, struct lruvec *lruvec,\n \t\t}\n \n \t\t/*\n-\t\t * See the comments on LRU_REFS_FLAGS.\n-\t\t *\n \t\t * The rejected folios are never added to the oldest generation,\n \t\t * so this effectively promotes them by at least one generation.\n+\t\t * PG_active is the only placement hint here, so a folio kept on\n+\t\t * its first page table access lands in the second newest one.\n+\t\t * See \"Referenced count feedback\" above.\n \t\t */\n-\t\tfolio_set_lru_refs(folio, 0);\n+\t\t__folio_set_lru_refs(folio, min(folio_lru_refs(folio), LRU_REFS_WORKINGSET));\n \t\tif (lru_gen_folio_seq(lruvec, folio, false) == min_seq[type])\n \t\t\tfolio_set_active(folio);\n \t}\n@@ -5859,7 +6036,8 @@ static int lru_gen_seq_show(struct seq_file *m, void *v)\n \t\t\tchar mark = full \u0026\u0026 seq \u003c min_seq[type] ? 'x' : ' ';\n \n \t\t\tfor (zone = 0; zone \u003c MAX_NR_ZONES; zone++)\n-\t\t\t\tsize += max(READ_ONCE(lrugen-\u003enr_pages[gen][type][zone]), 0L);\n+\t\t\t\tsize += max(atomic_long_read(\u0026lrugen-\u003enr_pages[gen][type][zone]),\n+\t\t\t\t\t 0L);\n \n \t\t\tseq_printf(m, \" %10lu%c\", size, mark);\n \t\t}\ndiff --git a/mm/workingset.c b/mm/workingset.c\nindex 1504f91cdca5f..7681af535ca7e 100644\n--- a/mm/workingset.c\n+++ b/mm/workingset.c\n@@ -188,6 +188,12 @@\n #define EVICTION_SHIFT_ANON\t(EVICTION_SHIFT + SWAP_COUNT_SHIFT)\n #define EVICTION_MASK\t(~0UL \u003e\u003e EVICTION_SHIFT)\n #define EVICTION_MASK_ANON\t(~0UL \u003e\u003e EVICTION_SHIFT_ANON)\n+/*\n+ * LRU refs uses LRU_REFS_WIDTH + 2 bits, the 2 bits being PG_workingset\n+ * and PG_referenced. The lowest bit is recorded in the workingset field\n+ * of the shadow entry (to reuse pack_shadow()).\n+ */\n+#define LRU_REFS_BITS ((LRU_REFS_WIDTH + 2) - 1)\n \n /*\n * Eviction timestamps need to be able to cover the full range of\n@@ -242,13 +248,12 @@ static void *lru_gen_eviction(struct folio *folio)\n \tint type = folio_is_file_lru(folio);\n \tint delta = folio_nr_pages(folio);\n \tint refs = folio_lru_refs(folio);\n-\tbool workingset = folio_test_workingset(folio);\n-\tint tier = lru_tier_from_refs(refs, workingset);\n+\tint tier = lru_tier_from_refs(refs);\n \tstruct mem_cgroup *memcg;\n \tstruct pglist_data *pgdat = folio_pgdat(folio);\n \tunsigned short memcg_id;\n \n-\tBUILD_BUG_ON(LRU_GEN_WIDTH + LRU_REFS_WIDTH \u003e\n+\tBUILD_BUG_ON(LRU_GEN_WIDTH + LRU_REFS_BITS \u003e\n \t\t BITS_PER_LONG - max(EVICTION_SHIFT, EVICTION_SHIFT_ANON));\n \n \trcu_read_lock();\n@@ -256,14 +261,14 @@ static void *lru_gen_eviction(struct folio *folio)\n \tlruvec = mem_cgroup_lruvec(memcg, pgdat);\n \tlrugen = \u0026lruvec-\u003elrugen;\n \tmin_seq = READ_ONCE(lrugen-\u003emin_seq[type]);\n-\ttoken = (min_seq \u003c\u003c LRU_REFS_WIDTH) | max(refs - 1, 0);\n+\ttoken = (min_seq \u003c\u003c LRU_REFS_BITS) | refs \u003e\u003e 1;\n \n \thist = lru_hist_from_seq(min_seq);\n \tatomic_long_add(delta, \u0026lrugen-\u003eevicted[hist][type][tier]);\n \tmemcg_id = mem_cgroup_private_id(memcg);\n \trcu_read_unlock();\n \n-\treturn pack_shadow(memcg_id, pgdat, token, workingset, type);\n+\treturn pack_shadow(memcg_id, pgdat, token, refs \u0026 1, type);\n }\n \n /*\n@@ -284,9 +289,9 @@ static bool lru_gen_test_recent(void *shadow, struct lruvec **lruvec,\n \t*lruvec = mem_cgroup_lruvec(memcg, pgdat);\n \n \tmax_seq = READ_ONCE((*lruvec)-\u003elrugen.max_seq);\n-\tmax_seq \u0026= (file ? EVICTION_MASK : EVICTION_MASK_ANON) \u003e\u003e LRU_REFS_WIDTH;\n+\tmax_seq \u0026= (file ? EVICTION_MASK : EVICTION_MASK_ANON) \u003e\u003e LRU_REFS_BITS;\n \n-\treturn abs_diff(max_seq, *token \u003e\u003e LRU_REFS_WIDTH) \u003c MAX_NR_GENS;\n+\treturn abs_diff(max_seq, *token \u003e\u003e LRU_REFS_BITS) \u003c MAX_NR_GENS;\n }\n \n static void lru_gen_refault(struct folio *folio, void *shadow)\n@@ -314,22 +319,26 @@ static void lru_gen_refault(struct folio *folio, void *shadow)\n \tlrugen = \u0026lruvec-\u003elrugen;\n \n \thist = lru_hist_from_seq(READ_ONCE(lrugen-\u003emin_seq[type]));\n-\trefs = (token \u0026 (BIT(LRU_REFS_WIDTH) - 1)) + 1;\n-\ttier = lru_tier_from_refs(refs, workingset);\n+\trefs = ((token \u0026 (BIT(LRU_REFS_BITS) - 1)) \u003c\u003c 1) + workingset;\n+\ttier = lru_tier_from_refs(refs);\n \n \tatomic_long_add(delta, \u0026lrugen-\u003erefaulted[hist][type][tier]);\n \n-\tif (workingset) {\n-\t\t/*\n-\t\t * see folio_add_lru(), where folio_set_active() is\n-\t\t * called for workingset folios\n-\t\t */\n-\t\tif (lru_gen_in_fault())\n+\t/*\n+\t * Activate a fault-driven refault folio, which would have been\n+\t * promoted had it stayed in memory.\n+\t */\n+\tif (refs \u003e= LRU_REFS_REFERENCED) {\n+\t\tif (lru_gen_in_fault()) {\n+\t\t\tfolio_set_active(folio);\n \t\t\tmod_lruvec_state(lruvec, WORKINGSET_ACTIVATE_BASE + type, delta);\n-\t\tfolio_set_workingset(folio);\n+\t\t}\n+\t\t/* Refault is also promotion, cap the refs like folio_inc_lru_refs */\n+\t\t__folio_set_lru_refs(folio, min(refs, LRU_REFS_PROTECTED));\n+\t}\n+\n+\tif (refs \u003e= LRU_REFS_WORKINGSET)\n \t\tmod_lruvec_state(lruvec, WORKINGSET_RESTORE_BASE + type, delta);\n-\t} else\n-\t\tset_mask_bits(\u0026folio-\u003eflags.f, LRU_REFS_MASK, (refs - 1UL) \u003c\u003c LRU_REFS_PGOFF);\n unlock:\n \trcu_read_unlock();\n }\n"
} |
| 2/1 | 2026/10/15 23:41 | agent | patch-evaluator |
1mModel:core Args: null Results: {
"EnableConfigs": [
"LRU_GEN",
"LRU_GEN_ENABLED",
"MEMCG"
],
"FocusSymbols": [
"folio_inc_lru_refs",
"folio_reset_lru_refs",
"folio_inc_gen",
"inc_min_seq",
"inc_max_seq",
"evict_folios",
"sort_folio",
"lru_gen_refault",
"lru_gen_eviction",
"lru_gen_seq_write",
"madvise_cold_or_pageout_pte_range",
"clear_refs_pte_range"
],
"Reasoning": "The patch modifies core memory management code by overhauling the Multi-Gen LRU (MGLRU) referenced count tracking, workingset protection, and generation promotion algorithms. It introduces new lockless generation advancement (folio_inc_lru_refs, folio_reset_lru_refs), modifies tier classification, refactors eviction sorting (sort_folio, evict_folios), and updates refault handling. These changes execute directly in standard virtualized environments under memory reclaim, page faults, madvise(MADV_COLD/MADV_PAGEOUT), and MGLRU sysfs controls (/sys/kernel/mm/lru_gen).",
"WorthFuzzing": true
}Instruction: You are an expert Linux kernel maintainer and security engineer.
Your job is to review a provided patch series and evaluate whether it warrants fuzzing with syzkaller.
IMPORTANT: The changes have ALREADY been applied and committed as the HEAD commit in
your workspace. Do NOT rely on internal assumptions. You must actively use your code access
tools to inspect the actual source code, callers, and surrounding context.
================================================================================
1. CORE TRIAGE PHILOSOPHY
================================================================================
The goal of patch fuzzing is to discover crashes, regressions, exposed latent bugs,
and newly triggered assertions introduced by the patch series.
- REACHABILITY IS THE PRIMARY GATE:
Fuzzing can only discover bugs in code that can actually execute in standard virtualized
environments (GCE or QEMU, utilizing software-emulated devices like USB gadgets, netdev, tun/tap).
If the modified code is structurally unreachable (see Section 2), it MUST NOT be fuzzed,
regardless of whether it adds assertions or complex logic.
- DO NOT BLINDLY TRUST "NO FUNCTIONAL CHANGE" (NFCI) OR "REFACTORING" CLAIMS:
Patch authors routinely label changes as "cleanups", "refactorings", or state
"No functional change intended". Do NOT take these claims at face value.
Code refactorings that rearrange logic, introduce helper functions, or alter state management
in core subsystems frequently introduce subtle semantic shifts or uncover latent kernel bugs.
If reachable executable code is modified or refactored, it MUST be fuzzed.
- NEW OR MODIFIED ASSERTIONS IN REACHABLE CODE MUST BE FUZZED:
When a patch introduces or modifies runtime checks or assertions (e.g., WARN_ON*, VM_WARN_ON*,
BUG_ON*, lockdep_assert*) in reachable code paths, it enforces new or stricter invariants.
Even if the author believes the invariant always holds, fuzzing is essential to verify whether
an unusual sequence of operations can violate it.
================================================================================
2. WHEN TO RETURN WorthFuzzing=false (NEGATIVE CRITERIA)
================================================================================
Return WorthFuzzing=false ONLY IF all modified code falls strictly into one or more of these categories:
- Non-kernel and non-executable changes:
* Modifications to Documentation/, comments, or spelling fixes.
* User-space directories, self-tests, samples, or scripts (e.g., tools/, samples/, scripts/, usr/)
that do not affect the compiled kernel image (vmlinux) or kernel modules.
* Purely decorative logging (e.g., message strings in pr_err, printk, dev_info) or tracepoints
that do not alter control flow or data structures.
* Build system or Kconfig changes that do not alter compiled C logic.
- Structurally unreachable hardware:
* Vendor-specific PCIe switches, SmartNICs, or GPU drivers (e.g., mlxsw, pds_core, qed,
ionic, amdgpu) requiring physical ASIC/PCIe cards not emulated in standard QEMU.
- Unreachable execution paths:
* Driver teardown callbacks (.remove, .shutdown, pci_unregister_driver) executed only during
physical PCI hot-unplug or manual sysfs driver unbinding.
* Code paths exclusive to architectures other than the target architecture.
================================================================================
3. WHEN TO RETURN WorthFuzzing=true (POSITIVE CRITERIA)
================================================================================
Return WorthFuzzing=true whenever the patch touches reachable executable code, including:
- Core Subsystems:
* Any logic modifications in memory management (mm/), synchronization/locking (kernel/locking/),
BPF, scheduler, core networking, VFS, or syscall handling.
- Refactorings and Code Cleanups:
* Any restructuring of reachable data structures, helper abstractions, or algorithm flows.
- Runtime Assertions and Defensive Checks:
* Any introduction or alteration of assertions (WARN_ON*, VM_WARN_ON*, BUG_ON*, etc.) in reachable paths.
- Reachable Drivers and Protocols:
* Drivers accessible via virtual buses (virtio, USB gadget, loopback, netlink, binder, sockets, etc.).
================================================================================
4. EXTRACTING FocusSymbols (PREVENTING DILUTION)
================================================================================
When WorthFuzzing=true, you must extract specific kernel functions into FocusSymbols to guide the fuzzer:
- AVOID UBIQUITOUS LIFECYCLE HOT-PATHS:
Do NOT list generic, ubiquitous functions called by almost every program in the corpus
(including, but not limited to: general memory allocators and deallocators, page fault
and trap handlers, or core synchronization primitives; this is not an exhaustive list).
Listing ubiquitous functions causes the fuzzer to classify thousands of unrelated tests as "focused",
which severely dilutes fuzzing effort away from the actual changes.
- TARGET SPECIFIC FEATURE LOGIC AND ENTRYPOINTS:
List functions that specifically implement the logic being added or altered, or direct API entrypoints
for the subsystem feature under review.
- HANDLING STATIC INLINE FUNCTIONS IN HEADERS (.h):
Compiler-inlined static functions (such as static inlines in mm/*.h or include/linux/*.h) lack
distinct symbol addresses in vmlinux and cannot be targeted directly by symbol coverage filters.
If the changes are primarily in static inline helpers, identify non-static, feature-specific caller
functions in .c files that exercise them (avoiding ubiquitous lifecycle wrappers).
================================================================================
5. IDENTIFYING EnableConfigs
================================================================================
Identify any specific CONFIG_ options required to properly compile and reach the modified code:
- Inspect Kconfig files and #ifdef guards; do not make assumptions.
- Check "depends on" lines in Kconfig to include any non-standard parent subsystem configs needed.
- Strip any 'CONFIG_' prefix (e.g., return "NET_IPV4" instead of "CONFIG_NET_IPV4").
Prefer calling several tools at the same time to save round-trips.
Use set-results tool to provide results of the analysis.
It must be called exactly once before the final reply.
Ignore results of this tool.
Prompt: Target architecture: amd64
For your convenience, here is the diff of the changes:
commit 5e17e2951caa17b8a656b8370b8228f83a262680
Author: syz-cluster <triage@syzkaller.com>
Date: Thu Oct 15 23:41:47 2026 +0000
syz-cluster: applied patch under review
diff --git a/Documentation/admin-guide/mm/multigen_lru.rst b/Documentation/admin-guide/mm/multigen_lru.rst
index 9cb54b4ff5d9d..808d2e97b3f30 100644
--- a/Documentation/admin-guide/mm/multigen_lru.rst
+++ b/Documentation/admin-guide/mm/multigen_lru.rst
@@ -149,8 +149,8 @@ generations less than or equal to ``min_gen_nr``.
``- memcg_id node_id min_gen_nr [swappiness [nr_to_reclaim]]``
``min_gen_nr`` should be less than ``max_gen_nr-1``, since
-``max_gen_nr`` and ``max_gen_nr-1`` are not fully aged (equivalent to
-the active list) and therefore cannot be evicted. ``swappiness``
+``max_gen_nr`` and ``max_gen_nr-1`` are not fully aged and therefore
+cannot be evicted. ``swappiness``
overrides the default value in ``/proc/sys/vm/swappiness`` and the valid
range is [0-200, max], with max being exclusively used for the reclamation
of anonymous memory. ``nr_to_reclaim`` limits the number of pages to evict.
diff --git a/Documentation/mm/multigen_lru.rst b/Documentation/mm/multigen_lru.rst
index 52ed5092022f1..ec8744c1f42ca 100644
--- a/Documentation/mm/multigen_lru.rst
+++ b/Documentation/mm/multigen_lru.rst
@@ -95,16 +95,19 @@ at most ``MAX_NR_GENS`` generations. The gen counter stores a value
within ``[1, MAX_NR_GENS]`` while a page is on one of
``lrugen->folios[]``; otherwise it stores zero.
-Each generation is divided into multiple tiers. A page accessed ``N``
-times through file descriptors is in tier ``order_base_2(N)``. Unlike
-generations, tiers do not have dedicated ``lrugen->folios[]``. In
-contrast to moving across generations, which requires the LRU lock,
-moving across tiers only involves atomic operations on
-``folio->flags`` and therefore has a negligible cost. A feedback loop
-modeled after the PID controller monitors refaults over all the tiers
-from anon and file types and decides which tiers from which types to
-evict or protect. The desired effect is to balance refault percentages
-between anon and file types proportional to the swappiness level.
+Each generation is divided into multiple tiers. A page's tier is
+derived from its referenced count, which counts the accesses collected
+from both channels above and is capped at ``LRU_REFS_MAX``: pages
+accessed at most once are in the lowest tier, and the tiers above hold
+pages accessed at least twice. Unlike generations, tiers do not have
+dedicated ``lrugen->folios[]``. In contrast to moving across
+generations, which requires the LRU lock, updating the referenced
+count only involves atomic operations on ``folio->flags`` and
+therefore has a negligible cost. A feedback loop modeled after the PID
+controller monitors refaults over all the tiers from anon and file
+types and decides which tiers from which types to evict or protect.
+The desired effect is to balance refault percentages between anon and
+file types proportional to the swappiness level.
There are two conceptually independent procedures: the aging and the
eviction. They form a closed-loop system, i.e., the page reclaim.
@@ -122,25 +125,31 @@ and calls ``walk_page_range()`` with each ``mm_struct`` on this list
to scan PTEs, and after each iteration, it increments ``max_seq``. For
the latter, when the eviction walks the rmap and finds a young PTE,
the aging scans the adjacent PTEs. For both, on finding a young PTE,
-the aging clears the accessed bit and updates the gen counter of the
-page mapped by this PTE to ``(max_seq%MAX_NR_GENS)+1``.
+the aging clears the accessed bit and bumps the referenced count of
+the page mapped by this PTE. A second access promotes the page by
+updating its gen counter to ``(max_seq%MAX_NR_GENS)+1``; the first
+access only moves a page one generation forward if it is at the tail
+of its LRU, i.e., in one of the two oldest generations of its type,
+so that the next time it becomes the oldest one, its accessed bit
+reflects whether it has been used again. Executable file pages are
+promoted on their first access, since reclaiming them causes typical
+I/O thrashing.
Eviction
--------
The eviction consumes old generations. Given an ``lruvec``, it
increments ``min_seq`` when ``lrugen->folios[]`` indexed by
-``min_seq%MAX_NR_GENS`` becomes empty. To select a type and a tier to
-evict from, it first compares ``min_seq[]`` to select the older type.
-If both types are equally old, it selects the one whose first tier has
-a lower refault percentage. The first tier contains single-use
-unmapped clean pages, which are the best bet. The eviction sorts a
-page according to its gen counter if the aging has found this page
-accessed through page tables and updated its gen counter. It also
-moves a page to the next generation, i.e., ``min_seq+1``, if this page
-was accessed multiple times through file descriptors and the feedback
-loop has detected outlying refaults from the tier this page is in. To
-this end, the feedback loop uses the first tier as the baseline, for
-the reason stated earlier.
+``min_seq%MAX_NR_GENS`` becomes empty. To select a type to evict from,
+it first compares ``min_seq[]`` to select the older type. If both
+types are equally old, it compares the refault percentages of all the
+tiers of each type proportional to the swappiness level. To select a
+tier to protect, it compares the refault percentage of each tier
+against that of the tiers below it. The lowest tier contains pages
+accessed at most once, which are the best bet. The eviction sorts a
+page according to its gen counter if the aging or an access has
+updated its gen counter. It also moves a page to the next generation,
+i.e., ``min_seq+1``, if the feedback loop has detected outlying
+refaults from the tier this page is in.
Working set protection
----------------------
diff --git a/fs/btrfs/compression.c b/fs/btrfs/compression.c
index c62b5148d5ac1..57d24413265e3 100644
--- a/fs/btrfs/compression.c
+++ b/fs/btrfs/compression.c
@@ -8,6 +8,7 @@
#include <linux/file.h>
#include <linux/fs.h>
#include <linux/pagemap.h>
+#include <linux/mm_inline.h>
#include <linux/folio_batch.h>
#include <linux/highmem.h>
#include <linux/kthread.h>
diff --git a/fs/erofs/zdata.c b/fs/erofs/zdata.c
index e981e371d6c29..34ee41e1ccdc9 100644
--- a/fs/erofs/zdata.c
+++ b/fs/erofs/zdata.c
@@ -6,6 +6,7 @@
*/
#include "compress.h"
#include <linux/psi.h>
+#include <linux/mm_inline.h>
#include <linux/cpuhotplug.h>
#include <trace/events/erofs.h>
@@ -1713,7 +1714,7 @@ static void z_erofs_submit_queue(struct z_erofs_frontend *f,
DBG_BUGON(bvec.bv_len < sb->s_blocksize);
}
- if (unlikely(PageWorkingset(bvec.bv_page)) &&
+ if (unlikely(folio_test_workingset(page_folio(bvec.bv_page))) &&
!memstall) {
psi_memstall_enter(&pflags);
memstall = 1;
diff --git a/fs/proc/task_mmu.c b/fs/proc/task_mmu.c
index 81c464139f8f8..55f3a35fd3aff 100644
--- a/fs/proc/task_mmu.c
+++ b/fs/proc/task_mmu.c
@@ -888,7 +888,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_young(folio) || folio_check_lru_refs(folio))
mss->referenced += size;
/*
@@ -1685,7 +1685,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;
@@ -1714,7 +1714,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/memcontrol.h b/include/linux/memcontrol.h
index 74110a324f9e2..829ab776ab449 100644
--- a/include/linux/memcontrol.h
+++ b/include/linux/memcontrol.h
@@ -1535,6 +1535,45 @@ static inline void lruvec_lock_irq(struct lruvec *lruvec)
spin_lock_irq(&lruvec->lru_lock);
}
+/**
+ * folio_lruvec_live_get - get a live lruvec for a folio under RCU
+ * @folio: the folio
+ *
+ * Computes @folio's lruvec and walks up to the nearest live ancestor
+ * if the folio's memcg is dying. Paired with folio_lruvec_live_put().
+ * The result may be stale: RCU keeps it alive but does not pin @folio
+ * to it. That is fine as the counters are fixed up on reparenting.
+ *
+ * Return: the live lruvec, with rcu_read_lock held.
+ */
+static inline struct lruvec *folio_lruvec_live_get(struct folio *folio)
+{
+#ifdef CONFIG_MEMCG
+ struct lruvec *lruvec;
+ struct pglist_data *pgdat;
+ struct mem_cgroup *memcg;
+
+ rcu_read_lock();
+ lruvec = folio_lruvec(folio);
+ pgdat = lruvec_pgdat(lruvec);
+ memcg = lruvec_memcg(lruvec);
+ while (unlikely(memcg && css_is_dying(&memcg->css))) {
+ memcg = parent_mem_cgroup(memcg);
+ lruvec = mem_cgroup_lruvec(memcg, pgdat);
+ }
+ return lruvec;
+#else
+ return folio_lruvec(folio);
+#endif
+}
+
+static inline void folio_lruvec_live_put(struct lruvec *lruvec)
+{
+#ifdef CONFIG_MEMCG
+ rcu_read_unlock();
+#endif
+}
+
static inline struct lruvec *lruvec_live_lock_irq(struct lruvec *lruvec)
{
#ifdef CONFIG_MEMCG
diff --git a/include/linux/mm_inline.h b/include/linux/mm_inline.h
index ab69b9930893f..ae50f9880da89 100644
--- a/include/linux/mm_inline.h
+++ b/include/linux/mm_inline.h
@@ -47,11 +47,10 @@ static __always_inline void __update_lru_size(struct lruvec *lruvec,
{
struct pglist_data *pgdat = lruvec_pgdat(lruvec);
- lockdep_assert_held(&lruvec->lru_lock);
WARN_ON_ONCE(nr_pages != (int)nr_pages);
mod_lruvec_state(lruvec, NR_LRU_BASE + lru, nr_pages);
- __mod_zone_page_state(&pgdat->node_zones[zid],
+ mod_zone_page_state(&pgdat->node_zones[zid],
NR_ZONE_LRU_BASE + lru, nr_pages);
}
@@ -106,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 = ((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;
+}
+
#ifdef CONFIG_LRU_GEN
static inline bool lru_gen_switching(void)
@@ -145,12 +160,13 @@ static inline int lru_hist_from_seq(unsigned long seq)
return seq % NR_HIST_GENS;
}
-static inline int lru_tier_from_refs(int refs, bool workingset)
+static inline int lru_tier_from_refs(unsigned int refs)
{
- VM_WARN_ON_ONCE(refs > BIT(LRU_REFS_WIDTH));
-
- /* see the comment on MAX_NR_TIERS */
- return workingset ? MAX_NR_TIERS - 1 : order_base_2(refs);
+ BUILD_BUG_ON(fls(LRU_REFS_MAX - 1) > MAX_NR_TIERS - 1);
+ VM_WARN_ON_ONCE(refs > LRU_REFS_MAX);
+ if (refs < LRU_REFS_WORKINGSET)
+ return 0;
+ return fls(refs - 1);
}
/**
@@ -167,42 +183,36 @@ 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)
+static inline int folio_lru_gen(const struct folio *folio)
{
- 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;
+ return lru_get_gen_flags(READ_ONCE(*const_folio_flags(folio, 0)));
}
+#endif
/**
* lru_set_refs_flags - Set the LRU referenced count to folio flags.
* @flags: pointer to the folio flags
* @refs: referenced / access count number, between 0 and LRU_REFS_MAX, inclusive.
*
- * For MGLRU, PG_referenced holds the first ref, and the extra bits hold the
- * remaining refs. For classical LRU the extra bits are not used, so it can
- * also be seen as the refs count never exceeds 1. In both cases, refs == 1
- * means PG_referenced is set and the extra bits are zero, and refs == 0 means
- * PG_referenced and the extra bits are all unset.
+ * For MGLRU, PG_referenced, PG_workingset are used as the lower two bits of
+ * refs counter, and extra bits hold the remaining higher bits. For classical
+ * LRU the extra bits are not used, and the two flags has no direct
+ * relationship with each other, but this helper can still be used to sync
+ * them. For both cases, refs == 0 means these two flags and the extra bits
+ * are all unset. And refs == 1 / 2 / 3 means PG_referenced and PG_workingset
+ * are set in an bit order way, which is more meaningful for MGLRU though.
*/
static inline void lru_set_refs_flags(unsigned long *flags, unsigned int refs)
{
VM_WARN_ON_ONCE(refs > LRU_REFS_MAX);
- BUILD_BUG_ON(LRU_REFS_MAX != (LRU_REFS_MASK >> LRU_REFS_PGOFF) + 1);
-
+ BUILD_BUG_ON(LRU_REFS_MASK & (BIT(PG_referenced) | BIT(PG_workingset)));
*flags &= ~LRU_REFS_FLAGS;
- if (!refs)
- return;
- *flags |= (BIT(PG_referenced) | ((refs - 1UL) << LRU_REFS_PGOFF));
+ if (refs & BIT(0))
+ *flags |= BIT(PG_referenced);
+ if (refs & BIT(1))
+ *flags |= BIT(PG_workingset);
+ if (LRU_REFS_WIDTH)
+ *flags |= ((unsigned long)refs >> 2) << LRU_REFS_PGOFF;
}
/**
@@ -213,13 +223,13 @@ static inline void lru_set_refs_flags(unsigned long *flags, unsigned int refs)
*/
static inline int lru_get_refs_flags(unsigned long flags)
{
- if (!(flags & BIT(PG_referenced)))
- return 0;
- /*
- * Return the total number of accesses including PG_referenced. Also see
- * the comment on LRU_REFS_FLAGS.
- */
- return ((flags & LRU_REFS_MASK) >> LRU_REFS_PGOFF) + 1;
+ int refs;
+
+ /* Return the total number of accesses. See the comment above MAX_NR_TIERS. */
+ refs = (flags & BIT(PG_referenced)) ? BIT(0) : 0;
+ refs += (flags & BIT(PG_workingset)) ? BIT(1) : 0;
+ refs += ((flags & LRU_REFS_MASK) >> LRU_REFS_PGOFF) << 2;
+ return refs;
}
static inline int folio_lru_refs(const struct folio *folio)
@@ -227,38 +237,99 @@ static inline int folio_lru_refs(const struct folio *folio)
return lru_get_refs_flags(READ_ONCE(*const_folio_flags(folio, 0)));
}
-static inline void folio_set_lru_refs(struct folio *folio, unsigned int refs)
+/**
+ * __folio_set_lru_refs - Set a folio's LRU refs.
+ * @folio: the folio
+ * @refs: the new referenced count (0 .. LRU_REFS_MAX)
+ *
+ * Set the folio's LRU refs. The folio must be off the LRU list (e.g.,
+ * isolated), or use folio_inc_lru_refs or folio_reset_lru_refs instead.
+ */
+static inline void __folio_set_lru_refs(struct folio *folio, unsigned int refs)
{
unsigned long new_flags, old_flags = READ_ONCE(*folio_flags(folio, 0));
do {
new_flags = old_flags;
+ VM_WARN_ON_ONCE(lru_get_gen_flags(old_flags) != -1);
lru_set_refs_flags(&new_flags, refs);
} while (!try_cmpxchg(folio_flags(folio, 0), &old_flags, new_flags));
}
-static inline int folio_lru_gen(const struct folio *folio)
+#ifdef CONFIG_LRU_GEN
+void folio_inc_lru_refs(struct folio *folio, unsigned int flags);
+bool folio_reset_lru_refs(struct folio *folio);
+#else
+static inline void folio_inc_lru_refs(struct folio *folio, unsigned int flags)
{
- return lru_get_gen_flags(READ_ONCE(*const_folio_flags(folio, 0)));
+ /* Should not be called with !CONFIG_LRU_GEN */
+ WARN_ON_ONCE(1);
}
-static inline bool lru_gen_is_active(const struct lruvec *lruvec, int gen)
+static inline bool folio_reset_lru_refs(struct folio *folio)
{
- unsigned long max_seq = lruvec->lrugen.max_seq;
+ return false;
+}
+#endif
- VM_WARN_ON_ONCE(gen >= MAX_NR_GENS);
+/**
+ * folio_migrate_lru_refs - copy the reference state to a new folio
+ * @new: the destination folio
+ * @old: the source folio
+ *
+ * Transfer the reference state to @new during migration: the MGLRU
+ * refs count, or PG_referenced and PG_workingset (bits 0-1 of the
+ * encoding) for the active/inactive LRU.
+ */
+static inline void folio_migrate_lru_refs(struct folio *new, const struct folio *old)
+{
+ VM_WARN_ON_ONCE_FOLIO(folio_test_lru(old), old);
+ VM_WARN_ON_ONCE_FOLIO(folio_test_lru(new), new);
+ __folio_set_lru_refs(new, folio_lru_refs(old));
+}
- /* see the comment on MIN_NR_GENS */
- return gen == lru_gen_from_seq(max_seq) || gen == lru_gen_from_seq(max_seq - 1);
+/**
+ * folio_test_workingset - Test if a folio is in the workingset.
+ * @folio: the folio
+ *
+ * A folio is workingset when its LRU refs count reaches
+ * LRU_REFS_WORKINGSET. Under the classical LRU the extra refs counter
+ * are always zero so this is just testing the PG_workingset bit.
+ * NOTE: folio_set_workingset() must not be used under MGLRU, as the
+ * folio refs are tracked by folio_inc_lru_refs() with rules.
+ *
+ * Return: true if the folio is workingset.
+ */
+static __always_inline bool folio_test_workingset(const struct folio *folio)
+{
+ return folio_lru_refs(folio) >= LRU_REFS_WORKINGSET;
}
-static inline void lru_gen_update_size(struct lruvec *lruvec, struct folio *folio,
- int old_gen, int new_gen)
+#ifdef CONFIG_LRU_GEN
+static inline bool lru_refs_is_active(unsigned int refs)
+{
+ VM_WARN_ON_ONCE(refs > LRU_REFS_MAX);
+ return refs >= LRU_REFS_ACTIVATED;
+}
+
+static inline bool lru_gen_at_tail(struct lruvec *lruvec, int type, int gen)
+{
+ unsigned long min_seq = READ_ONCE(lruvec->lrugen.min_seq[type]);
+
+ /*
+ * Consider a folio is at the tail of its LRU when it is within
+ * MIN_NR_GENS of the oldest generation of its own type
+ */
+ BUILD_BUG_ON(MIN_NR_GENS != 2);
+ return gen == lru_gen_from_seq(min_seq) ||
+ gen == lru_gen_from_seq(min_seq + 1);
+}
+
+static inline void lru_gen_update_size(struct lruvec *lruvec, int type,
+ struct folio *folio, int old_gen, int new_gen)
{
- int type = folio_is_file_lru(folio);
int zone = folio_zonenum(folio);
int delta = folio_nr_pages(folio);
- enum lru_list lru = type * LRU_INACTIVE_FILE;
struct lru_gen_folio *lrugen = &lruvec->lrugen;
VM_WARN_ON_ONCE(old_gen != -1 && old_gen >= MAX_NR_GENS);
@@ -266,36 +337,9 @@ static inline void lru_gen_update_size(struct lruvec *lruvec, struct folio *foli
VM_WARN_ON_ONCE(old_gen == -1 && new_gen == -1);
if (old_gen >= 0)
- WRITE_ONCE(lrugen->nr_pages[old_gen][type][zone],
- lrugen->nr_pages[old_gen][type][zone] - delta);
+ atomic_long_sub(delta, &lrugen->nr_pages[old_gen][type][zone]);
if (new_gen >= 0)
- WRITE_ONCE(lrugen->nr_pages[new_gen][type][zone],
- lrugen->nr_pages[new_gen][type][zone] + delta);
-
- /* addition */
- if (old_gen < 0) {
- if (lru_gen_is_active(lruvec, new_gen))
- lru += LRU_ACTIVE;
- __update_lru_size(lruvec, lru, zone, delta);
- return;
- }
-
- /* deletion */
- if (new_gen < 0) {
- if (lru_gen_is_active(lruvec, old_gen))
- lru += LRU_ACTIVE;
- __update_lru_size(lruvec, lru, zone, -delta);
- return;
- }
-
- /* promotion */
- if (!lru_gen_is_active(lruvec, old_gen) && lru_gen_is_active(lruvec, new_gen)) {
- __update_lru_size(lruvec, lru, zone, -delta);
- __update_lru_size(lruvec, lru + LRU_ACTIVE, zone, delta);
- }
-
- /* demotion requires isolation, e.g., lru_deactivate_fn() */
- VM_WARN_ON_ONCE(lru_gen_is_active(lruvec, old_gen) && !lru_gen_is_active(lruvec, new_gen));
+ atomic_long_add(delta, &lrugen->nr_pages[new_gen][type][zone]);
}
static inline unsigned long lru_gen_folio_seq(const struct lruvec *lruvec,
@@ -303,23 +347,24 @@ static inline unsigned long lru_gen_folio_seq(const struct lruvec *lruvec,
bool reclaiming)
{
int gen;
+ int refs = folio_lru_refs(folio);
int type = folio_is_file_lru(folio);
const struct lru_gen_folio *lrugen = &lruvec->lrugen;
/*
- * +-----------------------------------+-----------------------------------+
- * | Accessed through page tables and | Accessed through file descriptors |
- * | promoted by folio_update_gen() | and protected by folio_inc_gen() |
- * +-----------------------------------+-----------------------------------+
- * | PG_active (set while isolated) | |
- * +-----------------+-----------------+-----------------+-----------------+
- * | PG_workingset | PG_referenced | PG_workingset | LRU_REFS_FLAGS |
- * +-----------------------------------+-----------------------------------+
- * |<---------- MIN_NR_GENS ---------->| |
- * |<---------------------------- MAX_NR_GENS ---------------------------->|
+ * +------------------------------------------+------------------------------------------+
+ * | Accessed through page tables and | Accessed through file descriptors |
+ * | promoted by folio_inc_lru_refs_walk() | protected by folio_inc_lru_refs/inc_gen |
+ * +------------------------------------------+------------------------------------------+
+ * | PG_active (set at isolation or refault) | |
+ * +--------------------+---------------------+--------------------+---------------------+
+ * | LRU_REFS_MAX | LRU_REFS_WORKINGSET | LRU_REFS_MAX | LRU_REFS_WORKINGSET |
+ * +------------------------------------------+------------------------------------------+
+ * |<-------------- MIN_NR_GENS ------------->| |
+ * |<----------------------------------- MAX_NR_GENS ----------------------------------->|
*/
if (folio_test_active(folio))
- gen = MIN_NR_GENS - folio_test_workingset(folio);
+ gen = MIN_NR_GENS - (refs >= LRU_REFS_WORKINGSET);
else if (reclaiming)
gen = MAX_NR_GENS;
else if ((!folio_is_file_lru(folio) && !folio_test_swapcache(folio)) ||
@@ -327,7 +372,7 @@ static inline unsigned long lru_gen_folio_seq(const struct lruvec *lruvec,
(folio_test_dirty(folio) || folio_test_writeback(folio))))
gen = MIN_NR_GENS;
else
- gen = MAX_NR_GENS - (folio_test_workingset(folio) || folio_test_referenced(folio));
+ gen = MAX_NR_GENS - (refs >= LRU_REFS_WORKINGSET);
return max(READ_ONCE(lrugen->max_seq) - gen + 1, READ_ONCE(lrugen->min_seq[type]));
}
@@ -336,12 +381,15 @@ static inline bool lru_gen_add_folio(struct lruvec *lruvec, struct folio *folio,
{
unsigned long seq;
unsigned long flags;
- int gen = folio_lru_gen(folio);
+ int gen, refs;
int type = folio_is_file_lru(folio);
int zone = folio_zonenum(folio);
+ int delta = folio_nr_pages(folio);
+ enum lru_list lru = type * LRU_INACTIVE_FILE;
struct lru_gen_folio *lrugen = &lruvec->lrugen;
- VM_WARN_ON_ONCE_FOLIO(gen != -1, folio);
+ BUILD_BUG_ON(BIT(LRU_GEN_WIDTH - 1) != MAX_NR_GENS);
+ VM_WARN_ON_ONCE_FOLIO(folio_lru_gen(folio) != -1, folio);
if (folio_test_unevictable(folio) || !lrugen->enabled)
return false;
@@ -350,9 +398,15 @@ static inline bool lru_gen_add_folio(struct lruvec *lruvec, struct folio *folio,
gen = lru_gen_from_seq(seq);
flags = (gen + 1UL) << LRU_GEN_PGOFF;
/* see the comment on MIN_NR_GENS about PG_active */
- set_mask_bits(folio_flags(folio, 0), LRU_GEN_MASK | BIT(PG_active), flags);
+ flags = set_mask_bits(folio_flags(folio, 0), LRU_GEN_MASK | BIT(PG_active), flags);
+ /* use the refs from the atomic snapshot to avoid raced update */
+ refs = lru_get_refs_flags(flags);
+
+ lru_gen_update_size(lruvec, type, folio, -1, gen);
+ if (lru_refs_is_active(refs))
+ lru += LRU_ACTIVE;
+ __update_lru_size(lruvec, lru, zone, delta);
- lru_gen_update_size(lruvec, folio, -1, gen);
/* for folio_rotate_reclaimable() */
if (reclaiming)
list_add_tail(&folio->lru, &lrugen->folios[gen][type][zone]);
@@ -365,39 +419,38 @@ static inline bool lru_gen_add_folio(struct lruvec *lruvec, struct folio *folio,
static inline bool lru_gen_del_folio(struct lruvec *lruvec, struct folio *folio, bool reclaiming)
{
unsigned long flags;
- int gen = folio_lru_gen(folio);
+ int gen, refs;
+ int type = folio_is_file_lru(folio);
+ int zone = folio_zonenum(folio);
+ int delta = folio_nr_pages(folio);
+ enum lru_list lru = folio_is_file_lru(folio) * LRU_INACTIVE_FILE;
+ unsigned long max_seq = READ_ONCE(lruvec->lrugen.max_seq);
+ flags = set_mask_bits(folio_flags(folio, 0), LRU_GEN_MASK, 0);
+ gen = lru_get_gen_flags(flags);
+ refs = lru_get_refs_flags(flags);
if (gen < 0)
return false;
VM_WARN_ON_ONCE_FOLIO(folio_test_active(folio), folio);
VM_WARN_ON_ONCE_FOLIO(folio_test_unevictable(folio), folio);
- /* for folio_migrate_flags() */
- flags = !reclaiming && lru_gen_is_active(lruvec, gen) ? BIT(PG_active) : 0;
- flags = set_mask_bits(folio_flags(folio, 0), LRU_GEN_MASK, flags);
- gen = ((flags & LRU_GEN_MASK) >> LRU_GEN_PGOFF) - 1;
+ /*
+ * See the comment in lru_gen_folio_seq. For migration, compaction,
+ * or any other isolation of a hot folio, try best to retain its gen
+ * info. Ideally we would keep the full gen info.
+ */
+ if (!reclaiming && ((max_seq - gen) % MAX_NR_GENS) < MIN_NR_GENS)
+ folio_set_active(folio);
- lru_gen_update_size(lruvec, folio, gen, -1);
+ lru_gen_update_size(lruvec, type, folio, gen, -1);
+ if (lru_refs_is_active(refs))
+ lru += LRU_ACTIVE;
+ __update_lru_size(lruvec, lru, zone, -delta);
list_del(&folio->lru);
return true;
}
-
-/**
- * folio_migrate_lru_refs - copy the reference state to a new folio
- * @new: the destination folio
- * @old: the source folio
- *
- * Transfer the reference state to @new during migration: the MGLRU
- * refs count, including PG_referenced, or just PG_referenced for the
- * active/inactive LRU.
- */
-static inline void folio_migrate_lru_refs(struct folio *new, const struct folio *old)
-{
- BUILD_BUG_ON(LRU_REFS_MASK & BIT(PG_referenced));
- folio_set_lru_refs(new, folio_lru_refs(old));
-}
#else /* !CONFIG_LRU_GEN */
static inline bool lru_gen_enabled(void)
@@ -424,13 +477,76 @@ static inline bool lru_gen_del_folio(struct lruvec *lruvec, struct folio *folio,
{
return false;
}
+#endif /* CONFIG_LRU_GEN */
-static inline void folio_migrate_lru_refs(struct folio *new, const struct folio *old)
+/**
+ * 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 the classical LRU, a plain PG_referenced set.
+ */
+static __always_inline void folio_inc_lru_refs_fast(struct folio *folio)
{
- if (folio_test_referenced(old))
- folio_set_referenced(new);
+ unsigned long new_flags, old_flags;
+
+ if (!lru_gen_enabled()) {
+ folio_set_referenced(folio);
+ return;
+ }
+
+ BUILD_BUG_ON(LRU_REFS_REFERENCED >= LRU_REFS_ACTIVATED);
+ old_flags = READ_ONCE(*folio_flags(folio, 0));
+ do {
+ if (lru_get_refs_flags(old_flags))
+ break;
+ new_flags = old_flags;
+ lru_set_refs_flags(&new_flags, LRU_REFS_REFERENCED);
+ } while (!try_cmpxchg(folio_flags(folio, 0), &old_flags, new_flags));
+}
+
+/**
+ * folio_check_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. Kept as a separate helper,
+ * to avoid splattering the lru_gen_enabled() static key.
+ */
+static inline bool folio_check_lru_refs(const struct folio *folio)
+{
+ 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. Kept as a separate helper
+ * just like folio_check_lru_refs().
+ */
+static inline void folio_clear_lru_refs(struct folio *folio)
+{
+ if (lru_gen_enabled())
+ folio_reset_lru_refs(folio);
+ else
+ folio_clear_referenced(folio);
+}
+
+/*
+ * 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());
+ set_bit(PG_workingset, folio_flags(folio, FOLIO_HEAD_PAGE));
}
-#endif /* CONFIG_LRU_GEN */
static __always_inline
void lruvec_add_folio(struct lruvec *lruvec, struct folio *folio)
diff --git a/include/linux/mmzone.h b/include/linux/mmzone.h
index 3b96d6c7123b4..f50449d102423 100644
--- a/include/linux/mmzone.h
+++ b/include/linux/mmzone.h
@@ -434,15 +434,15 @@ enum lruvec_flags {
* least twice before handing this folio over to the eviction. The first check
* clears the accessed bit from the initial fault; the second check makes sure
* this folio hasn't been used since then. This process, AKA second chance,
- * requires a minimum of two generations, hence MIN_NR_GENS. And to maintain ABI
- * compatibility with the active/inactive LRU, e.g., /proc/vmstat, these two
- * generations are considered active; the rest of generations, if they exist,
- * are considered inactive. See lru_gen_is_active().
+ * requires a minimum of two generations, hence MIN_NR_GENS.
+ *
+ * Active/inactive is per-folio based on the referenced count;
+ * see the comment above MAX_NR_TIERS.
*
* PG_active is always cleared while a folio is on one of lrugen->folios[] so
- * that the sliding window needs not to worry about it. And it's set again when
- * a folio considered active is isolated for non-reclaiming purposes, e.g.,
- * migration. See lru_gen_add_folio() and lru_gen_del_folio().
+ * that the sliding window needs not to worry about it. It is set on a
+ * fault-driven refault (see lru_gen_refault), or isolation (see
+ * lru_gen_del_folio).
*
* MAX_NR_GENS is set to 4 so that the multi-gen LRU can support twice the
* number of categories of the active/inactive LRU when keeping track of
@@ -453,57 +453,128 @@ enum lruvec_flags {
#define MAX_NR_GENS 4U
/*
- * Each generation is divided into multiple tiers. A folio accessed N times
- * through file descriptors is in tier order_base_2(N). A folio in the first
- * tier (N=0,1) is marked by PG_referenced unless it was faulted in through page
- * tables or read ahead. A folio in the last tier (MAX_NR_TIERS-1) is marked by
- * PG_workingset. A folio in any other tier (1<N<5) between the first and last
- * is marked by additional bits of LRU_REFS_WIDTH in folio->flags.
+ * Each generation is divided into multiple tiers. A folio's referenced
+ * count maps to a tier as shown below:
*
- * In contrast to moving across generations which requires the LRU lock, moving
- * across tiers only involves atomic operations on folio->flags and therefore
- * has a negligible cost in the buffered access path. In the eviction path,
- * comparisons of refaulted/(evicted+protected) from the first tier and the rest
- * infer whether folios accessed multiple times through file descriptors are
- * statistically hot and thus worth protecting.
+ * MGLRU (frequency guidance)
+ * Refs Tier |- Refs: how many times (at least) a folio has been referenced.
+ * 0 0 |- Mostly cold pages, readahead, etc. [1]
+ * 1 0 |= LRU_REFS_REFERENCED: Used at least once. [2]
+ * -WORKINGSET-+|- Pages beyond are workingset and never fall below this floor. [3]
+ * 2 1<-+|= LRU_REFS_WORKINGSET: Classical workingset, accessed twice, protected. [4]
+ * 3 2 |- LRU_REFS_PROTECTED: Protected workingset, promoted pages capped at here. [5]
+ * 4 2 |
+ * 5 3 |- The tier here is MAX_NR_TIERS - 1
+ * 6 3 |
+ * 7 3 |= LRU_REFS_MAX: Promotion candidate. [6]
+ * -PROMOTION->-/
*
- * MAX_NR_TIERS is set to 4 so that the multi-gen LRU can support twice the
- * number of categories of the active/inactive LRU when keeping track of
- * accesses through file descriptors. This uses MAX_NR_TIERS-2 spare bits in
- * folio->flags, masked by LRU_REFS_MASK.
+ * Ideally each tier holds folios of similar access patterns: lower tiers
+ * are less important and evicted faster. A page's reference count and
+ * tier are capped when it changes generation, preventing it from
+ * dominating the new generation based on old-generation access history.
+ * Generation ordering already ensures a newer-gen page is hotter than an
+ * older-gen one regardless of tier.
+ *
+ * Refs tracks accesses from two sources: page table (lazily collected by
+ * the page table aging walk or rmap eviction lookup) and file descriptors
+ * (by folio_mark_accessed). Page table accesses are weighted heavier
+ * because the accessed bit is sticky (undercounts repeated accesses),
+ * passively collected, and page faults are generally more important as
+ * userspace does not expect a memory access to block on reclaim. Both
+ * access types increment refs by one; the result is capped at
+ * LRU_REFS_PROTECTED on promotion or deferral, or LRU_REFS_MAX otherwise.
+ *
+ * 1. Tier is fls(N-1) for N > 1, 0 otherwise. Folios with zero
+ * accesses (refs == 0) are generally cold, e.g. readahead folios.
+ *
+ * Freshly allocated folios start with refs == 0; faulted and mapped
+ * folios have their page table access bit set, so the first page table
+ * access check always sets LRU_REFS_REFERENCED. That is the second
+ * chance described above MIN_NR_GENS: it moves the folio one
+ * generation forward if it is at the tail of its LRU, i.e., in one
+ * of the two oldest generations of its type.
+ *
+ * 2. Folios accessed once stay on tier 0: one-time usage does not
+ * qualify for protection. A second access advances the folio,
+ * aligning with classical LRU's use-twice threshold. A second page
+ * table access promotes to the latest gen; file access only defers
+ * eviction from the tail gens of its type.
+ *
+ * 3. Folios accessed at least twice are considered workingset. This
+ * mostly aligns with classical LRU: at least one I/O is saved by
+ * keeping them in memory. Folios at or above this level never fall
+ * below tier 1 (the workingset floor), so tier 0 stays a clean tier
+ * for cold cache while tier 1 serves as the fallback line for
+ * actually reused or historically hot folios.
+ *
+ * Folios refaulted through a page fault at refs 1 will enter the second
+ * newest gen, so faulting will be protected better.
+ *
+ * 4. Starting from tier 1, PID protection sacrifices lower tiers to
+ * protect higher tiers by comparing refault rates for long-term
+ * accuracy, and caps higher refs to this value. Since PID protection
+ * bypasses page table lookup and clearing, when a further eviction
+ * attempt occurs after PID loosens, the folio's page table access is
+ * rechecked and a referenced folio is put back with refs capped to
+ * this value as well. This also gives folios a fair opportunity to be
+ * promoted by file access again.
+ *
+ * Folios refaulted through a page fault at tier 1 or above are activated
+ * and enter the newest gen. Non fault page will enter second oldest gen,
+ * driving aging and workingset shifting.
+ *
+ * 5. Pages beyond the ordinary workingset tier form new tiers for the
+ * PID controller to protect differently. Folios at or above this
+ * level are capped at LRU_REFS_PROTECTED on promotion or deferral,
+ * and at LRU_REFS_WORKINGSET under PID protection in the oldest
+ * generation, where they represent a historical workingset.
+ *
+ * 6. Folios that reach LRU_REFS_MAX are advanced to the next generation
+ * on further access, with refs capped to LRU_REFS_PROTECTED. This
+ * gives them a fair start for advancement to an even newer generation
+ * while keeping hot folios distinguishable.
+ *
+ * Tiering uses PG_referenced and PG_workingset as the lower two bits,
+ * and the bits masked by LRU_REFS_MASK as the higher bits, so the refs
+ * count ranges from 0 to LRU_REFS_MAX. A folio is on the workingset
+ * tier once accessed at least twice, which is more consistent with the
+ * classical LRU.
+ *
+ * A folio's referenced count never goes backwards except upon gen
+ * increase as described above, or when explicitly reset by
+ * folio_reset_lru_refs(). Refault of a reclaimed folio restores
+ * its referenced count, capped at LRU_REFS_PROTECTED, which aligns with
+ * promotion. Page table refaults of previous workingset folios send
+ * them to the latest gen, driving aging faster.
+ *
+ * For the active/inactive LRU ABI (/proc/vmstat), a folio is active
+ * if its referenced count reaches LRU_REFS_ACTIVATED. The threshold
+ * currently set to LRU_REFS_PROTECTED, which is tier 2.
+ *
+ * MAX_NR_TIERS is set to 4 so that the multi-gen LRU can support twice
+ * the number of categories of the active/inactive LRU.
*/
#define MAX_NR_TIERS 4U
#define LRU_TIER_MIN 0U
#define LRU_TIER_MAX (MAX_NR_TIERS - 1)
+/* Flags for folio_inc_lru_refs() */
+#define LRU_REF_MAPPED 0x1U
+#define LRU_REF_EXEC 0x2U
+#define LRU_REF_FORCE 0x4U
+
+#define LRU_REFS_REFERENCED 0x1
+#define LRU_REFS_WORKINGSET 0x2
+#define LRU_REFS_PROTECTED 0x3
+#define LRU_REFS_ACTIVATED LRU_REFS_PROTECTED
+
#ifndef __GENERATING_BOUNDS_H
#define LRU_GEN_MASK ((BIT(LRU_GEN_WIDTH) - 1) << LRU_GEN_PGOFF)
#define LRU_REFS_MASK ((BIT(LRU_REFS_WIDTH) - 1) << LRU_REFS_PGOFF)
-#define LRU_REFS_MAX BIT(LRU_REFS_WIDTH)
-
-/*
- * For folios accessed multiple times through file descriptors,
- * lru_gen_inc_refs() sets additional bits of LRU_REFS_WIDTH in folio->flags
- * after PG_referenced, then PG_workingset after LRU_REFS_WIDTH. After all its
- * bits are set, i.e., LRU_REFS_FLAGS|BIT(PG_workingset), a folio is lazily
- * promoted into the second oldest generation in the eviction path. And when
- * folio_inc_gen() does that, it clears LRU_REFS_FLAGS so that
- * lru_gen_inc_refs() can start over. Note that for this case, LRU_REFS_MASK is
- * only valid when PG_referenced is set.
- *
- * For folios accessed multiple times through page tables, folio_update_gen()
- * from a page table walk or lru_gen_set_refs() from a rmap walk sets
- * PG_referenced after the accessed bit is cleared for the first time.
- * Thereafter, those two paths set PG_workingset and promote folios to the
- * youngest generation. Like folio_inc_gen(), folio_update_gen() also clears
- * PG_referenced. Note that for this case, LRU_REFS_MASK is not used.
- *
- * For both cases above, after PG_workingset is set on a folio, it remains until
- * this folio is either reclaimed, or "deactivated" by lru_gen_clear_refs(). It
- * can be set again if lru_gen_test_recent() returns true upon a refault.
- */
-#define LRU_REFS_FLAGS (LRU_REFS_MASK | BIT(PG_referenced))
+#define LRU_REFS_FLAGS (LRU_REFS_MASK | BIT(PG_referenced) | BIT(PG_workingset))
+#define LRU_REFS_MAX (BIT(LRU_REFS_WIDTH + 2) - 1)
struct lruvec;
struct page_vma_mapped_walk;
@@ -554,7 +625,7 @@ struct lru_gen_folio {
/* the multi-gen LRU lists, lazily sorted on eviction */
struct list_head folios[MAX_NR_GENS][ANON_AND_FILE][MAX_NR_ZONES];
/* the multi-gen LRU sizes, eventually consistent */
- long nr_pages[MAX_NR_GENS][ANON_AND_FILE][MAX_NR_ZONES];
+ atomic_long_t nr_pages[MAX_NR_GENS][ANON_AND_FILE][MAX_NR_ZONES];
/* the exponential moving average of refaulted */
unsigned long avg_refaulted[ANON_AND_FILE][MAX_NR_TIERS];
/* the exponential moving average of evicted+protected */
@@ -605,6 +676,8 @@ struct lru_gen_mm_walk {
unsigned long seq;
/* the next address within an mm to scan */
unsigned long next_addr;
+ /* to batch activated pages */
+ int nr_activated[ANON_AND_FILE][MAX_NR_ZONES];
/* to batch promoted pages */
int nr_pages[MAX_NR_GENS][ANON_AND_FILE][MAX_NR_ZONES];
/* to batch the mm stats */
diff --git a/include/linux/page-flags.h b/include/linux/page-flags.h
index b0ddc652e76cc..f6625dc042dab 100644
--- a/include/linux/page-flags.h
+++ b/include/linux/page-flags.h
@@ -549,8 +549,6 @@ PAGEFLAG(LRU, lru, PF_HEAD) __CLEARPAGEFLAG(LRU, lru, PF_HEAD)
FOLIO_FLAG(active, FOLIO_HEAD_PAGE)
__FOLIO_CLEAR_FLAG(active, FOLIO_HEAD_PAGE)
FOLIO_TEST_CLEAR_FLAG(active, FOLIO_HEAD_PAGE)
-PAGEFLAG(Workingset, workingset, PF_HEAD)
- TESTCLEARFLAG(Workingset, workingset, PF_HEAD)
PAGEFLAG(Checked, checked, PF_NO_COMPOUND) /* Used by some filesystems */
/* Xen */
diff --git a/kernel/bounds.c b/kernel/bounds.c
index 02b619eb61065..06a034713b5dd 100644
--- a/kernel/bounds.c
+++ b/kernel/bounds.c
@@ -25,7 +25,7 @@ int main(void)
DEFINE(SPINLOCK_SIZE, sizeof(spinlock_t));
#ifdef CONFIG_LRU_GEN
DEFINE(LRU_GEN_WIDTH, order_base_2(MAX_NR_GENS + 1));
- DEFINE(__LRU_REFS_WIDTH, MAX_NR_TIERS - 2);
+ DEFINE(__LRU_REFS_WIDTH, MAX_NR_TIERS - 3);
#else
DEFINE(LRU_GEN_WIDTH, 0);
DEFINE(__LRU_REFS_WIDTH, 0);
diff --git a/mm/damon/paddr.c b/mm/damon/paddr.c
index 2cfdc356b4157..6412ff2396b3f 100644
--- a/mm/damon/paddr.c
+++ b/mm/damon/paddr.c
@@ -279,7 +279,8 @@ static unsigned long damon_pa_pageout(struct damon_region *r,
else
*sz_filter_passed += folio_size(folio) / addr_unit;
- folio_clear_referenced(folio);
+ /* The regions is cold, better drop the hotness info */
+ folio_clear_lru_refs(folio);
folio_test_clear_young(folio);
if (!folio_isolate_lru(folio))
goto put_folio;
diff --git a/mm/filemap.c b/mm/filemap.c
index b74bc1e5015c6..49e827058e147 100644
--- a/mm/filemap.c
+++ b/mm/filemap.c
@@ -21,6 +21,7 @@
#include <linux/gfp.h>
#include <linux/mm.h>
#include <linux/swap.h>
+#include <linux/mm_inline.h>
#include <linux/leafops.h>
#include <linux/syscalls.h>
#include <linux/mman.h>
diff --git a/mm/folio.c b/mm/folio.c
index 35e242b48870b..9b9a5fe568c98 100644
--- a/mm/folio.c
+++ b/mm/folio.c
@@ -273,7 +273,6 @@ static void lru_activate(struct lruvec *lruvec, struct folio *folio)
if (folio_test_active(folio) || folio_test_unevictable(folio))
return;
-
lruvec_del_folio(lruvec, folio);
folio_set_active(folio);
lruvec_add_folio(lruvec, folio);
@@ -350,66 +349,6 @@ static void __lru_cache_activate_folio(struct folio *folio)
local_unlock(&cpu_fbatches.lock);
}
-#ifdef CONFIG_LRU_GEN
-
-static void lru_gen_inc_refs(struct folio *folio)
-{
- unsigned long new_flags, old_flags = READ_ONCE(*folio_flags(folio, 0));
- int refs;
-
- if (folio_test_unevictable(folio))
- return;
-
- /* see the comment on LRU_REFS_FLAGS */
- if (!folio_lru_refs(folio)) {
- folio_set_lru_refs(folio, 1);
- return;
- }
-
- do {
- new_flags = old_flags;
- refs = lru_get_refs_flags(old_flags);
- if (refs == LRU_REFS_MAX) {
- if (!folio_test_workingset(folio))
- folio_set_workingset(folio);
- return;
- }
- lru_set_refs_flags(&new_flags, refs + 1);
- } while (!try_cmpxchg(folio_flags(folio, 0), &old_flags, new_flags));
-}
-
-static bool lru_gen_clear_refs(struct folio *folio)
-{
- int gen = folio_lru_gen(folio);
- int type = folio_is_file_lru(folio);
- unsigned long seq;
-
- if (gen < 0)
- return true;
-
- folio_set_lru_refs(folio, 0);
- folio_clear_workingset(folio);
-
- rcu_read_lock();
- seq = READ_ONCE(folio_lruvec(folio)->lrugen.min_seq[type]);
- rcu_read_unlock();
- /* whether can do without shuffling under the LRU lock */
- return gen == lru_gen_from_seq(seq);
-}
-
-#else /* !CONFIG_LRU_GEN */
-
-static void lru_gen_inc_refs(struct folio *folio)
-{
-}
-
-static bool lru_gen_clear_refs(struct folio *folio)
-{
- return false;
-}
-
-#endif /* CONFIG_LRU_GEN */
-
/**
* folio_mark_accessed - Mark a folio as having seen activity.
* @folio: The folio to mark.
@@ -428,7 +367,8 @@ void folio_mark_accessed(struct folio *folio)
if (folio_test_dropbehind(folio))
return;
if (lru_gen_enabled()) {
- lru_gen_inc_refs(folio);
+ if (!folio_test_unevictable(folio))
+ folio_inc_lru_refs(folio, 0);
return;
}
@@ -474,21 +414,6 @@ void folio_add_lru(struct folio *folio)
folio_test_unevictable(folio), folio);
VM_BUG_ON_FOLIO(folio_test_lru(folio), folio);
- /*
- * For refaulted workingset folios, set PG_active so they
- * can be added to active generations.
- * For prefaulted file folios, folio_mark_accessed() sets
- * PG_referenced so lru_gen_folio_seq() places them into
- * the second oldest generation.
- */
- if (lru_gen_enabled() && !folio_test_unevictable(folio) &&
- lru_gen_in_fault() && !(current->flags & PF_MEMALLOC)) {
- if (folio_test_workingset(folio))
- folio_set_active(folio);
- else if (!folio_test_referenced(folio))
- folio_mark_accessed(folio);
- }
-
folio_batch_add_and_move(folio, lru_add);
}
EXPORT_SYMBOL(folio_add_lru);
@@ -600,7 +525,7 @@ static void lru_lazyfree(struct lruvec *lruvec, struct folio *folio)
lruvec_del_folio(lruvec, folio);
folio_clear_active(folio);
if (lru_gen_enabled())
- lru_gen_clear_refs(folio);
+ __folio_set_lru_refs(folio, 0);
else
folio_clear_referenced(folio);
/*
@@ -673,7 +598,7 @@ void deactivate_file_folio(struct folio *folio)
if (folio_test_unevictable(folio) || !folio_test_lru(folio))
return;
- if (lru_gen_enabled() && lru_gen_clear_refs(folio))
+ if (lru_gen_enabled() && !folio_reset_lru_refs(folio))
return;
folio_batch_add_and_move(folio, lru_deactivate_file);
@@ -692,7 +617,7 @@ void folio_deactivate(struct folio *folio)
if (folio_test_unevictable(folio) || !folio_test_lru(folio))
return;
- if (lru_gen_enabled() ? lru_gen_clear_refs(folio) : !folio_test_active(folio))
+ if (lru_gen_enabled() ? !folio_reset_lru_refs(folio) : !folio_test_active(folio))
return;
folio_batch_add_and_move(folio, lru_deactivate);
diff --git a/mm/gup.c b/mm/gup.c
index f166acf794e30..b3f563d411923 100644
--- a/mm/gup.c
+++ b/mm/gup.c
@@ -2896,7 +2896,7 @@ static unsigned long gup_fast_pte_range(pmd_t pmd, pmd_t *pmdp,
gup_put_folio(folio, 1, flags);
goto pte_unmap;
}
- folio_set_referenced(folio);
+ folio_inc_lru_refs_fast(folio);
pages[nr_pages] = page;
nr_pages++;
} while (ptep++, addr += PAGE_SIZE, addr != end);
@@ -2965,7 +2965,7 @@ static unsigned long gup_fast_pmd_leaf(pmd_t orig, pmd_t *pmdp,
for (i = 0; i < nr_pages; i++)
*(pages++) = page++;
- folio_set_referenced(folio);
+ folio_inc_lru_refs_fast(folio);
return nr_pages;
}
@@ -3007,7 +3007,7 @@ static unsigned long gup_fast_pud_leaf(pud_t orig, pud_t *pudp,
for (i = 0; i < nr_pages; i++)
*(pages++) = page++;
- folio_set_referenced(folio);
+ folio_inc_lru_refs_fast(folio);
return nr_pages;
}
diff --git a/mm/huge_memory.c b/mm/huge_memory.c
index bd44d58e86845..1e2b6448c0102 100644
--- a/mm/huge_memory.c
+++ b/mm/huge_memory.c
@@ -3093,8 +3093,8 @@ static void __split_huge_pud_locked(struct vm_area_struct *vma, pud_t *pud,
if (!folio_test_dirty(folio) && pud_dirty(old_pud))
folio_mark_dirty(folio);
- if (!folio_test_referenced(folio) && pud_young(old_pud))
- folio_set_referenced(folio);
+ if (pud_young(old_pud) && vma_has_recency(vma))
+ folio_mark_accessed(folio);
folio_remove_rmap_pud(folio, page, vma);
add_mm_counter(vma->vm_mm, mm_counter_file(folio),
-HPAGE_PUD_NR);
@@ -3216,8 +3216,8 @@ static void __split_huge_pmd_locked(struct vm_area_struct *vma, pmd_t *pmd,
folio = page_folio(page);
if (!folio_test_dirty(folio) && pmd_dirty(old_pmd))
folio_mark_dirty(folio);
- if (!folio_test_referenced(folio) && pmd_young(old_pmd))
- folio_set_referenced(folio);
+ if (pmd_young(old_pmd) && vma_has_recency(vma))
+ folio_mark_accessed(folio);
folio_remove_rmap_pmd(folio, page, vma);
add_mm_counter(mm, mm_counter_file(folio), -HPAGE_PMD_NR);
folio_put(folio);
diff --git a/mm/khugepaged.c b/mm/khugepaged.c
index ef5cd6fda34f7..5332b4c51fec7 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;
/* ... or the PTE mapping was recently used */
return pte_young(pteval) || mmu_notifier_test_young(vma->vm_mm, addr);
diff --git a/mm/madvise.c b/mm/madvise.c
index 75b409e97dbf2..184611fdd6f9d 100644
--- a/mm/madvise.c
+++ b/mm/madvise.c
@@ -360,6 +360,45 @@ static inline int madvise_folio_pte_batch(unsigned long addr, unsigned long end,
FPB_MERGE_YOUNG_DIRTY);
}
+static void madvise_cold_prep_folio(struct folio *folio)
+{
+ /*
+ * VM couldn't reclaim the folio unless we clear PG_young.
+ * As a side effect, it makes confuse idle-page tracking
+ * because they will miss recent referenced history.
+ */
+ folio_test_clear_young(folio);
+
+ /*
+ * For the active/inactive LRU, a folio demoted out of the active
+ * list should have PG_workingset so its refault is still accounted
+ * as a workingset refault, and the referenced bit always needs to
+ * be cleared even for inactive folios. Nothing to do for MGLRU as
+ * folio_deactivate() clears the refs count through
+ * folio_reset_lru_refs().
+ */
+ if (!lru_gen_enabled()) {
+ folio_clear_referenced(folio);
+ if (folio_test_active(folio))
+ folio_set_workingset(folio);
+ }
+}
+
+static void madvise_pageout_prep_folio(struct folio *folio)
+{
+ int refs = folio_lru_refs(folio);
+
+ /*
+ * For MGLRU, drop the count below LRU_REFS_WORKINGSET and cap
+ * the rest at it: MADV is
+ * suggesting the folio is actually cold, so the eviction shadow
+ * should not record its hotness in the high tier counters, but
+ * the workingset classification is preserved for PSI.
+ */
+ if (lru_gen_enabled())
+ __folio_set_lru_refs(folio, refs >= LRU_REFS_WORKINGSET ? LRU_REFS_WORKINGSET : 0);
+}
+
static int madvise_cold_or_pageout_pte_range(pmd_t *pmd,
unsigned long addr, unsigned long end,
struct mm_walk *walk)
@@ -434,12 +473,10 @@ static int madvise_cold_or_pageout_pte_range(pmd_t *pmd,
tlb_remove_pmd_tlb_entry(tlb, pmd, addr);
}
- folio_clear_referenced(folio);
- folio_test_clear_young(folio);
- if (folio_test_active(folio))
- folio_set_workingset(folio);
+ madvise_cold_prep_folio(folio);
if (pageout) {
if (folio_isolate_lru(folio)) {
+ madvise_pageout_prep_folio(folio);
if (folio_test_unevictable(folio))
folio_putback_lru(folio);
else
@@ -544,18 +581,10 @@ static int madvise_cold_or_pageout_pte_range(pmd_t *pmd,
tlb_remove_tlb_entries(tlb, pte, nr, addr);
}
- /*
- * We are deactivating a folio for accelerating reclaiming.
- * VM couldn't reclaim the folio unless we clear PG_young.
- * As a side effect, it makes confuse idle-page tracking
- * because they will miss recent referenced history.
- */
- folio_clear_referenced(folio);
- folio_test_clear_young(folio);
- if (folio_test_active(folio))
- folio_set_workingset(folio);
+ madvise_cold_prep_folio(folio);
if (pageout) {
if (folio_isolate_lru(folio)) {
+ madvise_pageout_prep_folio(folio);
if (folio_test_unevictable(folio))
folio_putback_lru(folio);
else
diff --git a/mm/migrate.c b/mm/migrate.c
index 7bdcdb57652f8..5eefd8547f70e 100644
--- a/mm/migrate.c
+++ b/mm/migrate.c
@@ -786,8 +786,6 @@ void folio_migrate_flags(struct folio *newfolio, struct folio *folio)
folio_set_active(newfolio);
} else if (folio_test_clear_unevictable(folio))
folio_set_unevictable(newfolio);
- if (folio_test_workingset(folio))
- folio_set_workingset(newfolio);
if (folio_test_checked(folio))
folio_set_checked(newfolio);
/*
diff --git a/mm/page_io.c b/mm/page_io.c
index c6824fcd483e0..540ae8caa932d 100644
--- a/mm/page_io.c
+++ b/mm/page_io.c
@@ -16,6 +16,7 @@
#include <linux/gfp.h>
#include <linux/pagemap.h>
#include <linux/swap.h>
+#include <linux/mm_inline.h>
#include <linux/bio.h>
#include <linux/swapops.h>
#include <linux/writeback.h>
diff --git a/mm/vmscan.c b/mm/vmscan.c
index daa7e37c2236a..e604b0893847e 100644
--- a/mm/vmscan.c
+++ b/mm/vmscan.c
@@ -931,38 +931,257 @@ enum folio_references {
};
#ifdef CONFIG_LRU_GEN
+/******************************************************************************
+ * Referenced count feedback
+ ******************************************************************************/
+
/*
- * Only used on a mapped folio in the eviction (rmap walk) path, where promotion
- * needs to be done by taking the folio off the LRU list and then adding it back
- * with PG_active set. In contrast, the aging (page table walk) path uses
- * folio_update_gen().
+ * The folio_inc_lru_refs{_*} helpers below collect the referenced info
+ * (hotness) from other parts, including the page table walker, the rmap walk
+ * upon eviction, the rmap lookaround, and file descriptors
+ * (folio_mark_accessed).
+ *
+ * Page table accesses use refs from either source. The first reference
+ * advances tail-gen folios by one generation; a second promotes to the
+ * newest. Executable file folios promote on their first access to avoid
+ * I/O thrashing. Isolated rmap accesses use evict_folios() for putback.
+ *
+ * Promotion changes folio->flags; sort_folio() or inc_min_seq() moves the
+ * folio later. Young PTEs found during unmapping use folio_mark_accessed()
+ * and follow the file descriptor rules below.
+ *
+ * File descriptor accesses do not promote. They only defer eviction from
+ * the tail gens of their type, and only once the folio is a workingset
+ * folio (LRU_REFS_WORKINGSET), leaving the rest to PID protection. Page
+ * table accesses are treated more generously because the accessed bit is
+ * sticky (it under-counts repeated accesses) and because a page fault is
+ * more costly than file descriptor I/O.
+ *
+ * PID protection operates on tier > 0 folios. The one proactive promotion
+ * outside of it and the page table path is the overflow case where the
+ * referenced count exceeds LRU_REFS_MAX, which means the folio is hotter
+ * than everything else in its generation.
+ *
+ * Whenever a folio changes generation here its referenced count is capped at
+ * LRU_REFS_PROTECTED, so it starts at or below the protected tier regardless
+ * of its old-generation access history. PID protection (folio_inc_gen) caps
+ * at LRU_REFS_WORKINGSET independently.
+ *
+ * folio_inc_lru_refs() retries with LRU_REF_FORCE if the aging recycled the
+ * gen index it was about to store: newest gen and an extra reference.
*/
-static bool lru_gen_set_refs(struct folio *folio, const vma_flags_t *vma_flags)
-{
- /* see the comment on LRU_REFS_FLAGS */
- if (!folio_test_referenced(folio) && !folio_test_workingset(folio)) {
- /* Activate file-backed executable folios after first usage. */
- if (is_exec_file_folio(folio, vma_flags)) {
- folio_set_workingset(folio);
- folio_set_lru_refs(folio, 0);
- return true;
+
+/*
+ * Update the folio's lru refs indicator. The caller doesn't need to hold
+ * the folio lock, isolate the folio, or hold the lruvec lock. Used by both
+ * cache access (flags == 0) and page table access (LRU_REF_MAPPED,
+ * optionally with LRU_REF_EXEC).
+ */
+void folio_inc_lru_refs(struct folio *folio, unsigned int flags)
+{
+ int max_gen;
+ int file, old_refs, refs, old_gen, gen;
+ unsigned long new_flags, old_flags, max_seq;
+ long nr_pages = folio_nr_pages(folio);
+ struct lru_gen_folio *lrugen;
+ struct lruvec *lruvec = NULL;
+ bool aged = false;
+
+retry:
+ old_flags = READ_ONCE(*folio_flags(folio, 0));
+ do {
+ new_flags = old_flags;
+ old_gen = lru_get_gen_flags(old_flags);
+ old_refs = lru_get_refs_flags(old_flags);
+ refs = old_refs + 1;
+ gen = old_gen;
+ file = folio_flags_is_file_lru(&old_flags);
+ if (old_gen < 0)
+ goto out;
+ if (!lruvec) {
+ lruvec = folio_lruvec_live_get(folio);
+ lrugen = &lruvec->lrugen;
+ }
+ /*
+ * Read the flags before max_seq, paired with the store of a
+ * gen, which samples max_seq first.
+ */
+ smp_rmb();
+ max_seq = READ_ONCE(lrugen->max_seq);
+ max_gen = lru_gen_from_seq(max_seq);
+ if (old_gen == max_gen)
+ goto out;
+ if (flags & (LRU_REF_MAPPED | LRU_REF_EXEC | LRU_REF_FORCE)) {
+ if (refs > LRU_REFS_REFERENCED ||
+ flags & (LRU_REF_EXEC | LRU_REF_FORCE))
+ gen = max_gen;
+ /* First access only defers eviction of a tail gen */
+ else if (lru_gen_at_tail(lruvec, file, old_gen))
+ gen = (old_gen + 1) % MAX_NR_GENS;
+ refs = min(refs, LRU_REFS_PROTECTED);
+ } else if (refs > LRU_REFS_MAX) {
+ /* LRU refs counting overflow, bump the gen */
+ gen = (old_gen + 1) % MAX_NR_GENS;
+ refs = LRU_REFS_PROTECTED;
+ } else if (lru_gen_at_tail(lruvec, file, old_gen) && refs >= LRU_REFS_WORKINGSET) {
+ /* Defer eviction of just accessed workingset */
+ gen = (old_gen + 1) % MAX_NR_GENS;
+ refs = min(refs, LRU_REFS_PROTECTED);
}
+out:
+ refs = min(refs, LRU_REFS_MAX);
+ lru_set_refs_flags(&new_flags, refs);
+ if (gen != old_gen)
+ lru_set_gen_flags(&new_flags, gen);
+ if (new_flags == old_flags)
+ break;
+ } while (!try_cmpxchg(folio_flags(folio, 0), &old_flags, new_flags));
- folio_set_lru_refs(folio, 1);
- return false;
+ if (gen != old_gen) {
+ /*
+ * Gen-index reuse can fool cmpxchg(). Retry locklessly, accepting
+ * an extra reference to avoid folio_activate() latency.
+ */
+ if (unlikely(READ_ONCE(lrugen->max_seq) != max_seq)) {
+ flags = LRU_REF_FORCE;
+ aged = true;
+ }
+ lru_gen_update_size(lruvec, file, folio, old_gen, gen);
}
- /* Promote on second access */
- if (folio_lru_refs(folio) > 1) {
- folio_set_workingset(folio);
- folio_set_lru_refs(folio, 0);
- } else {
- folio_mark_accessed(folio);
+ if (lru_refs_is_active(old_refs) != lru_refs_is_active(refs) && gen >= 0) {
+ enum lru_list lru = file * LRU_INACTIVE_FILE;
+
+ __update_lru_size(lruvec, lru + lru_refs_is_active(old_refs),
+ folio_zonenum(folio), -nr_pages);
+ __update_lru_size(lruvec, lru + lru_refs_is_active(refs),
+ folio_zonenum(folio), nr_pages);
}
- return true;
+ if (aged) {
+ aged = false;
+ goto retry;
+ }
+ if (lruvec)
+ folio_lruvec_live_put(lruvec);
+}
+
+/*
+ * Reset the folio's lru refs indicator. The caller doesn't need to hold
+ * the folio lock, isolate the folio, or hold the lruvec lock.
+ */
+bool folio_reset_lru_refs(struct folio *folio)
+{
+ int type, gen, refs;
+ unsigned long seq, new_flags, old_flags;
+ long nr_pages = folio_nr_pages(folio);
+ struct lruvec *lruvec;
+ bool reset = false;
+
+ old_flags = READ_ONCE(*folio_flags(folio, 0));
+ do {
+ new_flags = old_flags;
+ refs = lru_get_refs_flags(old_flags);
+ type = folio_flags_is_file_lru(&old_flags);
+ gen = lru_get_gen_flags(old_flags);
+ if (!refs)
+ break;
+ lru_set_refs_flags(&new_flags, 0);
+ } while (!try_cmpxchg(folio_flags(folio, 0), &old_flags, new_flags));
+
+ if (gen >= 0) {
+ lruvec = folio_lruvec_live_get(folio);
+
+ /* If the folio is on list, caller might also want to demote it */
+ seq = READ_ONCE(lruvec->lrugen.min_seq[type]);
+ reset = (gen != lru_gen_from_seq(seq));
+ if (lru_refs_is_active(refs)) {
+ __update_lru_size(lruvec, type * LRU_FILE + LRU_ACTIVE,
+ folio_zonenum(folio), -nr_pages);
+ __update_lru_size(lruvec, type * LRU_FILE,
+ folio_zonenum(folio), nr_pages);
+ }
+
+ folio_lruvec_live_put(lruvec);
+ }
+
+ return reset;
+}
+
+/*
+ * Update the folio's lru refs indicator during a page table walk or the
+ * look-around. max_seq can be stale as neither holds the LRU lock.
+ *
+ * Returns the old generation, or -1 if the folio is off the LRU list.
+ * Old LRU refs, and updated gen and LRU refs info by the successful
+ * cmpxchg are all stored by returning arguments.
+ */
+static int folio_inc_lru_refs_walk(struct folio *folio, struct lruvec *lruvec,
+ const vma_flags_t *vma_flags, int *new_gen,
+ int *old_refs, int *new_refs, int *type)
+{
+ unsigned long new_flags, old_flags = READ_ONCE(*folio_flags(folio, 0));
+ int refs, old_gen, max_gen;
+ unsigned long max_seq;
+
+ do {
+ max_seq = READ_ONCE(lruvec->lrugen.max_seq);
+ max_gen = lru_gen_from_seq(max_seq);
+ old_gen = lru_get_gen_flags(old_flags);
+ refs = lru_get_refs_flags(old_flags) + 1;
+ *type = folio_flags_is_file_lru(&old_flags);
+ new_flags = old_flags;
+
+ if (old_gen >= 0 && old_gen != max_gen) {
+ *new_refs = min(refs, LRU_REFS_PROTECTED);
+ /* Promote second page table access or executable */
+ if (refs > LRU_REFS_REFERENCED || is_exec_file_folio(folio, vma_flags))
+ *new_gen = max_gen;
+ /* First access only defers eviction of a tail gen */
+ else if (lru_gen_at_tail(lruvec, *type, old_gen))
+ *new_gen = (old_gen + 1) % MAX_NR_GENS;
+ else
+ *new_gen = old_gen;
+ lru_set_gen_flags(&new_flags, *new_gen);
+ lru_set_refs_flags(&new_flags, *new_refs);
+ } else {
+ *new_gen = old_gen;
+ *new_refs = min(refs, LRU_REFS_MAX);
+ lru_set_refs_flags(&new_flags, *new_refs);
+ }
+ if (new_flags == old_flags)
+ break;
+ } while (!try_cmpxchg(folio_flags(folio, 0), &old_flags, new_flags));
+
+ *old_refs = lru_get_refs_flags(old_flags);
+
+ return old_gen;
+}
+
+/*
+ * Update the lru refs indicator of an isolated folio, only used on
+ * mapped folios upon the final eviction.
+ *
+ * Increments the refs count (capped at LRU_REFS_PROTECTED, evict_folios()
+ * then caps it at LRU_REFS_WORKINGSET). Returns true if the caller should
+ * activate the folio (second access or executable), false to put it back
+ * for a second chance.
+ */
+static bool folio_inc_lru_refs_isolated(struct folio *folio, const vma_flags_t *vma_flags)
+{
+ unsigned long new_flags, old_flags = READ_ONCE(*folio_flags(folio, 0));
+ int refs;
+
+ do {
+ new_flags = old_flags;
+ refs = lru_get_refs_flags(old_flags) + 1;
+ lru_set_refs_flags(&new_flags, min(refs, LRU_REFS_PROTECTED));
+ } while (!try_cmpxchg(folio_flags(folio, 0), &old_flags, new_flags));
+
+ /* Promote second page table access or executable */
+ return refs > LRU_REFS_REFERENCED || is_exec_file_folio(folio, vma_flags);
}
#else
-static bool lru_gen_set_refs(struct folio *folio, const vma_flags_t *vma_flags)
+static bool folio_inc_lru_refs_isolated(struct folio *folio, const vma_flags_t *vma_flags)
{
return false;
}
@@ -997,7 +1216,8 @@ static enum folio_references folio_check_references(struct folio *folio,
if (!referenced_ptes)
return FOLIOREF_RECLAIM;
- return lru_gen_set_refs(folio, &vma_flags) ? FOLIOREF_ACTIVATE : FOLIOREF_KEEP;
+ return folio_inc_lru_refs_isolated(folio, &vma_flags) ?
+ FOLIOREF_ACTIVATE : FOLIOREF_KEEP;
}
referenced_folio = folio_test_clear_referenced(folio);
@@ -3298,9 +3518,9 @@ static bool iterate_mm_list_nowalk(struct lruvec *lruvec, unsigned long seq)
* P term over the generations previously evicted, using the smoothing factor
* 1/2; the D term isn't supported.
*
- * The setpoint (SP) is always the first tier of one type; the process variable
- * (PV) is either any tier of the other type or any other tier of the same
- * type.
+ * To select a type, the setpoint (SP) is all tiers of one type and the process
+ * variable (PV) is all tiers of the other type. To select a tier to protect,
+ * the SP is the tiers below it and the PV is the tier itself.
*
* The error is the difference between the SP and the PV; the correction is to
* turn off protection when SP>PV or turn on protection when SP<PV.
@@ -3388,50 +3608,16 @@ static bool positive_ctrl_err(struct ctrl_pos *sp, struct ctrl_pos *pv)
* the aging
******************************************************************************/
-/* promote pages accessed through page tables */
-static int folio_update_gen(struct folio *folio, int new_gen, int *type,
- const vma_flags_t *vma_flags)
-{
- unsigned long new_flags, old_flags = READ_ONCE(*folio_flags(folio, 0));
- int old_gen;
-
- /*
- * See the comment on LRU_REFS_FLAGS, and activate file-backed
- * executable folios after first usage to avoid typical IO
- * thrashing from reclaiming.
- */
- if (!folio_test_referenced(folio) && !folio_test_workingset(folio) &&
- !is_exec_file_folio(folio, vma_flags)) {
- folio_set_lru_refs(folio, 1);
- return -1;
- }
-
- do {
- old_gen = lru_get_gen_flags(old_flags);
- new_flags = old_flags;
-
- /* lru_gen_del_folio() has isolated this page? */
- if (old_gen < 0)
- break;
-
- lru_set_gen_flags(&new_flags, new_gen);
- lru_set_refs_flags(&new_flags, 0);
- new_flags |= BIT(PG_workingset);
- } while (!try_cmpxchg(folio_flags(folio, 0), &old_flags, new_flags));
-
- *type = folio_flags_is_file_lru(&old_flags);
- return old_gen;
-}
-
-static int __folio_inc_gen(struct folio *folio, int old_gen, bool *increased)
+static int __folio_inc_gen(struct lruvec *lruvec, struct folio *folio,
+ int old_gen, bool *increased)
{
unsigned long new_flags, old_flags = READ_ONCE(*folio_flags(folio, 0));
- int new_gen;
+ int file, refs, old_refs, new_gen;
do {
new_gen = lru_get_gen_flags(old_flags);
- /* folio_update_gen() has promoted this page? */
+ /* folio_inc_lru_refs() has promoted this page? */
if (new_gen >= 0 && new_gen != old_gen) {
if (increased)
*increased = false;
@@ -3440,43 +3626,68 @@ static int __folio_inc_gen(struct folio *folio, int old_gen, bool *increased)
new_flags = old_flags;
new_gen = (old_gen + 1) % MAX_NR_GENS;
-
+ old_refs = lru_get_refs_flags(old_flags);
+ file = folio_flags_is_file_lru(&old_flags);
+ refs = min(old_refs, LRU_REFS_WORKINGSET);
lru_set_gen_flags(&new_flags, new_gen);
- lru_set_refs_flags(&new_flags, 0);
+ lru_set_refs_flags(&new_flags, refs);
} while (!try_cmpxchg(folio_flags(folio, 0), &old_flags, new_flags));
+ /* Refs capped below PROTECTED: demote if previously active. */
+ if (lru_refs_is_active(old_refs) != lru_refs_is_active(refs)) {
+ enum lru_list lru = file * LRU_INACTIVE_FILE;
+ int nr_pages = folio_nr_pages(folio);
+
+ __update_lru_size(lruvec, lru + lru_refs_is_active(old_refs),
+ folio_zonenum(folio), -nr_pages);
+ __update_lru_size(lruvec, lru + lru_refs_is_active(refs),
+ folio_zonenum(folio), nr_pages);
+ }
+
if (increased)
*increased = true;
return new_gen;
}
-/* protect pages accessed multiple times through file descriptors */
+/*
+ * Force bump a folio's generation. Used for PID protection or to skip a
+ * folio from a zone ineligible for the current reclaim.
+ */
static int folio_inc_gen(struct lruvec *lruvec, struct folio *folio)
{
+ bool gen_increased;
int type = folio_is_file_lru(folio);
struct lru_gen_folio *lrugen = &lruvec->lrugen;
int new_gen, old_gen = lru_gen_from_seq(lrugen->min_seq[type]);
- bool gen_increased;
- new_gen = __folio_inc_gen(folio, old_gen, &gen_increased);
+ new_gen = __folio_inc_gen(lruvec, folio, old_gen, &gen_increased);
if (gen_increased)
- lru_gen_update_size(lruvec, folio, old_gen, new_gen);
+ lru_gen_update_size(lruvec, type, folio, old_gen, new_gen);
+
return new_gen;
}
static void update_batch_size(struct lru_gen_mm_walk *walk, struct folio *folio,
- int old_gen, int new_gen, int type)
+ int old_gen, int new_gen, int old_refs, int new_refs, int type)
{
int zone = folio_zonenum(folio);
int delta = folio_nr_pages(folio);
- VM_WARN_ON_ONCE(old_gen >= MAX_NR_GENS);
- VM_WARN_ON_ONCE(new_gen >= MAX_NR_GENS);
-
- walk->batched++;
+ /* gen counter update */
+ if (old_gen != new_gen) {
+ VM_WARN_ON_ONCE(old_gen >= MAX_NR_GENS);
+ VM_WARN_ON_ONCE(new_gen >= MAX_NR_GENS);
+ walk->batched++;
+ walk->nr_pages[old_gen][type][zone] -= delta;
+ walk->nr_pages[new_gen][type][zone] += delta;
+ }
- walk->nr_pages[old_gen][type][zone] -= delta;
- walk->nr_pages[new_gen][type][zone] += delta;
+ /* active/inactive counter update */
+ if (lru_refs_is_active(old_refs) != lru_refs_is_active(new_refs)) {
+ walk->nr_activated[type][zone] +=
+ lru_refs_is_active(new_refs) ? delta : -delta;
+ walk->batched++;
+ }
}
static void reset_batch_size(struct lru_gen_mm_walk *walk)
@@ -3488,19 +3699,28 @@ static void reset_batch_size(struct lru_gen_mm_walk *walk)
walk->batched = 0;
for_each_gen_type_zone(gen, type, zone) {
- enum lru_list lru = type * LRU_INACTIVE_FILE;
int delta = walk->nr_pages[gen][type][zone];
if (!delta)
continue;
walk->nr_pages[gen][type][zone] = 0;
- WRITE_ONCE(lrugen->nr_pages[gen][type][zone],
- lrugen->nr_pages[gen][type][zone] + delta);
+ atomic_long_add(delta, &lrugen->nr_pages[gen][type][zone]);
+ }
+
+ /* apply batched active/inactive updates */
+ for (type = 0; type < ANON_AND_FILE; type++) {
+ for (zone = 0; zone < MAX_NR_ZONES; zone++) {
+ enum lru_list lru = type * LRU_INACTIVE_FILE;
+ int delta = walk->nr_activated[type][zone];
- if (lru_gen_is_active(lruvec, gen))
- lru += LRU_ACTIVE;
- __update_lru_size(lruvec, lru, zone, delta);
+ if (delta) {
+ __update_lru_size(lruvec, lru + LRU_ACTIVE,
+ zone, delta);
+ __update_lru_size(lruvec, lru, zone, -delta);
+ walk->nr_activated[type][zone] = 0;
+ }
+ }
}
lruvec_unlock_irq(lruvec);
@@ -3655,26 +3875,27 @@ static bool suitable_to_scan(int total, int young)
static void walk_update_folio(struct lru_gen_mm_walk *walk, struct vm_area_struct *vma,
struct lruvec *lruvec, struct folio *folio, bool dirty)
{
- int new_gen, old_gen, type;
+ int new_gen, old_gen, old_refs, new_refs, type;
+ unsigned int flags = LRU_REF_MAPPED;
if (!folio)
return;
- new_gen = lru_gen_from_seq(READ_ONCE(lruvec->lrugen.max_seq));
-
if (dirty && !folio_test_dirty(folio) &&
!(folio_test_anon(folio) && folio_test_swapbacked(folio) &&
!folio_test_swapcache(folio)))
folio_mark_dirty(folio);
if (walk) {
- old_gen = folio_update_gen(folio, new_gen, &type, &vma->flags);
- if (old_gen >= 0 && old_gen != new_gen)
- update_batch_size(walk, folio, old_gen, new_gen, type);
- } else if (lru_gen_set_refs(folio, &vma->flags)) {
- old_gen = folio_lru_gen(folio);
- if (old_gen >= 0 && old_gen != new_gen)
- folio_activate(folio);
+ old_gen = folio_inc_lru_refs_walk(folio, lruvec, &vma->flags,
+ &new_gen, &old_refs, &new_refs, &type);
+ if (old_gen >= 0)
+ update_batch_size(walk, folio, old_gen, new_gen,
+ old_refs, new_refs, type);
+ } else {
+ if (is_exec_file_folio(folio, &vma->flags))
+ flags |= LRU_REF_EXEC;
+ folio_inc_lru_refs(folio, flags);
}
}
@@ -4029,6 +4250,7 @@ static void clear_mm_walk(void)
VM_WARN_ON_ONCE(walk && memchr_inv(walk->nr_pages, 0, sizeof(walk->nr_pages)));
VM_WARN_ON_ONCE(walk && memchr_inv(walk->mm_stats, 0, sizeof(walk->mm_stats)));
+ VM_WARN_ON_ONCE(walk && memchr_inv(walk->nr_activated, 0, sizeof(walk->nr_activated)));
current->reclaim_state->mm_walk = NULL;
@@ -4067,8 +4289,6 @@ static bool inc_min_seq(struct lruvec *lruvec, int type, int swappiness)
goto done;
VM_WARN_ON_ONCE(get_nr_gens(lruvec, type) != MAX_NR_GENS);
- VM_WARN_ON_ONCE(lru_gen_is_active(lruvec, old_gen) !=
- lru_gen_is_active(lruvec, target_gen));
/* prevent cold/hot inversion if the type is evictable */
for (zone = 0; zone < MAX_NR_ZONES; zone++) {
struct list_head *target_list = &lrugen->folios[target_gen][type][zone];
@@ -4081,7 +4301,7 @@ static bool inc_min_seq(struct lruvec *lruvec, int type, int swappiness)
struct folio *folio = list_entry(pos, struct folio, lru);
long nr_pages = folio_nr_pages(folio);
int refs = folio_lru_refs(folio);
- bool workingset = folio_test_workingset(folio);
+ int tier = lru_tier_from_refs(refs);
bool gen_increased;
VM_WARN_ON_ONCE_FOLIO(folio_test_unevictable(folio), folio);
@@ -4091,7 +4311,7 @@ static bool inc_min_seq(struct lruvec *lruvec, int type, int swappiness)
prefetchw_next_lru_folio(folio, head);
pos = pos->next;
- new_gen = __folio_inc_gen(folio, old_gen, &gen_increased);
+ new_gen = __folio_inc_gen(lruvec, folio, old_gen, &gen_increased);
/*
* If gen_increased is false, this is a promotion. Put folios
* at the head of the promoted gen. Otherwise, put them at
@@ -4101,13 +4321,8 @@ static bool inc_min_seq(struct lruvec *lruvec, int type, int swappiness)
delta += nr_pages;
batch_end = &folio->lru;
- /* don't count the workingset being lazily promoted */
- if (refs + workingset != BIT(LRU_REFS_WIDTH) + 1) {
- int tier = lru_tier_from_refs(refs, workingset);
-
- WRITE_ONCE(lrugen->protected[hist][type][tier],
- lrugen->protected[hist][type][tier] + nr_pages);
- }
+ WRITE_ONCE(lrugen->protected[hist][type][tier],
+ lrugen->protected[hist][type][tier] + nr_pages);
} else {
flush_lru_batch(head, &batch_end, target_list);
list_move(&folio->lru, &lrugen->folios[new_gen][type][zone]);
@@ -4116,11 +4331,8 @@ static bool inc_min_seq(struct lruvec *lruvec, int type, int swappiness)
break;
}
flush_lru_batch(head, &batch_end, target_list);
-
- WRITE_ONCE(lrugen->nr_pages[old_gen][type][zone],
- lrugen->nr_pages[old_gen][type][zone] - delta);
- WRITE_ONCE(lrugen->nr_pages[target_gen][type][zone],
- lrugen->nr_pages[target_gen][type][zone] + delta);
+ atomic_long_sub(delta, &lrugen->nr_pages[old_gen][type][zone]);
+ atomic_long_add(delta, &lrugen->nr_pages[target_gen][type][zone]);
if (!remaining)
return false;
}
@@ -4186,8 +4398,7 @@ static void try_to_inc_min_seq(struct lruvec *lruvec, int swappiness)
static bool inc_max_seq(struct lruvec *lruvec, unsigned long seq, int swappiness)
{
bool success;
- int prev, next;
- int type, zone;
+ int type, next;
struct lru_gen_folio *lrugen = &lruvec->lrugen;
restart:
if (seq < READ_ONCE(lrugen->max_seq))
@@ -4213,32 +4424,10 @@ static bool inc_max_seq(struct lruvec *lruvec, unsigned long seq, int swappiness
goto restart;
}
- /*
- * Update the active/inactive LRU sizes for compatibility. Both sides of
- * the current max_seq need to be covered, since max_seq+1 can overlap
- * with min_seq[LRU_GEN_ANON] if swapping is constrained. And if they do
- * overlap, cold/hot inversion happens.
- */
- prev = lru_gen_from_seq(lrugen->max_seq - 1);
- next = lru_gen_from_seq(lrugen->max_seq + 1);
-
- for (type = 0; type < ANON_AND_FILE; type++) {
- for (zone = 0; zone < MAX_NR_ZONES; zone++) {
- enum lru_list lru = type * LRU_INACTIVE_FILE;
- long delta = lrugen->nr_pages[prev][type][zone] -
- lrugen->nr_pages[next][type][zone];
-
- if (!delta)
- continue;
-
- __update_lru_size(lruvec, lru, zone, delta);
- __update_lru_size(lruvec, lru + LRU_ACTIVE, zone, -delta);
- }
- }
-
for (type = 0; type < ANON_AND_FILE; type++)
reset_ctrl_pos(lruvec, type, false);
+ next = lru_gen_from_seq(lrugen->max_seq + 1);
WRITE_ONCE(lrugen->timestamps[next], jiffies);
/* make sure preceding modifications appear */
smp_store_release(&lrugen->max_seq, lrugen->max_seq + 1);
@@ -4344,7 +4533,8 @@ static unsigned long lruvec_evictable_size(struct lruvec *lruvec, int swappiness
for (seq = min_seq[type]; seq <= max_seq; seq++) {
gen = lru_gen_from_seq(seq);
for (zone = 0; zone < MAX_NR_ZONES; zone++)
- total += max(READ_ONCE(lrugen->nr_pages[gen][type][zone]), 0L);
+ total += max(atomic_long_read(&lrugen->nr_pages[gen][type][zone]),
+ 0L);
}
}
@@ -4756,30 +4946,22 @@ static void __lru_gen_reparent_memcg(struct lruvec *child_lruvec, struct lruvec
int zone, int type)
{
struct lru_gen_folio *child_lrugen, *parent_lrugen;
- enum lru_list lru = type * LRU_INACTIVE_FILE;
int i;
child_lrugen = &child_lruvec->lrugen;
parent_lrugen = &parent_lruvec->lrugen;
- for (i = 0; i < get_nr_gens(child_lruvec, type); i++) {
+ /* Lockless promotion may leave folios outside the child's window */
+ for (i = 0; i < MAX_NR_GENS; i++) {
int gen = lru_gen_from_seq(child_lrugen->max_seq - i);
- long nr_pages = child_lrugen->nr_pages[gen][type][zone];
- int child_lru_active = lru_gen_is_active(child_lruvec, gen) ? LRU_ACTIVE : 0;
- int parent_lru_active = lru_gen_is_active(parent_lruvec, gen) ? LRU_ACTIVE : 0;
+ long nr_pages = atomic_long_read(&child_lrugen->nr_pages[gen][type][zone]);
/* Assuming that child pages are colder than parent pages */
list_splice_tail_init(&child_lrugen->folios[gen][type][zone],
&parent_lrugen->folios[gen][type][zone]);
- WRITE_ONCE(child_lrugen->nr_pages[gen][type][zone], 0);
- WRITE_ONCE(parent_lrugen->nr_pages[gen][type][zone],
- parent_lrugen->nr_pages[gen][type][zone] + nr_pages);
-
- if (lru_gen_is_active(child_lruvec, gen) != lru_gen_is_active(parent_lruvec, gen)) {
- __update_lru_size(child_lruvec, lru + child_lru_active, zone, -nr_pages);
- __update_lru_size(parent_lruvec, lru + parent_lru_active, zone, nr_pages);
- }
+ atomic_long_set(&child_lrugen->nr_pages[gen][type][zone], 0);
+ atomic_long_add(nr_pages, &parent_lrugen->nr_pages[gen][type][zone]);
}
}
@@ -4825,8 +5007,7 @@ static bool sort_folio(struct lruvec *lruvec, struct folio *folio, struct scan_c
int zone = folio_zonenum(folio);
int delta = folio_nr_pages(folio);
int refs = folio_lru_refs(folio);
- bool workingset = folio_test_workingset(folio);
- int tier = lru_tier_from_refs(refs, workingset);
+ int tier = lru_tier_from_refs(refs);
struct lru_gen_folio *lrugen = &lruvec->lrugen;
VM_WARN_ON_ONCE_FOLIO(gen >= MAX_NR_GENS, folio);
@@ -4842,17 +5023,15 @@ static bool sort_folio(struct lruvec *lruvec, struct folio *folio, struct scan_c
}
/* protected */
- if (tier > tier_idx || refs + workingset == BIT(LRU_REFS_WIDTH) + 1) {
+ if (tier > tier_idx) {
+ int hist = lru_hist_from_seq(lrugen->min_seq[type]);
+
gen = folio_inc_gen(lruvec, folio);
list_move(&folio->lru, &lrugen->folios[gen][type][zone]);
- /* don't count the workingset being lazily promoted */
- if (refs + workingset != BIT(LRU_REFS_WIDTH) + 1) {
- int hist = lru_hist_from_seq(lrugen->min_seq[type]);
+ WRITE_ONCE(lrugen->protected[hist][type][tier],
+ lrugen->protected[hist][type][tier] + delta);
- WRITE_ONCE(lrugen->protected[hist][type][tier],
- lrugen->protected[hist][type][tier] + delta);
- }
return true;
}
@@ -4880,10 +5059,6 @@ static bool isolate_folio(struct lruvec *lruvec, struct folio *folio, struct sca
return false;
}
- /* see the comment on LRU_REFS_FLAGS */
- if (!folio_test_referenced(folio))
- folio_set_lru_refs(folio, 0);
-
success = lru_gen_del_folio(lruvec, folio, true);
VM_WARN_ON_ONCE_FOLIO(!success, folio);
@@ -4971,13 +5146,14 @@ static int get_tier_idx(struct lruvec *lruvec, int type)
struct ctrl_pos sp, pv;
/*
- * To leave a margin for fluctuations, use a larger gain factor (2:3).
- * This value is chosen because any other tier would have at least twice
- * as many refaults as the first tier.
+ * To leave a margin for fluctuations, use a larger gain factor (1:2).
+ * Stop at the first tier whose refault rate is clearly worse than
+ * that of the cumulative mass of the tiers below it; the PID
+ * protects the tiers above it.
*/
- read_ctrl_pos(lruvec, type, LRU_TIER_MIN, LRU_TIER_MIN, 2, &sp);
for (tier = LRU_TIER_MIN + 1; tier <= LRU_TIER_MAX; tier++) {
- read_ctrl_pos(lruvec, type, tier, tier, 3, &pv);
+ read_ctrl_pos(lruvec, type, LRU_TIER_MIN, tier - 1, 1, &sp);
+ read_ctrl_pos(lruvec, type, tier, tier, 2, &pv);
if (!positive_ctrl_err(&sp, &pv))
break;
}
@@ -5119,12 +5295,13 @@ static int evict_folios(unsigned long nr_to_scan, struct lruvec *lruvec,
}
/*
- * See the comments on LRU_REFS_FLAGS.
- *
* The rejected folios are never added to the oldest generation,
* so this effectively promotes them by at least one generation.
+ * PG_active is the only placement hint here, so a folio kept on
+ * its first page table access lands in the second newest one.
+ * See "Referenced count feedback" above.
*/
- folio_set_lru_refs(folio, 0);
+ __folio_set_lru_refs(folio, min(folio_lru_refs(folio), LRU_REFS_WORKINGSET));
if (lru_gen_folio_seq(lruvec, folio, false) == min_seq[type])
folio_set_active(folio);
}
@@ -5859,7 +6036,8 @@ static int lru_gen_seq_show(struct seq_file *m, void *v)
char mark = full && seq < min_seq[type] ? 'x' : ' ';
for (zone = 0; zone < MAX_NR_ZONES; zone++)
- size += max(READ_ONCE(lrugen->nr_pages[gen][type][zone]), 0L);
+ size += max(atomic_long_read(&lrugen->nr_pages[gen][type][zone]),
+ 0L);
seq_printf(m, " %10lu%c", size, mark);
}
diff --git a/mm/workingset.c b/mm/workingset.c
index 1504f91cdca5f..7681af535ca7e 100644
--- a/mm/workingset.c
+++ b/mm/workingset.c
@@ -188,6 +188,12 @@
#define EVICTION_SHIFT_ANON (EVICTION_SHIFT + SWAP_COUNT_SHIFT)
#define EVICTION_MASK (~0UL >> EVICTION_SHIFT)
#define EVICTION_MASK_ANON (~0UL >> EVICTION_SHIFT_ANON)
+/*
+ * LRU refs uses LRU_REFS_WIDTH + 2 bits, the 2 bits being PG_workingset
+ * and PG_referenced. The lowest bit is recorded in the workingset field
+ * of the shadow entry (to reuse pack_shadow()).
+ */
+#define LRU_REFS_BITS ((LRU_REFS_WIDTH + 2) - 1)
/*
* Eviction timestamps need to be able to cover the full range of
@@ -242,13 +248,12 @@ static void *lru_gen_eviction(struct folio *folio)
int type = folio_is_file_lru(folio);
int delta = folio_nr_pages(folio);
int refs = folio_lru_refs(folio);
- bool workingset = folio_test_workingset(folio);
- int tier = lru_tier_from_refs(refs, workingset);
+ int tier = lru_tier_from_refs(refs);
struct mem_cgroup *memcg;
struct pglist_data *pgdat = folio_pgdat(folio);
unsigned short memcg_id;
- BUILD_BUG_ON(LRU_GEN_WIDTH + LRU_REFS_WIDTH >
+ BUILD_BUG_ON(LRU_GEN_WIDTH + LRU_REFS_BITS >
BITS_PER_LONG - max(EVICTION_SHIFT, EVICTION_SHIFT_ANON));
rcu_read_lock();
@@ -256,14 +261,14 @@ static void *lru_gen_eviction(struct folio *folio)
lruvec = mem_cgroup_lruvec(memcg, pgdat);
lrugen = &lruvec->lrugen;
min_seq = READ_ONCE(lrugen->min_seq[type]);
- token = (min_seq << LRU_REFS_WIDTH) | max(refs - 1, 0);
+ token = (min_seq << LRU_REFS_BITS) | refs >> 1;
hist = lru_hist_from_seq(min_seq);
atomic_long_add(delta, &lrugen->evicted[hist][type][tier]);
memcg_id = mem_cgroup_private_id(memcg);
rcu_read_unlock();
- return pack_shadow(memcg_id, pgdat, token, workingset, type);
+ return pack_shadow(memcg_id, pgdat, token, refs & 1, type);
}
/*
@@ -284,9 +289,9 @@ static bool lru_gen_test_recent(void *shadow, struct lruvec **lruvec,
*lruvec = mem_cgroup_lruvec(memcg, pgdat);
max_seq = READ_ONCE((*lruvec)->lrugen.max_seq);
- max_seq &= (file ? EVICTION_MASK : EVICTION_MASK_ANON) >> LRU_REFS_WIDTH;
+ max_seq &= (file ? EVICTION_MASK : EVICTION_MASK_ANON) >> LRU_REFS_BITS;
- return abs_diff(max_seq, *token >> LRU_REFS_WIDTH) < MAX_NR_GENS;
+ return abs_diff(max_seq, *token >> LRU_REFS_BITS) < MAX_NR_GENS;
}
static void lru_gen_refault(struct folio *folio, void *shadow)
@@ -314,22 +319,26 @@ static void lru_gen_refault(struct folio *folio, void *shadow)
lrugen = &lruvec->lrugen;
hist = lru_hist_from_seq(READ_ONCE(lrugen->min_seq[type]));
- refs = (token & (BIT(LRU_REFS_WIDTH) - 1)) + 1;
- tier = lru_tier_from_refs(refs, workingset);
+ refs = ((token & (BIT(LRU_REFS_BITS) - 1)) << 1) + workingset;
+ tier = lru_tier_from_refs(refs);
atomic_long_add(delta, &lrugen->refaulted[hist][type][tier]);
- if (workingset) {
- /*
- * see folio_add_lru(), where folio_set_active() is
- * called for workingset folios
- */
- if (lru_gen_in_fault())
+ /*
+ * Activate a fault-driven refault folio, which would have been
+ * promoted had it stayed in memory.
+ */
+ if (refs >= LRU_REFS_REFERENCED) {
+ if (lru_gen_in_fault()) {
+ folio_set_active(folio);
mod_lruvec_state(lruvec, WORKINGSET_ACTIVATE_BASE + type, delta);
- folio_set_workingset(folio);
+ }
+ /* Refault is also promotion, cap the refs like folio_inc_lru_refs */
+ __folio_set_lru_refs(folio, min(refs, LRU_REFS_PROTECTED));
+ }
+
+ if (refs >= LRU_REFS_WORKINGSET)
mod_lruvec_state(lruvec, WORKINGSET_RESTORE_BASE + type, delta);
- } else
- set_mask_bits(&folio->flags.f, LRU_REFS_MASK, (refs - 1UL) << LRU_REFS_PGOFF);
unlock:
rcu_read_unlock();
}
|
| 3/2 | 2026/10/15 23:41 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 35094 output: 30 thoughts: 627 Thoughts: **My Assessment of the Patch's Fuzzing Worthiness** Okay, let's break this down. This patch is hitting core memory management code, and a *lot* of it: the documentation for multi-gen LRU, files like `mm/vmscan.c`, `mm/workingset.c`, `mm/folio.c`, `mm/madvise.c`, the `include/linux/mm_inline.h` header, and more. Specifically, it seems to be reworking multi-gen LRU (MGLRU) referenced count feedback mechanisms, tiering, workingset tracking, and even lockless promotion. This definitely falls squarely within the "logic modifications in memory management (mm/)" category as defined by the triage philosophy, and it's also restructuring data structures and algorithm flows within reachable paths. Given the instructions and the triage guidelines, "WorthFuzzing = true" is an undeniable conclusion. There's no way around it; this demands attention. This is 100% core memory management that we can reach in a QEMU environment, which is what the instructions describe as a requirement. My next step is to nail down the specifics for the fuzzer. I need to figure out what Kconfig options are required to actually compile and *reach* the modified code. The primary consideration is clearly `CONFIG_LRU_GEN`, which is referenced by the included files in this patch. I need to determine what that Kconfig option depends on. Is there a `CONFIG_LRU_GEN_ENABLED` or something similar? Time to dive into `mm/Kconfig` to start looking for dependencies. |
| 4/2 | 2026/10/15 23:41 | tool | grepper |
0mArgs:{
"Expression": "config LRU_GEN",
"PathPrefixes": [
"mm/Kconfig"
]
}Results: {
"Output": "mm/Kconfig=1388=config PTE_MARKER_UFFD_WP\n--\nmm/Kconfig-1398-# multi-gen LRU {\nmm/Kconfig:1399:config LRU_GEN\nmm/Kconfig-1400-\tbool \"Multi-Gen LRU\"\n--\nmm/Kconfig-1407-\nmm/Kconfig:1408:config LRU_GEN_ENABLED\nmm/Kconfig-1409-\tbool \"Enable by default\"\n--\nmm/Kconfig-1413-\nmm/Kconfig:1414:config LRU_GEN_STATS\nmm/Kconfig-1415-\tbool \"Full stats for debugging\"\n--\nmm/Kconfig-1422-\nmm/Kconfig:1423:config LRU_GEN_WALKS_MMU\nmm/Kconfig-1424-\tdef_bool y\n"
} |
| 5/2 | 2026/10/15 23:41 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 36337 output: 33 thoughts: 38 Thoughts: Okay, let's dive into this. Here's how I'd approach that kernel configuration investigation, and what I'd be thinking: **Inspecting Kernel Configuration Options: `LRU_GEN` and `LRU_GEN_ENABLED`** Alright, time to get my hands dirty. I need to understand the relationship and implications of `LRU_GEN` and `LRU_GEN_ENABLED`, so my first stop is definitely `mm/Kconfig`. That's where all the interesting configuration options live, and lines around 1398 sounds like a reasonable place to start looking. I'll need to check the dependencies to see what other options must be enabled for these to even show up, and the description of the options themselves will tell me what these options are really *for*. Let's see what the Kconfig says about the semantics of those options. Understanding the dependencies is key to understanding how this feature fits into the bigger picture of memory management. I suspect `LRU_GEN` will be the fundamental feature, and `LRU_GEN_ENABLED` might just be a conditional enable based on other parameters. Or perhaps it's a completely different feature with a confusingly similar name. Either way, that's what I'll figure out. |
| 6/2 | 2026/10/15 23:42 | tool | read-file |
0mArgs:{
"File": "mm/Kconfig",
"FirstLine": 1395,
"LineCount": 40
}Results: {
"Contents": "1395:\t\t purposes. It is required to enable userfaultfd write protection on\n1396:\t\t file-backed memory types like shmem and hugetlbfs.\n1397:\t\n1398:\t# multi-gen LRU {\n1399:\tconfig LRU_GEN\n1400:\t\tbool \"Multi-Gen LRU\"\n1401:\t\tdepends on MMU\n1402:\t\t# make sure folio-\u003eflags has enough spare bits\n1403:\t\tdepends on 64BIT || !SPARSEMEM || SPARSEMEM_VMEMMAP\n1404:\t\thelp\n1405:\t\t A high performance LRU implementation to overcommit memory. See\n1406:\t\t Documentation/admin-guide/mm/multigen_lru.rst for details.\n1407:\t\n1408:\tconfig LRU_GEN_ENABLED\n1409:\t\tbool \"Enable by default\"\n1410:\t\tdepends on LRU_GEN\n1411:\t\thelp\n1412:\t\t This option enables the multi-gen LRU by default.\n1413:\t\n1414:\tconfig LRU_GEN_STATS\n1415:\t\tbool \"Full stats for debugging\"\n1416:\t\tdepends on LRU_GEN\n1417:\t\thelp\n1418:\t\t Do not enable this option unless you plan to look at historical stats\n1419:\t\t from evicted generations for debugging purpose.\n1420:\t\n1421:\t\t This option has a per-memcg and per-node memory overhead.\n1422:\t\n1423:\tconfig LRU_GEN_WALKS_MMU\n1424:\t\tdef_bool y\n1425:\t\tdepends on LRU_GEN \u0026\u0026 ARCH_HAS_HW_PTE_YOUNG\n1426:\t# }\n1427:\t\n1428:\tconfig LOCK_MM_AND_FIND_VMA\n1429:\t\tbool\n1430:\t\tdepends on !STACK_GROWSUP\n1431:\t\n1432:\tconfig IOMMU_MM_DATA\n1433:\t\tbool\n1434:\t\n"
} |
| 7/2 | 2026/10/15 23:42 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 37243 output: 65 thoughts: 1879 Thoughts: **MGLRU Feature Analysis: Identifying Key Focus Symbols** Okay, so I'm diving into this MGLRU implementation and need to pinpoint the critical functions to guide the fuzzer. First things first, `CONFIG_LRU_GEN` and `CONFIG_LRU_GEN_ENABLED` are obviously key, but I need to make sure I'm not missing anything. I'll also check `CONFIG_MEMCG` because `folio_lruvec_live_get` references it, so that's definitely relevant here. Now, I should also check `CONFIG_DAMON` and `CONFIG_TRANSPARENT_HUGEPAGE` to see if those affect this new code. Now, let's turn our attention to the FocusSymbol guidance: I need to avoid listing ubiquitous lifecycle functions like allocators, deallocators, page faults, or core synchronization primitives. Instead, I need to focus on the specific logic being added, altered, or entry points for this subsystem. Static inline functions present a challenge, so I need to find the non-static callers that exercise these inline helpers. So, let's look at the functions modified or added: In `mm/vmscan.c`, `folio_inc_lru_refs` and `folio_reset_lru_refs` are non-static and exported. These are core to MGLRU's referenced count feedback and resetting. The static functions like `folio_inc_lru_refs_walk`, `folio_inc_gen`, `__folio_inc_gen`, `inc_min_seq`, `inc_max_seq`, `update_batch_size`, `reset_batch_size`, `sort_folio`, `evict_folios`, `get_tier_idx`, `folio_inc_lru_refs_isolated` are also in this file, which may be relevant if not inlined. In `mm/workingset.c`, `lru_gen_eviction`, `lru_gen_test_recent`, and `lru_gen_refault` are static. The key here is to see how they integrate with the MGLRU logic. In `mm/madvise.c`, `madvise_cold_prep_folio`, `madvise_pageout_prep_folio`, and `madvise_cold_or_pageout_pte_range` are static. Now, `folio_mark_accessed` in `mm/folio.c` *looks* tempting, but it's ubiquitous, so it's out. I was right to think about that because that would dilute the focus. I can't include that. I need to focus on these: - `folio_inc_lru_refs` and `folio_reset_lru_refs` are key since they are specific to MGLRU referenced count feedback and resetting. - I need to check the callers for these: `folio_mark_accessed`, `deactivate_file_folio`, `folio_deactivate`, `walk_update_folio`, `clear_refs_pte_range`, and `damon_pa_pageout`. - I need to focus on the other static functions like `inc_min_seq`, `inc_max_seq`, `sort_folio`, `evict_folios`, `get_tier_idx`, `lru_gen_refault`, and `lru_gen_eviction`. Are these even visible as symbols? Let's now investigate functions implementing MGLRU aging, eviction, and tiering in `mm/vmscan.c`. I need to ensure that `evict_folios`, `sort_folio`, `lru_gen_refault` aren't inlined and therefore make it into the final symbol table. And it's on to the next investigation! |
| 8/2 | 2026/10/15 23:42 | tool | grepper |
0mArgs:{
"Expression": "(folio_inc_lru_refs|folio_reset_lru_refs|lru_gen_refault|evict_folios|sort_folio|get_tier_idx)",
"PathPrefixes": [
"mm/"
]
}Results: {
"Output": "mm/folio.c=365=void folio_mark_accessed(struct folio *folio)\n--\nmm/folio.c-370-\t\tif (!folio_test_unevictable(folio))\nmm/folio.c:371:\t\t\tfolio_inc_lru_refs(folio, 0);\nmm/folio.c-372-\t\treturn;\n--\nmm/folio.c=595=void deactivate_file_folio(struct folio *folio)\n--\nmm/folio.c-600-\nmm/folio.c:601:\tif (lru_gen_enabled() \u0026\u0026 !folio_reset_lru_refs(folio))\nmm/folio.c-602-\t\treturn;\n--\nmm/folio.c=615=void folio_deactivate(struct folio *folio)\n--\nmm/folio.c-619-\nmm/folio.c:620:\tif (lru_gen_enabled() ? !folio_reset_lru_refs(folio) : !folio_test_active(folio))\nmm/folio.c-621-\t\treturn;\n--\nmm/gup.c=2834=static unsigned long gup_fast_pte_range(pmd_t pmd, pmd_t *pmdp,\n--\nmm/gup.c-2898-\t\t}\nmm/gup.c:2899:\t\tfolio_inc_lru_refs_fast(folio);\nmm/gup.c-2900-\t\tpages[nr_pages] = page;\n--\nmm/gup.c=2927=static unsigned long gup_fast_pmd_leaf(pmd_t orig, pmd_t *pmdp,\n--\nmm/gup.c-2967-\t\t*(pages++) = page++;\nmm/gup.c:2968:\tfolio_inc_lru_refs_fast(folio);\nmm/gup.c-2969-\treturn nr_pages;\n--\nmm/gup.c=2972=static unsigned long gup_fast_pud_leaf(pud_t orig, pud_t *pudp,\n--\nmm/gup.c-3009-\t\t*(pages++) = page++;\nmm/gup.c:3010:\tfolio_inc_lru_refs_fast(folio);\nmm/gup.c-3011-\treturn nr_pages;\n--\nmm/madvise.c=363=static void madvise_cold_prep_folio(struct folio *folio)\n--\nmm/madvise.c-377-\t * folio_deactivate() clears the refs count through\nmm/madvise.c:378:\t * folio_reset_lru_refs().\nmm/madvise.c-379-\t */\n--\nmm/vmscan.c=927=enum folio_references {\n--\nmm/vmscan.c-938-/*\nmm/vmscan.c:939: * The folio_inc_lru_refs{_*} helpers below collect the referenced info\nmm/vmscan.c-940- * (hotness) from other parts, including the page table walker, the rmap walk\n--\nmm/vmscan.c-946- * newest. Executable file folios promote on their first access to avoid\nmm/vmscan.c:947: * I/O thrashing. Isolated rmap accesses use evict_folios() for putback.\nmm/vmscan.c-948- *\nmm/vmscan.c:949: * Promotion changes folio-\u003eflags; sort_folio() or inc_min_seq() moves the\nmm/vmscan.c-950- * folio later. Young PTEs found during unmapping use folio_mark_accessed()\n--\nmm/vmscan.c-969- *\nmm/vmscan.c:970: * folio_inc_lru_refs() retries with LRU_REF_FORCE if the aging recycled the\nmm/vmscan.c-971- * gen index it was about to store: newest gen and an extra reference.\n--\nmm/vmscan.c-979- */\nmm/vmscan.c:980:void folio_inc_lru_refs(struct folio *folio, unsigned int flags)\nmm/vmscan.c-981-{\n--\nmm/vmscan.c-1071- */\nmm/vmscan.c:1072:bool folio_reset_lru_refs(struct folio *folio)\nmm/vmscan.c-1073-{\n--\nmm/vmscan.c-1117- */\nmm/vmscan.c:1118:static int folio_inc_lru_refs_walk(struct folio *folio, struct lruvec *lruvec,\nmm/vmscan.c-1119-\t\t\t\t const vma_flags_t *vma_flags, int *new_gen,\n--\nmm/vmscan.c-1163- *\nmm/vmscan.c:1164: * Increments the refs count (capped at LRU_REFS_PROTECTED, evict_folios()\nmm/vmscan.c-1165- * then caps it at LRU_REFS_WORKINGSET). Returns true if the caller should\n--\nmm/vmscan.c-1168- */\nmm/vmscan.c:1169:static bool folio_inc_lru_refs_isolated(struct folio *folio, const vma_flags_t *vma_flags)\nmm/vmscan.c-1170-{\n--\nmm/vmscan.c-1183-#else\nmm/vmscan.c:1184:static bool folio_inc_lru_refs_isolated(struct folio *folio, const vma_flags_t *vma_flags)\nmm/vmscan.c-1185-{\n--\nmm/vmscan.c=1190=static enum folio_references folio_check_references(struct folio *folio,\n--\nmm/vmscan.c-1218-\nmm/vmscan.c:1219:\t\treturn folio_inc_lru_refs_isolated(folio, \u0026vma_flags) ?\nmm/vmscan.c-1220-\t\t FOLIOREF_ACTIVATE : FOLIOREF_KEEP;\n--\nmm/vmscan.c=3611=static int __folio_inc_gen(struct lruvec *lruvec, struct folio *folio,\n--\nmm/vmscan.c-3619-\nmm/vmscan.c:3620:\t\t/* folio_inc_lru_refs() has promoted this page? */\nmm/vmscan.c-3621-\t\tif (new_gen \u003e= 0 \u0026\u0026 new_gen != old_gen) {\n--\nmm/vmscan.c=3875=static void walk_update_folio(struct lru_gen_mm_walk *walk, struct vm_area_struct *vma,\n--\nmm/vmscan.c-3889-\tif (walk) {\nmm/vmscan.c:3890:\t\told_gen = folio_inc_lru_refs_walk(folio, lruvec, \u0026vma-\u003eflags,\nmm/vmscan.c-3891-\t\t\t\t\t\t \u0026new_gen, \u0026old_refs, \u0026new_refs, \u0026type);\n--\nmm/vmscan.c-3897-\t\t\tflags |= LRU_REF_EXEC;\nmm/vmscan.c:3898:\t\tfolio_inc_lru_refs(folio, flags);\nmm/vmscan.c-3899-\t}\n--\nmm/vmscan.c=4968=void lru_gen_reparent_memcg(struct mem_cgroup *memcg, struct mem_cgroup *parent, int nid)\n--\nmm/vmscan.c-5001-\nmm/vmscan.c:5002:static bool sort_folio(struct lruvec *lruvec, struct folio *folio, struct scan_control *sc,\nmm/vmscan.c-5003-\t\t int tier_idx)\n--\nmm/vmscan.c=5068=static int scan_folios(unsigned long nr_to_scan, struct lruvec *lruvec,\n--\nmm/vmscan.c-5106-\nmm/vmscan.c:5107:\t\t\tif (sort_folio(lruvec, folio, sc, tier))\nmm/vmscan.c-5108-\t\t\t\tsorted += delta;\n--\nmm/vmscan.c-5142-\nmm/vmscan.c:5143:static int get_tier_idx(struct lruvec *lruvec, int type)\nmm/vmscan.c-5144-{\n--\nmm/vmscan.c=5191=static int isolate_folios(unsigned long nr_to_scan, struct lruvec *lruvec,\n--\nmm/vmscan.c-5201-retry:\nmm/vmscan.c:5202:\ttier = get_tier_idx(lruvec, type);\nmm/vmscan.c-5203-\tscanned = scan_folios(nr_to_scan, lruvec, sc,\n--\nmm/vmscan.c-5234-\nmm/vmscan.c:5235:static int evict_folios(unsigned long nr_to_scan, struct lruvec *lruvec,\nmm/vmscan.c-5236-\t\t\tstruct scan_control *sc, int swappiness)\n--\nmm/vmscan.c=5424=static bool try_to_shrink_lruvec(struct lruvec *lruvec, struct scan_control *sc)\n--\nmm/vmscan.c-5447-\t\tnr_batch = min(nr_to_scan, MIN_LRU_BATCH);\nmm/vmscan.c:5448:\t\tdelta = evict_folios(nr_batch, lruvec, sc, swappiness);\nmm/vmscan.c-5449-\t\tif (!delta)\n--\nmm/vmscan.c=6075=static int run_eviction(struct lruvec *lruvec, unsigned long seq, struct scan_control *sc,\n--\nmm/vmscan.c-6095-\t\tnr_batch = min(nr_to_reclaim - sc-\u003enr_reclaimed, MAX_LRU_BATCH);\nmm/vmscan.c:6096:\t\tif (!evict_folios(nr_batch, lruvec, sc, swappiness))\nmm/vmscan.c-6097-\t\t\treturn 0;\n--\nmm/workingset.c=278=static bool lru_gen_test_recent(void *shadow, struct lruvec **lruvec,\n--\nmm/workingset.c-296-\nmm/workingset.c:297:static void lru_gen_refault(struct folio *folio, void *shadow)\nmm/workingset.c-298-{\n--\nmm/workingset.c-335-\t\t}\nmm/workingset.c:336:\t\t/* Refault is also promotion, cap the refs like folio_inc_lru_refs */\nmm/workingset.c-337-\t\t__folio_set_lru_refs(folio, min(refs, LRU_REFS_PROTECTED));\n--\nmm/workingset.c=353=static bool lru_gen_test_recent(void *shadow, struct lruvec **lruvec,\n--\nmm/workingset.c-358-\nmm/workingset.c:359:static void lru_gen_refault(struct folio *folio, void *shadow)\nmm/workingset.c-360-{\n--\nmm/workingset.c=557=void workingset_refault(struct folio *folio, void *shadow)\n--\nmm/workingset.c-567-\tif (lru_gen_enabled()) {\nmm/workingset.c:568:\t\tlru_gen_refault(folio, shadow);\nmm/workingset.c-569-\t\treturn;\n"
} |
| 9/2 | 2026/10/15 23:42 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 43036 output: 41 thoughts: 677 Thoughts: **Analyzing LRU Generation Management** Okay, let's break down this LRU generation stuff. My initial thought process starts with the obvious entry points. I see `workingset_refault` calling `lru_gen_refault` and `workingset_eviction` calling `lru_gen_eviction`. Further down the line, I know `try_to_shrink_lruvec` and `run_eviction` are calling `evict_folios`. I'm also noting that `folio_inc_lru_refs` and `folio_reset_lru_refs` are globally visible functions – that's important for understanding scope. And, of course, the interplay with `folio_lruvec_live_get` and `folio_lruvec_live_put` in `include/linux/memcontrol.h`, as they're utilized by those `inc` and `reset` functions. I should probably also quickly review how `madvise_cold_or_pageout_pte_range` or `do_madvise` and `madvise_walk_vmas` fit in here. Now, let's drill down into the core LRU management. I need to refresh my memory on the inline functions: `lruvec_add_folio`, `lru_gen_add_folio`, and `lru_gen_del_folio` from `include/linux/mm_inline.h`. Ah, yes, there were some key changes in `lru_gen_add_folio` and `lru_gen_del_folio`. I'm immediately paying attention to the flag manipulation and the size updates. Specifically, the bitwise operations with `LRU_GEN_MASK`, `BIT(PG_active)`, and the calls to `lru_gen_update_size` are of interest. The difference between incrementing and decrementing the size based on activity/inactivity with those `lru_refs_is_active` calls and `__update_lru_size` is very clear. In `lru_gen_del_folio`, there's that interesting conditional `folio_set_active` based on the generation sequence. I need to fully understand that logic. Finally, I need to see where these inline functions are called. Time to search the codebase for usages of `lruvec_add_folio` and `lruvec_del_folio`, as well as the more specific `lru_gen_add_folio` and `lru_gen_del_folio` calls. This will give me the full picture of how they're being used in practice. |
| 10/2 | 2026/10/15 23:42 | tool | grepper |
0mArgs:{
"Expression": "lru_(gen|vec)_(add|del)_folio",
"PathPrefixes": [
"include/",
"mm/"
]
}Results: {
"Output": "include/linux/mm_inline.h=345=static inline unsigned long lru_gen_folio_seq(const struct lruvec *lruvec,\n--\ninclude/linux/mm_inline.h-379-\ninclude/linux/mm_inline.h:380:static inline bool lru_gen_add_folio(struct lruvec *lruvec, struct folio *folio, bool reclaiming)\ninclude/linux/mm_inline.h-381-{\n--\ninclude/linux/mm_inline.h-418-\ninclude/linux/mm_inline.h:419:static inline bool lru_gen_del_folio(struct lruvec *lruvec, struct folio *folio, bool reclaiming)\ninclude/linux/mm_inline.h-420-{\n--\ninclude/linux/mm_inline.h=466=static inline bool lru_gen_in_fault(void)\n--\ninclude/linux/mm_inline.h-470-\ninclude/linux/mm_inline.h:471:static inline bool lru_gen_add_folio(struct lruvec *lruvec, struct folio *folio, bool reclaiming)\ninclude/linux/mm_inline.h-472-{\n--\ninclude/linux/mm_inline.h-475-\ninclude/linux/mm_inline.h:476:static inline bool lru_gen_del_folio(struct lruvec *lruvec, struct folio *folio, bool reclaiming)\ninclude/linux/mm_inline.h-477-{\n--\ninclude/linux/mm_inline.h=552=void lruvec_add_folio(struct lruvec *lruvec, struct folio *folio)\n--\ninclude/linux/mm_inline.h-557-\ninclude/linux/mm_inline.h:558:\tif (lru_gen_add_folio(lruvec, folio, false))\ninclude/linux/mm_inline.h-559-\t\treturn;\n--\ninclude/linux/mm_inline.h=568=void lruvec_add_folio_tail(struct lruvec *lruvec, struct folio *folio)\n--\ninclude/linux/mm_inline.h-573-\ninclude/linux/mm_inline.h:574:\tif (lru_gen_add_folio(lruvec, folio, true))\ninclude/linux/mm_inline.h-575-\t\treturn;\n--\ninclude/linux/mm_inline.h=584=void lruvec_del_folio(struct lruvec *lruvec, struct folio *folio)\n--\ninclude/linux/mm_inline.h-589-\ninclude/linux/mm_inline.h:590:\tif (lru_gen_del_folio(lruvec, folio, false))\ninclude/linux/mm_inline.h-591-\t\treturn;\n--\ninclude/linux/mmzone.h=405=enum lruvec_flags {\n--\ninclude/linux/mmzone.h-444- * fault-driven refault (see lru_gen_refault), or isolation (see\ninclude/linux/mmzone.h:445: * lru_gen_del_folio).\ninclude/linux/mmzone.h-446- *\n--\nmm/vmscan.c=4904=void max_lru_gen_memcg(struct mem_cgroup *memcg, int nid)\n--\nmm/vmscan.c-4930- * generations on an LRU list.\nmm/vmscan.c:4931: * 4. In lru_gen_del_folio(), the generation to which the folio belongs is\nmm/vmscan.c-4932- * found based on the generation information in folio-\u003eflags, and the\n--\nmm/vmscan.c-4934- * the lru size correctly during reparenting, otherwise the lru size may\nmm/vmscan.c:4935: * be updated incorrectly in lru_gen_del_folio().\nmm/vmscan.c-4936- *\n--\nmm/vmscan.c=5048=static bool isolate_folio(struct lruvec *lruvec, struct folio *folio, struct scan_control *sc)\n--\nmm/vmscan.c-5061-\nmm/vmscan.c:5062:\tsuccess = lru_gen_del_folio(lruvec, folio, true);\nmm/vmscan.c-5063-\tVM_WARN_ON_ONCE_FOLIO(!success, folio);\n--\nmm/vmscan.c=5676=static bool fill_evictable(struct lruvec *lruvec)\n--\nmm/vmscan.c-5690-\t\t\t/*\nmm/vmscan.c:5691:\t\t\t * lru_gen_add_folio() uses list_add_tail() rather\nmm/vmscan.c-5692-\t\t\t * than list_add() when reclaiming is true. Match\n--\nmm/vmscan.c-5709-\t\t\t */\nmm/vmscan.c:5710:\t\t\tsuccess = lru_gen_add_folio(lruvec, folio, !active);\nmm/vmscan.c-5711-\t\t\tVM_WARN_ON_ONCE(!success);\n--\nmm/vmscan.c=5721=static bool drain_evictable(struct lruvec *lruvec)\n--\nmm/vmscan.c-5737-\nmm/vmscan.c:5738:\t\t\tsuccess = lru_gen_del_folio(lruvec, folio, false);\nmm/vmscan.c-5739-\t\t\tVM_WARN_ON_ONCE(!success);\n"
} |
| 11/2 | 2026/10/15 23:42 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 45751 output: 39 thoughts: 618 Thoughts: **My Thought Process on Identifying Relevant Functions for Fuzzing MGLRU** Okay, let's start by considering which functions are directly related to the Multi-Gen LRU (MGLRU) implementation that I need to target for fuzzing. Initially, I was thinking about functions like `lruvec_add_folio`, `lruvec_del_folio`, and related functions like `folio_batch_move_lru` and `folio_add_lru`. But wait, I need to remember rule number four: avoid listing ubiquitous lifecycle hot-paths. These functions, as I recall, are extremely generic and called by nearly everything. Listing these will be a waste of fuzzing resources, diluting the focus. Therefore, I need to identify *specific feature logic and entrypoints* for MGLRU. What fits this description? Here's what I've identified as potentially relevant: * `folio_inc_lru_refs`, `folio_reset_lru_refs`: These are specific to MGLRU's reference counting and generation handling, altered significantly for atomic operations. They are perfect. * `lru_gen_refault`, `lru_gen_eviction`: These are MGLRU-specific handlers related to refaults and eviction tokens, so I should definitely include them. * `evict_folios`, `sort_folio`: These are core MGLRU routines in vmscan, central to the logic, so definitely needed. * `inc_min_seq`, `inc_max_seq`, `folio_inc_gen`: These are MGLRU-specific functions that handle generation sequences and increments. These would be perfect targets. * `madvise_cold_or_pageout_pte_range`: This is in the `madvise` subsystem but it might exercise MGLRU through `madvise_cold_prep_folio` and `madvise_pageout_prep_folio`. Worth investigating. Now, I should consider other interfaces: I also need to check if there are sysfs or procfs interfaces for MGLRU. Based on the documentation, I should look for something like `/sys/kernel/mm/lru_gen/`. I will then need to search for relevant lru\_gen sysfs or proc interfaces in `mm/vmscan.c`. |
| 12/2 | 2026/10/15 23:42 | tool | grepper |
0mArgs:{
"Expression": "lru_gen.*(show|write|read)",
"PathPrefixes": [
"mm/vmscan.c"
]
}Results: {
"Output": "mm/vmscan.c=4556=static bool lruvec_is_reclaimable(struct lruvec *lruvec, struct scan_control *sc,\n--\nmm/vmscan.c-4577-/* to protect the working set of the last N jiffies */\nmm/vmscan.c:4578:static unsigned long lru_gen_min_ttl __read_mostly;\nmm/vmscan.c-4579-\n--\nmm/vmscan.c=5923=static void *lru_gen_seq_next(struct seq_file *m, void *v, loff_t *pos)\n--\nmm/vmscan.c-5941-\nmm/vmscan.c:5942:static void lru_gen_seq_show_full(struct seq_file *m, struct lruvec *lruvec,\nmm/vmscan.c-5943-\t\t\t\t unsigned long max_seq, unsigned long *min_seq,\n--\nmm/vmscan.c-5996-/* see Documentation/admin-guide/mm/multigen_lru.rst for details */\nmm/vmscan.c:5997:static int lru_gen_seq_show(struct seq_file *m, void *v)\nmm/vmscan.c-5998-{\n--\nmm/vmscan.c-6047-\t\tif (full)\nmm/vmscan.c:6048:\t\t\tlru_gen_seq_show_full(m, lruvec, max_seq, min_seq, seq);\nmm/vmscan.c-6049-\t}\n--\nmm/vmscan.c=6054=static const struct seq_operations lru_gen_seq_ops = {\n--\nmm/vmscan.c-6057-\t.next = lru_gen_seq_next,\nmm/vmscan.c:6058:\t.show = lru_gen_seq_show,\nmm/vmscan.c-6059-};\n--\nmm/vmscan.c=6105=static int run_cmd(char cmd, u64 memcg_id, int nid, unsigned long seq,\n--\nmm/vmscan.c-6146-/* see Documentation/admin-guide/mm/multigen_lru.rst for details */\nmm/vmscan.c:6147:static ssize_t lru_gen_seq_write(struct file *file, const char __user *src,\nmm/vmscan.c-6148-\t\t\t\t size_t len, loff_t *pos)\n--\nmm/vmscan.c=6236=static const struct file_operations lru_gen_rw_fops = {\n--\nmm/vmscan.c-6238-\t.read = seq_read,\nmm/vmscan.c:6239:\t.write = lru_gen_seq_write,\nmm/vmscan.c-6240-\t.llseek = seq_lseek,\n"
} |
| 13/2 | 2026/10/15 23:42 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 47666 output: 35 thoughts: 86 Thoughts: Okay, let's dive into this. Here's how I'm processing this information: **Deep Dive into `/sys/kernel/mm/lru_gen/` and its Implications** Alright, so I'm seeing `/sys/kernel/mm/lru_gen/` has these interesting files: `lru_gen_seq_write` and `lru_gen_seq_show`. That's a good starting point. The fact that there's a "write" interface, `lru_gen_seq_write`, is what really grabs my attention. I immediately suspect this might be how we control or interact with the LRU generation mechanism. Now, let's see, what happens when we *write* to `lru_gen_seq_write`? Ah-ha, it calls `run_cmd`, which is key. And `run_cmd` then executes either `run_eviction` or `inc_max_seq`. So, this `lru_gen_seq_write` likely provides a user-space interface to trigger eviction and/or to manipulate the sequence number, which suggests control over the LRU generational algorithm. It seems, by manipulating the sequence, there could be interesting implications for cache management or memory reclamation. I'll need to dig deeper into the details of `lru_gen_seq_write` to understand the exact parameters and how it directs the operation. I need to figure out the security implications, too, but this looks like a good place to start my investigation. |
| 14/2 | 2026/10/15 23:42 | tool | read-file |
0mArgs:{
"File": "mm/vmscan.c",
"FirstLine": 6146,
"LineCount": 50
}Results: {
"Contents": "6146:\t/* see Documentation/admin-guide/mm/multigen_lru.rst for details */\n6147:\tstatic ssize_t lru_gen_seq_write(struct file *file, const char __user *src,\n6148:\t\t\t\t\t size_t len, loff_t *pos)\n6149:\t{\n6150:\t\tvoid *buf;\n6151:\t\tchar *cur, *next;\n6152:\t\tunsigned int flags;\n6153:\t\tstruct blk_plug plug;\n6154:\t\tint err = -EINVAL;\n6155:\t\tstruct scan_control sc = {\n6156:\t\t\t.may_writepage = true,\n6157:\t\t\t.may_unmap = true,\n6158:\t\t\t.may_swap = true,\n6159:\t\t\t.reclaim_idx = MAX_NR_ZONES - 1,\n6160:\t\t\t.gfp_mask = GFP_KERNEL,\n6161:\t\t\t.proactive = true,\n6162:\t\t};\n6163:\t\n6164:\t\tbuf = kvmalloc(len + 1, GFP_KERNEL);\n6165:\t\tif (!buf)\n6166:\t\t\treturn -ENOMEM;\n6167:\t\n6168:\t\tif (copy_from_user(buf, src, len)) {\n6169:\t\t\tkvfree(buf);\n6170:\t\t\treturn -EFAULT;\n6171:\t\t}\n6172:\t\n6173:\t\tset_task_reclaim_state(current, \u0026sc.reclaim_state);\n6174:\t\tflags = memalloc_noreclaim_save();\n6175:\t\tblk_start_plug(\u0026plug);\n6176:\t\tif (!set_mm_walk(NULL, true)) {\n6177:\t\t\terr = -ENOMEM;\n6178:\t\t\tgoto done;\n6179:\t\t}\n6180:\t\n6181:\t\tnext = buf;\n6182:\t\tnext[len] = '\\0';\n6183:\t\n6184:\t\twhile ((cur = strsep(\u0026next, \",;\\n\"))) {\n6185:\t\t\tint n;\n6186:\t\t\tint end;\n6187:\t\t\tchar cmd, swap_string[5];\n6188:\t\t\tu64 memcg_id;\n6189:\t\t\tunsigned int nid;\n6190:\t\t\tunsigned long seq;\n6191:\t\t\tunsigned int swappiness;\n6192:\t\t\tunsigned long opt = -1;\n6193:\t\n6194:\t\t\tcur = skip_spaces(cur);\n6195:\t\t\tif (!*cur)\n"
} |
| 15/2 | 2026/10/15 23:42 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 48822 output: 35 thoughts: 41 Thoughts: Okay, here's my take on those thoughts, framed as an expert's internal monologue: **Investigating `lru_gen_seq_write` and `run_cmd`** Alright, so `lru_gen_seq_write` - that's a direct sysfs hook for initiating aging and eviction within MGLRU, isn't it? Fine. Let's see... what exactly is the `run_cmd` function doing under the hood? It's the critical piece that ties this sysfs entry to the kernel's actual memory management operations, and I want a deeper understanding of its execution flow. We need to dissect that function to really grasp how the aging and eviction are triggered by a user-space write to the sysfs node. Understanding that implementation detail is key to appreciating this feature. |
| 16/2 | 2026/10/15 23:42 | tool | read-file |
0mArgs:{
"File": "mm/vmscan.c",
"FirstLine": 6100,
"LineCount": 45
}Results: {
"Contents": "6100:\t\t}\n6101:\t\n6102:\t\treturn -EINTR;\n6103:\t}\n6104:\t\n6105:\tstatic int run_cmd(char cmd, u64 memcg_id, int nid, unsigned long seq,\n6106:\t\t\t struct scan_control *sc, int swappiness, unsigned long opt)\n6107:\t{\n6108:\t\tstruct lruvec *lruvec;\n6109:\t\tint err = -EINVAL;\n6110:\t\tstruct mem_cgroup *memcg = NULL;\n6111:\t\n6112:\t\tif (nid \u003c 0 || nid \u003e= MAX_NUMNODES || !node_state(nid, N_MEMORY))\n6113:\t\t\treturn -EINVAL;\n6114:\t\n6115:\t\tif (!mem_cgroup_disabled()) {\n6116:\t\t\tmemcg = mem_cgroup_get_from_id(memcg_id);\n6117:\t\t\tif (!memcg)\n6118:\t\t\t\treturn -EINVAL;\n6119:\t\t}\n6120:\t\n6121:\t\tif (memcg_id != mem_cgroup_id(memcg))\n6122:\t\t\tgoto done;\n6123:\t\n6124:\t\tsc-\u003etarget_mem_cgroup = memcg;\n6125:\t\tlruvec = get_lruvec(memcg, nid);\n6126:\t\n6127:\t\tif (swappiness \u003c MIN_SWAPPINESS)\n6128:\t\t\tswappiness = get_swappiness(lruvec, sc);\n6129:\t\telse if (swappiness \u003e SWAPPINESS_ANON_ONLY)\n6130:\t\t\tgoto done;\n6131:\t\n6132:\t\tswitch (cmd) {\n6133:\t\tcase '+':\n6134:\t\t\terr = run_aging(lruvec, seq, swappiness, opt);\n6135:\t\t\tbreak;\n6136:\t\tcase '-':\n6137:\t\t\terr = run_eviction(lruvec, seq, sc, swappiness, opt);\n6138:\t\t\tbreak;\n6139:\t\t}\n6140:\tdone:\n6141:\t\tmem_cgroup_put(memcg);\n6142:\t\n6143:\t\treturn err;\n6144:\t}\n"
} |
| 17/2 | 2026/10/15 23:42 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 49710 output: 28 thoughts: 1981 Thoughts: **Analyzing Multi-Gen LRU Changes for Coverage** Okay, so I'm diving into this multi-gen LRU patch and need to figure out the right symbols for effective coverage testing. First, I noticed the documentation (`Documentation/admin-guide/mm/multigen_lru.rst`) highlights a key invariant: `min_gen_nr` must be less than `max_gen_nr - 1`. This seems important to keep in mind throughout this. Now, I'm thinking about the core aging process. Let's trace the functions involved: `run_aging` -> `try_to_inc_max_seq` -> `inc_max_seq` -> `walk_pmd_range_locked` -> `walk_update_folio` -> `folio_inc_lru_refs_walk`. And for eviction, we have: `run_eviction` -> `evict_folios` -> `isolate_folios` -> `scan_folios` -> `sort_folio` -> `folio_inc_gen` -> `__folio_inc_gen`, along with `folio_check_references` and `folio_inc_lru_refs_isolated`. We also see calls to `folio_inc_lru_refs` from `walk_update_folio` (when not walking) and `folio_mark_accessed`. Plus, we see `folio_reset_lru_refs` being used in several places such as `deactivate_file_folio`, `folio_deactivate`, and `clear_refs_pte_range`. We can't forget `lru_gen_refault` and `lru_gen_eviction` in the workingset. Next, I'm working through a list of potential symbols to use in coverage filters. I want to make sure they're distinct and not ubiquitous. Let's start with a candidate list and see if they have clear usage: * `folio_inc_lru_refs`: Definitely looks good, a new or refactored non-static function in `mm/vmscan.c`. * `folio_reset_lru_refs`: Also solid, a non-static function added in `mm/vmscan.c`. * `folio_inc_gen`: It's static, but modified in `mm/vmscan.c`. I need to remember that static functions *can* be used. * `inc_min_seq`, `inc_max_seq`, `evict_folios`, `sort_folio`: These are all static functions in `mm/vmscan.c`, and modified. * `lru_gen_seq_write`: This is a sysfs interface handler in `mm/vmscan.c`. * `lru_gen_refault`, `lru_gen_eviction`: Located in `mm/workingset.c`, and modified. Then, there's `clear_refs_pte_range` in `fs/proc/task_mmu.c`, where `folio_clear_lru_refs(folio)` was added in `clear_refs_pte_range` for the `/proc/[pid]/clear_refs` interface. And `smaps_account` for `/proc/[pid]/smaps`. I'll want to carefully consider if `clear_refs_pte_range` is sufficiently specific. And, what about `madvise_cold_prep_folio` / `madvise_pageout_prep_folio`? These are static in `mm/madvise.c`, called from `madvise_cold_or_pageout_pte_range`. I like the specificity of `madvise_cold_or_pageout_pte_range` here! Regarding static functions, I know static inline functions in headers are a problem for symbol coverage. However, regular static functions in `.c` files *are* available in the `vmlinux` symbol table, so they *can* be used as focus symbols, assuming they are not inlined. Now, I'm checking the changes in static inline helpers in the headers: * `include/linux/mm_inline.h`: We see changes to `lru_gen_update_size`, `lru_gen_add_folio`, `lru_gen_del_folio`, `lru_tier_from_refs`, `lru_set_refs_flags`, `lru_get_refs_flags`, `__folio_set_lru_refs`, `folio_inc_lru_refs_fast`, `folio_check_lru_refs`, `folio_clear_lru_refs`, `folio_test_workingset`, `folio_ |
| 18/2 | 2026/10/15 23:43 | tool | grepper |
0mArgs:{
"Expression": "config MEMCG",
"PathPrefixes": [
"init/Kconfig"
]
}Results: {
"Output": "init/Kconfig=1073=config CGROUP_FAVOR_DYNMODS\n--\ninit/Kconfig-1082-\ninit/Kconfig:1083:config MEMCG\ninit/Kconfig-1084-\tbool \"Memory controller\"\n--\ninit/Kconfig-1091-\ninit/Kconfig:1092:config MEMCG_NMI_UNSAFE\ninit/Kconfig-1093-\tbool\n--\ninit/Kconfig-1098-\ninit/Kconfig:1099:config MEMCG_NMI_SAFETY_REQUIRES_ATOMIC\ninit/Kconfig-1100-\tbool\n--\ninit/Kconfig-1105-\ninit/Kconfig:1106:config MEMCG_V1\ninit/Kconfig-1107-\tbool \"Legacy cgroup v1 memory controller\"\n"
} |
| 19/2 | 2026/10/15 23:43 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 52972 output: 33 thoughts: 17 Thoughts: Okay, here's my take on those thoughts, tailored for an expert audience: **Examining Kconfig at Line 1083** Right, let's dive in. I need to get a handle on something quickly, and my mental map suggests the problem likely resides around `init/Kconfig`, specifically line 1083. I'm operating on the assumption that this section in the configuration system is relevant to [ *insert relevant topic - the user is an expert so I will not assume I know * ]. Perhaps there's a dependency issue, a newly introduced option, or maybe an interaction with another module's configuration that's causing grief. This is the obvious starting point. Time to pull up the source and see what's what. Let's see if this line is a branching point and where the error likely lies. |
| 20/2 | 2026/10/15 23:43 | tool | read-file |
0mArgs:{
"File": "init/Kconfig",
"FirstLine": 1080,
"LineCount": 15
}Results: {
"Contents": "1080:\t\n1081:\t\t Say N if unsure.\n1082:\t\n1083:\tconfig MEMCG\n1084:\t\tbool \"Memory controller\"\n1085:\t\tselect PAGE_COUNTER\n1086:\t\tselect EVENTFD\n1087:\t\tselect SLAB_OBJ_EXT\n1088:\t\tselect VM_EVENT_COUNTERS\n1089:\t\thelp\n1090:\t\t Provides control over the memory footprint of tasks in a cgroup.\n1091:\t\n1092:\tconfig MEMCG_NMI_UNSAFE\n1093:\t\tbool\n1094:\t\tdepends on MEMCG\n"
} |
| 21/2 | 2026/10/15 23:43 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 53383 output: 302 thoughts: 791 Thoughts: **My Assessment of the MGLRU Configuration and Fuzzing Potential** Okay, so I'm looking at the `MEMCG` configuration option, nested under `menuconfig CGROUPS`, and it's leading me to consider `LRU_GEN`, `LRU_GEN_ENABLED`, `MEMCG` (and, by extension, `CGROUPS`). First, I want to clarify if `LRU_GEN_STATS` is really necessary or if the core enablement lies with `CONFIG_LRU_GEN` and `CONFIG_LRU_GEN_ENABLED`. Those are the critical flags, I believe. Next, I need to identify specific functions that are modified or are entry points for the feature – not ubiquitous ones, but the ones *directly* related to MGLRU. I’ve compiled a list: `folio_inc_lru_refs`, `folio_reset_lru_refs`, `folio_inc_gen`, `__folio_inc_gen`, `walk_update_folio`, `inc_min_seq`, `inc_max_seq`, `evict_folios`, `sort_folio`, `lru_gen_refault`, `lru_gen_eviction`, `lru_gen_seq_write`, `madvise_cold_or_pageout_pte_range`, and `clear_refs_pte_range`. Are these truly specific? Let's check: * `folio_inc_lru_refs` is definitely MGLRU-specific, used for updating refs or promoting pages. * `folio_reset_lru_refs` is also MGLRU-specific, called when deactivating. * `sort_folio` is tied to MGLRU eviction sorting. * `evict_folios` is MGLRU-specific. * `inc_min_seq` and `inc_max_seq` are MGLRU eviction and aging advancement. * `lru_gen_refault`, `lru_gen_eviction`, and `lru_gen_seq_write` are all MGLRU internals. * `madvise_cold_or_pageout_pte_range` and `clear_refs_pte_range` are modified to use MGLRU helpers. Okay, this list appears *highly* targeted, exercising the modified code and entry points, avoiding the ubiquitous routines that would provide little signal. This looks good. Now, let's confirm the WorthFuzzing assessment: * The core subsystem is Memory Management (mm/), specifically Multi-Gen LRU (MGLRU). Check. * Is it reachable in QEMU/standard virtualization? Absolutely. We can exercise this directly through memory pressure, `madvise`, page faults, reclaim, the `/sys/kernel/mm/lru_gen/` sysfs interface, and `/proc/$PID/clear_refs` and `/proc/$PID/smaps`. Check. * So, `WorthFuzzing = true`. Check. Finally, a sanity check on the Reasoning: * We're dealing with core mm changes to Multi-Gen LRU (MGLRU) page reclaim and tiering logic. Check. * Specifically, the rework of referenced count tracking, tier assignment, workingset protection, and lockless generation promotion. Check. * These changes affect page reclamation, aging, refault handling, and memory control. Check. * And we can fully reach this via userspace memory access, page allocation under memory pressure, `madvise`, and `lru_gen` control interfaces. Check. Everything is clear. I'm ready to `set-results`. |
| 22/2 | 2026/10/15 23:43 | tool | set-results |
0mArgs:{
"EnableConfigs": [
"LRU_GEN",
"LRU_GEN_ENABLED",
"MEMCG"
],
"FocusSymbols": [
"folio_inc_lru_refs",
"folio_reset_lru_refs",
"folio_inc_gen",
"inc_min_seq",
"inc_max_seq",
"evict_folios",
"sort_folio",
"lru_gen_refault",
"lru_gen_eviction",
"lru_gen_seq_write",
"madvise_cold_or_pageout_pte_range",
"clear_refs_pte_range"
],
"Reasoning": "The patch modifies core memory management code by overhauling the Multi-Gen LRU (MGLRU) referenced count tracking, workingset protection, and generation promotion algorithms. It introduces new lockless generation advancement (folio_inc_lru_refs, folio_reset_lru_refs), modifies tier classification, refactors eviction sorting (sort_folio, evict_folios), and updates refault handling. These changes execute directly in standard virtualized environments under memory reclaim, page faults, madvise(MADV_COLD/MADV_PAGEOUT), and MGLRU sysfs controls (/sys/kernel/mm/lru_gen).",
"WorthFuzzing": true
}Results: {
"EnableConfigs": [
"LRU_GEN",
"LRU_GEN_ENABLED",
"MEMCG"
],
"FocusSymbols": [
"folio_inc_lru_refs",
"folio_reset_lru_refs",
"folio_inc_gen",
"inc_min_seq",
"inc_max_seq",
"evict_folios",
"sort_folio",
"lru_gen_refault",
"lru_gen_eviction",
"lru_gen_seq_write",
"madvise_cold_or_pageout_pte_range",
"clear_refs_pte_range"
],
"Reasoning": "The patch modifies core memory management code by overhauling the Multi-Gen LRU (MGLRU) referenced count tracking, workingset protection, and generation promotion algorithms. It introduces new lockless generation advancement (folio_inc_lru_refs, folio_reset_lru_refs), modifies tier classification, refactors eviction sorting (sort_folio, evict_folios), and updates refault handling. These changes execute directly in standard virtualized environments under memory reclaim, page faults, madvise(MADV_COLD/MADV_PAGEOUT), and MGLRU sysfs controls (/sys/kernel/mm/lru_gen).",
"WorthFuzzing": true
} |
| 23/1 | 2026/10/15 23:43 | action | If |
1mArgs:{
"WorthFuzzing": true
}Results: null |
| 24/2 | 2026/10/15 23:43 | agent | kmsan-evaluator |
1mModel:core Args: null Results: {
"KMSANReasoning": "The patch series reworks the Multi-Gen LRU (MGLRU) referenced count and tiering tracking, storing the reference count across PG_referenced, PG_workingset, and LRU_REFS bits in folio-\u003eflags, while updating page reclamation, migration, madvise, and compaction paths.\n\nKMSAN vs KASAN Applicability Analysis:\n1. Uninitialized Memory / Info-Leaks (KMSAN):\n - The changes do not introduce or modify any structures or buffers copied to user space (e.g., via copy_to_user, put_user, netlink, or ioctl). User-facing interfaces like smaps and lru_gen_seq_show only read formatted integers.\n - All newly introduced and modified functions (such as folio_inc_lru_refs, folio_reset_lru_refs, folio_inc_lru_refs_walk, __folio_inc_gen) explicitly initialize all local variables before use.\n - The new field `nr_activated` in `struct lru_gen_mm_walk` is zeroed either via `kzalloc_obj` in `set_mm_walk` or as part of the zero-initialized `pgdat-\u003emm_walk`, and is asserted to be zero via `memchr_inv` in `clear_mm_walk`.\n - Eviction shadow packing/unpacking handles integer tokens and booleans without exposing uninitialized memory.\n\n2. Memory Safety and Concurrency (KASAN / Standard Detectors):\n - The primary risks introduced by this patch series involve lockless atomic cmpxchg operations on folio flags, memory ordering (smp_rmb/smp_store_release), list splicing under RCU / lru_lock, and LRU size accounting (`__update_lru_size`).\n - Any potential regressions (such as use-after-free, double-free, out-of-bounds access, race conditions, or locking issues) are covered by KASAN, LOCKDEP, and kernel page/VM debugging assertions (e.g., VM_WARN_ON_ONCE_FOLIO).\n\nBecause no uninitialized memory risks or kernel-to-user information leaks are introduced, a dedicated KMSAN fuzzing session is not warranted.",
"NeedsKMSAN": false
}Instruction: You are an expert Linux kernel security engineer specializing in kernel memory
error detectors (KASAN and KMSAN). Your job is to review the provided patch series and
determine if the code changes justify spawning a dedicated KMSAN (KernelMemorySanitizer)
fuzzing session in addition to standard KASAN fuzzing.
CRITICAL DISTINCTION BETWEEN KASAN AND KMSAN:
- Standard KASAN kernel builds (upstream-apparmor-kasan.config) already enable
a comprehensive suite of debugging tools and sanitizers, including KASAN
(out-of-bounds accesses, use-after-free, double free, invalid free), LOCKDEP
(locking bugs and deadlocks), UB-sanitizers, and memory corruption checks.
- KMSAN (KernelMemorySanitizer) detects reads of UNINITIALIZED memory (stack, heap,
or page allocations) and kernel-to-user memory info-leaks.
Rule: THERE IS NO SENSE IN RUNNING A KMSAN SESSION IF A BUG CAN BE CAUGHT BY KASAN,
LOCKDEP, OR OTHER STANDARD BUG DETECTORS.
A dedicated KMSAN fuzzing session incurs significant resource costs. You must ONLY
set NeedsKMSAN=true if the code changes introduce or expose UNINITIALIZED MEMORY risks
that are detected ONLY by KMSAN.
Look holistically at the patch series and surrounding code. Even if no direct
uninitialized field accesses or new buffer allocations are added in the diff itself,
a patch may alter control flow, bounds checking, or data length calculations in ways
that change how the rest of the code operates on existing buffers (e.g. allowing
uninitialized stack/heap memory to be read, copied to user space, or used in control
flow). Do not hesitate to use your code access tools to inspect the surrounding code,
called functions, and callers.
Set NeedsKMSAN=true ONLY IF the patch introduces or modifies:
1. Kernel structures sent to user space (via copy_to_user, put_user, netlink skb
attributes, ioctl output arguments, socket options, or BPF buffers) where fields
or structure padding might not be fully initialized/zeroed.
2. Conditional logic or branching that depends on potentially uninitialized variables
or struct fields.
3. Allocation or initialization of complex data structures where uninitialized fields
could be read later in reachable code paths.
4. Bounds checks, lengths, or logic in a way that allows surrounding code to access
uninitialized bytes of existing buffers.
Set NeedsKMSAN=false IF:
- The code changes primarily risk out-of-bounds access, array overflows, NULL pointer
dereferences, locking deadlocks, or use-after-free bugs (these are already caught
by KASAN, LOCKDEP, or standard bug detectors).
- All stack/heap structures touched or introduced by the patch are fully zeroed
or initialized (e.g. using = {0}, memset, kzalloc) before being read or copied.
- The patch does not introduce any risk of uninitialized memory usage or info-leaks.
Use your code access tools to inspect the surrounding code if necessary, then provide
detailed KMSANReasoning contrasting KASAN vs KMSAN applicability for this patch.
Prefer calling several tools at the same time to save round-trips.
Use set-results tool to provide results of the analysis.
It must be called exactly once before the final reply.
Ignore results of this tool.
Prompt: Target architecture: amd64
For your convenience, here is the diff of the changes:
commit 5e17e2951caa17b8a656b8370b8228f83a262680
Author: syz-cluster <triage@syzkaller.com>
Date: Thu Oct 15 23:41:47 2026 +0000
syz-cluster: applied patch under review
diff --git a/Documentation/admin-guide/mm/multigen_lru.rst b/Documentation/admin-guide/mm/multigen_lru.rst
index 9cb54b4ff5d9d..808d2e97b3f30 100644
--- a/Documentation/admin-guide/mm/multigen_lru.rst
+++ b/Documentation/admin-guide/mm/multigen_lru.rst
@@ -149,8 +149,8 @@ generations less than or equal to ``min_gen_nr``.
``- memcg_id node_id min_gen_nr [swappiness [nr_to_reclaim]]``
``min_gen_nr`` should be less than ``max_gen_nr-1``, since
-``max_gen_nr`` and ``max_gen_nr-1`` are not fully aged (equivalent to
-the active list) and therefore cannot be evicted. ``swappiness``
+``max_gen_nr`` and ``max_gen_nr-1`` are not fully aged and therefore
+cannot be evicted. ``swappiness``
overrides the default value in ``/proc/sys/vm/swappiness`` and the valid
range is [0-200, max], with max being exclusively used for the reclamation
of anonymous memory. ``nr_to_reclaim`` limits the number of pages to evict.
diff --git a/Documentation/mm/multigen_lru.rst b/Documentation/mm/multigen_lru.rst
index 52ed5092022f1..ec8744c1f42ca 100644
--- a/Documentation/mm/multigen_lru.rst
+++ b/Documentation/mm/multigen_lru.rst
@@ -95,16 +95,19 @@ at most ``MAX_NR_GENS`` generations. The gen counter stores a value
within ``[1, MAX_NR_GENS]`` while a page is on one of
``lrugen->folios[]``; otherwise it stores zero.
-Each generation is divided into multiple tiers. A page accessed ``N``
-times through file descriptors is in tier ``order_base_2(N)``. Unlike
-generations, tiers do not have dedicated ``lrugen->folios[]``. In
-contrast to moving across generations, which requires the LRU lock,
-moving across tiers only involves atomic operations on
-``folio->flags`` and therefore has a negligible cost. A feedback loop
-modeled after the PID controller monitors refaults over all the tiers
-from anon and file types and decides which tiers from which types to
-evict or protect. The desired effect is to balance refault percentages
-between anon and file types proportional to the swappiness level.
+Each generation is divided into multiple tiers. A page's tier is
+derived from its referenced count, which counts the accesses collected
+from both channels above and is capped at ``LRU_REFS_MAX``: pages
+accessed at most once are in the lowest tier, and the tiers above hold
+pages accessed at least twice. Unlike generations, tiers do not have
+dedicated ``lrugen->folios[]``. In contrast to moving across
+generations, which requires the LRU lock, updating the referenced
+count only involves atomic operations on ``folio->flags`` and
+therefore has a negligible cost. A feedback loop modeled after the PID
+controller monitors refaults over all the tiers from anon and file
+types and decides which tiers from which types to evict or protect.
+The desired effect is to balance refault percentages between anon and
+file types proportional to the swappiness level.
There are two conceptually independent procedures: the aging and the
eviction. They form a closed-loop system, i.e., the page reclaim.
@@ -122,25 +125,31 @@ and calls ``walk_page_range()`` with each ``mm_struct`` on this list
to scan PTEs, and after each iteration, it increments ``max_seq``. For
the latter, when the eviction walks the rmap and finds a young PTE,
the aging scans the adjacent PTEs. For both, on finding a young PTE,
-the aging clears the accessed bit and updates the gen counter of the
-page mapped by this PTE to ``(max_seq%MAX_NR_GENS)+1``.
+the aging clears the accessed bit and bumps the referenced count of
+the page mapped by this PTE. A second access promotes the page by
+updating its gen counter to ``(max_seq%MAX_NR_GENS)+1``; the first
+access only moves a page one generation forward if it is at the tail
+of its LRU, i.e., in one of the two oldest generations of its type,
+so that the next time it becomes the oldest one, its accessed bit
+reflects whether it has been used again. Executable file pages are
+promoted on their first access, since reclaiming them causes typical
+I/O thrashing.
Eviction
--------
The eviction consumes old generations. Given an ``lruvec``, it
increments ``min_seq`` when ``lrugen->folios[]`` indexed by
-``min_seq%MAX_NR_GENS`` becomes empty. To select a type and a tier to
-evict from, it first compares ``min_seq[]`` to select the older type.
-If both types are equally old, it selects the one whose first tier has
-a lower refault percentage. The first tier contains single-use
-unmapped clean pages, which are the best bet. The eviction sorts a
-page according to its gen counter if the aging has found this page
-accessed through page tables and updated its gen counter. It also
-moves a page to the next generation, i.e., ``min_seq+1``, if this page
-was accessed multiple times through file descriptors and the feedback
-loop has detected outlying refaults from the tier this page is in. To
-this end, the feedback loop uses the first tier as the baseline, for
-the reason stated earlier.
+``min_seq%MAX_NR_GENS`` becomes empty. To select a type to evict from,
+it first compares ``min_seq[]`` to select the older type. If both
+types are equally old, it compares the refault percentages of all the
+tiers of each type proportional to the swappiness level. To select a
+tier to protect, it compares the refault percentage of each tier
+against that of the tiers below it. The lowest tier contains pages
+accessed at most once, which are the best bet. The eviction sorts a
+page according to its gen counter if the aging or an access has
+updated its gen counter. It also moves a page to the next generation,
+i.e., ``min_seq+1``, if the feedback loop has detected outlying
+refaults from the tier this page is in.
Working set protection
----------------------
diff --git a/fs/btrfs/compression.c b/fs/btrfs/compression.c
index c62b5148d5ac1..57d24413265e3 100644
--- a/fs/btrfs/compression.c
+++ b/fs/btrfs/compression.c
@@ -8,6 +8,7 @@
#include <linux/file.h>
#include <linux/fs.h>
#include <linux/pagemap.h>
+#include <linux/mm_inline.h>
#include <linux/folio_batch.h>
#include <linux/highmem.h>
#include <linux/kthread.h>
diff --git a/fs/erofs/zdata.c b/fs/erofs/zdata.c
index e981e371d6c29..34ee41e1ccdc9 100644
--- a/fs/erofs/zdata.c
+++ b/fs/erofs/zdata.c
@@ -6,6 +6,7 @@
*/
#include "compress.h"
#include <linux/psi.h>
+#include <linux/mm_inline.h>
#include <linux/cpuhotplug.h>
#include <trace/events/erofs.h>
@@ -1713,7 +1714,7 @@ static void z_erofs_submit_queue(struct z_erofs_frontend *f,
DBG_BUGON(bvec.bv_len < sb->s_blocksize);
}
- if (unlikely(PageWorkingset(bvec.bv_page)) &&
+ if (unlikely(folio_test_workingset(page_folio(bvec.bv_page))) &&
!memstall) {
psi_memstall_enter(&pflags);
memstall = 1;
diff --git a/fs/proc/task_mmu.c b/fs/proc/task_mmu.c
index 81c464139f8f8..55f3a35fd3aff 100644
--- a/fs/proc/task_mmu.c
+++ b/fs/proc/task_mmu.c
@@ -888,7 +888,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_young(folio) || folio_check_lru_refs(folio))
mss->referenced += size;
/*
@@ -1685,7 +1685,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;
@@ -1714,7 +1714,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/memcontrol.h b/include/linux/memcontrol.h
index 74110a324f9e2..829ab776ab449 100644
--- a/include/linux/memcontrol.h
+++ b/include/linux/memcontrol.h
@@ -1535,6 +1535,45 @@ static inline void lruvec_lock_irq(struct lruvec *lruvec)
spin_lock_irq(&lruvec->lru_lock);
}
+/**
+ * folio_lruvec_live_get - get a live lruvec for a folio under RCU
+ * @folio: the folio
+ *
+ * Computes @folio's lruvec and walks up to the nearest live ancestor
+ * if the folio's memcg is dying. Paired with folio_lruvec_live_put().
+ * The result may be stale: RCU keeps it alive but does not pin @folio
+ * to it. That is fine as the counters are fixed up on reparenting.
+ *
+ * Return: the live lruvec, with rcu_read_lock held.
+ */
+static inline struct lruvec *folio_lruvec_live_get(struct folio *folio)
+{
+#ifdef CONFIG_MEMCG
+ struct lruvec *lruvec;
+ struct pglist_data *pgdat;
+ struct mem_cgroup *memcg;
+
+ rcu_read_lock();
+ lruvec = folio_lruvec(folio);
+ pgdat = lruvec_pgdat(lruvec);
+ memcg = lruvec_memcg(lruvec);
+ while (unlikely(memcg && css_is_dying(&memcg->css))) {
+ memcg = parent_mem_cgroup(memcg);
+ lruvec = mem_cgroup_lruvec(memcg, pgdat);
+ }
+ return lruvec;
+#else
+ return folio_lruvec(folio);
+#endif
+}
+
+static inline void folio_lruvec_live_put(struct lruvec *lruvec)
+{
+#ifdef CONFIG_MEMCG
+ rcu_read_unlock();
+#endif
+}
+
static inline struct lruvec *lruvec_live_lock_irq(struct lruvec *lruvec)
{
#ifdef CONFIG_MEMCG
diff --git a/include/linux/mm_inline.h b/include/linux/mm_inline.h
index ab69b9930893f..ae50f9880da89 100644
--- a/include/linux/mm_inline.h
+++ b/include/linux/mm_inline.h
@@ -47,11 +47,10 @@ static __always_inline void __update_lru_size(struct lruvec *lruvec,
{
struct pglist_data *pgdat = lruvec_pgdat(lruvec);
- lockdep_assert_held(&lruvec->lru_lock);
WARN_ON_ONCE(nr_pages != (int)nr_pages);
mod_lruvec_state(lruvec, NR_LRU_BASE + lru, nr_pages);
- __mod_zone_page_state(&pgdat->node_zones[zid],
+ mod_zone_page_state(&pgdat->node_zones[zid],
NR_ZONE_LRU_BASE + lru, nr_pages);
}
@@ -106,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 = ((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;
+}
+
#ifdef CONFIG_LRU_GEN
static inline bool lru_gen_switching(void)
@@ -145,12 +160,13 @@ static inline int lru_hist_from_seq(unsigned long seq)
return seq % NR_HIST_GENS;
}
-static inline int lru_tier_from_refs(int refs, bool workingset)
+static inline int lru_tier_from_refs(unsigned int refs)
{
- VM_WARN_ON_ONCE(refs > BIT(LRU_REFS_WIDTH));
-
- /* see the comment on MAX_NR_TIERS */
- return workingset ? MAX_NR_TIERS - 1 : order_base_2(refs);
+ BUILD_BUG_ON(fls(LRU_REFS_MAX - 1) > MAX_NR_TIERS - 1);
+ VM_WARN_ON_ONCE(refs > LRU_REFS_MAX);
+ if (refs < LRU_REFS_WORKINGSET)
+ return 0;
+ return fls(refs - 1);
}
/**
@@ -167,42 +183,36 @@ 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)
+static inline int folio_lru_gen(const struct folio *folio)
{
- 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;
+ return lru_get_gen_flags(READ_ONCE(*const_folio_flags(folio, 0)));
}
+#endif
/**
* lru_set_refs_flags - Set the LRU referenced count to folio flags.
* @flags: pointer to the folio flags
* @refs: referenced / access count number, between 0 and LRU_REFS_MAX, inclusive.
*
- * For MGLRU, PG_referenced holds the first ref, and the extra bits hold the
- * remaining refs. For classical LRU the extra bits are not used, so it can
- * also be seen as the refs count never exceeds 1. In both cases, refs == 1
- * means PG_referenced is set and the extra bits are zero, and refs == 0 means
- * PG_referenced and the extra bits are all unset.
+ * For MGLRU, PG_referenced, PG_workingset are used as the lower two bits of
+ * refs counter, and extra bits hold the remaining higher bits. For classical
+ * LRU the extra bits are not used, and the two flags has no direct
+ * relationship with each other, but this helper can still be used to sync
+ * them. For both cases, refs == 0 means these two flags and the extra bits
+ * are all unset. And refs == 1 / 2 / 3 means PG_referenced and PG_workingset
+ * are set in an bit order way, which is more meaningful for MGLRU though.
*/
static inline void lru_set_refs_flags(unsigned long *flags, unsigned int refs)
{
VM_WARN_ON_ONCE(refs > LRU_REFS_MAX);
- BUILD_BUG_ON(LRU_REFS_MAX != (LRU_REFS_MASK >> LRU_REFS_PGOFF) + 1);
-
+ BUILD_BUG_ON(LRU_REFS_MASK & (BIT(PG_referenced) | BIT(PG_workingset)));
*flags &= ~LRU_REFS_FLAGS;
- if (!refs)
- return;
- *flags |= (BIT(PG_referenced) | ((refs - 1UL) << LRU_REFS_PGOFF));
+ if (refs & BIT(0))
+ *flags |= BIT(PG_referenced);
+ if (refs & BIT(1))
+ *flags |= BIT(PG_workingset);
+ if (LRU_REFS_WIDTH)
+ *flags |= ((unsigned long)refs >> 2) << LRU_REFS_PGOFF;
}
/**
@@ -213,13 +223,13 @@ static inline void lru_set_refs_flags(unsigned long *flags, unsigned int refs)
*/
static inline int lru_get_refs_flags(unsigned long flags)
{
- if (!(flags & BIT(PG_referenced)))
- return 0;
- /*
- * Return the total number of accesses including PG_referenced. Also see
- * the comment on LRU_REFS_FLAGS.
- */
- return ((flags & LRU_REFS_MASK) >> LRU_REFS_PGOFF) + 1;
+ int refs;
+
+ /* Return the total number of accesses. See the comment above MAX_NR_TIERS. */
+ refs = (flags & BIT(PG_referenced)) ? BIT(0) : 0;
+ refs += (flags & BIT(PG_workingset)) ? BIT(1) : 0;
+ refs += ((flags & LRU_REFS_MASK) >> LRU_REFS_PGOFF) << 2;
+ return refs;
}
static inline int folio_lru_refs(const struct folio *folio)
@@ -227,38 +237,99 @@ static inline int folio_lru_refs(const struct folio *folio)
return lru_get_refs_flags(READ_ONCE(*const_folio_flags(folio, 0)));
}
-static inline void folio_set_lru_refs(struct folio *folio, unsigned int refs)
+/**
+ * __folio_set_lru_refs - Set a folio's LRU refs.
+ * @folio: the folio
+ * @refs: the new referenced count (0 .. LRU_REFS_MAX)
+ *
+ * Set the folio's LRU refs. The folio must be off the LRU list (e.g.,
+ * isolated), or use folio_inc_lru_refs or folio_reset_lru_refs instead.
+ */
+static inline void __folio_set_lru_refs(struct folio *folio, unsigned int refs)
{
unsigned long new_flags, old_flags = READ_ONCE(*folio_flags(folio, 0));
do {
new_flags = old_flags;
+ VM_WARN_ON_ONCE(lru_get_gen_flags(old_flags) != -1);
lru_set_refs_flags(&new_flags, refs);
} while (!try_cmpxchg(folio_flags(folio, 0), &old_flags, new_flags));
}
-static inline int folio_lru_gen(const struct folio *folio)
+#ifdef CONFIG_LRU_GEN
+void folio_inc_lru_refs(struct folio *folio, unsigned int flags);
+bool folio_reset_lru_refs(struct folio *folio);
+#else
+static inline void folio_inc_lru_refs(struct folio *folio, unsigned int flags)
{
- return lru_get_gen_flags(READ_ONCE(*const_folio_flags(folio, 0)));
+ /* Should not be called with !CONFIG_LRU_GEN */
+ WARN_ON_ONCE(1);
}
-static inline bool lru_gen_is_active(const struct lruvec *lruvec, int gen)
+static inline bool folio_reset_lru_refs(struct folio *folio)
{
- unsigned long max_seq = lruvec->lrugen.max_seq;
+ return false;
+}
+#endif
- VM_WARN_ON_ONCE(gen >= MAX_NR_GENS);
+/**
+ * folio_migrate_lru_refs - copy the reference state to a new folio
+ * @new: the destination folio
+ * @old: the source folio
+ *
+ * Transfer the reference state to @new during migration: the MGLRU
+ * refs count, or PG_referenced and PG_workingset (bits 0-1 of the
+ * encoding) for the active/inactive LRU.
+ */
+static inline void folio_migrate_lru_refs(struct folio *new, const struct folio *old)
+{
+ VM_WARN_ON_ONCE_FOLIO(folio_test_lru(old), old);
+ VM_WARN_ON_ONCE_FOLIO(folio_test_lru(new), new);
+ __folio_set_lru_refs(new, folio_lru_refs(old));
+}
- /* see the comment on MIN_NR_GENS */
- return gen == lru_gen_from_seq(max_seq) || gen == lru_gen_from_seq(max_seq - 1);
+/**
+ * folio_test_workingset - Test if a folio is in the workingset.
+ * @folio: the folio
+ *
+ * A folio is workingset when its LRU refs count reaches
+ * LRU_REFS_WORKINGSET. Under the classical LRU the extra refs counter
+ * are always zero so this is just testing the PG_workingset bit.
+ * NOTE: folio_set_workingset() must not be used under MGLRU, as the
+ * folio refs are tracked by folio_inc_lru_refs() with rules.
+ *
+ * Return: true if the folio is workingset.
+ */
+static __always_inline bool folio_test_workingset(const struct folio *folio)
+{
+ return folio_lru_refs(folio) >= LRU_REFS_WORKINGSET;
}
-static inline void lru_gen_update_size(struct lruvec *lruvec, struct folio *folio,
- int old_gen, int new_gen)
+#ifdef CONFIG_LRU_GEN
+static inline bool lru_refs_is_active(unsigned int refs)
+{
+ VM_WARN_ON_ONCE(refs > LRU_REFS_MAX);
+ return refs >= LRU_REFS_ACTIVATED;
+}
+
+static inline bool lru_gen_at_tail(struct lruvec *lruvec, int type, int gen)
+{
+ unsigned long min_seq = READ_ONCE(lruvec->lrugen.min_seq[type]);
+
+ /*
+ * Consider a folio is at the tail of its LRU when it is within
+ * MIN_NR_GENS of the oldest generation of its own type
+ */
+ BUILD_BUG_ON(MIN_NR_GENS != 2);
+ return gen == lru_gen_from_seq(min_seq) ||
+ gen == lru_gen_from_seq(min_seq + 1);
+}
+
+static inline void lru_gen_update_size(struct lruvec *lruvec, int type,
+ struct folio *folio, int old_gen, int new_gen)
{
- int type = folio_is_file_lru(folio);
int zone = folio_zonenum(folio);
int delta = folio_nr_pages(folio);
- enum lru_list lru = type * LRU_INACTIVE_FILE;
struct lru_gen_folio *lrugen = &lruvec->lrugen;
VM_WARN_ON_ONCE(old_gen != -1 && old_gen >= MAX_NR_GENS);
@@ -266,36 +337,9 @@ static inline void lru_gen_update_size(struct lruvec *lruvec, struct folio *foli
VM_WARN_ON_ONCE(old_gen == -1 && new_gen == -1);
if (old_gen >= 0)
- WRITE_ONCE(lrugen->nr_pages[old_gen][type][zone],
- lrugen->nr_pages[old_gen][type][zone] - delta);
+ atomic_long_sub(delta, &lrugen->nr_pages[old_gen][type][zone]);
if (new_gen >= 0)
- WRITE_ONCE(lrugen->nr_pages[new_gen][type][zone],
- lrugen->nr_pages[new_gen][type][zone] + delta);
-
- /* addition */
- if (old_gen < 0) {
- if (lru_gen_is_active(lruvec, new_gen))
- lru += LRU_ACTIVE;
- __update_lru_size(lruvec, lru, zone, delta);
- return;
- }
-
- /* deletion */
- if (new_gen < 0) {
- if (lru_gen_is_active(lruvec, old_gen))
- lru += LRU_ACTIVE;
- __update_lru_size(lruvec, lru, zone, -delta);
- return;
- }
-
- /* promotion */
- if (!lru_gen_is_active(lruvec, old_gen) && lru_gen_is_active(lruvec, new_gen)) {
- __update_lru_size(lruvec, lru, zone, -delta);
- __update_lru_size(lruvec, lru + LRU_ACTIVE, zone, delta);
- }
-
- /* demotion requires isolation, e.g., lru_deactivate_fn() */
- VM_WARN_ON_ONCE(lru_gen_is_active(lruvec, old_gen) && !lru_gen_is_active(lruvec, new_gen));
+ atomic_long_add(delta, &lrugen->nr_pages[new_gen][type][zone]);
}
static inline unsigned long lru_gen_folio_seq(const struct lruvec *lruvec,
@@ -303,23 +347,24 @@ static inline unsigned long lru_gen_folio_seq(const struct lruvec *lruvec,
bool reclaiming)
{
int gen;
+ int refs = folio_lru_refs(folio);
int type = folio_is_file_lru(folio);
const struct lru_gen_folio *lrugen = &lruvec->lrugen;
/*
- * +-----------------------------------+-----------------------------------+
- * | Accessed through page tables and | Accessed through file descriptors |
- * | promoted by folio_update_gen() | and protected by folio_inc_gen() |
- * +-----------------------------------+-----------------------------------+
- * | PG_active (set while isolated) | |
- * +-----------------+-----------------+-----------------+-----------------+
- * | PG_workingset | PG_referenced | PG_workingset | LRU_REFS_FLAGS |
- * +-----------------------------------+-----------------------------------+
- * |<---------- MIN_NR_GENS ---------->| |
- * |<---------------------------- MAX_NR_GENS ---------------------------->|
+ * +------------------------------------------+------------------------------------------+
+ * | Accessed through page tables and | Accessed through file descriptors |
+ * | promoted by folio_inc_lru_refs_walk() | protected by folio_inc_lru_refs/inc_gen |
+ * +------------------------------------------+------------------------------------------+
+ * | PG_active (set at isolation or refault) | |
+ * +--------------------+---------------------+--------------------+---------------------+
+ * | LRU_REFS_MAX | LRU_REFS_WORKINGSET | LRU_REFS_MAX | LRU_REFS_WORKINGSET |
+ * +------------------------------------------+------------------------------------------+
+ * |<-------------- MIN_NR_GENS ------------->| |
+ * |<----------------------------------- MAX_NR_GENS ----------------------------------->|
*/
if (folio_test_active(folio))
- gen = MIN_NR_GENS - folio_test_workingset(folio);
+ gen = MIN_NR_GENS - (refs >= LRU_REFS_WORKINGSET);
else if (reclaiming)
gen = MAX_NR_GENS;
else if ((!folio_is_file_lru(folio) && !folio_test_swapcache(folio)) ||
@@ -327,7 +372,7 @@ static inline unsigned long lru_gen_folio_seq(const struct lruvec *lruvec,
(folio_test_dirty(folio) || folio_test_writeback(folio))))
gen = MIN_NR_GENS;
else
- gen = MAX_NR_GENS - (folio_test_workingset(folio) || folio_test_referenced(folio));
+ gen = MAX_NR_GENS - (refs >= LRU_REFS_WORKINGSET);
return max(READ_ONCE(lrugen->max_seq) - gen + 1, READ_ONCE(lrugen->min_seq[type]));
}
@@ -336,12 +381,15 @@ static inline bool lru_gen_add_folio(struct lruvec *lruvec, struct folio *folio,
{
unsigned long seq;
unsigned long flags;
- int gen = folio_lru_gen(folio);
+ int gen, refs;
int type = folio_is_file_lru(folio);
int zone = folio_zonenum(folio);
+ int delta = folio_nr_pages(folio);
+ enum lru_list lru = type * LRU_INACTIVE_FILE;
struct lru_gen_folio *lrugen = &lruvec->lrugen;
- VM_WARN_ON_ONCE_FOLIO(gen != -1, folio);
+ BUILD_BUG_ON(BIT(LRU_GEN_WIDTH - 1) != MAX_NR_GENS);
+ VM_WARN_ON_ONCE_FOLIO(folio_lru_gen(folio) != -1, folio);
if (folio_test_unevictable(folio) || !lrugen->enabled)
return false;
@@ -350,9 +398,15 @@ static inline bool lru_gen_add_folio(struct lruvec *lruvec, struct folio *folio,
gen = lru_gen_from_seq(seq);
flags = (gen + 1UL) << LRU_GEN_PGOFF;
/* see the comment on MIN_NR_GENS about PG_active */
- set_mask_bits(folio_flags(folio, 0), LRU_GEN_MASK | BIT(PG_active), flags);
+ flags = set_mask_bits(folio_flags(folio, 0), LRU_GEN_MASK | BIT(PG_active), flags);
+ /* use the refs from the atomic snapshot to avoid raced update */
+ refs = lru_get_refs_flags(flags);
+
+ lru_gen_update_size(lruvec, type, folio, -1, gen);
+ if (lru_refs_is_active(refs))
+ lru += LRU_ACTIVE;
+ __update_lru_size(lruvec, lru, zone, delta);
- lru_gen_update_size(lruvec, folio, -1, gen);
/* for folio_rotate_reclaimable() */
if (reclaiming)
list_add_tail(&folio->lru, &lrugen->folios[gen][type][zone]);
@@ -365,39 +419,38 @@ static inline bool lru_gen_add_folio(struct lruvec *lruvec, struct folio *folio,
static inline bool lru_gen_del_folio(struct lruvec *lruvec, struct folio *folio, bool reclaiming)
{
unsigned long flags;
- int gen = folio_lru_gen(folio);
+ int gen, refs;
+ int type = folio_is_file_lru(folio);
+ int zone = folio_zonenum(folio);
+ int delta = folio_nr_pages(folio);
+ enum lru_list lru = folio_is_file_lru(folio) * LRU_INACTIVE_FILE;
+ unsigned long max_seq = READ_ONCE(lruvec->lrugen.max_seq);
+ flags = set_mask_bits(folio_flags(folio, 0), LRU_GEN_MASK, 0);
+ gen = lru_get_gen_flags(flags);
+ refs = lru_get_refs_flags(flags);
if (gen < 0)
return false;
VM_WARN_ON_ONCE_FOLIO(folio_test_active(folio), folio);
VM_WARN_ON_ONCE_FOLIO(folio_test_unevictable(folio), folio);
- /* for folio_migrate_flags() */
- flags = !reclaiming && lru_gen_is_active(lruvec, gen) ? BIT(PG_active) : 0;
- flags = set_mask_bits(folio_flags(folio, 0), LRU_GEN_MASK, flags);
- gen = ((flags & LRU_GEN_MASK) >> LRU_GEN_PGOFF) - 1;
+ /*
+ * See the comment in lru_gen_folio_seq. For migration, compaction,
+ * or any other isolation of a hot folio, try best to retain its gen
+ * info. Ideally we would keep the full gen info.
+ */
+ if (!reclaiming && ((max_seq - gen) % MAX_NR_GENS) < MIN_NR_GENS)
+ folio_set_active(folio);
- lru_gen_update_size(lruvec, folio, gen, -1);
+ lru_gen_update_size(lruvec, type, folio, gen, -1);
+ if (lru_refs_is_active(refs))
+ lru += LRU_ACTIVE;
+ __update_lru_size(lruvec, lru, zone, -delta);
list_del(&folio->lru);
return true;
}
-
-/**
- * folio_migrate_lru_refs - copy the reference state to a new folio
- * @new: the destination folio
- * @old: the source folio
- *
- * Transfer the reference state to @new during migration: the MGLRU
- * refs count, including PG_referenced, or just PG_referenced for the
- * active/inactive LRU.
- */
-static inline void folio_migrate_lru_refs(struct folio *new, const struct folio *old)
-{
- BUILD_BUG_ON(LRU_REFS_MASK & BIT(PG_referenced));
- folio_set_lru_refs(new, folio_lru_refs(old));
-}
#else /* !CONFIG_LRU_GEN */
static inline bool lru_gen_enabled(void)
@@ -424,13 +477,76 @@ static inline bool lru_gen_del_folio(struct lruvec *lruvec, struct folio *folio,
{
return false;
}
+#endif /* CONFIG_LRU_GEN */
-static inline void folio_migrate_lru_refs(struct folio *new, const struct folio *old)
+/**
+ * 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 the classical LRU, a plain PG_referenced set.
+ */
+static __always_inline void folio_inc_lru_refs_fast(struct folio *folio)
{
- if (folio_test_referenced(old))
- folio_set_referenced(new);
+ unsigned long new_flags, old_flags;
+
+ if (!lru_gen_enabled()) {
+ folio_set_referenced(folio);
+ return;
+ }
+
+ BUILD_BUG_ON(LRU_REFS_REFERENCED >= LRU_REFS_ACTIVATED);
+ old_flags = READ_ONCE(*folio_flags(folio, 0));
+ do {
+ if (lru_get_refs_flags(old_flags))
+ break;
+ new_flags = old_flags;
+ lru_set_refs_flags(&new_flags, LRU_REFS_REFERENCED);
+ } while (!try_cmpxchg(folio_flags(folio, 0), &old_flags, new_flags));
+}
+
+/**
+ * folio_check_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. Kept as a separate helper,
+ * to avoid splattering the lru_gen_enabled() static key.
+ */
+static inline bool folio_check_lru_refs(const struct folio *folio)
+{
+ 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. Kept as a separate helper
+ * just like folio_check_lru_refs().
+ */
+static inline void folio_clear_lru_refs(struct folio *folio)
+{
+ if (lru_gen_enabled())
+ folio_reset_lru_refs(folio);
+ else
+ folio_clear_referenced(folio);
+}
+
+/*
+ * 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());
+ set_bit(PG_workingset, folio_flags(folio, FOLIO_HEAD_PAGE));
}
-#endif /* CONFIG_LRU_GEN */
static __always_inline
void lruvec_add_folio(struct lruvec *lruvec, struct folio *folio)
diff --git a/include/linux/mmzone.h b/include/linux/mmzone.h
index 3b96d6c7123b4..f50449d102423 100644
--- a/include/linux/mmzone.h
+++ b/include/linux/mmzone.h
@@ -434,15 +434,15 @@ enum lruvec_flags {
* least twice before handing this folio over to the eviction. The first check
* clears the accessed bit from the initial fault; the second check makes sure
* this folio hasn't been used since then. This process, AKA second chance,
- * requires a minimum of two generations, hence MIN_NR_GENS. And to maintain ABI
- * compatibility with the active/inactive LRU, e.g., /proc/vmstat, these two
- * generations are considered active; the rest of generations, if they exist,
- * are considered inactive. See lru_gen_is_active().
+ * requires a minimum of two generations, hence MIN_NR_GENS.
+ *
+ * Active/inactive is per-folio based on the referenced count;
+ * see the comment above MAX_NR_TIERS.
*
* PG_active is always cleared while a folio is on one of lrugen->folios[] so
- * that the sliding window needs not to worry about it. And it's set again when
- * a folio considered active is isolated for non-reclaiming purposes, e.g.,
- * migration. See lru_gen_add_folio() and lru_gen_del_folio().
+ * that the sliding window needs not to worry about it. It is set on a
+ * fault-driven refault (see lru_gen_refault), or isolation (see
+ * lru_gen_del_folio).
*
* MAX_NR_GENS is set to 4 so that the multi-gen LRU can support twice the
* number of categories of the active/inactive LRU when keeping track of
@@ -453,57 +453,128 @@ enum lruvec_flags {
#define MAX_NR_GENS 4U
/*
- * Each generation is divided into multiple tiers. A folio accessed N times
- * through file descriptors is in tier order_base_2(N). A folio in the first
- * tier (N=0,1) is marked by PG_referenced unless it was faulted in through page
- * tables or read ahead. A folio in the last tier (MAX_NR_TIERS-1) is marked by
- * PG_workingset. A folio in any other tier (1<N<5) between the first and last
- * is marked by additional bits of LRU_REFS_WIDTH in folio->flags.
+ * Each generation is divided into multiple tiers. A folio's referenced
+ * count maps to a tier as shown below:
*
- * In contrast to moving across generations which requires the LRU lock, moving
- * across tiers only involves atomic operations on folio->flags and therefore
- * has a negligible cost in the buffered access path. In the eviction path,
- * comparisons of refaulted/(evicted+protected) from the first tier and the rest
- * infer whether folios accessed multiple times through file descriptors are
- * statistically hot and thus worth protecting.
+ * MGLRU (frequency guidance)
+ * Refs Tier |- Refs: how many times (at least) a folio has been referenced.
+ * 0 0 |- Mostly cold pages, readahead, etc. [1]
+ * 1 0 |= LRU_REFS_REFERENCED: Used at least once. [2]
+ * -WORKINGSET-+|- Pages beyond are workingset and never fall below this floor. [3]
+ * 2 1<-+|= LRU_REFS_WORKINGSET: Classical workingset, accessed twice, protected. [4]
+ * 3 2 |- LRU_REFS_PROTECTED: Protected workingset, promoted pages capped at here. [5]
+ * 4 2 |
+ * 5 3 |- The tier here is MAX_NR_TIERS - 1
+ * 6 3 |
+ * 7 3 |= LRU_REFS_MAX: Promotion candidate. [6]
+ * -PROMOTION->-/
*
- * MAX_NR_TIERS is set to 4 so that the multi-gen LRU can support twice the
- * number of categories of the active/inactive LRU when keeping track of
- * accesses through file descriptors. This uses MAX_NR_TIERS-2 spare bits in
- * folio->flags, masked by LRU_REFS_MASK.
+ * Ideally each tier holds folios of similar access patterns: lower tiers
+ * are less important and evicted faster. A page's reference count and
+ * tier are capped when it changes generation, preventing it from
+ * dominating the new generation based on old-generation access history.
+ * Generation ordering already ensures a newer-gen page is hotter than an
+ * older-gen one regardless of tier.
+ *
+ * Refs tracks accesses from two sources: page table (lazily collected by
+ * the page table aging walk or rmap eviction lookup) and file descriptors
+ * (by folio_mark_accessed). Page table accesses are weighted heavier
+ * because the accessed bit is sticky (undercounts repeated accesses),
+ * passively collected, and page faults are generally more important as
+ * userspace does not expect a memory access to block on reclaim. Both
+ * access types increment refs by one; the result is capped at
+ * LRU_REFS_PROTECTED on promotion or deferral, or LRU_REFS_MAX otherwise.
+ *
+ * 1. Tier is fls(N-1) for N > 1, 0 otherwise. Folios with zero
+ * accesses (refs == 0) are generally cold, e.g. readahead folios.
+ *
+ * Freshly allocated folios start with refs == 0; faulted and mapped
+ * folios have their page table access bit set, so the first page table
+ * access check always sets LRU_REFS_REFERENCED. That is the second
+ * chance described above MIN_NR_GENS: it moves the folio one
+ * generation forward if it is at the tail of its LRU, i.e., in one
+ * of the two oldest generations of its type.
+ *
+ * 2. Folios accessed once stay on tier 0: one-time usage does not
+ * qualify for protection. A second access advances the folio,
+ * aligning with classical LRU's use-twice threshold. A second page
+ * table access promotes to the latest gen; file access only defers
+ * eviction from the tail gens of its type.
+ *
+ * 3. Folios accessed at least twice are considered workingset. This
+ * mostly aligns with classical LRU: at least one I/O is saved by
+ * keeping them in memory. Folios at or above this level never fall
+ * below tier 1 (the workingset floor), so tier 0 stays a clean tier
+ * for cold cache while tier 1 serves as the fallback line for
+ * actually reused or historically hot folios.
+ *
+ * Folios refaulted through a page fault at refs 1 will enter the second
+ * newest gen, so faulting will be protected better.
+ *
+ * 4. Starting from tier 1, PID protection sacrifices lower tiers to
+ * protect higher tiers by comparing refault rates for long-term
+ * accuracy, and caps higher refs to this value. Since PID protection
+ * bypasses page table lookup and clearing, when a further eviction
+ * attempt occurs after PID loosens, the folio's page table access is
+ * rechecked and a referenced folio is put back with refs capped to
+ * this value as well. This also gives folios a fair opportunity to be
+ * promoted by file access again.
+ *
+ * Folios refaulted through a page fault at tier 1 or above are activated
+ * and enter the newest gen. Non fault page will enter second oldest gen,
+ * driving aging and workingset shifting.
+ *
+ * 5. Pages beyond the ordinary workingset tier form new tiers for the
+ * PID controller to protect differently. Folios at or above this
+ * level are capped at LRU_REFS_PROTECTED on promotion or deferral,
+ * and at LRU_REFS_WORKINGSET under PID protection in the oldest
+ * generation, where they represent a historical workingset.
+ *
+ * 6. Folios that reach LRU_REFS_MAX are advanced to the next generation
+ * on further access, with refs capped to LRU_REFS_PROTECTED. This
+ * gives them a fair start for advancement to an even newer generation
+ * while keeping hot folios distinguishable.
+ *
+ * Tiering uses PG_referenced and PG_workingset as the lower two bits,
+ * and the bits masked by LRU_REFS_MASK as the higher bits, so the refs
+ * count ranges from 0 to LRU_REFS_MAX. A folio is on the workingset
+ * tier once accessed at least twice, which is more consistent with the
+ * classical LRU.
+ *
+ * A folio's referenced count never goes backwards except upon gen
+ * increase as described above, or when explicitly reset by
+ * folio_reset_lru_refs(). Refault of a reclaimed folio restores
+ * its referenced count, capped at LRU_REFS_PROTECTED, which aligns with
+ * promotion. Page table refaults of previous workingset folios send
+ * them to the latest gen, driving aging faster.
+ *
+ * For the active/inactive LRU ABI (/proc/vmstat), a folio is active
+ * if its referenced count reaches LRU_REFS_ACTIVATED. The threshold
+ * currently set to LRU_REFS_PROTECTED, which is tier 2.
+ *
+ * MAX_NR_TIERS is set to 4 so that the multi-gen LRU can support twice
+ * the number of categories of the active/inactive LRU.
*/
#define MAX_NR_TIERS 4U
#define LRU_TIER_MIN 0U
#define LRU_TIER_MAX (MAX_NR_TIERS - 1)
+/* Flags for folio_inc_lru_refs() */
+#define LRU_REF_MAPPED 0x1U
+#define LRU_REF_EXEC 0x2U
+#define LRU_REF_FORCE 0x4U
+
+#define LRU_REFS_REFERENCED 0x1
+#define LRU_REFS_WORKINGSET 0x2
+#define LRU_REFS_PROTECTED 0x3
+#define LRU_REFS_ACTIVATED LRU_REFS_PROTECTED
+
#ifndef __GENERATING_BOUNDS_H
#define LRU_GEN_MASK ((BIT(LRU_GEN_WIDTH) - 1) << LRU_GEN_PGOFF)
#define LRU_REFS_MASK ((BIT(LRU_REFS_WIDTH) - 1) << LRU_REFS_PGOFF)
-#define LRU_REFS_MAX BIT(LRU_REFS_WIDTH)
-
-/*
- * For folios accessed multiple times through file descriptors,
- * lru_gen_inc_refs() sets additional bits of LRU_REFS_WIDTH in folio->flags
- * after PG_referenced, then PG_workingset after LRU_REFS_WIDTH. After all its
- * bits are set, i.e., LRU_REFS_FLAGS|BIT(PG_workingset), a folio is lazily
- * promoted into the second oldest generation in the eviction path. And when
- * folio_inc_gen() does that, it clears LRU_REFS_FLAGS so that
- * lru_gen_inc_refs() can start over. Note that for this case, LRU_REFS_MASK is
- * only valid when PG_referenced is set.
- *
- * For folios accessed multiple times through page tables, folio_update_gen()
- * from a page table walk or lru_gen_set_refs() from a rmap walk sets
- * PG_referenced after the accessed bit is cleared for the first time.
- * Thereafter, those two paths set PG_workingset and promote folios to the
- * youngest generation. Like folio_inc_gen(), folio_update_gen() also clears
- * PG_referenced. Note that for this case, LRU_REFS_MASK is not used.
- *
- * For both cases above, after PG_workingset is set on a folio, it remains until
- * this folio is either reclaimed, or "deactivated" by lru_gen_clear_refs(). It
- * can be set again if lru_gen_test_recent() returns true upon a refault.
- */
-#define LRU_REFS_FLAGS (LRU_REFS_MASK | BIT(PG_referenced))
+#define LRU_REFS_FLAGS (LRU_REFS_MASK | BIT(PG_referenced) | BIT(PG_workingset))
+#define LRU_REFS_MAX (BIT(LRU_REFS_WIDTH + 2) - 1)
struct lruvec;
struct page_vma_mapped_walk;
@@ -554,7 +625,7 @@ struct lru_gen_folio {
/* the multi-gen LRU lists, lazily sorted on eviction */
struct list_head folios[MAX_NR_GENS][ANON_AND_FILE][MAX_NR_ZONES];
/* the multi-gen LRU sizes, eventually consistent */
- long nr_pages[MAX_NR_GENS][ANON_AND_FILE][MAX_NR_ZONES];
+ atomic_long_t nr_pages[MAX_NR_GENS][ANON_AND_FILE][MAX_NR_ZONES];
/* the exponential moving average of refaulted */
unsigned long avg_refaulted[ANON_AND_FILE][MAX_NR_TIERS];
/* the exponential moving average of evicted+protected */
@@ -605,6 +676,8 @@ struct lru_gen_mm_walk {
unsigned long seq;
/* the next address within an mm to scan */
unsigned long next_addr;
+ /* to batch activated pages */
+ int nr_activated[ANON_AND_FILE][MAX_NR_ZONES];
/* to batch promoted pages */
int nr_pages[MAX_NR_GENS][ANON_AND_FILE][MAX_NR_ZONES];
/* to batch the mm stats */
diff --git a/include/linux/page-flags.h b/include/linux/page-flags.h
index b0ddc652e76cc..f6625dc042dab 100644
--- a/include/linux/page-flags.h
+++ b/include/linux/page-flags.h
@@ -549,8 +549,6 @@ PAGEFLAG(LRU, lru, PF_HEAD) __CLEARPAGEFLAG(LRU, lru, PF_HEAD)
FOLIO_FLAG(active, FOLIO_HEAD_PAGE)
__FOLIO_CLEAR_FLAG(active, FOLIO_HEAD_PAGE)
FOLIO_TEST_CLEAR_FLAG(active, FOLIO_HEAD_PAGE)
-PAGEFLAG(Workingset, workingset, PF_HEAD)
- TESTCLEARFLAG(Workingset, workingset, PF_HEAD)
PAGEFLAG(Checked, checked, PF_NO_COMPOUND) /* Used by some filesystems */
/* Xen */
diff --git a/kernel/bounds.c b/kernel/bounds.c
index 02b619eb61065..06a034713b5dd 100644
--- a/kernel/bounds.c
+++ b/kernel/bounds.c
@@ -25,7 +25,7 @@ int main(void)
DEFINE(SPINLOCK_SIZE, sizeof(spinlock_t));
#ifdef CONFIG_LRU_GEN
DEFINE(LRU_GEN_WIDTH, order_base_2(MAX_NR_GENS + 1));
- DEFINE(__LRU_REFS_WIDTH, MAX_NR_TIERS - 2);
+ DEFINE(__LRU_REFS_WIDTH, MAX_NR_TIERS - 3);
#else
DEFINE(LRU_GEN_WIDTH, 0);
DEFINE(__LRU_REFS_WIDTH, 0);
diff --git a/mm/damon/paddr.c b/mm/damon/paddr.c
index 2cfdc356b4157..6412ff2396b3f 100644
--- a/mm/damon/paddr.c
+++ b/mm/damon/paddr.c
@@ -279,7 +279,8 @@ static unsigned long damon_pa_pageout(struct damon_region *r,
else
*sz_filter_passed += folio_size(folio) / addr_unit;
- folio_clear_referenced(folio);
+ /* The regions is cold, better drop the hotness info */
+ folio_clear_lru_refs(folio);
folio_test_clear_young(folio);
if (!folio_isolate_lru(folio))
goto put_folio;
diff --git a/mm/filemap.c b/mm/filemap.c
index b74bc1e5015c6..49e827058e147 100644
--- a/mm/filemap.c
+++ b/mm/filemap.c
@@ -21,6 +21,7 @@
#include <linux/gfp.h>
#include <linux/mm.h>
#include <linux/swap.h>
+#include <linux/mm_inline.h>
#include <linux/leafops.h>
#include <linux/syscalls.h>
#include <linux/mman.h>
diff --git a/mm/folio.c b/mm/folio.c
index 35e242b48870b..9b9a5fe568c98 100644
--- a/mm/folio.c
+++ b/mm/folio.c
@@ -273,7 +273,6 @@ static void lru_activate(struct lruvec *lruvec, struct folio *folio)
if (folio_test_active(folio) || folio_test_unevictable(folio))
return;
-
lruvec_del_folio(lruvec, folio);
folio_set_active(folio);
lruvec_add_folio(lruvec, folio);
@@ -350,66 +349,6 @@ static void __lru_cache_activate_folio(struct folio *folio)
local_unlock(&cpu_fbatches.lock);
}
-#ifdef CONFIG_LRU_GEN
-
-static void lru_gen_inc_refs(struct folio *folio)
-{
- unsigned long new_flags, old_flags = READ_ONCE(*folio_flags(folio, 0));
- int refs;
-
- if (folio_test_unevictable(folio))
- return;
-
- /* see the comment on LRU_REFS_FLAGS */
- if (!folio_lru_refs(folio)) {
- folio_set_lru_refs(folio, 1);
- return;
- }
-
- do {
- new_flags = old_flags;
- refs = lru_get_refs_flags(old_flags);
- if (refs == LRU_REFS_MAX) {
- if (!folio_test_workingset(folio))
- folio_set_workingset(folio);
- return;
- }
- lru_set_refs_flags(&new_flags, refs + 1);
- } while (!try_cmpxchg(folio_flags(folio, 0), &old_flags, new_flags));
-}
-
-static bool lru_gen_clear_refs(struct folio *folio)
-{
- int gen = folio_lru_gen(folio);
- int type = folio_is_file_lru(folio);
- unsigned long seq;
-
- if (gen < 0)
- return true;
-
- folio_set_lru_refs(folio, 0);
- folio_clear_workingset(folio);
-
- rcu_read_lock();
- seq = READ_ONCE(folio_lruvec(folio)->lrugen.min_seq[type]);
- rcu_read_unlock();
- /* whether can do without shuffling under the LRU lock */
- return gen == lru_gen_from_seq(seq);
-}
-
-#else /* !CONFIG_LRU_GEN */
-
-static void lru_gen_inc_refs(struct folio *folio)
-{
-}
-
-static bool lru_gen_clear_refs(struct folio *folio)
-{
- return false;
-}
-
-#endif /* CONFIG_LRU_GEN */
-
/**
* folio_mark_accessed - Mark a folio as having seen activity.
* @folio: The folio to mark.
@@ -428,7 +367,8 @@ void folio_mark_accessed(struct folio *folio)
if (folio_test_dropbehind(folio))
return;
if (lru_gen_enabled()) {
- lru_gen_inc_refs(folio);
+ if (!folio_test_unevictable(folio))
+ folio_inc_lru_refs(folio, 0);
return;
}
@@ -474,21 +414,6 @@ void folio_add_lru(struct folio *folio)
folio_test_unevictable(folio), folio);
VM_BUG_ON_FOLIO(folio_test_lru(folio), folio);
- /*
- * For refaulted workingset folios, set PG_active so they
- * can be added to active generations.
- * For prefaulted file folios, folio_mark_accessed() sets
- * PG_referenced so lru_gen_folio_seq() places them into
- * the second oldest generation.
- */
- if (lru_gen_enabled() && !folio_test_unevictable(folio) &&
- lru_gen_in_fault() && !(current->flags & PF_MEMALLOC)) {
- if (folio_test_workingset(folio))
- folio_set_active(folio);
- else if (!folio_test_referenced(folio))
- folio_mark_accessed(folio);
- }
-
folio_batch_add_and_move(folio, lru_add);
}
EXPORT_SYMBOL(folio_add_lru);
@@ -600,7 +525,7 @@ static void lru_lazyfree(struct lruvec *lruvec, struct folio *folio)
lruvec_del_folio(lruvec, folio);
folio_clear_active(folio);
if (lru_gen_enabled())
- lru_gen_clear_refs(folio);
+ __folio_set_lru_refs(folio, 0);
else
folio_clear_referenced(folio);
/*
@@ -673,7 +598,7 @@ void deactivate_file_folio(struct folio *folio)
if (folio_test_unevictable(folio) || !folio_test_lru(folio))
return;
- if (lru_gen_enabled() && lru_gen_clear_refs(folio))
+ if (lru_gen_enabled() && !folio_reset_lru_refs(folio))
return;
folio_batch_add_and_move(folio, lru_deactivate_file);
@@ -692,7 +617,7 @@ void folio_deactivate(struct folio *folio)
if (folio_test_unevictable(folio) || !folio_test_lru(folio))
return;
- if (lru_gen_enabled() ? lru_gen_clear_refs(folio) : !folio_test_active(folio))
+ if (lru_gen_enabled() ? !folio_reset_lru_refs(folio) : !folio_test_active(folio))
return;
folio_batch_add_and_move(folio, lru_deactivate);
diff --git a/mm/gup.c b/mm/gup.c
index f166acf794e30..b3f563d411923 100644
--- a/mm/gup.c
+++ b/mm/gup.c
@@ -2896,7 +2896,7 @@ static unsigned long gup_fast_pte_range(pmd_t pmd, pmd_t *pmdp,
gup_put_folio(folio, 1, flags);
goto pte_unmap;
}
- folio_set_referenced(folio);
+ folio_inc_lru_refs_fast(folio);
pages[nr_pages] = page;
nr_pages++;
} while (ptep++, addr += PAGE_SIZE, addr != end);
@@ -2965,7 +2965,7 @@ static unsigned long gup_fast_pmd_leaf(pmd_t orig, pmd_t *pmdp,
for (i = 0; i < nr_pages; i++)
*(pages++) = page++;
- folio_set_referenced(folio);
+ folio_inc_lru_refs_fast(folio);
return nr_pages;
}
@@ -3007,7 +3007,7 @@ static unsigned long gup_fast_pud_leaf(pud_t orig, pud_t *pudp,
for (i = 0; i < nr_pages; i++)
*(pages++) = page++;
- folio_set_referenced(folio);
+ folio_inc_lru_refs_fast(folio);
return nr_pages;
}
diff --git a/mm/huge_memory.c b/mm/huge_memory.c
index bd44d58e86845..1e2b6448c0102 100644
--- a/mm/huge_memory.c
+++ b/mm/huge_memory.c
@@ -3093,8 +3093,8 @@ static void __split_huge_pud_locked(struct vm_area_struct *vma, pud_t *pud,
if (!folio_test_dirty(folio) && pud_dirty(old_pud))
folio_mark_dirty(folio);
- if (!folio_test_referenced(folio) && pud_young(old_pud))
- folio_set_referenced(folio);
+ if (pud_young(old_pud) && vma_has_recency(vma))
+ folio_mark_accessed(folio);
folio_remove_rmap_pud(folio, page, vma);
add_mm_counter(vma->vm_mm, mm_counter_file(folio),
-HPAGE_PUD_NR);
@@ -3216,8 +3216,8 @@ static void __split_huge_pmd_locked(struct vm_area_struct *vma, pmd_t *pmd,
folio = page_folio(page);
if (!folio_test_dirty(folio) && pmd_dirty(old_pmd))
folio_mark_dirty(folio);
- if (!folio_test_referenced(folio) && pmd_young(old_pmd))
- folio_set_referenced(folio);
+ if (pmd_young(old_pmd) && vma_has_recency(vma))
+ folio_mark_accessed(folio);
folio_remove_rmap_pmd(folio, page, vma);
add_mm_counter(mm, mm_counter_file(folio), -HPAGE_PMD_NR);
folio_put(folio);
diff --git a/mm/khugepaged.c b/mm/khugepaged.c
index ef5cd6fda34f7..5332b4c51fec7 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;
/* ... or the PTE mapping was recently used */
return pte_young(pteval) || mmu_notifier_test_young(vma->vm_mm, addr);
diff --git a/mm/madvise.c b/mm/madvise.c
index 75b409e97dbf2..184611fdd6f9d 100644
--- a/mm/madvise.c
+++ b/mm/madvise.c
@@ -360,6 +360,45 @@ static inline int madvise_folio_pte_batch(unsigned long addr, unsigned long end,
FPB_MERGE_YOUNG_DIRTY);
}
+static void madvise_cold_prep_folio(struct folio *folio)
+{
+ /*
+ * VM couldn't reclaim the folio unless we clear PG_young.
+ * As a side effect, it makes confuse idle-page tracking
+ * because they will miss recent referenced history.
+ */
+ folio_test_clear_young(folio);
+
+ /*
+ * For the active/inactive LRU, a folio demoted out of the active
+ * list should have PG_workingset so its refault is still accounted
+ * as a workingset refault, and the referenced bit always needs to
+ * be cleared even for inactive folios. Nothing to do for MGLRU as
+ * folio_deactivate() clears the refs count through
+ * folio_reset_lru_refs().
+ */
+ if (!lru_gen_enabled()) {
+ folio_clear_referenced(folio);
+ if (folio_test_active(folio))
+ folio_set_workingset(folio);
+ }
+}
+
+static void madvise_pageout_prep_folio(struct folio *folio)
+{
+ int refs = folio_lru_refs(folio);
+
+ /*
+ * For MGLRU, drop the count below LRU_REFS_WORKINGSET and cap
+ * the rest at it: MADV is
+ * suggesting the folio is actually cold, so the eviction shadow
+ * should not record its hotness in the high tier counters, but
+ * the workingset classification is preserved for PSI.
+ */
+ if (lru_gen_enabled())
+ __folio_set_lru_refs(folio, refs >= LRU_REFS_WORKINGSET ? LRU_REFS_WORKINGSET : 0);
+}
+
static int madvise_cold_or_pageout_pte_range(pmd_t *pmd,
unsigned long addr, unsigned long end,
struct mm_walk *walk)
@@ -434,12 +473,10 @@ static int madvise_cold_or_pageout_pte_range(pmd_t *pmd,
tlb_remove_pmd_tlb_entry(tlb, pmd, addr);
}
- folio_clear_referenced(folio);
- folio_test_clear_young(folio);
- if (folio_test_active(folio))
- folio_set_workingset(folio);
+ madvise_cold_prep_folio(folio);
if (pageout) {
if (folio_isolate_lru(folio)) {
+ madvise_pageout_prep_folio(folio);
if (folio_test_unevictable(folio))
folio_putback_lru(folio);
else
@@ -544,18 +581,10 @@ static int madvise_cold_or_pageout_pte_range(pmd_t *pmd,
tlb_remove_tlb_entries(tlb, pte, nr, addr);
}
- /*
- * We are deactivating a folio for accelerating reclaiming.
- * VM couldn't reclaim the folio unless we clear PG_young.
- * As a side effect, it makes confuse idle-page tracking
- * because they will miss recent referenced history.
- */
- folio_clear_referenced(folio);
- folio_test_clear_young(folio);
- if (folio_test_active(folio))
- folio_set_workingset(folio);
+ madvise_cold_prep_folio(folio);
if (pageout) {
if (folio_isolate_lru(folio)) {
+ madvise_pageout_prep_folio(folio);
if (folio_test_unevictable(folio))
folio_putback_lru(folio);
else
diff --git a/mm/migrate.c b/mm/migrate.c
index 7bdcdb57652f8..5eefd8547f70e 100644
--- a/mm/migrate.c
+++ b/mm/migrate.c
@@ -786,8 +786,6 @@ void folio_migrate_flags(struct folio *newfolio, struct folio *folio)
folio_set_active(newfolio);
} else if (folio_test_clear_unevictable(folio))
folio_set_unevictable(newfolio);
- if (folio_test_workingset(folio))
- folio_set_workingset(newfolio);
if (folio_test_checked(folio))
folio_set_checked(newfolio);
/*
diff --git a/mm/page_io.c b/mm/page_io.c
index c6824fcd483e0..540ae8caa932d 100644
--- a/mm/page_io.c
+++ b/mm/page_io.c
@@ -16,6 +16,7 @@
#include <linux/gfp.h>
#include <linux/pagemap.h>
#include <linux/swap.h>
+#include <linux/mm_inline.h>
#include <linux/bio.h>
#include <linux/swapops.h>
#include <linux/writeback.h>
diff --git a/mm/vmscan.c b/mm/vmscan.c
index daa7e37c2236a..e604b0893847e 100644
--- a/mm/vmscan.c
+++ b/mm/vmscan.c
@@ -931,38 +931,257 @@ enum folio_references {
};
#ifdef CONFIG_LRU_GEN
+/******************************************************************************
+ * Referenced count feedback
+ ******************************************************************************/
+
/*
- * Only used on a mapped folio in the eviction (rmap walk) path, where promotion
- * needs to be done by taking the folio off the LRU list and then adding it back
- * with PG_active set. In contrast, the aging (page table walk) path uses
- * folio_update_gen().
+ * The folio_inc_lru_refs{_*} helpers below collect the referenced info
+ * (hotness) from other parts, including the page table walker, the rmap walk
+ * upon eviction, the rmap lookaround, and file descriptors
+ * (folio_mark_accessed).
+ *
+ * Page table accesses use refs from either source. The first reference
+ * advances tail-gen folios by one generation; a second promotes to the
+ * newest. Executable file folios promote on their first access to avoid
+ * I/O thrashing. Isolated rmap accesses use evict_folios() for putback.
+ *
+ * Promotion changes folio->flags; sort_folio() or inc_min_seq() moves the
+ * folio later. Young PTEs found during unmapping use folio_mark_accessed()
+ * and follow the file descriptor rules below.
+ *
+ * File descriptor accesses do not promote. They only defer eviction from
+ * the tail gens of their type, and only once the folio is a workingset
+ * folio (LRU_REFS_WORKINGSET), leaving the rest to PID protection. Page
+ * table accesses are treated more generously because the accessed bit is
+ * sticky (it under-counts repeated accesses) and because a page fault is
+ * more costly than file descriptor I/O.
+ *
+ * PID protection operates on tier > 0 folios. The one proactive promotion
+ * outside of it and the page table path is the overflow case where the
+ * referenced count exceeds LRU_REFS_MAX, which means the folio is hotter
+ * than everything else in its generation.
+ *
+ * Whenever a folio changes generation here its referenced count is capped at
+ * LRU_REFS_PROTECTED, so it starts at or below the protected tier regardless
+ * of its old-generation access history. PID protection (folio_inc_gen) caps
+ * at LRU_REFS_WORKINGSET independently.
+ *
+ * folio_inc_lru_refs() retries with LRU_REF_FORCE if the aging recycled the
+ * gen index it was about to store: newest gen and an extra reference.
*/
-static bool lru_gen_set_refs(struct folio *folio, const vma_flags_t *vma_flags)
-{
- /* see the comment on LRU_REFS_FLAGS */
- if (!folio_test_referenced(folio) && !folio_test_workingset(folio)) {
- /* Activate file-backed executable folios after first usage. */
- if (is_exec_file_folio(folio, vma_flags)) {
- folio_set_workingset(folio);
- folio_set_lru_refs(folio, 0);
- return true;
+
+/*
+ * Update the folio's lru refs indicator. The caller doesn't need to hold
+ * the folio lock, isolate the folio, or hold the lruvec lock. Used by both
+ * cache access (flags == 0) and page table access (LRU_REF_MAPPED,
+ * optionally with LRU_REF_EXEC).
+ */
+void folio_inc_lru_refs(struct folio *folio, unsigned int flags)
+{
+ int max_gen;
+ int file, old_refs, refs, old_gen, gen;
+ unsigned long new_flags, old_flags, max_seq;
+ long nr_pages = folio_nr_pages(folio);
+ struct lru_gen_folio *lrugen;
+ struct lruvec *lruvec = NULL;
+ bool aged = false;
+
+retry:
+ old_flags = READ_ONCE(*folio_flags(folio, 0));
+ do {
+ new_flags = old_flags;
+ old_gen = lru_get_gen_flags(old_flags);
+ old_refs = lru_get_refs_flags(old_flags);
+ refs = old_refs + 1;
+ gen = old_gen;
+ file = folio_flags_is_file_lru(&old_flags);
+ if (old_gen < 0)
+ goto out;
+ if (!lruvec) {
+ lruvec = folio_lruvec_live_get(folio);
+ lrugen = &lruvec->lrugen;
+ }
+ /*
+ * Read the flags before max_seq, paired with the store of a
+ * gen, which samples max_seq first.
+ */
+ smp_rmb();
+ max_seq = READ_ONCE(lrugen->max_seq);
+ max_gen = lru_gen_from_seq(max_seq);
+ if (old_gen == max_gen)
+ goto out;
+ if (flags & (LRU_REF_MAPPED | LRU_REF_EXEC | LRU_REF_FORCE)) {
+ if (refs > LRU_REFS_REFERENCED ||
+ flags & (LRU_REF_EXEC | LRU_REF_FORCE))
+ gen = max_gen;
+ /* First access only defers eviction of a tail gen */
+ else if (lru_gen_at_tail(lruvec, file, old_gen))
+ gen = (old_gen + 1) % MAX_NR_GENS;
+ refs = min(refs, LRU_REFS_PROTECTED);
+ } else if (refs > LRU_REFS_MAX) {
+ /* LRU refs counting overflow, bump the gen */
+ gen = (old_gen + 1) % MAX_NR_GENS;
+ refs = LRU_REFS_PROTECTED;
+ } else if (lru_gen_at_tail(lruvec, file, old_gen) && refs >= LRU_REFS_WORKINGSET) {
+ /* Defer eviction of just accessed workingset */
+ gen = (old_gen + 1) % MAX_NR_GENS;
+ refs = min(refs, LRU_REFS_PROTECTED);
}
+out:
+ refs = min(refs, LRU_REFS_MAX);
+ lru_set_refs_flags(&new_flags, refs);
+ if (gen != old_gen)
+ lru_set_gen_flags(&new_flags, gen);
+ if (new_flags == old_flags)
+ break;
+ } while (!try_cmpxchg(folio_flags(folio, 0), &old_flags, new_flags));
- folio_set_lru_refs(folio, 1);
- return false;
+ if (gen != old_gen) {
+ /*
+ * Gen-index reuse can fool cmpxchg(). Retry locklessly, accepting
+ * an extra reference to avoid folio_activate() latency.
+ */
+ if (unlikely(READ_ONCE(lrugen->max_seq) != max_seq)) {
+ flags = LRU_REF_FORCE;
+ aged = true;
+ }
+ lru_gen_update_size(lruvec, file, folio, old_gen, gen);
}
- /* Promote on second access */
- if (folio_lru_refs(folio) > 1) {
- folio_set_workingset(folio);
- folio_set_lru_refs(folio, 0);
- } else {
- folio_mark_accessed(folio);
+ if (lru_refs_is_active(old_refs) != lru_refs_is_active(refs) && gen >= 0) {
+ enum lru_list lru = file * LRU_INACTIVE_FILE;
+
+ __update_lru_size(lruvec, lru + lru_refs_is_active(old_refs),
+ folio_zonenum(folio), -nr_pages);
+ __update_lru_size(lruvec, lru + lru_refs_is_active(refs),
+ folio_zonenum(folio), nr_pages);
}
- return true;
+ if (aged) {
+ aged = false;
+ goto retry;
+ }
+ if (lruvec)
+ folio_lruvec_live_put(lruvec);
+}
+
+/*
+ * Reset the folio's lru refs indicator. The caller doesn't need to hold
+ * the folio lock, isolate the folio, or hold the lruvec lock.
+ */
+bool folio_reset_lru_refs(struct folio *folio)
+{
+ int type, gen, refs;
+ unsigned long seq, new_flags, old_flags;
+ long nr_pages = folio_nr_pages(folio);
+ struct lruvec *lruvec;
+ bool reset = false;
+
+ old_flags = READ_ONCE(*folio_flags(folio, 0));
+ do {
+ new_flags = old_flags;
+ refs = lru_get_refs_flags(old_flags);
+ type = folio_flags_is_file_lru(&old_flags);
+ gen = lru_get_gen_flags(old_flags);
+ if (!refs)
+ break;
+ lru_set_refs_flags(&new_flags, 0);
+ } while (!try_cmpxchg(folio_flags(folio, 0), &old_flags, new_flags));
+
+ if (gen >= 0) {
+ lruvec = folio_lruvec_live_get(folio);
+
+ /* If the folio is on list, caller might also want to demote it */
+ seq = READ_ONCE(lruvec->lrugen.min_seq[type]);
+ reset = (gen != lru_gen_from_seq(seq));
+ if (lru_refs_is_active(refs)) {
+ __update_lru_size(lruvec, type * LRU_FILE + LRU_ACTIVE,
+ folio_zonenum(folio), -nr_pages);
+ __update_lru_size(lruvec, type * LRU_FILE,
+ folio_zonenum(folio), nr_pages);
+ }
+
+ folio_lruvec_live_put(lruvec);
+ }
+
+ return reset;
+}
+
+/*
+ * Update the folio's lru refs indicator during a page table walk or the
+ * look-around. max_seq can be stale as neither holds the LRU lock.
+ *
+ * Returns the old generation, or -1 if the folio is off the LRU list.
+ * Old LRU refs, and updated gen and LRU refs info by the successful
+ * cmpxchg are all stored by returning arguments.
+ */
+static int folio_inc_lru_refs_walk(struct folio *folio, struct lruvec *lruvec,
+ const vma_flags_t *vma_flags, int *new_gen,
+ int *old_refs, int *new_refs, int *type)
+{
+ unsigned long new_flags, old_flags = READ_ONCE(*folio_flags(folio, 0));
+ int refs, old_gen, max_gen;
+ unsigned long max_seq;
+
+ do {
+ max_seq = READ_ONCE(lruvec->lrugen.max_seq);
+ max_gen = lru_gen_from_seq(max_seq);
+ old_gen = lru_get_gen_flags(old_flags);
+ refs = lru_get_refs_flags(old_flags) + 1;
+ *type = folio_flags_is_file_lru(&old_flags);
+ new_flags = old_flags;
+
+ if (old_gen >= 0 && old_gen != max_gen) {
+ *new_refs = min(refs, LRU_REFS_PROTECTED);
+ /* Promote second page table access or executable */
+ if (refs > LRU_REFS_REFERENCED || is_exec_file_folio(folio, vma_flags))
+ *new_gen = max_gen;
+ /* First access only defers eviction of a tail gen */
+ else if (lru_gen_at_tail(lruvec, *type, old_gen))
+ *new_gen = (old_gen + 1) % MAX_NR_GENS;
+ else
+ *new_gen = old_gen;
+ lru_set_gen_flags(&new_flags, *new_gen);
+ lru_set_refs_flags(&new_flags, *new_refs);
+ } else {
+ *new_gen = old_gen;
+ *new_refs = min(refs, LRU_REFS_MAX);
+ lru_set_refs_flags(&new_flags, *new_refs);
+ }
+ if (new_flags == old_flags)
+ break;
+ } while (!try_cmpxchg(folio_flags(folio, 0), &old_flags, new_flags));
+
+ *old_refs = lru_get_refs_flags(old_flags);
+
+ return old_gen;
+}
+
+/*
+ * Update the lru refs indicator of an isolated folio, only used on
+ * mapped folios upon the final eviction.
+ *
+ * Increments the refs count (capped at LRU_REFS_PROTECTED, evict_folios()
+ * then caps it at LRU_REFS_WORKINGSET). Returns true if the caller should
+ * activate the folio (second access or executable), false to put it back
+ * for a second chance.
+ */
+static bool folio_inc_lru_refs_isolated(struct folio *folio, const vma_flags_t *vma_flags)
+{
+ unsigned long new_flags, old_flags = READ_ONCE(*folio_flags(folio, 0));
+ int refs;
+
+ do {
+ new_flags = old_flags;
+ refs = lru_get_refs_flags(old_flags) + 1;
+ lru_set_refs_flags(&new_flags, min(refs, LRU_REFS_PROTECTED));
+ } while (!try_cmpxchg(folio_flags(folio, 0), &old_flags, new_flags));
+
+ /* Promote second page table access or executable */
+ return refs > LRU_REFS_REFERENCED || is_exec_file_folio(folio, vma_flags);
}
#else
-static bool lru_gen_set_refs(struct folio *folio, const vma_flags_t *vma_flags)
+static bool folio_inc_lru_refs_isolated(struct folio *folio, const vma_flags_t *vma_flags)
{
return false;
}
@@ -997,7 +1216,8 @@ static enum folio_references folio_check_references(struct folio *folio,
if (!referenced_ptes)
return FOLIOREF_RECLAIM;
- return lru_gen_set_refs(folio, &vma_flags) ? FOLIOREF_ACTIVATE : FOLIOREF_KEEP;
+ return folio_inc_lru_refs_isolated(folio, &vma_flags) ?
+ FOLIOREF_ACTIVATE : FOLIOREF_KEEP;
}
referenced_folio = folio_test_clear_referenced(folio);
@@ -3298,9 +3518,9 @@ static bool iterate_mm_list_nowalk(struct lruvec *lruvec, unsigned long seq)
* P term over the generations previously evicted, using the smoothing factor
* 1/2; the D term isn't supported.
*
- * The setpoint (SP) is always the first tier of one type; the process variable
- * (PV) is either any tier of the other type or any other tier of the same
- * type.
+ * To select a type, the setpoint (SP) is all tiers of one type and the process
+ * variable (PV) is all tiers of the other type. To select a tier to protect,
+ * the SP is the tiers below it and the PV is the tier itself.
*
* The error is the difference between the SP and the PV; the correction is to
* turn off protection when SP>PV or turn on protection when SP<PV.
@@ -3388,50 +3608,16 @@ static bool positive_ctrl_err(struct ctrl_pos *sp, struct ctrl_pos *pv)
* the aging
******************************************************************************/
-/* promote pages accessed through page tables */
-static int folio_update_gen(struct folio *folio, int new_gen, int *type,
- const vma_flags_t *vma_flags)
-{
- unsigned long new_flags, old_flags = READ_ONCE(*folio_flags(folio, 0));
- int old_gen;
-
- /*
- * See the comment on LRU_REFS_FLAGS, and activate file-backed
- * executable folios after first usage to avoid typical IO
- * thrashing from reclaiming.
- */
- if (!folio_test_referenced(folio) && !folio_test_workingset(folio) &&
- !is_exec_file_folio(folio, vma_flags)) {
- folio_set_lru_refs(folio, 1);
- return -1;
- }
-
- do {
- old_gen = lru_get_gen_flags(old_flags);
- new_flags = old_flags;
-
- /* lru_gen_del_folio() has isolated this page? */
- if (old_gen < 0)
- break;
-
- lru_set_gen_flags(&new_flags, new_gen);
- lru_set_refs_flags(&new_flags, 0);
- new_flags |= BIT(PG_workingset);
- } while (!try_cmpxchg(folio_flags(folio, 0), &old_flags, new_flags));
-
- *type = folio_flags_is_file_lru(&old_flags);
- return old_gen;
-}
-
-static int __folio_inc_gen(struct folio *folio, int old_gen, bool *increased)
+static int __folio_inc_gen(struct lruvec *lruvec, struct folio *folio,
+ int old_gen, bool *increased)
{
unsigned long new_flags, old_flags = READ_ONCE(*folio_flags(folio, 0));
- int new_gen;
+ int file, refs, old_refs, new_gen;
do {
new_gen = lru_get_gen_flags(old_flags);
- /* folio_update_gen() has promoted this page? */
+ /* folio_inc_lru_refs() has promoted this page? */
if (new_gen >= 0 && new_gen != old_gen) {
if (increased)
*increased = false;
@@ -3440,43 +3626,68 @@ static int __folio_inc_gen(struct folio *folio, int old_gen, bool *increased)
new_flags = old_flags;
new_gen = (old_gen + 1) % MAX_NR_GENS;
-
+ old_refs = lru_get_refs_flags(old_flags);
+ file = folio_flags_is_file_lru(&old_flags);
+ refs = min(old_refs, LRU_REFS_WORKINGSET);
lru_set_gen_flags(&new_flags, new_gen);
- lru_set_refs_flags(&new_flags, 0);
+ lru_set_refs_flags(&new_flags, refs);
} while (!try_cmpxchg(folio_flags(folio, 0), &old_flags, new_flags));
+ /* Refs capped below PROTECTED: demote if previously active. */
+ if (lru_refs_is_active(old_refs) != lru_refs_is_active(refs)) {
+ enum lru_list lru = file * LRU_INACTIVE_FILE;
+ int nr_pages = folio_nr_pages(folio);
+
+ __update_lru_size(lruvec, lru + lru_refs_is_active(old_refs),
+ folio_zonenum(folio), -nr_pages);
+ __update_lru_size(lruvec, lru + lru_refs_is_active(refs),
+ folio_zonenum(folio), nr_pages);
+ }
+
if (increased)
*increased = true;
return new_gen;
}
-/* protect pages accessed multiple times through file descriptors */
+/*
+ * Force bump a folio's generation. Used for PID protection or to skip a
+ * folio from a zone ineligible for the current reclaim.
+ */
static int folio_inc_gen(struct lruvec *lruvec, struct folio *folio)
{
+ bool gen_increased;
int type = folio_is_file_lru(folio);
struct lru_gen_folio *lrugen = &lruvec->lrugen;
int new_gen, old_gen = lru_gen_from_seq(lrugen->min_seq[type]);
- bool gen_increased;
- new_gen = __folio_inc_gen(folio, old_gen, &gen_increased);
+ new_gen = __folio_inc_gen(lruvec, folio, old_gen, &gen_increased);
if (gen_increased)
- lru_gen_update_size(lruvec, folio, old_gen, new_gen);
+ lru_gen_update_size(lruvec, type, folio, old_gen, new_gen);
+
return new_gen;
}
static void update_batch_size(struct lru_gen_mm_walk *walk, struct folio *folio,
- int old_gen, int new_gen, int type)
+ int old_gen, int new_gen, int old_refs, int new_refs, int type)
{
int zone = folio_zonenum(folio);
int delta = folio_nr_pages(folio);
- VM_WARN_ON_ONCE(old_gen >= MAX_NR_GENS);
- VM_WARN_ON_ONCE(new_gen >= MAX_NR_GENS);
-
- walk->batched++;
+ /* gen counter update */
+ if (old_gen != new_gen) {
+ VM_WARN_ON_ONCE(old_gen >= MAX_NR_GENS);
+ VM_WARN_ON_ONCE(new_gen >= MAX_NR_GENS);
+ walk->batched++;
+ walk->nr_pages[old_gen][type][zone] -= delta;
+ walk->nr_pages[new_gen][type][zone] += delta;
+ }
- walk->nr_pages[old_gen][type][zone] -= delta;
- walk->nr_pages[new_gen][type][zone] += delta;
+ /* active/inactive counter update */
+ if (lru_refs_is_active(old_refs) != lru_refs_is_active(new_refs)) {
+ walk->nr_activated[type][zone] +=
+ lru_refs_is_active(new_refs) ? delta : -delta;
+ walk->batched++;
+ }
}
static void reset_batch_size(struct lru_gen_mm_walk *walk)
@@ -3488,19 +3699,28 @@ static void reset_batch_size(struct lru_gen_mm_walk *walk)
walk->batched = 0;
for_each_gen_type_zone(gen, type, zone) {
- enum lru_list lru = type * LRU_INACTIVE_FILE;
int delta = walk->nr_pages[gen][type][zone];
if (!delta)
continue;
walk->nr_pages[gen][type][zone] = 0;
- WRITE_ONCE(lrugen->nr_pages[gen][type][zone],
- lrugen->nr_pages[gen][type][zone] + delta);
+ atomic_long_add(delta, &lrugen->nr_pages[gen][type][zone]);
+ }
+
+ /* apply batched active/inactive updates */
+ for (type = 0; type < ANON_AND_FILE; type++) {
+ for (zone = 0; zone < MAX_NR_ZONES; zone++) {
+ enum lru_list lru = type * LRU_INACTIVE_FILE;
+ int delta = walk->nr_activated[type][zone];
- if (lru_gen_is_active(lruvec, gen))
- lru += LRU_ACTIVE;
- __update_lru_size(lruvec, lru, zone, delta);
+ if (delta) {
+ __update_lru_size(lruvec, lru + LRU_ACTIVE,
+ zone, delta);
+ __update_lru_size(lruvec, lru, zone, -delta);
+ walk->nr_activated[type][zone] = 0;
+ }
+ }
}
lruvec_unlock_irq(lruvec);
@@ -3655,26 +3875,27 @@ static bool suitable_to_scan(int total, int young)
static void walk_update_folio(struct lru_gen_mm_walk *walk, struct vm_area_struct *vma,
struct lruvec *lruvec, struct folio *folio, bool dirty)
{
- int new_gen, old_gen, type;
+ int new_gen, old_gen, old_refs, new_refs, type;
+ unsigned int flags = LRU_REF_MAPPED;
if (!folio)
return;
- new_gen = lru_gen_from_seq(READ_ONCE(lruvec->lrugen.max_seq));
-
if (dirty && !folio_test_dirty(folio) &&
!(folio_test_anon(folio) && folio_test_swapbacked(folio) &&
!folio_test_swapcache(folio)))
folio_mark_dirty(folio);
if (walk) {
- old_gen = folio_update_gen(folio, new_gen, &type, &vma->flags);
- if (old_gen >= 0 && old_gen != new_gen)
- update_batch_size(walk, folio, old_gen, new_gen, type);
- } else if (lru_gen_set_refs(folio, &vma->flags)) {
- old_gen = folio_lru_gen(folio);
- if (old_gen >= 0 && old_gen != new_gen)
- folio_activate(folio);
+ old_gen = folio_inc_lru_refs_walk(folio, lruvec, &vma->flags,
+ &new_gen, &old_refs, &new_refs, &type);
+ if (old_gen >= 0)
+ update_batch_size(walk, folio, old_gen, new_gen,
+ old_refs, new_refs, type);
+ } else {
+ if (is_exec_file_folio(folio, &vma->flags))
+ flags |= LRU_REF_EXEC;
+ folio_inc_lru_refs(folio, flags);
}
}
@@ -4029,6 +4250,7 @@ static void clear_mm_walk(void)
VM_WARN_ON_ONCE(walk && memchr_inv(walk->nr_pages, 0, sizeof(walk->nr_pages)));
VM_WARN_ON_ONCE(walk && memchr_inv(walk->mm_stats, 0, sizeof(walk->mm_stats)));
+ VM_WARN_ON_ONCE(walk && memchr_inv(walk->nr_activated, 0, sizeof(walk->nr_activated)));
current->reclaim_state->mm_walk = NULL;
@@ -4067,8 +4289,6 @@ static bool inc_min_seq(struct lruvec *lruvec, int type, int swappiness)
goto done;
VM_WARN_ON_ONCE(get_nr_gens(lruvec, type) != MAX_NR_GENS);
- VM_WARN_ON_ONCE(lru_gen_is_active(lruvec, old_gen) !=
- lru_gen_is_active(lruvec, target_gen));
/* prevent cold/hot inversion if the type is evictable */
for (zone = 0; zone < MAX_NR_ZONES; zone++) {
struct list_head *target_list = &lrugen->folios[target_gen][type][zone];
@@ -4081,7 +4301,7 @@ static bool inc_min_seq(struct lruvec *lruvec, int type, int swappiness)
struct folio *folio = list_entry(pos, struct folio, lru);
long nr_pages = folio_nr_pages(folio);
int refs = folio_lru_refs(folio);
- bool workingset = folio_test_workingset(folio);
+ int tier = lru_tier_from_refs(refs);
bool gen_increased;
VM_WARN_ON_ONCE_FOLIO(folio_test_unevictable(folio), folio);
@@ -4091,7 +4311,7 @@ static bool inc_min_seq(struct lruvec *lruvec, int type, int swappiness)
prefetchw_next_lru_folio(folio, head);
pos = pos->next;
- new_gen = __folio_inc_gen(folio, old_gen, &gen_increased);
+ new_gen = __folio_inc_gen(lruvec, folio, old_gen, &gen_increased);
/*
* If gen_increased is false, this is a promotion. Put folios
* at the head of the promoted gen. Otherwise, put them at
@@ -4101,13 +4321,8 @@ static bool inc_min_seq(struct lruvec *lruvec, int type, int swappiness)
delta += nr_pages;
batch_end = &folio->lru;
- /* don't count the workingset being lazily promoted */
- if (refs + workingset != BIT(LRU_REFS_WIDTH) + 1) {
- int tier = lru_tier_from_refs(refs, workingset);
-
- WRITE_ONCE(lrugen->protected[hist][type][tier],
- lrugen->protected[hist][type][tier] + nr_pages);
- }
+ WRITE_ONCE(lrugen->protected[hist][type][tier],
+ lrugen->protected[hist][type][tier] + nr_pages);
} else {
flush_lru_batch(head, &batch_end, target_list);
list_move(&folio->lru, &lrugen->folios[new_gen][type][zone]);
@@ -4116,11 +4331,8 @@ static bool inc_min_seq(struct lruvec *lruvec, int type, int swappiness)
break;
}
flush_lru_batch(head, &batch_end, target_list);
-
- WRITE_ONCE(lrugen->nr_pages[old_gen][type][zone],
- lrugen->nr_pages[old_gen][type][zone] - delta);
- WRITE_ONCE(lrugen->nr_pages[target_gen][type][zone],
- lrugen->nr_pages[target_gen][type][zone] + delta);
+ atomic_long_sub(delta, &lrugen->nr_pages[old_gen][type][zone]);
+ atomic_long_add(delta, &lrugen->nr_pages[target_gen][type][zone]);
if (!remaining)
return false;
}
@@ -4186,8 +4398,7 @@ static void try_to_inc_min_seq(struct lruvec *lruvec, int swappiness)
static bool inc_max_seq(struct lruvec *lruvec, unsigned long seq, int swappiness)
{
bool success;
- int prev, next;
- int type, zone;
+ int type, next;
struct lru_gen_folio *lrugen = &lruvec->lrugen;
restart:
if (seq < READ_ONCE(lrugen->max_seq))
@@ -4213,32 +4424,10 @@ static bool inc_max_seq(struct lruvec *lruvec, unsigned long seq, int swappiness
goto restart;
}
- /*
- * Update the active/inactive LRU sizes for compatibility. Both sides of
- * the current max_seq need to be covered, since max_seq+1 can overlap
- * with min_seq[LRU_GEN_ANON] if swapping is constrained. And if they do
- * overlap, cold/hot inversion happens.
- */
- prev = lru_gen_from_seq(lrugen->max_seq - 1);
- next = lru_gen_from_seq(lrugen->max_seq + 1);
-
- for (type = 0; type < ANON_AND_FILE; type++) {
- for (zone = 0; zone < MAX_NR_ZONES; zone++) {
- enum lru_list lru = type * LRU_INACTIVE_FILE;
- long delta = lrugen->nr_pages[prev][type][zone] -
- lrugen->nr_pages[next][type][zone];
-
- if (!delta)
- continue;
-
- __update_lru_size(lruvec, lru, zone, delta);
- __update_lru_size(lruvec, lru + LRU_ACTIVE, zone, -delta);
- }
- }
-
for (type = 0; type < ANON_AND_FILE; type++)
reset_ctrl_pos(lruvec, type, false);
+ next = lru_gen_from_seq(lrugen->max_seq + 1);
WRITE_ONCE(lrugen->timestamps[next], jiffies);
/* make sure preceding modifications appear */
smp_store_release(&lrugen->max_seq, lrugen->max_seq + 1);
@@ -4344,7 +4533,8 @@ static unsigned long lruvec_evictable_size(struct lruvec *lruvec, int swappiness
for (seq = min_seq[type]; seq <= max_seq; seq++) {
gen = lru_gen_from_seq(seq);
for (zone = 0; zone < MAX_NR_ZONES; zone++)
- total += max(READ_ONCE(lrugen->nr_pages[gen][type][zone]), 0L);
+ total += max(atomic_long_read(&lrugen->nr_pages[gen][type][zone]),
+ 0L);
}
}
@@ -4756,30 +4946,22 @@ static void __lru_gen_reparent_memcg(struct lruvec *child_lruvec, struct lruvec
int zone, int type)
{
struct lru_gen_folio *child_lrugen, *parent_lrugen;
- enum lru_list lru = type * LRU_INACTIVE_FILE;
int i;
child_lrugen = &child_lruvec->lrugen;
parent_lrugen = &parent_lruvec->lrugen;
- for (i = 0; i < get_nr_gens(child_lruvec, type); i++) {
+ /* Lockless promotion may leave folios outside the child's window */
+ for (i = 0; i < MAX_NR_GENS; i++) {
int gen = lru_gen_from_seq(child_lrugen->max_seq - i);
- long nr_pages = child_lrugen->nr_pages[gen][type][zone];
- int child_lru_active = lru_gen_is_active(child_lruvec, gen) ? LRU_ACTIVE : 0;
- int parent_lru_active = lru_gen_is_active(parent_lruvec, gen) ? LRU_ACTIVE : 0;
+ long nr_pages = atomic_long_read(&child_lrugen->nr_pages[gen][type][zone]);
/* Assuming that child pages are colder than parent pages */
list_splice_tail_init(&child_lrugen->folios[gen][type][zone],
&parent_lrugen->folios[gen][type][zone]);
- WRITE_ONCE(child_lrugen->nr_pages[gen][type][zone], 0);
- WRITE_ONCE(parent_lrugen->nr_pages[gen][type][zone],
- parent_lrugen->nr_pages[gen][type][zone] + nr_pages);
-
- if (lru_gen_is_active(child_lruvec, gen) != lru_gen_is_active(parent_lruvec, gen)) {
- __update_lru_size(child_lruvec, lru + child_lru_active, zone, -nr_pages);
- __update_lru_size(parent_lruvec, lru + parent_lru_active, zone, nr_pages);
- }
+ atomic_long_set(&child_lrugen->nr_pages[gen][type][zone], 0);
+ atomic_long_add(nr_pages, &parent_lrugen->nr_pages[gen][type][zone]);
}
}
@@ -4825,8 +5007,7 @@ static bool sort_folio(struct lruvec *lruvec, struct folio *folio, struct scan_c
int zone = folio_zonenum(folio);
int delta = folio_nr_pages(folio);
int refs = folio_lru_refs(folio);
- bool workingset = folio_test_workingset(folio);
- int tier = lru_tier_from_refs(refs, workingset);
+ int tier = lru_tier_from_refs(refs);
struct lru_gen_folio *lrugen = &lruvec->lrugen;
VM_WARN_ON_ONCE_FOLIO(gen >= MAX_NR_GENS, folio);
@@ -4842,17 +5023,15 @@ static bool sort_folio(struct lruvec *lruvec, struct folio *folio, struct scan_c
}
/* protected */
- if (tier > tier_idx || refs + workingset == BIT(LRU_REFS_WIDTH) + 1) {
+ if (tier > tier_idx) {
+ int hist = lru_hist_from_seq(lrugen->min_seq[type]);
+
gen = folio_inc_gen(lruvec, folio);
list_move(&folio->lru, &lrugen->folios[gen][type][zone]);
- /* don't count the workingset being lazily promoted */
- if (refs + workingset != BIT(LRU_REFS_WIDTH) + 1) {
- int hist = lru_hist_from_seq(lrugen->min_seq[type]);
+ WRITE_ONCE(lrugen->protected[hist][type][tier],
+ lrugen->protected[hist][type][tier] + delta);
- WRITE_ONCE(lrugen->protected[hist][type][tier],
- lrugen->protected[hist][type][tier] + delta);
- }
return true;
}
@@ -4880,10 +5059,6 @@ static bool isolate_folio(struct lruvec *lruvec, struct folio *folio, struct sca
return false;
}
- /* see the comment on LRU_REFS_FLAGS */
- if (!folio_test_referenced(folio))
- folio_set_lru_refs(folio, 0);
-
success = lru_gen_del_folio(lruvec, folio, true);
VM_WARN_ON_ONCE_FOLIO(!success, folio);
@@ -4971,13 +5146,14 @@ static int get_tier_idx(struct lruvec *lruvec, int type)
struct ctrl_pos sp, pv;
/*
- * To leave a margin for fluctuations, use a larger gain factor (2:3).
- * This value is chosen because any other tier would have at least twice
- * as many refaults as the first tier.
+ * To leave a margin for fluctuations, use a larger gain factor (1:2).
+ * Stop at the first tier whose refault rate is clearly worse than
+ * that of the cumulative mass of the tiers below it; the PID
+ * protects the tiers above it.
*/
- read_ctrl_pos(lruvec, type, LRU_TIER_MIN, LRU_TIER_MIN, 2, &sp);
for (tier = LRU_TIER_MIN + 1; tier <= LRU_TIER_MAX; tier++) {
- read_ctrl_pos(lruvec, type, tier, tier, 3, &pv);
+ read_ctrl_pos(lruvec, type, LRU_TIER_MIN, tier - 1, 1, &sp);
+ read_ctrl_pos(lruvec, type, tier, tier, 2, &pv);
if (!positive_ctrl_err(&sp, &pv))
break;
}
@@ -5119,12 +5295,13 @@ static int evict_folios(unsigned long nr_to_scan, struct lruvec *lruvec,
}
/*
- * See the comments on LRU_REFS_FLAGS.
- *
* The rejected folios are never added to the oldest generation,
* so this effectively promotes them by at least one generation.
+ * PG_active is the only placement hint here, so a folio kept on
+ * its first page table access lands in the second newest one.
+ * See "Referenced count feedback" above.
*/
- folio_set_lru_refs(folio, 0);
+ __folio_set_lru_refs(folio, min(folio_lru_refs(folio), LRU_REFS_WORKINGSET));
if (lru_gen_folio_seq(lruvec, folio, false) == min_seq[type])
folio_set_active(folio);
}
@@ -5859,7 +6036,8 @@ static int lru_gen_seq_show(struct seq_file *m, void *v)
char mark = full && seq < min_seq[type] ? 'x' : ' ';
for (zone = 0; zone < MAX_NR_ZONES; zone++)
- size += max(READ_ONCE(lrugen->nr_pages[gen][type][zone]), 0L);
+ size += max(atomic_long_read(&lrugen->nr_pages[gen][type][zone]),
+ 0L);
seq_printf(m, " %10lu%c", size, mark);
}
diff --git a/mm/workingset.c b/mm/workingset.c
index 1504f91cdca5f..7681af535ca7e 100644
--- a/mm/workingset.c
+++ b/mm/workingset.c
@@ -188,6 +188,12 @@
#define EVICTION_SHIFT_ANON (EVICTION_SHIFT + SWAP_COUNT_SHIFT)
#define EVICTION_MASK (~0UL >> EVICTION_SHIFT)
#define EVICTION_MASK_ANON (~0UL >> EVICTION_SHIFT_ANON)
+/*
+ * LRU refs uses LRU_REFS_WIDTH + 2 bits, the 2 bits being PG_workingset
+ * and PG_referenced. The lowest bit is recorded in the workingset field
+ * of the shadow entry (to reuse pack_shadow()).
+ */
+#define LRU_REFS_BITS ((LRU_REFS_WIDTH + 2) - 1)
/*
* Eviction timestamps need to be able to cover the full range of
@@ -242,13 +248,12 @@ static void *lru_gen_eviction(struct folio *folio)
int type = folio_is_file_lru(folio);
int delta = folio_nr_pages(folio);
int refs = folio_lru_refs(folio);
- bool workingset = folio_test_workingset(folio);
- int tier = lru_tier_from_refs(refs, workingset);
+ int tier = lru_tier_from_refs(refs);
struct mem_cgroup *memcg;
struct pglist_data *pgdat = folio_pgdat(folio);
unsigned short memcg_id;
- BUILD_BUG_ON(LRU_GEN_WIDTH + LRU_REFS_WIDTH >
+ BUILD_BUG_ON(LRU_GEN_WIDTH + LRU_REFS_BITS >
BITS_PER_LONG - max(EVICTION_SHIFT, EVICTION_SHIFT_ANON));
rcu_read_lock();
@@ -256,14 +261,14 @@ static void *lru_gen_eviction(struct folio *folio)
lruvec = mem_cgroup_lruvec(memcg, pgdat);
lrugen = &lruvec->lrugen;
min_seq = READ_ONCE(lrugen->min_seq[type]);
- token = (min_seq << LRU_REFS_WIDTH) | max(refs - 1, 0);
+ token = (min_seq << LRU_REFS_BITS) | refs >> 1;
hist = lru_hist_from_seq(min_seq);
atomic_long_add(delta, &lrugen->evicted[hist][type][tier]);
memcg_id = mem_cgroup_private_id(memcg);
rcu_read_unlock();
- return pack_shadow(memcg_id, pgdat, token, workingset, type);
+ return pack_shadow(memcg_id, pgdat, token, refs & 1, type);
}
/*
@@ -284,9 +289,9 @@ static bool lru_gen_test_recent(void *shadow, struct lruvec **lruvec,
*lruvec = mem_cgroup_lruvec(memcg, pgdat);
max_seq = READ_ONCE((*lruvec)->lrugen.max_seq);
- max_seq &= (file ? EVICTION_MASK : EVICTION_MASK_ANON) >> LRU_REFS_WIDTH;
+ max_seq &= (file ? EVICTION_MASK : EVICTION_MASK_ANON) >> LRU_REFS_BITS;
- return abs_diff(max_seq, *token >> LRU_REFS_WIDTH) < MAX_NR_GENS;
+ return abs_diff(max_seq, *token >> LRU_REFS_BITS) < MAX_NR_GENS;
}
static void lru_gen_refault(struct folio *folio, void *shadow)
@@ -314,22 +319,26 @@ static void lru_gen_refault(struct folio *folio, void *shadow)
lrugen = &lruvec->lrugen;
hist = lru_hist_from_seq(READ_ONCE(lrugen->min_seq[type]));
- refs = (token & (BIT(LRU_REFS_WIDTH) - 1)) + 1;
- tier = lru_tier_from_refs(refs, workingset);
+ refs = ((token & (BIT(LRU_REFS_BITS) - 1)) << 1) + workingset;
+ tier = lru_tier_from_refs(refs);
atomic_long_add(delta, &lrugen->refaulted[hist][type][tier]);
- if (workingset) {
- /*
- * see folio_add_lru(), where folio_set_active() is
- * called for workingset folios
- */
- if (lru_gen_in_fault())
+ /*
+ * Activate a fault-driven refault folio, which would have been
+ * promoted had it stayed in memory.
+ */
+ if (refs >= LRU_REFS_REFERENCED) {
+ if (lru_gen_in_fault()) {
+ folio_set_active(folio);
mod_lruvec_state(lruvec, WORKINGSET_ACTIVATE_BASE + type, delta);
- folio_set_workingset(folio);
+ }
+ /* Refault is also promotion, cap the refs like folio_inc_lru_refs */
+ __folio_set_lru_refs(folio, min(refs, LRU_REFS_PROTECTED));
+ }
+
+ if (refs >= LRU_REFS_WORKINGSET)
mod_lruvec_state(lruvec, WORKINGSET_RESTORE_BASE + type, delta);
- } else
- set_mask_bits(&folio->flags.f, LRU_REFS_MASK, (refs - 1UL) << LRU_REFS_PGOFF);
unlock:
rcu_read_unlock();
}
|
| 25/3 | 2026/10/15 23:43 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 34306 output: 22 thoughts: 580 Thoughts: **KMSAN Fuzzing Session Assessment** Okay, let's dive into this kernel patch and figure out if we need to throw a KMSAN fuzzing session at it. The goal is to determine if `NeedsKMSAN` should be set to true or false. First, let's take a good, hard look at the patch itself. I see a broad touch across the memory management subsystem. There's stuff in documentation, btrfs, erofs, proc, include files like `memcontrol.h`, `mm_inline.h`, `mmzone.h`, and `page-flags.h`, and a good chunk of core MM code: `bounds.c`, `damon/paddr.c`, `filemap.c`, `folio.c`, `gup.c`, `huge_memory.c`, `khugepaged.c`, `madvise.c`, `migrate.c`, `page_io.c`, `vmscan.c`, and `workingset.c`. That's quite a wide scope, making me immediately suspicious. After a quick skim, it looks like this patch is all about refactoring and optimizing MGLRU, which is Multi-Gen LRU. It seems like the patch series is doing some clever things to track references. Instead of just using `PG_referenced` and some extra bits, MGLRU is now encoding references into `PG_referenced`, `PG_workingset`, and the `LRU_REFS_WIDTH` bits within `folio->flags`. Interesting. Looks like there are new helper functions like `folio_inc_lru_refs()`, `folio_reset_lru_refs()`, `folio_check_lru_refs()`, etc. It also includes some changes to `lru_gen_mm_walk`. Hold on. Let me zoom in on `struct lru_gen_mm_walk`. I see that there is this `nr_activated` member, that looks like it will be used for batch activation. Now I want to understand how and where `lru_gen_mm_walk` is initialized. I need to understand what this structure is and how it’s being used. I need to know where it's allocated, initialized, and how that `nr_activated` field is being used. Let's see if `grepper` or `codesearch` can give us a clearer picture of how this structure is managed and whether we could get some data corruption in edge cases! |
| 26/3 | 2026/10/15 23:43 | tool | grepper |
0mArgs:{
"Expression": "lru_gen_mm_walk"
}Results: {
"Output": "include/linux/mmzone.h=659=struct lru_gen_mm_state {\n--\ninclude/linux/mmzone.h-671-\ninclude/linux/mmzone.h:672:struct lru_gen_mm_walk {\ninclude/linux/mmzone.h-673-\t/* the lruvec under reclaim */\n--\ninclude/linux/mmzone.h=1524=typedef struct pglist_data {\n--\ninclude/linux/mmzone.h-1643-\t/* kswap mm walk data */\ninclude/linux/mmzone.h:1644:\tstruct lru_gen_mm_walk mm_walk;\ninclude/linux/mmzone.h-1645-\t/* lru_gen_folio list */\n--\ninclude/linux/swap.h=140=struct reclaim_state {\n--\ninclude/linux/swap.h-144-\t/* per-thread mm walk data */\ninclude/linux/swap.h:145:\tstruct lru_gen_mm_walk *mm_walk;\ninclude/linux/swap.h-146-#endif\n--\nmm/vmscan.c=3258=static struct lru_gen_mm_state *get_mm_state(struct lruvec *lruvec)\n--\nmm/vmscan.c-3262-\nmm/vmscan.c:3263:static struct mm_struct *get_next_mm(struct lru_gen_mm_walk *walk)\nmm/vmscan.c-3264-{\n--\nmm/vmscan.c=3385=static struct lru_gen_mm_state *get_mm_state(struct lruvec *lruvec)\n--\nmm/vmscan.c-3389-\nmm/vmscan.c:3390:static struct mm_struct *get_next_mm(struct lru_gen_mm_walk *walk)\nmm/vmscan.c-3391-{\n--\nmm/vmscan.c-3396-\nmm/vmscan.c:3397:static void reset_mm_stats(struct lru_gen_mm_walk *walk, bool last)\nmm/vmscan.c-3398-{\n--\nmm/vmscan.c-3421-\nmm/vmscan.c:3422:static bool iterate_mm_list(struct lru_gen_mm_walk *walk, struct mm_struct **iter)\nmm/vmscan.c-3423-{\n--\nmm/vmscan.c=3656=static int folio_inc_gen(struct lruvec *lruvec, struct folio *folio)\n--\nmm/vmscan.c-3669-\nmm/vmscan.c:3670:static void update_batch_size(struct lru_gen_mm_walk *walk, struct folio *folio,\nmm/vmscan.c-3671-\t\t\t int old_gen, int new_gen, int old_refs, int new_refs, int type)\n--\nmm/vmscan.c-3692-\nmm/vmscan.c:3693:static void reset_batch_size(struct lru_gen_mm_walk *walk)\nmm/vmscan.c-3694-{\n--\nmm/vmscan.c=3729=static int should_skip_vma(unsigned long start, unsigned long end, struct mm_walk *args)\n--\nmm/vmscan.c-3732-\tstruct vm_area_struct *vma = args-\u003evma;\nmm/vmscan.c:3733:\tstruct lru_gen_mm_walk *walk = args-\u003eprivate;\nmm/vmscan.c-3734-\n--\nmm/vmscan.c=3867=static bool suitable_to_scan(int total, int young)\n--\nmm/vmscan.c-3874-\nmm/vmscan.c:3875:static void walk_update_folio(struct lru_gen_mm_walk *walk, struct vm_area_struct *vma,\nmm/vmscan.c-3876-\t\tstruct lruvec *lruvec, struct folio *folio, bool dirty)\n--\nmm/vmscan.c=3902=static bool walk_pte_range(pmd_t *pmd, unsigned long start, unsigned long end,\n--\nmm/vmscan.c-3912-\tstruct folio *last = NULL;\nmm/vmscan.c:3913:\tstruct lru_gen_mm_walk *walk = args-\u003eprivate;\nmm/vmscan.c-3914-\tstruct mem_cgroup *memcg = lruvec_memcg(walk-\u003elruvec);\n--\nmm/vmscan.c=3991=static void walk_pmd_range_locked(pud_t *pud, unsigned long addr, struct vm_area_struct *vma,\n--\nmm/vmscan.c-3998-\tstruct folio *last = NULL;\nmm/vmscan.c:3999:\tstruct lru_gen_mm_walk *walk = args-\u003eprivate;\nmm/vmscan.c-4000-\tstruct mem_cgroup *memcg = lruvec_memcg(walk-\u003elruvec);\n--\nmm/vmscan.c=4077=static void walk_pmd_range(pud_t *pud, unsigned long start, unsigned long end,\n--\nmm/vmscan.c-4086-\tunsigned long first = -1;\nmm/vmscan.c:4087:\tstruct lru_gen_mm_walk *walk = args-\u003eprivate;\nmm/vmscan.c-4088-\tstruct lru_gen_mm_state *mm_state = get_mm_state(walk-\u003elruvec);\n--\nmm/vmscan.c=4150=static int walk_pud_range(p4d_t *p4d, unsigned long start, unsigned long end,\n--\nmm/vmscan.c-4156-\tunsigned long next;\nmm/vmscan.c:4157:\tstruct lru_gen_mm_walk *walk = args-\u003eprivate;\nmm/vmscan.c-4158-\n--\nmm/vmscan.c-4191-\nmm/vmscan.c:4192:static void walk_mm(struct mm_struct *mm, struct lru_gen_mm_walk *walk)\nmm/vmscan.c-4193-{\n--\nmm/vmscan.c-4226-\nmm/vmscan.c:4227:static struct lru_gen_mm_walk *set_mm_walk(struct pglist_data *pgdat, bool force_alloc)\nmm/vmscan.c-4228-{\nmm/vmscan.c:4229:\tstruct lru_gen_mm_walk *walk = current-\u003ereclaim_state-\u003emm_walk;\nmm/vmscan.c-4230-\n--\nmm/vmscan.c=4247=static void clear_mm_walk(void)\nmm/vmscan.c-4248-{\nmm/vmscan.c:4249:\tstruct lru_gen_mm_walk *walk = current-\u003ereclaim_state-\u003emm_walk;\nmm/vmscan.c-4250-\n--\nmm/vmscan.c=4440=static bool try_to_inc_max_seq(struct lruvec *lruvec, unsigned long seq,\n--\nmm/vmscan.c-4443-\tbool success;\nmm/vmscan.c:4444:\tstruct lru_gen_mm_walk *walk;\nmm/vmscan.c-4445-\tstruct mm_struct *mm = NULL;\n--\nmm/vmscan.c=4627=bool lru_gen_look_around(struct page_vma_mapped_walk *pvmw, unsigned int nr)\n--\nmm/vmscan.c-4632-\tunsigned long end;\nmm/vmscan.c:4633:\tstruct lru_gen_mm_walk *walk;\nmm/vmscan.c-4634-\tstruct folio *last = NULL;\n--\nmm/vmscan.c=5235=static int evict_folios(unsigned long nr_to_scan, struct lruvec *lruvec,\n--\nmm/vmscan.c-5243-\tstruct reclaim_stat stat;\nmm/vmscan.c:5244:\tstruct lru_gen_mm_walk *walk;\nmm/vmscan.c-5245-\tint scanned, reclaimed;\n"
} |
| 27/3 | 2026/10/15 23:43 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 37599 output: 35 thoughts: 28 Thoughts: Okay, let's dive into this. Here's how I'd approach summarizing the prompt, keeping in mind my own expert-level understanding of memory management: **Inspecting `set_mm_walk` and `clear_mm_walk` in `mm/vmscan.c`** Alright, so the task at hand is to understand the roles of `set_mm_walk` and `clear_mm_walk`, specifically within the context of `mm/vmscan.c`. Immediately, I'm thinking about how these functions fit into the larger VM scanning framework. My initial guess is that these functions likely deal with setting up and tearing down, or managing, the virtual memory walk structures used during the scan. Knowing that `vmscan.c` is the heart of the kernel's memory reclaim process, I'm already anticipating that these functions will be used to initialize and clean up data structures related to tracking and potentially modifying page tables or PTEs (Page Table Entries) during the scan. It's likely involved in either a direct memory reclaim process, possibly for swapping or freeing pages, or some similar process. I'll need to look at how they are called and what arguments are passed to ascertain their exact functionality. The naming convention is pretty clear, but the implementation will reveal a lot more. I'm especially interested in looking at the data structures associated with the `mm_walk` struct or similar constructs within `vmscan.c`. It is likely going to involve some form of synchronization, too, so I'll also be looking at those constructs. |
| 28/3 | 2026/10/15 23:43 | tool | read-file |
0mArgs:{
"File": "mm/vmscan.c",
"FirstLine": 4225,
"LineCount": 40
}Results: {
"Contents": "4225:\t}\n4226:\t\n4227:\tstatic struct lru_gen_mm_walk *set_mm_walk(struct pglist_data *pgdat, bool force_alloc)\n4228:\t{\n4229:\t\tstruct lru_gen_mm_walk *walk = current-\u003ereclaim_state-\u003emm_walk;\n4230:\t\n4231:\t\tif (pgdat \u0026\u0026 current_is_kswapd()) {\n4232:\t\t\tVM_WARN_ON_ONCE(walk);\n4233:\t\n4234:\t\t\twalk = \u0026pgdat-\u003emm_walk;\n4235:\t\t} else if (!walk \u0026\u0026 force_alloc) {\n4236:\t\t\tVM_WARN_ON_ONCE(current_is_kswapd());\n4237:\t\n4238:\t\t\twalk = kzalloc_obj(*walk,\n4239:\t\t\t\t\t __GFP_HIGH | __GFP_NOMEMALLOC | __GFP_NOWARN);\n4240:\t\t}\n4241:\t\n4242:\t\tcurrent-\u003ereclaim_state-\u003emm_walk = walk;\n4243:\t\n4244:\t\treturn walk;\n4245:\t}\n4246:\t\n4247:\tstatic void clear_mm_walk(void)\n4248:\t{\n4249:\t\tstruct lru_gen_mm_walk *walk = current-\u003ereclaim_state-\u003emm_walk;\n4250:\t\n4251:\t\tVM_WARN_ON_ONCE(walk \u0026\u0026 memchr_inv(walk-\u003enr_pages, 0, sizeof(walk-\u003enr_pages)));\n4252:\t\tVM_WARN_ON_ONCE(walk \u0026\u0026 memchr_inv(walk-\u003emm_stats, 0, sizeof(walk-\u003emm_stats)));\n4253:\t\tVM_WARN_ON_ONCE(walk \u0026\u0026 memchr_inv(walk-\u003enr_activated, 0, sizeof(walk-\u003enr_activated)));\n4254:\t\n4255:\t\tcurrent-\u003ereclaim_state-\u003emm_walk = NULL;\n4256:\t\n4257:\t\tif (!current_is_kswapd())\n4258:\t\t\tkfree(walk);\n4259:\t}\n4260:\t\n4261:\tstatic inline void flush_lru_batch(struct list_head *head, struct list_head **batch_end,\n4262:\t\t\t\t\t struct list_head *dst)\n4263:\t{\n4264:\t\tLIST_HEAD(movable);\n"
} |
| 29/3 | 2026/10/15 23:43 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 38653 output: 35 thoughts: 187 Thoughts: Here's my analysis of the provided code snippet, structured as if I were thinking through it myself: **Analyzing Memory Management and LRU Reference Tracking** Okay, so I'm looking at how memory walks are initialized and managed. It seems `set_mm_walk` is responsible for allocating the walk structure. It can either use `kzalloc_obj`, which guarantees zero-initialization, or leverage `pgdat->mm_walk`, a static structure pre-allocated and zeroed within the `pgdat` itself. Good, that establishes a clean slate. The `reset_batch_size` function seems to be used to reset the walk, and as part of it, it explicitly zeros out the `walk->nr_activated` array for each type and zone. Further, `clear_mm_walk` includes a `VM_WARN_ON_ONCE` check that verifies `walk->nr_activated` is entirely zero using `memchr_inv`. This confirms that we're explicitly zeroing or expecting zero initialization of the counters within that structure. This is essential for preventing stale data from affecting our counts. Now, let's drill down deeper, and see where `folio_inc_lru_refs_walk` is called, and how it is actually used. This means pulling up `mm/vmscan.c`. It's critical to understanding the bigger picture of how LRU references are handled. |
| 30/3 | 2026/10/15 23:43 | tool | read-file |
0mArgs:{
"File": "mm/vmscan.c",
"FirstLine": 1115,
"LineCount": 60
}Results: {
"Contents": "1115:\t * Old LRU refs, and updated gen and LRU refs info by the successful\n1116:\t * cmpxchg are all stored by returning arguments.\n1117:\t */\n1118:\tstatic int folio_inc_lru_refs_walk(struct folio *folio, struct lruvec *lruvec,\n1119:\t\t\t\t\t const vma_flags_t *vma_flags, int *new_gen,\n1120:\t\t\t\t\t int *old_refs, int *new_refs, int *type)\n1121:\t{\n1122:\t\tunsigned long new_flags, old_flags = READ_ONCE(*folio_flags(folio, 0));\n1123:\t\tint refs, old_gen, max_gen;\n1124:\t\tunsigned long max_seq;\n1125:\t\n1126:\t\tdo {\n1127:\t\t\tmax_seq = READ_ONCE(lruvec-\u003elrugen.max_seq);\n1128:\t\t\tmax_gen = lru_gen_from_seq(max_seq);\n1129:\t\t\told_gen = lru_get_gen_flags(old_flags);\n1130:\t\t\trefs = lru_get_refs_flags(old_flags) + 1;\n1131:\t\t\t*type = folio_flags_is_file_lru(\u0026old_flags);\n1132:\t\t\tnew_flags = old_flags;\n1133:\t\n1134:\t\t\tif (old_gen \u003e= 0 \u0026\u0026 old_gen != max_gen) {\n1135:\t\t\t\t*new_refs = min(refs, LRU_REFS_PROTECTED);\n1136:\t\t\t\t/* Promote second page table access or executable */\n1137:\t\t\t\tif (refs \u003e LRU_REFS_REFERENCED || is_exec_file_folio(folio, vma_flags))\n1138:\t\t\t\t\t*new_gen = max_gen;\n1139:\t\t\t\t/* First access only defers eviction of a tail gen */\n1140:\t\t\t\telse if (lru_gen_at_tail(lruvec, *type, old_gen))\n1141:\t\t\t\t\t*new_gen = (old_gen + 1) % MAX_NR_GENS;\n1142:\t\t\t\telse\n1143:\t\t\t\t\t*new_gen = old_gen;\n1144:\t\t\t\tlru_set_gen_flags(\u0026new_flags, *new_gen);\n1145:\t\t\t\tlru_set_refs_flags(\u0026new_flags, *new_refs);\n1146:\t\t\t} else {\n1147:\t\t\t\t*new_gen = old_gen;\n1148:\t\t\t\t*new_refs = min(refs, LRU_REFS_MAX);\n1149:\t\t\t\tlru_set_refs_flags(\u0026new_flags, *new_refs);\n1150:\t\t\t}\n1151:\t\t\tif (new_flags == old_flags)\n1152:\t\t\t\tbreak;\n1153:\t\t} while (!try_cmpxchg(folio_flags(folio, 0), \u0026old_flags, new_flags));\n1154:\t\n1155:\t\t*old_refs = lru_get_refs_flags(old_flags);\n1156:\t\n1157:\t\treturn old_gen;\n1158:\t}\n1159:\t\n1160:\t/*\n1161:\t * Update the lru refs indicator of an isolated folio, only used on\n1162:\t * mapped folios upon the final eviction.\n1163:\t *\n1164:\t * Increments the refs count (capped at LRU_REFS_PROTECTED, evict_folios()\n1165:\t * then caps it at LRU_REFS_WORKINGSET). Returns true if the caller should\n1166:\t * activate the folio (second access or executable), false to put it back\n1167:\t * for a second chance.\n1168:\t */\n1169:\tstatic bool folio_inc_lru_refs_isolated(struct folio *folio, const vma_flags_t *vma_flags)\n1170:\t{\n1171:\t\tunsigned long new_flags, old_flags = READ_ONCE(*folio_flags(folio, 0));\n1172:\t\tint refs;\n1173:\t\n1174:\t\tdo {\n"
} |
| 31/3 | 2026/10/15 23:43 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 40295 output: 35 thoughts: 4076 Thoughts: **My Analysis of LRU Reference Counting and Related Code** Okay, let's break this down. My main focus here is ensuring that all variables are correctly initialized and that no information leaks occur due to the changes related to the LRU reference counting scheme. I'm scrutinizing these code snippets in relation to potential uninitialized variables, race conditions, and possible information leaks to userspace. First, I examined `folio_inc_lru_refs_walk`. Here, I'm tracing the data flow to ensure that variables like `new_gen`, `old_gen`, `old_refs`, `new_refs`, and `type` are always initialized upon function return. I've concluded that these variables are, in fact, always initialized within the function's various execution paths, ensuring no use of uninitialized values. Next, I looked at `__folio_inc_gen`. This function increments the generation counter of a folio within the LRU. Here, the potential for early returns and the conditions inside the do-while loop are critical. I have confirmed that if the loop breaks via early return, the return value `new_gen` is valid. I've also verified that if the loop body executes, variables like `old_refs`, `refs`, and `file` are always set before `try_cmpxchg` succeeds. Moving on to `folio_inc_lru_refs`, I have performed a deep dive because it's a core function. I've been extremely careful here. My main concern was the handling of the `old_gen < 0` case and whether this could lead to the use of uninitialized variables. I've walked the logic carefully. I've looked at the implications of the conditional `if (gen != old_gen)`. I've determined that the `old_gen < 0` case does not lead to any uninitialized variable usage or null pointer dereferences, as all the variables are initialized at the top of the loop and appropriately handled in the edge cases. All the code paths are valid and all variables initialized. The `lruvec` and `max_seq` variables are assigned correctly before they are used. Then, I focused on `folio_reset_lru_refs`. I was concerned about variables being used before their initialization, especially inside the loop. However, I have verified that all required variables are correctly initialized within all execution paths, including `break` cases. Finally, I checked the rest of the code for user-space information leaks and found nothing. I reviewed the code in `fs/proc/task_mmu.c`, specifically `smaps_account` and `clear_refs_pte_range`, as these are related to user-space information. It is important to confirm that the changes did not introduce any information leaks. Replacing `folio_test_young` with `folio_check_lru_refs` and `folio_clear_referenced` with `folio_clear_lru_refs` seems safe. I reviewed the `lru_gen_seq_show` function in `mm/vmscan.c` and validated the usage of `atomic_long_read` to eliminate potential uninitialized values. Additionally, I reviewed `workingset.c`, checking `lru_gen_eviction` and `lru_gen_refault` for any usage of potentially uninitialized variables. |
| 32/3 | 2026/10/15 23:44 | tool | read-file |
0mArgs:{
"File": "mm/workingset.c",
"FirstLine": 240,
"LineCount": 95
}Results: {
"Contents": " 240:\t\n 241:\tstatic void *lru_gen_eviction(struct folio *folio)\n 242:\t{\n 243:\t\tint hist;\n 244:\t\tunsigned long token;\n 245:\t\tunsigned long min_seq;\n 246:\t\tstruct lruvec *lruvec;\n 247:\t\tstruct lru_gen_folio *lrugen;\n 248:\t\tint type = folio_is_file_lru(folio);\n 249:\t\tint delta = folio_nr_pages(folio);\n 250:\t\tint refs = folio_lru_refs(folio);\n 251:\t\tint tier = lru_tier_from_refs(refs);\n 252:\t\tstruct mem_cgroup *memcg;\n 253:\t\tstruct pglist_data *pgdat = folio_pgdat(folio);\n 254:\t\tunsigned short memcg_id;\n 255:\t\n 256:\t\tBUILD_BUG_ON(LRU_GEN_WIDTH + LRU_REFS_BITS \u003e\n 257:\t\t\t BITS_PER_LONG - max(EVICTION_SHIFT, EVICTION_SHIFT_ANON));\n 258:\t\n 259:\t\trcu_read_lock();\n 260:\t\tmemcg = folio_memcg(folio);\n 261:\t\tlruvec = mem_cgroup_lruvec(memcg, pgdat);\n 262:\t\tlrugen = \u0026lruvec-\u003elrugen;\n 263:\t\tmin_seq = READ_ONCE(lrugen-\u003emin_seq[type]);\n 264:\t\ttoken = (min_seq \u003c\u003c LRU_REFS_BITS) | refs \u003e\u003e 1;\n 265:\t\n 266:\t\thist = lru_hist_from_seq(min_seq);\n 267:\t\tatomic_long_add(delta, \u0026lrugen-\u003eevicted[hist][type][tier]);\n 268:\t\tmemcg_id = mem_cgroup_private_id(memcg);\n 269:\t\trcu_read_unlock();\n 270:\t\n 271:\t\treturn pack_shadow(memcg_id, pgdat, token, refs \u0026 1, type);\n 272:\t}\n 273:\t\n 274:\t/*\n 275:\t * Tests if the shadow entry is for a folio that was recently evicted.\n 276:\t * Fills in @lruvec, @token, @workingset with the values unpacked from shadow.\n 277:\t */\n 278:\tstatic bool lru_gen_test_recent(void *shadow, struct lruvec **lruvec,\n 279:\t\t\t\t\tunsigned long *token, bool *workingset, bool file)\n 280:\t{\n 281:\t\tint memcg_id;\n 282:\t\tunsigned long max_seq;\n 283:\t\tstruct mem_cgroup *memcg;\n 284:\t\tstruct pglist_data *pgdat;\n 285:\t\n 286:\t\tunpack_shadow(shadow, \u0026memcg_id, \u0026pgdat, token, workingset);\n 287:\t\n 288:\t\tmemcg = mem_cgroup_from_private_id(memcg_id);\n 289:\t\t*lruvec = mem_cgroup_lruvec(memcg, pgdat);\n 290:\t\n 291:\t\tmax_seq = READ_ONCE((*lruvec)-\u003elrugen.max_seq);\n 292:\t\tmax_seq \u0026= (file ? EVICTION_MASK : EVICTION_MASK_ANON) \u003e\u003e LRU_REFS_BITS;\n 293:\t\n 294:\t\treturn abs_diff(max_seq, *token \u003e\u003e LRU_REFS_BITS) \u003c MAX_NR_GENS;\n 295:\t}\n 296:\t\n 297:\tstatic void lru_gen_refault(struct folio *folio, void *shadow)\n 298:\t{\n 299:\t\tbool recent;\n 300:\t\tint hist, tier, refs;\n 301:\t\tbool workingset;\n 302:\t\tunsigned long token;\n 303:\t\tstruct lruvec *lruvec;\n 304:\t\tstruct lru_gen_folio *lrugen;\n 305:\t\tint type = folio_is_file_lru(folio);\n 306:\t\tint delta = folio_nr_pages(folio);\n 307:\t\n 308:\t\trcu_read_lock();\n 309:\t\n 310:\t\trecent = lru_gen_test_recent(shadow, \u0026lruvec, \u0026token, \u0026workingset, type);\n 311:\t\tif (lruvec != folio_lruvec(folio))\n 312:\t\t\tgoto unlock;\n 313:\t\n 314:\t\tmod_lruvec_state(lruvec, WORKINGSET_REFAULT_BASE + type, delta);\n 315:\t\n 316:\t\tif (!recent)\n 317:\t\t\tgoto unlock;\n 318:\t\n 319:\t\tlrugen = \u0026lruvec-\u003elrugen;\n 320:\t\n 321:\t\thist = lru_hist_from_seq(READ_ONCE(lrugen-\u003emin_seq[type]));\n 322:\t\trefs = ((token \u0026 (BIT(LRU_REFS_BITS) - 1)) \u003c\u003c 1) + workingset;\n 323:\t\ttier = lru_tier_from_refs(refs);\n 324:\t\n 325:\t\tatomic_long_add(delta, \u0026lrugen-\u003erefaulted[hist][type][tier]);\n 326:\t\n 327:\t\t/*\n 328:\t\t * Activate a fault-driven refault folio, which would have been\n 329:\t\t * promoted had it stayed in memory.\n 330:\t\t */\n 331:\t\tif (refs \u003e= LRU_REFS_REFERENCED) {\n 332:\t\t\tif (lru_gen_in_fault()) {\n 333:\t\t\t\tfolio_set_active(folio);\n 334:\t\t\t\tmod_lruvec_state(lruvec, WORKINGSET_ACTIVATE_BASE + type, delta);\n"
} |
| 33/3 | 2026/10/15 23:44 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 46755 output: 31 thoughts: 1305 Thoughts: **Analyzing LRU Reference Counting and Related Folio Operations** Okay, let's dive into this. First, I need to make absolutely sure about how `unpack_shadow` functions: specifically, `unpack_shadow(shadow, &memcg_id, &pgdat, token, workingset);`. It appears to be initializing `memcg_id`, `pgdat`, `token`, and `workingset`. From this, `refs` is calculated as `((token & (BIT(LRU_REFS_BITS) - 1)) << 1) + workingset;`. This looks clean, all variables initialized and values are what I expect. Next, I'll examine the inline functions in `include/linux/mm_inline.h`: `lru_set_refs_flags` and `lru_get_refs_flags`. I see the clear usage of bitwise operations for setting and getting the reference flags: `PG_referenced` and `PG_workingset`, as well as the more general LRU reference count, which is masked. The code uses `VM_WARN_ON_ONCE` to catch unexpected values. `lru_get_refs_flags` appears to cleanly return the number of accesses, handling all cases (including the total count stored in the flags). The `refs` variable is always initialized cleanly as expected. The `folio_lru_refs` inline function just wraps the `lru_get_refs_flags` call, so it's all good. Now, let's look at `lru_get_gen_flags`. This function is retrieving the generation flag. It's checking the generation value and using `VM_WARN_ON_ONCE` for error cases. Nothing to raise alarms there. Next up, `folio_lruvec_live_get`: This seems to deal with acquiring the `lruvec` and managing the `memcg` hierarchy, including checks for dying `memcg` instances. The variables are all initialized, and the code appears robust in handling the hierarchy traversal using `rcu_read_lock`. I'll now turn my attention to the call sites. I need to ensure the changes are consistent and correct across these different areas: `fs/erofs/zdata.c`, `mm/damon/paddr.c`, `mm/gup.c`, `mm/huge_memory.c`, `mm/khugepaged.c`, `mm/madvise.c`, and `mm/migrate.c`. I see the adjustments: in `erofs` now uses `folio_test_workingset` instead of `PageWorkingset` as expected. `mm/damon/paddr.c` is using `folio_clear_lru_refs` instead of `folio_clear_referenced`, which is consistent. `mm/gup.c` is using `folio_inc_lru_refs_fast` instead of `folio_set_referenced` which looks more efficient. `mm/huge_memory.c` now has different checks, using `vma_has_recency`. `mm/khugepaged.c` has also had a change, using `folio_check_lru_refs`, all of these updates are looking good. In `mm/madvise.c` it's just the expected calls to prepare the folio. Finally, in `mm/migrate.c`, the previous check for `folio_test_workingset` has been removed. I need to double-check `folio_migrate_lru_refs` specifically. Let's check `folio_migrate_lru_refs` in `mm/migrate.c`. |
| 34/3 | 2026/10/15 23:44 | tool | grepper |
0mArgs:{
"Expression": "folio_migrate_lru_refs",
"PathPrefixes": [
"mm/"
]
}Results: {
"Output": "mm/migrate.c=775=void folio_migrate_flags(struct folio *newfolio, struct folio *folio)\n--\nmm/migrate.c-779-\t/* Copy the reference state, including PG_referenced */\nmm/migrate.c:780:\tfolio_migrate_lru_refs(newfolio, folio);\nmm/migrate.c-781-\n"
} |
| 35/3 | 2026/10/15 23:44 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 48996 output: 514 thoughts: 1282 Thoughts: **MGLRU Patch Series: KMSAN Applicability Assessment** Okay, let's break this down. Line 780 of `folio_migrate_flags` already calls `folio_migrate_lru_refs(newfolio, folio)`, which I need to remember transfers the *entire* ref state, including the `referenced` and `workingset` bits. Got it. Now, I've got to be meticulous about this KMSAN/KASAN distinction. Standard KASAN kernel builds have so much already: out-of-bounds, use-after-free, double-free, lockdep, UB sanitizers... the works. KMSAN is for catching reads of uninitialized memory *specifically*. It's a resource hog, so no sense in wasting time with it if KASAN, LOCKDEP, or standard bug detectors have the potential to already catch an error. The rule is clear: *Only* set `NeedsKMSAN=true` if uninitialized memory risks are *only* detected by KMSAN. That means the patch *must* be introducing or changing things that involve uninitialized memory. Let's run down the checklist: 1. **Kernel structures to user space?** No. `/proc/kpageflags` is touched, but not modified directly. `/proc/<pid>/smaps` has `mss->referenced` modified, but that `mss` is allocated and initialized by the caller. `/proc/vmstat` and MGLRU debugfs use `seq_printf` for output. No user-space copy of structures with potential uninitialized fields or padding. 2. **Conditional logic on uninitialized variables?** Nope. Everything is initialized: local variables, struct fields (`old_flags`, `old_refs`, `old_gen`, `refs`, `gen`, `nr_activated`) are explicitly initialized or `kzalloc`'d. 3. **Complex data structures with potentially uninitialized fields?** No again. `lru_gen_mm_walk` is `kzalloc`'d or embedded in `pglist_data` (and thus zeroed). 4. **Bounds checks, lengths, etc., that expose uninitialized bytes?** No. 5. **Risk Assessment:** The patch series' risks revolve around MGLRU generation advancement, tier classification, folio flag bit changes, concurrency/synchronization (`cmpxchg` on flags, atomic operations, RCU-based lookups). Any bugs would likely be concurrency issues, race conditions, accounting errors, deadlocks, folio state corruption. Those are exactly the kinds of issues that KASAN, LOCKDEP, and page debugging are built to catch, with things like `VM_WARN_ON_ONCE_FOLIO`. So, it's clear: `NeedsKMSAN` is `false`. Now, time to craft the `set-results` call: `NeedsKMSAN: false` `KMSANReasoning: Provide a clear, structured contrast of KMSAN vs KASAN applicability for this patch series.` `- Nature of the changes: refactoring and redesigning MGLRU folio referenced count and tiering mechanism, using PG_referenced, PG_workingset, and LRU_REFS_WIDTH bits in folio flags, updating folio migration and reclamation paths, atomic stats tracking in `lru_gen_folio`.` `- KMSAN considerations: No kernel-to-user copy structures, netlink attributes, ioctls, or padding are introduced or modified. All variables in newly added and modified functions (`folio_inc_lru_refs`, `folio_reset_lru_refs`, `folio_inc_lru_refs_walk`, etc.) are explicitly initialized before use. The new field `nr_activated` in `struct lru_gen_mm_walk` is zero-allocated via `kzalloc_obj` or statically zero-initialized in `pglist_data`, and asserted to be zero via `memchr_inv` in `clear_mm_walk`.` `- KASAN / general detector applicability: Any risks in this patch revolve around concurrency/race conditions with lockless cmpxchg on folio flags, LRU accounting balance, or folio flag bitmask manipulation, which are within the scope of KASAN, LOCKDEP, and page flag / VM assertions.` `- Conclusion: NeedsKMSAN is false.` Okay, time to submit the call to `set-results`. |
| 36/3 | 2026/10/15 23:44 | tool | set-results |
0mArgs:{
"KMSANReasoning": "The patch series reworks the Multi-Gen LRU (MGLRU) referenced count and tiering tracking, storing the reference count across PG_referenced, PG_workingset, and LRU_REFS bits in folio-\u003eflags, while updating page reclamation, migration, madvise, and compaction paths.\n\nKMSAN vs KASAN Applicability Analysis:\n1. Uninitialized Memory / Info-Leaks (KMSAN):\n - The changes do not introduce or modify any structures or buffers copied to user space (e.g., via copy_to_user, put_user, netlink, or ioctl). User-facing interfaces like smaps and lru_gen_seq_show only read formatted integers.\n - All newly introduced and modified functions (such as folio_inc_lru_refs, folio_reset_lru_refs, folio_inc_lru_refs_walk, __folio_inc_gen) explicitly initialize all local variables before use.\n - The new field `nr_activated` in `struct lru_gen_mm_walk` is zeroed either via `kzalloc_obj` in `set_mm_walk` or as part of the zero-initialized `pgdat-\u003emm_walk`, and is asserted to be zero via `memchr_inv` in `clear_mm_walk`.\n - Eviction shadow packing/unpacking handles integer tokens and booleans without exposing uninitialized memory.\n\n2. Memory Safety and Concurrency (KASAN / Standard Detectors):\n - The primary risks introduced by this patch series involve lockless atomic cmpxchg operations on folio flags, memory ordering (smp_rmb/smp_store_release), list splicing under RCU / lru_lock, and LRU size accounting (`__update_lru_size`).\n - Any potential regressions (such as use-after-free, double-free, out-of-bounds access, race conditions, or locking issues) are covered by KASAN, LOCKDEP, and kernel page/VM debugging assertions (e.g., VM_WARN_ON_ONCE_FOLIO).\n\nBecause no uninitialized memory risks or kernel-to-user information leaks are introduced, a dedicated KMSAN fuzzing session is not warranted.",
"NeedsKMSAN": false
}Results: {
"KMSANReasoning": "The patch series reworks the Multi-Gen LRU (MGLRU) referenced count and tiering tracking, storing the reference count across PG_referenced, PG_workingset, and LRU_REFS bits in folio-\u003eflags, while updating page reclamation, migration, madvise, and compaction paths.\n\nKMSAN vs KASAN Applicability Analysis:\n1. Uninitialized Memory / Info-Leaks (KMSAN):\n - The changes do not introduce or modify any structures or buffers copied to user space (e.g., via copy_to_user, put_user, netlink, or ioctl). User-facing interfaces like smaps and lru_gen_seq_show only read formatted integers.\n - All newly introduced and modified functions (such as folio_inc_lru_refs, folio_reset_lru_refs, folio_inc_lru_refs_walk, __folio_inc_gen) explicitly initialize all local variables before use.\n - The new field `nr_activated` in `struct lru_gen_mm_walk` is zeroed either via `kzalloc_obj` in `set_mm_walk` or as part of the zero-initialized `pgdat-\u003emm_walk`, and is asserted to be zero via `memchr_inv` in `clear_mm_walk`.\n - Eviction shadow packing/unpacking handles integer tokens and booleans without exposing uninitialized memory.\n\n2. Memory Safety and Concurrency (KASAN / Standard Detectors):\n - The primary risks introduced by this patch series involve lockless atomic cmpxchg operations on folio flags, memory ordering (smp_rmb/smp_store_release), list splicing under RCU / lru_lock, and LRU size accounting (`__update_lru_size`).\n - Any potential regressions (such as use-after-free, double-free, out-of-bounds access, race conditions, or locking issues) are covered by KASAN, LOCKDEP, and kernel page/VM debugging assertions (e.g., VM_WARN_ON_ONCE_FOLIO).\n\nBecause no uninitialized memory risks or kernel-to-user information leaks are introduced, a dedicated KMSAN fuzzing session is not warranted.",
"NeedsKMSAN": false
} |
| Total Calls | Total Tokens | Avg Tokens | Total Duration (Seconds) | Avg Duration (Seconds) |
|---|
| Total Calls | Total Duration (Seconds) | Avg Duration (Seconds) |
|---|