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.
9 * Author: Anshuman Khandual <anshuman.khandual@arm.com>
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>
34 #include <asm/cacheflush.h>
35 #include <asm/pgalloc.h>
36 #include <asm/tlbflush.h>
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.
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.
50 #define S390_SKIP_MASK GENMASK(3, 0)
51 #if __BITS_PER_LONG == 64
52 #define PPC64_SKIP_MASK GENMASK(62, 62)
54 #define PPC64_SKIP_MASK 0x0
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)
60 struct pgtable_debug_args {
62 struct vm_area_struct *vma;
77 pgprot_t page_prot_none;
79 bool is_contiguous_page;
80 unsigned long pud_pfn;
81 unsigned long pmd_pfn;
82 unsigned long pte_pfn;
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;
92 static void __init pte_basic_tests(struct pgtable_debug_args *args, int idx)
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;
98 pr_debug("Validating PTE basic (%pGv)\n", ptr);
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.
107 WARN_ON(pte_dirty(pte_wrprotect(pte)));
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))));
120 static void __init pte_advanced_tests(struct pgtable_debug_args *args)
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
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.
136 page = (args->pte_pfn != ULONG_MAX) ? pfn_to_page(args->pte_pfn) : NULL;
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));
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));
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));
173 ptep_get_and_clear_full(args->mm, args->vaddr, args->ptep, 1);
176 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
177 static void __init pmd_basic_tests(struct pgtable_debug_args *args, int idx)
179 pgprot_t prot = vm_get_page_prot(idx);
180 unsigned long val = idx, *ptr = &val;
183 if (!has_transparent_hugepage())
186 pr_debug("Validating PMD basic (%pGv)\n", ptr);
187 pmd = pfn_pmd(args->fixed_pmd_pfn, prot);
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.
196 WARN_ON(pmd_dirty(pmd_wrprotect(pmd)));
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))));
209 * A huge page does not point to next level page table
210 * entry. Hence this must qualify as pmd_bad().
212 WARN_ON(!pmd_bad(pmd_mkhuge(pmd)));
215 static void __init pmd_advanced_tests(struct pgtable_debug_args *args)
219 unsigned long vaddr = args->vaddr;
221 if (!has_transparent_hugepage())
224 page = (args->pmd_pfn != ULONG_MAX) ? pfn_to_page(args->pmd_pfn) : NULL;
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.
235 pr_debug("Validating PMD advanced\n");
236 /* Align the address wrt HPAGE_PMD_SIZE */
237 vaddr &= HPAGE_PMD_MASK;
239 pgtable_trans_huge_deposit(args->mm, args->pmdp, args->start_ptep);
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));
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));
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));
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);
278 static void __init pmd_leaf_tests(struct pgtable_debug_args *args)
282 if (!has_transparent_hugepage())
285 pr_debug("Validating PMD leaf\n");
286 pmd = pfn_pmd(args->fixed_pmd_pfn, args->page_prot);
289 * PMD based THP is a leaf entry.
291 pmd = pmd_mkhuge(pmd);
292 WARN_ON(!pmd_leaf(pmd));
295 #ifdef CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD
296 static void __init pud_basic_tests(struct pgtable_debug_args *args, int idx)
298 pgprot_t prot = vm_get_page_prot(idx);
299 unsigned long val = idx, *ptr = &val;
302 if (!has_transparent_hugepage())
305 pr_debug("Validating PUD basic (%pGv)\n", ptr);
306 pud = pfn_pud(args->fixed_pud_pfn, prot);
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.
315 WARN_ON(pud_dirty(pud_wrprotect(pud)));
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))));
327 if (mm_pmd_folded(args->mm))
331 * A huge page does not point to next level page table
332 * entry. Hence this must qualify as pud_bad().
334 WARN_ON(!pud_bad(pud_mkhuge(pud)));
337 static void __init pud_advanced_tests(struct pgtable_debug_args *args)
340 unsigned long vaddr = args->vaddr;
343 if (!has_transparent_hugepage())
346 page = (args->pud_pfn != ULONG_MAX) ? pfn_to_page(args->pud_pfn) : NULL;
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.
357 pr_debug("Validating PUD advanced\n");
358 /* Align the address wrt HPAGE_PUD_SIZE */
359 vaddr &= HPAGE_PUD_MASK;
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));
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)));
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 */
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));
398 pudp_huge_get_and_clear(args->mm, vaddr, args->pudp);
401 static void __init pud_leaf_tests(struct pgtable_debug_args *args)
405 if (!has_transparent_hugepage())
408 pr_debug("Validating PUD leaf\n");
409 pud = pfn_pud(args->fixed_pud_pfn, args->page_prot);
411 * PUD based THP is a leaf entry.
413 pud = pud_mkhuge(pud);
414 WARN_ON(!pud_leaf(pud));
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 */
430 #ifdef CONFIG_HAVE_ARCH_HUGE_VMAP
431 static void __init pmd_huge_tests(struct pgtable_debug_args *args)
435 if (!arch_vmap_pmd_supported(args->page_prot) ||
436 args->fixed_alignment < PMD_SIZE)
439 pr_debug("Validating PMD huge\n");
441 * X86 defined pmd_set_huge() verifies that the given
442 * PMD is not a populated non-leaf entry.
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));
451 static void __init pud_huge_tests(struct pgtable_debug_args *args)
455 if (!arch_vmap_pud_supported(args->page_prot) ||
456 args->fixed_alignment < PUD_SIZE)
459 pr_debug("Validating PUD huge\n");
461 * X86 defined pud_set_huge() verifies that the given
462 * PUD is not a populated non-leaf entry.
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));
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 */
475 static void __init p4d_basic_tests(struct pgtable_debug_args *args)
479 pr_debug("Validating P4D basic\n");
480 memset(&p4d, RANDOM_NZVALUE, sizeof(p4d_t));
481 WARN_ON(!p4d_same(p4d, p4d));
484 static void __init pgd_basic_tests(struct pgtable_debug_args *args)
488 pr_debug("Validating PGD basic\n");
489 memset(&pgd, RANDOM_NZVALUE, sizeof(pgd_t));
490 WARN_ON(!pgd_same(pgd, pgd));
493 #ifndef __PAGETABLE_PUD_FOLDED
494 static void __init pud_clear_tests(struct pgtable_debug_args *args)
496 pud_t pud = READ_ONCE(*args->pudp);
498 if (mm_pmd_folded(args->mm))
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));
509 static void __init pud_populate_tests(struct pgtable_debug_args *args)
513 if (mm_pmd_folded(args->mm))
516 pr_debug("Validating PUD populate\n");
518 * This entry points to next level page table page.
519 * Hence this must not qualify as pud_bad().
521 pud_populate(args->mm, args->pudp, args->start_pmdp);
522 pud = READ_ONCE(*args->pudp);
523 WARN_ON(pud_bad(pud));
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 */
530 #ifndef __PAGETABLE_P4D_FOLDED
531 static void __init p4d_clear_tests(struct pgtable_debug_args *args)
533 p4d_t p4d = READ_ONCE(*args->p4dp);
535 if (mm_pud_folded(args->mm))
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));
546 static void __init p4d_populate_tests(struct pgtable_debug_args *args)
550 if (mm_pud_folded(args->mm))
553 pr_debug("Validating P4D populate\n");
555 * This entry points to next level page table page.
556 * Hence this must not qualify as p4d_bad().
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));
565 static void __init pgd_clear_tests(struct pgtable_debug_args *args)
567 pgd_t pgd = READ_ONCE(*(args->pgdp));
569 if (mm_p4d_folded(args->mm))
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));
580 static void __init pgd_populate_tests(struct pgtable_debug_args *args)
584 if (mm_p4d_folded(args->mm))
587 pr_debug("Validating PGD populate\n");
589 * This entry points to next level page table page.
590 * Hence this must not qualify as pgd_bad().
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));
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 */
605 static void __init pte_clear_tests(struct pgtable_debug_args *args)
608 pte_t pte = pfn_pte(args->pte_pfn, args->page_prot);
610 page = (args->pte_pfn != ULONG_MAX) ? pfn_to_page(args->pte_pfn) : NULL;
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.
621 pr_debug("Validating PTE clear\n");
623 pte = __pte(pte_val(pte) | RANDOM_ORVALUE);
625 set_pte_at(args->mm, args->vaddr, args->ptep, pte);
626 flush_dcache_page(page);
628 ptep_clear(args->mm, args->vaddr, args->ptep);
629 pte = ptep_get(args->ptep);
630 WARN_ON(!pte_none(pte));
633 static void __init pmd_clear_tests(struct pgtable_debug_args *args)
635 pmd_t pmd = READ_ONCE(*args->pmdp);
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));
645 static void __init pmd_populate_tests(struct pgtable_debug_args *args)
649 pr_debug("Validating PMD populate\n");
651 * This entry points to next level page table page.
652 * Hence this must not qualify as pmd_bad().
654 pmd_populate(args->mm, args->pmdp, args->start_ptep);
655 pmd = READ_ONCE(*args->pmdp);
656 WARN_ON(pmd_bad(pmd));
659 static void __init pte_special_tests(struct pgtable_debug_args *args)
661 pte_t pte = pfn_pte(args->fixed_pte_pfn, args->page_prot);
663 if (!IS_ENABLED(CONFIG_ARCH_HAS_PTE_SPECIAL))
666 pr_debug("Validating PTE special\n");
667 WARN_ON(!pte_special(pte_mkspecial(pte)));
670 static void __init pte_protnone_tests(struct pgtable_debug_args *args)
672 pte_t pte = pfn_pte(args->fixed_pte_pfn, args->page_prot_none);
674 if (!IS_ENABLED(CONFIG_NUMA_BALANCING))
677 pr_debug("Validating PTE protnone\n");
678 WARN_ON(!pte_protnone(pte));
679 WARN_ON(!pte_present(pte));
682 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
683 static void __init pmd_protnone_tests(struct pgtable_debug_args *args)
687 if (!IS_ENABLED(CONFIG_NUMA_BALANCING))
690 if (!has_transparent_hugepage())
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));
698 #else /* !CONFIG_TRANSPARENT_HUGEPAGE */
699 static void __init pmd_protnone_tests(struct pgtable_debug_args *args) { }
700 #endif /* CONFIG_TRANSPARENT_HUGEPAGE */
702 #ifdef CONFIG_ARCH_HAS_PTE_DEVMAP
703 static void __init pte_devmap_tests(struct pgtable_debug_args *args)
705 pte_t pte = pfn_pte(args->fixed_pte_pfn, args->page_prot);
707 pr_debug("Validating PTE devmap\n");
708 WARN_ON(!pte_devmap(pte_mkdevmap(pte)));
711 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
712 static void __init pmd_devmap_tests(struct pgtable_debug_args *args)
716 if (!has_transparent_hugepage())
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)));
724 #ifdef CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD
725 static void __init pud_devmap_tests(struct pgtable_debug_args *args)
729 if (!has_transparent_hugepage())
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)));
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 */
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 */
749 static void __init pte_soft_dirty_tests(struct pgtable_debug_args *args)
751 pte_t pte = pfn_pte(args->fixed_pte_pfn, args->page_prot);
753 if (!IS_ENABLED(CONFIG_MEM_SOFT_DIRTY))
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)));
761 static void __init pte_swap_soft_dirty_tests(struct pgtable_debug_args *args)
763 pte_t pte = pfn_pte(args->fixed_pte_pfn, args->page_prot);
765 if (!IS_ENABLED(CONFIG_MEM_SOFT_DIRTY))
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)));
773 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
774 static void __init pmd_soft_dirty_tests(struct pgtable_debug_args *args)
778 if (!IS_ENABLED(CONFIG_MEM_SOFT_DIRTY))
781 if (!has_transparent_hugepage())
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)));
790 static void __init pmd_swap_soft_dirty_tests(struct pgtable_debug_args *args)
794 if (!IS_ENABLED(CONFIG_MEM_SOFT_DIRTY) ||
795 !IS_ENABLED(CONFIG_ARCH_ENABLE_THP_MIGRATION))
798 if (!has_transparent_hugepage())
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)));
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 */
811 static void __init pte_swap_exclusive_tests(struct pgtable_debug_args *args)
813 unsigned long max_swap_offset;
814 swp_entry_t entry, entry2;
817 pr_debug("Validating PTE swap exclusive\n");
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))));
822 /* Create a swp entry with all possible bits set */
823 entry = swp_entry((1 << MAX_SWAPFILES_SHIFT) - 1, max_swap_offset);
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)));
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)));
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)));
845 static void __init pte_swap_tests(struct pgtable_debug_args *args)
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));
857 #ifdef CONFIG_ARCH_ENABLE_THP_MIGRATION
858 static void __init pmd_swap_tests(struct pgtable_debug_args *args)
863 if (!has_transparent_hugepage())
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));
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 */
876 static void __init swap_migration_tests(struct pgtable_debug_args *args)
881 if (!IS_ENABLED(CONFIG_MIGRATION))
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
891 page = (args->pte_pfn != ULONG_MAX) ? pfn_to_page(args->pte_pfn) : NULL;
895 pr_debug("Validating swap migration\n");
898 * make_[readable|writable]_migration_entry() expects given page to
899 * be locked, otherwise it stumbles upon a BUG_ON().
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));
906 swp = make_readable_migration_entry(swp_offset(swp));
907 WARN_ON(!is_migration_entry(swp));
908 WARN_ON(is_writable_migration_entry(swp));
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);
916 #ifdef CONFIG_HUGETLB_PAGE
917 static void __init hugetlb_basic_tests(struct pgtable_debug_args *args)
922 pr_debug("Validating HugeTLB basic\n");
924 * Accessing the page associated with the pfn is safe here,
925 * as it was previously derived from a real kernel symbol.
927 page = pfn_to_page(args->fixed_pmd_pfn);
928 pte = mk_huge_pte(page, args->page_prot);
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))));
934 #ifdef CONFIG_ARCH_WANT_GENERAL_HUGETLB
935 pte = pfn_pte(args->fixed_pmd_pfn, args->page_prot);
937 WARN_ON(!pte_huge(pte_mkhuge(pte)));
938 #endif /* CONFIG_ARCH_WANT_GENERAL_HUGETLB */
940 #else /* !CONFIG_HUGETLB_PAGE */
941 static void __init hugetlb_basic_tests(struct pgtable_debug_args *args) { }
942 #endif /* CONFIG_HUGETLB_PAGE */
944 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
945 static void __init pmd_thp_tests(struct pgtable_debug_args *args)
949 if (!has_transparent_hugepage())
952 pr_debug("Validating PMD based THP\n");
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.
964 pmd = pfn_pmd(args->fixed_pmd_pfn, args->page_prot);
965 WARN_ON(!pmd_trans_huge(pmd_mkhuge(pmd)));
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 */
973 #ifdef CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD
974 static void __init pud_thp_tests(struct pgtable_debug_args *args)
978 if (!has_transparent_hugepage())
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)));
986 * pud_mkinvalid() has been dropped for now. Enable back
987 * these tests when it comes back with a modified pud_present().
989 * WARN_ON(!pud_trans_huge(pud_mkinvalid(pud_mkhuge(pud))));
990 * WARN_ON(!pud_present(pud_mkinvalid(pud_mkhuge(pud))));
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 */
1001 static unsigned long __init get_random_vaddr(void)
1003 unsigned long random_vaddr, random_pages, total_user_pages;
1005 total_user_pages = (TASK_SIZE - FIRST_USER_ADDRESS) / PAGE_SIZE;
1007 random_pages = get_random_long() % total_user_pages;
1008 random_vaddr = FIRST_USER_ADDRESS + random_pages * PAGE_SIZE;
1010 return random_vaddr;
1013 static void __init destroy_args(struct pgtable_debug_args *args)
1015 struct page *page = NULL;
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)));
1026 page = pfn_to_page(args->pud_pfn);
1027 __free_pages(page, HPAGE_PUD_SHIFT - PAGE_SHIFT);
1030 args->pud_pfn = ULONG_MAX;
1031 args->pmd_pfn = ULONG_MAX;
1032 args->pte_pfn = ULONG_MAX;
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));
1041 page = pfn_to_page(args->pmd_pfn);
1042 __free_pages(page, HPAGE_PMD_ORDER);
1045 args->pmd_pfn = ULONG_MAX;
1046 args->pte_pfn = ULONG_MAX;
1049 if (args->pte_pfn != ULONG_MAX) {
1050 page = pfn_to_page(args->pte_pfn);
1051 __free_pages(page, 0);
1053 args->pte_pfn = ULONG_MAX;
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);
1062 if (args->start_pmdp) {
1063 pmd_free(args->mm, args->start_pmdp);
1064 mm_dec_nr_pmds(args->mm);
1067 if (args->start_pudp) {
1068 pud_free(args->mm, args->start_pudp);
1069 mm_dec_nr_puds(args->mm);
1072 if (args->start_p4dp)
1073 p4d_free(args->mm, args->start_p4dp);
1075 /* Free vma and mm struct */
1077 vm_area_free(args->vma);
1083 static struct page * __init
1084 debug_vm_pgtable_alloc_huge_page(struct pgtable_debug_args *args, int order)
1086 struct page *page = NULL;
1088 #ifdef CONFIG_CONTIG_ALLOC
1089 if (order >= MAX_ORDER) {
1090 page = alloc_contig_pages((1 << order), GFP_KERNEL,
1091 first_online_node, NULL);
1093 args->is_contiguous_page = true;
1099 if (order < MAX_ORDER)
1100 page = alloc_pages(GFP_KERNEL, order);
1106 * Check if a physical memory range described by <pstart, pend> contains
1107 * an area that is of size psize, and aligned to psize.
1109 * Don't use address 0, an all-zeroes physical address might mask bugs, and
1110 * it's not used on x86.
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)
1116 phys_addr_t aligned_start, aligned_end;
1121 aligned_start = ALIGN(pstart, psize);
1122 aligned_end = aligned_start + psize;
1124 if (aligned_end > aligned_start && aligned_end <= pend) {
1126 *physp = aligned_start;
1130 static void __init init_fixed_pfns(struct pgtable_debug_args *args)
1133 phys_addr_t phys, pstart, pend;
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.
1142 * The memory doesn't need to be allocated, it just needs to exist
1143 * as usable memory. It won't be touched.
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
1151 phys = __pa_symbol(&start_kernel);
1152 args->fixed_alignment = PAGE_SIZE;
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);
1159 /* If a PUD-aligned area is found, we're done */
1160 if (args->fixed_alignment == PUD_SIZE)
1164 * If no PMD-aligned area found yet, check for one,
1165 * but continue the loop to look for a PUD-aligned area.
1167 if (args->fixed_alignment < PMD_SIZE)
1168 phys_align_check(pstart, pend, PMD_SIZE, &phys,
1169 &args->fixed_alignment);
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));
1181 static int __init init_args(struct pgtable_debug_args *args)
1183 struct page *page = NULL;
1187 * Initialize the debugging data.
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().
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;
1207 /* Allocate mm and vma */
1208 args->mm = mm_alloc();
1210 pr_err("Failed to allocate mm struct\n");
1215 args->vma = vm_area_alloc(args->mm);
1217 pr_err("Failed to allocate vma\n");
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.
1227 args->pgdp = pgd_offset(args->mm, args->vaddr);
1228 args->p4dp = p4d_alloc(args->mm, args->pgdp, args->vaddr);
1230 pr_err("Failed to allocate p4d entries\n");
1234 args->start_p4dp = p4d_offset(args->pgdp, 0UL);
1235 WARN_ON(!args->start_p4dp);
1237 args->pudp = pud_alloc(args->mm, args->p4dp, args->vaddr);
1239 pr_err("Failed to allocate pud entries\n");
1243 args->start_pudp = pud_offset(args->p4dp, 0UL);
1244 WARN_ON(!args->start_pudp);
1246 args->pmdp = pmd_alloc(args->mm, args->pudp, args->vaddr);
1248 pr_err("Failed to allocate pmd entries\n");
1252 args->start_pmdp = pmd_offset(args->pudp, 0UL);
1253 WARN_ON(!args->start_pmdp);
1255 if (pte_alloc(args->mm, args->pmdp)) {
1256 pr_err("Failed to allocate pte entries\n");
1260 args->start_ptep = pmd_pgtable(READ_ONCE(*args->pmdp));
1261 WARN_ON(!args->start_ptep);
1263 init_fixed_pfns(args);
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.
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);
1276 args->pud_pfn = page_to_pfn(page);
1277 args->pmd_pfn = args->pud_pfn;
1278 args->pte_pfn = args->pud_pfn;
1283 if (IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE) &&
1284 has_transparent_hugepage()) {
1285 page = debug_vm_pgtable_alloc_huge_page(args, HPAGE_PMD_ORDER);
1287 args->pmd_pfn = page_to_pfn(page);
1288 args->pte_pfn = args->pmd_pfn;
1293 page = alloc_pages(GFP_KERNEL, 0);
1295 args->pte_pfn = page_to_pfn(page);
1304 static int __init debug_vm_pgtable(void)
1306 struct pgtable_debug_args args;
1307 spinlock_t *ptl = NULL;
1310 pr_info("Validating architecture page table helpers\n");
1311 ret = init_args(&args);
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.
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).
1325 #define VM_FLAGS_START (VM_NONE)
1326 #define VM_FLAGS_END (VM_SHARED | VM_EXEC | VM_WRITE | VM_READ)
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);
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
1341 p4d_basic_tests(&args);
1342 pgd_basic_tests(&args);
1344 pmd_leaf_tests(&args);
1345 pud_leaf_tests(&args);
1347 pte_special_tests(&args);
1348 pte_protnone_tests(&args);
1349 pmd_protnone_tests(&args);
1351 pte_devmap_tests(&args);
1352 pmd_devmap_tests(&args);
1353 pud_devmap_tests(&args);
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);
1360 pte_swap_exclusive_tests(&args);
1362 pte_swap_tests(&args);
1363 pmd_swap_tests(&args);
1365 swap_migration_tests(&args);
1367 pmd_thp_tests(&args);
1368 pud_thp_tests(&args);
1370 hugetlb_basic_tests(&args);
1373 * Page table modifying tests. They need to hold
1374 * proper page table lock.
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);
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);
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);
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));
1403 destroy_args(&args);
1406 late_initcall(debug_vm_pgtable);