Skip to content

Commit c0c6ccd

Browse files
davidhildenbrandakpm00
authored andcommitted
mm: fix __vm_normal_page() to handle missing support for pmd_special()/pud_special()
On x86 32-bit with THP enabled, zap_huge_pmd() is seen to generate a "WARNING: mm/memory.c:735 at __vm_normal_page+0x6a/0x7d", from the VM_WARN_ON_ONCE(is_zero_pfn(pfn) || is_huge_zero_pfn(pfn)); followed by "BUG: Bad rss-counter state"s, then later "BUG: Bad page state"s when reclaim gets to call shrink_huge_zero_folio_scan(). It's as if the _PAGE_SPECIAL bit never got set in the huge_zero pmd: and indeed, whereas pte_special() and pte_mkspecial() are subject to a dedicated CONFIG_ARCH_HAS_PTE_SPECIAL, pmd_special() and pmd_mkspecial() are subject to CONFIG_ARCH_SUPPORTS_PMD_PFNMAP, which is never enabled on any 32-bit architecture. While the problem was exposed through commit d80a9cb ("mm/huge_memory: add and use normal_or_softleaf_folio_pmd()"), it was an oversight in commit af38538 ("mm/memory: factor out common code from vm_normal_page_*()") and would result in other problems: * huge zero folio accounted in smaps, pagemap (PAGE_IS_FILE) and numamaps as file-backed THP * folio_walk_start() returning the folio even without FW_ZEROPAGE set. Callers seem to tolerate that, though. ... and triggering the VM_WARN_ON_ONE(), although never reported so far. To fix it, teach vm_normal_page_pmd()/vm_normal_page_pud() to consider whether pmd_special/pud_special is actually implemented. Link: https://lore.kernel.org/20260430-pmd_special-v1-1-dbcbcfd72c20@kernel.org Fixes: af38538 ("mm/memory: factor out common code from vm_normal_page_*()") Signed-off-by: David Hildenbrand (Arm) <david@kernel.org> Reported-by: Hugh Dickins <hughd@google.com> Closes: https://lore.kernel.org/r/74a75b59-2e13-3985-ee99-d5521f39df2a@google.com Reported-by: Bibo Mao <maobibo@loongson.cn> Closes: https://lore.kernel.org/r/20260430041121.2839350-1-maobibo@loongson.cn Debugged-by: Hugh Dickins <hughd@google.com> Reviewed-by: Lance Yang <lance.yang@linux.dev> Tested-by: Bibo Mao <maobibo@loongson.cn> Reviewed-by: Baolin Wang <baolin.wang@linux.alibaba.com> Reviewed-by: Oscar Salvador <osalvador@suse.de> Reviewed-by: Lorenzo Stoakes <ljs@kernel.org> Cc: Liam R. Howlett <liam@infradead.org> Cc: Michal Hocko <mhocko@suse.com> Cc: Mike Rapoport <rppt@kernel.org> Cc: Suren Baghdasaryan <surenb@google.com> Cc: Vlastimil Babka <vbabka@kernel.org> Cc: <stable@vger.kernel.org> Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
1 parent 03a2cc1 commit c0c6ccd

1 file changed

Lines changed: 19 additions & 3 deletions

File tree

mm/memory.c

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -612,6 +612,21 @@ static void print_bad_page_map(struct vm_area_struct *vma,
612612
dump_stack();
613613
add_taint(TAINT_BAD_PAGE, LOCKDEP_NOW_UNRELIABLE);
614614
}
615+
616+
static inline bool pgtable_level_has_pxx_special(enum pgtable_level level)
617+
{
618+
switch (level) {
619+
case PGTABLE_LEVEL_PTE:
620+
return IS_ENABLED(CONFIG_ARCH_HAS_PTE_SPECIAL);
621+
case PGTABLE_LEVEL_PMD:
622+
return IS_ENABLED(CONFIG_ARCH_SUPPORTS_PMD_PFNMAP);
623+
case PGTABLE_LEVEL_PUD:
624+
return IS_ENABLED(CONFIG_ARCH_SUPPORTS_PUD_PFNMAP);
625+
default:
626+
return false;
627+
}
628+
}
629+
615630
#define print_bad_pte(vma, addr, pte, page) \
616631
print_bad_page_map(vma, addr, pte_val(pte), page, PGTABLE_LEVEL_PTE)
617632

@@ -684,7 +699,7 @@ static inline struct page *__vm_normal_page(struct vm_area_struct *vma,
684699
unsigned long addr, unsigned long pfn, bool special,
685700
unsigned long long entry, enum pgtable_level level)
686701
{
687-
if (IS_ENABLED(CONFIG_ARCH_HAS_PTE_SPECIAL)) {
702+
if (pgtable_level_has_pxx_special(level)) {
688703
if (unlikely(special)) {
689704
#ifdef CONFIG_FIND_NORMAL_PAGE
690705
if (vma->vm_ops && vma->vm_ops->find_normal_page)
@@ -699,8 +714,9 @@ static inline struct page *__vm_normal_page(struct vm_area_struct *vma,
699714
return NULL;
700715
}
701716
/*
702-
* With CONFIG_ARCH_HAS_PTE_SPECIAL, any special page table
703-
* mappings (incl. shared zero folios) are marked accordingly.
717+
* With working pte_special()/pmd_special()..., any special page
718+
* table mappings (incl. shared zero folios) are marked
719+
* accordingly.
704720
*/
705721
} else {
706722
if (unlikely(vma->vm_flags & (VM_PFNMAP | VM_MIXEDMAP))) {

0 commit comments

Comments
 (0)