Anshuman Khandual | 399145f | 2020-06-04 16:47:15 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
| 2 | /* |
| 3 | * This kernel test validates architecture page table helpers and |
| 4 | * accessors and helps in verifying their continued compliance with |
| 5 | * expected generic MM semantics. |
| 6 | * |
| 7 | * Copyright (C) 2019 ARM Ltd. |
| 8 | * |
| 9 | * Author: Anshuman Khandual <anshuman.khandual@arm.com> |
| 10 | */ |
Anshuman Khandual | 6315df4 | 2020-08-06 23:19:25 -0700 | [diff] [blame] | 11 | #define pr_fmt(fmt) "debug_vm_pgtable: [%-25s]: " fmt, __func__ |
Anshuman Khandual | 399145f | 2020-06-04 16:47:15 -0700 | [diff] [blame] | 12 | |
| 13 | #include <linux/gfp.h> |
| 14 | #include <linux/highmem.h> |
| 15 | #include <linux/hugetlb.h> |
| 16 | #include <linux/kernel.h> |
| 17 | #include <linux/kconfig.h> |
| 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 Khandual | a5c3b9f | 2020-08-06 23:19:20 -0700 | [diff] [blame] | 24 | #include <linux/pgtable.h> |
Anshuman Khandual | 399145f | 2020-06-04 16:47:15 -0700 | [diff] [blame] | 25 | #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.V | 85a1446 | 2020-10-15 20:04:36 -0700 | [diff] [blame] | 31 | #include <linux/io.h> |
Anshuman Khandual | 399145f | 2020-06-04 16:47:15 -0700 | [diff] [blame] | 32 | #include <asm/pgalloc.h> |
Anshuman Khandual | a5c3b9f | 2020-08-06 23:19:20 -0700 | [diff] [blame] | 33 | #include <asm/tlbflush.h> |
Anshuman Khandual | 399145f | 2020-06-04 16:47:15 -0700 | [diff] [blame] | 34 | |
Anshuman Khandual | b1d0000 | 2020-08-06 23:19:28 -0700 | [diff] [blame] | 35 | /* |
| 36 | * Please refer Documentation/vm/arch_pgtable_helpers.rst for the semantics |
| 37 | * expectations that are being validated here. All future changes in here |
| 38 | * or the documentation need to be in sync. |
| 39 | */ |
| 40 | |
Anshuman Khandual | 399145f | 2020-06-04 16:47:15 -0700 | [diff] [blame] | 41 | #define VMFLAGS (VM_READ|VM_WRITE|VM_EXEC) |
| 42 | |
| 43 | /* |
| 44 | * On s390 platform, the lower 4 bits are used to identify given page table |
| 45 | * entry type. But these bits might affect the ability to clear entries with |
| 46 | * pxx_clear() because of how dynamic page table folding works on s390. So |
| 47 | * while loading up the entries do not change the lower 4 bits. It does not |
Aneesh Kumar K.V | cfc5bbc | 2020-10-15 20:04:33 -0700 | [diff] [blame] | 48 | * have affect any other platform. Also avoid the 62nd bit on ppc64 that is |
| 49 | * used to mark a pte entry. |
Anshuman Khandual | 399145f | 2020-06-04 16:47:15 -0700 | [diff] [blame] | 50 | */ |
Aneesh Kumar K.V | cfc5bbc | 2020-10-15 20:04:33 -0700 | [diff] [blame] | 51 | #define S390_SKIP_MASK GENMASK(3, 0) |
| 52 | #if __BITS_PER_LONG == 64 |
| 53 | #define PPC64_SKIP_MASK GENMASK(62, 62) |
| 54 | #else |
| 55 | #define PPC64_SKIP_MASK 0x0 |
| 56 | #endif |
| 57 | #define ARCH_SKIP_MASK (S390_SKIP_MASK | PPC64_SKIP_MASK) |
| 58 | #define RANDOM_ORVALUE (GENMASK(BITS_PER_LONG - 1, 0) & ~ARCH_SKIP_MASK) |
Anshuman Khandual | 399145f | 2020-06-04 16:47:15 -0700 | [diff] [blame] | 59 | #define RANDOM_NZVALUE GENMASK(7, 0) |
| 60 | |
Gavin Shan | 3c9b84f | 2021-09-02 14:52:19 -0700 | [diff] [blame] | 61 | struct pgtable_debug_args { |
| 62 | struct mm_struct *mm; |
| 63 | struct vm_area_struct *vma; |
| 64 | |
| 65 | pgd_t *pgdp; |
| 66 | p4d_t *p4dp; |
| 67 | pud_t *pudp; |
| 68 | pmd_t *pmdp; |
| 69 | pte_t *ptep; |
| 70 | |
| 71 | p4d_t *start_p4dp; |
| 72 | pud_t *start_pudp; |
| 73 | pmd_t *start_pmdp; |
| 74 | pgtable_t start_ptep; |
| 75 | |
| 76 | unsigned long vaddr; |
| 77 | pgprot_t page_prot; |
| 78 | pgprot_t page_prot_none; |
| 79 | |
| 80 | bool is_contiguous_page; |
| 81 | unsigned long pud_pfn; |
| 82 | unsigned long pmd_pfn; |
| 83 | unsigned long pte_pfn; |
| 84 | |
| 85 | unsigned long fixed_pgd_pfn; |
| 86 | unsigned long fixed_p4d_pfn; |
| 87 | unsigned long fixed_pud_pfn; |
| 88 | unsigned long fixed_pmd_pfn; |
| 89 | unsigned long fixed_pte_pfn; |
| 90 | }; |
| 91 | |
Gavin Shan | 36b77d1 | 2021-09-02 14:52:22 -0700 | [diff] [blame] | 92 | static void __init pte_basic_tests(struct pgtable_debug_args *args, int idx) |
Anshuman Khandual | 399145f | 2020-06-04 16:47:15 -0700 | [diff] [blame] | 93 | { |
Anshuman Khandual | 2e326c0 | 2021-02-24 12:01:36 -0800 | [diff] [blame] | 94 | pgprot_t prot = protection_map[idx]; |
Gavin Shan | 36b77d1 | 2021-09-02 14:52:22 -0700 | [diff] [blame] | 95 | pte_t pte = pfn_pte(args->fixed_pte_pfn, prot); |
Anshuman Khandual | 2e326c0 | 2021-02-24 12:01:36 -0800 | [diff] [blame] | 96 | unsigned long val = idx, *ptr = &val; |
Anshuman Khandual | 399145f | 2020-06-04 16:47:15 -0700 | [diff] [blame] | 97 | |
Anshuman Khandual | 2e326c0 | 2021-02-24 12:01:36 -0800 | [diff] [blame] | 98 | pr_debug("Validating PTE basic (%pGv)\n", ptr); |
Anshuman Khandual | bb5c47c | 2021-02-24 12:01:32 -0800 | [diff] [blame] | 99 | |
| 100 | /* |
| 101 | * This test needs to be executed after the given page table entry |
| 102 | * is created with pfn_pte() to make sure that protection_map[idx] |
| 103 | * does not have the dirty bit enabled from the beginning. This is |
| 104 | * important for platforms like arm64 where (!PTE_RDONLY) indicate |
| 105 | * dirty bit being set. |
| 106 | */ |
| 107 | WARN_ON(pte_dirty(pte_wrprotect(pte))); |
| 108 | |
Anshuman Khandual | 399145f | 2020-06-04 16:47:15 -0700 | [diff] [blame] | 109 | WARN_ON(!pte_same(pte, pte)); |
| 110 | WARN_ON(!pte_young(pte_mkyoung(pte_mkold(pte)))); |
| 111 | WARN_ON(!pte_dirty(pte_mkdirty(pte_mkclean(pte)))); |
| 112 | WARN_ON(!pte_write(pte_mkwrite(pte_wrprotect(pte)))); |
| 113 | WARN_ON(pte_young(pte_mkold(pte_mkyoung(pte)))); |
| 114 | WARN_ON(pte_dirty(pte_mkclean(pte_mkdirty(pte)))); |
| 115 | WARN_ON(pte_write(pte_wrprotect(pte_mkwrite(pte)))); |
Anshuman Khandual | bb5c47c | 2021-02-24 12:01:32 -0800 | [diff] [blame] | 116 | WARN_ON(pte_dirty(pte_wrprotect(pte_mkclean(pte)))); |
| 117 | WARN_ON(!pte_dirty(pte_wrprotect(pte_mkdirty(pte)))); |
Anshuman Khandual | 399145f | 2020-06-04 16:47:15 -0700 | [diff] [blame] | 118 | } |
| 119 | |
Gavin Shan | 44966c4 | 2021-09-02 14:52:38 -0700 | [diff] [blame] | 120 | static void __init pte_advanced_tests(struct pgtable_debug_args *args) |
Anshuman Khandual | a5c3b9f | 2020-08-06 23:19:20 -0700 | [diff] [blame] | 121 | { |
Shixin Liu | b593b90 | 2021-06-30 18:47:40 -0700 | [diff] [blame] | 122 | pte_t pte; |
Anshuman Khandual | a5c3b9f | 2020-08-06 23:19:20 -0700 | [diff] [blame] | 123 | |
Aneesh Kumar K.V | c3824e1 | 2020-10-15 20:04:46 -0700 | [diff] [blame] | 124 | /* |
| 125 | * Architectures optimize set_pte_at by avoiding TLB flush. |
| 126 | * This requires set_pte_at to be not used to update an |
| 127 | * existing pte entry. Clear pte before we do set_pte_at |
| 128 | */ |
Gavin Shan | 44966c4 | 2021-09-02 14:52:38 -0700 | [diff] [blame] | 129 | if (args->pte_pfn == ULONG_MAX) |
| 130 | return; |
Aneesh Kumar K.V | c3824e1 | 2020-10-15 20:04:46 -0700 | [diff] [blame] | 131 | |
Anshuman Khandual | 6315df4 | 2020-08-06 23:19:25 -0700 | [diff] [blame] | 132 | pr_debug("Validating PTE advanced\n"); |
Gavin Shan | 44966c4 | 2021-09-02 14:52:38 -0700 | [diff] [blame] | 133 | pte = pfn_pte(args->pte_pfn, args->page_prot); |
| 134 | set_pte_at(args->mm, args->vaddr, args->ptep, pte); |
| 135 | ptep_set_wrprotect(args->mm, args->vaddr, args->ptep); |
| 136 | pte = ptep_get(args->ptep); |
Anshuman Khandual | a5c3b9f | 2020-08-06 23:19:20 -0700 | [diff] [blame] | 137 | WARN_ON(pte_write(pte)); |
Gavin Shan | 44966c4 | 2021-09-02 14:52:38 -0700 | [diff] [blame] | 138 | ptep_get_and_clear(args->mm, args->vaddr, args->ptep); |
| 139 | pte = ptep_get(args->ptep); |
Anshuman Khandual | a5c3b9f | 2020-08-06 23:19:20 -0700 | [diff] [blame] | 140 | WARN_ON(!pte_none(pte)); |
| 141 | |
Gavin Shan | 44966c4 | 2021-09-02 14:52:38 -0700 | [diff] [blame] | 142 | pte = pfn_pte(args->pte_pfn, args->page_prot); |
Anshuman Khandual | a5c3b9f | 2020-08-06 23:19:20 -0700 | [diff] [blame] | 143 | pte = pte_wrprotect(pte); |
| 144 | pte = pte_mkclean(pte); |
Gavin Shan | 44966c4 | 2021-09-02 14:52:38 -0700 | [diff] [blame] | 145 | set_pte_at(args->mm, args->vaddr, args->ptep, pte); |
Anshuman Khandual | a5c3b9f | 2020-08-06 23:19:20 -0700 | [diff] [blame] | 146 | pte = pte_mkwrite(pte); |
| 147 | pte = pte_mkdirty(pte); |
Gavin Shan | 44966c4 | 2021-09-02 14:52:38 -0700 | [diff] [blame] | 148 | ptep_set_access_flags(args->vma, args->vaddr, args->ptep, pte, 1); |
| 149 | pte = ptep_get(args->ptep); |
Anshuman Khandual | a5c3b9f | 2020-08-06 23:19:20 -0700 | [diff] [blame] | 150 | WARN_ON(!(pte_write(pte) && pte_dirty(pte))); |
Gavin Shan | 44966c4 | 2021-09-02 14:52:38 -0700 | [diff] [blame] | 151 | ptep_get_and_clear_full(args->mm, args->vaddr, args->ptep, 1); |
| 152 | pte = ptep_get(args->ptep); |
Anshuman Khandual | a5c3b9f | 2020-08-06 23:19:20 -0700 | [diff] [blame] | 153 | WARN_ON(!pte_none(pte)); |
| 154 | |
Gavin Shan | 44966c4 | 2021-09-02 14:52:38 -0700 | [diff] [blame] | 155 | pte = pfn_pte(args->pte_pfn, args->page_prot); |
Anshuman Khandual | a5c3b9f | 2020-08-06 23:19:20 -0700 | [diff] [blame] | 156 | pte = pte_mkyoung(pte); |
Gavin Shan | 44966c4 | 2021-09-02 14:52:38 -0700 | [diff] [blame] | 157 | set_pte_at(args->mm, args->vaddr, args->ptep, pte); |
| 158 | ptep_test_and_clear_young(args->vma, args->vaddr, args->ptep); |
| 159 | pte = ptep_get(args->ptep); |
Anshuman Khandual | a5c3b9f | 2020-08-06 23:19:20 -0700 | [diff] [blame] | 160 | WARN_ON(pte_young(pte)); |
| 161 | } |
| 162 | |
Gavin Shan | 8983d23 | 2021-09-02 14:52:25 -0700 | [diff] [blame] | 163 | static void __init pte_savedwrite_tests(struct pgtable_debug_args *args) |
Anshuman Khandual | a5c3b9f | 2020-08-06 23:19:20 -0700 | [diff] [blame] | 164 | { |
Gavin Shan | 8983d23 | 2021-09-02 14:52:25 -0700 | [diff] [blame] | 165 | pte_t pte = pfn_pte(args->fixed_pte_pfn, args->page_prot_none); |
Anshuman Khandual | a5c3b9f | 2020-08-06 23:19:20 -0700 | [diff] [blame] | 166 | |
Aneesh Kumar K.V | 4200605 | 2020-10-15 20:04:40 -0700 | [diff] [blame] | 167 | if (!IS_ENABLED(CONFIG_NUMA_BALANCING)) |
| 168 | return; |
| 169 | |
Anshuman Khandual | 6315df4 | 2020-08-06 23:19:25 -0700 | [diff] [blame] | 170 | pr_debug("Validating PTE saved write\n"); |
Anshuman Khandual | a5c3b9f | 2020-08-06 23:19:20 -0700 | [diff] [blame] | 171 | WARN_ON(!pte_savedwrite(pte_mk_savedwrite(pte_clear_savedwrite(pte)))); |
| 172 | WARN_ON(pte_savedwrite(pte_clear_savedwrite(pte_mk_savedwrite(pte)))); |
| 173 | } |
Aneesh Kumar K.V | 4200605 | 2020-10-15 20:04:40 -0700 | [diff] [blame] | 174 | |
Anshuman Khandual | 399145f | 2020-06-04 16:47:15 -0700 | [diff] [blame] | 175 | #ifdef CONFIG_TRANSPARENT_HUGEPAGE |
Gavin Shan | 36b77d1 | 2021-09-02 14:52:22 -0700 | [diff] [blame] | 176 | static void __init pmd_basic_tests(struct pgtable_debug_args *args, int idx) |
Anshuman Khandual | 399145f | 2020-06-04 16:47:15 -0700 | [diff] [blame] | 177 | { |
Anshuman Khandual | 2e326c0 | 2021-02-24 12:01:36 -0800 | [diff] [blame] | 178 | pgprot_t prot = protection_map[idx]; |
Anshuman Khandual | 2e326c0 | 2021-02-24 12:01:36 -0800 | [diff] [blame] | 179 | unsigned long val = idx, *ptr = &val; |
Anshuman Khandual | 65ac1a6 | 2021-06-28 19:35:10 -0700 | [diff] [blame] | 180 | pmd_t pmd; |
Anshuman Khandual | 399145f | 2020-06-04 16:47:15 -0700 | [diff] [blame] | 181 | |
Aneesh Kumar K.V | 787d563 | 2020-06-10 18:41:44 -0700 | [diff] [blame] | 182 | if (!has_transparent_hugepage()) |
| 183 | return; |
| 184 | |
Anshuman Khandual | 2e326c0 | 2021-02-24 12:01:36 -0800 | [diff] [blame] | 185 | pr_debug("Validating PMD basic (%pGv)\n", ptr); |
Gavin Shan | 36b77d1 | 2021-09-02 14:52:22 -0700 | [diff] [blame] | 186 | pmd = pfn_pmd(args->fixed_pmd_pfn, prot); |
Anshuman Khandual | bb5c47c | 2021-02-24 12:01:32 -0800 | [diff] [blame] | 187 | |
| 188 | /* |
| 189 | * This test needs to be executed after the given page table entry |
| 190 | * is created with pfn_pmd() to make sure that protection_map[idx] |
| 191 | * does not have the dirty bit enabled from the beginning. This is |
| 192 | * important for platforms like arm64 where (!PTE_RDONLY) indicate |
| 193 | * dirty bit being set. |
| 194 | */ |
| 195 | WARN_ON(pmd_dirty(pmd_wrprotect(pmd))); |
| 196 | |
| 197 | |
Anshuman Khandual | 399145f | 2020-06-04 16:47:15 -0700 | [diff] [blame] | 198 | WARN_ON(!pmd_same(pmd, pmd)); |
| 199 | WARN_ON(!pmd_young(pmd_mkyoung(pmd_mkold(pmd)))); |
| 200 | WARN_ON(!pmd_dirty(pmd_mkdirty(pmd_mkclean(pmd)))); |
| 201 | WARN_ON(!pmd_write(pmd_mkwrite(pmd_wrprotect(pmd)))); |
| 202 | WARN_ON(pmd_young(pmd_mkold(pmd_mkyoung(pmd)))); |
| 203 | WARN_ON(pmd_dirty(pmd_mkclean(pmd_mkdirty(pmd)))); |
| 204 | WARN_ON(pmd_write(pmd_wrprotect(pmd_mkwrite(pmd)))); |
Anshuman Khandual | bb5c47c | 2021-02-24 12:01:32 -0800 | [diff] [blame] | 205 | WARN_ON(pmd_dirty(pmd_wrprotect(pmd_mkclean(pmd)))); |
| 206 | WARN_ON(!pmd_dirty(pmd_wrprotect(pmd_mkdirty(pmd)))); |
Anshuman Khandual | 399145f | 2020-06-04 16:47:15 -0700 | [diff] [blame] | 207 | /* |
| 208 | * A huge page does not point to next level page table |
| 209 | * entry. Hence this must qualify as pmd_bad(). |
| 210 | */ |
| 211 | WARN_ON(!pmd_bad(pmd_mkhuge(pmd))); |
| 212 | } |
| 213 | |
Gavin Shan | c0fe07b | 2021-09-02 14:52:41 -0700 | [diff] [blame] | 214 | static void __init pmd_advanced_tests(struct pgtable_debug_args *args) |
Anshuman Khandual | a5c3b9f | 2020-08-06 23:19:20 -0700 | [diff] [blame] | 215 | { |
Anshuman Khandual | 65ac1a6 | 2021-06-28 19:35:10 -0700 | [diff] [blame] | 216 | pmd_t pmd; |
Gavin Shan | c0fe07b | 2021-09-02 14:52:41 -0700 | [diff] [blame] | 217 | unsigned long vaddr = args->vaddr; |
Anshuman Khandual | a5c3b9f | 2020-08-06 23:19:20 -0700 | [diff] [blame] | 218 | |
| 219 | if (!has_transparent_hugepage()) |
| 220 | return; |
| 221 | |
Gavin Shan | c0fe07b | 2021-09-02 14:52:41 -0700 | [diff] [blame] | 222 | if (args->pmd_pfn == ULONG_MAX) |
| 223 | return; |
| 224 | |
Anshuman Khandual | 6315df4 | 2020-08-06 23:19:25 -0700 | [diff] [blame] | 225 | pr_debug("Validating PMD advanced\n"); |
Anshuman Khandual | a5c3b9f | 2020-08-06 23:19:20 -0700 | [diff] [blame] | 226 | /* Align the address wrt HPAGE_PMD_SIZE */ |
Gerald Schaefer | 04f7ce3 | 2021-06-04 20:01:18 -0700 | [diff] [blame] | 227 | vaddr &= HPAGE_PMD_MASK; |
Anshuman Khandual | a5c3b9f | 2020-08-06 23:19:20 -0700 | [diff] [blame] | 228 | |
Gavin Shan | c0fe07b | 2021-09-02 14:52:41 -0700 | [diff] [blame] | 229 | pgtable_trans_huge_deposit(args->mm, args->pmdp, args->start_ptep); |
Aneesh Kumar K.V | 87f3498 | 2020-10-15 20:04:56 -0700 | [diff] [blame] | 230 | |
Gavin Shan | c0fe07b | 2021-09-02 14:52:41 -0700 | [diff] [blame] | 231 | pmd = pfn_pmd(args->pmd_pfn, args->page_prot); |
| 232 | set_pmd_at(args->mm, vaddr, args->pmdp, pmd); |
| 233 | pmdp_set_wrprotect(args->mm, vaddr, args->pmdp); |
| 234 | pmd = READ_ONCE(*args->pmdp); |
Anshuman Khandual | a5c3b9f | 2020-08-06 23:19:20 -0700 | [diff] [blame] | 235 | WARN_ON(pmd_write(pmd)); |
Gavin Shan | c0fe07b | 2021-09-02 14:52:41 -0700 | [diff] [blame] | 236 | pmdp_huge_get_and_clear(args->mm, vaddr, args->pmdp); |
| 237 | pmd = READ_ONCE(*args->pmdp); |
Anshuman Khandual | a5c3b9f | 2020-08-06 23:19:20 -0700 | [diff] [blame] | 238 | WARN_ON(!pmd_none(pmd)); |
| 239 | |
Gavin Shan | c0fe07b | 2021-09-02 14:52:41 -0700 | [diff] [blame] | 240 | pmd = pfn_pmd(args->pmd_pfn, args->page_prot); |
Anshuman Khandual | a5c3b9f | 2020-08-06 23:19:20 -0700 | [diff] [blame] | 241 | pmd = pmd_wrprotect(pmd); |
| 242 | pmd = pmd_mkclean(pmd); |
Gavin Shan | c0fe07b | 2021-09-02 14:52:41 -0700 | [diff] [blame] | 243 | set_pmd_at(args->mm, vaddr, args->pmdp, pmd); |
Anshuman Khandual | a5c3b9f | 2020-08-06 23:19:20 -0700 | [diff] [blame] | 244 | pmd = pmd_mkwrite(pmd); |
| 245 | pmd = pmd_mkdirty(pmd); |
Gavin Shan | c0fe07b | 2021-09-02 14:52:41 -0700 | [diff] [blame] | 246 | pmdp_set_access_flags(args->vma, vaddr, args->pmdp, pmd, 1); |
| 247 | pmd = READ_ONCE(*args->pmdp); |
Anshuman Khandual | a5c3b9f | 2020-08-06 23:19:20 -0700 | [diff] [blame] | 248 | WARN_ON(!(pmd_write(pmd) && pmd_dirty(pmd))); |
Gavin Shan | c0fe07b | 2021-09-02 14:52:41 -0700 | [diff] [blame] | 249 | pmdp_huge_get_and_clear_full(args->vma, vaddr, args->pmdp, 1); |
| 250 | pmd = READ_ONCE(*args->pmdp); |
Anshuman Khandual | a5c3b9f | 2020-08-06 23:19:20 -0700 | [diff] [blame] | 251 | WARN_ON(!pmd_none(pmd)); |
| 252 | |
Gavin Shan | c0fe07b | 2021-09-02 14:52:41 -0700 | [diff] [blame] | 253 | pmd = pmd_mkhuge(pfn_pmd(args->pmd_pfn, args->page_prot)); |
Anshuman Khandual | a5c3b9f | 2020-08-06 23:19:20 -0700 | [diff] [blame] | 254 | pmd = pmd_mkyoung(pmd); |
Gavin Shan | c0fe07b | 2021-09-02 14:52:41 -0700 | [diff] [blame] | 255 | set_pmd_at(args->mm, vaddr, args->pmdp, pmd); |
| 256 | pmdp_test_and_clear_young(args->vma, vaddr, args->pmdp); |
| 257 | pmd = READ_ONCE(*args->pmdp); |
Anshuman Khandual | a5c3b9f | 2020-08-06 23:19:20 -0700 | [diff] [blame] | 258 | WARN_ON(pmd_young(pmd)); |
Aneesh Kumar K.V | 87f3498 | 2020-10-15 20:04:56 -0700 | [diff] [blame] | 259 | |
Aneesh Kumar K.V | 13af050 | 2020-10-15 20:04:59 -0700 | [diff] [blame] | 260 | /* Clear the pte entries */ |
Gavin Shan | c0fe07b | 2021-09-02 14:52:41 -0700 | [diff] [blame] | 261 | pmdp_huge_get_and_clear(args->mm, vaddr, args->pmdp); |
| 262 | pgtable_trans_huge_withdraw(args->mm, args->pmdp); |
Anshuman Khandual | a5c3b9f | 2020-08-06 23:19:20 -0700 | [diff] [blame] | 263 | } |
| 264 | |
Gavin Shan | 8983d23 | 2021-09-02 14:52:25 -0700 | [diff] [blame] | 265 | static void __init pmd_leaf_tests(struct pgtable_debug_args *args) |
Anshuman Khandual | a5c3b9f | 2020-08-06 23:19:20 -0700 | [diff] [blame] | 266 | { |
Anshuman Khandual | 65ac1a6 | 2021-06-28 19:35:10 -0700 | [diff] [blame] | 267 | pmd_t pmd; |
| 268 | |
| 269 | if (!has_transparent_hugepage()) |
| 270 | return; |
Anshuman Khandual | a5c3b9f | 2020-08-06 23:19:20 -0700 | [diff] [blame] | 271 | |
Anshuman Khandual | 6315df4 | 2020-08-06 23:19:25 -0700 | [diff] [blame] | 272 | pr_debug("Validating PMD leaf\n"); |
Gavin Shan | 8983d23 | 2021-09-02 14:52:25 -0700 | [diff] [blame] | 273 | pmd = pfn_pmd(args->fixed_pmd_pfn, args->page_prot); |
Anshuman Khandual | 65ac1a6 | 2021-06-28 19:35:10 -0700 | [diff] [blame] | 274 | |
Anshuman Khandual | a5c3b9f | 2020-08-06 23:19:20 -0700 | [diff] [blame] | 275 | /* |
| 276 | * PMD based THP is a leaf entry. |
| 277 | */ |
| 278 | pmd = pmd_mkhuge(pmd); |
| 279 | WARN_ON(!pmd_leaf(pmd)); |
| 280 | } |
| 281 | |
Gavin Shan | 8983d23 | 2021-09-02 14:52:25 -0700 | [diff] [blame] | 282 | static void __init pmd_savedwrite_tests(struct pgtable_debug_args *args) |
Anshuman Khandual | a5c3b9f | 2020-08-06 23:19:20 -0700 | [diff] [blame] | 283 | { |
Anshuman Khandual | 65ac1a6 | 2021-06-28 19:35:10 -0700 | [diff] [blame] | 284 | pmd_t pmd; |
Anshuman Khandual | a5c3b9f | 2020-08-06 23:19:20 -0700 | [diff] [blame] | 285 | |
Aneesh Kumar K.V | 4200605 | 2020-10-15 20:04:40 -0700 | [diff] [blame] | 286 | if (!IS_ENABLED(CONFIG_NUMA_BALANCING)) |
| 287 | return; |
| 288 | |
Anshuman Khandual | 65ac1a6 | 2021-06-28 19:35:10 -0700 | [diff] [blame] | 289 | if (!has_transparent_hugepage()) |
| 290 | return; |
| 291 | |
Anshuman Khandual | 6315df4 | 2020-08-06 23:19:25 -0700 | [diff] [blame] | 292 | pr_debug("Validating PMD saved write\n"); |
Gavin Shan | 8983d23 | 2021-09-02 14:52:25 -0700 | [diff] [blame] | 293 | pmd = pfn_pmd(args->fixed_pmd_pfn, args->page_prot_none); |
Anshuman Khandual | a5c3b9f | 2020-08-06 23:19:20 -0700 | [diff] [blame] | 294 | WARN_ON(!pmd_savedwrite(pmd_mk_savedwrite(pmd_clear_savedwrite(pmd)))); |
| 295 | WARN_ON(pmd_savedwrite(pmd_clear_savedwrite(pmd_mk_savedwrite(pmd)))); |
| 296 | } |
| 297 | |
Anshuman Khandual | 399145f | 2020-06-04 16:47:15 -0700 | [diff] [blame] | 298 | #ifdef CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD |
Gavin Shan | 36b77d1 | 2021-09-02 14:52:22 -0700 | [diff] [blame] | 299 | static void __init pud_basic_tests(struct pgtable_debug_args *args, int idx) |
Anshuman Khandual | 399145f | 2020-06-04 16:47:15 -0700 | [diff] [blame] | 300 | { |
Anshuman Khandual | 2e326c0 | 2021-02-24 12:01:36 -0800 | [diff] [blame] | 301 | pgprot_t prot = protection_map[idx]; |
Anshuman Khandual | 2e326c0 | 2021-02-24 12:01:36 -0800 | [diff] [blame] | 302 | unsigned long val = idx, *ptr = &val; |
Anshuman Khandual | 65ac1a6 | 2021-06-28 19:35:10 -0700 | [diff] [blame] | 303 | pud_t pud; |
Anshuman Khandual | 399145f | 2020-06-04 16:47:15 -0700 | [diff] [blame] | 304 | |
Aneesh Kumar K.V | 787d563 | 2020-06-10 18:41:44 -0700 | [diff] [blame] | 305 | if (!has_transparent_hugepage()) |
| 306 | return; |
| 307 | |
Anshuman Khandual | 2e326c0 | 2021-02-24 12:01:36 -0800 | [diff] [blame] | 308 | pr_debug("Validating PUD basic (%pGv)\n", ptr); |
Gavin Shan | 36b77d1 | 2021-09-02 14:52:22 -0700 | [diff] [blame] | 309 | pud = pfn_pud(args->fixed_pud_pfn, prot); |
Anshuman Khandual | bb5c47c | 2021-02-24 12:01:32 -0800 | [diff] [blame] | 310 | |
| 311 | /* |
| 312 | * This test needs to be executed after the given page table entry |
| 313 | * is created with pfn_pud() to make sure that protection_map[idx] |
| 314 | * does not have the dirty bit enabled from the beginning. This is |
| 315 | * important for platforms like arm64 where (!PTE_RDONLY) indicate |
| 316 | * dirty bit being set. |
| 317 | */ |
| 318 | WARN_ON(pud_dirty(pud_wrprotect(pud))); |
| 319 | |
Anshuman Khandual | 399145f | 2020-06-04 16:47:15 -0700 | [diff] [blame] | 320 | WARN_ON(!pud_same(pud, pud)); |
| 321 | WARN_ON(!pud_young(pud_mkyoung(pud_mkold(pud)))); |
Anshuman Khandual | bb5c47c | 2021-02-24 12:01:32 -0800 | [diff] [blame] | 322 | WARN_ON(!pud_dirty(pud_mkdirty(pud_mkclean(pud)))); |
| 323 | WARN_ON(pud_dirty(pud_mkclean(pud_mkdirty(pud)))); |
Anshuman Khandual | 399145f | 2020-06-04 16:47:15 -0700 | [diff] [blame] | 324 | WARN_ON(!pud_write(pud_mkwrite(pud_wrprotect(pud)))); |
| 325 | WARN_ON(pud_write(pud_wrprotect(pud_mkwrite(pud)))); |
| 326 | WARN_ON(pud_young(pud_mkold(pud_mkyoung(pud)))); |
Anshuman Khandual | bb5c47c | 2021-02-24 12:01:32 -0800 | [diff] [blame] | 327 | WARN_ON(pud_dirty(pud_wrprotect(pud_mkclean(pud)))); |
| 328 | WARN_ON(!pud_dirty(pud_wrprotect(pud_mkdirty(pud)))); |
Anshuman Khandual | 399145f | 2020-06-04 16:47:15 -0700 | [diff] [blame] | 329 | |
Gavin Shan | 36b77d1 | 2021-09-02 14:52:22 -0700 | [diff] [blame] | 330 | if (mm_pmd_folded(args->mm)) |
Anshuman Khandual | 399145f | 2020-06-04 16:47:15 -0700 | [diff] [blame] | 331 | return; |
| 332 | |
| 333 | /* |
| 334 | * A huge page does not point to next level page table |
| 335 | * entry. Hence this must qualify as pud_bad(). |
| 336 | */ |
| 337 | WARN_ON(!pud_bad(pud_mkhuge(pud))); |
| 338 | } |
Anshuman Khandual | a5c3b9f | 2020-08-06 23:19:20 -0700 | [diff] [blame] | 339 | |
Gavin Shan | 4cbde03 | 2021-09-02 14:52:45 -0700 | [diff] [blame^] | 340 | static void __init pud_advanced_tests(struct pgtable_debug_args *args) |
Anshuman Khandual | a5c3b9f | 2020-08-06 23:19:20 -0700 | [diff] [blame] | 341 | { |
Gavin Shan | 4cbde03 | 2021-09-02 14:52:45 -0700 | [diff] [blame^] | 342 | unsigned long vaddr = args->vaddr; |
Anshuman Khandual | 65ac1a6 | 2021-06-28 19:35:10 -0700 | [diff] [blame] | 343 | pud_t pud; |
Anshuman Khandual | a5c3b9f | 2020-08-06 23:19:20 -0700 | [diff] [blame] | 344 | |
| 345 | if (!has_transparent_hugepage()) |
| 346 | return; |
| 347 | |
Gavin Shan | 4cbde03 | 2021-09-02 14:52:45 -0700 | [diff] [blame^] | 348 | if (args->pud_pfn == ULONG_MAX) |
| 349 | return; |
| 350 | |
Anshuman Khandual | 6315df4 | 2020-08-06 23:19:25 -0700 | [diff] [blame] | 351 | pr_debug("Validating PUD advanced\n"); |
Anshuman Khandual | a5c3b9f | 2020-08-06 23:19:20 -0700 | [diff] [blame] | 352 | /* Align the address wrt HPAGE_PUD_SIZE */ |
Gerald Schaefer | 04f7ce3 | 2021-06-04 20:01:18 -0700 | [diff] [blame] | 353 | vaddr &= HPAGE_PUD_MASK; |
Anshuman Khandual | a5c3b9f | 2020-08-06 23:19:20 -0700 | [diff] [blame] | 354 | |
Gavin Shan | 4cbde03 | 2021-09-02 14:52:45 -0700 | [diff] [blame^] | 355 | pud = pfn_pud(args->pud_pfn, args->page_prot); |
| 356 | set_pud_at(args->mm, vaddr, args->pudp, pud); |
| 357 | pudp_set_wrprotect(args->mm, vaddr, args->pudp); |
| 358 | pud = READ_ONCE(*args->pudp); |
Anshuman Khandual | a5c3b9f | 2020-08-06 23:19:20 -0700 | [diff] [blame] | 359 | WARN_ON(pud_write(pud)); |
| 360 | |
| 361 | #ifndef __PAGETABLE_PMD_FOLDED |
Gavin Shan | 4cbde03 | 2021-09-02 14:52:45 -0700 | [diff] [blame^] | 362 | pudp_huge_get_and_clear(args->mm, vaddr, args->pudp); |
| 363 | pud = READ_ONCE(*args->pudp); |
Anshuman Khandual | a5c3b9f | 2020-08-06 23:19:20 -0700 | [diff] [blame] | 364 | WARN_ON(!pud_none(pud)); |
Anshuman Khandual | a5c3b9f | 2020-08-06 23:19:20 -0700 | [diff] [blame] | 365 | #endif /* __PAGETABLE_PMD_FOLDED */ |
Gavin Shan | 4cbde03 | 2021-09-02 14:52:45 -0700 | [diff] [blame^] | 366 | pud = pfn_pud(args->pud_pfn, args->page_prot); |
Anshuman Khandual | a5c3b9f | 2020-08-06 23:19:20 -0700 | [diff] [blame] | 367 | pud = pud_wrprotect(pud); |
| 368 | pud = pud_mkclean(pud); |
Gavin Shan | 4cbde03 | 2021-09-02 14:52:45 -0700 | [diff] [blame^] | 369 | set_pud_at(args->mm, vaddr, args->pudp, pud); |
Anshuman Khandual | a5c3b9f | 2020-08-06 23:19:20 -0700 | [diff] [blame] | 370 | pud = pud_mkwrite(pud); |
| 371 | pud = pud_mkdirty(pud); |
Gavin Shan | 4cbde03 | 2021-09-02 14:52:45 -0700 | [diff] [blame^] | 372 | pudp_set_access_flags(args->vma, vaddr, args->pudp, pud, 1); |
| 373 | pud = READ_ONCE(*args->pudp); |
Anshuman Khandual | a5c3b9f | 2020-08-06 23:19:20 -0700 | [diff] [blame] | 374 | WARN_ON(!(pud_write(pud) && pud_dirty(pud))); |
| 375 | |
Aneesh Kumar K.V | c3824e1 | 2020-10-15 20:04:46 -0700 | [diff] [blame] | 376 | #ifndef __PAGETABLE_PMD_FOLDED |
Gavin Shan | 4cbde03 | 2021-09-02 14:52:45 -0700 | [diff] [blame^] | 377 | pudp_huge_get_and_clear_full(args->mm, vaddr, args->pudp, 1); |
| 378 | pud = READ_ONCE(*args->pudp); |
Aneesh Kumar K.V | c3824e1 | 2020-10-15 20:04:46 -0700 | [diff] [blame] | 379 | WARN_ON(!pud_none(pud)); |
| 380 | #endif /* __PAGETABLE_PMD_FOLDED */ |
| 381 | |
Gavin Shan | 4cbde03 | 2021-09-02 14:52:45 -0700 | [diff] [blame^] | 382 | pud = pfn_pud(args->pud_pfn, args->page_prot); |
Anshuman Khandual | a5c3b9f | 2020-08-06 23:19:20 -0700 | [diff] [blame] | 383 | pud = pud_mkyoung(pud); |
Gavin Shan | 4cbde03 | 2021-09-02 14:52:45 -0700 | [diff] [blame^] | 384 | set_pud_at(args->mm, vaddr, args->pudp, pud); |
| 385 | pudp_test_and_clear_young(args->vma, vaddr, args->pudp); |
| 386 | pud = READ_ONCE(*args->pudp); |
Anshuman Khandual | a5c3b9f | 2020-08-06 23:19:20 -0700 | [diff] [blame] | 387 | WARN_ON(pud_young(pud)); |
Aneesh Kumar K.V | 13af050 | 2020-10-15 20:04:59 -0700 | [diff] [blame] | 388 | |
Gavin Shan | 4cbde03 | 2021-09-02 14:52:45 -0700 | [diff] [blame^] | 389 | pudp_huge_get_and_clear(args->mm, vaddr, args->pudp); |
Anshuman Khandual | a5c3b9f | 2020-08-06 23:19:20 -0700 | [diff] [blame] | 390 | } |
| 391 | |
Gavin Shan | 8983d23 | 2021-09-02 14:52:25 -0700 | [diff] [blame] | 392 | static void __init pud_leaf_tests(struct pgtable_debug_args *args) |
Anshuman Khandual | a5c3b9f | 2020-08-06 23:19:20 -0700 | [diff] [blame] | 393 | { |
Anshuman Khandual | 65ac1a6 | 2021-06-28 19:35:10 -0700 | [diff] [blame] | 394 | pud_t pud; |
| 395 | |
| 396 | if (!has_transparent_hugepage()) |
| 397 | return; |
Anshuman Khandual | a5c3b9f | 2020-08-06 23:19:20 -0700 | [diff] [blame] | 398 | |
Anshuman Khandual | 6315df4 | 2020-08-06 23:19:25 -0700 | [diff] [blame] | 399 | pr_debug("Validating PUD leaf\n"); |
Gavin Shan | 8983d23 | 2021-09-02 14:52:25 -0700 | [diff] [blame] | 400 | pud = pfn_pud(args->fixed_pud_pfn, args->page_prot); |
Anshuman Khandual | a5c3b9f | 2020-08-06 23:19:20 -0700 | [diff] [blame] | 401 | /* |
| 402 | * PUD based THP is a leaf entry. |
| 403 | */ |
| 404 | pud = pud_mkhuge(pud); |
| 405 | WARN_ON(!pud_leaf(pud)); |
| 406 | } |
Shixin Liu | 5fe77be | 2021-06-30 18:47:37 -0700 | [diff] [blame] | 407 | #else /* !CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD */ |
Gavin Shan | 36b77d1 | 2021-09-02 14:52:22 -0700 | [diff] [blame] | 408 | static void __init pud_basic_tests(struct pgtable_debug_args *args, int idx) { } |
Gavin Shan | 4cbde03 | 2021-09-02 14:52:45 -0700 | [diff] [blame^] | 409 | static void __init pud_advanced_tests(struct pgtable_debug_args *args) { } |
Gavin Shan | 8983d23 | 2021-09-02 14:52:25 -0700 | [diff] [blame] | 410 | static void __init pud_leaf_tests(struct pgtable_debug_args *args) { } |
Shixin Liu | 5fe77be | 2021-06-30 18:47:37 -0700 | [diff] [blame] | 411 | #endif /* CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD */ |
| 412 | #else /* !CONFIG_TRANSPARENT_HUGEPAGE */ |
Gavin Shan | 36b77d1 | 2021-09-02 14:52:22 -0700 | [diff] [blame] | 413 | static void __init pmd_basic_tests(struct pgtable_debug_args *args, int idx) { } |
| 414 | static void __init pud_basic_tests(struct pgtable_debug_args *args, int idx) { } |
Gavin Shan | c0fe07b | 2021-09-02 14:52:41 -0700 | [diff] [blame] | 415 | static void __init pmd_advanced_tests(struct pgtable_debug_args *args) { } |
Gavin Shan | 4cbde03 | 2021-09-02 14:52:45 -0700 | [diff] [blame^] | 416 | static void __init pud_advanced_tests(struct pgtable_debug_args *args) { } |
Gavin Shan | 8983d23 | 2021-09-02 14:52:25 -0700 | [diff] [blame] | 417 | static void __init pmd_leaf_tests(struct pgtable_debug_args *args) { } |
| 418 | static void __init pud_leaf_tests(struct pgtable_debug_args *args) { } |
| 419 | static void __init pmd_savedwrite_tests(struct pgtable_debug_args *args) { } |
Shixin Liu | 5fe77be | 2021-06-30 18:47:37 -0700 | [diff] [blame] | 420 | #endif /* CONFIG_TRANSPARENT_HUGEPAGE */ |
Anshuman Khandual | a5c3b9f | 2020-08-06 23:19:20 -0700 | [diff] [blame] | 421 | |
Aneesh Kumar K.V | 85a1446 | 2020-10-15 20:04:36 -0700 | [diff] [blame] | 422 | #ifdef CONFIG_HAVE_ARCH_HUGE_VMAP |
Gavin Shan | c0fe07b | 2021-09-02 14:52:41 -0700 | [diff] [blame] | 423 | static void __init pmd_huge_tests(struct pgtable_debug_args *args) |
Shixin Liu | 5fe77be | 2021-06-30 18:47:37 -0700 | [diff] [blame] | 424 | { |
| 425 | pmd_t pmd; |
| 426 | |
Gavin Shan | c0fe07b | 2021-09-02 14:52:41 -0700 | [diff] [blame] | 427 | if (!arch_vmap_pmd_supported(args->page_prot)) |
Shixin Liu | 5fe77be | 2021-06-30 18:47:37 -0700 | [diff] [blame] | 428 | return; |
| 429 | |
| 430 | pr_debug("Validating PMD huge\n"); |
| 431 | /* |
| 432 | * X86 defined pmd_set_huge() verifies that the given |
| 433 | * PMD is not a populated non-leaf entry. |
| 434 | */ |
Gavin Shan | c0fe07b | 2021-09-02 14:52:41 -0700 | [diff] [blame] | 435 | WRITE_ONCE(*args->pmdp, __pmd(0)); |
| 436 | WARN_ON(!pmd_set_huge(args->pmdp, __pfn_to_phys(args->fixed_pmd_pfn), args->page_prot)); |
| 437 | WARN_ON(!pmd_clear_huge(args->pmdp)); |
| 438 | pmd = READ_ONCE(*args->pmdp); |
Shixin Liu | 5fe77be | 2021-06-30 18:47:37 -0700 | [diff] [blame] | 439 | WARN_ON(!pmd_none(pmd)); |
| 440 | } |
| 441 | |
Gavin Shan | 4cbde03 | 2021-09-02 14:52:45 -0700 | [diff] [blame^] | 442 | static void __init pud_huge_tests(struct pgtable_debug_args *args) |
Anshuman Khandual | a5c3b9f | 2020-08-06 23:19:20 -0700 | [diff] [blame] | 443 | { |
| 444 | pud_t pud; |
| 445 | |
Gavin Shan | 4cbde03 | 2021-09-02 14:52:45 -0700 | [diff] [blame^] | 446 | if (!arch_vmap_pud_supported(args->page_prot)) |
Anshuman Khandual | a5c3b9f | 2020-08-06 23:19:20 -0700 | [diff] [blame] | 447 | return; |
Anshuman Khandual | 6315df4 | 2020-08-06 23:19:25 -0700 | [diff] [blame] | 448 | |
| 449 | pr_debug("Validating PUD huge\n"); |
Anshuman Khandual | a5c3b9f | 2020-08-06 23:19:20 -0700 | [diff] [blame] | 450 | /* |
| 451 | * X86 defined pud_set_huge() verifies that the given |
| 452 | * PUD is not a populated non-leaf entry. |
| 453 | */ |
Gavin Shan | 4cbde03 | 2021-09-02 14:52:45 -0700 | [diff] [blame^] | 454 | WRITE_ONCE(*args->pudp, __pud(0)); |
| 455 | WARN_ON(!pud_set_huge(args->pudp, __pfn_to_phys(args->fixed_pud_pfn), args->page_prot)); |
| 456 | WARN_ON(!pud_clear_huge(args->pudp)); |
| 457 | pud = READ_ONCE(*args->pudp); |
Anshuman Khandual | a5c3b9f | 2020-08-06 23:19:20 -0700 | [diff] [blame] | 458 | WARN_ON(!pud_none(pud)); |
| 459 | } |
Aneesh Kumar K.V | 85a1446 | 2020-10-15 20:04:36 -0700 | [diff] [blame] | 460 | #else /* !CONFIG_HAVE_ARCH_HUGE_VMAP */ |
Gavin Shan | c0fe07b | 2021-09-02 14:52:41 -0700 | [diff] [blame] | 461 | static void __init pmd_huge_tests(struct pgtable_debug_args *args) { } |
Gavin Shan | 4cbde03 | 2021-09-02 14:52:45 -0700 | [diff] [blame^] | 462 | static void __init pud_huge_tests(struct pgtable_debug_args *args) { } |
Shixin Liu | 5fe77be | 2021-06-30 18:47:37 -0700 | [diff] [blame] | 463 | #endif /* CONFIG_HAVE_ARCH_HUGE_VMAP */ |
Anshuman Khandual | 399145f | 2020-06-04 16:47:15 -0700 | [diff] [blame] | 464 | |
Gavin Shan | 36b77d1 | 2021-09-02 14:52:22 -0700 | [diff] [blame] | 465 | static void __init p4d_basic_tests(struct pgtable_debug_args *args) |
Anshuman Khandual | 399145f | 2020-06-04 16:47:15 -0700 | [diff] [blame] | 466 | { |
| 467 | p4d_t p4d; |
| 468 | |
Anshuman Khandual | 6315df4 | 2020-08-06 23:19:25 -0700 | [diff] [blame] | 469 | pr_debug("Validating P4D basic\n"); |
Anshuman Khandual | 399145f | 2020-06-04 16:47:15 -0700 | [diff] [blame] | 470 | memset(&p4d, RANDOM_NZVALUE, sizeof(p4d_t)); |
| 471 | WARN_ON(!p4d_same(p4d, p4d)); |
| 472 | } |
| 473 | |
Gavin Shan | 36b77d1 | 2021-09-02 14:52:22 -0700 | [diff] [blame] | 474 | static void __init pgd_basic_tests(struct pgtable_debug_args *args) |
Anshuman Khandual | 399145f | 2020-06-04 16:47:15 -0700 | [diff] [blame] | 475 | { |
| 476 | pgd_t pgd; |
| 477 | |
Anshuman Khandual | 6315df4 | 2020-08-06 23:19:25 -0700 | [diff] [blame] | 478 | pr_debug("Validating PGD basic\n"); |
Anshuman Khandual | 399145f | 2020-06-04 16:47:15 -0700 | [diff] [blame] | 479 | memset(&pgd, RANDOM_NZVALUE, sizeof(pgd_t)); |
| 480 | WARN_ON(!pgd_same(pgd, pgd)); |
| 481 | } |
| 482 | |
| 483 | #ifndef __PAGETABLE_PUD_FOLDED |
Gavin Shan | 4cbde03 | 2021-09-02 14:52:45 -0700 | [diff] [blame^] | 484 | static void __init pud_clear_tests(struct pgtable_debug_args *args) |
Anshuman Khandual | 399145f | 2020-06-04 16:47:15 -0700 | [diff] [blame] | 485 | { |
Gavin Shan | 4cbde03 | 2021-09-02 14:52:45 -0700 | [diff] [blame^] | 486 | pud_t pud = READ_ONCE(*args->pudp); |
Anshuman Khandual | 399145f | 2020-06-04 16:47:15 -0700 | [diff] [blame] | 487 | |
Gavin Shan | 4cbde03 | 2021-09-02 14:52:45 -0700 | [diff] [blame^] | 488 | if (mm_pmd_folded(args->mm)) |
Anshuman Khandual | 399145f | 2020-06-04 16:47:15 -0700 | [diff] [blame] | 489 | return; |
| 490 | |
Anshuman Khandual | 6315df4 | 2020-08-06 23:19:25 -0700 | [diff] [blame] | 491 | pr_debug("Validating PUD clear\n"); |
Anshuman Khandual | 399145f | 2020-06-04 16:47:15 -0700 | [diff] [blame] | 492 | pud = __pud(pud_val(pud) | RANDOM_ORVALUE); |
Gavin Shan | 4cbde03 | 2021-09-02 14:52:45 -0700 | [diff] [blame^] | 493 | WRITE_ONCE(*args->pudp, pud); |
| 494 | pud_clear(args->pudp); |
| 495 | pud = READ_ONCE(*args->pudp); |
Anshuman Khandual | 399145f | 2020-06-04 16:47:15 -0700 | [diff] [blame] | 496 | WARN_ON(!pud_none(pud)); |
| 497 | } |
| 498 | |
Gavin Shan | 4cbde03 | 2021-09-02 14:52:45 -0700 | [diff] [blame^] | 499 | static void __init pud_populate_tests(struct pgtable_debug_args *args) |
Anshuman Khandual | 399145f | 2020-06-04 16:47:15 -0700 | [diff] [blame] | 500 | { |
| 501 | pud_t pud; |
| 502 | |
Gavin Shan | 4cbde03 | 2021-09-02 14:52:45 -0700 | [diff] [blame^] | 503 | if (mm_pmd_folded(args->mm)) |
Anshuman Khandual | 399145f | 2020-06-04 16:47:15 -0700 | [diff] [blame] | 504 | return; |
Anshuman Khandual | 6315df4 | 2020-08-06 23:19:25 -0700 | [diff] [blame] | 505 | |
| 506 | pr_debug("Validating PUD populate\n"); |
Anshuman Khandual | 399145f | 2020-06-04 16:47:15 -0700 | [diff] [blame] | 507 | /* |
| 508 | * This entry points to next level page table page. |
| 509 | * Hence this must not qualify as pud_bad(). |
| 510 | */ |
Gavin Shan | 4cbde03 | 2021-09-02 14:52:45 -0700 | [diff] [blame^] | 511 | pud_populate(args->mm, args->pudp, args->start_pmdp); |
| 512 | pud = READ_ONCE(*args->pudp); |
Anshuman Khandual | 399145f | 2020-06-04 16:47:15 -0700 | [diff] [blame] | 513 | WARN_ON(pud_bad(pud)); |
| 514 | } |
| 515 | #else /* !__PAGETABLE_PUD_FOLDED */ |
Gavin Shan | 4cbde03 | 2021-09-02 14:52:45 -0700 | [diff] [blame^] | 516 | static void __init pud_clear_tests(struct pgtable_debug_args *args) { } |
| 517 | static void __init pud_populate_tests(struct pgtable_debug_args *args) { } |
Anshuman Khandual | 399145f | 2020-06-04 16:47:15 -0700 | [diff] [blame] | 518 | #endif /* PAGETABLE_PUD_FOLDED */ |
| 519 | |
| 520 | #ifndef __PAGETABLE_P4D_FOLDED |
| 521 | static void __init p4d_clear_tests(struct mm_struct *mm, p4d_t *p4dp) |
| 522 | { |
| 523 | p4d_t p4d = READ_ONCE(*p4dp); |
| 524 | |
| 525 | if (mm_pud_folded(mm)) |
| 526 | return; |
| 527 | |
Anshuman Khandual | 6315df4 | 2020-08-06 23:19:25 -0700 | [diff] [blame] | 528 | pr_debug("Validating P4D clear\n"); |
Anshuman Khandual | 399145f | 2020-06-04 16:47:15 -0700 | [diff] [blame] | 529 | p4d = __p4d(p4d_val(p4d) | RANDOM_ORVALUE); |
| 530 | WRITE_ONCE(*p4dp, p4d); |
| 531 | p4d_clear(p4dp); |
| 532 | p4d = READ_ONCE(*p4dp); |
| 533 | WARN_ON(!p4d_none(p4d)); |
| 534 | } |
| 535 | |
| 536 | static void __init p4d_populate_tests(struct mm_struct *mm, p4d_t *p4dp, |
| 537 | pud_t *pudp) |
| 538 | { |
| 539 | p4d_t p4d; |
| 540 | |
| 541 | if (mm_pud_folded(mm)) |
| 542 | return; |
| 543 | |
Anshuman Khandual | 6315df4 | 2020-08-06 23:19:25 -0700 | [diff] [blame] | 544 | pr_debug("Validating P4D populate\n"); |
Anshuman Khandual | 399145f | 2020-06-04 16:47:15 -0700 | [diff] [blame] | 545 | /* |
| 546 | * This entry points to next level page table page. |
| 547 | * Hence this must not qualify as p4d_bad(). |
| 548 | */ |
| 549 | pud_clear(pudp); |
| 550 | p4d_clear(p4dp); |
| 551 | p4d_populate(mm, p4dp, pudp); |
| 552 | p4d = READ_ONCE(*p4dp); |
| 553 | WARN_ON(p4d_bad(p4d)); |
| 554 | } |
| 555 | |
| 556 | static void __init pgd_clear_tests(struct mm_struct *mm, pgd_t *pgdp) |
| 557 | { |
| 558 | pgd_t pgd = READ_ONCE(*pgdp); |
| 559 | |
| 560 | if (mm_p4d_folded(mm)) |
| 561 | return; |
| 562 | |
Anshuman Khandual | 6315df4 | 2020-08-06 23:19:25 -0700 | [diff] [blame] | 563 | pr_debug("Validating PGD clear\n"); |
Anshuman Khandual | 399145f | 2020-06-04 16:47:15 -0700 | [diff] [blame] | 564 | pgd = __pgd(pgd_val(pgd) | RANDOM_ORVALUE); |
| 565 | WRITE_ONCE(*pgdp, pgd); |
| 566 | pgd_clear(pgdp); |
| 567 | pgd = READ_ONCE(*pgdp); |
| 568 | WARN_ON(!pgd_none(pgd)); |
| 569 | } |
| 570 | |
| 571 | static void __init pgd_populate_tests(struct mm_struct *mm, pgd_t *pgdp, |
| 572 | p4d_t *p4dp) |
| 573 | { |
| 574 | pgd_t pgd; |
| 575 | |
| 576 | if (mm_p4d_folded(mm)) |
| 577 | return; |
| 578 | |
Anshuman Khandual | 6315df4 | 2020-08-06 23:19:25 -0700 | [diff] [blame] | 579 | pr_debug("Validating PGD populate\n"); |
Anshuman Khandual | 399145f | 2020-06-04 16:47:15 -0700 | [diff] [blame] | 580 | /* |
| 581 | * This entry points to next level page table page. |
| 582 | * Hence this must not qualify as pgd_bad(). |
| 583 | */ |
| 584 | p4d_clear(p4dp); |
| 585 | pgd_clear(pgdp); |
| 586 | pgd_populate(mm, pgdp, p4dp); |
| 587 | pgd = READ_ONCE(*pgdp); |
| 588 | WARN_ON(pgd_bad(pgd)); |
| 589 | } |
| 590 | #else /* !__PAGETABLE_P4D_FOLDED */ |
| 591 | static void __init p4d_clear_tests(struct mm_struct *mm, p4d_t *p4dp) { } |
| 592 | static void __init pgd_clear_tests(struct mm_struct *mm, pgd_t *pgdp) { } |
| 593 | static void __init p4d_populate_tests(struct mm_struct *mm, p4d_t *p4dp, |
| 594 | pud_t *pudp) |
| 595 | { |
| 596 | } |
| 597 | static void __init pgd_populate_tests(struct mm_struct *mm, pgd_t *pgdp, |
| 598 | p4d_t *p4dp) |
| 599 | { |
| 600 | } |
| 601 | #endif /* PAGETABLE_P4D_FOLDED */ |
| 602 | |
Gavin Shan | 44966c4 | 2021-09-02 14:52:38 -0700 | [diff] [blame] | 603 | static void __init pte_clear_tests(struct pgtable_debug_args *args) |
Anshuman Khandual | 399145f | 2020-06-04 16:47:15 -0700 | [diff] [blame] | 604 | { |
Gavin Shan | 44966c4 | 2021-09-02 14:52:38 -0700 | [diff] [blame] | 605 | pte_t pte = pfn_pte(args->pte_pfn, args->page_prot); |
| 606 | |
| 607 | if (args->pte_pfn == ULONG_MAX) |
| 608 | return; |
Anshuman Khandual | 399145f | 2020-06-04 16:47:15 -0700 | [diff] [blame] | 609 | |
Anshuman Khandual | 6315df4 | 2020-08-06 23:19:25 -0700 | [diff] [blame] | 610 | pr_debug("Validating PTE clear\n"); |
Aneesh Kumar K.V | 401035d | 2020-10-15 20:05:06 -0700 | [diff] [blame] | 611 | #ifndef CONFIG_RISCV |
Anshuman Khandual | 399145f | 2020-06-04 16:47:15 -0700 | [diff] [blame] | 612 | pte = __pte(pte_val(pte) | RANDOM_ORVALUE); |
Aneesh Kumar K.V | 401035d | 2020-10-15 20:05:06 -0700 | [diff] [blame] | 613 | #endif |
Gavin Shan | 44966c4 | 2021-09-02 14:52:38 -0700 | [diff] [blame] | 614 | set_pte_at(args->mm, args->vaddr, args->ptep, pte); |
Anshuman Khandual | 399145f | 2020-06-04 16:47:15 -0700 | [diff] [blame] | 615 | barrier(); |
Gavin Shan | 44966c4 | 2021-09-02 14:52:38 -0700 | [diff] [blame] | 616 | pte_clear(args->mm, args->vaddr, args->ptep); |
| 617 | pte = ptep_get(args->ptep); |
Anshuman Khandual | 399145f | 2020-06-04 16:47:15 -0700 | [diff] [blame] | 618 | WARN_ON(!pte_none(pte)); |
| 619 | } |
| 620 | |
Gavin Shan | c0fe07b | 2021-09-02 14:52:41 -0700 | [diff] [blame] | 621 | static void __init pmd_clear_tests(struct pgtable_debug_args *args) |
Anshuman Khandual | 399145f | 2020-06-04 16:47:15 -0700 | [diff] [blame] | 622 | { |
Gavin Shan | c0fe07b | 2021-09-02 14:52:41 -0700 | [diff] [blame] | 623 | pmd_t pmd = READ_ONCE(*args->pmdp); |
Anshuman Khandual | 399145f | 2020-06-04 16:47:15 -0700 | [diff] [blame] | 624 | |
Anshuman Khandual | 6315df4 | 2020-08-06 23:19:25 -0700 | [diff] [blame] | 625 | pr_debug("Validating PMD clear\n"); |
Anshuman Khandual | 399145f | 2020-06-04 16:47:15 -0700 | [diff] [blame] | 626 | pmd = __pmd(pmd_val(pmd) | RANDOM_ORVALUE); |
Gavin Shan | c0fe07b | 2021-09-02 14:52:41 -0700 | [diff] [blame] | 627 | WRITE_ONCE(*args->pmdp, pmd); |
| 628 | pmd_clear(args->pmdp); |
| 629 | pmd = READ_ONCE(*args->pmdp); |
Anshuman Khandual | 399145f | 2020-06-04 16:47:15 -0700 | [diff] [blame] | 630 | WARN_ON(!pmd_none(pmd)); |
| 631 | } |
| 632 | |
Gavin Shan | c0fe07b | 2021-09-02 14:52:41 -0700 | [diff] [blame] | 633 | static void __init pmd_populate_tests(struct pgtable_debug_args *args) |
Anshuman Khandual | 399145f | 2020-06-04 16:47:15 -0700 | [diff] [blame] | 634 | { |
| 635 | pmd_t pmd; |
| 636 | |
Anshuman Khandual | 6315df4 | 2020-08-06 23:19:25 -0700 | [diff] [blame] | 637 | pr_debug("Validating PMD populate\n"); |
Anshuman Khandual | 399145f | 2020-06-04 16:47:15 -0700 | [diff] [blame] | 638 | /* |
| 639 | * This entry points to next level page table page. |
| 640 | * Hence this must not qualify as pmd_bad(). |
| 641 | */ |
Gavin Shan | c0fe07b | 2021-09-02 14:52:41 -0700 | [diff] [blame] | 642 | pmd_populate(args->mm, args->pmdp, args->start_ptep); |
| 643 | pmd = READ_ONCE(*args->pmdp); |
Anshuman Khandual | 399145f | 2020-06-04 16:47:15 -0700 | [diff] [blame] | 644 | WARN_ON(pmd_bad(pmd)); |
| 645 | } |
| 646 | |
Gavin Shan | 8cb183f | 2021-09-02 14:52:28 -0700 | [diff] [blame] | 647 | static void __init pte_special_tests(struct pgtable_debug_args *args) |
Anshuman Khandual | 0528940 | 2020-08-06 23:19:16 -0700 | [diff] [blame] | 648 | { |
Gavin Shan | 8cb183f | 2021-09-02 14:52:28 -0700 | [diff] [blame] | 649 | pte_t pte = pfn_pte(args->fixed_pte_pfn, args->page_prot); |
Anshuman Khandual | 0528940 | 2020-08-06 23:19:16 -0700 | [diff] [blame] | 650 | |
| 651 | if (!IS_ENABLED(CONFIG_ARCH_HAS_PTE_SPECIAL)) |
| 652 | return; |
| 653 | |
Anshuman Khandual | 6315df4 | 2020-08-06 23:19:25 -0700 | [diff] [blame] | 654 | pr_debug("Validating PTE special\n"); |
Anshuman Khandual | 0528940 | 2020-08-06 23:19:16 -0700 | [diff] [blame] | 655 | WARN_ON(!pte_special(pte_mkspecial(pte))); |
| 656 | } |
| 657 | |
Gavin Shan | 8cb183f | 2021-09-02 14:52:28 -0700 | [diff] [blame] | 658 | static void __init pte_protnone_tests(struct pgtable_debug_args *args) |
Anshuman Khandual | 0528940 | 2020-08-06 23:19:16 -0700 | [diff] [blame] | 659 | { |
Gavin Shan | 8cb183f | 2021-09-02 14:52:28 -0700 | [diff] [blame] | 660 | pte_t pte = pfn_pte(args->fixed_pte_pfn, args->page_prot_none); |
Anshuman Khandual | 0528940 | 2020-08-06 23:19:16 -0700 | [diff] [blame] | 661 | |
| 662 | if (!IS_ENABLED(CONFIG_NUMA_BALANCING)) |
| 663 | return; |
| 664 | |
Anshuman Khandual | 6315df4 | 2020-08-06 23:19:25 -0700 | [diff] [blame] | 665 | pr_debug("Validating PTE protnone\n"); |
Anshuman Khandual | 0528940 | 2020-08-06 23:19:16 -0700 | [diff] [blame] | 666 | WARN_ON(!pte_protnone(pte)); |
| 667 | WARN_ON(!pte_present(pte)); |
| 668 | } |
| 669 | |
| 670 | #ifdef CONFIG_TRANSPARENT_HUGEPAGE |
Gavin Shan | 8cb183f | 2021-09-02 14:52:28 -0700 | [diff] [blame] | 671 | static void __init pmd_protnone_tests(struct pgtable_debug_args *args) |
Anshuman Khandual | 0528940 | 2020-08-06 23:19:16 -0700 | [diff] [blame] | 672 | { |
Anshuman Khandual | 65ac1a6 | 2021-06-28 19:35:10 -0700 | [diff] [blame] | 673 | pmd_t pmd; |
Anshuman Khandual | 0528940 | 2020-08-06 23:19:16 -0700 | [diff] [blame] | 674 | |
| 675 | if (!IS_ENABLED(CONFIG_NUMA_BALANCING)) |
| 676 | return; |
| 677 | |
Anshuman Khandual | 65ac1a6 | 2021-06-28 19:35:10 -0700 | [diff] [blame] | 678 | if (!has_transparent_hugepage()) |
| 679 | return; |
| 680 | |
Anshuman Khandual | 6315df4 | 2020-08-06 23:19:25 -0700 | [diff] [blame] | 681 | pr_debug("Validating PMD protnone\n"); |
Gavin Shan | 8cb183f | 2021-09-02 14:52:28 -0700 | [diff] [blame] | 682 | pmd = pmd_mkhuge(pfn_pmd(args->fixed_pmd_pfn, args->page_prot_none)); |
Anshuman Khandual | 0528940 | 2020-08-06 23:19:16 -0700 | [diff] [blame] | 683 | WARN_ON(!pmd_protnone(pmd)); |
| 684 | WARN_ON(!pmd_present(pmd)); |
| 685 | } |
| 686 | #else /* !CONFIG_TRANSPARENT_HUGEPAGE */ |
Gavin Shan | 8cb183f | 2021-09-02 14:52:28 -0700 | [diff] [blame] | 687 | static void __init pmd_protnone_tests(struct pgtable_debug_args *args) { } |
Anshuman Khandual | 0528940 | 2020-08-06 23:19:16 -0700 | [diff] [blame] | 688 | #endif /* CONFIG_TRANSPARENT_HUGEPAGE */ |
| 689 | |
| 690 | #ifdef CONFIG_ARCH_HAS_PTE_DEVMAP |
Gavin Shan | 8cb183f | 2021-09-02 14:52:28 -0700 | [diff] [blame] | 691 | static void __init pte_devmap_tests(struct pgtable_debug_args *args) |
Anshuman Khandual | 0528940 | 2020-08-06 23:19:16 -0700 | [diff] [blame] | 692 | { |
Gavin Shan | 8cb183f | 2021-09-02 14:52:28 -0700 | [diff] [blame] | 693 | pte_t pte = pfn_pte(args->fixed_pte_pfn, args->page_prot); |
Anshuman Khandual | 0528940 | 2020-08-06 23:19:16 -0700 | [diff] [blame] | 694 | |
Anshuman Khandual | 6315df4 | 2020-08-06 23:19:25 -0700 | [diff] [blame] | 695 | pr_debug("Validating PTE devmap\n"); |
Anshuman Khandual | 0528940 | 2020-08-06 23:19:16 -0700 | [diff] [blame] | 696 | WARN_ON(!pte_devmap(pte_mkdevmap(pte))); |
| 697 | } |
| 698 | |
| 699 | #ifdef CONFIG_TRANSPARENT_HUGEPAGE |
Gavin Shan | 8cb183f | 2021-09-02 14:52:28 -0700 | [diff] [blame] | 700 | static void __init pmd_devmap_tests(struct pgtable_debug_args *args) |
Anshuman Khandual | 0528940 | 2020-08-06 23:19:16 -0700 | [diff] [blame] | 701 | { |
Anshuman Khandual | 65ac1a6 | 2021-06-28 19:35:10 -0700 | [diff] [blame] | 702 | pmd_t pmd; |
| 703 | |
| 704 | if (!has_transparent_hugepage()) |
| 705 | return; |
Anshuman Khandual | 0528940 | 2020-08-06 23:19:16 -0700 | [diff] [blame] | 706 | |
Anshuman Khandual | 6315df4 | 2020-08-06 23:19:25 -0700 | [diff] [blame] | 707 | pr_debug("Validating PMD devmap\n"); |
Gavin Shan | 8cb183f | 2021-09-02 14:52:28 -0700 | [diff] [blame] | 708 | pmd = pfn_pmd(args->fixed_pmd_pfn, args->page_prot); |
Anshuman Khandual | 0528940 | 2020-08-06 23:19:16 -0700 | [diff] [blame] | 709 | WARN_ON(!pmd_devmap(pmd_mkdevmap(pmd))); |
| 710 | } |
| 711 | |
| 712 | #ifdef CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD |
Gavin Shan | 8cb183f | 2021-09-02 14:52:28 -0700 | [diff] [blame] | 713 | static void __init pud_devmap_tests(struct pgtable_debug_args *args) |
Anshuman Khandual | 0528940 | 2020-08-06 23:19:16 -0700 | [diff] [blame] | 714 | { |
Anshuman Khandual | 65ac1a6 | 2021-06-28 19:35:10 -0700 | [diff] [blame] | 715 | pud_t pud; |
| 716 | |
| 717 | if (!has_transparent_hugepage()) |
| 718 | return; |
Anshuman Khandual | 0528940 | 2020-08-06 23:19:16 -0700 | [diff] [blame] | 719 | |
Anshuman Khandual | 6315df4 | 2020-08-06 23:19:25 -0700 | [diff] [blame] | 720 | pr_debug("Validating PUD devmap\n"); |
Gavin Shan | 8cb183f | 2021-09-02 14:52:28 -0700 | [diff] [blame] | 721 | pud = pfn_pud(args->fixed_pud_pfn, args->page_prot); |
Anshuman Khandual | 0528940 | 2020-08-06 23:19:16 -0700 | [diff] [blame] | 722 | WARN_ON(!pud_devmap(pud_mkdevmap(pud))); |
| 723 | } |
| 724 | #else /* !CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD */ |
Gavin Shan | 8cb183f | 2021-09-02 14:52:28 -0700 | [diff] [blame] | 725 | static void __init pud_devmap_tests(struct pgtable_debug_args *args) { } |
Anshuman Khandual | 0528940 | 2020-08-06 23:19:16 -0700 | [diff] [blame] | 726 | #endif /* CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD */ |
| 727 | #else /* CONFIG_TRANSPARENT_HUGEPAGE */ |
Gavin Shan | 8cb183f | 2021-09-02 14:52:28 -0700 | [diff] [blame] | 728 | static void __init pmd_devmap_tests(struct pgtable_debug_args *args) { } |
| 729 | static void __init pud_devmap_tests(struct pgtable_debug_args *args) { } |
Anshuman Khandual | 0528940 | 2020-08-06 23:19:16 -0700 | [diff] [blame] | 730 | #endif /* CONFIG_TRANSPARENT_HUGEPAGE */ |
| 731 | #else |
Gavin Shan | 8cb183f | 2021-09-02 14:52:28 -0700 | [diff] [blame] | 732 | static void __init pte_devmap_tests(struct pgtable_debug_args *args) { } |
| 733 | static void __init pmd_devmap_tests(struct pgtable_debug_args *args) { } |
| 734 | static void __init pud_devmap_tests(struct pgtable_debug_args *args) { } |
Anshuman Khandual | 0528940 | 2020-08-06 23:19:16 -0700 | [diff] [blame] | 735 | #endif /* CONFIG_ARCH_HAS_PTE_DEVMAP */ |
| 736 | |
Gavin Shan | 5f447e8 | 2021-09-02 14:52:32 -0700 | [diff] [blame] | 737 | static void __init pte_soft_dirty_tests(struct pgtable_debug_args *args) |
Anshuman Khandual | 0528940 | 2020-08-06 23:19:16 -0700 | [diff] [blame] | 738 | { |
Gavin Shan | 5f447e8 | 2021-09-02 14:52:32 -0700 | [diff] [blame] | 739 | pte_t pte = pfn_pte(args->fixed_pte_pfn, args->page_prot); |
Anshuman Khandual | 0528940 | 2020-08-06 23:19:16 -0700 | [diff] [blame] | 740 | |
| 741 | if (!IS_ENABLED(CONFIG_MEM_SOFT_DIRTY)) |
| 742 | return; |
| 743 | |
Anshuman Khandual | 6315df4 | 2020-08-06 23:19:25 -0700 | [diff] [blame] | 744 | pr_debug("Validating PTE soft dirty\n"); |
Anshuman Khandual | 0528940 | 2020-08-06 23:19:16 -0700 | [diff] [blame] | 745 | WARN_ON(!pte_soft_dirty(pte_mksoft_dirty(pte))); |
| 746 | WARN_ON(pte_soft_dirty(pte_clear_soft_dirty(pte))); |
| 747 | } |
| 748 | |
Gavin Shan | 5f447e8 | 2021-09-02 14:52:32 -0700 | [diff] [blame] | 749 | static void __init pte_swap_soft_dirty_tests(struct pgtable_debug_args *args) |
Anshuman Khandual | 0528940 | 2020-08-06 23:19:16 -0700 | [diff] [blame] | 750 | { |
Gavin Shan | 5f447e8 | 2021-09-02 14:52:32 -0700 | [diff] [blame] | 751 | pte_t pte = pfn_pte(args->fixed_pte_pfn, args->page_prot); |
Anshuman Khandual | 0528940 | 2020-08-06 23:19:16 -0700 | [diff] [blame] | 752 | |
| 753 | if (!IS_ENABLED(CONFIG_MEM_SOFT_DIRTY)) |
| 754 | return; |
| 755 | |
Anshuman Khandual | 6315df4 | 2020-08-06 23:19:25 -0700 | [diff] [blame] | 756 | pr_debug("Validating PTE swap soft dirty\n"); |
Anshuman Khandual | 0528940 | 2020-08-06 23:19:16 -0700 | [diff] [blame] | 757 | WARN_ON(!pte_swp_soft_dirty(pte_swp_mksoft_dirty(pte))); |
| 758 | WARN_ON(pte_swp_soft_dirty(pte_swp_clear_soft_dirty(pte))); |
| 759 | } |
| 760 | |
| 761 | #ifdef CONFIG_TRANSPARENT_HUGEPAGE |
Gavin Shan | 5f447e8 | 2021-09-02 14:52:32 -0700 | [diff] [blame] | 762 | static void __init pmd_soft_dirty_tests(struct pgtable_debug_args *args) |
Anshuman Khandual | 0528940 | 2020-08-06 23:19:16 -0700 | [diff] [blame] | 763 | { |
Anshuman Khandual | 65ac1a6 | 2021-06-28 19:35:10 -0700 | [diff] [blame] | 764 | pmd_t pmd; |
Anshuman Khandual | 0528940 | 2020-08-06 23:19:16 -0700 | [diff] [blame] | 765 | |
| 766 | if (!IS_ENABLED(CONFIG_MEM_SOFT_DIRTY)) |
| 767 | return; |
| 768 | |
Anshuman Khandual | 65ac1a6 | 2021-06-28 19:35:10 -0700 | [diff] [blame] | 769 | if (!has_transparent_hugepage()) |
| 770 | return; |
| 771 | |
Anshuman Khandual | 6315df4 | 2020-08-06 23:19:25 -0700 | [diff] [blame] | 772 | pr_debug("Validating PMD soft dirty\n"); |
Gavin Shan | 5f447e8 | 2021-09-02 14:52:32 -0700 | [diff] [blame] | 773 | pmd = pfn_pmd(args->fixed_pmd_pfn, args->page_prot); |
Anshuman Khandual | 0528940 | 2020-08-06 23:19:16 -0700 | [diff] [blame] | 774 | WARN_ON(!pmd_soft_dirty(pmd_mksoft_dirty(pmd))); |
| 775 | WARN_ON(pmd_soft_dirty(pmd_clear_soft_dirty(pmd))); |
| 776 | } |
| 777 | |
Gavin Shan | 5f447e8 | 2021-09-02 14:52:32 -0700 | [diff] [blame] | 778 | static void __init pmd_swap_soft_dirty_tests(struct pgtable_debug_args *args) |
Anshuman Khandual | 0528940 | 2020-08-06 23:19:16 -0700 | [diff] [blame] | 779 | { |
Anshuman Khandual | 65ac1a6 | 2021-06-28 19:35:10 -0700 | [diff] [blame] | 780 | pmd_t pmd; |
Anshuman Khandual | 0528940 | 2020-08-06 23:19:16 -0700 | [diff] [blame] | 781 | |
| 782 | if (!IS_ENABLED(CONFIG_MEM_SOFT_DIRTY) || |
| 783 | !IS_ENABLED(CONFIG_ARCH_ENABLE_THP_MIGRATION)) |
| 784 | return; |
| 785 | |
Anshuman Khandual | 65ac1a6 | 2021-06-28 19:35:10 -0700 | [diff] [blame] | 786 | if (!has_transparent_hugepage()) |
| 787 | return; |
| 788 | |
Anshuman Khandual | 6315df4 | 2020-08-06 23:19:25 -0700 | [diff] [blame] | 789 | pr_debug("Validating PMD swap soft dirty\n"); |
Gavin Shan | 5f447e8 | 2021-09-02 14:52:32 -0700 | [diff] [blame] | 790 | pmd = pfn_pmd(args->fixed_pmd_pfn, args->page_prot); |
Anshuman Khandual | 0528940 | 2020-08-06 23:19:16 -0700 | [diff] [blame] | 791 | WARN_ON(!pmd_swp_soft_dirty(pmd_swp_mksoft_dirty(pmd))); |
| 792 | WARN_ON(pmd_swp_soft_dirty(pmd_swp_clear_soft_dirty(pmd))); |
| 793 | } |
Shixin Liu | b593b90 | 2021-06-30 18:47:40 -0700 | [diff] [blame] | 794 | #else /* !CONFIG_TRANSPARENT_HUGEPAGE */ |
Gavin Shan | 5f447e8 | 2021-09-02 14:52:32 -0700 | [diff] [blame] | 795 | static void __init pmd_soft_dirty_tests(struct pgtable_debug_args *args) { } |
| 796 | static void __init pmd_swap_soft_dirty_tests(struct pgtable_debug_args *args) { } |
Shixin Liu | b593b90 | 2021-06-30 18:47:40 -0700 | [diff] [blame] | 797 | #endif /* CONFIG_TRANSPARENT_HUGEPAGE */ |
Anshuman Khandual | 0528940 | 2020-08-06 23:19:16 -0700 | [diff] [blame] | 798 | |
Gavin Shan | 5f447e8 | 2021-09-02 14:52:32 -0700 | [diff] [blame] | 799 | static void __init pte_swap_tests(struct pgtable_debug_args *args) |
Anshuman Khandual | 0528940 | 2020-08-06 23:19:16 -0700 | [diff] [blame] | 800 | { |
| 801 | swp_entry_t swp; |
| 802 | pte_t pte; |
| 803 | |
Anshuman Khandual | 6315df4 | 2020-08-06 23:19:25 -0700 | [diff] [blame] | 804 | pr_debug("Validating PTE swap\n"); |
Gavin Shan | 5f447e8 | 2021-09-02 14:52:32 -0700 | [diff] [blame] | 805 | pte = pfn_pte(args->fixed_pte_pfn, args->page_prot); |
Anshuman Khandual | 0528940 | 2020-08-06 23:19:16 -0700 | [diff] [blame] | 806 | swp = __pte_to_swp_entry(pte); |
| 807 | pte = __swp_entry_to_pte(swp); |
Gavin Shan | 5f447e8 | 2021-09-02 14:52:32 -0700 | [diff] [blame] | 808 | WARN_ON(args->fixed_pte_pfn != pte_pfn(pte)); |
Anshuman Khandual | 0528940 | 2020-08-06 23:19:16 -0700 | [diff] [blame] | 809 | } |
| 810 | |
| 811 | #ifdef CONFIG_ARCH_ENABLE_THP_MIGRATION |
Gavin Shan | 5f447e8 | 2021-09-02 14:52:32 -0700 | [diff] [blame] | 812 | static void __init pmd_swap_tests(struct pgtable_debug_args *args) |
Anshuman Khandual | 0528940 | 2020-08-06 23:19:16 -0700 | [diff] [blame] | 813 | { |
| 814 | swp_entry_t swp; |
| 815 | pmd_t pmd; |
| 816 | |
Anshuman Khandual | 65ac1a6 | 2021-06-28 19:35:10 -0700 | [diff] [blame] | 817 | if (!has_transparent_hugepage()) |
| 818 | return; |
| 819 | |
Anshuman Khandual | 6315df4 | 2020-08-06 23:19:25 -0700 | [diff] [blame] | 820 | pr_debug("Validating PMD swap\n"); |
Gavin Shan | 5f447e8 | 2021-09-02 14:52:32 -0700 | [diff] [blame] | 821 | pmd = pfn_pmd(args->fixed_pmd_pfn, args->page_prot); |
Anshuman Khandual | 0528940 | 2020-08-06 23:19:16 -0700 | [diff] [blame] | 822 | swp = __pmd_to_swp_entry(pmd); |
| 823 | pmd = __swp_entry_to_pmd(swp); |
Gavin Shan | 5f447e8 | 2021-09-02 14:52:32 -0700 | [diff] [blame] | 824 | WARN_ON(args->fixed_pmd_pfn != pmd_pfn(pmd)); |
Anshuman Khandual | 0528940 | 2020-08-06 23:19:16 -0700 | [diff] [blame] | 825 | } |
| 826 | #else /* !CONFIG_ARCH_ENABLE_THP_MIGRATION */ |
Gavin Shan | 5f447e8 | 2021-09-02 14:52:32 -0700 | [diff] [blame] | 827 | static void __init pmd_swap_tests(struct pgtable_debug_args *args) { } |
Anshuman Khandual | 0528940 | 2020-08-06 23:19:16 -0700 | [diff] [blame] | 828 | #endif /* CONFIG_ARCH_ENABLE_THP_MIGRATION */ |
| 829 | |
Gavin Shan | 4878a88 | 2021-09-02 14:52:35 -0700 | [diff] [blame] | 830 | static void __init swap_migration_tests(struct pgtable_debug_args *args) |
Anshuman Khandual | 0528940 | 2020-08-06 23:19:16 -0700 | [diff] [blame] | 831 | { |
| 832 | struct page *page; |
| 833 | swp_entry_t swp; |
| 834 | |
| 835 | if (!IS_ENABLED(CONFIG_MIGRATION)) |
| 836 | return; |
Anshuman Khandual | 6315df4 | 2020-08-06 23:19:25 -0700 | [diff] [blame] | 837 | |
Anshuman Khandual | 0528940 | 2020-08-06 23:19:16 -0700 | [diff] [blame] | 838 | /* |
| 839 | * swap_migration_tests() requires a dedicated page as it needs to |
| 840 | * be locked before creating a migration entry from it. Locking the |
| 841 | * page that actually maps kernel text ('start_kernel') can be real |
Gavin Shan | 4878a88 | 2021-09-02 14:52:35 -0700 | [diff] [blame] | 842 | * problematic. Lets use the allocated page explicitly for this |
| 843 | * purpose. |
Anshuman Khandual | 0528940 | 2020-08-06 23:19:16 -0700 | [diff] [blame] | 844 | */ |
Gavin Shan | 4878a88 | 2021-09-02 14:52:35 -0700 | [diff] [blame] | 845 | page = (args->pte_pfn != ULONG_MAX) ? pfn_to_page(args->pte_pfn) : NULL; |
| 846 | if (!page) |
Anshuman Khandual | 0528940 | 2020-08-06 23:19:16 -0700 | [diff] [blame] | 847 | return; |
Gavin Shan | 4878a88 | 2021-09-02 14:52:35 -0700 | [diff] [blame] | 848 | |
| 849 | pr_debug("Validating swap migration\n"); |
Anshuman Khandual | 0528940 | 2020-08-06 23:19:16 -0700 | [diff] [blame] | 850 | |
| 851 | /* |
| 852 | * make_migration_entry() expects given page to be |
| 853 | * locked, otherwise it stumbles upon a BUG_ON(). |
| 854 | */ |
| 855 | __SetPageLocked(page); |
Alistair Popple | 4dd845b | 2021-06-30 18:54:09 -0700 | [diff] [blame] | 856 | swp = make_writable_migration_entry(page_to_pfn(page)); |
Anshuman Khandual | 0528940 | 2020-08-06 23:19:16 -0700 | [diff] [blame] | 857 | WARN_ON(!is_migration_entry(swp)); |
Alistair Popple | 4dd845b | 2021-06-30 18:54:09 -0700 | [diff] [blame] | 858 | WARN_ON(!is_writable_migration_entry(swp)); |
Anshuman Khandual | 0528940 | 2020-08-06 23:19:16 -0700 | [diff] [blame] | 859 | |
Alistair Popple | 4dd845b | 2021-06-30 18:54:09 -0700 | [diff] [blame] | 860 | swp = make_readable_migration_entry(swp_offset(swp)); |
Anshuman Khandual | 0528940 | 2020-08-06 23:19:16 -0700 | [diff] [blame] | 861 | WARN_ON(!is_migration_entry(swp)); |
Alistair Popple | 4dd845b | 2021-06-30 18:54:09 -0700 | [diff] [blame] | 862 | WARN_ON(is_writable_migration_entry(swp)); |
Anshuman Khandual | 0528940 | 2020-08-06 23:19:16 -0700 | [diff] [blame] | 863 | |
Alistair Popple | 4dd845b | 2021-06-30 18:54:09 -0700 | [diff] [blame] | 864 | swp = make_readable_migration_entry(page_to_pfn(page)); |
Anshuman Khandual | 0528940 | 2020-08-06 23:19:16 -0700 | [diff] [blame] | 865 | WARN_ON(!is_migration_entry(swp)); |
Alistair Popple | 4dd845b | 2021-06-30 18:54:09 -0700 | [diff] [blame] | 866 | WARN_ON(is_writable_migration_entry(swp)); |
Anshuman Khandual | 0528940 | 2020-08-06 23:19:16 -0700 | [diff] [blame] | 867 | __ClearPageLocked(page); |
Anshuman Khandual | 0528940 | 2020-08-06 23:19:16 -0700 | [diff] [blame] | 868 | } |
| 869 | |
| 870 | #ifdef CONFIG_HUGETLB_PAGE |
Gavin Shan | 36b77d1 | 2021-09-02 14:52:22 -0700 | [diff] [blame] | 871 | static void __init hugetlb_basic_tests(struct pgtable_debug_args *args) |
Anshuman Khandual | 0528940 | 2020-08-06 23:19:16 -0700 | [diff] [blame] | 872 | { |
| 873 | struct page *page; |
| 874 | pte_t pte; |
| 875 | |
Anshuman Khandual | 6315df4 | 2020-08-06 23:19:25 -0700 | [diff] [blame] | 876 | pr_debug("Validating HugeTLB basic\n"); |
Anshuman Khandual | 0528940 | 2020-08-06 23:19:16 -0700 | [diff] [blame] | 877 | /* |
| 878 | * Accessing the page associated with the pfn is safe here, |
| 879 | * as it was previously derived from a real kernel symbol. |
| 880 | */ |
Gavin Shan | 36b77d1 | 2021-09-02 14:52:22 -0700 | [diff] [blame] | 881 | page = pfn_to_page(args->fixed_pmd_pfn); |
| 882 | pte = mk_huge_pte(page, args->page_prot); |
Anshuman Khandual | 0528940 | 2020-08-06 23:19:16 -0700 | [diff] [blame] | 883 | |
| 884 | WARN_ON(!huge_pte_dirty(huge_pte_mkdirty(pte))); |
| 885 | WARN_ON(!huge_pte_write(huge_pte_mkwrite(huge_pte_wrprotect(pte)))); |
| 886 | WARN_ON(huge_pte_write(huge_pte_wrprotect(huge_pte_mkwrite(pte)))); |
| 887 | |
| 888 | #ifdef CONFIG_ARCH_WANT_GENERAL_HUGETLB |
Gavin Shan | 36b77d1 | 2021-09-02 14:52:22 -0700 | [diff] [blame] | 889 | pte = pfn_pte(args->fixed_pmd_pfn, args->page_prot); |
Anshuman Khandual | 0528940 | 2020-08-06 23:19:16 -0700 | [diff] [blame] | 890 | |
| 891 | WARN_ON(!pte_huge(pte_mkhuge(pte))); |
| 892 | #endif /* CONFIG_ARCH_WANT_GENERAL_HUGETLB */ |
| 893 | } |
| 894 | #else /* !CONFIG_HUGETLB_PAGE */ |
Gavin Shan | 36b77d1 | 2021-09-02 14:52:22 -0700 | [diff] [blame] | 895 | static void __init hugetlb_basic_tests(struct pgtable_debug_args *args) { } |
Anshuman Khandual | 0528940 | 2020-08-06 23:19:16 -0700 | [diff] [blame] | 896 | #endif /* CONFIG_HUGETLB_PAGE */ |
| 897 | |
| 898 | #ifdef CONFIG_TRANSPARENT_HUGEPAGE |
Gavin Shan | 4878a88 | 2021-09-02 14:52:35 -0700 | [diff] [blame] | 899 | static void __init pmd_thp_tests(struct pgtable_debug_args *args) |
Anshuman Khandual | 0528940 | 2020-08-06 23:19:16 -0700 | [diff] [blame] | 900 | { |
| 901 | pmd_t pmd; |
| 902 | |
| 903 | if (!has_transparent_hugepage()) |
| 904 | return; |
| 905 | |
Anshuman Khandual | 6315df4 | 2020-08-06 23:19:25 -0700 | [diff] [blame] | 906 | pr_debug("Validating PMD based THP\n"); |
Anshuman Khandual | 0528940 | 2020-08-06 23:19:16 -0700 | [diff] [blame] | 907 | /* |
| 908 | * pmd_trans_huge() and pmd_present() must return positive after |
| 909 | * MMU invalidation with pmd_mkinvalid(). This behavior is an |
| 910 | * optimization for transparent huge page. pmd_trans_huge() must |
| 911 | * be true if pmd_page() returns a valid THP to avoid taking the |
| 912 | * pmd_lock when others walk over non transhuge pmds (i.e. there |
| 913 | * are no THP allocated). Especially when splitting a THP and |
| 914 | * removing the present bit from the pmd, pmd_trans_huge() still |
| 915 | * needs to return true. pmd_present() should be true whenever |
| 916 | * pmd_trans_huge() returns true. |
| 917 | */ |
Gavin Shan | 4878a88 | 2021-09-02 14:52:35 -0700 | [diff] [blame] | 918 | pmd = pfn_pmd(args->fixed_pmd_pfn, args->page_prot); |
Anshuman Khandual | 0528940 | 2020-08-06 23:19:16 -0700 | [diff] [blame] | 919 | WARN_ON(!pmd_trans_huge(pmd_mkhuge(pmd))); |
| 920 | |
| 921 | #ifndef __HAVE_ARCH_PMDP_INVALIDATE |
| 922 | WARN_ON(!pmd_trans_huge(pmd_mkinvalid(pmd_mkhuge(pmd)))); |
| 923 | WARN_ON(!pmd_present(pmd_mkinvalid(pmd_mkhuge(pmd)))); |
| 924 | #endif /* __HAVE_ARCH_PMDP_INVALIDATE */ |
| 925 | } |
| 926 | |
| 927 | #ifdef CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD |
Gavin Shan | 4878a88 | 2021-09-02 14:52:35 -0700 | [diff] [blame] | 928 | static void __init pud_thp_tests(struct pgtable_debug_args *args) |
Anshuman Khandual | 0528940 | 2020-08-06 23:19:16 -0700 | [diff] [blame] | 929 | { |
| 930 | pud_t pud; |
| 931 | |
| 932 | if (!has_transparent_hugepage()) |
| 933 | return; |
| 934 | |
Anshuman Khandual | 6315df4 | 2020-08-06 23:19:25 -0700 | [diff] [blame] | 935 | pr_debug("Validating PUD based THP\n"); |
Gavin Shan | 4878a88 | 2021-09-02 14:52:35 -0700 | [diff] [blame] | 936 | pud = pfn_pud(args->fixed_pud_pfn, args->page_prot); |
Anshuman Khandual | 0528940 | 2020-08-06 23:19:16 -0700 | [diff] [blame] | 937 | WARN_ON(!pud_trans_huge(pud_mkhuge(pud))); |
| 938 | |
| 939 | /* |
| 940 | * pud_mkinvalid() has been dropped for now. Enable back |
| 941 | * these tests when it comes back with a modified pud_present(). |
| 942 | * |
| 943 | * WARN_ON(!pud_trans_huge(pud_mkinvalid(pud_mkhuge(pud)))); |
| 944 | * WARN_ON(!pud_present(pud_mkinvalid(pud_mkhuge(pud)))); |
| 945 | */ |
| 946 | } |
| 947 | #else /* !CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD */ |
Gavin Shan | 4878a88 | 2021-09-02 14:52:35 -0700 | [diff] [blame] | 948 | static void __init pud_thp_tests(struct pgtable_debug_args *args) { } |
Anshuman Khandual | 0528940 | 2020-08-06 23:19:16 -0700 | [diff] [blame] | 949 | #endif /* CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD */ |
| 950 | #else /* !CONFIG_TRANSPARENT_HUGEPAGE */ |
Gavin Shan | 4878a88 | 2021-09-02 14:52:35 -0700 | [diff] [blame] | 951 | static void __init pmd_thp_tests(struct pgtable_debug_args *args) { } |
| 952 | static void __init pud_thp_tests(struct pgtable_debug_args *args) { } |
Anshuman Khandual | 0528940 | 2020-08-06 23:19:16 -0700 | [diff] [blame] | 953 | #endif /* CONFIG_TRANSPARENT_HUGEPAGE */ |
| 954 | |
Anshuman Khandual | 399145f | 2020-06-04 16:47:15 -0700 | [diff] [blame] | 955 | static unsigned long __init get_random_vaddr(void) |
| 956 | { |
| 957 | unsigned long random_vaddr, random_pages, total_user_pages; |
| 958 | |
| 959 | total_user_pages = (TASK_SIZE - FIRST_USER_ADDRESS) / PAGE_SIZE; |
| 960 | |
| 961 | random_pages = get_random_long() % total_user_pages; |
| 962 | random_vaddr = FIRST_USER_ADDRESS + random_pages * PAGE_SIZE; |
| 963 | |
| 964 | return random_vaddr; |
| 965 | } |
| 966 | |
Gavin Shan | 3c9b84f | 2021-09-02 14:52:19 -0700 | [diff] [blame] | 967 | static void __init destroy_args(struct pgtable_debug_args *args) |
| 968 | { |
| 969 | struct page *page = NULL; |
| 970 | |
| 971 | /* Free (huge) page */ |
| 972 | if (IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE) && |
| 973 | IS_ENABLED(CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD) && |
| 974 | has_transparent_hugepage() && |
| 975 | args->pud_pfn != ULONG_MAX) { |
| 976 | if (args->is_contiguous_page) { |
| 977 | free_contig_range(args->pud_pfn, |
| 978 | (1 << (HPAGE_PUD_SHIFT - PAGE_SHIFT))); |
| 979 | } else { |
| 980 | page = pfn_to_page(args->pud_pfn); |
| 981 | __free_pages(page, HPAGE_PUD_SHIFT - PAGE_SHIFT); |
| 982 | } |
| 983 | |
| 984 | args->pud_pfn = ULONG_MAX; |
| 985 | args->pmd_pfn = ULONG_MAX; |
| 986 | args->pte_pfn = ULONG_MAX; |
| 987 | } |
| 988 | |
| 989 | if (IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE) && |
| 990 | has_transparent_hugepage() && |
| 991 | args->pmd_pfn != ULONG_MAX) { |
| 992 | if (args->is_contiguous_page) { |
| 993 | free_contig_range(args->pmd_pfn, (1 << HPAGE_PMD_ORDER)); |
| 994 | } else { |
| 995 | page = pfn_to_page(args->pmd_pfn); |
| 996 | __free_pages(page, HPAGE_PMD_ORDER); |
| 997 | } |
| 998 | |
| 999 | args->pmd_pfn = ULONG_MAX; |
| 1000 | args->pte_pfn = ULONG_MAX; |
| 1001 | } |
| 1002 | |
| 1003 | if (args->pte_pfn != ULONG_MAX) { |
| 1004 | page = pfn_to_page(args->pte_pfn); |
| 1005 | __free_pages(page, 0); |
| 1006 | |
| 1007 | args->pte_pfn = ULONG_MAX; |
| 1008 | } |
| 1009 | |
| 1010 | /* Free page table entries */ |
| 1011 | if (args->start_ptep) { |
| 1012 | pte_free(args->mm, args->start_ptep); |
| 1013 | mm_dec_nr_ptes(args->mm); |
| 1014 | } |
| 1015 | |
| 1016 | if (args->start_pmdp) { |
| 1017 | pmd_free(args->mm, args->start_pmdp); |
| 1018 | mm_dec_nr_pmds(args->mm); |
| 1019 | } |
| 1020 | |
| 1021 | if (args->start_pudp) { |
| 1022 | pud_free(args->mm, args->start_pudp); |
| 1023 | mm_dec_nr_puds(args->mm); |
| 1024 | } |
| 1025 | |
| 1026 | if (args->start_p4dp) |
| 1027 | p4d_free(args->mm, args->start_p4dp); |
| 1028 | |
| 1029 | /* Free vma and mm struct */ |
| 1030 | if (args->vma) |
| 1031 | vm_area_free(args->vma); |
| 1032 | |
| 1033 | if (args->mm) |
| 1034 | mmdrop(args->mm); |
| 1035 | } |
| 1036 | |
| 1037 | static struct page * __init |
| 1038 | debug_vm_pgtable_alloc_huge_page(struct pgtable_debug_args *args, int order) |
| 1039 | { |
| 1040 | struct page *page = NULL; |
| 1041 | |
| 1042 | #ifdef CONFIG_CONTIG_ALLOC |
| 1043 | if (order >= MAX_ORDER) { |
| 1044 | page = alloc_contig_pages((1 << order), GFP_KERNEL, |
| 1045 | first_online_node, NULL); |
| 1046 | if (page) { |
| 1047 | args->is_contiguous_page = true; |
| 1048 | return page; |
| 1049 | } |
| 1050 | } |
| 1051 | #endif |
| 1052 | |
| 1053 | if (order < MAX_ORDER) |
| 1054 | page = alloc_pages(GFP_KERNEL, order); |
| 1055 | |
| 1056 | return page; |
| 1057 | } |
| 1058 | |
| 1059 | static int __init init_args(struct pgtable_debug_args *args) |
| 1060 | { |
| 1061 | struct page *page = NULL; |
| 1062 | phys_addr_t phys; |
| 1063 | int ret = 0; |
| 1064 | |
| 1065 | /* |
| 1066 | * Initialize the debugging data. |
| 1067 | * |
| 1068 | * __P000 (or even __S000) will help create page table entries with |
| 1069 | * PROT_NONE permission as required for pxx_protnone_tests(). |
| 1070 | */ |
| 1071 | memset(args, 0, sizeof(*args)); |
| 1072 | args->vaddr = get_random_vaddr(); |
| 1073 | args->page_prot = vm_get_page_prot(VMFLAGS); |
| 1074 | args->page_prot_none = __P000; |
| 1075 | args->is_contiguous_page = false; |
| 1076 | args->pud_pfn = ULONG_MAX; |
| 1077 | args->pmd_pfn = ULONG_MAX; |
| 1078 | args->pte_pfn = ULONG_MAX; |
| 1079 | args->fixed_pgd_pfn = ULONG_MAX; |
| 1080 | args->fixed_p4d_pfn = ULONG_MAX; |
| 1081 | args->fixed_pud_pfn = ULONG_MAX; |
| 1082 | args->fixed_pmd_pfn = ULONG_MAX; |
| 1083 | args->fixed_pte_pfn = ULONG_MAX; |
| 1084 | |
| 1085 | /* Allocate mm and vma */ |
| 1086 | args->mm = mm_alloc(); |
| 1087 | if (!args->mm) { |
| 1088 | pr_err("Failed to allocate mm struct\n"); |
| 1089 | ret = -ENOMEM; |
| 1090 | goto error; |
| 1091 | } |
| 1092 | |
| 1093 | args->vma = vm_area_alloc(args->mm); |
| 1094 | if (!args->vma) { |
| 1095 | pr_err("Failed to allocate vma\n"); |
| 1096 | ret = -ENOMEM; |
| 1097 | goto error; |
| 1098 | } |
| 1099 | |
| 1100 | /* |
| 1101 | * Allocate page table entries. They will be modified in the tests. |
| 1102 | * Lets save the page table entries so that they can be released |
| 1103 | * when the tests are completed. |
| 1104 | */ |
| 1105 | args->pgdp = pgd_offset(args->mm, args->vaddr); |
| 1106 | args->p4dp = p4d_alloc(args->mm, args->pgdp, args->vaddr); |
| 1107 | if (!args->p4dp) { |
| 1108 | pr_err("Failed to allocate p4d entries\n"); |
| 1109 | ret = -ENOMEM; |
| 1110 | goto error; |
| 1111 | } |
| 1112 | args->start_p4dp = p4d_offset(args->pgdp, 0UL); |
| 1113 | WARN_ON(!args->start_p4dp); |
| 1114 | |
| 1115 | args->pudp = pud_alloc(args->mm, args->p4dp, args->vaddr); |
| 1116 | if (!args->pudp) { |
| 1117 | pr_err("Failed to allocate pud entries\n"); |
| 1118 | ret = -ENOMEM; |
| 1119 | goto error; |
| 1120 | } |
| 1121 | args->start_pudp = pud_offset(args->p4dp, 0UL); |
| 1122 | WARN_ON(!args->start_pudp); |
| 1123 | |
| 1124 | args->pmdp = pmd_alloc(args->mm, args->pudp, args->vaddr); |
| 1125 | if (!args->pmdp) { |
| 1126 | pr_err("Failed to allocate pmd entries\n"); |
| 1127 | ret = -ENOMEM; |
| 1128 | goto error; |
| 1129 | } |
| 1130 | args->start_pmdp = pmd_offset(args->pudp, 0UL); |
| 1131 | WARN_ON(!args->start_pmdp); |
| 1132 | |
| 1133 | if (pte_alloc(args->mm, args->pmdp)) { |
| 1134 | pr_err("Failed to allocate pte entries\n"); |
| 1135 | ret = -ENOMEM; |
| 1136 | goto error; |
| 1137 | } |
| 1138 | args->start_ptep = pmd_pgtable(READ_ONCE(*args->pmdp)); |
| 1139 | WARN_ON(!args->start_ptep); |
| 1140 | |
| 1141 | /* |
| 1142 | * PFN for mapping at PTE level is determined from a standard kernel |
| 1143 | * text symbol. But pfns for higher page table levels are derived by |
| 1144 | * masking lower bits of this real pfn. These derived pfns might not |
| 1145 | * exist on the platform but that does not really matter as pfn_pxx() |
| 1146 | * helpers will still create appropriate entries for the test. This |
| 1147 | * helps avoid large memory block allocations to be used for mapping |
| 1148 | * at higher page table levels in some of the tests. |
| 1149 | */ |
| 1150 | phys = __pa_symbol(&start_kernel); |
| 1151 | args->fixed_pgd_pfn = __phys_to_pfn(phys & PGDIR_MASK); |
| 1152 | args->fixed_p4d_pfn = __phys_to_pfn(phys & P4D_MASK); |
| 1153 | args->fixed_pud_pfn = __phys_to_pfn(phys & PUD_MASK); |
| 1154 | args->fixed_pmd_pfn = __phys_to_pfn(phys & PMD_MASK); |
| 1155 | args->fixed_pte_pfn = __phys_to_pfn(phys & PAGE_MASK); |
| 1156 | WARN_ON(!pfn_valid(args->fixed_pte_pfn)); |
| 1157 | |
| 1158 | /* |
| 1159 | * Allocate (huge) pages because some of the tests need to access |
| 1160 | * the data in the pages. The corresponding tests will be skipped |
| 1161 | * if we fail to allocate (huge) pages. |
| 1162 | */ |
| 1163 | if (IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE) && |
| 1164 | IS_ENABLED(CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD) && |
| 1165 | has_transparent_hugepage()) { |
| 1166 | page = debug_vm_pgtable_alloc_huge_page(args, |
| 1167 | HPAGE_PUD_SHIFT - PAGE_SHIFT); |
| 1168 | if (page) { |
| 1169 | args->pud_pfn = page_to_pfn(page); |
| 1170 | args->pmd_pfn = args->pud_pfn; |
| 1171 | args->pte_pfn = args->pud_pfn; |
| 1172 | return 0; |
| 1173 | } |
| 1174 | } |
| 1175 | |
| 1176 | if (IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE) && |
| 1177 | has_transparent_hugepage()) { |
| 1178 | page = debug_vm_pgtable_alloc_huge_page(args, HPAGE_PMD_ORDER); |
| 1179 | if (page) { |
| 1180 | args->pmd_pfn = page_to_pfn(page); |
| 1181 | args->pte_pfn = args->pmd_pfn; |
| 1182 | return 0; |
| 1183 | } |
| 1184 | } |
| 1185 | |
| 1186 | page = alloc_pages(GFP_KERNEL, 0); |
| 1187 | if (page) |
| 1188 | args->pte_pfn = page_to_pfn(page); |
| 1189 | |
| 1190 | return 0; |
| 1191 | |
| 1192 | error: |
| 1193 | destroy_args(args); |
| 1194 | return ret; |
| 1195 | } |
| 1196 | |
Anshuman Khandual | 399145f | 2020-06-04 16:47:15 -0700 | [diff] [blame] | 1197 | static int __init debug_vm_pgtable(void) |
| 1198 | { |
Gavin Shan | 3c9b84f | 2021-09-02 14:52:19 -0700 | [diff] [blame] | 1199 | struct pgtable_debug_args args; |
Anshuman Khandual | a5c3b9f | 2020-08-06 23:19:20 -0700 | [diff] [blame] | 1200 | struct vm_area_struct *vma; |
Anshuman Khandual | 399145f | 2020-06-04 16:47:15 -0700 | [diff] [blame] | 1201 | struct mm_struct *mm; |
| 1202 | pgd_t *pgdp; |
| 1203 | p4d_t *p4dp, *saved_p4dp; |
| 1204 | pud_t *pudp, *saved_pudp; |
| 1205 | pmd_t *pmdp, *saved_pmdp, pmd; |
Anshuman Khandual | 399145f | 2020-06-04 16:47:15 -0700 | [diff] [blame] | 1206 | pgtable_t saved_ptep; |
Gavin Shan | c0fe07b | 2021-09-02 14:52:41 -0700 | [diff] [blame] | 1207 | unsigned long vaddr; |
Kees Cook | fea1120 | 2020-06-03 13:28:45 -0700 | [diff] [blame] | 1208 | spinlock_t *ptl = NULL; |
Gavin Shan | 3c9b84f | 2021-09-02 14:52:19 -0700 | [diff] [blame] | 1209 | int idx, ret; |
Anshuman Khandual | 399145f | 2020-06-04 16:47:15 -0700 | [diff] [blame] | 1210 | |
| 1211 | pr_info("Validating architecture page table helpers\n"); |
Gavin Shan | 3c9b84f | 2021-09-02 14:52:19 -0700 | [diff] [blame] | 1212 | ret = init_args(&args); |
| 1213 | if (ret) |
| 1214 | return ret; |
| 1215 | |
Anshuman Khandual | 399145f | 2020-06-04 16:47:15 -0700 | [diff] [blame] | 1216 | vaddr = get_random_vaddr(); |
| 1217 | mm = mm_alloc(); |
| 1218 | if (!mm) { |
| 1219 | pr_err("mm_struct allocation failed\n"); |
| 1220 | return 1; |
| 1221 | } |
| 1222 | |
Anshuman Khandual | a5c3b9f | 2020-08-06 23:19:20 -0700 | [diff] [blame] | 1223 | vma = vm_area_alloc(mm); |
| 1224 | if (!vma) { |
| 1225 | pr_err("vma allocation failed\n"); |
| 1226 | return 1; |
| 1227 | } |
| 1228 | |
Anshuman Khandual | 399145f | 2020-06-04 16:47:15 -0700 | [diff] [blame] | 1229 | pgdp = pgd_offset(mm, vaddr); |
| 1230 | p4dp = p4d_alloc(mm, pgdp, vaddr); |
| 1231 | pudp = pud_alloc(mm, p4dp, vaddr); |
| 1232 | pmdp = pmd_alloc(mm, pudp, vaddr); |
Aneesh Kumar K.V | f14312e | 2020-10-15 20:05:10 -0700 | [diff] [blame] | 1233 | /* |
| 1234 | * Allocate pgtable_t |
| 1235 | */ |
| 1236 | if (pte_alloc(mm, pmdp)) { |
| 1237 | pr_err("pgtable allocation failed\n"); |
| 1238 | return 1; |
| 1239 | } |
Anshuman Khandual | 399145f | 2020-06-04 16:47:15 -0700 | [diff] [blame] | 1240 | |
| 1241 | /* |
| 1242 | * Save all the page table page addresses as the page table |
| 1243 | * entries will be used for testing with random or garbage |
| 1244 | * values. These saved addresses will be used for freeing |
| 1245 | * page table pages. |
| 1246 | */ |
| 1247 | pmd = READ_ONCE(*pmdp); |
| 1248 | saved_p4dp = p4d_offset(pgdp, 0UL); |
| 1249 | saved_pudp = pud_offset(p4dp, 0UL); |
| 1250 | saved_pmdp = pmd_offset(pudp, 0UL); |
| 1251 | saved_ptep = pmd_pgtable(pmd); |
| 1252 | |
Anshuman Khandual | 2e326c0 | 2021-02-24 12:01:36 -0800 | [diff] [blame] | 1253 | /* |
| 1254 | * Iterate over the protection_map[] to make sure that all |
| 1255 | * the basic page table transformation validations just hold |
| 1256 | * true irrespective of the starting protection value for a |
| 1257 | * given page table entry. |
| 1258 | */ |
| 1259 | for (idx = 0; idx < ARRAY_SIZE(protection_map); idx++) { |
Gavin Shan | 36b77d1 | 2021-09-02 14:52:22 -0700 | [diff] [blame] | 1260 | pte_basic_tests(&args, idx); |
| 1261 | pmd_basic_tests(&args, idx); |
| 1262 | pud_basic_tests(&args, idx); |
Anshuman Khandual | 2e326c0 | 2021-02-24 12:01:36 -0800 | [diff] [blame] | 1263 | } |
| 1264 | |
| 1265 | /* |
| 1266 | * Both P4D and PGD level tests are very basic which do not |
| 1267 | * involve creating page table entries from the protection |
| 1268 | * value and the given pfn. Hence just keep them out from |
| 1269 | * the above iteration for now to save some test execution |
| 1270 | * time. |
| 1271 | */ |
Gavin Shan | 36b77d1 | 2021-09-02 14:52:22 -0700 | [diff] [blame] | 1272 | p4d_basic_tests(&args); |
| 1273 | pgd_basic_tests(&args); |
Anshuman Khandual | 399145f | 2020-06-04 16:47:15 -0700 | [diff] [blame] | 1274 | |
Gavin Shan | 8983d23 | 2021-09-02 14:52:25 -0700 | [diff] [blame] | 1275 | pmd_leaf_tests(&args); |
| 1276 | pud_leaf_tests(&args); |
Anshuman Khandual | a5c3b9f | 2020-08-06 23:19:20 -0700 | [diff] [blame] | 1277 | |
Gavin Shan | 8983d23 | 2021-09-02 14:52:25 -0700 | [diff] [blame] | 1278 | pte_savedwrite_tests(&args); |
| 1279 | pmd_savedwrite_tests(&args); |
Anshuman Khandual | a5c3b9f | 2020-08-06 23:19:20 -0700 | [diff] [blame] | 1280 | |
Gavin Shan | 8cb183f | 2021-09-02 14:52:28 -0700 | [diff] [blame] | 1281 | pte_special_tests(&args); |
| 1282 | pte_protnone_tests(&args); |
| 1283 | pmd_protnone_tests(&args); |
Anshuman Khandual | 0528940 | 2020-08-06 23:19:16 -0700 | [diff] [blame] | 1284 | |
Gavin Shan | 8cb183f | 2021-09-02 14:52:28 -0700 | [diff] [blame] | 1285 | pte_devmap_tests(&args); |
| 1286 | pmd_devmap_tests(&args); |
| 1287 | pud_devmap_tests(&args); |
Anshuman Khandual | 0528940 | 2020-08-06 23:19:16 -0700 | [diff] [blame] | 1288 | |
Gavin Shan | 5f447e8 | 2021-09-02 14:52:32 -0700 | [diff] [blame] | 1289 | pte_soft_dirty_tests(&args); |
| 1290 | pmd_soft_dirty_tests(&args); |
| 1291 | pte_swap_soft_dirty_tests(&args); |
| 1292 | pmd_swap_soft_dirty_tests(&args); |
Anshuman Khandual | 0528940 | 2020-08-06 23:19:16 -0700 | [diff] [blame] | 1293 | |
Gavin Shan | 5f447e8 | 2021-09-02 14:52:32 -0700 | [diff] [blame] | 1294 | pte_swap_tests(&args); |
| 1295 | pmd_swap_tests(&args); |
Anshuman Khandual | 0528940 | 2020-08-06 23:19:16 -0700 | [diff] [blame] | 1296 | |
Gavin Shan | 4878a88 | 2021-09-02 14:52:35 -0700 | [diff] [blame] | 1297 | swap_migration_tests(&args); |
Anshuman Khandual | 0528940 | 2020-08-06 23:19:16 -0700 | [diff] [blame] | 1298 | |
Gavin Shan | 4878a88 | 2021-09-02 14:52:35 -0700 | [diff] [blame] | 1299 | pmd_thp_tests(&args); |
| 1300 | pud_thp_tests(&args); |
Anshuman Khandual | 0528940 | 2020-08-06 23:19:16 -0700 | [diff] [blame] | 1301 | |
Gavin Shan | 36b77d1 | 2021-09-02 14:52:22 -0700 | [diff] [blame] | 1302 | hugetlb_basic_tests(&args); |
Aneesh Kumar K.V | e8edf0a | 2020-10-15 20:04:49 -0700 | [diff] [blame] | 1303 | |
Aneesh Kumar K.V | 6f302e27 | 2020-10-15 20:04:53 -0700 | [diff] [blame] | 1304 | /* |
| 1305 | * Page table modifying tests. They need to hold |
| 1306 | * proper page table lock. |
| 1307 | */ |
Aneesh Kumar K.V | e8edf0a | 2020-10-15 20:04:49 -0700 | [diff] [blame] | 1308 | |
Gavin Shan | 44966c4 | 2021-09-02 14:52:38 -0700 | [diff] [blame] | 1309 | args.ptep = pte_offset_map_lock(args.mm, args.pmdp, args.vaddr, &ptl); |
| 1310 | pte_clear_tests(&args); |
| 1311 | pte_advanced_tests(&args); |
| 1312 | pte_unmap_unlock(args.ptep, ptl); |
Aneesh Kumar K.V | e8edf0a | 2020-10-15 20:04:49 -0700 | [diff] [blame] | 1313 | |
Gavin Shan | c0fe07b | 2021-09-02 14:52:41 -0700 | [diff] [blame] | 1314 | ptl = pmd_lock(args.mm, args.pmdp); |
| 1315 | pmd_clear_tests(&args); |
| 1316 | pmd_advanced_tests(&args); |
| 1317 | pmd_huge_tests(&args); |
| 1318 | pmd_populate_tests(&args); |
Aneesh Kumar K.V | 6f302e27 | 2020-10-15 20:04:53 -0700 | [diff] [blame] | 1319 | spin_unlock(ptl); |
| 1320 | |
Gavin Shan | 4cbde03 | 2021-09-02 14:52:45 -0700 | [diff] [blame^] | 1321 | ptl = pud_lock(args.mm, args.pudp); |
| 1322 | pud_clear_tests(&args); |
| 1323 | pud_advanced_tests(&args); |
| 1324 | pud_huge_tests(&args); |
| 1325 | pud_populate_tests(&args); |
Aneesh Kumar K.V | 6f302e27 | 2020-10-15 20:04:53 -0700 | [diff] [blame] | 1326 | spin_unlock(ptl); |
| 1327 | |
Aneesh Kumar K.V | 6f302e27 | 2020-10-15 20:04:53 -0700 | [diff] [blame] | 1328 | spin_lock(&mm->page_table_lock); |
| 1329 | p4d_clear_tests(mm, p4dp); |
| 1330 | pgd_clear_tests(mm, pgdp); |
Aneesh Kumar K.V | e8edf0a | 2020-10-15 20:04:49 -0700 | [diff] [blame] | 1331 | p4d_populate_tests(mm, p4dp, saved_pudp); |
| 1332 | pgd_populate_tests(mm, pgdp, saved_p4dp); |
Aneesh Kumar K.V | 6f302e27 | 2020-10-15 20:04:53 -0700 | [diff] [blame] | 1333 | spin_unlock(&mm->page_table_lock); |
Aneesh Kumar K.V | e8edf0a | 2020-10-15 20:04:49 -0700 | [diff] [blame] | 1334 | |
Anshuman Khandual | 399145f | 2020-06-04 16:47:15 -0700 | [diff] [blame] | 1335 | p4d_free(mm, saved_p4dp); |
| 1336 | pud_free(mm, saved_pudp); |
| 1337 | pmd_free(mm, saved_pmdp); |
| 1338 | pte_free(mm, saved_ptep); |
| 1339 | |
Anshuman Khandual | a5c3b9f | 2020-08-06 23:19:20 -0700 | [diff] [blame] | 1340 | vm_area_free(vma); |
Anshuman Khandual | 399145f | 2020-06-04 16:47:15 -0700 | [diff] [blame] | 1341 | mm_dec_nr_puds(mm); |
| 1342 | mm_dec_nr_pmds(mm); |
| 1343 | mm_dec_nr_ptes(mm); |
| 1344 | mmdrop(mm); |
Gavin Shan | 3c9b84f | 2021-09-02 14:52:19 -0700 | [diff] [blame] | 1345 | |
| 1346 | destroy_args(&args); |
Anshuman Khandual | 399145f | 2020-06-04 16:47:15 -0700 | [diff] [blame] | 1347 | return 0; |
| 1348 | } |
| 1349 | late_initcall(debug_vm_pgtable); |