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