162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only 262306a36Sopenharmony_ci/* 362306a36Sopenharmony_ci * This kernel test validates architecture page table helpers and 462306a36Sopenharmony_ci * accessors and helps in verifying their continued compliance with 562306a36Sopenharmony_ci * expected generic MM semantics. 662306a36Sopenharmony_ci * 762306a36Sopenharmony_ci * Copyright (C) 2019 ARM Ltd. 862306a36Sopenharmony_ci * 962306a36Sopenharmony_ci * Author: Anshuman Khandual <anshuman.khandual@arm.com> 1062306a36Sopenharmony_ci */ 1162306a36Sopenharmony_ci#define pr_fmt(fmt) "debug_vm_pgtable: [%-25s]: " fmt, __func__ 1262306a36Sopenharmony_ci 1362306a36Sopenharmony_ci#include <linux/gfp.h> 1462306a36Sopenharmony_ci#include <linux/highmem.h> 1562306a36Sopenharmony_ci#include <linux/hugetlb.h> 1662306a36Sopenharmony_ci#include <linux/kernel.h> 1762306a36Sopenharmony_ci#include <linux/kconfig.h> 1862306a36Sopenharmony_ci#include <linux/memblock.h> 1962306a36Sopenharmony_ci#include <linux/mm.h> 2062306a36Sopenharmony_ci#include <linux/mman.h> 2162306a36Sopenharmony_ci#include <linux/mm_types.h> 2262306a36Sopenharmony_ci#include <linux/module.h> 2362306a36Sopenharmony_ci#include <linux/pfn_t.h> 2462306a36Sopenharmony_ci#include <linux/printk.h> 2562306a36Sopenharmony_ci#include <linux/pgtable.h> 2662306a36Sopenharmony_ci#include <linux/random.h> 2762306a36Sopenharmony_ci#include <linux/spinlock.h> 2862306a36Sopenharmony_ci#include <linux/swap.h> 2962306a36Sopenharmony_ci#include <linux/swapops.h> 3062306a36Sopenharmony_ci#include <linux/start_kernel.h> 3162306a36Sopenharmony_ci#include <linux/sched/mm.h> 3262306a36Sopenharmony_ci#include <linux/io.h> 3362306a36Sopenharmony_ci 3462306a36Sopenharmony_ci#include <asm/cacheflush.h> 3562306a36Sopenharmony_ci#include <asm/pgalloc.h> 3662306a36Sopenharmony_ci#include <asm/tlbflush.h> 3762306a36Sopenharmony_ci 3862306a36Sopenharmony_ci/* 3962306a36Sopenharmony_ci * Please refer Documentation/mm/arch_pgtable_helpers.rst for the semantics 4062306a36Sopenharmony_ci * expectations that are being validated here. All future changes in here 4162306a36Sopenharmony_ci * or the documentation need to be in sync. 4262306a36Sopenharmony_ci * 4362306a36Sopenharmony_ci * On s390 platform, the lower 4 bits are used to identify given page table 4462306a36Sopenharmony_ci * entry type. But these bits might affect the ability to clear entries with 4562306a36Sopenharmony_ci * pxx_clear() because of how dynamic page table folding works on s390. So 4662306a36Sopenharmony_ci * while loading up the entries do not change the lower 4 bits. It does not 4762306a36Sopenharmony_ci * have affect any other platform. Also avoid the 62nd bit on ppc64 that is 4862306a36Sopenharmony_ci * used to mark a pte entry. 4962306a36Sopenharmony_ci */ 5062306a36Sopenharmony_ci#define S390_SKIP_MASK GENMASK(3, 0) 5162306a36Sopenharmony_ci#if __BITS_PER_LONG == 64 5262306a36Sopenharmony_ci#define PPC64_SKIP_MASK GENMASK(62, 62) 5362306a36Sopenharmony_ci#else 5462306a36Sopenharmony_ci#define PPC64_SKIP_MASK 0x0 5562306a36Sopenharmony_ci#endif 5662306a36Sopenharmony_ci#define ARCH_SKIP_MASK (S390_SKIP_MASK | PPC64_SKIP_MASK) 5762306a36Sopenharmony_ci#define RANDOM_ORVALUE (GENMASK(BITS_PER_LONG - 1, 0) & ~ARCH_SKIP_MASK) 5862306a36Sopenharmony_ci#define RANDOM_NZVALUE GENMASK(7, 0) 5962306a36Sopenharmony_ci 6062306a36Sopenharmony_cistruct pgtable_debug_args { 6162306a36Sopenharmony_ci struct mm_struct *mm; 6262306a36Sopenharmony_ci struct vm_area_struct *vma; 6362306a36Sopenharmony_ci 6462306a36Sopenharmony_ci pgd_t *pgdp; 6562306a36Sopenharmony_ci p4d_t *p4dp; 6662306a36Sopenharmony_ci pud_t *pudp; 6762306a36Sopenharmony_ci pmd_t *pmdp; 6862306a36Sopenharmony_ci pte_t *ptep; 6962306a36Sopenharmony_ci 7062306a36Sopenharmony_ci p4d_t *start_p4dp; 7162306a36Sopenharmony_ci pud_t *start_pudp; 7262306a36Sopenharmony_ci pmd_t *start_pmdp; 7362306a36Sopenharmony_ci pgtable_t start_ptep; 7462306a36Sopenharmony_ci 7562306a36Sopenharmony_ci unsigned long vaddr; 7662306a36Sopenharmony_ci pgprot_t page_prot; 7762306a36Sopenharmony_ci pgprot_t page_prot_none; 7862306a36Sopenharmony_ci 7962306a36Sopenharmony_ci bool is_contiguous_page; 8062306a36Sopenharmony_ci unsigned long pud_pfn; 8162306a36Sopenharmony_ci unsigned long pmd_pfn; 8262306a36Sopenharmony_ci unsigned long pte_pfn; 8362306a36Sopenharmony_ci 8462306a36Sopenharmony_ci unsigned long fixed_alignment; 8562306a36Sopenharmony_ci unsigned long fixed_pgd_pfn; 8662306a36Sopenharmony_ci unsigned long fixed_p4d_pfn; 8762306a36Sopenharmony_ci unsigned long fixed_pud_pfn; 8862306a36Sopenharmony_ci unsigned long fixed_pmd_pfn; 8962306a36Sopenharmony_ci unsigned long fixed_pte_pfn; 9062306a36Sopenharmony_ci}; 9162306a36Sopenharmony_ci 9262306a36Sopenharmony_cistatic void __init pte_basic_tests(struct pgtable_debug_args *args, int idx) 9362306a36Sopenharmony_ci{ 9462306a36Sopenharmony_ci pgprot_t prot = vm_get_page_prot(idx); 9562306a36Sopenharmony_ci pte_t pte = pfn_pte(args->fixed_pte_pfn, prot); 9662306a36Sopenharmony_ci unsigned long val = idx, *ptr = &val; 9762306a36Sopenharmony_ci 9862306a36Sopenharmony_ci pr_debug("Validating PTE basic (%pGv)\n", ptr); 9962306a36Sopenharmony_ci 10062306a36Sopenharmony_ci /* 10162306a36Sopenharmony_ci * This test needs to be executed after the given page table entry 10262306a36Sopenharmony_ci * is created with pfn_pte() to make sure that vm_get_page_prot(idx) 10362306a36Sopenharmony_ci * does not have the dirty bit enabled from the beginning. This is 10462306a36Sopenharmony_ci * important for platforms like arm64 where (!PTE_RDONLY) indicate 10562306a36Sopenharmony_ci * dirty bit being set. 10662306a36Sopenharmony_ci */ 10762306a36Sopenharmony_ci WARN_ON(pte_dirty(pte_wrprotect(pte))); 10862306a36Sopenharmony_ci 10962306a36Sopenharmony_ci WARN_ON(!pte_same(pte, pte)); 11062306a36Sopenharmony_ci WARN_ON(!pte_young(pte_mkyoung(pte_mkold(pte)))); 11162306a36Sopenharmony_ci WARN_ON(!pte_dirty(pte_mkdirty(pte_mkclean(pte)))); 11262306a36Sopenharmony_ci WARN_ON(!pte_write(pte_mkwrite(pte_wrprotect(pte), args->vma))); 11362306a36Sopenharmony_ci WARN_ON(pte_young(pte_mkold(pte_mkyoung(pte)))); 11462306a36Sopenharmony_ci WARN_ON(pte_dirty(pte_mkclean(pte_mkdirty(pte)))); 11562306a36Sopenharmony_ci WARN_ON(pte_write(pte_wrprotect(pte_mkwrite(pte, args->vma)))); 11662306a36Sopenharmony_ci WARN_ON(pte_dirty(pte_wrprotect(pte_mkclean(pte)))); 11762306a36Sopenharmony_ci WARN_ON(!pte_dirty(pte_wrprotect(pte_mkdirty(pte)))); 11862306a36Sopenharmony_ci} 11962306a36Sopenharmony_ci 12062306a36Sopenharmony_cistatic void __init pte_advanced_tests(struct pgtable_debug_args *args) 12162306a36Sopenharmony_ci{ 12262306a36Sopenharmony_ci struct page *page; 12362306a36Sopenharmony_ci pte_t pte; 12462306a36Sopenharmony_ci 12562306a36Sopenharmony_ci /* 12662306a36Sopenharmony_ci * Architectures optimize set_pte_at by avoiding TLB flush. 12762306a36Sopenharmony_ci * This requires set_pte_at to be not used to update an 12862306a36Sopenharmony_ci * existing pte entry. Clear pte before we do set_pte_at 12962306a36Sopenharmony_ci * 13062306a36Sopenharmony_ci * flush_dcache_page() is called after set_pte_at() to clear 13162306a36Sopenharmony_ci * PG_arch_1 for the page on ARM64. The page flag isn't cleared 13262306a36Sopenharmony_ci * when it's released and page allocation check will fail when 13362306a36Sopenharmony_ci * the page is allocated again. For architectures other than ARM64, 13462306a36Sopenharmony_ci * the unexpected overhead of cache flushing is acceptable. 13562306a36Sopenharmony_ci */ 13662306a36Sopenharmony_ci page = (args->pte_pfn != ULONG_MAX) ? pfn_to_page(args->pte_pfn) : NULL; 13762306a36Sopenharmony_ci if (!page) 13862306a36Sopenharmony_ci return; 13962306a36Sopenharmony_ci 14062306a36Sopenharmony_ci pr_debug("Validating PTE advanced\n"); 14162306a36Sopenharmony_ci if (WARN_ON(!args->ptep)) 14262306a36Sopenharmony_ci return; 14362306a36Sopenharmony_ci 14462306a36Sopenharmony_ci pte = pfn_pte(args->pte_pfn, args->page_prot); 14562306a36Sopenharmony_ci set_pte_at(args->mm, args->vaddr, args->ptep, pte); 14662306a36Sopenharmony_ci flush_dcache_page(page); 14762306a36Sopenharmony_ci ptep_set_wrprotect(args->mm, args->vaddr, args->ptep); 14862306a36Sopenharmony_ci pte = ptep_get(args->ptep); 14962306a36Sopenharmony_ci WARN_ON(pte_write(pte)); 15062306a36Sopenharmony_ci ptep_get_and_clear(args->mm, args->vaddr, args->ptep); 15162306a36Sopenharmony_ci pte = ptep_get(args->ptep); 15262306a36Sopenharmony_ci WARN_ON(!pte_none(pte)); 15362306a36Sopenharmony_ci 15462306a36Sopenharmony_ci pte = pfn_pte(args->pte_pfn, args->page_prot); 15562306a36Sopenharmony_ci pte = pte_wrprotect(pte); 15662306a36Sopenharmony_ci pte = pte_mkclean(pte); 15762306a36Sopenharmony_ci set_pte_at(args->mm, args->vaddr, args->ptep, pte); 15862306a36Sopenharmony_ci flush_dcache_page(page); 15962306a36Sopenharmony_ci pte = pte_mkwrite(pte, args->vma); 16062306a36Sopenharmony_ci pte = pte_mkdirty(pte); 16162306a36Sopenharmony_ci ptep_set_access_flags(args->vma, args->vaddr, args->ptep, pte, 1); 16262306a36Sopenharmony_ci pte = ptep_get(args->ptep); 16362306a36Sopenharmony_ci WARN_ON(!(pte_write(pte) && pte_dirty(pte))); 16462306a36Sopenharmony_ci ptep_get_and_clear_full(args->mm, args->vaddr, args->ptep, 1); 16562306a36Sopenharmony_ci pte = ptep_get(args->ptep); 16662306a36Sopenharmony_ci WARN_ON(!pte_none(pte)); 16762306a36Sopenharmony_ci 16862306a36Sopenharmony_ci pte = pfn_pte(args->pte_pfn, args->page_prot); 16962306a36Sopenharmony_ci pte = pte_mkyoung(pte); 17062306a36Sopenharmony_ci set_pte_at(args->mm, args->vaddr, args->ptep, pte); 17162306a36Sopenharmony_ci flush_dcache_page(page); 17262306a36Sopenharmony_ci ptep_test_and_clear_young(args->vma, args->vaddr, args->ptep); 17362306a36Sopenharmony_ci pte = ptep_get(args->ptep); 17462306a36Sopenharmony_ci WARN_ON(pte_young(pte)); 17562306a36Sopenharmony_ci 17662306a36Sopenharmony_ci ptep_get_and_clear_full(args->mm, args->vaddr, args->ptep, 1); 17762306a36Sopenharmony_ci} 17862306a36Sopenharmony_ci 17962306a36Sopenharmony_ci#ifdef CONFIG_TRANSPARENT_HUGEPAGE 18062306a36Sopenharmony_cistatic void __init pmd_basic_tests(struct pgtable_debug_args *args, int idx) 18162306a36Sopenharmony_ci{ 18262306a36Sopenharmony_ci pgprot_t prot = vm_get_page_prot(idx); 18362306a36Sopenharmony_ci unsigned long val = idx, *ptr = &val; 18462306a36Sopenharmony_ci pmd_t pmd; 18562306a36Sopenharmony_ci 18662306a36Sopenharmony_ci if (!has_transparent_hugepage()) 18762306a36Sopenharmony_ci return; 18862306a36Sopenharmony_ci 18962306a36Sopenharmony_ci pr_debug("Validating PMD basic (%pGv)\n", ptr); 19062306a36Sopenharmony_ci pmd = pfn_pmd(args->fixed_pmd_pfn, prot); 19162306a36Sopenharmony_ci 19262306a36Sopenharmony_ci /* 19362306a36Sopenharmony_ci * This test needs to be executed after the given page table entry 19462306a36Sopenharmony_ci * is created with pfn_pmd() to make sure that vm_get_page_prot(idx) 19562306a36Sopenharmony_ci * does not have the dirty bit enabled from the beginning. This is 19662306a36Sopenharmony_ci * important for platforms like arm64 where (!PTE_RDONLY) indicate 19762306a36Sopenharmony_ci * dirty bit being set. 19862306a36Sopenharmony_ci */ 19962306a36Sopenharmony_ci WARN_ON(pmd_dirty(pmd_wrprotect(pmd))); 20062306a36Sopenharmony_ci 20162306a36Sopenharmony_ci 20262306a36Sopenharmony_ci WARN_ON(!pmd_same(pmd, pmd)); 20362306a36Sopenharmony_ci WARN_ON(!pmd_young(pmd_mkyoung(pmd_mkold(pmd)))); 20462306a36Sopenharmony_ci WARN_ON(!pmd_dirty(pmd_mkdirty(pmd_mkclean(pmd)))); 20562306a36Sopenharmony_ci WARN_ON(!pmd_write(pmd_mkwrite(pmd_wrprotect(pmd), args->vma))); 20662306a36Sopenharmony_ci WARN_ON(pmd_young(pmd_mkold(pmd_mkyoung(pmd)))); 20762306a36Sopenharmony_ci WARN_ON(pmd_dirty(pmd_mkclean(pmd_mkdirty(pmd)))); 20862306a36Sopenharmony_ci WARN_ON(pmd_write(pmd_wrprotect(pmd_mkwrite(pmd, args->vma)))); 20962306a36Sopenharmony_ci WARN_ON(pmd_dirty(pmd_wrprotect(pmd_mkclean(pmd)))); 21062306a36Sopenharmony_ci WARN_ON(!pmd_dirty(pmd_wrprotect(pmd_mkdirty(pmd)))); 21162306a36Sopenharmony_ci /* 21262306a36Sopenharmony_ci * A huge page does not point to next level page table 21362306a36Sopenharmony_ci * entry. Hence this must qualify as pmd_bad(). 21462306a36Sopenharmony_ci */ 21562306a36Sopenharmony_ci WARN_ON(!pmd_bad(pmd_mkhuge(pmd))); 21662306a36Sopenharmony_ci} 21762306a36Sopenharmony_ci 21862306a36Sopenharmony_cistatic void __init pmd_advanced_tests(struct pgtable_debug_args *args) 21962306a36Sopenharmony_ci{ 22062306a36Sopenharmony_ci struct page *page; 22162306a36Sopenharmony_ci pmd_t pmd; 22262306a36Sopenharmony_ci unsigned long vaddr = args->vaddr; 22362306a36Sopenharmony_ci 22462306a36Sopenharmony_ci if (!has_transparent_hugepage()) 22562306a36Sopenharmony_ci return; 22662306a36Sopenharmony_ci 22762306a36Sopenharmony_ci page = (args->pmd_pfn != ULONG_MAX) ? pfn_to_page(args->pmd_pfn) : NULL; 22862306a36Sopenharmony_ci if (!page) 22962306a36Sopenharmony_ci return; 23062306a36Sopenharmony_ci 23162306a36Sopenharmony_ci /* 23262306a36Sopenharmony_ci * flush_dcache_page() is called after set_pmd_at() to clear 23362306a36Sopenharmony_ci * PG_arch_1 for the page on ARM64. The page flag isn't cleared 23462306a36Sopenharmony_ci * when it's released and page allocation check will fail when 23562306a36Sopenharmony_ci * the page is allocated again. For architectures other than ARM64, 23662306a36Sopenharmony_ci * the unexpected overhead of cache flushing is acceptable. 23762306a36Sopenharmony_ci */ 23862306a36Sopenharmony_ci pr_debug("Validating PMD advanced\n"); 23962306a36Sopenharmony_ci /* Align the address wrt HPAGE_PMD_SIZE */ 24062306a36Sopenharmony_ci vaddr &= HPAGE_PMD_MASK; 24162306a36Sopenharmony_ci 24262306a36Sopenharmony_ci pgtable_trans_huge_deposit(args->mm, args->pmdp, args->start_ptep); 24362306a36Sopenharmony_ci 24462306a36Sopenharmony_ci pmd = pfn_pmd(args->pmd_pfn, args->page_prot); 24562306a36Sopenharmony_ci set_pmd_at(args->mm, vaddr, args->pmdp, pmd); 24662306a36Sopenharmony_ci flush_dcache_page(page); 24762306a36Sopenharmony_ci pmdp_set_wrprotect(args->mm, vaddr, args->pmdp); 24862306a36Sopenharmony_ci pmd = READ_ONCE(*args->pmdp); 24962306a36Sopenharmony_ci WARN_ON(pmd_write(pmd)); 25062306a36Sopenharmony_ci pmdp_huge_get_and_clear(args->mm, vaddr, args->pmdp); 25162306a36Sopenharmony_ci pmd = READ_ONCE(*args->pmdp); 25262306a36Sopenharmony_ci WARN_ON(!pmd_none(pmd)); 25362306a36Sopenharmony_ci 25462306a36Sopenharmony_ci pmd = pfn_pmd(args->pmd_pfn, args->page_prot); 25562306a36Sopenharmony_ci pmd = pmd_wrprotect(pmd); 25662306a36Sopenharmony_ci pmd = pmd_mkclean(pmd); 25762306a36Sopenharmony_ci set_pmd_at(args->mm, vaddr, args->pmdp, pmd); 25862306a36Sopenharmony_ci flush_dcache_page(page); 25962306a36Sopenharmony_ci pmd = pmd_mkwrite(pmd, args->vma); 26062306a36Sopenharmony_ci pmd = pmd_mkdirty(pmd); 26162306a36Sopenharmony_ci pmdp_set_access_flags(args->vma, vaddr, args->pmdp, pmd, 1); 26262306a36Sopenharmony_ci pmd = READ_ONCE(*args->pmdp); 26362306a36Sopenharmony_ci WARN_ON(!(pmd_write(pmd) && pmd_dirty(pmd))); 26462306a36Sopenharmony_ci pmdp_huge_get_and_clear_full(args->vma, vaddr, args->pmdp, 1); 26562306a36Sopenharmony_ci pmd = READ_ONCE(*args->pmdp); 26662306a36Sopenharmony_ci WARN_ON(!pmd_none(pmd)); 26762306a36Sopenharmony_ci 26862306a36Sopenharmony_ci pmd = pmd_mkhuge(pfn_pmd(args->pmd_pfn, args->page_prot)); 26962306a36Sopenharmony_ci pmd = pmd_mkyoung(pmd); 27062306a36Sopenharmony_ci set_pmd_at(args->mm, vaddr, args->pmdp, pmd); 27162306a36Sopenharmony_ci flush_dcache_page(page); 27262306a36Sopenharmony_ci pmdp_test_and_clear_young(args->vma, vaddr, args->pmdp); 27362306a36Sopenharmony_ci pmd = READ_ONCE(*args->pmdp); 27462306a36Sopenharmony_ci WARN_ON(pmd_young(pmd)); 27562306a36Sopenharmony_ci 27662306a36Sopenharmony_ci /* Clear the pte entries */ 27762306a36Sopenharmony_ci pmdp_huge_get_and_clear(args->mm, vaddr, args->pmdp); 27862306a36Sopenharmony_ci pgtable_trans_huge_withdraw(args->mm, args->pmdp); 27962306a36Sopenharmony_ci} 28062306a36Sopenharmony_ci 28162306a36Sopenharmony_cistatic void __init pmd_leaf_tests(struct pgtable_debug_args *args) 28262306a36Sopenharmony_ci{ 28362306a36Sopenharmony_ci pmd_t pmd; 28462306a36Sopenharmony_ci 28562306a36Sopenharmony_ci if (!has_transparent_hugepage()) 28662306a36Sopenharmony_ci return; 28762306a36Sopenharmony_ci 28862306a36Sopenharmony_ci pr_debug("Validating PMD leaf\n"); 28962306a36Sopenharmony_ci pmd = pfn_pmd(args->fixed_pmd_pfn, args->page_prot); 29062306a36Sopenharmony_ci 29162306a36Sopenharmony_ci /* 29262306a36Sopenharmony_ci * PMD based THP is a leaf entry. 29362306a36Sopenharmony_ci */ 29462306a36Sopenharmony_ci pmd = pmd_mkhuge(pmd); 29562306a36Sopenharmony_ci WARN_ON(!pmd_leaf(pmd)); 29662306a36Sopenharmony_ci} 29762306a36Sopenharmony_ci 29862306a36Sopenharmony_ci#ifdef CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD 29962306a36Sopenharmony_cistatic void __init pud_basic_tests(struct pgtable_debug_args *args, int idx) 30062306a36Sopenharmony_ci{ 30162306a36Sopenharmony_ci pgprot_t prot = vm_get_page_prot(idx); 30262306a36Sopenharmony_ci unsigned long val = idx, *ptr = &val; 30362306a36Sopenharmony_ci pud_t pud; 30462306a36Sopenharmony_ci 30562306a36Sopenharmony_ci if (!has_transparent_pud_hugepage()) 30662306a36Sopenharmony_ci return; 30762306a36Sopenharmony_ci 30862306a36Sopenharmony_ci pr_debug("Validating PUD basic (%pGv)\n", ptr); 30962306a36Sopenharmony_ci pud = pfn_pud(args->fixed_pud_pfn, prot); 31062306a36Sopenharmony_ci 31162306a36Sopenharmony_ci /* 31262306a36Sopenharmony_ci * This test needs to be executed after the given page table entry 31362306a36Sopenharmony_ci * is created with pfn_pud() to make sure that vm_get_page_prot(idx) 31462306a36Sopenharmony_ci * does not have the dirty bit enabled from the beginning. This is 31562306a36Sopenharmony_ci * important for platforms like arm64 where (!PTE_RDONLY) indicate 31662306a36Sopenharmony_ci * dirty bit being set. 31762306a36Sopenharmony_ci */ 31862306a36Sopenharmony_ci WARN_ON(pud_dirty(pud_wrprotect(pud))); 31962306a36Sopenharmony_ci 32062306a36Sopenharmony_ci WARN_ON(!pud_same(pud, pud)); 32162306a36Sopenharmony_ci WARN_ON(!pud_young(pud_mkyoung(pud_mkold(pud)))); 32262306a36Sopenharmony_ci WARN_ON(!pud_dirty(pud_mkdirty(pud_mkclean(pud)))); 32362306a36Sopenharmony_ci WARN_ON(pud_dirty(pud_mkclean(pud_mkdirty(pud)))); 32462306a36Sopenharmony_ci WARN_ON(!pud_write(pud_mkwrite(pud_wrprotect(pud)))); 32562306a36Sopenharmony_ci WARN_ON(pud_write(pud_wrprotect(pud_mkwrite(pud)))); 32662306a36Sopenharmony_ci WARN_ON(pud_young(pud_mkold(pud_mkyoung(pud)))); 32762306a36Sopenharmony_ci WARN_ON(pud_dirty(pud_wrprotect(pud_mkclean(pud)))); 32862306a36Sopenharmony_ci WARN_ON(!pud_dirty(pud_wrprotect(pud_mkdirty(pud)))); 32962306a36Sopenharmony_ci 33062306a36Sopenharmony_ci if (mm_pmd_folded(args->mm)) 33162306a36Sopenharmony_ci return; 33262306a36Sopenharmony_ci 33362306a36Sopenharmony_ci /* 33462306a36Sopenharmony_ci * A huge page does not point to next level page table 33562306a36Sopenharmony_ci * entry. Hence this must qualify as pud_bad(). 33662306a36Sopenharmony_ci */ 33762306a36Sopenharmony_ci WARN_ON(!pud_bad(pud_mkhuge(pud))); 33862306a36Sopenharmony_ci} 33962306a36Sopenharmony_ci 34062306a36Sopenharmony_cistatic void __init pud_advanced_tests(struct pgtable_debug_args *args) 34162306a36Sopenharmony_ci{ 34262306a36Sopenharmony_ci struct page *page; 34362306a36Sopenharmony_ci unsigned long vaddr = args->vaddr; 34462306a36Sopenharmony_ci pud_t pud; 34562306a36Sopenharmony_ci 34662306a36Sopenharmony_ci if (!has_transparent_pud_hugepage()) 34762306a36Sopenharmony_ci return; 34862306a36Sopenharmony_ci 34962306a36Sopenharmony_ci page = (args->pud_pfn != ULONG_MAX) ? pfn_to_page(args->pud_pfn) : NULL; 35062306a36Sopenharmony_ci if (!page) 35162306a36Sopenharmony_ci return; 35262306a36Sopenharmony_ci 35362306a36Sopenharmony_ci /* 35462306a36Sopenharmony_ci * flush_dcache_page() is called after set_pud_at() to clear 35562306a36Sopenharmony_ci * PG_arch_1 for the page on ARM64. The page flag isn't cleared 35662306a36Sopenharmony_ci * when it's released and page allocation check will fail when 35762306a36Sopenharmony_ci * the page is allocated again. For architectures other than ARM64, 35862306a36Sopenharmony_ci * the unexpected overhead of cache flushing is acceptable. 35962306a36Sopenharmony_ci */ 36062306a36Sopenharmony_ci pr_debug("Validating PUD advanced\n"); 36162306a36Sopenharmony_ci /* Align the address wrt HPAGE_PUD_SIZE */ 36262306a36Sopenharmony_ci vaddr &= HPAGE_PUD_MASK; 36362306a36Sopenharmony_ci 36462306a36Sopenharmony_ci pud = pfn_pud(args->pud_pfn, args->page_prot); 36562306a36Sopenharmony_ci /* 36662306a36Sopenharmony_ci * Some architectures have debug checks to make sure 36762306a36Sopenharmony_ci * huge pud mapping are only found with devmap entries 36862306a36Sopenharmony_ci * For now test with only devmap entries. 36962306a36Sopenharmony_ci */ 37062306a36Sopenharmony_ci pud = pud_mkdevmap(pud); 37162306a36Sopenharmony_ci set_pud_at(args->mm, vaddr, args->pudp, pud); 37262306a36Sopenharmony_ci flush_dcache_page(page); 37362306a36Sopenharmony_ci pudp_set_wrprotect(args->mm, vaddr, args->pudp); 37462306a36Sopenharmony_ci pud = READ_ONCE(*args->pudp); 37562306a36Sopenharmony_ci WARN_ON(pud_write(pud)); 37662306a36Sopenharmony_ci 37762306a36Sopenharmony_ci#ifndef __PAGETABLE_PMD_FOLDED 37862306a36Sopenharmony_ci pudp_huge_get_and_clear(args->mm, vaddr, args->pudp); 37962306a36Sopenharmony_ci pud = READ_ONCE(*args->pudp); 38062306a36Sopenharmony_ci WARN_ON(!pud_none(pud)); 38162306a36Sopenharmony_ci#endif /* __PAGETABLE_PMD_FOLDED */ 38262306a36Sopenharmony_ci pud = pfn_pud(args->pud_pfn, args->page_prot); 38362306a36Sopenharmony_ci pud = pud_mkdevmap(pud); 38462306a36Sopenharmony_ci pud = pud_wrprotect(pud); 38562306a36Sopenharmony_ci pud = pud_mkclean(pud); 38662306a36Sopenharmony_ci set_pud_at(args->mm, vaddr, args->pudp, pud); 38762306a36Sopenharmony_ci flush_dcache_page(page); 38862306a36Sopenharmony_ci pud = pud_mkwrite(pud); 38962306a36Sopenharmony_ci pud = pud_mkdirty(pud); 39062306a36Sopenharmony_ci pudp_set_access_flags(args->vma, vaddr, args->pudp, pud, 1); 39162306a36Sopenharmony_ci pud = READ_ONCE(*args->pudp); 39262306a36Sopenharmony_ci WARN_ON(!(pud_write(pud) && pud_dirty(pud))); 39362306a36Sopenharmony_ci 39462306a36Sopenharmony_ci#ifndef __PAGETABLE_PMD_FOLDED 39562306a36Sopenharmony_ci pudp_huge_get_and_clear_full(args->vma, vaddr, args->pudp, 1); 39662306a36Sopenharmony_ci pud = READ_ONCE(*args->pudp); 39762306a36Sopenharmony_ci WARN_ON(!pud_none(pud)); 39862306a36Sopenharmony_ci#endif /* __PAGETABLE_PMD_FOLDED */ 39962306a36Sopenharmony_ci 40062306a36Sopenharmony_ci pud = pfn_pud(args->pud_pfn, args->page_prot); 40162306a36Sopenharmony_ci pud = pud_mkdevmap(pud); 40262306a36Sopenharmony_ci pud = pud_mkyoung(pud); 40362306a36Sopenharmony_ci set_pud_at(args->mm, vaddr, args->pudp, pud); 40462306a36Sopenharmony_ci flush_dcache_page(page); 40562306a36Sopenharmony_ci pudp_test_and_clear_young(args->vma, vaddr, args->pudp); 40662306a36Sopenharmony_ci pud = READ_ONCE(*args->pudp); 40762306a36Sopenharmony_ci WARN_ON(pud_young(pud)); 40862306a36Sopenharmony_ci 40962306a36Sopenharmony_ci pudp_huge_get_and_clear(args->mm, vaddr, args->pudp); 41062306a36Sopenharmony_ci} 41162306a36Sopenharmony_ci 41262306a36Sopenharmony_cistatic void __init pud_leaf_tests(struct pgtable_debug_args *args) 41362306a36Sopenharmony_ci{ 41462306a36Sopenharmony_ci pud_t pud; 41562306a36Sopenharmony_ci 41662306a36Sopenharmony_ci if (!has_transparent_pud_hugepage()) 41762306a36Sopenharmony_ci return; 41862306a36Sopenharmony_ci 41962306a36Sopenharmony_ci pr_debug("Validating PUD leaf\n"); 42062306a36Sopenharmony_ci pud = pfn_pud(args->fixed_pud_pfn, args->page_prot); 42162306a36Sopenharmony_ci /* 42262306a36Sopenharmony_ci * PUD based THP is a leaf entry. 42362306a36Sopenharmony_ci */ 42462306a36Sopenharmony_ci pud = pud_mkhuge(pud); 42562306a36Sopenharmony_ci WARN_ON(!pud_leaf(pud)); 42662306a36Sopenharmony_ci} 42762306a36Sopenharmony_ci#else /* !CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD */ 42862306a36Sopenharmony_cistatic void __init pud_basic_tests(struct pgtable_debug_args *args, int idx) { } 42962306a36Sopenharmony_cistatic void __init pud_advanced_tests(struct pgtable_debug_args *args) { } 43062306a36Sopenharmony_cistatic void __init pud_leaf_tests(struct pgtable_debug_args *args) { } 43162306a36Sopenharmony_ci#endif /* CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD */ 43262306a36Sopenharmony_ci#else /* !CONFIG_TRANSPARENT_HUGEPAGE */ 43362306a36Sopenharmony_cistatic void __init pmd_basic_tests(struct pgtable_debug_args *args, int idx) { } 43462306a36Sopenharmony_cistatic void __init pud_basic_tests(struct pgtable_debug_args *args, int idx) { } 43562306a36Sopenharmony_cistatic void __init pmd_advanced_tests(struct pgtable_debug_args *args) { } 43662306a36Sopenharmony_cistatic void __init pud_advanced_tests(struct pgtable_debug_args *args) { } 43762306a36Sopenharmony_cistatic void __init pmd_leaf_tests(struct pgtable_debug_args *args) { } 43862306a36Sopenharmony_cistatic void __init pud_leaf_tests(struct pgtable_debug_args *args) { } 43962306a36Sopenharmony_ci#endif /* CONFIG_TRANSPARENT_HUGEPAGE */ 44062306a36Sopenharmony_ci 44162306a36Sopenharmony_ci#ifdef CONFIG_HAVE_ARCH_HUGE_VMAP 44262306a36Sopenharmony_cistatic void __init pmd_huge_tests(struct pgtable_debug_args *args) 44362306a36Sopenharmony_ci{ 44462306a36Sopenharmony_ci pmd_t pmd; 44562306a36Sopenharmony_ci 44662306a36Sopenharmony_ci if (!arch_vmap_pmd_supported(args->page_prot) || 44762306a36Sopenharmony_ci args->fixed_alignment < PMD_SIZE) 44862306a36Sopenharmony_ci return; 44962306a36Sopenharmony_ci 45062306a36Sopenharmony_ci pr_debug("Validating PMD huge\n"); 45162306a36Sopenharmony_ci /* 45262306a36Sopenharmony_ci * X86 defined pmd_set_huge() verifies that the given 45362306a36Sopenharmony_ci * PMD is not a populated non-leaf entry. 45462306a36Sopenharmony_ci */ 45562306a36Sopenharmony_ci WRITE_ONCE(*args->pmdp, __pmd(0)); 45662306a36Sopenharmony_ci WARN_ON(!pmd_set_huge(args->pmdp, __pfn_to_phys(args->fixed_pmd_pfn), args->page_prot)); 45762306a36Sopenharmony_ci WARN_ON(!pmd_clear_huge(args->pmdp)); 45862306a36Sopenharmony_ci pmd = READ_ONCE(*args->pmdp); 45962306a36Sopenharmony_ci WARN_ON(!pmd_none(pmd)); 46062306a36Sopenharmony_ci} 46162306a36Sopenharmony_ci 46262306a36Sopenharmony_cistatic void __init pud_huge_tests(struct pgtable_debug_args *args) 46362306a36Sopenharmony_ci{ 46462306a36Sopenharmony_ci pud_t pud; 46562306a36Sopenharmony_ci 46662306a36Sopenharmony_ci if (!arch_vmap_pud_supported(args->page_prot) || 46762306a36Sopenharmony_ci args->fixed_alignment < PUD_SIZE) 46862306a36Sopenharmony_ci return; 46962306a36Sopenharmony_ci 47062306a36Sopenharmony_ci pr_debug("Validating PUD huge\n"); 47162306a36Sopenharmony_ci /* 47262306a36Sopenharmony_ci * X86 defined pud_set_huge() verifies that the given 47362306a36Sopenharmony_ci * PUD is not a populated non-leaf entry. 47462306a36Sopenharmony_ci */ 47562306a36Sopenharmony_ci WRITE_ONCE(*args->pudp, __pud(0)); 47662306a36Sopenharmony_ci WARN_ON(!pud_set_huge(args->pudp, __pfn_to_phys(args->fixed_pud_pfn), args->page_prot)); 47762306a36Sopenharmony_ci WARN_ON(!pud_clear_huge(args->pudp)); 47862306a36Sopenharmony_ci pud = READ_ONCE(*args->pudp); 47962306a36Sopenharmony_ci WARN_ON(!pud_none(pud)); 48062306a36Sopenharmony_ci} 48162306a36Sopenharmony_ci#else /* !CONFIG_HAVE_ARCH_HUGE_VMAP */ 48262306a36Sopenharmony_cistatic void __init pmd_huge_tests(struct pgtable_debug_args *args) { } 48362306a36Sopenharmony_cistatic void __init pud_huge_tests(struct pgtable_debug_args *args) { } 48462306a36Sopenharmony_ci#endif /* CONFIG_HAVE_ARCH_HUGE_VMAP */ 48562306a36Sopenharmony_ci 48662306a36Sopenharmony_cistatic void __init p4d_basic_tests(struct pgtable_debug_args *args) 48762306a36Sopenharmony_ci{ 48862306a36Sopenharmony_ci p4d_t p4d; 48962306a36Sopenharmony_ci 49062306a36Sopenharmony_ci pr_debug("Validating P4D basic\n"); 49162306a36Sopenharmony_ci memset(&p4d, RANDOM_NZVALUE, sizeof(p4d_t)); 49262306a36Sopenharmony_ci WARN_ON(!p4d_same(p4d, p4d)); 49362306a36Sopenharmony_ci} 49462306a36Sopenharmony_ci 49562306a36Sopenharmony_cistatic void __init pgd_basic_tests(struct pgtable_debug_args *args) 49662306a36Sopenharmony_ci{ 49762306a36Sopenharmony_ci pgd_t pgd; 49862306a36Sopenharmony_ci 49962306a36Sopenharmony_ci pr_debug("Validating PGD basic\n"); 50062306a36Sopenharmony_ci memset(&pgd, RANDOM_NZVALUE, sizeof(pgd_t)); 50162306a36Sopenharmony_ci WARN_ON(!pgd_same(pgd, pgd)); 50262306a36Sopenharmony_ci} 50362306a36Sopenharmony_ci 50462306a36Sopenharmony_ci#ifndef __PAGETABLE_PUD_FOLDED 50562306a36Sopenharmony_cistatic void __init pud_clear_tests(struct pgtable_debug_args *args) 50662306a36Sopenharmony_ci{ 50762306a36Sopenharmony_ci pud_t pud = READ_ONCE(*args->pudp); 50862306a36Sopenharmony_ci 50962306a36Sopenharmony_ci if (mm_pmd_folded(args->mm)) 51062306a36Sopenharmony_ci return; 51162306a36Sopenharmony_ci 51262306a36Sopenharmony_ci pr_debug("Validating PUD clear\n"); 51362306a36Sopenharmony_ci pud = __pud(pud_val(pud) | RANDOM_ORVALUE); 51462306a36Sopenharmony_ci WRITE_ONCE(*args->pudp, pud); 51562306a36Sopenharmony_ci pud_clear(args->pudp); 51662306a36Sopenharmony_ci pud = READ_ONCE(*args->pudp); 51762306a36Sopenharmony_ci WARN_ON(!pud_none(pud)); 51862306a36Sopenharmony_ci} 51962306a36Sopenharmony_ci 52062306a36Sopenharmony_cistatic void __init pud_populate_tests(struct pgtable_debug_args *args) 52162306a36Sopenharmony_ci{ 52262306a36Sopenharmony_ci pud_t pud; 52362306a36Sopenharmony_ci 52462306a36Sopenharmony_ci if (mm_pmd_folded(args->mm)) 52562306a36Sopenharmony_ci return; 52662306a36Sopenharmony_ci 52762306a36Sopenharmony_ci pr_debug("Validating PUD populate\n"); 52862306a36Sopenharmony_ci /* 52962306a36Sopenharmony_ci * This entry points to next level page table page. 53062306a36Sopenharmony_ci * Hence this must not qualify as pud_bad(). 53162306a36Sopenharmony_ci */ 53262306a36Sopenharmony_ci pud_populate(args->mm, args->pudp, args->start_pmdp); 53362306a36Sopenharmony_ci pud = READ_ONCE(*args->pudp); 53462306a36Sopenharmony_ci WARN_ON(pud_bad(pud)); 53562306a36Sopenharmony_ci} 53662306a36Sopenharmony_ci#else /* !__PAGETABLE_PUD_FOLDED */ 53762306a36Sopenharmony_cistatic void __init pud_clear_tests(struct pgtable_debug_args *args) { } 53862306a36Sopenharmony_cistatic void __init pud_populate_tests(struct pgtable_debug_args *args) { } 53962306a36Sopenharmony_ci#endif /* PAGETABLE_PUD_FOLDED */ 54062306a36Sopenharmony_ci 54162306a36Sopenharmony_ci#ifndef __PAGETABLE_P4D_FOLDED 54262306a36Sopenharmony_cistatic void __init p4d_clear_tests(struct pgtable_debug_args *args) 54362306a36Sopenharmony_ci{ 54462306a36Sopenharmony_ci p4d_t p4d = READ_ONCE(*args->p4dp); 54562306a36Sopenharmony_ci 54662306a36Sopenharmony_ci if (mm_pud_folded(args->mm)) 54762306a36Sopenharmony_ci return; 54862306a36Sopenharmony_ci 54962306a36Sopenharmony_ci pr_debug("Validating P4D clear\n"); 55062306a36Sopenharmony_ci p4d = __p4d(p4d_val(p4d) | RANDOM_ORVALUE); 55162306a36Sopenharmony_ci WRITE_ONCE(*args->p4dp, p4d); 55262306a36Sopenharmony_ci p4d_clear(args->p4dp); 55362306a36Sopenharmony_ci p4d = READ_ONCE(*args->p4dp); 55462306a36Sopenharmony_ci WARN_ON(!p4d_none(p4d)); 55562306a36Sopenharmony_ci} 55662306a36Sopenharmony_ci 55762306a36Sopenharmony_cistatic void __init p4d_populate_tests(struct pgtable_debug_args *args) 55862306a36Sopenharmony_ci{ 55962306a36Sopenharmony_ci p4d_t p4d; 56062306a36Sopenharmony_ci 56162306a36Sopenharmony_ci if (mm_pud_folded(args->mm)) 56262306a36Sopenharmony_ci return; 56362306a36Sopenharmony_ci 56462306a36Sopenharmony_ci pr_debug("Validating P4D populate\n"); 56562306a36Sopenharmony_ci /* 56662306a36Sopenharmony_ci * This entry points to next level page table page. 56762306a36Sopenharmony_ci * Hence this must not qualify as p4d_bad(). 56862306a36Sopenharmony_ci */ 56962306a36Sopenharmony_ci pud_clear(args->pudp); 57062306a36Sopenharmony_ci p4d_clear(args->p4dp); 57162306a36Sopenharmony_ci p4d_populate(args->mm, args->p4dp, args->start_pudp); 57262306a36Sopenharmony_ci p4d = READ_ONCE(*args->p4dp); 57362306a36Sopenharmony_ci WARN_ON(p4d_bad(p4d)); 57462306a36Sopenharmony_ci} 57562306a36Sopenharmony_ci 57662306a36Sopenharmony_cistatic void __init pgd_clear_tests(struct pgtable_debug_args *args) 57762306a36Sopenharmony_ci{ 57862306a36Sopenharmony_ci pgd_t pgd = READ_ONCE(*(args->pgdp)); 57962306a36Sopenharmony_ci 58062306a36Sopenharmony_ci if (mm_p4d_folded(args->mm)) 58162306a36Sopenharmony_ci return; 58262306a36Sopenharmony_ci 58362306a36Sopenharmony_ci pr_debug("Validating PGD clear\n"); 58462306a36Sopenharmony_ci pgd = __pgd(pgd_val(pgd) | RANDOM_ORVALUE); 58562306a36Sopenharmony_ci WRITE_ONCE(*args->pgdp, pgd); 58662306a36Sopenharmony_ci pgd_clear(args->pgdp); 58762306a36Sopenharmony_ci pgd = READ_ONCE(*args->pgdp); 58862306a36Sopenharmony_ci WARN_ON(!pgd_none(pgd)); 58962306a36Sopenharmony_ci} 59062306a36Sopenharmony_ci 59162306a36Sopenharmony_cistatic void __init pgd_populate_tests(struct pgtable_debug_args *args) 59262306a36Sopenharmony_ci{ 59362306a36Sopenharmony_ci pgd_t pgd; 59462306a36Sopenharmony_ci 59562306a36Sopenharmony_ci if (mm_p4d_folded(args->mm)) 59662306a36Sopenharmony_ci return; 59762306a36Sopenharmony_ci 59862306a36Sopenharmony_ci pr_debug("Validating PGD populate\n"); 59962306a36Sopenharmony_ci /* 60062306a36Sopenharmony_ci * This entry points to next level page table page. 60162306a36Sopenharmony_ci * Hence this must not qualify as pgd_bad(). 60262306a36Sopenharmony_ci */ 60362306a36Sopenharmony_ci p4d_clear(args->p4dp); 60462306a36Sopenharmony_ci pgd_clear(args->pgdp); 60562306a36Sopenharmony_ci pgd_populate(args->mm, args->pgdp, args->start_p4dp); 60662306a36Sopenharmony_ci pgd = READ_ONCE(*args->pgdp); 60762306a36Sopenharmony_ci WARN_ON(pgd_bad(pgd)); 60862306a36Sopenharmony_ci} 60962306a36Sopenharmony_ci#else /* !__PAGETABLE_P4D_FOLDED */ 61062306a36Sopenharmony_cistatic void __init p4d_clear_tests(struct pgtable_debug_args *args) { } 61162306a36Sopenharmony_cistatic void __init pgd_clear_tests(struct pgtable_debug_args *args) { } 61262306a36Sopenharmony_cistatic void __init p4d_populate_tests(struct pgtable_debug_args *args) { } 61362306a36Sopenharmony_cistatic void __init pgd_populate_tests(struct pgtable_debug_args *args) { } 61462306a36Sopenharmony_ci#endif /* PAGETABLE_P4D_FOLDED */ 61562306a36Sopenharmony_ci 61662306a36Sopenharmony_cistatic void __init pte_clear_tests(struct pgtable_debug_args *args) 61762306a36Sopenharmony_ci{ 61862306a36Sopenharmony_ci struct page *page; 61962306a36Sopenharmony_ci pte_t pte = pfn_pte(args->pte_pfn, args->page_prot); 62062306a36Sopenharmony_ci 62162306a36Sopenharmony_ci page = (args->pte_pfn != ULONG_MAX) ? pfn_to_page(args->pte_pfn) : NULL; 62262306a36Sopenharmony_ci if (!page) 62362306a36Sopenharmony_ci return; 62462306a36Sopenharmony_ci 62562306a36Sopenharmony_ci /* 62662306a36Sopenharmony_ci * flush_dcache_page() is called after set_pte_at() to clear 62762306a36Sopenharmony_ci * PG_arch_1 for the page on ARM64. The page flag isn't cleared 62862306a36Sopenharmony_ci * when it's released and page allocation check will fail when 62962306a36Sopenharmony_ci * the page is allocated again. For architectures other than ARM64, 63062306a36Sopenharmony_ci * the unexpected overhead of cache flushing is acceptable. 63162306a36Sopenharmony_ci */ 63262306a36Sopenharmony_ci pr_debug("Validating PTE clear\n"); 63362306a36Sopenharmony_ci if (WARN_ON(!args->ptep)) 63462306a36Sopenharmony_ci return; 63562306a36Sopenharmony_ci 63662306a36Sopenharmony_ci#ifndef CONFIG_RISCV 63762306a36Sopenharmony_ci pte = __pte(pte_val(pte) | RANDOM_ORVALUE); 63862306a36Sopenharmony_ci#endif 63962306a36Sopenharmony_ci set_pte_at(args->mm, args->vaddr, args->ptep, pte); 64062306a36Sopenharmony_ci flush_dcache_page(page); 64162306a36Sopenharmony_ci barrier(); 64262306a36Sopenharmony_ci ptep_clear(args->mm, args->vaddr, args->ptep); 64362306a36Sopenharmony_ci pte = ptep_get(args->ptep); 64462306a36Sopenharmony_ci WARN_ON(!pte_none(pte)); 64562306a36Sopenharmony_ci} 64662306a36Sopenharmony_ci 64762306a36Sopenharmony_cistatic void __init pmd_clear_tests(struct pgtable_debug_args *args) 64862306a36Sopenharmony_ci{ 64962306a36Sopenharmony_ci pmd_t pmd = READ_ONCE(*args->pmdp); 65062306a36Sopenharmony_ci 65162306a36Sopenharmony_ci pr_debug("Validating PMD clear\n"); 65262306a36Sopenharmony_ci pmd = __pmd(pmd_val(pmd) | RANDOM_ORVALUE); 65362306a36Sopenharmony_ci WRITE_ONCE(*args->pmdp, pmd); 65462306a36Sopenharmony_ci pmd_clear(args->pmdp); 65562306a36Sopenharmony_ci pmd = READ_ONCE(*args->pmdp); 65662306a36Sopenharmony_ci WARN_ON(!pmd_none(pmd)); 65762306a36Sopenharmony_ci} 65862306a36Sopenharmony_ci 65962306a36Sopenharmony_cistatic void __init pmd_populate_tests(struct pgtable_debug_args *args) 66062306a36Sopenharmony_ci{ 66162306a36Sopenharmony_ci pmd_t pmd; 66262306a36Sopenharmony_ci 66362306a36Sopenharmony_ci pr_debug("Validating PMD populate\n"); 66462306a36Sopenharmony_ci /* 66562306a36Sopenharmony_ci * This entry points to next level page table page. 66662306a36Sopenharmony_ci * Hence this must not qualify as pmd_bad(). 66762306a36Sopenharmony_ci */ 66862306a36Sopenharmony_ci pmd_populate(args->mm, args->pmdp, args->start_ptep); 66962306a36Sopenharmony_ci pmd = READ_ONCE(*args->pmdp); 67062306a36Sopenharmony_ci WARN_ON(pmd_bad(pmd)); 67162306a36Sopenharmony_ci} 67262306a36Sopenharmony_ci 67362306a36Sopenharmony_cistatic void __init pte_special_tests(struct pgtable_debug_args *args) 67462306a36Sopenharmony_ci{ 67562306a36Sopenharmony_ci pte_t pte = pfn_pte(args->fixed_pte_pfn, args->page_prot); 67662306a36Sopenharmony_ci 67762306a36Sopenharmony_ci if (!IS_ENABLED(CONFIG_ARCH_HAS_PTE_SPECIAL)) 67862306a36Sopenharmony_ci return; 67962306a36Sopenharmony_ci 68062306a36Sopenharmony_ci pr_debug("Validating PTE special\n"); 68162306a36Sopenharmony_ci WARN_ON(!pte_special(pte_mkspecial(pte))); 68262306a36Sopenharmony_ci} 68362306a36Sopenharmony_ci 68462306a36Sopenharmony_cistatic void __init pte_protnone_tests(struct pgtable_debug_args *args) 68562306a36Sopenharmony_ci{ 68662306a36Sopenharmony_ci pte_t pte = pfn_pte(args->fixed_pte_pfn, args->page_prot_none); 68762306a36Sopenharmony_ci 68862306a36Sopenharmony_ci if (!IS_ENABLED(CONFIG_NUMA_BALANCING)) 68962306a36Sopenharmony_ci return; 69062306a36Sopenharmony_ci 69162306a36Sopenharmony_ci pr_debug("Validating PTE protnone\n"); 69262306a36Sopenharmony_ci WARN_ON(!pte_protnone(pte)); 69362306a36Sopenharmony_ci WARN_ON(!pte_present(pte)); 69462306a36Sopenharmony_ci} 69562306a36Sopenharmony_ci 69662306a36Sopenharmony_ci#ifdef CONFIG_TRANSPARENT_HUGEPAGE 69762306a36Sopenharmony_cistatic void __init pmd_protnone_tests(struct pgtable_debug_args *args) 69862306a36Sopenharmony_ci{ 69962306a36Sopenharmony_ci pmd_t pmd; 70062306a36Sopenharmony_ci 70162306a36Sopenharmony_ci if (!IS_ENABLED(CONFIG_NUMA_BALANCING)) 70262306a36Sopenharmony_ci return; 70362306a36Sopenharmony_ci 70462306a36Sopenharmony_ci if (!has_transparent_hugepage()) 70562306a36Sopenharmony_ci return; 70662306a36Sopenharmony_ci 70762306a36Sopenharmony_ci pr_debug("Validating PMD protnone\n"); 70862306a36Sopenharmony_ci pmd = pmd_mkhuge(pfn_pmd(args->fixed_pmd_pfn, args->page_prot_none)); 70962306a36Sopenharmony_ci WARN_ON(!pmd_protnone(pmd)); 71062306a36Sopenharmony_ci WARN_ON(!pmd_present(pmd)); 71162306a36Sopenharmony_ci} 71262306a36Sopenharmony_ci#else /* !CONFIG_TRANSPARENT_HUGEPAGE */ 71362306a36Sopenharmony_cistatic void __init pmd_protnone_tests(struct pgtable_debug_args *args) { } 71462306a36Sopenharmony_ci#endif /* CONFIG_TRANSPARENT_HUGEPAGE */ 71562306a36Sopenharmony_ci 71662306a36Sopenharmony_ci#ifdef CONFIG_ARCH_HAS_PTE_DEVMAP 71762306a36Sopenharmony_cistatic void __init pte_devmap_tests(struct pgtable_debug_args *args) 71862306a36Sopenharmony_ci{ 71962306a36Sopenharmony_ci pte_t pte = pfn_pte(args->fixed_pte_pfn, args->page_prot); 72062306a36Sopenharmony_ci 72162306a36Sopenharmony_ci pr_debug("Validating PTE devmap\n"); 72262306a36Sopenharmony_ci WARN_ON(!pte_devmap(pte_mkdevmap(pte))); 72362306a36Sopenharmony_ci} 72462306a36Sopenharmony_ci 72562306a36Sopenharmony_ci#ifdef CONFIG_TRANSPARENT_HUGEPAGE 72662306a36Sopenharmony_cistatic void __init pmd_devmap_tests(struct pgtable_debug_args *args) 72762306a36Sopenharmony_ci{ 72862306a36Sopenharmony_ci pmd_t pmd; 72962306a36Sopenharmony_ci 73062306a36Sopenharmony_ci if (!has_transparent_hugepage()) 73162306a36Sopenharmony_ci return; 73262306a36Sopenharmony_ci 73362306a36Sopenharmony_ci pr_debug("Validating PMD devmap\n"); 73462306a36Sopenharmony_ci pmd = pfn_pmd(args->fixed_pmd_pfn, args->page_prot); 73562306a36Sopenharmony_ci WARN_ON(!pmd_devmap(pmd_mkdevmap(pmd))); 73662306a36Sopenharmony_ci} 73762306a36Sopenharmony_ci 73862306a36Sopenharmony_ci#ifdef CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD 73962306a36Sopenharmony_cistatic void __init pud_devmap_tests(struct pgtable_debug_args *args) 74062306a36Sopenharmony_ci{ 74162306a36Sopenharmony_ci pud_t pud; 74262306a36Sopenharmony_ci 74362306a36Sopenharmony_ci if (!has_transparent_pud_hugepage()) 74462306a36Sopenharmony_ci return; 74562306a36Sopenharmony_ci 74662306a36Sopenharmony_ci pr_debug("Validating PUD devmap\n"); 74762306a36Sopenharmony_ci pud = pfn_pud(args->fixed_pud_pfn, args->page_prot); 74862306a36Sopenharmony_ci WARN_ON(!pud_devmap(pud_mkdevmap(pud))); 74962306a36Sopenharmony_ci} 75062306a36Sopenharmony_ci#else /* !CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD */ 75162306a36Sopenharmony_cistatic void __init pud_devmap_tests(struct pgtable_debug_args *args) { } 75262306a36Sopenharmony_ci#endif /* CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD */ 75362306a36Sopenharmony_ci#else /* CONFIG_TRANSPARENT_HUGEPAGE */ 75462306a36Sopenharmony_cistatic void __init pmd_devmap_tests(struct pgtable_debug_args *args) { } 75562306a36Sopenharmony_cistatic void __init pud_devmap_tests(struct pgtable_debug_args *args) { } 75662306a36Sopenharmony_ci#endif /* CONFIG_TRANSPARENT_HUGEPAGE */ 75762306a36Sopenharmony_ci#else 75862306a36Sopenharmony_cistatic void __init pte_devmap_tests(struct pgtable_debug_args *args) { } 75962306a36Sopenharmony_cistatic void __init pmd_devmap_tests(struct pgtable_debug_args *args) { } 76062306a36Sopenharmony_cistatic void __init pud_devmap_tests(struct pgtable_debug_args *args) { } 76162306a36Sopenharmony_ci#endif /* CONFIG_ARCH_HAS_PTE_DEVMAP */ 76262306a36Sopenharmony_ci 76362306a36Sopenharmony_cistatic void __init pte_soft_dirty_tests(struct pgtable_debug_args *args) 76462306a36Sopenharmony_ci{ 76562306a36Sopenharmony_ci pte_t pte = pfn_pte(args->fixed_pte_pfn, args->page_prot); 76662306a36Sopenharmony_ci 76762306a36Sopenharmony_ci if (!IS_ENABLED(CONFIG_MEM_SOFT_DIRTY)) 76862306a36Sopenharmony_ci return; 76962306a36Sopenharmony_ci 77062306a36Sopenharmony_ci pr_debug("Validating PTE soft dirty\n"); 77162306a36Sopenharmony_ci WARN_ON(!pte_soft_dirty(pte_mksoft_dirty(pte))); 77262306a36Sopenharmony_ci WARN_ON(pte_soft_dirty(pte_clear_soft_dirty(pte))); 77362306a36Sopenharmony_ci} 77462306a36Sopenharmony_ci 77562306a36Sopenharmony_cistatic void __init pte_swap_soft_dirty_tests(struct pgtable_debug_args *args) 77662306a36Sopenharmony_ci{ 77762306a36Sopenharmony_ci pte_t pte = pfn_pte(args->fixed_pte_pfn, args->page_prot); 77862306a36Sopenharmony_ci 77962306a36Sopenharmony_ci if (!IS_ENABLED(CONFIG_MEM_SOFT_DIRTY)) 78062306a36Sopenharmony_ci return; 78162306a36Sopenharmony_ci 78262306a36Sopenharmony_ci pr_debug("Validating PTE swap soft dirty\n"); 78362306a36Sopenharmony_ci WARN_ON(!pte_swp_soft_dirty(pte_swp_mksoft_dirty(pte))); 78462306a36Sopenharmony_ci WARN_ON(pte_swp_soft_dirty(pte_swp_clear_soft_dirty(pte))); 78562306a36Sopenharmony_ci} 78662306a36Sopenharmony_ci 78762306a36Sopenharmony_ci#ifdef CONFIG_TRANSPARENT_HUGEPAGE 78862306a36Sopenharmony_cistatic void __init pmd_soft_dirty_tests(struct pgtable_debug_args *args) 78962306a36Sopenharmony_ci{ 79062306a36Sopenharmony_ci pmd_t pmd; 79162306a36Sopenharmony_ci 79262306a36Sopenharmony_ci if (!IS_ENABLED(CONFIG_MEM_SOFT_DIRTY)) 79362306a36Sopenharmony_ci return; 79462306a36Sopenharmony_ci 79562306a36Sopenharmony_ci if (!has_transparent_hugepage()) 79662306a36Sopenharmony_ci return; 79762306a36Sopenharmony_ci 79862306a36Sopenharmony_ci pr_debug("Validating PMD soft dirty\n"); 79962306a36Sopenharmony_ci pmd = pfn_pmd(args->fixed_pmd_pfn, args->page_prot); 80062306a36Sopenharmony_ci WARN_ON(!pmd_soft_dirty(pmd_mksoft_dirty(pmd))); 80162306a36Sopenharmony_ci WARN_ON(pmd_soft_dirty(pmd_clear_soft_dirty(pmd))); 80262306a36Sopenharmony_ci} 80362306a36Sopenharmony_ci 80462306a36Sopenharmony_cistatic void __init pmd_swap_soft_dirty_tests(struct pgtable_debug_args *args) 80562306a36Sopenharmony_ci{ 80662306a36Sopenharmony_ci pmd_t pmd; 80762306a36Sopenharmony_ci 80862306a36Sopenharmony_ci if (!IS_ENABLED(CONFIG_MEM_SOFT_DIRTY) || 80962306a36Sopenharmony_ci !IS_ENABLED(CONFIG_ARCH_ENABLE_THP_MIGRATION)) 81062306a36Sopenharmony_ci return; 81162306a36Sopenharmony_ci 81262306a36Sopenharmony_ci if (!has_transparent_hugepage()) 81362306a36Sopenharmony_ci return; 81462306a36Sopenharmony_ci 81562306a36Sopenharmony_ci pr_debug("Validating PMD swap soft dirty\n"); 81662306a36Sopenharmony_ci pmd = pfn_pmd(args->fixed_pmd_pfn, args->page_prot); 81762306a36Sopenharmony_ci WARN_ON(!pmd_swp_soft_dirty(pmd_swp_mksoft_dirty(pmd))); 81862306a36Sopenharmony_ci WARN_ON(pmd_swp_soft_dirty(pmd_swp_clear_soft_dirty(pmd))); 81962306a36Sopenharmony_ci} 82062306a36Sopenharmony_ci#else /* !CONFIG_TRANSPARENT_HUGEPAGE */ 82162306a36Sopenharmony_cistatic void __init pmd_soft_dirty_tests(struct pgtable_debug_args *args) { } 82262306a36Sopenharmony_cistatic void __init pmd_swap_soft_dirty_tests(struct pgtable_debug_args *args) { } 82362306a36Sopenharmony_ci#endif /* CONFIG_TRANSPARENT_HUGEPAGE */ 82462306a36Sopenharmony_ci 82562306a36Sopenharmony_cistatic void __init pte_swap_exclusive_tests(struct pgtable_debug_args *args) 82662306a36Sopenharmony_ci{ 82762306a36Sopenharmony_ci unsigned long max_swap_offset; 82862306a36Sopenharmony_ci swp_entry_t entry, entry2; 82962306a36Sopenharmony_ci pte_t pte; 83062306a36Sopenharmony_ci 83162306a36Sopenharmony_ci pr_debug("Validating PTE swap exclusive\n"); 83262306a36Sopenharmony_ci 83362306a36Sopenharmony_ci /* See generic_max_swapfile_size(): probe the maximum offset */ 83462306a36Sopenharmony_ci max_swap_offset = swp_offset(pte_to_swp_entry(swp_entry_to_pte(swp_entry(0, ~0UL)))); 83562306a36Sopenharmony_ci 83662306a36Sopenharmony_ci /* Create a swp entry with all possible bits set */ 83762306a36Sopenharmony_ci entry = swp_entry((1 << MAX_SWAPFILES_SHIFT) - 1, max_swap_offset); 83862306a36Sopenharmony_ci 83962306a36Sopenharmony_ci pte = swp_entry_to_pte(entry); 84062306a36Sopenharmony_ci WARN_ON(pte_swp_exclusive(pte)); 84162306a36Sopenharmony_ci WARN_ON(!is_swap_pte(pte)); 84262306a36Sopenharmony_ci entry2 = pte_to_swp_entry(pte); 84362306a36Sopenharmony_ci WARN_ON(memcmp(&entry, &entry2, sizeof(entry))); 84462306a36Sopenharmony_ci 84562306a36Sopenharmony_ci pte = pte_swp_mkexclusive(pte); 84662306a36Sopenharmony_ci WARN_ON(!pte_swp_exclusive(pte)); 84762306a36Sopenharmony_ci WARN_ON(!is_swap_pte(pte)); 84862306a36Sopenharmony_ci WARN_ON(pte_swp_soft_dirty(pte)); 84962306a36Sopenharmony_ci entry2 = pte_to_swp_entry(pte); 85062306a36Sopenharmony_ci WARN_ON(memcmp(&entry, &entry2, sizeof(entry))); 85162306a36Sopenharmony_ci 85262306a36Sopenharmony_ci pte = pte_swp_clear_exclusive(pte); 85362306a36Sopenharmony_ci WARN_ON(pte_swp_exclusive(pte)); 85462306a36Sopenharmony_ci WARN_ON(!is_swap_pte(pte)); 85562306a36Sopenharmony_ci entry2 = pte_to_swp_entry(pte); 85662306a36Sopenharmony_ci WARN_ON(memcmp(&entry, &entry2, sizeof(entry))); 85762306a36Sopenharmony_ci} 85862306a36Sopenharmony_ci 85962306a36Sopenharmony_cistatic void __init pte_swap_tests(struct pgtable_debug_args *args) 86062306a36Sopenharmony_ci{ 86162306a36Sopenharmony_ci swp_entry_t swp; 86262306a36Sopenharmony_ci pte_t pte; 86362306a36Sopenharmony_ci 86462306a36Sopenharmony_ci pr_debug("Validating PTE swap\n"); 86562306a36Sopenharmony_ci pte = pfn_pte(args->fixed_pte_pfn, args->page_prot); 86662306a36Sopenharmony_ci swp = __pte_to_swp_entry(pte); 86762306a36Sopenharmony_ci pte = __swp_entry_to_pte(swp); 86862306a36Sopenharmony_ci WARN_ON(args->fixed_pte_pfn != pte_pfn(pte)); 86962306a36Sopenharmony_ci} 87062306a36Sopenharmony_ci 87162306a36Sopenharmony_ci#ifdef CONFIG_ARCH_ENABLE_THP_MIGRATION 87262306a36Sopenharmony_cistatic void __init pmd_swap_tests(struct pgtable_debug_args *args) 87362306a36Sopenharmony_ci{ 87462306a36Sopenharmony_ci swp_entry_t swp; 87562306a36Sopenharmony_ci pmd_t pmd; 87662306a36Sopenharmony_ci 87762306a36Sopenharmony_ci if (!has_transparent_hugepage()) 87862306a36Sopenharmony_ci return; 87962306a36Sopenharmony_ci 88062306a36Sopenharmony_ci pr_debug("Validating PMD swap\n"); 88162306a36Sopenharmony_ci pmd = pfn_pmd(args->fixed_pmd_pfn, args->page_prot); 88262306a36Sopenharmony_ci swp = __pmd_to_swp_entry(pmd); 88362306a36Sopenharmony_ci pmd = __swp_entry_to_pmd(swp); 88462306a36Sopenharmony_ci WARN_ON(args->fixed_pmd_pfn != pmd_pfn(pmd)); 88562306a36Sopenharmony_ci} 88662306a36Sopenharmony_ci#else /* !CONFIG_ARCH_ENABLE_THP_MIGRATION */ 88762306a36Sopenharmony_cistatic void __init pmd_swap_tests(struct pgtable_debug_args *args) { } 88862306a36Sopenharmony_ci#endif /* CONFIG_ARCH_ENABLE_THP_MIGRATION */ 88962306a36Sopenharmony_ci 89062306a36Sopenharmony_cistatic void __init swap_migration_tests(struct pgtable_debug_args *args) 89162306a36Sopenharmony_ci{ 89262306a36Sopenharmony_ci struct page *page; 89362306a36Sopenharmony_ci swp_entry_t swp; 89462306a36Sopenharmony_ci 89562306a36Sopenharmony_ci if (!IS_ENABLED(CONFIG_MIGRATION)) 89662306a36Sopenharmony_ci return; 89762306a36Sopenharmony_ci 89862306a36Sopenharmony_ci /* 89962306a36Sopenharmony_ci * swap_migration_tests() requires a dedicated page as it needs to 90062306a36Sopenharmony_ci * be locked before creating a migration entry from it. Locking the 90162306a36Sopenharmony_ci * page that actually maps kernel text ('start_kernel') can be real 90262306a36Sopenharmony_ci * problematic. Lets use the allocated page explicitly for this 90362306a36Sopenharmony_ci * purpose. 90462306a36Sopenharmony_ci */ 90562306a36Sopenharmony_ci page = (args->pte_pfn != ULONG_MAX) ? pfn_to_page(args->pte_pfn) : NULL; 90662306a36Sopenharmony_ci if (!page) 90762306a36Sopenharmony_ci return; 90862306a36Sopenharmony_ci 90962306a36Sopenharmony_ci pr_debug("Validating swap migration\n"); 91062306a36Sopenharmony_ci 91162306a36Sopenharmony_ci /* 91262306a36Sopenharmony_ci * make_[readable|writable]_migration_entry() expects given page to 91362306a36Sopenharmony_ci * be locked, otherwise it stumbles upon a BUG_ON(). 91462306a36Sopenharmony_ci */ 91562306a36Sopenharmony_ci __SetPageLocked(page); 91662306a36Sopenharmony_ci swp = make_writable_migration_entry(page_to_pfn(page)); 91762306a36Sopenharmony_ci WARN_ON(!is_migration_entry(swp)); 91862306a36Sopenharmony_ci WARN_ON(!is_writable_migration_entry(swp)); 91962306a36Sopenharmony_ci 92062306a36Sopenharmony_ci swp = make_readable_migration_entry(swp_offset(swp)); 92162306a36Sopenharmony_ci WARN_ON(!is_migration_entry(swp)); 92262306a36Sopenharmony_ci WARN_ON(is_writable_migration_entry(swp)); 92362306a36Sopenharmony_ci 92462306a36Sopenharmony_ci swp = make_readable_migration_entry(page_to_pfn(page)); 92562306a36Sopenharmony_ci WARN_ON(!is_migration_entry(swp)); 92662306a36Sopenharmony_ci WARN_ON(is_writable_migration_entry(swp)); 92762306a36Sopenharmony_ci __ClearPageLocked(page); 92862306a36Sopenharmony_ci} 92962306a36Sopenharmony_ci 93062306a36Sopenharmony_ci#ifdef CONFIG_HUGETLB_PAGE 93162306a36Sopenharmony_cistatic void __init hugetlb_basic_tests(struct pgtable_debug_args *args) 93262306a36Sopenharmony_ci{ 93362306a36Sopenharmony_ci struct page *page; 93462306a36Sopenharmony_ci pte_t pte; 93562306a36Sopenharmony_ci 93662306a36Sopenharmony_ci pr_debug("Validating HugeTLB basic\n"); 93762306a36Sopenharmony_ci /* 93862306a36Sopenharmony_ci * Accessing the page associated with the pfn is safe here, 93962306a36Sopenharmony_ci * as it was previously derived from a real kernel symbol. 94062306a36Sopenharmony_ci */ 94162306a36Sopenharmony_ci page = pfn_to_page(args->fixed_pmd_pfn); 94262306a36Sopenharmony_ci pte = mk_huge_pte(page, args->page_prot); 94362306a36Sopenharmony_ci 94462306a36Sopenharmony_ci WARN_ON(!huge_pte_dirty(huge_pte_mkdirty(pte))); 94562306a36Sopenharmony_ci WARN_ON(!huge_pte_write(huge_pte_mkwrite(huge_pte_wrprotect(pte)))); 94662306a36Sopenharmony_ci WARN_ON(huge_pte_write(huge_pte_wrprotect(huge_pte_mkwrite(pte)))); 94762306a36Sopenharmony_ci 94862306a36Sopenharmony_ci#ifdef CONFIG_ARCH_WANT_GENERAL_HUGETLB 94962306a36Sopenharmony_ci pte = pfn_pte(args->fixed_pmd_pfn, args->page_prot); 95062306a36Sopenharmony_ci 95162306a36Sopenharmony_ci WARN_ON(!pte_huge(arch_make_huge_pte(pte, PMD_SHIFT, VM_ACCESS_FLAGS))); 95262306a36Sopenharmony_ci#endif /* CONFIG_ARCH_WANT_GENERAL_HUGETLB */ 95362306a36Sopenharmony_ci} 95462306a36Sopenharmony_ci#else /* !CONFIG_HUGETLB_PAGE */ 95562306a36Sopenharmony_cistatic void __init hugetlb_basic_tests(struct pgtable_debug_args *args) { } 95662306a36Sopenharmony_ci#endif /* CONFIG_HUGETLB_PAGE */ 95762306a36Sopenharmony_ci 95862306a36Sopenharmony_ci#ifdef CONFIG_TRANSPARENT_HUGEPAGE 95962306a36Sopenharmony_cistatic void __init pmd_thp_tests(struct pgtable_debug_args *args) 96062306a36Sopenharmony_ci{ 96162306a36Sopenharmony_ci pmd_t pmd; 96262306a36Sopenharmony_ci 96362306a36Sopenharmony_ci if (!has_transparent_hugepage()) 96462306a36Sopenharmony_ci return; 96562306a36Sopenharmony_ci 96662306a36Sopenharmony_ci pr_debug("Validating PMD based THP\n"); 96762306a36Sopenharmony_ci /* 96862306a36Sopenharmony_ci * pmd_trans_huge() and pmd_present() must return positive after 96962306a36Sopenharmony_ci * MMU invalidation with pmd_mkinvalid(). This behavior is an 97062306a36Sopenharmony_ci * optimization for transparent huge page. pmd_trans_huge() must 97162306a36Sopenharmony_ci * be true if pmd_page() returns a valid THP to avoid taking the 97262306a36Sopenharmony_ci * pmd_lock when others walk over non transhuge pmds (i.e. there 97362306a36Sopenharmony_ci * are no THP allocated). Especially when splitting a THP and 97462306a36Sopenharmony_ci * removing the present bit from the pmd, pmd_trans_huge() still 97562306a36Sopenharmony_ci * needs to return true. pmd_present() should be true whenever 97662306a36Sopenharmony_ci * pmd_trans_huge() returns true. 97762306a36Sopenharmony_ci */ 97862306a36Sopenharmony_ci pmd = pfn_pmd(args->fixed_pmd_pfn, args->page_prot); 97962306a36Sopenharmony_ci WARN_ON(!pmd_trans_huge(pmd_mkhuge(pmd))); 98062306a36Sopenharmony_ci 98162306a36Sopenharmony_ci#ifndef __HAVE_ARCH_PMDP_INVALIDATE 98262306a36Sopenharmony_ci WARN_ON(!pmd_trans_huge(pmd_mkinvalid(pmd_mkhuge(pmd)))); 98362306a36Sopenharmony_ci WARN_ON(!pmd_present(pmd_mkinvalid(pmd_mkhuge(pmd)))); 98462306a36Sopenharmony_ci#endif /* __HAVE_ARCH_PMDP_INVALIDATE */ 98562306a36Sopenharmony_ci} 98662306a36Sopenharmony_ci 98762306a36Sopenharmony_ci#ifdef CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD 98862306a36Sopenharmony_cistatic void __init pud_thp_tests(struct pgtable_debug_args *args) 98962306a36Sopenharmony_ci{ 99062306a36Sopenharmony_ci pud_t pud; 99162306a36Sopenharmony_ci 99262306a36Sopenharmony_ci if (!has_transparent_pud_hugepage()) 99362306a36Sopenharmony_ci return; 99462306a36Sopenharmony_ci 99562306a36Sopenharmony_ci pr_debug("Validating PUD based THP\n"); 99662306a36Sopenharmony_ci pud = pfn_pud(args->fixed_pud_pfn, args->page_prot); 99762306a36Sopenharmony_ci WARN_ON(!pud_trans_huge(pud_mkhuge(pud))); 99862306a36Sopenharmony_ci 99962306a36Sopenharmony_ci /* 100062306a36Sopenharmony_ci * pud_mkinvalid() has been dropped for now. Enable back 100162306a36Sopenharmony_ci * these tests when it comes back with a modified pud_present(). 100262306a36Sopenharmony_ci * 100362306a36Sopenharmony_ci * WARN_ON(!pud_trans_huge(pud_mkinvalid(pud_mkhuge(pud)))); 100462306a36Sopenharmony_ci * WARN_ON(!pud_present(pud_mkinvalid(pud_mkhuge(pud)))); 100562306a36Sopenharmony_ci */ 100662306a36Sopenharmony_ci} 100762306a36Sopenharmony_ci#else /* !CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD */ 100862306a36Sopenharmony_cistatic void __init pud_thp_tests(struct pgtable_debug_args *args) { } 100962306a36Sopenharmony_ci#endif /* CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD */ 101062306a36Sopenharmony_ci#else /* !CONFIG_TRANSPARENT_HUGEPAGE */ 101162306a36Sopenharmony_cistatic void __init pmd_thp_tests(struct pgtable_debug_args *args) { } 101262306a36Sopenharmony_cistatic void __init pud_thp_tests(struct pgtable_debug_args *args) { } 101362306a36Sopenharmony_ci#endif /* CONFIG_TRANSPARENT_HUGEPAGE */ 101462306a36Sopenharmony_ci 101562306a36Sopenharmony_cistatic unsigned long __init get_random_vaddr(void) 101662306a36Sopenharmony_ci{ 101762306a36Sopenharmony_ci unsigned long random_vaddr, random_pages, total_user_pages; 101862306a36Sopenharmony_ci 101962306a36Sopenharmony_ci total_user_pages = (TASK_SIZE - FIRST_USER_ADDRESS) / PAGE_SIZE; 102062306a36Sopenharmony_ci 102162306a36Sopenharmony_ci random_pages = get_random_long() % total_user_pages; 102262306a36Sopenharmony_ci random_vaddr = FIRST_USER_ADDRESS + random_pages * PAGE_SIZE; 102362306a36Sopenharmony_ci 102462306a36Sopenharmony_ci return random_vaddr; 102562306a36Sopenharmony_ci} 102662306a36Sopenharmony_ci 102762306a36Sopenharmony_cistatic void __init destroy_args(struct pgtable_debug_args *args) 102862306a36Sopenharmony_ci{ 102962306a36Sopenharmony_ci struct page *page = NULL; 103062306a36Sopenharmony_ci 103162306a36Sopenharmony_ci /* Free (huge) page */ 103262306a36Sopenharmony_ci if (IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE) && 103362306a36Sopenharmony_ci has_transparent_pud_hugepage() && 103462306a36Sopenharmony_ci args->pud_pfn != ULONG_MAX) { 103562306a36Sopenharmony_ci if (args->is_contiguous_page) { 103662306a36Sopenharmony_ci free_contig_range(args->pud_pfn, 103762306a36Sopenharmony_ci (1 << (HPAGE_PUD_SHIFT - PAGE_SHIFT))); 103862306a36Sopenharmony_ci } else { 103962306a36Sopenharmony_ci page = pfn_to_page(args->pud_pfn); 104062306a36Sopenharmony_ci __free_pages(page, HPAGE_PUD_SHIFT - PAGE_SHIFT); 104162306a36Sopenharmony_ci } 104262306a36Sopenharmony_ci 104362306a36Sopenharmony_ci args->pud_pfn = ULONG_MAX; 104462306a36Sopenharmony_ci args->pmd_pfn = ULONG_MAX; 104562306a36Sopenharmony_ci args->pte_pfn = ULONG_MAX; 104662306a36Sopenharmony_ci } 104762306a36Sopenharmony_ci 104862306a36Sopenharmony_ci if (IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE) && 104962306a36Sopenharmony_ci has_transparent_hugepage() && 105062306a36Sopenharmony_ci args->pmd_pfn != ULONG_MAX) { 105162306a36Sopenharmony_ci if (args->is_contiguous_page) { 105262306a36Sopenharmony_ci free_contig_range(args->pmd_pfn, (1 << HPAGE_PMD_ORDER)); 105362306a36Sopenharmony_ci } else { 105462306a36Sopenharmony_ci page = pfn_to_page(args->pmd_pfn); 105562306a36Sopenharmony_ci __free_pages(page, HPAGE_PMD_ORDER); 105662306a36Sopenharmony_ci } 105762306a36Sopenharmony_ci 105862306a36Sopenharmony_ci args->pmd_pfn = ULONG_MAX; 105962306a36Sopenharmony_ci args->pte_pfn = ULONG_MAX; 106062306a36Sopenharmony_ci } 106162306a36Sopenharmony_ci 106262306a36Sopenharmony_ci if (args->pte_pfn != ULONG_MAX) { 106362306a36Sopenharmony_ci page = pfn_to_page(args->pte_pfn); 106462306a36Sopenharmony_ci __free_page(page); 106562306a36Sopenharmony_ci 106662306a36Sopenharmony_ci args->pte_pfn = ULONG_MAX; 106762306a36Sopenharmony_ci } 106862306a36Sopenharmony_ci 106962306a36Sopenharmony_ci /* Free page table entries */ 107062306a36Sopenharmony_ci if (args->start_ptep) { 107162306a36Sopenharmony_ci pte_free(args->mm, args->start_ptep); 107262306a36Sopenharmony_ci mm_dec_nr_ptes(args->mm); 107362306a36Sopenharmony_ci } 107462306a36Sopenharmony_ci 107562306a36Sopenharmony_ci if (args->start_pmdp) { 107662306a36Sopenharmony_ci pmd_free(args->mm, args->start_pmdp); 107762306a36Sopenharmony_ci mm_dec_nr_pmds(args->mm); 107862306a36Sopenharmony_ci } 107962306a36Sopenharmony_ci 108062306a36Sopenharmony_ci if (args->start_pudp) { 108162306a36Sopenharmony_ci pud_free(args->mm, args->start_pudp); 108262306a36Sopenharmony_ci mm_dec_nr_puds(args->mm); 108362306a36Sopenharmony_ci } 108462306a36Sopenharmony_ci 108562306a36Sopenharmony_ci if (args->start_p4dp) 108662306a36Sopenharmony_ci p4d_free(args->mm, args->start_p4dp); 108762306a36Sopenharmony_ci 108862306a36Sopenharmony_ci /* Free vma and mm struct */ 108962306a36Sopenharmony_ci if (args->vma) 109062306a36Sopenharmony_ci vm_area_free(args->vma); 109162306a36Sopenharmony_ci 109262306a36Sopenharmony_ci if (args->mm) 109362306a36Sopenharmony_ci mmdrop(args->mm); 109462306a36Sopenharmony_ci} 109562306a36Sopenharmony_ci 109662306a36Sopenharmony_cistatic struct page * __init 109762306a36Sopenharmony_cidebug_vm_pgtable_alloc_huge_page(struct pgtable_debug_args *args, int order) 109862306a36Sopenharmony_ci{ 109962306a36Sopenharmony_ci struct page *page = NULL; 110062306a36Sopenharmony_ci 110162306a36Sopenharmony_ci#ifdef CONFIG_CONTIG_ALLOC 110262306a36Sopenharmony_ci if (order > MAX_ORDER) { 110362306a36Sopenharmony_ci page = alloc_contig_pages((1 << order), GFP_KERNEL, 110462306a36Sopenharmony_ci first_online_node, NULL); 110562306a36Sopenharmony_ci if (page) { 110662306a36Sopenharmony_ci args->is_contiguous_page = true; 110762306a36Sopenharmony_ci return page; 110862306a36Sopenharmony_ci } 110962306a36Sopenharmony_ci } 111062306a36Sopenharmony_ci#endif 111162306a36Sopenharmony_ci 111262306a36Sopenharmony_ci if (order <= MAX_ORDER) 111362306a36Sopenharmony_ci page = alloc_pages(GFP_KERNEL, order); 111462306a36Sopenharmony_ci 111562306a36Sopenharmony_ci return page; 111662306a36Sopenharmony_ci} 111762306a36Sopenharmony_ci 111862306a36Sopenharmony_ci/* 111962306a36Sopenharmony_ci * Check if a physical memory range described by <pstart, pend> contains 112062306a36Sopenharmony_ci * an area that is of size psize, and aligned to psize. 112162306a36Sopenharmony_ci * 112262306a36Sopenharmony_ci * Don't use address 0, an all-zeroes physical address might mask bugs, and 112362306a36Sopenharmony_ci * it's not used on x86. 112462306a36Sopenharmony_ci */ 112562306a36Sopenharmony_cistatic void __init phys_align_check(phys_addr_t pstart, 112662306a36Sopenharmony_ci phys_addr_t pend, unsigned long psize, 112762306a36Sopenharmony_ci phys_addr_t *physp, unsigned long *alignp) 112862306a36Sopenharmony_ci{ 112962306a36Sopenharmony_ci phys_addr_t aligned_start, aligned_end; 113062306a36Sopenharmony_ci 113162306a36Sopenharmony_ci if (pstart == 0) 113262306a36Sopenharmony_ci pstart = PAGE_SIZE; 113362306a36Sopenharmony_ci 113462306a36Sopenharmony_ci aligned_start = ALIGN(pstart, psize); 113562306a36Sopenharmony_ci aligned_end = aligned_start + psize; 113662306a36Sopenharmony_ci 113762306a36Sopenharmony_ci if (aligned_end > aligned_start && aligned_end <= pend) { 113862306a36Sopenharmony_ci *alignp = psize; 113962306a36Sopenharmony_ci *physp = aligned_start; 114062306a36Sopenharmony_ci } 114162306a36Sopenharmony_ci} 114262306a36Sopenharmony_ci 114362306a36Sopenharmony_cistatic void __init init_fixed_pfns(struct pgtable_debug_args *args) 114462306a36Sopenharmony_ci{ 114562306a36Sopenharmony_ci u64 idx; 114662306a36Sopenharmony_ci phys_addr_t phys, pstart, pend; 114762306a36Sopenharmony_ci 114862306a36Sopenharmony_ci /* 114962306a36Sopenharmony_ci * Initialize the fixed pfns. To do this, try to find a 115062306a36Sopenharmony_ci * valid physical range, preferably aligned to PUD_SIZE, 115162306a36Sopenharmony_ci * but settling for aligned to PMD_SIZE as a fallback. If 115262306a36Sopenharmony_ci * neither of those is found, use the physical address of 115362306a36Sopenharmony_ci * the start_kernel symbol. 115462306a36Sopenharmony_ci * 115562306a36Sopenharmony_ci * The memory doesn't need to be allocated, it just needs to exist 115662306a36Sopenharmony_ci * as usable memory. It won't be touched. 115762306a36Sopenharmony_ci * 115862306a36Sopenharmony_ci * The alignment is recorded, and can be checked to see if we 115962306a36Sopenharmony_ci * can run the tests that require an actual valid physical 116062306a36Sopenharmony_ci * address range on some architectures ({pmd,pud}_huge_test 116162306a36Sopenharmony_ci * on x86). 116262306a36Sopenharmony_ci */ 116362306a36Sopenharmony_ci 116462306a36Sopenharmony_ci phys = __pa_symbol(&start_kernel); 116562306a36Sopenharmony_ci args->fixed_alignment = PAGE_SIZE; 116662306a36Sopenharmony_ci 116762306a36Sopenharmony_ci for_each_mem_range(idx, &pstart, &pend) { 116862306a36Sopenharmony_ci /* First check for a PUD-aligned area */ 116962306a36Sopenharmony_ci phys_align_check(pstart, pend, PUD_SIZE, &phys, 117062306a36Sopenharmony_ci &args->fixed_alignment); 117162306a36Sopenharmony_ci 117262306a36Sopenharmony_ci /* If a PUD-aligned area is found, we're done */ 117362306a36Sopenharmony_ci if (args->fixed_alignment == PUD_SIZE) 117462306a36Sopenharmony_ci break; 117562306a36Sopenharmony_ci 117662306a36Sopenharmony_ci /* 117762306a36Sopenharmony_ci * If no PMD-aligned area found yet, check for one, 117862306a36Sopenharmony_ci * but continue the loop to look for a PUD-aligned area. 117962306a36Sopenharmony_ci */ 118062306a36Sopenharmony_ci if (args->fixed_alignment < PMD_SIZE) 118162306a36Sopenharmony_ci phys_align_check(pstart, pend, PMD_SIZE, &phys, 118262306a36Sopenharmony_ci &args->fixed_alignment); 118362306a36Sopenharmony_ci } 118462306a36Sopenharmony_ci 118562306a36Sopenharmony_ci args->fixed_pgd_pfn = __phys_to_pfn(phys & PGDIR_MASK); 118662306a36Sopenharmony_ci args->fixed_p4d_pfn = __phys_to_pfn(phys & P4D_MASK); 118762306a36Sopenharmony_ci args->fixed_pud_pfn = __phys_to_pfn(phys & PUD_MASK); 118862306a36Sopenharmony_ci args->fixed_pmd_pfn = __phys_to_pfn(phys & PMD_MASK); 118962306a36Sopenharmony_ci args->fixed_pte_pfn = __phys_to_pfn(phys & PAGE_MASK); 119062306a36Sopenharmony_ci WARN_ON(!pfn_valid(args->fixed_pte_pfn)); 119162306a36Sopenharmony_ci} 119262306a36Sopenharmony_ci 119362306a36Sopenharmony_ci 119462306a36Sopenharmony_cistatic int __init init_args(struct pgtable_debug_args *args) 119562306a36Sopenharmony_ci{ 119662306a36Sopenharmony_ci struct page *page = NULL; 119762306a36Sopenharmony_ci int ret = 0; 119862306a36Sopenharmony_ci 119962306a36Sopenharmony_ci /* 120062306a36Sopenharmony_ci * Initialize the debugging data. 120162306a36Sopenharmony_ci * 120262306a36Sopenharmony_ci * vm_get_page_prot(VM_NONE) or vm_get_page_prot(VM_SHARED|VM_NONE) 120362306a36Sopenharmony_ci * will help create page table entries with PROT_NONE permission as 120462306a36Sopenharmony_ci * required for pxx_protnone_tests(). 120562306a36Sopenharmony_ci */ 120662306a36Sopenharmony_ci memset(args, 0, sizeof(*args)); 120762306a36Sopenharmony_ci args->vaddr = get_random_vaddr(); 120862306a36Sopenharmony_ci args->page_prot = vm_get_page_prot(VM_ACCESS_FLAGS); 120962306a36Sopenharmony_ci args->page_prot_none = vm_get_page_prot(VM_NONE); 121062306a36Sopenharmony_ci args->is_contiguous_page = false; 121162306a36Sopenharmony_ci args->pud_pfn = ULONG_MAX; 121262306a36Sopenharmony_ci args->pmd_pfn = ULONG_MAX; 121362306a36Sopenharmony_ci args->pte_pfn = ULONG_MAX; 121462306a36Sopenharmony_ci args->fixed_pgd_pfn = ULONG_MAX; 121562306a36Sopenharmony_ci args->fixed_p4d_pfn = ULONG_MAX; 121662306a36Sopenharmony_ci args->fixed_pud_pfn = ULONG_MAX; 121762306a36Sopenharmony_ci args->fixed_pmd_pfn = ULONG_MAX; 121862306a36Sopenharmony_ci args->fixed_pte_pfn = ULONG_MAX; 121962306a36Sopenharmony_ci 122062306a36Sopenharmony_ci /* Allocate mm and vma */ 122162306a36Sopenharmony_ci args->mm = mm_alloc(); 122262306a36Sopenharmony_ci if (!args->mm) { 122362306a36Sopenharmony_ci pr_err("Failed to allocate mm struct\n"); 122462306a36Sopenharmony_ci ret = -ENOMEM; 122562306a36Sopenharmony_ci goto error; 122662306a36Sopenharmony_ci } 122762306a36Sopenharmony_ci 122862306a36Sopenharmony_ci args->vma = vm_area_alloc(args->mm); 122962306a36Sopenharmony_ci if (!args->vma) { 123062306a36Sopenharmony_ci pr_err("Failed to allocate vma\n"); 123162306a36Sopenharmony_ci ret = -ENOMEM; 123262306a36Sopenharmony_ci goto error; 123362306a36Sopenharmony_ci } 123462306a36Sopenharmony_ci 123562306a36Sopenharmony_ci /* 123662306a36Sopenharmony_ci * Allocate page table entries. They will be modified in the tests. 123762306a36Sopenharmony_ci * Lets save the page table entries so that they can be released 123862306a36Sopenharmony_ci * when the tests are completed. 123962306a36Sopenharmony_ci */ 124062306a36Sopenharmony_ci args->pgdp = pgd_offset(args->mm, args->vaddr); 124162306a36Sopenharmony_ci args->p4dp = p4d_alloc(args->mm, args->pgdp, args->vaddr); 124262306a36Sopenharmony_ci if (!args->p4dp) { 124362306a36Sopenharmony_ci pr_err("Failed to allocate p4d entries\n"); 124462306a36Sopenharmony_ci ret = -ENOMEM; 124562306a36Sopenharmony_ci goto error; 124662306a36Sopenharmony_ci } 124762306a36Sopenharmony_ci args->start_p4dp = p4d_offset(args->pgdp, 0UL); 124862306a36Sopenharmony_ci WARN_ON(!args->start_p4dp); 124962306a36Sopenharmony_ci 125062306a36Sopenharmony_ci args->pudp = pud_alloc(args->mm, args->p4dp, args->vaddr); 125162306a36Sopenharmony_ci if (!args->pudp) { 125262306a36Sopenharmony_ci pr_err("Failed to allocate pud entries\n"); 125362306a36Sopenharmony_ci ret = -ENOMEM; 125462306a36Sopenharmony_ci goto error; 125562306a36Sopenharmony_ci } 125662306a36Sopenharmony_ci args->start_pudp = pud_offset(args->p4dp, 0UL); 125762306a36Sopenharmony_ci WARN_ON(!args->start_pudp); 125862306a36Sopenharmony_ci 125962306a36Sopenharmony_ci args->pmdp = pmd_alloc(args->mm, args->pudp, args->vaddr); 126062306a36Sopenharmony_ci if (!args->pmdp) { 126162306a36Sopenharmony_ci pr_err("Failed to allocate pmd entries\n"); 126262306a36Sopenharmony_ci ret = -ENOMEM; 126362306a36Sopenharmony_ci goto error; 126462306a36Sopenharmony_ci } 126562306a36Sopenharmony_ci args->start_pmdp = pmd_offset(args->pudp, 0UL); 126662306a36Sopenharmony_ci WARN_ON(!args->start_pmdp); 126762306a36Sopenharmony_ci 126862306a36Sopenharmony_ci if (pte_alloc(args->mm, args->pmdp)) { 126962306a36Sopenharmony_ci pr_err("Failed to allocate pte entries\n"); 127062306a36Sopenharmony_ci ret = -ENOMEM; 127162306a36Sopenharmony_ci goto error; 127262306a36Sopenharmony_ci } 127362306a36Sopenharmony_ci args->start_ptep = pmd_pgtable(READ_ONCE(*args->pmdp)); 127462306a36Sopenharmony_ci WARN_ON(!args->start_ptep); 127562306a36Sopenharmony_ci 127662306a36Sopenharmony_ci init_fixed_pfns(args); 127762306a36Sopenharmony_ci 127862306a36Sopenharmony_ci /* 127962306a36Sopenharmony_ci * Allocate (huge) pages because some of the tests need to access 128062306a36Sopenharmony_ci * the data in the pages. The corresponding tests will be skipped 128162306a36Sopenharmony_ci * if we fail to allocate (huge) pages. 128262306a36Sopenharmony_ci */ 128362306a36Sopenharmony_ci if (IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE) && 128462306a36Sopenharmony_ci has_transparent_pud_hugepage()) { 128562306a36Sopenharmony_ci page = debug_vm_pgtable_alloc_huge_page(args, 128662306a36Sopenharmony_ci HPAGE_PUD_SHIFT - PAGE_SHIFT); 128762306a36Sopenharmony_ci if (page) { 128862306a36Sopenharmony_ci args->pud_pfn = page_to_pfn(page); 128962306a36Sopenharmony_ci args->pmd_pfn = args->pud_pfn; 129062306a36Sopenharmony_ci args->pte_pfn = args->pud_pfn; 129162306a36Sopenharmony_ci return 0; 129262306a36Sopenharmony_ci } 129362306a36Sopenharmony_ci } 129462306a36Sopenharmony_ci 129562306a36Sopenharmony_ci if (IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE) && 129662306a36Sopenharmony_ci has_transparent_hugepage()) { 129762306a36Sopenharmony_ci page = debug_vm_pgtable_alloc_huge_page(args, HPAGE_PMD_ORDER); 129862306a36Sopenharmony_ci if (page) { 129962306a36Sopenharmony_ci args->pmd_pfn = page_to_pfn(page); 130062306a36Sopenharmony_ci args->pte_pfn = args->pmd_pfn; 130162306a36Sopenharmony_ci return 0; 130262306a36Sopenharmony_ci } 130362306a36Sopenharmony_ci } 130462306a36Sopenharmony_ci 130562306a36Sopenharmony_ci page = alloc_page(GFP_KERNEL); 130662306a36Sopenharmony_ci if (page) 130762306a36Sopenharmony_ci args->pte_pfn = page_to_pfn(page); 130862306a36Sopenharmony_ci 130962306a36Sopenharmony_ci return 0; 131062306a36Sopenharmony_ci 131162306a36Sopenharmony_cierror: 131262306a36Sopenharmony_ci destroy_args(args); 131362306a36Sopenharmony_ci return ret; 131462306a36Sopenharmony_ci} 131562306a36Sopenharmony_ci 131662306a36Sopenharmony_cistatic int __init debug_vm_pgtable(void) 131762306a36Sopenharmony_ci{ 131862306a36Sopenharmony_ci struct pgtable_debug_args args; 131962306a36Sopenharmony_ci spinlock_t *ptl = NULL; 132062306a36Sopenharmony_ci int idx, ret; 132162306a36Sopenharmony_ci 132262306a36Sopenharmony_ci pr_info("Validating architecture page table helpers\n"); 132362306a36Sopenharmony_ci ret = init_args(&args); 132462306a36Sopenharmony_ci if (ret) 132562306a36Sopenharmony_ci return ret; 132662306a36Sopenharmony_ci 132762306a36Sopenharmony_ci /* 132862306a36Sopenharmony_ci * Iterate over each possible vm_flags to make sure that all 132962306a36Sopenharmony_ci * the basic page table transformation validations just hold 133062306a36Sopenharmony_ci * true irrespective of the starting protection value for a 133162306a36Sopenharmony_ci * given page table entry. 133262306a36Sopenharmony_ci * 133362306a36Sopenharmony_ci * Protection based vm_flags combinatins are always linear 133462306a36Sopenharmony_ci * and increasing i.e starting from VM_NONE and going upto 133562306a36Sopenharmony_ci * (VM_SHARED | READ | WRITE | EXEC). 133662306a36Sopenharmony_ci */ 133762306a36Sopenharmony_ci#define VM_FLAGS_START (VM_NONE) 133862306a36Sopenharmony_ci#define VM_FLAGS_END (VM_SHARED | VM_EXEC | VM_WRITE | VM_READ) 133962306a36Sopenharmony_ci 134062306a36Sopenharmony_ci for (idx = VM_FLAGS_START; idx <= VM_FLAGS_END; idx++) { 134162306a36Sopenharmony_ci pte_basic_tests(&args, idx); 134262306a36Sopenharmony_ci pmd_basic_tests(&args, idx); 134362306a36Sopenharmony_ci pud_basic_tests(&args, idx); 134462306a36Sopenharmony_ci } 134562306a36Sopenharmony_ci 134662306a36Sopenharmony_ci /* 134762306a36Sopenharmony_ci * Both P4D and PGD level tests are very basic which do not 134862306a36Sopenharmony_ci * involve creating page table entries from the protection 134962306a36Sopenharmony_ci * value and the given pfn. Hence just keep them out from 135062306a36Sopenharmony_ci * the above iteration for now to save some test execution 135162306a36Sopenharmony_ci * time. 135262306a36Sopenharmony_ci */ 135362306a36Sopenharmony_ci p4d_basic_tests(&args); 135462306a36Sopenharmony_ci pgd_basic_tests(&args); 135562306a36Sopenharmony_ci 135662306a36Sopenharmony_ci pmd_leaf_tests(&args); 135762306a36Sopenharmony_ci pud_leaf_tests(&args); 135862306a36Sopenharmony_ci 135962306a36Sopenharmony_ci pte_special_tests(&args); 136062306a36Sopenharmony_ci pte_protnone_tests(&args); 136162306a36Sopenharmony_ci pmd_protnone_tests(&args); 136262306a36Sopenharmony_ci 136362306a36Sopenharmony_ci pte_devmap_tests(&args); 136462306a36Sopenharmony_ci pmd_devmap_tests(&args); 136562306a36Sopenharmony_ci pud_devmap_tests(&args); 136662306a36Sopenharmony_ci 136762306a36Sopenharmony_ci pte_soft_dirty_tests(&args); 136862306a36Sopenharmony_ci pmd_soft_dirty_tests(&args); 136962306a36Sopenharmony_ci pte_swap_soft_dirty_tests(&args); 137062306a36Sopenharmony_ci pmd_swap_soft_dirty_tests(&args); 137162306a36Sopenharmony_ci 137262306a36Sopenharmony_ci pte_swap_exclusive_tests(&args); 137362306a36Sopenharmony_ci 137462306a36Sopenharmony_ci pte_swap_tests(&args); 137562306a36Sopenharmony_ci pmd_swap_tests(&args); 137662306a36Sopenharmony_ci 137762306a36Sopenharmony_ci swap_migration_tests(&args); 137862306a36Sopenharmony_ci 137962306a36Sopenharmony_ci pmd_thp_tests(&args); 138062306a36Sopenharmony_ci pud_thp_tests(&args); 138162306a36Sopenharmony_ci 138262306a36Sopenharmony_ci hugetlb_basic_tests(&args); 138362306a36Sopenharmony_ci 138462306a36Sopenharmony_ci /* 138562306a36Sopenharmony_ci * Page table modifying tests. They need to hold 138662306a36Sopenharmony_ci * proper page table lock. 138762306a36Sopenharmony_ci */ 138862306a36Sopenharmony_ci 138962306a36Sopenharmony_ci args.ptep = pte_offset_map_lock(args.mm, args.pmdp, args.vaddr, &ptl); 139062306a36Sopenharmony_ci pte_clear_tests(&args); 139162306a36Sopenharmony_ci pte_advanced_tests(&args); 139262306a36Sopenharmony_ci if (args.ptep) 139362306a36Sopenharmony_ci pte_unmap_unlock(args.ptep, ptl); 139462306a36Sopenharmony_ci 139562306a36Sopenharmony_ci ptl = pmd_lock(args.mm, args.pmdp); 139662306a36Sopenharmony_ci pmd_clear_tests(&args); 139762306a36Sopenharmony_ci pmd_advanced_tests(&args); 139862306a36Sopenharmony_ci pmd_huge_tests(&args); 139962306a36Sopenharmony_ci pmd_populate_tests(&args); 140062306a36Sopenharmony_ci spin_unlock(ptl); 140162306a36Sopenharmony_ci 140262306a36Sopenharmony_ci ptl = pud_lock(args.mm, args.pudp); 140362306a36Sopenharmony_ci pud_clear_tests(&args); 140462306a36Sopenharmony_ci pud_advanced_tests(&args); 140562306a36Sopenharmony_ci pud_huge_tests(&args); 140662306a36Sopenharmony_ci pud_populate_tests(&args); 140762306a36Sopenharmony_ci spin_unlock(ptl); 140862306a36Sopenharmony_ci 140962306a36Sopenharmony_ci spin_lock(&(args.mm->page_table_lock)); 141062306a36Sopenharmony_ci p4d_clear_tests(&args); 141162306a36Sopenharmony_ci pgd_clear_tests(&args); 141262306a36Sopenharmony_ci p4d_populate_tests(&args); 141362306a36Sopenharmony_ci pgd_populate_tests(&args); 141462306a36Sopenharmony_ci spin_unlock(&(args.mm->page_table_lock)); 141562306a36Sopenharmony_ci 141662306a36Sopenharmony_ci destroy_args(&args); 141762306a36Sopenharmony_ci return 0; 141862306a36Sopenharmony_ci} 141962306a36Sopenharmony_cilate_initcall(debug_vm_pgtable); 1420