AI Trajectory


Trajectory: (Expand/Collapse All)
Seq Timestamp Type Name Duration
0/0 2026/09/08 03:11 flow patch-triage
0m Args:
null

Results:
{
  "EnableConfigs": [
    "SPARSEMEM_VMEMMAP",
    "HUGETLB_PAGE",
    "HUGETLB_PAGE_OPTIMIZE_VMEMMAP",
    "DEV_DAX",
    "ZONE_DEVICE"
  ],
  "FocusSymbols": [
    "vmemmap_shared_tail_page",
    "section_nr_vmemmap_pages",
    "vmemmap_populate_compound_pages",
    "hugetlb_vmemmap_optimize_folio",
    "section_activate",
    "sparse_add_section",
    "memmap_init_zone_device",
    "remove_pfn_range_from_zone"
  ],
  "KMSANReasoning": "The patch series refactors and unifies vmemmap tail page optimization/deduplication between HugeTLB and Device DAX across architectures (x86, powerpc) and core MM code. It introduces CONFIG_SPARSEMEM_VMEMMAP_OPTIMIZATION and helper functions in include/linux/vmemmap-optimization.h, sharing tail pages managed via vmemmap_shared_tail_page(). \n\nAll allocated tail pages are explicitly zero-initialized using mm_zero_struct_page() and properly configured before being linked into zone-\u003evmemmap_tails. The patch does not touch user-copy interfaces (copy_to_user/put_user), syscall/ioctl boundaries, or netlink handlers, nor does it introduce branching on uninitialized variables or uninitialized info-leak risks. Any potential memory bugs in page table setup, section activation/deactivation, or page struct lifecycles are addressable by standard debugging facilities and KASAN/DEBUG_VM. Therefore, dedicated KMSAN fuzzing is not required.",
  "NeedsKMSAN": false,
  "Reasoning": "The patch series refactors and unifies vmemmap deduplication/optimization between HugeTLB and Device-DAX under CONFIG_SPARSEMEM_VMEMMAP_OPTIMIZATION. It modifies core memory management (mm/) logic for shared tail page allocation/caching (vmemmap_shared_tail_page), section activation and memmap population (vmemmap_populate_compound_pages, section_activate, sparse_add_section), and zone/page poisoning during hot-unplug. These changes touch reachable core mm code and warrant fuzzing.",
  "WorthFuzzing": true
}

1/1 2026/09/08 03:11 action read-patch-diff
0m Args:
null

