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 613c9b84f0SGavin Shan struct pgtable_debug_args { 623c9b84f0SGavin Shan struct mm_struct *mm; 633c9b84f0SGavin Shan struct vm_area_struct *vma; 643c9b84f0SGavin Shan 653c9b84f0SGavin Shan pgd_t *pgdp; 663c9b84f0SGavin Shan p4d_t *p4dp; 673c9b84f0SGavin Shan pud_t *pudp; 683c9b84f0SGavin Shan pmd_t *pmdp; 693c9b84f0SGavin Shan pte_t *ptep; 703c9b84f0SGavin Shan 713c9b84f0SGavin Shan p4d_t *start_p4dp; 723c9b84f0SGavin Shan pud_t *start_pudp; 733c9b84f0SGavin Shan pmd_t *start_pmdp; 743c9b84f0SGavin Shan pgtable_t start_ptep; 753c9b84f0SGavin Shan 763c9b84f0SGavin Shan unsigned long vaddr; 773c9b84f0SGavin Shan pgprot_t page_prot; 783c9b84f0SGavin Shan pgprot_t page_prot_none; 793c9b84f0SGavin Shan 803c9b84f0SGavin Shan bool is_contiguous_page; 813c9b84f0SGavin Shan unsigned long pud_pfn; 823c9b84f0SGavin Shan unsigned long pmd_pfn; 833c9b84f0SGavin Shan unsigned long pte_pfn; 843c9b84f0SGavin Shan 853c9b84f0SGavin Shan unsigned long fixed_pgd_pfn; 863c9b84f0SGavin Shan unsigned long fixed_p4d_pfn; 873c9b84f0SGavin Shan unsigned long fixed_pud_pfn; 883c9b84f0SGavin Shan unsigned long fixed_pmd_pfn; 893c9b84f0SGavin Shan unsigned long fixed_pte_pfn; 903c9b84f0SGavin Shan }; 913c9b84f0SGavin Shan 9236b77d1eSGavin Shan static void __init pte_basic_tests(struct pgtable_debug_args *args, int idx) 93399145f9SAnshuman Khandual { 942e326c07SAnshuman Khandual pgprot_t prot = protection_map[idx]; 9536b77d1eSGavin Shan pte_t pte = pfn_pte(args->fixed_pte_pfn, prot); 962e326c07SAnshuman Khandual unsigned long val = idx, *ptr = &val; 97399145f9SAnshuman Khandual 982e326c07SAnshuman Khandual pr_debug("Validating PTE basic (%pGv)\n", ptr); 99bb5c47ceSAnshuman Khandual 100bb5c47ceSAnshuman Khandual /* 101bb5c47ceSAnshuman Khandual * This test needs to be executed after the given page table entry 102bb5c47ceSAnshuman Khandual * is created with pfn_pte() to make sure that protection_map[idx] 103bb5c47ceSAnshuman Khandual * does not have the dirty bit enabled from the beginning. This is 104bb5c47ceSAnshuman Khandual * important for platforms like arm64 where (!PTE_RDONLY) indicate 105bb5c47ceSAnshuman Khandual * dirty bit being set. 106bb5c47ceSAnshuman Khandual */ 107bb5c47ceSAnshuman Khandual WARN_ON(pte_dirty(pte_wrprotect(pte))); 108bb5c47ceSAnshuman Khandual 109399145f9SAnshuman Khandual WARN_ON(!pte_same(pte, pte)); 110399145f9SAnshuman Khandual WARN_ON(!pte_young(pte_mkyoung(pte_mkold(pte)))); 111399145f9SAnshuman Khandual WARN_ON(!pte_dirty(pte_mkdirty(pte_mkclean(pte)))); 112399145f9SAnshuman Khandual WARN_ON(!pte_write(pte_mkwrite(pte_wrprotect(pte)))); 113399145f9SAnshuman Khandual WARN_ON(pte_young(pte_mkold(pte_mkyoung(pte)))); 114399145f9SAnshuman Khandual WARN_ON(pte_dirty(pte_mkclean(pte_mkdirty(pte)))); 115399145f9SAnshuman Khandual WARN_ON(pte_write(pte_wrprotect(pte_mkwrite(pte)))); 116bb5c47ceSAnshuman Khandual WARN_ON(pte_dirty(pte_wrprotect(pte_mkclean(pte)))); 117bb5c47ceSAnshuman Khandual WARN_ON(!pte_dirty(pte_wrprotect(pte_mkdirty(pte)))); 118399145f9SAnshuman Khandual } 119399145f9SAnshuman Khandual 120a5c3b9ffSAnshuman Khandual static void __init pte_advanced_tests(struct mm_struct *mm, 121a5c3b9ffSAnshuman Khandual struct vm_area_struct *vma, pte_t *ptep, 122a5c3b9ffSAnshuman Khandual unsigned long pfn, unsigned long vaddr, 123a5c3b9ffSAnshuman Khandual pgprot_t prot) 124a5c3b9ffSAnshuman Khandual { 125b593b90dSShixin Liu pte_t pte; 126a5c3b9ffSAnshuman Khandual 127c3824e18SAneesh Kumar K.V /* 128c3824e18SAneesh Kumar K.V * Architectures optimize set_pte_at by avoiding TLB flush. 129c3824e18SAneesh Kumar K.V * This requires set_pte_at to be not used to update an 130c3824e18SAneesh Kumar K.V * existing pte entry. Clear pte before we do set_pte_at 131c3824e18SAneesh Kumar K.V */ 132c3824e18SAneesh Kumar K.V 1336315df41SAnshuman Khandual pr_debug("Validating PTE advanced\n"); 134a5c3b9ffSAnshuman Khandual pte = pfn_pte(pfn, prot); 135a5c3b9ffSAnshuman Khandual set_pte_at(mm, vaddr, ptep, pte); 136a5c3b9ffSAnshuman Khandual ptep_set_wrprotect(mm, vaddr, ptep); 137a5c3b9ffSAnshuman Khandual pte = ptep_get(ptep); 138a5c3b9ffSAnshuman Khandual WARN_ON(pte_write(pte)); 139a5c3b9ffSAnshuman Khandual ptep_get_and_clear(mm, vaddr, ptep); 140a5c3b9ffSAnshuman Khandual pte = ptep_get(ptep); 141a5c3b9ffSAnshuman Khandual WARN_ON(!pte_none(pte)); 142a5c3b9ffSAnshuman Khandual 143a5c3b9ffSAnshuman Khandual pte = pfn_pte(pfn, prot); 144a5c3b9ffSAnshuman Khandual pte = pte_wrprotect(pte); 145a5c3b9ffSAnshuman Khandual pte = pte_mkclean(pte); 146a5c3b9ffSAnshuman Khandual set_pte_at(mm, vaddr, ptep, pte); 147a5c3b9ffSAnshuman Khandual pte = pte_mkwrite(pte); 148a5c3b9ffSAnshuman Khandual pte = pte_mkdirty(pte); 149a5c3b9ffSAnshuman Khandual ptep_set_access_flags(vma, vaddr, ptep, pte, 1); 150a5c3b9ffSAnshuman Khandual pte = ptep_get(ptep); 151a5c3b9ffSAnshuman Khandual WARN_ON(!(pte_write(pte) && pte_dirty(pte))); 152a5c3b9ffSAnshuman Khandual ptep_get_and_clear_full(mm, vaddr, ptep, 1); 153a5c3b9ffSAnshuman Khandual pte = ptep_get(ptep); 154a5c3b9ffSAnshuman Khandual WARN_ON(!pte_none(pte)); 155a5c3b9ffSAnshuman Khandual 156c3824e18SAneesh Kumar K.V pte = pfn_pte(pfn, prot); 157a5c3b9ffSAnshuman Khandual pte = pte_mkyoung(pte); 158a5c3b9ffSAnshuman Khandual set_pte_at(mm, vaddr, ptep, pte); 159a5c3b9ffSAnshuman Khandual ptep_test_and_clear_young(vma, vaddr, ptep); 160a5c3b9ffSAnshuman Khandual pte = ptep_get(ptep); 161a5c3b9ffSAnshuman Khandual WARN_ON(pte_young(pte)); 162a5c3b9ffSAnshuman Khandual } 163a5c3b9ffSAnshuman Khandual 1648983d231SGavin Shan static void __init pte_savedwrite_tests(struct pgtable_debug_args *args) 165a5c3b9ffSAnshuman Khandual { 1668983d231SGavin Shan pte_t pte = pfn_pte(args->fixed_pte_pfn, args->page_prot_none); 167a5c3b9ffSAnshuman Khandual 1684200605bSAneesh Kumar K.V if (!IS_ENABLED(CONFIG_NUMA_BALANCING)) 1694200605bSAneesh Kumar K.V return; 1704200605bSAneesh Kumar K.V 1716315df41SAnshuman Khandual pr_debug("Validating PTE saved write\n"); 172a5c3b9ffSAnshuman Khandual WARN_ON(!pte_savedwrite(pte_mk_savedwrite(pte_clear_savedwrite(pte)))); 173a5c3b9ffSAnshuman Khandual WARN_ON(pte_savedwrite(pte_clear_savedwrite(pte_mk_savedwrite(pte)))); 174a5c3b9ffSAnshuman Khandual } 1754200605bSAneesh Kumar K.V 176399145f9SAnshuman Khandual #ifdef CONFIG_TRANSPARENT_HUGEPAGE 17736b77d1eSGavin Shan static void __init pmd_basic_tests(struct pgtable_debug_args *args, int idx) 178399145f9SAnshuman Khandual { 1792e326c07SAnshuman Khandual pgprot_t prot = protection_map[idx]; 1802e326c07SAnshuman Khandual unsigned long val = idx, *ptr = &val; 18165ac1a60SAnshuman Khandual pmd_t pmd; 182399145f9SAnshuman Khandual 183787d563bSAneesh Kumar K.V if (!has_transparent_hugepage()) 184787d563bSAneesh Kumar K.V return; 185787d563bSAneesh Kumar K.V 1862e326c07SAnshuman Khandual pr_debug("Validating PMD basic (%pGv)\n", ptr); 18736b77d1eSGavin Shan pmd = pfn_pmd(args->fixed_pmd_pfn, prot); 188bb5c47ceSAnshuman Khandual 189bb5c47ceSAnshuman Khandual /* 190bb5c47ceSAnshuman Khandual * This test needs to be executed after the given page table entry 191bb5c47ceSAnshuman Khandual * is created with pfn_pmd() to make sure that protection_map[idx] 192bb5c47ceSAnshuman Khandual * does not have the dirty bit enabled from the beginning. This is 193bb5c47ceSAnshuman Khandual * important for platforms like arm64 where (!PTE_RDONLY) indicate 194bb5c47ceSAnshuman Khandual * dirty bit being set. 195bb5c47ceSAnshuman Khandual */ 196bb5c47ceSAnshuman Khandual WARN_ON(pmd_dirty(pmd_wrprotect(pmd))); 197bb5c47ceSAnshuman Khandual 198bb5c47ceSAnshuman Khandual 199399145f9SAnshuman Khandual WARN_ON(!pmd_same(pmd, pmd)); 200399145f9SAnshuman Khandual WARN_ON(!pmd_young(pmd_mkyoung(pmd_mkold(pmd)))); 201399145f9SAnshuman Khandual WARN_ON(!pmd_dirty(pmd_mkdirty(pmd_mkclean(pmd)))); 202399145f9SAnshuman Khandual WARN_ON(!pmd_write(pmd_mkwrite(pmd_wrprotect(pmd)))); 203399145f9SAnshuman Khandual WARN_ON(pmd_young(pmd_mkold(pmd_mkyoung(pmd)))); 204399145f9SAnshuman Khandual WARN_ON(pmd_dirty(pmd_mkclean(pmd_mkdirty(pmd)))); 205399145f9SAnshuman Khandual WARN_ON(pmd_write(pmd_wrprotect(pmd_mkwrite(pmd)))); 206bb5c47ceSAnshuman Khandual WARN_ON(pmd_dirty(pmd_wrprotect(pmd_mkclean(pmd)))); 207bb5c47ceSAnshuman Khandual WARN_ON(!pmd_dirty(pmd_wrprotect(pmd_mkdirty(pmd)))); 208399145f9SAnshuman Khandual /* 209399145f9SAnshuman Khandual * A huge page does not point to next level page table 210399145f9SAnshuman Khandual * entry. Hence this must qualify as pmd_bad(). 211399145f9SAnshuman Khandual */ 212399145f9SAnshuman Khandual WARN_ON(!pmd_bad(pmd_mkhuge(pmd))); 213399145f9SAnshuman Khandual } 214399145f9SAnshuman Khandual 215a5c3b9ffSAnshuman Khandual static void __init pmd_advanced_tests(struct mm_struct *mm, 216a5c3b9ffSAnshuman Khandual struct vm_area_struct *vma, pmd_t *pmdp, 217a5c3b9ffSAnshuman Khandual unsigned long pfn, unsigned long vaddr, 21887f34986SAneesh Kumar K.V pgprot_t prot, pgtable_t pgtable) 219a5c3b9ffSAnshuman Khandual { 22065ac1a60SAnshuman Khandual pmd_t pmd; 221a5c3b9ffSAnshuman Khandual 222a5c3b9ffSAnshuman Khandual if (!has_transparent_hugepage()) 223a5c3b9ffSAnshuman Khandual return; 224a5c3b9ffSAnshuman Khandual 2256315df41SAnshuman Khandual pr_debug("Validating PMD advanced\n"); 226a5c3b9ffSAnshuman Khandual /* Align the address wrt HPAGE_PMD_SIZE */ 22704f7ce3fSGerald Schaefer vaddr &= HPAGE_PMD_MASK; 228a5c3b9ffSAnshuman Khandual 22987f34986SAneesh Kumar K.V pgtable_trans_huge_deposit(mm, pmdp, pgtable); 23087f34986SAneesh Kumar K.V 231a5c3b9ffSAnshuman Khandual pmd = pfn_pmd(pfn, prot); 232a5c3b9ffSAnshuman Khandual set_pmd_at(mm, vaddr, pmdp, pmd); 233a5c3b9ffSAnshuman Khandual pmdp_set_wrprotect(mm, vaddr, pmdp); 234a5c3b9ffSAnshuman Khandual pmd = READ_ONCE(*pmdp); 235a5c3b9ffSAnshuman Khandual WARN_ON(pmd_write(pmd)); 236a5c3b9ffSAnshuman Khandual pmdp_huge_get_and_clear(mm, vaddr, pmdp); 237a5c3b9ffSAnshuman Khandual pmd = READ_ONCE(*pmdp); 238a5c3b9ffSAnshuman Khandual WARN_ON(!pmd_none(pmd)); 239a5c3b9ffSAnshuman Khandual 240a5c3b9ffSAnshuman Khandual pmd = pfn_pmd(pfn, prot); 241a5c3b9ffSAnshuman Khandual pmd = pmd_wrprotect(pmd); 242a5c3b9ffSAnshuman Khandual pmd = pmd_mkclean(pmd); 243a5c3b9ffSAnshuman Khandual set_pmd_at(mm, vaddr, pmdp, pmd); 244a5c3b9ffSAnshuman Khandual pmd = pmd_mkwrite(pmd); 245a5c3b9ffSAnshuman Khandual pmd = pmd_mkdirty(pmd); 246a5c3b9ffSAnshuman Khandual pmdp_set_access_flags(vma, vaddr, pmdp, pmd, 1); 247a5c3b9ffSAnshuman Khandual pmd = READ_ONCE(*pmdp); 248a5c3b9ffSAnshuman Khandual WARN_ON(!(pmd_write(pmd) && pmd_dirty(pmd))); 249a5c3b9ffSAnshuman Khandual pmdp_huge_get_and_clear_full(vma, vaddr, pmdp, 1); 250a5c3b9ffSAnshuman Khandual pmd = READ_ONCE(*pmdp); 251a5c3b9ffSAnshuman Khandual WARN_ON(!pmd_none(pmd)); 252a5c3b9ffSAnshuman Khandual 253c3824e18SAneesh Kumar K.V pmd = pmd_mkhuge(pfn_pmd(pfn, prot)); 254a5c3b9ffSAnshuman Khandual pmd = pmd_mkyoung(pmd); 255a5c3b9ffSAnshuman Khandual set_pmd_at(mm, vaddr, pmdp, pmd); 256a5c3b9ffSAnshuman Khandual pmdp_test_and_clear_young(vma, vaddr, pmdp); 257a5c3b9ffSAnshuman Khandual pmd = READ_ONCE(*pmdp); 258a5c3b9ffSAnshuman Khandual WARN_ON(pmd_young(pmd)); 25987f34986SAneesh Kumar K.V 26013af0506SAneesh Kumar K.V /* Clear the pte entries */ 26113af0506SAneesh Kumar K.V pmdp_huge_get_and_clear(mm, vaddr, pmdp); 26287f34986SAneesh Kumar K.V pgtable = pgtable_trans_huge_withdraw(mm, pmdp); 263a5c3b9ffSAnshuman Khandual } 264a5c3b9ffSAnshuman Khandual 2658983d231SGavin Shan static void __init pmd_leaf_tests(struct pgtable_debug_args *args) 266a5c3b9ffSAnshuman Khandual { 26765ac1a60SAnshuman Khandual pmd_t pmd; 26865ac1a60SAnshuman Khandual 26965ac1a60SAnshuman Khandual if (!has_transparent_hugepage()) 27065ac1a60SAnshuman Khandual return; 271a5c3b9ffSAnshuman Khandual 2726315df41SAnshuman Khandual pr_debug("Validating PMD leaf\n"); 2738983d231SGavin Shan pmd = pfn_pmd(args->fixed_pmd_pfn, args->page_prot); 27465ac1a60SAnshuman Khandual 275a5c3b9ffSAnshuman Khandual /* 276a5c3b9ffSAnshuman Khandual * PMD based THP is a leaf entry. 277a5c3b9ffSAnshuman Khandual */ 278a5c3b9ffSAnshuman Khandual pmd = pmd_mkhuge(pmd); 279a5c3b9ffSAnshuman Khandual WARN_ON(!pmd_leaf(pmd)); 280a5c3b9ffSAnshuman Khandual } 281a5c3b9ffSAnshuman Khandual 2828983d231SGavin Shan static void __init pmd_savedwrite_tests(struct pgtable_debug_args *args) 283a5c3b9ffSAnshuman Khandual { 28465ac1a60SAnshuman Khandual pmd_t pmd; 285a5c3b9ffSAnshuman Khandual 2864200605bSAneesh Kumar K.V if (!IS_ENABLED(CONFIG_NUMA_BALANCING)) 2874200605bSAneesh Kumar K.V return; 2884200605bSAneesh Kumar K.V 28965ac1a60SAnshuman Khandual if (!has_transparent_hugepage()) 29065ac1a60SAnshuman Khandual return; 29165ac1a60SAnshuman Khandual 2926315df41SAnshuman Khandual pr_debug("Validating PMD saved write\n"); 2938983d231SGavin Shan pmd = pfn_pmd(args->fixed_pmd_pfn, args->page_prot_none); 294a5c3b9ffSAnshuman Khandual WARN_ON(!pmd_savedwrite(pmd_mk_savedwrite(pmd_clear_savedwrite(pmd)))); 295a5c3b9ffSAnshuman Khandual WARN_ON(pmd_savedwrite(pmd_clear_savedwrite(pmd_mk_savedwrite(pmd)))); 296a5c3b9ffSAnshuman Khandual } 297a5c3b9ffSAnshuman Khandual 298399145f9SAnshuman Khandual #ifdef CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD 29936b77d1eSGavin Shan static void __init pud_basic_tests(struct pgtable_debug_args *args, int idx) 300399145f9SAnshuman Khandual { 3012e326c07SAnshuman Khandual pgprot_t prot = protection_map[idx]; 3022e326c07SAnshuman Khandual unsigned long val = idx, *ptr = &val; 30365ac1a60SAnshuman Khandual pud_t pud; 304399145f9SAnshuman Khandual 305787d563bSAneesh Kumar K.V if (!has_transparent_hugepage()) 306787d563bSAneesh Kumar K.V return; 307787d563bSAneesh Kumar K.V 3082e326c07SAnshuman Khandual pr_debug("Validating PUD basic (%pGv)\n", ptr); 30936b77d1eSGavin Shan pud = pfn_pud(args->fixed_pud_pfn, prot); 310bb5c47ceSAnshuman Khandual 311bb5c47ceSAnshuman Khandual /* 312bb5c47ceSAnshuman Khandual * This test needs to be executed after the given page table entry 313bb5c47ceSAnshuman Khandual * is created with pfn_pud() to make sure that protection_map[idx] 314bb5c47ceSAnshuman Khandual * does not have the dirty bit enabled from the beginning. This is 315bb5c47ceSAnshuman Khandual * important for platforms like arm64 where (!PTE_RDONLY) indicate 316bb5c47ceSAnshuman Khandual * dirty bit being set. 317bb5c47ceSAnshuman Khandual */ 318bb5c47ceSAnshuman Khandual WARN_ON(pud_dirty(pud_wrprotect(pud))); 319bb5c47ceSAnshuman Khandual 320399145f9SAnshuman Khandual WARN_ON(!pud_same(pud, pud)); 321399145f9SAnshuman Khandual WARN_ON(!pud_young(pud_mkyoung(pud_mkold(pud)))); 322bb5c47ceSAnshuman Khandual WARN_ON(!pud_dirty(pud_mkdirty(pud_mkclean(pud)))); 323bb5c47ceSAnshuman Khandual WARN_ON(pud_dirty(pud_mkclean(pud_mkdirty(pud)))); 324399145f9SAnshuman Khandual WARN_ON(!pud_write(pud_mkwrite(pud_wrprotect(pud)))); 325399145f9SAnshuman Khandual WARN_ON(pud_write(pud_wrprotect(pud_mkwrite(pud)))); 326399145f9SAnshuman Khandual WARN_ON(pud_young(pud_mkold(pud_mkyoung(pud)))); 327bb5c47ceSAnshuman Khandual WARN_ON(pud_dirty(pud_wrprotect(pud_mkclean(pud)))); 328bb5c47ceSAnshuman Khandual WARN_ON(!pud_dirty(pud_wrprotect(pud_mkdirty(pud)))); 329399145f9SAnshuman Khandual 33036b77d1eSGavin Shan if (mm_pmd_folded(args->mm)) 331399145f9SAnshuman Khandual return; 332399145f9SAnshuman Khandual 333399145f9SAnshuman Khandual /* 334399145f9SAnshuman Khandual * A huge page does not point to next level page table 335399145f9SAnshuman Khandual * entry. Hence this must qualify as pud_bad(). 336399145f9SAnshuman Khandual */ 337399145f9SAnshuman Khandual WARN_ON(!pud_bad(pud_mkhuge(pud))); 338399145f9SAnshuman Khandual } 339a5c3b9ffSAnshuman Khandual 340a5c3b9ffSAnshuman Khandual static void __init pud_advanced_tests(struct mm_struct *mm, 341a5c3b9ffSAnshuman Khandual struct vm_area_struct *vma, pud_t *pudp, 342a5c3b9ffSAnshuman Khandual unsigned long pfn, unsigned long vaddr, 343a5c3b9ffSAnshuman Khandual pgprot_t prot) 344a5c3b9ffSAnshuman Khandual { 34565ac1a60SAnshuman Khandual pud_t pud; 346a5c3b9ffSAnshuman Khandual 347a5c3b9ffSAnshuman Khandual if (!has_transparent_hugepage()) 348a5c3b9ffSAnshuman Khandual return; 349a5c3b9ffSAnshuman Khandual 3506315df41SAnshuman Khandual pr_debug("Validating PUD advanced\n"); 351a5c3b9ffSAnshuman Khandual /* Align the address wrt HPAGE_PUD_SIZE */ 35204f7ce3fSGerald Schaefer vaddr &= HPAGE_PUD_MASK; 353a5c3b9ffSAnshuman Khandual 35465ac1a60SAnshuman Khandual pud = pfn_pud(pfn, prot); 355a5c3b9ffSAnshuman Khandual set_pud_at(mm, vaddr, pudp, pud); 356a5c3b9ffSAnshuman Khandual pudp_set_wrprotect(mm, vaddr, pudp); 357a5c3b9ffSAnshuman Khandual pud = READ_ONCE(*pudp); 358a5c3b9ffSAnshuman Khandual WARN_ON(pud_write(pud)); 359a5c3b9ffSAnshuman Khandual 360a5c3b9ffSAnshuman Khandual #ifndef __PAGETABLE_PMD_FOLDED 361a5c3b9ffSAnshuman Khandual pudp_huge_get_and_clear(mm, vaddr, pudp); 362a5c3b9ffSAnshuman Khandual pud = READ_ONCE(*pudp); 363a5c3b9ffSAnshuman Khandual WARN_ON(!pud_none(pud)); 364a5c3b9ffSAnshuman Khandual #endif /* __PAGETABLE_PMD_FOLDED */ 365a5c3b9ffSAnshuman Khandual pud = pfn_pud(pfn, prot); 366a5c3b9ffSAnshuman Khandual pud = pud_wrprotect(pud); 367a5c3b9ffSAnshuman Khandual pud = pud_mkclean(pud); 368a5c3b9ffSAnshuman Khandual set_pud_at(mm, vaddr, pudp, pud); 369a5c3b9ffSAnshuman Khandual pud = pud_mkwrite(pud); 370a5c3b9ffSAnshuman Khandual pud = pud_mkdirty(pud); 371a5c3b9ffSAnshuman Khandual pudp_set_access_flags(vma, vaddr, pudp, pud, 1); 372a5c3b9ffSAnshuman Khandual pud = READ_ONCE(*pudp); 373a5c3b9ffSAnshuman Khandual WARN_ON(!(pud_write(pud) && pud_dirty(pud))); 374a5c3b9ffSAnshuman Khandual 375c3824e18SAneesh Kumar K.V #ifndef __PAGETABLE_PMD_FOLDED 376c3824e18SAneesh Kumar K.V pudp_huge_get_and_clear_full(mm, vaddr, pudp, 1); 377c3824e18SAneesh Kumar K.V pud = READ_ONCE(*pudp); 378c3824e18SAneesh Kumar K.V WARN_ON(!pud_none(pud)); 379c3824e18SAneesh Kumar K.V #endif /* __PAGETABLE_PMD_FOLDED */ 380c3824e18SAneesh Kumar K.V 381c3824e18SAneesh Kumar K.V pud = pfn_pud(pfn, prot); 382a5c3b9ffSAnshuman Khandual pud = pud_mkyoung(pud); 383a5c3b9ffSAnshuman Khandual set_pud_at(mm, vaddr, pudp, pud); 384a5c3b9ffSAnshuman Khandual pudp_test_and_clear_young(vma, vaddr, pudp); 385a5c3b9ffSAnshuman Khandual pud = READ_ONCE(*pudp); 386a5c3b9ffSAnshuman Khandual WARN_ON(pud_young(pud)); 38713af0506SAneesh Kumar K.V 38813af0506SAneesh Kumar K.V pudp_huge_get_and_clear(mm, vaddr, pudp); 389a5c3b9ffSAnshuman Khandual } 390a5c3b9ffSAnshuman Khandual 3918983d231SGavin Shan static void __init pud_leaf_tests(struct pgtable_debug_args *args) 392a5c3b9ffSAnshuman Khandual { 39365ac1a60SAnshuman Khandual pud_t pud; 39465ac1a60SAnshuman Khandual 39565ac1a60SAnshuman Khandual if (!has_transparent_hugepage()) 39665ac1a60SAnshuman Khandual return; 397a5c3b9ffSAnshuman Khandual 3986315df41SAnshuman Khandual pr_debug("Validating PUD leaf\n"); 3998983d231SGavin Shan pud = pfn_pud(args->fixed_pud_pfn, args->page_prot); 400a5c3b9ffSAnshuman Khandual /* 401a5c3b9ffSAnshuman Khandual * PUD based THP is a leaf entry. 402a5c3b9ffSAnshuman Khandual */ 403a5c3b9ffSAnshuman Khandual pud = pud_mkhuge(pud); 404a5c3b9ffSAnshuman Khandual WARN_ON(!pud_leaf(pud)); 405a5c3b9ffSAnshuman Khandual } 4065fe77be6SShixin Liu #else /* !CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD */ 40736b77d1eSGavin Shan static void __init pud_basic_tests(struct pgtable_debug_args *args, int idx) { } 4085fe77be6SShixin Liu static void __init pud_advanced_tests(struct mm_struct *mm, 4095fe77be6SShixin Liu struct vm_area_struct *vma, pud_t *pudp, 4105fe77be6SShixin Liu unsigned long pfn, unsigned long vaddr, 4115fe77be6SShixin Liu pgprot_t prot) 4125fe77be6SShixin Liu { 4135fe77be6SShixin Liu } 4148983d231SGavin Shan static void __init pud_leaf_tests(struct pgtable_debug_args *args) { } 4155fe77be6SShixin Liu #endif /* CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD */ 4165fe77be6SShixin Liu #else /* !CONFIG_TRANSPARENT_HUGEPAGE */ 41736b77d1eSGavin Shan static void __init pmd_basic_tests(struct pgtable_debug_args *args, int idx) { } 41836b77d1eSGavin Shan static void __init pud_basic_tests(struct pgtable_debug_args *args, int idx) { } 4195fe77be6SShixin Liu static void __init pmd_advanced_tests(struct mm_struct *mm, 4205fe77be6SShixin Liu struct vm_area_struct *vma, pmd_t *pmdp, 4215fe77be6SShixin Liu unsigned long pfn, unsigned long vaddr, 4225fe77be6SShixin Liu pgprot_t prot, pgtable_t pgtable) 4235fe77be6SShixin Liu { 4245fe77be6SShixin Liu } 4255fe77be6SShixin Liu static void __init pud_advanced_tests(struct mm_struct *mm, 4265fe77be6SShixin Liu struct vm_area_struct *vma, pud_t *pudp, 4275fe77be6SShixin Liu unsigned long pfn, unsigned long vaddr, 4285fe77be6SShixin Liu pgprot_t prot) 4295fe77be6SShixin Liu { 4305fe77be6SShixin Liu } 4318983d231SGavin Shan static void __init pmd_leaf_tests(struct pgtable_debug_args *args) { } 4328983d231SGavin Shan static void __init pud_leaf_tests(struct pgtable_debug_args *args) { } 4338983d231SGavin Shan static void __init pmd_savedwrite_tests(struct pgtable_debug_args *args) { } 4345fe77be6SShixin Liu #endif /* CONFIG_TRANSPARENT_HUGEPAGE */ 435a5c3b9ffSAnshuman Khandual 43685a14463SAneesh Kumar K.V #ifdef CONFIG_HAVE_ARCH_HUGE_VMAP 4375fe77be6SShixin Liu static void __init pmd_huge_tests(pmd_t *pmdp, unsigned long pfn, pgprot_t prot) 4385fe77be6SShixin Liu { 4395fe77be6SShixin Liu pmd_t pmd; 4405fe77be6SShixin Liu 4415fe77be6SShixin Liu if (!arch_vmap_pmd_supported(prot)) 4425fe77be6SShixin Liu return; 4435fe77be6SShixin Liu 4445fe77be6SShixin Liu pr_debug("Validating PMD huge\n"); 4455fe77be6SShixin Liu /* 4465fe77be6SShixin Liu * X86 defined pmd_set_huge() verifies that the given 4475fe77be6SShixin Liu * PMD is not a populated non-leaf entry. 4485fe77be6SShixin Liu */ 4495fe77be6SShixin Liu WRITE_ONCE(*pmdp, __pmd(0)); 4505fe77be6SShixin Liu WARN_ON(!pmd_set_huge(pmdp, __pfn_to_phys(pfn), prot)); 4515fe77be6SShixin Liu WARN_ON(!pmd_clear_huge(pmdp)); 4525fe77be6SShixin Liu pmd = READ_ONCE(*pmdp); 4535fe77be6SShixin Liu WARN_ON(!pmd_none(pmd)); 4545fe77be6SShixin Liu } 4555fe77be6SShixin Liu 456a5c3b9ffSAnshuman Khandual static void __init pud_huge_tests(pud_t *pudp, unsigned long pfn, pgprot_t prot) 457a5c3b9ffSAnshuman Khandual { 458a5c3b9ffSAnshuman Khandual pud_t pud; 459a5c3b9ffSAnshuman Khandual 460bbc180a5SNicholas Piggin if (!arch_vmap_pud_supported(prot)) 461a5c3b9ffSAnshuman Khandual return; 4626315df41SAnshuman Khandual 4636315df41SAnshuman Khandual pr_debug("Validating PUD huge\n"); 464a5c3b9ffSAnshuman Khandual /* 465a5c3b9ffSAnshuman Khandual * X86 defined pud_set_huge() verifies that the given 466a5c3b9ffSAnshuman Khandual * PUD is not a populated non-leaf entry. 467a5c3b9ffSAnshuman Khandual */ 468a5c3b9ffSAnshuman Khandual WRITE_ONCE(*pudp, __pud(0)); 469a5c3b9ffSAnshuman Khandual WARN_ON(!pud_set_huge(pudp, __pfn_to_phys(pfn), prot)); 470a5c3b9ffSAnshuman Khandual WARN_ON(!pud_clear_huge(pudp)); 471a5c3b9ffSAnshuman Khandual pud = READ_ONCE(*pudp); 472a5c3b9ffSAnshuman Khandual WARN_ON(!pud_none(pud)); 473a5c3b9ffSAnshuman Khandual } 47485a14463SAneesh Kumar K.V #else /* !CONFIG_HAVE_ARCH_HUGE_VMAP */ 4755fe77be6SShixin Liu static void __init pmd_huge_tests(pmd_t *pmdp, unsigned long pfn, pgprot_t prot) { } 47685a14463SAneesh Kumar K.V static void __init pud_huge_tests(pud_t *pudp, unsigned long pfn, pgprot_t prot) { } 4775fe77be6SShixin Liu #endif /* CONFIG_HAVE_ARCH_HUGE_VMAP */ 478399145f9SAnshuman Khandual 47936b77d1eSGavin Shan static void __init p4d_basic_tests(struct pgtable_debug_args *args) 480399145f9SAnshuman Khandual { 481399145f9SAnshuman Khandual p4d_t p4d; 482399145f9SAnshuman Khandual 4836315df41SAnshuman Khandual pr_debug("Validating P4D basic\n"); 484399145f9SAnshuman Khandual memset(&p4d, RANDOM_NZVALUE, sizeof(p4d_t)); 485399145f9SAnshuman Khandual WARN_ON(!p4d_same(p4d, p4d)); 486399145f9SAnshuman Khandual } 487399145f9SAnshuman Khandual 48836b77d1eSGavin Shan static void __init pgd_basic_tests(struct pgtable_debug_args *args) 489399145f9SAnshuman Khandual { 490399145f9SAnshuman Khandual pgd_t pgd; 491399145f9SAnshuman Khandual 4926315df41SAnshuman Khandual pr_debug("Validating PGD basic\n"); 493399145f9SAnshuman Khandual memset(&pgd, RANDOM_NZVALUE, sizeof(pgd_t)); 494399145f9SAnshuman Khandual WARN_ON(!pgd_same(pgd, pgd)); 495399145f9SAnshuman Khandual } 496399145f9SAnshuman Khandual 497399145f9SAnshuman Khandual #ifndef __PAGETABLE_PUD_FOLDED 498399145f9SAnshuman Khandual static void __init pud_clear_tests(struct mm_struct *mm, pud_t *pudp) 499399145f9SAnshuman Khandual { 500399145f9SAnshuman Khandual pud_t pud = READ_ONCE(*pudp); 501399145f9SAnshuman Khandual 502399145f9SAnshuman Khandual if (mm_pmd_folded(mm)) 503399145f9SAnshuman Khandual return; 504399145f9SAnshuman Khandual 5056315df41SAnshuman Khandual pr_debug("Validating PUD clear\n"); 506399145f9SAnshuman Khandual pud = __pud(pud_val(pud) | RANDOM_ORVALUE); 507399145f9SAnshuman Khandual WRITE_ONCE(*pudp, pud); 508399145f9SAnshuman Khandual pud_clear(pudp); 509399145f9SAnshuman Khandual pud = READ_ONCE(*pudp); 510399145f9SAnshuman Khandual WARN_ON(!pud_none(pud)); 511399145f9SAnshuman Khandual } 512399145f9SAnshuman Khandual 513399145f9SAnshuman Khandual static void __init pud_populate_tests(struct mm_struct *mm, pud_t *pudp, 514399145f9SAnshuman Khandual pmd_t *pmdp) 515399145f9SAnshuman Khandual { 516399145f9SAnshuman Khandual pud_t pud; 517399145f9SAnshuman Khandual 518399145f9SAnshuman Khandual if (mm_pmd_folded(mm)) 519399145f9SAnshuman Khandual return; 5206315df41SAnshuman Khandual 5216315df41SAnshuman Khandual pr_debug("Validating PUD populate\n"); 522399145f9SAnshuman Khandual /* 523399145f9SAnshuman Khandual * This entry points to next level page table page. 524399145f9SAnshuman Khandual * Hence this must not qualify as pud_bad(). 525399145f9SAnshuman Khandual */ 526399145f9SAnshuman Khandual pud_populate(mm, pudp, pmdp); 527399145f9SAnshuman Khandual pud = READ_ONCE(*pudp); 528399145f9SAnshuman Khandual WARN_ON(pud_bad(pud)); 529399145f9SAnshuman Khandual } 530399145f9SAnshuman Khandual #else /* !__PAGETABLE_PUD_FOLDED */ 531399145f9SAnshuman Khandual static void __init pud_clear_tests(struct mm_struct *mm, pud_t *pudp) { } 532399145f9SAnshuman Khandual static void __init pud_populate_tests(struct mm_struct *mm, pud_t *pudp, 533399145f9SAnshuman Khandual pmd_t *pmdp) 534399145f9SAnshuman Khandual { 535399145f9SAnshuman Khandual } 536399145f9SAnshuman Khandual #endif /* PAGETABLE_PUD_FOLDED */ 537399145f9SAnshuman Khandual 538399145f9SAnshuman Khandual #ifndef __PAGETABLE_P4D_FOLDED 539399145f9SAnshuman Khandual static void __init p4d_clear_tests(struct mm_struct *mm, p4d_t *p4dp) 540399145f9SAnshuman Khandual { 541399145f9SAnshuman Khandual p4d_t p4d = READ_ONCE(*p4dp); 542399145f9SAnshuman Khandual 543399145f9SAnshuman Khandual if (mm_pud_folded(mm)) 544399145f9SAnshuman Khandual return; 545399145f9SAnshuman Khandual 5466315df41SAnshuman Khandual pr_debug("Validating P4D clear\n"); 547399145f9SAnshuman Khandual p4d = __p4d(p4d_val(p4d) | RANDOM_ORVALUE); 548399145f9SAnshuman Khandual WRITE_ONCE(*p4dp, p4d); 549399145f9SAnshuman Khandual p4d_clear(p4dp); 550399145f9SAnshuman Khandual p4d = READ_ONCE(*p4dp); 551399145f9SAnshuman Khandual WARN_ON(!p4d_none(p4d)); 552399145f9SAnshuman Khandual } 553399145f9SAnshuman Khandual 554399145f9SAnshuman Khandual static void __init p4d_populate_tests(struct mm_struct *mm, p4d_t *p4dp, 555399145f9SAnshuman Khandual pud_t *pudp) 556399145f9SAnshuman Khandual { 557399145f9SAnshuman Khandual p4d_t p4d; 558399145f9SAnshuman Khandual 559399145f9SAnshuman Khandual if (mm_pud_folded(mm)) 560399145f9SAnshuman Khandual return; 561399145f9SAnshuman Khandual 5626315df41SAnshuman Khandual pr_debug("Validating P4D populate\n"); 563399145f9SAnshuman Khandual /* 564399145f9SAnshuman Khandual * This entry points to next level page table page. 565399145f9SAnshuman Khandual * Hence this must not qualify as p4d_bad(). 566399145f9SAnshuman Khandual */ 567399145f9SAnshuman Khandual pud_clear(pudp); 568399145f9SAnshuman Khandual p4d_clear(p4dp); 569399145f9SAnshuman Khandual p4d_populate(mm, p4dp, pudp); 570399145f9SAnshuman Khandual p4d = READ_ONCE(*p4dp); 571399145f9SAnshuman Khandual WARN_ON(p4d_bad(p4d)); 572399145f9SAnshuman Khandual } 573399145f9SAnshuman Khandual 574399145f9SAnshuman Khandual static void __init pgd_clear_tests(struct mm_struct *mm, pgd_t *pgdp) 575399145f9SAnshuman Khandual { 576399145f9SAnshuman Khandual pgd_t pgd = READ_ONCE(*pgdp); 577399145f9SAnshuman Khandual 578399145f9SAnshuman Khandual if (mm_p4d_folded(mm)) 579399145f9SAnshuman Khandual return; 580399145f9SAnshuman Khandual 5816315df41SAnshuman Khandual pr_debug("Validating PGD clear\n"); 582399145f9SAnshuman Khandual pgd = __pgd(pgd_val(pgd) | RANDOM_ORVALUE); 583399145f9SAnshuman Khandual WRITE_ONCE(*pgdp, pgd); 584399145f9SAnshuman Khandual pgd_clear(pgdp); 585399145f9SAnshuman Khandual pgd = READ_ONCE(*pgdp); 586399145f9SAnshuman Khandual WARN_ON(!pgd_none(pgd)); 587399145f9SAnshuman Khandual } 588399145f9SAnshuman Khandual 589399145f9SAnshuman Khandual static void __init pgd_populate_tests(struct mm_struct *mm, pgd_t *pgdp, 590399145f9SAnshuman Khandual p4d_t *p4dp) 591399145f9SAnshuman Khandual { 592399145f9SAnshuman Khandual pgd_t pgd; 593399145f9SAnshuman Khandual 594399145f9SAnshuman Khandual if (mm_p4d_folded(mm)) 595399145f9SAnshuman Khandual return; 596399145f9SAnshuman Khandual 5976315df41SAnshuman Khandual pr_debug("Validating PGD populate\n"); 598399145f9SAnshuman Khandual /* 599399145f9SAnshuman Khandual * This entry points to next level page table page. 600399145f9SAnshuman Khandual * Hence this must not qualify as pgd_bad(). 601399145f9SAnshuman Khandual */ 602399145f9SAnshuman Khandual p4d_clear(p4dp); 603399145f9SAnshuman Khandual pgd_clear(pgdp); 604399145f9SAnshuman Khandual pgd_populate(mm, pgdp, p4dp); 605399145f9SAnshuman Khandual pgd = READ_ONCE(*pgdp); 606399145f9SAnshuman Khandual WARN_ON(pgd_bad(pgd)); 607399145f9SAnshuman Khandual } 608399145f9SAnshuman Khandual #else /* !__PAGETABLE_P4D_FOLDED */ 609399145f9SAnshuman Khandual static void __init p4d_clear_tests(struct mm_struct *mm, p4d_t *p4dp) { } 610399145f9SAnshuman Khandual static void __init pgd_clear_tests(struct mm_struct *mm, pgd_t *pgdp) { } 611399145f9SAnshuman Khandual static void __init p4d_populate_tests(struct mm_struct *mm, p4d_t *p4dp, 612399145f9SAnshuman Khandual pud_t *pudp) 613399145f9SAnshuman Khandual { 614399145f9SAnshuman Khandual } 615399145f9SAnshuman Khandual static void __init pgd_populate_tests(struct mm_struct *mm, pgd_t *pgdp, 616399145f9SAnshuman Khandual p4d_t *p4dp) 617399145f9SAnshuman Khandual { 618399145f9SAnshuman Khandual } 619399145f9SAnshuman Khandual #endif /* PAGETABLE_P4D_FOLDED */ 620399145f9SAnshuman Khandual 621399145f9SAnshuman Khandual static void __init pte_clear_tests(struct mm_struct *mm, pte_t *ptep, 622401035d5SAneesh Kumar K.V unsigned long pfn, unsigned long vaddr, 623401035d5SAneesh Kumar K.V pgprot_t prot) 624399145f9SAnshuman Khandual { 625401035d5SAneesh Kumar K.V pte_t pte = pfn_pte(pfn, prot); 626399145f9SAnshuman Khandual 6276315df41SAnshuman Khandual pr_debug("Validating PTE clear\n"); 628401035d5SAneesh Kumar K.V #ifndef CONFIG_RISCV 629399145f9SAnshuman Khandual pte = __pte(pte_val(pte) | RANDOM_ORVALUE); 630401035d5SAneesh Kumar K.V #endif 631399145f9SAnshuman Khandual set_pte_at(mm, vaddr, ptep, pte); 632399145f9SAnshuman Khandual barrier(); 633399145f9SAnshuman Khandual pte_clear(mm, vaddr, ptep); 6349449c9cbSChristophe Leroy pte = ptep_get(ptep); 635399145f9SAnshuman Khandual WARN_ON(!pte_none(pte)); 636399145f9SAnshuman Khandual } 637399145f9SAnshuman Khandual 638399145f9SAnshuman Khandual static void __init pmd_clear_tests(struct mm_struct *mm, pmd_t *pmdp) 639399145f9SAnshuman Khandual { 640399145f9SAnshuman Khandual pmd_t pmd = READ_ONCE(*pmdp); 641399145f9SAnshuman Khandual 6426315df41SAnshuman Khandual pr_debug("Validating PMD clear\n"); 643399145f9SAnshuman Khandual pmd = __pmd(pmd_val(pmd) | RANDOM_ORVALUE); 644399145f9SAnshuman Khandual WRITE_ONCE(*pmdp, pmd); 645399145f9SAnshuman Khandual pmd_clear(pmdp); 646399145f9SAnshuman Khandual pmd = READ_ONCE(*pmdp); 647399145f9SAnshuman Khandual WARN_ON(!pmd_none(pmd)); 648399145f9SAnshuman Khandual } 649399145f9SAnshuman Khandual 650399145f9SAnshuman Khandual static void __init pmd_populate_tests(struct mm_struct *mm, pmd_t *pmdp, 651399145f9SAnshuman Khandual pgtable_t pgtable) 652399145f9SAnshuman Khandual { 653399145f9SAnshuman Khandual pmd_t pmd; 654399145f9SAnshuman Khandual 6556315df41SAnshuman Khandual pr_debug("Validating PMD populate\n"); 656399145f9SAnshuman Khandual /* 657399145f9SAnshuman Khandual * This entry points to next level page table page. 658399145f9SAnshuman Khandual * Hence this must not qualify as pmd_bad(). 659399145f9SAnshuman Khandual */ 660399145f9SAnshuman Khandual pmd_populate(mm, pmdp, pgtable); 661399145f9SAnshuman Khandual pmd = READ_ONCE(*pmdp); 662399145f9SAnshuman Khandual WARN_ON(pmd_bad(pmd)); 663399145f9SAnshuman Khandual } 664399145f9SAnshuman Khandual 6658cb183f2SGavin Shan static void __init pte_special_tests(struct pgtable_debug_args *args) 66605289402SAnshuman Khandual { 6678cb183f2SGavin Shan pte_t pte = pfn_pte(args->fixed_pte_pfn, args->page_prot); 66805289402SAnshuman Khandual 66905289402SAnshuman Khandual if (!IS_ENABLED(CONFIG_ARCH_HAS_PTE_SPECIAL)) 67005289402SAnshuman Khandual return; 67105289402SAnshuman Khandual 6726315df41SAnshuman Khandual pr_debug("Validating PTE special\n"); 67305289402SAnshuman Khandual WARN_ON(!pte_special(pte_mkspecial(pte))); 67405289402SAnshuman Khandual } 67505289402SAnshuman Khandual 6768cb183f2SGavin Shan static void __init pte_protnone_tests(struct pgtable_debug_args *args) 67705289402SAnshuman Khandual { 6788cb183f2SGavin Shan pte_t pte = pfn_pte(args->fixed_pte_pfn, args->page_prot_none); 67905289402SAnshuman Khandual 68005289402SAnshuman Khandual if (!IS_ENABLED(CONFIG_NUMA_BALANCING)) 68105289402SAnshuman Khandual return; 68205289402SAnshuman Khandual 6836315df41SAnshuman Khandual pr_debug("Validating PTE protnone\n"); 68405289402SAnshuman Khandual WARN_ON(!pte_protnone(pte)); 68505289402SAnshuman Khandual WARN_ON(!pte_present(pte)); 68605289402SAnshuman Khandual } 68705289402SAnshuman Khandual 68805289402SAnshuman Khandual #ifdef CONFIG_TRANSPARENT_HUGEPAGE 6898cb183f2SGavin Shan static void __init pmd_protnone_tests(struct pgtable_debug_args *args) 69005289402SAnshuman Khandual { 69165ac1a60SAnshuman Khandual pmd_t pmd; 69205289402SAnshuman Khandual 69305289402SAnshuman Khandual if (!IS_ENABLED(CONFIG_NUMA_BALANCING)) 69405289402SAnshuman Khandual return; 69505289402SAnshuman Khandual 69665ac1a60SAnshuman Khandual if (!has_transparent_hugepage()) 69765ac1a60SAnshuman Khandual return; 69865ac1a60SAnshuman Khandual 6996315df41SAnshuman Khandual pr_debug("Validating PMD protnone\n"); 7008cb183f2SGavin Shan pmd = pmd_mkhuge(pfn_pmd(args->fixed_pmd_pfn, args->page_prot_none)); 70105289402SAnshuman Khandual WARN_ON(!pmd_protnone(pmd)); 70205289402SAnshuman Khandual WARN_ON(!pmd_present(pmd)); 70305289402SAnshuman Khandual } 70405289402SAnshuman Khandual #else /* !CONFIG_TRANSPARENT_HUGEPAGE */ 7058cb183f2SGavin Shan static void __init pmd_protnone_tests(struct pgtable_debug_args *args) { } 70605289402SAnshuman Khandual #endif /* CONFIG_TRANSPARENT_HUGEPAGE */ 70705289402SAnshuman Khandual 70805289402SAnshuman Khandual #ifdef CONFIG_ARCH_HAS_PTE_DEVMAP 7098cb183f2SGavin Shan static void __init pte_devmap_tests(struct pgtable_debug_args *args) 71005289402SAnshuman Khandual { 7118cb183f2SGavin Shan pte_t pte = pfn_pte(args->fixed_pte_pfn, args->page_prot); 71205289402SAnshuman Khandual 7136315df41SAnshuman Khandual pr_debug("Validating PTE devmap\n"); 71405289402SAnshuman Khandual WARN_ON(!pte_devmap(pte_mkdevmap(pte))); 71505289402SAnshuman Khandual } 71605289402SAnshuman Khandual 71705289402SAnshuman Khandual #ifdef CONFIG_TRANSPARENT_HUGEPAGE 7188cb183f2SGavin Shan static void __init pmd_devmap_tests(struct pgtable_debug_args *args) 71905289402SAnshuman Khandual { 72065ac1a60SAnshuman Khandual pmd_t pmd; 72165ac1a60SAnshuman Khandual 72265ac1a60SAnshuman Khandual if (!has_transparent_hugepage()) 72365ac1a60SAnshuman Khandual return; 72405289402SAnshuman Khandual 7256315df41SAnshuman Khandual pr_debug("Validating PMD devmap\n"); 7268cb183f2SGavin Shan pmd = pfn_pmd(args->fixed_pmd_pfn, args->page_prot); 72705289402SAnshuman Khandual WARN_ON(!pmd_devmap(pmd_mkdevmap(pmd))); 72805289402SAnshuman Khandual } 72905289402SAnshuman Khandual 73005289402SAnshuman Khandual #ifdef CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD 7318cb183f2SGavin Shan static void __init pud_devmap_tests(struct pgtable_debug_args *args) 73205289402SAnshuman Khandual { 73365ac1a60SAnshuman Khandual pud_t pud; 73465ac1a60SAnshuman Khandual 73565ac1a60SAnshuman Khandual if (!has_transparent_hugepage()) 73665ac1a60SAnshuman Khandual return; 73705289402SAnshuman Khandual 7386315df41SAnshuman Khandual pr_debug("Validating PUD devmap\n"); 7398cb183f2SGavin Shan pud = pfn_pud(args->fixed_pud_pfn, args->page_prot); 74005289402SAnshuman Khandual WARN_ON(!pud_devmap(pud_mkdevmap(pud))); 74105289402SAnshuman Khandual } 74205289402SAnshuman Khandual #else /* !CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD */ 7438cb183f2SGavin Shan static void __init pud_devmap_tests(struct pgtable_debug_args *args) { } 74405289402SAnshuman Khandual #endif /* CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD */ 74505289402SAnshuman Khandual #else /* CONFIG_TRANSPARENT_HUGEPAGE */ 7468cb183f2SGavin Shan static void __init pmd_devmap_tests(struct pgtable_debug_args *args) { } 7478cb183f2SGavin Shan static void __init pud_devmap_tests(struct pgtable_debug_args *args) { } 74805289402SAnshuman Khandual #endif /* CONFIG_TRANSPARENT_HUGEPAGE */ 74905289402SAnshuman Khandual #else 7508cb183f2SGavin Shan static void __init pte_devmap_tests(struct pgtable_debug_args *args) { } 7518cb183f2SGavin Shan static void __init pmd_devmap_tests(struct pgtable_debug_args *args) { } 7528cb183f2SGavin Shan static void __init pud_devmap_tests(struct pgtable_debug_args *args) { } 75305289402SAnshuman Khandual #endif /* CONFIG_ARCH_HAS_PTE_DEVMAP */ 75405289402SAnshuman Khandual 7555f447e80SGavin Shan static void __init pte_soft_dirty_tests(struct pgtable_debug_args *args) 75605289402SAnshuman Khandual { 7575f447e80SGavin Shan pte_t pte = pfn_pte(args->fixed_pte_pfn, args->page_prot); 75805289402SAnshuman Khandual 75905289402SAnshuman Khandual if (!IS_ENABLED(CONFIG_MEM_SOFT_DIRTY)) 76005289402SAnshuman Khandual return; 76105289402SAnshuman Khandual 7626315df41SAnshuman Khandual pr_debug("Validating PTE soft dirty\n"); 76305289402SAnshuman Khandual WARN_ON(!pte_soft_dirty(pte_mksoft_dirty(pte))); 76405289402SAnshuman Khandual WARN_ON(pte_soft_dirty(pte_clear_soft_dirty(pte))); 76505289402SAnshuman Khandual } 76605289402SAnshuman Khandual 7675f447e80SGavin Shan static void __init pte_swap_soft_dirty_tests(struct pgtable_debug_args *args) 76805289402SAnshuman Khandual { 7695f447e80SGavin Shan pte_t pte = pfn_pte(args->fixed_pte_pfn, args->page_prot); 77005289402SAnshuman Khandual 77105289402SAnshuman Khandual if (!IS_ENABLED(CONFIG_MEM_SOFT_DIRTY)) 77205289402SAnshuman Khandual return; 77305289402SAnshuman Khandual 7746315df41SAnshuman Khandual pr_debug("Validating PTE swap soft dirty\n"); 77505289402SAnshuman Khandual WARN_ON(!pte_swp_soft_dirty(pte_swp_mksoft_dirty(pte))); 77605289402SAnshuman Khandual WARN_ON(pte_swp_soft_dirty(pte_swp_clear_soft_dirty(pte))); 77705289402SAnshuman Khandual } 77805289402SAnshuman Khandual 77905289402SAnshuman Khandual #ifdef CONFIG_TRANSPARENT_HUGEPAGE 7805f447e80SGavin Shan static void __init pmd_soft_dirty_tests(struct pgtable_debug_args *args) 78105289402SAnshuman Khandual { 78265ac1a60SAnshuman Khandual pmd_t pmd; 78305289402SAnshuman Khandual 78405289402SAnshuman Khandual if (!IS_ENABLED(CONFIG_MEM_SOFT_DIRTY)) 78505289402SAnshuman Khandual return; 78605289402SAnshuman Khandual 78765ac1a60SAnshuman Khandual if (!has_transparent_hugepage()) 78865ac1a60SAnshuman Khandual return; 78965ac1a60SAnshuman Khandual 7906315df41SAnshuman Khandual pr_debug("Validating PMD soft dirty\n"); 7915f447e80SGavin Shan pmd = pfn_pmd(args->fixed_pmd_pfn, args->page_prot); 79205289402SAnshuman Khandual WARN_ON(!pmd_soft_dirty(pmd_mksoft_dirty(pmd))); 79305289402SAnshuman Khandual WARN_ON(pmd_soft_dirty(pmd_clear_soft_dirty(pmd))); 79405289402SAnshuman Khandual } 79505289402SAnshuman Khandual 7965f447e80SGavin Shan static void __init pmd_swap_soft_dirty_tests(struct pgtable_debug_args *args) 79705289402SAnshuman Khandual { 79865ac1a60SAnshuman Khandual pmd_t pmd; 79905289402SAnshuman Khandual 80005289402SAnshuman Khandual if (!IS_ENABLED(CONFIG_MEM_SOFT_DIRTY) || 80105289402SAnshuman Khandual !IS_ENABLED(CONFIG_ARCH_ENABLE_THP_MIGRATION)) 80205289402SAnshuman Khandual return; 80305289402SAnshuman Khandual 80465ac1a60SAnshuman Khandual if (!has_transparent_hugepage()) 80565ac1a60SAnshuman Khandual return; 80665ac1a60SAnshuman Khandual 8076315df41SAnshuman Khandual pr_debug("Validating PMD swap soft dirty\n"); 8085f447e80SGavin Shan pmd = pfn_pmd(args->fixed_pmd_pfn, args->page_prot); 80905289402SAnshuman Khandual WARN_ON(!pmd_swp_soft_dirty(pmd_swp_mksoft_dirty(pmd))); 81005289402SAnshuman Khandual WARN_ON(pmd_swp_soft_dirty(pmd_swp_clear_soft_dirty(pmd))); 81105289402SAnshuman Khandual } 812b593b90dSShixin Liu #else /* !CONFIG_TRANSPARENT_HUGEPAGE */ 8135f447e80SGavin Shan static void __init pmd_soft_dirty_tests(struct pgtable_debug_args *args) { } 8145f447e80SGavin Shan static void __init pmd_swap_soft_dirty_tests(struct pgtable_debug_args *args) { } 815b593b90dSShixin Liu #endif /* CONFIG_TRANSPARENT_HUGEPAGE */ 81605289402SAnshuman Khandual 8175f447e80SGavin Shan static void __init pte_swap_tests(struct pgtable_debug_args *args) 81805289402SAnshuman Khandual { 81905289402SAnshuman Khandual swp_entry_t swp; 82005289402SAnshuman Khandual pte_t pte; 82105289402SAnshuman Khandual 8226315df41SAnshuman Khandual pr_debug("Validating PTE swap\n"); 8235f447e80SGavin Shan pte = pfn_pte(args->fixed_pte_pfn, args->page_prot); 82405289402SAnshuman Khandual swp = __pte_to_swp_entry(pte); 82505289402SAnshuman Khandual pte = __swp_entry_to_pte(swp); 8265f447e80SGavin Shan WARN_ON(args->fixed_pte_pfn != pte_pfn(pte)); 82705289402SAnshuman Khandual } 82805289402SAnshuman Khandual 82905289402SAnshuman Khandual #ifdef CONFIG_ARCH_ENABLE_THP_MIGRATION 8305f447e80SGavin Shan static void __init pmd_swap_tests(struct pgtable_debug_args *args) 83105289402SAnshuman Khandual { 83205289402SAnshuman Khandual swp_entry_t swp; 83305289402SAnshuman Khandual pmd_t pmd; 83405289402SAnshuman Khandual 83565ac1a60SAnshuman Khandual if (!has_transparent_hugepage()) 83665ac1a60SAnshuman Khandual return; 83765ac1a60SAnshuman Khandual 8386315df41SAnshuman Khandual pr_debug("Validating PMD swap\n"); 8395f447e80SGavin Shan pmd = pfn_pmd(args->fixed_pmd_pfn, args->page_prot); 84005289402SAnshuman Khandual swp = __pmd_to_swp_entry(pmd); 84105289402SAnshuman Khandual pmd = __swp_entry_to_pmd(swp); 8425f447e80SGavin Shan WARN_ON(args->fixed_pmd_pfn != pmd_pfn(pmd)); 84305289402SAnshuman Khandual } 84405289402SAnshuman Khandual #else /* !CONFIG_ARCH_ENABLE_THP_MIGRATION */ 8455f447e80SGavin Shan static void __init pmd_swap_tests(struct pgtable_debug_args *args) { } 84605289402SAnshuman Khandual #endif /* CONFIG_ARCH_ENABLE_THP_MIGRATION */ 84705289402SAnshuman Khandual 848*4878a888SGavin Shan static void __init swap_migration_tests(struct pgtable_debug_args *args) 84905289402SAnshuman Khandual { 85005289402SAnshuman Khandual struct page *page; 85105289402SAnshuman Khandual swp_entry_t swp; 85205289402SAnshuman Khandual 85305289402SAnshuman Khandual if (!IS_ENABLED(CONFIG_MIGRATION)) 85405289402SAnshuman Khandual return; 8556315df41SAnshuman Khandual 85605289402SAnshuman Khandual /* 85705289402SAnshuman Khandual * swap_migration_tests() requires a dedicated page as it needs to 85805289402SAnshuman Khandual * be locked before creating a migration entry from it. Locking the 85905289402SAnshuman Khandual * page that actually maps kernel text ('start_kernel') can be real 860*4878a888SGavin Shan * problematic. Lets use the allocated page explicitly for this 861*4878a888SGavin Shan * purpose. 86205289402SAnshuman Khandual */ 863*4878a888SGavin Shan page = (args->pte_pfn != ULONG_MAX) ? pfn_to_page(args->pte_pfn) : NULL; 864*4878a888SGavin Shan if (!page) 86505289402SAnshuman Khandual return; 866*4878a888SGavin Shan 867*4878a888SGavin Shan pr_debug("Validating swap migration\n"); 86805289402SAnshuman Khandual 86905289402SAnshuman Khandual /* 87005289402SAnshuman Khandual * make_migration_entry() expects given page to be 87105289402SAnshuman Khandual * locked, otherwise it stumbles upon a BUG_ON(). 87205289402SAnshuman Khandual */ 87305289402SAnshuman Khandual __SetPageLocked(page); 8744dd845b5SAlistair Popple swp = make_writable_migration_entry(page_to_pfn(page)); 87505289402SAnshuman Khandual WARN_ON(!is_migration_entry(swp)); 8764dd845b5SAlistair Popple WARN_ON(!is_writable_migration_entry(swp)); 87705289402SAnshuman Khandual 8784dd845b5SAlistair Popple swp = make_readable_migration_entry(swp_offset(swp)); 87905289402SAnshuman Khandual WARN_ON(!is_migration_entry(swp)); 8804dd845b5SAlistair Popple WARN_ON(is_writable_migration_entry(swp)); 88105289402SAnshuman Khandual 8824dd845b5SAlistair Popple swp = make_readable_migration_entry(page_to_pfn(page)); 88305289402SAnshuman Khandual WARN_ON(!is_migration_entry(swp)); 8844dd845b5SAlistair Popple WARN_ON(is_writable_migration_entry(swp)); 88505289402SAnshuman Khandual __ClearPageLocked(page); 88605289402SAnshuman Khandual } 88705289402SAnshuman Khandual 88805289402SAnshuman Khandual #ifdef CONFIG_HUGETLB_PAGE 88936b77d1eSGavin Shan static void __init hugetlb_basic_tests(struct pgtable_debug_args *args) 89005289402SAnshuman Khandual { 89105289402SAnshuman Khandual struct page *page; 89205289402SAnshuman Khandual pte_t pte; 89305289402SAnshuman Khandual 8946315df41SAnshuman Khandual pr_debug("Validating HugeTLB basic\n"); 89505289402SAnshuman Khandual /* 89605289402SAnshuman Khandual * Accessing the page associated with the pfn is safe here, 89705289402SAnshuman Khandual * as it was previously derived from a real kernel symbol. 89805289402SAnshuman Khandual */ 89936b77d1eSGavin Shan page = pfn_to_page(args->fixed_pmd_pfn); 90036b77d1eSGavin Shan pte = mk_huge_pte(page, args->page_prot); 90105289402SAnshuman Khandual 90205289402SAnshuman Khandual WARN_ON(!huge_pte_dirty(huge_pte_mkdirty(pte))); 90305289402SAnshuman Khandual WARN_ON(!huge_pte_write(huge_pte_mkwrite(huge_pte_wrprotect(pte)))); 90405289402SAnshuman Khandual WARN_ON(huge_pte_write(huge_pte_wrprotect(huge_pte_mkwrite(pte)))); 90505289402SAnshuman Khandual 90605289402SAnshuman Khandual #ifdef CONFIG_ARCH_WANT_GENERAL_HUGETLB 90736b77d1eSGavin Shan pte = pfn_pte(args->fixed_pmd_pfn, args->page_prot); 90805289402SAnshuman Khandual 90905289402SAnshuman Khandual WARN_ON(!pte_huge(pte_mkhuge(pte))); 91005289402SAnshuman Khandual #endif /* CONFIG_ARCH_WANT_GENERAL_HUGETLB */ 91105289402SAnshuman Khandual } 91205289402SAnshuman Khandual #else /* !CONFIG_HUGETLB_PAGE */ 91336b77d1eSGavin Shan static void __init hugetlb_basic_tests(struct pgtable_debug_args *args) { } 91405289402SAnshuman Khandual #endif /* CONFIG_HUGETLB_PAGE */ 91505289402SAnshuman Khandual 91605289402SAnshuman Khandual #ifdef CONFIG_TRANSPARENT_HUGEPAGE 917*4878a888SGavin Shan static void __init pmd_thp_tests(struct pgtable_debug_args *args) 91805289402SAnshuman Khandual { 91905289402SAnshuman Khandual pmd_t pmd; 92005289402SAnshuman Khandual 92105289402SAnshuman Khandual if (!has_transparent_hugepage()) 92205289402SAnshuman Khandual return; 92305289402SAnshuman Khandual 9246315df41SAnshuman Khandual pr_debug("Validating PMD based THP\n"); 92505289402SAnshuman Khandual /* 92605289402SAnshuman Khandual * pmd_trans_huge() and pmd_present() must return positive after 92705289402SAnshuman Khandual * MMU invalidation with pmd_mkinvalid(). This behavior is an 92805289402SAnshuman Khandual * optimization for transparent huge page. pmd_trans_huge() must 92905289402SAnshuman Khandual * be true if pmd_page() returns a valid THP to avoid taking the 93005289402SAnshuman Khandual * pmd_lock when others walk over non transhuge pmds (i.e. there 93105289402SAnshuman Khandual * are no THP allocated). Especially when splitting a THP and 93205289402SAnshuman Khandual * removing the present bit from the pmd, pmd_trans_huge() still 93305289402SAnshuman Khandual * needs to return true. pmd_present() should be true whenever 93405289402SAnshuman Khandual * pmd_trans_huge() returns true. 93505289402SAnshuman Khandual */ 936*4878a888SGavin Shan pmd = pfn_pmd(args->fixed_pmd_pfn, args->page_prot); 93705289402SAnshuman Khandual WARN_ON(!pmd_trans_huge(pmd_mkhuge(pmd))); 93805289402SAnshuman Khandual 93905289402SAnshuman Khandual #ifndef __HAVE_ARCH_PMDP_INVALIDATE 94005289402SAnshuman Khandual WARN_ON(!pmd_trans_huge(pmd_mkinvalid(pmd_mkhuge(pmd)))); 94105289402SAnshuman Khandual WARN_ON(!pmd_present(pmd_mkinvalid(pmd_mkhuge(pmd)))); 94205289402SAnshuman Khandual #endif /* __HAVE_ARCH_PMDP_INVALIDATE */ 94305289402SAnshuman Khandual } 94405289402SAnshuman Khandual 94505289402SAnshuman Khandual #ifdef CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD 946*4878a888SGavin Shan static void __init pud_thp_tests(struct pgtable_debug_args *args) 94705289402SAnshuman Khandual { 94805289402SAnshuman Khandual pud_t pud; 94905289402SAnshuman Khandual 95005289402SAnshuman Khandual if (!has_transparent_hugepage()) 95105289402SAnshuman Khandual return; 95205289402SAnshuman Khandual 9536315df41SAnshuman Khandual pr_debug("Validating PUD based THP\n"); 954*4878a888SGavin Shan pud = pfn_pud(args->fixed_pud_pfn, args->page_prot); 95505289402SAnshuman Khandual WARN_ON(!pud_trans_huge(pud_mkhuge(pud))); 95605289402SAnshuman Khandual 95705289402SAnshuman Khandual /* 95805289402SAnshuman Khandual * pud_mkinvalid() has been dropped for now. Enable back 95905289402SAnshuman Khandual * these tests when it comes back with a modified pud_present(). 96005289402SAnshuman Khandual * 96105289402SAnshuman Khandual * WARN_ON(!pud_trans_huge(pud_mkinvalid(pud_mkhuge(pud)))); 96205289402SAnshuman Khandual * WARN_ON(!pud_present(pud_mkinvalid(pud_mkhuge(pud)))); 96305289402SAnshuman Khandual */ 96405289402SAnshuman Khandual } 96505289402SAnshuman Khandual #else /* !CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD */ 966*4878a888SGavin Shan static void __init pud_thp_tests(struct pgtable_debug_args *args) { } 96705289402SAnshuman Khandual #endif /* CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD */ 96805289402SAnshuman Khandual #else /* !CONFIG_TRANSPARENT_HUGEPAGE */ 969*4878a888SGavin Shan static void __init pmd_thp_tests(struct pgtable_debug_args *args) { } 970*4878a888SGavin Shan static void __init pud_thp_tests(struct pgtable_debug_args *args) { } 97105289402SAnshuman Khandual #endif /* CONFIG_TRANSPARENT_HUGEPAGE */ 97205289402SAnshuman Khandual 973399145f9SAnshuman Khandual static unsigned long __init get_random_vaddr(void) 974399145f9SAnshuman Khandual { 975399145f9SAnshuman Khandual unsigned long random_vaddr, random_pages, total_user_pages; 976399145f9SAnshuman Khandual 977399145f9SAnshuman Khandual total_user_pages = (TASK_SIZE - FIRST_USER_ADDRESS) / PAGE_SIZE; 978399145f9SAnshuman Khandual 979399145f9SAnshuman Khandual random_pages = get_random_long() % total_user_pages; 980399145f9SAnshuman Khandual random_vaddr = FIRST_USER_ADDRESS + random_pages * PAGE_SIZE; 981399145f9SAnshuman Khandual 982399145f9SAnshuman Khandual return random_vaddr; 983399145f9SAnshuman Khandual } 984399145f9SAnshuman Khandual 9853c9b84f0SGavin Shan static void __init destroy_args(struct pgtable_debug_args *args) 9863c9b84f0SGavin Shan { 9873c9b84f0SGavin Shan struct page *page = NULL; 9883c9b84f0SGavin Shan 9893c9b84f0SGavin Shan /* Free (huge) page */ 9903c9b84f0SGavin Shan if (IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE) && 9913c9b84f0SGavin Shan IS_ENABLED(CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD) && 9923c9b84f0SGavin Shan has_transparent_hugepage() && 9933c9b84f0SGavin Shan args->pud_pfn != ULONG_MAX) { 9943c9b84f0SGavin Shan if (args->is_contiguous_page) { 9953c9b84f0SGavin Shan free_contig_range(args->pud_pfn, 9963c9b84f0SGavin Shan (1 << (HPAGE_PUD_SHIFT - PAGE_SHIFT))); 9973c9b84f0SGavin Shan } else { 9983c9b84f0SGavin Shan page = pfn_to_page(args->pud_pfn); 9993c9b84f0SGavin Shan __free_pages(page, HPAGE_PUD_SHIFT - PAGE_SHIFT); 10003c9b84f0SGavin Shan } 10013c9b84f0SGavin Shan 10023c9b84f0SGavin Shan args->pud_pfn = ULONG_MAX; 10033c9b84f0SGavin Shan args->pmd_pfn = ULONG_MAX; 10043c9b84f0SGavin Shan args->pte_pfn = ULONG_MAX; 10053c9b84f0SGavin Shan } 10063c9b84f0SGavin Shan 10073c9b84f0SGavin Shan if (IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE) && 10083c9b84f0SGavin Shan has_transparent_hugepage() && 10093c9b84f0SGavin Shan args->pmd_pfn != ULONG_MAX) { 10103c9b84f0SGavin Shan if (args->is_contiguous_page) { 10113c9b84f0SGavin Shan free_contig_range(args->pmd_pfn, (1 << HPAGE_PMD_ORDER)); 10123c9b84f0SGavin Shan } else { 10133c9b84f0SGavin Shan page = pfn_to_page(args->pmd_pfn); 10143c9b84f0SGavin Shan __free_pages(page, HPAGE_PMD_ORDER); 10153c9b84f0SGavin Shan } 10163c9b84f0SGavin Shan 10173c9b84f0SGavin Shan args->pmd_pfn = ULONG_MAX; 10183c9b84f0SGavin Shan args->pte_pfn = ULONG_MAX; 10193c9b84f0SGavin Shan } 10203c9b84f0SGavin Shan 10213c9b84f0SGavin Shan if (args->pte_pfn != ULONG_MAX) { 10223c9b84f0SGavin Shan page = pfn_to_page(args->pte_pfn); 10233c9b84f0SGavin Shan __free_pages(page, 0); 10243c9b84f0SGavin Shan 10253c9b84f0SGavin Shan args->pte_pfn = ULONG_MAX; 10263c9b84f0SGavin Shan } 10273c9b84f0SGavin Shan 10283c9b84f0SGavin Shan /* Free page table entries */ 10293c9b84f0SGavin Shan if (args->start_ptep) { 10303c9b84f0SGavin Shan pte_free(args->mm, args->start_ptep); 10313c9b84f0SGavin Shan mm_dec_nr_ptes(args->mm); 10323c9b84f0SGavin Shan } 10333c9b84f0SGavin Shan 10343c9b84f0SGavin Shan if (args->start_pmdp) { 10353c9b84f0SGavin Shan pmd_free(args->mm, args->start_pmdp); 10363c9b84f0SGavin Shan mm_dec_nr_pmds(args->mm); 10373c9b84f0SGavin Shan } 10383c9b84f0SGavin Shan 10393c9b84f0SGavin Shan if (args->start_pudp) { 10403c9b84f0SGavin Shan pud_free(args->mm, args->start_pudp); 10413c9b84f0SGavin Shan mm_dec_nr_puds(args->mm); 10423c9b84f0SGavin Shan } 10433c9b84f0SGavin Shan 10443c9b84f0SGavin Shan if (args->start_p4dp) 10453c9b84f0SGavin Shan p4d_free(args->mm, args->start_p4dp); 10463c9b84f0SGavin Shan 10473c9b84f0SGavin Shan /* Free vma and mm struct */ 10483c9b84f0SGavin Shan if (args->vma) 10493c9b84f0SGavin Shan vm_area_free(args->vma); 10503c9b84f0SGavin Shan 10513c9b84f0SGavin Shan if (args->mm) 10523c9b84f0SGavin Shan mmdrop(args->mm); 10533c9b84f0SGavin Shan } 10543c9b84f0SGavin Shan 10553c9b84f0SGavin Shan static struct page * __init 10563c9b84f0SGavin Shan debug_vm_pgtable_alloc_huge_page(struct pgtable_debug_args *args, int order) 10573c9b84f0SGavin Shan { 10583c9b84f0SGavin Shan struct page *page = NULL; 10593c9b84f0SGavin Shan 10603c9b84f0SGavin Shan #ifdef CONFIG_CONTIG_ALLOC 10613c9b84f0SGavin Shan if (order >= MAX_ORDER) { 10623c9b84f0SGavin Shan page = alloc_contig_pages((1 << order), GFP_KERNEL, 10633c9b84f0SGavin Shan first_online_node, NULL); 10643c9b84f0SGavin Shan if (page) { 10653c9b84f0SGavin Shan args->is_contiguous_page = true; 10663c9b84f0SGavin Shan return page; 10673c9b84f0SGavin Shan } 10683c9b84f0SGavin Shan } 10693c9b84f0SGavin Shan #endif 10703c9b84f0SGavin Shan 10713c9b84f0SGavin Shan if (order < MAX_ORDER) 10723c9b84f0SGavin Shan page = alloc_pages(GFP_KERNEL, order); 10733c9b84f0SGavin Shan 10743c9b84f0SGavin Shan return page; 10753c9b84f0SGavin Shan } 10763c9b84f0SGavin Shan 10773c9b84f0SGavin Shan static int __init init_args(struct pgtable_debug_args *args) 10783c9b84f0SGavin Shan { 10793c9b84f0SGavin Shan struct page *page = NULL; 10803c9b84f0SGavin Shan phys_addr_t phys; 10813c9b84f0SGavin Shan int ret = 0; 10823c9b84f0SGavin Shan 10833c9b84f0SGavin Shan /* 10843c9b84f0SGavin Shan * Initialize the debugging data. 10853c9b84f0SGavin Shan * 10863c9b84f0SGavin Shan * __P000 (or even __S000) will help create page table entries with 10873c9b84f0SGavin Shan * PROT_NONE permission as required for pxx_protnone_tests(). 10883c9b84f0SGavin Shan */ 10893c9b84f0SGavin Shan memset(args, 0, sizeof(*args)); 10903c9b84f0SGavin Shan args->vaddr = get_random_vaddr(); 10913c9b84f0SGavin Shan args->page_prot = vm_get_page_prot(VMFLAGS); 10923c9b84f0SGavin Shan args->page_prot_none = __P000; 10933c9b84f0SGavin Shan args->is_contiguous_page = false; 10943c9b84f0SGavin Shan args->pud_pfn = ULONG_MAX; 10953c9b84f0SGavin Shan args->pmd_pfn = ULONG_MAX; 10963c9b84f0SGavin Shan args->pte_pfn = ULONG_MAX; 10973c9b84f0SGavin Shan args->fixed_pgd_pfn = ULONG_MAX; 10983c9b84f0SGavin Shan args->fixed_p4d_pfn = ULONG_MAX; 10993c9b84f0SGavin Shan args->fixed_pud_pfn = ULONG_MAX; 11003c9b84f0SGavin Shan args->fixed_pmd_pfn = ULONG_MAX; 11013c9b84f0SGavin Shan args->fixed_pte_pfn = ULONG_MAX; 11023c9b84f0SGavin Shan 11033c9b84f0SGavin Shan /* Allocate mm and vma */ 11043c9b84f0SGavin Shan args->mm = mm_alloc(); 11053c9b84f0SGavin Shan if (!args->mm) { 11063c9b84f0SGavin Shan pr_err("Failed to allocate mm struct\n"); 11073c9b84f0SGavin Shan ret = -ENOMEM; 11083c9b84f0SGavin Shan goto error; 11093c9b84f0SGavin Shan } 11103c9b84f0SGavin Shan 11113c9b84f0SGavin Shan args->vma = vm_area_alloc(args->mm); 11123c9b84f0SGavin Shan if (!args->vma) { 11133c9b84f0SGavin Shan pr_err("Failed to allocate vma\n"); 11143c9b84f0SGavin Shan ret = -ENOMEM; 11153c9b84f0SGavin Shan goto error; 11163c9b84f0SGavin Shan } 11173c9b84f0SGavin Shan 11183c9b84f0SGavin Shan /* 11193c9b84f0SGavin Shan * Allocate page table entries. They will be modified in the tests. 11203c9b84f0SGavin Shan * Lets save the page table entries so that they can be released 11213c9b84f0SGavin Shan * when the tests are completed. 11223c9b84f0SGavin Shan */ 11233c9b84f0SGavin Shan args->pgdp = pgd_offset(args->mm, args->vaddr); 11243c9b84f0SGavin Shan args->p4dp = p4d_alloc(args->mm, args->pgdp, args->vaddr); 11253c9b84f0SGavin Shan if (!args->p4dp) { 11263c9b84f0SGavin Shan pr_err("Failed to allocate p4d entries\n"); 11273c9b84f0SGavin Shan ret = -ENOMEM; 11283c9b84f0SGavin Shan goto error; 11293c9b84f0SGavin Shan } 11303c9b84f0SGavin Shan args->start_p4dp = p4d_offset(args->pgdp, 0UL); 11313c9b84f0SGavin Shan WARN_ON(!args->start_p4dp); 11323c9b84f0SGavin Shan 11333c9b84f0SGavin Shan args->pudp = pud_alloc(args->mm, args->p4dp, args->vaddr); 11343c9b84f0SGavin Shan if (!args->pudp) { 11353c9b84f0SGavin Shan pr_err("Failed to allocate pud entries\n"); 11363c9b84f0SGavin Shan ret = -ENOMEM; 11373c9b84f0SGavin Shan goto error; 11383c9b84f0SGavin Shan } 11393c9b84f0SGavin Shan args->start_pudp = pud_offset(args->p4dp, 0UL); 11403c9b84f0SGavin Shan WARN_ON(!args->start_pudp); 11413c9b84f0SGavin Shan 11423c9b84f0SGavin Shan args->pmdp = pmd_alloc(args->mm, args->pudp, args->vaddr); 11433c9b84f0SGavin Shan if (!args->pmdp) { 11443c9b84f0SGavin Shan pr_err("Failed to allocate pmd entries\n"); 11453c9b84f0SGavin Shan ret = -ENOMEM; 11463c9b84f0SGavin Shan goto error; 11473c9b84f0SGavin Shan } 11483c9b84f0SGavin Shan args->start_pmdp = pmd_offset(args->pudp, 0UL); 11493c9b84f0SGavin Shan WARN_ON(!args->start_pmdp); 11503c9b84f0SGavin Shan 11513c9b84f0SGavin Shan if (pte_alloc(args->mm, args->pmdp)) { 11523c9b84f0SGavin Shan pr_err("Failed to allocate pte entries\n"); 11533c9b84f0SGavin Shan ret = -ENOMEM; 11543c9b84f0SGavin Shan goto error; 11553c9b84f0SGavin Shan } 11563c9b84f0SGavin Shan args->start_ptep = pmd_pgtable(READ_ONCE(*args->pmdp)); 11573c9b84f0SGavin Shan WARN_ON(!args->start_ptep); 11583c9b84f0SGavin Shan 11593c9b84f0SGavin Shan /* 11603c9b84f0SGavin Shan * PFN for mapping at PTE level is determined from a standard kernel 11613c9b84f0SGavin Shan * text symbol. But pfns for higher page table levels are derived by 11623c9b84f0SGavin Shan * masking lower bits of this real pfn. These derived pfns might not 11633c9b84f0SGavin Shan * exist on the platform but that does not really matter as pfn_pxx() 11643c9b84f0SGavin Shan * helpers will still create appropriate entries for the test. This 11653c9b84f0SGavin Shan * helps avoid large memory block allocations to be used for mapping 11663c9b84f0SGavin Shan * at higher page table levels in some of the tests. 11673c9b84f0SGavin Shan */ 11683c9b84f0SGavin Shan phys = __pa_symbol(&start_kernel); 11693c9b84f0SGavin Shan args->fixed_pgd_pfn = __phys_to_pfn(phys & PGDIR_MASK); 11703c9b84f0SGavin Shan args->fixed_p4d_pfn = __phys_to_pfn(phys & P4D_MASK); 11713c9b84f0SGavin Shan args->fixed_pud_pfn = __phys_to_pfn(phys & PUD_MASK); 11723c9b84f0SGavin Shan args->fixed_pmd_pfn = __phys_to_pfn(phys & PMD_MASK); 11733c9b84f0SGavin Shan args->fixed_pte_pfn = __phys_to_pfn(phys & PAGE_MASK); 11743c9b84f0SGavin Shan WARN_ON(!pfn_valid(args->fixed_pte_pfn)); 11753c9b84f0SGavin Shan 11763c9b84f0SGavin Shan /* 11773c9b84f0SGavin Shan * Allocate (huge) pages because some of the tests need to access 11783c9b84f0SGavin Shan * the data in the pages. The corresponding tests will be skipped 11793c9b84f0SGavin Shan * if we fail to allocate (huge) pages. 11803c9b84f0SGavin Shan */ 11813c9b84f0SGavin Shan if (IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE) && 11823c9b84f0SGavin Shan IS_ENABLED(CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD) && 11833c9b84f0SGavin Shan has_transparent_hugepage()) { 11843c9b84f0SGavin Shan page = debug_vm_pgtable_alloc_huge_page(args, 11853c9b84f0SGavin Shan HPAGE_PUD_SHIFT - PAGE_SHIFT); 11863c9b84f0SGavin Shan if (page) { 11873c9b84f0SGavin Shan args->pud_pfn = page_to_pfn(page); 11883c9b84f0SGavin Shan args->pmd_pfn = args->pud_pfn; 11893c9b84f0SGavin Shan args->pte_pfn = args->pud_pfn; 11903c9b84f0SGavin Shan return 0; 11913c9b84f0SGavin Shan } 11923c9b84f0SGavin Shan } 11933c9b84f0SGavin Shan 11943c9b84f0SGavin Shan if (IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE) && 11953c9b84f0SGavin Shan has_transparent_hugepage()) { 11963c9b84f0SGavin Shan page = debug_vm_pgtable_alloc_huge_page(args, HPAGE_PMD_ORDER); 11973c9b84f0SGavin Shan if (page) { 11983c9b84f0SGavin Shan args->pmd_pfn = page_to_pfn(page); 11993c9b84f0SGavin Shan args->pte_pfn = args->pmd_pfn; 12003c9b84f0SGavin Shan return 0; 12013c9b84f0SGavin Shan } 12023c9b84f0SGavin Shan } 12033c9b84f0SGavin Shan 12043c9b84f0SGavin Shan page = alloc_pages(GFP_KERNEL, 0); 12053c9b84f0SGavin Shan if (page) 12063c9b84f0SGavin Shan args->pte_pfn = page_to_pfn(page); 12073c9b84f0SGavin Shan 12083c9b84f0SGavin Shan return 0; 12093c9b84f0SGavin Shan 12103c9b84f0SGavin Shan error: 12113c9b84f0SGavin Shan destroy_args(args); 12123c9b84f0SGavin Shan return ret; 12133c9b84f0SGavin Shan } 12143c9b84f0SGavin Shan 1215399145f9SAnshuman Khandual static int __init debug_vm_pgtable(void) 1216399145f9SAnshuman Khandual { 12173c9b84f0SGavin Shan struct pgtable_debug_args args; 1218a5c3b9ffSAnshuman Khandual struct vm_area_struct *vma; 1219399145f9SAnshuman Khandual struct mm_struct *mm; 1220399145f9SAnshuman Khandual pgd_t *pgdp; 1221399145f9SAnshuman Khandual p4d_t *p4dp, *saved_p4dp; 1222399145f9SAnshuman Khandual pud_t *pudp, *saved_pudp; 1223399145f9SAnshuman Khandual pmd_t *pmdp, *saved_pmdp, pmd; 1224399145f9SAnshuman Khandual pte_t *ptep; 1225399145f9SAnshuman Khandual pgtable_t saved_ptep; 12268cb183f2SGavin Shan pgprot_t prot; 1227399145f9SAnshuman Khandual phys_addr_t paddr; 1228399145f9SAnshuman Khandual unsigned long vaddr, pte_aligned, pmd_aligned; 122936b77d1eSGavin Shan unsigned long pud_aligned; 1230fea1120cSKees Cook spinlock_t *ptl = NULL; 12313c9b84f0SGavin Shan int idx, ret; 1232399145f9SAnshuman Khandual 1233399145f9SAnshuman Khandual pr_info("Validating architecture page table helpers\n"); 12343c9b84f0SGavin Shan ret = init_args(&args); 12353c9b84f0SGavin Shan if (ret) 12363c9b84f0SGavin Shan return ret; 12373c9b84f0SGavin Shan 1238399145f9SAnshuman Khandual prot = vm_get_page_prot(VMFLAGS); 1239399145f9SAnshuman Khandual vaddr = get_random_vaddr(); 1240399145f9SAnshuman Khandual mm = mm_alloc(); 1241399145f9SAnshuman Khandual if (!mm) { 1242399145f9SAnshuman Khandual pr_err("mm_struct allocation failed\n"); 1243399145f9SAnshuman Khandual return 1; 1244399145f9SAnshuman Khandual } 1245399145f9SAnshuman Khandual 1246a5c3b9ffSAnshuman Khandual vma = vm_area_alloc(mm); 1247a5c3b9ffSAnshuman Khandual if (!vma) { 1248a5c3b9ffSAnshuman Khandual pr_err("vma allocation failed\n"); 1249a5c3b9ffSAnshuman Khandual return 1; 1250a5c3b9ffSAnshuman Khandual } 1251a5c3b9ffSAnshuman Khandual 125205289402SAnshuman Khandual /* 1253399145f9SAnshuman Khandual * PFN for mapping at PTE level is determined from a standard kernel 1254399145f9SAnshuman Khandual * text symbol. But pfns for higher page table levels are derived by 1255399145f9SAnshuman Khandual * masking lower bits of this real pfn. These derived pfns might not 1256399145f9SAnshuman Khandual * exist on the platform but that does not really matter as pfn_pxx() 1257399145f9SAnshuman Khandual * helpers will still create appropriate entries for the test. This 1258399145f9SAnshuman Khandual * helps avoid large memory block allocations to be used for mapping 1259399145f9SAnshuman Khandual * at higher page table levels. 1260399145f9SAnshuman Khandual */ 1261399145f9SAnshuman Khandual paddr = __pa_symbol(&start_kernel); 1262399145f9SAnshuman Khandual 1263399145f9SAnshuman Khandual pte_aligned = (paddr & PAGE_MASK) >> PAGE_SHIFT; 1264399145f9SAnshuman Khandual pmd_aligned = (paddr & PMD_MASK) >> PAGE_SHIFT; 1265399145f9SAnshuman Khandual pud_aligned = (paddr & PUD_MASK) >> PAGE_SHIFT; 1266399145f9SAnshuman Khandual WARN_ON(!pfn_valid(pte_aligned)); 1267399145f9SAnshuman Khandual 1268399145f9SAnshuman Khandual pgdp = pgd_offset(mm, vaddr); 1269399145f9SAnshuman Khandual p4dp = p4d_alloc(mm, pgdp, vaddr); 1270399145f9SAnshuman Khandual pudp = pud_alloc(mm, p4dp, vaddr); 1271399145f9SAnshuman Khandual pmdp = pmd_alloc(mm, pudp, vaddr); 1272f14312e1SAneesh Kumar K.V /* 1273f14312e1SAneesh Kumar K.V * Allocate pgtable_t 1274f14312e1SAneesh Kumar K.V */ 1275f14312e1SAneesh Kumar K.V if (pte_alloc(mm, pmdp)) { 1276f14312e1SAneesh Kumar K.V pr_err("pgtable allocation failed\n"); 1277f14312e1SAneesh Kumar K.V return 1; 1278f14312e1SAneesh Kumar K.V } 1279399145f9SAnshuman Khandual 1280399145f9SAnshuman Khandual /* 1281399145f9SAnshuman Khandual * Save all the page table page addresses as the page table 1282399145f9SAnshuman Khandual * entries will be used for testing with random or garbage 1283399145f9SAnshuman Khandual * values. These saved addresses will be used for freeing 1284399145f9SAnshuman Khandual * page table pages. 1285399145f9SAnshuman Khandual */ 1286399145f9SAnshuman Khandual pmd = READ_ONCE(*pmdp); 1287399145f9SAnshuman Khandual saved_p4dp = p4d_offset(pgdp, 0UL); 1288399145f9SAnshuman Khandual saved_pudp = pud_offset(p4dp, 0UL); 1289399145f9SAnshuman Khandual saved_pmdp = pmd_offset(pudp, 0UL); 1290399145f9SAnshuman Khandual saved_ptep = pmd_pgtable(pmd); 1291399145f9SAnshuman Khandual 12922e326c07SAnshuman Khandual /* 12932e326c07SAnshuman Khandual * Iterate over the protection_map[] to make sure that all 12942e326c07SAnshuman Khandual * the basic page table transformation validations just hold 12952e326c07SAnshuman Khandual * true irrespective of the starting protection value for a 12962e326c07SAnshuman Khandual * given page table entry. 12972e326c07SAnshuman Khandual */ 12982e326c07SAnshuman Khandual for (idx = 0; idx < ARRAY_SIZE(protection_map); idx++) { 129936b77d1eSGavin Shan pte_basic_tests(&args, idx); 130036b77d1eSGavin Shan pmd_basic_tests(&args, idx); 130136b77d1eSGavin Shan pud_basic_tests(&args, idx); 13022e326c07SAnshuman Khandual } 13032e326c07SAnshuman Khandual 13042e326c07SAnshuman Khandual /* 13052e326c07SAnshuman Khandual * Both P4D and PGD level tests are very basic which do not 13062e326c07SAnshuman Khandual * involve creating page table entries from the protection 13072e326c07SAnshuman Khandual * value and the given pfn. Hence just keep them out from 13082e326c07SAnshuman Khandual * the above iteration for now to save some test execution 13092e326c07SAnshuman Khandual * time. 13102e326c07SAnshuman Khandual */ 131136b77d1eSGavin Shan p4d_basic_tests(&args); 131236b77d1eSGavin Shan pgd_basic_tests(&args); 1313399145f9SAnshuman Khandual 13148983d231SGavin Shan pmd_leaf_tests(&args); 13158983d231SGavin Shan pud_leaf_tests(&args); 1316a5c3b9ffSAnshuman Khandual 13178983d231SGavin Shan pte_savedwrite_tests(&args); 13188983d231SGavin Shan pmd_savedwrite_tests(&args); 1319a5c3b9ffSAnshuman Khandual 13208cb183f2SGavin Shan pte_special_tests(&args); 13218cb183f2SGavin Shan pte_protnone_tests(&args); 13228cb183f2SGavin Shan pmd_protnone_tests(&args); 132305289402SAnshuman Khandual 13248cb183f2SGavin Shan pte_devmap_tests(&args); 13258cb183f2SGavin Shan pmd_devmap_tests(&args); 13268cb183f2SGavin Shan pud_devmap_tests(&args); 132705289402SAnshuman Khandual 13285f447e80SGavin Shan pte_soft_dirty_tests(&args); 13295f447e80SGavin Shan pmd_soft_dirty_tests(&args); 13305f447e80SGavin Shan pte_swap_soft_dirty_tests(&args); 13315f447e80SGavin Shan pmd_swap_soft_dirty_tests(&args); 133205289402SAnshuman Khandual 13335f447e80SGavin Shan pte_swap_tests(&args); 13345f447e80SGavin Shan pmd_swap_tests(&args); 133505289402SAnshuman Khandual 1336*4878a888SGavin Shan swap_migration_tests(&args); 133705289402SAnshuman Khandual 1338*4878a888SGavin Shan pmd_thp_tests(&args); 1339*4878a888SGavin Shan pud_thp_tests(&args); 134005289402SAnshuman Khandual 134136b77d1eSGavin Shan hugetlb_basic_tests(&args); 1342e8edf0adSAneesh Kumar K.V 13436f302e27SAneesh Kumar K.V /* 13446f302e27SAneesh Kumar K.V * Page table modifying tests. They need to hold 13456f302e27SAneesh Kumar K.V * proper page table lock. 13466f302e27SAneesh Kumar K.V */ 1347e8edf0adSAneesh Kumar K.V 1348f14312e1SAneesh Kumar K.V ptep = pte_offset_map_lock(mm, pmdp, vaddr, &ptl); 1349401035d5SAneesh Kumar K.V pte_clear_tests(mm, ptep, pte_aligned, vaddr, prot); 1350e8edf0adSAneesh Kumar K.V pte_advanced_tests(mm, vma, ptep, pte_aligned, vaddr, prot); 1351e8edf0adSAneesh Kumar K.V pte_unmap_unlock(ptep, ptl); 1352e8edf0adSAneesh Kumar K.V 13536f302e27SAneesh Kumar K.V ptl = pmd_lock(mm, pmdp); 13546f302e27SAneesh Kumar K.V pmd_clear_tests(mm, pmdp); 135587f34986SAneesh Kumar K.V pmd_advanced_tests(mm, vma, pmdp, pmd_aligned, vaddr, prot, saved_ptep); 13566f302e27SAneesh Kumar K.V pmd_huge_tests(pmdp, pmd_aligned, prot); 1357e8edf0adSAneesh Kumar K.V pmd_populate_tests(mm, pmdp, saved_ptep); 13586f302e27SAneesh Kumar K.V spin_unlock(ptl); 13596f302e27SAneesh Kumar K.V 13606f302e27SAneesh Kumar K.V ptl = pud_lock(mm, pudp); 13616f302e27SAneesh Kumar K.V pud_clear_tests(mm, pudp); 13626f302e27SAneesh Kumar K.V pud_advanced_tests(mm, vma, pudp, pud_aligned, vaddr, prot); 13636f302e27SAneesh Kumar K.V pud_huge_tests(pudp, pud_aligned, prot); 1364e8edf0adSAneesh Kumar K.V pud_populate_tests(mm, pudp, saved_pmdp); 13656f302e27SAneesh Kumar K.V spin_unlock(ptl); 13666f302e27SAneesh Kumar K.V 13676f302e27SAneesh Kumar K.V spin_lock(&mm->page_table_lock); 13686f302e27SAneesh Kumar K.V p4d_clear_tests(mm, p4dp); 13696f302e27SAneesh Kumar K.V pgd_clear_tests(mm, pgdp); 1370e8edf0adSAneesh Kumar K.V p4d_populate_tests(mm, p4dp, saved_pudp); 1371e8edf0adSAneesh Kumar K.V pgd_populate_tests(mm, pgdp, saved_p4dp); 13726f302e27SAneesh Kumar K.V spin_unlock(&mm->page_table_lock); 1373e8edf0adSAneesh Kumar K.V 1374399145f9SAnshuman Khandual p4d_free(mm, saved_p4dp); 1375399145f9SAnshuman Khandual pud_free(mm, saved_pudp); 1376399145f9SAnshuman Khandual pmd_free(mm, saved_pmdp); 1377399145f9SAnshuman Khandual pte_free(mm, saved_ptep); 1378399145f9SAnshuman Khandual 1379a5c3b9ffSAnshuman Khandual vm_area_free(vma); 1380399145f9SAnshuman Khandual mm_dec_nr_puds(mm); 1381399145f9SAnshuman Khandual mm_dec_nr_pmds(mm); 1382399145f9SAnshuman Khandual mm_dec_nr_ptes(mm); 1383399145f9SAnshuman Khandual mmdrop(mm); 13843c9b84f0SGavin Shan 13853c9b84f0SGavin Shan destroy_args(&args); 1386399145f9SAnshuman Khandual return 0; 1387399145f9SAnshuman Khandual } 1388399145f9SAnshuman Khandual late_initcall(debug_vm_pgtable); 1389