xref: /openbmc/linux/mm/debug_vm_pgtable.c (revision 2f164822)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * This kernel test validates architecture page table helpers and
4  * accessors and helps in verifying their continued compliance with
5  * expected generic MM semantics.
6  *
7  * Copyright (C) 2019 ARM Ltd.
8  *
9  * Author: Anshuman Khandual <anshuman.khandual@arm.com>
10  */
11 #define pr_fmt(fmt) "debug_vm_pgtable: [%-25s]: " fmt, __func__
12 
13 #include <linux/gfp.h>
14 #include <linux/highmem.h>
15 #include <linux/hugetlb.h>
16 #include <linux/kernel.h>
17 #include <linux/kconfig.h>
18 #include <linux/memblock.h>
19 #include <linux/mm.h>
20 #include <linux/mman.h>
21 #include <linux/mm_types.h>
22 #include <linux/module.h>
23 #include <linux/pfn_t.h>
24 #include <linux/printk.h>
25 #include <linux/pgtable.h>
26 #include <linux/random.h>
27 #include <linux/spinlock.h>
28 #include <linux/swap.h>
29 #include <linux/swapops.h>
30 #include <linux/start_kernel.h>
31 #include <linux/sched/mm.h>
32 #include <linux/io.h>
33 
34 #include <asm/cacheflush.h>
35 #include <asm/pgalloc.h>
36 #include <asm/tlbflush.h>
37 
38 /*
39  * Please refer Documentation/mm/arch_pgtable_helpers.rst for the semantics
40  * expectations that are being validated here. All future changes in here
41  * or the documentation need to be in sync.
42  *
43  * On s390 platform, the lower 4 bits are used to identify given page table
44  * entry type. But these bits might affect the ability to clear entries with
45  * pxx_clear() because of how dynamic page table folding works on s390. So
46  * while loading up the entries do not change the lower 4 bits. It does not
47  * have affect any other platform. Also avoid the 62nd bit on ppc64 that is
48  * used to mark a pte entry.
49  */
50 #define S390_SKIP_MASK		GENMASK(3, 0)
51 #if __BITS_PER_LONG == 64
52 #define PPC64_SKIP_MASK		GENMASK(62, 62)
53 #else
54 #define PPC64_SKIP_MASK		0x0
55 #endif
56 #define ARCH_SKIP_MASK (S390_SKIP_MASK | PPC64_SKIP_MASK)
57 #define RANDOM_ORVALUE (GENMASK(BITS_PER_LONG - 1, 0) & ~ARCH_SKIP_MASK)
58 #define RANDOM_NZVALUE	GENMASK(7, 0)
59 
60 struct pgtable_debug_args {
61 	struct mm_struct	*mm;
62 	struct vm_area_struct	*vma;
63 
64 	pgd_t			*pgdp;
65 	p4d_t			*p4dp;
66 	pud_t			*pudp;
67 	pmd_t			*pmdp;
68 	pte_t			*ptep;
69 
70 	p4d_t			*start_p4dp;
71 	pud_t			*start_pudp;
72 	pmd_t			*start_pmdp;
73 	pgtable_t		start_ptep;
74 
75 	unsigned long		vaddr;
76 	pgprot_t		page_prot;
77 	pgprot_t		page_prot_none;
78 
79 	bool			is_contiguous_page;
80 	unsigned long		pud_pfn;
81 	unsigned long		pmd_pfn;
82 	unsigned long		pte_pfn;
83 
84 	unsigned long		fixed_alignment;
85 	unsigned long		fixed_pgd_pfn;
86 	unsigned long		fixed_p4d_pfn;
87 	unsigned long		fixed_pud_pfn;
88 	unsigned long		fixed_pmd_pfn;
89 	unsigned long		fixed_pte_pfn;
90 };
91 
92 static void __init pte_basic_tests(struct pgtable_debug_args *args, int idx)
93 {
94 	pgprot_t prot = vm_get_page_prot(idx);
95 	pte_t pte = pfn_pte(args->fixed_pte_pfn, prot);
96 	unsigned long val = idx, *ptr = &val;
97 
98 	pr_debug("Validating PTE basic (%pGv)\n", ptr);
99 
100 	/*
101 	 * This test needs to be executed after the given page table entry
102 	 * is created with pfn_pte() to make sure that vm_get_page_prot(idx)
103 	 * does not have the dirty bit enabled from the beginning. This is
104 	 * important for platforms like arm64 where (!PTE_RDONLY) indicate
105 	 * dirty bit being set.
106 	 */
107 	WARN_ON(pte_dirty(pte_wrprotect(pte)));
108 
109 	WARN_ON(!pte_same(pte, pte));
110 	WARN_ON(!pte_young(pte_mkyoung(pte_mkold(pte))));
111 	WARN_ON(!pte_dirty(pte_mkdirty(pte_mkclean(pte))));
112 	WARN_ON(!pte_write(pte_mkwrite(pte_wrprotect(pte))));
113 	WARN_ON(pte_young(pte_mkold(pte_mkyoung(pte))));
114 	WARN_ON(pte_dirty(pte_mkclean(pte_mkdirty(pte))));
115 	WARN_ON(pte_write(pte_wrprotect(pte_mkwrite(pte))));
116 	WARN_ON(pte_dirty(pte_wrprotect(pte_mkclean(pte))));
117 	WARN_ON(!pte_dirty(pte_wrprotect(pte_mkdirty(pte))));
118 }
119 
120 static void __init pte_advanced_tests(struct pgtable_debug_args *args)
121 {
122 	struct page *page;
123 	pte_t pte;
124 
125 	/*
126 	 * Architectures optimize set_pte_at by avoiding TLB flush.
127 	 * This requires set_pte_at to be not used to update an
128 	 * existing pte entry. Clear pte before we do set_pte_at
129 	 *
130 	 * flush_dcache_page() is called after set_pte_at() to clear
131 	 * PG_arch_1 for the page on ARM64. The page flag isn't cleared
132 	 * when it's released and page allocation check will fail when
133 	 * the page is allocated again. For architectures other than ARM64,
134 	 * the unexpected overhead of cache flushing is acceptable.
135 	 */
136 	page = (args->pte_pfn != ULONG_MAX) ? pfn_to_page(args->pte_pfn) : NULL;
137 	if (!page)
138 		return;
139 
140 	pr_debug("Validating PTE advanced\n");
141 	pte = pfn_pte(args->pte_pfn, args->page_prot);
142 	set_pte_at(args->mm, args->vaddr, args->ptep, pte);
143 	flush_dcache_page(page);
144 	ptep_set_wrprotect(args->mm, args->vaddr, args->ptep);
145 	pte = ptep_get(args->ptep);
146 	WARN_ON(pte_write(pte));
147 	ptep_get_and_clear(args->mm, args->vaddr, args->ptep);
148 	pte = ptep_get(args->ptep);
149 	WARN_ON(!pte_none(pte));
150 
151 	pte = pfn_pte(args->pte_pfn, args->page_prot);
152 	pte = pte_wrprotect(pte);
153 	pte = pte_mkclean(pte);
154 	set_pte_at(args->mm, args->vaddr, args->ptep, pte);
155 	flush_dcache_page(page);
156 	pte = pte_mkwrite(pte);
157 	pte = pte_mkdirty(pte);
158 	ptep_set_access_flags(args->vma, args->vaddr, args->ptep, pte, 1);
159 	pte = ptep_get(args->ptep);
160 	WARN_ON(!(pte_write(pte) && pte_dirty(pte)));
161 	ptep_get_and_clear_full(args->mm, args->vaddr, args->ptep, 1);
162 	pte = ptep_get(args->ptep);
163 	WARN_ON(!pte_none(pte));
164 
165 	pte = pfn_pte(args->pte_pfn, args->page_prot);
166 	pte = pte_mkyoung(pte);
167 	set_pte_at(args->mm, args->vaddr, args->ptep, pte);
168 	flush_dcache_page(page);
169 	ptep_test_and_clear_young(args->vma, args->vaddr, args->ptep);
170 	pte = ptep_get(args->ptep);
171 	WARN_ON(pte_young(pte));
172 
173 	ptep_get_and_clear_full(args->mm, args->vaddr, args->ptep, 1);
174 }
175 
176 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
177 static void __init pmd_basic_tests(struct pgtable_debug_args *args, int idx)
178 {
179 	pgprot_t prot = vm_get_page_prot(idx);
180 	unsigned long val = idx, *ptr = &val;
181 	pmd_t pmd;
182 
183 	if (!has_transparent_hugepage())
184 		return;
185 
186 	pr_debug("Validating PMD basic (%pGv)\n", ptr);
187 	pmd = pfn_pmd(args->fixed_pmd_pfn, prot);
188 
189 	/*
190 	 * This test needs to be executed after the given page table entry
191 	 * is created with pfn_pmd() to make sure that vm_get_page_prot(idx)
192 	 * does not have the dirty bit enabled from the beginning. This is
193 	 * important for platforms like arm64 where (!PTE_RDONLY) indicate
194 	 * dirty bit being set.
195 	 */
196 	WARN_ON(pmd_dirty(pmd_wrprotect(pmd)));
197 
198 
199 	WARN_ON(!pmd_same(pmd, pmd));
200 	WARN_ON(!pmd_young(pmd_mkyoung(pmd_mkold(pmd))));
201 	WARN_ON(!pmd_dirty(pmd_mkdirty(pmd_mkclean(pmd))));
202 	WARN_ON(!pmd_write(pmd_mkwrite(pmd_wrprotect(pmd))));
203 	WARN_ON(pmd_young(pmd_mkold(pmd_mkyoung(pmd))));
204 	WARN_ON(pmd_dirty(pmd_mkclean(pmd_mkdirty(pmd))));
205 	WARN_ON(pmd_write(pmd_wrprotect(pmd_mkwrite(pmd))));
206 	WARN_ON(pmd_dirty(pmd_wrprotect(pmd_mkclean(pmd))));
207 	WARN_ON(!pmd_dirty(pmd_wrprotect(pmd_mkdirty(pmd))));
208 	/*
209 	 * A huge page does not point to next level page table
210 	 * entry. Hence this must qualify as pmd_bad().
211 	 */
212 	WARN_ON(!pmd_bad(pmd_mkhuge(pmd)));
213 }
214 
215 static void __init pmd_advanced_tests(struct pgtable_debug_args *args)
216 {
217 	struct page *page;
218 	pmd_t pmd;
219 	unsigned long vaddr = args->vaddr;
220 
221 	if (!has_transparent_hugepage())
222 		return;
223 
224 	page = (args->pmd_pfn != ULONG_MAX) ? pfn_to_page(args->pmd_pfn) : NULL;
225 	if (!page)
226 		return;
227 
228 	/*
229 	 * flush_dcache_page() is called after set_pmd_at() to clear
230 	 * PG_arch_1 for the page on ARM64. The page flag isn't cleared
231 	 * when it's released and page allocation check will fail when
232 	 * the page is allocated again. For architectures other than ARM64,
233 	 * the unexpected overhead of cache flushing is acceptable.
234 	 */
235 	pr_debug("Validating PMD advanced\n");
236 	/* Align the address wrt HPAGE_PMD_SIZE */
237 	vaddr &= HPAGE_PMD_MASK;
238 
239 	pgtable_trans_huge_deposit(args->mm, args->pmdp, args->start_ptep);
240 
241 	pmd = pfn_pmd(args->pmd_pfn, args->page_prot);
242 	set_pmd_at(args->mm, vaddr, args->pmdp, pmd);
243 	flush_dcache_page(page);
244 	pmdp_set_wrprotect(args->mm, vaddr, args->pmdp);
245 	pmd = READ_ONCE(*args->pmdp);
246 	WARN_ON(pmd_write(pmd));
247 	pmdp_huge_get_and_clear(args->mm, vaddr, args->pmdp);
248 	pmd = READ_ONCE(*args->pmdp);
249 	WARN_ON(!pmd_none(pmd));
250 
251 	pmd = pfn_pmd(args->pmd_pfn, args->page_prot);
252 	pmd = pmd_wrprotect(pmd);
253 	pmd = pmd_mkclean(pmd);
254 	set_pmd_at(args->mm, vaddr, args->pmdp, pmd);
255 	flush_dcache_page(page);
256 	pmd = pmd_mkwrite(pmd);
257 	pmd = pmd_mkdirty(pmd);
258 	pmdp_set_access_flags(args->vma, vaddr, args->pmdp, pmd, 1);
259 	pmd = READ_ONCE(*args->pmdp);
260 	WARN_ON(!(pmd_write(pmd) && pmd_dirty(pmd)));
261 	pmdp_huge_get_and_clear_full(args->vma, vaddr, args->pmdp, 1);
262 	pmd = READ_ONCE(*args->pmdp);
263 	WARN_ON(!pmd_none(pmd));
264 
265 	pmd = pmd_mkhuge(pfn_pmd(args->pmd_pfn, args->page_prot));
266 	pmd = pmd_mkyoung(pmd);
267 	set_pmd_at(args->mm, vaddr, args->pmdp, pmd);
268 	flush_dcache_page(page);
269 	pmdp_test_and_clear_young(args->vma, vaddr, args->pmdp);
270 	pmd = READ_ONCE(*args->pmdp);
271 	WARN_ON(pmd_young(pmd));
272 
273 	/*  Clear the pte entries  */
274 	pmdp_huge_get_and_clear(args->mm, vaddr, args->pmdp);
275 	pgtable_trans_huge_withdraw(args->mm, args->pmdp);
276 }
277 
278 static void __init pmd_leaf_tests(struct pgtable_debug_args *args)
279 {
280 	pmd_t pmd;
281 
282 	if (!has_transparent_hugepage())
283 		return;
284 
285 	pr_debug("Validating PMD leaf\n");
286 	pmd = pfn_pmd(args->fixed_pmd_pfn, args->page_prot);
287 
288 	/*
289 	 * PMD based THP is a leaf entry.
290 	 */
291 	pmd = pmd_mkhuge(pmd);
292 	WARN_ON(!pmd_leaf(pmd));
293 }
294 
295 #ifdef CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD
296 static void __init pud_basic_tests(struct pgtable_debug_args *args, int idx)
297 {
298 	pgprot_t prot = vm_get_page_prot(idx);
299 	unsigned long val = idx, *ptr = &val;
300 	pud_t pud;
301 
302 	if (!has_transparent_hugepage())
303 		return;
304 
305 	pr_debug("Validating PUD basic (%pGv)\n", ptr);
306 	pud = pfn_pud(args->fixed_pud_pfn, prot);
307 
308 	/*
309 	 * This test needs to be executed after the given page table entry
310 	 * is created with pfn_pud() to make sure that vm_get_page_prot(idx)
311 	 * does not have the dirty bit enabled from the beginning. This is
312 	 * important for platforms like arm64 where (!PTE_RDONLY) indicate
313 	 * dirty bit being set.
314 	 */
315 	WARN_ON(pud_dirty(pud_wrprotect(pud)));
316 
317 	WARN_ON(!pud_same(pud, pud));
318 	WARN_ON(!pud_young(pud_mkyoung(pud_mkold(pud))));
319 	WARN_ON(!pud_dirty(pud_mkdirty(pud_mkclean(pud))));
320 	WARN_ON(pud_dirty(pud_mkclean(pud_mkdirty(pud))));
321 	WARN_ON(!pud_write(pud_mkwrite(pud_wrprotect(pud))));
322 	WARN_ON(pud_write(pud_wrprotect(pud_mkwrite(pud))));
323 	WARN_ON(pud_young(pud_mkold(pud_mkyoung(pud))));
324 	WARN_ON(pud_dirty(pud_wrprotect(pud_mkclean(pud))));
325 	WARN_ON(!pud_dirty(pud_wrprotect(pud_mkdirty(pud))));
326 
327 	if (mm_pmd_folded(args->mm))
328 		return;
329 
330 	/*
331 	 * A huge page does not point to next level page table
332 	 * entry. Hence this must qualify as pud_bad().
333 	 */
334 	WARN_ON(!pud_bad(pud_mkhuge(pud)));
335 }
336 
337 static void __init pud_advanced_tests(struct pgtable_debug_args *args)
338 {
339 	struct page *page;
340 	unsigned long vaddr = args->vaddr;
341 	pud_t pud;
342 
343 	if (!has_transparent_hugepage())
344 		return;
345 
346 	page = (args->pud_pfn != ULONG_MAX) ? pfn_to_page(args->pud_pfn) : NULL;
347 	if (!page)
348 		return;
349 
350 	/*
351 	 * flush_dcache_page() is called after set_pud_at() to clear
352 	 * PG_arch_1 for the page on ARM64. The page flag isn't cleared
353 	 * when it's released and page allocation check will fail when
354 	 * the page is allocated again. For architectures other than ARM64,
355 	 * the unexpected overhead of cache flushing is acceptable.
356 	 */
357 	pr_debug("Validating PUD advanced\n");
358 	/* Align the address wrt HPAGE_PUD_SIZE */
359 	vaddr &= HPAGE_PUD_MASK;
360 
361 	pud = pfn_pud(args->pud_pfn, args->page_prot);
362 	set_pud_at(args->mm, vaddr, args->pudp, pud);
363 	flush_dcache_page(page);
364 	pudp_set_wrprotect(args->mm, vaddr, args->pudp);
365 	pud = READ_ONCE(*args->pudp);
366 	WARN_ON(pud_write(pud));
367 
368 #ifndef __PAGETABLE_PMD_FOLDED
369 	pudp_huge_get_and_clear(args->mm, vaddr, args->pudp);
370 	pud = READ_ONCE(*args->pudp);
371 	WARN_ON(!pud_none(pud));
372 #endif /* __PAGETABLE_PMD_FOLDED */
373 	pud = pfn_pud(args->pud_pfn, args->page_prot);
374 	pud = pud_wrprotect(pud);
375 	pud = pud_mkclean(pud);
376 	set_pud_at(args->mm, vaddr, args->pudp, pud);
377 	flush_dcache_page(page);
378 	pud = pud_mkwrite(pud);
379 	pud = pud_mkdirty(pud);
380 	pudp_set_access_flags(args->vma, vaddr, args->pudp, pud, 1);
381 	pud = READ_ONCE(*args->pudp);
382 	WARN_ON(!(pud_write(pud) && pud_dirty(pud)));
383 
384 #ifndef __PAGETABLE_PMD_FOLDED
385 	pudp_huge_get_and_clear_full(args->mm, vaddr, args->pudp, 1);
386 	pud = READ_ONCE(*args->pudp);
387 	WARN_ON(!pud_none(pud));
388 #endif /* __PAGETABLE_PMD_FOLDED */
389 
390 	pud = pfn_pud(args->pud_pfn, args->page_prot);
391 	pud = pud_mkyoung(pud);
392 	set_pud_at(args->mm, vaddr, args->pudp, pud);
393 	flush_dcache_page(page);
394 	pudp_test_and_clear_young(args->vma, vaddr, args->pudp);
395 	pud = READ_ONCE(*args->pudp);
396 	WARN_ON(pud_young(pud));
397 
398 	pudp_huge_get_and_clear(args->mm, vaddr, args->pudp);
399 }
400 
401 static void __init pud_leaf_tests(struct pgtable_debug_args *args)
402 {
403 	pud_t pud;
404 
405 	if (!has_transparent_hugepage())
406 		return;
407 
408 	pr_debug("Validating PUD leaf\n");
409 	pud = pfn_pud(args->fixed_pud_pfn, args->page_prot);
410 	/*
411 	 * PUD based THP is a leaf entry.
412 	 */
413 	pud = pud_mkhuge(pud);
414 	WARN_ON(!pud_leaf(pud));
415 }
416 #else  /* !CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD */
417 static void __init pud_basic_tests(struct pgtable_debug_args *args, int idx) { }
418 static void __init pud_advanced_tests(struct pgtable_debug_args *args) { }
419 static void __init pud_leaf_tests(struct pgtable_debug_args *args) { }
420 #endif /* CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD */
421 #else  /* !CONFIG_TRANSPARENT_HUGEPAGE */
422 static void __init pmd_basic_tests(struct pgtable_debug_args *args, int idx) { }
423 static void __init pud_basic_tests(struct pgtable_debug_args *args, int idx) { }
424 static void __init pmd_advanced_tests(struct pgtable_debug_args *args) { }
425 static void __init pud_advanced_tests(struct pgtable_debug_args *args) { }
426 static void __init pmd_leaf_tests(struct pgtable_debug_args *args) { }
427 static void __init pud_leaf_tests(struct pgtable_debug_args *args) { }
428 #endif /* CONFIG_TRANSPARENT_HUGEPAGE */
429 
430 #ifdef CONFIG_HAVE_ARCH_HUGE_VMAP
431 static void __init pmd_huge_tests(struct pgtable_debug_args *args)
432 {
433 	pmd_t pmd;
434 
435 	if (!arch_vmap_pmd_supported(args->page_prot) ||
436 	    args->fixed_alignment < PMD_SIZE)
437 		return;
438 
439 	pr_debug("Validating PMD huge\n");
440 	/*
441 	 * X86 defined pmd_set_huge() verifies that the given
442 	 * PMD is not a populated non-leaf entry.
443 	 */
444 	WRITE_ONCE(*args->pmdp, __pmd(0));
445 	WARN_ON(!pmd_set_huge(args->pmdp, __pfn_to_phys(args->fixed_pmd_pfn), args->page_prot));
446 	WARN_ON(!pmd_clear_huge(args->pmdp));
447 	pmd = READ_ONCE(*args->pmdp);
448 	WARN_ON(!pmd_none(pmd));
449 }
450 
451 static void __init pud_huge_tests(struct pgtable_debug_args *args)
452 {
453 	pud_t pud;
454 
455 	if (!arch_vmap_pud_supported(args->page_prot) ||
456 	    args->fixed_alignment < PUD_SIZE)
457 		return;
458 
459 	pr_debug("Validating PUD huge\n");
460 	/*
461 	 * X86 defined pud_set_huge() verifies that the given
462 	 * PUD is not a populated non-leaf entry.
463 	 */
464 	WRITE_ONCE(*args->pudp, __pud(0));
465 	WARN_ON(!pud_set_huge(args->pudp, __pfn_to_phys(args->fixed_pud_pfn), args->page_prot));
466 	WARN_ON(!pud_clear_huge(args->pudp));
467 	pud = READ_ONCE(*args->pudp);
468 	WARN_ON(!pud_none(pud));
469 }
470 #else /* !CONFIG_HAVE_ARCH_HUGE_VMAP */
471 static void __init pmd_huge_tests(struct pgtable_debug_args *args) { }
472 static void __init pud_huge_tests(struct pgtable_debug_args *args) { }
473 #endif /* CONFIG_HAVE_ARCH_HUGE_VMAP */
474 
475 static void __init p4d_basic_tests(struct pgtable_debug_args *args)
476 {
477 	p4d_t p4d;
478 
479 	pr_debug("Validating P4D basic\n");
480 	memset(&p4d, RANDOM_NZVALUE, sizeof(p4d_t));
481 	WARN_ON(!p4d_same(p4d, p4d));
482 }
483 
484 static void __init pgd_basic_tests(struct pgtable_debug_args *args)
485 {
486 	pgd_t pgd;
487 
488 	pr_debug("Validating PGD basic\n");
489 	memset(&pgd, RANDOM_NZVALUE, sizeof(pgd_t));
490 	WARN_ON(!pgd_same(pgd, pgd));
491 }
492 
493 #ifndef __PAGETABLE_PUD_FOLDED
494 static void __init pud_clear_tests(struct pgtable_debug_args *args)
495 {
496 	pud_t pud = READ_ONCE(*args->pudp);
497 
498 	if (mm_pmd_folded(args->mm))
499 		return;
500 
501 	pr_debug("Validating PUD clear\n");
502 	pud = __pud(pud_val(pud) | RANDOM_ORVALUE);
503 	WRITE_ONCE(*args->pudp, pud);
504 	pud_clear(args->pudp);
505 	pud = READ_ONCE(*args->pudp);
506 	WARN_ON(!pud_none(pud));
507 }
508 
509 static void __init pud_populate_tests(struct pgtable_debug_args *args)
510 {
511 	pud_t pud;
512 
513 	if (mm_pmd_folded(args->mm))
514 		return;
515 
516 	pr_debug("Validating PUD populate\n");
517 	/*
518 	 * This entry points to next level page table page.
519 	 * Hence this must not qualify as pud_bad().
520 	 */
521 	pud_populate(args->mm, args->pudp, args->start_pmdp);
522 	pud = READ_ONCE(*args->pudp);
523 	WARN_ON(pud_bad(pud));
524 }
525 #else  /* !__PAGETABLE_PUD_FOLDED */
526 static void __init pud_clear_tests(struct pgtable_debug_args *args) { }
527 static void __init pud_populate_tests(struct pgtable_debug_args *args) { }
528 #endif /* PAGETABLE_PUD_FOLDED */
529 
530 #ifndef __PAGETABLE_P4D_FOLDED
531 static void __init p4d_clear_tests(struct pgtable_debug_args *args)
532 {
533 	p4d_t p4d = READ_ONCE(*args->p4dp);
534 
535 	if (mm_pud_folded(args->mm))
536 		return;
537 
538 	pr_debug("Validating P4D clear\n");
539 	p4d = __p4d(p4d_val(p4d) | RANDOM_ORVALUE);
540 	WRITE_ONCE(*args->p4dp, p4d);
541 	p4d_clear(args->p4dp);
542 	p4d = READ_ONCE(*args->p4dp);
543 	WARN_ON(!p4d_none(p4d));
544 }
545 
546 static void __init p4d_populate_tests(struct pgtable_debug_args *args)
547 {
548 	p4d_t p4d;
549 
550 	if (mm_pud_folded(args->mm))
551 		return;
552 
553 	pr_debug("Validating P4D populate\n");
554 	/*
555 	 * This entry points to next level page table page.
556 	 * Hence this must not qualify as p4d_bad().
557 	 */
558 	pud_clear(args->pudp);
559 	p4d_clear(args->p4dp);
560 	p4d_populate(args->mm, args->p4dp, args->start_pudp);
561 	p4d = READ_ONCE(*args->p4dp);
562 	WARN_ON(p4d_bad(p4d));
563 }
564 
565 static void __init pgd_clear_tests(struct pgtable_debug_args *args)
566 {
567 	pgd_t pgd = READ_ONCE(*(args->pgdp));
568 
569 	if (mm_p4d_folded(args->mm))
570 		return;
571 
572 	pr_debug("Validating PGD clear\n");
573 	pgd = __pgd(pgd_val(pgd) | RANDOM_ORVALUE);
574 	WRITE_ONCE(*args->pgdp, pgd);
575 	pgd_clear(args->pgdp);
576 	pgd = READ_ONCE(*args->pgdp);
577 	WARN_ON(!pgd_none(pgd));
578 }
579 
580 static void __init pgd_populate_tests(struct pgtable_debug_args *args)
581 {
582 	pgd_t pgd;
583 
584 	if (mm_p4d_folded(args->mm))
585 		return;
586 
587 	pr_debug("Validating PGD populate\n");
588 	/*
589 	 * This entry points to next level page table page.
590 	 * Hence this must not qualify as pgd_bad().
591 	 */
592 	p4d_clear(args->p4dp);
593 	pgd_clear(args->pgdp);
594 	pgd_populate(args->mm, args->pgdp, args->start_p4dp);
595 	pgd = READ_ONCE(*args->pgdp);
596 	WARN_ON(pgd_bad(pgd));
597 }
598 #else  /* !__PAGETABLE_P4D_FOLDED */
599 static void __init p4d_clear_tests(struct pgtable_debug_args *args) { }
600 static void __init pgd_clear_tests(struct pgtable_debug_args *args) { }
601 static void __init p4d_populate_tests(struct pgtable_debug_args *args) { }
602 static void __init pgd_populate_tests(struct pgtable_debug_args *args) { }
603 #endif /* PAGETABLE_P4D_FOLDED */
604 
605 static void __init pte_clear_tests(struct pgtable_debug_args *args)
606 {
607 	struct page *page;
608 	pte_t pte = pfn_pte(args->pte_pfn, args->page_prot);
609 
610 	page = (args->pte_pfn != ULONG_MAX) ? pfn_to_page(args->pte_pfn) : NULL;
611 	if (!page)
612 		return;
613 
614 	/*
615 	 * flush_dcache_page() is called after set_pte_at() to clear
616 	 * PG_arch_1 for the page on ARM64. The page flag isn't cleared
617 	 * when it's released and page allocation check will fail when
618 	 * the page is allocated again. For architectures other than ARM64,
619 	 * the unexpected overhead of cache flushing is acceptable.
620 	 */
621 	pr_debug("Validating PTE clear\n");
622 #ifndef CONFIG_RISCV
623 	pte = __pte(pte_val(pte) | RANDOM_ORVALUE);
624 #endif
625 	set_pte_at(args->mm, args->vaddr, args->ptep, pte);
626 	flush_dcache_page(page);
627 	barrier();
628 	ptep_clear(args->mm, args->vaddr, args->ptep);
629 	pte = ptep_get(args->ptep);
630 	WARN_ON(!pte_none(pte));
631 }
632 
633 static void __init pmd_clear_tests(struct pgtable_debug_args *args)
634 {
635 	pmd_t pmd = READ_ONCE(*args->pmdp);
636 
637 	pr_debug("Validating PMD clear\n");
638 	pmd = __pmd(pmd_val(pmd) | RANDOM_ORVALUE);
639 	WRITE_ONCE(*args->pmdp, pmd);
640 	pmd_clear(args->pmdp);
641 	pmd = READ_ONCE(*args->pmdp);
642 	WARN_ON(!pmd_none(pmd));
643 }
644 
645 static void __init pmd_populate_tests(struct pgtable_debug_args *args)
646 {
647 	pmd_t pmd;
648 
649 	pr_debug("Validating PMD populate\n");
650 	/*
651 	 * This entry points to next level page table page.
652 	 * Hence this must not qualify as pmd_bad().
653 	 */
654 	pmd_populate(args->mm, args->pmdp, args->start_ptep);
655 	pmd = READ_ONCE(*args->pmdp);
656 	WARN_ON(pmd_bad(pmd));
657 }
658 
659 static void __init pte_special_tests(struct pgtable_debug_args *args)
660 {
661 	pte_t pte = pfn_pte(args->fixed_pte_pfn, args->page_prot);
662 
663 	if (!IS_ENABLED(CONFIG_ARCH_HAS_PTE_SPECIAL))
664 		return;
665 
666 	pr_debug("Validating PTE special\n");
667 	WARN_ON(!pte_special(pte_mkspecial(pte)));
668 }
669 
670 static void __init pte_protnone_tests(struct pgtable_debug_args *args)
671 {
672 	pte_t pte = pfn_pte(args->fixed_pte_pfn, args->page_prot_none);
673 
674 	if (!IS_ENABLED(CONFIG_NUMA_BALANCING))
675 		return;
676 
677 	pr_debug("Validating PTE protnone\n");
678 	WARN_ON(!pte_protnone(pte));
679 	WARN_ON(!pte_present(pte));
680 }
681 
682 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
683 static void __init pmd_protnone_tests(struct pgtable_debug_args *args)
684 {
685 	pmd_t pmd;
686 
687 	if (!IS_ENABLED(CONFIG_NUMA_BALANCING))
688 		return;
689 
690 	if (!has_transparent_hugepage())
691 		return;
692 
693 	pr_debug("Validating PMD protnone\n");
694 	pmd = pmd_mkhuge(pfn_pmd(args->fixed_pmd_pfn, args->page_prot_none));
695 	WARN_ON(!pmd_protnone(pmd));
696 	WARN_ON(!pmd_present(pmd));
697 }
698 #else  /* !CONFIG_TRANSPARENT_HUGEPAGE */
699 static void __init pmd_protnone_tests(struct pgtable_debug_args *args) { }
700 #endif /* CONFIG_TRANSPARENT_HUGEPAGE */
701 
702 #ifdef CONFIG_ARCH_HAS_PTE_DEVMAP
703 static void __init pte_devmap_tests(struct pgtable_debug_args *args)
704 {
705 	pte_t pte = pfn_pte(args->fixed_pte_pfn, args->page_prot);
706 
707 	pr_debug("Validating PTE devmap\n");
708 	WARN_ON(!pte_devmap(pte_mkdevmap(pte)));
709 }
710 
711 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
712 static void __init pmd_devmap_tests(struct pgtable_debug_args *args)
713 {
714 	pmd_t pmd;
715 
716 	if (!has_transparent_hugepage())
717 		return;
718 
719 	pr_debug("Validating PMD devmap\n");
720 	pmd = pfn_pmd(args->fixed_pmd_pfn, args->page_prot);
721 	WARN_ON(!pmd_devmap(pmd_mkdevmap(pmd)));
722 }
723 
724 #ifdef CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD
725 static void __init pud_devmap_tests(struct pgtable_debug_args *args)
726 {
727 	pud_t pud;
728 
729 	if (!has_transparent_hugepage())
730 		return;
731 
732 	pr_debug("Validating PUD devmap\n");
733 	pud = pfn_pud(args->fixed_pud_pfn, args->page_prot);
734 	WARN_ON(!pud_devmap(pud_mkdevmap(pud)));
735 }
736 #else  /* !CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD */
737 static void __init pud_devmap_tests(struct pgtable_debug_args *args) { }
738 #endif /* CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD */
739 #else  /* CONFIG_TRANSPARENT_HUGEPAGE */
740 static void __init pmd_devmap_tests(struct pgtable_debug_args *args) { }
741 static void __init pud_devmap_tests(struct pgtable_debug_args *args) { }
742 #endif /* CONFIG_TRANSPARENT_HUGEPAGE */
743 #else
744 static void __init pte_devmap_tests(struct pgtable_debug_args *args) { }
745 static void __init pmd_devmap_tests(struct pgtable_debug_args *args) { }
746 static void __init pud_devmap_tests(struct pgtable_debug_args *args) { }
747 #endif /* CONFIG_ARCH_HAS_PTE_DEVMAP */
748 
749 static void __init pte_soft_dirty_tests(struct pgtable_debug_args *args)
750 {
751 	pte_t pte = pfn_pte(args->fixed_pte_pfn, args->page_prot);
752 
753 	if (!IS_ENABLED(CONFIG_MEM_SOFT_DIRTY))
754 		return;
755 
756 	pr_debug("Validating PTE soft dirty\n");
757 	WARN_ON(!pte_soft_dirty(pte_mksoft_dirty(pte)));
758 	WARN_ON(pte_soft_dirty(pte_clear_soft_dirty(pte)));
759 }
760 
761 static void __init pte_swap_soft_dirty_tests(struct pgtable_debug_args *args)
762 {
763 	pte_t pte = pfn_pte(args->fixed_pte_pfn, args->page_prot);
764 
765 	if (!IS_ENABLED(CONFIG_MEM_SOFT_DIRTY))
766 		return;
767 
768 	pr_debug("Validating PTE swap soft dirty\n");
769 	WARN_ON(!pte_swp_soft_dirty(pte_swp_mksoft_dirty(pte)));
770 	WARN_ON(pte_swp_soft_dirty(pte_swp_clear_soft_dirty(pte)));
771 }
772 
773 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
774 static void __init pmd_soft_dirty_tests(struct pgtable_debug_args *args)
775 {
776 	pmd_t pmd;
777 
778 	if (!IS_ENABLED(CONFIG_MEM_SOFT_DIRTY))
779 		return;
780 
781 	if (!has_transparent_hugepage())
782 		return;
783 
784 	pr_debug("Validating PMD soft dirty\n");
785 	pmd = pfn_pmd(args->fixed_pmd_pfn, args->page_prot);
786 	WARN_ON(!pmd_soft_dirty(pmd_mksoft_dirty(pmd)));
787 	WARN_ON(pmd_soft_dirty(pmd_clear_soft_dirty(pmd)));
788 }
789 
790 static void __init pmd_swap_soft_dirty_tests(struct pgtable_debug_args *args)
791 {
792 	pmd_t pmd;
793 
794 	if (!IS_ENABLED(CONFIG_MEM_SOFT_DIRTY) ||
795 		!IS_ENABLED(CONFIG_ARCH_ENABLE_THP_MIGRATION))
796 		return;
797 
798 	if (!has_transparent_hugepage())
799 		return;
800 
801 	pr_debug("Validating PMD swap soft dirty\n");
802 	pmd = pfn_pmd(args->fixed_pmd_pfn, args->page_prot);
803 	WARN_ON(!pmd_swp_soft_dirty(pmd_swp_mksoft_dirty(pmd)));
804 	WARN_ON(pmd_swp_soft_dirty(pmd_swp_clear_soft_dirty(pmd)));
805 }
806 #else  /* !CONFIG_TRANSPARENT_HUGEPAGE */
807 static void __init pmd_soft_dirty_tests(struct pgtable_debug_args *args) { }
808 static void __init pmd_swap_soft_dirty_tests(struct pgtable_debug_args *args) { }
809 #endif /* CONFIG_TRANSPARENT_HUGEPAGE */
810 
811 static void __init pte_swap_exclusive_tests(struct pgtable_debug_args *args)
812 {
813 	unsigned long max_swap_offset;
814 	swp_entry_t entry, entry2;
815 	pte_t pte;
816 
817 	pr_debug("Validating PTE swap exclusive\n");
818 
819 	/* See generic_max_swapfile_size(): probe the maximum offset */
820 	max_swap_offset = swp_offset(pte_to_swp_entry(swp_entry_to_pte(swp_entry(0, ~0UL))));
821 
822 	/* Create a swp entry with all possible bits set */
823 	entry = swp_entry((1 << MAX_SWAPFILES_SHIFT) - 1, max_swap_offset);
824 
825 	pte = swp_entry_to_pte(entry);
826 	WARN_ON(pte_swp_exclusive(pte));
827 	WARN_ON(!is_swap_pte(pte));
828 	entry2 = pte_to_swp_entry(pte);
829 	WARN_ON(memcmp(&entry, &entry2, sizeof(entry)));
830 
831 	pte = pte_swp_mkexclusive(pte);
832 	WARN_ON(!pte_swp_exclusive(pte));
833 	WARN_ON(!is_swap_pte(pte));
834 	WARN_ON(pte_swp_soft_dirty(pte));
835 	entry2 = pte_to_swp_entry(pte);
836 	WARN_ON(memcmp(&entry, &entry2, sizeof(entry)));
837 
838 	pte = pte_swp_clear_exclusive(pte);
839 	WARN_ON(pte_swp_exclusive(pte));
840 	WARN_ON(!is_swap_pte(pte));
841 	entry2 = pte_to_swp_entry(pte);
842 	WARN_ON(memcmp(&entry, &entry2, sizeof(entry)));
843 }
844 
845 static void __init pte_swap_tests(struct pgtable_debug_args *args)
846 {
847 	swp_entry_t swp;
848 	pte_t pte;
849 
850 	pr_debug("Validating PTE swap\n");
851 	pte = pfn_pte(args->fixed_pte_pfn, args->page_prot);
852 	swp = __pte_to_swp_entry(pte);
853 	pte = __swp_entry_to_pte(swp);
854 	WARN_ON(args->fixed_pte_pfn != pte_pfn(pte));
855 }
856 
857 #ifdef CONFIG_ARCH_ENABLE_THP_MIGRATION
858 static void __init pmd_swap_tests(struct pgtable_debug_args *args)
859 {
860 	swp_entry_t swp;
861 	pmd_t pmd;
862 
863 	if (!has_transparent_hugepage())
864 		return;
865 
866 	pr_debug("Validating PMD swap\n");
867 	pmd = pfn_pmd(args->fixed_pmd_pfn, args->page_prot);
868 	swp = __pmd_to_swp_entry(pmd);
869 	pmd = __swp_entry_to_pmd(swp);
870 	WARN_ON(args->fixed_pmd_pfn != pmd_pfn(pmd));
871 }
872 #else  /* !CONFIG_ARCH_ENABLE_THP_MIGRATION */
873 static void __init pmd_swap_tests(struct pgtable_debug_args *args) { }
874 #endif /* CONFIG_ARCH_ENABLE_THP_MIGRATION */
875 
876 static void __init swap_migration_tests(struct pgtable_debug_args *args)
877 {
878 	struct page *page;
879 	swp_entry_t swp;
880 
881 	if (!IS_ENABLED(CONFIG_MIGRATION))
882 		return;
883 
884 	/*
885 	 * swap_migration_tests() requires a dedicated page as it needs to
886 	 * be locked before creating a migration entry from it. Locking the
887 	 * page that actually maps kernel text ('start_kernel') can be real
888 	 * problematic. Lets use the allocated page explicitly for this
889 	 * purpose.
890 	 */
891 	page = (args->pte_pfn != ULONG_MAX) ? pfn_to_page(args->pte_pfn) : NULL;
892 	if (!page)
893 		return;
894 
895 	pr_debug("Validating swap migration\n");
896 
897 	/*
898 	 * make_[readable|writable]_migration_entry() expects given page to
899 	 * be locked, otherwise it stumbles upon a BUG_ON().
900 	 */
901 	__SetPageLocked(page);
902 	swp = make_writable_migration_entry(page_to_pfn(page));
903 	WARN_ON(!is_migration_entry(swp));
904 	WARN_ON(!is_writable_migration_entry(swp));
905 
906 	swp = make_readable_migration_entry(swp_offset(swp));
907 	WARN_ON(!is_migration_entry(swp));
908 	WARN_ON(is_writable_migration_entry(swp));
909 
910 	swp = make_readable_migration_entry(page_to_pfn(page));
911 	WARN_ON(!is_migration_entry(swp));
912 	WARN_ON(is_writable_migration_entry(swp));
913 	__ClearPageLocked(page);
914 }
915 
916 #ifdef CONFIG_HUGETLB_PAGE
917 static void __init hugetlb_basic_tests(struct pgtable_debug_args *args)
918 {
919 	struct page *page;
920 	pte_t pte;
921 
922 	pr_debug("Validating HugeTLB basic\n");
923 	/*
924 	 * Accessing the page associated with the pfn is safe here,
925 	 * as it was previously derived from a real kernel symbol.
926 	 */
927 	page = pfn_to_page(args->fixed_pmd_pfn);
928 	pte = mk_huge_pte(page, args->page_prot);
929 
930 	WARN_ON(!huge_pte_dirty(huge_pte_mkdirty(pte)));
931 	WARN_ON(!huge_pte_write(huge_pte_mkwrite(huge_pte_wrprotect(pte))));
932 	WARN_ON(huge_pte_write(huge_pte_wrprotect(huge_pte_mkwrite(pte))));
933 
934 #ifdef CONFIG_ARCH_WANT_GENERAL_HUGETLB
935 	pte = pfn_pte(args->fixed_pmd_pfn, args->page_prot);
936 
937 	WARN_ON(!pte_huge(pte_mkhuge(pte)));
938 #endif /* CONFIG_ARCH_WANT_GENERAL_HUGETLB */
939 }
940 #else  /* !CONFIG_HUGETLB_PAGE */
941 static void __init hugetlb_basic_tests(struct pgtable_debug_args *args) { }
942 #endif /* CONFIG_HUGETLB_PAGE */
943 
944 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
945 static void __init pmd_thp_tests(struct pgtable_debug_args *args)
946 {
947 	pmd_t pmd;
948 
949 	if (!has_transparent_hugepage())
950 		return;
951 
952 	pr_debug("Validating PMD based THP\n");
953 	/*
954 	 * pmd_trans_huge() and pmd_present() must return positive after
955 	 * MMU invalidation with pmd_mkinvalid(). This behavior is an
956 	 * optimization for transparent huge page. pmd_trans_huge() must
957 	 * be true if pmd_page() returns a valid THP to avoid taking the
958 	 * pmd_lock when others walk over non transhuge pmds (i.e. there
959 	 * are no THP allocated). Especially when splitting a THP and
960 	 * removing the present bit from the pmd, pmd_trans_huge() still
961 	 * needs to return true. pmd_present() should be true whenever
962 	 * pmd_trans_huge() returns true.
963 	 */
964 	pmd = pfn_pmd(args->fixed_pmd_pfn, args->page_prot);
965 	WARN_ON(!pmd_trans_huge(pmd_mkhuge(pmd)));
966 
967 #ifndef __HAVE_ARCH_PMDP_INVALIDATE
968 	WARN_ON(!pmd_trans_huge(pmd_mkinvalid(pmd_mkhuge(pmd))));
969 	WARN_ON(!pmd_present(pmd_mkinvalid(pmd_mkhuge(pmd))));
970 #endif /* __HAVE_ARCH_PMDP_INVALIDATE */
971 }
972 
973 #ifdef CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD
974 static void __init pud_thp_tests(struct pgtable_debug_args *args)
975 {
976 	pud_t pud;
977 
978 	if (!has_transparent_hugepage())
979 		return;
980 
981 	pr_debug("Validating PUD based THP\n");
982 	pud = pfn_pud(args->fixed_pud_pfn, args->page_prot);
983 	WARN_ON(!pud_trans_huge(pud_mkhuge(pud)));
984 
985 	/*
986 	 * pud_mkinvalid() has been dropped for now. Enable back
987 	 * these tests when it comes back with a modified pud_present().
988 	 *
989 	 * WARN_ON(!pud_trans_huge(pud_mkinvalid(pud_mkhuge(pud))));
990 	 * WARN_ON(!pud_present(pud_mkinvalid(pud_mkhuge(pud))));
991 	 */
992 }
993 #else  /* !CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD */
994 static void __init pud_thp_tests(struct pgtable_debug_args *args) { }
995 #endif /* CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD */
996 #else  /* !CONFIG_TRANSPARENT_HUGEPAGE */
997 static void __init pmd_thp_tests(struct pgtable_debug_args *args) { }
998 static void __init pud_thp_tests(struct pgtable_debug_args *args) { }
999 #endif /* CONFIG_TRANSPARENT_HUGEPAGE */
1000 
1001 static unsigned long __init get_random_vaddr(void)
1002 {
1003 	unsigned long random_vaddr, random_pages, total_user_pages;
1004 
1005 	total_user_pages = (TASK_SIZE - FIRST_USER_ADDRESS) / PAGE_SIZE;
1006 
1007 	random_pages = get_random_long() % total_user_pages;
1008 	random_vaddr = FIRST_USER_ADDRESS + random_pages * PAGE_SIZE;
1009 
1010 	return random_vaddr;
1011 }
1012 
1013 static void __init destroy_args(struct pgtable_debug_args *args)
1014 {
1015 	struct page *page = NULL;
1016 
1017 	/* Free (huge) page */
1018 	if (IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE) &&
1019 	    IS_ENABLED(CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD) &&
1020 	    has_transparent_hugepage() &&
1021 	    args->pud_pfn != ULONG_MAX) {
1022 		if (args->is_contiguous_page) {
1023 			free_contig_range(args->pud_pfn,
1024 					  (1 << (HPAGE_PUD_SHIFT - PAGE_SHIFT)));
1025 		} else {
1026 			page = pfn_to_page(args->pud_pfn);
1027 			__free_pages(page, HPAGE_PUD_SHIFT - PAGE_SHIFT);
1028 		}
1029 
1030 		args->pud_pfn = ULONG_MAX;
1031 		args->pmd_pfn = ULONG_MAX;
1032 		args->pte_pfn = ULONG_MAX;
1033 	}
1034 
1035 	if (IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE) &&
1036 	    has_transparent_hugepage() &&
1037 	    args->pmd_pfn != ULONG_MAX) {
1038 		if (args->is_contiguous_page) {
1039 			free_contig_range(args->pmd_pfn, (1 << HPAGE_PMD_ORDER));
1040 		} else {
1041 			page = pfn_to_page(args->pmd_pfn);
1042 			__free_pages(page, HPAGE_PMD_ORDER);
1043 		}
1044 
1045 		args->pmd_pfn = ULONG_MAX;
1046 		args->pte_pfn = ULONG_MAX;
1047 	}
1048 
1049 	if (args->pte_pfn != ULONG_MAX) {
1050 		page = pfn_to_page(args->pte_pfn);
1051 		__free_pages(page, 0);
1052 
1053 		args->pte_pfn = ULONG_MAX;
1054 	}
1055 
1056 	/* Free page table entries */
1057 	if (args->start_ptep) {
1058 		pte_free(args->mm, args->start_ptep);
1059 		mm_dec_nr_ptes(args->mm);
1060 	}
1061 
1062 	if (args->start_pmdp) {
1063 		pmd_free(args->mm, args->start_pmdp);
1064 		mm_dec_nr_pmds(args->mm);
1065 	}
1066 
1067 	if (args->start_pudp) {
1068 		pud_free(args->mm, args->start_pudp);
1069 		mm_dec_nr_puds(args->mm);
1070 	}
1071 
1072 	if (args->start_p4dp)
1073 		p4d_free(args->mm, args->start_p4dp);
1074 
1075 	/* Free vma and mm struct */
1076 	if (args->vma)
1077 		vm_area_free(args->vma);
1078 
1079 	if (args->mm)
1080 		mmdrop(args->mm);
1081 }
1082 
1083 static struct page * __init
1084 debug_vm_pgtable_alloc_huge_page(struct pgtable_debug_args *args, int order)
1085 {
1086 	struct page *page = NULL;
1087 
1088 #ifdef CONFIG_CONTIG_ALLOC
1089 	if (order >= MAX_ORDER) {
1090 		page = alloc_contig_pages((1 << order), GFP_KERNEL,
1091 					  first_online_node, NULL);
1092 		if (page) {
1093 			args->is_contiguous_page = true;
1094 			return page;
1095 		}
1096 	}
1097 #endif
1098 
1099 	if (order < MAX_ORDER)
1100 		page = alloc_pages(GFP_KERNEL, order);
1101 
1102 	return page;
1103 }
1104 
1105 /*
1106  * Check if a physical memory range described by <pstart, pend> contains
1107  * an area that is of size psize, and aligned to psize.
1108  *
1109  * Don't use address 0, an all-zeroes physical address might mask bugs, and
1110  * it's not used on x86.
1111  */
1112 static void  __init phys_align_check(phys_addr_t pstart,
1113 				     phys_addr_t pend, unsigned long psize,
1114 				     phys_addr_t *physp, unsigned long *alignp)
1115 {
1116 	phys_addr_t aligned_start, aligned_end;
1117 
1118 	if (pstart == 0)
1119 		pstart = PAGE_SIZE;
1120 
1121 	aligned_start = ALIGN(pstart, psize);
1122 	aligned_end = aligned_start + psize;
1123 
1124 	if (aligned_end > aligned_start && aligned_end <= pend) {
1125 		*alignp = psize;
1126 		*physp = aligned_start;
1127 	}
1128 }
1129 
1130 static void __init init_fixed_pfns(struct pgtable_debug_args *args)
1131 {
1132 	u64 idx;
1133 	phys_addr_t phys, pstart, pend;
1134 
1135 	/*
1136 	 * Initialize the fixed pfns. To do this, try to find a
1137 	 * valid physical range, preferably aligned to PUD_SIZE,
1138 	 * but settling for aligned to PMD_SIZE as a fallback. If
1139 	 * neither of those is found, use the physical address of
1140 	 * the start_kernel symbol.
1141 	 *
1142 	 * The memory doesn't need to be allocated, it just needs to exist
1143 	 * as usable memory. It won't be touched.
1144 	 *
1145 	 * The alignment is recorded, and can be checked to see if we
1146 	 * can run the tests that require an actual valid physical
1147 	 * address range on some architectures ({pmd,pud}_huge_test
1148 	 * on x86).
1149 	 */
1150 
1151 	phys = __pa_symbol(&start_kernel);
1152 	args->fixed_alignment = PAGE_SIZE;
1153 
1154 	for_each_mem_range(idx, &pstart, &pend) {
1155 		/* First check for a PUD-aligned area */
1156 		phys_align_check(pstart, pend, PUD_SIZE, &phys,
1157 				 &args->fixed_alignment);
1158 
1159 		/* If a PUD-aligned area is found, we're done */
1160 		if (args->fixed_alignment == PUD_SIZE)
1161 			break;
1162 
1163 		/*
1164 		 * If no PMD-aligned area found yet, check for one,
1165 		 * but continue the loop to look for a PUD-aligned area.
1166 		 */
1167 		if (args->fixed_alignment < PMD_SIZE)
1168 			phys_align_check(pstart, pend, PMD_SIZE, &phys,
1169 					 &args->fixed_alignment);
1170 	}
1171 
1172 	args->fixed_pgd_pfn = __phys_to_pfn(phys & PGDIR_MASK);
1173 	args->fixed_p4d_pfn = __phys_to_pfn(phys & P4D_MASK);
1174 	args->fixed_pud_pfn = __phys_to_pfn(phys & PUD_MASK);
1175 	args->fixed_pmd_pfn = __phys_to_pfn(phys & PMD_MASK);
1176 	args->fixed_pte_pfn = __phys_to_pfn(phys & PAGE_MASK);
1177 	WARN_ON(!pfn_valid(args->fixed_pte_pfn));
1178 }
1179 
1180 
1181 static int __init init_args(struct pgtable_debug_args *args)
1182 {
1183 	struct page *page = NULL;
1184 	int ret = 0;
1185 
1186 	/*
1187 	 * Initialize the debugging data.
1188 	 *
1189 	 * vm_get_page_prot(VM_NONE) or vm_get_page_prot(VM_SHARED|VM_NONE)
1190 	 * will help create page table entries with PROT_NONE permission as
1191 	 * required for pxx_protnone_tests().
1192 	 */
1193 	memset(args, 0, sizeof(*args));
1194 	args->vaddr              = get_random_vaddr();
1195 	args->page_prot          = vm_get_page_prot(VM_ACCESS_FLAGS);
1196 	args->page_prot_none     = vm_get_page_prot(VM_NONE);
1197 	args->is_contiguous_page = false;
1198 	args->pud_pfn            = ULONG_MAX;
1199 	args->pmd_pfn            = ULONG_MAX;
1200 	args->pte_pfn            = ULONG_MAX;
1201 	args->fixed_pgd_pfn      = ULONG_MAX;
1202 	args->fixed_p4d_pfn      = ULONG_MAX;
1203 	args->fixed_pud_pfn      = ULONG_MAX;
1204 	args->fixed_pmd_pfn      = ULONG_MAX;
1205 	args->fixed_pte_pfn      = ULONG_MAX;
1206 
1207 	/* Allocate mm and vma */
1208 	args->mm = mm_alloc();
1209 	if (!args->mm) {
1210 		pr_err("Failed to allocate mm struct\n");
1211 		ret = -ENOMEM;
1212 		goto error;
1213 	}
1214 
1215 	args->vma = vm_area_alloc(args->mm);
1216 	if (!args->vma) {
1217 		pr_err("Failed to allocate vma\n");
1218 		ret = -ENOMEM;
1219 		goto error;
1220 	}
1221 
1222 	/*
1223 	 * Allocate page table entries. They will be modified in the tests.
1224 	 * Lets save the page table entries so that they can be released
1225 	 * when the tests are completed.
1226 	 */
1227 	args->pgdp = pgd_offset(args->mm, args->vaddr);
1228 	args->p4dp = p4d_alloc(args->mm, args->pgdp, args->vaddr);
1229 	if (!args->p4dp) {
1230 		pr_err("Failed to allocate p4d entries\n");
1231 		ret = -ENOMEM;
1232 		goto error;
1233 	}
1234 	args->start_p4dp = p4d_offset(args->pgdp, 0UL);
1235 	WARN_ON(!args->start_p4dp);
1236 
1237 	args->pudp = pud_alloc(args->mm, args->p4dp, args->vaddr);
1238 	if (!args->pudp) {
1239 		pr_err("Failed to allocate pud entries\n");
1240 		ret = -ENOMEM;
1241 		goto error;
1242 	}
1243 	args->start_pudp = pud_offset(args->p4dp, 0UL);
1244 	WARN_ON(!args->start_pudp);
1245 
1246 	args->pmdp = pmd_alloc(args->mm, args->pudp, args->vaddr);
1247 	if (!args->pmdp) {
1248 		pr_err("Failed to allocate pmd entries\n");
1249 		ret = -ENOMEM;
1250 		goto error;
1251 	}
1252 	args->start_pmdp = pmd_offset(args->pudp, 0UL);
1253 	WARN_ON(!args->start_pmdp);
1254 
1255 	if (pte_alloc(args->mm, args->pmdp)) {
1256 		pr_err("Failed to allocate pte entries\n");
1257 		ret = -ENOMEM;
1258 		goto error;
1259 	}
1260 	args->start_ptep = pmd_pgtable(READ_ONCE(*args->pmdp));
1261 	WARN_ON(!args->start_ptep);
1262 
1263 	init_fixed_pfns(args);
1264 
1265 	/*
1266 	 * Allocate (huge) pages because some of the tests need to access
1267 	 * the data in the pages. The corresponding tests will be skipped
1268 	 * if we fail to allocate (huge) pages.
1269 	 */
1270 	if (IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE) &&
1271 	    IS_ENABLED(CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD) &&
1272 	    has_transparent_hugepage()) {
1273 		page = debug_vm_pgtable_alloc_huge_page(args,
1274 				HPAGE_PUD_SHIFT - PAGE_SHIFT);
1275 		if (page) {
1276 			args->pud_pfn = page_to_pfn(page);
1277 			args->pmd_pfn = args->pud_pfn;
1278 			args->pte_pfn = args->pud_pfn;
1279 			return 0;
1280 		}
1281 	}
1282 
1283 	if (IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE) &&
1284 	    has_transparent_hugepage()) {
1285 		page = debug_vm_pgtable_alloc_huge_page(args, HPAGE_PMD_ORDER);
1286 		if (page) {
1287 			args->pmd_pfn = page_to_pfn(page);
1288 			args->pte_pfn = args->pmd_pfn;
1289 			return 0;
1290 		}
1291 	}
1292 
1293 	page = alloc_pages(GFP_KERNEL, 0);
1294 	if (page)
1295 		args->pte_pfn = page_to_pfn(page);
1296 
1297 	return 0;
1298 
1299 error:
1300 	destroy_args(args);
1301 	return ret;
1302 }
1303 
1304 static int __init debug_vm_pgtable(void)
1305 {
1306 	struct pgtable_debug_args args;
1307 	spinlock_t *ptl = NULL;
1308 	int idx, ret;
1309 
1310 	pr_info("Validating architecture page table helpers\n");
1311 	ret = init_args(&args);
1312 	if (ret)
1313 		return ret;
1314 
1315 	/*
1316 	 * Iterate over each possible vm_flags to make sure that all
1317 	 * the basic page table transformation validations just hold
1318 	 * true irrespective of the starting protection value for a
1319 	 * given page table entry.
1320 	 *
1321 	 * Protection based vm_flags combinatins are always linear
1322 	 * and increasing i.e starting from VM_NONE and going upto
1323 	 * (VM_SHARED | READ | WRITE | EXEC).
1324 	 */
1325 #define VM_FLAGS_START	(VM_NONE)
1326 #define VM_FLAGS_END	(VM_SHARED | VM_EXEC | VM_WRITE | VM_READ)
1327 
1328 	for (idx = VM_FLAGS_START; idx <= VM_FLAGS_END; idx++) {
1329 		pte_basic_tests(&args, idx);
1330 		pmd_basic_tests(&args, idx);
1331 		pud_basic_tests(&args, idx);
1332 	}
1333 
1334 	/*
1335 	 * Both P4D and PGD level tests are very basic which do not
1336 	 * involve creating page table entries from the protection
1337 	 * value and the given pfn. Hence just keep them out from
1338 	 * the above iteration for now to save some test execution
1339 	 * time.
1340 	 */
1341 	p4d_basic_tests(&args);
1342 	pgd_basic_tests(&args);
1343 
1344 	pmd_leaf_tests(&args);
1345 	pud_leaf_tests(&args);
1346 
1347 	pte_special_tests(&args);
1348 	pte_protnone_tests(&args);
1349 	pmd_protnone_tests(&args);
1350 
1351 	pte_devmap_tests(&args);
1352 	pmd_devmap_tests(&args);
1353 	pud_devmap_tests(&args);
1354 
1355 	pte_soft_dirty_tests(&args);
1356 	pmd_soft_dirty_tests(&args);
1357 	pte_swap_soft_dirty_tests(&args);
1358 	pmd_swap_soft_dirty_tests(&args);
1359 
1360 	pte_swap_exclusive_tests(&args);
1361 
1362 	pte_swap_tests(&args);
1363 	pmd_swap_tests(&args);
1364 
1365 	swap_migration_tests(&args);
1366 
1367 	pmd_thp_tests(&args);
1368 	pud_thp_tests(&args);
1369 
1370 	hugetlb_basic_tests(&args);
1371 
1372 	/*
1373 	 * Page table modifying tests. They need to hold
1374 	 * proper page table lock.
1375 	 */
1376 
1377 	args.ptep = pte_offset_map_lock(args.mm, args.pmdp, args.vaddr, &ptl);
1378 	pte_clear_tests(&args);
1379 	pte_advanced_tests(&args);
1380 	pte_unmap_unlock(args.ptep, ptl);
1381 
1382 	ptl = pmd_lock(args.mm, args.pmdp);
1383 	pmd_clear_tests(&args);
1384 	pmd_advanced_tests(&args);
1385 	pmd_huge_tests(&args);
1386 	pmd_populate_tests(&args);
1387 	spin_unlock(ptl);
1388 
1389 	ptl = pud_lock(args.mm, args.pudp);
1390 	pud_clear_tests(&args);
1391 	pud_advanced_tests(&args);
1392 	pud_huge_tests(&args);
1393 	pud_populate_tests(&args);
1394 	spin_unlock(ptl);
1395 
1396 	spin_lock(&(args.mm->page_table_lock));
1397 	p4d_clear_tests(&args);
1398 	pgd_clear_tests(&args);
1399 	p4d_populate_tests(&args);
1400 	pgd_populate_tests(&args);
1401 	spin_unlock(&(args.mm->page_table_lock));
1402 
1403 	destroy_args(&args);
1404 	return 0;
1405 }
1406 late_initcall(debug_vm_pgtable);
1407