blob: 79480fd184433aa97b4ccd03e8fca95755f846b8 [file] [log] [blame]
Anshuman Khandual399145f2020-06-04 16:47:15 -07001// 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 */
Anshuman Khandual6315df42020-08-06 23:19:25 -070011#define pr_fmt(fmt) "debug_vm_pgtable: [%-25s]: " fmt, __func__
Anshuman Khandual399145f2020-06-04 16:47:15 -070012
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>
18#include <linux/mm.h>
19#include <linux/mman.h>
20#include <linux/mm_types.h>
21#include <linux/module.h>
22#include <linux/pfn_t.h>
23#include <linux/printk.h>
Anshuman Khanduala5c3b9f2020-08-06 23:19:20 -070024#include <linux/pgtable.h>
Anshuman Khandual399145f2020-06-04 16:47:15 -070025#include <linux/random.h>
26#include <linux/spinlock.h>
27#include <linux/swap.h>
28#include <linux/swapops.h>
29#include <linux/start_kernel.h>
30#include <linux/sched/mm.h>
Aneesh Kumar K.V85a14462020-10-15 20:04:36 -070031#include <linux/io.h>
Anshuman Khandual399145f2020-06-04 16:47:15 -070032#include <asm/pgalloc.h>
Anshuman Khanduala5c3b9f2020-08-06 23:19:20 -070033#include <asm/tlbflush.h>
Anshuman Khandual399145f2020-06-04 16:47:15 -070034
Anshuman Khandualb1d00002020-08-06 23:19:28 -070035/*
36 * Please refer Documentation/vm/arch_pgtable_helpers.rst for the semantics
37 * expectations that are being validated here. All future changes in here
38 * or the documentation need to be in sync.
39 */
40
Anshuman Khandual399145f2020-06-04 16:47:15 -070041#define VMFLAGS (VM_READ|VM_WRITE|VM_EXEC)
42
43/*
44 * On s390 platform, the lower 4 bits are used to identify given page table
45 * entry type. But these bits might affect the ability to clear entries with
46 * pxx_clear() because of how dynamic page table folding works on s390. So
47 * while loading up the entries do not change the lower 4 bits. It does not
Aneesh Kumar K.Vcfc5bbc2020-10-15 20:04:33 -070048 * have affect any other platform. Also avoid the 62nd bit on ppc64 that is
49 * used to mark a pte entry.
Anshuman Khandual399145f2020-06-04 16:47:15 -070050 */
Aneesh Kumar K.Vcfc5bbc2020-10-15 20:04:33 -070051#define S390_SKIP_MASK GENMASK(3, 0)
52#if __BITS_PER_LONG == 64
53#define PPC64_SKIP_MASK GENMASK(62, 62)
54#else
55#define PPC64_SKIP_MASK 0x0
56#endif
57#define ARCH_SKIP_MASK (S390_SKIP_MASK | PPC64_SKIP_MASK)
58#define RANDOM_ORVALUE (GENMASK(BITS_PER_LONG - 1, 0) & ~ARCH_SKIP_MASK)
Anshuman Khandual399145f2020-06-04 16:47:15 -070059#define RANDOM_NZVALUE GENMASK(7, 0)
60
61static void __init pte_basic_tests(unsigned long pfn, pgprot_t prot)
62{
63 pte_t pte = pfn_pte(pfn, prot);
64
Anshuman Khandual6315df42020-08-06 23:19:25 -070065 pr_debug("Validating PTE basic\n");
Anshuman Khandual27634d62021-02-24 12:01:32 -080066
67 /*
68 * This test needs to be executed after the given page table entry
69 * is created with pfn_pte() to make sure that protection_map[idx]
70 * does not have the dirty bit enabled from the beginning. This is
71 * important for platforms like arm64 where (!PTE_RDONLY) indicate
72 * dirty bit being set.
73 */
74 WARN_ON(pte_dirty(pte_wrprotect(pte)));
75
Anshuman Khandual399145f2020-06-04 16:47:15 -070076 WARN_ON(!pte_same(pte, pte));
77 WARN_ON(!pte_young(pte_mkyoung(pte_mkold(pte))));
78 WARN_ON(!pte_dirty(pte_mkdirty(pte_mkclean(pte))));
79 WARN_ON(!pte_write(pte_mkwrite(pte_wrprotect(pte))));
80 WARN_ON(pte_young(pte_mkold(pte_mkyoung(pte))));
81 WARN_ON(pte_dirty(pte_mkclean(pte_mkdirty(pte))));
82 WARN_ON(pte_write(pte_wrprotect(pte_mkwrite(pte))));
Anshuman Khandual27634d62021-02-24 12:01:32 -080083 WARN_ON(pte_dirty(pte_wrprotect(pte_mkclean(pte))));
84 WARN_ON(!pte_dirty(pte_wrprotect(pte_mkdirty(pte))));
Anshuman Khandual399145f2020-06-04 16:47:15 -070085}
86
Anshuman Khanduala5c3b9f2020-08-06 23:19:20 -070087static void __init pte_advanced_tests(struct mm_struct *mm,
88 struct vm_area_struct *vma, pte_t *ptep,
89 unsigned long pfn, unsigned long vaddr,
90 pgprot_t prot)
91{
92 pte_t pte = pfn_pte(pfn, prot);
93
Aneesh Kumar K.Vc3824e12020-10-15 20:04:46 -070094 /*
95 * Architectures optimize set_pte_at by avoiding TLB flush.
96 * This requires set_pte_at to be not used to update an
97 * existing pte entry. Clear pte before we do set_pte_at
98 */
99
Anshuman Khandual6315df42020-08-06 23:19:25 -0700100 pr_debug("Validating PTE advanced\n");
Anshuman Khanduala5c3b9f2020-08-06 23:19:20 -0700101 pte = pfn_pte(pfn, prot);
102 set_pte_at(mm, vaddr, ptep, pte);
103 ptep_set_wrprotect(mm, vaddr, ptep);
104 pte = ptep_get(ptep);
105 WARN_ON(pte_write(pte));
Anshuman Khanduala5c3b9f2020-08-06 23:19:20 -0700106 ptep_get_and_clear(mm, vaddr, ptep);
107 pte = ptep_get(ptep);
108 WARN_ON(!pte_none(pte));
109
110 pte = pfn_pte(pfn, prot);
111 pte = pte_wrprotect(pte);
112 pte = pte_mkclean(pte);
113 set_pte_at(mm, vaddr, ptep, pte);
114 pte = pte_mkwrite(pte);
115 pte = pte_mkdirty(pte);
116 ptep_set_access_flags(vma, vaddr, ptep, pte, 1);
117 pte = ptep_get(ptep);
118 WARN_ON(!(pte_write(pte) && pte_dirty(pte)));
Anshuman Khanduala5c3b9f2020-08-06 23:19:20 -0700119 ptep_get_and_clear_full(mm, vaddr, ptep, 1);
120 pte = ptep_get(ptep);
121 WARN_ON(!pte_none(pte));
122
Aneesh Kumar K.Vc3824e12020-10-15 20:04:46 -0700123 pte = pfn_pte(pfn, prot);
Anshuman Khanduala5c3b9f2020-08-06 23:19:20 -0700124 pte = pte_mkyoung(pte);
125 set_pte_at(mm, vaddr, ptep, pte);
126 ptep_test_and_clear_young(vma, vaddr, ptep);
127 pte = ptep_get(ptep);
128 WARN_ON(pte_young(pte));
129}
130
131static void __init pte_savedwrite_tests(unsigned long pfn, pgprot_t prot)
132{
133 pte_t pte = pfn_pte(pfn, prot);
134
Aneesh Kumar K.V42006052020-10-15 20:04:40 -0700135 if (!IS_ENABLED(CONFIG_NUMA_BALANCING))
136 return;
137
Anshuman Khandual6315df42020-08-06 23:19:25 -0700138 pr_debug("Validating PTE saved write\n");
Anshuman Khanduala5c3b9f2020-08-06 23:19:20 -0700139 WARN_ON(!pte_savedwrite(pte_mk_savedwrite(pte_clear_savedwrite(pte))));
140 WARN_ON(pte_savedwrite(pte_clear_savedwrite(pte_mk_savedwrite(pte))));
141}
Aneesh Kumar K.V42006052020-10-15 20:04:40 -0700142
Anshuman Khandual399145f2020-06-04 16:47:15 -0700143#ifdef CONFIG_TRANSPARENT_HUGEPAGE
144static void __init pmd_basic_tests(unsigned long pfn, pgprot_t prot)
145{
146 pmd_t pmd = pfn_pmd(pfn, prot);
147
Aneesh Kumar K.V787d5632020-06-10 18:41:44 -0700148 if (!has_transparent_hugepage())
149 return;
150
Anshuman Khandual6315df42020-08-06 23:19:25 -0700151 pr_debug("Validating PMD basic\n");
Anshuman Khandual27634d62021-02-24 12:01:32 -0800152
153 /*
154 * This test needs to be executed after the given page table entry
155 * is created with pfn_pmd() to make sure that protection_map[idx]
156 * does not have the dirty bit enabled from the beginning. This is
157 * important for platforms like arm64 where (!PTE_RDONLY) indicate
158 * dirty bit being set.
159 */
160 WARN_ON(pmd_dirty(pmd_wrprotect(pmd)));
161
162
Anshuman Khandual399145f2020-06-04 16:47:15 -0700163 WARN_ON(!pmd_same(pmd, pmd));
164 WARN_ON(!pmd_young(pmd_mkyoung(pmd_mkold(pmd))));
165 WARN_ON(!pmd_dirty(pmd_mkdirty(pmd_mkclean(pmd))));
166 WARN_ON(!pmd_write(pmd_mkwrite(pmd_wrprotect(pmd))));
167 WARN_ON(pmd_young(pmd_mkold(pmd_mkyoung(pmd))));
168 WARN_ON(pmd_dirty(pmd_mkclean(pmd_mkdirty(pmd))));
169 WARN_ON(pmd_write(pmd_wrprotect(pmd_mkwrite(pmd))));
Anshuman Khandual27634d62021-02-24 12:01:32 -0800170 WARN_ON(pmd_dirty(pmd_wrprotect(pmd_mkclean(pmd))));
171 WARN_ON(!pmd_dirty(pmd_wrprotect(pmd_mkdirty(pmd))));
Anshuman Khandual399145f2020-06-04 16:47:15 -0700172 /*
173 * A huge page does not point to next level page table
174 * entry. Hence this must qualify as pmd_bad().
175 */
176 WARN_ON(!pmd_bad(pmd_mkhuge(pmd)));
177}
178
Anshuman Khanduala5c3b9f2020-08-06 23:19:20 -0700179static void __init pmd_advanced_tests(struct mm_struct *mm,
180 struct vm_area_struct *vma, pmd_t *pmdp,
181 unsigned long pfn, unsigned long vaddr,
Aneesh Kumar K.V87f34982020-10-15 20:04:56 -0700182 pgprot_t prot, pgtable_t pgtable)
Anshuman Khanduala5c3b9f2020-08-06 23:19:20 -0700183{
184 pmd_t pmd = pfn_pmd(pfn, prot);
185
186 if (!has_transparent_hugepage())
187 return;
188
Anshuman Khandual6315df42020-08-06 23:19:25 -0700189 pr_debug("Validating PMD advanced\n");
Anshuman Khanduala5c3b9f2020-08-06 23:19:20 -0700190 /* Align the address wrt HPAGE_PMD_SIZE */
Gerald Schaefer5f2e1e82021-06-04 20:01:18 -0700191 vaddr &= HPAGE_PMD_MASK;
Anshuman Khanduala5c3b9f2020-08-06 23:19:20 -0700192
Aneesh Kumar K.V87f34982020-10-15 20:04:56 -0700193 pgtable_trans_huge_deposit(mm, pmdp, pgtable);
194
Anshuman Khanduala5c3b9f2020-08-06 23:19:20 -0700195 pmd = pfn_pmd(pfn, prot);
196 set_pmd_at(mm, vaddr, pmdp, pmd);
197 pmdp_set_wrprotect(mm, vaddr, pmdp);
198 pmd = READ_ONCE(*pmdp);
199 WARN_ON(pmd_write(pmd));
Anshuman Khanduala5c3b9f2020-08-06 23:19:20 -0700200 pmdp_huge_get_and_clear(mm, vaddr, pmdp);
201 pmd = READ_ONCE(*pmdp);
202 WARN_ON(!pmd_none(pmd));
203
204 pmd = pfn_pmd(pfn, prot);
205 pmd = pmd_wrprotect(pmd);
206 pmd = pmd_mkclean(pmd);
207 set_pmd_at(mm, vaddr, pmdp, pmd);
208 pmd = pmd_mkwrite(pmd);
209 pmd = pmd_mkdirty(pmd);
210 pmdp_set_access_flags(vma, vaddr, pmdp, pmd, 1);
211 pmd = READ_ONCE(*pmdp);
212 WARN_ON(!(pmd_write(pmd) && pmd_dirty(pmd)));
Anshuman Khanduala5c3b9f2020-08-06 23:19:20 -0700213 pmdp_huge_get_and_clear_full(vma, vaddr, pmdp, 1);
214 pmd = READ_ONCE(*pmdp);
215 WARN_ON(!pmd_none(pmd));
216
Aneesh Kumar K.Vc3824e12020-10-15 20:04:46 -0700217 pmd = pmd_mkhuge(pfn_pmd(pfn, prot));
Anshuman Khanduala5c3b9f2020-08-06 23:19:20 -0700218 pmd = pmd_mkyoung(pmd);
219 set_pmd_at(mm, vaddr, pmdp, pmd);
220 pmdp_test_and_clear_young(vma, vaddr, pmdp);
221 pmd = READ_ONCE(*pmdp);
222 WARN_ON(pmd_young(pmd));
Aneesh Kumar K.V87f34982020-10-15 20:04:56 -0700223
Aneesh Kumar K.V13af0502020-10-15 20:04:59 -0700224 /* Clear the pte entries */
225 pmdp_huge_get_and_clear(mm, vaddr, pmdp);
Aneesh Kumar K.V87f34982020-10-15 20:04:56 -0700226 pgtable = pgtable_trans_huge_withdraw(mm, pmdp);
Anshuman Khanduala5c3b9f2020-08-06 23:19:20 -0700227}
228
229static void __init pmd_leaf_tests(unsigned long pfn, pgprot_t prot)
230{
231 pmd_t pmd = pfn_pmd(pfn, prot);
232
Anshuman Khandual6315df42020-08-06 23:19:25 -0700233 pr_debug("Validating PMD leaf\n");
Anshuman Khanduala5c3b9f2020-08-06 23:19:20 -0700234 /*
235 * PMD based THP is a leaf entry.
236 */
237 pmd = pmd_mkhuge(pmd);
238 WARN_ON(!pmd_leaf(pmd));
239}
240
Aneesh Kumar K.V85a14462020-10-15 20:04:36 -0700241#ifdef CONFIG_HAVE_ARCH_HUGE_VMAP
Anshuman Khanduala5c3b9f2020-08-06 23:19:20 -0700242static void __init pmd_huge_tests(pmd_t *pmdp, unsigned long pfn, pgprot_t prot)
243{
244 pmd_t pmd;
245
Aneesh Kumar K.V85a14462020-10-15 20:04:36 -0700246 if (!arch_ioremap_pmd_supported())
Anshuman Khanduala5c3b9f2020-08-06 23:19:20 -0700247 return;
Anshuman Khandual6315df42020-08-06 23:19:25 -0700248
249 pr_debug("Validating PMD huge\n");
Anshuman Khanduala5c3b9f2020-08-06 23:19:20 -0700250 /*
251 * X86 defined pmd_set_huge() verifies that the given
252 * PMD is not a populated non-leaf entry.
253 */
254 WRITE_ONCE(*pmdp, __pmd(0));
255 WARN_ON(!pmd_set_huge(pmdp, __pfn_to_phys(pfn), prot));
256 WARN_ON(!pmd_clear_huge(pmdp));
257 pmd = READ_ONCE(*pmdp);
258 WARN_ON(!pmd_none(pmd));
259}
Aneesh Kumar K.V85a14462020-10-15 20:04:36 -0700260#else /* CONFIG_HAVE_ARCH_HUGE_VMAP */
261static void __init pmd_huge_tests(pmd_t *pmdp, unsigned long pfn, pgprot_t prot) { }
262#endif /* CONFIG_HAVE_ARCH_HUGE_VMAP */
Anshuman Khanduala5c3b9f2020-08-06 23:19:20 -0700263
264static void __init pmd_savedwrite_tests(unsigned long pfn, pgprot_t prot)
265{
266 pmd_t pmd = pfn_pmd(pfn, prot);
267
Aneesh Kumar K.V42006052020-10-15 20:04:40 -0700268 if (!IS_ENABLED(CONFIG_NUMA_BALANCING))
269 return;
270
Anshuman Khandual6315df42020-08-06 23:19:25 -0700271 pr_debug("Validating PMD saved write\n");
Anshuman Khanduala5c3b9f2020-08-06 23:19:20 -0700272 WARN_ON(!pmd_savedwrite(pmd_mk_savedwrite(pmd_clear_savedwrite(pmd))));
273 WARN_ON(pmd_savedwrite(pmd_clear_savedwrite(pmd_mk_savedwrite(pmd))));
274}
275
Anshuman Khandual399145f2020-06-04 16:47:15 -0700276#ifdef CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD
277static void __init pud_basic_tests(unsigned long pfn, pgprot_t prot)
278{
279 pud_t pud = pfn_pud(pfn, prot);
280
Aneesh Kumar K.V787d5632020-06-10 18:41:44 -0700281 if (!has_transparent_hugepage())
282 return;
283
Anshuman Khandual6315df42020-08-06 23:19:25 -0700284 pr_debug("Validating PUD basic\n");
Anshuman Khandual27634d62021-02-24 12:01:32 -0800285
286 /*
287 * This test needs to be executed after the given page table entry
288 * is created with pfn_pud() to make sure that protection_map[idx]
289 * does not have the dirty bit enabled from the beginning. This is
290 * important for platforms like arm64 where (!PTE_RDONLY) indicate
291 * dirty bit being set.
292 */
293 WARN_ON(pud_dirty(pud_wrprotect(pud)));
294
Anshuman Khandual399145f2020-06-04 16:47:15 -0700295 WARN_ON(!pud_same(pud, pud));
296 WARN_ON(!pud_young(pud_mkyoung(pud_mkold(pud))));
Anshuman Khandual27634d62021-02-24 12:01:32 -0800297 WARN_ON(!pud_dirty(pud_mkdirty(pud_mkclean(pud))));
298 WARN_ON(pud_dirty(pud_mkclean(pud_mkdirty(pud))));
Anshuman Khandual399145f2020-06-04 16:47:15 -0700299 WARN_ON(!pud_write(pud_mkwrite(pud_wrprotect(pud))));
300 WARN_ON(pud_write(pud_wrprotect(pud_mkwrite(pud))));
301 WARN_ON(pud_young(pud_mkold(pud_mkyoung(pud))));
Anshuman Khandual27634d62021-02-24 12:01:32 -0800302 WARN_ON(pud_dirty(pud_wrprotect(pud_mkclean(pud))));
303 WARN_ON(!pud_dirty(pud_wrprotect(pud_mkdirty(pud))));
Anshuman Khandual399145f2020-06-04 16:47:15 -0700304
305 if (mm_pmd_folded(mm))
306 return;
307
308 /*
309 * A huge page does not point to next level page table
310 * entry. Hence this must qualify as pud_bad().
311 */
312 WARN_ON(!pud_bad(pud_mkhuge(pud)));
313}
Anshuman Khanduala5c3b9f2020-08-06 23:19:20 -0700314
315static void __init pud_advanced_tests(struct mm_struct *mm,
316 struct vm_area_struct *vma, pud_t *pudp,
317 unsigned long pfn, unsigned long vaddr,
318 pgprot_t prot)
319{
320 pud_t pud = pfn_pud(pfn, prot);
321
322 if (!has_transparent_hugepage())
323 return;
324
Anshuman Khandual6315df42020-08-06 23:19:25 -0700325 pr_debug("Validating PUD advanced\n");
Anshuman Khanduala5c3b9f2020-08-06 23:19:20 -0700326 /* Align the address wrt HPAGE_PUD_SIZE */
Gerald Schaefer5f2e1e82021-06-04 20:01:18 -0700327 vaddr &= HPAGE_PUD_MASK;
Anshuman Khanduala5c3b9f2020-08-06 23:19:20 -0700328
329 set_pud_at(mm, vaddr, pudp, pud);
330 pudp_set_wrprotect(mm, vaddr, pudp);
331 pud = READ_ONCE(*pudp);
332 WARN_ON(pud_write(pud));
333
334#ifndef __PAGETABLE_PMD_FOLDED
Anshuman Khanduala5c3b9f2020-08-06 23:19:20 -0700335 pudp_huge_get_and_clear(mm, vaddr, pudp);
336 pud = READ_ONCE(*pudp);
337 WARN_ON(!pud_none(pud));
Anshuman Khanduala5c3b9f2020-08-06 23:19:20 -0700338#endif /* __PAGETABLE_PMD_FOLDED */
339 pud = pfn_pud(pfn, prot);
340 pud = pud_wrprotect(pud);
341 pud = pud_mkclean(pud);
342 set_pud_at(mm, vaddr, pudp, pud);
343 pud = pud_mkwrite(pud);
344 pud = pud_mkdirty(pud);
345 pudp_set_access_flags(vma, vaddr, pudp, pud, 1);
346 pud = READ_ONCE(*pudp);
347 WARN_ON(!(pud_write(pud) && pud_dirty(pud)));
348
Aneesh Kumar K.Vc3824e12020-10-15 20:04:46 -0700349#ifndef __PAGETABLE_PMD_FOLDED
350 pudp_huge_get_and_clear_full(mm, vaddr, pudp, 1);
351 pud = READ_ONCE(*pudp);
352 WARN_ON(!pud_none(pud));
353#endif /* __PAGETABLE_PMD_FOLDED */
354
355 pud = pfn_pud(pfn, prot);
Anshuman Khanduala5c3b9f2020-08-06 23:19:20 -0700356 pud = pud_mkyoung(pud);
357 set_pud_at(mm, vaddr, pudp, pud);
358 pudp_test_and_clear_young(vma, vaddr, pudp);
359 pud = READ_ONCE(*pudp);
360 WARN_ON(pud_young(pud));
Aneesh Kumar K.V13af0502020-10-15 20:04:59 -0700361
362 pudp_huge_get_and_clear(mm, vaddr, pudp);
Anshuman Khanduala5c3b9f2020-08-06 23:19:20 -0700363}
364
365static void __init pud_leaf_tests(unsigned long pfn, pgprot_t prot)
366{
367 pud_t pud = pfn_pud(pfn, prot);
368
Anshuman Khandual6315df42020-08-06 23:19:25 -0700369 pr_debug("Validating PUD leaf\n");
Anshuman Khanduala5c3b9f2020-08-06 23:19:20 -0700370 /*
371 * PUD based THP is a leaf entry.
372 */
373 pud = pud_mkhuge(pud);
374 WARN_ON(!pud_leaf(pud));
375}
376
Aneesh Kumar K.V85a14462020-10-15 20:04:36 -0700377#ifdef CONFIG_HAVE_ARCH_HUGE_VMAP
Anshuman Khanduala5c3b9f2020-08-06 23:19:20 -0700378static void __init pud_huge_tests(pud_t *pudp, unsigned long pfn, pgprot_t prot)
379{
380 pud_t pud;
381
Aneesh Kumar K.V85a14462020-10-15 20:04:36 -0700382 if (!arch_ioremap_pud_supported())
Anshuman Khanduala5c3b9f2020-08-06 23:19:20 -0700383 return;
Anshuman Khandual6315df42020-08-06 23:19:25 -0700384
385 pr_debug("Validating PUD huge\n");
Anshuman Khanduala5c3b9f2020-08-06 23:19:20 -0700386 /*
387 * X86 defined pud_set_huge() verifies that the given
388 * PUD is not a populated non-leaf entry.
389 */
390 WRITE_ONCE(*pudp, __pud(0));
391 WARN_ON(!pud_set_huge(pudp, __pfn_to_phys(pfn), prot));
392 WARN_ON(!pud_clear_huge(pudp));
393 pud = READ_ONCE(*pudp);
394 WARN_ON(!pud_none(pud));
395}
Aneesh Kumar K.V85a14462020-10-15 20:04:36 -0700396#else /* !CONFIG_HAVE_ARCH_HUGE_VMAP */
397static void __init pud_huge_tests(pud_t *pudp, unsigned long pfn, pgprot_t prot) { }
398#endif /* !CONFIG_HAVE_ARCH_HUGE_VMAP */
399
Anshuman Khandual399145f2020-06-04 16:47:15 -0700400#else /* !CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD */
401static void __init pud_basic_tests(unsigned long pfn, pgprot_t prot) { }
Anshuman Khanduala5c3b9f2020-08-06 23:19:20 -0700402static void __init pud_advanced_tests(struct mm_struct *mm,
403 struct vm_area_struct *vma, pud_t *pudp,
404 unsigned long pfn, unsigned long vaddr,
405 pgprot_t prot)
406{
407}
408static void __init pud_leaf_tests(unsigned long pfn, pgprot_t prot) { }
409static void __init pud_huge_tests(pud_t *pudp, unsigned long pfn, pgprot_t prot)
410{
411}
Anshuman Khandual399145f2020-06-04 16:47:15 -0700412#endif /* CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD */
413#else /* !CONFIG_TRANSPARENT_HUGEPAGE */
414static void __init pmd_basic_tests(unsigned long pfn, pgprot_t prot) { }
415static void __init pud_basic_tests(unsigned long pfn, pgprot_t prot) { }
Anshuman Khanduala5c3b9f2020-08-06 23:19:20 -0700416static void __init pmd_advanced_tests(struct mm_struct *mm,
417 struct vm_area_struct *vma, pmd_t *pmdp,
418 unsigned long pfn, unsigned long vaddr,
Aneesh Kumar K.V87f34982020-10-15 20:04:56 -0700419 pgprot_t prot, pgtable_t pgtable)
Anshuman Khanduala5c3b9f2020-08-06 23:19:20 -0700420{
421}
422static void __init pud_advanced_tests(struct mm_struct *mm,
423 struct vm_area_struct *vma, pud_t *pudp,
424 unsigned long pfn, unsigned long vaddr,
425 pgprot_t prot)
426{
427}
428static void __init pmd_leaf_tests(unsigned long pfn, pgprot_t prot) { }
429static void __init pud_leaf_tests(unsigned long pfn, pgprot_t prot) { }
430static void __init pmd_huge_tests(pmd_t *pmdp, unsigned long pfn, pgprot_t prot)
431{
432}
433static void __init pud_huge_tests(pud_t *pudp, unsigned long pfn, pgprot_t prot)
434{
435}
436static void __init pmd_savedwrite_tests(unsigned long pfn, pgprot_t prot) { }
Anshuman Khandual399145f2020-06-04 16:47:15 -0700437#endif /* CONFIG_TRANSPARENT_HUGEPAGE */
438
439static void __init p4d_basic_tests(unsigned long pfn, pgprot_t prot)
440{
441 p4d_t p4d;
442
Anshuman Khandual6315df42020-08-06 23:19:25 -0700443 pr_debug("Validating P4D basic\n");
Anshuman Khandual399145f2020-06-04 16:47:15 -0700444 memset(&p4d, RANDOM_NZVALUE, sizeof(p4d_t));
445 WARN_ON(!p4d_same(p4d, p4d));
446}
447
448static void __init pgd_basic_tests(unsigned long pfn, pgprot_t prot)
449{
450 pgd_t pgd;
451
Anshuman Khandual6315df42020-08-06 23:19:25 -0700452 pr_debug("Validating PGD basic\n");
Anshuman Khandual399145f2020-06-04 16:47:15 -0700453 memset(&pgd, RANDOM_NZVALUE, sizeof(pgd_t));
454 WARN_ON(!pgd_same(pgd, pgd));
455}
456
457#ifndef __PAGETABLE_PUD_FOLDED
458static void __init pud_clear_tests(struct mm_struct *mm, pud_t *pudp)
459{
460 pud_t pud = READ_ONCE(*pudp);
461
462 if (mm_pmd_folded(mm))
463 return;
464
Anshuman Khandual6315df42020-08-06 23:19:25 -0700465 pr_debug("Validating PUD clear\n");
Anshuman Khandual399145f2020-06-04 16:47:15 -0700466 pud = __pud(pud_val(pud) | RANDOM_ORVALUE);
467 WRITE_ONCE(*pudp, pud);
468 pud_clear(pudp);
469 pud = READ_ONCE(*pudp);
470 WARN_ON(!pud_none(pud));
471}
472
473static void __init pud_populate_tests(struct mm_struct *mm, pud_t *pudp,
474 pmd_t *pmdp)
475{
476 pud_t pud;
477
478 if (mm_pmd_folded(mm))
479 return;
Anshuman Khandual6315df42020-08-06 23:19:25 -0700480
481 pr_debug("Validating PUD populate\n");
Anshuman Khandual399145f2020-06-04 16:47:15 -0700482 /*
483 * This entry points to next level page table page.
484 * Hence this must not qualify as pud_bad().
485 */
Anshuman Khandual399145f2020-06-04 16:47:15 -0700486 pud_populate(mm, pudp, pmdp);
487 pud = READ_ONCE(*pudp);
488 WARN_ON(pud_bad(pud));
489}
490#else /* !__PAGETABLE_PUD_FOLDED */
491static void __init pud_clear_tests(struct mm_struct *mm, pud_t *pudp) { }
492static void __init pud_populate_tests(struct mm_struct *mm, pud_t *pudp,
493 pmd_t *pmdp)
494{
495}
496#endif /* PAGETABLE_PUD_FOLDED */
497
498#ifndef __PAGETABLE_P4D_FOLDED
499static void __init p4d_clear_tests(struct mm_struct *mm, p4d_t *p4dp)
500{
501 p4d_t p4d = READ_ONCE(*p4dp);
502
503 if (mm_pud_folded(mm))
504 return;
505
Anshuman Khandual6315df42020-08-06 23:19:25 -0700506 pr_debug("Validating P4D clear\n");
Anshuman Khandual399145f2020-06-04 16:47:15 -0700507 p4d = __p4d(p4d_val(p4d) | RANDOM_ORVALUE);
508 WRITE_ONCE(*p4dp, p4d);
509 p4d_clear(p4dp);
510 p4d = READ_ONCE(*p4dp);
511 WARN_ON(!p4d_none(p4d));
512}
513
514static void __init p4d_populate_tests(struct mm_struct *mm, p4d_t *p4dp,
515 pud_t *pudp)
516{
517 p4d_t p4d;
518
519 if (mm_pud_folded(mm))
520 return;
521
Anshuman Khandual6315df42020-08-06 23:19:25 -0700522 pr_debug("Validating P4D populate\n");
Anshuman Khandual399145f2020-06-04 16:47:15 -0700523 /*
524 * This entry points to next level page table page.
525 * Hence this must not qualify as p4d_bad().
526 */
527 pud_clear(pudp);
528 p4d_clear(p4dp);
529 p4d_populate(mm, p4dp, pudp);
530 p4d = READ_ONCE(*p4dp);
531 WARN_ON(p4d_bad(p4d));
532}
533
534static void __init pgd_clear_tests(struct mm_struct *mm, pgd_t *pgdp)
535{
536 pgd_t pgd = READ_ONCE(*pgdp);
537
538 if (mm_p4d_folded(mm))
539 return;
540
Anshuman Khandual6315df42020-08-06 23:19:25 -0700541 pr_debug("Validating PGD clear\n");
Anshuman Khandual399145f2020-06-04 16:47:15 -0700542 pgd = __pgd(pgd_val(pgd) | RANDOM_ORVALUE);
543 WRITE_ONCE(*pgdp, pgd);
544 pgd_clear(pgdp);
545 pgd = READ_ONCE(*pgdp);
546 WARN_ON(!pgd_none(pgd));
547}
548
549static void __init pgd_populate_tests(struct mm_struct *mm, pgd_t *pgdp,
550 p4d_t *p4dp)
551{
552 pgd_t pgd;
553
554 if (mm_p4d_folded(mm))
555 return;
556
Anshuman Khandual6315df42020-08-06 23:19:25 -0700557 pr_debug("Validating PGD populate\n");
Anshuman Khandual399145f2020-06-04 16:47:15 -0700558 /*
559 * This entry points to next level page table page.
560 * Hence this must not qualify as pgd_bad().
561 */
562 p4d_clear(p4dp);
563 pgd_clear(pgdp);
564 pgd_populate(mm, pgdp, p4dp);
565 pgd = READ_ONCE(*pgdp);
566 WARN_ON(pgd_bad(pgd));
567}
568#else /* !__PAGETABLE_P4D_FOLDED */
569static void __init p4d_clear_tests(struct mm_struct *mm, p4d_t *p4dp) { }
570static void __init pgd_clear_tests(struct mm_struct *mm, pgd_t *pgdp) { }
571static void __init p4d_populate_tests(struct mm_struct *mm, p4d_t *p4dp,
572 pud_t *pudp)
573{
574}
575static void __init pgd_populate_tests(struct mm_struct *mm, pgd_t *pgdp,
576 p4d_t *p4dp)
577{
578}
579#endif /* PAGETABLE_P4D_FOLDED */
580
581static void __init pte_clear_tests(struct mm_struct *mm, pte_t *ptep,
Aneesh Kumar K.V401035d2020-10-15 20:05:06 -0700582 unsigned long pfn, unsigned long vaddr,
583 pgprot_t prot)
Anshuman Khandual399145f2020-06-04 16:47:15 -0700584{
Aneesh Kumar K.V401035d2020-10-15 20:05:06 -0700585 pte_t pte = pfn_pte(pfn, prot);
Anshuman Khandual399145f2020-06-04 16:47:15 -0700586
Anshuman Khandual6315df42020-08-06 23:19:25 -0700587 pr_debug("Validating PTE clear\n");
Aneesh Kumar K.V401035d2020-10-15 20:05:06 -0700588#ifndef CONFIG_RISCV
Anshuman Khandual399145f2020-06-04 16:47:15 -0700589 pte = __pte(pte_val(pte) | RANDOM_ORVALUE);
Aneesh Kumar K.V401035d2020-10-15 20:05:06 -0700590#endif
Anshuman Khandual399145f2020-06-04 16:47:15 -0700591 set_pte_at(mm, vaddr, ptep, pte);
592 barrier();
593 pte_clear(mm, vaddr, ptep);
Christophe Leroy9449c9c2020-06-25 20:30:04 -0700594 pte = ptep_get(ptep);
Anshuman Khandual399145f2020-06-04 16:47:15 -0700595 WARN_ON(!pte_none(pte));
596}
597
598static void __init pmd_clear_tests(struct mm_struct *mm, pmd_t *pmdp)
599{
600 pmd_t pmd = READ_ONCE(*pmdp);
601
Anshuman Khandual6315df42020-08-06 23:19:25 -0700602 pr_debug("Validating PMD clear\n");
Anshuman Khandual399145f2020-06-04 16:47:15 -0700603 pmd = __pmd(pmd_val(pmd) | RANDOM_ORVALUE);
604 WRITE_ONCE(*pmdp, pmd);
605 pmd_clear(pmdp);
606 pmd = READ_ONCE(*pmdp);
607 WARN_ON(!pmd_none(pmd));
608}
609
610static void __init pmd_populate_tests(struct mm_struct *mm, pmd_t *pmdp,
611 pgtable_t pgtable)
612{
613 pmd_t pmd;
614
Anshuman Khandual6315df42020-08-06 23:19:25 -0700615 pr_debug("Validating PMD populate\n");
Anshuman Khandual399145f2020-06-04 16:47:15 -0700616 /*
617 * This entry points to next level page table page.
618 * Hence this must not qualify as pmd_bad().
619 */
Anshuman Khandual399145f2020-06-04 16:47:15 -0700620 pmd_populate(mm, pmdp, pgtable);
621 pmd = READ_ONCE(*pmdp);
622 WARN_ON(pmd_bad(pmd));
623}
624
Anshuman Khandual05289402020-08-06 23:19:16 -0700625static void __init pte_special_tests(unsigned long pfn, pgprot_t prot)
626{
627 pte_t pte = pfn_pte(pfn, prot);
628
629 if (!IS_ENABLED(CONFIG_ARCH_HAS_PTE_SPECIAL))
630 return;
631
Anshuman Khandual6315df42020-08-06 23:19:25 -0700632 pr_debug("Validating PTE special\n");
Anshuman Khandual05289402020-08-06 23:19:16 -0700633 WARN_ON(!pte_special(pte_mkspecial(pte)));
634}
635
636static void __init pte_protnone_tests(unsigned long pfn, pgprot_t prot)
637{
638 pte_t pte = pfn_pte(pfn, prot);
639
640 if (!IS_ENABLED(CONFIG_NUMA_BALANCING))
641 return;
642
Anshuman Khandual6315df42020-08-06 23:19:25 -0700643 pr_debug("Validating PTE protnone\n");
Anshuman Khandual05289402020-08-06 23:19:16 -0700644 WARN_ON(!pte_protnone(pte));
645 WARN_ON(!pte_present(pte));
646}
647
648#ifdef CONFIG_TRANSPARENT_HUGEPAGE
649static void __init pmd_protnone_tests(unsigned long pfn, pgprot_t prot)
650{
651 pmd_t pmd = pmd_mkhuge(pfn_pmd(pfn, prot));
652
653 if (!IS_ENABLED(CONFIG_NUMA_BALANCING))
654 return;
655
Anshuman Khandual6315df42020-08-06 23:19:25 -0700656 pr_debug("Validating PMD protnone\n");
Anshuman Khandual05289402020-08-06 23:19:16 -0700657 WARN_ON(!pmd_protnone(pmd));
658 WARN_ON(!pmd_present(pmd));
659}
660#else /* !CONFIG_TRANSPARENT_HUGEPAGE */
661static void __init pmd_protnone_tests(unsigned long pfn, pgprot_t prot) { }
662#endif /* CONFIG_TRANSPARENT_HUGEPAGE */
663
664#ifdef CONFIG_ARCH_HAS_PTE_DEVMAP
665static void __init pte_devmap_tests(unsigned long pfn, pgprot_t prot)
666{
667 pte_t pte = pfn_pte(pfn, prot);
668
Anshuman Khandual6315df42020-08-06 23:19:25 -0700669 pr_debug("Validating PTE devmap\n");
Anshuman Khandual05289402020-08-06 23:19:16 -0700670 WARN_ON(!pte_devmap(pte_mkdevmap(pte)));
671}
672
673#ifdef CONFIG_TRANSPARENT_HUGEPAGE
674static void __init pmd_devmap_tests(unsigned long pfn, pgprot_t prot)
675{
676 pmd_t pmd = pfn_pmd(pfn, prot);
677
Anshuman Khandual6315df42020-08-06 23:19:25 -0700678 pr_debug("Validating PMD devmap\n");
Anshuman Khandual05289402020-08-06 23:19:16 -0700679 WARN_ON(!pmd_devmap(pmd_mkdevmap(pmd)));
680}
681
682#ifdef CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD
683static void __init pud_devmap_tests(unsigned long pfn, pgprot_t prot)
684{
685 pud_t pud = pfn_pud(pfn, prot);
686
Anshuman Khandual6315df42020-08-06 23:19:25 -0700687 pr_debug("Validating PUD devmap\n");
Anshuman Khandual05289402020-08-06 23:19:16 -0700688 WARN_ON(!pud_devmap(pud_mkdevmap(pud)));
689}
690#else /* !CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD */
691static void __init pud_devmap_tests(unsigned long pfn, pgprot_t prot) { }
692#endif /* CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD */
693#else /* CONFIG_TRANSPARENT_HUGEPAGE */
694static void __init pmd_devmap_tests(unsigned long pfn, pgprot_t prot) { }
695static void __init pud_devmap_tests(unsigned long pfn, pgprot_t prot) { }
696#endif /* CONFIG_TRANSPARENT_HUGEPAGE */
697#else
698static void __init pte_devmap_tests(unsigned long pfn, pgprot_t prot) { }
699static void __init pmd_devmap_tests(unsigned long pfn, pgprot_t prot) { }
700static void __init pud_devmap_tests(unsigned long pfn, pgprot_t prot) { }
701#endif /* CONFIG_ARCH_HAS_PTE_DEVMAP */
702
703static void __init pte_soft_dirty_tests(unsigned long pfn, pgprot_t prot)
704{
705 pte_t pte = pfn_pte(pfn, prot);
706
707 if (!IS_ENABLED(CONFIG_MEM_SOFT_DIRTY))
708 return;
709
Anshuman Khandual6315df42020-08-06 23:19:25 -0700710 pr_debug("Validating PTE soft dirty\n");
Anshuman Khandual05289402020-08-06 23:19:16 -0700711 WARN_ON(!pte_soft_dirty(pte_mksoft_dirty(pte)));
712 WARN_ON(pte_soft_dirty(pte_clear_soft_dirty(pte)));
713}
714
715static void __init pte_swap_soft_dirty_tests(unsigned long pfn, pgprot_t prot)
716{
717 pte_t pte = pfn_pte(pfn, prot);
718
719 if (!IS_ENABLED(CONFIG_MEM_SOFT_DIRTY))
720 return;
721
Anshuman Khandual6315df42020-08-06 23:19:25 -0700722 pr_debug("Validating PTE swap soft dirty\n");
Anshuman Khandual05289402020-08-06 23:19:16 -0700723 WARN_ON(!pte_swp_soft_dirty(pte_swp_mksoft_dirty(pte)));
724 WARN_ON(pte_swp_soft_dirty(pte_swp_clear_soft_dirty(pte)));
725}
726
727#ifdef CONFIG_TRANSPARENT_HUGEPAGE
728static void __init pmd_soft_dirty_tests(unsigned long pfn, pgprot_t prot)
729{
730 pmd_t pmd = pfn_pmd(pfn, prot);
731
732 if (!IS_ENABLED(CONFIG_MEM_SOFT_DIRTY))
733 return;
734
Anshuman Khandual6315df42020-08-06 23:19:25 -0700735 pr_debug("Validating PMD soft dirty\n");
Anshuman Khandual05289402020-08-06 23:19:16 -0700736 WARN_ON(!pmd_soft_dirty(pmd_mksoft_dirty(pmd)));
737 WARN_ON(pmd_soft_dirty(pmd_clear_soft_dirty(pmd)));
738}
739
740static void __init pmd_swap_soft_dirty_tests(unsigned long pfn, pgprot_t prot)
741{
742 pmd_t pmd = pfn_pmd(pfn, prot);
743
744 if (!IS_ENABLED(CONFIG_MEM_SOFT_DIRTY) ||
745 !IS_ENABLED(CONFIG_ARCH_ENABLE_THP_MIGRATION))
746 return;
747
Anshuman Khandual6315df42020-08-06 23:19:25 -0700748 pr_debug("Validating PMD swap soft dirty\n");
Anshuman Khandual05289402020-08-06 23:19:16 -0700749 WARN_ON(!pmd_swp_soft_dirty(pmd_swp_mksoft_dirty(pmd)));
750 WARN_ON(pmd_swp_soft_dirty(pmd_swp_clear_soft_dirty(pmd)));
751}
752#else /* !CONFIG_ARCH_HAS_PTE_DEVMAP */
753static void __init pmd_soft_dirty_tests(unsigned long pfn, pgprot_t prot) { }
754static void __init pmd_swap_soft_dirty_tests(unsigned long pfn, pgprot_t prot)
755{
756}
757#endif /* CONFIG_ARCH_HAS_PTE_DEVMAP */
758
759static void __init pte_swap_tests(unsigned long pfn, pgprot_t prot)
760{
761 swp_entry_t swp;
762 pte_t pte;
763
Anshuman Khandual6315df42020-08-06 23:19:25 -0700764 pr_debug("Validating PTE swap\n");
Anshuman Khandual05289402020-08-06 23:19:16 -0700765 pte = pfn_pte(pfn, prot);
766 swp = __pte_to_swp_entry(pte);
767 pte = __swp_entry_to_pte(swp);
768 WARN_ON(pfn != pte_pfn(pte));
769}
770
771#ifdef CONFIG_ARCH_ENABLE_THP_MIGRATION
772static void __init pmd_swap_tests(unsigned long pfn, pgprot_t prot)
773{
774 swp_entry_t swp;
775 pmd_t pmd;
776
Anshuman Khandual6315df42020-08-06 23:19:25 -0700777 pr_debug("Validating PMD swap\n");
Anshuman Khandual05289402020-08-06 23:19:16 -0700778 pmd = pfn_pmd(pfn, prot);
779 swp = __pmd_to_swp_entry(pmd);
780 pmd = __swp_entry_to_pmd(swp);
781 WARN_ON(pfn != pmd_pfn(pmd));
782}
783#else /* !CONFIG_ARCH_ENABLE_THP_MIGRATION */
784static void __init pmd_swap_tests(unsigned long pfn, pgprot_t prot) { }
785#endif /* CONFIG_ARCH_ENABLE_THP_MIGRATION */
786
787static void __init swap_migration_tests(void)
788{
789 struct page *page;
790 swp_entry_t swp;
791
792 if (!IS_ENABLED(CONFIG_MIGRATION))
793 return;
Anshuman Khandual6315df42020-08-06 23:19:25 -0700794
795 pr_debug("Validating swap migration\n");
Anshuman Khandual05289402020-08-06 23:19:16 -0700796 /*
797 * swap_migration_tests() requires a dedicated page as it needs to
798 * be locked before creating a migration entry from it. Locking the
799 * page that actually maps kernel text ('start_kernel') can be real
800 * problematic. Lets allocate a dedicated page explicitly for this
801 * purpose that will be freed subsequently.
802 */
803 page = alloc_page(GFP_KERNEL);
804 if (!page) {
805 pr_err("page allocation failed\n");
806 return;
807 }
808
809 /*
810 * make_migration_entry() expects given page to be
811 * locked, otherwise it stumbles upon a BUG_ON().
812 */
813 __SetPageLocked(page);
814 swp = make_migration_entry(page, 1);
815 WARN_ON(!is_migration_entry(swp));
816 WARN_ON(!is_write_migration_entry(swp));
817
818 make_migration_entry_read(&swp);
819 WARN_ON(!is_migration_entry(swp));
820 WARN_ON(is_write_migration_entry(swp));
821
822 swp = make_migration_entry(page, 0);
823 WARN_ON(!is_migration_entry(swp));
824 WARN_ON(is_write_migration_entry(swp));
825 __ClearPageLocked(page);
826 __free_page(page);
827}
828
829#ifdef CONFIG_HUGETLB_PAGE
830static void __init hugetlb_basic_tests(unsigned long pfn, pgprot_t prot)
831{
832 struct page *page;
833 pte_t pte;
834
Anshuman Khandual6315df42020-08-06 23:19:25 -0700835 pr_debug("Validating HugeTLB basic\n");
Anshuman Khandual05289402020-08-06 23:19:16 -0700836 /*
837 * Accessing the page associated with the pfn is safe here,
838 * as it was previously derived from a real kernel symbol.
839 */
840 page = pfn_to_page(pfn);
841 pte = mk_huge_pte(page, prot);
842
843 WARN_ON(!huge_pte_dirty(huge_pte_mkdirty(pte)));
844 WARN_ON(!huge_pte_write(huge_pte_mkwrite(huge_pte_wrprotect(pte))));
845 WARN_ON(huge_pte_write(huge_pte_wrprotect(huge_pte_mkwrite(pte))));
846
847#ifdef CONFIG_ARCH_WANT_GENERAL_HUGETLB
848 pte = pfn_pte(pfn, prot);
849
850 WARN_ON(!pte_huge(pte_mkhuge(pte)));
851#endif /* CONFIG_ARCH_WANT_GENERAL_HUGETLB */
852}
853#else /* !CONFIG_HUGETLB_PAGE */
854static void __init hugetlb_basic_tests(unsigned long pfn, pgprot_t prot) { }
855#endif /* CONFIG_HUGETLB_PAGE */
856
857#ifdef CONFIG_TRANSPARENT_HUGEPAGE
858static void __init pmd_thp_tests(unsigned long pfn, pgprot_t prot)
859{
860 pmd_t pmd;
861
862 if (!has_transparent_hugepage())
863 return;
864
Anshuman Khandual6315df42020-08-06 23:19:25 -0700865 pr_debug("Validating PMD based THP\n");
Anshuman Khandual05289402020-08-06 23:19:16 -0700866 /*
867 * pmd_trans_huge() and pmd_present() must return positive after
868 * MMU invalidation with pmd_mkinvalid(). This behavior is an
869 * optimization for transparent huge page. pmd_trans_huge() must
870 * be true if pmd_page() returns a valid THP to avoid taking the
871 * pmd_lock when others walk over non transhuge pmds (i.e. there
872 * are no THP allocated). Especially when splitting a THP and
873 * removing the present bit from the pmd, pmd_trans_huge() still
874 * needs to return true. pmd_present() should be true whenever
875 * pmd_trans_huge() returns true.
876 */
877 pmd = pfn_pmd(pfn, prot);
878 WARN_ON(!pmd_trans_huge(pmd_mkhuge(pmd)));
879
880#ifndef __HAVE_ARCH_PMDP_INVALIDATE
881 WARN_ON(!pmd_trans_huge(pmd_mkinvalid(pmd_mkhuge(pmd))));
882 WARN_ON(!pmd_present(pmd_mkinvalid(pmd_mkhuge(pmd))));
883#endif /* __HAVE_ARCH_PMDP_INVALIDATE */
884}
885
886#ifdef CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD
887static void __init pud_thp_tests(unsigned long pfn, pgprot_t prot)
888{
889 pud_t pud;
890
891 if (!has_transparent_hugepage())
892 return;
893
Anshuman Khandual6315df42020-08-06 23:19:25 -0700894 pr_debug("Validating PUD based THP\n");
Anshuman Khandual05289402020-08-06 23:19:16 -0700895 pud = pfn_pud(pfn, prot);
896 WARN_ON(!pud_trans_huge(pud_mkhuge(pud)));
897
898 /*
899 * pud_mkinvalid() has been dropped for now. Enable back
900 * these tests when it comes back with a modified pud_present().
901 *
902 * WARN_ON(!pud_trans_huge(pud_mkinvalid(pud_mkhuge(pud))));
903 * WARN_ON(!pud_present(pud_mkinvalid(pud_mkhuge(pud))));
904 */
905}
906#else /* !CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD */
907static void __init pud_thp_tests(unsigned long pfn, pgprot_t prot) { }
908#endif /* CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD */
909#else /* !CONFIG_TRANSPARENT_HUGEPAGE */
910static void __init pmd_thp_tests(unsigned long pfn, pgprot_t prot) { }
911static void __init pud_thp_tests(unsigned long pfn, pgprot_t prot) { }
912#endif /* CONFIG_TRANSPARENT_HUGEPAGE */
913
Anshuman Khandual399145f2020-06-04 16:47:15 -0700914static unsigned long __init get_random_vaddr(void)
915{
916 unsigned long random_vaddr, random_pages, total_user_pages;
917
918 total_user_pages = (TASK_SIZE - FIRST_USER_ADDRESS) / PAGE_SIZE;
919
920 random_pages = get_random_long() % total_user_pages;
921 random_vaddr = FIRST_USER_ADDRESS + random_pages * PAGE_SIZE;
922
923 return random_vaddr;
924}
925
926static int __init debug_vm_pgtable(void)
927{
Anshuman Khanduala5c3b9f2020-08-06 23:19:20 -0700928 struct vm_area_struct *vma;
Anshuman Khandual399145f2020-06-04 16:47:15 -0700929 struct mm_struct *mm;
930 pgd_t *pgdp;
931 p4d_t *p4dp, *saved_p4dp;
932 pud_t *pudp, *saved_pudp;
933 pmd_t *pmdp, *saved_pmdp, pmd;
934 pte_t *ptep;
935 pgtable_t saved_ptep;
Anshuman Khandual05289402020-08-06 23:19:16 -0700936 pgprot_t prot, protnone;
Anshuman Khandual399145f2020-06-04 16:47:15 -0700937 phys_addr_t paddr;
938 unsigned long vaddr, pte_aligned, pmd_aligned;
939 unsigned long pud_aligned, p4d_aligned, pgd_aligned;
Kees Cookfea11202020-06-03 13:28:45 -0700940 spinlock_t *ptl = NULL;
Anshuman Khandual399145f2020-06-04 16:47:15 -0700941
942 pr_info("Validating architecture page table helpers\n");
943 prot = vm_get_page_prot(VMFLAGS);
944 vaddr = get_random_vaddr();
945 mm = mm_alloc();
946 if (!mm) {
947 pr_err("mm_struct allocation failed\n");
948 return 1;
949 }
950
951 /*
Anshuman Khandual05289402020-08-06 23:19:16 -0700952 * __P000 (or even __S000) will help create page table entries with
953 * PROT_NONE permission as required for pxx_protnone_tests().
954 */
955 protnone = __P000;
956
Anshuman Khanduala5c3b9f2020-08-06 23:19:20 -0700957 vma = vm_area_alloc(mm);
958 if (!vma) {
959 pr_err("vma allocation failed\n");
960 return 1;
961 }
962
Anshuman Khandual05289402020-08-06 23:19:16 -0700963 /*
Anshuman Khandual399145f2020-06-04 16:47:15 -0700964 * PFN for mapping at PTE level is determined from a standard kernel
965 * text symbol. But pfns for higher page table levels are derived by
966 * masking lower bits of this real pfn. These derived pfns might not
967 * exist on the platform but that does not really matter as pfn_pxx()
968 * helpers will still create appropriate entries for the test. This
969 * helps avoid large memory block allocations to be used for mapping
970 * at higher page table levels.
971 */
972 paddr = __pa_symbol(&start_kernel);
973
974 pte_aligned = (paddr & PAGE_MASK) >> PAGE_SHIFT;
975 pmd_aligned = (paddr & PMD_MASK) >> PAGE_SHIFT;
976 pud_aligned = (paddr & PUD_MASK) >> PAGE_SHIFT;
977 p4d_aligned = (paddr & P4D_MASK) >> PAGE_SHIFT;
978 pgd_aligned = (paddr & PGDIR_MASK) >> PAGE_SHIFT;
979 WARN_ON(!pfn_valid(pte_aligned));
980
981 pgdp = pgd_offset(mm, vaddr);
982 p4dp = p4d_alloc(mm, pgdp, vaddr);
983 pudp = pud_alloc(mm, p4dp, vaddr);
984 pmdp = pmd_alloc(mm, pudp, vaddr);
Aneesh Kumar K.Vf14312e2020-10-15 20:05:10 -0700985 /*
986 * Allocate pgtable_t
987 */
988 if (pte_alloc(mm, pmdp)) {
989 pr_err("pgtable allocation failed\n");
990 return 1;
991 }
Anshuman Khandual399145f2020-06-04 16:47:15 -0700992
993 /*
994 * Save all the page table page addresses as the page table
995 * entries will be used for testing with random or garbage
996 * values. These saved addresses will be used for freeing
997 * page table pages.
998 */
999 pmd = READ_ONCE(*pmdp);
1000 saved_p4dp = p4d_offset(pgdp, 0UL);
1001 saved_pudp = pud_offset(p4dp, 0UL);
1002 saved_pmdp = pmd_offset(pudp, 0UL);
1003 saved_ptep = pmd_pgtable(pmd);
1004
1005 pte_basic_tests(pte_aligned, prot);
1006 pmd_basic_tests(pmd_aligned, prot);
1007 pud_basic_tests(pud_aligned, prot);
1008 p4d_basic_tests(p4d_aligned, prot);
1009 pgd_basic_tests(pgd_aligned, prot);
1010
Anshuman Khanduala5c3b9f2020-08-06 23:19:20 -07001011 pmd_leaf_tests(pmd_aligned, prot);
1012 pud_leaf_tests(pud_aligned, prot);
1013
Aneesh Kumar K.V42006052020-10-15 20:04:40 -07001014 pte_savedwrite_tests(pte_aligned, protnone);
1015 pmd_savedwrite_tests(pmd_aligned, protnone);
Anshuman Khanduala5c3b9f2020-08-06 23:19:20 -07001016
Anshuman Khandual05289402020-08-06 23:19:16 -07001017 pte_special_tests(pte_aligned, prot);
1018 pte_protnone_tests(pte_aligned, protnone);
1019 pmd_protnone_tests(pmd_aligned, protnone);
1020
1021 pte_devmap_tests(pte_aligned, prot);
1022 pmd_devmap_tests(pmd_aligned, prot);
1023 pud_devmap_tests(pud_aligned, prot);
1024
1025 pte_soft_dirty_tests(pte_aligned, prot);
1026 pmd_soft_dirty_tests(pmd_aligned, prot);
1027 pte_swap_soft_dirty_tests(pte_aligned, prot);
1028 pmd_swap_soft_dirty_tests(pmd_aligned, prot);
1029
1030 pte_swap_tests(pte_aligned, prot);
1031 pmd_swap_tests(pmd_aligned, prot);
1032
1033 swap_migration_tests();
Anshuman Khandual05289402020-08-06 23:19:16 -07001034
1035 pmd_thp_tests(pmd_aligned, prot);
1036 pud_thp_tests(pud_aligned, prot);
1037
Aneesh Kumar K.Ve8edf0a2020-10-15 20:04:49 -07001038 hugetlb_basic_tests(pte_aligned, prot);
1039
Aneesh Kumar K.V6f302e272020-10-15 20:04:53 -07001040 /*
1041 * Page table modifying tests. They need to hold
1042 * proper page table lock.
1043 */
Aneesh Kumar K.Ve8edf0a2020-10-15 20:04:49 -07001044
Aneesh Kumar K.Vf14312e2020-10-15 20:05:10 -07001045 ptep = pte_offset_map_lock(mm, pmdp, vaddr, &ptl);
Aneesh Kumar K.V401035d2020-10-15 20:05:06 -07001046 pte_clear_tests(mm, ptep, pte_aligned, vaddr, prot);
Aneesh Kumar K.Ve8edf0a2020-10-15 20:04:49 -07001047 pte_advanced_tests(mm, vma, ptep, pte_aligned, vaddr, prot);
Aneesh Kumar K.Ve8edf0a2020-10-15 20:04:49 -07001048 pte_unmap_unlock(ptep, ptl);
1049
Aneesh Kumar K.V6f302e272020-10-15 20:04:53 -07001050 ptl = pmd_lock(mm, pmdp);
1051 pmd_clear_tests(mm, pmdp);
Aneesh Kumar K.V87f34982020-10-15 20:04:56 -07001052 pmd_advanced_tests(mm, vma, pmdp, pmd_aligned, vaddr, prot, saved_ptep);
Aneesh Kumar K.V6f302e272020-10-15 20:04:53 -07001053 pmd_huge_tests(pmdp, pmd_aligned, prot);
Aneesh Kumar K.Ve8edf0a2020-10-15 20:04:49 -07001054 pmd_populate_tests(mm, pmdp, saved_ptep);
Aneesh Kumar K.V6f302e272020-10-15 20:04:53 -07001055 spin_unlock(ptl);
1056
1057 ptl = pud_lock(mm, pudp);
1058 pud_clear_tests(mm, pudp);
1059 pud_advanced_tests(mm, vma, pudp, pud_aligned, vaddr, prot);
1060 pud_huge_tests(pudp, pud_aligned, prot);
Aneesh Kumar K.Ve8edf0a2020-10-15 20:04:49 -07001061 pud_populate_tests(mm, pudp, saved_pmdp);
Aneesh Kumar K.V6f302e272020-10-15 20:04:53 -07001062 spin_unlock(ptl);
1063
Aneesh Kumar K.V6f302e272020-10-15 20:04:53 -07001064 spin_lock(&mm->page_table_lock);
1065 p4d_clear_tests(mm, p4dp);
1066 pgd_clear_tests(mm, pgdp);
Aneesh Kumar K.Ve8edf0a2020-10-15 20:04:49 -07001067 p4d_populate_tests(mm, p4dp, saved_pudp);
1068 pgd_populate_tests(mm, pgdp, saved_p4dp);
Aneesh Kumar K.V6f302e272020-10-15 20:04:53 -07001069 spin_unlock(&mm->page_table_lock);
Aneesh Kumar K.Ve8edf0a2020-10-15 20:04:49 -07001070
Anshuman Khandual399145f2020-06-04 16:47:15 -07001071 p4d_free(mm, saved_p4dp);
1072 pud_free(mm, saved_pudp);
1073 pmd_free(mm, saved_pmdp);
1074 pte_free(mm, saved_ptep);
1075
Anshuman Khanduala5c3b9f2020-08-06 23:19:20 -07001076 vm_area_free(vma);
Anshuman Khandual399145f2020-06-04 16:47:15 -07001077 mm_dec_nr_puds(mm);
1078 mm_dec_nr_pmds(mm);
1079 mm_dec_nr_ptes(mm);
1080 mmdrop(mm);
1081 return 0;
1082}
1083late_initcall(debug_vm_pgtable);