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>
19 #include <linux/mman.h>
20 #include <linux/mm_types.h>
21 #include <linux/module.h>
22 #include <linux/pfn_t.h>
23 #include <linux/printk.h>
24 #include <linux/pgtable.h>
25 #include <linux/random.h>
26 #include <linux/spinlock.h>
27 #include <linux/swap.h>
28 #include <linux/swapops.h>
29 #include <linux/start_kernel.h>
30 #include <linux/sched/mm.h>
32 #include <asm/pgalloc.h>
33 #include <asm/tlbflush.h>
36 * Please refer Documentation/vm/arch_pgtable_helpers.rst for the semantics
37 * expectations that are being validated here. All future changes in here
38 * or the documentation need to be in sync.
41 #define VMFLAGS (VM_READ|VM_WRITE|VM_EXEC)
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 static void __init pte_basic_tests(unsigned long pfn, int idx)
63 pgprot_t prot = protection_map[idx];
64 pte_t pte = pfn_pte(pfn, prot);
65 unsigned long val = idx, *ptr = &val;
67 pr_debug("Validating PTE basic (%pGv)\n", ptr);
70 * This test needs to be executed after the given page table entry
71 * is created with pfn_pte() to make sure that protection_map[idx]
72 * does not have the dirty bit enabled from the beginning. This is
73 * important for platforms like arm64 where (!PTE_RDONLY) indicate
74 * dirty bit being set.
76 WARN_ON(pte_dirty(pte_wrprotect(pte)));
78 WARN_ON(!pte_same(pte, pte));
79 WARN_ON(!pte_young(pte_mkyoung(pte_mkold(pte))));
80 WARN_ON(!pte_dirty(pte_mkdirty(pte_mkclean(pte))));
81 WARN_ON(!pte_write(pte_mkwrite(pte_wrprotect(pte))));
82 WARN_ON(pte_young(pte_mkold(pte_mkyoung(pte))));
83 WARN_ON(pte_dirty(pte_mkclean(pte_mkdirty(pte))));
84 WARN_ON(pte_write(pte_wrprotect(pte_mkwrite(pte))));
85 WARN_ON(pte_dirty(pte_wrprotect(pte_mkclean(pte))));
86 WARN_ON(!pte_dirty(pte_wrprotect(pte_mkdirty(pte))));
89 static void __init pte_advanced_tests(struct mm_struct *mm,
90 struct vm_area_struct *vma, pte_t *ptep,
91 unsigned long pfn, unsigned long vaddr,
94 pte_t pte = pfn_pte(pfn, prot);
97 * Architectures optimize set_pte_at by avoiding TLB flush.
98 * This requires set_pte_at to be not used to update an
99 * existing pte entry. Clear pte before we do set_pte_at
102 pr_debug("Validating PTE advanced\n");
103 pte = pfn_pte(pfn, prot);
104 set_pte_at(mm, vaddr, ptep, pte);
105 ptep_set_wrprotect(mm, vaddr, ptep);
106 pte = ptep_get(ptep);
107 WARN_ON(pte_write(pte));
108 ptep_get_and_clear(mm, vaddr, ptep);
109 pte = ptep_get(ptep);
110 WARN_ON(!pte_none(pte));
112 pte = pfn_pte(pfn, prot);
113 pte = pte_wrprotect(pte);
114 pte = pte_mkclean(pte);
115 set_pte_at(mm, vaddr, ptep, pte);
116 pte = pte_mkwrite(pte);
117 pte = pte_mkdirty(pte);
118 ptep_set_access_flags(vma, vaddr, ptep, pte, 1);
119 pte = ptep_get(ptep);
120 WARN_ON(!(pte_write(pte) && pte_dirty(pte)));
121 ptep_get_and_clear_full(mm, vaddr, ptep, 1);
122 pte = ptep_get(ptep);
123 WARN_ON(!pte_none(pte));
125 pte = pfn_pte(pfn, prot);
126 pte = pte_mkyoung(pte);
127 set_pte_at(mm, vaddr, ptep, pte);
128 ptep_test_and_clear_young(vma, vaddr, ptep);
129 pte = ptep_get(ptep);
130 WARN_ON(pte_young(pte));
133 static void __init pte_savedwrite_tests(unsigned long pfn, pgprot_t prot)
135 pte_t pte = pfn_pte(pfn, prot);
137 if (!IS_ENABLED(CONFIG_NUMA_BALANCING))
140 pr_debug("Validating PTE saved write\n");
141 WARN_ON(!pte_savedwrite(pte_mk_savedwrite(pte_clear_savedwrite(pte))));
142 WARN_ON(pte_savedwrite(pte_clear_savedwrite(pte_mk_savedwrite(pte))));
145 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
146 static void __init pmd_basic_tests(unsigned long pfn, int idx)
148 pgprot_t prot = protection_map[idx];
149 pmd_t pmd = pfn_pmd(pfn, prot);
150 unsigned long val = idx, *ptr = &val;
152 if (!has_transparent_hugepage())
155 pr_debug("Validating PMD basic (%pGv)\n", ptr);
158 * This test needs to be executed after the given page table entry
159 * is created with pfn_pmd() to make sure that protection_map[idx]
160 * does not have the dirty bit enabled from the beginning. This is
161 * important for platforms like arm64 where (!PTE_RDONLY) indicate
162 * dirty bit being set.
164 WARN_ON(pmd_dirty(pmd_wrprotect(pmd)));
167 WARN_ON(!pmd_same(pmd, pmd));
168 WARN_ON(!pmd_young(pmd_mkyoung(pmd_mkold(pmd))));
169 WARN_ON(!pmd_dirty(pmd_mkdirty(pmd_mkclean(pmd))));
170 WARN_ON(!pmd_write(pmd_mkwrite(pmd_wrprotect(pmd))));
171 WARN_ON(pmd_young(pmd_mkold(pmd_mkyoung(pmd))));
172 WARN_ON(pmd_dirty(pmd_mkclean(pmd_mkdirty(pmd))));
173 WARN_ON(pmd_write(pmd_wrprotect(pmd_mkwrite(pmd))));
174 WARN_ON(pmd_dirty(pmd_wrprotect(pmd_mkclean(pmd))));
175 WARN_ON(!pmd_dirty(pmd_wrprotect(pmd_mkdirty(pmd))));
177 * A huge page does not point to next level page table
178 * entry. Hence this must qualify as pmd_bad().
180 WARN_ON(!pmd_bad(pmd_mkhuge(pmd)));
183 static void __init pmd_advanced_tests(struct mm_struct *mm,
184 struct vm_area_struct *vma, pmd_t *pmdp,
185 unsigned long pfn, unsigned long vaddr,
186 pgprot_t prot, pgtable_t pgtable)
188 pmd_t pmd = pfn_pmd(pfn, prot);
190 if (!has_transparent_hugepage())
193 pr_debug("Validating PMD advanced\n");
194 /* Align the address wrt HPAGE_PMD_SIZE */
195 vaddr = (vaddr & HPAGE_PMD_MASK) + HPAGE_PMD_SIZE;
197 pgtable_trans_huge_deposit(mm, pmdp, pgtable);
199 pmd = pfn_pmd(pfn, prot);
200 set_pmd_at(mm, vaddr, pmdp, pmd);
201 pmdp_set_wrprotect(mm, vaddr, pmdp);
202 pmd = READ_ONCE(*pmdp);
203 WARN_ON(pmd_write(pmd));
204 pmdp_huge_get_and_clear(mm, vaddr, pmdp);
205 pmd = READ_ONCE(*pmdp);
206 WARN_ON(!pmd_none(pmd));
208 pmd = pfn_pmd(pfn, prot);
209 pmd = pmd_wrprotect(pmd);
210 pmd = pmd_mkclean(pmd);
211 set_pmd_at(mm, vaddr, pmdp, pmd);
212 pmd = pmd_mkwrite(pmd);
213 pmd = pmd_mkdirty(pmd);
214 pmdp_set_access_flags(vma, vaddr, pmdp, pmd, 1);
215 pmd = READ_ONCE(*pmdp);
216 WARN_ON(!(pmd_write(pmd) && pmd_dirty(pmd)));
217 pmdp_huge_get_and_clear_full(vma, vaddr, pmdp, 1);
218 pmd = READ_ONCE(*pmdp);
219 WARN_ON(!pmd_none(pmd));
221 pmd = pmd_mkhuge(pfn_pmd(pfn, prot));
222 pmd = pmd_mkyoung(pmd);
223 set_pmd_at(mm, vaddr, pmdp, pmd);
224 pmdp_test_and_clear_young(vma, vaddr, pmdp);
225 pmd = READ_ONCE(*pmdp);
226 WARN_ON(pmd_young(pmd));
228 /* Clear the pte entries */
229 pmdp_huge_get_and_clear(mm, vaddr, pmdp);
230 pgtable = pgtable_trans_huge_withdraw(mm, pmdp);
233 static void __init pmd_leaf_tests(unsigned long pfn, pgprot_t prot)
235 pmd_t pmd = pfn_pmd(pfn, prot);
237 pr_debug("Validating PMD leaf\n");
239 * PMD based THP is a leaf entry.
241 pmd = pmd_mkhuge(pmd);
242 WARN_ON(!pmd_leaf(pmd));
245 #ifdef CONFIG_HAVE_ARCH_HUGE_VMAP
246 static void __init pmd_huge_tests(pmd_t *pmdp, unsigned long pfn, pgprot_t prot)
250 if (!arch_vmap_pmd_supported(prot))
253 pr_debug("Validating PMD huge\n");
255 * X86 defined pmd_set_huge() verifies that the given
256 * PMD is not a populated non-leaf entry.
258 WRITE_ONCE(*pmdp, __pmd(0));
259 WARN_ON(!pmd_set_huge(pmdp, __pfn_to_phys(pfn), prot));
260 WARN_ON(!pmd_clear_huge(pmdp));
261 pmd = READ_ONCE(*pmdp);
262 WARN_ON(!pmd_none(pmd));
264 #else /* CONFIG_HAVE_ARCH_HUGE_VMAP */
265 static void __init pmd_huge_tests(pmd_t *pmdp, unsigned long pfn, pgprot_t prot) { }
266 #endif /* CONFIG_HAVE_ARCH_HUGE_VMAP */
268 static void __init pmd_savedwrite_tests(unsigned long pfn, pgprot_t prot)
270 pmd_t pmd = pfn_pmd(pfn, prot);
272 if (!IS_ENABLED(CONFIG_NUMA_BALANCING))
275 pr_debug("Validating PMD saved write\n");
276 WARN_ON(!pmd_savedwrite(pmd_mk_savedwrite(pmd_clear_savedwrite(pmd))));
277 WARN_ON(pmd_savedwrite(pmd_clear_savedwrite(pmd_mk_savedwrite(pmd))));
280 #ifdef CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD
281 static void __init pud_basic_tests(struct mm_struct *mm, unsigned long pfn, int idx)
283 pgprot_t prot = protection_map[idx];
284 pud_t pud = pfn_pud(pfn, prot);
285 unsigned long val = idx, *ptr = &val;
287 if (!has_transparent_hugepage())
290 pr_debug("Validating PUD basic (%pGv)\n", ptr);
293 * This test needs to be executed after the given page table entry
294 * is created with pfn_pud() to make sure that protection_map[idx]
295 * does not have the dirty bit enabled from the beginning. This is
296 * important for platforms like arm64 where (!PTE_RDONLY) indicate
297 * dirty bit being set.
299 WARN_ON(pud_dirty(pud_wrprotect(pud)));
301 WARN_ON(!pud_same(pud, pud));
302 WARN_ON(!pud_young(pud_mkyoung(pud_mkold(pud))));
303 WARN_ON(!pud_dirty(pud_mkdirty(pud_mkclean(pud))));
304 WARN_ON(pud_dirty(pud_mkclean(pud_mkdirty(pud))));
305 WARN_ON(!pud_write(pud_mkwrite(pud_wrprotect(pud))));
306 WARN_ON(pud_write(pud_wrprotect(pud_mkwrite(pud))));
307 WARN_ON(pud_young(pud_mkold(pud_mkyoung(pud))));
308 WARN_ON(pud_dirty(pud_wrprotect(pud_mkclean(pud))));
309 WARN_ON(!pud_dirty(pud_wrprotect(pud_mkdirty(pud))));
311 if (mm_pmd_folded(mm))
315 * A huge page does not point to next level page table
316 * entry. Hence this must qualify as pud_bad().
318 WARN_ON(!pud_bad(pud_mkhuge(pud)));
321 static void __init pud_advanced_tests(struct mm_struct *mm,
322 struct vm_area_struct *vma, pud_t *pudp,
323 unsigned long pfn, unsigned long vaddr,
326 pud_t pud = pfn_pud(pfn, prot);
328 if (!has_transparent_hugepage())
331 pr_debug("Validating PUD advanced\n");
332 /* Align the address wrt HPAGE_PUD_SIZE */
333 vaddr = (vaddr & HPAGE_PUD_MASK) + HPAGE_PUD_SIZE;
335 set_pud_at(mm, vaddr, pudp, pud);
336 pudp_set_wrprotect(mm, vaddr, pudp);
337 pud = READ_ONCE(*pudp);
338 WARN_ON(pud_write(pud));
340 #ifndef __PAGETABLE_PMD_FOLDED
341 pudp_huge_get_and_clear(mm, vaddr, pudp);
342 pud = READ_ONCE(*pudp);
343 WARN_ON(!pud_none(pud));
344 #endif /* __PAGETABLE_PMD_FOLDED */
345 pud = pfn_pud(pfn, prot);
346 pud = pud_wrprotect(pud);
347 pud = pud_mkclean(pud);
348 set_pud_at(mm, vaddr, pudp, pud);
349 pud = pud_mkwrite(pud);
350 pud = pud_mkdirty(pud);
351 pudp_set_access_flags(vma, vaddr, pudp, pud, 1);
352 pud = READ_ONCE(*pudp);
353 WARN_ON(!(pud_write(pud) && pud_dirty(pud)));
355 #ifndef __PAGETABLE_PMD_FOLDED
356 pudp_huge_get_and_clear_full(mm, vaddr, pudp, 1);
357 pud = READ_ONCE(*pudp);
358 WARN_ON(!pud_none(pud));
359 #endif /* __PAGETABLE_PMD_FOLDED */
361 pud = pfn_pud(pfn, prot);
362 pud = pud_mkyoung(pud);
363 set_pud_at(mm, vaddr, pudp, pud);
364 pudp_test_and_clear_young(vma, vaddr, pudp);
365 pud = READ_ONCE(*pudp);
366 WARN_ON(pud_young(pud));
368 pudp_huge_get_and_clear(mm, vaddr, pudp);
371 static void __init pud_leaf_tests(unsigned long pfn, pgprot_t prot)
373 pud_t pud = pfn_pud(pfn, prot);
375 pr_debug("Validating PUD leaf\n");
377 * PUD based THP is a leaf entry.
379 pud = pud_mkhuge(pud);
380 WARN_ON(!pud_leaf(pud));
383 #ifdef CONFIG_HAVE_ARCH_HUGE_VMAP
384 static void __init pud_huge_tests(pud_t *pudp, unsigned long pfn, pgprot_t prot)
388 if (!arch_vmap_pud_supported(prot))
391 pr_debug("Validating PUD huge\n");
393 * X86 defined pud_set_huge() verifies that the given
394 * PUD is not a populated non-leaf entry.
396 WRITE_ONCE(*pudp, __pud(0));
397 WARN_ON(!pud_set_huge(pudp, __pfn_to_phys(pfn), prot));
398 WARN_ON(!pud_clear_huge(pudp));
399 pud = READ_ONCE(*pudp);
400 WARN_ON(!pud_none(pud));
402 #else /* !CONFIG_HAVE_ARCH_HUGE_VMAP */
403 static void __init pud_huge_tests(pud_t *pudp, unsigned long pfn, pgprot_t prot) { }
404 #endif /* !CONFIG_HAVE_ARCH_HUGE_VMAP */
406 #else /* !CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD */
407 static void __init pud_basic_tests(struct mm_struct *mm, unsigned long pfn, int idx) { }
408 static void __init pud_advanced_tests(struct mm_struct *mm,
409 struct vm_area_struct *vma, pud_t *pudp,
410 unsigned long pfn, unsigned long vaddr,
414 static void __init pud_leaf_tests(unsigned long pfn, pgprot_t prot) { }
415 static void __init pud_huge_tests(pud_t *pudp, unsigned long pfn, pgprot_t prot)
418 #endif /* CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD */
419 #else /* !CONFIG_TRANSPARENT_HUGEPAGE */
420 static void __init pmd_basic_tests(unsigned long pfn, int idx) { }
421 static void __init pud_basic_tests(struct mm_struct *mm, unsigned long pfn, int idx) { }
422 static void __init pmd_advanced_tests(struct mm_struct *mm,
423 struct vm_area_struct *vma, pmd_t *pmdp,
424 unsigned long pfn, unsigned long vaddr,
425 pgprot_t prot, pgtable_t pgtable)
428 static void __init pud_advanced_tests(struct mm_struct *mm,
429 struct vm_area_struct *vma, pud_t *pudp,
430 unsigned long pfn, unsigned long vaddr,
434 static void __init pmd_leaf_tests(unsigned long pfn, pgprot_t prot) { }
435 static void __init pud_leaf_tests(unsigned long pfn, pgprot_t prot) { }
436 static void __init pmd_huge_tests(pmd_t *pmdp, unsigned long pfn, pgprot_t prot)
439 static void __init pud_huge_tests(pud_t *pudp, unsigned long pfn, pgprot_t prot)
442 static void __init pmd_savedwrite_tests(unsigned long pfn, pgprot_t prot) { }
443 #endif /* CONFIG_TRANSPARENT_HUGEPAGE */
445 static void __init p4d_basic_tests(unsigned long pfn, pgprot_t prot)
449 pr_debug("Validating P4D basic\n");
450 memset(&p4d, RANDOM_NZVALUE, sizeof(p4d_t));
451 WARN_ON(!p4d_same(p4d, p4d));
454 static void __init pgd_basic_tests(unsigned long pfn, pgprot_t prot)
458 pr_debug("Validating PGD basic\n");
459 memset(&pgd, RANDOM_NZVALUE, sizeof(pgd_t));
460 WARN_ON(!pgd_same(pgd, pgd));
463 #ifndef __PAGETABLE_PUD_FOLDED
464 static void __init pud_clear_tests(struct mm_struct *mm, pud_t *pudp)
466 pud_t pud = READ_ONCE(*pudp);
468 if (mm_pmd_folded(mm))
471 pr_debug("Validating PUD clear\n");
472 pud = __pud(pud_val(pud) | RANDOM_ORVALUE);
473 WRITE_ONCE(*pudp, pud);
475 pud = READ_ONCE(*pudp);
476 WARN_ON(!pud_none(pud));
479 static void __init pud_populate_tests(struct mm_struct *mm, pud_t *pudp,
484 if (mm_pmd_folded(mm))
487 pr_debug("Validating PUD populate\n");
489 * This entry points to next level page table page.
490 * Hence this must not qualify as pud_bad().
492 pud_populate(mm, pudp, pmdp);
493 pud = READ_ONCE(*pudp);
494 WARN_ON(pud_bad(pud));
496 #else /* !__PAGETABLE_PUD_FOLDED */
497 static void __init pud_clear_tests(struct mm_struct *mm, pud_t *pudp) { }
498 static void __init pud_populate_tests(struct mm_struct *mm, pud_t *pudp,
502 #endif /* PAGETABLE_PUD_FOLDED */
504 #ifndef __PAGETABLE_P4D_FOLDED
505 static void __init p4d_clear_tests(struct mm_struct *mm, p4d_t *p4dp)
507 p4d_t p4d = READ_ONCE(*p4dp);
509 if (mm_pud_folded(mm))
512 pr_debug("Validating P4D clear\n");
513 p4d = __p4d(p4d_val(p4d) | RANDOM_ORVALUE);
514 WRITE_ONCE(*p4dp, p4d);
516 p4d = READ_ONCE(*p4dp);
517 WARN_ON(!p4d_none(p4d));
520 static void __init p4d_populate_tests(struct mm_struct *mm, p4d_t *p4dp,
525 if (mm_pud_folded(mm))
528 pr_debug("Validating P4D populate\n");
530 * This entry points to next level page table page.
531 * Hence this must not qualify as p4d_bad().
535 p4d_populate(mm, p4dp, pudp);
536 p4d = READ_ONCE(*p4dp);
537 WARN_ON(p4d_bad(p4d));
540 static void __init pgd_clear_tests(struct mm_struct *mm, pgd_t *pgdp)
542 pgd_t pgd = READ_ONCE(*pgdp);
544 if (mm_p4d_folded(mm))
547 pr_debug("Validating PGD clear\n");
548 pgd = __pgd(pgd_val(pgd) | RANDOM_ORVALUE);
549 WRITE_ONCE(*pgdp, pgd);
551 pgd = READ_ONCE(*pgdp);
552 WARN_ON(!pgd_none(pgd));
555 static void __init pgd_populate_tests(struct mm_struct *mm, pgd_t *pgdp,
560 if (mm_p4d_folded(mm))
563 pr_debug("Validating PGD populate\n");
565 * This entry points to next level page table page.
566 * Hence this must not qualify as pgd_bad().
570 pgd_populate(mm, pgdp, p4dp);
571 pgd = READ_ONCE(*pgdp);
572 WARN_ON(pgd_bad(pgd));
574 #else /* !__PAGETABLE_P4D_FOLDED */
575 static void __init p4d_clear_tests(struct mm_struct *mm, p4d_t *p4dp) { }
576 static void __init pgd_clear_tests(struct mm_struct *mm, pgd_t *pgdp) { }
577 static void __init p4d_populate_tests(struct mm_struct *mm, p4d_t *p4dp,
581 static void __init pgd_populate_tests(struct mm_struct *mm, pgd_t *pgdp,
585 #endif /* PAGETABLE_P4D_FOLDED */
587 static void __init pte_clear_tests(struct mm_struct *mm, pte_t *ptep,
588 unsigned long pfn, unsigned long vaddr,
591 pte_t pte = pfn_pte(pfn, prot);
593 pr_debug("Validating PTE clear\n");
595 pte = __pte(pte_val(pte) | RANDOM_ORVALUE);
597 set_pte_at(mm, vaddr, ptep, pte);
599 pte_clear(mm, vaddr, ptep);
600 pte = ptep_get(ptep);
601 WARN_ON(!pte_none(pte));
604 static void __init pmd_clear_tests(struct mm_struct *mm, pmd_t *pmdp)
606 pmd_t pmd = READ_ONCE(*pmdp);
608 pr_debug("Validating PMD clear\n");
609 pmd = __pmd(pmd_val(pmd) | RANDOM_ORVALUE);
610 WRITE_ONCE(*pmdp, pmd);
612 pmd = READ_ONCE(*pmdp);
613 WARN_ON(!pmd_none(pmd));
616 static void __init pmd_populate_tests(struct mm_struct *mm, pmd_t *pmdp,
621 pr_debug("Validating PMD populate\n");
623 * This entry points to next level page table page.
624 * Hence this must not qualify as pmd_bad().
626 pmd_populate(mm, pmdp, pgtable);
627 pmd = READ_ONCE(*pmdp);
628 WARN_ON(pmd_bad(pmd));
631 static void __init pte_special_tests(unsigned long pfn, pgprot_t prot)
633 pte_t pte = pfn_pte(pfn, prot);
635 if (!IS_ENABLED(CONFIG_ARCH_HAS_PTE_SPECIAL))
638 pr_debug("Validating PTE special\n");
639 WARN_ON(!pte_special(pte_mkspecial(pte)));
642 static void __init pte_protnone_tests(unsigned long pfn, pgprot_t prot)
644 pte_t pte = pfn_pte(pfn, prot);
646 if (!IS_ENABLED(CONFIG_NUMA_BALANCING))
649 pr_debug("Validating PTE protnone\n");
650 WARN_ON(!pte_protnone(pte));
651 WARN_ON(!pte_present(pte));
654 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
655 static void __init pmd_protnone_tests(unsigned long pfn, pgprot_t prot)
657 pmd_t pmd = pmd_mkhuge(pfn_pmd(pfn, prot));
659 if (!IS_ENABLED(CONFIG_NUMA_BALANCING))
662 pr_debug("Validating PMD protnone\n");
663 WARN_ON(!pmd_protnone(pmd));
664 WARN_ON(!pmd_present(pmd));
666 #else /* !CONFIG_TRANSPARENT_HUGEPAGE */
667 static void __init pmd_protnone_tests(unsigned long pfn, pgprot_t prot) { }
668 #endif /* CONFIG_TRANSPARENT_HUGEPAGE */
670 #ifdef CONFIG_ARCH_HAS_PTE_DEVMAP
671 static void __init pte_devmap_tests(unsigned long pfn, pgprot_t prot)
673 pte_t pte = pfn_pte(pfn, prot);
675 pr_debug("Validating PTE devmap\n");
676 WARN_ON(!pte_devmap(pte_mkdevmap(pte)));
679 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
680 static void __init pmd_devmap_tests(unsigned long pfn, pgprot_t prot)
682 pmd_t pmd = pfn_pmd(pfn, prot);
684 pr_debug("Validating PMD devmap\n");
685 WARN_ON(!pmd_devmap(pmd_mkdevmap(pmd)));
688 #ifdef CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD
689 static void __init pud_devmap_tests(unsigned long pfn, pgprot_t prot)
691 pud_t pud = pfn_pud(pfn, prot);
693 pr_debug("Validating PUD devmap\n");
694 WARN_ON(!pud_devmap(pud_mkdevmap(pud)));
696 #else /* !CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD */
697 static void __init pud_devmap_tests(unsigned long pfn, pgprot_t prot) { }
698 #endif /* CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD */
699 #else /* CONFIG_TRANSPARENT_HUGEPAGE */
700 static void __init pmd_devmap_tests(unsigned long pfn, pgprot_t prot) { }
701 static void __init pud_devmap_tests(unsigned long pfn, pgprot_t prot) { }
702 #endif /* CONFIG_TRANSPARENT_HUGEPAGE */
704 static void __init pte_devmap_tests(unsigned long pfn, pgprot_t prot) { }
705 static void __init pmd_devmap_tests(unsigned long pfn, pgprot_t prot) { }
706 static void __init pud_devmap_tests(unsigned long pfn, pgprot_t prot) { }
707 #endif /* CONFIG_ARCH_HAS_PTE_DEVMAP */
709 static void __init pte_soft_dirty_tests(unsigned long pfn, pgprot_t prot)
711 pte_t pte = pfn_pte(pfn, prot);
713 if (!IS_ENABLED(CONFIG_MEM_SOFT_DIRTY))
716 pr_debug("Validating PTE soft dirty\n");
717 WARN_ON(!pte_soft_dirty(pte_mksoft_dirty(pte)));
718 WARN_ON(pte_soft_dirty(pte_clear_soft_dirty(pte)));
721 static void __init pte_swap_soft_dirty_tests(unsigned long pfn, pgprot_t prot)
723 pte_t pte = pfn_pte(pfn, prot);
725 if (!IS_ENABLED(CONFIG_MEM_SOFT_DIRTY))
728 pr_debug("Validating PTE swap soft dirty\n");
729 WARN_ON(!pte_swp_soft_dirty(pte_swp_mksoft_dirty(pte)));
730 WARN_ON(pte_swp_soft_dirty(pte_swp_clear_soft_dirty(pte)));
733 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
734 static void __init pmd_soft_dirty_tests(unsigned long pfn, pgprot_t prot)
736 pmd_t pmd = pfn_pmd(pfn, prot);
738 if (!IS_ENABLED(CONFIG_MEM_SOFT_DIRTY))
741 pr_debug("Validating PMD soft dirty\n");
742 WARN_ON(!pmd_soft_dirty(pmd_mksoft_dirty(pmd)));
743 WARN_ON(pmd_soft_dirty(pmd_clear_soft_dirty(pmd)));
746 static void __init pmd_swap_soft_dirty_tests(unsigned long pfn, pgprot_t prot)
748 pmd_t pmd = pfn_pmd(pfn, prot);
750 if (!IS_ENABLED(CONFIG_MEM_SOFT_DIRTY) ||
751 !IS_ENABLED(CONFIG_ARCH_ENABLE_THP_MIGRATION))
754 pr_debug("Validating PMD swap soft dirty\n");
755 WARN_ON(!pmd_swp_soft_dirty(pmd_swp_mksoft_dirty(pmd)));
756 WARN_ON(pmd_swp_soft_dirty(pmd_swp_clear_soft_dirty(pmd)));
758 #else /* !CONFIG_ARCH_HAS_PTE_DEVMAP */
759 static void __init pmd_soft_dirty_tests(unsigned long pfn, pgprot_t prot) { }
760 static void __init pmd_swap_soft_dirty_tests(unsigned long pfn, pgprot_t prot)
763 #endif /* CONFIG_ARCH_HAS_PTE_DEVMAP */
765 static void __init pte_swap_tests(unsigned long pfn, pgprot_t prot)
770 pr_debug("Validating PTE swap\n");
771 pte = pfn_pte(pfn, prot);
772 swp = __pte_to_swp_entry(pte);
773 pte = __swp_entry_to_pte(swp);
774 WARN_ON(pfn != pte_pfn(pte));
777 #ifdef CONFIG_ARCH_ENABLE_THP_MIGRATION
778 static void __init pmd_swap_tests(unsigned long pfn, pgprot_t prot)
783 pr_debug("Validating PMD swap\n");
784 pmd = pfn_pmd(pfn, prot);
785 swp = __pmd_to_swp_entry(pmd);
786 pmd = __swp_entry_to_pmd(swp);
787 WARN_ON(pfn != pmd_pfn(pmd));
789 #else /* !CONFIG_ARCH_ENABLE_THP_MIGRATION */
790 static void __init pmd_swap_tests(unsigned long pfn, pgprot_t prot) { }
791 #endif /* CONFIG_ARCH_ENABLE_THP_MIGRATION */
793 static void __init swap_migration_tests(void)
798 if (!IS_ENABLED(CONFIG_MIGRATION))
801 pr_debug("Validating swap migration\n");
803 * swap_migration_tests() requires a dedicated page as it needs to
804 * be locked before creating a migration entry from it. Locking the
805 * page that actually maps kernel text ('start_kernel') can be real
806 * problematic. Lets allocate a dedicated page explicitly for this
807 * purpose that will be freed subsequently.
809 page = alloc_page(GFP_KERNEL);
811 pr_err("page allocation failed\n");
816 * make_migration_entry() expects given page to be
817 * locked, otherwise it stumbles upon a BUG_ON().
819 __SetPageLocked(page);
820 swp = make_migration_entry(page, 1);
821 WARN_ON(!is_migration_entry(swp));
822 WARN_ON(!is_write_migration_entry(swp));
824 make_migration_entry_read(&swp);
825 WARN_ON(!is_migration_entry(swp));
826 WARN_ON(is_write_migration_entry(swp));
828 swp = make_migration_entry(page, 0);
829 WARN_ON(!is_migration_entry(swp));
830 WARN_ON(is_write_migration_entry(swp));
831 __ClearPageLocked(page);
835 #ifdef CONFIG_HUGETLB_PAGE
836 static void __init hugetlb_basic_tests(unsigned long pfn, pgprot_t prot)
841 pr_debug("Validating HugeTLB basic\n");
843 * Accessing the page associated with the pfn is safe here,
844 * as it was previously derived from a real kernel symbol.
846 page = pfn_to_page(pfn);
847 pte = mk_huge_pte(page, prot);
849 WARN_ON(!huge_pte_dirty(huge_pte_mkdirty(pte)));
850 WARN_ON(!huge_pte_write(huge_pte_mkwrite(huge_pte_wrprotect(pte))));
851 WARN_ON(huge_pte_write(huge_pte_wrprotect(huge_pte_mkwrite(pte))));
853 #ifdef CONFIG_ARCH_WANT_GENERAL_HUGETLB
854 pte = pfn_pte(pfn, prot);
856 WARN_ON(!pte_huge(pte_mkhuge(pte)));
857 #endif /* CONFIG_ARCH_WANT_GENERAL_HUGETLB */
859 #else /* !CONFIG_HUGETLB_PAGE */
860 static void __init hugetlb_basic_tests(unsigned long pfn, pgprot_t prot) { }
861 #endif /* CONFIG_HUGETLB_PAGE */
863 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
864 static void __init pmd_thp_tests(unsigned long pfn, pgprot_t prot)
868 if (!has_transparent_hugepage())
871 pr_debug("Validating PMD based THP\n");
873 * pmd_trans_huge() and pmd_present() must return positive after
874 * MMU invalidation with pmd_mkinvalid(). This behavior is an
875 * optimization for transparent huge page. pmd_trans_huge() must
876 * be true if pmd_page() returns a valid THP to avoid taking the
877 * pmd_lock when others walk over non transhuge pmds (i.e. there
878 * are no THP allocated). Especially when splitting a THP and
879 * removing the present bit from the pmd, pmd_trans_huge() still
880 * needs to return true. pmd_present() should be true whenever
881 * pmd_trans_huge() returns true.
883 pmd = pfn_pmd(pfn, prot);
884 WARN_ON(!pmd_trans_huge(pmd_mkhuge(pmd)));
886 #ifndef __HAVE_ARCH_PMDP_INVALIDATE
887 WARN_ON(!pmd_trans_huge(pmd_mkinvalid(pmd_mkhuge(pmd))));
888 WARN_ON(!pmd_present(pmd_mkinvalid(pmd_mkhuge(pmd))));
889 #endif /* __HAVE_ARCH_PMDP_INVALIDATE */
892 #ifdef CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD
893 static void __init pud_thp_tests(unsigned long pfn, pgprot_t prot)
897 if (!has_transparent_hugepage())
900 pr_debug("Validating PUD based THP\n");
901 pud = pfn_pud(pfn, prot);
902 WARN_ON(!pud_trans_huge(pud_mkhuge(pud)));
905 * pud_mkinvalid() has been dropped for now. Enable back
906 * these tests when it comes back with a modified pud_present().
908 * WARN_ON(!pud_trans_huge(pud_mkinvalid(pud_mkhuge(pud))));
909 * WARN_ON(!pud_present(pud_mkinvalid(pud_mkhuge(pud))));
912 #else /* !CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD */
913 static void __init pud_thp_tests(unsigned long pfn, pgprot_t prot) { }
914 #endif /* CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD */
915 #else /* !CONFIG_TRANSPARENT_HUGEPAGE */
916 static void __init pmd_thp_tests(unsigned long pfn, pgprot_t prot) { }
917 static void __init pud_thp_tests(unsigned long pfn, pgprot_t prot) { }
918 #endif /* CONFIG_TRANSPARENT_HUGEPAGE */
920 static unsigned long __init get_random_vaddr(void)
922 unsigned long random_vaddr, random_pages, total_user_pages;
924 total_user_pages = (TASK_SIZE - FIRST_USER_ADDRESS) / PAGE_SIZE;
926 random_pages = get_random_long() % total_user_pages;
927 random_vaddr = FIRST_USER_ADDRESS + random_pages * PAGE_SIZE;
932 static int __init debug_vm_pgtable(void)
934 struct vm_area_struct *vma;
935 struct mm_struct *mm;
937 p4d_t *p4dp, *saved_p4dp;
938 pud_t *pudp, *saved_pudp;
939 pmd_t *pmdp, *saved_pmdp, pmd;
941 pgtable_t saved_ptep;
942 pgprot_t prot, protnone;
944 unsigned long vaddr, pte_aligned, pmd_aligned;
945 unsigned long pud_aligned, p4d_aligned, pgd_aligned;
946 spinlock_t *ptl = NULL;
949 pr_info("Validating architecture page table helpers\n");
950 prot = vm_get_page_prot(VMFLAGS);
951 vaddr = get_random_vaddr();
954 pr_err("mm_struct allocation failed\n");
959 * __P000 (or even __S000) will help create page table entries with
960 * PROT_NONE permission as required for pxx_protnone_tests().
964 vma = vm_area_alloc(mm);
966 pr_err("vma allocation failed\n");
971 * PFN for mapping at PTE level is determined from a standard kernel
972 * text symbol. But pfns for higher page table levels are derived by
973 * masking lower bits of this real pfn. These derived pfns might not
974 * exist on the platform but that does not really matter as pfn_pxx()
975 * helpers will still create appropriate entries for the test. This
976 * helps avoid large memory block allocations to be used for mapping
977 * at higher page table levels.
979 paddr = __pa_symbol(&start_kernel);
981 pte_aligned = (paddr & PAGE_MASK) >> PAGE_SHIFT;
982 pmd_aligned = (paddr & PMD_MASK) >> PAGE_SHIFT;
983 pud_aligned = (paddr & PUD_MASK) >> PAGE_SHIFT;
984 p4d_aligned = (paddr & P4D_MASK) >> PAGE_SHIFT;
985 pgd_aligned = (paddr & PGDIR_MASK) >> PAGE_SHIFT;
986 WARN_ON(!pfn_valid(pte_aligned));
988 pgdp = pgd_offset(mm, vaddr);
989 p4dp = p4d_alloc(mm, pgdp, vaddr);
990 pudp = pud_alloc(mm, p4dp, vaddr);
991 pmdp = pmd_alloc(mm, pudp, vaddr);
995 if (pte_alloc(mm, pmdp)) {
996 pr_err("pgtable allocation failed\n");
1001 * Save all the page table page addresses as the page table
1002 * entries will be used for testing with random or garbage
1003 * values. These saved addresses will be used for freeing
1006 pmd = READ_ONCE(*pmdp);
1007 saved_p4dp = p4d_offset(pgdp, 0UL);
1008 saved_pudp = pud_offset(p4dp, 0UL);
1009 saved_pmdp = pmd_offset(pudp, 0UL);
1010 saved_ptep = pmd_pgtable(pmd);
1013 * Iterate over the protection_map[] to make sure that all
1014 * the basic page table transformation validations just hold
1015 * true irrespective of the starting protection value for a
1016 * given page table entry.
1018 for (idx = 0; idx < ARRAY_SIZE(protection_map); idx++) {
1019 pte_basic_tests(pte_aligned, idx);
1020 pmd_basic_tests(pmd_aligned, idx);
1021 pud_basic_tests(mm, pud_aligned, idx);
1025 * Both P4D and PGD level tests are very basic which do not
1026 * involve creating page table entries from the protection
1027 * value and the given pfn. Hence just keep them out from
1028 * the above iteration for now to save some test execution
1031 p4d_basic_tests(p4d_aligned, prot);
1032 pgd_basic_tests(pgd_aligned, prot);
1034 pmd_leaf_tests(pmd_aligned, prot);
1035 pud_leaf_tests(pud_aligned, prot);
1037 pte_savedwrite_tests(pte_aligned, protnone);
1038 pmd_savedwrite_tests(pmd_aligned, protnone);
1040 pte_special_tests(pte_aligned, prot);
1041 pte_protnone_tests(pte_aligned, protnone);
1042 pmd_protnone_tests(pmd_aligned, protnone);
1044 pte_devmap_tests(pte_aligned, prot);
1045 pmd_devmap_tests(pmd_aligned, prot);
1046 pud_devmap_tests(pud_aligned, prot);
1048 pte_soft_dirty_tests(pte_aligned, prot);
1049 pmd_soft_dirty_tests(pmd_aligned, prot);
1050 pte_swap_soft_dirty_tests(pte_aligned, prot);
1051 pmd_swap_soft_dirty_tests(pmd_aligned, prot);
1053 pte_swap_tests(pte_aligned, prot);
1054 pmd_swap_tests(pmd_aligned, prot);
1056 swap_migration_tests();
1058 pmd_thp_tests(pmd_aligned, prot);
1059 pud_thp_tests(pud_aligned, prot);
1061 hugetlb_basic_tests(pte_aligned, prot);
1064 * Page table modifying tests. They need to hold
1065 * proper page table lock.
1068 ptep = pte_offset_map_lock(mm, pmdp, vaddr, &ptl);
1069 pte_clear_tests(mm, ptep, pte_aligned, vaddr, prot);
1070 pte_advanced_tests(mm, vma, ptep, pte_aligned, vaddr, prot);
1071 pte_unmap_unlock(ptep, ptl);
1073 ptl = pmd_lock(mm, pmdp);
1074 pmd_clear_tests(mm, pmdp);
1075 pmd_advanced_tests(mm, vma, pmdp, pmd_aligned, vaddr, prot, saved_ptep);
1076 pmd_huge_tests(pmdp, pmd_aligned, prot);
1077 pmd_populate_tests(mm, pmdp, saved_ptep);
1080 ptl = pud_lock(mm, pudp);
1081 pud_clear_tests(mm, pudp);
1082 pud_advanced_tests(mm, vma, pudp, pud_aligned, vaddr, prot);
1083 pud_huge_tests(pudp, pud_aligned, prot);
1084 pud_populate_tests(mm, pudp, saved_pmdp);
1087 spin_lock(&mm->page_table_lock);
1088 p4d_clear_tests(mm, p4dp);
1089 pgd_clear_tests(mm, pgdp);
1090 p4d_populate_tests(mm, p4dp, saved_pudp);
1091 pgd_populate_tests(mm, pgdp, saved_p4dp);
1092 spin_unlock(&mm->page_table_lock);
1094 p4d_free(mm, saved_p4dp);
1095 pud_free(mm, saved_pudp);
1096 pmd_free(mm, saved_pmdp);
1097 pte_free(mm, saved_ptep);
1106 late_initcall(debug_vm_pgtable);