1 // SPDX-License-Identifier: GPL-2.0 2 #include <linux/pagewalk.h> 3 #include <linux/highmem.h> 4 #include <linux/sched.h> 5 #include <linux/hugetlb.h> 6 #include <linux/mmu_context.h> 7 #include <linux/swap.h> 8 9 #include <asm/tlbflush.h> 10 11 #include "internal.h" 12 13 /* 14 * We want to know the real level where a entry is located ignoring any 15 * folding of levels which may be happening. For example if p4d is folded then 16 * a missing entry found at level 1 (p4d) is actually at level 0 (pgd). 17 */ 18 static int real_depth(int depth) 19 { 20 if (depth == 3 && PTRS_PER_PMD == 1) 21 depth = 2; 22 if (depth == 2 && PTRS_PER_PUD == 1) 23 depth = 1; 24 if (depth == 1 && PTRS_PER_P4D == 1) 25 depth = 0; 26 return depth; 27 } 28 29 static int walk_pte_range_inner(pte_t *pte, unsigned long addr, 30 unsigned long end, struct mm_walk *walk) 31 { 32 const struct mm_walk_ops *ops = walk->ops; 33 int err = 0; 34 35 for (;;) { 36 if (ops->install_pte && pte_none(ptep_get(pte))) { 37 pte_t new_pte; 38 39 err = ops->install_pte(addr, addr + PAGE_SIZE, &new_pte, 40 walk); 41 if (err) 42 break; 43 44 set_pte_at(walk->mm, addr, pte, new_pte); 45 /* Non-present before, so for arches that need it. */ 46 if (!WARN_ON_ONCE(walk->no_vma)) 47 update_mmu_cache(walk->vma, addr, pte); 48 } else { 49 err = ops->pte_entry(pte, addr, addr + PAGE_SIZE, walk); 50 if (err) 51 break; 52 } 53 if (addr >= end - PAGE_SIZE) 54 break; 55 addr += PAGE_SIZE; 56 pte++; 57 } 58 return err; 59 } 60 61 static int walk_pte_range(pmd_t *pmd, unsigned long addr, unsigned long end, 62 struct mm_walk *walk) 63 { 64 pte_t *pte; 65 int err = 0; 66 spinlock_t *ptl; 67 68 if (walk->no_vma) { 69 /* 70 * pte_offset_map() might apply user-specific validation. 71 * Indeed, on x86_64 the pmd entries set up by init_espfix_ap() 72 * fit its pmd_bad() check (_PAGE_NX set and _PAGE_RW clear), 73 * and CONFIG_EFI_PGT_DUMP efi_mm goes so far as to walk them. 74 */ 75 if (walk->mm == &init_mm || addr >= TASK_SIZE) 76 pte = pte_offset_kernel(pmd, addr); 77 else 78 pte = pte_offset_map(pmd, addr); 79 if (pte) { 80 err = walk_pte_range_inner(pte, addr, end, walk); 81 if (walk->mm != &init_mm && addr < TASK_SIZE) 82 pte_unmap(pte); 83 } 84 } else { 85 pte = pte_offset_map_lock(walk->mm, pmd, addr, &ptl); 86 if (pte) { 87 err = walk_pte_range_inner(pte, addr, end, walk); 88 pte_unmap_unlock(pte, ptl); 89 } 90 } 91 if (!pte) 92 walk->action = ACTION_AGAIN; 93 return err; 94 } 95 96 static int walk_pmd_range(pud_t *pud, unsigned long addr, unsigned long end, 97 struct mm_walk *walk) 98 { 99 pmd_t *pmd; 100 unsigned long next; 101 const struct mm_walk_ops *ops = walk->ops; 102 bool has_handler = ops->pte_entry; 103 bool has_install = ops->install_pte; 104 int err = 0; 105 int depth = real_depth(3); 106 107 pmd = pmd_offset(pud, addr); 108 do { 109 again: 110 next = pmd_addr_end(addr, end); 111 if (pmd_none(*pmd)) { 112 if (has_install) 113 err = __pte_alloc(walk->mm, pmd); 114 else if (ops->pte_hole) 115 err = ops->pte_hole(addr, next, depth, walk); 116 if (err) 117 break; 118 if (!has_install) 119 continue; 120 } 121 122 walk->action = ACTION_SUBTREE; 123 124 /* 125 * This implies that each ->pmd_entry() handler 126 * needs to know about pmd_trans_huge() pmds 127 */ 128 if (ops->pmd_entry) 129 err = ops->pmd_entry(pmd, addr, next, walk); 130 if (err) 131 break; 132 133 if (walk->action == ACTION_AGAIN) 134 goto again; 135 if (walk->action == ACTION_CONTINUE) 136 continue; 137 138 if (!has_handler) { /* No handlers for lower page tables. */ 139 if (!has_install) 140 continue; /* Nothing to do. */ 141 /* 142 * We are ONLY installing, so avoid unnecessarily 143 * splitting a present huge page. 144 */ 145 if (pmd_present(*pmd) && pmd_trans_huge(*pmd)) 146 continue; 147 } 148 149 if (walk->vma) 150 split_huge_pmd(walk->vma, pmd, addr); 151 else if (pmd_leaf(*pmd) || !pmd_present(*pmd)) 152 continue; /* Nothing to do. */ 153 154 err = walk_pte_range(pmd, addr, next, walk); 155 if (err) 156 break; 157 158 if (walk->action == ACTION_AGAIN) 159 goto again; 160 161 } while (pmd++, addr = next, addr != end); 162 163 return err; 164 } 165 166 static int walk_pud_range(p4d_t *p4d, unsigned long addr, unsigned long end, 167 struct mm_walk *walk) 168 { 169 pud_t *pud; 170 unsigned long next; 171 const struct mm_walk_ops *ops = walk->ops; 172 bool has_handler = ops->pmd_entry || ops->pte_entry; 173 bool has_install = ops->install_pte; 174 int err = 0; 175 int depth = real_depth(2); 176 177 pud = pud_offset(p4d, addr); 178 do { 179 again: 180 next = pud_addr_end(addr, end); 181 if (pud_none(*pud)) { 182 if (has_install) 183 err = __pmd_alloc(walk->mm, pud, addr); 184 else if (ops->pte_hole) 185 err = ops->pte_hole(addr, next, depth, walk); 186 if (err) 187 break; 188 if (!has_install) 189 continue; 190 } 191 192 walk->action = ACTION_SUBTREE; 193 194 if (ops->pud_entry) 195 err = ops->pud_entry(pud, addr, next, walk); 196 if (err) 197 break; 198 199 if (walk->action == ACTION_AGAIN) 200 goto again; 201 if (walk->action == ACTION_CONTINUE) 202 continue; 203 204 if (!has_handler) { /* No handlers for lower page tables. */ 205 if (!has_install) 206 continue; /* Nothing to do. */ 207 /* 208 * We are ONLY installing, so avoid unnecessarily 209 * splitting a present huge page. 210 */ 211 if (pud_present(*pud) && pud_trans_huge(*pud)) 212 continue; 213 } 214 215 if (walk->vma) 216 split_huge_pud(walk->vma, pud, addr); 217 else if (pud_leaf(*pud) || !pud_present(*pud)) 218 continue; /* Nothing to do. */ 219 220 if (pud_none(*pud)) 221 goto again; 222 223 err = walk_pmd_range(pud, addr, next, walk); 224 if (err) 225 break; 226 } while (pud++, addr = next, addr != end); 227 228 return err; 229 } 230 231 static int walk_p4d_range(pgd_t *pgd, unsigned long addr, unsigned long end, 232 struct mm_walk *walk) 233 { 234 p4d_t *p4d; 235 unsigned long next; 236 const struct mm_walk_ops *ops = walk->ops; 237 bool has_handler = ops->pud_entry || ops->pmd_entry || ops->pte_entry; 238 bool has_install = ops->install_pte; 239 int err = 0; 240 int depth = real_depth(1); 241 242 p4d = p4d_offset(pgd, addr); 243 do { 244 next = p4d_addr_end(addr, end); 245 if (p4d_none_or_clear_bad(p4d)) { 246 if (has_install) 247 err = __pud_alloc(walk->mm, p4d, addr); 248 else if (ops->pte_hole) 249 err = ops->pte_hole(addr, next, depth, walk); 250 if (err) 251 break; 252 if (!has_install) 253 continue; 254 } 255 if (ops->p4d_entry) { 256 err = ops->p4d_entry(p4d, addr, next, walk); 257 if (err) 258 break; 259 } 260 if (has_handler || has_install) 261 err = walk_pud_range(p4d, addr, next, walk); 262 if (err) 263 break; 264 } while (p4d++, addr = next, addr != end); 265 266 return err; 267 } 268 269 static int walk_pgd_range(unsigned long addr, unsigned long end, 270 struct mm_walk *walk) 271 { 272 pgd_t *pgd; 273 unsigned long next; 274 const struct mm_walk_ops *ops = walk->ops; 275 bool has_handler = ops->p4d_entry || ops->pud_entry || ops->pmd_entry || 276 ops->pte_entry; 277 bool has_install = ops->install_pte; 278 int err = 0; 279 280 if (walk->pgd) 281 pgd = walk->pgd + pgd_index(addr); 282 else 283 pgd = pgd_offset(walk->mm, addr); 284 do { 285 next = pgd_addr_end(addr, end); 286 if (pgd_none_or_clear_bad(pgd)) { 287 if (has_install) 288 err = __p4d_alloc(walk->mm, pgd, addr); 289 else if (ops->pte_hole) 290 err = ops->pte_hole(addr, next, 0, walk); 291 if (err) 292 break; 293 if (!has_install) 294 continue; 295 } 296 if (ops->pgd_entry) { 297 err = ops->pgd_entry(pgd, addr, next, walk); 298 if (err) 299 break; 300 } 301 if (has_handler || has_install) 302 err = walk_p4d_range(pgd, addr, next, walk); 303 if (err) 304 break; 305 } while (pgd++, addr = next, addr != end); 306 307 return err; 308 } 309 310 #ifdef CONFIG_HUGETLB_PAGE 311 static unsigned long hugetlb_entry_end(struct hstate *h, unsigned long addr, 312 unsigned long end) 313 { 314 unsigned long boundary = (addr & huge_page_mask(h)) + huge_page_size(h); 315 316 return min(boundary, end); 317 } 318 319 static int walk_hugetlb_range(unsigned long addr, unsigned long end, 320 struct mm_walk *walk) 321 { 322 struct vm_area_struct *vma = walk->vma; 323 struct hstate *h = hstate_vma(vma); 324 unsigned long next; 325 unsigned long hmask = huge_page_mask(h); 326 unsigned long sz = huge_page_size(h); 327 pte_t *pte; 328 const struct mm_walk_ops *ops = walk->ops; 329 int err = 0; 330 331 hugetlb_vma_lock_read(vma); 332 do { 333 next = hugetlb_entry_end(h, addr, end); 334 pte = hugetlb_walk(vma, addr & hmask, sz); 335 if (pte) 336 err = ops->hugetlb_entry(pte, hmask, addr, next, walk); 337 else if (ops->pte_hole) 338 err = ops->pte_hole(addr, next, -1, walk); 339 if (err) 340 break; 341 } while (addr = next, addr != end); 342 hugetlb_vma_unlock_read(vma); 343 344 return err; 345 } 346 347 #else /* CONFIG_HUGETLB_PAGE */ 348 static int walk_hugetlb_range(unsigned long addr, unsigned long end, 349 struct mm_walk *walk) 350 { 351 return 0; 352 } 353 354 #endif /* CONFIG_HUGETLB_PAGE */ 355 356 /* 357 * Decide whether we really walk over the current vma on [@start, @end) 358 * or skip it via the returned value. Return 0 if we do walk over the 359 * current vma, and return 1 if we skip the vma. Negative values means 360 * error, where we abort the current walk. 361 */ 362 static int walk_page_test(unsigned long start, unsigned long end, 363 struct mm_walk *walk) 364 { 365 struct vm_area_struct *vma = walk->vma; 366 const struct mm_walk_ops *ops = walk->ops; 367 368 if (ops->test_walk) 369 return ops->test_walk(start, end, walk); 370 371 /* 372 * vma(VM_PFNMAP) doesn't have any valid struct pages behind VM_PFNMAP 373 * range, so we don't walk over it as we do for normal vmas. However, 374 * Some callers are interested in handling hole range and they don't 375 * want to just ignore any single address range. Such users certainly 376 * define their ->pte_hole() callbacks, so let's delegate them to handle 377 * vma(VM_PFNMAP). 378 */ 379 if (vma->vm_flags & VM_PFNMAP) { 380 int err = 1; 381 if (ops->pte_hole) 382 err = ops->pte_hole(start, end, -1, walk); 383 return err ? err : 1; 384 } 385 return 0; 386 } 387 388 static int __walk_page_range(unsigned long start, unsigned long end, 389 struct mm_walk *walk) 390 { 391 int err = 0; 392 struct vm_area_struct *vma = walk->vma; 393 const struct mm_walk_ops *ops = walk->ops; 394 bool is_hugetlb = is_vm_hugetlb_page(vma); 395 396 /* We do not support hugetlb PTE installation. */ 397 if (ops->install_pte && is_hugetlb) 398 return -EINVAL; 399 400 if (ops->pre_vma) { 401 err = ops->pre_vma(start, end, walk); 402 if (err) 403 return err; 404 } 405 406 if (is_hugetlb) { 407 if (ops->hugetlb_entry) 408 err = walk_hugetlb_range(start, end, walk); 409 } else 410 err = walk_pgd_range(start, end, walk); 411 412 if (ops->post_vma) 413 ops->post_vma(walk); 414 415 return err; 416 } 417 418 static inline void process_mm_walk_lock(struct mm_struct *mm, 419 enum page_walk_lock walk_lock) 420 { 421 if (walk_lock == PGWALK_RDLOCK) 422 mmap_assert_locked(mm); 423 else if (walk_lock != PGWALK_VMA_RDLOCK_VERIFY) 424 mmap_assert_write_locked(mm); 425 } 426 427 static inline void process_vma_walk_lock(struct vm_area_struct *vma, 428 enum page_walk_lock walk_lock) 429 { 430 #ifdef CONFIG_PER_VMA_LOCK 431 switch (walk_lock) { 432 case PGWALK_WRLOCK: 433 vma_start_write(vma); 434 break; 435 case PGWALK_WRLOCK_VERIFY: 436 vma_assert_write_locked(vma); 437 break; 438 case PGWALK_VMA_RDLOCK_VERIFY: 439 vma_assert_locked(vma); 440 break; 441 case PGWALK_RDLOCK: 442 /* PGWALK_RDLOCK is handled by process_mm_walk_lock */ 443 break; 444 } 445 #endif 446 } 447 448 /* 449 * See the comment for walk_page_range(), this performs the heavy lifting of the 450 * operation, only sets no restrictions on how the walk proceeds. 451 * 452 * We usually restrict the ability to install PTEs, but this functionality is 453 * available to internal memory management code and provided in mm/internal.h. 454 */ 455 int walk_page_range_mm_unsafe(struct mm_struct *mm, unsigned long start, 456 unsigned long end, const struct mm_walk_ops *ops, 457 void *private) 458 { 459 int err = 0; 460 unsigned long next; 461 struct vm_area_struct *vma; 462 struct mm_walk walk = { 463 .ops = ops, 464 .mm = mm, 465 .private = private, 466 }; 467 468 if (start >= end) 469 return -EINVAL; 470 471 if (!walk.mm) 472 return -EINVAL; 473 474 process_mm_walk_lock(walk.mm, ops->walk_lock); 475 476 vma = find_vma(walk.mm, start); 477 do { 478 if (!vma) { /* after the last vma */ 479 walk.vma = NULL; 480 next = end; 481 if (ops->pte_hole) 482 err = ops->pte_hole(start, next, -1, &walk); 483 } else if (start < vma->vm_start) { /* outside vma */ 484 walk.vma = NULL; 485 next = min(end, vma->vm_start); 486 if (ops->pte_hole) 487 err = ops->pte_hole(start, next, -1, &walk); 488 } else { /* inside vma */ 489 process_vma_walk_lock(vma, ops->walk_lock); 490 walk.vma = vma; 491 next = min(end, vma->vm_end); 492 vma = find_vma(mm, vma->vm_end); 493 494 err = walk_page_test(start, next, &walk); 495 if (err > 0) { 496 /* 497 * positive return values are purely for 498 * controlling the pagewalk, so should never 499 * be passed to the callers. 500 */ 501 err = 0; 502 continue; 503 } 504 if (err < 0) 505 break; 506 err = __walk_page_range(start, next, &walk); 507 } 508 if (err) 509 break; 510 } while (start = next, start < end); 511 return err; 512 } 513 514 /* 515 * Determine if the walk operations specified are permitted to be used for a 516 * page table walk. 517 * 518 * This check is performed on all functions which are parameterised by walk 519 * operations and exposed in include/linux/pagewalk.h. 520 * 521 * Internal memory management code can use *_unsafe() functions to be able to 522 * use all page walking operations. 523 */ 524 static bool check_ops_safe(const struct mm_walk_ops *ops) 525 { 526 /* 527 * The installation of PTEs is solely under the control of memory 528 * management logic and subject to many subtle locking, security and 529 * cache considerations so we cannot permit other users to do so, and 530 * certainly not for exported symbols. 531 */ 532 if (ops->install_pte) 533 return false; 534 535 return true; 536 } 537 538 /** 539 * walk_page_range - walk page table with caller specific callbacks 540 * @mm: mm_struct representing the target process of page table walk 541 * @start: start address of the virtual address range 542 * @end: end address of the virtual address range 543 * @ops: operation to call during the walk 544 * @private: private data for callbacks' usage 545 * 546 * Recursively walk the page table tree of the process represented by @mm 547 * within the virtual address range [@start, @end). During walking, we can do 548 * some caller-specific works for each entry, by setting up pmd_entry(), 549 * pte_entry(), and/or hugetlb_entry(). If you don't set up for some of these 550 * callbacks, the associated entries/pages are just ignored. 551 * The return values of these callbacks are commonly defined like below: 552 * 553 * - 0 : succeeded to handle the current entry, and if you don't reach the 554 * end address yet, continue to walk. 555 * - >0 : succeeded to handle the current entry, and return to the caller 556 * with caller specific value. 557 * - <0 : failed to handle the current entry, and return to the caller 558 * with error code. 559 * 560 * Before starting to walk page table, some callers want to check whether 561 * they really want to walk over the current vma, typically by checking 562 * its vm_flags. walk_page_test() and @ops->test_walk() are used for this 563 * purpose. 564 * 565 * If operations need to be staged before and committed after a vma is walked, 566 * there are two callbacks, pre_vma() and post_vma(). Note that post_vma(), 567 * since it is intended to handle commit-type operations, can't return any 568 * errors. 569 * 570 * struct mm_walk keeps current values of some common data like vma and pmd, 571 * which are useful for the access from callbacks. If you want to pass some 572 * caller-specific data to callbacks, @private should be helpful. 573 * 574 * Locking: 575 * Callers of walk_page_range() and walk_page_vma() should hold @mm->mmap_lock, 576 * because these function traverse vma list and/or access to vma's data. 577 */ 578 int walk_page_range(struct mm_struct *mm, unsigned long start, 579 unsigned long end, const struct mm_walk_ops *ops, 580 void *private) 581 { 582 if (!check_ops_safe(ops)) 583 return -EINVAL; 584 585 return walk_page_range_mm_unsafe(mm, start, end, ops, private); 586 } 587 588 /** 589 * walk_kernel_page_table_range - walk a range of kernel pagetables. 590 * @start: start address of the virtual address range 591 * @end: end address of the virtual address range 592 * @ops: operation to call during the walk 593 * @pgd: pgd to walk if different from mm->pgd 594 * @private: private data for callbacks' usage 595 * 596 * Similar to walk_page_range() but can walk any page tables even if they are 597 * not backed by VMAs. Because 'unusual' entries may be walked this function 598 * will also not lock the PTEs for the pte_entry() callback. This is useful for 599 * walking kernel pages tables or page tables for firmware. 600 * 601 * Note: Be careful to walk the kernel pages tables, the caller may be need to 602 * take other effective approaches (mmap lock may be insufficient) to prevent 603 * the intermediate kernel page tables belonging to the specified address range 604 * from being freed (e.g. memory hot-remove). 605 */ 606 int walk_kernel_page_table_range(unsigned long start, unsigned long end, 607 const struct mm_walk_ops *ops, pgd_t *pgd, void *private) 608 { 609 /* 610 * Kernel intermediate page tables are usually not freed, so the mmap 611 * read lock is sufficient. But there are some exceptions. 612 * E.g. memory hot-remove. In which case, the mmap lock is insufficient 613 * to prevent the intermediate kernel pages tables belonging to the 614 * specified address range from being freed. The caller should take 615 * other actions to prevent this race. 616 */ 617 mmap_assert_locked(&init_mm); 618 619 return walk_kernel_page_table_range_lockless(start, end, ops, pgd, 620 private); 621 } 622 623 /* 624 * Use this function to walk the kernel page tables locklessly. It should be 625 * guaranteed that the caller has exclusive access over the range they are 626 * operating on - that there should be no concurrent access, for example, 627 * changing permissions for vmalloc objects. 628 */ 629 int walk_kernel_page_table_range_lockless(unsigned long start, unsigned long end, 630 const struct mm_walk_ops *ops, pgd_t *pgd, void *private) 631 { 632 struct mm_walk walk = { 633 .ops = ops, 634 .mm = &init_mm, 635 .pgd = pgd, 636 .private = private, 637 .no_vma = true 638 }; 639 640 if (start >= end) 641 return -EINVAL; 642 if (!check_ops_safe(ops)) 643 return -EINVAL; 644 645 return walk_pgd_range(start, end, &walk); 646 } 647 648 /** 649 * walk_page_range_debug - walk a range of pagetables not backed by a vma 650 * @mm: mm_struct representing the target process of page table walk 651 * @start: start address of the virtual address range 652 * @end: end address of the virtual address range 653 * @ops: operation to call during the walk 654 * @pgd: pgd to walk if different from mm->pgd 655 * @private: private data for callbacks' usage 656 * 657 * Similar to walk_page_range() but can walk any page tables even if they are 658 * not backed by VMAs. Because 'unusual' entries may be walked this function 659 * will also not lock the PTEs for the pte_entry() callback. 660 * 661 * This is for debugging purposes ONLY. 662 */ 663 int walk_page_range_debug(struct mm_struct *mm, unsigned long start, 664 unsigned long end, const struct mm_walk_ops *ops, 665 pgd_t *pgd, void *private) 666 { 667 struct mm_walk walk = { 668 .ops = ops, 669 .mm = mm, 670 .pgd = pgd, 671 .private = private, 672 .no_vma = true 673 }; 674 675 /* For convenience, we allow traversal of kernel mappings. */ 676 if (mm == &init_mm) 677 return walk_kernel_page_table_range(start, end, ops, 678 pgd, private); 679 if (start >= end || !walk.mm) 680 return -EINVAL; 681 if (!check_ops_safe(ops)) 682 return -EINVAL; 683 684 /* 685 * The mmap lock protects the page walker from changes to the page 686 * tables during the walk. However a read lock is insufficient to 687 * protect those areas which don't have a VMA as munmap() detaches 688 * the VMAs before downgrading to a read lock and actually tearing 689 * down PTEs/page tables. In which case, the mmap write lock should 690 * be held. 691 */ 692 mmap_assert_write_locked(mm); 693 694 return walk_pgd_range(start, end, &walk); 695 } 696 697 int walk_page_range_vma_unsafe(struct vm_area_struct *vma, unsigned long start, 698 unsigned long end, const struct mm_walk_ops *ops, void *private) 699 { 700 struct mm_walk walk = { 701 .ops = ops, 702 .mm = vma->vm_mm, 703 .vma = vma, 704 .private = private, 705 }; 706 707 if (start >= end || !walk.mm) 708 return -EINVAL; 709 if (start < vma->vm_start || end > vma->vm_end) 710 return -EINVAL; 711 712 process_mm_walk_lock(walk.mm, ops->walk_lock); 713 process_vma_walk_lock(vma, ops->walk_lock); 714 return __walk_page_range(start, end, &walk); 715 } 716 717 int walk_page_range_vma(struct vm_area_struct *vma, unsigned long start, 718 unsigned long end, const struct mm_walk_ops *ops, 719 void *private) 720 { 721 if (!check_ops_safe(ops)) 722 return -EINVAL; 723 724 return walk_page_range_vma_unsafe(vma, start, end, ops, private); 725 } 726 727 int walk_page_vma(struct vm_area_struct *vma, const struct mm_walk_ops *ops, 728 void *private) 729 { 730 struct mm_walk walk = { 731 .ops = ops, 732 .mm = vma->vm_mm, 733 .vma = vma, 734 .private = private, 735 }; 736 737 if (!walk.mm) 738 return -EINVAL; 739 if (!check_ops_safe(ops)) 740 return -EINVAL; 741 742 process_mm_walk_lock(walk.mm, ops->walk_lock); 743 process_vma_walk_lock(vma, ops->walk_lock); 744 return __walk_page_range(vma->vm_start, vma->vm_end, &walk); 745 } 746 747 /** 748 * walk_page_mapping - walk all memory areas mapped into a struct address_space. 749 * @mapping: Pointer to the struct address_space 750 * @first_index: First page offset in the address_space 751 * @nr: Number of incremental page offsets to cover 752 * @ops: operation to call during the walk 753 * @private: private data for callbacks' usage 754 * 755 * This function walks all memory areas mapped into a struct address_space. 756 * The walk is limited to only the given page-size index range, but if 757 * the index boundaries cross a huge page-table entry, that entry will be 758 * included. 759 * 760 * Also see walk_page_range() for additional information. 761 * 762 * Locking: 763 * This function can't require that the struct mm_struct::mmap_lock is held, 764 * since @mapping may be mapped by multiple processes. Instead 765 * @mapping->i_mmap_rwsem must be held. This might have implications in the 766 * callbacks, and it's up tho the caller to ensure that the 767 * struct mm_struct::mmap_lock is not needed. 768 * 769 * Also this means that a caller can't rely on the struct 770 * vm_area_struct::vm_flags to be constant across a call, 771 * except for immutable flags. Callers requiring this shouldn't use 772 * this function. 773 * 774 * Return: 0 on success, negative error code on failure, positive number on 775 * caller defined premature termination. 776 */ 777 int walk_page_mapping(struct address_space *mapping, pgoff_t first_index, 778 pgoff_t nr, const struct mm_walk_ops *ops, 779 void *private) 780 { 781 struct mm_walk walk = { 782 .ops = ops, 783 .private = private, 784 }; 785 struct vm_area_struct *vma; 786 pgoff_t vba, vea, cba, cea; 787 unsigned long start_addr, end_addr; 788 int err = 0; 789 790 if (!check_ops_safe(ops)) 791 return -EINVAL; 792 793 lockdep_assert_held(&mapping->i_mmap_rwsem); 794 vma_interval_tree_foreach(vma, &mapping->i_mmap, first_index, 795 first_index + nr - 1) { 796 /* Clip to the vma */ 797 vba = vma->vm_pgoff; 798 vea = vba + vma_pages(vma); 799 cba = first_index; 800 cba = max(cba, vba); 801 cea = first_index + nr; 802 cea = min(cea, vea); 803 804 start_addr = ((cba - vba) << PAGE_SHIFT) + vma->vm_start; 805 end_addr = ((cea - vba) << PAGE_SHIFT) + vma->vm_start; 806 if (start_addr >= end_addr) 807 continue; 808 809 walk.vma = vma; 810 walk.mm = vma->vm_mm; 811 812 err = walk_page_test(vma->vm_start, vma->vm_end, &walk); 813 if (err > 0) { 814 err = 0; 815 break; 816 } else if (err < 0) 817 break; 818 819 err = __walk_page_range(start_addr, end_addr, &walk); 820 if (err) 821 break; 822 } 823 824 return err; 825 } 826 827 /** 828 * folio_walk_start - walk the page tables to a folio 829 * @fw: filled with information on success. 830 * @vma: the VMA. 831 * @addr: the virtual address to use for the page table walk. 832 * @flags: flags modifying which folios to walk to. 833 * 834 * Walk the page tables using @addr in a given @vma to a mapped folio and 835 * return the folio, making sure that the page table entry referenced by 836 * @addr cannot change until folio_walk_end() was called. 837 * 838 * As default, this function returns only folios that are not special (e.g., not 839 * the zeropage) and never returns folios that are supposed to be ignored by the 840 * VM as documented by vm_normal_page(). If requested, zeropages will be 841 * returned as well. 842 * 843 * If this function returns NULL it might either indicate "there is nothing" or 844 * "there is nothing suitable". 845 * 846 * On success, @fw is filled and the function returns the folio while the PTL 847 * is still held and folio_walk_end() must be called to clean up, 848 * releasing any held locks. The returned folio must *not* be used after the 849 * call to folio_walk_end(), unless a short-term folio reference is taken before 850 * that call. 851 * 852 * @fw->page will correspond to the page that is effectively referenced by 853 * @addr. However, for shared zeropages @fw->page is set to NULL. Note that 854 * large folios might be mapped by multiple page table entries, and this 855 * function will always only lookup a single entry as specified by @addr, which 856 * might or might not cover more than a single page of the returned folio. 857 * 858 * This function must *not* be used as a naive replacement for 859 * get_user_pages() / pin_user_pages(), especially not to perform DMA or 860 * to carelessly modify page content. This function may *only* be used to grab 861 * short-term folio references, never to grab long-term folio references. 862 * 863 * Using the page table entry pointers in @fw for reading or modifying the 864 * entry should be avoided where possible: however, there might be valid 865 * use cases. 866 * 867 * WARNING: Modifying page table entries in hugetlb VMAs requires a lot of care. 868 * For example, PMD page table sharing might require prior unsharing. Also, 869 * logical hugetlb entries might span multiple physical page table entries, 870 * which *must* be modified in a single operation (set_huge_pte_at(), 871 * huge_ptep_set_*, ...). Note that the page table entry stored in @fw might 872 * not correspond to the first physical entry of a logical hugetlb entry. 873 * 874 * The mmap lock must be held in read mode. 875 * 876 * Return: folio pointer on success, otherwise NULL. 877 */ 878 struct folio *folio_walk_start(struct folio_walk *fw, 879 struct vm_area_struct *vma, unsigned long addr, 880 folio_walk_flags_t flags) 881 { 882 unsigned long entry_size; 883 bool zeropage = false; 884 struct page *page; 885 pud_t *pudp, pud; 886 pmd_t *pmdp, pmd; 887 pte_t *ptep, pte; 888 spinlock_t *ptl; 889 pgd_t *pgdp; 890 p4d_t *p4dp; 891 892 mmap_assert_locked(vma->vm_mm); 893 vma_pgtable_walk_begin(vma); 894 895 if (WARN_ON_ONCE(addr < vma->vm_start || addr >= vma->vm_end)) 896 goto not_found; 897 898 pgdp = pgd_offset(vma->vm_mm, addr); 899 if (pgd_none_or_clear_bad(pgdp)) 900 goto not_found; 901 902 p4dp = p4d_offset(pgdp, addr); 903 if (p4d_none_or_clear_bad(p4dp)) 904 goto not_found; 905 906 pudp = pud_offset(p4dp, addr); 907 pud = pudp_get(pudp); 908 if (pud_none(pud)) 909 goto not_found; 910 if (IS_ENABLED(CONFIG_PGTABLE_HAS_HUGE_LEAVES) && 911 (!pud_present(pud) || pud_leaf(pud))) { 912 ptl = pud_lock(vma->vm_mm, pudp); 913 pud = pudp_get(pudp); 914 915 entry_size = PUD_SIZE; 916 fw->level = FW_LEVEL_PUD; 917 fw->pudp = pudp; 918 fw->pud = pud; 919 920 if (pud_none(pud)) { 921 spin_unlock(ptl); 922 goto not_found; 923 } else if (pud_present(pud) && !pud_leaf(pud)) { 924 spin_unlock(ptl); 925 goto pmd_table; 926 } else if (pud_present(pud)) { 927 page = vm_normal_page_pud(vma, addr, pud); 928 if (page) 929 goto found; 930 } 931 spin_unlock(ptl); 932 goto not_found; 933 } 934 935 pmd_table: 936 VM_WARN_ON_ONCE(!pud_present(pud) || pud_leaf(pud)); 937 pmdp = pmd_offset(pudp, addr); 938 pmd = pmdp_get_lockless(pmdp); 939 if (pmd_none(pmd)) 940 goto not_found; 941 if (IS_ENABLED(CONFIG_PGTABLE_HAS_HUGE_LEAVES) && 942 (!pmd_present(pmd) || pmd_leaf(pmd))) { 943 ptl = pmd_lock(vma->vm_mm, pmdp); 944 pmd = pmdp_get(pmdp); 945 946 entry_size = PMD_SIZE; 947 fw->level = FW_LEVEL_PMD; 948 fw->pmdp = pmdp; 949 fw->pmd = pmd; 950 951 if (pmd_none(pmd)) { 952 spin_unlock(ptl); 953 goto not_found; 954 } else if (pmd_present(pmd) && !pmd_leaf(pmd)) { 955 spin_unlock(ptl); 956 goto pte_table; 957 } else if (pmd_present(pmd)) { 958 page = vm_normal_page_pmd(vma, addr, pmd); 959 if (page) { 960 goto found; 961 } else if ((flags & FW_ZEROPAGE) && 962 is_huge_zero_pmd(pmd)) { 963 page = pfn_to_page(pmd_pfn(pmd)); 964 zeropage = true; 965 goto found; 966 } 967 } 968 spin_unlock(ptl); 969 goto not_found; 970 } 971 972 pte_table: 973 VM_WARN_ON_ONCE(!pmd_present(pmd) || pmd_leaf(pmd)); 974 ptep = pte_offset_map_lock(vma->vm_mm, pmdp, addr, &ptl); 975 if (!ptep) 976 goto not_found; 977 pte = ptep_get(ptep); 978 979 entry_size = PAGE_SIZE; 980 fw->level = FW_LEVEL_PTE; 981 fw->ptep = ptep; 982 fw->pte = pte; 983 984 if (pte_present(pte)) { 985 page = vm_normal_page(vma, addr, pte); 986 if (page) 987 goto found; 988 if ((flags & FW_ZEROPAGE) && 989 is_zero_pfn(pte_pfn(pte))) { 990 page = pfn_to_page(pte_pfn(pte)); 991 zeropage = true; 992 goto found; 993 } 994 } 995 pte_unmap_unlock(ptep, ptl); 996 not_found: 997 vma_pgtable_walk_end(vma); 998 return NULL; 999 found: 1000 if (!zeropage) 1001 /* Note: Offset from the mapped page, not the folio start. */ 1002 fw->page = page + ((addr & (entry_size - 1)) >> PAGE_SHIFT); 1003 else 1004 fw->page = NULL; 1005 fw->ptl = ptl; 1006 return page_folio(page); 1007 } 1008