blob: ea0b9e606ad13525a726899e818cc0104d5185f2 [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 }
Dave Hansen03319322011-03-22 16:32:56 -070049 /*
50 * This implies that each ->pmd_entry() handler
51 * needs to know about pmd_trans_huge() pmds
52 */
Christoph Hellwig7b86ac32019-08-28 16:19:54 +020053 if (ops->pmd_entry)
54 err = ops->pmd_entry(pmd, addr, next, walk);
Dave Hansen03319322011-03-22 16:32:56 -070055 if (err)
56 break;
57
58 /*
59 * Check this here so we only break down trans_huge
60 * pages when we _need_ to
61 */
Christoph Hellwig7b86ac32019-08-28 16:19:54 +020062 if (!ops->pte_entry)
Dave Hansen03319322011-03-22 16:32:56 -070063 continue;
64
Kirill A. Shutemov78ddc532016-01-15 16:52:42 -080065 split_huge_pmd(walk->vma, pmd, addr);
Naoya Horiguchifafaa422015-02-11 15:27:37 -080066 if (pmd_trans_unstable(pmd))
Dave Hansen03319322011-03-22 16:32:56 -070067 goto again;
68 err = walk_pte_range(pmd, addr, next, walk);
Matt Mackalle6473092008-02-04 22:29:01 -080069 if (err)
70 break;
71 } while (pmd++, addr = next, addr != end);
72
73 return err;
74}
75
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +030076static int walk_pud_range(p4d_t *p4d, unsigned long addr, unsigned long end,
Dave Hansen21650092008-06-12 15:21:47 -070077 struct mm_walk *walk)
Matt Mackalle6473092008-02-04 22:29:01 -080078{
79 pud_t *pud;
80 unsigned long next;
Christoph Hellwig7b86ac32019-08-28 16:19:54 +020081 const struct mm_walk_ops *ops = walk->ops;
Matt Mackalle6473092008-02-04 22:29:01 -080082 int err = 0;
83
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +030084 pud = pud_offset(p4d, addr);
Matt Mackalle6473092008-02-04 22:29:01 -080085 do {
Matthew Wilcoxa00cc7d2017-02-24 14:57:02 -080086 again:
Matt Mackalle6473092008-02-04 22:29:01 -080087 next = pud_addr_end(addr, end);
Matthew Wilcoxa00cc7d2017-02-24 14:57:02 -080088 if (pud_none(*pud) || !walk->vma) {
Christoph Hellwig7b86ac32019-08-28 16:19:54 +020089 if (ops->pte_hole)
90 err = ops->pte_hole(addr, next, walk);
Matt Mackalle6473092008-02-04 22:29:01 -080091 if (err)
92 break;
93 continue;
94 }
Matthew Wilcoxa00cc7d2017-02-24 14:57:02 -080095
Christoph Hellwig7b86ac32019-08-28 16:19:54 +020096 if (ops->pud_entry) {
Matthew Wilcoxa00cc7d2017-02-24 14:57:02 -080097 spinlock_t *ptl = pud_trans_huge_lock(pud, walk->vma);
98
99 if (ptl) {
Christoph Hellwig7b86ac32019-08-28 16:19:54 +0200100 err = ops->pud_entry(pud, addr, next, walk);
Matthew Wilcoxa00cc7d2017-02-24 14:57:02 -0800101 spin_unlock(ptl);
102 if (err)
103 break;
104 continue;
105 }
106 }
107
108 split_huge_pud(walk->vma, pud, addr);
109 if (pud_none(*pud))
110 goto again;
111
Christoph Hellwig7b86ac32019-08-28 16:19:54 +0200112 if (ops->pmd_entry || ops->pte_entry)
Dave Hansen21650092008-06-12 15:21:47 -0700113 err = walk_pmd_range(pud, addr, next, walk);
Matt Mackalle6473092008-02-04 22:29:01 -0800114 if (err)
115 break;
116 } while (pud++, addr = next, addr != end);
117
118 return err;
119}
120
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +0300121static int walk_p4d_range(pgd_t *pgd, unsigned long addr, unsigned long end,
122 struct mm_walk *walk)
123{
124 p4d_t *p4d;
125 unsigned long next;
Christoph Hellwig7b86ac32019-08-28 16:19:54 +0200126 const struct mm_walk_ops *ops = walk->ops;
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +0300127 int err = 0;
128
129 p4d = p4d_offset(pgd, addr);
130 do {
131 next = p4d_addr_end(addr, end);
132 if (p4d_none_or_clear_bad(p4d)) {
Christoph Hellwig7b86ac32019-08-28 16:19:54 +0200133 if (ops->pte_hole)
134 err = ops->pte_hole(addr, next, walk);
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +0300135 if (err)
136 break;
137 continue;
138 }
Christoph Hellwig7b86ac32019-08-28 16:19:54 +0200139 if (ops->pmd_entry || ops->pte_entry)
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +0300140 err = walk_pud_range(p4d, addr, next, walk);
141 if (err)
142 break;
143 } while (p4d++, addr = next, addr != end);
144
145 return err;
146}
147
Naoya Horiguchifafaa422015-02-11 15:27:37 -0800148static int walk_pgd_range(unsigned long addr, unsigned long end,
149 struct mm_walk *walk)
150{
151 pgd_t *pgd;
152 unsigned long next;
Christoph Hellwig7b86ac32019-08-28 16:19:54 +0200153 const struct mm_walk_ops *ops = walk->ops;
Naoya Horiguchifafaa422015-02-11 15:27:37 -0800154 int err = 0;
155
156 pgd = pgd_offset(walk->mm, addr);
157 do {
158 next = pgd_addr_end(addr, end);
159 if (pgd_none_or_clear_bad(pgd)) {
Christoph Hellwig7b86ac32019-08-28 16:19:54 +0200160 if (ops->pte_hole)
161 err = ops->pte_hole(addr, next, walk);
Naoya Horiguchifafaa422015-02-11 15:27:37 -0800162 if (err)
163 break;
164 continue;
165 }
Christoph Hellwig7b86ac32019-08-28 16:19:54 +0200166 if (ops->pmd_entry || ops->pte_entry)
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +0300167 err = walk_p4d_range(pgd, addr, next, walk);
Naoya Horiguchifafaa422015-02-11 15:27:37 -0800168 if (err)
169 break;
170 } while (pgd++, addr = next, addr != end);
171
172 return err;
173}
174
Naoya Horiguchi116354d2010-04-06 14:35:04 -0700175#ifdef CONFIG_HUGETLB_PAGE
176static unsigned long hugetlb_entry_end(struct hstate *h, unsigned long addr,
177 unsigned long end)
178{
179 unsigned long boundary = (addr & huge_page_mask(h)) + huge_page_size(h);
180 return boundary < end ? boundary : end;
181}
182
Naoya Horiguchifafaa422015-02-11 15:27:37 -0800183static int walk_hugetlb_range(unsigned long addr, unsigned long end,
Naoya Horiguchi116354d2010-04-06 14:35:04 -0700184 struct mm_walk *walk)
185{
Naoya Horiguchifafaa422015-02-11 15:27:37 -0800186 struct vm_area_struct *vma = walk->vma;
Naoya Horiguchi116354d2010-04-06 14:35:04 -0700187 struct hstate *h = hstate_vma(vma);
188 unsigned long next;
189 unsigned long hmask = huge_page_mask(h);
Punit Agrawal7868a202017-07-06 15:39:42 -0700190 unsigned long sz = huge_page_size(h);
Naoya Horiguchi116354d2010-04-06 14:35:04 -0700191 pte_t *pte;
Christoph Hellwig7b86ac32019-08-28 16:19:54 +0200192 const struct mm_walk_ops *ops = walk->ops;
Naoya Horiguchi116354d2010-04-06 14:35:04 -0700193 int err = 0;
194
195 do {
196 next = hugetlb_entry_end(h, addr, end);
Punit Agrawal7868a202017-07-06 15:39:42 -0700197 pte = huge_pte_offset(walk->mm, addr & hmask, sz);
Jann Horn373c4552017-11-14 01:03:44 +0100198
199 if (pte)
Christoph Hellwig7b86ac32019-08-28 16:19:54 +0200200 err = ops->hugetlb_entry(pte, hmask, addr, next, walk);
201 else if (ops->pte_hole)
202 err = ops->pte_hole(addr, next, walk);
Jann Horn373c4552017-11-14 01:03:44 +0100203
Naoya Horiguchi116354d2010-04-06 14:35:04 -0700204 if (err)
Naoya Horiguchifafaa422015-02-11 15:27:37 -0800205 break;
Naoya Horiguchi116354d2010-04-06 14:35:04 -0700206 } while (addr = next, addr != end);
207
Naoya Horiguchifafaa422015-02-11 15:27:37 -0800208 return err;
Naoya Horiguchi116354d2010-04-06 14:35:04 -0700209}
KOSAKI Motohiro6c6d5282011-07-25 17:12:09 -0700210
KOSAKI Motohiro6c6d5282011-07-25 17:12:09 -0700211#else /* CONFIG_HUGETLB_PAGE */
Naoya Horiguchifafaa422015-02-11 15:27:37 -0800212static int walk_hugetlb_range(unsigned long addr, unsigned long end,
KOSAKI Motohiro6c6d5282011-07-25 17:12:09 -0700213 struct mm_walk *walk)
214{
215 return 0;
216}
217
218#endif /* CONFIG_HUGETLB_PAGE */
219
Naoya Horiguchifafaa422015-02-11 15:27:37 -0800220/*
221 * Decide whether we really walk over the current vma on [@start, @end)
222 * or skip it via the returned value. Return 0 if we do walk over the
223 * current vma, and return 1 if we skip the vma. Negative values means
224 * error, where we abort the current walk.
Naoya Horiguchifafaa422015-02-11 15:27:37 -0800225 */
226static int walk_page_test(unsigned long start, unsigned long end,
227 struct mm_walk *walk)
228{
229 struct vm_area_struct *vma = walk->vma;
Christoph Hellwig7b86ac32019-08-28 16:19:54 +0200230 const struct mm_walk_ops *ops = walk->ops;
KOSAKI Motohiro6c6d5282011-07-25 17:12:09 -0700231
Christoph Hellwig7b86ac32019-08-28 16:19:54 +0200232 if (ops->test_walk)
233 return ops->test_walk(start, end, walk);
Naoya Horiguchifafaa422015-02-11 15:27:37 -0800234
235 /*
Naoya Horiguchi48684a62015-02-11 15:28:06 -0800236 * vma(VM_PFNMAP) doesn't have any valid struct pages behind VM_PFNMAP
237 * range, so we don't walk over it as we do for normal vmas. However,
238 * Some callers are interested in handling hole range and they don't
239 * want to just ignore any single address range. Such users certainly
240 * define their ->pte_hole() callbacks, so let's delegate them to handle
241 * vma(VM_PFNMAP).
Naoya Horiguchifafaa422015-02-11 15:27:37 -0800242 */
Naoya Horiguchi48684a62015-02-11 15:28:06 -0800243 if (vma->vm_flags & VM_PFNMAP) {
244 int err = 1;
Christoph Hellwig7b86ac32019-08-28 16:19:54 +0200245 if (ops->pte_hole)
246 err = ops->pte_hole(start, end, walk);
Naoya Horiguchi48684a62015-02-11 15:28:06 -0800247 return err ? err : 1;
248 }
Naoya Horiguchifafaa422015-02-11 15:27:37 -0800249 return 0;
250}
251
252static int __walk_page_range(unsigned long start, unsigned long end,
253 struct mm_walk *walk)
254{
255 int err = 0;
256 struct vm_area_struct *vma = walk->vma;
Thomas Hellstromecaad8a2019-10-01 11:17:34 +0200257 const struct mm_walk_ops *ops = walk->ops;
258
259 if (vma && ops->pre_vma) {
260 err = ops->pre_vma(start, end, walk);
261 if (err)
262 return err;
263 }
Naoya Horiguchifafaa422015-02-11 15:27:37 -0800264
265 if (vma && is_vm_hugetlb_page(vma)) {
Thomas Hellstromecaad8a2019-10-01 11:17:34 +0200266 if (ops->hugetlb_entry)
Naoya Horiguchifafaa422015-02-11 15:27:37 -0800267 err = walk_hugetlb_range(start, end, walk);
268 } else
269 err = walk_pgd_range(start, end, walk);
270
Thomas Hellstromecaad8a2019-10-01 11:17:34 +0200271 if (vma && ops->post_vma)
272 ops->post_vma(walk);
273
Naoya Horiguchifafaa422015-02-11 15:27:37 -0800274 return err;
275}
Naoya Horiguchi116354d2010-04-06 14:35:04 -0700276
Matt Mackalle6473092008-02-04 22:29:01 -0800277/**
Naoya Horiguchifafaa422015-02-11 15:27:37 -0800278 * walk_page_range - walk page table with caller specific callbacks
Christoph Hellwig7b86ac32019-08-28 16:19:54 +0200279 * @mm: mm_struct representing the target process of page table walk
280 * @start: start address of the virtual address range
281 * @end: end address of the virtual address range
282 * @ops: operation to call during the walk
283 * @private: private data for callbacks' usage
Matt Mackalle6473092008-02-04 22:29:01 -0800284 *
Christoph Hellwig7b86ac32019-08-28 16:19:54 +0200285 * Recursively walk the page table tree of the process represented by @mm
Naoya Horiguchifafaa422015-02-11 15:27:37 -0800286 * within the virtual address range [@start, @end). During walking, we can do
287 * some caller-specific works for each entry, by setting up pmd_entry(),
288 * pte_entry(), and/or hugetlb_entry(). If you don't set up for some of these
289 * callbacks, the associated entries/pages are just ignored.
290 * The return values of these callbacks are commonly defined like below:
Mike Rapoporta5d09be2018-02-06 15:42:19 -0800291 *
Naoya Horiguchifafaa422015-02-11 15:27:37 -0800292 * - 0 : succeeded to handle the current entry, and if you don't reach the
293 * end address yet, continue to walk.
294 * - >0 : succeeded to handle the current entry, and return to the caller
295 * with caller specific value.
296 * - <0 : failed to handle the current entry, and return to the caller
297 * with error code.
Matt Mackalle6473092008-02-04 22:29:01 -0800298 *
Naoya Horiguchifafaa422015-02-11 15:27:37 -0800299 * Before starting to walk page table, some callers want to check whether
300 * they really want to walk over the current vma, typically by checking
Christoph Hellwig7b86ac32019-08-28 16:19:54 +0200301 * its vm_flags. walk_page_test() and @ops->test_walk() are used for this
Naoya Horiguchifafaa422015-02-11 15:27:37 -0800302 * purpose.
Matt Mackalle6473092008-02-04 22:29:01 -0800303 *
Thomas Hellstromecaad8a2019-10-01 11:17:34 +0200304 * If operations need to be staged before and committed after a vma is walked,
305 * there are two callbacks, pre_vma() and post_vma(). Note that post_vma(),
306 * since it is intended to handle commit-type operations, can't return any
307 * errors.
308 *
Naoya Horiguchifafaa422015-02-11 15:27:37 -0800309 * struct mm_walk keeps current values of some common data like vma and pmd,
310 * which are useful for the access from callbacks. If you want to pass some
Christoph Hellwig7b86ac32019-08-28 16:19:54 +0200311 * caller-specific data to callbacks, @private should be helpful.
Matt Mackalle6473092008-02-04 22:29:01 -0800312 *
Naoya Horiguchifafaa422015-02-11 15:27:37 -0800313 * Locking:
Christoph Hellwig7b86ac32019-08-28 16:19:54 +0200314 * Callers of walk_page_range() and walk_page_vma() should hold @mm->mmap_sem,
315 * because these function traverse vma list and/or access to vma's data.
Matt Mackalle6473092008-02-04 22:29:01 -0800316 */
Christoph Hellwig7b86ac32019-08-28 16:19:54 +0200317int walk_page_range(struct mm_struct *mm, unsigned long start,
318 unsigned long end, const struct mm_walk_ops *ops,
319 void *private)
Matt Mackalle6473092008-02-04 22:29:01 -0800320{
Matt Mackalle6473092008-02-04 22:29:01 -0800321 int err = 0;
Naoya Horiguchifafaa422015-02-11 15:27:37 -0800322 unsigned long next;
323 struct vm_area_struct *vma;
Christoph Hellwig7b86ac32019-08-28 16:19:54 +0200324 struct mm_walk walk = {
325 .ops = ops,
326 .mm = mm,
327 .private = private,
328 };
Matt Mackalle6473092008-02-04 22:29:01 -0800329
Naoya Horiguchifafaa422015-02-11 15:27:37 -0800330 if (start >= end)
331 return -EINVAL;
Matt Mackalle6473092008-02-04 22:29:01 -0800332
Christoph Hellwig7b86ac32019-08-28 16:19:54 +0200333 if (!walk.mm)
Dave Hansen21650092008-06-12 15:21:47 -0700334 return -EINVAL;
335
Christoph Hellwigb4bc7812019-08-28 16:19:55 +0200336 lockdep_assert_held(&walk.mm->mmap_sem);
Cliff Wickmana9ff7852013-05-24 15:55:36 -0700337
Christoph Hellwig7b86ac32019-08-28 16:19:54 +0200338 vma = find_vma(walk.mm, start);
Matt Mackalle6473092008-02-04 22:29:01 -0800339 do {
Naoya Horiguchifafaa422015-02-11 15:27:37 -0800340 if (!vma) { /* after the last vma */
Christoph Hellwig7b86ac32019-08-28 16:19:54 +0200341 walk.vma = NULL;
Naoya Horiguchifafaa422015-02-11 15:27:37 -0800342 next = end;
343 } else if (start < vma->vm_start) { /* outside vma */
Christoph Hellwig7b86ac32019-08-28 16:19:54 +0200344 walk.vma = NULL;
Naoya Horiguchifafaa422015-02-11 15:27:37 -0800345 next = min(end, vma->vm_start);
346 } else { /* inside vma */
Christoph Hellwig7b86ac32019-08-28 16:19:54 +0200347 walk.vma = vma;
Naoya Horiguchifafaa422015-02-11 15:27:37 -0800348 next = min(end, vma->vm_end);
349 vma = vma->vm_next;
David Sterba5f0af702010-11-24 12:57:10 -0800350
Christoph Hellwig7b86ac32019-08-28 16:19:54 +0200351 err = walk_page_test(start, next, &walk);
Naoya Horiguchif6837392015-03-25 15:55:14 -0700352 if (err > 0) {
353 /*
354 * positive return values are purely for
355 * controlling the pagewalk, so should never
356 * be passed to the callers.
357 */
358 err = 0;
Cliff Wickmana9ff7852013-05-24 15:55:36 -0700359 continue;
Naoya Horiguchif6837392015-03-25 15:55:14 -0700360 }
Naoya Horiguchifafaa422015-02-11 15:27:37 -0800361 if (err < 0)
Matt Mackalle6473092008-02-04 22:29:01 -0800362 break;
Matt Mackalle6473092008-02-04 22:29:01 -0800363 }
Christoph Hellwig7b86ac32019-08-28 16:19:54 +0200364 if (walk.vma || walk.ops->pte_hole)
365 err = __walk_page_range(start, next, &walk);
Matt Mackalle6473092008-02-04 22:29:01 -0800366 if (err)
367 break;
Naoya Horiguchifafaa422015-02-11 15:27:37 -0800368 } while (start = next, start < end);
Matt Mackalle6473092008-02-04 22:29:01 -0800369 return err;
370}
Naoya Horiguchi900fc5f2015-02-11 15:27:40 -0800371
Christoph Hellwig7b86ac32019-08-28 16:19:54 +0200372int walk_page_vma(struct vm_area_struct *vma, const struct mm_walk_ops *ops,
373 void *private)
Naoya Horiguchi900fc5f2015-02-11 15:27:40 -0800374{
Christoph Hellwig7b86ac32019-08-28 16:19:54 +0200375 struct mm_walk walk = {
376 .ops = ops,
377 .mm = vma->vm_mm,
378 .vma = vma,
379 .private = private,
380 };
Naoya Horiguchi900fc5f2015-02-11 15:27:40 -0800381 int err;
382
Christoph Hellwig7b86ac32019-08-28 16:19:54 +0200383 if (!walk.mm)
Naoya Horiguchi900fc5f2015-02-11 15:27:40 -0800384 return -EINVAL;
385
Christoph Hellwigb4bc7812019-08-28 16:19:55 +0200386 lockdep_assert_held(&walk.mm->mmap_sem);
Christoph Hellwig7b86ac32019-08-28 16:19:54 +0200387
388 err = walk_page_test(vma->vm_start, vma->vm_end, &walk);
Naoya Horiguchi900fc5f2015-02-11 15:27:40 -0800389 if (err > 0)
390 return 0;
391 if (err < 0)
392 return err;
Christoph Hellwig7b86ac32019-08-28 16:19:54 +0200393 return __walk_page_range(vma->vm_start, vma->vm_end, &walk);
Naoya Horiguchi900fc5f2015-02-11 15:27:40 -0800394}
Thomas Hellstromecaad8a2019-10-01 11:17:34 +0200395
396/**
397 * walk_page_mapping - walk all memory areas mapped into a struct address_space.
398 * @mapping: Pointer to the struct address_space
399 * @first_index: First page offset in the address_space
400 * @nr: Number of incremental page offsets to cover
401 * @ops: operation to call during the walk
402 * @private: private data for callbacks' usage
403 *
404 * This function walks all memory areas mapped into a struct address_space.
405 * The walk is limited to only the given page-size index range, but if
406 * the index boundaries cross a huge page-table entry, that entry will be
407 * included.
408 *
409 * Also see walk_page_range() for additional information.
410 *
411 * Locking:
412 * This function can't require that the struct mm_struct::mmap_sem is held,
413 * since @mapping may be mapped by multiple processes. Instead
414 * @mapping->i_mmap_rwsem must be held. This might have implications in the
415 * callbacks, and it's up tho the caller to ensure that the
416 * struct mm_struct::mmap_sem is not needed.
417 *
418 * Also this means that a caller can't rely on the struct
419 * vm_area_struct::vm_flags to be constant across a call,
420 * except for immutable flags. Callers requiring this shouldn't use
421 * this function.
422 *
423 * Return: 0 on success, negative error code on failure, positive number on
424 * caller defined premature termination.
425 */
426int walk_page_mapping(struct address_space *mapping, pgoff_t first_index,
427 pgoff_t nr, const struct mm_walk_ops *ops,
428 void *private)
429{
430 struct mm_walk walk = {
431 .ops = ops,
432 .private = private,
433 };
434 struct vm_area_struct *vma;
435 pgoff_t vba, vea, cba, cea;
436 unsigned long start_addr, end_addr;
437 int err = 0;
438
439 lockdep_assert_held(&mapping->i_mmap_rwsem);
440 vma_interval_tree_foreach(vma, &mapping->i_mmap, first_index,
441 first_index + nr - 1) {
442 /* Clip to the vma */
443 vba = vma->vm_pgoff;
444 vea = vba + vma_pages(vma);
445 cba = first_index;
446 cba = max(cba, vba);
447 cea = first_index + nr;
448 cea = min(cea, vea);
449
450 start_addr = ((cba - vba) << PAGE_SHIFT) + vma->vm_start;
451 end_addr = ((cea - vba) << PAGE_SHIFT) + vma->vm_start;
452 if (start_addr >= end_addr)
453 continue;
454
455 walk.vma = vma;
456 walk.mm = vma->vm_mm;
457
458 err = walk_page_test(vma->vm_start, vma->vm_end, &walk);
459 if (err > 0) {
460 err = 0;
461 break;
462 } else if (err < 0)
463 break;
464
465 err = __walk_page_range(start_addr, end_addr, &walk);
466 if (err)
467 break;
468 }
469
470 return err;
471}