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