Results:
{
  "PatchDiff": "commit d8ae45bfc251d4b01248c95dae7fdb0a92e389d2\nAuthor: syz-cluster \u003ctriage@syzkaller.com\u003e\nDate:   Tue Sep 8 03:11:08 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 c7aaa2e4790ec..2985286cbbbf4 100644\n--- a/MAINTAINERS\n+++ b/MAINTAINERS\n@@ -12096,6 +12096,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/powerpc/mm/book3s64/radix_pgtable.c b/arch/powerpc/mm/book3s64/radix_pgtable.c\nindex cf692b2b5f7bc..e7e751c48dd24 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_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/x86/entry/vdso/vdso32/fake_32bit_build.h b/arch/x86/entry/vdso/vdso32/fake_32bit_build.h\nindex bc3e549795c3f..5f8424eade2bd 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_SPARSEMEM_VMEMMAP_OPTIMIZATION\n #undef CONFIG_NR_CPUS\n #undef CONFIG_PARAVIRT_XXL\n \ndiff --git a/drivers/dax/Kconfig b/drivers/dax/Kconfig\nindex 602f9a0839a91..6250954b0fa7a 100644\n--- a/drivers/dax/Kconfig\n+++ b/drivers/dax/Kconfig\n@@ -8,6 +8,8 @@ if DAX\n config DEV_DAX\n \ttristate \"Device DAX: direct access mapping device\"\n \tdepends on TRANSPARENT_HUGEPAGE\n+\tdepends on ZONE_DEVICE\n+\tselect SPARSEMEM_VMEMMAP_OPTIMIZATION if ARCH_WANT_OPTIMIZE_DAX_VMEMMAP\n \thelp\n \t  Support raw access to differentiated (persistence, bandwidth,\n \t  latency...) memory via an mmap(2) capable character\ndiff --git a/fs/Kconfig b/fs/Kconfig\nindex d1c210c6508f0..9b32ce79cc805 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 SPARSEMEM_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..969594074fd2d 100644\n--- a/include/linux/mm.h\n+++ b/include/linux/mm.h\n@@ -5167,7 +5167,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 +5174,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_SPARSEMEM_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 +5186,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 c9ae7991a8b20..03ed9232f186b 100644\n--- a/include/linux/mmzone.h\n+++ b/include/linux/mmzone.h\n@@ -102,9 +102,9 @@\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+#define MAX_FOLIO_VMEMMAP_ALIGN\t\t\t\t\t\\\n+\t(IS_ENABLED(CONFIG_SPARSEMEM_VMEMMAP_OPTIMIZATION) \u0026\u0026\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@@ -116,7 +116,8 @@\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+\t((__VMEMMAP_OPTIMIZATION_NR_ORDERS \u003e 0 \u0026\u0026\t\\\n+\t  IS_ENABLED(CONFIG_SPARSEMEM_VMEMMAP_OPTIMIZATION)) ? __VMEMMAP_OPTIMIZATION_NR_ORDERS : 0)\n \n enum migratetype {\n \tMIGRATE_UNMOVABLE,\n@@ -1155,7 +1156,7 @@ 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+#ifdef CONFIG_SPARSEMEM_VMEMMAP\n \tstruct page *vmemmap_tails[VMEMMAP_OPTIMIZATION_NR_ORDERS];\n #endif\n } ____cacheline_internodealigned_in_smp;\n@@ -1685,11 +1686,21 @@ static inline bool zone_is_zone_device(const struct zone *zone)\n {\n \treturn zone_idx(zone) == ZONE_DEVICE;\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 bool zone_is_zone_device(const struct zone *zone)\n {\n \treturn false;\n }\n+\n+static inline struct zone *device_zone(int nid)\n+{\n+\treturn NULL;\n+}\n #endif\n \n /*\n@@ -2019,7 +2030,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_SPARSEMEM_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\ndiff --git a/include/linux/page-flags.h b/include/linux/page-flags.h\nindex 86dd0470da117..462e89e055485 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_SPARSEMEM_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..492dc662aae73\n--- /dev/null\n+++ b/include/linux/vmemmap-optimization.h\n@@ -0,0 +1,91 @@\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+#ifdef CONFIG_SPARSEMEM_VMEMMAP_OPTIMIZATION\n+static inline unsigned int section_order(const struct mem_section *section)\n+{\n+\treturn section-\u003eorder;\n+}\n+\n+static inline void section_set_order(struct mem_section *section, unsigned int order)\n+{\n+\tVM_WARN_ON(section_order(section) \u0026\u0026 order \u0026\u0026 section_order(section) != order);\n+\tsection-\u003eorder = order;\n+}\n+\n+static inline void section_set_order_range(unsigned long pfn, unsigned long nr_pages,\n+\t\t\t\t\t   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_order(__nr_to_section(section_nr + i), order);\n+}\n+\n+static inline unsigned int pfn_to_section_order(unsigned long pfn)\n+{\n+\treturn section_order(__pfn_to_section(pfn));\n+}\n+#else\n+static inline unsigned int section_order(const struct mem_section *section)\n+{\n+\treturn 0;\n+}\n+\n+static inline void section_set_order(struct mem_section *section, unsigned int order)\n+{\n+}\n+\n+static inline void section_set_order_range(unsigned long pfn, unsigned long nr_pages,\n+\t\t\t\t\t   unsigned int order)\n+{\n+}\n+\n+static inline unsigned int pfn_to_section_order(unsigned long pfn)\n+{\n+\treturn 0;\n+}\n+#endif /* CONFIG_SPARSEMEM_VMEMMAP_OPTIMIZATION */\n+\n+static inline bool vmemmap_optimizable_pfn(unsigned long pfn)\n+{\n+\tconst unsigned int order = pfn_to_section_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_SPARSEMEM_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+\n+#ifdef CONFIG_SPARSEMEM_VMEMMAP\n+struct page *vmemmap_shared_tail_page(unsigned int order, struct zone *zone);\n+#endif /* CONFIG_SPARSEMEM_VMEMMAP */\n+#endif /* _LINUX_VMEMMAP_OPTIMIZATION_H */\ndiff --git a/mm/Kconfig b/mm/Kconfig\nindex 79163b7d795a3..288b32dcc350f 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 SPARSEMEM_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.\ndiff --git a/mm/hugetlb.c b/mm/hugetlb.c\nindex fda525bccf50e..dff915e6c48b7 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 eb339c4a71f40..25c4e7d2664c3 100644\n--- a/mm/hugetlb_vmemmap.c\n+++ b/mm/hugetlb_vmemmap.c\n@@ -15,10 +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@@ -493,32 +493,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 +509,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 9e8ffd01b4f75..629420a838918 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 e62e6aa07f126..4950ac2a1c012 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_order(ms);\n-\tconst int vmemmap_pages = pgmap ? VMEMMAP_RESERVE_NR : VMEMMAP_OPTIMIZATION_PAGES;\n+\tconst int order = section_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,62 @@ 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+struct page __ref *vmemmap_shared_tail_page(unsigned int order, struct zone *zone)\n {\n-\tstruct page *p, *tail;\n-\tunsigned int idx;\n-\tint node = zone_to_nid(zone);\n+\tvoid *addr;\n+\tstruct page *page;\n+\tconst unsigned int idx = order - VMEMMAP_OPTIMIZATION_MIN_ORDER;\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+\tif (WARN_ON_ONCE(idx \u003e= ARRAY_SIZE(zone-\u003evmemmap_tails)))\n \t\treturn NULL;\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+\tpage = READ_ONCE(zone-\u003evmemmap_tails[idx]);\n+\tif (likely(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-\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+\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-\treturn tail;\n-}\n-#else\n-static inline struct page *vmemmap_get_tail(unsigned int order, struct zone *zone)\n-{\n-\treturn NULL;\n+\tpage = virt_to_page(addr);\n+\tif (cmpxchg(\u0026zone-\u003evmemmap_tails[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(zone-\u003evmemmap_tails[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_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 +240,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@@ -262,7 +255,7 @@ static pte_t * __meminit vmemmap_pte_populate(pmd_t *pmd, unsigned long addr, in\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\tif (flags \u0026 VMEMMAP_POPULATE_PAGEREF)\n+\t\t\tif (flags \u0026 VMEMMAP_POPULATE_DAX)\n \t\t\t\tget_page(pfn_to_page(ptpfn));\n \t\t}\n \t\tentry = pfn_pte(ptpfn, PAGE_KERNEL);\n@@ -497,23 +490,6 @@ 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@@ -522,44 +498,34 @@ static int __meminit vmemmap_populate_compound_pages(unsigned long start_pfn,\n \tunsigned long size, addr;\n \tpte_t *pte;\n \tint rc;\n+\tunsigned long flags = VMEMMAP_POPULATE_DAX;\n+\tstruct page *page;\n+\tunsigned int order = pfn_to_section_order(start_pfn);\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 +547,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 +636,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 +647,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 +657,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 +765,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_order(ms, 0);\n+\t}\n }\n \n static struct page * __meminit section_activate(int nid, unsigned long pfn,\n@@ -811,8 +778,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_order(ms))\n+\t\treturn ERR_PTR(-ENOTSUPP);\n+\n \tif (!ms-\u003eusage) {\n \t\tusage = kzalloc(mem_section_usage_size(), GFP_KERNEL);\n \t\tif (!usage)\n@@ -838,6 +810,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_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 +856,14 @@ 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 */\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 9349ed6326c01..adf057f54c0f0 100644\n--- a/mm/sparse.c\n+++ b/mm/sparse.c\n@@ -251,8 +251,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 049272aba84e5..5d0f407bde7e0 100644\n--- a/mm/sparse.h\n+++ b/mm/sparse.h\n@@ -9,77 +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_order(const struct mem_section *section)\n-{\n-\treturn section-\u003eorder;\n-}\n-\n-static inline void section_set_order(struct mem_section *section, unsigned int order)\n-{\n-\tVM_WARN_ON(section_order(section) \u0026\u0026 order \u0026\u0026 section_order(section) != order);\n-\tsection-\u003eorder = order;\n-}\n-\n-static inline void section_set_order_range(unsigned long pfn, unsigned long nr_pages,\n-\t\t\t\t\t   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_order(__nr_to_section(section_nr + i), order);\n-}\n-\n-static inline unsigned int pfn_to_section_order(unsigned long pfn)\n-{\n-\treturn section_order(__pfn_to_section(pfn));\n-}\n-#else\n-static inline unsigned int section_order(const struct mem_section *section)\n-{\n-\treturn 0;\n-}\n-\n-static inline void section_set_order(struct mem_section *section, unsigned int order)\n-{\n-}\n-\n-static inline void section_set_order_range(unsigned long pfn, unsigned long nr_pages,\n-\t\t\t\t\t   unsigned int order)\n-{\n-}\n-\n-static inline unsigned int pfn_to_section_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_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@@ -140,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/09/08 03:11 agent patch-evaluator
0m Model:
core

Args:
null

Results:
{
  "EnableConfigs": [
    "SPARSEMEM_VMEMMAP",
    "HUGETLB_PAGE",
    "HUGETLB_PAGE_OPTIMIZE_VMEMMAP",
    "DEV_DAX",
    "ZONE_DEVICE"
  ],
  "FocusSymbols": [
    "vmemmap_shared_tail_page",
    "section_nr_vmemmap_pages",
    "vmemmap_populate_compound_pages",
    "hugetlb_vmemmap_optimize_folio",
    "section_activate",
    "sparse_add_section",
    "memmap_init_zone_device",
    "remove_pfn_range_from_zone"
  ],
  "Reasoning": "The patch series refactors and unifies vmemmap deduplication/optimization between HugeTLB and Device-DAX under CONFIG_SPARSEMEM_VMEMMAP_OPTIMIZATION. It modifies core memory management (mm/) logic for shared tail page allocation/caching (vmemmap_shared_tail_page), section activation and memmap population (vmemmap_populate_compound_pages, section_activate, sparse_add_section), and zone/page poisoning during hot-unplug. These changes touch reachable core mm code and warrant fuzzing.",
  "WorthFuzzing": true
}

Instruction:
You are an expert Linux kernel maintainer and security engineer.
Your job is to review a provided patch series and evaluate whether it warrants fuzzing with syzkaller.

IMPORTANT: The changes have ALREADY been applied and committed as the HEAD commit in
your workspace. Do NOT rely on internal assumptions. You must actively use your code access
tools to inspect the actual source code, callers, and surrounding context.

================================================================================
1. CORE TRIAGE PHILOSOPHY
================================================================================
The goal of patch fuzzing is to discover crashes, regressions, exposed latent bugs,
and newly triggered assertions introduced by the patch series.

- REACHABILITY IS THE PRIMARY GATE:
  Fuzzing can only discover bugs in code that can actually execute in standard virtualized
  environments (GCE or QEMU, utilizing software-emulated devices like USB gadgets, netdev, tun/tap).
  If the modified code is structurally unreachable (see Section 2), it MUST NOT be fuzzed,
  regardless of whether it adds assertions or complex logic.

- DO NOT BLINDLY TRUST "NO FUNCTIONAL CHANGE" (NFCI) OR "REFACTORING" CLAIMS:
  Patch authors routinely label changes as "cleanups", "refactorings", or state
  "No functional change intended". Do NOT take these claims at face value.
  Code refactorings that rearrange logic, introduce helper functions, or alter state management
  in core subsystems frequently introduce subtle semantic shifts or uncover latent kernel bugs.
  If reachable executable code is modified or refactored, it MUST be fuzzed.

- NEW OR MODIFIED ASSERTIONS IN REACHABLE CODE MUST BE FUZZED:
  When a patch introduces or modifies runtime checks or assertions (e.g., WARN_ON*, VM_WARN_ON*,
  BUG_ON*, lockdep_assert*) in reachable code paths, it enforces new or stricter invariants.
  Even if the author believes the invariant always holds, fuzzing is essential to verify whether
  an unusual sequence of operations can violate it.

================================================================================
2. WHEN TO RETURN WorthFuzzing=false (NEGATIVE CRITERIA)
================================================================================
Return WorthFuzzing=false ONLY IF all modified code falls strictly into one or more of these categories:

- Non-kernel and non-executable changes:
  * Modifications to Documentation/, comments, or spelling fixes.
  * User-space directories, self-tests, samples, or scripts (e.g., tools/, samples/, scripts/, usr/)
    that do not affect the compiled kernel image (vmlinux) or kernel modules.
  * Purely decorative logging (e.g., message strings in pr_err, printk, dev_info) or tracepoints
    that do not alter control flow or data structures.
  * Build system or Kconfig changes that do not alter compiled C logic.
- Structurally unreachable hardware:
  * Vendor-specific PCIe switches, SmartNICs, or GPU drivers (e.g., mlxsw, pds_core, qed,
    ionic, amdgpu) requiring physical ASIC/PCIe cards not emulated in standard QEMU.
- Unreachable execution paths:
  * Driver teardown callbacks (.remove, .shutdown, pci_unregister_driver) executed only during
    physical PCI hot-unplug or manual sysfs driver unbinding.
  * Code paths exclusive to architectures other than the target architecture.

================================================================================
3. WHEN TO RETURN WorthFuzzing=true (POSITIVE CRITERIA)
================================================================================
Return WorthFuzzing=true whenever the patch touches reachable executable code, including:
- Core Subsystems:
  * Any logic modifications in memory management (mm/), synchronization/locking (kernel/locking/),
    BPF, scheduler, core networking, VFS, or syscall handling.
- Refactorings and Code Cleanups:
  * Any restructuring of reachable data structures, helper abstractions, or algorithm flows.
- Runtime Assertions and Defensive Checks:
  * Any introduction or alteration of assertions (WARN_ON*, VM_WARN_ON*, BUG_ON*, etc.) in reachable paths.
- Reachable Drivers and Protocols:
  * Drivers accessible via virtual buses (virtio, USB gadget, loopback, netlink, binder, sockets, etc.).

================================================================================
4. EXTRACTING FocusSymbols (PREVENTING DILUTION)
================================================================================
When WorthFuzzing=true, you must extract specific kernel functions into FocusSymbols to guide the fuzzer:

- AVOID UBIQUITOUS LIFECYCLE HOT-PATHS:
  Do NOT list generic, ubiquitous functions called by almost every program in the corpus
  (including, but not limited to: general memory allocators and deallocators, page fault
  and trap handlers, or core synchronization primitives; this is not an exhaustive list).
  Listing ubiquitous functions causes the fuzzer to classify thousands of unrelated tests as "focused",
  which severely dilutes fuzzing effort away from the actual changes.

- TARGET SPECIFIC FEATURE LOGIC AND ENTRYPOINTS:
  List functions that specifically implement the logic being added or altered, or direct API entrypoints
  for the subsystem feature under review.

- HANDLING STATIC INLINE FUNCTIONS IN HEADERS (.h):
  Compiler-inlined static functions (such as static inlines in mm/*.h or include/linux/*.h) lack
  distinct symbol addresses in vmlinux and cannot be targeted directly by symbol coverage filters.
  If the changes are primarily in static inline helpers, identify non-static, feature-specific caller
  functions in .c files that exercise them (avoiding ubiquitous lifecycle wrappers).

================================================================================
5. IDENTIFYING EnableConfigs
================================================================================
Identify any specific CONFIG_ options required to properly compile and reach the modified code:
- Inspect Kconfig files and #ifdef guards; do not make assumptions.
- Check "depends on" lines in Kconfig to include any non-standard parent subsystem configs needed.
- Strip any 'CONFIG_' prefix (e.g., return "NET_IPV4" instead of "CONFIG_NET_IPV4").
Prefer calling several tools at the same time to save round-trips.


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

Prompt:
Target architecture: amd64

For your convenience, here is the diff of the changes:
commit d8ae45bfc251d4b01248c95dae7fdb0a92e389d2
Author: syz-cluster <triage@syzkaller.com>
Date:   Tue Sep 8 03:11:08 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 c7aaa2e4790ec..2985286cbbbf4 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -12096,6 +12096,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/powerpc/mm/book3s64/radix_pgtable.c b/arch/powerpc/mm/book3s64/radix_pgtable.c
index cf692b2b5f7bc..e7e751c48dd24 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_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/x86/entry/vdso/vdso32/fake_32bit_build.h b/arch/x86/entry/vdso/vdso32/fake_32bit_build.h
index bc3e549795c3f..5f8424eade2bd 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_SPARSEMEM_VMEMMAP_OPTIMIZATION
 #undef CONFIG_NR_CPUS
 #undef CONFIG_PARAVIRT_XXL
 
diff --git a/drivers/dax/Kconfig b/drivers/dax/Kconfig
index 602f9a0839a91..6250954b0fa7a 100644
--- a/drivers/dax/Kconfig
+++ b/drivers/dax/Kconfig
@@ -8,6 +8,8 @@ if DAX
 config DEV_DAX
 	tristate "Device DAX: direct access mapping device"
 	depends on TRANSPARENT_HUGEPAGE
+	depends on ZONE_DEVICE
+	select SPARSEMEM_VMEMMAP_OPTIMIZATION if ARCH_WANT_OPTIMIZE_DAX_VMEMMAP
 	help
 	  Support raw access to differentiated (persistence, bandwidth,
 	  latency...) memory via an mmap(2) capable character
diff --git a/fs/Kconfig b/fs/Kconfig
index d1c210c6508f0..9b32ce79cc805 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 SPARSEMEM_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..969594074fd2d 100644
--- a/include/linux/mm.h
+++ b/include/linux/mm.h
@@ -5167,7 +5167,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 +5174,9 @@ static inline bool __vmemmap_can_optimize(struct vmem_altmap *altmap,
 	unsigned long nr_pages;
 	unsigned long nr_vmemmap_pages;
 
+	if (!IS_ENABLED(CONFIG_SPARSEMEM_VMEMMAP_OPTIMIZATION))
+		return false;
+
 	if (!pgmap || !is_power_of_2(sizeof(struct page)))
 		return false;
 
@@ -5184,7 +5186,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 c9ae7991a8b20..03ed9232f186b 100644
--- a/include/linux/mmzone.h
+++ b/include/linux/mmzone.h
@@ -102,9 +102,9 @@
  *
  * 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)) ? \
+#define MAX_FOLIO_VMEMMAP_ALIGN					\
+	(IS_ENABLED(CONFIG_SPARSEMEM_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. */
@@ -116,7 +116,8 @@
 #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)
+	((__VMEMMAP_OPTIMIZATION_NR_ORDERS > 0 &&	\
+	  IS_ENABLED(CONFIG_SPARSEMEM_VMEMMAP_OPTIMIZATION)) ? __VMEMMAP_OPTIMIZATION_NR_ORDERS : 0)
 
 enum migratetype {
 	MIGRATE_UNMOVABLE,
@@ -1155,7 +1156,7 @@ 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
+#ifdef CONFIG_SPARSEMEM_VMEMMAP
 	struct page *vmemmap_tails[VMEMMAP_OPTIMIZATION_NR_ORDERS];
 #endif
 } ____cacheline_internodealigned_in_smp;
@@ -1685,11 +1686,21 @@ static inline bool zone_is_zone_device(const struct zone *zone)
 {
 	return zone_idx(zone) == ZONE_DEVICE;
 }
+
+static inline struct zone *device_zone(int nid)
+{
+	return &NODE_DATA(nid)->node_zones[ZONE_DEVICE];
+}
 #else
 static inline bool zone_is_zone_device(const struct zone *zone)
 {
 	return false;
 }
+
+static inline struct zone *device_zone(int nid)
+{
+	return NULL;
+}
 #endif
 
 /*
@@ -2019,7 +2030,7 @@ struct mem_section {
 	unsigned long section_mem_map;
 
 	struct mem_section_usage *usage;
-#ifdef CONFIG_HUGETLB_PAGE_OPTIMIZE_VMEMMAP
+#ifdef CONFIG_SPARSEMEM_VMEMMAP_OPTIMIZATION
 	/*
 	 * Normally, sections hold regular (order-0) pages. However, for
 	 * sections with HVO enabled, this tracks the compound page order
diff --git a/include/linux/page-flags.h b/include/linux/page-flags.h
index 86dd0470da117..462e89e055485 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_SPARSEMEM_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..492dc662aae73
--- /dev/null
+++ b/include/linux/vmemmap-optimization.h
@@ -0,0 +1,91 @@
+/* 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>
+
+#ifdef CONFIG_SPARSEMEM_VMEMMAP_OPTIMIZATION
+static inline unsigned int section_order(const struct mem_section *section)
+{
+	return section->order;
+}
+
+static inline void section_set_order(struct mem_section *section, unsigned int order)
+{
+	VM_WARN_ON(section_order(section) && order && section_order(section) != order);
+	section->order = order;
+}
+
+static inline void section_set_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_order(__nr_to_section(section_nr + i), order);
+}
+
+static inline unsigned int pfn_to_section_order(unsigned long pfn)
+{
+	return section_order(__pfn_to_section(pfn));
+}
+#else
+static inline unsigned int section_order(const struct mem_section *section)
+{
+	return 0;
+}
+
+static inline void section_set_order(struct mem_section *section, unsigned int order)
+{
+}
+
+static inline void section_set_order_range(unsigned long pfn, unsigned long nr_pages,
+					   unsigned int order)
+{
+}
+
+static inline unsigned int pfn_to_section_order(unsigned long pfn)
+{
+	return 0;
+}
+#endif /* CONFIG_SPARSEMEM_VMEMMAP_OPTIMIZATION */
+
+static inline bool vmemmap_optimizable_pfn(unsigned long pfn)
+{
+	const unsigned int order = pfn_to_section_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_SPARSEMEM_VMEMMAP_OPTIMIZATION))
+		return false;
+
+	if (!is_power_of_2(sizeof(struct page)))
+		return false;
+
+	return order >= VMEMMAP_OPTIMIZATION_MIN_ORDER;
+}
+
+#ifdef CONFIG_SPARSEMEM_VMEMMAP
+struct page *vmemmap_shared_tail_page(unsigned int order, struct zone *zone);
+#endif /* CONFIG_SPARSEMEM_VMEMMAP */
+#endif /* _LINUX_VMEMMAP_OPTIMIZATION_H */
diff --git a/mm/Kconfig b/mm/Kconfig
index 79163b7d795a3..288b32dcc350f 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 SPARSEMEM_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.
diff --git a/mm/hugetlb.c b/mm/hugetlb.c
index fda525bccf50e..dff915e6c48b7 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 eb339c4a71f40..25c4e7d2664c3 100644
--- a/mm/hugetlb_vmemmap.c
+++ b/mm/hugetlb_vmemmap.c
@@ -15,10 +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"
 
 /**
@@ -493,32 +493,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 +509,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 9e8ffd01b4f75..629420a838918 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 e62e6aa07f126..4950ac2a1c012 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_order(ms);
-	const int vmemmap_pages = pgmap ? VMEMMAP_RESERVE_NR : VMEMMAP_OPTIMIZATION_PAGES;
+	const int order = section_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,62 @@ 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)
+struct page __ref *vmemmap_shared_tail_page(unsigned int order, struct zone *zone)
 {
-	struct page *p, *tail;
-	unsigned int idx;
-	int node = zone_to_nid(zone);
+	void *addr;
+	struct page *page;
+	const unsigned int idx = order - VMEMMAP_OPTIMIZATION_MIN_ORDER;
 
-	if (WARN_ON_ONCE(order < VMEMMAP_OPTIMIZATION_MIN_ORDER))
-		return NULL;
-	if (WARN_ON_ONCE(order > MAX_FOLIO_ORDER))
+	if (WARN_ON_ONCE(idx >= ARRAY_SIZE(zone->vmemmap_tails)))
 		return NULL;
 
-	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)
+	page = READ_ONCE(zone->vmemmap_tails[idx]);
+	if (likely(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++)
-		init_compound_tail(p + i, NULL, order, zone);
 
-	tail = virt_to_page(p);
-	zone->vmemmap_tails[idx] = tail;
+	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);
+	}
 
-	return tail;
-}
-#else
-static inline struct page *vmemmap_get_tail(unsigned int order, struct zone *zone)
-{
-	return NULL;
+	page = virt_to_page(addr);
+	if (cmpxchg(&zone->vmemmap_tails[idx], NULL, page) != NULL) {
+		if (slab_is_available())
+			__free_page(page);
+		else
+			memblock_free(addr, PAGE_SIZE);
+		page = READ_ONCE(zone->vmemmap_tails[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_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 +240,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;
@@ -262,7 +255,7 @@ static pte_t * __meminit vmemmap_pte_populate(pmd_t *pmd, unsigned long addr, in
 			 * and through vmemmap_populate_compound_pages() when
 			 * slab is available.
 			 */
