pmdp_invalidate() replaces a huge PMD with an invalidated one (_PAGE_PRESENT cleared and _PAGE_INVALID set) and calls page_table_check_pmd_clear() on the old entry. This decreases the page table check map count. When the invalidated entry is later replaced with set_pmd_at(), page_table_check_pmds_set() in set_pmd_at() decreases the map count again because pmd_user_accessible_page() still counts it as a live user mapping. The same happens when it is cleared with pmdp_huge_get_and_clear(). Then the map count underflows and hits BUG_ON() in page_table_check_clear(). Found by code inspection and confirmed in QEMU (pseries, POWER9, 64K pages, CONFIG_PAGE_TABLE_CHECK_ENFORCED=y): MADV_COLD (before v7.4) or mprotect() on an anonymous THP hits the BUG_ON() with both the radix and the hash MMU. Fix it by only counting entries that are hardware valid, so an invalidated entry is skipped, as commit bdda2887cbf39 ("riscv: mm: exclude invalid THP PMDs from page table check") did for riscv. While at it, remove the migrate_vma_collect_huge_pmd() item from the comment in pmdp_huge_get_and_clear(): set_pmd_migration_entry() no longer clears device private PMDs. Fixes: 641d47d4c963 ("powerpc/mm: support page table check") Cc: stable@vger.kernel.org To: Madhavan Srinivasan To: Andrew Donnellan To: Rohan McLure To: Pasha Tatashin To: Andrew Morton Cc: Michael Ellerman Cc: Nicholas Piggin Cc: "Christophe Leroy (CS GROUP)" Cc: "Ritesh Harjani (IBM)" Cc: Shrikanth Hegde Cc: linuxppc-dev@lists.ozlabs.org Cc: linux-kernel@vger.kernel.org Acked-by: Usama Arif Signed-off-by: Zi Yan --- arch/powerpc/include/asm/book3s/64/pgtable.h | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/arch/powerpc/include/asm/book3s/64/pgtable.h b/arch/powerpc/include/asm/book3s/64/pgtable.h index dff8790a047db..d3808c3560703 100644 --- a/arch/powerpc/include/asm/book3s/64/pgtable.h +++ b/arch/powerpc/include/asm/book3s/64/pgtable.h @@ -1091,7 +1091,8 @@ static inline bool pmd_access_permitted(pmd_t pmd, bool write) #define pmd_user_accessible_page pmd_user_accessible_page static inline bool pmd_user_accessible_page(struct mm_struct *mm, unsigned long addr, pmd_t pmd) { - return pmd_leaf(pmd) && pte_user_accessible_page(mm, addr, pmd_pte(pmd)); + return pmd_leaf(pmd) && pte_hw_valid(pmd_pte(pmd)) && + pte_user_accessible_page(mm, addr, pmd_pte(pmd)); } #ifdef CONFIG_TRANSPARENT_HUGEPAGE @@ -1331,11 +1332,9 @@ static inline pmd_t pmdp_huge_get_and_clear(struct mm_struct *mm, /* * Non-present PMDs can be migration entries or device-private THP - * entries. This can happen at 2 places: + * entries. This can happen at: * - When the address space is being unmapped zap_huge_pmd(), and we * encounter non-present pmds. - * - migrate_vma_collect_huge_pmd() could calls this during migration - * of device-private pmd entries. */ if (!pmd_present(*pmdp)) { old_pmd = READ_ONCE(*pmdp); -- 2.53.0