AI Trajectory


Trajectory: (Expand/Collapse All)
Seq Timestamp Type Name Duration
0/0 2026/09/06 16:23 flow patch-triage
1m Args:
null

Results:
{
  "EnableConfigs": [
    "LRU_GEN",
    "LRU_GEN_ENABLED",
    "MEMCG",
    "MIGRATION"
  ],
  "FocusSymbols": [
    "lru_gen_look_around",
    "folio_migrate_flags",
    "mem_cgroup_update_lru_size",
    "walk_pte_range",
    "walk_pmd_range_locked",
    "folio_update_gen",
    "evict_folios",
    "isolate_folio",
    "lru_gen_inc_refs",
    "lru_gen_clear_refs"
  ],
  "KMSANReasoning": "The patch series refactors Multi-Generational LRU (MGLRU) folio flag helpers, page table / rmap scanning logic, and memcg LRU zone size accounting. Specifically:\n- It replaces direct `folio-\u003eflags.f` access with `folio_flags(folio, 0)` accessor helpers (`lru_set_gen_flags`, `lru_get_gen_flags`, `lru_set_refs_flags`, `lru_get_refs_flags`, `folio_set_lru_refs`, `folio_migrate_lru_refs`).\n- It cleans up `read_ctrl_pos` parameterization and `walk_update_folio` callers. Stack variables such as `struct ctrl_pos pos` are fully initialized in `read_ctrl_pos`, and the `int *type` output parameter in `folio_update_gen` is guaranteed to be initialized when the return value indicates promotion (`old_gen \u003e= 0`).\n- It converts `mz-\u003elru_zone_size` to signed `long` to simplify underflow handling.\n\nNone of these changes allocate uninitialized buffers, copy kernel data to user space, or read uninitialized memory. Potential bugs in these changes (such as concurrency/locking races or state corruption) are adequately covered by standard KASAN, LOCKDEP, and kernel assertion builds without requiring a dedicated KMSAN fuzzing session.",
  "NeedsKMSAN": false,
  "Reasoning": "The patch touches core memory management code across mm/vmscan.c, mm/folio.c, mm/memcontrol.c, and mm/migrate.c, altering Multi-Gen LRU (MGLRU) reference tracking, folio flag manipulation, tier calculations, page eviction, and memcg LRU zone accounting. It also adds several VM_WARN_ON_ONCE assertions and changes synchronization/arithmetic logic. These paths are fully reachable and worth fuzzing.",
  "WorthFuzzing": true
}

1/1 2026/09/06 16:23 action read-patch-diff
0m Args:
null

Results:
{
  "PatchDiff": "commit e3cbbc76e1c966c76fe621299a32d17afa7e4003\nAuthor: syz-cluster \u003ctriage@syzkaller.com\u003e\nDate:   Sun Sep 6 16:23:49 2026 +0000\n\n    syz-cluster: applied patch under review\n\ndiff --git a/include/linux/memcontrol.h b/include/linux/memcontrol.h\nindex c799926435560..2b67694285d04 100644\n--- a/include/linux/memcontrol.h\n+++ b/include/linux/memcontrol.h\n@@ -100,7 +100,7 @@ struct mem_cgroup_per_node {\n \t/* Fields which get updated often at the end. */\n \tstruct lruvec\t\tlruvec;\n \tCACHELINE_PADDING(_pad2_);\n-\tunsigned long\t\tlru_zone_size[MAX_NR_ZONES][NR_LRU_LISTS];\n+\tlong\t\t\tlru_zone_size[MAX_NR_ZONES][NR_LRU_LISTS];\n \tstruct mem_cgroup_reclaim_iter\titer;\n \n \t/*\n@@ -887,10 +887,15 @@ static inline\n unsigned long mem_cgroup_get_zone_lru_size(struct lruvec *lruvec,\n \t\tenum lru_list lru, int zone_idx)\n {\n+\tlong val;\n \tstruct mem_cgroup_per_node *mz;\n \n \tmz = container_of(lruvec, struct mem_cgroup_per_node, lruvec);\n-\treturn READ_ONCE(mz-\u003elru_zone_size[zone_idx][lru]);\n+\tval = READ_ONCE(mz-\u003elru_zone_size[zone_idx][lru]);\n+\tif (WARN_ON_ONCE(val \u003c 0))\n+\t\treturn 0;\n+\n+\treturn val;\n }\n \n void __mem_cgroup_handle_over_high(gfp_t gfp_mask);\ndiff --git a/include/linux/mm_inline.h b/include/linux/mm_inline.h\nindex 621c8653d8f7e..ab69b9930893f 100644\n--- a/include/linux/mm_inline.h\n+++ b/include/linux/mm_inline.h\n@@ -30,6 +30,17 @@ static inline int folio_is_file_lru(const struct folio *folio)\n \treturn !folio_test_swapbacked(folio);\n }\n \n+/**\n+ * folio_flags_is_file_lru - Should the folio be on a file LRU or anon LRU?\n+ * @flags: The folio's flags.\n+ *\n+ * Just like folio_is_file_lru but take the folio flags directly instead.\n+ */\n+static inline int folio_flags_is_file_lru(const unsigned long *flags)\n+{\n+\treturn !test_bit(PG_swapbacked, flags);\n+}\n+\n static __always_inline void __update_lru_size(struct lruvec *lruvec,\n \t\t\t\tenum lru_list lru, enum zone_type zid,\n \t\t\t\tlong nr_pages)\n@@ -142,10 +153,66 @@ static inline int lru_tier_from_refs(int refs, bool workingset)\n \treturn workingset ? MAX_NR_TIERS - 1 : order_base_2(refs);\n }\n \n-static inline int folio_lru_refs(const struct folio *folio)\n+/**\n+ * lru_set_gen_flags - Set the LRU generation number to specified folio flags.\n+ * @flags: pointer to the folio flags\n+ * @gen: generation number, between 0 and (MAX_NR_GENS - 1), inclusive.\n+ */\n+static inline void lru_set_gen_flags(unsigned long *flags, int gen)\n+{\n+\tBUILD_BUG_ON(LRU_GEN_MASK \u0026 LRU_REFS_MASK);\n+\tVM_WARN_ON_ONCE(gen \u003e= MAX_NR_GENS || gen \u003c 0);\n+\t/* Store gen offset by 1, zero means the folio is off-list. */\n+\t*flags \u0026= ~LRU_GEN_MASK;\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+{\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+/**\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+ */\n+static inline void lru_set_refs_flags(unsigned long *flags, unsigned int refs)\n {\n-\tunsigned long flags = READ_ONCE(folio-\u003eflags.f);\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+\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+}\n+\n+/**\n+ * lru_get_refs_flags - Return LRU referenced / access count from folio flags.\n+ * @flags: folio flags\n+ *\n+ * Reads the LRU referenced count set by lru_set_refs_flags().\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@@ -155,11 +222,24 @@ static inline int folio_lru_refs(const struct folio *folio)\n \treturn ((flags \u0026 LRU_REFS_MASK) \u003e\u003e LRU_REFS_PGOFF) + 1;\n }\n \n-static inline int folio_lru_gen(const struct folio *folio)\n+static inline int folio_lru_refs(const struct folio *folio)\n {\n-\tunsigned long flags = READ_ONCE(folio-\u003eflags.f);\n+\treturn lru_get_refs_flags(READ_ONCE(*const_folio_flags(folio, 0)));\n+}\n \n-\treturn ((flags \u0026 LRU_GEN_MASK) \u003e\u003e LRU_GEN_PGOFF) - 1;\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\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+{\n+\treturn lru_get_gen_flags(READ_ONCE(*const_folio_flags(folio, 0)));\n }\n \n static inline bool lru_gen_is_active(const struct lruvec *lruvec, int gen)\n@@ -270,7 +350,7 @@ 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(\u0026folio-\u003eflags.f, LRU_GEN_MASK | BIT(PG_active), flags);\n+\tset_mask_bits(folio_flags(folio, 0), LRU_GEN_MASK | BIT(PG_active), flags);\n \n \tlru_gen_update_size(lruvec, folio, -1, gen);\n \t/* for folio_rotate_reclaimable() */\n@@ -295,7 +375,7 @@ static inline bool lru_gen_del_folio(struct lruvec *lruvec, struct 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(\u0026folio-\u003eflags.f, LRU_GEN_MASK, flags);\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 \n \tlru_gen_update_size(lruvec, folio, gen, -1);\n@@ -304,11 +384,19 @@ static inline bool lru_gen_del_folio(struct lruvec *lruvec, struct folio *folio,\n \treturn true;\n }\n \n-static inline void folio_migrate_refs(struct folio *new, const struct folio *old)\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-\tunsigned long refs = READ_ONCE(old-\u003eflags.f) \u0026 LRU_REFS_MASK;\n-\n-\tset_mask_bits(\u0026new-\u003eflags.f, LRU_REFS_MASK, refs);\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@@ -337,9 +425,10 @@ static inline bool lru_gen_del_folio(struct lruvec *lruvec, struct folio *folio,\n \treturn false;\n }\n \n-static inline void folio_migrate_refs(struct folio *new, const struct folio *old)\n+static inline void folio_migrate_lru_refs(struct folio *new, const struct folio *old)\n {\n-\n+\tif (folio_test_referenced(old))\n+\t\tfolio_set_referenced(new);\n }\n #endif /* CONFIG_LRU_GEN */\n \ndiff --git a/include/linux/mmzone.h b/include/linux/mmzone.h\nindex 84e237f2c17db..c9ae7991a8b20 100644\n--- a/include/linux/mmzone.h\n+++ b/include/linux/mmzone.h\n@@ -495,11 +495,14 @@ enum lruvec_flags {\n  * folio-\u003eflags, masked by LRU_REFS_MASK.\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 #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,\ndiff --git a/mm/folio.c b/mm/folio.c\nindex 50a6dbe55998e..47a437e0f7fde 100644\n--- a/mm/folio.c\n+++ b/mm/folio.c\n@@ -354,26 +354,28 @@ static void __lru_cache_activate_folio(struct folio *folio)\n \n static void lru_gen_inc_refs(struct folio *folio)\n {\n-\tunsigned long new_flags, old_flags = READ_ONCE(folio-\u003eflags.f);\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_test_referenced(folio)) {\n-\t\tset_mask_bits(\u0026folio-\u003eflags.f, LRU_REFS_MASK, BIT(PG_referenced));\n+\tif (!folio_lru_refs(folio)) {\n+\t\tfolio_set_lru_refs(folio, 1);\n \t\treturn;\n \t}\n \n \tdo {\n-\t\tif ((old_flags \u0026 LRU_REFS_MASK) == LRU_REFS_MASK) {\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-\n-\t\tnew_flags = old_flags + BIT(LRU_REFS_PGOFF);\n-\t} while (!try_cmpxchg(\u0026folio-\u003eflags.f, \u0026old_flags, new_flags));\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@@ -385,7 +387,8 @@ static bool lru_gen_clear_refs(struct folio *folio)\n \tif (gen \u003c 0)\n \t\treturn true;\n \n-\tset_mask_bits(\u0026folio-\u003eflags.f, LRU_REFS_FLAGS | BIT(PG_workingset), 0);\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]);\ndiff --git a/mm/memcontrol.c b/mm/memcontrol.c\nindex bd1e7e1544265..f17b2b2d61055 100644\n--- a/mm/memcontrol.c\n+++ b/mm/memcontrol.c\n@@ -1529,28 +1529,12 @@ void mem_cgroup_update_lru_size(struct lruvec *lruvec, enum lru_list lru,\n \t\t\t\tint zid, long nr_pages)\n {\n \tstruct mem_cgroup_per_node *mz;\n-\tunsigned long *lru_size;\n-\tlong size;\n \n \tif (mem_cgroup_disabled())\n \t\treturn;\n \n \tmz = container_of(lruvec, struct mem_cgroup_per_node, lruvec);\n-\tlru_size = \u0026mz-\u003elru_zone_size[zid][lru];\n-\n-\tif (nr_pages \u003c 0)\n-\t\t*lru_size += nr_pages;\n-\n-\tsize = *lru_size;\n-\tif (WARN_ONCE(size \u003c 0,\n-\t\t\"%s(%p, %d, %ld): lru_size %ld\\n\",\n-\t\t__func__, lruvec, lru, nr_pages, size)) {\n-\t\tVM_BUG_ON(1);\n-\t\t*lru_size = 0;\n-\t}\n-\n-\tif (nr_pages \u003e 0)\n-\t\t*lru_size += nr_pages;\n+\tmz-\u003elru_zone_size[zid][lru] += nr_pages;\n }\n \n /**\ndiff --git a/mm/migrate.c b/mm/migrate.c\nindex 15b45832bcfa7..a369d0c95c386 100644\n--- a/mm/migrate.c\n+++ b/mm/migrate.c\n@@ -776,8 +776,9 @@ void folio_migrate_flags(struct folio *newfolio, struct folio *folio)\n {\n \tint cpupid;\n \n-\tif (folio_test_referenced(folio))\n-\t\tfolio_set_referenced(newfolio);\n+\t/* Copy the reference state, including PG_referenced */\n+\tfolio_migrate_lru_refs(newfolio, folio);\n+\n \tif (folio_test_uptodate(folio))\n \t\tfolio_mark_uptodate(newfolio);\n \tif (folio_test_clear_active(folio)) {\n@@ -807,7 +808,6 @@ void folio_migrate_flags(struct folio *newfolio, struct folio *folio)\n \tif (folio_test_idle(folio))\n \t\tfolio_set_idle(newfolio);\n \n-\tfolio_migrate_refs(newfolio, folio);\n \t/*\n \t * Copy NUMA information to the new page, to prevent over-eager\n \t * future migrations of this same page.\ndiff --git a/mm/vmscan.c b/mm/vmscan.c\nindex 245f68c75b289..40d3f1b48a74c 100644\n--- a/mm/vmscan.c\n+++ b/mm/vmscan.c\n@@ -863,19 +863,22 @@ static bool lru_gen_set_refs(struct folio *folio, const vma_flags_t *vma_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\tset_mask_bits(\u0026folio-\u003eflags.f, LRU_REFS_FLAGS, BIT(PG_workingset));\n+\t\t\tfolio_set_workingset(folio);\n+\t\t\tfolio_set_lru_refs(folio, 0);\n \t\t\treturn true;\n \t\t}\n \n-\t\tset_mask_bits(\u0026folio-\u003eflags.f, LRU_REFS_MASK, BIT(PG_referenced));\n+\t\tfolio_set_lru_refs(folio, 1);\n \t\treturn false;\n \t}\n \n \t/* Promote on second access */\n-\tif (folio_lru_refs(folio) \u003e 1)\n-\t\tset_mask_bits(\u0026folio-\u003eflags.f, LRU_REFS_FLAGS, BIT(PG_workingset));\n-\telse\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+\t}\n \treturn true;\n }\n #else\n@@ -3220,8 +3223,8 @@ struct ctrl_pos {\n \tint gain;\n };\n \n-static void read_ctrl_pos(struct lruvec *lruvec, int type, int tier, int gain,\n-\t\t\t  struct ctrl_pos *pos)\n+static void read_ctrl_pos(struct lruvec *lruvec, int type, int tier_min,\n+\t\t\t  int tier_max, int gain, struct ctrl_pos *pos)\n {\n \tint i;\n \tstruct lru_gen_folio *lrugen = \u0026lruvec-\u003elrugen;\n@@ -3230,7 +3233,7 @@ static void read_ctrl_pos(struct lruvec *lruvec, int type, int tier, int gain,\n \tpos-\u003egain = gain;\n \tpos-\u003erefaulted = pos-\u003etotal = 0;\n \n-\tfor (i = tier % MAX_NR_TIERS; i \u003c= min(tier, MAX_NR_TIERS - 1); i++) {\n+\tfor (i = tier_min; i \u003c= tier_max; i++) {\n \t\tpos-\u003erefaulted += lrugen-\u003eavg_refaulted[type][i] +\n \t\t\t\t  atomic_long_read(\u0026lrugen-\u003erefaulted[hist][type][i]);\n \t\tpos-\u003etotal += lrugen-\u003eavg_total[type][i] +\n@@ -3291,11 +3294,11 @@ static bool positive_ctrl_err(struct ctrl_pos *sp, struct ctrl_pos *pv)\n  ******************************************************************************/\n \n /* promote pages accessed through page tables */\n-static int folio_update_gen(struct folio *folio, int gen, const vma_flags_t *vma_flags)\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-\u003eflags.f);\n-\n-\tVM_WARN_ON_ONCE(gen \u003e= MAX_NR_GENS);\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@@ -3304,31 +3307,35 @@ static int folio_update_gen(struct folio *folio, int gen, const vma_flags_t *vma\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\tset_mask_bits(\u0026folio-\u003eflags.f, LRU_REFS_MASK, BIT(PG_referenced));\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_flags \u0026 LRU_GEN_MASK))\n-\t\t\treturn -1;\n+\t\tif (old_gen \u003c 0)\n+\t\t\tbreak;\n \n-\t\tnew_flags = old_flags \u0026 ~(LRU_GEN_MASK | LRU_REFS_FLAGS);\n-\t\tnew_flags |= ((gen + 1UL) \u003c\u003c LRU_GEN_PGOFF) | BIT(PG_workingset);\n-\t} while (!try_cmpxchg(\u0026folio-\u003eflags.f, \u0026old_flags, new_flags));\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-\treturn ((old_flags \u0026 LRU_GEN_MASK) \u003e\u003e LRU_GEN_PGOFF) - 1;\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 {\n-\tunsigned long new_flags, old_flags = READ_ONCE(folio-\u003eflags.f);\n+\tunsigned long new_flags, old_flags = READ_ONCE(*folio_flags(folio, 0));\n \tint new_gen;\n \n-\tVM_WARN_ON_ONCE_FOLIO(!(old_flags \u0026 LRU_GEN_MASK), folio);\n-\n \tdo {\n-\t\tnew_gen = ((old_flags \u0026 LRU_GEN_MASK) \u003e\u003e LRU_GEN_PGOFF) - 1;\n+\t\tnew_gen = lru_get_gen_flags(old_flags);\n+\n \t\t/* folio_update_gen() has promoted this page? */\n \t\tif (new_gen \u003e= 0 \u0026\u0026 new_gen != old_gen) {\n \t\t\tif (increased)\n@@ -3336,11 +3343,12 @@ static int __folio_inc_gen(struct folio *folio, int old_gen, bool *increased)\n \t\t\treturn new_gen;\n \t\t}\n \n+\t\tnew_flags = old_flags;\n \t\tnew_gen = (old_gen + 1) % MAX_NR_GENS;\n \n-\t\tnew_flags = old_flags \u0026 ~(LRU_GEN_MASK | LRU_REFS_FLAGS);\n-\t\tnew_flags |= (new_gen + 1UL) \u003c\u003c LRU_GEN_PGOFF;\n-\t} while (!try_cmpxchg(\u0026folio-\u003eflags.f, \u0026old_flags, new_flags));\n+\t\tlru_set_gen_flags(\u0026new_flags, new_gen);\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 (increased)\n \t\t*increased = true;\n@@ -3362,9 +3370,8 @@ static int folio_inc_gen(struct lruvec *lruvec, struct folio *folio)\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)\n+\t\t\t      int old_gen, int new_gen, int type)\n {\n-\tint type = folio_is_file_lru(folio);\n \tint zone = folio_zonenum(folio);\n \tint delta = folio_nr_pages(folio);\n \n@@ -3551,22 +3558,24 @@ static bool suitable_to_scan(int total, int young)\n }\n \n static void walk_update_folio(struct lru_gen_mm_walk *walk, struct vm_area_struct *vma,\n-\t\tstruct folio *folio, int new_gen, bool dirty)\n+\t\tstruct lruvec *lruvec, struct folio *folio, bool dirty)\n {\n-\tint old_gen;\n+\tint new_gen, old_gen, type;\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, \u0026vma-\u003eflags);\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);\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@@ -3588,8 +3597,6 @@ static bool walk_pte_range(pmd_t *pmd, unsigned long start, unsigned long end,\n \tstruct lru_gen_mm_walk *walk = args-\u003eprivate;\n \tstruct mem_cgroup *memcg = lruvec_memcg(walk-\u003elruvec);\n \tstruct pglist_data *pgdat = lruvec_pgdat(walk-\u003elruvec);\n-\tDEFINE_MAX_SEQ(walk-\u003elruvec);\n-\tint gen = lru_gen_from_seq(max_seq);\n \tunsigned int nr;\n \tpmd_t pmdval;\n \n@@ -3640,7 +3647,7 @@ static bool walk_pte_range(pmd_t *pmd, unsigned long start, unsigned long end,\n \t\t\tcontinue;\n \n \t\tif (last != folio) {\n-\t\t\twalk_update_folio(walk, args-\u003evma, last, gen, dirty);\n+\t\t\twalk_update_folio(walk, args-\u003evma, walk-\u003elruvec, last, dirty);\n \n \t\t\tlast = folio;\n \t\t\tdirty = false;\n@@ -3653,7 +3660,7 @@ static bool walk_pte_range(pmd_t *pmd, unsigned long start, unsigned long end,\n \t\twalk-\u003emm_stats[MM_LEAF_YOUNG] += nr;\n \t}\n \n-\twalk_update_folio(walk, args-\u003evma, last, gen, dirty);\n+\twalk_update_folio(walk, args-\u003evma, walk-\u003elruvec, last, dirty);\n \tlast = NULL;\n \n \tif (i \u003c PTRS_PER_PTE \u0026\u0026 get_next_vma(PMD_MASK, PAGE_SIZE, args, \u0026start, \u0026end))\n@@ -3676,8 +3683,6 @@ static void walk_pmd_range_locked(pud_t *pud, unsigned long addr, struct vm_area\n \tstruct lru_gen_mm_walk *walk = args-\u003eprivate;\n \tstruct mem_cgroup *memcg = lruvec_memcg(walk-\u003elruvec);\n \tstruct pglist_data *pgdat = lruvec_pgdat(walk-\u003elruvec);\n-\tDEFINE_MAX_SEQ(walk-\u003elruvec);\n-\tint gen = lru_gen_from_seq(max_seq);\n \n \tVM_WARN_ON_ONCE(pud_leaf(*pud));\n \n@@ -3731,7 +3736,7 @@ static void walk_pmd_range_locked(pud_t *pud, unsigned long addr, struct vm_area\n \t\t\tgoto next;\n \n \t\tif (last != folio) {\n-\t\t\twalk_update_folio(walk, vma, last, gen, dirty);\n+\t\t\twalk_update_folio(walk, vma, walk-\u003elruvec, last, dirty);\n \n \t\t\tlast = folio;\n \t\t\tdirty = false;\n@@ -3745,7 +3750,7 @@ static void walk_pmd_range_locked(pud_t *pud, unsigned long addr, struct vm_area\n \t\ti = i \u003e MIN_LRU_BATCH ? 0 : find_next_bit(bitmap, MIN_LRU_BATCH, i) + 1;\n \t} while (i \u003c= MIN_LRU_BATCH);\n \n-\twalk_update_folio(walk, vma, last, gen, dirty);\n+\twalk_update_folio(walk, vma, walk-\u003elruvec, last, dirty);\n \n \tlazy_mmu_mode_disable();\n \tspin_unlock(ptl);\n@@ -4351,8 +4356,6 @@ bool lru_gen_look_around(struct page_vma_mapped_walk *pvmw, unsigned int nr)\n \tstruct pglist_data *pgdat = folio_pgdat(folio);\n \tstruct lruvec *lruvec;\n \tstruct lru_gen_mm_state *mm_state;\n-\tunsigned long max_seq;\n-\tint gen;\n \n \tlockdep_assert_held(pvmw-\u003eptl);\n \tVM_WARN_ON_ONCE_FOLIO(folio_test_lru(folio), folio);\n@@ -4389,8 +4392,6 @@ bool lru_gen_look_around(struct page_vma_mapped_walk *pvmw, unsigned int nr)\n \n \tmemcg = get_mem_cgroup_from_folio(folio);\n \tlruvec = mem_cgroup_lruvec(memcg, pgdat);\n-\tmax_seq = READ_ONCE((lruvec)-\u003elrugen.max_seq);\n-\tgen = lru_gen_from_seq(max_seq);\n \tmm_state = get_mm_state(lruvec);\n \n \tlazy_mmu_mode_enable();\n@@ -4422,7 +4423,7 @@ bool lru_gen_look_around(struct page_vma_mapped_walk *pvmw, unsigned int nr)\n \t\t\tcontinue;\n \n \t\tif (last != folio) {\n-\t\t\twalk_update_folio(walk, vma, last, gen, dirty);\n+\t\t\twalk_update_folio(walk, vma, lruvec, last, dirty);\n \n \t\t\tlast = folio;\n \t\t\tdirty = false;\n@@ -4434,13 +4435,14 @@ bool lru_gen_look_around(struct page_vma_mapped_walk *pvmw, unsigned int nr)\n \t\tyoung += nr;\n \t}\n \n-\twalk_update_folio(walk, vma, last, gen, dirty);\n+\twalk_update_folio(walk, vma, lruvec, last, dirty);\n \n \tlazy_mmu_mode_disable();\n \n \t/* feedback from rmap walkers to page table walkers */\n \tif (mm_state \u0026\u0026 suitable_to_scan(i, young))\n-\t\tupdate_bloom_filter(mm_state, max_seq, pvmw-\u003epmd);\n+\t\tupdate_bloom_filter(mm_state, READ_ONCE(lruvec-\u003elrugen.max_seq),\n+\t\t\t\t    pvmw-\u003epmd);\n \n \tmem_cgroup_put(memcg);\n \n@@ -4785,7 +4787,7 @@ static bool isolate_folio(struct lruvec *lruvec, struct folio *folio, struct sca\n \n \t/* see the comment on LRU_REFS_FLAGS */\n \tif (!folio_test_referenced(folio))\n-\t\tset_mask_bits(\u0026folio-\u003eflags.f, LRU_REFS_MASK, 0);\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@@ -4878,9 +4880,9 @@ static int get_tier_idx(struct lruvec *lruvec, int type)\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 */\n-\tread_ctrl_pos(lruvec, type, 0, 2, \u0026sp);\n-\tfor (tier = 1; tier \u003c MAX_NR_TIERS; tier++) {\n-\t\tread_ctrl_pos(lruvec, type, tier, 3, \u0026pv);\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\tif (!positive_ctrl_err(\u0026sp, \u0026pv))\n \t\t\tbreak;\n \t}\n@@ -4901,8 +4903,10 @@ static int get_type_to_scan(struct lruvec *lruvec, int swappiness)\n \t * Compare the sum of all tiers of anon with that of file to determine\n \t * which type to scan.\n \t */\n-\tread_ctrl_pos(lruvec, LRU_GEN_ANON, MAX_NR_TIERS, swappiness, \u0026sp);\n-\tread_ctrl_pos(lruvec, LRU_GEN_FILE, MAX_NR_TIERS, MAX_SWAPPINESS - swappiness, \u0026pv);\n+\tread_ctrl_pos(lruvec, LRU_GEN_ANON, LRU_TIER_MIN, LRU_TIER_MAX,\n+\t\t      swappiness, \u0026sp);\n+\tread_ctrl_pos(lruvec, LRU_GEN_FILE, LRU_TIER_MIN, LRU_TIER_MAX,\n+\t\t      MAX_SWAPPINESS - swappiness, \u0026pv);\n \n \treturn positive_ctrl_err(\u0026sp, \u0026pv);\n }\n@@ -5017,8 +5021,10 @@ static int evict_folios(unsigned long nr_to_scan, struct lruvec *lruvec,\n \t\t}\n \n \t\t/* don't add rejected folios to the oldest generation */\n-\t\tif (lru_gen_folio_seq(lruvec, folio, false) == min_seq[type])\n-\t\t\tset_mask_bits(\u0026folio-\u003eflags.f, LRU_REFS_FLAGS, BIT(PG_active));\n+\t\tif (lru_gen_folio_seq(lruvec, folio, false) == min_seq[type]) {\n+\t\t\tfolio_set_lru_refs(folio, 0);\n+\t\t\tfolio_set_active(folio);\n+\t\t}\n \t}\n \n \tmove_folios_to_lru(\u0026list);\n"
}

