xref: /openbmc/linux/mm/debug_vm_pgtable.c (revision 1760371b277718062211fc7eb6f3042c5051c1a5)
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>
18c4876ff6SFrank van der Linden #include <linux/memblock.h>
19399145f9SAnshuman Khandual #include <linux/mm.h>
20399145f9SAnshuman Khandual #include <linux/mman.h>
21399145f9SAnshuman Khandual #include <linux/mm_types.h>
22399145f9SAnshuman Khandual #include <linux/module.h>
23399145f9SAnshuman Khandual #include <linux/pfn_t.h>
24399145f9SAnshuman Khandual #include <linux/printk.h>
25a5c3b9ffSAnshuman Khandual #include <linux/pgtable.h>
26399145f9SAnshuman Khandual #include <linux/random.h>
27399145f9SAnshuman Khandual #include <linux/spinlock.h>
28399145f9SAnshuman Khandual #include <linux/swap.h>
29399145f9SAnshuman Khandual #include <linux/swapops.h>
30399145f9SAnshuman Khandual #include <linux/start_kernel.h>
31399145f9SAnshuman Khandual #include <linux/sched/mm.h>
3285a14463SAneesh Kumar K.V #include <linux/io.h>
338c5b3a8aSGavin Shan 
348c5b3a8aSGavin Shan #include <asm/cacheflush.h>
35399145f9SAnshuman Khandual #include <asm/pgalloc.h>
36a5c3b9ffSAnshuman Khandual #include <asm/tlbflush.h>
37399145f9SAnshuman Khandual 
38b1d00007SAnshuman Khandual /*
39ee65728eSMike Rapoport  * Please refer Documentation/mm/arch_pgtable_helpers.rst for the semantics
40b1d00007SAnshuman Khandual  * expectations that are being validated here. All future changes in here
41b1d00007SAnshuman Khandual  * or the documentation need to be in sync.
42399145f9SAnshuman Khandual  */
43399145f9SAnshuman Khandual #define RANDOM_NZVALUE	GENMASK(7, 0)
44399145f9SAnshuman Khandual 
453c9b84f0SGavin Shan struct pgtable_debug_args {
463c9b84f0SGavin Shan 	struct mm_struct	*mm;
473c9b84f0SGavin Shan 	struct vm_area_struct	*vma;
483c9b84f0SGavin Shan 
493c9b84f0SGavin Shan 	pgd_t			*pgdp;
503c9b84f0SGavin Shan 	p4d_t			*p4dp;
513c9b84f0SGavin Shan 	pud_t			*pudp;
523c9b84f0SGavin Shan 	pmd_t			*pmdp;
533c9b84f0SGavin Shan 	pte_t			*ptep;
543c9b84f0SGavin Shan 
553c9b84f0SGavin Shan 	p4d_t			*start_p4dp;
563c9b84f0SGavin Shan 	pud_t			*start_pudp;
573c9b84f0SGavin Shan 	pmd_t			*start_pmdp;
583c9b84f0SGavin Shan 	pgtable_t		start_ptep;
593c9b84f0SGavin Shan 
603c9b84f0SGavin Shan 	unsigned long		vaddr;
613c9b84f0SGavin Shan 	pgprot_t		page_prot;
623c9b84f0SGavin Shan 	pgprot_t		page_prot_none;
633c9b84f0SGavin Shan 
643c9b84f0SGavin Shan 	bool			is_contiguous_page;
653c9b84f0SGavin Shan 	unsigned long		pud_pfn;
663c9b84f0SGavin Shan 	unsigned long		pmd_pfn;
673c9b84f0SGavin Shan 	unsigned long		pte_pfn;
683c9b84f0SGavin Shan 
69c4876ff6SFrank van der Linden 	unsigned long		fixed_alignment;
703c9b84f0SGavin Shan 	unsigned long		fixed_pgd_pfn;
713c9b84f0SGavin Shan 	unsigned long		fixed_p4d_pfn;
723c9b84f0SGavin Shan 	unsigned long		fixed_pud_pfn;
733c9b84f0SGavin Shan 	unsigned long		fixed_pmd_pfn;
743c9b84f0SGavin Shan 	unsigned long		fixed_pte_pfn;
753c9b84f0SGavin Shan };
763c9b84f0SGavin Shan 
pte_basic_tests(struct pgtable_debug_args * args,int idx)7736b77d1eSGavin Shan static void __init pte_basic_tests(struct pgtable_debug_args *args, int idx)
78399145f9SAnshuman Khandual {
7931d17076SAnshuman Khandual 	pgprot_t prot = vm_get_page_prot(idx);
8036b77d1eSGavin Shan 	pte_t pte = pfn_pte(args->fixed_pte_pfn, prot);
812e326c07SAnshuman Khandual 	unsigned long val = idx, *ptr = &val;
82399145f9SAnshuman Khandual 
832e326c07SAnshuman Khandual 	pr_debug("Validating PTE basic (%pGv)\n", ptr);
84bb5c47ceSAnshuman Khandual 
85bb5c47ceSAnshuman Khandual 	/*
86bb5c47ceSAnshuman Khandual 	 * This test needs to be executed after the given page table entry
8731d17076SAnshuman Khandual 	 * is created with pfn_pte() to make sure that vm_get_page_prot(idx)
88bb5c47ceSAnshuman Khandual 	 * does not have the dirty bit enabled from the beginning. This is
89bb5c47ceSAnshuman Khandual 	 * important for platforms like arm64 where (!PTE_RDONLY) indicate
90bb5c47ceSAnshuman Khandual 	 * dirty bit being set.
91bb5c47ceSAnshuman Khandual 	 */
92bb5c47ceSAnshuman Khandual 	WARN_ON(pte_dirty(pte_wrprotect(pte)));
93bb5c47ceSAnshuman Khandual 
94399145f9SAnshuman Khandual 	WARN_ON(!pte_same(pte, pte));
95399145f9SAnshuman Khandual 	WARN_ON(!pte_young(pte_mkyoung(pte_mkold(pte))));
96399145f9SAnshuman Khandual 	WARN_ON(!pte_dirty(pte_mkdirty(pte_mkclean(pte))));
97161e393cSRick Edgecombe 	WARN_ON(!pte_write(pte_mkwrite(pte_wrprotect(pte), args->vma)));
98399145f9SAnshuman Khandual 	WARN_ON(pte_young(pte_mkold(pte_mkyoung(pte))));
99399145f9SAnshuman Khandual 	WARN_ON(pte_dirty(pte_mkclean(pte_mkdirty(pte))));
100161e393cSRick Edgecombe 	WARN_ON(pte_write(pte_wrprotect(pte_mkwrite(pte, args->vma))));
101bb5c47ceSAnshuman Khandual 	WARN_ON(pte_dirty(pte_wrprotect(pte_mkclean(pte))));
102bb5c47ceSAnshuman Khandual 	WARN_ON(!pte_dirty(pte_wrprotect(pte_mkdirty(pte))));
103399145f9SAnshuman Khandual }
104399145f9SAnshuman Khandual 
pte_advanced_tests(struct pgtable_debug_args * args)10544966c44SGavin Shan static void __init pte_advanced_tests(struct pgtable_debug_args *args)
106a5c3b9ffSAnshuman Khandual {
1078c5b3a8aSGavin Shan 	struct page *page;
108b593b90dSShixin Liu 	pte_t pte;
109a5c3b9ffSAnshuman Khandual 
110c3824e18SAneesh Kumar K.V 	/*
111c3824e18SAneesh Kumar K.V 	 * Architectures optimize set_pte_at by avoiding TLB flush.
112c3824e18SAneesh Kumar K.V 	 * This requires set_pte_at to be not used to update an
113c3824e18SAneesh Kumar K.V 	 * existing pte entry. Clear pte before we do set_pte_at
1148c5b3a8aSGavin Shan 	 *
1158c5b3a8aSGavin Shan 	 * flush_dcache_page() is called after set_pte_at() to clear
1168c5b3a8aSGavin Shan 	 * PG_arch_1 for the page on ARM64. The page flag isn't cleared
1178c5b3a8aSGavin Shan 	 * when it's released and page allocation check will fail when
1188c5b3a8aSGavin Shan 	 * the page is allocated again. For architectures other than ARM64,
1198c5b3a8aSGavin Shan 	 * the unexpected overhead of cache flushing is acceptable.
120c3824e18SAneesh Kumar K.V 	 */
1218c5b3a8aSGavin Shan 	page = (args->pte_pfn != ULONG_MAX) ? pfn_to_page(args->pte_pfn) : NULL;
1228c5b3a8aSGavin Shan 	if (!page)
12344966c44SGavin Shan 		return;
124c3824e18SAneesh Kumar K.V 
1256315df41SAnshuman Khandual 	pr_debug("Validating PTE advanced\n");
1269f2bad09SHugh Dickins 	if (WARN_ON(!args->ptep))
1279f2bad09SHugh Dickins 		return;
1289f2bad09SHugh Dickins 
12944966c44SGavin Shan 	pte = pfn_pte(args->pte_pfn, args->page_prot);
13044966c44SGavin Shan 	set_pte_at(args->mm, args->vaddr, args->ptep, pte);
1318c5b3a8aSGavin Shan 	flush_dcache_page(page);
13244966c44SGavin Shan 	ptep_set_wrprotect(args->mm, args->vaddr, args->ptep);
13344966c44SGavin Shan 	pte = ptep_get(args->ptep);
134a5c3b9ffSAnshuman Khandual 	WARN_ON(pte_write(pte));
13544966c44SGavin Shan 	ptep_get_and_clear(args->mm, args->vaddr, args->ptep);
13644966c44SGavin Shan 	pte = ptep_get(args->ptep);
137a5c3b9ffSAnshuman Khandual 	WARN_ON(!pte_none(pte));
138a5c3b9ffSAnshuman Khandual 
13944966c44SGavin Shan 	pte = pfn_pte(args->pte_pfn, args->page_prot);
140a5c3b9ffSAnshuman Khandual 	pte = pte_wrprotect(pte);
141a5c3b9ffSAnshuman Khandual 	pte = pte_mkclean(pte);
14244966c44SGavin Shan 	set_pte_at(args->mm, args->vaddr, args->ptep, pte);
1438c5b3a8aSGavin Shan 	flush_dcache_page(page);
144161e393cSRick Edgecombe 	pte = pte_mkwrite(pte, args->vma);
145a5c3b9ffSAnshuman Khandual 	pte = pte_mkdirty(pte);
14644966c44SGavin Shan 	ptep_set_access_flags(args->vma, args->vaddr, args->ptep, pte, 1);
14744966c44SGavin Shan 	pte = ptep_get(args->ptep);
148a5c3b9ffSAnshuman Khandual 	WARN_ON(!(pte_write(pte) && pte_dirty(pte)));
14944966c44SGavin Shan 	ptep_get_and_clear_full(args->mm, args->vaddr, args->ptep, 1);
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_mkyoung(pte);
15544966c44SGavin Shan 	set_pte_at(args->mm, args->vaddr, args->ptep, pte);
1568c5b3a8aSGavin Shan 	flush_dcache_page(page);
15744966c44SGavin Shan 	ptep_test_and_clear_young(args->vma, args->vaddr, args->ptep);
15844966c44SGavin Shan 	pte = ptep_get(args->ptep);
159a5c3b9ffSAnshuman Khandual 	WARN_ON(pte_young(pte));
160fb5222aaSPasha Tatashin 
161fb5222aaSPasha Tatashin 	ptep_get_and_clear_full(args->mm, args->vaddr, args->ptep, 1);
162a5c3b9ffSAnshuman Khandual }
163a5c3b9ffSAnshuman Khandual 
164399145f9SAnshuman Khandual #ifdef CONFIG_TRANSPARENT_HUGEPAGE
pmd_basic_tests(struct pgtable_debug_args * args,int idx)16536b77d1eSGavin Shan static void __init pmd_basic_tests(struct pgtable_debug_args *args, int idx)
166399145f9SAnshuman Khandual {
16731d17076SAnshuman Khandual 	pgprot_t prot = vm_get_page_prot(idx);
1682e326c07SAnshuman Khandual 	unsigned long val = idx, *ptr = &val;
16965ac1a60SAnshuman Khandual 	pmd_t pmd;
170399145f9SAnshuman Khandual 
171787d563bSAneesh Kumar K.V 	if (!has_transparent_hugepage())
172787d563bSAneesh Kumar K.V 		return;
173787d563bSAneesh Kumar K.V 
1742e326c07SAnshuman Khandual 	pr_debug("Validating PMD basic (%pGv)\n", ptr);
17536b77d1eSGavin Shan 	pmd = pfn_pmd(args->fixed_pmd_pfn, prot);
176bb5c47ceSAnshuman Khandual 
177bb5c47ceSAnshuman Khandual 	/*
178bb5c47ceSAnshuman Khandual 	 * This test needs to be executed after the given page table entry
17931d17076SAnshuman Khandual 	 * is created with pfn_pmd() to make sure that vm_get_page_prot(idx)
180bb5c47ceSAnshuman Khandual 	 * does not have the dirty bit enabled from the beginning. This is
181bb5c47ceSAnshuman Khandual 	 * important for platforms like arm64 where (!PTE_RDONLY) indicate
182bb5c47ceSAnshuman Khandual 	 * dirty bit being set.
183bb5c47ceSAnshuman Khandual 	 */
184bb5c47ceSAnshuman Khandual 	WARN_ON(pmd_dirty(pmd_wrprotect(pmd)));
185bb5c47ceSAnshuman Khandual 
186bb5c47ceSAnshuman Khandual 
187399145f9SAnshuman Khandual 	WARN_ON(!pmd_same(pmd, pmd));
188399145f9SAnshuman Khandual 	WARN_ON(!pmd_young(pmd_mkyoung(pmd_mkold(pmd))));
189399145f9SAnshuman Khandual 	WARN_ON(!pmd_dirty(pmd_mkdirty(pmd_mkclean(pmd))));
190161e393cSRick Edgecombe 	WARN_ON(!pmd_write(pmd_mkwrite(pmd_wrprotect(pmd), args->vma)));
191399145f9SAnshuman Khandual 	WARN_ON(pmd_young(pmd_mkold(pmd_mkyoung(pmd))));
192399145f9SAnshuman Khandual 	WARN_ON(pmd_dirty(pmd_mkclean(pmd_mkdirty(pmd))));
193161e393cSRick Edgecombe 	WARN_ON(pmd_write(pmd_wrprotect(pmd_mkwrite(pmd, args->vma))));
194bb5c47ceSAnshuman Khandual 	WARN_ON(pmd_dirty(pmd_wrprotect(pmd_mkclean(pmd))));
195bb5c47ceSAnshuman Khandual 	WARN_ON(!pmd_dirty(pmd_wrprotect(pmd_mkdirty(pmd))));
196399145f9SAnshuman Khandual 	/*
197399145f9SAnshuman Khandual 	 * A huge page does not point to next level page table
198399145f9SAnshuman Khandual 	 * entry. Hence this must qualify as pmd_bad().
199399145f9SAnshuman Khandual 	 */
200399145f9SAnshuman Khandual 	WARN_ON(!pmd_bad(pmd_mkhuge(pmd)));
201399145f9SAnshuman Khandual }
202399145f9SAnshuman Khandual 
pmd_advanced_tests(struct pgtable_debug_args * args)203c0fe07b0SGavin Shan static void __init pmd_advanced_tests(struct pgtable_debug_args *args)
204a5c3b9ffSAnshuman Khandual {
2058c5b3a8aSGavin Shan 	struct page *page;
20665ac1a60SAnshuman Khandual 	pmd_t pmd;
207c0fe07b0SGavin Shan 	unsigned long vaddr = args->vaddr;
208a5c3b9ffSAnshuman Khandual 
209a5c3b9ffSAnshuman Khandual 	if (!has_transparent_hugepage())
210a5c3b9ffSAnshuman Khandual 		return;
211a5c3b9ffSAnshuman Khandual 
2128c5b3a8aSGavin Shan 	page = (args->pmd_pfn != ULONG_MAX) ? pfn_to_page(args->pmd_pfn) : NULL;
2138c5b3a8aSGavin Shan 	if (!page)
214c0fe07b0SGavin Shan 		return;
215c0fe07b0SGavin Shan 
2168c5b3a8aSGavin Shan 	/*
2178c5b3a8aSGavin Shan 	 * flush_dcache_page() is called after set_pmd_at() to clear
2188c5b3a8aSGavin Shan 	 * PG_arch_1 for the page on ARM64. The page flag isn't cleared
2198c5b3a8aSGavin Shan 	 * when it's released and page allocation check will fail when
2208c5b3a8aSGavin Shan 	 * the page is allocated again. For architectures other than ARM64,
2218c5b3a8aSGavin Shan 	 * the unexpected overhead of cache flushing is acceptable.
2228c5b3a8aSGavin Shan 	 */
2236315df41SAnshuman Khandual 	pr_debug("Validating PMD advanced\n");
224a5c3b9ffSAnshuman Khandual 	/* Align the address wrt HPAGE_PMD_SIZE */
22504f7ce3fSGerald Schaefer 	vaddr &= HPAGE_PMD_MASK;
226a5c3b9ffSAnshuman Khandual 
227c0fe07b0SGavin Shan 	pgtable_trans_huge_deposit(args->mm, args->pmdp, args->start_ptep);
22887f34986SAneesh Kumar K.V 
229c0fe07b0SGavin Shan 	pmd = pfn_pmd(args->pmd_pfn, args->page_prot);
230c0fe07b0SGavin Shan 	set_pmd_at(args->mm, vaddr, args->pmdp, pmd);
2318c5b3a8aSGavin Shan 	flush_dcache_page(page);
232c0fe07b0SGavin Shan 	pmdp_set_wrprotect(args->mm, vaddr, args->pmdp);
233c0fe07b0SGavin Shan 	pmd = READ_ONCE(*args->pmdp);
234a5c3b9ffSAnshuman Khandual 	WARN_ON(pmd_write(pmd));
235c0fe07b0SGavin Shan 	pmdp_huge_get_and_clear(args->mm, vaddr, args->pmdp);
236c0fe07b0SGavin Shan 	pmd = READ_ONCE(*args->pmdp);
237a5c3b9ffSAnshuman Khandual 	WARN_ON(!pmd_none(pmd));
238a5c3b9ffSAnshuman Khandual 
239c0fe07b0SGavin Shan 	pmd = pfn_pmd(args->pmd_pfn, args->page_prot);
240a5c3b9ffSAnshuman Khandual 	pmd = pmd_wrprotect(pmd);
241a5c3b9ffSAnshuman Khandual 	pmd = pmd_mkclean(pmd);
242c0fe07b0SGavin Shan 	set_pmd_at(args->mm, vaddr, args->pmdp, pmd);
2438c5b3a8aSGavin Shan 	flush_dcache_page(page);
244161e393cSRick Edgecombe 	pmd = pmd_mkwrite(pmd, args->vma);
245a5c3b9ffSAnshuman Khandual 	pmd = pmd_mkdirty(pmd);
246c0fe07b0SGavin Shan 	pmdp_set_access_flags(args->vma, vaddr, args->pmdp, pmd, 1);
247c0fe07b0SGavin Shan 	pmd = READ_ONCE(*args->pmdp);
248a5c3b9ffSAnshuman Khandual 	WARN_ON(!(pmd_write(pmd) && pmd_dirty(pmd)));
249c0fe07b0SGavin Shan 	pmdp_huge_get_and_clear_full(args->vma, vaddr, args->pmdp, 1);
250c0fe07b0SGavin Shan 	pmd = READ_ONCE(*args->pmdp);
251a5c3b9ffSAnshuman Khandual 	WARN_ON(!pmd_none(pmd));
252a5c3b9ffSAnshuman Khandual 
253c0fe07b0SGavin Shan 	pmd = pmd_mkhuge(pfn_pmd(args->pmd_pfn, args->page_prot));
254a5c3b9ffSAnshuman Khandual 	pmd = pmd_mkyoung(pmd);
255c0fe07b0SGavin Shan 	set_pmd_at(args->mm, vaddr, args->pmdp, pmd);
2568c5b3a8aSGavin Shan 	flush_dcache_page(page);
257c0fe07b0SGavin Shan 	pmdp_test_and_clear_young(args->vma, vaddr, args->pmdp);
258c0fe07b0SGavin Shan 	pmd = READ_ONCE(*args->pmdp);
259a5c3b9ffSAnshuman Khandual 	WARN_ON(pmd_young(pmd));
26087f34986SAneesh Kumar K.V 
26113af0506SAneesh Kumar K.V 	/*  Clear the pte entries  */
262c0fe07b0SGavin Shan 	pmdp_huge_get_and_clear(args->mm, vaddr, args->pmdp);
263c0fe07b0SGavin Shan 	pgtable_trans_huge_withdraw(args->mm, args->pmdp);
264a5c3b9ffSAnshuman Khandual }
265a5c3b9ffSAnshuman Khandual 
pmd_leaf_tests(struct pgtable_debug_args * args)2668983d231SGavin Shan static void __init pmd_leaf_tests(struct pgtable_debug_args *args)
267a5c3b9ffSAnshuman Khandual {
26865ac1a60SAnshuman Khandual 	pmd_t pmd;
26965ac1a60SAnshuman Khandual 
27065ac1a60SAnshuman Khandual 	if (!has_transparent_hugepage())
27165ac1a60SAnshuman Khandual 		return;
272a5c3b9ffSAnshuman Khandual 
2736315df41SAnshuman Khandual 	pr_debug("Validating PMD leaf\n");
2748983d231SGavin Shan 	pmd = pfn_pmd(args->fixed_pmd_pfn, args->page_prot);
27565ac1a60SAnshuman Khandual 
276a5c3b9ffSAnshuman Khandual 	/*
277a5c3b9ffSAnshuman Khandual 	 * PMD based THP is a leaf entry.
278a5c3b9ffSAnshuman Khandual 	 */
279a5c3b9ffSAnshuman Khandual 	pmd = pmd_mkhuge(pmd);
280a5c3b9ffSAnshuman Khandual 	WARN_ON(!pmd_leaf(pmd));
281a5c3b9ffSAnshuman Khandual }
282a5c3b9ffSAnshuman Khandual 
283399145f9SAnshuman Khandual #ifdef CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD
pud_basic_tests(struct pgtable_debug_args * args,int idx)28436b77d1eSGavin Shan static void __init pud_basic_tests(struct pgtable_debug_args *args, int idx)
285399145f9SAnshuman Khandual {
28631d17076SAnshuman Khandual 	pgprot_t prot = vm_get_page_prot(idx);
2872e326c07SAnshuman Khandual 	unsigned long val = idx, *ptr = &val;
28865ac1a60SAnshuman Khandual 	pud_t pud;
289399145f9SAnshuman Khandual 
290348ad160SAneesh Kumar K.V 	if (!has_transparent_pud_hugepage())
291787d563bSAneesh Kumar K.V 		return;
292787d563bSAneesh Kumar K.V 
2932e326c07SAnshuman Khandual 	pr_debug("Validating PUD basic (%pGv)\n", ptr);
29436b77d1eSGavin Shan 	pud = pfn_pud(args->fixed_pud_pfn, prot);
295bb5c47ceSAnshuman Khandual 
296bb5c47ceSAnshuman Khandual 	/*
297bb5c47ceSAnshuman Khandual 	 * This test needs to be executed after the given page table entry
29831d17076SAnshuman Khandual 	 * is created with pfn_pud() to make sure that vm_get_page_prot(idx)
299bb5c47ceSAnshuman Khandual 	 * does not have the dirty bit enabled from the beginning. This is
300bb5c47ceSAnshuman Khandual 	 * important for platforms like arm64 where (!PTE_RDONLY) indicate
301bb5c47ceSAnshuman Khandual 	 * dirty bit being set.
302bb5c47ceSAnshuman Khandual 	 */
303bb5c47ceSAnshuman Khandual 	WARN_ON(pud_dirty(pud_wrprotect(pud)));
304bb5c47ceSAnshuman Khandual 
305399145f9SAnshuman Khandual 	WARN_ON(!pud_same(pud, pud));
306399145f9SAnshuman Khandual 	WARN_ON(!pud_young(pud_mkyoung(pud_mkold(pud))));
307bb5c47ceSAnshuman Khandual 	WARN_ON(!pud_dirty(pud_mkdirty(pud_mkclean(pud))));
308bb5c47ceSAnshuman Khandual 	WARN_ON(pud_dirty(pud_mkclean(pud_mkdirty(pud))));
309399145f9SAnshuman Khandual 	WARN_ON(!pud_write(pud_mkwrite(pud_wrprotect(pud))));
310399145f9SAnshuman Khandual 	WARN_ON(pud_write(pud_wrprotect(pud_mkwrite(pud))));
311399145f9SAnshuman Khandual 	WARN_ON(pud_young(pud_mkold(pud_mkyoung(pud))));
312bb5c47ceSAnshuman Khandual 	WARN_ON(pud_dirty(pud_wrprotect(pud_mkclean(pud))));
313bb5c47ceSAnshuman Khandual 	WARN_ON(!pud_dirty(pud_wrprotect(pud_mkdirty(pud))));
314399145f9SAnshuman Khandual 
31536b77d1eSGavin Shan 	if (mm_pmd_folded(args->mm))
316399145f9SAnshuman Khandual 		return;
317399145f9SAnshuman Khandual 
318399145f9SAnshuman Khandual 	/*
319399145f9SAnshuman Khandual 	 * A huge page does not point to next level page table
320399145f9SAnshuman Khandual 	 * entry. Hence this must qualify as pud_bad().
321399145f9SAnshuman Khandual 	 */
322399145f9SAnshuman Khandual 	WARN_ON(!pud_bad(pud_mkhuge(pud)));
323399145f9SAnshuman Khandual }
324a5c3b9ffSAnshuman Khandual 
pud_advanced_tests(struct pgtable_debug_args * args)3254cbde03bSGavin Shan static void __init pud_advanced_tests(struct pgtable_debug_args *args)
326a5c3b9ffSAnshuman Khandual {
3278c5b3a8aSGavin Shan 	struct page *page;
3284cbde03bSGavin Shan 	unsigned long vaddr = args->vaddr;
32965ac1a60SAnshuman Khandual 	pud_t pud;
330a5c3b9ffSAnshuman Khandual 
331348ad160SAneesh Kumar K.V 	if (!has_transparent_pud_hugepage())
332a5c3b9ffSAnshuman Khandual 		return;
333a5c3b9ffSAnshuman Khandual 
3348c5b3a8aSGavin Shan 	page = (args->pud_pfn != ULONG_MAX) ? pfn_to_page(args->pud_pfn) : NULL;
3358c5b3a8aSGavin Shan 	if (!page)
3364cbde03bSGavin Shan 		return;
3374cbde03bSGavin Shan 
3388c5b3a8aSGavin Shan 	/*
3398c5b3a8aSGavin Shan 	 * flush_dcache_page() is called after set_pud_at() to clear
3408c5b3a8aSGavin Shan 	 * PG_arch_1 for the page on ARM64. The page flag isn't cleared
3418c5b3a8aSGavin Shan 	 * when it's released and page allocation check will fail when
3428c5b3a8aSGavin Shan 	 * the page is allocated again. For architectures other than ARM64,
3438c5b3a8aSGavin Shan 	 * the unexpected overhead of cache flushing is acceptable.
3448c5b3a8aSGavin Shan 	 */
3456315df41SAnshuman Khandual 	pr_debug("Validating PUD advanced\n");
346a5c3b9ffSAnshuman Khandual 	/* Align the address wrt HPAGE_PUD_SIZE */
34704f7ce3fSGerald Schaefer 	vaddr &= HPAGE_PUD_MASK;
348a5c3b9ffSAnshuman Khandual 
3494cbde03bSGavin Shan 	pud = pfn_pud(args->pud_pfn, args->page_prot);
350d2a9510cSAneesh Kumar K.V (IBM) 	/*
351d2a9510cSAneesh Kumar K.V (IBM) 	 * Some architectures have debug checks to make sure
352d2a9510cSAneesh Kumar K.V (IBM) 	 * huge pud mapping are only found with devmap entries
353d2a9510cSAneesh Kumar K.V (IBM) 	 * For now test with only devmap entries.
354d2a9510cSAneesh Kumar K.V (IBM) 	 */
355d2a9510cSAneesh Kumar K.V (IBM) 	pud = pud_mkdevmap(pud);
3564cbde03bSGavin Shan 	set_pud_at(args->mm, vaddr, args->pudp, pud);
3578c5b3a8aSGavin Shan 	flush_dcache_page(page);
3584cbde03bSGavin Shan 	pudp_set_wrprotect(args->mm, vaddr, args->pudp);
3594cbde03bSGavin Shan 	pud = READ_ONCE(*args->pudp);
360a5c3b9ffSAnshuman Khandual 	WARN_ON(pud_write(pud));
361a5c3b9ffSAnshuman Khandual 
362a5c3b9ffSAnshuman Khandual #ifndef __PAGETABLE_PMD_FOLDED
3634cbde03bSGavin Shan 	pudp_huge_get_and_clear(args->mm, vaddr, args->pudp);
3644cbde03bSGavin Shan 	pud = READ_ONCE(*args->pudp);
365a5c3b9ffSAnshuman Khandual 	WARN_ON(!pud_none(pud));
366a5c3b9ffSAnshuman Khandual #endif /* __PAGETABLE_PMD_FOLDED */
3674cbde03bSGavin Shan 	pud = pfn_pud(args->pud_pfn, args->page_prot);
368d2a9510cSAneesh Kumar K.V (IBM) 	pud = pud_mkdevmap(pud);
369a5c3b9ffSAnshuman Khandual 	pud = pud_wrprotect(pud);
370a5c3b9ffSAnshuman Khandual 	pud = pud_mkclean(pud);
3714cbde03bSGavin Shan 	set_pud_at(args->mm, vaddr, args->pudp, pud);
3728c5b3a8aSGavin Shan 	flush_dcache_page(page);
373a5c3b9ffSAnshuman Khandual 	pud = pud_mkwrite(pud);
374a5c3b9ffSAnshuman Khandual 	pud = pud_mkdirty(pud);
3754cbde03bSGavin Shan 	pudp_set_access_flags(args->vma, vaddr, args->pudp, pud, 1);
3764cbde03bSGavin Shan 	pud = READ_ONCE(*args->pudp);
377a5c3b9ffSAnshuman Khandual 	WARN_ON(!(pud_write(pud) && pud_dirty(pud)));
378a5c3b9ffSAnshuman Khandual 
379c3824e18SAneesh Kumar K.V #ifndef __PAGETABLE_PMD_FOLDED
380f32928abSAneesh Kumar K.V 	pudp_huge_get_and_clear_full(args->vma, vaddr, args->pudp, 1);
3814cbde03bSGavin Shan 	pud = READ_ONCE(*args->pudp);
382c3824e18SAneesh Kumar K.V 	WARN_ON(!pud_none(pud));
383c3824e18SAneesh Kumar K.V #endif /* __PAGETABLE_PMD_FOLDED */
384c3824e18SAneesh Kumar K.V 
3854cbde03bSGavin Shan 	pud = pfn_pud(args->pud_pfn, args->page_prot);
386d2a9510cSAneesh Kumar K.V (IBM) 	pud = pud_mkdevmap(pud);
387a5c3b9ffSAnshuman Khandual 	pud = pud_mkyoung(pud);
3884cbde03bSGavin Shan 	set_pud_at(args->mm, vaddr, args->pudp, pud);
3898c5b3a8aSGavin Shan 	flush_dcache_page(page);
3904cbde03bSGavin Shan 	pudp_test_and_clear_young(args->vma, vaddr, args->pudp);
3914cbde03bSGavin Shan 	pud = READ_ONCE(*args->pudp);
392a5c3b9ffSAnshuman Khandual 	WARN_ON(pud_young(pud));
39313af0506SAneesh Kumar K.V 
3944cbde03bSGavin Shan 	pudp_huge_get_and_clear(args->mm, vaddr, args->pudp);
395a5c3b9ffSAnshuman Khandual }
396a5c3b9ffSAnshuman Khandual 
pud_leaf_tests(struct pgtable_debug_args * args)3978983d231SGavin Shan static void __init pud_leaf_tests(struct pgtable_debug_args *args)
398a5c3b9ffSAnshuman Khandual {
39965ac1a60SAnshuman Khandual 	pud_t pud;
40065ac1a60SAnshuman Khandual 
401348ad160SAneesh Kumar K.V 	if (!has_transparent_pud_hugepage())
40265ac1a60SAnshuman Khandual 		return;
403a5c3b9ffSAnshuman Khandual 
4046315df41SAnshuman Khandual 	pr_debug("Validating PUD leaf\n");
4058983d231SGavin Shan 	pud = pfn_pud(args->fixed_pud_pfn, args->page_prot);
406a5c3b9ffSAnshuman Khandual 	/*
407a5c3b9ffSAnshuman Khandual 	 * PUD based THP is a leaf entry.
408a5c3b9ffSAnshuman Khandual 	 */
409a5c3b9ffSAnshuman Khandual 	pud = pud_mkhuge(pud);
410a5c3b9ffSAnshuman Khandual 	WARN_ON(!pud_leaf(pud));
411a5c3b9ffSAnshuman Khandual }
4125fe77be6SShixin Liu #else  /* !CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD */
pud_basic_tests(struct pgtable_debug_args * args,int idx)41336b77d1eSGavin Shan static void __init pud_basic_tests(struct pgtable_debug_args *args, int idx) { }
pud_advanced_tests(struct pgtable_debug_args * args)4144cbde03bSGavin Shan static void __init pud_advanced_tests(struct pgtable_debug_args *args) { }
pud_leaf_tests(struct pgtable_debug_args * args)4158983d231SGavin Shan static void __init pud_leaf_tests(struct pgtable_debug_args *args) { }
4165fe77be6SShixin Liu #endif /* CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD */
4175fe77be6SShixin Liu #else  /* !CONFIG_TRANSPARENT_HUGEPAGE */
pmd_basic_tests(struct pgtable_debug_args * args,int idx)41836b77d1eSGavin Shan static void __init pmd_basic_tests(struct pgtable_debug_args *args, int idx) { }
pud_basic_tests(struct pgtable_debug_args * args,int idx)41936b77d1eSGavin Shan static void __init pud_basic_tests(struct pgtable_debug_args *args, int idx) { }
pmd_advanced_tests(struct pgtable_debug_args * args)420c0fe07b0SGavin Shan static void __init pmd_advanced_tests(struct pgtable_debug_args *args) { }
pud_advanced_tests(struct pgtable_debug_args * args)4214cbde03bSGavin Shan static void __init pud_advanced_tests(struct pgtable_debug_args *args) { }
pmd_leaf_tests(struct pgtable_debug_args * args)4228983d231SGavin Shan static void __init pmd_leaf_tests(struct pgtable_debug_args *args) { }
pud_leaf_tests(struct pgtable_debug_args * args)4238983d231SGavin Shan static void __init pud_leaf_tests(struct pgtable_debug_args *args) { }
4245fe77be6SShixin Liu #endif /* CONFIG_TRANSPARENT_HUGEPAGE */
425a5c3b9ffSAnshuman Khandual 
42685a14463SAneesh Kumar K.V #ifdef CONFIG_HAVE_ARCH_HUGE_VMAP
pmd_huge_tests(struct pgtable_debug_args * args)427c0fe07b0SGavin Shan static void __init pmd_huge_tests(struct pgtable_debug_args *args)
4285fe77be6SShixin Liu {
4295fe77be6SShixin Liu 	pmd_t pmd;
4305fe77be6SShixin Liu 
431c4876ff6SFrank van der Linden 	if (!arch_vmap_pmd_supported(args->page_prot) ||
432c4876ff6SFrank van der Linden 	    args->fixed_alignment < PMD_SIZE)
4335fe77be6SShixin Liu 		return;
4345fe77be6SShixin Liu 
4355fe77be6SShixin Liu 	pr_debug("Validating PMD huge\n");
4365fe77be6SShixin Liu 	/*
4375fe77be6SShixin Liu 	 * X86 defined pmd_set_huge() verifies that the given
4385fe77be6SShixin Liu 	 * PMD is not a populated non-leaf entry.
4395fe77be6SShixin Liu 	 */
440c0fe07b0SGavin Shan 	WRITE_ONCE(*args->pmdp, __pmd(0));
441c0fe07b0SGavin Shan 	WARN_ON(!pmd_set_huge(args->pmdp, __pfn_to_phys(args->fixed_pmd_pfn), args->page_prot));
442c0fe07b0SGavin Shan 	WARN_ON(!pmd_clear_huge(args->pmdp));
443c0fe07b0SGavin Shan 	pmd = READ_ONCE(*args->pmdp);
4445fe77be6SShixin Liu 	WARN_ON(!pmd_none(pmd));
4455fe77be6SShixin Liu }
4465fe77be6SShixin Liu 
pud_huge_tests(struct pgtable_debug_args * args)4474cbde03bSGavin Shan static void __init pud_huge_tests(struct pgtable_debug_args *args)
448a5c3b9ffSAnshuman Khandual {
449a5c3b9ffSAnshuman Khandual 	pud_t pud;
450a5c3b9ffSAnshuman Khandual 
451c4876ff6SFrank van der Linden 	if (!arch_vmap_pud_supported(args->page_prot) ||
452c4876ff6SFrank van der Linden 	    args->fixed_alignment < PUD_SIZE)
453a5c3b9ffSAnshuman Khandual 		return;
4546315df41SAnshuman Khandual 
4556315df41SAnshuman Khandual 	pr_debug("Validating PUD huge\n");
456a5c3b9ffSAnshuman Khandual 	/*
457a5c3b9ffSAnshuman Khandual 	 * X86 defined pud_set_huge() verifies that the given
458a5c3b9ffSAnshuman Khandual 	 * PUD is not a populated non-leaf entry.
459a5c3b9ffSAnshuman Khandual 	 */
4604cbde03bSGavin Shan 	WRITE_ONCE(*args->pudp, __pud(0));
4614cbde03bSGavin Shan 	WARN_ON(!pud_set_huge(args->pudp, __pfn_to_phys(args->fixed_pud_pfn), args->page_prot));
4624cbde03bSGavin Shan 	WARN_ON(!pud_clear_huge(args->pudp));
4634cbde03bSGavin Shan 	pud = READ_ONCE(*args->pudp);
464a5c3b9ffSAnshuman Khandual 	WARN_ON(!pud_none(pud));
465a5c3b9ffSAnshuman Khandual }
46685a14463SAneesh Kumar K.V #else /* !CONFIG_HAVE_ARCH_HUGE_VMAP */
pmd_huge_tests(struct pgtable_debug_args * args)467c0fe07b0SGavin Shan static void __init pmd_huge_tests(struct pgtable_debug_args *args) { }
pud_huge_tests(struct pgtable_debug_args * args)4684cbde03bSGavin Shan static void __init pud_huge_tests(struct pgtable_debug_args *args) { }
4695fe77be6SShixin Liu #endif /* CONFIG_HAVE_ARCH_HUGE_VMAP */
470399145f9SAnshuman Khandual 
p4d_basic_tests(struct pgtable_debug_args * args)47136b77d1eSGavin Shan static void __init p4d_basic_tests(struct pgtable_debug_args *args)
472399145f9SAnshuman Khandual {
473399145f9SAnshuman Khandual 	p4d_t p4d;
474399145f9SAnshuman Khandual 
4756315df41SAnshuman Khandual 	pr_debug("Validating P4D basic\n");
476399145f9SAnshuman Khandual 	memset(&p4d, RANDOM_NZVALUE, sizeof(p4d_t));
477399145f9SAnshuman Khandual 	WARN_ON(!p4d_same(p4d, p4d));
478399145f9SAnshuman Khandual }
479399145f9SAnshuman Khandual 
pgd_basic_tests(struct pgtable_debug_args * args)48036b77d1eSGavin Shan static void __init pgd_basic_tests(struct pgtable_debug_args *args)
481399145f9SAnshuman Khandual {
482399145f9SAnshuman Khandual 	pgd_t pgd;
483399145f9SAnshuman Khandual 
4846315df41SAnshuman Khandual 	pr_debug("Validating PGD basic\n");
485399145f9SAnshuman Khandual 	memset(&pgd, RANDOM_NZVALUE, sizeof(pgd_t));
486399145f9SAnshuman Khandual 	WARN_ON(!pgd_same(pgd, pgd));
487399145f9SAnshuman Khandual }
488399145f9SAnshuman Khandual 
489399145f9SAnshuman Khandual #ifndef __PAGETABLE_PUD_FOLDED
pud_clear_tests(struct pgtable_debug_args * args)4904cbde03bSGavin Shan static void __init pud_clear_tests(struct pgtable_debug_args *args)
491399145f9SAnshuman Khandual {
4924cbde03bSGavin Shan 	pud_t pud = READ_ONCE(*args->pudp);
493399145f9SAnshuman Khandual 
4944cbde03bSGavin Shan 	if (mm_pmd_folded(args->mm))
495399145f9SAnshuman Khandual 		return;
496399145f9SAnshuman Khandual 
4976315df41SAnshuman Khandual 	pr_debug("Validating PUD clear\n");
498*6419341bSPeter Xu 	WARN_ON(pud_none(pud));
4994cbde03bSGavin Shan 	pud_clear(args->pudp);
5004cbde03bSGavin Shan 	pud = READ_ONCE(*args->pudp);
501399145f9SAnshuman Khandual 	WARN_ON(!pud_none(pud));
502399145f9SAnshuman Khandual }
503399145f9SAnshuman Khandual 
pud_populate_tests(struct pgtable_debug_args * args)5044cbde03bSGavin Shan static void __init pud_populate_tests(struct pgtable_debug_args *args)
505399145f9SAnshuman Khandual {
506399145f9SAnshuman Khandual 	pud_t pud;
507399145f9SAnshuman Khandual 
5084cbde03bSGavin Shan 	if (mm_pmd_folded(args->mm))
509399145f9SAnshuman Khandual 		return;
5106315df41SAnshuman Khandual 
5116315df41SAnshuman Khandual 	pr_debug("Validating PUD populate\n");
512399145f9SAnshuman Khandual 	/*
513399145f9SAnshuman Khandual 	 * This entry points to next level page table page.
514399145f9SAnshuman Khandual 	 * Hence this must not qualify as pud_bad().
515399145f9SAnshuman Khandual 	 */
5164cbde03bSGavin Shan 	pud_populate(args->mm, args->pudp, args->start_pmdp);
5174cbde03bSGavin Shan 	pud = READ_ONCE(*args->pudp);
518399145f9SAnshuman Khandual 	WARN_ON(pud_bad(pud));
519399145f9SAnshuman Khandual }
520399145f9SAnshuman Khandual #else  /* !__PAGETABLE_PUD_FOLDED */
pud_clear_tests(struct pgtable_debug_args * args)5214cbde03bSGavin Shan static void __init pud_clear_tests(struct pgtable_debug_args *args) { }
pud_populate_tests(struct pgtable_debug_args * args)5224cbde03bSGavin Shan static void __init pud_populate_tests(struct pgtable_debug_args *args) { }
523399145f9SAnshuman Khandual #endif /* PAGETABLE_PUD_FOLDED */
524399145f9SAnshuman Khandual 
525399145f9SAnshuman Khandual #ifndef __PAGETABLE_P4D_FOLDED
p4d_clear_tests(struct pgtable_debug_args * args)5262f87f8c3SGavin Shan static void __init p4d_clear_tests(struct pgtable_debug_args *args)
527399145f9SAnshuman Khandual {
5282f87f8c3SGavin Shan 	p4d_t p4d = READ_ONCE(*args->p4dp);
529399145f9SAnshuman Khandual 
5302f87f8c3SGavin Shan 	if (mm_pud_folded(args->mm))
531399145f9SAnshuman Khandual 		return;
532399145f9SAnshuman Khandual 
5336315df41SAnshuman Khandual 	pr_debug("Validating P4D clear\n");
534*6419341bSPeter Xu 	WARN_ON(p4d_none(p4d));
5352f87f8c3SGavin Shan 	p4d_clear(args->p4dp);
5362f87f8c3SGavin Shan 	p4d = READ_ONCE(*args->p4dp);
537399145f9SAnshuman Khandual 	WARN_ON(!p4d_none(p4d));
538399145f9SAnshuman Khandual }
539399145f9SAnshuman Khandual 
p4d_populate_tests(struct pgtable_debug_args * args)5402f87f8c3SGavin Shan static void __init p4d_populate_tests(struct pgtable_debug_args *args)
541399145f9SAnshuman Khandual {
542399145f9SAnshuman Khandual 	p4d_t p4d;
543399145f9SAnshuman Khandual 
5442f87f8c3SGavin Shan 	if (mm_pud_folded(args->mm))
545399145f9SAnshuman Khandual 		return;
546399145f9SAnshuman Khandual 
5476315df41SAnshuman Khandual 	pr_debug("Validating P4D populate\n");
548399145f9SAnshuman Khandual 	/*
549399145f9SAnshuman Khandual 	 * This entry points to next level page table page.
550399145f9SAnshuman Khandual 	 * Hence this must not qualify as p4d_bad().
551399145f9SAnshuman Khandual 	 */
5522f87f8c3SGavin Shan 	pud_clear(args->pudp);
5532f87f8c3SGavin Shan 	p4d_clear(args->p4dp);
5542f87f8c3SGavin Shan 	p4d_populate(args->mm, args->p4dp, args->start_pudp);
5552f87f8c3SGavin Shan 	p4d = READ_ONCE(*args->p4dp);
556399145f9SAnshuman Khandual 	WARN_ON(p4d_bad(p4d));
557399145f9SAnshuman Khandual }
558399145f9SAnshuman Khandual 
pgd_clear_tests(struct pgtable_debug_args * args)5592f87f8c3SGavin Shan static void __init pgd_clear_tests(struct pgtable_debug_args *args)
560399145f9SAnshuman Khandual {
5612f87f8c3SGavin Shan 	pgd_t pgd = READ_ONCE(*(args->pgdp));
562399145f9SAnshuman Khandual 
5632f87f8c3SGavin Shan 	if (mm_p4d_folded(args->mm))
564399145f9SAnshuman Khandual 		return;
565399145f9SAnshuman Khandual 
5666315df41SAnshuman Khandual 	pr_debug("Validating PGD clear\n");
567*6419341bSPeter Xu 	WARN_ON(pgd_none(pgd));
5682f87f8c3SGavin Shan 	pgd_clear(args->pgdp);
5692f87f8c3SGavin Shan 	pgd = READ_ONCE(*args->pgdp);
570399145f9SAnshuman Khandual 	WARN_ON(!pgd_none(pgd));
571399145f9SAnshuman Khandual }
572399145f9SAnshuman Khandual 
pgd_populate_tests(struct pgtable_debug_args * args)5732f87f8c3SGavin Shan static void __init pgd_populate_tests(struct pgtable_debug_args *args)
574399145f9SAnshuman Khandual {
575399145f9SAnshuman Khandual 	pgd_t pgd;
576399145f9SAnshuman Khandual 
5772f87f8c3SGavin Shan 	if (mm_p4d_folded(args->mm))
578399145f9SAnshuman Khandual 		return;
579399145f9SAnshuman Khandual 
5806315df41SAnshuman Khandual 	pr_debug("Validating PGD populate\n");
581399145f9SAnshuman Khandual 	/*
582399145f9SAnshuman Khandual 	 * This entry points to next level page table page.
583399145f9SAnshuman Khandual 	 * Hence this must not qualify as pgd_bad().
584399145f9SAnshuman Khandual 	 */
5852f87f8c3SGavin Shan 	p4d_clear(args->p4dp);
5862f87f8c3SGavin Shan 	pgd_clear(args->pgdp);
5872f87f8c3SGavin Shan 	pgd_populate(args->mm, args->pgdp, args->start_p4dp);
5882f87f8c3SGavin Shan 	pgd = READ_ONCE(*args->pgdp);
589399145f9SAnshuman Khandual 	WARN_ON(pgd_bad(pgd));
590399145f9SAnshuman Khandual }
591399145f9SAnshuman Khandual #else  /* !__PAGETABLE_P4D_FOLDED */
p4d_clear_tests(struct pgtable_debug_args * args)5922f87f8c3SGavin Shan static void __init p4d_clear_tests(struct pgtable_debug_args *args) { }
pgd_clear_tests(struct pgtable_debug_args * args)5932f87f8c3SGavin Shan static void __init pgd_clear_tests(struct pgtable_debug_args *args) { }
p4d_populate_tests(struct pgtable_debug_args * args)5942f87f8c3SGavin Shan static void __init p4d_populate_tests(struct pgtable_debug_args *args) { }
pgd_populate_tests(struct pgtable_debug_args * args)5952f87f8c3SGavin Shan static void __init pgd_populate_tests(struct pgtable_debug_args *args) { }
596399145f9SAnshuman Khandual #endif /* PAGETABLE_P4D_FOLDED */
597399145f9SAnshuman Khandual 
pte_clear_tests(struct pgtable_debug_args * args)59844966c44SGavin Shan static void __init pte_clear_tests(struct pgtable_debug_args *args)
599399145f9SAnshuman Khandual {
6008c5b3a8aSGavin Shan 	struct page *page;
60144966c44SGavin Shan 	pte_t pte = pfn_pte(args->pte_pfn, args->page_prot);
60244966c44SGavin Shan 
6038c5b3a8aSGavin Shan 	page = (args->pte_pfn != ULONG_MAX) ? pfn_to_page(args->pte_pfn) : NULL;
6048c5b3a8aSGavin Shan 	if (!page)
60544966c44SGavin Shan 		return;
606399145f9SAnshuman Khandual 
6078c5b3a8aSGavin Shan 	/*
6088c5b3a8aSGavin Shan 	 * flush_dcache_page() is called after set_pte_at() to clear
6098c5b3a8aSGavin Shan 	 * PG_arch_1 for the page on ARM64. The page flag isn't cleared
6108c5b3a8aSGavin Shan 	 * when it's released and page allocation check will fail when
6118c5b3a8aSGavin Shan 	 * the page is allocated again. For architectures other than ARM64,
6128c5b3a8aSGavin Shan 	 * the unexpected overhead of cache flushing is acceptable.
6138c5b3a8aSGavin Shan 	 */
6146315df41SAnshuman Khandual 	pr_debug("Validating PTE clear\n");
6159f2bad09SHugh Dickins 	if (WARN_ON(!args->ptep))
6169f2bad09SHugh Dickins 		return;
6179f2bad09SHugh Dickins 
61844966c44SGavin Shan 	set_pte_at(args->mm, args->vaddr, args->ptep, pte);
619*6419341bSPeter Xu 	WARN_ON(pte_none(pte));
6208c5b3a8aSGavin Shan 	flush_dcache_page(page);
621399145f9SAnshuman Khandual 	barrier();
62208d5b29eSPasha Tatashin 	ptep_clear(args->mm, args->vaddr, args->ptep);
62344966c44SGavin Shan 	pte = ptep_get(args->ptep);
624399145f9SAnshuman Khandual 	WARN_ON(!pte_none(pte));
625399145f9SAnshuman Khandual }
626399145f9SAnshuman Khandual 
pmd_clear_tests(struct pgtable_debug_args * args)627c0fe07b0SGavin Shan static void __init pmd_clear_tests(struct pgtable_debug_args *args)
628399145f9SAnshuman Khandual {
629c0fe07b0SGavin Shan 	pmd_t pmd = READ_ONCE(*args->pmdp);
630399145f9SAnshuman Khandual 
6316315df41SAnshuman Khandual 	pr_debug("Validating PMD clear\n");
632*6419341bSPeter Xu 	WARN_ON(pmd_none(pmd));
633c0fe07b0SGavin Shan 	pmd_clear(args->pmdp);
634c0fe07b0SGavin Shan 	pmd = READ_ONCE(*args->pmdp);
635399145f9SAnshuman Khandual 	WARN_ON(!pmd_none(pmd));
636399145f9SAnshuman Khandual }
637399145f9SAnshuman Khandual 
pmd_populate_tests(struct pgtable_debug_args * args)638c0fe07b0SGavin Shan static void __init pmd_populate_tests(struct pgtable_debug_args *args)
639399145f9SAnshuman Khandual {
640399145f9SAnshuman Khandual 	pmd_t pmd;
641399145f9SAnshuman Khandual 
6426315df41SAnshuman Khandual 	pr_debug("Validating PMD populate\n");
643399145f9SAnshuman Khandual 	/*
644399145f9SAnshuman Khandual 	 * This entry points to next level page table page.
645399145f9SAnshuman Khandual 	 * Hence this must not qualify as pmd_bad().
646399145f9SAnshuman Khandual 	 */
647c0fe07b0SGavin Shan 	pmd_populate(args->mm, args->pmdp, args->start_ptep);
648c0fe07b0SGavin Shan 	pmd = READ_ONCE(*args->pmdp);
649399145f9SAnshuman Khandual 	WARN_ON(pmd_bad(pmd));
650399145f9SAnshuman Khandual }
651399145f9SAnshuman Khandual 
pte_special_tests(struct pgtable_debug_args * args)6528cb183f2SGavin Shan static void __init pte_special_tests(struct pgtable_debug_args *args)
65305289402SAnshuman Khandual {
6548cb183f2SGavin Shan 	pte_t pte = pfn_pte(args->fixed_pte_pfn, args->page_prot);
65505289402SAnshuman Khandual 
65605289402SAnshuman Khandual 	if (!IS_ENABLED(CONFIG_ARCH_HAS_PTE_SPECIAL))
65705289402SAnshuman Khandual 		return;
65805289402SAnshuman Khandual 
6596315df41SAnshuman Khandual 	pr_debug("Validating PTE special\n");
66005289402SAnshuman Khandual 	WARN_ON(!pte_special(pte_mkspecial(pte)));
66105289402SAnshuman Khandual }
66205289402SAnshuman Khandual 
pte_protnone_tests(struct pgtable_debug_args * args)6638cb183f2SGavin Shan static void __init pte_protnone_tests(struct pgtable_debug_args *args)
66405289402SAnshuman Khandual {
6658cb183f2SGavin Shan 	pte_t pte = pfn_pte(args->fixed_pte_pfn, args->page_prot_none);
66605289402SAnshuman Khandual 
66705289402SAnshuman Khandual 	if (!IS_ENABLED(CONFIG_NUMA_BALANCING))
66805289402SAnshuman Khandual 		return;
66905289402SAnshuman Khandual 
6706315df41SAnshuman Khandual 	pr_debug("Validating PTE protnone\n");
67105289402SAnshuman Khandual 	WARN_ON(!pte_protnone(pte));
67205289402SAnshuman Khandual 	WARN_ON(!pte_present(pte));
67305289402SAnshuman Khandual }
67405289402SAnshuman Khandual 
67505289402SAnshuman Khandual #ifdef CONFIG_TRANSPARENT_HUGEPAGE
pmd_protnone_tests(struct pgtable_debug_args * args)6768cb183f2SGavin Shan static void __init pmd_protnone_tests(struct pgtable_debug_args *args)
67705289402SAnshuman Khandual {
67865ac1a60SAnshuman Khandual 	pmd_t pmd;
67905289402SAnshuman Khandual 
68005289402SAnshuman Khandual 	if (!IS_ENABLED(CONFIG_NUMA_BALANCING))
68105289402SAnshuman Khandual 		return;
68205289402SAnshuman Khandual 
68365ac1a60SAnshuman Khandual 	if (!has_transparent_hugepage())
68465ac1a60SAnshuman Khandual 		return;
68565ac1a60SAnshuman Khandual 
6866315df41SAnshuman Khandual 	pr_debug("Validating PMD protnone\n");
6878cb183f2SGavin Shan 	pmd = pmd_mkhuge(pfn_pmd(args->fixed_pmd_pfn, args->page_prot_none));
68805289402SAnshuman Khandual 	WARN_ON(!pmd_protnone(pmd));
68905289402SAnshuman Khandual 	WARN_ON(!pmd_present(pmd));
69005289402SAnshuman Khandual }
69105289402SAnshuman Khandual #else  /* !CONFIG_TRANSPARENT_HUGEPAGE */
pmd_protnone_tests(struct pgtable_debug_args * args)6928cb183f2SGavin Shan static void __init pmd_protnone_tests(struct pgtable_debug_args *args) { }
69305289402SAnshuman Khandual #endif /* CONFIG_TRANSPARENT_HUGEPAGE */
69405289402SAnshuman Khandual 
69505289402SAnshuman Khandual #ifdef CONFIG_ARCH_HAS_PTE_DEVMAP
pte_devmap_tests(struct pgtable_debug_args * args)6968cb183f2SGavin Shan static void __init pte_devmap_tests(struct pgtable_debug_args *args)
69705289402SAnshuman Khandual {
6988cb183f2SGavin Shan 	pte_t pte = pfn_pte(args->fixed_pte_pfn, args->page_prot);
69905289402SAnshuman Khandual 
7006315df41SAnshuman Khandual 	pr_debug("Validating PTE devmap\n");
70105289402SAnshuman Khandual 	WARN_ON(!pte_devmap(pte_mkdevmap(pte)));
70205289402SAnshuman Khandual }
70305289402SAnshuman Khandual 
70405289402SAnshuman Khandual #ifdef CONFIG_TRANSPARENT_HUGEPAGE
pmd_devmap_tests(struct pgtable_debug_args * args)7058cb183f2SGavin Shan static void __init pmd_devmap_tests(struct pgtable_debug_args *args)
70605289402SAnshuman Khandual {
70765ac1a60SAnshuman Khandual 	pmd_t pmd;
70865ac1a60SAnshuman Khandual 
70965ac1a60SAnshuman Khandual 	if (!has_transparent_hugepage())
71065ac1a60SAnshuman Khandual 		return;
71105289402SAnshuman Khandual 
7126315df41SAnshuman Khandual 	pr_debug("Validating PMD devmap\n");
7138cb183f2SGavin Shan 	pmd = pfn_pmd(args->fixed_pmd_pfn, args->page_prot);
71405289402SAnshuman Khandual 	WARN_ON(!pmd_devmap(pmd_mkdevmap(pmd)));
71505289402SAnshuman Khandual }
71605289402SAnshuman Khandual 
71705289402SAnshuman Khandual #ifdef CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD
pud_devmap_tests(struct pgtable_debug_args * args)7188cb183f2SGavin Shan static void __init pud_devmap_tests(struct pgtable_debug_args *args)
71905289402SAnshuman Khandual {
72065ac1a60SAnshuman Khandual 	pud_t pud;
72165ac1a60SAnshuman Khandual 
722348ad160SAneesh Kumar K.V 	if (!has_transparent_pud_hugepage())
72365ac1a60SAnshuman Khandual 		return;
72405289402SAnshuman Khandual 
7256315df41SAnshuman Khandual 	pr_debug("Validating PUD devmap\n");
7268cb183f2SGavin Shan 	pud = pfn_pud(args->fixed_pud_pfn, args->page_prot);
72705289402SAnshuman Khandual 	WARN_ON(!pud_devmap(pud_mkdevmap(pud)));
72805289402SAnshuman Khandual }
72905289402SAnshuman Khandual #else  /* !CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD */
pud_devmap_tests(struct pgtable_debug_args * args)7308cb183f2SGavin Shan static void __init pud_devmap_tests(struct pgtable_debug_args *args) { }
73105289402SAnshuman Khandual #endif /* CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD */
73205289402SAnshuman Khandual #else  /* CONFIG_TRANSPARENT_HUGEPAGE */
pmd_devmap_tests(struct pgtable_debug_args * args)7338cb183f2SGavin Shan static void __init pmd_devmap_tests(struct pgtable_debug_args *args) { }
pud_devmap_tests(struct pgtable_debug_args * args)7348cb183f2SGavin Shan static void __init pud_devmap_tests(struct pgtable_debug_args *args) { }
73505289402SAnshuman Khandual #endif /* CONFIG_TRANSPARENT_HUGEPAGE */
73605289402SAnshuman Khandual #else
pte_devmap_tests(struct pgtable_debug_args * args)7378cb183f2SGavin Shan static void __init pte_devmap_tests(struct pgtable_debug_args *args) { }
pmd_devmap_tests(struct pgtable_debug_args * args)7388cb183f2SGavin Shan static void __init pmd_devmap_tests(struct pgtable_debug_args *args) { }
pud_devmap_tests(struct pgtable_debug_args * args)7398cb183f2SGavin Shan static void __init pud_devmap_tests(struct pgtable_debug_args *args) { }
74005289402SAnshuman Khandual #endif /* CONFIG_ARCH_HAS_PTE_DEVMAP */
74105289402SAnshuman Khandual 
pte_soft_dirty_tests(struct pgtable_debug_args * args)7425f447e80SGavin Shan static void __init pte_soft_dirty_tests(struct pgtable_debug_args *args)
74305289402SAnshuman Khandual {
7445f447e80SGavin Shan 	pte_t pte = pfn_pte(args->fixed_pte_pfn, args->page_prot);
74505289402SAnshuman Khandual 
74605289402SAnshuman Khandual 	if (!IS_ENABLED(CONFIG_MEM_SOFT_DIRTY))
74705289402SAnshuman Khandual 		return;
74805289402SAnshuman Khandual 
7496315df41SAnshuman Khandual 	pr_debug("Validating PTE soft dirty\n");
75005289402SAnshuman Khandual 	WARN_ON(!pte_soft_dirty(pte_mksoft_dirty(pte)));
75105289402SAnshuman Khandual 	WARN_ON(pte_soft_dirty(pte_clear_soft_dirty(pte)));
75205289402SAnshuman Khandual }
75305289402SAnshuman Khandual 
pte_swap_soft_dirty_tests(struct pgtable_debug_args * args)7545f447e80SGavin Shan static void __init pte_swap_soft_dirty_tests(struct pgtable_debug_args *args)
75505289402SAnshuman Khandual {
7565f447e80SGavin Shan 	pte_t pte = pfn_pte(args->fixed_pte_pfn, args->page_prot);
75705289402SAnshuman Khandual 
75805289402SAnshuman Khandual 	if (!IS_ENABLED(CONFIG_MEM_SOFT_DIRTY))
75905289402SAnshuman Khandual 		return;
76005289402SAnshuman Khandual 
7616315df41SAnshuman Khandual 	pr_debug("Validating PTE swap soft dirty\n");
76205289402SAnshuman Khandual 	WARN_ON(!pte_swp_soft_dirty(pte_swp_mksoft_dirty(pte)));
76305289402SAnshuman Khandual 	WARN_ON(pte_swp_soft_dirty(pte_swp_clear_soft_dirty(pte)));
76405289402SAnshuman Khandual }
76505289402SAnshuman Khandual 
76605289402SAnshuman Khandual #ifdef CONFIG_TRANSPARENT_HUGEPAGE
pmd_soft_dirty_tests(struct pgtable_debug_args * args)7675f447e80SGavin Shan static void __init pmd_soft_dirty_tests(struct pgtable_debug_args *args)
76805289402SAnshuman Khandual {
76965ac1a60SAnshuman Khandual 	pmd_t pmd;
77005289402SAnshuman Khandual 
77105289402SAnshuman Khandual 	if (!IS_ENABLED(CONFIG_MEM_SOFT_DIRTY))
77205289402SAnshuman Khandual 		return;
77305289402SAnshuman Khandual 
77465ac1a60SAnshuman Khandual 	if (!has_transparent_hugepage())
77565ac1a60SAnshuman Khandual 		return;
77665ac1a60SAnshuman Khandual 
7776315df41SAnshuman Khandual 	pr_debug("Validating PMD soft dirty\n");
7785f447e80SGavin Shan 	pmd = pfn_pmd(args->fixed_pmd_pfn, args->page_prot);
77905289402SAnshuman Khandual 	WARN_ON(!pmd_soft_dirty(pmd_mksoft_dirty(pmd)));
78005289402SAnshuman Khandual 	WARN_ON(pmd_soft_dirty(pmd_clear_soft_dirty(pmd)));
78105289402SAnshuman Khandual }
78205289402SAnshuman Khandual 
pmd_swap_soft_dirty_tests(struct pgtable_debug_args * args)7835f447e80SGavin Shan static void __init pmd_swap_soft_dirty_tests(struct pgtable_debug_args *args)
78405289402SAnshuman Khandual {
78565ac1a60SAnshuman Khandual 	pmd_t pmd;
78605289402SAnshuman Khandual 
78705289402SAnshuman Khandual 	if (!IS_ENABLED(CONFIG_MEM_SOFT_DIRTY) ||
78805289402SAnshuman Khandual 		!IS_ENABLED(CONFIG_ARCH_ENABLE_THP_MIGRATION))
78905289402SAnshuman Khandual 		return;
79005289402SAnshuman Khandual 
79165ac1a60SAnshuman Khandual 	if (!has_transparent_hugepage())
79265ac1a60SAnshuman Khandual 		return;
79365ac1a60SAnshuman Khandual 
7946315df41SAnshuman Khandual 	pr_debug("Validating PMD swap soft dirty\n");
7955f447e80SGavin Shan 	pmd = pfn_pmd(args->fixed_pmd_pfn, args->page_prot);
79605289402SAnshuman Khandual 	WARN_ON(!pmd_swp_soft_dirty(pmd_swp_mksoft_dirty(pmd)));
79705289402SAnshuman Khandual 	WARN_ON(pmd_swp_soft_dirty(pmd_swp_clear_soft_dirty(pmd)));
79805289402SAnshuman Khandual }
799b593b90dSShixin Liu #else  /* !CONFIG_TRANSPARENT_HUGEPAGE */
pmd_soft_dirty_tests(struct pgtable_debug_args * args)8005f447e80SGavin Shan static void __init pmd_soft_dirty_tests(struct pgtable_debug_args *args) { }
pmd_swap_soft_dirty_tests(struct pgtable_debug_args * args)8015f447e80SGavin Shan static void __init pmd_swap_soft_dirty_tests(struct pgtable_debug_args *args) { }
802b593b90dSShixin Liu #endif /* CONFIG_TRANSPARENT_HUGEPAGE */
80305289402SAnshuman Khandual 
pte_swap_exclusive_tests(struct pgtable_debug_args * args)804210d1e8aSDavid Hildenbrand static void __init pte_swap_exclusive_tests(struct pgtable_debug_args *args)
805210d1e8aSDavid Hildenbrand {
8062321ba3eSDavid Hildenbrand 	unsigned long max_swap_offset;
8072321ba3eSDavid Hildenbrand 	swp_entry_t entry, entry2;
8082321ba3eSDavid Hildenbrand 	pte_t pte;
809210d1e8aSDavid Hildenbrand 
810210d1e8aSDavid Hildenbrand 	pr_debug("Validating PTE swap exclusive\n");
8112321ba3eSDavid Hildenbrand 
8122321ba3eSDavid Hildenbrand 	/* See generic_max_swapfile_size(): probe the maximum offset */
8132321ba3eSDavid Hildenbrand 	max_swap_offset = swp_offset(pte_to_swp_entry(swp_entry_to_pte(swp_entry(0, ~0UL))));
8142321ba3eSDavid Hildenbrand 
8152321ba3eSDavid Hildenbrand 	/* Create a swp entry with all possible bits set */
8162321ba3eSDavid Hildenbrand 	entry = swp_entry((1 << MAX_SWAPFILES_SHIFT) - 1, max_swap_offset);
8172321ba3eSDavid Hildenbrand 
8182321ba3eSDavid Hildenbrand 	pte = swp_entry_to_pte(entry);
8192321ba3eSDavid Hildenbrand 	WARN_ON(pte_swp_exclusive(pte));
8202321ba3eSDavid Hildenbrand 	WARN_ON(!is_swap_pte(pte));
8212321ba3eSDavid Hildenbrand 	entry2 = pte_to_swp_entry(pte);
8222321ba3eSDavid Hildenbrand 	WARN_ON(memcmp(&entry, &entry2, sizeof(entry)));
8232321ba3eSDavid Hildenbrand 
824210d1e8aSDavid Hildenbrand 	pte = pte_swp_mkexclusive(pte);
825210d1e8aSDavid Hildenbrand 	WARN_ON(!pte_swp_exclusive(pte));
8262321ba3eSDavid Hildenbrand 	WARN_ON(!is_swap_pte(pte));
8272321ba3eSDavid Hildenbrand 	WARN_ON(pte_swp_soft_dirty(pte));
8282321ba3eSDavid Hildenbrand 	entry2 = pte_to_swp_entry(pte);
8292321ba3eSDavid Hildenbrand 	WARN_ON(memcmp(&entry, &entry2, sizeof(entry)));
8302321ba3eSDavid Hildenbrand 
831210d1e8aSDavid Hildenbrand 	pte = pte_swp_clear_exclusive(pte);
832210d1e8aSDavid Hildenbrand 	WARN_ON(pte_swp_exclusive(pte));
8332321ba3eSDavid Hildenbrand 	WARN_ON(!is_swap_pte(pte));
8342321ba3eSDavid Hildenbrand 	entry2 = pte_to_swp_entry(pte);
8352321ba3eSDavid Hildenbrand 	WARN_ON(memcmp(&entry, &entry2, sizeof(entry)));
836210d1e8aSDavid Hildenbrand }
837210d1e8aSDavid Hildenbrand 
pte_swap_tests(struct pgtable_debug_args * args)8385f447e80SGavin Shan static void __init pte_swap_tests(struct pgtable_debug_args *args)
83905289402SAnshuman Khandual {
84005289402SAnshuman Khandual 	swp_entry_t swp;
84105289402SAnshuman Khandual 	pte_t pte;
84205289402SAnshuman Khandual 
8436315df41SAnshuman Khandual 	pr_debug("Validating PTE swap\n");
8445f447e80SGavin Shan 	pte = pfn_pte(args->fixed_pte_pfn, args->page_prot);
84505289402SAnshuman Khandual 	swp = __pte_to_swp_entry(pte);
84605289402SAnshuman Khandual 	pte = __swp_entry_to_pte(swp);
8475f447e80SGavin Shan 	WARN_ON(args->fixed_pte_pfn != pte_pfn(pte));
84805289402SAnshuman Khandual }
84905289402SAnshuman Khandual 
85005289402SAnshuman Khandual #ifdef CONFIG_ARCH_ENABLE_THP_MIGRATION
pmd_swap_tests(struct pgtable_debug_args * args)8515f447e80SGavin Shan static void __init pmd_swap_tests(struct pgtable_debug_args *args)
85205289402SAnshuman Khandual {
85305289402SAnshuman Khandual 	swp_entry_t swp;
85405289402SAnshuman Khandual 	pmd_t pmd;
85505289402SAnshuman Khandual 
85665ac1a60SAnshuman Khandual 	if (!has_transparent_hugepage())
85765ac1a60SAnshuman Khandual 		return;
85865ac1a60SAnshuman Khandual 
8596315df41SAnshuman Khandual 	pr_debug("Validating PMD swap\n");
8605f447e80SGavin Shan 	pmd = pfn_pmd(args->fixed_pmd_pfn, args->page_prot);
86105289402SAnshuman Khandual 	swp = __pmd_to_swp_entry(pmd);
86205289402SAnshuman Khandual 	pmd = __swp_entry_to_pmd(swp);
8635f447e80SGavin Shan 	WARN_ON(args->fixed_pmd_pfn != pmd_pfn(pmd));
86405289402SAnshuman Khandual }
86505289402SAnshuman Khandual #else  /* !CONFIG_ARCH_ENABLE_THP_MIGRATION */
pmd_swap_tests(struct pgtable_debug_args * args)8665f447e80SGavin Shan static void __init pmd_swap_tests(struct pgtable_debug_args *args) { }
86705289402SAnshuman Khandual #endif /* CONFIG_ARCH_ENABLE_THP_MIGRATION */
86805289402SAnshuman Khandual 
swap_migration_tests(struct pgtable_debug_args * args)8694878a888SGavin Shan static void __init swap_migration_tests(struct pgtable_debug_args *args)
87005289402SAnshuman Khandual {
87105289402SAnshuman Khandual 	struct page *page;
87205289402SAnshuman Khandual 	swp_entry_t swp;
87305289402SAnshuman Khandual 
87405289402SAnshuman Khandual 	if (!IS_ENABLED(CONFIG_MIGRATION))
87505289402SAnshuman Khandual 		return;
8766315df41SAnshuman Khandual 
87705289402SAnshuman Khandual 	/*
87805289402SAnshuman Khandual 	 * swap_migration_tests() requires a dedicated page as it needs to
87905289402SAnshuman Khandual 	 * be locked before creating a migration entry from it. Locking the
88005289402SAnshuman Khandual 	 * page that actually maps kernel text ('start_kernel') can be real
8814878a888SGavin Shan 	 * problematic. Lets use the allocated page explicitly for this
8824878a888SGavin Shan 	 * purpose.
88305289402SAnshuman Khandual 	 */
8844878a888SGavin Shan 	page = (args->pte_pfn != ULONG_MAX) ? pfn_to_page(args->pte_pfn) : NULL;
8854878a888SGavin Shan 	if (!page)
88605289402SAnshuman Khandual 		return;
8874878a888SGavin Shan 
8884878a888SGavin Shan 	pr_debug("Validating swap migration\n");
88905289402SAnshuman Khandual 
89005289402SAnshuman Khandual 	/*
89123647618SAnshuman Khandual 	 * make_[readable|writable]_migration_entry() expects given page to
89223647618SAnshuman Khandual 	 * be locked, otherwise it stumbles upon a BUG_ON().
89305289402SAnshuman Khandual 	 */
89405289402SAnshuman Khandual 	__SetPageLocked(page);
8954dd845b5SAlistair Popple 	swp = make_writable_migration_entry(page_to_pfn(page));
89605289402SAnshuman Khandual 	WARN_ON(!is_migration_entry(swp));
8974dd845b5SAlistair Popple 	WARN_ON(!is_writable_migration_entry(swp));
89805289402SAnshuman Khandual 
8994dd845b5SAlistair Popple 	swp = make_readable_migration_entry(swp_offset(swp));
90005289402SAnshuman Khandual 	WARN_ON(!is_migration_entry(swp));
9014dd845b5SAlistair Popple 	WARN_ON(is_writable_migration_entry(swp));
90205289402SAnshuman Khandual 
9034dd845b5SAlistair Popple 	swp = make_readable_migration_entry(page_to_pfn(page));
90405289402SAnshuman Khandual 	WARN_ON(!is_migration_entry(swp));
9054dd845b5SAlistair Popple 	WARN_ON(is_writable_migration_entry(swp));
90605289402SAnshuman Khandual 	__ClearPageLocked(page);
90705289402SAnshuman Khandual }
90805289402SAnshuman Khandual 
90905289402SAnshuman Khandual #ifdef CONFIG_HUGETLB_PAGE
hugetlb_basic_tests(struct pgtable_debug_args * args)91036b77d1eSGavin Shan static void __init hugetlb_basic_tests(struct pgtable_debug_args *args)
91105289402SAnshuman Khandual {
91205289402SAnshuman Khandual 	struct page *page;
91305289402SAnshuman Khandual 	pte_t pte;
91405289402SAnshuman Khandual 
9156315df41SAnshuman Khandual 	pr_debug("Validating HugeTLB basic\n");
91605289402SAnshuman Khandual 	/*
91705289402SAnshuman Khandual 	 * Accessing the page associated with the pfn is safe here,
91805289402SAnshuman Khandual 	 * as it was previously derived from a real kernel symbol.
91905289402SAnshuman Khandual 	 */
92036b77d1eSGavin Shan 	page = pfn_to_page(args->fixed_pmd_pfn);
92136b77d1eSGavin Shan 	pte = mk_huge_pte(page, args->page_prot);
92205289402SAnshuman Khandual 
92305289402SAnshuman Khandual 	WARN_ON(!huge_pte_dirty(huge_pte_mkdirty(pte)));
92405289402SAnshuman Khandual 	WARN_ON(!huge_pte_write(huge_pte_mkwrite(huge_pte_wrprotect(pte))));
92505289402SAnshuman Khandual 	WARN_ON(huge_pte_write(huge_pte_wrprotect(huge_pte_mkwrite(pte))));
92605289402SAnshuman Khandual 
92705289402SAnshuman Khandual #ifdef CONFIG_ARCH_WANT_GENERAL_HUGETLB
92836b77d1eSGavin Shan 	pte = pfn_pte(args->fixed_pmd_pfn, args->page_prot);
92905289402SAnshuman Khandual 
9309dabf6e1SAnshuman Khandual 	WARN_ON(!pte_huge(arch_make_huge_pte(pte, PMD_SHIFT, VM_ACCESS_FLAGS)));
93105289402SAnshuman Khandual #endif /* CONFIG_ARCH_WANT_GENERAL_HUGETLB */
93205289402SAnshuman Khandual }
93305289402SAnshuman Khandual #else  /* !CONFIG_HUGETLB_PAGE */
hugetlb_basic_tests(struct pgtable_debug_args * args)93436b77d1eSGavin Shan static void __init hugetlb_basic_tests(struct pgtable_debug_args *args) { }
93505289402SAnshuman Khandual #endif /* CONFIG_HUGETLB_PAGE */
93605289402SAnshuman Khandual 
93705289402SAnshuman Khandual #ifdef CONFIG_TRANSPARENT_HUGEPAGE
pmd_thp_tests(struct pgtable_debug_args * args)9384878a888SGavin Shan static void __init pmd_thp_tests(struct pgtable_debug_args *args)
93905289402SAnshuman Khandual {
94005289402SAnshuman Khandual 	pmd_t pmd;
94105289402SAnshuman Khandual 
94205289402SAnshuman Khandual 	if (!has_transparent_hugepage())
94305289402SAnshuman Khandual 		return;
94405289402SAnshuman Khandual 
9456315df41SAnshuman Khandual 	pr_debug("Validating PMD based THP\n");
94605289402SAnshuman Khandual 	/*
94705289402SAnshuman Khandual 	 * pmd_trans_huge() and pmd_present() must return positive after
94805289402SAnshuman Khandual 	 * MMU invalidation with pmd_mkinvalid(). This behavior is an
94905289402SAnshuman Khandual 	 * optimization for transparent huge page. pmd_trans_huge() must
95005289402SAnshuman Khandual 	 * be true if pmd_page() returns a valid THP to avoid taking the
95105289402SAnshuman Khandual 	 * pmd_lock when others walk over non transhuge pmds (i.e. there
95205289402SAnshuman Khandual 	 * are no THP allocated). Especially when splitting a THP and
95305289402SAnshuman Khandual 	 * removing the present bit from the pmd, pmd_trans_huge() still
95405289402SAnshuman Khandual 	 * needs to return true. pmd_present() should be true whenever
95505289402SAnshuman Khandual 	 * pmd_trans_huge() returns true.
95605289402SAnshuman Khandual 	 */
9574878a888SGavin Shan 	pmd = pfn_pmd(args->fixed_pmd_pfn, args->page_prot);
95805289402SAnshuman Khandual 	WARN_ON(!pmd_trans_huge(pmd_mkhuge(pmd)));
95905289402SAnshuman Khandual 
96005289402SAnshuman Khandual #ifndef __HAVE_ARCH_PMDP_INVALIDATE
96105289402SAnshuman Khandual 	WARN_ON(!pmd_trans_huge(pmd_mkinvalid(pmd_mkhuge(pmd))));
96205289402SAnshuman Khandual 	WARN_ON(!pmd_present(pmd_mkinvalid(pmd_mkhuge(pmd))));
96305289402SAnshuman Khandual #endif /* __HAVE_ARCH_PMDP_INVALIDATE */
96405289402SAnshuman Khandual }
96505289402SAnshuman Khandual 
96605289402SAnshuman Khandual #ifdef CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD
pud_thp_tests(struct pgtable_debug_args * args)9674878a888SGavin Shan static void __init pud_thp_tests(struct pgtable_debug_args *args)
96805289402SAnshuman Khandual {
96905289402SAnshuman Khandual 	pud_t pud;
97005289402SAnshuman Khandual 
971348ad160SAneesh Kumar K.V 	if (!has_transparent_pud_hugepage())
97205289402SAnshuman Khandual 		return;
97305289402SAnshuman Khandual 
9746315df41SAnshuman Khandual 	pr_debug("Validating PUD based THP\n");
9754878a888SGavin Shan 	pud = pfn_pud(args->fixed_pud_pfn, args->page_prot);
97605289402SAnshuman Khandual 	WARN_ON(!pud_trans_huge(pud_mkhuge(pud)));
97705289402SAnshuman Khandual 
97805289402SAnshuman Khandual 	/*
97905289402SAnshuman Khandual 	 * pud_mkinvalid() has been dropped for now. Enable back
98005289402SAnshuman Khandual 	 * these tests when it comes back with a modified pud_present().
98105289402SAnshuman Khandual 	 *
98205289402SAnshuman Khandual 	 * WARN_ON(!pud_trans_huge(pud_mkinvalid(pud_mkhuge(pud))));
98305289402SAnshuman Khandual 	 * WARN_ON(!pud_present(pud_mkinvalid(pud_mkhuge(pud))));
98405289402SAnshuman Khandual 	 */
98505289402SAnshuman Khandual }
98605289402SAnshuman Khandual #else  /* !CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD */
pud_thp_tests(struct pgtable_debug_args * args)9874878a888SGavin Shan static void __init pud_thp_tests(struct pgtable_debug_args *args) { }
98805289402SAnshuman Khandual #endif /* CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD */
98905289402SAnshuman Khandual #else  /* !CONFIG_TRANSPARENT_HUGEPAGE */
pmd_thp_tests(struct pgtable_debug_args * args)9904878a888SGavin Shan static void __init pmd_thp_tests(struct pgtable_debug_args *args) { }
pud_thp_tests(struct pgtable_debug_args * args)9914878a888SGavin Shan static void __init pud_thp_tests(struct pgtable_debug_args *args) { }
99205289402SAnshuman Khandual #endif /* CONFIG_TRANSPARENT_HUGEPAGE */
99305289402SAnshuman Khandual 
get_random_vaddr(void)994399145f9SAnshuman Khandual static unsigned long __init get_random_vaddr(void)
995399145f9SAnshuman Khandual {
996399145f9SAnshuman Khandual 	unsigned long random_vaddr, random_pages, total_user_pages;
997399145f9SAnshuman Khandual 
998399145f9SAnshuman Khandual 	total_user_pages = (TASK_SIZE - FIRST_USER_ADDRESS) / PAGE_SIZE;
999399145f9SAnshuman Khandual 
1000399145f9SAnshuman Khandual 	random_pages = get_random_long() % total_user_pages;
1001399145f9SAnshuman Khandual 	random_vaddr = FIRST_USER_ADDRESS + random_pages * PAGE_SIZE;
1002399145f9SAnshuman Khandual 
1003399145f9SAnshuman Khandual 	return random_vaddr;
1004399145f9SAnshuman Khandual }
1005399145f9SAnshuman Khandual 
destroy_args(struct pgtable_debug_args * args)10063c9b84f0SGavin Shan static void __init destroy_args(struct pgtable_debug_args *args)
10073c9b84f0SGavin Shan {
10083c9b84f0SGavin Shan 	struct page *page = NULL;
10093c9b84f0SGavin Shan 
10103c9b84f0SGavin Shan 	/* Free (huge) page */
10113c9b84f0SGavin Shan 	if (IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE) &&
1012348ad160SAneesh Kumar K.V 	    has_transparent_pud_hugepage() &&
10133c9b84f0SGavin Shan 	    args->pud_pfn != ULONG_MAX) {
10143c9b84f0SGavin Shan 		if (args->is_contiguous_page) {
10153c9b84f0SGavin Shan 			free_contig_range(args->pud_pfn,
10163c9b84f0SGavin Shan 					  (1 << (HPAGE_PUD_SHIFT - PAGE_SHIFT)));
10173c9b84f0SGavin Shan 		} else {
10183c9b84f0SGavin Shan 			page = pfn_to_page(args->pud_pfn);
10193c9b84f0SGavin Shan 			__free_pages(page, HPAGE_PUD_SHIFT - PAGE_SHIFT);
10203c9b84f0SGavin Shan 		}
10213c9b84f0SGavin Shan 
10223c9b84f0SGavin Shan 		args->pud_pfn = ULONG_MAX;
10233c9b84f0SGavin Shan 		args->pmd_pfn = ULONG_MAX;
10243c9b84f0SGavin Shan 		args->pte_pfn = ULONG_MAX;
10253c9b84f0SGavin Shan 	}
10263c9b84f0SGavin Shan 
10273c9b84f0SGavin Shan 	if (IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE) &&
10283c9b84f0SGavin Shan 	    has_transparent_hugepage() &&
10293c9b84f0SGavin Shan 	    args->pmd_pfn != ULONG_MAX) {
10303c9b84f0SGavin Shan 		if (args->is_contiguous_page) {
10313c9b84f0SGavin Shan 			free_contig_range(args->pmd_pfn, (1 << HPAGE_PMD_ORDER));
10323c9b84f0SGavin Shan 		} else {
10333c9b84f0SGavin Shan 			page = pfn_to_page(args->pmd_pfn);
10343c9b84f0SGavin Shan 			__free_pages(page, HPAGE_PMD_ORDER);
10353c9b84f0SGavin Shan 		}
10363c9b84f0SGavin Shan 
10373c9b84f0SGavin Shan 		args->pmd_pfn = ULONG_MAX;
10383c9b84f0SGavin Shan 		args->pte_pfn = ULONG_MAX;
10393c9b84f0SGavin Shan 	}
10403c9b84f0SGavin Shan 
10413c9b84f0SGavin Shan 	if (args->pte_pfn != ULONG_MAX) {
10423c9b84f0SGavin Shan 		page = pfn_to_page(args->pte_pfn);
1043dcc1be11SLorenzo Stoakes 		__free_page(page);
10443c9b84f0SGavin Shan 
10453c9b84f0SGavin Shan 		args->pte_pfn = ULONG_MAX;
10463c9b84f0SGavin Shan 	}
10473c9b84f0SGavin Shan 
10483c9b84f0SGavin Shan 	/* Free page table entries */
10493c9b84f0SGavin Shan 	if (args->start_ptep) {
10503c9b84f0SGavin Shan 		pte_free(args->mm, args->start_ptep);
10513c9b84f0SGavin Shan 		mm_dec_nr_ptes(args->mm);
10523c9b84f0SGavin Shan 	}
10533c9b84f0SGavin Shan 
10543c9b84f0SGavin Shan 	if (args->start_pmdp) {
10553c9b84f0SGavin Shan 		pmd_free(args->mm, args->start_pmdp);
10563c9b84f0SGavin Shan 		mm_dec_nr_pmds(args->mm);
10573c9b84f0SGavin Shan 	}
10583c9b84f0SGavin Shan 
10593c9b84f0SGavin Shan 	if (args->start_pudp) {
10603c9b84f0SGavin Shan 		pud_free(args->mm, args->start_pudp);
10613c9b84f0SGavin Shan 		mm_dec_nr_puds(args->mm);
10623c9b84f0SGavin Shan 	}
10633c9b84f0SGavin Shan 
10643c9b84f0SGavin Shan 	if (args->start_p4dp)
10653c9b84f0SGavin Shan 		p4d_free(args->mm, args->start_p4dp);
10663c9b84f0SGavin Shan 
10673c9b84f0SGavin Shan 	/* Free vma and mm struct */
10683c9b84f0SGavin Shan 	if (args->vma)
10693c9b84f0SGavin Shan 		vm_area_free(args->vma);
10703c9b84f0SGavin Shan 
10713c9b84f0SGavin Shan 	if (args->mm)
10723c9b84f0SGavin Shan 		mmdrop(args->mm);
10733c9b84f0SGavin Shan }
10743c9b84f0SGavin Shan 
10753c9b84f0SGavin Shan static struct page * __init
debug_vm_pgtable_alloc_huge_page(struct pgtable_debug_args * args,int order)10763c9b84f0SGavin Shan debug_vm_pgtable_alloc_huge_page(struct pgtable_debug_args *args, int order)
10773c9b84f0SGavin Shan {
10783c9b84f0SGavin Shan 	struct page *page = NULL;
10793c9b84f0SGavin Shan 
10803c9b84f0SGavin Shan #ifdef CONFIG_CONTIG_ALLOC
108123baf831SKirill A. Shutemov 	if (order > MAX_ORDER) {
10823c9b84f0SGavin Shan 		page = alloc_contig_pages((1 << order), GFP_KERNEL,
10833c9b84f0SGavin Shan 					  first_online_node, NULL);
10843c9b84f0SGavin Shan 		if (page) {
10853c9b84f0SGavin Shan 			args->is_contiguous_page = true;
10863c9b84f0SGavin Shan 			return page;
10873c9b84f0SGavin Shan 		}
10883c9b84f0SGavin Shan 	}
10893c9b84f0SGavin Shan #endif
10903c9b84f0SGavin Shan 
109123baf831SKirill A. Shutemov 	if (order <= MAX_ORDER)
10923c9b84f0SGavin Shan 		page = alloc_pages(GFP_KERNEL, order);
10933c9b84f0SGavin Shan 
10943c9b84f0SGavin Shan 	return page;
10953c9b84f0SGavin Shan }
10963c9b84f0SGavin Shan 
1097c4876ff6SFrank van der Linden /*
1098c4876ff6SFrank van der Linden  * Check if a physical memory range described by <pstart, pend> contains
1099c4876ff6SFrank van der Linden  * an area that is of size psize, and aligned to psize.
1100c4876ff6SFrank van der Linden  *
1101c4876ff6SFrank van der Linden  * Don't use address 0, an all-zeroes physical address might mask bugs, and
1102c4876ff6SFrank van der Linden  * it's not used on x86.
1103c4876ff6SFrank van der Linden  */
phys_align_check(phys_addr_t pstart,phys_addr_t pend,unsigned long psize,phys_addr_t * physp,unsigned long * alignp)1104c4876ff6SFrank van der Linden static void  __init phys_align_check(phys_addr_t pstart,
1105c4876ff6SFrank van der Linden 				     phys_addr_t pend, unsigned long psize,
1106c4876ff6SFrank van der Linden 				     phys_addr_t *physp, unsigned long *alignp)
1107c4876ff6SFrank van der Linden {
1108c4876ff6SFrank van der Linden 	phys_addr_t aligned_start, aligned_end;
1109c4876ff6SFrank van der Linden 
1110c4876ff6SFrank van der Linden 	if (pstart == 0)
1111c4876ff6SFrank van der Linden 		pstart = PAGE_SIZE;
1112c4876ff6SFrank van der Linden 
1113c4876ff6SFrank van der Linden 	aligned_start = ALIGN(pstart, psize);
1114c4876ff6SFrank van der Linden 	aligned_end = aligned_start + psize;
1115c4876ff6SFrank van der Linden 
1116c4876ff6SFrank van der Linden 	if (aligned_end > aligned_start && aligned_end <= pend) {
1117c4876ff6SFrank van der Linden 		*alignp = psize;
1118c4876ff6SFrank van der Linden 		*physp = aligned_start;
1119c4876ff6SFrank van der Linden 	}
1120c4876ff6SFrank van der Linden }
1121c4876ff6SFrank van der Linden 
init_fixed_pfns(struct pgtable_debug_args * args)1122c4876ff6SFrank van der Linden static void __init init_fixed_pfns(struct pgtable_debug_args *args)
1123c4876ff6SFrank van der Linden {
1124c4876ff6SFrank van der Linden 	u64 idx;
1125c4876ff6SFrank van der Linden 	phys_addr_t phys, pstart, pend;
1126c4876ff6SFrank van der Linden 
1127c4876ff6SFrank van der Linden 	/*
1128c4876ff6SFrank van der Linden 	 * Initialize the fixed pfns. To do this, try to find a
1129c4876ff6SFrank van der Linden 	 * valid physical range, preferably aligned to PUD_SIZE,
1130c4876ff6SFrank van der Linden 	 * but settling for aligned to PMD_SIZE as a fallback. If
1131c4876ff6SFrank van der Linden 	 * neither of those is found, use the physical address of
1132c4876ff6SFrank van der Linden 	 * the start_kernel symbol.
1133c4876ff6SFrank van der Linden 	 *
1134c4876ff6SFrank van der Linden 	 * The memory doesn't need to be allocated, it just needs to exist
1135c4876ff6SFrank van der Linden 	 * as usable memory. It won't be touched.
1136c4876ff6SFrank van der Linden 	 *
1137c4876ff6SFrank van der Linden 	 * The alignment is recorded, and can be checked to see if we
1138c4876ff6SFrank van der Linden 	 * can run the tests that require an actual valid physical
1139c4876ff6SFrank van der Linden 	 * address range on some architectures ({pmd,pud}_huge_test
1140c4876ff6SFrank van der Linden 	 * on x86).
1141c4876ff6SFrank van der Linden 	 */
1142c4876ff6SFrank van der Linden 
1143c4876ff6SFrank van der Linden 	phys = __pa_symbol(&start_kernel);
1144c4876ff6SFrank van der Linden 	args->fixed_alignment = PAGE_SIZE;
1145c4876ff6SFrank van der Linden 
1146c4876ff6SFrank van der Linden 	for_each_mem_range(idx, &pstart, &pend) {
1147c4876ff6SFrank van der Linden 		/* First check for a PUD-aligned area */
1148c4876ff6SFrank van der Linden 		phys_align_check(pstart, pend, PUD_SIZE, &phys,
1149c4876ff6SFrank van der Linden 				 &args->fixed_alignment);
1150c4876ff6SFrank van der Linden 
1151c4876ff6SFrank van der Linden 		/* If a PUD-aligned area is found, we're done */
1152c4876ff6SFrank van der Linden 		if (args->fixed_alignment == PUD_SIZE)
1153c4876ff6SFrank van der Linden 			break;
1154c4876ff6SFrank van der Linden 
1155c4876ff6SFrank van der Linden 		/*
1156c4876ff6SFrank van der Linden 		 * If no PMD-aligned area found yet, check for one,
1157c4876ff6SFrank van der Linden 		 * but continue the loop to look for a PUD-aligned area.
1158c4876ff6SFrank van der Linden 		 */
1159c4876ff6SFrank van der Linden 		if (args->fixed_alignment < PMD_SIZE)
1160c4876ff6SFrank van der Linden 			phys_align_check(pstart, pend, PMD_SIZE, &phys,
1161c4876ff6SFrank van der Linden 					 &args->fixed_alignment);
1162c4876ff6SFrank van der Linden 	}
1163c4876ff6SFrank van der Linden 
1164c4876ff6SFrank van der Linden 	args->fixed_pgd_pfn = __phys_to_pfn(phys & PGDIR_MASK);
1165c4876ff6SFrank van der Linden 	args->fixed_p4d_pfn = __phys_to_pfn(phys & P4D_MASK);
1166c4876ff6SFrank van der Linden 	args->fixed_pud_pfn = __phys_to_pfn(phys & PUD_MASK);
1167c4876ff6SFrank van der Linden 	args->fixed_pmd_pfn = __phys_to_pfn(phys & PMD_MASK);
1168c4876ff6SFrank van der Linden 	args->fixed_pte_pfn = __phys_to_pfn(phys & PAGE_MASK);
1169c4876ff6SFrank van der Linden 	WARN_ON(!pfn_valid(args->fixed_pte_pfn));
1170c4876ff6SFrank van der Linden }
1171c4876ff6SFrank van der Linden 
1172c4876ff6SFrank van der Linden 
init_args(struct pgtable_debug_args * args)11733c9b84f0SGavin Shan static int __init init_args(struct pgtable_debug_args *args)
11743c9b84f0SGavin Shan {
11753c9b84f0SGavin Shan 	struct page *page = NULL;
11763c9b84f0SGavin Shan 	int ret = 0;
11773c9b84f0SGavin Shan 
11783c9b84f0SGavin Shan 	/*
11793c9b84f0SGavin Shan 	 * Initialize the debugging data.
11803c9b84f0SGavin Shan 	 *
118131d17076SAnshuman Khandual 	 * vm_get_page_prot(VM_NONE) or vm_get_page_prot(VM_SHARED|VM_NONE)
118231d17076SAnshuman Khandual 	 * will help create page table entries with PROT_NONE permission as
118331d17076SAnshuman Khandual 	 * required for pxx_protnone_tests().
11843c9b84f0SGavin Shan 	 */
11853c9b84f0SGavin Shan 	memset(args, 0, sizeof(*args));
11863c9b84f0SGavin Shan 	args->vaddr              = get_random_vaddr();
1187d7e679b6SKefeng Wang 	args->page_prot          = vm_get_page_prot(VM_ACCESS_FLAGS);
118831d17076SAnshuman Khandual 	args->page_prot_none     = vm_get_page_prot(VM_NONE);
11893c9b84f0SGavin Shan 	args->is_contiguous_page = false;
11903c9b84f0SGavin Shan 	args->pud_pfn            = ULONG_MAX;
11913c9b84f0SGavin Shan 	args->pmd_pfn            = ULONG_MAX;
11923c9b84f0SGavin Shan 	args->pte_pfn            = ULONG_MAX;
11933c9b84f0SGavin Shan 	args->fixed_pgd_pfn      = ULONG_MAX;
11943c9b84f0SGavin Shan 	args->fixed_p4d_pfn      = ULONG_MAX;
11953c9b84f0SGavin Shan 	args->fixed_pud_pfn      = ULONG_MAX;
11963c9b84f0SGavin Shan 	args->fixed_pmd_pfn      = ULONG_MAX;
11973c9b84f0SGavin Shan 	args->fixed_pte_pfn      = ULONG_MAX;
11983c9b84f0SGavin Shan 
11993c9b84f0SGavin Shan 	/* Allocate mm and vma */
12003c9b84f0SGavin Shan 	args->mm = mm_alloc();
12013c9b84f0SGavin Shan 	if (!args->mm) {
12023c9b84f0SGavin Shan 		pr_err("Failed to allocate mm struct\n");
12033c9b84f0SGavin Shan 		ret = -ENOMEM;
12043c9b84f0SGavin Shan 		goto error;
12053c9b84f0SGavin Shan 	}
12063c9b84f0SGavin Shan 
12073c9b84f0SGavin Shan 	args->vma = vm_area_alloc(args->mm);
12083c9b84f0SGavin Shan 	if (!args->vma) {
12093c9b84f0SGavin Shan 		pr_err("Failed to allocate vma\n");
12103c9b84f0SGavin Shan 		ret = -ENOMEM;
12113c9b84f0SGavin Shan 		goto error;
12123c9b84f0SGavin Shan 	}
12133c9b84f0SGavin Shan 
12143c9b84f0SGavin Shan 	/*
12153c9b84f0SGavin Shan 	 * Allocate page table entries. They will be modified in the tests.
12163c9b84f0SGavin Shan 	 * Lets save the page table entries so that they can be released
12173c9b84f0SGavin Shan 	 * when the tests are completed.
12183c9b84f0SGavin Shan 	 */
12193c9b84f0SGavin Shan 	args->pgdp = pgd_offset(args->mm, args->vaddr);
12203c9b84f0SGavin Shan 	args->p4dp = p4d_alloc(args->mm, args->pgdp, args->vaddr);
12213c9b84f0SGavin Shan 	if (!args->p4dp) {
12223c9b84f0SGavin Shan 		pr_err("Failed to allocate p4d entries\n");
12233c9b84f0SGavin Shan 		ret = -ENOMEM;
12243c9b84f0SGavin Shan 		goto error;
12253c9b84f0SGavin Shan 	}
12263c9b84f0SGavin Shan 	args->start_p4dp = p4d_offset(args->pgdp, 0UL);
12273c9b84f0SGavin Shan 	WARN_ON(!args->start_p4dp);
12283c9b84f0SGavin Shan 
12293c9b84f0SGavin Shan 	args->pudp = pud_alloc(args->mm, args->p4dp, args->vaddr);
12303c9b84f0SGavin Shan 	if (!args->pudp) {
12313c9b84f0SGavin Shan 		pr_err("Failed to allocate pud entries\n");
12323c9b84f0SGavin Shan 		ret = -ENOMEM;
12333c9b84f0SGavin Shan 		goto error;
12343c9b84f0SGavin Shan 	}
12353c9b84f0SGavin Shan 	args->start_pudp = pud_offset(args->p4dp, 0UL);
12363c9b84f0SGavin Shan 	WARN_ON(!args->start_pudp);
12373c9b84f0SGavin Shan 
12383c9b84f0SGavin Shan 	args->pmdp = pmd_alloc(args->mm, args->pudp, args->vaddr);
12393c9b84f0SGavin Shan 	if (!args->pmdp) {
12403c9b84f0SGavin Shan 		pr_err("Failed to allocate pmd entries\n");
12413c9b84f0SGavin Shan 		ret = -ENOMEM;
12423c9b84f0SGavin Shan 		goto error;
12433c9b84f0SGavin Shan 	}
12443c9b84f0SGavin Shan 	args->start_pmdp = pmd_offset(args->pudp, 0UL);
12453c9b84f0SGavin Shan 	WARN_ON(!args->start_pmdp);
12463c9b84f0SGavin Shan 
12473c9b84f0SGavin Shan 	if (pte_alloc(args->mm, args->pmdp)) {
12483c9b84f0SGavin Shan 		pr_err("Failed to allocate pte entries\n");
12493c9b84f0SGavin Shan 		ret = -ENOMEM;
12503c9b84f0SGavin Shan 		goto error;
12513c9b84f0SGavin Shan 	}
12523c9b84f0SGavin Shan 	args->start_ptep = pmd_pgtable(READ_ONCE(*args->pmdp));
12533c9b84f0SGavin Shan 	WARN_ON(!args->start_ptep);
12543c9b84f0SGavin Shan 
1255c4876ff6SFrank van der Linden 	init_fixed_pfns(args);
12563c9b84f0SGavin Shan 
12573c9b84f0SGavin Shan 	/*
12583c9b84f0SGavin Shan 	 * Allocate (huge) pages because some of the tests need to access
12593c9b84f0SGavin Shan 	 * the data in the pages. The corresponding tests will be skipped
12603c9b84f0SGavin Shan 	 * if we fail to allocate (huge) pages.
12613c9b84f0SGavin Shan 	 */
12623c9b84f0SGavin Shan 	if (IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE) &&
1263348ad160SAneesh Kumar K.V 	    has_transparent_pud_hugepage()) {
12643c9b84f0SGavin Shan 		page = debug_vm_pgtable_alloc_huge_page(args,
12653c9b84f0SGavin Shan 				HPAGE_PUD_SHIFT - PAGE_SHIFT);
12663c9b84f0SGavin Shan 		if (page) {
12673c9b84f0SGavin Shan 			args->pud_pfn = page_to_pfn(page);
12683c9b84f0SGavin Shan 			args->pmd_pfn = args->pud_pfn;
12693c9b84f0SGavin Shan 			args->pte_pfn = args->pud_pfn;
12703c9b84f0SGavin Shan 			return 0;
12713c9b84f0SGavin Shan 		}
12723c9b84f0SGavin Shan 	}
12733c9b84f0SGavin Shan 
12743c9b84f0SGavin Shan 	if (IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE) &&
12753c9b84f0SGavin Shan 	    has_transparent_hugepage()) {
12763c9b84f0SGavin Shan 		page = debug_vm_pgtable_alloc_huge_page(args, HPAGE_PMD_ORDER);
12773c9b84f0SGavin Shan 		if (page) {
12783c9b84f0SGavin Shan 			args->pmd_pfn = page_to_pfn(page);
12793c9b84f0SGavin Shan 			args->pte_pfn = args->pmd_pfn;
12803c9b84f0SGavin Shan 			return 0;
12813c9b84f0SGavin Shan 		}
12823c9b84f0SGavin Shan 	}
12833c9b84f0SGavin Shan 
1284dcc1be11SLorenzo Stoakes 	page = alloc_page(GFP_KERNEL);
12853c9b84f0SGavin Shan 	if (page)
12863c9b84f0SGavin Shan 		args->pte_pfn = page_to_pfn(page);
12873c9b84f0SGavin Shan 
12883c9b84f0SGavin Shan 	return 0;
12893c9b84f0SGavin Shan 
12903c9b84f0SGavin Shan error:
12913c9b84f0SGavin Shan 	destroy_args(args);
12923c9b84f0SGavin Shan 	return ret;
12933c9b84f0SGavin Shan }
12943c9b84f0SGavin Shan 
debug_vm_pgtable(void)1295399145f9SAnshuman Khandual static int __init debug_vm_pgtable(void)
1296399145f9SAnshuman Khandual {
12973c9b84f0SGavin Shan 	struct pgtable_debug_args args;
1298fea1120cSKees Cook 	spinlock_t *ptl = NULL;
12993c9b84f0SGavin Shan 	int idx, ret;
1300399145f9SAnshuman Khandual 
1301399145f9SAnshuman Khandual 	pr_info("Validating architecture page table helpers\n");
13023c9b84f0SGavin Shan 	ret = init_args(&args);
13033c9b84f0SGavin Shan 	if (ret)
13043c9b84f0SGavin Shan 		return ret;
13053c9b84f0SGavin Shan 
13062e326c07SAnshuman Khandual 	/*
130731d17076SAnshuman Khandual 	 * Iterate over each possible vm_flags to make sure that all
13082e326c07SAnshuman Khandual 	 * the basic page table transformation validations just hold
13092e326c07SAnshuman Khandual 	 * true irrespective of the starting protection value for a
13102e326c07SAnshuman Khandual 	 * given page table entry.
131131d17076SAnshuman Khandual 	 *
131231d17076SAnshuman Khandual 	 * Protection based vm_flags combinatins are always linear
131331d17076SAnshuman Khandual 	 * and increasing i.e starting from VM_NONE and going upto
131431d17076SAnshuman Khandual 	 * (VM_SHARED | READ | WRITE | EXEC).
13152e326c07SAnshuman Khandual 	 */
131631d17076SAnshuman Khandual #define VM_FLAGS_START	(VM_NONE)
131731d17076SAnshuman Khandual #define VM_FLAGS_END	(VM_SHARED | VM_EXEC | VM_WRITE | VM_READ)
131831d17076SAnshuman Khandual 
131931d17076SAnshuman Khandual 	for (idx = VM_FLAGS_START; idx <= VM_FLAGS_END; idx++) {
132036b77d1eSGavin Shan 		pte_basic_tests(&args, idx);
132136b77d1eSGavin Shan 		pmd_basic_tests(&args, idx);
132236b77d1eSGavin Shan 		pud_basic_tests(&args, idx);
13232e326c07SAnshuman Khandual 	}
13242e326c07SAnshuman Khandual 
13252e326c07SAnshuman Khandual 	/*
13262e326c07SAnshuman Khandual 	 * Both P4D and PGD level tests are very basic which do not
13272e326c07SAnshuman Khandual 	 * involve creating page table entries from the protection
13282e326c07SAnshuman Khandual 	 * value and the given pfn. Hence just keep them out from
13292e326c07SAnshuman Khandual 	 * the above iteration for now to save some test execution
13302e326c07SAnshuman Khandual 	 * time.
13312e326c07SAnshuman Khandual 	 */
133236b77d1eSGavin Shan 	p4d_basic_tests(&args);
133336b77d1eSGavin Shan 	pgd_basic_tests(&args);
1334399145f9SAnshuman Khandual 
13358983d231SGavin Shan 	pmd_leaf_tests(&args);
13368983d231SGavin Shan 	pud_leaf_tests(&args);
1337a5c3b9ffSAnshuman Khandual 
13388cb183f2SGavin Shan 	pte_special_tests(&args);
13398cb183f2SGavin Shan 	pte_protnone_tests(&args);
13408cb183f2SGavin Shan 	pmd_protnone_tests(&args);
134105289402SAnshuman Khandual 
13428cb183f2SGavin Shan 	pte_devmap_tests(&args);
13438cb183f2SGavin Shan 	pmd_devmap_tests(&args);
13448cb183f2SGavin Shan 	pud_devmap_tests(&args);
134505289402SAnshuman Khandual 
13465f447e80SGavin Shan 	pte_soft_dirty_tests(&args);
13475f447e80SGavin Shan 	pmd_soft_dirty_tests(&args);
13485f447e80SGavin Shan 	pte_swap_soft_dirty_tests(&args);
13495f447e80SGavin Shan 	pmd_swap_soft_dirty_tests(&args);
135005289402SAnshuman Khandual 
1351210d1e8aSDavid Hildenbrand 	pte_swap_exclusive_tests(&args);
1352210d1e8aSDavid Hildenbrand 
13535f447e80SGavin Shan 	pte_swap_tests(&args);
13545f447e80SGavin Shan 	pmd_swap_tests(&args);
135505289402SAnshuman Khandual 
13564878a888SGavin Shan 	swap_migration_tests(&args);
135705289402SAnshuman Khandual 
13584878a888SGavin Shan 	pmd_thp_tests(&args);
13594878a888SGavin Shan 	pud_thp_tests(&args);
136005289402SAnshuman Khandual 
136136b77d1eSGavin Shan 	hugetlb_basic_tests(&args);
1362e8edf0adSAneesh Kumar K.V 
13636f302e27SAneesh Kumar K.V 	/*
13646f302e27SAneesh Kumar K.V 	 * Page table modifying tests. They need to hold
13656f302e27SAneesh Kumar K.V 	 * proper page table lock.
13666f302e27SAneesh Kumar K.V 	 */
1367e8edf0adSAneesh Kumar K.V 
136844966c44SGavin Shan 	args.ptep = pte_offset_map_lock(args.mm, args.pmdp, args.vaddr, &ptl);
136944966c44SGavin Shan 	pte_clear_tests(&args);
137044966c44SGavin Shan 	pte_advanced_tests(&args);
13719f2bad09SHugh Dickins 	if (args.ptep)
137244966c44SGavin Shan 		pte_unmap_unlock(args.ptep, ptl);
1373e8edf0adSAneesh Kumar K.V 
1374c0fe07b0SGavin Shan 	ptl = pmd_lock(args.mm, args.pmdp);
1375c0fe07b0SGavin Shan 	pmd_clear_tests(&args);
1376c0fe07b0SGavin Shan 	pmd_advanced_tests(&args);
1377c0fe07b0SGavin Shan 	pmd_huge_tests(&args);
1378c0fe07b0SGavin Shan 	pmd_populate_tests(&args);
13796f302e27SAneesh Kumar K.V 	spin_unlock(ptl);
13806f302e27SAneesh Kumar K.V 
13814cbde03bSGavin Shan 	ptl = pud_lock(args.mm, args.pudp);
13824cbde03bSGavin Shan 	pud_clear_tests(&args);
13834cbde03bSGavin Shan 	pud_advanced_tests(&args);
13844cbde03bSGavin Shan 	pud_huge_tests(&args);
13854cbde03bSGavin Shan 	pud_populate_tests(&args);
13866f302e27SAneesh Kumar K.V 	spin_unlock(ptl);
13876f302e27SAneesh Kumar K.V 
13882f87f8c3SGavin Shan 	spin_lock(&(args.mm->page_table_lock));
13892f87f8c3SGavin Shan 	p4d_clear_tests(&args);
13902f87f8c3SGavin Shan 	pgd_clear_tests(&args);
13912f87f8c3SGavin Shan 	p4d_populate_tests(&args);
13922f87f8c3SGavin Shan 	pgd_populate_tests(&args);
13932f87f8c3SGavin Shan 	spin_unlock(&(args.mm->page_table_lock));
1394e8edf0adSAneesh Kumar K.V 
13953c9b84f0SGavin Shan 	destroy_args(&args);
1396399145f9SAnshuman Khandual 	return 0;
1397399145f9SAnshuman Khandual }
1398399145f9SAnshuman Khandual late_initcall(debug_vm_pgtable);
1399