mm: fix __vm_normal_page() to handle missing support for pmd_special()/pud_special()
Subsystems (3)
David Hildenbrand (Arm)
maintainerPATCH 1/1 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 d80a9cb1a64a ("mm/huge_memory:
add and use normal_or_softleaf_folio_pmd()"), it was an oversight in commit af38538801c6 ("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.
---
This is an alternative to Hugh's patch, whereby we leave pmd_special() be a NOP and instead teach __vm_normal_page() about lack of support for pmd_special/pud_special.
--- mm/memory.c | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-)
mm/memory.c +19 -3
@@ -612,6 +612,21 @@ static void print_bad_page_map(struct vm_area_struct *vma,dump_stack ();add_taint (TAINT_BAD_PAGE ,LOCKDEP_NOW_UNRELIABLE ); } + +static inline bool pgtable_level_has_pxx_special (enum pgtable_level level) +{ +switch (level) { +case PGTABLE_LEVEL_PTE : +return IS_ENABLED (CONFIG_ARCH_HAS_PTE_SPECIAL ); +case PGTABLE_LEVEL_PMD : +return IS_ENABLED (CONFIG_ARCH_SUPPORTS_PMD_PFNMAP ); +case PGTABLE_LEVEL_PUD : +return IS_ENABLED (CONFIG_ARCH_SUPPORTS_PUD_PFNMAP ); +default : +return false; + } +} +#define print_bad_pte (vma, addr, pte, page) \ print_bad_page_map(vma, addr, pte_val(pte), page, PGTABLE_LEVEL_PTE)
@@ -684,7 +699,7 @@ static inline struct page *__vm_normal_page(struct vm_area_struct *vma, unsigned long addr, unsigned long pfn, bool special, unsigned long long entry, enum pgtable_level level) { - if (IS_ENABLED(CONFIG_ARCH_HAS_PTE_SPECIAL)) { + if (pgtable_level_has_pxx_special(level)) { if (unlikely(special)) { #ifdef CONFIG_FIND_NORMAL_PAGE 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,return NULL ; }/* -* With CONFIG_ARCH_HAS_PTE_SPECIAL, any special page table -* mappings (incl. shared zero folios) are marked accordingly. +* With working pte_special()/pmd_special()..., any special page +* table mappings (incl. shared zero folios) are marked +* accordingly. */ }else {if (unlikely (vma->vm_flags & (VM_PFNMAP |VM_MIXEDMAP ))) {
---
base-commit: d94322006a51b522dd361128a450bf9e75aad889
change-id: 20260430-pmd_special-610dbdd8ac3c
--
Cheers,
David
Quoted 11 lines · David Hildenbrand (Arm)
On x86 32-bit with THP enabled,
zap_huge_pmd()is seen to generate a "WARNING:mm/memory.c:735at__vm_normal_page+0x6a/0x7d", from theVM_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 callshrink_huge_zero_folio_scan().It's as if the
_PAGE_SPECIALbit never got set in thehuge_zeropmd:
and indeed, whereaspte_special()andpte_mkspecial()are subject to a dedicatedCONFIG_ARCH_HAS_PTE_SPECIAL,pmd_special()andpmd_mkspecial()are subject toCONFIG_ARCH_SUPPORTS_PMD_PFNMAP, which is never enabled on any 32-bit architecture.
Ah damn. I wonder if it's really a combination of 'supports THP' and 'has a spare software defined bit free in PTE'?
In any case obviously have to fix this.
Quoted 15 lines
While the problem was exposed through commit
d80a9cb1a64a("mm/huge_memory:
add and usenormal_or_softleaf_folio_pmd()"), it was an oversight in commitaf38538801c6("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 withoutFW_ZEROPAGEset. 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.
Quoted 10 lines
Fixes af38538801c6 ("mm/memory: factor out common code from vm_normal_page_*()")Reported-by Hugh Dickins <[email protected]>Reported-by Bibo Mao <[email protected]>Debugged-by Hugh Dickins <[email protected]>Reviewed-by Lance Yang <[email protected]>Tested-by Bibo Mao <[email protected]>Signed-off-by David Hildenbrand (Arm) <[email protected]>
This LGTM, so:
Quoted 68 lines
---
This is an alternative to Hugh's patch, whereby we leavepmd_special()be a NOP and instead teach__vm_normal_page()about lack of support for pmd_special/pud_special.--- mm/memory.c | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-)mm/memory.c +19
-4@@ -612,6 +612,21 @@ static void print_bad_page_map(struct vm_area_struct *vma,dump_stack ();add_taint (TAINT_BAD_PAGE ,LOCKDEP_NOW_UNRELIABLE ); } + +static inline bool pgtable_level_has_pxx_special (enum pgtable_level level) +{ +switch (level) { +case PGTABLE_LEVEL_PTE : +return IS_ENABLED (CONFIG_ARCH_HAS_PTE_SPECIAL ); +case PGTABLE_LEVEL_PMD : +return IS_ENABLED (CONFIG_ARCH_SUPPORTS_PMD_PFNMAP ); +case PGTABLE_LEVEL_PUD : +return IS_ENABLED (CONFIG_ARCH_SUPPORTS_PUD_PFNMAP ); +default : +return false; + } +} +#define print_bad_pte (vma, addr, pte, page) \ print_bad_page_map(vma, addr, pte_val(pte), page, PGTABLE_LEVEL_PTE)@@ -684,7 +699,7 @@ static inline struct page *__vm_normal_page(struct vm_area_struct *vma, unsigned long addr, unsigned long pfn, bool special, unsigned long long entry, enum pgtable_level level) { - if (IS_ENABLED(CONFIG_ARCH_HAS_PTE_SPECIAL)) { + if (pgtable_level_has_pxx_special(level)) { if (unlikely(special)) { #ifdef CONFIG_FIND_NORMAL_PAGE 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,return NULL ; }/* -* With CONFIG_ARCH_HAS_PTE_SPECIAL, any special page table -* mappings (incl. shared zero folios) are marked accordingly. +* With working pte_special()/pmd_special()..., any special page +* table mappings (incl. shared zero folios) are marked +* accordingly. */ }else {if (unlikely (vma->vm_flags & (VM_PFNMAP |VM_MIXEDMAP ))) { ---base-commit:
d94322006a51b522dd361128a450bf9e75aad889change-id: 20260430-pmd_special-610dbdd8ac3c
--
Cheers,
David
David Hildenbrand (Arm)
maintainerQuoted 7 lines · Lorenzo Stoakes
Quoted 6 lines · David Hildenbrand (Arm)
On x86 32-bit with THP enabled,
zap_huge_pmd()is seen to generate a "WARNING:mm/memory.c:735at__vm_normal_page+0x6a/0x7d", from theVM_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 callshrink_huge_zero_folio_scan().
Quoted 9 lines
Quoted 5 lines
It's as if the
_PAGE_SPECIALbit never got set in thehuge_zeropmd:
and indeed, whereaspte_special()andpte_mkspecial()are subject to a dedicatedCONFIG_ARCH_HAS_PTE_SPECIAL,pmd_special()andpmd_mkspecial()are subject toCONFIG_ARCH_SUPPORTS_PMD_PFNMAP, which is never enabled on any 32-bit architecture.Ah damn. I wonder if it's really a combination of 'supports THP' and 'has a spare software defined bit free in PTE'?
I think it's mostly "we have a spare bit (pte_special() implemented), but we didn't wire up pmd_special()"
Thanks!
Signature · 3 lines
Cheers, David
Quoted 26 lines · David Hildenbrand (Arm)
On x86 32-bit with THP enabled,
zap_huge_pmd()is seen to generate a "WARNING:mm/memory.c:735at__vm_normal_page+0x6a/0x7d", from theVM_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 callshrink_huge_zero_folio_scan().It's as if the
_PAGE_SPECIALbit never got set in thehuge_zeropmd:
and indeed, whereaspte_special()andpte_mkspecial()are subject to a dedicatedCONFIG_ARCH_HAS_PTE_SPECIAL,pmd_special()andpmd_mkspecial()are subject toCONFIG_ARCH_SUPPORTS_PMD_PFNMAP, which is never enabled on any 32-bit architecture.While the problem was exposed through commit
d80a9cb1a64a("mm/huge_memory:
add and usenormal_or_softleaf_folio_pmd()"), it was an oversight in commitaf38538801c6("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 withoutFW_ZEROPAGEset. 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.
Quoted 10 lines
Fixes af38538801c6 ("mm/memory: factor out common code from vm_normal_page_*()")Reported-by Hugh Dickins <[email protected]>Reported-by Bibo Mao <[email protected]>Debugged-by Hugh Dickins <[email protected]>Reviewed-by Lance Yang <[email protected]>Tested-by Bibo Mao <[email protected]>Signed-off-by David Hildenbrand (Arm) <[email protected]>
Signature · 2 lines
Oscar Salvador SUSE Labs
Quoted 36 lines · David Hildenbrand (Arm)
On x86 32-bit with THP enabled,
zap_huge_pmd()is seen to generate a "WARNING:mm/memory.c:735at__vm_normal_page+0x6a/0x7d", from theVM_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 callshrink_huge_zero_folio_scan().It's as if the
_PAGE_SPECIALbit never got set in thehuge_zeropmd:
and indeed, whereaspte_special()andpte_mkspecial()are subject to a dedicatedCONFIG_ARCH_HAS_PTE_SPECIAL,pmd_special()andpmd_mkspecial()are subject toCONFIG_ARCH_SUPPORTS_PMD_PFNMAP, which is never enabled on any 32-bit architecture.While the problem was exposed through commit
d80a9cb1a64a("mm/huge_memory:
add and usenormal_or_softleaf_folio_pmd()"), it was an oversight in commitaf38538801c6("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 withoutFW_ZEROPAGEset. 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.
Fixes af38538801c6 ("mm/memory: factor out common code from vm_normal_page_*()")Reported-by Hugh Dickins <[email protected]>Reported-by Bibo Mao <[email protected]>Debugged-by Hugh Dickins <[email protected]>Reviewed-by Lance Yang <[email protected]>Tested-by Bibo Mao <[email protected]>Signed-off-by David Hildenbrand (Arm) <[email protected]>
Quoted 1 line
---
LGTM. Feel free to add: