blob: 228e3954b90c155ed77bd49086bbbc4dc22e1941 [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>
Gavin Shan8c5b3a82021-09-02 14:52:54 -070032
33#include <asm/cacheflush.h>
Anshuman Khandual399145f2020-06-04 16:47:15 -070034#include <asm/pgalloc.h>
Anshuman Khanduala5c3b9f2020-08-06 23:19:20 -070035#include <asm/tlbflush.h>
Anshuman Khandual399145f2020-06-04 16:47:15 -070036
Anshuman Khandualb1d00002020-08-06 23:19:28 -070037/*
38 * Please refer Documentation/vm/arch_pgtable_helpers.rst for the semantics
39 * expectations that are being validated here. All future changes in here
40 * or the documentation need to be in sync.
41 */
42
Anshuman Khandual399145f2020-06-04 16:47:15 -070043#define VMFLAGS (VM_READ|VM_WRITE|VM_EXEC)
44
45/*
46 * On s390 platform, the lower 4 bits are used to identify given page table
47 * entry type. But these bits might affect the ability to clear entries with
48 * pxx_clear() because of how dynamic page table folding works on s390. So
49 * while loading up the entries do not change the lower 4 bits. It does not
Aneesh Kumar K.Vcfc5bbc2020-10-15 20:04:33 -070050 * have affect any other platform. Also avoid the 62nd bit on ppc64 that is
51 * used to mark a pte entry.
Anshuman Khandual399145f2020-06-04 16:47:15 -070052 */
Aneesh Kumar K.Vcfc5bbc2020-10-15 20:04:33 -070053#define S390_SKIP_MASK GENMASK(3, 0)
54#if __BITS_PER_LONG == 64
55#define PPC64_SKIP_MASK GENMASK(62, 62)
56#else
57#define PPC64_SKIP_MASK 0x0
58#endif
59#define ARCH_SKIP_MASK (S390_SKIP_MASK | PPC64_SKIP_MASK)
60#define RANDOM_ORVALUE (GENMASK(BITS_PER_LONG - 1, 0) & ~ARCH_SKIP_MASK)
Anshuman Khandual399145f2020-06-04 16:47:15 -070061#define RANDOM_NZVALUE GENMASK(7, 0)
62
Gavin Shan3c9b84f2021-09-02 14:52:19 -070063struct pgtable_debug_args {
64 struct mm_struct *mm;
65 struct vm_area_struct *vma;
66
67 pgd_t *pgdp;
68 p4d_t *p4dp;
69 pud_t *pudp;
70 pmd_t *pmdp;
71 pte_t *ptep;
72
73 p4d_t *start_p4dp;
74 pud_t *start_pudp;
75 pmd_t *start_pmdp;
76 pgtable_t start_ptep;
77
78 unsigned long vaddr;
79 pgprot_t page_prot;
80 pgprot_t page_prot_none;
81
82 bool is_contiguous_page;
83 unsigned long pud_pfn;
84 unsigned long pmd_pfn;
85 unsigned long pte_pfn;
86
87 unsigned long fixed_pgd_pfn;
88 unsigned long fixed_p4d_pfn;
89 unsigned long fixed_pud_pfn;
90 unsigned long fixed_pmd_pfn;
91 unsigned long fixed_pte_pfn;
92};
93
Gavin Shan36b77d12021-09-02 14:52:22 -070094static void __init pte_basic_tests(struct pgtable_debug_args *args, int idx)
Anshuman Khandual399145f2020-06-04 16:47:15 -070095{
Anshuman Khandual2e326c02021-02-24 12:01:36 -080096 pgprot_t prot = protection_map[idx];
Gavin Shan36b77d12021-09-02 14:52:22 -070097 pte_t pte = pfn_pte(args->fixed_pte_pfn, prot);
Anshuman Khandual2e326c02021-02-24 12:01:36 -080098 unsigned long val = idx, *ptr = &val;
Anshuman Khandual399145f2020-06-04 16:47:15 -070099
Anshuman Khandual2e326c02021-02-24 12:01:36 -0800100 pr_debug("Validating PTE basic (%pGv)\n", ptr);
Anshuman Khandualbb5c47c2021-02-24 12:01:32 -0800101
102 /*
103 * This test needs to be executed after the given page table entry
104 * is created with pfn_pte() to make sure that protection_map[idx]
105 * does not have the dirty bit enabled from the beginning. This is
106 * important for platforms like arm64 where (!PTE_RDONLY) indicate
107 * dirty bit being set.
108 */
109 WARN_ON(pte_dirty(pte_wrprotect(pte)));
110
Anshuman Khandual399145f2020-06-04 16:47:15 -0700111 WARN_ON(!pte_same(pte, pte));
112 WARN_ON(!pte_young(pte_mkyoung(pte_mkold(pte))));
113 WARN_ON(!pte_dirty(pte_mkdirty(pte_mkclean(pte))));
114 WARN_ON(!pte_write(pte_mkwrite(pte_wrprotect(pte))));
115 WARN_ON(pte_young(pte_mkold(pte_mkyoung(pte))));
116 WARN_ON(pte_dirty(pte_mkclean(pte_mkdirty(pte))));
117 WARN_ON(pte_write(pte_wrprotect(pte_mkwrite(pte))));
Anshuman Khandualbb5c47c2021-02-24 12:01:32 -0800118 WARN_ON(pte_dirty(pte_wrprotect(pte_mkclean(pte))));
119 WARN_ON(!pte_dirty(pte_wrprotect(pte_mkdirty(pte))));
Anshuman Khandual399145f2020-06-04 16:47:15 -0700120}
121
Gavin Shan44966c42021-09-02 14:52:38 -0700122static void __init pte_advanced_tests(struct pgtable_debug_args *args)
Anshuman Khanduala5c3b9f2020-08-06 23:19:20 -0700123{
Gavin Shan8c5b3a82021-09-02 14:52:54 -0700124 struct page *page;
Shixin Liub593b902021-06-30 18:47:40 -0700125 pte_t pte;
Anshuman Khanduala5c3b9f2020-08-06 23:19:20 -0700126
Aneesh Kumar K.Vc3824e12020-10-15 20:04:46 -0700127 /*
128 * Architectures optimize set_pte_at by avoiding TLB flush.
129 * This requires set_pte_at to be not used to update an
130 * existing pte entry. Clear pte before we do set_pte_at
Gavin Shan8c5b3a82021-09-02 14:52:54 -0700131 *
132 * flush_dcache_page() is called after set_pte_at() to clear
133 * PG_arch_1 for the page on ARM64. The page flag isn't cleared
134 * when it's released and page allocation check will fail when
135 * the page is allocated again. For architectures other than ARM64,
136 * the unexpected overhead of cache flushing is acceptable.
Aneesh Kumar K.Vc3824e12020-10-15 20:04:46 -0700137 */
Gavin Shan8c5b3a82021-09-02 14:52:54 -0700138 page = (args->pte_pfn != ULONG_MAX) ? pfn_to_page(args->pte_pfn) : NULL;
139 if (!page)
Gavin Shan44966c42021-09-02 14:52:38 -0700140 return;
Aneesh Kumar K.Vc3824e12020-10-15 20:04:46 -0700141
Anshuman Khandual6315df42020-08-06 23:19:25 -0700142 pr_debug("Validating PTE advanced\n");
Gavin Shan44966c42021-09-02 14:52:38 -0700143 pte = pfn_pte(args->pte_pfn, args->page_prot);
144 set_pte_at(args->mm, args->vaddr, args->ptep, pte);
Gavin Shan8c5b3a82021-09-02 14:52:54 -0700145 flush_dcache_page(page);
Gavin Shan44966c42021-09-02 14:52:38 -0700146 ptep_set_wrprotect(args->mm, args->vaddr, args->ptep);
147 pte = ptep_get(args->ptep);
Anshuman Khanduala5c3b9f2020-08-06 23:19:20 -0700148 WARN_ON(pte_write(pte));
Gavin Shan44966c42021-09-02 14:52:38 -0700149 ptep_get_and_clear(args->mm, args->vaddr, args->ptep);
150 pte = ptep_get(args->ptep);
Anshuman Khanduala5c3b9f2020-08-06 23:19:20 -0700151 WARN_ON(!pte_none(pte));
152
Gavin Shan44966c42021-09-02 14:52:38 -0700153 pte = pfn_pte(args->pte_pfn, args->page_prot);
Anshuman Khanduala5c3b9f2020-08-06 23:19:20 -0700154 pte = pte_wrprotect(pte);
155 pte = pte_mkclean(pte);
Gavin Shan44966c42021-09-02 14:52:38 -0700156 set_pte_at(args->mm, args->vaddr, args->ptep, pte);
Gavin Shan8c5b3a82021-09-02 14:52:54 -0700157 flush_dcache_page(page);
Anshuman Khanduala5c3b9f2020-08-06 23:19:20 -0700158 pte = pte_mkwrite(pte);
159 pte = pte_mkdirty(pte);
Gavin Shan44966c42021-09-02 14:52:38 -0700160 ptep_set_access_flags(args->vma, args->vaddr, args->ptep, pte, 1);
161 pte = ptep_get(args->ptep);
Anshuman Khanduala5c3b9f2020-08-06 23:19:20 -0700162 WARN_ON(!(pte_write(pte) && pte_dirty(pte)));
Gavin Shan44966c42021-09-02 14:52:38 -0700163 ptep_get_and_clear_full(args->mm, args->vaddr, args->ptep, 1);
164 pte = ptep_get(args->ptep);
Anshuman Khanduala5c3b9f2020-08-06 23:19:20 -0700165 WARN_ON(!pte_none(pte));
166
Gavin Shan44966c42021-09-02 14:52:38 -0700167 pte = pfn_pte(args->pte_pfn, args->page_prot);
Anshuman Khanduala5c3b9f2020-08-06 23:19:20 -0700168 pte = pte_mkyoung(pte);
Gavin Shan44966c42021-09-02 14:52:38 -0700169 set_pte_at(args->mm, args->vaddr, args->ptep, pte);
Gavin Shan8c5b3a82021-09-02 14:52:54 -0700170 flush_dcache_page(page);
Gavin Shan44966c42021-09-02 14:52:38 -0700171 ptep_test_and_clear_young(args->vma, args->vaddr, args->ptep);
172 pte = ptep_get(args->ptep);
Anshuman Khanduala5c3b9f2020-08-06 23:19:20 -0700173 WARN_ON(pte_young(pte));
174}
175
Gavin Shan8983d232021-09-02 14:52:25 -0700176static void __init pte_savedwrite_tests(struct pgtable_debug_args *args)
Anshuman Khanduala5c3b9f2020-08-06 23:19:20 -0700177{
Gavin Shan8983d232021-09-02 14:52:25 -0700178 pte_t pte = pfn_pte(args->fixed_pte_pfn, args->page_prot_none);
Anshuman Khanduala5c3b9f2020-08-06 23:19:20 -0700179
Aneesh Kumar K.V42006052020-10-15 20:04:40 -0700180 if (!IS_ENABLED(CONFIG_NUMA_BALANCING))
181 return;
182
Anshuman Khandual6315df42020-08-06 23:19:25 -0700183 pr_debug("Validating PTE saved write\n");
Anshuman Khanduala5c3b9f2020-08-06 23:19:20 -0700184 WARN_ON(!pte_savedwrite(pte_mk_savedwrite(pte_clear_savedwrite(pte))));
185 WARN_ON(pte_savedwrite(pte_clear_savedwrite(pte_mk_savedwrite(pte))));
186}
Aneesh Kumar K.V42006052020-10-15 20:04:40 -0700187
Anshuman Khandual399145f2020-06-04 16:47:15 -0700188#ifdef CONFIG_TRANSPARENT_HUGEPAGE
Gavin Shan36b77d12021-09-02 14:52:22 -0700189static void __init pmd_basic_tests(struct pgtable_debug_args *args, int idx)
Anshuman Khandual399145f2020-06-04 16:47:15 -0700190{
Anshuman Khandual2e326c02021-02-24 12:01:36 -0800191 pgprot_t prot = protection_map[idx];
Anshuman Khandual2e326c02021-02-24 12:01:36 -0800192 unsigned long val = idx, *ptr = &val;
Anshuman Khandual65ac1a62021-06-28 19:35:10 -0700193 pmd_t pmd;
Anshuman Khandual399145f2020-06-04 16:47:15 -0700194
Aneesh Kumar K.V787d5632020-06-10 18:41:44 -0700195 if (!has_transparent_hugepage())
196 return;
197
Anshuman Khandual2e326c02021-02-24 12:01:36 -0800198 pr_debug("Validating PMD basic (%pGv)\n", ptr);
Gavin Shan36b77d12021-09-02 14:52:22 -0700199 pmd = pfn_pmd(args->fixed_pmd_pfn, prot);
Anshuman Khandualbb5c47c2021-02-24 12:01:32 -0800200
201 /*
202 * This test needs to be executed after the given page table entry
203 * is created with pfn_pmd() to make sure that protection_map[idx]
204 * does not have the dirty bit enabled from the beginning. This is
205 * important for platforms like arm64 where (!PTE_RDONLY) indicate
206 * dirty bit being set.
207 */
208 WARN_ON(pmd_dirty(pmd_wrprotect(pmd)));
209
210
Anshuman Khandual399145f2020-06-04 16:47:15 -0700211 WARN_ON(!pmd_same(pmd, pmd));
212 WARN_ON(!pmd_young(pmd_mkyoung(pmd_mkold(pmd))));
213 WARN_ON(!pmd_dirty(pmd_mkdirty(pmd_mkclean(pmd))));
214 WARN_ON(!pmd_write(pmd_mkwrite(pmd_wrprotect(pmd))));
215 WARN_ON(pmd_young(pmd_mkold(pmd_mkyoung(pmd))));
216 WARN_ON(pmd_dirty(pmd_mkclean(pmd_mkdirty(pmd))));
217 WARN_ON(pmd_write(pmd_wrprotect(pmd_mkwrite(pmd))));
Anshuman Khandualbb5c47c2021-02-24 12:01:32 -0800218 WARN_ON(pmd_dirty(pmd_wrprotect(pmd_mkclean(pmd))));
219 WARN_ON(!pmd_dirty(pmd_wrprotect(pmd_mkdirty(pmd))));
Anshuman Khandual399145f2020-06-04 16:47:15 -0700220 /*
221 * A huge page does not point to next level page table
222 * entry. Hence this must qualify as pmd_bad().
223 */
224 WARN_ON(!pmd_bad(pmd_mkhuge(pmd)));
225}
226
Gavin Shanc0fe07b2021-09-02 14:52:41 -0700227static void __init pmd_advanced_tests(struct pgtable_debug_args *args)
Anshuman Khanduala5c3b9f2020-08-06 23:19:20 -0700228{
Gavin Shan8c5b3a82021-09-02 14:52:54 -0700229 struct page *page;
Anshuman Khandual65ac1a62021-06-28 19:35:10 -0700230 pmd_t pmd;
Gavin Shanc0fe07b2021-09-02 14:52:41 -0700231 unsigned long vaddr = args->vaddr;
Anshuman Khanduala5c3b9f2020-08-06 23:19:20 -0700232
233 if (!has_transparent_hugepage())
234 return;
235
Gavin Shan8c5b3a82021-09-02 14:52:54 -0700236 page = (args->pmd_pfn != ULONG_MAX) ? pfn_to_page(args->pmd_pfn) : NULL;
237 if (!page)
Gavin Shanc0fe07b2021-09-02 14:52:41 -0700238 return;
239
Gavin Shan8c5b3a82021-09-02 14:52:54 -0700240 /*
241 * flush_dcache_page() is called after set_pmd_at() to clear
242 * PG_arch_1 for the page on ARM64. The page flag isn't cleared
243 * when it's released and page allocation check will fail when
244 * the page is allocated again. For architectures other than ARM64,
245 * the unexpected overhead of cache flushing is acceptable.
246 */
Anshuman Khandual6315df42020-08-06 23:19:25 -0700247 pr_debug("Validating PMD advanced\n");
Anshuman Khanduala5c3b9f2020-08-06 23:19:20 -0700248 /* Align the address wrt HPAGE_PMD_SIZE */
Gerald Schaefer04f7ce32021-06-04 20:01:18 -0700249 vaddr &= HPAGE_PMD_MASK;
Anshuman Khanduala5c3b9f2020-08-06 23:19:20 -0700250
Gavin Shanc0fe07b2021-09-02 14:52:41 -0700251 pgtable_trans_huge_deposit(args->mm, args->pmdp, args->start_ptep);
Aneesh Kumar K.V87f34982020-10-15 20:04:56 -0700252
Gavin Shanc0fe07b2021-09-02 14:52:41 -0700253 pmd = pfn_pmd(args->pmd_pfn, args->page_prot);
254 set_pmd_at(args->mm, vaddr, args->pmdp, pmd);
Gavin Shan8c5b3a82021-09-02 14:52:54 -0700255 flush_dcache_page(page);
Gavin Shanc0fe07b2021-09-02 14:52:41 -0700256 pmdp_set_wrprotect(args->mm, vaddr, args->pmdp);
257 pmd = READ_ONCE(*args->pmdp);
Anshuman Khanduala5c3b9f2020-08-06 23:19:20 -0700258 WARN_ON(pmd_write(pmd));
Gavin Shanc0fe07b2021-09-02 14:52:41 -0700259 pmdp_huge_get_and_clear(args->mm, vaddr, args->pmdp);
260 pmd = READ_ONCE(*args->pmdp);
Anshuman Khanduala5c3b9f2020-08-06 23:19:20 -0700261 WARN_ON(!pmd_none(pmd));
262
Gavin Shanc0fe07b2021-09-02 14:52:41 -0700263 pmd = pfn_pmd(args->pmd_pfn, args->page_prot);
Anshuman Khanduala5c3b9f2020-08-06 23:19:20 -0700264 pmd = pmd_wrprotect(pmd);
265 pmd = pmd_mkclean(pmd);
Gavin Shanc0fe07b2021-09-02 14:52:41 -0700266 set_pmd_at(args->mm, vaddr, args->pmdp, pmd);
Gavin Shan8c5b3a82021-09-02 14:52:54 -0700267 flush_dcache_page(page);
Anshuman Khanduala5c3b9f2020-08-06 23:19:20 -0700268 pmd = pmd_mkwrite(pmd);
269 pmd = pmd_mkdirty(pmd);
Gavin Shanc0fe07b2021-09-02 14:52:41 -0700270 pmdp_set_access_flags(args->vma, vaddr, args->pmdp, pmd, 1);
271 pmd = READ_ONCE(*args->pmdp);
Anshuman Khanduala5c3b9f2020-08-06 23:19:20 -0700272 WARN_ON(!(pmd_write(pmd) && pmd_dirty(pmd)));
Gavin Shanc0fe07b2021-09-02 14:52:41 -0700273 pmdp_huge_get_and_clear_full(args->vma, vaddr, args->pmdp, 1);
274 pmd = READ_ONCE(*args->pmdp);
Anshuman Khanduala5c3b9f2020-08-06 23:19:20 -0700275 WARN_ON(!pmd_none(pmd));
276
Gavin Shanc0fe07b2021-09-02 14:52:41 -0700277 pmd = pmd_mkhuge(pfn_pmd(args->pmd_pfn, args->page_prot));
Anshuman Khanduala5c3b9f2020-08-06 23:19:20 -0700278 pmd = pmd_mkyoung(pmd);
Gavin Shanc0fe07b2021-09-02 14:52:41 -0700279 set_pmd_at(args->mm, vaddr, args->pmdp, pmd);
Gavin Shan8c5b3a82021-09-02 14:52:54 -0700280 flush_dcache_page(page);
Gavin Shanc0fe07b2021-09-02 14:52:41 -0700281 pmdp_test_and_clear_young(args->vma, vaddr, args->pmdp);
282 pmd = READ_ONCE(*args->pmdp);
Anshuman Khanduala5c3b9f2020-08-06 23:19:20 -0700283 WARN_ON(pmd_young(pmd));
Aneesh Kumar K.V87f34982020-10-15 20:04:56 -0700284
Aneesh Kumar K.V13af0502020-10-15 20:04:59 -0700285 /* Clear the pte entries */
Gavin Shanc0fe07b2021-09-02 14:52:41 -0700286 pmdp_huge_get_and_clear(args->mm, vaddr, args->pmdp);
287 pgtable_trans_huge_withdraw(args->mm, args->pmdp);
Anshuman Khanduala5c3b9f2020-08-06 23:19:20 -0700288}
289
Gavin Shan8983d232021-09-02 14:52:25 -0700290static void __init pmd_leaf_tests(struct pgtable_debug_args *args)
Anshuman Khanduala5c3b9f2020-08-06 23:19:20 -0700291{
Anshuman Khandual65ac1a62021-06-28 19:35:10 -0700292 pmd_t pmd;
293
294 if (!has_transparent_hugepage())
295 return;
Anshuman Khanduala5c3b9f2020-08-06 23:19:20 -0700296
Anshuman Khandual6315df42020-08-06 23:19:25 -0700297 pr_debug("Validating PMD leaf\n");
Gavin Shan8983d232021-09-02 14:52:25 -0700298 pmd = pfn_pmd(args->fixed_pmd_pfn, args->page_prot);
Anshuman Khandual65ac1a62021-06-28 19:35:10 -0700299
Anshuman Khanduala5c3b9f2020-08-06 23:19:20 -0700300 /*
301 * PMD based THP is a leaf entry.
302 */
303 pmd = pmd_mkhuge(pmd);
304 WARN_ON(!pmd_leaf(pmd));
305}
306
Gavin Shan8983d232021-09-02 14:52:25 -0700307static void __init pmd_savedwrite_tests(struct pgtable_debug_args *args)
Anshuman Khanduala5c3b9f2020-08-06 23:19:20 -0700308{
Anshuman Khandual65ac1a62021-06-28 19:35:10 -0700309 pmd_t pmd;
Anshuman Khanduala5c3b9f2020-08-06 23:19:20 -0700310
Aneesh Kumar K.V42006052020-10-15 20:04:40 -0700311 if (!IS_ENABLED(CONFIG_NUMA_BALANCING))
312 return;
313
Anshuman Khandual65ac1a62021-06-28 19:35:10 -0700314 if (!has_transparent_hugepage())
315 return;
316
Anshuman Khandual6315df42020-08-06 23:19:25 -0700317 pr_debug("Validating PMD saved write\n");
Gavin Shan8983d232021-09-02 14:52:25 -0700318 pmd = pfn_pmd(args->fixed_pmd_pfn, args->page_prot_none);
Anshuman Khanduala5c3b9f2020-08-06 23:19:20 -0700319 WARN_ON(!pmd_savedwrite(pmd_mk_savedwrite(pmd_clear_savedwrite(pmd))));
320 WARN_ON(pmd_savedwrite(pmd_clear_savedwrite(pmd_mk_savedwrite(pmd))));
321}
322
Anshuman Khandual399145f2020-06-04 16:47:15 -0700323#ifdef CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD
Gavin Shan36b77d12021-09-02 14:52:22 -0700324static void __init pud_basic_tests(struct pgtable_debug_args *args, int idx)
Anshuman Khandual399145f2020-06-04 16:47:15 -0700325{
Anshuman Khandual2e326c02021-02-24 12:01:36 -0800326 pgprot_t prot = protection_map[idx];
Anshuman Khandual2e326c02021-02-24 12:01:36 -0800327 unsigned long val = idx, *ptr = &val;
Anshuman Khandual65ac1a62021-06-28 19:35:10 -0700328 pud_t pud;
Anshuman Khandual399145f2020-06-04 16:47:15 -0700329
Aneesh Kumar K.V787d5632020-06-10 18:41:44 -0700330 if (!has_transparent_hugepage())
331 return;
332
Anshuman Khandual2e326c02021-02-24 12:01:36 -0800333 pr_debug("Validating PUD basic (%pGv)\n", ptr);
Gavin Shan36b77d12021-09-02 14:52:22 -0700334 pud = pfn_pud(args->fixed_pud_pfn, prot);
Anshuman Khandualbb5c47c2021-02-24 12:01:32 -0800335
336 /*
337 * This test needs to be executed after the given page table entry
338 * is created with pfn_pud() to make sure that protection_map[idx]
339 * does not have the dirty bit enabled from the beginning. This is
340 * important for platforms like arm64 where (!PTE_RDONLY) indicate
341 * dirty bit being set.
342 */
343 WARN_ON(pud_dirty(pud_wrprotect(pud)));
344
Anshuman Khandual399145f2020-06-04 16:47:15 -0700345 WARN_ON(!pud_same(pud, pud));
346 WARN_ON(!pud_young(pud_mkyoung(pud_mkold(pud))));
Anshuman Khandualbb5c47c2021-02-24 12:01:32 -0800347 WARN_ON(!pud_dirty(pud_mkdirty(pud_mkclean(pud))));
348 WARN_ON(pud_dirty(pud_mkclean(pud_mkdirty(pud))));
Anshuman Khandual399145f2020-06-04 16:47:15 -0700349 WARN_ON(!pud_write(pud_mkwrite(pud_wrprotect(pud))));
350 WARN_ON(pud_write(pud_wrprotect(pud_mkwrite(pud))));
351 WARN_ON(pud_young(pud_mkold(pud_mkyoung(pud))));
Anshuman Khandualbb5c47c2021-02-24 12:01:32 -0800352 WARN_ON(pud_dirty(pud_wrprotect(pud_mkclean(pud))));
353 WARN_ON(!pud_dirty(pud_wrprotect(pud_mkdirty(pud))));
Anshuman Khandual399145f2020-06-04 16:47:15 -0700354
Gavin Shan36b77d12021-09-02 14:52:22 -0700355 if (mm_pmd_folded(args->mm))
Anshuman Khandual399145f2020-06-04 16:47:15 -0700356 return;
357
358 /*
359 * A huge page does not point to next level page table
360 * entry. Hence this must qualify as pud_bad().
361 */
362 WARN_ON(!pud_bad(pud_mkhuge(pud)));
363}
Anshuman Khanduala5c3b9f2020-08-06 23:19:20 -0700364
Gavin Shan4cbde032021-09-02 14:52:45 -0700365static void __init pud_advanced_tests(struct pgtable_debug_args *args)
Anshuman Khanduala5c3b9f2020-08-06 23:19:20 -0700366{
Gavin Shan8c5b3a82021-09-02 14:52:54 -0700367 struct page *page;
Gavin Shan4cbde032021-09-02 14:52:45 -0700368 unsigned long vaddr = args->vaddr;
Anshuman Khandual65ac1a62021-06-28 19:35:10 -0700369 pud_t pud;
Anshuman Khanduala5c3b9f2020-08-06 23:19:20 -0700370
371 if (!has_transparent_hugepage())
372 return;
373
Gavin Shan8c5b3a82021-09-02 14:52:54 -0700374 page = (args->pud_pfn != ULONG_MAX) ? pfn_to_page(args->pud_pfn) : NULL;
375 if (!page)
Gavin Shan4cbde032021-09-02 14:52:45 -0700376 return;
377
Gavin Shan8c5b3a82021-09-02 14:52:54 -0700378 /*
379 * flush_dcache_page() is called after set_pud_at() to clear
380 * PG_arch_1 for the page on ARM64. The page flag isn't cleared
381 * when it's released and page allocation check will fail when
382 * the page is allocated again. For architectures other than ARM64,
383 * the unexpected overhead of cache flushing is acceptable.
384 */
Anshuman Khandual6315df42020-08-06 23:19:25 -0700385 pr_debug("Validating PUD advanced\n");
Anshuman Khanduala5c3b9f2020-08-06 23:19:20 -0700386 /* Align the address wrt HPAGE_PUD_SIZE */
Gerald Schaefer04f7ce32021-06-04 20:01:18 -0700387 vaddr &= HPAGE_PUD_MASK;
Anshuman Khanduala5c3b9f2020-08-06 23:19:20 -0700388
Gavin Shan4cbde032021-09-02 14:52:45 -0700389 pud = pfn_pud(args->pud_pfn, args->page_prot);
390 set_pud_at(args->mm, vaddr, args->pudp, pud);
Gavin Shan8c5b3a82021-09-02 14:52:54 -0700391 flush_dcache_page(page);
Gavin Shan4cbde032021-09-02 14:52:45 -0700392 pudp_set_wrprotect(args->mm, vaddr, args->pudp);
393 pud = READ_ONCE(*args->pudp);
Anshuman Khanduala5c3b9f2020-08-06 23:19:20 -0700394 WARN_ON(pud_write(pud));
395
396#ifndef __PAGETABLE_PMD_FOLDED
Gavin Shan4cbde032021-09-02 14:52:45 -0700397 pudp_huge_get_and_clear(args->mm, vaddr, args->pudp);
398 pud = READ_ONCE(*args->pudp);
Anshuman Khanduala5c3b9f2020-08-06 23:19:20 -0700399 WARN_ON(!pud_none(pud));
Anshuman Khanduala5c3b9f2020-08-06 23:19:20 -0700400#endif /* __PAGETABLE_PMD_FOLDED */
Gavin Shan4cbde032021-09-02 14:52:45 -0700401 pud = pfn_pud(args->pud_pfn, args->page_prot);
Anshuman Khanduala5c3b9f2020-08-06 23:19:20 -0700402 pud = pud_wrprotect(pud);
403 pud = pud_mkclean(pud);
Gavin Shan4cbde032021-09-02 14:52:45 -0700404 set_pud_at(args->mm, vaddr, args->pudp, pud);
Gavin Shan8c5b3a82021-09-02 14:52:54 -0700405 flush_dcache_page(page);
Anshuman Khanduala5c3b9f2020-08-06 23:19:20 -0700406 pud = pud_mkwrite(pud);
407 pud = pud_mkdirty(pud);
Gavin Shan4cbde032021-09-02 14:52:45 -0700408 pudp_set_access_flags(args->vma, vaddr, args->pudp, pud, 1);
409 pud = READ_ONCE(*args->pudp);
Anshuman Khanduala5c3b9f2020-08-06 23:19:20 -0700410 WARN_ON(!(pud_write(pud) && pud_dirty(pud)));
411
Aneesh Kumar K.Vc3824e12020-10-15 20:04:46 -0700412#ifndef __PAGETABLE_PMD_FOLDED
Gavin Shan4cbde032021-09-02 14:52:45 -0700413 pudp_huge_get_and_clear_full(args->mm, vaddr, args->pudp, 1);
414 pud = READ_ONCE(*args->pudp);
Aneesh Kumar K.Vc3824e12020-10-15 20:04:46 -0700415 WARN_ON(!pud_none(pud));
416#endif /* __PAGETABLE_PMD_FOLDED */
417
Gavin Shan4cbde032021-09-02 14:52:45 -0700418 pud = pfn_pud(args->pud_pfn, args->page_prot);
Anshuman Khanduala5c3b9f2020-08-06 23:19:20 -0700419 pud = pud_mkyoung(pud);
Gavin Shan4cbde032021-09-02 14:52:45 -0700420 set_pud_at(args->mm, vaddr, args->pudp, pud);
Gavin Shan8c5b3a82021-09-02 14:52:54 -0700421 flush_dcache_page(page);
Gavin Shan4cbde032021-09-02 14:52:45 -0700422 pudp_test_and_clear_young(args->vma, vaddr, args->pudp);
423 pud = READ_ONCE(*args->pudp);
Anshuman Khanduala5c3b9f2020-08-06 23:19:20 -0700424 WARN_ON(pud_young(pud));
Aneesh Kumar K.V13af0502020-10-15 20:04:59 -0700425
Gavin Shan4cbde032021-09-02 14:52:45 -0700426 pudp_huge_get_and_clear(args->mm, vaddr, args->pudp);
Anshuman Khanduala5c3b9f2020-08-06 23:19:20 -0700427}
428
Gavin Shan8983d232021-09-02 14:52:25 -0700429static void __init pud_leaf_tests(struct pgtable_debug_args *args)
Anshuman Khanduala5c3b9f2020-08-06 23:19:20 -0700430{
Anshuman Khandual65ac1a62021-06-28 19:35:10 -0700431 pud_t pud;
432
433 if (!has_transparent_hugepage())
434 return;
Anshuman Khanduala5c3b9f2020-08-06 23:19:20 -0700435
Anshuman Khandual6315df42020-08-06 23:19:25 -0700436 pr_debug("Validating PUD leaf\n");
Gavin Shan8983d232021-09-02 14:52:25 -0700437 pud = pfn_pud(args->fixed_pud_pfn, args->page_prot);
Anshuman Khanduala5c3b9f2020-08-06 23:19:20 -0700438 /*
439 * PUD based THP is a leaf entry.
440 */
441 pud = pud_mkhuge(pud);
442 WARN_ON(!pud_leaf(pud));
443}
Shixin Liu5fe77be2021-06-30 18:47:37 -0700444#else /* !CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD */
Gavin Shan36b77d12021-09-02 14:52:22 -0700445static void __init pud_basic_tests(struct pgtable_debug_args *args, int idx) { }
Gavin Shan4cbde032021-09-02 14:52:45 -0700446static void __init pud_advanced_tests(struct pgtable_debug_args *args) { }
Gavin Shan8983d232021-09-02 14:52:25 -0700447static void __init pud_leaf_tests(struct pgtable_debug_args *args) { }
Shixin Liu5fe77be2021-06-30 18:47:37 -0700448#endif /* CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD */
449#else /* !CONFIG_TRANSPARENT_HUGEPAGE */
Gavin Shan36b77d12021-09-02 14:52:22 -0700450static void __init pmd_basic_tests(struct pgtable_debug_args *args, int idx) { }
451static void __init pud_basic_tests(struct pgtable_debug_args *args, int idx) { }
Gavin Shanc0fe07b2021-09-02 14:52:41 -0700452static void __init pmd_advanced_tests(struct pgtable_debug_args *args) { }
Gavin Shan4cbde032021-09-02 14:52:45 -0700453static void __init pud_advanced_tests(struct pgtable_debug_args *args) { }
Gavin Shan8983d232021-09-02 14:52:25 -0700454static void __init pmd_leaf_tests(struct pgtable_debug_args *args) { }
455static void __init pud_leaf_tests(struct pgtable_debug_args *args) { }
456static void __init pmd_savedwrite_tests(struct pgtable_debug_args *args) { }
Shixin Liu5fe77be2021-06-30 18:47:37 -0700457#endif /* CONFIG_TRANSPARENT_HUGEPAGE */
Anshuman Khanduala5c3b9f2020-08-06 23:19:20 -0700458
Aneesh Kumar K.V85a14462020-10-15 20:04:36 -0700459#ifdef CONFIG_HAVE_ARCH_HUGE_VMAP
Gavin Shanc0fe07b2021-09-02 14:52:41 -0700460static void __init pmd_huge_tests(struct pgtable_debug_args *args)
Shixin Liu5fe77be2021-06-30 18:47:37 -0700461{
462 pmd_t pmd;
463
Gavin Shanc0fe07b2021-09-02 14:52:41 -0700464 if (!arch_vmap_pmd_supported(args->page_prot))
Shixin Liu5fe77be2021-06-30 18:47:37 -0700465 return;
466
467 pr_debug("Validating PMD huge\n");
468 /*
469 * X86 defined pmd_set_huge() verifies that the given
470 * PMD is not a populated non-leaf entry.
471 */
Gavin Shanc0fe07b2021-09-02 14:52:41 -0700472 WRITE_ONCE(*args->pmdp, __pmd(0));
473 WARN_ON(!pmd_set_huge(args->pmdp, __pfn_to_phys(args->fixed_pmd_pfn), args->page_prot));
474 WARN_ON(!pmd_clear_huge(args->pmdp));
475 pmd = READ_ONCE(*args->pmdp);
Shixin Liu5fe77be2021-06-30 18:47:37 -0700476 WARN_ON(!pmd_none(pmd));
477}
478
Gavin Shan4cbde032021-09-02 14:52:45 -0700479static void __init pud_huge_tests(struct pgtable_debug_args *args)
Anshuman Khanduala5c3b9f2020-08-06 23:19:20 -0700480{
481 pud_t pud;
482
Gavin Shan4cbde032021-09-02 14:52:45 -0700483 if (!arch_vmap_pud_supported(args->page_prot))
Anshuman Khanduala5c3b9f2020-08-06 23:19:20 -0700484 return;
Anshuman Khandual6315df42020-08-06 23:19:25 -0700485
486 pr_debug("Validating PUD huge\n");
Anshuman Khanduala5c3b9f2020-08-06 23:19:20 -0700487 /*
488 * X86 defined pud_set_huge() verifies that the given
489 * PUD is not a populated non-leaf entry.
490 */
Gavin Shan4cbde032021-09-02 14:52:45 -0700491 WRITE_ONCE(*args->pudp, __pud(0));
492 WARN_ON(!pud_set_huge(args->pudp, __pfn_to_phys(args->fixed_pud_pfn), args->page_prot));
493 WARN_ON(!pud_clear_huge(args->pudp));
494 pud = READ_ONCE(*args->pudp);
Anshuman Khanduala5c3b9f2020-08-06 23:19:20 -0700495 WARN_ON(!pud_none(pud));
496}
Aneesh Kumar K.V85a14462020-10-15 20:04:36 -0700497#else /* !CONFIG_HAVE_ARCH_HUGE_VMAP */
Gavin Shanc0fe07b2021-09-02 14:52:41 -0700498static void __init pmd_huge_tests(struct pgtable_debug_args *args) { }
Gavin Shan4cbde032021-09-02 14:52:45 -0700499static void __init pud_huge_tests(struct pgtable_debug_args *args) { }
Shixin Liu5fe77be2021-06-30 18:47:37 -0700500#endif /* CONFIG_HAVE_ARCH_HUGE_VMAP */
Anshuman Khandual399145f2020-06-04 16:47:15 -0700501
Gavin Shan36b77d12021-09-02 14:52:22 -0700502static void __init p4d_basic_tests(struct pgtable_debug_args *args)
Anshuman Khandual399145f2020-06-04 16:47:15 -0700503{
504 p4d_t p4d;
505
Anshuman Khandual6315df42020-08-06 23:19:25 -0700506 pr_debug("Validating P4D basic\n");
Anshuman Khandual399145f2020-06-04 16:47:15 -0700507 memset(&p4d, RANDOM_NZVALUE, sizeof(p4d_t));
508 WARN_ON(!p4d_same(p4d, p4d));
509}
510
Gavin Shan36b77d12021-09-02 14:52:22 -0700511static void __init pgd_basic_tests(struct pgtable_debug_args *args)
Anshuman Khandual399145f2020-06-04 16:47:15 -0700512{
513 pgd_t pgd;
514
Anshuman Khandual6315df42020-08-06 23:19:25 -0700515 pr_debug("Validating PGD basic\n");
Anshuman Khandual399145f2020-06-04 16:47:15 -0700516 memset(&pgd, RANDOM_NZVALUE, sizeof(pgd_t));
517 WARN_ON(!pgd_same(pgd, pgd));
518}
519
520#ifndef __PAGETABLE_PUD_FOLDED
Gavin Shan4cbde032021-09-02 14:52:45 -0700521static void __init pud_clear_tests(struct pgtable_debug_args *args)
Anshuman Khandual399145f2020-06-04 16:47:15 -0700522{
Gavin Shan4cbde032021-09-02 14:52:45 -0700523 pud_t pud = READ_ONCE(*args->pudp);
Anshuman Khandual399145f2020-06-04 16:47:15 -0700524
Gavin Shan4cbde032021-09-02 14:52:45 -0700525 if (mm_pmd_folded(args->mm))
Anshuman Khandual399145f2020-06-04 16:47:15 -0700526 return;
527
Anshuman Khandual6315df42020-08-06 23:19:25 -0700528 pr_debug("Validating PUD clear\n");
Anshuman Khandual399145f2020-06-04 16:47:15 -0700529 pud = __pud(pud_val(pud) | RANDOM_ORVALUE);
Gavin Shan4cbde032021-09-02 14:52:45 -0700530 WRITE_ONCE(*args->pudp, pud);
531 pud_clear(args->pudp);
532 pud = READ_ONCE(*args->pudp);
Anshuman Khandual399145f2020-06-04 16:47:15 -0700533 WARN_ON(!pud_none(pud));
534}
535
Gavin Shan4cbde032021-09-02 14:52:45 -0700536static void __init pud_populate_tests(struct pgtable_debug_args *args)
Anshuman Khandual399145f2020-06-04 16:47:15 -0700537{
538 pud_t pud;
539
Gavin Shan4cbde032021-09-02 14:52:45 -0700540 if (mm_pmd_folded(args->mm))
Anshuman Khandual399145f2020-06-04 16:47:15 -0700541 return;
Anshuman Khandual6315df42020-08-06 23:19:25 -0700542
543 pr_debug("Validating PUD populate\n");
Anshuman Khandual399145f2020-06-04 16:47:15 -0700544 /*
545 * This entry points to next level page table page.
546 * Hence this must not qualify as pud_bad().
547 */
Gavin Shan4cbde032021-09-02 14:52:45 -0700548 pud_populate(args->mm, args->pudp, args->start_pmdp);
549 pud = READ_ONCE(*args->pudp);
Anshuman Khandual399145f2020-06-04 16:47:15 -0700550 WARN_ON(pud_bad(pud));
551}
552#else /* !__PAGETABLE_PUD_FOLDED */
Gavin Shan4cbde032021-09-02 14:52:45 -0700553static void __init pud_clear_tests(struct pgtable_debug_args *args) { }
554static void __init pud_populate_tests(struct pgtable_debug_args *args) { }
Anshuman Khandual399145f2020-06-04 16:47:15 -0700555#endif /* PAGETABLE_PUD_FOLDED */
556
557#ifndef __PAGETABLE_P4D_FOLDED
Gavin Shan2f87f8c2021-09-02 14:52:48 -0700558static void __init p4d_clear_tests(struct pgtable_debug_args *args)
Anshuman Khandual399145f2020-06-04 16:47:15 -0700559{
Gavin Shan2f87f8c2021-09-02 14:52:48 -0700560 p4d_t p4d = READ_ONCE(*args->p4dp);
Anshuman Khandual399145f2020-06-04 16:47:15 -0700561
Gavin Shan2f87f8c2021-09-02 14:52:48 -0700562 if (mm_pud_folded(args->mm))
Anshuman Khandual399145f2020-06-04 16:47:15 -0700563 return;
564
Anshuman Khandual6315df42020-08-06 23:19:25 -0700565 pr_debug("Validating P4D clear\n");
Anshuman Khandual399145f2020-06-04 16:47:15 -0700566 p4d = __p4d(p4d_val(p4d) | RANDOM_ORVALUE);
Gavin Shan2f87f8c2021-09-02 14:52:48 -0700567 WRITE_ONCE(*args->p4dp, p4d);
568 p4d_clear(args->p4dp);
569 p4d = READ_ONCE(*args->p4dp);
Anshuman Khandual399145f2020-06-04 16:47:15 -0700570 WARN_ON(!p4d_none(p4d));
571}
572
Gavin Shan2f87f8c2021-09-02 14:52:48 -0700573static void __init p4d_populate_tests(struct pgtable_debug_args *args)
Anshuman Khandual399145f2020-06-04 16:47:15 -0700574{
575 p4d_t p4d;
576
Gavin Shan2f87f8c2021-09-02 14:52:48 -0700577 if (mm_pud_folded(args->mm))
Anshuman Khandual399145f2020-06-04 16:47:15 -0700578 return;
579
Anshuman Khandual6315df42020-08-06 23:19:25 -0700580 pr_debug("Validating P4D populate\n");
Anshuman Khandual399145f2020-06-04 16:47:15 -0700581 /*
582 * This entry points to next level page table page.
583 * Hence this must not qualify as p4d_bad().
584 */
Gavin Shan2f87f8c2021-09-02 14:52:48 -0700585 pud_clear(args->pudp);
586 p4d_clear(args->p4dp);
587 p4d_populate(args->mm, args->p4dp, args->start_pudp);
588 p4d = READ_ONCE(*args->p4dp);
Anshuman Khandual399145f2020-06-04 16:47:15 -0700589 WARN_ON(p4d_bad(p4d));
590}
591
Gavin Shan2f87f8c2021-09-02 14:52:48 -0700592static void __init pgd_clear_tests(struct pgtable_debug_args *args)
Anshuman Khandual399145f2020-06-04 16:47:15 -0700593{
Gavin Shan2f87f8c2021-09-02 14:52:48 -0700594 pgd_t pgd = READ_ONCE(*(args->pgdp));
Anshuman Khandual399145f2020-06-04 16:47:15 -0700595
Gavin Shan2f87f8c2021-09-02 14:52:48 -0700596 if (mm_p4d_folded(args->mm))
Anshuman Khandual399145f2020-06-04 16:47:15 -0700597 return;
598
Anshuman Khandual6315df42020-08-06 23:19:25 -0700599 pr_debug("Validating PGD clear\n");
Anshuman Khandual399145f2020-06-04 16:47:15 -0700600 pgd = __pgd(pgd_val(pgd) | RANDOM_ORVALUE);
Gavin Shan2f87f8c2021-09-02 14:52:48 -0700601 WRITE_ONCE(*args->pgdp, pgd);
602 pgd_clear(args->pgdp);
603 pgd = READ_ONCE(*args->pgdp);
Anshuman Khandual399145f2020-06-04 16:47:15 -0700604 WARN_ON(!pgd_none(pgd));
605}
606
Gavin Shan2f87f8c2021-09-02 14:52:48 -0700607static void __init pgd_populate_tests(struct pgtable_debug_args *args)
Anshuman Khandual399145f2020-06-04 16:47:15 -0700608{
609 pgd_t pgd;
610
Gavin Shan2f87f8c2021-09-02 14:52:48 -0700611 if (mm_p4d_folded(args->mm))
Anshuman Khandual399145f2020-06-04 16:47:15 -0700612 return;
613
Anshuman Khandual6315df42020-08-06 23:19:25 -0700614 pr_debug("Validating PGD populate\n");
Anshuman Khandual399145f2020-06-04 16:47:15 -0700615 /*
616 * This entry points to next level page table page.
617 * Hence this must not qualify as pgd_bad().
618 */
Gavin Shan2f87f8c2021-09-02 14:52:48 -0700619 p4d_clear(args->p4dp);
620 pgd_clear(args->pgdp);
621 pgd_populate(args->mm, args->pgdp, args->start_p4dp);
622 pgd = READ_ONCE(*args->pgdp);
Anshuman Khandual399145f2020-06-04 16:47:15 -0700623 WARN_ON(pgd_bad(pgd));
624}
625#else /* !__PAGETABLE_P4D_FOLDED */
Gavin Shan2f87f8c2021-09-02 14:52:48 -0700626static void __init p4d_clear_tests(struct pgtable_debug_args *args) { }
627static void __init pgd_clear_tests(struct pgtable_debug_args *args) { }
628static void __init p4d_populate_tests(struct pgtable_debug_args *args) { }
629static void __init pgd_populate_tests(struct pgtable_debug_args *args) { }
Anshuman Khandual399145f2020-06-04 16:47:15 -0700630#endif /* PAGETABLE_P4D_FOLDED */
631
Gavin Shan44966c42021-09-02 14:52:38 -0700632static void __init pte_clear_tests(struct pgtable_debug_args *args)
Anshuman Khandual399145f2020-06-04 16:47:15 -0700633{
Gavin Shan8c5b3a82021-09-02 14:52:54 -0700634 struct page *page;
Gavin Shan44966c42021-09-02 14:52:38 -0700635 pte_t pte = pfn_pte(args->pte_pfn, args->page_prot);
636
Gavin Shan8c5b3a82021-09-02 14:52:54 -0700637 page = (args->pte_pfn != ULONG_MAX) ? pfn_to_page(args->pte_pfn) : NULL;
638 if (!page)
Gavin Shan44966c42021-09-02 14:52:38 -0700639 return;
Anshuman Khandual399145f2020-06-04 16:47:15 -0700640
Gavin Shan8c5b3a82021-09-02 14:52:54 -0700641 /*
642 * flush_dcache_page() is called after set_pte_at() to clear
643 * PG_arch_1 for the page on ARM64. The page flag isn't cleared
644 * when it's released and page allocation check will fail when
645 * the page is allocated again. For architectures other than ARM64,
646 * the unexpected overhead of cache flushing is acceptable.
647 */
Anshuman Khandual6315df42020-08-06 23:19:25 -0700648 pr_debug("Validating PTE clear\n");
Aneesh Kumar K.V401035d2020-10-15 20:05:06 -0700649#ifndef CONFIG_RISCV
Anshuman Khandual399145f2020-06-04 16:47:15 -0700650 pte = __pte(pte_val(pte) | RANDOM_ORVALUE);
Aneesh Kumar K.V401035d2020-10-15 20:05:06 -0700651#endif
Gavin Shan44966c42021-09-02 14:52:38 -0700652 set_pte_at(args->mm, args->vaddr, args->ptep, pte);
Gavin Shan8c5b3a82021-09-02 14:52:54 -0700653 flush_dcache_page(page);
Anshuman Khandual399145f2020-06-04 16:47:15 -0700654 barrier();
Gavin Shan44966c42021-09-02 14:52:38 -0700655 pte_clear(args->mm, args->vaddr, args->ptep);
656 pte = ptep_get(args->ptep);
Anshuman Khandual399145f2020-06-04 16:47:15 -0700657 WARN_ON(!pte_none(pte));
658}
659
Gavin Shanc0fe07b2021-09-02 14:52:41 -0700660static void __init pmd_clear_tests(struct pgtable_debug_args *args)
Anshuman Khandual399145f2020-06-04 16:47:15 -0700661{
Gavin Shanc0fe07b2021-09-02 14:52:41 -0700662 pmd_t pmd = READ_ONCE(*args->pmdp);
Anshuman Khandual399145f2020-06-04 16:47:15 -0700663
Anshuman Khandual6315df42020-08-06 23:19:25 -0700664 pr_debug("Validating PMD clear\n");
Anshuman Khandual399145f2020-06-04 16:47:15 -0700665 pmd = __pmd(pmd_val(pmd) | RANDOM_ORVALUE);
Gavin Shanc0fe07b2021-09-02 14:52:41 -0700666 WRITE_ONCE(*args->pmdp, pmd);
667 pmd_clear(args->pmdp);
668 pmd = READ_ONCE(*args->pmdp);
Anshuman Khandual399145f2020-06-04 16:47:15 -0700669 WARN_ON(!pmd_none(pmd));
670}
671
Gavin Shanc0fe07b2021-09-02 14:52:41 -0700672static void __init pmd_populate_tests(struct pgtable_debug_args *args)
Anshuman Khandual399145f2020-06-04 16:47:15 -0700673{
674 pmd_t pmd;
675
Anshuman Khandual6315df42020-08-06 23:19:25 -0700676 pr_debug("Validating PMD populate\n");
Anshuman Khandual399145f2020-06-04 16:47:15 -0700677 /*
678 * This entry points to next level page table page.
679 * Hence this must not qualify as pmd_bad().
680 */
Gavin Shanc0fe07b2021-09-02 14:52:41 -0700681 pmd_populate(args->mm, args->pmdp, args->start_ptep);
682 pmd = READ_ONCE(*args->pmdp);
Anshuman Khandual399145f2020-06-04 16:47:15 -0700683 WARN_ON(pmd_bad(pmd));
684}
685
Gavin Shan8cb183f2021-09-02 14:52:28 -0700686static void __init pte_special_tests(struct pgtable_debug_args *args)
Anshuman Khandual05289402020-08-06 23:19:16 -0700687{
Gavin Shan8cb183f2021-09-02 14:52:28 -0700688 pte_t pte = pfn_pte(args->fixed_pte_pfn, args->page_prot);
Anshuman Khandual05289402020-08-06 23:19:16 -0700689
690 if (!IS_ENABLED(CONFIG_ARCH_HAS_PTE_SPECIAL))
691 return;
692
Anshuman Khandual6315df42020-08-06 23:19:25 -0700693 pr_debug("Validating PTE special\n");
Anshuman Khandual05289402020-08-06 23:19:16 -0700694 WARN_ON(!pte_special(pte_mkspecial(pte)));
695}
696
Gavin Shan8cb183f2021-09-02 14:52:28 -0700697static void __init pte_protnone_tests(struct pgtable_debug_args *args)
Anshuman Khandual05289402020-08-06 23:19:16 -0700698{
Gavin Shan8cb183f2021-09-02 14:52:28 -0700699 pte_t pte = pfn_pte(args->fixed_pte_pfn, args->page_prot_none);
Anshuman Khandual05289402020-08-06 23:19:16 -0700700
701 if (!IS_ENABLED(CONFIG_NUMA_BALANCING))
702 return;
703
Anshuman Khandual6315df42020-08-06 23:19:25 -0700704 pr_debug("Validating PTE protnone\n");
Anshuman Khandual05289402020-08-06 23:19:16 -0700705 WARN_ON(!pte_protnone(pte));
706 WARN_ON(!pte_present(pte));
707}
708
709#ifdef CONFIG_TRANSPARENT_HUGEPAGE
Gavin Shan8cb183f2021-09-02 14:52:28 -0700710static void __init pmd_protnone_tests(struct pgtable_debug_args *args)
Anshuman Khandual05289402020-08-06 23:19:16 -0700711{
Anshuman Khandual65ac1a62021-06-28 19:35:10 -0700712 pmd_t pmd;
Anshuman Khandual05289402020-08-06 23:19:16 -0700713
714 if (!IS_ENABLED(CONFIG_NUMA_BALANCING))
715 return;
716
Anshuman Khandual65ac1a62021-06-28 19:35:10 -0700717 if (!has_transparent_hugepage())
718 return;
719
Anshuman Khandual6315df42020-08-06 23:19:25 -0700720 pr_debug("Validating PMD protnone\n");
Gavin Shan8cb183f2021-09-02 14:52:28 -0700721 pmd = pmd_mkhuge(pfn_pmd(args->fixed_pmd_pfn, args->page_prot_none));
Anshuman Khandual05289402020-08-06 23:19:16 -0700722 WARN_ON(!pmd_protnone(pmd));
723 WARN_ON(!pmd_present(pmd));
724}
725#else /* !CONFIG_TRANSPARENT_HUGEPAGE */
Gavin Shan8cb183f2021-09-02 14:52:28 -0700726static void __init pmd_protnone_tests(struct pgtable_debug_args *args) { }
Anshuman Khandual05289402020-08-06 23:19:16 -0700727#endif /* CONFIG_TRANSPARENT_HUGEPAGE */
728
729#ifdef CONFIG_ARCH_HAS_PTE_DEVMAP
Gavin Shan8cb183f2021-09-02 14:52:28 -0700730static void __init pte_devmap_tests(struct pgtable_debug_args *args)
Anshuman Khandual05289402020-08-06 23:19:16 -0700731{
Gavin Shan8cb183f2021-09-02 14:52:28 -0700732 pte_t pte = pfn_pte(args->fixed_pte_pfn, args->page_prot);
Anshuman Khandual05289402020-08-06 23:19:16 -0700733
Anshuman Khandual6315df42020-08-06 23:19:25 -0700734 pr_debug("Validating PTE devmap\n");
Anshuman Khandual05289402020-08-06 23:19:16 -0700735 WARN_ON(!pte_devmap(pte_mkdevmap(pte)));
736}
737
738#ifdef CONFIG_TRANSPARENT_HUGEPAGE
Gavin Shan8cb183f2021-09-02 14:52:28 -0700739static void __init pmd_devmap_tests(struct pgtable_debug_args *args)
Anshuman Khandual05289402020-08-06 23:19:16 -0700740{
Anshuman Khandual65ac1a62021-06-28 19:35:10 -0700741 pmd_t pmd;
742
743 if (!has_transparent_hugepage())
744 return;
Anshuman Khandual05289402020-08-06 23:19:16 -0700745
Anshuman Khandual6315df42020-08-06 23:19:25 -0700746 pr_debug("Validating PMD devmap\n");
Gavin Shan8cb183f2021-09-02 14:52:28 -0700747 pmd = pfn_pmd(args->fixed_pmd_pfn, args->page_prot);
Anshuman Khandual05289402020-08-06 23:19:16 -0700748 WARN_ON(!pmd_devmap(pmd_mkdevmap(pmd)));
749}
750
751#ifdef CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD
Gavin Shan8cb183f2021-09-02 14:52:28 -0700752static void __init pud_devmap_tests(struct pgtable_debug_args *args)
Anshuman Khandual05289402020-08-06 23:19:16 -0700753{
Anshuman Khandual65ac1a62021-06-28 19:35:10 -0700754 pud_t pud;
755
756 if (!has_transparent_hugepage())
757 return;
Anshuman Khandual05289402020-08-06 23:19:16 -0700758
Anshuman Khandual6315df42020-08-06 23:19:25 -0700759 pr_debug("Validating PUD devmap\n");
Gavin Shan8cb183f2021-09-02 14:52:28 -0700760 pud = pfn_pud(args->fixed_pud_pfn, args->page_prot);
Anshuman Khandual05289402020-08-06 23:19:16 -0700761 WARN_ON(!pud_devmap(pud_mkdevmap(pud)));
762}
763#else /* !CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD */
Gavin Shan8cb183f2021-09-02 14:52:28 -0700764static void __init pud_devmap_tests(struct pgtable_debug_args *args) { }
Anshuman Khandual05289402020-08-06 23:19:16 -0700765#endif /* CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD */
766#else /* CONFIG_TRANSPARENT_HUGEPAGE */
Gavin Shan8cb183f2021-09-02 14:52:28 -0700767static void __init pmd_devmap_tests(struct pgtable_debug_args *args) { }
768static void __init pud_devmap_tests(struct pgtable_debug_args *args) { }
Anshuman Khandual05289402020-08-06 23:19:16 -0700769#endif /* CONFIG_TRANSPARENT_HUGEPAGE */
770#else
Gavin Shan8cb183f2021-09-02 14:52:28 -0700771static void __init pte_devmap_tests(struct pgtable_debug_args *args) { }
772static void __init pmd_devmap_tests(struct pgtable_debug_args *args) { }
773static void __init pud_devmap_tests(struct pgtable_debug_args *args) { }
Anshuman Khandual05289402020-08-06 23:19:16 -0700774#endif /* CONFIG_ARCH_HAS_PTE_DEVMAP */
775
Gavin Shan5f447e82021-09-02 14:52:32 -0700776static void __init pte_soft_dirty_tests(struct pgtable_debug_args *args)
Anshuman Khandual05289402020-08-06 23:19:16 -0700777{
Gavin Shan5f447e82021-09-02 14:52:32 -0700778 pte_t pte = pfn_pte(args->fixed_pte_pfn, args->page_prot);
Anshuman Khandual05289402020-08-06 23:19:16 -0700779
780 if (!IS_ENABLED(CONFIG_MEM_SOFT_DIRTY))
781 return;
782
Anshuman Khandual6315df42020-08-06 23:19:25 -0700783 pr_debug("Validating PTE soft dirty\n");
Anshuman Khandual05289402020-08-06 23:19:16 -0700784 WARN_ON(!pte_soft_dirty(pte_mksoft_dirty(pte)));
785 WARN_ON(pte_soft_dirty(pte_clear_soft_dirty(pte)));
786}
787
Gavin Shan5f447e82021-09-02 14:52:32 -0700788static void __init pte_swap_soft_dirty_tests(struct pgtable_debug_args *args)
Anshuman Khandual05289402020-08-06 23:19:16 -0700789{
Gavin Shan5f447e82021-09-02 14:52:32 -0700790 pte_t pte = pfn_pte(args->fixed_pte_pfn, args->page_prot);
Anshuman Khandual05289402020-08-06 23:19:16 -0700791
792 if (!IS_ENABLED(CONFIG_MEM_SOFT_DIRTY))
793 return;
794
Anshuman Khandual6315df42020-08-06 23:19:25 -0700795 pr_debug("Validating PTE swap soft dirty\n");
Anshuman Khandual05289402020-08-06 23:19:16 -0700796 WARN_ON(!pte_swp_soft_dirty(pte_swp_mksoft_dirty(pte)));
797 WARN_ON(pte_swp_soft_dirty(pte_swp_clear_soft_dirty(pte)));
798}
799
800#ifdef CONFIG_TRANSPARENT_HUGEPAGE
Gavin Shan5f447e82021-09-02 14:52:32 -0700801static void __init pmd_soft_dirty_tests(struct pgtable_debug_args *args)
Anshuman Khandual05289402020-08-06 23:19:16 -0700802{
Anshuman Khandual65ac1a62021-06-28 19:35:10 -0700803 pmd_t pmd;
Anshuman Khandual05289402020-08-06 23:19:16 -0700804
805 if (!IS_ENABLED(CONFIG_MEM_SOFT_DIRTY))
806 return;
807
Anshuman Khandual65ac1a62021-06-28 19:35:10 -0700808 if (!has_transparent_hugepage())
809 return;
810
Anshuman Khandual6315df42020-08-06 23:19:25 -0700811 pr_debug("Validating PMD soft dirty\n");
Gavin Shan5f447e82021-09-02 14:52:32 -0700812 pmd = pfn_pmd(args->fixed_pmd_pfn, args->page_prot);
Anshuman Khandual05289402020-08-06 23:19:16 -0700813 WARN_ON(!pmd_soft_dirty(pmd_mksoft_dirty(pmd)));
814 WARN_ON(pmd_soft_dirty(pmd_clear_soft_dirty(pmd)));
815}
816
Gavin Shan5f447e82021-09-02 14:52:32 -0700817static void __init pmd_swap_soft_dirty_tests(struct pgtable_debug_args *args)
Anshuman Khandual05289402020-08-06 23:19:16 -0700818{
Anshuman Khandual65ac1a62021-06-28 19:35:10 -0700819 pmd_t pmd;
Anshuman Khandual05289402020-08-06 23:19:16 -0700820
821 if (!IS_ENABLED(CONFIG_MEM_SOFT_DIRTY) ||
822 !IS_ENABLED(CONFIG_ARCH_ENABLE_THP_MIGRATION))
823 return;
824
Anshuman Khandual65ac1a62021-06-28 19:35:10 -0700825 if (!has_transparent_hugepage())
826 return;
827
Anshuman Khandual6315df42020-08-06 23:19:25 -0700828 pr_debug("Validating PMD swap soft dirty\n");
Gavin Shan5f447e82021-09-02 14:52:32 -0700829 pmd = pfn_pmd(args->fixed_pmd_pfn, args->page_prot);
Anshuman Khandual05289402020-08-06 23:19:16 -0700830 WARN_ON(!pmd_swp_soft_dirty(pmd_swp_mksoft_dirty(pmd)));
831 WARN_ON(pmd_swp_soft_dirty(pmd_swp_clear_soft_dirty(pmd)));
832}
Shixin Liub593b902021-06-30 18:47:40 -0700833#else /* !CONFIG_TRANSPARENT_HUGEPAGE */
Gavin Shan5f447e82021-09-02 14:52:32 -0700834static void __init pmd_soft_dirty_tests(struct pgtable_debug_args *args) { }
835static void __init pmd_swap_soft_dirty_tests(struct pgtable_debug_args *args) { }
Shixin Liub593b902021-06-30 18:47:40 -0700836#endif /* CONFIG_TRANSPARENT_HUGEPAGE */
Anshuman Khandual05289402020-08-06 23:19:16 -0700837
Gavin Shan5f447e82021-09-02 14:52:32 -0700838static void __init pte_swap_tests(struct pgtable_debug_args *args)
Anshuman Khandual05289402020-08-06 23:19:16 -0700839{
840 swp_entry_t swp;
841 pte_t pte;
842
Anshuman Khandual6315df42020-08-06 23:19:25 -0700843 pr_debug("Validating PTE swap\n");
Gavin Shan5f447e82021-09-02 14:52:32 -0700844 pte = pfn_pte(args->fixed_pte_pfn, args->page_prot);
Anshuman Khandual05289402020-08-06 23:19:16 -0700845 swp = __pte_to_swp_entry(pte);
846 pte = __swp_entry_to_pte(swp);
Gavin Shan5f447e82021-09-02 14:52:32 -0700847 WARN_ON(args->fixed_pte_pfn != pte_pfn(pte));
Anshuman Khandual05289402020-08-06 23:19:16 -0700848}
849
850#ifdef CONFIG_ARCH_ENABLE_THP_MIGRATION
Gavin Shan5f447e82021-09-02 14:52:32 -0700851static void __init pmd_swap_tests(struct pgtable_debug_args *args)
Anshuman Khandual05289402020-08-06 23:19:16 -0700852{
853 swp_entry_t swp;
854 pmd_t pmd;
855
Anshuman Khandual65ac1a62021-06-28 19:35:10 -0700856 if (!has_transparent_hugepage())
857 return;
858
Anshuman Khandual6315df42020-08-06 23:19:25 -0700859 pr_debug("Validating PMD swap\n");
Gavin Shan5f447e82021-09-02 14:52:32 -0700860 pmd = pfn_pmd(args->fixed_pmd_pfn, args->page_prot);
Anshuman Khandual05289402020-08-06 23:19:16 -0700861 swp = __pmd_to_swp_entry(pmd);
862 pmd = __swp_entry_to_pmd(swp);
Gavin Shan5f447e82021-09-02 14:52:32 -0700863 WARN_ON(args->fixed_pmd_pfn != pmd_pfn(pmd));
Anshuman Khandual05289402020-08-06 23:19:16 -0700864}
865#else /* !CONFIG_ARCH_ENABLE_THP_MIGRATION */
Gavin Shan5f447e82021-09-02 14:52:32 -0700866static void __init pmd_swap_tests(struct pgtable_debug_args *args) { }
Anshuman Khandual05289402020-08-06 23:19:16 -0700867#endif /* CONFIG_ARCH_ENABLE_THP_MIGRATION */
868
Gavin Shan4878a882021-09-02 14:52:35 -0700869static void __init swap_migration_tests(struct pgtable_debug_args *args)
Anshuman Khandual05289402020-08-06 23:19:16 -0700870{
871 struct page *page;
872 swp_entry_t swp;
873
874 if (!IS_ENABLED(CONFIG_MIGRATION))
875 return;
Anshuman Khandual6315df42020-08-06 23:19:25 -0700876
Anshuman Khandual05289402020-08-06 23:19:16 -0700877 /*
878 * swap_migration_tests() requires a dedicated page as it needs to
879 * be locked before creating a migration entry from it. Locking the
880 * page that actually maps kernel text ('start_kernel') can be real
Gavin Shan4878a882021-09-02 14:52:35 -0700881 * problematic. Lets use the allocated page explicitly for this
882 * purpose.
Anshuman Khandual05289402020-08-06 23:19:16 -0700883 */
Gavin Shan4878a882021-09-02 14:52:35 -0700884 page = (args->pte_pfn != ULONG_MAX) ? pfn_to_page(args->pte_pfn) : NULL;
885 if (!page)
Anshuman Khandual05289402020-08-06 23:19:16 -0700886 return;
Gavin Shan4878a882021-09-02 14:52:35 -0700887
888 pr_debug("Validating swap migration\n");
Anshuman Khandual05289402020-08-06 23:19:16 -0700889
890 /*
891 * make_migration_entry() expects given page to be
892 * locked, otherwise it stumbles upon a BUG_ON().
893 */
894 __SetPageLocked(page);
Alistair Popple4dd845b2021-06-30 18:54:09 -0700895 swp = make_writable_migration_entry(page_to_pfn(page));
Anshuman Khandual05289402020-08-06 23:19:16 -0700896 WARN_ON(!is_migration_entry(swp));
Alistair Popple4dd845b2021-06-30 18:54:09 -0700897 WARN_ON(!is_writable_migration_entry(swp));
Anshuman Khandual05289402020-08-06 23:19:16 -0700898
Alistair Popple4dd845b2021-06-30 18:54:09 -0700899 swp = make_readable_migration_entry(swp_offset(swp));
Anshuman Khandual05289402020-08-06 23:19:16 -0700900 WARN_ON(!is_migration_entry(swp));
Alistair Popple4dd845b2021-06-30 18:54:09 -0700901 WARN_ON(is_writable_migration_entry(swp));
Anshuman Khandual05289402020-08-06 23:19:16 -0700902
Alistair Popple4dd845b2021-06-30 18:54:09 -0700903 swp = make_readable_migration_entry(page_to_pfn(page));
Anshuman Khandual05289402020-08-06 23:19:16 -0700904 WARN_ON(!is_migration_entry(swp));
Alistair Popple4dd845b2021-06-30 18:54:09 -0700905 WARN_ON(is_writable_migration_entry(swp));
Anshuman Khandual05289402020-08-06 23:19:16 -0700906 __ClearPageLocked(page);
Anshuman Khandual05289402020-08-06 23:19:16 -0700907}
908
909#ifdef CONFIG_HUGETLB_PAGE
Gavin Shan36b77d12021-09-02 14:52:22 -0700910static void __init hugetlb_basic_tests(struct pgtable_debug_args *args)
Anshuman Khandual05289402020-08-06 23:19:16 -0700911{
912 struct page *page;
913 pte_t pte;
914
Anshuman Khandual6315df42020-08-06 23:19:25 -0700915 pr_debug("Validating HugeTLB basic\n");
Anshuman Khandual05289402020-08-06 23:19:16 -0700916 /*
917 * Accessing the page associated with the pfn is safe here,
918 * as it was previously derived from a real kernel symbol.
919 */
Gavin Shan36b77d12021-09-02 14:52:22 -0700920 page = pfn_to_page(args->fixed_pmd_pfn);
921 pte = mk_huge_pte(page, args->page_prot);
Anshuman Khandual05289402020-08-06 23:19:16 -0700922
923 WARN_ON(!huge_pte_dirty(huge_pte_mkdirty(pte)));
924 WARN_ON(!huge_pte_write(huge_pte_mkwrite(huge_pte_wrprotect(pte))));
925 WARN_ON(huge_pte_write(huge_pte_wrprotect(huge_pte_mkwrite(pte))));
926
927#ifdef CONFIG_ARCH_WANT_GENERAL_HUGETLB
Gavin Shan36b77d12021-09-02 14:52:22 -0700928 pte = pfn_pte(args->fixed_pmd_pfn, args->page_prot);
Anshuman Khandual05289402020-08-06 23:19:16 -0700929
930 WARN_ON(!pte_huge(pte_mkhuge(pte)));
931#endif /* CONFIG_ARCH_WANT_GENERAL_HUGETLB */
932}
933#else /* !CONFIG_HUGETLB_PAGE */
Gavin Shan36b77d12021-09-02 14:52:22 -0700934static void __init hugetlb_basic_tests(struct pgtable_debug_args *args) { }
Anshuman Khandual05289402020-08-06 23:19:16 -0700935#endif /* CONFIG_HUGETLB_PAGE */
936
937#ifdef CONFIG_TRANSPARENT_HUGEPAGE
Gavin Shan4878a882021-09-02 14:52:35 -0700938static void __init pmd_thp_tests(struct pgtable_debug_args *args)
Anshuman Khandual05289402020-08-06 23:19:16 -0700939{
940 pmd_t pmd;
941
942 if (!has_transparent_hugepage())
943 return;
944
Anshuman Khandual6315df42020-08-06 23:19:25 -0700945 pr_debug("Validating PMD based THP\n");
Anshuman Khandual05289402020-08-06 23:19:16 -0700946 /*
947 * pmd_trans_huge() and pmd_present() must return positive after
948 * MMU invalidation with pmd_mkinvalid(). This behavior is an
949 * optimization for transparent huge page. pmd_trans_huge() must
950 * be true if pmd_page() returns a valid THP to avoid taking the
951 * pmd_lock when others walk over non transhuge pmds (i.e. there
952 * are no THP allocated). Especially when splitting a THP and
953 * removing the present bit from the pmd, pmd_trans_huge() still
954 * needs to return true. pmd_present() should be true whenever
955 * pmd_trans_huge() returns true.
956 */
Gavin Shan4878a882021-09-02 14:52:35 -0700957 pmd = pfn_pmd(args->fixed_pmd_pfn, args->page_prot);
Anshuman Khandual05289402020-08-06 23:19:16 -0700958 WARN_ON(!pmd_trans_huge(pmd_mkhuge(pmd)));
959
960#ifndef __HAVE_ARCH_PMDP_INVALIDATE
961 WARN_ON(!pmd_trans_huge(pmd_mkinvalid(pmd_mkhuge(pmd))));
962 WARN_ON(!pmd_present(pmd_mkinvalid(pmd_mkhuge(pmd))));
963#endif /* __HAVE_ARCH_PMDP_INVALIDATE */
964}
965
966#ifdef CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD
Gavin Shan4878a882021-09-02 14:52:35 -0700967static void __init pud_thp_tests(struct pgtable_debug_args *args)
Anshuman Khandual05289402020-08-06 23:19:16 -0700968{
969 pud_t pud;
970
971 if (!has_transparent_hugepage())
972 return;
973
Anshuman Khandual6315df42020-08-06 23:19:25 -0700974 pr_debug("Validating PUD based THP\n");
Gavin Shan4878a882021-09-02 14:52:35 -0700975 pud = pfn_pud(args->fixed_pud_pfn, args->page_prot);
Anshuman Khandual05289402020-08-06 23:19:16 -0700976 WARN_ON(!pud_trans_huge(pud_mkhuge(pud)));
977
978 /*
979 * pud_mkinvalid() has been dropped for now. Enable back
980 * these tests when it comes back with a modified pud_present().
981 *
982 * WARN_ON(!pud_trans_huge(pud_mkinvalid(pud_mkhuge(pud))));
983 * WARN_ON(!pud_present(pud_mkinvalid(pud_mkhuge(pud))));
984 */
985}
986#else /* !CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD */
Gavin Shan4878a882021-09-02 14:52:35 -0700987static void __init pud_thp_tests(struct pgtable_debug_args *args) { }
Anshuman Khandual05289402020-08-06 23:19:16 -0700988#endif /* CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD */
989#else /* !CONFIG_TRANSPARENT_HUGEPAGE */
Gavin Shan4878a882021-09-02 14:52:35 -0700990static void __init pmd_thp_tests(struct pgtable_debug_args *args) { }
991static void __init pud_thp_tests(struct pgtable_debug_args *args) { }
Anshuman Khandual05289402020-08-06 23:19:16 -0700992#endif /* CONFIG_TRANSPARENT_HUGEPAGE */
993
Anshuman Khandual399145f2020-06-04 16:47:15 -0700994static unsigned long __init get_random_vaddr(void)
995{
996 unsigned long random_vaddr, random_pages, total_user_pages;
997
998 total_user_pages = (TASK_SIZE - FIRST_USER_ADDRESS) / PAGE_SIZE;
999
1000 random_pages = get_random_long() % total_user_pages;
1001 random_vaddr = FIRST_USER_ADDRESS + random_pages * PAGE_SIZE;
1002
1003 return random_vaddr;
1004}
1005
Gavin Shan3c9b84f2021-09-02 14:52:19 -07001006static void __init destroy_args(struct pgtable_debug_args *args)
1007{
1008 struct page *page = NULL;
1009
1010 /* Free (huge) page */
1011 if (IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE) &&
1012 IS_ENABLED(CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD) &&
1013 has_transparent_hugepage() &&
1014 args->pud_pfn != ULONG_MAX) {
1015 if (args->is_contiguous_page) {
1016 free_contig_range(args->pud_pfn,
1017 (1 << (HPAGE_PUD_SHIFT - PAGE_SHIFT)));
1018 } else {
1019 page = pfn_to_page(args->pud_pfn);
1020 __free_pages(page, HPAGE_PUD_SHIFT - PAGE_SHIFT);
1021 }
1022
1023 args->pud_pfn = ULONG_MAX;
1024 args->pmd_pfn = ULONG_MAX;
1025 args->pte_pfn = ULONG_MAX;
1026 }
1027
1028 if (IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE) &&
1029 has_transparent_hugepage() &&
1030 args->pmd_pfn != ULONG_MAX) {
1031 if (args->is_contiguous_page) {
1032 free_contig_range(args->pmd_pfn, (1 << HPAGE_PMD_ORDER));
1033 } else {
1034 page = pfn_to_page(args->pmd_pfn);
1035 __free_pages(page, HPAGE_PMD_ORDER);
1036 }
1037
1038 args->pmd_pfn = ULONG_MAX;
1039 args->pte_pfn = ULONG_MAX;
1040 }
1041
1042 if (args->pte_pfn != ULONG_MAX) {
1043 page = pfn_to_page(args->pte_pfn);
1044 __free_pages(page, 0);
1045
1046 args->pte_pfn = ULONG_MAX;
1047 }
1048
1049 /* Free page table entries */
1050 if (args->start_ptep) {
1051 pte_free(args->mm, args->start_ptep);
1052 mm_dec_nr_ptes(args->mm);
1053 }
1054
1055 if (args->start_pmdp) {
1056 pmd_free(args->mm, args->start_pmdp);
1057 mm_dec_nr_pmds(args->mm);
1058 }
1059
1060 if (args->start_pudp) {
1061 pud_free(args->mm, args->start_pudp);
1062 mm_dec_nr_puds(args->mm);
1063 }
1064
1065 if (args->start_p4dp)
1066 p4d_free(args->mm, args->start_p4dp);
1067
1068 /* Free vma and mm struct */
1069 if (args->vma)
1070 vm_area_free(args->vma);
1071
1072 if (args->mm)
1073 mmdrop(args->mm);
1074}
1075
1076static struct page * __init
1077debug_vm_pgtable_alloc_huge_page(struct pgtable_debug_args *args, int order)
1078{
1079 struct page *page = NULL;
1080
1081#ifdef CONFIG_CONTIG_ALLOC
1082 if (order >= MAX_ORDER) {
1083 page = alloc_contig_pages((1 << order), GFP_KERNEL,
1084 first_online_node, NULL);
1085 if (page) {
1086 args->is_contiguous_page = true;
1087 return page;
1088 }
1089 }
1090#endif
1091
1092 if (order < MAX_ORDER)
1093 page = alloc_pages(GFP_KERNEL, order);
1094
1095 return page;
1096}
1097
1098static int __init init_args(struct pgtable_debug_args *args)
1099{
1100 struct page *page = NULL;
1101 phys_addr_t phys;
1102 int ret = 0;
1103
1104 /*
1105 * Initialize the debugging data.
1106 *
Guo Ren87727162021-11-05 13:36:09 -07001107 * protection_map[0] (or even protection_map[8]) will help create
1108 * page table entries with PROT_NONE permission as required for
1109 * pxx_protnone_tests().
Gavin Shan3c9b84f2021-09-02 14:52:19 -07001110 */
1111 memset(args, 0, sizeof(*args));
1112 args->vaddr = get_random_vaddr();
1113 args->page_prot = vm_get_page_prot(VMFLAGS);
Guo Ren87727162021-11-05 13:36:09 -07001114 args->page_prot_none = protection_map[0];
Gavin Shan3c9b84f2021-09-02 14:52:19 -07001115 args->is_contiguous_page = false;
1116 args->pud_pfn = ULONG_MAX;
1117 args->pmd_pfn = ULONG_MAX;
1118 args->pte_pfn = ULONG_MAX;
1119 args->fixed_pgd_pfn = ULONG_MAX;
1120 args->fixed_p4d_pfn = ULONG_MAX;
1121 args->fixed_pud_pfn = ULONG_MAX;
1122 args->fixed_pmd_pfn = ULONG_MAX;
1123 args->fixed_pte_pfn = ULONG_MAX;
1124
1125 /* Allocate mm and vma */
1126 args->mm = mm_alloc();
1127 if (!args->mm) {
1128 pr_err("Failed to allocate mm struct\n");
1129 ret = -ENOMEM;
1130 goto error;
1131 }
1132
1133 args->vma = vm_area_alloc(args->mm);
1134 if (!args->vma) {
1135 pr_err("Failed to allocate vma\n");
1136 ret = -ENOMEM;
1137 goto error;
1138 }
1139
1140 /*
1141 * Allocate page table entries. They will be modified in the tests.
1142 * Lets save the page table entries so that they can be released
1143 * when the tests are completed.
1144 */
1145 args->pgdp = pgd_offset(args->mm, args->vaddr);
1146 args->p4dp = p4d_alloc(args->mm, args->pgdp, args->vaddr);
1147 if (!args->p4dp) {
1148 pr_err("Failed to allocate p4d entries\n");
1149 ret = -ENOMEM;
1150 goto error;
1151 }
1152 args->start_p4dp = p4d_offset(args->pgdp, 0UL);
1153 WARN_ON(!args->start_p4dp);
1154
1155 args->pudp = pud_alloc(args->mm, args->p4dp, args->vaddr);
1156 if (!args->pudp) {
1157 pr_err("Failed to allocate pud entries\n");
1158 ret = -ENOMEM;
1159 goto error;
1160 }
1161 args->start_pudp = pud_offset(args->p4dp, 0UL);
1162 WARN_ON(!args->start_pudp);
1163
1164 args->pmdp = pmd_alloc(args->mm, args->pudp, args->vaddr);
1165 if (!args->pmdp) {
1166 pr_err("Failed to allocate pmd entries\n");
1167 ret = -ENOMEM;
1168 goto error;
1169 }
1170 args->start_pmdp = pmd_offset(args->pudp, 0UL);
1171 WARN_ON(!args->start_pmdp);
1172
1173 if (pte_alloc(args->mm, args->pmdp)) {
1174 pr_err("Failed to allocate pte entries\n");
1175 ret = -ENOMEM;
1176 goto error;
1177 }
1178 args->start_ptep = pmd_pgtable(READ_ONCE(*args->pmdp));
1179 WARN_ON(!args->start_ptep);
1180
1181 /*
1182 * PFN for mapping at PTE level is determined from a standard kernel
1183 * text symbol. But pfns for higher page table levels are derived by
1184 * masking lower bits of this real pfn. These derived pfns might not
1185 * exist on the platform but that does not really matter as pfn_pxx()
1186 * helpers will still create appropriate entries for the test. This
1187 * helps avoid large memory block allocations to be used for mapping
1188 * at higher page table levels in some of the tests.
1189 */
1190 phys = __pa_symbol(&start_kernel);
1191 args->fixed_pgd_pfn = __phys_to_pfn(phys & PGDIR_MASK);
1192 args->fixed_p4d_pfn = __phys_to_pfn(phys & P4D_MASK);
1193 args->fixed_pud_pfn = __phys_to_pfn(phys & PUD_MASK);
1194 args->fixed_pmd_pfn = __phys_to_pfn(phys & PMD_MASK);
1195 args->fixed_pte_pfn = __phys_to_pfn(phys & PAGE_MASK);
1196 WARN_ON(!pfn_valid(args->fixed_pte_pfn));
1197
1198 /*
1199 * Allocate (huge) pages because some of the tests need to access
1200 * the data in the pages. The corresponding tests will be skipped
1201 * if we fail to allocate (huge) pages.
1202 */
1203 if (IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE) &&
1204 IS_ENABLED(CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD) &&
1205 has_transparent_hugepage()) {
1206 page = debug_vm_pgtable_alloc_huge_page(args,
1207 HPAGE_PUD_SHIFT - PAGE_SHIFT);
1208 if (page) {
1209 args->pud_pfn = page_to_pfn(page);
1210 args->pmd_pfn = args->pud_pfn;
1211 args->pte_pfn = args->pud_pfn;
1212 return 0;
1213 }
1214 }
1215
1216 if (IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE) &&
1217 has_transparent_hugepage()) {
1218 page = debug_vm_pgtable_alloc_huge_page(args, HPAGE_PMD_ORDER);
1219 if (page) {
1220 args->pmd_pfn = page_to_pfn(page);
1221 args->pte_pfn = args->pmd_pfn;
1222 return 0;
1223 }
1224 }
1225
1226 page = alloc_pages(GFP_KERNEL, 0);
1227 if (page)
1228 args->pte_pfn = page_to_pfn(page);
1229
1230 return 0;
1231
1232error:
1233 destroy_args(args);
1234 return ret;
1235}
1236
Anshuman Khandual399145f2020-06-04 16:47:15 -07001237static int __init debug_vm_pgtable(void)
1238{
Gavin Shan3c9b84f2021-09-02 14:52:19 -07001239 struct pgtable_debug_args args;
Kees Cookfea11202020-06-03 13:28:45 -07001240 spinlock_t *ptl = NULL;
Gavin Shan3c9b84f2021-09-02 14:52:19 -07001241 int idx, ret;
Anshuman Khandual399145f2020-06-04 16:47:15 -07001242
1243 pr_info("Validating architecture page table helpers\n");
Gavin Shan3c9b84f2021-09-02 14:52:19 -07001244 ret = init_args(&args);
1245 if (ret)
1246 return ret;
1247
Anshuman Khandual2e326c02021-02-24 12:01:36 -08001248 /*
1249 * Iterate over the protection_map[] to make sure that all
1250 * the basic page table transformation validations just hold
1251 * true irrespective of the starting protection value for a
1252 * given page table entry.
1253 */
1254 for (idx = 0; idx < ARRAY_SIZE(protection_map); idx++) {
Gavin Shan36b77d12021-09-02 14:52:22 -07001255 pte_basic_tests(&args, idx);
1256 pmd_basic_tests(&args, idx);
1257 pud_basic_tests(&args, idx);
Anshuman Khandual2e326c02021-02-24 12:01:36 -08001258 }
1259
1260 /*
1261 * Both P4D and PGD level tests are very basic which do not
1262 * involve creating page table entries from the protection
1263 * value and the given pfn. Hence just keep them out from
1264 * the above iteration for now to save some test execution
1265 * time.
1266 */
Gavin Shan36b77d12021-09-02 14:52:22 -07001267 p4d_basic_tests(&args);
1268 pgd_basic_tests(&args);
Anshuman Khandual399145f2020-06-04 16:47:15 -07001269
Gavin Shan8983d232021-09-02 14:52:25 -07001270 pmd_leaf_tests(&args);
1271 pud_leaf_tests(&args);
Anshuman Khanduala5c3b9f2020-08-06 23:19:20 -07001272
Gavin Shan8983d232021-09-02 14:52:25 -07001273 pte_savedwrite_tests(&args);
1274 pmd_savedwrite_tests(&args);
Anshuman Khanduala5c3b9f2020-08-06 23:19:20 -07001275
Gavin Shan8cb183f2021-09-02 14:52:28 -07001276 pte_special_tests(&args);
1277 pte_protnone_tests(&args);
1278 pmd_protnone_tests(&args);
Anshuman Khandual05289402020-08-06 23:19:16 -07001279
Gavin Shan8cb183f2021-09-02 14:52:28 -07001280 pte_devmap_tests(&args);
1281 pmd_devmap_tests(&args);
1282 pud_devmap_tests(&args);
Anshuman Khandual05289402020-08-06 23:19:16 -07001283
Gavin Shan5f447e82021-09-02 14:52:32 -07001284 pte_soft_dirty_tests(&args);
1285 pmd_soft_dirty_tests(&args);
1286 pte_swap_soft_dirty_tests(&args);
1287 pmd_swap_soft_dirty_tests(&args);
Anshuman Khandual05289402020-08-06 23:19:16 -07001288
Gavin Shan5f447e82021-09-02 14:52:32 -07001289 pte_swap_tests(&args);
1290 pmd_swap_tests(&args);
Anshuman Khandual05289402020-08-06 23:19:16 -07001291
Gavin Shan4878a882021-09-02 14:52:35 -07001292 swap_migration_tests(&args);
Anshuman Khandual05289402020-08-06 23:19:16 -07001293
Gavin Shan4878a882021-09-02 14:52:35 -07001294 pmd_thp_tests(&args);
1295 pud_thp_tests(&args);
Anshuman Khandual05289402020-08-06 23:19:16 -07001296
Gavin Shan36b77d12021-09-02 14:52:22 -07001297 hugetlb_basic_tests(&args);
Aneesh Kumar K.Ve8edf0a2020-10-15 20:04:49 -07001298
Aneesh Kumar K.V6f302e272020-10-15 20:04:53 -07001299 /*
1300 * Page table modifying tests. They need to hold
1301 * proper page table lock.
1302 */
Aneesh Kumar K.Ve8edf0a2020-10-15 20:04:49 -07001303
Gavin Shan44966c42021-09-02 14:52:38 -07001304 args.ptep = pte_offset_map_lock(args.mm, args.pmdp, args.vaddr, &ptl);
1305 pte_clear_tests(&args);
1306 pte_advanced_tests(&args);
1307 pte_unmap_unlock(args.ptep, ptl);
Aneesh Kumar K.Ve8edf0a2020-10-15 20:04:49 -07001308
Gavin Shanc0fe07b2021-09-02 14:52:41 -07001309 ptl = pmd_lock(args.mm, args.pmdp);
1310 pmd_clear_tests(&args);
1311 pmd_advanced_tests(&args);
1312 pmd_huge_tests(&args);
1313 pmd_populate_tests(&args);
Aneesh Kumar K.V6f302e272020-10-15 20:04:53 -07001314 spin_unlock(ptl);
1315
Gavin Shan4cbde032021-09-02 14:52:45 -07001316 ptl = pud_lock(args.mm, args.pudp);
1317 pud_clear_tests(&args);
1318 pud_advanced_tests(&args);
1319 pud_huge_tests(&args);
1320 pud_populate_tests(&args);
Aneesh Kumar K.V6f302e272020-10-15 20:04:53 -07001321 spin_unlock(ptl);
1322
Gavin Shan2f87f8c2021-09-02 14:52:48 -07001323 spin_lock(&(args.mm->page_table_lock));
1324 p4d_clear_tests(&args);
1325 pgd_clear_tests(&args);
1326 p4d_populate_tests(&args);
1327 pgd_populate_tests(&args);
1328 spin_unlock(&(args.mm->page_table_lock));
Aneesh Kumar K.Ve8edf0a2020-10-15 20:04:49 -07001329
Gavin Shan3c9b84f2021-09-02 14:52:19 -07001330 destroy_args(&args);
Anshuman Khandual399145f2020-06-04 16:47:15 -07001331 return 0;
1332}
1333late_initcall(debug_vm_pgtable);