AI Trajectory


Trajectory: (Expand/Collapse All)
Seq Timestamp Type Name Duration
0/0 2026/11/02 21:59 flow patch-triage
14m Args:
null

Results:
{
  "EnableConfigs": [
    "LRU_GEN",
    "LRU_GEN_ENABLED",
    "PTDUMP_DEBUGFS",
    "X86_DIRECT_GBPAGES"
  ],
  "FocusSymbols": [
    "walk_pud_range",
    "walk_pmd_range",
    "ptdump_walk_pgd_level_core",
    "collapse_pud_page",
    "populate_pud"
  ],
  "KMSANReasoning": "The patch series cleans up and refactors compile-time page table folding across architecture-independent MM code and specific architectures (ARM, LoongArch, MIPS, RISC-V, and x86). Key changes include:\n1. Hard-coding dummy getters (pgdp_get, pudp_get, p4dp_get) to return explicitly zero-initialized values ({ 0 }) for compile-time folded page table levels, and adding build-time checks (BUILD_BUG_ON) to prevent folded helpers from operating on dummy values.\n2. In mm/vmscan.c and KVM MMU walkers (LoongArch, RISC-V), replacing references to local stack-allocated page-table value variables with proper page-table pointers (e.g., pmd + i, pud + i) and lockless offset helpers.\n3. Introducing helper functions (direct_gbpages_enabled(), mm_first_pgtable_level()) and updating effective_prot tracking in x86 ptdump.\n\nNone of these changes introduce or expose uninitialized memory risks:\n- All new stack variables and dummy structs are explicitly zero-initialized (e.g., pgd_t dummy = { 0 }).\n- No structures or buffers are copied to user space (no copy_to_user, put_user, netlink skb, or ioctls modified).\n- Page table entries are read from live page tables or set to zeroed dummy values.\n- Any pointer dereferencing or boundary errors in page table walking are within the scope of KASAN (OOB/UAF) rather than KMSAN.\n\nTherefore, a dedicated KMSAN fuzzing session is not warranted.",
  "NeedsKMSAN": false,
  "Reasoning": "The patch refactors page-table folding definitions across asm-generic and architecture headers, addressing cases where addresses of stack variables were incorrectly passed to page-table walkers. In mm/vmscan.c (MGLRU), walk_pud_range() and walk_pmd_range() are modified to pass proper page table pointers to lower-level walkers. On x86, arch/x86/mm/dump_pagetables.c is updated to use the reordered pgtable_level enum and the new mm_first_pgtable_level() helper to determine effective permissions, and arch/x86/mm/pat/set_memory.c adopts the new direct_gbpages_enabled() helper in CPA logic. Because these modifications alter core memory management and page-table walking logic in reachable paths on x86_64, fuzzing is warranted to uncover potential invariant violations or regressions.",
  "WorthFuzzing": true
}

1/1 2026/11/02 21:59 action read-patch-diff
0m Args:
null

Results:
{
  "PatchDiff": "commit a0a56350684d753b1a84f5f158b0a4d9cd6c69a5\nAuthor: syz-cluster \u003ctriage@syzkaller.com\u003e\nDate:   Mon Nov 2 21:59:07 2026 +0000\n\n    syz-cluster: applied patch under review\n\ndiff --git a/Documentation/mm/page_tables.rst b/Documentation/mm/page_tables.rst\nindex 126c8762825092..d4f3e2e343ca2b 100644\n--- a/Documentation/mm/page_tables.rst\n+++ b/Documentation/mm/page_tables.rst\n@@ -143,15 +143,81 @@ pointers on each level is architecture-defined.::\n Page Table Folding\n ==================\n \n-If the architecture does not use all the page table levels, they can be *folded*\n-which means skipped, and all operations performed on page tables will be\n-compile-time augmented to just skip a level when accessing the next lower\n-level.\n-\n-Page table handling code that wishes to be architecture-neutral, such as the\n-virtual memory manager, will need to be written so that it traverses all of the\n-currently five levels. This style should also be preferred for\n-architecture-specific code, so as to be robust to future changes.\n+Not all architectures support 5-level page tables; while for some of them\n+the exact number of supported page table levels is known at compile time,\n+others can determine the number of page table levels at runtime based on\n+hardware support and address space sizes.\n+\n+Generic page table walking code always assumes that 5 levels of page table\n+exist. To make page table walking code not have to worry about that,\n+`compile-time folding` and `runtime folding` of page tables are used.\n+Compile-time folding is mostly handled in common code, whereas runtime folding\n+is exclusively handled in architecture code.\n+\n+This description focuses on generic compile-time folded page tables; for\n+architecture-specific variants, some details can vary, however, without\n+affecting common page table walkers.\n+\n+When walking folded page tables, all upper page table levels up to the supported\n+level are skipped in page table walkers: this is achieved by (a) treating\n+entries in upper page table levels as present and pointing at a page table; and\n+(b) having page table walkers cast the entry pointer to the next-level entry\n+instead of dereferencing that table. From the perspective of a page table\n+walker, the entry points at itself.\n+\n+Assuming compile-time folded 4-level page tables, to achieve (a), pgd_present()\n+and pgd_leaf() are hard-coded to indicate a present page table entry that\n+points at a page table, and to achieve (b) p4d_offset() and\n+p4d_offset_lockless() simply cast the page table entry pointer to the next\n+lower level.\n+\n+In the current design, this is further modeled by having the P4D have a\n+single page table entry::\n+\n+        PGD\n+  --\u003e +------+          NOP4D\n+      | ptr0 |-------\u003e +------+           PUD\n+      | ptr1 |-        | ptr0 |-------\u003e +-----+\n+      | ptr2 | \\       +------+         | ptr |-------\u003e ...\n+      | ptr3 |  \\                       | ptr |\n+        ...      \\                        ..\n+                  \\        NOP4D\n+                   +----\u003e +------+           PUD\n+                          | ptr1 |-------\u003e +-----+\n+                          +------+         | ptr |-------\u003e ...\n+                                           | ptr |\n+                                             ...\n+\n+Note that the arrows from PGD to NOP4D represent page-table-walker\n+transitions, not pointers stored in the pgd entries.\n+\n+Using p4d as an example, `nop4d`/`p4d folded` translates to the following:\n+\n+- p4d is considered folded into pgd; both are operating on the same page\n+  table.\n+\n+- Most pgd_* helpers are hard-coded dummy functions that ignore the passed\n+  pgd_t values entirely. Exceptions are pgd_val() and low-level helpers\n+  set_pgd() + pgd_page_vaddr(), which effectively translate to set_p4d()/\n+  p4d_pgtable() to keep existing arch code working.\n+\n+  Architectures must provide p4d_* helpers (unless further common\n+  compile-time folding applies).\n+\n+- PTRS_PER_P4D is hard-coded to 1. Architectures must define PTRS_PER_PGD.\n+\n+To avoid reading a value that will never be used but cannot be entirely\n+optimized out, compile-time folded page table code also makes pXdp_get()\n+return a constant dummy value.\n+\n+In common code, this only affects pXd_val() when used for printing page\n+table entries for debugging purposes. As we don't want architecture code\n+that uses set_pXd(), pgd_page_vaddr() or pXd_pgtable() to accidentally\n+operate on dummy values, the compiler will error out if it detects that the\n+helpers are used with dummy values. For a folded level, pXd_page() must not\n+be used and unconditionally triggers a compiler error. Architecture code must\n+instead call the helpers on the proper first page table level: e.g., set_p4d()\n+instead of set_pgd().\n \n \n MMU, TLB, and Page Faults\ndiff --git a/arch/arm/include/asm/page-nommu.h b/arch/arm/include/asm/page-nommu.h\nindex e74415c959beac..88659b38e23649 100644\n--- a/arch/arm/include/asm/page-nommu.h\n+++ b/arch/arm/include/asm/page-nommu.h\n@@ -18,12 +18,12 @@\n  */\n typedef unsigned long pte_t;\n typedef unsigned long pmd_t;\n-typedef unsigned long pgd_t[2];\n+typedef unsigned long pgd_t;\n typedef unsigned long pgprot_t;\n \n #define pte_val(x)      (x)\n #define pmd_val(x)      (x)\n-#define pgd_val(x)\t((x)[0])\n+#define pgd_val(x)      (x)\n #define pgprot_val(x)   (x)\n \n #define __pte(x)        (x)\ndiff --git a/arch/arm/include/asm/pgtable-2level-types.h b/arch/arm/include/asm/pgtable-2level-types.h\nindex 650e793f41429f..805044ea315b7d 100644\n--- a/arch/arm/include/asm/pgtable-2level-types.h\n+++ b/arch/arm/include/asm/pgtable-2level-types.h\n@@ -25,7 +25,7 @@ typedef struct { pteval_t pgprot; } pgprot_t;\n \n #define pte_val(x)      ((x).pte)\n #define pmd_val(x)      ((x).pmd)\n-#define pgd_val(x)\t((x).pgd[0])\n+#define pgd_val(x)      ((x).pgd[0])\n #define pgprot_val(x)   ((x).pgprot)\n \n #define __pte(x)        ((pte_t) { (x) } )\n@@ -36,14 +36,28 @@ typedef struct { pteval_t pgprot; } pgprot_t;\n /*\n  * .. while these make it easier on the compiler\n  */\n+typedef u64 pgdval_t;\n+\n typedef pteval_t pte_t;\n typedef pmdval_t pmd_t;\n-typedef pmdval_t pgd_t[2];\n+typedef pgdval_t pgd_t;\n typedef pteval_t pgprot_t;\n \n #define pte_val(x)      (x)\n #define pmd_val(x)      (x)\n-#define pgd_val(x)\t((x)[0])\n+\n+static inline pmdval_t pgd_val(pgd_t pgd)\n+{\n+\t/*\n+\t * A PGD entry actually corresponds to two PMD entries in the PMD table.\n+\t * Both PMD entries point to PTE tables residing in the same physical page,\n+\t * but at different offsets. See include/asm/pgtable-2level.h for details.\n+\t *\n+\t * Historically, pgd_val() has returned the lower of the two PMD entries.\n+\t */\n+\treturn (*(pmdval_t (*)[2])\u0026pgd)[0];\n+}\n+\n #define pgprot_val(x)   (x)\n \n #define __pte(x)        (x)\ndiff --git a/arch/arm/include/asm/pgtable.h b/arch/arm/include/asm/pgtable.h\nindex 8dd17d20faa33f..ff3868acea9454 100644\n--- a/arch/arm/include/asm/pgtable.h\n+++ b/arch/arm/include/asm/pgtable.h\n@@ -134,8 +134,6 @@ extern pgprot_t phys_mem_access_prot(struct file *file, unsigned long pfn,\n \n extern pgd_t swapper_pg_dir[PTRS_PER_PGD];\n \n-#define pgdp_get(pgpd)\t\tREAD_ONCE(*pgdp)\n-\n #define pud_page(pud)\t\tpmd_page(__pmd(pud_val(pud)))\n #define pud_write(pud)\t\tpmd_write(__pmd(pud_val(pud)))\n \ndiff --git a/arch/arm64/include/asm/pgtable.h b/arch/arm64/include/asm/pgtable.h\nindex 763c5a411d64e3..b391f325670f81 100644\n--- a/arch/arm64/include/asm/pgtable.h\n+++ b/arch/arm64/include/asm/pgtable.h\n@@ -1141,29 +1141,6 @@ static inline bool pgtable_l5_enabled(void) { return false; }\n #define p4d_clear_fixmap()\n \n #define p4d_offset_kimg(dir,addr)\t((p4d_t *)dir)\n-\n-static inline\n-p4d_t *p4d_offset_lockless_folded(pgd_t *pgdp, pgd_t pgd, unsigned long addr)\n-{\n-\t/*\n-\t * With runtime folding of the pud, pud_offset_lockless() passes\n-\t * the 'pgd_t *' we return here to p4d_to_folded_pud(), which\n-\t * will offset the pointer assuming that it points into\n-\t * a page-table page. However, the fast GUP path passes us a\n-\t * pgd_t allocated on the stack and so we must use the original\n-\t * pointer in 'pgdp' to construct the p4d pointer instead of\n-\t * using the generic p4d_offset_lockless() implementation.\n-\t *\n-\t * Note: reusing the original pointer means that we may\n-\t * dereference the same (live) page-table entry multiple times.\n-\t * This is safe because it is still only loaded once in the\n-\t * context of each level and the CPU guarantees same-address\n-\t * read-after-read ordering.\n-\t */\n-\treturn p4d_offset(pgdp, addr);\n-}\n-#define p4d_offset_lockless p4d_offset_lockless_folded\n-\n #endif  /* CONFIG_PGTABLE_LEVELS \u003e 4 */\n \n #define pgd_set_fixmap(addr)\t((pgd_t *)set_fixmap_offset(FIX_PGD, addr))\ndiff --git a/arch/loongarch/include/asm/pgtable.h b/arch/loongarch/include/asm/pgtable.h\nindex f876031351319a..9a62b87c246de0 100644\n--- a/arch/loongarch/include/asm/pgtable.h\n+++ b/arch/loongarch/include/asm/pgtable.h\n@@ -631,7 +631,9 @@ static inline long pmd_protnone(pmd_t pmd)\n #endif /* CONFIG_ARCH_HAS_PTE_PROTNONE */\n \n #define pmd_leaf(pmd)\t\t((pmd_val(pmd) \u0026 _PAGE_HUGE) != 0)\n+#ifndef __PAGETABLE_PMD_FOLDED\n #define pud_leaf(pud)\t\t((pud_val(pud) \u0026 _PAGE_HUGE) != 0)\n+#endif\n \n /*\n  * We provide our own get_unmapped area to cope with the virtual aliasing\ndiff --git a/arch/loongarch/kvm/mmu.c b/arch/loongarch/kvm/mmu.c\nindex 64fddd9f0ada1e..ab0a06b73e8d38 100644\n--- a/arch/loongarch/kvm/mmu.c\n+++ b/arch/loongarch/kvm/mmu.c\n@@ -679,10 +679,10 @@ static int host_pfn_mapping_level(struct kvm *kvm, gfn_t gfn,\n \tint level = 0;\n \tunsigned long hva;\n \tunsigned long flags;\n-\tpgd_t pgd;\n-\tp4d_t p4d;\n-\tpud_t pud;\n-\tpmd_t pmd;\n+\tpgd_t *pgdp, pgd;\n+\tp4d_t *p4dp, p4d;\n+\tpud_t *pudp, pud;\n+\tpmd_t *pmdp, pmd;\n \n \t/*\n \t * Note, using the already-retrieved memslot and __gfn_to_hva_memslot()\n@@ -708,19 +708,23 @@ static int host_pfn_mapping_level(struct kvm *kvm, gfn_t gfn,\n \t * value) and then p*d_offset() walks into the target huge page instead\n \t * of the old page table (sees the new value).\n \t */\n-\tpgd = pgdp_get(pgd_offset(kvm-\u003emm, hva));\n+\tpgdp = pgd_offset(kvm-\u003emm, hva);\n+\tpgd = pgdp_get(pgdp);\n \tif (pgd_none(pgd))\n \t\tgoto out;\n \n-\tp4d = p4dp_get(p4d_offset(\u0026pgd, hva));\n+\tp4dp = p4d_offset_lockless(pgdp, pgd, hva);\n+\tp4d = p4dp_get(p4dp);\n \tif (p4d_none(p4d) || !p4d_present(p4d))\n \t\tgoto out;\n \n-\tpud = pudp_get(pud_offset(\u0026p4d, hva));\n+\tpudp = pud_offset_lockless(p4dp, p4d, hva);\n+\tpud = pudp_get(pudp);\n \tif (pud_none(pud) || !pud_present(pud))\n \t\tgoto out;\n \n-\tpmd = pmdp_get(pmd_offset(\u0026pud, hva));\n+\tpmdp = pmd_offset_lockless(pudp, pud, hva);\n+\tpmd = pmdp_get(pmdp);\n \tif (pmd_none(pmd) || !pmd_present(pmd))\n \t\tgoto out;\n \ndiff --git a/arch/mips/include/asm/pgtable.h b/arch/mips/include/asm/pgtable.h\nindex fa7b935f947ca0..151f4d15bd12bf 100644\n--- a/arch/mips/include/asm/pgtable.h\n+++ b/arch/mips/include/asm/pgtable.h\n@@ -745,8 +745,10 @@ static inline pmd_t pmdp_huge_get_and_clear(struct mm_struct *mm,\n \n #ifdef _PAGE_HUGE\n #define pmd_leaf(pmd)\t((pmd_val(pmd) \u0026 _PAGE_HUGE) != 0)\n+#ifndef __PAGETABLE_PMD_FOLDED\n #define pud_leaf(pud)\t((pud_val(pud) \u0026 _PAGE_HUGE) != 0)\n #endif\n+#endif\n \n #define gup_fast_permitted(start, end)\t(!cpu_has_dc_aliases)\n \ndiff --git a/arch/openrisc/include/asm/pgtable.h b/arch/openrisc/include/asm/pgtable.h\nindex 13afcc0bd8631b..7fceed641610c4 100644\n--- a/arch/openrisc/include/asm/pgtable.h\n+++ b/arch/openrisc/include/asm/pgtable.h\n@@ -331,9 +331,6 @@ static inline unsigned long pmd_page_vaddr(pmd_t pmd)\n \treturn ((unsigned long) __va(pmd_val(pmd) \u0026 PAGE_MASK));\n }\n \n-#define __pmd_offset(address) \\\n-\t(((address) \u003e\u003e PMD_SHIFT) \u0026 (PTRS_PER_PMD-1))\n-\n #define PFN_PTE_SHIFT\t\tPAGE_SHIFT\n #define pte_pfn(x)\t\t((unsigned long)(((x).pte)) \u003e\u003e PAGE_SHIFT)\n #define pfn_pte(pfn, prot)  __pte((((pfn) \u003c\u003c PAGE_SHIFT)) | pgprot_val(prot))\ndiff --git a/arch/riscv/kvm/mmu.c b/arch/riscv/kvm/mmu.c\nindex 371fccaf9df08a..a062e8508d3f97 100644\n--- a/arch/riscv/kvm/mmu.c\n+++ b/arch/riscv/kvm/mmu.c\n@@ -450,10 +450,10 @@ static int get_hva_mapping_size(struct kvm *kvm,\n {\n \tint size = PAGE_SIZE;\n \tunsigned long flags;\n-\tpgd_t pgd;\n-\tp4d_t p4d;\n-\tpud_t pud;\n-\tpmd_t pmd;\n+\tpgd_t *pgdp, pgd;\n+\tp4d_t *p4dp, p4d;\n+\tpud_t *pudp, pud;\n+\tpmd_t *pmdp, pmd;\n \n \t/*\n \t * Disable IRQs to prevent concurrent tear down of host page tables,\n@@ -469,15 +469,18 @@ static int get_hva_mapping_size(struct kvm *kvm,\n \t * value) and then p*d_offset() walks into the target huge page instead\n \t * of the old page table (sees the new value).\n \t */\n-\tpgd = pgdp_get(pgd_offset(kvm-\u003emm, hva));\n+\tpgdp = pgd_offset(kvm-\u003emm, hva);\n+\tpgd = pgdp_get(pgdp);\n \tif (pgd_none(pgd))\n \t\tgoto out;\n \n-\tp4d = p4dp_get(p4d_offset(\u0026pgd, hva));\n+\tp4dp = p4d_offset_lockless(pgdp, pgd, hva);\n+\tp4d = p4dp_get(p4dp);\n \tif (p4d_none(p4d) || !p4d_present(p4d))\n \t\tgoto out;\n \n-\tpud = pudp_get(pud_offset(\u0026p4d, hva));\n+\tpudp = pud_offset_lockless(p4dp, p4d, hva);\n+\tpud = pudp_get(pudp);\n \tif (pud_none(pud) || !pud_present(pud))\n \t\tgoto out;\n \n@@ -486,7 +489,8 @@ static int get_hva_mapping_size(struct kvm *kvm,\n \t\tgoto out;\n \t}\n \n-\tpmd = pmdp_get(pmd_offset(\u0026pud, hva));\n+\tpmdp = pmd_offset_lockless(pudp, pud, hva);\n+\tpmd = pmdp_get(pmdp);\n \tif (pmd_none(pmd) || !pmd_present(pmd))\n \t\tgoto out;\n \ndiff --git a/arch/riscv/mm/fault.c b/arch/riscv/mm/fault.c\nindex 04ed6f8acae4fd..b2bcaf10d13f1d 100644\n--- a/arch/riscv/mm/fault.c\n+++ b/arch/riscv/mm/fault.c\n@@ -167,14 +167,16 @@ bad_area(struct pt_regs *regs, struct mm_struct *mm, int code,\n \n static inline void vmalloc_fault(struct pt_regs *regs, int code, unsigned long addr)\n {\n-\tpgd_t *pgd, *pgd_k;\n-\tpud_t *pud_k;\n-\tp4d_t *p4d_k;\n-\tpmd_t *pmd_k;\n-\tpte_t *pte_k;\n+\tpgd_t *pgdp_k, *pgdp, pgd_k;\n+\tp4d_t *p4dp_k, *p4dp;\n+\tpud_t *pudp_k, *pudp, pud_k;\n+\tpmd_t *pmdp_k, *pmdp, pmd_k;\n+\tpte_t *ptep_k;\n \tint index;\n \tunsigned long pfn;\n \n+\tBUILD_BUG_ON(CONFIG_PGTABLE_LEVELS != 5 \u0026\u0026 CONFIG_PGTABLE_LEVELS != 2);\n+\n \t/* User mode accesses just cause a SIGSEGV */\n \tif (user_mode(regs))\n \t\treturn do_trap(regs, SIGSEGV, code, addr);\n@@ -189,39 +191,51 @@ static inline void vmalloc_fault(struct pt_regs *regs, int code, unsigned long a\n \t */\n \tindex = pgd_index(addr);\n \tpfn = csr_read(CSR_SATP) \u0026 SATP_PPN;\n-\tpgd = (pgd_t *)pfn_to_virt(pfn) + index;\n-\tpgd_k = init_mm.pgd + index;\n \n-\tif (!pgd_present(pgdp_get(pgd_k))) {\n+\tpgdp = (pgd_t *)pfn_to_virt(pfn) + index;\n+\tpgdp_k = init_mm.pgd + index;\n+\n+\tpgd_k = pgdp_get(pgdp_k);\n+\tif (!pgd_present(pgd_k)) {\n \t\tno_context(regs, addr);\n \t\treturn;\n \t}\n-\tset_pgd(pgd, pgdp_get(pgd_k));\n+\tif (CONFIG_PGTABLE_LEVELS == 5)\n+\t\tset_pgd(pgdp, pgd_k);\n \n-\tp4d_k = p4d_offset(pgd_k, addr);\n-\tif (!p4d_present(p4dp_get(p4d_k))) {\n+\tp4dp = p4d_offset(pgdp, addr);\n+\tp4dp_k = p4d_offset(pgdp_k, addr);\n+\tif (!p4d_present(p4dp_get(p4dp_k))) {\n \t\tno_context(regs, addr);\n \t\treturn;\n \t}\n \n-\tpud_k = pud_offset(p4d_k, addr);\n-\tif (!pud_present(pudp_get(pud_k))) {\n+\tpudp = pud_offset(p4dp, addr);\n+\tpudp_k = pud_offset(p4dp_k, addr);\n+\n+\tpud_k = pudp_get(pudp_k);\n+\tif (!pud_present(pud_k)) {\n \t\tno_context(regs, addr);\n \t\treturn;\n \t}\n-\tif (pud_leaf(pudp_get(pud_k)))\n+\tif (pud_leaf(pud_k))\n \t\tgoto flush_tlb;\n \n \t/*\n \t * Since the vmalloc area is global, it is unnecessary\n \t * to copy individual PTEs\n \t */\n-\tpmd_k = pmd_offset(pud_k, addr);\n-\tif (!pmd_present(pmdp_get(pmd_k))) {\n+\tpmdp = pmd_offset(pudp, addr);\n+\tpmdp_k = pmd_offset(pudp_k, addr);\n+\n+\tpmd_k = pmdp_get(pmdp_k);\n+\tif (!pmd_present(pmd_k)) {\n \t\tno_context(regs, addr);\n \t\treturn;\n \t}\n-\tif (pmd_leaf(pmdp_get(pmd_k)))\n+\tif (CONFIG_PGTABLE_LEVELS == 2)\n+\t\tset_pmd(pmdp, pmd_k);\n+\tif (pmd_leaf(pmd_k))\n \t\tgoto flush_tlb;\n \n \t/*\n@@ -230,8 +244,8 @@ static inline void vmalloc_fault(struct pt_regs *regs, int code, unsigned long a\n \t * addresses. If we don't do this, this will just\n \t * silently loop forever.\n \t */\n-\tpte_k = pte_offset_kernel(pmd_k, addr);\n-\tif (!pte_present(ptep_get(pte_k))) {\n+\tptep_k = pte_offset_kernel(pmdp_k, addr);\n+\tif (!pte_present(ptep_get(ptep_k))) {\n \t\tno_context(regs, addr);\n \t\treturn;\n \t}\ndiff --git a/arch/x86/include/asm/pgtable.h b/arch/x86/include/asm/pgtable.h\nindex 000fc6c1ca37c4..a2770821218783 100644\n--- a/arch/x86/include/asm/pgtable.h\n+++ b/arch/x86/include/asm/pgtable.h\n@@ -1167,6 +1167,20 @@ static inline int pgd_none(pgd_t pgd)\n #ifndef __ASSEMBLER__\n \n extern int direct_gbpages;\n+static inline bool direct_gbpages_enabled(void)\n+{\n+\t/* Check the direct map config option: */\n+\tif (!IS_ENABLED(CONFIG_X86_DIRECT_GBPAGES))\n+\t\treturn false;\n+\n+\t/* Check the CPU feature: */\n+\tif (!boot_cpu_has(X86_FEATURE_GBPAGES))\n+\t\treturn false;\n+\n+\t/* Check the command-line and early setup variable: */\n+\treturn direct_gbpages;\n+}\n+\n void init_mem_mapping(void);\n void early_alloc_pgt_buf(void);\n void __init poking_init(void);\ndiff --git a/arch/x86/kernel/cpu/common.c b/arch/x86/kernel/cpu/common.c\nindex 9bd50c68cd7e5e..12a51dba166276 100644\n--- a/arch/x86/kernel/cpu/common.c\n+++ b/arch/x86/kernel/cpu/common.c\n@@ -2655,7 +2655,7 @@ void __init arch_cpu_finalize_init(void)\n \t\t * Right now we don't do that with gbpages because there seems\n \t\t * very little benefit for that case.\n \t\t */\n-\t\tif (!direct_gbpages)\n+\t\tif (!direct_gbpages_enabled())\n \t\t\tset_memory_4k((unsigned long)__va(0), 1);\n \t} else {\n \t\tfpu__init_check_bugs();\ndiff --git a/arch/x86/kernel/machine_kexec_64.c b/arch/x86/kernel/machine_kexec_64.c\nindex c3f4a389992daa..0da0e89f2611a5 100644\n--- a/arch/x86/kernel/machine_kexec_64.c\n+++ b/arch/x86/kernel/machine_kexec_64.c\n@@ -257,7 +257,7 @@ static int init_pgtable(struct kimage *image, unsigned long control_page)\n \t\tinfo.kernpg_flag |= _PAGE_ENC;\n \t}\n \n-\tif (direct_gbpages)\n+\tif (direct_gbpages_enabled())\n \t\tinfo.direct_gbpages = true;\n \n \tfor (i = 0; i \u003c nr_pfn_mapped; i++) {\ndiff --git a/arch/x86/mm/dump_pagetables.c b/arch/x86/mm/dump_pagetables.c\nindex 2afa7a23340e92..2acbe41eb58f26 100644\n--- a/arch/x86/mm/dump_pagetables.c\n+++ b/arch/x86/mm/dump_pagetables.c\n@@ -38,6 +38,7 @@ struct pg_state {\n \tbool check_wx;\n \tunsigned long wx_pages;\n \tstruct seq_file *seq;\n+\tstruct mm_struct *mm;\n };\n \n struct addr_marker {\n@@ -248,13 +249,13 @@ static void note_wx(struct pg_state *st, unsigned long addr)\n \t\t  (void *)st-\u003estart_address);\n }\n \n-static void effective_prot(struct ptdump_state *pt_st, int level, u64 val)\n+static void effective_prot(struct ptdump_state *pt_st, enum pgtable_level level, u64 val)\n {\n \tstruct pg_state *st = container_of(pt_st, struct pg_state, ptdump);\n \tpgprotval_t prot = val \u0026 PTE_FLAGS_MASK;\n \tpgprotval_t effective;\n \n-\tif (level \u003e 0) {\n+\tif (level \u003e mm_first_pgtable_level(st-\u003emm)) {\n \t\tpgprotval_t higher_prot = st-\u003eprot_levels[level - 1];\n \n \t\teffective = (higher_prot \u0026 prot \u0026 (_PAGE_USER | _PAGE_RW)) |\n@@ -268,27 +269,27 @@ static void effective_prot(struct ptdump_state *pt_st, int level, u64 val)\n \n static void effective_prot_pte(struct ptdump_state *st, pte_t pte)\n {\n-\teffective_prot(st, 4, pte_val(pte));\n+\teffective_prot(st, PGTABLE_LEVEL_PTE, pte_val(pte));\n }\n \n static void effective_prot_pmd(struct ptdump_state *st, pmd_t pmd)\n {\n-\teffective_prot(st, 3, pmd_val(pmd));\n+\teffective_prot(st, PGTABLE_LEVEL_PMD, pmd_val(pmd));\n }\n \n static void effective_prot_pud(struct ptdump_state *st, pud_t pud)\n {\n-\teffective_prot(st, 2, pud_val(pud));\n+\teffective_prot(st, PGTABLE_LEVEL_PUD, pud_val(pud));\n }\n \n static void effective_prot_p4d(struct ptdump_state *st, p4d_t p4d)\n {\n-\teffective_prot(st, 1, p4d_val(p4d));\n+\teffective_prot(st, PGTABLE_LEVEL_P4D, p4d_val(p4d));\n }\n \n static void effective_prot_pgd(struct ptdump_state *st, pgd_t pgd)\n {\n-\teffective_prot(st, 0, pgd_val(pgd));\n+\teffective_prot(st, PGTABLE_LEVEL_PGD, pgd_val(pgd));\n }\n \n \n@@ -452,7 +453,8 @@ bool ptdump_walk_pgd_level_core(struct seq_file *m,\n \t\t.level = -1,\n \t\t.to_dmesg\t= dmesg,\n \t\t.check_wx\t= checkwx,\n-\t\t.seq\t\t= m\n+\t\t.seq\t\t= m,\n+\t\t.mm\t\t= mm,\n \t};\n \n \tptdump_walk_pgd(\u0026st.ptdump, mm, pgd);\ndiff --git a/arch/x86/mm/init.c b/arch/x86/mm/init.c\nindex 079f8c7e9e3cde..f0f4a06584c61c 100644\n--- a/arch/x86/mm/init.c\n+++ b/arch/x86/mm/init.c\n@@ -251,7 +251,7 @@ static void __init probe_page_size_mask(void)\n \t\t__default_kernel_pte_mask \u0026= ~_PAGE_GLOBAL;\n \n \t/* Enable 1 GB linear kernel mappings if available: */\n-\tif (direct_gbpages \u0026\u0026 boot_cpu_has(X86_FEATURE_GBPAGES)) {\n+\tif (direct_gbpages_enabled()) {\n \t\tprintk(KERN_INFO \"Using GB pages for direct mapping\\n\");\n \t\tpage_size_mask |= 1 \u003c\u003c PG_LEVEL_1G;\n \t} else {\ndiff --git a/arch/x86/mm/pat/set_memory.c b/arch/x86/mm/pat/set_memory.c\nindex b1d5c5a0ffb41d..f27811e6e00bd7 100644\n--- a/arch/x86/mm/pat/set_memory.c\n+++ b/arch/x86/mm/pat/set_memory.c\n@@ -130,7 +130,7 @@ void arch_report_meminfo(struct seq_file *m)\n \tseq_printf(m, \"DirectMap4M:    %8lu kB\\n\",\n \t\t\tdirect_pages_count[PG_LEVEL_2M] \u003c\u003c 12);\n #endif\n-\tif (direct_gbpages)\n+\tif (direct_gbpages_enabled())\n \t\tseq_printf(m, \"DirectMap1G:    %8lu kB\\n\",\n \t\t\tdirect_pages_count[PG_LEVEL_1G] \u003c\u003c 20);\n }\n@@ -1355,7 +1355,7 @@ static int collapse_pud_page(pud_t *pud, unsigned long addr,\n \tpmd_t *pmd, first;\n \tint i;\n \n-\tif (!direct_gbpages)\n+\tif (!direct_gbpages_enabled())\n \t\treturn 0;\n \n \taddr \u0026= PUD_MASK;\n@@ -1737,7 +1737,7 @@ static int populate_pud(struct cpa_data *cpa, unsigned long start, p4d_t *p4d,\n \t/*\n \t * Map everything starting from the Gb boundary, possibly with 1G pages\n \t */\n-\twhile (boot_cpu_has(X86_FEATURE_GBPAGES) \u0026\u0026 end - start \u003e= PUD_SIZE) {\n+\twhile (direct_gbpages_enabled() \u0026\u0026 end - start \u003e= PUD_SIZE) {\n \t\tset_pud(pud, pud_mkhuge(pfn_pud(cpa-\u003epfn,\n \t\t\t\t   canon_pgprot(pud_pgprot))));\n \ndiff --git a/include/asm-generic/pgtable-nop4d.h b/include/asm-generic/pgtable-nop4d.h\nindex 1cf739ee38aa15..12b01768134b5a 100644\n--- a/include/asm-generic/pgtable-nop4d.h\n+++ b/include/asm-generic/pgtable-nop4d.h\n@@ -22,25 +22,61 @@ static inline int pgd_none(pgd_t pgd)\t\t{ return 0; }\n static inline int pgd_bad(pgd_t pgd)\t\t{ return 0; }\n static inline int pgd_present(pgd_t pgd)\t{ return 1; }\n static inline void pgd_clear(pgd_t *pgd)\t{ }\n+static inline bool pgd_leaf(pgd_t pgd)\t\t{ return false; }\n+#define pgd_leaf pgd_leaf\n+#define p4d_ERROR(p4d)\t\t\t\t(pgd_ERROR((p4d).pgd))\n \n #define pgd_populate(mm, pgd, p4d)\t\tdo { } while (0)\n #define pgd_populate_safe(mm, pgd, p4d)\t\tdo { } while (0)\n-/*\n- * (p4ds are folded into pgds so this doesn't get actually called,\n- * but the define is needed for a generic inline function.)\n- */\n-#define set_pgd(pgdptr, pgdval)\tset_p4d((p4d_t *)(pgdptr), (p4d_t) { pgdval })\n \n-static inline p4d_t *p4d_offset(pgd_t *pgd, unsigned long address)\n+#define set_pgd(pgdptr, pgdval)\t\t\t\t\t\t\\\n+({\t\t\t\t\t\t\t\t\t\\\n+\tpgd_check_dummy(pgdval);\t\t\t\t\t\\\n+\tset_p4d((p4d_t *)(pgdptr), (p4d_t) { pgdval });\t\t\t\\\n+})\n+\n+static __always_inline pgd_t pgdp_get(pgd_t *pgdp)\n {\n-\treturn (p4d_t *)pgd;\n+\tpgd_t dummy = { 0 };\n+\n+\treturn dummy;\n }\n+#define pgdp_get pgdp_get\n+\n+#define pgd_check_dummy(pgd) BUILD_BUG_ON(__builtin_constant_p(pgd_val(pgd)))\n+\n+static __always_inline p4d_t *__p4d_offset(pgd_t *pgdp, unsigned long address)\n+{\n+\treturn (p4d_t *)pgdp;\n+}\n+\n+#define p4d_offset(pgdp, address)\t\t\t\t\t\\\n+({\t\t\t\t\t\t\t\t\t\\\n+\tpgd_check_dummy(*(pgdp));\t\t\t\t\t\\\n+\t__p4d_offset(pgdp, address);\t\t\t\t\t\\\n+})\n+\n+static __always_inline p4d_t *__p4d_offset_lockless(pgd_t *pgdp, pgd_t pgd,\n+\t\tunsigned long address)\n+{\n+\treturn (p4d_t *)pgdp;\n+}\n+\n+#define p4d_offset_lockless(pgdp, pgd, address)\t\t\t\t\\\n+({\t\t\t\t\t\t\t\t\t\\\n+\tpgd_check_dummy(*(pgdp));\t\t\t\t\t\\\n+\t__p4d_offset_lockless(pgdp, pgd, address);\t\t\t\\\n+})\n \n #define p4d_val(x)\t\t\t\t(pgd_val((x).pgd))\n #define __p4d(x)\t\t\t\t((p4d_t) { __pgd(x) })\n \n-#define pgd_page(pgd)\t\t\t\t(p4d_page((p4d_t){ pgd }))\n-#define pgd_page_vaddr(pgd)\t\t\t((unsigned long)(p4d_pgtable((p4d_t){ pgd })))\n+#define pgd_page(pgd)\t\t\t\t({ BUILD_BUG(); (struct page *)NULL; })\n+#define pgd_page_vaddr(pgd)\t\t\t\t\t\t\\\n+({ \t\t\t\t\t\t\t\t\t\\\n+\tpgd_check_dummy(pgd);\t\t\t\t\t\t\\\n+\t(unsigned long)p4d_pgtable((p4d_t) { pgd });\t\t\t\\\n+})\n \n /*\n  * allocating and freeing a p4d is trivial: the 1-entry p4d is\ndiff --git a/include/asm-generic/pgtable-nopmd.h b/include/asm-generic/pgtable-nopmd.h\nindex ff4235cf84d772..11b04c10712957 100644\n--- a/include/asm-generic/pgtable-nopmd.h\n+++ b/include/asm-generic/pgtable-nopmd.h\n@@ -31,28 +31,60 @@ static inline int pud_none(pud_t pud)\t\t{ return 0; }\n static inline int pud_bad(pud_t pud)\t\t{ return 0; }\n static inline int pud_present(pud_t pud)\t{ return 1; }\n static inline int pud_user(pud_t pud)\t\t{ return 0; }\n-static inline int pud_leaf(pud_t pud)\t\t{ return 0; }\n+static inline bool pud_leaf(pud_t pud)\t\t{ return false; }\n+#define pud_leaf pud_leaf\n static inline void pud_clear(pud_t *pud)\t{ }\n \n #define pud_populate(mm, pmd, pte)\t\tdo { } while (0)\n \n-/*\n- * (pmds are folded into puds so this doesn't get actually called,\n- * but the define is needed for a generic inline function.)\n- */\n-#define set_pud(pudptr, pudval)\t\t\tset_pmd((pmd_t *)(pudptr), (pmd_t) { pudval })\n+#define set_pud(pudptr, pudval)\t\t\t\t\t\t\\\n+({\t\t\t\t\t\t\t\t\t\\\n+\tpud_check_dummy(pudval);\t\t\t\t\t\\\n+\tset_pmd((pmd_t *)(pudptr), (pmd_t) { pudval });\t\t\t\\\n+})\n+\n+static __always_inline pud_t pudp_get(pud_t *pudp)\n+{\n+\tpud_t dummy = { 0 };\n+\n+\treturn dummy;\n+}\n+#define pudp_get pudp_get\n \n-static inline pmd_t * pmd_offset(pud_t * pud, unsigned long address)\n+#define pud_check_dummy(pud) BUILD_BUG_ON(__builtin_constant_p(pud_val(pud)))\n+\n+static __always_inline pmd_t *__pmd_offset(pud_t *pudp, unsigned long address)\n+{\n+\treturn (pmd_t *)pudp;\n+}\n+\n+#define pmd_offset(pudp, address)\t\t\t\t\t\\\n+({\t\t\t\t\t\t\t\t\t\\\n+\tpud_check_dummy(*(pudp));\t\t\t\t\t\\\n+\t__pmd_offset(pudp, address);\t\t\t\t\t\\\n+})\n+\n+static __always_inline pmd_t *__pmd_offset_lockless(pud_t *pudp, pud_t pud,\n+\t\tunsigned long address)\n {\n-\treturn (pmd_t *)pud;\n+\treturn (pmd_t *)pudp;\n }\n-#define pmd_offset pmd_offset\n+\n+#define pmd_offset_lockless(pudp, pud, address)\t\t\t\t\\\n+({\t\t\t\t\t\t\t\t\t\\\n+\tpud_check_dummy(*(pudp));\t\t\t\t\t\\\n+\t__pmd_offset_lockless(pudp, pud, address);\t\t\t\\\n+})\n \n #define pmd_val(x)\t\t\t\t(pud_val((x).pud))\n #define __pmd(x)\t\t\t\t((pmd_t) { __pud(x) } )\n \n-#define pud_page(pud)\t\t\t\t(pmd_page((pmd_t){ pud }))\n-#define pud_pgtable(pud)\t\t\t((pmd_t *)(pmd_page_vaddr((pmd_t){ pud })))\n+#define pud_page(pud)\t\t\t\t({ BUILD_BUG(); (struct page *)NULL; })\n+#define pud_pgtable(pud)\t\t\t\t\t\t\\\n+({\t\t\t\t\t\t\t\t\t\\\n+\tpud_check_dummy(pud);\t\t\t\t\t\t\\\n+\t((pmd_t *)(pmd_page_vaddr((pmd_t) { pud })));\t\t\t\\\n+})\n \n /*\n  * allocating and freeing a pmd is trivial: the 1-entry pmd is\ndiff --git a/include/asm-generic/pgtable-nopud.h b/include/asm-generic/pgtable-nopud.h\nindex eedee8e3ad68fd..5ee8f1852cd8c9 100644\n--- a/include/asm-generic/pgtable-nopud.h\n+++ b/include/asm-generic/pgtable-nopud.h\n@@ -29,26 +29,62 @@ static inline int p4d_none(p4d_t p4d)\t\t{ return 0; }\n static inline int p4d_bad(p4d_t p4d)\t\t{ return 0; }\n static inline int p4d_present(p4d_t p4d)\t{ return 1; }\n static inline void p4d_clear(p4d_t *p4d)\t{ }\n+static inline bool p4d_leaf(p4d_t p4d)\t\t{ return false; }\n+#define p4d_leaf p4d_leaf\n+#define pud_ERROR(pud)\t\t\t\t(p4d_ERROR((pud).p4d))\n \n #define p4d_populate(mm, p4d, pud)\t\tdo { } while (0)\n #define p4d_populate_safe(mm, p4d, pud)\t\tdo { } while (0)\n-/*\n- * (puds are folded into p4ds so this doesn't get actually called,\n- * but the define is needed for a generic inline function.)\n- */\n-#define set_p4d(p4dptr, p4dval)\tset_pud((pud_t *)(p4dptr), (pud_t) { p4dval })\n \n-static inline pud_t *pud_offset(p4d_t *p4d, unsigned long address)\n+#define set_p4d(p4dptr, p4dval)\t\t\t\t\t\t\\\n+({\t\t\t\t\t\t\t\t\t\\\n+\tp4d_check_dummy(p4dval);\t\t\t\t\t\\\n+\tset_pud((pud_t *)(p4dptr), (pud_t) { p4dval });\t\t\t\\\n+})\n+\n+\n+static __always_inline p4d_t p4dp_get(p4d_t *p4dp)\n+{\n+\tp4d_t dummy = { 0 };\n+\n+\treturn dummy;\n+}\n+#define p4dp_get p4dp_get\n+\n+#define p4d_check_dummy(p4d) BUILD_BUG_ON(__builtin_constant_p(p4d_val(p4d)))\n+\n+static __always_inline pud_t *__pud_offset(p4d_t *p4dp, unsigned long address)\n {\n-\treturn (pud_t *)p4d;\n+\treturn (pud_t *)p4dp;\n }\n-#define pud_offset pud_offset\n+\n+#define pud_offset(p4dp, address)\t\t\t\t\t\\\n+({\t\t\t\t\t\t\t\t\t\\\n+\tp4d_check_dummy(*(p4dp));\t\t\t\t\t\\\n+\t__pud_offset(p4dp, address);\t\t\t\t\t\\\n+})\n+\n+static __always_inline pud_t *__pud_offset_lockless(p4d_t *p4dp, p4d_t p4d,\n+\t\tunsigned long address)\n+{\n+\treturn (pud_t *)p4dp;\n+}\n+\n+#define pud_offset_lockless(p4dp, p4d, address)\t\t\t\t\\\n+({\t\t\t\t\t\t\t\t\t\\\n+\tp4d_check_dummy(*(p4dp));\t\t\t\t\t\\\n+\t__pud_offset_lockless(p4dp, p4d, address);\t\t\t\\\n+})\n \n #define pud_val(x)\t\t\t\t(p4d_val((x).p4d))\n #define __pud(x)\t\t\t\t((pud_t) { __p4d(x) })\n \n-#define p4d_page(p4d)\t\t\t\t(pud_page((pud_t){ p4d }))\n-#define p4d_pgtable(p4d)\t\t\t((pud_t *)(pud_pgtable((pud_t){ p4d })))\n+#define p4d_page(p4d)\t\t\t\t({ BUILD_BUG(); (struct page *)NULL; })\n+#define p4d_pgtable(p4d)\t\t\t\t\t\t\\\n+({\t\t\t\t\t\t\t\t\t\\\n+\tp4d_check_dummy(p4d);\t\t\t\t\t\t\\\n+\t((pud_t *)(pud_pgtable((pud_t) { p4d })));\t\t\t\\\n+})\n \n /*\n  * allocating and freeing a pud is trivial: the 1-entry pud is\ndiff --git a/include/linux/pgtable.h b/include/linux/pgtable.h\nindex 780fe849ff8b7e..b0d0e32ad6aef4 100644\n--- a/include/linux/pgtable.h\n+++ b/include/linux/pgtable.h\n@@ -392,6 +392,22 @@ static inline void lazy_mmu_mode_pause(void) {}\n static inline void lazy_mmu_mode_resume(void) {}\n #endif\n \n+/*\n+ * On some architectures it depends on the mm if the p4d/pud or pmd\n+ * layer of the page table hierarchy is folded or not.\n+ */\n+#ifndef mm_p4d_folded\n+#define mm_p4d_folded(mm)\t__is_defined(__PAGETABLE_P4D_FOLDED)\n+#endif\n+\n+#ifndef mm_pud_folded\n+#define mm_pud_folded(mm)\t__is_defined(__PAGETABLE_PUD_FOLDED)\n+#endif\n+\n+#ifndef mm_pmd_folded\n+#define mm_pmd_folded(mm)\t__is_defined(__PAGETABLE_PMD_FOLDED)\n+#endif\n+\n #ifndef pte_batch_hint\n /**\n  * pte_batch_hint - Number of pages that can be added to batch without scanning.\n@@ -2288,11 +2304,11 @@ static inline bool arch_has_pfn_modify_check(void)\n typedef unsigned int pgtbl_mod_mask;\n \n enum pgtable_level {\n-\tPGTABLE_LEVEL_PTE = 0,\n-\tPGTABLE_LEVEL_PMD,\n-\tPGTABLE_LEVEL_PUD,\n+\tPGTABLE_LEVEL_PGD = 0,\n \tPGTABLE_LEVEL_P4D,\n-\tPGTABLE_LEVEL_PGD,\n+\tPGTABLE_LEVEL_PUD,\n+\tPGTABLE_LEVEL_PMD,\n+\tPGTABLE_LEVEL_PTE,\n };\n \n static inline const char *pgtable_level_to_str(enum pgtable_level level)\n@@ -2313,6 +2329,36 @@ static inline const char *pgtable_level_to_str(enum pgtable_level level)\n \t}\n }\n \n+#ifdef CONFIG_MMU\n+/**\n+ * mm_first_pgtable_level - return the first non-folded page-table level\n+ * @mm: The mm structure.\n+ *\n+ * With runtime- or compile-time folded page tables, page-table walkers\n+ * effectively skip the folded top-level page tables, treating them as\n+ * always-present entries pointing to the next lower-level page table.\n+ *\n+ * Return the first page-table level in @mm that is not folded.\n+ *\n+ * Return: the first non-folded page-table level.\n+ */\n+static inline enum pgtable_level mm_first_pgtable_level(struct mm_struct *mm)\n+{\n+\t/*\n+\t * \"PMD folded\" actually means \"PMD is folded into PUD\", and\n+\t * pud_present() etc. are hard-coded to look like present page table\n+\t * levels, whereby pmd_present() etc. contain real logic.\n+\t */\n+\tif (mm_pmd_folded(mm))\n+\t\treturn PGTABLE_LEVEL_PMD;\n+\tif (mm_pud_folded(mm))\n+\t\treturn PGTABLE_LEVEL_PUD;\n+\tif (mm_p4d_folded(mm))\n+\t\treturn PGTABLE_LEVEL_P4D;\n+\treturn PGTABLE_LEVEL_PGD;\n+}\n+#endif /* CONFIG_MMU */\n+\n void ptval_bytes_to_hex_str(char *buf, size_t buf_size, const void *entry, size_t entry_size);\n \n #define ptval_to_str(buf, val)\t\t\t\t\t\t\t\t\\\n@@ -2349,21 +2395,6 @@ void ptval_bytes_to_hex_str(char *buf, size_t buf_size, const void *entry, size_\n #ifndef has_transparent_pud_hugepage\n #define has_transparent_pud_hugepage() IS_BUILTIN(CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD)\n #endif\n-/*\n- * On some architectures it depends on the mm if the p4d/pud or pmd\n- * layer of the page table hierarchy is folded or not.\n- */\n-#ifndef mm_p4d_folded\n-#define mm_p4d_folded(mm)\t__is_defined(__PAGETABLE_P4D_FOLDED)\n-#endif\n-\n-#ifndef mm_pud_folded\n-#define mm_pud_folded(mm)\t__is_defined(__PAGETABLE_PUD_FOLDED)\n-#endif\n-\n-#ifndef mm_pmd_folded\n-#define mm_pmd_folded(mm)\t__is_defined(__PAGETABLE_PMD_FOLDED)\n-#endif\n \n #ifndef p4d_offset_lockless\n #define p4d_offset_lockless(pgdp, pgd, address) p4d_offset(\u0026(pgd), address)\ndiff --git a/mm/vmscan.c b/mm/vmscan.c\nindex 9959ca158ce835..6a1af9b92349fb 100644\n--- a/mm/vmscan.c\n+++ b/mm/vmscan.c\n@@ -3911,7 +3911,7 @@ static void walk_pmd_range(pud_t *pud, unsigned long start, unsigned long end,\n \n \t\twalk-\u003emm_stats[MM_NONLEAF_FOUND]++;\n \n-\t\tif (!walk_pte_range(\u0026val, addr, next, args))\n+\t\tif (!walk_pte_range(pmd + i, addr, next, args))\n \t\t\tcontinue;\n \n \t\twalk-\u003emm_stats[MM_NONLEAF_ADDED]++;\n@@ -3947,7 +3947,7 @@ static int walk_pud_range(p4d_t *p4d, unsigned long start, unsigned long end,\n \t\tif (!pud_present(val) || WARN_ON_ONCE(pud_leaf(val)))\n \t\t\tcontinue;\n \n-\t\twalk_pmd_range(\u0026val, addr, next, args);\n+\t\twalk_pmd_range(pud + i, addr, next, args);\n \n \t\tif (need_resched() || walk-\u003ebatched \u003e= MAX_LRU_BATCH) {\n \t\t\tend = (addr | ~PUD_MASK) + 1;\n"
}

