AI Trajectory


Trajectory: (Expand/Collapse All)
Seq Timestamp Type Name Duration
0/0 2026/10/01 14:24 flow patch-triage
14m Args:
null

Results:
{
  "EnableConfigs": [
    "HUGETLBFS",
    "HUGETLB_PAGE_OPTIMIZE_VMEMMAP_DEFAULT_ON",
    "SPARSEMEM_VMEMMAP",
    "ZONE_DEVICE",
    "MEMORY_HOTPLUG",
    "MEMORY_HOTREMOVE"
  ],
  "FocusSymbols": [
    "vmemmap_shared_tail_page",
    "hugetlb_vmemmap_optimize_folio",
    "hugetlb_vmemmap_optimize_folios",
    "__populate_section_memmap",
    "sparse_add_section",
    "section_nr_vmemmap_pages"
  ],
  "KMSANReasoning": "The patch series refactors and unifies vmemmap deduplication logic across architectures and subsystems, consolidating HugeTLB Vmemmap Optimization (HVO) and Device-DAX vmemmap optimization under a generic CONFIG_VMEMMAP_OPTIMIZATION framework. It introduces a common vmemmap_shared_tail_page() mechanism for allocating, zeroing (via mm_zero_struct_page), and reusing tail struct pages across sparse memory sections.\n\nKMSAN is specifically designed to detect uninitialized memory reads on the stack/heap/allocations and leaks of uninitialized kernel data to userspace. This patch does not touch any user-space boundary (no copy_to_user, put_user, socket/netlink/ioctl interfaces), nor does it leave any kernel heap or stack structures uninitialized (dynamic allocations are initialized with kzalloc_objs and mm_zero_struct_page).\n\nAny potential bugs in this patch would involve page table manipulations, pfn/section arithmetic, refcounting on struct page, or memory corruption, which are covered by KASAN, DEBUG_VM, and standard kernel debug facilities. A dedicated KMSAN fuzzing session is not warranted.",
  "NeedsKMSAN": false,
  "Reasoning": "The patch modifies core memory management (mm/) logic by unifying HugeTLB Vmemmap Optimization (HVO) and DAX vmemmap deduplication under a common VMEMMAP_OPTIMIZATION framework. It replaces per-zone static tail page arrays with dynamically allocated structures (vmemmap_tails), adds vmemmap_shared_tail_page(), alters page table population with new DAX flags and reference count management, and changes memory section activation, deactivation, and page poisoning behavior. These code paths are fully reachable on amd64 via HugeTLB folio allocation and memory hotplug/DAX workflows.",
  "WorthFuzzing": true
}

1/1 2026/10/01 14:24 action read-patch-diff
0m Args:
null

Results:
{
  "PatchDiff": "commit c5cd987b2e9292f2580f8a5fb953333aa1ec4a3b\nAuthor: syz-cluster \u003ctriage@syzkaller.com\u003e\nDate:   Thu Oct 1 14:24:00 2026 +0000\n\n    syz-cluster: applied patch under review\n\ndiff --git a/Documentation/arch/powerpc/vmemmap_dedup.rst b/Documentation/arch/powerpc/vmemmap_dedup.rst\nindex dc4db59fdf87b..8286acbca9bc5 100644\n--- a/Documentation/arch/powerpc/vmemmap_dedup.rst\n+++ b/Documentation/arch/powerpc/vmemmap_dedup.rst\n@@ -19,82 +19,28 @@ With 1G PUD level mapping, we require 16384 struct pages and a single 64K\n vmemmap page can contain 1024 struct pages (64K/sizeof(struct page)). Hence we\n require 16 64K pages in vmemmap to map the struct page for 1G PUD level mapping.\n \n-Here's how things look like on device-dax after the sections are populated::\n- +-----------+ ---virt_to_page---\u003e +-----------+   mapping to   +-----------+\n- |           |                     |     0     | -------------\u003e |     0     |\n- |           |                     +-----------+                +-----------+\n- |           |                     |     1     | -------------\u003e |     1     |\n- |           |                     +-----------+                +-----------+\n- |           |                     |     2     | ----------------^ ^ ^ ^ ^ ^\n- |           |                     +-----------+                   | | | | |\n- |           |                     |     3     | ------------------+ | | | |\n- |           |                     +-----------+                     | | | |\n- |           |                     |     4     | --------------------+ | | |\n- |    PUD    |                     +-----------+                       | | |\n- |   level   |                     |     .     | ----------------------+ | |\n- |  mapping  |                     +-----------+                         | |\n- |           |                     |     .     | ------------------------+ |\n- |           |                     +-----------+                           |\n- |           |                     |     15    | --------------------------+\n- |           |                     +-----------+\n- |           |\n- |           |\n- |           |\n- +-----------+\n-\n-\n With 4K page size, 2M PMD level mapping requires 512 struct pages and a single\n 4K vmemmap page contains 64 struct pages(4K/sizeof(struct page)). Hence we\n require 8 4K pages in vmemmap to map the struct page for 2M pmd level mapping.\n \n-Here's how things look like on device-dax after the sections are populated::\n-\n- +-----------+ ---virt_to_page---\u003e +-----------+   mapping to   +-----------+\n- |           |                     |     0     | -------------\u003e |     0     |\n- |           |                     +-----------+                +-----------+\n- |           |                     |     1     | -------------\u003e |     1     |\n- |           |                     +-----------+                +-----------+\n- |           |                     |     2     | ----------------^ ^ ^ ^ ^ ^\n- |           |                     +-----------+                   | | | | |\n- |           |                     |     3     | ------------------+ | | | |\n- |           |                     +-----------+                     | | | |\n- |           |                     |     4     | --------------------+ | | |\n- |    PMD    |                     +-----------+                       | | |\n- |   level   |                     |     5     | ----------------------+ | |\n- |  mapping  |                     +-----------+                         | |\n- |           |                     |     6     | ------------------------+ |\n- |           |                     +-----------+                           |\n- |           |                     |     7     | --------------------------+\n- |           |                     +-----------+\n- |           |\n- |           |\n- |           |\n- +-----------+\n-\n-With 1G PUD level mapping, we require 262144 struct pages and a single 4K\n-vmemmap page can contain 64 struct pages (4K/sizeof(struct page)). Hence we\n-require 4096 4K pages in vmemmap to map the struct pages for 1G PUD level\n-mapping.\n-\n-Here's how things look like on device-dax after the sections are populated::\n-\n- +-----------+ ---virt_to_page---\u003e +-----------+   mapping to   +-----------+\n- |           |                     |     0     | -------------\u003e |     0     |\n- |           |                     +-----------+                +-----------+\n- |           |                     |     1     | -------------\u003e |     1     |\n- |           |                     +-----------+                +-----------+\n- |           |                     |     2     | ----------------^ ^ ^ ^ ^ ^\n- |           |                     +-----------+                   | | | | |\n- |           |                     |     3     | ------------------+ | | | |\n- |           |                     +-----------+                     | | | |\n- |           |                     |     4     | --------------------+ | | |\n- |    PUD    |                     +-----------+                       | | |\n- |   level   |                     |     .     | ----------------------+ | |\n- |  mapping  |                     +-----------+                         | |\n- |           |                     |     .     | ------------------------+ |\n- |           |                     +-----------+                           |\n- |           |                     |   4095    | --------------------------+\n- |           |                     +-----------+\n+Here's how things look on device-dax after vmemmap-optimized sections are\n+populated. ``N`` is the number of vmemmap pages required by the DAX mapping\n+above::\n+\n+  Device DAX                  vmemmap pages (N pages)         backing page frames\n+ +-----------+ ---virt_to_page---\u003e +-----------+   mapping to   +-------------+\n+ |           |                     |     0     | -------------\u003e |      0      |\n+ |           |                     +-----------+                +-------------+\n+ |           |                     |     1     | ------+\n+ |           |                     +-----------+       |\n+ |           |                     |     2     | ------+\n+ |           |                     +-----------+       |\n+ |           |                     |     .     | ------+        +-------------+\n+ |  PMD/PUD  |                     +-----------+       |        | A single,   |\n+ |   level   |                     |     .     | ------+------\u003e | per-zone    |\n+ |  mapping  |                     +-----------+       |        | shared tail |\n+ |           |                     |   N - 1   | ------+        | page        |\n+ |           |                     +-----------+                +-------------+\n  |           |\n  |           |\n  |           |\ndiff --git a/Documentation/mm/vmemmap_dedup.rst b/Documentation/mm/vmemmap_dedup.rst\nindex 9fa8642ded483..8c287ae3f86cc 100644\n--- a/Documentation/mm/vmemmap_dedup.rst\n+++ b/Documentation/mm/vmemmap_dedup.rst\n@@ -1,4 +1,3 @@\n-\n .. SPDX-License-Identifier: GPL-2.0\n \n =========================================\n@@ -192,32 +191,7 @@ to 4 on HugeTLB pages.\n \n There's no remapping of vmemmap given that device-dax memory is not part of\n System RAM ranges initialized at boot. Thus the tail page deduplication\n-happens at a later stage when we populate the sections. HugeTLB reuses the\n-the head vmemmap page representing, whereas device-dax reuses the tail\n-vmemmap page. This results in only half of the savings compared to HugeTLB.\n-\n-Deduplicated tail pages are not mapped read-only.\n+happens at a later stage when we populate the sections.\n \n-Here's how things look like on device-dax after the sections are populated::\n-\n- +-----------+ ---virt_to_page---\u003e +-----------+   mapping to   +-----------+\n- |           |                     |     0     | -------------\u003e |     0     |\n- |           |                     +-----------+                +-----------+\n- |           |                     |     1     | -------------\u003e |     1     |\n- |           |                     +-----------+                +-----------+\n- |           |                     |     2     | ----------------^ ^ ^ ^ ^ ^\n- |           |                     +-----------+                   | | | | |\n- |           |                     |     3     | ------------------+ | | | |\n- |           |                     +-----------+                     | | | |\n- |           |                     |     4     | --------------------+ | | |\n- |    PMD    |                     +-----------+                       | | |\n- |   level   |                     |     5     | ----------------------+ | |\n- |  mapping  |                     +-----------+                         | |\n- |           |                     |     6     | ------------------------+ |\n- |           |                     +-----------+                           |\n- |           |                     |     7     | --------------------------+\n- |           |                     +-----------+\n- |           |\n- |           |\n- |           |\n- +-----------+\n+Deduplicated tail pages are not mapped read-only. The mapping layout is the same\n+as HugeTLB.\ndiff --git a/MAINTAINERS b/MAINTAINERS\nindex fe1d70ed5100b..4689a02100600 100644\n--- a/MAINTAINERS\n+++ b/MAINTAINERS\n@@ -12097,6 +12097,7 @@ F:\tDocumentation/mm/hugetlbfs_reserv.rst\n F:\tDocumentation/mm/vmemmap_dedup.rst\n F:\tfs/hugetlbfs/\n F:\tinclude/linux/hugetlb.h\n+F:\tinclude/linux/vmemmap-optimization.h\n F:\tinclude/trace/events/hugetlbfs.h\n F:\tmm/hugetlb.c\n F:\tmm/hugetlb_cgroup.c\ndiff --git a/arch/loongarch/include/asm/pgtable.h b/arch/loongarch/include/asm/pgtable.h\nindex cf29a4c8ac593..f876031351319 100644\n--- a/arch/loongarch/include/asm/pgtable.h\n+++ b/arch/loongarch/include/asm/pgtable.h\n@@ -72,6 +72,7 @@\n \n #include \u003clinux/mm_types.h\u003e\n #include \u003clinux/mmzone.h\u003e\n+#include \u003clinux/vmemmap-optimization.h\u003e\n #include \u003casm/fixmap.h\u003e\n #include \u003casm/sparsemem.h\u003e\n \ndiff --git a/arch/powerpc/mm/book3s64/radix_pgtable.c b/arch/powerpc/mm/book3s64/radix_pgtable.c\nindex cf692b2b5f7bc..9ca28e4a610a2 100644\n--- a/arch/powerpc/mm/book3s64/radix_pgtable.c\n+++ b/arch/powerpc/mm/book3s64/radix_pgtable.c\n@@ -19,6 +19,7 @@\n #include \u003clinux/string_helpers.h\u003e\n #include \u003clinux/memory.h\u003e\n #include \u003clinux/kfence.h\u003e\n+#include \u003clinux/vmemmap-optimization.h\u003e\n \n #include \u003casm/pgalloc.h\u003e\n #include \u003casm/mmu_context.h\u003e\n@@ -1217,92 +1218,6 @@ int __meminit radix__vmemmap_populate(unsigned long start, unsigned long end, in\n \treturn 0;\n }\n \n-static pte_t * __meminit radix__vmemmap_populate_address(unsigned long addr, int node,\n-\t\t\t\t\t\t\t struct vmem_altmap *altmap,\n-\t\t\t\t\t\t\t struct page *reuse)\n-{\n-\tpgd_t *pgd;\n-\tp4d_t *p4d;\n-\tpud_t *pud;\n-\tpmd_t *pmd;\n-\tpte_t *pte;\n-\n-\tpgd = pgd_offset_k(addr);\n-\tp4d = p4d_offset(pgd, addr);\n-\tpud = vmemmap_pud_alloc(p4d, node, addr);\n-\tif (!pud)\n-\t\treturn NULL;\n-\tpmd = vmemmap_pmd_alloc(pud, node, addr);\n-\tif (!pmd)\n-\t\treturn NULL;\n-\tif (pmd_leaf(*pmd))\n-\t\t/*\n-\t\t * The second page is mapped as a hugepage due to a nearby request.\n-\t\t * Force our mapping to page size without deduplication\n-\t\t */\n-\t\treturn NULL;\n-\tpte = vmemmap_pte_alloc(pmd, node, addr);\n-\tif (!pte)\n-\t\treturn NULL;\n-\tradix__vmemmap_pte_populate(pmd, addr, node, NULL, NULL);\n-\tvmemmap_verify(pte, node, addr, addr + PAGE_SIZE);\n-\n-\treturn pte;\n-}\n-\n-static pte_t * __meminit vmemmap_compound_tail_page(unsigned long addr,\n-\t\t\t\t\t\t    unsigned long pfn_offset, int node)\n-{\n-\tpgd_t *pgd;\n-\tp4d_t *p4d;\n-\tpud_t *pud;\n-\tpmd_t *pmd;\n-\tpte_t *pte;\n-\tunsigned long map_addr;\n-\n-\t/* the second vmemmap page which we use for duplication */\n-\tmap_addr = addr - pfn_offset * sizeof(struct page) + PAGE_SIZE;\n-\tpgd = pgd_offset_k(map_addr);\n-\tp4d = p4d_offset(pgd, map_addr);\n-\tpud = vmemmap_pud_alloc(p4d, node, map_addr);\n-\tif (!pud)\n-\t\treturn NULL;\n-\tpmd = vmemmap_pmd_alloc(pud, node, map_addr);\n-\tif (!pmd)\n-\t\treturn NULL;\n-\tif (pmd_leaf(*pmd))\n-\t\t/*\n-\t\t * The second page is mapped as a hugepage due to a nearby request.\n-\t\t * Force our mapping to page size without deduplication\n-\t\t */\n-\t\treturn NULL;\n-\tpte = vmemmap_pte_alloc(pmd, node, map_addr);\n-\tif (!pte)\n-\t\treturn NULL;\n-\t/*\n-\t * Check if there exist a mapping to the left\n-\t */\n-\tif (pte_none(*pte)) {\n-\t\t/*\n-\t\t * Populate the head page vmemmap page.\n-\t\t * It can fall in different pmd, hence\n-\t\t * vmemmap_populate_address()\n-\t\t */\n-\t\tpte = radix__vmemmap_populate_address(map_addr - PAGE_SIZE, node, NULL, NULL);\n-\t\tif (!pte)\n-\t\t\treturn NULL;\n-\t\t/*\n-\t\t * Populate the tail pages vmemmap page\n-\t\t */\n-\t\tpte = radix__vmemmap_pte_populate(pmd, map_addr, node, NULL, NULL);\n-\t\tif (!pte)\n-\t\t\treturn NULL;\n-\t\tvmemmap_verify(pte, node, map_addr, map_addr + PAGE_SIZE);\n-\t\treturn pte;\n-\t}\n-\treturn pte;\n-}\n-\n int __meminit vmemmap_populate_compound_pages(unsigned long start_pfn,\n \t\t\t\t\t      unsigned long start,\n \t\t\t\t\t      unsigned long end, int node,\n@@ -1320,6 +1235,12 @@ int __meminit vmemmap_populate_compound_pages(unsigned long start_pfn,\n \tpud_t *pud;\n \tpmd_t *pmd;\n \tpte_t *pte;\n+\tstruct page *tail_page;\n+\tunsigned int order = pfn_to_section_compound_order(start_pfn);\n+\n+\ttail_page = vmemmap_shared_tail_page(order, device_zone(node));\n+\tif (!tail_page)\n+\t\treturn -ENOMEM;\n \n \tfor (addr = start; addr \u003c end; addr = next) {\n \n@@ -1343,16 +1264,15 @@ int __meminit vmemmap_populate_compound_pages(unsigned long start_pfn,\n \t\tif (!pte_none(*pte)) {\n \t\t\t/*\n \t\t\t * This could be because we already have a compound\n-\t\t\t * page whose VMEMMAP_RESERVE_NR pages were mapped and\n+\t\t\t * page whose retained vmemmap page was mapped and\n \t\t\t * this request fall in those pages.\n \t\t\t */\n \t\t\tnext = addr + PAGE_SIZE;\n \t\t\tcontinue;\n \t\t} else {\n-\t\t\tunsigned long nr_pages = pgmap_vmemmap_nr(pgmap);\n+\t\t\tunsigned long nr_pages = 1UL \u003c\u003c order;\n \t\t\tunsigned long addr_pfn = page_to_pfn((struct page *)addr);\n \t\t\tunsigned long pfn_offset = addr_pfn - ALIGN_DOWN(addr_pfn, nr_pages);\n-\t\t\tpte_t *tail_page_pte;\n \n \t\t\t/*\n \t\t\t * if the address is aligned to huge page size it is the\n@@ -1365,35 +1285,11 @@ int __meminit vmemmap_populate_compound_pages(unsigned long start_pfn,\n \t\t\t\t\treturn -ENOMEM;\n \t\t\t\tvmemmap_verify(pte, node, addr, addr + PAGE_SIZE);\n \n-\t\t\t\t/*\n-\t\t\t\t * Populate the tail pages vmemmap page\n-\t\t\t\t * It can fall in different pmd, hence\n-\t\t\t\t * vmemmap_populate_address()\n-\t\t\t\t */\n-\t\t\t\tpte = radix__vmemmap_populate_address(addr + PAGE_SIZE, node, NULL, NULL);\n-\t\t\t\tif (!pte)\n-\t\t\t\t\treturn -ENOMEM;\n-\n-\t\t\t\tnext = addr + 2 * PAGE_SIZE;\n-\t\t\t\tcontinue;\n-\t\t\t}\n-\t\t\t/*\n-\t\t\t * get the 2nd mapping details\n-\t\t\t * Also create it if that doesn't exist\n-\t\t\t */\n-\t\t\ttail_page_pte = vmemmap_compound_tail_page(addr, pfn_offset, node);\n-\t\t\tif (!tail_page_pte) {\n-\n-\t\t\t\tpte = radix__vmemmap_pte_populate(pmd, addr, node, NULL, NULL);\n-\t\t\t\tif (!pte)\n-\t\t\t\t\treturn -ENOMEM;\n-\t\t\t\tvmemmap_verify(pte, node, addr, addr + PAGE_SIZE);\n-\n \t\t\t\tnext = addr + PAGE_SIZE;\n \t\t\t\tcontinue;\n \t\t\t}\n \n-\t\t\tpte = radix__vmemmap_pte_populate(pmd, addr, node, NULL, pte_page(*tail_page_pte));\n+\t\t\tpte = radix__vmemmap_pte_populate(pmd, addr, node, NULL, tail_page);\n \t\t\tif (!pte)\n \t\t\t\treturn -ENOMEM;\n \t\t\tvmemmap_verify(pte, node, addr, addr + PAGE_SIZE);\ndiff --git a/arch/riscv/mm/init.c b/arch/riscv/mm/init.c\nindex fb37b0b67efec..857f9a55039ce 100644\n--- a/arch/riscv/mm/init.c\n+++ b/arch/riscv/mm/init.c\n@@ -22,6 +22,7 @@\n #include \u003clinux/hugetlb.h\u003e\n #include \u003clinux/kfence.h\u003e\n #include \u003clinux/execmem.h\u003e\n+#include \u003clinux/vmemmap-optimization.h\u003e\n \n #include \u003casm/alternative.h\u003e\n #include \u003casm/fixmap.h\u003e\ndiff --git a/arch/x86/entry/vdso/vdso32/fake_32bit_build.h b/arch/x86/entry/vdso/vdso32/fake_32bit_build.h\nindex bc3e549795c3f..72a92cb9b53d3 100644\n--- a/arch/x86/entry/vdso/vdso32/fake_32bit_build.h\n+++ b/arch/x86/entry/vdso/vdso32/fake_32bit_build.h\n@@ -11,7 +11,7 @@\n #undef CONFIG_PGTABLE_LEVELS\n #undef CONFIG_ILLEGAL_POINTER_VALUE\n #undef CONFIG_SPARSEMEM_VMEMMAP\n-#undef CONFIG_HUGETLB_PAGE_OPTIMIZE_VMEMMAP\n+#undef CONFIG_VMEMMAP_OPTIMIZATION\n #undef CONFIG_NR_CPUS\n #undef CONFIG_PARAVIRT_XXL\n \ndiff --git a/fs/Kconfig b/fs/Kconfig\nindex d1c210c6508f0..1454b7fe9641f 100644\n--- a/fs/Kconfig\n+++ b/fs/Kconfig\n@@ -278,6 +278,7 @@ config HUGETLB_PAGE_OPTIMIZE_VMEMMAP\n \tdef_bool HUGETLB_PAGE\n \tdepends on ARCH_WANT_OPTIMIZE_HUGETLB_VMEMMAP\n \tdepends on SPARSEMEM_VMEMMAP\n+\tselect VMEMMAP_OPTIMIZATION\n \n config HUGETLB_PMD_PAGE_TABLE_SHARING\n \tdef_bool HUGETLB_PAGE\ndiff --git a/include/linux/mm.h b/include/linux/mm.h\nindex c49ef99b4413b..30a3365bca827 100644\n--- a/include/linux/mm.h\n+++ b/include/linux/mm.h\n@@ -38,6 +38,7 @@\n #include \u003clinux/bitops.h\u003e\n #include \u003clinux/iommu-debug-pagealloc.h\u003e\n #include \u003clinux/kcsan-checks.h\u003e\n+#include \u003clinux/vmemmap-optimization.h\u003e\n \n struct mempolicy;\n struct anon_vma;\n@@ -5167,7 +5168,6 @@ static inline void vmem_altmap_free(struct vmem_altmap *altmap,\n }\n #endif\n \n-#define VMEMMAP_RESERVE_NR\t2\n #ifdef CONFIG_ARCH_WANT_OPTIMIZE_DAX_VMEMMAP\n static inline bool __vmemmap_can_optimize(struct vmem_altmap *altmap,\n \t\t\t\t\t  struct dev_pagemap *pgmap)\n@@ -5175,6 +5175,9 @@ static inline bool __vmemmap_can_optimize(struct vmem_altmap *altmap,\n \tunsigned long nr_pages;\n \tunsigned long nr_vmemmap_pages;\n \n+\tif (!IS_ENABLED(CONFIG_VMEMMAP_OPTIMIZATION))\n+\t\treturn false;\n+\n \tif (!pgmap || !is_power_of_2(sizeof(struct page)))\n \t\treturn false;\n \n@@ -5184,7 +5187,7 @@ static inline bool __vmemmap_can_optimize(struct vmem_altmap *altmap,\n \t * For vmemmap optimization with DAX we need minimum 2 vmemmap\n \t * pages. See layout diagram in Documentation/mm/vmemmap_dedup.rst\n \t */\n-\treturn !altmap \u0026\u0026 (nr_vmemmap_pages \u003e VMEMMAP_RESERVE_NR);\n+\treturn !altmap \u0026\u0026 (nr_vmemmap_pages \u003e VMEMMAP_OPTIMIZATION_PAGES);\n }\n /*\n  * If we don't have an architecture override, use the generic rule\ndiff --git a/include/linux/mmzone.h b/include/linux/mmzone.h\nindex acd94cecc0d39..65de3bb13eb3a 100644\n--- a/include/linux/mmzone.h\n+++ b/include/linux/mmzone.h\n@@ -96,28 +96,6 @@\n \n #define MAX_FOLIO_NR_PAGES\t(1UL \u003c\u003c MAX_FOLIO_ORDER)\n \n-/*\n- * HugeTLB Vmemmap Optimization (HVO) requires struct pages of the head page to\n- * be naturally aligned with regard to the folio size.\n- *\n- * HVO which is only active if the size of struct page is a power of 2.\n- */\n-#define MAX_FOLIO_VMEMMAP_ALIGN \\\n-\t(IS_ENABLED(CONFIG_HUGETLB_PAGE_OPTIMIZE_VMEMMAP) \u0026\u0026 \\\n-\t is_power_of_2(sizeof(struct page)) ? \\\n-\t MAX_FOLIO_NR_PAGES * sizeof(struct page) : 0)\n-\n-/* The number of retained vmemmap pages with HVO enabled. */\n-#define VMEMMAP_OPTIMIZATION_PAGES\t\t1\n-#define VMEMMAP_OPTIMIZATION_NR_STRUCT_PAGES\t\\\n-\t(VMEMMAP_OPTIMIZATION_PAGES * PAGE_SIZE / sizeof(struct page))\n-#define VMEMMAP_OPTIMIZATION_MIN_ORDER\t\t(ilog2(VMEMMAP_OPTIMIZATION_NR_STRUCT_PAGES) + 1)\n-\n-#define __VMEMMAP_OPTIMIZATION_NR_ORDERS\t\\\n-\t(MAX_FOLIO_ORDER - VMEMMAP_OPTIMIZATION_MIN_ORDER + 1)\n-#define VMEMMAP_OPTIMIZATION_NR_ORDERS\t\t\\\n-\t(__VMEMMAP_OPTIMIZATION_NR_ORDERS \u003e 0 ? __VMEMMAP_OPTIMIZATION_NR_ORDERS : 0)\n-\n enum migratetype {\n \tMIGRATE_UNMOVABLE,\n \tMIGRATE_MOVABLE,\n@@ -1155,8 +1133,8 @@ struct zone {\n \t/* Zone statistics */\n \tatomic_long_t\t\tvm_stat[NR_VM_ZONE_STAT_ITEMS];\n \tatomic_long_t\t\tvm_numa_event[NR_VM_NUMA_EVENT_ITEMS];\n-#ifdef CONFIG_HUGETLB_PAGE_OPTIMIZE_VMEMMAP\n-\tstruct page *vmemmap_tails[VMEMMAP_OPTIMIZATION_NR_ORDERS];\n+#ifdef CONFIG_VMEMMAP_OPTIMIZATION\n+\tstruct page **vmemmap_tails;\n #endif\n } ____cacheline_internodealigned_in_smp;\n \n@@ -2019,7 +1997,7 @@ struct mem_section {\n \tunsigned long section_mem_map;\n \n \tstruct mem_section_usage *usage;\n-#ifdef CONFIG_HUGETLB_PAGE_OPTIMIZE_VMEMMAP\n+#ifdef CONFIG_VMEMMAP_OPTIMIZATION\n \t/*\n \t * Normally, sections hold regular (order-0) pages. However, for\n \t * sections with HVO enabled, this tracks the compound page order\n@@ -2148,11 +2126,21 @@ static inline int online_device_section(const struct mem_section *section)\n \n \treturn section \u0026\u0026 ((section-\u003esection_mem_map \u0026 flags) == flags);\n }\n+\n+static inline struct zone *device_zone(int nid)\n+{\n+\treturn \u0026NODE_DATA(nid)-\u003enode_zones[ZONE_DEVICE];\n+}\n #else\n static inline int online_device_section(const struct mem_section *section)\n {\n \treturn 0;\n }\n+\n+static inline struct zone *device_zone(int nid)\n+{\n+\treturn NULL;\n+}\n #endif\n \n static inline int online_section_nr(unsigned long nr)\ndiff --git a/include/linux/page-flags.h b/include/linux/page-flags.h\nindex 86dd0470da117..7080a6a1a79e7 100644\n--- a/include/linux/page-flags.h\n+++ b/include/linux/page-flags.h\n@@ -208,14 +208,13 @@ enum pageflags {\n static __always_inline bool compound_info_has_mask(void)\n {\n \t/*\n-\t * Limit mask usage to HugeTLB vmemmap optimization (HVO) where it\n-\t * makes a difference.\n+\t * Limit mask usage to HVO where it makes a difference.\n \t *\n \t * The approach with mask would work in the wider set of conditions,\n \t * but it requires validating that struct pages are naturally aligned\n \t * for all orders up to the MAX_FOLIO_ORDER, which can be tricky.\n \t */\n-\tif (!IS_ENABLED(CONFIG_HUGETLB_PAGE_OPTIMIZE_VMEMMAP))\n+\tif (!IS_ENABLED(CONFIG_VMEMMAP_OPTIMIZATION))\n \t\treturn false;\n \n \treturn is_power_of_2(sizeof(struct page));\ndiff --git a/include/linux/vmemmap-optimization.h b/include/linux/vmemmap-optimization.h\nnew file mode 100644\nindex 0000000000000..fa9e9abd66560\n--- /dev/null\n+++ b/include/linux/vmemmap-optimization.h\n@@ -0,0 +1,115 @@\n+/* SPDX-License-Identifier: GPL-2.0-or-later */\n+/*\n+ * vmemmap-optimization.h\n+ *\n+ * Generic vmemmap optimization declarations.\n+ *\n+ * Author: Muchun Song \u003csongmuchun@bytedance.com\u003e\n+ */\n+#ifndef _LINUX_VMEMMAP_OPTIMIZATION_H\n+#define _LINUX_VMEMMAP_OPTIMIZATION_H\n+\n+#include \u003clinux/align.h\u003e\n+#include \u003clinux/log2.h\u003e\n+#include \u003clinux/mmdebug.h\u003e\n+#include \u003clinux/mmzone.h\u003e\n+\n+/*\n+ * HugeTLB Vmemmap Optimization (HVO) requires struct pages of the head page to\n+ * be naturally aligned with regard to the folio size.\n+ *\n+ * HVO which is only active if the size of struct page is a power of 2.\n+ */\n+#define MAX_FOLIO_VMEMMAP_ALIGN\t\t\t\t\t\\\n+\t(IS_ENABLED(CONFIG_VMEMMAP_OPTIMIZATION) \u0026\u0026\t\t\\\n+\t is_power_of_2(sizeof(struct page)) ?\t\t\t\\\n+\t MAX_FOLIO_NR_PAGES * sizeof(struct page) : 0)\n+\n+/* The number of retained vmemmap pages with HVO enabled. */\n+#define VMEMMAP_OPTIMIZATION_PAGES\t\t1\n+#define VMEMMAP_OPTIMIZATION_NR_STRUCT_PAGES\t\\\n+\t(VMEMMAP_OPTIMIZATION_PAGES * PAGE_SIZE / sizeof(struct page))\n+#define VMEMMAP_OPTIMIZATION_MIN_ORDER\t\t(ilog2(VMEMMAP_OPTIMIZATION_NR_STRUCT_PAGES) + 1)\n+\n+#ifdef CONFIG_VMEMMAP_OPTIMIZATION\n+static inline unsigned int section_compound_order(const struct mem_section *section)\n+{\n+\treturn section-\u003ecompound_page_order;\n+}\n+\n+static inline void section_set_compound_order(struct mem_section *section,\n+\t\t\t\t\t      unsigned int order)\n+{\n+\tVM_WARN_ON(section_compound_order(section) \u0026\u0026 order \u0026\u0026\n+\t\t   section_compound_order(section) != order);\n+\tsection-\u003ecompound_page_order = order;\n+}\n+\n+static inline void section_set_compound_order_range(unsigned long pfn,\n+\t\tunsigned long nr_pages, unsigned int order)\n+{\n+\tunsigned long section_nr = pfn_to_section_nr(pfn);\n+\n+\tif (!IS_ALIGNED(pfn | nr_pages, PAGES_PER_SECTION))\n+\t\treturn;\n+\n+\tfor (unsigned long i = 0; i \u003c nr_pages / PAGES_PER_SECTION; i++)\n+\t\tsection_set_compound_order(__nr_to_section(section_nr + i), order);\n+}\n+\n+static inline unsigned int pfn_to_section_compound_order(unsigned long pfn)\n+{\n+\treturn section_compound_order(__pfn_to_section(pfn));\n+}\n+\n+struct page *vmemmap_shared_tail_page(unsigned int order, struct zone *zone);\n+#else\n+static inline unsigned int section_compound_order(const struct mem_section *section)\n+{\n+\treturn 0;\n+}\n+\n+static inline void section_set_compound_order(struct mem_section *section,\n+\t\t\t\t\t      unsigned int order)\n+{\n+}\n+\n+static inline void section_set_compound_order_range(unsigned long pfn,\n+\t\tunsigned long nr_pages, unsigned int order)\n+{\n+}\n+\n+static inline unsigned int pfn_to_section_compound_order(unsigned long pfn)\n+{\n+\treturn 0;\n+}\n+\n+static inline struct page *vmemmap_shared_tail_page(unsigned int order,\n+\t\t\t\t\t\t    struct zone *zone)\n+{\n+\treturn NULL;\n+}\n+#endif /* CONFIG_VMEMMAP_OPTIMIZATION */\n+\n+static inline bool vmemmap_optimizable_pfn(unsigned long pfn)\n+{\n+\tconst unsigned int order = pfn_to_section_compound_order(pfn);\n+\tconst unsigned long nr_pages = 1UL \u003c\u003c order;\n+\n+\tif (!is_power_of_2(sizeof(struct page)))\n+\t\treturn false;\n+\n+\treturn (pfn \u0026 (nr_pages - 1)) \u003e= VMEMMAP_OPTIMIZATION_NR_STRUCT_PAGES;\n+}\n+\n+static inline bool vmemmap_optimizable_order(unsigned int order)\n+{\n+\tif (!IS_ENABLED(CONFIG_VMEMMAP_OPTIMIZATION))\n+\t\treturn false;\n+\n+\tif (!is_power_of_2(sizeof(struct page)))\n+\t\treturn false;\n+\n+\treturn order \u003e= VMEMMAP_OPTIMIZATION_MIN_ORDER;\n+}\n+#endif /* _LINUX_VMEMMAP_OPTIMIZATION_H */\ndiff --git a/mm/Kconfig b/mm/Kconfig\nindex bc7befafb47b5..30170a936f1fc 100644\n--- a/mm/Kconfig\n+++ b/mm/Kconfig\n@@ -461,6 +461,10 @@ config SPARSEMEM_VMEMMAP\n \t  pfn_to_page and page_to_pfn operations.  This is the most\n \t  efficient option when sufficient kernel resources are available.\n \n+config VMEMMAP_OPTIMIZATION\n+\tbool\n+\tdepends on SPARSEMEM_VMEMMAP\n+\n #\n # Select this config option from the architecture Kconfig, if it is preferred\n # to enable the feature of HugeTLB/dev_dax vmemmap optimization.\n@@ -1220,6 +1224,7 @@ config ZONE_DMA32\n config ZONE_DEVICE\n \tbool \"Device memory (pmem, HMM, etc...) hotplug support\"\n \tdepends on MEMORY_HOTREMOVE\n+\tselect VMEMMAP_OPTIMIZATION if ARCH_WANT_OPTIMIZE_DAX_VMEMMAP\n \tselect XARRAY_MULTI\n \n \thelp\ndiff --git a/mm/hugetlb.c b/mm/hugetlb.c\nindex fd00141b089a9..2003439ea13c6 100644\n--- a/mm/hugetlb.c\n+++ b/mm/hugetlb.c\n@@ -38,6 +38,7 @@\n #include \u003clinux/mm_inline.h\u003e\n #include \u003clinux/padata.h\u003e\n #include \u003clinux/pgalloc.h\u003e\n+#include \u003clinux/vmemmap-optimization.h\u003e\n \n #include \u003casm/page.h\u003e\n #include \u003casm/tlb.h\u003e\n@@ -52,7 +53,6 @@\n #include \"hugetlb_cma.h\"\n #include \"hugetlb_internal.h\"\n #include \"mm_init.h\"\n-#include \"sparse.h\"\n #include \u003clinux/page-isolation.h\u003e\n \n #define HUGE_BOOTMEM_ZONES_VALID\tBIT(0)\ndiff --git a/mm/hugetlb_vmemmap.c b/mm/hugetlb_vmemmap.c\nindex f977d0a7e0027..0057fa2a16a89 100644\n--- a/mm/hugetlb_vmemmap.c\n+++ b/mm/hugetlb_vmemmap.c\n@@ -15,11 +15,10 @@\n #include \u003clinux/mmdebug.h\u003e\n #include \u003clinux/pagewalk.h\u003e\n #include \u003clinux/pgalloc.h\u003e\n+#include \u003clinux/vmemmap-optimization.h\u003e\n \n #include \u003casm/tlbflush.h\u003e\n #include \"hugetlb_vmemmap.h\"\n-#include \"sparse.h\"\n-#include \"internal.h\"\n \n /**\n  * struct vmemmap_remap_walk - walk vmemmap page table\n@@ -493,32 +492,6 @@ static bool vmemmap_should_optimize_folio(const struct hstate *h, struct folio *\n \treturn true;\n }\n \n-static struct page *vmemmap_get_tail(unsigned int order, struct zone *zone)\n-{\n-\tconst unsigned int idx = order - VMEMMAP_OPTIMIZATION_MIN_ORDER;\n-\tstruct page *tail, *p;\n-\tint node = zone_to_nid(zone);\n-\n-\ttail = READ_ONCE(zone-\u003evmemmap_tails[idx]);\n-\tif (likely(tail))\n-\t\treturn tail;\n-\n-\ttail = alloc_pages_node(node, GFP_KERNEL | __GFP_ZERO, 0);\n-\tif (!tail)\n-\t\treturn NULL;\n-\n-\tp = page_to_virt(tail);\n-\tfor (int i = 0; i \u003c PAGE_SIZE / sizeof(struct page); i++)\n-\t\tinit_compound_tail(p + i, NULL, order, zone);\n-\n-\tif (cmpxchg(\u0026zone-\u003evmemmap_tails[idx], NULL, tail)) {\n-\t\t__free_page(tail);\n-\t\ttail = READ_ONCE(zone-\u003evmemmap_tails[idx]);\n-\t}\n-\n-\treturn tail;\n-}\n-\n static int __hugetlb_vmemmap_optimize_folio(const struct hstate *h,\n \t\t\t\t\t    struct folio *folio,\n \t\t\t\t\t    struct list_head *vmemmap_pages,\n@@ -535,7 +508,7 @@ static int __hugetlb_vmemmap_optimize_folio(const struct hstate *h,\n \t\treturn ret;\n \n \tnid = folio_nid(folio);\n-\tvmemmap_tail = vmemmap_get_tail(h-\u003eorder, folio_zone(folio));\n+\tvmemmap_tail = vmemmap_shared_tail_page(h-\u003eorder, folio_zone(folio));\n \tif (!vmemmap_tail)\n \t\treturn -ENOMEM;\n \ndiff --git a/mm/internal.h b/mm/internal.h\nindex da14c56fb24e1..0dca33db068f6 100644\n--- a/mm/internal.h\n+++ b/mm/internal.h\n@@ -786,15 +786,6 @@ static inline void prep_compound_tail(struct page *tail,\n \tVM_WARN_ON_ONCE(tail-\u003eprivate);\n }\n \n-static inline void init_compound_tail(struct page *tail,\n-\t\tconst struct page *head, unsigned int order, struct zone *zone)\n-{\n-\tatomic_set(\u0026tail-\u003e_mapcount, -1);\n-\tset_page_node(tail, zone_to_nid(zone));\n-\tset_page_zone(tail, zone_idx(zone));\n-\tprep_compound_tail(tail, head, order);\n-}\n-\n #if defined CONFIG_COMPACTION || defined CONFIG_CMA\n \n /*\ndiff --git a/mm/memory_hotplug.c b/mm/memory_hotplug.c\nindex b428da66d279c..d7a59167bec43 100644\n--- a/mm/memory_hotplug.c\n+++ b/mm/memory_hotplug.c\n@@ -43,6 +43,7 @@\n #include \"mm_init.h\"\n #include \"page_alloc.h\"\n #include \"shuffle.h\"\n+#include \"sparse.h\"\n \n enum {\n \tMEMMAP_ON_MEMORY_DISABLE = 0,\n@@ -554,8 +555,9 @@ void remove_pfn_range_from_zone(struct zone *zone,\n \t\t/* Select all remaining pages up to the next section boundary */\n \t\tcur_nr_pages =\n \t\t\tmin(end_pfn - pfn, SECTION_ALIGN_UP(pfn + 1) - pfn);\n-\t\tpage_init_poison(pfn_to_page(pfn),\n-\t\t\t\t sizeof(struct page) * cur_nr_pages);\n+\t\tif (!section_vmemmap_optimizable(__pfn_to_section(pfn)))\n+\t\t\tpage_init_poison(pfn_to_page(pfn),\n+\t\t\t\t\t sizeof(struct page) * cur_nr_pages);\n \t}\n \n \t/*\ndiff --git a/mm/mm_init.c b/mm/mm_init.c\nindex 97e0158d2aca5..56bb4567a4940 100644\n--- a/mm/mm_init.c\n+++ b/mm/mm_init.c\n@@ -1049,19 +1049,14 @@ static void zone_device_page_init_from_template(struct page *page,\n  * of an altmap. See vmemmap_populate_compound_pages().\n  */\n static inline unsigned long compound_nr_pages(unsigned long pfn,\n-\t\t\t\t\t      struct vmem_altmap *altmap,\n \t\t\t\t\t      struct dev_pagemap *pgmap)\n {\n-\t/*\n-\t * If DAX memory is hot-plugged into an unoccupied subsection\n-\t * of an early section, the unoptimized boot memmap is reused.\n-\t * See section_activate().\n-\t */\n-\tif (early_section(__pfn_to_section(pfn)) ||\n-\t    !vmemmap_can_optimize(altmap, pgmap))\n+\tconst struct mem_section *ms = __pfn_to_section(pfn);\n+\n+\tif (!section_vmemmap_optimizable(ms))\n \t\treturn pgmap_vmemmap_nr(pgmap);\n \n-\treturn VMEMMAP_RESERVE_NR * (PAGE_SIZE / sizeof(struct page));\n+\treturn VMEMMAP_OPTIMIZATION_PAGES * (PAGE_SIZE / sizeof(struct page));\n }\n \n static void __ref memmap_init_compound(struct page *head,\n@@ -1144,7 +1139,7 @@ void __ref memmap_init_zone_device(struct zone *zone,\n \tmemcpy(\u0026template, page, sizeof(*page));\n \tif (pfns_per_compound != 1)\n \t\tmemmap_init_compound(page, pfn, zone_idx, nid, pgmap,\n-\t\t\t\t     compound_nr_pages(pfn, altmap, pgmap));\n+\t\t\t\t     compound_nr_pages(pfn, pgmap));\n \tpfn += pfns_per_compound;\n \n \t/* Initialize the remaining head pages from template. */\n@@ -1160,7 +1155,7 @@ void __ref memmap_init_zone_device(struct zone *zone,\n \t\t\tcontinue;\n \n \t\tmemmap_init_compound(page, pfn, zone_idx, nid, pgmap,\n-\t\t\t\t     compound_nr_pages(pfn, altmap, pgmap));\n+\t\t\t\t     compound_nr_pages(pfn, pgmap));\n \t}\n \n \tpageblock_migratetype_init_range(start_pfn, nr_pages, MIGRATE_MOVABLE,\ndiff --git a/mm/sparse-vmemmap.c b/mm/sparse-vmemmap.c\nindex f22d815d7af04..4be85e194c782 100644\n--- a/mm/sparse-vmemmap.c\n+++ b/mm/sparse-vmemmap.c\n@@ -35,34 +35,20 @@\n /*\n  * Flags for vmemmap_populate_range and friends.\n  */\n-/* Get a ref on the head page struct page, for ZONE_DEVICE compound pages */\n-#define VMEMMAP_POPULATE_PAGEREF\t0x0001\n+/* Vmemmap population for ZONE_DEVICE compound pages */\n+#define VMEMMAP_POPULATE_DAX\t\t0x0001\n \n #include \"internal.h\"\n #include \"mm_init.h\"\n #include \"sparse.h\"\n \n-/*\n- * Allocate a block of memory to be used to back the virtual memory map\n- * or to back the page tables that are used to create the mapping.\n- * Uses the main allocators if they are available, else bootmem.\n- */\n-\n-static void * __ref __earlyonly_bootmem_alloc(int node,\n-\t\t\t\tunsigned long size,\n-\t\t\t\tunsigned long align,\n-\t\t\t\tunsigned long goal)\n-{\n-\treturn memmap_alloc(size, align, goal, node, false);\n-}\n-\n-void * __meminit vmemmap_alloc_block(unsigned long size, int node)\n+void __ref *vmemmap_alloc_block(unsigned long size, int node)\n {\n \t/* If the main allocator is up use that, fallback to bootmem. */\n \tif (slab_is_available()) {\n \t\tgfp_t gfp_mask = GFP_KERNEL|__GFP_RETRY_MAYFAIL|__GFP_NOWARN;\n \t\tint order = get_order(size);\n-\t\tstatic bool warned __meminitdata;\n+\t\tstatic bool warned;\n \t\tstruct page *page;\n \n \t\tpage = alloc_pages_node(node, gfp_mask, order);\n@@ -76,8 +62,7 @@ void * __meminit vmemmap_alloc_block(unsigned long size, int node)\n \t\t}\n \t\treturn NULL;\n \t} else\n-\t\treturn __earlyonly_bootmem_alloc(node, size, size,\n-\t\t\t\t__pa(MAX_DMA_ADDRESS));\n+\t\treturn memmap_alloc(size, size, __pa(MAX_DMA_ADDRESS), node, false);\n }\n \n static void * __meminit altmap_alloc_block_buf(unsigned long size,\n@@ -146,29 +131,27 @@ void __meminit vmemmap_verify(pte_t *pte, int node,\n \t\t\tstart, end - 1);\n }\n \n-int __meminit section_nr_vmemmap_pages(unsigned long pfn, unsigned long nr_pages,\n-\t\tstruct vmem_altmap *altmap, struct dev_pagemap *pgmap)\n+int __meminit section_nr_vmemmap_pages(unsigned long pfn, unsigned long nr_pages)\n {\n \tconst struct mem_section *ms = __pfn_to_section(pfn);\n-\tconst int order = pgmap ? pgmap-\u003evmemmap_shift : section_compound_order(ms);\n-\tconst int vmemmap_pages = pgmap ? VMEMMAP_RESERVE_NR : VMEMMAP_OPTIMIZATION_PAGES;\n+\tconst int order = section_compound_order(ms);\n \tconst unsigned long pages_per_compound = 1UL \u003c\u003c order;\n \n \tVM_WARN_ON_ONCE(!IS_ALIGNED(pfn | nr_pages, PAGES_PER_SUBSECTION));\n \tVM_WARN_ON_ONCE(nr_pages \u003e PAGES_PER_SECTION);\n \n-\tif (!vmemmap_can_optimize(altmap, pgmap) \u0026\u0026 !section_vmemmap_optimizable(ms))\n+\tif (!section_vmemmap_optimizable(ms))\n \t\treturn DIV_ROUND_UP(nr_pages * sizeof(struct page), PAGE_SIZE);\n \n \tif (order \u003c PFN_SECTION_SHIFT) {\n \t\tVM_WARN_ON_ONCE(!IS_ALIGNED(pfn | nr_pages, pages_per_compound));\n-\t\treturn vmemmap_pages * nr_pages / pages_per_compound;\n+\t\treturn VMEMMAP_OPTIMIZATION_PAGES * nr_pages / pages_per_compound;\n \t}\n \n \tVM_WARN_ON_ONCE(!IS_ALIGNED(pfn | nr_pages, PAGES_PER_SECTION));\n \n \tif (IS_ALIGNED(pfn, pages_per_compound))\n-\t\treturn vmemmap_pages;\n+\t\treturn VMEMMAP_OPTIMIZATION_PAGES;\n \n \treturn 0;\n }\n@@ -184,52 +167,95 @@ static void * __meminit vmemmap_alloc_block_zero(unsigned long size, int node)\n \treturn p;\n }\n \n-#ifdef CONFIG_HUGETLB_PAGE_OPTIMIZE_VMEMMAP\n-static __meminit struct page *vmemmap_get_tail(unsigned int order, struct zone *zone)\n+#ifdef CONFIG_VMEMMAP_OPTIMIZATION\n+#define VMEMMAP_OPTIMIZATION_NR_ORDERS\t(MAX_FOLIO_ORDER - VMEMMAP_OPTIMIZATION_MIN_ORDER + 1)\n+\n+static __ref struct page **vmemmap_tails(struct zone *zone)\n {\n-\tstruct page *p, *tail;\n-\tunsigned int idx;\n-\tint node = zone_to_nid(zone);\n+\tconst size_t size = array_size(VMEMMAP_OPTIMIZATION_NR_ORDERS,\n+\t\t\t\t       sizeof(*zone-\u003evmemmap_tails));\n+\tstruct page **pages = READ_ONCE(zone-\u003evmemmap_tails);\n \n-\tif (WARN_ON_ONCE(order \u003c VMEMMAP_OPTIMIZATION_MIN_ORDER))\n-\t\treturn NULL;\n-\tif (WARN_ON_ONCE(order \u003e MAX_FOLIO_ORDER))\n-\t\treturn NULL;\n+\tif (pages)\n+\t\treturn pages;\n \n-\tidx = order - VMEMMAP_OPTIMIZATION_MIN_ORDER;\n-\ttail = zone-\u003evmemmap_tails[idx];\n-\tif (tail)\n-\t\treturn tail;\n-\tp = vmemmap_alloc_block_zero(PAGE_SIZE, node);\n-\tif (!p)\n+\tpages = slab_is_available() ? kzalloc_objs(*pages, VMEMMAP_OPTIMIZATION_NR_ORDERS) :\n+\t\tmemblock_alloc(size, __alignof__(*pages));\n+\tif (!pages)\n \t\treturn NULL;\n-\tfor (int i = 0; i \u003c PAGE_SIZE / sizeof(struct page); i++)\n-\t\tinit_compound_tail(p + i, NULL, order, zone);\n \n-\ttail = virt_to_page(p);\n-\tzone-\u003evmemmap_tails[idx] = tail;\n+\tif (cmpxchg(\u0026zone-\u003evmemmap_tails, NULL, pages) != NULL) {\n+\t\tif (slab_is_available())\n+\t\t\tkfree(pages);\n+\t\telse\n+\t\t\tmemblock_free(pages, size);\n+\t\tpages = READ_ONCE(zone-\u003evmemmap_tails);\n+\t}\n \n-\treturn tail;\n+\treturn pages;\n }\n-#else\n-static inline struct page *vmemmap_get_tail(unsigned int order, struct zone *zone)\n+\n+struct page __ref *vmemmap_shared_tail_page(unsigned int order, struct zone *zone)\n {\n-\treturn NULL;\n+\tconst unsigned int idx = order - VMEMMAP_OPTIMIZATION_MIN_ORDER;\n+\tstruct page *page, **pages;\n+\tvoid *addr;\n+\n+\tif (WARN_ON_ONCE(idx \u003e= VMEMMAP_OPTIMIZATION_NR_ORDERS))\n+\t\treturn NULL;\n+\n+\tpages = vmemmap_tails(zone);\n+\tif (!pages)\n+\t\treturn NULL;\n+\n+\tpage = READ_ONCE(pages[idx]);\n+\tif (page)\n+\t\treturn page;\n+\n+\taddr = vmemmap_alloc_block(PAGE_SIZE, zone_to_nid(zone));\n+\tif (!addr)\n+\t\treturn NULL;\n+\n+\tfor (int i = 0; i \u003c PAGE_SIZE / sizeof(struct page); i++) {\n+\t\tpage = (struct page *)addr + i;\n+\t\tmm_zero_struct_page(page);\n+\t\tatomic_set(\u0026page-\u003e_mapcount, -1);\n+\t\tset_page_node(page, zone_to_nid(zone));\n+\t\tset_page_zone(page, zone_idx(zone));\n+\t\tprep_compound_tail(page, NULL, order);\n+\t\tif (zone_is_zone_device(zone))\n+\t\t\t__SetPageReserved(page);\n+\t}\n+\n+\tpage = virt_to_page(addr);\n+\tif (cmpxchg(\u0026pages[idx], NULL, page) != NULL) {\n+\t\tif (slab_is_available())\n+\t\t\t__free_page(page);\n+\t\telse\n+\t\t\tmemblock_free(addr, PAGE_SIZE);\n+\t\tpage = READ_ONCE(pages[idx]);\n+\t}\n+\n+\treturn page;\n }\n #endif\n \n static __meminit void *vmemmap_alloc_pte(unsigned long pfn, int node,\n-\t\t\t\t\t struct vmem_altmap *altmap)\n+\t\tstruct vmem_altmap *altmap, unsigned long flags)\n {\n \tstruct zone *zone;\n \tstruct page *page;\n \tconst unsigned int order = pfn_to_section_compound_order(pfn);\n \n-\tif (!vmemmap_optimizable_pfn(pfn))\n+\t/*\n+\t * Device DAX still relies on vmemmap_populate_compound_pages() for\n+\t * head/first-tail allocation and tail-page reuse.\n+\t */\n+\tif (!vmemmap_optimizable_pfn(pfn) || flags \u0026 VMEMMAP_POPULATE_DAX)\n \t\treturn vmemmap_alloc_block_buf(PAGE_SIZE, node, altmap);\n \n \tzone = pfn_to_zone(pfn, node);\n-\tpage = vmemmap_get_tail(order, zone);\n+\tpage = vmemmap_shared_tail_page(order, zone);\n \tif (!page)\n \t\treturn NULL;\n \n@@ -247,7 +273,7 @@ static pte_t * __meminit vmemmap_pte_populate(pmd_t *pmd, unsigned long addr, in\n \t\tpte_t entry;\n \n \t\tif (ptpfn == (unsigned long)-1) {\n-\t\t\tvoid *p = vmemmap_alloc_pte(pfn, node, altmap);\n+\t\t\tvoid *p = vmemmap_alloc_pte(pfn, node, altmap, flags);\n \n \t\t\tif (!p)\n \t\t\t\treturn NULL;\n@@ -256,14 +282,18 @@ static pte_t * __meminit vmemmap_pte_populate(pmd_t *pmd, unsigned long addr, in\n \t\t\t/*\n \t\t\t * When a PTE/PMD entry is freed from the init_mm\n \t\t\t * there's a free_pages() call to this page allocated\n-\t\t\t * above. Thus this get_page() is paired with the\n+\t\t\t * above. Thus this try_get_page() is paired with the\n \t\t\t * put_page_testzero() on the freeing path.\n \t\t\t * This can only called by certain ZONE_DEVICE path,\n \t\t\t * and through vmemmap_populate_compound_pages() when\n \t\t\t * slab is available.\n+\t\t\t *\n+\t\t\t * Use try_get_page() to prevent the shared page refcount\n+\t\t\t * from overflowing.\n \t\t\t */\n-\t\t\tif (flags \u0026 VMEMMAP_POPULATE_PAGEREF)\n-\t\t\t\tget_page(pfn_to_page(ptpfn));\n+\t\t\tif ((flags \u0026 VMEMMAP_POPULATE_DAX) \u0026\u0026\n+\t\t\t    !try_get_page(pfn_to_page(ptpfn)))\n+\t\t\t\treturn NULL;\n \t\t}\n \t\tentry = pfn_pte(ptpfn, PAGE_KERNEL);\n \t\tset_pte_at(\u0026init_mm, addr, pte, entry);\n@@ -497,69 +527,42 @@ static bool __meminit reuse_compound_section(unsigned long start_pfn,\n \treturn !IS_ALIGNED(offset, nr_pages) \u0026\u0026 nr_pages \u003e PAGES_PER_SUBSECTION;\n }\n \n-static pte_t * __meminit compound_section_tail_page(unsigned long addr)\n-{\n-\tpte_t *pte;\n-\n-\taddr -= PAGE_SIZE;\n-\n-\t/*\n-\t * Assuming sections are populated sequentially, the previous section's\n-\t * page data can be reused.\n-\t */\n-\tpte = pte_offset_kernel(pmd_off_k(addr), addr);\n-\tif (!pte)\n-\t\treturn NULL;\n-\n-\treturn pte;\n-}\n-\n static int __meminit vmemmap_populate_compound_pages(unsigned long start_pfn,\n \t\t\t\t\t\t     unsigned long start,\n \t\t\t\t\t\t     unsigned long end, int node,\n \t\t\t\t\t\t     struct dev_pagemap *pgmap)\n {\n+\tconst unsigned long flags = VMEMMAP_POPULATE_DAX;\n+\tconst unsigned int order = pfn_to_section_compound_order(start_pfn);\n \tunsigned long size, addr;\n \tpte_t *pte;\n+\tstruct page *page;\n \tint rc;\n \n-\tif (reuse_compound_section(start_pfn, pgmap)) {\n-\t\tpte = compound_section_tail_page(start);\n-\t\tif (!pte)\n-\t\t\treturn -ENOMEM;\n+\tpage = vmemmap_shared_tail_page(order, device_zone(node));\n+\tif (!page)\n+\t\treturn -ENOMEM;\n \n-\t\t/*\n-\t\t * Reuse the page that was populated in the prior iteration\n-\t\t * with just tail struct pages.\n-\t\t */\n+\tif (reuse_compound_section(start_pfn, pgmap))\n \t\treturn vmemmap_populate_range(start, end, node, NULL,\n-\t\t\t\t\t      pte_pfn(ptep_get(pte)),\n-\t\t\t\t\t      VMEMMAP_POPULATE_PAGEREF);\n-\t}\n+\t\t\t\t\t      page_to_pfn(page), flags);\n \n-\tsize = min(end - start, pgmap_vmemmap_nr(pgmap) * sizeof(struct page));\n+\tsize = min(end - start, (1UL \u003c\u003c order) * sizeof(struct page));\n \tfor (addr = start; addr \u003c end; addr += size) {\n \t\tunsigned long next, last = addr + size;\n \n \t\t/* Populate the head page vmemmap page */\n-\t\tpte = vmemmap_populate_address(addr, node, NULL, -1, 0);\n-\t\tif (!pte)\n-\t\t\treturn -ENOMEM;\n-\n-\t\t/* Populate the tail pages vmemmap page */\n-\t\tnext = addr + PAGE_SIZE;\n-\t\tpte = vmemmap_populate_address(next, node, NULL, -1, 0);\n+\t\tpte = vmemmap_populate_address(addr, node, NULL, -1, flags);\n \t\tif (!pte)\n \t\t\treturn -ENOMEM;\n \n \t\t/*\n-\t\t * Reuse the previous page for the rest of tail pages\n+\t\t * Reuse the shared page for the rest of tail pages\n \t\t * See layout diagram in Documentation/mm/vmemmap_dedup.rst\n \t\t */\n-\t\tnext += PAGE_SIZE;\n+\t\tnext = addr + PAGE_SIZE;\n \t\trc = vmemmap_populate_range(next, last, node, NULL,\n-\t\t\t\t\t    pte_pfn(ptep_get(pte)),\n-\t\t\t\t\t    VMEMMAP_POPULATE_PAGEREF);\n+\t\t\t\t\t    page_to_pfn(page), flags);\n \t\tif (rc)\n \t\t\treturn -ENOMEM;\n \t}\n@@ -581,7 +584,7 @@ struct page * __meminit __populate_section_memmap(unsigned long pfn,\n \t\t!IS_ALIGNED(nr_pages, PAGES_PER_SUBSECTION)))\n \t\treturn NULL;\n \n-\tif (vmemmap_can_optimize(altmap, pgmap))\n+\tif (pgmap \u0026\u0026 section_vmemmap_optimizable(__pfn_to_section(pfn)))\n \t\tr = vmemmap_populate_compound_pages(pfn, start, end, nid, pgmap);\n \telse\n \t\tr = vmemmap_populate(start, end, nid, altmap);\n@@ -670,7 +673,7 @@ static struct page * __meminit populate_section_memmap(unsigned long pfn,\n \tstruct page *page = __populate_section_memmap(pfn, nr_pages, nid, altmap,\n \t\t\t\t\t\t      pgmap);\n \n-\tmemmap_pages_add(section_nr_vmemmap_pages(pfn, nr_pages, altmap, pgmap));\n+\tmemmap_pages_add(section_nr_vmemmap_pages(pfn, nr_pages));\n \n \treturn page;\n }\n@@ -681,7 +684,7 @@ static void depopulate_section_memmap(unsigned long pfn, unsigned long nr_pages,\n \tunsigned long start = (unsigned long) pfn_to_page(pfn);\n \tunsigned long end = start + nr_pages * sizeof(struct page);\n \n-\tmemmap_pages_add(-section_nr_vmemmap_pages(pfn, nr_pages, altmap, pgmap));\n+\tmemmap_pages_add(-section_nr_vmemmap_pages(pfn, nr_pages));\n \tvmemmap_free(start, end, altmap);\n }\n \n@@ -691,8 +694,7 @@ static void free_map_bootmem(struct page *memmap)\n \tunsigned long end = (unsigned long)(memmap + PAGES_PER_SECTION);\n \tunsigned long pfn = page_to_pfn(memmap);\n \n-\tmemmap_boot_pages_add(-section_nr_vmemmap_pages(pfn, PAGES_PER_SECTION,\n-\t\t\t\t\t\t\tNULL, NULL));\n+\tmemmap_boot_pages_add(-section_nr_vmemmap_pages(pfn, PAGES_PER_SECTION));\n \tvmemmap_free(start, end, NULL);\n }\n \n@@ -800,8 +802,10 @@ static void section_deactivate(unsigned long pfn, unsigned long nr_pages,\n \telse if (memmap)\n \t\tfree_map_bootmem(memmap);\n \n-\tif (empty)\n+\tif (empty) {\n \t\tms-\u003esection_mem_map = (unsigned long)NULL;\n+\t\tsection_set_compound_order(ms, 0);\n+\t}\n }\n \n static struct page * __meminit section_activate(int nid, unsigned long pfn,\n@@ -811,8 +815,13 @@ static struct page * __meminit section_activate(int nid, unsigned long pfn,\n \tstruct mem_section *ms = __pfn_to_section(pfn);\n \tstruct mem_section_usage *usage = NULL;\n \tstruct page *memmap;\n+\tunsigned int order;\n \tint rc;\n \n+\torder = vmemmap_can_optimize(altmap, pgmap) ? pgmap-\u003evmemmap_shift : 0;\n+\tif (nr_pages \u003c PAGES_PER_SECTION \u0026\u0026 section_compound_order(ms))\n+\t\treturn ERR_PTR(-EOPNOTSUPP);\n+\n \tif (!ms-\u003eusage) {\n \t\tusage = kzalloc(mem_section_usage_size(), GFP_KERNEL);\n \t\tif (!usage)\n@@ -838,6 +847,7 @@ static struct page * __meminit section_activate(int nid, unsigned long pfn,\n \tif (nr_pages \u003c PAGES_PER_SECTION \u0026\u0026 early_section(ms))\n \t\treturn pfn_to_page(pfn);\n \n+\tsection_set_compound_order_range(pfn, nr_pages, order);\n \tmemmap = populate_section_memmap(pfn, nr_pages, nid, altmap, pgmap);\n \tif (!memmap) {\n \t\tsection_deactivate(pfn, nr_pages, altmap, pgmap);\n@@ -883,13 +893,16 @@ int __meminit sparse_add_section(int nid, unsigned long start_pfn,\n \tif (IS_ERR(memmap))\n \t\treturn PTR_ERR(memmap);\n \n+\tms = __nr_to_section(section_nr);\n \t/*\n-\t * Poison uninitialized struct pages in order to catch invalid flags\n-\t * combinations.\n+\t * Poison uninitialized struct pages to catch invalid flag combinations.\n+\t *\n+\t * Tail struct pages in a vmemmap-optimized section are initialized and\n+\t * shared during vmemmap population, so they must not be overwritten here.\n \t */\n-\tpage_init_poison(memmap, sizeof(struct page) * nr_pages);\n+\tif (!section_vmemmap_optimizable(ms))\n+\t\tpage_init_poison(memmap, sizeof(struct page) * nr_pages);\n \n-\tms = __nr_to_section(section_nr);\n \t__section_mark_present(ms, section_nr);\n \n \t/* Align memmap to section boundary in the subsection case */\ndiff --git a/mm/sparse.c b/mm/sparse.c\nindex cc28bb41fdb1f..b75921c622ede 100644\n--- a/mm/sparse.c\n+++ b/mm/sparse.c\n@@ -250,8 +250,7 @@ static void __init sparse_init_nid(int nid, unsigned long pnum_begin,\n \t\t\t\t\t\tnid, NULL, NULL);\n \t\tif (!map)\n \t\t\tpanic(\"Failed to allocate memmap for section %lu\\n\", pnum);\n-\t\tmemmap_boot_pages_add(section_nr_vmemmap_pages(pfn, PAGES_PER_SECTION,\n-\t\t\t\t\t\t\t       NULL, NULL));\n+\t\tmemmap_boot_pages_add(section_nr_vmemmap_pages(pfn, PAGES_PER_SECTION));\n \t\tsparse_init_one_section(__nr_to_section(pnum), pnum, map, usage,\n \t\t\t\t\tSECTION_IS_EARLY);\n \t\tusage = (void *)usage + mem_section_usage_size();\ndiff --git a/mm/sparse.h b/mm/sparse.h\nindex d3a71ef4fad0f..530692cdd516f 100644\n--- a/mm/sparse.h\n+++ b/mm/sparse.h\n@@ -9,80 +9,7 @@\n #define __MM_SPARSE_H\n \n #include \u003clinux/mmzone.h\u003e\n-\n-#ifdef CONFIG_HUGETLB_PAGE_OPTIMIZE_VMEMMAP\n-static inline unsigned int section_compound_order(const struct mem_section *section)\n-{\n-\treturn section-\u003ecompound_page_order;\n-}\n-\n-static inline void section_set_compound_order(struct mem_section *section,\n-\t\t\t\t\t      unsigned int order)\n-{\n-\tVM_WARN_ON(section_compound_order(section) \u0026\u0026 order \u0026\u0026\n-\t\t   section_compound_order(section) != order);\n-\tsection-\u003ecompound_page_order = order;\n-}\n-\n-static inline void section_set_compound_order_range(unsigned long pfn,\n-\t\tunsigned long nr_pages, unsigned int order)\n-{\n-\tunsigned long section_nr = pfn_to_section_nr(pfn);\n-\n-\tif (!IS_ALIGNED(pfn | nr_pages, PAGES_PER_SECTION))\n-\t\treturn;\n-\n-\tfor (unsigned long i = 0; i \u003c nr_pages / PAGES_PER_SECTION; i++)\n-\t\tsection_set_compound_order(__nr_to_section(section_nr + i), order);\n-}\n-\n-static inline unsigned int pfn_to_section_compound_order(unsigned long pfn)\n-{\n-\treturn section_compound_order(__pfn_to_section(pfn));\n-}\n-#else\n-static inline unsigned int section_compound_order(const struct mem_section *section)\n-{\n-\treturn 0;\n-}\n-\n-static inline void section_set_compound_order(struct mem_section *section,\n-\t\t\t\t\t      unsigned int order)\n-{\n-}\n-\n-static inline void section_set_compound_order_range(unsigned long pfn,\n-\t\tunsigned long nr_pages, unsigned int order)\n-{\n-}\n-\n-static inline unsigned int pfn_to_section_compound_order(unsigned long pfn)\n-{\n-\treturn 0;\n-}\n-#endif\n-\n-static inline bool vmemmap_optimizable_pfn(unsigned long pfn)\n-{\n-\tconst unsigned int order = pfn_to_section_compound_order(pfn);\n-\tconst unsigned long nr_pages = 1UL \u003c\u003c order;\n-\n-\tif (!is_power_of_2(sizeof(struct page)))\n-\t\treturn false;\n-\n-\treturn (pfn \u0026 (nr_pages - 1)) \u003e= VMEMMAP_OPTIMIZATION_NR_STRUCT_PAGES;\n-}\n-\n-static inline bool vmemmap_optimizable_order(unsigned int order)\n-{\n-\tif (!IS_ENABLED(CONFIG_HUGETLB_PAGE_OPTIMIZE_VMEMMAP))\n-\t\treturn false;\n-\n-\tif (!is_power_of_2(sizeof(struct page)))\n-\t\treturn false;\n-\n-\treturn order \u003e= VMEMMAP_OPTIMIZATION_MIN_ORDER;\n-}\n+#include \u003clinux/vmemmap-optimization.h\u003e\n \n /*\n  * mm/sparse.c\n@@ -143,12 +70,10 @@ static inline void sparse_sections_init(void) {}\n  */\n #ifdef CONFIG_SPARSEMEM_VMEMMAP\n void sparse_init_subsection_map(void);\n-int section_nr_vmemmap_pages(unsigned long pfn, unsigned long nr_pages,\n-\t\tstruct vmem_altmap *altmap, struct dev_pagemap *pgmap);\n+int section_nr_vmemmap_pages(unsigned long pfn, unsigned long nr_pages);\n #else\n static inline void sparse_init_subsection_map(void) {}\n-static inline int section_nr_vmemmap_pages(unsigned long pfn, unsigned long nr_pages,\n-\t\tstruct vmem_altmap *altmap, struct dev_pagemap *pgmap)\n+static inline int section_nr_vmemmap_pages(unsigned long pfn, unsigned long nr_pages)\n {\n \treturn DIV_ROUND_UP(nr_pages * sizeof(struct page), PAGE_SIZE);\n }\n"
}

