blob: db2abd9e415b7d8b8cdf28611360d5e234367e53 [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));
Pasha Tatashinfb5222a2022-02-03 20:49:10 -0800174
175 ptep_get_and_clear_full(args->mm, args->vaddr, args->ptep, 1);
Anshuman Khanduala5c3b9f2020-08-06 23:19:20 -0700176}
177
Gavin Shan8983d232021-09-02 14:52:25 -0700178static void __init pte_savedwrite_tests(struct pgtable_debug_args *args)
Anshuman Khanduala5c3b9f2020-08-06 23:19:20 -0700179{
Gavin Shan8983d232021-09-02 14:52:25 -0700180 pte_t pte = pfn_pte(args->fixed_pte_pfn, args->page_prot_none);
Anshuman Khanduala5c3b9f2020-08-06 23:19:20 -0700181
Aneesh Kumar K.V42006052020-10-15 20:04:40 -0700182 if (!IS_ENABLED(CONFIG_NUMA_BALANCING))
183 return;
184
Anshuman Khandual6315df42020-08-06 23:19:25 -0700185 pr_debug("Validating PTE saved write\n");
Anshuman Khanduala5c3b9f2020-08-06 23:19:20 -0700186 WARN_ON(!pte_savedwrite(pte_mk_savedwrite(pte_clear_savedwrite(pte))));
187 WARN_ON(pte_savedwrite(pte_clear_savedwrite(pte_mk_savedwrite(pte))));
188}
Aneesh Kumar K.V42006052020-10-15 20:04:40 -0700189
Anshuman Khandual399145f2020-06-04 16:47:15 -0700190#ifdef CONFIG_TRANSPARENT_HUGEPAGE
Gavin Shan36b77d12021-09-02 14:52:22 -0700191static void __init pmd_basic_tests(struct pgtable_debug_args *args, int idx)
Anshuman Khandual399145f2020-06-04 16:47:15 -0700192{
Anshuman Khandual2e326c02021-02-24 12:01:36 -0800193 pgprot_t prot = protection_map[idx];
Anshuman Khandual2e326c02021-02-24 12:01:36 -0800194 unsigned long val = idx, *ptr = &val;
Anshuman Khandual65ac1a62021-06-28 19:35:10 -0700195 pmd_t pmd;
Anshuman Khandual399145f2020-06-04 16:47:15 -0700196
Aneesh Kumar K.V787d5632020-06-10 18:41:44 -0700197 if (!has_transparent_hugepage())
198 return;
199
Anshuman Khandual2e326c02021-02-24 12:01:36 -0800200 pr_debug("Validating PMD basic (%pGv)\n", ptr);
Gavin Shan36b77d12021-09-02 14:52:22 -0700201 pmd = pfn_pmd(args->fixed_pmd_pfn, prot);
Anshuman Khandualbb5c47c2021-02-24 12:01:32 -0800202
203 /*
204 * This test needs to be executed after the given page table entry
205 * is created with pfn_pmd() to make sure that protection_map[idx]
206 * does not have the dirty bit enabled from the beginning. This is
207 * important for platforms like arm64 where (!PTE_RDONLY) indicate
208 * dirty bit being set.
209 */
210 WARN_ON(pmd_dirty(pmd_wrprotect(pmd)));
211
212
Anshuman Khandual399145f2020-06-04 16:47:15 -0700213 WARN_ON(!pmd_same(pmd, pmd));
214 WARN_ON(!pmd_young(pmd_mkyoung(pmd_mkold(pmd))));
215 WARN_ON(!pmd_dirty(pmd_mkdirty(pmd_mkclean(pmd))));
216 WARN_ON(!pmd_write(pmd_mkwrite(pmd_wrprotect(pmd))));
217 WARN_ON(pmd_young(pmd_mkold(pmd_mkyoung(pmd))));
218 WARN_ON(pmd_dirty(pmd_mkclean(pmd_mkdirty(pmd))));
219 WARN_ON(pmd_write(pmd_wrprotect(pmd_mkwrite(pmd))));
Anshuman Khandualbb5c47c2021-02-24 12:01:32 -0800220 WARN_ON(pmd_dirty(pmd_wrprotect(pmd_mkclean(pmd))));
221 WARN_ON(!pmd_dirty(pmd_wrprotect(pmd_mkdirty(pmd))));
Anshuman Khandual399145f2020-06-04 16:47:15 -0700222 /*
223 * A huge page does not point to next level page table
224 * entry. Hence this must qualify as pmd_bad().
225 */
226 WARN_ON(!pmd_bad(pmd_mkhuge(pmd)));
227}
228
Gavin Shanc0fe07b2021-09-02 14:52:41 -0700229static void __init pmd_advanced_tests(struct pgtable_debug_args *args)
Anshuman Khanduala5c3b9f2020-08-06 23:19:20 -0700230{
Gavin Shan8c5b3a82021-09-02 14:52:54 -0700231 struct page *page;
Anshuman Khandual65ac1a62021-06-28 19:35:10 -0700232 pmd_t pmd;
Gavin Shanc0fe07b2021-09-02 14:52:41 -0700233 unsigned long vaddr = args->vaddr;
Anshuman Khanduala5c3b9f2020-08-06 23:19:20 -0700234
235 if (!has_transparent_hugepage())
236 return;
237
Gavin Shan8c5b3a82021-09-02 14:52:54 -0700238 page = (args->pmd_pfn != ULONG_MAX) ? pfn_to_page(args->pmd_pfn) : NULL;
239 if (!page)
Gavin Shanc0fe07b2021-09-02 14:52:41 -0700240 return;
241
Gavin Shan8c5b3a82021-09-02 14:52:54 -0700242 /*
243 * flush_dcache_page() is called after set_pmd_at() to clear
244 * PG_arch_1 for the page on ARM64. The page flag isn't cleared
245 * when it's released and page allocation check will fail when
246 * the page is allocated again. For architectures other than ARM64,
247 * the unexpected overhead of cache flushing is acceptable.
248 */
Anshuman Khandual6315df42020-08-06 23:19:25 -0700249 pr_debug("Validating PMD advanced\n");
Anshuman Khanduala5c3b9f2020-08-06 23:19:20 -0700250 /* Align the address wrt HPAGE_PMD_SIZE */
Gerald Schaefer04f7ce32021-06-04 20:01:18 -0700251 vaddr &= HPAGE_PMD_MASK;
Anshuman Khanduala5c3b9f2020-08-06 23:19:20 -0700252
Gavin Shanc0fe07b2021-09-02 14:52:41 -0700253 pgtable_trans_huge_deposit(args->mm, args->pmdp, args->start_ptep);
Aneesh Kumar K.V87f34982020-10-15 20:04:56 -0700254
Gavin Shanc0fe07b2021-09-02 14:52:41 -0700255 pmd = pfn_pmd(args->pmd_pfn, args->page_prot);
256 set_pmd_at(args->mm, vaddr, args->pmdp, pmd);
Gavin Shan8c5b3a82021-09-02 14:52:54 -0700257 flush_dcache_page(page);
Gavin Shanc0fe07b2021-09-02 14:52:41 -0700258 pmdp_set_wrprotect(args->mm, vaddr, args->pmdp);
259 pmd = READ_ONCE(*args->pmdp);
Anshuman Khanduala5c3b9f2020-08-06 23:19:20 -0700260 WARN_ON(pmd_write(pmd));
Gavin Shanc0fe07b2021-09-02 14:52:41 -0700261 pmdp_huge_get_and_clear(args->mm, vaddr, args->pmdp);
262 pmd = READ_ONCE(*args->pmdp);
Anshuman Khanduala5c3b9f2020-08-06 23:19:20 -0700263 WARN_ON(!pmd_none(pmd));
264
Gavin Shanc0fe07b2021-09-02 14:52:41 -0700265 pmd = pfn_pmd(args->pmd_pfn, args->page_prot);
Anshuman Khanduala5c3b9f2020-08-06 23:19:20 -0700266 pmd = pmd_wrprotect(pmd);
267 pmd = pmd_mkclean(pmd);
Gavin Shanc0fe07b2021-09-02 14:52:41 -0700268 set_pmd_at(args->mm, vaddr, args->pmdp, pmd);
Gavin Shan8c5b3a82021-09-02 14:52:54 -0700269 flush_dcache_page(page);
Anshuman Khanduala5c3b9f2020-08-06 23:19:20 -0700270 pmd = pmd_mkwrite(pmd);
271 pmd = pmd_mkdirty(pmd);
Gavin Shanc0fe07b2021-09-02 14:52:41 -0700272 pmdp_set_access_flags(args->vma, vaddr, args->pmdp, pmd, 1);
273 pmd = READ_ONCE(*args->pmdp);
Anshuman Khanduala5c3b9f2020-08-06 23:19:20 -0700274 WARN_ON(!(pmd_write(pmd) && pmd_dirty(pmd)));
Gavin Shanc0fe07b2021-09-02 14:52:41 -0700275 pmdp_huge_get_and_clear_full(args->vma, vaddr, args->pmdp, 1);
276 pmd = READ_ONCE(*args->pmdp);
Anshuman Khanduala5c3b9f2020-08-06 23:19:20 -0700277 WARN_ON(!pmd_none(pmd));
278
Gavin Shanc0fe07b2021-09-02 14:52:41 -0700279 pmd = pmd_mkhuge(pfn_pmd(args->pmd_pfn, args->page_prot));
Anshuman Khanduala5c3b9f2020-08-06 23:19:20 -0700280 pmd = pmd_mkyoung(pmd);
Gavin Shanc0fe07b2021-09-02 14:52:41 -0700281 set_pmd_at(args->mm, vaddr, args->pmdp, pmd);
Gavin Shan8c5b3a82021-09-02 14:52:54 -0700282 flush_dcache_page(page);
Gavin Shanc0fe07b2021-09-02 14:52:41 -0700283 pmdp_test_and_clear_young(args->vma, vaddr, args->pmdp);
284 pmd = READ_ONCE(*args->pmdp);
Anshuman Khanduala5c3b9f2020-08-06 23:19:20 -0700285 WARN_ON(pmd_young(pmd));
Aneesh Kumar K.V87f34982020-10-15 20:04:56 -0700286
Aneesh Kumar K.V13af0502020-10-15 20:04:59 -0700287 /* Clear the pte entries */
Gavin Shanc0fe07b2021-09-02 14:52:41 -0700288 pmdp_huge_get_and_clear(args->mm, vaddr, args->pmdp);
289 pgtable_trans_huge_withdraw(args->mm, args->pmdp);
Anshuman Khanduala5c3b9f2020-08-06 23:19:20 -0700290}
291
Gavin Shan8983d232021-09-02 14:52:25 -0700292static void __init pmd_leaf_tests(struct pgtable_debug_args *args)
Anshuman Khanduala5c3b9f2020-08-06 23:19:20 -0700293{
Anshuman Khandual65ac1a62021-06-28 19:35:10 -0700294 pmd_t pmd;
295
296 if (!has_transparent_hugepage())
297 return;
Anshuman Khanduala5c3b9f2020-08-06 23:19:20 -0700298
Anshuman Khandual6315df42020-08-06 23:19:25 -0700299 pr_debug("Validating PMD leaf\n");
Gavin Shan8983d232021-09-02 14:52:25 -0700300 pmd = pfn_pmd(args->fixed_pmd_pfn, args->page_prot);
Anshuman Khandual65ac1a62021-06-28 19:35:10 -0700301
Anshuman Khanduala5c3b9f2020-08-06 23:19:20 -0700302 /*
303 * PMD based THP is a leaf entry.
304 */
305 pmd = pmd_mkhuge(pmd);
306 WARN_ON(!pmd_leaf(pmd));
307}
308
Gavin Shan8983d232021-09-02 14:52:25 -0700309static void __init pmd_savedwrite_tests(struct pgtable_debug_args *args)
Anshuman Khanduala5c3b9f2020-08-06 23:19:20 -0700310{
Anshuman Khandual65ac1a62021-06-28 19:35:10 -0700311 pmd_t pmd;
Anshuman Khanduala5c3b9f2020-08-06 23:19:20 -0700312
Aneesh Kumar K.V42006052020-10-15 20:04:40 -0700313 if (!IS_ENABLED(CONFIG_NUMA_BALANCING))
314 return;
315
Anshuman Khandual65ac1a62021-06-28 19:35:10 -0700316 if (!has_transparent_hugepage())
317 return;
318
Anshuman Khandual6315df42020-08-06 23:19:25 -0700319 pr_debug("Validating PMD saved write\n");
Gavin Shan8983d232021-09-02 14:52:25 -0700320 pmd = pfn_pmd(args->fixed_pmd_pfn, args->page_prot_none);
Anshuman Khanduala5c3b9f2020-08-06 23:19:20 -0700321 WARN_ON(!pmd_savedwrite(pmd_mk_savedwrite(pmd_clear_savedwrite(pmd))));
322 WARN_ON(pmd_savedwrite(pmd_clear_savedwrite(pmd_mk_savedwrite(pmd))));
323}
324
Anshuman Khandual399145f2020-06-04 16:47:15 -0700325#ifdef CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD
Gavin Shan36b77d12021-09-02 14:52:22 -0700326static void __init pud_basic_tests(struct pgtable_debug_args *args, int idx)
Anshuman Khandual399145f2020-06-04 16:47:15 -0700327{
Anshuman Khandual2e326c02021-02-24 12:01:36 -0800328 pgprot_t prot = protection_map[idx];
Anshuman Khandual2e326c02021-02-24 12:01:36 -0800329 unsigned long val = idx, *ptr = &val;
Anshuman Khandual65ac1a62021-06-28 19:35:10 -0700330 pud_t pud;
Anshuman Khandual399145f2020-06-04 16:47:15 -0700331
Aneesh Kumar K.V787d5632020-06-10 18:41:44 -0700332 if (!has_transparent_hugepage())
333 return;
334
Anshuman Khandual2e326c02021-02-24 12:01:36 -0800335 pr_debug("Validating PUD basic (%pGv)\n", ptr);
Gavin Shan36b77d12021-09-02 14:52:22 -0700336 pud = pfn_pud(args->fixed_pud_pfn, prot);
Anshuman Khandualbb5c47c2021-02-24 12:01:32 -0800337
338 /*
339 * This test needs to be executed after the given page table entry
340 * is created with pfn_pud() to make sure that protection_map[idx]
341 * does not have the dirty bit enabled from the beginning. This is
342 * important for platforms like arm64 where (!PTE_RDONLY) indicate
343 * dirty bit being set.
344 */
345 WARN_ON(pud_dirty(pud_wrprotect(pud)));
346
Anshuman Khandual399145f2020-06-04 16:47:15 -0700347 WARN_ON(!pud_same(pud, pud));
348 WARN_ON(!pud_young(pud_mkyoung(pud_mkold(pud))));
Anshuman Khandualbb5c47c2021-02-24 12:01:32 -0800349 WARN_ON(!pud_dirty(pud_mkdirty(pud_mkclean(pud))));
350 WARN_ON(pud_dirty(pud_mkclean(pud_mkdirty(pud))));
Anshuman Khandual399145f2020-06-04 16:47:15 -0700351 WARN_ON(!pud_write(pud_mkwrite(pud_wrprotect(pud))));
352 WARN_ON(pud_write(pud_wrprotect(pud_mkwrite(pud))));
353 WARN_ON(pud_young(pud_mkold(pud_mkyoung(pud))));
Anshuman Khandualbb5c47c2021-02-24 12:01:32 -0800354 WARN_ON(pud_dirty(pud_wrprotect(pud_mkclean(pud))));
355 WARN_ON(!pud_dirty(pud_wrprotect(pud_mkdirty(pud))));
Anshuman Khandual399145f2020-06-04 16:47:15 -0700356
Gavin Shan36b77d12021-09-02 14:52:22 -0700357 if (mm_pmd_folded(args->mm))
Anshuman Khandual399145f2020-06-04 16:47:15 -0700358 return;
359
360 /*
361 * A huge page does not point to next level page table
362 * entry. Hence this must qualify as pud_bad().
363 */
364 WARN_ON(!pud_bad(pud_mkhuge(pud)));
365}
Anshuman Khanduala5c3b9f2020-08-06 23:19:20 -0700366
Gavin Shan4cbde032021-09-02 14:52:45 -0700367static void __init pud_advanced_tests(struct pgtable_debug_args *args)
Anshuman Khanduala5c3b9f2020-08-06 23:19:20 -0700368{
Gavin Shan8c5b3a82021-09-02 14:52:54 -0700369 struct page *page;
Gavin Shan4cbde032021-09-02 14:52:45 -0700370 unsigned long vaddr = args->vaddr;
Anshuman Khandual65ac1a62021-06-28 19:35:10 -0700371 pud_t pud;
Anshuman Khanduala5c3b9f2020-08-06 23:19:20 -0700372
373 if (!has_transparent_hugepage())
374 return;
375
Gavin Shan8c5b3a82021-09-02 14:52:54 -0700376 page = (args->pud_pfn != ULONG_MAX) ? pfn_to_page(args->pud_pfn) : NULL;
377 if (!page)
Gavin Shan4cbde032021-09-02 14:52:45 -0700378 return;
379
Gavin Shan8c5b3a82021-09-02 14:52:54 -0700380 /*
381 * flush_dcache_page() is called after set_pud_at() to clear
382 * PG_arch_1 for the page on ARM64. The page flag isn't cleared
383 * when it's released and page allocation check will fail when
384 * the page is allocated again. For architectures other than ARM64,
385 * the unexpected overhead of cache flushing is acceptable.
386 */
Anshuman Khandual6315df42020-08-06 23:19:25 -0700387 pr_debug("Validating PUD advanced\n");
Anshuman Khanduala5c3b9f2020-08-06 23:19:20 -0700388 /* Align the address wrt HPAGE_PUD_SIZE */
Gerald Schaefer04f7ce32021-06-04 20:01:18 -0700389 vaddr &= HPAGE_PUD_MASK;
Anshuman Khanduala5c3b9f2020-08-06 23:19:20 -0700390
Gavin Shan4cbde032021-09-02 14:52:45 -0700391 pud = pfn_pud(args->pud_pfn, args->page_prot);
392 set_pud_at(args->mm, vaddr, args->pudp, pud);
Gavin Shan8c5b3a82021-09-02 14:52:54 -0700393 flush_dcache_page(page);
Gavin Shan4cbde032021-09-02 14:52:45 -0700394 pudp_set_wrprotect(args->mm, vaddr, args->pudp);
395 pud = READ_ONCE(*args->pudp);
Anshuman Khanduala5c3b9f2020-08-06 23:19:20 -0700396 WARN_ON(pud_write(pud));
397
398#ifndef __PAGETABLE_PMD_FOLDED
Gavin Shan4cbde032021-09-02 14:52:45 -0700399 pudp_huge_get_and_clear(args->mm, vaddr, args->pudp);
400 pud = READ_ONCE(*args->pudp);
Anshuman Khanduala5c3b9f2020-08-06 23:19:20 -0700401 WARN_ON(!pud_none(pud));
Anshuman Khanduala5c3b9f2020-08-06 23:19:20 -0700402#endif /* __PAGETABLE_PMD_FOLDED */
Gavin Shan4cbde032021-09-02 14:52:45 -0700403 pud = pfn_pud(args->pud_pfn, args->page_prot);
Anshuman Khanduala5c3b9f2020-08-06 23:19:20 -0700404 pud = pud_wrprotect(pud);
405 pud = pud_mkclean(pud);
Gavin Shan4cbde032021-09-02 14:52:45 -0700406 set_pud_at(args->mm, vaddr, args->pudp, pud);
Gavin Shan8c5b3a82021-09-02 14:52:54 -0700407 flush_dcache_page(page);
Anshuman Khanduala5c3b9f2020-08-06 23:19:20 -0700408 pud = pud_mkwrite(pud);
409 pud = pud_mkdirty(pud);
Gavin Shan4cbde032021-09-02 14:52:45 -0700410 pudp_set_access_flags(args->vma, vaddr, args->pudp, pud, 1);
411 pud = READ_ONCE(*args->pudp);
Anshuman Khanduala5c3b9f2020-08-06 23:19:20 -0700412 WARN_ON(!(pud_write(pud) && pud_dirty(pud)));
413
Aneesh Kumar K.Vc3824e12020-10-15 20:04:46 -0700414#ifndef __PAGETABLE_PMD_FOLDED
Gavin Shan4cbde032021-09-02 14:52:45 -0700415 pudp_huge_get_and_clear_full(args->mm, vaddr, args->pudp, 1);
416 pud = READ_ONCE(*args->pudp);
Aneesh Kumar K.Vc3824e12020-10-15 20:04:46 -0700417 WARN_ON(!pud_none(pud));
418#endif /* __PAGETABLE_PMD_FOLDED */
419
Gavin Shan4cbde032021-09-02 14:52:45 -0700420 pud = pfn_pud(args->pud_pfn, args->page_prot);
Anshuman Khanduala5c3b9f2020-08-06 23:19:20 -0700421 pud = pud_mkyoung(pud);
Gavin Shan4cbde032021-09-02 14:52:45 -0700422 set_pud_at(args->mm, vaddr, args->pudp, pud);
Gavin Shan8c5b3a82021-09-02 14:52:54 -0700423 flush_dcache_page(page);
Gavin Shan4cbde032021-09-02 14:52:45 -0700424 pudp_test_and_clear_young(args->vma, vaddr, args->pudp);
425 pud = READ_ONCE(*args->pudp);
Anshuman Khanduala5c3b9f2020-08-06 23:19:20 -0700426 WARN_ON(pud_young(pud));
Aneesh Kumar K.V13af0502020-10-15 20:04:59 -0700427
Gavin Shan4cbde032021-09-02 14:52:45 -0700428 pudp_huge_get_and_clear(args->mm, vaddr, args->pudp);
Anshuman Khanduala5c3b9f2020-08-06 23:19:20 -0700429}
430
Gavin Shan8983d232021-09-02 14:52:25 -0700431static void __init pud_leaf_tests(struct pgtable_debug_args *args)
Anshuman Khanduala5c3b9f2020-08-06 23:19:20 -0700432{
Anshuman Khandual65ac1a62021-06-28 19:35:10 -0700433 pud_t pud;
434
435 if (!has_transparent_hugepage())
436 return;
Anshuman Khanduala5c3b9f2020-08-06 23:19:20 -0700437
Anshuman Khandual6315df42020-08-06 23:19:25 -0700438 pr_debug("Validating PUD leaf\n");
Gavin Shan8983d232021-09-02 14:52:25 -0700439 pud = pfn_pud(args->fixed_pud_pfn, args->page_prot);
Anshuman Khanduala5c3b9f2020-08-06 23:19:20 -0700440 /*
441 * PUD based THP is a leaf entry.
442 */
443 pud = pud_mkhuge(pud);
444 WARN_ON(!pud_leaf(pud));
445}
Shixin Liu5fe77be2021-06-30 18:47:37 -0700446#else /* !CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD */
Gavin Shan36b77d12021-09-02 14:52:22 -0700447static void __init pud_basic_tests(struct pgtable_debug_args *args, int idx) { }
Gavin Shan4cbde032021-09-02 14:52:45 -0700448static void __init pud_advanced_tests(struct pgtable_debug_args *args) { }
Gavin Shan8983d232021-09-02 14:52:25 -0700449static void __init pud_leaf_tests(struct pgtable_debug_args *args) { }
Shixin Liu5fe77be2021-06-30 18:47:37 -0700450#endif /* CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD */
451#else /* !CONFIG_TRANSPARENT_HUGEPAGE */
Gavin Shan36b77d12021-09-02 14:52:22 -0700452static void __init pmd_basic_tests(struct pgtable_debug_args *args, int idx) { }
453static void __init pud_basic_tests(struct pgtable_debug_args *args, int idx) { }
Gavin Shanc0fe07b2021-09-02 14:52:41 -0700454static void __init pmd_advanced_tests(struct pgtable_debug_args *args) { }
Gavin Shan4cbde032021-09-02 14:52:45 -0700455static void __init pud_advanced_tests(struct pgtable_debug_args *args) { }
Gavin Shan8983d232021-09-02 14:52:25 -0700456static void __init pmd_leaf_tests(struct pgtable_debug_args *args) { }
457static void __init pud_leaf_tests(struct pgtable_debug_args *args) { }
458static void __init pmd_savedwrite_tests(struct pgtable_debug_args *args) { }
Shixin Liu5fe77be2021-06-30 18:47:37 -0700459#endif /* CONFIG_TRANSPARENT_HUGEPAGE */
Anshuman Khanduala5c3b9f2020-08-06 23:19:20 -0700460
Aneesh Kumar K.V85a14462020-10-15 20:04:36 -0700461#ifdef CONFIG_HAVE_ARCH_HUGE_VMAP
Gavin Shanc0fe07b2021-09-02 14:52:41 -0700462static void __init pmd_huge_tests(struct pgtable_debug_args *args)
Shixin Liu5fe77be2021-06-30 18:47:37 -0700463{
464 pmd_t pmd;
465
Gavin Shanc0fe07b2021-09-02 14:52:41 -0700466 if (!arch_vmap_pmd_supported(args->page_prot))
Shixin Liu5fe77be2021-06-30 18:47:37 -0700467 return;
468
469 pr_debug("Validating PMD huge\n");
470 /*
471 * X86 defined pmd_set_huge() verifies that the given
472 * PMD is not a populated non-leaf entry.
473 */
Gavin Shanc0fe07b2021-09-02 14:52:41 -0700474 WRITE_ONCE(*args->pmdp, __pmd(0));
475 WARN_ON(!pmd_set_huge(args->pmdp, __pfn_to_phys(args->fixed_pmd_pfn), args->page_prot));
476 WARN_ON(!pmd_clear_huge(args->pmdp));
477 pmd = READ_ONCE(*args->pmdp);
Shixin Liu5fe77be2021-06-30 18:47:37 -0700478 WARN_ON(!pmd_none(pmd));
479}
480
Gavin Shan4cbde032021-09-02 14:52:45 -0700481static void __init pud_huge_tests(struct pgtable_debug_args *args)
Anshuman Khanduala5c3b9f2020-08-06 23:19:20 -0700482{
483 pud_t pud;
484
Gavin Shan4cbde032021-09-02 14:52:45 -0700485 if (!arch_vmap_pud_supported(args->page_prot))
Anshuman Khanduala5c3b9f2020-08-06 23:19:20 -0700486 return;
Anshuman Khandual6315df42020-08-06 23:19:25 -0700487
488 pr_debug("Validating PUD huge\n");
Anshuman Khanduala5c3b9f2020-08-06 23:19:20 -0700489 /*
490 * X86 defined pud_set_huge() verifies that the given
491 * PUD is not a populated non-leaf entry.
492 */
Gavin Shan4cbde032021-09-02 14:52:45 -0700493 WRITE_ONCE(*args->pudp, __pud(0));
494 WARN_ON(!pud_set_huge(args->pudp, __pfn_to_phys(args->fixed_pud_pfn), args->page_prot));
495 WARN_ON(!pud_clear_huge(args->pudp));
496 pud = READ_ONCE(*args->pudp);
Anshuman Khanduala5c3b9f2020-08-06 23:19:20 -0700497 WARN_ON(!pud_none(pud));
498}
Aneesh Kumar K.V85a14462020-10-15 20:04:36 -0700499#else /* !CONFIG_HAVE_ARCH_HUGE_VMAP */
Gavin Shanc0fe07b2021-09-02 14:52:41 -0700500static void __init pmd_huge_tests(struct pgtable_debug_args *args) { }
Gavin Shan4cbde032021-09-02 14:52:45 -0700501static void __init pud_huge_tests(struct pgtable_debug_args *args) { }
Shixin Liu5fe77be2021-06-30 18:47:37 -0700502#endif /* CONFIG_HAVE_ARCH_HUGE_VMAP */
Anshuman Khandual399145f2020-06-04 16:47:15 -0700503
Gavin Shan36b77d12021-09-02 14:52:22 -0700504static void __init p4d_basic_tests(struct pgtable_debug_args *args)
Anshuman Khandual399145f2020-06-04 16:47:15 -0700505{
506 p4d_t p4d;
507
Anshuman Khandual6315df42020-08-06 23:19:25 -0700508 pr_debug("Validating P4D basic\n");
Anshuman Khandual399145f2020-06-04 16:47:15 -0700509 memset(&p4d, RANDOM_NZVALUE, sizeof(p4d_t));
510 WARN_ON(!p4d_same(p4d, p4d));
511}
512
Gavin Shan36b77d12021-09-02 14:52:22 -0700513static void __init pgd_basic_tests(struct pgtable_debug_args *args)
Anshuman Khandual399145f2020-06-04 16:47:15 -0700514{
515 pgd_t pgd;
516
Anshuman Khandual6315df42020-08-06 23:19:25 -0700517 pr_debug("Validating PGD basic\n");
Anshuman Khandual399145f2020-06-04 16:47:15 -0700518 memset(&pgd, RANDOM_NZVALUE, sizeof(pgd_t));
519 WARN_ON(!pgd_same(pgd, pgd));
520}
521
522#ifndef __PAGETABLE_PUD_FOLDED
Gavin Shan4cbde032021-09-02 14:52:45 -0700523static void __init pud_clear_tests(struct pgtable_debug_args *args)
Anshuman Khandual399145f2020-06-04 16:47:15 -0700524{
Gavin Shan4cbde032021-09-02 14:52:45 -0700525 pud_t pud = READ_ONCE(*args->pudp);
Anshuman Khandual399145f2020-06-04 16:47:15 -0700526
Gavin Shan4cbde032021-09-02 14:52:45 -0700527 if (mm_pmd_folded(args->mm))
Anshuman Khandual399145f2020-06-04 16:47:15 -0700528 return;
529
Anshuman Khandual6315df42020-08-06 23:19:25 -0700530 pr_debug("Validating PUD clear\n");
Anshuman Khandual399145f2020-06-04 16:47:15 -0700531 pud = __pud(pud_val(pud) | RANDOM_ORVALUE);
Gavin Shan4cbde032021-09-02 14:52:45 -0700532 WRITE_ONCE(*args->pudp, pud);
533 pud_clear(args->pudp);
534 pud = READ_ONCE(*args->pudp);
Anshuman Khandual399145f2020-06-04 16:47:15 -0700535 WARN_ON(!pud_none(pud));
536}
537
Gavin Shan4cbde032021-09-02 14:52:45 -0700538static void __init pud_populate_tests(struct pgtable_debug_args *args)
Anshuman Khandual399145f2020-06-04 16:47:15 -0700539{
540 pud_t pud;
541
Gavin Shan4cbde032021-09-02 14:52:45 -0700542 if (mm_pmd_folded(args->mm))
Anshuman Khandual399145f2020-06-04 16:47:15 -0700543 return;
Anshuman Khandual6315df42020-08-06 23:19:25 -0700544
545 pr_debug("Validating PUD populate\n");
Anshuman Khandual399145f2020-06-04 16:47:15 -0700546 /*
547 * This entry points to next level page table page.
548 * Hence this must not qualify as pud_bad().
549 */
Gavin Shan4cbde032021-09-02 14:52:45 -0700550 pud_populate(args->mm, args->pudp, args->start_pmdp);
551 pud = READ_ONCE(*args->pudp);
Anshuman Khandual399145f2020-06-04 16:47:15 -0700552 WARN_ON(pud_bad(pud));
553}
554#else /* !__PAGETABLE_PUD_FOLDED */
Gavin Shan4cbde032021-09-02 14:52:45 -0700555static void __init pud_clear_tests(struct pgtable_debug_args *args) { }
556static void __init pud_populate_tests(struct pgtable_debug_args *args) { }
Anshuman Khandual399145f2020-06-04 16:47:15 -0700557#endif /* PAGETABLE_PUD_FOLDED */
558
559#ifndef __PAGETABLE_P4D_FOLDED
Gavin Shan2f87f8c2021-09-02 14:52:48 -0700560static void __init p4d_clear_tests(struct pgtable_debug_args *args)
Anshuman Khandual399145f2020-06-04 16:47:15 -0700561{
Gavin Shan2f87f8c2021-09-02 14:52:48 -0700562 p4d_t p4d = READ_ONCE(*args->p4dp);
Anshuman Khandual399145f2020-06-04 16:47:15 -0700563
Gavin Shan2f87f8c2021-09-02 14:52:48 -0700564 if (mm_pud_folded(args->mm))
Anshuman Khandual399145f2020-06-04 16:47:15 -0700565 return;
566
Anshuman Khandual6315df42020-08-06 23:19:25 -0700567 pr_debug("Validating P4D clear\n");
Anshuman Khandual399145f2020-06-04 16:47:15 -0700568 p4d = __p4d(p4d_val(p4d) | RANDOM_ORVALUE);
Gavin Shan2f87f8c2021-09-02 14:52:48 -0700569 WRITE_ONCE(*args->p4dp, p4d);
570 p4d_clear(args->p4dp);
571 p4d = READ_ONCE(*args->p4dp);
Anshuman Khandual399145f2020-06-04 16:47:15 -0700572 WARN_ON(!p4d_none(p4d));
573}
574
Gavin Shan2f87f8c2021-09-02 14:52:48 -0700575static void __init p4d_populate_tests(struct pgtable_debug_args *args)
Anshuman Khandual399145f2020-06-04 16:47:15 -0700576{
577 p4d_t p4d;
578
Gavin Shan2f87f8c2021-09-02 14:52:48 -0700579 if (mm_pud_folded(args->mm))
Anshuman Khandual399145f2020-06-04 16:47:15 -0700580 return;
581
Anshuman Khandual6315df42020-08-06 23:19:25 -0700582 pr_debug("Validating P4D populate\n");
Anshuman Khandual399145f2020-06-04 16:47:15 -0700583 /*
584 * This entry points to next level page table page.
585 * Hence this must not qualify as p4d_bad().
586 */
Gavin Shan2f87f8c2021-09-02 14:52:48 -0700587 pud_clear(args->pudp);
588 p4d_clear(args->p4dp);
589 p4d_populate(args->mm, args->p4dp, args->start_pudp);
590 p4d = READ_ONCE(*args->p4dp);
Anshuman Khandual399145f2020-06-04 16:47:15 -0700591 WARN_ON(p4d_bad(p4d));
592}
593
Gavin Shan2f87f8c2021-09-02 14:52:48 -0700594static void __init pgd_clear_tests(struct pgtable_debug_args *args)
Anshuman Khandual399145f2020-06-04 16:47:15 -0700595{
Gavin Shan2f87f8c2021-09-02 14:52:48 -0700596 pgd_t pgd = READ_ONCE(*(args->pgdp));
Anshuman Khandual399145f2020-06-04 16:47:15 -0700597
Gavin Shan2f87f8c2021-09-02 14:52:48 -0700598 if (mm_p4d_folded(args->mm))
Anshuman Khandual399145f2020-06-04 16:47:15 -0700599 return;
600
Anshuman Khandual6315df42020-08-06 23:19:25 -0700601 pr_debug("Validating PGD clear\n");
Anshuman Khandual399145f2020-06-04 16:47:15 -0700602 pgd = __pgd(pgd_val(pgd) | RANDOM_ORVALUE);
Gavin Shan2f87f8c2021-09-02 14:52:48 -0700603 WRITE_ONCE(*args->pgdp, pgd);
604 pgd_clear(args->pgdp);
605 pgd = READ_ONCE(*args->pgdp);
Anshuman Khandual399145f2020-06-04 16:47:15 -0700606 WARN_ON(!pgd_none(pgd));
607}
608
Gavin Shan2f87f8c2021-09-02 14:52:48 -0700609static void __init pgd_populate_tests(struct pgtable_debug_args *args)
Anshuman Khandual399145f2020-06-04 16:47:15 -0700610{
611 pgd_t pgd;
612
Gavin Shan2f87f8c2021-09-02 14:52:48 -0700613 if (mm_p4d_folded(args->mm))
Anshuman Khandual399145f2020-06-04 16:47:15 -0700614 return;
615
Anshuman Khandual6315df42020-08-06 23:19:25 -0700616 pr_debug("Validating PGD populate\n");
Anshuman Khandual399145f2020-06-04 16:47:15 -0700617 /*
618 * This entry points to next level page table page.
619 * Hence this must not qualify as pgd_bad().
620 */
Gavin Shan2f87f8c2021-09-02 14:52:48 -0700621 p4d_clear(args->p4dp);
622 pgd_clear(args->pgdp);
623 pgd_populate(args->mm, args->pgdp, args->start_p4dp);
624 pgd = READ_ONCE(*args->pgdp);
Anshuman Khandual399145f2020-06-04 16:47:15 -0700625 WARN_ON(pgd_bad(pgd));
626}
627#else /* !__PAGETABLE_P4D_FOLDED */
Gavin Shan2f87f8c2021-09-02 14:52:48 -0700628static void __init p4d_clear_tests(struct pgtable_debug_args *args) { }
629static void __init pgd_clear_tests(struct pgtable_debug_args *args) { }
630static void __init p4d_populate_tests(struct pgtable_debug_args *args) { }
631static void __init pgd_populate_tests(struct pgtable_debug_args *args) { }
Anshuman Khandual399145f2020-06-04 16:47:15 -0700632#endif /* PAGETABLE_P4D_FOLDED */
633
Gavin Shan44966c42021-09-02 14:52:38 -0700634static void __init pte_clear_tests(struct pgtable_debug_args *args)
Anshuman Khandual399145f2020-06-04 16:47:15 -0700635{
Gavin Shan8c5b3a82021-09-02 14:52:54 -0700636 struct page *page;
Gavin Shan44966c42021-09-02 14:52:38 -0700637 pte_t pte = pfn_pte(args->pte_pfn, args->page_prot);
638
Gavin Shan8c5b3a82021-09-02 14:52:54 -0700639 page = (args->pte_pfn != ULONG_MAX) ? pfn_to_page(args->pte_pfn) : NULL;
640 if (!page)
Gavin Shan44966c42021-09-02 14:52:38 -0700641 return;
Anshuman Khandual399145f2020-06-04 16:47:15 -0700642
Gavin Shan8c5b3a82021-09-02 14:52:54 -0700643 /*
644 * flush_dcache_page() is called after set_pte_at() to clear
645 * PG_arch_1 for the page on ARM64. The page flag isn't cleared
646 * when it's released and page allocation check will fail when
647 * the page is allocated again. For architectures other than ARM64,
648 * the unexpected overhead of cache flushing is acceptable.
649 */
Anshuman Khandual6315df42020-08-06 23:19:25 -0700650 pr_debug("Validating PTE clear\n");
Aneesh Kumar K.V401035d2020-10-15 20:05:06 -0700651#ifndef CONFIG_RISCV
Anshuman Khandual399145f2020-06-04 16:47:15 -0700652 pte = __pte(pte_val(pte) | RANDOM_ORVALUE);
Aneesh Kumar K.V401035d2020-10-15 20:05:06 -0700653#endif
Gavin Shan44966c42021-09-02 14:52:38 -0700654 set_pte_at(args->mm, args->vaddr, args->ptep, pte);
Gavin Shan8c5b3a82021-09-02 14:52:54 -0700655 flush_dcache_page(page);
Anshuman Khandual399145f2020-06-04 16:47:15 -0700656 barrier();
Pasha Tatashin08d5b292022-01-14 14:06:33 -0800657 ptep_clear(args->mm, args->vaddr, args->ptep);
Gavin Shan44966c42021-09-02 14:52:38 -0700658 pte = ptep_get(args->ptep);
Anshuman Khandual399145f2020-06-04 16:47:15 -0700659 WARN_ON(!pte_none(pte));
660}
661
Gavin Shanc0fe07b2021-09-02 14:52:41 -0700662static void __init pmd_clear_tests(struct pgtable_debug_args *args)
Anshuman Khandual399145f2020-06-04 16:47:15 -0700663{
Gavin Shanc0fe07b2021-09-02 14:52:41 -0700664 pmd_t pmd = READ_ONCE(*args->pmdp);
Anshuman Khandual399145f2020-06-04 16:47:15 -0700665
Anshuman Khandual6315df42020-08-06 23:19:25 -0700666 pr_debug("Validating PMD clear\n");
Anshuman Khandual399145f2020-06-04 16:47:15 -0700667 pmd = __pmd(pmd_val(pmd) | RANDOM_ORVALUE);
Gavin Shanc0fe07b2021-09-02 14:52:41 -0700668 WRITE_ONCE(*args->pmdp, pmd);
669 pmd_clear(args->pmdp);
670 pmd = READ_ONCE(*args->pmdp);
Anshuman Khandual399145f2020-06-04 16:47:15 -0700671 WARN_ON(!pmd_none(pmd));
672}
673
Gavin Shanc0fe07b2021-09-02 14:52:41 -0700674static void __init pmd_populate_tests(struct pgtable_debug_args *args)
Anshuman Khandual399145f2020-06-04 16:47:15 -0700675{
676 pmd_t pmd;
677
Anshuman Khandual6315df42020-08-06 23:19:25 -0700678 pr_debug("Validating PMD populate\n");
Anshuman Khandual399145f2020-06-04 16:47:15 -0700679 /*
680 * This entry points to next level page table page.
681 * Hence this must not qualify as pmd_bad().
682 */
Gavin Shanc0fe07b2021-09-02 14:52:41 -0700683 pmd_populate(args->mm, args->pmdp, args->start_ptep);
684 pmd = READ_ONCE(*args->pmdp);
Anshuman Khandual399145f2020-06-04 16:47:15 -0700685 WARN_ON(pmd_bad(pmd));
686}
687
Gavin Shan8cb183f2021-09-02 14:52:28 -0700688static void __init pte_special_tests(struct pgtable_debug_args *args)
Anshuman Khandual05289402020-08-06 23:19:16 -0700689{
Gavin Shan8cb183f2021-09-02 14:52:28 -0700690 pte_t pte = pfn_pte(args->fixed_pte_pfn, args->page_prot);
Anshuman Khandual05289402020-08-06 23:19:16 -0700691
692 if (!IS_ENABLED(CONFIG_ARCH_HAS_PTE_SPECIAL))
693 return;
694
Anshuman Khandual6315df42020-08-06 23:19:25 -0700695 pr_debug("Validating PTE special\n");
Anshuman Khandual05289402020-08-06 23:19:16 -0700696 WARN_ON(!pte_special(pte_mkspecial(pte)));
697}
698
Gavin Shan8cb183f2021-09-02 14:52:28 -0700699static void __init pte_protnone_tests(struct pgtable_debug_args *args)
Anshuman Khandual05289402020-08-06 23:19:16 -0700700{
Gavin Shan8cb183f2021-09-02 14:52:28 -0700701 pte_t pte = pfn_pte(args->fixed_pte_pfn, args->page_prot_none);
Anshuman Khandual05289402020-08-06 23:19:16 -0700702
703 if (!IS_ENABLED(CONFIG_NUMA_BALANCING))
704 return;
705
Anshuman Khandual6315df42020-08-06 23:19:25 -0700706 pr_debug("Validating PTE protnone\n");
Anshuman Khandual05289402020-08-06 23:19:16 -0700707 WARN_ON(!pte_protnone(pte));
708 WARN_ON(!pte_present(pte));
709}
710
711#ifdef CONFIG_TRANSPARENT_HUGEPAGE
Gavin Shan8cb183f2021-09-02 14:52:28 -0700712static void __init pmd_protnone_tests(struct pgtable_debug_args *args)
Anshuman Khandual05289402020-08-06 23:19:16 -0700713{
Anshuman Khandual65ac1a62021-06-28 19:35:10 -0700714 pmd_t pmd;
Anshuman Khandual05289402020-08-06 23:19:16 -0700715
716 if (!IS_ENABLED(CONFIG_NUMA_BALANCING))
717 return;
718
Anshuman Khandual65ac1a62021-06-28 19:35:10 -0700719 if (!has_transparent_hugepage())
720 return;
721
Anshuman Khandual6315df42020-08-06 23:19:25 -0700722 pr_debug("Validating PMD protnone\n");
Gavin Shan8cb183f2021-09-02 14:52:28 -0700723 pmd = pmd_mkhuge(pfn_pmd(args->fixed_pmd_pfn, args->page_prot_none));
Anshuman Khandual05289402020-08-06 23:19:16 -0700724 WARN_ON(!pmd_protnone(pmd));
725 WARN_ON(!pmd_present(pmd));
726}
727#else /* !CONFIG_TRANSPARENT_HUGEPAGE */
Gavin Shan8cb183f2021-09-02 14:52:28 -0700728static void __init pmd_protnone_tests(struct pgtable_debug_args *args) { }
Anshuman Khandual05289402020-08-06 23:19:16 -0700729#endif /* CONFIG_TRANSPARENT_HUGEPAGE */
730
731#ifdef CONFIG_ARCH_HAS_PTE_DEVMAP
Gavin Shan8cb183f2021-09-02 14:52:28 -0700732static void __init pte_devmap_tests(struct pgtable_debug_args *args)
Anshuman Khandual05289402020-08-06 23:19:16 -0700733{
Gavin Shan8cb183f2021-09-02 14:52:28 -0700734 pte_t pte = pfn_pte(args->fixed_pte_pfn, args->page_prot);
Anshuman Khandual05289402020-08-06 23:19:16 -0700735
Anshuman Khandual6315df42020-08-06 23:19:25 -0700736 pr_debug("Validating PTE devmap\n");
Anshuman Khandual05289402020-08-06 23:19:16 -0700737 WARN_ON(!pte_devmap(pte_mkdevmap(pte)));
738}
739
740#ifdef CONFIG_TRANSPARENT_HUGEPAGE
Gavin Shan8cb183f2021-09-02 14:52:28 -0700741static void __init pmd_devmap_tests(struct pgtable_debug_args *args)
Anshuman Khandual05289402020-08-06 23:19:16 -0700742{
Anshuman Khandual65ac1a62021-06-28 19:35:10 -0700743 pmd_t pmd;
744
745 if (!has_transparent_hugepage())
746 return;
Anshuman Khandual05289402020-08-06 23:19:16 -0700747
Anshuman Khandual6315df42020-08-06 23:19:25 -0700748 pr_debug("Validating PMD devmap\n");
Gavin Shan8cb183f2021-09-02 14:52:28 -0700749 pmd = pfn_pmd(args->fixed_pmd_pfn, args->page_prot);
Anshuman Khandual05289402020-08-06 23:19:16 -0700750 WARN_ON(!pmd_devmap(pmd_mkdevmap(pmd)));
751}
752
753#ifdef CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD
Gavin Shan8cb183f2021-09-02 14:52:28 -0700754static void __init pud_devmap_tests(struct pgtable_debug_args *args)
Anshuman Khandual05289402020-08-06 23:19:16 -0700755{
Anshuman Khandual65ac1a62021-06-28 19:35:10 -0700756 pud_t pud;
757
758 if (!has_transparent_hugepage())
759 return;
Anshuman Khandual05289402020-08-06 23:19:16 -0700760
Anshuman Khandual6315df42020-08-06 23:19:25 -0700761 pr_debug("Validating PUD devmap\n");
Gavin Shan8cb183f2021-09-02 14:52:28 -0700762 pud = pfn_pud(args->fixed_pud_pfn, args->page_prot);
Anshuman Khandual05289402020-08-06 23:19:16 -0700763 WARN_ON(!pud_devmap(pud_mkdevmap(pud)));
764}
765#else /* !CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD */
Gavin Shan8cb183f2021-09-02 14:52:28 -0700766static void __init pud_devmap_tests(struct pgtable_debug_args *args) { }
Anshuman Khandual05289402020-08-06 23:19:16 -0700767#endif /* CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD */
768#else /* CONFIG_TRANSPARENT_HUGEPAGE */
Gavin Shan8cb183f2021-09-02 14:52:28 -0700769static void __init pmd_devmap_tests(struct pgtable_debug_args *args) { }
770static void __init pud_devmap_tests(struct pgtable_debug_args *args) { }
Anshuman Khandual05289402020-08-06 23:19:16 -0700771#endif /* CONFIG_TRANSPARENT_HUGEPAGE */
772#else
Gavin Shan8cb183f2021-09-02 14:52:28 -0700773static void __init pte_devmap_tests(struct pgtable_debug_args *args) { }
774static void __init pmd_devmap_tests(struct pgtable_debug_args *args) { }
775static void __init pud_devmap_tests(struct pgtable_debug_args *args) { }
Anshuman Khandual05289402020-08-06 23:19:16 -0700776#endif /* CONFIG_ARCH_HAS_PTE_DEVMAP */
777
Gavin Shan5f447e82021-09-02 14:52:32 -0700778static void __init pte_soft_dirty_tests(struct pgtable_debug_args *args)
Anshuman Khandual05289402020-08-06 23:19:16 -0700779{
Gavin Shan5f447e82021-09-02 14:52:32 -0700780 pte_t pte = pfn_pte(args->fixed_pte_pfn, args->page_prot);
Anshuman Khandual05289402020-08-06 23:19:16 -0700781
782 if (!IS_ENABLED(CONFIG_MEM_SOFT_DIRTY))
783 return;
784
Anshuman Khandual6315df42020-08-06 23:19:25 -0700785 pr_debug("Validating PTE soft dirty\n");
Anshuman Khandual05289402020-08-06 23:19:16 -0700786 WARN_ON(!pte_soft_dirty(pte_mksoft_dirty(pte)));
787 WARN_ON(pte_soft_dirty(pte_clear_soft_dirty(pte)));
788}
789
Gavin Shan5f447e82021-09-02 14:52:32 -0700790static void __init pte_swap_soft_dirty_tests(struct pgtable_debug_args *args)
Anshuman Khandual05289402020-08-06 23:19:16 -0700791{
Gavin Shan5f447e82021-09-02 14:52:32 -0700792 pte_t pte = pfn_pte(args->fixed_pte_pfn, args->page_prot);
Anshuman Khandual05289402020-08-06 23:19:16 -0700793
794 if (!IS_ENABLED(CONFIG_MEM_SOFT_DIRTY))
795 return;
796
Anshuman Khandual6315df42020-08-06 23:19:25 -0700797 pr_debug("Validating PTE swap soft dirty\n");
Anshuman Khandual05289402020-08-06 23:19:16 -0700798 WARN_ON(!pte_swp_soft_dirty(pte_swp_mksoft_dirty(pte)));
799 WARN_ON(pte_swp_soft_dirty(pte_swp_clear_soft_dirty(pte)));
800}
801
802#ifdef CONFIG_TRANSPARENT_HUGEPAGE
Gavin Shan5f447e82021-09-02 14:52:32 -0700803static void __init pmd_soft_dirty_tests(struct pgtable_debug_args *args)
Anshuman Khandual05289402020-08-06 23:19:16 -0700804{
Anshuman Khandual65ac1a62021-06-28 19:35:10 -0700805 pmd_t pmd;
Anshuman Khandual05289402020-08-06 23:19:16 -0700806
807 if (!IS_ENABLED(CONFIG_MEM_SOFT_DIRTY))
808 return;
809
Anshuman Khandual65ac1a62021-06-28 19:35:10 -0700810 if (!has_transparent_hugepage())
811 return;
812
Anshuman Khandual6315df42020-08-06 23:19:25 -0700813 pr_debug("Validating PMD soft dirty\n");
Gavin Shan5f447e82021-09-02 14:52:32 -0700814 pmd = pfn_pmd(args->fixed_pmd_pfn, args->page_prot);
Anshuman Khandual05289402020-08-06 23:19:16 -0700815 WARN_ON(!pmd_soft_dirty(pmd_mksoft_dirty(pmd)));
816 WARN_ON(pmd_soft_dirty(pmd_clear_soft_dirty(pmd)));
817}
818
Gavin Shan5f447e82021-09-02 14:52:32 -0700819static void __init pmd_swap_soft_dirty_tests(struct pgtable_debug_args *args)
Anshuman Khandual05289402020-08-06 23:19:16 -0700820{
Anshuman Khandual65ac1a62021-06-28 19:35:10 -0700821 pmd_t pmd;
Anshuman Khandual05289402020-08-06 23:19:16 -0700822
823 if (!IS_ENABLED(CONFIG_MEM_SOFT_DIRTY) ||
824 !IS_ENABLED(CONFIG_ARCH_ENABLE_THP_MIGRATION))
825 return;
826
Anshuman Khandual65ac1a62021-06-28 19:35:10 -0700827 if (!has_transparent_hugepage())
828 return;
829
Anshuman Khandual6315df42020-08-06 23:19:25 -0700830 pr_debug("Validating PMD swap soft dirty\n");
Gavin Shan5f447e82021-09-02 14:52:32 -0700831 pmd = pfn_pmd(args->fixed_pmd_pfn, args->page_prot);
Anshuman Khandual05289402020-08-06 23:19:16 -0700832 WARN_ON(!pmd_swp_soft_dirty(pmd_swp_mksoft_dirty(pmd)));
833 WARN_ON(pmd_swp_soft_dirty(pmd_swp_clear_soft_dirty(pmd)));
834}
Shixin Liub593b902021-06-30 18:47:40 -0700835#else /* !CONFIG_TRANSPARENT_HUGEPAGE */
Gavin Shan5f447e82021-09-02 14:52:32 -0700836static void __init pmd_soft_dirty_tests(struct pgtable_debug_args *args) { }
837static void __init pmd_swap_soft_dirty_tests(struct pgtable_debug_args *args) { }
Shixin Liub593b902021-06-30 18:47:40 -0700838#endif /* CONFIG_TRANSPARENT_HUGEPAGE */
Anshuman Khandual05289402020-08-06 23:19:16 -0700839
Gavin Shan5f447e82021-09-02 14:52:32 -0700840static void __init pte_swap_tests(struct pgtable_debug_args *args)
Anshuman Khandual05289402020-08-06 23:19:16 -0700841{
842 swp_entry_t swp;
843 pte_t pte;
844
Anshuman Khandual6315df42020-08-06 23:19:25 -0700845 pr_debug("Validating PTE swap\n");
Gavin Shan5f447e82021-09-02 14:52:32 -0700846 pte = pfn_pte(args->fixed_pte_pfn, args->page_prot);
Anshuman Khandual05289402020-08-06 23:19:16 -0700847 swp = __pte_to_swp_entry(pte);
848 pte = __swp_entry_to_pte(swp);
Gavin Shan5f447e82021-09-02 14:52:32 -0700849 WARN_ON(args->fixed_pte_pfn != pte_pfn(pte));
Anshuman Khandual05289402020-08-06 23:19:16 -0700850}
851
852#ifdef CONFIG_ARCH_ENABLE_THP_MIGRATION
Gavin Shan5f447e82021-09-02 14:52:32 -0700853static void __init pmd_swap_tests(struct pgtable_debug_args *args)
Anshuman Khandual05289402020-08-06 23:19:16 -0700854{
855 swp_entry_t swp;
856 pmd_t pmd;
857
Anshuman Khandual65ac1a62021-06-28 19:35:10 -0700858 if (!has_transparent_hugepage())
859 return;
860
Anshuman Khandual6315df42020-08-06 23:19:25 -0700861 pr_debug("Validating PMD swap\n");
Gavin Shan5f447e82021-09-02 14:52:32 -0700862 pmd = pfn_pmd(args->fixed_pmd_pfn, args->page_prot);
Anshuman Khandual05289402020-08-06 23:19:16 -0700863 swp = __pmd_to_swp_entry(pmd);
864 pmd = __swp_entry_to_pmd(swp);
Gavin Shan5f447e82021-09-02 14:52:32 -0700865 WARN_ON(args->fixed_pmd_pfn != pmd_pfn(pmd));
Anshuman Khandual05289402020-08-06 23:19:16 -0700866}
867#else /* !CONFIG_ARCH_ENABLE_THP_MIGRATION */
Gavin Shan5f447e82021-09-02 14:52:32 -0700868static void __init pmd_swap_tests(struct pgtable_debug_args *args) { }
Anshuman Khandual05289402020-08-06 23:19:16 -0700869#endif /* CONFIG_ARCH_ENABLE_THP_MIGRATION */
870
Gavin Shan4878a882021-09-02 14:52:35 -0700871static void __init swap_migration_tests(struct pgtable_debug_args *args)
Anshuman Khandual05289402020-08-06 23:19:16 -0700872{
873 struct page *page;
874 swp_entry_t swp;
875
876 if (!IS_ENABLED(CONFIG_MIGRATION))
877 return;
Anshuman Khandual6315df42020-08-06 23:19:25 -0700878
Anshuman Khandual05289402020-08-06 23:19:16 -0700879 /*
880 * swap_migration_tests() requires a dedicated page as it needs to
881 * be locked before creating a migration entry from it. Locking the
882 * page that actually maps kernel text ('start_kernel') can be real
Gavin Shan4878a882021-09-02 14:52:35 -0700883 * problematic. Lets use the allocated page explicitly for this
884 * purpose.
Anshuman Khandual05289402020-08-06 23:19:16 -0700885 */
Gavin Shan4878a882021-09-02 14:52:35 -0700886 page = (args->pte_pfn != ULONG_MAX) ? pfn_to_page(args->pte_pfn) : NULL;
887 if (!page)
Anshuman Khandual05289402020-08-06 23:19:16 -0700888 return;
Gavin Shan4878a882021-09-02 14:52:35 -0700889
890 pr_debug("Validating swap migration\n");
Anshuman Khandual05289402020-08-06 23:19:16 -0700891
892 /*
Anshuman Khandual23647612022-01-14 14:05:07 -0800893 * make_[readable|writable]_migration_entry() expects given page to
894 * be locked, otherwise it stumbles upon a BUG_ON().
Anshuman Khandual05289402020-08-06 23:19:16 -0700895 */
896 __SetPageLocked(page);
Alistair Popple4dd845b2021-06-30 18:54:09 -0700897 swp = make_writable_migration_entry(page_to_pfn(page));
Anshuman Khandual05289402020-08-06 23:19:16 -0700898 WARN_ON(!is_migration_entry(swp));
Alistair Popple4dd845b2021-06-30 18:54:09 -0700899 WARN_ON(!is_writable_migration_entry(swp));
Anshuman Khandual05289402020-08-06 23:19:16 -0700900
Alistair Popple4dd845b2021-06-30 18:54:09 -0700901 swp = make_readable_migration_entry(swp_offset(swp));
Anshuman Khandual05289402020-08-06 23:19:16 -0700902 WARN_ON(!is_migration_entry(swp));
Alistair Popple4dd845b2021-06-30 18:54:09 -0700903 WARN_ON(is_writable_migration_entry(swp));
Anshuman Khandual05289402020-08-06 23:19:16 -0700904
Alistair Popple4dd845b2021-06-30 18:54:09 -0700905 swp = make_readable_migration_entry(page_to_pfn(page));
Anshuman Khandual05289402020-08-06 23:19:16 -0700906 WARN_ON(!is_migration_entry(swp));
Alistair Popple4dd845b2021-06-30 18:54:09 -0700907 WARN_ON(is_writable_migration_entry(swp));
Anshuman Khandual05289402020-08-06 23:19:16 -0700908 __ClearPageLocked(page);
Anshuman Khandual05289402020-08-06 23:19:16 -0700909}
910
911#ifdef CONFIG_HUGETLB_PAGE
Gavin Shan36b77d12021-09-02 14:52:22 -0700912static void __init hugetlb_basic_tests(struct pgtable_debug_args *args)
Anshuman Khandual05289402020-08-06 23:19:16 -0700913{
914 struct page *page;
915 pte_t pte;
916
Anshuman Khandual6315df42020-08-06 23:19:25 -0700917 pr_debug("Validating HugeTLB basic\n");
Anshuman Khandual05289402020-08-06 23:19:16 -0700918 /*
919 * Accessing the page associated with the pfn is safe here,
920 * as it was previously derived from a real kernel symbol.
921 */
Gavin Shan36b77d12021-09-02 14:52:22 -0700922 page = pfn_to_page(args->fixed_pmd_pfn);
923 pte = mk_huge_pte(page, args->page_prot);
Anshuman Khandual05289402020-08-06 23:19:16 -0700924
925 WARN_ON(!huge_pte_dirty(huge_pte_mkdirty(pte)));
926 WARN_ON(!huge_pte_write(huge_pte_mkwrite(huge_pte_wrprotect(pte))));
927 WARN_ON(huge_pte_write(huge_pte_wrprotect(huge_pte_mkwrite(pte))));
928
929#ifdef CONFIG_ARCH_WANT_GENERAL_HUGETLB
Gavin Shan36b77d12021-09-02 14:52:22 -0700930 pte = pfn_pte(args->fixed_pmd_pfn, args->page_prot);
Anshuman Khandual05289402020-08-06 23:19:16 -0700931
932 WARN_ON(!pte_huge(pte_mkhuge(pte)));
933#endif /* CONFIG_ARCH_WANT_GENERAL_HUGETLB */
934}
935#else /* !CONFIG_HUGETLB_PAGE */
Gavin Shan36b77d12021-09-02 14:52:22 -0700936static void __init hugetlb_basic_tests(struct pgtable_debug_args *args) { }
Anshuman Khandual05289402020-08-06 23:19:16 -0700937#endif /* CONFIG_HUGETLB_PAGE */
938
939#ifdef CONFIG_TRANSPARENT_HUGEPAGE
Gavin Shan4878a882021-09-02 14:52:35 -0700940static void __init pmd_thp_tests(struct pgtable_debug_args *args)
Anshuman Khandual05289402020-08-06 23:19:16 -0700941{
942 pmd_t pmd;
943
944 if (!has_transparent_hugepage())
945 return;
946
Anshuman Khandual6315df42020-08-06 23:19:25 -0700947 pr_debug("Validating PMD based THP\n");
Anshuman Khandual05289402020-08-06 23:19:16 -0700948 /*
949 * pmd_trans_huge() and pmd_present() must return positive after
950 * MMU invalidation with pmd_mkinvalid(). This behavior is an
951 * optimization for transparent huge page. pmd_trans_huge() must
952 * be true if pmd_page() returns a valid THP to avoid taking the
953 * pmd_lock when others walk over non transhuge pmds (i.e. there
954 * are no THP allocated). Especially when splitting a THP and
955 * removing the present bit from the pmd, pmd_trans_huge() still
956 * needs to return true. pmd_present() should be true whenever
957 * pmd_trans_huge() returns true.
958 */
Gavin Shan4878a882021-09-02 14:52:35 -0700959 pmd = pfn_pmd(args->fixed_pmd_pfn, args->page_prot);
Anshuman Khandual05289402020-08-06 23:19:16 -0700960 WARN_ON(!pmd_trans_huge(pmd_mkhuge(pmd)));
961
962#ifndef __HAVE_ARCH_PMDP_INVALIDATE
963 WARN_ON(!pmd_trans_huge(pmd_mkinvalid(pmd_mkhuge(pmd))));
964 WARN_ON(!pmd_present(pmd_mkinvalid(pmd_mkhuge(pmd))));
965#endif /* __HAVE_ARCH_PMDP_INVALIDATE */
966}
967
968#ifdef CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD
Gavin Shan4878a882021-09-02 14:52:35 -0700969static void __init pud_thp_tests(struct pgtable_debug_args *args)
Anshuman Khandual05289402020-08-06 23:19:16 -0700970{
971 pud_t pud;
972
973 if (!has_transparent_hugepage())
974 return;
975
Anshuman Khandual6315df42020-08-06 23:19:25 -0700976 pr_debug("Validating PUD based THP\n");
Gavin Shan4878a882021-09-02 14:52:35 -0700977 pud = pfn_pud(args->fixed_pud_pfn, args->page_prot);
Anshuman Khandual05289402020-08-06 23:19:16 -0700978 WARN_ON(!pud_trans_huge(pud_mkhuge(pud)));
979
980 /*
981 * pud_mkinvalid() has been dropped for now. Enable back
982 * these tests when it comes back with a modified pud_present().
983 *
984 * WARN_ON(!pud_trans_huge(pud_mkinvalid(pud_mkhuge(pud))));
985 * WARN_ON(!pud_present(pud_mkinvalid(pud_mkhuge(pud))));
986 */
987}
988#else /* !CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD */
Gavin Shan4878a882021-09-02 14:52:35 -0700989static void __init pud_thp_tests(struct pgtable_debug_args *args) { }
Anshuman Khandual05289402020-08-06 23:19:16 -0700990#endif /* CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD */
991#else /* !CONFIG_TRANSPARENT_HUGEPAGE */
Gavin Shan4878a882021-09-02 14:52:35 -0700992static void __init pmd_thp_tests(struct pgtable_debug_args *args) { }
993static void __init pud_thp_tests(struct pgtable_debug_args *args) { }
Anshuman Khandual05289402020-08-06 23:19:16 -0700994#endif /* CONFIG_TRANSPARENT_HUGEPAGE */
995
Anshuman Khandual399145f2020-06-04 16:47:15 -0700996static unsigned long __init get_random_vaddr(void)
997{
998 unsigned long random_vaddr, random_pages, total_user_pages;
999
1000 total_user_pages = (TASK_SIZE - FIRST_USER_ADDRESS) / PAGE_SIZE;
1001
1002 random_pages = get_random_long() % total_user_pages;
1003 random_vaddr = FIRST_USER_ADDRESS + random_pages * PAGE_SIZE;
1004
1005 return random_vaddr;
1006}
1007
Gavin Shan3c9b84f2021-09-02 14:52:19 -07001008static void __init destroy_args(struct pgtable_debug_args *args)
1009{
1010 struct page *page = NULL;
1011
1012 /* Free (huge) page */
1013 if (IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE) &&
1014 IS_ENABLED(CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD) &&
1015 has_transparent_hugepage() &&
1016 args->pud_pfn != ULONG_MAX) {
1017 if (args->is_contiguous_page) {
1018 free_contig_range(args->pud_pfn,
1019 (1 << (HPAGE_PUD_SHIFT - PAGE_SHIFT)));
1020 } else {
1021 page = pfn_to_page(args->pud_pfn);
1022 __free_pages(page, HPAGE_PUD_SHIFT - PAGE_SHIFT);
1023 }
1024
1025 args->pud_pfn = ULONG_MAX;
1026 args->pmd_pfn = ULONG_MAX;
1027 args->pte_pfn = ULONG_MAX;
1028 }
1029
1030 if (IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE) &&
1031 has_transparent_hugepage() &&
1032 args->pmd_pfn != ULONG_MAX) {
1033 if (args->is_contiguous_page) {
1034 free_contig_range(args->pmd_pfn, (1 << HPAGE_PMD_ORDER));
1035 } else {
1036 page = pfn_to_page(args->pmd_pfn);
1037 __free_pages(page, HPAGE_PMD_ORDER);
1038 }
1039
1040 args->pmd_pfn = ULONG_MAX;
1041 args->pte_pfn = ULONG_MAX;
1042 }
1043
1044 if (args->pte_pfn != ULONG_MAX) {
1045 page = pfn_to_page(args->pte_pfn);
1046 __free_pages(page, 0);
1047
1048 args->pte_pfn = ULONG_MAX;
1049 }
1050
1051 /* Free page table entries */
1052 if (args->start_ptep) {
1053 pte_free(args->mm, args->start_ptep);
1054 mm_dec_nr_ptes(args->mm);
1055 }
1056
1057 if (args->start_pmdp) {
1058 pmd_free(args->mm, args->start_pmdp);
1059 mm_dec_nr_pmds(args->mm);
1060 }
1061
1062 if (args->start_pudp) {
1063 pud_free(args->mm, args->start_pudp);
1064 mm_dec_nr_puds(args->mm);
1065 }
1066
1067 if (args->start_p4dp)
1068 p4d_free(args->mm, args->start_p4dp);
1069
1070 /* Free vma and mm struct */
1071 if (args->vma)
1072 vm_area_free(args->vma);
1073
1074 if (args->mm)
1075 mmdrop(args->mm);
1076}
1077
1078static struct page * __init
1079debug_vm_pgtable_alloc_huge_page(struct pgtable_debug_args *args, int order)
1080{
1081 struct page *page = NULL;
1082
1083#ifdef CONFIG_CONTIG_ALLOC
1084 if (order >= MAX_ORDER) {
1085 page = alloc_contig_pages((1 << order), GFP_KERNEL,
1086 first_online_node, NULL);
1087 if (page) {
1088 args->is_contiguous_page = true;
1089 return page;
1090 }
1091 }
1092#endif
1093
1094 if (order < MAX_ORDER)
1095 page = alloc_pages(GFP_KERNEL, order);
1096
1097 return page;
1098}
1099
1100static int __init init_args(struct pgtable_debug_args *args)
1101{
1102 struct page *page = NULL;
1103 phys_addr_t phys;
1104 int ret = 0;
1105
1106 /*
1107 * Initialize the debugging data.
1108 *
Guo Ren87727162021-11-05 13:36:09 -07001109 * protection_map[0] (or even protection_map[8]) will help create
1110 * page table entries with PROT_NONE permission as required for
1111 * pxx_protnone_tests().
Gavin Shan3c9b84f2021-09-02 14:52:19 -07001112 */
1113 memset(args, 0, sizeof(*args));
1114 args->vaddr = get_random_vaddr();
1115 args->page_prot = vm_get_page_prot(VMFLAGS);
Guo Ren87727162021-11-05 13:36:09 -07001116 args->page_prot_none = protection_map[0];
Gavin Shan3c9b84f2021-09-02 14:52:19 -07001117 args->is_contiguous_page = false;
1118 args->pud_pfn = ULONG_MAX;
1119 args->pmd_pfn = ULONG_MAX;
1120 args->pte_pfn = ULONG_MAX;
1121 args->fixed_pgd_pfn = ULONG_MAX;
1122 args->fixed_p4d_pfn = ULONG_MAX;
1123 args->fixed_pud_pfn = ULONG_MAX;
1124 args->fixed_pmd_pfn = ULONG_MAX;
1125 args->fixed_pte_pfn = ULONG_MAX;
1126
1127 /* Allocate mm and vma */
1128 args->mm = mm_alloc();
1129 if (!args->mm) {
1130 pr_err("Failed to allocate mm struct\n");
1131 ret = -ENOMEM;
1132 goto error;
1133 }
1134
1135 args->vma = vm_area_alloc(args->mm);
1136 if (!args->vma) {
1137 pr_err("Failed to allocate vma\n");
1138 ret = -ENOMEM;
1139 goto error;
1140 }
1141
1142 /*
1143 * Allocate page table entries. They will be modified in the tests.
1144 * Lets save the page table entries so that they can be released
1145 * when the tests are completed.
1146 */
1147 args->pgdp = pgd_offset(args->mm, args->vaddr);
1148 args->p4dp = p4d_alloc(args->mm, args->pgdp, args->vaddr);
1149 if (!args->p4dp) {
1150 pr_err("Failed to allocate p4d entries\n");
1151 ret = -ENOMEM;
1152 goto error;
1153 }
1154 args->start_p4dp = p4d_offset(args->pgdp, 0UL);
1155 WARN_ON(!args->start_p4dp);
1156
1157 args->pudp = pud_alloc(args->mm, args->p4dp, args->vaddr);
1158 if (!args->pudp) {
1159 pr_err("Failed to allocate pud entries\n");
1160 ret = -ENOMEM;
1161 goto error;
1162 }
1163 args->start_pudp = pud_offset(args->p4dp, 0UL);
1164 WARN_ON(!args->start_pudp);
1165
1166 args->pmdp = pmd_alloc(args->mm, args->pudp, args->vaddr);
1167 if (!args->pmdp) {
1168 pr_err("Failed to allocate pmd entries\n");
1169 ret = -ENOMEM;
1170 goto error;
1171 }
1172 args->start_pmdp = pmd_offset(args->pudp, 0UL);
1173 WARN_ON(!args->start_pmdp);
1174
1175 if (pte_alloc(args->mm, args->pmdp)) {
1176 pr_err("Failed to allocate pte entries\n");
1177 ret = -ENOMEM;
1178 goto error;
1179 }
1180 args->start_ptep = pmd_pgtable(READ_ONCE(*args->pmdp));
1181 WARN_ON(!args->start_ptep);
1182
1183 /*
1184 * PFN for mapping at PTE level is determined from a standard kernel
1185 * text symbol. But pfns for higher page table levels are derived by
1186 * masking lower bits of this real pfn. These derived pfns might not
1187 * exist on the platform but that does not really matter as pfn_pxx()
1188 * helpers will still create appropriate entries for the test. This
1189 * helps avoid large memory block allocations to be used for mapping
1190 * at higher page table levels in some of the tests.
1191 */
1192 phys = __pa_symbol(&start_kernel);
1193 args->fixed_pgd_pfn = __phys_to_pfn(phys & PGDIR_MASK);
1194 args->fixed_p4d_pfn = __phys_to_pfn(phys & P4D_MASK);
1195 args->fixed_pud_pfn = __phys_to_pfn(phys & PUD_MASK);
1196 args->fixed_pmd_pfn = __phys_to_pfn(phys & PMD_MASK);
1197 args->fixed_pte_pfn = __phys_to_pfn(phys & PAGE_MASK);
1198 WARN_ON(!pfn_valid(args->fixed_pte_pfn));
1199
1200 /*
1201 * Allocate (huge) pages because some of the tests need to access
1202 * the data in the pages. The corresponding tests will be skipped
1203 * if we fail to allocate (huge) pages.
1204 */
1205 if (IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE) &&
1206 IS_ENABLED(CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD) &&
1207 has_transparent_hugepage()) {
1208 page = debug_vm_pgtable_alloc_huge_page(args,
1209 HPAGE_PUD_SHIFT - PAGE_SHIFT);
1210 if (page) {
1211 args->pud_pfn = page_to_pfn(page);
1212 args->pmd_pfn = args->pud_pfn;
1213 args->pte_pfn = args->pud_pfn;
1214 return 0;
1215 }
1216 }
1217
1218 if (IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE) &&
1219 has_transparent_hugepage()) {
1220 page = debug_vm_pgtable_alloc_huge_page(args, HPAGE_PMD_ORDER);
1221 if (page) {
1222 args->pmd_pfn = page_to_pfn(page);
1223 args->pte_pfn = args->pmd_pfn;
1224 return 0;
1225 }
1226 }
1227
1228 page = alloc_pages(GFP_KERNEL, 0);
1229 if (page)
1230 args->pte_pfn = page_to_pfn(page);
1231
1232 return 0;
1233
1234error:
1235 destroy_args(args);
1236 return ret;
1237}
1238
Anshuman Khandual399145f2020-06-04 16:47:15 -07001239static int __init debug_vm_pgtable(void)
1240{
Gavin Shan3c9b84f2021-09-02 14:52:19 -07001241 struct pgtable_debug_args args;
Kees Cookfea11202020-06-03 13:28:45 -07001242 spinlock_t *ptl = NULL;
Gavin Shan3c9b84f2021-09-02 14:52:19 -07001243 int idx, ret;
Anshuman Khandual399145f2020-06-04 16:47:15 -07001244
1245 pr_info("Validating architecture page table helpers\n");
Gavin Shan3c9b84f2021-09-02 14:52:19 -07001246 ret = init_args(&args);
1247 if (ret)
1248 return ret;
1249
Anshuman Khandual2e326c02021-02-24 12:01:36 -08001250 /*
1251 * Iterate over the protection_map[] to make sure that all
1252 * the basic page table transformation validations just hold
1253 * true irrespective of the starting protection value for a
1254 * given page table entry.
1255 */
1256 for (idx = 0; idx < ARRAY_SIZE(protection_map); idx++) {
Gavin Shan36b77d12021-09-02 14:52:22 -07001257 pte_basic_tests(&args, idx);
1258 pmd_basic_tests(&args, idx);
1259 pud_basic_tests(&args, idx);
Anshuman Khandual2e326c02021-02-24 12:01:36 -08001260 }
1261
1262 /*
1263 * Both P4D and PGD level tests are very basic which do not
1264 * involve creating page table entries from the protection
1265 * value and the given pfn. Hence just keep them out from
1266 * the above iteration for now to save some test execution
1267 * time.
1268 */
Gavin Shan36b77d12021-09-02 14:52:22 -07001269 p4d_basic_tests(&args);
1270 pgd_basic_tests(&args);
Anshuman Khandual399145f2020-06-04 16:47:15 -07001271
Gavin Shan8983d232021-09-02 14:52:25 -07001272 pmd_leaf_tests(&args);
1273 pud_leaf_tests(&args);
Anshuman Khanduala5c3b9f2020-08-06 23:19:20 -07001274
Gavin Shan8983d232021-09-02 14:52:25 -07001275 pte_savedwrite_tests(&args);
1276 pmd_savedwrite_tests(&args);
Anshuman Khanduala5c3b9f2020-08-06 23:19:20 -07001277
Gavin Shan8cb183f2021-09-02 14:52:28 -07001278 pte_special_tests(&args);
1279 pte_protnone_tests(&args);
1280 pmd_protnone_tests(&args);
Anshuman Khandual05289402020-08-06 23:19:16 -07001281
Gavin Shan8cb183f2021-09-02 14:52:28 -07001282 pte_devmap_tests(&args);
1283 pmd_devmap_tests(&args);
1284 pud_devmap_tests(&args);
Anshuman Khandual05289402020-08-06 23:19:16 -07001285
Gavin Shan5f447e82021-09-02 14:52:32 -07001286 pte_soft_dirty_tests(&args);
1287 pmd_soft_dirty_tests(&args);
1288 pte_swap_soft_dirty_tests(&args);
1289 pmd_swap_soft_dirty_tests(&args);
Anshuman Khandual05289402020-08-06 23:19:16 -07001290
Gavin Shan5f447e82021-09-02 14:52:32 -07001291 pte_swap_tests(&args);
1292 pmd_swap_tests(&args);
Anshuman Khandual05289402020-08-06 23:19:16 -07001293
Gavin Shan4878a882021-09-02 14:52:35 -07001294 swap_migration_tests(&args);
Anshuman Khandual05289402020-08-06 23:19:16 -07001295
Gavin Shan4878a882021-09-02 14:52:35 -07001296 pmd_thp_tests(&args);
1297 pud_thp_tests(&args);
Anshuman Khandual05289402020-08-06 23:19:16 -07001298
Gavin Shan36b77d12021-09-02 14:52:22 -07001299 hugetlb_basic_tests(&args);
Aneesh Kumar K.Ve8edf0a2020-10-15 20:04:49 -07001300
Aneesh Kumar K.V6f302e272020-10-15 20:04:53 -07001301 /*
1302 * Page table modifying tests. They need to hold
1303 * proper page table lock.
1304 */
Aneesh Kumar K.Ve8edf0a2020-10-15 20:04:49 -07001305
Gavin Shan44966c42021-09-02 14:52:38 -07001306 args.ptep = pte_offset_map_lock(args.mm, args.pmdp, args.vaddr, &ptl);
1307 pte_clear_tests(&args);
1308 pte_advanced_tests(&args);
1309 pte_unmap_unlock(args.ptep, ptl);
Aneesh Kumar K.Ve8edf0a2020-10-15 20:04:49 -07001310
Gavin Shanc0fe07b2021-09-02 14:52:41 -07001311 ptl = pmd_lock(args.mm, args.pmdp);
1312 pmd_clear_tests(&args);
1313 pmd_advanced_tests(&args);
1314 pmd_huge_tests(&args);
1315 pmd_populate_tests(&args);
Aneesh Kumar K.V6f302e272020-10-15 20:04:53 -07001316 spin_unlock(ptl);
1317
Gavin Shan4cbde032021-09-02 14:52:45 -07001318 ptl = pud_lock(args.mm, args.pudp);
1319 pud_clear_tests(&args);
1320 pud_advanced_tests(&args);
1321 pud_huge_tests(&args);
1322 pud_populate_tests(&args);
Aneesh Kumar K.V6f302e272020-10-15 20:04:53 -07001323 spin_unlock(ptl);
1324
Gavin Shan2f87f8c2021-09-02 14:52:48 -07001325 spin_lock(&(args.mm->page_table_lock));
1326 p4d_clear_tests(&args);
1327 pgd_clear_tests(&args);
1328 p4d_populate_tests(&args);
1329 pgd_populate_tests(&args);
1330 spin_unlock(&(args.mm->page_table_lock));
Aneesh Kumar K.Ve8edf0a2020-10-15 20:04:49 -07001331
Gavin Shan3c9b84f2021-09-02 14:52:19 -07001332 destroy_args(&args);
Anshuman Khandual399145f2020-06-04 16:47:15 -07001333 return 0;
1334}
1335late_initcall(debug_vm_pgtable);