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>
8 * We want to know the real level where a entry is located ignoring any
9 * folding of levels which may be happening. For example if p4d is folded then
10 * a missing entry found at level 1 (p4d) is actually at level 0 (pgd).
12 static int real_depth(int depth)
14 if (depth == 3 && PTRS_PER_PMD == 1)
16 if (depth == 2 && PTRS_PER_PUD == 1)
18 if (depth == 1 && PTRS_PER_P4D == 1)
23 static int walk_pte_range_inner(pte_t *pte, unsigned long addr,
24 unsigned long end, struct mm_walk *walk)
26 const struct mm_walk_ops *ops = walk->ops;
30 err = ops->pte_entry(pte, addr, addr + PAGE_SIZE, walk);
33 if (addr >= end - PAGE_SIZE)
41 static int walk_pte_range(pmd_t *pmd, unsigned long addr, unsigned long end,
50 * pte_offset_map() might apply user-specific validation.
51 * Indeed, on x86_64 the pmd entries set up by init_espfix_ap()
52 * fit its pmd_bad() check (_PAGE_NX set and _PAGE_RW clear),
53 * and CONFIG_EFI_PGT_DUMP efi_mm goes so far as to walk them.
55 if (walk->mm == &init_mm || addr >= TASK_SIZE)
56 pte = pte_offset_kernel(pmd, addr);
58 pte = pte_offset_map(pmd, addr);
60 err = walk_pte_range_inner(pte, addr, end, walk);
61 if (walk->mm != &init_mm && addr < TASK_SIZE)
65 pte = pte_offset_map_lock(walk->mm, pmd, addr, &ptl);
67 err = walk_pte_range_inner(pte, addr, end, walk);
68 pte_unmap_unlock(pte, ptl);
72 walk->action = ACTION_AGAIN;
76 static int walk_pmd_range(pud_t *pud, unsigned long addr, unsigned long end,
81 const struct mm_walk_ops *ops = walk->ops;
83 int depth = real_depth(3);
85 pmd = pmd_offset(pud, addr);
88 next = pmd_addr_end(addr, end);
91 err = ops->pte_hole(addr, next, depth, walk);
97 walk->action = ACTION_SUBTREE;
100 * This implies that each ->pmd_entry() handler
101 * needs to know about pmd_trans_huge() pmds
104 err = ops->pmd_entry(pmd, addr, next, walk);
108 if (walk->action == ACTION_AGAIN)
112 * Check this here so we only break down trans_huge
113 * pages when we _need_ to
115 if ((!walk->vma && (pmd_leaf(*pmd) || !pmd_present(*pmd))) ||
116 walk->action == ACTION_CONTINUE ||
121 split_huge_pmd(walk->vma, pmd, addr);
123 err = walk_pte_range(pmd, addr, next, walk);
127 if (walk->action == ACTION_AGAIN)
130 } while (pmd++, addr = next, addr != end);
135 static int walk_pud_range(p4d_t *p4d, unsigned long addr, unsigned long end,
136 struct mm_walk *walk)
140 const struct mm_walk_ops *ops = walk->ops;
142 int depth = real_depth(2);
144 pud = pud_offset(p4d, addr);
147 next = pud_addr_end(addr, end);
148 if (pud_none(*pud)) {
150 err = ops->pte_hole(addr, next, depth, walk);
156 walk->action = ACTION_SUBTREE;
159 err = ops->pud_entry(pud, addr, next, walk);
163 if (walk->action == ACTION_AGAIN)
166 if ((!walk->vma && (pud_leaf(*pud) || !pud_present(*pud))) ||
167 walk->action == ACTION_CONTINUE ||
168 !(ops->pmd_entry || ops->pte_entry))
172 split_huge_pud(walk->vma, pud, addr);
176 err = walk_pmd_range(pud, addr, next, walk);
179 } while (pud++, addr = next, addr != end);
184 static int walk_p4d_range(pgd_t *pgd, unsigned long addr, unsigned long end,
185 struct mm_walk *walk)
189 const struct mm_walk_ops *ops = walk->ops;
191 int depth = real_depth(1);
193 p4d = p4d_offset(pgd, addr);
195 next = p4d_addr_end(addr, end);
196 if (p4d_none_or_clear_bad(p4d)) {
198 err = ops->pte_hole(addr, next, depth, walk);
203 if (ops->p4d_entry) {
204 err = ops->p4d_entry(p4d, addr, next, walk);
208 if (ops->pud_entry || ops->pmd_entry || ops->pte_entry)
209 err = walk_pud_range(p4d, addr, next, walk);
212 } while (p4d++, addr = next, addr != end);
217 static int walk_pgd_range(unsigned long addr, unsigned long end,
218 struct mm_walk *walk)
222 const struct mm_walk_ops *ops = walk->ops;
226 pgd = walk->pgd + pgd_index(addr);
228 pgd = pgd_offset(walk->mm, addr);
230 next = pgd_addr_end(addr, end);
231 if (pgd_none_or_clear_bad(pgd)) {
233 err = ops->pte_hole(addr, next, 0, walk);
238 if (ops->pgd_entry) {
239 err = ops->pgd_entry(pgd, addr, next, walk);
243 if (ops->p4d_entry || ops->pud_entry || ops->pmd_entry || ops->pte_entry)
244 err = walk_p4d_range(pgd, addr, next, walk);
247 } while (pgd++, addr = next, addr != end);
252 #ifdef CONFIG_HUGETLB_PAGE
253 static unsigned long hugetlb_entry_end(struct hstate *h, unsigned long addr,
256 unsigned long boundary = (addr & huge_page_mask(h)) + huge_page_size(h);
257 return boundary < end ? boundary : end;
260 static int walk_hugetlb_range(unsigned long addr, unsigned long end,
261 struct mm_walk *walk)
263 struct vm_area_struct *vma = walk->vma;
264 struct hstate *h = hstate_vma(vma);
266 unsigned long hmask = huge_page_mask(h);
267 unsigned long sz = huge_page_size(h);
269 const struct mm_walk_ops *ops = walk->ops;
272 hugetlb_vma_lock_read(vma);
274 next = hugetlb_entry_end(h, addr, end);
275 pte = hugetlb_walk(vma, addr & hmask, sz);
277 err = ops->hugetlb_entry(pte, hmask, addr, next, walk);
278 else if (ops->pte_hole)
279 err = ops->pte_hole(addr, next, -1, walk);
282 } while (addr = next, addr != end);
283 hugetlb_vma_unlock_read(vma);
288 #else /* CONFIG_HUGETLB_PAGE */
289 static int walk_hugetlb_range(unsigned long addr, unsigned long end,
290 struct mm_walk *walk)
295 #endif /* CONFIG_HUGETLB_PAGE */
298 * Decide whether we really walk over the current vma on [@start, @end)
299 * or skip it via the returned value. Return 0 if we do walk over the
300 * current vma, and return 1 if we skip the vma. Negative values means
301 * error, where we abort the current walk.
303 static int walk_page_test(unsigned long start, unsigned long end,
304 struct mm_walk *walk)
306 struct vm_area_struct *vma = walk->vma;
307 const struct mm_walk_ops *ops = walk->ops;
310 return ops->test_walk(start, end, walk);
313 * vma(VM_PFNMAP) doesn't have any valid struct pages behind VM_PFNMAP
314 * range, so we don't walk over it as we do for normal vmas. However,
315 * Some callers are interested in handling hole range and they don't
316 * want to just ignore any single address range. Such users certainly
317 * define their ->pte_hole() callbacks, so let's delegate them to handle
320 if (vma->vm_flags & VM_PFNMAP) {
323 err = ops->pte_hole(start, end, -1, walk);
324 return err ? err : 1;
329 static int __walk_page_range(unsigned long start, unsigned long end,
330 struct mm_walk *walk)
333 struct vm_area_struct *vma = walk->vma;
334 const struct mm_walk_ops *ops = walk->ops;
337 err = ops->pre_vma(start, end, walk);
342 if (is_vm_hugetlb_page(vma)) {
343 if (ops->hugetlb_entry)
344 err = walk_hugetlb_range(start, end, walk);
346 err = walk_pgd_range(start, end, walk);
354 static inline void process_mm_walk_lock(struct mm_struct *mm,
355 enum page_walk_lock walk_lock)
357 if (walk_lock == PGWALK_RDLOCK)
358 mmap_assert_locked(mm);
360 mmap_assert_write_locked(mm);
363 static inline void process_vma_walk_lock(struct vm_area_struct *vma,
364 enum page_walk_lock walk_lock)
366 #ifdef CONFIG_PER_VMA_LOCK
369 vma_start_write(vma);
371 case PGWALK_WRLOCK_VERIFY:
372 vma_assert_write_locked(vma);
375 /* PGWALK_RDLOCK is handled by process_mm_walk_lock */
382 * walk_page_range - walk page table with caller specific callbacks
383 * @mm: mm_struct representing the target process of page table walk
384 * @start: start address of the virtual address range
385 * @end: end address of the virtual address range
386 * @ops: operation to call during the walk
387 * @private: private data for callbacks' usage
389 * Recursively walk the page table tree of the process represented by @mm
390 * within the virtual address range [@start, @end). During walking, we can do
391 * some caller-specific works for each entry, by setting up pmd_entry(),
392 * pte_entry(), and/or hugetlb_entry(). If you don't set up for some of these
393 * callbacks, the associated entries/pages are just ignored.
394 * The return values of these callbacks are commonly defined like below:
396 * - 0 : succeeded to handle the current entry, and if you don't reach the
397 * end address yet, continue to walk.
398 * - >0 : succeeded to handle the current entry, and return to the caller
399 * with caller specific value.
400 * - <0 : failed to handle the current entry, and return to the caller
403 * Before starting to walk page table, some callers want to check whether
404 * they really want to walk over the current vma, typically by checking
405 * its vm_flags. walk_page_test() and @ops->test_walk() are used for this
408 * If operations need to be staged before and committed after a vma is walked,
409 * there are two callbacks, pre_vma() and post_vma(). Note that post_vma(),
410 * since it is intended to handle commit-type operations, can't return any
413 * struct mm_walk keeps current values of some common data like vma and pmd,
414 * which are useful for the access from callbacks. If you want to pass some
415 * caller-specific data to callbacks, @private should be helpful.
418 * Callers of walk_page_range() and walk_page_vma() should hold @mm->mmap_lock,
419 * because these function traverse vma list and/or access to vma's data.
421 int walk_page_range(struct mm_struct *mm, unsigned long start,
422 unsigned long end, const struct mm_walk_ops *ops,
427 struct vm_area_struct *vma;
428 struct mm_walk walk = {
440 process_mm_walk_lock(walk.mm, ops->walk_lock);
442 vma = find_vma(walk.mm, start);
444 if (!vma) { /* after the last vma */
448 err = ops->pte_hole(start, next, -1, &walk);
449 } else if (start < vma->vm_start) { /* outside vma */
451 next = min(end, vma->vm_start);
453 err = ops->pte_hole(start, next, -1, &walk);
454 } else { /* inside vma */
455 process_vma_walk_lock(vma, ops->walk_lock);
457 next = min(end, vma->vm_end);
458 vma = find_vma(mm, vma->vm_end);
460 err = walk_page_test(start, next, &walk);
463 * positive return values are purely for
464 * controlling the pagewalk, so should never
465 * be passed to the callers.
472 err = __walk_page_range(start, next, &walk);
476 } while (start = next, start < end);
481 * walk_page_range_novma - walk a range of pagetables not backed by a vma
482 * @mm: mm_struct representing the target process of page table walk
483 * @start: start address of the virtual address range
484 * @end: end address of the virtual address range
485 * @ops: operation to call during the walk
486 * @pgd: pgd to walk if different from mm->pgd
487 * @private: private data for callbacks' usage
489 * Similar to walk_page_range() but can walk any page tables even if they are
490 * not backed by VMAs. Because 'unusual' entries may be walked this function
491 * will also not lock the PTEs for the pte_entry() callback. This is useful for
492 * walking the kernel pages tables or page tables for firmware.
494 * Note: Be careful to walk the kernel pages tables, the caller may be need to
495 * take other effective approache (mmap lock may be insufficient) to prevent
496 * the intermediate kernel page tables belonging to the specified address range
497 * from being freed (e.g. memory hot-remove).
499 int walk_page_range_novma(struct mm_struct *mm, unsigned long start,
500 unsigned long end, const struct mm_walk_ops *ops,
504 struct mm_walk walk = {
512 if (start >= end || !walk.mm)
516 * 1) For walking the user virtual address space:
518 * The mmap lock protects the page walker from changes to the page
519 * tables during the walk. However a read lock is insufficient to
520 * protect those areas which don't have a VMA as munmap() detaches
521 * the VMAs before downgrading to a read lock and actually tearing
522 * down PTEs/page tables. In which case, the mmap write lock should
525 * 2) For walking the kernel virtual address space:
527 * The kernel intermediate page tables usually do not be freed, so
528 * the mmap map read lock is sufficient. But there are some exceptions.
529 * E.g. memory hot-remove. In which case, the mmap lock is insufficient
530 * to prevent the intermediate kernel pages tables belonging to the
531 * specified address range from being freed. The caller should take
532 * other actions to prevent this race.
535 mmap_assert_locked(walk.mm);
537 mmap_assert_write_locked(walk.mm);
539 return walk_pgd_range(start, end, &walk);
542 int walk_page_range_vma(struct vm_area_struct *vma, unsigned long start,
543 unsigned long end, const struct mm_walk_ops *ops,
546 struct mm_walk walk = {
553 if (start >= end || !walk.mm)
555 if (start < vma->vm_start || end > vma->vm_end)
558 process_mm_walk_lock(walk.mm, ops->walk_lock);
559 process_vma_walk_lock(vma, ops->walk_lock);
560 return __walk_page_range(start, end, &walk);
563 int walk_page_vma(struct vm_area_struct *vma, const struct mm_walk_ops *ops,
566 struct mm_walk walk = {
576 process_mm_walk_lock(walk.mm, ops->walk_lock);
577 process_vma_walk_lock(vma, ops->walk_lock);
578 return __walk_page_range(vma->vm_start, vma->vm_end, &walk);
582 * walk_page_mapping - walk all memory areas mapped into a struct address_space.
583 * @mapping: Pointer to the struct address_space
584 * @first_index: First page offset in the address_space
585 * @nr: Number of incremental page offsets to cover
586 * @ops: operation to call during the walk
587 * @private: private data for callbacks' usage
589 * This function walks all memory areas mapped into a struct address_space.
590 * The walk is limited to only the given page-size index range, but if
591 * the index boundaries cross a huge page-table entry, that entry will be
594 * Also see walk_page_range() for additional information.
597 * This function can't require that the struct mm_struct::mmap_lock is held,
598 * since @mapping may be mapped by multiple processes. Instead
599 * @mapping->i_mmap_rwsem must be held. This might have implications in the
600 * callbacks, and it's up tho the caller to ensure that the
601 * struct mm_struct::mmap_lock is not needed.
603 * Also this means that a caller can't rely on the struct
604 * vm_area_struct::vm_flags to be constant across a call,
605 * except for immutable flags. Callers requiring this shouldn't use
608 * Return: 0 on success, negative error code on failure, positive number on
609 * caller defined premature termination.
611 int walk_page_mapping(struct address_space *mapping, pgoff_t first_index,
612 pgoff_t nr, const struct mm_walk_ops *ops,
615 struct mm_walk walk = {
619 struct vm_area_struct *vma;
620 pgoff_t vba, vea, cba, cea;
621 unsigned long start_addr, end_addr;
624 lockdep_assert_held(&mapping->i_mmap_rwsem);
625 vma_interval_tree_foreach(vma, &mapping->i_mmap, first_index,
626 first_index + nr - 1) {
627 /* Clip to the vma */
629 vea = vba + vma_pages(vma);
632 cea = first_index + nr;
635 start_addr = ((cba - vba) << PAGE_SHIFT) + vma->vm_start;
636 end_addr = ((cea - vba) << PAGE_SHIFT) + vma->vm_start;
637 if (start_addr >= end_addr)
641 walk.mm = vma->vm_mm;
643 err = walk_page_test(vma->vm_start, vma->vm_end, &walk);
650 err = __walk_page_range(start_addr, end_addr, &walk);