-			if (flags & VMEMMAP_POPULATE_PAGEREF)
+			if (flags & VMEMMAP_POPULATE_DAX)
 				get_page(pfn_to_page(ptpfn));
 		}
 		entry = pfn_pte(ptpfn, PAGE_KERNEL);
@@ -497,23 +490,6 @@ 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,
@@ -522,44 +498,34 @@ static int __meminit vmemmap_populate_compound_pages(unsigned long start_pfn,
 	unsigned long size, addr;
 	pte_t *pte;
 	int rc;
+	unsigned long flags = VMEMMAP_POPULATE_DAX;
+	struct page *page;
+	unsigned int order = pfn_to_section_order(start_pfn);
 
-	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 +547,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 +636,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 +647,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 +657,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 +765,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_order(ms, 0);
+	}
 }
 
 static struct page * __meminit section_activate(int nid, unsigned long pfn,
@@ -811,8 +778,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_order(ms))
+		return ERR_PTR(-ENOTSUPP);
+
 	if (!ms->usage) {
 		usage = kzalloc(mem_section_usage_size(), GFP_KERNEL);
 		if (!usage)
@@ -838,6 +810,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_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 +856,14 @@ 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.
 	 */
-	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 9349ed6326c01..adf057f54c0f0 100644
--- a/mm/sparse.c
+++ b/mm/sparse.c
@@ -251,8 +251,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 049272aba84e5..5d0f407bde7e0 100644
--- a/mm/sparse.h
+++ b/mm/sparse.h
@@ -9,77 +9,7 @@
 #define __MM_SPARSE_H
 
 #include <linux/mmzone.h>
-
-#ifdef CONFIG_HUGETLB_PAGE_OPTIMIZE_VMEMMAP
-static inline unsigned int section_order(const struct mem_section *section)
-{
-	return section->order;
-}
-
-static inline void section_set_order(struct mem_section *section, unsigned int order)
-{
-	VM_WARN_ON(section_order(section) && order && section_order(section) != order);
-	section->order = order;
-}
-
-static inline void section_set_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_order(__nr_to_section(section_nr + i), order);
-}
-
-static inline unsigned int pfn_to_section_order(unsigned long pfn)
-{
-	return section_order(__pfn_to_section(pfn));
-}
-#else
-static inline unsigned int section_order(const struct mem_section *section)
-{
-	return 0;
-}
-
-static inline void section_set_order(struct mem_section *section, unsigned int order)
-{
-}
-
-static inline void section_set_order_range(unsigned long pfn, unsigned long nr_pages,
-					   unsigned int order)
-{
-}
-
-static inline unsigned int pfn_to_section_order(unsigned long pfn)
-{
-	return 0;
-}
-#endif
-
-static inline bool vmemmap_optimizable_pfn(unsigned long pfn)
-{
-	const unsigned int order = pfn_to_section_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
@@ -140,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/09/08 03:11 llm patch-evaluator
0m Model:
gemini-3.7-flash

Args:
null

Results:
null

Tokens:
							input: 19044
							output: 27
							thoughts: 948
						

Thoughts:
**Analyzing a Patch Series for Fuzzing Potential**

Okay, let's break this down. First, the target is `amd64`, so that narrows the focus somewhat. I'm seeing changes across a range of files, from documentation and MAINTAINERS to architecture-specific code for PowerPC, but also key areas like `mm/`, `include/linux/`, and `drivers/dax/`. The big picture is that this patch series is about unifying and refactoring HugeTLB Vmemmap Optimization (HVO) and DAX vmemmap optimization under a common configuration: `CONFIG_SPARSEMEM_VMEMMAP_OPTIMIZATION`.

I'm particularly interested in the core memory management changes. We're looking at modifications to `mm/sparse-vmemmap.c`, including key functions like `vmemmap_alloc_block`, `vmemmap_shared_tail_page` (which is now exported), and functions that deal with PTE population and section management. The involvement of `mm/hugetlb_vmemmap.c`, `mm/mm_init.c`, and `mm/memory_hotplug.c` all signals significant alterations to how memory is managed. The new `include/linux/vmemmap-optimization.h` is also important, as it introduces new definitions and helper functions related to section order and optimization.

Given my experience, this immediately jumps out as worth investigating from a fuzzing perspective. This touches the memory management core (`mm/`), which is always interesting. The refactoring aspect, where we have restructuring of reachable data structures and algorithms, *especially* if it deals with `hugetlb` and `dax`, ticks the "WorthFuzzing=true" boxes according to the Core Triaging Philosophy. Any changes to memory management code are prime targets, and this clearly has touched various memory management subcomponents.

Now, the crucial question is reachability on amd64 in our testing environment (QEMU/syzkaller). Fortunately, HugeTLB vmemmap optimization is standard on x86_64 when `SPARSEMEM_VMEMMAP` and `HUGETLB_PAGE_OPTIMIZE_VMEMMAP` are enabled, and device DAX, memory hotplug, and allocations/deallocations of hugetlb pages exercise HVO in normal syzkaller fuzzing.

I'll need to do some more grepping to confirm the configurations and dependencies. But my initial impression is this is a strong candidate for fuzzing.



4/2 2026/09/08 03:11 tool grepper
0m Args:
{
  "Expression": "CONFIG_SPARSEMEM_VMEMMAP_OPTIMIZATION"
}

Results:
{
  "Output": "arch/x86/entry/vdso/vdso32/fake_32bit_build.h-13-#undef CONFIG_SPARSEMEM_VMEMMAP\narch/x86/entry/vdso/vdso32/fake_32bit_build.h:14:#undef CONFIG_SPARSEMEM_VMEMMAP_OPTIMIZATION\narch/x86/entry/vdso/vdso32/fake_32bit_build.h-15-#undef CONFIG_NR_CPUS\n--\ninclude/linux/mm.h=5171=static inline bool __vmemmap_can_optimize(struct vmem_altmap *altmap,\n--\ninclude/linux/mm.h-5176-\ninclude/linux/mm.h:5177:\tif (!IS_ENABLED(CONFIG_SPARSEMEM_VMEMMAP_OPTIMIZATION))\ninclude/linux/mm.h-5178-\t\treturn false;\n--\ninclude/linux/mmzone.h-105-#define MAX_FOLIO_VMEMMAP_ALIGN\t\t\t\t\t\\\ninclude/linux/mmzone.h:106:\t(IS_ENABLED(CONFIG_SPARSEMEM_VMEMMAP_OPTIMIZATION) \u0026\u0026\t\\\ninclude/linux/mmzone.h-107-\t is_power_of_2(sizeof(struct page)) ?\t\t\t\\\n--\ninclude/linux/mmzone.h-119-\t((__VMEMMAP_OPTIMIZATION_NR_ORDERS \u003e 0 \u0026\u0026\t\\\ninclude/linux/mmzone.h:120:\t  IS_ENABLED(CONFIG_SPARSEMEM_VMEMMAP_OPTIMIZATION)) ? __VMEMMAP_OPTIMIZATION_NR_ORDERS : 0)\ninclude/linux/mmzone.h-121-\n--\ninclude/linux/mmzone.h=2017=struct mem_section {\n--\ninclude/linux/mmzone.h-2032-\tstruct mem_section_usage *usage;\ninclude/linux/mmzone.h:2033:#ifdef CONFIG_SPARSEMEM_VMEMMAP_OPTIMIZATION\ninclude/linux/mmzone.h-2034-\t/*\n--\ninclude/linux/page-flags.h=208=static __always_inline bool compound_info_has_mask(void)\n--\ninclude/linux/page-flags.h-216-\t */\ninclude/linux/page-flags.h:217:\tif (!IS_ENABLED(CONFIG_SPARSEMEM_VMEMMAP_OPTIMIZATION))\ninclude/linux/page-flags.h-218-\t\treturn false;\n--\ninclude/linux/vmemmap-optimization.h-16-\ninclude/linux/vmemmap-optimization.h:17:#ifdef CONFIG_SPARSEMEM_VMEMMAP_OPTIMIZATION\ninclude/linux/vmemmap-optimization.h-18-static inline unsigned int section_order(const struct mem_section *section)\n--\ninclude/linux/vmemmap-optimization.h=60=static inline unsigned int pfn_to_section_order(unsigned long pfn)\n--\ninclude/linux/vmemmap-optimization.h-63-}\ninclude/linux/vmemmap-optimization.h:64:#endif /* CONFIG_SPARSEMEM_VMEMMAP_OPTIMIZATION */\ninclude/linux/vmemmap-optimization.h-65-\n--\ninclude/linux/vmemmap-optimization.h=77=static inline bool vmemmap_optimizable_order(unsigned int order)\ninclude/linux/vmemmap-optimization.h-78-{\ninclude/linux/vmemmap-optimization.h:79:\tif (!IS_ENABLED(CONFIG_SPARSEMEM_VMEMMAP_OPTIMIZATION))\ninclude/linux/vmemmap-optimization.h-80-\t\treturn false;\n"
}

5/2 2026/09/08 03:11 llm patch-evaluator
0m Model:
gemini-3.7-flash

Args:
null

Results:
null

Tokens:
							input: 21481
							output: 21
							thoughts: 70
						

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

**Optimizing vmemmap: A Kconfig Deep Dive**

Okay, time to get my hands dirty with some Kconfig exploration. I need to understand how the vmemmap is being handled, especially with all the new optimization flags floating around. My gut feeling is that there are some interdependencies here that I need to clarify. I'll start by digging into the Kconfig definitions. I need to confirm the impact of `SPARSEMEM_VMEMMAP_OPTIMIZATION`, see how it interacts with the hugetlb optimizations: `HUGETLB_PAGE_OPTIMIZE_VMEMMAP`, and the architecture-specific flags such as `ARCH_WANT_OPTIMIZE_HUGETLB_VMEMMAP` and `ARCH_WANT_OPTIMIZE_DAX_VMEMMAP`. Are they mutually exclusive, dependent, or orthogonal? What are the implications of each of these flags being set or unset? I suspect the interplay of these options will give a good insight into the overall memory management strategy and hopefully expose any inefficiencies I can address. I need to understand the control flow, the configurations and the relationships between these features. It's time to follow the trail of these Kconfig options.



6/2 2026/09/08 03:11 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--\ndrivers/dax/Kconfig=8=config DEV_DAX\n--\ndrivers/dax/Kconfig-11-\tdepends on ZONE_DEVICE\ndrivers/dax/Kconfig:12:\tselect SPARSEMEM_VMEMMAP_OPTIMIZATION if ARCH_WANT_OPTIMIZE_DAX_VMEMMAP\ndrivers/dax/Kconfig-13-\thelp\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=5164=static inline void vmem_altmap_free(struct vmem_altmap *altmap,\n--\ninclude/linux/mm.h-5169-\ninclude/linux/mm.h:5170:#ifdef CONFIG_ARCH_WANT_OPTIMIZE_DAX_VMEMMAP\ninclude/linux/mm.h-5171-static inline bool __vmemmap_can_optimize(struct vmem_altmap *altmap,\n--\nmm/Kconfig=464=config SPARSEMEM_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"
}

7/2 2026/09/08 03:11 llm patch-evaluator
0m Model:
gemini-3.7-flash

Args:
null

Results:
null

Tokens:
							input: 23118
							output: 32
							thoughts: 15
						

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

**Diving into the Kernel Config**

Okay, so I need to understand something within the file system configuration space. My gut feeling tells me the answer lies in `fs/Kconfig`. It's usually the go-to spot for file system-related settings. I'm going to jump straight to around line 277. That seems like a reasonable starting point, maybe it's a section I recognize or a specific feature I have experience with. Let's see what's there. Hopefully, that'll give me the context I need to get started. I'm going to look for the relevant symbols, dependencies, and any hints about how this particular file system feature is configured.



8/2 2026/09/08 03:11 tool read-file
0m Args:
{
  "File": "fs/Kconfig",
  "FirstLine": 275,
  "LineCount": 20
}

Results:
{
  "Contents": " 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 SPARSEMEM_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"
}

9/2 2026/09/08 03:11 llm patch-evaluator
0m Model:
gemini-3.7-flash

Args:
null

Results:
null

Tokens:
							input: 23625
							output: 31
							thoughts: 592
						

Thoughts:
**Analyzing vmemmap Optimization and Related Code Changes**

Okay, let's dive into this. I need to understand the implications of these changes, specifically regarding `HUGETLB_PAGE_OPTIMIZE_VMEMMAP`, `SPARSEMEM_VMEMMAP_OPTIMIZATION`, and `DEV_DAX`. I know how these relate, but it's always good to refresh. Right, if `HUGETLB_PAGE` is enabled on x86_64, which is the standard setup for `SPARSEMEM_VMEMMAP`, then `HUGETLB_PAGE_OPTIMIZE_VMEMMAP` will select `SPARSEMEM_VMEMMAP_OPTIMIZATION`. And `DEV_DAX` also triggers `SPARSEMEM_VMEMMAP_OPTIMIZATION` if `ARCH_WANT_OPTIMIZE_DAX_VMEMMAP` is set, which is probably the intended behaviour.

Now, let's get into the nitty-gritty of the code modifications. I see a list of symbols that have been altered or newly introduced. Specifically:

1.  `vmemmap_shared_tail_page` in `mm/sparse-vmemmap.c`. Hmm, I need to check if this is static or exported. Oh, it's declared in `include/linux/vmemmap-optimization.h` as a `struct page *` and implemented in `mm/sparse-vmemmap.c` as a `struct page __ref *`. Interesting.

2.  `section_nr_vmemmap_pages` in `mm/sparse-vmemmap.c` - a signature change, removing two arguments. Okay, that's a direct signal of functionality being changed.
3.  `vmemmap_alloc_block` in `mm/sparse-vmemmap.c` - Probably involved in allocating the vmemmap.
4.  `vmemmap_alloc_pte` in `mm/sparse-vmemmap.c` - Allocating page table entries, seems expected.
5.  `vmemmap_pte_populate` in `mm/sparse-vmemmap.c` - Populating the PTEs, got it.
6.  `vmemmap_populate_compound_pages` in `mm/sparse-vmemmap.c` - Sounds like optimization for compound pages.
7.  `__hugetlb_vmemmap_optimize_folio` in `mm/hugetlb_vmemmap.c` - This is significant, clearly tied to huge pages and vmemmap optimization.
8.  `section_activate` in `mm/sparse-vmemmap.c` - Part of sparse memory section management.
9.  `section_deactivate` in `mm/sparse-vmemmap.c` - The reverse operation.
10. `memmap_init_zone_device` in `mm/mm_init.c` - This is how the memmap is initialized for a specific zone device.
11. `remove_pfn_range_from_zone` in `mm/memory_hotplug.c` - Memory hotplug is involved.

Alright, that gives me a good overview of the symbols. Now, I need to make sure I'm not missing any other relevant code in `sparse-vmemmap.c` and elsewhere. I'll need to inspect `mm/sparse-vmemmap.c` very closely, particularly around the modified sections to understand the full scope of these changes and how the optimization is implemented.



10/2 2026/09/08 03:11 tool grepper
0m Args:
{
  "Expression": "vmemmap_shared_tail_page",
  "PathPrefix": "mm/"
}

Results:
{
  "Output": "mm/hugetlb_vmemmap.c=496=static int __hugetlb_vmemmap_optimize_folio(const struct hstate *h,\n--\nmm/hugetlb_vmemmap.c-511-\tnid = folio_nid(folio);\nmm/hugetlb_vmemmap.c:512:\tvmemmap_tail = vmemmap_shared_tail_page(h-\u003eorder, folio_zone(folio));\nmm/hugetlb_vmemmap.c-513-\tif (!vmemmap_tail)\n--\nmm/sparse-vmemmap.c=159=static void * __meminit vmemmap_alloc_block_zero(unsigned long size, int node)\n--\nmm/sparse-vmemmap.c-169-\nmm/sparse-vmemmap.c:170:struct page __ref *vmemmap_shared_tail_page(unsigned int order, struct zone *zone)\nmm/sparse-vmemmap.c-171-{\n--\nmm/sparse-vmemmap.c=210=static __meminit void *vmemmap_alloc_pte(unsigned long pfn, int node,\n--\nmm/sparse-vmemmap.c-224-\tzone = pfn_to_zone(pfn, node);\nmm/sparse-vmemmap.c:225:\tpage = vmemmap_shared_tail_page(order, zone);\nmm/sparse-vmemmap.c-226-\tif (!page)\n--\nmm/sparse-vmemmap.c=493=static int __meminit vmemmap_populate_compound_pages(unsigned long start_pfn,\n--\nmm/sparse-vmemmap.c-504-\nmm/sparse-vmemmap.c:505:\tpage = vmemmap_shared_tail_page(order, device_zone(node));\nmm/sparse-vmemmap.c-506-\tif (!page)\n"
}

11/2 2026/09/08 03:11 llm patch-evaluator
0m Model:
gemini-3.7-flash

Args:
null

Results:
null

Tokens:
							input: 25516
							output: 37
							thoughts: 39
						

Thoughts:
Okay, let's dive into this. Here's my thought process on dissecting `mm/sparse-vmemmap.c` around `vmemmap_shared_tail_page` and its associated calls, from my perspective as an expert in this domain:

**Investigating `vmemmap_shared_tail_page` and its Interactions**

Right, I need to get a clear picture of how this shared tail page mechanism in the sparse vmemmap is implemented. The first thing I'll do is zero in on `mm/sparse-vmemmap.c`. I need to understand what this `vmemmap_shared_tail_page` actually does and, critically, how it's used. I'll be looking for the fundamental principles of its operation. I'm especially interested in how it interacts with the rest of the memory management system, and what specific scenarios are it's employed. Are we talking about handling large pages, numa, or something else entirely? To facilitate this, I'll need to identify the key callers of this function. I'm going to start by grepping the source for the symbol to see if I can find the code and understand how the pieces come together to realize the design. I will keep an eye on memory usage, page-table configuration, and the overall performance implications of this design.



12/2 2026/09/08 03:11 tool read-file
0m Args:
{
  "File": "mm/sparse-vmemmap.c",
  "FirstLine": 165,
  "LineCount": 55
}

Results:
{
  "Contents": " 165:\t\tmemset(p, 0, size);\n 166:\t\n 167:\t\treturn p;\n 168:\t}\n 169:\t\n 170:\tstruct page __ref *vmemmap_shared_tail_page(unsigned int order, struct zone *zone)\n 171:\t{\n 172:\t\tvoid *addr;\n 173:\t\tstruct page *page;\n 174:\t\tconst unsigned int idx = order - VMEMMAP_OPTIMIZATION_MIN_ORDER;\n 175:\t\n 176:\t\tif (WARN_ON_ONCE(idx \u003e= ARRAY_SIZE(zone-\u003evmemmap_tails)))\n 177:\t\t\treturn NULL;\n 178:\t\n 179:\t\tpage = READ_ONCE(zone-\u003evmemmap_tails[idx]);\n 180:\t\tif (likely(page))\n 181:\t\t\treturn page;\n 182:\t\n 183:\t\taddr = vmemmap_alloc_block(PAGE_SIZE, zone_to_nid(zone));\n 184:\t\tif (!addr)\n 185:\t\t\treturn NULL;\n 186:\t\n 187:\t\tfor (int i = 0; i \u003c PAGE_SIZE / sizeof(struct page); i++) {\n 188:\t\t\tpage = (struct page *)addr + i;\n 189:\t\t\tmm_zero_struct_page(page);\n 190:\t\t\tatomic_set(\u0026page-\u003e_mapcount, -1);\n 191:\t\t\tset_page_node(page, zone_to_nid(zone));\n 192:\t\t\tset_page_zone(page, zone_idx(zone));\n 193:\t\t\tprep_compound_tail(page, NULL, order);\n 194:\t\t\tif (zone_is_zone_device(zone))\n 195:\t\t\t\t__SetPageReserved(page);\n 196:\t\t}\n 197:\t\n 198:\t\tpage = virt_to_page(addr);\n 199:\t\tif (cmpxchg(\u0026zone-\u003evmemmap_tails[idx], NULL, page) != NULL) {\n 200:\t\t\tif (slab_is_available())\n 201:\t\t\t\t__free_page(page);\n 202:\t\t\telse\n 203:\t\t\t\tmemblock_free(addr, PAGE_SIZE);\n 204:\t\t\tpage = READ_ONCE(zone-\u003evmemmap_tails[idx]);\n 205:\t\t}\n 206:\t\n 207:\t\treturn page;\n 208:\t}\n 209:\t\n 210:\tstatic __meminit void *vmemmap_alloc_pte(unsigned long pfn, int node,\n 211:\t\t\tstruct vmem_altmap *altmap, unsigned long flags)\n 212:\t{\n 213:\t\tstruct zone *zone;\n 214:\t\tstruct page *page;\n 215:\t\tconst unsigned int order = pfn_to_section_order(pfn);\n 216:\t\n 217:\t\t/*\n 218:\t\t * Device DAX still relies on vmemmap_populate_compound_pages() for\n 219:\t\t * head/first-tail allocation and tail-page reuse.\n"
}

13/2 2026/09/08 03:11 llm patch-evaluator
0m Model:
gemini-3.7-flash

Args:
null

Results:
null

Tokens:
							input: 26715
							output: 31
							thoughts: 95
						

Thoughts:
Here's my thought process, as I delve into this code:

**Analyzing Huge Page Optimization in Virtual Memory**

Okay, so I need to get a handle on how this HugeTLB virtual memory optimization is working. I'm going to start with a systematic approach. First, I'll go through the key functions involved in managing these sections and virtual memory mappings. Let's see... I should check `sparse_add_section`, `section_activate`, `vmemmap_populate_compound_pages`, `section_nr_vmemmap_pages`, and `hugetlb_vmemmap_optimize_folio`. Those seem like core components. I need to understand how they interact to achieve this optimization. I know `hugetlb_vmemmap_optimize_folio` plays a pivotal role, so I definitely want to examine its callers to grasp the flow of execution. After that, I'll identify the critical entrypoints within `hugetlb_vmemmap.c` that handle HVO functionality. I'm hoping this will give me the information I need to understand the big picture and identify any potential performance bottlenecks or areas for further investigation within the HugeTLB virtual memory management system.



14/2 2026/09/08 03:11 tool grepper
0m Args:
{
  "Expression": "hugetlb_vmemmap_optimize",
  "PathPrefix": "mm/"
}

Results:
{
  "Output": "mm/hugetlb.c=1390=void remove_hugetlb_folio(struct hstate *h, struct folio *folio,\n--\nmm/hugetlb.c-1421-\t */\nmm/hugetlb.c:1422:\tif (!folio_test_hugetlb_vmemmap_optimized(folio))\nmm/hugetlb.c-1423-\t\t__folio_clear_hugetlb(folio);\n--\nmm/hugetlb.c=1429=void add_hugetlb_folio(struct hstate *h, struct folio *folio,\n--\nmm/hugetlb.c-1433-\nmm/hugetlb.c:1434:\tVM_BUG_ON_FOLIO(!folio_test_hugetlb_vmemmap_optimized(folio), folio);\nmm/hugetlb.c-1435-\n--\nmm/hugetlb.c-1449-\t/*\nmm/hugetlb.c:1450:\t * We have to set hugetlb_vmemmap_optimized again as above\nmm/hugetlb.c-1451-\t * folio_change_private(folio, NULL) cleared it.\nmm/hugetlb.c-1452-\t */\nmm/hugetlb.c:1453:\tfolio_set_hugetlb_vmemmap_optimized(folio);\nmm/hugetlb.c-1454-\n--\nmm/hugetlb.c=1459=static void __update_and_free_hugetlb_folio(struct hstate *h,\n--\nmm/hugetlb.c-1461-{\nmm/hugetlb.c:1462:\tbool clear_flag = folio_test_hugetlb_vmemmap_optimized(folio);\nmm/hugetlb.c-1463-\n--\nmm/hugetlb.c=1566=static void update_and_free_hugetlb_folio(struct hstate *h, struct folio *folio,\n--\nmm/hugetlb.c-1568-{\nmm/hugetlb.c:1569:\tif (!folio_test_hugetlb_vmemmap_optimized(folio) || !atomic) {\nmm/hugetlb.c-1570-\t\t__update_and_free_hugetlb_folio(h, folio);\n--\nmm/hugetlb.c=1868=static struct folio *alloc_fresh_hugetlb_folio(struct hstate *h,\n--\nmm/hugetlb.c-1874-\tif (folio)\nmm/hugetlb.c:1875:\t\thugetlb_vmemmap_optimize_folio(h, folio);\nmm/hugetlb.c-1876-\treturn folio;\n--\nmm/hugetlb.c=1879=void prep_and_add_allocated_folios(struct hstate *h,\n--\nmm/hugetlb.c-1885-\t/* Send list for bulk vmemmap optimization processing */\nmm/hugetlb.c:1886:\thugetlb_vmemmap_optimize_folios(h, folio_list);\nmm/hugetlb.c-1887-\n--\nmm/hugetlb.c=3155=static bool __init alloc_bootmem_huge_page(struct hstate *h, int nid)\n--\nmm/hugetlb.c-3191-\t\tm-\u003eflags |= HUGE_BOOTMEM_ZONES_VALID;\nmm/hugetlb.c:3192:\t\thugetlb_vmemmap_optimize_bootmem_page(pfn, huge_page_order(h));\nmm/hugetlb.c-3193-\t\t/*\n--\nmm/hugetlb.c=3276=static void __init prep_and_add_bootmem_folios(struct hstate *h,\n--\nmm/hugetlb.c-3282-\t/* Send list for bulk vmemmap optimization processing */\nmm/hugetlb.c:3283:\thugetlb_vmemmap_optimize_bootmem_folios(h, folio_list);\nmm/hugetlb.c-3284-\nmm/hugetlb.c-3285-\tlist_for_each_entry_safe(folio, tmp_f, folio_list, lru) {\nmm/hugetlb.c:3286:\t\tif (!folio_test_hugetlb_vmemmap_optimized(folio)) {\nmm/hugetlb.c-3287-\t\t\t/*\n--\nmm/hugetlb.c=3310=static void __init gather_bootmem_prealloc_node(unsigned long nid)\n--\nmm/hugetlb.c-3336-\t\tif (vmemmap_optimizable_order(pfn_to_section_order(folio_pfn(folio))))\nmm/hugetlb.c:3337:\t\t\tfolio_set_hugetlb_vmemmap_optimized(folio);\nmm/hugetlb.c-3338-\t\tsection_set_order_range(folio_pfn(folio), folio_nr_pages(folio), 0);\n--\nmm/hugetlb.c=3959=static long demote_free_hugetlb_folios(struct hstate *src, struct hstate *dst,\n--\nmm/hugetlb.c-3983-\nmm/hugetlb.c:3984:\t\tif (folio_test_hugetlb_vmemmap_optimized(folio))\nmm/hugetlb.c-3985-\t\t\tcontinue;\n--\nmm/hugetlb_vmemmap.c=388=static bool vmemmap_optimize_enabled = IS_ENABLED(CONFIG_HUGETLB_PAGE_OPTIMIZE_VMEMMAP_DEFAULT_ON);\nmm/hugetlb_vmemmap.c:389:static int __init hugetlb_vmemmap_optimize_param(char *buf)\nmm/hugetlb_vmemmap.c-390-{\n--\nmm/hugetlb_vmemmap.c-392-}\nmm/hugetlb_vmemmap.c:393:early_param(\"hugetlb_free_vmemmap\", hugetlb_vmemmap_optimize_param);\nmm/hugetlb_vmemmap.c-394-\nmm/hugetlb_vmemmap.c=395=static int __hugetlb_vmemmap_restore_folio(const struct hstate *h,\n--\nmm/hugetlb_vmemmap.c-403-\nmm/hugetlb_vmemmap.c:404:\tif (!folio_test_hugetlb_vmemmap_optimized(folio))\nmm/hugetlb_vmemmap.c-405-\t\treturn 0;\n--\nmm/hugetlb_vmemmap.c-419-\tif (!ret)\nmm/hugetlb_vmemmap.c:420:\t\tfolio_clear_hugetlb_vmemmap_optimized(folio);\nmm/hugetlb_vmemmap.c-421-\n--\nmm/hugetlb_vmemmap.c-426- * hugetlb_vmemmap_restore_folio - restore previously optimized (by\nmm/hugetlb_vmemmap.c:427: *\t\t\t\thugetlb_vmemmap_optimize_folio()) vmemmap pages which\nmm/hugetlb_vmemmap.c-428- *\t\t\t\twill be reallocated and remapped.\n--\nmm/hugetlb_vmemmap.c=453=long hugetlb_vmemmap_restore_folios(const struct hstate *h,\n--\nmm/hugetlb_vmemmap.c-462-\tlist_for_each_entry_safe(folio, t_folio, folio_list, lru) {\nmm/hugetlb_vmemmap.c:463:\t\tif (folio_test_hugetlb_vmemmap_optimized(folio)) {\nmm/hugetlb_vmemmap.c-464-\t\t\tret = __hugetlb_vmemmap_restore_folio(h, folio, flags);\n--\nmm/hugetlb_vmemmap.c=482=static bool vmemmap_should_optimize_folio(const struct hstate *h, struct folio *folio)\nmm/hugetlb_vmemmap.c-483-{\nmm/hugetlb_vmemmap.c:484:\tif (folio_test_hugetlb_vmemmap_optimized(folio))\nmm/hugetlb_vmemmap.c-485-\t\treturn false;\n--\nmm/hugetlb_vmemmap.c-495-\nmm/hugetlb_vmemmap.c:496:static int __hugetlb_vmemmap_optimize_folio(const struct hstate *h,\nmm/hugetlb_vmemmap.c-497-\t\t\t\t\t    struct folio *folio,\n--\nmm/hugetlb_vmemmap.c-526-\t */\nmm/hugetlb_vmemmap.c:527:\tfolio_set_hugetlb_vmemmap_optimized(folio);\nmm/hugetlb_vmemmap.c-528-\n--\nmm/hugetlb_vmemmap.c-551-\tif (ret)\nmm/hugetlb_vmemmap.c:552:\t\tfolio_clear_hugetlb_vmemmap_optimized(folio);\nmm/hugetlb_vmemmap.c-553-\n--\nmm/hugetlb_vmemmap.c-557-/**\nmm/hugetlb_vmemmap.c:558: * hugetlb_vmemmap_optimize_folio - optimize @folio's vmemmap pages.\nmm/hugetlb_vmemmap.c-559- * @h:\t\tstruct hstate.\n--\nmm/hugetlb_vmemmap.c-563- * guarantee that the optimization will succeed after it returns. The caller\nmm/hugetlb_vmemmap.c:564: * can use folio_test_hugetlb_vmemmap_optimized(@folio) to detect if @folio's\nmm/hugetlb_vmemmap.c-565- * vmemmap pages have been optimized.\nmm/hugetlb_vmemmap.c-566- */\nmm/hugetlb_vmemmap.c:567:void hugetlb_vmemmap_optimize_folio(const struct hstate *h, struct folio *folio)\nmm/hugetlb_vmemmap.c-568-{\n--\nmm/hugetlb_vmemmap.c-570-\nmm/hugetlb_vmemmap.c:571:\t__hugetlb_vmemmap_optimize_folio(h, folio, \u0026vmemmap_pages, 0);\nmm/hugetlb_vmemmap.c-572-\tfree_vmemmap_page_list(\u0026vmemmap_pages);\n--\nmm/hugetlb_vmemmap.c=575=static int hugetlb_vmemmap_split_folio(const struct hstate *h, struct folio *folio)\n--\nmm/hugetlb_vmemmap.c-591-\nmm/hugetlb_vmemmap.c:592:static void __hugetlb_vmemmap_optimize_folios(struct hstate *h,\nmm/hugetlb_vmemmap.c-593-\t\t\t\t\t      struct list_head *folio_list,\n--\nmm/hugetlb_vmemmap.c-608-\nmm/hugetlb_vmemmap.c:609:\t\tif (boot \u0026\u0026 folio_test_hugetlb_vmemmap_optimized(folio)) {\nmm/hugetlb_vmemmap.c-610-\t\t\t/*\n--\nmm/hugetlb_vmemmap.c-638-\t\t * there is a mix of pre-HVO and not yet HVO-ed folios\nmm/hugetlb_vmemmap.c:639:\t\t * here, as __hugetlb_vmemmap_optimize_folio() will\nmm/hugetlb_vmemmap.c-640-\t\t * skip any folios that already have the optimized flag\n--\nmm/hugetlb_vmemmap.c-649-\nmm/hugetlb_vmemmap.c:650:\t\tret = __hugetlb_vmemmap_optimize_folio(h, folio, \u0026vmemmap_pages, flags);\nmm/hugetlb_vmemmap.c-651-\n--\nmm/hugetlb_vmemmap.c-656-\t\t * halfway and head page allocation also failed. In this\nmm/hugetlb_vmemmap.c:657:\t\t * case __hugetlb_vmemmap_optimize_folio() would free memory\nmm/hugetlb_vmemmap.c-658-\t\t * allowing more vmemmap remaps to occur.\n--\nmm/hugetlb_vmemmap.c-663-\t\t\tINIT_LIST_HEAD(\u0026vmemmap_pages);\nmm/hugetlb_vmemmap.c:664:\t\t\t__hugetlb_vmemmap_optimize_folio(h, folio, \u0026vmemmap_pages, flags);\nmm/hugetlb_vmemmap.c-665-\t\t}\n--\nmm/hugetlb_vmemmap.c-672-\nmm/hugetlb_vmemmap.c:673:void hugetlb_vmemmap_optimize_folios(struct hstate *h, struct list_head *folio_list)\nmm/hugetlb_vmemmap.c-674-{\nmm/hugetlb_vmemmap.c:675:\t__hugetlb_vmemmap_optimize_folios(h, folio_list, false);\nmm/hugetlb_vmemmap.c-676-}\nmm/hugetlb_vmemmap.c-677-\nmm/hugetlb_vmemmap.c:678:void hugetlb_vmemmap_optimize_bootmem_folios(struct hstate *h, struct list_head *folio_list)\nmm/hugetlb_vmemmap.c-679-{\nmm/hugetlb_vmemmap.c:680:\t__hugetlb_vmemmap_optimize_folios(h, folio_list, true);\nmm/hugetlb_vmemmap.c-681-}\nmm/hugetlb_vmemmap.c-682-\nmm/hugetlb_vmemmap.c:683:void __init hugetlb_vmemmap_optimize_bootmem_page(unsigned long pfn, unsigned int order)\nmm/hugetlb_vmemmap.c-684-{\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);\nmm/hugetlb_vmemmap.h:28:void hugetlb_vmemmap_optimize_bootmem_page(unsigned long pfn, unsigned int order);\nmm/hugetlb_vmemmap.h-29-\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--\nmm/hugetlb_vmemmap.h-68-\nmm/hugetlb_vmemmap.h:69:static inline void hugetlb_vmemmap_optimize_bootmem_folios(struct hstate *h,\nmm/hugetlb_vmemmap.h-70-\t\t\t\t\t\tstruct list_head *folio_list)\n--\nmm/hugetlb_vmemmap.h=74=static inline unsigned int hugetlb_vmemmap_optimizable_size(const struct hstate *h)\n--\nmm/hugetlb_vmemmap.h-78-\nmm/hugetlb_vmemmap.h:79:static inline void hugetlb_vmemmap_optimize_bootmem_page(unsigned long pfn, unsigned int order)\nmm/hugetlb_vmemmap.h-80-{\n--\nmm/memory-failure.c=1984=static unsigned long folio_free_raw_hwp(struct folio *folio, bool move_flag)\n--\nmm/memory-failure.c-1986-\t/*\nmm/memory-failure.c:1987:\t * hugetlb_vmemmap_optimized hugepages can't be freed because struct\nmm/memory-failure.c-1988-\t * pages for tail pages are required but they don't exist.\nmm/memory-failure.c-1989-\t */\nmm/memory-failure.c:1990:\tif (move_flag \u0026\u0026 folio_test_hugetlb_vmemmap_optimized(folio))\nmm/memory-failure.c-1991-\t\treturn 0;\n--\nmm/memory-failure.c=2003=void folio_clear_hugetlb_hwpoison(struct folio *folio)\n--\nmm/memory-failure.c-2006-\t\treturn;\nmm/memory-failure.c:2007:\tif (folio_test_hugetlb_vmemmap_optimized(folio))\nmm/memory-failure.c-2008-\t\treturn;\n"
}

15/2 2026/09/08 03:11 llm patch-evaluator
0m Model:
gemini-3.7-flash

Args:
null

Results:
null

Tokens:
							input: 31728
							output: 281
							thoughts: 1482
						

Thoughts:
**Analyzing vmemmap Optimization and Related Code for Fuzzing**

Alright, let's break this down systematically. My immediate thought is, "Where does this patch *really* touch?" I need to figure out if it's fuzzable and how best to target it.

First, I need to identify the key functions related to `vmemmap`, focusing specifically on memory hotplug and device-dax. I've listed the functions I'm interested in, based on the filenames provided. The ones that catch my eye are the shared tail page functions and the `memmap` modifications.

Next, I'm checking which of these functions are actually exposed – whether they're symbols in the kernel's symbol table or non-static. The intent is to determine if they are accessible from outside their immediate compilation unit.

Now, considering the patch description, it's clear this refactors vmemmap optimization logic, affecting how shared tail pages are handled in both HugeTLB and device-dax contexts, along with changes to how device dax tail pages are mapped and managed. It also seems to change zone device tail page reservation. This has implications in core memory management, which I categorize as reachable during standard QEMU amd64 kernel execution. *That* makes it fuzzable, so `WorthFuzzing` is `true`.

I also need to verify the relevant configuration options. `SPARSEMEM_VMEMMAP`, `HUGETLB_PAGE_OPTIMIZE_VMEMMAP`, `HUGETLB_PAGE`, `DEV_DAX`, `ZONE_DEVICE`, and `TRANSPARENT_HUGEPAGE` are the ones to keep an eye on. I need to make sure I've got the correct names and dependencies straight from the Kconfig files.

Okay, let's define the specific symbols I need to *really* focus on. I'm looking for functions that *implement* or *directly exercise* the refactored logic, not just generic memory management primitives. That means:

*   `vmemmap_shared_tail_page` (critical because it's the core of the new tail page handling).
*   `section_nr_vmemmap_pages` (important for verifying new calculations).
*   `vmemmap_populate_compound_pages` (which is updated to utilize the new tail page handling).
*   `hugetlb_vmemmap_optimize_folio` (calls the new tail page handling during HugeTLB optimization).
*   `section_activate` (for section order handling).
*   `sparse_add_section` (for vmemmap section poisoning / addition).
*   `memmap_init_zone_device` (for compound page initialization).
*   `remove_pfn_range_from_zone` (for poison logic).

I've made sure to specifically exclude any generic or ubiquitous functions. These are all focused on the vmemmap deduplication/optimization, HugeTLB HVO, memory section hotplug/activation, and ZONE_DEVICE memmap setup. Good. It's time to generate the result and move forward.



16/2 2026/09/08 03:11 tool set-results
0m Args:
{
  "EnableConfigs": [
    "SPARSEMEM_VMEMMAP",
    "HUGETLB_PAGE",
    "HUGETLB_PAGE_OPTIMIZE_VMEMMAP",
    "DEV_DAX",
    "ZONE_DEVICE"
  ],
  "FocusSymbols": [
    "vmemmap_shared_tail_page",
    "section_nr_vmemmap_pages",
    "vmemmap_populate_compound_pages",
    "hugetlb_vmemmap_optimize_folio",
    "section_activate",
    "sparse_add_section",
    "memmap_init_zone_device",
    "remove_pfn_range_from_zone"
  ],
  "Reasoning": "The patch series refactors and unifies vmemmap deduplication/optimization between HugeTLB and Device-DAX under CONFIG_SPARSEMEM_VMEMMAP_OPTIMIZATION. It modifies core memory management (mm/) logic for shared tail page allocation/caching (vmemmap_shared_tail_page), section activation and memmap population (vmemmap_populate_compound_pages, section_activate, sparse_add_section), and zone/page poisoning during hot-unplug. These changes touch reachable core mm code and warrant fuzzing.",
  "WorthFuzzing": true
}

Results:
{
  "EnableConfigs": [
    "SPARSEMEM_VMEMMAP",
    "HUGETLB_PAGE",
    "HUGETLB_PAGE_OPTIMIZE_VMEMMAP",
    "DEV_DAX",
    "ZONE_DEVICE"
  ],
  "FocusSymbols": [
    "vmemmap_shared_tail_page",
    "section_nr_vmemmap_pages",
    "vmemmap_populate_compound_pages",
    "hugetlb_vmemmap_optimize_folio",
    "section_activate",
    "sparse_add_section",
    "memmap_init_zone_device",
    "remove_pfn_range_from_zone"
  ],
  "Reasoning": "The patch series refactors and unifies vmemmap deduplication/optimization between HugeTLB and Device-DAX under CONFIG_SPARSEMEM_VMEMMAP_OPTIMIZATION. It modifies core memory management (mm/) logic for shared tail page allocation/caching (vmemmap_shared_tail_page), section activation and memmap population (vmemmap_populate_compound_pages, section_activate, sparse_add_section), and zone/page poisoning during hot-unplug. These changes touch reachable core mm code and warrant fuzzing.",
  "WorthFuzzing": true
}

17/1 2026/09/08 03:11 action If
0m Args:
{
  "WorthFuzzing": true
}

Results:
null

18/2 2026/09/08 03:11 agent kmsan-evaluator
0m Model:
core

Args:
null

Results:
{
  "KMSANReasoning": "The patch series refactors and unifies vmemmap tail page optimization/deduplication between HugeTLB and Device DAX across architectures (x86, powerpc) and core MM code. It introduces CONFIG_SPARSEMEM_VMEMMAP_OPTIMIZATION and helper functions in include/linux/vmemmap-optimization.h, sharing tail pages managed via vmemmap_shared_tail_page(). \n\nAll allocated tail pages are explicitly zero-initialized using mm_zero_struct_page() and properly configured before being linked into zone-\u003evmemmap_tails. The patch does not touch user-copy interfaces (copy_to_user/put_user), syscall/ioctl boundaries, or netlink handlers, nor does it introduce branching on uninitialized variables or uninitialized info-leak risks. Any potential memory bugs in page table setup, section activation/deactivation, or page struct lifecycles are addressable by standard debugging facilities and KASAN/DEBUG_VM. Therefore, dedicated KMSAN fuzzing is not required.",
  "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 d8ae45bfc251d4b01248c95dae7fdb0a92e389d2
Author: syz-cluster <triage@syzkaller.com>
Date:   Tue Sep 8 03:11:08 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 c7aaa2e4790ec..2985286cbbbf4 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -12096,6 +12096,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/powerpc/mm/book3s64/radix_pgtable.c b/arch/powerpc/mm/book3s64/radix_pgtable.c
index cf692b2b5f7bc..e7e751c48dd24 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_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/x86/entry/vdso/vdso32/fake_32bit_build.h b/arch/x86/entry/vdso/vdso32/fake_32bit_build.h
index bc3e549795c3f..5f8424eade2bd 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_SPARSEMEM_VMEMMAP_OPTIMIZATION
 #undef CONFIG_NR_CPUS
 #undef CONFIG_PARAVIRT_XXL
 
diff --git a/drivers/dax/Kconfig b/drivers/dax/Kconfig
index 602f9a0839a91..6250954b0fa7a 100644
--- a/drivers/dax/Kconfig
+++ b/drivers/dax/Kconfig
@@ -8,6 +8,8 @@ if DAX
 config DEV_DAX
 	tristate "Device DAX: direct access mapping device"
 	depends on TRANSPARENT_HUGEPAGE
+	depends on ZONE_DEVICE
+	select SPARSEMEM_VMEMMAP_OPTIMIZATION if ARCH_WANT_OPTIMIZE_DAX_VMEMMAP
 	help
 	  Support raw access to differentiated (persistence, bandwidth,
 	  latency...) memory via an mmap(2) capable character
diff --git a/fs/Kconfig b/fs/Kconfig
index d1c210c6508f0..9b32ce79cc805 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 SPARSEMEM_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..969594074fd2d 100644
--- a/include/linux/mm.h
+++ b/include/linux/mm.h
@@ -5167,7 +5167,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 +5174,9 @@ static inline bool __vmemmap_can_optimize(struct vmem_altmap *altmap,
 	unsigned long nr_pages;
 	unsigned long nr_vmemmap_pages;
 
+	if (!IS_ENABLED(CONFIG_SPARSEMEM_VMEMMAP_OPTIMIZATION))
+		return false;
+
 	if (!pgmap || !is_power_of_2(sizeof(struct page)))
 		return false;
 
@@ -5184,7 +5186,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 c9ae7991a8b20..03ed9232f186b 100644
--- a/include/linux/mmzone.h
+++ b/include/linux/mmzone.h
@@ -102,9 +102,9 @@
  *
  * 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)) ? \
+#define MAX_FOLIO_VMEMMAP_ALIGN					\
+	(IS_ENABLED(CONFIG_SPARSEMEM_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. */
@@ -116,7 +116,8 @@
 #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)
+	((__VMEMMAP_OPTIMIZATION_NR_ORDERS > 0 &&	\
+	  IS_ENABLED(CONFIG_SPARSEMEM_VMEMMAP_OPTIMIZATION)) ? __VMEMMAP_OPTIMIZATION_NR_ORDERS : 0)
 
 enum migratetype {
 	MIGRATE_UNMOVABLE,
@@ -1155,7 +1156,7 @@ 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
+#ifdef CONFIG_SPARSEMEM_VMEMMAP
 	struct page *vmemmap_tails[VMEMMAP_OPTIMIZATION_NR_ORDERS];
 #endif
 } ____cacheline_internodealigned_in_smp;
@@ -1685,11 +1686,21 @@ static inline bool zone_is_zone_device(const struct zone *zone)
 {
 	return zone_idx(zone) == ZONE_DEVICE;
 }
+
+static inline struct zone *device_zone(int nid)
+{
+	return &NODE_DATA(nid)->node_zones[ZONE_DEVICE];
+}
 #else
 static inline bool zone_is_zone_device(const struct zone *zone)
 {
 	return false;
 }
+
+static inline struct zone *device_zone(int nid)
+{
+	return NULL;
+}
 #endif
 
 /*
@@ -2019,7 +2030,7 @@ struct mem_section {
 	unsigned long section_mem_map;
 
 	struct mem_section_usage *usage;
-#ifdef CONFIG_HUGETLB_PAGE_OPTIMIZE_VMEMMAP
+#ifdef CONFIG_SPARSEMEM_VMEMMAP_OPTIMIZATION
 	/*
 	 * Normally, sections hold regular (order-0) pages. However, for
 	 * sections with HVO enabled, this tracks the compound page order
diff --git a/include/linux/page-flags.h b/include/linux/page-flags.h
index 86dd0470da117..462e89e055485 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_SPARSEMEM_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..492dc662aae73
--- /dev/null
+++ b/include/linux/vmemmap-optimization.h
@@ -0,0 +1,91 @@
+/* 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>
+
+#ifdef CONFIG_SPARSEMEM_VMEMMAP_OPTIMIZATION
+static inline unsigned int section_order(const struct mem_section *section)
+{
+	return section->order;
+}
+
+static inline void section_set_order(struct mem_section *section, unsigned int order)
+{
+	VM_WARN_ON(section_order(section) && order && section_order(section) != order);
+	section->order = order;
+}
+
+static inline void section_set_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_order(__nr_to_section(section_nr + i), order);
+}
+
+static inline unsigned int pfn_to_section_order(unsigned long pfn)
+{
+	return section_order(__pfn_to_section(pfn));
+}
+#else
+static inline unsigned int section_order(const struct mem_section *section)
+{
+	return 0;
+}
+
+static inline void section_set_order(struct mem_section *section, unsigned int order)
+{
+}
+
+static inline void section_set_order_range(unsigned long pfn, unsigned long nr_pages,
+					   unsigned int order)
+{
+}
+
+static inline unsigned int pfn_to_section_order(unsigned long pfn)
+{
+	return 0;
+}
+#endif /* CONFIG_SPARSEMEM_VMEMMAP_OPTIMIZATION */
+
+static inline bool vmemmap_optimizable_pfn(unsigned long pfn)
+{
+	const unsigned int order = pfn_to_section_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_SPARSEMEM_VMEMMAP_OPTIMIZATION))
+		return false;
+
+	if (!is_power_of_2(sizeof(struct page)))
+		return false;
+
+	return order >= VMEMMAP_OPTIMIZATION_MIN_ORDER;
+}
+
+#ifdef CONFIG_SPARSEMEM_VMEMMAP
+struct page *vmemmap_shared_tail_page(unsigned int order, struct zone *zone);
+#endif /* CONFIG_SPARSEMEM_VMEMMAP */
+#endif /* _LINUX_VMEMMAP_OPTIMIZATION_H */
diff --git a/mm/Kconfig b/mm/Kconfig
index 79163b7d795a3..288b32dcc350f 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 SPARSEMEM_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.
diff --git a/mm/hugetlb.c b/mm/hugetlb.c
index fda525bccf50e..dff915e6c48b7 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 eb339c4a71f40..25c4e7d2664c3 100644
--- a/mm/hugetlb_vmemmap.c
+++ b/mm/hugetlb_vmemmap.c
@@ -15,10 +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"
 
 /**
@@ -493,32 +493,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 +509,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 9e8ffd01b4f75..629420a838918 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 e62e6aa07f126..4950ac2a1c012 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_order(ms);
-	const int vmemmap_pages = pgmap ? VMEMMAP_RESERVE_NR : VMEMMAP_OPTIMIZATION_PAGES;
+	const int order = section_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,62 @@ 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)
+struct page __ref *vmemmap_shared_tail_page(unsigned int order, struct zone *zone)
 {
-	struct page *p, *tail;
-	unsigned int idx;
-	int node = zone_to_nid(zone);
+	void *addr;
+	struct page *page;
+	const unsigned int idx = order - VMEMMAP_OPTIMIZATION_MIN_ORDER;
 
-	if (WARN_ON_ONCE(order < VMEMMAP_OPTIMIZATION_MIN_ORDER))
-		return NULL;
-	if (WARN_ON_ONCE(order > MAX_FOLIO_ORDER))
+	if (WARN_ON_ONCE(idx >= ARRAY_SIZE(zone->vmemmap_tails)))
 		return NULL;
 
-	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)
+	page = READ_ONCE(zone->vmemmap_tails[idx]);
+	if (likely(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++)
-		init_compound_tail(p + i, NULL, order, zone);
 
-	tail = virt_to_page(p);
-	zone->vmemmap_tails[idx] = tail;
+	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);
+	}
 
-	return tail;
-}
-#else
-static inline struct page *vmemmap_get_tail(unsigned int order, struct zone *zone)
-{
-	return NULL;
+	page = virt_to_page(addr);
+	if (cmpxchg(&zone->vmemmap_tails[idx], NULL, page) != NULL) {
+		if (slab_is_available())
+			__free_page(page);
+		else
+			memblock_free(addr, PAGE_SIZE);
+		page = READ_ONCE(zone->vmemmap_tails[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_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 +240,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;
@@ -262,7 +255,7 @@ static pte_t * __meminit vmemmap_pte_populate(pmd_t *pmd, unsigned long addr, in
 			 * and through vmemmap_populate_compound_pages() when
 			 * slab is available.
 			 */
-			if (flags & VMEMMAP_POPULATE_PAGEREF)
+			if (flags & VMEMMAP_POPULATE_DAX)
 				get_page(pfn_to_page(ptpfn));
 		}
 		entry = pfn_pte(ptpfn, PAGE_KERNEL);
@@ -497,23 +490,6 @@ 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,
@@ -522,44 +498,34 @@ static int __meminit vmemmap_populate_compound_pages(unsigned long start_pfn,
 	unsigned long size, addr;
 	pte_t *pte;
 	int rc;
+	unsigned long flags = VMEMMAP_POPULATE_DAX;
+	struct page *page;
+	unsigned int order = pfn_to_section_order(start_pfn);
 
-	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 +547,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 +636,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 +647,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 +657,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 +765,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_order(ms, 0);
+	}
 }
 
 static struct page * __meminit section_activate(int nid, unsigned long pfn,
@@ -811,8 +778,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_order(ms))
+		return ERR_PTR(-ENOTSUPP);
+
 	if (!ms->usage) {
 		usage = kzalloc(mem_section_usage_size(), GFP_KERNEL);
 		if (!usage)
@@ -838,6 +810,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_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 +856,14 @@ 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.
 	 */
-	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 9349ed6326c01..adf057f54c0f0 100644
--- a/mm/sparse.c
+++ b/mm/sparse.c
@@ -251,8 +251,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 049272aba84e5..5d0f407bde7e0 100644
--- a/mm/sparse.h
+++ b/mm/sparse.h
@@ -9,77 +9,7 @@
 #define __MM_SPARSE_H
 
 #include <linux/mmzone.h>
-
-#ifdef CONFIG_HUGETLB_PAGE_OPTIMIZE_VMEMMAP
-static inline unsigned int section_order(const struct mem_section *section)
-{
-	return section->order;
-}
-
-static inline void section_set_order(struct mem_section *section, unsigned int order)
-{
-	VM_WARN_ON(section_order(section) && order && section_order(section) != order);
-	section->order = order;
-}
-
-static inline void section_set_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_order(__nr_to_section(section_nr + i), order);
-}
-
-static inline unsigned int pfn_to_section_order(unsigned long pfn)
-{
-	return section_order(__pfn_to_section(pfn));
-}
-#else
-static inline unsigned int section_order(const struct mem_section *section)
-{
-	return 0;
-}
-
-static inline void section_set_order(struct mem_section *section, unsigned int order)
-{
-}
-
-static inline void section_set_order_range(unsigned long pfn, unsigned long nr_pages,
-					   unsigned int order)
-{
-}
-
-static inline unsigned int pfn_to_section_order(unsigned long pfn)
-{
-	return 0;
-}
-#endif
-
-static inline bool vmemmap_optimizable_pfn(unsigned long pfn)
-{
-	const unsigned int order = pfn_to_section_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
@@ -140,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);
 }

