1 // SPDX-License-Identifier: GPL-2.0-only
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.
7 * Copyright (C) 2019 ARM Ltd.
11 #define pr_fmt(fmt) "debug_vm_pgtable: [%-25s]: " fmt, __func__
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>
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>
33 #include <linux/vmalloc.h>
35 #include <asm/cacheflush.h>
36 #include <asm/pgalloc.h>
37 #include <asm/tlbflush.h>
40 * Please refer Documentation/mm/arch_pgtable_helpers.rst for the semantics
41 * expectations that are being validated here. All future changes in here
42 * or the documentation need to be in sync.
44 * On s390 platform, the lower 4 bits are used to identify given page table
45 * entry type. But these bits might affect the ability to clear entries with
46 * pxx_clear() because of how dynamic page table folding works on s390. So
47 * while loading up the entries do not change the lower 4 bits. It does not
48 * have affect any other platform. Also avoid the 62nd bit on ppc64 that is
49 * used to mark a pte entry.
51 #define S390_SKIP_MASK GENMASK(3, 0)
52 #if __BITS_PER_LONG == 64
53 #define PPC64_SKIP_MASK GENMASK(62, 62)
55 #define PPC64_SKIP_MASK 0x0
57 #define ARCH_SKIP_MASK (S390_SKIP_MASK | PPC64_SKIP_MASK)
58 #define RANDOM_ORVALUE (GENMASK(BITS_PER_LONG - 1, 0) & ~ARCH_SKIP_MASK)
59 #define RANDOM_NZVALUE GENMASK(7, 0)
61 struct pgtable_debug_args {
63 struct vm_area_struct *vma;
78 pgprot_t page_prot_none;
80 bool is_contiguous_page;
81 unsigned long pud_pfn;
82 unsigned long pmd_pfn;
83 unsigned long pte_pfn;
85 unsigned long fixed_alignment;
86 unsigned long fixed_pgd_pfn;
87 unsigned long fixed_p4d_pfn;
88 unsigned long fixed_pud_pfn;
89 unsigned long fixed_pmd_pfn;
90 unsigned long fixed_pte_pfn;
93 static void __init pte_basic_tests(struct pgtable_debug_args *args, int idx)
95 pgprot_t prot = vm_get_page_prot(idx);
96 pte_t pte = pfn_pte(args->fixed_pte_pfn, prot);
97 unsigned long val = idx, *ptr = &val;
99 pr_debug("Validating PTE basic (%pGv)\n", ptr);
102 * This test needs to be executed after the given page table entry
103 * is created with pfn_pte() to make sure that vm_get_page_prot(idx)
104 * does not have the dirty bit enabled from the beginning. This is
105 * important for platforms like arm64 where (!PTE_RDONLY) indicate
106 * dirty bit being set.
108 WARN_ON(pte_dirty(pte_wrprotect(pte)));
110 WARN_ON(!pte_same(pte, pte));
111 WARN_ON(!pte_young(pte_mkyoung(pte_mkold(pte))));
112 WARN_ON(!pte_dirty(pte_mkdirty(pte_mkclean(pte))));
113 WARN_ON(!pte_write(pte_mkwrite(pte_wrprotect(pte), args->vma)));
114 WARN_ON(pte_young(pte_mkold(pte_mkyoung(pte))));
115 WARN_ON(pte_dirty(pte_mkclean(pte_mkdirty(pte))));
116 WARN_ON(pte_write(pte_wrprotect(pte_mkwrite(pte, args->vma))));
117 WARN_ON(pte_dirty(pte_wrprotect(pte_mkclean(pte))));
118 WARN_ON(!pte_dirty(pte_wrprotect(pte_mkdirty(pte))));
121 static void __init pte_advanced_tests(struct pgtable_debug_args *args)
127 * Architectures optimize set_pte_at by avoiding TLB flush.
128 * This requires set_pte_at to be not used to update an
129 * existing pte entry. Clear pte before we do set_pte_at
131 * flush_dcache_page() is called after set_pte_at() to clear
132 * PG_arch_1 for the page on ARM64. The page flag isn't cleared
133 * when it's released and page allocation check will fail when
134 * the page is allocated again. For architectures other than ARM64,
135 * the unexpected overhead of cache flushing is acceptable.
137 page = (args->pte_pfn != ULONG_MAX) ? pfn_to_page(args->pte_pfn) : NULL;
141 pr_debug("Validating PTE advanced\n");
142 if (WARN_ON(!args->ptep))
145 pte = pfn_pte(args->pte_pfn, args->page_prot);
146 set_pte_at(args->mm, args->vaddr, args->ptep, pte);
147 flush_dcache_page(page);
148 ptep_set_wrprotect(args->mm, args->vaddr, args->ptep);
149 pte = ptep_get(args->ptep);
150 WARN_ON(pte_write(pte));
151 ptep_get_and_clear(args->mm, args->vaddr, args->ptep);
152 pte = ptep_get(args->ptep);
153 WARN_ON(!pte_none(pte));
155 pte = pfn_pte(args->pte_pfn, args->page_prot);
156 pte = pte_wrprotect(pte);
157 pte = pte_mkclean(pte);
158 set_pte_at(args->mm, args->vaddr, args->ptep, pte);
159 flush_dcache_page(page);
160 pte = pte_mkwrite(pte, args->vma);
161 pte = pte_mkdirty(pte);
162 ptep_set_access_flags(args->vma, args->vaddr, args->ptep, pte, 1);
163 pte = ptep_get(args->ptep);
164 WARN_ON(!(pte_write(pte) && pte_dirty(pte)));
165 ptep_get_and_clear_full(args->mm, args->vaddr, args->ptep, 1);
166 pte = ptep_get(args->ptep);
167 WARN_ON(!pte_none(pte));
169 pte = pfn_pte(args->pte_pfn, args->page_prot);
170 pte = pte_mkyoung(pte);
171 set_pte_at(args->mm, args->vaddr, args->ptep, pte);
172 flush_dcache_page(page);
173 ptep_test_and_clear_young(args->vma, args->vaddr, args->ptep);
174 pte = ptep_get(args->ptep);
175 WARN_ON(pte_young(pte));
177 ptep_get_and_clear_full(args->mm, args->vaddr, args->ptep, 1);
180 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
181 static void __init pmd_basic_tests(struct pgtable_debug_args *args, int idx)
183 pgprot_t prot = vm_get_page_prot(idx);
184 unsigned long val = idx, *ptr = &val;
187 if (!has_transparent_hugepage())
190 pr_debug("Validating PMD basic (%pGv)\n", ptr);
191 pmd = pfn_pmd(args->fixed_pmd_pfn, prot);
194 * This test needs to be executed after the given page table entry
195 * is created with pfn_pmd() to make sure that vm_get_page_prot(idx)
196 * does not have the dirty bit enabled from the beginning. This is
197 * important for platforms like arm64 where (!PTE_RDONLY) indicate
198 * dirty bit being set.
200 WARN_ON(pmd_dirty(pmd_wrprotect(pmd)));
203 WARN_ON(!pmd_same(pmd, pmd));
204 WARN_ON(!pmd_young(pmd_mkyoung(pmd_mkold(pmd))));
205 WARN_ON(!pmd_dirty(pmd_mkdirty(pmd_mkclean(pmd))));
206 WARN_ON(!pmd_write(pmd_mkwrite(pmd_wrprotect(pmd), args->vma)));
207 WARN_ON(pmd_young(pmd_mkold(pmd_mkyoung(pmd))));
208 WARN_ON(pmd_dirty(pmd_mkclean(pmd_mkdirty(pmd))));
209 WARN_ON(pmd_write(pmd_wrprotect(pmd_mkwrite(pmd, args->vma))));
210 WARN_ON(pmd_dirty(pmd_wrprotect(pmd_mkclean(pmd))));
211 WARN_ON(!pmd_dirty(pmd_wrprotect(pmd_mkdirty(pmd))));
213 * A huge page does not point to next level page table
214 * entry. Hence this must qualify as pmd_bad().
216 WARN_ON(!pmd_bad(pmd_mkhuge(pmd)));
219 static void __init pmd_advanced_tests(struct pgtable_debug_args *args)
223 unsigned long vaddr = args->vaddr;
225 if (!has_transparent_hugepage())
228 page = (args->pmd_pfn != ULONG_MAX) ? pfn_to_page(args->pmd_pfn) : NULL;
233 * flush_dcache_page() is called after set_pmd_at() to clear
234 * PG_arch_1 for the page on ARM64. The page flag isn't cleared
235 * when it's released and page allocation check will fail when
236 * the page is allocated again. For architectures other than ARM64,
237 * the unexpected overhead of cache flushing is acceptable.
239 pr_debug("Validating PMD advanced\n");
240 /* Align the address wrt HPAGE_PMD_SIZE */
241 vaddr &= HPAGE_PMD_MASK;
243 pgtable_trans_huge_deposit(args->mm, args->pmdp, args->start_ptep);
245 pmd = pfn_pmd(args->pmd_pfn, args->page_prot);
246 set_pmd_at(args->mm, vaddr, args->pmdp, pmd);
247 flush_dcache_page(page);
248 pmdp_set_wrprotect(args->mm, vaddr, args->pmdp);
249 pmd = READ_ONCE(*args->pmdp);
250 WARN_ON(pmd_write(pmd));
251 pmdp_huge_get_and_clear(args->mm, vaddr, args->pmdp);
252 pmd = READ_ONCE(*args->pmdp);
253 WARN_ON(!pmd_none(pmd));
255 pmd = pfn_pmd(args->pmd_pfn, args->page_prot);
256 pmd = pmd_wrprotect(pmd);
257 pmd = pmd_mkclean(pmd);
258 set_pmd_at(args->mm, vaddr, args->pmdp, pmd);
259 flush_dcache_page(page);
260 pmd = pmd_mkwrite(pmd, args->vma);
261 pmd = pmd_mkdirty(pmd);
262 pmdp_set_access_flags(args->vma, vaddr, args->pmdp, pmd, 1);
263 pmd = READ_ONCE(*args->pmdp);
264 WARN_ON(!(pmd_write(pmd) && pmd_dirty(pmd)));
265 pmdp_huge_get_and_clear_full(args->vma, vaddr, args->pmdp, 1);
266 pmd = READ_ONCE(*args->pmdp);
267 WARN_ON(!pmd_none(pmd));
269 pmd = pmd_mkhuge(pfn_pmd(args->pmd_pfn, args->page_prot));
270 pmd = pmd_mkyoung(pmd);
271 set_pmd_at(args->mm, vaddr, args->pmdp, pmd);
272 flush_dcache_page(page);
273 pmdp_test_and_clear_young(args->vma, vaddr, args->pmdp);
274 pmd = READ_ONCE(*args->pmdp);
275 WARN_ON(pmd_young(pmd));
277 /* Clear the pte entries */
278 pmdp_huge_get_and_clear(args->mm, vaddr, args->pmdp);
279 pgtable_trans_huge_withdraw(args->mm, args->pmdp);
282 static void __init pmd_leaf_tests(struct pgtable_debug_args *args)
286 if (!has_transparent_hugepage())
289 pr_debug("Validating PMD leaf\n");
290 pmd = pfn_pmd(args->fixed_pmd_pfn, args->page_prot);
293 * PMD based THP is a leaf entry.
295 pmd = pmd_mkhuge(pmd);
296 WARN_ON(!pmd_leaf(pmd));
299 #ifdef CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD
300 static void __init pud_basic_tests(struct pgtable_debug_args *args, int idx)
302 pgprot_t prot = vm_get_page_prot(idx);
303 unsigned long val = idx, *ptr = &val;
306 if (!has_transparent_pud_hugepage())
309 pr_debug("Validating PUD basic (%pGv)\n", ptr);
310 pud = pfn_pud(args->fixed_pud_pfn, prot);
313 * This test needs to be executed after the given page table entry
314 * is created with pfn_pud() to make sure that vm_get_page_prot(idx)
315 * does not have the dirty bit enabled from the beginning. This is
316 * important for platforms like arm64 where (!PTE_RDONLY) indicate
317 * dirty bit being set.
319 WARN_ON(pud_dirty(pud_wrprotect(pud)));
321 WARN_ON(!pud_same(pud, pud));
322 WARN_ON(!pud_young(pud_mkyoung(pud_mkold(pud))));
323 WARN_ON(!pud_dirty(pud_mkdirty(pud_mkclean(pud))));
324 WARN_ON(pud_dirty(pud_mkclean(pud_mkdirty(pud))));
325 WARN_ON(!pud_write(pud_mkwrite(pud_wrprotect(pud))));
326 WARN_ON(pud_write(pud_wrprotect(pud_mkwrite(pud))));
327 WARN_ON(pud_young(pud_mkold(pud_mkyoung(pud))));
328 WARN_ON(pud_dirty(pud_wrprotect(pud_mkclean(pud))));
329 WARN_ON(!pud_dirty(pud_wrprotect(pud_mkdirty(pud))));
331 if (mm_pmd_folded(args->mm))
335 * A huge page does not point to next level page table
336 * entry. Hence this must qualify as pud_bad().
338 WARN_ON(!pud_bad(pud_mkhuge(pud)));
341 static void __init pud_advanced_tests(struct pgtable_debug_args *args)
344 unsigned long vaddr = args->vaddr;
347 if (!has_transparent_pud_hugepage())
350 page = (args->pud_pfn != ULONG_MAX) ? pfn_to_page(args->pud_pfn) : NULL;
355 * flush_dcache_page() is called after set_pud_at() to clear
356 * PG_arch_1 for the page on ARM64. The page flag isn't cleared
357 * when it's released and page allocation check will fail when
358 * the page is allocated again. For architectures other than ARM64,
359 * the unexpected overhead of cache flushing is acceptable.
361 pr_debug("Validating PUD advanced\n");
362 /* Align the address wrt HPAGE_PUD_SIZE */
363 vaddr &= HPAGE_PUD_MASK;
365 pud = pfn_pud(args->pud_pfn, args->page_prot);
367 * Some architectures have debug checks to make sure
368 * huge pud mapping are only found with devmap entries
369 * For now test with only devmap entries.
371 pud = pud_mkdevmap(pud);
372 set_pud_at(args->mm, vaddr, args->pudp, pud);
373 flush_dcache_page(page);
374 pudp_set_wrprotect(args->mm, vaddr, args->pudp);
375 pud = READ_ONCE(*args->pudp);
376 WARN_ON(pud_write(pud));
378 #ifndef __PAGETABLE_PMD_FOLDED
379 pudp_huge_get_and_clear(args->mm, vaddr, args->pudp);
380 pud = READ_ONCE(*args->pudp);
381 WARN_ON(!pud_none(pud));
382 #endif /* __PAGETABLE_PMD_FOLDED */
383 pud = pfn_pud(args->pud_pfn, args->page_prot);
384 pud = pud_mkdevmap(pud);
385 pud = pud_wrprotect(pud);
386 pud = pud_mkclean(pud);
387 set_pud_at(args->mm, vaddr, args->pudp, pud);
388 flush_dcache_page(page);
389 pud = pud_mkwrite(pud);
390 pud = pud_mkdirty(pud);
391 pudp_set_access_flags(args->vma, vaddr, args->pudp, pud, 1);
392 pud = READ_ONCE(*args->pudp);
393 WARN_ON(!(pud_write(pud) && pud_dirty(pud)));
395 #ifndef __PAGETABLE_PMD_FOLDED
396 pudp_huge_get_and_clear_full(args->vma, vaddr, args->pudp, 1);
397 pud = READ_ONCE(*args->pudp);
398 WARN_ON(!pud_none(pud));
399 #endif /* __PAGETABLE_PMD_FOLDED */
401 pud = pfn_pud(args->pud_pfn, args->page_prot);
402 pud = pud_mkdevmap(pud);
403 pud = pud_mkyoung(pud);
404 set_pud_at(args->mm, vaddr, args->pudp, pud);
405 flush_dcache_page(page);
406 pudp_test_and_clear_young(args->vma, vaddr, args->pudp);
407 pud = READ_ONCE(*args->pudp);
408 WARN_ON(pud_young(pud));
410 pudp_huge_get_and_clear(args->mm, vaddr, args->pudp);
413 static void __init pud_leaf_tests(struct pgtable_debug_args *args)
417 if (!has_transparent_pud_hugepage())
420 pr_debug("Validating PUD leaf\n");
421 pud = pfn_pud(args->fixed_pud_pfn, args->page_prot);
423 * PUD based THP is a leaf entry.
425 pud = pud_mkhuge(pud);
426 WARN_ON(!pud_leaf(pud));
428 #else /* !CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD */
429 static void __init pud_basic_tests(struct pgtable_debug_args *args, int idx) { }
430 static void __init pud_advanced_tests(struct pgtable_debug_args *args) { }
431 static void __init pud_leaf_tests(struct pgtable_debug_args *args) { }
432 #endif /* CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD */
433 #else /* !CONFIG_TRANSPARENT_HUGEPAGE */
434 static void __init pmd_basic_tests(struct pgtable_debug_args *args, int idx) { }
435 static void __init pud_basic_tests(struct pgtable_debug_args *args, int idx) { }
436 static void __init pmd_advanced_tests(struct pgtable_debug_args *args) { }
437 static void __init pud_advanced_tests(struct pgtable_debug_args *args) { }
438 static void __init pmd_leaf_tests(struct pgtable_debug_args *args) { }
439 static void __init pud_leaf_tests(struct pgtable_debug_args *args) { }
440 #endif /* CONFIG_TRANSPARENT_HUGEPAGE */
442 #ifdef CONFIG_HAVE_ARCH_HUGE_VMAP
443 static void __init pmd_huge_tests(struct pgtable_debug_args *args)
447 if (!arch_vmap_pmd_supported(args->page_prot) ||
448 args->fixed_alignment < PMD_SIZE)
451 pr_debug("Validating PMD huge\n");
453 * X86 defined pmd_set_huge() verifies that the given
454 * PMD is not a populated non-leaf entry.
456 WRITE_ONCE(*args->pmdp, __pmd(0));
457 WARN_ON(!pmd_set_huge(args->pmdp, __pfn_to_phys(args->fixed_pmd_pfn), args->page_prot));
458 WARN_ON(!pmd_clear_huge(args->pmdp));
459 pmd = READ_ONCE(*args->pmdp);
460 WARN_ON(!pmd_none(pmd));
463 static void __init pud_huge_tests(struct pgtable_debug_args *args)
467 if (!arch_vmap_pud_supported(args->page_prot) ||
468 args->fixed_alignment < PUD_SIZE)
471 pr_debug("Validating PUD huge\n");
473 * X86 defined pud_set_huge() verifies that the given
474 * PUD is not a populated non-leaf entry.
476 WRITE_ONCE(*args->pudp, __pud(0));
477 WARN_ON(!pud_set_huge(args->pudp, __pfn_to_phys(args->fixed_pud_pfn), args->page_prot));
478 WARN_ON(!pud_clear_huge(args->pudp));
479 pud = READ_ONCE(*args->pudp);
480 WARN_ON(!pud_none(pud));
482 #else /* !CONFIG_HAVE_ARCH_HUGE_VMAP */
483 static void __init pmd_huge_tests(struct pgtable_debug_args *args) { }
484 static void __init pud_huge_tests(struct pgtable_debug_args *args) { }
485 #endif /* CONFIG_HAVE_ARCH_HUGE_VMAP */
487 static void __init p4d_basic_tests(struct pgtable_debug_args *args)
491 pr_debug("Validating P4D basic\n");
492 memset(&p4d, RANDOM_NZVALUE, sizeof(p4d_t));
493 WARN_ON(!p4d_same(p4d, p4d));
496 static void __init pgd_basic_tests(struct pgtable_debug_args *args)
500 pr_debug("Validating PGD basic\n");
501 memset(&pgd, RANDOM_NZVALUE, sizeof(pgd_t));
502 WARN_ON(!pgd_same(pgd, pgd));
505 #ifndef __PAGETABLE_PUD_FOLDED
506 static void __init pud_clear_tests(struct pgtable_debug_args *args)
508 pud_t pud = READ_ONCE(*args->pudp);
510 if (mm_pmd_folded(args->mm))
513 pr_debug("Validating PUD clear\n");
514 pud = __pud(pud_val(pud) | RANDOM_ORVALUE);
515 WRITE_ONCE(*args->pudp, pud);
516 pud_clear(args->pudp);
517 pud = READ_ONCE(*args->pudp);
518 WARN_ON(!pud_none(pud));
521 static void __init pud_populate_tests(struct pgtable_debug_args *args)
525 if (mm_pmd_folded(args->mm))
528 pr_debug("Validating PUD populate\n");
530 * This entry points to next level page table page.
531 * Hence this must not qualify as pud_bad().
533 pud_populate(args->mm, args->pudp, args->start_pmdp);
534 pud = READ_ONCE(*args->pudp);
535 WARN_ON(pud_bad(pud));
537 #else /* !__PAGETABLE_PUD_FOLDED */
538 static void __init pud_clear_tests(struct pgtable_debug_args *args) { }
539 static void __init pud_populate_tests(struct pgtable_debug_args *args) { }
540 #endif /* PAGETABLE_PUD_FOLDED */
542 #ifndef __PAGETABLE_P4D_FOLDED
543 static void __init p4d_clear_tests(struct pgtable_debug_args *args)
545 p4d_t p4d = READ_ONCE(*args->p4dp);
547 if (mm_pud_folded(args->mm))
550 pr_debug("Validating P4D clear\n");
551 p4d = __p4d(p4d_val(p4d) | RANDOM_ORVALUE);
552 WRITE_ONCE(*args->p4dp, p4d);
553 p4d_clear(args->p4dp);
554 p4d = READ_ONCE(*args->p4dp);
555 WARN_ON(!p4d_none(p4d));
558 static void __init p4d_populate_tests(struct pgtable_debug_args *args)
562 if (mm_pud_folded(args->mm))
565 pr_debug("Validating P4D populate\n");
567 * This entry points to next level page table page.
568 * Hence this must not qualify as p4d_bad().
570 pud_clear(args->pudp);
571 p4d_clear(args->p4dp);
572 p4d_populate(args->mm, args->p4dp, args->start_pudp);
573 p4d = READ_ONCE(*args->p4dp);
574 WARN_ON(p4d_bad(p4d));
577 static void __init pgd_clear_tests(struct pgtable_debug_args *args)
579 pgd_t pgd = READ_ONCE(*(args->pgdp));
581 if (mm_p4d_folded(args->mm))
584 pr_debug("Validating PGD clear\n");
585 pgd = __pgd(pgd_val(pgd) | RANDOM_ORVALUE);
586 WRITE_ONCE(*args->pgdp, pgd);
587 pgd_clear(args->pgdp);
588 pgd = READ_ONCE(*args->pgdp);
589 WARN_ON(!pgd_none(pgd));
592 static void __init pgd_populate_tests(struct pgtable_debug_args *args)
596 if (mm_p4d_folded(args->mm))
599 pr_debug("Validating PGD populate\n");
601 * This entry points to next level page table page.
602 * Hence this must not qualify as pgd_bad().
604 p4d_clear(args->p4dp);
605 pgd_clear(args->pgdp);
606 pgd_populate(args->mm, args->pgdp, args->start_p4dp);
607 pgd = READ_ONCE(*args->pgdp);
608 WARN_ON(pgd_bad(pgd));
610 #else /* !__PAGETABLE_P4D_FOLDED */
611 static void __init p4d_clear_tests(struct pgtable_debug_args *args) { }
612 static void __init pgd_clear_tests(struct pgtable_debug_args *args) { }
613 static void __init p4d_populate_tests(struct pgtable_debug_args *args) { }
614 static void __init pgd_populate_tests(struct pgtable_debug_args *args) { }
615 #endif /* PAGETABLE_P4D_FOLDED */
617 static void __init pte_clear_tests(struct pgtable_debug_args *args)
620 pte_t pte = pfn_pte(args->pte_pfn, args->page_prot);
622 page = (args->pte_pfn != ULONG_MAX) ? pfn_to_page(args->pte_pfn) : NULL;
627 * flush_dcache_page() is called after set_pte_at() to clear
628 * PG_arch_1 for the page on ARM64. The page flag isn't cleared
629 * when it's released and page allocation check will fail when
630 * the page is allocated again. For architectures other than ARM64,
631 * the unexpected overhead of cache flushing is acceptable.
633 pr_debug("Validating PTE clear\n");
634 if (WARN_ON(!args->ptep))
638 pte = __pte(pte_val(pte) | RANDOM_ORVALUE);
640 set_pte_at(args->mm, args->vaddr, args->ptep, pte);
641 flush_dcache_page(page);
643 ptep_clear(args->mm, args->vaddr, args->ptep);
644 pte = ptep_get(args->ptep);
645 WARN_ON(!pte_none(pte));
648 static void __init pmd_clear_tests(struct pgtable_debug_args *args)
650 pmd_t pmd = READ_ONCE(*args->pmdp);
652 pr_debug("Validating PMD clear\n");
653 pmd = __pmd(pmd_val(pmd) | RANDOM_ORVALUE);
654 WRITE_ONCE(*args->pmdp, pmd);
655 pmd_clear(args->pmdp);
656 pmd = READ_ONCE(*args->pmdp);
657 WARN_ON(!pmd_none(pmd));
660 static void __init pmd_populate_tests(struct pgtable_debug_args *args)
664 pr_debug("Validating PMD populate\n");
666 * This entry points to next level page table page.
667 * Hence this must not qualify as pmd_bad().
669 pmd_populate(args->mm, args->pmdp, args->start_ptep);
670 pmd = READ_ONCE(*args->pmdp);
671 WARN_ON(pmd_bad(pmd));
674 static void __init pte_special_tests(struct pgtable_debug_args *args)
676 pte_t pte = pfn_pte(args->fixed_pte_pfn, args->page_prot);
678 if (!IS_ENABLED(CONFIG_ARCH_HAS_PTE_SPECIAL))
681 pr_debug("Validating PTE special\n");
682 WARN_ON(!pte_special(pte_mkspecial(pte)));
685 static void __init pte_protnone_tests(struct pgtable_debug_args *args)
687 pte_t pte = pfn_pte(args->fixed_pte_pfn, args->page_prot_none);
689 if (!IS_ENABLED(CONFIG_NUMA_BALANCING))
692 pr_debug("Validating PTE protnone\n");
693 WARN_ON(!pte_protnone(pte));
694 WARN_ON(!pte_present(pte));
697 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
698 static void __init pmd_protnone_tests(struct pgtable_debug_args *args)
702 if (!IS_ENABLED(CONFIG_NUMA_BALANCING))
705 if (!has_transparent_hugepage())
708 pr_debug("Validating PMD protnone\n");
709 pmd = pmd_mkhuge(pfn_pmd(args->fixed_pmd_pfn, args->page_prot_none));
710 WARN_ON(!pmd_protnone(pmd));
711 WARN_ON(!pmd_present(pmd));
713 #else /* !CONFIG_TRANSPARENT_HUGEPAGE */
714 static void __init pmd_protnone_tests(struct pgtable_debug_args *args) { }
715 #endif /* CONFIG_TRANSPARENT_HUGEPAGE */
717 #ifdef CONFIG_ARCH_HAS_PTE_DEVMAP
718 static void __init pte_devmap_tests(struct pgtable_debug_args *args)
720 pte_t pte = pfn_pte(args->fixed_pte_pfn, args->page_prot);
722 pr_debug("Validating PTE devmap\n");
723 WARN_ON(!pte_devmap(pte_mkdevmap(pte)));
726 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
727 static void __init pmd_devmap_tests(struct pgtable_debug_args *args)
731 if (!has_transparent_hugepage())
734 pr_debug("Validating PMD devmap\n");
735 pmd = pfn_pmd(args->fixed_pmd_pfn, args->page_prot);
736 WARN_ON(!pmd_devmap(pmd_mkdevmap(pmd)));
739 #ifdef CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD
740 static void __init pud_devmap_tests(struct pgtable_debug_args *args)
744 if (!has_transparent_pud_hugepage())
747 pr_debug("Validating PUD devmap\n");
748 pud = pfn_pud(args->fixed_pud_pfn, args->page_prot);
749 WARN_ON(!pud_devmap(pud_mkdevmap(pud)));
751 #else /* !CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD */
752 static void __init pud_devmap_tests(struct pgtable_debug_args *args) { }
753 #endif /* CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD */
754 #else /* CONFIG_TRANSPARENT_HUGEPAGE */
755 static void __init pmd_devmap_tests(struct pgtable_debug_args *args) { }
756 static void __init pud_devmap_tests(struct pgtable_debug_args *args) { }
757 #endif /* CONFIG_TRANSPARENT_HUGEPAGE */
759 static void __init pte_devmap_tests(struct pgtable_debug_args *args) { }
760 static void __init pmd_devmap_tests(struct pgtable_debug_args *args) { }
761 static void __init pud_devmap_tests(struct pgtable_debug_args *args) { }
762 #endif /* CONFIG_ARCH_HAS_PTE_DEVMAP */
764 static void __init pte_soft_dirty_tests(struct pgtable_debug_args *args)
766 pte_t pte = pfn_pte(args->fixed_pte_pfn, args->page_prot);
768 if (!IS_ENABLED(CONFIG_MEM_SOFT_DIRTY))
771 pr_debug("Validating PTE soft dirty\n");
772 WARN_ON(!pte_soft_dirty(pte_mksoft_dirty(pte)));
773 WARN_ON(pte_soft_dirty(pte_clear_soft_dirty(pte)));
776 static void __init pte_swap_soft_dirty_tests(struct pgtable_debug_args *args)
778 pte_t pte = pfn_pte(args->fixed_pte_pfn, args->page_prot);
780 if (!IS_ENABLED(CONFIG_MEM_SOFT_DIRTY))
783 pr_debug("Validating PTE swap soft dirty\n");
784 WARN_ON(!pte_swp_soft_dirty(pte_swp_mksoft_dirty(pte)));
785 WARN_ON(pte_swp_soft_dirty(pte_swp_clear_soft_dirty(pte)));
788 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
789 static void __init pmd_soft_dirty_tests(struct pgtable_debug_args *args)
793 if (!IS_ENABLED(CONFIG_MEM_SOFT_DIRTY))
796 if (!has_transparent_hugepage())
799 pr_debug("Validating PMD soft dirty\n");
800 pmd = pfn_pmd(args->fixed_pmd_pfn, args->page_prot);
801 WARN_ON(!pmd_soft_dirty(pmd_mksoft_dirty(pmd)));
802 WARN_ON(pmd_soft_dirty(pmd_clear_soft_dirty(pmd)));
805 static void __init pmd_swap_soft_dirty_tests(struct pgtable_debug_args *args)
809 if (!IS_ENABLED(CONFIG_MEM_SOFT_DIRTY) ||
810 !IS_ENABLED(CONFIG_ARCH_ENABLE_THP_MIGRATION))
813 if (!has_transparent_hugepage())
816 pr_debug("Validating PMD swap soft dirty\n");
817 pmd = pfn_pmd(args->fixed_pmd_pfn, args->page_prot);
818 WARN_ON(!pmd_swp_soft_dirty(pmd_swp_mksoft_dirty(pmd)));
819 WARN_ON(pmd_swp_soft_dirty(pmd_swp_clear_soft_dirty(pmd)));
821 #else /* !CONFIG_TRANSPARENT_HUGEPAGE */
822 static void __init pmd_soft_dirty_tests(struct pgtable_debug_args *args) { }
823 static void __init pmd_swap_soft_dirty_tests(struct pgtable_debug_args *args) { }
824 #endif /* CONFIG_TRANSPARENT_HUGEPAGE */
826 static void __init pte_swap_exclusive_tests(struct pgtable_debug_args *args)
828 unsigned long max_swap_offset;
829 swp_entry_t entry, entry2;
832 pr_debug("Validating PTE swap exclusive\n");
834 /* See generic_max_swapfile_size(): probe the maximum offset */
835 max_swap_offset = swp_offset(pte_to_swp_entry(swp_entry_to_pte(swp_entry(0, ~0UL))));
837 /* Create a swp entry with all possible bits set */
838 entry = swp_entry((1 << MAX_SWAPFILES_SHIFT) - 1, max_swap_offset);
840 pte = swp_entry_to_pte(entry);
841 WARN_ON(pte_swp_exclusive(pte));
842 WARN_ON(!is_swap_pte(pte));
843 entry2 = pte_to_swp_entry(pte);
844 WARN_ON(memcmp(&entry, &entry2, sizeof(entry)));
846 pte = pte_swp_mkexclusive(pte);
847 WARN_ON(!pte_swp_exclusive(pte));
848 WARN_ON(!is_swap_pte(pte));
849 WARN_ON(pte_swp_soft_dirty(pte));
850 entry2 = pte_to_swp_entry(pte);
851 WARN_ON(memcmp(&entry, &entry2, sizeof(entry)));
853 pte = pte_swp_clear_exclusive(pte);
854 WARN_ON(pte_swp_exclusive(pte));
855 WARN_ON(!is_swap_pte(pte));
856 entry2 = pte_to_swp_entry(pte);
857 WARN_ON(memcmp(&entry, &entry2, sizeof(entry)));
860 static void __init pte_swap_tests(struct pgtable_debug_args *args)
865 pr_debug("Validating PTE swap\n");
866 pte = pfn_pte(args->fixed_pte_pfn, args->page_prot);
867 swp = __pte_to_swp_entry(pte);
868 pte = __swp_entry_to_pte(swp);
869 WARN_ON(args->fixed_pte_pfn != pte_pfn(pte));
872 #ifdef CONFIG_ARCH_ENABLE_THP_MIGRATION
873 static void __init pmd_swap_tests(struct pgtable_debug_args *args)
878 if (!has_transparent_hugepage())
881 pr_debug("Validating PMD swap\n");
882 pmd = pfn_pmd(args->fixed_pmd_pfn, args->page_prot);
883 swp = __pmd_to_swp_entry(pmd);
884 pmd = __swp_entry_to_pmd(swp);
885 WARN_ON(args->fixed_pmd_pfn != pmd_pfn(pmd));
887 #else /* !CONFIG_ARCH_ENABLE_THP_MIGRATION */
888 static void __init pmd_swap_tests(struct pgtable_debug_args *args) { }
889 #endif /* CONFIG_ARCH_ENABLE_THP_MIGRATION */
891 static void __init swap_migration_tests(struct pgtable_debug_args *args)
896 if (!IS_ENABLED(CONFIG_MIGRATION))
900 * swap_migration_tests() requires a dedicated page as it needs to
901 * be locked before creating a migration entry from it. Locking the
902 * page that actually maps kernel text ('start_kernel') can be real
903 * problematic. Lets use the allocated page explicitly for this
906 page = (args->pte_pfn != ULONG_MAX) ? pfn_to_page(args->pte_pfn) : NULL;
910 pr_debug("Validating swap migration\n");
913 * make_[readable|writable]_migration_entry() expects given page to
914 * be locked, otherwise it stumbles upon a BUG_ON().
916 __SetPageLocked(page);
917 swp = make_writable_migration_entry(page_to_pfn(page));
918 WARN_ON(!is_migration_entry(swp));
919 WARN_ON(!is_writable_migration_entry(swp));
921 swp = make_readable_migration_entry(swp_offset(swp));
922 WARN_ON(!is_migration_entry(swp));
923 WARN_ON(is_writable_migration_entry(swp));
925 swp = make_readable_migration_entry(page_to_pfn(page));
926 WARN_ON(!is_migration_entry(swp));
927 WARN_ON(is_writable_migration_entry(swp));
928 __ClearPageLocked(page);
931 #ifdef CONFIG_HUGETLB_PAGE
932 static void __init hugetlb_basic_tests(struct pgtable_debug_args *args)
937 pr_debug("Validating HugeTLB basic\n");
939 * Accessing the page associated with the pfn is safe here,
940 * as it was previously derived from a real kernel symbol.
942 page = pfn_to_page(args->fixed_pmd_pfn);
943 pte = mk_huge_pte(page, args->page_prot);
945 WARN_ON(!huge_pte_dirty(huge_pte_mkdirty(pte)));
946 WARN_ON(!huge_pte_write(huge_pte_mkwrite(huge_pte_wrprotect(pte))));
947 WARN_ON(huge_pte_write(huge_pte_wrprotect(huge_pte_mkwrite(pte))));
949 #ifdef CONFIG_ARCH_WANT_GENERAL_HUGETLB
950 pte = pfn_pte(args->fixed_pmd_pfn, args->page_prot);
952 WARN_ON(!pte_huge(arch_make_huge_pte(pte, PMD_SHIFT, VM_ACCESS_FLAGS)));
953 #endif /* CONFIG_ARCH_WANT_GENERAL_HUGETLB */
955 #else /* !CONFIG_HUGETLB_PAGE */
956 static void __init hugetlb_basic_tests(struct pgtable_debug_args *args) { }
957 #endif /* CONFIG_HUGETLB_PAGE */
959 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
960 static void __init pmd_thp_tests(struct pgtable_debug_args *args)
964 if (!has_transparent_hugepage())
967 pr_debug("Validating PMD based THP\n");
969 * pmd_trans_huge() and pmd_present() must return positive after
970 * MMU invalidation with pmd_mkinvalid(). This behavior is an
971 * optimization for transparent huge page. pmd_trans_huge() must
972 * be true if pmd_page() returns a valid THP to avoid taking the
973 * pmd_lock when others walk over non transhuge pmds (i.e. there
974 * are no THP allocated). Especially when splitting a THP and
975 * removing the present bit from the pmd, pmd_trans_huge() still
976 * needs to return true. pmd_present() should be true whenever
977 * pmd_trans_huge() returns true.
979 pmd = pfn_pmd(args->fixed_pmd_pfn, args->page_prot);
980 WARN_ON(!pmd_trans_huge(pmd_mkhuge(pmd)));
982 #ifndef __HAVE_ARCH_PMDP_INVALIDATE
983 WARN_ON(!pmd_trans_huge(pmd_mkinvalid(pmd_mkhuge(pmd))));
984 WARN_ON(!pmd_present(pmd_mkinvalid(pmd_mkhuge(pmd))));
985 WARN_ON(!pmd_leaf(pmd_mkinvalid(pmd_mkhuge(pmd))));
986 #endif /* __HAVE_ARCH_PMDP_INVALIDATE */
989 #ifdef CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD
990 static void __init pud_thp_tests(struct pgtable_debug_args *args)
994 if (!has_transparent_pud_hugepage())
997 pr_debug("Validating PUD based THP\n");
998 pud = pfn_pud(args->fixed_pud_pfn, args->page_prot);
999 WARN_ON(!pud_trans_huge(pud_mkhuge(pud)));
1002 * pud_mkinvalid() has been dropped for now. Enable back
1003 * these tests when it comes back with a modified pud_present().
1005 * WARN_ON(!pud_trans_huge(pud_mkinvalid(pud_mkhuge(pud))));
1006 * WARN_ON(!pud_present(pud_mkinvalid(pud_mkhuge(pud))));
1009 #else /* !CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD */
1010 static void __init pud_thp_tests(struct pgtable_debug_args *args) { }
1011 #endif /* CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD */
1012 #else /* !CONFIG_TRANSPARENT_HUGEPAGE */
1013 static void __init pmd_thp_tests(struct pgtable_debug_args *args) { }
1014 static void __init pud_thp_tests(struct pgtable_debug_args *args) { }
1015 #endif /* CONFIG_TRANSPARENT_HUGEPAGE */
1017 static unsigned long __init get_random_vaddr(void)
1019 unsigned long random_vaddr, random_pages, total_user_pages;
1021 total_user_pages = (TASK_SIZE - FIRST_USER_ADDRESS) / PAGE_SIZE;
1023 random_pages = get_random_long() % total_user_pages;
1024 random_vaddr = FIRST_USER_ADDRESS + random_pages * PAGE_SIZE;
1026 return random_vaddr;
1029 static void __init destroy_args(struct pgtable_debug_args *args)
1031 struct page *page = NULL;
1033 /* Free (huge) page */
1034 if (IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE) &&
1035 has_transparent_pud_hugepage() &&
1036 args->pud_pfn != ULONG_MAX) {
1037 if (args->is_contiguous_page) {
1038 free_contig_range(args->pud_pfn,
1039 (1 << (HPAGE_PUD_SHIFT - PAGE_SHIFT)));
1041 page = pfn_to_page(args->pud_pfn);
1042 __free_pages(page, HPAGE_PUD_SHIFT - PAGE_SHIFT);
1045 args->pud_pfn = ULONG_MAX;
1046 args->pmd_pfn = ULONG_MAX;
1047 args->pte_pfn = ULONG_MAX;
1050 if (IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE) &&
1051 has_transparent_hugepage() &&
1052 args->pmd_pfn != ULONG_MAX) {
1053 if (args->is_contiguous_page) {
1054 free_contig_range(args->pmd_pfn, (1 << HPAGE_PMD_ORDER));
1056 page = pfn_to_page(args->pmd_pfn);
1057 __free_pages(page, HPAGE_PMD_ORDER);
1060 args->pmd_pfn = ULONG_MAX;
1061 args->pte_pfn = ULONG_MAX;
1064 if (args->pte_pfn != ULONG_MAX) {
1065 page = pfn_to_page(args->pte_pfn);
1068 args->pte_pfn = ULONG_MAX;
1071 /* Free page table entries */
1072 if (args->start_ptep) {
1073 pte_free(args->mm, args->start_ptep);
1074 mm_dec_nr_ptes(args->mm);
1077 if (args->start_pmdp) {
1078 pmd_free(args->mm, args->start_pmdp);
1079 mm_dec_nr_pmds(args->mm);
1082 if (args->start_pudp) {
1083 pud_free(args->mm, args->start_pudp);
1084 mm_dec_nr_puds(args->mm);
1087 if (args->start_p4dp)
1088 p4d_free(args->mm, args->start_p4dp);
1090 /* Free vma and mm struct */
1092 vm_area_free(args->vma);
1098 static struct page * __init
1099 debug_vm_pgtable_alloc_huge_page(struct pgtable_debug_args *args, int order)
1101 struct page *page = NULL;
1103 #ifdef CONFIG_CONTIG_ALLOC
1104 if (order > MAX_PAGE_ORDER) {
1105 page = alloc_contig_pages((1 << order), GFP_KERNEL,
1106 first_online_node, NULL);
1108 args->is_contiguous_page = true;
1114 if (order <= MAX_PAGE_ORDER)
1115 page = alloc_pages(GFP_KERNEL, order);
1121 * Check if a physical memory range described by <pstart, pend> contains
1122 * an area that is of size psize, and aligned to psize.
1124 * Don't use address 0, an all-zeroes physical address might mask bugs, and
1125 * it's not used on x86.
1127 static void __init phys_align_check(phys_addr_t pstart,
1128 phys_addr_t pend, unsigned long psize,
1129 phys_addr_t *physp, unsigned long *alignp)
1131 phys_addr_t aligned_start, aligned_end;
1136 aligned_start = ALIGN(pstart, psize);
1137 aligned_end = aligned_start + psize;
1139 if (aligned_end > aligned_start && aligned_end <= pend) {
1141 *physp = aligned_start;
1145 static void __init init_fixed_pfns(struct pgtable_debug_args *args)
1148 phys_addr_t phys, pstart, pend;
1151 * Initialize the fixed pfns. To do this, try to find a
1152 * valid physical range, preferably aligned to PUD_SIZE,
1153 * but settling for aligned to PMD_SIZE as a fallback. If
1154 * neither of those is found, use the physical address of
1155 * the start_kernel symbol.
1157 * The memory doesn't need to be allocated, it just needs to exist
1158 * as usable memory. It won't be touched.
1160 * The alignment is recorded, and can be checked to see if we
1161 * can run the tests that require an actual valid physical
1162 * address range on some architectures ({pmd,pud}_huge_test
1166 phys = __pa_symbol(&start_kernel);
1167 args->fixed_alignment = PAGE_SIZE;
1169 for_each_mem_range(idx, &pstart, &pend) {
1170 /* First check for a PUD-aligned area */
1171 phys_align_check(pstart, pend, PUD_SIZE, &phys,
1172 &args->fixed_alignment);
1174 /* If a PUD-aligned area is found, we're done */
1175 if (args->fixed_alignment == PUD_SIZE)
1179 * If no PMD-aligned area found yet, check for one,
1180 * but continue the loop to look for a PUD-aligned area.
1182 if (args->fixed_alignment < PMD_SIZE)
1183 phys_align_check(pstart, pend, PMD_SIZE, &phys,
1184 &args->fixed_alignment);
1187 args->fixed_pgd_pfn = __phys_to_pfn(phys & PGDIR_MASK);
1188 args->fixed_p4d_pfn = __phys_to_pfn(phys & P4D_MASK);
1189 args->fixed_pud_pfn = __phys_to_pfn(phys & PUD_MASK);
1190 args->fixed_pmd_pfn = __phys_to_pfn(phys & PMD_MASK);
1191 args->fixed_pte_pfn = __phys_to_pfn(phys & PAGE_MASK);
1192 WARN_ON(!pfn_valid(args->fixed_pte_pfn));
1196 static int __init init_args(struct pgtable_debug_args *args)
1198 struct page *page = NULL;
1202 * Initialize the debugging data.
1204 * vm_get_page_prot(VM_NONE) or vm_get_page_prot(VM_SHARED|VM_NONE)
1205 * will help create page table entries with PROT_NONE permission as
1206 * required for pxx_protnone_tests().
1208 memset(args, 0, sizeof(*args));
1209 args->vaddr = get_random_vaddr();
1210 args->page_prot = vm_get_page_prot(VM_ACCESS_FLAGS);
1211 args->page_prot_none = vm_get_page_prot(VM_NONE);
1212 args->is_contiguous_page = false;
1213 args->pud_pfn = ULONG_MAX;
1214 args->pmd_pfn = ULONG_MAX;
1215 args->pte_pfn = ULONG_MAX;
1216 args->fixed_pgd_pfn = ULONG_MAX;
1217 args->fixed_p4d_pfn = ULONG_MAX;
1218 args->fixed_pud_pfn = ULONG_MAX;
1219 args->fixed_pmd_pfn = ULONG_MAX;
1220 args->fixed_pte_pfn = ULONG_MAX;
1222 /* Allocate mm and vma */
1223 args->mm = mm_alloc();
1225 pr_err("Failed to allocate mm struct\n");
1230 args->vma = vm_area_alloc(args->mm);
1232 pr_err("Failed to allocate vma\n");
1238 * Allocate page table entries. They will be modified in the tests.
1239 * Lets save the page table entries so that they can be released
1240 * when the tests are completed.
1242 args->pgdp = pgd_offset(args->mm, args->vaddr);
1243 args->p4dp = p4d_alloc(args->mm, args->pgdp, args->vaddr);
1245 pr_err("Failed to allocate p4d entries\n");
1249 args->start_p4dp = p4d_offset(args->pgdp, 0UL);
1250 WARN_ON(!args->start_p4dp);
1252 args->pudp = pud_alloc(args->mm, args->p4dp, args->vaddr);
1254 pr_err("Failed to allocate pud entries\n");
1258 args->start_pudp = pud_offset(args->p4dp, 0UL);
1259 WARN_ON(!args->start_pudp);
1261 args->pmdp = pmd_alloc(args->mm, args->pudp, args->vaddr);
1263 pr_err("Failed to allocate pmd entries\n");
1267 args->start_pmdp = pmd_offset(args->pudp, 0UL);
1268 WARN_ON(!args->start_pmdp);
1270 if (pte_alloc(args->mm, args->pmdp)) {
1271 pr_err("Failed to allocate pte entries\n");
1275 args->start_ptep = pmd_pgtable(READ_ONCE(*args->pmdp));
1276 WARN_ON(!args->start_ptep);
1278 init_fixed_pfns(args);
1281 * Allocate (huge) pages because some of the tests need to access
1282 * the data in the pages. The corresponding tests will be skipped
1283 * if we fail to allocate (huge) pages.
1285 if (IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE) &&
1286 has_transparent_pud_hugepage()) {
1287 page = debug_vm_pgtable_alloc_huge_page(args,
1288 HPAGE_PUD_SHIFT - PAGE_SHIFT);
1290 args->pud_pfn = page_to_pfn(page);
1291 args->pmd_pfn = args->pud_pfn;
1292 args->pte_pfn = args->pud_pfn;
1297 if (IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE) &&
1298 has_transparent_hugepage()) {
1299 page = debug_vm_pgtable_alloc_huge_page(args, HPAGE_PMD_ORDER);
1301 args->pmd_pfn = page_to_pfn(page);
1302 args->pte_pfn = args->pmd_pfn;
1307 page = alloc_page(GFP_KERNEL);
1309 args->pte_pfn = page_to_pfn(page);
1318 static int __init debug_vm_pgtable(void)
1320 struct pgtable_debug_args args;
1321 spinlock_t *ptl = NULL;
1324 pr_info("Validating architecture page table helpers\n");
1325 ret = init_args(&args);
1330 * Iterate over each possible vm_flags to make sure that all
1331 * the basic page table transformation validations just hold
1332 * true irrespective of the starting protection value for a
1333 * given page table entry.
1335 * Protection based vm_flags combinations are always linear
1336 * and increasing i.e starting from VM_NONE and going up to
1337 * (VM_SHARED | READ | WRITE | EXEC).
1339 #define VM_FLAGS_START (VM_NONE)
1340 #define VM_FLAGS_END (VM_SHARED | VM_EXEC | VM_WRITE | VM_READ)
1342 for (idx = VM_FLAGS_START; idx <= VM_FLAGS_END; idx++) {
1343 pte_basic_tests(&args, idx);
1344 pmd_basic_tests(&args, idx);
1345 pud_basic_tests(&args, idx);
1349 * Both P4D and PGD level tests are very basic which do not
1350 * involve creating page table entries from the protection
1351 * value and the given pfn. Hence just keep them out from
1352 * the above iteration for now to save some test execution
1355 p4d_basic_tests(&args);
1356 pgd_basic_tests(&args);
1358 pmd_leaf_tests(&args);
1359 pud_leaf_tests(&args);
1361 pte_special_tests(&args);
1362 pte_protnone_tests(&args);
1363 pmd_protnone_tests(&args);
1365 pte_devmap_tests(&args);
1366 pmd_devmap_tests(&args);
1367 pud_devmap_tests(&args);
1369 pte_soft_dirty_tests(&args);
1370 pmd_soft_dirty_tests(&args);
1371 pte_swap_soft_dirty_tests(&args);
1372 pmd_swap_soft_dirty_tests(&args);
1374 pte_swap_exclusive_tests(&args);
1376 pte_swap_tests(&args);
1377 pmd_swap_tests(&args);
1379 swap_migration_tests(&args);
1381 pmd_thp_tests(&args);
1382 pud_thp_tests(&args);
1384 hugetlb_basic_tests(&args);
1387 * Page table modifying tests. They need to hold
1388 * proper page table lock.
1391 args.ptep = pte_offset_map_lock(args.mm, args.pmdp, args.vaddr, &ptl);
1392 pte_clear_tests(&args);
1393 pte_advanced_tests(&args);
1395 pte_unmap_unlock(args.ptep, ptl);
1397 ptl = pmd_lock(args.mm, args.pmdp);
1398 pmd_clear_tests(&args);
1399 pmd_advanced_tests(&args);
1400 pmd_huge_tests(&args);
1401 pmd_populate_tests(&args);
1404 ptl = pud_lock(args.mm, args.pudp);
1405 pud_clear_tests(&args);
1406 pud_advanced_tests(&args);
1407 pud_huge_tests(&args);
1408 pud_populate_tests(&args);
1411 spin_lock(&(args.mm->page_table_lock));
1412 p4d_clear_tests(&args);
1413 pgd_clear_tests(&args);
1414 p4d_populate_tests(&args);
1415 pgd_populate_tests(&args);
1416 spin_unlock(&(args.mm->page_table_lock));
1418 destroy_args(&args);
1421 late_initcall(debug_vm_pgtable);