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