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 | */ |
| 11 | #define pr_fmt(fmt) "debug_vm_pgtable: %s: " fmt, __func__ |
| 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> |
| 24 | #include <linux/random.h> |
| 25 | #include <linux/spinlock.h> |
| 26 | #include <linux/swap.h> |
| 27 | #include <linux/swapops.h> |
| 28 | #include <linux/start_kernel.h> |
| 29 | #include <linux/sched/mm.h> |
| 30 | #include <asm/pgalloc.h> |
Anshuman Khandual | 399145f | 2020-06-04 16:47:15 -0700 | [diff] [blame] | 31 | |
| 32 | #define VMFLAGS (VM_READ|VM_WRITE|VM_EXEC) |
| 33 | |
| 34 | /* |
| 35 | * On s390 platform, the lower 4 bits are used to identify given page table |
| 36 | * entry type. But these bits might affect the ability to clear entries with |
| 37 | * pxx_clear() because of how dynamic page table folding works on s390. So |
| 38 | * while loading up the entries do not change the lower 4 bits. It does not |
| 39 | * have affect any other platform. |
| 40 | */ |
| 41 | #define S390_MASK_BITS 4 |
| 42 | #define RANDOM_ORVALUE GENMASK(BITS_PER_LONG - 1, S390_MASK_BITS) |
| 43 | #define RANDOM_NZVALUE GENMASK(7, 0) |
| 44 | |
| 45 | static void __init pte_basic_tests(unsigned long pfn, pgprot_t prot) |
| 46 | { |
| 47 | pte_t pte = pfn_pte(pfn, prot); |
| 48 | |
| 49 | WARN_ON(!pte_same(pte, pte)); |
| 50 | WARN_ON(!pte_young(pte_mkyoung(pte_mkold(pte)))); |
| 51 | WARN_ON(!pte_dirty(pte_mkdirty(pte_mkclean(pte)))); |
| 52 | WARN_ON(!pte_write(pte_mkwrite(pte_wrprotect(pte)))); |
| 53 | WARN_ON(pte_young(pte_mkold(pte_mkyoung(pte)))); |
| 54 | WARN_ON(pte_dirty(pte_mkclean(pte_mkdirty(pte)))); |
| 55 | WARN_ON(pte_write(pte_wrprotect(pte_mkwrite(pte)))); |
| 56 | } |
| 57 | |
| 58 | #ifdef CONFIG_TRANSPARENT_HUGEPAGE |
| 59 | static void __init pmd_basic_tests(unsigned long pfn, pgprot_t prot) |
| 60 | { |
| 61 | pmd_t pmd = pfn_pmd(pfn, prot); |
| 62 | |
Aneesh Kumar K.V | 787d563 | 2020-06-10 18:41:44 -0700 | [diff] [blame] | 63 | if (!has_transparent_hugepage()) |
| 64 | return; |
| 65 | |
Anshuman Khandual | 399145f | 2020-06-04 16:47:15 -0700 | [diff] [blame] | 66 | WARN_ON(!pmd_same(pmd, pmd)); |
| 67 | WARN_ON(!pmd_young(pmd_mkyoung(pmd_mkold(pmd)))); |
| 68 | WARN_ON(!pmd_dirty(pmd_mkdirty(pmd_mkclean(pmd)))); |
| 69 | WARN_ON(!pmd_write(pmd_mkwrite(pmd_wrprotect(pmd)))); |
| 70 | WARN_ON(pmd_young(pmd_mkold(pmd_mkyoung(pmd)))); |
| 71 | WARN_ON(pmd_dirty(pmd_mkclean(pmd_mkdirty(pmd)))); |
| 72 | WARN_ON(pmd_write(pmd_wrprotect(pmd_mkwrite(pmd)))); |
| 73 | /* |
| 74 | * A huge page does not point to next level page table |
| 75 | * entry. Hence this must qualify as pmd_bad(). |
| 76 | */ |
| 77 | WARN_ON(!pmd_bad(pmd_mkhuge(pmd))); |
| 78 | } |
| 79 | |
| 80 | #ifdef CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD |
| 81 | static void __init pud_basic_tests(unsigned long pfn, pgprot_t prot) |
| 82 | { |
| 83 | pud_t pud = pfn_pud(pfn, prot); |
| 84 | |
Aneesh Kumar K.V | 787d563 | 2020-06-10 18:41:44 -0700 | [diff] [blame] | 85 | if (!has_transparent_hugepage()) |
| 86 | return; |
| 87 | |
Anshuman Khandual | 399145f | 2020-06-04 16:47:15 -0700 | [diff] [blame] | 88 | WARN_ON(!pud_same(pud, pud)); |
| 89 | WARN_ON(!pud_young(pud_mkyoung(pud_mkold(pud)))); |
| 90 | WARN_ON(!pud_write(pud_mkwrite(pud_wrprotect(pud)))); |
| 91 | WARN_ON(pud_write(pud_wrprotect(pud_mkwrite(pud)))); |
| 92 | WARN_ON(pud_young(pud_mkold(pud_mkyoung(pud)))); |
| 93 | |
| 94 | if (mm_pmd_folded(mm)) |
| 95 | return; |
| 96 | |
| 97 | /* |
| 98 | * A huge page does not point to next level page table |
| 99 | * entry. Hence this must qualify as pud_bad(). |
| 100 | */ |
| 101 | WARN_ON(!pud_bad(pud_mkhuge(pud))); |
| 102 | } |
| 103 | #else /* !CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD */ |
| 104 | static void __init pud_basic_tests(unsigned long pfn, pgprot_t prot) { } |
| 105 | #endif /* CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD */ |
| 106 | #else /* !CONFIG_TRANSPARENT_HUGEPAGE */ |
| 107 | static void __init pmd_basic_tests(unsigned long pfn, pgprot_t prot) { } |
| 108 | static void __init pud_basic_tests(unsigned long pfn, pgprot_t prot) { } |
| 109 | #endif /* CONFIG_TRANSPARENT_HUGEPAGE */ |
| 110 | |
| 111 | static void __init p4d_basic_tests(unsigned long pfn, pgprot_t prot) |
| 112 | { |
| 113 | p4d_t p4d; |
| 114 | |
| 115 | memset(&p4d, RANDOM_NZVALUE, sizeof(p4d_t)); |
| 116 | WARN_ON(!p4d_same(p4d, p4d)); |
| 117 | } |
| 118 | |
| 119 | static void __init pgd_basic_tests(unsigned long pfn, pgprot_t prot) |
| 120 | { |
| 121 | pgd_t pgd; |
| 122 | |
| 123 | memset(&pgd, RANDOM_NZVALUE, sizeof(pgd_t)); |
| 124 | WARN_ON(!pgd_same(pgd, pgd)); |
| 125 | } |
| 126 | |
| 127 | #ifndef __PAGETABLE_PUD_FOLDED |
| 128 | static void __init pud_clear_tests(struct mm_struct *mm, pud_t *pudp) |
| 129 | { |
| 130 | pud_t pud = READ_ONCE(*pudp); |
| 131 | |
| 132 | if (mm_pmd_folded(mm)) |
| 133 | return; |
| 134 | |
| 135 | pud = __pud(pud_val(pud) | RANDOM_ORVALUE); |
| 136 | WRITE_ONCE(*pudp, pud); |
| 137 | pud_clear(pudp); |
| 138 | pud = READ_ONCE(*pudp); |
| 139 | WARN_ON(!pud_none(pud)); |
| 140 | } |
| 141 | |
| 142 | static void __init pud_populate_tests(struct mm_struct *mm, pud_t *pudp, |
| 143 | pmd_t *pmdp) |
| 144 | { |
| 145 | pud_t pud; |
| 146 | |
| 147 | if (mm_pmd_folded(mm)) |
| 148 | return; |
| 149 | /* |
| 150 | * This entry points to next level page table page. |
| 151 | * Hence this must not qualify as pud_bad(). |
| 152 | */ |
| 153 | pmd_clear(pmdp); |
| 154 | pud_clear(pudp); |
| 155 | pud_populate(mm, pudp, pmdp); |
| 156 | pud = READ_ONCE(*pudp); |
| 157 | WARN_ON(pud_bad(pud)); |
| 158 | } |
| 159 | #else /* !__PAGETABLE_PUD_FOLDED */ |
| 160 | static void __init pud_clear_tests(struct mm_struct *mm, pud_t *pudp) { } |
| 161 | static void __init pud_populate_tests(struct mm_struct *mm, pud_t *pudp, |
| 162 | pmd_t *pmdp) |
| 163 | { |
| 164 | } |
| 165 | #endif /* PAGETABLE_PUD_FOLDED */ |
| 166 | |
| 167 | #ifndef __PAGETABLE_P4D_FOLDED |
| 168 | static void __init p4d_clear_tests(struct mm_struct *mm, p4d_t *p4dp) |
| 169 | { |
| 170 | p4d_t p4d = READ_ONCE(*p4dp); |
| 171 | |
| 172 | if (mm_pud_folded(mm)) |
| 173 | return; |
| 174 | |
| 175 | p4d = __p4d(p4d_val(p4d) | RANDOM_ORVALUE); |
| 176 | WRITE_ONCE(*p4dp, p4d); |
| 177 | p4d_clear(p4dp); |
| 178 | p4d = READ_ONCE(*p4dp); |
| 179 | WARN_ON(!p4d_none(p4d)); |
| 180 | } |
| 181 | |
| 182 | static void __init p4d_populate_tests(struct mm_struct *mm, p4d_t *p4dp, |
| 183 | pud_t *pudp) |
| 184 | { |
| 185 | p4d_t p4d; |
| 186 | |
| 187 | if (mm_pud_folded(mm)) |
| 188 | return; |
| 189 | |
| 190 | /* |
| 191 | * This entry points to next level page table page. |
| 192 | * Hence this must not qualify as p4d_bad(). |
| 193 | */ |
| 194 | pud_clear(pudp); |
| 195 | p4d_clear(p4dp); |
| 196 | p4d_populate(mm, p4dp, pudp); |
| 197 | p4d = READ_ONCE(*p4dp); |
| 198 | WARN_ON(p4d_bad(p4d)); |
| 199 | } |
| 200 | |
| 201 | static void __init pgd_clear_tests(struct mm_struct *mm, pgd_t *pgdp) |
| 202 | { |
| 203 | pgd_t pgd = READ_ONCE(*pgdp); |
| 204 | |
| 205 | if (mm_p4d_folded(mm)) |
| 206 | return; |
| 207 | |
| 208 | pgd = __pgd(pgd_val(pgd) | RANDOM_ORVALUE); |
| 209 | WRITE_ONCE(*pgdp, pgd); |
| 210 | pgd_clear(pgdp); |
| 211 | pgd = READ_ONCE(*pgdp); |
| 212 | WARN_ON(!pgd_none(pgd)); |
| 213 | } |
| 214 | |
| 215 | static void __init pgd_populate_tests(struct mm_struct *mm, pgd_t *pgdp, |
| 216 | p4d_t *p4dp) |
| 217 | { |
| 218 | pgd_t pgd; |
| 219 | |
| 220 | if (mm_p4d_folded(mm)) |
| 221 | return; |
| 222 | |
| 223 | /* |
| 224 | * This entry points to next level page table page. |
| 225 | * Hence this must not qualify as pgd_bad(). |
| 226 | */ |
| 227 | p4d_clear(p4dp); |
| 228 | pgd_clear(pgdp); |
| 229 | pgd_populate(mm, pgdp, p4dp); |
| 230 | pgd = READ_ONCE(*pgdp); |
| 231 | WARN_ON(pgd_bad(pgd)); |
| 232 | } |
| 233 | #else /* !__PAGETABLE_P4D_FOLDED */ |
| 234 | static void __init p4d_clear_tests(struct mm_struct *mm, p4d_t *p4dp) { } |
| 235 | static void __init pgd_clear_tests(struct mm_struct *mm, pgd_t *pgdp) { } |
| 236 | static void __init p4d_populate_tests(struct mm_struct *mm, p4d_t *p4dp, |
| 237 | pud_t *pudp) |
| 238 | { |
| 239 | } |
| 240 | static void __init pgd_populate_tests(struct mm_struct *mm, pgd_t *pgdp, |
| 241 | p4d_t *p4dp) |
| 242 | { |
| 243 | } |
| 244 | #endif /* PAGETABLE_P4D_FOLDED */ |
| 245 | |
| 246 | static void __init pte_clear_tests(struct mm_struct *mm, pte_t *ptep, |
| 247 | unsigned long vaddr) |
| 248 | { |
Christophe Leroy | 9449c9c | 2020-06-25 20:30:04 -0700 | [diff] [blame] | 249 | pte_t pte = ptep_get(ptep); |
Anshuman Khandual | 399145f | 2020-06-04 16:47:15 -0700 | [diff] [blame] | 250 | |
| 251 | pte = __pte(pte_val(pte) | RANDOM_ORVALUE); |
| 252 | set_pte_at(mm, vaddr, ptep, pte); |
| 253 | barrier(); |
| 254 | pte_clear(mm, vaddr, ptep); |
Christophe Leroy | 9449c9c | 2020-06-25 20:30:04 -0700 | [diff] [blame] | 255 | pte = ptep_get(ptep); |
Anshuman Khandual | 399145f | 2020-06-04 16:47:15 -0700 | [diff] [blame] | 256 | WARN_ON(!pte_none(pte)); |
| 257 | } |
| 258 | |
| 259 | static void __init pmd_clear_tests(struct mm_struct *mm, pmd_t *pmdp) |
| 260 | { |
| 261 | pmd_t pmd = READ_ONCE(*pmdp); |
| 262 | |
| 263 | pmd = __pmd(pmd_val(pmd) | RANDOM_ORVALUE); |
| 264 | WRITE_ONCE(*pmdp, pmd); |
| 265 | pmd_clear(pmdp); |
| 266 | pmd = READ_ONCE(*pmdp); |
| 267 | WARN_ON(!pmd_none(pmd)); |
| 268 | } |
| 269 | |
| 270 | static void __init pmd_populate_tests(struct mm_struct *mm, pmd_t *pmdp, |
| 271 | pgtable_t pgtable) |
| 272 | { |
| 273 | pmd_t pmd; |
| 274 | |
| 275 | /* |
| 276 | * This entry points to next level page table page. |
| 277 | * Hence this must not qualify as pmd_bad(). |
| 278 | */ |
| 279 | pmd_clear(pmdp); |
| 280 | pmd_populate(mm, pmdp, pgtable); |
| 281 | pmd = READ_ONCE(*pmdp); |
| 282 | WARN_ON(pmd_bad(pmd)); |
| 283 | } |
| 284 | |
Anshuman Khandual | 0528940 | 2020-08-06 23:19:16 -0700 | [diff] [blame^] | 285 | static void __init pte_special_tests(unsigned long pfn, pgprot_t prot) |
| 286 | { |
| 287 | pte_t pte = pfn_pte(pfn, prot); |
| 288 | |
| 289 | if (!IS_ENABLED(CONFIG_ARCH_HAS_PTE_SPECIAL)) |
| 290 | return; |
| 291 | |
| 292 | WARN_ON(!pte_special(pte_mkspecial(pte))); |
| 293 | } |
| 294 | |
| 295 | static void __init pte_protnone_tests(unsigned long pfn, pgprot_t prot) |
| 296 | { |
| 297 | pte_t pte = pfn_pte(pfn, prot); |
| 298 | |
| 299 | if (!IS_ENABLED(CONFIG_NUMA_BALANCING)) |
| 300 | return; |
| 301 | |
| 302 | WARN_ON(!pte_protnone(pte)); |
| 303 | WARN_ON(!pte_present(pte)); |
| 304 | } |
| 305 | |
| 306 | #ifdef CONFIG_TRANSPARENT_HUGEPAGE |
| 307 | static void __init pmd_protnone_tests(unsigned long pfn, pgprot_t prot) |
| 308 | { |
| 309 | pmd_t pmd = pmd_mkhuge(pfn_pmd(pfn, prot)); |
| 310 | |
| 311 | if (!IS_ENABLED(CONFIG_NUMA_BALANCING)) |
| 312 | return; |
| 313 | |
| 314 | WARN_ON(!pmd_protnone(pmd)); |
| 315 | WARN_ON(!pmd_present(pmd)); |
| 316 | } |
| 317 | #else /* !CONFIG_TRANSPARENT_HUGEPAGE */ |
| 318 | static void __init pmd_protnone_tests(unsigned long pfn, pgprot_t prot) { } |
| 319 | #endif /* CONFIG_TRANSPARENT_HUGEPAGE */ |
| 320 | |
| 321 | #ifdef CONFIG_ARCH_HAS_PTE_DEVMAP |
| 322 | static void __init pte_devmap_tests(unsigned long pfn, pgprot_t prot) |
| 323 | { |
| 324 | pte_t pte = pfn_pte(pfn, prot); |
| 325 | |
| 326 | WARN_ON(!pte_devmap(pte_mkdevmap(pte))); |
| 327 | } |
| 328 | |
| 329 | #ifdef CONFIG_TRANSPARENT_HUGEPAGE |
| 330 | static void __init pmd_devmap_tests(unsigned long pfn, pgprot_t prot) |
| 331 | { |
| 332 | pmd_t pmd = pfn_pmd(pfn, prot); |
| 333 | |
| 334 | WARN_ON(!pmd_devmap(pmd_mkdevmap(pmd))); |
| 335 | } |
| 336 | |
| 337 | #ifdef CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD |
| 338 | static void __init pud_devmap_tests(unsigned long pfn, pgprot_t prot) |
| 339 | { |
| 340 | pud_t pud = pfn_pud(pfn, prot); |
| 341 | |
| 342 | WARN_ON(!pud_devmap(pud_mkdevmap(pud))); |
| 343 | } |
| 344 | #else /* !CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD */ |
| 345 | static void __init pud_devmap_tests(unsigned long pfn, pgprot_t prot) { } |
| 346 | #endif /* CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD */ |
| 347 | #else /* CONFIG_TRANSPARENT_HUGEPAGE */ |
| 348 | static void __init pmd_devmap_tests(unsigned long pfn, pgprot_t prot) { } |
| 349 | static void __init pud_devmap_tests(unsigned long pfn, pgprot_t prot) { } |
| 350 | #endif /* CONFIG_TRANSPARENT_HUGEPAGE */ |
| 351 | #else |
| 352 | static void __init pte_devmap_tests(unsigned long pfn, pgprot_t prot) { } |
| 353 | static void __init pmd_devmap_tests(unsigned long pfn, pgprot_t prot) { } |
| 354 | static void __init pud_devmap_tests(unsigned long pfn, pgprot_t prot) { } |
| 355 | #endif /* CONFIG_ARCH_HAS_PTE_DEVMAP */ |
| 356 | |
| 357 | static void __init pte_soft_dirty_tests(unsigned long pfn, pgprot_t prot) |
| 358 | { |
| 359 | pte_t pte = pfn_pte(pfn, prot); |
| 360 | |
| 361 | if (!IS_ENABLED(CONFIG_MEM_SOFT_DIRTY)) |
| 362 | return; |
| 363 | |
| 364 | WARN_ON(!pte_soft_dirty(pte_mksoft_dirty(pte))); |
| 365 | WARN_ON(pte_soft_dirty(pte_clear_soft_dirty(pte))); |
| 366 | } |
| 367 | |
| 368 | static void __init pte_swap_soft_dirty_tests(unsigned long pfn, pgprot_t prot) |
| 369 | { |
| 370 | pte_t pte = pfn_pte(pfn, prot); |
| 371 | |
| 372 | if (!IS_ENABLED(CONFIG_MEM_SOFT_DIRTY)) |
| 373 | return; |
| 374 | |
| 375 | WARN_ON(!pte_swp_soft_dirty(pte_swp_mksoft_dirty(pte))); |
| 376 | WARN_ON(pte_swp_soft_dirty(pte_swp_clear_soft_dirty(pte))); |
| 377 | } |
| 378 | |
| 379 | #ifdef CONFIG_TRANSPARENT_HUGEPAGE |
| 380 | static void __init pmd_soft_dirty_tests(unsigned long pfn, pgprot_t prot) |
| 381 | { |
| 382 | pmd_t pmd = pfn_pmd(pfn, prot); |
| 383 | |
| 384 | if (!IS_ENABLED(CONFIG_MEM_SOFT_DIRTY)) |
| 385 | return; |
| 386 | |
| 387 | WARN_ON(!pmd_soft_dirty(pmd_mksoft_dirty(pmd))); |
| 388 | WARN_ON(pmd_soft_dirty(pmd_clear_soft_dirty(pmd))); |
| 389 | } |
| 390 | |
| 391 | static void __init pmd_swap_soft_dirty_tests(unsigned long pfn, pgprot_t prot) |
| 392 | { |
| 393 | pmd_t pmd = pfn_pmd(pfn, prot); |
| 394 | |
| 395 | if (!IS_ENABLED(CONFIG_MEM_SOFT_DIRTY) || |
| 396 | !IS_ENABLED(CONFIG_ARCH_ENABLE_THP_MIGRATION)) |
| 397 | return; |
| 398 | |
| 399 | WARN_ON(!pmd_swp_soft_dirty(pmd_swp_mksoft_dirty(pmd))); |
| 400 | WARN_ON(pmd_swp_soft_dirty(pmd_swp_clear_soft_dirty(pmd))); |
| 401 | } |
| 402 | #else /* !CONFIG_ARCH_HAS_PTE_DEVMAP */ |
| 403 | static void __init pmd_soft_dirty_tests(unsigned long pfn, pgprot_t prot) { } |
| 404 | static void __init pmd_swap_soft_dirty_tests(unsigned long pfn, pgprot_t prot) |
| 405 | { |
| 406 | } |
| 407 | #endif /* CONFIG_ARCH_HAS_PTE_DEVMAP */ |
| 408 | |
| 409 | static void __init pte_swap_tests(unsigned long pfn, pgprot_t prot) |
| 410 | { |
| 411 | swp_entry_t swp; |
| 412 | pte_t pte; |
| 413 | |
| 414 | pte = pfn_pte(pfn, prot); |
| 415 | swp = __pte_to_swp_entry(pte); |
| 416 | pte = __swp_entry_to_pte(swp); |
| 417 | WARN_ON(pfn != pte_pfn(pte)); |
| 418 | } |
| 419 | |
| 420 | #ifdef CONFIG_ARCH_ENABLE_THP_MIGRATION |
| 421 | static void __init pmd_swap_tests(unsigned long pfn, pgprot_t prot) |
| 422 | { |
| 423 | swp_entry_t swp; |
| 424 | pmd_t pmd; |
| 425 | |
| 426 | pmd = pfn_pmd(pfn, prot); |
| 427 | swp = __pmd_to_swp_entry(pmd); |
| 428 | pmd = __swp_entry_to_pmd(swp); |
| 429 | WARN_ON(pfn != pmd_pfn(pmd)); |
| 430 | } |
| 431 | #else /* !CONFIG_ARCH_ENABLE_THP_MIGRATION */ |
| 432 | static void __init pmd_swap_tests(unsigned long pfn, pgprot_t prot) { } |
| 433 | #endif /* CONFIG_ARCH_ENABLE_THP_MIGRATION */ |
| 434 | |
| 435 | static void __init swap_migration_tests(void) |
| 436 | { |
| 437 | struct page *page; |
| 438 | swp_entry_t swp; |
| 439 | |
| 440 | if (!IS_ENABLED(CONFIG_MIGRATION)) |
| 441 | return; |
| 442 | /* |
| 443 | * swap_migration_tests() requires a dedicated page as it needs to |
| 444 | * be locked before creating a migration entry from it. Locking the |
| 445 | * page that actually maps kernel text ('start_kernel') can be real |
| 446 | * problematic. Lets allocate a dedicated page explicitly for this |
| 447 | * purpose that will be freed subsequently. |
| 448 | */ |
| 449 | page = alloc_page(GFP_KERNEL); |
| 450 | if (!page) { |
| 451 | pr_err("page allocation failed\n"); |
| 452 | return; |
| 453 | } |
| 454 | |
| 455 | /* |
| 456 | * make_migration_entry() expects given page to be |
| 457 | * locked, otherwise it stumbles upon a BUG_ON(). |
| 458 | */ |
| 459 | __SetPageLocked(page); |
| 460 | swp = make_migration_entry(page, 1); |
| 461 | WARN_ON(!is_migration_entry(swp)); |
| 462 | WARN_ON(!is_write_migration_entry(swp)); |
| 463 | |
| 464 | make_migration_entry_read(&swp); |
| 465 | WARN_ON(!is_migration_entry(swp)); |
| 466 | WARN_ON(is_write_migration_entry(swp)); |
| 467 | |
| 468 | swp = make_migration_entry(page, 0); |
| 469 | WARN_ON(!is_migration_entry(swp)); |
| 470 | WARN_ON(is_write_migration_entry(swp)); |
| 471 | __ClearPageLocked(page); |
| 472 | __free_page(page); |
| 473 | } |
| 474 | |
| 475 | #ifdef CONFIG_HUGETLB_PAGE |
| 476 | static void __init hugetlb_basic_tests(unsigned long pfn, pgprot_t prot) |
| 477 | { |
| 478 | struct page *page; |
| 479 | pte_t pte; |
| 480 | |
| 481 | /* |
| 482 | * Accessing the page associated with the pfn is safe here, |
| 483 | * as it was previously derived from a real kernel symbol. |
| 484 | */ |
| 485 | page = pfn_to_page(pfn); |
| 486 | pte = mk_huge_pte(page, prot); |
| 487 | |
| 488 | WARN_ON(!huge_pte_dirty(huge_pte_mkdirty(pte))); |
| 489 | WARN_ON(!huge_pte_write(huge_pte_mkwrite(huge_pte_wrprotect(pte)))); |
| 490 | WARN_ON(huge_pte_write(huge_pte_wrprotect(huge_pte_mkwrite(pte)))); |
| 491 | |
| 492 | #ifdef CONFIG_ARCH_WANT_GENERAL_HUGETLB |
| 493 | pte = pfn_pte(pfn, prot); |
| 494 | |
| 495 | WARN_ON(!pte_huge(pte_mkhuge(pte))); |
| 496 | #endif /* CONFIG_ARCH_WANT_GENERAL_HUGETLB */ |
| 497 | } |
| 498 | #else /* !CONFIG_HUGETLB_PAGE */ |
| 499 | static void __init hugetlb_basic_tests(unsigned long pfn, pgprot_t prot) { } |
| 500 | #endif /* CONFIG_HUGETLB_PAGE */ |
| 501 | |
| 502 | #ifdef CONFIG_TRANSPARENT_HUGEPAGE |
| 503 | static void __init pmd_thp_tests(unsigned long pfn, pgprot_t prot) |
| 504 | { |
| 505 | pmd_t pmd; |
| 506 | |
| 507 | if (!has_transparent_hugepage()) |
| 508 | return; |
| 509 | |
| 510 | /* |
| 511 | * pmd_trans_huge() and pmd_present() must return positive after |
| 512 | * MMU invalidation with pmd_mkinvalid(). This behavior is an |
| 513 | * optimization for transparent huge page. pmd_trans_huge() must |
| 514 | * be true if pmd_page() returns a valid THP to avoid taking the |
| 515 | * pmd_lock when others walk over non transhuge pmds (i.e. there |
| 516 | * are no THP allocated). Especially when splitting a THP and |
| 517 | * removing the present bit from the pmd, pmd_trans_huge() still |
| 518 | * needs to return true. pmd_present() should be true whenever |
| 519 | * pmd_trans_huge() returns true. |
| 520 | */ |
| 521 | pmd = pfn_pmd(pfn, prot); |
| 522 | WARN_ON(!pmd_trans_huge(pmd_mkhuge(pmd))); |
| 523 | |
| 524 | #ifndef __HAVE_ARCH_PMDP_INVALIDATE |
| 525 | WARN_ON(!pmd_trans_huge(pmd_mkinvalid(pmd_mkhuge(pmd)))); |
| 526 | WARN_ON(!pmd_present(pmd_mkinvalid(pmd_mkhuge(pmd)))); |
| 527 | #endif /* __HAVE_ARCH_PMDP_INVALIDATE */ |
| 528 | } |
| 529 | |
| 530 | #ifdef CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD |
| 531 | static void __init pud_thp_tests(unsigned long pfn, pgprot_t prot) |
| 532 | { |
| 533 | pud_t pud; |
| 534 | |
| 535 | if (!has_transparent_hugepage()) |
| 536 | return; |
| 537 | |
| 538 | pud = pfn_pud(pfn, prot); |
| 539 | WARN_ON(!pud_trans_huge(pud_mkhuge(pud))); |
| 540 | |
| 541 | /* |
| 542 | * pud_mkinvalid() has been dropped for now. Enable back |
| 543 | * these tests when it comes back with a modified pud_present(). |
| 544 | * |
| 545 | * WARN_ON(!pud_trans_huge(pud_mkinvalid(pud_mkhuge(pud)))); |
| 546 | * WARN_ON(!pud_present(pud_mkinvalid(pud_mkhuge(pud)))); |
| 547 | */ |
| 548 | } |
| 549 | #else /* !CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD */ |
| 550 | static void __init pud_thp_tests(unsigned long pfn, pgprot_t prot) { } |
| 551 | #endif /* CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD */ |
| 552 | #else /* !CONFIG_TRANSPARENT_HUGEPAGE */ |
| 553 | static void __init pmd_thp_tests(unsigned long pfn, pgprot_t prot) { } |
| 554 | static void __init pud_thp_tests(unsigned long pfn, pgprot_t prot) { } |
| 555 | #endif /* CONFIG_TRANSPARENT_HUGEPAGE */ |
| 556 | |
Anshuman Khandual | 399145f | 2020-06-04 16:47:15 -0700 | [diff] [blame] | 557 | static unsigned long __init get_random_vaddr(void) |
| 558 | { |
| 559 | unsigned long random_vaddr, random_pages, total_user_pages; |
| 560 | |
| 561 | total_user_pages = (TASK_SIZE - FIRST_USER_ADDRESS) / PAGE_SIZE; |
| 562 | |
| 563 | random_pages = get_random_long() % total_user_pages; |
| 564 | random_vaddr = FIRST_USER_ADDRESS + random_pages * PAGE_SIZE; |
| 565 | |
| 566 | return random_vaddr; |
| 567 | } |
| 568 | |
| 569 | static int __init debug_vm_pgtable(void) |
| 570 | { |
| 571 | struct mm_struct *mm; |
| 572 | pgd_t *pgdp; |
| 573 | p4d_t *p4dp, *saved_p4dp; |
| 574 | pud_t *pudp, *saved_pudp; |
| 575 | pmd_t *pmdp, *saved_pmdp, pmd; |
| 576 | pte_t *ptep; |
| 577 | pgtable_t saved_ptep; |
Anshuman Khandual | 0528940 | 2020-08-06 23:19:16 -0700 | [diff] [blame^] | 578 | pgprot_t prot, protnone; |
Anshuman Khandual | 399145f | 2020-06-04 16:47:15 -0700 | [diff] [blame] | 579 | phys_addr_t paddr; |
| 580 | unsigned long vaddr, pte_aligned, pmd_aligned; |
| 581 | unsigned long pud_aligned, p4d_aligned, pgd_aligned; |
Kees Cook | fea1120 | 2020-06-03 13:28:45 -0700 | [diff] [blame] | 582 | spinlock_t *ptl = NULL; |
Anshuman Khandual | 399145f | 2020-06-04 16:47:15 -0700 | [diff] [blame] | 583 | |
| 584 | pr_info("Validating architecture page table helpers\n"); |
| 585 | prot = vm_get_page_prot(VMFLAGS); |
| 586 | vaddr = get_random_vaddr(); |
| 587 | mm = mm_alloc(); |
| 588 | if (!mm) { |
| 589 | pr_err("mm_struct allocation failed\n"); |
| 590 | return 1; |
| 591 | } |
| 592 | |
| 593 | /* |
Anshuman Khandual | 0528940 | 2020-08-06 23:19:16 -0700 | [diff] [blame^] | 594 | * __P000 (or even __S000) will help create page table entries with |
| 595 | * PROT_NONE permission as required for pxx_protnone_tests(). |
| 596 | */ |
| 597 | protnone = __P000; |
| 598 | |
| 599 | /* |
Anshuman Khandual | 399145f | 2020-06-04 16:47:15 -0700 | [diff] [blame] | 600 | * PFN for mapping at PTE level is determined from a standard kernel |
| 601 | * text symbol. But pfns for higher page table levels are derived by |
| 602 | * masking lower bits of this real pfn. These derived pfns might not |
| 603 | * exist on the platform but that does not really matter as pfn_pxx() |
| 604 | * helpers will still create appropriate entries for the test. This |
| 605 | * helps avoid large memory block allocations to be used for mapping |
| 606 | * at higher page table levels. |
| 607 | */ |
| 608 | paddr = __pa_symbol(&start_kernel); |
| 609 | |
| 610 | pte_aligned = (paddr & PAGE_MASK) >> PAGE_SHIFT; |
| 611 | pmd_aligned = (paddr & PMD_MASK) >> PAGE_SHIFT; |
| 612 | pud_aligned = (paddr & PUD_MASK) >> PAGE_SHIFT; |
| 613 | p4d_aligned = (paddr & P4D_MASK) >> PAGE_SHIFT; |
| 614 | pgd_aligned = (paddr & PGDIR_MASK) >> PAGE_SHIFT; |
| 615 | WARN_ON(!pfn_valid(pte_aligned)); |
| 616 | |
| 617 | pgdp = pgd_offset(mm, vaddr); |
| 618 | p4dp = p4d_alloc(mm, pgdp, vaddr); |
| 619 | pudp = pud_alloc(mm, p4dp, vaddr); |
| 620 | pmdp = pmd_alloc(mm, pudp, vaddr); |
| 621 | ptep = pte_alloc_map_lock(mm, pmdp, vaddr, &ptl); |
| 622 | |
| 623 | /* |
| 624 | * Save all the page table page addresses as the page table |
| 625 | * entries will be used for testing with random or garbage |
| 626 | * values. These saved addresses will be used for freeing |
| 627 | * page table pages. |
| 628 | */ |
| 629 | pmd = READ_ONCE(*pmdp); |
| 630 | saved_p4dp = p4d_offset(pgdp, 0UL); |
| 631 | saved_pudp = pud_offset(p4dp, 0UL); |
| 632 | saved_pmdp = pmd_offset(pudp, 0UL); |
| 633 | saved_ptep = pmd_pgtable(pmd); |
| 634 | |
| 635 | pte_basic_tests(pte_aligned, prot); |
| 636 | pmd_basic_tests(pmd_aligned, prot); |
| 637 | pud_basic_tests(pud_aligned, prot); |
| 638 | p4d_basic_tests(p4d_aligned, prot); |
| 639 | pgd_basic_tests(pgd_aligned, prot); |
| 640 | |
| 641 | pte_clear_tests(mm, ptep, vaddr); |
| 642 | pmd_clear_tests(mm, pmdp); |
| 643 | pud_clear_tests(mm, pudp); |
| 644 | p4d_clear_tests(mm, p4dp); |
| 645 | pgd_clear_tests(mm, pgdp); |
| 646 | |
| 647 | pte_unmap_unlock(ptep, ptl); |
| 648 | |
| 649 | pmd_populate_tests(mm, pmdp, saved_ptep); |
| 650 | pud_populate_tests(mm, pudp, saved_pmdp); |
| 651 | p4d_populate_tests(mm, p4dp, saved_pudp); |
| 652 | pgd_populate_tests(mm, pgdp, saved_p4dp); |
| 653 | |
Anshuman Khandual | 0528940 | 2020-08-06 23:19:16 -0700 | [diff] [blame^] | 654 | pte_special_tests(pte_aligned, prot); |
| 655 | pte_protnone_tests(pte_aligned, protnone); |
| 656 | pmd_protnone_tests(pmd_aligned, protnone); |
| 657 | |
| 658 | pte_devmap_tests(pte_aligned, prot); |
| 659 | pmd_devmap_tests(pmd_aligned, prot); |
| 660 | pud_devmap_tests(pud_aligned, prot); |
| 661 | |
| 662 | pte_soft_dirty_tests(pte_aligned, prot); |
| 663 | pmd_soft_dirty_tests(pmd_aligned, prot); |
| 664 | pte_swap_soft_dirty_tests(pte_aligned, prot); |
| 665 | pmd_swap_soft_dirty_tests(pmd_aligned, prot); |
| 666 | |
| 667 | pte_swap_tests(pte_aligned, prot); |
| 668 | pmd_swap_tests(pmd_aligned, prot); |
| 669 | |
| 670 | swap_migration_tests(); |
| 671 | hugetlb_basic_tests(pte_aligned, prot); |
| 672 | |
| 673 | pmd_thp_tests(pmd_aligned, prot); |
| 674 | pud_thp_tests(pud_aligned, prot); |
| 675 | |
Anshuman Khandual | 399145f | 2020-06-04 16:47:15 -0700 | [diff] [blame] | 676 | p4d_free(mm, saved_p4dp); |
| 677 | pud_free(mm, saved_pudp); |
| 678 | pmd_free(mm, saved_pmdp); |
| 679 | pte_free(mm, saved_ptep); |
| 680 | |
| 681 | mm_dec_nr_puds(mm); |
| 682 | mm_dec_nr_pmds(mm); |
| 683 | mm_dec_nr_ptes(mm); |
| 684 | mmdrop(mm); |
| 685 | return 0; |
| 686 | } |
| 687 | late_initcall(debug_vm_pgtable); |