blob: 64437105fe0d928d7447f121d24c57fe8363f574 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Christoph Hellwiga5201102019-08-28 16:19:53 +02002#include <linux/pagewalk.h>
Matt Mackalle6473092008-02-04 22:29:01 -08003#include <linux/highmem.h>
4#include <linux/sched.h>
Naoya Horiguchid33b9f42009-12-14 17:59:59 -08005#include <linux/hugetlb.h>
Matt Mackalle6473092008-02-04 22:29:01 -08006
Steven Priceb7a16c72020-02-03 17:36:03 -08007/*
8 * We want to know the real level where a entry is located ignoring any
9 * folding of levels which may be happening. For example if p4d is folded then
10 * a missing entry found at level 1 (p4d) is actually at level 0 (pgd).
11 */
12static int real_depth(int depth)
13{
14 if (depth == 3 && PTRS_PER_PMD == 1)
15 depth = 2;
16 if (depth == 2 && PTRS_PER_PUD == 1)
17 depth = 1;
18 if (depth == 1 && PTRS_PER_P4D == 1)
19 depth = 0;
20 return depth;
21}
22
Steven Pricefbf56342020-02-03 17:35:54 -080023static int walk_pte_range_inner(pte_t *pte, unsigned long addr,
24 unsigned long end, struct mm_walk *walk)
Matt Mackalle6473092008-02-04 22:29:01 -080025{
Christoph Hellwig7b86ac32019-08-28 16:19:54 +020026 const struct mm_walk_ops *ops = walk->ops;
Steven Pricefbf56342020-02-03 17:35:54 -080027 int err = 0;
Matt Mackalle6473092008-02-04 22:29:01 -080028
Johannes Weiner556637c2008-04-28 02:11:47 -070029 for (;;) {
Christoph Hellwig7b86ac32019-08-28 16:19:54 +020030 err = ops->pte_entry(pte, addr, addr + PAGE_SIZE, walk);
Matt Mackalle6473092008-02-04 22:29:01 -080031 if (err)
32 break;
Steven Pricec02a9872020-02-03 17:35:58 -080033 if (addr >= end - PAGE_SIZE)
Johannes Weiner556637c2008-04-28 02:11:47 -070034 break;
Steven Pricec02a9872020-02-03 17:35:58 -080035 addr += PAGE_SIZE;
Johannes Weiner556637c2008-04-28 02:11:47 -070036 pte++;
37 }
Steven Pricefbf56342020-02-03 17:35:54 -080038 return err;
39}
Matt Mackalle6473092008-02-04 22:29:01 -080040
Steven Pricefbf56342020-02-03 17:35:54 -080041static int walk_pte_range(pmd_t *pmd, unsigned long addr, unsigned long end,
42 struct mm_walk *walk)
43{
44 pte_t *pte;
45 int err = 0;
46 spinlock_t *ptl;
47
48 if (walk->no_vma) {
Hugh Dickinsbe872f82023-06-08 18:18:49 -070049 /*
50 * pte_offset_map() might apply user-specific validation.
51 */
52 if (walk->mm == &init_mm)
53 pte = pte_offset_kernel(pmd, addr);
54 else
55 pte = pte_offset_map(pmd, addr);
56 if (pte) {
57 err = walk_pte_range_inner(pte, addr, end, walk);
58 if (walk->mm != &init_mm)
59 pte_unmap(pte);
60 }
Steven Pricefbf56342020-02-03 17:35:54 -080061 } else {
62 pte = pte_offset_map_lock(walk->mm, pmd, addr, &ptl);
Hugh Dickinsbe872f82023-06-08 18:18:49 -070063 if (pte) {
64 err = walk_pte_range_inner(pte, addr, end, walk);
65 pte_unmap_unlock(pte, ptl);
66 }
Steven Pricefbf56342020-02-03 17:35:54 -080067 }
Hugh Dickinsbe872f82023-06-08 18:18:49 -070068 if (!pte)
69 walk->action = ACTION_AGAIN;
Matt Mackalle6473092008-02-04 22:29:01 -080070 return err;
71}
72
Christophe Leroye17eae22021-06-28 19:36:43 -070073#ifdef CONFIG_ARCH_HAS_HUGEPD
74static int walk_hugepd_range(hugepd_t *phpd, unsigned long addr,
75 unsigned long end, struct mm_walk *walk, int pdshift)
76{
77 int err = 0;
78 const struct mm_walk_ops *ops = walk->ops;
79 int shift = hugepd_shift(*phpd);
80 int page_size = 1 << shift;
81
82 if (!ops->pte_entry)
83 return 0;
84
85 if (addr & (page_size - 1))
86 return 0;
87
88 for (;;) {
89 pte_t *pte;
90
91 spin_lock(&walk->mm->page_table_lock);
92 pte = hugepte_offset(*phpd, addr, pdshift);
93 err = ops->pte_entry(pte, addr, addr + page_size, walk);
94 spin_unlock(&walk->mm->page_table_lock);
95
96 if (err)
97 break;
98 if (addr >= end - page_size)
99 break;
100 addr += page_size;
101 }
102 return err;
103}
104#else
105static int walk_hugepd_range(hugepd_t *phpd, unsigned long addr,
106 unsigned long end, struct mm_walk *walk, int pdshift)
107{
108 return 0;
109}
110#endif
111
Matt Mackalle6473092008-02-04 22:29:01 -0800112static int walk_pmd_range(pud_t *pud, unsigned long addr, unsigned long end,
Dave Hansen21650092008-06-12 15:21:47 -0700113 struct mm_walk *walk)
Matt Mackalle6473092008-02-04 22:29:01 -0800114{
115 pmd_t *pmd;
116 unsigned long next;
Christoph Hellwig7b86ac32019-08-28 16:19:54 +0200117 const struct mm_walk_ops *ops = walk->ops;
Matt Mackalle6473092008-02-04 22:29:01 -0800118 int err = 0;
Steven Priceb7a16c72020-02-03 17:36:03 -0800119 int depth = real_depth(3);
Matt Mackalle6473092008-02-04 22:29:01 -0800120
121 pmd = pmd_offset(pud, addr);
122 do {
Dave Hansen03319322011-03-22 16:32:56 -0700123again:
Matt Mackalle6473092008-02-04 22:29:01 -0800124 next = pmd_addr_end(addr, end);
Steven Price8782fb62022-09-02 12:26:12 +0100125 if (pmd_none(*pmd)) {
Christoph Hellwig7b86ac32019-08-28 16:19:54 +0200126 if (ops->pte_hole)
Steven Priceb7a16c72020-02-03 17:36:03 -0800127 err = ops->pte_hole(addr, next, depth, walk);
Matt Mackalle6473092008-02-04 22:29:01 -0800128 if (err)
129 break;
130 continue;
131 }
Steven Price3afc4232020-02-03 17:35:45 -0800132
133 walk->action = ACTION_SUBTREE;
134
Dave Hansen03319322011-03-22 16:32:56 -0700135 /*
136 * This implies that each ->pmd_entry() handler
137 * needs to know about pmd_trans_huge() pmds
138 */
Christoph Hellwig7b86ac32019-08-28 16:19:54 +0200139 if (ops->pmd_entry)
140 err = ops->pmd_entry(pmd, addr, next, walk);
Dave Hansen03319322011-03-22 16:32:56 -0700141 if (err)
142 break;
143
Steven Price3afc4232020-02-03 17:35:45 -0800144 if (walk->action == ACTION_AGAIN)
145 goto again;
146
Dave Hansen03319322011-03-22 16:32:56 -0700147 /*
148 * Check this here so we only break down trans_huge
149 * pages when we _need_ to
150 */
Steven Price488ae6a2020-02-03 17:35:50 -0800151 if ((!walk->vma && (pmd_leaf(*pmd) || !pmd_present(*pmd))) ||
152 walk->action == ACTION_CONTINUE ||
Steven Price3afc4232020-02-03 17:35:45 -0800153 !(ops->pte_entry))
Dave Hansen03319322011-03-22 16:32:56 -0700154 continue;
155
Hugh Dickinsbe872f82023-06-08 18:18:49 -0700156 if (walk->vma)
Steven Price488ae6a2020-02-03 17:35:50 -0800157 split_huge_pmd(walk->vma, pmd, addr);
Steven Price3afc4232020-02-03 17:35:45 -0800158
Christophe Leroye17eae22021-06-28 19:36:43 -0700159 if (is_hugepd(__hugepd(pmd_val(*pmd))))
160 err = walk_hugepd_range((hugepd_t *)pmd, addr, next, walk, PMD_SHIFT);
161 else
162 err = walk_pte_range(pmd, addr, next, walk);
Matt Mackalle6473092008-02-04 22:29:01 -0800163 if (err)
164 break;
Hugh Dickinsbe872f82023-06-08 18:18:49 -0700165
166 if (walk->action == ACTION_AGAIN)
167 goto again;
168
Matt Mackalle6473092008-02-04 22:29:01 -0800169 } while (pmd++, addr = next, addr != end);
170
171 return err;
172}
173
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +0300174static int walk_pud_range(p4d_t *p4d, unsigned long addr, unsigned long end,
Dave Hansen21650092008-06-12 15:21:47 -0700175 struct mm_walk *walk)
Matt Mackalle6473092008-02-04 22:29:01 -0800176{
177 pud_t *pud;
178 unsigned long next;
Christoph Hellwig7b86ac32019-08-28 16:19:54 +0200179 const struct mm_walk_ops *ops = walk->ops;
Matt Mackalle6473092008-02-04 22:29:01 -0800180 int err = 0;
Steven Priceb7a16c72020-02-03 17:36:03 -0800181 int depth = real_depth(2);
Matt Mackalle6473092008-02-04 22:29:01 -0800182
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +0300183 pud = pud_offset(p4d, addr);
Matt Mackalle6473092008-02-04 22:29:01 -0800184 do {
Matthew Wilcoxa00cc7d2017-02-24 14:57:02 -0800185 again:
Matt Mackalle6473092008-02-04 22:29:01 -0800186 next = pud_addr_end(addr, end);
Steven Price8782fb62022-09-02 12:26:12 +0100187 if (pud_none(*pud)) {
Christoph Hellwig7b86ac32019-08-28 16:19:54 +0200188 if (ops->pte_hole)
Steven Priceb7a16c72020-02-03 17:36:03 -0800189 err = ops->pte_hole(addr, next, depth, walk);
Matt Mackalle6473092008-02-04 22:29:01 -0800190 if (err)
191 break;
192 continue;
193 }
Matthew Wilcoxa00cc7d2017-02-24 14:57:02 -0800194
Steven Price3afc4232020-02-03 17:35:45 -0800195 walk->action = ACTION_SUBTREE;
Matthew Wilcoxa00cc7d2017-02-24 14:57:02 -0800196
Steven Price3afc4232020-02-03 17:35:45 -0800197 if (ops->pud_entry)
198 err = ops->pud_entry(pud, addr, next, walk);
199 if (err)
200 break;
201
202 if (walk->action == ACTION_AGAIN)
203 goto again;
204
Steven Price488ae6a2020-02-03 17:35:50 -0800205 if ((!walk->vma && (pud_leaf(*pud) || !pud_present(*pud))) ||
206 walk->action == ACTION_CONTINUE ||
Steven Price3afc4232020-02-03 17:35:45 -0800207 !(ops->pmd_entry || ops->pte_entry))
208 continue;
Matthew Wilcoxa00cc7d2017-02-24 14:57:02 -0800209
Steven Price488ae6a2020-02-03 17:35:50 -0800210 if (walk->vma)
211 split_huge_pud(walk->vma, pud, addr);
Matthew Wilcoxa00cc7d2017-02-24 14:57:02 -0800212 if (pud_none(*pud))
213 goto again;
214
Christophe Leroye17eae22021-06-28 19:36:43 -0700215 if (is_hugepd(__hugepd(pud_val(*pud))))
216 err = walk_hugepd_range((hugepd_t *)pud, addr, next, walk, PUD_SHIFT);
217 else
218 err = walk_pmd_range(pud, addr, next, walk);
Matt Mackalle6473092008-02-04 22:29:01 -0800219 if (err)
220 break;
221 } while (pud++, addr = next, addr != end);
222
223 return err;
224}
225
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +0300226static int walk_p4d_range(pgd_t *pgd, unsigned long addr, unsigned long end,
227 struct mm_walk *walk)
228{
229 p4d_t *p4d;
230 unsigned long next;
Christoph Hellwig7b86ac32019-08-28 16:19:54 +0200231 const struct mm_walk_ops *ops = walk->ops;
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +0300232 int err = 0;
Steven Priceb7a16c72020-02-03 17:36:03 -0800233 int depth = real_depth(1);
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +0300234
235 p4d = p4d_offset(pgd, addr);
236 do {
237 next = p4d_addr_end(addr, end);
238 if (p4d_none_or_clear_bad(p4d)) {
Christoph Hellwig7b86ac32019-08-28 16:19:54 +0200239 if (ops->pte_hole)
Steven Priceb7a16c72020-02-03 17:36:03 -0800240 err = ops->pte_hole(addr, next, depth, walk);
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +0300241 if (err)
242 break;
243 continue;
244 }
Steven Price3afc4232020-02-03 17:35:45 -0800245 if (ops->p4d_entry) {
246 err = ops->p4d_entry(p4d, addr, next, walk);
247 if (err)
248 break;
249 }
Christophe Leroye17eae22021-06-28 19:36:43 -0700250 if (is_hugepd(__hugepd(p4d_val(*p4d))))
251 err = walk_hugepd_range((hugepd_t *)p4d, addr, next, walk, P4D_SHIFT);
252 else if (ops->pud_entry || ops->pmd_entry || ops->pte_entry)
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +0300253 err = walk_pud_range(p4d, addr, next, walk);
254 if (err)
255 break;
256 } while (p4d++, addr = next, addr != end);
257
258 return err;
259}
260
Naoya Horiguchifafaa422015-02-11 15:27:37 -0800261static int walk_pgd_range(unsigned long addr, unsigned long end,
262 struct mm_walk *walk)
263{
264 pgd_t *pgd;
265 unsigned long next;
Christoph Hellwig7b86ac32019-08-28 16:19:54 +0200266 const struct mm_walk_ops *ops = walk->ops;
Naoya Horiguchifafaa422015-02-11 15:27:37 -0800267 int err = 0;
268
Steven Pricee47690d2020-02-03 17:36:42 -0800269 if (walk->pgd)
270 pgd = walk->pgd + pgd_index(addr);
271 else
272 pgd = pgd_offset(walk->mm, addr);
Naoya Horiguchifafaa422015-02-11 15:27:37 -0800273 do {
274 next = pgd_addr_end(addr, end);
275 if (pgd_none_or_clear_bad(pgd)) {
Christoph Hellwig7b86ac32019-08-28 16:19:54 +0200276 if (ops->pte_hole)
Steven Priceb7a16c72020-02-03 17:36:03 -0800277 err = ops->pte_hole(addr, next, 0, walk);
Naoya Horiguchifafaa422015-02-11 15:27:37 -0800278 if (err)
279 break;
280 continue;
281 }
Steven Price3afc4232020-02-03 17:35:45 -0800282 if (ops->pgd_entry) {
283 err = ops->pgd_entry(pgd, addr, next, walk);
284 if (err)
285 break;
286 }
Christophe Leroye17eae22021-06-28 19:36:43 -0700287 if (is_hugepd(__hugepd(pgd_val(*pgd))))
288 err = walk_hugepd_range((hugepd_t *)pgd, addr, next, walk, PGDIR_SHIFT);
289 else if (ops->p4d_entry || ops->pud_entry || ops->pmd_entry || ops->pte_entry)
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +0300290 err = walk_p4d_range(pgd, addr, next, walk);
Naoya Horiguchifafaa422015-02-11 15:27:37 -0800291 if (err)
292 break;
293 } while (pgd++, addr = next, addr != end);
294
295 return err;
296}
297
Naoya Horiguchi116354d2010-04-06 14:35:04 -0700298#ifdef CONFIG_HUGETLB_PAGE
299static unsigned long hugetlb_entry_end(struct hstate *h, unsigned long addr,
300 unsigned long end)
301{
302 unsigned long boundary = (addr & huge_page_mask(h)) + huge_page_size(h);
303 return boundary < end ? boundary : end;
304}
305
Naoya Horiguchifafaa422015-02-11 15:27:37 -0800306static int walk_hugetlb_range(unsigned long addr, unsigned long end,
Naoya Horiguchi116354d2010-04-06 14:35:04 -0700307 struct mm_walk *walk)
308{
Naoya Horiguchifafaa422015-02-11 15:27:37 -0800309 struct vm_area_struct *vma = walk->vma;
Naoya Horiguchi116354d2010-04-06 14:35:04 -0700310 struct hstate *h = hstate_vma(vma);
311 unsigned long next;
312 unsigned long hmask = huge_page_mask(h);
Punit Agrawal7868a202017-07-06 15:39:42 -0700313 unsigned long sz = huge_page_size(h);
Naoya Horiguchi116354d2010-04-06 14:35:04 -0700314 pte_t *pte;
Christoph Hellwig7b86ac32019-08-28 16:19:54 +0200315 const struct mm_walk_ops *ops = walk->ops;
Naoya Horiguchi116354d2010-04-06 14:35:04 -0700316 int err = 0;
317
Peter Xudd361e52022-12-16 10:52:26 -0500318 hugetlb_vma_lock_read(vma);
Naoya Horiguchi116354d2010-04-06 14:35:04 -0700319 do {
320 next = hugetlb_entry_end(h, addr, end);
Peter Xu9c67a202022-12-16 10:52:29 -0500321 pte = hugetlb_walk(vma, addr & hmask, sz);
Jann Horn373c4552017-11-14 01:03:44 +0100322 if (pte)
Christoph Hellwig7b86ac32019-08-28 16:19:54 +0200323 err = ops->hugetlb_entry(pte, hmask, addr, next, walk);
324 else if (ops->pte_hole)
Steven Priceb7a16c72020-02-03 17:36:03 -0800325 err = ops->pte_hole(addr, next, -1, walk);
Naoya Horiguchi116354d2010-04-06 14:35:04 -0700326 if (err)
Naoya Horiguchifafaa422015-02-11 15:27:37 -0800327 break;
Naoya Horiguchi116354d2010-04-06 14:35:04 -0700328 } while (addr = next, addr != end);
Peter Xudd361e52022-12-16 10:52:26 -0500329 hugetlb_vma_unlock_read(vma);
Naoya Horiguchi116354d2010-04-06 14:35:04 -0700330
Naoya Horiguchifafaa422015-02-11 15:27:37 -0800331 return err;
Naoya Horiguchi116354d2010-04-06 14:35:04 -0700332}
KOSAKI Motohiro6c6d5282011-07-25 17:12:09 -0700333
KOSAKI Motohiro6c6d5282011-07-25 17:12:09 -0700334#else /* CONFIG_HUGETLB_PAGE */
Naoya Horiguchifafaa422015-02-11 15:27:37 -0800335static int walk_hugetlb_range(unsigned long addr, unsigned long end,
KOSAKI Motohiro6c6d5282011-07-25 17:12:09 -0700336 struct mm_walk *walk)
337{
338 return 0;
339}
340
341#endif /* CONFIG_HUGETLB_PAGE */
342
Naoya Horiguchifafaa422015-02-11 15:27:37 -0800343/*
344 * Decide whether we really walk over the current vma on [@start, @end)
345 * or skip it via the returned value. Return 0 if we do walk over the
346 * current vma, and return 1 if we skip the vma. Negative values means
347 * error, where we abort the current walk.
Naoya Horiguchifafaa422015-02-11 15:27:37 -0800348 */
349static int walk_page_test(unsigned long start, unsigned long end,
350 struct mm_walk *walk)
351{
352 struct vm_area_struct *vma = walk->vma;
Christoph Hellwig7b86ac32019-08-28 16:19:54 +0200353 const struct mm_walk_ops *ops = walk->ops;
KOSAKI Motohiro6c6d5282011-07-25 17:12:09 -0700354
Christoph Hellwig7b86ac32019-08-28 16:19:54 +0200355 if (ops->test_walk)
356 return ops->test_walk(start, end, walk);
Naoya Horiguchifafaa422015-02-11 15:27:37 -0800357
358 /*
Naoya Horiguchi48684a62015-02-11 15:28:06 -0800359 * vma(VM_PFNMAP) doesn't have any valid struct pages behind VM_PFNMAP
360 * range, so we don't walk over it as we do for normal vmas. However,
361 * Some callers are interested in handling hole range and they don't
362 * want to just ignore any single address range. Such users certainly
363 * define their ->pte_hole() callbacks, so let's delegate them to handle
364 * vma(VM_PFNMAP).
Naoya Horiguchifafaa422015-02-11 15:27:37 -0800365 */
Naoya Horiguchi48684a62015-02-11 15:28:06 -0800366 if (vma->vm_flags & VM_PFNMAP) {
367 int err = 1;
Christoph Hellwig7b86ac32019-08-28 16:19:54 +0200368 if (ops->pte_hole)
Steven Priceb7a16c72020-02-03 17:36:03 -0800369 err = ops->pte_hole(start, end, -1, walk);
Naoya Horiguchi48684a62015-02-11 15:28:06 -0800370 return err ? err : 1;
371 }
Naoya Horiguchifafaa422015-02-11 15:27:37 -0800372 return 0;
373}
374
375static int __walk_page_range(unsigned long start, unsigned long end,
376 struct mm_walk *walk)
377{
378 int err = 0;
379 struct vm_area_struct *vma = walk->vma;
Thomas Hellstromecaad8a2019-10-01 11:17:34 +0200380 const struct mm_walk_ops *ops = walk->ops;
381
Steven Price8782fb62022-09-02 12:26:12 +0100382 if (ops->pre_vma) {
Thomas Hellstromecaad8a2019-10-01 11:17:34 +0200383 err = ops->pre_vma(start, end, walk);
384 if (err)
385 return err;
386 }
Naoya Horiguchifafaa422015-02-11 15:27:37 -0800387
Steven Price8782fb62022-09-02 12:26:12 +0100388 if (is_vm_hugetlb_page(vma)) {
Thomas Hellstromecaad8a2019-10-01 11:17:34 +0200389 if (ops->hugetlb_entry)
Naoya Horiguchifafaa422015-02-11 15:27:37 -0800390 err = walk_hugetlb_range(start, end, walk);
391 } else
392 err = walk_pgd_range(start, end, walk);
393
Steven Price8782fb62022-09-02 12:26:12 +0100394 if (ops->post_vma)
Thomas Hellstromecaad8a2019-10-01 11:17:34 +0200395 ops->post_vma(walk);
396
Naoya Horiguchifafaa422015-02-11 15:27:37 -0800397 return err;
398}
Naoya Horiguchi116354d2010-04-06 14:35:04 -0700399
Matt Mackalle6473092008-02-04 22:29:01 -0800400/**
Naoya Horiguchifafaa422015-02-11 15:27:37 -0800401 * walk_page_range - walk page table with caller specific callbacks
Christoph Hellwig7b86ac32019-08-28 16:19:54 +0200402 * @mm: mm_struct representing the target process of page table walk
403 * @start: start address of the virtual address range
404 * @end: end address of the virtual address range
405 * @ops: operation to call during the walk
406 * @private: private data for callbacks' usage
Matt Mackalle6473092008-02-04 22:29:01 -0800407 *
Christoph Hellwig7b86ac32019-08-28 16:19:54 +0200408 * Recursively walk the page table tree of the process represented by @mm
Naoya Horiguchifafaa422015-02-11 15:27:37 -0800409 * within the virtual address range [@start, @end). During walking, we can do
410 * some caller-specific works for each entry, by setting up pmd_entry(),
411 * pte_entry(), and/or hugetlb_entry(). If you don't set up for some of these
412 * callbacks, the associated entries/pages are just ignored.
413 * The return values of these callbacks are commonly defined like below:
Mike Rapoporta5d09be2018-02-06 15:42:19 -0800414 *
Naoya Horiguchifafaa422015-02-11 15:27:37 -0800415 * - 0 : succeeded to handle the current entry, and if you don't reach the
416 * end address yet, continue to walk.
417 * - >0 : succeeded to handle the current entry, and return to the caller
418 * with caller specific value.
419 * - <0 : failed to handle the current entry, and return to the caller
420 * with error code.
Matt Mackalle6473092008-02-04 22:29:01 -0800421 *
Naoya Horiguchifafaa422015-02-11 15:27:37 -0800422 * Before starting to walk page table, some callers want to check whether
423 * they really want to walk over the current vma, typically by checking
Christoph Hellwig7b86ac32019-08-28 16:19:54 +0200424 * its vm_flags. walk_page_test() and @ops->test_walk() are used for this
Naoya Horiguchifafaa422015-02-11 15:27:37 -0800425 * purpose.
Matt Mackalle6473092008-02-04 22:29:01 -0800426 *
Thomas Hellstromecaad8a2019-10-01 11:17:34 +0200427 * If operations need to be staged before and committed after a vma is walked,
428 * there are two callbacks, pre_vma() and post_vma(). Note that post_vma(),
429 * since it is intended to handle commit-type operations, can't return any
430 * errors.
431 *
Naoya Horiguchifafaa422015-02-11 15:27:37 -0800432 * struct mm_walk keeps current values of some common data like vma and pmd,
433 * which are useful for the access from callbacks. If you want to pass some
Christoph Hellwig7b86ac32019-08-28 16:19:54 +0200434 * caller-specific data to callbacks, @private should be helpful.
Matt Mackalle6473092008-02-04 22:29:01 -0800435 *
Naoya Horiguchifafaa422015-02-11 15:27:37 -0800436 * Locking:
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -0700437 * Callers of walk_page_range() and walk_page_vma() should hold @mm->mmap_lock,
Christoph Hellwig7b86ac32019-08-28 16:19:54 +0200438 * because these function traverse vma list and/or access to vma's data.
Matt Mackalle6473092008-02-04 22:29:01 -0800439 */
Christoph Hellwig7b86ac32019-08-28 16:19:54 +0200440int walk_page_range(struct mm_struct *mm, unsigned long start,
441 unsigned long end, const struct mm_walk_ops *ops,
442 void *private)
Matt Mackalle6473092008-02-04 22:29:01 -0800443{
Matt Mackalle6473092008-02-04 22:29:01 -0800444 int err = 0;
Naoya Horiguchifafaa422015-02-11 15:27:37 -0800445 unsigned long next;
446 struct vm_area_struct *vma;
Christoph Hellwig7b86ac32019-08-28 16:19:54 +0200447 struct mm_walk walk = {
448 .ops = ops,
449 .mm = mm,
450 .private = private,
451 };
Matt Mackalle6473092008-02-04 22:29:01 -0800452
Naoya Horiguchifafaa422015-02-11 15:27:37 -0800453 if (start >= end)
454 return -EINVAL;
Matt Mackalle6473092008-02-04 22:29:01 -0800455
Christoph Hellwig7b86ac32019-08-28 16:19:54 +0200456 if (!walk.mm)
Dave Hansen21650092008-06-12 15:21:47 -0700457 return -EINVAL;
458
Michel Lespinasse42fc5412020-06-08 21:33:44 -0700459 mmap_assert_locked(walk.mm);
Cliff Wickmana9ff7852013-05-24 15:55:36 -0700460
Christoph Hellwig7b86ac32019-08-28 16:19:54 +0200461 vma = find_vma(walk.mm, start);
Matt Mackalle6473092008-02-04 22:29:01 -0800462 do {
Naoya Horiguchifafaa422015-02-11 15:27:37 -0800463 if (!vma) { /* after the last vma */
Christoph Hellwig7b86ac32019-08-28 16:19:54 +0200464 walk.vma = NULL;
Naoya Horiguchifafaa422015-02-11 15:27:37 -0800465 next = end;
Steven Price8782fb62022-09-02 12:26:12 +0100466 if (ops->pte_hole)
467 err = ops->pte_hole(start, next, -1, &walk);
Naoya Horiguchifafaa422015-02-11 15:27:37 -0800468 } else if (start < vma->vm_start) { /* outside vma */
Christoph Hellwig7b86ac32019-08-28 16:19:54 +0200469 walk.vma = NULL;
Naoya Horiguchifafaa422015-02-11 15:27:37 -0800470 next = min(end, vma->vm_start);
Steven Price8782fb62022-09-02 12:26:12 +0100471 if (ops->pte_hole)
472 err = ops->pte_hole(start, next, -1, &walk);
Naoya Horiguchifafaa422015-02-11 15:27:37 -0800473 } else { /* inside vma */
Christoph Hellwig7b86ac32019-08-28 16:19:54 +0200474 walk.vma = vma;
Naoya Horiguchifafaa422015-02-11 15:27:37 -0800475 next = min(end, vma->vm_end);
Matthew Wilcox (Oracle)9ec08f32022-09-06 19:49:04 +0000476 vma = find_vma(mm, vma->vm_end);
David Sterba5f0af702010-11-24 12:57:10 -0800477
Christoph Hellwig7b86ac32019-08-28 16:19:54 +0200478 err = walk_page_test(start, next, &walk);
Naoya Horiguchif6837392015-03-25 15:55:14 -0700479 if (err > 0) {
480 /*
481 * positive return values are purely for
482 * controlling the pagewalk, so should never
483 * be passed to the callers.
484 */
485 err = 0;
Cliff Wickmana9ff7852013-05-24 15:55:36 -0700486 continue;
Naoya Horiguchif6837392015-03-25 15:55:14 -0700487 }
Naoya Horiguchifafaa422015-02-11 15:27:37 -0800488 if (err < 0)
Matt Mackalle6473092008-02-04 22:29:01 -0800489 break;
Christoph Hellwig7b86ac32019-08-28 16:19:54 +0200490 err = __walk_page_range(start, next, &walk);
Steven Price8782fb62022-09-02 12:26:12 +0100491 }
Matt Mackalle6473092008-02-04 22:29:01 -0800492 if (err)
493 break;
Naoya Horiguchifafaa422015-02-11 15:27:37 -0800494 } while (start = next, start < end);
Matt Mackalle6473092008-02-04 22:29:01 -0800495 return err;
496}
Naoya Horiguchi900fc5f12015-02-11 15:27:40 -0800497
Rolf Eike Beer8bd38732022-08-22 15:02:36 +0200498/**
499 * walk_page_range_novma - walk a range of pagetables not backed by a vma
500 * @mm: mm_struct representing the target process of page table walk
501 * @start: start address of the virtual address range
502 * @end: end address of the virtual address range
503 * @ops: operation to call during the walk
504 * @pgd: pgd to walk if different from mm->pgd
505 * @private: private data for callbacks' usage
506 *
Steven Pricefbf56342020-02-03 17:35:54 -0800507 * Similar to walk_page_range() but can walk any page tables even if they are
508 * not backed by VMAs. Because 'unusual' entries may be walked this function
509 * will also not lock the PTEs for the pte_entry() callback. This is useful for
510 * walking the kernel pages tables or page tables for firmware.
511 */
Steven Price488ae6a2020-02-03 17:35:50 -0800512int walk_page_range_novma(struct mm_struct *mm, unsigned long start,
513 unsigned long end, const struct mm_walk_ops *ops,
Steven Pricee47690d2020-02-03 17:36:42 -0800514 pgd_t *pgd,
Steven Price488ae6a2020-02-03 17:35:50 -0800515 void *private)
516{
517 struct mm_walk walk = {
518 .ops = ops,
519 .mm = mm,
Steven Pricee47690d2020-02-03 17:36:42 -0800520 .pgd = pgd,
Steven Price488ae6a2020-02-03 17:35:50 -0800521 .private = private,
522 .no_vma = true
523 };
524
525 if (start >= end || !walk.mm)
526 return -EINVAL;
527
Steven Price8782fb62022-09-02 12:26:12 +0100528 mmap_assert_write_locked(walk.mm);
Steven Price488ae6a2020-02-03 17:35:50 -0800529
Steven Price8782fb62022-09-02 12:26:12 +0100530 return walk_pgd_range(start, end, &walk);
Steven Price488ae6a2020-02-03 17:35:50 -0800531}
532
David Hildenbrande07cda52022-10-21 12:11:39 +0200533int walk_page_range_vma(struct vm_area_struct *vma, unsigned long start,
534 unsigned long end, const struct mm_walk_ops *ops,
535 void *private)
536{
537 struct mm_walk walk = {
538 .ops = ops,
539 .mm = vma->vm_mm,
540 .vma = vma,
541 .private = private,
542 };
543
544 if (start >= end || !walk.mm)
545 return -EINVAL;
546 if (start < vma->vm_start || end > vma->vm_end)
547 return -EINVAL;
548
549 mmap_assert_locked(walk.mm);
550 return __walk_page_range(start, end, &walk);
551}
552
Christoph Hellwig7b86ac32019-08-28 16:19:54 +0200553int walk_page_vma(struct vm_area_struct *vma, const struct mm_walk_ops *ops,
554 void *private)
Naoya Horiguchi900fc5f12015-02-11 15:27:40 -0800555{
Christoph Hellwig7b86ac32019-08-28 16:19:54 +0200556 struct mm_walk walk = {
557 .ops = ops,
558 .mm = vma->vm_mm,
559 .vma = vma,
560 .private = private,
561 };
Naoya Horiguchi900fc5f12015-02-11 15:27:40 -0800562
Christoph Hellwig7b86ac32019-08-28 16:19:54 +0200563 if (!walk.mm)
Naoya Horiguchi900fc5f12015-02-11 15:27:40 -0800564 return -EINVAL;
565
Michel Lespinasse42fc5412020-06-08 21:33:44 -0700566 mmap_assert_locked(walk.mm);
Christoph Hellwig7b86ac32019-08-28 16:19:54 +0200567 return __walk_page_range(vma->vm_start, vma->vm_end, &walk);
Naoya Horiguchi900fc5f12015-02-11 15:27:40 -0800568}
Thomas Hellstromecaad8a2019-10-01 11:17:34 +0200569
570/**
571 * walk_page_mapping - walk all memory areas mapped into a struct address_space.
572 * @mapping: Pointer to the struct address_space
573 * @first_index: First page offset in the address_space
574 * @nr: Number of incremental page offsets to cover
575 * @ops: operation to call during the walk
576 * @private: private data for callbacks' usage
577 *
578 * This function walks all memory areas mapped into a struct address_space.
579 * The walk is limited to only the given page-size index range, but if
580 * the index boundaries cross a huge page-table entry, that entry will be
581 * included.
582 *
583 * Also see walk_page_range() for additional information.
584 *
585 * Locking:
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -0700586 * This function can't require that the struct mm_struct::mmap_lock is held,
Thomas Hellstromecaad8a2019-10-01 11:17:34 +0200587 * since @mapping may be mapped by multiple processes. Instead
588 * @mapping->i_mmap_rwsem must be held. This might have implications in the
589 * callbacks, and it's up tho the caller to ensure that the
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -0700590 * struct mm_struct::mmap_lock is not needed.
Thomas Hellstromecaad8a2019-10-01 11:17:34 +0200591 *
592 * Also this means that a caller can't rely on the struct
593 * vm_area_struct::vm_flags to be constant across a call,
594 * except for immutable flags. Callers requiring this shouldn't use
595 * this function.
596 *
597 * Return: 0 on success, negative error code on failure, positive number on
598 * caller defined premature termination.
599 */
600int walk_page_mapping(struct address_space *mapping, pgoff_t first_index,
601 pgoff_t nr, const struct mm_walk_ops *ops,
602 void *private)
603{
604 struct mm_walk walk = {
605 .ops = ops,
606 .private = private,
607 };
608 struct vm_area_struct *vma;
609 pgoff_t vba, vea, cba, cea;
610 unsigned long start_addr, end_addr;
611 int err = 0;
612
613 lockdep_assert_held(&mapping->i_mmap_rwsem);
614 vma_interval_tree_foreach(vma, &mapping->i_mmap, first_index,
615 first_index + nr - 1) {
616 /* Clip to the vma */
617 vba = vma->vm_pgoff;
618 vea = vba + vma_pages(vma);
619 cba = first_index;
620 cba = max(cba, vba);
621 cea = first_index + nr;
622 cea = min(cea, vea);
623
624 start_addr = ((cba - vba) << PAGE_SHIFT) + vma->vm_start;
625 end_addr = ((cea - vba) << PAGE_SHIFT) + vma->vm_start;
626 if (start_addr >= end_addr)
627 continue;
628
629 walk.vma = vma;
630 walk.mm = vma->vm_mm;
631
632 err = walk_page_test(vma->vm_start, vma->vm_end, &walk);
633 if (err > 0) {
634 err = 0;
635 break;
636 } else if (err < 0)
637 break;
638
639 err = __walk_page_range(start_addr, end_addr, &walk);
640 if (err)
641 break;
642 }
643
644 return err;
645}