xref: /linux/mm/pagewalk.c (revision 22c55fb9eb92395d999b8404d73e58540d11bdd8)
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 #include <linux/swapops.h>
9 
10 #include <asm/tlbflush.h>
11 
12 #include "internal.h"
13 
14 /*
15  * We want to know the real level where a entry is located ignoring any
16  * folding of levels which may be happening. For example if p4d is folded then
17  * a missing entry found at level 1 (p4d) is actually at level 0 (pgd).
18  */
19 static int real_depth(int depth)
20 {
21 	if (depth == 3 && PTRS_PER_PMD == 1)
22 		depth = 2;
23 	if (depth == 2 && PTRS_PER_PUD == 1)
24 		depth = 1;
25 	if (depth == 1 && PTRS_PER_P4D == 1)
26 		depth = 0;
27 	return depth;
28 }
29 
30 static int walk_pte_range_inner(pte_t *pte, unsigned long addr,
31 				unsigned long end, struct mm_walk *walk)
32 {
33 	const struct mm_walk_ops *ops = walk->ops;
34 	int err = 0;
35 
36 	for (;;) {
37 		if (ops->install_pte && pte_none(ptep_get(pte))) {
38 			pte_t new_pte;
39 
40 			err = ops->install_pte(addr, addr + PAGE_SIZE, &new_pte,
41 					       walk);
42 			if (err)
43 				break;
44 
45 			set_pte_at(walk->mm, addr, pte, new_pte);
46 			/* Non-present before, so for arches that need it. */
47 			if (!WARN_ON_ONCE(walk->no_vma))
48 				update_mmu_cache(walk->vma, addr, pte);
49 		} else {
50 			err = ops->pte_entry(pte, addr, addr + PAGE_SIZE, walk);
51 			if (err)
52 				break;
53 		}
54 		if (addr >= end - PAGE_SIZE)
55 			break;
56 		addr += PAGE_SIZE;
57 		pte++;
58 	}
59 	return err;
60 }
61 
62 static int walk_pte_range(pmd_t *pmd, unsigned long addr, unsigned long end,
63 			  struct mm_walk *walk)
64 {
65 	pte_t *pte;
66 	int err = 0;
67 	spinlock_t *ptl;
68 
69 	if (walk->no_vma) {
70 		/*
71 		 * pte_offset_map() might apply user-specific validation.
72 		 * Indeed, on x86_64 the pmd entries set up by init_espfix_ap()
73 		 * fit its pmd_bad() check (_PAGE_NX set and _PAGE_RW clear),
74 		 * and CONFIG_EFI_PGT_DUMP efi_mm goes so far as to walk them.
75 		 */
76 		if (walk->mm == &init_mm || addr >= TASK_SIZE)
77 			pte = pte_offset_kernel(pmd, addr);
78 		else
79 			pte = pte_offset_map(pmd, addr);
80 		if (pte) {
81 			err = walk_pte_range_inner(pte, addr, end, walk);
82 			if (walk->mm != &init_mm && addr < TASK_SIZE)
83 				pte_unmap(pte);
84 		}
85 	} else {
86 		pte = pte_offset_map_lock(walk->mm, pmd, addr, &ptl);
87 		if (pte) {
88 			err = walk_pte_range_inner(pte, addr, end, walk);
89 			pte_unmap_unlock(pte, ptl);
90 		}
91 	}
92 	if (!pte)
93 		walk->action = ACTION_AGAIN;
94 	return err;
95 }
96 
97 static int walk_pmd_range(pud_t *pud, unsigned long addr, unsigned long end,
98 			  struct mm_walk *walk)
99 {
100 	pmd_t *pmd;
101 	unsigned long next;
102 	const struct mm_walk_ops *ops = walk->ops;
103 	bool has_handler = ops->pte_entry;
104 	bool has_install = ops->install_pte;
105 	int err = 0;
106 	int depth = real_depth(3);
107 
108 	pmd = pmd_offset(pud, addr);
109 	do {
110 again:
111 		next = pmd_addr_end(addr, end);
112 		if (pmd_none(*pmd)) {
113 			if (has_install)
114 				err = __pte_alloc(walk->mm, pmd);
115 			else if (ops->pte_hole)
116 				err = ops->pte_hole(addr, next, depth, walk);
117 			if (err)
118 				break;
119 			if (!has_install)
120 				continue;
121 		}
122 
123 		walk->action = ACTION_SUBTREE;
124 
125 		/*
126 		 * This implies that each ->pmd_entry() handler
127 		 * needs to know about pmd_trans_huge() pmds
128 		 */
129 		if (ops->pmd_entry)
130 			err = ops->pmd_entry(pmd, addr, next, walk);
131 		if (err)
132 			break;
133 
134 		if (walk->action == ACTION_AGAIN)
135 			goto again;
136 		if (walk->action == ACTION_CONTINUE)
137 			continue;
138 
139 		if (!has_handler) { /* No handlers for lower page tables. */
140 			if (!has_install)
141 				continue; /* Nothing to do. */
142 			/*
143 			 * We are ONLY installing, so avoid unnecessarily
144 			 * splitting a present huge page.
145 			 */
146 			if (pmd_present(*pmd) && pmd_trans_huge(*pmd))
147 				continue;
148 		}
149 
150 		if (walk->vma)
151 			split_huge_pmd(walk->vma, pmd, addr);
152 		else if (pmd_leaf(*pmd) || !pmd_present(*pmd))
153 			continue; /* Nothing to do. */
154 
155 		err = walk_pte_range(pmd, addr, next, walk);
156 		if (err)
157 			break;
158 
159 		if (walk->action == ACTION_AGAIN)
160 			goto again;
161 
162 	} while (pmd++, addr = next, addr != end);
163 
164 	return err;
165 }
166 
167 static int walk_pud_range(p4d_t *p4d, unsigned long addr, unsigned long end,
168 			  struct mm_walk *walk)
169 {
170 	pud_t *pud;
171 	unsigned long next;
172 	const struct mm_walk_ops *ops = walk->ops;
173 	bool has_handler = ops->pmd_entry || ops->pte_entry;
174 	bool has_install = ops->install_pte;
175 	int err = 0;
176 	int depth = real_depth(2);
177 
178 	pud = pud_offset(p4d, addr);
179 	do {
180  again:
181 		next = pud_addr_end(addr, end);
182 		if (pud_none(*pud)) {
183 			if (has_install)
184 				err = __pmd_alloc(walk->mm, pud, addr);
185 			else if (ops->pte_hole)
186 				err = ops->pte_hole(addr, next, depth, walk);
187 			if (err)
188 				break;
189 			if (!has_install)
190 				continue;
191 		}
192 
193 		walk->action = ACTION_SUBTREE;
194 
195 		if (ops->pud_entry)
196 			err = ops->pud_entry(pud, addr, next, walk);
197 		if (err)
198 			break;
199 
200 		if (walk->action == ACTION_AGAIN)
201 			goto again;
202 		if (walk->action == ACTION_CONTINUE)
203 			continue;
204 
205 		if (!has_handler) { /* No handlers for lower page tables. */
206 			if (!has_install)
207 				continue; /* Nothing to do. */
208 			/*
209 			 * We are ONLY installing, so avoid unnecessarily
210 			 * splitting a present huge page.
211 			 */
212 			if (pud_present(*pud) && pud_trans_huge(*pud))
213 				continue;
214 		}
215 
216 		if (walk->vma)
217 			split_huge_pud(walk->vma, pud, addr);
218 		else if (pud_leaf(*pud) || !pud_present(*pud))
219 			continue; /* Nothing to do. */
220 
221 		if (pud_none(*pud))
222 			goto again;
223 
224 		err = walk_pmd_range(pud, addr, next, walk);
225 		if (err)
226 			break;
227 	} while (pud++, addr = next, addr != end);
228 
229 	return err;
230 }
231 
232 static int walk_p4d_range(pgd_t *pgd, unsigned long addr, unsigned long end,
233 			  struct mm_walk *walk)
234 {
235 	p4d_t *p4d;
236 	unsigned long next;
237 	const struct mm_walk_ops *ops = walk->ops;
238 	bool has_handler = ops->pud_entry || ops->pmd_entry || ops->pte_entry;
239 	bool has_install = ops->install_pte;
240 	int err = 0;
241 	int depth = real_depth(1);
242 
243 	p4d = p4d_offset(pgd, addr);
244 	do {
245 		next = p4d_addr_end(addr, end);
246 		if (p4d_none_or_clear_bad(p4d)) {
247 			if (has_install)
248 				err = __pud_alloc(walk->mm, p4d, addr);
249 			else if (ops->pte_hole)
250 				err = ops->pte_hole(addr, next, depth, walk);
251 			if (err)
252 				break;
253 			if (!has_install)
254 				continue;
255 		}
256 		if (ops->p4d_entry) {
257 			err = ops->p4d_entry(p4d, addr, next, walk);
258 			if (err)
259 				break;
260 		}
261 		if (has_handler || has_install)
262 			err = walk_pud_range(p4d, addr, next, walk);
263 		if (err)
264 			break;
265 	} while (p4d++, addr = next, addr != end);
266 
267 	return err;
268 }
269 
270 static int walk_pgd_range(unsigned long addr, unsigned long end,
271 			  struct mm_walk *walk)
272 {
273 	pgd_t *pgd;
274 	unsigned long next;
275 	const struct mm_walk_ops *ops = walk->ops;
276 	bool has_handler = ops->p4d_entry || ops->pud_entry || ops->pmd_entry ||
277 		ops->pte_entry;
278 	bool has_install = ops->install_pte;
279 	int err = 0;
280 
281 	if (walk->pgd)
282 		pgd = walk->pgd + pgd_index(addr);
283 	else
284 		pgd = pgd_offset(walk->mm, addr);
285 	do {
286 		next = pgd_addr_end(addr, end);
287 		if (pgd_none_or_clear_bad(pgd)) {
288 			if (has_install)
289 				err = __p4d_alloc(walk->mm, pgd, addr);
290 			else if (ops->pte_hole)
291 				err = ops->pte_hole(addr, next, 0, walk);
292 			if (err)
293 				break;
294 			if (!has_install)
295 				continue;
296 		}
297 		if (ops->pgd_entry) {
298 			err = ops->pgd_entry(pgd, addr, next, walk);
299 			if (err)
300 				break;
301 		}
302 		if (has_handler || has_install)
303 			err = walk_p4d_range(pgd, addr, next, walk);
304 		if (err)
305 			break;
306 	} while (pgd++, addr = next, addr != end);
307 
308 	return err;
309 }
310 
311 #ifdef CONFIG_HUGETLB_PAGE
312 static unsigned long hugetlb_entry_end(struct hstate *h, unsigned long addr,
313 				       unsigned long end)
314 {
315 	unsigned long boundary = (addr & huge_page_mask(h)) + huge_page_size(h);
316 	return boundary < end ? 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(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 the walk_page_range_mm() function to
522  * be able to use all page walking operations.
523  */
524 static bool check_ops_valid(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_valid(ops))
583 		return -EINVAL;
584 
585 	return walk_page_range_mm(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 	struct mm_struct *mm = &init_mm;
610 	struct mm_walk walk = {
611 		.ops		= ops,
612 		.mm		= mm,
613 		.pgd		= pgd,
614 		.private	= private,
615 		.no_vma		= true
616 	};
617 
618 	if (start >= end)
619 		return -EINVAL;
620 	if (!check_ops_valid(ops))
621 		return -EINVAL;
622 
623 	/*
624 	 * Kernel intermediate page tables are usually not freed, so the mmap
625 	 * read lock is sufficient. But there are some exceptions.
626 	 * E.g. memory hot-remove. In which case, the mmap lock is insufficient
627 	 * to prevent the intermediate kernel pages tables belonging to the
628 	 * specified address range from being freed. The caller should take
629 	 * other actions to prevent this race.
630 	 */
631 	mmap_assert_locked(mm);
632 
633 	return walk_pgd_range(start, end, &walk);
634 }
635 
636 /**
637  * walk_page_range_debug - walk a range of pagetables not backed by a vma
638  * @mm:		mm_struct representing the target process of page table walk
639  * @start:	start address of the virtual address range
640  * @end:	end address of the virtual address range
641  * @ops:	operation to call during the walk
642  * @pgd:	pgd to walk if different from mm->pgd
643  * @private:	private data for callbacks' usage
644  *
645  * Similar to walk_page_range() but can walk any page tables even if they are
646  * not backed by VMAs. Because 'unusual' entries may be walked this function
647  * will also not lock the PTEs for the pte_entry() callback.
648  *
649  * This is for debugging purposes ONLY.
650  */
651 int walk_page_range_debug(struct mm_struct *mm, unsigned long start,
652 			  unsigned long end, const struct mm_walk_ops *ops,
653 			  pgd_t *pgd, void *private)
654 {
655 	struct mm_walk walk = {
656 		.ops		= ops,
657 		.mm		= mm,
658 		.pgd		= pgd,
659 		.private	= private,
660 		.no_vma		= true
661 	};
662 
663 	/* For convenience, we allow traversal of kernel mappings. */
664 	if (mm == &init_mm)
665 		return walk_kernel_page_table_range(start, end, ops,
666 						    pgd, private);
667 	if (start >= end || !walk.mm)
668 		return -EINVAL;
669 	if (!check_ops_valid(ops))
670 		return -EINVAL;
671 
672 	/*
673 	 * The mmap lock protects the page walker from changes to the page
674 	 * tables during the walk.  However a read lock is insufficient to
675 	 * protect those areas which don't have a VMA as munmap() detaches
676 	 * the VMAs before downgrading to a read lock and actually tearing
677 	 * down PTEs/page tables. In which case, the mmap write lock should
678 	 * be held.
679 	 */
680 	mmap_assert_write_locked(mm);
681 
682 	return walk_pgd_range(start, end, &walk);
683 }
684 
685 int walk_page_range_vma(struct vm_area_struct *vma, unsigned long start,
686 			unsigned long end, const struct mm_walk_ops *ops,
687 			void *private)
688 {
689 	struct mm_walk walk = {
690 		.ops		= ops,
691 		.mm		= vma->vm_mm,
692 		.vma		= vma,
693 		.private	= private,
694 	};
695 
696 	if (start >= end || !walk.mm)
697 		return -EINVAL;
698 	if (start < vma->vm_start || end > vma->vm_end)
699 		return -EINVAL;
700 	if (!check_ops_valid(ops))
701 		return -EINVAL;
702 
703 	process_mm_walk_lock(walk.mm, ops->walk_lock);
704 	process_vma_walk_lock(vma, ops->walk_lock);
705 	return __walk_page_range(start, end, &walk);
706 }
707 
708 int walk_page_vma(struct vm_area_struct *vma, const struct mm_walk_ops *ops,
709 		void *private)
710 {
711 	struct mm_walk walk = {
712 		.ops		= ops,
713 		.mm		= vma->vm_mm,
714 		.vma		= vma,
715 		.private	= private,
716 	};
717 
718 	if (!walk.mm)
719 		return -EINVAL;
720 	if (!check_ops_valid(ops))
721 		return -EINVAL;
722 
723 	process_mm_walk_lock(walk.mm, ops->walk_lock);
724 	process_vma_walk_lock(vma, ops->walk_lock);
725 	return __walk_page_range(vma->vm_start, vma->vm_end, &walk);
726 }
727 
728 /**
729  * walk_page_mapping - walk all memory areas mapped into a struct address_space.
730  * @mapping: Pointer to the struct address_space
731  * @first_index: First page offset in the address_space
732  * @nr: Number of incremental page offsets to cover
733  * @ops:	operation to call during the walk
734  * @private:	private data for callbacks' usage
735  *
736  * This function walks all memory areas mapped into a struct address_space.
737  * The walk is limited to only the given page-size index range, but if
738  * the index boundaries cross a huge page-table entry, that entry will be
739  * included.
740  *
741  * Also see walk_page_range() for additional information.
742  *
743  * Locking:
744  *   This function can't require that the struct mm_struct::mmap_lock is held,
745  *   since @mapping may be mapped by multiple processes. Instead
746  *   @mapping->i_mmap_rwsem must be held. This might have implications in the
747  *   callbacks, and it's up tho the caller to ensure that the
748  *   struct mm_struct::mmap_lock is not needed.
749  *
750  *   Also this means that a caller can't rely on the struct
751  *   vm_area_struct::vm_flags to be constant across a call,
752  *   except for immutable flags. Callers requiring this shouldn't use
753  *   this function.
754  *
755  * Return: 0 on success, negative error code on failure, positive number on
756  * caller defined premature termination.
757  */
758 int walk_page_mapping(struct address_space *mapping, pgoff_t first_index,
759 		      pgoff_t nr, const struct mm_walk_ops *ops,
760 		      void *private)
761 {
762 	struct mm_walk walk = {
763 		.ops		= ops,
764 		.private	= private,
765 	};
766 	struct vm_area_struct *vma;
767 	pgoff_t vba, vea, cba, cea;
768 	unsigned long start_addr, end_addr;
769 	int err = 0;
770 
771 	if (!check_ops_valid(ops))
772 		return -EINVAL;
773 
774 	lockdep_assert_held(&mapping->i_mmap_rwsem);
775 	vma_interval_tree_foreach(vma, &mapping->i_mmap, first_index,
776 				  first_index + nr - 1) {
777 		/* Clip to the vma */
778 		vba = vma->vm_pgoff;
779 		vea = vba + vma_pages(vma);
780 		cba = first_index;
781 		cba = max(cba, vba);
782 		cea = first_index + nr;
783 		cea = min(cea, vea);
784 
785 		start_addr = ((cba - vba) << PAGE_SHIFT) + vma->vm_start;
786 		end_addr = ((cea - vba) << PAGE_SHIFT) + vma->vm_start;
787 		if (start_addr >= end_addr)
788 			continue;
789 
790 		walk.vma = vma;
791 		walk.mm = vma->vm_mm;
792 
793 		err = walk_page_test(vma->vm_start, vma->vm_end, &walk);
794 		if (err > 0) {
795 			err = 0;
796 			break;
797 		} else if (err < 0)
798 			break;
799 
800 		err = __walk_page_range(start_addr, end_addr, &walk);
801 		if (err)
802 			break;
803 	}
804 
805 	return err;
806 }
807 
808 /**
809  * folio_walk_start - walk the page tables to a folio
810  * @fw: filled with information on success.
811  * @vma: the VMA.
812  * @addr: the virtual address to use for the page table walk.
813  * @flags: flags modifying which folios to walk to.
814  *
815  * Walk the page tables using @addr in a given @vma to a mapped folio and
816  * return the folio, making sure that the page table entry referenced by
817  * @addr cannot change until folio_walk_end() was called.
818  *
819  * As default, this function returns only folios that are not special (e.g., not
820  * the zeropage) and never returns folios that are supposed to be ignored by the
821  * VM as documented by vm_normal_page(). If requested, zeropages will be
822  * returned as well.
823  *
824  * As default, this function only considers present page table entries.
825  * If requested, it will also consider migration entries.
826  *
827  * If this function returns NULL it might either indicate "there is nothing" or
828  * "there is nothing suitable".
829  *
830  * On success, @fw is filled and the function returns the folio while the PTL
831  * is still held and folio_walk_end() must be called to clean up,
832  * releasing any held locks. The returned folio must *not* be used after the
833  * call to folio_walk_end(), unless a short-term folio reference is taken before
834  * that call.
835  *
836  * @fw->page will correspond to the page that is effectively referenced by
837  * @addr. However, for migration entries and shared zeropages @fw->page is
838  * set to NULL. Note that large folios might be mapped by multiple page table
839  * entries, and this function will always only lookup a single entry as
840  * specified by @addr, which might or might not cover more than a single page of
841  * the returned folio.
842  *
843  * This function must *not* be used as a naive replacement for
844  * get_user_pages() / pin_user_pages(), especially not to perform DMA or
845  * to carelessly modify page content. This function may *only* be used to grab
846  * short-term folio references, never to grab long-term folio references.
847  *
848  * Using the page table entry pointers in @fw for reading or modifying the
849  * entry should be avoided where possible: however, there might be valid
850  * use cases.
851  *
852  * WARNING: Modifying page table entries in hugetlb VMAs requires a lot of care.
853  * For example, PMD page table sharing might require prior unsharing. Also,
854  * logical hugetlb entries might span multiple physical page table entries,
855  * which *must* be modified in a single operation (set_huge_pte_at(),
856  * huge_ptep_set_*, ...). Note that the page table entry stored in @fw might
857  * not correspond to the first physical entry of a logical hugetlb entry.
858  *
859  * The mmap lock must be held in read mode.
860  *
861  * Return: folio pointer on success, otherwise NULL.
862  */
863 struct folio *folio_walk_start(struct folio_walk *fw,
864 		struct vm_area_struct *vma, unsigned long addr,
865 		folio_walk_flags_t flags)
866 {
867 	unsigned long entry_size;
868 	bool expose_page = true;
869 	struct page *page;
870 	pud_t *pudp, pud;
871 	pmd_t *pmdp, pmd;
872 	pte_t *ptep, pte;
873 	spinlock_t *ptl;
874 	pgd_t *pgdp;
875 	p4d_t *p4dp;
876 
877 	mmap_assert_locked(vma->vm_mm);
878 	vma_pgtable_walk_begin(vma);
879 
880 	if (WARN_ON_ONCE(addr < vma->vm_start || addr >= vma->vm_end))
881 		goto not_found;
882 
883 	pgdp = pgd_offset(vma->vm_mm, addr);
884 	if (pgd_none_or_clear_bad(pgdp))
885 		goto not_found;
886 
887 	p4dp = p4d_offset(pgdp, addr);
888 	if (p4d_none_or_clear_bad(p4dp))
889 		goto not_found;
890 
891 	pudp = pud_offset(p4dp, addr);
892 	pud = pudp_get(pudp);
893 	if (pud_none(pud))
894 		goto not_found;
895 	if (IS_ENABLED(CONFIG_PGTABLE_HAS_HUGE_LEAVES) &&
896 	    (!pud_present(pud) || pud_leaf(pud))) {
897 		ptl = pud_lock(vma->vm_mm, pudp);
898 		pud = pudp_get(pudp);
899 
900 		entry_size = PUD_SIZE;
901 		fw->level = FW_LEVEL_PUD;
902 		fw->pudp = pudp;
903 		fw->pud = pud;
904 
905 		/*
906 		 * TODO: FW_MIGRATION support for PUD migration entries
907 		 * once there are relevant users.
908 		 */
909 		if (!pud_present(pud) || pud_special(pud)) {
910 			spin_unlock(ptl);
911 			goto not_found;
912 		} else if (!pud_leaf(pud)) {
913 			spin_unlock(ptl);
914 			goto pmd_table;
915 		}
916 		/*
917 		 * TODO: vm_normal_page_pud() will be handy once we want to
918 		 * support PUD mappings in VM_PFNMAP|VM_MIXEDMAP VMAs.
919 		 */
920 		page = pud_page(pud);
921 		goto found;
922 	}
923 
924 pmd_table:
925 	VM_WARN_ON_ONCE(!pud_present(pud) || pud_leaf(pud));
926 	pmdp = pmd_offset(pudp, addr);
927 	pmd = pmdp_get_lockless(pmdp);
928 	if (pmd_none(pmd))
929 		goto not_found;
930 	if (IS_ENABLED(CONFIG_PGTABLE_HAS_HUGE_LEAVES) &&
931 	    (!pmd_present(pmd) || pmd_leaf(pmd))) {
932 		ptl = pmd_lock(vma->vm_mm, pmdp);
933 		pmd = pmdp_get(pmdp);
934 
935 		entry_size = PMD_SIZE;
936 		fw->level = FW_LEVEL_PMD;
937 		fw->pmdp = pmdp;
938 		fw->pmd = pmd;
939 
940 		if (pmd_none(pmd)) {
941 			spin_unlock(ptl);
942 			goto not_found;
943 		} else if (pmd_present(pmd) && !pmd_leaf(pmd)) {
944 			spin_unlock(ptl);
945 			goto pte_table;
946 		} else if (pmd_present(pmd)) {
947 			page = vm_normal_page_pmd(vma, addr, pmd);
948 			if (page) {
949 				goto found;
950 			} else if ((flags & FW_ZEROPAGE) &&
951 				    is_huge_zero_pmd(pmd)) {
952 				page = pfn_to_page(pmd_pfn(pmd));
953 				expose_page = false;
954 				goto found;
955 			}
956 		} else if ((flags & FW_MIGRATION) &&
957 			   is_pmd_migration_entry(pmd)) {
958 			swp_entry_t entry = pmd_to_swp_entry(pmd);
959 
960 			page = pfn_swap_entry_to_page(entry);
961 			expose_page = false;
962 			goto found;
963 		}
964 		spin_unlock(ptl);
965 		goto not_found;
966 	}
967 
968 pte_table:
969 	VM_WARN_ON_ONCE(!pmd_present(pmd) || pmd_leaf(pmd));
970 	ptep = pte_offset_map_lock(vma->vm_mm, pmdp, addr, &ptl);
971 	if (!ptep)
972 		goto not_found;
973 	pte = ptep_get(ptep);
974 
975 	entry_size = PAGE_SIZE;
976 	fw->level = FW_LEVEL_PTE;
977 	fw->ptep = ptep;
978 	fw->pte = pte;
979 
980 	if (pte_present(pte)) {
981 		page = vm_normal_page(vma, addr, pte);
982 		if (page)
983 			goto found;
984 		if ((flags & FW_ZEROPAGE) &&
985 		    is_zero_pfn(pte_pfn(pte))) {
986 			page = pfn_to_page(pte_pfn(pte));
987 			expose_page = false;
988 			goto found;
989 		}
990 	} else if (!pte_none(pte)) {
991 		swp_entry_t entry = pte_to_swp_entry(pte);
992 
993 		if ((flags & FW_MIGRATION) &&
994 		    is_migration_entry(entry)) {
995 			page = pfn_swap_entry_to_page(entry);
996 			expose_page = false;
997 			goto found;
998 		}
999 	}
1000 	pte_unmap_unlock(ptep, ptl);
1001 not_found:
1002 	vma_pgtable_walk_end(vma);
1003 	return NULL;
1004 found:
1005 	if (expose_page)
1006 		/* Note: Offset from the mapped page, not the folio start. */
1007 		fw->page = nth_page(page, (addr & (entry_size - 1)) >> PAGE_SHIFT);
1008 	else
1009 		fw->page = NULL;
1010 	fw->ptl = ptl;
1011 	return page_folio(page);
1012 }
1013