18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * This kernel test validates architecture page table helpers and
48c2ecf20Sopenharmony_ci * accessors and helps in verifying their continued compliance with
58c2ecf20Sopenharmony_ci * expected generic MM semantics.
68c2ecf20Sopenharmony_ci *
78c2ecf20Sopenharmony_ci * Copyright (C) 2019 ARM Ltd.
88c2ecf20Sopenharmony_ci *
98c2ecf20Sopenharmony_ci * Author: Anshuman Khandual <anshuman.khandual@arm.com>
108c2ecf20Sopenharmony_ci */
118c2ecf20Sopenharmony_ci#define pr_fmt(fmt) "debug_vm_pgtable: [%-25s]: " fmt, __func__
128c2ecf20Sopenharmony_ci
138c2ecf20Sopenharmony_ci#include <linux/gfp.h>
148c2ecf20Sopenharmony_ci#include <linux/highmem.h>
158c2ecf20Sopenharmony_ci#include <linux/hugetlb.h>
168c2ecf20Sopenharmony_ci#include <linux/kernel.h>
178c2ecf20Sopenharmony_ci#include <linux/kconfig.h>
188c2ecf20Sopenharmony_ci#include <linux/mm.h>
198c2ecf20Sopenharmony_ci#include <linux/mman.h>
208c2ecf20Sopenharmony_ci#include <linux/mm_types.h>
218c2ecf20Sopenharmony_ci#include <linux/module.h>
228c2ecf20Sopenharmony_ci#include <linux/pfn_t.h>
238c2ecf20Sopenharmony_ci#include <linux/printk.h>
248c2ecf20Sopenharmony_ci#include <linux/pgtable.h>
258c2ecf20Sopenharmony_ci#include <linux/random.h>
268c2ecf20Sopenharmony_ci#include <linux/spinlock.h>
278c2ecf20Sopenharmony_ci#include <linux/swap.h>
288c2ecf20Sopenharmony_ci#include <linux/swapops.h>
298c2ecf20Sopenharmony_ci#include <linux/start_kernel.h>
308c2ecf20Sopenharmony_ci#include <linux/sched/mm.h>
318c2ecf20Sopenharmony_ci#include <linux/io.h>
328c2ecf20Sopenharmony_ci#include <asm/pgalloc.h>
338c2ecf20Sopenharmony_ci#include <asm/tlbflush.h>
348c2ecf20Sopenharmony_ci
358c2ecf20Sopenharmony_ci/*
368c2ecf20Sopenharmony_ci * Please refer Documentation/vm/arch_pgtable_helpers.rst for the semantics
378c2ecf20Sopenharmony_ci * expectations that are being validated here. All future changes in here
388c2ecf20Sopenharmony_ci * or the documentation need to be in sync.
398c2ecf20Sopenharmony_ci */
408c2ecf20Sopenharmony_ci
418c2ecf20Sopenharmony_ci#define VMFLAGS	(VM_READ|VM_WRITE|VM_EXEC)
428c2ecf20Sopenharmony_ci
438c2ecf20Sopenharmony_ci/*
448c2ecf20Sopenharmony_ci * On s390 platform, the lower 4 bits are used to identify given page table
458c2ecf20Sopenharmony_ci * entry type. But these bits might affect the ability to clear entries with
468c2ecf20Sopenharmony_ci * pxx_clear() because of how dynamic page table folding works on s390. So
478c2ecf20Sopenharmony_ci * while loading up the entries do not change the lower 4 bits. It does not
488c2ecf20Sopenharmony_ci * have affect any other platform. Also avoid the 62nd bit on ppc64 that is
498c2ecf20Sopenharmony_ci * used to mark a pte entry.
508c2ecf20Sopenharmony_ci */
518c2ecf20Sopenharmony_ci#define S390_SKIP_MASK		GENMASK(3, 0)
528c2ecf20Sopenharmony_ci#if __BITS_PER_LONG == 64
538c2ecf20Sopenharmony_ci#define PPC64_SKIP_MASK		GENMASK(62, 62)
548c2ecf20Sopenharmony_ci#else
558c2ecf20Sopenharmony_ci#define PPC64_SKIP_MASK		0x0
568c2ecf20Sopenharmony_ci#endif
578c2ecf20Sopenharmony_ci#define ARCH_SKIP_MASK (S390_SKIP_MASK | PPC64_SKIP_MASK)
588c2ecf20Sopenharmony_ci#define RANDOM_ORVALUE (GENMASK(BITS_PER_LONG - 1, 0) & ~ARCH_SKIP_MASK)
598c2ecf20Sopenharmony_ci#define RANDOM_NZVALUE	GENMASK(7, 0)
608c2ecf20Sopenharmony_ci
618c2ecf20Sopenharmony_cistatic void __init pte_basic_tests(unsigned long pfn, int idx)
628c2ecf20Sopenharmony_ci{
638c2ecf20Sopenharmony_ci	pgprot_t prot = protection_map[idx];
648c2ecf20Sopenharmony_ci	pte_t pte = pfn_pte(pfn, prot);
658c2ecf20Sopenharmony_ci	unsigned long val = idx, *ptr = &val;
668c2ecf20Sopenharmony_ci
678c2ecf20Sopenharmony_ci	pr_debug("Validating PTE basic (%pGv)\n", ptr);
688c2ecf20Sopenharmony_ci
698c2ecf20Sopenharmony_ci	/*
708c2ecf20Sopenharmony_ci	 * This test needs to be executed after the given page table entry
718c2ecf20Sopenharmony_ci	 * is created with pfn_pte() to make sure that protection_map[idx]
728c2ecf20Sopenharmony_ci	 * does not have the dirty bit enabled from the beginning. This is
738c2ecf20Sopenharmony_ci	 * important for platforms like arm64 where (!PTE_RDONLY) indicate
748c2ecf20Sopenharmony_ci	 * dirty bit being set.
758c2ecf20Sopenharmony_ci	 */
768c2ecf20Sopenharmony_ci	WARN_ON(pte_dirty(pte_wrprotect(pte)));
778c2ecf20Sopenharmony_ci
788c2ecf20Sopenharmony_ci	WARN_ON(!pte_same(pte, pte));
798c2ecf20Sopenharmony_ci	WARN_ON(!pte_young(pte_mkyoung(pte_mkold(pte))));
808c2ecf20Sopenharmony_ci	WARN_ON(!pte_dirty(pte_mkdirty(pte_mkclean(pte))));
818c2ecf20Sopenharmony_ci	WARN_ON(!pte_write(pte_mkwrite(pte_wrprotect(pte))));
828c2ecf20Sopenharmony_ci	WARN_ON(pte_young(pte_mkold(pte_mkyoung(pte))));
838c2ecf20Sopenharmony_ci	WARN_ON(pte_dirty(pte_mkclean(pte_mkdirty(pte))));
848c2ecf20Sopenharmony_ci	WARN_ON(pte_write(pte_wrprotect(pte_mkwrite(pte))));
858c2ecf20Sopenharmony_ci	WARN_ON(pte_dirty(pte_wrprotect(pte_mkclean(pte))));
868c2ecf20Sopenharmony_ci	WARN_ON(!pte_dirty(pte_wrprotect(pte_mkdirty(pte))));
878c2ecf20Sopenharmony_ci}
888c2ecf20Sopenharmony_ci
898c2ecf20Sopenharmony_cistatic void __init pte_advanced_tests(struct mm_struct *mm,
908c2ecf20Sopenharmony_ci				      struct vm_area_struct *vma, pte_t *ptep,
918c2ecf20Sopenharmony_ci				      unsigned long pfn, unsigned long vaddr,
928c2ecf20Sopenharmony_ci				      pgprot_t prot)
938c2ecf20Sopenharmony_ci{
948c2ecf20Sopenharmony_ci	pte_t pte = pfn_pte(pfn, prot);
958c2ecf20Sopenharmony_ci
968c2ecf20Sopenharmony_ci	/*
978c2ecf20Sopenharmony_ci	 * Architectures optimize set_pte_at by avoiding TLB flush.
988c2ecf20Sopenharmony_ci	 * This requires set_pte_at to be not used to update an
998c2ecf20Sopenharmony_ci	 * existing pte entry. Clear pte before we do set_pte_at
1008c2ecf20Sopenharmony_ci	 */
1018c2ecf20Sopenharmony_ci
1028c2ecf20Sopenharmony_ci	pr_debug("Validating PTE advanced\n");
1038c2ecf20Sopenharmony_ci	pte = pfn_pte(pfn, prot);
1048c2ecf20Sopenharmony_ci	set_pte_at(mm, vaddr, ptep, pte);
1058c2ecf20Sopenharmony_ci	ptep_set_wrprotect(mm, vaddr, ptep);
1068c2ecf20Sopenharmony_ci	pte = ptep_get(ptep);
1078c2ecf20Sopenharmony_ci	WARN_ON(pte_write(pte));
1088c2ecf20Sopenharmony_ci	ptep_get_and_clear(mm, vaddr, ptep);
1098c2ecf20Sopenharmony_ci	pte = ptep_get(ptep);
1108c2ecf20Sopenharmony_ci	WARN_ON(!pte_none(pte));
1118c2ecf20Sopenharmony_ci
1128c2ecf20Sopenharmony_ci	pte = pfn_pte(pfn, prot);
1138c2ecf20Sopenharmony_ci	pte = pte_wrprotect(pte);
1148c2ecf20Sopenharmony_ci	pte = pte_mkclean(pte);
1158c2ecf20Sopenharmony_ci	set_pte_at(mm, vaddr, ptep, pte);
1168c2ecf20Sopenharmony_ci	pte = pte_mkwrite(pte);
1178c2ecf20Sopenharmony_ci	pte = pte_mkdirty(pte);
1188c2ecf20Sopenharmony_ci	ptep_set_access_flags(vma, vaddr, ptep, pte, 1);
1198c2ecf20Sopenharmony_ci	pte = ptep_get(ptep);
1208c2ecf20Sopenharmony_ci	WARN_ON(!(pte_write(pte) && pte_dirty(pte)));
1218c2ecf20Sopenharmony_ci	ptep_get_and_clear_full(mm, vaddr, ptep, 1);
1228c2ecf20Sopenharmony_ci	pte = ptep_get(ptep);
1238c2ecf20Sopenharmony_ci	WARN_ON(!pte_none(pte));
1248c2ecf20Sopenharmony_ci
1258c2ecf20Sopenharmony_ci	pte = pfn_pte(pfn, prot);
1268c2ecf20Sopenharmony_ci	pte = pte_mkyoung(pte);
1278c2ecf20Sopenharmony_ci	set_pte_at(mm, vaddr, ptep, pte);
1288c2ecf20Sopenharmony_ci	ptep_test_and_clear_young(vma, vaddr, ptep);
1298c2ecf20Sopenharmony_ci	pte = ptep_get(ptep);
1308c2ecf20Sopenharmony_ci	WARN_ON(pte_young(pte));
1318c2ecf20Sopenharmony_ci
1328c2ecf20Sopenharmony_ci	ptep_get_and_clear_full(mm, vaddr, ptep, 1);
1338c2ecf20Sopenharmony_ci}
1348c2ecf20Sopenharmony_ci
1358c2ecf20Sopenharmony_cistatic void __init pte_savedwrite_tests(unsigned long pfn, pgprot_t prot)
1368c2ecf20Sopenharmony_ci{
1378c2ecf20Sopenharmony_ci	pte_t pte = pfn_pte(pfn, prot);
1388c2ecf20Sopenharmony_ci
1398c2ecf20Sopenharmony_ci	if (!IS_ENABLED(CONFIG_NUMA_BALANCING))
1408c2ecf20Sopenharmony_ci		return;
1418c2ecf20Sopenharmony_ci
1428c2ecf20Sopenharmony_ci	pr_debug("Validating PTE saved write\n");
1438c2ecf20Sopenharmony_ci	WARN_ON(!pte_savedwrite(pte_mk_savedwrite(pte_clear_savedwrite(pte))));
1448c2ecf20Sopenharmony_ci	WARN_ON(pte_savedwrite(pte_clear_savedwrite(pte_mk_savedwrite(pte))));
1458c2ecf20Sopenharmony_ci}
1468c2ecf20Sopenharmony_ci
1478c2ecf20Sopenharmony_ci#ifdef CONFIG_TRANSPARENT_HUGEPAGE
1488c2ecf20Sopenharmony_cistatic void __init pmd_basic_tests(unsigned long pfn, int idx)
1498c2ecf20Sopenharmony_ci{
1508c2ecf20Sopenharmony_ci	pgprot_t prot = protection_map[idx];
1518c2ecf20Sopenharmony_ci	unsigned long val = idx, *ptr = &val;
1528c2ecf20Sopenharmony_ci	pmd_t pmd;
1538c2ecf20Sopenharmony_ci
1548c2ecf20Sopenharmony_ci	if (!has_transparent_hugepage())
1558c2ecf20Sopenharmony_ci		return;
1568c2ecf20Sopenharmony_ci
1578c2ecf20Sopenharmony_ci	pr_debug("Validating PMD basic (%pGv)\n", ptr);
1588c2ecf20Sopenharmony_ci	pmd = pfn_pmd(pfn, prot);
1598c2ecf20Sopenharmony_ci
1608c2ecf20Sopenharmony_ci	/*
1618c2ecf20Sopenharmony_ci	 * This test needs to be executed after the given page table entry
1628c2ecf20Sopenharmony_ci	 * is created with pfn_pmd() to make sure that protection_map[idx]
1638c2ecf20Sopenharmony_ci	 * does not have the dirty bit enabled from the beginning. This is
1648c2ecf20Sopenharmony_ci	 * important for platforms like arm64 where (!PTE_RDONLY) indicate
1658c2ecf20Sopenharmony_ci	 * dirty bit being set.
1668c2ecf20Sopenharmony_ci	 */
1678c2ecf20Sopenharmony_ci	WARN_ON(pmd_dirty(pmd_wrprotect(pmd)));
1688c2ecf20Sopenharmony_ci
1698c2ecf20Sopenharmony_ci
1708c2ecf20Sopenharmony_ci	WARN_ON(!pmd_same(pmd, pmd));
1718c2ecf20Sopenharmony_ci	WARN_ON(!pmd_young(pmd_mkyoung(pmd_mkold(pmd))));
1728c2ecf20Sopenharmony_ci	WARN_ON(!pmd_dirty(pmd_mkdirty(pmd_mkclean(pmd))));
1738c2ecf20Sopenharmony_ci	WARN_ON(!pmd_write(pmd_mkwrite(pmd_wrprotect(pmd))));
1748c2ecf20Sopenharmony_ci	WARN_ON(pmd_young(pmd_mkold(pmd_mkyoung(pmd))));
1758c2ecf20Sopenharmony_ci	WARN_ON(pmd_dirty(pmd_mkclean(pmd_mkdirty(pmd))));
1768c2ecf20Sopenharmony_ci	WARN_ON(pmd_write(pmd_wrprotect(pmd_mkwrite(pmd))));
1778c2ecf20Sopenharmony_ci	WARN_ON(pmd_dirty(pmd_wrprotect(pmd_mkclean(pmd))));
1788c2ecf20Sopenharmony_ci	WARN_ON(!pmd_dirty(pmd_wrprotect(pmd_mkdirty(pmd))));
1798c2ecf20Sopenharmony_ci	/*
1808c2ecf20Sopenharmony_ci	 * A huge page does not point to next level page table
1818c2ecf20Sopenharmony_ci	 * entry. Hence this must qualify as pmd_bad().
1828c2ecf20Sopenharmony_ci	 */
1838c2ecf20Sopenharmony_ci	WARN_ON(!pmd_bad(pmd_mkhuge(pmd)));
1848c2ecf20Sopenharmony_ci}
1858c2ecf20Sopenharmony_ci
1868c2ecf20Sopenharmony_cistatic void __init pmd_advanced_tests(struct mm_struct *mm,
1878c2ecf20Sopenharmony_ci				      struct vm_area_struct *vma, pmd_t *pmdp,
1888c2ecf20Sopenharmony_ci				      unsigned long pfn, unsigned long vaddr,
1898c2ecf20Sopenharmony_ci				      pgprot_t prot, pgtable_t pgtable)
1908c2ecf20Sopenharmony_ci{
1918c2ecf20Sopenharmony_ci	pmd_t pmd;
1928c2ecf20Sopenharmony_ci
1938c2ecf20Sopenharmony_ci	if (!has_transparent_hugepage())
1948c2ecf20Sopenharmony_ci		return;
1958c2ecf20Sopenharmony_ci
1968c2ecf20Sopenharmony_ci	pr_debug("Validating PMD advanced\n");
1978c2ecf20Sopenharmony_ci	/* Align the address wrt HPAGE_PMD_SIZE */
1988c2ecf20Sopenharmony_ci	vaddr &= HPAGE_PMD_MASK;
1998c2ecf20Sopenharmony_ci
2008c2ecf20Sopenharmony_ci	pgtable_trans_huge_deposit(mm, pmdp, pgtable);
2018c2ecf20Sopenharmony_ci
2028c2ecf20Sopenharmony_ci	pmd = pfn_pmd(pfn, prot);
2038c2ecf20Sopenharmony_ci	set_pmd_at(mm, vaddr, pmdp, pmd);
2048c2ecf20Sopenharmony_ci	pmdp_set_wrprotect(mm, vaddr, pmdp);
2058c2ecf20Sopenharmony_ci	pmd = READ_ONCE(*pmdp);
2068c2ecf20Sopenharmony_ci	WARN_ON(pmd_write(pmd));
2078c2ecf20Sopenharmony_ci	pmdp_huge_get_and_clear(mm, vaddr, pmdp);
2088c2ecf20Sopenharmony_ci	pmd = READ_ONCE(*pmdp);
2098c2ecf20Sopenharmony_ci	WARN_ON(!pmd_none(pmd));
2108c2ecf20Sopenharmony_ci
2118c2ecf20Sopenharmony_ci	pmd = pfn_pmd(pfn, prot);
2128c2ecf20Sopenharmony_ci	pmd = pmd_wrprotect(pmd);
2138c2ecf20Sopenharmony_ci	pmd = pmd_mkclean(pmd);
2148c2ecf20Sopenharmony_ci	set_pmd_at(mm, vaddr, pmdp, pmd);
2158c2ecf20Sopenharmony_ci	pmd = pmd_mkwrite(pmd);
2168c2ecf20Sopenharmony_ci	pmd = pmd_mkdirty(pmd);
2178c2ecf20Sopenharmony_ci	pmdp_set_access_flags(vma, vaddr, pmdp, pmd, 1);
2188c2ecf20Sopenharmony_ci	pmd = READ_ONCE(*pmdp);
2198c2ecf20Sopenharmony_ci	WARN_ON(!(pmd_write(pmd) && pmd_dirty(pmd)));
2208c2ecf20Sopenharmony_ci	pmdp_huge_get_and_clear_full(vma, vaddr, pmdp, 1);
2218c2ecf20Sopenharmony_ci	pmd = READ_ONCE(*pmdp);
2228c2ecf20Sopenharmony_ci	WARN_ON(!pmd_none(pmd));
2238c2ecf20Sopenharmony_ci
2248c2ecf20Sopenharmony_ci	pmd = pmd_mkhuge(pfn_pmd(pfn, prot));
2258c2ecf20Sopenharmony_ci	pmd = pmd_mkyoung(pmd);
2268c2ecf20Sopenharmony_ci	set_pmd_at(mm, vaddr, pmdp, pmd);
2278c2ecf20Sopenharmony_ci	pmdp_test_and_clear_young(vma, vaddr, pmdp);
2288c2ecf20Sopenharmony_ci	pmd = READ_ONCE(*pmdp);
2298c2ecf20Sopenharmony_ci	WARN_ON(pmd_young(pmd));
2308c2ecf20Sopenharmony_ci
2318c2ecf20Sopenharmony_ci	/*  Clear the pte entries  */
2328c2ecf20Sopenharmony_ci	pmdp_huge_get_and_clear(mm, vaddr, pmdp);
2338c2ecf20Sopenharmony_ci	pgtable = pgtable_trans_huge_withdraw(mm, pmdp);
2348c2ecf20Sopenharmony_ci}
2358c2ecf20Sopenharmony_ci
2368c2ecf20Sopenharmony_cistatic void __init pmd_leaf_tests(unsigned long pfn, pgprot_t prot)
2378c2ecf20Sopenharmony_ci{
2388c2ecf20Sopenharmony_ci	pmd_t pmd;
2398c2ecf20Sopenharmony_ci
2408c2ecf20Sopenharmony_ci	if (!has_transparent_hugepage())
2418c2ecf20Sopenharmony_ci		return;
2428c2ecf20Sopenharmony_ci
2438c2ecf20Sopenharmony_ci	pr_debug("Validating PMD leaf\n");
2448c2ecf20Sopenharmony_ci	pmd = pfn_pmd(pfn, prot);
2458c2ecf20Sopenharmony_ci
2468c2ecf20Sopenharmony_ci	/*
2478c2ecf20Sopenharmony_ci	 * PMD based THP is a leaf entry.
2488c2ecf20Sopenharmony_ci	 */
2498c2ecf20Sopenharmony_ci	pmd = pmd_mkhuge(pmd);
2508c2ecf20Sopenharmony_ci	WARN_ON(!pmd_leaf(pmd));
2518c2ecf20Sopenharmony_ci}
2528c2ecf20Sopenharmony_ci
2538c2ecf20Sopenharmony_ci#ifdef CONFIG_HAVE_ARCH_HUGE_VMAP
2548c2ecf20Sopenharmony_cistatic void __init pmd_huge_tests(pmd_t *pmdp, unsigned long pfn, pgprot_t prot)
2558c2ecf20Sopenharmony_ci{
2568c2ecf20Sopenharmony_ci	pmd_t pmd;
2578c2ecf20Sopenharmony_ci
2588c2ecf20Sopenharmony_ci	if (!arch_ioremap_pmd_supported())
2598c2ecf20Sopenharmony_ci		return;
2608c2ecf20Sopenharmony_ci
2618c2ecf20Sopenharmony_ci	pr_debug("Validating PMD huge\n");
2628c2ecf20Sopenharmony_ci	/*
2638c2ecf20Sopenharmony_ci	 * X86 defined pmd_set_huge() verifies that the given
2648c2ecf20Sopenharmony_ci	 * PMD is not a populated non-leaf entry.
2658c2ecf20Sopenharmony_ci	 */
2668c2ecf20Sopenharmony_ci	WRITE_ONCE(*pmdp, __pmd(0));
2678c2ecf20Sopenharmony_ci	WARN_ON(!pmd_set_huge(pmdp, __pfn_to_phys(pfn), prot));
2688c2ecf20Sopenharmony_ci	WARN_ON(!pmd_clear_huge(pmdp));
2698c2ecf20Sopenharmony_ci	pmd = READ_ONCE(*pmdp);
2708c2ecf20Sopenharmony_ci	WARN_ON(!pmd_none(pmd));
2718c2ecf20Sopenharmony_ci}
2728c2ecf20Sopenharmony_ci#else /* CONFIG_HAVE_ARCH_HUGE_VMAP */
2738c2ecf20Sopenharmony_cistatic void __init pmd_huge_tests(pmd_t *pmdp, unsigned long pfn, pgprot_t prot) { }
2748c2ecf20Sopenharmony_ci#endif /* CONFIG_HAVE_ARCH_HUGE_VMAP */
2758c2ecf20Sopenharmony_ci
2768c2ecf20Sopenharmony_cistatic void __init pmd_savedwrite_tests(unsigned long pfn, pgprot_t prot)
2778c2ecf20Sopenharmony_ci{
2788c2ecf20Sopenharmony_ci	pmd_t pmd;
2798c2ecf20Sopenharmony_ci
2808c2ecf20Sopenharmony_ci	if (!IS_ENABLED(CONFIG_NUMA_BALANCING))
2818c2ecf20Sopenharmony_ci		return;
2828c2ecf20Sopenharmony_ci
2838c2ecf20Sopenharmony_ci	if (!has_transparent_hugepage())
2848c2ecf20Sopenharmony_ci		return;
2858c2ecf20Sopenharmony_ci
2868c2ecf20Sopenharmony_ci	pr_debug("Validating PMD saved write\n");
2878c2ecf20Sopenharmony_ci	pmd = pfn_pmd(pfn, prot);
2888c2ecf20Sopenharmony_ci	WARN_ON(!pmd_savedwrite(pmd_mk_savedwrite(pmd_clear_savedwrite(pmd))));
2898c2ecf20Sopenharmony_ci	WARN_ON(pmd_savedwrite(pmd_clear_savedwrite(pmd_mk_savedwrite(pmd))));
2908c2ecf20Sopenharmony_ci}
2918c2ecf20Sopenharmony_ci
2928c2ecf20Sopenharmony_ci#ifdef CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD
2938c2ecf20Sopenharmony_cistatic void __init pud_basic_tests(struct mm_struct *mm, unsigned long pfn, int idx)
2948c2ecf20Sopenharmony_ci{
2958c2ecf20Sopenharmony_ci	pgprot_t prot = protection_map[idx];
2968c2ecf20Sopenharmony_ci	unsigned long val = idx, *ptr = &val;
2978c2ecf20Sopenharmony_ci	pud_t pud;
2988c2ecf20Sopenharmony_ci
2998c2ecf20Sopenharmony_ci	if (!has_transparent_hugepage())
3008c2ecf20Sopenharmony_ci		return;
3018c2ecf20Sopenharmony_ci
3028c2ecf20Sopenharmony_ci	pr_debug("Validating PUD basic (%pGv)\n", ptr);
3038c2ecf20Sopenharmony_ci	pud = pfn_pud(pfn, prot);
3048c2ecf20Sopenharmony_ci
3058c2ecf20Sopenharmony_ci	/*
3068c2ecf20Sopenharmony_ci	 * This test needs to be executed after the given page table entry
3078c2ecf20Sopenharmony_ci	 * is created with pfn_pud() to make sure that protection_map[idx]
3088c2ecf20Sopenharmony_ci	 * does not have the dirty bit enabled from the beginning. This is
3098c2ecf20Sopenharmony_ci	 * important for platforms like arm64 where (!PTE_RDONLY) indicate
3108c2ecf20Sopenharmony_ci	 * dirty bit being set.
3118c2ecf20Sopenharmony_ci	 */
3128c2ecf20Sopenharmony_ci	WARN_ON(pud_dirty(pud_wrprotect(pud)));
3138c2ecf20Sopenharmony_ci
3148c2ecf20Sopenharmony_ci	WARN_ON(!pud_same(pud, pud));
3158c2ecf20Sopenharmony_ci	WARN_ON(!pud_young(pud_mkyoung(pud_mkold(pud))));
3168c2ecf20Sopenharmony_ci	WARN_ON(!pud_dirty(pud_mkdirty(pud_mkclean(pud))));
3178c2ecf20Sopenharmony_ci	WARN_ON(pud_dirty(pud_mkclean(pud_mkdirty(pud))));
3188c2ecf20Sopenharmony_ci	WARN_ON(!pud_write(pud_mkwrite(pud_wrprotect(pud))));
3198c2ecf20Sopenharmony_ci	WARN_ON(pud_write(pud_wrprotect(pud_mkwrite(pud))));
3208c2ecf20Sopenharmony_ci	WARN_ON(pud_young(pud_mkold(pud_mkyoung(pud))));
3218c2ecf20Sopenharmony_ci	WARN_ON(pud_dirty(pud_wrprotect(pud_mkclean(pud))));
3228c2ecf20Sopenharmony_ci	WARN_ON(!pud_dirty(pud_wrprotect(pud_mkdirty(pud))));
3238c2ecf20Sopenharmony_ci
3248c2ecf20Sopenharmony_ci	if (mm_pmd_folded(mm))
3258c2ecf20Sopenharmony_ci		return;
3268c2ecf20Sopenharmony_ci
3278c2ecf20Sopenharmony_ci	/*
3288c2ecf20Sopenharmony_ci	 * A huge page does not point to next level page table
3298c2ecf20Sopenharmony_ci	 * entry. Hence this must qualify as pud_bad().
3308c2ecf20Sopenharmony_ci	 */
3318c2ecf20Sopenharmony_ci	WARN_ON(!pud_bad(pud_mkhuge(pud)));
3328c2ecf20Sopenharmony_ci}
3338c2ecf20Sopenharmony_ci
3348c2ecf20Sopenharmony_cistatic void __init pud_advanced_tests(struct mm_struct *mm,
3358c2ecf20Sopenharmony_ci				      struct vm_area_struct *vma, pud_t *pudp,
3368c2ecf20Sopenharmony_ci				      unsigned long pfn, unsigned long vaddr,
3378c2ecf20Sopenharmony_ci				      pgprot_t prot)
3388c2ecf20Sopenharmony_ci{
3398c2ecf20Sopenharmony_ci	pud_t pud;
3408c2ecf20Sopenharmony_ci
3418c2ecf20Sopenharmony_ci	if (!has_transparent_hugepage())
3428c2ecf20Sopenharmony_ci		return;
3438c2ecf20Sopenharmony_ci
3448c2ecf20Sopenharmony_ci	pr_debug("Validating PUD advanced\n");
3458c2ecf20Sopenharmony_ci	/* Align the address wrt HPAGE_PUD_SIZE */
3468c2ecf20Sopenharmony_ci	vaddr &= HPAGE_PUD_MASK;
3478c2ecf20Sopenharmony_ci
3488c2ecf20Sopenharmony_ci	pud = pfn_pud(pfn, prot);
3498c2ecf20Sopenharmony_ci	set_pud_at(mm, vaddr, pudp, pud);
3508c2ecf20Sopenharmony_ci	pudp_set_wrprotect(mm, vaddr, pudp);
3518c2ecf20Sopenharmony_ci	pud = READ_ONCE(*pudp);
3528c2ecf20Sopenharmony_ci	WARN_ON(pud_write(pud));
3538c2ecf20Sopenharmony_ci
3548c2ecf20Sopenharmony_ci#ifndef __PAGETABLE_PMD_FOLDED
3558c2ecf20Sopenharmony_ci	pudp_huge_get_and_clear(mm, vaddr, pudp);
3568c2ecf20Sopenharmony_ci	pud = READ_ONCE(*pudp);
3578c2ecf20Sopenharmony_ci	WARN_ON(!pud_none(pud));
3588c2ecf20Sopenharmony_ci#endif /* __PAGETABLE_PMD_FOLDED */
3598c2ecf20Sopenharmony_ci	pud = pfn_pud(pfn, prot);
3608c2ecf20Sopenharmony_ci	pud = pud_wrprotect(pud);
3618c2ecf20Sopenharmony_ci	pud = pud_mkclean(pud);
3628c2ecf20Sopenharmony_ci	set_pud_at(mm, vaddr, pudp, pud);
3638c2ecf20Sopenharmony_ci	pud = pud_mkwrite(pud);
3648c2ecf20Sopenharmony_ci	pud = pud_mkdirty(pud);
3658c2ecf20Sopenharmony_ci	pudp_set_access_flags(vma, vaddr, pudp, pud, 1);
3668c2ecf20Sopenharmony_ci	pud = READ_ONCE(*pudp);
3678c2ecf20Sopenharmony_ci	WARN_ON(!(pud_write(pud) && pud_dirty(pud)));
3688c2ecf20Sopenharmony_ci
3698c2ecf20Sopenharmony_ci#ifndef __PAGETABLE_PMD_FOLDED
3708c2ecf20Sopenharmony_ci	pudp_huge_get_and_clear_full(mm, vaddr, pudp, 1);
3718c2ecf20Sopenharmony_ci	pud = READ_ONCE(*pudp);
3728c2ecf20Sopenharmony_ci	WARN_ON(!pud_none(pud));
3738c2ecf20Sopenharmony_ci#endif /* __PAGETABLE_PMD_FOLDED */
3748c2ecf20Sopenharmony_ci
3758c2ecf20Sopenharmony_ci	pud = pfn_pud(pfn, prot);
3768c2ecf20Sopenharmony_ci	pud = pud_mkyoung(pud);
3778c2ecf20Sopenharmony_ci	set_pud_at(mm, vaddr, pudp, pud);
3788c2ecf20Sopenharmony_ci	pudp_test_and_clear_young(vma, vaddr, pudp);
3798c2ecf20Sopenharmony_ci	pud = READ_ONCE(*pudp);
3808c2ecf20Sopenharmony_ci	WARN_ON(pud_young(pud));
3818c2ecf20Sopenharmony_ci
3828c2ecf20Sopenharmony_ci	pudp_huge_get_and_clear(mm, vaddr, pudp);
3838c2ecf20Sopenharmony_ci}
3848c2ecf20Sopenharmony_ci
3858c2ecf20Sopenharmony_cistatic void __init pud_leaf_tests(unsigned long pfn, pgprot_t prot)
3868c2ecf20Sopenharmony_ci{
3878c2ecf20Sopenharmony_ci	pud_t pud;
3888c2ecf20Sopenharmony_ci
3898c2ecf20Sopenharmony_ci	if (!has_transparent_hugepage())
3908c2ecf20Sopenharmony_ci		return;
3918c2ecf20Sopenharmony_ci
3928c2ecf20Sopenharmony_ci	pr_debug("Validating PUD leaf\n");
3938c2ecf20Sopenharmony_ci	pud = pfn_pud(pfn, prot);
3948c2ecf20Sopenharmony_ci	/*
3958c2ecf20Sopenharmony_ci	 * PUD based THP is a leaf entry.
3968c2ecf20Sopenharmony_ci	 */
3978c2ecf20Sopenharmony_ci	pud = pud_mkhuge(pud);
3988c2ecf20Sopenharmony_ci	WARN_ON(!pud_leaf(pud));
3998c2ecf20Sopenharmony_ci}
4008c2ecf20Sopenharmony_ci
4018c2ecf20Sopenharmony_ci#ifdef CONFIG_HAVE_ARCH_HUGE_VMAP
4028c2ecf20Sopenharmony_cistatic void __init pud_huge_tests(pud_t *pudp, unsigned long pfn, pgprot_t prot)
4038c2ecf20Sopenharmony_ci{
4048c2ecf20Sopenharmony_ci	pud_t pud;
4058c2ecf20Sopenharmony_ci
4068c2ecf20Sopenharmony_ci	if (!arch_ioremap_pud_supported())
4078c2ecf20Sopenharmony_ci		return;
4088c2ecf20Sopenharmony_ci
4098c2ecf20Sopenharmony_ci	pr_debug("Validating PUD huge\n");
4108c2ecf20Sopenharmony_ci	/*
4118c2ecf20Sopenharmony_ci	 * X86 defined pud_set_huge() verifies that the given
4128c2ecf20Sopenharmony_ci	 * PUD is not a populated non-leaf entry.
4138c2ecf20Sopenharmony_ci	 */
4148c2ecf20Sopenharmony_ci	WRITE_ONCE(*pudp, __pud(0));
4158c2ecf20Sopenharmony_ci	WARN_ON(!pud_set_huge(pudp, __pfn_to_phys(pfn), prot));
4168c2ecf20Sopenharmony_ci	WARN_ON(!pud_clear_huge(pudp));
4178c2ecf20Sopenharmony_ci	pud = READ_ONCE(*pudp);
4188c2ecf20Sopenharmony_ci	WARN_ON(!pud_none(pud));
4198c2ecf20Sopenharmony_ci}
4208c2ecf20Sopenharmony_ci#else /* !CONFIG_HAVE_ARCH_HUGE_VMAP */
4218c2ecf20Sopenharmony_cistatic void __init pud_huge_tests(pud_t *pudp, unsigned long pfn, pgprot_t prot) { }
4228c2ecf20Sopenharmony_ci#endif /* !CONFIG_HAVE_ARCH_HUGE_VMAP */
4238c2ecf20Sopenharmony_ci
4248c2ecf20Sopenharmony_ci#else  /* !CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD */
4258c2ecf20Sopenharmony_cistatic void __init pud_basic_tests(struct mm_struct *mm, unsigned long pfn, int idx) { }
4268c2ecf20Sopenharmony_cistatic void __init pud_advanced_tests(struct mm_struct *mm,
4278c2ecf20Sopenharmony_ci				      struct vm_area_struct *vma, pud_t *pudp,
4288c2ecf20Sopenharmony_ci				      unsigned long pfn, unsigned long vaddr,
4298c2ecf20Sopenharmony_ci				      pgprot_t prot)
4308c2ecf20Sopenharmony_ci{
4318c2ecf20Sopenharmony_ci}
4328c2ecf20Sopenharmony_cistatic void __init pud_leaf_tests(unsigned long pfn, pgprot_t prot) { }
4338c2ecf20Sopenharmony_cistatic void __init pud_huge_tests(pud_t *pudp, unsigned long pfn, pgprot_t prot)
4348c2ecf20Sopenharmony_ci{
4358c2ecf20Sopenharmony_ci}
4368c2ecf20Sopenharmony_ci#endif /* CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD */
4378c2ecf20Sopenharmony_ci#else  /* !CONFIG_TRANSPARENT_HUGEPAGE */
4388c2ecf20Sopenharmony_cistatic void __init pmd_basic_tests(unsigned long pfn, int idx) { }
4398c2ecf20Sopenharmony_cistatic void __init pud_basic_tests(struct mm_struct *mm, unsigned long pfn, int idx) { }
4408c2ecf20Sopenharmony_cistatic void __init pmd_advanced_tests(struct mm_struct *mm,
4418c2ecf20Sopenharmony_ci				      struct vm_area_struct *vma, pmd_t *pmdp,
4428c2ecf20Sopenharmony_ci				      unsigned long pfn, unsigned long vaddr,
4438c2ecf20Sopenharmony_ci				      pgprot_t prot, pgtable_t pgtable)
4448c2ecf20Sopenharmony_ci{
4458c2ecf20Sopenharmony_ci}
4468c2ecf20Sopenharmony_cistatic void __init pud_advanced_tests(struct mm_struct *mm,
4478c2ecf20Sopenharmony_ci				      struct vm_area_struct *vma, pud_t *pudp,
4488c2ecf20Sopenharmony_ci				      unsigned long pfn, unsigned long vaddr,
4498c2ecf20Sopenharmony_ci				      pgprot_t prot)
4508c2ecf20Sopenharmony_ci{
4518c2ecf20Sopenharmony_ci}
4528c2ecf20Sopenharmony_cistatic void __init pmd_leaf_tests(unsigned long pfn, pgprot_t prot) { }
4538c2ecf20Sopenharmony_cistatic void __init pud_leaf_tests(unsigned long pfn, pgprot_t prot) { }
4548c2ecf20Sopenharmony_cistatic void __init pmd_huge_tests(pmd_t *pmdp, unsigned long pfn, pgprot_t prot)
4558c2ecf20Sopenharmony_ci{
4568c2ecf20Sopenharmony_ci}
4578c2ecf20Sopenharmony_cistatic void __init pud_huge_tests(pud_t *pudp, unsigned long pfn, pgprot_t prot)
4588c2ecf20Sopenharmony_ci{
4598c2ecf20Sopenharmony_ci}
4608c2ecf20Sopenharmony_cistatic void __init pmd_savedwrite_tests(unsigned long pfn, pgprot_t prot) { }
4618c2ecf20Sopenharmony_ci#endif /* CONFIG_TRANSPARENT_HUGEPAGE */
4628c2ecf20Sopenharmony_ci
4638c2ecf20Sopenharmony_cistatic void __init p4d_basic_tests(unsigned long pfn, pgprot_t prot)
4648c2ecf20Sopenharmony_ci{
4658c2ecf20Sopenharmony_ci	p4d_t p4d;
4668c2ecf20Sopenharmony_ci
4678c2ecf20Sopenharmony_ci	pr_debug("Validating P4D basic\n");
4688c2ecf20Sopenharmony_ci	memset(&p4d, RANDOM_NZVALUE, sizeof(p4d_t));
4698c2ecf20Sopenharmony_ci	WARN_ON(!p4d_same(p4d, p4d));
4708c2ecf20Sopenharmony_ci}
4718c2ecf20Sopenharmony_ci
4728c2ecf20Sopenharmony_cistatic void __init pgd_basic_tests(unsigned long pfn, pgprot_t prot)
4738c2ecf20Sopenharmony_ci{
4748c2ecf20Sopenharmony_ci	pgd_t pgd;
4758c2ecf20Sopenharmony_ci
4768c2ecf20Sopenharmony_ci	pr_debug("Validating PGD basic\n");
4778c2ecf20Sopenharmony_ci	memset(&pgd, RANDOM_NZVALUE, sizeof(pgd_t));
4788c2ecf20Sopenharmony_ci	WARN_ON(!pgd_same(pgd, pgd));
4798c2ecf20Sopenharmony_ci}
4808c2ecf20Sopenharmony_ci
4818c2ecf20Sopenharmony_ci#ifndef __PAGETABLE_PUD_FOLDED
4828c2ecf20Sopenharmony_cistatic void __init pud_clear_tests(struct mm_struct *mm, pud_t *pudp)
4838c2ecf20Sopenharmony_ci{
4848c2ecf20Sopenharmony_ci	pud_t pud = READ_ONCE(*pudp);
4858c2ecf20Sopenharmony_ci
4868c2ecf20Sopenharmony_ci	if (mm_pmd_folded(mm))
4878c2ecf20Sopenharmony_ci		return;
4888c2ecf20Sopenharmony_ci
4898c2ecf20Sopenharmony_ci	pr_debug("Validating PUD clear\n");
4908c2ecf20Sopenharmony_ci	pud = __pud(pud_val(pud) | RANDOM_ORVALUE);
4918c2ecf20Sopenharmony_ci	WRITE_ONCE(*pudp, pud);
4928c2ecf20Sopenharmony_ci	pud_clear(pudp);
4938c2ecf20Sopenharmony_ci	pud = READ_ONCE(*pudp);
4948c2ecf20Sopenharmony_ci	WARN_ON(!pud_none(pud));
4958c2ecf20Sopenharmony_ci}
4968c2ecf20Sopenharmony_ci
4978c2ecf20Sopenharmony_cistatic void __init pud_populate_tests(struct mm_struct *mm, pud_t *pudp,
4988c2ecf20Sopenharmony_ci				      pmd_t *pmdp)
4998c2ecf20Sopenharmony_ci{
5008c2ecf20Sopenharmony_ci	pud_t pud;
5018c2ecf20Sopenharmony_ci
5028c2ecf20Sopenharmony_ci	if (mm_pmd_folded(mm))
5038c2ecf20Sopenharmony_ci		return;
5048c2ecf20Sopenharmony_ci
5058c2ecf20Sopenharmony_ci	pr_debug("Validating PUD populate\n");
5068c2ecf20Sopenharmony_ci	/*
5078c2ecf20Sopenharmony_ci	 * This entry points to next level page table page.
5088c2ecf20Sopenharmony_ci	 * Hence this must not qualify as pud_bad().
5098c2ecf20Sopenharmony_ci	 */
5108c2ecf20Sopenharmony_ci	pud_populate(mm, pudp, pmdp);
5118c2ecf20Sopenharmony_ci	pud = READ_ONCE(*pudp);
5128c2ecf20Sopenharmony_ci	WARN_ON(pud_bad(pud));
5138c2ecf20Sopenharmony_ci}
5148c2ecf20Sopenharmony_ci#else  /* !__PAGETABLE_PUD_FOLDED */
5158c2ecf20Sopenharmony_cistatic void __init pud_clear_tests(struct mm_struct *mm, pud_t *pudp) { }
5168c2ecf20Sopenharmony_cistatic void __init pud_populate_tests(struct mm_struct *mm, pud_t *pudp,
5178c2ecf20Sopenharmony_ci				      pmd_t *pmdp)
5188c2ecf20Sopenharmony_ci{
5198c2ecf20Sopenharmony_ci}
5208c2ecf20Sopenharmony_ci#endif /* PAGETABLE_PUD_FOLDED */
5218c2ecf20Sopenharmony_ci
5228c2ecf20Sopenharmony_ci#ifndef __PAGETABLE_P4D_FOLDED
5238c2ecf20Sopenharmony_cistatic void __init p4d_clear_tests(struct mm_struct *mm, p4d_t *p4dp)
5248c2ecf20Sopenharmony_ci{
5258c2ecf20Sopenharmony_ci	p4d_t p4d = READ_ONCE(*p4dp);
5268c2ecf20Sopenharmony_ci
5278c2ecf20Sopenharmony_ci	if (mm_pud_folded(mm))
5288c2ecf20Sopenharmony_ci		return;
5298c2ecf20Sopenharmony_ci
5308c2ecf20Sopenharmony_ci	pr_debug("Validating P4D clear\n");
5318c2ecf20Sopenharmony_ci	p4d = __p4d(p4d_val(p4d) | RANDOM_ORVALUE);
5328c2ecf20Sopenharmony_ci	WRITE_ONCE(*p4dp, p4d);
5338c2ecf20Sopenharmony_ci	p4d_clear(p4dp);
5348c2ecf20Sopenharmony_ci	p4d = READ_ONCE(*p4dp);
5358c2ecf20Sopenharmony_ci	WARN_ON(!p4d_none(p4d));
5368c2ecf20Sopenharmony_ci}
5378c2ecf20Sopenharmony_ci
5388c2ecf20Sopenharmony_cistatic void __init p4d_populate_tests(struct mm_struct *mm, p4d_t *p4dp,
5398c2ecf20Sopenharmony_ci				      pud_t *pudp)
5408c2ecf20Sopenharmony_ci{
5418c2ecf20Sopenharmony_ci	p4d_t p4d;
5428c2ecf20Sopenharmony_ci
5438c2ecf20Sopenharmony_ci	if (mm_pud_folded(mm))
5448c2ecf20Sopenharmony_ci		return;
5458c2ecf20Sopenharmony_ci
5468c2ecf20Sopenharmony_ci	pr_debug("Validating P4D populate\n");
5478c2ecf20Sopenharmony_ci	/*
5488c2ecf20Sopenharmony_ci	 * This entry points to next level page table page.
5498c2ecf20Sopenharmony_ci	 * Hence this must not qualify as p4d_bad().
5508c2ecf20Sopenharmony_ci	 */
5518c2ecf20Sopenharmony_ci	pud_clear(pudp);
5528c2ecf20Sopenharmony_ci	p4d_clear(p4dp);
5538c2ecf20Sopenharmony_ci	p4d_populate(mm, p4dp, pudp);
5548c2ecf20Sopenharmony_ci	p4d = READ_ONCE(*p4dp);
5558c2ecf20Sopenharmony_ci	WARN_ON(p4d_bad(p4d));
5568c2ecf20Sopenharmony_ci}
5578c2ecf20Sopenharmony_ci
5588c2ecf20Sopenharmony_cistatic void __init pgd_clear_tests(struct mm_struct *mm, pgd_t *pgdp)
5598c2ecf20Sopenharmony_ci{
5608c2ecf20Sopenharmony_ci	pgd_t pgd = READ_ONCE(*pgdp);
5618c2ecf20Sopenharmony_ci
5628c2ecf20Sopenharmony_ci	if (mm_p4d_folded(mm))
5638c2ecf20Sopenharmony_ci		return;
5648c2ecf20Sopenharmony_ci
5658c2ecf20Sopenharmony_ci	pr_debug("Validating PGD clear\n");
5668c2ecf20Sopenharmony_ci	pgd = __pgd(pgd_val(pgd) | RANDOM_ORVALUE);
5678c2ecf20Sopenharmony_ci	WRITE_ONCE(*pgdp, pgd);
5688c2ecf20Sopenharmony_ci	pgd_clear(pgdp);
5698c2ecf20Sopenharmony_ci	pgd = READ_ONCE(*pgdp);
5708c2ecf20Sopenharmony_ci	WARN_ON(!pgd_none(pgd));
5718c2ecf20Sopenharmony_ci}
5728c2ecf20Sopenharmony_ci
5738c2ecf20Sopenharmony_cistatic void __init pgd_populate_tests(struct mm_struct *mm, pgd_t *pgdp,
5748c2ecf20Sopenharmony_ci				      p4d_t *p4dp)
5758c2ecf20Sopenharmony_ci{
5768c2ecf20Sopenharmony_ci	pgd_t pgd;
5778c2ecf20Sopenharmony_ci
5788c2ecf20Sopenharmony_ci	if (mm_p4d_folded(mm))
5798c2ecf20Sopenharmony_ci		return;
5808c2ecf20Sopenharmony_ci
5818c2ecf20Sopenharmony_ci	pr_debug("Validating PGD populate\n");
5828c2ecf20Sopenharmony_ci	/*
5838c2ecf20Sopenharmony_ci	 * This entry points to next level page table page.
5848c2ecf20Sopenharmony_ci	 * Hence this must not qualify as pgd_bad().
5858c2ecf20Sopenharmony_ci	 */
5868c2ecf20Sopenharmony_ci	p4d_clear(p4dp);
5878c2ecf20Sopenharmony_ci	pgd_clear(pgdp);
5888c2ecf20Sopenharmony_ci	pgd_populate(mm, pgdp, p4dp);
5898c2ecf20Sopenharmony_ci	pgd = READ_ONCE(*pgdp);
5908c2ecf20Sopenharmony_ci	WARN_ON(pgd_bad(pgd));
5918c2ecf20Sopenharmony_ci}
5928c2ecf20Sopenharmony_ci#else  /* !__PAGETABLE_P4D_FOLDED */
5938c2ecf20Sopenharmony_cistatic void __init p4d_clear_tests(struct mm_struct *mm, p4d_t *p4dp) { }
5948c2ecf20Sopenharmony_cistatic void __init pgd_clear_tests(struct mm_struct *mm, pgd_t *pgdp) { }
5958c2ecf20Sopenharmony_cistatic void __init p4d_populate_tests(struct mm_struct *mm, p4d_t *p4dp,
5968c2ecf20Sopenharmony_ci				      pud_t *pudp)
5978c2ecf20Sopenharmony_ci{
5988c2ecf20Sopenharmony_ci}
5998c2ecf20Sopenharmony_cistatic void __init pgd_populate_tests(struct mm_struct *mm, pgd_t *pgdp,
6008c2ecf20Sopenharmony_ci				      p4d_t *p4dp)
6018c2ecf20Sopenharmony_ci{
6028c2ecf20Sopenharmony_ci}
6038c2ecf20Sopenharmony_ci#endif /* PAGETABLE_P4D_FOLDED */
6048c2ecf20Sopenharmony_ci
6058c2ecf20Sopenharmony_cistatic void __init pte_clear_tests(struct mm_struct *mm, pte_t *ptep,
6068c2ecf20Sopenharmony_ci				   unsigned long pfn, unsigned long vaddr,
6078c2ecf20Sopenharmony_ci				   pgprot_t prot)
6088c2ecf20Sopenharmony_ci{
6098c2ecf20Sopenharmony_ci	pte_t pte = pfn_pte(pfn, prot);
6108c2ecf20Sopenharmony_ci
6118c2ecf20Sopenharmony_ci	pr_debug("Validating PTE clear\n");
6128c2ecf20Sopenharmony_ci#ifndef CONFIG_RISCV
6138c2ecf20Sopenharmony_ci	pte = __pte(pte_val(pte) | RANDOM_ORVALUE);
6148c2ecf20Sopenharmony_ci#endif
6158c2ecf20Sopenharmony_ci	set_pte_at(mm, vaddr, ptep, pte);
6168c2ecf20Sopenharmony_ci	barrier();
6178c2ecf20Sopenharmony_ci	pte_clear(mm, vaddr, ptep);
6188c2ecf20Sopenharmony_ci	pte = ptep_get(ptep);
6198c2ecf20Sopenharmony_ci	WARN_ON(!pte_none(pte));
6208c2ecf20Sopenharmony_ci}
6218c2ecf20Sopenharmony_ci
6228c2ecf20Sopenharmony_cistatic void __init pmd_clear_tests(struct mm_struct *mm, pmd_t *pmdp)
6238c2ecf20Sopenharmony_ci{
6248c2ecf20Sopenharmony_ci	pmd_t pmd = READ_ONCE(*pmdp);
6258c2ecf20Sopenharmony_ci
6268c2ecf20Sopenharmony_ci	pr_debug("Validating PMD clear\n");
6278c2ecf20Sopenharmony_ci	pmd = __pmd(pmd_val(pmd) | RANDOM_ORVALUE);
6288c2ecf20Sopenharmony_ci	WRITE_ONCE(*pmdp, pmd);
6298c2ecf20Sopenharmony_ci	pmd_clear(pmdp);
6308c2ecf20Sopenharmony_ci	pmd = READ_ONCE(*pmdp);
6318c2ecf20Sopenharmony_ci	WARN_ON(!pmd_none(pmd));
6328c2ecf20Sopenharmony_ci}
6338c2ecf20Sopenharmony_ci
6348c2ecf20Sopenharmony_cistatic void __init pmd_populate_tests(struct mm_struct *mm, pmd_t *pmdp,
6358c2ecf20Sopenharmony_ci				      pgtable_t pgtable)
6368c2ecf20Sopenharmony_ci{
6378c2ecf20Sopenharmony_ci	pmd_t pmd;
6388c2ecf20Sopenharmony_ci
6398c2ecf20Sopenharmony_ci	pr_debug("Validating PMD populate\n");
6408c2ecf20Sopenharmony_ci	/*
6418c2ecf20Sopenharmony_ci	 * This entry points to next level page table page.
6428c2ecf20Sopenharmony_ci	 * Hence this must not qualify as pmd_bad().
6438c2ecf20Sopenharmony_ci	 */
6448c2ecf20Sopenharmony_ci	pmd_populate(mm, pmdp, pgtable);
6458c2ecf20Sopenharmony_ci	pmd = READ_ONCE(*pmdp);
6468c2ecf20Sopenharmony_ci	WARN_ON(pmd_bad(pmd));
6478c2ecf20Sopenharmony_ci}
6488c2ecf20Sopenharmony_ci
6498c2ecf20Sopenharmony_cistatic void __init pte_special_tests(unsigned long pfn, pgprot_t prot)
6508c2ecf20Sopenharmony_ci{
6518c2ecf20Sopenharmony_ci	pte_t pte = pfn_pte(pfn, prot);
6528c2ecf20Sopenharmony_ci
6538c2ecf20Sopenharmony_ci	if (!IS_ENABLED(CONFIG_ARCH_HAS_PTE_SPECIAL))
6548c2ecf20Sopenharmony_ci		return;
6558c2ecf20Sopenharmony_ci
6568c2ecf20Sopenharmony_ci	pr_debug("Validating PTE special\n");
6578c2ecf20Sopenharmony_ci	WARN_ON(!pte_special(pte_mkspecial(pte)));
6588c2ecf20Sopenharmony_ci}
6598c2ecf20Sopenharmony_ci
6608c2ecf20Sopenharmony_cistatic void __init pte_protnone_tests(unsigned long pfn, pgprot_t prot)
6618c2ecf20Sopenharmony_ci{
6628c2ecf20Sopenharmony_ci	pte_t pte = pfn_pte(pfn, prot);
6638c2ecf20Sopenharmony_ci
6648c2ecf20Sopenharmony_ci	if (!IS_ENABLED(CONFIG_NUMA_BALANCING))
6658c2ecf20Sopenharmony_ci		return;
6668c2ecf20Sopenharmony_ci
6678c2ecf20Sopenharmony_ci	pr_debug("Validating PTE protnone\n");
6688c2ecf20Sopenharmony_ci	WARN_ON(!pte_protnone(pte));
6698c2ecf20Sopenharmony_ci	WARN_ON(!pte_present(pte));
6708c2ecf20Sopenharmony_ci}
6718c2ecf20Sopenharmony_ci
6728c2ecf20Sopenharmony_ci#ifdef CONFIG_TRANSPARENT_HUGEPAGE
6738c2ecf20Sopenharmony_cistatic void __init pmd_protnone_tests(unsigned long pfn, pgprot_t prot)
6748c2ecf20Sopenharmony_ci{
6758c2ecf20Sopenharmony_ci	pmd_t pmd;
6768c2ecf20Sopenharmony_ci
6778c2ecf20Sopenharmony_ci	if (!IS_ENABLED(CONFIG_NUMA_BALANCING))
6788c2ecf20Sopenharmony_ci		return;
6798c2ecf20Sopenharmony_ci
6808c2ecf20Sopenharmony_ci	if (!has_transparent_hugepage())
6818c2ecf20Sopenharmony_ci		return;
6828c2ecf20Sopenharmony_ci
6838c2ecf20Sopenharmony_ci	pr_debug("Validating PMD protnone\n");
6848c2ecf20Sopenharmony_ci	pmd = pmd_mkhuge(pfn_pmd(pfn, prot));
6858c2ecf20Sopenharmony_ci	WARN_ON(!pmd_protnone(pmd));
6868c2ecf20Sopenharmony_ci	WARN_ON(!pmd_present(pmd));
6878c2ecf20Sopenharmony_ci}
6888c2ecf20Sopenharmony_ci#else  /* !CONFIG_TRANSPARENT_HUGEPAGE */
6898c2ecf20Sopenharmony_cistatic void __init pmd_protnone_tests(unsigned long pfn, pgprot_t prot) { }
6908c2ecf20Sopenharmony_ci#endif /* CONFIG_TRANSPARENT_HUGEPAGE */
6918c2ecf20Sopenharmony_ci
6928c2ecf20Sopenharmony_ci#ifdef CONFIG_ARCH_HAS_PTE_DEVMAP
6938c2ecf20Sopenharmony_cistatic void __init pte_devmap_tests(unsigned long pfn, pgprot_t prot)
6948c2ecf20Sopenharmony_ci{
6958c2ecf20Sopenharmony_ci	pte_t pte = pfn_pte(pfn, prot);
6968c2ecf20Sopenharmony_ci
6978c2ecf20Sopenharmony_ci	pr_debug("Validating PTE devmap\n");
6988c2ecf20Sopenharmony_ci	WARN_ON(!pte_devmap(pte_mkdevmap(pte)));
6998c2ecf20Sopenharmony_ci}
7008c2ecf20Sopenharmony_ci
7018c2ecf20Sopenharmony_ci#ifdef CONFIG_TRANSPARENT_HUGEPAGE
7028c2ecf20Sopenharmony_cistatic void __init pmd_devmap_tests(unsigned long pfn, pgprot_t prot)
7038c2ecf20Sopenharmony_ci{
7048c2ecf20Sopenharmony_ci	pmd_t pmd;
7058c2ecf20Sopenharmony_ci
7068c2ecf20Sopenharmony_ci	if (!has_transparent_hugepage())
7078c2ecf20Sopenharmony_ci		return;
7088c2ecf20Sopenharmony_ci
7098c2ecf20Sopenharmony_ci	pr_debug("Validating PMD devmap\n");
7108c2ecf20Sopenharmony_ci	pmd = pfn_pmd(pfn, prot);
7118c2ecf20Sopenharmony_ci	WARN_ON(!pmd_devmap(pmd_mkdevmap(pmd)));
7128c2ecf20Sopenharmony_ci}
7138c2ecf20Sopenharmony_ci
7148c2ecf20Sopenharmony_ci#ifdef CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD
7158c2ecf20Sopenharmony_cistatic void __init pud_devmap_tests(unsigned long pfn, pgprot_t prot)
7168c2ecf20Sopenharmony_ci{
7178c2ecf20Sopenharmony_ci	pud_t pud;
7188c2ecf20Sopenharmony_ci
7198c2ecf20Sopenharmony_ci	if (!has_transparent_hugepage())
7208c2ecf20Sopenharmony_ci		return;
7218c2ecf20Sopenharmony_ci
7228c2ecf20Sopenharmony_ci	pr_debug("Validating PUD devmap\n");
7238c2ecf20Sopenharmony_ci	pud = pfn_pud(pfn, prot);
7248c2ecf20Sopenharmony_ci	WARN_ON(!pud_devmap(pud_mkdevmap(pud)));
7258c2ecf20Sopenharmony_ci}
7268c2ecf20Sopenharmony_ci#else  /* !CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD */
7278c2ecf20Sopenharmony_cistatic void __init pud_devmap_tests(unsigned long pfn, pgprot_t prot) { }
7288c2ecf20Sopenharmony_ci#endif /* CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD */
7298c2ecf20Sopenharmony_ci#else  /* CONFIG_TRANSPARENT_HUGEPAGE */
7308c2ecf20Sopenharmony_cistatic void __init pmd_devmap_tests(unsigned long pfn, pgprot_t prot) { }
7318c2ecf20Sopenharmony_cistatic void __init pud_devmap_tests(unsigned long pfn, pgprot_t prot) { }
7328c2ecf20Sopenharmony_ci#endif /* CONFIG_TRANSPARENT_HUGEPAGE */
7338c2ecf20Sopenharmony_ci#else
7348c2ecf20Sopenharmony_cistatic void __init pte_devmap_tests(unsigned long pfn, pgprot_t prot) { }
7358c2ecf20Sopenharmony_cistatic void __init pmd_devmap_tests(unsigned long pfn, pgprot_t prot) { }
7368c2ecf20Sopenharmony_cistatic void __init pud_devmap_tests(unsigned long pfn, pgprot_t prot) { }
7378c2ecf20Sopenharmony_ci#endif /* CONFIG_ARCH_HAS_PTE_DEVMAP */
7388c2ecf20Sopenharmony_ci
7398c2ecf20Sopenharmony_cistatic void __init pte_soft_dirty_tests(unsigned long pfn, pgprot_t prot)
7408c2ecf20Sopenharmony_ci{
7418c2ecf20Sopenharmony_ci	pte_t pte = pfn_pte(pfn, prot);
7428c2ecf20Sopenharmony_ci
7438c2ecf20Sopenharmony_ci	if (!IS_ENABLED(CONFIG_MEM_SOFT_DIRTY))
7448c2ecf20Sopenharmony_ci		return;
7458c2ecf20Sopenharmony_ci
7468c2ecf20Sopenharmony_ci	pr_debug("Validating PTE soft dirty\n");
7478c2ecf20Sopenharmony_ci	WARN_ON(!pte_soft_dirty(pte_mksoft_dirty(pte)));
7488c2ecf20Sopenharmony_ci	WARN_ON(pte_soft_dirty(pte_clear_soft_dirty(pte)));
7498c2ecf20Sopenharmony_ci}
7508c2ecf20Sopenharmony_ci
7518c2ecf20Sopenharmony_cistatic void __init pte_swap_soft_dirty_tests(unsigned long pfn, pgprot_t prot)
7528c2ecf20Sopenharmony_ci{
7538c2ecf20Sopenharmony_ci	pte_t pte = pfn_pte(pfn, prot);
7548c2ecf20Sopenharmony_ci
7558c2ecf20Sopenharmony_ci	if (!IS_ENABLED(CONFIG_MEM_SOFT_DIRTY))
7568c2ecf20Sopenharmony_ci		return;
7578c2ecf20Sopenharmony_ci
7588c2ecf20Sopenharmony_ci	pr_debug("Validating PTE swap soft dirty\n");
7598c2ecf20Sopenharmony_ci	WARN_ON(!pte_swp_soft_dirty(pte_swp_mksoft_dirty(pte)));
7608c2ecf20Sopenharmony_ci	WARN_ON(pte_swp_soft_dirty(pte_swp_clear_soft_dirty(pte)));
7618c2ecf20Sopenharmony_ci}
7628c2ecf20Sopenharmony_ci
7638c2ecf20Sopenharmony_ci#ifdef CONFIG_TRANSPARENT_HUGEPAGE
7648c2ecf20Sopenharmony_cistatic void __init pmd_soft_dirty_tests(unsigned long pfn, pgprot_t prot)
7658c2ecf20Sopenharmony_ci{
7668c2ecf20Sopenharmony_ci	pmd_t pmd;
7678c2ecf20Sopenharmony_ci
7688c2ecf20Sopenharmony_ci	if (!IS_ENABLED(CONFIG_MEM_SOFT_DIRTY))
7698c2ecf20Sopenharmony_ci		return;
7708c2ecf20Sopenharmony_ci
7718c2ecf20Sopenharmony_ci	if (!has_transparent_hugepage())
7728c2ecf20Sopenharmony_ci		return;
7738c2ecf20Sopenharmony_ci
7748c2ecf20Sopenharmony_ci	pr_debug("Validating PMD soft dirty\n");
7758c2ecf20Sopenharmony_ci	pmd = pfn_pmd(pfn, prot);
7768c2ecf20Sopenharmony_ci	WARN_ON(!pmd_soft_dirty(pmd_mksoft_dirty(pmd)));
7778c2ecf20Sopenharmony_ci	WARN_ON(pmd_soft_dirty(pmd_clear_soft_dirty(pmd)));
7788c2ecf20Sopenharmony_ci}
7798c2ecf20Sopenharmony_ci
7808c2ecf20Sopenharmony_cistatic void __init pmd_swap_soft_dirty_tests(unsigned long pfn, pgprot_t prot)
7818c2ecf20Sopenharmony_ci{
7828c2ecf20Sopenharmony_ci	pmd_t pmd;
7838c2ecf20Sopenharmony_ci
7848c2ecf20Sopenharmony_ci	if (!IS_ENABLED(CONFIG_MEM_SOFT_DIRTY) ||
7858c2ecf20Sopenharmony_ci		!IS_ENABLED(CONFIG_ARCH_ENABLE_THP_MIGRATION))
7868c2ecf20Sopenharmony_ci		return;
7878c2ecf20Sopenharmony_ci
7888c2ecf20Sopenharmony_ci	if (!has_transparent_hugepage())
7898c2ecf20Sopenharmony_ci		return;
7908c2ecf20Sopenharmony_ci
7918c2ecf20Sopenharmony_ci	pr_debug("Validating PMD swap soft dirty\n");
7928c2ecf20Sopenharmony_ci	pmd = pfn_pmd(pfn, prot);
7938c2ecf20Sopenharmony_ci	WARN_ON(!pmd_swp_soft_dirty(pmd_swp_mksoft_dirty(pmd)));
7948c2ecf20Sopenharmony_ci	WARN_ON(pmd_swp_soft_dirty(pmd_swp_clear_soft_dirty(pmd)));
7958c2ecf20Sopenharmony_ci}
7968c2ecf20Sopenharmony_ci#else  /* !CONFIG_ARCH_HAS_PTE_DEVMAP */
7978c2ecf20Sopenharmony_cistatic void __init pmd_soft_dirty_tests(unsigned long pfn, pgprot_t prot) { }
7988c2ecf20Sopenharmony_cistatic void __init pmd_swap_soft_dirty_tests(unsigned long pfn, pgprot_t prot)
7998c2ecf20Sopenharmony_ci{
8008c2ecf20Sopenharmony_ci}
8018c2ecf20Sopenharmony_ci#endif /* CONFIG_ARCH_HAS_PTE_DEVMAP */
8028c2ecf20Sopenharmony_ci
8038c2ecf20Sopenharmony_cistatic void __init pte_swap_tests(unsigned long pfn, pgprot_t prot)
8048c2ecf20Sopenharmony_ci{
8058c2ecf20Sopenharmony_ci	swp_entry_t swp;
8068c2ecf20Sopenharmony_ci	pte_t pte;
8078c2ecf20Sopenharmony_ci
8088c2ecf20Sopenharmony_ci	pr_debug("Validating PTE swap\n");
8098c2ecf20Sopenharmony_ci	pte = pfn_pte(pfn, prot);
8108c2ecf20Sopenharmony_ci	swp = __pte_to_swp_entry(pte);
8118c2ecf20Sopenharmony_ci	pte = __swp_entry_to_pte(swp);
8128c2ecf20Sopenharmony_ci	WARN_ON(pfn != pte_pfn(pte));
8138c2ecf20Sopenharmony_ci}
8148c2ecf20Sopenharmony_ci
8158c2ecf20Sopenharmony_ci#ifdef CONFIG_ARCH_ENABLE_THP_MIGRATION
8168c2ecf20Sopenharmony_cistatic void __init pmd_swap_tests(unsigned long pfn, pgprot_t prot)
8178c2ecf20Sopenharmony_ci{
8188c2ecf20Sopenharmony_ci	swp_entry_t swp;
8198c2ecf20Sopenharmony_ci	pmd_t pmd;
8208c2ecf20Sopenharmony_ci
8218c2ecf20Sopenharmony_ci	if (!has_transparent_hugepage())
8228c2ecf20Sopenharmony_ci		return;
8238c2ecf20Sopenharmony_ci
8248c2ecf20Sopenharmony_ci	pr_debug("Validating PMD swap\n");
8258c2ecf20Sopenharmony_ci	pmd = pfn_pmd(pfn, prot);
8268c2ecf20Sopenharmony_ci	swp = __pmd_to_swp_entry(pmd);
8278c2ecf20Sopenharmony_ci	pmd = __swp_entry_to_pmd(swp);
8288c2ecf20Sopenharmony_ci	WARN_ON(pfn != pmd_pfn(pmd));
8298c2ecf20Sopenharmony_ci}
8308c2ecf20Sopenharmony_ci#else  /* !CONFIG_ARCH_ENABLE_THP_MIGRATION */
8318c2ecf20Sopenharmony_cistatic void __init pmd_swap_tests(unsigned long pfn, pgprot_t prot) { }
8328c2ecf20Sopenharmony_ci#endif /* CONFIG_ARCH_ENABLE_THP_MIGRATION */
8338c2ecf20Sopenharmony_ci
8348c2ecf20Sopenharmony_cistatic void __init swap_migration_tests(void)
8358c2ecf20Sopenharmony_ci{
8368c2ecf20Sopenharmony_ci	struct page *page;
8378c2ecf20Sopenharmony_ci	swp_entry_t swp;
8388c2ecf20Sopenharmony_ci
8398c2ecf20Sopenharmony_ci	if (!IS_ENABLED(CONFIG_MIGRATION))
8408c2ecf20Sopenharmony_ci		return;
8418c2ecf20Sopenharmony_ci
8428c2ecf20Sopenharmony_ci	pr_debug("Validating swap migration\n");
8438c2ecf20Sopenharmony_ci	/*
8448c2ecf20Sopenharmony_ci	 * swap_migration_tests() requires a dedicated page as it needs to
8458c2ecf20Sopenharmony_ci	 * be locked before creating a migration entry from it. Locking the
8468c2ecf20Sopenharmony_ci	 * page that actually maps kernel text ('start_kernel') can be real
8478c2ecf20Sopenharmony_ci	 * problematic. Lets allocate a dedicated page explicitly for this
8488c2ecf20Sopenharmony_ci	 * purpose that will be freed subsequently.
8498c2ecf20Sopenharmony_ci	 */
8508c2ecf20Sopenharmony_ci	page = alloc_page(GFP_KERNEL);
8518c2ecf20Sopenharmony_ci	if (!page) {
8528c2ecf20Sopenharmony_ci		pr_err("page allocation failed\n");
8538c2ecf20Sopenharmony_ci		return;
8548c2ecf20Sopenharmony_ci	}
8558c2ecf20Sopenharmony_ci
8568c2ecf20Sopenharmony_ci	/*
8578c2ecf20Sopenharmony_ci	 * make_migration_entry() expects given page to be
8588c2ecf20Sopenharmony_ci	 * locked, otherwise it stumbles upon a BUG_ON().
8598c2ecf20Sopenharmony_ci	 */
8608c2ecf20Sopenharmony_ci	__SetPageLocked(page);
8618c2ecf20Sopenharmony_ci	swp = make_migration_entry(page, 1);
8628c2ecf20Sopenharmony_ci	WARN_ON(!is_migration_entry(swp));
8638c2ecf20Sopenharmony_ci	WARN_ON(!is_write_migration_entry(swp));
8648c2ecf20Sopenharmony_ci
8658c2ecf20Sopenharmony_ci	make_migration_entry_read(&swp);
8668c2ecf20Sopenharmony_ci	WARN_ON(!is_migration_entry(swp));
8678c2ecf20Sopenharmony_ci	WARN_ON(is_write_migration_entry(swp));
8688c2ecf20Sopenharmony_ci
8698c2ecf20Sopenharmony_ci	swp = make_migration_entry(page, 0);
8708c2ecf20Sopenharmony_ci	WARN_ON(!is_migration_entry(swp));
8718c2ecf20Sopenharmony_ci	WARN_ON(is_write_migration_entry(swp));
8728c2ecf20Sopenharmony_ci	__ClearPageLocked(page);
8738c2ecf20Sopenharmony_ci	__free_page(page);
8748c2ecf20Sopenharmony_ci}
8758c2ecf20Sopenharmony_ci
8768c2ecf20Sopenharmony_ci#ifdef CONFIG_HUGETLB_PAGE
8778c2ecf20Sopenharmony_cistatic void __init hugetlb_basic_tests(unsigned long pfn, pgprot_t prot)
8788c2ecf20Sopenharmony_ci{
8798c2ecf20Sopenharmony_ci	struct page *page;
8808c2ecf20Sopenharmony_ci	pte_t pte;
8818c2ecf20Sopenharmony_ci
8828c2ecf20Sopenharmony_ci	pr_debug("Validating HugeTLB basic\n");
8838c2ecf20Sopenharmony_ci	/*
8848c2ecf20Sopenharmony_ci	 * Accessing the page associated with the pfn is safe here,
8858c2ecf20Sopenharmony_ci	 * as it was previously derived from a real kernel symbol.
8868c2ecf20Sopenharmony_ci	 */
8878c2ecf20Sopenharmony_ci	page = pfn_to_page(pfn);
8888c2ecf20Sopenharmony_ci	pte = mk_huge_pte(page, prot);
8898c2ecf20Sopenharmony_ci
8908c2ecf20Sopenharmony_ci	WARN_ON(!huge_pte_dirty(huge_pte_mkdirty(pte)));
8918c2ecf20Sopenharmony_ci	WARN_ON(!huge_pte_write(huge_pte_mkwrite(huge_pte_wrprotect(pte))));
8928c2ecf20Sopenharmony_ci	WARN_ON(huge_pte_write(huge_pte_wrprotect(huge_pte_mkwrite(pte))));
8938c2ecf20Sopenharmony_ci
8948c2ecf20Sopenharmony_ci#ifdef CONFIG_ARCH_WANT_GENERAL_HUGETLB
8958c2ecf20Sopenharmony_ci	pte = pfn_pte(pfn, prot);
8968c2ecf20Sopenharmony_ci
8978c2ecf20Sopenharmony_ci	WARN_ON(!pte_huge(pte_mkhuge(pte)));
8988c2ecf20Sopenharmony_ci#endif /* CONFIG_ARCH_WANT_GENERAL_HUGETLB */
8998c2ecf20Sopenharmony_ci}
9008c2ecf20Sopenharmony_ci#else  /* !CONFIG_HUGETLB_PAGE */
9018c2ecf20Sopenharmony_cistatic void __init hugetlb_basic_tests(unsigned long pfn, pgprot_t prot) { }
9028c2ecf20Sopenharmony_ci#endif /* CONFIG_HUGETLB_PAGE */
9038c2ecf20Sopenharmony_ci
9048c2ecf20Sopenharmony_ci#ifdef CONFIG_TRANSPARENT_HUGEPAGE
9058c2ecf20Sopenharmony_cistatic void __init pmd_thp_tests(unsigned long pfn, pgprot_t prot)
9068c2ecf20Sopenharmony_ci{
9078c2ecf20Sopenharmony_ci	pmd_t pmd;
9088c2ecf20Sopenharmony_ci
9098c2ecf20Sopenharmony_ci	if (!has_transparent_hugepage())
9108c2ecf20Sopenharmony_ci		return;
9118c2ecf20Sopenharmony_ci
9128c2ecf20Sopenharmony_ci	pr_debug("Validating PMD based THP\n");
9138c2ecf20Sopenharmony_ci	/*
9148c2ecf20Sopenharmony_ci	 * pmd_trans_huge() and pmd_present() must return positive after
9158c2ecf20Sopenharmony_ci	 * MMU invalidation with pmd_mkinvalid(). This behavior is an
9168c2ecf20Sopenharmony_ci	 * optimization for transparent huge page. pmd_trans_huge() must
9178c2ecf20Sopenharmony_ci	 * be true if pmd_page() returns a valid THP to avoid taking the
9188c2ecf20Sopenharmony_ci	 * pmd_lock when others walk over non transhuge pmds (i.e. there
9198c2ecf20Sopenharmony_ci	 * are no THP allocated). Especially when splitting a THP and
9208c2ecf20Sopenharmony_ci	 * removing the present bit from the pmd, pmd_trans_huge() still
9218c2ecf20Sopenharmony_ci	 * needs to return true. pmd_present() should be true whenever
9228c2ecf20Sopenharmony_ci	 * pmd_trans_huge() returns true.
9238c2ecf20Sopenharmony_ci	 */
9248c2ecf20Sopenharmony_ci	pmd = pfn_pmd(pfn, prot);
9258c2ecf20Sopenharmony_ci	WARN_ON(!pmd_trans_huge(pmd_mkhuge(pmd)));
9268c2ecf20Sopenharmony_ci
9278c2ecf20Sopenharmony_ci#ifndef __HAVE_ARCH_PMDP_INVALIDATE
9288c2ecf20Sopenharmony_ci	WARN_ON(!pmd_trans_huge(pmd_mkinvalid(pmd_mkhuge(pmd))));
9298c2ecf20Sopenharmony_ci	WARN_ON(!pmd_present(pmd_mkinvalid(pmd_mkhuge(pmd))));
9308c2ecf20Sopenharmony_ci#endif /* __HAVE_ARCH_PMDP_INVALIDATE */
9318c2ecf20Sopenharmony_ci}
9328c2ecf20Sopenharmony_ci
9338c2ecf20Sopenharmony_ci#ifdef CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD
9348c2ecf20Sopenharmony_cistatic void __init pud_thp_tests(unsigned long pfn, pgprot_t prot)
9358c2ecf20Sopenharmony_ci{
9368c2ecf20Sopenharmony_ci	pud_t pud;
9378c2ecf20Sopenharmony_ci
9388c2ecf20Sopenharmony_ci	if (!has_transparent_hugepage())
9398c2ecf20Sopenharmony_ci		return;
9408c2ecf20Sopenharmony_ci
9418c2ecf20Sopenharmony_ci	pr_debug("Validating PUD based THP\n");
9428c2ecf20Sopenharmony_ci	pud = pfn_pud(pfn, prot);
9438c2ecf20Sopenharmony_ci	WARN_ON(!pud_trans_huge(pud_mkhuge(pud)));
9448c2ecf20Sopenharmony_ci
9458c2ecf20Sopenharmony_ci	/*
9468c2ecf20Sopenharmony_ci	 * pud_mkinvalid() has been dropped for now. Enable back
9478c2ecf20Sopenharmony_ci	 * these tests when it comes back with a modified pud_present().
9488c2ecf20Sopenharmony_ci	 *
9498c2ecf20Sopenharmony_ci	 * WARN_ON(!pud_trans_huge(pud_mkinvalid(pud_mkhuge(pud))));
9508c2ecf20Sopenharmony_ci	 * WARN_ON(!pud_present(pud_mkinvalid(pud_mkhuge(pud))));
9518c2ecf20Sopenharmony_ci	 */
9528c2ecf20Sopenharmony_ci}
9538c2ecf20Sopenharmony_ci#else  /* !CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD */
9548c2ecf20Sopenharmony_cistatic void __init pud_thp_tests(unsigned long pfn, pgprot_t prot) { }
9558c2ecf20Sopenharmony_ci#endif /* CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD */
9568c2ecf20Sopenharmony_ci#else  /* !CONFIG_TRANSPARENT_HUGEPAGE */
9578c2ecf20Sopenharmony_cistatic void __init pmd_thp_tests(unsigned long pfn, pgprot_t prot) { }
9588c2ecf20Sopenharmony_cistatic void __init pud_thp_tests(unsigned long pfn, pgprot_t prot) { }
9598c2ecf20Sopenharmony_ci#endif /* CONFIG_TRANSPARENT_HUGEPAGE */
9608c2ecf20Sopenharmony_ci
9618c2ecf20Sopenharmony_cistatic unsigned long __init get_random_vaddr(void)
9628c2ecf20Sopenharmony_ci{
9638c2ecf20Sopenharmony_ci	unsigned long random_vaddr, random_pages, total_user_pages;
9648c2ecf20Sopenharmony_ci
9658c2ecf20Sopenharmony_ci	total_user_pages = (TASK_SIZE - FIRST_USER_ADDRESS) / PAGE_SIZE;
9668c2ecf20Sopenharmony_ci
9678c2ecf20Sopenharmony_ci	random_pages = get_random_long() % total_user_pages;
9688c2ecf20Sopenharmony_ci	random_vaddr = FIRST_USER_ADDRESS + random_pages * PAGE_SIZE;
9698c2ecf20Sopenharmony_ci
9708c2ecf20Sopenharmony_ci	return random_vaddr;
9718c2ecf20Sopenharmony_ci}
9728c2ecf20Sopenharmony_ci
9738c2ecf20Sopenharmony_cistatic int __init debug_vm_pgtable(void)
9748c2ecf20Sopenharmony_ci{
9758c2ecf20Sopenharmony_ci	struct vm_area_struct *vma;
9768c2ecf20Sopenharmony_ci	struct mm_struct *mm;
9778c2ecf20Sopenharmony_ci	pgd_t *pgdp;
9788c2ecf20Sopenharmony_ci	p4d_t *p4dp, *saved_p4dp;
9798c2ecf20Sopenharmony_ci	pud_t *pudp, *saved_pudp;
9808c2ecf20Sopenharmony_ci	pmd_t *pmdp, *saved_pmdp, pmd;
9818c2ecf20Sopenharmony_ci	pte_t *ptep;
9828c2ecf20Sopenharmony_ci	pgtable_t saved_ptep;
9838c2ecf20Sopenharmony_ci	pgprot_t prot, protnone;
9848c2ecf20Sopenharmony_ci	phys_addr_t paddr;
9858c2ecf20Sopenharmony_ci	unsigned long vaddr, pte_aligned, pmd_aligned;
9868c2ecf20Sopenharmony_ci	unsigned long pud_aligned, p4d_aligned, pgd_aligned;
9878c2ecf20Sopenharmony_ci	spinlock_t *ptl = NULL;
9888c2ecf20Sopenharmony_ci	int idx;
9898c2ecf20Sopenharmony_ci
9908c2ecf20Sopenharmony_ci	pr_info("Validating architecture page table helpers\n");
9918c2ecf20Sopenharmony_ci	prot = vm_get_page_prot(VMFLAGS);
9928c2ecf20Sopenharmony_ci	vaddr = get_random_vaddr();
9938c2ecf20Sopenharmony_ci	mm = mm_alloc();
9948c2ecf20Sopenharmony_ci	if (!mm) {
9958c2ecf20Sopenharmony_ci		pr_err("mm_struct allocation failed\n");
9968c2ecf20Sopenharmony_ci		return 1;
9978c2ecf20Sopenharmony_ci	}
9988c2ecf20Sopenharmony_ci
9998c2ecf20Sopenharmony_ci	/*
10008c2ecf20Sopenharmony_ci	 * __P000 (or even __S000) will help create page table entries with
10018c2ecf20Sopenharmony_ci	 * PROT_NONE permission as required for pxx_protnone_tests().
10028c2ecf20Sopenharmony_ci	 */
10038c2ecf20Sopenharmony_ci	protnone = __P000;
10048c2ecf20Sopenharmony_ci
10058c2ecf20Sopenharmony_ci	vma = vm_area_alloc(mm);
10068c2ecf20Sopenharmony_ci	if (!vma) {
10078c2ecf20Sopenharmony_ci		pr_err("vma allocation failed\n");
10088c2ecf20Sopenharmony_ci		return 1;
10098c2ecf20Sopenharmony_ci	}
10108c2ecf20Sopenharmony_ci
10118c2ecf20Sopenharmony_ci	/*
10128c2ecf20Sopenharmony_ci	 * PFN for mapping at PTE level is determined from a standard kernel
10138c2ecf20Sopenharmony_ci	 * text symbol. But pfns for higher page table levels are derived by
10148c2ecf20Sopenharmony_ci	 * masking lower bits of this real pfn. These derived pfns might not
10158c2ecf20Sopenharmony_ci	 * exist on the platform but that does not really matter as pfn_pxx()
10168c2ecf20Sopenharmony_ci	 * helpers will still create appropriate entries for the test. This
10178c2ecf20Sopenharmony_ci	 * helps avoid large memory block allocations to be used for mapping
10188c2ecf20Sopenharmony_ci	 * at higher page table levels.
10198c2ecf20Sopenharmony_ci	 */
10208c2ecf20Sopenharmony_ci	paddr = __pa_symbol(&start_kernel);
10218c2ecf20Sopenharmony_ci
10228c2ecf20Sopenharmony_ci	pte_aligned = (paddr & PAGE_MASK) >> PAGE_SHIFT;
10238c2ecf20Sopenharmony_ci	pmd_aligned = (paddr & PMD_MASK) >> PAGE_SHIFT;
10248c2ecf20Sopenharmony_ci	pud_aligned = (paddr & PUD_MASK) >> PAGE_SHIFT;
10258c2ecf20Sopenharmony_ci	p4d_aligned = (paddr & P4D_MASK) >> PAGE_SHIFT;
10268c2ecf20Sopenharmony_ci	pgd_aligned = (paddr & PGDIR_MASK) >> PAGE_SHIFT;
10278c2ecf20Sopenharmony_ci	WARN_ON(!pfn_valid(pte_aligned));
10288c2ecf20Sopenharmony_ci
10298c2ecf20Sopenharmony_ci	pgdp = pgd_offset(mm, vaddr);
10308c2ecf20Sopenharmony_ci	p4dp = p4d_alloc(mm, pgdp, vaddr);
10318c2ecf20Sopenharmony_ci	pudp = pud_alloc(mm, p4dp, vaddr);
10328c2ecf20Sopenharmony_ci	pmdp = pmd_alloc(mm, pudp, vaddr);
10338c2ecf20Sopenharmony_ci	/*
10348c2ecf20Sopenharmony_ci	 * Allocate pgtable_t
10358c2ecf20Sopenharmony_ci	 */
10368c2ecf20Sopenharmony_ci	if (pte_alloc(mm, pmdp)) {
10378c2ecf20Sopenharmony_ci		pr_err("pgtable allocation failed\n");
10388c2ecf20Sopenharmony_ci		return 1;
10398c2ecf20Sopenharmony_ci	}
10408c2ecf20Sopenharmony_ci
10418c2ecf20Sopenharmony_ci	/*
10428c2ecf20Sopenharmony_ci	 * Save all the page table page addresses as the page table
10438c2ecf20Sopenharmony_ci	 * entries will be used for testing with random or garbage
10448c2ecf20Sopenharmony_ci	 * values. These saved addresses will be used for freeing
10458c2ecf20Sopenharmony_ci	 * page table pages.
10468c2ecf20Sopenharmony_ci	 */
10478c2ecf20Sopenharmony_ci	pmd = READ_ONCE(*pmdp);
10488c2ecf20Sopenharmony_ci	saved_p4dp = p4d_offset(pgdp, 0UL);
10498c2ecf20Sopenharmony_ci	saved_pudp = pud_offset(p4dp, 0UL);
10508c2ecf20Sopenharmony_ci	saved_pmdp = pmd_offset(pudp, 0UL);
10518c2ecf20Sopenharmony_ci	saved_ptep = pmd_pgtable(pmd);
10528c2ecf20Sopenharmony_ci
10538c2ecf20Sopenharmony_ci	/*
10548c2ecf20Sopenharmony_ci	 * Iterate over the protection_map[] to make sure that all
10558c2ecf20Sopenharmony_ci	 * the basic page table transformation validations just hold
10568c2ecf20Sopenharmony_ci	 * true irrespective of the starting protection value for a
10578c2ecf20Sopenharmony_ci	 * given page table entry.
10588c2ecf20Sopenharmony_ci	 */
10598c2ecf20Sopenharmony_ci	for (idx = 0; idx < ARRAY_SIZE(protection_map); idx++) {
10608c2ecf20Sopenharmony_ci		pte_basic_tests(pte_aligned, idx);
10618c2ecf20Sopenharmony_ci		pmd_basic_tests(pmd_aligned, idx);
10628c2ecf20Sopenharmony_ci		pud_basic_tests(mm, pud_aligned, idx);
10638c2ecf20Sopenharmony_ci	}
10648c2ecf20Sopenharmony_ci
10658c2ecf20Sopenharmony_ci	/*
10668c2ecf20Sopenharmony_ci	 * Both P4D and PGD level tests are very basic which do not
10678c2ecf20Sopenharmony_ci	 * involve creating page table entries from the protection
10688c2ecf20Sopenharmony_ci	 * value and the given pfn. Hence just keep them out from
10698c2ecf20Sopenharmony_ci	 * the above iteration for now to save some test execution
10708c2ecf20Sopenharmony_ci	 * time.
10718c2ecf20Sopenharmony_ci	 */
10728c2ecf20Sopenharmony_ci	p4d_basic_tests(p4d_aligned, prot);
10738c2ecf20Sopenharmony_ci	pgd_basic_tests(pgd_aligned, prot);
10748c2ecf20Sopenharmony_ci
10758c2ecf20Sopenharmony_ci	pmd_leaf_tests(pmd_aligned, prot);
10768c2ecf20Sopenharmony_ci	pud_leaf_tests(pud_aligned, prot);
10778c2ecf20Sopenharmony_ci
10788c2ecf20Sopenharmony_ci	pte_savedwrite_tests(pte_aligned, protnone);
10798c2ecf20Sopenharmony_ci	pmd_savedwrite_tests(pmd_aligned, protnone);
10808c2ecf20Sopenharmony_ci
10818c2ecf20Sopenharmony_ci	pte_special_tests(pte_aligned, prot);
10828c2ecf20Sopenharmony_ci	pte_protnone_tests(pte_aligned, protnone);
10838c2ecf20Sopenharmony_ci	pmd_protnone_tests(pmd_aligned, protnone);
10848c2ecf20Sopenharmony_ci
10858c2ecf20Sopenharmony_ci	pte_devmap_tests(pte_aligned, prot);
10868c2ecf20Sopenharmony_ci	pmd_devmap_tests(pmd_aligned, prot);
10878c2ecf20Sopenharmony_ci	pud_devmap_tests(pud_aligned, prot);
10888c2ecf20Sopenharmony_ci
10898c2ecf20Sopenharmony_ci	pte_soft_dirty_tests(pte_aligned, prot);
10908c2ecf20Sopenharmony_ci	pmd_soft_dirty_tests(pmd_aligned, prot);
10918c2ecf20Sopenharmony_ci	pte_swap_soft_dirty_tests(pte_aligned, prot);
10928c2ecf20Sopenharmony_ci	pmd_swap_soft_dirty_tests(pmd_aligned, prot);
10938c2ecf20Sopenharmony_ci
10948c2ecf20Sopenharmony_ci	pte_swap_tests(pte_aligned, prot);
10958c2ecf20Sopenharmony_ci	pmd_swap_tests(pmd_aligned, prot);
10968c2ecf20Sopenharmony_ci
10978c2ecf20Sopenharmony_ci	swap_migration_tests();
10988c2ecf20Sopenharmony_ci
10998c2ecf20Sopenharmony_ci	pmd_thp_tests(pmd_aligned, prot);
11008c2ecf20Sopenharmony_ci	pud_thp_tests(pud_aligned, prot);
11018c2ecf20Sopenharmony_ci
11028c2ecf20Sopenharmony_ci	hugetlb_basic_tests(pte_aligned, prot);
11038c2ecf20Sopenharmony_ci
11048c2ecf20Sopenharmony_ci	/*
11058c2ecf20Sopenharmony_ci	 * Page table modifying tests. They need to hold
11068c2ecf20Sopenharmony_ci	 * proper page table lock.
11078c2ecf20Sopenharmony_ci	 */
11088c2ecf20Sopenharmony_ci
11098c2ecf20Sopenharmony_ci	ptep = pte_offset_map_lock(mm, pmdp, vaddr, &ptl);
11108c2ecf20Sopenharmony_ci	pte_clear_tests(mm, ptep, pte_aligned, vaddr, prot);
11118c2ecf20Sopenharmony_ci	pte_advanced_tests(mm, vma, ptep, pte_aligned, vaddr, prot);
11128c2ecf20Sopenharmony_ci	pte_unmap_unlock(ptep, ptl);
11138c2ecf20Sopenharmony_ci
11148c2ecf20Sopenharmony_ci	ptl = pmd_lock(mm, pmdp);
11158c2ecf20Sopenharmony_ci	pmd_clear_tests(mm, pmdp);
11168c2ecf20Sopenharmony_ci	pmd_advanced_tests(mm, vma, pmdp, pmd_aligned, vaddr, prot, saved_ptep);
11178c2ecf20Sopenharmony_ci	pmd_huge_tests(pmdp, pmd_aligned, prot);
11188c2ecf20Sopenharmony_ci	pmd_populate_tests(mm, pmdp, saved_ptep);
11198c2ecf20Sopenharmony_ci	spin_unlock(ptl);
11208c2ecf20Sopenharmony_ci
11218c2ecf20Sopenharmony_ci	ptl = pud_lock(mm, pudp);
11228c2ecf20Sopenharmony_ci	pud_clear_tests(mm, pudp);
11238c2ecf20Sopenharmony_ci	pud_advanced_tests(mm, vma, pudp, pud_aligned, vaddr, prot);
11248c2ecf20Sopenharmony_ci	pud_huge_tests(pudp, pud_aligned, prot);
11258c2ecf20Sopenharmony_ci	pud_populate_tests(mm, pudp, saved_pmdp);
11268c2ecf20Sopenharmony_ci	spin_unlock(ptl);
11278c2ecf20Sopenharmony_ci
11288c2ecf20Sopenharmony_ci	spin_lock(&mm->page_table_lock);
11298c2ecf20Sopenharmony_ci	p4d_clear_tests(mm, p4dp);
11308c2ecf20Sopenharmony_ci	pgd_clear_tests(mm, pgdp);
11318c2ecf20Sopenharmony_ci	p4d_populate_tests(mm, p4dp, saved_pudp);
11328c2ecf20Sopenharmony_ci	pgd_populate_tests(mm, pgdp, saved_p4dp);
11338c2ecf20Sopenharmony_ci	spin_unlock(&mm->page_table_lock);
11348c2ecf20Sopenharmony_ci
11358c2ecf20Sopenharmony_ci	p4d_free(mm, saved_p4dp);
11368c2ecf20Sopenharmony_ci	pud_free(mm, saved_pudp);
11378c2ecf20Sopenharmony_ci	pmd_free(mm, saved_pmdp);
11388c2ecf20Sopenharmony_ci	pte_free(mm, saved_ptep);
11398c2ecf20Sopenharmony_ci
11408c2ecf20Sopenharmony_ci	vm_area_free(vma);
11418c2ecf20Sopenharmony_ci	mm_dec_nr_puds(mm);
11428c2ecf20Sopenharmony_ci	mm_dec_nr_pmds(mm);
11438c2ecf20Sopenharmony_ci	mm_dec_nr_ptes(mm);
11448c2ecf20Sopenharmony_ci	mmdrop(mm);
11458c2ecf20Sopenharmony_ci	return 0;
11468c2ecf20Sopenharmony_ci}
11478c2ecf20Sopenharmony_cilate_initcall(debug_vm_pgtable);
1148