2/1 2026/09/06 16:23 agent patch-evaluator
0m Model:
core

Args:
null

Results:
{
  "EnableConfigs": [
    "LRU_GEN",
    "LRU_GEN_ENABLED",
    "MEMCG",
    "MIGRATION"
  ],
  "FocusSymbols": [
    "lru_gen_look_around",
    "folio_migrate_flags",
    "mem_cgroup_update_lru_size",
    "walk_pte_range",
    "walk_pmd_range_locked",
    "folio_update_gen",
    "evict_folios",
    "isolate_folio",
    "lru_gen_inc_refs",
    "lru_gen_clear_refs"
  ],
  "Reasoning": "The patch touches core memory management code across mm/vmscan.c, mm/folio.c, mm/memcontrol.c, and mm/migrate.c, altering Multi-Gen LRU (MGLRU) reference tracking, folio flag manipulation, tier calculations, page eviction, and memcg LRU zone accounting. It also adds several VM_WARN_ON_ONCE assertions and changes synchronization/arithmetic logic. These paths are fully reachable and worth fuzzing.",
  "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 e3cbbc76e1c966c76fe621299a32d17afa7e4003
Author: syz-cluster <triage@syzkaller.com>
Date:   Sun Sep 6 16:23:49 2026 +0000

    syz-cluster: applied patch under review

diff --git a/include/linux/memcontrol.h b/include/linux/memcontrol.h
index c799926435560..2b67694285d04 100644
--- a/include/linux/memcontrol.h
+++ b/include/linux/memcontrol.h
@@ -100,7 +100,7 @@ struct mem_cgroup_per_node {
 	/* Fields which get updated often at the end. */
 	struct lruvec		lruvec;
 	CACHELINE_PADDING(_pad2_);
-	unsigned long		lru_zone_size[MAX_NR_ZONES][NR_LRU_LISTS];
+	long			lru_zone_size[MAX_NR_ZONES][NR_LRU_LISTS];
 	struct mem_cgroup_reclaim_iter	iter;
 
 	/*
@@ -887,10 +887,15 @@ static inline
 unsigned long mem_cgroup_get_zone_lru_size(struct lruvec *lruvec,
 		enum lru_list lru, int zone_idx)
 {
+	long val;
 	struct mem_cgroup_per_node *mz;
 
 	mz = container_of(lruvec, struct mem_cgroup_per_node, lruvec);
-	return READ_ONCE(mz->lru_zone_size[zone_idx][lru]);
+	val = READ_ONCE(mz->lru_zone_size[zone_idx][lru]);
+	if (WARN_ON_ONCE(val < 0))
+		return 0;
+
+	return val;
 }
 
 void __mem_cgroup_handle_over_high(gfp_t gfp_mask);
diff --git a/include/linux/mm_inline.h b/include/linux/mm_inline.h
index 621c8653d8f7e..ab69b9930893f 100644
--- a/include/linux/mm_inline.h
+++ b/include/linux/mm_inline.h
@@ -30,6 +30,17 @@ static inline int folio_is_file_lru(const struct folio *folio)
 	return !folio_test_swapbacked(folio);
 }
 
+/**
+ * folio_flags_is_file_lru - Should the folio be on a file LRU or anon LRU?
+ * @flags: The folio's flags.
+ *
+ * Just like folio_is_file_lru but take the folio flags directly instead.
+ */
+static inline int folio_flags_is_file_lru(const unsigned long *flags)
+{
+	return !test_bit(PG_swapbacked, flags);
+}
+
 static __always_inline void __update_lru_size(struct lruvec *lruvec,
 				enum lru_list lru, enum zone_type zid,
 				long nr_pages)
@@ -142,10 +153,66 @@ static inline int lru_tier_from_refs(int refs, bool workingset)
 	return workingset ? MAX_NR_TIERS - 1 : order_base_2(refs);
 }
 
-static inline int folio_lru_refs(const struct folio *folio)
+/**
+ * lru_set_gen_flags - Set the LRU generation number to specified folio flags.
+ * @flags: pointer to the folio flags
+ * @gen: generation number, between 0 and (MAX_NR_GENS - 1), inclusive.
+ */
+static inline void lru_set_gen_flags(unsigned long *flags, int gen)
+{
+	BUILD_BUG_ON(LRU_GEN_MASK & LRU_REFS_MASK);
+	VM_WARN_ON_ONCE(gen >= MAX_NR_GENS || gen < 0);
+	/* Store gen offset by 1, zero means the folio is off-list. */
+	*flags &= ~LRU_GEN_MASK;
+	*flags |= (gen + 1UL) << LRU_GEN_PGOFF;
+}
+
+/**
+ * lru_get_gen_flags - Return the LRU generation number from folio flags.
+ * @flags: folio flags
+ *
+ * Returns: A number between 0 and (MAX_NR_GENS - 1), inclusive. Returns
+ * -1 if the flags indicate the folio is off the list (e.g., isolated).
+ */
+static inline int lru_get_gen_flags(unsigned long flags)
+{
+	int gen = ((flags & LRU_GEN_MASK) >> LRU_GEN_PGOFF) - 1;
+
+	/* Exclude the legal -1 from the unsigned MAX_NR_GENS comparison */
+	VM_WARN_ON_ONCE(gen != -1 && gen >= MAX_NR_GENS);
+	return gen;
+}
+
+/**
+ * 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.
+ */
+static inline void lru_set_refs_flags(unsigned long *flags, unsigned int refs)
 {
-	unsigned long flags = READ_ONCE(folio->flags.f);
+	VM_WARN_ON_ONCE(refs > LRU_REFS_MAX);
+	BUILD_BUG_ON(LRU_REFS_MAX != (LRU_REFS_MASK >> LRU_REFS_PGOFF) + 1);
 
+	*flags &= ~LRU_REFS_FLAGS;
+	if (!refs)
+		return;
+	*flags |= (BIT(PG_referenced) | ((refs - 1UL) << LRU_REFS_PGOFF));
+}
+
+/**
+ * lru_get_refs_flags - Return LRU referenced / access count from folio flags.
+ * @flags: folio flags
+ *
+ * Reads the LRU referenced count set by lru_set_refs_flags().
+ */
+static inline int lru_get_refs_flags(unsigned long flags)
+{
 	if (!(flags & BIT(PG_referenced)))
 		return 0;
 	/*
@@ -155,11 +222,24 @@ static inline int folio_lru_refs(const struct folio *folio)
 	return ((flags & LRU_REFS_MASK) >> LRU_REFS_PGOFF) + 1;
 }
 
-static inline int folio_lru_gen(const struct folio *folio)
+static inline int folio_lru_refs(const struct folio *folio)
 {
-	unsigned long flags = READ_ONCE(folio->flags.f);
+	return lru_get_refs_flags(READ_ONCE(*const_folio_flags(folio, 0)));
+}
 
-	return ((flags & LRU_GEN_MASK) >> LRU_GEN_PGOFF) - 1;
+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;
+		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)
+{
+	return lru_get_gen_flags(READ_ONCE(*const_folio_flags(folio, 0)));
 }
 
 static inline bool lru_gen_is_active(const struct lruvec *lruvec, int gen)
@@ -270,7 +350,7 @@ 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.f, LRU_GEN_MASK | BIT(PG_active), flags);
+	set_mask_bits(folio_flags(folio, 0), LRU_GEN_MASK | BIT(PG_active), flags);
 
 	lru_gen_update_size(lruvec, folio, -1, gen);
 	/* for folio_rotate_reclaimable() */
@@ -295,7 +375,7 @@ static inline bool lru_gen_del_folio(struct lruvec *lruvec, struct folio *folio,
 
 	/* for folio_migrate_flags() */
 	flags = !reclaiming && lru_gen_is_active(lruvec, gen) ? BIT(PG_active) : 0;
-	flags = set_mask_bits(&folio->flags.f, LRU_GEN_MASK, flags);
+	flags = set_mask_bits(folio_flags(folio, 0), LRU_GEN_MASK, flags);
 	gen = ((flags & LRU_GEN_MASK) >> LRU_GEN_PGOFF) - 1;
 
 	lru_gen_update_size(lruvec, folio, gen, -1);
@@ -304,11 +384,19 @@ static inline bool lru_gen_del_folio(struct lruvec *lruvec, struct folio *folio,
 	return true;
 }
 
-static inline void folio_migrate_refs(struct folio *new, const struct folio *old)
+/**
+ * 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)
 {
-	unsigned long refs = READ_ONCE(old->flags.f) & LRU_REFS_MASK;
-
-	set_mask_bits(&new->flags.f, LRU_REFS_MASK, refs);
+	BUILD_BUG_ON(LRU_REFS_MASK & BIT(PG_referenced));
+	folio_set_lru_refs(new, folio_lru_refs(old));
 }
 #else /* !CONFIG_LRU_GEN */
 
@@ -337,9 +425,10 @@ static inline bool lru_gen_del_folio(struct lruvec *lruvec, struct folio *folio,
 	return false;
 }
 
-static inline void folio_migrate_refs(struct folio *new, const struct folio *old)
+static inline void folio_migrate_lru_refs(struct folio *new, const struct folio *old)
 {
-
+	if (folio_test_referenced(old))
+		folio_set_referenced(new);
 }
 #endif /* CONFIG_LRU_GEN */
 
diff --git a/include/linux/mmzone.h b/include/linux/mmzone.h
index 84e237f2c17db..c9ae7991a8b20 100644
--- a/include/linux/mmzone.h
+++ b/include/linux/mmzone.h
@@ -495,11 +495,14 @@ enum lruvec_flags {
  * folio->flags, masked by LRU_REFS_MASK.
  */
 #define MAX_NR_TIERS		4U
+#define LRU_TIER_MIN		0U
+#define LRU_TIER_MAX		(MAX_NR_TIERS - 1)
 
 #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,
diff --git a/mm/folio.c b/mm/folio.c
index 50a6dbe55998e..47a437e0f7fde 100644
--- a/mm/folio.c
+++ b/mm/folio.c
@@ -354,26 +354,28 @@ static void __lru_cache_activate_folio(struct folio *folio)
 
 static void lru_gen_inc_refs(struct folio *folio)
 {
-	unsigned long new_flags, old_flags = READ_ONCE(folio->flags.f);
+	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_test_referenced(folio)) {
-		set_mask_bits(&folio->flags.f, LRU_REFS_MASK, BIT(PG_referenced));
+	if (!folio_lru_refs(folio)) {
+		folio_set_lru_refs(folio, 1);
 		return;
 	}
 
 	do {
-		if ((old_flags & LRU_REFS_MASK) == LRU_REFS_MASK) {
+		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;
 		}
-
-		new_flags = old_flags + BIT(LRU_REFS_PGOFF);
-	} while (!try_cmpxchg(&folio->flags.f, &old_flags, new_flags));
+		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)
@@ -385,7 +387,8 @@ static bool lru_gen_clear_refs(struct folio *folio)
 	if (gen < 0)
 		return true;
 
-	set_mask_bits(&folio->flags.f, LRU_REFS_FLAGS | BIT(PG_workingset), 0);
+	folio_set_lru_refs(folio, 0);
+	folio_clear_workingset(folio);
 
 	rcu_read_lock();
 	seq = READ_ONCE(folio_lruvec(folio)->lrugen.min_seq[type]);
diff --git a/mm/memcontrol.c b/mm/memcontrol.c
index bd1e7e1544265..f17b2b2d61055 100644
--- a/mm/memcontrol.c
+++ b/mm/memcontrol.c
@@ -1529,28 +1529,12 @@ void mem_cgroup_update_lru_size(struct lruvec *lruvec, enum lru_list lru,
 				int zid, long nr_pages)
 {
 	struct mem_cgroup_per_node *mz;
-	unsigned long *lru_size;
-	long size;
 
 	if (mem_cgroup_disabled())
 		return;
 
 	mz = container_of(lruvec, struct mem_cgroup_per_node, lruvec);
-	lru_size = &mz->lru_zone_size[zid][lru];
-
-	if (nr_pages < 0)
-		*lru_size += nr_pages;
-
-	size = *lru_size;
-	if (WARN_ONCE(size < 0,
-		"%s(%p, %d, %ld): lru_size %ld\n",
-		__func__, lruvec, lru, nr_pages, size)) {
-		VM_BUG_ON(1);
-		*lru_size = 0;
-	}
-
-	if (nr_pages > 0)
-		*lru_size += nr_pages;
+	mz->lru_zone_size[zid][lru] += nr_pages;
 }
 
 /**
diff --git a/mm/migrate.c b/mm/migrate.c
index 15b45832bcfa7..a369d0c95c386 100644
--- a/mm/migrate.c
+++ b/mm/migrate.c
@@ -776,8 +776,9 @@ void folio_migrate_flags(struct folio *newfolio, struct folio *folio)
 {
 	int cpupid;
 
-	if (folio_test_referenced(folio))
-		folio_set_referenced(newfolio);
+	/* Copy the reference state, including PG_referenced */
+	folio_migrate_lru_refs(newfolio, folio);
+
 	if (folio_test_uptodate(folio))
 		folio_mark_uptodate(newfolio);
 	if (folio_test_clear_active(folio)) {
@@ -807,7 +808,6 @@ void folio_migrate_flags(struct folio *newfolio, struct folio *folio)
 	if (folio_test_idle(folio))
 		folio_set_idle(newfolio);
 
-	folio_migrate_refs(newfolio, folio);
 	/*
 	 * Copy NUMA information to the new page, to prevent over-eager
 	 * future migrations of this same page.
diff --git a/mm/vmscan.c b/mm/vmscan.c
index 245f68c75b289..40d3f1b48a74c 100644
--- a/mm/vmscan.c
+++ b/mm/vmscan.c
@@ -863,19 +863,22 @@ static bool lru_gen_set_refs(struct folio *folio, const vma_flags_t *vma_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)) {
-			set_mask_bits(&folio->flags.f, LRU_REFS_FLAGS, BIT(PG_workingset));
+			folio_set_workingset(folio);
+			folio_set_lru_refs(folio, 0);
 			return true;
 		}
 
-		set_mask_bits(&folio->flags.f, LRU_REFS_MASK, BIT(PG_referenced));
+		folio_set_lru_refs(folio, 1);
 		return false;
 	}
 
 	/* Promote on second access */
-	if (folio_lru_refs(folio) > 1)
-		set_mask_bits(&folio->flags.f, LRU_REFS_FLAGS, BIT(PG_workingset));
-	else
+	if (folio_lru_refs(folio) > 1) {
+		folio_set_workingset(folio);
+		folio_set_lru_refs(folio, 0);
+	} else {
 		folio_mark_accessed(folio);
+	}
 	return true;
 }
 #else
@@ -3220,8 +3223,8 @@ struct ctrl_pos {
 	int gain;
 };
 
-static void read_ctrl_pos(struct lruvec *lruvec, int type, int tier, int gain,
-			  struct ctrl_pos *pos)
+static void read_ctrl_pos(struct lruvec *lruvec, int type, int tier_min,
+			  int tier_max, int gain, struct ctrl_pos *pos)
 {
 	int i;
 	struct lru_gen_folio *lrugen = &lruvec->lrugen;
@@ -3230,7 +3233,7 @@ static void read_ctrl_pos(struct lruvec *lruvec, int type, int tier, int gain,
 	pos->gain = gain;
 	pos->refaulted = pos->total = 0;
 
-	for (i = tier % MAX_NR_TIERS; i <= min(tier, MAX_NR_TIERS - 1); i++) {
+	for (i = tier_min; i <= tier_max; i++) {
 		pos->refaulted += lrugen->avg_refaulted[type][i] +
 				  atomic_long_read(&lrugen->refaulted[hist][type][i]);
 		pos->total += lrugen->avg_total[type][i] +
@@ -3291,11 +3294,11 @@ static bool positive_ctrl_err(struct ctrl_pos *sp, struct ctrl_pos *pv)
  ******************************************************************************/
 
 /* promote pages accessed through page tables */
-static int folio_update_gen(struct folio *folio, int gen, const vma_flags_t *vma_flags)
+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.f);
-
-	VM_WARN_ON_ONCE(gen >= MAX_NR_GENS);
+	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
@@ -3304,31 +3307,35 @@ static int folio_update_gen(struct folio *folio, int gen, const vma_flags_t *vma
 	 */
 	if (!folio_test_referenced(folio) && !folio_test_workingset(folio) &&
 	    !is_exec_file_folio(folio, vma_flags)) {
-		set_mask_bits(&folio->flags.f, LRU_REFS_MASK, BIT(PG_referenced));
+		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_flags & LRU_GEN_MASK))
-			return -1;
+		if (old_gen < 0)
+			break;
 
-		new_flags = old_flags & ~(LRU_GEN_MASK | LRU_REFS_FLAGS);
-		new_flags |= ((gen + 1UL) << LRU_GEN_PGOFF) | BIT(PG_workingset);
-	} while (!try_cmpxchg(&folio->flags.f, &old_flags, new_flags));
+		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));
 
-	return ((old_flags & LRU_GEN_MASK) >> LRU_GEN_PGOFF) - 1;
+	*type = folio_flags_is_file_lru(&old_flags);
+	return old_gen;
 }
 
 static int __folio_inc_gen(struct folio *folio, int old_gen, bool *increased)
 {
-	unsigned long new_flags, old_flags = READ_ONCE(folio->flags.f);
+	unsigned long new_flags, old_flags = READ_ONCE(*folio_flags(folio, 0));
 	int new_gen;
 
-	VM_WARN_ON_ONCE_FOLIO(!(old_flags & LRU_GEN_MASK), folio);
-
 	do {
-		new_gen = ((old_flags & LRU_GEN_MASK) >> LRU_GEN_PGOFF) - 1;
+		new_gen = lru_get_gen_flags(old_flags);
+
 		/* folio_update_gen() has promoted this page? */
 		if (new_gen >= 0 && new_gen != old_gen) {
 			if (increased)
@@ -3336,11 +3343,12 @@ static int __folio_inc_gen(struct folio *folio, int old_gen, bool *increased)
 			return new_gen;
 		}
 
+		new_flags = old_flags;
 		new_gen = (old_gen + 1) % MAX_NR_GENS;
 
-		new_flags = old_flags & ~(LRU_GEN_MASK | LRU_REFS_FLAGS);
-		new_flags |= (new_gen + 1UL) << LRU_GEN_PGOFF;
-	} while (!try_cmpxchg(&folio->flags.f, &old_flags, new_flags));
+		lru_set_gen_flags(&new_flags, new_gen);
+		lru_set_refs_flags(&new_flags, 0);
+	} while (!try_cmpxchg(folio_flags(folio, 0), &old_flags, new_flags));
 
 	if (increased)
 		*increased = true;
@@ -3362,9 +3370,8 @@ static int folio_inc_gen(struct lruvec *lruvec, struct folio *folio)
 }
 
 static void update_batch_size(struct lru_gen_mm_walk *walk, struct folio *folio,
-			      int old_gen, int new_gen)
+			      int old_gen, int new_gen, int type)
 {
-	int type = folio_is_file_lru(folio);
 	int zone = folio_zonenum(folio);
 	int delta = folio_nr_pages(folio);
 
@@ -3551,22 +3558,24 @@ 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 folio *folio, int new_gen, bool dirty)
+		struct lruvec *lruvec, struct folio *folio, bool dirty)
 {
-	int old_gen;
+	int new_gen, old_gen, type;
 
 	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, &vma->flags);
