blob: d5773465f6da1f50e324b8153b51951dff6bf6d1 [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
7static int walk_pte_range(pmd_t *pmd, unsigned long addr, unsigned long end,
Dave Hansen21650092008-06-12 15:21:47 -07008 struct mm_walk *walk)
Matt Mackalle6473092008-02-04 22:29:01 -08009{
10 pte_t *pte;
11 int err = 0;
Christoph Hellwig7b86ac32019-08-28 16:19:54 +020012 const struct mm_walk_ops *ops = walk->ops;
Thomas Hellstromace88f12019-10-04 11:04:43 +020013 spinlock_t *ptl;
Matt Mackalle6473092008-02-04 22:29:01 -080014
Thomas Hellstromace88f12019-10-04 11:04:43 +020015 pte = pte_offset_map_lock(walk->mm, pmd, addr, &ptl);
Johannes Weiner556637c2008-04-28 02:11:47 -070016 for (;;) {
Christoph Hellwig7b86ac32019-08-28 16:19:54 +020017 err = ops->pte_entry(pte, addr, addr + PAGE_SIZE, walk);
Matt Mackalle6473092008-02-04 22:29:01 -080018 if (err)
19 break;
Johannes Weiner556637c2008-04-28 02:11:47 -070020 addr += PAGE_SIZE;
21 if (addr == end)
22 break;
23 pte++;
24 }
Matt Mackalle6473092008-02-04 22:29:01 -080025
Thomas Hellstromace88f12019-10-04 11:04:43 +020026 pte_unmap_unlock(pte, ptl);
Matt Mackalle6473092008-02-04 22:29:01 -080027 return err;
28}
29
30static int walk_pmd_range(pud_t *pud, unsigned long addr, unsigned long end,
Dave Hansen21650092008-06-12 15:21:47 -070031 struct mm_walk *walk)
Matt Mackalle6473092008-02-04 22:29:01 -080032{
33 pmd_t *pmd;
34 unsigned long next;
Christoph Hellwig7b86ac32019-08-28 16:19:54 +020035 const struct mm_walk_ops *ops = walk->ops;
Matt Mackalle6473092008-02-04 22:29:01 -080036 int err = 0;
37
38 pmd = pmd_offset(pud, addr);
39 do {
Dave Hansen03319322011-03-22 16:32:56 -070040again:
Matt Mackalle6473092008-02-04 22:29:01 -080041 next = pmd_addr_end(addr, end);
Steven Price488ae6a2020-02-03 17:35:50 -080042 if (pmd_none(*pmd) || (!walk->vma && !walk->no_vma)) {
Christoph Hellwig7b86ac32019-08-28 16:19:54 +020043 if (ops->pte_hole)
44 err = ops->pte_hole(addr, next, walk);
Matt Mackalle6473092008-02-04 22:29:01 -080045 if (err)
46 break;
47 continue;
48 }
Steven Price3afc4232020-02-03 17:35:45 -080049
50 walk->action = ACTION_SUBTREE;
51
Dave Hansen03319322011-03-22 16:32:56 -070052 /*
53 * This implies that each ->pmd_entry() handler
54 * needs to know about pmd_trans_huge() pmds
55 */
Christoph Hellwig7b86ac32019-08-28 16:19:54 +020056 if (ops->pmd_entry)
57 err = ops->pmd_entry(pmd, addr, next, walk);
Dave Hansen03319322011-03-22 16:32:56 -070058 if (err)
59 break;
60
Steven Price3afc4232020-02-03 17:35:45 -080061 if (walk->action == ACTION_AGAIN)
62 goto again;
63
Dave Hansen03319322011-03-22 16:32:56 -070064 /*
65 * Check this here so we only break down trans_huge
66 * pages when we _need_ to
67 */
Steven Price488ae6a2020-02-03 17:35:50 -080068 if ((!walk->vma && (pmd_leaf(*pmd) || !pmd_present(*pmd))) ||
69 walk->action == ACTION_CONTINUE ||
Steven Price3afc4232020-02-03 17:35:45 -080070 !(ops->pte_entry))
Dave Hansen03319322011-03-22 16:32:56 -070071 continue;
72
Steven Price488ae6a2020-02-03 17:35:50 -080073 if (walk->vma) {
74 split_huge_pmd(walk->vma, pmd, addr);
75 if (pmd_trans_unstable(pmd))
76 goto again;
77 }
Steven Price3afc4232020-02-03 17:35:45 -080078
Dave Hansen03319322011-03-22 16:32:56 -070079 err = walk_pte_range(pmd, addr, next, walk);
Matt Mackalle6473092008-02-04 22:29:01 -080080 if (err)
81 break;
82 } while (pmd++, addr = next, addr != end);
83
84 return err;
85}
86
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +030087static int walk_pud_range(p4d_t *p4d, unsigned long addr, unsigned long end,
Dave Hansen21650092008-06-12 15:21:47 -070088 struct mm_walk *walk)
Matt Mackalle6473092008-02-04 22:29:01 -080089{
90 pud_t *pud;
91 unsigned long next;
Christoph Hellwig7b86ac32019-08-28 16:19:54 +020092 const struct mm_walk_ops *ops = walk->ops;
Matt Mackalle6473092008-02-04 22:29:01 -080093 int err = 0;
94
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +030095 pud = pud_offset(p4d, addr);
Matt Mackalle6473092008-02-04 22:29:01 -080096 do {
Matthew Wilcoxa00cc7d2017-02-24 14:57:02 -080097 again:
Matt Mackalle6473092008-02-04 22:29:01 -080098 next = pud_addr_end(addr, end);
Steven Price488ae6a2020-02-03 17:35:50 -080099 if (pud_none(*pud) || (!walk->vma && !walk->no_vma)) {
Christoph Hellwig7b86ac32019-08-28 16:19:54 +0200100 if (ops->pte_hole)
101 err = ops->pte_hole(addr, next, walk);
Matt Mackalle6473092008-02-04 22:29:01 -0800102 if (err)
103 break;
104 continue;
105 }
Matthew Wilcoxa00cc7d2017-02-24 14:57:02 -0800106
Steven Price3afc4232020-02-03 17:35:45 -0800107 walk->action = ACTION_SUBTREE;
Matthew Wilcoxa00cc7d2017-02-24 14:57:02 -0800108
Steven Price3afc4232020-02-03 17:35:45 -0800109 if (ops->pud_entry)
110 err = ops->pud_entry(pud, addr, next, walk);
111 if (err)
112 break;
113
114 if (walk->action == ACTION_AGAIN)
115 goto again;
116
Steven Price488ae6a2020-02-03 17:35:50 -0800117 if ((!walk->vma && (pud_leaf(*pud) || !pud_present(*pud))) ||
118 walk->action == ACTION_CONTINUE ||
Steven Price3afc4232020-02-03 17:35:45 -0800119 !(ops->pmd_entry || ops->pte_entry))
120 continue;
Matthew Wilcoxa00cc7d2017-02-24 14:57:02 -0800121
Steven Price488ae6a2020-02-03 17:35:50 -0800122 if (walk->vma)
123 split_huge_pud(walk->vma, pud, addr);
Matthew Wilcoxa00cc7d2017-02-24 14:57:02 -0800124 if (pud_none(*pud))
125 goto again;
126
Steven Price3afc4232020-02-03 17:35:45 -0800127 err = walk_pmd_range(pud, addr, next, walk);
Matt Mackalle6473092008-02-04 22:29:01 -0800128 if (err)
129 break;
130 } while (pud++, addr = next, addr != end);
131
132 return err;
133}
134
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +0300135static int walk_p4d_range(pgd_t *pgd, unsigned long addr, unsigned long end,
136 struct mm_walk *walk)
137{
138 p4d_t *p4d;
139 unsigned long next;
Christoph Hellwig7b86ac32019-08-28 16:19:54 +0200140 const struct mm_walk_ops *ops = walk->ops;
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +0300141 int err = 0;
142
143 p4d = p4d_offset(pgd, addr);
144 do {
145 next = p4d_addr_end(addr, end);
146 if (p4d_none_or_clear_bad(p4d)) {
Christoph Hellwig7b86ac32019-08-28 16:19:54 +0200147 if (ops->pte_hole)
148 err = ops->pte_hole(addr, next, walk);
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +0300149 if (err)
150 break;
151 continue;
152 }
Steven Price3afc4232020-02-03 17:35:45 -0800153 if (ops->p4d_entry) {
154 err = ops->p4d_entry(p4d, addr, next, walk);
155 if (err)
156 break;
157 }
158 if (ops->pud_entry || ops->pmd_entry || ops->pte_entry)
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +0300159 err = walk_pud_range(p4d, addr, next, walk);
160 if (err)
161 break;
162 } while (p4d++, addr = next, addr != end);
163
164 return err;
165}
166
Naoya Horiguchifafaa422015-02-11 15:27:37 -0800167static int walk_pgd_range(unsigned long addr, unsigned long end,
168 struct mm_walk *walk)
169{
170 pgd_t *pgd;
171 unsigned long next;
Christoph Hellwig7b86ac32019-08-28 16:19:54 +0200172 const struct mm_walk_ops *ops = walk->ops;
Naoya Horiguchifafaa422015-02-11 15:27:37 -0800173 int err = 0;
174
175 pgd = pgd_offset(walk->mm, addr);
176 do {
177 next = pgd_addr_end(addr, end);
178 if (pgd_none_or_clear_bad(pgd)) {
Christoph Hellwig7b86ac32019-08-28 16:19:54 +0200179 if (ops->pte_hole)
180 err = ops->pte_hole(addr, next, walk);
Naoya Horiguchifafaa422015-02-11 15:27:37 -0800181 if (err)
182 break;
183 continue;
184 }
Steven Price3afc4232020-02-03 17:35:45 -0800185 if (ops->pgd_entry) {
186 err = ops->pgd_entry(pgd, addr, next, walk);
187 if (err)
188 break;
189 }
190 if (ops->p4d_entry || ops->pud_entry || ops->pmd_entry ||
191 ops->pte_entry)
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +0300192 err = walk_p4d_range(pgd, addr, next, walk);
Naoya Horiguchifafaa422015-02-11 15:27:37 -0800193 if (err)
194 break;
195 } while (pgd++, addr = next, addr != end);
196
197 return err;
198}
199
Naoya Horiguchi116354d2010-04-06 14:35:04 -0700200#ifdef CONFIG_HUGETLB_PAGE
201static unsigned long hugetlb_entry_end(struct hstate *h, unsigned long addr,
202 unsigned long end)
203{
204 unsigned long boundary = (addr & huge_page_mask(h)) + huge_page_size(h);
205 return boundary < end ? boundary : end;
206}
207
Naoya Horiguchifafaa422015-02-11 15:27:37 -0800208static int walk_hugetlb_range(unsigned long addr, unsigned long end,
Naoya Horiguchi116354d2010-04-06 14:35:04 -0700209 struct mm_walk *walk)
210{
Naoya Horiguchifafaa422015-02-11 15:27:37 -0800211 struct vm_area_struct *vma = walk->vma;
Naoya Horiguchi116354d2010-04-06 14:35:04 -0700212 struct hstate *h = hstate_vma(vma);
213 unsigned long next;
214 unsigned long hmask = huge_page_mask(h);
Punit Agrawal7868a202017-07-06 15:39:42 -0700215 unsigned long sz = huge_page_size(h);
Naoya Horiguchi116354d2010-04-06 14:35:04 -0700216 pte_t *pte;
Christoph Hellwig7b86ac32019-08-28 16:19:54 +0200217 const struct mm_walk_ops *ops = walk->ops;
Naoya Horiguchi116354d2010-04-06 14:35:04 -0700218 int err = 0;
219
220 do {
221 next = hugetlb_entry_end(h, addr, end);
Punit Agrawal7868a202017-07-06 15:39:42 -0700222 pte = huge_pte_offset(walk->mm, addr & hmask, sz);
Jann Horn373c4552017-11-14 01:03:44 +0100223
224 if (pte)
Christoph Hellwig7b86ac32019-08-28 16:19:54 +0200225 err = ops->hugetlb_entry(pte, hmask, addr, next, walk);
226 else if (ops->pte_hole)
227 err = ops->pte_hole(addr, next, walk);
Jann Horn373c4552017-11-14 01:03:44 +0100228
Naoya Horiguchi116354d2010-04-06 14:35:04 -0700229 if (err)
Naoya Horiguchifafaa422015-02-11 15:27:37 -0800230 break;
Naoya Horiguchi116354d2010-04-06 14:35:04 -0700231 } while (addr = next, addr != end);
232
Naoya Horiguchifafaa422015-02-11 15:27:37 -0800233 return err;
Naoya Horiguchi116354d2010-04-06 14:35:04 -0700234}
KOSAKI Motohiro6c6d5282011-07-25 17:12:09 -0700235
KOSAKI Motohiro6c6d5282011-07-25 17:12:09 -0700236#else /* CONFIG_HUGETLB_PAGE */
Naoya Horiguchifafaa422015-02-11 15:27:37 -0800237static int walk_hugetlb_range(unsigned long addr, unsigned long end,
KOSAKI Motohiro6c6d5282011-07-25 17:12:09 -0700238 struct mm_walk *walk)
239{
240 return 0;
241}
242
243#endif /* CONFIG_HUGETLB_PAGE */
244
Naoya Horiguchifafaa422015-02-11 15:27:37 -0800245/*
246 * Decide whether we really walk over the current vma on [@start, @end)
247 * or skip it via the returned value. Return 0 if we do walk over the
248 * current vma, and return 1 if we skip the vma. Negative values means
249 * error, where we abort the current walk.
Naoya Horiguchifafaa422015-02-11 15:27:37 -0800250 */
251static int walk_page_test(unsigned long start, unsigned long end,
252 struct mm_walk *walk)
253{
254 struct vm_area_struct *vma = walk->vma;
Christoph Hellwig7b86ac32019-08-28 16:19:54 +0200255 const struct mm_walk_ops *ops = walk->ops;
KOSAKI Motohiro6c6d5282011-07-25 17:12:09 -0700256
Christoph Hellwig7b86ac32019-08-28 16:19:54 +0200257 if (ops->test_walk)
258 return ops->test_walk(start, end, walk);
Naoya Horiguchifafaa422015-02-11 15:27:37 -0800259
260 /*
Naoya Horiguchi48684a62015-02-11 15:28:06 -0800261 * vma(VM_PFNMAP) doesn't have any valid struct pages behind VM_PFNMAP
262 * range, so we don't walk over it as we do for normal vmas. However,
263 * Some callers are interested in handling hole range and they don't
264 * want to just ignore any single address range. Such users certainly
265 * define their ->pte_hole() callbacks, so let's delegate them to handle
266 * vma(VM_PFNMAP).
Naoya Horiguchifafaa422015-02-11 15:27:37 -0800267 */
Naoya Horiguchi48684a62015-02-11 15:28:06 -0800268 if (vma->vm_flags & VM_PFNMAP) {
269 int err = 1;
Christoph Hellwig7b86ac32019-08-28 16:19:54 +0200270 if (ops->pte_hole)
271 err = ops->pte_hole(start, end, walk);
Naoya Horiguchi48684a62015-02-11 15:28:06 -0800272 return err ? err : 1;
273 }
Naoya Horiguchifafaa422015-02-11 15:27:37 -0800274 return 0;
275}
276
277static int __walk_page_range(unsigned long start, unsigned long end,
278 struct mm_walk *walk)
279{
280 int err = 0;
281 struct vm_area_struct *vma = walk->vma;
Thomas Hellstromecaad8a2019-10-01 11:17:34 +0200282 const struct mm_walk_ops *ops = walk->ops;
283
284 if (vma && ops->pre_vma) {
285 err = ops->pre_vma(start, end, walk);
286 if (err)
287 return err;
288 }
Naoya Horiguchifafaa422015-02-11 15:27:37 -0800289
290 if (vma && is_vm_hugetlb_page(vma)) {
Thomas Hellstromecaad8a2019-10-01 11:17:34 +0200291 if (ops->hugetlb_entry)
Naoya Horiguchifafaa422015-02-11 15:27:37 -0800292 err = walk_hugetlb_range(start, end, walk);
293 } else
294 err = walk_pgd_range(start, end, walk);
295
Thomas Hellstromecaad8a2019-10-01 11:17:34 +0200296 if (vma && ops->post_vma)
297 ops->post_vma(walk);
298
Naoya Horiguchifafaa422015-02-11 15:27:37 -0800299 return err;
300}
Naoya Horiguchi116354d2010-04-06 14:35:04 -0700301
Matt Mackalle6473092008-02-04 22:29:01 -0800302/**
Naoya Horiguchifafaa422015-02-11 15:27:37 -0800303 * walk_page_range - walk page table with caller specific callbacks
Christoph Hellwig7b86ac32019-08-28 16:19:54 +0200304 * @mm: mm_struct representing the target process of page table walk
305 * @start: start address of the virtual address range
306 * @end: end address of the virtual address range
307 * @ops: operation to call during the walk
308 * @private: private data for callbacks' usage
Matt Mackalle6473092008-02-04 22:29:01 -0800309 *
Christoph Hellwig7b86ac32019-08-28 16:19:54 +0200310 * Recursively walk the page table tree of the process represented by @mm
Naoya Horiguchifafaa422015-02-11 15:27:37 -0800311 * within the virtual address range [@start, @end). During walking, we can do
312 * some caller-specific works for each entry, by setting up pmd_entry(),
313 * pte_entry(), and/or hugetlb_entry(). If you don't set up for some of these
314 * callbacks, the associated entries/pages are just ignored.
315 * The return values of these callbacks are commonly defined like below:
Mike Rapoporta5d09be2018-02-06 15:42:19 -0800316 *
Naoya Horiguchifafaa422015-02-11 15:27:37 -0800317 * - 0 : succeeded to handle the current entry, and if you don't reach the
318 * end address yet, continue to walk.
319 * - >0 : succeeded to handle the current entry, and return to the caller
320 * with caller specific value.
321 * - <0 : failed to handle the current entry, and return to the caller
322 * with error code.
Matt Mackalle6473092008-02-04 22:29:01 -0800323 *
Naoya Horiguchifafaa422015-02-11 15:27:37 -0800324 * Before starting to walk page table, some callers want to check whether
325 * they really want to walk over the current vma, typically by checking
Christoph Hellwig7b86ac32019-08-28 16:19:54 +0200326 * its vm_flags. walk_page_test() and @ops->test_walk() are used for this
Naoya Horiguchifafaa422015-02-11 15:27:37 -0800327 * purpose.
Matt Mackalle6473092008-02-04 22:29:01 -0800328 *
Thomas Hellstromecaad8a2019-10-01 11:17:34 +0200329 * If operations need to be staged before and committed after a vma is walked,
330 * there are two callbacks, pre_vma() and post_vma(). Note that post_vma(),
331 * since it is intended to handle commit-type operations, can't return any
332 * errors.
333 *
Naoya Horiguchifafaa422015-02-11 15:27:37 -0800334 * struct mm_walk keeps current values of some common data like vma and pmd,
335 * which are useful for the access from callbacks. If you want to pass some
Christoph Hellwig7b86ac32019-08-28 16:19:54 +0200336 * caller-specific data to callbacks, @private should be helpful.
Matt Mackalle6473092008-02-04 22:29:01 -0800337 *
Naoya Horiguchifafaa422015-02-11 15:27:37 -0800338 * Locking:
Christoph Hellwig7b86ac32019-08-28 16:19:54 +0200339 * Callers of walk_page_range() and walk_page_vma() should hold @mm->mmap_sem,
340 * because these function traverse vma list and/or access to vma's data.
Matt Mackalle6473092008-02-04 22:29:01 -0800341 */
Christoph Hellwig7b86ac32019-08-28 16:19:54 +0200342int walk_page_range(struct mm_struct *mm, unsigned long start,
343 unsigned long end, const struct mm_walk_ops *ops,
344 void *private)
Matt Mackalle6473092008-02-04 22:29:01 -0800345{
Matt Mackalle6473092008-02-04 22:29:01 -0800346 int err = 0;
Naoya Horiguchifafaa422015-02-11 15:27:37 -0800347 unsigned long next;
348 struct vm_area_struct *vma;
Christoph Hellwig7b86ac32019-08-28 16:19:54 +0200349 struct mm_walk walk = {
350 .ops = ops,
351 .mm = mm,
352 .private = private,
353 };
Matt Mackalle6473092008-02-04 22:29:01 -0800354
Naoya Horiguchifafaa422015-02-11 15:27:37 -0800355 if (start >= end)
356 return -EINVAL;
Matt Mackalle6473092008-02-04 22:29:01 -0800357
Christoph Hellwig7b86ac32019-08-28 16:19:54 +0200358 if (!walk.mm)
Dave Hansen21650092008-06-12 15:21:47 -0700359 return -EINVAL;
360
Christoph Hellwigb4bc7812019-08-28 16:19:55 +0200361 lockdep_assert_held(&walk.mm->mmap_sem);
Cliff Wickmana9ff7852013-05-24 15:55:36 -0700362
Christoph Hellwig7b86ac32019-08-28 16:19:54 +0200363 vma = find_vma(walk.mm, start);
Matt Mackalle6473092008-02-04 22:29:01 -0800364 do {
Naoya Horiguchifafaa422015-02-11 15:27:37 -0800365 if (!vma) { /* after the last vma */
Christoph Hellwig7b86ac32019-08-28 16:19:54 +0200366 walk.vma = NULL;
Naoya Horiguchifafaa422015-02-11 15:27:37 -0800367 next = end;
368 } else if (start < vma->vm_start) { /* outside vma */
Christoph Hellwig7b86ac32019-08-28 16:19:54 +0200369 walk.vma = NULL;
Naoya Horiguchifafaa422015-02-11 15:27:37 -0800370 next = min(end, vma->vm_start);
371 } else { /* inside vma */
Christoph Hellwig7b86ac32019-08-28 16:19:54 +0200372 walk.vma = vma;
Naoya Horiguchifafaa422015-02-11 15:27:37 -0800373 next = min(end, vma->vm_end);
374 vma = vma->vm_next;
David Sterba5f0af702010-11-24 12:57:10 -0800375
Christoph Hellwig7b86ac32019-08-28 16:19:54 +0200376 err = walk_page_test(start, next, &walk);
Naoya Horiguchif6837392015-03-25 15:55:14 -0700377 if (err > 0) {
378 /*
379 * positive return values are purely for
380 * controlling the pagewalk, so should never
381 * be passed to the callers.
382 */
383 err = 0;
Cliff Wickmana9ff7852013-05-24 15:55:36 -0700384 continue;
Naoya Horiguchif6837392015-03-25 15:55:14 -0700385 }
Naoya Horiguchifafaa422015-02-11 15:27:37 -0800386 if (err < 0)
Matt Mackalle6473092008-02-04 22:29:01 -0800387 break;
Matt Mackalle6473092008-02-04 22:29:01 -0800388 }
Christoph Hellwig7b86ac32019-08-28 16:19:54 +0200389 if (walk.vma || walk.ops->pte_hole)
390 err = __walk_page_range(start, next, &walk);
Matt Mackalle6473092008-02-04 22:29:01 -0800391 if (err)
392 break;
Naoya Horiguchifafaa422015-02-11 15:27:37 -0800393 } while (start = next, start < end);
Matt Mackalle6473092008-02-04 22:29:01 -0800394 return err;
395}
Naoya Horiguchi900fc5f2015-02-11 15:27:40 -0800396
Steven Price488ae6a2020-02-03 17:35:50 -0800397int walk_page_range_novma(struct mm_struct *mm, unsigned long start,
398 unsigned long end, const struct mm_walk_ops *ops,
399 void *private)
400{
401 struct mm_walk walk = {
402 .ops = ops,
403 .mm = mm,
404 .private = private,
405 .no_vma = true
406 };
407
408 if (start >= end || !walk.mm)
409 return -EINVAL;
410
411 lockdep_assert_held(&walk.mm->mmap_sem);
412
413 return __walk_page_range(start, end, &walk);
414}
415
Christoph Hellwig7b86ac32019-08-28 16:19:54 +0200416int walk_page_vma(struct vm_area_struct *vma, const struct mm_walk_ops *ops,
417 void *private)
Naoya Horiguchi900fc5f2015-02-11 15:27:40 -0800418{
Christoph Hellwig7b86ac32019-08-28 16:19:54 +0200419 struct mm_walk walk = {
420 .ops = ops,
421 .mm = vma->vm_mm,
422 .vma = vma,
423 .private = private,
424 };
Naoya Horiguchi900fc5f2015-02-11 15:27:40 -0800425 int err;
426
Christoph Hellwig7b86ac32019-08-28 16:19:54 +0200427 if (!walk.mm)
Naoya Horiguchi900fc5f2015-02-11 15:27:40 -0800428 return -EINVAL;
429
Christoph Hellwigb4bc7812019-08-28 16:19:55 +0200430 lockdep_assert_held(&walk.mm->mmap_sem);
Christoph Hellwig7b86ac32019-08-28 16:19:54 +0200431
432 err = walk_page_test(vma->vm_start, vma->vm_end, &walk);
Naoya Horiguchi900fc5f2015-02-11 15:27:40 -0800433 if (err > 0)
434 return 0;
435 if (err < 0)
436 return err;
Christoph Hellwig7b86ac32019-08-28 16:19:54 +0200437 return __walk_page_range(vma->vm_start, vma->vm_end, &walk);
Naoya Horiguchi900fc5f2015-02-11 15:27:40 -0800438}
Thomas Hellstromecaad8a2019-10-01 11:17:34 +0200439
440/**
441 * walk_page_mapping - walk all memory areas mapped into a struct address_space.
442 * @mapping: Pointer to the struct address_space
443 * @first_index: First page offset in the address_space
444 * @nr: Number of incremental page offsets to cover
445 * @ops: operation to call during the walk
446 * @private: private data for callbacks' usage
447 *
448 * This function walks all memory areas mapped into a struct address_space.
449 * The walk is limited to only the given page-size index range, but if
450 * the index boundaries cross a huge page-table entry, that entry will be
451 * included.
452 *
453 * Also see walk_page_range() for additional information.
454 *
455 * Locking:
456 * This function can't require that the struct mm_struct::mmap_sem is held,
457 * since @mapping may be mapped by multiple processes. Instead
458 * @mapping->i_mmap_rwsem must be held. This might have implications in the
459 * callbacks, and it's up tho the caller to ensure that the
460 * struct mm_struct::mmap_sem is not needed.
461 *
462 * Also this means that a caller can't rely on the struct
463 * vm_area_struct::vm_flags to be constant across a call,
464 * except for immutable flags. Callers requiring this shouldn't use
465 * this function.
466 *
467 * Return: 0 on success, negative error code on failure, positive number on
468 * caller defined premature termination.
469 */
470int walk_page_mapping(struct address_space *mapping, pgoff_t first_index,
471 pgoff_t nr, const struct mm_walk_ops *ops,
472 void *private)
473{
474 struct mm_walk walk = {
475 .ops = ops,
476 .private = private,
477 };
478 struct vm_area_struct *vma;
479 pgoff_t vba, vea, cba, cea;
480 unsigned long start_addr, end_addr;
481 int err = 0;
482
483 lockdep_assert_held(&mapping->i_mmap_rwsem);
484 vma_interval_tree_foreach(vma, &mapping->i_mmap, first_index,
485 first_index + nr - 1) {
486 /* Clip to the vma */
487 vba = vma->vm_pgoff;
488 vea = vba + vma_pages(vma);
489 cba = first_index;
490 cba = max(cba, vba);
491 cea = first_index + nr;
492 cea = min(cea, vea);
493
494 start_addr = ((cba - vba) << PAGE_SHIFT) + vma->vm_start;
495 end_addr = ((cea - vba) << PAGE_SHIFT) + vma->vm_start;
496 if (start_addr >= end_addr)
497 continue;
498
499 walk.vma = vma;
500 walk.mm = vma->vm_mm;
501
502 err = walk_page_test(vma->vm_start, vma->vm_end, &walk);
503 if (err > 0) {
504 err = 0;
505 break;
506 } else if (err < 0)
507 break;
508
509 err = __walk_page_range(start_addr, end_addr, &walk);
510 if (err)
511 break;
512 }
513
514 return err;
515}