19/3 2026/09/08 03:11 llm kmsan-evaluator
0m Model:
gemini-3.7-flash

Args:
null

Results:
null

Tokens:
							input: 18256
							output: 243
							thoughts: 1129
						

Thoughts:
**KMSAN Fuzzing Session Assessment: vmemmap Tail Page Optimization**

Okay, let's break down this patch series. My initial thought is to determine if a dedicated Kernel Memory Sanitizer (KMSAN) fuzzing session is warranted. First, I need to fully understand the changes. This patch is essentially refactoring and generalizing the vmemmap tail page deduplication, previously specific to HugeTLB, to also include Device DAX. They're introducing `CONFIG_SPARSEMEM_VMEMMAP_OPTIMIZATION`, a new header for inline helpers, and altering how tail pages are handled for Device DAX, leveraging the HugeTLB approach of sharing tail pages via `vmemmap_shared_tail_page`. In essence, they are making tail page sharing more generic.

Now, the critical part: KMSAN relevance. I have to assess whether this introduces any potential for uninitialized memory leaks or reads. I'm focusing on these specific areas:
1.  **User-space exposure:** Does this patch touch any kernel structures that are sent to user space (e.g., via `copy_to_user`) where uninitialized fields might leak? No.
2.  **Conditional logic and uninitialized variables:** Is there any conditional branching based on uninitialized variables? No.
3.  **Complex data structures:** Are there any changes that could lead to uninitialized fields being read from complex data structures? No. Critically, `vmemmap_shared_tail_page` explicitly initializes the pages with `mm_zero_struct_page(page)` or allocates them with `vmemmap_alloc_block`.
4.  **Bounds checks:** Could any changes to buffer lengths or bounds checks expose uninitialized memory? No.