+		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);
+			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)
@@ -3588,8 +3597,6 @@ static bool walk_pte_range(pmd_t *pmd, unsigned long start, unsigned long end,
 	struct lru_gen_mm_walk *walk = args->private;
 	struct mem_cgroup *memcg = lruvec_memcg(walk->lruvec);
 	struct pglist_data *pgdat = lruvec_pgdat(walk->lruvec);
-	DEFINE_MAX_SEQ(walk->lruvec);
-	int gen = lru_gen_from_seq(max_seq);
 	unsigned int nr;
 	pmd_t pmdval;
 
@@ -3640,7 +3647,7 @@ static bool walk_pte_range(pmd_t *pmd, unsigned long start, unsigned long end,
 			continue;
 
 		if (last != folio) {
-			walk_update_folio(walk, args->vma, last, gen, dirty);
+			walk_update_folio(walk, args->vma, walk->lruvec, last, dirty);
 
 			last = folio;
 			dirty = false;
@@ -3653,7 +3660,7 @@ static bool walk_pte_range(pmd_t *pmd, unsigned long start, unsigned long end,
 		walk->mm_stats[MM_LEAF_YOUNG] += nr;
 	}
 
-	walk_update_folio(walk, args->vma, last, gen, dirty);
+	walk_update_folio(walk, args->vma, walk->lruvec, last, dirty);
 	last = NULL;
 
 	if (i < PTRS_PER_PTE && get_next_vma(PMD_MASK, PAGE_SIZE, args, &start, &end))
@@ -3676,8 +3683,6 @@ static void walk_pmd_range_locked(pud_t *pud, unsigned long addr, struct vm_area
 	struct lru_gen_mm_walk *walk = args->private;
 	struct mem_cgroup *memcg = lruvec_memcg(walk->lruvec);
 	struct pglist_data *pgdat = lruvec_pgdat(walk->lruvec);
-	DEFINE_MAX_SEQ(walk->lruvec);
-	int gen = lru_gen_from_seq(max_seq);
 
 	VM_WARN_ON_ONCE(pud_leaf(*pud));
 
@@ -3731,7 +3736,7 @@ static void walk_pmd_range_locked(pud_t *pud, unsigned long addr, struct vm_area
 			goto next;
 
 		if (last != folio) {
-			walk_update_folio(walk, vma, last, gen, dirty);
+			walk_update_folio(walk, vma, walk->lruvec, last, dirty);
 
 			last = folio;
 			dirty = false;
@@ -3745,7 +3750,7 @@ static void walk_pmd_range_locked(pud_t *pud, unsigned long addr, struct vm_area
 		i = i > MIN_LRU_BATCH ? 0 : find_next_bit(bitmap, MIN_LRU_BATCH, i) + 1;
 	} while (i <= MIN_LRU_BATCH);
 
-	walk_update_folio(walk, vma, last, gen, dirty);
+	walk_update_folio(walk, vma, walk->lruvec, last, dirty);
 
 	lazy_mmu_mode_disable();
 	spin_unlock(ptl);
@@ -4351,8 +4356,6 @@ bool lru_gen_look_around(struct page_vma_mapped_walk *pvmw, unsigned int nr)
 	struct pglist_data *pgdat = folio_pgdat(folio);
 	struct lruvec *lruvec;
 	struct lru_gen_mm_state *mm_state;
-	unsigned long max_seq;
-	int gen;
 
 	lockdep_assert_held(pvmw->ptl);
 	VM_WARN_ON_ONCE_FOLIO(folio_test_lru(folio), folio);
@@ -4389,8 +4392,6 @@ bool lru_gen_look_around(struct page_vma_mapped_walk *pvmw, unsigned int nr)
 
 	memcg = get_mem_cgroup_from_folio(folio);
 	lruvec = mem_cgroup_lruvec(memcg, pgdat);
-	max_seq = READ_ONCE((lruvec)->lrugen.max_seq);
-	gen = lru_gen_from_seq(max_seq);
 	mm_state = get_mm_state(lruvec);
 
 	lazy_mmu_mode_enable();
@@ -4422,7 +4423,7 @@ bool lru_gen_look_around(struct page_vma_mapped_walk *pvmw, unsigned int nr)
 			continue;
 
 		if (last != folio) {
-			walk_update_folio(walk, vma, last, gen, dirty);
+			walk_update_folio(walk, vma, lruvec, last, dirty);
 
 			last = folio;
 			dirty = false;
@@ -4434,13 +4435,14 @@ bool lru_gen_look_around(struct page_vma_mapped_walk *pvmw, unsigned int nr)
 		young += nr;
 	}
 
-	walk_update_folio(walk, vma, last, gen, dirty);
+	walk_update_folio(walk, vma, lruvec, last, dirty);
 
 	lazy_mmu_mode_disable();
 
 	/* feedback from rmap walkers to page table walkers */
 	if (mm_state && suitable_to_scan(i, young))
-		update_bloom_filter(mm_state, max_seq, pvmw->pmd);
+		update_bloom_filter(mm_state, READ_ONCE(lruvec->lrugen.max_seq),
+				    pvmw->pmd);
 
 	mem_cgroup_put(memcg);
 
@@ -4785,7 +4787,7 @@ static bool isolate_folio(struct lruvec *lruvec, struct folio *folio, struct sca
 
 	/* see the comment on LRU_REFS_FLAGS */
 	if (!folio_test_referenced(folio))
-		set_mask_bits(&folio->flags.f, LRU_REFS_MASK, 0);
+		folio_set_lru_refs(folio, 0);
 
 	success = lru_gen_del_folio(lruvec, folio, true);
 	VM_WARN_ON_ONCE_FOLIO(!success, folio);
@@ -4878,9 +4880,9 @@ static int get_tier_idx(struct lruvec *lruvec, int type)
 	 * This value is chosen because any other tier would have at least twice
 	 * as many refaults as the first tier.
 	 */
-	read_ctrl_pos(lruvec, type, 0, 2, &sp);
-	for (tier = 1; tier < MAX_NR_TIERS; tier++) {
-		read_ctrl_pos(lruvec, type, tier, 3, &pv);
+	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);
 		if (!positive_ctrl_err(&sp, &pv))
 			break;
 	}
@@ -4901,8 +4903,10 @@ static int get_type_to_scan(struct lruvec *lruvec, int swappiness)
 	 * Compare the sum of all tiers of anon with that of file to determine
 	 * which type to scan.
 	 */
-	read_ctrl_pos(lruvec, LRU_GEN_ANON, MAX_NR_TIERS, swappiness, &sp);
-	read_ctrl_pos(lruvec, LRU_GEN_FILE, MAX_NR_TIERS, MAX_SWAPPINESS - swappiness, &pv);
+	read_ctrl_pos(lruvec, LRU_GEN_ANON, LRU_TIER_MIN, LRU_TIER_MAX,
+		      swappiness, &sp);
+	read_ctrl_pos(lruvec, LRU_GEN_FILE, LRU_TIER_MIN, LRU_TIER_MAX,
+		      MAX_SWAPPINESS - swappiness, &pv);
 
 	return positive_ctrl_err(&sp, &pv);
 }
@@ -5017,8 +5021,10 @@ static int evict_folios(unsigned long nr_to_scan, struct lruvec *lruvec,
 		}
 
 		/* don't add rejected folios to the oldest generation */
-		if (lru_gen_folio_seq(lruvec, folio, false) == min_seq[type])
-			set_mask_bits(&folio->flags.f, LRU_REFS_FLAGS, BIT(PG_active));
+		if (lru_gen_folio_seq(lruvec, folio, false) == min_seq[type]) {
+			folio_set_lru_refs(folio, 0);
+			folio_set_active(folio);
+		}
 	}
 
 	move_folios_to_lru(&list);

3/2 2026/09/06 16:23 llm patch-evaluator
0m Model:
gemini-3.7-flash

Args:
null

Results:
null

Tokens:
							input: 12374
							output: 27
							thoughts: 601
						

Thoughts:
**My Initial Assessment of the Patch's Impact**

Okay, so this patch is digging deep into the kernel's memory management, specifically within the `mm/` directory – that's a key area. It looks like it's touching critical files: `memcontrol.h`, `mm_inline.h`, `mmzone.h`, `folio.c`, `memcontrol.c`, `migrate.c`, and `vmscan.c`.  A comprehensive overview is definitely required.

First, it seems there's a type change for `lru_zone_size` from `unsigned long` to `long`, and there are checks now for negative values. That's immediately interesting – potential signedness bugs, or maybe addressing a subtle overflow issue in the memcg LRU accounting. Always worth scrutinizing those changes!

The second, and potentially more significant, part involves a major refactoring and bug fixing related to Multi-Gen LRU (MGLRU). I see a swarm of function names: `folio_flags_is_file_lru`, `lru_set_gen_flags`, `folio_migrate_lru_refs`, and many others that are related to manipulating the LRU flags and generations. That's a good amount of work being done on fundamental LRU logic. They're changing the way reference counts and generation counts are handled. This touches on page isolation, tiering, and eviction. That's all core stuff. The migrate code's `folio_migrate_flags` makes sense, as well.

Given that this modifies files in `mm/`, it's directly in the core memory management subsystem. This is a highly reachable area, and syzkaller probably hits this all the time. So, if we look at the positive criteria, this patch definitely fits the bill. It's modifying memory management logic (mm/), and it's refactoring reachable data structures (LRU flags, etc.). The introduction of `WARN_ON_ONCE` indicates runtime assertions – a big check! This is definitely "WorthFuzzing"!

Now, I'm going to quickly check Kconfig dependencies to see which features are needed to enable this and use `grepper` to search for LRU_GEN, MEMCG, and see if there are any specific FocusSymbols related to the specific changes. That will tell me which features are impacted, and where to focus the fuzzing effort.



4/2 2026/09/06 16:23 tool grepper
0m Args:
{
  "Expression": "config LRU_GEN",
  "PathPrefix": "mm/"
}

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/09/06 16:23 llm patch-evaluator
0m Model:
gemini-3.7-flash

Args:
null

Results:
null

Tokens:
							input: 13751
							output: 25
							thoughts: 26
						

Thoughts:
Okay, here's my take on those thoughts, framed as an expert's internal monologue:

**Checking `MEMCG` and Memory Control**

Right, let's circle back to `MEMCG` – memory control groups. Always a good place to start when digging into memory behavior. I need to make sure I'm covering all the angles. My initial focus will be on the core implementation. I should check `memcontrol.c` for the fundamental framework. Specifically, I'm thinking about how the system tracks and enforces memory limits within these control groups.

That also brings `lru_zone_size` into the picture. Need to correlate that with how the kernel manages pages within zones and how `MEMCG` interacts with those zone-level calculations. The LRU is critical in determining what gets swapped out, and what pages are considered "in-use" versus reclaimable. I should be able to trace how limits affect the LRU decisions. Let's see how memory pressure impacts the zones and the LRU calculations within them.



6/2 2026/09/06 16:24 tool grepper
0m Args:
{
  "Expression": "config MEMCG",
  "PathPrefix": "init/"
}

Results:
{
  "Output": "init/Kconfig=1075=config CGROUP_FAVOR_DYNMODS\n--\ninit/Kconfig-1084-\ninit/Kconfig:1085:config MEMCG\ninit/Kconfig-1086-\tbool \"Memory controller\"\n--\ninit/Kconfig-1093-\ninit/Kconfig:1094:config MEMCG_NMI_UNSAFE\ninit/Kconfig-1095-\tbool\n--\ninit/Kconfig-1100-\ninit/Kconfig:1101:config MEMCG_NMI_SAFETY_REQUIRES_ATOMIC\ninit/Kconfig-1102-\tbool\n--\ninit/Kconfig-1107-\ninit/Kconfig:1108:config MEMCG_V1\ninit/Kconfig-1109-\tbool \"Legacy cgroup v1 memory controller\"\n"
}

7/2 2026/09/06 16:24 llm patch-evaluator
0m Model:
gemini-3.7-flash

Args:
null

Results:
null

Tokens:
							input: 14249
							output: 32
							thoughts: 336
						

Thoughts:
**Analyzing Recent Changes in Memory Management Code**

Okay, let's dive into these recent modifications. First, I need to get a handle on what's been touched in the `mm/` directory. Scanning `mm/memcontrol.c`, I see `mem_cgroup_update_lru_size` and `mem_cgroup_get_zone_lru_size` – the latter's inline, so that's a quick note to myself. Then, over in `mm/folio.c`, I see a couple of functions flagged as `static`: `lru_gen_inc_refs` and `lru_gen_clear_refs`. Since they're `static`, I should check within `folio.c` to understand where they are used.

Moving on, `mm/migrate.c` has `folio_migrate_flags`, which warrants a closer look later. And the big one, `mm/vmscan.c`, has a whole slew of changes. I note `lru_gen_set_refs`, `read_ctrl_pos`, `folio_update_gen`, `__folio_inc_gen`, `folio_inc_gen`, `update_batch_size`, `walk_update_folio`, `walk_pte_range`, `walk_pmd_range_locked`, `lru_gen_look_around`, `isolate_folio`, `get_tier_idx`, `get_type_to_scan`, and `evict_folios`. That's a lot to parse, but it seems VM scanning has been significantly impacted.

My next step is to figure out the scope of these changes, focusing especially on the functions in `mm/folio.c` like `lru_gen_inc_refs` and `lru_gen_clear_refs`. Are they internal to `folio.c`, or are they part of a larger interface? I'll quickly check the callers of those, or other functions in `mm/folio.c`, to see what the targets are, and determine what the right code to target for my purposes is. This will give me a better understanding of the overall impact and where to dig deeper.



8/2 2026/09/06 16:24 tool grepper
0m Args:
{
  "Expression": "lru_gen_(inc|clear)_refs",
  "PathPrefix": "mm/"
}

Results:
{
  "Output": "mm/folio.c=323=static void __lru_cache_activate_folio(struct folio *folio)\n--\nmm/folio.c-354-\nmm/folio.c:355:static void lru_gen_inc_refs(struct folio *folio)\nmm/folio.c-356-{\n--\nmm/folio.c-380-\nmm/folio.c:381:static bool lru_gen_clear_refs(struct folio *folio)\nmm/folio.c-382-{\n--\nmm/folio.c-401-\nmm/folio.c:402:static void lru_gen_inc_refs(struct folio *folio)\nmm/folio.c-403-{\n--\nmm/folio.c-405-\nmm/folio.c:406:static bool lru_gen_clear_refs(struct folio *folio)\nmm/folio.c-407-{\n--\nmm/folio.c=426=void folio_mark_accessed(struct folio *folio)\n--\nmm/folio.c-430-\tif (lru_gen_enabled()) {\nmm/folio.c:431:\t\tlru_gen_inc_refs(folio);\nmm/folio.c-432-\t\treturn;\n--\nmm/folio.c=592=static void lru_lazyfree(struct lruvec *lruvec, struct folio *folio)\n--\nmm/folio.c-602-\tif (lru_gen_enabled())\nmm/folio.c:603:\t\tlru_gen_clear_refs(folio);\nmm/folio.c-604-\telse\n--\nmm/folio.c=670=void deactivate_file_folio(struct folio *folio)\n--\nmm/folio.c-675-\nmm/folio.c:676:\tif (lru_gen_enabled() \u0026\u0026 lru_gen_clear_refs(folio))\nmm/folio.c-677-\t\treturn;\n--\nmm/folio.c=690=void folio_deactivate(struct folio *folio)\n--\nmm/folio.c-694-\nmm/folio.c:695:\tif (lru_gen_enabled() ? lru_gen_clear_refs(folio) : !folio_test_active(folio))\nmm/folio.c-696-\t\treturn;\n"
}

9/2 2026/09/06 16:24 llm patch-evaluator
0m Model:
gemini-3.7-flash

Args:
null

Results:
null

Tokens:
							input: 15686
							output: 30
							thoughts: 194
						

Thoughts:
**My Initial Examination of the Relevant Code**

Alright, let's dive in. I need to get a handle on the key functions involved in this memory management flow. My initial focus will be on the following: `isolate_folio`, `evict_folios`, `walk_pte_range`, `walk_pmd_range_locked`, `lru_gen_look_around`, `mem_cgroup_update_lru_size`, `folio_migrate_flags`, `lru_gen_inc_refs`, `lru_gen_clear_refs`, `get_tier_idx`, `get_type_to_scan`, `read_ctrl_pos`, `folio_update_gen`, and `__folio_inc_gen`. These seem like the core components.

Now, I want to quickly identify potential areas for deeper investigation, perhaps looking at whether those functions are declared as `static` or, if not, which of them have good potential as `FocusSymbols` that might make sense to trace. In particular, I’m interested in whether `isolate_folio`, `evict_folios`, `folio_migrate_flags`, or `mem_cgroup_update_lru_size` are non-static and, if so, what their call trees look like.

As a starting point, I'll drill down specifically into `evict_folios` and its callers, starting in `mm/vmscan.c`. That should give me some context about how it's used within the scanning logic.



10/2 2026/09/06 16:24 tool grepper
0m Args:
{
  "Expression": "evict_folios",
  "PathPrefix": "mm/vmscan.c"
}

Results:
{
  "Output": "mm/vmscan.c=4920=static int isolate_folios(unsigned long nr_to_scan, struct lruvec *lruvec,\n--\nmm/vmscan.c-4963-\nmm/vmscan.c:4964:static int evict_folios(unsigned long nr_to_scan, struct lruvec *lruvec,\nmm/vmscan.c-4965-\t\t\tstruct scan_control *sc, int swappiness)\n--\nmm/vmscan.c=5143=static bool try_to_shrink_lruvec(struct lruvec *lruvec, struct scan_control *sc)\n--\nmm/vmscan.c-5166-\t\tnr_batch = min(nr_to_scan, MIN_LRU_BATCH);\nmm/vmscan.c:5167:\t\tdelta = evict_folios(nr_batch, lruvec, sc, swappiness);\nmm/vmscan.c-5168-\t\tif (!delta)\n--\nmm/vmscan.c=5784=static int run_eviction(struct lruvec *lruvec, unsigned long seq, struct scan_control *sc,\n--\nmm/vmscan.c-5804-\t\tnr_batch = min(nr_to_reclaim - sc-\u003enr_reclaimed, MAX_LRU_BATCH);\nmm/vmscan.c:5805:\t\tif (!evict_folios(nr_batch, lruvec, sc, swappiness))\nmm/vmscan.c-5806-\t\t\treturn 0;\n"
}

11/2 2026/09/06 16:24 llm patch-evaluator
0m Model:
gemini-3.7-flash

Args:
null

Results:
null

Tokens:
							input: 16630
							output: 33
							thoughts: 872
						

Thoughts:
**Focusing on Key Symbols for Analysis**

Okay, let's break this down. My goal here is to identify and zero in on the most relevant functions for whatever investigation or modification I'm planning, likely related to the Multi-Gen LRU or memory management in general. I'll start by systematically checking specific functions that are non-static and seem central to the core logic.

First, I'm checking `lru_gen_look_around` in `mm/vmscan.c`. It's non-static, which is good for my purposes. Then I'll look at `folio_migrate_flags` in `mm/migrate.c`, and `mem_cgroup_update_lru_size` in `mm/memcontrol.c`. I'm also going to examine some related functions, including `lru_gen_inc_refs`, `lru_gen_clear_refs`, `folio_update_gen`, `__folio_inc_gen`, `isolate_folio`, `get_tier_idx`, `get_type_to_scan`, `walk_update_folio`, `walk_pte_range`, `walk_pmd_range_locked`, and `read_ctrl_pos`.

Now, I'm going to look at the surrounding code. Which other non-static functions in `mm/vmscan.c` call or are called by these functions, or are otherwise modified in relation to them? I need to build a broader understanding of the context. I'll focus on these:
*   `lru_gen_look_around`
*   `walk_pmd_range_locked`
*   `walk_pte_range`
*   `folio_update_gen`
*   `folio_migrate_flags`
*   `mem_cgroup_update_lru_size`
*   `evict_folios`
*   `isolate_folio`
*   `lru_gen_inc_refs`
*   `lru_gen_clear_refs`

Let's think about ubiquity. I want to avoid functions that are called everywhere; I need symbols that are more specific to the area I'm interested in. I should check whether `folio_mark_accessed` or `folio_deactivate` are called everywhere. `folio_mark_accessed` is, but maybe `lru_gen_inc_refs`, `lru_gen_clear_refs` or `folio_deactivate` / `deactivate_file_folio` are specific. Perhaps `lru_gen_inc_refs` is specific to MGLRU. Hold on, wasn't that static?

*Ah*, yes, `lru_gen_inc_refs` is static, which can complicate symbol analysis. Can `kallsyms` help us find static functions? Right. Static functions in `.c` files *usually* appear in `/proc/kallsyms` unless they're inlined. And I remember the note about inlined static functions in headers. They're basically invisible as symbols. So, I need to focus on non-static, or subsystem-level entrypoint functions. That’s safer.

Therefore, for the sake of clear analysis, these are the "good" `FocusSymbols`:

1.  `lru_gen_look_around`
2.  `folio_migrate_flags`
3.  `mem_cgroup_update_lru_size`
4.  `folio_update_gen`
5.  `walk_pmd_range_locked`
6.  `walk_pte_range`
7.  `evict_folios`
8.  `isolate_folio`
9.  `lru_gen_inc_refs`
10. `lru_gen_clear_refs`

Finally, let's identify the needed configurations. `LRU_GEN` is critical for Multi-Gen LRU, and `MEMCG` is required because of the changes related to memory cgroup functions (`mem_cgroup_update_lru_size` and `lru_zone_size`). Now I need to explore if there are any other configuration dependencies. `LRU_GEN_ENABLED` enables `LRU_GEN` by default at boot. Let's check `Kconfig` for `LRU_GEN` and proceed.



12/2 2026/09/06 16:24 tool read-file
0m Args:
{
  "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"
}

13/2 2026/09/06 16:24 llm patch-evaluator
0m Model:
gemini-3.7-flash

Args:
null

Results:
null

Tokens:
							input: 19053
							output: 26
							thoughts: 13
						

Thoughts:
Here's my attempt at summarizing the thought process, tailored for an expert audience:

**Checking for Migration Configuration**

Okay, so the first thing I need to do is verify the existence of the `MIGRATION` configuration. This is standard operating procedure, especially when dealing with any type of automated or scripted deployment, update, or data transformation. I can't proceed with any migration-related task until I confirm that the necessary configuration is present and accessible. This probably means verifying the presence of a specific file, environment variable, database entry, or other means that defines the migration process parameters. Without knowing the precise system, I have to assume a few possibilities such as a migration file, or a specific database entry. Now, how do I actually check? Do I have a pre-existing function? Library? Is it a manual check? I am going to assume some kind of system already exists, and I need to check for the system.



14/2 2026/09/06 16:24 tool grepper
0m Args:
{
  "Expression": "config MIGRATION",
  "PathPrefix": "mm/"
}

Results:
{
  "Output": "mm/Kconfig=668=config NUMA_MIGRATION\n--\nmm/Kconfig-678-\nmm/Kconfig:679:config MIGRATION\nmm/Kconfig-680-\tbool\n"
}

15/2 2026/09/06 16:24 llm patch-evaluator
0m Model:
gemini-3.7-flash

Args:
null

Results:
null

Tokens:
							input: 19353
							output: 32
							thoughts: 16
						

Thoughts:
Here's my take on those thoughts:

**Checking the Migration Configuration in mm/Kconfig**

Okay, so I'm thinking, "Right, time to dive into the memory management configuration." Specifically, I need to check the `MIGRATION` options within the `mm/Kconfig` file. This is crucial for understanding or potentially tweaking the system's memory migration capabilities. I need to make sure I understand the current state of these configurations, and what options are available. This is a pretty fundamental aspect of how the kernel manages memory, so I need a clear understanding of what's going on with migration. Let's see what's set and if there's anything unusual or something that should be adjusted in this particular configuration. This is something I'm intimately familiar with, so I'll be looking at the details as well as the bigger picture.



16/2 2026/09/06 16:24 tool read-file
0m Args:
{
  "File": "mm/Kconfig",
  "FirstLine": 670,
  "LineCount": 30
}

Results:
{
  "Contents": " 670:\t\tdefault y\n 671:\t\tdepends on NUMA \u0026\u0026 MMU\n 672:\t\tselect MIGRATION\n 673:\t\thelp\n 674:\t\t  Support the migration of pages to other NUMA nodes, available to\n 675:\t\t  user space through interfaces like migrate_pages(), move_pages(),\n 676:\t\t  and mbind(). Selecting this option also enables support for page\n 677:\t\t  demotion for memory tiering.\n 678:\t\n 679:\tconfig MIGRATION\n 680:\t\tbool\n 681:\t\tdepends on MMU\n 682:\t\n 683:\tconfig DEVICE_MIGRATION\n 684:\t\tdef_bool MIGRATION \u0026\u0026 ZONE_DEVICE\n 685:\t\n 686:\tconfig ARCH_ENABLE_HUGEPAGE_MIGRATION\n 687:\t\tbool\n 688:\t\n 689:\tconfig ARCH_HAS_PMD_SOFTLEAVES\n 690:\t\tbool\n 691:\t\n 692:\tconfig HUGETLB_PAGE_SIZE_VARIABLE\n 693:\t\tdef_bool n\n 694:\t\thelp\n 695:\t\t  Allows the pageblock_order value to be dynamic instead of just standard\n 696:\t\t  HUGETLB_PAGE_ORDER when there are multiple HugeTLB page sizes available\n 697:\t\t  on a platform.\n 698:\t\n 699:\t\t  Note that the pageblock_order cannot exceed MAX_PAGE_ORDER and will be\n"
}

17/2 2026/09/06 16:24 llm patch-evaluator
0m Model:
gemini-3.7-flash

Args:
null

Results:
null

Tokens:
							input: 19990
							output: 249
							thoughts: 426
						

Thoughts:
**My Analysis of this Patch's Impact on Memory Management**

Okay, so this patch is clearly deeply involved with memory migration, compaction, and NUMA balancing, given the mentions of `NUMA_MIGRATION`, `MIGRATION`, `COMPACTION`, and `NUMA_BALANCING`. We're enabling migration here. The fact that `LRU_GEN`, `LRU_GEN_ENABLED`, and `MEMCG` are also relevant suggests a connection to generational LRU and memory cgroup management as it relates to migration, which is intriguing.

I need to meticulously review this patch. It impacts some core areas. Specifically, the reachable core memory management code, including MGLRU, memcg LRU zone size adjustments, and page migration reference flags, all of which are very sensitive. The introduction of new assertions, such as those using `VM_WARN_ON_ONCE` in functions like `lru_set_gen_flags` and `mem_cgroup_get_zone_lru_size`, is something I need to pay very close attention to. These are potential points of failure or debugging hooks. It also touches generation and tier calculations, page scanning, and eviction, specifically in `mm/vmscan.c`. Lastly, flag manipulation related to folios, like in `lru_set_refs_flags`, `folio_set_lru_refs`, etc., is being modified.

Based on all of this, this patch definitely warrants extensive fuzzing. The changes are too low-level and far-reaching to ignore. The following functions look like particularly fruitful targets for fuzzing: `lru_gen_look_around`, `folio_migrate_flags`, `mem_cgroup_update_lru_size`, `walk_pte_range`, `walk_pmd_range_locked`, `folio_update_gen`, `evict_folios`, `isolate_folio`, `lru_gen_inc_refs`, and `lru_gen_clear_refs`. They handle the most sensitive aspects of memory management changes.

And, of course, the `LRU_GEN`, `LRU_GEN_ENABLED`, `MEMCG`, and `MIGRATION` configurations must be enabled to fully exercise this patch.

I'll proceed to call `set-results` with this information.



18/2 2026/09/06 16:24 tool set-results
0m Args:
{
  "EnableConfigs": [
    "LRU_GEN",
    "LRU_GEN_ENABLED",
    "MEMCG",
    "MIGRATION"
  ],
  "FocusSymbols": [
    "lru_gen_look_around",
    "folio_migrate_flags",
    "mem_cgroup_update_lru_size",
    "walk_pte_range",
    "walk_pmd_range_locked",
    "folio_update_gen",
    "evict_folios",
    "isolate_folio",
    "lru_gen_inc_refs",
    "lru_gen_clear_refs"
  ],
  "Reasoning": "The patch touches core memory management code across mm/vmscan.c, mm/folio.c, mm/memcontrol.c, and mm/migrate.c, altering Multi-Gen LRU (MGLRU) reference tracking, folio flag manipulation, tier calculations, page eviction, and memcg LRU zone accounting. It also adds several VM_WARN_ON_ONCE assertions and changes synchronization/arithmetic logic. These paths are fully reachable and worth fuzzing.",
  "WorthFuzzing": true
}

Results:
{
  "EnableConfigs": [
    "LRU_GEN",
    "LRU_GEN_ENABLED",
    "MEMCG",
    "MIGRATION"
  ],
  "FocusSymbols": [
    "lru_gen_look_around",
    "folio_migrate_flags",
    "mem_cgroup_update_lru_size",
    "walk_pte_range",
    "walk_pmd_range_locked",
    "folio_update_gen",
    "evict_folios",
    "isolate_folio",
    "lru_gen_inc_refs",
    "lru_gen_clear_refs"
  ],
  "Reasoning": "The patch touches core memory management code across mm/vmscan.c, mm/folio.c, mm/memcontrol.c, and mm/migrate.c, altering Multi-Gen LRU (MGLRU) reference tracking, folio flag manipulation, tier calculations, page eviction, and memcg LRU zone accounting. It also adds several VM_WARN_ON_ONCE assertions and changes synchronization/arithmetic logic. These paths are fully reachable and worth fuzzing.",
  "WorthFuzzing": true
}

19/1 2026/09/06 16:24 action If
0m Args:
{
  "WorthFuzzing": true
}

Results:
null

20/2 2026/09/06 16:24 agent kmsan-evaluator
0m Model:
core

Args:
null

Results:
{
  "KMSANReasoning": "The patch series refactors Multi-Generational LRU (MGLRU) folio flag helpers, page table / rmap scanning logic, and memcg LRU zone size accounting. Specifically:\n- It replaces direct `folio-\u003eflags.f` access with `folio_flags(folio, 0)` accessor helpers (`lru_set_gen_flags`, `lru_get_gen_flags`, `lru_set_refs_flags`, `lru_get_refs_flags`, `folio_set_lru_refs`, `folio_migrate_lru_refs`).\n- It cleans up `read_ctrl_pos` parameterization and `walk_update_folio` callers. Stack variables such as `struct ctrl_pos pos` are fully initialized in `read_ctrl_pos`, and the `int *type` output parameter in `folio_update_gen` is guaranteed to be initialized when the return value indicates promotion (`old_gen \u003e= 0`).\n- It converts `mz-\u003elru_zone_size` to signed `long` to simplify underflow handling.\n\nNone of these changes allocate uninitialized buffers, copy kernel data to user space, or read uninitialized memory. Potential bugs in these changes (such as concurrency/locking races or state corruption) are adequately covered by standard KASAN, LOCKDEP, and kernel assertion builds without requiring a dedicated KMSAN fuzzing session.",
  "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 e3cbbc76e1c966c76fe621299a32d17afa7e4003
Author: syz-cluster <triage@syzkaller.com>
Date:   Sun Sep 6 16:23:49 2026 +0000

    syz-cluster: applied patch under review

diff --git a/include/linux/memcontrol.h b/include/linux/memcontrol.h
index c799926435560..2b67694285d04 100644
--- a/include/linux/memcontrol.h
+++ b/include/linux/memcontrol.h
@@ -100,7 +100,7 @@ struct mem_cgroup_per_node {
 	/* Fields which get updated often at the end. */
 	struct lruvec		lruvec;
 	CACHELINE_PADDING(_pad2_);
-	unsigned long		lru_zone_size[MAX_NR_ZONES][NR_LRU_LISTS];
+	long			lru_zone_size[MAX_NR_ZONES][NR_LRU_LISTS];
 	struct mem_cgroup_reclaim_iter	iter;
 
 	/*
@@ -887,10 +887,15 @@ static inline
 unsigned long mem_cgroup_get_zone_lru_size(struct lruvec *lruvec,
 		enum lru_list lru, int zone_idx)
 {
+	long val;
 	struct mem_cgroup_per_node *mz;
 
 	mz = container_of(lruvec, struct mem_cgroup_per_node, lruvec);
-	return READ_ONCE(mz->lru_zone_size[zone_idx][lru]);
+	val = READ_ONCE(mz->lru_zone_size[zone_idx][lru]);
+	if (WARN_ON_ONCE(val < 0))
+		return 0;
+
+	return val;
 }
 
 void __mem_cgroup_handle_over_high(gfp_t gfp_mask);
diff --git a/include/linux/mm_inline.h b/include/linux/mm_inline.h
index 621c8653d8f7e..ab69b9930893f 100644
--- a/include/linux/mm_inline.h
+++ b/include/linux/mm_inline.h
@@ -30,6 +30,17 @@ static inline int folio_is_file_lru(const struct folio *folio)
 	return !folio_test_swapbacked(folio);
 }
 
+/**
+ * folio_flags_is_file_lru - Should the folio be on a file LRU or anon LRU?
+ * @flags: The folio's flags.
+ *
+ * Just like folio_is_file_lru but take the folio flags directly instead.
+ */
+static inline int folio_flags_is_file_lru(const unsigned long *flags)
+{
+	return !test_bit(PG_swapbacked, flags);
+}
+
 static __always_inline void __update_lru_size(struct lruvec *lruvec,
 				enum lru_list lru, enum zone_type zid,
 				long nr_pages)
@@ -142,10 +153,66 @@ static inline int lru_tier_from_refs(int refs, bool workingset)
 	return workingset ? MAX_NR_TIERS - 1 : order_base_2(refs);
 }
 
-static inline int folio_lru_refs(const struct folio *folio)
+/**
+ * lru_set_gen_flags - Set the LRU generation number to specified folio flags.
+ * @flags: pointer to the folio flags
+ * @gen: generation number, between 0 and (MAX_NR_GENS - 1), inclusive.
+ */
+static inline void lru_set_gen_flags(unsigned long *flags, int gen)
+{
+	BUILD_BUG_ON(LRU_GEN_MASK & LRU_REFS_MASK);
+	VM_WARN_ON_ONCE(gen >= MAX_NR_GENS || gen < 0);
+	/* Store gen offset by 1, zero means the folio is off-list. */
+	*flags &= ~LRU_GEN_MASK;
+	*flags |= (gen + 1UL) << LRU_GEN_PGOFF;
+}
+
+/**
+ * lru_get_gen_flags - Return the LRU generation number from folio flags.
+ * @flags: folio flags
+ *
+ * Returns: A number between 0 and (MAX_NR_GENS - 1), inclusive. Returns
+ * -1 if the flags indicate the folio is off the list (e.g., isolated).
+ */
+static inline int lru_get_gen_flags(unsigned long flags)
+{
+	int gen = ((flags & LRU_GEN_MASK) >> LRU_GEN_PGOFF) - 1;
+
+	/* Exclude the legal -1 from the unsigned MAX_NR_GENS comparison */
+	VM_WARN_ON_ONCE(gen != -1 && gen >= MAX_NR_GENS);
+	return gen;
+}
+
+/**
+ * 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.
+ */
+static inline void lru_set_refs_flags(unsigned long *flags, unsigned int refs)
 {
-	unsigned long flags = READ_ONCE(folio->flags.f);
+	VM_WARN_ON_ONCE(refs > LRU_REFS_MAX);
+	BUILD_BUG_ON(LRU_REFS_MAX != (LRU_REFS_MASK >> LRU_REFS_PGOFF) + 1);
 
+	*flags &= ~LRU_REFS_FLAGS;
+	if (!refs)
+		return;
+	*flags |= (BIT(PG_referenced) | ((refs - 1UL) << LRU_REFS_PGOFF));
+}
+
+/**
+ * lru_get_refs_flags - Return LRU referenced / access count from folio flags.
+ * @flags: folio flags
+ *
+ * Reads the LRU referenced count set by lru_set_refs_flags().
+ */
+static inline int lru_get_refs_flags(unsigned long flags)
+{
 	if (!(flags & BIT(PG_referenced)))
 		return 0;
 	/*
@@ -155,11 +222,24 @@ static inline int folio_lru_refs(const struct folio *folio)
 	return ((flags & LRU_REFS_MASK) >> LRU_REFS_PGOFF) + 1;
 }
 
-static inline int folio_lru_gen(const struct folio *folio)
+static inline int folio_lru_refs(const struct folio *folio)
 {
-	unsigned long flags = READ_ONCE(folio->flags.f);
+	return lru_get_refs_flags(READ_ONCE(*const_folio_flags(folio, 0)));
+}
 
-	return ((flags & LRU_GEN_MASK) >> LRU_GEN_PGOFF) - 1;
+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;
+		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)
+{
+	return lru_get_gen_flags(READ_ONCE(*const_folio_flags(folio, 0)));
 }
 
 static inline bool lru_gen_is_active(const struct lruvec *lruvec, int gen)
@@ -270,7 +350,7 @@ 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.f, LRU_GEN_MASK | BIT(PG_active), flags);
+	set_mask_bits(folio_flags(folio, 0), LRU_GEN_MASK | BIT(PG_active), flags);
 
 	lru_gen_update_size(lruvec, folio, -1, gen);
 	/* for folio_rotate_reclaimable() */
@@ -295,7 +375,7 @@ static inline bool lru_gen_del_folio(struct lruvec *lruvec, struct folio *folio,
 
 	/* for folio_migrate_flags() */
 	flags = !reclaiming && lru_gen_is_active(lruvec, gen) ? BIT(PG_active) : 0;
-	flags = set_mask_bits(&folio->flags.f, LRU_GEN_MASK, flags);
+	flags = set_mask_bits(folio_flags(folio, 0), LRU_GEN_MASK, flags);
 	gen = ((flags & LRU_GEN_MASK) >> LRU_GEN_PGOFF) - 1;
 
 	lru_gen_update_size(lruvec, folio, gen, -1);
@@ -304,11 +384,19 @@ static inline bool lru_gen_del_folio(struct lruvec *lruvec, struct folio *folio,
 	return true;
 }
 
-static inline void folio_migrate_refs(struct folio *new, const struct folio *old)
+/**
+ * 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)
 {
-	unsigned long refs = READ_ONCE(old->flags.f) & LRU_REFS_MASK;
-
-	set_mask_bits(&new->flags.f, LRU_REFS_MASK, refs);
+	BUILD_BUG_ON(LRU_REFS_MASK & BIT(PG_referenced));
+	folio_set_lru_refs(new, folio_lru_refs(old));
 }
 #else /* !CONFIG_LRU_GEN */
 
@@ -337,9 +425,10 @@ static inline bool lru_gen_del_folio(struct lruvec *lruvec, struct folio *folio,
 	return false;
 }
 
-static inline void folio_migrate_refs(struct folio *new, const struct folio *old)
+static inline void folio_migrate_lru_refs(struct folio *new, const struct folio *old)
 {
-
+	if (folio_test_referenced(old))
+		folio_set_referenced(new);
 }
 #endif /* CONFIG_LRU_GEN */
 
diff --git a/include/linux/mmzone.h b/include/linux/mmzone.h
index 84e237f2c17db..c9ae7991a8b20 100644
--- a/include/linux/mmzone.h
+++ b/include/linux/mmzone.h
@@ -495,11 +495,14 @@ enum lruvec_flags {
  * folio->flags, masked by LRU_REFS_MASK.
  */
 #define MAX_NR_TIERS		4U
+#define LRU_TIER_MIN		0U
+#define LRU_TIER_MAX		(MAX_NR_TIERS - 1)
 
 #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,
diff --git a/mm/folio.c b/mm/folio.c
index 50a6dbe55998e..47a437e0f7fde 100644
--- a/mm/folio.c
+++ b/mm/folio.c
@@ -354,26 +354,28 @@ static void __lru_cache_activate_folio(struct folio *folio)
 
 static void lru_gen_inc_refs(struct folio *folio)
 {
-	unsigned long new_flags, old_flags = READ_ONCE(folio->flags.f);
+	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_test_referenced(folio)) {
-		set_mask_bits(&folio->flags.f, LRU_REFS_MASK, BIT(PG_referenced));
+	if (!folio_lru_refs(folio)) {
+		folio_set_lru_refs(folio, 1);
 		return;
 	}
 
 	do {
-		if ((old_flags & LRU_REFS_MASK) == LRU_REFS_MASK) {
+		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;
 		}
-
-		new_flags = old_flags + BIT(LRU_REFS_PGOFF);
-	} while (!try_cmpxchg(&folio->flags.f, &old_flags, new_flags));
+		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)
@@ -385,7 +387,8 @@ static bool lru_gen_clear_refs(struct folio *folio)
 	if (gen < 0)
 		return true;
 
-	set_mask_bits(&folio->flags.f, LRU_REFS_FLAGS | BIT(PG_workingset), 0);
+	folio_set_lru_refs(folio, 0);
+	folio_clear_workingset(folio);
 
 	rcu_read_lock();
 	seq = READ_ONCE(folio_lruvec(folio)->lrugen.min_seq[type]);
diff --git a/mm/memcontrol.c b/mm/memcontrol.c
index bd1e7e1544265..f17b2b2d61055 100644
--- a/mm/memcontrol.c
+++ b/mm/memcontrol.c
@@ -1529,28 +1529,12 @@ void mem_cgroup_update_lru_size(struct lruvec *lruvec, enum lru_list lru,
 				int zid, long nr_pages)
 {
 	struct mem_cgroup_per_node *mz;
-	unsigned long *lru_size;
-	long size;
 
 	if (mem_cgroup_disabled())
 		return;
 
 	mz = container_of(lruvec, struct mem_cgroup_per_node, lruvec);
-	lru_size = &mz->lru_zone_size[zid][lru];
-
-	if (nr_pages < 0)
-		*lru_size += nr_pages;
-
-	size = *lru_size;
-	if (WARN_ONCE(size < 0,
-		"%s(%p, %d, %ld): lru_size %ld\n",
-		__func__, lruvec, lru, nr_pages, size)) {
-		VM_BUG_ON(1);
-		*lru_size = 0;
-	}
-
-	if (nr_pages > 0)
-		*lru_size += nr_pages;
+	mz->lru_zone_size[zid][lru] += nr_pages;
 }
 
 /**
diff --git a/mm/migrate.c b/mm/migrate.c
index 15b45832bcfa7..a369d0c95c386 100644
--- a/mm/migrate.c
+++ b/mm/migrate.c
@@ -776,8 +776,9 @@ void folio_migrate_flags(struct folio *newfolio, struct folio *folio)
 {
 	int cpupid;
 
-	if (folio_test_referenced(folio))
-		folio_set_referenced(newfolio);
+	/* Copy the reference state, including PG_referenced */
+	folio_migrate_lru_refs(newfolio, folio);
+
 	if (folio_test_uptodate(folio))
 		folio_mark_uptodate(newfolio);
 	if (folio_test_clear_active(folio)) {
@@ -807,7 +808,6 @@ void folio_migrate_flags(struct folio *newfolio, struct folio *folio)
 	if (folio_test_idle(folio))
 		folio_set_idle(newfolio);
 
-	folio_migrate_refs(newfolio, folio);
 	/*
 	 * Copy NUMA information to the new page, to prevent over-eager
 	 * future migrations of this same page.
diff --git a/mm/vmscan.c b/mm/vmscan.c
index 245f68c75b289..40d3f1b48a74c 100644
--- a/mm/vmscan.c
+++ b/mm/vmscan.c
@@ -863,19 +863,22 @@ static bool lru_gen_set_refs(struct folio *folio, const vma_flags_t *vma_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)) {
-			set_mask_bits(&folio->flags.f, LRU_REFS_FLAGS, BIT(PG_workingset));
+			folio_set_workingset(folio);
+			folio_set_lru_refs(folio, 0);
 			return true;
 		}
 
-		set_mask_bits(&folio->flags.f, LRU_REFS_MASK, BIT(PG_referenced));
+		folio_set_lru_refs(folio, 1);
 		return false;
 	}
 
 	/* Promote on second access */
-	if (folio_lru_refs(folio) > 1)
-		set_mask_bits(&folio->flags.f, LRU_REFS_FLAGS, BIT(PG_workingset));
-	else
+	if (folio_lru_refs(folio) > 1) {
+		folio_set_workingset(folio);
+		folio_set_lru_refs(folio, 0);
+	} else {
 		folio_mark_accessed(folio);
+	}
 	return true;
 }
 #else
@@ -3220,8 +3223,8 @@ struct ctrl_pos {
 	int gain;
 };
 
-static void read_ctrl_pos(struct lruvec *lruvec, int type, int tier, int gain,
-			  struct ctrl_pos *pos)
+static void read_ctrl_pos(struct lruvec *lruvec, int type, int tier_min,
+			  int tier_max, int gain, struct ctrl_pos *pos)
 {
 	int i;
 	struct lru_gen_folio *lrugen = &lruvec->lrugen;
@@ -3230,7 +3233,7 @@ static void read_ctrl_pos(struct lruvec *lruvec, int type, int tier, int gain,
 	pos->gain = gain;
 	pos->refaulted = pos->total = 0;
 
-	for (i = tier % MAX_NR_TIERS; i <= min(tier, MAX_NR_TIERS - 1); i++) {
+	for (i = tier_min; i <= tier_max; i++) {
 		pos->refaulted += lrugen->avg_refaulted[type][i] +
 				  atomic_long_read(&lrugen->refaulted[hist][type][i]);
 		pos->total += lrugen->avg_total[type][i] +
@@ -3291,11 +3294,11 @@ static bool positive_ctrl_err(struct ctrl_pos *sp, struct ctrl_pos *pv)
  ******************************************************************************/
 
 /* promote pages accessed through page tables */
-static int folio_update_gen(struct folio *folio, int gen, const vma_flags_t *vma_flags)
+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.f);
-
-	VM_WARN_ON_ONCE(gen >= MAX_NR_GENS);
+	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
@@ -3304,31 +3307,35 @@ static int folio_update_gen(struct folio *folio, int gen, const vma_flags_t *vma
 	 */
 	if (!folio_test_referenced(folio) && !folio_test_workingset(folio) &&
 	    !is_exec_file_folio(folio, vma_flags)) {
-		set_mask_bits(&folio->flags.f, LRU_REFS_MASK, BIT(PG_referenced));
+		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_flags & LRU_GEN_MASK))
-			return -1;
+		if (old_gen < 0)
+			break;
 
-		new_flags = old_flags & ~(LRU_GEN_MASK | LRU_REFS_FLAGS);
-		new_flags |= ((gen + 1UL) << LRU_GEN_PGOFF) | BIT(PG_workingset);
-	} while (!try_cmpxchg(&folio->flags.f, &old_flags, new_flags));
+		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));
 
-	return ((old_flags & LRU_GEN_MASK) >> LRU_GEN_PGOFF) - 1;
+	*type = folio_flags_is_file_lru(&old_flags);
+	return old_gen;
 }
 
 static int __folio_inc_gen(struct folio *folio, int old_gen, bool *increased)
 {
-	unsigned long new_flags, old_flags = READ_ONCE(folio->flags.f);
+	unsigned long new_flags, old_flags = READ_ONCE(*folio_flags(folio, 0));
 	int new_gen;
 
-	VM_WARN_ON_ONCE_FOLIO(!(old_flags & LRU_GEN_MASK), folio);
-
 	do {
-		new_gen = ((old_flags & LRU_GEN_MASK) >> LRU_GEN_PGOFF) - 1;
+		new_gen = lru_get_gen_flags(old_flags);
+
 		/* folio_update_gen() has promoted this page? */
 		if (new_gen >= 0 && new_gen != old_gen) {
 			if (increased)
@@ -3336,11 +3343,12 @@ static int __folio_inc_gen(struct folio *folio, int old_gen, bool *increased)
 			return new_gen;
 		}
 
+		new_flags = old_flags;
 		new_gen = (old_gen + 1) % MAX_NR_GENS;
 
-		new_flags = old_flags & ~(LRU_GEN_MASK | LRU_REFS_FLAGS);
-		new_flags |= (new_gen + 1UL) << LRU_GEN_PGOFF;
-	} while (!try_cmpxchg(&folio->flags.f, &old_flags, new_flags));
+		lru_set_gen_flags(&new_flags, new_gen);
+		lru_set_refs_flags(&new_flags, 0);
+	} while (!try_cmpxchg(folio_flags(folio, 0), &old_flags, new_flags));
 
 	if (increased)
 		*increased = true;
@@ -3362,9 +3370,8 @@ static int folio_inc_gen(struct lruvec *lruvec, struct folio *folio)
 }
 
 static void update_batch_size(struct lru_gen_mm_walk *walk, struct folio *folio,
-			      int old_gen, int new_gen)
+			      int old_gen, int new_gen, int type)
 {
-	int type = folio_is_file_lru(folio);
 	int zone = folio_zonenum(folio);
 	int delta = folio_nr_pages(folio);
 
@@ -3551,22 +3558,24 @@ 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 folio *folio, int new_gen, bool dirty)
+		struct lruvec *lruvec, struct folio *folio, bool dirty)
 {
-	int old_gen;
+	int new_gen, old_gen, type;
 
 	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, &vma->flags);
+		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);
+			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)
@@ -3588,8 +3597,6 @@ static bool walk_pte_range(pmd_t *pmd, unsigned long start, unsigned long end,
 	struct lru_gen_mm_walk *walk = args->private;
 	struct mem_cgroup *memcg = lruvec_memcg(walk->lruvec);
 	struct pglist_data *pgdat = lruvec_pgdat(walk->lruvec);
-	DEFINE_MAX_SEQ(walk->lruvec);
-	int gen = lru_gen_from_seq(max_seq);
 	unsigned int nr;
 	pmd_t pmdval;
 
@@ -3640,7 +3647,7 @@ static bool walk_pte_range(pmd_t *pmd, unsigned long start, unsigned long end,
 			continue;
 
 		if (last != folio) {
-			walk_update_folio(walk, args->vma, last, gen, dirty);
+			walk_update_folio(walk, args->vma, walk->lruvec, last, dirty);
 
 			last = folio;
 			dirty = false;
@@ -3653,7 +3660,7 @@ static bool walk_pte_range(pmd_t *pmd, unsigned long start, unsigned long end,
 		walk->mm_stats[MM_LEAF_YOUNG] += nr;
 	}
 
-	walk_update_folio(walk, args->vma, last, gen, dirty);
+	walk_update_folio(walk, args->vma, walk->lruvec, last, dirty);
 	last = NULL;
 
 	if (i < PTRS_PER_PTE && get_next_vma(PMD_MASK, PAGE_SIZE, args, &start, &end))
@@ -3676,8 +3683,6 @@ static void walk_pmd_range_locked(pud_t *pud, unsigned long addr, struct vm_area
 	struct lru_gen_mm_walk *walk = args->private;
 	struct mem_cgroup *memcg = lruvec_memcg(walk->lruvec);
 	struct pglist_data *pgdat = lruvec_pgdat(walk->lruvec);
-	DEFINE_MAX_SEQ(walk->lruvec);
-	int gen = lru_gen_from_seq(max_seq);
 
 	VM_WARN_ON_ONCE(pud_leaf(*pud));
 
@@ -3731,7 +3736,7 @@ static void walk_pmd_range_locked(pud_t *pud, unsigned long addr, struct vm_area
 			goto next;
 
 		if (last != folio) {
-			walk_update_folio(walk, vma, last, gen, dirty);
+			walk_update_folio(walk, vma, walk->lruvec, last, dirty);
 
 			last = folio;
 			dirty = false;
@@ -3745,7 +3750,7 @@ static void walk_pmd_range_locked(pud_t *pud, unsigned long addr, struct vm_area
 		i = i > MIN_LRU_BATCH ? 0 : find_next_bit(bitmap, MIN_LRU_BATCH, i) + 1;
 	} while (i <= MIN_LRU_BATCH);
 
-	walk_update_folio(walk, vma, last, gen, dirty);
+	walk_update_folio(walk, vma, walk->lruvec, last, dirty);
 
 	lazy_mmu_mode_disable();
 	spin_unlock(ptl);
@@ -4351,8 +4356,6 @@ bool lru_gen_look_around(struct page_vma_mapped_walk *pvmw, unsigned int nr)
 	struct pglist_data *pgdat = folio_pgdat(folio);
 	struct lruvec *lruvec;
 	struct lru_gen_mm_state *mm_state;
-	unsigned long max_seq;
-	int gen;
 
 	lockdep_assert_held(pvmw->ptl);
 	VM_WARN_ON_ONCE_FOLIO(folio_test_lru(folio), folio);
@@ -4389,8 +4392,6 @@ bool lru_gen_look_around(struct page_vma_mapped_walk *pvmw, unsigned int nr)
 
 	memcg = get_mem_cgroup_from_folio(folio);
 	lruvec = mem_cgroup_lruvec(memcg, pgdat);
-	max_seq = READ_ONCE((lruvec)->lrugen.max_seq);
-	gen = lru_gen_from_seq(max_seq);
 	mm_state = get_mm_state(lruvec);
 
 	lazy_mmu_mode_enable();
@@ -4422,7 +4423,7 @@ bool lru_gen_look_around(struct page_vma_mapped_walk *pvmw, unsigned int nr)
 			continue;
 
 		if (last != folio) {
-			walk_update_folio(walk, vma, last, gen, dirty);
+			walk_update_folio(walk, vma, lruvec, last, dirty);
 
 			last = folio;
 			dirty = false;
@@ -4434,13 +4435,14 @@ bool lru_gen_look_around(struct page_vma_mapped_walk *pvmw, unsigned int nr)
 		young += nr;
 	}
 
-	walk_update_folio(walk, vma, last, gen, dirty);
+	walk_update_folio(walk, vma, lruvec, last, dirty);
 
 	lazy_mmu_mode_disable();
 
 	/* feedback from rmap walkers to page table walkers */
 	if (mm_state && suitable_to_scan(i, young))
-		update_bloom_filter(mm_state, max_seq, pvmw->pmd);
+		update_bloom_filter(mm_state, READ_ONCE(lruvec->lrugen.max_seq),
+				    pvmw->pmd);
 
 	mem_cgroup_put(memcg);
 
@@ -4785,7 +4787,7 @@ static bool isolate_folio(struct lruvec *lruvec, struct folio *folio, struct sca
 
 	/* see the comment on LRU_REFS_FLAGS */
 	if (!folio_test_referenced(folio))
-		set_mask_bits(&folio->flags.f, LRU_REFS_MASK, 0);
+		folio_set_lru_refs(folio, 0);
 
 	success = lru_gen_del_folio(lruvec, folio, true);
 	VM_WARN_ON_ONCE_FOLIO(!success, folio);
@@ -4878,9 +4880,9 @@ static int get_tier_idx(struct lruvec *lruvec, int type)
 	 * This value is chosen because any other tier would have at least twice
 	 * as many refaults as the first tier.
 	 */
-	read_ctrl_pos(lruvec, type, 0, 2, &sp);
-	for (tier = 1; tier < MAX_NR_TIERS; tier++) {
-		read_ctrl_pos(lruvec, type, tier, 3, &pv);
+	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);
 		if (!positive_ctrl_err(&sp, &pv))
 			break;
 	}
@@ -4901,8 +4903,10 @@ static int get_type_to_scan(struct lruvec *lruvec, int swappiness)
 	 * Compare the sum of all tiers of anon with that of file to determine
 	 * which type to scan.
 	 */
-	read_ctrl_pos(lruvec, LRU_GEN_ANON, MAX_NR_TIERS, swappiness, &sp);
-	read_ctrl_pos(lruvec, LRU_GEN_FILE, MAX_NR_TIERS, MAX_SWAPPINESS - swappiness, &pv);
+	read_ctrl_pos(lruvec, LRU_GEN_ANON, LRU_TIER_MIN, LRU_TIER_MAX,
+		      swappiness, &sp);
+	read_ctrl_pos(lruvec, LRU_GEN_FILE, LRU_TIER_MIN, LRU_TIER_MAX,
+		      MAX_SWAPPINESS - swappiness, &pv);
 
 	return positive_ctrl_err(&sp, &pv);
 }
@@ -5017,8 +5021,10 @@ static int evict_folios(unsigned long nr_to_scan, struct lruvec *lruvec,
 		}
 
 		/* don't add rejected folios to the oldest generation */
-		if (lru_gen_folio_seq(lruvec, folio, false) == min_seq[type])
-			set_mask_bits(&folio->flags.f, LRU_REFS_FLAGS, BIT(PG_active));
+		if (lru_gen_folio_seq(lruvec, folio, false) == min_seq[type]) {
+			folio_set_lru_refs(folio, 0);
+			folio_set_active(folio);
+		}
 	}
 
 	move_folios_to_lru(&list);

21/3 2026/09/06 16:24 llm kmsan-evaluator
0m Model:
gemini-3.7-flash

Args:
null

Results:
null

Tokens:
							input: 11586
							output: 19
							thoughts: 2096
						

Thoughts:
**KMSAN Fuzzing Session Justification Analysis**

Alright, let's break down this patch and see if we need a dedicated KMSAN fuzzing session on top of our standard KASAN fuzzing. My focus, as always, is on potential uninitialized memory reads or writes that could be exposed to userspace.

First, I see changes in `include/linux/memcontrol.h`, specifically changing an array's type in `struct mem_cgroup_per_node`. There's also a check and return of `0` if a value is negative. This doesn't seem directly related to uninitialized data sent to userspace, so I'm not overly concerned here.

The changes in `include/linux/mm_inline.h`, defining `folio_flags_is_file_lru` and refactoring LRU generation and flags manipulation, are interesting but likely internal.  They mostly deal with bit manipulation and internal struct flag settings. Still, I'll need to keep this in mind as I look at the larger call chains, but this looks like an internal refactor.

`include/linux/mmzone.h` just defines some constants, so no immediate red flags there.

In `mm/folio.c`, `lru_gen_inc_refs` and `lru_gen_clear_refs` utilize the flag manipulation functions. Again, the potential problem depends on if the flags are initialized. I need to trace back from the use of those functions.

`mm/memcontrol.c` simplifies `mem_cgroup_update_lru_size`. This *could* be a vector for uninitialized data, but it seems to directly update a known location.  I'll still keep an eye on this as I look for upstream data.

The `mm/migrate.c` change, `folio_migrate_lru_refs`, is a refactor of the `folio_migrate_refs` function and should be safe.

Finally, we're at `mm/vmscan.c`.  The signature change of `folio_update_gen` and a new out-parameter `int *type` is what has my attention!  I need to verify the initialization of `*type`.

I dove into `folio_update_gen` and found a conditional return where `*type` might not be set: the initial `if` statement. But I traced it back.  If the first check returns `-1`, it sets the refs and returns `-1`. The `walk_update_folio` caller will *not* use `type`.  However, if the `if` check does *not* happen, `type` is ALWAYS written to because of the logic within the `do...while` loop and the return.  So, in all cases `type` is set.

Furthermore, `type` is only *used* in `walk_update_folio` when `old_gen >= 0 && old_gen != new_gen`.  In this case, `type` is guaranteed to have been set when calling `update_batch_size`.

Given my analysis, I am not seeing any clear KMSAN-triggering issues here. No obvious structures sent to userspace with potentially uninitialized fields, no conditional logic depending on uninitialized variables. The new code does not seem to introduce new paths that could trigger issues.

Therefore, based on my analysis of the provided patch and the rule, I believe that a dedicated KMSAN fuzzing session is not warranted in addition to our existing KASAN fuzzing for this patch. I recommend proceeding with KASAN fuzzing as usual.



22/3 2026/09/06 16:24 tool grepper
0m Args:
{
  "Expression": "folio_update_gen"
}

Results:
{
  "Output": "include/linux/mm_inline.h=301=static inline unsigned long lru_gen_folio_seq(const struct lruvec *lruvec,\n--\ninclude/linux/mm_inline.h-311-\t * | Accessed through page tables and  | Accessed through file descriptors |\ninclude/linux/mm_inline.h:312:\t * | promoted by folio_update_gen()    | and protected by folio_inc_gen()  |\ninclude/linux/mm_inline.h-313-\t * +-----------------------------------+-----------------------------------+\n--\ninclude/linux/mmzone.h=427=enum lruvec_flags {\n--\ninclude/linux/mmzone.h-516- *\ninclude/linux/mmzone.h:517: * For folios accessed multiple times through page tables, folio_update_gen()\ninclude/linux/mmzone.h-518- * from a page table walk or lru_gen_set_refs() from a rmap walk sets\n--\ninclude/linux/mmzone.h-520- * Thereafter, those two paths set PG_workingset and promote folios to the\ninclude/linux/mmzone.h:521: * youngest generation. Like folio_inc_gen(), folio_update_gen() also clears\ninclude/linux/mmzone.h-522- * PG_referenced. Note that for this case, LRU_REFS_MASK is not used.\n--\nmm/vmscan.c=847=enum folio_references {\n--\nmm/vmscan.c-857- * with PG_active set. In contrast, the aging (page table walk) path uses\nmm/vmscan.c:858: * folio_update_gen().\nmm/vmscan.c-859- */\n--\nmm/vmscan.c=3281=static bool positive_ctrl_err(struct ctrl_pos *sp, struct ctrl_pos *pv)\n--\nmm/vmscan.c-3296-/* promote pages accessed through page tables */\nmm/vmscan.c:3297:static int folio_update_gen(struct folio *folio, int new_gen, int *type,\nmm/vmscan.c-3298-\t\t\t    const vma_flags_t *vma_flags)\n--\nmm/vmscan.c=3331=static int __folio_inc_gen(struct folio *folio, int old_gen, bool *increased)\n--\nmm/vmscan.c-3338-\nmm/vmscan.c:3339:\t\t/* folio_update_gen() has promoted this page? */\nmm/vmscan.c-3340-\t\tif (new_gen \u003e= 0 \u0026\u0026 new_gen != old_gen) {\n--\nmm/vmscan.c=3560=static void walk_update_folio(struct lru_gen_mm_walk *walk, struct vm_area_struct *vma,\n--\nmm/vmscan.c-3575-\tif (walk) {\nmm/vmscan.c:3576:\t\told_gen = folio_update_gen(folio, new_gen, \u0026type, \u0026vma-\u003eflags);\nmm/vmscan.c-3577-\t\tif (old_gen \u003e= 0 \u0026\u0026 old_gen != new_gen)\n"
}

23/3 2026/09/06 16:24 llm kmsan-evaluator
0m Model:
gemini-3.7-flash

Args:
null

Results:
null

Tokens:
							input: 15280
							output: 19
							thoughts: 619
						

Thoughts:
**Analyzing `read_ctrl_pos` and its Usage**

Okay, let's break down how `read_ctrl_pos` is being used. I need to understand its behavior in the context of these functions. From the code, it looks like `read_ctrl_pos` gets called in two key functions: `get_tier_idx` and `get_type_to_scan`. 

In `get_tier_idx`, it's called twice. The first call sets up an initial position, using `LRU_TIER_MIN`. The second call iterates through tiers, from `LRU_TIER_MIN + 1` up to `LRU_TIER_MAX`, and it looks like a comparison is made with the help of `positive_ctrl_err`, which I'll need to check as well.

In `get_type_to_scan`, `read_ctrl_pos` is called for both `LRU_GEN_ANON` and `LRU_GEN_FILE`, with some logic related to `swappiness`. This is where the function is fetching data for different types of LRU entries.

Now, focusing on the `read_ctrl_pos` function itself. It takes a `struct lruvec`, an integer `type`, and two tiers as inputs, and also a gain value. It then iterates through the tiers from `tier_min` to `tier_max`. Within the loop, it calculates a sum of `refaulted` and `total` values, which are used to update the `pos` structure. `pos` is a `struct ctrl_pos` which, importantly, is allocated on the stack in the calling functions. The code explicitly initializes `pos->gain`, `pos->refaulted`, and `pos->total` within `read_ctrl_pos`. I'm especially interested in that `gain` parameter and how it influences the `ctrl_pos` struct.

So, I need to check if this function is used elsewhere, and I should really look at how the `ctrl_pos` struct is being used after the calls to `read_ctrl_pos` to understand what that data means in the larger context. Also, I'll need to figure out what `lru_hist_from_seq` does.



24/3 2026/09/06 16:24 tool grepper
0m Args:
{
  "Expression": "read_ctrl_pos"
}

Results:
{
  "Output": "mm/vmscan.c=3220=struct ctrl_pos {\n--\nmm/vmscan.c-3225-\nmm/vmscan.c:3226:static void read_ctrl_pos(struct lruvec *lruvec, int type, int tier_min,\nmm/vmscan.c-3227-\t\t\t  int tier_max, int gain, struct ctrl_pos *pos)\n--\nmm/vmscan.c=4873=static int get_tier_idx(struct lruvec *lruvec, int type)\n--\nmm/vmscan.c-4882-\t */\nmm/vmscan.c:4883:\tread_ctrl_pos(lruvec, type, LRU_TIER_MIN, LRU_TIER_MIN, 2, \u0026sp);\nmm/vmscan.c-4884-\tfor (tier = LRU_TIER_MIN + 1; tier \u003c= LRU_TIER_MAX; tier++) {\nmm/vmscan.c:4885:\t\tread_ctrl_pos(lruvec, type, tier, tier, 3, \u0026pv);\nmm/vmscan.c-4886-\t\tif (!positive_ctrl_err(\u0026sp, \u0026pv))\n--\nmm/vmscan.c=4893=static int get_type_to_scan(struct lruvec *lruvec, int swappiness)\n--\nmm/vmscan.c-4905-\t */\nmm/vmscan.c:4906:\tread_ctrl_pos(lruvec, LRU_GEN_ANON, LRU_TIER_MIN, LRU_TIER_MAX,\nmm/vmscan.c-4907-\t\t      swappiness, \u0026sp);\nmm/vmscan.c:4908:\tread_ctrl_pos(lruvec, LRU_GEN_FILE, LRU_TIER_MIN, LRU_TIER_MAX,\nmm/vmscan.c-4909-\t\t      MAX_SWAPPINESS - swappiness, \u0026pv);\n"
}

25/3 2026/09/06 16:24 llm kmsan-evaluator
0m Model:
gemini-3.7-flash

Args:
null

Results:
null

Tokens:
							input: 16927
							output: 18
							thoughts: 688
						

Thoughts:
**Analyzing LRU Reference Migration and Memory Cgroup LRU Size Updates**

Okay, let's break this down. My immediate focus is on how LRU references are handled during folio migration and how these updates interact with memory cgroup LRU size tracking. First, I need to check the `folio_migrate_lru_refs` function. I see in `include/linux/mm_inline.h` that there's a conditional compilation. If `CONFIG_LRU_GEN` is defined, the function simply copies the LRU reference bits directly.  However, if `CONFIG_LRU_GEN` is *not* defined, it seems to only copy the `PG_referenced` bit, suggesting an older approach. The `BUILD_BUG_ON` ensures no overlap of the mask. Now, looking at `mm/migrate.c`, `folio_migrate_flags` calls `folio_migrate_lru_refs` to perform the actual copying of this referenced state during folio migration. This is essential for maintaining the correct LRU state after a page move.

Next, I need to understand how this interacts with memory cgroup LRU accounting. I have to look at `mem_cgroup_update_lru_size` and `mem_cgroup_get_zone_lru_size`. These functions seem to be core to tracking the size of the LRU lists within each memory cgroup. The `mem_cgroup_per_node` structure, defined in `include/linux/memcontrol.h`, contains the `lru_zone_size` array which appears to track the size of each LRU list, per zone, within a memory cgroup node.  `mem_cgroup_get_zone_lru_size` reads this array. It uses `READ_ONCE` for atomic reads to prevent issues with concurrency and the `WARN_ON_ONCE` offers an extra safeguard should the size turn negative. `mem_cgroup_update_lru_size` modifies this counter, adding or subtracting page counts, and has a guard against disabled memory cgroups.

Finally, I'm going to quickly check if `folio_flags` is a standard inline function within the kernel. I'll use `grep` to quickly confirm this, as I expect it will be.



26/3 2026/09/06 16:24 tool grepper
0m Args:
{
  "Expression": "folio_flags\\("
}

Results:
{
  "Output": "arch/sh/mm/cache-sh4.c=214=static void sh4_flush_cache_page(void *args)\n--\narch/sh/mm/cache-sh4.c-249-\t\tmap_coherent = (current_cpu_data.dcache.n_aliases \u0026\u0026\narch/sh/mm/cache-sh4.c:250:\t\t\ttest_bit(PG_dcache_clean, folio_flags(folio, 0)) \u0026\u0026\narch/sh/mm/cache-sh4.c-251-\t\t\tfolio_mapped(folio));\n--\narch/xtensa/mm/cache.c=88=void clear_user_highpage(struct page *page, unsigned long vaddr)\n--\narch/xtensa/mm/cache.c-95-\tkmap_invalidate_coherent(page, vaddr);\narch/xtensa/mm/cache.c:96:\tset_bit(PG_arch_1, folio_flags(folio, 0));\narch/xtensa/mm/cache.c-97-\tclear_page_alias(kvaddr, paddr);\n--\narch/xtensa/mm/cache.c=102=void copy_user_highpage(struct page *dst, struct page *src,\n--\narch/xtensa/mm/cache.c-113-\tkmap_invalidate_coherent(dst, vaddr);\narch/xtensa/mm/cache.c:114:\tset_bit(PG_arch_1, folio_flags(folio, 0));\narch/xtensa/mm/cache.c-115-\tcopy_page_alias(dst_vaddr, src_vaddr, dst_paddr, src_paddr);\n--\ninclude/linux/mm_inline.h=225=static inline int folio_lru_refs(const struct folio *folio)\ninclude/linux/mm_inline.h-226-{\ninclude/linux/mm_inline.h:227:\treturn lru_get_refs_flags(READ_ONCE(*const_folio_flags(folio, 0)));\ninclude/linux/mm_inline.h-228-}\n--\ninclude/linux/mm_inline.h=230=static inline void folio_set_lru_refs(struct folio *folio, unsigned int refs)\ninclude/linux/mm_inline.h-231-{\ninclude/linux/mm_inline.h:232:\tunsigned long new_flags, old_flags = READ_ONCE(*folio_flags(folio, 0));\ninclude/linux/mm_inline.h-233-\n--\ninclude/linux/mm_inline.h-236-\t\tlru_set_refs_flags(\u0026new_flags, refs);\ninclude/linux/mm_inline.h:237:\t} while (!try_cmpxchg(folio_flags(folio, 0), \u0026old_flags, new_flags));\ninclude/linux/mm_inline.h-238-}\n--\ninclude/linux/mm_inline.h=240=static inline int folio_lru_gen(const struct folio *folio)\ninclude/linux/mm_inline.h-241-{\ninclude/linux/mm_inline.h:242:\treturn lru_get_gen_flags(READ_ONCE(*const_folio_flags(folio, 0)));\ninclude/linux/mm_inline.h-243-}\n--\ninclude/linux/mm_inline.h=335=static inline bool lru_gen_add_folio(struct lruvec *lruvec, struct folio *folio, bool reclaiming)\n--\ninclude/linux/mm_inline.h-352-\t/* see the comment on MIN_NR_GENS about PG_active */\ninclude/linux/mm_inline.h:353:\tset_mask_bits(folio_flags(folio, 0), LRU_GEN_MASK | BIT(PG_active), flags);\ninclude/linux/mm_inline.h-354-\n--\ninclude/linux/mm_inline.h=365=static inline bool lru_gen_del_folio(struct lruvec *lruvec, struct folio *folio, bool reclaiming)\n--\ninclude/linux/mm_inline.h-377-\tflags = !reclaiming \u0026\u0026 lru_gen_is_active(lruvec, gen) ? BIT(PG_active) : 0;\ninclude/linux/mm_inline.h:378:\tflags = set_mask_bits(folio_flags(folio, 0), LRU_GEN_MASK, flags);\ninclude/linux/mm_inline.h-379-\tgen = ((flags \u0026 LRU_GEN_MASK) \u003e\u003e LRU_GEN_PGOFF) - 1;\n--\ninclude/linux/page-flags.h=335=static inline void page_init_poison(struct page *page, size_t size)\n--\ninclude/linux/page-flags.h-339-\ninclude/linux/page-flags.h:340:static const unsigned long *const_folio_flags(const struct folio *folio,\ninclude/linux/page-flags.h-341-\t\tunsigned n)\n--\ninclude/linux/page-flags.h-349-\ninclude/linux/page-flags.h:350:static unsigned long *folio_flags(struct folio *folio, unsigned n)\ninclude/linux/page-flags.h-351-{\n--\ninclude/linux/page-flags.h=411=static __always_inline bool folio_test_##name(const struct folio *folio) \\\ninclude/linux/page-flags.h:412:{ return test_bit(PG_##name, const_folio_flags(folio, page)); }\ninclude/linux/page-flags.h-413-\n--\ninclude/linux/page-flags.h=415=static __always_inline void folio_set_##name(struct folio *folio)\t\\\ninclude/linux/page-flags.h:416:{ set_bit(PG_##name, folio_flags(folio, page)); }\ninclude/linux/page-flags.h-417-\n--\ninclude/linux/page-flags.h=419=static __always_inline void folio_clear_##name(struct folio *folio)\t\\\ninclude/linux/page-flags.h:420:{ clear_bit(PG_##name, folio_flags(folio, page)); }\ninclude/linux/page-flags.h-421-\n--\ninclude/linux/page-flags.h=423=static __always_inline void __folio_set_##name(struct folio *folio)\t\\\ninclude/linux/page-flags.h:424:{ __set_bit(PG_##name, folio_flags(folio, page)); }\ninclude/linux/page-flags.h-425-\n--\ninclude/linux/page-flags.h=427=static __always_inline void __folio_clear_##name(struct folio *folio)\t\\\ninclude/linux/page-flags.h:428:{ __clear_bit(PG_##name, folio_flags(folio, page)); }\ninclude/linux/page-flags.h-429-\n--\ninclude/linux/page-flags.h=431=static __always_inline bool folio_test_set_##name(struct folio *folio)\t\\\ninclude/linux/page-flags.h:432:{ return test_and_set_bit(PG_##name, folio_flags(folio, page)); }\ninclude/linux/page-flags.h-433-\n--\ninclude/linux/page-flags.h=435=static __always_inline bool folio_test_clear_##name(struct folio *folio) \\\ninclude/linux/page-flags.h:436:{ return test_and_clear_bit(PG_##name, folio_flags(folio, page)); }\ninclude/linux/page-flags.h-437-\n--\ninclude/linux/page-flags.h=624=static __always_inline bool folio_test_swapcache(const struct folio *folio)\n--\ninclude/linux/page-flags.h-626-\treturn folio_test_swapbacked(folio) \u0026\u0026\ninclude/linux/page-flags.h:627:\t\t\ttest_bit(PG_swapcache, const_folio_flags(folio, 0));\ninclude/linux/page-flags.h-628-}\n--\ninclude/linux/page-flags.h=767=static inline bool folio_xor_flags_has_waiters(struct folio *folio,\n--\ninclude/linux/page-flags.h-769-{\ninclude/linux/page-flags.h:770:\treturn xor_unlock_is_negative_byte(mask, folio_flags(folio, 0));\ninclude/linux/page-flags.h-771-}\n--\ninclude/linux/page-flags.h=783=static inline bool folio_test_uptodate(const struct folio *folio)\ninclude/linux/page-flags.h-784-{\ninclude/linux/page-flags.h:785:\tbool ret = test_bit(PG_uptodate, const_folio_flags(folio, 0));\ninclude/linux/page-flags.h-786-\t/*\n--\ninclude/linux/page-flags.h=805=static __always_inline void __folio_mark_uptodate(struct folio *folio)\n--\ninclude/linux/page-flags.h-807-\tsmp_wmb();\ninclude/linux/page-flags.h:808:\t__set_bit(PG_uptodate, folio_flags(folio, 0));\ninclude/linux/page-flags.h-809-}\n--\ninclude/linux/page-flags.h=811=static __always_inline void folio_mark_uptodate(struct folio *folio)\n--\ninclude/linux/page-flags.h-818-\tsmp_wmb();\ninclude/linux/page-flags.h:819:\tset_bit(PG_uptodate, folio_flags(folio, 0));\ninclude/linux/page-flags.h-820-}\n--\ninclude/linux/page-flags.h=840=static __always_inline bool folio_test_head(const struct folio *folio)\ninclude/linux/page-flags.h-841-{\ninclude/linux/page-flags.h:842:\treturn test_bit(PG_head, const_folio_flags(folio, FOLIO_PF_ANY));\ninclude/linux/page-flags.h-843-}\n--\ninclude/linux/pagemap.h=1180=static inline bool folio_trylock(struct folio *folio)\ninclude/linux/pagemap.h-1181-{\ninclude/linux/pagemap.h:1182:\treturn likely(!test_and_set_bit_lock(PG_locked, folio_flags(folio, 0)));\ninclude/linux/pagemap.h-1183-}\n--\nmm/filemap.c=1250=static inline int folio_wait_bit_common(struct folio *folio, int bit_nr,\n--\nmm/filemap.c-1346-\t\t */\nmm/filemap.c:1347:\t\tif (unlikely(test_and_set_bit(bit_nr, folio_flags(folio, 0))))\nmm/filemap.c-1348-\t\t\tgoto repeat;\n--\nmm/filemap.c=1562=void folio_end_private_2(struct folio *folio)\n--\nmm/filemap.c-1564-\tVM_BUG_ON_FOLIO(!folio_test_private_2(folio), folio);\nmm/filemap.c:1565:\tclear_bit_unlock(PG_private_2, folio_flags(folio, 0));\nmm/filemap.c-1566-\tfolio_wake_bit(folio, PG_private_2);\n--\nmm/folio.c=355=static void lru_gen_inc_refs(struct folio *folio)\nmm/folio.c-356-{\nmm/folio.c:357:\tunsigned long new_flags, old_flags = READ_ONCE(*folio_flags(folio, 0));\nmm/folio.c-358-\tint refs;\n--\nmm/folio.c-377-\t\tlru_set_refs_flags(\u0026new_flags, refs + 1);\nmm/folio.c:378:\t} while (!try_cmpxchg(folio_flags(folio, 0), \u0026old_flags, new_flags));\nmm/folio.c-379-}\n--\nmm/vmscan.c=3297=static int folio_update_gen(struct folio *folio, int new_gen, int *type,\n--\nmm/vmscan.c-3299-{\nmm/vmscan.c:3300:\tunsigned long new_flags, old_flags = READ_ONCE(*folio_flags(folio, 0));\nmm/vmscan.c-3301-\tint old_gen;\n--\nmm/vmscan.c-3324-\t\tnew_flags |= BIT(PG_workingset);\nmm/vmscan.c:3325:\t} while (!try_cmpxchg(folio_flags(folio, 0), \u0026old_flags, new_flags));\nmm/vmscan.c-3326-\n--\nmm/vmscan.c=3331=static int __folio_inc_gen(struct folio *folio, int old_gen, bool *increased)\nmm/vmscan.c-3332-{\nmm/vmscan.c:3333:\tunsigned long new_flags, old_flags = READ_ONCE(*folio_flags(folio, 0));\nmm/vmscan.c-3334-\tint new_gen;\n--\nmm/vmscan.c-3350-\t\tlru_set_refs_flags(\u0026new_flags, 0);\nmm/vmscan.c:3351:\t} while (!try_cmpxchg(folio_flags(folio, 0), \u0026old_flags, new_flags));\nmm/vmscan.c-3352-\n"
}

27/3 2026/09/06 16:24 llm kmsan-evaluator
0m Model:
gemini-3.7-flash

Args:
null

Results:
null

Tokens:
							input: 21621
							output: 323
							thoughts: 486
						

Thoughts:
**My Assessment of the Patch's Memory Safety**

Okay, let's break this down. First, I see a few key areas of modification in this patch. The accounting of `lru_zone_size` is being refined, transitioning from `unsigned long` to `long` within `mem_cgroup_per_node`, and the related update and get functions are being simplified. Then, there's a significant refactoring of MGLRU and page flag helper functions, aiming for a more consistent access pattern using `folio_flags(folio, 0)`.  This includes introducing new helper functions for managing file LRU flags, generation flags, and reference flags. Finally, the patch touches `folio_update_gen` for better return values and some adjustments to `read_ctrl_pos`.

The crucial question, of course, is memory safety. I'm focusing on the potential for uninitialized memory to be exposed. From a quick scan, I don't see any obvious red flags like kernel-to-user memory copies where uninitialized data could leak, and I also don't see any changes that would involve exposing uninitialized fields of structs, stack buffers, or heap buffers. All of the modifications operate on existing in-memory data structures like folios, lruvecs, memcgs, and internal flags that are already initialized.

Looking closer at the specific refactorings, the out-parameter `*type` in `folio_update_gen` seems safe because it's set before returning any valid `old_gen`, and callers only access it after checking `old_gen >= 0`. The struct `ctrl_pos` being passed to `read_ctrl_pos` also seems safe because it's fully initialized before being used. I also checked for padding or memory leaks, and I don't see any such issue.

Given this analysis, I'm confident that this patch doesn't introduce or expose any new uninitialized memory risks. Therefore, "NeedsKMSAN" should be false. While the patch seems safe regarding uninitialized memory, KASAN, LOCKDEP, and the standard kernel debugging tools would be appropriate to validate the memory management and page flag synchronization changes more thoroughly.

Okay, with that assessment complete, I'm ready to finalize the response and call `set-results`.



28/3 2026/09/06 16:24 tool set-results
0m Args:
{
  "KMSANReasoning": "The patch series refactors Multi-Generational LRU (MGLRU) folio flag helpers, page table / rmap scanning logic, and memcg LRU zone size accounting. Specifically:\n- It replaces direct `folio-\u003eflags.f` access with `folio_flags(folio, 0)` accessor helpers (`lru_set_gen_flags`, `lru_get_gen_flags`, `lru_set_refs_flags`, `lru_get_refs_flags`, `folio_set_lru_refs`, `folio_migrate_lru_refs`).\n- It cleans up `read_ctrl_pos` parameterization and `walk_update_folio` callers. Stack variables such as `struct ctrl_pos pos` are fully initialized in `read_ctrl_pos`, and the `int *type` output parameter in `folio_update_gen` is guaranteed to be initialized when the return value indicates promotion (`old_gen \u003e= 0`).\n- It converts `mz-\u003elru_zone_size` to signed `long` to simplify underflow handling.\n\nNone of these changes allocate uninitialized buffers, copy kernel data to user space, or read uninitialized memory. Potential bugs in these changes (such as concurrency/locking races or state corruption) are adequately covered by standard KASAN, LOCKDEP, and kernel assertion builds without requiring a dedicated KMSAN fuzzing session.",
  "NeedsKMSAN": false
}

Results:
{
  "KMSANReasoning": "The patch series refactors Multi-Generational LRU (MGLRU) folio flag helpers, page table / rmap scanning logic, and memcg LRU zone size accounting. Specifically:\n- It replaces direct `folio-\u003eflags.f` access with `folio_flags(folio, 0)` accessor helpers (`lru_set_gen_flags`, `lru_get_gen_flags`, `lru_set_refs_flags`, `lru_get_refs_flags`, `folio_set_lru_refs`, `folio_migrate_lru_refs`).\n- It cleans up `read_ctrl_pos` parameterization and `walk_update_folio` callers. Stack variables such as `struct ctrl_pos pos` are fully initialized in `read_ctrl_pos`, and the `int *type` output parameter in `folio_update_gen` is guaranteed to be initialized when the return value indicates promotion (`old_gen \u003e= 0`).\n- It converts `mz-\u003elru_zone_size` to signed `long` to simplify underflow handling.\n\nNone of these changes allocate uninitialized buffers, copy kernel data to user space, or read uninitialized memory. Potential bugs in these changes (such as concurrency/locking races or state corruption) are adequately covered by standard KASAN, LOCKDEP, and kernel assertion builds without requiring a dedicated KMSAN fuzzing session.",
  "NeedsKMSAN": false
}

LLM Calls Summary:
Total Calls Total Tokens Avg Tokens Total Duration (Seconds) Avg Duration (Seconds)
Tool Calls Summary:
Total Calls Total Duration (Seconds) Avg Duration (Seconds)