Commit | Line | Data |
---|---|---|
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 |
60 | struct 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 | 92 | static 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 | 120 | static 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 | 177 | static 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 | 215 | static 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 | 278 | static 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 | 296 | static 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 | 337 | static 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 | 401 | static 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 | 417 | static void __init pud_basic_tests(struct pgtable_debug_args *args, int idx) { } |
4cbde03b | 418 | static void __init pud_advanced_tests(struct pgtable_debug_args *args) { } |
8983d231 | 419 | static 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 |
422 | static void __init pmd_basic_tests(struct pgtable_debug_args *args, int idx) { } |
423 | static void __init pud_basic_tests(struct pgtable_debug_args *args, int idx) { } | |
c0fe07b0 | 424 | static void __init pmd_advanced_tests(struct pgtable_debug_args *args) { } |
4cbde03b | 425 | static void __init pud_advanced_tests(struct pgtable_debug_args *args) { } |
8983d231 GS |
426 | static void __init pmd_leaf_tests(struct pgtable_debug_args *args) { } |
427 | static 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 | 431 | static 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 | 451 | static 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 | 471 | static void __init pmd_huge_tests(struct pgtable_debug_args *args) { } |
4cbde03b | 472 | static void __init pud_huge_tests(struct pgtable_debug_args *args) { } |
5fe77be6 | 473 | #endif /* CONFIG_HAVE_ARCH_HUGE_VMAP */ |
399145f9 | 474 | |
36b77d1e | 475 | static 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 | 484 | static 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 | 494 | static 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 | 509 | static 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 |
526 | static void __init pud_clear_tests(struct pgtable_debug_args *args) { } |
527 | static void __init pud_populate_tests(struct pgtable_debug_args *args) { } | |
399145f9 AK |
528 | #endif /* PAGETABLE_PUD_FOLDED */ |
529 | ||
530 | #ifndef __PAGETABLE_P4D_FOLDED | |
2f87f8c3 | 531 | static 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 | 546 | static 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 | 565 | static 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 | 580 | static 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 |
599 | static void __init p4d_clear_tests(struct pgtable_debug_args *args) { } |
600 | static void __init pgd_clear_tests(struct pgtable_debug_args *args) { } | |
601 | static void __init p4d_populate_tests(struct pgtable_debug_args *args) { } | |
602 | static void __init pgd_populate_tests(struct pgtable_debug_args *args) { } | |
399145f9 AK |
603 | #endif /* PAGETABLE_P4D_FOLDED */ |
604 | ||
44966c44 | 605 | static 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 | 633 | static 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 | 645 | static 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 | 659 | static 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 | 670 | static 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 | 683 | static 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 | 699 | static 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 | 703 | static 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 | 712 | static 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 | 725 | static 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 | 737 | static 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 |
740 | static void __init pmd_devmap_tests(struct pgtable_debug_args *args) { } |
741 | static void __init pud_devmap_tests(struct pgtable_debug_args *args) { } | |
05289402 AK |
742 | #endif /* CONFIG_TRANSPARENT_HUGEPAGE */ |
743 | #else | |
8cb183f2 GS |
744 | static void __init pte_devmap_tests(struct pgtable_debug_args *args) { } |
745 | static void __init pmd_devmap_tests(struct pgtable_debug_args *args) { } | |
746 | static void __init pud_devmap_tests(struct pgtable_debug_args *args) { } | |
05289402 AK |
747 | #endif /* CONFIG_ARCH_HAS_PTE_DEVMAP */ |
748 | ||
5f447e80 | 749 | static 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 | 761 | static 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 | 774 | static 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 | 790 | static 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 |
807 | static void __init pmd_soft_dirty_tests(struct pgtable_debug_args *args) { } |
808 | static void __init pmd_swap_soft_dirty_tests(struct pgtable_debug_args *args) { } | |
b593b90d | 809 | #endif /* CONFIG_TRANSPARENT_HUGEPAGE */ |
05289402 | 810 | |
210d1e8a DH |
811 | static void __init pte_swap_exclusive_tests(struct pgtable_debug_args *args) |
812 | { | |
2321ba3e DH |
813 | unsigned long max_swap_offset; |
814 | swp_entry_t entry, entry2; | |
815 | pte_t pte; | |
210d1e8a DH |
816 | |
817 | pr_debug("Validating PTE swap exclusive\n"); | |
2321ba3e DH |
818 | |
819 | /* See generic_max_swapfile_size(): probe the maximum offset */ | |
820 | max_swap_offset = swp_offset(pte_to_swp_entry(swp_entry_to_pte(swp_entry(0, ~0UL)))); | |
821 | ||
822 | /* Create a swp entry with all possible bits set */ | |
823 | entry = swp_entry((1 << MAX_SWAPFILES_SHIFT) - 1, max_swap_offset); | |
824 | ||
825 | pte = swp_entry_to_pte(entry); | |
826 | WARN_ON(pte_swp_exclusive(pte)); | |
827 | WARN_ON(!is_swap_pte(pte)); | |
828 | entry2 = pte_to_swp_entry(pte); | |
829 | WARN_ON(memcmp(&entry, &entry2, sizeof(entry))); | |
830 | ||
210d1e8a DH |
831 | pte = pte_swp_mkexclusive(pte); |
832 | WARN_ON(!pte_swp_exclusive(pte)); | |
2321ba3e DH |
833 | WARN_ON(!is_swap_pte(pte)); |
834 | WARN_ON(pte_swp_soft_dirty(pte)); | |
835 | entry2 = pte_to_swp_entry(pte); | |
836 | WARN_ON(memcmp(&entry, &entry2, sizeof(entry))); | |
837 | ||
210d1e8a DH |
838 | pte = pte_swp_clear_exclusive(pte); |
839 | WARN_ON(pte_swp_exclusive(pte)); | |
2321ba3e DH |
840 | WARN_ON(!is_swap_pte(pte)); |
841 | entry2 = pte_to_swp_entry(pte); | |
842 | WARN_ON(memcmp(&entry, &entry2, sizeof(entry))); | |
210d1e8a DH |
843 | } |
844 | ||
5f447e80 | 845 | static void __init pte_swap_tests(struct pgtable_debug_args *args) |
05289402 AK |
846 | { |
847 | swp_entry_t swp; | |
848 | pte_t pte; | |
849 | ||
6315df41 | 850 | pr_debug("Validating PTE swap\n"); |
5f447e80 | 851 | pte = pfn_pte(args->fixed_pte_pfn, args->page_prot); |
05289402 AK |
852 | swp = __pte_to_swp_entry(pte); |
853 | pte = __swp_entry_to_pte(swp); | |
5f447e80 | 854 | WARN_ON(args->fixed_pte_pfn != pte_pfn(pte)); |
05289402 AK |
855 | } |
856 | ||
857 | #ifdef CONFIG_ARCH_ENABLE_THP_MIGRATION | |
5f447e80 | 858 | static void __init pmd_swap_tests(struct pgtable_debug_args *args) |
05289402 AK |
859 | { |
860 | swp_entry_t swp; | |
861 | pmd_t pmd; | |
862 | ||
65ac1a60 AK |
863 | if (!has_transparent_hugepage()) |
864 | return; | |
865 | ||
6315df41 | 866 | pr_debug("Validating PMD swap\n"); |
5f447e80 | 867 | pmd = pfn_pmd(args->fixed_pmd_pfn, args->page_prot); |
05289402 AK |
868 | swp = __pmd_to_swp_entry(pmd); |
869 | pmd = __swp_entry_to_pmd(swp); | |
5f447e80 | 870 | WARN_ON(args->fixed_pmd_pfn != pmd_pfn(pmd)); |
05289402 AK |
871 | } |
872 | #else /* !CONFIG_ARCH_ENABLE_THP_MIGRATION */ | |
5f447e80 | 873 | static void __init pmd_swap_tests(struct pgtable_debug_args *args) { } |
05289402 AK |
874 | #endif /* CONFIG_ARCH_ENABLE_THP_MIGRATION */ |
875 | ||
4878a888 | 876 | static void __init swap_migration_tests(struct pgtable_debug_args *args) |
05289402 AK |
877 | { |
878 | struct page *page; | |
879 | swp_entry_t swp; | |
880 | ||
881 | if (!IS_ENABLED(CONFIG_MIGRATION)) | |
882 | return; | |
6315df41 | 883 | |
05289402 AK |
884 | /* |
885 | * swap_migration_tests() requires a dedicated page as it needs to | |
886 | * be locked before creating a migration entry from it. Locking the | |
887 | * page that actually maps kernel text ('start_kernel') can be real | |
4878a888 GS |
888 | * problematic. Lets use the allocated page explicitly for this |
889 | * purpose. | |
05289402 | 890 | */ |
4878a888 GS |
891 | page = (args->pte_pfn != ULONG_MAX) ? pfn_to_page(args->pte_pfn) : NULL; |
892 | if (!page) | |
05289402 | 893 | return; |
4878a888 GS |
894 | |
895 | pr_debug("Validating swap migration\n"); | |
05289402 AK |
896 | |
897 | /* | |
23647618 AK |
898 | * make_[readable|writable]_migration_entry() expects given page to |
899 | * be locked, otherwise it stumbles upon a BUG_ON(). | |
05289402 AK |
900 | */ |
901 | __SetPageLocked(page); | |
4dd845b5 | 902 | swp = make_writable_migration_entry(page_to_pfn(page)); |
05289402 | 903 | WARN_ON(!is_migration_entry(swp)); |
4dd845b5 | 904 | WARN_ON(!is_writable_migration_entry(swp)); |
05289402 | 905 | |
4dd845b5 | 906 | swp = make_readable_migration_entry(swp_offset(swp)); |
05289402 | 907 | WARN_ON(!is_migration_entry(swp)); |
4dd845b5 | 908 | WARN_ON(is_writable_migration_entry(swp)); |
05289402 | 909 | |
4dd845b5 | 910 | swp = make_readable_migration_entry(page_to_pfn(page)); |
05289402 | 911 | WARN_ON(!is_migration_entry(swp)); |
4dd845b5 | 912 | WARN_ON(is_writable_migration_entry(swp)); |
05289402 | 913 | __ClearPageLocked(page); |
05289402 AK |
914 | } |
915 | ||
916 | #ifdef CONFIG_HUGETLB_PAGE | |
36b77d1e | 917 | static void __init hugetlb_basic_tests(struct pgtable_debug_args *args) |
05289402 AK |
918 | { |
919 | struct page *page; | |
920 | pte_t pte; | |
921 | ||
6315df41 | 922 | pr_debug("Validating HugeTLB basic\n"); |
05289402 AK |
923 | /* |
924 | * Accessing the page associated with the pfn is safe here, | |
925 | * as it was previously derived from a real kernel symbol. | |
926 | */ | |
36b77d1e GS |
927 | page = pfn_to_page(args->fixed_pmd_pfn); |
928 | pte = mk_huge_pte(page, args->page_prot); | |
05289402 AK |
929 | |
930 | WARN_ON(!huge_pte_dirty(huge_pte_mkdirty(pte))); | |
931 | WARN_ON(!huge_pte_write(huge_pte_mkwrite(huge_pte_wrprotect(pte)))); | |
932 | WARN_ON(huge_pte_write(huge_pte_wrprotect(huge_pte_mkwrite(pte)))); | |
933 | ||
934 | #ifdef CONFIG_ARCH_WANT_GENERAL_HUGETLB | |
36b77d1e | 935 | pte = pfn_pte(args->fixed_pmd_pfn, args->page_prot); |
05289402 | 936 | |
9dabf6e1 | 937 | WARN_ON(!pte_huge(arch_make_huge_pte(pte, PMD_SHIFT, VM_ACCESS_FLAGS))); |
05289402 AK |
938 | #endif /* CONFIG_ARCH_WANT_GENERAL_HUGETLB */ |
939 | } | |
940 | #else /* !CONFIG_HUGETLB_PAGE */ | |
36b77d1e | 941 | static void __init hugetlb_basic_tests(struct pgtable_debug_args *args) { } |
05289402 AK |
942 | #endif /* CONFIG_HUGETLB_PAGE */ |
943 | ||
944 | #ifdef CONFIG_TRANSPARENT_HUGEPAGE | |
4878a888 | 945 | static void __init pmd_thp_tests(struct pgtable_debug_args *args) |
05289402 AK |
946 | { |
947 | pmd_t pmd; | |
948 | ||
949 | if (!has_transparent_hugepage()) | |
950 | return; | |
951 | ||
6315df41 | 952 | pr_debug("Validating PMD based THP\n"); |
05289402 AK |
953 | /* |
954 | * pmd_trans_huge() and pmd_present() must return positive after | |
955 | * MMU invalidation with pmd_mkinvalid(). This behavior is an | |
956 | * optimization for transparent huge page. pmd_trans_huge() must | |
957 | * be true if pmd_page() returns a valid THP to avoid taking the | |
958 | * pmd_lock when others walk over non transhuge pmds (i.e. there | |
959 | * are no THP allocated). Especially when splitting a THP and | |
960 | * removing the present bit from the pmd, pmd_trans_huge() still | |
961 | * needs to return true. pmd_present() should be true whenever | |
962 | * pmd_trans_huge() returns true. | |
963 | */ | |
4878a888 | 964 | pmd = pfn_pmd(args->fixed_pmd_pfn, args->page_prot); |
05289402 AK |
965 | WARN_ON(!pmd_trans_huge(pmd_mkhuge(pmd))); |
966 | ||
967 | #ifndef __HAVE_ARCH_PMDP_INVALIDATE | |
968 | WARN_ON(!pmd_trans_huge(pmd_mkinvalid(pmd_mkhuge(pmd)))); | |
969 | WARN_ON(!pmd_present(pmd_mkinvalid(pmd_mkhuge(pmd)))); | |
970 | #endif /* __HAVE_ARCH_PMDP_INVALIDATE */ | |
971 | } | |
972 | ||
973 | #ifdef CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD | |
4878a888 | 974 | static void __init pud_thp_tests(struct pgtable_debug_args *args) |
05289402 AK |
975 | { |
976 | pud_t pud; | |
977 | ||
978 | if (!has_transparent_hugepage()) | |
979 | return; | |
980 | ||
6315df41 | 981 | pr_debug("Validating PUD based THP\n"); |
4878a888 | 982 | pud = pfn_pud(args->fixed_pud_pfn, args->page_prot); |
05289402 AK |
983 | WARN_ON(!pud_trans_huge(pud_mkhuge(pud))); |
984 | ||
985 | /* | |
986 | * pud_mkinvalid() has been dropped for now. Enable back | |
987 | * these tests when it comes back with a modified pud_present(). | |
988 | * | |
989 | * WARN_ON(!pud_trans_huge(pud_mkinvalid(pud_mkhuge(pud)))); | |
990 | * WARN_ON(!pud_present(pud_mkinvalid(pud_mkhuge(pud)))); | |
991 | */ | |
992 | } | |
993 | #else /* !CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD */ | |
4878a888 | 994 | static void __init pud_thp_tests(struct pgtable_debug_args *args) { } |
05289402 AK |
995 | #endif /* CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD */ |
996 | #else /* !CONFIG_TRANSPARENT_HUGEPAGE */ | |
4878a888 GS |
997 | static void __init pmd_thp_tests(struct pgtable_debug_args *args) { } |
998 | static void __init pud_thp_tests(struct pgtable_debug_args *args) { } | |
05289402 AK |
999 | #endif /* CONFIG_TRANSPARENT_HUGEPAGE */ |
1000 | ||
399145f9 AK |
1001 | static unsigned long __init get_random_vaddr(void) |
1002 | { | |
1003 | unsigned long random_vaddr, random_pages, total_user_pages; | |
1004 | ||
1005 | total_user_pages = (TASK_SIZE - FIRST_USER_ADDRESS) / PAGE_SIZE; | |
1006 | ||
1007 | random_pages = get_random_long() % total_user_pages; | |
1008 | random_vaddr = FIRST_USER_ADDRESS + random_pages * PAGE_SIZE; | |
1009 | ||
1010 | return random_vaddr; | |
1011 | } | |
1012 | ||
3c9b84f0 GS |
1013 | static void __init destroy_args(struct pgtable_debug_args *args) |
1014 | { | |
1015 | struct page *page = NULL; | |
1016 | ||
1017 | /* Free (huge) page */ | |
1018 | if (IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE) && | |
1019 | IS_ENABLED(CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD) && | |
1020 | has_transparent_hugepage() && | |
1021 | args->pud_pfn != ULONG_MAX) { | |
1022 | if (args->is_contiguous_page) { | |
1023 | free_contig_range(args->pud_pfn, | |
1024 | (1 << (HPAGE_PUD_SHIFT - PAGE_SHIFT))); | |
1025 | } else { | |
1026 | page = pfn_to_page(args->pud_pfn); | |
1027 | __free_pages(page, HPAGE_PUD_SHIFT - PAGE_SHIFT); | |
1028 | } | |
1029 | ||
1030 | args->pud_pfn = ULONG_MAX; | |
1031 | args->pmd_pfn = ULONG_MAX; | |
1032 | args->pte_pfn = ULONG_MAX; | |
1033 | } | |
1034 | ||
1035 | if (IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE) && | |
1036 | has_transparent_hugepage() && | |
1037 | args->pmd_pfn != ULONG_MAX) { | |
1038 | if (args->is_contiguous_page) { | |
1039 | free_contig_range(args->pmd_pfn, (1 << HPAGE_PMD_ORDER)); | |
1040 | } else { | |
1041 | page = pfn_to_page(args->pmd_pfn); | |
1042 | __free_pages(page, HPAGE_PMD_ORDER); | |
1043 | } | |
1044 | ||
1045 | args->pmd_pfn = ULONG_MAX; | |
1046 | args->pte_pfn = ULONG_MAX; | |
1047 | } | |
1048 | ||
1049 | if (args->pte_pfn != ULONG_MAX) { | |
1050 | page = pfn_to_page(args->pte_pfn); | |
dcc1be11 | 1051 | __free_page(page); |
3c9b84f0 GS |
1052 | |
1053 | args->pte_pfn = ULONG_MAX; | |
1054 | } | |
1055 | ||
1056 | /* Free page table entries */ | |
1057 | if (args->start_ptep) { | |
1058 | pte_free(args->mm, args->start_ptep); | |
1059 | mm_dec_nr_ptes(args->mm); | |
1060 | } | |
1061 | ||
1062 | if (args->start_pmdp) { | |
1063 | pmd_free(args->mm, args->start_pmdp); | |
1064 | mm_dec_nr_pmds(args->mm); | |
1065 | } | |
1066 | ||
1067 | if (args->start_pudp) { | |
1068 | pud_free(args->mm, args->start_pudp); | |
1069 | mm_dec_nr_puds(args->mm); | |
1070 | } | |
1071 | ||
1072 | if (args->start_p4dp) | |
1073 | p4d_free(args->mm, args->start_p4dp); | |
1074 | ||
1075 | /* Free vma and mm struct */ | |
1076 | if (args->vma) | |
1077 | vm_area_free(args->vma); | |
1078 | ||
1079 | if (args->mm) | |
1080 | mmdrop(args->mm); | |
1081 | } | |
1082 | ||
1083 | static struct page * __init | |
1084 | debug_vm_pgtable_alloc_huge_page(struct pgtable_debug_args *args, int order) | |
1085 | { | |
1086 | struct page *page = NULL; | |
1087 | ||
1088 | #ifdef CONFIG_CONTIG_ALLOC | |
23baf831 | 1089 | if (order > MAX_ORDER) { |
3c9b84f0 GS |
1090 | page = alloc_contig_pages((1 << order), GFP_KERNEL, |
1091 | first_online_node, NULL); | |
1092 | if (page) { | |
1093 | args->is_contiguous_page = true; | |
1094 | return page; | |
1095 | } | |
1096 | } | |
1097 | #endif | |
1098 | ||
23baf831 | 1099 | if (order <= MAX_ORDER) |
3c9b84f0 GS |
1100 | page = alloc_pages(GFP_KERNEL, order); |
1101 | ||
1102 | return page; | |
1103 | } | |
1104 | ||
c4876ff6 FL |
1105 | /* |
1106 | * Check if a physical memory range described by <pstart, pend> contains | |
1107 | * an area that is of size psize, and aligned to psize. | |
1108 | * | |
1109 | * Don't use address 0, an all-zeroes physical address might mask bugs, and | |
1110 | * it's not used on x86. | |
1111 | */ | |
1112 | static void __init phys_align_check(phys_addr_t pstart, | |
1113 | phys_addr_t pend, unsigned long psize, | |
1114 | phys_addr_t *physp, unsigned long *alignp) | |
1115 | { | |
1116 | phys_addr_t aligned_start, aligned_end; | |
1117 | ||
1118 | if (pstart == 0) | |
1119 | pstart = PAGE_SIZE; | |
1120 | ||
1121 | aligned_start = ALIGN(pstart, psize); | |
1122 | aligned_end = aligned_start + psize; | |
1123 | ||
1124 | if (aligned_end > aligned_start && aligned_end <= pend) { | |
1125 | *alignp = psize; | |
1126 | *physp = aligned_start; | |
1127 | } | |
1128 | } | |
1129 | ||
1130 | static void __init init_fixed_pfns(struct pgtable_debug_args *args) | |
1131 | { | |
1132 | u64 idx; | |
1133 | phys_addr_t phys, pstart, pend; | |
1134 | ||
1135 | /* | |
1136 | * Initialize the fixed pfns. To do this, try to find a | |
1137 | * valid physical range, preferably aligned to PUD_SIZE, | |
1138 | * but settling for aligned to PMD_SIZE as a fallback. If | |
1139 | * neither of those is found, use the physical address of | |
1140 | * the start_kernel symbol. | |
1141 | * | |
1142 | * The memory doesn't need to be allocated, it just needs to exist | |
1143 | * as usable memory. It won't be touched. | |
1144 | * | |
1145 | * The alignment is recorded, and can be checked to see if we | |
1146 | * can run the tests that require an actual valid physical | |
1147 | * address range on some architectures ({pmd,pud}_huge_test | |
1148 | * on x86). | |
1149 | */ | |
1150 | ||
1151 | phys = __pa_symbol(&start_kernel); | |
1152 | args->fixed_alignment = PAGE_SIZE; | |
1153 | ||
1154 | for_each_mem_range(idx, &pstart, &pend) { | |
1155 | /* First check for a PUD-aligned area */ | |
1156 | phys_align_check(pstart, pend, PUD_SIZE, &phys, | |
1157 | &args->fixed_alignment); | |
1158 | ||
1159 | /* If a PUD-aligned area is found, we're done */ | |
1160 | if (args->fixed_alignment == PUD_SIZE) | |
1161 | break; | |
1162 | ||
1163 | /* | |
1164 | * If no PMD-aligned area found yet, check for one, | |
1165 | * but continue the loop to look for a PUD-aligned area. | |
1166 | */ | |
1167 | if (args->fixed_alignment < PMD_SIZE) | |
1168 | phys_align_check(pstart, pend, PMD_SIZE, &phys, | |
1169 | &args->fixed_alignment); | |
1170 | } | |
1171 | ||
1172 | args->fixed_pgd_pfn = __phys_to_pfn(phys & PGDIR_MASK); | |
1173 | args->fixed_p4d_pfn = __phys_to_pfn(phys & P4D_MASK); | |
1174 | args->fixed_pud_pfn = __phys_to_pfn(phys & PUD_MASK); | |
1175 | args->fixed_pmd_pfn = __phys_to_pfn(phys & PMD_MASK); | |
1176 | args->fixed_pte_pfn = __phys_to_pfn(phys & PAGE_MASK); | |
1177 | WARN_ON(!pfn_valid(args->fixed_pte_pfn)); | |
1178 | } | |
1179 | ||
1180 | ||
3c9b84f0 GS |
1181 | static int __init init_args(struct pgtable_debug_args *args) |
1182 | { | |
1183 | struct page *page = NULL; | |
3c9b84f0 GS |
1184 | int ret = 0; |
1185 | ||
1186 | /* | |
1187 | * Initialize the debugging data. | |
1188 | * | |
31d17076 AK |
1189 | * vm_get_page_prot(VM_NONE) or vm_get_page_prot(VM_SHARED|VM_NONE) |
1190 | * will help create page table entries with PROT_NONE permission as | |
1191 | * required for pxx_protnone_tests(). | |
3c9b84f0 GS |
1192 | */ |
1193 | memset(args, 0, sizeof(*args)); | |
1194 | args->vaddr = get_random_vaddr(); | |
d7e679b6 | 1195 | args->page_prot = vm_get_page_prot(VM_ACCESS_FLAGS); |
31d17076 | 1196 | args->page_prot_none = vm_get_page_prot(VM_NONE); |
3c9b84f0 GS |
1197 | args->is_contiguous_page = false; |
1198 | args->pud_pfn = ULONG_MAX; | |
1199 | args->pmd_pfn = ULONG_MAX; | |
1200 | args->pte_pfn = ULONG_MAX; | |
1201 | args->fixed_pgd_pfn = ULONG_MAX; | |
1202 | args->fixed_p4d_pfn = ULONG_MAX; | |
1203 | args->fixed_pud_pfn = ULONG_MAX; | |
1204 | args->fixed_pmd_pfn = ULONG_MAX; | |
1205 | args->fixed_pte_pfn = ULONG_MAX; | |
1206 | ||
1207 | /* Allocate mm and vma */ | |
1208 | args->mm = mm_alloc(); | |
1209 | if (!args->mm) { | |
1210 | pr_err("Failed to allocate mm struct\n"); | |
1211 | ret = -ENOMEM; | |
1212 | goto error; | |
1213 | } | |
1214 | ||
1215 | args->vma = vm_area_alloc(args->mm); | |
1216 | if (!args->vma) { | |
1217 | pr_err("Failed to allocate vma\n"); | |
1218 | ret = -ENOMEM; | |
1219 | goto error; | |
1220 | } | |
1221 | ||
1222 | /* | |
1223 | * Allocate page table entries. They will be modified in the tests. | |
1224 | * Lets save the page table entries so that they can be released | |
1225 | * when the tests are completed. | |
1226 | */ | |
1227 | args->pgdp = pgd_offset(args->mm, args->vaddr); | |
1228 | args->p4dp = p4d_alloc(args->mm, args->pgdp, args->vaddr); | |
1229 | if (!args->p4dp) { | |
1230 | pr_err("Failed to allocate p4d entries\n"); | |
1231 | ret = -ENOMEM; | |
1232 | goto error; | |
1233 | } | |
1234 | args->start_p4dp = p4d_offset(args->pgdp, 0UL); | |
1235 | WARN_ON(!args->start_p4dp); | |
1236 | ||
1237 | args->pudp = pud_alloc(args->mm, args->p4dp, args->vaddr); | |
1238 | if (!args->pudp) { | |
1239 | pr_err("Failed to allocate pud entries\n"); | |
1240 | ret = -ENOMEM; | |
1241 | goto error; | |
1242 | } | |
1243 | args->start_pudp = pud_offset(args->p4dp, 0UL); | |
1244 | WARN_ON(!args->start_pudp); | |
1245 | ||
1246 | args->pmdp = pmd_alloc(args->mm, args->pudp, args->vaddr); | |
1247 | if (!args->pmdp) { | |
1248 | pr_err("Failed to allocate pmd entries\n"); | |
1249 | ret = -ENOMEM; | |
1250 | goto error; | |
1251 | } | |
1252 | args->start_pmdp = pmd_offset(args->pudp, 0UL); | |
1253 | WARN_ON(!args->start_pmdp); | |
1254 | ||
1255 | if (pte_alloc(args->mm, args->pmdp)) { | |
1256 | pr_err("Failed to allocate pte entries\n"); | |
1257 | ret = -ENOMEM; | |
1258 | goto error; | |
1259 | } | |
1260 | args->start_ptep = pmd_pgtable(READ_ONCE(*args->pmdp)); | |
1261 | WARN_ON(!args->start_ptep); | |
1262 | ||
c4876ff6 | 1263 | init_fixed_pfns(args); |
3c9b84f0 GS |
1264 | |
1265 | /* | |
1266 | * Allocate (huge) pages because some of the tests need to access | |
1267 | * the data in the pages. The corresponding tests will be skipped | |
1268 | * if we fail to allocate (huge) pages. | |
1269 | */ | |
1270 | if (IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE) && | |
1271 | IS_ENABLED(CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD) && | |
1272 | has_transparent_hugepage()) { | |
1273 | page = debug_vm_pgtable_alloc_huge_page(args, | |
1274 | HPAGE_PUD_SHIFT - PAGE_SHIFT); | |
1275 | if (page) { | |
1276 | args->pud_pfn = page_to_pfn(page); | |
1277 | args->pmd_pfn = args->pud_pfn; | |
1278 | args->pte_pfn = args->pud_pfn; | |
1279 | return 0; | |
1280 | } | |
1281 | } | |
1282 | ||
1283 | if (IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE) && | |
1284 | has_transparent_hugepage()) { | |
1285 | page = debug_vm_pgtable_alloc_huge_page(args, HPAGE_PMD_ORDER); | |
1286 | if (page) { | |
1287 | args->pmd_pfn = page_to_pfn(page); | |
1288 | args->pte_pfn = args->pmd_pfn; | |
1289 | return 0; | |
1290 | } | |
1291 | } | |
1292 | ||
dcc1be11 | 1293 | page = alloc_page(GFP_KERNEL); |
3c9b84f0 GS |
1294 | if (page) |
1295 | args->pte_pfn = page_to_pfn(page); | |
1296 | ||
1297 | return 0; | |
1298 | ||
1299 | error: | |
1300 | destroy_args(args); | |
1301 | return ret; | |
1302 | } | |
1303 | ||
399145f9 AK |
1304 | static int __init debug_vm_pgtable(void) |
1305 | { | |
3c9b84f0 | 1306 | struct pgtable_debug_args args; |
fea1120c | 1307 | spinlock_t *ptl = NULL; |
3c9b84f0 | 1308 | int idx, ret; |
399145f9 AK |
1309 | |
1310 | pr_info("Validating architecture page table helpers\n"); | |
3c9b84f0 GS |
1311 | ret = init_args(&args); |
1312 | if (ret) | |
1313 | return ret; | |
1314 | ||
2e326c07 | 1315 | /* |
31d17076 | 1316 | * Iterate over each possible vm_flags to make sure that all |
2e326c07 AK |
1317 | * the basic page table transformation validations just hold |
1318 | * true irrespective of the starting protection value for a | |
1319 | * given page table entry. | |
31d17076 AK |
1320 | * |
1321 | * Protection based vm_flags combinatins are always linear | |
1322 | * and increasing i.e starting from VM_NONE and going upto | |
1323 | * (VM_SHARED | READ | WRITE | EXEC). | |
2e326c07 | 1324 | */ |
31d17076 AK |
1325 | #define VM_FLAGS_START (VM_NONE) |
1326 | #define VM_FLAGS_END (VM_SHARED | VM_EXEC | VM_WRITE | VM_READ) | |
1327 | ||
1328 | for (idx = VM_FLAGS_START; idx <= VM_FLAGS_END; idx++) { | |
36b77d1e GS |
1329 | pte_basic_tests(&args, idx); |
1330 | pmd_basic_tests(&args, idx); | |
1331 | pud_basic_tests(&args, idx); | |
2e326c07 AK |
1332 | } |
1333 | ||
1334 | /* | |
1335 | * Both P4D and PGD level tests are very basic which do not | |
1336 | * involve creating page table entries from the protection | |
1337 | * value and the given pfn. Hence just keep them out from | |
1338 | * the above iteration for now to save some test execution | |
1339 | * time. | |
1340 | */ | |
36b77d1e GS |
1341 | p4d_basic_tests(&args); |
1342 | pgd_basic_tests(&args); | |
399145f9 | 1343 | |
8983d231 GS |
1344 | pmd_leaf_tests(&args); |
1345 | pud_leaf_tests(&args); | |
a5c3b9ff | 1346 | |
8cb183f2 GS |
1347 | pte_special_tests(&args); |
1348 | pte_protnone_tests(&args); | |
1349 | pmd_protnone_tests(&args); | |
05289402 | 1350 | |
8cb183f2 GS |
1351 | pte_devmap_tests(&args); |
1352 | pmd_devmap_tests(&args); | |
1353 | pud_devmap_tests(&args); | |
05289402 | 1354 | |
5f447e80 GS |
1355 | pte_soft_dirty_tests(&args); |
1356 | pmd_soft_dirty_tests(&args); | |
1357 | pte_swap_soft_dirty_tests(&args); | |
1358 | pmd_swap_soft_dirty_tests(&args); | |
05289402 | 1359 | |
210d1e8a DH |
1360 | pte_swap_exclusive_tests(&args); |
1361 | ||
5f447e80 GS |
1362 | pte_swap_tests(&args); |
1363 | pmd_swap_tests(&args); | |
05289402 | 1364 | |
4878a888 | 1365 | swap_migration_tests(&args); |
05289402 | 1366 | |
4878a888 GS |
1367 | pmd_thp_tests(&args); |
1368 | pud_thp_tests(&args); | |
05289402 | 1369 | |
36b77d1e | 1370 | hugetlb_basic_tests(&args); |
e8edf0ad | 1371 | |
6f302e27 AK |
1372 | /* |
1373 | * Page table modifying tests. They need to hold | |
1374 | * proper page table lock. | |
1375 | */ | |
e8edf0ad | 1376 | |
44966c44 GS |
1377 | args.ptep = pte_offset_map_lock(args.mm, args.pmdp, args.vaddr, &ptl); |
1378 | pte_clear_tests(&args); | |
1379 | pte_advanced_tests(&args); | |
1380 | pte_unmap_unlock(args.ptep, ptl); | |
e8edf0ad | 1381 | |
c0fe07b0 GS |
1382 | ptl = pmd_lock(args.mm, args.pmdp); |
1383 | pmd_clear_tests(&args); | |
1384 | pmd_advanced_tests(&args); | |
1385 | pmd_huge_tests(&args); | |
1386 | pmd_populate_tests(&args); | |
6f302e27 AK |
1387 | spin_unlock(ptl); |
1388 | ||
4cbde03b GS |
1389 | ptl = pud_lock(args.mm, args.pudp); |
1390 | pud_clear_tests(&args); | |
1391 | pud_advanced_tests(&args); | |
1392 | pud_huge_tests(&args); | |
1393 | pud_populate_tests(&args); | |
6f302e27 | 1394 | spin_unlock(ptl); |
e8edf0ad | 1395 | |
2f87f8c3 GS |
1396 | spin_lock(&(args.mm->page_table_lock)); |
1397 | p4d_clear_tests(&args); | |
1398 | pgd_clear_tests(&args); | |
1399 | p4d_populate_tests(&args); | |
1400 | pgd_populate_tests(&args); | |
1401 | spin_unlock(&(args.mm->page_table_lock)); | |
e8edf0ad | 1402 | |
3c9b84f0 | 1403 | destroy_args(&args); |
399145f9 AK |
1404 | return 0; |
1405 | } | |
1406 | late_initcall(debug_vm_pgtable); |