blob: 690af44609e2d9c62cd6d37edf47875f29adeaa2 [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);
Naoya Horiguchi48684a62015-02-11 15:28:06 -080042 if (pmd_none(*pmd) || !walk->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 Price3afc4232020-02-03 17:35:45 -080068 if (walk->action == ACTION_CONTINUE ||
69 !(ops->pte_entry))
Dave Hansen03319322011-03-22 16:32:56 -070070 continue;
71
Kirill A. Shutemov78ddc532016-01-15 16:52:42 -080072 split_huge_pmd(walk->vma, pmd, addr);
Naoya Horiguchifafaa422015-02-11 15:27:37 -080073 if (pmd_trans_unstable(pmd))
Dave Hansen03319322011-03-22 16:32:56 -070074 goto again;
Steven Price3afc4232020-02-03 17:35:45 -080075
Dave Hansen03319322011-03-22 16:32:56 -070076 err = walk_pte_range(pmd, addr, next, walk);
Matt Mackalle6473092008-02-04 22:29:01 -080077 if (err)
78 break;
79 } while (pmd++, addr = next, addr != end);
80
81 return err;
82}
83
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +030084static int walk_pud_range(p4d_t *p4d, unsigned long addr, unsigned long end,
Dave Hansen21650092008-06-12 15:21:47 -070085 struct mm_walk *walk)
Matt Mackalle6473092008-02-04 22:29:01 -080086{
87 pud_t *pud;
88 unsigned long next;
Christoph Hellwig7b86ac32019-08-28 16:19:54 +020089 const struct mm_walk_ops *ops = walk->ops;
Matt Mackalle6473092008-02-04 22:29:01 -080090 int err = 0;
91
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +030092 pud = pud_offset(p4d, addr);
Matt Mackalle6473092008-02-04 22:29:01 -080093 do {
Matthew Wilcoxa00cc7d2017-02-24 14:57:02 -080094 again:
Matt Mackalle6473092008-02-04 22:29:01 -080095 next = pud_addr_end(addr, end);
Matthew Wilcoxa00cc7d2017-02-24 14:57:02 -080096 if (pud_none(*pud) || !walk->vma) {
Christoph Hellwig7b86ac32019-08-28 16:19:54 +020097 if (ops->pte_hole)
98 err = ops->pte_hole(addr, next, walk);
Matt Mackalle6473092008-02-04 22:29:01 -080099 if (err)
100 break;
101 continue;
102 }
Matthew Wilcoxa00cc7d2017-02-24 14:57:02 -0800103
Steven Price3afc4232020-02-03 17:35:45 -0800104 walk->action = ACTION_SUBTREE;
Matthew Wilcoxa00cc7d2017-02-24 14:57:02 -0800105
Steven Price3afc4232020-02-03 17:35:45 -0800106 if (ops->pud_entry)
107 err = ops->pud_entry(pud, addr, next, walk);
108 if (err)
109 break;
110
111 if (walk->action == ACTION_AGAIN)
112 goto again;
113
114 if (walk->action == ACTION_CONTINUE ||
115 !(ops->pmd_entry || ops->pte_entry))
116 continue;
Matthew Wilcoxa00cc7d2017-02-24 14:57:02 -0800117
118 split_huge_pud(walk->vma, pud, addr);
119 if (pud_none(*pud))
120 goto again;
121
Steven Price3afc4232020-02-03 17:35:45 -0800122 err = walk_pmd_range(pud, addr, next, walk);
Matt Mackalle6473092008-02-04 22:29:01 -0800123 if (err)
124 break;
125 } while (pud++, addr = next, addr != end);
126
127 return err;
128}
129
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +0300130static int walk_p4d_range(pgd_t *pgd, unsigned long addr, unsigned long end,
131 struct mm_walk *walk)
132{
133 p4d_t *p4d;
134 unsigned long next;
Christoph Hellwig7b86ac32019-08-28 16:19:54 +0200135 const struct mm_walk_ops *ops = walk->ops;
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +0300136 int err = 0;
137
138 p4d = p4d_offset(pgd, addr);
139 do {
140 next = p4d_addr_end(addr, end);
141 if (p4d_none_or_clear_bad(p4d)) {
Christoph Hellwig7b86ac32019-08-28 16:19:54 +0200142 if (ops->pte_hole)
143 err = ops->pte_hole(addr, next, walk);
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +0300144 if (err)
145 break;
146 continue;
147 }
Steven Price3afc4232020-02-03 17:35:45 -0800148 if (ops->p4d_entry) {
149 err = ops->p4d_entry(p4d, addr, next, walk);
150 if (err)
151 break;
152 }
153 if (ops->pud_entry || ops->pmd_entry || ops->pte_entry)
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +0300154 err = walk_pud_range(p4d, addr, next, walk);
155 if (err)
156 break;
157 } while (p4d++, addr = next, addr != end);
158
159 return err;
160}
161
Naoya Horiguchifafaa422015-02-11 15:27:37 -0800162static int walk_pgd_range(unsigned long addr, unsigned long end,
163 struct mm_walk *walk)
164{
165 pgd_t *pgd;
166 unsigned long next;
Christoph Hellwig7b86ac32019-08-28 16:19:54 +0200167 const struct mm_walk_ops *ops = walk->ops;
Naoya Horiguchifafaa422015-02-11 15:27:37 -0800168 int err = 0;
169
170 pgd = pgd_offset(walk->mm, addr);
171 do {
172 next = pgd_addr_end(addr, end);
173 if (pgd_none_or_clear_bad(pgd)) {
Christoph Hellwig7b86ac32019-08-28 16:19:54 +0200174 if (ops->pte_hole)
175 err = ops->pte_hole(addr, next, walk);
Naoya Horiguchifafaa422015-02-11 15:27:37 -0800176 if (err)
177 break;
178 continue;
179 }
Steven Price3afc4232020-02-03 17:35:45 -0800180 if (ops->pgd_entry) {
181 err = ops->pgd_entry(pgd, addr, next, walk);
182 if (err)
183 break;
184 }
185 if (ops->p4d_entry || ops->pud_entry || ops->pmd_entry ||
186 ops->pte_entry)
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +0300187 err = walk_p4d_range(pgd, addr, next, walk);
Naoya Horiguchifafaa422015-02-11 15:27:37 -0800188 if (err)
189 break;
190 } while (pgd++, addr = next, addr != end);
191
192 return err;
193}
194
Naoya Horiguchi116354d2010-04-06 14:35:04 -0700195#ifdef CONFIG_HUGETLB_PAGE
196static unsigned long hugetlb_entry_end(struct hstate *h, unsigned long addr,
197 unsigned long end)
198{
199 unsigned long boundary = (addr & huge_page_mask(h)) + huge_page_size(h);
200 return boundary < end ? boundary : end;
201}
202
Naoya Horiguchifafaa422015-02-11 15:27:37 -0800203static int walk_hugetlb_range(unsigned long addr, unsigned long end,
Naoya Horiguchi116354d2010-04-06 14:35:04 -0700204 struct mm_walk *walk)
205{
Naoya Horiguchifafaa422015-02-11 15:27:37 -0800206 struct vm_area_struct *vma = walk->vma;
Naoya Horiguchi116354d2010-04-06 14:35:04 -0700207 struct hstate *h = hstate_vma(vma);
208 unsigned long next;
209 unsigned long hmask = huge_page_mask(h);
Punit Agrawal7868a202017-07-06 15:39:42 -0700210 unsigned long sz = huge_page_size(h);
Naoya Horiguchi116354d2010-04-06 14:35:04 -0700211 pte_t *pte;
Christoph Hellwig7b86ac32019-08-28 16:19:54 +0200212 const struct mm_walk_ops *ops = walk->ops;
Naoya Horiguchi116354d2010-04-06 14:35:04 -0700213 int err = 0;
214
215 do {
216 next = hugetlb_entry_end(h, addr, end);
Punit Agrawal7868a202017-07-06 15:39:42 -0700217 pte = huge_pte_offset(walk->mm, addr & hmask, sz);
Jann Horn373c4552017-11-14 01:03:44 +0100218
219 if (pte)
Christoph Hellwig7b86ac32019-08-28 16:19:54 +0200220 err = ops->hugetlb_entry(pte, hmask, addr, next, walk);
221 else if (ops->pte_hole)
222 err = ops->pte_hole(addr, next, walk);
Jann Horn373c4552017-11-14 01:03:44 +0100223
Naoya Horiguchi116354d2010-04-06 14:35:04 -0700224 if (err)
Naoya Horiguchifafaa422015-02-11 15:27:37 -0800225 break;
Naoya Horiguchi116354d2010-04-06 14:35:04 -0700226 } while (addr = next, addr != end);
227
Naoya Horiguchifafaa422015-02-11 15:27:37 -0800228 return err;
Naoya Horiguchi116354d2010-04-06 14:35:04 -0700229}
KOSAKI Motohiro6c6d5282011-07-25 17:12:09 -0700230
KOSAKI Motohiro6c6d5282011-07-25 17:12:09 -0700231#else /* CONFIG_HUGETLB_PAGE */
Naoya Horiguchifafaa422015-02-11 15:27:37 -0800232static int walk_hugetlb_range(unsigned long addr, unsigned long end,
KOSAKI Motohiro6c6d5282011-07-25 17:12:09 -0700233 struct mm_walk *walk)
234{
235 return 0;
236}
237
238#endif /* CONFIG_HUGETLB_PAGE */
239
Naoya Horiguchifafaa422015-02-11 15:27:37 -0800240/*
241 * Decide whether we really walk over the current vma on [@start, @end)
242 * or skip it via the returned value. Return 0 if we do walk over the
243 * current vma, and return 1 if we skip the vma. Negative values means
244 * error, where we abort the current walk.
Naoya Horiguchifafaa422015-02-11 15:27:37 -0800245 */
246static int walk_page_test(unsigned long start, unsigned long end,
247 struct mm_walk *walk)
248{
249 struct vm_area_struct *vma = walk->vma;
Christoph Hellwig7b86ac32019-08-28 16:19:54 +0200250 const struct mm_walk_ops *ops = walk->ops;
KOSAKI Motohiro6c6d5282011-07-25 17:12:09 -0700251
Christoph Hellwig7b86ac32019-08-28 16:19:54 +0200252 if (ops->test_walk)
253 return ops->test_walk(start, end, walk);
Naoya Horiguchifafaa422015-02-11 15:27:37 -0800254
255 /*
Naoya Horiguchi48684a62015-02-11 15:28:06 -0800256 * vma(VM_PFNMAP) doesn't have any valid struct pages behind VM_PFNMAP
257 * range, so we don't walk over it as we do for normal vmas. However,
258 * Some callers are interested in handling hole range and they don't
259 * want to just ignore any single address range. Such users certainly
260 * define their ->pte_hole() callbacks, so let's delegate them to handle
261 * vma(VM_PFNMAP).
Naoya Horiguchifafaa422015-02-11 15:27:37 -0800262 */
Naoya Horiguchi48684a62015-02-11 15:28:06 -0800263 if (vma->vm_flags & VM_PFNMAP) {
264 int err = 1;
Christoph Hellwig7b86ac32019-08-28 16:19:54 +0200265 if (ops->pte_hole)
266 err = ops->pte_hole(start, end, walk);
Naoya Horiguchi48684a62015-02-11 15:28:06 -0800267 return err ? err : 1;
268 }
Naoya Horiguchifafaa422015-02-11 15:27:37 -0800269 return 0;
270}
271
272static int __walk_page_range(unsigned long start, unsigned long end,
273 struct mm_walk *walk)
274{
275 int err = 0;
276 struct vm_area_struct *vma = walk->vma;
Thomas Hellstromecaad8a2019-10-01 11:17:34 +0200277 const struct mm_walk_ops *ops = walk->ops;
278
279 if (vma && ops->pre_vma) {
280 err = ops->pre_vma(start, end, walk);
281 if (err)
282 return err;
283 }
Naoya Horiguchifafaa422015-02-11 15:27:37 -0800284
285 if (vma && is_vm_hugetlb_page(vma)) {
Thomas Hellstromecaad8a2019-10-01 11:17:34 +0200286 if (ops->hugetlb_entry)
Naoya Horiguchifafaa422015-02-11 15:27:37 -0800287 err = walk_hugetlb_range(start, end, walk);
288 } else
289 err = walk_pgd_range(start, end, walk);
290
Thomas Hellstromecaad8a2019-10-01 11:17:34 +0200291 if (vma && ops->post_vma)
292 ops->post_vma(walk);
293
Naoya Horiguchifafaa422015-02-11 15:27:37 -0800294 return err;
295}
Naoya Horiguchi116354d2010-04-06 14:35:04 -0700296
Matt Mackalle6473092008-02-04 22:29:01 -0800297/**
Naoya Horiguchifafaa422015-02-11 15:27:37 -0800298 * walk_page_range - walk page table with caller specific callbacks
Christoph Hellwig7b86ac32019-08-28 16:19:54 +0200299 * @mm: mm_struct representing the target process of page table walk
300 * @start: start address of the virtual address range
301 * @end: end address of the virtual address range
302 * @ops: operation to call during the walk
303 * @private: private data for callbacks' usage
Matt Mackalle6473092008-02-04 22:29:01 -0800304 *
Christoph Hellwig7b86ac32019-08-28 16:19:54 +0200305 * Recursively walk the page table tree of the process represented by @mm
Naoya Horiguchifafaa422015-02-11 15:27:37 -0800306 * within the virtual address range [@start, @end). During walking, we can do
307 * some caller-specific works for each entry, by setting up pmd_entry(),
308 * pte_entry(), and/or hugetlb_entry(). If you don't set up for some of these
309 * callbacks, the associated entries/pages are just ignored.
310 * The return values of these callbacks are commonly defined like below:
Mike Rapoporta5d09be2018-02-06 15:42:19 -0800311 *
Naoya Horiguchifafaa422015-02-11 15:27:37 -0800312 * - 0 : succeeded to handle the current entry, and if you don't reach the
313 * end address yet, continue to walk.
314 * - >0 : succeeded to handle the current entry, and return to the caller
315 * with caller specific value.
316 * - <0 : failed to handle the current entry, and return to the caller
317 * with error code.
Matt Mackalle6473092008-02-04 22:29:01 -0800318 *
Naoya Horiguchifafaa422015-02-11 15:27:37 -0800319 * Before starting to walk page table, some callers want to check whether
320 * they really want to walk over the current vma, typically by checking
Christoph Hellwig7b86ac32019-08-28 16:19:54 +0200321 * its vm_flags. walk_page_test() and @ops->test_walk() are used for this
Naoya Horiguchifafaa422015-02-11 15:27:37 -0800322 * purpose.
Matt Mackalle6473092008-02-04 22:29:01 -0800323 *
Thomas Hellstromecaad8a2019-10-01 11:17:34 +0200324 * If operations need to be staged before and committed after a vma is walked,
325 * there are two callbacks, pre_vma() and post_vma(). Note that post_vma(),
326 * since it is intended to handle commit-type operations, can't return any
327 * errors.
328 *
Naoya Horiguchifafaa422015-02-11 15:27:37 -0800329 * struct mm_walk keeps current values of some common data like vma and pmd,
330 * which are useful for the access from callbacks. If you want to pass some
Christoph Hellwig7b86ac32019-08-28 16:19:54 +0200331 * caller-specific data to callbacks, @private should be helpful.
Matt Mackalle6473092008-02-04 22:29:01 -0800332 *
Naoya Horiguchifafaa422015-02-11 15:27:37 -0800333 * Locking:
Christoph Hellwig7b86ac32019-08-28 16:19:54 +0200334 * Callers of walk_page_range() and walk_page_vma() should hold @mm->mmap_sem,
335 * because these function traverse vma list and/or access to vma's data.
Matt Mackalle6473092008-02-04 22:29:01 -0800336 */
Christoph Hellwig7b86ac32019-08-28 16:19:54 +0200337int walk_page_range(struct mm_struct *mm, unsigned long start,
338 unsigned long end, const struct mm_walk_ops *ops,
339 void *private)
Matt Mackalle6473092008-02-04 22:29:01 -0800340{
Matt Mackalle6473092008-02-04 22:29:01 -0800341 int err = 0;
Naoya Horiguchifafaa422015-02-11 15:27:37 -0800342 unsigned long next;
343 struct vm_area_struct *vma;
Christoph Hellwig7b86ac32019-08-28 16:19:54 +0200344 struct mm_walk walk = {
345 .ops = ops,
346 .mm = mm,
347 .private = private,
348 };
Matt Mackalle6473092008-02-04 22:29:01 -0800349
Naoya Horiguchifafaa422015-02-11 15:27:37 -0800350 if (start >= end)
351 return -EINVAL;
Matt Mackalle6473092008-02-04 22:29:01 -0800352
Christoph Hellwig7b86ac32019-08-28 16:19:54 +0200353 if (!walk.mm)
Dave Hansen21650092008-06-12 15:21:47 -0700354 return -EINVAL;
355
Christoph Hellwigb4bc7812019-08-28 16:19:55 +0200356 lockdep_assert_held(&walk.mm->mmap_sem);
Cliff Wickmana9ff7852013-05-24 15:55:36 -0700357
Christoph Hellwig7b86ac32019-08-28 16:19:54 +0200358 vma = find_vma(walk.mm, start);
Matt Mackalle6473092008-02-04 22:29:01 -0800359 do {
Naoya Horiguchifafaa422015-02-11 15:27:37 -0800360 if (!vma) { /* after the last vma */
Christoph Hellwig7b86ac32019-08-28 16:19:54 +0200361 walk.vma = NULL;
Naoya Horiguchifafaa422015-02-11 15:27:37 -0800362 next = end;
363 } else if (start < vma->vm_start) { /* outside vma */
Christoph Hellwig7b86ac32019-08-28 16:19:54 +0200364 walk.vma = NULL;
Naoya Horiguchifafaa422015-02-11 15:27:37 -0800365 next = min(end, vma->vm_start);
366 } else { /* inside vma */
Christoph Hellwig7b86ac32019-08-28 16:19:54 +0200367 walk.vma = vma;
Naoya Horiguchifafaa422015-02-11 15:27:37 -0800368 next = min(end, vma->vm_end);
369 vma = vma->vm_next;
David Sterba5f0af702010-11-24 12:57:10 -0800370
Christoph Hellwig7b86ac32019-08-28 16:19:54 +0200371 err = walk_page_test(start, next, &walk);
Naoya Horiguchif6837392015-03-25 15:55:14 -0700372 if (err > 0) {
373 /*
374 * positive return values are purely for
375 * controlling the pagewalk, so should never
376 * be passed to the callers.
377 */
378 err = 0;
Cliff Wickmana9ff7852013-05-24 15:55:36 -0700379 continue;
Naoya Horiguchif6837392015-03-25 15:55:14 -0700380 }
Naoya Horiguchifafaa422015-02-11 15:27:37 -0800381 if (err < 0)
Matt Mackalle6473092008-02-04 22:29:01 -0800382 break;
Matt Mackalle6473092008-02-04 22:29:01 -0800383 }
Christoph Hellwig7b86ac32019-08-28 16:19:54 +0200384 if (walk.vma || walk.ops->pte_hole)
385 err = __walk_page_range(start, next, &walk);
Matt Mackalle6473092008-02-04 22:29:01 -0800386 if (err)
387 break;
Naoya Horiguchifafaa422015-02-11 15:27:37 -0800388 } while (start = next, start < end);
Matt Mackalle6473092008-02-04 22:29:01 -0800389 return err;
390}
Naoya Horiguchi900fc5f2015-02-11 15:27:40 -0800391
Christoph Hellwig7b86ac32019-08-28 16:19:54 +0200392int walk_page_vma(struct vm_area_struct *vma, const struct mm_walk_ops *ops,
393 void *private)
Naoya Horiguchi900fc5f2015-02-11 15:27:40 -0800394{
Christoph Hellwig7b86ac32019-08-28 16:19:54 +0200395 struct mm_walk walk = {
396 .ops = ops,
397 .mm = vma->vm_mm,
398 .vma = vma,
399 .private = private,
400 };
Naoya Horiguchi900fc5f2015-02-11 15:27:40 -0800401 int err;
402
Christoph Hellwig7b86ac32019-08-28 16:19:54 +0200403 if (!walk.mm)
Naoya Horiguchi900fc5f2015-02-11 15:27:40 -0800404 return -EINVAL;
405
Christoph Hellwigb4bc7812019-08-28 16:19:55 +0200406 lockdep_assert_held(&walk.mm->mmap_sem);
Christoph Hellwig7b86ac32019-08-28 16:19:54 +0200407
408 err = walk_page_test(vma->vm_start, vma->vm_end, &walk);
Naoya Horiguchi900fc5f2015-02-11 15:27:40 -0800409 if (err > 0)
410 return 0;
411 if (err < 0)
412 return err;
Christoph Hellwig7b86ac32019-08-28 16:19:54 +0200413 return __walk_page_range(vma->vm_start, vma->vm_end, &walk);
Naoya Horiguchi900fc5f2015-02-11 15:27:40 -0800414}
Thomas Hellstromecaad8a2019-10-01 11:17:34 +0200415
416/**
417 * walk_page_mapping - walk all memory areas mapped into a struct address_space.
418 * @mapping: Pointer to the struct address_space
419 * @first_index: First page offset in the address_space
420 * @nr: Number of incremental page offsets to cover
421 * @ops: operation to call during the walk
422 * @private: private data for callbacks' usage
423 *
424 * This function walks all memory areas mapped into a struct address_space.
425 * The walk is limited to only the given page-size index range, but if
426 * the index boundaries cross a huge page-table entry, that entry will be
427 * included.
428 *
429 * Also see walk_page_range() for additional information.
430 *
431 * Locking:
432 * This function can't require that the struct mm_struct::mmap_sem is held,
433 * since @mapping may be mapped by multiple processes. Instead
434 * @mapping->i_mmap_rwsem must be held. This might have implications in the
435 * callbacks, and it's up tho the caller to ensure that the
436 * struct mm_struct::mmap_sem is not needed.
437 *
438 * Also this means that a caller can't rely on the struct
439 * vm_area_struct::vm_flags to be constant across a call,
440 * except for immutable flags. Callers requiring this shouldn't use
441 * this function.
442 *
443 * Return: 0 on success, negative error code on failure, positive number on
444 * caller defined premature termination.
445 */
446int walk_page_mapping(struct address_space *mapping, pgoff_t first_index,
447 pgoff_t nr, const struct mm_walk_ops *ops,
448 void *private)
449{
450 struct mm_walk walk = {
451 .ops = ops,
452 .private = private,
453 };
454 struct vm_area_struct *vma;
455 pgoff_t vba, vea, cba, cea;
456 unsigned long start_addr, end_addr;
457 int err = 0;
458
459 lockdep_assert_held(&mapping->i_mmap_rwsem);
460 vma_interval_tree_foreach(vma, &mapping->i_mmap, first_index,
461 first_index + nr - 1) {
462 /* Clip to the vma */
463 vba = vma->vm_pgoff;
464 vea = vba + vma_pages(vma);
465 cba = first_index;
466 cba = max(cba, vba);
467 cea = first_index + nr;
468 cea = min(cea, vea);
469
470 start_addr = ((cba - vba) << PAGE_SHIFT) + vma->vm_start;
471 end_addr = ((cea - vba) << PAGE_SHIFT) + vma->vm_start;
472 if (start_addr >= end_addr)
473 continue;
474
475 walk.vma = vma;
476 walk.mm = vma->vm_mm;
477
478 err = walk_page_test(vma->vm_start, vma->vm_end, &walk);
479 if (err > 0) {
480 err = 0;
481 break;
482 } else if (err < 0)
483 break;
484
485 err = __walk_page_range(start_addr, end_addr, &walk);
486 if (err)
487 break;
488 }
489
490 return err;
491}