b.liu | e958203 | 2025-04-17 19:18:16 +0800 | [diff] [blame^] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
| 2 | /* |
| 3 | * arch/arm64/mm/hugetlbpage.c |
| 4 | * |
| 5 | * Copyright (C) 2013 Linaro Ltd. |
| 6 | * |
| 7 | * Based on arch/x86/mm/hugetlbpage.c. |
| 8 | */ |
| 9 | |
| 10 | #include <linux/init.h> |
| 11 | #include <linux/fs.h> |
| 12 | #include <linux/mm.h> |
| 13 | #include <linux/hugetlb.h> |
| 14 | #include <linux/pagemap.h> |
| 15 | #include <linux/err.h> |
| 16 | #include <linux/sysctl.h> |
| 17 | #include <asm/mman.h> |
| 18 | #include <asm/tlb.h> |
| 19 | #include <asm/tlbflush.h> |
| 20 | #include <asm/pgalloc.h> |
| 21 | |
| 22 | #ifdef CONFIG_ARCH_ENABLE_HUGEPAGE_MIGRATION |
| 23 | bool arch_hugetlb_migration_supported(struct hstate *h) |
| 24 | { |
| 25 | size_t pagesize = huge_page_size(h); |
| 26 | |
| 27 | switch (pagesize) { |
| 28 | #ifdef CONFIG_ARM64_4K_PAGES |
| 29 | case PUD_SIZE: |
| 30 | #endif |
| 31 | case PMD_SIZE: |
| 32 | case CONT_PMD_SIZE: |
| 33 | case CONT_PTE_SIZE: |
| 34 | return true; |
| 35 | } |
| 36 | pr_warn("%s: unrecognized huge page size 0x%lx\n", |
| 37 | __func__, pagesize); |
| 38 | return false; |
| 39 | } |
| 40 | #endif |
| 41 | |
| 42 | int pmd_huge(pmd_t pmd) |
| 43 | { |
| 44 | return pmd_val(pmd) && !(pmd_val(pmd) & PMD_TABLE_BIT); |
| 45 | } |
| 46 | |
| 47 | int pud_huge(pud_t pud) |
| 48 | { |
| 49 | #ifndef __PAGETABLE_PMD_FOLDED |
| 50 | return pud_val(pud) && !(pud_val(pud) & PUD_TABLE_BIT); |
| 51 | #else |
| 52 | return 0; |
| 53 | #endif |
| 54 | } |
| 55 | |
| 56 | /* |
| 57 | * Select all bits except the pfn |
| 58 | */ |
| 59 | static inline pgprot_t pte_pgprot(pte_t pte) |
| 60 | { |
| 61 | unsigned long pfn = pte_pfn(pte); |
| 62 | |
| 63 | return __pgprot(pte_val(pfn_pte(pfn, __pgprot(0))) ^ pte_val(pte)); |
| 64 | } |
| 65 | |
| 66 | static int find_num_contig(struct mm_struct *mm, unsigned long addr, |
| 67 | pte_t *ptep, size_t *pgsize) |
| 68 | { |
| 69 | pgd_t *pgdp = pgd_offset(mm, addr); |
| 70 | pud_t *pudp; |
| 71 | pmd_t *pmdp; |
| 72 | |
| 73 | *pgsize = PAGE_SIZE; |
| 74 | pudp = pud_offset(pgdp, addr); |
| 75 | pmdp = pmd_offset(pudp, addr); |
| 76 | if ((pte_t *)pmdp == ptep) { |
| 77 | *pgsize = PMD_SIZE; |
| 78 | return CONT_PMDS; |
| 79 | } |
| 80 | return CONT_PTES; |
| 81 | } |
| 82 | |
| 83 | static inline int num_contig_ptes(unsigned long size, size_t *pgsize) |
| 84 | { |
| 85 | int contig_ptes = 0; |
| 86 | |
| 87 | *pgsize = size; |
| 88 | |
| 89 | switch (size) { |
| 90 | #ifdef CONFIG_ARM64_4K_PAGES |
| 91 | case PUD_SIZE: |
| 92 | #endif |
| 93 | case PMD_SIZE: |
| 94 | contig_ptes = 1; |
| 95 | break; |
| 96 | case CONT_PMD_SIZE: |
| 97 | *pgsize = PMD_SIZE; |
| 98 | contig_ptes = CONT_PMDS; |
| 99 | break; |
| 100 | case CONT_PTE_SIZE: |
| 101 | *pgsize = PAGE_SIZE; |
| 102 | contig_ptes = CONT_PTES; |
| 103 | break; |
| 104 | } |
| 105 | |
| 106 | return contig_ptes; |
| 107 | } |
| 108 | |
| 109 | /* |
| 110 | * Changing some bits of contiguous entries requires us to follow a |
| 111 | * Break-Before-Make approach, breaking the whole contiguous set |
| 112 | * before we can change any entries. See ARM DDI 0487A.k_iss10775, |
| 113 | * "Misprogramming of the Contiguous bit", page D4-1762. |
| 114 | * |
| 115 | * This helper performs the break step. |
| 116 | */ |
| 117 | static pte_t get_clear_flush(struct mm_struct *mm, |
| 118 | unsigned long addr, |
| 119 | pte_t *ptep, |
| 120 | unsigned long pgsize, |
| 121 | unsigned long ncontig) |
| 122 | { |
| 123 | pte_t orig_pte = huge_ptep_get(ptep); |
| 124 | bool valid = pte_valid(orig_pte); |
| 125 | unsigned long i, saddr = addr; |
| 126 | |
| 127 | for (i = 0; i < ncontig; i++, addr += pgsize, ptep++) { |
| 128 | pte_t pte = ptep_get_and_clear(mm, addr, ptep); |
| 129 | |
| 130 | /* |
| 131 | * If HW_AFDBM is enabled, then the HW could turn on |
| 132 | * the dirty or accessed bit for any page in the set, |
| 133 | * so check them all. |
| 134 | */ |
| 135 | if (pte_dirty(pte)) |
| 136 | orig_pte = pte_mkdirty(orig_pte); |
| 137 | |
| 138 | if (pte_young(pte)) |
| 139 | orig_pte = pte_mkyoung(orig_pte); |
| 140 | } |
| 141 | |
| 142 | if (valid) { |
| 143 | struct vm_area_struct vma = TLB_FLUSH_VMA(mm, 0); |
| 144 | flush_tlb_range(&vma, saddr, addr); |
| 145 | } |
| 146 | return orig_pte; |
| 147 | } |
| 148 | |
| 149 | /* |
| 150 | * Changing some bits of contiguous entries requires us to follow a |
| 151 | * Break-Before-Make approach, breaking the whole contiguous set |
| 152 | * before we can change any entries. See ARM DDI 0487A.k_iss10775, |
| 153 | * "Misprogramming of the Contiguous bit", page D4-1762. |
| 154 | * |
| 155 | * This helper performs the break step for use cases where the |
| 156 | * original pte is not needed. |
| 157 | */ |
| 158 | static void clear_flush(struct mm_struct *mm, |
| 159 | unsigned long addr, |
| 160 | pte_t *ptep, |
| 161 | unsigned long pgsize, |
| 162 | unsigned long ncontig) |
| 163 | { |
| 164 | struct vm_area_struct vma = TLB_FLUSH_VMA(mm, 0); |
| 165 | unsigned long i, saddr = addr; |
| 166 | |
| 167 | for (i = 0; i < ncontig; i++, addr += pgsize, ptep++) |
| 168 | pte_clear(mm, addr, ptep); |
| 169 | |
| 170 | flush_tlb_range(&vma, saddr, addr); |
| 171 | } |
| 172 | |
| 173 | void set_huge_pte_at(struct mm_struct *mm, unsigned long addr, |
| 174 | pte_t *ptep, pte_t pte) |
| 175 | { |
| 176 | size_t pgsize; |
| 177 | int i; |
| 178 | int ncontig; |
| 179 | unsigned long pfn, dpfn; |
| 180 | pgprot_t hugeprot; |
| 181 | |
| 182 | /* |
| 183 | * Code needs to be expanded to handle huge swap and migration |
| 184 | * entries. Needed for HUGETLB and MEMORY_FAILURE. |
| 185 | */ |
| 186 | WARN_ON(!pte_present(pte)); |
| 187 | |
| 188 | if (!pte_cont(pte)) { |
| 189 | set_pte_at(mm, addr, ptep, pte); |
| 190 | return; |
| 191 | } |
| 192 | |
| 193 | ncontig = find_num_contig(mm, addr, ptep, &pgsize); |
| 194 | pfn = pte_pfn(pte); |
| 195 | dpfn = pgsize >> PAGE_SHIFT; |
| 196 | hugeprot = pte_pgprot(pte); |
| 197 | |
| 198 | clear_flush(mm, addr, ptep, pgsize, ncontig); |
| 199 | |
| 200 | for (i = 0; i < ncontig; i++, ptep++, addr += pgsize, pfn += dpfn) |
| 201 | set_pte_at(mm, addr, ptep, pfn_pte(pfn, hugeprot)); |
| 202 | } |
| 203 | |
| 204 | void set_huge_swap_pte_at(struct mm_struct *mm, unsigned long addr, |
| 205 | pte_t *ptep, pte_t pte, unsigned long sz) |
| 206 | { |
| 207 | int i, ncontig; |
| 208 | size_t pgsize; |
| 209 | |
| 210 | ncontig = num_contig_ptes(sz, &pgsize); |
| 211 | |
| 212 | for (i = 0; i < ncontig; i++, ptep++) |
| 213 | set_pte(ptep, pte); |
| 214 | } |
| 215 | |
| 216 | pte_t *huge_pte_alloc(struct mm_struct *mm, struct vm_area_struct *vma, |
| 217 | unsigned long addr, unsigned long sz) |
| 218 | { |
| 219 | pgd_t *pgdp; |
| 220 | pud_t *pudp; |
| 221 | pmd_t *pmdp; |
| 222 | pte_t *ptep = NULL; |
| 223 | |
| 224 | pgdp = pgd_offset(mm, addr); |
| 225 | pudp = pud_alloc(mm, pgdp, addr); |
| 226 | if (!pudp) |
| 227 | return NULL; |
| 228 | |
| 229 | if (sz == PUD_SIZE) { |
| 230 | ptep = (pte_t *)pudp; |
| 231 | } else if (sz == (CONT_PTE_SIZE)) { |
| 232 | pmdp = pmd_alloc(mm, pudp, addr); |
| 233 | if (!pmdp) |
| 234 | return NULL; |
| 235 | |
| 236 | WARN_ON(addr & (sz - 1)); |
| 237 | /* |
| 238 | * Note that if this code were ever ported to the |
| 239 | * 32-bit arm platform then it will cause trouble in |
| 240 | * the case where CONFIG_HIGHPTE is set, since there |
| 241 | * will be no pte_unmap() to correspond with this |
| 242 | * pte_alloc_map(). |
| 243 | */ |
| 244 | ptep = pte_alloc_map(mm, pmdp, addr); |
| 245 | } else if (sz == PMD_SIZE) { |
| 246 | if (want_pmd_share(vma, addr) && pud_none(READ_ONCE(*pudp))) |
| 247 | ptep = huge_pmd_share(mm, vma, addr, pudp); |
| 248 | else |
| 249 | ptep = (pte_t *)pmd_alloc(mm, pudp, addr); |
| 250 | } else if (sz == (CONT_PMD_SIZE)) { |
| 251 | pmdp = pmd_alloc(mm, pudp, addr); |
| 252 | WARN_ON(addr & (sz - 1)); |
| 253 | return (pte_t *)pmdp; |
| 254 | } |
| 255 | |
| 256 | return ptep; |
| 257 | } |
| 258 | |
| 259 | pte_t *huge_pte_offset(struct mm_struct *mm, |
| 260 | unsigned long addr, unsigned long sz) |
| 261 | { |
| 262 | pgd_t *pgdp; |
| 263 | pud_t *pudp, pud; |
| 264 | pmd_t *pmdp, pmd; |
| 265 | |
| 266 | pgdp = pgd_offset(mm, addr); |
| 267 | if (!pgd_present(READ_ONCE(*pgdp))) |
| 268 | return NULL; |
| 269 | |
| 270 | pudp = pud_offset(pgdp, addr); |
| 271 | pud = READ_ONCE(*pudp); |
| 272 | if (sz != PUD_SIZE && pud_none(pud)) |
| 273 | return NULL; |
| 274 | /* hugepage or swap? */ |
| 275 | if (pud_huge(pud) || !pud_present(pud)) |
| 276 | return (pte_t *)pudp; |
| 277 | /* table; check the next level */ |
| 278 | |
| 279 | if (sz == CONT_PMD_SIZE) |
| 280 | addr &= CONT_PMD_MASK; |
| 281 | |
| 282 | pmdp = pmd_offset(pudp, addr); |
| 283 | pmd = READ_ONCE(*pmdp); |
| 284 | if (!(sz == PMD_SIZE || sz == CONT_PMD_SIZE) && |
| 285 | pmd_none(pmd)) |
| 286 | return NULL; |
| 287 | if (pmd_huge(pmd) || !pmd_present(pmd)) |
| 288 | return (pte_t *)pmdp; |
| 289 | |
| 290 | if (sz == CONT_PTE_SIZE) |
| 291 | return pte_offset_kernel(pmdp, (addr & CONT_PTE_MASK)); |
| 292 | |
| 293 | return NULL; |
| 294 | } |
| 295 | |
| 296 | pte_t arch_make_huge_pte(pte_t entry, struct vm_area_struct *vma, |
| 297 | struct page *page, int writable) |
| 298 | { |
| 299 | size_t pagesize = huge_page_size(hstate_vma(vma)); |
| 300 | |
| 301 | if (pagesize == CONT_PTE_SIZE) { |
| 302 | entry = pte_mkcont(entry); |
| 303 | } else if (pagesize == CONT_PMD_SIZE) { |
| 304 | entry = pmd_pte(pmd_mkcont(pte_pmd(entry))); |
| 305 | } else if (pagesize != PUD_SIZE && pagesize != PMD_SIZE) { |
| 306 | pr_warn("%s: unrecognized huge page size 0x%lx\n", |
| 307 | __func__, pagesize); |
| 308 | } |
| 309 | return entry; |
| 310 | } |
| 311 | |
| 312 | void huge_pte_clear(struct mm_struct *mm, unsigned long addr, |
| 313 | pte_t *ptep, unsigned long sz) |
| 314 | { |
| 315 | int i, ncontig; |
| 316 | size_t pgsize; |
| 317 | |
| 318 | ncontig = num_contig_ptes(sz, &pgsize); |
| 319 | |
| 320 | for (i = 0; i < ncontig; i++, addr += pgsize, ptep++) |
| 321 | pte_clear(mm, addr, ptep); |
| 322 | } |
| 323 | |
| 324 | pte_t huge_ptep_get_and_clear(struct mm_struct *mm, |
| 325 | unsigned long addr, pte_t *ptep) |
| 326 | { |
| 327 | int ncontig; |
| 328 | size_t pgsize; |
| 329 | pte_t orig_pte = huge_ptep_get(ptep); |
| 330 | |
| 331 | if (!pte_cont(orig_pte)) |
| 332 | return ptep_get_and_clear(mm, addr, ptep); |
| 333 | |
| 334 | ncontig = find_num_contig(mm, addr, ptep, &pgsize); |
| 335 | |
| 336 | return get_clear_flush(mm, addr, ptep, pgsize, ncontig); |
| 337 | } |
| 338 | |
| 339 | /* |
| 340 | * huge_ptep_set_access_flags will update access flags (dirty, accesssed) |
| 341 | * and write permission. |
| 342 | * |
| 343 | * For a contiguous huge pte range we need to check whether or not write |
| 344 | * permission has to change only on the first pte in the set. Then for |
| 345 | * all the contiguous ptes we need to check whether or not there is a |
| 346 | * discrepancy between dirty or young. |
| 347 | */ |
| 348 | static int __cont_access_flags_changed(pte_t *ptep, pte_t pte, int ncontig) |
| 349 | { |
| 350 | int i; |
| 351 | |
| 352 | if (pte_write(pte) != pte_write(huge_ptep_get(ptep))) |
| 353 | return 1; |
| 354 | |
| 355 | for (i = 0; i < ncontig; i++) { |
| 356 | pte_t orig_pte = huge_ptep_get(ptep + i); |
| 357 | |
| 358 | if (pte_dirty(pte) != pte_dirty(orig_pte)) |
| 359 | return 1; |
| 360 | |
| 361 | if (pte_young(pte) != pte_young(orig_pte)) |
| 362 | return 1; |
| 363 | } |
| 364 | |
| 365 | return 0; |
| 366 | } |
| 367 | |
| 368 | int huge_ptep_set_access_flags(struct vm_area_struct *vma, |
| 369 | unsigned long addr, pte_t *ptep, |
| 370 | pte_t pte, int dirty) |
| 371 | { |
| 372 | int ncontig, i; |
| 373 | size_t pgsize = 0; |
| 374 | unsigned long pfn = pte_pfn(pte), dpfn; |
| 375 | pgprot_t hugeprot; |
| 376 | pte_t orig_pte; |
| 377 | |
| 378 | if (!pte_cont(pte)) |
| 379 | return ptep_set_access_flags(vma, addr, ptep, pte, dirty); |
| 380 | |
| 381 | ncontig = find_num_contig(vma->vm_mm, addr, ptep, &pgsize); |
| 382 | dpfn = pgsize >> PAGE_SHIFT; |
| 383 | |
| 384 | if (!__cont_access_flags_changed(ptep, pte, ncontig)) |
| 385 | return 0; |
| 386 | |
| 387 | orig_pte = get_clear_flush(vma->vm_mm, addr, ptep, pgsize, ncontig); |
| 388 | |
| 389 | /* Make sure we don't lose the dirty or young state */ |
| 390 | if (pte_dirty(orig_pte)) |
| 391 | pte = pte_mkdirty(pte); |
| 392 | |
| 393 | if (pte_young(orig_pte)) |
| 394 | pte = pte_mkyoung(pte); |
| 395 | |
| 396 | hugeprot = pte_pgprot(pte); |
| 397 | for (i = 0; i < ncontig; i++, ptep++, addr += pgsize, pfn += dpfn) |
| 398 | set_pte_at(vma->vm_mm, addr, ptep, pfn_pte(pfn, hugeprot)); |
| 399 | |
| 400 | return 1; |
| 401 | } |
| 402 | |
| 403 | void huge_ptep_set_wrprotect(struct mm_struct *mm, |
| 404 | unsigned long addr, pte_t *ptep) |
| 405 | { |
| 406 | unsigned long pfn, dpfn; |
| 407 | pgprot_t hugeprot; |
| 408 | int ncontig, i; |
| 409 | size_t pgsize; |
| 410 | pte_t pte; |
| 411 | |
| 412 | if (!pte_cont(READ_ONCE(*ptep))) { |
| 413 | ptep_set_wrprotect(mm, addr, ptep); |
| 414 | return; |
| 415 | } |
| 416 | |
| 417 | ncontig = find_num_contig(mm, addr, ptep, &pgsize); |
| 418 | dpfn = pgsize >> PAGE_SHIFT; |
| 419 | |
| 420 | pte = get_clear_flush(mm, addr, ptep, pgsize, ncontig); |
| 421 | pte = pte_wrprotect(pte); |
| 422 | |
| 423 | hugeprot = pte_pgprot(pte); |
| 424 | pfn = pte_pfn(pte); |
| 425 | |
| 426 | for (i = 0; i < ncontig; i++, ptep++, addr += pgsize, pfn += dpfn) |
| 427 | set_pte_at(mm, addr, ptep, pfn_pte(pfn, hugeprot)); |
| 428 | } |
| 429 | |
| 430 | void huge_ptep_clear_flush(struct vm_area_struct *vma, |
| 431 | unsigned long addr, pte_t *ptep) |
| 432 | { |
| 433 | size_t pgsize; |
| 434 | int ncontig; |
| 435 | |
| 436 | if (!pte_cont(READ_ONCE(*ptep))) { |
| 437 | ptep_clear_flush(vma, addr, ptep); |
| 438 | return; |
| 439 | } |
| 440 | |
| 441 | ncontig = find_num_contig(vma->vm_mm, addr, ptep, &pgsize); |
| 442 | clear_flush(vma->vm_mm, addr, ptep, pgsize, ncontig); |
| 443 | } |
| 444 | |
| 445 | static void __init add_huge_page_size(unsigned long size) |
| 446 | { |
| 447 | if (size_to_hstate(size)) |
| 448 | return; |
| 449 | |
| 450 | hugetlb_add_hstate(ilog2(size) - PAGE_SHIFT); |
| 451 | } |
| 452 | |
| 453 | static int __init hugetlbpage_init(void) |
| 454 | { |
| 455 | #ifdef CONFIG_ARM64_4K_PAGES |
| 456 | add_huge_page_size(PUD_SIZE); |
| 457 | #endif |
| 458 | add_huge_page_size(CONT_PMD_SIZE); |
| 459 | add_huge_page_size(PMD_SIZE); |
| 460 | add_huge_page_size(CONT_PTE_SIZE); |
| 461 | |
| 462 | return 0; |
| 463 | } |
| 464 | arch_initcall(hugetlbpage_init); |
| 465 | |
| 466 | static __init int setup_hugepagesz(char *opt) |
| 467 | { |
| 468 | unsigned long ps = memparse(opt, &opt); |
| 469 | |
| 470 | switch (ps) { |
| 471 | #ifdef CONFIG_ARM64_4K_PAGES |
| 472 | case PUD_SIZE: |
| 473 | #endif |
| 474 | case CONT_PMD_SIZE: |
| 475 | case PMD_SIZE: |
| 476 | case CONT_PTE_SIZE: |
| 477 | add_huge_page_size(ps); |
| 478 | return 1; |
| 479 | } |
| 480 | |
| 481 | hugetlb_bad_size(); |
| 482 | pr_err("hugepagesz: Unsupported page size %lu K\n", ps >> 10); |
| 483 | return 0; |
| 484 | } |
| 485 | __setup("hugepagesz=", setup_hugepagesz); |