blob: 4e453ae0f291284f2ce6cd70826554eeb8b2f712 [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
Gavin Shan3c9b84f2021-09-02 14:52:19 -070061struct pgtable_debug_args {
62 struct mm_struct *mm;
63 struct vm_area_struct *vma;
64
65 pgd_t *pgdp;
66 p4d_t *p4dp;
67 pud_t *pudp;
68 pmd_t *pmdp;
69 pte_t *ptep;
70
71 p4d_t *start_p4dp;
72 pud_t *start_pudp;
73 pmd_t *start_pmdp;
74 pgtable_t start_ptep;
75
76 unsigned long vaddr;
77 pgprot_t page_prot;
78 pgprot_t page_prot_none;
79
80 bool is_contiguous_page;
81 unsigned long pud_pfn;
82 unsigned long pmd_pfn;
83 unsigned long pte_pfn;
84
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
Gavin Shan36b77d12021-09-02 14:52:22 -070092static void __init pte_basic_tests(struct pgtable_debug_args *args, int idx)
Anshuman Khandual399145f2020-06-04 16:47:15 -070093{
Anshuman Khandual2e326c02021-02-24 12:01:36 -080094 pgprot_t prot = protection_map[idx];
Gavin Shan36b77d12021-09-02 14:52:22 -070095 pte_t pte = pfn_pte(args->fixed_pte_pfn, prot);
Anshuman Khandual2e326c02021-02-24 12:01:36 -080096 unsigned long val = idx, *ptr = &val;
Anshuman Khandual399145f2020-06-04 16:47:15 -070097
Anshuman Khandual2e326c02021-02-24 12:01:36 -080098 pr_debug("Validating PTE basic (%pGv)\n", ptr);
Anshuman Khandualbb5c47c2021-02-24 12:01:32 -080099
100 /*
101 * This test needs to be executed after the given page table entry
102 * is created with pfn_pte() to make sure that protection_map[idx]
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
Anshuman Khandual399145f2020-06-04 16:47:15 -0700109 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))));
Anshuman Khandualbb5c47c2021-02-24 12:01:32 -0800116 WARN_ON(pte_dirty(pte_wrprotect(pte_mkclean(pte))));
117 WARN_ON(!pte_dirty(pte_wrprotect(pte_mkdirty(pte))));
Anshuman Khandual399145f2020-06-04 16:47:15 -0700118}
119
Gavin Shan44966c42021-09-02 14:52:38 -0700120static void __init pte_advanced_tests(struct pgtable_debug_args *args)
Anshuman Khanduala5c3b9f2020-08-06 23:19:20 -0700121{
Shixin Liub593b902021-06-30 18:47:40 -0700122 pte_t pte;
Anshuman Khanduala5c3b9f2020-08-06 23:19:20 -0700123
Aneesh Kumar K.Vc3824e12020-10-15 20:04:46 -0700124 /*
125 * Architectures optimize set_pte_at by avoiding TLB flush.
126 * This requires set_pte_at to be not used to update an
127 * existing pte entry. Clear pte before we do set_pte_at
128 */
Gavin Shan44966c42021-09-02 14:52:38 -0700129 if (args->pte_pfn == ULONG_MAX)
130 return;
Aneesh Kumar K.Vc3824e12020-10-15 20:04:46 -0700131
Anshuman Khandual6315df42020-08-06 23:19:25 -0700132 pr_debug("Validating PTE advanced\n");
Gavin Shan44966c42021-09-02 14:52:38 -0700133 pte = pfn_pte(args->pte_pfn, args->page_prot);
134 set_pte_at(args->mm, args->vaddr, args->ptep, pte);
135 ptep_set_wrprotect(args->mm, args->vaddr, args->ptep);
136 pte = ptep_get(args->ptep);
Anshuman Khanduala5c3b9f2020-08-06 23:19:20 -0700137 WARN_ON(pte_write(pte));
Gavin Shan44966c42021-09-02 14:52:38 -0700138 ptep_get_and_clear(args->mm, args->vaddr, args->ptep);
139 pte = ptep_get(args->ptep);
Anshuman Khanduala5c3b9f2020-08-06 23:19:20 -0700140 WARN_ON(!pte_none(pte));
141
Gavin Shan44966c42021-09-02 14:52:38 -0700142 pte = pfn_pte(args->pte_pfn, args->page_prot);
Anshuman Khanduala5c3b9f2020-08-06 23:19:20 -0700143 pte = pte_wrprotect(pte);
144 pte = pte_mkclean(pte);
Gavin Shan44966c42021-09-02 14:52:38 -0700145 set_pte_at(args->mm, args->vaddr, args->ptep, pte);
Anshuman Khanduala5c3b9f2020-08-06 23:19:20 -0700146 pte = pte_mkwrite(pte);
147 pte = pte_mkdirty(pte);
Gavin Shan44966c42021-09-02 14:52:38 -0700148 ptep_set_access_flags(args->vma, args->vaddr, args->ptep, pte, 1);
149 pte = ptep_get(args->ptep);
Anshuman Khanduala5c3b9f2020-08-06 23:19:20 -0700150 WARN_ON(!(pte_write(pte) && pte_dirty(pte)));
Gavin Shan44966c42021-09-02 14:52:38 -0700151 ptep_get_and_clear_full(args->mm, args->vaddr, args->ptep, 1);
152 pte = ptep_get(args->ptep);
Anshuman Khanduala5c3b9f2020-08-06 23:19:20 -0700153 WARN_ON(!pte_none(pte));
154
Gavin Shan44966c42021-09-02 14:52:38 -0700155 pte = pfn_pte(args->pte_pfn, args->page_prot);
Anshuman Khanduala5c3b9f2020-08-06 23:19:20 -0700156 pte = pte_mkyoung(pte);
Gavin Shan44966c42021-09-02 14:52:38 -0700157 set_pte_at(args->mm, args->vaddr, args->ptep, pte);
158 ptep_test_and_clear_young(args->vma, args->vaddr, args->ptep);
159 pte = ptep_get(args->ptep);
Anshuman Khanduala5c3b9f2020-08-06 23:19:20 -0700160 WARN_ON(pte_young(pte));
161}
162
Gavin Shan8983d232021-09-02 14:52:25 -0700163static void __init pte_savedwrite_tests(struct pgtable_debug_args *args)
Anshuman Khanduala5c3b9f2020-08-06 23:19:20 -0700164{
Gavin Shan8983d232021-09-02 14:52:25 -0700165 pte_t pte = pfn_pte(args->fixed_pte_pfn, args->page_prot_none);
Anshuman Khanduala5c3b9f2020-08-06 23:19:20 -0700166
Aneesh Kumar K.V42006052020-10-15 20:04:40 -0700167 if (!IS_ENABLED(CONFIG_NUMA_BALANCING))
168 return;
169
Anshuman Khandual6315df42020-08-06 23:19:25 -0700170 pr_debug("Validating PTE saved write\n");
Anshuman Khanduala5c3b9f2020-08-06 23:19:20 -0700171 WARN_ON(!pte_savedwrite(pte_mk_savedwrite(pte_clear_savedwrite(pte))));
172 WARN_ON(pte_savedwrite(pte_clear_savedwrite(pte_mk_savedwrite(pte))));
173}
Aneesh Kumar K.V42006052020-10-15 20:04:40 -0700174
Anshuman Khandual399145f2020-06-04 16:47:15 -0700175#ifdef CONFIG_TRANSPARENT_HUGEPAGE
Gavin Shan36b77d12021-09-02 14:52:22 -0700176static void __init pmd_basic_tests(struct pgtable_debug_args *args, int idx)
Anshuman Khandual399145f2020-06-04 16:47:15 -0700177{
Anshuman Khandual2e326c02021-02-24 12:01:36 -0800178 pgprot_t prot = protection_map[idx];
Anshuman Khandual2e326c02021-02-24 12:01:36 -0800179 unsigned long val = idx, *ptr = &val;
Anshuman Khandual65ac1a62021-06-28 19:35:10 -0700180 pmd_t pmd;
Anshuman Khandual399145f2020-06-04 16:47:15 -0700181
Aneesh Kumar K.V787d5632020-06-10 18:41:44 -0700182 if (!has_transparent_hugepage())
183 return;
184
Anshuman Khandual2e326c02021-02-24 12:01:36 -0800185 pr_debug("Validating PMD basic (%pGv)\n", ptr);
Gavin Shan36b77d12021-09-02 14:52:22 -0700186 pmd = pfn_pmd(args->fixed_pmd_pfn, prot);
Anshuman Khandualbb5c47c2021-02-24 12:01:32 -0800187
188 /*
189 * This test needs to be executed after the given page table entry
190 * is created with pfn_pmd() to make sure that protection_map[idx]
191 * does not have the dirty bit enabled from the beginning. This is
192 * important for platforms like arm64 where (!PTE_RDONLY) indicate
193 * dirty bit being set.
194 */
195 WARN_ON(pmd_dirty(pmd_wrprotect(pmd)));
196
197
Anshuman Khandual399145f2020-06-04 16:47:15 -0700198 WARN_ON(!pmd_same(pmd, pmd));
199 WARN_ON(!pmd_young(pmd_mkyoung(pmd_mkold(pmd))));
200 WARN_ON(!pmd_dirty(pmd_mkdirty(pmd_mkclean(pmd))));
201 WARN_ON(!pmd_write(pmd_mkwrite(pmd_wrprotect(pmd))));
202 WARN_ON(pmd_young(pmd_mkold(pmd_mkyoung(pmd))));
203 WARN_ON(pmd_dirty(pmd_mkclean(pmd_mkdirty(pmd))));
204 WARN_ON(pmd_write(pmd_wrprotect(pmd_mkwrite(pmd))));
Anshuman Khandualbb5c47c2021-02-24 12:01:32 -0800205 WARN_ON(pmd_dirty(pmd_wrprotect(pmd_mkclean(pmd))));
206 WARN_ON(!pmd_dirty(pmd_wrprotect(pmd_mkdirty(pmd))));
Anshuman Khandual399145f2020-06-04 16:47:15 -0700207 /*
208 * A huge page does not point to next level page table
209 * entry. Hence this must qualify as pmd_bad().
210 */
211 WARN_ON(!pmd_bad(pmd_mkhuge(pmd)));
212}
213
Gavin Shanc0fe07b2021-09-02 14:52:41 -0700214static void __init pmd_advanced_tests(struct pgtable_debug_args *args)
Anshuman Khanduala5c3b9f2020-08-06 23:19:20 -0700215{
Anshuman Khandual65ac1a62021-06-28 19:35:10 -0700216 pmd_t pmd;
Gavin Shanc0fe07b2021-09-02 14:52:41 -0700217 unsigned long vaddr = args->vaddr;
Anshuman Khanduala5c3b9f2020-08-06 23:19:20 -0700218
219 if (!has_transparent_hugepage())
220 return;
221
Gavin Shanc0fe07b2021-09-02 14:52:41 -0700222 if (args->pmd_pfn == ULONG_MAX)
223 return;
224
Anshuman Khandual6315df42020-08-06 23:19:25 -0700225 pr_debug("Validating PMD advanced\n");
Anshuman Khanduala5c3b9f2020-08-06 23:19:20 -0700226 /* Align the address wrt HPAGE_PMD_SIZE */
Gerald Schaefer04f7ce32021-06-04 20:01:18 -0700227 vaddr &= HPAGE_PMD_MASK;
Anshuman Khanduala5c3b9f2020-08-06 23:19:20 -0700228
Gavin Shanc0fe07b2021-09-02 14:52:41 -0700229 pgtable_trans_huge_deposit(args->mm, args->pmdp, args->start_ptep);
Aneesh Kumar K.V87f34982020-10-15 20:04:56 -0700230
Gavin Shanc0fe07b2021-09-02 14:52:41 -0700231 pmd = pfn_pmd(args->pmd_pfn, args->page_prot);
232 set_pmd_at(args->mm, vaddr, args->pmdp, pmd);
233 pmdp_set_wrprotect(args->mm, vaddr, args->pmdp);
234 pmd = READ_ONCE(*args->pmdp);
Anshuman Khanduala5c3b9f2020-08-06 23:19:20 -0700235 WARN_ON(pmd_write(pmd));
Gavin Shanc0fe07b2021-09-02 14:52:41 -0700236 pmdp_huge_get_and_clear(args->mm, vaddr, args->pmdp);
237 pmd = READ_ONCE(*args->pmdp);
Anshuman Khanduala5c3b9f2020-08-06 23:19:20 -0700238 WARN_ON(!pmd_none(pmd));
239
Gavin Shanc0fe07b2021-09-02 14:52:41 -0700240 pmd = pfn_pmd(args->pmd_pfn, args->page_prot);
Anshuman Khanduala5c3b9f2020-08-06 23:19:20 -0700241 pmd = pmd_wrprotect(pmd);
242 pmd = pmd_mkclean(pmd);
Gavin Shanc0fe07b2021-09-02 14:52:41 -0700243 set_pmd_at(args->mm, vaddr, args->pmdp, pmd);
Anshuman Khanduala5c3b9f2020-08-06 23:19:20 -0700244 pmd = pmd_mkwrite(pmd);
245 pmd = pmd_mkdirty(pmd);
Gavin Shanc0fe07b2021-09-02 14:52:41 -0700246 pmdp_set_access_flags(args->vma, vaddr, args->pmdp, pmd, 1);
247 pmd = READ_ONCE(*args->pmdp);
Anshuman Khanduala5c3b9f2020-08-06 23:19:20 -0700248 WARN_ON(!(pmd_write(pmd) && pmd_dirty(pmd)));
Gavin Shanc0fe07b2021-09-02 14:52:41 -0700249 pmdp_huge_get_and_clear_full(args->vma, vaddr, args->pmdp, 1);
250 pmd = READ_ONCE(*args->pmdp);
Anshuman Khanduala5c3b9f2020-08-06 23:19:20 -0700251 WARN_ON(!pmd_none(pmd));
252
Gavin Shanc0fe07b2021-09-02 14:52:41 -0700253 pmd = pmd_mkhuge(pfn_pmd(args->pmd_pfn, args->page_prot));
Anshuman Khanduala5c3b9f2020-08-06 23:19:20 -0700254 pmd = pmd_mkyoung(pmd);
Gavin Shanc0fe07b2021-09-02 14:52:41 -0700255 set_pmd_at(args->mm, vaddr, args->pmdp, pmd);
256 pmdp_test_and_clear_young(args->vma, vaddr, args->pmdp);
257 pmd = READ_ONCE(*args->pmdp);
Anshuman Khanduala5c3b9f2020-08-06 23:19:20 -0700258 WARN_ON(pmd_young(pmd));
Aneesh Kumar K.V87f34982020-10-15 20:04:56 -0700259
Aneesh Kumar K.V13af0502020-10-15 20:04:59 -0700260 /* Clear the pte entries */
Gavin Shanc0fe07b2021-09-02 14:52:41 -0700261 pmdp_huge_get_and_clear(args->mm, vaddr, args->pmdp);
262 pgtable_trans_huge_withdraw(args->mm, args->pmdp);
Anshuman Khanduala5c3b9f2020-08-06 23:19:20 -0700263}
264
Gavin Shan8983d232021-09-02 14:52:25 -0700265static void __init pmd_leaf_tests(struct pgtable_debug_args *args)
Anshuman Khanduala5c3b9f2020-08-06 23:19:20 -0700266{
Anshuman Khandual65ac1a62021-06-28 19:35:10 -0700267 pmd_t pmd;
268
269 if (!has_transparent_hugepage())
270 return;
Anshuman Khanduala5c3b9f2020-08-06 23:19:20 -0700271
Anshuman Khandual6315df42020-08-06 23:19:25 -0700272 pr_debug("Validating PMD leaf\n");
Gavin Shan8983d232021-09-02 14:52:25 -0700273 pmd = pfn_pmd(args->fixed_pmd_pfn, args->page_prot);
Anshuman Khandual65ac1a62021-06-28 19:35:10 -0700274
Anshuman Khanduala5c3b9f2020-08-06 23:19:20 -0700275 /*
276 * PMD based THP is a leaf entry.
277 */
278 pmd = pmd_mkhuge(pmd);
279 WARN_ON(!pmd_leaf(pmd));
280}
281
Gavin Shan8983d232021-09-02 14:52:25 -0700282static void __init pmd_savedwrite_tests(struct pgtable_debug_args *args)
Anshuman Khanduala5c3b9f2020-08-06 23:19:20 -0700283{
Anshuman Khandual65ac1a62021-06-28 19:35:10 -0700284 pmd_t pmd;
Anshuman Khanduala5c3b9f2020-08-06 23:19:20 -0700285
Aneesh Kumar K.V42006052020-10-15 20:04:40 -0700286 if (!IS_ENABLED(CONFIG_NUMA_BALANCING))
287 return;
288
Anshuman Khandual65ac1a62021-06-28 19:35:10 -0700289 if (!has_transparent_hugepage())
290 return;
291
Anshuman Khandual6315df42020-08-06 23:19:25 -0700292 pr_debug("Validating PMD saved write\n");
Gavin Shan8983d232021-09-02 14:52:25 -0700293 pmd = pfn_pmd(args->fixed_pmd_pfn, args->page_prot_none);
Anshuman Khanduala5c3b9f2020-08-06 23:19:20 -0700294 WARN_ON(!pmd_savedwrite(pmd_mk_savedwrite(pmd_clear_savedwrite(pmd))));
295 WARN_ON(pmd_savedwrite(pmd_clear_savedwrite(pmd_mk_savedwrite(pmd))));
296}
297
Anshuman Khandual399145f2020-06-04 16:47:15 -0700298#ifdef CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD
Gavin Shan36b77d12021-09-02 14:52:22 -0700299static void __init pud_basic_tests(struct pgtable_debug_args *args, int idx)
Anshuman Khandual399145f2020-06-04 16:47:15 -0700300{
Anshuman Khandual2e326c02021-02-24 12:01:36 -0800301 pgprot_t prot = protection_map[idx];
Anshuman Khandual2e326c02021-02-24 12:01:36 -0800302 unsigned long val = idx, *ptr = &val;
Anshuman Khandual65ac1a62021-06-28 19:35:10 -0700303 pud_t pud;
Anshuman Khandual399145f2020-06-04 16:47:15 -0700304
Aneesh Kumar K.V787d5632020-06-10 18:41:44 -0700305 if (!has_transparent_hugepage())
306 return;
307
Anshuman Khandual2e326c02021-02-24 12:01:36 -0800308 pr_debug("Validating PUD basic (%pGv)\n", ptr);
Gavin Shan36b77d12021-09-02 14:52:22 -0700309 pud = pfn_pud(args->fixed_pud_pfn, prot);
Anshuman Khandualbb5c47c2021-02-24 12:01:32 -0800310
311 /*
312 * This test needs to be executed after the given page table entry
313 * is created with pfn_pud() to make sure that protection_map[idx]
314 * does not have the dirty bit enabled from the beginning. This is
315 * important for platforms like arm64 where (!PTE_RDONLY) indicate
316 * dirty bit being set.
317 */
318 WARN_ON(pud_dirty(pud_wrprotect(pud)));
319
Anshuman Khandual399145f2020-06-04 16:47:15 -0700320 WARN_ON(!pud_same(pud, pud));
321 WARN_ON(!pud_young(pud_mkyoung(pud_mkold(pud))));
Anshuman Khandualbb5c47c2021-02-24 12:01:32 -0800322 WARN_ON(!pud_dirty(pud_mkdirty(pud_mkclean(pud))));
323 WARN_ON(pud_dirty(pud_mkclean(pud_mkdirty(pud))));
Anshuman Khandual399145f2020-06-04 16:47:15 -0700324 WARN_ON(!pud_write(pud_mkwrite(pud_wrprotect(pud))));
325 WARN_ON(pud_write(pud_wrprotect(pud_mkwrite(pud))));
326 WARN_ON(pud_young(pud_mkold(pud_mkyoung(pud))));
Anshuman Khandualbb5c47c2021-02-24 12:01:32 -0800327 WARN_ON(pud_dirty(pud_wrprotect(pud_mkclean(pud))));
328 WARN_ON(!pud_dirty(pud_wrprotect(pud_mkdirty(pud))));
Anshuman Khandual399145f2020-06-04 16:47:15 -0700329
Gavin Shan36b77d12021-09-02 14:52:22 -0700330 if (mm_pmd_folded(args->mm))
Anshuman Khandual399145f2020-06-04 16:47:15 -0700331 return;
332
333 /*
334 * A huge page does not point to next level page table
335 * entry. Hence this must qualify as pud_bad().
336 */
337 WARN_ON(!pud_bad(pud_mkhuge(pud)));
338}
Anshuman Khanduala5c3b9f2020-08-06 23:19:20 -0700339
Gavin Shan4cbde032021-09-02 14:52:45 -0700340static void __init pud_advanced_tests(struct pgtable_debug_args *args)
Anshuman Khanduala5c3b9f2020-08-06 23:19:20 -0700341{
Gavin Shan4cbde032021-09-02 14:52:45 -0700342 unsigned long vaddr = args->vaddr;
Anshuman Khandual65ac1a62021-06-28 19:35:10 -0700343 pud_t pud;
Anshuman Khanduala5c3b9f2020-08-06 23:19:20 -0700344
345 if (!has_transparent_hugepage())
346 return;
347
Gavin Shan4cbde032021-09-02 14:52:45 -0700348 if (args->pud_pfn == ULONG_MAX)
349 return;
350
Anshuman Khandual6315df42020-08-06 23:19:25 -0700351 pr_debug("Validating PUD advanced\n");
Anshuman Khanduala5c3b9f2020-08-06 23:19:20 -0700352 /* Align the address wrt HPAGE_PUD_SIZE */
Gerald Schaefer04f7ce32021-06-04 20:01:18 -0700353 vaddr &= HPAGE_PUD_MASK;
Anshuman Khanduala5c3b9f2020-08-06 23:19:20 -0700354
Gavin Shan4cbde032021-09-02 14:52:45 -0700355 pud = pfn_pud(args->pud_pfn, args->page_prot);
356 set_pud_at(args->mm, vaddr, args->pudp, pud);
357 pudp_set_wrprotect(args->mm, vaddr, args->pudp);
358 pud = READ_ONCE(*args->pudp);
Anshuman Khanduala5c3b9f2020-08-06 23:19:20 -0700359 WARN_ON(pud_write(pud));
360
361#ifndef __PAGETABLE_PMD_FOLDED
Gavin Shan4cbde032021-09-02 14:52:45 -0700362 pudp_huge_get_and_clear(args->mm, vaddr, args->pudp);
363 pud = READ_ONCE(*args->pudp);
Anshuman Khanduala5c3b9f2020-08-06 23:19:20 -0700364 WARN_ON(!pud_none(pud));
Anshuman Khanduala5c3b9f2020-08-06 23:19:20 -0700365#endif /* __PAGETABLE_PMD_FOLDED */
Gavin Shan4cbde032021-09-02 14:52:45 -0700366 pud = pfn_pud(args->pud_pfn, args->page_prot);
Anshuman Khanduala5c3b9f2020-08-06 23:19:20 -0700367 pud = pud_wrprotect(pud);
368 pud = pud_mkclean(pud);
Gavin Shan4cbde032021-09-02 14:52:45 -0700369 set_pud_at(args->mm, vaddr, args->pudp, pud);
Anshuman Khanduala5c3b9f2020-08-06 23:19:20 -0700370 pud = pud_mkwrite(pud);
371 pud = pud_mkdirty(pud);
Gavin Shan4cbde032021-09-02 14:52:45 -0700372 pudp_set_access_flags(args->vma, vaddr, args->pudp, pud, 1);
373 pud = READ_ONCE(*args->pudp);
Anshuman Khanduala5c3b9f2020-08-06 23:19:20 -0700374 WARN_ON(!(pud_write(pud) && pud_dirty(pud)));
375
Aneesh Kumar K.Vc3824e12020-10-15 20:04:46 -0700376#ifndef __PAGETABLE_PMD_FOLDED
Gavin Shan4cbde032021-09-02 14:52:45 -0700377 pudp_huge_get_and_clear_full(args->mm, vaddr, args->pudp, 1);
378 pud = READ_ONCE(*args->pudp);
Aneesh Kumar K.Vc3824e12020-10-15 20:04:46 -0700379 WARN_ON(!pud_none(pud));
380#endif /* __PAGETABLE_PMD_FOLDED */
381
Gavin Shan4cbde032021-09-02 14:52:45 -0700382 pud = pfn_pud(args->pud_pfn, args->page_prot);
Anshuman Khanduala5c3b9f2020-08-06 23:19:20 -0700383 pud = pud_mkyoung(pud);
Gavin Shan4cbde032021-09-02 14:52:45 -0700384 set_pud_at(args->mm, vaddr, args->pudp, pud);
385 pudp_test_and_clear_young(args->vma, vaddr, args->pudp);
386 pud = READ_ONCE(*args->pudp);
Anshuman Khanduala5c3b9f2020-08-06 23:19:20 -0700387 WARN_ON(pud_young(pud));
Aneesh Kumar K.V13af0502020-10-15 20:04:59 -0700388
Gavin Shan4cbde032021-09-02 14:52:45 -0700389 pudp_huge_get_and_clear(args->mm, vaddr, args->pudp);
Anshuman Khanduala5c3b9f2020-08-06 23:19:20 -0700390}
391
Gavin Shan8983d232021-09-02 14:52:25 -0700392static void __init pud_leaf_tests(struct pgtable_debug_args *args)
Anshuman Khanduala5c3b9f2020-08-06 23:19:20 -0700393{
Anshuman Khandual65ac1a62021-06-28 19:35:10 -0700394 pud_t pud;
395
396 if (!has_transparent_hugepage())
397 return;
Anshuman Khanduala5c3b9f2020-08-06 23:19:20 -0700398
Anshuman Khandual6315df42020-08-06 23:19:25 -0700399 pr_debug("Validating PUD leaf\n");
Gavin Shan8983d232021-09-02 14:52:25 -0700400 pud = pfn_pud(args->fixed_pud_pfn, args->page_prot);
Anshuman Khanduala5c3b9f2020-08-06 23:19:20 -0700401 /*
402 * PUD based THP is a leaf entry.
403 */
404 pud = pud_mkhuge(pud);
405 WARN_ON(!pud_leaf(pud));
406}
Shixin Liu5fe77be2021-06-30 18:47:37 -0700407#else /* !CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD */
Gavin Shan36b77d12021-09-02 14:52:22 -0700408static void __init pud_basic_tests(struct pgtable_debug_args *args, int idx) { }
Gavin Shan4cbde032021-09-02 14:52:45 -0700409static void __init pud_advanced_tests(struct pgtable_debug_args *args) { }
Gavin Shan8983d232021-09-02 14:52:25 -0700410static void __init pud_leaf_tests(struct pgtable_debug_args *args) { }
Shixin Liu5fe77be2021-06-30 18:47:37 -0700411#endif /* CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD */
412#else /* !CONFIG_TRANSPARENT_HUGEPAGE */
Gavin Shan36b77d12021-09-02 14:52:22 -0700413static void __init pmd_basic_tests(struct pgtable_debug_args *args, int idx) { }
414static void __init pud_basic_tests(struct pgtable_debug_args *args, int idx) { }
Gavin Shanc0fe07b2021-09-02 14:52:41 -0700415static void __init pmd_advanced_tests(struct pgtable_debug_args *args) { }
Gavin Shan4cbde032021-09-02 14:52:45 -0700416static void __init pud_advanced_tests(struct pgtable_debug_args *args) { }
Gavin Shan8983d232021-09-02 14:52:25 -0700417static void __init pmd_leaf_tests(struct pgtable_debug_args *args) { }
418static void __init pud_leaf_tests(struct pgtable_debug_args *args) { }
419static void __init pmd_savedwrite_tests(struct pgtable_debug_args *args) { }
Shixin Liu5fe77be2021-06-30 18:47:37 -0700420#endif /* CONFIG_TRANSPARENT_HUGEPAGE */
Anshuman Khanduala5c3b9f2020-08-06 23:19:20 -0700421
Aneesh Kumar K.V85a14462020-10-15 20:04:36 -0700422#ifdef CONFIG_HAVE_ARCH_HUGE_VMAP
Gavin Shanc0fe07b2021-09-02 14:52:41 -0700423static void __init pmd_huge_tests(struct pgtable_debug_args *args)
Shixin Liu5fe77be2021-06-30 18:47:37 -0700424{
425 pmd_t pmd;
426
Gavin Shanc0fe07b2021-09-02 14:52:41 -0700427 if (!arch_vmap_pmd_supported(args->page_prot))
Shixin Liu5fe77be2021-06-30 18:47:37 -0700428 return;
429
430 pr_debug("Validating PMD huge\n");
431 /*
432 * X86 defined pmd_set_huge() verifies that the given
433 * PMD is not a populated non-leaf entry.
434 */
Gavin Shanc0fe07b2021-09-02 14:52:41 -0700435 WRITE_ONCE(*args->pmdp, __pmd(0));
436 WARN_ON(!pmd_set_huge(args->pmdp, __pfn_to_phys(args->fixed_pmd_pfn), args->page_prot));
437 WARN_ON(!pmd_clear_huge(args->pmdp));
438 pmd = READ_ONCE(*args->pmdp);
Shixin Liu5fe77be2021-06-30 18:47:37 -0700439 WARN_ON(!pmd_none(pmd));
440}
441
Gavin Shan4cbde032021-09-02 14:52:45 -0700442static void __init pud_huge_tests(struct pgtable_debug_args *args)
Anshuman Khanduala5c3b9f2020-08-06 23:19:20 -0700443{
444 pud_t pud;
445
Gavin Shan4cbde032021-09-02 14:52:45 -0700446 if (!arch_vmap_pud_supported(args->page_prot))
Anshuman Khanduala5c3b9f2020-08-06 23:19:20 -0700447 return;
Anshuman Khandual6315df42020-08-06 23:19:25 -0700448
449 pr_debug("Validating PUD huge\n");
Anshuman Khanduala5c3b9f2020-08-06 23:19:20 -0700450 /*
451 * X86 defined pud_set_huge() verifies that the given
452 * PUD is not a populated non-leaf entry.
453 */
Gavin Shan4cbde032021-09-02 14:52:45 -0700454 WRITE_ONCE(*args->pudp, __pud(0));
455 WARN_ON(!pud_set_huge(args->pudp, __pfn_to_phys(args->fixed_pud_pfn), args->page_prot));
456 WARN_ON(!pud_clear_huge(args->pudp));
457 pud = READ_ONCE(*args->pudp);
Anshuman Khanduala5c3b9f2020-08-06 23:19:20 -0700458 WARN_ON(!pud_none(pud));
459}
Aneesh Kumar K.V85a14462020-10-15 20:04:36 -0700460#else /* !CONFIG_HAVE_ARCH_HUGE_VMAP */
Gavin Shanc0fe07b2021-09-02 14:52:41 -0700461static void __init pmd_huge_tests(struct pgtable_debug_args *args) { }
Gavin Shan4cbde032021-09-02 14:52:45 -0700462static void __init pud_huge_tests(struct pgtable_debug_args *args) { }
Shixin Liu5fe77be2021-06-30 18:47:37 -0700463#endif /* CONFIG_HAVE_ARCH_HUGE_VMAP */
Anshuman Khandual399145f2020-06-04 16:47:15 -0700464
Gavin Shan36b77d12021-09-02 14:52:22 -0700465static void __init p4d_basic_tests(struct pgtable_debug_args *args)
Anshuman Khandual399145f2020-06-04 16:47:15 -0700466{
467 p4d_t p4d;
468
Anshuman Khandual6315df42020-08-06 23:19:25 -0700469 pr_debug("Validating P4D basic\n");
Anshuman Khandual399145f2020-06-04 16:47:15 -0700470 memset(&p4d, RANDOM_NZVALUE, sizeof(p4d_t));
471 WARN_ON(!p4d_same(p4d, p4d));
472}
473
Gavin Shan36b77d12021-09-02 14:52:22 -0700474static void __init pgd_basic_tests(struct pgtable_debug_args *args)
Anshuman Khandual399145f2020-06-04 16:47:15 -0700475{
476 pgd_t pgd;
477
Anshuman Khandual6315df42020-08-06 23:19:25 -0700478 pr_debug("Validating PGD basic\n");
Anshuman Khandual399145f2020-06-04 16:47:15 -0700479 memset(&pgd, RANDOM_NZVALUE, sizeof(pgd_t));
480 WARN_ON(!pgd_same(pgd, pgd));
481}
482
483#ifndef __PAGETABLE_PUD_FOLDED
Gavin Shan4cbde032021-09-02 14:52:45 -0700484static void __init pud_clear_tests(struct pgtable_debug_args *args)
Anshuman Khandual399145f2020-06-04 16:47:15 -0700485{
Gavin Shan4cbde032021-09-02 14:52:45 -0700486 pud_t pud = READ_ONCE(*args->pudp);
Anshuman Khandual399145f2020-06-04 16:47:15 -0700487
Gavin Shan4cbde032021-09-02 14:52:45 -0700488 if (mm_pmd_folded(args->mm))
Anshuman Khandual399145f2020-06-04 16:47:15 -0700489 return;
490
Anshuman Khandual6315df42020-08-06 23:19:25 -0700491 pr_debug("Validating PUD clear\n");
Anshuman Khandual399145f2020-06-04 16:47:15 -0700492 pud = __pud(pud_val(pud) | RANDOM_ORVALUE);
Gavin Shan4cbde032021-09-02 14:52:45 -0700493 WRITE_ONCE(*args->pudp, pud);
494 pud_clear(args->pudp);
495 pud = READ_ONCE(*args->pudp);
Anshuman Khandual399145f2020-06-04 16:47:15 -0700496 WARN_ON(!pud_none(pud));
497}
498
Gavin Shan4cbde032021-09-02 14:52:45 -0700499static void __init pud_populate_tests(struct pgtable_debug_args *args)
Anshuman Khandual399145f2020-06-04 16:47:15 -0700500{
501 pud_t pud;
502
Gavin Shan4cbde032021-09-02 14:52:45 -0700503 if (mm_pmd_folded(args->mm))
Anshuman Khandual399145f2020-06-04 16:47:15 -0700504 return;
Anshuman Khandual6315df42020-08-06 23:19:25 -0700505
506 pr_debug("Validating PUD populate\n");
Anshuman Khandual399145f2020-06-04 16:47:15 -0700507 /*
508 * This entry points to next level page table page.
509 * Hence this must not qualify as pud_bad().
510 */
Gavin Shan4cbde032021-09-02 14:52:45 -0700511 pud_populate(args->mm, args->pudp, args->start_pmdp);
512 pud = READ_ONCE(*args->pudp);
Anshuman Khandual399145f2020-06-04 16:47:15 -0700513 WARN_ON(pud_bad(pud));
514}
515#else /* !__PAGETABLE_PUD_FOLDED */
Gavin Shan4cbde032021-09-02 14:52:45 -0700516static void __init pud_clear_tests(struct pgtable_debug_args *args) { }
517static void __init pud_populate_tests(struct pgtable_debug_args *args) { }
Anshuman Khandual399145f2020-06-04 16:47:15 -0700518#endif /* PAGETABLE_PUD_FOLDED */
519
520#ifndef __PAGETABLE_P4D_FOLDED
521static void __init p4d_clear_tests(struct mm_struct *mm, p4d_t *p4dp)
522{
523 p4d_t p4d = READ_ONCE(*p4dp);
524
525 if (mm_pud_folded(mm))
526 return;
527
Anshuman Khandual6315df42020-08-06 23:19:25 -0700528 pr_debug("Validating P4D clear\n");
Anshuman Khandual399145f2020-06-04 16:47:15 -0700529 p4d = __p4d(p4d_val(p4d) | RANDOM_ORVALUE);
530 WRITE_ONCE(*p4dp, p4d);
531 p4d_clear(p4dp);
532 p4d = READ_ONCE(*p4dp);
533 WARN_ON(!p4d_none(p4d));
534}
535
536static void __init p4d_populate_tests(struct mm_struct *mm, p4d_t *p4dp,
537 pud_t *pudp)
538{
539 p4d_t p4d;
540
541 if (mm_pud_folded(mm))
542 return;
543
Anshuman Khandual6315df42020-08-06 23:19:25 -0700544 pr_debug("Validating P4D populate\n");
Anshuman Khandual399145f2020-06-04 16:47:15 -0700545 /*
546 * This entry points to next level page table page.
547 * Hence this must not qualify as p4d_bad().
548 */
549 pud_clear(pudp);
550 p4d_clear(p4dp);
551 p4d_populate(mm, p4dp, pudp);
552 p4d = READ_ONCE(*p4dp);
553 WARN_ON(p4d_bad(p4d));
554}
555
556static void __init pgd_clear_tests(struct mm_struct *mm, pgd_t *pgdp)
557{
558 pgd_t pgd = READ_ONCE(*pgdp);
559
560 if (mm_p4d_folded(mm))
561 return;
562
Anshuman Khandual6315df42020-08-06 23:19:25 -0700563 pr_debug("Validating PGD clear\n");
Anshuman Khandual399145f2020-06-04 16:47:15 -0700564 pgd = __pgd(pgd_val(pgd) | RANDOM_ORVALUE);
565 WRITE_ONCE(*pgdp, pgd);
566 pgd_clear(pgdp);
567 pgd = READ_ONCE(*pgdp);
568 WARN_ON(!pgd_none(pgd));
569}
570
571static void __init pgd_populate_tests(struct mm_struct *mm, pgd_t *pgdp,
572 p4d_t *p4dp)
573{
574 pgd_t pgd;
575
576 if (mm_p4d_folded(mm))
577 return;
578
Anshuman Khandual6315df42020-08-06 23:19:25 -0700579 pr_debug("Validating PGD populate\n");
Anshuman Khandual399145f2020-06-04 16:47:15 -0700580 /*
581 * This entry points to next level page table page.
582 * Hence this must not qualify as pgd_bad().
583 */
584 p4d_clear(p4dp);
585 pgd_clear(pgdp);
586 pgd_populate(mm, pgdp, p4dp);
587 pgd = READ_ONCE(*pgdp);
588 WARN_ON(pgd_bad(pgd));
589}
590#else /* !__PAGETABLE_P4D_FOLDED */
591static void __init p4d_clear_tests(struct mm_struct *mm, p4d_t *p4dp) { }
592static void __init pgd_clear_tests(struct mm_struct *mm, pgd_t *pgdp) { }
593static void __init p4d_populate_tests(struct mm_struct *mm, p4d_t *p4dp,
594 pud_t *pudp)
595{
596}
597static void __init pgd_populate_tests(struct mm_struct *mm, pgd_t *pgdp,
598 p4d_t *p4dp)
599{
600}
601#endif /* PAGETABLE_P4D_FOLDED */
602
Gavin Shan44966c42021-09-02 14:52:38 -0700603static void __init pte_clear_tests(struct pgtable_debug_args *args)
Anshuman Khandual399145f2020-06-04 16:47:15 -0700604{
Gavin Shan44966c42021-09-02 14:52:38 -0700605 pte_t pte = pfn_pte(args->pte_pfn, args->page_prot);
606
607 if (args->pte_pfn == ULONG_MAX)
608 return;
Anshuman Khandual399145f2020-06-04 16:47:15 -0700609
Anshuman Khandual6315df42020-08-06 23:19:25 -0700610 pr_debug("Validating PTE clear\n");
Aneesh Kumar K.V401035d2020-10-15 20:05:06 -0700611#ifndef CONFIG_RISCV
Anshuman Khandual399145f2020-06-04 16:47:15 -0700612 pte = __pte(pte_val(pte) | RANDOM_ORVALUE);
Aneesh Kumar K.V401035d2020-10-15 20:05:06 -0700613#endif
Gavin Shan44966c42021-09-02 14:52:38 -0700614 set_pte_at(args->mm, args->vaddr, args->ptep, pte);
Anshuman Khandual399145f2020-06-04 16:47:15 -0700615 barrier();
Gavin Shan44966c42021-09-02 14:52:38 -0700616 pte_clear(args->mm, args->vaddr, args->ptep);
617 pte = ptep_get(args->ptep);
Anshuman Khandual399145f2020-06-04 16:47:15 -0700618 WARN_ON(!pte_none(pte));
619}
620
Gavin Shanc0fe07b2021-09-02 14:52:41 -0700621static void __init pmd_clear_tests(struct pgtable_debug_args *args)
Anshuman Khandual399145f2020-06-04 16:47:15 -0700622{
Gavin Shanc0fe07b2021-09-02 14:52:41 -0700623 pmd_t pmd = READ_ONCE(*args->pmdp);
Anshuman Khandual399145f2020-06-04 16:47:15 -0700624
Anshuman Khandual6315df42020-08-06 23:19:25 -0700625 pr_debug("Validating PMD clear\n");
Anshuman Khandual399145f2020-06-04 16:47:15 -0700626 pmd = __pmd(pmd_val(pmd) | RANDOM_ORVALUE);
Gavin Shanc0fe07b2021-09-02 14:52:41 -0700627 WRITE_ONCE(*args->pmdp, pmd);
628 pmd_clear(args->pmdp);
629 pmd = READ_ONCE(*args->pmdp);
Anshuman Khandual399145f2020-06-04 16:47:15 -0700630 WARN_ON(!pmd_none(pmd));
631}
632
Gavin Shanc0fe07b2021-09-02 14:52:41 -0700633static void __init pmd_populate_tests(struct pgtable_debug_args *args)
Anshuman Khandual399145f2020-06-04 16:47:15 -0700634{
635 pmd_t pmd;
636
Anshuman Khandual6315df42020-08-06 23:19:25 -0700637 pr_debug("Validating PMD populate\n");
Anshuman Khandual399145f2020-06-04 16:47:15 -0700638 /*
639 * This entry points to next level page table page.
640 * Hence this must not qualify as pmd_bad().
641 */
Gavin Shanc0fe07b2021-09-02 14:52:41 -0700642 pmd_populate(args->mm, args->pmdp, args->start_ptep);
643 pmd = READ_ONCE(*args->pmdp);
Anshuman Khandual399145f2020-06-04 16:47:15 -0700644 WARN_ON(pmd_bad(pmd));
645}
646
Gavin Shan8cb183f2021-09-02 14:52:28 -0700647static void __init pte_special_tests(struct pgtable_debug_args *args)
Anshuman Khandual05289402020-08-06 23:19:16 -0700648{
Gavin Shan8cb183f2021-09-02 14:52:28 -0700649 pte_t pte = pfn_pte(args->fixed_pte_pfn, args->page_prot);
Anshuman Khandual05289402020-08-06 23:19:16 -0700650
651 if (!IS_ENABLED(CONFIG_ARCH_HAS_PTE_SPECIAL))
652 return;
653
Anshuman Khandual6315df42020-08-06 23:19:25 -0700654 pr_debug("Validating PTE special\n");
Anshuman Khandual05289402020-08-06 23:19:16 -0700655 WARN_ON(!pte_special(pte_mkspecial(pte)));
656}
657
Gavin Shan8cb183f2021-09-02 14:52:28 -0700658static void __init pte_protnone_tests(struct pgtable_debug_args *args)
Anshuman Khandual05289402020-08-06 23:19:16 -0700659{
Gavin Shan8cb183f2021-09-02 14:52:28 -0700660 pte_t pte = pfn_pte(args->fixed_pte_pfn, args->page_prot_none);
Anshuman Khandual05289402020-08-06 23:19:16 -0700661
662 if (!IS_ENABLED(CONFIG_NUMA_BALANCING))
663 return;
664
Anshuman Khandual6315df42020-08-06 23:19:25 -0700665 pr_debug("Validating PTE protnone\n");
Anshuman Khandual05289402020-08-06 23:19:16 -0700666 WARN_ON(!pte_protnone(pte));
667 WARN_ON(!pte_present(pte));
668}
669
670#ifdef CONFIG_TRANSPARENT_HUGEPAGE
Gavin Shan8cb183f2021-09-02 14:52:28 -0700671static void __init pmd_protnone_tests(struct pgtable_debug_args *args)
Anshuman Khandual05289402020-08-06 23:19:16 -0700672{
Anshuman Khandual65ac1a62021-06-28 19:35:10 -0700673 pmd_t pmd;
Anshuman Khandual05289402020-08-06 23:19:16 -0700674
675 if (!IS_ENABLED(CONFIG_NUMA_BALANCING))
676 return;
677
Anshuman Khandual65ac1a62021-06-28 19:35:10 -0700678 if (!has_transparent_hugepage())
679 return;
680
Anshuman Khandual6315df42020-08-06 23:19:25 -0700681 pr_debug("Validating PMD protnone\n");
Gavin Shan8cb183f2021-09-02 14:52:28 -0700682 pmd = pmd_mkhuge(pfn_pmd(args->fixed_pmd_pfn, args->page_prot_none));
Anshuman Khandual05289402020-08-06 23:19:16 -0700683 WARN_ON(!pmd_protnone(pmd));
684 WARN_ON(!pmd_present(pmd));
685}
686#else /* !CONFIG_TRANSPARENT_HUGEPAGE */
Gavin Shan8cb183f2021-09-02 14:52:28 -0700687static void __init pmd_protnone_tests(struct pgtable_debug_args *args) { }
Anshuman Khandual05289402020-08-06 23:19:16 -0700688#endif /* CONFIG_TRANSPARENT_HUGEPAGE */
689
690#ifdef CONFIG_ARCH_HAS_PTE_DEVMAP
Gavin Shan8cb183f2021-09-02 14:52:28 -0700691static void __init pte_devmap_tests(struct pgtable_debug_args *args)
Anshuman Khandual05289402020-08-06 23:19:16 -0700692{
Gavin Shan8cb183f2021-09-02 14:52:28 -0700693 pte_t pte = pfn_pte(args->fixed_pte_pfn, args->page_prot);
Anshuman Khandual05289402020-08-06 23:19:16 -0700694
Anshuman Khandual6315df42020-08-06 23:19:25 -0700695 pr_debug("Validating PTE devmap\n");
Anshuman Khandual05289402020-08-06 23:19:16 -0700696 WARN_ON(!pte_devmap(pte_mkdevmap(pte)));
697}
698
699#ifdef CONFIG_TRANSPARENT_HUGEPAGE
Gavin Shan8cb183f2021-09-02 14:52:28 -0700700static void __init pmd_devmap_tests(struct pgtable_debug_args *args)
Anshuman Khandual05289402020-08-06 23:19:16 -0700701{
Anshuman Khandual65ac1a62021-06-28 19:35:10 -0700702 pmd_t pmd;
703
704 if (!has_transparent_hugepage())
705 return;
Anshuman Khandual05289402020-08-06 23:19:16 -0700706
Anshuman Khandual6315df42020-08-06 23:19:25 -0700707 pr_debug("Validating PMD devmap\n");
Gavin Shan8cb183f2021-09-02 14:52:28 -0700708 pmd = pfn_pmd(args->fixed_pmd_pfn, args->page_prot);
Anshuman Khandual05289402020-08-06 23:19:16 -0700709 WARN_ON(!pmd_devmap(pmd_mkdevmap(pmd)));
710}
711
712#ifdef CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD
Gavin Shan8cb183f2021-09-02 14:52:28 -0700713static void __init pud_devmap_tests(struct pgtable_debug_args *args)
Anshuman Khandual05289402020-08-06 23:19:16 -0700714{
Anshuman Khandual65ac1a62021-06-28 19:35:10 -0700715 pud_t pud;
716
717 if (!has_transparent_hugepage())
718 return;
Anshuman Khandual05289402020-08-06 23:19:16 -0700719
Anshuman Khandual6315df42020-08-06 23:19:25 -0700720 pr_debug("Validating PUD devmap\n");
Gavin Shan8cb183f2021-09-02 14:52:28 -0700721 pud = pfn_pud(args->fixed_pud_pfn, args->page_prot);
Anshuman Khandual05289402020-08-06 23:19:16 -0700722 WARN_ON(!pud_devmap(pud_mkdevmap(pud)));
723}
724#else /* !CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD */
Gavin Shan8cb183f2021-09-02 14:52:28 -0700725static void __init pud_devmap_tests(struct pgtable_debug_args *args) { }
Anshuman Khandual05289402020-08-06 23:19:16 -0700726#endif /* CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD */
727#else /* CONFIG_TRANSPARENT_HUGEPAGE */
Gavin Shan8cb183f2021-09-02 14:52:28 -0700728static void __init pmd_devmap_tests(struct pgtable_debug_args *args) { }
729static void __init pud_devmap_tests(struct pgtable_debug_args *args) { }
Anshuman Khandual05289402020-08-06 23:19:16 -0700730#endif /* CONFIG_TRANSPARENT_HUGEPAGE */
731#else
Gavin Shan8cb183f2021-09-02 14:52:28 -0700732static void __init pte_devmap_tests(struct pgtable_debug_args *args) { }
733static void __init pmd_devmap_tests(struct pgtable_debug_args *args) { }
734static void __init pud_devmap_tests(struct pgtable_debug_args *args) { }
Anshuman Khandual05289402020-08-06 23:19:16 -0700735#endif /* CONFIG_ARCH_HAS_PTE_DEVMAP */
736
Gavin Shan5f447e82021-09-02 14:52:32 -0700737static void __init pte_soft_dirty_tests(struct pgtable_debug_args *args)
Anshuman Khandual05289402020-08-06 23:19:16 -0700738{
Gavin Shan5f447e82021-09-02 14:52:32 -0700739 pte_t pte = pfn_pte(args->fixed_pte_pfn, args->page_prot);
Anshuman Khandual05289402020-08-06 23:19:16 -0700740
741 if (!IS_ENABLED(CONFIG_MEM_SOFT_DIRTY))
742 return;
743
Anshuman Khandual6315df42020-08-06 23:19:25 -0700744 pr_debug("Validating PTE soft dirty\n");
Anshuman Khandual05289402020-08-06 23:19:16 -0700745 WARN_ON(!pte_soft_dirty(pte_mksoft_dirty(pte)));
746 WARN_ON(pte_soft_dirty(pte_clear_soft_dirty(pte)));
747}
748
Gavin Shan5f447e82021-09-02 14:52:32 -0700749static void __init pte_swap_soft_dirty_tests(struct pgtable_debug_args *args)
Anshuman Khandual05289402020-08-06 23:19:16 -0700750{
Gavin Shan5f447e82021-09-02 14:52:32 -0700751 pte_t pte = pfn_pte(args->fixed_pte_pfn, args->page_prot);
Anshuman Khandual05289402020-08-06 23:19:16 -0700752
753 if (!IS_ENABLED(CONFIG_MEM_SOFT_DIRTY))
754 return;
755
Anshuman Khandual6315df42020-08-06 23:19:25 -0700756 pr_debug("Validating PTE swap soft dirty\n");
Anshuman Khandual05289402020-08-06 23:19:16 -0700757 WARN_ON(!pte_swp_soft_dirty(pte_swp_mksoft_dirty(pte)));
758 WARN_ON(pte_swp_soft_dirty(pte_swp_clear_soft_dirty(pte)));
759}
760
761#ifdef CONFIG_TRANSPARENT_HUGEPAGE
Gavin Shan5f447e82021-09-02 14:52:32 -0700762static void __init pmd_soft_dirty_tests(struct pgtable_debug_args *args)
Anshuman Khandual05289402020-08-06 23:19:16 -0700763{
Anshuman Khandual65ac1a62021-06-28 19:35:10 -0700764 pmd_t pmd;
Anshuman Khandual05289402020-08-06 23:19:16 -0700765
766 if (!IS_ENABLED(CONFIG_MEM_SOFT_DIRTY))
767 return;
768
Anshuman Khandual65ac1a62021-06-28 19:35:10 -0700769 if (!has_transparent_hugepage())
770 return;
771
Anshuman Khandual6315df42020-08-06 23:19:25 -0700772 pr_debug("Validating PMD soft dirty\n");
Gavin Shan5f447e82021-09-02 14:52:32 -0700773 pmd = pfn_pmd(args->fixed_pmd_pfn, args->page_prot);
Anshuman Khandual05289402020-08-06 23:19:16 -0700774 WARN_ON(!pmd_soft_dirty(pmd_mksoft_dirty(pmd)));
775 WARN_ON(pmd_soft_dirty(pmd_clear_soft_dirty(pmd)));
776}
777
Gavin Shan5f447e82021-09-02 14:52:32 -0700778static void __init pmd_swap_soft_dirty_tests(struct pgtable_debug_args *args)
Anshuman Khandual05289402020-08-06 23:19:16 -0700779{
Anshuman Khandual65ac1a62021-06-28 19:35:10 -0700780 pmd_t pmd;
Anshuman Khandual05289402020-08-06 23:19:16 -0700781
782 if (!IS_ENABLED(CONFIG_MEM_SOFT_DIRTY) ||
783 !IS_ENABLED(CONFIG_ARCH_ENABLE_THP_MIGRATION))
784 return;
785
Anshuman Khandual65ac1a62021-06-28 19:35:10 -0700786 if (!has_transparent_hugepage())
787 return;
788
Anshuman Khandual6315df42020-08-06 23:19:25 -0700789 pr_debug("Validating PMD swap soft dirty\n");
Gavin Shan5f447e82021-09-02 14:52:32 -0700790 pmd = pfn_pmd(args->fixed_pmd_pfn, args->page_prot);
Anshuman Khandual05289402020-08-06 23:19:16 -0700791 WARN_ON(!pmd_swp_soft_dirty(pmd_swp_mksoft_dirty(pmd)));
792 WARN_ON(pmd_swp_soft_dirty(pmd_swp_clear_soft_dirty(pmd)));
793}
Shixin Liub593b902021-06-30 18:47:40 -0700794#else /* !CONFIG_TRANSPARENT_HUGEPAGE */
Gavin Shan5f447e82021-09-02 14:52:32 -0700795static void __init pmd_soft_dirty_tests(struct pgtable_debug_args *args) { }
796static void __init pmd_swap_soft_dirty_tests(struct pgtable_debug_args *args) { }
Shixin Liub593b902021-06-30 18:47:40 -0700797#endif /* CONFIG_TRANSPARENT_HUGEPAGE */
Anshuman Khandual05289402020-08-06 23:19:16 -0700798
Gavin Shan5f447e82021-09-02 14:52:32 -0700799static void __init pte_swap_tests(struct pgtable_debug_args *args)
Anshuman Khandual05289402020-08-06 23:19:16 -0700800{
801 swp_entry_t swp;
802 pte_t pte;
803
Anshuman Khandual6315df42020-08-06 23:19:25 -0700804 pr_debug("Validating PTE swap\n");
Gavin Shan5f447e82021-09-02 14:52:32 -0700805 pte = pfn_pte(args->fixed_pte_pfn, args->page_prot);
Anshuman Khandual05289402020-08-06 23:19:16 -0700806 swp = __pte_to_swp_entry(pte);
807 pte = __swp_entry_to_pte(swp);
Gavin Shan5f447e82021-09-02 14:52:32 -0700808 WARN_ON(args->fixed_pte_pfn != pte_pfn(pte));
Anshuman Khandual05289402020-08-06 23:19:16 -0700809}
810
811#ifdef CONFIG_ARCH_ENABLE_THP_MIGRATION
Gavin Shan5f447e82021-09-02 14:52:32 -0700812static void __init pmd_swap_tests(struct pgtable_debug_args *args)
Anshuman Khandual05289402020-08-06 23:19:16 -0700813{
814 swp_entry_t swp;
815 pmd_t pmd;
816
Anshuman Khandual65ac1a62021-06-28 19:35:10 -0700817 if (!has_transparent_hugepage())
818 return;
819
Anshuman Khandual6315df42020-08-06 23:19:25 -0700820 pr_debug("Validating PMD swap\n");
Gavin Shan5f447e82021-09-02 14:52:32 -0700821 pmd = pfn_pmd(args->fixed_pmd_pfn, args->page_prot);
Anshuman Khandual05289402020-08-06 23:19:16 -0700822 swp = __pmd_to_swp_entry(pmd);
823 pmd = __swp_entry_to_pmd(swp);
Gavin Shan5f447e82021-09-02 14:52:32 -0700824 WARN_ON(args->fixed_pmd_pfn != pmd_pfn(pmd));
Anshuman Khandual05289402020-08-06 23:19:16 -0700825}
826#else /* !CONFIG_ARCH_ENABLE_THP_MIGRATION */
Gavin Shan5f447e82021-09-02 14:52:32 -0700827static void __init pmd_swap_tests(struct pgtable_debug_args *args) { }
Anshuman Khandual05289402020-08-06 23:19:16 -0700828#endif /* CONFIG_ARCH_ENABLE_THP_MIGRATION */
829
Gavin Shan4878a882021-09-02 14:52:35 -0700830static void __init swap_migration_tests(struct pgtable_debug_args *args)
Anshuman Khandual05289402020-08-06 23:19:16 -0700831{
832 struct page *page;
833 swp_entry_t swp;
834
835 if (!IS_ENABLED(CONFIG_MIGRATION))
836 return;
Anshuman Khandual6315df42020-08-06 23:19:25 -0700837
Anshuman Khandual05289402020-08-06 23:19:16 -0700838 /*
839 * swap_migration_tests() requires a dedicated page as it needs to
840 * be locked before creating a migration entry from it. Locking the
841 * page that actually maps kernel text ('start_kernel') can be real
Gavin Shan4878a882021-09-02 14:52:35 -0700842 * problematic. Lets use the allocated page explicitly for this
843 * purpose.
Anshuman Khandual05289402020-08-06 23:19:16 -0700844 */
Gavin Shan4878a882021-09-02 14:52:35 -0700845 page = (args->pte_pfn != ULONG_MAX) ? pfn_to_page(args->pte_pfn) : NULL;
846 if (!page)
Anshuman Khandual05289402020-08-06 23:19:16 -0700847 return;
Gavin Shan4878a882021-09-02 14:52:35 -0700848
849 pr_debug("Validating swap migration\n");
Anshuman Khandual05289402020-08-06 23:19:16 -0700850
851 /*
852 * make_migration_entry() expects given page to be
853 * locked, otherwise it stumbles upon a BUG_ON().
854 */
855 __SetPageLocked(page);
Alistair Popple4dd845b2021-06-30 18:54:09 -0700856 swp = make_writable_migration_entry(page_to_pfn(page));
Anshuman Khandual05289402020-08-06 23:19:16 -0700857 WARN_ON(!is_migration_entry(swp));
Alistair Popple4dd845b2021-06-30 18:54:09 -0700858 WARN_ON(!is_writable_migration_entry(swp));
Anshuman Khandual05289402020-08-06 23:19:16 -0700859
Alistair Popple4dd845b2021-06-30 18:54:09 -0700860 swp = make_readable_migration_entry(swp_offset(swp));
Anshuman Khandual05289402020-08-06 23:19:16 -0700861 WARN_ON(!is_migration_entry(swp));
Alistair Popple4dd845b2021-06-30 18:54:09 -0700862 WARN_ON(is_writable_migration_entry(swp));
Anshuman Khandual05289402020-08-06 23:19:16 -0700863
Alistair Popple4dd845b2021-06-30 18:54:09 -0700864 swp = make_readable_migration_entry(page_to_pfn(page));
Anshuman Khandual05289402020-08-06 23:19:16 -0700865 WARN_ON(!is_migration_entry(swp));
Alistair Popple4dd845b2021-06-30 18:54:09 -0700866 WARN_ON(is_writable_migration_entry(swp));
Anshuman Khandual05289402020-08-06 23:19:16 -0700867 __ClearPageLocked(page);
Anshuman Khandual05289402020-08-06 23:19:16 -0700868}
869
870#ifdef CONFIG_HUGETLB_PAGE
Gavin Shan36b77d12021-09-02 14:52:22 -0700871static void __init hugetlb_basic_tests(struct pgtable_debug_args *args)
Anshuman Khandual05289402020-08-06 23:19:16 -0700872{
873 struct page *page;
874 pte_t pte;
875
Anshuman Khandual6315df42020-08-06 23:19:25 -0700876 pr_debug("Validating HugeTLB basic\n");
Anshuman Khandual05289402020-08-06 23:19:16 -0700877 /*
878 * Accessing the page associated with the pfn is safe here,
879 * as it was previously derived from a real kernel symbol.
880 */
Gavin Shan36b77d12021-09-02 14:52:22 -0700881 page = pfn_to_page(args->fixed_pmd_pfn);
882 pte = mk_huge_pte(page, args->page_prot);
Anshuman Khandual05289402020-08-06 23:19:16 -0700883
884 WARN_ON(!huge_pte_dirty(huge_pte_mkdirty(pte)));
885 WARN_ON(!huge_pte_write(huge_pte_mkwrite(huge_pte_wrprotect(pte))));
886 WARN_ON(huge_pte_write(huge_pte_wrprotect(huge_pte_mkwrite(pte))));
887
888#ifdef CONFIG_ARCH_WANT_GENERAL_HUGETLB
Gavin Shan36b77d12021-09-02 14:52:22 -0700889 pte = pfn_pte(args->fixed_pmd_pfn, args->page_prot);
Anshuman Khandual05289402020-08-06 23:19:16 -0700890
891 WARN_ON(!pte_huge(pte_mkhuge(pte)));
892#endif /* CONFIG_ARCH_WANT_GENERAL_HUGETLB */
893}
894#else /* !CONFIG_HUGETLB_PAGE */
Gavin Shan36b77d12021-09-02 14:52:22 -0700895static void __init hugetlb_basic_tests(struct pgtable_debug_args *args) { }
Anshuman Khandual05289402020-08-06 23:19:16 -0700896#endif /* CONFIG_HUGETLB_PAGE */
897
898#ifdef CONFIG_TRANSPARENT_HUGEPAGE
Gavin Shan4878a882021-09-02 14:52:35 -0700899static void __init pmd_thp_tests(struct pgtable_debug_args *args)
Anshuman Khandual05289402020-08-06 23:19:16 -0700900{
901 pmd_t pmd;
902
903 if (!has_transparent_hugepage())
904 return;
905
Anshuman Khandual6315df42020-08-06 23:19:25 -0700906 pr_debug("Validating PMD based THP\n");
Anshuman Khandual05289402020-08-06 23:19:16 -0700907 /*
908 * pmd_trans_huge() and pmd_present() must return positive after
909 * MMU invalidation with pmd_mkinvalid(). This behavior is an
910 * optimization for transparent huge page. pmd_trans_huge() must
911 * be true if pmd_page() returns a valid THP to avoid taking the
912 * pmd_lock when others walk over non transhuge pmds (i.e. there
913 * are no THP allocated). Especially when splitting a THP and
914 * removing the present bit from the pmd, pmd_trans_huge() still
915 * needs to return true. pmd_present() should be true whenever
916 * pmd_trans_huge() returns true.
917 */
Gavin Shan4878a882021-09-02 14:52:35 -0700918 pmd = pfn_pmd(args->fixed_pmd_pfn, args->page_prot);
Anshuman Khandual05289402020-08-06 23:19:16 -0700919 WARN_ON(!pmd_trans_huge(pmd_mkhuge(pmd)));
920
921#ifndef __HAVE_ARCH_PMDP_INVALIDATE
922 WARN_ON(!pmd_trans_huge(pmd_mkinvalid(pmd_mkhuge(pmd))));
923 WARN_ON(!pmd_present(pmd_mkinvalid(pmd_mkhuge(pmd))));
924#endif /* __HAVE_ARCH_PMDP_INVALIDATE */
925}
926
927#ifdef CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD
Gavin Shan4878a882021-09-02 14:52:35 -0700928static void __init pud_thp_tests(struct pgtable_debug_args *args)
Anshuman Khandual05289402020-08-06 23:19:16 -0700929{
930 pud_t pud;
931
932 if (!has_transparent_hugepage())
933 return;
934
Anshuman Khandual6315df42020-08-06 23:19:25 -0700935 pr_debug("Validating PUD based THP\n");
Gavin Shan4878a882021-09-02 14:52:35 -0700936 pud = pfn_pud(args->fixed_pud_pfn, args->page_prot);
Anshuman Khandual05289402020-08-06 23:19:16 -0700937 WARN_ON(!pud_trans_huge(pud_mkhuge(pud)));
938
939 /*
940 * pud_mkinvalid() has been dropped for now. Enable back
941 * these tests when it comes back with a modified pud_present().
942 *
943 * WARN_ON(!pud_trans_huge(pud_mkinvalid(pud_mkhuge(pud))));
944 * WARN_ON(!pud_present(pud_mkinvalid(pud_mkhuge(pud))));
945 */
946}
947#else /* !CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD */
Gavin Shan4878a882021-09-02 14:52:35 -0700948static void __init pud_thp_tests(struct pgtable_debug_args *args) { }
Anshuman Khandual05289402020-08-06 23:19:16 -0700949#endif /* CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD */
950#else /* !CONFIG_TRANSPARENT_HUGEPAGE */
Gavin Shan4878a882021-09-02 14:52:35 -0700951static void __init pmd_thp_tests(struct pgtable_debug_args *args) { }
952static void __init pud_thp_tests(struct pgtable_debug_args *args) { }
Anshuman Khandual05289402020-08-06 23:19:16 -0700953#endif /* CONFIG_TRANSPARENT_HUGEPAGE */
954
Anshuman Khandual399145f2020-06-04 16:47:15 -0700955static unsigned long __init get_random_vaddr(void)
956{
957 unsigned long random_vaddr, random_pages, total_user_pages;
958
959 total_user_pages = (TASK_SIZE - FIRST_USER_ADDRESS) / PAGE_SIZE;
960
961 random_pages = get_random_long() % total_user_pages;
962 random_vaddr = FIRST_USER_ADDRESS + random_pages * PAGE_SIZE;
963
964 return random_vaddr;
965}
966
Gavin Shan3c9b84f2021-09-02 14:52:19 -0700967static void __init destroy_args(struct pgtable_debug_args *args)
968{
969 struct page *page = NULL;
970
971 /* Free (huge) page */
972 if (IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE) &&
973 IS_ENABLED(CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD) &&
974 has_transparent_hugepage() &&
975 args->pud_pfn != ULONG_MAX) {
976 if (args->is_contiguous_page) {
977 free_contig_range(args->pud_pfn,
978 (1 << (HPAGE_PUD_SHIFT - PAGE_SHIFT)));
979 } else {
980 page = pfn_to_page(args->pud_pfn);
981 __free_pages(page, HPAGE_PUD_SHIFT - PAGE_SHIFT);
982 }
983
984 args->pud_pfn = ULONG_MAX;
985 args->pmd_pfn = ULONG_MAX;
986 args->pte_pfn = ULONG_MAX;
987 }
988
989 if (IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE) &&
990 has_transparent_hugepage() &&
991 args->pmd_pfn != ULONG_MAX) {
992 if (args->is_contiguous_page) {
993 free_contig_range(args->pmd_pfn, (1 << HPAGE_PMD_ORDER));
994 } else {
995 page = pfn_to_page(args->pmd_pfn);
996 __free_pages(page, HPAGE_PMD_ORDER);
997 }
998
999 args->pmd_pfn = ULONG_MAX;
1000 args->pte_pfn = ULONG_MAX;
1001 }
1002
1003 if (args->pte_pfn != ULONG_MAX) {
1004 page = pfn_to_page(args->pte_pfn);
1005 __free_pages(page, 0);
1006
1007 args->pte_pfn = ULONG_MAX;
1008 }
1009
1010 /* Free page table entries */
1011 if (args->start_ptep) {
1012 pte_free(args->mm, args->start_ptep);
1013 mm_dec_nr_ptes(args->mm);
1014 }
1015
1016 if (args->start_pmdp) {
1017 pmd_free(args->mm, args->start_pmdp);
1018 mm_dec_nr_pmds(args->mm);
1019 }
1020
1021 if (args->start_pudp) {
1022 pud_free(args->mm, args->start_pudp);
1023 mm_dec_nr_puds(args->mm);
1024 }
1025
1026 if (args->start_p4dp)
1027 p4d_free(args->mm, args->start_p4dp);
1028
1029 /* Free vma and mm struct */
1030 if (args->vma)
1031 vm_area_free(args->vma);
1032
1033 if (args->mm)
1034 mmdrop(args->mm);
1035}
1036
1037static struct page * __init
1038debug_vm_pgtable_alloc_huge_page(struct pgtable_debug_args *args, int order)
1039{
1040 struct page *page = NULL;
1041
1042#ifdef CONFIG_CONTIG_ALLOC
1043 if (order >= MAX_ORDER) {
1044 page = alloc_contig_pages((1 << order), GFP_KERNEL,
1045 first_online_node, NULL);
1046 if (page) {
1047 args->is_contiguous_page = true;
1048 return page;
1049 }
1050 }
1051#endif
1052
1053 if (order < MAX_ORDER)
1054 page = alloc_pages(GFP_KERNEL, order);
1055
1056 return page;
1057}
1058
1059static int __init init_args(struct pgtable_debug_args *args)
1060{
1061 struct page *page = NULL;
1062 phys_addr_t phys;
1063 int ret = 0;
1064
1065 /*
1066 * Initialize the debugging data.
1067 *
1068 * __P000 (or even __S000) will help create page table entries with
1069 * PROT_NONE permission as required for pxx_protnone_tests().
1070 */
1071 memset(args, 0, sizeof(*args));
1072 args->vaddr = get_random_vaddr();
1073 args->page_prot = vm_get_page_prot(VMFLAGS);
1074 args->page_prot_none = __P000;
1075 args->is_contiguous_page = false;
1076 args->pud_pfn = ULONG_MAX;
1077 args->pmd_pfn = ULONG_MAX;
1078 args->pte_pfn = ULONG_MAX;
1079 args->fixed_pgd_pfn = ULONG_MAX;
1080 args->fixed_p4d_pfn = ULONG_MAX;
1081 args->fixed_pud_pfn = ULONG_MAX;
1082 args->fixed_pmd_pfn = ULONG_MAX;
1083 args->fixed_pte_pfn = ULONG_MAX;
1084
1085 /* Allocate mm and vma */
1086 args->mm = mm_alloc();
1087 if (!args->mm) {
1088 pr_err("Failed to allocate mm struct\n");
1089 ret = -ENOMEM;
1090 goto error;
1091 }
1092
1093 args->vma = vm_area_alloc(args->mm);
1094 if (!args->vma) {
1095 pr_err("Failed to allocate vma\n");
1096 ret = -ENOMEM;
1097 goto error;
1098 }
1099
1100 /*
1101 * Allocate page table entries. They will be modified in the tests.
1102 * Lets save the page table entries so that they can be released
1103 * when the tests are completed.
1104 */
1105 args->pgdp = pgd_offset(args->mm, args->vaddr);
1106 args->p4dp = p4d_alloc(args->mm, args->pgdp, args->vaddr);
1107 if (!args->p4dp) {
1108 pr_err("Failed to allocate p4d entries\n");
1109 ret = -ENOMEM;
1110 goto error;
1111 }
1112 args->start_p4dp = p4d_offset(args->pgdp, 0UL);
1113 WARN_ON(!args->start_p4dp);
1114
1115 args->pudp = pud_alloc(args->mm, args->p4dp, args->vaddr);
1116 if (!args->pudp) {
1117 pr_err("Failed to allocate pud entries\n");
1118 ret = -ENOMEM;
1119 goto error;
1120 }
1121 args->start_pudp = pud_offset(args->p4dp, 0UL);
1122 WARN_ON(!args->start_pudp);
1123
1124 args->pmdp = pmd_alloc(args->mm, args->pudp, args->vaddr);
1125 if (!args->pmdp) {
1126 pr_err("Failed to allocate pmd entries\n");
1127 ret = -ENOMEM;
1128 goto error;
1129 }
1130 args->start_pmdp = pmd_offset(args->pudp, 0UL);
1131 WARN_ON(!args->start_pmdp);
1132
1133 if (pte_alloc(args->mm, args->pmdp)) {
1134 pr_err("Failed to allocate pte entries\n");
1135 ret = -ENOMEM;
1136 goto error;
1137 }
1138 args->start_ptep = pmd_pgtable(READ_ONCE(*args->pmdp));
1139 WARN_ON(!args->start_ptep);
1140
1141 /*
1142 * PFN for mapping at PTE level is determined from a standard kernel
1143 * text symbol. But pfns for higher page table levels are derived by
1144 * masking lower bits of this real pfn. These derived pfns might not
1145 * exist on the platform but that does not really matter as pfn_pxx()
1146 * helpers will still create appropriate entries for the test. This
1147 * helps avoid large memory block allocations to be used for mapping
1148 * at higher page table levels in some of the tests.
1149 */
1150 phys = __pa_symbol(&start_kernel);
1151 args->fixed_pgd_pfn = __phys_to_pfn(phys & PGDIR_MASK);
1152 args->fixed_p4d_pfn = __phys_to_pfn(phys & P4D_MASK);
1153 args->fixed_pud_pfn = __phys_to_pfn(phys & PUD_MASK);
1154 args->fixed_pmd_pfn = __phys_to_pfn(phys & PMD_MASK);
1155 args->fixed_pte_pfn = __phys_to_pfn(phys & PAGE_MASK);
1156 WARN_ON(!pfn_valid(args->fixed_pte_pfn));
1157
1158 /*
1159 * Allocate (huge) pages because some of the tests need to access
1160 * the data in the pages. The corresponding tests will be skipped
1161 * if we fail to allocate (huge) pages.
1162 */
1163 if (IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE) &&
1164 IS_ENABLED(CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD) &&
1165 has_transparent_hugepage()) {
1166 page = debug_vm_pgtable_alloc_huge_page(args,
1167 HPAGE_PUD_SHIFT - PAGE_SHIFT);
1168 if (page) {
1169 args->pud_pfn = page_to_pfn(page);
1170 args->pmd_pfn = args->pud_pfn;
1171 args->pte_pfn = args->pud_pfn;
1172 return 0;
1173 }
1174 }
1175
1176 if (IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE) &&
1177 has_transparent_hugepage()) {
1178 page = debug_vm_pgtable_alloc_huge_page(args, HPAGE_PMD_ORDER);
1179 if (page) {
1180 args->pmd_pfn = page_to_pfn(page);
1181 args->pte_pfn = args->pmd_pfn;
1182 return 0;
1183 }
1184 }
1185
1186 page = alloc_pages(GFP_KERNEL, 0);
1187 if (page)
1188 args->pte_pfn = page_to_pfn(page);
1189
1190 return 0;
1191
1192error:
1193 destroy_args(args);
1194 return ret;
1195}
1196
Anshuman Khandual399145f2020-06-04 16:47:15 -07001197static int __init debug_vm_pgtable(void)
1198{
Gavin Shan3c9b84f2021-09-02 14:52:19 -07001199 struct pgtable_debug_args args;
Anshuman Khanduala5c3b9f2020-08-06 23:19:20 -07001200 struct vm_area_struct *vma;
Anshuman Khandual399145f2020-06-04 16:47:15 -07001201 struct mm_struct *mm;
1202 pgd_t *pgdp;
1203 p4d_t *p4dp, *saved_p4dp;
1204 pud_t *pudp, *saved_pudp;
1205 pmd_t *pmdp, *saved_pmdp, pmd;
Anshuman Khandual399145f2020-06-04 16:47:15 -07001206 pgtable_t saved_ptep;
Gavin Shanc0fe07b2021-09-02 14:52:41 -07001207 unsigned long vaddr;
Kees Cookfea11202020-06-03 13:28:45 -07001208 spinlock_t *ptl = NULL;
Gavin Shan3c9b84f2021-09-02 14:52:19 -07001209 int idx, ret;
Anshuman Khandual399145f2020-06-04 16:47:15 -07001210
1211 pr_info("Validating architecture page table helpers\n");
Gavin Shan3c9b84f2021-09-02 14:52:19 -07001212 ret = init_args(&args);
1213 if (ret)
1214 return ret;
1215
Anshuman Khandual399145f2020-06-04 16:47:15 -07001216 vaddr = get_random_vaddr();
1217 mm = mm_alloc();
1218 if (!mm) {
1219 pr_err("mm_struct allocation failed\n");
1220 return 1;
1221 }
1222
Anshuman Khanduala5c3b9f2020-08-06 23:19:20 -07001223 vma = vm_area_alloc(mm);
1224 if (!vma) {
1225 pr_err("vma allocation failed\n");
1226 return 1;
1227 }
1228
Anshuman Khandual399145f2020-06-04 16:47:15 -07001229 pgdp = pgd_offset(mm, vaddr);
1230 p4dp = p4d_alloc(mm, pgdp, vaddr);
1231 pudp = pud_alloc(mm, p4dp, vaddr);
1232 pmdp = pmd_alloc(mm, pudp, vaddr);
Aneesh Kumar K.Vf14312e2020-10-15 20:05:10 -07001233 /*
1234 * Allocate pgtable_t
1235 */
1236 if (pte_alloc(mm, pmdp)) {
1237 pr_err("pgtable allocation failed\n");
1238 return 1;
1239 }
Anshuman Khandual399145f2020-06-04 16:47:15 -07001240
1241 /*
1242 * Save all the page table page addresses as the page table
1243 * entries will be used for testing with random or garbage
1244 * values. These saved addresses will be used for freeing
1245 * page table pages.
1246 */
1247 pmd = READ_ONCE(*pmdp);
1248 saved_p4dp = p4d_offset(pgdp, 0UL);
1249 saved_pudp = pud_offset(p4dp, 0UL);
1250 saved_pmdp = pmd_offset(pudp, 0UL);
1251 saved_ptep = pmd_pgtable(pmd);
1252
Anshuman Khandual2e326c02021-02-24 12:01:36 -08001253 /*
1254 * Iterate over the protection_map[] to make sure that all
1255 * the basic page table transformation validations just hold
1256 * true irrespective of the starting protection value for a
1257 * given page table entry.
1258 */
1259 for (idx = 0; idx < ARRAY_SIZE(protection_map); idx++) {
Gavin Shan36b77d12021-09-02 14:52:22 -07001260 pte_basic_tests(&args, idx);
1261 pmd_basic_tests(&args, idx);
1262 pud_basic_tests(&args, idx);
Anshuman Khandual2e326c02021-02-24 12:01:36 -08001263 }
1264
1265 /*
1266 * Both P4D and PGD level tests are very basic which do not
1267 * involve creating page table entries from the protection
1268 * value and the given pfn. Hence just keep them out from
1269 * the above iteration for now to save some test execution
1270 * time.
1271 */
Gavin Shan36b77d12021-09-02 14:52:22 -07001272 p4d_basic_tests(&args);
1273 pgd_basic_tests(&args);
Anshuman Khandual399145f2020-06-04 16:47:15 -07001274
Gavin Shan8983d232021-09-02 14:52:25 -07001275 pmd_leaf_tests(&args);
1276 pud_leaf_tests(&args);
Anshuman Khanduala5c3b9f2020-08-06 23:19:20 -07001277
Gavin Shan8983d232021-09-02 14:52:25 -07001278 pte_savedwrite_tests(&args);
1279 pmd_savedwrite_tests(&args);
Anshuman Khanduala5c3b9f2020-08-06 23:19:20 -07001280
Gavin Shan8cb183f2021-09-02 14:52:28 -07001281 pte_special_tests(&args);
1282 pte_protnone_tests(&args);
1283 pmd_protnone_tests(&args);
Anshuman Khandual05289402020-08-06 23:19:16 -07001284
Gavin Shan8cb183f2021-09-02 14:52:28 -07001285 pte_devmap_tests(&args);
1286 pmd_devmap_tests(&args);
1287 pud_devmap_tests(&args);
Anshuman Khandual05289402020-08-06 23:19:16 -07001288
Gavin Shan5f447e82021-09-02 14:52:32 -07001289 pte_soft_dirty_tests(&args);
1290 pmd_soft_dirty_tests(&args);
1291 pte_swap_soft_dirty_tests(&args);
1292 pmd_swap_soft_dirty_tests(&args);
Anshuman Khandual05289402020-08-06 23:19:16 -07001293
Gavin Shan5f447e82021-09-02 14:52:32 -07001294 pte_swap_tests(&args);
1295 pmd_swap_tests(&args);
Anshuman Khandual05289402020-08-06 23:19:16 -07001296
Gavin Shan4878a882021-09-02 14:52:35 -07001297 swap_migration_tests(&args);
Anshuman Khandual05289402020-08-06 23:19:16 -07001298
Gavin Shan4878a882021-09-02 14:52:35 -07001299 pmd_thp_tests(&args);
1300 pud_thp_tests(&args);
Anshuman Khandual05289402020-08-06 23:19:16 -07001301
Gavin Shan36b77d12021-09-02 14:52:22 -07001302 hugetlb_basic_tests(&args);
Aneesh Kumar K.Ve8edf0a2020-10-15 20:04:49 -07001303
Aneesh Kumar K.V6f302e272020-10-15 20:04:53 -07001304 /*
1305 * Page table modifying tests. They need to hold
1306 * proper page table lock.
1307 */
Aneesh Kumar K.Ve8edf0a2020-10-15 20:04:49 -07001308
Gavin Shan44966c42021-09-02 14:52:38 -07001309 args.ptep = pte_offset_map_lock(args.mm, args.pmdp, args.vaddr, &ptl);
1310 pte_clear_tests(&args);
1311 pte_advanced_tests(&args);
1312 pte_unmap_unlock(args.ptep, ptl);
Aneesh Kumar K.Ve8edf0a2020-10-15 20:04:49 -07001313
Gavin Shanc0fe07b2021-09-02 14:52:41 -07001314 ptl = pmd_lock(args.mm, args.pmdp);
1315 pmd_clear_tests(&args);
1316 pmd_advanced_tests(&args);
1317 pmd_huge_tests(&args);
1318 pmd_populate_tests(&args);
Aneesh Kumar K.V6f302e272020-10-15 20:04:53 -07001319 spin_unlock(ptl);
1320
Gavin Shan4cbde032021-09-02 14:52:45 -07001321 ptl = pud_lock(args.mm, args.pudp);
1322 pud_clear_tests(&args);
1323 pud_advanced_tests(&args);
1324 pud_huge_tests(&args);
1325 pud_populate_tests(&args);
Aneesh Kumar K.V6f302e272020-10-15 20:04:53 -07001326 spin_unlock(ptl);
1327
Aneesh Kumar K.V6f302e272020-10-15 20:04:53 -07001328 spin_lock(&mm->page_table_lock);
1329 p4d_clear_tests(mm, p4dp);
1330 pgd_clear_tests(mm, pgdp);
Aneesh Kumar K.Ve8edf0a2020-10-15 20:04:49 -07001331 p4d_populate_tests(mm, p4dp, saved_pudp);
1332 pgd_populate_tests(mm, pgdp, saved_p4dp);
Aneesh Kumar K.V6f302e272020-10-15 20:04:53 -07001333 spin_unlock(&mm->page_table_lock);
Aneesh Kumar K.Ve8edf0a2020-10-15 20:04:49 -07001334
Anshuman Khandual399145f2020-06-04 16:47:15 -07001335 p4d_free(mm, saved_p4dp);
1336 pud_free(mm, saved_pudp);
1337 pmd_free(mm, saved_pmdp);
1338 pte_free(mm, saved_ptep);
1339
Anshuman Khanduala5c3b9f2020-08-06 23:19:20 -07001340 vm_area_free(vma);
Anshuman Khandual399145f2020-06-04 16:47:15 -07001341 mm_dec_nr_puds(mm);
1342 mm_dec_nr_pmds(mm);
1343 mm_dec_nr_ptes(mm);
1344 mmdrop(mm);
Gavin Shan3c9b84f2021-09-02 14:52:19 -07001345
1346 destroy_args(&args);
Anshuman Khandual399145f2020-06-04 16:47:15 -07001347 return 0;
1348}
1349late_initcall(debug_vm_pgtable);