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 */ 11399145f9SAnshuman Khandual #define pr_fmt(fmt) "debug_vm_pgtable: %s: " 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> 24399145f9SAnshuman Khandual #include <linux/random.h> 25399145f9SAnshuman Khandual #include <linux/spinlock.h> 26399145f9SAnshuman Khandual #include <linux/swap.h> 27399145f9SAnshuman Khandual #include <linux/swapops.h> 28399145f9SAnshuman Khandual #include <linux/start_kernel.h> 29399145f9SAnshuman Khandual #include <linux/sched/mm.h> 30399145f9SAnshuman Khandual #include <asm/pgalloc.h> 31399145f9SAnshuman Khandual 32399145f9SAnshuman Khandual #define VMFLAGS (VM_READ|VM_WRITE|VM_EXEC) 33399145f9SAnshuman Khandual 34399145f9SAnshuman Khandual /* 35399145f9SAnshuman Khandual * On s390 platform, the lower 4 bits are used to identify given page table 36399145f9SAnshuman Khandual * entry type. But these bits might affect the ability to clear entries with 37399145f9SAnshuman Khandual * pxx_clear() because of how dynamic page table folding works on s390. So 38399145f9SAnshuman Khandual * while loading up the entries do not change the lower 4 bits. It does not 39399145f9SAnshuman Khandual * have affect any other platform. 40399145f9SAnshuman Khandual */ 41399145f9SAnshuman Khandual #define S390_MASK_BITS 4 42399145f9SAnshuman Khandual #define RANDOM_ORVALUE GENMASK(BITS_PER_LONG - 1, S390_MASK_BITS) 43399145f9SAnshuman Khandual #define RANDOM_NZVALUE GENMASK(7, 0) 44399145f9SAnshuman Khandual 45399145f9SAnshuman Khandual static void __init pte_basic_tests(unsigned long pfn, pgprot_t prot) 46399145f9SAnshuman Khandual { 47399145f9SAnshuman Khandual pte_t pte = pfn_pte(pfn, prot); 48399145f9SAnshuman Khandual 49399145f9SAnshuman Khandual WARN_ON(!pte_same(pte, pte)); 50399145f9SAnshuman Khandual WARN_ON(!pte_young(pte_mkyoung(pte_mkold(pte)))); 51399145f9SAnshuman Khandual WARN_ON(!pte_dirty(pte_mkdirty(pte_mkclean(pte)))); 52399145f9SAnshuman Khandual WARN_ON(!pte_write(pte_mkwrite(pte_wrprotect(pte)))); 53399145f9SAnshuman Khandual WARN_ON(pte_young(pte_mkold(pte_mkyoung(pte)))); 54399145f9SAnshuman Khandual WARN_ON(pte_dirty(pte_mkclean(pte_mkdirty(pte)))); 55399145f9SAnshuman Khandual WARN_ON(pte_write(pte_wrprotect(pte_mkwrite(pte)))); 56399145f9SAnshuman Khandual } 57399145f9SAnshuman Khandual 58399145f9SAnshuman Khandual #ifdef CONFIG_TRANSPARENT_HUGEPAGE 59399145f9SAnshuman Khandual static void __init pmd_basic_tests(unsigned long pfn, pgprot_t prot) 60399145f9SAnshuman Khandual { 61399145f9SAnshuman Khandual pmd_t pmd = pfn_pmd(pfn, prot); 62399145f9SAnshuman Khandual 63*787d563bSAneesh Kumar K.V if (!has_transparent_hugepage()) 64*787d563bSAneesh Kumar K.V return; 65*787d563bSAneesh Kumar K.V 66399145f9SAnshuman Khandual WARN_ON(!pmd_same(pmd, pmd)); 67399145f9SAnshuman Khandual WARN_ON(!pmd_young(pmd_mkyoung(pmd_mkold(pmd)))); 68399145f9SAnshuman Khandual WARN_ON(!pmd_dirty(pmd_mkdirty(pmd_mkclean(pmd)))); 69399145f9SAnshuman Khandual WARN_ON(!pmd_write(pmd_mkwrite(pmd_wrprotect(pmd)))); 70399145f9SAnshuman Khandual WARN_ON(pmd_young(pmd_mkold(pmd_mkyoung(pmd)))); 71399145f9SAnshuman Khandual WARN_ON(pmd_dirty(pmd_mkclean(pmd_mkdirty(pmd)))); 72399145f9SAnshuman Khandual WARN_ON(pmd_write(pmd_wrprotect(pmd_mkwrite(pmd)))); 73399145f9SAnshuman Khandual /* 74399145f9SAnshuman Khandual * A huge page does not point to next level page table 75399145f9SAnshuman Khandual * entry. Hence this must qualify as pmd_bad(). 76399145f9SAnshuman Khandual */ 77399145f9SAnshuman Khandual WARN_ON(!pmd_bad(pmd_mkhuge(pmd))); 78399145f9SAnshuman Khandual } 79399145f9SAnshuman Khandual 80399145f9SAnshuman Khandual #ifdef CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD 81399145f9SAnshuman Khandual static void __init pud_basic_tests(unsigned long pfn, pgprot_t prot) 82399145f9SAnshuman Khandual { 83399145f9SAnshuman Khandual pud_t pud = pfn_pud(pfn, prot); 84399145f9SAnshuman Khandual 85*787d563bSAneesh Kumar K.V if (!has_transparent_hugepage()) 86*787d563bSAneesh Kumar K.V return; 87*787d563bSAneesh Kumar K.V 88399145f9SAnshuman Khandual WARN_ON(!pud_same(pud, pud)); 89399145f9SAnshuman Khandual WARN_ON(!pud_young(pud_mkyoung(pud_mkold(pud)))); 90399145f9SAnshuman Khandual WARN_ON(!pud_write(pud_mkwrite(pud_wrprotect(pud)))); 91399145f9SAnshuman Khandual WARN_ON(pud_write(pud_wrprotect(pud_mkwrite(pud)))); 92399145f9SAnshuman Khandual WARN_ON(pud_young(pud_mkold(pud_mkyoung(pud)))); 93399145f9SAnshuman Khandual 94399145f9SAnshuman Khandual if (mm_pmd_folded(mm)) 95399145f9SAnshuman Khandual return; 96399145f9SAnshuman Khandual 97399145f9SAnshuman Khandual /* 98399145f9SAnshuman Khandual * A huge page does not point to next level page table 99399145f9SAnshuman Khandual * entry. Hence this must qualify as pud_bad(). 100399145f9SAnshuman Khandual */ 101399145f9SAnshuman Khandual WARN_ON(!pud_bad(pud_mkhuge(pud))); 102399145f9SAnshuman Khandual } 103399145f9SAnshuman Khandual #else /* !CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD */ 104399145f9SAnshuman Khandual static void __init pud_basic_tests(unsigned long pfn, pgprot_t prot) { } 105399145f9SAnshuman Khandual #endif /* CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD */ 106399145f9SAnshuman Khandual #else /* !CONFIG_TRANSPARENT_HUGEPAGE */ 107399145f9SAnshuman Khandual static void __init pmd_basic_tests(unsigned long pfn, pgprot_t prot) { } 108399145f9SAnshuman Khandual static void __init pud_basic_tests(unsigned long pfn, pgprot_t prot) { } 109399145f9SAnshuman Khandual #endif /* CONFIG_TRANSPARENT_HUGEPAGE */ 110399145f9SAnshuman Khandual 111399145f9SAnshuman Khandual static void __init p4d_basic_tests(unsigned long pfn, pgprot_t prot) 112399145f9SAnshuman Khandual { 113399145f9SAnshuman Khandual p4d_t p4d; 114399145f9SAnshuman Khandual 115399145f9SAnshuman Khandual memset(&p4d, RANDOM_NZVALUE, sizeof(p4d_t)); 116399145f9SAnshuman Khandual WARN_ON(!p4d_same(p4d, p4d)); 117399145f9SAnshuman Khandual } 118399145f9SAnshuman Khandual 119399145f9SAnshuman Khandual static void __init pgd_basic_tests(unsigned long pfn, pgprot_t prot) 120399145f9SAnshuman Khandual { 121399145f9SAnshuman Khandual pgd_t pgd; 122399145f9SAnshuman Khandual 123399145f9SAnshuman Khandual memset(&pgd, RANDOM_NZVALUE, sizeof(pgd_t)); 124399145f9SAnshuman Khandual WARN_ON(!pgd_same(pgd, pgd)); 125399145f9SAnshuman Khandual } 126399145f9SAnshuman Khandual 127399145f9SAnshuman Khandual #ifndef __PAGETABLE_PUD_FOLDED 128399145f9SAnshuman Khandual static void __init pud_clear_tests(struct mm_struct *mm, pud_t *pudp) 129399145f9SAnshuman Khandual { 130399145f9SAnshuman Khandual pud_t pud = READ_ONCE(*pudp); 131399145f9SAnshuman Khandual 132399145f9SAnshuman Khandual if (mm_pmd_folded(mm)) 133399145f9SAnshuman Khandual return; 134399145f9SAnshuman Khandual 135399145f9SAnshuman Khandual pud = __pud(pud_val(pud) | RANDOM_ORVALUE); 136399145f9SAnshuman Khandual WRITE_ONCE(*pudp, pud); 137399145f9SAnshuman Khandual pud_clear(pudp); 138399145f9SAnshuman Khandual pud = READ_ONCE(*pudp); 139399145f9SAnshuman Khandual WARN_ON(!pud_none(pud)); 140399145f9SAnshuman Khandual } 141399145f9SAnshuman Khandual 142399145f9SAnshuman Khandual static void __init pud_populate_tests(struct mm_struct *mm, pud_t *pudp, 143399145f9SAnshuman Khandual pmd_t *pmdp) 144399145f9SAnshuman Khandual { 145399145f9SAnshuman Khandual pud_t pud; 146399145f9SAnshuman Khandual 147399145f9SAnshuman Khandual if (mm_pmd_folded(mm)) 148399145f9SAnshuman Khandual return; 149399145f9SAnshuman Khandual /* 150399145f9SAnshuman Khandual * This entry points to next level page table page. 151399145f9SAnshuman Khandual * Hence this must not qualify as pud_bad(). 152399145f9SAnshuman Khandual */ 153399145f9SAnshuman Khandual pmd_clear(pmdp); 154399145f9SAnshuman Khandual pud_clear(pudp); 155399145f9SAnshuman Khandual pud_populate(mm, pudp, pmdp); 156399145f9SAnshuman Khandual pud = READ_ONCE(*pudp); 157399145f9SAnshuman Khandual WARN_ON(pud_bad(pud)); 158399145f9SAnshuman Khandual } 159399145f9SAnshuman Khandual #else /* !__PAGETABLE_PUD_FOLDED */ 160399145f9SAnshuman Khandual static void __init pud_clear_tests(struct mm_struct *mm, pud_t *pudp) { } 161399145f9SAnshuman Khandual static void __init pud_populate_tests(struct mm_struct *mm, pud_t *pudp, 162399145f9SAnshuman Khandual pmd_t *pmdp) 163399145f9SAnshuman Khandual { 164399145f9SAnshuman Khandual } 165399145f9SAnshuman Khandual #endif /* PAGETABLE_PUD_FOLDED */ 166399145f9SAnshuman Khandual 167399145f9SAnshuman Khandual #ifndef __PAGETABLE_P4D_FOLDED 168399145f9SAnshuman Khandual static void __init p4d_clear_tests(struct mm_struct *mm, p4d_t *p4dp) 169399145f9SAnshuman Khandual { 170399145f9SAnshuman Khandual p4d_t p4d = READ_ONCE(*p4dp); 171399145f9SAnshuman Khandual 172399145f9SAnshuman Khandual if (mm_pud_folded(mm)) 173399145f9SAnshuman Khandual return; 174399145f9SAnshuman Khandual 175399145f9SAnshuman Khandual p4d = __p4d(p4d_val(p4d) | RANDOM_ORVALUE); 176399145f9SAnshuman Khandual WRITE_ONCE(*p4dp, p4d); 177399145f9SAnshuman Khandual p4d_clear(p4dp); 178399145f9SAnshuman Khandual p4d = READ_ONCE(*p4dp); 179399145f9SAnshuman Khandual WARN_ON(!p4d_none(p4d)); 180399145f9SAnshuman Khandual } 181399145f9SAnshuman Khandual 182399145f9SAnshuman Khandual static void __init p4d_populate_tests(struct mm_struct *mm, p4d_t *p4dp, 183399145f9SAnshuman Khandual pud_t *pudp) 184399145f9SAnshuman Khandual { 185399145f9SAnshuman Khandual p4d_t p4d; 186399145f9SAnshuman Khandual 187399145f9SAnshuman Khandual if (mm_pud_folded(mm)) 188399145f9SAnshuman Khandual return; 189399145f9SAnshuman Khandual 190399145f9SAnshuman Khandual /* 191399145f9SAnshuman Khandual * This entry points to next level page table page. 192399145f9SAnshuman Khandual * Hence this must not qualify as p4d_bad(). 193399145f9SAnshuman Khandual */ 194399145f9SAnshuman Khandual pud_clear(pudp); 195399145f9SAnshuman Khandual p4d_clear(p4dp); 196399145f9SAnshuman Khandual p4d_populate(mm, p4dp, pudp); 197399145f9SAnshuman Khandual p4d = READ_ONCE(*p4dp); 198399145f9SAnshuman Khandual WARN_ON(p4d_bad(p4d)); 199399145f9SAnshuman Khandual } 200399145f9SAnshuman Khandual 201399145f9SAnshuman Khandual static void __init pgd_clear_tests(struct mm_struct *mm, pgd_t *pgdp) 202399145f9SAnshuman Khandual { 203399145f9SAnshuman Khandual pgd_t pgd = READ_ONCE(*pgdp); 204399145f9SAnshuman Khandual 205399145f9SAnshuman Khandual if (mm_p4d_folded(mm)) 206399145f9SAnshuman Khandual return; 207399145f9SAnshuman Khandual 208399145f9SAnshuman Khandual pgd = __pgd(pgd_val(pgd) | RANDOM_ORVALUE); 209399145f9SAnshuman Khandual WRITE_ONCE(*pgdp, pgd); 210399145f9SAnshuman Khandual pgd_clear(pgdp); 211399145f9SAnshuman Khandual pgd = READ_ONCE(*pgdp); 212399145f9SAnshuman Khandual WARN_ON(!pgd_none(pgd)); 213399145f9SAnshuman Khandual } 214399145f9SAnshuman Khandual 215399145f9SAnshuman Khandual static void __init pgd_populate_tests(struct mm_struct *mm, pgd_t *pgdp, 216399145f9SAnshuman Khandual p4d_t *p4dp) 217399145f9SAnshuman Khandual { 218399145f9SAnshuman Khandual pgd_t pgd; 219399145f9SAnshuman Khandual 220399145f9SAnshuman Khandual if (mm_p4d_folded(mm)) 221399145f9SAnshuman Khandual return; 222399145f9SAnshuman Khandual 223399145f9SAnshuman Khandual /* 224399145f9SAnshuman Khandual * This entry points to next level page table page. 225399145f9SAnshuman Khandual * Hence this must not qualify as pgd_bad(). 226399145f9SAnshuman Khandual */ 227399145f9SAnshuman Khandual p4d_clear(p4dp); 228399145f9SAnshuman Khandual pgd_clear(pgdp); 229399145f9SAnshuman Khandual pgd_populate(mm, pgdp, p4dp); 230399145f9SAnshuman Khandual pgd = READ_ONCE(*pgdp); 231399145f9SAnshuman Khandual WARN_ON(pgd_bad(pgd)); 232399145f9SAnshuman Khandual } 233399145f9SAnshuman Khandual #else /* !__PAGETABLE_P4D_FOLDED */ 234399145f9SAnshuman Khandual static void __init p4d_clear_tests(struct mm_struct *mm, p4d_t *p4dp) { } 235399145f9SAnshuman Khandual static void __init pgd_clear_tests(struct mm_struct *mm, pgd_t *pgdp) { } 236399145f9SAnshuman Khandual static void __init p4d_populate_tests(struct mm_struct *mm, p4d_t *p4dp, 237399145f9SAnshuman Khandual pud_t *pudp) 238399145f9SAnshuman Khandual { 239399145f9SAnshuman Khandual } 240399145f9SAnshuman Khandual static void __init pgd_populate_tests(struct mm_struct *mm, pgd_t *pgdp, 241399145f9SAnshuman Khandual p4d_t *p4dp) 242399145f9SAnshuman Khandual { 243399145f9SAnshuman Khandual } 244399145f9SAnshuman Khandual #endif /* PAGETABLE_P4D_FOLDED */ 245399145f9SAnshuman Khandual 246399145f9SAnshuman Khandual static void __init pte_clear_tests(struct mm_struct *mm, pte_t *ptep, 247399145f9SAnshuman Khandual unsigned long vaddr) 248399145f9SAnshuman Khandual { 249399145f9SAnshuman Khandual pte_t pte = READ_ONCE(*ptep); 250399145f9SAnshuman Khandual 251399145f9SAnshuman Khandual pte = __pte(pte_val(pte) | RANDOM_ORVALUE); 252399145f9SAnshuman Khandual set_pte_at(mm, vaddr, ptep, pte); 253399145f9SAnshuman Khandual barrier(); 254399145f9SAnshuman Khandual pte_clear(mm, vaddr, ptep); 255399145f9SAnshuman Khandual pte = READ_ONCE(*ptep); 256399145f9SAnshuman Khandual WARN_ON(!pte_none(pte)); 257399145f9SAnshuman Khandual } 258399145f9SAnshuman Khandual 259399145f9SAnshuman Khandual static void __init pmd_clear_tests(struct mm_struct *mm, pmd_t *pmdp) 260399145f9SAnshuman Khandual { 261399145f9SAnshuman Khandual pmd_t pmd = READ_ONCE(*pmdp); 262399145f9SAnshuman Khandual 263399145f9SAnshuman Khandual pmd = __pmd(pmd_val(pmd) | RANDOM_ORVALUE); 264399145f9SAnshuman Khandual WRITE_ONCE(*pmdp, pmd); 265399145f9SAnshuman Khandual pmd_clear(pmdp); 266399145f9SAnshuman Khandual pmd = READ_ONCE(*pmdp); 267399145f9SAnshuman Khandual WARN_ON(!pmd_none(pmd)); 268399145f9SAnshuman Khandual } 269399145f9SAnshuman Khandual 270399145f9SAnshuman Khandual static void __init pmd_populate_tests(struct mm_struct *mm, pmd_t *pmdp, 271399145f9SAnshuman Khandual pgtable_t pgtable) 272399145f9SAnshuman Khandual { 273399145f9SAnshuman Khandual pmd_t pmd; 274399145f9SAnshuman Khandual 275399145f9SAnshuman Khandual /* 276399145f9SAnshuman Khandual * This entry points to next level page table page. 277399145f9SAnshuman Khandual * Hence this must not qualify as pmd_bad(). 278399145f9SAnshuman Khandual */ 279399145f9SAnshuman Khandual pmd_clear(pmdp); 280399145f9SAnshuman Khandual pmd_populate(mm, pmdp, pgtable); 281399145f9SAnshuman Khandual pmd = READ_ONCE(*pmdp); 282399145f9SAnshuman Khandual WARN_ON(pmd_bad(pmd)); 283399145f9SAnshuman Khandual } 284399145f9SAnshuman Khandual 285399145f9SAnshuman Khandual static unsigned long __init get_random_vaddr(void) 286399145f9SAnshuman Khandual { 287399145f9SAnshuman Khandual unsigned long random_vaddr, random_pages, total_user_pages; 288399145f9SAnshuman Khandual 289399145f9SAnshuman Khandual total_user_pages = (TASK_SIZE - FIRST_USER_ADDRESS) / PAGE_SIZE; 290399145f9SAnshuman Khandual 291399145f9SAnshuman Khandual random_pages = get_random_long() % total_user_pages; 292399145f9SAnshuman Khandual random_vaddr = FIRST_USER_ADDRESS + random_pages * PAGE_SIZE; 293399145f9SAnshuman Khandual 294399145f9SAnshuman Khandual return random_vaddr; 295399145f9SAnshuman Khandual } 296399145f9SAnshuman Khandual 297399145f9SAnshuman Khandual static int __init debug_vm_pgtable(void) 298399145f9SAnshuman Khandual { 299399145f9SAnshuman Khandual struct mm_struct *mm; 300399145f9SAnshuman Khandual pgd_t *pgdp; 301399145f9SAnshuman Khandual p4d_t *p4dp, *saved_p4dp; 302399145f9SAnshuman Khandual pud_t *pudp, *saved_pudp; 303399145f9SAnshuman Khandual pmd_t *pmdp, *saved_pmdp, pmd; 304399145f9SAnshuman Khandual pte_t *ptep; 305399145f9SAnshuman Khandual pgtable_t saved_ptep; 306399145f9SAnshuman Khandual pgprot_t prot; 307399145f9SAnshuman Khandual phys_addr_t paddr; 308399145f9SAnshuman Khandual unsigned long vaddr, pte_aligned, pmd_aligned; 309399145f9SAnshuman Khandual unsigned long pud_aligned, p4d_aligned, pgd_aligned; 310399145f9SAnshuman Khandual spinlock_t *uninitialized_var(ptl); 311399145f9SAnshuman Khandual 312399145f9SAnshuman Khandual pr_info("Validating architecture page table helpers\n"); 313399145f9SAnshuman Khandual prot = vm_get_page_prot(VMFLAGS); 314399145f9SAnshuman Khandual vaddr = get_random_vaddr(); 315399145f9SAnshuman Khandual mm = mm_alloc(); 316399145f9SAnshuman Khandual if (!mm) { 317399145f9SAnshuman Khandual pr_err("mm_struct allocation failed\n"); 318399145f9SAnshuman Khandual return 1; 319399145f9SAnshuman Khandual } 320399145f9SAnshuman Khandual 321399145f9SAnshuman Khandual /* 322399145f9SAnshuman Khandual * PFN for mapping at PTE level is determined from a standard kernel 323399145f9SAnshuman Khandual * text symbol. But pfns for higher page table levels are derived by 324399145f9SAnshuman Khandual * masking lower bits of this real pfn. These derived pfns might not 325399145f9SAnshuman Khandual * exist on the platform but that does not really matter as pfn_pxx() 326399145f9SAnshuman Khandual * helpers will still create appropriate entries for the test. This 327399145f9SAnshuman Khandual * helps avoid large memory block allocations to be used for mapping 328399145f9SAnshuman Khandual * at higher page table levels. 329399145f9SAnshuman Khandual */ 330399145f9SAnshuman Khandual paddr = __pa_symbol(&start_kernel); 331399145f9SAnshuman Khandual 332399145f9SAnshuman Khandual pte_aligned = (paddr & PAGE_MASK) >> PAGE_SHIFT; 333399145f9SAnshuman Khandual pmd_aligned = (paddr & PMD_MASK) >> PAGE_SHIFT; 334399145f9SAnshuman Khandual pud_aligned = (paddr & PUD_MASK) >> PAGE_SHIFT; 335399145f9SAnshuman Khandual p4d_aligned = (paddr & P4D_MASK) >> PAGE_SHIFT; 336399145f9SAnshuman Khandual pgd_aligned = (paddr & PGDIR_MASK) >> PAGE_SHIFT; 337399145f9SAnshuman Khandual WARN_ON(!pfn_valid(pte_aligned)); 338399145f9SAnshuman Khandual 339399145f9SAnshuman Khandual pgdp = pgd_offset(mm, vaddr); 340399145f9SAnshuman Khandual p4dp = p4d_alloc(mm, pgdp, vaddr); 341399145f9SAnshuman Khandual pudp = pud_alloc(mm, p4dp, vaddr); 342399145f9SAnshuman Khandual pmdp = pmd_alloc(mm, pudp, vaddr); 343399145f9SAnshuman Khandual ptep = pte_alloc_map_lock(mm, pmdp, vaddr, &ptl); 344399145f9SAnshuman Khandual 345399145f9SAnshuman Khandual /* 346399145f9SAnshuman Khandual * Save all the page table page addresses as the page table 347399145f9SAnshuman Khandual * entries will be used for testing with random or garbage 348399145f9SAnshuman Khandual * values. These saved addresses will be used for freeing 349399145f9SAnshuman Khandual * page table pages. 350399145f9SAnshuman Khandual */ 351399145f9SAnshuman Khandual pmd = READ_ONCE(*pmdp); 352399145f9SAnshuman Khandual saved_p4dp = p4d_offset(pgdp, 0UL); 353399145f9SAnshuman Khandual saved_pudp = pud_offset(p4dp, 0UL); 354399145f9SAnshuman Khandual saved_pmdp = pmd_offset(pudp, 0UL); 355399145f9SAnshuman Khandual saved_ptep = pmd_pgtable(pmd); 356399145f9SAnshuman Khandual 357399145f9SAnshuman Khandual pte_basic_tests(pte_aligned, prot); 358399145f9SAnshuman Khandual pmd_basic_tests(pmd_aligned, prot); 359399145f9SAnshuman Khandual pud_basic_tests(pud_aligned, prot); 360399145f9SAnshuman Khandual p4d_basic_tests(p4d_aligned, prot); 361399145f9SAnshuman Khandual pgd_basic_tests(pgd_aligned, prot); 362399145f9SAnshuman Khandual 363399145f9SAnshuman Khandual pte_clear_tests(mm, ptep, vaddr); 364399145f9SAnshuman Khandual pmd_clear_tests(mm, pmdp); 365399145f9SAnshuman Khandual pud_clear_tests(mm, pudp); 366399145f9SAnshuman Khandual p4d_clear_tests(mm, p4dp); 367399145f9SAnshuman Khandual pgd_clear_tests(mm, pgdp); 368399145f9SAnshuman Khandual 369399145f9SAnshuman Khandual pte_unmap_unlock(ptep, ptl); 370399145f9SAnshuman Khandual 371399145f9SAnshuman Khandual pmd_populate_tests(mm, pmdp, saved_ptep); 372399145f9SAnshuman Khandual pud_populate_tests(mm, pudp, saved_pmdp); 373399145f9SAnshuman Khandual p4d_populate_tests(mm, p4dp, saved_pudp); 374399145f9SAnshuman Khandual pgd_populate_tests(mm, pgdp, saved_p4dp); 375399145f9SAnshuman Khandual 376399145f9SAnshuman Khandual p4d_free(mm, saved_p4dp); 377399145f9SAnshuman Khandual pud_free(mm, saved_pudp); 378399145f9SAnshuman Khandual pmd_free(mm, saved_pmdp); 379399145f9SAnshuman Khandual pte_free(mm, saved_ptep); 380399145f9SAnshuman Khandual 381399145f9SAnshuman Khandual mm_dec_nr_puds(mm); 382399145f9SAnshuman Khandual mm_dec_nr_pmds(mm); 383399145f9SAnshuman Khandual mm_dec_nr_ptes(mm); 384399145f9SAnshuman Khandual mmdrop(mm); 385399145f9SAnshuman Khandual return 0; 386399145f9SAnshuman Khandual } 387399145f9SAnshuman Khandual late_initcall(debug_vm_pgtable); 388