Some architectures can use a PMD-sized vmemmap mapping that covers the vmemmap space for more than one sparsemem section. When such a mapping is created for one section, a later HVO-capable section covered by the same PMD can find the PMD mapping already populated. That PMD entry is ordinary vmemmap backing, not an HVO-specific mapping for this section. So mark the section as not HVO-enabled. An existing PMD mapping can be reused only if it came from ordinary vmemmap population for a neighboring section in the same PMD-sized vmemmap range. If the naturally aligned HVO-capable compound page spans that whole range, the mapping must have been left behind by an earlier removal path, so warn and fail instead. Signed-off-by: Muchun Song --- mm/sparse-vmemmap.c | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/mm/sparse-vmemmap.c b/mm/sparse-vmemmap.c index 6ec0cac8eab6..6aeb55c38525 100644 --- a/mm/sparse-vmemmap.c +++ b/mm/sparse-vmemmap.c @@ -460,7 +460,7 @@ int __meminit vmemmap_populate_hugepages(unsigned long start, unsigned long end, for (addr = start; addr < end; addr = next) { unsigned long pfn = page_to_pfn((struct page *)addr); - const struct mem_section *ms = __pfn_to_section(pfn); + struct mem_section *ms = __pfn_to_section(pfn); pgd = pgd_offset_k(addr); p4d = vmemmap_pgd_populate(pgd, addr, node); @@ -493,8 +493,23 @@ int __meminit vmemmap_populate_hugepages(unsigned long start, unsigned long end, return -ENOMEM; } } else if (vmemmap_check_pmd(pmd, node, addr, next)) { - if (WARN_ON_ONCE(section_vmemmap_optimizable(ms))) + unsigned long nr_pages = 1UL << section_compound_order(ms); + + /* + * A naturally aligned HVO compound page covering a full + * PMD-sized vmemmap range cannot share that range with + * a neighboring section. An existing PMD mapping is + * therefore unexpected. + */ + if (WARN_ON_ONCE(nr_pages >= PMD_SIZE / sizeof(struct page))) return -EOPNOTSUPP; + + /* + * A PMD mapping from a neighboring section may also cover + * this section. Reuse its ordinary vmemmap backing and + * disable HVO. + */ + section_set_compound_order(ms, 0); continue; } if (vmemmap_populate_basepages(addr, next, node, altmap)) -- 2.54.0