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.
52 if (walk->mm == &init_mm)
53 pte = pte_offset_kernel(pmd, addr);
55 pte = pte_offset_map(pmd, addr);
57 err = walk_pte_range_inner(pte, addr, end, walk);
58 if (walk->mm != &init_mm)
62 pte = pte_offset_map_lock(walk->mm, pmd, addr, &ptl);
64 err = walk_pte_range_inner(pte, addr, end, walk);
65 pte_unmap_unlock(pte, ptl);
69 walk->action = ACTION_AGAIN;
73 #ifdef CONFIG_ARCH_HAS_HUGEPD
74 static int walk_hugepd_range(hugepd_t *phpd, unsigned long addr,
75 unsigned long end, struct mm_walk *walk, int pdshift)
78 const struct mm_walk_ops *ops = walk->ops;
79 int shift = hugepd_shift(*phpd);
80 int page_size = 1 << shift;
85 if (addr & (page_size - 1))
91 spin_lock(&walk->mm->page_table_lock);
92 pte = hugepte_offset(*phpd, addr, pdshift);
93 err = ops->pte_entry(pte, addr, addr + page_size, walk);
94 spin_unlock(&walk->mm->page_table_lock);
98 if (addr >= end - page_size)
105 static int walk_hugepd_range(hugepd_t *phpd, unsigned long addr,
106 unsigned long end, struct mm_walk *walk, int pdshift)
112 static int walk_pmd_range(pud_t *pud, unsigned long addr, unsigned long end,
113 struct mm_walk *walk)
117 const struct mm_walk_ops *ops = walk->ops;
119 int depth = real_depth(3);
121 pmd = pmd_offset(pud, addr);
124 next = pmd_addr_end(addr, end);
125 if (pmd_none(*pmd)) {
127 err = ops->pte_hole(addr, next, depth, walk);
133 walk->action = ACTION_SUBTREE;
136 * This implies that each ->pmd_entry() handler
137 * needs to know about pmd_trans_huge() pmds
140 err = ops->pmd_entry(pmd, addr, next, walk);
144 if (walk->action == ACTION_AGAIN)
148 * Check this here so we only break down trans_huge
149 * pages when we _need_ to
151 if ((!walk->vma && (pmd_leaf(*pmd) || !pmd_present(*pmd))) ||
152 walk->action == ACTION_CONTINUE ||
157 split_huge_pmd(walk->vma, pmd, addr);
159 if (is_hugepd(__hugepd(pmd_val(*pmd))))
160 err = walk_hugepd_range((hugepd_t *)pmd, addr, next, walk, PMD_SHIFT);
162 err = walk_pte_range(pmd, addr, next, walk);
166 if (walk->action == ACTION_AGAIN)
169 } while (pmd++, addr = next, addr != end);
174 static int walk_pud_range(p4d_t *p4d, unsigned long addr, unsigned long end,
175 struct mm_walk *walk)
179 const struct mm_walk_ops *ops = walk->ops;
181 int depth = real_depth(2);
183 pud = pud_offset(p4d, addr);
186 next = pud_addr_end(addr, end);
187 if (pud_none(*pud)) {
189 err = ops->pte_hole(addr, next, depth, walk);
195 walk->action = ACTION_SUBTREE;
198 err = ops->pud_entry(pud, addr, next, walk);
202 if (walk->action == ACTION_AGAIN)
205 if ((!walk->vma && (pud_leaf(*pud) || !pud_present(*pud))) ||
206 walk->action == ACTION_CONTINUE ||
207 !(ops->pmd_entry || ops->pte_entry))
211 split_huge_pud(walk->vma, pud, addr);
215 if (is_hugepd(__hugepd(pud_val(*pud))))
216 err = walk_hugepd_range((hugepd_t *)pud, addr, next, walk, PUD_SHIFT);
218 err = walk_pmd_range(pud, addr, next, walk);
221 } while (pud++, addr = next, addr != end);
226 static int walk_p4d_range(pgd_t *pgd, unsigned long addr, unsigned long end,
227 struct mm_walk *walk)
231 const struct mm_walk_ops *ops = walk->ops;
233 int depth = real_depth(1);
235 p4d = p4d_offset(pgd, addr);
237 next = p4d_addr_end(addr, end);
238 if (p4d_none_or_clear_bad(p4d)) {
240 err = ops->pte_hole(addr, next, depth, walk);
245 if (ops->p4d_entry) {
246 err = ops->p4d_entry(p4d, addr, next, walk);
250 if (is_hugepd(__hugepd(p4d_val(*p4d))))
251 err = walk_hugepd_range((hugepd_t *)p4d, addr, next, walk, P4D_SHIFT);
252 else if (ops->pud_entry || ops->pmd_entry || ops->pte_entry)
253 err = walk_pud_range(p4d, addr, next, walk);
256 } while (p4d++, addr = next, addr != end);
261 static int walk_pgd_range(unsigned long addr, unsigned long end,
262 struct mm_walk *walk)
266 const struct mm_walk_ops *ops = walk->ops;
270 pgd = walk->pgd + pgd_index(addr);
272 pgd = pgd_offset(walk->mm, addr);
274 next = pgd_addr_end(addr, end);
275 if (pgd_none_or_clear_bad(pgd)) {
277 err = ops->pte_hole(addr, next, 0, walk);
282 if (ops->pgd_entry) {
283 err = ops->pgd_entry(pgd, addr, next, walk);
287 if (is_hugepd(__hugepd(pgd_val(*pgd))))
288 err = walk_hugepd_range((hugepd_t *)pgd, addr, next, walk, PGDIR_SHIFT);
289 else if (ops->p4d_entry || ops->pud_entry || ops->pmd_entry || ops->pte_entry)
290 err = walk_p4d_range(pgd, addr, next, walk);
293 } while (pgd++, addr = next, addr != end);
298 #ifdef CONFIG_HUGETLB_PAGE
299 static unsigned long hugetlb_entry_end(struct hstate *h, unsigned long addr,
302 unsigned long boundary = (addr & huge_page_mask(h)) + huge_page_size(h);
303 return boundary < end ? boundary : end;
306 static int walk_hugetlb_range(unsigned long addr, unsigned long end,
307 struct mm_walk *walk)
309 struct vm_area_struct *vma = walk->vma;
310 struct hstate *h = hstate_vma(vma);
312 unsigned long hmask = huge_page_mask(h);
313 unsigned long sz = huge_page_size(h);
315 const struct mm_walk_ops *ops = walk->ops;
318 hugetlb_vma_lock_read(vma);
320 next = hugetlb_entry_end(h, addr, end);
321 pte = hugetlb_walk(vma, addr & hmask, sz);
323 err = ops->hugetlb_entry(pte, hmask, addr, next, walk);
324 else if (ops->pte_hole)
325 err = ops->pte_hole(addr, next, -1, walk);
328 } while (addr = next, addr != end);
329 hugetlb_vma_unlock_read(vma);
334 #else /* CONFIG_HUGETLB_PAGE */
335 static int walk_hugetlb_range(unsigned long addr, unsigned long end,
336 struct mm_walk *walk)
341 #endif /* CONFIG_HUGETLB_PAGE */
344 * Decide whether we really walk over the current vma on [@start, @end)
345 * or skip it via the returned value. Return 0 if we do walk over the
346 * current vma, and return 1 if we skip the vma. Negative values means
347 * error, where we abort the current walk.
349 static int walk_page_test(unsigned long start, unsigned long end,
350 struct mm_walk *walk)
352 struct vm_area_struct *vma = walk->vma;
353 const struct mm_walk_ops *ops = walk->ops;
356 return ops->test_walk(start, end, walk);
359 * vma(VM_PFNMAP) doesn't have any valid struct pages behind VM_PFNMAP
360 * range, so we don't walk over it as we do for normal vmas. However,
361 * Some callers are interested in handling hole range and they don't
362 * want to just ignore any single address range. Such users certainly
363 * define their ->pte_hole() callbacks, so let's delegate them to handle
366 if (vma->vm_flags & VM_PFNMAP) {
369 err = ops->pte_hole(start, end, -1, walk);
370 return err ? err : 1;
375 static int __walk_page_range(unsigned long start, unsigned long end,
376 struct mm_walk *walk)
379 struct vm_area_struct *vma = walk->vma;
380 const struct mm_walk_ops *ops = walk->ops;
383 err = ops->pre_vma(start, end, walk);
388 if (is_vm_hugetlb_page(vma)) {
389 if (ops->hugetlb_entry)
390 err = walk_hugetlb_range(start, end, walk);
392 err = walk_pgd_range(start, end, walk);
401 * walk_page_range - walk page table with caller specific callbacks
402 * @mm: mm_struct representing the target process of page table walk
403 * @start: start address of the virtual address range
404 * @end: end address of the virtual address range
405 * @ops: operation to call during the walk
406 * @private: private data for callbacks' usage
408 * Recursively walk the page table tree of the process represented by @mm
409 * within the virtual address range [@start, @end). During walking, we can do
410 * some caller-specific works for each entry, by setting up pmd_entry(),
411 * pte_entry(), and/or hugetlb_entry(). If you don't set up for some of these
412 * callbacks, the associated entries/pages are just ignored.
413 * The return values of these callbacks are commonly defined like below:
415 * - 0 : succeeded to handle the current entry, and if you don't reach the
416 * end address yet, continue to walk.
417 * - >0 : succeeded to handle the current entry, and return to the caller
418 * with caller specific value.
419 * - <0 : failed to handle the current entry, and return to the caller
422 * Before starting to walk page table, some callers want to check whether
423 * they really want to walk over the current vma, typically by checking
424 * its vm_flags. walk_page_test() and @ops->test_walk() are used for this
427 * If operations need to be staged before and committed after a vma is walked,
428 * there are two callbacks, pre_vma() and post_vma(). Note that post_vma(),
429 * since it is intended to handle commit-type operations, can't return any
432 * struct mm_walk keeps current values of some common data like vma and pmd,
433 * which are useful for the access from callbacks. If you want to pass some
434 * caller-specific data to callbacks, @private should be helpful.
437 * Callers of walk_page_range() and walk_page_vma() should hold @mm->mmap_lock,
438 * because these function traverse vma list and/or access to vma's data.
440 int walk_page_range(struct mm_struct *mm, unsigned long start,
441 unsigned long end, const struct mm_walk_ops *ops,
446 struct vm_area_struct *vma;
447 struct mm_walk walk = {
459 mmap_assert_locked(walk.mm);
461 vma = find_vma(walk.mm, start);
463 if (!vma) { /* after the last vma */
467 err = ops->pte_hole(start, next, -1, &walk);
468 } else if (start < vma->vm_start) { /* outside vma */
470 next = min(end, vma->vm_start);
472 err = ops->pte_hole(start, next, -1, &walk);
473 } else { /* inside vma */
475 next = min(end, vma->vm_end);
476 vma = find_vma(mm, vma->vm_end);
478 err = walk_page_test(start, next, &walk);
481 * positive return values are purely for
482 * controlling the pagewalk, so should never
483 * be passed to the callers.
490 err = __walk_page_range(start, next, &walk);
494 } while (start = next, start < end);
499 * walk_page_range_novma - walk a range of pagetables not backed by a vma
500 * @mm: mm_struct representing the target process of page table walk
501 * @start: start address of the virtual address range
502 * @end: end address of the virtual address range
503 * @ops: operation to call during the walk
504 * @pgd: pgd to walk if different from mm->pgd
505 * @private: private data for callbacks' usage
507 * Similar to walk_page_range() but can walk any page tables even if they are
508 * not backed by VMAs. Because 'unusual' entries may be walked this function
509 * will also not lock the PTEs for the pte_entry() callback. This is useful for
510 * walking the kernel pages tables or page tables for firmware.
512 int walk_page_range_novma(struct mm_struct *mm, unsigned long start,
513 unsigned long end, const struct mm_walk_ops *ops,
517 struct mm_walk walk = {
525 if (start >= end || !walk.mm)
528 mmap_assert_write_locked(walk.mm);
530 return walk_pgd_range(start, end, &walk);
533 int walk_page_range_vma(struct vm_area_struct *vma, unsigned long start,
534 unsigned long end, const struct mm_walk_ops *ops,
537 struct mm_walk walk = {
544 if (start >= end || !walk.mm)
546 if (start < vma->vm_start || end > vma->vm_end)
549 mmap_assert_locked(walk.mm);
550 return __walk_page_range(start, end, &walk);
553 int walk_page_vma(struct vm_area_struct *vma, const struct mm_walk_ops *ops,
556 struct mm_walk walk = {
566 mmap_assert_locked(walk.mm);
567 return __walk_page_range(vma->vm_start, vma->vm_end, &walk);
571 * walk_page_mapping - walk all memory areas mapped into a struct address_space.
572 * @mapping: Pointer to the struct address_space
573 * @first_index: First page offset in the address_space
574 * @nr: Number of incremental page offsets to cover
575 * @ops: operation to call during the walk
576 * @private: private data for callbacks' usage
578 * This function walks all memory areas mapped into a struct address_space.
579 * The walk is limited to only the given page-size index range, but if
580 * the index boundaries cross a huge page-table entry, that entry will be
583 * Also see walk_page_range() for additional information.
586 * This function can't require that the struct mm_struct::mmap_lock is held,
587 * since @mapping may be mapped by multiple processes. Instead
588 * @mapping->i_mmap_rwsem must be held. This might have implications in the
589 * callbacks, and it's up tho the caller to ensure that the
590 * struct mm_struct::mmap_lock is not needed.
592 * Also this means that a caller can't rely on the struct
593 * vm_area_struct::vm_flags to be constant across a call,
594 * except for immutable flags. Callers requiring this shouldn't use
597 * Return: 0 on success, negative error code on failure, positive number on
598 * caller defined premature termination.
600 int walk_page_mapping(struct address_space *mapping, pgoff_t first_index,
601 pgoff_t nr, const struct mm_walk_ops *ops,
604 struct mm_walk walk = {
608 struct vm_area_struct *vma;
609 pgoff_t vba, vea, cba, cea;
610 unsigned long start_addr, end_addr;
613 lockdep_assert_held(&mapping->i_mmap_rwsem);
614 vma_interval_tree_foreach(vma, &mapping->i_mmap, first_index,
615 first_index + nr - 1) {
616 /* Clip to the vma */
618 vea = vba + vma_pages(vma);
621 cea = first_index + nr;
624 start_addr = ((cba - vba) << PAGE_SHIFT) + vma->vm_start;
625 end_addr = ((cea - vba) << PAGE_SHIFT) + vma->vm_start;
626 if (start_addr >= end_addr)
630 walk.mm = vma->vm_mm;
632 err = walk_page_test(vma->vm_start, vma->vm_end, &walk);
639 err = __walk_page_range(start_addr, end_addr, &walk);