xref: /openbmc/linux/mm/debug_vm_pgtable.c (revision ee65728e103bb7dd99d8604bf6c7aa89c7d7e446)
1399145f9SAnshuman Khandual // SPDX-License-Identifier: GPL-2.0-only
2399145f9SAnshuman Khandual /*
3399145f9SAnshuman Khandual  * This kernel test validates architecture page table helpers and
4399145f9SAnshuman Khandual  * accessors and helps in verifying their continued compliance with
5399145f9SAnshuman Khandual  * expected generic MM semantics.
6399145f9SAnshuman Khandual  *
7399145f9SAnshuman Khandual  * Copyright (C) 2019 ARM Ltd.
8399145f9SAnshuman Khandual  *
9399145f9SAnshuman Khandual  * Author: Anshuman Khandual <anshuman.khandual@arm.com>
10399145f9SAnshuman Khandual  */
116315df41SAnshuman Khandual #define pr_fmt(fmt) "debug_vm_pgtable: [%-25s]: " fmt, __func__
12399145f9SAnshuman Khandual 
13399145f9SAnshuman Khandual #include <linux/gfp.h>
14399145f9SAnshuman Khandual #include <linux/highmem.h>
15399145f9SAnshuman Khandual #include <linux/hugetlb.h>
16399145f9SAnshuman Khandual #include <linux/kernel.h>
17399145f9SAnshuman Khandual #include <linux/kconfig.h>
18399145f9SAnshuman Khandual #include <linux/mm.h>
19399145f9SAnshuman Khandual #include <linux/mman.h>
20399145f9SAnshuman Khandual #include <linux/mm_types.h>
21399145f9SAnshuman Khandual #include <linux/module.h>
22399145f9SAnshuman Khandual #include <linux/pfn_t.h>
23399145f9SAnshuman Khandual #include <linux/printk.h>
24a5c3b9ffSAnshuman Khandual #include <linux/pgtable.h>
25399145f9SAnshuman Khandual #include <linux/random.h>
26399145f9SAnshuman Khandual #include <linux/spinlock.h>
27399145f9SAnshuman Khandual #include <linux/swap.h>
28399145f9SAnshuman Khandual #include <linux/swapops.h>
29399145f9SAnshuman Khandual #include <linux/start_kernel.h>
30399145f9SAnshuman Khandual #include <linux/sched/mm.h>
3185a14463SAneesh Kumar K.V #include <linux/io.h>
328c5b3a8aSGavin Shan 
338c5b3a8aSGavin Shan #include <asm/cacheflush.h>
34399145f9SAnshuman Khandual #include <asm/pgalloc.h>
35a5c3b9ffSAnshuman Khandual #include <asm/tlbflush.h>
36399145f9SAnshuman Khandual 
37b1d00007SAnshuman Khandual /*
38*ee65728eSMike Rapoport  * Please refer Documentation/mm/arch_pgtable_helpers.rst for the semantics
39b1d00007SAnshuman Khandual  * expectations that are being validated here. All future changes in here
40b1d00007SAnshuman Khandual  * or the documentation need to be in sync.
41b1d00007SAnshuman Khandual  */
42b1d00007SAnshuman Khandual 
43399145f9SAnshuman Khandual #define VMFLAGS	(VM_READ|VM_WRITE|VM_EXEC)
44399145f9SAnshuman Khandual 
45399145f9SAnshuman Khandual /*
46399145f9SAnshuman Khandual  * On s390 platform, the lower 4 bits are used to identify given page table
47399145f9SAnshuman Khandual  * entry type. But these bits might affect the ability to clear entries with
48399145f9SAnshuman Khandual  * pxx_clear() because of how dynamic page table folding works on s390. So
49399145f9SAnshuman Khandual  * while loading up the entries do not change the lower 4 bits. It does not
50cfc5bbc4SAneesh Kumar K.V  * have affect any other platform. Also avoid the 62nd bit on ppc64 that is
51cfc5bbc4SAneesh Kumar K.V  * used to mark a pte entry.
52399145f9SAnshuman Khandual  */
53cfc5bbc4SAneesh Kumar K.V #define S390_SKIP_MASK		GENMASK(3, 0)
54cfc5bbc4SAneesh Kumar K.V #if __BITS_PER_LONG == 64
55cfc5bbc4SAneesh Kumar K.V #define PPC64_SKIP_MASK		GENMASK(62, 62)
56cfc5bbc4SAneesh Kumar K.V #else
57cfc5bbc4SAneesh Kumar K.V #define PPC64_SKIP_MASK		0x0
58cfc5bbc4SAneesh Kumar K.V #endif
59cfc5bbc4SAneesh Kumar K.V #define ARCH_SKIP_MASK (S390_SKIP_MASK | PPC64_SKIP_MASK)
60cfc5bbc4SAneesh Kumar K.V #define RANDOM_ORVALUE (GENMASK(BITS_PER_LONG - 1, 0) & ~ARCH_SKIP_MASK)
61399145f9SAnshuman Khandual #define RANDOM_NZVALUE	GENMASK(7, 0)
62399145f9SAnshuman Khandual 
633c9b84f0SGavin Shan struct pgtable_debug_args {
643c9b84f0SGavin Shan 	struct mm_struct	*mm;
653c9b84f0SGavin Shan 	struct vm_area_struct	*vma;
663c9b84f0SGavin Shan 
673c9b84f0SGavin Shan 	pgd_t			*pgdp;
683c9b84f0SGavin Shan 	p4d_t			*p4dp;
693c9b84f0SGavin Shan 	pud_t			*pudp;
703c9b84f0SGavin Shan 	pmd_t			*pmdp;
713c9b84f0SGavin Shan 	pte_t			*ptep;
723c9b84f0SGavin Shan 
733c9b84f0SGavin Shan 	p4d_t			*start_p4dp;
743c9b84f0SGavin Shan 	pud_t			*start_pudp;
753c9b84f0SGavin Shan 	pmd_t			*start_pmdp;
763c9b84f0SGavin Shan 	pgtable_t		start_ptep;
773c9b84f0SGavin Shan 
783c9b84f0SGavin Shan 	unsigned long		vaddr;
793c9b84f0SGavin Shan 	pgprot_t		page_prot;
803c9b84f0SGavin Shan 	pgprot_t		page_prot_none;
813c9b84f0SGavin Shan 
823c9b84f0SGavin Shan 	bool			is_contiguous_page;
833c9b84f0SGavin Shan 	unsigned long		pud_pfn;
843c9b84f0SGavin Shan 	unsigned long		pmd_pfn;
853c9b84f0SGavin Shan 	unsigned long		pte_pfn;
863c9b84f0SGavin Shan 
873c9b84f0SGavin Shan 	unsigned long		fixed_pgd_pfn;
883c9b84f0SGavin Shan 	unsigned long		fixed_p4d_pfn;
893c9b84f0SGavin Shan 	unsigned long		fixed_pud_pfn;
903c9b84f0SGavin Shan 	unsigned long		fixed_pmd_pfn;
913c9b84f0SGavin Shan 	unsigned long		fixed_pte_pfn;
923c9b84f0SGavin Shan };
933c9b84f0SGavin Shan 
9436b77d1eSGavin Shan static void __init pte_basic_tests(struct pgtable_debug_args *args, int idx)
95399145f9SAnshuman Khandual {
9631d17076SAnshuman Khandual 	pgprot_t prot = vm_get_page_prot(idx);
9736b77d1eSGavin Shan 	pte_t pte = pfn_pte(args->fixed_pte_pfn, prot);
982e326c07SAnshuman Khandual 	unsigned long val = idx, *ptr = &val;
99399145f9SAnshuman Khandual 
1002e326c07SAnshuman Khandual 	pr_debug("Validating PTE basic (%pGv)\n", ptr);
101bb5c47ceSAnshuman Khandual 
102bb5c47ceSAnshuman Khandual 	/*
103bb5c47ceSAnshuman Khandual 	 * This test needs to be executed after the given page table entry
10431d17076SAnshuman Khandual 	 * is created with pfn_pte() to make sure that vm_get_page_prot(idx)
105bb5c47ceSAnshuman Khandual 	 * does not have the dirty bit enabled from the beginning. This is
106bb5c47ceSAnshuman Khandual 	 * important for platforms like arm64 where (!PTE_RDONLY) indicate
107bb5c47ceSAnshuman Khandual 	 * dirty bit being set.
108bb5c47ceSAnshuman Khandual 	 */
109bb5c47ceSAnshuman Khandual 	WARN_ON(pte_dirty(pte_wrprotect(pte)));
110bb5c47ceSAnshuman Khandual 
111399145f9SAnshuman Khandual 	WARN_ON(!pte_same(pte, pte));
112399145f9SAnshuman Khandual 	WARN_ON(!pte_young(pte_mkyoung(pte_mkold(pte))));
113399145f9SAnshuman Khandual 	WARN_ON(!pte_dirty(pte_mkdirty(pte_mkclean(pte))));
114399145f9SAnshuman Khandual 	WARN_ON(!pte_write(pte_mkwrite(pte_wrprotect(pte))));
115399145f9SAnshuman Khandual 	WARN_ON(pte_young(pte_mkold(pte_mkyoung(pte))));
116399145f9SAnshuman Khandual 	WARN_ON(pte_dirty(pte_mkclean(pte_mkdirty(pte))));
117399145f9SAnshuman Khandual 	WARN_ON(pte_write(pte_wrprotect(pte_mkwrite(pte))));
118bb5c47ceSAnshuman Khandual 	WARN_ON(pte_dirty(pte_wrprotect(pte_mkclean(pte))));
119bb5c47ceSAnshuman Khandual 	WARN_ON(!pte_dirty(pte_wrprotect(pte_mkdirty(pte))));
120399145f9SAnshuman Khandual }
121399145f9SAnshuman Khandual 
12244966c44SGavin Shan static void __init pte_advanced_tests(struct pgtable_debug_args *args)
123a5c3b9ffSAnshuman Khandual {
1248c5b3a8aSGavin Shan 	struct page *page;
125b593b90dSShixin Liu 	pte_t pte;
126a5c3b9ffSAnshuman Khandual 
127c3824e18SAneesh Kumar K.V 	/*
128c3824e18SAneesh Kumar K.V 	 * Architectures optimize set_pte_at by avoiding TLB flush.
129c3824e18SAneesh Kumar K.V 	 * This requires set_pte_at to be not used to update an
130c3824e18SAneesh Kumar K.V 	 * existing pte entry. Clear pte before we do set_pte_at
1318c5b3a8aSGavin Shan 	 *
1328c5b3a8aSGavin Shan 	 * flush_dcache_page() is called after set_pte_at() to clear
1338c5b3a8aSGavin Shan 	 * PG_arch_1 for the page on ARM64. The page flag isn't cleared
1348c5b3a8aSGavin Shan 	 * when it's released and page allocation check will fail when
1358c5b3a8aSGavin Shan 	 * the page is allocated again. For architectures other than ARM64,
1368c5b3a8aSGavin Shan 	 * the unexpected overhead of cache flushing is acceptable.
137c3824e18SAneesh Kumar K.V 	 */
1388c5b3a8aSGavin Shan 	page = (args->pte_pfn != ULONG_MAX) ? pfn_to_page(args->pte_pfn) : NULL;
1398c5b3a8aSGavin Shan 	if (!page)
14044966c44SGavin Shan 		return;
141c3824e18SAneesh Kumar K.V 
1426315df41SAnshuman Khandual 	pr_debug("Validating PTE advanced\n");
14344966c44SGavin Shan 	pte = pfn_pte(args->pte_pfn, args->page_prot);
14444966c44SGavin Shan 	set_pte_at(args->mm, args->vaddr, args->ptep, pte);
1458c5b3a8aSGavin Shan 	flush_dcache_page(page);
14644966c44SGavin Shan 	ptep_set_wrprotect(args->mm, args->vaddr, args->ptep);
14744966c44SGavin Shan 	pte = ptep_get(args->ptep);
148a5c3b9ffSAnshuman Khandual 	WARN_ON(pte_write(pte));
14944966c44SGavin Shan 	ptep_get_and_clear(args->mm, args->vaddr, args->ptep);
15044966c44SGavin Shan 	pte = ptep_get(args->ptep);
151a5c3b9ffSAnshuman Khandual 	WARN_ON(!pte_none(pte));
152a5c3b9ffSAnshuman Khandual 
15344966c44SGavin Shan 	pte = pfn_pte(args->pte_pfn, args->page_prot);
154a5c3b9ffSAnshuman Khandual 	pte = pte_wrprotect(pte);
155a5c3b9ffSAnshuman Khandual 	pte = pte_mkclean(pte);
15644966c44SGavin Shan 	set_pte_at(args->mm, args->vaddr, args->ptep, pte);
1578c5b3a8aSGavin Shan 	flush_dcache_page(page);
158a5c3b9ffSAnshuman Khandual 	pte = pte_mkwrite(pte);
159a5c3b9ffSAnshuman Khandual 	pte = pte_mkdirty(pte);
16044966c44SGavin Shan 	ptep_set_access_flags(args->vma, args->vaddr, args->ptep, pte, 1);
16144966c44SGavin Shan 	pte = ptep_get(args->ptep);
162a5c3b9ffSAnshuman Khandual 	WARN_ON(!(pte_write(pte) && pte_dirty(pte)));
16344966c44SGavin Shan 	ptep_get_and_clear_full(args->mm, args->vaddr, args->ptep, 1);
16444966c44SGavin Shan 	pte = ptep_get(args->ptep);
165a5c3b9ffSAnshuman Khandual 	WARN_ON(!pte_none(pte));
166a5c3b9ffSAnshuman Khandual 
16744966c44SGavin Shan 	pte = pfn_pte(args->pte_pfn, args->page_prot);
168a5c3b9ffSAnshuman Khandual 	pte = pte_mkyoung(pte);
16944966c44SGavin Shan 	set_pte_at(args->mm, args->vaddr, args->ptep, pte);
1708c5b3a8aSGavin Shan 	flush_dcache_page(page);
17144966c44SGavin Shan 	ptep_test_and_clear_young(args->vma, args->vaddr, args->ptep);
17244966c44SGavin Shan 	pte = ptep_get(args->ptep);
173a5c3b9ffSAnshuman Khandual 	WARN_ON(pte_young(pte));
174fb5222aaSPasha Tatashin 
175fb5222aaSPasha Tatashin 	ptep_get_and_clear_full(args->mm, args->vaddr, args->ptep, 1);
176a5c3b9ffSAnshuman Khandual }
177a5c3b9ffSAnshuman Khandual 
1788983d231SGavin Shan static void __init pte_savedwrite_tests(struct pgtable_debug_args *args)
179a5c3b9ffSAnshuman Khandual {
1808983d231SGavin Shan 	pte_t pte = pfn_pte(args->fixed_pte_pfn, args->page_prot_none);
181a5c3b9ffSAnshuman Khandual 
1824200605bSAneesh Kumar K.V 	if (!IS_ENABLED(CONFIG_NUMA_BALANCING))
1834200605bSAneesh Kumar K.V 		return;
1844200605bSAneesh Kumar K.V 
1856315df41SAnshuman Khandual 	pr_debug("Validating PTE saved write\n");
186a5c3b9ffSAnshuman Khandual 	WARN_ON(!pte_savedwrite(pte_mk_savedwrite(pte_clear_savedwrite(pte))));
187a5c3b9ffSAnshuman Khandual 	WARN_ON(pte_savedwrite(pte_clear_savedwrite(pte_mk_savedwrite(pte))));
188a5c3b9ffSAnshuman Khandual }
1894200605bSAneesh Kumar K.V 
190399145f9SAnshuman Khandual #ifdef CONFIG_TRANSPARENT_HUGEPAGE
19136b77d1eSGavin Shan static void __init pmd_basic_tests(struct pgtable_debug_args *args, int idx)
192399145f9SAnshuman Khandual {
19331d17076SAnshuman Khandual 	pgprot_t prot = vm_get_page_prot(idx);
1942e326c07SAnshuman Khandual 	unsigned long val = idx, *ptr = &val;
19565ac1a60SAnshuman Khandual 	pmd_t pmd;
196399145f9SAnshuman Khandual 
197787d563bSAneesh Kumar K.V 	if (!has_transparent_hugepage())
198787d563bSAneesh Kumar K.V 		return;
199787d563bSAneesh Kumar K.V 
2002e326c07SAnshuman Khandual 	pr_debug("Validating PMD basic (%pGv)\n", ptr);
20136b77d1eSGavin Shan 	pmd = pfn_pmd(args->fixed_pmd_pfn, prot);
202bb5c47ceSAnshuman Khandual 
203bb5c47ceSAnshuman Khandual 	/*
204bb5c47ceSAnshuman Khandual 	 * This test needs to be executed after the given page table entry
20531d17076SAnshuman Khandual 	 * is created with pfn_pmd() to make sure that vm_get_page_prot(idx)
206bb5c47ceSAnshuman Khandual 	 * does not have the dirty bit enabled from the beginning. This is
207bb5c47ceSAnshuman Khandual 	 * important for platforms like arm64 where (!PTE_RDONLY) indicate
208bb5c47ceSAnshuman Khandual 	 * dirty bit being set.
209bb5c47ceSAnshuman Khandual 	 */
210bb5c47ceSAnshuman Khandual 	WARN_ON(pmd_dirty(pmd_wrprotect(pmd)));
211bb5c47ceSAnshuman Khandual 
212bb5c47ceSAnshuman Khandual 
213399145f9SAnshuman Khandual 	WARN_ON(!pmd_same(pmd, pmd));
214399145f9SAnshuman Khandual 	WARN_ON(!pmd_young(pmd_mkyoung(pmd_mkold(pmd))));
215399145f9SAnshuman Khandual 	WARN_ON(!pmd_dirty(pmd_mkdirty(pmd_mkclean(pmd))));
216399145f9SAnshuman Khandual 	WARN_ON(!pmd_write(pmd_mkwrite(pmd_wrprotect(pmd))));
217399145f9SAnshuman Khandual 	WARN_ON(pmd_young(pmd_mkold(pmd_mkyoung(pmd))));
218399145f9SAnshuman Khandual 	WARN_ON(pmd_dirty(pmd_mkclean(pmd_mkdirty(pmd))));
219399145f9SAnshuman Khandual 	WARN_ON(pmd_write(pmd_wrprotect(pmd_mkwrite(pmd))));
220bb5c47ceSAnshuman Khandual 	WARN_ON(pmd_dirty(pmd_wrprotect(pmd_mkclean(pmd))));
221bb5c47ceSAnshuman Khandual 	WARN_ON(!pmd_dirty(pmd_wrprotect(pmd_mkdirty(pmd))));
222399145f9SAnshuman Khandual 	/*
223399145f9SAnshuman Khandual 	 * A huge page does not point to next level page table
224399145f9SAnshuman Khandual 	 * entry. Hence this must qualify as pmd_bad().
225399145f9SAnshuman Khandual 	 */
226399145f9SAnshuman Khandual 	WARN_ON(!pmd_bad(pmd_mkhuge(pmd)));
227399145f9SAnshuman Khandual }
228399145f9SAnshuman Khandual 
229c0fe07b0SGavin Shan static void __init pmd_advanced_tests(struct pgtable_debug_args *args)
230a5c3b9ffSAnshuman Khandual {
2318c5b3a8aSGavin Shan 	struct page *page;
23265ac1a60SAnshuman Khandual 	pmd_t pmd;
233c0fe07b0SGavin Shan 	unsigned long vaddr = args->vaddr;
234a5c3b9ffSAnshuman Khandual 
235a5c3b9ffSAnshuman Khandual 	if (!has_transparent_hugepage())
236a5c3b9ffSAnshuman Khandual 		return;
237a5c3b9ffSAnshuman Khandual 
2388c5b3a8aSGavin Shan 	page = (args->pmd_pfn != ULONG_MAX) ? pfn_to_page(args->pmd_pfn) : NULL;
2398c5b3a8aSGavin Shan 	if (!page)
240c0fe07b0SGavin Shan 		return;
241c0fe07b0SGavin Shan 
2428c5b3a8aSGavin Shan 	/*
2438c5b3a8aSGavin Shan 	 * flush_dcache_page() is called after set_pmd_at() to clear
2448c5b3a8aSGavin Shan 	 * PG_arch_1 for the page on ARM64. The page flag isn't cleared
2458c5b3a8aSGavin Shan 	 * when it's released and page allocation check will fail when
2468c5b3a8aSGavin Shan 	 * the page is allocated again. For architectures other than ARM64,
2478c5b3a8aSGavin Shan 	 * the unexpected overhead of cache flushing is acceptable.
2488c5b3a8aSGavin Shan 	 */
2496315df41SAnshuman Khandual 	pr_debug("Validating PMD advanced\n");
250a5c3b9ffSAnshuman Khandual 	/* Align the address wrt HPAGE_PMD_SIZE */
25104f7ce3fSGerald Schaefer 	vaddr &= HPAGE_PMD_MASK;
252a5c3b9ffSAnshuman Khandual 
253c0fe07b0SGavin Shan 	pgtable_trans_huge_deposit(args->mm, args->pmdp, args->start_ptep);
25487f34986SAneesh Kumar K.V 
255c0fe07b0SGavin Shan 	pmd = pfn_pmd(args->pmd_pfn, args->page_prot);
256c0fe07b0SGavin Shan 	set_pmd_at(args->mm, vaddr, args->pmdp, pmd);
2578c5b3a8aSGavin Shan 	flush_dcache_page(page);
258c0fe07b0SGavin Shan 	pmdp_set_wrprotect(args->mm, vaddr, args->pmdp);
259c0fe07b0SGavin Shan 	pmd = READ_ONCE(*args->pmdp);
260a5c3b9ffSAnshuman Khandual 	WARN_ON(pmd_write(pmd));
261c0fe07b0SGavin Shan 	pmdp_huge_get_and_clear(args->mm, vaddr, args->pmdp);
262c0fe07b0SGavin Shan 	pmd = READ_ONCE(*args->pmdp);
263a5c3b9ffSAnshuman Khandual 	WARN_ON(!pmd_none(pmd));
264a5c3b9ffSAnshuman Khandual 
265c0fe07b0SGavin Shan 	pmd = pfn_pmd(args->pmd_pfn, args->page_prot);
266a5c3b9ffSAnshuman Khandual 	pmd = pmd_wrprotect(pmd);
267a5c3b9ffSAnshuman Khandual 	pmd = pmd_mkclean(pmd);
268c0fe07b0SGavin Shan 	set_pmd_at(args->mm, vaddr, args->pmdp, pmd);
2698c5b3a8aSGavin Shan 	flush_dcache_page(page);
270a5c3b9ffSAnshuman Khandual 	pmd = pmd_mkwrite(pmd);
271a5c3b9ffSAnshuman Khandual 	pmd = pmd_mkdirty(pmd);
272c0fe07b0SGavin Shan 	pmdp_set_access_flags(args->vma, vaddr, args->pmdp, pmd, 1);
273c0fe07b0SGavin Shan 	pmd = READ_ONCE(*args->pmdp);
274a5c3b9ffSAnshuman Khandual 	WARN_ON(!(pmd_write(pmd) && pmd_dirty(pmd)));
275c0fe07b0SGavin Shan 	pmdp_huge_get_and_clear_full(args->vma, vaddr, args->pmdp, 1);
276c0fe07b0SGavin Shan 	pmd = READ_ONCE(*args->pmdp);
277a5c3b9ffSAnshuman Khandual 	WARN_ON(!pmd_none(pmd));
278a5c3b9ffSAnshuman Khandual 
279c0fe07b0SGavin Shan 	pmd = pmd_mkhuge(pfn_pmd(args->pmd_pfn, args->page_prot));
280a5c3b9ffSAnshuman Khandual 	pmd = pmd_mkyoung(pmd);
281c0fe07b0SGavin Shan 	set_pmd_at(args->mm, vaddr, args->pmdp, pmd);
2828c5b3a8aSGavin Shan 	flush_dcache_page(page);
283c0fe07b0SGavin Shan 	pmdp_test_and_clear_young(args->vma, vaddr, args->pmdp);
284c0fe07b0SGavin Shan 	pmd = READ_ONCE(*args->pmdp);
285a5c3b9ffSAnshuman Khandual 	WARN_ON(pmd_young(pmd));
28687f34986SAneesh Kumar K.V 
28713af0506SAneesh Kumar K.V 	/*  Clear the pte entries  */
288c0fe07b0SGavin Shan 	pmdp_huge_get_and_clear(args->mm, vaddr, args->pmdp);
289c0fe07b0SGavin Shan 	pgtable_trans_huge_withdraw(args->mm, args->pmdp);
290a5c3b9ffSAnshuman Khandual }
291a5c3b9ffSAnshuman Khandual 
2928983d231SGavin Shan static void __init pmd_leaf_tests(struct pgtable_debug_args *args)
293a5c3b9ffSAnshuman Khandual {
29465ac1a60SAnshuman Khandual 	pmd_t pmd;
29565ac1a60SAnshuman Khandual 
29665ac1a60SAnshuman Khandual 	if (!has_transparent_hugepage())
29765ac1a60SAnshuman Khandual 		return;
298a5c3b9ffSAnshuman Khandual 
2996315df41SAnshuman Khandual 	pr_debug("Validating PMD leaf\n");
3008983d231SGavin Shan 	pmd = pfn_pmd(args->fixed_pmd_pfn, args->page_prot);
30165ac1a60SAnshuman Khandual 
302a5c3b9ffSAnshuman Khandual 	/*
303a5c3b9ffSAnshuman Khandual 	 * PMD based THP is a leaf entry.
304a5c3b9ffSAnshuman Khandual 	 */
305a5c3b9ffSAnshuman Khandual 	pmd = pmd_mkhuge(pmd);
306a5c3b9ffSAnshuman Khandual 	WARN_ON(!pmd_leaf(pmd));
307a5c3b9ffSAnshuman Khandual }
308a5c3b9ffSAnshuman Khandual 
3098983d231SGavin Shan static void __init pmd_savedwrite_tests(struct pgtable_debug_args *args)
310a5c3b9ffSAnshuman Khandual {
31165ac1a60SAnshuman Khandual 	pmd_t pmd;
312a5c3b9ffSAnshuman Khandual 
3134200605bSAneesh Kumar K.V 	if (!IS_ENABLED(CONFIG_NUMA_BALANCING))
3144200605bSAneesh Kumar K.V 		return;
3154200605bSAneesh Kumar K.V 
31665ac1a60SAnshuman Khandual 	if (!has_transparent_hugepage())
31765ac1a60SAnshuman Khandual 		return;
31865ac1a60SAnshuman Khandual 
3196315df41SAnshuman Khandual 	pr_debug("Validating PMD saved write\n");
3208983d231SGavin Shan 	pmd = pfn_pmd(args->fixed_pmd_pfn, args->page_prot_none);
321a5c3b9ffSAnshuman Khandual 	WARN_ON(!pmd_savedwrite(pmd_mk_savedwrite(pmd_clear_savedwrite(pmd))));
322a5c3b9ffSAnshuman Khandual 	WARN_ON(pmd_savedwrite(pmd_clear_savedwrite(pmd_mk_savedwrite(pmd))));
323a5c3b9ffSAnshuman Khandual }
324a5c3b9ffSAnshuman Khandual 
325399145f9SAnshuman Khandual #ifdef CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD
32636b77d1eSGavin Shan static void __init pud_basic_tests(struct pgtable_debug_args *args, int idx)
327399145f9SAnshuman Khandual {
32831d17076SAnshuman Khandual 	pgprot_t prot = vm_get_page_prot(idx);
3292e326c07SAnshuman Khandual 	unsigned long val = idx, *ptr = &val;
33065ac1a60SAnshuman Khandual 	pud_t pud;
331399145f9SAnshuman Khandual 
332787d563bSAneesh Kumar K.V 	if (!has_transparent_hugepage())
333787d563bSAneesh Kumar K.V 		return;
334787d563bSAneesh Kumar K.V 
3352e326c07SAnshuman Khandual 	pr_debug("Validating PUD basic (%pGv)\n", ptr);
33636b77d1eSGavin Shan 	pud = pfn_pud(args->fixed_pud_pfn, prot);
337bb5c47ceSAnshuman Khandual 
338bb5c47ceSAnshuman Khandual 	/*
339bb5c47ceSAnshuman Khandual 	 * This test needs to be executed after the given page table entry
34031d17076SAnshuman Khandual 	 * is created with pfn_pud() to make sure that vm_get_page_prot(idx)
341bb5c47ceSAnshuman Khandual 	 * does not have the dirty bit enabled from the beginning. This is
342bb5c47ceSAnshuman Khandual 	 * important for platforms like arm64 where (!PTE_RDONLY) indicate
343bb5c47ceSAnshuman Khandual 	 * dirty bit being set.
344bb5c47ceSAnshuman Khandual 	 */
345bb5c47ceSAnshuman Khandual 	WARN_ON(pud_dirty(pud_wrprotect(pud)));
346bb5c47ceSAnshuman Khandual 
347399145f9SAnshuman Khandual 	WARN_ON(!pud_same(pud, pud));
348399145f9SAnshuman Khandual 	WARN_ON(!pud_young(pud_mkyoung(pud_mkold(pud))));
349bb5c47ceSAnshuman Khandual 	WARN_ON(!pud_dirty(pud_mkdirty(pud_mkclean(pud))));
350bb5c47ceSAnshuman Khandual 	WARN_ON(pud_dirty(pud_mkclean(pud_mkdirty(pud))));
351399145f9SAnshuman Khandual 	WARN_ON(!pud_write(pud_mkwrite(pud_wrprotect(pud))));
352399145f9SAnshuman Khandual 	WARN_ON(pud_write(pud_wrprotect(pud_mkwrite(pud))));
353399145f9SAnshuman Khandual 	WARN_ON(pud_young(pud_mkold(pud_mkyoung(pud))));
354bb5c47ceSAnshuman Khandual 	WARN_ON(pud_dirty(pud_wrprotect(pud_mkclean(pud))));
355bb5c47ceSAnshuman Khandual 	WARN_ON(!pud_dirty(pud_wrprotect(pud_mkdirty(pud))));
356399145f9SAnshuman Khandual 
35736b77d1eSGavin Shan 	if (mm_pmd_folded(args->mm))
358399145f9SAnshuman Khandual 		return;
359399145f9SAnshuman Khandual 
360399145f9SAnshuman Khandual 	/*
361399145f9SAnshuman Khandual 	 * A huge page does not point to next level page table
362399145f9SAnshuman Khandual 	 * entry. Hence this must qualify as pud_bad().
363399145f9SAnshuman Khandual 	 */
364399145f9SAnshuman Khandual 	WARN_ON(!pud_bad(pud_mkhuge(pud)));
365399145f9SAnshuman Khandual }
366a5c3b9ffSAnshuman Khandual 
3674cbde03bSGavin Shan static void __init pud_advanced_tests(struct pgtable_debug_args *args)
368a5c3b9ffSAnshuman Khandual {
3698c5b3a8aSGavin Shan 	struct page *page;
3704cbde03bSGavin Shan 	unsigned long vaddr = args->vaddr;
37165ac1a60SAnshuman Khandual 	pud_t pud;
372a5c3b9ffSAnshuman Khandual 
373a5c3b9ffSAnshuman Khandual 	if (!has_transparent_hugepage())
374a5c3b9ffSAnshuman Khandual 		return;
375a5c3b9ffSAnshuman Khandual 
3768c5b3a8aSGavin Shan 	page = (args->pud_pfn != ULONG_MAX) ? pfn_to_page(args->pud_pfn) : NULL;
3778c5b3a8aSGavin Shan 	if (!page)
3784cbde03bSGavin Shan 		return;
3794cbde03bSGavin Shan 
3808c5b3a8aSGavin Shan 	/*
3818c5b3a8aSGavin Shan 	 * flush_dcache_page() is called after set_pud_at() to clear
3828c5b3a8aSGavin Shan 	 * PG_arch_1 for the page on ARM64. The page flag isn't cleared
3838c5b3a8aSGavin Shan 	 * when it's released and page allocation check will fail when
3848c5b3a8aSGavin Shan 	 * the page is allocated again. For architectures other than ARM64,
3858c5b3a8aSGavin Shan 	 * the unexpected overhead of cache flushing is acceptable.
3868c5b3a8aSGavin Shan 	 */
3876315df41SAnshuman Khandual 	pr_debug("Validating PUD advanced\n");
388a5c3b9ffSAnshuman Khandual 	/* Align the address wrt HPAGE_PUD_SIZE */
38904f7ce3fSGerald Schaefer 	vaddr &= HPAGE_PUD_MASK;
390a5c3b9ffSAnshuman Khandual 
3914cbde03bSGavin Shan 	pud = pfn_pud(args->pud_pfn, args->page_prot);
3924cbde03bSGavin Shan 	set_pud_at(args->mm, vaddr, args->pudp, pud);
3938c5b3a8aSGavin Shan 	flush_dcache_page(page);
3944cbde03bSGavin Shan 	pudp_set_wrprotect(args->mm, vaddr, args->pudp);
3954cbde03bSGavin Shan 	pud = READ_ONCE(*args->pudp);
396a5c3b9ffSAnshuman Khandual 	WARN_ON(pud_write(pud));
397a5c3b9ffSAnshuman Khandual 
398a5c3b9ffSAnshuman Khandual #ifndef __PAGETABLE_PMD_FOLDED
3994cbde03bSGavin Shan 	pudp_huge_get_and_clear(args->mm, vaddr, args->pudp);
4004cbde03bSGavin Shan 	pud = READ_ONCE(*args->pudp);
401a5c3b9ffSAnshuman Khandual 	WARN_ON(!pud_none(pud));
402a5c3b9ffSAnshuman Khandual #endif /* __PAGETABLE_PMD_FOLDED */
4034cbde03bSGavin Shan 	pud = pfn_pud(args->pud_pfn, args->page_prot);
404a5c3b9ffSAnshuman Khandual 	pud = pud_wrprotect(pud);
405a5c3b9ffSAnshuman Khandual 	pud = pud_mkclean(pud);
4064cbde03bSGavin Shan 	set_pud_at(args->mm, vaddr, args->pudp, pud);
4078c5b3a8aSGavin Shan 	flush_dcache_page(page);
408a5c3b9ffSAnshuman Khandual 	pud = pud_mkwrite(pud);
409a5c3b9ffSAnshuman Khandual 	pud = pud_mkdirty(pud);
4104cbde03bSGavin Shan 	pudp_set_access_flags(args->vma, vaddr, args->pudp, pud, 1);
4114cbde03bSGavin Shan 	pud = READ_ONCE(*args->pudp);
412a5c3b9ffSAnshuman Khandual 	WARN_ON(!(pud_write(pud) && pud_dirty(pud)));
413a5c3b9ffSAnshuman Khandual 
414c3824e18SAneesh Kumar K.V #ifndef __PAGETABLE_PMD_FOLDED
4154cbde03bSGavin Shan 	pudp_huge_get_and_clear_full(args->mm, vaddr, args->pudp, 1);
4164cbde03bSGavin Shan 	pud = READ_ONCE(*args->pudp);
417c3824e18SAneesh Kumar K.V 	WARN_ON(!pud_none(pud));
418c3824e18SAneesh Kumar K.V #endif /* __PAGETABLE_PMD_FOLDED */
419c3824e18SAneesh Kumar K.V 
4204cbde03bSGavin Shan 	pud = pfn_pud(args->pud_pfn, args->page_prot);
421a5c3b9ffSAnshuman Khandual 	pud = pud_mkyoung(pud);
4224cbde03bSGavin Shan 	set_pud_at(args->mm, vaddr, args->pudp, pud);
4238c5b3a8aSGavin Shan 	flush_dcache_page(page);
4244cbde03bSGavin Shan 	pudp_test_and_clear_young(args->vma, vaddr, args->pudp);
4254cbde03bSGavin Shan 	pud = READ_ONCE(*args->pudp);
426a5c3b9ffSAnshuman Khandual 	WARN_ON(pud_young(pud));
42713af0506SAneesh Kumar K.V 
4284cbde03bSGavin Shan 	pudp_huge_get_and_clear(args->mm, vaddr, args->pudp);
429a5c3b9ffSAnshuman Khandual }
430a5c3b9ffSAnshuman Khandual 
4318983d231SGavin Shan static void __init pud_leaf_tests(struct pgtable_debug_args *args)
432a5c3b9ffSAnshuman Khandual {
43365ac1a60SAnshuman Khandual 	pud_t pud;
43465ac1a60SAnshuman Khandual 
43565ac1a60SAnshuman Khandual 	if (!has_transparent_hugepage())
43665ac1a60SAnshuman Khandual 		return;
437a5c3b9ffSAnshuman Khandual 
4386315df41SAnshuman Khandual 	pr_debug("Validating PUD leaf\n");
4398983d231SGavin Shan 	pud = pfn_pud(args->fixed_pud_pfn, args->page_prot);
440a5c3b9ffSAnshuman Khandual 	/*
441a5c3b9ffSAnshuman Khandual 	 * PUD based THP is a leaf entry.
442a5c3b9ffSAnshuman Khandual 	 */
443a5c3b9ffSAnshuman Khandual 	pud = pud_mkhuge(pud);
444a5c3b9ffSAnshuman Khandual 	WARN_ON(!pud_leaf(pud));
445a5c3b9ffSAnshuman Khandual }
4465fe77be6SShixin Liu #else  /* !CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD */
44736b77d1eSGavin Shan static void __init pud_basic_tests(struct pgtable_debug_args *args, int idx) { }
4484cbde03bSGavin Shan static void __init pud_advanced_tests(struct pgtable_debug_args *args) { }
4498983d231SGavin Shan static void __init pud_leaf_tests(struct pgtable_debug_args *args) { }
4505fe77be6SShixin Liu #endif /* CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD */
4515fe77be6SShixin Liu #else  /* !CONFIG_TRANSPARENT_HUGEPAGE */
45236b77d1eSGavin Shan static void __init pmd_basic_tests(struct pgtable_debug_args *args, int idx) { }
45336b77d1eSGavin Shan static void __init pud_basic_tests(struct pgtable_debug_args *args, int idx) { }
454c0fe07b0SGavin Shan static void __init pmd_advanced_tests(struct pgtable_debug_args *args) { }
4554cbde03bSGavin Shan static void __init pud_advanced_tests(struct pgtable_debug_args *args) { }
4568983d231SGavin Shan static void __init pmd_leaf_tests(struct pgtable_debug_args *args) { }
4578983d231SGavin Shan static void __init pud_leaf_tests(struct pgtable_debug_args *args) { }
4588983d231SGavin Shan static void __init pmd_savedwrite_tests(struct pgtable_debug_args *args) { }
4595fe77be6SShixin Liu #endif /* CONFIG_TRANSPARENT_HUGEPAGE */
460a5c3b9ffSAnshuman Khandual 
46185a14463SAneesh Kumar K.V #ifdef CONFIG_HAVE_ARCH_HUGE_VMAP
462c0fe07b0SGavin Shan static void __init pmd_huge_tests(struct pgtable_debug_args *args)
4635fe77be6SShixin Liu {
4645fe77be6SShixin Liu 	pmd_t pmd;
4655fe77be6SShixin Liu 
466c0fe07b0SGavin Shan 	if (!arch_vmap_pmd_supported(args->page_prot))
4675fe77be6SShixin Liu 		return;
4685fe77be6SShixin Liu 
4695fe77be6SShixin Liu 	pr_debug("Validating PMD huge\n");
4705fe77be6SShixin Liu 	/*
4715fe77be6SShixin Liu 	 * X86 defined pmd_set_huge() verifies that the given
4725fe77be6SShixin Liu 	 * PMD is not a populated non-leaf entry.
4735fe77be6SShixin Liu 	 */
474c0fe07b0SGavin Shan 	WRITE_ONCE(*args->pmdp, __pmd(0));
475c0fe07b0SGavin Shan 	WARN_ON(!pmd_set_huge(args->pmdp, __pfn_to_phys(args->fixed_pmd_pfn), args->page_prot));
476c0fe07b0SGavin Shan 	WARN_ON(!pmd_clear_huge(args->pmdp));
477c0fe07b0SGavin Shan 	pmd = READ_ONCE(*args->pmdp);
4785fe77be6SShixin Liu 	WARN_ON(!pmd_none(pmd));
4795fe77be6SShixin Liu }
4805fe77be6SShixin Liu 
4814cbde03bSGavin Shan static void __init pud_huge_tests(struct pgtable_debug_args *args)
482a5c3b9ffSAnshuman Khandual {
483a5c3b9ffSAnshuman Khandual 	pud_t pud;
484a5c3b9ffSAnshuman Khandual 
4854cbde03bSGavin Shan 	if (!arch_vmap_pud_supported(args->page_prot))
486a5c3b9ffSAnshuman Khandual 		return;
4876315df41SAnshuman Khandual 
4886315df41SAnshuman Khandual 	pr_debug("Validating PUD huge\n");
489a5c3b9ffSAnshuman Khandual 	/*
490a5c3b9ffSAnshuman Khandual 	 * X86 defined pud_set_huge() verifies that the given
491a5c3b9ffSAnshuman Khandual 	 * PUD is not a populated non-leaf entry.
492a5c3b9ffSAnshuman Khandual 	 */
4934cbde03bSGavin Shan 	WRITE_ONCE(*args->pudp, __pud(0));
4944cbde03bSGavin Shan 	WARN_ON(!pud_set_huge(args->pudp, __pfn_to_phys(args->fixed_pud_pfn), args->page_prot));
4954cbde03bSGavin Shan 	WARN_ON(!pud_clear_huge(args->pudp));
4964cbde03bSGavin Shan 	pud = READ_ONCE(*args->pudp);
497a5c3b9ffSAnshuman Khandual 	WARN_ON(!pud_none(pud));
498a5c3b9ffSAnshuman Khandual }
49985a14463SAneesh Kumar K.V #else /* !CONFIG_HAVE_ARCH_HUGE_VMAP */
500c0fe07b0SGavin Shan static void __init pmd_huge_tests(struct pgtable_debug_args *args) { }
5014cbde03bSGavin Shan static void __init pud_huge_tests(struct pgtable_debug_args *args) { }
5025fe77be6SShixin Liu #endif /* CONFIG_HAVE_ARCH_HUGE_VMAP */
503399145f9SAnshuman Khandual 
50436b77d1eSGavin Shan static void __init p4d_basic_tests(struct pgtable_debug_args *args)
505399145f9SAnshuman Khandual {
506399145f9SAnshuman Khandual 	p4d_t p4d;
507399145f9SAnshuman Khandual 
5086315df41SAnshuman Khandual 	pr_debug("Validating P4D basic\n");
509399145f9SAnshuman Khandual 	memset(&p4d, RANDOM_NZVALUE, sizeof(p4d_t));
510399145f9SAnshuman Khandual 	WARN_ON(!p4d_same(p4d, p4d));
511399145f9SAnshuman Khandual }
512399145f9SAnshuman Khandual 
51336b77d1eSGavin Shan static void __init pgd_basic_tests(struct pgtable_debug_args *args)
514399145f9SAnshuman Khandual {
515399145f9SAnshuman Khandual 	pgd_t pgd;
516399145f9SAnshuman Khandual 
5176315df41SAnshuman Khandual 	pr_debug("Validating PGD basic\n");
518399145f9SAnshuman Khandual 	memset(&pgd, RANDOM_NZVALUE, sizeof(pgd_t));
519399145f9SAnshuman Khandual 	WARN_ON(!pgd_same(pgd, pgd));
520399145f9SAnshuman Khandual }
521399145f9SAnshuman Khandual 
522399145f9SAnshuman Khandual #ifndef __PAGETABLE_PUD_FOLDED
5234cbde03bSGavin Shan static void __init pud_clear_tests(struct pgtable_debug_args *args)
524399145f9SAnshuman Khandual {
5254cbde03bSGavin Shan 	pud_t pud = READ_ONCE(*args->pudp);
526399145f9SAnshuman Khandual 
5274cbde03bSGavin Shan 	if (mm_pmd_folded(args->mm))
528399145f9SAnshuman Khandual 		return;
529399145f9SAnshuman Khandual 
5306315df41SAnshuman Khandual 	pr_debug("Validating PUD clear\n");
531399145f9SAnshuman Khandual 	pud = __pud(pud_val(pud) | RANDOM_ORVALUE);
5324cbde03bSGavin Shan 	WRITE_ONCE(*args->pudp, pud);
5334cbde03bSGavin Shan 	pud_clear(args->pudp);
5344cbde03bSGavin Shan 	pud = READ_ONCE(*args->pudp);
535399145f9SAnshuman Khandual 	WARN_ON(!pud_none(pud));
536399145f9SAnshuman Khandual }
537399145f9SAnshuman Khandual 
5384cbde03bSGavin Shan static void __init pud_populate_tests(struct pgtable_debug_args *args)
539399145f9SAnshuman Khandual {
540399145f9SAnshuman Khandual 	pud_t pud;
541399145f9SAnshuman Khandual 
5424cbde03bSGavin Shan 	if (mm_pmd_folded(args->mm))
543399145f9SAnshuman Khandual 		return;
5446315df41SAnshuman Khandual 
5456315df41SAnshuman Khandual 	pr_debug("Validating PUD populate\n");
546399145f9SAnshuman Khandual 	/*
547399145f9SAnshuman Khandual 	 * This entry points to next level page table page.
548399145f9SAnshuman Khandual 	 * Hence this must not qualify as pud_bad().
549399145f9SAnshuman Khandual 	 */
5504cbde03bSGavin Shan 	pud_populate(args->mm, args->pudp, args->start_pmdp);
5514cbde03bSGavin Shan 	pud = READ_ONCE(*args->pudp);
552399145f9SAnshuman Khandual 	WARN_ON(pud_bad(pud));
553399145f9SAnshuman Khandual }
554399145f9SAnshuman Khandual #else  /* !__PAGETABLE_PUD_FOLDED */
5554cbde03bSGavin Shan static void __init pud_clear_tests(struct pgtable_debug_args *args) { }
5564cbde03bSGavin Shan static void __init pud_populate_tests(struct pgtable_debug_args *args) { }
557399145f9SAnshuman Khandual #endif /* PAGETABLE_PUD_FOLDED */
558399145f9SAnshuman Khandual 
559399145f9SAnshuman Khandual #ifndef __PAGETABLE_P4D_FOLDED
5602f87f8c3SGavin Shan static void __init p4d_clear_tests(struct pgtable_debug_args *args)
561399145f9SAnshuman Khandual {
5622f87f8c3SGavin Shan 	p4d_t p4d = READ_ONCE(*args->p4dp);
563399145f9SAnshuman Khandual 
5642f87f8c3SGavin Shan 	if (mm_pud_folded(args->mm))
565399145f9SAnshuman Khandual 		return;
566399145f9SAnshuman Khandual 
5676315df41SAnshuman Khandual 	pr_debug("Validating P4D clear\n");
568399145f9SAnshuman Khandual 	p4d = __p4d(p4d_val(p4d) | RANDOM_ORVALUE);
5692f87f8c3SGavin Shan 	WRITE_ONCE(*args->p4dp, p4d);
5702f87f8c3SGavin Shan 	p4d_clear(args->p4dp);
5712f87f8c3SGavin Shan 	p4d = READ_ONCE(*args->p4dp);
572399145f9SAnshuman Khandual 	WARN_ON(!p4d_none(p4d));
573399145f9SAnshuman Khandual }
574399145f9SAnshuman Khandual 
5752f87f8c3SGavin Shan static void __init p4d_populate_tests(struct pgtable_debug_args *args)
576399145f9SAnshuman Khandual {
577399145f9SAnshuman Khandual 	p4d_t p4d;
578399145f9SAnshuman Khandual 
5792f87f8c3SGavin Shan 	if (mm_pud_folded(args->mm))
580399145f9SAnshuman Khandual 		return;
581399145f9SAnshuman Khandual 
5826315df41SAnshuman Khandual 	pr_debug("Validating P4D populate\n");
583399145f9SAnshuman Khandual 	/*
584399145f9SAnshuman Khandual 	 * This entry points to next level page table page.
585399145f9SAnshuman Khandual 	 * Hence this must not qualify as p4d_bad().
586399145f9SAnshuman Khandual 	 */
5872f87f8c3SGavin Shan 	pud_clear(args->pudp);
5882f87f8c3SGavin Shan 	p4d_clear(args->p4dp);
5892f87f8c3SGavin Shan 	p4d_populate(args->mm, args->p4dp, args->start_pudp);
5902f87f8c3SGavin Shan 	p4d = READ_ONCE(*args->p4dp);
591399145f9SAnshuman Khandual 	WARN_ON(p4d_bad(p4d));
592399145f9SAnshuman Khandual }
593399145f9SAnshuman Khandual 
5942f87f8c3SGavin Shan static void __init pgd_clear_tests(struct pgtable_debug_args *args)
595399145f9SAnshuman Khandual {
5962f87f8c3SGavin Shan 	pgd_t pgd = READ_ONCE(*(args->pgdp));
597399145f9SAnshuman Khandual 
5982f87f8c3SGavin Shan 	if (mm_p4d_folded(args->mm))
599399145f9SAnshuman Khandual 		return;
600399145f9SAnshuman Khandual 
6016315df41SAnshuman Khandual 	pr_debug("Validating PGD clear\n");
602399145f9SAnshuman Khandual 	pgd = __pgd(pgd_val(pgd) | RANDOM_ORVALUE);
6032f87f8c3SGavin Shan 	WRITE_ONCE(*args->pgdp, pgd);
6042f87f8c3SGavin Shan 	pgd_clear(args->pgdp);
6052f87f8c3SGavin Shan 	pgd = READ_ONCE(*args->pgdp);
606399145f9SAnshuman Khandual 	WARN_ON(!pgd_none(pgd));
607399145f9SAnshuman Khandual }
608399145f9SAnshuman Khandual 
6092f87f8c3SGavin Shan static void __init pgd_populate_tests(struct pgtable_debug_args *args)
610399145f9SAnshuman Khandual {
611399145f9SAnshuman Khandual 	pgd_t pgd;
612399145f9SAnshuman Khandual 
6132f87f8c3SGavin Shan 	if (mm_p4d_folded(args->mm))
614399145f9SAnshuman Khandual 		return;
615399145f9SAnshuman Khandual 
6166315df41SAnshuman Khandual 	pr_debug("Validating PGD populate\n");
617399145f9SAnshuman Khandual 	/*
618399145f9SAnshuman Khandual 	 * This entry points to next level page table page.
619399145f9SAnshuman Khandual 	 * Hence this must not qualify as pgd_bad().
620399145f9SAnshuman Khandual 	 */
6212f87f8c3SGavin Shan 	p4d_clear(args->p4dp);
6222f87f8c3SGavin Shan 	pgd_clear(args->pgdp);
6232f87f8c3SGavin Shan 	pgd_populate(args->mm, args->pgdp, args->start_p4dp);
6242f87f8c3SGavin Shan 	pgd = READ_ONCE(*args->pgdp);
625399145f9SAnshuman Khandual 	WARN_ON(pgd_bad(pgd));
626399145f9SAnshuman Khandual }
627399145f9SAnshuman Khandual #else  /* !__PAGETABLE_P4D_FOLDED */
6282f87f8c3SGavin Shan static void __init p4d_clear_tests(struct pgtable_debug_args *args) { }
6292f87f8c3SGavin Shan static void __init pgd_clear_tests(struct pgtable_debug_args *args) { }
6302f87f8c3SGavin Shan static void __init p4d_populate_tests(struct pgtable_debug_args *args) { }
6312f87f8c3SGavin Shan static void __init pgd_populate_tests(struct pgtable_debug_args *args) { }
632399145f9SAnshuman Khandual #endif /* PAGETABLE_P4D_FOLDED */
633399145f9SAnshuman Khandual 
63444966c44SGavin Shan static void __init pte_clear_tests(struct pgtable_debug_args *args)
635399145f9SAnshuman Khandual {
6368c5b3a8aSGavin Shan 	struct page *page;
63744966c44SGavin Shan 	pte_t pte = pfn_pte(args->pte_pfn, args->page_prot);
63844966c44SGavin Shan 
6398c5b3a8aSGavin Shan 	page = (args->pte_pfn != ULONG_MAX) ? pfn_to_page(args->pte_pfn) : NULL;
6408c5b3a8aSGavin Shan 	if (!page)
64144966c44SGavin Shan 		return;
642399145f9SAnshuman Khandual 
6438c5b3a8aSGavin Shan 	/*
6448c5b3a8aSGavin Shan 	 * flush_dcache_page() is called after set_pte_at() to clear
6458c5b3a8aSGavin Shan 	 * PG_arch_1 for the page on ARM64. The page flag isn't cleared
6468c5b3a8aSGavin Shan 	 * when it's released and page allocation check will fail when
6478c5b3a8aSGavin Shan 	 * the page is allocated again. For architectures other than ARM64,
6488c5b3a8aSGavin Shan 	 * the unexpected overhead of cache flushing is acceptable.
6498c5b3a8aSGavin Shan 	 */
6506315df41SAnshuman Khandual 	pr_debug("Validating PTE clear\n");
651401035d5SAneesh Kumar K.V #ifndef CONFIG_RISCV
652399145f9SAnshuman Khandual 	pte = __pte(pte_val(pte) | RANDOM_ORVALUE);
653401035d5SAneesh Kumar K.V #endif
65444966c44SGavin Shan 	set_pte_at(args->mm, args->vaddr, args->ptep, pte);
6558c5b3a8aSGavin Shan 	flush_dcache_page(page);
656399145f9SAnshuman Khandual 	barrier();
65708d5b29eSPasha Tatashin 	ptep_clear(args->mm, args->vaddr, args->ptep);
65844966c44SGavin Shan 	pte = ptep_get(args->ptep);
659399145f9SAnshuman Khandual 	WARN_ON(!pte_none(pte));
660399145f9SAnshuman Khandual }
661399145f9SAnshuman Khandual 
662c0fe07b0SGavin Shan static void __init pmd_clear_tests(struct pgtable_debug_args *args)
663399145f9SAnshuman Khandual {
664c0fe07b0SGavin Shan 	pmd_t pmd = READ_ONCE(*args->pmdp);
665399145f9SAnshuman Khandual 
6666315df41SAnshuman Khandual 	pr_debug("Validating PMD clear\n");
667399145f9SAnshuman Khandual 	pmd = __pmd(pmd_val(pmd) | RANDOM_ORVALUE);
668c0fe07b0SGavin Shan 	WRITE_ONCE(*args->pmdp, pmd);
669c0fe07b0SGavin Shan 	pmd_clear(args->pmdp);
670c0fe07b0SGavin Shan 	pmd = READ_ONCE(*args->pmdp);
671399145f9SAnshuman Khandual 	WARN_ON(!pmd_none(pmd));
672399145f9SAnshuman Khandual }
673399145f9SAnshuman Khandual 
674c0fe07b0SGavin Shan static void __init pmd_populate_tests(struct pgtable_debug_args *args)
675399145f9SAnshuman Khandual {
676399145f9SAnshuman Khandual 	pmd_t pmd;
677399145f9SAnshuman Khandual 
6786315df41SAnshuman Khandual 	pr_debug("Validating PMD populate\n");
679399145f9SAnshuman Khandual 	/*
680399145f9SAnshuman Khandual 	 * This entry points to next level page table page.
681399145f9SAnshuman Khandual 	 * Hence this must not qualify as pmd_bad().
682399145f9SAnshuman Khandual 	 */
683c0fe07b0SGavin Shan 	pmd_populate(args->mm, args->pmdp, args->start_ptep);
684c0fe07b0SGavin Shan 	pmd = READ_ONCE(*args->pmdp);
685399145f9SAnshuman Khandual 	WARN_ON(pmd_bad(pmd));
686399145f9SAnshuman Khandual }
687399145f9SAnshuman Khandual 
6888cb183f2SGavin Shan static void __init pte_special_tests(struct pgtable_debug_args *args)
68905289402SAnshuman Khandual {
6908cb183f2SGavin Shan 	pte_t pte = pfn_pte(args->fixed_pte_pfn, args->page_prot);
69105289402SAnshuman Khandual 
69205289402SAnshuman Khandual 	if (!IS_ENABLED(CONFIG_ARCH_HAS_PTE_SPECIAL))
69305289402SAnshuman Khandual 		return;
69405289402SAnshuman Khandual 
6956315df41SAnshuman Khandual 	pr_debug("Validating PTE special\n");
69605289402SAnshuman Khandual 	WARN_ON(!pte_special(pte_mkspecial(pte)));
69705289402SAnshuman Khandual }
69805289402SAnshuman Khandual 
6998cb183f2SGavin Shan static void __init pte_protnone_tests(struct pgtable_debug_args *args)
70005289402SAnshuman Khandual {
7018cb183f2SGavin Shan 	pte_t pte = pfn_pte(args->fixed_pte_pfn, args->page_prot_none);
70205289402SAnshuman Khandual 
70305289402SAnshuman Khandual 	if (!IS_ENABLED(CONFIG_NUMA_BALANCING))
70405289402SAnshuman Khandual 		return;
70505289402SAnshuman Khandual 
7066315df41SAnshuman Khandual 	pr_debug("Validating PTE protnone\n");
70705289402SAnshuman Khandual 	WARN_ON(!pte_protnone(pte));
70805289402SAnshuman Khandual 	WARN_ON(!pte_present(pte));
70905289402SAnshuman Khandual }
71005289402SAnshuman Khandual 
71105289402SAnshuman Khandual #ifdef CONFIG_TRANSPARENT_HUGEPAGE
7128cb183f2SGavin Shan static void __init pmd_protnone_tests(struct pgtable_debug_args *args)
71305289402SAnshuman Khandual {
71465ac1a60SAnshuman Khandual 	pmd_t pmd;
71505289402SAnshuman Khandual 
71605289402SAnshuman Khandual 	if (!IS_ENABLED(CONFIG_NUMA_BALANCING))
71705289402SAnshuman Khandual 		return;
71805289402SAnshuman Khandual 
71965ac1a60SAnshuman Khandual 	if (!has_transparent_hugepage())
72065ac1a60SAnshuman Khandual 		return;
72165ac1a60SAnshuman Khandual 
7226315df41SAnshuman Khandual 	pr_debug("Validating PMD protnone\n");
7238cb183f2SGavin Shan 	pmd = pmd_mkhuge(pfn_pmd(args->fixed_pmd_pfn, args->page_prot_none));
72405289402SAnshuman Khandual 	WARN_ON(!pmd_protnone(pmd));
72505289402SAnshuman Khandual 	WARN_ON(!pmd_present(pmd));
72605289402SAnshuman Khandual }
72705289402SAnshuman Khandual #else  /* !CONFIG_TRANSPARENT_HUGEPAGE */
7288cb183f2SGavin Shan static void __init pmd_protnone_tests(struct pgtable_debug_args *args) { }
72905289402SAnshuman Khandual #endif /* CONFIG_TRANSPARENT_HUGEPAGE */
73005289402SAnshuman Khandual 
73105289402SAnshuman Khandual #ifdef CONFIG_ARCH_HAS_PTE_DEVMAP
7328cb183f2SGavin Shan static void __init pte_devmap_tests(struct pgtable_debug_args *args)
73305289402SAnshuman Khandual {
7348cb183f2SGavin Shan 	pte_t pte = pfn_pte(args->fixed_pte_pfn, args->page_prot);
73505289402SAnshuman Khandual 
7366315df41SAnshuman Khandual 	pr_debug("Validating PTE devmap\n");
73705289402SAnshuman Khandual 	WARN_ON(!pte_devmap(pte_mkdevmap(pte)));
73805289402SAnshuman Khandual }
73905289402SAnshuman Khandual 
74005289402SAnshuman Khandual #ifdef CONFIG_TRANSPARENT_HUGEPAGE
7418cb183f2SGavin Shan static void __init pmd_devmap_tests(struct pgtable_debug_args *args)
74205289402SAnshuman Khandual {
74365ac1a60SAnshuman Khandual 	pmd_t pmd;
74465ac1a60SAnshuman Khandual 
74565ac1a60SAnshuman Khandual 	if (!has_transparent_hugepage())
74665ac1a60SAnshuman Khandual 		return;
74705289402SAnshuman Khandual 
7486315df41SAnshuman Khandual 	pr_debug("Validating PMD devmap\n");
7498cb183f2SGavin Shan 	pmd = pfn_pmd(args->fixed_pmd_pfn, args->page_prot);
75005289402SAnshuman Khandual 	WARN_ON(!pmd_devmap(pmd_mkdevmap(pmd)));
75105289402SAnshuman Khandual }
75205289402SAnshuman Khandual 
75305289402SAnshuman Khandual #ifdef CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD
7548cb183f2SGavin Shan static void __init pud_devmap_tests(struct pgtable_debug_args *args)
75505289402SAnshuman Khandual {
75665ac1a60SAnshuman Khandual 	pud_t pud;
75765ac1a60SAnshuman Khandual 
75865ac1a60SAnshuman Khandual 	if (!has_transparent_hugepage())
75965ac1a60SAnshuman Khandual 		return;
76005289402SAnshuman Khandual 
7616315df41SAnshuman Khandual 	pr_debug("Validating PUD devmap\n");
7628cb183f2SGavin Shan 	pud = pfn_pud(args->fixed_pud_pfn, args->page_prot);
76305289402SAnshuman Khandual 	WARN_ON(!pud_devmap(pud_mkdevmap(pud)));
76405289402SAnshuman Khandual }
76505289402SAnshuman Khandual #else  /* !CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD */
7668cb183f2SGavin Shan static void __init pud_devmap_tests(struct pgtable_debug_args *args) { }
76705289402SAnshuman Khandual #endif /* CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD */
76805289402SAnshuman Khandual #else  /* CONFIG_TRANSPARENT_HUGEPAGE */
7698cb183f2SGavin Shan static void __init pmd_devmap_tests(struct pgtable_debug_args *args) { }
7708cb183f2SGavin Shan static void __init pud_devmap_tests(struct pgtable_debug_args *args) { }
77105289402SAnshuman Khandual #endif /* CONFIG_TRANSPARENT_HUGEPAGE */
77205289402SAnshuman Khandual #else
7738cb183f2SGavin Shan static void __init pte_devmap_tests(struct pgtable_debug_args *args) { }
7748cb183f2SGavin Shan static void __init pmd_devmap_tests(struct pgtable_debug_args *args) { }
7758cb183f2SGavin Shan static void __init pud_devmap_tests(struct pgtable_debug_args *args) { }
77605289402SAnshuman Khandual #endif /* CONFIG_ARCH_HAS_PTE_DEVMAP */
77705289402SAnshuman Khandual 
7785f447e80SGavin Shan static void __init pte_soft_dirty_tests(struct pgtable_debug_args *args)
77905289402SAnshuman Khandual {
7805f447e80SGavin Shan 	pte_t pte = pfn_pte(args->fixed_pte_pfn, args->page_prot);
78105289402SAnshuman Khandual 
78205289402SAnshuman Khandual 	if (!IS_ENABLED(CONFIG_MEM_SOFT_DIRTY))
78305289402SAnshuman Khandual 		return;
78405289402SAnshuman Khandual 
7856315df41SAnshuman Khandual 	pr_debug("Validating PTE soft dirty\n");
78605289402SAnshuman Khandual 	WARN_ON(!pte_soft_dirty(pte_mksoft_dirty(pte)));
78705289402SAnshuman Khandual 	WARN_ON(pte_soft_dirty(pte_clear_soft_dirty(pte)));
78805289402SAnshuman Khandual }
78905289402SAnshuman Khandual 
7905f447e80SGavin Shan static void __init pte_swap_soft_dirty_tests(struct pgtable_debug_args *args)
79105289402SAnshuman Khandual {
7925f447e80SGavin Shan 	pte_t pte = pfn_pte(args->fixed_pte_pfn, args->page_prot);
79305289402SAnshuman Khandual 
79405289402SAnshuman Khandual 	if (!IS_ENABLED(CONFIG_MEM_SOFT_DIRTY))
79505289402SAnshuman Khandual 		return;
79605289402SAnshuman Khandual 
7976315df41SAnshuman Khandual 	pr_debug("Validating PTE swap soft dirty\n");
79805289402SAnshuman Khandual 	WARN_ON(!pte_swp_soft_dirty(pte_swp_mksoft_dirty(pte)));
79905289402SAnshuman Khandual 	WARN_ON(pte_swp_soft_dirty(pte_swp_clear_soft_dirty(pte)));
80005289402SAnshuman Khandual }
80105289402SAnshuman Khandual 
80205289402SAnshuman Khandual #ifdef CONFIG_TRANSPARENT_HUGEPAGE
8035f447e80SGavin Shan static void __init pmd_soft_dirty_tests(struct pgtable_debug_args *args)
80405289402SAnshuman Khandual {
80565ac1a60SAnshuman Khandual 	pmd_t pmd;
80605289402SAnshuman Khandual 
80705289402SAnshuman Khandual 	if (!IS_ENABLED(CONFIG_MEM_SOFT_DIRTY))
80805289402SAnshuman Khandual 		return;
80905289402SAnshuman Khandual 
81065ac1a60SAnshuman Khandual 	if (!has_transparent_hugepage())
81165ac1a60SAnshuman Khandual 		return;
81265ac1a60SAnshuman Khandual 
8136315df41SAnshuman Khandual 	pr_debug("Validating PMD soft dirty\n");
8145f447e80SGavin Shan 	pmd = pfn_pmd(args->fixed_pmd_pfn, args->page_prot);
81505289402SAnshuman Khandual 	WARN_ON(!pmd_soft_dirty(pmd_mksoft_dirty(pmd)));
81605289402SAnshuman Khandual 	WARN_ON(pmd_soft_dirty(pmd_clear_soft_dirty(pmd)));
81705289402SAnshuman Khandual }
81805289402SAnshuman Khandual 
8195f447e80SGavin Shan static void __init pmd_swap_soft_dirty_tests(struct pgtable_debug_args *args)
82005289402SAnshuman Khandual {
82165ac1a60SAnshuman Khandual 	pmd_t pmd;
82205289402SAnshuman Khandual 
82305289402SAnshuman Khandual 	if (!IS_ENABLED(CONFIG_MEM_SOFT_DIRTY) ||
82405289402SAnshuman Khandual 		!IS_ENABLED(CONFIG_ARCH_ENABLE_THP_MIGRATION))
82505289402SAnshuman Khandual 		return;
82605289402SAnshuman Khandual 
82765ac1a60SAnshuman Khandual 	if (!has_transparent_hugepage())
82865ac1a60SAnshuman Khandual 		return;
82965ac1a60SAnshuman Khandual 
8306315df41SAnshuman Khandual 	pr_debug("Validating PMD swap soft dirty\n");
8315f447e80SGavin Shan 	pmd = pfn_pmd(args->fixed_pmd_pfn, args->page_prot);
83205289402SAnshuman Khandual 	WARN_ON(!pmd_swp_soft_dirty(pmd_swp_mksoft_dirty(pmd)));
83305289402SAnshuman Khandual 	WARN_ON(pmd_swp_soft_dirty(pmd_swp_clear_soft_dirty(pmd)));
83405289402SAnshuman Khandual }
835b593b90dSShixin Liu #else  /* !CONFIG_TRANSPARENT_HUGEPAGE */
8365f447e80SGavin Shan static void __init pmd_soft_dirty_tests(struct pgtable_debug_args *args) { }
8375f447e80SGavin Shan static void __init pmd_swap_soft_dirty_tests(struct pgtable_debug_args *args) { }
838b593b90dSShixin Liu #endif /* CONFIG_TRANSPARENT_HUGEPAGE */
83905289402SAnshuman Khandual 
840210d1e8aSDavid Hildenbrand static void __init pte_swap_exclusive_tests(struct pgtable_debug_args *args)
841210d1e8aSDavid Hildenbrand {
842210d1e8aSDavid Hildenbrand #ifdef __HAVE_ARCH_PTE_SWP_EXCLUSIVE
843210d1e8aSDavid Hildenbrand 	pte_t pte = pfn_pte(args->fixed_pte_pfn, args->page_prot);
844210d1e8aSDavid Hildenbrand 
845210d1e8aSDavid Hildenbrand 	pr_debug("Validating PTE swap exclusive\n");
846210d1e8aSDavid Hildenbrand 	pte = pte_swp_mkexclusive(pte);
847210d1e8aSDavid Hildenbrand 	WARN_ON(!pte_swp_exclusive(pte));
848210d1e8aSDavid Hildenbrand 	pte = pte_swp_clear_exclusive(pte);
849210d1e8aSDavid Hildenbrand 	WARN_ON(pte_swp_exclusive(pte));
850210d1e8aSDavid Hildenbrand #endif /* __HAVE_ARCH_PTE_SWP_EXCLUSIVE */
851210d1e8aSDavid Hildenbrand }
852210d1e8aSDavid Hildenbrand 
8535f447e80SGavin Shan static void __init pte_swap_tests(struct pgtable_debug_args *args)
85405289402SAnshuman Khandual {
85505289402SAnshuman Khandual 	swp_entry_t swp;
85605289402SAnshuman Khandual 	pte_t pte;
85705289402SAnshuman Khandual 
8586315df41SAnshuman Khandual 	pr_debug("Validating PTE swap\n");
8595f447e80SGavin Shan 	pte = pfn_pte(args->fixed_pte_pfn, args->page_prot);
86005289402SAnshuman Khandual 	swp = __pte_to_swp_entry(pte);
86105289402SAnshuman Khandual 	pte = __swp_entry_to_pte(swp);
8625f447e80SGavin Shan 	WARN_ON(args->fixed_pte_pfn != pte_pfn(pte));
86305289402SAnshuman Khandual }
86405289402SAnshuman Khandual 
86505289402SAnshuman Khandual #ifdef CONFIG_ARCH_ENABLE_THP_MIGRATION
8665f447e80SGavin Shan static void __init pmd_swap_tests(struct pgtable_debug_args *args)
86705289402SAnshuman Khandual {
86805289402SAnshuman Khandual 	swp_entry_t swp;
86905289402SAnshuman Khandual 	pmd_t pmd;
87005289402SAnshuman Khandual 
87165ac1a60SAnshuman Khandual 	if (!has_transparent_hugepage())
87265ac1a60SAnshuman Khandual 		return;
87365ac1a60SAnshuman Khandual 
8746315df41SAnshuman Khandual 	pr_debug("Validating PMD swap\n");
8755f447e80SGavin Shan 	pmd = pfn_pmd(args->fixed_pmd_pfn, args->page_prot);
87605289402SAnshuman Khandual 	swp = __pmd_to_swp_entry(pmd);
87705289402SAnshuman Khandual 	pmd = __swp_entry_to_pmd(swp);
8785f447e80SGavin Shan 	WARN_ON(args->fixed_pmd_pfn != pmd_pfn(pmd));
87905289402SAnshuman Khandual }
88005289402SAnshuman Khandual #else  /* !CONFIG_ARCH_ENABLE_THP_MIGRATION */
8815f447e80SGavin Shan static void __init pmd_swap_tests(struct pgtable_debug_args *args) { }
88205289402SAnshuman Khandual #endif /* CONFIG_ARCH_ENABLE_THP_MIGRATION */
88305289402SAnshuman Khandual 
8844878a888SGavin Shan static void __init swap_migration_tests(struct pgtable_debug_args *args)
88505289402SAnshuman Khandual {
88605289402SAnshuman Khandual 	struct page *page;
88705289402SAnshuman Khandual 	swp_entry_t swp;
88805289402SAnshuman Khandual 
88905289402SAnshuman Khandual 	if (!IS_ENABLED(CONFIG_MIGRATION))
89005289402SAnshuman Khandual 		return;
8916315df41SAnshuman Khandual 
89205289402SAnshuman Khandual 	/*
89305289402SAnshuman Khandual 	 * swap_migration_tests() requires a dedicated page as it needs to
89405289402SAnshuman Khandual 	 * be locked before creating a migration entry from it. Locking the
89505289402SAnshuman Khandual 	 * page that actually maps kernel text ('start_kernel') can be real
8964878a888SGavin Shan 	 * problematic. Lets use the allocated page explicitly for this
8974878a888SGavin Shan 	 * purpose.
89805289402SAnshuman Khandual 	 */
8994878a888SGavin Shan 	page = (args->pte_pfn != ULONG_MAX) ? pfn_to_page(args->pte_pfn) : NULL;
9004878a888SGavin Shan 	if (!page)
90105289402SAnshuman Khandual 		return;
9024878a888SGavin Shan 
9034878a888SGavin Shan 	pr_debug("Validating swap migration\n");
90405289402SAnshuman Khandual 
90505289402SAnshuman Khandual 	/*
90623647618SAnshuman Khandual 	 * make_[readable|writable]_migration_entry() expects given page to
90723647618SAnshuman Khandual 	 * be locked, otherwise it stumbles upon a BUG_ON().
90805289402SAnshuman Khandual 	 */
90905289402SAnshuman Khandual 	__SetPageLocked(page);
9104dd845b5SAlistair Popple 	swp = make_writable_migration_entry(page_to_pfn(page));
91105289402SAnshuman Khandual 	WARN_ON(!is_migration_entry(swp));
9124dd845b5SAlistair Popple 	WARN_ON(!is_writable_migration_entry(swp));
91305289402SAnshuman Khandual 
9144dd845b5SAlistair Popple 	swp = make_readable_migration_entry(swp_offset(swp));
91505289402SAnshuman Khandual 	WARN_ON(!is_migration_entry(swp));
9164dd845b5SAlistair Popple 	WARN_ON(is_writable_migration_entry(swp));
91705289402SAnshuman Khandual 
9184dd845b5SAlistair Popple 	swp = make_readable_migration_entry(page_to_pfn(page));
91905289402SAnshuman Khandual 	WARN_ON(!is_migration_entry(swp));
9204dd845b5SAlistair Popple 	WARN_ON(is_writable_migration_entry(swp));
92105289402SAnshuman Khandual 	__ClearPageLocked(page);
92205289402SAnshuman Khandual }
92305289402SAnshuman Khandual 
92405289402SAnshuman Khandual #ifdef CONFIG_HUGETLB_PAGE
92536b77d1eSGavin Shan static void __init hugetlb_basic_tests(struct pgtable_debug_args *args)
92605289402SAnshuman Khandual {
92705289402SAnshuman Khandual 	struct page *page;
92805289402SAnshuman Khandual 	pte_t pte;
92905289402SAnshuman Khandual 
9306315df41SAnshuman Khandual 	pr_debug("Validating HugeTLB basic\n");
93105289402SAnshuman Khandual 	/*
93205289402SAnshuman Khandual 	 * Accessing the page associated with the pfn is safe here,
93305289402SAnshuman Khandual 	 * as it was previously derived from a real kernel symbol.
93405289402SAnshuman Khandual 	 */
93536b77d1eSGavin Shan 	page = pfn_to_page(args->fixed_pmd_pfn);
93636b77d1eSGavin Shan 	pte = mk_huge_pte(page, args->page_prot);
93705289402SAnshuman Khandual 
93805289402SAnshuman Khandual 	WARN_ON(!huge_pte_dirty(huge_pte_mkdirty(pte)));
93905289402SAnshuman Khandual 	WARN_ON(!huge_pte_write(huge_pte_mkwrite(huge_pte_wrprotect(pte))));
94005289402SAnshuman Khandual 	WARN_ON(huge_pte_write(huge_pte_wrprotect(huge_pte_mkwrite(pte))));
94105289402SAnshuman Khandual 
94205289402SAnshuman Khandual #ifdef CONFIG_ARCH_WANT_GENERAL_HUGETLB
94336b77d1eSGavin Shan 	pte = pfn_pte(args->fixed_pmd_pfn, args->page_prot);
94405289402SAnshuman Khandual 
94505289402SAnshuman Khandual 	WARN_ON(!pte_huge(pte_mkhuge(pte)));
94605289402SAnshuman Khandual #endif /* CONFIG_ARCH_WANT_GENERAL_HUGETLB */
94705289402SAnshuman Khandual }
94805289402SAnshuman Khandual #else  /* !CONFIG_HUGETLB_PAGE */
94936b77d1eSGavin Shan static void __init hugetlb_basic_tests(struct pgtable_debug_args *args) { }
95005289402SAnshuman Khandual #endif /* CONFIG_HUGETLB_PAGE */
95105289402SAnshuman Khandual 
95205289402SAnshuman Khandual #ifdef CONFIG_TRANSPARENT_HUGEPAGE
9534878a888SGavin Shan static void __init pmd_thp_tests(struct pgtable_debug_args *args)
95405289402SAnshuman Khandual {
95505289402SAnshuman Khandual 	pmd_t pmd;
95605289402SAnshuman Khandual 
95705289402SAnshuman Khandual 	if (!has_transparent_hugepage())
95805289402SAnshuman Khandual 		return;
95905289402SAnshuman Khandual 
9606315df41SAnshuman Khandual 	pr_debug("Validating PMD based THP\n");
96105289402SAnshuman Khandual 	/*
96205289402SAnshuman Khandual 	 * pmd_trans_huge() and pmd_present() must return positive after
96305289402SAnshuman Khandual 	 * MMU invalidation with pmd_mkinvalid(). This behavior is an
96405289402SAnshuman Khandual 	 * optimization for transparent huge page. pmd_trans_huge() must
96505289402SAnshuman Khandual 	 * be true if pmd_page() returns a valid THP to avoid taking the
96605289402SAnshuman Khandual 	 * pmd_lock when others walk over non transhuge pmds (i.e. there
96705289402SAnshuman Khandual 	 * are no THP allocated). Especially when splitting a THP and
96805289402SAnshuman Khandual 	 * removing the present bit from the pmd, pmd_trans_huge() still
96905289402SAnshuman Khandual 	 * needs to return true. pmd_present() should be true whenever
97005289402SAnshuman Khandual 	 * pmd_trans_huge() returns true.
97105289402SAnshuman Khandual 	 */
9724878a888SGavin Shan 	pmd = pfn_pmd(args->fixed_pmd_pfn, args->page_prot);
97305289402SAnshuman Khandual 	WARN_ON(!pmd_trans_huge(pmd_mkhuge(pmd)));
97405289402SAnshuman Khandual 
97505289402SAnshuman Khandual #ifndef __HAVE_ARCH_PMDP_INVALIDATE
97605289402SAnshuman Khandual 	WARN_ON(!pmd_trans_huge(pmd_mkinvalid(pmd_mkhuge(pmd))));
97705289402SAnshuman Khandual 	WARN_ON(!pmd_present(pmd_mkinvalid(pmd_mkhuge(pmd))));
97805289402SAnshuman Khandual #endif /* __HAVE_ARCH_PMDP_INVALIDATE */
97905289402SAnshuman Khandual }
98005289402SAnshuman Khandual 
98105289402SAnshuman Khandual #ifdef CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD
9824878a888SGavin Shan static void __init pud_thp_tests(struct pgtable_debug_args *args)
98305289402SAnshuman Khandual {
98405289402SAnshuman Khandual 	pud_t pud;
98505289402SAnshuman Khandual 
98605289402SAnshuman Khandual 	if (!has_transparent_hugepage())
98705289402SAnshuman Khandual 		return;
98805289402SAnshuman Khandual 
9896315df41SAnshuman Khandual 	pr_debug("Validating PUD based THP\n");
9904878a888SGavin Shan 	pud = pfn_pud(args->fixed_pud_pfn, args->page_prot);
99105289402SAnshuman Khandual 	WARN_ON(!pud_trans_huge(pud_mkhuge(pud)));
99205289402SAnshuman Khandual 
99305289402SAnshuman Khandual 	/*
99405289402SAnshuman Khandual 	 * pud_mkinvalid() has been dropped for now. Enable back
99505289402SAnshuman Khandual 	 * these tests when it comes back with a modified pud_present().
99605289402SAnshuman Khandual 	 *
99705289402SAnshuman Khandual 	 * WARN_ON(!pud_trans_huge(pud_mkinvalid(pud_mkhuge(pud))));
99805289402SAnshuman Khandual 	 * WARN_ON(!pud_present(pud_mkinvalid(pud_mkhuge(pud))));
99905289402SAnshuman Khandual 	 */
100005289402SAnshuman Khandual }
100105289402SAnshuman Khandual #else  /* !CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD */
10024878a888SGavin Shan static void __init pud_thp_tests(struct pgtable_debug_args *args) { }
100305289402SAnshuman Khandual #endif /* CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD */
100405289402SAnshuman Khandual #else  /* !CONFIG_TRANSPARENT_HUGEPAGE */
10054878a888SGavin Shan static void __init pmd_thp_tests(struct pgtable_debug_args *args) { }
10064878a888SGavin Shan static void __init pud_thp_tests(struct pgtable_debug_args *args) { }
100705289402SAnshuman Khandual #endif /* CONFIG_TRANSPARENT_HUGEPAGE */
100805289402SAnshuman Khandual 
1009399145f9SAnshuman Khandual static unsigned long __init get_random_vaddr(void)
1010399145f9SAnshuman Khandual {
1011399145f9SAnshuman Khandual 	unsigned long random_vaddr, random_pages, total_user_pages;
1012399145f9SAnshuman Khandual 
1013399145f9SAnshuman Khandual 	total_user_pages = (TASK_SIZE - FIRST_USER_ADDRESS) / PAGE_SIZE;
1014399145f9SAnshuman Khandual 
1015399145f9SAnshuman Khandual 	random_pages = get_random_long() % total_user_pages;
1016399145f9SAnshuman Khandual 	random_vaddr = FIRST_USER_ADDRESS + random_pages * PAGE_SIZE;
1017399145f9SAnshuman Khandual 
1018399145f9SAnshuman Khandual 	return random_vaddr;
1019399145f9SAnshuman Khandual }
1020399145f9SAnshuman Khandual 
10213c9b84f0SGavin Shan static void __init destroy_args(struct pgtable_debug_args *args)
10223c9b84f0SGavin Shan {
10233c9b84f0SGavin Shan 	struct page *page = NULL;
10243c9b84f0SGavin Shan 
10253c9b84f0SGavin Shan 	/* Free (huge) page */
10263c9b84f0SGavin Shan 	if (IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE) &&
10273c9b84f0SGavin Shan 	    IS_ENABLED(CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD) &&
10283c9b84f0SGavin Shan 	    has_transparent_hugepage() &&
10293c9b84f0SGavin Shan 	    args->pud_pfn != ULONG_MAX) {
10303c9b84f0SGavin Shan 		if (args->is_contiguous_page) {
10313c9b84f0SGavin Shan 			free_contig_range(args->pud_pfn,
10323c9b84f0SGavin Shan 					  (1 << (HPAGE_PUD_SHIFT - PAGE_SHIFT)));
10333c9b84f0SGavin Shan 		} else {
10343c9b84f0SGavin Shan 			page = pfn_to_page(args->pud_pfn);
10353c9b84f0SGavin Shan 			__free_pages(page, HPAGE_PUD_SHIFT - PAGE_SHIFT);
10363c9b84f0SGavin Shan 		}
10373c9b84f0SGavin Shan 
10383c9b84f0SGavin Shan 		args->pud_pfn = ULONG_MAX;
10393c9b84f0SGavin Shan 		args->pmd_pfn = ULONG_MAX;
10403c9b84f0SGavin Shan 		args->pte_pfn = ULONG_MAX;
10413c9b84f0SGavin Shan 	}
10423c9b84f0SGavin Shan 
10433c9b84f0SGavin Shan 	if (IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE) &&
10443c9b84f0SGavin Shan 	    has_transparent_hugepage() &&
10453c9b84f0SGavin Shan 	    args->pmd_pfn != ULONG_MAX) {
10463c9b84f0SGavin Shan 		if (args->is_contiguous_page) {
10473c9b84f0SGavin Shan 			free_contig_range(args->pmd_pfn, (1 << HPAGE_PMD_ORDER));
10483c9b84f0SGavin Shan 		} else {
10493c9b84f0SGavin Shan 			page = pfn_to_page(args->pmd_pfn);
10503c9b84f0SGavin Shan 			__free_pages(page, HPAGE_PMD_ORDER);
10513c9b84f0SGavin Shan 		}
10523c9b84f0SGavin Shan 
10533c9b84f0SGavin Shan 		args->pmd_pfn = ULONG_MAX;
10543c9b84f0SGavin Shan 		args->pte_pfn = ULONG_MAX;
10553c9b84f0SGavin Shan 	}
10563c9b84f0SGavin Shan 
10573c9b84f0SGavin Shan 	if (args->pte_pfn != ULONG_MAX) {
10583c9b84f0SGavin Shan 		page = pfn_to_page(args->pte_pfn);
10593c9b84f0SGavin Shan 		__free_pages(page, 0);
10603c9b84f0SGavin Shan 
10613c9b84f0SGavin Shan 		args->pte_pfn = ULONG_MAX;
10623c9b84f0SGavin Shan 	}
10633c9b84f0SGavin Shan 
10643c9b84f0SGavin Shan 	/* Free page table entries */
10653c9b84f0SGavin Shan 	if (args->start_ptep) {
10663c9b84f0SGavin Shan 		pte_free(args->mm, args->start_ptep);
10673c9b84f0SGavin Shan 		mm_dec_nr_ptes(args->mm);
10683c9b84f0SGavin Shan 	}
10693c9b84f0SGavin Shan 
10703c9b84f0SGavin Shan 	if (args->start_pmdp) {
10713c9b84f0SGavin Shan 		pmd_free(args->mm, args->start_pmdp);
10723c9b84f0SGavin Shan 		mm_dec_nr_pmds(args->mm);
10733c9b84f0SGavin Shan 	}
10743c9b84f0SGavin Shan 
10753c9b84f0SGavin Shan 	if (args->start_pudp) {
10763c9b84f0SGavin Shan 		pud_free(args->mm, args->start_pudp);
10773c9b84f0SGavin Shan 		mm_dec_nr_puds(args->mm);
10783c9b84f0SGavin Shan 	}
10793c9b84f0SGavin Shan 
10803c9b84f0SGavin Shan 	if (args->start_p4dp)
10813c9b84f0SGavin Shan 		p4d_free(args->mm, args->start_p4dp);
10823c9b84f0SGavin Shan 
10833c9b84f0SGavin Shan 	/* Free vma and mm struct */
10843c9b84f0SGavin Shan 	if (args->vma)
10853c9b84f0SGavin Shan 		vm_area_free(args->vma);
10863c9b84f0SGavin Shan 
10873c9b84f0SGavin Shan 	if (args->mm)
10883c9b84f0SGavin Shan 		mmdrop(args->mm);
10893c9b84f0SGavin Shan }
10903c9b84f0SGavin Shan 
10913c9b84f0SGavin Shan static struct page * __init
10923c9b84f0SGavin Shan debug_vm_pgtable_alloc_huge_page(struct pgtable_debug_args *args, int order)
10933c9b84f0SGavin Shan {
10943c9b84f0SGavin Shan 	struct page *page = NULL;
10953c9b84f0SGavin Shan 
10963c9b84f0SGavin Shan #ifdef CONFIG_CONTIG_ALLOC
10973c9b84f0SGavin Shan 	if (order >= MAX_ORDER) {
10983c9b84f0SGavin Shan 		page = alloc_contig_pages((1 << order), GFP_KERNEL,
10993c9b84f0SGavin Shan 					  first_online_node, NULL);
11003c9b84f0SGavin Shan 		if (page) {
11013c9b84f0SGavin Shan 			args->is_contiguous_page = true;
11023c9b84f0SGavin Shan 			return page;
11033c9b84f0SGavin Shan 		}
11043c9b84f0SGavin Shan 	}
11053c9b84f0SGavin Shan #endif
11063c9b84f0SGavin Shan 
11073c9b84f0SGavin Shan 	if (order < MAX_ORDER)
11083c9b84f0SGavin Shan 		page = alloc_pages(GFP_KERNEL, order);
11093c9b84f0SGavin Shan 
11103c9b84f0SGavin Shan 	return page;
11113c9b84f0SGavin Shan }
11123c9b84f0SGavin Shan 
11133c9b84f0SGavin Shan static int __init init_args(struct pgtable_debug_args *args)
11143c9b84f0SGavin Shan {
11153c9b84f0SGavin Shan 	struct page *page = NULL;
11163c9b84f0SGavin Shan 	phys_addr_t phys;
11173c9b84f0SGavin Shan 	int ret = 0;
11183c9b84f0SGavin Shan 
11193c9b84f0SGavin Shan 	/*
11203c9b84f0SGavin Shan 	 * Initialize the debugging data.
11213c9b84f0SGavin Shan 	 *
112231d17076SAnshuman Khandual 	 * vm_get_page_prot(VM_NONE) or vm_get_page_prot(VM_SHARED|VM_NONE)
112331d17076SAnshuman Khandual 	 * will help create page table entries with PROT_NONE permission as
112431d17076SAnshuman Khandual 	 * required for pxx_protnone_tests().
11253c9b84f0SGavin Shan 	 */
11263c9b84f0SGavin Shan 	memset(args, 0, sizeof(*args));
11273c9b84f0SGavin Shan 	args->vaddr              = get_random_vaddr();
11283c9b84f0SGavin Shan 	args->page_prot          = vm_get_page_prot(VMFLAGS);
112931d17076SAnshuman Khandual 	args->page_prot_none     = vm_get_page_prot(VM_NONE);
11303c9b84f0SGavin Shan 	args->is_contiguous_page = false;
11313c9b84f0SGavin Shan 	args->pud_pfn            = ULONG_MAX;
11323c9b84f0SGavin Shan 	args->pmd_pfn            = ULONG_MAX;
11333c9b84f0SGavin Shan 	args->pte_pfn            = ULONG_MAX;
11343c9b84f0SGavin Shan 	args->fixed_pgd_pfn      = ULONG_MAX;
11353c9b84f0SGavin Shan 	args->fixed_p4d_pfn      = ULONG_MAX;
11363c9b84f0SGavin Shan 	args->fixed_pud_pfn      = ULONG_MAX;
11373c9b84f0SGavin Shan 	args->fixed_pmd_pfn      = ULONG_MAX;
11383c9b84f0SGavin Shan 	args->fixed_pte_pfn      = ULONG_MAX;
11393c9b84f0SGavin Shan 
11403c9b84f0SGavin Shan 	/* Allocate mm and vma */
11413c9b84f0SGavin Shan 	args->mm = mm_alloc();
11423c9b84f0SGavin Shan 	if (!args->mm) {
11433c9b84f0SGavin Shan 		pr_err("Failed to allocate mm struct\n");
11443c9b84f0SGavin Shan 		ret = -ENOMEM;
11453c9b84f0SGavin Shan 		goto error;
11463c9b84f0SGavin Shan 	}
11473c9b84f0SGavin Shan 
11483c9b84f0SGavin Shan 	args->vma = vm_area_alloc(args->mm);
11493c9b84f0SGavin Shan 	if (!args->vma) {
11503c9b84f0SGavin Shan 		pr_err("Failed to allocate vma\n");
11513c9b84f0SGavin Shan 		ret = -ENOMEM;
11523c9b84f0SGavin Shan 		goto error;
11533c9b84f0SGavin Shan 	}
11543c9b84f0SGavin Shan 
11553c9b84f0SGavin Shan 	/*
11563c9b84f0SGavin Shan 	 * Allocate page table entries. They will be modified in the tests.
11573c9b84f0SGavin Shan 	 * Lets save the page table entries so that they can be released
11583c9b84f0SGavin Shan 	 * when the tests are completed.
11593c9b84f0SGavin Shan 	 */
11603c9b84f0SGavin Shan 	args->pgdp = pgd_offset(args->mm, args->vaddr);
11613c9b84f0SGavin Shan 	args->p4dp = p4d_alloc(args->mm, args->pgdp, args->vaddr);
11623c9b84f0SGavin Shan 	if (!args->p4dp) {
11633c9b84f0SGavin Shan 		pr_err("Failed to allocate p4d entries\n");
11643c9b84f0SGavin Shan 		ret = -ENOMEM;
11653c9b84f0SGavin Shan 		goto error;
11663c9b84f0SGavin Shan 	}
11673c9b84f0SGavin Shan 	args->start_p4dp = p4d_offset(args->pgdp, 0UL);
11683c9b84f0SGavin Shan 	WARN_ON(!args->start_p4dp);
11693c9b84f0SGavin Shan 
11703c9b84f0SGavin Shan 	args->pudp = pud_alloc(args->mm, args->p4dp, args->vaddr);
11713c9b84f0SGavin Shan 	if (!args->pudp) {
11723c9b84f0SGavin Shan 		pr_err("Failed to allocate pud entries\n");
11733c9b84f0SGavin Shan 		ret = -ENOMEM;
11743c9b84f0SGavin Shan 		goto error;
11753c9b84f0SGavin Shan 	}
11763c9b84f0SGavin Shan 	args->start_pudp = pud_offset(args->p4dp, 0UL);
11773c9b84f0SGavin Shan 	WARN_ON(!args->start_pudp);
11783c9b84f0SGavin Shan 
11793c9b84f0SGavin Shan 	args->pmdp = pmd_alloc(args->mm, args->pudp, args->vaddr);
11803c9b84f0SGavin Shan 	if (!args->pmdp) {
11813c9b84f0SGavin Shan 		pr_err("Failed to allocate pmd entries\n");
11823c9b84f0SGavin Shan 		ret = -ENOMEM;
11833c9b84f0SGavin Shan 		goto error;
11843c9b84f0SGavin Shan 	}
11853c9b84f0SGavin Shan 	args->start_pmdp = pmd_offset(args->pudp, 0UL);
11863c9b84f0SGavin Shan 	WARN_ON(!args->start_pmdp);
11873c9b84f0SGavin Shan 
11883c9b84f0SGavin Shan 	if (pte_alloc(args->mm, args->pmdp)) {
11893c9b84f0SGavin Shan 		pr_err("Failed to allocate pte entries\n");
11903c9b84f0SGavin Shan 		ret = -ENOMEM;
11913c9b84f0SGavin Shan 		goto error;
11923c9b84f0SGavin Shan 	}
11933c9b84f0SGavin Shan 	args->start_ptep = pmd_pgtable(READ_ONCE(*args->pmdp));
11943c9b84f0SGavin Shan 	WARN_ON(!args->start_ptep);
11953c9b84f0SGavin Shan 
11963c9b84f0SGavin Shan 	/*
11973c9b84f0SGavin Shan 	 * PFN for mapping at PTE level is determined from a standard kernel
11983c9b84f0SGavin Shan 	 * text symbol. But pfns for higher page table levels are derived by
11993c9b84f0SGavin Shan 	 * masking lower bits of this real pfn. These derived pfns might not
12003c9b84f0SGavin Shan 	 * exist on the platform but that does not really matter as pfn_pxx()
12013c9b84f0SGavin Shan 	 * helpers will still create appropriate entries for the test. This
12023c9b84f0SGavin Shan 	 * helps avoid large memory block allocations to be used for mapping
12033c9b84f0SGavin Shan 	 * at higher page table levels in some of the tests.
12043c9b84f0SGavin Shan 	 */
12053c9b84f0SGavin Shan 	phys = __pa_symbol(&start_kernel);
12063c9b84f0SGavin Shan 	args->fixed_pgd_pfn = __phys_to_pfn(phys & PGDIR_MASK);
12073c9b84f0SGavin Shan 	args->fixed_p4d_pfn = __phys_to_pfn(phys & P4D_MASK);
12083c9b84f0SGavin Shan 	args->fixed_pud_pfn = __phys_to_pfn(phys & PUD_MASK);
12093c9b84f0SGavin Shan 	args->fixed_pmd_pfn = __phys_to_pfn(phys & PMD_MASK);
12103c9b84f0SGavin Shan 	args->fixed_pte_pfn = __phys_to_pfn(phys & PAGE_MASK);
12113c9b84f0SGavin Shan 	WARN_ON(!pfn_valid(args->fixed_pte_pfn));
12123c9b84f0SGavin Shan 
12133c9b84f0SGavin Shan 	/*
12143c9b84f0SGavin Shan 	 * Allocate (huge) pages because some of the tests need to access
12153c9b84f0SGavin Shan 	 * the data in the pages. The corresponding tests will be skipped
12163c9b84f0SGavin Shan 	 * if we fail to allocate (huge) pages.
12173c9b84f0SGavin Shan 	 */
12183c9b84f0SGavin Shan 	if (IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE) &&
12193c9b84f0SGavin Shan 	    IS_ENABLED(CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD) &&
12203c9b84f0SGavin Shan 	    has_transparent_hugepage()) {
12213c9b84f0SGavin Shan 		page = debug_vm_pgtable_alloc_huge_page(args,
12223c9b84f0SGavin Shan 				HPAGE_PUD_SHIFT - PAGE_SHIFT);
12233c9b84f0SGavin Shan 		if (page) {
12243c9b84f0SGavin Shan 			args->pud_pfn = page_to_pfn(page);
12253c9b84f0SGavin Shan 			args->pmd_pfn = args->pud_pfn;
12263c9b84f0SGavin Shan 			args->pte_pfn = args->pud_pfn;
12273c9b84f0SGavin Shan 			return 0;
12283c9b84f0SGavin Shan 		}
12293c9b84f0SGavin Shan 	}
12303c9b84f0SGavin Shan 
12313c9b84f0SGavin Shan 	if (IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE) &&
12323c9b84f0SGavin Shan 	    has_transparent_hugepage()) {
12333c9b84f0SGavin Shan 		page = debug_vm_pgtable_alloc_huge_page(args, HPAGE_PMD_ORDER);
12343c9b84f0SGavin Shan 		if (page) {
12353c9b84f0SGavin Shan 			args->pmd_pfn = page_to_pfn(page);
12363c9b84f0SGavin Shan 			args->pte_pfn = args->pmd_pfn;
12373c9b84f0SGavin Shan 			return 0;
12383c9b84f0SGavin Shan 		}
12393c9b84f0SGavin Shan 	}
12403c9b84f0SGavin Shan 
12413c9b84f0SGavin Shan 	page = alloc_pages(GFP_KERNEL, 0);
12423c9b84f0SGavin Shan 	if (page)
12433c9b84f0SGavin Shan 		args->pte_pfn = page_to_pfn(page);
12443c9b84f0SGavin Shan 
12453c9b84f0SGavin Shan 	return 0;
12463c9b84f0SGavin Shan 
12473c9b84f0SGavin Shan error:
12483c9b84f0SGavin Shan 	destroy_args(args);
12493c9b84f0SGavin Shan 	return ret;
12503c9b84f0SGavin Shan }
12513c9b84f0SGavin Shan 
1252399145f9SAnshuman Khandual static int __init debug_vm_pgtable(void)
1253399145f9SAnshuman Khandual {
12543c9b84f0SGavin Shan 	struct pgtable_debug_args args;
1255fea1120cSKees Cook 	spinlock_t *ptl = NULL;
12563c9b84f0SGavin Shan 	int idx, ret;
1257399145f9SAnshuman Khandual 
1258399145f9SAnshuman Khandual 	pr_info("Validating architecture page table helpers\n");
12593c9b84f0SGavin Shan 	ret = init_args(&args);
12603c9b84f0SGavin Shan 	if (ret)
12613c9b84f0SGavin Shan 		return ret;
12623c9b84f0SGavin Shan 
12632e326c07SAnshuman Khandual 	/*
126431d17076SAnshuman Khandual 	 * Iterate over each possible vm_flags to make sure that all
12652e326c07SAnshuman Khandual 	 * the basic page table transformation validations just hold
12662e326c07SAnshuman Khandual 	 * true irrespective of the starting protection value for a
12672e326c07SAnshuman Khandual 	 * given page table entry.
126831d17076SAnshuman Khandual 	 *
126931d17076SAnshuman Khandual 	 * Protection based vm_flags combinatins are always linear
127031d17076SAnshuman Khandual 	 * and increasing i.e starting from VM_NONE and going upto
127131d17076SAnshuman Khandual 	 * (VM_SHARED | READ | WRITE | EXEC).
12722e326c07SAnshuman Khandual 	 */
127331d17076SAnshuman Khandual #define VM_FLAGS_START	(VM_NONE)
127431d17076SAnshuman Khandual #define VM_FLAGS_END	(VM_SHARED | VM_EXEC | VM_WRITE | VM_READ)
127531d17076SAnshuman Khandual 
127631d17076SAnshuman Khandual 	for (idx = VM_FLAGS_START; idx <= VM_FLAGS_END; idx++) {
127736b77d1eSGavin Shan 		pte_basic_tests(&args, idx);
127836b77d1eSGavin Shan 		pmd_basic_tests(&args, idx);
127936b77d1eSGavin Shan 		pud_basic_tests(&args, idx);
12802e326c07SAnshuman Khandual 	}
12812e326c07SAnshuman Khandual 
12822e326c07SAnshuman Khandual 	/*
12832e326c07SAnshuman Khandual 	 * Both P4D and PGD level tests are very basic which do not
12842e326c07SAnshuman Khandual 	 * involve creating page table entries from the protection
12852e326c07SAnshuman Khandual 	 * value and the given pfn. Hence just keep them out from
12862e326c07SAnshuman Khandual 	 * the above iteration for now to save some test execution
12872e326c07SAnshuman Khandual 	 * time.
12882e326c07SAnshuman Khandual 	 */
128936b77d1eSGavin Shan 	p4d_basic_tests(&args);
129036b77d1eSGavin Shan 	pgd_basic_tests(&args);
1291399145f9SAnshuman Khandual 
12928983d231SGavin Shan 	pmd_leaf_tests(&args);
12938983d231SGavin Shan 	pud_leaf_tests(&args);
1294a5c3b9ffSAnshuman Khandual 
12958983d231SGavin Shan 	pte_savedwrite_tests(&args);
12968983d231SGavin Shan 	pmd_savedwrite_tests(&args);
1297a5c3b9ffSAnshuman Khandual 
12988cb183f2SGavin Shan 	pte_special_tests(&args);
12998cb183f2SGavin Shan 	pte_protnone_tests(&args);
13008cb183f2SGavin Shan 	pmd_protnone_tests(&args);
130105289402SAnshuman Khandual 
13028cb183f2SGavin Shan 	pte_devmap_tests(&args);
13038cb183f2SGavin Shan 	pmd_devmap_tests(&args);
13048cb183f2SGavin Shan 	pud_devmap_tests(&args);
130505289402SAnshuman Khandual 
13065f447e80SGavin Shan 	pte_soft_dirty_tests(&args);
13075f447e80SGavin Shan 	pmd_soft_dirty_tests(&args);
13085f447e80SGavin Shan 	pte_swap_soft_dirty_tests(&args);
13095f447e80SGavin Shan 	pmd_swap_soft_dirty_tests(&args);
131005289402SAnshuman Khandual 
1311210d1e8aSDavid Hildenbrand 	pte_swap_exclusive_tests(&args);
1312210d1e8aSDavid Hildenbrand 
13135f447e80SGavin Shan 	pte_swap_tests(&args);
13145f447e80SGavin Shan 	pmd_swap_tests(&args);
131505289402SAnshuman Khandual 
13164878a888SGavin Shan 	swap_migration_tests(&args);
131705289402SAnshuman Khandual 
13184878a888SGavin Shan 	pmd_thp_tests(&args);
13194878a888SGavin Shan 	pud_thp_tests(&args);
132005289402SAnshuman Khandual 
132136b77d1eSGavin Shan 	hugetlb_basic_tests(&args);
1322e8edf0adSAneesh Kumar K.V 
13236f302e27SAneesh Kumar K.V 	/*
13246f302e27SAneesh Kumar K.V 	 * Page table modifying tests. They need to hold
13256f302e27SAneesh Kumar K.V 	 * proper page table lock.
13266f302e27SAneesh Kumar K.V 	 */
1327e8edf0adSAneesh Kumar K.V 
132844966c44SGavin Shan 	args.ptep = pte_offset_map_lock(args.mm, args.pmdp, args.vaddr, &ptl);
132944966c44SGavin Shan 	pte_clear_tests(&args);
133044966c44SGavin Shan 	pte_advanced_tests(&args);
133144966c44SGavin Shan 	pte_unmap_unlock(args.ptep, ptl);
1332e8edf0adSAneesh Kumar K.V 
1333c0fe07b0SGavin Shan 	ptl = pmd_lock(args.mm, args.pmdp);
1334c0fe07b0SGavin Shan 	pmd_clear_tests(&args);
1335c0fe07b0SGavin Shan 	pmd_advanced_tests(&args);
1336c0fe07b0SGavin Shan 	pmd_huge_tests(&args);
1337c0fe07b0SGavin Shan 	pmd_populate_tests(&args);
13386f302e27SAneesh Kumar K.V 	spin_unlock(ptl);
13396f302e27SAneesh Kumar K.V 
13404cbde03bSGavin Shan 	ptl = pud_lock(args.mm, args.pudp);
13414cbde03bSGavin Shan 	pud_clear_tests(&args);
13424cbde03bSGavin Shan 	pud_advanced_tests(&args);
13434cbde03bSGavin Shan 	pud_huge_tests(&args);
13444cbde03bSGavin Shan 	pud_populate_tests(&args);
13456f302e27SAneesh Kumar K.V 	spin_unlock(ptl);
13466f302e27SAneesh Kumar K.V 
13472f87f8c3SGavin Shan 	spin_lock(&(args.mm->page_table_lock));
13482f87f8c3SGavin Shan 	p4d_clear_tests(&args);
13492f87f8c3SGavin Shan 	pgd_clear_tests(&args);
13502f87f8c3SGavin Shan 	p4d_populate_tests(&args);
13512f87f8c3SGavin Shan 	pgd_populate_tests(&args);
13522f87f8c3SGavin Shan 	spin_unlock(&(args.mm->page_table_lock));
1353e8edf0adSAneesh Kumar K.V 
13543c9b84f0SGavin Shan 	destroy_args(&args);
1355399145f9SAnshuman Khandual 	return 0;
1356399145f9SAnshuman Khandual }
1357399145f9SAnshuman Khandual late_initcall(debug_vm_pgtable);
1358