2/1 2026/10/01 14:24 agent patch-evaluator
12m Model:
core

Args:
null

Results:
{
  "EnableConfigs": [
    "HUGETLBFS",
    "HUGETLB_PAGE_OPTIMIZE_VMEMMAP_DEFAULT_ON",
    "SPARSEMEM_VMEMMAP",
    "ZONE_DEVICE",
    "MEMORY_HOTPLUG",
    "MEMORY_HOTREMOVE"
  ],
  "FocusSymbols": [
    "vmemmap_shared_tail_page",
    "hugetlb_vmemmap_optimize_folio",
    "hugetlb_vmemmap_optimize_folios",
    "__populate_section_memmap",
    "sparse_add_section",
    "section_nr_vmemmap_pages"
  ],
  "Reasoning": "The patch modifies core memory management (mm/) logic by unifying HugeTLB Vmemmap Optimization (HVO) and DAX vmemmap deduplication under a common VMEMMAP_OPTIMIZATION framework. It replaces per-zone static tail page arrays with dynamically allocated structures (vmemmap_tails), adds vmemmap_shared_tail_page(), alters page table population with new DAX flags and reference count management, and changes memory section activation, deactivation, and page poisoning behavior. These code paths are fully reachable on amd64 via HugeTLB folio allocation and memory hotplug/DAX workflows.",
  "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 c5cd987b2e9292f2580f8a5fb953333aa1ec4a3b
Author: syz-cluster <triage@syzkaller.com>
Date:   Thu Oct 1 14:24:00 2026 +0000

    syz-cluster: applied patch under review

diff --git a/Documentation/arch/powerpc/vmemmap_dedup.rst b/Documentation/arch/powerpc/vmemmap_dedup.rst
index dc4db59fdf87b..8286acbca9bc5 100644
--- a/Documentation/arch/powerpc/vmemmap_dedup.rst
+++ b/Documentation/arch/powerpc/vmemmap_dedup.rst
@@ -19,82 +19,28 @@ With 1G PUD level mapping, we require 16384 struct pages and a single 64K
 vmemmap page can contain 1024 struct pages (64K/sizeof(struct page)). Hence we
 require 16 64K pages in vmemmap to map the struct page for 1G PUD level mapping.
 
-Here's how things look like on device-dax after the sections are populated::
- +-----------+ ---virt_to_page---> +-----------+   mapping to   +-----------+
- |           |                     |     0     | -------------> |     0     |
- |           |                     +-----------+                +-----------+
- |           |                     |     1     | -------------> |     1     |
- |           |                     +-----------+                +-----------+
- |           |                     |     2     | ----------------^ ^ ^ ^ ^ ^
- |           |                     +-----------+                   | | | | |
- |           |                     |     3     | ------------------+ | | | |
- |           |                     +-----------+                     | | | |
- |           |                     |     4     | --------------------+ | | |
- |    PUD    |                     +-----------+                       | | |
- |   level   |                     |     .     | ----------------------+ | |
- |  mapping  |                     +-----------+                         | |
- |           |                     |     .     | ------------------------+ |
- |           |                     +-----------+                           |
- |           |                     |     15    | --------------------------+
- |           |                     +-----------+
- |           |
- |           |
- |           |
- +-----------+
-
-
 With 4K page size, 2M PMD level mapping requires 512 struct pages and a single
 4K vmemmap page contains 64 struct pages(4K/sizeof(struct page)). Hence we
 require 8 4K pages in vmemmap to map the struct page for 2M pmd level mapping.
 
-Here's how things look like on device-dax after the sections are populated::
-
- +-----------+ ---virt_to_page---> +-----------+   mapping to   +-----------+
- |           |                     |     0     | -------------> |     0     |
- |           |                     +-----------+                +-----------+
- |           |                     |     1     | -------------> |     1     |
- |           |                     +-----------+                +-----------+
- |           |                     |     2     | ----------------^ ^ ^ ^ ^ ^
- |           |                     +-----------+                   | | | | |
- |           |                     |     3     | ------------------+ | | | |
- |           |                     +-----------+                     | | | |
- |           |                     |     4     | --------------------+ | | |
- |    PMD    |                     +-----------+                       | | |
- |   level   |                     |     5     | ----------------------+ | |
- |  mapping  |                     +-----------+                         | |
- |           |                     |     6     | ------------------------+ |
- |           |                     +-----------+                           |
- |           |                     |     7     | --------------------------+
- |           |                     +-----------+
- |           |
- |           |
- |           |
- +-----------+
-
-With 1G PUD level mapping, we require 262144 struct pages and a single 4K
-vmemmap page can contain 64 struct pages (4K/sizeof(struct page)). Hence we
-require 4096 4K pages in vmemmap to map the struct pages for 1G PUD level
-mapping.
-
-Here's how things look like on device-dax after the sections are populated::
-
- +-----------+ ---virt_to_page---> +-----------+   mapping to   +-----------+
- |           |                     |     0     | -------------> |     0     |
- |           |                     +-----------+                +-----------+
- |           |                     |     1     | -------------> |     1     |
- |           |                     +-----------+                +-----------+
- |           |                     |     2     | ----------------^ ^ ^ ^ ^ ^
- |           |                     +-----------+                   | | | | |
- |           |                     |     3     | ------------------+ | | | |
- |           |                     +-----------+                     | | | |
- |           |                     |     4     | --------------------+ | | |
- |    PUD    |                     +-----------+                       | | |
- |   level   |                     |     .     | ----------------------+ | |
- |  mapping  |                     +-----------+                         | |
- |           |                     |     .     | ------------------------+ |
- |           |                     +-----------+                           |
- |           |                     |   4095    | --------------------------+
- |           |                     +-----------+
+Here's how things look on device-dax after vmemmap-optimized sections are
+populated. ``N`` is the number of vmemmap pages required by the DAX mapping
+above::
+
+  Device DAX                  vmemmap pages (N pages)         backing page frames
+ +-----------+ ---virt_to_page---> +-----------+   mapping to   +-------------+
+ |           |                     |     0     | -------------> |      0      |
+ |           |                     +-----------+                +-------------+
+ |           |                     |     1     | ------+
+ |           |                     +-----------+       |
+ |           |                     |     2     | ------+
+ |           |                     +-----------+       |
+ |           |                     |     .     | ------+        +-------------+
+ |  PMD/PUD  |                     +-----------+       |        | A single,   |
+ |   level   |                     |     .     | ------+------> | per-zone    |
+ |  mapping  |                     +-----------+       |        | shared tail |
+ |           |                     |   N - 1   | ------+        | page        |
+ |           |                     +-----------+                +-------------+
  |           |
  |           |
  |           |
diff --git a/Documentation/mm/vmemmap_dedup.rst b/Documentation/mm/vmemmap_dedup.rst
index 9fa8642ded483..8c287ae3f86cc 100644
--- a/Documentation/mm/vmemmap_dedup.rst
+++ b/Documentation/mm/vmemmap_dedup.rst
@@ -1,4 +1,3 @@
-
 .. SPDX-License-Identifier: GPL-2.0
 
 =========================================
@@ -192,32 +191,7 @@ to 4 on HugeTLB pages.
 
 There's no remapping of vmemmap given that device-dax memory is not part of
 System RAM ranges initialized at boot. Thus the tail page deduplication
-happens at a later stage when we populate the sections. HugeTLB reuses the
-the head vmemmap page representing, whereas device-dax reuses the tail
-vmemmap page. This results in only half of the savings compared to HugeTLB.
-
-Deduplicated tail pages are not mapped read-only.
+happens at a later stage when we populate the sections.
 
-Here's how things look like on device-dax after the sections are populated::
-
- +-----------+ ---virt_to_page---> +-----------+   mapping to   +-----------+
- |           |                     |     0     | -------------> |     0     |
- |           |                     +-----------+                +-----------+
- |           |                     |     1     | -------------> |     1     |
- |           |                     +-----------+                +-----------+
- |           |                     |     2     | ----------------^ ^ ^ ^ ^ ^
- |           |                     +-----------+                   | | | | |
- |           |                     |     3     | ------------------+ | | | |
- |           |                     +-----------+                     | | | |
- |           |                     |     4     | --------------------+ | | |
- |    PMD    |                     +-----------+                       | | |
- |   level   |                     |     5     | ----------------------+ | |
- |  mapping  |                     +-----------+                         | |
- |           |                     |     6     | ------------------------+ |
- |           |                     +-----------+                           |
- |           |                     |     7     | --------------------------+
- |           |                     +-----------+
- |           |
- |           |
- |           |
- +-----------+
+Deduplicated tail pages are not mapped read-only. The mapping layout is the same
+as HugeTLB.
diff --git a/MAINTAINERS b/MAINTAINERS
index fe1d70ed5100b..4689a02100600 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -12097,6 +12097,7 @@ F:	Documentation/mm/hugetlbfs_reserv.rst
 F:	Documentation/mm/vmemmap_dedup.rst
 F:	fs/hugetlbfs/
 F:	include/linux/hugetlb.h
+F:	include/linux/vmemmap-optimization.h
 F:	include/trace/events/hugetlbfs.h
 F:	mm/hugetlb.c
 F:	mm/hugetlb_cgroup.c
diff --git a/arch/loongarch/include/asm/pgtable.h b/arch/loongarch/include/asm/pgtable.h
index cf29a4c8ac593..f876031351319 100644
--- a/arch/loongarch/include/asm/pgtable.h
+++ b/arch/loongarch/include/asm/pgtable.h
@@ -72,6 +72,7 @@
 
 #include <linux/mm_types.h>
 #include <linux/mmzone.h>
+#include <linux/vmemmap-optimization.h>
 #include <asm/fixmap.h>
 #include <asm/sparsemem.h>
 
diff --git a/arch/powerpc/mm/book3s64/radix_pgtable.c b/arch/powerpc/mm/book3s64/radix_pgtable.c
index cf692b2b5f7bc..9ca28e4a610a2 100644
--- a/arch/powerpc/mm/book3s64/radix_pgtable.c
+++ b/arch/powerpc/mm/book3s64/radix_pgtable.c
@@ -19,6 +19,7 @@
 #include <linux/string_helpers.h>
 #include <linux/memory.h>
 #include <linux/kfence.h>
+#include <linux/vmemmap-optimization.h>
 
 #include <asm/pgalloc.h>
 #include <asm/mmu_context.h>
@@ -1217,92 +1218,6 @@ int __meminit radix__vmemmap_populate(unsigned long start, unsigned long end, in
 	return 0;
 }
 
-static pte_t * __meminit radix__vmemmap_populate_address(unsigned long addr, int node,
-							 struct vmem_altmap *altmap,
-							 struct page *reuse)
-{
-	pgd_t *pgd;
-	p4d_t *p4d;
-	pud_t *pud;
-	pmd_t *pmd;
-	pte_t *pte;
-
-	pgd = pgd_offset_k(addr);
-	p4d = p4d_offset(pgd, addr);
-	pud = vmemmap_pud_alloc(p4d, node, addr);
-	if (!pud)
-		return NULL;
-	pmd = vmemmap_pmd_alloc(pud, node, addr);
-	if (!pmd)
-		return NULL;
-	if (pmd_leaf(*pmd))
-		/*
-		 * The second page is mapped as a hugepage due to a nearby request.
-		 * Force our mapping to page size without deduplication
-		 */
-		return NULL;
-	pte = vmemmap_pte_alloc(pmd, node, addr);
-	if (!pte)
-		return NULL;
-	radix__vmemmap_pte_populate(pmd, addr, node, NULL, NULL);
-	vmemmap_verify(pte, node, addr, addr + PAGE_SIZE);
-
-	return pte;
-}
-
-static pte_t * __meminit vmemmap_compound_tail_page(unsigned long addr,
-						    unsigned long pfn_offset, int node)
-{
-	pgd_t *pgd;
-	p4d_t *p4d;
-	pud_t *pud;
-	pmd_t *pmd;
-	pte_t *pte;
-	unsigned long map_addr;
-
-	/* the second vmemmap page which we use for duplication */
-	map_addr = addr - pfn_offset * sizeof(struct page) + PAGE_SIZE;
-	pgd = pgd_offset_k(map_addr);
-	p4d = p4d_offset(pgd, map_addr);
-	pud = vmemmap_pud_alloc(p4d, node, map_addr);
-	if (!pud)
-		return NULL;
-	pmd = vmemmap_pmd_alloc(pud, node, map_addr);
-	if (!pmd)
-		return NULL;
-	if (pmd_leaf(*pmd))
-		/*
-		 * The second page is mapped as a hugepage due to a nearby request.
-		 * Force our mapping to page size without deduplication
-		 */
-		return NULL;
-	pte = vmemmap_pte_alloc(pmd, node, map_addr);
-	if (!pte)
-		return NULL;
-	/*
-	 * Check if there exist a mapping to the left
-	 */
-	if (pte_none(*pte)) {
-		/*
-		 * Populate the head page vmemmap page.
-		 * It can fall in different pmd, hence
-		 * vmemmap_populate_address()
-		 */
-		pte = radix__vmemmap_populate_address(map_addr - PAGE_SIZE, node, NULL, NULL);
-		if (!pte)
-			return NULL;
-		/*
-		 * Populate the tail pages vmemmap page
-		 */
-		pte = radix__vmemmap_pte_populate(pmd, map_addr, node, NULL, NULL);
-		if (!pte)
-			return NULL;
-		vmemmap_verify(pte, node, map_addr, map_addr + PAGE_SIZE);
-		return pte;
-	}
-	return pte;
-}
-
 int __meminit vmemmap_populate_compound_pages(unsigned long start_pfn,
 					      unsigned long start,
 					      unsigned long end, int node,
@@ -1320,6 +1235,12 @@ int __meminit vmemmap_populate_compound_pages(unsigned long start_pfn,
 	pud_t *pud;
 	pmd_t *pmd;
 	pte_t *pte;
+	struct page *tail_page;
+	unsigned int order = pfn_to_section_compound_order(start_pfn);
+
+	tail_page = vmemmap_shared_tail_page(order, device_zone(node));
+	if (!tail_page)
+		return -ENOMEM;
 
 	for (addr = start; addr < end; addr = next) {
 
@@ -1343,16 +1264,15 @@ int __meminit vmemmap_populate_compound_pages(unsigned long start_pfn,
 		if (!pte_none(*pte)) {
 			/*
 			 * This could be because we already have a compound
-			 * page whose VMEMMAP_RESERVE_NR pages were mapped and
+			 * page whose retained vmemmap page was mapped and
 			 * this request fall in those pages.
 			 */
 			next = addr + PAGE_SIZE;
 			continue;
 		} else {
-			unsigned long nr_pages = pgmap_vmemmap_nr(pgmap);
+			unsigned long nr_pages = 1UL << order;
 			unsigned long addr_pfn = page_to_pfn((struct page *)addr);
 			unsigned long pfn_offset = addr_pfn - ALIGN_DOWN(addr_pfn, nr_pages);
-			pte_t *tail_page_pte;
 
 			/*
 			 * if the address is aligned to huge page size it is the
@@ -1365,35 +1285,11 @@ int __meminit vmemmap_populate_compound_pages(unsigned long start_pfn,
 					return -ENOMEM;
 				vmemmap_verify(pte, node, addr, addr + PAGE_SIZE);
 
-				/*
-				 * Populate the tail pages vmemmap page
-				 * It can fall in different pmd, hence
-				 * vmemmap_populate_address()
-				 */
-				pte = radix__vmemmap_populate_address(addr + PAGE_SIZE, node, NULL, NULL);
-				if (!pte)
-					return -ENOMEM;
-
-				next = addr + 2 * PAGE_SIZE;
-				continue;
-			}
-			/*
-			 * get the 2nd mapping details
-			 * Also create it if that doesn't exist
-			 */
-			tail_page_pte = vmemmap_compound_tail_page(addr, pfn_offset, node);
-			if (!tail_page_pte) {
-
-				pte = radix__vmemmap_pte_populate(pmd, addr, node, NULL, NULL);
-				if (!pte)
-					return -ENOMEM;
-				vmemmap_verify(pte, node, addr, addr + PAGE_SIZE);
-
 				next = addr + PAGE_SIZE;
 				continue;
 			}
 
-			pte = radix__vmemmap_pte_populate(pmd, addr, node, NULL, pte_page(*tail_page_pte));
+			pte = radix__vmemmap_pte_populate(pmd, addr, node, NULL, tail_page);
 			if (!pte)
 				return -ENOMEM;
 			vmemmap_verify(pte, node, addr, addr + PAGE_SIZE);
diff --git a/arch/riscv/mm/init.c b/arch/riscv/mm/init.c
index fb37b0b67efec..857f9a55039ce 100644
--- a/arch/riscv/mm/init.c
+++ b/arch/riscv/mm/init.c
@@ -22,6 +22,7 @@
 #include <linux/hugetlb.h>
 #include <linux/kfence.h>
 #include <linux/execmem.h>
+#include <linux/vmemmap-optimization.h>
 
 #include <asm/alternative.h>
 #include <asm/fixmap.h>
diff --git a/arch/x86/entry/vdso/vdso32/fake_32bit_build.h b/arch/x86/entry/vdso/vdso32/fake_32bit_build.h
index bc3e549795c3f..72a92cb9b53d3 100644
--- a/arch/x86/entry/vdso/vdso32/fake_32bit_build.h
+++ b/arch/x86/entry/vdso/vdso32/fake_32bit_build.h
@@ -11,7 +11,7 @@
 #undef CONFIG_PGTABLE_LEVELS
 #undef CONFIG_ILLEGAL_POINTER_VALUE
 #undef CONFIG_SPARSEMEM_VMEMMAP
-#undef CONFIG_HUGETLB_PAGE_OPTIMIZE_VMEMMAP
+#undef CONFIG_VMEMMAP_OPTIMIZATION
 #undef CONFIG_NR_CPUS
 #undef CONFIG_PARAVIRT_XXL
 
diff --git a/fs/Kconfig b/fs/Kconfig
index d1c210c6508f0..1454b7fe9641f 100644
--- a/fs/Kconfig
+++ b/fs/Kconfig
@@ -278,6 +278,7 @@ config HUGETLB_PAGE_OPTIMIZE_VMEMMAP
 	def_bool HUGETLB_PAGE
 	depends on ARCH_WANT_OPTIMIZE_HUGETLB_VMEMMAP
 	depends on SPARSEMEM_VMEMMAP
+	select VMEMMAP_OPTIMIZATION
 
 config HUGETLB_PMD_PAGE_TABLE_SHARING
 	def_bool HUGETLB_PAGE
diff --git a/include/linux/mm.h b/include/linux/mm.h
index c49ef99b4413b..30a3365bca827 100644
--- a/include/linux/mm.h
+++ b/include/linux/mm.h
@@ -38,6 +38,7 @@
 #include <linux/bitops.h>
 #include <linux/iommu-debug-pagealloc.h>
 #include <linux/kcsan-checks.h>
+#include <linux/vmemmap-optimization.h>
 
 struct mempolicy;
 struct anon_vma;
@@ -5167,7 +5168,6 @@ static inline void vmem_altmap_free(struct vmem_altmap *altmap,
 }
 #endif
 
-#define VMEMMAP_RESERVE_NR	2
 #ifdef CONFIG_ARCH_WANT_OPTIMIZE_DAX_VMEMMAP
 static inline bool __vmemmap_can_optimize(struct vmem_altmap *altmap,
 					  struct dev_pagemap *pgmap)
@@ -5175,6 +5175,9 @@ static inline bool __vmemmap_can_optimize(struct vmem_altmap *altmap,
 	unsigned long nr_pages;
 	unsigned long nr_vmemmap_pages;
 
+	if (!IS_ENABLED(CONFIG_VMEMMAP_OPTIMIZATION))
+		return false;
+
 	if (!pgmap || !is_power_of_2(sizeof(struct page)))
 		return false;
 
@@ -5184,7 +5187,7 @@ static inline bool __vmemmap_can_optimize(struct vmem_altmap *altmap,
 	 * For vmemmap optimization with DAX we need minimum 2 vmemmap
 	 * pages. See layout diagram in Documentation/mm/vmemmap_dedup.rst
 	 */
-	return !altmap && (nr_vmemmap_pages > VMEMMAP_RESERVE_NR);
+	return !altmap && (nr_vmemmap_pages > VMEMMAP_OPTIMIZATION_PAGES);
 }
 /*
  * If we don't have an architecture override, use the generic rule
diff --git a/include/linux/mmzone.h b/include/linux/mmzone.h
index acd94cecc0d39..65de3bb13eb3a 100644
--- a/include/linux/mmzone.h
+++ b/include/linux/mmzone.h
@@ -96,28 +96,6 @@
 
 #define MAX_FOLIO_NR_PAGES	(1UL << MAX_FOLIO_ORDER)
 
-/*
- * HugeTLB Vmemmap Optimization (HVO) requires struct pages of the head page to
- * be naturally aligned with regard to the folio size.
- *
- * HVO which is only active if the size of struct page is a power of 2.
- */
-#define MAX_FOLIO_VMEMMAP_ALIGN \
-	(IS_ENABLED(CONFIG_HUGETLB_PAGE_OPTIMIZE_VMEMMAP) && \
-	 is_power_of_2(sizeof(struct page)) ? \
-	 MAX_FOLIO_NR_PAGES * sizeof(struct page) : 0)
-
-/* The number of retained vmemmap pages with HVO enabled. */
-#define VMEMMAP_OPTIMIZATION_PAGES		1
-#define VMEMMAP_OPTIMIZATION_NR_STRUCT_PAGES	\
-	(VMEMMAP_OPTIMIZATION_PAGES * PAGE_SIZE / sizeof(struct page))
-#define VMEMMAP_OPTIMIZATION_MIN_ORDER		(ilog2(VMEMMAP_OPTIMIZATION_NR_STRUCT_PAGES) + 1)
-
-#define __VMEMMAP_OPTIMIZATION_NR_ORDERS	\
-	(MAX_FOLIO_ORDER - VMEMMAP_OPTIMIZATION_MIN_ORDER + 1)
-#define VMEMMAP_OPTIMIZATION_NR_ORDERS		\
-	(__VMEMMAP_OPTIMIZATION_NR_ORDERS > 0 ? __VMEMMAP_OPTIMIZATION_NR_ORDERS : 0)
-
 enum migratetype {
 	MIGRATE_UNMOVABLE,
 	MIGRATE_MOVABLE,
@@ -1155,8 +1133,8 @@ struct zone {
 	/* Zone statistics */
 	atomic_long_t		vm_stat[NR_VM_ZONE_STAT_ITEMS];
 	atomic_long_t		vm_numa_event[NR_VM_NUMA_EVENT_ITEMS];
-#ifdef CONFIG_HUGETLB_PAGE_OPTIMIZE_VMEMMAP
-	struct page *vmemmap_tails[VMEMMAP_OPTIMIZATION_NR_ORDERS];
+#ifdef CONFIG_VMEMMAP_OPTIMIZATION
+	struct page **vmemmap_tails;
 #endif
 } ____cacheline_internodealigned_in_smp;
 
@@ -2019,7 +1997,7 @@ struct mem_section {
 	unsigned long section_mem_map;
 
 	struct mem_section_usage *usage;
-#ifdef CONFIG_HUGETLB_PAGE_OPTIMIZE_VMEMMAP
+#ifdef CONFIG_VMEMMAP_OPTIMIZATION
 	/*
 	 * Normally, sections hold regular (order-0) pages. However, for
 	 * sections with HVO enabled, this tracks the compound page order
@@ -2148,11 +2126,21 @@ static inline int online_device_section(const struct mem_section *section)
 
 	return section && ((section->section_mem_map & flags) == flags);
 }
+
+static inline struct zone *device_zone(int nid)
+{
+	return &NODE_DATA(nid)->node_zones[ZONE_DEVICE];
+}
 #else
 static inline int online_device_section(const struct mem_section *section)
 {
 	return 0;
 }
+
+static inline struct zone *device_zone(int nid)
+{
+	return NULL;
+}
 #endif
 
 static inline int online_section_nr(unsigned long nr)
diff --git a/include/linux/page-flags.h b/include/linux/page-flags.h
index 86dd0470da117..7080a6a1a79e7 100644
--- a/include/linux/page-flags.h
+++ b/include/linux/page-flags.h
@@ -208,14 +208,13 @@ enum pageflags {
 static __always_inline bool compound_info_has_mask(void)
 {
 	/*
-	 * Limit mask usage to HugeTLB vmemmap optimization (HVO) where it
-	 * makes a difference.
+	 * Limit mask usage to HVO where it makes a difference.
 	 *
 	 * The approach with mask would work in the wider set of conditions,
 	 * but it requires validating that struct pages are naturally aligned
 	 * for all orders up to the MAX_FOLIO_ORDER, which can be tricky.
 	 */
-	if (!IS_ENABLED(CONFIG_HUGETLB_PAGE_OPTIMIZE_VMEMMAP))
+	if (!IS_ENABLED(CONFIG_VMEMMAP_OPTIMIZATION))
 		return false;
 
 	return is_power_of_2(sizeof(struct page));
diff --git a/include/linux/vmemmap-optimization.h b/include/linux/vmemmap-optimization.h
new file mode 100644
index 0000000000000..fa9e9abd66560
--- /dev/null
+++ b/include/linux/vmemmap-optimization.h
@@ -0,0 +1,115 @@
+/* SPDX-License-Identifier: GPL-2.0-or-later */
+/*
+ * vmemmap-optimization.h
+ *
+ * Generic vmemmap optimization declarations.
+ *
+ * Author: Muchun Song <songmuchun@bytedance.com>
+ */
+#ifndef _LINUX_VMEMMAP_OPTIMIZATION_H
+#define _LINUX_VMEMMAP_OPTIMIZATION_H
+
+#include <linux/align.h>
+#include <linux/log2.h>
+#include <linux/mmdebug.h>
+#include <linux/mmzone.h>
+
+/*
+ * HugeTLB Vmemmap Optimization (HVO) requires struct pages of the head page to
+ * be naturally aligned with regard to the folio size.
+ *
+ * HVO which is only active if the size of struct page is a power of 2.
+ */
+#define MAX_FOLIO_VMEMMAP_ALIGN					\
+	(IS_ENABLED(CONFIG_VMEMMAP_OPTIMIZATION) &&		\
+	 is_power_of_2(sizeof(struct page)) ?			\
+	 MAX_FOLIO_NR_PAGES * sizeof(struct page) : 0)
+
+/* The number of retained vmemmap pages with HVO enabled. */
+#define VMEMMAP_OPTIMIZATION_PAGES		1
+#define VMEMMAP_OPTIMIZATION_NR_STRUCT_PAGES	\
+	(VMEMMAP_OPTIMIZATION_PAGES * PAGE_SIZE / sizeof(struct page))
+#define VMEMMAP_OPTIMIZATION_MIN_ORDER		(ilog2(VMEMMAP_OPTIMIZATION_NR_STRUCT_PAGES) + 1)
+
+#ifdef CONFIG_VMEMMAP_OPTIMIZATION
+static inline unsigned int section_compound_order(const struct mem_section *section)
+{
+	return section->compound_page_order;
+}
+
+static inline void section_set_compound_order(struct mem_section *section,
+					      unsigned int order)
+{
+	VM_WARN_ON(section_compound_order(section) && order &&
+		   section_compound_order(section) != order);
+	section->compound_page_order = order;
+}
+
+static inline void section_set_compound_order_range(unsigned long pfn,
+		unsigned long nr_pages, unsigned int order)
+{
+	unsigned long section_nr = pfn_to_section_nr(pfn);
+
+	if (!IS_ALIGNED(pfn | nr_pages, PAGES_PER_SECTION))
+		return;
+
+	for (unsigned long i = 0; i < nr_pages / PAGES_PER_SECTION; i++)
+		section_set_compound_order(__nr_to_section(section_nr + i), order);
+}
+
+static inline unsigned int pfn_to_section_compound_order(unsigned long pfn)
+{
+	return section_compound_order(__pfn_to_section(pfn));
+}
+
+struct page *vmemmap_shared_tail_page(unsigned int order, struct zone *zone);
+#else
+static inline unsigned int section_compound_order(const struct mem_section *section)
+{
+	return 0;
+}
+
+static inline void section_set_compound_order(struct mem_section *section,
+					      unsigned int order)
+{
+}
+
+static inline void section_set_compound_order_range(unsigned long pfn,
+		unsigned long nr_pages, unsigned int order)
+{
+}
+
+static inline unsigned int pfn_to_section_compound_order(unsigned long pfn)
+{
+	return 0;
+}
+
+static inline struct page *vmemmap_shared_tail_page(unsigned int order,
+						    struct zone *zone)
+{
+	return NULL;
+}
+#endif /* CONFIG_VMEMMAP_OPTIMIZATION */
+
+static inline bool vmemmap_optimizable_pfn(unsigned long pfn)
+{
+	const unsigned int order = pfn_to_section_compound_order(pfn);
+	const unsigned long nr_pages = 1UL << order;
+
+	if (!is_power_of_2(sizeof(struct page)))
+		return false;
+
+	return (pfn & (nr_pages - 1)) >= VMEMMAP_OPTIMIZATION_NR_STRUCT_PAGES;
+}
+
+static inline bool vmemmap_optimizable_order(unsigned int order)
+{
+	if (!IS_ENABLED(CONFIG_VMEMMAP_OPTIMIZATION))
+		return false;
+
+	if (!is_power_of_2(sizeof(struct page)))
+		return false;
+
+	return order >= VMEMMAP_OPTIMIZATION_MIN_ORDER;
+}
+#endif /* _LINUX_VMEMMAP_OPTIMIZATION_H */
diff --git a/mm/Kconfig b/mm/Kconfig
index bc7befafb47b5..30170a936f1fc 100644
--- a/mm/Kconfig
+++ b/mm/Kconfig
@@ -461,6 +461,10 @@ config SPARSEMEM_VMEMMAP
 	  pfn_to_page and page_to_pfn operations.  This is the most
 	  efficient option when sufficient kernel resources are available.
 
+config VMEMMAP_OPTIMIZATION
+	bool
+	depends on SPARSEMEM_VMEMMAP
+
 #
 # Select this config option from the architecture Kconfig, if it is preferred
 # to enable the feature of HugeTLB/dev_dax vmemmap optimization.
@@ -1220,6 +1224,7 @@ config ZONE_DMA32
 config ZONE_DEVICE
 	bool "Device memory (pmem, HMM, etc...) hotplug support"
 	depends on MEMORY_HOTREMOVE
+	select VMEMMAP_OPTIMIZATION if ARCH_WANT_OPTIMIZE_DAX_VMEMMAP
 	select XARRAY_MULTI
 
 	help
diff --git a/mm/hugetlb.c b/mm/hugetlb.c
index fd00141b089a9..2003439ea13c6 100644
--- a/mm/hugetlb.c
+++ b/mm/hugetlb.c
@@ -38,6 +38,7 @@
 #include <linux/mm_inline.h>
 #include <linux/padata.h>
 #include <linux/pgalloc.h>
+#include <linux/vmemmap-optimization.h>
 
 #include <asm/page.h>
 #include <asm/tlb.h>
@@ -52,7 +53,6 @@
 #include "hugetlb_cma.h"
 #include "hugetlb_internal.h"
 #include "mm_init.h"
-#include "sparse.h"
 #include <linux/page-isolation.h>
 
 #define HUGE_BOOTMEM_ZONES_VALID	BIT(0)
diff --git a/mm/hugetlb_vmemmap.c b/mm/hugetlb_vmemmap.c
index f977d0a7e0027..0057fa2a16a89 100644
--- a/mm/hugetlb_vmemmap.c
+++ b/mm/hugetlb_vmemmap.c
@@ -15,11 +15,10 @@
 #include <linux/mmdebug.h>
 #include <linux/pagewalk.h>
 #include <linux/pgalloc.h>
+#include <linux/vmemmap-optimization.h>
 
 #include <asm/tlbflush.h>
 #include "hugetlb_vmemmap.h"
-#include "sparse.h"
-#include "internal.h"
 
 /**
  * struct vmemmap_remap_walk - walk vmemmap page table
@@ -493,32 +492,6 @@ static bool vmemmap_should_optimize_folio(const struct hstate *h, struct folio *
 	return true;
 }
 
-static struct page *vmemmap_get_tail(unsigned int order, struct zone *zone)
-{
-	const unsigned int idx = order - VMEMMAP_OPTIMIZATION_MIN_ORDER;
-	struct page *tail, *p;
-	int node = zone_to_nid(zone);
-
-	tail = READ_ONCE(zone->vmemmap_tails[idx]);
-	if (likely(tail))
-		return tail;
-
-	tail = alloc_pages_node(node, GFP_KERNEL | __GFP_ZERO, 0);
-	if (!tail)
-		return NULL;
-
-	p = page_to_virt(tail);
-	for (int i = 0; i < PAGE_SIZE / sizeof(struct page); i++)
-		init_compound_tail(p + i, NULL, order, zone);
-
-	if (cmpxchg(&zone->vmemmap_tails[idx], NULL, tail)) {
-		__free_page(tail);
-		tail = READ_ONCE(zone->vmemmap_tails[idx]);
-	}
-
-	return tail;
-}
-
 static int __hugetlb_vmemmap_optimize_folio(const struct hstate *h,
 					    struct folio *folio,
 					    struct list_head *vmemmap_pages,
@@ -535,7 +508,7 @@ static int __hugetlb_vmemmap_optimize_folio(const struct hstate *h,
 		return ret;
 
 	nid = folio_nid(folio);
-	vmemmap_tail = vmemmap_get_tail(h->order, folio_zone(folio));
+	vmemmap_tail = vmemmap_shared_tail_page(h->order, folio_zone(folio));
 	if (!vmemmap_tail)
 		return -ENOMEM;
 
diff --git a/mm/internal.h b/mm/internal.h
index da14c56fb24e1..0dca33db068f6 100644
--- a/mm/internal.h
+++ b/mm/internal.h
@@ -786,15 +786,6 @@ static inline void prep_compound_tail(struct page *tail,
 	VM_WARN_ON_ONCE(tail->private);
 }
 
-static inline void init_compound_tail(struct page *tail,
-		const struct page *head, unsigned int order, struct zone *zone)
-{
-	atomic_set(&tail->_mapcount, -1);
-	set_page_node(tail, zone_to_nid(zone));
-	set_page_zone(tail, zone_idx(zone));
-	prep_compound_tail(tail, head, order);
-}
-
 #if defined CONFIG_COMPACTION || defined CONFIG_CMA
 
 /*
diff --git a/mm/memory_hotplug.c b/mm/memory_hotplug.c
index b428da66d279c..d7a59167bec43 100644
--- a/mm/memory_hotplug.c
+++ b/mm/memory_hotplug.c
@@ -43,6 +43,7 @@
 #include "mm_init.h"
 #include "page_alloc.h"
 #include "shuffle.h"
+#include "sparse.h"
 
 enum {
 	MEMMAP_ON_MEMORY_DISABLE = 0,
@@ -554,8 +555,9 @@ void remove_pfn_range_from_zone(struct zone *zone,
 		/* Select all remaining pages up to the next section boundary */
 		cur_nr_pages =
 			min(end_pfn - pfn, SECTION_ALIGN_UP(pfn + 1) - pfn);
-		page_init_poison(pfn_to_page(pfn),
-				 sizeof(struct page) * cur_nr_pages);
+		if (!section_vmemmap_optimizable(__pfn_to_section(pfn)))
+			page_init_poison(pfn_to_page(pfn),
+					 sizeof(struct page) * cur_nr_pages);
 	}
 
 	/*
diff --git a/mm/mm_init.c b/mm/mm_init.c
index 97e0158d2aca5..56bb4567a4940 100644
--- a/mm/mm_init.c
+++ b/mm/mm_init.c
@@ -1049,19 +1049,14 @@ static void zone_device_page_init_from_template(struct page *page,
  * of an altmap. See vmemmap_populate_compound_pages().
  */
 static inline unsigned long compound_nr_pages(unsigned long pfn,
-					      struct vmem_altmap *altmap,
 					      struct dev_pagemap *pgmap)
 {
-	/*
-	 * If DAX memory is hot-plugged into an unoccupied subsection
-	 * of an early section, the unoptimized boot memmap is reused.
-	 * See section_activate().
-	 */
-	if (early_section(__pfn_to_section(pfn)) ||
-	    !vmemmap_can_optimize(altmap, pgmap))
+	const struct mem_section *ms = __pfn_to_section(pfn);
+
+	if (!section_vmemmap_optimizable(ms))
 		return pgmap_vmemmap_nr(pgmap);
 
-	return VMEMMAP_RESERVE_NR * (PAGE_SIZE / sizeof(struct page));
+	return VMEMMAP_OPTIMIZATION_PAGES * (PAGE_SIZE / sizeof(struct page));
 }
 
 static void __ref memmap_init_compound(struct page *head,
@@ -1144,7 +1139,7 @@ void __ref memmap_init_zone_device(struct zone *zone,
 	memcpy(&template, page, sizeof(*page));
 	if (pfns_per_compound != 1)
 		memmap_init_compound(page, pfn, zone_idx, nid, pgmap,
-				     compound_nr_pages(pfn, altmap, pgmap));
+				     compound_nr_pages(pfn, pgmap));
 	pfn += pfns_per_compound;
 
 	/* Initialize the remaining head pages from template. */
@@ -1160,7 +1155,7 @@ void __ref memmap_init_zone_device(struct zone *zone,
 			continue;
 
 		memmap_init_compound(page, pfn, zone_idx, nid, pgmap,
-				     compound_nr_pages(pfn, altmap, pgmap));
+				     compound_nr_pages(pfn, pgmap));
 	}
 
 	pageblock_migratetype_init_range(start_pfn, nr_pages, MIGRATE_MOVABLE,
diff --git a/mm/sparse-vmemmap.c b/mm/sparse-vmemmap.c
index f22d815d7af04..4be85e194c782 100644
--- a/mm/sparse-vmemmap.c
+++ b/mm/sparse-vmemmap.c
@@ -35,34 +35,20 @@
 /*
  * Flags for vmemmap_populate_range and friends.
  */
-/* Get a ref on the head page struct page, for ZONE_DEVICE compound pages */
-#define VMEMMAP_POPULATE_PAGEREF	0x0001
+/* Vmemmap population for ZONE_DEVICE compound pages */
+#define VMEMMAP_POPULATE_DAX		0x0001
 
 #include "internal.h"
 #include "mm_init.h"
 #include "sparse.h"
 
-/*
- * Allocate a block of memory to be used to back the virtual memory map
- * or to back the page tables that are used to create the mapping.
- * Uses the main allocators if they are available, else bootmem.
- */
-
-static void * __ref __earlyonly_bootmem_alloc(int node,
-				unsigned long size,
-				unsigned long align,
-				unsigned long goal)
-{
-	return memmap_alloc(size, align, goal, node, false);
-}
-
-void * __meminit vmemmap_alloc_block(unsigned long size, int node)
+void __ref *vmemmap_alloc_block(unsigned long size, int node)
 {
 	/* If the main allocator is up use that, fallback to bootmem. */
 	if (slab_is_available()) {
 		gfp_t gfp_mask = GFP_KERNEL|__GFP_RETRY_MAYFAIL|__GFP_NOWARN;
 		int order = get_order(size);
-		static bool warned __meminitdata;
+		static bool warned;
 		struct page *page;
 
 		page = alloc_pages_node(node, gfp_mask, order);
@@ -76,8 +62,7 @@ void * __meminit vmemmap_alloc_block(unsigned long size, int node)
 		}
 		return NULL;
 	} else
-		return __earlyonly_bootmem_alloc(node, size, size,
-				__pa(MAX_DMA_ADDRESS));
+		return memmap_alloc(size, size, __pa(MAX_DMA_ADDRESS), node, false);
 }
 
 static void * __meminit altmap_alloc_block_buf(unsigned long size,
@@ -146,29 +131,27 @@ void __meminit vmemmap_verify(pte_t *pte, int node,
 			start, end - 1);
 }
 
-int __meminit section_nr_vmemmap_pages(unsigned long pfn, unsigned long nr_pages,
-		struct vmem_altmap *altmap, struct dev_pagemap *pgmap)
+int __meminit section_nr_vmemmap_pages(unsigned long pfn, unsigned long nr_pages)
 {
 	const struct mem_section *ms = __pfn_to_section(pfn);
-	const int order = pgmap ? pgmap->vmemmap_shift : section_compound_order(ms);
-	const int vmemmap_pages = pgmap ? VMEMMAP_RESERVE_NR : VMEMMAP_OPTIMIZATION_PAGES;
+	const int order = section_compound_order(ms);
 	const unsigned long pages_per_compound = 1UL << order;
 
 	VM_WARN_ON_ONCE(!IS_ALIGNED(pfn | nr_pages, PAGES_PER_SUBSECTION));
 	VM_WARN_ON_ONCE(nr_pages > PAGES_PER_SECTION);
 
-	if (!vmemmap_can_optimize(altmap, pgmap) && !section_vmemmap_optimizable(ms))
+	if (!section_vmemmap_optimizable(ms))
 		return DIV_ROUND_UP(nr_pages * sizeof(struct page), PAGE_SIZE);
 
 	if (order < PFN_SECTION_SHIFT) {
 		VM_WARN_ON_ONCE(!IS_ALIGNED(pfn | nr_pages, pages_per_compound));
-		return vmemmap_pages * nr_pages / pages_per_compound;
+		return VMEMMAP_OPTIMIZATION_PAGES * nr_pages / pages_per_compound;
 	}
 
 	VM_WARN_ON_ONCE(!IS_ALIGNED(pfn | nr_pages, PAGES_PER_SECTION));
 
 	if (IS_ALIGNED(pfn, pages_per_compound))
-		return vmemmap_pages;
+		return VMEMMAP_OPTIMIZATION_PAGES;
 
 	return 0;
 }
@@ -184,52 +167,95 @@ static void * __meminit vmemmap_alloc_block_zero(unsigned long size, int node)
 	return p;
 }
 
-#ifdef CONFIG_HUGETLB_PAGE_OPTIMIZE_VMEMMAP
-static __meminit struct page *vmemmap_get_tail(unsigned int order, struct zone *zone)
+#ifdef CONFIG_VMEMMAP_OPTIMIZATION
+#define VMEMMAP_OPTIMIZATION_NR_ORDERS	(MAX_FOLIO_ORDER - VMEMMAP_OPTIMIZATION_MIN_ORDER + 1)
+
+static __ref struct page **vmemmap_tails(struct zone *zone)
 {
-	struct page *p, *tail;
-	unsigned int idx;
-	int node = zone_to_nid(zone);
+	const size_t size = array_size(VMEMMAP_OPTIMIZATION_NR_ORDERS,
+				       sizeof(*zone->vmemmap_tails));
+	struct page **pages = READ_ONCE(zone->vmemmap_tails);
 
-	if (WARN_ON_ONCE(order < VMEMMAP_OPTIMIZATION_MIN_ORDER))
-		return NULL;
-	if (WARN_ON_ONCE(order > MAX_FOLIO_ORDER))
-		return NULL;
+	if (pages)
+		return pages;
 
-	idx = order - VMEMMAP_OPTIMIZATION_MIN_ORDER;
-	tail = zone->vmemmap_tails[idx];
-	if (tail)
-		return tail;
-	p = vmemmap_alloc_block_zero(PAGE_SIZE, node);
-	if (!p)
+	pages = slab_is_available() ? kzalloc_objs(*pages, VMEMMAP_OPTIMIZATION_NR_ORDERS) :
+		memblock_alloc(size, __alignof__(*pages));
+	if (!pages)
 		return NULL;
-	for (int i = 0; i < PAGE_SIZE / sizeof(struct page); i++)
-		init_compound_tail(p + i, NULL, order, zone);
 
-	tail = virt_to_page(p);
-	zone->vmemmap_tails[idx] = tail;
+	if (cmpxchg(&zone->vmemmap_tails, NULL, pages) != NULL) {
+		if (slab_is_available())
+			kfree(pages);
+		else
+			memblock_free(pages, size);
+		pages = READ_ONCE(zone->vmemmap_tails);
+	}
 
-	return tail;
+	return pages;
 }
-#else
-static inline struct page *vmemmap_get_tail(unsigned int order, struct zone *zone)
+
+struct page __ref *vmemmap_shared_tail_page(unsigned int order, struct zone *zone)
 {
-	return NULL;
+	const unsigned int idx = order - VMEMMAP_OPTIMIZATION_MIN_ORDER;
+	struct page *page, **pages;
+	void *addr;
+
+	if (WARN_ON_ONCE(idx >= VMEMMAP_OPTIMIZATION_NR_ORDERS))
+		return NULL;
+
+	pages = vmemmap_tails(zone);
+	if (!pages)
+		return NULL;
+
+	page = READ_ONCE(pages[idx]);
+	if (page)
+		return page;
+
+	addr = vmemmap_alloc_block(PAGE_SIZE, zone_to_nid(zone));
+	if (!addr)
+		return NULL;
+
+	for (int i = 0; i < PAGE_SIZE / sizeof(struct page); i++) {
+		page = (struct page *)addr + i;
+		mm_zero_struct_page(page);
+		atomic_set(&page->_mapcount, -1);
+		set_page_node(page, zone_to_nid(zone));
+		set_page_zone(page, zone_idx(zone));
+		prep_compound_tail(page, NULL, order);
+		if (zone_is_zone_device(zone))
+			__SetPageReserved(page);
+	}
+
+	page = virt_to_page(addr);
+	if (cmpxchg(&pages[idx], NULL, page) != NULL) {
+		if (slab_is_available())
+			__free_page(page);
+		else
+			memblock_free(addr, PAGE_SIZE);
+		page = READ_ONCE(pages[idx]);
+	}
+
+	return page;
 }
 #endif
 
 static __meminit void *vmemmap_alloc_pte(unsigned long pfn, int node,
-					 struct vmem_altmap *altmap)
+		struct vmem_altmap *altmap, unsigned long flags)
 {
 	struct zone *zone;
 	struct page *page;
 	const unsigned int order = pfn_to_section_compound_order(pfn);
 
-	if (!vmemmap_optimizable_pfn(pfn))
+	/*
+	 * Device DAX still relies on vmemmap_populate_compound_pages() for
+	 * head/first-tail allocation and tail-page reuse.
+	 */
+	if (!vmemmap_optimizable_pfn(pfn) || flags & VMEMMAP_POPULATE_DAX)
 		return vmemmap_alloc_block_buf(PAGE_SIZE, node, altmap);
 
 	zone = pfn_to_zone(pfn, node);
-	page = vmemmap_get_tail(order, zone);
+	page = vmemmap_shared_tail_page(order, zone);
 	if (!page)
 		return NULL;
 
@@ -247,7 +273,7 @@ static pte_t * __meminit vmemmap_pte_populate(pmd_t *pmd, unsigned long addr, in
 		pte_t entry;
 
 		if (ptpfn == (unsigned long)-1) {
-			void *p = vmemmap_alloc_pte(pfn, node, altmap);
+			void *p = vmemmap_alloc_pte(pfn, node, altmap, flags);
 
 			if (!p)
 				return NULL;
@@ -256,14 +282,18 @@ static pte_t * __meminit vmemmap_pte_populate(pmd_t *pmd, unsigned long addr, in
 			/*
 			 * When a PTE/PMD entry is freed from the init_mm
 			 * there's a free_pages() call to this page allocated
-			 * above. Thus this get_page() is paired with the
+			 * above. Thus this try_get_page() is paired with the
 			 * put_page_testzero() on the freeing path.
 			 * This can only called by certain ZONE_DEVICE path,
 			 * and through vmemmap_populate_compound_pages() when
 			 * slab is available.
+			 *
+			 * Use try_get_page() to prevent the shared page refcount
+			 * from overflowing.
 			 */
-			if (flags & VMEMMAP_POPULATE_PAGEREF)
-				get_page(pfn_to_page(ptpfn));
+			if ((flags & VMEMMAP_POPULATE_DAX) &&
+			    !try_get_page(pfn_to_page(ptpfn)))
+				return NULL;
 		}
 		entry = pfn_pte(ptpfn, PAGE_KERNEL);
 		set_pte_at(&init_mm, addr, pte, entry);
@@ -497,69 +527,42 @@ static bool __meminit reuse_compound_section(unsigned long start_pfn,
 	return !IS_ALIGNED(offset, nr_pages) && nr_pages > PAGES_PER_SUBSECTION;
 }
 
-static pte_t * __meminit compound_section_tail_page(unsigned long addr)
-{
-	pte_t *pte;
-
-	addr -= PAGE_SIZE;
-
-	/*
-	 * Assuming sections are populated sequentially, the previous section's
-	 * page data can be reused.
-	 */
-	pte = pte_offset_kernel(pmd_off_k(addr), addr);
-	if (!pte)
-		return NULL;
-
-	return pte;
-}
-
 static int __meminit vmemmap_populate_compound_pages(unsigned long start_pfn,
 						     unsigned long start,
 						     unsigned long end, int node,
 						     struct dev_pagemap *pgmap)
 {
+	const unsigned long flags = VMEMMAP_POPULATE_DAX;
+	const unsigned int order = pfn_to_section_compound_order(start_pfn);
 	unsigned long size, addr;
 	pte_t *pte;
+	struct page *page;
 	int rc;
 
-	if (reuse_compound_section(start_pfn, pgmap)) {
-		pte = compound_section_tail_page(start);
-		if (!pte)
-			return -ENOMEM;
+	page = vmemmap_shared_tail_page(order, device_zone(node));
+	if (!page)
+		return -ENOMEM;
 
-		/*
-		 * Reuse the page that was populated in the prior iteration
-		 * with just tail struct pages.
-		 */
+	if (reuse_compound_section(start_pfn, pgmap))
 		return vmemmap_populate_range(start, end, node, NULL,
-					      pte_pfn(ptep_get(pte)),
-					      VMEMMAP_POPULATE_PAGEREF);
-	}
+					      page_to_pfn(page), flags);
 
-	size = min(end - start, pgmap_vmemmap_nr(pgmap) * sizeof(struct page));
+	size = min(end - start, (1UL << order) * sizeof(struct page));
 	for (addr = start; addr < end; addr += size) {
 		unsigned long next, last = addr + size;
 
 		/* Populate the head page vmemmap page */
-		pte = vmemmap_populate_address(addr, node, NULL, -1, 0);
-		if (!pte)
-			return -ENOMEM;
-
-		/* Populate the tail pages vmemmap page */
-		next = addr + PAGE_SIZE;
-		pte = vmemmap_populate_address(next, node, NULL, -1, 0);
+		pte = vmemmap_populate_address(addr, node, NULL, -1, flags);
 		if (!pte)
 			return -ENOMEM;
 
 		/*
-		 * Reuse the previous page for the rest of tail pages
+		 * Reuse the shared page for the rest of tail pages
 		 * See layout diagram in Documentation/mm/vmemmap_dedup.rst
 		 */
-		next += PAGE_SIZE;
+		next = addr + PAGE_SIZE;
 		rc = vmemmap_populate_range(next, last, node, NULL,
-					    pte_pfn(ptep_get(pte)),
-					    VMEMMAP_POPULATE_PAGEREF);
+					    page_to_pfn(page), flags);
 		if (rc)
 			return -ENOMEM;
 	}
@@ -581,7 +584,7 @@ struct page * __meminit __populate_section_memmap(unsigned long pfn,
 		!IS_ALIGNED(nr_pages, PAGES_PER_SUBSECTION)))
 		return NULL;
 
-	if (vmemmap_can_optimize(altmap, pgmap))
+	if (pgmap && section_vmemmap_optimizable(__pfn_to_section(pfn)))
 		r = vmemmap_populate_compound_pages(pfn, start, end, nid, pgmap);
 	else
 		r = vmemmap_populate(start, end, nid, altmap);
@@ -670,7 +673,7 @@ static struct page * __meminit populate_section_memmap(unsigned long pfn,
 	struct page *page = __populate_section_memmap(pfn, nr_pages, nid, altmap,
 						      pgmap);
 
-	memmap_pages_add(section_nr_vmemmap_pages(pfn, nr_pages, altmap, pgmap));
+	memmap_pages_add(section_nr_vmemmap_pages(pfn, nr_pages));
 
 	return page;
 }
@@ -681,7 +684,7 @@ static void depopulate_section_memmap(unsigned long pfn, unsigned long nr_pages,
 	unsigned long start = (unsigned long) pfn_to_page(pfn);
 	unsigned long end = start + nr_pages * sizeof(struct page);
 
-	memmap_pages_add(-section_nr_vmemmap_pages(pfn, nr_pages, altmap, pgmap));
+	memmap_pages_add(-section_nr_vmemmap_pages(pfn, nr_pages));
 	vmemmap_free(start, end, altmap);
 }
 
@@ -691,8 +694,7 @@ static void free_map_bootmem(struct page *memmap)
 	unsigned long end = (unsigned long)(memmap + PAGES_PER_SECTION);
 	unsigned long pfn = page_to_pfn(memmap);
 
-	memmap_boot_pages_add(-section_nr_vmemmap_pages(pfn, PAGES_PER_SECTION,
-							NULL, NULL));
+	memmap_boot_pages_add(-section_nr_vmemmap_pages(pfn, PAGES_PER_SECTION));
 	vmemmap_free(start, end, NULL);
 }
 
@@ -800,8 +802,10 @@ static void section_deactivate(unsigned long pfn, unsigned long nr_pages,
 	else if (memmap)
 		free_map_bootmem(memmap);
 
-	if (empty)
+	if (empty) {
 		ms->section_mem_map = (unsigned long)NULL;
+		section_set_compound_order(ms, 0);
+	}
 }
 
 static struct page * __meminit section_activate(int nid, unsigned long pfn,
@@ -811,8 +815,13 @@ static struct page * __meminit section_activate(int nid, unsigned long pfn,
 	struct mem_section *ms = __pfn_to_section(pfn);
 	struct mem_section_usage *usage = NULL;
 	struct page *memmap;
+	unsigned int order;
 	int rc;
 
+	order = vmemmap_can_optimize(altmap, pgmap) ? pgmap->vmemmap_shift : 0;
+	if (nr_pages < PAGES_PER_SECTION && section_compound_order(ms))
+		return ERR_PTR(-EOPNOTSUPP);
+
 	if (!ms->usage) {
 		usage = kzalloc(mem_section_usage_size(), GFP_KERNEL);
 		if (!usage)
@@ -838,6 +847,7 @@ static struct page * __meminit section_activate(int nid, unsigned long pfn,
 	if (nr_pages < PAGES_PER_SECTION && early_section(ms))
 		return pfn_to_page(pfn);
 
+	section_set_compound_order_range(pfn, nr_pages, order);
 	memmap = populate_section_memmap(pfn, nr_pages, nid, altmap, pgmap);
 	if (!memmap) {
 		section_deactivate(pfn, nr_pages, altmap, pgmap);
@@ -883,13 +893,16 @@ int __meminit sparse_add_section(int nid, unsigned long start_pfn,
 	if (IS_ERR(memmap))
 		return PTR_ERR(memmap);
 
+	ms = __nr_to_section(section_nr);
 	/*
-	 * Poison uninitialized struct pages in order to catch invalid flags
-	 * combinations.
+	 * Poison uninitialized struct pages to catch invalid flag combinations.
+	 *
+	 * Tail struct pages in a vmemmap-optimized section are initialized and
+	 * shared during vmemmap population, so they must not be overwritten here.
 	 */
-	page_init_poison(memmap, sizeof(struct page) * nr_pages);
+	if (!section_vmemmap_optimizable(ms))
+		page_init_poison(memmap, sizeof(struct page) * nr_pages);
 
-	ms = __nr_to_section(section_nr);
 	__section_mark_present(ms, section_nr);
 
 	/* Align memmap to section boundary in the subsection case */
diff --git a/mm/sparse.c b/mm/sparse.c
index cc28bb41fdb1f..b75921c622ede 100644
--- a/mm/sparse.c
+++ b/mm/sparse.c
@@ -250,8 +250,7 @@ static void __init sparse_init_nid(int nid, unsigned long pnum_begin,
 						nid, NULL, NULL);
 		if (!map)
 			panic("Failed to allocate memmap for section %lu\n", pnum);
-		memmap_boot_pages_add(section_nr_vmemmap_pages(pfn, PAGES_PER_SECTION,
-							       NULL, NULL));
+		memmap_boot_pages_add(section_nr_vmemmap_pages(pfn, PAGES_PER_SECTION));
 		sparse_init_one_section(__nr_to_section(pnum), pnum, map, usage,
 					SECTION_IS_EARLY);
 		usage = (void *)usage + mem_section_usage_size();
diff --git a/mm/sparse.h b/mm/sparse.h
index d3a71ef4fad0f..530692cdd516f 100644
--- a/mm/sparse.h
+++ b/mm/sparse.h
@@ -9,80 +9,7 @@
 #define __MM_SPARSE_H
 
 #include <linux/mmzone.h>
-
-#ifdef CONFIG_HUGETLB_PAGE_OPTIMIZE_VMEMMAP
-static inline unsigned int section_compound_order(const struct mem_section *section)
-{
-	return section->compound_page_order;
-}
-
-static inline void section_set_compound_order(struct mem_section *section,
-					      unsigned int order)
-{
-	VM_WARN_ON(section_compound_order(section) && order &&
-		   section_compound_order(section) != order);
-	section->compound_page_order = order;
-}
-
-static inline void section_set_compound_order_range(unsigned long pfn,
-		unsigned long nr_pages, unsigned int order)
-{
-	unsigned long section_nr = pfn_to_section_nr(pfn);
-
-	if (!IS_ALIGNED(pfn | nr_pages, PAGES_PER_SECTION))
-		return;
-
-	for (unsigned long i = 0; i < nr_pages / PAGES_PER_SECTION; i++)
-		section_set_compound_order(__nr_to_section(section_nr + i), order);
-}
-
-static inline unsigned int pfn_to_section_compound_order(unsigned long pfn)
-{
-	return section_compound_order(__pfn_to_section(pfn));
-}
-#else
-static inline unsigned int section_compound_order(const struct mem_section *section)
-{
-	return 0;
-}
-
-static inline void section_set_compound_order(struct mem_section *section,
-					      unsigned int order)
-{
-}
-
-static inline void section_set_compound_order_range(unsigned long pfn,
-		unsigned long nr_pages, unsigned int order)
-{
-}
-
-static inline unsigned int pfn_to_section_compound_order(unsigned long pfn)
-{
-	return 0;
-}
-#endif
-
-static inline bool vmemmap_optimizable_pfn(unsigned long pfn)
-{
-	const unsigned int order = pfn_to_section_compound_order(pfn);
-	const unsigned long nr_pages = 1UL << order;
-
-	if (!is_power_of_2(sizeof(struct page)))
-		return false;
-
-	return (pfn & (nr_pages - 1)) >= VMEMMAP_OPTIMIZATION_NR_STRUCT_PAGES;
-}
-
-static inline bool vmemmap_optimizable_order(unsigned int order)
-{
-	if (!IS_ENABLED(CONFIG_HUGETLB_PAGE_OPTIMIZE_VMEMMAP))
-		return false;
-
-	if (!is_power_of_2(sizeof(struct page)))
-		return false;
-
-	return order >= VMEMMAP_OPTIMIZATION_MIN_ORDER;
-}
+#include <linux/vmemmap-optimization.h>
 
 /*
  * mm/sparse.c
@@ -143,12 +70,10 @@ static inline void sparse_sections_init(void) {}
  */
 #ifdef CONFIG_SPARSEMEM_VMEMMAP
 void sparse_init_subsection_map(void);
-int section_nr_vmemmap_pages(unsigned long pfn, unsigned long nr_pages,
-		struct vmem_altmap *altmap, struct dev_pagemap *pgmap);
+int section_nr_vmemmap_pages(unsigned long pfn, unsigned long nr_pages);
 #else
 static inline void sparse_init_subsection_map(void) {}
-static inline int section_nr_vmemmap_pages(unsigned long pfn, unsigned long nr_pages,
-		struct vmem_altmap *altmap, struct dev_pagemap *pgmap)
+static inline int section_nr_vmemmap_pages(unsigned long pfn, unsigned long nr_pages)
 {
 	return DIV_ROUND_UP(nr_pages * sizeof(struct page), PAGE_SIZE);
 }

3/2 2026/10/01 14:24 llm patch-evaluator
0m Model:
gemini-3.8-flash

Args:
null

Results:
null

Tokens:
							input: 20325
							output: 21
							thoughts: 1178
						

Thoughts:
**Analyzing the Vmemmap Optimization Patch Series**

Okay, let's dive into this patch series. My initial focus is the target architecture, amd64, since that's what's relevant to me. I've been given a diff and I've quickly noted the changed files. I see a wide range of files affected, including documentation, MAINTAINERS, and code across various architectures (loongarch, powerpc, riscv, x86), as well as core memory management code.

It looks like the goal here is to unify the infrastructure for HugeTLB Vmemmap Optimization (HVO) and DAX vmemmap optimization under a common `CONFIG_VMEMMAP_OPTIMIZATION` framework. This feels like a good step toward simplifying and maintaining this code.

I'm seeing some key changes in the `Kconfig` files. `fs/Kconfig` now has `HUGETLB_PAGE_OPTIMIZE_VMEMMAP` selecting `VMEMMAP_OPTIMIZATION` if `HUGETLB_PAGE`, `ARCH_WANT_OPTIMIZE_HUGETLB_VMEMMAP`, and `SPARSEMEM_VMEMMAP` are all true. Also, in `mm/Kconfig` I see that `ZONE_DEVICE` selects `VMEMMAP_OPTIMIZATION` if `ARCH_WANT_OPTIMIZE_DAX_VMEMMAP` is also true. This suggests some conditional logic based on architecture and enabled features.

Looking at the code changes, `vmemmap_get_tail` appears to be refactored into `vmemmap_shared_tail_page` and moved to `mm/sparse-vmemmap.c`. This seems like a core change. I'm noting that `vmemmap_alloc_pte` now calls `vmemmap_shared_tail_page` and the `vmemmap_populate_compound_pages` function has been rewritten to also use it, along with `VMEMMAP_POPULATE_DAX`. There are functions in `mm/sparse-vmemmap.c` like `section_set_compound_order_range` and `section_set_compound_order` for activating and deactivating the compound order.

Also in `mm/memory_hotplug.c`, there's a conditional check now in `remove_pfn_range_from_zone` with `section_vmemmap_optimizable`. This is related to the poisoning of pages during memory hotplug. In `mm/mm_init.c`, the changes related to `compound_nr_pages` also use the same new `section_vmemmap_optimizable` function. There are other functions in `mm/sparse-vmemmap.c` that appear to relate to the Vmemmap optimization, like `section_nr_vmemmap_pages`.

I need to confirm that this is applicable to amd64, since `mm/` code is common and I want to see if this is reachable on x86-64. Okay, yes, based on my understanding, amd64 *does* support `SPARSEMEM_VMEMMAP`, and `HUGETLB_PAGE_OPTIMIZE_VMEMMAP` is also enabled by default. I need to double-check that `ARCH_WANT_OPTIMIZE_HUGETLB_VMEMMAP` or `ARCH_WANT_OPTIMIZE_DAX_VMEMMAP` are defined for x86. I'll need to look at the definitions.



4/2 2026/10/01 14:24 tool grepper
0m Args:
{
  "Expression": "ARCH_WANT_OPTIMIZE"
}

Results:
{
  "Output": "arch/loongarch/Kconfig=2=config LOONGARCH\n--\narch/loongarch/Kconfig-82-\tselect ARCH_WANT_LD_ORPHAN_WARN\narch/loongarch/Kconfig:83:\tselect ARCH_WANT_OPTIMIZE_HUGETLB_VMEMMAP if 64BIT\narch/loongarch/Kconfig-84-\tselect ARCH_WANTS_NO_INSTR\n--\narch/powerpc/Kconfig=118=config PPC\n--\narch/powerpc/Kconfig-187-\tselect ARCH_WANT_LD_ORPHAN_WARN\narch/powerpc/Kconfig:188:\tselect ARCH_WANT_OPTIMIZE_DAX_VMEMMAP\tif PPC_RADIX_MMU\narch/powerpc/Kconfig-189-\tselect ARCH_WANTS_MODULES_DATA_IN_VMALLOC\tif PPC_BOOK3S_32 || PPC_8xx\n--\narch/powerpc/include/asm/book3s/64/radix.h=352=int radix__remove_section_mapping(unsigned long start, unsigned long end);\n--\narch/powerpc/include/asm/book3s/64/radix.h-354-\narch/powerpc/include/asm/book3s/64/radix.h:355:#ifdef CONFIG_ARCH_WANT_OPTIMIZE_DAX_VMEMMAP\narch/powerpc/include/asm/book3s/64/radix.h-356-#define vmemmap_can_optimize vmemmap_can_optimize\n--\narch/powerpc/mm/book3s64/radix_pgtable.c=962=int __meminit radix__vmemmap_create_mapping(unsigned long start,\n--\narch/powerpc/mm/book3s64/radix_pgtable.c-980-\narch/powerpc/mm/book3s64/radix_pgtable.c:981:#ifdef CONFIG_ARCH_WANT_OPTIMIZE_DAX_VMEMMAP\narch/powerpc/mm/book3s64/radix_pgtable.c-982-bool vmemmap_can_optimize(struct vmem_altmap *altmap, struct dev_pagemap *pgmap)\n--\narch/riscv/Kconfig=13=config RISCV\n--\narch/riscv/Kconfig-89-\tselect ARCH_WANT_LD_ORPHAN_WARN\narch/riscv/Kconfig:90:\tselect ARCH_WANT_OPTIMIZE_DAX_VMEMMAP\narch/riscv/Kconfig:91:\tselect ARCH_WANT_OPTIMIZE_HUGETLB_VMEMMAP\narch/riscv/Kconfig-92-\tselect ARCH_WANTS_NO_INSTR\n--\narch/x86/Kconfig=57=config X86\n--\narch/x86/Kconfig-149-\tselect ARCH_WANT_LD_ORPHAN_WARN\narch/x86/Kconfig:150:\tselect ARCH_WANT_OPTIMIZE_DAX_VMEMMAP\tif X86_64\narch/x86/Kconfig:151:\tselect ARCH_WANT_OPTIMIZE_HUGETLB_VMEMMAP\tif X86_64\narch/x86/Kconfig-152-\tselect ARCH_WANTS_THP_SWAP\t\tif X86_64\n--\nfs/Kconfig=277=config HUGETLB_PAGE_OPTIMIZE_VMEMMAP\nfs/Kconfig-278-\tdef_bool HUGETLB_PAGE\nfs/Kconfig:279:\tdepends on ARCH_WANT_OPTIMIZE_HUGETLB_VMEMMAP\nfs/Kconfig-280-\tdepends on SPARSEMEM_VMEMMAP\n--\ninclude/linux/mm.h=5165=static inline void vmem_altmap_free(struct vmem_altmap *altmap,\n--\ninclude/linux/mm.h-5170-\ninclude/linux/mm.h:5171:#ifdef CONFIG_ARCH_WANT_OPTIMIZE_DAX_VMEMMAP\ninclude/linux/mm.h-5172-static inline bool __vmemmap_can_optimize(struct vmem_altmap *altmap,\n--\nmm/Kconfig=464=config VMEMMAP_OPTIMIZATION\n--\nmm/Kconfig-471-#\nmm/Kconfig:472:config ARCH_WANT_OPTIMIZE_DAX_VMEMMAP\nmm/Kconfig-473-\tbool\nmm/Kconfig-474-\nmm/Kconfig:475:config ARCH_WANT_OPTIMIZE_HUGETLB_VMEMMAP\nmm/Kconfig-476-\tbool\n--\nmm/Kconfig=1224=config ZONE_DEVICE\n--\nmm/Kconfig-1226-\tdepends on MEMORY_HOTREMOVE\nmm/Kconfig:1227:\tselect VMEMMAP_OPTIMIZATION if ARCH_WANT_OPTIMIZE_DAX_VMEMMAP\nmm/Kconfig-1228-\tselect XARRAY_MULTI\n"
}

5/2 2026/10/01 14:24 llm patch-evaluator
0m Model:
gemini-3.8-flash

Args:
null

Results:
null

Tokens:
							input: 23551
							output: 31
							thoughts: 199
						

Thoughts:
**Analyzing x86_64 Memory Management Optimizations**

Okay, so looking at the Kconfig for x86 architecture, I see that `ARCH_WANT_OPTIMIZE_DAX_VMEMMAP` and `ARCH_WANT_OPTIMIZE_HUGETLB_VMEMMAP` are automatically selected when compiling for x86_64. No surprises there; performance is always a priority on 64-bit systems. And as expected, `SPARSEMEM_VMEMMAP` is the standard approach, and `HUGETLB_PAGE` is also enabled by default. What's interesting is the dependency for `VMEMMAP_OPTIMIZATION`: it's selected when either `HUGETLB_PAGE_OPTIMIZE_VMEMMAP` or `ZONE_DEVICE` is enabled.

Right, time to dive deeper. I need to understand the implications of these selections. I'll need to read the specific patch changes related to these configurations. I need to understand the reachability of this optimization. I know to look at the relevant source code, specifically `mm/sparse-vmemmap.c` and `mm/hugetlb_vmemmap.c`, to grasp the precise details of how these optimizations are implemented and under what conditions they're triggered. Let's see what's changed.



6/2 2026/10/01 14:25 tool grepper
0m Args:
{
  "Expression": "vmemmap_shared_tail_page",
  "PathPrefix": "mm/"
}

Results:
{
  "Output": "mm/hugetlb_vmemmap.c=495=static int __hugetlb_vmemmap_optimize_folio(const struct hstate *h,\n--\nmm/hugetlb_vmemmap.c-510-\tnid = folio_nid(folio);\nmm/hugetlb_vmemmap.c:511:\tvmemmap_tail = vmemmap_shared_tail_page(h-\u003eorder, folio_zone(folio));\nmm/hugetlb_vmemmap.c-512-\tif (!vmemmap_tail)\n--\nmm/sparse-vmemmap.c=173=static __ref struct page **vmemmap_tails(struct zone *zone)\n--\nmm/sparse-vmemmap.c-197-\nmm/sparse-vmemmap.c:198:struct page __ref *vmemmap_shared_tail_page(unsigned int order, struct zone *zone)\nmm/sparse-vmemmap.c-199-{\n--\nmm/sparse-vmemmap.c=243=static __meminit void *vmemmap_alloc_pte(unsigned long pfn, int node,\n--\nmm/sparse-vmemmap.c-257-\tzone = pfn_to_zone(pfn, node);\nmm/sparse-vmemmap.c:258:\tpage = vmemmap_shared_tail_page(order, zone);\nmm/sparse-vmemmap.c-259-\tif (!page)\n--\nmm/sparse-vmemmap.c=530=static int __meminit vmemmap_populate_compound_pages(unsigned long start_pfn,\n--\nmm/sparse-vmemmap.c-541-\nmm/sparse-vmemmap.c:542:\tpage = vmemmap_shared_tail_page(order, device_zone(node));\nmm/sparse-vmemmap.c-543-\tif (!page)\n"
}

7/2 2026/10/01 14:25 llm patch-evaluator
0m Model:
gemini-3.8-flash

Args:
null

Results:
null

Tokens:
							input: 24553
							output: 37
							thoughts: 57
						

Thoughts:
Okay, let's dive into `mm/sparse-vmemmap.c`. I need to refresh my memory on how this all works, specifically concerning the shared tail pages in the vmemmap. I'm going to focus my attention on the sections around `vmemmap_shared_tail_page`, `vmemmap_tails`, `vmemmap_alloc_pte`, and `vmemmap_populate_compound_pages`. I recall these functions play key roles in managing the virtual memory mapping for the sparse vmemmap, especially in the context of compound pages. I want to understand how the allocation and population of those compound pages is performed within this specific context. Understanding the shared tail page mechanism is critical because this offers a significant optimization. How are these tail pages managed and shared, and how does the code ensure the necessary PTEs are allocated and populated efficiently? The intricacies of this area always require a close look, and I expect to see some interesting interactions between the allocation logic and the page table management.



8/2 2026/10/01 14:25 tool read-file
0m Args:
{
  "File": "mm/sparse-vmemmap.c",
  "FirstLine": 165,
  "LineCount": 80
}

Results:
{
  "Contents": " 165:\t\tmemset(p, 0, size);\n 166:\t\n 167:\t\treturn p;\n 168:\t}\n 169:\t\n 170:\t#ifdef CONFIG_VMEMMAP_OPTIMIZATION\n 171:\t#define VMEMMAP_OPTIMIZATION_NR_ORDERS\t(MAX_FOLIO_ORDER - VMEMMAP_OPTIMIZATION_MIN_ORDER + 1)\n 172:\t\n 173:\tstatic __ref struct page **vmemmap_tails(struct zone *zone)\n 174:\t{\n 175:\t\tconst size_t size = array_size(VMEMMAP_OPTIMIZATION_NR_ORDERS,\n 176:\t\t\t\t\t       sizeof(*zone-\u003evmemmap_tails));\n 177:\t\tstruct page **pages = READ_ONCE(zone-\u003evmemmap_tails);\n 178:\t\n 179:\t\tif (pages)\n 180:\t\t\treturn pages;\n 181:\t\n 182:\t\tpages = slab_is_available() ? kzalloc_objs(*pages, VMEMMAP_OPTIMIZATION_NR_ORDERS) :\n 183:\t\t\tmemblock_alloc(size, __alignof__(*pages));\n 184:\t\tif (!pages)\n 185:\t\t\treturn NULL;\n 186:\t\n 187:\t\tif (cmpxchg(\u0026zone-\u003evmemmap_tails, NULL, pages) != NULL) {\n 188:\t\t\tif (slab_is_available())\n 189:\t\t\t\tkfree(pages);\n 190:\t\t\telse\n 191:\t\t\t\tmemblock_free(pages, size);\n 192:\t\t\tpages = READ_ONCE(zone-\u003evmemmap_tails);\n 193:\t\t}\n 194:\t\n 195:\t\treturn pages;\n 196:\t}\n 197:\t\n 198:\tstruct page __ref *vmemmap_shared_tail_page(unsigned int order, struct zone *zone)\n 199:\t{\n 200:\t\tconst unsigned int idx = order - VMEMMAP_OPTIMIZATION_MIN_ORDER;\n 201:\t\tstruct page *page, **pages;\n 202:\t\tvoid *addr;\n 203:\t\n 204:\t\tif (WARN_ON_ONCE(idx \u003e= VMEMMAP_OPTIMIZATION_NR_ORDERS))\n 205:\t\t\treturn NULL;\n 206:\t\n 207:\t\tpages = vmemmap_tails(zone);\n 208:\t\tif (!pages)\n 209:\t\t\treturn NULL;\n 210:\t\n 211:\t\tpage = READ_ONCE(pages[idx]);\n 212:\t\tif (page)\n 213:\t\t\treturn page;\n 214:\t\n 215:\t\taddr = vmemmap_alloc_block(PAGE_SIZE, zone_to_nid(zone));\n 216:\t\tif (!addr)\n 217:\t\t\treturn NULL;\n 218:\t\n 219:\t\tfor (int i = 0; i \u003c PAGE_SIZE / sizeof(struct page); i++) {\n 220:\t\t\tpage = (struct page *)addr + i;\n 221:\t\t\tmm_zero_struct_page(page);\n 222:\t\t\tatomic_set(\u0026page-\u003e_mapcount, -1);\n 223:\t\t\tset_page_node(page, zone_to_nid(zone));\n 224:\t\t\tset_page_zone(page, zone_idx(zone));\n 225:\t\t\tprep_compound_tail(page, NULL, order);\n 226:\t\t\tif (zone_is_zone_device(zone))\n 227:\t\t\t\t__SetPageReserved(page);\n 228:\t\t}\n 229:\t\n 230:\t\tpage = virt_to_page(addr);\n 231:\t\tif (cmpxchg(\u0026pages[idx], NULL, page) != NULL) {\n 232:\t\t\tif (slab_is_available())\n 233:\t\t\t\t__free_page(page);\n 234:\t\t\telse\n 235:\t\t\t\tmemblock_free(addr, PAGE_SIZE);\n 236:\t\t\tpage = READ_ONCE(pages[idx]);\n 237:\t\t}\n 238:\t\n 239:\t\treturn page;\n 240:\t}\n 241:\t#endif\n 242:\t\n 243:\tstatic __meminit void *vmemmap_alloc_pte(unsigned long pfn, int node,\n 244:\t\t\tstruct vmem_altmap *altmap, unsigned long flags)\n"
}

9/2 2026/10/01 14:25 llm patch-evaluator
0m Model:
gemini-3.8-flash

Args:
null

Results:
null

Tokens:
							input: 26049
							output: 37
							thoughts: 17
						

Thoughts:
Okay, here's my attempt at summarizing the thought process, tailored for an expert audience, and written in the first person:

**Diving into `mm/sparse-vmemmap.c`**

Alright, let's get down to brass tacks. I need to take a closer look at what's going on within `mm/sparse-vmemmap.c`. This suggests we're dealing with the virtual memory map structures related to sparse memory configurations. Given my background, I'm already anticipating considerations around handling discontiguous physical memory regions and how the kernel maps these to a contiguous virtual address space. I'll be focused on how the `vmemmap` is structured, how it interacts with the page tables, and specifically how the code manages the allocation and deallocation of these virtual memory entries for physical pages that may not be directly contiguous. I'm keen to see if they're using a multi-level approach, possibly with a per-node or per-section granularity to reduce overhead, or if there is some other method being used. I'll be paying close attention to the lock contention aspects when dealing with multiple cpus. It will be interesting to see how the code handles NUMA-aware memory management within this context. Time to dig in and see how they've addressed the complexities of sparse memory layout.



10/2 2026/10/01 14:25 tool read-file
0m Args:
{
  "File": "mm/sparse-vmemmap.c",
  "FirstLine": 244,
  "LineCount": 80
}

Results:
{
  "Contents": " 244:\t\t\tstruct vmem_altmap *altmap, unsigned long flags)\n 245:\t{\n 246:\t\tstruct zone *zone;\n 247:\t\tstruct page *page;\n 248:\t\tconst unsigned int order = pfn_to_section_compound_order(pfn);\n 249:\t\n 250:\t\t/*\n 251:\t\t * Device DAX still relies on vmemmap_populate_compound_pages() for\n 252:\t\t * head/first-tail allocation and tail-page reuse.\n 253:\t\t */\n 254:\t\tif (!vmemmap_optimizable_pfn(pfn) || flags \u0026 VMEMMAP_POPULATE_DAX)\n 255:\t\t\treturn vmemmap_alloc_block_buf(PAGE_SIZE, node, altmap);\n 256:\t\n 257:\t\tzone = pfn_to_zone(pfn, node);\n 258:\t\tpage = vmemmap_shared_tail_page(order, zone);\n 259:\t\tif (!page)\n 260:\t\t\treturn NULL;\n 261:\t\n 262:\t\treturn page_address(page);\n 263:\t}\n 264:\t\n 265:\tstatic pte_t * __meminit vmemmap_pte_populate(pmd_t *pmd, unsigned long addr, int node,\n 266:\t\t\t\t\t       struct vmem_altmap *altmap,\n 267:\t\t\t\t\t       unsigned long ptpfn, unsigned long flags)\n 268:\t{\n 269:\t\tpte_t *pte = pte_offset_kernel(pmd, addr);\n 270:\t\tunsigned long pfn = page_to_pfn((struct page *)addr);\n 271:\t\n 272:\t\tif (pte_none(ptep_get(pte))) {\n 273:\t\t\tpte_t entry;\n 274:\t\n 275:\t\t\tif (ptpfn == (unsigned long)-1) {\n 276:\t\t\t\tvoid *p = vmemmap_alloc_pte(pfn, node, altmap, flags);\n 277:\t\n 278:\t\t\t\tif (!p)\n 279:\t\t\t\t\treturn NULL;\n 280:\t\t\t\tptpfn = PHYS_PFN(__pa(p));\n 281:\t\t\t} else {\n 282:\t\t\t\t/*\n 283:\t\t\t\t * When a PTE/PMD entry is freed from the init_mm\n 284:\t\t\t\t * there's a free_pages() call to this page allocated\n 285:\t\t\t\t * above. Thus this try_get_page() is paired with the\n 286:\t\t\t\t * put_page_testzero() on the freeing path.\n 287:\t\t\t\t * This can only called by certain ZONE_DEVICE path,\n 288:\t\t\t\t * and through vmemmap_populate_compound_pages() when\n 289:\t\t\t\t * slab is available.\n 290:\t\t\t\t *\n 291:\t\t\t\t * Use try_get_page() to prevent the shared page refcount\n 292:\t\t\t\t * from overflowing.\n 293:\t\t\t\t */\n 294:\t\t\t\tif ((flags \u0026 VMEMMAP_POPULATE_DAX) \u0026\u0026\n 295:\t\t\t\t    !try_get_page(pfn_to_page(ptpfn)))\n 296:\t\t\t\t\treturn NULL;\n 297:\t\t\t}\n 298:\t\t\tentry = pfn_pte(ptpfn, PAGE_KERNEL);\n 299:\t\t\tset_pte_at(\u0026init_mm, addr, pte, entry);\n 300:\t\t} else if (WARN_ON_ONCE(vmemmap_optimizable_pfn(pfn)))\n 301:\t\t\treturn NULL;\n 302:\t\treturn pte;\n 303:\t}\n 304:\t\n 305:\tstatic pmd_t * __meminit vmemmap_pmd_populate(pud_t *pud, unsigned long addr, int node)\n 306:\t{\n 307:\t\tpmd_t *pmd = pmd_offset(pud, addr);\n 308:\t\tif (pmd_none(*pmd)) {\n 309:\t\t\tvoid *p = vmemmap_alloc_block_zero(PAGE_SIZE, node);\n 310:\t\t\tif (!p)\n 311:\t\t\t\treturn NULL;\n 312:\t\t\tkernel_pte_init(p);\n 313:\t\t\tpmd_populate_kernel(\u0026init_mm, pmd, p);\n 314:\t\t}\n 315:\t\treturn pmd;\n 316:\t}\n 317:\t\n 318:\tstatic pud_t * __meminit vmemmap_pud_populate(p4d_t *p4d, unsigned long addr, int node)\n 319:\t{\n 320:\t\tpud_t *pud = pud_offset(p4d, addr);\n 321:\t\tif (pud_none(*pud)) {\n 322:\t\t\tvoid *p = vmemmap_alloc_block_zero(PAGE_SIZE, node);\n 323:\t\t\tif (!p)\n"
}

11/2 2026/10/01 14:25 llm patch-evaluator
0m Model:
gemini-3.8-flash

Args:
null

Results:
null

Tokens:
							input: 27714
							output: 37
							thoughts: 27
						

Thoughts:
Okay, here's my take on those lines in `mm/sparse-vmemmap.c`, expressed as a first-person summary, and tailored for someone who knows their way around memory management:

**Diving into `mm/sparse-vmemmap.c` - Lines 525-600**

Alright, let's zero in on lines 525 through 600 in `mm/sparse-vmemmap.c`. I need to refresh my memory on what's happening in this section of the code, so I'll lay out my thoughts as I read:

My immediate impression is, this segment looks focused on handling the allocation and deallocation of the virtual memory maps used in sparse memory implementations. We're obviously in the weeds here, dealing with the nitty-gritty details of how the kernel manages its view of memory when physical memory is not contiguous.

I need to confirm, but I believe we're likely in a function involved in either creating or destroying the vmemmap structures. It is highly probable, as the name of the file suggests, that this code will deal with sparse memory maps in virtual memory. It seems like it's crucial for efficiently representing large, non-contiguous physical memory regions in a virtual address space.

As I start to read through the code, I expect to see logic relating to mapping page frames to virtual addresses, managing the allocation and deallocation of vmemmap pages themselves (which act as the metadata for the physical pages), and likely handling potential race conditions given the multi-threaded nature of the kernel.

I'll be looking for any indications of spinlocks or other synchronization mechanisms to ensure that concurrent accesses to the vmemmap structures are handled safely. I'll need to see how the kernel deals with both page-level and section-level mappings, if applicable. My intuition tells me that the code probably needs to consider the specific hardware architecture, and the page table formats it uses.

I'm ready to dive into the code now to verify these initial thoughts, noting, in particular, the interplay between the vmemmap and the actual physical memory pages. I want to see how the code handles different page sizes and NUMA nodes. I'm keen to see the exact algorithms used for mapping and unmapping, and how the kernel handles errors or allocation failures within this critical code path.



12/2 2026/10/01 14:26 tool read-file
0m Args:
{
  "File": "mm/sparse-vmemmap.c",
  "FirstLine": 525,
  "LineCount": 75
}

Results:
{
  "Contents": " 525:\t\t\tPHYS_PFN(pgmap-\u003eranges[pgmap-\u003enr_range].start);\n 526:\t\n 527:\t\treturn !IS_ALIGNED(offset, nr_pages) \u0026\u0026 nr_pages \u003e PAGES_PER_SUBSECTION;\n 528:\t}\n 529:\t\n 530:\tstatic int __meminit vmemmap_populate_compound_pages(unsigned long start_pfn,\n 531:\t\t\t\t\t\t\t     unsigned long start,\n 532:\t\t\t\t\t\t\t     unsigned long end, int node,\n 533:\t\t\t\t\t\t\t     struct dev_pagemap *pgmap)\n 534:\t{\n 535:\t\tconst unsigned long flags = VMEMMAP_POPULATE_DAX;\n 536:\t\tconst unsigned int order = pfn_to_section_compound_order(start_pfn);\n 537:\t\tunsigned long size, addr;\n 538:\t\tpte_t *pte;\n 539:\t\tstruct page *page;\n 540:\t\tint rc;\n 541:\t\n 542:\t\tpage = vmemmap_shared_tail_page(order, device_zone(node));\n 543:\t\tif (!page)\n 544:\t\t\treturn -ENOMEM;\n 545:\t\n 546:\t\tif (reuse_compound_section(start_pfn, pgmap))\n 547:\t\t\treturn vmemmap_populate_range(start, end, node, NULL,\n 548:\t\t\t\t\t\t      page_to_pfn(page), flags);\n 549:\t\n 550:\t\tsize = min(end - start, (1UL \u003c\u003c order) * sizeof(struct page));\n 551:\t\tfor (addr = start; addr \u003c end; addr += size) {\n 552:\t\t\tunsigned long next, last = addr + size;\n 553:\t\n 554:\t\t\t/* Populate the head page vmemmap page */\n 555:\t\t\tpte = vmemmap_populate_address(addr, node, NULL, -1, flags);\n 556:\t\t\tif (!pte)\n 557:\t\t\t\treturn -ENOMEM;\n 558:\t\n 559:\t\t\t/*\n 560:\t\t\t * Reuse the shared page for the rest of tail pages\n 561:\t\t\t * See layout diagram in Documentation/mm/vmemmap_dedup.rst\n 562:\t\t\t */\n 563:\t\t\tnext = addr + PAGE_SIZE;\n 564:\t\t\trc = vmemmap_populate_range(next, last, node, NULL,\n 565:\t\t\t\t\t\t    page_to_pfn(page), flags);\n 566:\t\t\tif (rc)\n 567:\t\t\t\treturn -ENOMEM;\n 568:\t\t}\n 569:\t\n 570:\t\treturn 0;\n 571:\t}\n 572:\t\n 573:\t#endif\n 574:\t\n 575:\tstruct page * __meminit __populate_section_memmap(unsigned long pfn,\n 576:\t\t\tunsigned long nr_pages, int nid, struct vmem_altmap *altmap,\n 577:\t\t\tstruct dev_pagemap *pgmap)\n 578:\t{\n 579:\t\tunsigned long start = (unsigned long) pfn_to_page(pfn);\n 580:\t\tunsigned long end = start + nr_pages * sizeof(struct page);\n 581:\t\tint r;\n 582:\t\n 583:\t\tif (WARN_ON_ONCE(!IS_ALIGNED(pfn, PAGES_PER_SUBSECTION) ||\n 584:\t\t\t!IS_ALIGNED(nr_pages, PAGES_PER_SUBSECTION)))\n 585:\t\t\treturn NULL;\n 586:\t\n 587:\t\tif (pgmap \u0026\u0026 section_vmemmap_optimizable(__pfn_to_section(pfn)))\n 588:\t\t\tr = vmemmap_populate_compound_pages(pfn, start, end, nid, pgmap);\n 589:\t\telse\n 590:\t\t\tr = vmemmap_populate(start, end, nid, altmap);\n 591:\t\n 592:\t\tif (r \u003c 0)\n 593:\t\t\treturn NULL;\n 594:\t\n 595:\t\tflush_cache_vmap(start, end);\n 596:\t\n 597:\t\treturn pfn_to_page(pfn);\n 598:\t}\n 599:\t\n"
}

13/2 2026/10/01 14:26 llm patch-evaluator
0m Model:
gemini-3.8-flash

Args:
null

Results:
null

Tokens:
							input: 29437
							output: 37
							thoughts: 62
						

Thoughts:
Here's my summary, as if I were thinking it:

**Examining DAX Optimization in `mm/sparse-vmemmap.c`**

Alright, let's dive into the kernel code related to DAX and virtual memory mapping. I'm focusing on optimization, so I need to check how the system handles the vmemmap, specifically within the context of sparse memory maps. I see that the function `vmemmap_populate_compound_pages` in `mm/sparse-vmemmap.c` is conditionally compiled with `#ifdef CONFIG_ARCH_WANT_OPTIMIZE_DAX_VMEMMAP`. That's a clear indicator that this might be where some interesting DAX-specific performance tuning is happening. My instinct tells me this is the critical section to examine. I'll need to drill down into the code between lines 500 and 530 to understand exactly how that optimization is implemented and if it aligns with my current understanding of the DAX and vmemmap interactions. This should reveal how the system optimizes DAX in this vmemmap context.



14/2 2026/10/01 14:26 tool read-file
0m Args:
{
  "File": "mm/sparse-vmemmap.c",
  "FirstLine": 500,
  "LineCount": 30
}

Results:
{
  "Contents": " 500:\t\t\t\t\treturn -EOPNOTSUPP;\n 501:\t\t\t\tcontinue;\n 502:\t\t\t}\n 503:\t\t\tif (vmemmap_populate_basepages(addr, next, node, altmap))\n 504:\t\t\t\treturn -ENOMEM;\n 505:\t\t}\n 506:\t\treturn 0;\n 507:\t}\n 508:\t\n 509:\t#ifndef vmemmap_populate_compound_pages\n 510:\t/*\n 511:\t * For compound pages bigger than section size (e.g. x86 1G compound\n 512:\t * pages with 2M subsection size) fill the rest of sections as tail\n 513:\t * pages.\n 514:\t *\n 515:\t * Note that memremap_pages() resets @nr_range value and will increment\n 516:\t * it after each range successful onlining. Thus the value or @nr_range\n 517:\t * at section memmap populate corresponds to the in-progress range\n 518:\t * being onlined here.\n 519:\t */\n 520:\tstatic bool __meminit reuse_compound_section(unsigned long start_pfn,\n 521:\t\t\t\t\t\t     struct dev_pagemap *pgmap)\n 522:\t{\n 523:\t\tunsigned long nr_pages = pgmap_vmemmap_nr(pgmap);\n 524:\t\tunsigned long offset = start_pfn -\n 525:\t\t\tPHYS_PFN(pgmap-\u003eranges[pgmap-\u003enr_range].start);\n 526:\t\n 527:\t\treturn !IS_ALIGNED(offset, nr_pages) \u0026\u0026 nr_pages \u003e PAGES_PER_SUBSECTION;\n 528:\t}\n 529:\t\n"
}

15/2 2026/10/01 14:26 llm patch-evaluator
0m Model:
gemini-3.8-flash

Args:
null

Results:
null

Tokens:
							input: 30243
							output: 37
							thoughts: 22
						

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

**Diving into Sparse Vmemmap Activation**

Alright, let's zero in on `section_activate` within `mm/sparse-vmemmap.c`. I need to refresh my understanding of how we're handling the activation of sections within the sparse vmemmap framework. I know the core concept, but I'm thinking I should take a fresh look at this particular function and its interaction with the broader memory management landscape. I want to solidify the mechanics, focusing on when and how this routine is triggered, especially in different boot scenarios or when dealing with dynamic memory allocation. Understanding this will help me when I troubleshoot more complex memory-related issues.



16/2 2026/10/01 14:26 tool read-file
0m Args:
{
  "File": "mm/sparse-vmemmap.c",
  "FirstLine": 800,
  "LineCount": 60
}

Results:
{
  "Contents": " 800:\t\tif (!section_is_early)\n 801:\t\t\tdepopulate_section_memmap(pfn, nr_pages, altmap, pgmap);\n 802:\t\telse if (memmap)\n 803:\t\t\tfree_map_bootmem(memmap);\n 804:\t\n 805:\t\tif (empty) {\n 806:\t\t\tms-\u003esection_mem_map = (unsigned long)NULL;\n 807:\t\t\tsection_set_compound_order(ms, 0);\n 808:\t\t}\n 809:\t}\n 810:\t\n 811:\tstatic struct page * __meminit section_activate(int nid, unsigned long pfn,\n 812:\t\t\tunsigned long nr_pages, struct vmem_altmap *altmap,\n 813:\t\t\tstruct dev_pagemap *pgmap)\n 814:\t{\n 815:\t\tstruct mem_section *ms = __pfn_to_section(pfn);\n 816:\t\tstruct mem_section_usage *usage = NULL;\n 817:\t\tstruct page *memmap;\n 818:\t\tunsigned int order;\n 819:\t\tint rc;\n 820:\t\n 821:\t\torder = vmemmap_can_optimize(altmap, pgmap) ? pgmap-\u003evmemmap_shift : 0;\n 822:\t\tif (nr_pages \u003c PAGES_PER_SECTION \u0026\u0026 section_compound_order(ms))\n 823:\t\t\treturn ERR_PTR(-EOPNOTSUPP);\n 824:\t\n 825:\t\tif (!ms-\u003eusage) {\n 826:\t\t\tusage = kzalloc(mem_section_usage_size(), GFP_KERNEL);\n 827:\t\t\tif (!usage)\n 828:\t\t\t\treturn ERR_PTR(-ENOMEM);\n 829:\t\t\tms-\u003eusage = usage;\n 830:\t\t}\n 831:\t\n 832:\t\trc = fill_subsection_map(pfn, nr_pages);\n 833:\t\tif (rc) {\n 834:\t\t\tif (usage)\n 835:\t\t\t\tms-\u003eusage = NULL;\n 836:\t\t\tkfree(usage);\n 837:\t\t\treturn ERR_PTR(rc);\n 838:\t\t}\n 839:\t\n 840:\t\t/*\n 841:\t\t * The early init code does not consider partially populated\n 842:\t\t * initial sections, it simply assumes that memory will never be\n 843:\t\t * referenced.  If we hot-add memory into such a section then we\n 844:\t\t * do not need to populate the memmap and can simply reuse what\n 845:\t\t * is already there.\n 846:\t\t */\n 847:\t\tif (nr_pages \u003c PAGES_PER_SECTION \u0026\u0026 early_section(ms))\n 848:\t\t\treturn pfn_to_page(pfn);\n 849:\t\n 850:\t\tsection_set_compound_order_range(pfn, nr_pages, order);\n 851:\t\tmemmap = populate_section_memmap(pfn, nr_pages, nid, altmap, pgmap);\n 852:\t\tif (!memmap) {\n 853:\t\t\tsection_deactivate(pfn, nr_pages, altmap, pgmap);\n 854:\t\t\treturn ERR_PTR(-ENOMEM);\n 855:\t\t}\n 856:\t\n 857:\t\treturn memmap;\n 858:\t}\n 859:\t\n"
}

17/2 2026/10/01 14:26 llm patch-evaluator
0m Model:
gemini-3.8-flash

Args:
null

Results:
null

Tokens:
							input: 31374
							output: 33
							thoughts: 158
						

Thoughts:
**Analyzing HugeTLB and Memory Hotplug's Interaction with Vmemmap Optimization**

Okay, let's break this down. My initial question is how `HugeTLB` and memory hotplug utilize these specific functions. I'm focusing on the interaction with the vmemmap. I see a relevant function, `__hugetlb_vmemmap_optimize_folio`, in `mm/hugetlb_vmemmap.c`.

Specifically, I observe this line within it:

```c
vmemmap_tail = vmemmap_shared_tail_page(h->order, folio_zone(folio));
```

This is triggered when a HugeTLB page is allocated, *provided* HVO (HugeTLB vmemmap Optimization) is active, which is, importantly, the default behavior or enabled through the sysctl/kernel parameter `hugetlb_free_vmemmap`. So, this optimization *should* be at play when memory is handled.

Now, my instinct is to dig deeper. I need to understand the larger context. Let me immediately check the other places where `__hugetlb_vmemmap_optimize_folio` is called. That's the next logical step.



18/2 2026/10/01 14:27 tool grepper
0m Args:
{
  "Expression": "hugetlb_vmemmap_optimize_folio",
  "PathPrefix": "mm/"
}

Results:
{
  "Output": "mm/hugetlb.c=1869=static struct folio *alloc_fresh_hugetlb_folio(struct hstate *h,\n--\nmm/hugetlb.c-1875-\tif (folio)\nmm/hugetlb.c:1876:\t\thugetlb_vmemmap_optimize_folio(h, folio);\nmm/hugetlb.c-1877-\treturn folio;\n--\nmm/hugetlb.c=1880=void prep_and_add_allocated_folios(struct hstate *h,\n--\nmm/hugetlb.c-1886-\t/* Send list for bulk vmemmap optimization processing */\nmm/hugetlb.c:1887:\thugetlb_vmemmap_optimize_folios(h, folio_list);\nmm/hugetlb.c-1888-\n--\nmm/hugetlb_vmemmap.c=394=static int __hugetlb_vmemmap_restore_folio(const struct hstate *h,\n--\nmm/hugetlb_vmemmap.c-425- * hugetlb_vmemmap_restore_folio - restore previously optimized (by\nmm/hugetlb_vmemmap.c:426: *\t\t\t\thugetlb_vmemmap_optimize_folio()) vmemmap pages which\nmm/hugetlb_vmemmap.c-427- *\t\t\t\twill be reallocated and remapped.\n--\nmm/hugetlb_vmemmap.c=481=static bool vmemmap_should_optimize_folio(const struct hstate *h, struct folio *folio)\n--\nmm/hugetlb_vmemmap.c-494-\nmm/hugetlb_vmemmap.c:495:static int __hugetlb_vmemmap_optimize_folio(const struct hstate *h,\nmm/hugetlb_vmemmap.c-496-\t\t\t\t\t    struct folio *folio,\n--\nmm/hugetlb_vmemmap.c-556-/**\nmm/hugetlb_vmemmap.c:557: * hugetlb_vmemmap_optimize_folio - optimize @folio's vmemmap pages.\nmm/hugetlb_vmemmap.c-558- * @h:\t\tstruct hstate.\n--\nmm/hugetlb_vmemmap.c-565- */\nmm/hugetlb_vmemmap.c:566:void hugetlb_vmemmap_optimize_folio(const struct hstate *h, struct folio *folio)\nmm/hugetlb_vmemmap.c-567-{\n--\nmm/hugetlb_vmemmap.c-569-\nmm/hugetlb_vmemmap.c:570:\t__hugetlb_vmemmap_optimize_folio(h, folio, \u0026vmemmap_pages, 0);\nmm/hugetlb_vmemmap.c-571-\tfree_vmemmap_page_list(\u0026vmemmap_pages);\n--\nmm/hugetlb_vmemmap.c=574=static int hugetlb_vmemmap_split_folio(const struct hstate *h, struct folio *folio)\n--\nmm/hugetlb_vmemmap.c-590-\nmm/hugetlb_vmemmap.c:591:static void __hugetlb_vmemmap_optimize_folios(struct hstate *h,\nmm/hugetlb_vmemmap.c-592-\t\t\t\t\t      struct list_head *folio_list,\n--\nmm/hugetlb_vmemmap.c-637-\t\t * there is a mix of pre-HVO and not yet HVO-ed folios\nmm/hugetlb_vmemmap.c:638:\t\t * here, as __hugetlb_vmemmap_optimize_folio() will\nmm/hugetlb_vmemmap.c-639-\t\t * skip any folios that already have the optimized flag\n--\nmm/hugetlb_vmemmap.c-648-\nmm/hugetlb_vmemmap.c:649:\t\tret = __hugetlb_vmemmap_optimize_folio(h, folio, \u0026vmemmap_pages, flags);\nmm/hugetlb_vmemmap.c-650-\n--\nmm/hugetlb_vmemmap.c-655-\t\t * halfway and head page allocation also failed. In this\nmm/hugetlb_vmemmap.c:656:\t\t * case __hugetlb_vmemmap_optimize_folio() would free memory\nmm/hugetlb_vmemmap.c-657-\t\t * allowing more vmemmap remaps to occur.\n--\nmm/hugetlb_vmemmap.c-662-\t\t\tINIT_LIST_HEAD(\u0026vmemmap_pages);\nmm/hugetlb_vmemmap.c:663:\t\t\t__hugetlb_vmemmap_optimize_folio(h, folio, \u0026vmemmap_pages, flags);\nmm/hugetlb_vmemmap.c-664-\t\t}\n--\nmm/hugetlb_vmemmap.c-671-\nmm/hugetlb_vmemmap.c:672:void hugetlb_vmemmap_optimize_folios(struct hstate *h, struct list_head *folio_list)\nmm/hugetlb_vmemmap.c-673-{\nmm/hugetlb_vmemmap.c:674:\t__hugetlb_vmemmap_optimize_folios(h, folio_list, false);\nmm/hugetlb_vmemmap.c-675-}\n--\nmm/hugetlb_vmemmap.c=677=void hugetlb_vmemmap_optimize_bootmem_folios(struct hstate *h, struct list_head *folio_list)\nmm/hugetlb_vmemmap.c-678-{\nmm/hugetlb_vmemmap.c:679:\t__hugetlb_vmemmap_optimize_folios(h, folio_list, true);\nmm/hugetlb_vmemmap.c-680-}\n--\nmm/hugetlb_vmemmap.h=22=long hugetlb_vmemmap_restore_folios(const struct hstate *h,\n--\nmm/hugetlb_vmemmap.h-24-\t\t\t\t\tstruct list_head *non_hvo_folios);\nmm/hugetlb_vmemmap.h:25:void hugetlb_vmemmap_optimize_folio(const struct hstate *h, struct folio *folio);\nmm/hugetlb_vmemmap.h:26:void hugetlb_vmemmap_optimize_folios(struct hstate *h, struct list_head *folio_list);\nmm/hugetlb_vmemmap.h-27-void hugetlb_vmemmap_optimize_bootmem_folios(struct hstate *h, struct list_head *folio_list);\n--\nmm/hugetlb_vmemmap.h=53=static inline long hugetlb_vmemmap_restore_folios(const struct hstate *h,\n--\nmm/hugetlb_vmemmap.h-60-\nmm/hugetlb_vmemmap.h:61:static inline void hugetlb_vmemmap_optimize_folio(const struct hstate *h, struct folio *folio)\nmm/hugetlb_vmemmap.h-62-{\n--\nmm/hugetlb_vmemmap.h-64-\nmm/hugetlb_vmemmap.h:65:static inline void hugetlb_vmemmap_optimize_folios(struct hstate *h, struct list_head *folio_list)\nmm/hugetlb_vmemmap.h-66-{\n"
}

19/2 2026/10/01 14:27 llm patch-evaluator
1m Model:
gemini-3.8-flash

Args:
null

Results:
null

Tokens:
							input: 33744
							output: 25
							thoughts: 32
						

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

**Investigating Potential Interactions with Section Activation and Device Memory**

Right, time to dig deeper. That `section_activate` function... I need to see what else in the kernel is calling it. It's too important to leave any stone unturned, especially given my concerns around potential interactions with memory hotplug or ZONE_DEVICE. A simple grep won't cut it; I need a comprehensive view. So, I'll start with a focused search for all instances of `section_activate` within the relevant code base. This is the first step towards understanding the complete call graph and identifying potential conflict points or opportunities for optimization. I'll also be mindful of indirect calls through macros or inline functions - those often get missed in a naive search. The goal is a clear picture before I delve further into the code. Let's start with that search for `section_activate` and see what emerges.



20/2 2026/10/01 14:28 tool grepper
0m Args:
{
  "Expression": "section_activate",
  "PathPrefix": "mm/"
}

Results:
{
  "Output": "mm/sparse-vmemmap.c=762=static void section_deactivate(unsigned long pfn, unsigned long nr_pages,\n--\nmm/sparse-vmemmap.c-797-\t * The memmap of early sections is always fully populated. See\nmm/sparse-vmemmap.c:798:\t * section_activate() and pfn_valid() .\nmm/sparse-vmemmap.c-799-\t */\n--\nmm/sparse-vmemmap.c-810-\nmm/sparse-vmemmap.c:811:static struct page * __meminit section_activate(int nid, unsigned long pfn,\nmm/sparse-vmemmap.c-812-\t\tunsigned long nr_pages, struct vmem_altmap *altmap,\n--\nmm/sparse-vmemmap.c=879=int __meminit sparse_add_section(int nid, unsigned long start_pfn,\n--\nmm/sparse-vmemmap.c-891-\nmm/sparse-vmemmap.c:892:\tmemmap = section_activate(nid, start_pfn, nr_pages, altmap, pgmap);\nmm/sparse-vmemmap.c-893-\tif (IS_ERR(memmap))\n"
}

21/2 2026/10/01 14:28 llm patch-evaluator
0m Model:
gemini-3.8-flash

Args:
null

Results:
null

Tokens:
							input: 34329
							output: 19
							thoughts: 11
						

Thoughts:
Okay, here's my summary, tailored for an expert audience, as if I'm thinking these things myself:

**Investigating the Call Hierarchy of `sparse_add_section`**

Right, so I need to understand what's triggering `sparse_add_section`. It’s a key function, and understanding its callers is crucial for figuring out the overall flow and potential performance bottlenecks. I need to trace back through the call stack to see where this function is being invoked. Is it part of the data ingestion pipeline? Perhaps it's triggered during a specific operation on the sparse data structures, like resizing or re-sharding? Is it coming from the serialization/deserialization logic when dealing with disk storage? Or maybe this section is responsible for the actual "add" and is invoked from a series of sections?

I need to quickly identify the calling functions and their relation to the overall program structure. I'll need to use debugging tools, static analysis, or a combination of both to build a mental map of this section of the code. Finding the entry points is the first step.



22/2 2026/10/01 14:29 tool grepper
0m Args:
{
  "Expression": "sparse_add_section"
}

Results:
{
  "Output": "include/linux/memory_hotplug.h=316=extern void remove_pfn_range_from_zone(struct zone *zone,\n--\ninclude/linux/memory_hotplug.h-318-\t\t\t\t       unsigned long nr_pages);\ninclude/linux/memory_hotplug.h:319:extern int sparse_add_section(int nid, unsigned long pfn,\ninclude/linux/memory_hotplug.h-320-\t\tunsigned long nr_pages, struct vmem_altmap *altmap,\n--\nmm/memory_hotplug.c=385=int __add_pages(int nid, unsigned long pfn, unsigned long nr_pages,\n--\nmm/memory_hotplug.c-418-\t\t\t\t   SECTION_ALIGN_UP(pfn + 1) - pfn);\nmm/memory_hotplug.c:419:\t\terr = sparse_add_section(nid, pfn, cur_nr_pages, altmap,\nmm/memory_hotplug.c-420-\t\t\t\t\t params-\u003epgmap);\n--\nmm/sparse-vmemmap.c=811=static struct page * __meminit section_activate(int nid, unsigned long pfn,\n--\nmm/sparse-vmemmap.c-860-/**\nmm/sparse-vmemmap.c:861: * sparse_add_section - add a memory section, or populate an existing one\nmm/sparse-vmemmap.c-862- * @nid: The node to add section on\n--\nmm/sparse-vmemmap.c-878- */\nmm/sparse-vmemmap.c:879:int __meminit sparse_add_section(int nid, unsigned long start_pfn,\nmm/sparse-vmemmap.c-880-\t\tunsigned long nr_pages, struct vmem_altmap *altmap,\n"
}

23/2 2026/10/01 14:29 llm patch-evaluator
1m Model:
gemini-3.8-flash

Args:
null

Results:
null

Tokens:
							input: 35047
							output: 19
							thoughts: 9
						

Thoughts:
Okay, here's my attempt at summarizing the thought process, tailored for an expert audience, and written in the first person:

**Digging into the Page Addition Process**

Alright, I'm thinking about this `__add_pages` method. My immediate question, of course, is: **what's *calling* it?** Understanding the call stack is absolutely crucial here. Without knowing the context of where this method is being triggered, I'm flying blind. Is it a direct user action? Part of an automated process? Is it triggered by some event in another object? Is it related to caching, or memory management, or maybe even error handling? The caller holds the key to the *why* this operation is occurring. I need to trace back from that method to identify the initiation point of the process. I am certain I could provide a good answer for you if I could see that function.

From there I could get the bigger picture.



24/2 2026/10/01 14:30 tool grepper
0m Args:
{
  "Expression": "__add_pages\\("
}

Results:
{
  "Output": "arch/arm64/mm/mmu.c=2007=int arch_add_memory(int nid, u64 start, u64 size,\n--\narch/arm64/mm/mmu.c-2024-\narch/arm64/mm/mmu.c:2025:\tret = __add_pages(nid, start \u003e\u003e PAGE_SHIFT, size \u003e\u003e PAGE_SHIFT,\narch/arm64/mm/mmu.c-2026-\t\t\t   params);\n--\narch/loongarch/mm/init.c=107=int arch_add_memory(int nid, u64 start, u64 size, struct mhp_params *params)\n--\narch/loongarch/mm/init.c-112-\narch/loongarch/mm/init.c:113:\tret = __add_pages(nid, start_pfn, nr_pages, params);\narch/loongarch/mm/init.c-114-\narch/loongarch/mm/init.c-115-\tif (ret)\narch/loongarch/mm/init.c:116:\t\tpr_warn(\"%s: Problem encountered in __add_pages() as ret=%d\\n\",\narch/loongarch/mm/init.c-117-\t\t\t\t__func__,  ret);\n--\narch/powerpc/mm/mem.c=129=int __ref add_pages(int nid, unsigned long start_pfn, unsigned long nr_pages,\n--\narch/powerpc/mm/mem.c-133-\narch/powerpc/mm/mem.c:134:\tret = __add_pages(nid, start_pfn, nr_pages, params);\narch/powerpc/mm/mem.c-135-\tif (ret)\n--\narch/riscv/mm/init.c=1724=int __ref arch_add_memory(int nid, u64 start, u64 size, struct mhp_params *params)\n--\narch/riscv/mm/init.c-1728-\tcreate_linear_mapping_range(start, start + size, 0, \u0026params-\u003epgprot);\narch/riscv/mm/init.c:1729:\tret = __add_pages(nid, start \u003e\u003e PAGE_SHIFT, size \u003e\u003e PAGE_SHIFT, params);\narch/riscv/mm/init.c-1730-\tif (ret) {\n--\narch/s390/mm/init.c=275=int arch_add_memory(int nid, u64 start, u64 size,\n--\narch/s390/mm/init.c-289-\narch/s390/mm/init.c:290:\trc = __add_pages(nid, start_pfn, size_pages, params);\narch/s390/mm/init.c-291-\tif (rc)\n--\narch/x86/mm/init_64.c=963=int add_pages(int nid, unsigned long start_pfn, unsigned long nr_pages,\n--\narch/x86/mm/init_64.c-971-\narch/x86/mm/init_64.c:972:\tret = __add_pages(nid, start_pfn, nr_pages, params);\narch/x86/mm/init_64.c-973-\tWARN_ON_ONCE(ret);\n--\ninclude/linux/memory_hotplug.h=141=extern void __remove_pages(unsigned long start_pfn, unsigned long nr_pages,\n--\ninclude/linux/memory_hotplug.h-144-/* reasonably generic interface to expand the physical pages */\ninclude/linux/memory_hotplug.h:145:extern int __add_pages(int nid, unsigned long start_pfn, unsigned long nr_pages,\ninclude/linux/memory_hotplug.h-146-\t\t       struct mhp_params *params);\n--\ninclude/linux/memory_hotplug.h=149=static inline int add_pages(int nid, unsigned long start_pfn,\n--\ninclude/linux/memory_hotplug.h-151-{\ninclude/linux/memory_hotplug.h:152:\treturn __add_pages(nid, start_pfn, nr_pages, params);\ninclude/linux/memory_hotplug.h-153-}\n--\nmm/memory_hotplug.c=383=EXPORT_SYMBOL_GPL(pfn_to_online_page);\nmm/memory_hotplug.c-384-\nmm/memory_hotplug.c:385:int __add_pages(int nid, unsigned long pfn, unsigned long nr_pages,\nmm/memory_hotplug.c-386-\t\tstruct mhp_params *params)\n"
}

25/2 2026/10/01 14:30 llm patch-evaluator
1m Model:
gemini-3.8-flash

Args:
null

Results:
null

Tokens:
							input: 36486
							output: 26
							thoughts: 52
						

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

**Diving into Memory Management for Hotplugging**

Okay, so I need to get my head around memory hotplugging, particularly how it interacts with different memory types. Right, `add_pages` – that's definitely a core function in the hotplug path, residing in `mm/`. And of course, the underlying mechanisms must involve `memory hotplug` itself, also in `mm/`. Now, where does `devm_memremap_pages` fit into this? And ZONE_DEVICE, that's crucial when we're talking about device memory, dax, and also hugetlb. They're all in the same area. Let's make sure I really understand what `memremap_pages` is actually doing in this process. Time to dig in a bit deeper there.



26/2 2026/10/01 14:31 tool grepper
0m Args:
{
  "Expression": "add_pages\\(",
  "PathPrefix": "mm/"
}

Results:
{
  "Output": "mm/memory_hotplug.c=383=EXPORT_SYMBOL_GPL(pfn_to_online_page);\nmm/memory_hotplug.c-384-\nmm/memory_hotplug.c:385:int __add_pages(int nid, unsigned long pfn, unsigned long nr_pages,\nmm/memory_hotplug.c-386-\t\tstruct mhp_params *params)\n--\nmm/memremap.c=144=static int pagemap_range(struct dev_pagemap *pgmap, struct mhp_params *params,\n--\nmm/memremap.c-200-\t/*\nmm/memremap.c:201:\t * For device private memory we call add_pages() as we only need to\nmm/memremap.c-202-\t * allocate and initialize struct page for the device memory. More-\n--\nmm/memremap.c-211-\tif (is_private) {\nmm/memremap.c:212:\t\terror = add_pages(nid, PHYS_PFN(range-\u003estart),\nmm/memremap.c-213-\t\t\t\tPHYS_PFN(range_len(range)), params);\n"
}

27/2 2026/10/01 14:31 llm patch-evaluator
0m Model:
gemini-3.8-flash

Args:
null

Results:
null

Tokens:
							input: 37061
							output: 19
							thoughts: 30
						

Thoughts:
Okay, here's my interpretation of that thought process, assuming I'm already deep in kernel memory management.

**Investigating the Caller Chain of Memory Remapping Functions**

Right, so I'm trying to trace the path, the *who* and *why*, behind these memory remapping operations. Specifically, I'm thinking about how the system got to the point where it's calling `memremap_pages` or `pagemap_range`. These are low-level and important functions, so understanding their context is crucial for debugging, performance tuning, or just plain old comprehension of the code.

My train of thought is to pick one of the functions for a deeper dive. Let's start with `memremap_pages`. I need to figure out the calling hierarchy. I want to know what functions, processes, or kernel subsystems are responsible for initiating these remapping events. This is the first question I need answered to gain a better understanding. This will help inform subsequent investigations as needed.



28/2 2026/10/01 14:32 tool grepper
0m Args:
{
  "Expression": "arch_add_memory"
}

Results:
{
  "Output": "Documentation/mm/memory-model.rst=155=this lack of user-api constraint to allow sub-section sized memory\nDocumentation/mm/memory-model.rst:156:ranges to be specified to :c:func:`arch_add_memory`, the top-half of\nDocumentation/mm/memory-model.rst-157-memory hotplug. Sub-section support allows for 2MB as the cross-arch\n--\nDocumentation/translations/zh_CN/mm/memory-model.rst=107=ZONE_DEVICE\n--\nDocumentation/translations/zh_CN/mm/memory-model.rst-121-上。这个实现依赖于这种缺乏用户接口的约束,允许子段大小的内存范围被指定给\nDocumentation/translations/zh_CN/mm/memory-model.rst:122::c:func:`arch_add_memory` ,即内存热插拔的上半部分。子段支持允许2MB作为\nDocumentation/translations/zh_CN/mm/memory-model.rst-123-:c:func:`devm_memremap_pages` 的跨架构通用对齐颗粒度。\n--\narch/arm64/mm/mmu.c=1975=struct range arch_get_mappable_range(void)\n--\narch/arm64/mm/mmu.c-2006-\narch/arm64/mm/mmu.c:2007:int arch_add_memory(int nid, u64 start, u64 size,\narch/arm64/mm/mmu.c-2008-\t\t    struct mhp_params *params)\n--\narch/loongarch/mm/init.c=64=void __init fixrange_init(unsigned long start, unsigned long end, pgd_t *pgd_base)\n--\narch/loongarch/mm/init.c-106-#ifdef CONFIG_MEMORY_HOTPLUG\narch/loongarch/mm/init.c:107:int arch_add_memory(int nid, u64 start, u64 size, struct mhp_params *params)\narch/loongarch/mm/init.c-108-{\n--\narch/powerpc/mm/mem.c=129=int __ref add_pages(int nid, unsigned long start_pfn, unsigned long nr_pages,\n--\narch/powerpc/mm/mem.c-144-\narch/powerpc/mm/mem.c:145:int __ref arch_add_memory(int nid, u64 start, u64 size,\narch/powerpc/mm/mem.c-146-\t\t\t  struct mhp_params *params)\n--\narch/riscv/mm/init.c=1715=struct range arch_get_mappable_range(void)\n--\narch/riscv/mm/init.c-1723-\narch/riscv/mm/init.c:1724:int __ref arch_add_memory(int nid, u64 start, u64 size, struct mhp_params *params)\narch/riscv/mm/init.c-1725-{\n--\narch/s390/mm/init.c=271=device_initcall(s390_cma_mem_init);\n--\narch/s390/mm/init.c-274-\narch/s390/mm/init.c:275:int arch_add_memory(int nid, u64 start, u64 size,\narch/s390/mm/init.c-276-\t\t    struct mhp_params *params)\n--\narch/x86/mm/init_64.c=963=int add_pages(int nid, unsigned long start_pfn, unsigned long nr_pages,\n--\narch/x86/mm/init_64.c-990-\narch/x86/mm/init_64.c:991:int arch_add_memory(int nid, u64 start, u64 size,\narch/x86/mm/init_64.c-992-\t\t    struct mhp_params *params)\n--\ninclude/linux/memory_hotplug.h=123=extern int try_online_node(int nid);\ninclude/linux/memory_hotplug.h-124-\ninclude/linux/memory_hotplug.h:125:extern int arch_add_memory(int nid, u64 start, u64 size,\ninclude/linux/memory_hotplug.h-126-\t\t\t   struct mhp_params *params);\n--\nmm/memory_hotplug.c=1448=static int create_altmaps_and_memory_blocks(int nid, struct memory_group *group,\n--\nmm/memory_hotplug.c-1472-\t\t/* call arch's memory hotadd */\nmm/memory_hotplug.c:1473:\t\tret = arch_add_memory(nid, cur_start, memblock_size, \u0026params);\nmm/memory_hotplug.c-1474-\t\tif (ret \u003c 0) {\n--\nmm/memory_hotplug.c=1502=static int __add_memory_resource(int nid, struct resource *res, mhp_t mhp_flags,\n--\nmm/memory_hotplug.c-1562-\t} else {\nmm/memory_hotplug.c:1563:\t\tret = arch_add_memory(nid, start, size, \u0026params);\nmm/memory_hotplug.c-1564-\t\tif (ret \u003c 0)\n--\nmm/memremap.c=144=static int pagemap_range(struct dev_pagemap *pgmap, struct mhp_params *params,\n--\nmm/memremap.c-203-\t * over the device memory is un-accessible thus we do not want to\nmm/memremap.c:204:\t * create a linear mapping for the memory like arch_add_memory()\nmm/memremap.c-205-\t * would do.\n--\nmm/memremap.c-208-\t * the CPU, we do want the linear mapping and thus use\nmm/memremap.c:209:\t * arch_add_memory().\nmm/memremap.c-210-\t */\n--\nmm/memremap.c-220-\nmm/memremap.c:221:\t\terror = arch_add_memory(nid, range-\u003estart, range_len(range),\nmm/memremap.c-222-\t\t\t\t\tparams);\n"
}

29/2 2026/10/01 14:32 llm patch-evaluator
1m Model:
gemini-3.8-flash

Args:
null

Results:
null

Tokens:
							input: 38951
							output: 22
							thoughts: 2108
						

Thoughts:
**Analyzing the Hugetlb vmemmap Optimization**

Okay, so I'm looking at this `hugetlb_vmemmap_optimize_folio` function.  It's called from `alloc_fresh_hugetlb_folio` in `mm/hugetlb.c`. That's whenever we allocate huge pages – via `/proc/sys/vm/nr_hugepages`, `mmap` with `MAP_HUGETLB`, `sys_shmget` with `SHM_HUGETLB`, and similar calls.  This function, `hugetlb_vmemmap_optimize_folio`, directly invokes `vmemmap_shared_tail_page`.  That's the key. This means that tail pages are shared directly during HugeTLB allocation.

Previously, `mm/hugetlb_vmemmap.c` used `vmemmap_get_tail` which allocated and cached tail pages. With this patch, however:

1.  `zone->vmemmap_tails` in `struct zone` is now dynamically allocated instead of being a fixed-size array. I need to dig into `vmemmap_tails` to understand how it's dynamically allocated - either by slab allocator or memblock.
2.  `vmemmap_shared_tail_page` is directly responsible for allocating and returning the shared tail page. It uses `vmemmap_alloc_block`. It then iterates across all pages allocated from the block setting the node, zone and compound tail. It also uses a compare and exchange to safely handle concurrent access.
3.  `vmemmap_alloc_pte` in `mm/sparse-vmemmap.c` also calls `vmemmap_shared_tail_page` under certain conditions. It's used for device DAX.
4.  `vmemmap_pte_populate` now uses `try_get_page` to prevent the shared page refcount from overflowing during page population if `VMEMMAP_POPULATE_DAX` is set.
5.  `section_activate` determines `order` and calls `section_set_compound_order_range`. It also calls `populate_section_memmap`, with the new changes in `vmemmap_shared_tail_page`.
6.  `section_deactivate` clears `section->compound_page_order` when deactivating a section.
7.  `remove_pfn_range_from_zone` poisons pages if the section is not vmemmap optimizable.
8.  `compound_nr_pages` now uses a defined macro `VMEMMAP_OPTIMIZATION_PAGES` to determine the number of pages in a compound page if the section is vmemmap optimizable.
9.  `sparse_add_section` poisons the memmap if the section is not vmemmap optimizable.
10. The macro `section_set_compound_order` contains a `VM_WARN_ON` assertion. `vmemmap_shared_tail_page` has a `WARN_ON_ONCE` and `vmemmap_pte_populate` also contains a `WARN_ON_ONCE`.

These are all hints that the entire architecture has been changed.

Now, I need to check how `section_vmemmap_optimizable` is defined. This appears to be the central control flag to determine if the changes should take effect. I need to understand this to have a proper picture.



30/2 2026/10/01 14:33 tool grepper
0m Args:
{
  "Expression": "section_vmemmap_optimizable"
}

Results:
{
  "Output": "mm/memory_hotplug.c=543=void remove_pfn_range_from_zone(struct zone *zone,\n--\nmm/memory_hotplug.c-557-\t\t\tmin(end_pfn - pfn, SECTION_ALIGN_UP(pfn + 1) - pfn);\nmm/memory_hotplug.c:558:\t\tif (!section_vmemmap_optimizable(__pfn_to_section(pfn)))\nmm/memory_hotplug.c-559-\t\t\tpage_init_poison(pfn_to_page(pfn),\n--\nmm/mm_init.c=1051=static inline unsigned long compound_nr_pages(unsigned long pfn,\n--\nmm/mm_init.c-1055-\nmm/mm_init.c:1056:\tif (!section_vmemmap_optimizable(ms))\nmm/mm_init.c-1057-\t\treturn pgmap_vmemmap_nr(pgmap);\n--\nmm/sparse-vmemmap.c=134=int __meminit section_nr_vmemmap_pages(unsigned long pfn, unsigned long nr_pages)\n--\nmm/sparse-vmemmap.c-142-\nmm/sparse-vmemmap.c:143:\tif (!section_vmemmap_optimizable(ms))\nmm/sparse-vmemmap.c-144-\t\treturn DIV_ROUND_UP(nr_pages * sizeof(struct page), PAGE_SIZE);\n--\nmm/sparse-vmemmap.c=451=int __meminit vmemmap_populate_hugepages(unsigned long start, unsigned long end,\n--\nmm/sparse-vmemmap.c-479-\t\tpmd = pmd_offset(pud, addr);\nmm/sparse-vmemmap.c:480:\t\tif (pmd_none(pmdp_get(pmd)) \u0026\u0026 !section_vmemmap_optimizable(ms)) {\nmm/sparse-vmemmap.c-481-\t\t\tvoid *p;\n--\nmm/sparse-vmemmap.c-498-\t\t} else if (vmemmap_check_pmd(pmd, node, addr, next)) {\nmm/sparse-vmemmap.c:499:\t\t\tif (WARN_ON_ONCE(section_vmemmap_optimizable(ms)))\nmm/sparse-vmemmap.c-500-\t\t\t\treturn -EOPNOTSUPP;\n--\nmm/sparse-vmemmap.c=575=struct page * __meminit __populate_section_memmap(unsigned long pfn,\n--\nmm/sparse-vmemmap.c-586-\nmm/sparse-vmemmap.c:587:\tif (pgmap \u0026\u0026 section_vmemmap_optimizable(__pfn_to_section(pfn)))\nmm/sparse-vmemmap.c-588-\t\tr = vmemmap_populate_compound_pages(pfn, start, end, nid, pgmap);\n--\nmm/sparse-vmemmap.c=879=int __meminit sparse_add_section(int nid, unsigned long start_pfn,\n--\nmm/sparse-vmemmap.c-902-\t */\nmm/sparse-vmemmap.c:903:\tif (!section_vmemmap_optimizable(ms))\nmm/sparse-vmemmap.c-904-\t\tpage_init_poison(memmap, sizeof(struct page) * nr_pages);\n--\nmm/sparse.h=53=static inline size_t mem_section_usage_size(void)\n--\nmm/sparse.h-58-\nmm/sparse.h:59:static inline bool section_vmemmap_optimizable(const struct mem_section *ms)\nmm/sparse.h-60-{\n"
}

31/2 2026/10/01 14:34 llm patch-evaluator
0m Model:
gemini-3.8-flash

Args:
null

Results:
null

Tokens:
							input: 42778
							output: 32
							thoughts: 25
						

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

**Investigating the `section_vmemmap_optimizable` Macro**

Right, so I'm curious about the `section_vmemmap_optimizable` macro. Given my expertise, I suspect this has something to do with the virtual memory map, and likely optimization strategies related to sparse memory mapping, which is something I'm very familiar with. The name itself hints at some sort of conditional optimization. My first instinct, as always, is to go straight to the source. The header file specified, `mm/sparse.h`, is exactly where I'd expect to find the definition. Let's see what the macro actually *does*. Understanding its implementation is key to figuring out how and when this optimization kicks in. I need to understand its conditions and the implications for memory management performance. From there, I'll be able to quickly determine its role.



32/2 2026/10/01 14:34 tool read-file
0m Args:
{
  "File": "mm/sparse.h",
  "FirstLine": 55,
  "LineCount": 20
}

Results:
{
  "Contents": "  55:\t\treturn struct_size_t(struct mem_section_usage, pageblock_flags,\n  56:\t\t\t\t     BITS_TO_LONGS(SECTION_BLOCKFLAGS_BITS));\n  57:\t}\n  58:\t\n  59:\tstatic inline bool section_vmemmap_optimizable(const struct mem_section *ms)\n  60:\t{\n  61:\t\treturn vmemmap_optimizable_order(section_compound_order(ms));\n  62:\t}\n  63:\t#else\n  64:\tstatic inline void sparse_init(void) {}\n  65:\tstatic inline void sparse_sections_init(void) {}\n  66:\t#endif /* CONFIG_SPARSEMEM */\n  67:\t\n  68:\t/*\n  69:\t * mm/sparse-vmemmap.c\n  70:\t */\n  71:\t#ifdef CONFIG_SPARSEMEM_VMEMMAP\n  72:\tvoid sparse_init_subsection_map(void);\n  73:\tint section_nr_vmemmap_pages(unsigned long pfn, unsigned long nr_pages);\n  74:\t#else\n"
}

33/2 2026/10/01 14:34 llm patch-evaluator
0m Model:
gemini-3.8-flash

Args:
null

Results:
null

Tokens:
							input: 43344
							output: 35
							thoughts: 2137
						

Thoughts:
**Analysis of Kernel Memory Management Optimization**

Okay, so here's the deal. I've been digging into this vmemmap optimization stuff, and it looks like a juicy target for fuzzing. The key observation is that `section_vmemmap_optimizable` now checks `vmemmap_optimizable_order`, which seems to be central. The `section_activate` function is setting the `compound_page_order` for DAX memory, not just HugeTLB anymore. This, in turn, influences `section_vmemmap_optimizable`, which is now called everywhere. Previously, it was only for HugeTLB!

I see that `memmap_init_zone_device` uses the new check, as well.  The poisoning of tail pages in `sparse_add_section` and `remove_pfn_range_from_zone` is conditional on the optimizability, so that has been updated as well. It looks like `vmemmap_populate_compound_pages` is now using a shared tail page, like HugeTLB, rather than reusing pages, with the shared tail page being dynamically allocated and used, as it is dynamically allocated using `kzalloc_objs` and `cmpxchg`. This dynamic allocation is important because it is using `vmemmap_tails(zone)`.

I'm particularly interested in the `vmemmap_pte_populate` function because it now takes a reference on the shared tail page via `try_get_page` for DAX mappings. This is a potential area for refcount overflows or other concurrency issues, especially with the shared tail page approach.

Now, the question is, can this be triggered by Syzkaller?

HugeTLB HVO is definitely reachable.  `hugetlb_vmemmap_optimize_folio` is called when folios are allocated, and syzkaller *does* have syscall descriptions for HugeTLB and sysctl parameters!  That function uses `vmemmap_shared_tail_page` and then indirectly calls `vmemmap_tails(zone)`, which allocates the shared tail page, which is very interesting from a fuzzing perspective.

Even without direct DAX, the HugeTLB path is valuable because it directly exercises `vmemmap_shared_tail_page` and `vmemmap_tails`.

I also noticed a bunch of related changes: `HUGETLB_PAGE_OPTIMIZE_VMEMMAP` selects `VMEMMAP_OPTIMIZATION`, zone's `vmemmap_tails` is changed, `compound_page_order` is under `CONFIG_VMEMMAP_OPTIMIZATION`, new functions defined, like `vmemmap_shared_tail_page()` and `vmemmap_optimizable_pfn()`, and `vmemmap_pte_populate()` is updated to use `try_get_page()`.  `section_activate` and `section_deactivate` are also now setting compound order for dax and resetting it, respectively.  A number of other functions have had related updates.

Based on the core triage philosophy, this is *absolutely* worth fuzzing.  This code is reachable in a standard QEMU environment, and these are core memory management changes.  The potential for introducing subtle semantic shifts or uncovering latent kernel bugs due to refactoring is high.

Let's look at the config options. We need `HUGETLB_PAGE_OPTIMIZE_VMEMMAP`, `SPARSEMEM_VMEMMAP` and  `ZONE_DEVICE` to be enabled to reach all code paths. The dependencies and selects seem to indicate that those are all needed. `HUGETLB_PAGE_OPTIMIZE_VMEMMAP` depends on `ARCH_WANT_OPTIMIZE_HUGETLB_VMEMMAP` and `SPARSEMEM_VMEMMAP` and `ZONE_DEVICE` depends on `ARCH_WANT_OPTIMIZE_DAX_VMEMMAP`. Both of these `ARCH_WANT_*` options are enabled by default for x86_64 architectures.



34/2 2026/10/01 14:34 tool grepper
0m Args:
{
  "Expression": "config HUGETLB_PAGE_OPTIMIZE_VMEMMAP",
  "PathPrefix": "fs/"
}

Results:
{
  "Output": "fs/Kconfig=262=if HUGETLBFS\nfs/Kconfig:263:config HUGETLB_PAGE_OPTIMIZE_VMEMMAP_DEFAULT_ON\nfs/Kconfig-264-\tbool \"HugeTLB Vmemmap Optimization (HVO) defaults to on\"\n--\nfs/Kconfig=273=config HUGETLB_PAGE\n--\nfs/Kconfig-276-\nfs/Kconfig:277:config HUGETLB_PAGE_OPTIMIZE_VMEMMAP\nfs/Kconfig-278-\tdef_bool HUGETLB_PAGE\n"
}

35/2 2026/10/01 14:34 llm patch-evaluator
0m Model:
gemini-3.8-flash

Args:
null

Results:
null

Tokens:
							input: 46535
							output: 32
							thoughts: 21
						

Thoughts:
Here's my take on the thought process, tailored for an expert audience:

**Diving into Kconfig: Investigating Filesystem Configuration**

Okay, time to get my hands dirty and understand how this filesystem configuration is structured. Let's zero in on `fs/Kconfig`, specifically around lines 260 to 290. That area usually holds crucial definitions, perhaps related to a particular driver or feature I'm trying to decipher. I'm expecting to find conditional statements, `config` options, maybe some dependencies or default values defined there. Knowing what's in that specific region will likely give me a clearer picture of how a certain filesystem's build flags are managed or how a specific feature is enabled. I need to understand the dependencies and relationships within the configuration system itself, and that chunk of Kconfig seems like the right place to begin this exploration. Let's see what's actually there.



36/2 2026/10/01 14:35 tool read-file
0m Args:
{
  "File": "fs/Kconfig",
  "FirstLine": 260,
  "LineCount": 35
}

Results:
{
  "Contents": " 260:\t\t  If unsure, say N.\n 261:\t\n 262:\tif HUGETLBFS\n 263:\tconfig HUGETLB_PAGE_OPTIMIZE_VMEMMAP_DEFAULT_ON\n 264:\t\tbool \"HugeTLB Vmemmap Optimization (HVO) defaults to on\"\n 265:\t\tdefault n\n 266:\t\tdepends on HUGETLB_PAGE_OPTIMIZE_VMEMMAP\n 267:\t\thelp\n 268:\t\t  The HugeTLB Vmemmap Optimization (HVO) defaults to off. Say Y here to\n 269:\t\t  enable HVO by default. It can be disabled via hugetlb_free_vmemmap=off\n 270:\t\t  (boot command line) or hugetlb_optimize_vmemmap (sysctl).\n 271:\tendif # HUGETLBFS\n 272:\t\n 273:\tconfig HUGETLB_PAGE\n 274:\t\tdef_bool HUGETLBFS\n 275:\t\tselect XARRAY_MULTI\n 276:\t\n 277:\tconfig HUGETLB_PAGE_OPTIMIZE_VMEMMAP\n 278:\t\tdef_bool HUGETLB_PAGE\n 279:\t\tdepends on ARCH_WANT_OPTIMIZE_HUGETLB_VMEMMAP\n 280:\t\tdepends on SPARSEMEM_VMEMMAP\n 281:\t\tselect VMEMMAP_OPTIMIZATION\n 282:\t\n 283:\tconfig HUGETLB_PMD_PAGE_TABLE_SHARING\n 284:\t\tdef_bool HUGETLB_PAGE\n 285:\t\tdepends on ARCH_WANT_HUGE_PMD_SHARE \u0026\u0026 SPLIT_PMD_PTLOCKS\n 286:\t\n 287:\tconfig ARCH_HAS_GIGANTIC_PAGE\n 288:\t\tbool\n 289:\t\n 290:\tsource \"fs/configfs/Kconfig\"\n 291:\tsource \"fs/efivarfs/Kconfig\"\n 292:\t\n 293:\tendmenu\n 294:\t\n"
}

37/2 2026/10/01 14:35 llm patch-evaluator
0m Model:
gemini-3.8-flash

Args:
null

Results:
null

Tokens:
							input: 47324
							output: 26
							thoughts: 144
						

Thoughts:
**My Initial Thoughts on HugeTLB Configuration**

Okay, let's dive into this. The key thing that jumps out is `HUGETLB_PAGE_OPTIMIZE_VMEMMAP_DEFAULT_ON`. If that's enabled, then HVO (HugeTLB Virtual Memory Optimization) is automatically on, which is great. That simplifies things considerably; no more fiddling with `hugetlb_free_vmemmap=on` on the boot command line if this is active. I like that.

And while I'm here, I need to remind myself that `HUGETLBFS` is what ultimately controls `HUGETLB_PAGE`, that fundamental building block. Important connection there.

Now, let me make sure I'm not missing anything. I need to keep in mind `SPARSEMEM_VMEMMAP` and also `ZONE_DEVICE`, which is, of course, dependent on `MEMORY_HOTREMOVE`. Then `MEMORY_HOTPLUG` too. Always connected.

I think I need to quickly refresh my understanding of how `ZONE_DEVICE` works. Let's go look at `mm/Kconfig` and refresh my memory of that. That will give me the proper context for the rest of these connections.



38/2 2026/10/01 14:35 tool grepper
0m Args:
{
  "Expression": "config ZONE_DEVICE",
  "PathPrefix": "mm/"
}

Results:
{
  "Output": "mm/Kconfig=1219=config ZONE_DMA32\n--\nmm/Kconfig-1223-\nmm/Kconfig:1224:config ZONE_DEVICE\nmm/Kconfig-1225-\tbool \"Device memory (pmem, HMM, etc...) hotplug support\"\n"
}

39/2 2026/10/01 14:35 llm patch-evaluator
0m Model:
gemini-3.8-flash

Args:
null

Results:
null

Tokens:
							input: 47844
							output: 33
							thoughts: 18
						

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

**Diving into the Kernel Configuration Landscape**

Okay, so I need to get my bearings on a specific configuration setting, and the most logical place to start is the Kconfig file for the relevant subsystem. Given the context, my initial thought is to navigate directly to `mm/Kconfig`. That's where I anticipate the core memory management configuration options will reside. I'm homing in on roughly line 1224; based on my experience, that's often a good spot to find related configurations or includes within such a large file. This spot will likely provide a helpful overview of the feature I need to examine.



40/2 2026/10/01 14:35 tool read-file
0m Args:
{
  "File": "mm/Kconfig",
  "FirstLine": 1220,
  "LineCount": 25
}

Results:
{
  "Contents": "1220:\t\tbool \"Support DMA32 zone\" if ARCH_HAS_ZONE_DMA_SET\n1221:\t\tdepends on !X86_32\n1222:\t\tdefault y if ARM64\n1223:\t\n1224:\tconfig ZONE_DEVICE\n1225:\t\tbool \"Device memory (pmem, HMM, etc...) hotplug support\"\n1226:\t\tdepends on MEMORY_HOTREMOVE\n1227:\t\tselect VMEMMAP_OPTIMIZATION if ARCH_WANT_OPTIMIZE_DAX_VMEMMAP\n1228:\t\tselect XARRAY_MULTI\n1229:\t\n1230:\t\thelp\n1231:\t\t  Device memory hotplug support allows for establishing pmem,\n1232:\t\t  or other device driver discovered memory regions, in the\n1233:\t\t  memmap. This allows pfn_to_page() lookups of otherwise\n1234:\t\t  \"device-physical\" addresses which is needed for DAX, PCI_P2PDMA, and\n1235:\t\t  DEVICE_PRIVATE features among others.\n1236:\t\n1237:\t\t  Enabling this option will reduce the entropy of x86 KASLR memory\n1238:\t\t  regions. For example - on a 46 bit system, the entropy goes down\n1239:\t\t  from 16 bits to 15 bits. The actual reduction in entropy depends\n1240:\t\t  on the physical address bits, on processor features, kernel config\n1241:\t\t  (5 level page table) and physical memory present on the system.\n1242:\t\n1243:\t#\n1244:\t# Helpers to mirror range of the CPU page tables of a process into device page\n"
}

41/2 2026/10/01 14:35 llm patch-evaluator
0m Model:
gemini-3.8-flash

Args:
null

Results:
null

Tokens:
							input: 48489
							output: 28
							thoughts: 17
						

Thoughts:
Okay, here's my thought process, rendered as a first-person expert summary:

**Checking Kernel Configuration for Hotplug Memory**

Right, let's get down to brass tacks. I need to understand if memory hot-removal is enabled in this particular kernel configuration. My instinct says to dive straight into the heart of the matter: `mm/Kconfig`. This is where all the memory management related configuration options will be defined. Specifically, I'm after the `MEMORY_HOTREMOVE` symbol. I'm assuming it's under that name, but I can check. Knowing the location of the relevant configuration option is the first step to determining if the kernel even *supports* removing memory while running. That's crucial for understanding the overall system's capabilities.



42/2 2026/10/01 14:35 tool grepper
0m Args:
{
  "Expression": "config MEMORY_HOTREMOVE",
  "PathPrefix": "mm/"
}

Results:
{
  "Output": "mm/Kconfig=579=endchoice\nmm/Kconfig-580-\nmm/Kconfig:581:config MEMORY_HOTREMOVE\nmm/Kconfig-582-\tbool \"Allow for memory hot remove\"\n"
}

43/2 2026/10/01 14:35 llm patch-evaluator
0m Model:
gemini-3.8-flash

Args:
null

Results:
null

Tokens:
							input: 48765
							output: 32
							thoughts: 12
						

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

**Memory Hot-Remove Dependency Audit**

Okay, time to dig into `MEMORY_HOTREMOVE`. I need to ensure all the relevant dependencies are accounted for before proceeding. My mental checklist starts with the obvious: kernel modules. Are the hot-remove features properly integrated with the memory management layer? Are there any potential race conditions that need to be addressed in the code paths dealing with memory allocation and deallocation during hot-removal? Then, the device drivers themselves. How robust are they in handling memory disappearing on them mid-operation? Do they have proper error handling and recovery mechanisms in place for `ENOMEM` or related errors stemming from the hot-remove?

I also need to consider the higher-level systems that rely on memory. The filesystem, for instance. How does it react when a memory region it uses is abruptly removed? Is there data loss potential or corruption? And what about virtual memory management? The interplay with paging, swap space, and NUMA configurations is crucial. Any regressions there could be catastrophic. I need to make sure the memory hot-remove logic integrates correctly with the current NUMA balancing strategies. I need to make sure the hot-remove features are integrated with the kdump implementation in particular so we don't lose the crash dump.

Finally, let's not forget user-space tools and applications. They often aren’t written with the assumption that memory can disappear at any time. I should consider mechanisms to allow for graceful handling of such a removal - can we signal userspace, offer ways to remap memory, or otherwise alert applications to the potential loss of memory? If the hot-remove isn't transparent, or it's improperly handled, it's a huge problem. This whole situation warrants a deep dive into the current implementation, and a solid set of tests to ensure everything functions properly.



44/2 2026/10/01 14:36 tool read-file
0m Args:
{
  "File": "mm/Kconfig",
  "FirstLine": 580,
  "LineCount": 20
}

Results:
{
  "Contents": " 580:\t\n 581:\tconfig MEMORY_HOTREMOVE\n 582:\t\tbool \"Allow for memory hot remove\"\n 583:\t\tselect MIGRATION\n 584:\t\n 585:\tconfig MHP_MEMMAP_ON_MEMORY\n 586:\t\tdef_bool y\n 587:\t\tdepends on ARCH_MHP_MEMMAP_ON_MEMORY_ENABLE\n 588:\t\n 589:\tendif # MEMORY_HOTPLUG\n 590:\t\n 591:\tconfig ARCH_MHP_MEMMAP_ON_MEMORY_ENABLE\n 592:\t       bool\n 593:\t\n 594:\t# Heavily threaded applications may benefit from splitting the mm-wide\n 595:\t# page_table_lock, so that faults on different parts of the user address\n 596:\t# space can be handled with less contention: split it at this NR_CPUS.\n 597:\t# Default to 4 for wider testing, though 8 might be more appropriate.\n 598:\t# ARM's adjust_pte (unused if VIPT) depends on mm-wide page_table_lock.\n 599:\t# PA-RISC 7xxx's spinlock_t would enlarge struct page from 32 to 44 bytes.\n"
}

45/2 2026/10/01 14:36 llm patch-evaluator
0m Model:
gemini-3.8-flash

Args:
null

Results:
null

Tokens:
							input: 49545
							output: 30
							thoughts: 93
						

Thoughts:
Okay, let's see, my mental gears are turning...

**Analyzing Memory Hotplug and DAX Configuration Options**

Right, so `MEMORY_HOTREMOVE` is nested within the `MEMORY_HOTPLUG` configuration. That's good to keep in mind, a basic dependency check. Let me just quickly list the variables I have to consider: `MEMORY_HOTPLUG`, `MEMORY_HOTREMOVE`, `ZONE_DEVICE`, `SPARSEMEM_VMEMMAP`, `HUGETLBFS`, and `HUGETLB_PAGE_OPTIMIZE_VMEMMAP_DEFAULT_ON`. I need to ensure the correct values for these.

Now, the real question is whether any of those configs are directly relevant for DAX. This is key. Are there any `DEV_DAX` options, or anything similar, that I should also be considering to ensure proper functionality and integration of DAX with the memory hotplug features? I need to ascertain this as DAX is essential for the application.



46/2 2026/10/01 14:36 tool grepper
0m Args:
{
  "Expression": "config DEV_DAX",
  "PathPrefix": "drivers/dax/"
}

Results:
{
  "Output": "drivers/dax/Kconfig=6=if DAX\ndrivers/dax/Kconfig-7-\ndrivers/dax/Kconfig:8:config DEV_DAX\ndrivers/dax/Kconfig-9-\ttristate \"Device DAX: direct access mapping device\"\n--\ndrivers/dax/Kconfig-18-\ndrivers/dax/Kconfig:19:config DEV_DAX_PMEM\ndrivers/dax/Kconfig-20-\ttristate \"PMEM DAX: direct access to persistent memory\"\n--\ndrivers/dax/Kconfig-29-\ndrivers/dax/Kconfig:30:config DEV_DAX_HMEM\ndrivers/dax/Kconfig-31-\ttristate \"HMEM DAX: direct access to 'specific purpose' memory\"\n--\ndrivers/dax/Kconfig-49-\ndrivers/dax/Kconfig:50:config DEV_DAX_CXL\ndrivers/dax/Kconfig-51-\ttristate \"CXL DAX: direct access to CXL RAM regions\"\n--\ndrivers/dax/Kconfig-63-\ndrivers/dax/Kconfig:64:config DEV_DAX_HMEM_DEVICES\ndrivers/dax/Kconfig-65-\tdepends on DEV_DAX_HMEM \u0026\u0026 DAX\n--\ndrivers/dax/Kconfig-67-\ndrivers/dax/Kconfig:68:config DEV_DAX_FSDEV\ndrivers/dax/Kconfig-69-\ttristate\n--\ndrivers/dax/Kconfig-72-\ndrivers/dax/Kconfig:73:config DEV_DAX_KMEM\ndrivers/dax/Kconfig-74-\ttristate \"KMEM DAX: map dax-devices as System-RAM\"\n"
}

47/2 2026/10/01 14:36 llm patch-evaluator
0m Model:
gemini-3.8-flash

Args:
null

Results:
null

Tokens:
							input: 50327
							output: 283
							thoughts: 2413
						

Thoughts:
**Analysis of Code Changes for Fuzzing Focus**

Alright, let's break down this patch for effective fuzzing. My initial focus is on identifying key functions that represent the core logic changes, and avoid getting bogged down in ubiquitous functions. First, I've gone through the file modifications: `mm/sparse-vmemmap.c`, `mm/hugetlb_vmemmap.c`, `mm/memory_hotplug.c`, and `mm/mm_init.c`.

I've flagged the following non-static functions as potential targets for `FocusSymbols`: `vmemmap_shared_tail_page`, `hugetlb_vmemmap_optimize_folio`, `hugetlb_vmemmap_optimize_folios`, `sparse_add_section`, `section_nr_vmemmap_pages`, `memmap_init_zone_device`. I avoided any static inlines or functions that are likely to be called everywhere in the kernel, and instead focused on the direct entrypoints or core logic functions. For instance, `vmemmap_shared_tail_page` is key to the shared tail page management, `hugetlb_vmemmap_optimize_folio` directly implements HVO logic. The others are critical for section management, memory hotplug, and zone initialization.

Now, I'm verifying each function against the fuzzing guidelines. Are any of these functions *ubiquitous*? No, each is specific to the added/altered feature logic: HugeTLB/DAX vmemmap optimization or zone initialization.

Looking at the configurations, `HUGETLBFS`, `HUGETLB_PAGE_OPTIMIZE_VMEMMAP_DEFAULT_ON`, `SPARSEMEM_VMEMMAP`, `ZONE_DEVICE`, `MEMORY_HOTPLUG`, `MEMORY_HOTREMOVE` and `DEV_DAX` seem relevant because they directly enable the code. I then looked at the Kconfig and verified that `VMEMMAP_OPTIMIZATION` is selected by either `HUGETLB_PAGE_OPTIMIZE_VMEMMAP` (enabled by `HUGETLBFS`) or `ZONE_DEVICE`.

I've determined that `WorthFuzzing` should definitely be `true`. This patch series fundamentally changes how tail pages and vmemmaps are handled, generalizing HugeTLB vmemmap optimization. It alters core memory management, and is directly reachable in x86_64/amd64 via standard HugeTLB folio allocation, or memory hotplug/DAX workflows. This is a high-impact area.

Finally, I'm confident in the `FocusSymbols` list: `vmemmap_shared_tail_page`, `hugetlb_vmemmap_optimize_folio`, `hugetlb_vmemmap_optimize_folios`, `__populate_section_memmap`, `sparse_add_section`, `section_nr_vmemmap_pages`. These are the key entrypoints for fuzzing.



48/2 2026/10/01 14:36 tool set-results
0m Args:
{
  "EnableConfigs": [
    "HUGETLBFS",
    "HUGETLB_PAGE_OPTIMIZE_VMEMMAP_DEFAULT_ON",
    "SPARSEMEM_VMEMMAP",
    "ZONE_DEVICE",
    "MEMORY_HOTPLUG",
    "MEMORY_HOTREMOVE"
  ],
  "FocusSymbols": [
    "vmemmap_shared_tail_page",
    "hugetlb_vmemmap_optimize_folio",
    "hugetlb_vmemmap_optimize_folios",
    "__populate_section_memmap",
    "sparse_add_section",
    "section_nr_vmemmap_pages"
  ],
  "Reasoning": "The patch modifies core memory management (mm/) logic by unifying HugeTLB Vmemmap Optimization (HVO) and DAX vmemmap deduplication under a common VMEMMAP_OPTIMIZATION framework. It replaces per-zone static tail page arrays with dynamically allocated structures (vmemmap_tails), adds vmemmap_shared_tail_page(), alters page table population with new DAX flags and reference count management, and changes memory section activation, deactivation, and page poisoning behavior. These code paths are fully reachable on amd64 via HugeTLB folio allocation and memory hotplug/DAX workflows.",
  "WorthFuzzing": true
}

Results:
{
  "EnableConfigs": [
    "HUGETLBFS",
    "HUGETLB_PAGE_OPTIMIZE_VMEMMAP_DEFAULT_ON",
    "SPARSEMEM_VMEMMAP",
    "ZONE_DEVICE",
    "MEMORY_HOTPLUG",
    "MEMORY_HOTREMOVE"
  ],
  "FocusSymbols": [
    "vmemmap_shared_tail_page",
    "hugetlb_vmemmap_optimize_folio",
    "hugetlb_vmemmap_optimize_folios",
    "__populate_section_memmap",
    "sparse_add_section",
    "section_nr_vmemmap_pages"
  ],
  "Reasoning": "The patch modifies core memory management (mm/) logic by unifying HugeTLB Vmemmap Optimization (HVO) and DAX vmemmap deduplication under a common VMEMMAP_OPTIMIZATION framework. It replaces per-zone static tail page arrays with dynamically allocated structures (vmemmap_tails), adds vmemmap_shared_tail_page(), alters page table population with new DAX flags and reference count management, and changes memory section activation, deactivation, and page poisoning behavior. These code paths are fully reachable on amd64 via HugeTLB folio allocation and memory hotplug/DAX workflows.",
  "WorthFuzzing": true
}

49/1 2026/10/01 14:36 action If
1m Args:
{
  "WorthFuzzing": true
}

Results:
null

50/2 2026/10/01 14:36 agent kmsan-evaluator
1m Model:
core

Args:
null

Results:
{
  "KMSANReasoning": "The patch series refactors and unifies vmemmap deduplication logic across architectures and subsystems, consolidating HugeTLB Vmemmap Optimization (HVO) and Device-DAX vmemmap optimization under a generic CONFIG_VMEMMAP_OPTIMIZATION framework. It introduces a common vmemmap_shared_tail_page() mechanism for allocating, zeroing (via mm_zero_struct_page), and reusing tail struct pages across sparse memory sections.\n\nKMSAN is specifically designed to detect uninitialized memory reads on the stack/heap/allocations and leaks of uninitialized kernel data to userspace. This patch does not touch any user-space boundary (no copy_to_user, put_user, socket/netlink/ioctl interfaces), nor does it leave any kernel heap or stack structures uninitialized (dynamic allocations are initialized with kzalloc_objs and mm_zero_struct_page).\n\nAny potential bugs in this patch would involve page table manipulations, pfn/section arithmetic, refcounting on struct page, or memory corruption, which are covered by KASAN, DEBUG_VM, and standard kernel debug facilities. 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 c5cd987b2e9292f2580f8a5fb953333aa1ec4a3b
Author: syz-cluster <triage@syzkaller.com>
Date:   Thu Oct 1 14:24:00 2026 +0000

    syz-cluster: applied patch under review

diff --git a/Documentation/arch/powerpc/vmemmap_dedup.rst b/Documentation/arch/powerpc/vmemmap_dedup.rst
index dc4db59fdf87b..8286acbca9bc5 100644
--- a/Documentation/arch/powerpc/vmemmap_dedup.rst
+++ b/Documentation/arch/powerpc/vmemmap_dedup.rst
@@ -19,82 +19,28 @@ With 1G PUD level mapping, we require 16384 struct pages and a single 64K
 vmemmap page can contain 1024 struct pages (64K/sizeof(struct page)). Hence we
 require 16 64K pages in vmemmap to map the struct page for 1G PUD level mapping.
 
-Here's how things look like on device-dax after the sections are populated::
- +-----------+ ---virt_to_page---> +-----------+   mapping to   +-----------+
- |           |                     |     0     | -------------> |     0     |
- |           |                     +-----------+                +-----------+
- |           |                     |     1     | -------------> |     1     |
- |           |                     +-----------+                +-----------+
- |           |                     |     2     | ----------------^ ^ ^ ^ ^ ^
- |           |                     +-----------+                   | | | | |
- |           |                     |     3     | ------------------+ | | | |
- |           |                     +-----------+                     | | | |
- |           |                     |     4     | --------------------+ | | |
- |    PUD    |                     +-----------+                       | | |
- |   level   |                     |     .     | ----------------------+ | |
- |  mapping  |                     +-----------+                         | |
- |           |                     |     .     | ------------------------+ |
- |           |                     +-----------+                           |
- |           |                     |     15    | --------------------------+
- |           |                     +-----------+
- |           |
- |           |
- |           |
- +-----------+
-
-
 With 4K page size, 2M PMD level mapping requires 512 struct pages and a single
 4K vmemmap page contains 64 struct pages(4K/sizeof(struct page)). Hence we
 require 8 4K pages in vmemmap to map the struct page for 2M pmd level mapping.
 
-Here's how things look like on device-dax after the sections are populated::
-
- +-----------+ ---virt_to_page---> +-----------+   mapping to   +-----------+
- |           |                     |     0     | -------------> |     0     |
- |           |                     +-----------+                +-----------+
- |           |                     |     1     | -------------> |     1     |
- |           |                     +-----------+                +-----------+
- |           |                     |     2     | ----------------^ ^ ^ ^ ^ ^
- |           |                     +-----------+                   | | | | |
- |           |                     |     3     | ------------------+ | | | |
- |           |                     +-----------+                     | | | |
- |           |                     |     4     | --------------------+ | | |
- |    PMD    |                     +-----------+                       | | |
- |   level   |                     |     5     | ----------------------+ | |
- |  mapping  |                     +-----------+                         | |
- |           |                     |     6     | ------------------------+ |
- |           |                     +-----------+                           |
- |           |                     |     7     | --------------------------+
- |           |                     +-----------+
- |           |
- |           |
- |           |
- +-----------+
-
-With 1G PUD level mapping, we require 262144 struct pages and a single 4K
-vmemmap page can contain 64 struct pages (4K/sizeof(struct page)). Hence we
-require 4096 4K pages in vmemmap to map the struct pages for 1G PUD level
-mapping.
-
-Here's how things look like on device-dax after the sections are populated::
-
- +-----------+ ---virt_to_page---> +-----------+   mapping to   +-----------+
- |           |                     |     0     | -------------> |     0     |
- |           |                     +-----------+                +-----------+
- |           |                     |     1     | -------------> |     1     |
- |           |                     +-----------+                +-----------+
- |           |                     |     2     | ----------------^ ^ ^ ^ ^ ^
- |           |                     +-----------+                   | | | | |
- |           |                     |     3     | ------------------+ | | | |
- |           |                     +-----------+                     | | | |
- |           |                     |     4     | --------------------+ | | |
- |    PUD    |                     +-----------+                       | | |
- |   level   |                     |     .     | ----------------------+ | |
- |  mapping  |                     +-----------+                         | |
- |           |                     |     .     | ------------------------+ |
- |           |                     +-----------+                           |
- |           |                     |   4095    | --------------------------+
- |           |                     +-----------+
+Here's how things look on device-dax after vmemmap-optimized sections are
+populated. ``N`` is the number of vmemmap pages required by the DAX mapping
+above::
+
+  Device DAX                  vmemmap pages (N pages)         backing page frames
+ +-----------+ ---virt_to_page---> +-----------+   mapping to   +-------------+
+ |           |                     |     0     | -------------> |      0      |
+ |           |                     +-----------+                +-------------+
+ |           |                     |     1     | ------+
+ |           |                     +-----------+       |
+ |           |                     |     2     | ------+
+ |           |                     +-----------+       |
+ |           |                     |     .     | ------+        +-------------+
+ |  PMD/PUD  |                     +-----------+       |        | A single,   |
+ |   level   |                     |     .     | ------+------> | per-zone    |
+ |  mapping  |                     +-----------+       |        | shared tail |
+ |           |                     |   N - 1   | ------+        | page        |
+ |           |                     +-----------+                +-------------+
  |           |
  |           |
  |           |
diff --git a/Documentation/mm/vmemmap_dedup.rst b/Documentation/mm/vmemmap_dedup.rst
index 9fa8642ded483..8c287ae3f86cc 100644
--- a/Documentation/mm/vmemmap_dedup.rst
+++ b/Documentation/mm/vmemmap_dedup.rst
@@ -1,4 +1,3 @@
-
 .. SPDX-License-Identifier: GPL-2.0
 
 =========================================
@@ -192,32 +191,7 @@ to 4 on HugeTLB pages.
 
 There's no remapping of vmemmap given that device-dax memory is not part of
 System RAM ranges initialized at boot. Thus the tail page deduplication
-happens at a later stage when we populate the sections. HugeTLB reuses the
-the head vmemmap page representing, whereas device-dax reuses the tail
-vmemmap page. This results in only half of the savings compared to HugeTLB.
-
-Deduplicated tail pages are not mapped read-only.
+happens at a later stage when we populate the sections.
 
-Here's how things look like on device-dax after the sections are populated::
-
- +-----------+ ---virt_to_page---> +-----------+   mapping to   +-----------+
- |           |                     |     0     | -------------> |     0     |
- |           |                     +-----------+                +-----------+
- |           |                     |     1     | -------------> |     1     |
- |           |                     +-----------+                +-----------+
- |           |                     |     2     | ----------------^ ^ ^ ^ ^ ^
- |           |                     +-----------+                   | | | | |
- |           |                     |     3     | ------------------+ | | | |
- |           |                     +-----------+                     | | | |
- |           |                     |     4     | --------------------+ | | |
- |    PMD    |                     +-----------+                       | | |
- |   level   |                     |     5     | ----------------------+ | |
- |  mapping  |                     +-----------+                         | |
- |           |                     |     6     | ------------------------+ |
- |           |                     +-----------+                           |
- |           |                     |     7     | --------------------------+
- |           |                     +-----------+
- |           |
- |           |
- |           |
- +-----------+
+Deduplicated tail pages are not mapped read-only. The mapping layout is the same
+as HugeTLB.
diff --git a/MAINTAINERS b/MAINTAINERS
index fe1d70ed5100b..4689a02100600 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -12097,6 +12097,7 @@ F:	Documentation/mm/hugetlbfs_reserv.rst
 F:	Documentation/mm/vmemmap_dedup.rst
 F:	fs/hugetlbfs/
 F:	include/linux/hugetlb.h
+F:	include/linux/vmemmap-optimization.h
 F:	include/trace/events/hugetlbfs.h
 F:	mm/hugetlb.c
 F:	mm/hugetlb_cgroup.c
diff --git a/arch/loongarch/include/asm/pgtable.h b/arch/loongarch/include/asm/pgtable.h
index cf29a4c8ac593..f876031351319 100644
--- a/arch/loongarch/include/asm/pgtable.h
+++ b/arch/loongarch/include/asm/pgtable.h
@@ -72,6 +72,7 @@
 
 #include <linux/mm_types.h>
 #include <linux/mmzone.h>
+#include <linux/vmemmap-optimization.h>
 #include <asm/fixmap.h>
 #include <asm/sparsemem.h>
 
diff --git a/arch/powerpc/mm/book3s64/radix_pgtable.c b/arch/powerpc/mm/book3s64/radix_pgtable.c
index cf692b2b5f7bc..9ca28e4a610a2 100644
--- a/arch/powerpc/mm/book3s64/radix_pgtable.c
+++ b/arch/powerpc/mm/book3s64/radix_pgtable.c
@@ -19,6 +19,7 @@
 #include <linux/string_helpers.h>
 #include <linux/memory.h>
 #include <linux/kfence.h>
+#include <linux/vmemmap-optimization.h>
 
 #include <asm/pgalloc.h>
 #include <asm/mmu_context.h>
@@ -1217,92 +1218,6 @@ int __meminit radix__vmemmap_populate(unsigned long start, unsigned long end, in
 	return 0;
 }
 
-static pte_t * __meminit radix__vmemmap_populate_address(unsigned long addr, int node,
-							 struct vmem_altmap *altmap,
-							 struct page *reuse)
-{
-	pgd_t *pgd;
-	p4d_t *p4d;
-	pud_t *pud;
-	pmd_t *pmd;
-	pte_t *pte;
-
-	pgd = pgd_offset_k(addr);
-	p4d = p4d_offset(pgd, addr);
-	pud = vmemmap_pud_alloc(p4d, node, addr);
-	if (!pud)
-		return NULL;
-	pmd = vmemmap_pmd_alloc(pud, node, addr);
-	if (!pmd)
-		return NULL;
-	if (pmd_leaf(*pmd))
-		/*
-		 * The second page is mapped as a hugepage due to a nearby request.
-		 * Force our mapping to page size without deduplication
-		 */
-		return NULL;
-	pte = vmemmap_pte_alloc(pmd, node, addr);
-	if (!pte)
-		return NULL;
-	radix__vmemmap_pte_populate(pmd, addr, node, NULL, NULL);
-	vmemmap_verify(pte, node, addr, addr + PAGE_SIZE);
-
-	return pte;
-}
-
-static pte_t * __meminit vmemmap_compound_tail_page(unsigned long addr,
-						    unsigned long pfn_offset, int node)
-{
-	pgd_t *pgd;
-	p4d_t *p4d;
-	pud_t *pud;
-	pmd_t *pmd;
-	pte_t *pte;
-	unsigned long map_addr;
-
-	/* the second vmemmap page which we use for duplication */
-	map_addr = addr - pfn_offset * sizeof(struct page) + PAGE_SIZE;
-	pgd = pgd_offset_k(map_addr);
-	p4d = p4d_offset(pgd, map_addr);
-	pud = vmemmap_pud_alloc(p4d, node, map_addr);
-	if (!pud)
-		return NULL;
-	pmd = vmemmap_pmd_alloc(pud, node, map_addr);
-	if (!pmd)
-		return NULL;
-	if (pmd_leaf(*pmd))
-		/*
-		 * The second page is mapped as a hugepage due to a nearby request.
-		 * Force our mapping to page size without deduplication
-		 */
-		return NULL;
-	pte = vmemmap_pte_alloc(pmd, node, map_addr);
-	if (!pte)
-		return NULL;
-	/*
-	 * Check if there exist a mapping to the left
-	 */
-	if (pte_none(*pte)) {
-		/*
-		 * Populate the head page vmemmap page.
-		 * It can fall in different pmd, hence
-		 * vmemmap_populate_address()
-		 */
-		pte = radix__vmemmap_populate_address(map_addr - PAGE_SIZE, node, NULL, NULL);
-		if (!pte)
-			return NULL;
-		/*
-		 * Populate the tail pages vmemmap page
-		 */
-		pte = radix__vmemmap_pte_populate(pmd, map_addr, node, NULL, NULL);
-		if (!pte)
-			return NULL;
-		vmemmap_verify(pte, node, map_addr, map_addr + PAGE_SIZE);
-		return pte;
-	}
-	return pte;
-}
-
 int __meminit vmemmap_populate_compound_pages(unsigned long start_pfn,
 					      unsigned long start,
 					      unsigned long end, int node,
@@ -1320,6 +1235,12 @@ int __meminit vmemmap_populate_compound_pages(unsigned long start_pfn,
 	pud_t *pud;
 	pmd_t *pmd;
 	pte_t *pte;
+	struct page *tail_page;
+	unsigned int order = pfn_to_section_compound_order(start_pfn);
+
+	tail_page = vmemmap_shared_tail_page(order, device_zone(node));
+	if (!tail_page)
+		return -ENOMEM;
 
 	for (addr = start; addr < end; addr = next) {
 
@@ -1343,16 +1264,15 @@ int __meminit vmemmap_populate_compound_pages(unsigned long start_pfn,
 		if (!pte_none(*pte)) {
 			/*
 			 * This could be because we already have a compound
-			 * page whose VMEMMAP_RESERVE_NR pages were mapped and
+			 * page whose retained vmemmap page was mapped and
 			 * this request fall in those pages.
 			 */
 			next = addr + PAGE_SIZE;
 			continue;
 		} else {
-			unsigned long nr_pages = pgmap_vmemmap_nr(pgmap);
+			unsigned long nr_pages = 1UL << order;
 			unsigned long addr_pfn = page_to_pfn((struct page *)addr);
 			unsigned long pfn_offset = addr_pfn - ALIGN_DOWN(addr_pfn, nr_pages);
-			pte_t *tail_page_pte;
 
 			/*
 			 * if the address is aligned to huge page size it is the
@@ -1365,35 +1285,11 @@ int __meminit vmemmap_populate_compound_pages(unsigned long start_pfn,
 					return -ENOMEM;
 				vmemmap_verify(pte, node, addr, addr + PAGE_SIZE);
 
-				/*
-				 * Populate the tail pages vmemmap page
-				 * It can fall in different pmd, hence
-				 * vmemmap_populate_address()
-				 */
-				pte = radix__vmemmap_populate_address(addr + PAGE_SIZE, node, NULL, NULL);
-				if (!pte)
-					return -ENOMEM;
-
-				next = addr + 2 * PAGE_SIZE;
-				continue;
-			}
-			/*
-			 * get the 2nd mapping details
-			 * Also create it if that doesn't exist
-			 */
-			tail_page_pte = vmemmap_compound_tail_page(addr, pfn_offset, node);
-			if (!tail_page_pte) {
-
-				pte = radix__vmemmap_pte_populate(pmd, addr, node, NULL, NULL);
-				if (!pte)
-					return -ENOMEM;
-				vmemmap_verify(pte, node, addr, addr + PAGE_SIZE);
-
 				next = addr + PAGE_SIZE;
 				continue;
 			}
 
-			pte = radix__vmemmap_pte_populate(pmd, addr, node, NULL, pte_page(*tail_page_pte));
+			pte = radix__vmemmap_pte_populate(pmd, addr, node, NULL, tail_page);
 			if (!pte)
 				return -ENOMEM;
 			vmemmap_verify(pte, node, addr, addr + PAGE_SIZE);
diff --git a/arch/riscv/mm/init.c b/arch/riscv/mm/init.c
index fb37b0b67efec..857f9a55039ce 100644
--- a/arch/riscv/mm/init.c
+++ b/arch/riscv/mm/init.c
@@ -22,6 +22,7 @@
 #include <linux/hugetlb.h>
 #include <linux/kfence.h>
 #include <linux/execmem.h>
+#include <linux/vmemmap-optimization.h>
 
 #include <asm/alternative.h>
 #include <asm/fixmap.h>
diff --git a/arch/x86/entry/vdso/vdso32/fake_32bit_build.h b/arch/x86/entry/vdso/vdso32/fake_32bit_build.h
index bc3e549795c3f..72a92cb9b53d3 100644
--- a/arch/x86/entry/vdso/vdso32/fake_32bit_build.h
+++ b/arch/x86/entry/vdso/vdso32/fake_32bit_build.h
@@ -11,7 +11,7 @@
 #undef CONFIG_PGTABLE_LEVELS
 #undef CONFIG_ILLEGAL_POINTER_VALUE
 #undef CONFIG_SPARSEMEM_VMEMMAP
-#undef CONFIG_HUGETLB_PAGE_OPTIMIZE_VMEMMAP
+#undef CONFIG_VMEMMAP_OPTIMIZATION
 #undef CONFIG_NR_CPUS
 #undef CONFIG_PARAVIRT_XXL
 
diff --git a/fs/Kconfig b/fs/Kconfig
index d1c210c6508f0..1454b7fe9641f 100644
--- a/fs/Kconfig
+++ b/fs/Kconfig
@@ -278,6 +278,7 @@ config HUGETLB_PAGE_OPTIMIZE_VMEMMAP
 	def_bool HUGETLB_PAGE
 	depends on ARCH_WANT_OPTIMIZE_HUGETLB_VMEMMAP
 	depends on SPARSEMEM_VMEMMAP
+	select VMEMMAP_OPTIMIZATION
 
 config HUGETLB_PMD_PAGE_TABLE_SHARING
 	def_bool HUGETLB_PAGE
diff --git a/include/linux/mm.h b/include/linux/mm.h
index c49ef99b4413b..30a3365bca827 100644
--- a/include/linux/mm.h
+++ b/include/linux/mm.h
@@ -38,6 +38,7 @@
 #include <linux/bitops.h>
 #include <linux/iommu-debug-pagealloc.h>
 #include <linux/kcsan-checks.h>
+#include <linux/vmemmap-optimization.h>
 
 struct mempolicy;
 struct anon_vma;
@@ -5167,7 +5168,6 @@ static inline void vmem_altmap_free(struct vmem_altmap *altmap,
 }
 #endif
 
-#define VMEMMAP_RESERVE_NR	2
 #ifdef CONFIG_ARCH_WANT_OPTIMIZE_DAX_VMEMMAP
 static inline bool __vmemmap_can_optimize(struct vmem_altmap *altmap,
 					  struct dev_pagemap *pgmap)
@@ -5175,6 +5175,9 @@ static inline bool __vmemmap_can_optimize(struct vmem_altmap *altmap,
 	unsigned long nr_pages;
 	unsigned long nr_vmemmap_pages;
 
+	if (!IS_ENABLED(CONFIG_VMEMMAP_OPTIMIZATION))
+		return false;
+
 	if (!pgmap || !is_power_of_2(sizeof(struct page)))
 		return false;
 
@@ -5184,7 +5187,7 @@ static inline bool __vmemmap_can_optimize(struct vmem_altmap *altmap,
 	 * For vmemmap optimization with DAX we need minimum 2 vmemmap
 	 * pages. See layout diagram in Documentation/mm/vmemmap_dedup.rst
 	 */
-	return !altmap && (nr_vmemmap_pages > VMEMMAP_RESERVE_NR);
+	return !altmap && (nr_vmemmap_pages > VMEMMAP_OPTIMIZATION_PAGES);
 }
 /*
  * If we don't have an architecture override, use the generic rule
diff --git a/include/linux/mmzone.h b/include/linux/mmzone.h
index acd94cecc0d39..65de3bb13eb3a 100644
--- a/include/linux/mmzone.h
+++ b/include/linux/mmzone.h
@@ -96,28 +96,6 @@
 
 #define MAX_FOLIO_NR_PAGES	(1UL << MAX_FOLIO_ORDER)
 
-/*
- * HugeTLB Vmemmap Optimization (HVO) requires struct pages of the head page to
- * be naturally aligned with regard to the folio size.
- *
- * HVO which is only active if the size of struct page is a power of 2.
- */
-#define MAX_FOLIO_VMEMMAP_ALIGN \
-	(IS_ENABLED(CONFIG_HUGETLB_PAGE_OPTIMIZE_VMEMMAP) && \
-	 is_power_of_2(sizeof(struct page)) ? \
-	 MAX_FOLIO_NR_PAGES * sizeof(struct page) : 0)
-
-/* The number of retained vmemmap pages with HVO enabled. */
-#define VMEMMAP_OPTIMIZATION_PAGES		1
-#define VMEMMAP_OPTIMIZATION_NR_STRUCT_PAGES	\
-	(VMEMMAP_OPTIMIZATION_PAGES * PAGE_SIZE / sizeof(struct page))
-#define VMEMMAP_OPTIMIZATION_MIN_ORDER		(ilog2(VMEMMAP_OPTIMIZATION_NR_STRUCT_PAGES) + 1)
-
-#define __VMEMMAP_OPTIMIZATION_NR_ORDERS	\
-	(MAX_FOLIO_ORDER - VMEMMAP_OPTIMIZATION_MIN_ORDER + 1)
-#define VMEMMAP_OPTIMIZATION_NR_ORDERS		\
-	(__VMEMMAP_OPTIMIZATION_NR_ORDERS > 0 ? __VMEMMAP_OPTIMIZATION_NR_ORDERS : 0)
-
 enum migratetype {
 	MIGRATE_UNMOVABLE,
 	MIGRATE_MOVABLE,
@@ -1155,8 +1133,8 @@ struct zone {
 	/* Zone statistics */
 	atomic_long_t		vm_stat[NR_VM_ZONE_STAT_ITEMS];
 	atomic_long_t		vm_numa_event[NR_VM_NUMA_EVENT_ITEMS];
-#ifdef CONFIG_HUGETLB_PAGE_OPTIMIZE_VMEMMAP
-	struct page *vmemmap_tails[VMEMMAP_OPTIMIZATION_NR_ORDERS];
+#ifdef CONFIG_VMEMMAP_OPTIMIZATION
+	struct page **vmemmap_tails;
 #endif
 } ____cacheline_internodealigned_in_smp;
 
@@ -2019,7 +1997,7 @@ struct mem_section {
 	unsigned long section_mem_map;
 
 	struct mem_section_usage *usage;
-#ifdef CONFIG_HUGETLB_PAGE_OPTIMIZE_VMEMMAP
+#ifdef CONFIG_VMEMMAP_OPTIMIZATION
 	/*
 	 * Normally, sections hold regular (order-0) pages. However, for
 	 * sections with HVO enabled, this tracks the compound page order
@@ -2148,11 +2126,21 @@ static inline int online_device_section(const struct mem_section *section)
 
 	return section && ((section->section_mem_map & flags) == flags);
 }
+
+static inline struct zone *device_zone(int nid)
+{
+	return &NODE_DATA(nid)->node_zones[ZONE_DEVICE];
+}
 #else
 static inline int online_device_section(const struct mem_section *section)
 {
 	return 0;
 }
+
+static inline struct zone *device_zone(int nid)
+{
+	return NULL;
+}
 #endif
 
 static inline int online_section_nr(unsigned long nr)
diff --git a/include/linux/page-flags.h b/include/linux/page-flags.h
index 86dd0470da117..7080a6a1a79e7 100644
--- a/include/linux/page-flags.h
+++ b/include/linux/page-flags.h
@@ -208,14 +208,13 @@ enum pageflags {
 static __always_inline bool compound_info_has_mask(void)
 {
 	/*
-	 * Limit mask usage to HugeTLB vmemmap optimization (HVO) where it
-	 * makes a difference.
+	 * Limit mask usage to HVO where it makes a difference.
 	 *
 	 * The approach with mask would work in the wider set of conditions,
 	 * but it requires validating that struct pages are naturally aligned
 	 * for all orders up to the MAX_FOLIO_ORDER, which can be tricky.
 	 */
-	if (!IS_ENABLED(CONFIG_HUGETLB_PAGE_OPTIMIZE_VMEMMAP))
+	if (!IS_ENABLED(CONFIG_VMEMMAP_OPTIMIZATION))
 		return false;
 
 	return is_power_of_2(sizeof(struct page));
diff --git a/include/linux/vmemmap-optimization.h b/include/linux/vmemmap-optimization.h
new file mode 100644
index 0000000000000..fa9e9abd66560
--- /dev/null
+++ b/include/linux/vmemmap-optimization.h
@@ -0,0 +1,115 @@
+/* SPDX-License-Identifier: GPL-2.0-or-later */
+/*
+ * vmemmap-optimization.h
+ *
+ * Generic vmemmap optimization declarations.
+ *
+ * Author: Muchun Song <songmuchun@bytedance.com>
+ */
+#ifndef _LINUX_VMEMMAP_OPTIMIZATION_H
+#define _LINUX_VMEMMAP_OPTIMIZATION_H
+
+#include <linux/align.h>
+#include <linux/log2.h>
+#include <linux/mmdebug.h>
+#include <linux/mmzone.h>
+
+/*
+ * HugeTLB Vmemmap Optimization (HVO) requires struct pages of the head page to
+ * be naturally aligned with regard to the folio size.
+ *
+ * HVO which is only active if the size of struct page is a power of 2.
+ */
+#define MAX_FOLIO_VMEMMAP_ALIGN					\
+	(IS_ENABLED(CONFIG_VMEMMAP_OPTIMIZATION) &&		\
+	 is_power_of_2(sizeof(struct page)) ?			\
+	 MAX_FOLIO_NR_PAGES * sizeof(struct page) : 0)
+
+/* The number of retained vmemmap pages with HVO enabled. */
+#define VMEMMAP_OPTIMIZATION_PAGES		1
+#define VMEMMAP_OPTIMIZATION_NR_STRUCT_PAGES	\
+	(VMEMMAP_OPTIMIZATION_PAGES * PAGE_SIZE / sizeof(struct page))
+#define VMEMMAP_OPTIMIZATION_MIN_ORDER		(ilog2(VMEMMAP_OPTIMIZATION_NR_STRUCT_PAGES) + 1)
+
+#ifdef CONFIG_VMEMMAP_OPTIMIZATION
+static inline unsigned int section_compound_order(const struct mem_section *section)
+{
+	return section->compound_page_order;
+}
+
+static inline void section_set_compound_order(struct mem_section *section,
+					      unsigned int order)
+{
+	VM_WARN_ON(section_compound_order(section) && order &&
+		   section_compound_order(section) != order);
+	section->compound_page_order = order;
+}
+
+static inline void section_set_compound_order_range(unsigned long pfn,
+		unsigned long nr_pages, unsigned int order)
+{
+	unsigned long section_nr = pfn_to_section_nr(pfn);
+
+	if (!IS_ALIGNED(pfn | nr_pages, PAGES_PER_SECTION))
+		return;
+
+	for (unsigned long i = 0; i < nr_pages / PAGES_PER_SECTION; i++)
+		section_set_compound_order(__nr_to_section(section_nr + i), order);
+}
+
+static inline unsigned int pfn_to_section_compound_order(unsigned long pfn)
+{
+	return section_compound_order(__pfn_to_section(pfn));
+}
+
+struct page *vmemmap_shared_tail_page(unsigned int order, struct zone *zone);
+#else
+static inline unsigned int section_compound_order(const struct mem_section *section)
+{
+	return 0;
+}
+
+static inline void section_set_compound_order(struct mem_section *section,
+					      unsigned int order)
+{
+}
+
+static inline void section_set_compound_order_range(unsigned long pfn,
+		unsigned long nr_pages, unsigned int order)
+{
+}
+
+static inline unsigned int pfn_to_section_compound_order(unsigned long pfn)
+{
+	return 0;
+}
+
+static inline struct page *vmemmap_shared_tail_page(unsigned int order,
+						    struct zone *zone)
+{
+	return NULL;
+}
+#endif /* CONFIG_VMEMMAP_OPTIMIZATION */
+
+static inline bool vmemmap_optimizable_pfn(unsigned long pfn)
+{
+	const unsigned int order = pfn_to_section_compound_order(pfn);
+	const unsigned long nr_pages = 1UL << order;
+
+	if (!is_power_of_2(sizeof(struct page)))
+		return false;
+
+	return (pfn & (nr_pages - 1)) >= VMEMMAP_OPTIMIZATION_NR_STRUCT_PAGES;
+}
+
+static inline bool vmemmap_optimizable_order(unsigned int order)
+{
+	if (!IS_ENABLED(CONFIG_VMEMMAP_OPTIMIZATION))
+		return false;
+
+	if (!is_power_of_2(sizeof(struct page)))
+		return false;
+
+	return order >= VMEMMAP_OPTIMIZATION_MIN_ORDER;
+}
+#endif /* _LINUX_VMEMMAP_OPTIMIZATION_H */
diff --git a/mm/Kconfig b/mm/Kconfig
index bc7befafb47b5..30170a936f1fc 100644
--- a/mm/Kconfig
+++ b/mm/Kconfig
@@ -461,6 +461,10 @@ config SPARSEMEM_VMEMMAP
 	  pfn_to_page and page_to_pfn operations.  This is the most
 	  efficient option when sufficient kernel resources are available.
 
+config VMEMMAP_OPTIMIZATION
+	bool
+	depends on SPARSEMEM_VMEMMAP
+
 #
 # Select this config option from the architecture Kconfig, if it is preferred
 # to enable the feature of HugeTLB/dev_dax vmemmap optimization.
@@ -1220,6 +1224,7 @@ config ZONE_DMA32
 config ZONE_DEVICE
 	bool "Device memory (pmem, HMM, etc...) hotplug support"
 	depends on MEMORY_HOTREMOVE
+	select VMEMMAP_OPTIMIZATION if ARCH_WANT_OPTIMIZE_DAX_VMEMMAP
 	select XARRAY_MULTI
 
 	help
diff --git a/mm/hugetlb.c b/mm/hugetlb.c
index fd00141b089a9..2003439ea13c6 100644
--- a/mm/hugetlb.c
+++ b/mm/hugetlb.c
@@ -38,6 +38,7 @@
 #include <linux/mm_inline.h>
 #include <linux/padata.h>
 #include <linux/pgalloc.h>
+#include <linux/vmemmap-optimization.h>
 
 #include <asm/page.h>
 #include <asm/tlb.h>
@@ -52,7 +53,6 @@
 #include "hugetlb_cma.h"
 #include "hugetlb_internal.h"
 #include "mm_init.h"
-#include "sparse.h"
 #include <linux/page-isolation.h>
 
 #define HUGE_BOOTMEM_ZONES_VALID	BIT(0)
diff --git a/mm/hugetlb_vmemmap.c b/mm/hugetlb_vmemmap.c
index f977d0a7e0027..0057fa2a16a89 100644
--- a/mm/hugetlb_vmemmap.c
+++ b/mm/hugetlb_vmemmap.c
@@ -15,11 +15,10 @@
 #include <linux/mmdebug.h>
 #include <linux/pagewalk.h>
 #include <linux/pgalloc.h>
+#include <linux/vmemmap-optimization.h>
 
 #include <asm/tlbflush.h>
 #include "hugetlb_vmemmap.h"
-#include "sparse.h"
-#include "internal.h"
 
 /**
  * struct vmemmap_remap_walk - walk vmemmap page table
@@ -493,32 +492,6 @@ static bool vmemmap_should_optimize_folio(const struct hstate *h, struct folio *
 	return true;
 }
 
-static struct page *vmemmap_get_tail(unsigned int order, struct zone *zone)
-{
-	const unsigned int idx = order - VMEMMAP_OPTIMIZATION_MIN_ORDER;
-	struct page *tail, *p;
-	int node = zone_to_nid(zone);
-
-	tail = READ_ONCE(zone->vmemmap_tails[idx]);
-	if (likely(tail))
-		return tail;
-
-	tail = alloc_pages_node(node, GFP_KERNEL | __GFP_ZERO, 0);
-	if (!tail)
-		return NULL;
-
-	p = page_to_virt(tail);
-	for (int i = 0; i < PAGE_SIZE / sizeof(struct page); i++)
-		init_compound_tail(p + i, NULL, order, zone);
-
-	if (cmpxchg(&zone->vmemmap_tails[idx], NULL, tail)) {
-		__free_page(tail);
-		tail = READ_ONCE(zone->vmemmap_tails[idx]);
-	}
-
-	return tail;
-}
-
 static int __hugetlb_vmemmap_optimize_folio(const struct hstate *h,
 					    struct folio *folio,
 					    struct list_head *vmemmap_pages,
@@ -535,7 +508,7 @@ static int __hugetlb_vmemmap_optimize_folio(const struct hstate *h,
 		return ret;
 
 	nid = folio_nid(folio);
-	vmemmap_tail = vmemmap_get_tail(h->order, folio_zone(folio));
+	vmemmap_tail = vmemmap_shared_tail_page(h->order, folio_zone(folio));
 	if (!vmemmap_tail)
 		return -ENOMEM;
 
diff --git a/mm/internal.h b/mm/internal.h
index da14c56fb24e1..0dca33db068f6 100644
--- a/mm/internal.h
+++ b/mm/internal.h
@@ -786,15 +786,6 @@ static inline void prep_compound_tail(struct page *tail,
 	VM_WARN_ON_ONCE(tail->private);
 }
 
-static inline void init_compound_tail(struct page *tail,
-		const struct page *head, unsigned int order, struct zone *zone)
-{
-	atomic_set(&tail->_mapcount, -1);
-	set_page_node(tail, zone_to_nid(zone));
-	set_page_zone(tail, zone_idx(zone));
-	prep_compound_tail(tail, head, order);
-}
-
 #if defined CONFIG_COMPACTION || defined CONFIG_CMA
 
 /*
diff --git a/mm/memory_hotplug.c b/mm/memory_hotplug.c
index b428da66d279c..d7a59167bec43 100644
--- a/mm/memory_hotplug.c
+++ b/mm/memory_hotplug.c
@@ -43,6 +43,7 @@
 #include "mm_init.h"
 #include "page_alloc.h"
 #include "shuffle.h"
+#include "sparse.h"
 
 enum {
 	MEMMAP_ON_MEMORY_DISABLE = 0,
@@ -554,8 +555,9 @@ void remove_pfn_range_from_zone(struct zone *zone,
 		/* Select all remaining pages up to the next section boundary */
 		cur_nr_pages =
 			min(end_pfn - pfn, SECTION_ALIGN_UP(pfn + 1) - pfn);
-		page_init_poison(pfn_to_page(pfn),
-				 sizeof(struct page) * cur_nr_pages);
+		if (!section_vmemmap_optimizable(__pfn_to_section(pfn)))
+			page_init_poison(pfn_to_page(pfn),
+					 sizeof(struct page) * cur_nr_pages);
 	}
 
 	/*
diff --git a/mm/mm_init.c b/mm/mm_init.c
index 97e0158d2aca5..56bb4567a4940 100644
--- a/mm/mm_init.c
+++ b/mm/mm_init.c
@@ -1049,19 +1049,14 @@ static void zone_device_page_init_from_template(struct page *page,
  * of an altmap. See vmemmap_populate_compound_pages().
  */
 static inline unsigned long compound_nr_pages(unsigned long pfn,
-					      struct vmem_altmap *altmap,
 					      struct dev_pagemap *pgmap)
 {
-	/*
-	 * If DAX memory is hot-plugged into an unoccupied subsection
-	 * of an early section, the unoptimized boot memmap is reused.
-	 * See section_activate().
-	 */
-	if (early_section(__pfn_to_section(pfn)) ||
-	    !vmemmap_can_optimize(altmap, pgmap))
+	const struct mem_section *ms = __pfn_to_section(pfn);
+
+	if (!section_vmemmap_optimizable(ms))
 		return pgmap_vmemmap_nr(pgmap);
 
-	return VMEMMAP_RESERVE_NR * (PAGE_SIZE / sizeof(struct page));
+	return VMEMMAP_OPTIMIZATION_PAGES * (PAGE_SIZE / sizeof(struct page));
 }
 
 static void __ref memmap_init_compound(struct page *head,
@@ -1144,7 +1139,7 @@ void __ref memmap_init_zone_device(struct zone *zone,
 	memcpy(&template, page, sizeof(*page));
 	if (pfns_per_compound != 1)
 		memmap_init_compound(page, pfn, zone_idx, nid, pgmap,
-				     compound_nr_pages(pfn, altmap, pgmap));
+				     compound_nr_pages(pfn, pgmap));
 	pfn += pfns_per_compound;
 
 	/* Initialize the remaining head pages from template. */
@@ -1160,7 +1155,7 @@ void __ref memmap_init_zone_device(struct zone *zone,
 			continue;
 
 		memmap_init_compound(page, pfn, zone_idx, nid, pgmap,
-				     compound_nr_pages(pfn, altmap, pgmap));
+				     compound_nr_pages(pfn, pgmap));
 	}
 
 	pageblock_migratetype_init_range(start_pfn, nr_pages, MIGRATE_MOVABLE,
diff --git a/mm/sparse-vmemmap.c b/mm/sparse-vmemmap.c
index f22d815d7af04..4be85e194c782 100644
--- a/mm/sparse-vmemmap.c
+++ b/mm/sparse-vmemmap.c
@@ -35,34 +35,20 @@
 /*
  * Flags for vmemmap_populate_range and friends.
  */
-/* Get a ref on the head page struct page, for ZONE_DEVICE compound pages */
-#define VMEMMAP_POPULATE_PAGEREF	0x0001
+/* Vmemmap population for ZONE_DEVICE compound pages */
+#define VMEMMAP_POPULATE_DAX		0x0001
 
 #include "internal.h"
 #include "mm_init.h"
 #include "sparse.h"
 
-/*
- * Allocate a block of memory to be used to back the virtual memory map
- * or to back the page tables that are used to create the mapping.
- * Uses the main allocators if they are available, else bootmem.
- */
-
-static void * __ref __earlyonly_bootmem_alloc(int node,
-				unsigned long size,
-				unsigned long align,
-				unsigned long goal)
-{
-	return memmap_alloc(size, align, goal, node, false);
-}
-
-void * __meminit vmemmap_alloc_block(unsigned long size, int node)
+void __ref *vmemmap_alloc_block(unsigned long size, int node)
 {
 	/* If the main allocator is up use that, fallback to bootmem. */
 	if (slab_is_available()) {
 		gfp_t gfp_mask = GFP_KERNEL|__GFP_RETRY_MAYFAIL|__GFP_NOWARN;
 		int order = get_order(size);
-		static bool warned __meminitdata;
+		static bool warned;
 		struct page *page;
 
 		page = alloc_pages_node(node, gfp_mask, order);
@@ -76,8 +62,7 @@ void * __meminit vmemmap_alloc_block(unsigned long size, int node)
 		}
 		return NULL;
 	} else
-		return __earlyonly_bootmem_alloc(node, size, size,
-				__pa(MAX_DMA_ADDRESS));
+		return memmap_alloc(size, size, __pa(MAX_DMA_ADDRESS), node, false);
 }
 
 static void * __meminit altmap_alloc_block_buf(unsigned long size,
@@ -146,29 +131,27 @@ void __meminit vmemmap_verify(pte_t *pte, int node,
 			start, end - 1);
 }
 
-int __meminit section_nr_vmemmap_pages(unsigned long pfn, unsigned long nr_pages,
-		struct vmem_altmap *altmap, struct dev_pagemap *pgmap)
+int __meminit section_nr_vmemmap_pages(unsigned long pfn, unsigned long nr_pages)
 {
 	const struct mem_section *ms = __pfn_to_section(pfn);
-	const int order = pgmap ? pgmap->vmemmap_shift : section_compound_order(ms);
-	const int vmemmap_pages = pgmap ? VMEMMAP_RESERVE_NR : VMEMMAP_OPTIMIZATION_PAGES;
+	const int order = section_compound_order(ms);
 	const unsigned long pages_per_compound = 1UL << order;
 
 	VM_WARN_ON_ONCE(!IS_ALIGNED(pfn | nr_pages, PAGES_PER_SUBSECTION));
 	VM_WARN_ON_ONCE(nr_pages > PAGES_PER_SECTION);
 
-	if (!vmemmap_can_optimize(altmap, pgmap) && !section_vmemmap_optimizable(ms))
+	if (!section_vmemmap_optimizable(ms))
 		return DIV_ROUND_UP(nr_pages * sizeof(struct page), PAGE_SIZE);
 
 	if (order < PFN_SECTION_SHIFT) {
 		VM_WARN_ON_ONCE(!IS_ALIGNED(pfn | nr_pages, pages_per_compound));
-		return vmemmap_pages * nr_pages / pages_per_compound;
+		return VMEMMAP_OPTIMIZATION_PAGES * nr_pages / pages_per_compound;
 	}
 
 	VM_WARN_ON_ONCE(!IS_ALIGNED(pfn | nr_pages, PAGES_PER_SECTION));
 
 	if (IS_ALIGNED(pfn, pages_per_compound))
-		return vmemmap_pages;
+		return VMEMMAP_OPTIMIZATION_PAGES;
 
 	return 0;
 }
@@ -184,52 +167,95 @@ static void * __meminit vmemmap_alloc_block_zero(unsigned long size, int node)
 	return p;
 }
 
-#ifdef CONFIG_HUGETLB_PAGE_OPTIMIZE_VMEMMAP
-static __meminit struct page *vmemmap_get_tail(unsigned int order, struct zone *zone)
+#ifdef CONFIG_VMEMMAP_OPTIMIZATION
+#define VMEMMAP_OPTIMIZATION_NR_ORDERS	(MAX_FOLIO_ORDER - VMEMMAP_OPTIMIZATION_MIN_ORDER + 1)
+
+static __ref struct page **vmemmap_tails(struct zone *zone)
 {
-	struct page *p, *tail;
-	unsigned int idx;
-	int node = zone_to_nid(zone);
+	const size_t size = array_size(VMEMMAP_OPTIMIZATION_NR_ORDERS,
+				       sizeof(*zone->vmemmap_tails));
+	struct page **pages = READ_ONCE(zone->vmemmap_tails);
 
-	if (WARN_ON_ONCE(order < VMEMMAP_OPTIMIZATION_MIN_ORDER))
-		return NULL;
-	if (WARN_ON_ONCE(order > MAX_FOLIO_ORDER))
-		return NULL;
+	if (pages)
+		return pages;
 
-	idx = order - VMEMMAP_OPTIMIZATION_MIN_ORDER;
-	tail = zone->vmemmap_tails[idx];
-	if (tail)
-		return tail;
-	p = vmemmap_alloc_block_zero(PAGE_SIZE, node);
-	if (!p)
+	pages = slab_is_available() ? kzalloc_objs(*pages, VMEMMAP_OPTIMIZATION_NR_ORDERS) :
+		memblock_alloc(size, __alignof__(*pages));
+	if (!pages)
 		return NULL;
-	for (int i = 0; i < PAGE_SIZE / sizeof(struct page); i++)
-		init_compound_tail(p + i, NULL, order, zone);
 
-	tail = virt_to_page(p);
-	zone->vmemmap_tails[idx] = tail;
+	if (cmpxchg(&zone->vmemmap_tails, NULL, pages) != NULL) {
+		if (slab_is_available())
+			kfree(pages);
+		else
+			memblock_free(pages, size);
+		pages = READ_ONCE(zone->vmemmap_tails);
+	}
 
-	return tail;
+	return pages;
 }
-#else
-static inline struct page *vmemmap_get_tail(unsigned int order, struct zone *zone)
+
+struct page __ref *vmemmap_shared_tail_page(unsigned int order, struct zone *zone)
 {
-	return NULL;
+	const unsigned int idx = order - VMEMMAP_OPTIMIZATION_MIN_ORDER;
+	struct page *page, **pages;
+	void *addr;
+
+	if (WARN_ON_ONCE(idx >= VMEMMAP_OPTIMIZATION_NR_ORDERS))
+		return NULL;
+
+	pages = vmemmap_tails(zone);
+	if (!pages)
+		return NULL;
+
+	page = READ_ONCE(pages[idx]);
+	if (page)
+		return page;
+
+	addr = vmemmap_alloc_block(PAGE_SIZE, zone_to_nid(zone));
+	if (!addr)
+		return NULL;
+
+	for (int i = 0; i < PAGE_SIZE / sizeof(struct page); i++) {
+		page = (struct page *)addr + i;
+		mm_zero_struct_page(page);
+		atomic_set(&page->_mapcount, -1);
+		set_page_node(page, zone_to_nid(zone));
+		set_page_zone(page, zone_idx(zone));
+		prep_compound_tail(page, NULL, order);
+		if (zone_is_zone_device(zone))
+			__SetPageReserved(page);
+	}
+
+	page = virt_to_page(addr);
+	if (cmpxchg(&pages[idx], NULL, page) != NULL) {
+		if (slab_is_available())
+			__free_page(page);
+		else
+			memblock_free(addr, PAGE_SIZE);
+		page = READ_ONCE(pages[idx]);
+	}
+
+	return page;
 }
 #endif
 
 static __meminit void *vmemmap_alloc_pte(unsigned long pfn, int node,
-					 struct vmem_altmap *altmap)
+		struct vmem_altmap *altmap, unsigned long flags)
 {
 	struct zone *zone;
 	struct page *page;
 	const unsigned int order = pfn_to_section_compound_order(pfn);
 
-	if (!vmemmap_optimizable_pfn(pfn))
+	/*
+	 * Device DAX still relies on vmemmap_populate_compound_pages() for
+	 * head/first-tail allocation and tail-page reuse.
+	 */
+	if (!vmemmap_optimizable_pfn(pfn) || flags & VMEMMAP_POPULATE_DAX)
 		return vmemmap_alloc_block_buf(PAGE_SIZE, node, altmap);
 
 	zone = pfn_to_zone(pfn, node);
-	page = vmemmap_get_tail(order, zone);
+	page = vmemmap_shared_tail_page(order, zone);
 	if (!page)
 		return NULL;
 
@@ -247,7 +273,7 @@ static pte_t * __meminit vmemmap_pte_populate(pmd_t *pmd, unsigned long addr, in
 		pte_t entry;
 
 		if (ptpfn == (unsigned long)-1) {
-			void *p = vmemmap_alloc_pte(pfn, node, altmap);
+			void *p = vmemmap_alloc_pte(pfn, node, altmap, flags);
 
 			if (!p)
 				return NULL;
@@ -256,14 +282,18 @@ static pte_t * __meminit vmemmap_pte_populate(pmd_t *pmd, unsigned long addr, in
 			/*
 			 * When a PTE/PMD entry is freed from the init_mm
 			 * there's a free_pages() call to this page allocated
-			 * above. Thus this get_page() is paired with the
+			 * above. Thus this try_get_page() is paired with the
 			 * put_page_testzero() on the freeing path.
 			 * This can only called by certain ZONE_DEVICE path,
 			 * and through vmemmap_populate_compound_pages() when
 			 * slab is available.
+			 *
+			 * Use try_get_page() to prevent the shared page refcount
+			 * from overflowing.
 			 */
-			if (flags & VMEMMAP_POPULATE_PAGEREF)
-				get_page(pfn_to_page(ptpfn));
+			if ((flags & VMEMMAP_POPULATE_DAX) &&
+			    !try_get_page(pfn_to_page(ptpfn)))
+				return NULL;
 		}
 		entry = pfn_pte(ptpfn, PAGE_KERNEL);
 		set_pte_at(&init_mm, addr, pte, entry);
@@ -497,69 +527,42 @@ static bool __meminit reuse_compound_section(unsigned long start_pfn,
 	return !IS_ALIGNED(offset, nr_pages) && nr_pages > PAGES_PER_SUBSECTION;
 }
 
-static pte_t * __meminit compound_section_tail_page(unsigned long addr)
-{
-	pte_t *pte;
-
-	addr -= PAGE_SIZE;
-
-	/*
-	 * Assuming sections are populated sequentially, the previous section's
-	 * page data can be reused.
-	 */
-	pte = pte_offset_kernel(pmd_off_k(addr), addr);
-	if (!pte)
-		return NULL;
-
-	return pte;
-}
-
 static int __meminit vmemmap_populate_compound_pages(unsigned long start_pfn,
 						     unsigned long start,
 						     unsigned long end, int node,
 						     struct dev_pagemap *pgmap)
 {
+	const unsigned long flags = VMEMMAP_POPULATE_DAX;
+	const unsigned int order = pfn_to_section_compound_order(start_pfn);
 	unsigned long size, addr;
 	pte_t *pte;
+	struct page *page;
 	int rc;
 
-	if (reuse_compound_section(start_pfn, pgmap)) {
-		pte = compound_section_tail_page(start);
-		if (!pte)
-			return -ENOMEM;
+	page = vmemmap_shared_tail_page(order, device_zone(node));
+	if (!page)
+		return -ENOMEM;
 
-		/*
-		 * Reuse the page that was populated in the prior iteration
-		 * with just tail struct pages.
-		 */
+	if (reuse_compound_section(start_pfn, pgmap))
 		return vmemmap_populate_range(start, end, node, NULL,
-					      pte_pfn(ptep_get(pte)),
-					      VMEMMAP_POPULATE_PAGEREF);
-	}
+					      page_to_pfn(page), flags);
 
-	size = min(end - start, pgmap_vmemmap_nr(pgmap) * sizeof(struct page));
+	size = min(end - start, (1UL << order) * sizeof(struct page));
 	for (addr = start; addr < end; addr += size) {
 		unsigned long next, last = addr + size;
 
 		/* Populate the head page vmemmap page */
-		pte = vmemmap_populate_address(addr, node, NULL, -1, 0);
-		if (!pte)
-			return -ENOMEM;
-
-		/* Populate the tail pages vmemmap page */
-		next = addr + PAGE_SIZE;
-		pte = vmemmap_populate_address(next, node, NULL, -1, 0);
+		pte = vmemmap_populate_address(addr, node, NULL, -1, flags);
 		if (!pte)
 			return -ENOMEM;
 
 		/*
-		 * Reuse the previous page for the rest of tail pages
+		 * Reuse the shared page for the rest of tail pages
 		 * See layout diagram in Documentation/mm/vmemmap_dedup.rst
 		 */
-		next += PAGE_SIZE;
+		next = addr + PAGE_SIZE;
 		rc = vmemmap_populate_range(next, last, node, NULL,
-					    pte_pfn(ptep_get(pte)),
-					    VMEMMAP_POPULATE_PAGEREF);
+					    page_to_pfn(page), flags);
 		if (rc)
 			return -ENOMEM;
 	}
@@ -581,7 +584,7 @@ struct page * __meminit __populate_section_memmap(unsigned long pfn,
 		!IS_ALIGNED(nr_pages, PAGES_PER_SUBSECTION)))
 		return NULL;
 
-	if (vmemmap_can_optimize(altmap, pgmap))
+	if (pgmap && section_vmemmap_optimizable(__pfn_to_section(pfn)))
 		r = vmemmap_populate_compound_pages(pfn, start, end, nid, pgmap);
 	else
 		r = vmemmap_populate(start, end, nid, altmap);
@@ -670,7 +673,7 @@ static struct page * __meminit populate_section_memmap(unsigned long pfn,
 	struct page *page = __populate_section_memmap(pfn, nr_pages, nid, altmap,
 						      pgmap);
 
-	memmap_pages_add(section_nr_vmemmap_pages(pfn, nr_pages, altmap, pgmap));
+	memmap_pages_add(section_nr_vmemmap_pages(pfn, nr_pages));
 
 	return page;
 }
@@ -681,7 +684,7 @@ static void depopulate_section_memmap(unsigned long pfn, unsigned long nr_pages,
 	unsigned long start = (unsigned long) pfn_to_page(pfn);
 	unsigned long end = start + nr_pages * sizeof(struct page);
 
-	memmap_pages_add(-section_nr_vmemmap_pages(pfn, nr_pages, altmap, pgmap));
+	memmap_pages_add(-section_nr_vmemmap_pages(pfn, nr_pages));
 	vmemmap_free(start, end, altmap);
 }
 
@@ -691,8 +694,7 @@ static void free_map_bootmem(struct page *memmap)
 	unsigned long end = (unsigned long)(memmap + PAGES_PER_SECTION);
 	unsigned long pfn = page_to_pfn(memmap);
 
-	memmap_boot_pages_add(-section_nr_vmemmap_pages(pfn, PAGES_PER_SECTION,
-							NULL, NULL));
+	memmap_boot_pages_add(-section_nr_vmemmap_pages(pfn, PAGES_PER_SECTION));
 	vmemmap_free(start, end, NULL);
 }
 
@@ -800,8 +802,10 @@ static void section_deactivate(unsigned long pfn, unsigned long nr_pages,
 	else if (memmap)
 		free_map_bootmem(memmap);
 
-	if (empty)
+	if (empty) {
 		ms->section_mem_map = (unsigned long)NULL;
+		section_set_compound_order(ms, 0);
+	}
 }
 
 static struct page * __meminit section_activate(int nid, unsigned long pfn,
@@ -811,8 +815,13 @@ static struct page * __meminit section_activate(int nid, unsigned long pfn,
 	struct mem_section *ms = __pfn_to_section(pfn);
 	struct mem_section_usage *usage = NULL;
 	struct page *memmap;
+	unsigned int order;
 	int rc;
 
+	order = vmemmap_can_optimize(altmap, pgmap) ? pgmap->vmemmap_shift : 0;
+	if (nr_pages < PAGES_PER_SECTION && section_compound_order(ms))
+		return ERR_PTR(-EOPNOTSUPP);
+
 	if (!ms->usage) {
 		usage = kzalloc(mem_section_usage_size(), GFP_KERNEL);
 		if (!usage)
@@ -838,6 +847,7 @@ static struct page * __meminit section_activate(int nid, unsigned long pfn,
 	if (nr_pages < PAGES_PER_SECTION && early_section(ms))
 		return pfn_to_page(pfn);
 
+	section_set_compound_order_range(pfn, nr_pages, order);
 	memmap = populate_section_memmap(pfn, nr_pages, nid, altmap, pgmap);
 	if (!memmap) {
 		section_deactivate(pfn, nr_pages, altmap, pgmap);
@@ -883,13 +893,16 @@ int __meminit sparse_add_section(int nid, unsigned long start_pfn,
 	if (IS_ERR(memmap))
 		return PTR_ERR(memmap);
 
+	ms = __nr_to_section(section_nr);
 	/*
-	 * Poison uninitialized struct pages in order to catch invalid flags
-	 * combinations.
+	 * Poison uninitialized struct pages to catch invalid flag combinations.
+	 *
+	 * Tail struct pages in a vmemmap-optimized section are initialized and
+	 * shared during vmemmap population, so they must not be overwritten here.
 	 */
-	page_init_poison(memmap, sizeof(struct page) * nr_pages);
+	if (!section_vmemmap_optimizable(ms))
+		page_init_poison(memmap, sizeof(struct page) * nr_pages);
 
-	ms = __nr_to_section(section_nr);
 	__section_mark_present(ms, section_nr);
 
 	/* Align memmap to section boundary in the subsection case */
diff --git a/mm/sparse.c b/mm/sparse.c
index cc28bb41fdb1f..b75921c622ede 100644
--- a/mm/sparse.c
+++ b/mm/sparse.c
@@ -250,8 +250,7 @@ static void __init sparse_init_nid(int nid, unsigned long pnum_begin,
 						nid, NULL, NULL);
 		if (!map)
 			panic("Failed to allocate memmap for section %lu\n", pnum);
-		memmap_boot_pages_add(section_nr_vmemmap_pages(pfn, PAGES_PER_SECTION,
-							       NULL, NULL));
+		memmap_boot_pages_add(section_nr_vmemmap_pages(pfn, PAGES_PER_SECTION));
 		sparse_init_one_section(__nr_to_section(pnum), pnum, map, usage,
 					SECTION_IS_EARLY);
 		usage = (void *)usage + mem_section_usage_size();
diff --git a/mm/sparse.h b/mm/sparse.h
index d3a71ef4fad0f..530692cdd516f 100644
--- a/mm/sparse.h
+++ b/mm/sparse.h
@@ -9,80 +9,7 @@
 #define __MM_SPARSE_H
 
 #include <linux/mmzone.h>
-
-#ifdef CONFIG_HUGETLB_PAGE_OPTIMIZE_VMEMMAP
-static inline unsigned int section_compound_order(const struct mem_section *section)
-{
-	return section->compound_page_order;
-}
-
-static inline void section_set_compound_order(struct mem_section *section,
-					      unsigned int order)
-{
-	VM_WARN_ON(section_compound_order(section) && order &&
-		   section_compound_order(section) != order);
-	section->compound_page_order = order;
-}
-
-static inline void section_set_compound_order_range(unsigned long pfn,
-		unsigned long nr_pages, unsigned int order)
-{
-	unsigned long section_nr = pfn_to_section_nr(pfn);
-
-	if (!IS_ALIGNED(pfn | nr_pages, PAGES_PER_SECTION))
-		return;
-
-	for (unsigned long i = 0; i < nr_pages / PAGES_PER_SECTION; i++)
-		section_set_compound_order(__nr_to_section(section_nr + i), order);
-}
-
-static inline unsigned int pfn_to_section_compound_order(unsigned long pfn)
-{
-	return section_compound_order(__pfn_to_section(pfn));
-}
-#else
-static inline unsigned int section_compound_order(const struct mem_section *section)
-{
-	return 0;
-}
-
-static inline void section_set_compound_order(struct mem_section *section,
-					      unsigned int order)
-{
-}
-
-static inline void section_set_compound_order_range(unsigned long pfn,
-		unsigned long nr_pages, unsigned int order)
-{
-}
-
-static inline unsigned int pfn_to_section_compound_order(unsigned long pfn)
-{
-	return 0;
-}
-#endif
-
-static inline bool vmemmap_optimizable_pfn(unsigned long pfn)
-{
-	const unsigned int order = pfn_to_section_compound_order(pfn);
-	const unsigned long nr_pages = 1UL << order;
-
-	if (!is_power_of_2(sizeof(struct page)))
-		return false;
-
-	return (pfn & (nr_pages - 1)) >= VMEMMAP_OPTIMIZATION_NR_STRUCT_PAGES;
-}
-
-static inline bool vmemmap_optimizable_order(unsigned int order)
-{
-	if (!IS_ENABLED(CONFIG_HUGETLB_PAGE_OPTIMIZE_VMEMMAP))
-		return false;
-
-	if (!is_power_of_2(sizeof(struct page)))
-		return false;
-
-	return order >= VMEMMAP_OPTIMIZATION_MIN_ORDER;
-}
+#include <linux/vmemmap-optimization.h>
 
 /*
  * mm/sparse.c
@@ -143,12 +70,10 @@ static inline void sparse_sections_init(void) {}
  */
 #ifdef CONFIG_SPARSEMEM_VMEMMAP
 void sparse_init_subsection_map(void);
-int section_nr_vmemmap_pages(unsigned long pfn, unsigned long nr_pages,
-		struct vmem_altmap *altmap, struct dev_pagemap *pgmap);
+int section_nr_vmemmap_pages(unsigned long pfn, unsigned long nr_pages);
 #else
 static inline void sparse_init_subsection_map(void) {}
-static inline int section_nr_vmemmap_pages(unsigned long pfn, unsigned long nr_pages,
-		struct vmem_altmap *altmap, struct dev_pagemap *pgmap)
+static inline int section_nr_vmemmap_pages(unsigned long pfn, unsigned long nr_pages)
 {
 	return DIV_ROUND_UP(nr_pages * sizeof(struct page), PAGE_SIZE);
 }

51/3 2026/10/01 14:36 llm kmsan-evaluator
1m Model:
gemini-3.8-flash

Args:
null

Results:
null

Tokens:
							input: 19537
							output: 19
							thoughts: 1730
						

Thoughts:
**KMSAN Analysis of Vmemmap Optimization Patch**

Okay, here's the deal: I've got a patch in front of me that generalizes vmemmap deduplication, which was previously tied to specific things like hugetlb and device DAX. My job is to figure out whether this warrants a dedicated KMSAN fuzzing run. That means figuring out if it introduces any new uninitialized memory risks that KASAN wouldn't catch.

First, I went through the patch changes. It's touching a *lot* of files, everything from documentation to architecture-specific code and core memory management. It's essentially pulling out common logic for optimizing the vmemmap and introducing a new header file, `vmemmap-optimization.h`. A key function is `vmemmap_shared_tail_page`, which allocates and initializes shared tail pages for hugetlb and zone devices.

The code allocates a block, *then* calls `mm_zero_struct_page(page)` for each page within that block, *then* sets the `_mapcount` and the zone and node. There's also some special handling for `zone_device`.  Then, there's `sparse_add_section` and `remove_pfn_range_from_zone`, which have checks about the optimization status and a call to `page_init_poison` – but it *skips* the poison if the section is vmemmap-optimizable. It *says* it's skipping the poison because the tail struct pages are already initialized and shared during vmemmap population.

Now, I need to zoom in on those initialization steps.  Does KMSAN track `struct page` itself?  *No*. KMSAN isn't designed to track metadata directly. It focuses on user-accessible memory and kernel-to-user information leaks.

So, let's refresh on what KMSAN is for: It detects uninitialized memory *reads* in cases that KASAN, lockdep, or other standard tools won't catch. KMSAN is expensive, and you only use it if there's a *unique* risk. That means changes that introduce or modify: 1. Kernel structures sent to userspace, 2. Conditional logic or branching that uses uninitialized variables, 3. Allocation of complex data structures where uninitialized fields could be read later in reachable code paths, or 4. Bounds checks that allow access to uninitialized bytes of existing buffers.

Okay, back to the code. In `vmemmap_shared_tail_page`, I see that the code explicitly zero-initializes the allocated memory using `mm_zero_struct_page`. Then, the `vmemmap_tails` allocation also uses `kzalloc_objs` (which zeroes) or uses memblock, which *also* zeroes the memory. So, it seems like the code is *already* taking care of zero-initialization.

There's the comment about skipping `page_init_poison` in `sparse_add_section`, but that's *because* the pages are already initialized.

Given that the code *explicitly* initializes the shared tail pages and that KMSAN isn't going to help here as it doesn't track `struct page`, it's zeroed by initialization, there's no obvious risk of uninitialized memory *reads* that are unique to KMSAN. Therefore, `NeedsKMSAN=false` for this patch.



52/3 2026/10/01 14:37 tool grepper
0m Args:
{
  "Expression": "page_init_poison"
}

Results:
{
  "Output": "include/linux/page-flags.h=326=static inline int PagePoisoned(const struct page *page)\n--\ninclude/linux/page-flags.h-331-#ifdef CONFIG_DEBUG_VM\ninclude/linux/page-flags.h:332:void page_init_poison(struct page *page, size_t size);\ninclude/linux/page-flags.h-333-#else\ninclude/linux/page-flags.h:334:static inline void page_init_poison(struct page *page, size_t size)\ninclude/linux/page-flags.h-335-{\n--\nmm/debug.c=306=EXPORT_SYMBOL(dump_vmg);\nmm/debug.c-307-\nmm/debug.c:308:static bool page_init_poisoning __read_mostly = true;\nmm/debug.c-309-\nmm/debug.c=310=static int __init setup_vm_debug(char *str)\nmm/debug.c-311-{\nmm/debug.c:312:\tbool __page_init_poisoning = true;\nmm/debug.c-313-\n--\nmm/debug.c-320-\nmm/debug.c:321:\t__page_init_poisoning = false;\nmm/debug.c-322-\tif (*str == '-')\n--\nmm/debug.c-327-\t\tcase 'p':\nmm/debug.c:328:\t\t\t__page_init_poisoning = true;\nmm/debug.c-329-\t\t\tbreak;\n--\nmm/debug.c-337-out:\nmm/debug.c:338:\tif (page_init_poisoning \u0026\u0026 !__page_init_poisoning)\nmm/debug.c-339-\t\tpr_warn(\"Page struct poisoning disabled by kernel command line option 'vm_debug'\\n\");\nmm/debug.c-340-\nmm/debug.c:341:\tpage_init_poisoning = __page_init_poisoning;\nmm/debug.c-342-\n--\nmm/debug.c=345=__setup(\"vm_debug\", setup_vm_debug);\nmm/debug.c-346-\nmm/debug.c:347:void page_init_poison(struct page *page, size_t size)\nmm/debug.c-348-{\nmm/debug.c:349:\tif (page_init_poisoning)\nmm/debug.c-350-\t\tmemset(page, PAGE_POISON_PATTERN, size);\n--\nmm/memory_hotplug.c=543=void remove_pfn_range_from_zone(struct zone *zone,\n--\nmm/memory_hotplug.c-558-\t\tif (!section_vmemmap_optimizable(__pfn_to_section(pfn)))\nmm/memory_hotplug.c:559:\t\t\tpage_init_poison(pfn_to_page(pfn),\nmm/memory_hotplug.c-560-\t\t\t\t\t sizeof(struct page) * cur_nr_pages);\n--\nmm/mm_init.c=1608=void __init *memmap_alloc(phys_addr_t size, phys_addr_t align,\n--\nmm/mm_init.c-1626-\tif (ptr \u0026\u0026 size \u003e 0)\nmm/mm_init.c:1627:\t\tpage_init_poison(ptr, size);\nmm/mm_init.c-1628-\n--\nmm/sparse-vmemmap.c=879=int __meminit sparse_add_section(int nid, unsigned long start_pfn,\n--\nmm/sparse-vmemmap.c-903-\tif (!section_vmemmap_optimizable(ms))\nmm/sparse-vmemmap.c:904:\t\tpage_init_poison(memmap, sizeof(struct page) * nr_pages);\nmm/sparse-vmemmap.c-905-\n"
}

53/3 2026/10/01 14:37 llm kmsan-evaluator
0m Model:
gemini-3.8-flash

Args:
null

Results:
null

Tokens:
							input: 23025
							output: 270
							thoughts: 1216
						

Thoughts:
**KMSAN Assessment: vmemmap Deduplication Refactor**

Okay, let's break this down. First, `page_init_poison` using `memset` indicates `CONFIG_DEBUG_VM` page poisoning, which is a good sign for catching early memory errors.

The patch itself seems to be about vmemmap deduplication across various architectures. I see documentation updates, MAINTAINERS updates, and a bunch of header file modifications to account for the more generic `CONFIG_VMEMMAP_OPTIMIZATION`. The core of the change appears to be centered around the unified `vmemmap_shared_tail_page()` function in `mm/sparse-vmemmap.c`, which handles the creation of shared tail pages for compound pages. Critically, before initializing any of the page's fields, the patch *explicitly* zeroes the `struct page` using `mm_zero_struct_page()`. This is a very important detail. We're talking about allocating a page block, zeroing it, setting compound tail fields, sharing the tail page, and handling DAX.

My first thought: Are there any user-space touches? No, not directly. This is all deeply internal kernel memory management – specifically around vmemmap page table manipulation and `struct page` deduplication.

Now, the crucial question: Is any uninitialized memory being read or copied to user space? Absolutely not. The code explicitly zero-initializes the `struct page` with `mm_zero_struct_page(page)` before it's used. This eliminates any immediate KMSAN concerns regarding info leaks.

Does this patch *introduce* any risk of uninitialized memory usage? Again, no. It's carefully zeroing everything out.

So, what are the *potential* problems? We're talking about page table corruption, incorrect PFN mappings, refcount bugs, bad pointer dereferences, out-of-bounds accesses during section indexing, or memory leaks. All classic kernel memory management issues. But, these are precisely the kinds of things that KASAN, VM_BUG_ON, and other standard kernel debugging tools are designed to catch.

Does KMSAN even have *any* relevance here? Nope. KMSAN isn't designed to deal with page table or struct page metadata tracking in this fashion, and there's no risk of uninitialized memory access here. There is no information leakage potential.

Let's rigorously go through the KMSAN criteria.
1. Kernel structures sent to user space where fields might be uninitialized? No.
2. Conditional logic on uninitialized variables or struct fields? No.
3. Allocation or initialization of complex data structures where uninitialized fields could be read later? No, allocations are zeroed.
4. Bounds checks allowing access to uninitialized bytes of existing buffers? No.

On the other hand:
- The code changes risk out-of-bounds access, array overflows, NULL pointer dereferences, locking deadlocks, or use-after-free bugs. That’s KASAN's domain.
- All structures are fully zeroed/initialized before being read or copied. Check.
- No risk of uninitialized memory usage or information leaks. Check.

Therefore, `NeedsKMSAN=false` is a clear conclusion.

The patch refactors vmemmap deduplication. It introduces `vmemmap_shared_tail_page()` and supporting functions. KMSAN is for catching uninitialized data access and leaks to user space. This patch *never* touches user-kernel boundaries and explicitly zeros all relevant memory using `mm_zero_struct_page()`. The risks here are in page table manipulation, reference counting, etc. – issues already addressed by KASAN and standard kernel debugging tools. Therefore, KASAN is the relevant instrument.



54/3 2026/10/01 14:38 tool set-results
0m Args:
{
  "KMSANReasoning": "The patch series refactors and unifies vmemmap deduplication logic across architectures and subsystems, consolidating HugeTLB Vmemmap Optimization (HVO) and Device-DAX vmemmap optimization under a generic CONFIG_VMEMMAP_OPTIMIZATION framework. It introduces a common vmemmap_shared_tail_page() mechanism for allocating, zeroing (via mm_zero_struct_page), and reusing tail struct pages across sparse memory sections.\n\nKMSAN is specifically designed to detect uninitialized memory reads on the stack/heap/allocations and leaks of uninitialized kernel data to userspace. This patch does not touch any user-space boundary (no copy_to_user, put_user, socket/netlink/ioctl interfaces), nor does it leave any kernel heap or stack structures uninitialized (dynamic allocations are initialized with kzalloc_objs and mm_zero_struct_page).\n\nAny potential bugs in this patch would involve page table manipulations, pfn/section arithmetic, refcounting on struct page, or memory corruption, which are covered by KASAN, DEBUG_VM, and standard kernel debug facilities. A dedicated KMSAN fuzzing session is not warranted.",
  "NeedsKMSAN": false
}

Results:
{
  "KMSANReasoning": "The patch series refactors and unifies vmemmap deduplication logic across architectures and subsystems, consolidating HugeTLB Vmemmap Optimization (HVO) and Device-DAX vmemmap optimization under a generic CONFIG_VMEMMAP_OPTIMIZATION framework. It introduces a common vmemmap_shared_tail_page() mechanism for allocating, zeroing (via mm_zero_struct_page), and reusing tail struct pages across sparse memory sections.\n\nKMSAN is specifically designed to detect uninitialized memory reads on the stack/heap/allocations and leaks of uninitialized kernel data to userspace. This patch does not touch any user-space boundary (no copy_to_user, put_user, socket/netlink/ioctl interfaces), nor does it leave any kernel heap or stack structures uninitialized (dynamic allocations are initialized with kzalloc_objs and mm_zero_struct_page).\n\nAny potential bugs in this patch would involve page table manipulations, pfn/section arithmetic, refcounting on struct page, or memory corruption, which are covered by KASAN, DEBUG_VM, and standard kernel debug facilities. 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)