xref: /linux/mm/pagewalk.c (revision 6aacab308a5dfd222b2d23662bbae60c11007cfb)
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/leafops.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 
317 	return min(boundary, end);
318 }
319 
320 static int walk_hugetlb_range(unsigned long addr, unsigned long end,
321 			      struct mm_walk *walk)
322 {
323 	struct vm_area_struct *vma = walk->vma;
324 	struct hstate *h = hstate_vma(vma);
325 	unsigned long next;
326 	unsigned long hmask = huge_page_mask(h);
327 	unsigned long sz = huge_page_size(h);
328 	pte_t *pte;
329 	const struct mm_walk_ops *ops = walk->ops;
330 	int err = 0;
331 
332 	hugetlb_vma_lock_read(vma);
333 	do {
334 		next = hugetlb_entry_end(h, addr, end);
335 		pte = hugetlb_walk(vma, addr & hmask, sz);
336 		if (pte)
337 			err = ops->hugetlb_entry(pte, hmask, addr, next, walk);
338 		else if (ops->pte_hole)
339 			err = ops->pte_hole(addr, next, -1, walk);
340 		if (err)
341 			break;
342 	} while (addr = next, addr != end);
343 	hugetlb_vma_unlock_read(vma);
344 
345 	return err;
346 }
347 
348 #else /* CONFIG_HUGETLB_PAGE */
349 static int walk_hugetlb_range(unsigned long addr, unsigned long end,
350 			      struct mm_walk *walk)
351 {
352 	return 0;
353 }
354 
355 #endif /* CONFIG_HUGETLB_PAGE */
356 
357 /*
358  * Decide whether we really walk over the current vma on [@start, @end)
359  * or skip it via the returned value. Return 0 if we do walk over the
360  * current vma, and return 1 if we skip the vma. Negative values means
361  * error, where we abort the current walk.
362  */
363 static int walk_page_test(unsigned long start, unsigned long end,
364 			struct mm_walk *walk)
365 {
366 	struct vm_area_struct *vma = walk->vma;
367 	const struct mm_walk_ops *ops = walk->ops;
368 
369 	if (ops->test_walk)
370 		return ops->test_walk(start, end, walk);
371 
372 	/*
373 	 * vma(VM_PFNMAP) doesn't have any valid struct pages behind VM_PFNMAP
374 	 * range, so we don't walk over it as we do for normal vmas. However,
375 	 * Some callers are interested in handling hole range and they don't
376 	 * want to just ignore any single address range. Such users certainly
377 	 * define their ->pte_hole() callbacks, so let's delegate them to handle
378 	 * vma(VM_PFNMAP).
379 	 */
380 	if (vma->vm_flags & VM_PFNMAP) {
381 		int err = 1;
382 		if (ops->pte_hole)
383 			err = ops->pte_hole(start, end, -1, walk);
384 		return err ? err : 1;
385 	}
386 	return 0;
387 }
388 
389 static int __walk_page_range(unsigned long start, unsigned long end,
390 			struct mm_walk *walk)
391 {
392 	int err = 0;
393 	struct vm_area_struct *vma = walk->vma;
394 	const struct mm_walk_ops *ops = walk->ops;
395 	bool is_hugetlb = is_vm_hugetlb_page(vma);
396 
397 	/* We do not support hugetlb PTE installation. */
398 	if (ops->install_pte && is_hugetlb)
399 		return -EINVAL;
400 
401 	if (ops->pre_vma) {
402 		err = ops->pre_vma(start, end, walk);
403 		if (err)
404 			return err;
405 	}
406 
407 	if (is_hugetlb) {
408 		if (ops->hugetlb_entry)
409 			err = walk_hugetlb_range(start, end, walk);
410 	} else
411 		err = walk_pgd_range(start, end, walk);
412 
413 	if (ops->post_vma)
414 		ops->post_vma(walk);
415 
416 	return err;
417 }
418 
419 static inline void process_mm_walk_lock(struct mm_struct *mm,
420 					enum page_walk_lock walk_lock)
421 {
422 	if (walk_lock == PGWALK_RDLOCK)
423 		mmap_assert_locked(mm);
424 	else if (walk_lock != PGWALK_VMA_RDLOCK_VERIFY)
425 		mmap_assert_write_locked(mm);
426 }
427 
428 static inline void process_vma_walk_lock(struct vm_area_struct *vma,
429 					 enum page_walk_lock walk_lock)
430 {
431 #ifdef CONFIG_PER_VMA_LOCK
432 	switch (walk_lock) {
433 	case PGWALK_WRLOCK:
434 		vma_start_write(vma);
435 		break;
436 	case PGWALK_WRLOCK_VERIFY:
437 		vma_assert_write_locked(vma);
438 		break;
439 	case PGWALK_VMA_RDLOCK_VERIFY:
440 		vma_assert_locked(vma);
441 		break;
442 	case PGWALK_RDLOCK:
443 		/* PGWALK_RDLOCK is handled by process_mm_walk_lock */
444 		break;
445 	}
446 #endif
447 }
448 
449 /*
450  * See the comment for walk_page_range(), this performs the heavy lifting of the
451  * operation, only sets no restrictions on how the walk proceeds.
452  *
453  * We usually restrict the ability to install PTEs, but this functionality is
454  * available to internal memory management code and provided in mm/internal.h.
455  */
456 int walk_page_range_mm_unsafe(struct mm_struct *mm, unsigned long start,
457 		unsigned long end, const struct mm_walk_ops *ops,
458 		void *private)
459 {
460 	int err = 0;
461 	unsigned long next;
462 	struct vm_area_struct *vma;
463 	struct mm_walk walk = {
464 		.ops		= ops,
465 		.mm		= mm,
466 		.private	= private,
467 	};
468 
469 	if (start >= end)
470 		return -EINVAL;
471 
472 	if (!walk.mm)
473 		return -EINVAL;
474 
475 	process_mm_walk_lock(walk.mm, ops->walk_lock);
476 
477 	vma = find_vma(walk.mm, start);
478 	do {
479 		if (!vma) { /* after the last vma */
480 			walk.vma = NULL;
481 			next = end;
482 			if (ops->pte_hole)
483 				err = ops->pte_hole(start, next, -1, &walk);
484 		} else if (start < vma->vm_start) { /* outside vma */
485 			walk.vma = NULL;
486 			next = min(end, vma->vm_start);
487 			if (ops->pte_hole)
488 				err = ops->pte_hole(start, next, -1, &walk);
489 		} else { /* inside vma */
490 			process_vma_walk_lock(vma, ops->walk_lock);
491 			walk.vma = vma;
492 			next = min(end, vma->vm_end);
493 			vma = find_vma(mm, vma->vm_end);
494 
495 			err = walk_page_test(start, next, &walk);
496 			if (err > 0) {
497 				/*
498 				 * positive return values are purely for
499 				 * controlling the pagewalk, so should never
500 				 * be passed to the callers.
501 				 */
502 				err = 0;
503 				continue;
504 			}
505 			if (err < 0)
506 				break;
507 			err = __walk_page_range(start, next, &walk);
508 		}
509 		if (err)
510 			break;
511 	} while (start = next, start < end);
512 	return err;
513 }
514 
515 /*
516  * Determine if the walk operations specified are permitted to be used for a
517  * page table walk.
518  *
519  * This check is performed on all functions which are parameterised by walk
520  * operations and exposed in include/linux/pagewalk.h.
521  *
522  * Internal memory management code can use *_unsafe() functions to be able to
523  * use all page walking operations.
524  */
525 static bool check_ops_safe(const struct mm_walk_ops *ops)
526 {
527 	/*
528 	 * The installation of PTEs is solely under the control of memory
529 	 * management logic and subject to many subtle locking, security and
530 	 * cache considerations so we cannot permit other users to do so, and
531 	 * certainly not for exported symbols.
532 	 */
533 	if (ops->install_pte)
534 		return false;
535 
536 	return true;
537 }
538 
539 /**
540  * walk_page_range - walk page table with caller specific callbacks
541  * @mm:		mm_struct representing the target process of page table walk
542  * @start:	start address of the virtual address range
543  * @end:	end address of the virtual address range
544  * @ops:	operation to call during the walk
545  * @private:	private data for callbacks' usage
546  *
547  * Recursively walk the page table tree of the process represented by @mm
548  * within the virtual address range [@start, @end). During walking, we can do
549  * some caller-specific works for each entry, by setting up pmd_entry(),
550  * pte_entry(), and/or hugetlb_entry(). If you don't set up for some of these
551  * callbacks, the associated entries/pages are just ignored.
552  * The return values of these callbacks are commonly defined like below:
553  *
554  *  - 0  : succeeded to handle the current entry, and if you don't reach the
555  *         end address yet, continue to walk.
556  *  - >0 : succeeded to handle the current entry, and return to the caller
557  *         with caller specific value.
558  *  - <0 : failed to handle the current entry, and return to the caller
559  *         with error code.
560  *
561  * Before starting to walk page table, some callers want to check whether
562  * they really want to walk over the current vma, typically by checking
563  * its vm_flags. walk_page_test() and @ops->test_walk() are used for this
564  * purpose.
565  *
566  * If operations need to be staged before and committed after a vma is walked,
567  * there are two callbacks, pre_vma() and post_vma(). Note that post_vma(),
568  * since it is intended to handle commit-type operations, can't return any
569  * errors.
570  *
571  * struct mm_walk keeps current values of some common data like vma and pmd,
572  * which are useful for the access from callbacks. If you want to pass some
573  * caller-specific data to callbacks, @private should be helpful.
574  *
575  * Locking:
576  *   Callers of walk_page_range() and walk_page_vma() should hold @mm->mmap_lock,
577  *   because these function traverse vma list and/or access to vma's data.
578  */
579 int walk_page_range(struct mm_struct *mm, unsigned long start,
580 		unsigned long end, const struct mm_walk_ops *ops,
581 		void *private)
582 {
583 	if (!check_ops_safe(ops))
584 		return -EINVAL;
585 
586 	return walk_page_range_mm_unsafe(mm, start, end, ops, private);
587 }
588 
589 /**
590  * walk_kernel_page_table_range - walk a range of kernel pagetables.
591  * @start:	start address of the virtual address range
592  * @end:	end address of the virtual address range
593  * @ops:	operation to call during the walk
594  * @pgd:	pgd to walk if different from mm->pgd
595  * @private:	private data for callbacks' usage
596  *
597  * Similar to walk_page_range() but can walk any page tables even if they are
598  * not backed by VMAs. Because 'unusual' entries may be walked this function
599  * will also not lock the PTEs for the pte_entry() callback. This is useful for
600  * walking kernel pages tables or page tables for firmware.
601  *
602  * Note: Be careful to walk the kernel pages tables, the caller may be need to
603  * take other effective approaches (mmap lock may be insufficient) to prevent
604  * the intermediate kernel page tables belonging to the specified address range
605  * from being freed (e.g. memory hot-remove).
606  */
607 int walk_kernel_page_table_range(unsigned long start, unsigned long end,
608 		const struct mm_walk_ops *ops, pgd_t *pgd, void *private)
609 {
610 	/*
611 	 * Kernel intermediate page tables are usually not freed, so the mmap
612 	 * read lock is sufficient. But there are some exceptions.
613 	 * E.g. memory hot-remove. In which case, the mmap lock is insufficient
614 	 * to prevent the intermediate kernel pages tables belonging to the
615 	 * specified address range from being freed. The caller should take
616 	 * other actions to prevent this race.
617 	 */
618 	mmap_assert_locked(&init_mm);
619 
620 	return walk_kernel_page_table_range_lockless(start, end, ops, pgd,
621 						     private);
622 }
623 
624 /*
625  * Use this function to walk the kernel page tables locklessly. It should be
626  * guaranteed that the caller has exclusive access over the range they are
627  * operating on - that there should be no concurrent access, for example,
628  * changing permissions for vmalloc objects.
629  */
630 int walk_kernel_page_table_range_lockless(unsigned long start, unsigned long end,
631 		const struct mm_walk_ops *ops, pgd_t *pgd, void *private)
632 {
633 	struct mm_walk walk = {
634 		.ops		= ops,
635 		.mm		= &init_mm,
636 		.pgd		= pgd,
637 		.private	= private,
638 		.no_vma		= true
639 	};
640 
641 	if (start >= end)
642 		return -EINVAL;
643 	if (!check_ops_safe(ops))
644 		return -EINVAL;
645 
646 	return walk_pgd_range(start, end, &walk);
647 }
648 
649 /**
650  * walk_page_range_debug - walk a range of pagetables not backed by a vma
651  * @mm:		mm_struct representing the target process of page table walk
652  * @start:	start address of the virtual address range
653  * @end:	end address of the virtual address range
654  * @ops:	operation to call during the walk
655  * @pgd:	pgd to walk if different from mm->pgd
656  * @private:	private data for callbacks' usage
657  *
658  * Similar to walk_page_range() but can walk any page tables even if they are
659  * not backed by VMAs. Because 'unusual' entries may be walked this function
660  * will also not lock the PTEs for the pte_entry() callback.
661  *
662  * This is for debugging purposes ONLY.
663  */
664 int walk_page_range_debug(struct mm_struct *mm, unsigned long start,
665 			  unsigned long end, const struct mm_walk_ops *ops,
666 			  pgd_t *pgd, void *private)
667 {
668 	struct mm_walk walk = {
669 		.ops		= ops,
670 		.mm		= mm,
671 		.pgd		= pgd,
672 		.private	= private,
673 		.no_vma		= true
674 	};
675 
676 	/* For convenience, we allow traversal of kernel mappings. */
677 	if (mm == &init_mm)
678 		return walk_kernel_page_table_range(start, end, ops,
679 						    pgd, private);
680 	if (start >= end || !walk.mm)
681 		return -EINVAL;
682 	if (!check_ops_safe(ops))
683 		return -EINVAL;
684 
685 	/*
686 	 * The mmap lock protects the page walker from changes to the page
687 	 * tables during the walk.  However a read lock is insufficient to
688 	 * protect those areas which don't have a VMA as munmap() detaches
689 	 * the VMAs before downgrading to a read lock and actually tearing
690 	 * down PTEs/page tables. In which case, the mmap write lock should
691 	 * be held.
692 	 */
693 	mmap_assert_write_locked(mm);
694 
695 	return walk_pgd_range(start, end, &walk);
696 }
697 
698 int walk_page_range_vma_unsafe(struct vm_area_struct *vma, unsigned long start,
699 		unsigned long end, const struct mm_walk_ops *ops, void *private)
700 {
701 	struct mm_walk walk = {
702 		.ops		= ops,
703 		.mm		= vma->vm_mm,
704 		.vma		= vma,
705 		.private	= private,
706 	};
707 
708 	if (start >= end || !walk.mm)
709 		return -EINVAL;
710 	if (start < vma->vm_start || end > vma->vm_end)
711 		return -EINVAL;
712 
713 	process_mm_walk_lock(walk.mm, ops->walk_lock);
714 	process_vma_walk_lock(vma, ops->walk_lock);
715 	return __walk_page_range(start, end, &walk);
716 }
717 
718 int walk_page_range_vma(struct vm_area_struct *vma, unsigned long start,
719 			unsigned long end, const struct mm_walk_ops *ops,
720 			void *private)
721 {
722 	if (!check_ops_safe(ops))
723 		return -EINVAL;
724 
725 	return walk_page_range_vma_unsafe(vma, start, end, ops, private);
726 }
727 
728 int walk_page_vma(struct vm_area_struct *vma, const struct mm_walk_ops *ops,
729 		void *private)
730 {
731 	struct mm_walk walk = {
732 		.ops		= ops,
733 		.mm		= vma->vm_mm,
734 		.vma		= vma,
735 		.private	= private,
736 	};
737 
738 	if (!walk.mm)
739 		return -EINVAL;
740 	if (!check_ops_safe(ops))
741 		return -EINVAL;
742 
743 	process_mm_walk_lock(walk.mm, ops->walk_lock);
744 	process_vma_walk_lock(vma, ops->walk_lock);
745 	return __walk_page_range(vma->vm_start, vma->vm_end, &walk);
746 }
747 
748 /**
749  * walk_page_mapping - walk all memory areas mapped into a struct address_space.
750  * @mapping: Pointer to the struct address_space
751  * @first_index: First page offset in the address_space
752  * @nr: Number of incremental page offsets to cover
753  * @ops:	operation to call during the walk
754  * @private:	private data for callbacks' usage
755  *
756  * This function walks all memory areas mapped into a struct address_space.
757  * The walk is limited to only the given page-size index range, but if
758  * the index boundaries cross a huge page-table entry, that entry will be
759  * included.
760  *
761  * Also see walk_page_range() for additional information.
762  *
763  * Locking:
764  *   This function can't require that the struct mm_struct::mmap_lock is held,
765  *   since @mapping may be mapped by multiple processes. Instead
766  *   @mapping->i_mmap_rwsem must be held. This might have implications in the
767  *   callbacks, and it's up tho the caller to ensure that the
768  *   struct mm_struct::mmap_lock is not needed.
769  *
770  *   Also this means that a caller can't rely on the struct
771  *   vm_area_struct::vm_flags to be constant across a call,
772  *   except for immutable flags. Callers requiring this shouldn't use
773  *   this function.
774  *
775  * Return: 0 on success, negative error code on failure, positive number on
776  * caller defined premature termination.
777  */
778 int walk_page_mapping(struct address_space *mapping, pgoff_t first_index,
779 		      pgoff_t nr, const struct mm_walk_ops *ops,
780 		      void *private)
781 {
782 	struct mm_walk walk = {
783 		.ops		= ops,
784 		.private	= private,
785 	};
786 	struct vm_area_struct *vma;
787 	pgoff_t vba, vea, cba, cea;
788 	unsigned long start_addr, end_addr;
789 	int err = 0;
790 
791 	if (!check_ops_safe(ops))
792 		return -EINVAL;
793 
794 	lockdep_assert_held(&mapping->i_mmap_rwsem);
795 	vma_interval_tree_foreach(vma, &mapping->i_mmap, first_index,
796 				  first_index + nr - 1) {
797 		/* Clip to the vma */
798 		vba = vma->vm_pgoff;
799 		vea = vba + vma_pages(vma);
800 		cba = first_index;
801 		cba = max(cba, vba);
802 		cea = first_index + nr;
803 		cea = min(cea, vea);
804 
805 		start_addr = ((cba - vba) << PAGE_SHIFT) + vma->vm_start;
806 		end_addr = ((cea - vba) << PAGE_SHIFT) + vma->vm_start;
807 		if (start_addr >= end_addr)
808 			continue;
809 
810 		walk.vma = vma;
811 		walk.mm = vma->vm_mm;
812 
813 		err = walk_page_test(vma->vm_start, vma->vm_end, &walk);
814 		if (err > 0) {
815 			err = 0;
816 			break;
817 		} else if (err < 0)
818 			break;
819 
820 		err = __walk_page_range(start_addr, end_addr, &walk);
821 		if (err)
822 			break;
823 	}
824 
825 	return err;
826 }
827 
828 /**
829  * folio_walk_start - walk the page tables to a folio
830  * @fw: filled with information on success.
831  * @vma: the VMA.
832  * @addr: the virtual address to use for the page table walk.
833  * @flags: flags modifying which folios to walk to.
834  *
835  * Walk the page tables using @addr in a given @vma to a mapped folio and
836  * return the folio, making sure that the page table entry referenced by
837  * @addr cannot change until folio_walk_end() was called.
838  *
839  * As default, this function returns only folios that are not special (e.g., not
840  * the zeropage) and never returns folios that are supposed to be ignored by the
841  * VM as documented by vm_normal_page(). If requested, zeropages will be
842  * returned as well.
843  *
844  * As default, this function only considers present page table entries.
845  * If requested, it will also consider migration entries.
846  *
847  * If this function returns NULL it might either indicate "there is nothing" or
848  * "there is nothing suitable".
849  *
850  * On success, @fw is filled and the function returns the folio while the PTL
851  * is still held and folio_walk_end() must be called to clean up,
852  * releasing any held locks. The returned folio must *not* be used after the
853  * call to folio_walk_end(), unless a short-term folio reference is taken before
854  * that call.
855  *
856  * @fw->page will correspond to the page that is effectively referenced by
857  * @addr. However, for migration entries and shared zeropages @fw->page is
858  * set to NULL. Note that large folios might be mapped by multiple page table
859  * entries, and this function will always only lookup a single entry as
860  * specified by @addr, which might or might not cover more than a single page of
861  * the returned folio.
862  *
863  * This function must *not* be used as a naive replacement for
864  * get_user_pages() / pin_user_pages(), especially not to perform DMA or
865  * to carelessly modify page content. This function may *only* be used to grab
866  * short-term folio references, never to grab long-term folio references.
867  *
868  * Using the page table entry pointers in @fw for reading or modifying the
869  * entry should be avoided where possible: however, there might be valid
870  * use cases.
871  *
872  * WARNING: Modifying page table entries in hugetlb VMAs requires a lot of care.
873  * For example, PMD page table sharing might require prior unsharing. Also,
874  * logical hugetlb entries might span multiple physical page table entries,
875  * which *must* be modified in a single operation (set_huge_pte_at(),
876  * huge_ptep_set_*, ...). Note that the page table entry stored in @fw might
877  * not correspond to the first physical entry of a logical hugetlb entry.
878  *
879  * The mmap lock must be held in read mode.
880  *
881  * Return: folio pointer on success, otherwise NULL.
882  */
883 struct folio *folio_walk_start(struct folio_walk *fw,
884 		struct vm_area_struct *vma, unsigned long addr,
885 		folio_walk_flags_t flags)
886 {
887 	unsigned long entry_size;
888 	bool expose_page = true;
889 	struct page *page;
890 	pud_t *pudp, pud;
891 	pmd_t *pmdp, pmd;
892 	pte_t *ptep, pte;
893 	spinlock_t *ptl;
894 	pgd_t *pgdp;
895 	p4d_t *p4dp;
896 
897 	mmap_assert_locked(vma->vm_mm);
898 	vma_pgtable_walk_begin(vma);
899 
900 	if (WARN_ON_ONCE(addr < vma->vm_start || addr >= vma->vm_end))
901 		goto not_found;
902 
903 	pgdp = pgd_offset(vma->vm_mm, addr);
904 	if (pgd_none_or_clear_bad(pgdp))
905 		goto not_found;
906 
907 	p4dp = p4d_offset(pgdp, addr);
908 	if (p4d_none_or_clear_bad(p4dp))
909 		goto not_found;
910 
911 	pudp = pud_offset(p4dp, addr);
912 	pud = pudp_get(pudp);
913 	if (pud_none(pud))
914 		goto not_found;
915 	if (IS_ENABLED(CONFIG_PGTABLE_HAS_HUGE_LEAVES) &&
916 	    (!pud_present(pud) || pud_leaf(pud))) {
917 		ptl = pud_lock(vma->vm_mm, pudp);
918 		pud = pudp_get(pudp);
919 
920 		entry_size = PUD_SIZE;
921 		fw->level = FW_LEVEL_PUD;
922 		fw->pudp = pudp;
923 		fw->pud = pud;
924 
925 		if (pud_none(pud)) {
926 			spin_unlock(ptl);
927 			goto not_found;
928 		} else if (pud_present(pud) && !pud_leaf(pud)) {
929 			spin_unlock(ptl);
930 			goto pmd_table;
931 		} else if (pud_present(pud)) {
932 			page = vm_normal_page_pud(vma, addr, pud);
933 			if (page)
934 				goto found;
935 		}
936 		/*
937 		 * TODO: FW_MIGRATION support for PUD migration entries
938 		 * once there are relevant users.
939 		 */
940 		spin_unlock(ptl);
941 		goto not_found;
942 	}
943 
944 pmd_table:
945 	VM_WARN_ON_ONCE(!pud_present(pud) || pud_leaf(pud));
946 	pmdp = pmd_offset(pudp, addr);
947 	pmd = pmdp_get_lockless(pmdp);
948 	if (pmd_none(pmd))
949 		goto not_found;
950 	if (IS_ENABLED(CONFIG_PGTABLE_HAS_HUGE_LEAVES) &&
951 	    (!pmd_present(pmd) || pmd_leaf(pmd))) {
952 		ptl = pmd_lock(vma->vm_mm, pmdp);
953 		pmd = pmdp_get(pmdp);
954 
955 		entry_size = PMD_SIZE;
956 		fw->level = FW_LEVEL_PMD;
957 		fw->pmdp = pmdp;
958 		fw->pmd = pmd;
959 
960 		if (pmd_none(pmd)) {
961 			spin_unlock(ptl);
962 			goto not_found;
963 		} else if (pmd_present(pmd) && !pmd_leaf(pmd)) {
964 			spin_unlock(ptl);
965 			goto pte_table;
966 		} else if (pmd_present(pmd)) {
967 			page = vm_normal_page_pmd(vma, addr, pmd);
968 			if (page) {
969 				goto found;
970 			} else if ((flags & FW_ZEROPAGE) &&
971 				    is_huge_zero_pmd(pmd)) {
972 				page = pfn_to_page(pmd_pfn(pmd));
973 				expose_page = false;
974 				goto found;
975 			}
976 		} else if ((flags & FW_MIGRATION) &&
977 			   pmd_is_migration_entry(pmd)) {
978 			const softleaf_t entry = softleaf_from_pmd(pmd);
979 
980 			page = softleaf_to_page(entry);
981 			expose_page = false;
982 			goto found;
983 		}
984 		spin_unlock(ptl);
985 		goto not_found;
986 	}
987 
988 pte_table:
989 	VM_WARN_ON_ONCE(!pmd_present(pmd) || pmd_leaf(pmd));
990 	ptep = pte_offset_map_lock(vma->vm_mm, pmdp, addr, &ptl);
991 	if (!ptep)
992 		goto not_found;
993 	pte = ptep_get(ptep);
994 
995 	entry_size = PAGE_SIZE;
996 	fw->level = FW_LEVEL_PTE;
997 	fw->ptep = ptep;
998 	fw->pte = pte;
999 
1000 	if (pte_present(pte)) {
1001 		page = vm_normal_page(vma, addr, pte);
1002 		if (page)
1003 			goto found;
1004 		if ((flags & FW_ZEROPAGE) &&
1005 		    is_zero_pfn(pte_pfn(pte))) {
1006 			page = pfn_to_page(pte_pfn(pte));
1007 			expose_page = false;
1008 			goto found;
1009 		}
1010 	} else if (!pte_none(pte)) {
1011 		const softleaf_t entry = softleaf_from_pte(pte);
1012 
1013 		if ((flags & FW_MIGRATION) && softleaf_is_migration(entry)) {
1014 			page = softleaf_to_page(entry);
1015 			expose_page = false;
1016 			goto found;
1017 		}
1018 	}
1019 	pte_unmap_unlock(ptep, ptl);
1020 not_found:
1021 	vma_pgtable_walk_end(vma);
1022 	return NULL;
1023 found:
1024 	if (expose_page)
1025 		/* Note: Offset from the mapped page, not the folio start. */
1026 		fw->page = page + ((addr & (entry_size - 1)) >> PAGE_SHIFT);
1027 	else
1028 		fw->page = NULL;
1029 	fw->ptl = ptl;
1030 	return page_folio(page);
1031 }
1032