I need to confirm proper initialization of the `vmemmap` struct pages within `vmemmap_shared_tail_page`. Looking closely at the code, `mm_zero_struct_page(page)` is indeed used to zero the `struct page`. Excellent.

What about changes related to `page_init_poison()`? In the `remove_pfn_range_from_zone` and `sparse_add_section` functions, the code skips `page_init_poison` if `section_vmemmap_optimizable(ms)`. This makes sense because the optimized sections share the tail page. Poisoning the tail page would effectively corrupt the shared resource. This appears to be correct MM memory map setup logic.

Are there any changes that directly expose buffers to userspace, or alter any `copy_to_user` operations? No.

Based on all the areas I've looked at, are any uninitialized memory risks being introduced? No. I am very confident that the changes are safe as they are initializing everything properly.

Any bugs that could occur here would be the usual types of memory mapping issues, like reference count bugs, use-after-free, or out-of-bounds page access, and those are covered by KASAN and standard debuggers (like `CONFIG_DEBUG_VM`). In my professional opinion, KMSAN is not the right tool here. KMSAN doesn't track this type of vmemmap deduplication page frame content.

**Therefore, NeedsKMSAN = false.**

I'll now trigger the `set-results` tool and move on to the next patch.



20/3 2026/09/08 03:11 tool set-results
0m Args:
{
  "KMSANReasoning": "The patch series refactors and unifies vmemmap tail page optimization/deduplication between HugeTLB and Device DAX across architectures (x86, powerpc) and core MM code. It introduces CONFIG_SPARSEMEM_VMEMMAP_OPTIMIZATION and helper functions in include/linux/vmemmap-optimization.h, sharing tail pages managed via vmemmap_shared_tail_page(). \n\nAll allocated tail pages are explicitly zero-initialized using mm_zero_struct_page() and properly configured before being linked into zone-\u003evmemmap_tails. The patch does not touch user-copy interfaces (copy_to_user/put_user), syscall/ioctl boundaries, or netlink handlers, nor does it introduce branching on uninitialized variables or uninitialized info-leak risks. Any potential memory bugs in page table setup, section activation/deactivation, or page struct lifecycles are addressable by standard debugging facilities and KASAN/DEBUG_VM. Therefore, dedicated KMSAN fuzzing is not required.",
  "NeedsKMSAN": false
}

Results:
{
  "KMSANReasoning": "The patch series refactors and unifies vmemmap tail page optimization/deduplication between HugeTLB and Device DAX across architectures (x86, powerpc) and core MM code. It introduces CONFIG_SPARSEMEM_VMEMMAP_OPTIMIZATION and helper functions in include/linux/vmemmap-optimization.h, sharing tail pages managed via vmemmap_shared_tail_page(). \n\nAll allocated tail pages are explicitly zero-initialized using mm_zero_struct_page() and properly configured before being linked into zone-\u003evmemmap_tails. The patch does not touch user-copy interfaces (copy_to_user/put_user), syscall/ioctl boundaries, or netlink handlers, nor does it introduce branching on uninitialized variables or uninitialized info-leak risks. Any potential memory bugs in page table setup, section activation/deactivation, or page struct lifecycles are addressable by standard debugging facilities and KASAN/DEBUG_VM. Therefore, dedicated KMSAN fuzzing is not required.",
  "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)