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> 31*85a14463SAneesh Kumar K.V #include <linux/io.h> 32399145f9SAnshuman Khandual #include <asm/pgalloc.h> 33a5c3b9ffSAnshuman Khandual #include <asm/tlbflush.h> 34399145f9SAnshuman Khandual 35b1d00007SAnshuman Khandual /* 36b1d00007SAnshuman Khandual * Please refer Documentation/vm/arch_pgtable_helpers.rst for the semantics 37b1d00007SAnshuman Khandual * expectations that are being validated here. All future changes in here 38b1d00007SAnshuman Khandual * or the documentation need to be in sync. 39b1d00007SAnshuman Khandual */ 40b1d00007SAnshuman Khandual 41399145f9SAnshuman Khandual #define VMFLAGS (VM_READ|VM_WRITE|VM_EXEC) 42399145f9SAnshuman Khandual 43399145f9SAnshuman Khandual /* 44399145f9SAnshuman Khandual * On s390 platform, the lower 4 bits are used to identify given page table 45399145f9SAnshuman Khandual * entry type. But these bits might affect the ability to clear entries with 46399145f9SAnshuman Khandual * pxx_clear() because of how dynamic page table folding works on s390. So 47399145f9SAnshuman Khandual * while loading up the entries do not change the lower 4 bits. It does not 48cfc5bbc4SAneesh Kumar K.V * have affect any other platform. Also avoid the 62nd bit on ppc64 that is 49cfc5bbc4SAneesh Kumar K.V * used to mark a pte entry. 50399145f9SAnshuman Khandual */ 51cfc5bbc4SAneesh Kumar K.V #define S390_SKIP_MASK GENMASK(3, 0) 52cfc5bbc4SAneesh Kumar K.V #if __BITS_PER_LONG == 64 53cfc5bbc4SAneesh Kumar K.V #define PPC64_SKIP_MASK GENMASK(62, 62) 54cfc5bbc4SAneesh Kumar K.V #else 55cfc5bbc4SAneesh Kumar K.V #define PPC64_SKIP_MASK 0x0 56cfc5bbc4SAneesh Kumar K.V #endif 57cfc5bbc4SAneesh Kumar K.V #define ARCH_SKIP_MASK (S390_SKIP_MASK | PPC64_SKIP_MASK) 58cfc5bbc4SAneesh Kumar K.V #define RANDOM_ORVALUE (GENMASK(BITS_PER_LONG - 1, 0) & ~ARCH_SKIP_MASK) 59399145f9SAnshuman Khandual #define RANDOM_NZVALUE GENMASK(7, 0) 60399145f9SAnshuman Khandual 61399145f9SAnshuman Khandual static void __init pte_basic_tests(unsigned long pfn, pgprot_t prot) 62399145f9SAnshuman Khandual { 63399145f9SAnshuman Khandual pte_t pte = pfn_pte(pfn, prot); 64399145f9SAnshuman Khandual 656315df41SAnshuman Khandual pr_debug("Validating PTE basic\n"); 66399145f9SAnshuman Khandual WARN_ON(!pte_same(pte, pte)); 67399145f9SAnshuman Khandual WARN_ON(!pte_young(pte_mkyoung(pte_mkold(pte)))); 68399145f9SAnshuman Khandual WARN_ON(!pte_dirty(pte_mkdirty(pte_mkclean(pte)))); 69399145f9SAnshuman Khandual WARN_ON(!pte_write(pte_mkwrite(pte_wrprotect(pte)))); 70399145f9SAnshuman Khandual WARN_ON(pte_young(pte_mkold(pte_mkyoung(pte)))); 71399145f9SAnshuman Khandual WARN_ON(pte_dirty(pte_mkclean(pte_mkdirty(pte)))); 72399145f9SAnshuman Khandual WARN_ON(pte_write(pte_wrprotect(pte_mkwrite(pte)))); 73399145f9SAnshuman Khandual } 74399145f9SAnshuman Khandual 75a5c3b9ffSAnshuman Khandual static void __init pte_advanced_tests(struct mm_struct *mm, 76a5c3b9ffSAnshuman Khandual struct vm_area_struct *vma, pte_t *ptep, 77a5c3b9ffSAnshuman Khandual unsigned long pfn, unsigned long vaddr, 78a5c3b9ffSAnshuman Khandual pgprot_t prot) 79a5c3b9ffSAnshuman Khandual { 80a5c3b9ffSAnshuman Khandual pte_t pte = pfn_pte(pfn, prot); 81a5c3b9ffSAnshuman Khandual 826315df41SAnshuman Khandual pr_debug("Validating PTE advanced\n"); 83a5c3b9ffSAnshuman Khandual pte = pfn_pte(pfn, prot); 84a5c3b9ffSAnshuman Khandual set_pte_at(mm, vaddr, ptep, pte); 85a5c3b9ffSAnshuman Khandual ptep_set_wrprotect(mm, vaddr, ptep); 86a5c3b9ffSAnshuman Khandual pte = ptep_get(ptep); 87a5c3b9ffSAnshuman Khandual WARN_ON(pte_write(pte)); 88a5c3b9ffSAnshuman Khandual 89a5c3b9ffSAnshuman Khandual pte = pfn_pte(pfn, prot); 90a5c3b9ffSAnshuman Khandual set_pte_at(mm, vaddr, ptep, pte); 91a5c3b9ffSAnshuman Khandual ptep_get_and_clear(mm, vaddr, ptep); 92a5c3b9ffSAnshuman Khandual pte = ptep_get(ptep); 93a5c3b9ffSAnshuman Khandual WARN_ON(!pte_none(pte)); 94a5c3b9ffSAnshuman Khandual 95a5c3b9ffSAnshuman Khandual pte = pfn_pte(pfn, prot); 96a5c3b9ffSAnshuman Khandual pte = pte_wrprotect(pte); 97a5c3b9ffSAnshuman Khandual pte = pte_mkclean(pte); 98a5c3b9ffSAnshuman Khandual set_pte_at(mm, vaddr, ptep, pte); 99a5c3b9ffSAnshuman Khandual pte = pte_mkwrite(pte); 100a5c3b9ffSAnshuman Khandual pte = pte_mkdirty(pte); 101a5c3b9ffSAnshuman Khandual ptep_set_access_flags(vma, vaddr, ptep, pte, 1); 102a5c3b9ffSAnshuman Khandual pte = ptep_get(ptep); 103a5c3b9ffSAnshuman Khandual WARN_ON(!(pte_write(pte) && pte_dirty(pte))); 104a5c3b9ffSAnshuman Khandual 105a5c3b9ffSAnshuman Khandual pte = pfn_pte(pfn, prot); 106a5c3b9ffSAnshuman Khandual set_pte_at(mm, vaddr, ptep, pte); 107a5c3b9ffSAnshuman Khandual ptep_get_and_clear_full(mm, vaddr, ptep, 1); 108a5c3b9ffSAnshuman Khandual pte = ptep_get(ptep); 109a5c3b9ffSAnshuman Khandual WARN_ON(!pte_none(pte)); 110a5c3b9ffSAnshuman Khandual 111a5c3b9ffSAnshuman Khandual pte = pte_mkyoung(pte); 112a5c3b9ffSAnshuman Khandual set_pte_at(mm, vaddr, ptep, pte); 113a5c3b9ffSAnshuman Khandual ptep_test_and_clear_young(vma, vaddr, ptep); 114a5c3b9ffSAnshuman Khandual pte = ptep_get(ptep); 115a5c3b9ffSAnshuman Khandual WARN_ON(pte_young(pte)); 116a5c3b9ffSAnshuman Khandual } 117a5c3b9ffSAnshuman Khandual 118a5c3b9ffSAnshuman Khandual static void __init pte_savedwrite_tests(unsigned long pfn, pgprot_t prot) 119a5c3b9ffSAnshuman Khandual { 120a5c3b9ffSAnshuman Khandual pte_t pte = pfn_pte(pfn, prot); 121a5c3b9ffSAnshuman Khandual 1226315df41SAnshuman Khandual pr_debug("Validating PTE saved write\n"); 123a5c3b9ffSAnshuman Khandual WARN_ON(!pte_savedwrite(pte_mk_savedwrite(pte_clear_savedwrite(pte)))); 124a5c3b9ffSAnshuman Khandual WARN_ON(pte_savedwrite(pte_clear_savedwrite(pte_mk_savedwrite(pte)))); 125a5c3b9ffSAnshuman Khandual } 126399145f9SAnshuman Khandual #ifdef CONFIG_TRANSPARENT_HUGEPAGE 127399145f9SAnshuman Khandual static void __init pmd_basic_tests(unsigned long pfn, pgprot_t prot) 128399145f9SAnshuman Khandual { 129399145f9SAnshuman Khandual pmd_t pmd = pfn_pmd(pfn, prot); 130399145f9SAnshuman Khandual 131787d563bSAneesh Kumar K.V if (!has_transparent_hugepage()) 132787d563bSAneesh Kumar K.V return; 133787d563bSAneesh Kumar K.V 1346315df41SAnshuman Khandual pr_debug("Validating PMD basic\n"); 135399145f9SAnshuman Khandual WARN_ON(!pmd_same(pmd, pmd)); 136399145f9SAnshuman Khandual WARN_ON(!pmd_young(pmd_mkyoung(pmd_mkold(pmd)))); 137399145f9SAnshuman Khandual WARN_ON(!pmd_dirty(pmd_mkdirty(pmd_mkclean(pmd)))); 138399145f9SAnshuman Khandual WARN_ON(!pmd_write(pmd_mkwrite(pmd_wrprotect(pmd)))); 139399145f9SAnshuman Khandual WARN_ON(pmd_young(pmd_mkold(pmd_mkyoung(pmd)))); 140399145f9SAnshuman Khandual WARN_ON(pmd_dirty(pmd_mkclean(pmd_mkdirty(pmd)))); 141399145f9SAnshuman Khandual WARN_ON(pmd_write(pmd_wrprotect(pmd_mkwrite(pmd)))); 142399145f9SAnshuman Khandual /* 143399145f9SAnshuman Khandual * A huge page does not point to next level page table 144399145f9SAnshuman Khandual * entry. Hence this must qualify as pmd_bad(). 145399145f9SAnshuman Khandual */ 146399145f9SAnshuman Khandual WARN_ON(!pmd_bad(pmd_mkhuge(pmd))); 147399145f9SAnshuman Khandual } 148399145f9SAnshuman Khandual 149a5c3b9ffSAnshuman Khandual static void __init pmd_advanced_tests(struct mm_struct *mm, 150a5c3b9ffSAnshuman Khandual struct vm_area_struct *vma, pmd_t *pmdp, 151a5c3b9ffSAnshuman Khandual unsigned long pfn, unsigned long vaddr, 152a5c3b9ffSAnshuman Khandual pgprot_t prot) 153a5c3b9ffSAnshuman Khandual { 154a5c3b9ffSAnshuman Khandual pmd_t pmd = pfn_pmd(pfn, prot); 155a5c3b9ffSAnshuman Khandual 156a5c3b9ffSAnshuman Khandual if (!has_transparent_hugepage()) 157a5c3b9ffSAnshuman Khandual return; 158a5c3b9ffSAnshuman Khandual 1596315df41SAnshuman Khandual pr_debug("Validating PMD advanced\n"); 160a5c3b9ffSAnshuman Khandual /* Align the address wrt HPAGE_PMD_SIZE */ 161a5c3b9ffSAnshuman Khandual vaddr = (vaddr & HPAGE_PMD_MASK) + HPAGE_PMD_SIZE; 162a5c3b9ffSAnshuman Khandual 163a5c3b9ffSAnshuman Khandual pmd = pfn_pmd(pfn, prot); 164a5c3b9ffSAnshuman Khandual set_pmd_at(mm, vaddr, pmdp, pmd); 165a5c3b9ffSAnshuman Khandual pmdp_set_wrprotect(mm, vaddr, pmdp); 166a5c3b9ffSAnshuman Khandual pmd = READ_ONCE(*pmdp); 167a5c3b9ffSAnshuman Khandual WARN_ON(pmd_write(pmd)); 168a5c3b9ffSAnshuman Khandual 169a5c3b9ffSAnshuman Khandual pmd = pfn_pmd(pfn, prot); 170a5c3b9ffSAnshuman Khandual set_pmd_at(mm, vaddr, pmdp, pmd); 171a5c3b9ffSAnshuman Khandual pmdp_huge_get_and_clear(mm, vaddr, pmdp); 172a5c3b9ffSAnshuman Khandual pmd = READ_ONCE(*pmdp); 173a5c3b9ffSAnshuman Khandual WARN_ON(!pmd_none(pmd)); 174a5c3b9ffSAnshuman Khandual 175a5c3b9ffSAnshuman Khandual pmd = pfn_pmd(pfn, prot); 176a5c3b9ffSAnshuman Khandual pmd = pmd_wrprotect(pmd); 177a5c3b9ffSAnshuman Khandual pmd = pmd_mkclean(pmd); 178a5c3b9ffSAnshuman Khandual set_pmd_at(mm, vaddr, pmdp, pmd); 179a5c3b9ffSAnshuman Khandual pmd = pmd_mkwrite(pmd); 180a5c3b9ffSAnshuman Khandual pmd = pmd_mkdirty(pmd); 181a5c3b9ffSAnshuman Khandual pmdp_set_access_flags(vma, vaddr, pmdp, pmd, 1); 182a5c3b9ffSAnshuman Khandual pmd = READ_ONCE(*pmdp); 183a5c3b9ffSAnshuman Khandual WARN_ON(!(pmd_write(pmd) && pmd_dirty(pmd))); 184a5c3b9ffSAnshuman Khandual 185a5c3b9ffSAnshuman Khandual pmd = pmd_mkhuge(pfn_pmd(pfn, prot)); 186a5c3b9ffSAnshuman Khandual set_pmd_at(mm, vaddr, pmdp, pmd); 187a5c3b9ffSAnshuman Khandual pmdp_huge_get_and_clear_full(vma, vaddr, pmdp, 1); 188a5c3b9ffSAnshuman Khandual pmd = READ_ONCE(*pmdp); 189a5c3b9ffSAnshuman Khandual WARN_ON(!pmd_none(pmd)); 190a5c3b9ffSAnshuman Khandual 191a5c3b9ffSAnshuman Khandual pmd = pmd_mkyoung(pmd); 192a5c3b9ffSAnshuman Khandual set_pmd_at(mm, vaddr, pmdp, pmd); 193a5c3b9ffSAnshuman Khandual pmdp_test_and_clear_young(vma, vaddr, pmdp); 194a5c3b9ffSAnshuman Khandual pmd = READ_ONCE(*pmdp); 195a5c3b9ffSAnshuman Khandual WARN_ON(pmd_young(pmd)); 196a5c3b9ffSAnshuman Khandual } 197a5c3b9ffSAnshuman Khandual 198a5c3b9ffSAnshuman Khandual static void __init pmd_leaf_tests(unsigned long pfn, pgprot_t prot) 199a5c3b9ffSAnshuman Khandual { 200a5c3b9ffSAnshuman Khandual pmd_t pmd = pfn_pmd(pfn, prot); 201a5c3b9ffSAnshuman Khandual 2026315df41SAnshuman Khandual pr_debug("Validating PMD leaf\n"); 203a5c3b9ffSAnshuman Khandual /* 204a5c3b9ffSAnshuman Khandual * PMD based THP is a leaf entry. 205a5c3b9ffSAnshuman Khandual */ 206a5c3b9ffSAnshuman Khandual pmd = pmd_mkhuge(pmd); 207a5c3b9ffSAnshuman Khandual WARN_ON(!pmd_leaf(pmd)); 208a5c3b9ffSAnshuman Khandual } 209a5c3b9ffSAnshuman Khandual 210*85a14463SAneesh Kumar K.V #ifdef CONFIG_HAVE_ARCH_HUGE_VMAP 211a5c3b9ffSAnshuman Khandual static void __init pmd_huge_tests(pmd_t *pmdp, unsigned long pfn, pgprot_t prot) 212a5c3b9ffSAnshuman Khandual { 213a5c3b9ffSAnshuman Khandual pmd_t pmd; 214a5c3b9ffSAnshuman Khandual 215*85a14463SAneesh Kumar K.V if (!arch_ioremap_pmd_supported()) 216a5c3b9ffSAnshuman Khandual return; 2176315df41SAnshuman Khandual 2186315df41SAnshuman Khandual pr_debug("Validating PMD huge\n"); 219a5c3b9ffSAnshuman Khandual /* 220a5c3b9ffSAnshuman Khandual * X86 defined pmd_set_huge() verifies that the given 221a5c3b9ffSAnshuman Khandual * PMD is not a populated non-leaf entry. 222a5c3b9ffSAnshuman Khandual */ 223a5c3b9ffSAnshuman Khandual WRITE_ONCE(*pmdp, __pmd(0)); 224a5c3b9ffSAnshuman Khandual WARN_ON(!pmd_set_huge(pmdp, __pfn_to_phys(pfn), prot)); 225a5c3b9ffSAnshuman Khandual WARN_ON(!pmd_clear_huge(pmdp)); 226a5c3b9ffSAnshuman Khandual pmd = READ_ONCE(*pmdp); 227a5c3b9ffSAnshuman Khandual WARN_ON(!pmd_none(pmd)); 228a5c3b9ffSAnshuman Khandual } 229*85a14463SAneesh Kumar K.V #else /* CONFIG_HAVE_ARCH_HUGE_VMAP */ 230*85a14463SAneesh Kumar K.V static void __init pmd_huge_tests(pmd_t *pmdp, unsigned long pfn, pgprot_t prot) { } 231*85a14463SAneesh Kumar K.V #endif /* CONFIG_HAVE_ARCH_HUGE_VMAP */ 232a5c3b9ffSAnshuman Khandual 233a5c3b9ffSAnshuman Khandual static void __init pmd_savedwrite_tests(unsigned long pfn, pgprot_t prot) 234a5c3b9ffSAnshuman Khandual { 235a5c3b9ffSAnshuman Khandual pmd_t pmd = pfn_pmd(pfn, prot); 236a5c3b9ffSAnshuman Khandual 2376315df41SAnshuman Khandual pr_debug("Validating PMD saved write\n"); 238a5c3b9ffSAnshuman Khandual WARN_ON(!pmd_savedwrite(pmd_mk_savedwrite(pmd_clear_savedwrite(pmd)))); 239a5c3b9ffSAnshuman Khandual WARN_ON(pmd_savedwrite(pmd_clear_savedwrite(pmd_mk_savedwrite(pmd)))); 240a5c3b9ffSAnshuman Khandual } 241a5c3b9ffSAnshuman Khandual 242399145f9SAnshuman Khandual #ifdef CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD 243399145f9SAnshuman Khandual static void __init pud_basic_tests(unsigned long pfn, pgprot_t prot) 244399145f9SAnshuman Khandual { 245399145f9SAnshuman Khandual pud_t pud = pfn_pud(pfn, prot); 246399145f9SAnshuman Khandual 247787d563bSAneesh Kumar K.V if (!has_transparent_hugepage()) 248787d563bSAneesh Kumar K.V return; 249787d563bSAneesh Kumar K.V 2506315df41SAnshuman Khandual pr_debug("Validating PUD basic\n"); 251399145f9SAnshuman Khandual WARN_ON(!pud_same(pud, pud)); 252399145f9SAnshuman Khandual WARN_ON(!pud_young(pud_mkyoung(pud_mkold(pud)))); 253399145f9SAnshuman Khandual WARN_ON(!pud_write(pud_mkwrite(pud_wrprotect(pud)))); 254399145f9SAnshuman Khandual WARN_ON(pud_write(pud_wrprotect(pud_mkwrite(pud)))); 255399145f9SAnshuman Khandual WARN_ON(pud_young(pud_mkold(pud_mkyoung(pud)))); 256399145f9SAnshuman Khandual 257399145f9SAnshuman Khandual if (mm_pmd_folded(mm)) 258399145f9SAnshuman Khandual return; 259399145f9SAnshuman Khandual 260399145f9SAnshuman Khandual /* 261399145f9SAnshuman Khandual * A huge page does not point to next level page table 262399145f9SAnshuman Khandual * entry. Hence this must qualify as pud_bad(). 263399145f9SAnshuman Khandual */ 264399145f9SAnshuman Khandual WARN_ON(!pud_bad(pud_mkhuge(pud))); 265399145f9SAnshuman Khandual } 266a5c3b9ffSAnshuman Khandual 267a5c3b9ffSAnshuman Khandual static void __init pud_advanced_tests(struct mm_struct *mm, 268a5c3b9ffSAnshuman Khandual struct vm_area_struct *vma, pud_t *pudp, 269a5c3b9ffSAnshuman Khandual unsigned long pfn, unsigned long vaddr, 270a5c3b9ffSAnshuman Khandual pgprot_t prot) 271a5c3b9ffSAnshuman Khandual { 272a5c3b9ffSAnshuman Khandual pud_t pud = pfn_pud(pfn, prot); 273a5c3b9ffSAnshuman Khandual 274a5c3b9ffSAnshuman Khandual if (!has_transparent_hugepage()) 275a5c3b9ffSAnshuman Khandual return; 276a5c3b9ffSAnshuman Khandual 2776315df41SAnshuman Khandual pr_debug("Validating PUD advanced\n"); 278a5c3b9ffSAnshuman Khandual /* Align the address wrt HPAGE_PUD_SIZE */ 279a5c3b9ffSAnshuman Khandual vaddr = (vaddr & HPAGE_PUD_MASK) + HPAGE_PUD_SIZE; 280a5c3b9ffSAnshuman Khandual 281a5c3b9ffSAnshuman Khandual set_pud_at(mm, vaddr, pudp, pud); 282a5c3b9ffSAnshuman Khandual pudp_set_wrprotect(mm, vaddr, pudp); 283a5c3b9ffSAnshuman Khandual pud = READ_ONCE(*pudp); 284a5c3b9ffSAnshuman Khandual WARN_ON(pud_write(pud)); 285a5c3b9ffSAnshuman Khandual 286a5c3b9ffSAnshuman Khandual #ifndef __PAGETABLE_PMD_FOLDED 287a5c3b9ffSAnshuman Khandual pud = pfn_pud(pfn, prot); 288a5c3b9ffSAnshuman Khandual set_pud_at(mm, vaddr, pudp, pud); 289a5c3b9ffSAnshuman Khandual pudp_huge_get_and_clear(mm, vaddr, pudp); 290a5c3b9ffSAnshuman Khandual pud = READ_ONCE(*pudp); 291a5c3b9ffSAnshuman Khandual WARN_ON(!pud_none(pud)); 292a5c3b9ffSAnshuman Khandual 293a5c3b9ffSAnshuman Khandual pud = pfn_pud(pfn, prot); 294a5c3b9ffSAnshuman Khandual set_pud_at(mm, vaddr, pudp, pud); 295a5c3b9ffSAnshuman Khandual pudp_huge_get_and_clear_full(mm, vaddr, pudp, 1); 296a5c3b9ffSAnshuman Khandual pud = READ_ONCE(*pudp); 297a5c3b9ffSAnshuman Khandual WARN_ON(!pud_none(pud)); 298a5c3b9ffSAnshuman Khandual #endif /* __PAGETABLE_PMD_FOLDED */ 299a5c3b9ffSAnshuman Khandual pud = pfn_pud(pfn, prot); 300a5c3b9ffSAnshuman Khandual pud = pud_wrprotect(pud); 301a5c3b9ffSAnshuman Khandual pud = pud_mkclean(pud); 302a5c3b9ffSAnshuman Khandual set_pud_at(mm, vaddr, pudp, pud); 303a5c3b9ffSAnshuman Khandual pud = pud_mkwrite(pud); 304a5c3b9ffSAnshuman Khandual pud = pud_mkdirty(pud); 305a5c3b9ffSAnshuman Khandual pudp_set_access_flags(vma, vaddr, pudp, pud, 1); 306a5c3b9ffSAnshuman Khandual pud = READ_ONCE(*pudp); 307a5c3b9ffSAnshuman Khandual WARN_ON(!(pud_write(pud) && pud_dirty(pud))); 308a5c3b9ffSAnshuman Khandual 309a5c3b9ffSAnshuman Khandual pud = pud_mkyoung(pud); 310a5c3b9ffSAnshuman Khandual set_pud_at(mm, vaddr, pudp, pud); 311a5c3b9ffSAnshuman Khandual pudp_test_and_clear_young(vma, vaddr, pudp); 312a5c3b9ffSAnshuman Khandual pud = READ_ONCE(*pudp); 313a5c3b9ffSAnshuman Khandual WARN_ON(pud_young(pud)); 314a5c3b9ffSAnshuman Khandual } 315a5c3b9ffSAnshuman Khandual 316a5c3b9ffSAnshuman Khandual static void __init pud_leaf_tests(unsigned long pfn, pgprot_t prot) 317a5c3b9ffSAnshuman Khandual { 318a5c3b9ffSAnshuman Khandual pud_t pud = pfn_pud(pfn, prot); 319a5c3b9ffSAnshuman Khandual 3206315df41SAnshuman Khandual pr_debug("Validating PUD leaf\n"); 321a5c3b9ffSAnshuman Khandual /* 322a5c3b9ffSAnshuman Khandual * PUD based THP is a leaf entry. 323a5c3b9ffSAnshuman Khandual */ 324a5c3b9ffSAnshuman Khandual pud = pud_mkhuge(pud); 325a5c3b9ffSAnshuman Khandual WARN_ON(!pud_leaf(pud)); 326a5c3b9ffSAnshuman Khandual } 327a5c3b9ffSAnshuman Khandual 328*85a14463SAneesh Kumar K.V #ifdef CONFIG_HAVE_ARCH_HUGE_VMAP 329a5c3b9ffSAnshuman Khandual static void __init pud_huge_tests(pud_t *pudp, unsigned long pfn, pgprot_t prot) 330a5c3b9ffSAnshuman Khandual { 331a5c3b9ffSAnshuman Khandual pud_t pud; 332a5c3b9ffSAnshuman Khandual 333*85a14463SAneesh Kumar K.V if (!arch_ioremap_pud_supported()) 334a5c3b9ffSAnshuman Khandual return; 3356315df41SAnshuman Khandual 3366315df41SAnshuman Khandual pr_debug("Validating PUD huge\n"); 337a5c3b9ffSAnshuman Khandual /* 338a5c3b9ffSAnshuman Khandual * X86 defined pud_set_huge() verifies that the given 339a5c3b9ffSAnshuman Khandual * PUD is not a populated non-leaf entry. 340a5c3b9ffSAnshuman Khandual */ 341a5c3b9ffSAnshuman Khandual WRITE_ONCE(*pudp, __pud(0)); 342a5c3b9ffSAnshuman Khandual WARN_ON(!pud_set_huge(pudp, __pfn_to_phys(pfn), prot)); 343a5c3b9ffSAnshuman Khandual WARN_ON(!pud_clear_huge(pudp)); 344a5c3b9ffSAnshuman Khandual pud = READ_ONCE(*pudp); 345a5c3b9ffSAnshuman Khandual WARN_ON(!pud_none(pud)); 346a5c3b9ffSAnshuman Khandual } 347*85a14463SAneesh Kumar K.V #else /* !CONFIG_HAVE_ARCH_HUGE_VMAP */ 348*85a14463SAneesh Kumar K.V static void __init pud_huge_tests(pud_t *pudp, unsigned long pfn, pgprot_t prot) { } 349*85a14463SAneesh Kumar K.V #endif /* !CONFIG_HAVE_ARCH_HUGE_VMAP */ 350*85a14463SAneesh Kumar K.V 351399145f9SAnshuman Khandual #else /* !CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD */ 352399145f9SAnshuman Khandual static void __init pud_basic_tests(unsigned long pfn, pgprot_t prot) { } 353a5c3b9ffSAnshuman Khandual static void __init pud_advanced_tests(struct mm_struct *mm, 354a5c3b9ffSAnshuman Khandual struct vm_area_struct *vma, pud_t *pudp, 355a5c3b9ffSAnshuman Khandual unsigned long pfn, unsigned long vaddr, 356a5c3b9ffSAnshuman Khandual pgprot_t prot) 357a5c3b9ffSAnshuman Khandual { 358a5c3b9ffSAnshuman Khandual } 359a5c3b9ffSAnshuman Khandual static void __init pud_leaf_tests(unsigned long pfn, pgprot_t prot) { } 360a5c3b9ffSAnshuman Khandual static void __init pud_huge_tests(pud_t *pudp, unsigned long pfn, pgprot_t prot) 361a5c3b9ffSAnshuman Khandual { 362a5c3b9ffSAnshuman Khandual } 363399145f9SAnshuman Khandual #endif /* CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD */ 364399145f9SAnshuman Khandual #else /* !CONFIG_TRANSPARENT_HUGEPAGE */ 365399145f9SAnshuman Khandual static void __init pmd_basic_tests(unsigned long pfn, pgprot_t prot) { } 366399145f9SAnshuman Khandual static void __init pud_basic_tests(unsigned long pfn, pgprot_t prot) { } 367a5c3b9ffSAnshuman Khandual static void __init pmd_advanced_tests(struct mm_struct *mm, 368a5c3b9ffSAnshuman Khandual struct vm_area_struct *vma, pmd_t *pmdp, 369a5c3b9ffSAnshuman Khandual unsigned long pfn, unsigned long vaddr, 370a5c3b9ffSAnshuman Khandual pgprot_t prot) 371a5c3b9ffSAnshuman Khandual { 372a5c3b9ffSAnshuman Khandual } 373a5c3b9ffSAnshuman Khandual static void __init pud_advanced_tests(struct mm_struct *mm, 374a5c3b9ffSAnshuman Khandual struct vm_area_struct *vma, pud_t *pudp, 375a5c3b9ffSAnshuman Khandual unsigned long pfn, unsigned long vaddr, 376a5c3b9ffSAnshuman Khandual pgprot_t prot) 377a5c3b9ffSAnshuman Khandual { 378a5c3b9ffSAnshuman Khandual } 379a5c3b9ffSAnshuman Khandual static void __init pmd_leaf_tests(unsigned long pfn, pgprot_t prot) { } 380a5c3b9ffSAnshuman Khandual static void __init pud_leaf_tests(unsigned long pfn, pgprot_t prot) { } 381a5c3b9ffSAnshuman Khandual static void __init pmd_huge_tests(pmd_t *pmdp, unsigned long pfn, pgprot_t prot) 382a5c3b9ffSAnshuman Khandual { 383a5c3b9ffSAnshuman Khandual } 384a5c3b9ffSAnshuman Khandual static void __init pud_huge_tests(pud_t *pudp, unsigned long pfn, pgprot_t prot) 385a5c3b9ffSAnshuman Khandual { 386a5c3b9ffSAnshuman Khandual } 387a5c3b9ffSAnshuman Khandual static void __init pmd_savedwrite_tests(unsigned long pfn, pgprot_t prot) { } 388399145f9SAnshuman Khandual #endif /* CONFIG_TRANSPARENT_HUGEPAGE */ 389399145f9SAnshuman Khandual 390399145f9SAnshuman Khandual static void __init p4d_basic_tests(unsigned long pfn, pgprot_t prot) 391399145f9SAnshuman Khandual { 392399145f9SAnshuman Khandual p4d_t p4d; 393399145f9SAnshuman Khandual 3946315df41SAnshuman Khandual pr_debug("Validating P4D basic\n"); 395399145f9SAnshuman Khandual memset(&p4d, RANDOM_NZVALUE, sizeof(p4d_t)); 396399145f9SAnshuman Khandual WARN_ON(!p4d_same(p4d, p4d)); 397399145f9SAnshuman Khandual } 398399145f9SAnshuman Khandual 399399145f9SAnshuman Khandual static void __init pgd_basic_tests(unsigned long pfn, pgprot_t prot) 400399145f9SAnshuman Khandual { 401399145f9SAnshuman Khandual pgd_t pgd; 402399145f9SAnshuman Khandual 4036315df41SAnshuman Khandual pr_debug("Validating PGD basic\n"); 404399145f9SAnshuman Khandual memset(&pgd, RANDOM_NZVALUE, sizeof(pgd_t)); 405399145f9SAnshuman Khandual WARN_ON(!pgd_same(pgd, pgd)); 406399145f9SAnshuman Khandual } 407399145f9SAnshuman Khandual 408399145f9SAnshuman Khandual #ifndef __PAGETABLE_PUD_FOLDED 409399145f9SAnshuman Khandual static void __init pud_clear_tests(struct mm_struct *mm, pud_t *pudp) 410399145f9SAnshuman Khandual { 411399145f9SAnshuman Khandual pud_t pud = READ_ONCE(*pudp); 412399145f9SAnshuman Khandual 413399145f9SAnshuman Khandual if (mm_pmd_folded(mm)) 414399145f9SAnshuman Khandual return; 415399145f9SAnshuman Khandual 4166315df41SAnshuman Khandual pr_debug("Validating PUD clear\n"); 417399145f9SAnshuman Khandual pud = __pud(pud_val(pud) | RANDOM_ORVALUE); 418399145f9SAnshuman Khandual WRITE_ONCE(*pudp, pud); 419399145f9SAnshuman Khandual pud_clear(pudp); 420399145f9SAnshuman Khandual pud = READ_ONCE(*pudp); 421399145f9SAnshuman Khandual WARN_ON(!pud_none(pud)); 422399145f9SAnshuman Khandual } 423399145f9SAnshuman Khandual 424399145f9SAnshuman Khandual static void __init pud_populate_tests(struct mm_struct *mm, pud_t *pudp, 425399145f9SAnshuman Khandual pmd_t *pmdp) 426399145f9SAnshuman Khandual { 427399145f9SAnshuman Khandual pud_t pud; 428399145f9SAnshuman Khandual 429399145f9SAnshuman Khandual if (mm_pmd_folded(mm)) 430399145f9SAnshuman Khandual return; 4316315df41SAnshuman Khandual 4326315df41SAnshuman Khandual pr_debug("Validating PUD populate\n"); 433399145f9SAnshuman Khandual /* 434399145f9SAnshuman Khandual * This entry points to next level page table page. 435399145f9SAnshuman Khandual * Hence this must not qualify as pud_bad(). 436399145f9SAnshuman Khandual */ 437399145f9SAnshuman Khandual pmd_clear(pmdp); 438399145f9SAnshuman Khandual pud_clear(pudp); 439399145f9SAnshuman Khandual pud_populate(mm, pudp, pmdp); 440399145f9SAnshuman Khandual pud = READ_ONCE(*pudp); 441399145f9SAnshuman Khandual WARN_ON(pud_bad(pud)); 442399145f9SAnshuman Khandual } 443399145f9SAnshuman Khandual #else /* !__PAGETABLE_PUD_FOLDED */ 444399145f9SAnshuman Khandual static void __init pud_clear_tests(struct mm_struct *mm, pud_t *pudp) { } 445399145f9SAnshuman Khandual static void __init pud_populate_tests(struct mm_struct *mm, pud_t *pudp, 446399145f9SAnshuman Khandual pmd_t *pmdp) 447399145f9SAnshuman Khandual { 448399145f9SAnshuman Khandual } 449399145f9SAnshuman Khandual #endif /* PAGETABLE_PUD_FOLDED */ 450399145f9SAnshuman Khandual 451399145f9SAnshuman Khandual #ifndef __PAGETABLE_P4D_FOLDED 452399145f9SAnshuman Khandual static void __init p4d_clear_tests(struct mm_struct *mm, p4d_t *p4dp) 453399145f9SAnshuman Khandual { 454399145f9SAnshuman Khandual p4d_t p4d = READ_ONCE(*p4dp); 455399145f9SAnshuman Khandual 456399145f9SAnshuman Khandual if (mm_pud_folded(mm)) 457399145f9SAnshuman Khandual return; 458399145f9SAnshuman Khandual 4596315df41SAnshuman Khandual pr_debug("Validating P4D clear\n"); 460399145f9SAnshuman Khandual p4d = __p4d(p4d_val(p4d) | RANDOM_ORVALUE); 461399145f9SAnshuman Khandual WRITE_ONCE(*p4dp, p4d); 462399145f9SAnshuman Khandual p4d_clear(p4dp); 463399145f9SAnshuman Khandual p4d = READ_ONCE(*p4dp); 464399145f9SAnshuman Khandual WARN_ON(!p4d_none(p4d)); 465399145f9SAnshuman Khandual } 466399145f9SAnshuman Khandual 467399145f9SAnshuman Khandual static void __init p4d_populate_tests(struct mm_struct *mm, p4d_t *p4dp, 468399145f9SAnshuman Khandual pud_t *pudp) 469399145f9SAnshuman Khandual { 470399145f9SAnshuman Khandual p4d_t p4d; 471399145f9SAnshuman Khandual 472399145f9SAnshuman Khandual if (mm_pud_folded(mm)) 473399145f9SAnshuman Khandual return; 474399145f9SAnshuman Khandual 4756315df41SAnshuman Khandual pr_debug("Validating P4D populate\n"); 476399145f9SAnshuman Khandual /* 477399145f9SAnshuman Khandual * This entry points to next level page table page. 478399145f9SAnshuman Khandual * Hence this must not qualify as p4d_bad(). 479399145f9SAnshuman Khandual */ 480399145f9SAnshuman Khandual pud_clear(pudp); 481399145f9SAnshuman Khandual p4d_clear(p4dp); 482399145f9SAnshuman Khandual p4d_populate(mm, p4dp, pudp); 483399145f9SAnshuman Khandual p4d = READ_ONCE(*p4dp); 484399145f9SAnshuman Khandual WARN_ON(p4d_bad(p4d)); 485399145f9SAnshuman Khandual } 486399145f9SAnshuman Khandual 487399145f9SAnshuman Khandual static void __init pgd_clear_tests(struct mm_struct *mm, pgd_t *pgdp) 488399145f9SAnshuman Khandual { 489399145f9SAnshuman Khandual pgd_t pgd = READ_ONCE(*pgdp); 490399145f9SAnshuman Khandual 491399145f9SAnshuman Khandual if (mm_p4d_folded(mm)) 492399145f9SAnshuman Khandual return; 493399145f9SAnshuman Khandual 4946315df41SAnshuman Khandual pr_debug("Validating PGD clear\n"); 495399145f9SAnshuman Khandual pgd = __pgd(pgd_val(pgd) | RANDOM_ORVALUE); 496399145f9SAnshuman Khandual WRITE_ONCE(*pgdp, pgd); 497399145f9SAnshuman Khandual pgd_clear(pgdp); 498399145f9SAnshuman Khandual pgd = READ_ONCE(*pgdp); 499399145f9SAnshuman Khandual WARN_ON(!pgd_none(pgd)); 500399145f9SAnshuman Khandual } 501399145f9SAnshuman Khandual 502399145f9SAnshuman Khandual static void __init pgd_populate_tests(struct mm_struct *mm, pgd_t *pgdp, 503399145f9SAnshuman Khandual p4d_t *p4dp) 504399145f9SAnshuman Khandual { 505399145f9SAnshuman Khandual pgd_t pgd; 506399145f9SAnshuman Khandual 507399145f9SAnshuman Khandual if (mm_p4d_folded(mm)) 508399145f9SAnshuman Khandual return; 509399145f9SAnshuman Khandual 5106315df41SAnshuman Khandual pr_debug("Validating PGD populate\n"); 511399145f9SAnshuman Khandual /* 512399145f9SAnshuman Khandual * This entry points to next level page table page. 513399145f9SAnshuman Khandual * Hence this must not qualify as pgd_bad(). 514399145f9SAnshuman Khandual */ 515399145f9SAnshuman Khandual p4d_clear(p4dp); 516399145f9SAnshuman Khandual pgd_clear(pgdp); 517399145f9SAnshuman Khandual pgd_populate(mm, pgdp, p4dp); 518399145f9SAnshuman Khandual pgd = READ_ONCE(*pgdp); 519399145f9SAnshuman Khandual WARN_ON(pgd_bad(pgd)); 520399145f9SAnshuman Khandual } 521399145f9SAnshuman Khandual #else /* !__PAGETABLE_P4D_FOLDED */ 522399145f9SAnshuman Khandual static void __init p4d_clear_tests(struct mm_struct *mm, p4d_t *p4dp) { } 523399145f9SAnshuman Khandual static void __init pgd_clear_tests(struct mm_struct *mm, pgd_t *pgdp) { } 524399145f9SAnshuman Khandual static void __init p4d_populate_tests(struct mm_struct *mm, p4d_t *p4dp, 525399145f9SAnshuman Khandual pud_t *pudp) 526399145f9SAnshuman Khandual { 527399145f9SAnshuman Khandual } 528399145f9SAnshuman Khandual static void __init pgd_populate_tests(struct mm_struct *mm, pgd_t *pgdp, 529399145f9SAnshuman Khandual p4d_t *p4dp) 530399145f9SAnshuman Khandual { 531399145f9SAnshuman Khandual } 532399145f9SAnshuman Khandual #endif /* PAGETABLE_P4D_FOLDED */ 533399145f9SAnshuman Khandual 534399145f9SAnshuman Khandual static void __init pte_clear_tests(struct mm_struct *mm, pte_t *ptep, 535399145f9SAnshuman Khandual unsigned long vaddr) 536399145f9SAnshuman Khandual { 5379449c9cbSChristophe Leroy pte_t pte = ptep_get(ptep); 538399145f9SAnshuman Khandual 5396315df41SAnshuman Khandual pr_debug("Validating PTE clear\n"); 540399145f9SAnshuman Khandual pte = __pte(pte_val(pte) | RANDOM_ORVALUE); 541399145f9SAnshuman Khandual set_pte_at(mm, vaddr, ptep, pte); 542399145f9SAnshuman Khandual barrier(); 543399145f9SAnshuman Khandual pte_clear(mm, vaddr, ptep); 5449449c9cbSChristophe Leroy pte = ptep_get(ptep); 545399145f9SAnshuman Khandual WARN_ON(!pte_none(pte)); 546399145f9SAnshuman Khandual } 547399145f9SAnshuman Khandual 548399145f9SAnshuman Khandual static void __init pmd_clear_tests(struct mm_struct *mm, pmd_t *pmdp) 549399145f9SAnshuman Khandual { 550399145f9SAnshuman Khandual pmd_t pmd = READ_ONCE(*pmdp); 551399145f9SAnshuman Khandual 5526315df41SAnshuman Khandual pr_debug("Validating PMD clear\n"); 553399145f9SAnshuman Khandual pmd = __pmd(pmd_val(pmd) | RANDOM_ORVALUE); 554399145f9SAnshuman Khandual WRITE_ONCE(*pmdp, pmd); 555399145f9SAnshuman Khandual pmd_clear(pmdp); 556399145f9SAnshuman Khandual pmd = READ_ONCE(*pmdp); 557399145f9SAnshuman Khandual WARN_ON(!pmd_none(pmd)); 558399145f9SAnshuman Khandual } 559399145f9SAnshuman Khandual 560399145f9SAnshuman Khandual static void __init pmd_populate_tests(struct mm_struct *mm, pmd_t *pmdp, 561399145f9SAnshuman Khandual pgtable_t pgtable) 562399145f9SAnshuman Khandual { 563399145f9SAnshuman Khandual pmd_t pmd; 564399145f9SAnshuman Khandual 5656315df41SAnshuman Khandual pr_debug("Validating PMD populate\n"); 566399145f9SAnshuman Khandual /* 567399145f9SAnshuman Khandual * This entry points to next level page table page. 568399145f9SAnshuman Khandual * Hence this must not qualify as pmd_bad(). 569399145f9SAnshuman Khandual */ 570399145f9SAnshuman Khandual pmd_clear(pmdp); 571399145f9SAnshuman Khandual pmd_populate(mm, pmdp, pgtable); 572399145f9SAnshuman Khandual pmd = READ_ONCE(*pmdp); 573399145f9SAnshuman Khandual WARN_ON(pmd_bad(pmd)); 574399145f9SAnshuman Khandual } 575399145f9SAnshuman Khandual 57605289402SAnshuman Khandual static void __init pte_special_tests(unsigned long pfn, pgprot_t prot) 57705289402SAnshuman Khandual { 57805289402SAnshuman Khandual pte_t pte = pfn_pte(pfn, prot); 57905289402SAnshuman Khandual 58005289402SAnshuman Khandual if (!IS_ENABLED(CONFIG_ARCH_HAS_PTE_SPECIAL)) 58105289402SAnshuman Khandual return; 58205289402SAnshuman Khandual 5836315df41SAnshuman Khandual pr_debug("Validating PTE special\n"); 58405289402SAnshuman Khandual WARN_ON(!pte_special(pte_mkspecial(pte))); 58505289402SAnshuman Khandual } 58605289402SAnshuman Khandual 58705289402SAnshuman Khandual static void __init pte_protnone_tests(unsigned long pfn, pgprot_t prot) 58805289402SAnshuman Khandual { 58905289402SAnshuman Khandual pte_t pte = pfn_pte(pfn, prot); 59005289402SAnshuman Khandual 59105289402SAnshuman Khandual if (!IS_ENABLED(CONFIG_NUMA_BALANCING)) 59205289402SAnshuman Khandual return; 59305289402SAnshuman Khandual 5946315df41SAnshuman Khandual pr_debug("Validating PTE protnone\n"); 59505289402SAnshuman Khandual WARN_ON(!pte_protnone(pte)); 59605289402SAnshuman Khandual WARN_ON(!pte_present(pte)); 59705289402SAnshuman Khandual } 59805289402SAnshuman Khandual 59905289402SAnshuman Khandual #ifdef CONFIG_TRANSPARENT_HUGEPAGE 60005289402SAnshuman Khandual static void __init pmd_protnone_tests(unsigned long pfn, pgprot_t prot) 60105289402SAnshuman Khandual { 60205289402SAnshuman Khandual pmd_t pmd = pmd_mkhuge(pfn_pmd(pfn, prot)); 60305289402SAnshuman Khandual 60405289402SAnshuman Khandual if (!IS_ENABLED(CONFIG_NUMA_BALANCING)) 60505289402SAnshuman Khandual return; 60605289402SAnshuman Khandual 6076315df41SAnshuman Khandual pr_debug("Validating PMD protnone\n"); 60805289402SAnshuman Khandual WARN_ON(!pmd_protnone(pmd)); 60905289402SAnshuman Khandual WARN_ON(!pmd_present(pmd)); 61005289402SAnshuman Khandual } 61105289402SAnshuman Khandual #else /* !CONFIG_TRANSPARENT_HUGEPAGE */ 61205289402SAnshuman Khandual static void __init pmd_protnone_tests(unsigned long pfn, pgprot_t prot) { } 61305289402SAnshuman Khandual #endif /* CONFIG_TRANSPARENT_HUGEPAGE */ 61405289402SAnshuman Khandual 61505289402SAnshuman Khandual #ifdef CONFIG_ARCH_HAS_PTE_DEVMAP 61605289402SAnshuman Khandual static void __init pte_devmap_tests(unsigned long pfn, pgprot_t prot) 61705289402SAnshuman Khandual { 61805289402SAnshuman Khandual pte_t pte = pfn_pte(pfn, prot); 61905289402SAnshuman Khandual 6206315df41SAnshuman Khandual pr_debug("Validating PTE devmap\n"); 62105289402SAnshuman Khandual WARN_ON(!pte_devmap(pte_mkdevmap(pte))); 62205289402SAnshuman Khandual } 62305289402SAnshuman Khandual 62405289402SAnshuman Khandual #ifdef CONFIG_TRANSPARENT_HUGEPAGE 62505289402SAnshuman Khandual static void __init pmd_devmap_tests(unsigned long pfn, pgprot_t prot) 62605289402SAnshuman Khandual { 62705289402SAnshuman Khandual pmd_t pmd = pfn_pmd(pfn, prot); 62805289402SAnshuman Khandual 6296315df41SAnshuman Khandual pr_debug("Validating PMD devmap\n"); 63005289402SAnshuman Khandual WARN_ON(!pmd_devmap(pmd_mkdevmap(pmd))); 63105289402SAnshuman Khandual } 63205289402SAnshuman Khandual 63305289402SAnshuman Khandual #ifdef CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD 63405289402SAnshuman Khandual static void __init pud_devmap_tests(unsigned long pfn, pgprot_t prot) 63505289402SAnshuman Khandual { 63605289402SAnshuman Khandual pud_t pud = pfn_pud(pfn, prot); 63705289402SAnshuman Khandual 6386315df41SAnshuman Khandual pr_debug("Validating PUD devmap\n"); 63905289402SAnshuman Khandual WARN_ON(!pud_devmap(pud_mkdevmap(pud))); 64005289402SAnshuman Khandual } 64105289402SAnshuman Khandual #else /* !CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD */ 64205289402SAnshuman Khandual static void __init pud_devmap_tests(unsigned long pfn, pgprot_t prot) { } 64305289402SAnshuman Khandual #endif /* CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD */ 64405289402SAnshuman Khandual #else /* CONFIG_TRANSPARENT_HUGEPAGE */ 64505289402SAnshuman Khandual static void __init pmd_devmap_tests(unsigned long pfn, pgprot_t prot) { } 64605289402SAnshuman Khandual static void __init pud_devmap_tests(unsigned long pfn, pgprot_t prot) { } 64705289402SAnshuman Khandual #endif /* CONFIG_TRANSPARENT_HUGEPAGE */ 64805289402SAnshuman Khandual #else 64905289402SAnshuman Khandual static void __init pte_devmap_tests(unsigned long pfn, pgprot_t prot) { } 65005289402SAnshuman Khandual static void __init pmd_devmap_tests(unsigned long pfn, pgprot_t prot) { } 65105289402SAnshuman Khandual static void __init pud_devmap_tests(unsigned long pfn, pgprot_t prot) { } 65205289402SAnshuman Khandual #endif /* CONFIG_ARCH_HAS_PTE_DEVMAP */ 65305289402SAnshuman Khandual 65405289402SAnshuman Khandual static void __init pte_soft_dirty_tests(unsigned long pfn, pgprot_t prot) 65505289402SAnshuman Khandual { 65605289402SAnshuman Khandual pte_t pte = pfn_pte(pfn, prot); 65705289402SAnshuman Khandual 65805289402SAnshuman Khandual if (!IS_ENABLED(CONFIG_MEM_SOFT_DIRTY)) 65905289402SAnshuman Khandual return; 66005289402SAnshuman Khandual 6616315df41SAnshuman Khandual pr_debug("Validating PTE soft dirty\n"); 66205289402SAnshuman Khandual WARN_ON(!pte_soft_dirty(pte_mksoft_dirty(pte))); 66305289402SAnshuman Khandual WARN_ON(pte_soft_dirty(pte_clear_soft_dirty(pte))); 66405289402SAnshuman Khandual } 66505289402SAnshuman Khandual 66605289402SAnshuman Khandual static void __init pte_swap_soft_dirty_tests(unsigned long pfn, pgprot_t prot) 66705289402SAnshuman Khandual { 66805289402SAnshuman Khandual pte_t pte = pfn_pte(pfn, prot); 66905289402SAnshuman Khandual 67005289402SAnshuman Khandual if (!IS_ENABLED(CONFIG_MEM_SOFT_DIRTY)) 67105289402SAnshuman Khandual return; 67205289402SAnshuman Khandual 6736315df41SAnshuman Khandual pr_debug("Validating PTE swap soft dirty\n"); 67405289402SAnshuman Khandual WARN_ON(!pte_swp_soft_dirty(pte_swp_mksoft_dirty(pte))); 67505289402SAnshuman Khandual WARN_ON(pte_swp_soft_dirty(pte_swp_clear_soft_dirty(pte))); 67605289402SAnshuman Khandual } 67705289402SAnshuman Khandual 67805289402SAnshuman Khandual #ifdef CONFIG_TRANSPARENT_HUGEPAGE 67905289402SAnshuman Khandual static void __init pmd_soft_dirty_tests(unsigned long pfn, pgprot_t prot) 68005289402SAnshuman Khandual { 68105289402SAnshuman Khandual pmd_t pmd = pfn_pmd(pfn, prot); 68205289402SAnshuman Khandual 68305289402SAnshuman Khandual if (!IS_ENABLED(CONFIG_MEM_SOFT_DIRTY)) 68405289402SAnshuman Khandual return; 68505289402SAnshuman Khandual 6866315df41SAnshuman Khandual pr_debug("Validating PMD soft dirty\n"); 68705289402SAnshuman Khandual WARN_ON(!pmd_soft_dirty(pmd_mksoft_dirty(pmd))); 68805289402SAnshuman Khandual WARN_ON(pmd_soft_dirty(pmd_clear_soft_dirty(pmd))); 68905289402SAnshuman Khandual } 69005289402SAnshuman Khandual 69105289402SAnshuman Khandual static void __init pmd_swap_soft_dirty_tests(unsigned long pfn, pgprot_t prot) 69205289402SAnshuman Khandual { 69305289402SAnshuman Khandual pmd_t pmd = pfn_pmd(pfn, prot); 69405289402SAnshuman Khandual 69505289402SAnshuman Khandual if (!IS_ENABLED(CONFIG_MEM_SOFT_DIRTY) || 69605289402SAnshuman Khandual !IS_ENABLED(CONFIG_ARCH_ENABLE_THP_MIGRATION)) 69705289402SAnshuman Khandual return; 69805289402SAnshuman Khandual 6996315df41SAnshuman Khandual pr_debug("Validating PMD swap soft dirty\n"); 70005289402SAnshuman Khandual WARN_ON(!pmd_swp_soft_dirty(pmd_swp_mksoft_dirty(pmd))); 70105289402SAnshuman Khandual WARN_ON(pmd_swp_soft_dirty(pmd_swp_clear_soft_dirty(pmd))); 70205289402SAnshuman Khandual } 70305289402SAnshuman Khandual #else /* !CONFIG_ARCH_HAS_PTE_DEVMAP */ 70405289402SAnshuman Khandual static void __init pmd_soft_dirty_tests(unsigned long pfn, pgprot_t prot) { } 70505289402SAnshuman Khandual static void __init pmd_swap_soft_dirty_tests(unsigned long pfn, pgprot_t prot) 70605289402SAnshuman Khandual { 70705289402SAnshuman Khandual } 70805289402SAnshuman Khandual #endif /* CONFIG_ARCH_HAS_PTE_DEVMAP */ 70905289402SAnshuman Khandual 71005289402SAnshuman Khandual static void __init pte_swap_tests(unsigned long pfn, pgprot_t prot) 71105289402SAnshuman Khandual { 71205289402SAnshuman Khandual swp_entry_t swp; 71305289402SAnshuman Khandual pte_t pte; 71405289402SAnshuman Khandual 7156315df41SAnshuman Khandual pr_debug("Validating PTE swap\n"); 71605289402SAnshuman Khandual pte = pfn_pte(pfn, prot); 71705289402SAnshuman Khandual swp = __pte_to_swp_entry(pte); 71805289402SAnshuman Khandual pte = __swp_entry_to_pte(swp); 71905289402SAnshuman Khandual WARN_ON(pfn != pte_pfn(pte)); 72005289402SAnshuman Khandual } 72105289402SAnshuman Khandual 72205289402SAnshuman Khandual #ifdef CONFIG_ARCH_ENABLE_THP_MIGRATION 72305289402SAnshuman Khandual static void __init pmd_swap_tests(unsigned long pfn, pgprot_t prot) 72405289402SAnshuman Khandual { 72505289402SAnshuman Khandual swp_entry_t swp; 72605289402SAnshuman Khandual pmd_t pmd; 72705289402SAnshuman Khandual 7286315df41SAnshuman Khandual pr_debug("Validating PMD swap\n"); 72905289402SAnshuman Khandual pmd = pfn_pmd(pfn, prot); 73005289402SAnshuman Khandual swp = __pmd_to_swp_entry(pmd); 73105289402SAnshuman Khandual pmd = __swp_entry_to_pmd(swp); 73205289402SAnshuman Khandual WARN_ON(pfn != pmd_pfn(pmd)); 73305289402SAnshuman Khandual } 73405289402SAnshuman Khandual #else /* !CONFIG_ARCH_ENABLE_THP_MIGRATION */ 73505289402SAnshuman Khandual static void __init pmd_swap_tests(unsigned long pfn, pgprot_t prot) { } 73605289402SAnshuman Khandual #endif /* CONFIG_ARCH_ENABLE_THP_MIGRATION */ 73705289402SAnshuman Khandual 73805289402SAnshuman Khandual static void __init swap_migration_tests(void) 73905289402SAnshuman Khandual { 74005289402SAnshuman Khandual struct page *page; 74105289402SAnshuman Khandual swp_entry_t swp; 74205289402SAnshuman Khandual 74305289402SAnshuman Khandual if (!IS_ENABLED(CONFIG_MIGRATION)) 74405289402SAnshuman Khandual return; 7456315df41SAnshuman Khandual 7466315df41SAnshuman Khandual pr_debug("Validating swap migration\n"); 74705289402SAnshuman Khandual /* 74805289402SAnshuman Khandual * swap_migration_tests() requires a dedicated page as it needs to 74905289402SAnshuman Khandual * be locked before creating a migration entry from it. Locking the 75005289402SAnshuman Khandual * page that actually maps kernel text ('start_kernel') can be real 75105289402SAnshuman Khandual * problematic. Lets allocate a dedicated page explicitly for this 75205289402SAnshuman Khandual * purpose that will be freed subsequently. 75305289402SAnshuman Khandual */ 75405289402SAnshuman Khandual page = alloc_page(GFP_KERNEL); 75505289402SAnshuman Khandual if (!page) { 75605289402SAnshuman Khandual pr_err("page allocation failed\n"); 75705289402SAnshuman Khandual return; 75805289402SAnshuman Khandual } 75905289402SAnshuman Khandual 76005289402SAnshuman Khandual /* 76105289402SAnshuman Khandual * make_migration_entry() expects given page to be 76205289402SAnshuman Khandual * locked, otherwise it stumbles upon a BUG_ON(). 76305289402SAnshuman Khandual */ 76405289402SAnshuman Khandual __SetPageLocked(page); 76505289402SAnshuman Khandual swp = make_migration_entry(page, 1); 76605289402SAnshuman Khandual WARN_ON(!is_migration_entry(swp)); 76705289402SAnshuman Khandual WARN_ON(!is_write_migration_entry(swp)); 76805289402SAnshuman Khandual 76905289402SAnshuman Khandual make_migration_entry_read(&swp); 77005289402SAnshuman Khandual WARN_ON(!is_migration_entry(swp)); 77105289402SAnshuman Khandual WARN_ON(is_write_migration_entry(swp)); 77205289402SAnshuman Khandual 77305289402SAnshuman Khandual swp = make_migration_entry(page, 0); 77405289402SAnshuman Khandual WARN_ON(!is_migration_entry(swp)); 77505289402SAnshuman Khandual WARN_ON(is_write_migration_entry(swp)); 77605289402SAnshuman Khandual __ClearPageLocked(page); 77705289402SAnshuman Khandual __free_page(page); 77805289402SAnshuman Khandual } 77905289402SAnshuman Khandual 78005289402SAnshuman Khandual #ifdef CONFIG_HUGETLB_PAGE 78105289402SAnshuman Khandual static void __init hugetlb_basic_tests(unsigned long pfn, pgprot_t prot) 78205289402SAnshuman Khandual { 78305289402SAnshuman Khandual struct page *page; 78405289402SAnshuman Khandual pte_t pte; 78505289402SAnshuman Khandual 7866315df41SAnshuman Khandual pr_debug("Validating HugeTLB basic\n"); 78705289402SAnshuman Khandual /* 78805289402SAnshuman Khandual * Accessing the page associated with the pfn is safe here, 78905289402SAnshuman Khandual * as it was previously derived from a real kernel symbol. 79005289402SAnshuman Khandual */ 79105289402SAnshuman Khandual page = pfn_to_page(pfn); 79205289402SAnshuman Khandual pte = mk_huge_pte(page, prot); 79305289402SAnshuman Khandual 79405289402SAnshuman Khandual WARN_ON(!huge_pte_dirty(huge_pte_mkdirty(pte))); 79505289402SAnshuman Khandual WARN_ON(!huge_pte_write(huge_pte_mkwrite(huge_pte_wrprotect(pte)))); 79605289402SAnshuman Khandual WARN_ON(huge_pte_write(huge_pte_wrprotect(huge_pte_mkwrite(pte)))); 79705289402SAnshuman Khandual 79805289402SAnshuman Khandual #ifdef CONFIG_ARCH_WANT_GENERAL_HUGETLB 79905289402SAnshuman Khandual pte = pfn_pte(pfn, prot); 80005289402SAnshuman Khandual 80105289402SAnshuman Khandual WARN_ON(!pte_huge(pte_mkhuge(pte))); 80205289402SAnshuman Khandual #endif /* CONFIG_ARCH_WANT_GENERAL_HUGETLB */ 80305289402SAnshuman Khandual } 804a5c3b9ffSAnshuman Khandual 805a5c3b9ffSAnshuman Khandual static void __init hugetlb_advanced_tests(struct mm_struct *mm, 806a5c3b9ffSAnshuman Khandual struct vm_area_struct *vma, 807a5c3b9ffSAnshuman Khandual pte_t *ptep, unsigned long pfn, 808a5c3b9ffSAnshuman Khandual unsigned long vaddr, pgprot_t prot) 809a5c3b9ffSAnshuman Khandual { 810a5c3b9ffSAnshuman Khandual struct page *page = pfn_to_page(pfn); 811a5c3b9ffSAnshuman Khandual pte_t pte = ptep_get(ptep); 812a5c3b9ffSAnshuman Khandual unsigned long paddr = __pfn_to_phys(pfn) & PMD_MASK; 813a5c3b9ffSAnshuman Khandual 8146315df41SAnshuman Khandual pr_debug("Validating HugeTLB advanced\n"); 815a5c3b9ffSAnshuman Khandual pte = pte_mkhuge(mk_pte(pfn_to_page(PHYS_PFN(paddr)), prot)); 816a5c3b9ffSAnshuman Khandual set_huge_pte_at(mm, vaddr, ptep, pte); 817a5c3b9ffSAnshuman Khandual barrier(); 818a5c3b9ffSAnshuman Khandual WARN_ON(!pte_same(pte, huge_ptep_get(ptep))); 819a5c3b9ffSAnshuman Khandual huge_pte_clear(mm, vaddr, ptep, PMD_SIZE); 820a5c3b9ffSAnshuman Khandual pte = huge_ptep_get(ptep); 821a5c3b9ffSAnshuman Khandual WARN_ON(!huge_pte_none(pte)); 822a5c3b9ffSAnshuman Khandual 823a5c3b9ffSAnshuman Khandual pte = mk_huge_pte(page, prot); 824a5c3b9ffSAnshuman Khandual set_huge_pte_at(mm, vaddr, ptep, pte); 825a5c3b9ffSAnshuman Khandual barrier(); 826a5c3b9ffSAnshuman Khandual huge_ptep_set_wrprotect(mm, vaddr, ptep); 827a5c3b9ffSAnshuman Khandual pte = huge_ptep_get(ptep); 828a5c3b9ffSAnshuman Khandual WARN_ON(huge_pte_write(pte)); 829a5c3b9ffSAnshuman Khandual 830a5c3b9ffSAnshuman Khandual pte = mk_huge_pte(page, prot); 831a5c3b9ffSAnshuman Khandual set_huge_pte_at(mm, vaddr, ptep, pte); 832a5c3b9ffSAnshuman Khandual barrier(); 833a5c3b9ffSAnshuman Khandual huge_ptep_get_and_clear(mm, vaddr, ptep); 834a5c3b9ffSAnshuman Khandual pte = huge_ptep_get(ptep); 835a5c3b9ffSAnshuman Khandual WARN_ON(!huge_pte_none(pte)); 836a5c3b9ffSAnshuman Khandual 837a5c3b9ffSAnshuman Khandual pte = mk_huge_pte(page, prot); 838a5c3b9ffSAnshuman Khandual pte = huge_pte_wrprotect(pte); 839a5c3b9ffSAnshuman Khandual set_huge_pte_at(mm, vaddr, ptep, pte); 840a5c3b9ffSAnshuman Khandual barrier(); 841a5c3b9ffSAnshuman Khandual pte = huge_pte_mkwrite(pte); 842a5c3b9ffSAnshuman Khandual pte = huge_pte_mkdirty(pte); 843a5c3b9ffSAnshuman Khandual huge_ptep_set_access_flags(vma, vaddr, ptep, pte, 1); 844a5c3b9ffSAnshuman Khandual pte = huge_ptep_get(ptep); 845a5c3b9ffSAnshuman Khandual WARN_ON(!(huge_pte_write(pte) && huge_pte_dirty(pte))); 846a5c3b9ffSAnshuman Khandual } 84705289402SAnshuman Khandual #else /* !CONFIG_HUGETLB_PAGE */ 84805289402SAnshuman Khandual static void __init hugetlb_basic_tests(unsigned long pfn, pgprot_t prot) { } 849a5c3b9ffSAnshuman Khandual static void __init hugetlb_advanced_tests(struct mm_struct *mm, 850a5c3b9ffSAnshuman Khandual struct vm_area_struct *vma, 851a5c3b9ffSAnshuman Khandual pte_t *ptep, unsigned long pfn, 852a5c3b9ffSAnshuman Khandual unsigned long vaddr, pgprot_t prot) 853a5c3b9ffSAnshuman Khandual { 854a5c3b9ffSAnshuman Khandual } 85505289402SAnshuman Khandual #endif /* CONFIG_HUGETLB_PAGE */ 85605289402SAnshuman Khandual 85705289402SAnshuman Khandual #ifdef CONFIG_TRANSPARENT_HUGEPAGE 85805289402SAnshuman Khandual static void __init pmd_thp_tests(unsigned long pfn, pgprot_t prot) 85905289402SAnshuman Khandual { 86005289402SAnshuman Khandual pmd_t pmd; 86105289402SAnshuman Khandual 86205289402SAnshuman Khandual if (!has_transparent_hugepage()) 86305289402SAnshuman Khandual return; 86405289402SAnshuman Khandual 8656315df41SAnshuman Khandual pr_debug("Validating PMD based THP\n"); 86605289402SAnshuman Khandual /* 86705289402SAnshuman Khandual * pmd_trans_huge() and pmd_present() must return positive after 86805289402SAnshuman Khandual * MMU invalidation with pmd_mkinvalid(). This behavior is an 86905289402SAnshuman Khandual * optimization for transparent huge page. pmd_trans_huge() must 87005289402SAnshuman Khandual * be true if pmd_page() returns a valid THP to avoid taking the 87105289402SAnshuman Khandual * pmd_lock when others walk over non transhuge pmds (i.e. there 87205289402SAnshuman Khandual * are no THP allocated). Especially when splitting a THP and 87305289402SAnshuman Khandual * removing the present bit from the pmd, pmd_trans_huge() still 87405289402SAnshuman Khandual * needs to return true. pmd_present() should be true whenever 87505289402SAnshuman Khandual * pmd_trans_huge() returns true. 87605289402SAnshuman Khandual */ 87705289402SAnshuman Khandual pmd = pfn_pmd(pfn, prot); 87805289402SAnshuman Khandual WARN_ON(!pmd_trans_huge(pmd_mkhuge(pmd))); 87905289402SAnshuman Khandual 88005289402SAnshuman Khandual #ifndef __HAVE_ARCH_PMDP_INVALIDATE 88105289402SAnshuman Khandual WARN_ON(!pmd_trans_huge(pmd_mkinvalid(pmd_mkhuge(pmd)))); 88205289402SAnshuman Khandual WARN_ON(!pmd_present(pmd_mkinvalid(pmd_mkhuge(pmd)))); 88305289402SAnshuman Khandual #endif /* __HAVE_ARCH_PMDP_INVALIDATE */ 88405289402SAnshuman Khandual } 88505289402SAnshuman Khandual 88605289402SAnshuman Khandual #ifdef CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD 88705289402SAnshuman Khandual static void __init pud_thp_tests(unsigned long pfn, pgprot_t prot) 88805289402SAnshuman Khandual { 88905289402SAnshuman Khandual pud_t pud; 89005289402SAnshuman Khandual 89105289402SAnshuman Khandual if (!has_transparent_hugepage()) 89205289402SAnshuman Khandual return; 89305289402SAnshuman Khandual 8946315df41SAnshuman Khandual pr_debug("Validating PUD based THP\n"); 89505289402SAnshuman Khandual pud = pfn_pud(pfn, prot); 89605289402SAnshuman Khandual WARN_ON(!pud_trans_huge(pud_mkhuge(pud))); 89705289402SAnshuman Khandual 89805289402SAnshuman Khandual /* 89905289402SAnshuman Khandual * pud_mkinvalid() has been dropped for now. Enable back 90005289402SAnshuman Khandual * these tests when it comes back with a modified pud_present(). 90105289402SAnshuman Khandual * 90205289402SAnshuman Khandual * WARN_ON(!pud_trans_huge(pud_mkinvalid(pud_mkhuge(pud)))); 90305289402SAnshuman Khandual * WARN_ON(!pud_present(pud_mkinvalid(pud_mkhuge(pud)))); 90405289402SAnshuman Khandual */ 90505289402SAnshuman Khandual } 90605289402SAnshuman Khandual #else /* !CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD */ 90705289402SAnshuman Khandual static void __init pud_thp_tests(unsigned long pfn, pgprot_t prot) { } 90805289402SAnshuman Khandual #endif /* CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD */ 90905289402SAnshuman Khandual #else /* !CONFIG_TRANSPARENT_HUGEPAGE */ 91005289402SAnshuman Khandual static void __init pmd_thp_tests(unsigned long pfn, pgprot_t prot) { } 91105289402SAnshuman Khandual static void __init pud_thp_tests(unsigned long pfn, pgprot_t prot) { } 91205289402SAnshuman Khandual #endif /* CONFIG_TRANSPARENT_HUGEPAGE */ 91305289402SAnshuman Khandual 914399145f9SAnshuman Khandual static unsigned long __init get_random_vaddr(void) 915399145f9SAnshuman Khandual { 916399145f9SAnshuman Khandual unsigned long random_vaddr, random_pages, total_user_pages; 917399145f9SAnshuman Khandual 918399145f9SAnshuman Khandual total_user_pages = (TASK_SIZE - FIRST_USER_ADDRESS) / PAGE_SIZE; 919399145f9SAnshuman Khandual 920399145f9SAnshuman Khandual random_pages = get_random_long() % total_user_pages; 921399145f9SAnshuman Khandual random_vaddr = FIRST_USER_ADDRESS + random_pages * PAGE_SIZE; 922399145f9SAnshuman Khandual 923399145f9SAnshuman Khandual return random_vaddr; 924399145f9SAnshuman Khandual } 925399145f9SAnshuman Khandual 926399145f9SAnshuman Khandual static int __init debug_vm_pgtable(void) 927399145f9SAnshuman Khandual { 928a5c3b9ffSAnshuman Khandual struct vm_area_struct *vma; 929399145f9SAnshuman Khandual struct mm_struct *mm; 930399145f9SAnshuman Khandual pgd_t *pgdp; 931399145f9SAnshuman Khandual p4d_t *p4dp, *saved_p4dp; 932399145f9SAnshuman Khandual pud_t *pudp, *saved_pudp; 933399145f9SAnshuman Khandual pmd_t *pmdp, *saved_pmdp, pmd; 934399145f9SAnshuman Khandual pte_t *ptep; 935399145f9SAnshuman Khandual pgtable_t saved_ptep; 93605289402SAnshuman Khandual pgprot_t prot, protnone; 937399145f9SAnshuman Khandual phys_addr_t paddr; 938399145f9SAnshuman Khandual unsigned long vaddr, pte_aligned, pmd_aligned; 939399145f9SAnshuman Khandual unsigned long pud_aligned, p4d_aligned, pgd_aligned; 940fea1120cSKees Cook spinlock_t *ptl = NULL; 941399145f9SAnshuman Khandual 942399145f9SAnshuman Khandual pr_info("Validating architecture page table helpers\n"); 943399145f9SAnshuman Khandual prot = vm_get_page_prot(VMFLAGS); 944399145f9SAnshuman Khandual vaddr = get_random_vaddr(); 945399145f9SAnshuman Khandual mm = mm_alloc(); 946399145f9SAnshuman Khandual if (!mm) { 947399145f9SAnshuman Khandual pr_err("mm_struct allocation failed\n"); 948399145f9SAnshuman Khandual return 1; 949399145f9SAnshuman Khandual } 950399145f9SAnshuman Khandual 951399145f9SAnshuman Khandual /* 95205289402SAnshuman Khandual * __P000 (or even __S000) will help create page table entries with 95305289402SAnshuman Khandual * PROT_NONE permission as required for pxx_protnone_tests(). 95405289402SAnshuman Khandual */ 95505289402SAnshuman Khandual protnone = __P000; 95605289402SAnshuman Khandual 957a5c3b9ffSAnshuman Khandual vma = vm_area_alloc(mm); 958a5c3b9ffSAnshuman Khandual if (!vma) { 959a5c3b9ffSAnshuman Khandual pr_err("vma allocation failed\n"); 960a5c3b9ffSAnshuman Khandual return 1; 961a5c3b9ffSAnshuman Khandual } 962a5c3b9ffSAnshuman Khandual 96305289402SAnshuman Khandual /* 964399145f9SAnshuman Khandual * PFN for mapping at PTE level is determined from a standard kernel 965399145f9SAnshuman Khandual * text symbol. But pfns for higher page table levels are derived by 966399145f9SAnshuman Khandual * masking lower bits of this real pfn. These derived pfns might not 967399145f9SAnshuman Khandual * exist on the platform but that does not really matter as pfn_pxx() 968399145f9SAnshuman Khandual * helpers will still create appropriate entries for the test. This 969399145f9SAnshuman Khandual * helps avoid large memory block allocations to be used for mapping 970399145f9SAnshuman Khandual * at higher page table levels. 971399145f9SAnshuman Khandual */ 972399145f9SAnshuman Khandual paddr = __pa_symbol(&start_kernel); 973399145f9SAnshuman Khandual 974399145f9SAnshuman Khandual pte_aligned = (paddr & PAGE_MASK) >> PAGE_SHIFT; 975399145f9SAnshuman Khandual pmd_aligned = (paddr & PMD_MASK) >> PAGE_SHIFT; 976399145f9SAnshuman Khandual pud_aligned = (paddr & PUD_MASK) >> PAGE_SHIFT; 977399145f9SAnshuman Khandual p4d_aligned = (paddr & P4D_MASK) >> PAGE_SHIFT; 978399145f9SAnshuman Khandual pgd_aligned = (paddr & PGDIR_MASK) >> PAGE_SHIFT; 979399145f9SAnshuman Khandual WARN_ON(!pfn_valid(pte_aligned)); 980399145f9SAnshuman Khandual 981399145f9SAnshuman Khandual pgdp = pgd_offset(mm, vaddr); 982399145f9SAnshuman Khandual p4dp = p4d_alloc(mm, pgdp, vaddr); 983399145f9SAnshuman Khandual pudp = pud_alloc(mm, p4dp, vaddr); 984399145f9SAnshuman Khandual pmdp = pmd_alloc(mm, pudp, vaddr); 985399145f9SAnshuman Khandual ptep = pte_alloc_map_lock(mm, pmdp, vaddr, &ptl); 986399145f9SAnshuman Khandual 987399145f9SAnshuman Khandual /* 988399145f9SAnshuman Khandual * Save all the page table page addresses as the page table 989399145f9SAnshuman Khandual * entries will be used for testing with random or garbage 990399145f9SAnshuman Khandual * values. These saved addresses will be used for freeing 991399145f9SAnshuman Khandual * page table pages. 992399145f9SAnshuman Khandual */ 993399145f9SAnshuman Khandual pmd = READ_ONCE(*pmdp); 994399145f9SAnshuman Khandual saved_p4dp = p4d_offset(pgdp, 0UL); 995399145f9SAnshuman Khandual saved_pudp = pud_offset(p4dp, 0UL); 996399145f9SAnshuman Khandual saved_pmdp = pmd_offset(pudp, 0UL); 997399145f9SAnshuman Khandual saved_ptep = pmd_pgtable(pmd); 998399145f9SAnshuman Khandual 999399145f9SAnshuman Khandual pte_basic_tests(pte_aligned, prot); 1000399145f9SAnshuman Khandual pmd_basic_tests(pmd_aligned, prot); 1001399145f9SAnshuman Khandual pud_basic_tests(pud_aligned, prot); 1002399145f9SAnshuman Khandual p4d_basic_tests(p4d_aligned, prot); 1003399145f9SAnshuman Khandual pgd_basic_tests(pgd_aligned, prot); 1004399145f9SAnshuman Khandual 1005399145f9SAnshuman Khandual pte_clear_tests(mm, ptep, vaddr); 1006399145f9SAnshuman Khandual pmd_clear_tests(mm, pmdp); 1007399145f9SAnshuman Khandual pud_clear_tests(mm, pudp); 1008399145f9SAnshuman Khandual p4d_clear_tests(mm, p4dp); 1009399145f9SAnshuman Khandual pgd_clear_tests(mm, pgdp); 1010399145f9SAnshuman Khandual 1011a5c3b9ffSAnshuman Khandual pte_advanced_tests(mm, vma, ptep, pte_aligned, vaddr, prot); 1012a5c3b9ffSAnshuman Khandual pmd_advanced_tests(mm, vma, pmdp, pmd_aligned, vaddr, prot); 1013a5c3b9ffSAnshuman Khandual pud_advanced_tests(mm, vma, pudp, pud_aligned, vaddr, prot); 1014a5c3b9ffSAnshuman Khandual hugetlb_advanced_tests(mm, vma, ptep, pte_aligned, vaddr, prot); 1015a5c3b9ffSAnshuman Khandual 1016a5c3b9ffSAnshuman Khandual pmd_leaf_tests(pmd_aligned, prot); 1017a5c3b9ffSAnshuman Khandual pud_leaf_tests(pud_aligned, prot); 1018a5c3b9ffSAnshuman Khandual 1019a5c3b9ffSAnshuman Khandual pmd_huge_tests(pmdp, pmd_aligned, prot); 1020a5c3b9ffSAnshuman Khandual pud_huge_tests(pudp, pud_aligned, prot); 1021a5c3b9ffSAnshuman Khandual 1022a5c3b9ffSAnshuman Khandual pte_savedwrite_tests(pte_aligned, prot); 1023a5c3b9ffSAnshuman Khandual pmd_savedwrite_tests(pmd_aligned, prot); 1024a5c3b9ffSAnshuman Khandual 1025399145f9SAnshuman Khandual pte_unmap_unlock(ptep, ptl); 1026399145f9SAnshuman Khandual 1027399145f9SAnshuman Khandual pmd_populate_tests(mm, pmdp, saved_ptep); 1028399145f9SAnshuman Khandual pud_populate_tests(mm, pudp, saved_pmdp); 1029399145f9SAnshuman Khandual p4d_populate_tests(mm, p4dp, saved_pudp); 1030399145f9SAnshuman Khandual pgd_populate_tests(mm, pgdp, saved_p4dp); 1031399145f9SAnshuman Khandual 103205289402SAnshuman Khandual pte_special_tests(pte_aligned, prot); 103305289402SAnshuman Khandual pte_protnone_tests(pte_aligned, protnone); 103405289402SAnshuman Khandual pmd_protnone_tests(pmd_aligned, protnone); 103505289402SAnshuman Khandual 103605289402SAnshuman Khandual pte_devmap_tests(pte_aligned, prot); 103705289402SAnshuman Khandual pmd_devmap_tests(pmd_aligned, prot); 103805289402SAnshuman Khandual pud_devmap_tests(pud_aligned, prot); 103905289402SAnshuman Khandual 104005289402SAnshuman Khandual pte_soft_dirty_tests(pte_aligned, prot); 104105289402SAnshuman Khandual pmd_soft_dirty_tests(pmd_aligned, prot); 104205289402SAnshuman Khandual pte_swap_soft_dirty_tests(pte_aligned, prot); 104305289402SAnshuman Khandual pmd_swap_soft_dirty_tests(pmd_aligned, prot); 104405289402SAnshuman Khandual 104505289402SAnshuman Khandual pte_swap_tests(pte_aligned, prot); 104605289402SAnshuman Khandual pmd_swap_tests(pmd_aligned, prot); 104705289402SAnshuman Khandual 104805289402SAnshuman Khandual swap_migration_tests(); 104905289402SAnshuman Khandual hugetlb_basic_tests(pte_aligned, prot); 105005289402SAnshuman Khandual 105105289402SAnshuman Khandual pmd_thp_tests(pmd_aligned, prot); 105205289402SAnshuman Khandual pud_thp_tests(pud_aligned, prot); 105305289402SAnshuman Khandual 1054399145f9SAnshuman Khandual p4d_free(mm, saved_p4dp); 1055399145f9SAnshuman Khandual pud_free(mm, saved_pudp); 1056399145f9SAnshuman Khandual pmd_free(mm, saved_pmdp); 1057399145f9SAnshuman Khandual pte_free(mm, saved_ptep); 1058399145f9SAnshuman Khandual 1059a5c3b9ffSAnshuman Khandual vm_area_free(vma); 1060399145f9SAnshuman Khandual mm_dec_nr_puds(mm); 1061399145f9SAnshuman Khandual mm_dec_nr_pmds(mm); 1062399145f9SAnshuman Khandual mm_dec_nr_ptes(mm); 1063399145f9SAnshuman Khandual mmdrop(mm); 1064399145f9SAnshuman Khandual return 0; 1065399145f9SAnshuman Khandual } 1066399145f9SAnshuman Khandual late_initcall(debug_vm_pgtable); 1067