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