1399145f9SAnshuman Khandual // SPDX-License-Identifier: GPL-2.0-only 2399145f9SAnshuman Khandual /* 3399145f9SAnshuman Khandual * This kernel test validates architecture page table helpers and 4399145f9SAnshuman Khandual * accessors and helps in verifying their continued compliance with 5399145f9SAnshuman Khandual * expected generic MM semantics. 6399145f9SAnshuman Khandual * 7399145f9SAnshuman Khandual * Copyright (C) 2019 ARM Ltd. 8399145f9SAnshuman Khandual * 9399145f9SAnshuman Khandual * Author: Anshuman Khandual <anshuman.khandual@arm.com> 10399145f9SAnshuman Khandual */ 116315df41SAnshuman Khandual #define pr_fmt(fmt) "debug_vm_pgtable: [%-25s]: " fmt, __func__ 12399145f9SAnshuman Khandual 13399145f9SAnshuman Khandual #include <linux/gfp.h> 14399145f9SAnshuman Khandual #include <linux/highmem.h> 15399145f9SAnshuman Khandual #include <linux/hugetlb.h> 16399145f9SAnshuman Khandual #include <linux/kernel.h> 17399145f9SAnshuman Khandual #include <linux/kconfig.h> 18399145f9SAnshuman Khandual #include <linux/mm.h> 19399145f9SAnshuman Khandual #include <linux/mman.h> 20399145f9SAnshuman Khandual #include <linux/mm_types.h> 21399145f9SAnshuman Khandual #include <linux/module.h> 22399145f9SAnshuman Khandual #include <linux/pfn_t.h> 23399145f9SAnshuman Khandual #include <linux/printk.h> 24a5c3b9ffSAnshuman Khandual #include <linux/pgtable.h> 25399145f9SAnshuman Khandual #include <linux/random.h> 26399145f9SAnshuman Khandual #include <linux/spinlock.h> 27399145f9SAnshuman Khandual #include <linux/swap.h> 28399145f9SAnshuman Khandual #include <linux/swapops.h> 29399145f9SAnshuman Khandual #include <linux/start_kernel.h> 30399145f9SAnshuman Khandual #include <linux/sched/mm.h> 3185a14463SAneesh Kumar K.V #include <linux/io.h> 32399145f9SAnshuman Khandual #include <asm/pgalloc.h> 33a5c3b9ffSAnshuman Khandual #include <asm/tlbflush.h> 34399145f9SAnshuman Khandual 35b1d00007SAnshuman Khandual /* 36b1d00007SAnshuman Khandual * Please refer Documentation/vm/arch_pgtable_helpers.rst for the semantics 37b1d00007SAnshuman Khandual * expectations that are being validated here. All future changes in here 38b1d00007SAnshuman Khandual * or the documentation need to be in sync. 39b1d00007SAnshuman Khandual */ 40b1d00007SAnshuman Khandual 41399145f9SAnshuman Khandual #define VMFLAGS (VM_READ|VM_WRITE|VM_EXEC) 42399145f9SAnshuman Khandual 43399145f9SAnshuman Khandual /* 44399145f9SAnshuman Khandual * On s390 platform, the lower 4 bits are used to identify given page table 45399145f9SAnshuman Khandual * entry type. But these bits might affect the ability to clear entries with 46399145f9SAnshuman Khandual * pxx_clear() because of how dynamic page table folding works on s390. So 47399145f9SAnshuman Khandual * while loading up the entries do not change the lower 4 bits. It does not 48cfc5bbc4SAneesh Kumar K.V * have affect any other platform. Also avoid the 62nd bit on ppc64 that is 49cfc5bbc4SAneesh Kumar K.V * used to mark a pte entry. 50399145f9SAnshuman Khandual */ 51cfc5bbc4SAneesh Kumar K.V #define S390_SKIP_MASK GENMASK(3, 0) 52cfc5bbc4SAneesh Kumar K.V #if __BITS_PER_LONG == 64 53cfc5bbc4SAneesh Kumar K.V #define PPC64_SKIP_MASK GENMASK(62, 62) 54cfc5bbc4SAneesh Kumar K.V #else 55cfc5bbc4SAneesh Kumar K.V #define PPC64_SKIP_MASK 0x0 56cfc5bbc4SAneesh Kumar K.V #endif 57cfc5bbc4SAneesh Kumar K.V #define ARCH_SKIP_MASK (S390_SKIP_MASK | PPC64_SKIP_MASK) 58cfc5bbc4SAneesh Kumar K.V #define RANDOM_ORVALUE (GENMASK(BITS_PER_LONG - 1, 0) & ~ARCH_SKIP_MASK) 59399145f9SAnshuman Khandual #define RANDOM_NZVALUE GENMASK(7, 0) 60399145f9SAnshuman Khandual 61399145f9SAnshuman Khandual static void __init pte_basic_tests(unsigned long pfn, pgprot_t prot) 62399145f9SAnshuman Khandual { 63399145f9SAnshuman Khandual pte_t pte = pfn_pte(pfn, prot); 64399145f9SAnshuman Khandual 656315df41SAnshuman Khandual pr_debug("Validating PTE basic\n"); 66399145f9SAnshuman Khandual WARN_ON(!pte_same(pte, pte)); 67399145f9SAnshuman Khandual WARN_ON(!pte_young(pte_mkyoung(pte_mkold(pte)))); 68399145f9SAnshuman Khandual WARN_ON(!pte_dirty(pte_mkdirty(pte_mkclean(pte)))); 69399145f9SAnshuman Khandual WARN_ON(!pte_write(pte_mkwrite(pte_wrprotect(pte)))); 70399145f9SAnshuman Khandual WARN_ON(pte_young(pte_mkold(pte_mkyoung(pte)))); 71399145f9SAnshuman Khandual WARN_ON(pte_dirty(pte_mkclean(pte_mkdirty(pte)))); 72399145f9SAnshuman Khandual WARN_ON(pte_write(pte_wrprotect(pte_mkwrite(pte)))); 73399145f9SAnshuman Khandual } 74399145f9SAnshuman Khandual 75a5c3b9ffSAnshuman Khandual static void __init pte_advanced_tests(struct mm_struct *mm, 76a5c3b9ffSAnshuman Khandual struct vm_area_struct *vma, pte_t *ptep, 77a5c3b9ffSAnshuman Khandual unsigned long pfn, unsigned long vaddr, 78a5c3b9ffSAnshuman Khandual pgprot_t prot) 79a5c3b9ffSAnshuman Khandual { 80a5c3b9ffSAnshuman Khandual pte_t pte = pfn_pte(pfn, prot); 81a5c3b9ffSAnshuman Khandual 82c3824e18SAneesh Kumar K.V /* 83c3824e18SAneesh Kumar K.V * Architectures optimize set_pte_at by avoiding TLB flush. 84c3824e18SAneesh Kumar K.V * This requires set_pte_at to be not used to update an 85c3824e18SAneesh Kumar K.V * existing pte entry. Clear pte before we do set_pte_at 86c3824e18SAneesh Kumar K.V */ 87c3824e18SAneesh Kumar K.V 886315df41SAnshuman Khandual pr_debug("Validating PTE advanced\n"); 89a5c3b9ffSAnshuman Khandual pte = pfn_pte(pfn, prot); 90a5c3b9ffSAnshuman Khandual set_pte_at(mm, vaddr, ptep, pte); 91a5c3b9ffSAnshuman Khandual ptep_set_wrprotect(mm, vaddr, ptep); 92a5c3b9ffSAnshuman Khandual pte = ptep_get(ptep); 93a5c3b9ffSAnshuman Khandual WARN_ON(pte_write(pte)); 94a5c3b9ffSAnshuman Khandual ptep_get_and_clear(mm, vaddr, ptep); 95a5c3b9ffSAnshuman Khandual pte = ptep_get(ptep); 96a5c3b9ffSAnshuman Khandual WARN_ON(!pte_none(pte)); 97a5c3b9ffSAnshuman Khandual 98a5c3b9ffSAnshuman Khandual pte = pfn_pte(pfn, prot); 99a5c3b9ffSAnshuman Khandual pte = pte_wrprotect(pte); 100a5c3b9ffSAnshuman Khandual pte = pte_mkclean(pte); 101a5c3b9ffSAnshuman Khandual set_pte_at(mm, vaddr, ptep, pte); 102a5c3b9ffSAnshuman Khandual pte = pte_mkwrite(pte); 103a5c3b9ffSAnshuman Khandual pte = pte_mkdirty(pte); 104a5c3b9ffSAnshuman Khandual ptep_set_access_flags(vma, vaddr, ptep, pte, 1); 105a5c3b9ffSAnshuman Khandual pte = ptep_get(ptep); 106a5c3b9ffSAnshuman Khandual WARN_ON(!(pte_write(pte) && pte_dirty(pte))); 107a5c3b9ffSAnshuman Khandual ptep_get_and_clear_full(mm, vaddr, ptep, 1); 108a5c3b9ffSAnshuman Khandual pte = ptep_get(ptep); 109a5c3b9ffSAnshuman Khandual WARN_ON(!pte_none(pte)); 110a5c3b9ffSAnshuman Khandual 111c3824e18SAneesh Kumar K.V pte = pfn_pte(pfn, prot); 112a5c3b9ffSAnshuman Khandual pte = pte_mkyoung(pte); 113a5c3b9ffSAnshuman Khandual set_pte_at(mm, vaddr, ptep, pte); 114a5c3b9ffSAnshuman Khandual ptep_test_and_clear_young(vma, vaddr, ptep); 115a5c3b9ffSAnshuman Khandual pte = ptep_get(ptep); 116a5c3b9ffSAnshuman Khandual WARN_ON(pte_young(pte)); 117a5c3b9ffSAnshuman Khandual } 118a5c3b9ffSAnshuman Khandual 119a5c3b9ffSAnshuman Khandual static void __init pte_savedwrite_tests(unsigned long pfn, pgprot_t prot) 120a5c3b9ffSAnshuman Khandual { 121a5c3b9ffSAnshuman Khandual pte_t pte = pfn_pte(pfn, prot); 122a5c3b9ffSAnshuman Khandual 1234200605bSAneesh Kumar K.V if (!IS_ENABLED(CONFIG_NUMA_BALANCING)) 1244200605bSAneesh Kumar K.V return; 1254200605bSAneesh Kumar K.V 1266315df41SAnshuman Khandual pr_debug("Validating PTE saved write\n"); 127a5c3b9ffSAnshuman Khandual WARN_ON(!pte_savedwrite(pte_mk_savedwrite(pte_clear_savedwrite(pte)))); 128a5c3b9ffSAnshuman Khandual WARN_ON(pte_savedwrite(pte_clear_savedwrite(pte_mk_savedwrite(pte)))); 129a5c3b9ffSAnshuman Khandual } 1304200605bSAneesh Kumar K.V 131399145f9SAnshuman Khandual #ifdef CONFIG_TRANSPARENT_HUGEPAGE 132399145f9SAnshuman Khandual static void __init pmd_basic_tests(unsigned long pfn, pgprot_t prot) 133399145f9SAnshuman Khandual { 134399145f9SAnshuman Khandual pmd_t pmd = pfn_pmd(pfn, prot); 135399145f9SAnshuman Khandual 136787d563bSAneesh Kumar K.V if (!has_transparent_hugepage()) 137787d563bSAneesh Kumar K.V return; 138787d563bSAneesh Kumar K.V 1396315df41SAnshuman Khandual pr_debug("Validating PMD basic\n"); 140399145f9SAnshuman Khandual WARN_ON(!pmd_same(pmd, pmd)); 141399145f9SAnshuman Khandual WARN_ON(!pmd_young(pmd_mkyoung(pmd_mkold(pmd)))); 142399145f9SAnshuman Khandual WARN_ON(!pmd_dirty(pmd_mkdirty(pmd_mkclean(pmd)))); 143399145f9SAnshuman Khandual WARN_ON(!pmd_write(pmd_mkwrite(pmd_wrprotect(pmd)))); 144399145f9SAnshuman Khandual WARN_ON(pmd_young(pmd_mkold(pmd_mkyoung(pmd)))); 145399145f9SAnshuman Khandual WARN_ON(pmd_dirty(pmd_mkclean(pmd_mkdirty(pmd)))); 146399145f9SAnshuman Khandual WARN_ON(pmd_write(pmd_wrprotect(pmd_mkwrite(pmd)))); 147399145f9SAnshuman Khandual /* 148399145f9SAnshuman Khandual * A huge page does not point to next level page table 149399145f9SAnshuman Khandual * entry. Hence this must qualify as pmd_bad(). 150399145f9SAnshuman Khandual */ 151399145f9SAnshuman Khandual WARN_ON(!pmd_bad(pmd_mkhuge(pmd))); 152399145f9SAnshuman Khandual } 153399145f9SAnshuman Khandual 154a5c3b9ffSAnshuman Khandual static void __init pmd_advanced_tests(struct mm_struct *mm, 155a5c3b9ffSAnshuman Khandual struct vm_area_struct *vma, pmd_t *pmdp, 156a5c3b9ffSAnshuman Khandual unsigned long pfn, unsigned long vaddr, 157*87f34986SAneesh Kumar K.V pgprot_t prot, pgtable_t pgtable) 158a5c3b9ffSAnshuman Khandual { 159a5c3b9ffSAnshuman Khandual pmd_t pmd = pfn_pmd(pfn, prot); 160a5c3b9ffSAnshuman Khandual 161a5c3b9ffSAnshuman Khandual if (!has_transparent_hugepage()) 162a5c3b9ffSAnshuman Khandual return; 163a5c3b9ffSAnshuman Khandual 1646315df41SAnshuman Khandual pr_debug("Validating PMD advanced\n"); 165a5c3b9ffSAnshuman Khandual /* Align the address wrt HPAGE_PMD_SIZE */ 166a5c3b9ffSAnshuman Khandual vaddr = (vaddr & HPAGE_PMD_MASK) + HPAGE_PMD_SIZE; 167a5c3b9ffSAnshuman Khandual 168*87f34986SAneesh Kumar K.V pgtable_trans_huge_deposit(mm, pmdp, pgtable); 169*87f34986SAneesh Kumar K.V 170a5c3b9ffSAnshuman Khandual pmd = pfn_pmd(pfn, prot); 171a5c3b9ffSAnshuman Khandual set_pmd_at(mm, vaddr, pmdp, pmd); 172a5c3b9ffSAnshuman Khandual pmdp_set_wrprotect(mm, vaddr, pmdp); 173a5c3b9ffSAnshuman Khandual pmd = READ_ONCE(*pmdp); 174a5c3b9ffSAnshuman Khandual WARN_ON(pmd_write(pmd)); 175a5c3b9ffSAnshuman Khandual pmdp_huge_get_and_clear(mm, vaddr, pmdp); 176a5c3b9ffSAnshuman Khandual pmd = READ_ONCE(*pmdp); 177a5c3b9ffSAnshuman Khandual WARN_ON(!pmd_none(pmd)); 178a5c3b9ffSAnshuman Khandual 179a5c3b9ffSAnshuman Khandual pmd = pfn_pmd(pfn, prot); 180a5c3b9ffSAnshuman Khandual pmd = pmd_wrprotect(pmd); 181a5c3b9ffSAnshuman Khandual pmd = pmd_mkclean(pmd); 182a5c3b9ffSAnshuman Khandual set_pmd_at(mm, vaddr, pmdp, pmd); 183a5c3b9ffSAnshuman Khandual pmd = pmd_mkwrite(pmd); 184a5c3b9ffSAnshuman Khandual pmd = pmd_mkdirty(pmd); 185a5c3b9ffSAnshuman Khandual pmdp_set_access_flags(vma, vaddr, pmdp, pmd, 1); 186a5c3b9ffSAnshuman Khandual pmd = READ_ONCE(*pmdp); 187a5c3b9ffSAnshuman Khandual WARN_ON(!(pmd_write(pmd) && pmd_dirty(pmd))); 188a5c3b9ffSAnshuman Khandual pmdp_huge_get_and_clear_full(vma, vaddr, pmdp, 1); 189a5c3b9ffSAnshuman Khandual pmd = READ_ONCE(*pmdp); 190a5c3b9ffSAnshuman Khandual WARN_ON(!pmd_none(pmd)); 191a5c3b9ffSAnshuman Khandual 192c3824e18SAneesh Kumar K.V pmd = pmd_mkhuge(pfn_pmd(pfn, prot)); 193a5c3b9ffSAnshuman Khandual pmd = pmd_mkyoung(pmd); 194a5c3b9ffSAnshuman Khandual set_pmd_at(mm, vaddr, pmdp, pmd); 195a5c3b9ffSAnshuman Khandual pmdp_test_and_clear_young(vma, vaddr, pmdp); 196a5c3b9ffSAnshuman Khandual pmd = READ_ONCE(*pmdp); 197a5c3b9ffSAnshuman Khandual WARN_ON(pmd_young(pmd)); 198*87f34986SAneesh Kumar K.V 199*87f34986SAneesh Kumar K.V pgtable = pgtable_trans_huge_withdraw(mm, pmdp); 200a5c3b9ffSAnshuman Khandual } 201a5c3b9ffSAnshuman Khandual 202a5c3b9ffSAnshuman Khandual static void __init pmd_leaf_tests(unsigned long pfn, pgprot_t prot) 203a5c3b9ffSAnshuman Khandual { 204a5c3b9ffSAnshuman Khandual pmd_t pmd = pfn_pmd(pfn, prot); 205a5c3b9ffSAnshuman Khandual 2066315df41SAnshuman Khandual pr_debug("Validating PMD leaf\n"); 207a5c3b9ffSAnshuman Khandual /* 208a5c3b9ffSAnshuman Khandual * PMD based THP is a leaf entry. 209a5c3b9ffSAnshuman Khandual */ 210a5c3b9ffSAnshuman Khandual pmd = pmd_mkhuge(pmd); 211a5c3b9ffSAnshuman Khandual WARN_ON(!pmd_leaf(pmd)); 212a5c3b9ffSAnshuman Khandual } 213a5c3b9ffSAnshuman Khandual 21485a14463SAneesh Kumar K.V #ifdef CONFIG_HAVE_ARCH_HUGE_VMAP 215a5c3b9ffSAnshuman Khandual static void __init pmd_huge_tests(pmd_t *pmdp, unsigned long pfn, pgprot_t prot) 216a5c3b9ffSAnshuman Khandual { 217a5c3b9ffSAnshuman Khandual pmd_t pmd; 218a5c3b9ffSAnshuman Khandual 21985a14463SAneesh Kumar K.V if (!arch_ioremap_pmd_supported()) 220a5c3b9ffSAnshuman Khandual return; 2216315df41SAnshuman Khandual 2226315df41SAnshuman Khandual pr_debug("Validating PMD huge\n"); 223a5c3b9ffSAnshuman Khandual /* 224a5c3b9ffSAnshuman Khandual * X86 defined pmd_set_huge() verifies that the given 225a5c3b9ffSAnshuman Khandual * PMD is not a populated non-leaf entry. 226a5c3b9ffSAnshuman Khandual */ 227a5c3b9ffSAnshuman Khandual WRITE_ONCE(*pmdp, __pmd(0)); 228a5c3b9ffSAnshuman Khandual WARN_ON(!pmd_set_huge(pmdp, __pfn_to_phys(pfn), prot)); 229a5c3b9ffSAnshuman Khandual WARN_ON(!pmd_clear_huge(pmdp)); 230a5c3b9ffSAnshuman Khandual pmd = READ_ONCE(*pmdp); 231a5c3b9ffSAnshuman Khandual WARN_ON(!pmd_none(pmd)); 232a5c3b9ffSAnshuman Khandual } 23385a14463SAneesh Kumar K.V #else /* CONFIG_HAVE_ARCH_HUGE_VMAP */ 23485a14463SAneesh Kumar K.V static void __init pmd_huge_tests(pmd_t *pmdp, unsigned long pfn, pgprot_t prot) { } 23585a14463SAneesh Kumar K.V #endif /* CONFIG_HAVE_ARCH_HUGE_VMAP */ 236a5c3b9ffSAnshuman Khandual 237a5c3b9ffSAnshuman Khandual static void __init pmd_savedwrite_tests(unsigned long pfn, pgprot_t prot) 238a5c3b9ffSAnshuman Khandual { 239a5c3b9ffSAnshuman Khandual pmd_t pmd = pfn_pmd(pfn, prot); 240a5c3b9ffSAnshuman Khandual 2414200605bSAneesh Kumar K.V if (!IS_ENABLED(CONFIG_NUMA_BALANCING)) 2424200605bSAneesh Kumar K.V return; 2434200605bSAneesh Kumar K.V 2446315df41SAnshuman Khandual pr_debug("Validating PMD saved write\n"); 245a5c3b9ffSAnshuman Khandual WARN_ON(!pmd_savedwrite(pmd_mk_savedwrite(pmd_clear_savedwrite(pmd)))); 246a5c3b9ffSAnshuman Khandual WARN_ON(pmd_savedwrite(pmd_clear_savedwrite(pmd_mk_savedwrite(pmd)))); 247a5c3b9ffSAnshuman Khandual } 248a5c3b9ffSAnshuman Khandual 249399145f9SAnshuman Khandual #ifdef CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD 250399145f9SAnshuman Khandual static void __init pud_basic_tests(unsigned long pfn, pgprot_t prot) 251399145f9SAnshuman Khandual { 252399145f9SAnshuman Khandual pud_t pud = pfn_pud(pfn, prot); 253399145f9SAnshuman Khandual 254787d563bSAneesh Kumar K.V if (!has_transparent_hugepage()) 255787d563bSAneesh Kumar K.V return; 256787d563bSAneesh Kumar K.V 2576315df41SAnshuman Khandual pr_debug("Validating PUD basic\n"); 258399145f9SAnshuman Khandual WARN_ON(!pud_same(pud, pud)); 259399145f9SAnshuman Khandual WARN_ON(!pud_young(pud_mkyoung(pud_mkold(pud)))); 260399145f9SAnshuman Khandual WARN_ON(!pud_write(pud_mkwrite(pud_wrprotect(pud)))); 261399145f9SAnshuman Khandual WARN_ON(pud_write(pud_wrprotect(pud_mkwrite(pud)))); 262399145f9SAnshuman Khandual WARN_ON(pud_young(pud_mkold(pud_mkyoung(pud)))); 263399145f9SAnshuman Khandual 264399145f9SAnshuman Khandual if (mm_pmd_folded(mm)) 265399145f9SAnshuman Khandual return; 266399145f9SAnshuman Khandual 267399145f9SAnshuman Khandual /* 268399145f9SAnshuman Khandual * A huge page does not point to next level page table 269399145f9SAnshuman Khandual * entry. Hence this must qualify as pud_bad(). 270399145f9SAnshuman Khandual */ 271399145f9SAnshuman Khandual WARN_ON(!pud_bad(pud_mkhuge(pud))); 272399145f9SAnshuman Khandual } 273a5c3b9ffSAnshuman Khandual 274a5c3b9ffSAnshuman Khandual static void __init pud_advanced_tests(struct mm_struct *mm, 275a5c3b9ffSAnshuman Khandual struct vm_area_struct *vma, pud_t *pudp, 276a5c3b9ffSAnshuman Khandual unsigned long pfn, unsigned long vaddr, 277a5c3b9ffSAnshuman Khandual pgprot_t prot) 278a5c3b9ffSAnshuman Khandual { 279a5c3b9ffSAnshuman Khandual pud_t pud = pfn_pud(pfn, prot); 280a5c3b9ffSAnshuman Khandual 281a5c3b9ffSAnshuman Khandual if (!has_transparent_hugepage()) 282a5c3b9ffSAnshuman Khandual return; 283a5c3b9ffSAnshuman Khandual 2846315df41SAnshuman Khandual pr_debug("Validating PUD advanced\n"); 285a5c3b9ffSAnshuman Khandual /* Align the address wrt HPAGE_PUD_SIZE */ 286a5c3b9ffSAnshuman Khandual vaddr = (vaddr & HPAGE_PUD_MASK) + HPAGE_PUD_SIZE; 287a5c3b9ffSAnshuman Khandual 288a5c3b9ffSAnshuman Khandual set_pud_at(mm, vaddr, pudp, pud); 289a5c3b9ffSAnshuman Khandual pudp_set_wrprotect(mm, vaddr, pudp); 290a5c3b9ffSAnshuman Khandual pud = READ_ONCE(*pudp); 291a5c3b9ffSAnshuman Khandual WARN_ON(pud_write(pud)); 292a5c3b9ffSAnshuman Khandual 293a5c3b9ffSAnshuman Khandual #ifndef __PAGETABLE_PMD_FOLDED 294a5c3b9ffSAnshuman Khandual pudp_huge_get_and_clear(mm, vaddr, pudp); 295a5c3b9ffSAnshuman Khandual pud = READ_ONCE(*pudp); 296a5c3b9ffSAnshuman Khandual WARN_ON(!pud_none(pud)); 297a5c3b9ffSAnshuman Khandual #endif /* __PAGETABLE_PMD_FOLDED */ 298a5c3b9ffSAnshuman Khandual pud = pfn_pud(pfn, prot); 299a5c3b9ffSAnshuman Khandual pud = pud_wrprotect(pud); 300a5c3b9ffSAnshuman Khandual pud = pud_mkclean(pud); 301a5c3b9ffSAnshuman Khandual set_pud_at(mm, vaddr, pudp, pud); 302a5c3b9ffSAnshuman Khandual pud = pud_mkwrite(pud); 303a5c3b9ffSAnshuman Khandual pud = pud_mkdirty(pud); 304a5c3b9ffSAnshuman Khandual pudp_set_access_flags(vma, vaddr, pudp, pud, 1); 305a5c3b9ffSAnshuman Khandual pud = READ_ONCE(*pudp); 306a5c3b9ffSAnshuman Khandual WARN_ON(!(pud_write(pud) && pud_dirty(pud))); 307a5c3b9ffSAnshuman Khandual 308c3824e18SAneesh Kumar K.V #ifndef __PAGETABLE_PMD_FOLDED 309c3824e18SAneesh Kumar K.V pudp_huge_get_and_clear_full(mm, vaddr, pudp, 1); 310c3824e18SAneesh Kumar K.V pud = READ_ONCE(*pudp); 311c3824e18SAneesh Kumar K.V WARN_ON(!pud_none(pud)); 312c3824e18SAneesh Kumar K.V #endif /* __PAGETABLE_PMD_FOLDED */ 313c3824e18SAneesh Kumar K.V 314c3824e18SAneesh Kumar K.V pud = pfn_pud(pfn, prot); 315a5c3b9ffSAnshuman Khandual pud = pud_mkyoung(pud); 316a5c3b9ffSAnshuman Khandual set_pud_at(mm, vaddr, pudp, pud); 317a5c3b9ffSAnshuman Khandual pudp_test_and_clear_young(vma, vaddr, pudp); 318a5c3b9ffSAnshuman Khandual pud = READ_ONCE(*pudp); 319a5c3b9ffSAnshuman Khandual WARN_ON(pud_young(pud)); 320a5c3b9ffSAnshuman Khandual } 321a5c3b9ffSAnshuman Khandual 322a5c3b9ffSAnshuman Khandual static void __init pud_leaf_tests(unsigned long pfn, pgprot_t prot) 323a5c3b9ffSAnshuman Khandual { 324a5c3b9ffSAnshuman Khandual pud_t pud = pfn_pud(pfn, prot); 325a5c3b9ffSAnshuman Khandual 3266315df41SAnshuman Khandual pr_debug("Validating PUD leaf\n"); 327a5c3b9ffSAnshuman Khandual /* 328a5c3b9ffSAnshuman Khandual * PUD based THP is a leaf entry. 329a5c3b9ffSAnshuman Khandual */ 330a5c3b9ffSAnshuman Khandual pud = pud_mkhuge(pud); 331a5c3b9ffSAnshuman Khandual WARN_ON(!pud_leaf(pud)); 332a5c3b9ffSAnshuman Khandual } 333a5c3b9ffSAnshuman Khandual 33485a14463SAneesh Kumar K.V #ifdef CONFIG_HAVE_ARCH_HUGE_VMAP 335a5c3b9ffSAnshuman Khandual static void __init pud_huge_tests(pud_t *pudp, unsigned long pfn, pgprot_t prot) 336a5c3b9ffSAnshuman Khandual { 337a5c3b9ffSAnshuman Khandual pud_t pud; 338a5c3b9ffSAnshuman Khandual 33985a14463SAneesh Kumar K.V if (!arch_ioremap_pud_supported()) 340a5c3b9ffSAnshuman Khandual return; 3416315df41SAnshuman Khandual 3426315df41SAnshuman Khandual pr_debug("Validating PUD huge\n"); 343a5c3b9ffSAnshuman Khandual /* 344a5c3b9ffSAnshuman Khandual * X86 defined pud_set_huge() verifies that the given 345a5c3b9ffSAnshuman Khandual * PUD is not a populated non-leaf entry. 346a5c3b9ffSAnshuman Khandual */ 347a5c3b9ffSAnshuman Khandual WRITE_ONCE(*pudp, __pud(0)); 348a5c3b9ffSAnshuman Khandual WARN_ON(!pud_set_huge(pudp, __pfn_to_phys(pfn), prot)); 349a5c3b9ffSAnshuman Khandual WARN_ON(!pud_clear_huge(pudp)); 350a5c3b9ffSAnshuman Khandual pud = READ_ONCE(*pudp); 351a5c3b9ffSAnshuman Khandual WARN_ON(!pud_none(pud)); 352a5c3b9ffSAnshuman Khandual } 35385a14463SAneesh Kumar K.V #else /* !CONFIG_HAVE_ARCH_HUGE_VMAP */ 35485a14463SAneesh Kumar K.V static void __init pud_huge_tests(pud_t *pudp, unsigned long pfn, pgprot_t prot) { } 35585a14463SAneesh Kumar K.V #endif /* !CONFIG_HAVE_ARCH_HUGE_VMAP */ 35685a14463SAneesh Kumar K.V 357399145f9SAnshuman Khandual #else /* !CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD */ 358399145f9SAnshuman Khandual static void __init pud_basic_tests(unsigned long pfn, pgprot_t prot) { } 359a5c3b9ffSAnshuman Khandual static void __init pud_advanced_tests(struct mm_struct *mm, 360a5c3b9ffSAnshuman Khandual struct vm_area_struct *vma, pud_t *pudp, 361a5c3b9ffSAnshuman Khandual unsigned long pfn, unsigned long vaddr, 362a5c3b9ffSAnshuman Khandual pgprot_t prot) 363a5c3b9ffSAnshuman Khandual { 364a5c3b9ffSAnshuman Khandual } 365a5c3b9ffSAnshuman Khandual static void __init pud_leaf_tests(unsigned long pfn, pgprot_t prot) { } 366a5c3b9ffSAnshuman Khandual static void __init pud_huge_tests(pud_t *pudp, unsigned long pfn, pgprot_t prot) 367a5c3b9ffSAnshuman Khandual { 368a5c3b9ffSAnshuman Khandual } 369399145f9SAnshuman Khandual #endif /* CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD */ 370399145f9SAnshuman Khandual #else /* !CONFIG_TRANSPARENT_HUGEPAGE */ 371399145f9SAnshuman Khandual static void __init pmd_basic_tests(unsigned long pfn, pgprot_t prot) { } 372399145f9SAnshuman Khandual static void __init pud_basic_tests(unsigned long pfn, pgprot_t prot) { } 373a5c3b9ffSAnshuman Khandual static void __init pmd_advanced_tests(struct mm_struct *mm, 374a5c3b9ffSAnshuman Khandual struct vm_area_struct *vma, pmd_t *pmdp, 375a5c3b9ffSAnshuman Khandual unsigned long pfn, unsigned long vaddr, 376*87f34986SAneesh Kumar K.V pgprot_t prot, pgtable_t pgtable) 377a5c3b9ffSAnshuman Khandual { 378a5c3b9ffSAnshuman Khandual } 379a5c3b9ffSAnshuman Khandual static void __init pud_advanced_tests(struct mm_struct *mm, 380a5c3b9ffSAnshuman Khandual struct vm_area_struct *vma, pud_t *pudp, 381a5c3b9ffSAnshuman Khandual unsigned long pfn, unsigned long vaddr, 382a5c3b9ffSAnshuman Khandual pgprot_t prot) 383a5c3b9ffSAnshuman Khandual { 384a5c3b9ffSAnshuman Khandual } 385a5c3b9ffSAnshuman Khandual static void __init pmd_leaf_tests(unsigned long pfn, pgprot_t prot) { } 386a5c3b9ffSAnshuman Khandual static void __init pud_leaf_tests(unsigned long pfn, pgprot_t prot) { } 387a5c3b9ffSAnshuman Khandual static void __init pmd_huge_tests(pmd_t *pmdp, unsigned long pfn, pgprot_t prot) 388a5c3b9ffSAnshuman Khandual { 389a5c3b9ffSAnshuman Khandual } 390a5c3b9ffSAnshuman Khandual static void __init pud_huge_tests(pud_t *pudp, unsigned long pfn, pgprot_t prot) 391a5c3b9ffSAnshuman Khandual { 392a5c3b9ffSAnshuman Khandual } 393a5c3b9ffSAnshuman Khandual static void __init pmd_savedwrite_tests(unsigned long pfn, pgprot_t prot) { } 394399145f9SAnshuman Khandual #endif /* CONFIG_TRANSPARENT_HUGEPAGE */ 395399145f9SAnshuman Khandual 396399145f9SAnshuman Khandual static void __init p4d_basic_tests(unsigned long pfn, pgprot_t prot) 397399145f9SAnshuman Khandual { 398399145f9SAnshuman Khandual p4d_t p4d; 399399145f9SAnshuman Khandual 4006315df41SAnshuman Khandual pr_debug("Validating P4D basic\n"); 401399145f9SAnshuman Khandual memset(&p4d, RANDOM_NZVALUE, sizeof(p4d_t)); 402399145f9SAnshuman Khandual WARN_ON(!p4d_same(p4d, p4d)); 403399145f9SAnshuman Khandual } 404399145f9SAnshuman Khandual 405399145f9SAnshuman Khandual static void __init pgd_basic_tests(unsigned long pfn, pgprot_t prot) 406399145f9SAnshuman Khandual { 407399145f9SAnshuman Khandual pgd_t pgd; 408399145f9SAnshuman Khandual 4096315df41SAnshuman Khandual pr_debug("Validating PGD basic\n"); 410399145f9SAnshuman Khandual memset(&pgd, RANDOM_NZVALUE, sizeof(pgd_t)); 411399145f9SAnshuman Khandual WARN_ON(!pgd_same(pgd, pgd)); 412399145f9SAnshuman Khandual } 413399145f9SAnshuman Khandual 414399145f9SAnshuman Khandual #ifndef __PAGETABLE_PUD_FOLDED 415399145f9SAnshuman Khandual static void __init pud_clear_tests(struct mm_struct *mm, pud_t *pudp) 416399145f9SAnshuman Khandual { 417399145f9SAnshuman Khandual pud_t pud = READ_ONCE(*pudp); 418399145f9SAnshuman Khandual 419399145f9SAnshuman Khandual if (mm_pmd_folded(mm)) 420399145f9SAnshuman Khandual return; 421399145f9SAnshuman Khandual 4226315df41SAnshuman Khandual pr_debug("Validating PUD clear\n"); 423399145f9SAnshuman Khandual pud = __pud(pud_val(pud) | RANDOM_ORVALUE); 424399145f9SAnshuman Khandual WRITE_ONCE(*pudp, pud); 425399145f9SAnshuman Khandual pud_clear(pudp); 426399145f9SAnshuman Khandual pud = READ_ONCE(*pudp); 427399145f9SAnshuman Khandual WARN_ON(!pud_none(pud)); 428399145f9SAnshuman Khandual } 429399145f9SAnshuman Khandual 430399145f9SAnshuman Khandual static void __init pud_populate_tests(struct mm_struct *mm, pud_t *pudp, 431399145f9SAnshuman Khandual pmd_t *pmdp) 432399145f9SAnshuman Khandual { 433399145f9SAnshuman Khandual pud_t pud; 434399145f9SAnshuman Khandual 435399145f9SAnshuman Khandual if (mm_pmd_folded(mm)) 436399145f9SAnshuman Khandual return; 4376315df41SAnshuman Khandual 4386315df41SAnshuman Khandual pr_debug("Validating PUD populate\n"); 439399145f9SAnshuman Khandual /* 440399145f9SAnshuman Khandual * This entry points to next level page table page. 441399145f9SAnshuman Khandual * Hence this must not qualify as pud_bad(). 442399145f9SAnshuman Khandual */ 443399145f9SAnshuman Khandual pmd_clear(pmdp); 444399145f9SAnshuman Khandual pud_clear(pudp); 445399145f9SAnshuman Khandual pud_populate(mm, pudp, pmdp); 446399145f9SAnshuman Khandual pud = READ_ONCE(*pudp); 447399145f9SAnshuman Khandual WARN_ON(pud_bad(pud)); 448399145f9SAnshuman Khandual } 449399145f9SAnshuman Khandual #else /* !__PAGETABLE_PUD_FOLDED */ 450399145f9SAnshuman Khandual static void __init pud_clear_tests(struct mm_struct *mm, pud_t *pudp) { } 451399145f9SAnshuman Khandual static void __init pud_populate_tests(struct mm_struct *mm, pud_t *pudp, 452399145f9SAnshuman Khandual pmd_t *pmdp) 453399145f9SAnshuman Khandual { 454399145f9SAnshuman Khandual } 455399145f9SAnshuman Khandual #endif /* PAGETABLE_PUD_FOLDED */ 456399145f9SAnshuman Khandual 457399145f9SAnshuman Khandual #ifndef __PAGETABLE_P4D_FOLDED 458399145f9SAnshuman Khandual static void __init p4d_clear_tests(struct mm_struct *mm, p4d_t *p4dp) 459399145f9SAnshuman Khandual { 460399145f9SAnshuman Khandual p4d_t p4d = READ_ONCE(*p4dp); 461399145f9SAnshuman Khandual 462399145f9SAnshuman Khandual if (mm_pud_folded(mm)) 463399145f9SAnshuman Khandual return; 464399145f9SAnshuman Khandual 4656315df41SAnshuman Khandual pr_debug("Validating P4D clear\n"); 466399145f9SAnshuman Khandual p4d = __p4d(p4d_val(p4d) | RANDOM_ORVALUE); 467399145f9SAnshuman Khandual WRITE_ONCE(*p4dp, p4d); 468399145f9SAnshuman Khandual p4d_clear(p4dp); 469399145f9SAnshuman Khandual p4d = READ_ONCE(*p4dp); 470399145f9SAnshuman Khandual WARN_ON(!p4d_none(p4d)); 471399145f9SAnshuman Khandual } 472399145f9SAnshuman Khandual 473399145f9SAnshuman Khandual static void __init p4d_populate_tests(struct mm_struct *mm, p4d_t *p4dp, 474399145f9SAnshuman Khandual pud_t *pudp) 475399145f9SAnshuman Khandual { 476399145f9SAnshuman Khandual p4d_t p4d; 477399145f9SAnshuman Khandual 478399145f9SAnshuman Khandual if (mm_pud_folded(mm)) 479399145f9SAnshuman Khandual return; 480399145f9SAnshuman Khandual 4816315df41SAnshuman Khandual pr_debug("Validating P4D populate\n"); 482399145f9SAnshuman Khandual /* 483399145f9SAnshuman Khandual * This entry points to next level page table page. 484399145f9SAnshuman Khandual * Hence this must not qualify as p4d_bad(). 485399145f9SAnshuman Khandual */ 486399145f9SAnshuman Khandual pud_clear(pudp); 487399145f9SAnshuman Khandual p4d_clear(p4dp); 488399145f9SAnshuman Khandual p4d_populate(mm, p4dp, pudp); 489399145f9SAnshuman Khandual p4d = READ_ONCE(*p4dp); 490399145f9SAnshuman Khandual WARN_ON(p4d_bad(p4d)); 491399145f9SAnshuman Khandual } 492399145f9SAnshuman Khandual 493399145f9SAnshuman Khandual static void __init pgd_clear_tests(struct mm_struct *mm, pgd_t *pgdp) 494399145f9SAnshuman Khandual { 495399145f9SAnshuman Khandual pgd_t pgd = READ_ONCE(*pgdp); 496399145f9SAnshuman Khandual 497399145f9SAnshuman Khandual if (mm_p4d_folded(mm)) 498399145f9SAnshuman Khandual return; 499399145f9SAnshuman Khandual 5006315df41SAnshuman Khandual pr_debug("Validating PGD clear\n"); 501399145f9SAnshuman Khandual pgd = __pgd(pgd_val(pgd) | RANDOM_ORVALUE); 502399145f9SAnshuman Khandual WRITE_ONCE(*pgdp, pgd); 503399145f9SAnshuman Khandual pgd_clear(pgdp); 504399145f9SAnshuman Khandual pgd = READ_ONCE(*pgdp); 505399145f9SAnshuman Khandual WARN_ON(!pgd_none(pgd)); 506399145f9SAnshuman Khandual } 507399145f9SAnshuman Khandual 508399145f9SAnshuman Khandual static void __init pgd_populate_tests(struct mm_struct *mm, pgd_t *pgdp, 509399145f9SAnshuman Khandual p4d_t *p4dp) 510399145f9SAnshuman Khandual { 511399145f9SAnshuman Khandual pgd_t pgd; 512399145f9SAnshuman Khandual 513399145f9SAnshuman Khandual if (mm_p4d_folded(mm)) 514399145f9SAnshuman Khandual return; 515399145f9SAnshuman Khandual 5166315df41SAnshuman Khandual pr_debug("Validating PGD populate\n"); 517399145f9SAnshuman Khandual /* 518399145f9SAnshuman Khandual * This entry points to next level page table page. 519399145f9SAnshuman Khandual * Hence this must not qualify as pgd_bad(). 520399145f9SAnshuman Khandual */ 521399145f9SAnshuman Khandual p4d_clear(p4dp); 522399145f9SAnshuman Khandual pgd_clear(pgdp); 523399145f9SAnshuman Khandual pgd_populate(mm, pgdp, p4dp); 524399145f9SAnshuman Khandual pgd = READ_ONCE(*pgdp); 525399145f9SAnshuman Khandual WARN_ON(pgd_bad(pgd)); 526399145f9SAnshuman Khandual } 527399145f9SAnshuman Khandual #else /* !__PAGETABLE_P4D_FOLDED */ 528399145f9SAnshuman Khandual static void __init p4d_clear_tests(struct mm_struct *mm, p4d_t *p4dp) { } 529399145f9SAnshuman Khandual static void __init pgd_clear_tests(struct mm_struct *mm, pgd_t *pgdp) { } 530399145f9SAnshuman Khandual static void __init p4d_populate_tests(struct mm_struct *mm, p4d_t *p4dp, 531399145f9SAnshuman Khandual pud_t *pudp) 532399145f9SAnshuman Khandual { 533399145f9SAnshuman Khandual } 534399145f9SAnshuman Khandual static void __init pgd_populate_tests(struct mm_struct *mm, pgd_t *pgdp, 535399145f9SAnshuman Khandual p4d_t *p4dp) 536399145f9SAnshuman Khandual { 537399145f9SAnshuman Khandual } 538399145f9SAnshuman Khandual #endif /* PAGETABLE_P4D_FOLDED */ 539399145f9SAnshuman Khandual 540399145f9SAnshuman Khandual static void __init pte_clear_tests(struct mm_struct *mm, pte_t *ptep, 541399145f9SAnshuman Khandual unsigned long vaddr) 542399145f9SAnshuman Khandual { 5439449c9cbSChristophe Leroy pte_t pte = ptep_get(ptep); 544399145f9SAnshuman Khandual 5456315df41SAnshuman Khandual pr_debug("Validating PTE clear\n"); 546399145f9SAnshuman Khandual pte = __pte(pte_val(pte) | RANDOM_ORVALUE); 547399145f9SAnshuman Khandual set_pte_at(mm, vaddr, ptep, pte); 548399145f9SAnshuman Khandual barrier(); 549399145f9SAnshuman Khandual pte_clear(mm, vaddr, ptep); 5509449c9cbSChristophe Leroy pte = ptep_get(ptep); 551399145f9SAnshuman Khandual WARN_ON(!pte_none(pte)); 552399145f9SAnshuman Khandual } 553399145f9SAnshuman Khandual 554399145f9SAnshuman Khandual static void __init pmd_clear_tests(struct mm_struct *mm, pmd_t *pmdp) 555399145f9SAnshuman Khandual { 556399145f9SAnshuman Khandual pmd_t pmd = READ_ONCE(*pmdp); 557399145f9SAnshuman Khandual 5586315df41SAnshuman Khandual pr_debug("Validating PMD clear\n"); 559399145f9SAnshuman Khandual pmd = __pmd(pmd_val(pmd) | RANDOM_ORVALUE); 560399145f9SAnshuman Khandual WRITE_ONCE(*pmdp, pmd); 561399145f9SAnshuman Khandual pmd_clear(pmdp); 562399145f9SAnshuman Khandual pmd = READ_ONCE(*pmdp); 563399145f9SAnshuman Khandual WARN_ON(!pmd_none(pmd)); 564399145f9SAnshuman Khandual } 565399145f9SAnshuman Khandual 566399145f9SAnshuman Khandual static void __init pmd_populate_tests(struct mm_struct *mm, pmd_t *pmdp, 567399145f9SAnshuman Khandual pgtable_t pgtable) 568399145f9SAnshuman Khandual { 569399145f9SAnshuman Khandual pmd_t pmd; 570399145f9SAnshuman Khandual 5716315df41SAnshuman Khandual pr_debug("Validating PMD populate\n"); 572399145f9SAnshuman Khandual /* 573399145f9SAnshuman Khandual * This entry points to next level page table page. 574399145f9SAnshuman Khandual * Hence this must not qualify as pmd_bad(). 575399145f9SAnshuman Khandual */ 576399145f9SAnshuman Khandual pmd_clear(pmdp); 577399145f9SAnshuman Khandual pmd_populate(mm, pmdp, pgtable); 578399145f9SAnshuman Khandual pmd = READ_ONCE(*pmdp); 579399145f9SAnshuman Khandual WARN_ON(pmd_bad(pmd)); 580399145f9SAnshuman Khandual } 581399145f9SAnshuman Khandual 58205289402SAnshuman Khandual static void __init pte_special_tests(unsigned long pfn, pgprot_t prot) 58305289402SAnshuman Khandual { 58405289402SAnshuman Khandual pte_t pte = pfn_pte(pfn, prot); 58505289402SAnshuman Khandual 58605289402SAnshuman Khandual if (!IS_ENABLED(CONFIG_ARCH_HAS_PTE_SPECIAL)) 58705289402SAnshuman Khandual return; 58805289402SAnshuman Khandual 5896315df41SAnshuman Khandual pr_debug("Validating PTE special\n"); 59005289402SAnshuman Khandual WARN_ON(!pte_special(pte_mkspecial(pte))); 59105289402SAnshuman Khandual } 59205289402SAnshuman Khandual 59305289402SAnshuman Khandual static void __init pte_protnone_tests(unsigned long pfn, pgprot_t prot) 59405289402SAnshuman Khandual { 59505289402SAnshuman Khandual pte_t pte = pfn_pte(pfn, prot); 59605289402SAnshuman Khandual 59705289402SAnshuman Khandual if (!IS_ENABLED(CONFIG_NUMA_BALANCING)) 59805289402SAnshuman Khandual return; 59905289402SAnshuman Khandual 6006315df41SAnshuman Khandual pr_debug("Validating PTE protnone\n"); 60105289402SAnshuman Khandual WARN_ON(!pte_protnone(pte)); 60205289402SAnshuman Khandual WARN_ON(!pte_present(pte)); 60305289402SAnshuman Khandual } 60405289402SAnshuman Khandual 60505289402SAnshuman Khandual #ifdef CONFIG_TRANSPARENT_HUGEPAGE 60605289402SAnshuman Khandual static void __init pmd_protnone_tests(unsigned long pfn, pgprot_t prot) 60705289402SAnshuman Khandual { 60805289402SAnshuman Khandual pmd_t pmd = pmd_mkhuge(pfn_pmd(pfn, prot)); 60905289402SAnshuman Khandual 61005289402SAnshuman Khandual if (!IS_ENABLED(CONFIG_NUMA_BALANCING)) 61105289402SAnshuman Khandual return; 61205289402SAnshuman Khandual 6136315df41SAnshuman Khandual pr_debug("Validating PMD protnone\n"); 61405289402SAnshuman Khandual WARN_ON(!pmd_protnone(pmd)); 61505289402SAnshuman Khandual WARN_ON(!pmd_present(pmd)); 61605289402SAnshuman Khandual } 61705289402SAnshuman Khandual #else /* !CONFIG_TRANSPARENT_HUGEPAGE */ 61805289402SAnshuman Khandual static void __init pmd_protnone_tests(unsigned long pfn, pgprot_t prot) { } 61905289402SAnshuman Khandual #endif /* CONFIG_TRANSPARENT_HUGEPAGE */ 62005289402SAnshuman Khandual 62105289402SAnshuman Khandual #ifdef CONFIG_ARCH_HAS_PTE_DEVMAP 62205289402SAnshuman Khandual static void __init pte_devmap_tests(unsigned long pfn, pgprot_t prot) 62305289402SAnshuman Khandual { 62405289402SAnshuman Khandual pte_t pte = pfn_pte(pfn, prot); 62505289402SAnshuman Khandual 6266315df41SAnshuman Khandual pr_debug("Validating PTE devmap\n"); 62705289402SAnshuman Khandual WARN_ON(!pte_devmap(pte_mkdevmap(pte))); 62805289402SAnshuman Khandual } 62905289402SAnshuman Khandual 63005289402SAnshuman Khandual #ifdef CONFIG_TRANSPARENT_HUGEPAGE 63105289402SAnshuman Khandual static void __init pmd_devmap_tests(unsigned long pfn, pgprot_t prot) 63205289402SAnshuman Khandual { 63305289402SAnshuman Khandual pmd_t pmd = pfn_pmd(pfn, prot); 63405289402SAnshuman Khandual 6356315df41SAnshuman Khandual pr_debug("Validating PMD devmap\n"); 63605289402SAnshuman Khandual WARN_ON(!pmd_devmap(pmd_mkdevmap(pmd))); 63705289402SAnshuman Khandual } 63805289402SAnshuman Khandual 63905289402SAnshuman Khandual #ifdef CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD 64005289402SAnshuman Khandual static void __init pud_devmap_tests(unsigned long pfn, pgprot_t prot) 64105289402SAnshuman Khandual { 64205289402SAnshuman Khandual pud_t pud = pfn_pud(pfn, prot); 64305289402SAnshuman Khandual 6446315df41SAnshuman Khandual pr_debug("Validating PUD devmap\n"); 64505289402SAnshuman Khandual WARN_ON(!pud_devmap(pud_mkdevmap(pud))); 64605289402SAnshuman Khandual } 64705289402SAnshuman Khandual #else /* !CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD */ 64805289402SAnshuman Khandual static void __init pud_devmap_tests(unsigned long pfn, pgprot_t prot) { } 64905289402SAnshuman Khandual #endif /* CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD */ 65005289402SAnshuman Khandual #else /* CONFIG_TRANSPARENT_HUGEPAGE */ 65105289402SAnshuman Khandual static void __init pmd_devmap_tests(unsigned long pfn, pgprot_t prot) { } 65205289402SAnshuman Khandual static void __init pud_devmap_tests(unsigned long pfn, pgprot_t prot) { } 65305289402SAnshuman Khandual #endif /* CONFIG_TRANSPARENT_HUGEPAGE */ 65405289402SAnshuman Khandual #else 65505289402SAnshuman Khandual static void __init pte_devmap_tests(unsigned long pfn, pgprot_t prot) { } 65605289402SAnshuman Khandual static void __init pmd_devmap_tests(unsigned long pfn, pgprot_t prot) { } 65705289402SAnshuman Khandual static void __init pud_devmap_tests(unsigned long pfn, pgprot_t prot) { } 65805289402SAnshuman Khandual #endif /* CONFIG_ARCH_HAS_PTE_DEVMAP */ 65905289402SAnshuman Khandual 66005289402SAnshuman Khandual static void __init pte_soft_dirty_tests(unsigned long pfn, pgprot_t prot) 66105289402SAnshuman Khandual { 66205289402SAnshuman Khandual pte_t pte = pfn_pte(pfn, prot); 66305289402SAnshuman Khandual 66405289402SAnshuman Khandual if (!IS_ENABLED(CONFIG_MEM_SOFT_DIRTY)) 66505289402SAnshuman Khandual return; 66605289402SAnshuman Khandual 6676315df41SAnshuman Khandual pr_debug("Validating PTE soft dirty\n"); 66805289402SAnshuman Khandual WARN_ON(!pte_soft_dirty(pte_mksoft_dirty(pte))); 66905289402SAnshuman Khandual WARN_ON(pte_soft_dirty(pte_clear_soft_dirty(pte))); 67005289402SAnshuman Khandual } 67105289402SAnshuman Khandual 67205289402SAnshuman Khandual static void __init pte_swap_soft_dirty_tests(unsigned long pfn, pgprot_t prot) 67305289402SAnshuman Khandual { 67405289402SAnshuman Khandual pte_t pte = pfn_pte(pfn, prot); 67505289402SAnshuman Khandual 67605289402SAnshuman Khandual if (!IS_ENABLED(CONFIG_MEM_SOFT_DIRTY)) 67705289402SAnshuman Khandual return; 67805289402SAnshuman Khandual 6796315df41SAnshuman Khandual pr_debug("Validating PTE swap soft dirty\n"); 68005289402SAnshuman Khandual WARN_ON(!pte_swp_soft_dirty(pte_swp_mksoft_dirty(pte))); 68105289402SAnshuman Khandual WARN_ON(pte_swp_soft_dirty(pte_swp_clear_soft_dirty(pte))); 68205289402SAnshuman Khandual } 68305289402SAnshuman Khandual 68405289402SAnshuman Khandual #ifdef CONFIG_TRANSPARENT_HUGEPAGE 68505289402SAnshuman Khandual static void __init pmd_soft_dirty_tests(unsigned long pfn, pgprot_t prot) 68605289402SAnshuman Khandual { 68705289402SAnshuman Khandual pmd_t pmd = pfn_pmd(pfn, prot); 68805289402SAnshuman Khandual 68905289402SAnshuman Khandual if (!IS_ENABLED(CONFIG_MEM_SOFT_DIRTY)) 69005289402SAnshuman Khandual return; 69105289402SAnshuman Khandual 6926315df41SAnshuman Khandual pr_debug("Validating PMD soft dirty\n"); 69305289402SAnshuman Khandual WARN_ON(!pmd_soft_dirty(pmd_mksoft_dirty(pmd))); 69405289402SAnshuman Khandual WARN_ON(pmd_soft_dirty(pmd_clear_soft_dirty(pmd))); 69505289402SAnshuman Khandual } 69605289402SAnshuman Khandual 69705289402SAnshuman Khandual static void __init pmd_swap_soft_dirty_tests(unsigned long pfn, pgprot_t prot) 69805289402SAnshuman Khandual { 69905289402SAnshuman Khandual pmd_t pmd = pfn_pmd(pfn, prot); 70005289402SAnshuman Khandual 70105289402SAnshuman Khandual if (!IS_ENABLED(CONFIG_MEM_SOFT_DIRTY) || 70205289402SAnshuman Khandual !IS_ENABLED(CONFIG_ARCH_ENABLE_THP_MIGRATION)) 70305289402SAnshuman Khandual return; 70405289402SAnshuman Khandual 7056315df41SAnshuman Khandual pr_debug("Validating PMD swap soft dirty\n"); 70605289402SAnshuman Khandual WARN_ON(!pmd_swp_soft_dirty(pmd_swp_mksoft_dirty(pmd))); 70705289402SAnshuman Khandual WARN_ON(pmd_swp_soft_dirty(pmd_swp_clear_soft_dirty(pmd))); 70805289402SAnshuman Khandual } 70905289402SAnshuman Khandual #else /* !CONFIG_ARCH_HAS_PTE_DEVMAP */ 71005289402SAnshuman Khandual static void __init pmd_soft_dirty_tests(unsigned long pfn, pgprot_t prot) { } 71105289402SAnshuman Khandual static void __init pmd_swap_soft_dirty_tests(unsigned long pfn, pgprot_t prot) 71205289402SAnshuman Khandual { 71305289402SAnshuman Khandual } 71405289402SAnshuman Khandual #endif /* CONFIG_ARCH_HAS_PTE_DEVMAP */ 71505289402SAnshuman Khandual 71605289402SAnshuman Khandual static void __init pte_swap_tests(unsigned long pfn, pgprot_t prot) 71705289402SAnshuman Khandual { 71805289402SAnshuman Khandual swp_entry_t swp; 71905289402SAnshuman Khandual pte_t pte; 72005289402SAnshuman Khandual 7216315df41SAnshuman Khandual pr_debug("Validating PTE swap\n"); 72205289402SAnshuman Khandual pte = pfn_pte(pfn, prot); 72305289402SAnshuman Khandual swp = __pte_to_swp_entry(pte); 72405289402SAnshuman Khandual pte = __swp_entry_to_pte(swp); 72505289402SAnshuman Khandual WARN_ON(pfn != pte_pfn(pte)); 72605289402SAnshuman Khandual } 72705289402SAnshuman Khandual 72805289402SAnshuman Khandual #ifdef CONFIG_ARCH_ENABLE_THP_MIGRATION 72905289402SAnshuman Khandual static void __init pmd_swap_tests(unsigned long pfn, pgprot_t prot) 73005289402SAnshuman Khandual { 73105289402SAnshuman Khandual swp_entry_t swp; 73205289402SAnshuman Khandual pmd_t pmd; 73305289402SAnshuman Khandual 7346315df41SAnshuman Khandual pr_debug("Validating PMD swap\n"); 73505289402SAnshuman Khandual pmd = pfn_pmd(pfn, prot); 73605289402SAnshuman Khandual swp = __pmd_to_swp_entry(pmd); 73705289402SAnshuman Khandual pmd = __swp_entry_to_pmd(swp); 73805289402SAnshuman Khandual WARN_ON(pfn != pmd_pfn(pmd)); 73905289402SAnshuman Khandual } 74005289402SAnshuman Khandual #else /* !CONFIG_ARCH_ENABLE_THP_MIGRATION */ 74105289402SAnshuman Khandual static void __init pmd_swap_tests(unsigned long pfn, pgprot_t prot) { } 74205289402SAnshuman Khandual #endif /* CONFIG_ARCH_ENABLE_THP_MIGRATION */ 74305289402SAnshuman Khandual 74405289402SAnshuman Khandual static void __init swap_migration_tests(void) 74505289402SAnshuman Khandual { 74605289402SAnshuman Khandual struct page *page; 74705289402SAnshuman Khandual swp_entry_t swp; 74805289402SAnshuman Khandual 74905289402SAnshuman Khandual if (!IS_ENABLED(CONFIG_MIGRATION)) 75005289402SAnshuman Khandual return; 7516315df41SAnshuman Khandual 7526315df41SAnshuman Khandual pr_debug("Validating swap migration\n"); 75305289402SAnshuman Khandual /* 75405289402SAnshuman Khandual * swap_migration_tests() requires a dedicated page as it needs to 75505289402SAnshuman Khandual * be locked before creating a migration entry from it. Locking the 75605289402SAnshuman Khandual * page that actually maps kernel text ('start_kernel') can be real 75705289402SAnshuman Khandual * problematic. Lets allocate a dedicated page explicitly for this 75805289402SAnshuman Khandual * purpose that will be freed subsequently. 75905289402SAnshuman Khandual */ 76005289402SAnshuman Khandual page = alloc_page(GFP_KERNEL); 76105289402SAnshuman Khandual if (!page) { 76205289402SAnshuman Khandual pr_err("page allocation failed\n"); 76305289402SAnshuman Khandual return; 76405289402SAnshuman Khandual } 76505289402SAnshuman Khandual 76605289402SAnshuman Khandual /* 76705289402SAnshuman Khandual * make_migration_entry() expects given page to be 76805289402SAnshuman Khandual * locked, otherwise it stumbles upon a BUG_ON(). 76905289402SAnshuman Khandual */ 77005289402SAnshuman Khandual __SetPageLocked(page); 77105289402SAnshuman Khandual swp = make_migration_entry(page, 1); 77205289402SAnshuman Khandual WARN_ON(!is_migration_entry(swp)); 77305289402SAnshuman Khandual WARN_ON(!is_write_migration_entry(swp)); 77405289402SAnshuman Khandual 77505289402SAnshuman Khandual make_migration_entry_read(&swp); 77605289402SAnshuman Khandual WARN_ON(!is_migration_entry(swp)); 77705289402SAnshuman Khandual WARN_ON(is_write_migration_entry(swp)); 77805289402SAnshuman Khandual 77905289402SAnshuman Khandual swp = make_migration_entry(page, 0); 78005289402SAnshuman Khandual WARN_ON(!is_migration_entry(swp)); 78105289402SAnshuman Khandual WARN_ON(is_write_migration_entry(swp)); 78205289402SAnshuman Khandual __ClearPageLocked(page); 78305289402SAnshuman Khandual __free_page(page); 78405289402SAnshuman Khandual } 78505289402SAnshuman Khandual 78605289402SAnshuman Khandual #ifdef CONFIG_HUGETLB_PAGE 78705289402SAnshuman Khandual static void __init hugetlb_basic_tests(unsigned long pfn, pgprot_t prot) 78805289402SAnshuman Khandual { 78905289402SAnshuman Khandual struct page *page; 79005289402SAnshuman Khandual pte_t pte; 79105289402SAnshuman Khandual 7926315df41SAnshuman Khandual pr_debug("Validating HugeTLB basic\n"); 79305289402SAnshuman Khandual /* 79405289402SAnshuman Khandual * Accessing the page associated with the pfn is safe here, 79505289402SAnshuman Khandual * as it was previously derived from a real kernel symbol. 79605289402SAnshuman Khandual */ 79705289402SAnshuman Khandual page = pfn_to_page(pfn); 79805289402SAnshuman Khandual pte = mk_huge_pte(page, prot); 79905289402SAnshuman Khandual 80005289402SAnshuman Khandual WARN_ON(!huge_pte_dirty(huge_pte_mkdirty(pte))); 80105289402SAnshuman Khandual WARN_ON(!huge_pte_write(huge_pte_mkwrite(huge_pte_wrprotect(pte)))); 80205289402SAnshuman Khandual WARN_ON(huge_pte_write(huge_pte_wrprotect(huge_pte_mkwrite(pte)))); 80305289402SAnshuman Khandual 80405289402SAnshuman Khandual #ifdef CONFIG_ARCH_WANT_GENERAL_HUGETLB 80505289402SAnshuman Khandual pte = pfn_pte(pfn, prot); 80605289402SAnshuman Khandual 80705289402SAnshuman Khandual WARN_ON(!pte_huge(pte_mkhuge(pte))); 80805289402SAnshuman Khandual #endif /* CONFIG_ARCH_WANT_GENERAL_HUGETLB */ 80905289402SAnshuman Khandual } 810a5c3b9ffSAnshuman Khandual 811a5c3b9ffSAnshuman Khandual static void __init hugetlb_advanced_tests(struct mm_struct *mm, 812a5c3b9ffSAnshuman Khandual struct vm_area_struct *vma, 813a5c3b9ffSAnshuman Khandual pte_t *ptep, unsigned long pfn, 814a5c3b9ffSAnshuman Khandual unsigned long vaddr, pgprot_t prot) 815a5c3b9ffSAnshuman Khandual { 816a5c3b9ffSAnshuman Khandual struct page *page = pfn_to_page(pfn); 817a5c3b9ffSAnshuman Khandual pte_t pte = ptep_get(ptep); 818a5c3b9ffSAnshuman Khandual unsigned long paddr = __pfn_to_phys(pfn) & PMD_MASK; 819a5c3b9ffSAnshuman Khandual 8206315df41SAnshuman Khandual pr_debug("Validating HugeTLB advanced\n"); 821a5c3b9ffSAnshuman Khandual pte = pte_mkhuge(mk_pte(pfn_to_page(PHYS_PFN(paddr)), prot)); 822a5c3b9ffSAnshuman Khandual set_huge_pte_at(mm, vaddr, ptep, pte); 823a5c3b9ffSAnshuman Khandual barrier(); 824a5c3b9ffSAnshuman Khandual WARN_ON(!pte_same(pte, huge_ptep_get(ptep))); 825a5c3b9ffSAnshuman Khandual huge_pte_clear(mm, vaddr, ptep, PMD_SIZE); 826a5c3b9ffSAnshuman Khandual pte = huge_ptep_get(ptep); 827a5c3b9ffSAnshuman Khandual WARN_ON(!huge_pte_none(pte)); 828a5c3b9ffSAnshuman Khandual 829a5c3b9ffSAnshuman Khandual pte = mk_huge_pte(page, prot); 830a5c3b9ffSAnshuman Khandual set_huge_pte_at(mm, vaddr, ptep, pte); 831a5c3b9ffSAnshuman Khandual barrier(); 832a5c3b9ffSAnshuman Khandual huge_ptep_set_wrprotect(mm, vaddr, ptep); 833a5c3b9ffSAnshuman Khandual pte = huge_ptep_get(ptep); 834a5c3b9ffSAnshuman Khandual WARN_ON(huge_pte_write(pte)); 835a5c3b9ffSAnshuman Khandual 836a5c3b9ffSAnshuman Khandual pte = mk_huge_pte(page, prot); 837a5c3b9ffSAnshuman Khandual set_huge_pte_at(mm, vaddr, ptep, pte); 838a5c3b9ffSAnshuman Khandual barrier(); 839a5c3b9ffSAnshuman Khandual huge_ptep_get_and_clear(mm, vaddr, ptep); 840a5c3b9ffSAnshuman Khandual pte = huge_ptep_get(ptep); 841a5c3b9ffSAnshuman Khandual WARN_ON(!huge_pte_none(pte)); 842a5c3b9ffSAnshuman Khandual 843a5c3b9ffSAnshuman Khandual pte = mk_huge_pte(page, prot); 844a5c3b9ffSAnshuman Khandual pte = huge_pte_wrprotect(pte); 845a5c3b9ffSAnshuman Khandual set_huge_pte_at(mm, vaddr, ptep, pte); 846a5c3b9ffSAnshuman Khandual barrier(); 847a5c3b9ffSAnshuman Khandual pte = huge_pte_mkwrite(pte); 848a5c3b9ffSAnshuman Khandual pte = huge_pte_mkdirty(pte); 849a5c3b9ffSAnshuman Khandual huge_ptep_set_access_flags(vma, vaddr, ptep, pte, 1); 850a5c3b9ffSAnshuman Khandual pte = huge_ptep_get(ptep); 851a5c3b9ffSAnshuman Khandual WARN_ON(!(huge_pte_write(pte) && huge_pte_dirty(pte))); 852a5c3b9ffSAnshuman Khandual } 85305289402SAnshuman Khandual #else /* !CONFIG_HUGETLB_PAGE */ 85405289402SAnshuman Khandual static void __init hugetlb_basic_tests(unsigned long pfn, pgprot_t prot) { } 855a5c3b9ffSAnshuman Khandual static void __init hugetlb_advanced_tests(struct mm_struct *mm, 856a5c3b9ffSAnshuman Khandual struct vm_area_struct *vma, 857a5c3b9ffSAnshuman Khandual pte_t *ptep, unsigned long pfn, 858a5c3b9ffSAnshuman Khandual unsigned long vaddr, pgprot_t prot) 859a5c3b9ffSAnshuman Khandual { 860a5c3b9ffSAnshuman Khandual } 86105289402SAnshuman Khandual #endif /* CONFIG_HUGETLB_PAGE */ 86205289402SAnshuman Khandual 86305289402SAnshuman Khandual #ifdef CONFIG_TRANSPARENT_HUGEPAGE 86405289402SAnshuman Khandual static void __init pmd_thp_tests(unsigned long pfn, pgprot_t prot) 86505289402SAnshuman Khandual { 86605289402SAnshuman Khandual pmd_t pmd; 86705289402SAnshuman Khandual 86805289402SAnshuman Khandual if (!has_transparent_hugepage()) 86905289402SAnshuman Khandual return; 87005289402SAnshuman Khandual 8716315df41SAnshuman Khandual pr_debug("Validating PMD based THP\n"); 87205289402SAnshuman Khandual /* 87305289402SAnshuman Khandual * pmd_trans_huge() and pmd_present() must return positive after 87405289402SAnshuman Khandual * MMU invalidation with pmd_mkinvalid(). This behavior is an 87505289402SAnshuman Khandual * optimization for transparent huge page. pmd_trans_huge() must 87605289402SAnshuman Khandual * be true if pmd_page() returns a valid THP to avoid taking the 87705289402SAnshuman Khandual * pmd_lock when others walk over non transhuge pmds (i.e. there 87805289402SAnshuman Khandual * are no THP allocated). Especially when splitting a THP and 87905289402SAnshuman Khandual * removing the present bit from the pmd, pmd_trans_huge() still 88005289402SAnshuman Khandual * needs to return true. pmd_present() should be true whenever 88105289402SAnshuman Khandual * pmd_trans_huge() returns true. 88205289402SAnshuman Khandual */ 88305289402SAnshuman Khandual pmd = pfn_pmd(pfn, prot); 88405289402SAnshuman Khandual WARN_ON(!pmd_trans_huge(pmd_mkhuge(pmd))); 88505289402SAnshuman Khandual 88605289402SAnshuman Khandual #ifndef __HAVE_ARCH_PMDP_INVALIDATE 88705289402SAnshuman Khandual WARN_ON(!pmd_trans_huge(pmd_mkinvalid(pmd_mkhuge(pmd)))); 88805289402SAnshuman Khandual WARN_ON(!pmd_present(pmd_mkinvalid(pmd_mkhuge(pmd)))); 88905289402SAnshuman Khandual #endif /* __HAVE_ARCH_PMDP_INVALIDATE */ 89005289402SAnshuman Khandual } 89105289402SAnshuman Khandual 89205289402SAnshuman Khandual #ifdef CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD 89305289402SAnshuman Khandual static void __init pud_thp_tests(unsigned long pfn, pgprot_t prot) 89405289402SAnshuman Khandual { 89505289402SAnshuman Khandual pud_t pud; 89605289402SAnshuman Khandual 89705289402SAnshuman Khandual if (!has_transparent_hugepage()) 89805289402SAnshuman Khandual return; 89905289402SAnshuman Khandual 9006315df41SAnshuman Khandual pr_debug("Validating PUD based THP\n"); 90105289402SAnshuman Khandual pud = pfn_pud(pfn, prot); 90205289402SAnshuman Khandual WARN_ON(!pud_trans_huge(pud_mkhuge(pud))); 90305289402SAnshuman Khandual 90405289402SAnshuman Khandual /* 90505289402SAnshuman Khandual * pud_mkinvalid() has been dropped for now. Enable back 90605289402SAnshuman Khandual * these tests when it comes back with a modified pud_present(). 90705289402SAnshuman Khandual * 90805289402SAnshuman Khandual * WARN_ON(!pud_trans_huge(pud_mkinvalid(pud_mkhuge(pud)))); 90905289402SAnshuman Khandual * WARN_ON(!pud_present(pud_mkinvalid(pud_mkhuge(pud)))); 91005289402SAnshuman Khandual */ 91105289402SAnshuman Khandual } 91205289402SAnshuman Khandual #else /* !CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD */ 91305289402SAnshuman Khandual static void __init pud_thp_tests(unsigned long pfn, pgprot_t prot) { } 91405289402SAnshuman Khandual #endif /* CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD */ 91505289402SAnshuman Khandual #else /* !CONFIG_TRANSPARENT_HUGEPAGE */ 91605289402SAnshuman Khandual static void __init pmd_thp_tests(unsigned long pfn, pgprot_t prot) { } 91705289402SAnshuman Khandual static void __init pud_thp_tests(unsigned long pfn, pgprot_t prot) { } 91805289402SAnshuman Khandual #endif /* CONFIG_TRANSPARENT_HUGEPAGE */ 91905289402SAnshuman Khandual 920399145f9SAnshuman Khandual static unsigned long __init get_random_vaddr(void) 921399145f9SAnshuman Khandual { 922399145f9SAnshuman Khandual unsigned long random_vaddr, random_pages, total_user_pages; 923399145f9SAnshuman Khandual 924399145f9SAnshuman Khandual total_user_pages = (TASK_SIZE - FIRST_USER_ADDRESS) / PAGE_SIZE; 925399145f9SAnshuman Khandual 926399145f9SAnshuman Khandual random_pages = get_random_long() % total_user_pages; 927399145f9SAnshuman Khandual random_vaddr = FIRST_USER_ADDRESS + random_pages * PAGE_SIZE; 928399145f9SAnshuman Khandual 929399145f9SAnshuman Khandual return random_vaddr; 930399145f9SAnshuman Khandual } 931399145f9SAnshuman Khandual 932399145f9SAnshuman Khandual static int __init debug_vm_pgtable(void) 933399145f9SAnshuman Khandual { 934a5c3b9ffSAnshuman Khandual struct vm_area_struct *vma; 935399145f9SAnshuman Khandual struct mm_struct *mm; 936399145f9SAnshuman Khandual pgd_t *pgdp; 937399145f9SAnshuman Khandual p4d_t *p4dp, *saved_p4dp; 938399145f9SAnshuman Khandual pud_t *pudp, *saved_pudp; 939399145f9SAnshuman Khandual pmd_t *pmdp, *saved_pmdp, pmd; 940399145f9SAnshuman Khandual pte_t *ptep; 941399145f9SAnshuman Khandual pgtable_t saved_ptep; 94205289402SAnshuman Khandual pgprot_t prot, protnone; 943399145f9SAnshuman Khandual phys_addr_t paddr; 944399145f9SAnshuman Khandual unsigned long vaddr, pte_aligned, pmd_aligned; 945399145f9SAnshuman Khandual unsigned long pud_aligned, p4d_aligned, pgd_aligned; 946fea1120cSKees Cook spinlock_t *ptl = NULL; 947399145f9SAnshuman Khandual 948399145f9SAnshuman Khandual pr_info("Validating architecture page table helpers\n"); 949399145f9SAnshuman Khandual prot = vm_get_page_prot(VMFLAGS); 950399145f9SAnshuman Khandual vaddr = get_random_vaddr(); 951399145f9SAnshuman Khandual mm = mm_alloc(); 952399145f9SAnshuman Khandual if (!mm) { 953399145f9SAnshuman Khandual pr_err("mm_struct allocation failed\n"); 954399145f9SAnshuman Khandual return 1; 955399145f9SAnshuman Khandual } 956399145f9SAnshuman Khandual 957399145f9SAnshuman Khandual /* 95805289402SAnshuman Khandual * __P000 (or even __S000) will help create page table entries with 95905289402SAnshuman Khandual * PROT_NONE permission as required for pxx_protnone_tests(). 96005289402SAnshuman Khandual */ 96105289402SAnshuman Khandual protnone = __P000; 96205289402SAnshuman Khandual 963a5c3b9ffSAnshuman Khandual vma = vm_area_alloc(mm); 964a5c3b9ffSAnshuman Khandual if (!vma) { 965a5c3b9ffSAnshuman Khandual pr_err("vma allocation failed\n"); 966a5c3b9ffSAnshuman Khandual return 1; 967a5c3b9ffSAnshuman Khandual } 968a5c3b9ffSAnshuman Khandual 96905289402SAnshuman Khandual /* 970399145f9SAnshuman Khandual * PFN for mapping at PTE level is determined from a standard kernel 971399145f9SAnshuman Khandual * text symbol. But pfns for higher page table levels are derived by 972399145f9SAnshuman Khandual * masking lower bits of this real pfn. These derived pfns might not 973399145f9SAnshuman Khandual * exist on the platform but that does not really matter as pfn_pxx() 974399145f9SAnshuman Khandual * helpers will still create appropriate entries for the test. This 975399145f9SAnshuman Khandual * helps avoid large memory block allocations to be used for mapping 976399145f9SAnshuman Khandual * at higher page table levels. 977399145f9SAnshuman Khandual */ 978399145f9SAnshuman Khandual paddr = __pa_symbol(&start_kernel); 979399145f9SAnshuman Khandual 980399145f9SAnshuman Khandual pte_aligned = (paddr & PAGE_MASK) >> PAGE_SHIFT; 981399145f9SAnshuman Khandual pmd_aligned = (paddr & PMD_MASK) >> PAGE_SHIFT; 982399145f9SAnshuman Khandual pud_aligned = (paddr & PUD_MASK) >> PAGE_SHIFT; 983399145f9SAnshuman Khandual p4d_aligned = (paddr & P4D_MASK) >> PAGE_SHIFT; 984399145f9SAnshuman Khandual pgd_aligned = (paddr & PGDIR_MASK) >> PAGE_SHIFT; 985399145f9SAnshuman Khandual WARN_ON(!pfn_valid(pte_aligned)); 986399145f9SAnshuman Khandual 987399145f9SAnshuman Khandual pgdp = pgd_offset(mm, vaddr); 988399145f9SAnshuman Khandual p4dp = p4d_alloc(mm, pgdp, vaddr); 989399145f9SAnshuman Khandual pudp = pud_alloc(mm, p4dp, vaddr); 990399145f9SAnshuman Khandual pmdp = pmd_alloc(mm, pudp, vaddr); 991e8edf0adSAneesh Kumar K.V ptep = pte_alloc_map(mm, pmdp, vaddr); 992399145f9SAnshuman Khandual 993399145f9SAnshuman Khandual /* 994399145f9SAnshuman Khandual * Save all the page table page addresses as the page table 995399145f9SAnshuman Khandual * entries will be used for testing with random or garbage 996399145f9SAnshuman Khandual * values. These saved addresses will be used for freeing 997399145f9SAnshuman Khandual * page table pages. 998399145f9SAnshuman Khandual */ 999399145f9SAnshuman Khandual pmd = READ_ONCE(*pmdp); 1000399145f9SAnshuman Khandual saved_p4dp = p4d_offset(pgdp, 0UL); 1001399145f9SAnshuman Khandual saved_pudp = pud_offset(p4dp, 0UL); 1002399145f9SAnshuman Khandual saved_pmdp = pmd_offset(pudp, 0UL); 1003399145f9SAnshuman Khandual saved_ptep = pmd_pgtable(pmd); 1004399145f9SAnshuman Khandual 1005399145f9SAnshuman Khandual pte_basic_tests(pte_aligned, prot); 1006399145f9SAnshuman Khandual pmd_basic_tests(pmd_aligned, prot); 1007399145f9SAnshuman Khandual pud_basic_tests(pud_aligned, prot); 1008399145f9SAnshuman Khandual p4d_basic_tests(p4d_aligned, prot); 1009399145f9SAnshuman Khandual pgd_basic_tests(pgd_aligned, prot); 1010399145f9SAnshuman Khandual 1011a5c3b9ffSAnshuman Khandual pmd_leaf_tests(pmd_aligned, prot); 1012a5c3b9ffSAnshuman Khandual pud_leaf_tests(pud_aligned, prot); 1013a5c3b9ffSAnshuman Khandual 10144200605bSAneesh Kumar K.V pte_savedwrite_tests(pte_aligned, protnone); 10154200605bSAneesh Kumar K.V pmd_savedwrite_tests(pmd_aligned, protnone); 1016a5c3b9ffSAnshuman Khandual 101705289402SAnshuman Khandual pte_special_tests(pte_aligned, prot); 101805289402SAnshuman Khandual pte_protnone_tests(pte_aligned, protnone); 101905289402SAnshuman Khandual pmd_protnone_tests(pmd_aligned, protnone); 102005289402SAnshuman Khandual 102105289402SAnshuman Khandual pte_devmap_tests(pte_aligned, prot); 102205289402SAnshuman Khandual pmd_devmap_tests(pmd_aligned, prot); 102305289402SAnshuman Khandual pud_devmap_tests(pud_aligned, prot); 102405289402SAnshuman Khandual 102505289402SAnshuman Khandual pte_soft_dirty_tests(pte_aligned, prot); 102605289402SAnshuman Khandual pmd_soft_dirty_tests(pmd_aligned, prot); 102705289402SAnshuman Khandual pte_swap_soft_dirty_tests(pte_aligned, prot); 102805289402SAnshuman Khandual pmd_swap_soft_dirty_tests(pmd_aligned, prot); 102905289402SAnshuman Khandual 103005289402SAnshuman Khandual pte_swap_tests(pte_aligned, prot); 103105289402SAnshuman Khandual pmd_swap_tests(pmd_aligned, prot); 103205289402SAnshuman Khandual 103305289402SAnshuman Khandual swap_migration_tests(); 103405289402SAnshuman Khandual 103505289402SAnshuman Khandual pmd_thp_tests(pmd_aligned, prot); 103605289402SAnshuman Khandual pud_thp_tests(pud_aligned, prot); 103705289402SAnshuman Khandual 1038e8edf0adSAneesh Kumar K.V hugetlb_basic_tests(pte_aligned, prot); 1039e8edf0adSAneesh Kumar K.V 10406f302e27SAneesh Kumar K.V /* 10416f302e27SAneesh Kumar K.V * Page table modifying tests. They need to hold 10426f302e27SAneesh Kumar K.V * proper page table lock. 10436f302e27SAneesh Kumar K.V */ 1044e8edf0adSAneesh Kumar K.V 1045e8edf0adSAneesh Kumar K.V ptl = pte_lockptr(mm, pmdp); 1046e8edf0adSAneesh Kumar K.V spin_lock(ptl); 10476f302e27SAneesh Kumar K.V pte_clear_tests(mm, ptep, vaddr); 1048e8edf0adSAneesh Kumar K.V pte_advanced_tests(mm, vma, ptep, pte_aligned, vaddr, prot); 1049e8edf0adSAneesh Kumar K.V pte_unmap_unlock(ptep, ptl); 1050e8edf0adSAneesh Kumar K.V 10516f302e27SAneesh Kumar K.V ptl = pmd_lock(mm, pmdp); 10526f302e27SAneesh Kumar K.V pmd_clear_tests(mm, pmdp); 1053*87f34986SAneesh Kumar K.V pmd_advanced_tests(mm, vma, pmdp, pmd_aligned, vaddr, prot, saved_ptep); 10546f302e27SAneesh Kumar K.V pmd_huge_tests(pmdp, pmd_aligned, prot); 1055e8edf0adSAneesh Kumar K.V pmd_populate_tests(mm, pmdp, saved_ptep); 10566f302e27SAneesh Kumar K.V spin_unlock(ptl); 10576f302e27SAneesh Kumar K.V 10586f302e27SAneesh Kumar K.V ptl = pud_lock(mm, pudp); 10596f302e27SAneesh Kumar K.V pud_clear_tests(mm, pudp); 10606f302e27SAneesh Kumar K.V pud_advanced_tests(mm, vma, pudp, pud_aligned, vaddr, prot); 10616f302e27SAneesh Kumar K.V pud_huge_tests(pudp, pud_aligned, prot); 1062e8edf0adSAneesh Kumar K.V pud_populate_tests(mm, pudp, saved_pmdp); 10636f302e27SAneesh Kumar K.V spin_unlock(ptl); 10646f302e27SAneesh Kumar K.V 10656f302e27SAneesh Kumar K.V hugetlb_advanced_tests(mm, vma, ptep, pte_aligned, vaddr, prot); 10666f302e27SAneesh Kumar K.V 10676f302e27SAneesh Kumar K.V spin_lock(&mm->page_table_lock); 10686f302e27SAneesh Kumar K.V p4d_clear_tests(mm, p4dp); 10696f302e27SAneesh Kumar K.V pgd_clear_tests(mm, pgdp); 1070e8edf0adSAneesh Kumar K.V p4d_populate_tests(mm, p4dp, saved_pudp); 1071e8edf0adSAneesh Kumar K.V pgd_populate_tests(mm, pgdp, saved_p4dp); 10726f302e27SAneesh Kumar K.V spin_unlock(&mm->page_table_lock); 1073e8edf0adSAneesh Kumar K.V 1074399145f9SAnshuman Khandual p4d_free(mm, saved_p4dp); 1075399145f9SAnshuman Khandual pud_free(mm, saved_pudp); 1076399145f9SAnshuman Khandual pmd_free(mm, saved_pmdp); 1077399145f9SAnshuman Khandual pte_free(mm, saved_ptep); 1078399145f9SAnshuman Khandual 1079a5c3b9ffSAnshuman Khandual vm_area_free(vma); 1080399145f9SAnshuman Khandual mm_dec_nr_puds(mm); 1081399145f9SAnshuman Khandual mm_dec_nr_pmds(mm); 1082399145f9SAnshuman Khandual mm_dec_nr_ptes(mm); 1083399145f9SAnshuman Khandual mmdrop(mm); 1084399145f9SAnshuman Khandual return 0; 1085399145f9SAnshuman Khandual } 1086399145f9SAnshuman Khandual late_initcall(debug_vm_pgtable); 1087