2/1 2026/11/02 21:59 agent patch-evaluator
13m Model:
core

Args:
null

Results:
{
  "EnableConfigs": [
    "LRU_GEN",
    "LRU_GEN_ENABLED",
    "PTDUMP_DEBUGFS",
    "X86_DIRECT_GBPAGES"
  ],
  "FocusSymbols": [
    "walk_pud_range",
    "walk_pmd_range",
    "ptdump_walk_pgd_level_core",
    "collapse_pud_page",
    "populate_pud"
  ],
  "Reasoning": "The patch refactors page-table folding definitions across asm-generic and architecture headers, addressing cases where addresses of stack variables were incorrectly passed to page-table walkers. In mm/vmscan.c (MGLRU), walk_pud_range() and walk_pmd_range() are modified to pass proper page table pointers to lower-level walkers. On x86, arch/x86/mm/dump_pagetables.c is updated to use the reordered pgtable_level enum and the new mm_first_pgtable_level() helper to determine effective permissions, and arch/x86/mm/pat/set_memory.c adopts the new direct_gbpages_enabled() helper in CPA logic. Because these modifications alter core memory management and page-table walking logic in reachable paths on x86_64, fuzzing is warranted to uncover potential invariant violations or regressions.",
  "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 a0a56350684d753b1a84f5f158b0a4d9cd6c69a5
Author: syz-cluster <triage@syzkaller.com>
Date:   Mon Nov 2 21:59:07 2026 +0000

    syz-cluster: applied patch under review

diff --git a/Documentation/mm/page_tables.rst b/Documentation/mm/page_tables.rst
index 126c8762825092..d4f3e2e343ca2b 100644
--- a/Documentation/mm/page_tables.rst
+++ b/Documentation/mm/page_tables.rst
@@ -143,15 +143,81 @@ pointers on each level is architecture-defined.::
 Page Table Folding
 ==================
 
-If the architecture does not use all the page table levels, they can be *folded*
-which means skipped, and all operations performed on page tables will be
-compile-time augmented to just skip a level when accessing the next lower
-level.
-
-Page table handling code that wishes to be architecture-neutral, such as the
-virtual memory manager, will need to be written so that it traverses all of the
-currently five levels. This style should also be preferred for
-architecture-specific code, so as to be robust to future changes.
+Not all architectures support 5-level page tables; while for some of them
+the exact number of supported page table levels is known at compile time,
+others can determine the number of page table levels at runtime based on
+hardware support and address space sizes.
+
+Generic page table walking code always assumes that 5 levels of page table
+exist. To make page table walking code not have to worry about that,
+`compile-time folding` and `runtime folding` of page tables are used.
+Compile-time folding is mostly handled in common code, whereas runtime folding
+is exclusively handled in architecture code.
+
+This description focuses on generic compile-time folded page tables; for
+architecture-specific variants, some details can vary, however, without
+affecting common page table walkers.
+
+When walking folded page tables, all upper page table levels up to the supported
+level are skipped in page table walkers: this is achieved by (a) treating
+entries in upper page table levels as present and pointing at a page table; and
+(b) having page table walkers cast the entry pointer to the next-level entry
+instead of dereferencing that table. From the perspective of a page table
+walker, the entry points at itself.
+
+Assuming compile-time folded 4-level page tables, to achieve (a), pgd_present()
+and pgd_leaf() are hard-coded to indicate a present page table entry that
+points at a page table, and to achieve (b) p4d_offset() and
+p4d_offset_lockless() simply cast the page table entry pointer to the next
+lower level.
+
+In the current design, this is further modeled by having the P4D have a
+single page table entry::
+
+        PGD
+  --> +------+          NOP4D
+      | ptr0 |-------> +------+           PUD
+      | ptr1 |-        | ptr0 |-------> +-----+
+      | ptr2 | \       +------+         | ptr |-------> ...
+      | ptr3 |  \                       | ptr |
+        ...      \                        ..
+                  \        NOP4D
+                   +----> +------+           PUD
+                          | ptr1 |-------> +-----+
+                          +------+         | ptr |-------> ...
+                                           | ptr |
+                                             ...
+
+Note that the arrows from PGD to NOP4D represent page-table-walker
+transitions, not pointers stored in the pgd entries.
+
+Using p4d as an example, `nop4d`/`p4d folded` translates to the following:
+
+- p4d is considered folded into pgd; both are operating on the same page
+  table.
+
+- Most pgd_* helpers are hard-coded dummy functions that ignore the passed
+  pgd_t values entirely. Exceptions are pgd_val() and low-level helpers
+  set_pgd() + pgd_page_vaddr(), which effectively translate to set_p4d()/
+  p4d_pgtable() to keep existing arch code working.
+
+  Architectures must provide p4d_* helpers (unless further common
+  compile-time folding applies).
+
+- PTRS_PER_P4D is hard-coded to 1. Architectures must define PTRS_PER_PGD.
+
+To avoid reading a value that will never be used but cannot be entirely
+optimized out, compile-time folded page table code also makes pXdp_get()
+return a constant dummy value.
+
+In common code, this only affects pXd_val() when used for printing page
+table entries for debugging purposes. As we don't want architecture code
+that uses set_pXd(), pgd_page_vaddr() or pXd_pgtable() to accidentally
+operate on dummy values, the compiler will error out if it detects that the
+helpers are used with dummy values. For a folded level, pXd_page() must not
+be used and unconditionally triggers a compiler error. Architecture code must
+instead call the helpers on the proper first page table level: e.g., set_p4d()
+instead of set_pgd().
 
 
 MMU, TLB, and Page Faults
diff --git a/arch/arm/include/asm/page-nommu.h b/arch/arm/include/asm/page-nommu.h
index e74415c959beac..88659b38e23649 100644
--- a/arch/arm/include/asm/page-nommu.h
+++ b/arch/arm/include/asm/page-nommu.h
@@ -18,12 +18,12 @@
  */
 typedef unsigned long pte_t;
 typedef unsigned long pmd_t;
-typedef unsigned long pgd_t[2];
+typedef unsigned long pgd_t;
 typedef unsigned long pgprot_t;
 
 #define pte_val(x)      (x)
 #define pmd_val(x)      (x)
-#define pgd_val(x)	((x)[0])
+#define pgd_val(x)      (x)
 #define pgprot_val(x)   (x)
 
 #define __pte(x)        (x)
diff --git a/arch/arm/include/asm/pgtable-2level-types.h b/arch/arm/include/asm/pgtable-2level-types.h
index 650e793f41429f..805044ea315b7d 100644
--- a/arch/arm/include/asm/pgtable-2level-types.h
+++ b/arch/arm/include/asm/pgtable-2level-types.h
@@ -25,7 +25,7 @@ typedef struct { pteval_t pgprot; } pgprot_t;
 
 #define pte_val(x)      ((x).pte)
 #define pmd_val(x)      ((x).pmd)
-#define pgd_val(x)	((x).pgd[0])
+#define pgd_val(x)      ((x).pgd[0])
 #define pgprot_val(x)   ((x).pgprot)
 
 #define __pte(x)        ((pte_t) { (x) } )
@@ -36,14 +36,28 @@ typedef struct { pteval_t pgprot; } pgprot_t;
 /*
  * .. while these make it easier on the compiler
  */
+typedef u64 pgdval_t;
+
 typedef pteval_t pte_t;
 typedef pmdval_t pmd_t;
-typedef pmdval_t pgd_t[2];
+typedef pgdval_t pgd_t;
 typedef pteval_t pgprot_t;
 
 #define pte_val(x)      (x)
 #define pmd_val(x)      (x)
-#define pgd_val(x)	((x)[0])
+
+static inline pmdval_t pgd_val(pgd_t pgd)
+{
+	/*
+	 * A PGD entry actually corresponds to two PMD entries in the PMD table.
+	 * Both PMD entries point to PTE tables residing in the same physical page,
+	 * but at different offsets. See include/asm/pgtable-2level.h for details.
+	 *
+	 * Historically, pgd_val() has returned the lower of the two PMD entries.
+	 */
+	return (*(pmdval_t (*)[2])&pgd)[0];
+}
+
 #define pgprot_val(x)   (x)
 
 #define __pte(x)        (x)
diff --git a/arch/arm/include/asm/pgtable.h b/arch/arm/include/asm/pgtable.h
index 8dd17d20faa33f..ff3868acea9454 100644
--- a/arch/arm/include/asm/pgtable.h
+++ b/arch/arm/include/asm/pgtable.h
@@ -134,8 +134,6 @@ extern pgprot_t phys_mem_access_prot(struct file *file, unsigned long pfn,
 
 extern pgd_t swapper_pg_dir[PTRS_PER_PGD];
 
-#define pgdp_get(pgpd)		READ_ONCE(*pgdp)
-
 #define pud_page(pud)		pmd_page(__pmd(pud_val(pud)))
 #define pud_write(pud)		pmd_write(__pmd(pud_val(pud)))
 
diff --git a/arch/arm64/include/asm/pgtable.h b/arch/arm64/include/asm/pgtable.h
index 763c5a411d64e3..b391f325670f81 100644
--- a/arch/arm64/include/asm/pgtable.h
+++ b/arch/arm64/include/asm/pgtable.h
@@ -1141,29 +1141,6 @@ static inline bool pgtable_l5_enabled(void) { return false; }
 #define p4d_clear_fixmap()
 
 #define p4d_offset_kimg(dir,addr)	((p4d_t *)dir)
-
-static inline
-p4d_t *p4d_offset_lockless_folded(pgd_t *pgdp, pgd_t pgd, unsigned long addr)
-{
-	/*
-	 * With runtime folding of the pud, pud_offset_lockless() passes
-	 * the 'pgd_t *' we return here to p4d_to_folded_pud(), which
-	 * will offset the pointer assuming that it points into
-	 * a page-table page. However, the fast GUP path passes us a
-	 * pgd_t allocated on the stack and so we must use the original
-	 * pointer in 'pgdp' to construct the p4d pointer instead of
-	 * using the generic p4d_offset_lockless() implementation.
-	 *
-	 * Note: reusing the original pointer means that we may
-	 * dereference the same (live) page-table entry multiple times.
-	 * This is safe because it is still only loaded once in the
-	 * context of each level and the CPU guarantees same-address
-	 * read-after-read ordering.
-	 */
-	return p4d_offset(pgdp, addr);
-}
-#define p4d_offset_lockless p4d_offset_lockless_folded
-
 #endif  /* CONFIG_PGTABLE_LEVELS > 4 */
 
 #define pgd_set_fixmap(addr)	((pgd_t *)set_fixmap_offset(FIX_PGD, addr))
diff --git a/arch/loongarch/include/asm/pgtable.h b/arch/loongarch/include/asm/pgtable.h
index f876031351319a..9a62b87c246de0 100644
--- a/arch/loongarch/include/asm/pgtable.h
+++ b/arch/loongarch/include/asm/pgtable.h
@@ -631,7 +631,9 @@ static inline long pmd_protnone(pmd_t pmd)
 #endif /* CONFIG_ARCH_HAS_PTE_PROTNONE */
 
 #define pmd_leaf(pmd)		((pmd_val(pmd) & _PAGE_HUGE) != 0)
+#ifndef __PAGETABLE_PMD_FOLDED
 #define pud_leaf(pud)		((pud_val(pud) & _PAGE_HUGE) != 0)
+#endif
 
 /*
  * We provide our own get_unmapped area to cope with the virtual aliasing
diff --git a/arch/loongarch/kvm/mmu.c b/arch/loongarch/kvm/mmu.c
index 64fddd9f0ada1e..ab0a06b73e8d38 100644
--- a/arch/loongarch/kvm/mmu.c
+++ b/arch/loongarch/kvm/mmu.c
@@ -679,10 +679,10 @@ static int host_pfn_mapping_level(struct kvm *kvm, gfn_t gfn,
 	int level = 0;
 	unsigned long hva;
 	unsigned long flags;
-	pgd_t pgd;
-	p4d_t p4d;
-	pud_t pud;
-	pmd_t pmd;
+	pgd_t *pgdp, pgd;
+	p4d_t *p4dp, p4d;
+	pud_t *pudp, pud;
+	pmd_t *pmdp, pmd;
 
 	/*
 	 * Note, using the already-retrieved memslot and __gfn_to_hva_memslot()
@@ -708,19 +708,23 @@ static int host_pfn_mapping_level(struct kvm *kvm, gfn_t gfn,
 	 * value) and then p*d_offset() walks into the target huge page instead
 	 * of the old page table (sees the new value).
 	 */
-	pgd = pgdp_get(pgd_offset(kvm->mm, hva));
+	pgdp = pgd_offset(kvm->mm, hva);
+	pgd = pgdp_get(pgdp);
 	if (pgd_none(pgd))
 		goto out;
 
-	p4d = p4dp_get(p4d_offset(&pgd, hva));
+	p4dp = p4d_offset_lockless(pgdp, pgd, hva);
+	p4d = p4dp_get(p4dp);
 	if (p4d_none(p4d) || !p4d_present(p4d))
 		goto out;
 
-	pud = pudp_get(pud_offset(&p4d, hva));
+	pudp = pud_offset_lockless(p4dp, p4d, hva);
+	pud = pudp_get(pudp);
 	if (pud_none(pud) || !pud_present(pud))
 		goto out;
 
-	pmd = pmdp_get(pmd_offset(&pud, hva));
+	pmdp = pmd_offset_lockless(pudp, pud, hva);
+	pmd = pmdp_get(pmdp);
 	if (pmd_none(pmd) || !pmd_present(pmd))
 		goto out;
 
diff --git a/arch/mips/include/asm/pgtable.h b/arch/mips/include/asm/pgtable.h
index fa7b935f947ca0..151f4d15bd12bf 100644
--- a/arch/mips/include/asm/pgtable.h
+++ b/arch/mips/include/asm/pgtable.h
@@ -745,8 +745,10 @@ static inline pmd_t pmdp_huge_get_and_clear(struct mm_struct *mm,
 
 #ifdef _PAGE_HUGE
 #define pmd_leaf(pmd)	((pmd_val(pmd) & _PAGE_HUGE) != 0)
+#ifndef __PAGETABLE_PMD_FOLDED
 #define pud_leaf(pud)	((pud_val(pud) & _PAGE_HUGE) != 0)
 #endif
+#endif
 
 #define gup_fast_permitted(start, end)	(!cpu_has_dc_aliases)
 
diff --git a/arch/openrisc/include/asm/pgtable.h b/arch/openrisc/include/asm/pgtable.h
index 13afcc0bd8631b..7fceed641610c4 100644
--- a/arch/openrisc/include/asm/pgtable.h
+++ b/arch/openrisc/include/asm/pgtable.h
@@ -331,9 +331,6 @@ static inline unsigned long pmd_page_vaddr(pmd_t pmd)
 	return ((unsigned long) __va(pmd_val(pmd) & PAGE_MASK));
 }
 
-#define __pmd_offset(address) \
-	(((address) >> PMD_SHIFT) & (PTRS_PER_PMD-1))
-
 #define PFN_PTE_SHIFT		PAGE_SHIFT
 #define pte_pfn(x)		((unsigned long)(((x).pte)) >> PAGE_SHIFT)
 #define pfn_pte(pfn, prot)  __pte((((pfn) << PAGE_SHIFT)) | pgprot_val(prot))
diff --git a/arch/riscv/kvm/mmu.c b/arch/riscv/kvm/mmu.c
index 371fccaf9df08a..a062e8508d3f97 100644
--- a/arch/riscv/kvm/mmu.c
+++ b/arch/riscv/kvm/mmu.c
@@ -450,10 +450,10 @@ static int get_hva_mapping_size(struct kvm *kvm,
 {
 	int size = PAGE_SIZE;
 	unsigned long flags;
-	pgd_t pgd;
-	p4d_t p4d;
-	pud_t pud;
-	pmd_t pmd;
+	pgd_t *pgdp, pgd;
+	p4d_t *p4dp, p4d;
+	pud_t *pudp, pud;
+	pmd_t *pmdp, pmd;
 
 	/*
 	 * Disable IRQs to prevent concurrent tear down of host page tables,
@@ -469,15 +469,18 @@ static int get_hva_mapping_size(struct kvm *kvm,
 	 * value) and then p*d_offset() walks into the target huge page instead
 	 * of the old page table (sees the new value).
 	 */
-	pgd = pgdp_get(pgd_offset(kvm->mm, hva));
+	pgdp = pgd_offset(kvm->mm, hva);
+	pgd = pgdp_get(pgdp);
 	if (pgd_none(pgd))
 		goto out;
 
-	p4d = p4dp_get(p4d_offset(&pgd, hva));
+	p4dp = p4d_offset_lockless(pgdp, pgd, hva);
+	p4d = p4dp_get(p4dp);
 	if (p4d_none(p4d) || !p4d_present(p4d))
 		goto out;
 
-	pud = pudp_get(pud_offset(&p4d, hva));
+	pudp = pud_offset_lockless(p4dp, p4d, hva);
+	pud = pudp_get(pudp);
 	if (pud_none(pud) || !pud_present(pud))
 		goto out;
 
@@ -486,7 +489,8 @@ static int get_hva_mapping_size(struct kvm *kvm,
 		goto out;
 	}
 
-	pmd = pmdp_get(pmd_offset(&pud, hva));
+	pmdp = pmd_offset_lockless(pudp, pud, hva);
+	pmd = pmdp_get(pmdp);
 	if (pmd_none(pmd) || !pmd_present(pmd))
 		goto out;
 
diff --git a/arch/riscv/mm/fault.c b/arch/riscv/mm/fault.c
index 04ed6f8acae4fd..b2bcaf10d13f1d 100644
--- a/arch/riscv/mm/fault.c
+++ b/arch/riscv/mm/fault.c
@@ -167,14 +167,16 @@ bad_area(struct pt_regs *regs, struct mm_struct *mm, int code,
 
 static inline void vmalloc_fault(struct pt_regs *regs, int code, unsigned long addr)
 {
-	pgd_t *pgd, *pgd_k;
-	pud_t *pud_k;
-	p4d_t *p4d_k;
-	pmd_t *pmd_k;
-	pte_t *pte_k;
+	pgd_t *pgdp_k, *pgdp, pgd_k;
+	p4d_t *p4dp_k, *p4dp;
+	pud_t *pudp_k, *pudp, pud_k;
+	pmd_t *pmdp_k, *pmdp, pmd_k;
+	pte_t *ptep_k;
 	int index;
 	unsigned long pfn;
 
+	BUILD_BUG_ON(CONFIG_PGTABLE_LEVELS != 5 && CONFIG_PGTABLE_LEVELS != 2);
+
 	/* User mode accesses just cause a SIGSEGV */
 	if (user_mode(regs))
 		return do_trap(regs, SIGSEGV, code, addr);
@@ -189,39 +191,51 @@ static inline void vmalloc_fault(struct pt_regs *regs, int code, unsigned long a
 	 */
 	index = pgd_index(addr);
 	pfn = csr_read(CSR_SATP) & SATP_PPN;
-	pgd = (pgd_t *)pfn_to_virt(pfn) + index;
-	pgd_k = init_mm.pgd + index;
 
-	if (!pgd_present(pgdp_get(pgd_k))) {
+	pgdp = (pgd_t *)pfn_to_virt(pfn) + index;
+	pgdp_k = init_mm.pgd + index;
+
+	pgd_k = pgdp_get(pgdp_k);
+	if (!pgd_present(pgd_k)) {
 		no_context(regs, addr);
 		return;
 	}
-	set_pgd(pgd, pgdp_get(pgd_k));
+	if (CONFIG_PGTABLE_LEVELS == 5)
+		set_pgd(pgdp, pgd_k);
 
-	p4d_k = p4d_offset(pgd_k, addr);
-	if (!p4d_present(p4dp_get(p4d_k))) {
+	p4dp = p4d_offset(pgdp, addr);
+	p4dp_k = p4d_offset(pgdp_k, addr);
+	if (!p4d_present(p4dp_get(p4dp_k))) {
 		no_context(regs, addr);
 		return;
 	}
 
-	pud_k = pud_offset(p4d_k, addr);
-	if (!pud_present(pudp_get(pud_k))) {
+	pudp = pud_offset(p4dp, addr);
+	pudp_k = pud_offset(p4dp_k, addr);
+
+	pud_k = pudp_get(pudp_k);
+	if (!pud_present(pud_k)) {
 		no_context(regs, addr);
 		return;
 	}
-	if (pud_leaf(pudp_get(pud_k)))
+	if (pud_leaf(pud_k))
 		goto flush_tlb;
 
 	/*
 	 * Since the vmalloc area is global, it is unnecessary
 	 * to copy individual PTEs
 	 */
-	pmd_k = pmd_offset(pud_k, addr);
-	if (!pmd_present(pmdp_get(pmd_k))) {
+	pmdp = pmd_offset(pudp, addr);
+	pmdp_k = pmd_offset(pudp_k, addr);
+
+	pmd_k = pmdp_get(pmdp_k);
+	if (!pmd_present(pmd_k)) {
 		no_context(regs, addr);
 		return;
 	}
-	if (pmd_leaf(pmdp_get(pmd_k)))
+	if (CONFIG_PGTABLE_LEVELS == 2)
+		set_pmd(pmdp, pmd_k);
+	if (pmd_leaf(pmd_k))
 		goto flush_tlb;
 
 	/*
@@ -230,8 +244,8 @@ static inline void vmalloc_fault(struct pt_regs *regs, int code, unsigned long a
 	 * addresses. If we don't do this, this will just
 	 * silently loop forever.
 	 */
-	pte_k = pte_offset_kernel(pmd_k, addr);
-	if (!pte_present(ptep_get(pte_k))) {
+	ptep_k = pte_offset_kernel(pmdp_k, addr);
+	if (!pte_present(ptep_get(ptep_k))) {
 		no_context(regs, addr);
 		return;
 	}
diff --git a/arch/x86/include/asm/pgtable.h b/arch/x86/include/asm/pgtable.h
index 000fc6c1ca37c4..a2770821218783 100644
--- a/arch/x86/include/asm/pgtable.h
+++ b/arch/x86/include/asm/pgtable.h
@@ -1167,6 +1167,20 @@ static inline int pgd_none(pgd_t pgd)
 #ifndef __ASSEMBLER__
 
 extern int direct_gbpages;
+static inline bool direct_gbpages_enabled(void)
+{
+	/* Check the direct map config option: */
+	if (!IS_ENABLED(CONFIG_X86_DIRECT_GBPAGES))
+		return false;
+
+	/* Check the CPU feature: */
+	if (!boot_cpu_has(X86_FEATURE_GBPAGES))
+		return false;
+
+	/* Check the command-line and early setup variable: */
+	return direct_gbpages;
+}
+
 void init_mem_mapping(void);
 void early_alloc_pgt_buf(void);
 void __init poking_init(void);
diff --git a/arch/x86/kernel/cpu/common.c b/arch/x86/kernel/cpu/common.c
index 9bd50c68cd7e5e..12a51dba166276 100644
--- a/arch/x86/kernel/cpu/common.c
+++ b/arch/x86/kernel/cpu/common.c
@@ -2655,7 +2655,7 @@ void __init arch_cpu_finalize_init(void)
 		 * Right now we don't do that with gbpages because there seems
 		 * very little benefit for that case.
 		 */
-		if (!direct_gbpages)
+		if (!direct_gbpages_enabled())
 			set_memory_4k((unsigned long)__va(0), 1);
 	} else {
 		fpu__init_check_bugs();
diff --git a/arch/x86/kernel/machine_kexec_64.c b/arch/x86/kernel/machine_kexec_64.c
index c3f4a389992daa..0da0e89f2611a5 100644
--- a/arch/x86/kernel/machine_kexec_64.c
+++ b/arch/x86/kernel/machine_kexec_64.c
@@ -257,7 +257,7 @@ static int init_pgtable(struct kimage *image, unsigned long control_page)
 		info.kernpg_flag |= _PAGE_ENC;
 	}
 
-	if (direct_gbpages)
+	if (direct_gbpages_enabled())
 		info.direct_gbpages = true;
 
 	for (i = 0; i < nr_pfn_mapped; i++) {
diff --git a/arch/x86/mm/dump_pagetables.c b/arch/x86/mm/dump_pagetables.c
index 2afa7a23340e92..2acbe41eb58f26 100644
--- a/arch/x86/mm/dump_pagetables.c
+++ b/arch/x86/mm/dump_pagetables.c
@@ -38,6 +38,7 @@ struct pg_state {
 	bool check_wx;
 	unsigned long wx_pages;
 	struct seq_file *seq;
+	struct mm_struct *mm;
 };
 
 struct addr_marker {
@@ -248,13 +249,13 @@ static void note_wx(struct pg_state *st, unsigned long addr)
 		  (void *)st->start_address);
 }
 
-static void effective_prot(struct ptdump_state *pt_st, int level, u64 val)
+static void effective_prot(struct ptdump_state *pt_st, enum pgtable_level level, u64 val)
 {
 	struct pg_state *st = container_of(pt_st, struct pg_state, ptdump);
 	pgprotval_t prot = val & PTE_FLAGS_MASK;
 	pgprotval_t effective;
 
-	if (level > 0) {
+	if (level > mm_first_pgtable_level(st->mm)) {
 		pgprotval_t higher_prot = st->prot_levels[level - 1];
 
 		effective = (higher_prot & prot & (_PAGE_USER | _PAGE_RW)) |
@@ -268,27 +269,27 @@ static void effective_prot(struct ptdump_state *pt_st, int level, u64 val)
 
 static void effective_prot_pte(struct ptdump_state *st, pte_t pte)
 {
-	effective_prot(st, 4, pte_val(pte));
+	effective_prot(st, PGTABLE_LEVEL_PTE, pte_val(pte));
 }
 
 static void effective_prot_pmd(struct ptdump_state *st, pmd_t pmd)
 {
-	effective_prot(st, 3, pmd_val(pmd));
+	effective_prot(st, PGTABLE_LEVEL_PMD, pmd_val(pmd));
 }
 
 static void effective_prot_pud(struct ptdump_state *st, pud_t pud)
 {
-	effective_prot(st, 2, pud_val(pud));
+	effective_prot(st, PGTABLE_LEVEL_PUD, pud_val(pud));
 }
 
 static void effective_prot_p4d(struct ptdump_state *st, p4d_t p4d)
 {
-	effective_prot(st, 1, p4d_val(p4d));
+	effective_prot(st, PGTABLE_LEVEL_P4D, p4d_val(p4d));
 }
 
 static void effective_prot_pgd(struct ptdump_state *st, pgd_t pgd)
 {
-	effective_prot(st, 0, pgd_val(pgd));
+	effective_prot(st, PGTABLE_LEVEL_PGD, pgd_val(pgd));
 }
 
 
@@ -452,7 +453,8 @@ bool ptdump_walk_pgd_level_core(struct seq_file *m,
 		.level = -1,
 		.to_dmesg	= dmesg,
 		.check_wx	= checkwx,
-		.seq		= m
+		.seq		= m,
+		.mm		= mm,
 	};
 
 	ptdump_walk_pgd(&st.ptdump, mm, pgd);
diff --git a/arch/x86/mm/init.c b/arch/x86/mm/init.c
index 079f8c7e9e3cde..f0f4a06584c61c 100644
--- a/arch/x86/mm/init.c
+++ b/arch/x86/mm/init.c
@@ -251,7 +251,7 @@ static void __init probe_page_size_mask(void)
 		__default_kernel_pte_mask &= ~_PAGE_GLOBAL;
 
 	/* Enable 1 GB linear kernel mappings if available: */
-	if (direct_gbpages && boot_cpu_has(X86_FEATURE_GBPAGES)) {
+	if (direct_gbpages_enabled()) {
 		printk(KERN_INFO "Using GB pages for direct mapping\n");
 		page_size_mask |= 1 << PG_LEVEL_1G;
 	} else {
diff --git a/arch/x86/mm/pat/set_memory.c b/arch/x86/mm/pat/set_memory.c
index b1d5c5a0ffb41d..f27811e6e00bd7 100644
--- a/arch/x86/mm/pat/set_memory.c
+++ b/arch/x86/mm/pat/set_memory.c
@@ -130,7 +130,7 @@ void arch_report_meminfo(struct seq_file *m)
 	seq_printf(m, "DirectMap4M:    %8lu kB\n",
 			direct_pages_count[PG_LEVEL_2M] << 12);
 #endif
-	if (direct_gbpages)
+	if (direct_gbpages_enabled())
 		seq_printf(m, "DirectMap1G:    %8lu kB\n",
 			direct_pages_count[PG_LEVEL_1G] << 20);
 }
@@ -1355,7 +1355,7 @@ static int collapse_pud_page(pud_t *pud, unsigned long addr,
 	pmd_t *pmd, first;
 	int i;
 
-	if (!direct_gbpages)
+	if (!direct_gbpages_enabled())
 		return 0;
 
 	addr &= PUD_MASK;
@@ -1737,7 +1737,7 @@ static int populate_pud(struct cpa_data *cpa, unsigned long start, p4d_t *p4d,
 	/*
 	 * Map everything starting from the Gb boundary, possibly with 1G pages
 	 */
-	while (boot_cpu_has(X86_FEATURE_GBPAGES) && end - start >= PUD_SIZE) {
+	while (direct_gbpages_enabled() && end - start >= PUD_SIZE) {
 		set_pud(pud, pud_mkhuge(pfn_pud(cpa->pfn,
 				   canon_pgprot(pud_pgprot))));
 
diff --git a/include/asm-generic/pgtable-nop4d.h b/include/asm-generic/pgtable-nop4d.h
index 1cf739ee38aa15..12b01768134b5a 100644
--- a/include/asm-generic/pgtable-nop4d.h
+++ b/include/asm-generic/pgtable-nop4d.h
@@ -22,25 +22,61 @@ static inline int pgd_none(pgd_t pgd)		{ return 0; }
 static inline int pgd_bad(pgd_t pgd)		{ return 0; }
 static inline int pgd_present(pgd_t pgd)	{ return 1; }
 static inline void pgd_clear(pgd_t *pgd)	{ }
+static inline bool pgd_leaf(pgd_t pgd)		{ return false; }
+#define pgd_leaf pgd_leaf
+#define p4d_ERROR(p4d)				(pgd_ERROR((p4d).pgd))
 
 #define pgd_populate(mm, pgd, p4d)		do { } while (0)
 #define pgd_populate_safe(mm, pgd, p4d)		do { } while (0)
-/*
- * (p4ds are folded into pgds so this doesn't get actually called,
- * but the define is needed for a generic inline function.)
- */
-#define set_pgd(pgdptr, pgdval)	set_p4d((p4d_t *)(pgdptr), (p4d_t) { pgdval })
 
-static inline p4d_t *p4d_offset(pgd_t *pgd, unsigned long address)
+#define set_pgd(pgdptr, pgdval)						\
+({									\
+	pgd_check_dummy(pgdval);					\
+	set_p4d((p4d_t *)(pgdptr), (p4d_t) { pgdval });			\
+})
+
+static __always_inline pgd_t pgdp_get(pgd_t *pgdp)
 {
-	return (p4d_t *)pgd;
+	pgd_t dummy = { 0 };
+
+	return dummy;
 }
+#define pgdp_get pgdp_get
+
+#define pgd_check_dummy(pgd) BUILD_BUG_ON(__builtin_constant_p(pgd_val(pgd)))
+
+static __always_inline p4d_t *__p4d_offset(pgd_t *pgdp, unsigned long address)
+{
+	return (p4d_t *)pgdp;
+}
+
+#define p4d_offset(pgdp, address)					\
+({									\
+	pgd_check_dummy(*(pgdp));					\
+	__p4d_offset(pgdp, address);					\
+})
+
+static __always_inline p4d_t *__p4d_offset_lockless(pgd_t *pgdp, pgd_t pgd,
+		unsigned long address)
+{
+	return (p4d_t *)pgdp;
+}
+
+#define p4d_offset_lockless(pgdp, pgd, address)				\
+({									\
+	pgd_check_dummy(*(pgdp));					\
+	__p4d_offset_lockless(pgdp, pgd, address);			\
+})
 
 #define p4d_val(x)				(pgd_val((x).pgd))
 #define __p4d(x)				((p4d_t) { __pgd(x) })
 
-#define pgd_page(pgd)				(p4d_page((p4d_t){ pgd }))
-#define pgd_page_vaddr(pgd)			((unsigned long)(p4d_pgtable((p4d_t){ pgd })))
+#define pgd_page(pgd)				({ BUILD_BUG(); (struct page *)NULL; })
+#define pgd_page_vaddr(pgd)						\
+({ 									\
+	pgd_check_dummy(pgd);						\
+	(unsigned long)p4d_pgtable((p4d_t) { pgd });			\
+})
 
 /*
  * allocating and freeing a p4d is trivial: the 1-entry p4d is
diff --git a/include/asm-generic/pgtable-nopmd.h b/include/asm-generic/pgtable-nopmd.h
index ff4235cf84d772..11b04c10712957 100644
--- a/include/asm-generic/pgtable-nopmd.h
+++ b/include/asm-generic/pgtable-nopmd.h
@@ -31,28 +31,60 @@ static inline int pud_none(pud_t pud)		{ return 0; }
 static inline int pud_bad(pud_t pud)		{ return 0; }
 static inline int pud_present(pud_t pud)	{ return 1; }
 static inline int pud_user(pud_t pud)		{ return 0; }
-static inline int pud_leaf(pud_t pud)		{ return 0; }
+static inline bool pud_leaf(pud_t pud)		{ return false; }
+#define pud_leaf pud_leaf
 static inline void pud_clear(pud_t *pud)	{ }
 
 #define pud_populate(mm, pmd, pte)		do { } while (0)
 
-/*
- * (pmds are folded into puds so this doesn't get actually called,
- * but the define is needed for a generic inline function.)
- */
-#define set_pud(pudptr, pudval)			set_pmd((pmd_t *)(pudptr), (pmd_t) { pudval })
+#define set_pud(pudptr, pudval)						\
+({									\
+	pud_check_dummy(pudval);					\
+	set_pmd((pmd_t *)(pudptr), (pmd_t) { pudval });			\
+})
+
+static __always_inline pud_t pudp_get(pud_t *pudp)
+{
+	pud_t dummy = { 0 };
+
+	return dummy;
+}
+#define pudp_get pudp_get
 
-static inline pmd_t * pmd_offset(pud_t * pud, unsigned long address)
+#define pud_check_dummy(pud) BUILD_BUG_ON(__builtin_constant_p(pud_val(pud)))
+
+static __always_inline pmd_t *__pmd_offset(pud_t *pudp, unsigned long address)
+{
+	return (pmd_t *)pudp;
+}
+
+#define pmd_offset(pudp, address)					\
+({									\
+	pud_check_dummy(*(pudp));					\
+	__pmd_offset(pudp, address);					\
+})
+
+static __always_inline pmd_t *__pmd_offset_lockless(pud_t *pudp, pud_t pud,
+		unsigned long address)
 {
-	return (pmd_t *)pud;
+	return (pmd_t *)pudp;
 }
-#define pmd_offset pmd_offset
+
+#define pmd_offset_lockless(pudp, pud, address)				\
+({									\
+	pud_check_dummy(*(pudp));					\
+	__pmd_offset_lockless(pudp, pud, address);			\
+})
 
 #define pmd_val(x)				(pud_val((x).pud))
 #define __pmd(x)				((pmd_t) { __pud(x) } )
 
-#define pud_page(pud)				(pmd_page((pmd_t){ pud }))
-#define pud_pgtable(pud)			((pmd_t *)(pmd_page_vaddr((pmd_t){ pud })))
+#define pud_page(pud)				({ BUILD_BUG(); (struct page *)NULL; })
+#define pud_pgtable(pud)						\
+({									\
+	pud_check_dummy(pud);						\
+	((pmd_t *)(pmd_page_vaddr((pmd_t) { pud })));			\
+})
 
 /*
  * allocating and freeing a pmd is trivial: the 1-entry pmd is
diff --git a/include/asm-generic/pgtable-nopud.h b/include/asm-generic/pgtable-nopud.h
index eedee8e3ad68fd..5ee8f1852cd8c9 100644
--- a/include/asm-generic/pgtable-nopud.h
+++ b/include/asm-generic/pgtable-nopud.h
@@ -29,26 +29,62 @@ static inline int p4d_none(p4d_t p4d)		{ return 0; }
 static inline int p4d_bad(p4d_t p4d)		{ return 0; }
 static inline int p4d_present(p4d_t p4d)	{ return 1; }
 static inline void p4d_clear(p4d_t *p4d)	{ }
+static inline bool p4d_leaf(p4d_t p4d)		{ return false; }
+#define p4d_leaf p4d_leaf
+#define pud_ERROR(pud)				(p4d_ERROR((pud).p4d))
 
 #define p4d_populate(mm, p4d, pud)		do { } while (0)
 #define p4d_populate_safe(mm, p4d, pud)		do { } while (0)
-/*
- * (puds are folded into p4ds so this doesn't get actually called,
- * but the define is needed for a generic inline function.)
- */
-#define set_p4d(p4dptr, p4dval)	set_pud((pud_t *)(p4dptr), (pud_t) { p4dval })
 
-static inline pud_t *pud_offset(p4d_t *p4d, unsigned long address)
+#define set_p4d(p4dptr, p4dval)						\
+({									\
+	p4d_check_dummy(p4dval);					\
+	set_pud((pud_t *)(p4dptr), (pud_t) { p4dval });			\
+})
+
+
+static __always_inline p4d_t p4dp_get(p4d_t *p4dp)
+{
+	p4d_t dummy = { 0 };
+
+	return dummy;
+}
+#define p4dp_get p4dp_get
+
+#define p4d_check_dummy(p4d) BUILD_BUG_ON(__builtin_constant_p(p4d_val(p4d)))
+
+static __always_inline pud_t *__pud_offset(p4d_t *p4dp, unsigned long address)
 {
-	return (pud_t *)p4d;
+	return (pud_t *)p4dp;
 }
-#define pud_offset pud_offset
+
+#define pud_offset(p4dp, address)					\
+({									\
+	p4d_check_dummy(*(p4dp));					\
+	__pud_offset(p4dp, address);					\
+})
+
+static __always_inline pud_t *__pud_offset_lockless(p4d_t *p4dp, p4d_t p4d,
+		unsigned long address)
+{
+	return (pud_t *)p4dp;
+}
+
+#define pud_offset_lockless(p4dp, p4d, address)				\
+({									\
+	p4d_check_dummy(*(p4dp));					\
+	__pud_offset_lockless(p4dp, p4d, address);			\
+})
 
 #define pud_val(x)				(p4d_val((x).p4d))
 #define __pud(x)				((pud_t) { __p4d(x) })
 
-#define p4d_page(p4d)				(pud_page((pud_t){ p4d }))
-#define p4d_pgtable(p4d)			((pud_t *)(pud_pgtable((pud_t){ p4d })))
+#define p4d_page(p4d)				({ BUILD_BUG(); (struct page *)NULL; })
+#define p4d_pgtable(p4d)						\
+({									\
+	p4d_check_dummy(p4d);						\
+	((pud_t *)(pud_pgtable((pud_t) { p4d })));			\
+})
 
 /*
  * allocating and freeing a pud is trivial: the 1-entry pud is
diff --git a/include/linux/pgtable.h b/include/linux/pgtable.h
index 780fe849ff8b7e..b0d0e32ad6aef4 100644
--- a/include/linux/pgtable.h
+++ b/include/linux/pgtable.h
@@ -392,6 +392,22 @@ static inline void lazy_mmu_mode_pause(void) {}
 static inline void lazy_mmu_mode_resume(void) {}
 #endif
 
+/*
+ * On some architectures it depends on the mm if the p4d/pud or pmd
+ * layer of the page table hierarchy is folded or not.
+ */
+#ifndef mm_p4d_folded
+#define mm_p4d_folded(mm)	__is_defined(__PAGETABLE_P4D_FOLDED)
+#endif
+
+#ifndef mm_pud_folded
+#define mm_pud_folded(mm)	__is_defined(__PAGETABLE_PUD_FOLDED)
+#endif
+
+#ifndef mm_pmd_folded
+#define mm_pmd_folded(mm)	__is_defined(__PAGETABLE_PMD_FOLDED)
+#endif
+
 #ifndef pte_batch_hint
 /**
  * pte_batch_hint - Number of pages that can be added to batch without scanning.
@@ -2288,11 +2304,11 @@ static inline bool arch_has_pfn_modify_check(void)
 typedef unsigned int pgtbl_mod_mask;
 
 enum pgtable_level {
-	PGTABLE_LEVEL_PTE = 0,
-	PGTABLE_LEVEL_PMD,
-	PGTABLE_LEVEL_PUD,
+	PGTABLE_LEVEL_PGD = 0,
 	PGTABLE_LEVEL_P4D,
-	PGTABLE_LEVEL_PGD,
+	PGTABLE_LEVEL_PUD,
+	PGTABLE_LEVEL_PMD,
+	PGTABLE_LEVEL_PTE,
 };
 
 static inline const char *pgtable_level_to_str(enum pgtable_level level)
@@ -2313,6 +2329,36 @@ static inline const char *pgtable_level_to_str(enum pgtable_level level)
 	}
 }
 
+#ifdef CONFIG_MMU
+/**
+ * mm_first_pgtable_level - return the first non-folded page-table level
+ * @mm: The mm structure.
+ *
+ * With runtime- or compile-time folded page tables, page-table walkers
+ * effectively skip the folded top-level page tables, treating them as
+ * always-present entries pointing to the next lower-level page table.
+ *
+ * Return the first page-table level in @mm that is not folded.
+ *
+ * Return: the first non-folded page-table level.
+ */
+static inline enum pgtable_level mm_first_pgtable_level(struct mm_struct *mm)
+{
+	/*
+	 * "PMD folded" actually means "PMD is folded into PUD", and
+	 * pud_present() etc. are hard-coded to look like present page table
+	 * levels, whereby pmd_present() etc. contain real logic.
+	 */
+	if (mm_pmd_folded(mm))
+		return PGTABLE_LEVEL_PMD;
+	if (mm_pud_folded(mm))
+		return PGTABLE_LEVEL_PUD;
+	if (mm_p4d_folded(mm))
+		return PGTABLE_LEVEL_P4D;
+	return PGTABLE_LEVEL_PGD;
+}
+#endif /* CONFIG_MMU */
+
 void ptval_bytes_to_hex_str(char *buf, size_t buf_size, const void *entry, size_t entry_size);
 
 #define ptval_to_str(buf, val)								\
@@ -2349,21 +2395,6 @@ void ptval_bytes_to_hex_str(char *buf, size_t buf_size, const void *entry, size_
 #ifndef has_transparent_pud_hugepage
 #define has_transparent_pud_hugepage() IS_BUILTIN(CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD)
 #endif
-/*
- * On some architectures it depends on the mm if the p4d/pud or pmd
- * layer of the page table hierarchy is folded or not.
- */
-#ifndef mm_p4d_folded
-#define mm_p4d_folded(mm)	__is_defined(__PAGETABLE_P4D_FOLDED)
-#endif
-
-#ifndef mm_pud_folded
-#define mm_pud_folded(mm)	__is_defined(__PAGETABLE_PUD_FOLDED)
-#endif
-
-#ifndef mm_pmd_folded
-#define mm_pmd_folded(mm)	__is_defined(__PAGETABLE_PMD_FOLDED)
-#endif
 
 #ifndef p4d_offset_lockless
 #define p4d_offset_lockless(pgdp, pgd, address) p4d_offset(&(pgd), address)
diff --git a/mm/vmscan.c b/mm/vmscan.c
index 9959ca158ce835..6a1af9b92349fb 100644
--- a/mm/vmscan.c
+++ b/mm/vmscan.c
@@ -3911,7 +3911,7 @@ static void walk_pmd_range(pud_t *pud, unsigned long start, unsigned long end,
 
 		walk->mm_stats[MM_NONLEAF_FOUND]++;
 
-		if (!walk_pte_range(&val, addr, next, args))
+		if (!walk_pte_range(pmd + i, addr, next, args))
 			continue;
 
 		walk->mm_stats[MM_NONLEAF_ADDED]++;
@@ -3947,7 +3947,7 @@ static int walk_pud_range(p4d_t *p4d, unsigned long start, unsigned long end,
 		if (!pud_present(val) || WARN_ON_ONCE(pud_leaf(val)))
 			continue;
 
-		walk_pmd_range(&val, addr, next, args);
+		walk_pmd_range(pud + i, addr, next, args);
 
 		if (need_resched() || walk->batched >= MAX_LRU_BATCH) {
 			end = (addr | ~PUD_MASK) + 1;

3/2 2026/11/02 21:59 llm patch-evaluator
0m Model:
gemini-3.8-flash

Args:
null

Results:
null

Tokens:
							input: 16894
							output: 35
							thoughts: 465
						

Thoughts:
**Fuzzing Potential: Patch Series Analysis**

Okay, so I've got this patch series in front of me, and my immediate thought is whether it's worth the effort of throwing it at syzkaller. The target architecture is amd64, which is good; it's a well-supported platform.

My first step is always to get a handle on the scope of the changes. The diff shows modifications across quite a few files: documentation, architecture-specific include files for ARM, ARM64, LoongArch, MIPS, OpenRISC, RISC-V, and x86, along with some kernel code. Alright, that’s a broad touch. I see changes in `Documentation/mm/page_tables.rst`, so it looks like at least some of the changes are related to documentation. I have to look carefully through the files that are touched.

Now, wait a second... hold on. There's `mm/vmscan.c` in the mix, and a bunch of x86 files in `arch/x86/`, plus some generic include files like `include/asm-generic/pgtable-nop4d.h`, and `include/linux/pgtable.h`. Those are prime targets for memory management bugs or something related to x86, which is interesting to me. It's time to dig in. I should start by looking at `mm/vmscan.c`. Lines 3911 and 3947 are important. Let’s read those lines and see what's happening.



4/2 2026/11/02 21:59 tool read-file
0m Args:
{
  "File": "mm/vmscan.c",
  "FirstLine": 3900,
  "LineCount": 60
}

Results:
{
  "Contents": "3900:\t\n3901:\t\t\tif (!walk-\u003eforce_scan \u0026\u0026 should_clear_pmd_young() \u0026\u0026\n3902:\t\t\t    !mm_has_notifiers(args-\u003emm)) {\n3903:\t\t\t\tif (!pmd_young(val))\n3904:\t\t\t\t\tcontinue;\n3905:\t\n3906:\t\t\t\twalk_pmd_range_locked(pud, addr, vma, args, bitmap, \u0026first);\n3907:\t\t\t}\n3908:\t\n3909:\t\t\tif (!walk-\u003eforce_scan \u0026\u0026 !test_bloom_filter(mm_state, walk-\u003eseq, pmd + i))\n3910:\t\t\t\tcontinue;\n3911:\t\n3912:\t\t\twalk-\u003emm_stats[MM_NONLEAF_FOUND]++;\n3913:\t\n3914:\t\t\tif (!walk_pte_range(pmd + i, addr, next, args))\n3915:\t\t\t\tcontinue;\n3916:\t\n3917:\t\t\twalk-\u003emm_stats[MM_NONLEAF_ADDED]++;\n3918:\t\n3919:\t\t\t/* carry over to the next generation */\n3920:\t\t\tupdate_bloom_filter(mm_state, walk-\u003eseq + 1, pmd + i);\n3921:\t\t}\n3922:\t\n3923:\t\twalk_pmd_range_locked(pud, -1, vma, args, bitmap, \u0026first);\n3924:\t\n3925:\t\tif (i \u003c PTRS_PER_PMD \u0026\u0026 get_next_vma(PUD_MASK, PMD_SIZE, args, \u0026start, \u0026end))\n3926:\t\t\tgoto restart;\n3927:\t}\n3928:\t\n3929:\tstatic int walk_pud_range(p4d_t *p4d, unsigned long start, unsigned long end,\n3930:\t\t\t\t  struct mm_walk *args)\n3931:\t{\n3932:\t\tint i;\n3933:\t\tpud_t *pud;\n3934:\t\tunsigned long addr;\n3935:\t\tunsigned long next;\n3936:\t\tstruct lru_gen_mm_walk *walk = args-\u003eprivate;\n3937:\t\n3938:\t\tVM_WARN_ON_ONCE(p4d_leaf(*p4d));\n3939:\t\n3940:\t\tpud = pud_offset(p4d, start \u0026 P4D_MASK);\n3941:\trestart:\n3942:\t\tfor (i = pud_index(start), addr = start; addr != end; i++, addr = next) {\n3943:\t\t\tpud_t val = pudp_get(pud + i);\n3944:\t\n3945:\t\t\tnext = pud_addr_end(addr, end);\n3946:\t\n3947:\t\t\tif (!pud_present(val) || WARN_ON_ONCE(pud_leaf(val)))\n3948:\t\t\t\tcontinue;\n3949:\t\n3950:\t\t\twalk_pmd_range(pud + i, addr, next, args);\n3951:\t\n3952:\t\t\tif (need_resched() || walk-\u003ebatched \u003e= MAX_LRU_BATCH) {\n3953:\t\t\t\tend = (addr | ~PUD_MASK) + 1;\n3954:\t\t\t\tgoto done;\n3955:\t\t\t}\n3956:\t\t}\n3957:\t\n3958:\t\tif (i \u003c PTRS_PER_PUD \u0026\u0026 get_next_vma(P4D_MASK, PUD_SIZE, args, \u0026start, \u0026end))\n3959:\t\t\tgoto restart;\n"
}

5/2 2026/11/02 21:59 llm patch-evaluator
0m Model:
gemini-3.8-flash

Args:
null

Results:
null

Tokens:
							input: 18696
							output: 35
							thoughts: 116
						

Thoughts:
Okay, let's dive into this kernel code, specifically `mm/vmscan.c`. I'm focusing on the `walk_pmd_range()` function, and something immediately catches my eye.

**Intriguing Memory Access in `walk_pmd_range()`**

The core of the issue seems to be how they're handling the `walk_pte_range()` call within `walk_pmd_range()`.  Specifically, the previous code snippet: `walk_pte_range(&val, addr, next, args)`.  The first argument, `&val`, is the key.  Wait a minute... `val` is a local variable, declared as `pmd_t val = pmdp_get(pmd + i);`!

So, we're passing the *address* of a local variable (`&val`) to `walk_pte_range()`. But shouldn't this be an *index* to a table? Doesn't this seem...off? I'd have expected to see `pmd + i`, but it's not. I'm immediately skeptical.

This needs further investigation. I'll need to go back and examine the definitions of both `walk_pte_range()` and `walk_pmd_range()` to understand what `walk_pte_range()` expects for that first argument.  This might be a subtle bug or a clever optimization, but it's crucial to understand why they're passing the *address* of a local variable rather than the PUD.



6/2 2026/11/02 21:59 tool read-file
0m Args:
{
  "File": "mm/vmscan.c",
  "FirstLine": 3870,
  "LineCount": 40
}

Results:
{
  "Contents": "3870:\t\n3871:\t\t/*\n3872:\t\t * Finish an entire PMD in two passes: the first only reaches to PTE\n3873:\t\t * tables to avoid taking the PMD lock; the second, if necessary, takes\n3874:\t\t * the PMD lock to clear the accessed bit in PMD entries.\n3875:\t\t */\n3876:\t\tpmd = pmd_offset(pud, start \u0026 PUD_MASK);\n3877:\trestart:\n3878:\t\t/* walk_pte_range() may call get_next_vma() */\n3879:\t\tvma = args-\u003evma;\n3880:\t\tfor (i = pmd_index(start), addr = start; addr != end; i++, addr = next) {\n3881:\t\t\tpmd_t val = pmdp_get_lockless(pmd + i);\n3882:\t\n3883:\t\t\tnext = pmd_addr_end(addr, end);\n3884:\t\n3885:\t\t\tif (!pmd_present(val) || is_huge_zero_pmd(val)) {\n3886:\t\t\t\twalk-\u003emm_stats[MM_LEAF_TOTAL]++;\n3887:\t\t\t\tcontinue;\n3888:\t\t\t}\n3889:\t\n3890:\t\t\tif (pmd_trans_huge(val)) {\n3891:\t\t\t\tstruct pglist_data *pgdat = lruvec_pgdat(walk-\u003elruvec);\n3892:\t\t\t\tunsigned long pfn = get_pmd_pfn(val, vma, addr, pgdat);\n3893:\t\n3894:\t\t\t\twalk-\u003emm_stats[MM_LEAF_TOTAL]++;\n3895:\t\n3896:\t\t\t\tif (pfn != -1)\n3897:\t\t\t\t\twalk_pmd_range_locked(pud, addr, vma, args, bitmap, \u0026first);\n3898:\t\t\t\tcontinue;\n3899:\t\t\t}\n3900:\t\n3901:\t\t\tif (!walk-\u003eforce_scan \u0026\u0026 should_clear_pmd_young() \u0026\u0026\n3902:\t\t\t    !mm_has_notifiers(args-\u003emm)) {\n3903:\t\t\t\tif (!pmd_young(val))\n3904:\t\t\t\t\tcontinue;\n3905:\t\n3906:\t\t\t\twalk_pmd_range_locked(pud, addr, vma, args, bitmap, \u0026first);\n3907:\t\t\t}\n3908:\t\n3909:\t\t\tif (!walk-\u003eforce_scan \u0026\u0026 !test_bloom_filter(mm_state, walk-\u003eseq, pmd + i))\n"
}

7/2 2026/11/02 21:59 llm patch-evaluator
0m Model:
gemini-3.8-flash

Args:
null

Results:
null

Tokens:
							input: 19861
							output: 35
							thoughts: 95
						

Thoughts:
Okay, let me break this down.

**Diving into `walk_pud_range` - A Trace**

Alright, I'm focusing in on `walk_pud_range`. I've got to understand how it's interacting with the page table structures. Specifically, I'm looking at how it handles the `pud_t` level. It used to be calling `walk_pmd_range` with `pud_t val = pudp_get(pud + i)` which is a common pattern in the memory management world. Okay, so `walk_pmd_range` is the critical piece I need to look at. And, I know the first argument that `walk_pmd_range` takes is vital, so I'll check its definition. Let me go jump to line 3860 and start reading through to 3870, and then I should have it figured out. I need to see *precisely* what that first argument is and how it's being used. Got it.



8/2 2026/11/02 22:00 tool read-file
0m Args:
{
  "File": "mm/vmscan.c",
  "FirstLine": 3855,
  "LineCount": 20
}

Results:
{
  "Contents": "3855:\t\n3856:\tstatic void walk_pmd_range(pud_t *pud, unsigned long start, unsigned long end,\n3857:\t\t\t\t   struct mm_walk *args)\n3858:\t{\n3859:\t\tint i;\n3860:\t\tpmd_t *pmd;\n3861:\t\tunsigned long next;\n3862:\t\tunsigned long addr;\n3863:\t\tstruct vm_area_struct *vma;\n3864:\t\tDECLARE_BITMAP(bitmap, MIN_LRU_BATCH);\n3865:\t\tunsigned long first = -1;\n3866:\t\tstruct lru_gen_mm_walk *walk = args-\u003eprivate;\n3867:\t\tstruct lru_gen_mm_state *mm_state = get_mm_state(walk-\u003elruvec);\n3868:\t\n3869:\t\tVM_WARN_ON_ONCE(pud_leaf(*pud));\n3870:\t\n3871:\t\t/*\n3872:\t\t * Finish an entire PMD in two passes: the first only reaches to PTE\n3873:\t\t * tables to avoid taking the PMD lock; the second, if necessary, takes\n3874:\t\t * the PMD lock to clear the accessed bit in PMD entries.\n"
}

9/2 2026/11/02 22:00 llm patch-evaluator
0m Model:
gemini-3.8-flash

Args:
null

Results:
null

Tokens:
							input: 20560
							output: 35
							thoughts: 517
						

Thoughts:
**Debugging a Potential Page Table Walk Bug**

Okay, so I'm looking at this `walk_pmd_range` function, and it's calling `walk_pte_range`, which is good. But wait a minute... I'm noticing a change. Previously, it looks like `walk_pmd_range` was being called with a local stack variable, `val`, like this: `walk_pmd_range(&val, ...)`. That immediately sets off alarm bells!  Now, I see the code is using the actual calculated `pmd + i` value.

Digging deeper, I see on line 3876, the code uses `pmd_offset(pud, start & PUD_MASK)`.  If the `pud` is in a "folded" state, it looks like `pmd_offset(pud, ...)` is the one to inspect. Let me check the `include/asm-generic/pgtable-nopmd.h` and `nopud.h` headers.

Okay, in `nopud.h`, I find this `pud_offset` macro: it looks like it includes a check, using `pud_check_dummy(*(p4dp))`. And what's this? `pud_check_dummy(pud)` uses `BUILD_BUG_ON(__builtin_constant_p(pud_val(pud)))`. Aha! I remember that `p4dp_get()` returns a constant dummy value of `{ 0 }`.

So, *if* `p4dp_get()` returned `{ 0 }`, and someone passed the address of that stack variable `val`, which was initially loaded with the return from `p4dp_get()`, `p4d_val(val)` would be constant 0, or with folded tables, `pud_val(dummy)` would be 0, so the `BUILD_BUG_ON(__builtin_constant_p(...))` would trigger, *or more importantly*, passing the address of that stack variable `val` meant that the `pmd_offset(&val, ...)` call was taking a pointer to a stack variable, rather than the actual page table pointer `pud + i`!

Okay, that's the smoking gun! The fix of switching the call to `walk_pte_range` from using `&val` to using the calculated pointer, `pmd + i`, is definitely correct. Now, before I move on, I'd better double check the definition of `walk_pte_range` to make sure the change is correct.



10/2 2026/11/02 22:00 tool read-file
0m Args:
{
  "File": "mm/vmscan.c",
  "FirstLine": 3810,
  "LineCount": 45
}

Results:
{
  "Contents": "3810:\t\t\taddr = i ? (*first \u0026 PMD_MASK) + i * PMD_SIZE : *first;\n3811:\t\n3812:\t\t\tif (!pmd_present(pmd[i]))\n3813:\t\t\t\tgoto next;\n3814:\t\n3815:\t\t\tif (!pmd_trans_huge(pmd[i])) {\n3816:\t\t\t\tif (!walk-\u003eforce_scan \u0026\u0026 should_clear_pmd_young() \u0026\u0026\n3817:\t\t\t\t    !mm_has_notifiers(args-\u003emm))\n3818:\t\t\t\t\tpmdp_test_and_clear_young(vma, addr, pmd + i);\n3819:\t\t\t\tgoto next;\n3820:\t\t\t}\n3821:\t\n3822:\t\t\tpfn = get_pmd_pfn(pmd[i], vma, addr, pgdat);\n3823:\t\t\tif (pfn == -1)\n3824:\t\t\t\tgoto next;\n3825:\t\n3826:\t\t\tfolio = get_pfn_folio(pfn, memcg, pgdat);\n3827:\t\t\tif (!folio)\n3828:\t\t\t\tgoto next;\n3829:\t\n3830:\t\t\tif (!pmdp_test_and_clear_young_notify(vma, addr, pmd + i))\n3831:\t\t\t\tgoto next;\n3832:\t\n3833:\t\t\tif (last != folio) {\n3834:\t\t\t\twalk_update_folio(walk, vma, walk-\u003elruvec, last, dirty);\n3835:\t\n3836:\t\t\t\tlast = folio;\n3837:\t\t\t\tdirty = false;\n3838:\t\t\t}\n3839:\t\n3840:\t\t\tif (pmd_dirty(pmd[i]))\n3841:\t\t\t\tdirty = true;\n3842:\t\n3843:\t\t\twalk-\u003emm_stats[MM_LEAF_YOUNG]++;\n3844:\tnext:\n3845:\t\t\ti = i \u003e MIN_LRU_BATCH ? 0 : find_next_bit(bitmap, MIN_LRU_BATCH, i) + 1;\n3846:\t\t} while (i \u003c= MIN_LRU_BATCH);\n3847:\t\n3848:\t\twalk_update_folio(walk, vma, walk-\u003elruvec, last, dirty);\n3849:\t\n3850:\t\tlazy_mmu_mode_disable();\n3851:\t\tspin_unlock(ptl);\n3852:\tdone:\n3853:\t\t*first = -1;\n3854:\t}\n"
}

11/2 2026/11/02 22:00 llm patch-evaluator
0m Model:
gemini-3.8-flash

Args:
null

Results:
null

Tokens:
							input: 22356
							output: 32
							thoughts: 15
						

Thoughts:
Here's my take on those thoughts, tailored for an expert audience:

**Tracing the Virtual Memory Footprint**

Alright, let's dive in. I need to understand how this virtual address space is being mapped, so I'm thinking I'll start by tracing the function `walk_pte_range`. I know this is the function responsible for iterating through the page table entries for a given virtual address range. The key here is to determine how the physical pages are being linked to the virtual address. By pinpointing the definition of `walk_pte_range`, I can see exactly how it's iterating, what structures it's using (likely `pgd`, `pud`, `pmd`, `pte` - though the kernel version may influence the specifics), and how it handles the individual page mappings. This is where I'll get a clear picture of the underlying translation process. Knowing where this function lives will be my entry point into the details of the page table walk and how the kernel handles the memory mappings. I'm expecting to see calls to functions dealing with page table manipulation or possibly directly inspecting the hardware page tables, depending on the architecture and the kernel's implementation.



12/2 2026/11/02 22:01 tool grepper
0m Args:
{
  "Expression": "walk_pte_range",
  "PathPrefixes": [
    "mm/vmscan.c"
  ]
}

Results:
{
  "Output": "mm/vmscan.c=3655=static void walk_update_folio(struct lru_gen_mm_walk *walk, struct vm_area_struct *vma,\n--\nmm/vmscan.c-3680-\nmm/vmscan.c:3681:static bool walk_pte_range(pmd_t *pmd, unsigned long start, unsigned long end,\nmm/vmscan.c-3682-\t\t\t   struct mm_walk *args)\n--\nmm/vmscan.c=3856=static void walk_pmd_range(pud_t *pud, unsigned long start, unsigned long end,\n--\nmm/vmscan.c-3877-restart:\nmm/vmscan.c:3878:\t/* walk_pte_range() may call get_next_vma() */\nmm/vmscan.c-3879-\tvma = args-\u003evma;\n--\nmm/vmscan.c-3913-\nmm/vmscan.c:3914:\t\tif (!walk_pte_range(pmd + i, addr, next, args))\nmm/vmscan.c-3915-\t\t\tcontinue;\n"
}

13/2 2026/11/02 22:01 llm patch-evaluator
0m Model:
gemini-3.8-flash

Args:
null

Results:
null

Tokens:
							input: 22954
							output: 35
							thoughts: 19
						

Thoughts:
Okay, here's my interpretation of that thought process, tailored for an expert audience, and presented as if it were my own internal monologue:

**Diving into `mm/vmscan.c`**

Alright, time to dig into this memory issue. I recall some odd behavior during recent testing, and I've got a hunch it relates to the VM scanning process. Let's start with a specific point: line 3681 in `mm/vmscan.c`. I need to understand what's happening at that particular stage in the code execution. This is where the core logic of the VM scanner lies; likely some vital decision is made in that vicinity. Depending on the architecture, there could be interesting implications for the translation lookaside buffer, cache coherency, or page replacement. Hopefully, the surrounding code will clarify the context. Let me check what that line is doing.



14/2 2026/11/02 22:01 tool read-file
0m Args:
{
  "File": "mm/vmscan.c",
  "FirstLine": 3680,
  "LineCount": 40
}

Results:
{
  "Contents": "3680:\t\n3681:\tstatic bool walk_pte_range(pmd_t *pmd, unsigned long start, unsigned long end,\n3682:\t\t\t\t   struct mm_walk *args)\n3683:\t{\n3684:\t\tint i;\n3685:\t\tbool dirty;\n3686:\t\tpte_t *pte;\n3687:\t\tspinlock_t *ptl;\n3688:\t\tunsigned long addr;\n3689:\t\tint total = 0;\n3690:\t\tint young = 0;\n3691:\t\tstruct folio *last = NULL;\n3692:\t\tstruct lru_gen_mm_walk *walk = args-\u003eprivate;\n3693:\t\tstruct mem_cgroup *memcg = lruvec_memcg(walk-\u003elruvec);\n3694:\t\tstruct pglist_data *pgdat = lruvec_pgdat(walk-\u003elruvec);\n3695:\t\tunsigned int nr;\n3696:\t\tpmd_t pmdval;\n3697:\t\n3698:\t\tpte = pte_offset_map_rw_nolock(args-\u003emm, pmd, start \u0026 PMD_MASK, \u0026pmdval, \u0026ptl);\n3699:\t\tif (!pte)\n3700:\t\t\treturn false;\n3701:\t\n3702:\t\tif (!spin_trylock(ptl)) {\n3703:\t\t\tpte_unmap(pte);\n3704:\t\t\treturn true;\n3705:\t\t}\n3706:\t\n3707:\t\tif (unlikely(!pmd_same(pmdval, pmdp_get_lockless(pmd)))) {\n3708:\t\t\tpte_unmap_unlock(pte, ptl);\n3709:\t\t\treturn false;\n3710:\t\t}\n3711:\t\n3712:\t\tlazy_mmu_mode_enable();\n3713:\trestart:\n3714:\t\tfor (i = pte_index(start), addr = start; addr != end; i += nr, addr += nr * PAGE_SIZE) {\n3715:\t\t\tunsigned long pfn;\n3716:\t\t\tstruct folio *folio;\n3717:\t\t\tpte_t *cur_pte = pte + i;\n3718:\t\t\tpte_t ptent = ptep_get(cur_pte);\n3719:\t\n"
}

15/2 2026/11/02 22:01 llm patch-evaluator
1m Model:
gemini-3.8-flash

Args:
null

Results:
null

Tokens:
							input: 23824
							output: 33
							thoughts: 1140
						

Thoughts:
**Analyzing a Multi-Gen LRU Bug: A Deep Dive into Page Table Manipulation**

Okay, let's break this down. I'm looking at `pte_offset_map_rw_nolock`, specifically line 3707 where we're comparing PMD values. It's immediately apparent that the code is doing something fundamentally wrong. We're reading the PMD value *from a stack variable* (`&val`), instead of from the actual, live PMD in the page table using `pmdp_get_lockless(&val)`. That's a classic off-by-one error or more subtly an uninitialized or incorrect value. The `walk_pmd_range` and `walk_pud_range` functions are then being passed pointers to *these stack variables* (`&val` and `&val`), when they should be operating on the actual page table entries (`pud + i`). That's definitely a bug within the multi-gen LRU (LRU_GEN) implementation. This is going to corrupt the mappings, or otherwise lead to incorrect page table walks.

Now, I need to understand the full scope of this issue. I can't directly run `git log`, but I can inspect the patch to see what else was touched. The sheer breadth of changes is interesting:

*   We have `Documentation/mm/page_tables.rst`, which may shed some light on the design.
*   We're touching various architecture-specific include files (`arch/arm`, `arch/arm64`, `arch/loongarch`, `arch/mips`, `arch/openrisc`, `arch/riscv`, `arch/x86`). This is not surprising given the nature of page table manipulation, which is architecture-dependent.
*   We see `arch/loongarch/kvm/mmu.c`, `arch/riscv/kvm/mmu.c` which makes me wonder if this is related to a KVM interaction, possibly some KVM feature support that's being introduced or modified.
*   `arch/riscv/mm/fault.c` could be related if the errors are leading to faults.
*   The x86 changes include `direct_gbpages_enabled()` which indicates changes in how large pages are handled. The usage of this helper in several x86 kernel files (CPU, kexec, init, and PAT) is interesting. Also the changes in x86's pagetable code (`dump_pagetables.c` and `init.c`, `pat/set_memory.c`).
*   The generic include files (`pgtable-nop4d.h`, `pgtable-nopmd.h`, `pgtable-nopud.h`) are particularly revealing. They contain a lot of changes in the dummy and offset functions. The changes to `enum pgtable_level` in `linux/pgtable.h` and the addition of `mm_first_pgtable_level()` are especially relevant. This shows the underlying data structures, and the ordering change is likely significant here.
*   Finally, the critical fix: `mm/vmscan.c` where `walk_pmd_range` and `walk_pud_range` are modified to pass the *correct* pointers, which is exactly the bug I identified. These are, of course, the heart of the MGLRU code!

So, the bug is a mis-use of the `walk_pmd_range` and `walk_pud_range` functions in `mm/vmscan.c`, likely leading to incorrect page table manipulation and potentially causing page faults or memory corruption. The patch fixes this issue by correctly passing page table entries instead of stack variables. I need to now check how `mm/vmscan.c` itself works to fully grasp the bug. I also need to find the call sites of `walk_pud_range` to understand its full context within MGLRU.



16/2 2026/11/02 22:03 tool grepper
0m Args:
{
  "Expression": "walk_pud_range",
  "PathPrefixes": [
    "mm/vmscan.c"
  ]
}

Results:
{
  "Output": "mm/vmscan.c=3856=static void walk_pmd_range(pud_t *pud, unsigned long start, unsigned long end,\n--\nmm/vmscan.c-3928-\nmm/vmscan.c:3929:static int walk_pud_range(p4d_t *p4d, unsigned long start, unsigned long end,\nmm/vmscan.c-3930-\t\t\t  struct mm_walk *args)\n--\nmm/vmscan.c=3971=static void walk_mm(struct mm_struct *mm, struct lru_gen_mm_walk *walk)\n--\nmm/vmscan.c-3974-\t\t.test_walk = should_skip_vma,\nmm/vmscan.c:3975:\t\t.p4d_entry = walk_pud_range,\nmm/vmscan.c-3976-\t\t.walk_lock = PGWALK_RDLOCK,\n"
}

17/2 2026/11/02 22:03 llm patch-evaluator
0m Model:
gemini-3.8-flash

Args:
null

Results:
null

Tokens:
							input: 26098
							output: 30
							thoughts: 74
						

Thoughts:
Here's my understanding of the situation, given my expertise:

**Diving into the MGLRU Walk Function**

Okay, so I'm looking at how MGLRU's page walking works, specifically the `walk_mm()` function. It's clear that `walk_mm()` is a crucial component because it's responsible for iterating through the memory management structures associated with a specific memory space (`mm_struct`). It looks like `walk_mm()` uses `walk_pud_range()` and, likely indirectly, `walk_page_range()`. That's the core of how the pages are scanned, likely for the MGLRU replacement strategy. My next step, naturally, is to trace back and understand *how* and *why* `walk_mm()` is called. I need to figure out where `walk_mm()` is actually used. Also, I have to check all related configurations which can effect behavior. This will give me crucial context. This should help me confirm that this code is running as it should.



18/2 2026/11/02 22:03 tool grepper
0m Args:
{
  "Expression": "walk_mm",
  "PathPrefixes": [
    "mm/vmscan.c"
  ]
}

Results:
{
  "Output": "mm/vmscan.c=2819=DEFINE_STATIC_KEY_ARRAY_FALSE(lru_gen_caps, NR_LRU_GEN_CAPS);\n--\nmm/vmscan.c-2822-\nmm/vmscan.c:2823:static bool should_walk_mmu(void)\nmm/vmscan.c-2824-{\n--\nmm/vmscan.c=3929=static int walk_pud_range(p4d_t *p4d, unsigned long start, unsigned long end,\n--\nmm/vmscan.c-3970-\nmm/vmscan.c:3971:static void walk_mm(struct mm_struct *mm, struct lru_gen_mm_walk *walk)\nmm/vmscan.c-3972-{\n--\nmm/vmscan.c=4251=static bool try_to_inc_max_seq(struct lruvec *lruvec, unsigned long seq,\n--\nmm/vmscan.c-4274-\t */\nmm/vmscan.c:4275:\tif (!should_walk_mmu()) {\nmm/vmscan.c-4276-\t\tsuccess = iterate_mm_list_nowalk(lruvec, seq);\n--\nmm/vmscan.c-4293-\t\tif (mm)\nmm/vmscan.c:4294:\t\t\twalk_mm(mm, walk);\nmm/vmscan.c-4295-\t} while (mm);\n--\nmm/vmscan.c=5654=static ssize_t enabled_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf)\n--\nmm/vmscan.c-5660-\nmm/vmscan.c:5661:\tif (should_walk_mmu())\nmm/vmscan.c-5662-\t\tcaps |= BIT(LRU_GEN_MM_WALK);\n"
}

19/2 2026/11/02 22:03 llm patch-evaluator
0m Model:
gemini-3.8-flash

Args:
null

Results:
null

Tokens:
							input: 26914
							output: 34
							thoughts: 656
						

Thoughts:
**Deep Dive into Core Memory Management and x86 Architecture**

Okay, this is some seriously low-level stuff – we're talking core memory management here! I see references to `LRU_GEN`, which I immediately recognize as linked to `CONFIG_LRU_GEN`, a key parameter. But, this isn't the whole picture. I need to understand this in the context of x86-64, my expertise lies in this system.

Let's dig into the x86 changes. Specifically, I'm drawn to changes in `arch/x86/mm/dump_pagetables.c`. I see `effective_prot()` being modified to use `mm_first_pgtable_level` and `PGTABLE_LEVEL_PTE` etc. This is fascinating; I know these changes impact how we dump page tables, and I recall debugfs being a useful tool (`/sys/kernel/debug/page_tables/...`). The call stack looks straightforward: `ptdump_walk_pgd_level_core()`.

Now, moving over to `arch/x86/mm/pat/set_memory.c`, the mention of `direct_gbpages_enabled()` is a critical flag. I see that it's related to `collapse_pud_page()` and `populate_pud()`, and is used in a few key locations. The condition is now using the function `direct_gbpages_enabled()` instead of the global `direct_gbpages`. I need to figure out how this is enabled, so I'll check its definition.

Looking at the definition: the function is checking the direct map config option (`CONFIG_X86_DIRECT_GBPAGES`), the CPU feature (`X86_FEATURE_GBPAGES`), and the command line, plus an early setup variable. Makes sense!

But wait, I need to consider a potential wrinkle here. Let me check `include/asm-generic/pgtable-nop4d.h`. I need to confirm whether x86_64 uses nop4d. The `CONFIG_PGTABLE_LEVELS` configuration option is the obvious thing to check for this! I'll look into `arch/x86/Kconfig` or `arch/x86/include/asm/pgtable_types.h` to see how the number of page table levels is configured. This will give me a better understanding of how the x86 page table implementation works.



20/2 2026/11/02 22:03 tool grepper
0m Args:
{
  "Expression": "pgtable-nop4d\\.h",
  "PathPrefixes": [
    "arch/x86"
  ]
}

Results:
{
  "Output": "arch/x86/include/asm/pgtable_types.h=349=static inline p4dval_t native_p4d_val(p4d_t p4d)\n--\narch/x86/include/asm/pgtable_types.h-353-#else\narch/x86/include/asm/pgtable_types.h:354:#include \u003casm-generic/pgtable-nop4d.h\u003e\narch/x86/include/asm/pgtable_types.h-355-\n"
}

21/2 2026/11/02 22:03 llm patch-evaluator
0m Model:
gemini-3.8-flash

Args:
null

Results:
null

Tokens:
							input: 28274
							output: 44
							thoughts: 29
						

Thoughts:
Okay, let's dive into this. Here's my take on those lines in `arch/x86/include/asm/pgtable_types.h`:

**Analyzing Page Table Types in the x86 Architecture**

Right, let's take a look at `arch/x86/include/asm/pgtable_types.h`, specifically around line 350. I'm expecting to see the core definitions for the page table structures themselves. Given my familiarity with the x86 memory management model, I'm anticipating this is where the `pml4_t`, `pdpt_t`, `pdt_t`, and `pte_t` types are declared, probably along with some critical bitfield definitions that govern how these entries are interpreted. I'll be interested to check for any architecture-specific extensions or features, like PAT (Page Attribute Table) support or anything related to larger page sizes (2MB/1GB). My focus will be on understanding how these types lay the foundation for the hierarchical paging scheme, as well as checking for any nuances that might influence how these structures are handled by the kernel – specifically for the current memory management implementation.



22/2 2026/11/02 22:04 tool read-file
0m Args:
{
  "File": "arch/x86/include/asm/pgtable_types.h",
  "FirstLine": 340,
  "LineCount": 25
}

Results:
{
  "Contents": " 340:\t\n 341:\t#if CONFIG_PGTABLE_LEVELS \u003e 4\n 342:\ttypedef struct { p4dval_t p4d; } p4d_t;\n 343:\t\n 344:\tstatic inline p4d_t native_make_p4d(pudval_t val)\n 345:\t{\n 346:\t\treturn (p4d_t) { val };\n 347:\t}\n 348:\t\n 349:\tstatic inline p4dval_t native_p4d_val(p4d_t p4d)\n 350:\t{\n 351:\t\treturn p4d.p4d;\n 352:\t}\n 353:\t#else\n 354:\t#include \u003casm-generic/pgtable-nop4d.h\u003e\n 355:\t\n 356:\tstatic inline p4d_t native_make_p4d(pudval_t val)\n 357:\t{\n 358:\t\treturn (p4d_t) { .pgd = native_make_pgd((pgdval_t)val) };\n 359:\t}\n 360:\t\n 361:\tstatic inline p4dval_t native_p4d_val(p4d_t p4d)\n 362:\t{\n 363:\t\treturn native_pgd_val(p4d.pgd);\n 364:\t}\n"
}

23/2 2026/11/02 22:04 llm patch-evaluator
0m Model:
gemini-3.8-flash

Args:
null

Results:
null

Tokens:
							input: 29001
							output: 31
							thoughts: 130
						

Thoughts:
**My Current Paging Puzzle**

Okay, so I'm focusing on x86_64 architecture here. My initial assumption was that with `CONFIG_PGTABLE_LEVELS` set to 4 – which is, let's face it, the *standard* 4-level paging setup for x86_64 unless we've explicitly enabled 5-level paging with `CONFIG_X86_5LEVEL`, or even if `CONFIG_X86_5LEVEL` is *disabled* – the kernel would use `asm-generic/pgtable-nop4d.h`. That makes perfect sense, at least at first glance.

But then... a little alarm bell started ringing in the back of my head. What happens if `CONFIG_X86_5LEVEL` *is* enabled? Does that change the picture? I need to double-check that. I need to get back to basics and figure out exactly how `CONFIG_PGTABLE_LEVELS` is defined within the x86 architecture. I need to trace that logic out.



24/2 2026/11/02 22:04 tool grepper
0m Args:
{
  "Expression": "PGTABLE_LEVELS",
  "PathPrefixes": [
    "arch/x86"
  ]
}

Results:
{
  "Output": "arch/x86/Kconfig=57=config X86\n--\narch/x86/Kconfig-70-\tselect ARCH_ENABLE_MEMORY_HOTPLUG if X86_64\narch/x86/Kconfig:71:\tselect ARCH_ENABLE_SPLIT_PMD_PTLOCK if (PGTABLE_LEVELS \u003e 2) \u0026\u0026 (X86_64 || X86_PAE)\narch/x86/Kconfig-72-\tselect ARCH_HAS_PMD_SOFTLEAVES if X86_64 \u0026\u0026 TRANSPARENT_HUGEPAGE\n--\narch/x86/Kconfig-101-\tselect ARCH_HAS_HW_PTE_YOUNG\narch/x86/Kconfig:102:\tselect ARCH_HAS_NONLEAF_PMD_YOUNG\tif PGTABLE_LEVELS \u003e 2\narch/x86/Kconfig-103-\tselect ARCH_HAS_UACCESS_FLUSHCACHE\tif X86_64\n--\narch/x86/Kconfig=426=config DYNAMIC_PHYSICAL_MASK\n--\narch/x86/Kconfig-428-\narch/x86/Kconfig:429:config PGTABLE_LEVELS\narch/x86/Kconfig-430-\tint\n--\narch/x86/entry/vdso/vdso32/fake_32bit_build.h-10-#undef CONFIG_COMPAT\narch/x86/entry/vdso/vdso32/fake_32bit_build.h:11:#undef CONFIG_PGTABLE_LEVELS\narch/x86/entry/vdso/vdso32/fake_32bit_build.h-12-#undef CONFIG_ILLEGAL_POINTER_VALUE\n--\narch/x86/entry/vdso/vdso32/fake_32bit_build.h-18-#define CONFIG_X86_32 1\narch/x86/entry/vdso/vdso32/fake_32bit_build.h:19:#define CONFIG_PGTABLE_LEVELS 2\narch/x86/entry/vdso/vdso32/fake_32bit_build.h-20-#define CONFIG_PAGE_OFFSET 0\n--\narch/x86/include/asm/pgalloc.h=76=static inline void pmd_populate(struct mm_struct *mm, pmd_t *pmd,\n--\narch/x86/include/asm/pgalloc.h-84-\narch/x86/include/asm/pgalloc.h:85:#if CONFIG_PGTABLE_LEVELS \u003e 2\narch/x86/include/asm/pgalloc.h-86-extern void ___pmd_free_tlb(struct mmu_gather *tlb, pmd_t *pmd);\n--\narch/x86/include/asm/pgalloc.h=103=static inline void pud_populate_safe(struct mm_struct *mm, pud_t *pud, pmd_t *pmd)\n--\narch/x86/include/asm/pgalloc.h-109-\narch/x86/include/asm/pgalloc.h:110:#if CONFIG_PGTABLE_LEVELS \u003e 3\narch/x86/include/asm/pgalloc.h-111-static inline void p4d_populate(struct mm_struct *mm, p4d_t *p4d, pud_t *pud)\n--\narch/x86/include/asm/pgalloc.h=125=static inline void __pud_free_tlb(struct mmu_gather *tlb, pud_t *pud,\n--\narch/x86/include/asm/pgalloc.h-130-\narch/x86/include/asm/pgalloc.h:131:#if CONFIG_PGTABLE_LEVELS \u003e 4\narch/x86/include/asm/pgalloc.h-132-static inline void pgd_populate(struct mm_struct *mm, pgd_t *pgd, p4d_t *p4d)\n--\narch/x86/include/asm/pgalloc.h=150=static inline void __p4d_free_tlb(struct mmu_gather *tlb, p4d_t *p4d,\n--\narch/x86/include/asm/pgalloc.h-156-\narch/x86/include/asm/pgalloc.h:157:#endif\t/* CONFIG_PGTABLE_LEVELS \u003e 4 */\narch/x86/include/asm/pgalloc.h:158:#endif\t/* CONFIG_PGTABLE_LEVELS \u003e 3 */\narch/x86/include/asm/pgalloc.h:159:#endif\t/* CONFIG_PGTABLE_LEVELS \u003e 2 */\narch/x86/include/asm/pgalloc.h-160-\n--\narch/x86/include/asm/pgtable.h=1031=static inline unsigned long pages_to_mb(unsigned long npg)\n--\narch/x86/include/asm/pgtable.h-1035-\narch/x86/include/asm/pgtable.h:1036:#if CONFIG_PGTABLE_LEVELS \u003e 2\narch/x86/include/asm/pgtable.h-1037-static inline int pud_none(pud_t pud)\n--\narch/x86/include/asm/pgtable.h=1064=static inline int pud_bad(pud_t pud)\n--\narch/x86/include/asm/pgtable.h-1067-}\narch/x86/include/asm/pgtable.h:1068:#endif\t/* CONFIG_PGTABLE_LEVELS \u003e 2 */\narch/x86/include/asm/pgtable.h-1069-\narch/x86/include/asm/pgtable.h:1070:#if CONFIG_PGTABLE_LEVELS \u003e 3\narch/x86/include/asm/pgtable.h-1071-static inline int p4d_none(p4d_t p4d)\n--\narch/x86/include/asm/pgtable.h=1092=static inline int p4d_bad(p4d_t p4d)\n--\narch/x86/include/asm/pgtable.h-1100-}\narch/x86/include/asm/pgtable.h:1101:#endif  /* CONFIG_PGTABLE_LEVELS \u003e 3 */\narch/x86/include/asm/pgtable.h-1102-\narch/x86/include/asm/pgtable.h=1103=static inline unsigned long p4d_index(unsigned long address)\n--\narch/x86/include/asm/pgtable.h-1107-\narch/x86/include/asm/pgtable.h:1108:#if CONFIG_PGTABLE_LEVELS \u003e 4\narch/x86/include/asm/pgtable.h-1109-static inline int pgd_present(pgd_t pgd)\n--\narch/x86/include/asm/pgtable.h=1148=static inline int pgd_none(pgd_t pgd)\n--\narch/x86/include/asm/pgtable.h-1159-}\narch/x86/include/asm/pgtable.h:1160:#endif\t/* CONFIG_PGTABLE_LEVELS \u003e 4 */\narch/x86/include/asm/pgtable.h-1161-\n--\narch/x86/include/asm/pgtable_types.h=336=static inline pgdval_t pgd_flags(pgd_t pgd)\n--\narch/x86/include/asm/pgtable_types.h-340-\narch/x86/include/asm/pgtable_types.h:341:#if CONFIG_PGTABLE_LEVELS \u003e 4\narch/x86/include/asm/pgtable_types.h-342-typedef struct { p4dval_t p4d; } p4d_t;\n--\narch/x86/include/asm/pgtable_types.h=361=static inline p4dval_t native_p4d_val(p4d_t p4d)\n--\narch/x86/include/asm/pgtable_types.h-366-\narch/x86/include/asm/pgtable_types.h:367:#if CONFIG_PGTABLE_LEVELS \u003e 3\narch/x86/include/asm/pgtable_types.h-368-typedef struct { pudval_t pud; } pud_t;\n--\narch/x86/include/asm/pgtable_types.h=387=static inline pudval_t native_pud_val(pud_t pud)\n--\narch/x86/include/asm/pgtable_types.h-392-\narch/x86/include/asm/pgtable_types.h:393:#if CONFIG_PGTABLE_LEVELS \u003e 2\narch/x86/include/asm/pgtable_types.h-394-static inline pmd_t native_make_pmd(pmdval_t val)\n--\narch/x86/kernel/cpu/bugs.c=2832=static void __init l1tf_apply_mitigation(void)\n--\narch/x86/kernel/cpu/bugs.c-2855-\narch/x86/kernel/cpu/bugs.c:2856:#if CONFIG_PGTABLE_LEVELS == 2\narch/x86/kernel/cpu/bugs.c-2857-\tpr_warn(\"Kernel not compiled for PAE. No mitigation for L1TF\\n\");\n--\narch/x86/mm/init.c=1034=unsigned long arch_max_swapfile_size(void)\n--\narch/x86/mm/init.c-1046-\t\t */\narch/x86/mm/init.c:1047:#if CONFIG_PGTABLE_LEVELS \u003e 2\narch/x86/mm/init.c-1048-\t\tl1tf_limit \u003c\u003c= PAGE_SHIFT - SWP_OFFSET_FIRST_BIT;\n--\narch/x86/mm/pgtable.c=21=void ___pte_free_tlb(struct mmu_gather *tlb, struct page *pte)\n--\narch/x86/mm/pgtable.c-26-\narch/x86/mm/pgtable.c:27:#if CONFIG_PGTABLE_LEVELS \u003e 2\narch/x86/mm/pgtable.c-28-void ___pmd_free_tlb(struct mmu_gather *tlb, pmd_t *pmd)\n--\narch/x86/mm/pgtable.c-40-\narch/x86/mm/pgtable.c:41:#if CONFIG_PGTABLE_LEVELS \u003e 3\narch/x86/mm/pgtable.c-42-void ___pud_free_tlb(struct mmu_gather *tlb, pud_t *pud)\n--\narch/x86/mm/pgtable.c-47-\narch/x86/mm/pgtable.c:48:#if CONFIG_PGTABLE_LEVELS \u003e 4\narch/x86/mm/pgtable.c-49-void ___p4d_free_tlb(struct mmu_gather *tlb, p4d_t *p4d)\n--\narch/x86/mm/pgtable.c-53-}\narch/x86/mm/pgtable.c:54:#endif\t/* CONFIG_PGTABLE_LEVELS \u003e 4 */\narch/x86/mm/pgtable.c:55:#endif\t/* CONFIG_PGTABLE_LEVELS \u003e 3 */\narch/x86/mm/pgtable.c:56:#endif\t/* CONFIG_PGTABLE_LEVELS \u003e 2 */\narch/x86/mm/pgtable.c-57-\n--\narch/x86/mm/pgtable.c=574=void native_set_fixmap(unsigned /* enum fixed_addresses */ idx,\n--\narch/x86/mm/pgtable.c-583-#ifdef CONFIG_HAVE_ARCH_HUGE_VMAP\narch/x86/mm/pgtable.c:584:#if CONFIG_PGTABLE_LEVELS \u003e 4\narch/x86/mm/pgtable.c-585-/**\n"
}

25/2 2026/11/02 22:04 llm patch-evaluator
0m Model:
gemini-3.8-flash

Args:
null

Results:
null

Tokens:
							input: 32537
							output: 26
							thoughts: 1360
						

Thoughts:
**Diving Deep into x86_64 Paging and Potential Vulnerabilities**

Okay, let's break this down. On x86_64, `CONFIG_PGTABLE_LEVELS` can be either 4 or 5. Now, when it's the standard 4-level paging setup, `CONFIG_PGTABLE_LEVELS > 4` is false, and therefore, `<asm-generic/pgtable-nop4d.h>` is included. In this file, `p4d_offset` is defined such that it checks whether the pgd is a dummy, and if so, it throws a compile-time error.  However, `pgdp_get()` returns a dummy pgd with zeros.

If we have a scenario where `pgdp_get()` returns this dummy, and then someone calls `p4d_offset` with it, or passes this `pgd` to `pgd_check_dummy`, we have an issue. Likewise, there might be other issues if someone calls `p4d_offset_lockless` with it! The potential for misuse is clear.

I'm now thinking about the `mm/vmscan.c` code. Specifically, the `walk_pud_range` and `walk_pmd_range` functions.  I see a bug there! Before the fix,  `walk_pte_range` was called with a stack variable passed as a `pmd`! Similarly, `walk_pmd_range` in the past was given the *address* of a stack variable that held the `pud`. This means, when paging is folded, this would've caused issues! We were passing a pointer to a stack variable, which is incorrect.  The corrected version now passes the correct pointer to the page table entry, fixing the underlying bug!

Finally, I'm analyzing the changes in `arch/x86/mm/dump_pagetables.c`. The `enum pgtable_level` has been reordered. Previously, the order was PTE, PMD, PUD, P4D, PGD. Now, it's PGD, P4D, PUD, PMD, PTE.  That's a significant shift. And I am looking at how `effective_prot` handled level checking. Specifically, `if (level > mm_first_pgtable_level(st->mm))` has a problem. The old ordering meant, for PGD (level 0), `level - 1` would be out of bounds, which would lead to an uninitialized variable, possibly leading to a security issue. Now, with the new ordering, `level == 0` is PGD, but it could still be an issue if the levels are folded (like when P4D is folded into PGD on x86, which is flagged via `mm_p4d_folded`). So I'm now looking at the definition of `mm_p4d_folded`.



26/2 2026/11/02 22:05 tool grepper
0m Args:
{
  "Expression": "__PAGETABLE_P4D_FOLDED"
}

Results:
{
  "Output": "arch/arm64/mm/mmu.c=1878=int pmd_set_huge(pmd_t *pmdp, phys_addr_t phys, pgprot_t prot)\n--\narch/arm64/mm/mmu.c-1891-\narch/arm64/mm/mmu.c:1892:#ifndef __PAGETABLE_P4D_FOLDED\narch/arm64/mm/mmu.c-1893-void p4d_clear_huge(p4d_t *p4dp)\n--\narch/loongarch/mm/kasan_init.c=14=static pgd_t kasan_pg_dir[PTRS_PER_PGD] __initdata __aligned(PAGE_SIZE);\narch/loongarch/mm/kasan_init.c-15-\narch/loongarch/mm/kasan_init.c:16:#ifdef __PAGETABLE_P4D_FOLDED\narch/loongarch/mm/kasan_init.c-17-#define __pgd_none(early, pgd) (0)\n--\narch/x86/include/asm/pgtable.h=55=extern pmdval_t early_pmd_flags;\n--\narch/x86/include/asm/pgtable.h-66-\narch/x86/include/asm/pgtable.h:67:#ifndef __PAGETABLE_P4D_FOLDED\narch/x86/include/asm/pgtable.h-68-#define set_pgd(pgdp, pgd)\t\tnative_set_pgd(pgdp, pgd)\n--\narch/x86/include/asm/pgtable.h=70=do {\t\t\t\t\t\\\n--\narch/x86/include/asm/pgtable.h-97-\narch/x86/include/asm/pgtable.h:98:#ifndef __PAGETABLE_P4D_FOLDED\narch/x86/include/asm/pgtable.h-99-#define p4d_val(x)\tnative_p4d_val(x)\n--\narch/x86/include/asm/xen/page.h=329=static inline pte_t __pte_ma(pteval_t x)\n--\narch/x86/include/asm/xen/page.h-341-\narch/x86/include/asm/xen/page.h:342:#ifdef __PAGETABLE_P4D_FOLDED\narch/x86/include/asm/xen/page.h-343-#define p4d_val_ma(x)\t((x).pgd.pgd)\n--\ninclude/asm-generic/pgtable-nop4d.h-6-\ninclude/asm-generic/pgtable-nop4d.h:7:#define __PAGETABLE_P4D_FOLDED 1\ninclude/asm-generic/pgtable-nop4d.h-8-\n--\ninclude/linux/mm.h=3748=extern pte_t *get_locked_pte(struct mm_struct *mm, unsigned long addr,\n--\ninclude/linux/mm.h-3750-\ninclude/linux/mm.h:3751:#ifdef __PAGETABLE_P4D_FOLDED\ninclude/linux/mm.h-3752-static inline int __p4d_alloc(struct mm_struct *mm, pgd_t *pgd,\n--\ninclude/linux/pgtable.h-19-\ninclude/linux/pgtable.h:20:#if 5 - defined(__PAGETABLE_P4D_FOLDED) - defined(__PAGETABLE_PUD_FOLDED) - \\\ninclude/linux/pgtable.h-21-\tdefined(__PAGETABLE_PMD_FOLDED) != CONFIG_PGTABLE_LEVELS\n--\ninclude/linux/pgtable.h=392=static inline void lazy_mmu_mode_resume(void) {}\n--\ninclude/linux/pgtable.h-399-#ifndef mm_p4d_folded\ninclude/linux/pgtable.h:400:#define mm_p4d_folded(mm)\t__is_defined(__PAGETABLE_P4D_FOLDED)\ninclude/linux/pgtable.h-401-#endif\n--\ninclude/linux/pgtable.h=1542=void pgd_clear_bad(pgd_t *);\ninclude/linux/pgtable.h-1543-\ninclude/linux/pgtable.h:1544:#ifndef __PAGETABLE_P4D_FOLDED\ninclude/linux/pgtable.h-1545-void p4d_clear_bad(p4d_t *);\n--\ninclude/linux/pgtable.h=2155=static inline int pmd_protnone(pmd_t pmd)\n--\ninclude/linux/pgtable.h-2168-\ninclude/linux/pgtable.h:2169:#ifndef __PAGETABLE_P4D_FOLDED\ninclude/linux/pgtable.h-2170-int p4d_set_huge(p4d_t *p4d, phys_addr_t addr, pgprot_t prot);\n--\ninclude/linux/pgtable.h=2177=static inline void p4d_clear_huge(p4d_t *p4d) { }\ninclude/linux/pgtable.h:2178:#endif /* !__PAGETABLE_P4D_FOLDED */\ninclude/linux/pgtable.h-2179-\n--\nmm/debug_vm_pgtable.c=530=static void __init pud_populate_tests(struct pgtable_debug_args *args) { }\n--\nmm/debug_vm_pgtable.c-532-\nmm/debug_vm_pgtable.c:533:#ifndef __PAGETABLE_P4D_FOLDED\nmm/debug_vm_pgtable.c-534-static void __init p4d_clear_tests(struct pgtable_debug_args *args)\n--\nmm/debug_vm_pgtable.c=581=static void __init pgd_populate_tests(struct pgtable_debug_args *args)\n--\nmm/debug_vm_pgtable.c-598-}\nmm/debug_vm_pgtable.c:599:#else  /* !__PAGETABLE_P4D_FOLDED */\nmm/debug_vm_pgtable.c-600-static void __init p4d_clear_tests(struct pgtable_debug_args *args) { }\n--\nmm/memory.c=7006=EXPORT_SYMBOL_GPL(handle_mm_fault);\nmm/memory.c-7007-\nmm/memory.c:7008:#ifndef __PAGETABLE_P4D_FOLDED\nmm/memory.c-7009-/*\n--\nmm/memory.c=7013=int __p4d_alloc(struct mm_struct *mm, pgd_t *pgd, unsigned long address)\n--\nmm/memory.c-7028-}\nmm/memory.c:7029:#endif /* __PAGETABLE_P4D_FOLDED */\nmm/memory.c-7030-\n--\nmm/pgtable-generic.c=27=void pgd_clear_bad(pgd_t *pgd)\n--\nmm/pgtable-generic.c-35-\nmm/pgtable-generic.c:36:#ifndef __PAGETABLE_P4D_FOLDED\nmm/pgtable-generic.c-37-void p4d_clear_bad(p4d_t *p4d)\n"
}

27/2 2026/11/02 22:05 llm patch-evaluator
0m Model:
gemini-3.8-flash

Args:
null

Results:
null

Tokens:
							input: 36483
							output: 43
							thoughts: 759
						

Thoughts:
**Analyzing Page Table Folding in x86**

Okay, let's break this down. First, I see that `include/asm-generic/pgtable-nop4d.h` defines `__PAGETABLE_P4D_FOLDED 1`. That's the key. Then, looking at `mm_first_pgtable_level()`, it correctly identifies that if P4D is folded, then the first level is actually the P4D. Folding means the page table level is effectively skipped by merging its function into the next higher level.

Now, let's get to `effective_prot` in `arch/x86/mm/dump_pagetables.c`. This is where it gets interesting. I'm examining how protection bits are handled at each page table level during the dump. The critical section is within the `if (level > mm_first_pgtable_level(st->mm))` block. This is what calculates effective protection based on the page table hierarchy.

On x86 without 5-level paging, where we are indeed using standard 4-level paging, `mm_p4d_folded(mm)` is true. So, `mm_first_pgtable_level()` *returns* `PGTABLE_LEVEL_P4D`, which is 1.

Consider this: When `level` is 1 (P4D), the `if` condition `(level > mm_first_pgtable_level(st->mm))` evaluates to false (1 > 1 is false). Consequently, `effective = prot;` and the effective protection for the P4D level is simply assigned the incoming protection bits.

However, when `level` is 2 (PUD), the `if` condition becomes true (2 > 1). Now, the effective protection calculation takes place. `higher_prot` becomes `st->prot_levels[1]`, which is the protection calculated and stored for the P4D level. The `effective` protection for PUD is a combination of bits from P4D and PUD, considering read/write and no execute flags.

So, in the dump process, on 4-level paging, the effective protection for P4D is determined directly and not derived from a higher level, but the effective PUD protection *is* computed based on P4D. I need to make sure I've properly understood the behavior for PGD, P4D, PUD, PMD and PTE.

The question that arises is: Did the dump actually walk P4D entries? I need to check how the code handling that level, specifically `ptdump_walk_pgd_level_core`, behaves.



28/2 2026/11/02 22:05 tool read-file
0m Args:
{
  "File": "arch/x86/mm/dump_pagetables.c",
  "FirstLine": 440,
  "LineCount": 45
}

Results:
{
  "Contents": " 440:\t\t\t\t.note_page_pte = note_page_pte,\n 441:\t\t\t\t.note_page_pmd = note_page_pmd,\n 442:\t\t\t\t.note_page_pud = note_page_pud,\n 443:\t\t\t\t.note_page_p4d = note_page_p4d,\n 444:\t\t\t\t.note_page_pgd = note_page_pgd,\n 445:\t\t\t\t.note_page_flush = note_page_flush,\n 446:\t\t\t\t.effective_prot_pte = effective_prot_pte,\n 447:\t\t\t\t.effective_prot_pmd = effective_prot_pmd,\n 448:\t\t\t\t.effective_prot_pud = effective_prot_pud,\n 449:\t\t\t\t.effective_prot_p4d = effective_prot_p4d,\n 450:\t\t\t\t.effective_prot_pgd = effective_prot_pgd,\n 451:\t\t\t\t.range\t\t= ptdump_ranges\n 452:\t\t\t},\n 453:\t\t\t.level = -1,\n 454:\t\t\t.to_dmesg\t= dmesg,\n 455:\t\t\t.check_wx\t= checkwx,\n 456:\t\t\t.seq\t\t= m,\n 457:\t\t\t.mm\t\t= mm,\n 458:\t\t};\n 459:\t\n 460:\t\tptdump_walk_pgd(\u0026st.ptdump, mm, pgd);\n 461:\t\n 462:\t\tif (!checkwx)\n 463:\t\t\treturn true;\n 464:\t\tif (st.wx_pages) {\n 465:\t\t\tpr_info(\"x86/mm: Checked W+X mappings: FAILED, %lu W+X pages found.\\n\",\n 466:\t\t\t\tst.wx_pages);\n 467:\t\n 468:\t\t\treturn false;\n 469:\t\t} else {\n 470:\t\t\tpr_info(\"x86/mm: Checked W+X mappings: passed, no W+X pages found.\\n\");\n 471:\t\n 472:\t\t\treturn true;\n 473:\t\t}\n 474:\t}\n 475:\t\n 476:\tvoid ptdump_walk_pgd_level(struct seq_file *m, struct mm_struct *mm)\n 477:\t{\n 478:\t\tptdump_walk_pgd_level_core(m, mm, mm-\u003epgd, false, true);\n 479:\t}\n 480:\t\n 481:\tvoid ptdump_walk_pgd_level_debugfs(struct seq_file *m, struct mm_struct *mm,\n 482:\t\t\t\t\t   bool user)\n 483:\t{\n 484:\t\tpgd_t *pgd = mm-\u003epgd;\n"
}

29/2 2026/11/02 22:05 llm patch-evaluator
0m Model:
gemini-3.8-flash

Args:
null

Results:
null

Tokens:
							input: 38610
							output: 23
							thoughts: 29
						

Thoughts:
Okay, let's dive into this. Here's my understanding, presented as a personal thought process:

**Diving into ptdump_walk_pgd**

Alright, I'm going to take a closer look at what's going on during this `ptdump_walk_pgd` process. The first thing that jumps to my mind is: where is this function even defined? I need to understand its scope and origin before I can truly grasp its role. I need to know the implementation details of `ptdump_walk_pgd` and the source files it belongs to.



30/2 2026/11/02 22:05 tool grepper
0m Args:
{
  "Expression": "void ptdump_walk_pgd"
}

Results:
{
  "Output": "arch/arm/include/asm/ptdump.h=16=struct ptdump_info {\n--\narch/arm/include/asm/ptdump.h-21-\narch/arm/include/asm/ptdump.h:22:void ptdump_walk_pgd(struct seq_file *s, struct ptdump_info *info);\narch/arm/include/asm/ptdump.h-23-#ifdef CONFIG_ARM_PTDUMP_DEBUGFS\n--\narch/arm/mm/dump.c=398=static void walk_pgd(struct pg_state *st, struct mm_struct *mm,\n--\narch/arm/mm/dump.c-414-\narch/arm/mm/dump.c:415:void ptdump_walk_pgd(struct seq_file *m, struct ptdump_info *info)\narch/arm/mm/dump.c-416-{\n--\narch/x86/include/asm/pgtable.h=30=struct seq_file;\narch/x86/include/asm/pgtable.h:31:void ptdump_walk_pgd_level(struct seq_file *m, struct mm_struct *mm);\narch/x86/include/asm/pgtable.h:32:void ptdump_walk_pgd_level_debugfs(struct seq_file *m, struct mm_struct *mm,\narch/x86/include/asm/pgtable.h-33-\t\t\t\t   bool user);\n--\narch/x86/mm/dump_pagetables.c=424=bool ptdump_walk_pgd_level_core(struct seq_file *m,\n--\narch/x86/mm/dump_pagetables.c-475-\narch/x86/mm/dump_pagetables.c:476:void ptdump_walk_pgd_level(struct seq_file *m, struct mm_struct *mm)\narch/x86/mm/dump_pagetables.c-477-{\n--\narch/x86/mm/dump_pagetables.c-480-\narch/x86/mm/dump_pagetables.c:481:void ptdump_walk_pgd_level_debugfs(struct seq_file *m, struct mm_struct *mm,\narch/x86/mm/dump_pagetables.c-482-\t\t\t\t   bool user)\n--\ninclude/linux/ptdump.h=28=bool ptdump_walk_pgd_level_core(struct seq_file *m,\n--\ninclude/linux/ptdump.h-30-\t\t\t\tbool checkwx, bool dmesg);\ninclude/linux/ptdump.h:31:void ptdump_walk_pgd(struct ptdump_state *st, struct mm_struct *mm, pgd_t *pgd);\ninclude/linux/ptdump.h-32-bool ptdump_check_wx(void);\n--\nmm/ptdump.c=166=static const struct mm_walk_ops ptdump_ops = {\n--\nmm/ptdump.c-174-\nmm/ptdump.c:175:void ptdump_walk_pgd(struct ptdump_state *st, struct mm_struct *mm, pgd_t *pgd)\nmm/ptdump.c-176-{\n"
}

31/2 2026/11/02 22:05 llm patch-evaluator
0m Model:
gemini-3.8-flash

Args:
null

Results:
null

Tokens:
							input: 39634
							output: 34
							thoughts: 19
						

Thoughts:
Okay, let's dive into this. Here's my take on those thoughts, as if they're my own:

**Focusing on `mm/ptdump.c` Around Line 175**

Alright, my internal radar is pinging on `mm/ptdump.c` around line 175. That suggests we're likely in the realm of page table dumping or introspection. Knowing that, I need to understand what the larger context is. What kind of debugging or analysis is prompting this line number? Am I looking at a specific bug, performance bottleneck, or perhaps just exploring the internals of the memory management system? Without further context I have to assume the best. It's safe to assume I'll be looking for something like:

*   **Page Table Structure:** I'll be expecting to see some code that accesses and perhaps interprets the layout of page table entries. The goal would be to look for specific flags, addresses, or other data within those entries.
*   **Data Structures:** It's likely I'll be looking at page table related data structures. This could involve traversing page tables, printing their contents, or potentially analyzing their integrity.
*   **Context:** What is the intent of the dump that's happening around this line? Is it for debugging a memory-related fault, or just a general inspection of memory mapping? Understanding the *why* helps narrow the focus of my analysis.
*   **Potential issues:** Based on the dump's purpose, I'll be asking myself questions such as, Is this dump printing valid data? Am I expecting certain values and are they there? If this is for debugging I might be looking for any hints about memory leaks, page faults, or other issues related to virtual memory management.

Basically, I need to consider the surrounding code and determine what that specific line is doing in relation to memory management, debugging, or analysis. I'm mentally prepared to jump into the code and start tracing the execution flow and data structures.



32/2 2026/11/02 22:06 tool read-file
0m Args:
{
  "File": "mm/ptdump.c",
  "FirstLine": 140,
  "LineCount": 50
}

Results:
{
  "Contents": " 140:\t\tpud_t pud_zero = {0};\n 141:\t\tp4d_t p4d_zero = {0};\n 142:\t\tpgd_t pgd_zero = {0};\n 143:\t\n 144:\t\tswitch (depth) {\n 145:\t\tcase 4:\n 146:\t\t\tst-\u003enote_page_pte(st, addr, pte_zero);\n 147:\t\t\tbreak;\n 148:\t\tcase 3:\n 149:\t\t\tst-\u003enote_page_pmd(st, addr, pmd_zero);\n 150:\t\t\tbreak;\n 151:\t\tcase 2:\n 152:\t\t\tst-\u003enote_page_pud(st, addr, pud_zero);\n 153:\t\t\tbreak;\n 154:\t\tcase 1:\n 155:\t\t\tst-\u003enote_page_p4d(st, addr, p4d_zero);\n 156:\t\t\tbreak;\n 157:\t\tcase 0:\n 158:\t\t\tst-\u003enote_page_pgd(st, addr, pgd_zero);\n 159:\t\t\tbreak;\n 160:\t\tdefault:\n 161:\t\t\tbreak;\n 162:\t\t}\n 163:\t\treturn 0;\n 164:\t}\n 165:\t\n 166:\tstatic const struct mm_walk_ops ptdump_ops = {\n 167:\t\t.pgd_entry\t= ptdump_pgd_entry,\n 168:\t\t.p4d_entry\t= ptdump_p4d_entry,\n 169:\t\t.pud_entry\t= ptdump_pud_entry,\n 170:\t\t.pmd_entry\t= ptdump_pmd_entry,\n 171:\t\t.pte_entry\t= ptdump_pte_entry,\n 172:\t\t.pte_hole\t= ptdump_hole,\n 173:\t};\n 174:\t\n 175:\tvoid ptdump_walk_pgd(struct ptdump_state *st, struct mm_struct *mm, pgd_t *pgd)\n 176:\t{\n 177:\t\tconst struct ptdump_range *range = st-\u003erange;\n 178:\t\n 179:\t\tget_online_mems();\n 180:\t\tmmap_write_lock(mm);\n 181:\t\t/* To stabilise kernel page tables we must hold the init_mm lock too. */\n 182:\t\tif (mm != \u0026init_mm)\n 183:\t\t\tmmap_write_lock_nested(\u0026init_mm, SINGLE_DEPTH_NESTING);\n 184:\t\n 185:\t\twhile (range-\u003estart != range-\u003eend) {\n 186:\t\t\twalk_page_range_debug(mm, range-\u003estart, range-\u003eend,\n 187:\t\t\t\t\t      \u0026ptdump_ops, pgd, st);\n 188:\t\t\trange++;\n 189:\t\t}\n"
}

33/2 2026/11/02 22:06 llm patch-evaluator
0m Model:
gemini-3.8-flash

Args:
null

Results:
null

Tokens:
							input: 40906
							output: 33
							thoughts: 36
						

Thoughts:
Okay, let's dive into this. I'm focusing on `mm/ptdump.c` and specifically, what's going on within `ptdump_pgd_entry` and `ptdump_p4d_entry`. I know these functions are crucial for understanding page table structures, but I want to get a much finer grain understanding of the execution paths within them. This is basic, but necessary to be thorough, I need to know exactly what operations are being performed on the page table entries at each level. Knowing this will help when I have to debug and analyze memory-related issues.



34/2 2026/11/02 22:06 tool read-file
0m Args:
{
  "File": "mm/ptdump.c",
  "FirstLine": 80,
  "LineCount": 55
}

Results:
{
  "Contents": "  80:\t\tpud_t val = pudp_get(pud);\n  81:\t\n  82:\t#if CONFIG_PGTABLE_LEVELS \u003e 2 \u0026\u0026 \\\n  83:\t\t\t(defined(CONFIG_KASAN_GENERIC) || defined(CONFIG_KASAN_SW_TAGS))\n  84:\t\tif (pud_page(val) == virt_to_page(lm_alias(kasan_early_shadow_pmd)))\n  85:\t\t\treturn note_kasan_page_table(walk, addr);\n  86:\t#endif\n  87:\t\n  88:\t\tif (st-\u003eeffective_prot_pud)\n  89:\t\t\tst-\u003eeffective_prot_pud(st, val);\n  90:\t\n  91:\t\tif (pud_leaf(val)) {\n  92:\t\t\tst-\u003enote_page_pud(st, addr, val);\n  93:\t\t\twalk-\u003eaction = ACTION_CONTINUE;\n  94:\t\t}\n  95:\t\n  96:\t\treturn 0;\n  97:\t}\n  98:\t\n  99:\tstatic int ptdump_pmd_entry(pmd_t *pmd, unsigned long addr,\n 100:\t\t\t\t    unsigned long next, struct mm_walk *walk)\n 101:\t{\n 102:\t\tstruct ptdump_state *st = walk-\u003eprivate;\n 103:\t\tpmd_t val = pmdp_get(pmd);\n 104:\t\n 105:\t#if defined(CONFIG_KASAN_GENERIC) || defined(CONFIG_KASAN_SW_TAGS)\n 106:\t\tif (pmd_page(val) == virt_to_page(lm_alias(kasan_early_shadow_pte)))\n 107:\t\t\treturn note_kasan_page_table(walk, addr);\n 108:\t#endif\n 109:\t\n 110:\t\tif (st-\u003eeffective_prot_pmd)\n 111:\t\t\tst-\u003eeffective_prot_pmd(st, val);\n 112:\t\tif (pmd_leaf(val)) {\n 113:\t\t\tst-\u003enote_page_pmd(st, addr, val);\n 114:\t\t\twalk-\u003eaction = ACTION_CONTINUE;\n 115:\t\t}\n 116:\t\n 117:\t\treturn 0;\n 118:\t}\n 119:\t\n 120:\tstatic int ptdump_pte_entry(pte_t *pte, unsigned long addr,\n 121:\t\t\t\t    unsigned long next, struct mm_walk *walk)\n 122:\t{\n 123:\t\tstruct ptdump_state *st = walk-\u003eprivate;\n 124:\t\tpte_t val = ptep_get(pte);\n 125:\t\n 126:\t\tif (st-\u003eeffective_prot_pte)\n 127:\t\t\tst-\u003eeffective_prot_pte(st, val);\n 128:\t\n 129:\t\tst-\u003enote_page_pte(st, addr, val);\n 130:\t\n 131:\t\treturn 0;\n 132:\t}\n 133:\t\n 134:\tstatic int ptdump_hole(unsigned long addr, unsigned long next,\n"
}

35/2 2026/11/02 22:06 llm patch-evaluator
0m Model:
gemini-3.8-flash

Args:
null

Results:
null

Tokens:
							input: 41958
							output: 33
							thoughts: 35
						

Thoughts:
Okay, let's dive into this. Here's my take on those `ptdump` functions:

**Deconstructing the Page Table Dump Functions**

Alright, so I'm thinking about those functions, `ptdump_pgd_entry` and `ptdump_p4d_entry`, lurking in `mm/ptdump.c`. It's pretty clear what they're *for*, but let's break down the implications and potential use cases here. I'm seeing this as a critical piece for any debugging or analysis related to memory management, specifically when dealing with page table structures. What are the key things I'd be looking for when I encounter these?

First, I expect that these functions are designed to extract and present the contents of specific entries within the Page Global Directory (PGD) or the Page 4-Level Directory (P4D). That's the basic function. Beyond that, the real utility is in *how* they present it. Are they giving me the raw bit-level representation, or are they providing a more human-readable interpretation? Are they decoding the flags (present, writeable, user access, etc.) in a clear and concise way? The level of detail and clarity will really impact their usefulness in a debugging scenario.

From there, I'd immediately be thinking about the context of their usage. Are they intended to be called interactively from a debugger like `gdb`? Or are they part of a larger kernel infrastructure for logging or diagnostic output? The answer here will shape my expectations around their output format and how easily they integrate with other debugging tools and strategies. For example, if they're meant to be interactive, I'd want them to provide a clean and informative display, perhaps with color-coding or symbolic representations of different flags. If they're meant for logging, then precision is key.

Finally, I'm already anticipating some of the nuances involved in the implementation. I'm betting there's some bit-field magic going on, and likely some architecture-specific considerations. I'll want to see how well they handle different architectures (x86, ARM, etc.), and if they account for any potential differences in page table formats across these architectures. Are there any conditional compilation directives that handle those differences? Understanding those architectural dependencies will be paramount for robust debugging. In essence, my interest goes beyond just the function itself: it is how I might use the functions within my work and understanding of memory.



36/2 2026/11/02 22:06 tool read-file
0m Args:
{
  "File": "mm/ptdump.c",
  "FirstLine": 40,
  "LineCount": 40
}

Results:
{
  "Contents": "  40:\t#endif\n  41:\t\n  42:\t\tif (st-\u003eeffective_prot_pgd)\n  43:\t\t\tst-\u003eeffective_prot_pgd(st, val);\n  44:\t\n  45:\t\tif (pgd_leaf(val)) {\n  46:\t\t\tst-\u003enote_page_pgd(st, addr, val);\n  47:\t\t\twalk-\u003eaction = ACTION_CONTINUE;\n  48:\t\t}\n  49:\t\n  50:\t\treturn 0;\n  51:\t}\n  52:\t\n  53:\tstatic int ptdump_p4d_entry(p4d_t *p4d, unsigned long addr,\n  54:\t\t\t\t    unsigned long next, struct mm_walk *walk)\n  55:\t{\n  56:\t\tstruct ptdump_state *st = walk-\u003eprivate;\n  57:\t\tp4d_t val = p4dp_get(p4d);\n  58:\t\n  59:\t#if CONFIG_PGTABLE_LEVELS \u003e 3 \u0026\u0026 \\\n  60:\t\t\t(defined(CONFIG_KASAN_GENERIC) || defined(CONFIG_KASAN_SW_TAGS))\n  61:\t\tif (p4d_page(val) == virt_to_page(lm_alias(kasan_early_shadow_pud)))\n  62:\t\t\treturn note_kasan_page_table(walk, addr);\n  63:\t#endif\n  64:\t\n  65:\t\tif (st-\u003eeffective_prot_p4d)\n  66:\t\t\tst-\u003eeffective_prot_p4d(st, val);\n  67:\t\n  68:\t\tif (p4d_leaf(val)) {\n  69:\t\t\tst-\u003enote_page_p4d(st, addr, val);\n  70:\t\t\twalk-\u003eaction = ACTION_CONTINUE;\n  71:\t\t}\n  72:\t\n  73:\t\treturn 0;\n  74:\t}\n  75:\t\n  76:\tstatic int ptdump_pud_entry(pud_t *pud, unsigned long addr,\n  77:\t\t\t\t    unsigned long next, struct mm_walk *walk)\n  78:\t{\n  79:\t\tstruct ptdump_state *st = walk-\u003eprivate;\n"
}

37/2 2026/11/02 22:06 llm patch-evaluator
0m Model:
gemini-3.8-flash

Args:
null

Results:
null

Tokens:
							input: 43135
							output: 30
							thoughts: 142
						

Thoughts:
**Examining the P4D Entry and Page Walk**

Okay, here's what's running through my head as I look at this code snippet. First, I see `p4d_t val = p4dp_get(p4d);` inside `ptdump_p4d_entry`. This is where we're pulling a value from the P4D, which is interesting. But then I have to pause and consider the implications of 4-level paging, specifically when the P4D is *folded* into the PGD, meaning we're effectively using `nop4d`. My brain immediately jumps to, "Wait, if we're not using a dedicated P4D, what's the value of `CONFIG_PGTABLE_LEVELS` in this specific context?" It's definitely something to verify, and it should be 4.

My next step is to understand how the page walk mechanism handles these "folded" levels. I need to refresh my memory on how this all works. I know that `walk_page_range` is the overarching function in `include/linux/pagewalk.h` or potentially `mm/pagewalk.c`. But I'm going to drill down directly to the function that specifically deals with the P4D level, so I'm heading for `walk_p4d_range` in `mm/pagewalk.c`. I need to see exactly how it processes a P4D entry when `nop4d` is in play and the P4D is conceptually collapsed into the PGD. That should clarify the behavior of that call.



38/2 2026/11/02 22:06 tool grepper
0m Args:
{
  "Expression": "walk_p4d_range",
  "PathPrefixes": [
    "mm/"
  ]
}

Results:
{
  "Output": "mm/pagewalk.c=184=static int walk_pud_range(p4d_t *p4d, unsigned long addr, unsigned long end,\n--\nmm/pagewalk.c-247-\nmm/pagewalk.c:248:static int walk_p4d_range(pgd_t *pgd, unsigned long addr, unsigned long end,\nmm/pagewalk.c-249-\t\t\t  struct mm_walk *walk)\n--\nmm/pagewalk.c=286=static int walk_pgd_range(unsigned long addr, unsigned long end,\n--\nmm/pagewalk.c-318-\t\tif (has_handler || has_install)\nmm/pagewalk.c:319:\t\t\terr = walk_p4d_range(pgd, addr, next, walk);\nmm/pagewalk.c-320-\t\tif (err)\n"
}

39/2 2026/11/02 22:06 llm patch-evaluator
0m Model:
gemini-3.8-flash

Args:
null

Results:
null

Tokens:
							input: 43878
							output: 34
							thoughts: 19
						

Thoughts:
Okay, let's dive into this. Here's my take on those thoughts, as if they're my own:

**Navigating the Memory Management Labyrinth**

Alright, I'm thinking about memory management, specifically within the kernel. I need to refresh my understanding of how page walks function. My mental map is guiding me towards `mm/pagewalk.c`, and specifically, around line 248. The context here is likely crucial to understanding some current issue I'm debugging or some architectural question I have. I need to get a clear picture of how those page table entries are being resolved and used. This probably involves understanding how the kernel translates virtual addresses to physical ones, and I'm betting that line 248 is a critical spot in that process. I need to see what's happening there with page tables. Time to get my hands dirty.



40/2 2026/11/02 22:06 tool read-file
0m Args:
{
  "File": "mm/pagewalk.c",
  "FirstLine": 240,
  "LineCount": 40
}

Results:
{
  "Contents": " 240:\t\n 241:\t\t\tif (walk-\u003eaction == ACTION_AGAIN)\n 242:\t\t\t\tgoto again;\n 243:\t\t} while (pud++, addr = next, addr != end);\n 244:\t\n 245:\t\treturn err;\n 246:\t}\n 247:\t\n 248:\tstatic int walk_p4d_range(pgd_t *pgd, unsigned long addr, unsigned long end,\n 249:\t\t\t\t  struct mm_walk *walk)\n 250:\t{\n 251:\t\tp4d_t *p4d;\n 252:\t\tunsigned long next;\n 253:\t\tconst struct mm_walk_ops *ops = walk-\u003eops;\n 254:\t\tbool has_handler = ops-\u003epud_entry || ops-\u003epmd_entry || ops-\u003epte_entry;\n 255:\t\tbool has_install = ops-\u003einstall_pte;\n 256:\t\tint err = 0;\n 257:\t\tint depth = real_depth(1);\n 258:\t\n 259:\t\tp4d = p4d_offset(pgd, addr);\n 260:\t\tdo {\n 261:\t\t\tnext = p4d_addr_end(addr, end);\n 262:\t\t\tif (p4d_none_or_clear_bad(p4d)) {\n 263:\t\t\t\tif (has_install)\n 264:\t\t\t\t\terr = __pud_alloc(walk-\u003emm, p4d, addr);\n 265:\t\t\t\telse if (ops-\u003epte_hole)\n 266:\t\t\t\t\terr = ops-\u003epte_hole(addr, next, depth, walk);\n 267:\t\t\t\tif (err)\n 268:\t\t\t\t\tbreak;\n 269:\t\t\t\tif (!has_install)\n 270:\t\t\t\t\tcontinue;\n 271:\t\t\t}\n 272:\t\t\tif (ops-\u003ep4d_entry) {\n 273:\t\t\t\terr = ops-\u003ep4d_entry(p4d, addr, next, walk);\n 274:\t\t\t\tif (err)\n 275:\t\t\t\t\tbreak;\n 276:\t\t\t}\n 277:\t\t\tif (has_handler || has_install)\n 278:\t\t\t\terr = walk_pud_range(p4d, addr, next, walk);\n 279:\t\t\tif (err)\n"
}

41/2 2026/11/02 22:06 llm patch-evaluator
0m Model:
gemini-3.8-flash

Args:
null

Results:
null

Tokens:
							input: 44739
							output: 33
							thoughts: 1935
						

Thoughts:
**Analysis of a Kernel Patch for Fuzzing Eligibility**

Okay, let's break this down. First, I see significant changes impacting core memory management, specifically within `walk_p4d_range`, which utilizes `include/asm-generic/pgtable-nop4d.h` for 4-level paging.  My patch has directly modified `p4d_offset` and related functions like `pgdp_get` and `set_pgd`. The `pgd_check_dummy` macro and `BUILD_BUG_ON` calls are being introduced, raising a flag. The changes to `arch/x86/mm/pat/set_memory.c` (particularly the inclusion of `direct_gbpages_enabled()`) and modifications in `arch/x86/mm/dump_pagetables.c`, are also significant.  The conditional logic in `collapse_pud_page` and `populate_pud` involving `direct_gbpages_enabled()` is interesting, and the changes to `effective_prot()` and `mm_first_pgtable_level()` in `dump_pagetables.c` warrant deeper consideration.  Crucially, I'm observing changes that directly impact the page table walking routines, `walk_pmd_range` and `walk_pud_range`, which is a key process.

Based on this, I'm confirming the modified code is *reachable*. Memory management is the core of the kernel; code related to the MMU is always active. The Multi-Gen LRU (MGLRU) mechanism runs continuously, and it's exposed through sysfs. Also x86 CPA, ptdump and page table folding generic helpers.  So, this is a definite "YES."

The patch demonstrably *touches* reachable executable code, including core memory management, x86 memory management code, and related helpers.  This includes not only the functions directly modified but also the surrounding code that interacts with them. This is also a "YES."

Given the criteria for "WorthFuzzing" in Section 3, I am seeing direct modifications within core subsystems (specifically memory management), refactorings/code cleanups involving data structures, and the addition of assertions (like `pgd_check_dummy`). So, "WorthFuzzing" is *absolutely* true here.

Now, I need to focus on symbol extraction to avoid dilution as per section 4.

I’ve identified `mm/vmscan.c` as a key area. Specifically, `walk_pmd_range` and `walk_pud_range` are relevant.  Crucially, I noticed that `walk_pud_range` *cannot* be completely inlined because its address is taken and passed to `walk_page_range` (verified by the usage as `.p4d_entry = walk_pud_range,` on line 3975).  This means I can target those directly.  `walk_pmd_range` is called by `walk_pud_range`. `walk_mm` is static, so I can ignore it for now. I need to make sure I don't select ubiquitous functions, so I will have to look to see what else calls `walk_pmd_range` and `walk_pte_range` in that file.

Ultimately, fuzzing these functions is important to ensure these critical changes in the page table walking and x86 memory management code are robust.



42/2 2026/11/02 22:07 tool grepper
0m Args:
{
  "Expression": "walk_pmd_range",
  "PathPrefixes": [
    "mm/vmscan.c"
  ]
}

Results:
{
  "Output": "mm/vmscan.c=2908=static bool __maybe_unused seq_is_valid(struct lruvec *lruvec)\n--\nmm/vmscan.c-2936- * leaf entries, the aging carries them over to the next generation in\nmm/vmscan.c:2937: * walk_pmd_range(); the eviction also report them when walking the rmap\nmm/vmscan.c-2938- * in lru_gen_look_around().\n--\nmm/vmscan.c=3681=static bool walk_pte_range(pmd_t *pmd, unsigned long start, unsigned long end,\n--\nmm/vmscan.c-3769-\nmm/vmscan.c:3770:static void walk_pmd_range_locked(pud_t *pud, unsigned long addr, struct vm_area_struct *vma,\nmm/vmscan.c-3771-\t\t\t\t  struct mm_walk *args, unsigned long *bitmap, unsigned long *first)\n--\nmm/vmscan.c-3855-\nmm/vmscan.c:3856:static void walk_pmd_range(pud_t *pud, unsigned long start, unsigned long end,\nmm/vmscan.c-3857-\t\t\t   struct mm_walk *args)\n--\nmm/vmscan.c-3896-\t\t\tif (pfn != -1)\nmm/vmscan.c:3897:\t\t\t\twalk_pmd_range_locked(pud, addr, vma, args, bitmap, \u0026first);\nmm/vmscan.c-3898-\t\t\tcontinue;\n--\nmm/vmscan.c-3905-\nmm/vmscan.c:3906:\t\t\twalk_pmd_range_locked(pud, addr, vma, args, bitmap, \u0026first);\nmm/vmscan.c-3907-\t\t}\n--\nmm/vmscan.c-3922-\nmm/vmscan.c:3923:\twalk_pmd_range_locked(pud, -1, vma, args, bitmap, \u0026first);\nmm/vmscan.c-3924-\n--\nmm/vmscan.c=3929=static int walk_pud_range(p4d_t *p4d, unsigned long start, unsigned long end,\n--\nmm/vmscan.c-3949-\nmm/vmscan.c:3950:\t\twalk_pmd_range(pud + i, addr, next, args);\nmm/vmscan.c-3951-\n"
}

43/2 2026/11/02 22:07 llm patch-evaluator
0m Model:
gemini-3.8-flash

Args:
null

Results:
null

Tokens:
							input: 48103
							output: 43
							thoughts: 144
						

Thoughts:
Okay, let's dive into this.

**Investigating Recent Changes in `dump_pagetables.c`**

Alright, I need to get a handle on what's been touched in `arch/x86/mm/dump_pagetables.c`. I need to figure out the scope of any recent changes. My initial thought is to examine the function declarations for the most critical impacts.

So, let's see... the diff shows modifications to `effective_prot`, which is a static function within the file. Okay, good to know; that likely indicates internal adjustments, and not a change to the exported interface. Then there are some changes to `ptdump_walk_pgd_level_core`, and both `ptdump_walk_pgd_level` and `ptdump_walk_pgd_level_debugfs`. *ptdump_walk_pgd_level_core* is exported, so I should analyze that closely. It looks like the core logic might be at the center of the change, so let's start there. I'll need to check the context of changes around `ptdump_walk_pgd_level_core`.



44/2 2026/11/02 22:07 tool read-file
0m Args:
{
  "File": "arch/x86/mm/dump_pagetables.c",
  "FirstLine": 420,
  "LineCount": 40
}

Results:
{
  "Contents": " 420:\t\n 421:\t\tnote_page(pt_st, 0, -1, pte_val(pte_zero));\n 422:\t}\n 423:\t\n 424:\tbool ptdump_walk_pgd_level_core(struct seq_file *m,\n 425:\t\t\t\t\tstruct mm_struct *mm, pgd_t *pgd,\n 426:\t\t\t\t\tbool checkwx, bool dmesg)\n 427:\t{\n 428:\t\tconst struct ptdump_range ptdump_ranges[] = {\n 429:\t#ifdef CONFIG_X86_64\n 430:\t\t{0, PTRS_PER_PGD * PGD_LEVEL_MULT / 2},\n 431:\t\t{GUARD_HOLE_END_ADDR, ~0UL},\n 432:\t#else\n 433:\t\t{0, ~0UL},\n 434:\t#endif\n 435:\t\t{0, 0}\n 436:\t};\n 437:\t\n 438:\t\tstruct pg_state st = {\n 439:\t\t\t.ptdump = {\n 440:\t\t\t\t.note_page_pte = note_page_pte,\n 441:\t\t\t\t.note_page_pmd = note_page_pmd,\n 442:\t\t\t\t.note_page_pud = note_page_pud,\n 443:\t\t\t\t.note_page_p4d = note_page_p4d,\n 444:\t\t\t\t.note_page_pgd = note_page_pgd,\n 445:\t\t\t\t.note_page_flush = note_page_flush,\n 446:\t\t\t\t.effective_prot_pte = effective_prot_pte,\n 447:\t\t\t\t.effective_prot_pmd = effective_prot_pmd,\n 448:\t\t\t\t.effective_prot_pud = effective_prot_pud,\n 449:\t\t\t\t.effective_prot_p4d = effective_prot_p4d,\n 450:\t\t\t\t.effective_prot_pgd = effective_prot_pgd,\n 451:\t\t\t\t.range\t\t= ptdump_ranges\n 452:\t\t\t},\n 453:\t\t\t.level = -1,\n 454:\t\t\t.to_dmesg\t= dmesg,\n 455:\t\t\t.check_wx\t= checkwx,\n 456:\t\t\t.seq\t\t= m,\n 457:\t\t\t.mm\t\t= mm,\n 458:\t\t};\n 459:\t\n"
}

45/2 2026/11/02 22:07 llm patch-evaluator
0m Model:
gemini-3.8-flash

Args:
null

Results:
null

Tokens:
							input: 49207
							output: 35
							thoughts: 23
						

Thoughts:
Okay, here's my interpretation of that thought process, tailored for an expert audience, and written in the first person:

**Digging into the Debugfs Data Flow**

Alright, so `ptdump_walk_pgd_level_debugfs`… where does *that* fit into the grand scheme of things? I need to understand its call graph. Knowing who calls *this* function is key to tracing the data flow through debugfs and potentially understanding its role in dumping page table data. Let's get to the bottom of this. It's time to delve into the code and see who's invoking it. Time to start the investigation...



46/2 2026/11/02 22:08 tool grepper
0m Args:
{
  "Expression": "ptdump_walk_pgd_level",
  "PathPrefixes": [
    "arch/x86"
  ]
}

Results:
{
  "Output": "arch/x86/include/asm/pgtable.h=30=struct seq_file;\narch/x86/include/asm/pgtable.h:31:void ptdump_walk_pgd_level(struct seq_file *m, struct mm_struct *mm);\narch/x86/include/asm/pgtable.h:32:void ptdump_walk_pgd_level_debugfs(struct seq_file *m, struct mm_struct *mm,\narch/x86/include/asm/pgtable.h-33-\t\t\t\t   bool user);\narch/x86/include/asm/pgtable.h:34:bool ptdump_walk_pgd_level_checkwx(void);\narch/x86/include/asm/pgtable.h:35:#define ptdump_check_wx ptdump_walk_pgd_level_checkwx\narch/x86/include/asm/pgtable.h-36-void ptdump_walk_user_pgd_level_checkwx(void);\n--\narch/x86/mm/debug_pagetables.c=8=static int ptdump_show(struct seq_file *m, void *v)\narch/x86/mm/debug_pagetables.c-9-{\narch/x86/mm/debug_pagetables.c:10:\tptdump_walk_pgd_level_debugfs(m, \u0026init_mm, false);\narch/x86/mm/debug_pagetables.c-11-\treturn 0;\n--\narch/x86/mm/debug_pagetables.c=16=static int ptdump_curknl_show(struct seq_file *m, void *v)\n--\narch/x86/mm/debug_pagetables.c-18-\tif (current-\u003emm-\u003epgd)\narch/x86/mm/debug_pagetables.c:19:\t\tptdump_walk_pgd_level_debugfs(m, current-\u003emm, false);\narch/x86/mm/debug_pagetables.c-20-\treturn 0;\n--\narch/x86/mm/debug_pagetables.c=26=static int ptdump_curusr_show(struct seq_file *m, void *v)\n--\narch/x86/mm/debug_pagetables.c-28-\tif (current-\u003emm-\u003epgd)\narch/x86/mm/debug_pagetables.c:29:\t\tptdump_walk_pgd_level_debugfs(m, current-\u003emm, true);\narch/x86/mm/debug_pagetables.c-30-\treturn 0;\n--\narch/x86/mm/debug_pagetables.c=37=static int ptdump_efi_show(struct seq_file *m, void *v)\n--\narch/x86/mm/debug_pagetables.c-39-\tif (efi_mm.pgd)\narch/x86/mm/debug_pagetables.c:40:\t\tptdump_walk_pgd_level_debugfs(m, \u0026efi_mm, false);\narch/x86/mm/debug_pagetables.c-41-\treturn 0;\n--\narch/x86/mm/dump_pagetables.c=417=static void note_page_flush(struct ptdump_state *pt_st)\n--\narch/x86/mm/dump_pagetables.c-423-\narch/x86/mm/dump_pagetables.c:424:bool ptdump_walk_pgd_level_core(struct seq_file *m,\narch/x86/mm/dump_pagetables.c-425-\t\t\t\tstruct mm_struct *mm, pgd_t *pgd,\n--\narch/x86/mm/dump_pagetables.c-475-\narch/x86/mm/dump_pagetables.c:476:void ptdump_walk_pgd_level(struct seq_file *m, struct mm_struct *mm)\narch/x86/mm/dump_pagetables.c-477-{\narch/x86/mm/dump_pagetables.c:478:\tptdump_walk_pgd_level_core(m, mm, mm-\u003epgd, false, true);\narch/x86/mm/dump_pagetables.c-479-}\narch/x86/mm/dump_pagetables.c-480-\narch/x86/mm/dump_pagetables.c:481:void ptdump_walk_pgd_level_debugfs(struct seq_file *m, struct mm_struct *mm,\narch/x86/mm/dump_pagetables.c-482-\t\t\t\t   bool user)\n--\narch/x86/mm/dump_pagetables.c-488-#endif\narch/x86/mm/dump_pagetables.c:489:\tptdump_walk_pgd_level_core(m, mm, pgd, false, false);\narch/x86/mm/dump_pagetables.c-490-}\n--\narch/x86/mm/dump_pagetables.c=492=void ptdump_walk_user_pgd_level_checkwx(void)\n--\narch/x86/mm/dump_pagetables.c-502-\tpgd = kernel_to_user_pgdp(pgd);\narch/x86/mm/dump_pagetables.c:503:\tptdump_walk_pgd_level_core(NULL, \u0026init_mm, pgd, true, false);\narch/x86/mm/dump_pagetables.c-504-#endif\n--\narch/x86/mm/dump_pagetables.c-506-\narch/x86/mm/dump_pagetables.c:507:bool ptdump_walk_pgd_level_checkwx(void)\narch/x86/mm/dump_pagetables.c-508-{\n--\narch/x86/mm/dump_pagetables.c-511-\narch/x86/mm/dump_pagetables.c:512:\treturn ptdump_walk_pgd_level_core(NULL, \u0026init_mm, INIT_PGD, true, false);\narch/x86/mm/dump_pagetables.c-513-}\n--\narch/x86/platform/efi/efi_32.c=75=void __init efi_dump_pagetable(void)\n--\narch/x86/platform/efi/efi_32.c-77-#ifdef CONFIG_EFI_PGT_DUMP\narch/x86/platform/efi/efi_32.c:78:\tptdump_walk_pgd_level(NULL, \u0026init_mm);\narch/x86/platform/efi/efi_32.c-79-#endif\n--\narch/x86/platform/efi/efi_64.c=423=void __init efi_dump_pagetable(void)\n--\narch/x86/platform/efi/efi_64.c-425-#ifdef CONFIG_EFI_PGT_DUMP\narch/x86/platform/efi/efi_64.c:426:\tptdump_walk_pgd_level(NULL, \u0026efi_mm);\narch/x86/platform/efi/efi_64.c-427-#endif\n"
}

47/2 2026/11/02 22:08 llm patch-evaluator
0m Model:
gemini-3.8-flash

Args:
null

Results:
null

Tokens:
							input: 51310
							output: 42
							thoughts: 248
						

Thoughts:
**My Analysis of a Recent Kernel Change**

Okay, so I'm looking at this recent patch, and it's immediately clear that it's related to how the kernel handles page table dumps and permissions checks, specifically regarding user memory. From what I can see, `ptdump_curusr_show` reads `current->mm`, which is the current process's memory map. When syzkaller triggers `/sys/kernel/debug/page_tables/current_user`, we end up in `ptdump_walk_pgd_level_debugfs`, which passes `current->mm` along. This `current->mm` then feeds into `ptdump_walk_pgd_level_core`, and the `st.mm = mm;` assignment is crucial.

The important function here is `effective_prot()`. The key change lies in this: before this patch, it was simply doing a `if (level > 0)` check. Now, it's comparing against `mm_first_pgtable_level(st->mm)`. This `.mm` member of `st` wasn't even initialized previously! Therefore, when `ptdump_walk_user_pgd_level_checkwx()` or debugfs routines were called on user memory, the `effective_prot` function would calculate permissions based on this `mm_first_pgtable_level(st->mm)`.

So, the fundamental shift is that we're now correctly determining protection levels based on the specific memory map of the user process, rather than assuming a default. This is a vital correction. It makes me wonder about the implications for memory protection and security.

Now, I want to pivot to related code. Specifically, I'm thinking about the implications for `arch/x86/mm/pat/set_memory.c`. I need to review the callers of `collapse_pud_page` and `populate_pud` to get a full view of the changes' potential impact on the overall memory management. I suspect it touches on the PAT (Page Attribute Table) and how it's handled in relation to user space memory and page table manipulations.



48/2 2026/11/02 22:08 tool grepper
0m Args:
{
  "Expression": "collapse_pud_page",
  "PathPrefixes": [
    "arch/x86/mm/pat/set_memory.c"
  ]
}

Results:
{
  "Output": "arch/x86/mm/pat/set_memory.c=1281=static int collapse_pmd_page(pmd_t *pmd, unsigned long addr,\n--\narch/x86/mm/pat/set_memory.c-1350-\narch/x86/mm/pat/set_memory.c:1351:static int collapse_pud_page(pud_t *pud, unsigned long addr,\narch/x86/mm/pat/set_memory.c-1352-\t\t\t     struct list_head *pgtables)\n--\narch/x86/mm/pat/set_memory.c=1406=static int collapse_large_pages(unsigned long addr, struct list_head *pgtables)\n--\narch/x86/mm/pat/set_memory.c-1431-\tif (collapsed)\narch/x86/mm/pat/set_memory.c:1432:\t\tcollapsed += collapse_pud_page(pud, addr, pgtables);\narch/x86/mm/pat/set_memory.c-1433-\n"
}

49/2 2026/11/02 22:08 llm patch-evaluator
0m Model:
gemini-3.8-flash

Args:
null

Results:
null

Tokens:
							input: 52346
							output: 40
							thoughts: 14
						

Thoughts:
Okay, here's my interpretation of that thought process, tailored for an expert audience, and written in the first person:

**Digging Deeper: Expanding the Investigation**

Alright, I need to get to the bottom of this. We've been focusing on `populate_pud`, but I can't just assume it's the only point of interest. It's time to take a broader look. My next step is clear: I need to methodically check all of the callers of the `populate_pud` function. I have to trace back how it's being used within the system to understand the full implications of its behavior, and to make sure I don't miss any critical dependencies or potential side effects. This broader view is critical for understanding the scope of the potential problem.



50/2 2026/11/02 22:08 tool grepper
0m Args:
{
  "Expression": "populate_pud",
  "PathPrefixes": [
    "arch/x86/mm/pat/set_memory.c"
  ]
}

Results:
{
  "Output": "arch/x86/mm/pat/set_memory.c=1617=static long populate_pmd(struct cpa_data *cpa,\n--\narch/x86/mm/pat/set_memory.c-1690-\narch/x86/mm/pat/set_memory.c:1691:static int populate_pud(struct cpa_data *cpa, unsigned long start, p4d_t *p4d,\narch/x86/mm/pat/set_memory.c-1692-\t\t\tpgprot_t pgprot)\n--\narch/x86/mm/pat/set_memory.c=1773=static int populate_pgd(struct cpa_data *cpa, unsigned long addr)\n--\narch/x86/mm/pat/set_memory.c-1813-\narch/x86/mm/pat/set_memory.c:1814:\tret = populate_pud(cpa, addr, p4d, pgprot);\narch/x86/mm/pat/set_memory.c-1815-\tif (ret \u003c 0) {\n"
}

51/2 2026/11/02 22:08 llm patch-evaluator
0m Model:
gemini-3.8-flash

Args:
null

Results:
null

Tokens:
							input: 52865
							output: 36
							thoughts: 166
						

Thoughts:
Okay, let's dive into this. Here's how I'm processing this information:

**Tracing Memory Attribute Changes**

Right, so I'm looking at how memory attributes are being altered, specifically focusing on the core mechanisms. `set_memory.c` is the key file here. I see that the `__change_page_attr_set_clr` function is the workhorse, the one doing the real work for changing permissions like read-only (`set_memory_ro`), read-write (`set_memory_rw`), execute (`set_memory_x`), and no-execute (`set_memory_nx`). This is triggered in multiple contexts, including BPF JIT compilation or allocation, module handling, or even DMA buffers. Understanding this core function is vital to grasping the overall flow.

Now, I'm thinking about the low-level page table walks. I want to see how these attribute changes propagate. I'm going to check out `walk_pud_range` and `walk_pmd_range` in `mm/vmscan.c`. These must be the functions that actually traverse the page table hierarchy, right?

But, how do we reach this point in the first place? I need to figure out how `walk_mm` is being invoked. Let's trace back from the point where we do the walks. It looks like `try_to_inc_max_seq(lruvec, ...)` eventually calls `walk_mm(mm, walk)`. This is interesting; `try_to_inc_max_seq` is the entry point, but I'm trying to see what triggers it. What *calls* `try_to_inc_max_seq`? I need to understand that. I need to understand the initial trigger.



52/2 2026/11/02 22:09 tool grepper
0m Args:
{
  "Expression": "try_to_inc_max_seq",
  "PathPrefixes": [
    "mm/vmscan.c"
  ]
}

Results:
{
  "Output": "mm/vmscan.c=4186=static bool inc_max_seq(struct lruvec *lruvec, unsigned long seq, int swappiness)\n--\nmm/vmscan.c-4250-\nmm/vmscan.c:4251:static bool try_to_inc_max_seq(struct lruvec *lruvec, unsigned long seq,\nmm/vmscan.c-4252-\t\t\t       int swappiness, bool force_scan)\n--\nmm/vmscan.c=4667=bool recheck_lru_gen_max_memcg(struct mem_cgroup *memcg, int nid)\n--\nmm/vmscan.c-4679-\nmm/vmscan.c:4680:static void try_to_inc_max_seq_nowalk(struct mem_cgroup *memcg,\nmm/vmscan.c-4681-\t\t\t\t      struct lruvec *lruvec)\n--\nmm/vmscan.c=4714=void max_lru_gen_memcg(struct mem_cgroup *memcg, int nid)\n--\nmm/vmscan.c-4720-\t\twhile (get_nr_gens(lruvec, type) \u003c MAX_NR_GENS) {\nmm/vmscan.c:4721:\t\t\ttry_to_inc_max_seq_nowalk(memcg, lruvec);\nmm/vmscan.c-4722-\t\t\tcond_resched();\n--\nmm/vmscan.c=5212=static bool should_abort_scan(struct lruvec *lruvec, struct scan_control *sc)\n--\nmm/vmscan.c-5243- * For future optimizations:\nmm/vmscan.c:5244: * 1. Defer try_to_inc_max_seq() to workqueues to reduce latency for memcg\nmm/vmscan.c-5245- *    reclaim.\n--\nmm/vmscan.c=5247=static bool try_to_shrink_lruvec(struct lruvec *lruvec, struct scan_control *sc)\n--\nmm/vmscan.c-5264-\t\tif (should_run_aging(lruvec, max_seq, sc, swappiness)) {\nmm/vmscan.c:5265:\t\t\tif (try_to_inc_max_seq(lruvec, max_seq, swappiness, false))\nmm/vmscan.c-5266-\t\t\t\tneed_rotate = true;\n--\nmm/vmscan.c=5883=static int run_aging(struct lruvec *lruvec, unsigned long seq,\n--\nmm/vmscan.c-5893-\nmm/vmscan.c:5894:\treturn try_to_inc_max_seq(lruvec, max_seq, swappiness, force_scan) ? 0 : -EEXIST;\nmm/vmscan.c-5895-}\n"
}

53/2 2026/11/02 22:09 llm patch-evaluator
0m Model:
gemini-3.8-flash

Args:
null

Results:
null

Tokens:
							input: 54193
							output: 30
							thoughts: 36
						

Thoughts:
Okay, here's my take on those thoughts, tailored for an expert audience, and written as if they were my own mental process:

**Diving into the `run_aging` Rabbit Hole**

Alright, *that* `run_aging` call on line 5883... let's see, what's triggering that particular function call within `mm/vmscan.c`? Gotta understand the context of the aging process to fully grasp its impact. I'm going to quickly trace the callers of `run_aging`. Understanding where and why it's invoked is key to getting the complete picture. The aging mechanisms are critical, I need to check out the context of `vmscan.c` and how it is interacting with the rest of the memory subsystem. Time to dig!



54/2 2026/11/02 22:09 tool grepper
0m Args:
{
  "Expression": "run_aging",
  "PathPrefixes": [
    "mm/vmscan.c"
  ]
}

Results:
{
  "Output": "mm/vmscan.c=5059=static int evict_folios(unsigned long nr_to_scan, struct lruvec *lruvec,\n--\nmm/vmscan.c-5163-\nmm/vmscan.c:5164:static bool should_run_aging(struct lruvec *lruvec, unsigned long max_seq,\nmm/vmscan.c-5165-\t\t\t     struct scan_control *sc, int swappiness)\n--\nmm/vmscan.c=5247=static bool try_to_shrink_lruvec(struct lruvec *lruvec, struct scan_control *sc)\n--\nmm/vmscan.c-5263-\nmm/vmscan.c:5264:\t\tif (should_run_aging(lruvec, max_seq, sc, swappiness)) {\nmm/vmscan.c-5265-\t\t\tif (try_to_inc_max_seq(lruvec, max_seq, swappiness, false))\n--\nmm/vmscan.c=5876=static const struct seq_operations lru_gen_seq_ops = {\n--\nmm/vmscan.c-5882-\nmm/vmscan.c:5883:static int run_aging(struct lruvec *lruvec, unsigned long seq,\nmm/vmscan.c-5884-\t\t     int swappiness, bool force_scan)\n--\nmm/vmscan.c=5927=static int run_cmd(char cmd, u64 memcg_id, int nid, unsigned long seq,\n--\nmm/vmscan.c-5955-\tcase '+':\nmm/vmscan.c:5956:\t\terr = run_aging(lruvec, seq, swappiness, opt);\nmm/vmscan.c-5957-\t\tbreak;\n"
}

55/2 2026/11/02 22:09 llm patch-evaluator
0m Model:
gemini-3.8-flash

Args:
null

Results:
null

Tokens:
							input: 54897
							output: 22
							thoughts: 1408
						

Thoughts:
**Analyzing the LRU Gen Patch and Its Implications**

Okay, so I'm digging into this patch. My initial thought is that writing to `/sys/kernel/mm/lru_gen/` triggers a cascade of function calls: `run_cmd` -> `run_aging` -> `try_to_inc_max_seq` -> `walk_mm` -> `walk_pud_range` -> `walk_pmd_range`. I also know that `try_to_shrink_lruvec` is active during direct and kswapd reclaim. The focus here is the modifications to `walk_pud_range` and `walk_pmd_range`.

Looking at the code, it's immediately clear that there's a significant change. In the original version, `walk_pte_range` was called with a stack variable address. Now the functions are called using an actual pointer into the page table. This is a critical shift. Specifically:
*   In `walk_pmd_range`:  `walk_pte_range(pmd + i, addr, next, args);`
*   In `walk_pud_range`: `walk_pmd_range(pud + i, addr, next, args);`

The previous call was `walk_pmd_range(&val, ...)`. The runtime assertion in `walk_pud_range` - `WARN_ON_ONCE(pud_leaf(val))` - is a canary. Then there's the `VM_WARN_ON_ONCE(pud_leaf(*pud))` in `walk_pmd_range`. When `walk_pmd_range(&val, ...)` was called previously, `*pud` was `val`. Now `pud` is `pud + i`, an actual pointer into the page table!

This matters because now the `pmd` being dereferenced in `walk_pte_range` is a proper page table entry. When previously, dereferencing `pmd` was essentially dereferencing the stack variable.

My gut feeling, considering the level of expertise in my field, is to look for the key symbols or functions that this patch affects. I need to identify the "FocusSymbols" for this change.

Initially, I looked at functions in `mm/vmscan.c` like `walk_pud_range`, `walk_pmd_range`, and `walk_mm`. However, I realized the Multi-gen LRU page table walk is specific to MGLRU (`CONFIG_LRU_GEN_WALKS_MMU` / `CONFIG_LRU_GEN`), which narrows the scope.

I then started looking in `arch/x86/mm/dump_pagetables.c`, functions like `ptdump_walk_pgd_level_core`, and the `effective_prot` functions. However, `effective_prot` and its variants are static functions, which likely would not be symbols I need to target. However, `ptdump_walk_pgd_level_core` is a non-static symbol.

Then, I looked at `arch/x86/mm/pat/set_memory.c`, and found functions like `collapse_pud_page` and `populate_pud` that appear relevant, however these are all static.

I am not sure if it helps, but I'll check `include/linux/pgtable.h` for other potential issues. `mm_first_pgtable_level` is there, but static inline.

Ultimately, I need to understand what this means for the integrity of the page table walking, and verify that the intended changes are being reflected correctly.



56/2 2026/11/02 22:09 tool grepper
0m Args:
{
  "Expression": "mm_first_pgtable_level"
}

Results:
{
  "Output": "arch/x86/mm/dump_pagetables.c=252=static void effective_prot(struct ptdump_state *pt_st, enum pgtable_level level, u64 val)\n--\narch/x86/mm/dump_pagetables.c-257-\narch/x86/mm/dump_pagetables.c:258:\tif (level \u003e mm_first_pgtable_level(st-\u003emm)) {\narch/x86/mm/dump_pagetables.c-259-\t\tpgprotval_t higher_prot = st-\u003eprot_levels[level - 1];\n--\ninclude/linux/pgtable.h=2314=static inline const char *pgtable_level_to_str(enum pgtable_level level)\n--\ninclude/linux/pgtable.h-2333-/**\ninclude/linux/pgtable.h:2334: * mm_first_pgtable_level - return the first non-folded page-table level\ninclude/linux/pgtable.h-2335- * @mm: The mm structure.\n--\ninclude/linux/pgtable.h-2344- */\ninclude/linux/pgtable.h:2345:static inline enum pgtable_level mm_first_pgtable_level(struct mm_struct *mm)\ninclude/linux/pgtable.h-2346-{\n"
}

57/2 2026/11/02 22:09 llm patch-evaluator
0m Model:
gemini-3.8-flash

Args:
null

Results:
null

Tokens:
							input: 57476
							output: 33
							thoughts: 183
						

Thoughts:
**Analyzing Page Table Dumping and MGLRU MM Walk**

Okay, so I'm digging into how the page tables are dumped, and I've noticed something specific about `mm_first_pgtable_level`. This function is exclusively called by `effective_prot`, and only in that `arch/x86/mm/dump_pagetables.c` file, right? `effective_prot` itself is a key function, as it's the gateway for retrieving protection information at each level of the page table: PGD, P4D, PUD, PMD, and PTE. This is important context for what I am trying to achieve.

Now, I need to understand what's going on with the MGLRU MM walk, so I'll check some Kconfig options. Specifically, the file `mm/vmscan.c`. I'm trying to figure out which configurations enable that particular walk (`walk_pud_range`, `walk_pmd_range`, `walk_pte_range`). I'll need to scrutinize the `#ifdef` directives or Kconfig entries surrounding `walk_mm` or those specific range walkers in `mm/vmscan.c`. Knowing this context will help me with my understanding of how memory management is implemented.



58/2 2026/11/02 22:10 tool grepper
0m Args:
{
  "Expression": "walk_pud_range",
  "PathPrefixes": [
    "mm/vmscan.c"
  ]
}

Results:
{
  "Output": "mm/vmscan.c=3856=static void walk_pmd_range(pud_t *pud, unsigned long start, unsigned long end,\n--\nmm/vmscan.c-3928-\nmm/vmscan.c:3929:static int walk_pud_range(p4d_t *p4d, unsigned long start, unsigned long end,\nmm/vmscan.c-3930-\t\t\t  struct mm_walk *args)\n--\nmm/vmscan.c=3971=static void walk_mm(struct mm_struct *mm, struct lru_gen_mm_walk *walk)\n--\nmm/vmscan.c-3974-\t\t.test_walk = should_skip_vma,\nmm/vmscan.c:3975:\t\t.p4d_entry = walk_pud_range,\nmm/vmscan.c-3976-\t\t.walk_lock = PGWALK_RDLOCK,\n"
}

59/2 2026/11/02 22:10 llm patch-evaluator
0m Model:
gemini-3.8-flash

Args:
null

Results:
null

Tokens:
							input: 58212
							output: 33
							thoughts: 26
						

Thoughts:
Okay, here's my interpretation of that request, formatted for a domain expert like yourself:

**Analyzing `mm/vmscan.c` (Lines 3600-4000)**

Alright, so the directive is to zoom in on lines 3600 through 4000 of `mm/vmscan.c`. Immediately, my brain starts to churn, attempting to recall the surrounding code and context. That section... that's likely within the core of the kernel's virtual memory management, specifically dealing with the `vmscan` thread. It probably involves the process of how the kernel identifies and reclaims pages. Given the line numbers, I'd wager we're likely in the thick of a significant loop or decision point within the `kswapd` process. 

I'm thinking about the possible functions/macros that may be called within this section, such as `shrink_slab`, `shrink_page_list`, or even something related to managing the LRU lists or evaluating page reclaim targets. It's highly probable we're dealing with either the evaluation of which pages to reclaim or the actual reclaiming process itself. I might also be thinking about the specific zones and the memory pressure at the time of the call, because this code is highly sensitive to the overall memory conditions.

I'm expecting to see calls to functions managing the page cache, swap cache, or even direct reclaim operations on anonymous pages. It's also likely there will be interaction with the page flags (dirty, accessed, etc.). More generally, I know this is a critical section for performance, as poor reclaim decisions can lead to disastrous performance implications, hence it's quite likely there's a lot of complex logic and optimization within that section. This is where the magic happens, or the system grinds to a halt!



60/2 2026/11/02 22:10 tool grepper
0m Args:
{
  "Expression": "CONFIG_LRU_GEN",
  "PathPrefixes": [
    "mm/vmscan.c"
  ]
}

Results:
{
  "Output": "mm/vmscan.c=927=enum folio_references {\n--\nmm/vmscan.c-932-\nmm/vmscan.c:933:#ifdef CONFIG_LRU_GEN\nmm/vmscan.c-934-/*\n--\nmm/vmscan.c=965=static bool lru_gen_set_refs(struct folio *folio, const vma_flags_t *vma_flags)\n--\nmm/vmscan.c-968-}\nmm/vmscan.c:969:#endif /* CONFIG_LRU_GEN */\nmm/vmscan.c-970-\n--\nmm/vmscan.c=2800=static bool can_age_anon_pages(struct lruvec *lruvec,\n--\nmm/vmscan.c-2811-\nmm/vmscan.c:2812:#ifdef CONFIG_LRU_GEN\nmm/vmscan.c-2813-\nmm/vmscan.c=2814=DEFINE_STATIC_KEY_FALSE(lru_switch);\nmm/vmscan.c:2815:#ifdef CONFIG_LRU_GEN_ENABLED\nmm/vmscan.c-2816-DEFINE_STATIC_KEY_ARRAY_TRUE(lru_gen_caps, NR_LRU_GEN_CAPS);\n--\nmm/vmscan.c=3000=static void reset_bloom_filter(struct lru_gen_mm_state *mm_state, unsigned long seq)\n--\nmm/vmscan.c-3019-\nmm/vmscan.c:3020:#ifdef CONFIG_LRU_GEN_WALKS_MMU\nmm/vmscan.c-3021-\n--\nmm/vmscan.c=3129=void lru_gen_migrate_mm(struct mm_struct *mm)\n--\nmm/vmscan.c-3157-\nmm/vmscan.c:3158:#else /* !CONFIG_LRU_GEN_WALKS_MMU */\nmm/vmscan.c-3159-\n--\nmm/vmscan.c=6166=late_initcall(init_lru_gen);\nmm/vmscan.c-6167-\nmm/vmscan.c:6168:#else /* !CONFIG_LRU_GEN */\nmm/vmscan.c-6169-\n--\nmm/vmscan.c=6180=static void lru_gen_shrink_node(struct pglist_data *pgdat, struct scan_control *sc)\n--\nmm/vmscan.c-6184-\nmm/vmscan.c:6185:#endif /* CONFIG_LRU_GEN */\nmm/vmscan.c-6186-\n"
}

61/2 2026/11/02 22:10 llm patch-evaluator
0m Model:
gemini-3.8-flash

Args:
null

Results:
null

Tokens:
							input: 59341
							output: 28
							thoughts: 56
						

Thoughts:
Okay, let's see. Here's how I'd approach this...

**Initial Observations on LRU_GEN Configuration**

Right, *there* it is! `CONFIG_LRU_GEN` and `CONFIG_LRU_GEN_WALKS_MMU` – interesting! My immediate inclination is to dive into the Kconfig files. Time to check how `LRU_GEN` and `LRU_GEN_WALKS_MMU` are defined, what dependencies they have, and what implications their settings carry. Gotta see what's being offered, what the defaults are, and how they play together in the broader memory management landscape. Let's see what the configuration system is telling us.



62/2 2026/11/02 22:10 tool grepper
0m Args:
{
  "Expression": "config LRU_GEN",
  "PathPrefixes": [
    "mm/"
  ]
}

Results:
{
  "Output": "mm/Kconfig=1384=config PTE_MARKER_UFFD_WP\n--\nmm/Kconfig-1394-# multi-gen LRU {\nmm/Kconfig:1395:config LRU_GEN\nmm/Kconfig-1396-\tbool \"Multi-Gen LRU\"\n--\nmm/Kconfig-1403-\nmm/Kconfig:1404:config LRU_GEN_ENABLED\nmm/Kconfig-1405-\tbool \"Enable by default\"\n--\nmm/Kconfig-1409-\nmm/Kconfig:1410:config LRU_GEN_STATS\nmm/Kconfig-1411-\tbool \"Full stats for debugging\"\n--\nmm/Kconfig-1418-\nmm/Kconfig:1419:config LRU_GEN_WALKS_MMU\nmm/Kconfig-1420-\tdef_bool y\n"
}

63/2 2026/11/02 22:10 llm patch-evaluator
0m Model:
gemini-3.8-flash

Args:
null

Results:
null

Tokens:
							input: 59814
							output: 33
							thoughts: 23
						

Thoughts:
Okay, let's dive into this. Here's my take on those lines in `mm/Kconfig`, specifically lines 1395 to 1430.

**Diving Deep into `mm/Kconfig` Section**

Alright, my immediate thought is to get my bearings. Lines 1395 through 1430 in `mm/Kconfig` – right, I know the territory. I'm assuming we're dealing with memory management configuration options here, probably related to some specific feature or optimization. The question is, which one? Given the range, it's likely a relatively self-contained block of related configurations.

I'll be looking for keywords, dependencies, and any hints about the underlying mechanism. Is this a new feature being added, a legacy option being refined, or a specific platform adaptation? Are there any obvious `depends on` statements or `select` choices that give context to the broader feature set? I'll probably scan for `bool`, `tristate`, `int`, or `string` options to understand the nature of the configuration variables. The presence of help text is also crucial. It'll give me a more in-depth understanding.

Knowing the typical structure of these files, I anticipate encountering a mixture of feature enables, parameter tunings, and possibly some platform-specific adaptations. I'll mentally map out the logic: "If this, then enable that; if not, then disable the other. And oh, I see a tristate there, allowing for a module load – that's potentially interesting from a debugging and flexibility perspective."

My initial approach will be to extract the key variables and their definitions, noting down the relationships between them. Then, I can start to form a higher-level understanding of what this configuration block is truly about. After that, I will check the `Kconfig` files that are referenced in this section. Finally, the commit logs and relevant kernel documentation for added context.



64/2 2026/11/02 22:10 tool read-file
0m Args:
{
  "File": "mm/Kconfig",
  "FirstLine": 1395,
  "LineCount": 35
}

Results:
{
  "Contents": "1395:\tconfig LRU_GEN\n1396:\t\tbool \"Multi-Gen LRU\"\n1397:\t\tdepends on MMU\n1398:\t\t# make sure folio-\u003eflags has enough spare bits\n1399:\t\tdepends on 64BIT || !SPARSEMEM || SPARSEMEM_VMEMMAP\n1400:\t\thelp\n1401:\t\t  A high performance LRU implementation to overcommit memory. See\n1402:\t\t  Documentation/admin-guide/mm/multigen_lru.rst for details.\n1403:\t\n1404:\tconfig LRU_GEN_ENABLED\n1405:\t\tbool \"Enable by default\"\n1406:\t\tdepends on LRU_GEN\n1407:\t\thelp\n1408:\t\t  This option enables the multi-gen LRU by default.\n1409:\t\n1410:\tconfig LRU_GEN_STATS\n1411:\t\tbool \"Full stats for debugging\"\n1412:\t\tdepends on LRU_GEN\n1413:\t\thelp\n1414:\t\t  Do not enable this option unless you plan to look at historical stats\n1415:\t\t  from evicted generations for debugging purpose.\n1416:\t\n1417:\t\t  This option has a per-memcg and per-node memory overhead.\n1418:\t\n1419:\tconfig LRU_GEN_WALKS_MMU\n1420:\t\tdef_bool y\n1421:\t\tdepends on LRU_GEN \u0026\u0026 ARCH_HAS_HW_PTE_YOUNG\n1422:\t# }\n1423:\t\n1424:\tconfig LOCK_MM_AND_FIND_VMA\n1425:\t\tbool\n1426:\t\tdepends on !STACK_GROWSUP\n1427:\t\n1428:\tconfig IOMMU_MM_DATA\n1429:\t\tbool\n"
}

65/2 2026/11/02 22:10 llm patch-evaluator
0m Model:
gemini-3.8-flash

Args:
null

Results:
null

Tokens:
							input: 60777
							output: 36
							thoughts: 109
						

Thoughts:
**Analyzing LRU and Page Table Dump Configurations**

Okay, so I'm looking at these kernel configurations and trying to understand their dependencies. First, `LRU_GEN_WALKS_MMU` is triggered if both `LRU_GEN` and `ARCH_HAS_HW_PTE_YOUNG` are set. That makes sense, uses hardware PTE young bits. But then, `LRU_GEN` is in turn dependent on `MMU` being enabled. Seems like standard dependencies there.

Now, `LRU_GEN_ENABLED`, which by default, enables multi-gen LRU. Good to know, it's probably useful to enable it when testing the generation.

But what I'm really trying to get at is how we enable `ptdump` in `arch/x86/mm/dump_pagetables.c`. I need a quick way to enable and disable it. My first instinct is to check the debug Kconfig file. Let's see what `arch/x86/Kconfig.debug` says.



66/2 2026/11/02 22:11 tool grepper
0m Args:
{
  "Expression": "dump_pagetables\\.o",
  "PathPrefixes": [
    "arch/x86/mm/"
  ]
}

Results:
{
  "Output": "arch/x86/mm/Makefile=36=obj-$(CONFIG_HUGETLB_PAGE)\t+= hugetlbpage.o\narch/x86/mm/Makefile:37:obj-$(CONFIG_PTDUMP)\t\t+= dump_pagetables.o\narch/x86/mm/Makefile-38-obj-$(CONFIG_PTDUMP_DEBUGFS)\t+= debug_pagetables.o\n"
}

67/2 2026/11/02 22:11 llm patch-evaluator
0m Model:
gemini-3.8-flash

Args:
null

Results:
null

Tokens:
							input: 61259
							output: 18
							thoughts: 27
						

Thoughts:
Okay, here's my take on those thoughts, tailored for an expert audience:

**Investigating `CONFIG_PTDUMP` and `CONFIG_PTDUMP_DEBUGFS`**

Alright, let's dive into this. My initial thought process revolves around pinpointing the definitions and locations of `CONFIG_PTDUMP` and `CONFIG_PTDUMP_DEBUGFS`. I immediately need to understand where these configuration options are declared within the kernel build system. I'll need to know this to understand their purpose, dependencies, and ultimately, their behavior. Where are they defined, and more importantly, how are they *used* within the kernel source? I'm anticipating that understanding the configuration options will quickly lead to understanding the functionality they enable and disable, and what their interaction with the `debugfs` filesystem is all about. This is the starting point for any deeper analysis or potential modification.



68/2 2026/11/02 22:11 tool grepper
0m Args:
{
  "Expression": "config PTDUMP"
}

Results:
{
  "Output": "arch/arm64/kvm/Kconfig=42=if KVM\narch/arm64/kvm/Kconfig-43-\narch/arm64/kvm/Kconfig:44:config PTDUMP_STAGE2_DEBUGFS\narch/arm64/kvm/Kconfig-45-\tbool \"Present the stage-2 pagetables to debugfs\"\n--\nmm/Kconfig=1527=config ARCH_HAS_PTDUMP\n--\nmm/Kconfig-1529-\nmm/Kconfig:1530:config PTDUMP\nmm/Kconfig-1531-\tbool\n--\nmm/Kconfig.debug=177=config DEBUG_RODATA_TEST\n--\nmm/Kconfig.debug-182-\nmm/Kconfig.debug:183:config PTDUMP_DEBUGFS\nmm/Kconfig.debug-184-\tbool \"Export kernel pagetable layout to userspace via debugfs\"\n"
}

69/2 2026/11/02 22:11 llm patch-evaluator
0m Model:
gemini-3.8-flash

Args:
null

Results:
null

Tokens:
							input: 61735
							output: 34
							thoughts: 19
						

Thoughts:
Okay, here's my interpretation of that thought process, tailored for an expert audience, and rendered in the first person:

**Navigating the Debugging Landscape in the Kernel Configuration**

Alright, I'm thinking I need to delve into some debugging options within the kernel build system. Specifically, I'm zeroing in on `mm/Kconfig.debug`. The memory management subsystem, as usual, seems like a likely candidate for intricate debug configurations. I'm focusing on that particular Kconfig file, and I'm guessing around line 183 will be the starting point. It's usually around that point where the core debugging features are listed. That file will almost certainly contain all sorts of crucial configuration options relating to debugging, tracing, and logging within the memory management subsystem. Time to dig in and see what goodies are there.



70/2 2026/11/02 22:11 tool read-file
0m Args:
{
  "File": "mm/Kconfig.debug",
  "FirstLine": 180,
  "LineCount": 20
}

Results:
{
  "Contents": " 180:\t\thelp\n 181:\t\t  This option enables a testcase for the setting rodata read-only.\n 182:\t\n 183:\tconfig PTDUMP_DEBUGFS\n 184:\t\tbool \"Export kernel pagetable layout to userspace via debugfs\"\n 185:\t\tdepends on DEBUG_KERNEL\n 186:\t\tdepends on DEBUG_FS\n 187:\t\tdepends on ARCH_HAS_PTDUMP\n 188:\t\tselect PTDUMP\n 189:\t\thelp\n 190:\t\t  Say Y here if you want to show the kernel pagetable layout in a\n 191:\t\t  debugfs file. This information is only useful for kernel developers\n 192:\t\t  who are working in architecture specific areas of the kernel.\n 193:\t\t  It is probably not a good idea to enable this feature in a production\n 194:\t\t  kernel.\n 195:\t\n 196:\t\t  If in doubt, say N.\n 197:\t\n 198:\tconfig HAVE_DEBUG_KMEMLEAK\n 199:\t\tbool\n"
}

71/2 2026/11/02 22:11 llm patch-evaluator
0m Model:
gemini-3.8-flash

Args:
null

Results:
null

Tokens:
							input: 62254
							output: 23
							thoughts: 52
						

Thoughts:
Here's my summary, as I see it:

**Digging into Page Table Dumps and Direct GBPAGES**

Okay, so `CONFIG_PTDUMP_DEBUGFS` automatically selects `CONFIG_PTDUMP`. That's straightforward, and likely means we're dealing with debugging page table structures. Now, I see the conditional involving `CONFIG_X86_DIRECT_GBPAGES`. This makes me think we're dealing with x86 architecture features specifically, potentially related to how we handle larger memory pages. I better go double-check the Kconfig file for `CONFIG_X86_DIRECT_GBPAGES` to get the full context. What are the dependencies? What are the implications? Let's get the full picture.



72/2 2026/11/02 22:11 tool grepper
0m Args:
{
  "Expression": "config X86_DIRECT_GBPAGES"
}

Results:
{
  "Output": "arch/x86/Kconfig=1457=config X86_PAE\n--\narch/x86/Kconfig-1466-\narch/x86/Kconfig:1467:config X86_DIRECT_GBPAGES\narch/x86/Kconfig-1468-\tdef_bool y\n"
}

73/2 2026/11/02 22:11 llm patch-evaluator
0m Model:
gemini-3.8-flash

Args:
null

Results:
null

Tokens:
							input: 62582
							output: 37
							thoughts: 47
						

Thoughts:
Here's my summary, as though I'm thinking through this myself:

**Investigating a Configuration Dependency**

Okay, so `X86_DIRECT_GBPAGES` has this dependency on `X86_64`. I need to understand that relationship better. The most logical place to start is the configuration file itself, so let's check lines 1465-1475 in `arch/x86/Kconfig`. That should spell out the details of this specific dependency. This is pretty standard practice when you're digging into configuration options; the `Kconfig` files are usually the best source of truth for understanding these kinds of dependencies.



74/2 2026/11/02 22:12 tool read-file
0m Args:
{
  "File": "arch/x86/Kconfig",
  "FirstLine": 1465,
  "LineCount": 15
}

Results:
{
  "Contents": "1465:\t\t  consumes more pagetable space per process.\n1466:\t\n1467:\tconfig X86_DIRECT_GBPAGES\n1468:\t\tdef_bool y\n1469:\t\tdepends on X86_64\n1470:\t\thelp\n1471:\t\t  Certain kernel features effectively disable kernel\n1472:\t\t  linear 1 GB mappings (even if the CPU otherwise\n1473:\t\t  supports them), so don't confuse the user by printing\n1474:\t\t  that we have them enabled.\n1475:\t\n1476:\tconfig X86_CPA_STATISTICS\n1477:\t\tbool \"Enable statistic for Change Page Attribute\"\n1478:\t\tdepends on DEBUG_FS\n1479:\t\thelp\n"
}

75/2 2026/11/02 22:12 llm patch-evaluator
0m Model:
gemini-3.8-flash

Args:
null

Results:
null

Tokens:
							input: 63023
							output: 303
							thoughts: 1204
						

Thoughts:
**Fuzzing Verdict: WorthFuzzing**

Okay, let's break this down. First, on x86_64, we're dealing with a few key configs: `LRU_GEN`, `LRU_GEN_ENABLED`, `PTDUMP_DEBUGFS`, and `X86_DIRECT_GBPAGES`. These seem relevant, let's keep an eye on them.

Now, focusing on the core logic being modified, I see changes in `mm/vmscan.c` to `walk_pud_range`, `walk_pmd_range`, and `walk_mm`. These are related to the MGLRU (Multi-Gen LRU) page table scanning. It looks like they are fixing issues where the code was passing the addresses of stack variables (like `&val`) instead of the correct `pud + i` or `pmd + i` entries. Important to note, these functions are entrypoints for walking page tables in MGLRU.

In `arch/x86/mm/dump_pagetables.c`, there's modification to `ptdump_walk_pgd_level_core` to correctly populate the `.mm` field and modifications to `effective_prot` using `enum pgtable_level` and `mm_first_pgtable_level(st->mm)`. This is obviously relevant to page table dumping, and `effective_prot` is static within the file.

Then, we have changes in `arch/x86/mm/pat/set_memory.c` affecting `collapse_pud_page` and `populate_pud`, which are now using `direct_gbpages_enabled()` instead of directly reading `direct_gbpages`. This deals with the CPA (Change Page Attribute) process and how we handle 1GB direct pages.

My first thought is, are these changes widely used? The MGLRU walkers and the page table dumper (`ptdump_walk_pgd_level_core`) are not, they are only used under specific conditions. However,  `collapse_pud_page` and `populate_pud` are called during CPA and are always reachable.

The patch touches other architectures like arm, arm64, loongarch, mips, openrisc, riscv, and asm-generic which confirms its scope beyond amd64. Focusing on amd64, we have those five functions: `walk_pud_range`, `walk_pmd_range`, `ptdump_walk_pgd_level_core`, `collapse_pud_page`, and `populate_pud`. And, *of course* `walk_pte_range`, which is called by `walk_pmd_range`, is also affected by the `pmd + i` change. This is a very clean and focused set of modifications.

Double-checking negative criteria, none of the changes are to non-kernel code, unreachable hardware, or totally unreachable execution paths. MGLRU page table walks and x86 page table dumping are both reachable from userspace, especially in standard QEMU/KVM environments. So, this warrants fuzzing to detect any regressions.

The reasoning is pretty clear: The patch modifies page-table folding abstractions and generic page table walking across architectures, fixing memory management bugs. On amd64, there are also x86 page-table dumping logic updates and modifications to x86 PAT/CPA and 1GB direct page checks. These changes affect reachable memory management and architecture-specific logic. We need to fuzz these and check for regressions or assertion failures during page table traversal.

So, the plan is to enable the following configs: `LRU_GEN`, `LRU_GEN_ENABLED`, `PTDUMP_DEBUGFS`, and `X86_DIRECT_GBPAGES`. And, we'll focus fuzzing efforts on these functions: `walk_pud_range`, `walk_pmd_range`, `ptdump_walk_pgd_level_core`, `collapse_pud_page`, and `populate_pud`.

Everything checks out. I'm ready to call `set-results`.



76/2 2026/11/02 22:12 tool set-results
0m Args:
{
  "EnableConfigs": [
    "LRU_GEN",
    "LRU_GEN_ENABLED",
    "PTDUMP_DEBUGFS",
    "X86_DIRECT_GBPAGES"
  ],
  "FocusSymbols": [
    "walk_pud_range",
    "walk_pmd_range",
    "ptdump_walk_pgd_level_core",
    "collapse_pud_page",
    "populate_pud"
  ],
  "Reasoning": "The patch refactors page-table folding definitions across asm-generic and architecture headers, addressing cases where addresses of stack variables were incorrectly passed to page-table walkers. In mm/vmscan.c (MGLRU), walk_pud_range() and walk_pmd_range() are modified to pass proper page table pointers to lower-level walkers. On x86, arch/x86/mm/dump_pagetables.c is updated to use the reordered pgtable_level enum and the new mm_first_pgtable_level() helper to determine effective permissions, and arch/x86/mm/pat/set_memory.c adopts the new direct_gbpages_enabled() helper in CPA logic. Because these modifications alter core memory management and page-table walking logic in reachable paths on x86_64, fuzzing is warranted to uncover potential invariant violations or regressions.",
  "WorthFuzzing": true
}

Results:
{
  "EnableConfigs": [
    "LRU_GEN",
    "LRU_GEN_ENABLED",
    "PTDUMP_DEBUGFS",
    "X86_DIRECT_GBPAGES"
  ],
  "FocusSymbols": [
    "walk_pud_range",
    "walk_pmd_range",
    "ptdump_walk_pgd_level_core",
    "collapse_pud_page",
    "populate_pud"
  ],
  "Reasoning": "The patch refactors page-table folding definitions across asm-generic and architecture headers, addressing cases where addresses of stack variables were incorrectly passed to page-table walkers. In mm/vmscan.c (MGLRU), walk_pud_range() and walk_pmd_range() are modified to pass proper page table pointers to lower-level walkers. On x86, arch/x86/mm/dump_pagetables.c is updated to use the reordered pgtable_level enum and the new mm_first_pgtable_level() helper to determine effective permissions, and arch/x86/mm/pat/set_memory.c adopts the new direct_gbpages_enabled() helper in CPA logic. Because these modifications alter core memory management and page-table walking logic in reachable paths on x86_64, fuzzing is warranted to uncover potential invariant violations or regressions.",
  "WorthFuzzing": true
}

77/1 2026/11/02 22:12 action If
1m Args:
{
  "WorthFuzzing": true
}

Results:
null

78/2 2026/11/02 22:12 agent kmsan-evaluator
1m Model:
core

Args:
null

Results:
{
  "KMSANReasoning": "The patch series cleans up and refactors compile-time page table folding across architecture-independent MM code and specific architectures (ARM, LoongArch, MIPS, RISC-V, and x86). Key changes include:\n1. Hard-coding dummy getters (pgdp_get, pudp_get, p4dp_get) to return explicitly zero-initialized values ({ 0 }) for compile-time folded page table levels, and adding build-time checks (BUILD_BUG_ON) to prevent folded helpers from operating on dummy values.\n2. In mm/vmscan.c and KVM MMU walkers (LoongArch, RISC-V), replacing references to local stack-allocated page-table value variables with proper page-table pointers (e.g., pmd + i, pud + i) and lockless offset helpers.\n3. Introducing helper functions (direct_gbpages_enabled(), mm_first_pgtable_level()) and updating effective_prot tracking in x86 ptdump.\n\nNone of these changes introduce or expose uninitialized memory risks:\n- All new stack variables and dummy structs are explicitly zero-initialized (e.g., pgd_t dummy = { 0 }).\n- No structures or buffers are copied to user space (no copy_to_user, put_user, netlink skb, or ioctls modified).\n- Page table entries are read from live page tables or set to zeroed dummy values.\n- Any pointer dereferencing or boundary errors in page table walking are within the scope of KASAN (OOB/UAF) rather than KMSAN.\n\nTherefore, a dedicated KMSAN fuzzing session is not warranted.",
  "NeedsKMSAN": false
}

Instruction:
You are an expert Linux kernel security engineer specializing in kernel memory
error detectors (KASAN and KMSAN). Your job is to review the provided patch series and
determine if the code changes justify spawning a dedicated KMSAN (KernelMemorySanitizer)
fuzzing session in addition to standard KASAN fuzzing.

CRITICAL DISTINCTION BETWEEN KASAN AND KMSAN:
- Standard KASAN kernel builds (upstream-apparmor-kasan.config) already enable
  a comprehensive suite of debugging tools and sanitizers, including KASAN
  (out-of-bounds accesses, use-after-free, double free, invalid free), LOCKDEP
  (locking bugs and deadlocks), UB-sanitizers, and memory corruption checks.
- KMSAN (KernelMemorySanitizer) detects reads of UNINITIALIZED memory (stack, heap,
  or page allocations) and kernel-to-user memory info-leaks.

Rule: THERE IS NO SENSE IN RUNNING A KMSAN SESSION IF A BUG CAN BE CAUGHT BY KASAN,
LOCKDEP, OR OTHER STANDARD BUG DETECTORS.
A dedicated KMSAN fuzzing session incurs significant resource costs. You must ONLY
set NeedsKMSAN=true if the code changes introduce or expose UNINITIALIZED MEMORY risks
that are detected ONLY by KMSAN.

Look holistically at the patch series and surrounding code. Even if no direct
uninitialized field accesses or new buffer allocations are added in the diff itself,
a patch may alter control flow, bounds checking, or data length calculations in ways
that change how the rest of the code operates on existing buffers (e.g. allowing
uninitialized stack/heap memory to be read, copied to user space, or used in control
flow). Do not hesitate to use your code access tools to inspect the surrounding code,
called functions, and callers.

Set NeedsKMSAN=true ONLY IF the patch introduces or modifies:
1. Kernel structures sent to user space (via copy_to_user, put_user, netlink skb
   attributes, ioctl output arguments, socket options, or BPF buffers) where fields
   or structure padding might not be fully initialized/zeroed.
2. Conditional logic or branching that depends on potentially uninitialized variables
   or struct fields.
3. Allocation or initialization of complex data structures where uninitialized fields
   could be read later in reachable code paths.
4. Bounds checks, lengths, or logic in a way that allows surrounding code to access
   uninitialized bytes of existing buffers.

Set NeedsKMSAN=false IF:
- The code changes primarily risk out-of-bounds access, array overflows, NULL pointer
  dereferences, locking deadlocks, or use-after-free bugs (these are already caught
  by KASAN, LOCKDEP, or standard bug detectors).
- All stack/heap structures touched or introduced by the patch are fully zeroed
  or initialized (e.g. using = {0}, memset, kzalloc) before being read or copied.
- The patch does not introduce any risk of uninitialized memory usage or info-leaks.

Use your code access tools to inspect the surrounding code if necessary, then provide
detailed KMSANReasoning contrasting KASAN vs KMSAN applicability for this patch.
Prefer calling several tools at the same time to save round-trips.


Use set-results tool to provide results of the analysis.
It must be called exactly once before the final reply.
Ignore results of this tool.

Prompt:
Target architecture: amd64

For your convenience, here is the diff of the changes:
commit a0a56350684d753b1a84f5f158b0a4d9cd6c69a5
Author: syz-cluster <triage@syzkaller.com>
Date:   Mon Nov 2 21:59:07 2026 +0000

    syz-cluster: applied patch under review

diff --git a/Documentation/mm/page_tables.rst b/Documentation/mm/page_tables.rst
index 126c8762825092..d4f3e2e343ca2b 100644
--- a/Documentation/mm/page_tables.rst
+++ b/Documentation/mm/page_tables.rst
@@ -143,15 +143,81 @@ pointers on each level is architecture-defined.::
 Page Table Folding
 ==================
 
-If the architecture does not use all the page table levels, they can be *folded*
-which means skipped, and all operations performed on page tables will be
-compile-time augmented to just skip a level when accessing the next lower
-level.
-
-Page table handling code that wishes to be architecture-neutral, such as the
-virtual memory manager, will need to be written so that it traverses all of the
-currently five levels. This style should also be preferred for
-architecture-specific code, so as to be robust to future changes.
+Not all architectures support 5-level page tables; while for some of them
+the exact number of supported page table levels is known at compile time,
+others can determine the number of page table levels at runtime based on
+hardware support and address space sizes.
+
+Generic page table walking code always assumes that 5 levels of page table
+exist. To make page table walking code not have to worry about that,
+`compile-time folding` and `runtime folding` of page tables are used.
+Compile-time folding is mostly handled in common code, whereas runtime folding
+is exclusively handled in architecture code.
+
+This description focuses on generic compile-time folded page tables; for
+architecture-specific variants, some details can vary, however, without
+affecting common page table walkers.
+
+When walking folded page tables, all upper page table levels up to the supported
+level are skipped in page table walkers: this is achieved by (a) treating
+entries in upper page table levels as present and pointing at a page table; and
+(b) having page table walkers cast the entry pointer to the next-level entry
+instead of dereferencing that table. From the perspective of a page table
+walker, the entry points at itself.
+
+Assuming compile-time folded 4-level page tables, to achieve (a), pgd_present()
+and pgd_leaf() are hard-coded to indicate a present page table entry that
+points at a page table, and to achieve (b) p4d_offset() and
+p4d_offset_lockless() simply cast the page table entry pointer to the next
+lower level.
+
+In the current design, this is further modeled by having the P4D have a
+single page table entry::
+
+        PGD
+  --> +------+          NOP4D
+      | ptr0 |-------> +------+           PUD
+      | ptr1 |-        | ptr0 |-------> +-----+
+      | ptr2 | \       +------+         | ptr |-------> ...
+      | ptr3 |  \                       | ptr |
+        ...      \                        ..
+                  \        NOP4D
+                   +----> +------+           PUD
+                          | ptr1 |-------> +-----+
+                          +------+         | ptr |-------> ...
+                                           | ptr |
+                                             ...
+
+Note that the arrows from PGD to NOP4D represent page-table-walker
+transitions, not pointers stored in the pgd entries.
+
+Using p4d as an example, `nop4d`/`p4d folded` translates to the following:
+
+- p4d is considered folded into pgd; both are operating on the same page
+  table.
+
+- Most pgd_* helpers are hard-coded dummy functions that ignore the passed
+  pgd_t values entirely. Exceptions are pgd_val() and low-level helpers
+  set_pgd() + pgd_page_vaddr(), which effectively translate to set_p4d()/
+  p4d_pgtable() to keep existing arch code working.
+
+  Architectures must provide p4d_* helpers (unless further common
+  compile-time folding applies).
+
+- PTRS_PER_P4D is hard-coded to 1. Architectures must define PTRS_PER_PGD.
+
+To avoid reading a value that will never be used but cannot be entirely
+optimized out, compile-time folded page table code also makes pXdp_get()
+return a constant dummy value.
+
+In common code, this only affects pXd_val() when used for printing page
+table entries for debugging purposes. As we don't want architecture code
+that uses set_pXd(), pgd_page_vaddr() or pXd_pgtable() to accidentally
+operate on dummy values, the compiler will error out if it detects that the
+helpers are used with dummy values. For a folded level, pXd_page() must not
+be used and unconditionally triggers a compiler error. Architecture code must
+instead call the helpers on the proper first page table level: e.g., set_p4d()
+instead of set_pgd().
 
 
 MMU, TLB, and Page Faults
diff --git a/arch/arm/include/asm/page-nommu.h b/arch/arm/include/asm/page-nommu.h
index e74415c959beac..88659b38e23649 100644
--- a/arch/arm/include/asm/page-nommu.h
+++ b/arch/arm/include/asm/page-nommu.h
@@ -18,12 +18,12 @@
  */
 typedef unsigned long pte_t;
 typedef unsigned long pmd_t;
-typedef unsigned long pgd_t[2];
+typedef unsigned long pgd_t;
 typedef unsigned long pgprot_t;
 
 #define pte_val(x)      (x)
 #define pmd_val(x)      (x)
-#define pgd_val(x)	((x)[0])
+#define pgd_val(x)      (x)
 #define pgprot_val(x)   (x)
 
 #define __pte(x)        (x)
diff --git a/arch/arm/include/asm/pgtable-2level-types.h b/arch/arm/include/asm/pgtable-2level-types.h
index 650e793f41429f..805044ea315b7d 100644
--- a/arch/arm/include/asm/pgtable-2level-types.h
+++ b/arch/arm/include/asm/pgtable-2level-types.h
@@ -25,7 +25,7 @@ typedef struct { pteval_t pgprot; } pgprot_t;
 
 #define pte_val(x)      ((x).pte)
 #define pmd_val(x)      ((x).pmd)
-#define pgd_val(x)	((x).pgd[0])
+#define pgd_val(x)      ((x).pgd[0])
 #define pgprot_val(x)   ((x).pgprot)
 
 #define __pte(x)        ((pte_t) { (x) } )
@@ -36,14 +36,28 @@ typedef struct { pteval_t pgprot; } pgprot_t;
 /*
  * .. while these make it easier on the compiler
  */
+typedef u64 pgdval_t;
+
 typedef pteval_t pte_t;
 typedef pmdval_t pmd_t;
-typedef pmdval_t pgd_t[2];
+typedef pgdval_t pgd_t;
 typedef pteval_t pgprot_t;
 
 #define pte_val(x)      (x)
 #define pmd_val(x)      (x)
-#define pgd_val(x)	((x)[0])
+
+static inline pmdval_t pgd_val(pgd_t pgd)
+{
+	/*
+	 * A PGD entry actually corresponds to two PMD entries in the PMD table.
+	 * Both PMD entries point to PTE tables residing in the same physical page,
+	 * but at different offsets. See include/asm/pgtable-2level.h for details.
+	 *
+	 * Historically, pgd_val() has returned the lower of the two PMD entries.
+	 */
+	return (*(pmdval_t (*)[2])&pgd)[0];
+}
+
 #define pgprot_val(x)   (x)
 
 #define __pte(x)        (x)
diff --git a/arch/arm/include/asm/pgtable.h b/arch/arm/include/asm/pgtable.h
index 8dd17d20faa33f..ff3868acea9454 100644
--- a/arch/arm/include/asm/pgtable.h
+++ b/arch/arm/include/asm/pgtable.h
@@ -134,8 +134,6 @@ extern pgprot_t phys_mem_access_prot(struct file *file, unsigned long pfn,
 
 extern pgd_t swapper_pg_dir[PTRS_PER_PGD];
 
-#define pgdp_get(pgpd)		READ_ONCE(*pgdp)
-
 #define pud_page(pud)		pmd_page(__pmd(pud_val(pud)))
 #define pud_write(pud)		pmd_write(__pmd(pud_val(pud)))
 
diff --git a/arch/arm64/include/asm/pgtable.h b/arch/arm64/include/asm/pgtable.h
index 763c5a411d64e3..b391f325670f81 100644
--- a/arch/arm64/include/asm/pgtable.h
+++ b/arch/arm64/include/asm/pgtable.h
@@ -1141,29 +1141,6 @@ static inline bool pgtable_l5_enabled(void) { return false; }
 #define p4d_clear_fixmap()
 
 #define p4d_offset_kimg(dir,addr)	((p4d_t *)dir)
-
-static inline
-p4d_t *p4d_offset_lockless_folded(pgd_t *pgdp, pgd_t pgd, unsigned long addr)
-{
-	/*
-	 * With runtime folding of the pud, pud_offset_lockless() passes
-	 * the 'pgd_t *' we return here to p4d_to_folded_pud(), which
-	 * will offset the pointer assuming that it points into
-	 * a page-table page. However, the fast GUP path passes us a
-	 * pgd_t allocated on the stack and so we must use the original
-	 * pointer in 'pgdp' to construct the p4d pointer instead of
-	 * using the generic p4d_offset_lockless() implementation.
-	 *
-	 * Note: reusing the original pointer means that we may
-	 * dereference the same (live) page-table entry multiple times.
-	 * This is safe because it is still only loaded once in the
-	 * context of each level and the CPU guarantees same-address
-	 * read-after-read ordering.
-	 */
-	return p4d_offset(pgdp, addr);
-}
-#define p4d_offset_lockless p4d_offset_lockless_folded
-
 #endif  /* CONFIG_PGTABLE_LEVELS > 4 */
 
 #define pgd_set_fixmap(addr)	((pgd_t *)set_fixmap_offset(FIX_PGD, addr))
diff --git a/arch/loongarch/include/asm/pgtable.h b/arch/loongarch/include/asm/pgtable.h
index f876031351319a..9a62b87c246de0 100644
--- a/arch/loongarch/include/asm/pgtable.h
+++ b/arch/loongarch/include/asm/pgtable.h
@@ -631,7 +631,9 @@ static inline long pmd_protnone(pmd_t pmd)
 #endif /* CONFIG_ARCH_HAS_PTE_PROTNONE */
 
 #define pmd_leaf(pmd)		((pmd_val(pmd) & _PAGE_HUGE) != 0)
+#ifndef __PAGETABLE_PMD_FOLDED
 #define pud_leaf(pud)		((pud_val(pud) & _PAGE_HUGE) != 0)
+#endif
 
 /*
  * We provide our own get_unmapped area to cope with the virtual aliasing
diff --git a/arch/loongarch/kvm/mmu.c b/arch/loongarch/kvm/mmu.c
index 64fddd9f0ada1e..ab0a06b73e8d38 100644
--- a/arch/loongarch/kvm/mmu.c
+++ b/arch/loongarch/kvm/mmu.c
@@ -679,10 +679,10 @@ static int host_pfn_mapping_level(struct kvm *kvm, gfn_t gfn,
 	int level = 0;
 	unsigned long hva;
 	unsigned long flags;
-	pgd_t pgd;
-	p4d_t p4d;
-	pud_t pud;
-	pmd_t pmd;
+	pgd_t *pgdp, pgd;
+	p4d_t *p4dp, p4d;
+	pud_t *pudp, pud;
+	pmd_t *pmdp, pmd;
 
 	/*
 	 * Note, using the already-retrieved memslot and __gfn_to_hva_memslot()
@@ -708,19 +708,23 @@ static int host_pfn_mapping_level(struct kvm *kvm, gfn_t gfn,
 	 * value) and then p*d_offset() walks into the target huge page instead
 	 * of the old page table (sees the new value).
 	 */
-	pgd = pgdp_get(pgd_offset(kvm->mm, hva));
+	pgdp = pgd_offset(kvm->mm, hva);
+	pgd = pgdp_get(pgdp);
 	if (pgd_none(pgd))
 		goto out;
 
-	p4d = p4dp_get(p4d_offset(&pgd, hva));
+	p4dp = p4d_offset_lockless(pgdp, pgd, hva);
+	p4d = p4dp_get(p4dp);
 	if (p4d_none(p4d) || !p4d_present(p4d))
 		goto out;
 
-	pud = pudp_get(pud_offset(&p4d, hva));
+	pudp = pud_offset_lockless(p4dp, p4d, hva);
+	pud = pudp_get(pudp);
 	if (pud_none(pud) || !pud_present(pud))
 		goto out;
 
-	pmd = pmdp_get(pmd_offset(&pud, hva));
+	pmdp = pmd_offset_lockless(pudp, pud, hva);
+	pmd = pmdp_get(pmdp);
 	if (pmd_none(pmd) || !pmd_present(pmd))
 		goto out;
 
diff --git a/arch/mips/include/asm/pgtable.h b/arch/mips/include/asm/pgtable.h
index fa7b935f947ca0..151f4d15bd12bf 100644
--- a/arch/mips/include/asm/pgtable.h
+++ b/arch/mips/include/asm/pgtable.h
@@ -745,8 +745,10 @@ static inline pmd_t pmdp_huge_get_and_clear(struct mm_struct *mm,
 
 #ifdef _PAGE_HUGE
 #define pmd_leaf(pmd)	((pmd_val(pmd) & _PAGE_HUGE) != 0)
+#ifndef __PAGETABLE_PMD_FOLDED
 #define pud_leaf(pud)	((pud_val(pud) & _PAGE_HUGE) != 0)
 #endif
+#endif
 
 #define gup_fast_permitted(start, end)	(!cpu_has_dc_aliases)
 
diff --git a/arch/openrisc/include/asm/pgtable.h b/arch/openrisc/include/asm/pgtable.h
index 13afcc0bd8631b..7fceed641610c4 100644
--- a/arch/openrisc/include/asm/pgtable.h
+++ b/arch/openrisc/include/asm/pgtable.h
@@ -331,9 +331,6 @@ static inline unsigned long pmd_page_vaddr(pmd_t pmd)
 	return ((unsigned long) __va(pmd_val(pmd) & PAGE_MASK));
 }
 
-#define __pmd_offset(address) \
-	(((address) >> PMD_SHIFT) & (PTRS_PER_PMD-1))
-
 #define PFN_PTE_SHIFT		PAGE_SHIFT
 #define pte_pfn(x)		((unsigned long)(((x).pte)) >> PAGE_SHIFT)
 #define pfn_pte(pfn, prot)  __pte((((pfn) << PAGE_SHIFT)) | pgprot_val(prot))
diff --git a/arch/riscv/kvm/mmu.c b/arch/riscv/kvm/mmu.c
index 371fccaf9df08a..a062e8508d3f97 100644
--- a/arch/riscv/kvm/mmu.c
+++ b/arch/riscv/kvm/mmu.c
@@ -450,10 +450,10 @@ static int get_hva_mapping_size(struct kvm *kvm,
 {
 	int size = PAGE_SIZE;
 	unsigned long flags;
-	pgd_t pgd;
-	p4d_t p4d;
-	pud_t pud;
-	pmd_t pmd;
+	pgd_t *pgdp, pgd;
+	p4d_t *p4dp, p4d;
+	pud_t *pudp, pud;
+	pmd_t *pmdp, pmd;
 
 	/*
 	 * Disable IRQs to prevent concurrent tear down of host page tables,
@@ -469,15 +469,18 @@ static int get_hva_mapping_size(struct kvm *kvm,
 	 * value) and then p*d_offset() walks into the target huge page instead
 	 * of the old page table (sees the new value).
 	 */
-	pgd = pgdp_get(pgd_offset(kvm->mm, hva));
+	pgdp = pgd_offset(kvm->mm, hva);
+	pgd = pgdp_get(pgdp);
 	if (pgd_none(pgd))
 		goto out;
 
-	p4d = p4dp_get(p4d_offset(&pgd, hva));
+	p4dp = p4d_offset_lockless(pgdp, pgd, hva);
+	p4d = p4dp_get(p4dp);
 	if (p4d_none(p4d) || !p4d_present(p4d))
 		goto out;
 
-	pud = pudp_get(pud_offset(&p4d, hva));
+	pudp = pud_offset_lockless(p4dp, p4d, hva);
+	pud = pudp_get(pudp);
 	if (pud_none(pud) || !pud_present(pud))
 		goto out;
 
@@ -486,7 +489,8 @@ static int get_hva_mapping_size(struct kvm *kvm,
 		goto out;
 	}
 
-	pmd = pmdp_get(pmd_offset(&pud, hva));
+	pmdp = pmd_offset_lockless(pudp, pud, hva);
+	pmd = pmdp_get(pmdp);
 	if (pmd_none(pmd) || !pmd_present(pmd))
 		goto out;
 
diff --git a/arch/riscv/mm/fault.c b/arch/riscv/mm/fault.c
index 04ed6f8acae4fd..b2bcaf10d13f1d 100644
--- a/arch/riscv/mm/fault.c
+++ b/arch/riscv/mm/fault.c
@@ -167,14 +167,16 @@ bad_area(struct pt_regs *regs, struct mm_struct *mm, int code,
 
 static inline void vmalloc_fault(struct pt_regs *regs, int code, unsigned long addr)
 {
-	pgd_t *pgd, *pgd_k;
-	pud_t *pud_k;
-	p4d_t *p4d_k;
-	pmd_t *pmd_k;
-	pte_t *pte_k;
+	pgd_t *pgdp_k, *pgdp, pgd_k;
+	p4d_t *p4dp_k, *p4dp;
+	pud_t *pudp_k, *pudp, pud_k;
+	pmd_t *pmdp_k, *pmdp, pmd_k;
+	pte_t *ptep_k;
 	int index;
 	unsigned long pfn;
 
+	BUILD_BUG_ON(CONFIG_PGTABLE_LEVELS != 5 && CONFIG_PGTABLE_LEVELS != 2);
+
 	/* User mode accesses just cause a SIGSEGV */
 	if (user_mode(regs))
 		return do_trap(regs, SIGSEGV, code, addr);
@@ -189,39 +191,51 @@ static inline void vmalloc_fault(struct pt_regs *regs, int code, unsigned long a
 	 */
 	index = pgd_index(addr);
 	pfn = csr_read(CSR_SATP) & SATP_PPN;
-	pgd = (pgd_t *)pfn_to_virt(pfn) + index;
-	pgd_k = init_mm.pgd + index;
 
-	if (!pgd_present(pgdp_get(pgd_k))) {
+	pgdp = (pgd_t *)pfn_to_virt(pfn) + index;
+	pgdp_k = init_mm.pgd + index;
+
+	pgd_k = pgdp_get(pgdp_k);
+	if (!pgd_present(pgd_k)) {
 		no_context(regs, addr);
 		return;
 	}
-	set_pgd(pgd, pgdp_get(pgd_k));
+	if (CONFIG_PGTABLE_LEVELS == 5)
+		set_pgd(pgdp, pgd_k);
 
-	p4d_k = p4d_offset(pgd_k, addr);
-	if (!p4d_present(p4dp_get(p4d_k))) {
+	p4dp = p4d_offset(pgdp, addr);
+	p4dp_k = p4d_offset(pgdp_k, addr);
+	if (!p4d_present(p4dp_get(p4dp_k))) {
 		no_context(regs, addr);
 		return;
 	}
 
-	pud_k = pud_offset(p4d_k, addr);
-	if (!pud_present(pudp_get(pud_k))) {
+	pudp = pud_offset(p4dp, addr);
+	pudp_k = pud_offset(p4dp_k, addr);
+
+	pud_k = pudp_get(pudp_k);
+	if (!pud_present(pud_k)) {
 		no_context(regs, addr);
 		return;
 	}
-	if (pud_leaf(pudp_get(pud_k)))
+	if (pud_leaf(pud_k))
 		goto flush_tlb;
 
 	/*
 	 * Since the vmalloc area is global, it is unnecessary
 	 * to copy individual PTEs
 	 */
-	pmd_k = pmd_offset(pud_k, addr);
-	if (!pmd_present(pmdp_get(pmd_k))) {
+	pmdp = pmd_offset(pudp, addr);
+	pmdp_k = pmd_offset(pudp_k, addr);
+
+	pmd_k = pmdp_get(pmdp_k);
+	if (!pmd_present(pmd_k)) {
 		no_context(regs, addr);
 		return;
 	}
-	if (pmd_leaf(pmdp_get(pmd_k)))
+	if (CONFIG_PGTABLE_LEVELS == 2)
+		set_pmd(pmdp, pmd_k);
+	if (pmd_leaf(pmd_k))
 		goto flush_tlb;
 
 	/*
@@ -230,8 +244,8 @@ static inline void vmalloc_fault(struct pt_regs *regs, int code, unsigned long a
 	 * addresses. If we don't do this, this will just
 	 * silently loop forever.
 	 */
-	pte_k = pte_offset_kernel(pmd_k, addr);
-	if (!pte_present(ptep_get(pte_k))) {
+	ptep_k = pte_offset_kernel(pmdp_k, addr);
+	if (!pte_present(ptep_get(ptep_k))) {
 		no_context(regs, addr);
 		return;
 	}
diff --git a/arch/x86/include/asm/pgtable.h b/arch/x86/include/asm/pgtable.h
index 000fc6c1ca37c4..a2770821218783 100644
--- a/arch/x86/include/asm/pgtable.h
+++ b/arch/x86/include/asm/pgtable.h
@@ -1167,6 +1167,20 @@ static inline int pgd_none(pgd_t pgd)
 #ifndef __ASSEMBLER__
 
 extern int direct_gbpages;
+static inline bool direct_gbpages_enabled(void)
+{
+	/* Check the direct map config option: */
+	if (!IS_ENABLED(CONFIG_X86_DIRECT_GBPAGES))
+		return false;
+
+	/* Check the CPU feature: */
+	if (!boot_cpu_has(X86_FEATURE_GBPAGES))
+		return false;
+
+	/* Check the command-line and early setup variable: */
+	return direct_gbpages;
+}
+
 void init_mem_mapping(void);
 void early_alloc_pgt_buf(void);
 void __init poking_init(void);
diff --git a/arch/x86/kernel/cpu/common.c b/arch/x86/kernel/cpu/common.c
index 9bd50c68cd7e5e..12a51dba166276 100644
--- a/arch/x86/kernel/cpu/common.c
+++ b/arch/x86/kernel/cpu/common.c
@@ -2655,7 +2655,7 @@ void __init arch_cpu_finalize_init(void)
 		 * Right now we don't do that with gbpages because there seems
 		 * very little benefit for that case.
 		 */
-		if (!direct_gbpages)
+		if (!direct_gbpages_enabled())
 			set_memory_4k((unsigned long)__va(0), 1);
 	} else {
 		fpu__init_check_bugs();
diff --git a/arch/x86/kernel/machine_kexec_64.c b/arch/x86/kernel/machine_kexec_64.c
index c3f4a389992daa..0da0e89f2611a5 100644
--- a/arch/x86/kernel/machine_kexec_64.c
+++ b/arch/x86/kernel/machine_kexec_64.c
@@ -257,7 +257,7 @@ static int init_pgtable(struct kimage *image, unsigned long control_page)
 		info.kernpg_flag |= _PAGE_ENC;
 	}
 
-	if (direct_gbpages)
+	if (direct_gbpages_enabled())
 		info.direct_gbpages = true;
 
 	for (i = 0; i < nr_pfn_mapped; i++) {
diff --git a/arch/x86/mm/dump_pagetables.c b/arch/x86/mm/dump_pagetables.c
index 2afa7a23340e92..2acbe41eb58f26 100644
--- a/arch/x86/mm/dump_pagetables.c
+++ b/arch/x86/mm/dump_pagetables.c
@@ -38,6 +38,7 @@ struct pg_state {
 	bool check_wx;
 	unsigned long wx_pages;
 	struct seq_file *seq;
+	struct mm_struct *mm;
 };
 
 struct addr_marker {
@@ -248,13 +249,13 @@ static void note_wx(struct pg_state *st, unsigned long addr)
 		  (void *)st->start_address);
 }
 
-static void effective_prot(struct ptdump_state *pt_st, int level, u64 val)
+static void effective_prot(struct ptdump_state *pt_st, enum pgtable_level level, u64 val)
 {
 	struct pg_state *st = container_of(pt_st, struct pg_state, ptdump);
 	pgprotval_t prot = val & PTE_FLAGS_MASK;
 	pgprotval_t effective;
 
-	if (level > 0) {
+	if (level > mm_first_pgtable_level(st->mm)) {
 		pgprotval_t higher_prot = st->prot_levels[level - 1];
 
 		effective = (higher_prot & prot & (_PAGE_USER | _PAGE_RW)) |
@@ -268,27 +269,27 @@ static void effective_prot(struct ptdump_state *pt_st, int level, u64 val)
 
 static void effective_prot_pte(struct ptdump_state *st, pte_t pte)
 {
-	effective_prot(st, 4, pte_val(pte));
+	effective_prot(st, PGTABLE_LEVEL_PTE, pte_val(pte));
 }
 
 static void effective_prot_pmd(struct ptdump_state *st, pmd_t pmd)
 {
-	effective_prot(st, 3, pmd_val(pmd));
+	effective_prot(st, PGTABLE_LEVEL_PMD, pmd_val(pmd));
 }
 
 static void effective_prot_pud(struct ptdump_state *st, pud_t pud)
 {
-	effective_prot(st, 2, pud_val(pud));
+	effective_prot(st, PGTABLE_LEVEL_PUD, pud_val(pud));
 }
 
 static void effective_prot_p4d(struct ptdump_state *st, p4d_t p4d)
 {
-	effective_prot(st, 1, p4d_val(p4d));
+	effective_prot(st, PGTABLE_LEVEL_P4D, p4d_val(p4d));
 }
 
 static void effective_prot_pgd(struct ptdump_state *st, pgd_t pgd)
 {
-	effective_prot(st, 0, pgd_val(pgd));
+	effective_prot(st, PGTABLE_LEVEL_PGD, pgd_val(pgd));
 }
 
 
@@ -452,7 +453,8 @@ bool ptdump_walk_pgd_level_core(struct seq_file *m,
 		.level = -1,
 		.to_dmesg	= dmesg,
 		.check_wx	= checkwx,
-		.seq		= m
+		.seq		= m,
+		.mm		= mm,
 	};
 
 	ptdump_walk_pgd(&st.ptdump, mm, pgd);
diff --git a/arch/x86/mm/init.c b/arch/x86/mm/init.c
index 079f8c7e9e3cde..f0f4a06584c61c 100644
--- a/arch/x86/mm/init.c
+++ b/arch/x86/mm/init.c
@@ -251,7 +251,7 @@ static void __init probe_page_size_mask(void)
 		__default_kernel_pte_mask &= ~_PAGE_GLOBAL;
 
 	/* Enable 1 GB linear kernel mappings if available: */
-	if (direct_gbpages && boot_cpu_has(X86_FEATURE_GBPAGES)) {
+	if (direct_gbpages_enabled()) {
 		printk(KERN_INFO "Using GB pages for direct mapping\n");
 		page_size_mask |= 1 << PG_LEVEL_1G;
 	} else {
diff --git a/arch/x86/mm/pat/set_memory.c b/arch/x86/mm/pat/set_memory.c
index b1d5c5a0ffb41d..f27811e6e00bd7 100644
--- a/arch/x86/mm/pat/set_memory.c
+++ b/arch/x86/mm/pat/set_memory.c
@@ -130,7 +130,7 @@ void arch_report_meminfo(struct seq_file *m)
 	seq_printf(m, "DirectMap4M:    %8lu kB\n",
 			direct_pages_count[PG_LEVEL_2M] << 12);
 #endif
-	if (direct_gbpages)
+	if (direct_gbpages_enabled())
 		seq_printf(m, "DirectMap1G:    %8lu kB\n",
 			direct_pages_count[PG_LEVEL_1G] << 20);
 }
@@ -1355,7 +1355,7 @@ static int collapse_pud_page(pud_t *pud, unsigned long addr,
 	pmd_t *pmd, first;
 	int i;
 
-	if (!direct_gbpages)
+	if (!direct_gbpages_enabled())
 		return 0;
 
 	addr &= PUD_MASK;
@@ -1737,7 +1737,7 @@ static int populate_pud(struct cpa_data *cpa, unsigned long start, p4d_t *p4d,
 	/*
 	 * Map everything starting from the Gb boundary, possibly with 1G pages
 	 */
-	while (boot_cpu_has(X86_FEATURE_GBPAGES) && end - start >= PUD_SIZE) {
+	while (direct_gbpages_enabled() && end - start >= PUD_SIZE) {
 		set_pud(pud, pud_mkhuge(pfn_pud(cpa->pfn,
 				   canon_pgprot(pud_pgprot))));
 
diff --git a/include/asm-generic/pgtable-nop4d.h b/include/asm-generic/pgtable-nop4d.h
index 1cf739ee38aa15..12b01768134b5a 100644
--- a/include/asm-generic/pgtable-nop4d.h
+++ b/include/asm-generic/pgtable-nop4d.h
@@ -22,25 +22,61 @@ static inline int pgd_none(pgd_t pgd)		{ return 0; }
 static inline int pgd_bad(pgd_t pgd)		{ return 0; }
 static inline int pgd_present(pgd_t pgd)	{ return 1; }
 static inline void pgd_clear(pgd_t *pgd)	{ }
+static inline bool pgd_leaf(pgd_t pgd)		{ return false; }
+#define pgd_leaf pgd_leaf
+#define p4d_ERROR(p4d)				(pgd_ERROR((p4d).pgd))
 
 #define pgd_populate(mm, pgd, p4d)		do { } while (0)
 #define pgd_populate_safe(mm, pgd, p4d)		do { } while (0)
-/*
- * (p4ds are folded into pgds so this doesn't get actually called,
- * but the define is needed for a generic inline function.)
- */
-#define set_pgd(pgdptr, pgdval)	set_p4d((p4d_t *)(pgdptr), (p4d_t) { pgdval })
 
-static inline p4d_t *p4d_offset(pgd_t *pgd, unsigned long address)
+#define set_pgd(pgdptr, pgdval)						\
+({									\
+	pgd_check_dummy(pgdval);					\
+	set_p4d((p4d_t *)(pgdptr), (p4d_t) { pgdval });			\
+})
+
+static __always_inline pgd_t pgdp_get(pgd_t *pgdp)
 {
-	return (p4d_t *)pgd;
+	pgd_t dummy = { 0 };
+
+	return dummy;
 }
+#define pgdp_get pgdp_get
+
+#define pgd_check_dummy(pgd) BUILD_BUG_ON(__builtin_constant_p(pgd_val(pgd)))
+
+static __always_inline p4d_t *__p4d_offset(pgd_t *pgdp, unsigned long address)
+{
+	return (p4d_t *)pgdp;
+}
+
+#define p4d_offset(pgdp, address)					\
+({									\
+	pgd_check_dummy(*(pgdp));					\
+	__p4d_offset(pgdp, address);					\
+})
+
+static __always_inline p4d_t *__p4d_offset_lockless(pgd_t *pgdp, pgd_t pgd,
+		unsigned long address)
+{
+	return (p4d_t *)pgdp;
+}
+
+#define p4d_offset_lockless(pgdp, pgd, address)				\
+({									\
+	pgd_check_dummy(*(pgdp));					\
+	__p4d_offset_lockless(pgdp, pgd, address);			\
+})
 
 #define p4d_val(x)				(pgd_val((x).pgd))
 #define __p4d(x)				((p4d_t) { __pgd(x) })
 
-#define pgd_page(pgd)				(p4d_page((p4d_t){ pgd }))
-#define pgd_page_vaddr(pgd)			((unsigned long)(p4d_pgtable((p4d_t){ pgd })))
+#define pgd_page(pgd)				({ BUILD_BUG(); (struct page *)NULL; })
+#define pgd_page_vaddr(pgd)						\
+({ 									\
+	pgd_check_dummy(pgd);						\
+	(unsigned long)p4d_pgtable((p4d_t) { pgd });			\
+})
 
 /*
  * allocating and freeing a p4d is trivial: the 1-entry p4d is
diff --git a/include/asm-generic/pgtable-nopmd.h b/include/asm-generic/pgtable-nopmd.h
index ff4235cf84d772..11b04c10712957 100644
--- a/include/asm-generic/pgtable-nopmd.h
+++ b/include/asm-generic/pgtable-nopmd.h
@@ -31,28 +31,60 @@ static inline int pud_none(pud_t pud)		{ return 0; }
 static inline int pud_bad(pud_t pud)		{ return 0; }
 static inline int pud_present(pud_t pud)	{ return 1; }
 static inline int pud_user(pud_t pud)		{ return 0; }
-static inline int pud_leaf(pud_t pud)		{ return 0; }
+static inline bool pud_leaf(pud_t pud)		{ return false; }
+#define pud_leaf pud_leaf
 static inline void pud_clear(pud_t *pud)	{ }
 
 #define pud_populate(mm, pmd, pte)		do { } while (0)
 
-/*
- * (pmds are folded into puds so this doesn't get actually called,
- * but the define is needed for a generic inline function.)
- */
-#define set_pud(pudptr, pudval)			set_pmd((pmd_t *)(pudptr), (pmd_t) { pudval })
+#define set_pud(pudptr, pudval)						\
+({									\
+	pud_check_dummy(pudval);					\
+	set_pmd((pmd_t *)(pudptr), (pmd_t) { pudval });			\
+})
+
+static __always_inline pud_t pudp_get(pud_t *pudp)
+{
+	pud_t dummy = { 0 };
+
+	return dummy;
+}
+#define pudp_get pudp_get
 
-static inline pmd_t * pmd_offset(pud_t * pud, unsigned long address)
+#define pud_check_dummy(pud) BUILD_BUG_ON(__builtin_constant_p(pud_val(pud)))
+
+static __always_inline pmd_t *__pmd_offset(pud_t *pudp, unsigned long address)
+{
+	return (pmd_t *)pudp;
+}
+
+#define pmd_offset(pudp, address)					\
+({									\
+	pud_check_dummy(*(pudp));					\
+	__pmd_offset(pudp, address);					\
+})
+
+static __always_inline pmd_t *__pmd_offset_lockless(pud_t *pudp, pud_t pud,
+		unsigned long address)
 {
-	return (pmd_t *)pud;
+	return (pmd_t *)pudp;
 }
-#define pmd_offset pmd_offset
+
+#define pmd_offset_lockless(pudp, pud, address)				\
+({									\
+	pud_check_dummy(*(pudp));					\
+	__pmd_offset_lockless(pudp, pud, address);			\
+})
 
 #define pmd_val(x)				(pud_val((x).pud))
 #define __pmd(x)				((pmd_t) { __pud(x) } )
 
-#define pud_page(pud)				(pmd_page((pmd_t){ pud }))
-#define pud_pgtable(pud)			((pmd_t *)(pmd_page_vaddr((pmd_t){ pud })))
+#define pud_page(pud)				({ BUILD_BUG(); (struct page *)NULL; })
+#define pud_pgtable(pud)						\
+({									\
+	pud_check_dummy(pud);						\
+	((pmd_t *)(pmd_page_vaddr((pmd_t) { pud })));			\
+})
 
 /*
  * allocating and freeing a pmd is trivial: the 1-entry pmd is
diff --git a/include/asm-generic/pgtable-nopud.h b/include/asm-generic/pgtable-nopud.h
index eedee8e3ad68fd..5ee8f1852cd8c9 100644
--- a/include/asm-generic/pgtable-nopud.h
+++ b/include/asm-generic/pgtable-nopud.h
@@ -29,26 +29,62 @@ static inline int p4d_none(p4d_t p4d)		{ return 0; }
 static inline int p4d_bad(p4d_t p4d)		{ return 0; }
 static inline int p4d_present(p4d_t p4d)	{ return 1; }
 static inline void p4d_clear(p4d_t *p4d)	{ }
+static inline bool p4d_leaf(p4d_t p4d)		{ return false; }
+#define p4d_leaf p4d_leaf
+#define pud_ERROR(pud)				(p4d_ERROR((pud).p4d))
 
 #define p4d_populate(mm, p4d, pud)		do { } while (0)
 #define p4d_populate_safe(mm, p4d, pud)		do { } while (0)
-/*
- * (puds are folded into p4ds so this doesn't get actually called,
- * but the define is needed for a generic inline function.)
- */
-#define set_p4d(p4dptr, p4dval)	set_pud((pud_t *)(p4dptr), (pud_t) { p4dval })
 
-static inline pud_t *pud_offset(p4d_t *p4d, unsigned long address)
+#define set_p4d(p4dptr, p4dval)						\
+({									\
+	p4d_check_dummy(p4dval);					\
+	set_pud((pud_t *)(p4dptr), (pud_t) { p4dval });			\
+})
+
+
+static __always_inline p4d_t p4dp_get(p4d_t *p4dp)
+{
+	p4d_t dummy = { 0 };
+
+	return dummy;
+}
+#define p4dp_get p4dp_get
+
+#define p4d_check_dummy(p4d) BUILD_BUG_ON(__builtin_constant_p(p4d_val(p4d)))
+
+static __always_inline pud_t *__pud_offset(p4d_t *p4dp, unsigned long address)
 {
-	return (pud_t *)p4d;
+	return (pud_t *)p4dp;
 }
-#define pud_offset pud_offset
+
+#define pud_offset(p4dp, address)					\
+({									\
+	p4d_check_dummy(*(p4dp));					\
+	__pud_offset(p4dp, address);					\
+})
+
+static __always_inline pud_t *__pud_offset_lockless(p4d_t *p4dp, p4d_t p4d,
+		unsigned long address)
+{
+	return (pud_t *)p4dp;
+}
+
+#define pud_offset_lockless(p4dp, p4d, address)				\
+({									\
+	p4d_check_dummy(*(p4dp));					\
+	__pud_offset_lockless(p4dp, p4d, address);			\
+})
 
 #define pud_val(x)				(p4d_val((x).p4d))
 #define __pud(x)				((pud_t) { __p4d(x) })
 
-#define p4d_page(p4d)				(pud_page((pud_t){ p4d }))
-#define p4d_pgtable(p4d)			((pud_t *)(pud_pgtable((pud_t){ p4d })))
+#define p4d_page(p4d)				({ BUILD_BUG(); (struct page *)NULL; })
+#define p4d_pgtable(p4d)						\
+({									\
+	p4d_check_dummy(p4d);						\
+	((pud_t *)(pud_pgtable((pud_t) { p4d })));			\
+})
 
 /*
  * allocating and freeing a pud is trivial: the 1-entry pud is
diff --git a/include/linux/pgtable.h b/include/linux/pgtable.h
index 780fe849ff8b7e..b0d0e32ad6aef4 100644
--- a/include/linux/pgtable.h
+++ b/include/linux/pgtable.h
@@ -392,6 +392,22 @@ static inline void lazy_mmu_mode_pause(void) {}
 static inline void lazy_mmu_mode_resume(void) {}
 #endif
 
+/*
+ * On some architectures it depends on the mm if the p4d/pud or pmd
+ * layer of the page table hierarchy is folded or not.
+ */
+#ifndef mm_p4d_folded
+#define mm_p4d_folded(mm)	__is_defined(__PAGETABLE_P4D_FOLDED)
+#endif
+
+#ifndef mm_pud_folded
+#define mm_pud_folded(mm)	__is_defined(__PAGETABLE_PUD_FOLDED)
+#endif
+
+#ifndef mm_pmd_folded
+#define mm_pmd_folded(mm)	__is_defined(__PAGETABLE_PMD_FOLDED)
+#endif
+
 #ifndef pte_batch_hint
 /**
  * pte_batch_hint - Number of pages that can be added to batch without scanning.
@@ -2288,11 +2304,11 @@ static inline bool arch_has_pfn_modify_check(void)
 typedef unsigned int pgtbl_mod_mask;
 
 enum pgtable_level {
-	PGTABLE_LEVEL_PTE = 0,
-	PGTABLE_LEVEL_PMD,
-	PGTABLE_LEVEL_PUD,
+	PGTABLE_LEVEL_PGD = 0,
 	PGTABLE_LEVEL_P4D,
-	PGTABLE_LEVEL_PGD,
+	PGTABLE_LEVEL_PUD,
+	PGTABLE_LEVEL_PMD,
+	PGTABLE_LEVEL_PTE,
 };
 
 static inline const char *pgtable_level_to_str(enum pgtable_level level)
@@ -2313,6 +2329,36 @@ static inline const char *pgtable_level_to_str(enum pgtable_level level)
 	}
 }
 
+#ifdef CONFIG_MMU
+/**
+ * mm_first_pgtable_level - return the first non-folded page-table level
+ * @mm: The mm structure.
+ *
+ * With runtime- or compile-time folded page tables, page-table walkers
+ * effectively skip the folded top-level page tables, treating them as
+ * always-present entries pointing to the next lower-level page table.
+ *
+ * Return the first page-table level in @mm that is not folded.
+ *
+ * Return: the first non-folded page-table level.
+ */
+static inline enum pgtable_level mm_first_pgtable_level(struct mm_struct *mm)
+{
+	/*
+	 * "PMD folded" actually means "PMD is folded into PUD", and
+	 * pud_present() etc. are hard-coded to look like present page table
+	 * levels, whereby pmd_present() etc. contain real logic.
+	 */
+	if (mm_pmd_folded(mm))
+		return PGTABLE_LEVEL_PMD;
+	if (mm_pud_folded(mm))
+		return PGTABLE_LEVEL_PUD;
+	if (mm_p4d_folded(mm))
+		return PGTABLE_LEVEL_P4D;
+	return PGTABLE_LEVEL_PGD;
+}
+#endif /* CONFIG_MMU */
+
 void ptval_bytes_to_hex_str(char *buf, size_t buf_size, const void *entry, size_t entry_size);
 
 #define ptval_to_str(buf, val)								\
@@ -2349,21 +2395,6 @@ void ptval_bytes_to_hex_str(char *buf, size_t buf_size, const void *entry, size_
 #ifndef has_transparent_pud_hugepage
 #define has_transparent_pud_hugepage() IS_BUILTIN(CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD)
 #endif
-/*
- * On some architectures it depends on the mm if the p4d/pud or pmd
- * layer of the page table hierarchy is folded or not.
- */
-#ifndef mm_p4d_folded
-#define mm_p4d_folded(mm)	__is_defined(__PAGETABLE_P4D_FOLDED)
-#endif
-
-#ifndef mm_pud_folded
-#define mm_pud_folded(mm)	__is_defined(__PAGETABLE_PUD_FOLDED)
-#endif
-
-#ifndef mm_pmd_folded
-#define mm_pmd_folded(mm)	__is_defined(__PAGETABLE_PMD_FOLDED)
-#endif
 
 #ifndef p4d_offset_lockless
 #define p4d_offset_lockless(pgdp, pgd, address) p4d_offset(&(pgd), address)
diff --git a/mm/vmscan.c b/mm/vmscan.c
index 9959ca158ce835..6a1af9b92349fb 100644
--- a/mm/vmscan.c
+++ b/mm/vmscan.c
@@ -3911,7 +3911,7 @@ static void walk_pmd_range(pud_t *pud, unsigned long start, unsigned long end,
 
 		walk->mm_stats[MM_NONLEAF_FOUND]++;
 
-		if (!walk_pte_range(&val, addr, next, args))
+		if (!walk_pte_range(pmd + i, addr, next, args))
 			continue;
 
 		walk->mm_stats[MM_NONLEAF_ADDED]++;
@@ -3947,7 +3947,7 @@ static int walk_pud_range(p4d_t *p4d, unsigned long start, unsigned long end,
 		if (!pud_present(val) || WARN_ON_ONCE(pud_leaf(val)))
 			continue;
 
-		walk_pmd_range(&val, addr, next, args);
+		walk_pmd_range(pud + i, addr, next, args);
 
 		if (need_resched() || walk->batched >= MAX_LRU_BATCH) {
 			end = (addr | ~PUD_MASK) + 1;

79/3 2026/11/02 22:12 llm kmsan-evaluator
0m Model:
gemini-3.8-flash

Args:
null

Results:
null

Tokens:
							input: 16106
							output: 32
							thoughts: 1469
						

Thoughts:
**KMSAN Fuzzing Session Analysis**

Okay, let's break this down. The task is to determine if this patch series warrants a dedicated KMSAN fuzzing session. The key is to remember that KMSAN is for detecting *uninitialized* memory reads, *not* out-of-bounds access, use-after-free, or other issues KASAN, LOCKDEP, or standard bug detectors would catch. A dedicated KMSAN session is costly, so it's a judgment call. The rules are clear: only if the changes introduce *or* expose uninitialized memory risks *uniquely* detectable by KMSAN.

My mind is running through the criteria: kernel structures to userspace, conditional logic on potentially uninitialized data, allocation of complex structures with uninitialized fields, or bounds checks that expose uninitialized memory. If all stack/heap changes are fully zeroed or initialized, or the patch doesn't introduce uninitialized memory risks, then *no* KMSAN is needed.

Alright, let's dissect the patch. I'm seeing changes across a lot of files, mostly related to page table management and the folding/unfolding of page table levels. There are updates to documentation, type definitions, helper functions, and removal of unused code. A common theme is adjustments to page table traversal functions, like `p4d_offset_lockless`, and changes to `pgtable_level` enumerations.

I'm noting changes in several architecture-specific directories: arm, arm64, loongarch, mips, openrisc, riscv, and x86. The x86 changes are interesting; introducing `direct_gbpages_enabled()` looks like a good change to avoid potential errors. The general modifications seem to be around streamlining and cleaning up the handling of different page table levels and folding logic. The `mm/vmscan.c` patch is interesting; changes to `walk_pmd_range` and `walk_pud_range` involving pointer arithmetic look like potential hotspots.

Wait a second, there's a suspicious modification in `mm/vmscan.c`! The code used to pass `&val`, and now it's passing `pmd + i` and `pud + i`. Why the change? This means I need to zoom in and examine lines 3911 and 3947 in `mm/vmscan.c`. Is there a potential for uninitialized memory to be read? Is this about the iteration in the for loop? I need to understand what `val` represents in the original code, and what `pmd + i` and `pud + i` are now. It is likely a bug fix. If the `val` was not zeroed prior to read, this is a KMSAN issue and needs to be analyzed in detail. If it is being allocated as a proper PMD/PUD pointer with proper initialization, then it is not.

I'll carefully analyze these lines in context, looking for any conditional logic that might depend on the *initial* state of the memory being read. Is the new code properly initializing those pointers before use, or is it possible an uninitialized value is being read and used in logic? Is the passed value is used for address translation, checking, or something else? If I find something that screams "uninitialized memory read," then yes, we *definitely* need a KMSAN session. Otherwise, based on what I see here, it doesn't *seem* like there are direct risks. But I need to confirm the code now.



80/3 2026/11/02 22:12 tool grepper
0m Args:
{
  "Expression": "walk_pte_range",
  "PathPrefixes": [
    "mm/vmscan.c"
  ]
}

Results:
{
  "Output": "mm/vmscan.c=3655=static void walk_update_folio(struct lru_gen_mm_walk *walk, struct vm_area_struct *vma,\n--\nmm/vmscan.c-3680-\nmm/vmscan.c:3681:static bool walk_pte_range(pmd_t *pmd, unsigned long start, unsigned long end,\nmm/vmscan.c-3682-\t\t\t   struct mm_walk *args)\n--\nmm/vmscan.c=3856=static void walk_pmd_range(pud_t *pud, unsigned long start, unsigned long end,\n--\nmm/vmscan.c-3877-restart:\nmm/vmscan.c:3878:\t/* walk_pte_range() may call get_next_vma() */\nmm/vmscan.c-3879-\tvma = args-\u003evma;\n--\nmm/vmscan.c-3913-\nmm/vmscan.c:3914:\t\tif (!walk_pte_range(pmd + i, addr, next, args))\nmm/vmscan.c-3915-\t\t\tcontinue;\n"
}

81/3 2026/11/02 22:12 llm kmsan-evaluator
0m Model:
gemini-3.8-flash

Args:
null

Results:
null

Tokens:
							input: 18661
							output: 35
							thoughts: 24
						

Thoughts:
Okay, let's dive into this. Here's my take on those lines in `mm/vmscan.c`, specifically around the 3870 to 3955 range:

**My Deep Dive into `mm/vmscan.c` (Lines 3870-3955)**

Alright, so I'm honing in on that specific chunk of `mm/vmscan.c`... It's all about how the kernel is handling memory scanning, right? From looking at the code I see we're digging into the core of how the Virtual Memory Manager (VMM) is making decisions about reclaiming pages. We're getting right to the heart of what's happening when memory gets tight and the system needs to shed some load. My guess is that, based on the line range, we're likely in the thick of the "reclaim" process. I bet we're considering the various criteria for selecting victims – perhaps we're looking at things like page age, dirty/clean status, access patterns, and whether the pages are anonymous or file-backed. I'm imagining this section deals with how the kernel selects which pages to kick out to make room for active data, and where it makes those decisions about what needs to happen to these pages. It's likely a critical section because it's where the system balances performance and memory availability. I'm keen to see if we're dealing with direct reclaim (triggered by a process) or kswapd (background reclaim) strategies, because both use different considerations. Hopefully it's well-commented so I can see what optimizations they are using!



82/3 2026/11/02 22:13 tool read-file
0m Args:
{
  "File": "mm/vmscan.c",
  "FirstLine": 3870,
  "LineCount": 90
}

Results:
{
  "Contents": "3870:\t\n3871:\t\t/*\n3872:\t\t * Finish an entire PMD in two passes: the first only reaches to PTE\n3873:\t\t * tables to avoid taking the PMD lock; the second, if necessary, takes\n3874:\t\t * the PMD lock to clear the accessed bit in PMD entries.\n3875:\t\t */\n3876:\t\tpmd = pmd_offset(pud, start \u0026 PUD_MASK);\n3877:\trestart:\n3878:\t\t/* walk_pte_range() may call get_next_vma() */\n3879:\t\tvma = args-\u003evma;\n3880:\t\tfor (i = pmd_index(start), addr = start; addr != end; i++, addr = next) {\n3881:\t\t\tpmd_t val = pmdp_get_lockless(pmd + i);\n3882:\t\n3883:\t\t\tnext = pmd_addr_end(addr, end);\n3884:\t\n3885:\t\t\tif (!pmd_present(val) || is_huge_zero_pmd(val)) {\n3886:\t\t\t\twalk-\u003emm_stats[MM_LEAF_TOTAL]++;\n3887:\t\t\t\tcontinue;\n3888:\t\t\t}\n3889:\t\n3890:\t\t\tif (pmd_trans_huge(val)) {\n3891:\t\t\t\tstruct pglist_data *pgdat = lruvec_pgdat(walk-\u003elruvec);\n3892:\t\t\t\tunsigned long pfn = get_pmd_pfn(val, vma, addr, pgdat);\n3893:\t\n3894:\t\t\t\twalk-\u003emm_stats[MM_LEAF_TOTAL]++;\n3895:\t\n3896:\t\t\t\tif (pfn != -1)\n3897:\t\t\t\t\twalk_pmd_range_locked(pud, addr, vma, args, bitmap, \u0026first);\n3898:\t\t\t\tcontinue;\n3899:\t\t\t}\n3900:\t\n3901:\t\t\tif (!walk-\u003eforce_scan \u0026\u0026 should_clear_pmd_young() \u0026\u0026\n3902:\t\t\t    !mm_has_notifiers(args-\u003emm)) {\n3903:\t\t\t\tif (!pmd_young(val))\n3904:\t\t\t\t\tcontinue;\n3905:\t\n3906:\t\t\t\twalk_pmd_range_locked(pud, addr, vma, args, bitmap, \u0026first);\n3907:\t\t\t}\n3908:\t\n3909:\t\t\tif (!walk-\u003eforce_scan \u0026\u0026 !test_bloom_filter(mm_state, walk-\u003eseq, pmd + i))\n3910:\t\t\t\tcontinue;\n3911:\t\n3912:\t\t\twalk-\u003emm_stats[MM_NONLEAF_FOUND]++;\n3913:\t\n3914:\t\t\tif (!walk_pte_range(pmd + i, addr, next, args))\n3915:\t\t\t\tcontinue;\n3916:\t\n3917:\t\t\twalk-\u003emm_stats[MM_NONLEAF_ADDED]++;\n3918:\t\n3919:\t\t\t/* carry over to the next generation */\n3920:\t\t\tupdate_bloom_filter(mm_state, walk-\u003eseq + 1, pmd + i);\n3921:\t\t}\n3922:\t\n3923:\t\twalk_pmd_range_locked(pud, -1, vma, args, bitmap, \u0026first);\n3924:\t\n3925:\t\tif (i \u003c PTRS_PER_PMD \u0026\u0026 get_next_vma(PUD_MASK, PMD_SIZE, args, \u0026start, \u0026end))\n3926:\t\t\tgoto restart;\n3927:\t}\n3928:\t\n3929:\tstatic int walk_pud_range(p4d_t *p4d, unsigned long start, unsigned long end,\n3930:\t\t\t\t  struct mm_walk *args)\n3931:\t{\n3932:\t\tint i;\n3933:\t\tpud_t *pud;\n3934:\t\tunsigned long addr;\n3935:\t\tunsigned long next;\n3936:\t\tstruct lru_gen_mm_walk *walk = args-\u003eprivate;\n3937:\t\n3938:\t\tVM_WARN_ON_ONCE(p4d_leaf(*p4d));\n3939:\t\n3940:\t\tpud = pud_offset(p4d, start \u0026 P4D_MASK);\n3941:\trestart:\n3942:\t\tfor (i = pud_index(start), addr = start; addr != end; i++, addr = next) {\n3943:\t\t\tpud_t val = pudp_get(pud + i);\n3944:\t\n3945:\t\t\tnext = pud_addr_end(addr, end);\n3946:\t\n3947:\t\t\tif (!pud_present(val) || WARN_ON_ONCE(pud_leaf(val)))\n3948:\t\t\t\tcontinue;\n3949:\t\n3950:\t\t\twalk_pmd_range(pud + i, addr, next, args);\n3951:\t\n3952:\t\t\tif (need_resched() || walk-\u003ebatched \u003e= MAX_LRU_BATCH) {\n3953:\t\t\t\tend = (addr | ~PUD_MASK) + 1;\n3954:\t\t\t\tgoto done;\n3955:\t\t\t}\n3956:\t\t}\n3957:\t\n3958:\t\tif (i \u003c PTRS_PER_PUD \u0026\u0026 get_next_vma(P4D_MASK, PUD_SIZE, args, \u0026start, \u0026end))\n3959:\t\t\tgoto restart;\n"
}

83/3 2026/11/02 22:13 llm kmsan-evaluator
0m Model:
gemini-3.8-flash

Args:
null

Results:
null

Tokens:
							input: 20541
							output: 35
							thoughts: 146
						

Thoughts:
**Analyzing the Potential Issue in mm/vmscan.c**

Okay, let's dive into this. I'm looking at `mm/vmscan.c` and specifically focusing on the `walk_pmd_range()` function. My immediate observation is concerning – before the patch was applied, there's a local variable, `val`, declared on the stack within `walk_pmd_range()`: `pmd_t val = pmdp_get_lockless(pmd + i);`.  The code then proceeds to pass the *address* of this stack-allocated `val` to `walk_pte_range()` in the following line: `if (!walk_pte_range(&val, addr, next, args))`.

That's where the alarm bells start ringing. I need to understand what `walk_pte_range` does with the `pmd` argument (which, in this instance, is the address of `val`). Is it storing that address, dereferencing it after `walk_pmd_range()` exits, or passing it further? This is a textbook example of a potential use-after-free or stack corruption vulnerability if the referenced memory is used after `walk_pmd_range` exits, and that local variable's stack memory becomes available. I need to examine the implementation of `walk_pte_range` *immediately* to determine the fate of that address! Let's read the code and confirm my suspicion or see if there's something mitigating I'm missing.



84/3 2026/11/02 22:13 tool read-file
0m Args:
{
  "File": "mm/vmscan.c",
  "FirstLine": 3681,
  "LineCount": 50
}

Results:
{
  "Contents": "3681:\tstatic bool walk_pte_range(pmd_t *pmd, unsigned long start, unsigned long end,\n3682:\t\t\t\t   struct mm_walk *args)\n3683:\t{\n3684:\t\tint i;\n3685:\t\tbool dirty;\n3686:\t\tpte_t *pte;\n3687:\t\tspinlock_t *ptl;\n3688:\t\tunsigned long addr;\n3689:\t\tint total = 0;\n3690:\t\tint young = 0;\n3691:\t\tstruct folio *last = NULL;\n3692:\t\tstruct lru_gen_mm_walk *walk = args-\u003eprivate;\n3693:\t\tstruct mem_cgroup *memcg = lruvec_memcg(walk-\u003elruvec);\n3694:\t\tstruct pglist_data *pgdat = lruvec_pgdat(walk-\u003elruvec);\n3695:\t\tunsigned int nr;\n3696:\t\tpmd_t pmdval;\n3697:\t\n3698:\t\tpte = pte_offset_map_rw_nolock(args-\u003emm, pmd, start \u0026 PMD_MASK, \u0026pmdval, \u0026ptl);\n3699:\t\tif (!pte)\n3700:\t\t\treturn false;\n3701:\t\n3702:\t\tif (!spin_trylock(ptl)) {\n3703:\t\t\tpte_unmap(pte);\n3704:\t\t\treturn true;\n3705:\t\t}\n3706:\t\n3707:\t\tif (unlikely(!pmd_same(pmdval, pmdp_get_lockless(pmd)))) {\n3708:\t\t\tpte_unmap_unlock(pte, ptl);\n3709:\t\t\treturn false;\n3710:\t\t}\n3711:\t\n3712:\t\tlazy_mmu_mode_enable();\n3713:\trestart:\n3714:\t\tfor (i = pte_index(start), addr = start; addr != end; i += nr, addr += nr * PAGE_SIZE) {\n3715:\t\t\tunsigned long pfn;\n3716:\t\t\tstruct folio *folio;\n3717:\t\t\tpte_t *cur_pte = pte + i;\n3718:\t\t\tpte_t ptent = ptep_get(cur_pte);\n3719:\t\n3720:\t\t\tnr = 1;\n3721:\t\t\ttotal++;\n3722:\t\t\twalk-\u003emm_stats[MM_LEAF_TOTAL]++;\n3723:\t\n3724:\t\t\tpfn = get_pte_pfn(ptent, args-\u003evma, addr, pgdat);\n3725:\t\t\tif (pfn == -1)\n3726:\t\t\t\tcontinue;\n3727:\t\n3728:\t\t\tfolio = get_pfn_folio(pfn, memcg, pgdat);\n3729:\t\t\tif (!folio)\n3730:\t\t\t\tcontinue;\n"
}

85/3 2026/11/02 22:13 llm kmsan-evaluator
0m Model:
gemini-3.8-flash

Args:
null

Results:
null

Tokens:
							input: 21822
							output: 20
							thoughts: 966
						

Thoughts:
**Understanding the Page Table Folding Optimization**

Okay, so I'm looking at this `walk_pte_range` function and it's calling `pte_offset_map_rw_nolock` and `pmdp_get_lockless`.  But then my mind immediately goes to the `pmd` argument. If `pmd` is *itself* a value (like `&val`), and we're using `pmd_offset`... wait a minute, I know where this is going.  Let's see what's happening.

Right, `pmd_offset` in `nopmd.h`... but with *this* patch, it's been redefined with a `pud_check_dummy` call! That's a clever way to catch potential errors. And that `pud_check_dummy` uses `BUILD_BUG_ON(__builtin_constant_p(pud_val(pud)))`. This means we're checking at compile time if the value is a constant. I see what they're doing!

And the `nopgd`, `nopud`, and `nopmd` files, I see what's happening now: they're all defining a "dummy" page table entry. The `pudp_get` function returns a zero-initialized value (e.g. `{ 0 }`). So if someone declares `pud_t val = pudp_get(pud + i);`, `val` *is* a constant zero. Then, if that constant is passed to `walk_pmd_range`, and we call `pmd_offset(pud, ...)` inside of `walk_pmd_range`, the `pud_check_dummy` will fail to compile. The patch *correctly* changes the call to `walk_pmd_range(pud + i, ...)` to *avoid* this compile-time error.

That makes sense, and is the key. The documentation explains this perfectly! They're preventing any code from accidentally using the dummy values generated by the compile-time folding. The compiler will catch usages of the dummy values in set/get methods. Architectures that would operate on dummy values are explicitly prevented by a compile time error. Instead of operating on `pgd`, calls are made on the first page table level such as `p4d`.

This is a series cleaning up page table folding (probably David Hildenbrand's series "mm: clarify and clean up compile-time page table folding" or similar). This whole set of changes is designed to make sure the optimizations are safe and that no code inadvertently tries to manipulate these dummy values.  Now, I'll use `git` to find those commits and dig into this more.



86/3 2026/11/02 22:13 tool grepper
0m Args:
{
  "Expression": "pgd_check_dummy"
}

Results:
{
  "Output": "include/asm-generic/pgtable-nop4d.h=25=static inline bool pgd_leaf(pgd_t pgd)\t\t{ return false; }\n--\ninclude/asm-generic/pgtable-nop4d.h-33-({\t\t\t\t\t\t\t\t\t\\\ninclude/asm-generic/pgtable-nop4d.h:34:\tpgd_check_dummy(pgdval);\t\t\t\t\t\\\ninclude/asm-generic/pgtable-nop4d.h-35-\tset_p4d((p4d_t *)(pgdptr), (p4d_t) { pgdval });\t\t\t\\\n--\ninclude/asm-generic/pgtable-nop4d.h=38=static __always_inline pgd_t pgdp_get(pgd_t *pgdp)\n--\ninclude/asm-generic/pgtable-nop4d.h-45-\ninclude/asm-generic/pgtable-nop4d.h:46:#define pgd_check_dummy(pgd) BUILD_BUG_ON(__builtin_constant_p(pgd_val(pgd)))\ninclude/asm-generic/pgtable-nop4d.h-47-\ninclude/asm-generic/pgtable-nop4d.h=48=static __always_inline p4d_t *__p4d_offset(pgd_t *pgdp, unsigned long address)\n--\ninclude/asm-generic/pgtable-nop4d.h-54-({\t\t\t\t\t\t\t\t\t\\\ninclude/asm-generic/pgtable-nop4d.h:55:\tpgd_check_dummy(*(pgdp));\t\t\t\t\t\\\ninclude/asm-generic/pgtable-nop4d.h-56-\t__p4d_offset(pgdp, address);\t\t\t\t\t\\\n--\ninclude/asm-generic/pgtable-nop4d.h=59=static __always_inline p4d_t *__p4d_offset_lockless(pgd_t *pgdp, pgd_t pgd,\n--\ninclude/asm-generic/pgtable-nop4d.h-66-({\t\t\t\t\t\t\t\t\t\\\ninclude/asm-generic/pgtable-nop4d.h:67:\tpgd_check_dummy(*(pgdp));\t\t\t\t\t\\\ninclude/asm-generic/pgtable-nop4d.h-68-\t__p4d_offset_lockless(pgdp, pgd, address);\t\t\t\\\n--\ninclude/asm-generic/pgtable-nop4d.h-76-({ \t\t\t\t\t\t\t\t\t\\\ninclude/asm-generic/pgtable-nop4d.h:77:\tpgd_check_dummy(pgd);\t\t\t\t\t\t\\\ninclude/asm-generic/pgtable-nop4d.h-78-\t(unsigned long)p4d_pgtable((p4d_t) { pgd });\t\t\t\\\n"
}

87/3 2026/11/02 22:13 llm kmsan-evaluator
0m Model:
gemini-3.8-flash

Args:
null

Results:
null

Tokens:
							input: 24079
							output: 385
							thoughts: 2994
						

Thoughts:
**Patch Series Analysis: NeedsKMSAN Evaluation**

Okay, let's dive into this patch series. My initial impression is that it primarily focuses on cleaning up and rationalizing the page table infrastructure, particularly around the handling of folded page table levels, and making some minor changes to various architecture-specific implementations (ARM, LoongArch, MIPS, OpenRISC, RISC-V, x86). The changes range from documentation updates and header file refactoring to minor modifications in KVM MMU walkers and page table dumping. The primary goal seems to be increased consistency and potentially improved performance by eliminating redundant code and making better use of compile-time checks and helpers.

Now, the critical question: Does this introduce any risk of uninitialized memory usage, potentially requiring KMSAN? Let's go through the checklist methodically.

First, I see that the patch modifies various page table access functions. We're removing some `#define pgdp_get(pgpd)` and other macros, replacing them with generic or folded definitions, and using `_offset_lockless` variants. This seems mostly like code cleanup, replacing stack pointers, and making some small optimizations. The fact that the patch is touching page table access functions and KVM MMU walkers is important because those deal with memory areas. I'm going to carefully review the code surrounding those areas.

Let's examine those areas more closely. I see there are changes in `arch/loongarch/kvm/mmu.c` and `arch/riscv/kvm/mmu.c`, where `p4d_offset_lockless`, `pud_offset_lockless`, and `pmd_offset_lockless` are used. Good. I see that the code explicitly checks whether entries in those page tables are valid, and if not, branches out. I'm also looking for cases where memory areas are allocated or uninitialized structs are introduced. I see that local variables are initialized immediately upon assignment. No memory is being allocated or copied, and there's a strong emphasis on checking the validity of the entries *before* dereferencing them. This is good.

I'm also seeing an evolution in the `arch/riscv/mm/fault.c` code, where there are some changes in how `pgdp`, `p4dp`, `pudp`, and `pmdp` are retrieved and set for different page table levels. I'm checking that the code correctly accesses those pointers and values in that context, ensuring that these are all initialized pointers to valid page table entries. The local pointers and values are assigned from the existing page tables. This implies that these are *not* uninitialized memory areas.

Next, I look for potentially complex data structures. I see that the x86 code introduces `direct_gbpages_enabled()`, which is a helper function that checks some configuration options. I see no memory allocation here. It's a boolean function that returns a value based on those checks. That's a good sign. The `pg_state` struct used in page table dumping is initialized on the stack with a designated initializer (`st = { ... }`), zeroing out unspecified fields. This is perfect.

I have also reviewed the include files (`include/asm-generic/pgtable-nop4d.h`, `nopud.h`, `nopmd.h`) and noted the explicit zero-initialization of the structs involved. This means the dummy struct returns a zeroed value which is good. Furthermore, the use of `BUILD_BUG_ON(__builtin_constant_p(...))` indicates compile-time checks for consistency, which improves reliability.

Looking at `mm/vmscan.c`, I see a small change in `walk_pmd_range` and `walk_pud_range`: They pass `pmd + i` and `pud + i`, which are direct pointers into the *page tables*, instead of stack pointers or the address of local variables. Since the page tables themselves are managed by the kernel memory management, there is no chance of uninitialized memory usage. This is a very safe change.

Now let's apply the KMSAN filter criteria.

*   **Kernel structures sent to user space:** Nope. I see no `copy_to_user`, `put_user`, netlink, ioctl, or BPF usage at all. The page table dumping happens via `seq_file`, with formatted text, and does not expose uninitialized kernel memory.

*   **Conditional logic or branching dependent on uninitialized variables:** No. All variables used in control flow are either initialized directly or loaded from memory areas which are managed as part of the page table infrastructure.

*   **Allocation of complex data structures:** No. Zero-initialized dummy structs are introduced, which is safe. All other structures are either fully initialized or point to existing page tables managed by the


88/3 2026/11/02 22:14 tool set-results
0m Args:
{
  "KMSANReasoning": "The patch series cleans up and refactors compile-time page table folding across architecture-independent MM code and specific architectures (ARM, LoongArch, MIPS, RISC-V, and x86). Key changes include:\n1. Hard-coding dummy getters (pgdp_get, pudp_get, p4dp_get) to return explicitly zero-initialized values ({ 0 }) for compile-time folded page table levels, and adding build-time checks (BUILD_BUG_ON) to prevent folded helpers from operating on dummy values.\n2. In mm/vmscan.c and KVM MMU walkers (LoongArch, RISC-V), replacing references to local stack-allocated page-table value variables with proper page-table pointers (e.g., pmd + i, pud + i) and lockless offset helpers.\n3. Introducing helper functions (direct_gbpages_enabled(), mm_first_pgtable_level()) and updating effective_prot tracking in x86 ptdump.\n\nNone of these changes introduce or expose uninitialized memory risks:\n- All new stack variables and dummy structs are explicitly zero-initialized (e.g., pgd_t dummy = { 0 }).\n- No structures or buffers are copied to user space (no copy_to_user, put_user, netlink skb, or ioctls modified).\n- Page table entries are read from live page tables or set to zeroed dummy values.\n- Any pointer dereferencing or boundary errors in page table walking are within the scope of KASAN (OOB/UAF) rather than KMSAN.\n\nTherefore, a dedicated KMSAN fuzzing session is not warranted.",
  "NeedsKMSAN": false
}

Results:
{
  "KMSANReasoning": "The patch series cleans up and refactors compile-time page table folding across architecture-independent MM code and specific architectures (ARM, LoongArch, MIPS, RISC-V, and x86). Key changes include:\n1. Hard-coding dummy getters (pgdp_get, pudp_get, p4dp_get) to return explicitly zero-initialized values ({ 0 }) for compile-time folded page table levels, and adding build-time checks (BUILD_BUG_ON) to prevent folded helpers from operating on dummy values.\n2. In mm/vmscan.c and KVM MMU walkers (LoongArch, RISC-V), replacing references to local stack-allocated page-table value variables with proper page-table pointers (e.g., pmd + i, pud + i) and lockless offset helpers.\n3. Introducing helper functions (direct_gbpages_enabled(), mm_first_pgtable_level()) and updating effective_prot tracking in x86 ptdump.\n\nNone of these changes introduce or expose uninitialized memory risks:\n- All new stack variables and dummy structs are explicitly zero-initialized (e.g., pgd_t dummy = { 0 }).\n- No structures or buffers are copied to user space (no copy_to_user, put_user, netlink skb, or ioctls modified).\n- Page table entries are read from live page tables or set to zeroed dummy values.\n- Any pointer dereferencing or boundary errors in page table walking are within the scope of KASAN (OOB/UAF) rather than KMSAN.\n\nTherefore, a dedicated KMSAN fuzzing session is not warranted.",
  "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)