]>
Commit | Line | Data |
---|---|---|
457c8996 | 1 | // SPDX-License-Identifier: GPL-2.0-only |
4bbd4c77 KS |
2 | #include <linux/kernel.h> |
3 | #include <linux/errno.h> | |
4 | #include <linux/err.h> | |
5 | #include <linux/spinlock.h> | |
6 | ||
4bbd4c77 | 7 | #include <linux/mm.h> |
3565fce3 | 8 | #include <linux/memremap.h> |
4bbd4c77 KS |
9 | #include <linux/pagemap.h> |
10 | #include <linux/rmap.h> | |
11 | #include <linux/swap.h> | |
12 | #include <linux/swapops.h> | |
1507f512 | 13 | #include <linux/secretmem.h> |
4bbd4c77 | 14 | |
174cd4b1 | 15 | #include <linux/sched/signal.h> |
2667f50e | 16 | #include <linux/rwsem.h> |
f30c59e9 | 17 | #include <linux/hugetlb.h> |
9a4e9f3b AK |
18 | #include <linux/migrate.h> |
19 | #include <linux/mm_inline.h> | |
20 | #include <linux/sched/mm.h> | |
1027e443 | 21 | |
33a709b2 | 22 | #include <asm/mmu_context.h> |
1027e443 | 23 | #include <asm/tlbflush.h> |
2667f50e | 24 | |
4bbd4c77 KS |
25 | #include "internal.h" |
26 | ||
df06b37f KB |
27 | struct follow_page_context { |
28 | struct dev_pagemap *pgmap; | |
29 | unsigned int page_mask; | |
30 | }; | |
31 | ||
47e29d32 JH |
32 | static void hpage_pincount_add(struct page *page, int refs) |
33 | { | |
34 | VM_BUG_ON_PAGE(!hpage_pincount_available(page), page); | |
35 | VM_BUG_ON_PAGE(page != compound_head(page), page); | |
36 | ||
37 | atomic_add(refs, compound_pincount_ptr(page)); | |
38 | } | |
39 | ||
40 | static void hpage_pincount_sub(struct page *page, int refs) | |
41 | { | |
42 | VM_BUG_ON_PAGE(!hpage_pincount_available(page), page); | |
43 | VM_BUG_ON_PAGE(page != compound_head(page), page); | |
44 | ||
45 | atomic_sub(refs, compound_pincount_ptr(page)); | |
46 | } | |
47 | ||
c24d3732 JH |
48 | /* Equivalent to calling put_page() @refs times. */ |
49 | static void put_page_refs(struct page *page, int refs) | |
50 | { | |
51 | #ifdef CONFIG_DEBUG_VM | |
52 | if (VM_WARN_ON_ONCE_PAGE(page_ref_count(page) < refs, page)) | |
53 | return; | |
54 | #endif | |
55 | ||
56 | /* | |
57 | * Calling put_page() for each ref is unnecessarily slow. Only the last | |
58 | * ref needs a put_page(). | |
59 | */ | |
60 | if (refs > 1) | |
61 | page_ref_sub(page, refs - 1); | |
62 | put_page(page); | |
63 | } | |
64 | ||
cd1adf1b LT |
65 | /* |
66 | * Return the compound head page with ref appropriately incremented, | |
67 | * or NULL if that failed. | |
a707cdd5 | 68 | */ |
cd1adf1b | 69 | static inline struct page *try_get_compound_head(struct page *page, int refs) |
a707cdd5 JH |
70 | { |
71 | struct page *head = compound_head(page); | |
72 | ||
73 | if (WARN_ON_ONCE(page_ref_count(head) < 0)) | |
74 | return NULL; | |
75 | if (unlikely(!page_cache_add_speculative(head, refs))) | |
76 | return NULL; | |
c24d3732 JH |
77 | |
78 | /* | |
79 | * At this point we have a stable reference to the head page; but it | |
80 | * could be that between the compound_head() lookup and the refcount | |
81 | * increment, the compound page was split, in which case we'd end up | |
82 | * holding a reference on a page that has nothing to do with the page | |
83 | * we were given anymore. | |
84 | * So now that the head page is stable, recheck that the pages still | |
85 | * belong together. | |
86 | */ | |
87 | if (unlikely(compound_head(page) != head)) { | |
88 | put_page_refs(head, refs); | |
89 | return NULL; | |
90 | } | |
91 | ||
a707cdd5 JH |
92 | return head; |
93 | } | |
94 | ||
3967db22 | 95 | /** |
3faa52c0 JH |
96 | * try_grab_compound_head() - attempt to elevate a page's refcount, by a |
97 | * flags-dependent amount. | |
98 | * | |
3967db22 JH |
99 | * Even though the name includes "compound_head", this function is still |
100 | * appropriate for callers that have a non-compound @page to get. | |
101 | * | |
102 | * @page: pointer to page to be grabbed | |
103 | * @refs: the value to (effectively) add to the page's refcount | |
104 | * @flags: gup flags: these are the FOLL_* flag values. | |
105 | * | |
3faa52c0 JH |
106 | * "grab" names in this file mean, "look at flags to decide whether to use |
107 | * FOLL_PIN or FOLL_GET behavior, when incrementing the page's refcount. | |
108 | * | |
109 | * Either FOLL_PIN or FOLL_GET (or neither) must be set, but not both at the | |
110 | * same time. (That's true throughout the get_user_pages*() and | |
111 | * pin_user_pages*() APIs.) Cases: | |
112 | * | |
3967db22 JH |
113 | * FOLL_GET: page's refcount will be incremented by @refs. |
114 | * | |
115 | * FOLL_PIN on compound pages that are > two pages long: page's refcount will | |
116 | * be incremented by @refs, and page[2].hpage_pinned_refcount will be | |
117 | * incremented by @refs * GUP_PIN_COUNTING_BIAS. | |
118 | * | |
119 | * FOLL_PIN on normal pages, or compound pages that are two pages long: | |
120 | * page's refcount will be incremented by @refs * GUP_PIN_COUNTING_BIAS. | |
3faa52c0 JH |
121 | * |
122 | * Return: head page (with refcount appropriately incremented) for success, or | |
123 | * NULL upon failure. If neither FOLL_GET nor FOLL_PIN was set, that's | |
124 | * considered failure, and furthermore, a likely bug in the caller, so a warning | |
125 | * is also emitted. | |
126 | */ | |
c36c04c2 JH |
127 | __maybe_unused struct page *try_grab_compound_head(struct page *page, |
128 | int refs, unsigned int flags) | |
3faa52c0 JH |
129 | { |
130 | if (flags & FOLL_GET) | |
131 | return try_get_compound_head(page, refs); | |
132 | else if (flags & FOLL_PIN) { | |
df3a0a21 | 133 | /* |
d1e153fe PT |
134 | * Can't do FOLL_LONGTERM + FOLL_PIN gup fast path if not in a |
135 | * right zone, so fail and let the caller fall back to the slow | |
136 | * path. | |
df3a0a21 | 137 | */ |
d1e153fe PT |
138 | if (unlikely((flags & FOLL_LONGTERM) && |
139 | !is_pinnable_page(page))) | |
df3a0a21 PL |
140 | return NULL; |
141 | ||
c24d3732 JH |
142 | /* |
143 | * CAUTION: Don't use compound_head() on the page before this | |
144 | * point, the result won't be stable. | |
145 | */ | |
146 | page = try_get_compound_head(page, refs); | |
147 | if (!page) | |
148 | return NULL; | |
149 | ||
47e29d32 JH |
150 | /* |
151 | * When pinning a compound page of order > 1 (which is what | |
152 | * hpage_pincount_available() checks for), use an exact count to | |
153 | * track it, via hpage_pincount_add/_sub(). | |
154 | * | |
155 | * However, be sure to *also* increment the normal page refcount | |
156 | * field at least once, so that the page really is pinned. | |
3967db22 JH |
157 | * That's why the refcount from the earlier |
158 | * try_get_compound_head() is left intact. | |
47e29d32 | 159 | */ |
47e29d32 JH |
160 | if (hpage_pincount_available(page)) |
161 | hpage_pincount_add(page, refs); | |
c24d3732 JH |
162 | else |
163 | page_ref_add(page, refs * (GUP_PIN_COUNTING_BIAS - 1)); | |
47e29d32 | 164 | |
1970dc6f | 165 | mod_node_page_state(page_pgdat(page), NR_FOLL_PIN_ACQUIRED, |
0fef147b | 166 | refs); |
1970dc6f | 167 | |
47e29d32 | 168 | return page; |
3faa52c0 JH |
169 | } |
170 | ||
171 | WARN_ON_ONCE(1); | |
172 | return NULL; | |
173 | } | |
174 | ||
4509b42c JG |
175 | static void put_compound_head(struct page *page, int refs, unsigned int flags) |
176 | { | |
177 | if (flags & FOLL_PIN) { | |
178 | mod_node_page_state(page_pgdat(page), NR_FOLL_PIN_RELEASED, | |
179 | refs); | |
180 | ||
181 | if (hpage_pincount_available(page)) | |
182 | hpage_pincount_sub(page, refs); | |
183 | else | |
184 | refs *= GUP_PIN_COUNTING_BIAS; | |
185 | } | |
186 | ||
c24d3732 | 187 | put_page_refs(page, refs); |
4509b42c JG |
188 | } |
189 | ||
3faa52c0 JH |
190 | /** |
191 | * try_grab_page() - elevate a page's refcount by a flag-dependent amount | |
192 | * | |
193 | * This might not do anything at all, depending on the flags argument. | |
194 | * | |
195 | * "grab" names in this file mean, "look at flags to decide whether to use | |
196 | * FOLL_PIN or FOLL_GET behavior, when incrementing the page's refcount. | |
197 | * | |
198 | * @page: pointer to page to be grabbed | |
199 | * @flags: gup flags: these are the FOLL_* flag values. | |
200 | * | |
201 | * Either FOLL_PIN or FOLL_GET (or neither) may be set, but not both at the same | |
3967db22 JH |
202 | * time. Cases: please see the try_grab_compound_head() documentation, with |
203 | * "refs=1". | |
3faa52c0 JH |
204 | * |
205 | * Return: true for success, or if no action was required (if neither FOLL_PIN | |
206 | * nor FOLL_GET was set, nothing is done). False for failure: FOLL_GET or | |
207 | * FOLL_PIN was set, but the page could not be grabbed. | |
208 | */ | |
209 | bool __must_check try_grab_page(struct page *page, unsigned int flags) | |
210 | { | |
c36c04c2 | 211 | WARN_ON_ONCE((flags & (FOLL_GET | FOLL_PIN)) == (FOLL_GET | FOLL_PIN)); |
3faa52c0 | 212 | |
c36c04c2 JH |
213 | if (flags & FOLL_GET) |
214 | return try_get_page(page); | |
215 | else if (flags & FOLL_PIN) { | |
216 | int refs = 1; | |
217 | ||
218 | page = compound_head(page); | |
219 | ||
220 | if (WARN_ON_ONCE(page_ref_count(page) <= 0)) | |
221 | return false; | |
222 | ||
223 | if (hpage_pincount_available(page)) | |
224 | hpage_pincount_add(page, 1); | |
225 | else | |
226 | refs = GUP_PIN_COUNTING_BIAS; | |
227 | ||
228 | /* | |
229 | * Similar to try_grab_compound_head(): even if using the | |
230 | * hpage_pincount_add/_sub() routines, be sure to | |
231 | * *also* increment the normal page refcount field at least | |
232 | * once, so that the page really is pinned. | |
233 | */ | |
234 | page_ref_add(page, refs); | |
235 | ||
236 | mod_node_page_state(page_pgdat(page), NR_FOLL_PIN_ACQUIRED, 1); | |
237 | } | |
238 | ||
239 | return true; | |
3faa52c0 JH |
240 | } |
241 | ||
3faa52c0 JH |
242 | /** |
243 | * unpin_user_page() - release a dma-pinned page | |
244 | * @page: pointer to page to be released | |
245 | * | |
246 | * Pages that were pinned via pin_user_pages*() must be released via either | |
247 | * unpin_user_page(), or one of the unpin_user_pages*() routines. This is so | |
248 | * that such pages can be separately tracked and uniquely handled. In | |
249 | * particular, interactions with RDMA and filesystems need special handling. | |
250 | */ | |
251 | void unpin_user_page(struct page *page) | |
252 | { | |
4509b42c | 253 | put_compound_head(compound_head(page), 1, FOLL_PIN); |
3faa52c0 JH |
254 | } |
255 | EXPORT_SYMBOL(unpin_user_page); | |
256 | ||
458a4f78 JM |
257 | static inline void compound_range_next(unsigned long i, unsigned long npages, |
258 | struct page **list, struct page **head, | |
259 | unsigned int *ntails) | |
260 | { | |
261 | struct page *next, *page; | |
262 | unsigned int nr = 1; | |
263 | ||
264 | if (i >= npages) | |
265 | return; | |
266 | ||
267 | next = *list + i; | |
268 | page = compound_head(next); | |
269 | if (PageCompound(page) && compound_order(page) >= 1) | |
270 | nr = min_t(unsigned int, | |
271 | page + compound_nr(page) - next, npages - i); | |
272 | ||
273 | *head = page; | |
274 | *ntails = nr; | |
275 | } | |
276 | ||
277 | #define for_each_compound_range(__i, __list, __npages, __head, __ntails) \ | |
278 | for (__i = 0, \ | |
279 | compound_range_next(__i, __npages, __list, &(__head), &(__ntails)); \ | |
280 | __i < __npages; __i += __ntails, \ | |
281 | compound_range_next(__i, __npages, __list, &(__head), &(__ntails))) | |
282 | ||
8745d7f6 JM |
283 | static inline void compound_next(unsigned long i, unsigned long npages, |
284 | struct page **list, struct page **head, | |
285 | unsigned int *ntails) | |
286 | { | |
287 | struct page *page; | |
288 | unsigned int nr; | |
289 | ||
290 | if (i >= npages) | |
291 | return; | |
292 | ||
293 | page = compound_head(list[i]); | |
294 | for (nr = i + 1; nr < npages; nr++) { | |
295 | if (compound_head(list[nr]) != page) | |
296 | break; | |
297 | } | |
298 | ||
299 | *head = page; | |
300 | *ntails = nr - i; | |
301 | } | |
302 | ||
303 | #define for_each_compound_head(__i, __list, __npages, __head, __ntails) \ | |
304 | for (__i = 0, \ | |
305 | compound_next(__i, __npages, __list, &(__head), &(__ntails)); \ | |
306 | __i < __npages; __i += __ntails, \ | |
307 | compound_next(__i, __npages, __list, &(__head), &(__ntails))) | |
308 | ||
fc1d8e7c | 309 | /** |
f1f6a7dd | 310 | * unpin_user_pages_dirty_lock() - release and optionally dirty gup-pinned pages |
2d15eb31 | 311 | * @pages: array of pages to be maybe marked dirty, and definitely released. |
fc1d8e7c | 312 | * @npages: number of pages in the @pages array. |
2d15eb31 | 313 | * @make_dirty: whether to mark the pages dirty |
fc1d8e7c JH |
314 | * |
315 | * "gup-pinned page" refers to a page that has had one of the get_user_pages() | |
316 | * variants called on that page. | |
317 | * | |
318 | * For each page in the @pages array, make that page (or its head page, if a | |
2d15eb31 | 319 | * compound page) dirty, if @make_dirty is true, and if the page was previously |
f1f6a7dd JH |
320 | * listed as clean. In any case, releases all pages using unpin_user_page(), |
321 | * possibly via unpin_user_pages(), for the non-dirty case. | |
fc1d8e7c | 322 | * |
f1f6a7dd | 323 | * Please see the unpin_user_page() documentation for details. |
fc1d8e7c | 324 | * |
2d15eb31 AM |
325 | * set_page_dirty_lock() is used internally. If instead, set_page_dirty() is |
326 | * required, then the caller should a) verify that this is really correct, | |
327 | * because _lock() is usually required, and b) hand code it: | |
f1f6a7dd | 328 | * set_page_dirty_lock(), unpin_user_page(). |
fc1d8e7c JH |
329 | * |
330 | */ | |
f1f6a7dd JH |
331 | void unpin_user_pages_dirty_lock(struct page **pages, unsigned long npages, |
332 | bool make_dirty) | |
fc1d8e7c | 333 | { |
2d15eb31 | 334 | unsigned long index; |
31b912de JM |
335 | struct page *head; |
336 | unsigned int ntails; | |
2d15eb31 AM |
337 | |
338 | if (!make_dirty) { | |
f1f6a7dd | 339 | unpin_user_pages(pages, npages); |
2d15eb31 AM |
340 | return; |
341 | } | |
342 | ||
31b912de | 343 | for_each_compound_head(index, pages, npages, head, ntails) { |
2d15eb31 AM |
344 | /* |
345 | * Checking PageDirty at this point may race with | |
346 | * clear_page_dirty_for_io(), but that's OK. Two key | |
347 | * cases: | |
348 | * | |
349 | * 1) This code sees the page as already dirty, so it | |
350 | * skips the call to set_page_dirty(). That could happen | |
351 | * because clear_page_dirty_for_io() called | |
352 | * page_mkclean(), followed by set_page_dirty(). | |
353 | * However, now the page is going to get written back, | |
354 | * which meets the original intention of setting it | |
355 | * dirty, so all is well: clear_page_dirty_for_io() goes | |
356 | * on to call TestClearPageDirty(), and write the page | |
357 | * back. | |
358 | * | |
359 | * 2) This code sees the page as clean, so it calls | |
360 | * set_page_dirty(). The page stays dirty, despite being | |
361 | * written back, so it gets written back again in the | |
362 | * next writeback cycle. This is harmless. | |
363 | */ | |
31b912de JM |
364 | if (!PageDirty(head)) |
365 | set_page_dirty_lock(head); | |
366 | put_compound_head(head, ntails, FOLL_PIN); | |
2d15eb31 | 367 | } |
fc1d8e7c | 368 | } |
f1f6a7dd | 369 | EXPORT_SYMBOL(unpin_user_pages_dirty_lock); |
fc1d8e7c | 370 | |
458a4f78 JM |
371 | /** |
372 | * unpin_user_page_range_dirty_lock() - release and optionally dirty | |
373 | * gup-pinned page range | |
374 | * | |
375 | * @page: the starting page of a range maybe marked dirty, and definitely released. | |
376 | * @npages: number of consecutive pages to release. | |
377 | * @make_dirty: whether to mark the pages dirty | |
378 | * | |
379 | * "gup-pinned page range" refers to a range of pages that has had one of the | |
380 | * pin_user_pages() variants called on that page. | |
381 | * | |
382 | * For the page ranges defined by [page .. page+npages], make that range (or | |
383 | * its head pages, if a compound page) dirty, if @make_dirty is true, and if the | |
384 | * page range was previously listed as clean. | |
385 | * | |
386 | * set_page_dirty_lock() is used internally. If instead, set_page_dirty() is | |
387 | * required, then the caller should a) verify that this is really correct, | |
388 | * because _lock() is usually required, and b) hand code it: | |
389 | * set_page_dirty_lock(), unpin_user_page(). | |
390 | * | |
391 | */ | |
392 | void unpin_user_page_range_dirty_lock(struct page *page, unsigned long npages, | |
393 | bool make_dirty) | |
394 | { | |
395 | unsigned long index; | |
396 | struct page *head; | |
397 | unsigned int ntails; | |
398 | ||
399 | for_each_compound_range(index, &page, npages, head, ntails) { | |
400 | if (make_dirty && !PageDirty(head)) | |
401 | set_page_dirty_lock(head); | |
402 | put_compound_head(head, ntails, FOLL_PIN); | |
403 | } | |
404 | } | |
405 | EXPORT_SYMBOL(unpin_user_page_range_dirty_lock); | |
406 | ||
fc1d8e7c | 407 | /** |
f1f6a7dd | 408 | * unpin_user_pages() - release an array of gup-pinned pages. |
fc1d8e7c JH |
409 | * @pages: array of pages to be marked dirty and released. |
410 | * @npages: number of pages in the @pages array. | |
411 | * | |
f1f6a7dd | 412 | * For each page in the @pages array, release the page using unpin_user_page(). |
fc1d8e7c | 413 | * |
f1f6a7dd | 414 | * Please see the unpin_user_page() documentation for details. |
fc1d8e7c | 415 | */ |
f1f6a7dd | 416 | void unpin_user_pages(struct page **pages, unsigned long npages) |
fc1d8e7c JH |
417 | { |
418 | unsigned long index; | |
31b912de JM |
419 | struct page *head; |
420 | unsigned int ntails; | |
fc1d8e7c | 421 | |
146608bb JH |
422 | /* |
423 | * If this WARN_ON() fires, then the system *might* be leaking pages (by | |
424 | * leaving them pinned), but probably not. More likely, gup/pup returned | |
425 | * a hard -ERRNO error to the caller, who erroneously passed it here. | |
426 | */ | |
427 | if (WARN_ON(IS_ERR_VALUE(npages))) | |
428 | return; | |
31b912de JM |
429 | |
430 | for_each_compound_head(index, pages, npages, head, ntails) | |
431 | put_compound_head(head, ntails, FOLL_PIN); | |
fc1d8e7c | 432 | } |
f1f6a7dd | 433 | EXPORT_SYMBOL(unpin_user_pages); |
fc1d8e7c | 434 | |
a458b76a AA |
435 | /* |
436 | * Set the MMF_HAS_PINNED if not set yet; after set it'll be there for the mm's | |
437 | * lifecycle. Avoid setting the bit unless necessary, or it might cause write | |
438 | * cache bouncing on large SMP machines for concurrent pinned gups. | |
439 | */ | |
440 | static inline void mm_set_has_pinned_flag(unsigned long *mm_flags) | |
441 | { | |
442 | if (!test_bit(MMF_HAS_PINNED, mm_flags)) | |
443 | set_bit(MMF_HAS_PINNED, mm_flags); | |
444 | } | |
445 | ||
050a9adc | 446 | #ifdef CONFIG_MMU |
69e68b4f KS |
447 | static struct page *no_page_table(struct vm_area_struct *vma, |
448 | unsigned int flags) | |
4bbd4c77 | 449 | { |
69e68b4f KS |
450 | /* |
451 | * When core dumping an enormous anonymous area that nobody | |
452 | * has touched so far, we don't want to allocate unnecessary pages or | |
453 | * page tables. Return error instead of NULL to skip handle_mm_fault, | |
454 | * then get_dump_page() will return NULL to leave a hole in the dump. | |
455 | * But we can only make this optimization where a hole would surely | |
456 | * be zero-filled if handle_mm_fault() actually did handle it. | |
457 | */ | |
a0137f16 AK |
458 | if ((flags & FOLL_DUMP) && |
459 | (vma_is_anonymous(vma) || !vma->vm_ops->fault)) | |
69e68b4f KS |
460 | return ERR_PTR(-EFAULT); |
461 | return NULL; | |
462 | } | |
4bbd4c77 | 463 | |
1027e443 KS |
464 | static int follow_pfn_pte(struct vm_area_struct *vma, unsigned long address, |
465 | pte_t *pte, unsigned int flags) | |
466 | { | |
467 | /* No page to get reference */ | |
468 | if (flags & FOLL_GET) | |
469 | return -EFAULT; | |
470 | ||
471 | if (flags & FOLL_TOUCH) { | |
472 | pte_t entry = *pte; | |
473 | ||
474 | if (flags & FOLL_WRITE) | |
475 | entry = pte_mkdirty(entry); | |
476 | entry = pte_mkyoung(entry); | |
477 | ||
478 | if (!pte_same(*pte, entry)) { | |
479 | set_pte_at(vma->vm_mm, address, pte, entry); | |
480 | update_mmu_cache(vma, address, pte); | |
481 | } | |
482 | } | |
483 | ||
484 | /* Proper page table entry exists, but no corresponding struct page */ | |
485 | return -EEXIST; | |
486 | } | |
487 | ||
19be0eaf | 488 | /* |
a308c71b PX |
489 | * FOLL_FORCE can write to even unwritable pte's, but only |
490 | * after we've gone through a COW cycle and they are dirty. | |
19be0eaf LT |
491 | */ |
492 | static inline bool can_follow_write_pte(pte_t pte, unsigned int flags) | |
493 | { | |
a308c71b PX |
494 | return pte_write(pte) || |
495 | ((flags & FOLL_FORCE) && (flags & FOLL_COW) && pte_dirty(pte)); | |
19be0eaf LT |
496 | } |
497 | ||
69e68b4f | 498 | static struct page *follow_page_pte(struct vm_area_struct *vma, |
df06b37f KB |
499 | unsigned long address, pmd_t *pmd, unsigned int flags, |
500 | struct dev_pagemap **pgmap) | |
69e68b4f KS |
501 | { |
502 | struct mm_struct *mm = vma->vm_mm; | |
503 | struct page *page; | |
504 | spinlock_t *ptl; | |
505 | pte_t *ptep, pte; | |
f28d4363 | 506 | int ret; |
4bbd4c77 | 507 | |
eddb1c22 JH |
508 | /* FOLL_GET and FOLL_PIN are mutually exclusive. */ |
509 | if (WARN_ON_ONCE((flags & (FOLL_PIN | FOLL_GET)) == | |
510 | (FOLL_PIN | FOLL_GET))) | |
511 | return ERR_PTR(-EINVAL); | |
69e68b4f | 512 | retry: |
4bbd4c77 | 513 | if (unlikely(pmd_bad(*pmd))) |
69e68b4f | 514 | return no_page_table(vma, flags); |
4bbd4c77 KS |
515 | |
516 | ptep = pte_offset_map_lock(mm, pmd, address, &ptl); | |
4bbd4c77 KS |
517 | pte = *ptep; |
518 | if (!pte_present(pte)) { | |
519 | swp_entry_t entry; | |
520 | /* | |
521 | * KSM's break_ksm() relies upon recognizing a ksm page | |
522 | * even while it is being migrated, so for that case we | |
523 | * need migration_entry_wait(). | |
524 | */ | |
525 | if (likely(!(flags & FOLL_MIGRATION))) | |
526 | goto no_page; | |
0661a336 | 527 | if (pte_none(pte)) |
4bbd4c77 KS |
528 | goto no_page; |
529 | entry = pte_to_swp_entry(pte); | |
530 | if (!is_migration_entry(entry)) | |
531 | goto no_page; | |
532 | pte_unmap_unlock(ptep, ptl); | |
533 | migration_entry_wait(mm, pmd, address); | |
69e68b4f | 534 | goto retry; |
4bbd4c77 | 535 | } |
8a0516ed | 536 | if ((flags & FOLL_NUMA) && pte_protnone(pte)) |
4bbd4c77 | 537 | goto no_page; |
19be0eaf | 538 | if ((flags & FOLL_WRITE) && !can_follow_write_pte(pte, flags)) { |
69e68b4f KS |
539 | pte_unmap_unlock(ptep, ptl); |
540 | return NULL; | |
541 | } | |
4bbd4c77 KS |
542 | |
543 | page = vm_normal_page(vma, address, pte); | |
3faa52c0 | 544 | if (!page && pte_devmap(pte) && (flags & (FOLL_GET | FOLL_PIN))) { |
3565fce3 | 545 | /* |
3faa52c0 JH |
546 | * Only return device mapping pages in the FOLL_GET or FOLL_PIN |
547 | * case since they are only valid while holding the pgmap | |
548 | * reference. | |
3565fce3 | 549 | */ |
df06b37f KB |
550 | *pgmap = get_dev_pagemap(pte_pfn(pte), *pgmap); |
551 | if (*pgmap) | |
3565fce3 DW |
552 | page = pte_page(pte); |
553 | else | |
554 | goto no_page; | |
555 | } else if (unlikely(!page)) { | |
1027e443 KS |
556 | if (flags & FOLL_DUMP) { |
557 | /* Avoid special (like zero) pages in core dumps */ | |
558 | page = ERR_PTR(-EFAULT); | |
559 | goto out; | |
560 | } | |
561 | ||
562 | if (is_zero_pfn(pte_pfn(pte))) { | |
563 | page = pte_page(pte); | |
564 | } else { | |
1027e443 KS |
565 | ret = follow_pfn_pte(vma, address, ptep, flags); |
566 | page = ERR_PTR(ret); | |
567 | goto out; | |
568 | } | |
4bbd4c77 KS |
569 | } |
570 | ||
3faa52c0 JH |
571 | /* try_grab_page() does nothing unless FOLL_GET or FOLL_PIN is set. */ |
572 | if (unlikely(!try_grab_page(page, flags))) { | |
573 | page = ERR_PTR(-ENOMEM); | |
574 | goto out; | |
8fde12ca | 575 | } |
f28d4363 CI |
576 | /* |
577 | * We need to make the page accessible if and only if we are going | |
578 | * to access its content (the FOLL_PIN case). Please see | |
579 | * Documentation/core-api/pin_user_pages.rst for details. | |
580 | */ | |
581 | if (flags & FOLL_PIN) { | |
582 | ret = arch_make_page_accessible(page); | |
583 | if (ret) { | |
584 | unpin_user_page(page); | |
585 | page = ERR_PTR(ret); | |
586 | goto out; | |
587 | } | |
588 | } | |
4bbd4c77 KS |
589 | if (flags & FOLL_TOUCH) { |
590 | if ((flags & FOLL_WRITE) && | |
591 | !pte_dirty(pte) && !PageDirty(page)) | |
592 | set_page_dirty(page); | |
593 | /* | |
594 | * pte_mkyoung() would be more correct here, but atomic care | |
595 | * is needed to avoid losing the dirty bit: it is easier to use | |
596 | * mark_page_accessed(). | |
597 | */ | |
598 | mark_page_accessed(page); | |
599 | } | |
de60f5f1 | 600 | if ((flags & FOLL_MLOCK) && (vma->vm_flags & VM_LOCKED)) { |
e90309c9 KS |
601 | /* Do not mlock pte-mapped THP */ |
602 | if (PageTransCompound(page)) | |
603 | goto out; | |
604 | ||
4bbd4c77 KS |
605 | /* |
606 | * The preliminary mapping check is mainly to avoid the | |
607 | * pointless overhead of lock_page on the ZERO_PAGE | |
608 | * which might bounce very badly if there is contention. | |
609 | * | |
610 | * If the page is already locked, we don't need to | |
611 | * handle it now - vmscan will handle it later if and | |
612 | * when it attempts to reclaim the page. | |
613 | */ | |
614 | if (page->mapping && trylock_page(page)) { | |
615 | lru_add_drain(); /* push cached pages to LRU */ | |
616 | /* | |
617 | * Because we lock page here, and migration is | |
618 | * blocked by the pte's page reference, and we | |
619 | * know the page is still mapped, we don't even | |
620 | * need to check for file-cache page truncation. | |
621 | */ | |
622 | mlock_vma_page(page); | |
623 | unlock_page(page); | |
624 | } | |
625 | } | |
1027e443 | 626 | out: |
4bbd4c77 | 627 | pte_unmap_unlock(ptep, ptl); |
4bbd4c77 | 628 | return page; |
4bbd4c77 KS |
629 | no_page: |
630 | pte_unmap_unlock(ptep, ptl); | |
631 | if (!pte_none(pte)) | |
69e68b4f KS |
632 | return NULL; |
633 | return no_page_table(vma, flags); | |
634 | } | |
635 | ||
080dbb61 AK |
636 | static struct page *follow_pmd_mask(struct vm_area_struct *vma, |
637 | unsigned long address, pud_t *pudp, | |
df06b37f KB |
638 | unsigned int flags, |
639 | struct follow_page_context *ctx) | |
69e68b4f | 640 | { |
68827280 | 641 | pmd_t *pmd, pmdval; |
69e68b4f KS |
642 | spinlock_t *ptl; |
643 | struct page *page; | |
644 | struct mm_struct *mm = vma->vm_mm; | |
645 | ||
080dbb61 | 646 | pmd = pmd_offset(pudp, address); |
68827280 YH |
647 | /* |
648 | * The READ_ONCE() will stabilize the pmdval in a register or | |
649 | * on the stack so that it will stop changing under the code. | |
650 | */ | |
651 | pmdval = READ_ONCE(*pmd); | |
652 | if (pmd_none(pmdval)) | |
69e68b4f | 653 | return no_page_table(vma, flags); |
be9d3045 | 654 | if (pmd_huge(pmdval) && is_vm_hugetlb_page(vma)) { |
e66f17ff NH |
655 | page = follow_huge_pmd(mm, address, pmd, flags); |
656 | if (page) | |
657 | return page; | |
658 | return no_page_table(vma, flags); | |
69e68b4f | 659 | } |
68827280 | 660 | if (is_hugepd(__hugepd(pmd_val(pmdval)))) { |
4dc71451 | 661 | page = follow_huge_pd(vma, address, |
68827280 | 662 | __hugepd(pmd_val(pmdval)), flags, |
4dc71451 AK |
663 | PMD_SHIFT); |
664 | if (page) | |
665 | return page; | |
666 | return no_page_table(vma, flags); | |
667 | } | |
84c3fc4e | 668 | retry: |
68827280 | 669 | if (!pmd_present(pmdval)) { |
28b0ee3f LX |
670 | /* |
671 | * Should never reach here, if thp migration is not supported; | |
672 | * Otherwise, it must be a thp migration entry. | |
673 | */ | |
674 | VM_BUG_ON(!thp_migration_supported() || | |
675 | !is_pmd_migration_entry(pmdval)); | |
676 | ||
84c3fc4e ZY |
677 | if (likely(!(flags & FOLL_MIGRATION))) |
678 | return no_page_table(vma, flags); | |
28b0ee3f LX |
679 | |
680 | pmd_migration_entry_wait(mm, pmd); | |
68827280 YH |
681 | pmdval = READ_ONCE(*pmd); |
682 | /* | |
683 | * MADV_DONTNEED may convert the pmd to null because | |
c1e8d7c6 | 684 | * mmap_lock is held in read mode |
68827280 YH |
685 | */ |
686 | if (pmd_none(pmdval)) | |
687 | return no_page_table(vma, flags); | |
84c3fc4e ZY |
688 | goto retry; |
689 | } | |
68827280 | 690 | if (pmd_devmap(pmdval)) { |
3565fce3 | 691 | ptl = pmd_lock(mm, pmd); |
df06b37f | 692 | page = follow_devmap_pmd(vma, address, pmd, flags, &ctx->pgmap); |
3565fce3 DW |
693 | spin_unlock(ptl); |
694 | if (page) | |
695 | return page; | |
696 | } | |
68827280 | 697 | if (likely(!pmd_trans_huge(pmdval))) |
df06b37f | 698 | return follow_page_pte(vma, address, pmd, flags, &ctx->pgmap); |
6742d293 | 699 | |
68827280 | 700 | if ((flags & FOLL_NUMA) && pmd_protnone(pmdval)) |
db08f203 AK |
701 | return no_page_table(vma, flags); |
702 | ||
84c3fc4e | 703 | retry_locked: |
6742d293 | 704 | ptl = pmd_lock(mm, pmd); |
68827280 YH |
705 | if (unlikely(pmd_none(*pmd))) { |
706 | spin_unlock(ptl); | |
707 | return no_page_table(vma, flags); | |
708 | } | |
84c3fc4e ZY |
709 | if (unlikely(!pmd_present(*pmd))) { |
710 | spin_unlock(ptl); | |
711 | if (likely(!(flags & FOLL_MIGRATION))) | |
712 | return no_page_table(vma, flags); | |
713 | pmd_migration_entry_wait(mm, pmd); | |
714 | goto retry_locked; | |
715 | } | |
6742d293 KS |
716 | if (unlikely(!pmd_trans_huge(*pmd))) { |
717 | spin_unlock(ptl); | |
df06b37f | 718 | return follow_page_pte(vma, address, pmd, flags, &ctx->pgmap); |
6742d293 | 719 | } |
4066c119 | 720 | if (flags & FOLL_SPLIT_PMD) { |
6742d293 KS |
721 | int ret; |
722 | page = pmd_page(*pmd); | |
723 | if (is_huge_zero_page(page)) { | |
724 | spin_unlock(ptl); | |
725 | ret = 0; | |
78ddc534 | 726 | split_huge_pmd(vma, pmd, address); |
337d9abf NH |
727 | if (pmd_trans_unstable(pmd)) |
728 | ret = -EBUSY; | |
4066c119 | 729 | } else { |
bfe7b00d SL |
730 | spin_unlock(ptl); |
731 | split_huge_pmd(vma, pmd, address); | |
732 | ret = pte_alloc(mm, pmd) ? -ENOMEM : 0; | |
6742d293 KS |
733 | } |
734 | ||
735 | return ret ? ERR_PTR(ret) : | |
df06b37f | 736 | follow_page_pte(vma, address, pmd, flags, &ctx->pgmap); |
69e68b4f | 737 | } |
6742d293 KS |
738 | page = follow_trans_huge_pmd(vma, address, pmd, flags); |
739 | spin_unlock(ptl); | |
df06b37f | 740 | ctx->page_mask = HPAGE_PMD_NR - 1; |
6742d293 | 741 | return page; |
4bbd4c77 KS |
742 | } |
743 | ||
080dbb61 AK |
744 | static struct page *follow_pud_mask(struct vm_area_struct *vma, |
745 | unsigned long address, p4d_t *p4dp, | |
df06b37f KB |
746 | unsigned int flags, |
747 | struct follow_page_context *ctx) | |
080dbb61 AK |
748 | { |
749 | pud_t *pud; | |
750 | spinlock_t *ptl; | |
751 | struct page *page; | |
752 | struct mm_struct *mm = vma->vm_mm; | |
753 | ||
754 | pud = pud_offset(p4dp, address); | |
755 | if (pud_none(*pud)) | |
756 | return no_page_table(vma, flags); | |
be9d3045 | 757 | if (pud_huge(*pud) && is_vm_hugetlb_page(vma)) { |
080dbb61 AK |
758 | page = follow_huge_pud(mm, address, pud, flags); |
759 | if (page) | |
760 | return page; | |
761 | return no_page_table(vma, flags); | |
762 | } | |
4dc71451 AK |
763 | if (is_hugepd(__hugepd(pud_val(*pud)))) { |
764 | page = follow_huge_pd(vma, address, | |
765 | __hugepd(pud_val(*pud)), flags, | |
766 | PUD_SHIFT); | |
767 | if (page) | |
768 | return page; | |
769 | return no_page_table(vma, flags); | |
770 | } | |
080dbb61 AK |
771 | if (pud_devmap(*pud)) { |
772 | ptl = pud_lock(mm, pud); | |
df06b37f | 773 | page = follow_devmap_pud(vma, address, pud, flags, &ctx->pgmap); |
080dbb61 AK |
774 | spin_unlock(ptl); |
775 | if (page) | |
776 | return page; | |
777 | } | |
778 | if (unlikely(pud_bad(*pud))) | |
779 | return no_page_table(vma, flags); | |
780 | ||
df06b37f | 781 | return follow_pmd_mask(vma, address, pud, flags, ctx); |
080dbb61 AK |
782 | } |
783 | ||
080dbb61 AK |
784 | static struct page *follow_p4d_mask(struct vm_area_struct *vma, |
785 | unsigned long address, pgd_t *pgdp, | |
df06b37f KB |
786 | unsigned int flags, |
787 | struct follow_page_context *ctx) | |
080dbb61 AK |
788 | { |
789 | p4d_t *p4d; | |
4dc71451 | 790 | struct page *page; |
080dbb61 AK |
791 | |
792 | p4d = p4d_offset(pgdp, address); | |
793 | if (p4d_none(*p4d)) | |
794 | return no_page_table(vma, flags); | |
795 | BUILD_BUG_ON(p4d_huge(*p4d)); | |
796 | if (unlikely(p4d_bad(*p4d))) | |
797 | return no_page_table(vma, flags); | |
798 | ||
4dc71451 AK |
799 | if (is_hugepd(__hugepd(p4d_val(*p4d)))) { |
800 | page = follow_huge_pd(vma, address, | |
801 | __hugepd(p4d_val(*p4d)), flags, | |
802 | P4D_SHIFT); | |
803 | if (page) | |
804 | return page; | |
805 | return no_page_table(vma, flags); | |
806 | } | |
df06b37f | 807 | return follow_pud_mask(vma, address, p4d, flags, ctx); |
080dbb61 AK |
808 | } |
809 | ||
810 | /** | |
811 | * follow_page_mask - look up a page descriptor from a user-virtual address | |
812 | * @vma: vm_area_struct mapping @address | |
813 | * @address: virtual address to look up | |
814 | * @flags: flags modifying lookup behaviour | |
78179556 MR |
815 | * @ctx: contains dev_pagemap for %ZONE_DEVICE memory pinning and a |
816 | * pointer to output page_mask | |
080dbb61 AK |
817 | * |
818 | * @flags can have FOLL_ flags set, defined in <linux/mm.h> | |
819 | * | |
78179556 MR |
820 | * When getting pages from ZONE_DEVICE memory, the @ctx->pgmap caches |
821 | * the device's dev_pagemap metadata to avoid repeating expensive lookups. | |
822 | * | |
823 | * On output, the @ctx->page_mask is set according to the size of the page. | |
824 | * | |
825 | * Return: the mapped (struct page *), %NULL if no mapping exists, or | |
080dbb61 AK |
826 | * an error pointer if there is a mapping to something not represented |
827 | * by a page descriptor (see also vm_normal_page()). | |
828 | */ | |
a7030aea | 829 | static struct page *follow_page_mask(struct vm_area_struct *vma, |
080dbb61 | 830 | unsigned long address, unsigned int flags, |
df06b37f | 831 | struct follow_page_context *ctx) |
080dbb61 AK |
832 | { |
833 | pgd_t *pgd; | |
834 | struct page *page; | |
835 | struct mm_struct *mm = vma->vm_mm; | |
836 | ||
df06b37f | 837 | ctx->page_mask = 0; |
080dbb61 AK |
838 | |
839 | /* make this handle hugepd */ | |
840 | page = follow_huge_addr(mm, address, flags & FOLL_WRITE); | |
841 | if (!IS_ERR(page)) { | |
3faa52c0 | 842 | WARN_ON_ONCE(flags & (FOLL_GET | FOLL_PIN)); |
080dbb61 AK |
843 | return page; |
844 | } | |
845 | ||
846 | pgd = pgd_offset(mm, address); | |
847 | ||
848 | if (pgd_none(*pgd) || unlikely(pgd_bad(*pgd))) | |
849 | return no_page_table(vma, flags); | |
850 | ||
faaa5b62 AK |
851 | if (pgd_huge(*pgd)) { |
852 | page = follow_huge_pgd(mm, address, pgd, flags); | |
853 | if (page) | |
854 | return page; | |
855 | return no_page_table(vma, flags); | |
856 | } | |
4dc71451 AK |
857 | if (is_hugepd(__hugepd(pgd_val(*pgd)))) { |
858 | page = follow_huge_pd(vma, address, | |
859 | __hugepd(pgd_val(*pgd)), flags, | |
860 | PGDIR_SHIFT); | |
861 | if (page) | |
862 | return page; | |
863 | return no_page_table(vma, flags); | |
864 | } | |
faaa5b62 | 865 | |
df06b37f KB |
866 | return follow_p4d_mask(vma, address, pgd, flags, ctx); |
867 | } | |
868 | ||
869 | struct page *follow_page(struct vm_area_struct *vma, unsigned long address, | |
870 | unsigned int foll_flags) | |
871 | { | |
872 | struct follow_page_context ctx = { NULL }; | |
873 | struct page *page; | |
874 | ||
1507f512 MR |
875 | if (vma_is_secretmem(vma)) |
876 | return NULL; | |
877 | ||
df06b37f KB |
878 | page = follow_page_mask(vma, address, foll_flags, &ctx); |
879 | if (ctx.pgmap) | |
880 | put_dev_pagemap(ctx.pgmap); | |
881 | return page; | |
080dbb61 AK |
882 | } |
883 | ||
f2b495ca KS |
884 | static int get_gate_page(struct mm_struct *mm, unsigned long address, |
885 | unsigned int gup_flags, struct vm_area_struct **vma, | |
886 | struct page **page) | |
887 | { | |
888 | pgd_t *pgd; | |
c2febafc | 889 | p4d_t *p4d; |
f2b495ca KS |
890 | pud_t *pud; |
891 | pmd_t *pmd; | |
892 | pte_t *pte; | |
893 | int ret = -EFAULT; | |
894 | ||
895 | /* user gate pages are read-only */ | |
896 | if (gup_flags & FOLL_WRITE) | |
897 | return -EFAULT; | |
898 | if (address > TASK_SIZE) | |
899 | pgd = pgd_offset_k(address); | |
900 | else | |
901 | pgd = pgd_offset_gate(mm, address); | |
b5d1c39f AL |
902 | if (pgd_none(*pgd)) |
903 | return -EFAULT; | |
c2febafc | 904 | p4d = p4d_offset(pgd, address); |
b5d1c39f AL |
905 | if (p4d_none(*p4d)) |
906 | return -EFAULT; | |
c2febafc | 907 | pud = pud_offset(p4d, address); |
b5d1c39f AL |
908 | if (pud_none(*pud)) |
909 | return -EFAULT; | |
f2b495ca | 910 | pmd = pmd_offset(pud, address); |
84c3fc4e | 911 | if (!pmd_present(*pmd)) |
f2b495ca KS |
912 | return -EFAULT; |
913 | VM_BUG_ON(pmd_trans_huge(*pmd)); | |
914 | pte = pte_offset_map(pmd, address); | |
915 | if (pte_none(*pte)) | |
916 | goto unmap; | |
917 | *vma = get_gate_vma(mm); | |
918 | if (!page) | |
919 | goto out; | |
920 | *page = vm_normal_page(*vma, address, *pte); | |
921 | if (!*page) { | |
922 | if ((gup_flags & FOLL_DUMP) || !is_zero_pfn(pte_pfn(*pte))) | |
923 | goto unmap; | |
924 | *page = pte_page(*pte); | |
925 | } | |
9fa2dd94 | 926 | if (unlikely(!try_grab_page(*page, gup_flags))) { |
8fde12ca LT |
927 | ret = -ENOMEM; |
928 | goto unmap; | |
929 | } | |
f2b495ca KS |
930 | out: |
931 | ret = 0; | |
932 | unmap: | |
933 | pte_unmap(pte); | |
934 | return ret; | |
935 | } | |
936 | ||
9a95f3cf | 937 | /* |
c1e8d7c6 ML |
938 | * mmap_lock must be held on entry. If @locked != NULL and *@flags |
939 | * does not include FOLL_NOWAIT, the mmap_lock may be released. If it | |
4f6da934 | 940 | * is, *@locked will be set to 0 and -EBUSY returned. |
9a95f3cf | 941 | */ |
64019a2e | 942 | static int faultin_page(struct vm_area_struct *vma, |
4f6da934 | 943 | unsigned long address, unsigned int *flags, int *locked) |
16744483 | 944 | { |
16744483 | 945 | unsigned int fault_flags = 0; |
2b740303 | 946 | vm_fault_t ret; |
16744483 | 947 | |
de60f5f1 EM |
948 | /* mlock all present pages, but do not fault in new pages */ |
949 | if ((*flags & (FOLL_POPULATE | FOLL_MLOCK)) == FOLL_MLOCK) | |
950 | return -ENOENT; | |
55b8fe70 AG |
951 | if (*flags & FOLL_NOFAULT) |
952 | return -EFAULT; | |
16744483 KS |
953 | if (*flags & FOLL_WRITE) |
954 | fault_flags |= FAULT_FLAG_WRITE; | |
1b2ee126 DH |
955 | if (*flags & FOLL_REMOTE) |
956 | fault_flags |= FAULT_FLAG_REMOTE; | |
4f6da934 | 957 | if (locked) |
71335f37 | 958 | fault_flags |= FAULT_FLAG_ALLOW_RETRY | FAULT_FLAG_KILLABLE; |
16744483 KS |
959 | if (*flags & FOLL_NOWAIT) |
960 | fault_flags |= FAULT_FLAG_ALLOW_RETRY | FAULT_FLAG_RETRY_NOWAIT; | |
234b239b | 961 | if (*flags & FOLL_TRIED) { |
4426e945 PX |
962 | /* |
963 | * Note: FAULT_FLAG_ALLOW_RETRY and FAULT_FLAG_TRIED | |
964 | * can co-exist | |
965 | */ | |
234b239b ALC |
966 | fault_flags |= FAULT_FLAG_TRIED; |
967 | } | |
16744483 | 968 | |
bce617ed | 969 | ret = handle_mm_fault(vma, address, fault_flags, NULL); |
16744483 | 970 | if (ret & VM_FAULT_ERROR) { |
9a291a7c JM |
971 | int err = vm_fault_to_errno(ret, *flags); |
972 | ||
973 | if (err) | |
974 | return err; | |
16744483 KS |
975 | BUG(); |
976 | } | |
977 | ||
16744483 | 978 | if (ret & VM_FAULT_RETRY) { |
4f6da934 PX |
979 | if (locked && !(fault_flags & FAULT_FLAG_RETRY_NOWAIT)) |
980 | *locked = 0; | |
16744483 KS |
981 | return -EBUSY; |
982 | } | |
983 | ||
984 | /* | |
985 | * The VM_FAULT_WRITE bit tells us that do_wp_page has broken COW when | |
986 | * necessary, even if maybe_mkwrite decided not to set pte_write. We | |
987 | * can thus safely do subsequent page lookups as if they were reads. | |
988 | * But only do so when looping for pte_write is futile: in some cases | |
989 | * userspace may also be wanting to write to the gotten user page, | |
990 | * which a read fault here might prevent (a readonly page might get | |
991 | * reCOWed by userspace write). | |
992 | */ | |
993 | if ((ret & VM_FAULT_WRITE) && !(vma->vm_flags & VM_WRITE)) | |
2923117b | 994 | *flags |= FOLL_COW; |
16744483 KS |
995 | return 0; |
996 | } | |
997 | ||
fa5bb209 KS |
998 | static int check_vma_flags(struct vm_area_struct *vma, unsigned long gup_flags) |
999 | { | |
1000 | vm_flags_t vm_flags = vma->vm_flags; | |
1b2ee126 DH |
1001 | int write = (gup_flags & FOLL_WRITE); |
1002 | int foreign = (gup_flags & FOLL_REMOTE); | |
fa5bb209 KS |
1003 | |
1004 | if (vm_flags & (VM_IO | VM_PFNMAP)) | |
1005 | return -EFAULT; | |
1006 | ||
7f7ccc2c WT |
1007 | if (gup_flags & FOLL_ANON && !vma_is_anonymous(vma)) |
1008 | return -EFAULT; | |
1009 | ||
52650c8b JG |
1010 | if ((gup_flags & FOLL_LONGTERM) && vma_is_fsdax(vma)) |
1011 | return -EOPNOTSUPP; | |
1012 | ||
1507f512 MR |
1013 | if (vma_is_secretmem(vma)) |
1014 | return -EFAULT; | |
1015 | ||
1b2ee126 | 1016 | if (write) { |
fa5bb209 KS |
1017 | if (!(vm_flags & VM_WRITE)) { |
1018 | if (!(gup_flags & FOLL_FORCE)) | |
1019 | return -EFAULT; | |
1020 | /* | |
1021 | * We used to let the write,force case do COW in a | |
1022 | * VM_MAYWRITE VM_SHARED !VM_WRITE vma, so ptrace could | |
1023 | * set a breakpoint in a read-only mapping of an | |
1024 | * executable, without corrupting the file (yet only | |
1025 | * when that file had been opened for writing!). | |
1026 | * Anon pages in shared mappings are surprising: now | |
1027 | * just reject it. | |
1028 | */ | |
46435364 | 1029 | if (!is_cow_mapping(vm_flags)) |
fa5bb209 | 1030 | return -EFAULT; |
fa5bb209 KS |
1031 | } |
1032 | } else if (!(vm_flags & VM_READ)) { | |
1033 | if (!(gup_flags & FOLL_FORCE)) | |
1034 | return -EFAULT; | |
1035 | /* | |
1036 | * Is there actually any vma we can reach here which does not | |
1037 | * have VM_MAYREAD set? | |
1038 | */ | |
1039 | if (!(vm_flags & VM_MAYREAD)) | |
1040 | return -EFAULT; | |
1041 | } | |
d61172b4 DH |
1042 | /* |
1043 | * gups are always data accesses, not instruction | |
1044 | * fetches, so execute=false here | |
1045 | */ | |
1046 | if (!arch_vma_access_permitted(vma, write, false, foreign)) | |
33a709b2 | 1047 | return -EFAULT; |
fa5bb209 KS |
1048 | return 0; |
1049 | } | |
1050 | ||
4bbd4c77 KS |
1051 | /** |
1052 | * __get_user_pages() - pin user pages in memory | |
4bbd4c77 KS |
1053 | * @mm: mm_struct of target mm |
1054 | * @start: starting user address | |
1055 | * @nr_pages: number of pages from start to pin | |
1056 | * @gup_flags: flags modifying pin behaviour | |
1057 | * @pages: array that receives pointers to the pages pinned. | |
1058 | * Should be at least nr_pages long. Or NULL, if caller | |
1059 | * only intends to ensure the pages are faulted in. | |
1060 | * @vmas: array of pointers to vmas corresponding to each page. | |
1061 | * Or NULL if the caller does not require them. | |
c1e8d7c6 | 1062 | * @locked: whether we're still with the mmap_lock held |
4bbd4c77 | 1063 | * |
d2dfbe47 LX |
1064 | * Returns either number of pages pinned (which may be less than the |
1065 | * number requested), or an error. Details about the return value: | |
1066 | * | |
1067 | * -- If nr_pages is 0, returns 0. | |
1068 | * -- If nr_pages is >0, but no pages were pinned, returns -errno. | |
1069 | * -- If nr_pages is >0, and some pages were pinned, returns the number of | |
1070 | * pages pinned. Again, this may be less than nr_pages. | |
2d3a36a4 | 1071 | * -- 0 return value is possible when the fault would need to be retried. |
d2dfbe47 LX |
1072 | * |
1073 | * The caller is responsible for releasing returned @pages, via put_page(). | |
1074 | * | |
c1e8d7c6 | 1075 | * @vmas are valid only as long as mmap_lock is held. |
4bbd4c77 | 1076 | * |
c1e8d7c6 | 1077 | * Must be called with mmap_lock held. It may be released. See below. |
4bbd4c77 KS |
1078 | * |
1079 | * __get_user_pages walks a process's page tables and takes a reference to | |
1080 | * each struct page that each user address corresponds to at a given | |
1081 | * instant. That is, it takes the page that would be accessed if a user | |
1082 | * thread accesses the given user virtual address at that instant. | |
1083 | * | |
1084 | * This does not guarantee that the page exists in the user mappings when | |
1085 | * __get_user_pages returns, and there may even be a completely different | |
1086 | * page there in some cases (eg. if mmapped pagecache has been invalidated | |
1087 | * and subsequently re faulted). However it does guarantee that the page | |
1088 | * won't be freed completely. And mostly callers simply care that the page | |
1089 | * contains data that was valid *at some point in time*. Typically, an IO | |
1090 | * or similar operation cannot guarantee anything stronger anyway because | |
1091 | * locks can't be held over the syscall boundary. | |
1092 | * | |
1093 | * If @gup_flags & FOLL_WRITE == 0, the page must not be written to. If | |
1094 | * the page is written to, set_page_dirty (or set_page_dirty_lock, as | |
1095 | * appropriate) must be called after the page is finished with, and | |
1096 | * before put_page is called. | |
1097 | * | |
c1e8d7c6 | 1098 | * If @locked != NULL, *@locked will be set to 0 when mmap_lock is |
4f6da934 PX |
1099 | * released by an up_read(). That can happen if @gup_flags does not |
1100 | * have FOLL_NOWAIT. | |
9a95f3cf | 1101 | * |
4f6da934 | 1102 | * A caller using such a combination of @locked and @gup_flags |
c1e8d7c6 | 1103 | * must therefore hold the mmap_lock for reading only, and recognize |
9a95f3cf PC |
1104 | * when it's been released. Otherwise, it must be held for either |
1105 | * reading or writing and will not be released. | |
4bbd4c77 KS |
1106 | * |
1107 | * In most cases, get_user_pages or get_user_pages_fast should be used | |
1108 | * instead of __get_user_pages. __get_user_pages should be used only if | |
1109 | * you need some special @gup_flags. | |
1110 | */ | |
64019a2e | 1111 | static long __get_user_pages(struct mm_struct *mm, |
4bbd4c77 KS |
1112 | unsigned long start, unsigned long nr_pages, |
1113 | unsigned int gup_flags, struct page **pages, | |
4f6da934 | 1114 | struct vm_area_struct **vmas, int *locked) |
4bbd4c77 | 1115 | { |
df06b37f | 1116 | long ret = 0, i = 0; |
fa5bb209 | 1117 | struct vm_area_struct *vma = NULL; |
df06b37f | 1118 | struct follow_page_context ctx = { NULL }; |
4bbd4c77 KS |
1119 | |
1120 | if (!nr_pages) | |
1121 | return 0; | |
1122 | ||
f9652594 AK |
1123 | start = untagged_addr(start); |
1124 | ||
eddb1c22 | 1125 | VM_BUG_ON(!!pages != !!(gup_flags & (FOLL_GET | FOLL_PIN))); |
4bbd4c77 KS |
1126 | |
1127 | /* | |
1128 | * If FOLL_FORCE is set then do not force a full fault as the hinting | |
1129 | * fault information is unrelated to the reference behaviour of a task | |
1130 | * using the address space | |
1131 | */ | |
1132 | if (!(gup_flags & FOLL_FORCE)) | |
1133 | gup_flags |= FOLL_NUMA; | |
1134 | ||
4bbd4c77 | 1135 | do { |
fa5bb209 KS |
1136 | struct page *page; |
1137 | unsigned int foll_flags = gup_flags; | |
1138 | unsigned int page_increm; | |
1139 | ||
1140 | /* first iteration or cross vma bound */ | |
1141 | if (!vma || start >= vma->vm_end) { | |
1142 | vma = find_extend_vma(mm, start); | |
1143 | if (!vma && in_gate_area(mm, start)) { | |
fa5bb209 KS |
1144 | ret = get_gate_page(mm, start & PAGE_MASK, |
1145 | gup_flags, &vma, | |
1146 | pages ? &pages[i] : NULL); | |
1147 | if (ret) | |
08be37b7 | 1148 | goto out; |
df06b37f | 1149 | ctx.page_mask = 0; |
fa5bb209 KS |
1150 | goto next_page; |
1151 | } | |
4bbd4c77 | 1152 | |
52650c8b | 1153 | if (!vma) { |
df06b37f KB |
1154 | ret = -EFAULT; |
1155 | goto out; | |
1156 | } | |
52650c8b JG |
1157 | ret = check_vma_flags(vma, gup_flags); |
1158 | if (ret) | |
1159 | goto out; | |
1160 | ||
fa5bb209 KS |
1161 | if (is_vm_hugetlb_page(vma)) { |
1162 | i = follow_hugetlb_page(mm, vma, pages, vmas, | |
1163 | &start, &nr_pages, i, | |
a308c71b | 1164 | gup_flags, locked); |
ad415db8 PX |
1165 | if (locked && *locked == 0) { |
1166 | /* | |
1167 | * We've got a VM_FAULT_RETRY | |
c1e8d7c6 | 1168 | * and we've lost mmap_lock. |
ad415db8 PX |
1169 | * We must stop here. |
1170 | */ | |
1171 | BUG_ON(gup_flags & FOLL_NOWAIT); | |
ad415db8 PX |
1172 | goto out; |
1173 | } | |
fa5bb209 | 1174 | continue; |
4bbd4c77 | 1175 | } |
fa5bb209 KS |
1176 | } |
1177 | retry: | |
1178 | /* | |
1179 | * If we have a pending SIGKILL, don't keep faulting pages and | |
1180 | * potentially allocating memory. | |
1181 | */ | |
fa45f116 | 1182 | if (fatal_signal_pending(current)) { |
d180870d | 1183 | ret = -EINTR; |
df06b37f KB |
1184 | goto out; |
1185 | } | |
fa5bb209 | 1186 | cond_resched(); |
df06b37f KB |
1187 | |
1188 | page = follow_page_mask(vma, start, foll_flags, &ctx); | |
fa5bb209 | 1189 | if (!page) { |
64019a2e | 1190 | ret = faultin_page(vma, start, &foll_flags, locked); |
fa5bb209 KS |
1191 | switch (ret) { |
1192 | case 0: | |
1193 | goto retry; | |
df06b37f KB |
1194 | case -EBUSY: |
1195 | ret = 0; | |
e4a9bc58 | 1196 | fallthrough; |
fa5bb209 KS |
1197 | case -EFAULT: |
1198 | case -ENOMEM: | |
1199 | case -EHWPOISON: | |
df06b37f | 1200 | goto out; |
fa5bb209 KS |
1201 | case -ENOENT: |
1202 | goto next_page; | |
4bbd4c77 | 1203 | } |
fa5bb209 | 1204 | BUG(); |
1027e443 KS |
1205 | } else if (PTR_ERR(page) == -EEXIST) { |
1206 | /* | |
1207 | * Proper page table entry exists, but no corresponding | |
1208 | * struct page. | |
1209 | */ | |
1210 | goto next_page; | |
1211 | } else if (IS_ERR(page)) { | |
df06b37f KB |
1212 | ret = PTR_ERR(page); |
1213 | goto out; | |
1027e443 | 1214 | } |
fa5bb209 KS |
1215 | if (pages) { |
1216 | pages[i] = page; | |
1217 | flush_anon_page(vma, page, start); | |
1218 | flush_dcache_page(page); | |
df06b37f | 1219 | ctx.page_mask = 0; |
4bbd4c77 | 1220 | } |
4bbd4c77 | 1221 | next_page: |
fa5bb209 KS |
1222 | if (vmas) { |
1223 | vmas[i] = vma; | |
df06b37f | 1224 | ctx.page_mask = 0; |
fa5bb209 | 1225 | } |
df06b37f | 1226 | page_increm = 1 + (~(start >> PAGE_SHIFT) & ctx.page_mask); |
fa5bb209 KS |
1227 | if (page_increm > nr_pages) |
1228 | page_increm = nr_pages; | |
1229 | i += page_increm; | |
1230 | start += page_increm * PAGE_SIZE; | |
1231 | nr_pages -= page_increm; | |
4bbd4c77 | 1232 | } while (nr_pages); |
df06b37f KB |
1233 | out: |
1234 | if (ctx.pgmap) | |
1235 | put_dev_pagemap(ctx.pgmap); | |
1236 | return i ? i : ret; | |
4bbd4c77 | 1237 | } |
4bbd4c77 | 1238 | |
771ab430 TK |
1239 | static bool vma_permits_fault(struct vm_area_struct *vma, |
1240 | unsigned int fault_flags) | |
d4925e00 | 1241 | { |
1b2ee126 DH |
1242 | bool write = !!(fault_flags & FAULT_FLAG_WRITE); |
1243 | bool foreign = !!(fault_flags & FAULT_FLAG_REMOTE); | |
33a709b2 | 1244 | vm_flags_t vm_flags = write ? VM_WRITE : VM_READ; |
d4925e00 DH |
1245 | |
1246 | if (!(vm_flags & vma->vm_flags)) | |
1247 | return false; | |
1248 | ||
33a709b2 DH |
1249 | /* |
1250 | * The architecture might have a hardware protection | |
1b2ee126 | 1251 | * mechanism other than read/write that can deny access. |
d61172b4 DH |
1252 | * |
1253 | * gup always represents data access, not instruction | |
1254 | * fetches, so execute=false here: | |
33a709b2 | 1255 | */ |
d61172b4 | 1256 | if (!arch_vma_access_permitted(vma, write, false, foreign)) |
33a709b2 DH |
1257 | return false; |
1258 | ||
d4925e00 DH |
1259 | return true; |
1260 | } | |
1261 | ||
adc8cb40 | 1262 | /** |
4bbd4c77 | 1263 | * fixup_user_fault() - manually resolve a user page fault |
4bbd4c77 KS |
1264 | * @mm: mm_struct of target mm |
1265 | * @address: user address | |
1266 | * @fault_flags:flags to pass down to handle_mm_fault() | |
c1e8d7c6 | 1267 | * @unlocked: did we unlock the mmap_lock while retrying, maybe NULL if caller |
548b6a1e MC |
1268 | * does not allow retry. If NULL, the caller must guarantee |
1269 | * that fault_flags does not contain FAULT_FLAG_ALLOW_RETRY. | |
4bbd4c77 KS |
1270 | * |
1271 | * This is meant to be called in the specific scenario where for locking reasons | |
1272 | * we try to access user memory in atomic context (within a pagefault_disable() | |
1273 | * section), this returns -EFAULT, and we want to resolve the user fault before | |
1274 | * trying again. | |
1275 | * | |
1276 | * Typically this is meant to be used by the futex code. | |
1277 | * | |
1278 | * The main difference with get_user_pages() is that this function will | |
1279 | * unconditionally call handle_mm_fault() which will in turn perform all the | |
1280 | * necessary SW fixup of the dirty and young bits in the PTE, while | |
4a9e1cda | 1281 | * get_user_pages() only guarantees to update these in the struct page. |
4bbd4c77 KS |
1282 | * |
1283 | * This is important for some architectures where those bits also gate the | |
1284 | * access permission to the page because they are maintained in software. On | |
1285 | * such architectures, gup() will not be enough to make a subsequent access | |
1286 | * succeed. | |
1287 | * | |
c1e8d7c6 ML |
1288 | * This function will not return with an unlocked mmap_lock. So it has not the |
1289 | * same semantics wrt the @mm->mmap_lock as does filemap_fault(). | |
4bbd4c77 | 1290 | */ |
64019a2e | 1291 | int fixup_user_fault(struct mm_struct *mm, |
4a9e1cda DD |
1292 | unsigned long address, unsigned int fault_flags, |
1293 | bool *unlocked) | |
4bbd4c77 KS |
1294 | { |
1295 | struct vm_area_struct *vma; | |
8fed2f3c | 1296 | vm_fault_t ret; |
4a9e1cda | 1297 | |
f9652594 AK |
1298 | address = untagged_addr(address); |
1299 | ||
4a9e1cda | 1300 | if (unlocked) |
71335f37 | 1301 | fault_flags |= FAULT_FLAG_ALLOW_RETRY | FAULT_FLAG_KILLABLE; |
4bbd4c77 | 1302 | |
4a9e1cda | 1303 | retry: |
4bbd4c77 KS |
1304 | vma = find_extend_vma(mm, address); |
1305 | if (!vma || address < vma->vm_start) | |
1306 | return -EFAULT; | |
1307 | ||
d4925e00 | 1308 | if (!vma_permits_fault(vma, fault_flags)) |
4bbd4c77 KS |
1309 | return -EFAULT; |
1310 | ||
475f4dfc PX |
1311 | if ((fault_flags & FAULT_FLAG_KILLABLE) && |
1312 | fatal_signal_pending(current)) | |
1313 | return -EINTR; | |
1314 | ||
bce617ed | 1315 | ret = handle_mm_fault(vma, address, fault_flags, NULL); |
4bbd4c77 | 1316 | if (ret & VM_FAULT_ERROR) { |
9a291a7c JM |
1317 | int err = vm_fault_to_errno(ret, 0); |
1318 | ||
1319 | if (err) | |
1320 | return err; | |
4bbd4c77 KS |
1321 | BUG(); |
1322 | } | |
4a9e1cda DD |
1323 | |
1324 | if (ret & VM_FAULT_RETRY) { | |
d8ed45c5 | 1325 | mmap_read_lock(mm); |
475f4dfc PX |
1326 | *unlocked = true; |
1327 | fault_flags |= FAULT_FLAG_TRIED; | |
1328 | goto retry; | |
4a9e1cda DD |
1329 | } |
1330 | ||
4bbd4c77 KS |
1331 | return 0; |
1332 | } | |
add6a0cd | 1333 | EXPORT_SYMBOL_GPL(fixup_user_fault); |
4bbd4c77 | 1334 | |
2d3a36a4 MH |
1335 | /* |
1336 | * Please note that this function, unlike __get_user_pages will not | |
1337 | * return 0 for nr_pages > 0 without FOLL_NOWAIT | |
1338 | */ | |
64019a2e | 1339 | static __always_inline long __get_user_pages_locked(struct mm_struct *mm, |
f0818f47 AA |
1340 | unsigned long start, |
1341 | unsigned long nr_pages, | |
f0818f47 AA |
1342 | struct page **pages, |
1343 | struct vm_area_struct **vmas, | |
e716712f | 1344 | int *locked, |
0fd71a56 | 1345 | unsigned int flags) |
f0818f47 | 1346 | { |
f0818f47 AA |
1347 | long ret, pages_done; |
1348 | bool lock_dropped; | |
1349 | ||
1350 | if (locked) { | |
1351 | /* if VM_FAULT_RETRY can be returned, vmas become invalid */ | |
1352 | BUG_ON(vmas); | |
1353 | /* check caller initialized locked */ | |
1354 | BUG_ON(*locked != 1); | |
1355 | } | |
1356 | ||
a458b76a AA |
1357 | if (flags & FOLL_PIN) |
1358 | mm_set_has_pinned_flag(&mm->flags); | |
008cfe44 | 1359 | |
eddb1c22 JH |
1360 | /* |
1361 | * FOLL_PIN and FOLL_GET are mutually exclusive. Traditional behavior | |
1362 | * is to set FOLL_GET if the caller wants pages[] filled in (but has | |
1363 | * carelessly failed to specify FOLL_GET), so keep doing that, but only | |
1364 | * for FOLL_GET, not for the newer FOLL_PIN. | |
1365 | * | |
1366 | * FOLL_PIN always expects pages to be non-null, but no need to assert | |
1367 | * that here, as any failures will be obvious enough. | |
1368 | */ | |
1369 | if (pages && !(flags & FOLL_PIN)) | |
f0818f47 | 1370 | flags |= FOLL_GET; |
f0818f47 AA |
1371 | |
1372 | pages_done = 0; | |
1373 | lock_dropped = false; | |
1374 | for (;;) { | |
64019a2e | 1375 | ret = __get_user_pages(mm, start, nr_pages, flags, pages, |
f0818f47 AA |
1376 | vmas, locked); |
1377 | if (!locked) | |
1378 | /* VM_FAULT_RETRY couldn't trigger, bypass */ | |
1379 | return ret; | |
1380 | ||
1381 | /* VM_FAULT_RETRY cannot return errors */ | |
1382 | if (!*locked) { | |
1383 | BUG_ON(ret < 0); | |
1384 | BUG_ON(ret >= nr_pages); | |
1385 | } | |
1386 | ||
f0818f47 AA |
1387 | if (ret > 0) { |
1388 | nr_pages -= ret; | |
1389 | pages_done += ret; | |
1390 | if (!nr_pages) | |
1391 | break; | |
1392 | } | |
1393 | if (*locked) { | |
96312e61 AA |
1394 | /* |
1395 | * VM_FAULT_RETRY didn't trigger or it was a | |
1396 | * FOLL_NOWAIT. | |
1397 | */ | |
f0818f47 AA |
1398 | if (!pages_done) |
1399 | pages_done = ret; | |
1400 | break; | |
1401 | } | |
df17277b MR |
1402 | /* |
1403 | * VM_FAULT_RETRY triggered, so seek to the faulting offset. | |
1404 | * For the prefault case (!pages) we only update counts. | |
1405 | */ | |
1406 | if (likely(pages)) | |
1407 | pages += ret; | |
f0818f47 | 1408 | start += ret << PAGE_SHIFT; |
4426e945 | 1409 | lock_dropped = true; |
f0818f47 | 1410 | |
4426e945 | 1411 | retry: |
f0818f47 AA |
1412 | /* |
1413 | * Repeat on the address that fired VM_FAULT_RETRY | |
4426e945 PX |
1414 | * with both FAULT_FLAG_ALLOW_RETRY and |
1415 | * FAULT_FLAG_TRIED. Note that GUP can be interrupted | |
1416 | * by fatal signals, so we need to check it before we | |
1417 | * start trying again otherwise it can loop forever. | |
f0818f47 | 1418 | */ |
4426e945 | 1419 | |
ae46d2aa HD |
1420 | if (fatal_signal_pending(current)) { |
1421 | if (!pages_done) | |
1422 | pages_done = -EINTR; | |
4426e945 | 1423 | break; |
ae46d2aa | 1424 | } |
4426e945 | 1425 | |
d8ed45c5 | 1426 | ret = mmap_read_lock_killable(mm); |
71335f37 PX |
1427 | if (ret) { |
1428 | BUG_ON(ret > 0); | |
1429 | if (!pages_done) | |
1430 | pages_done = ret; | |
1431 | break; | |
1432 | } | |
4426e945 | 1433 | |
c7b6a566 | 1434 | *locked = 1; |
64019a2e | 1435 | ret = __get_user_pages(mm, start, 1, flags | FOLL_TRIED, |
4426e945 PX |
1436 | pages, NULL, locked); |
1437 | if (!*locked) { | |
1438 | /* Continue to retry until we succeeded */ | |
1439 | BUG_ON(ret != 0); | |
1440 | goto retry; | |
1441 | } | |
f0818f47 AA |
1442 | if (ret != 1) { |
1443 | BUG_ON(ret > 1); | |
1444 | if (!pages_done) | |
1445 | pages_done = ret; | |
1446 | break; | |
1447 | } | |
1448 | nr_pages--; | |
1449 | pages_done++; | |
1450 | if (!nr_pages) | |
1451 | break; | |
df17277b MR |
1452 | if (likely(pages)) |
1453 | pages++; | |
f0818f47 AA |
1454 | start += PAGE_SIZE; |
1455 | } | |
e716712f | 1456 | if (lock_dropped && *locked) { |
f0818f47 AA |
1457 | /* |
1458 | * We must let the caller know we temporarily dropped the lock | |
1459 | * and so the critical section protected by it was lost. | |
1460 | */ | |
d8ed45c5 | 1461 | mmap_read_unlock(mm); |
f0818f47 AA |
1462 | *locked = 0; |
1463 | } | |
1464 | return pages_done; | |
1465 | } | |
1466 | ||
d3649f68 CH |
1467 | /** |
1468 | * populate_vma_page_range() - populate a range of pages in the vma. | |
1469 | * @vma: target vma | |
1470 | * @start: start address | |
1471 | * @end: end address | |
c1e8d7c6 | 1472 | * @locked: whether the mmap_lock is still held |
d3649f68 CH |
1473 | * |
1474 | * This takes care of mlocking the pages too if VM_LOCKED is set. | |
1475 | * | |
0a36f7f8 TY |
1476 | * Return either number of pages pinned in the vma, or a negative error |
1477 | * code on error. | |
d3649f68 | 1478 | * |
c1e8d7c6 | 1479 | * vma->vm_mm->mmap_lock must be held. |
d3649f68 | 1480 | * |
4f6da934 | 1481 | * If @locked is NULL, it may be held for read or write and will |
d3649f68 CH |
1482 | * be unperturbed. |
1483 | * | |
4f6da934 PX |
1484 | * If @locked is non-NULL, it must held for read only and may be |
1485 | * released. If it's released, *@locked will be set to 0. | |
d3649f68 CH |
1486 | */ |
1487 | long populate_vma_page_range(struct vm_area_struct *vma, | |
4f6da934 | 1488 | unsigned long start, unsigned long end, int *locked) |
d3649f68 CH |
1489 | { |
1490 | struct mm_struct *mm = vma->vm_mm; | |
1491 | unsigned long nr_pages = (end - start) / PAGE_SIZE; | |
1492 | int gup_flags; | |
1493 | ||
be51eb18 ML |
1494 | VM_BUG_ON(!PAGE_ALIGNED(start)); |
1495 | VM_BUG_ON(!PAGE_ALIGNED(end)); | |
d3649f68 CH |
1496 | VM_BUG_ON_VMA(start < vma->vm_start, vma); |
1497 | VM_BUG_ON_VMA(end > vma->vm_end, vma); | |
42fc5414 | 1498 | mmap_assert_locked(mm); |
d3649f68 CH |
1499 | |
1500 | gup_flags = FOLL_TOUCH | FOLL_POPULATE | FOLL_MLOCK; | |
1501 | if (vma->vm_flags & VM_LOCKONFAULT) | |
1502 | gup_flags &= ~FOLL_POPULATE; | |
1503 | /* | |
1504 | * We want to touch writable mappings with a write fault in order | |
1505 | * to break COW, except for shared mappings because these don't COW | |
1506 | * and we would not want to dirty them for nothing. | |
1507 | */ | |
1508 | if ((vma->vm_flags & (VM_WRITE | VM_SHARED)) == VM_WRITE) | |
1509 | gup_flags |= FOLL_WRITE; | |
1510 | ||
1511 | /* | |
1512 | * We want mlock to succeed for regions that have any permissions | |
1513 | * other than PROT_NONE. | |
1514 | */ | |
3122e80e | 1515 | if (vma_is_accessible(vma)) |
d3649f68 CH |
1516 | gup_flags |= FOLL_FORCE; |
1517 | ||
1518 | /* | |
1519 | * We made sure addr is within a VMA, so the following will | |
1520 | * not result in a stack expansion that recurses back here. | |
1521 | */ | |
64019a2e | 1522 | return __get_user_pages(mm, start, nr_pages, gup_flags, |
4f6da934 | 1523 | NULL, NULL, locked); |
d3649f68 CH |
1524 | } |
1525 | ||
4ca9b385 DH |
1526 | /* |
1527 | * faultin_vma_page_range() - populate (prefault) page tables inside the | |
1528 | * given VMA range readable/writable | |
1529 | * | |
1530 | * This takes care of mlocking the pages, too, if VM_LOCKED is set. | |
1531 | * | |
1532 | * @vma: target vma | |
1533 | * @start: start address | |
1534 | * @end: end address | |
1535 | * @write: whether to prefault readable or writable | |
1536 | * @locked: whether the mmap_lock is still held | |
1537 | * | |
1538 | * Returns either number of processed pages in the vma, or a negative error | |
1539 | * code on error (see __get_user_pages()). | |
1540 | * | |
1541 | * vma->vm_mm->mmap_lock must be held. The range must be page-aligned and | |
1542 | * covered by the VMA. | |
1543 | * | |
1544 | * If @locked is NULL, it may be held for read or write and will be unperturbed. | |
1545 | * | |
1546 | * If @locked is non-NULL, it must held for read only and may be released. If | |
1547 | * it's released, *@locked will be set to 0. | |
1548 | */ | |
1549 | long faultin_vma_page_range(struct vm_area_struct *vma, unsigned long start, | |
1550 | unsigned long end, bool write, int *locked) | |
1551 | { | |
1552 | struct mm_struct *mm = vma->vm_mm; | |
1553 | unsigned long nr_pages = (end - start) / PAGE_SIZE; | |
1554 | int gup_flags; | |
1555 | ||
1556 | VM_BUG_ON(!PAGE_ALIGNED(start)); | |
1557 | VM_BUG_ON(!PAGE_ALIGNED(end)); | |
1558 | VM_BUG_ON_VMA(start < vma->vm_start, vma); | |
1559 | VM_BUG_ON_VMA(end > vma->vm_end, vma); | |
1560 | mmap_assert_locked(mm); | |
1561 | ||
1562 | /* | |
1563 | * FOLL_TOUCH: Mark page accessed and thereby young; will also mark | |
1564 | * the page dirty with FOLL_WRITE -- which doesn't make a | |
1565 | * difference with !FOLL_FORCE, because the page is writable | |
1566 | * in the page table. | |
1567 | * FOLL_HWPOISON: Return -EHWPOISON instead of -EFAULT when we hit | |
1568 | * a poisoned page. | |
1569 | * FOLL_POPULATE: Always populate memory with VM_LOCKONFAULT. | |
1570 | * !FOLL_FORCE: Require proper access permissions. | |
1571 | */ | |
1572 | gup_flags = FOLL_TOUCH | FOLL_POPULATE | FOLL_MLOCK | FOLL_HWPOISON; | |
1573 | if (write) | |
1574 | gup_flags |= FOLL_WRITE; | |
1575 | ||
1576 | /* | |
eb2faa51 DH |
1577 | * We want to report -EINVAL instead of -EFAULT for any permission |
1578 | * problems or incompatible mappings. | |
4ca9b385 | 1579 | */ |
eb2faa51 DH |
1580 | if (check_vma_flags(vma, gup_flags)) |
1581 | return -EINVAL; | |
1582 | ||
4ca9b385 DH |
1583 | return __get_user_pages(mm, start, nr_pages, gup_flags, |
1584 | NULL, NULL, locked); | |
1585 | } | |
1586 | ||
d3649f68 CH |
1587 | /* |
1588 | * __mm_populate - populate and/or mlock pages within a range of address space. | |
1589 | * | |
1590 | * This is used to implement mlock() and the MAP_POPULATE / MAP_LOCKED mmap | |
1591 | * flags. VMAs must be already marked with the desired vm_flags, and | |
c1e8d7c6 | 1592 | * mmap_lock must not be held. |
d3649f68 CH |
1593 | */ |
1594 | int __mm_populate(unsigned long start, unsigned long len, int ignore_errors) | |
1595 | { | |
1596 | struct mm_struct *mm = current->mm; | |
1597 | unsigned long end, nstart, nend; | |
1598 | struct vm_area_struct *vma = NULL; | |
1599 | int locked = 0; | |
1600 | long ret = 0; | |
1601 | ||
1602 | end = start + len; | |
1603 | ||
1604 | for (nstart = start; nstart < end; nstart = nend) { | |
1605 | /* | |
1606 | * We want to fault in pages for [nstart; end) address range. | |
1607 | * Find first corresponding VMA. | |
1608 | */ | |
1609 | if (!locked) { | |
1610 | locked = 1; | |
d8ed45c5 | 1611 | mmap_read_lock(mm); |
d3649f68 CH |
1612 | vma = find_vma(mm, nstart); |
1613 | } else if (nstart >= vma->vm_end) | |
1614 | vma = vma->vm_next; | |
1615 | if (!vma || vma->vm_start >= end) | |
1616 | break; | |
1617 | /* | |
1618 | * Set [nstart; nend) to intersection of desired address | |
1619 | * range with the first VMA. Also, skip undesirable VMA types. | |
1620 | */ | |
1621 | nend = min(end, vma->vm_end); | |
1622 | if (vma->vm_flags & (VM_IO | VM_PFNMAP)) | |
1623 | continue; | |
1624 | if (nstart < vma->vm_start) | |
1625 | nstart = vma->vm_start; | |
1626 | /* | |
1627 | * Now fault in a range of pages. populate_vma_page_range() | |
1628 | * double checks the vma flags, so that it won't mlock pages | |
1629 | * if the vma was already munlocked. | |
1630 | */ | |
1631 | ret = populate_vma_page_range(vma, nstart, nend, &locked); | |
1632 | if (ret < 0) { | |
1633 | if (ignore_errors) { | |
1634 | ret = 0; | |
1635 | continue; /* continue at next VMA */ | |
1636 | } | |
1637 | break; | |
1638 | } | |
1639 | nend = nstart + ret * PAGE_SIZE; | |
1640 | ret = 0; | |
1641 | } | |
1642 | if (locked) | |
d8ed45c5 | 1643 | mmap_read_unlock(mm); |
d3649f68 CH |
1644 | return ret; /* 0 or negative error code */ |
1645 | } | |
050a9adc | 1646 | #else /* CONFIG_MMU */ |
64019a2e | 1647 | static long __get_user_pages_locked(struct mm_struct *mm, unsigned long start, |
050a9adc CH |
1648 | unsigned long nr_pages, struct page **pages, |
1649 | struct vm_area_struct **vmas, int *locked, | |
1650 | unsigned int foll_flags) | |
1651 | { | |
1652 | struct vm_area_struct *vma; | |
1653 | unsigned long vm_flags; | |
24dc20c7 | 1654 | long i; |
050a9adc CH |
1655 | |
1656 | /* calculate required read or write permissions. | |
1657 | * If FOLL_FORCE is set, we only require the "MAY" flags. | |
1658 | */ | |
1659 | vm_flags = (foll_flags & FOLL_WRITE) ? | |
1660 | (VM_WRITE | VM_MAYWRITE) : (VM_READ | VM_MAYREAD); | |
1661 | vm_flags &= (foll_flags & FOLL_FORCE) ? | |
1662 | (VM_MAYREAD | VM_MAYWRITE) : (VM_READ | VM_WRITE); | |
1663 | ||
1664 | for (i = 0; i < nr_pages; i++) { | |
1665 | vma = find_vma(mm, start); | |
1666 | if (!vma) | |
1667 | goto finish_or_fault; | |
1668 | ||
1669 | /* protect what we can, including chardevs */ | |
1670 | if ((vma->vm_flags & (VM_IO | VM_PFNMAP)) || | |
1671 | !(vm_flags & vma->vm_flags)) | |
1672 | goto finish_or_fault; | |
1673 | ||
1674 | if (pages) { | |
1675 | pages[i] = virt_to_page(start); | |
1676 | if (pages[i]) | |
1677 | get_page(pages[i]); | |
1678 | } | |
1679 | if (vmas) | |
1680 | vmas[i] = vma; | |
1681 | start = (start + PAGE_SIZE) & PAGE_MASK; | |
1682 | } | |
1683 | ||
1684 | return i; | |
1685 | ||
1686 | finish_or_fault: | |
1687 | return i ? : -EFAULT; | |
1688 | } | |
1689 | #endif /* !CONFIG_MMU */ | |
d3649f68 | 1690 | |
bb523b40 AG |
1691 | /** |
1692 | * fault_in_writeable - fault in userspace address range for writing | |
1693 | * @uaddr: start of address range | |
1694 | * @size: size of address range | |
1695 | * | |
1696 | * Returns the number of bytes not faulted in (like copy_to_user() and | |
1697 | * copy_from_user()). | |
1698 | */ | |
1699 | size_t fault_in_writeable(char __user *uaddr, size_t size) | |
1700 | { | |
1701 | char __user *start = uaddr, *end; | |
1702 | ||
1703 | if (unlikely(size == 0)) | |
1704 | return 0; | |
677b2a8c CL |
1705 | if (!user_write_access_begin(uaddr, size)) |
1706 | return size; | |
bb523b40 | 1707 | if (!PAGE_ALIGNED(uaddr)) { |
677b2a8c | 1708 | unsafe_put_user(0, uaddr, out); |
bb523b40 AG |
1709 | uaddr = (char __user *)PAGE_ALIGN((unsigned long)uaddr); |
1710 | } | |
1711 | end = (char __user *)PAGE_ALIGN((unsigned long)start + size); | |
1712 | if (unlikely(end < start)) | |
1713 | end = NULL; | |
1714 | while (uaddr != end) { | |
677b2a8c | 1715 | unsafe_put_user(0, uaddr, out); |
bb523b40 AG |
1716 | uaddr += PAGE_SIZE; |
1717 | } | |
1718 | ||
1719 | out: | |
677b2a8c | 1720 | user_write_access_end(); |
bb523b40 AG |
1721 | if (size > uaddr - start) |
1722 | return size - (uaddr - start); | |
1723 | return 0; | |
1724 | } | |
1725 | EXPORT_SYMBOL(fault_in_writeable); | |
1726 | ||
cdd591fc AG |
1727 | /* |
1728 | * fault_in_safe_writeable - fault in an address range for writing | |
1729 | * @uaddr: start of address range | |
1730 | * @size: length of address range | |
1731 | * | |
fe673d3f LT |
1732 | * Faults in an address range for writing. This is primarily useful when we |
1733 | * already know that some or all of the pages in the address range aren't in | |
1734 | * memory. | |
cdd591fc | 1735 | * |
fe673d3f | 1736 | * Unlike fault_in_writeable(), this function is non-destructive. |
cdd591fc AG |
1737 | * |
1738 | * Note that we don't pin or otherwise hold the pages referenced that we fault | |
1739 | * in. There's no guarantee that they'll stay in memory for any duration of | |
1740 | * time. | |
1741 | * | |
1742 | * Returns the number of bytes not faulted in, like copy_to_user() and | |
1743 | * copy_from_user(). | |
1744 | */ | |
1745 | size_t fault_in_safe_writeable(const char __user *uaddr, size_t size) | |
1746 | { | |
fe673d3f | 1747 | unsigned long start = (unsigned long)uaddr, end; |
cdd591fc | 1748 | struct mm_struct *mm = current->mm; |
fe673d3f | 1749 | bool unlocked = false; |
cdd591fc | 1750 | |
fe673d3f LT |
1751 | if (unlikely(size == 0)) |
1752 | return 0; | |
cdd591fc | 1753 | end = PAGE_ALIGN(start + size); |
fe673d3f | 1754 | if (end < start) |
cdd591fc | 1755 | end = 0; |
cdd591fc | 1756 | |
fe673d3f LT |
1757 | mmap_read_lock(mm); |
1758 | do { | |
1759 | if (fixup_user_fault(mm, start, FAULT_FLAG_WRITE, &unlocked)) | |
cdd591fc | 1760 | break; |
fe673d3f LT |
1761 | start = (start + PAGE_SIZE) & PAGE_MASK; |
1762 | } while (start != end); | |
1763 | mmap_read_unlock(mm); | |
1764 | ||
1765 | if (size > (unsigned long)uaddr - start) | |
1766 | return size - ((unsigned long)uaddr - start); | |
1767 | return 0; | |
cdd591fc AG |
1768 | } |
1769 | EXPORT_SYMBOL(fault_in_safe_writeable); | |
1770 | ||
bb523b40 AG |
1771 | /** |
1772 | * fault_in_readable - fault in userspace address range for reading | |
1773 | * @uaddr: start of user address range | |
1774 | * @size: size of user address range | |
1775 | * | |
1776 | * Returns the number of bytes not faulted in (like copy_to_user() and | |
1777 | * copy_from_user()). | |
1778 | */ | |
1779 | size_t fault_in_readable(const char __user *uaddr, size_t size) | |
1780 | { | |
1781 | const char __user *start = uaddr, *end; | |
1782 | volatile char c; | |
1783 | ||
1784 | if (unlikely(size == 0)) | |
1785 | return 0; | |
677b2a8c CL |
1786 | if (!user_read_access_begin(uaddr, size)) |
1787 | return size; | |
bb523b40 | 1788 | if (!PAGE_ALIGNED(uaddr)) { |
677b2a8c | 1789 | unsafe_get_user(c, uaddr, out); |
bb523b40 AG |
1790 | uaddr = (const char __user *)PAGE_ALIGN((unsigned long)uaddr); |
1791 | } | |
1792 | end = (const char __user *)PAGE_ALIGN((unsigned long)start + size); | |
1793 | if (unlikely(end < start)) | |
1794 | end = NULL; | |
1795 | while (uaddr != end) { | |
677b2a8c | 1796 | unsafe_get_user(c, uaddr, out); |
bb523b40 AG |
1797 | uaddr += PAGE_SIZE; |
1798 | } | |
1799 | ||
1800 | out: | |
677b2a8c | 1801 | user_read_access_end(); |
bb523b40 AG |
1802 | (void)c; |
1803 | if (size > uaddr - start) | |
1804 | return size - (uaddr - start); | |
1805 | return 0; | |
1806 | } | |
1807 | EXPORT_SYMBOL(fault_in_readable); | |
1808 | ||
8f942eea JH |
1809 | /** |
1810 | * get_dump_page() - pin user page in memory while writing it to core dump | |
1811 | * @addr: user address | |
1812 | * | |
1813 | * Returns struct page pointer of user page pinned for dump, | |
1814 | * to be freed afterwards by put_page(). | |
1815 | * | |
1816 | * Returns NULL on any kind of failure - a hole must then be inserted into | |
1817 | * the corefile, to preserve alignment with its headers; and also returns | |
1818 | * NULL wherever the ZERO_PAGE, or an anonymous pte_none, has been found - | |
f0953a1b | 1819 | * allowing a hole to be left in the corefile to save disk space. |
8f942eea | 1820 | * |
7f3bfab5 | 1821 | * Called without mmap_lock (takes and releases the mmap_lock by itself). |
8f942eea JH |
1822 | */ |
1823 | #ifdef CONFIG_ELF_CORE | |
1824 | struct page *get_dump_page(unsigned long addr) | |
1825 | { | |
7f3bfab5 | 1826 | struct mm_struct *mm = current->mm; |
8f942eea | 1827 | struct page *page; |
7f3bfab5 JH |
1828 | int locked = 1; |
1829 | int ret; | |
8f942eea | 1830 | |
7f3bfab5 | 1831 | if (mmap_read_lock_killable(mm)) |
8f942eea | 1832 | return NULL; |
7f3bfab5 JH |
1833 | ret = __get_user_pages_locked(mm, addr, 1, &page, NULL, &locked, |
1834 | FOLL_FORCE | FOLL_DUMP | FOLL_GET); | |
1835 | if (locked) | |
1836 | mmap_read_unlock(mm); | |
1837 | return (ret == 1) ? page : NULL; | |
8f942eea JH |
1838 | } |
1839 | #endif /* CONFIG_ELF_CORE */ | |
1840 | ||
d1e153fe | 1841 | #ifdef CONFIG_MIGRATION |
f68749ec PT |
1842 | /* |
1843 | * Check whether all pages are pinnable, if so return number of pages. If some | |
1844 | * pages are not pinnable, migrate them, and unpin all pages. Return zero if | |
1845 | * pages were migrated, or if some pages were not successfully isolated. | |
1846 | * Return negative error if migration fails. | |
1847 | */ | |
1848 | static long check_and_migrate_movable_pages(unsigned long nr_pages, | |
d1e153fe | 1849 | struct page **pages, |
d1e153fe | 1850 | unsigned int gup_flags) |
9a4e9f3b | 1851 | { |
f68749ec PT |
1852 | unsigned long i; |
1853 | unsigned long isolation_error_count = 0; | |
1854 | bool drain_allow = true; | |
d1e153fe | 1855 | LIST_HEAD(movable_page_list); |
f68749ec PT |
1856 | long ret = 0; |
1857 | struct page *prev_head = NULL; | |
1858 | struct page *head; | |
ed03d924 JK |
1859 | struct migration_target_control mtc = { |
1860 | .nid = NUMA_NO_NODE, | |
c991ffef | 1861 | .gfp_mask = GFP_USER | __GFP_NOWARN, |
ed03d924 | 1862 | }; |
9a4e9f3b | 1863 | |
83c02c23 PT |
1864 | for (i = 0; i < nr_pages; i++) { |
1865 | head = compound_head(pages[i]); | |
1866 | if (head == prev_head) | |
1867 | continue; | |
1868 | prev_head = head; | |
9a4e9f3b | 1869 | /* |
d1e153fe PT |
1870 | * If we get a movable page, since we are going to be pinning |
1871 | * these entries, try to move them out if possible. | |
9a4e9f3b | 1872 | */ |
d1e153fe | 1873 | if (!is_pinnable_page(head)) { |
6e7f34eb | 1874 | if (PageHuge(head)) { |
d1e153fe | 1875 | if (!isolate_huge_page(head, &movable_page_list)) |
6e7f34eb PT |
1876 | isolation_error_count++; |
1877 | } else { | |
9a4e9f3b AK |
1878 | if (!PageLRU(head) && drain_allow) { |
1879 | lru_add_drain_all(); | |
1880 | drain_allow = false; | |
1881 | } | |
1882 | ||
6e7f34eb PT |
1883 | if (isolate_lru_page(head)) { |
1884 | isolation_error_count++; | |
1885 | continue; | |
9a4e9f3b | 1886 | } |
d1e153fe | 1887 | list_add_tail(&head->lru, &movable_page_list); |
6e7f34eb PT |
1888 | mod_node_page_state(page_pgdat(head), |
1889 | NR_ISOLATED_ANON + | |
1890 | page_is_file_lru(head), | |
1891 | thp_nr_pages(head)); | |
9a4e9f3b AK |
1892 | } |
1893 | } | |
1894 | } | |
1895 | ||
6e7f34eb PT |
1896 | /* |
1897 | * If list is empty, and no isolation errors, means that all pages are | |
1898 | * in the correct zone. | |
1899 | */ | |
d1e153fe | 1900 | if (list_empty(&movable_page_list) && !isolation_error_count) |
f68749ec | 1901 | return nr_pages; |
6e7f34eb | 1902 | |
f68749ec PT |
1903 | if (gup_flags & FOLL_PIN) { |
1904 | unpin_user_pages(pages, nr_pages); | |
1905 | } else { | |
1906 | for (i = 0; i < nr_pages; i++) | |
1907 | put_page(pages[i]); | |
1908 | } | |
d1e153fe | 1909 | if (!list_empty(&movable_page_list)) { |
d1e153fe | 1910 | ret = migrate_pages(&movable_page_list, alloc_migration_target, |
f0f44638 | 1911 | NULL, (unsigned long)&mtc, MIGRATE_SYNC, |
5ac95884 | 1912 | MR_LONGTERM_PIN, NULL); |
f68749ec PT |
1913 | if (ret && !list_empty(&movable_page_list)) |
1914 | putback_movable_pages(&movable_page_list); | |
9a4e9f3b AK |
1915 | } |
1916 | ||
f68749ec | 1917 | return ret > 0 ? -ENOMEM : ret; |
9a4e9f3b AK |
1918 | } |
1919 | #else | |
f68749ec | 1920 | static long check_and_migrate_movable_pages(unsigned long nr_pages, |
d1e153fe | 1921 | struct page **pages, |
d1e153fe | 1922 | unsigned int gup_flags) |
9a4e9f3b AK |
1923 | { |
1924 | return nr_pages; | |
1925 | } | |
d1e153fe | 1926 | #endif /* CONFIG_MIGRATION */ |
9a4e9f3b | 1927 | |
2bb6d283 | 1928 | /* |
932f4a63 IW |
1929 | * __gup_longterm_locked() is a wrapper for __get_user_pages_locked which |
1930 | * allows us to process the FOLL_LONGTERM flag. | |
2bb6d283 | 1931 | */ |
64019a2e | 1932 | static long __gup_longterm_locked(struct mm_struct *mm, |
932f4a63 IW |
1933 | unsigned long start, |
1934 | unsigned long nr_pages, | |
1935 | struct page **pages, | |
1936 | struct vm_area_struct **vmas, | |
1937 | unsigned int gup_flags) | |
2bb6d283 | 1938 | { |
f68749ec | 1939 | unsigned int flags; |
52650c8b | 1940 | long rc; |
2bb6d283 | 1941 | |
f68749ec PT |
1942 | if (!(gup_flags & FOLL_LONGTERM)) |
1943 | return __get_user_pages_locked(mm, start, nr_pages, pages, vmas, | |
1944 | NULL, gup_flags); | |
1945 | flags = memalloc_pin_save(); | |
1946 | do { | |
1947 | rc = __get_user_pages_locked(mm, start, nr_pages, pages, vmas, | |
1948 | NULL, gup_flags); | |
1949 | if (rc <= 0) | |
1950 | break; | |
1951 | rc = check_and_migrate_movable_pages(rc, pages, gup_flags); | |
1952 | } while (!rc); | |
1953 | memalloc_pin_restore(flags); | |
2bb6d283 | 1954 | |
2bb6d283 DW |
1955 | return rc; |
1956 | } | |
932f4a63 | 1957 | |
447f3e45 BS |
1958 | static bool is_valid_gup_flags(unsigned int gup_flags) |
1959 | { | |
1960 | /* | |
1961 | * FOLL_PIN must only be set internally by the pin_user_pages*() APIs, | |
1962 | * never directly by the caller, so enforce that with an assertion: | |
1963 | */ | |
1964 | if (WARN_ON_ONCE(gup_flags & FOLL_PIN)) | |
1965 | return false; | |
1966 | /* | |
1967 | * FOLL_PIN is a prerequisite to FOLL_LONGTERM. Another way of saying | |
1968 | * that is, FOLL_LONGTERM is a specific case, more restrictive case of | |
1969 | * FOLL_PIN. | |
1970 | */ | |
1971 | if (WARN_ON_ONCE(gup_flags & FOLL_LONGTERM)) | |
1972 | return false; | |
1973 | ||
1974 | return true; | |
1975 | } | |
1976 | ||
22bf29b6 | 1977 | #ifdef CONFIG_MMU |
64019a2e | 1978 | static long __get_user_pages_remote(struct mm_struct *mm, |
22bf29b6 JH |
1979 | unsigned long start, unsigned long nr_pages, |
1980 | unsigned int gup_flags, struct page **pages, | |
1981 | struct vm_area_struct **vmas, int *locked) | |
1982 | { | |
1983 | /* | |
1984 | * Parts of FOLL_LONGTERM behavior are incompatible with | |
1985 | * FAULT_FLAG_ALLOW_RETRY because of the FS DAX check requirement on | |
1986 | * vmas. However, this only comes up if locked is set, and there are | |
1987 | * callers that do request FOLL_LONGTERM, but do not set locked. So, | |
1988 | * allow what we can. | |
1989 | */ | |
1990 | if (gup_flags & FOLL_LONGTERM) { | |
1991 | if (WARN_ON_ONCE(locked)) | |
1992 | return -EINVAL; | |
1993 | /* | |
1994 | * This will check the vmas (even if our vmas arg is NULL) | |
1995 | * and return -ENOTSUPP if DAX isn't allowed in this case: | |
1996 | */ | |
64019a2e | 1997 | return __gup_longterm_locked(mm, start, nr_pages, pages, |
22bf29b6 JH |
1998 | vmas, gup_flags | FOLL_TOUCH | |
1999 | FOLL_REMOTE); | |
2000 | } | |
2001 | ||
64019a2e | 2002 | return __get_user_pages_locked(mm, start, nr_pages, pages, vmas, |
22bf29b6 JH |
2003 | locked, |
2004 | gup_flags | FOLL_TOUCH | FOLL_REMOTE); | |
2005 | } | |
2006 | ||
adc8cb40 | 2007 | /** |
c4237f8b | 2008 | * get_user_pages_remote() - pin user pages in memory |
c4237f8b JH |
2009 | * @mm: mm_struct of target mm |
2010 | * @start: starting user address | |
2011 | * @nr_pages: number of pages from start to pin | |
2012 | * @gup_flags: flags modifying lookup behaviour | |
2013 | * @pages: array that receives pointers to the pages pinned. | |
2014 | * Should be at least nr_pages long. Or NULL, if caller | |
2015 | * only intends to ensure the pages are faulted in. | |
2016 | * @vmas: array of pointers to vmas corresponding to each page. | |
2017 | * Or NULL if the caller does not require them. | |
2018 | * @locked: pointer to lock flag indicating whether lock is held and | |
2019 | * subsequently whether VM_FAULT_RETRY functionality can be | |
2020 | * utilised. Lock must initially be held. | |
2021 | * | |
2022 | * Returns either number of pages pinned (which may be less than the | |
2023 | * number requested), or an error. Details about the return value: | |
2024 | * | |
2025 | * -- If nr_pages is 0, returns 0. | |
2026 | * -- If nr_pages is >0, but no pages were pinned, returns -errno. | |
2027 | * -- If nr_pages is >0, and some pages were pinned, returns the number of | |
2028 | * pages pinned. Again, this may be less than nr_pages. | |
2029 | * | |
2030 | * The caller is responsible for releasing returned @pages, via put_page(). | |
2031 | * | |
c1e8d7c6 | 2032 | * @vmas are valid only as long as mmap_lock is held. |
c4237f8b | 2033 | * |
c1e8d7c6 | 2034 | * Must be called with mmap_lock held for read or write. |
c4237f8b | 2035 | * |
adc8cb40 SJ |
2036 | * get_user_pages_remote walks a process's page tables and takes a reference |
2037 | * to each struct page that each user address corresponds to at a given | |
c4237f8b JH |
2038 | * instant. That is, it takes the page that would be accessed if a user |
2039 | * thread accesses the given user virtual address at that instant. | |
2040 | * | |
2041 | * This does not guarantee that the page exists in the user mappings when | |
adc8cb40 | 2042 | * get_user_pages_remote returns, and there may even be a completely different |
c4237f8b JH |
2043 | * page there in some cases (eg. if mmapped pagecache has been invalidated |
2044 | * and subsequently re faulted). However it does guarantee that the page | |
2045 | * won't be freed completely. And mostly callers simply care that the page | |
2046 | * contains data that was valid *at some point in time*. Typically, an IO | |
2047 | * or similar operation cannot guarantee anything stronger anyway because | |
2048 | * locks can't be held over the syscall boundary. | |
2049 | * | |
2050 | * If gup_flags & FOLL_WRITE == 0, the page must not be written to. If the page | |
2051 | * is written to, set_page_dirty (or set_page_dirty_lock, as appropriate) must | |
2052 | * be called after the page is finished with, and before put_page is called. | |
2053 | * | |
adc8cb40 SJ |
2054 | * get_user_pages_remote is typically used for fewer-copy IO operations, |
2055 | * to get a handle on the memory by some means other than accesses | |
2056 | * via the user virtual addresses. The pages may be submitted for | |
2057 | * DMA to devices or accessed via their kernel linear mapping (via the | |
2058 | * kmap APIs). Care should be taken to use the correct cache flushing APIs. | |
c4237f8b JH |
2059 | * |
2060 | * See also get_user_pages_fast, for performance critical applications. | |
2061 | * | |
adc8cb40 | 2062 | * get_user_pages_remote should be phased out in favor of |
c4237f8b | 2063 | * get_user_pages_locked|unlocked or get_user_pages_fast. Nothing |
adc8cb40 | 2064 | * should use get_user_pages_remote because it cannot pass |
c4237f8b JH |
2065 | * FAULT_FLAG_ALLOW_RETRY to handle_mm_fault. |
2066 | */ | |
64019a2e | 2067 | long get_user_pages_remote(struct mm_struct *mm, |
c4237f8b JH |
2068 | unsigned long start, unsigned long nr_pages, |
2069 | unsigned int gup_flags, struct page **pages, | |
2070 | struct vm_area_struct **vmas, int *locked) | |
2071 | { | |
447f3e45 | 2072 | if (!is_valid_gup_flags(gup_flags)) |
eddb1c22 JH |
2073 | return -EINVAL; |
2074 | ||
64019a2e | 2075 | return __get_user_pages_remote(mm, start, nr_pages, gup_flags, |
22bf29b6 | 2076 | pages, vmas, locked); |
c4237f8b JH |
2077 | } |
2078 | EXPORT_SYMBOL(get_user_pages_remote); | |
2079 | ||
eddb1c22 | 2080 | #else /* CONFIG_MMU */ |
64019a2e | 2081 | long get_user_pages_remote(struct mm_struct *mm, |
eddb1c22 JH |
2082 | unsigned long start, unsigned long nr_pages, |
2083 | unsigned int gup_flags, struct page **pages, | |
2084 | struct vm_area_struct **vmas, int *locked) | |
2085 | { | |
2086 | return 0; | |
2087 | } | |
3faa52c0 | 2088 | |
64019a2e | 2089 | static long __get_user_pages_remote(struct mm_struct *mm, |
3faa52c0 JH |
2090 | unsigned long start, unsigned long nr_pages, |
2091 | unsigned int gup_flags, struct page **pages, | |
2092 | struct vm_area_struct **vmas, int *locked) | |
2093 | { | |
2094 | return 0; | |
2095 | } | |
eddb1c22 JH |
2096 | #endif /* !CONFIG_MMU */ |
2097 | ||
adc8cb40 SJ |
2098 | /** |
2099 | * get_user_pages() - pin user pages in memory | |
2100 | * @start: starting user address | |
2101 | * @nr_pages: number of pages from start to pin | |
2102 | * @gup_flags: flags modifying lookup behaviour | |
2103 | * @pages: array that receives pointers to the pages pinned. | |
2104 | * Should be at least nr_pages long. Or NULL, if caller | |
2105 | * only intends to ensure the pages are faulted in. | |
2106 | * @vmas: array of pointers to vmas corresponding to each page. | |
2107 | * Or NULL if the caller does not require them. | |
2108 | * | |
64019a2e PX |
2109 | * This is the same as get_user_pages_remote(), just with a less-flexible |
2110 | * calling convention where we assume that the mm being operated on belongs to | |
2111 | * the current task, and doesn't allow passing of a locked parameter. We also | |
2112 | * obviously don't pass FOLL_REMOTE in here. | |
932f4a63 IW |
2113 | */ |
2114 | long get_user_pages(unsigned long start, unsigned long nr_pages, | |
2115 | unsigned int gup_flags, struct page **pages, | |
2116 | struct vm_area_struct **vmas) | |
2117 | { | |
447f3e45 | 2118 | if (!is_valid_gup_flags(gup_flags)) |
eddb1c22 JH |
2119 | return -EINVAL; |
2120 | ||
64019a2e | 2121 | return __gup_longterm_locked(current->mm, start, nr_pages, |
932f4a63 IW |
2122 | pages, vmas, gup_flags | FOLL_TOUCH); |
2123 | } | |
2124 | EXPORT_SYMBOL(get_user_pages); | |
2bb6d283 | 2125 | |
adc8cb40 | 2126 | /** |
a00cda3f MCC |
2127 | * get_user_pages_locked() - variant of get_user_pages() |
2128 | * | |
2129 | * @start: starting user address | |
2130 | * @nr_pages: number of pages from start to pin | |
2131 | * @gup_flags: flags modifying lookup behaviour | |
2132 | * @pages: array that receives pointers to the pages pinned. | |
2133 | * Should be at least nr_pages long. Or NULL, if caller | |
2134 | * only intends to ensure the pages are faulted in. | |
2135 | * @locked: pointer to lock flag indicating whether lock is held and | |
2136 | * subsequently whether VM_FAULT_RETRY functionality can be | |
2137 | * utilised. Lock must initially be held. | |
2138 | * | |
2139 | * It is suitable to replace the form: | |
acc3c8d1 | 2140 | * |
3e4e28c5 | 2141 | * mmap_read_lock(mm); |
d3649f68 | 2142 | * do_something() |
64019a2e | 2143 | * get_user_pages(mm, ..., pages, NULL); |
3e4e28c5 | 2144 | * mmap_read_unlock(mm); |
acc3c8d1 | 2145 | * |
d3649f68 | 2146 | * to: |
acc3c8d1 | 2147 | * |
d3649f68 | 2148 | * int locked = 1; |
3e4e28c5 | 2149 | * mmap_read_lock(mm); |
d3649f68 | 2150 | * do_something() |
64019a2e | 2151 | * get_user_pages_locked(mm, ..., pages, &locked); |
d3649f68 | 2152 | * if (locked) |
3e4e28c5 | 2153 | * mmap_read_unlock(mm); |
adc8cb40 | 2154 | * |
adc8cb40 SJ |
2155 | * We can leverage the VM_FAULT_RETRY functionality in the page fault |
2156 | * paths better by using either get_user_pages_locked() or | |
2157 | * get_user_pages_unlocked(). | |
2158 | * | |
acc3c8d1 | 2159 | */ |
d3649f68 CH |
2160 | long get_user_pages_locked(unsigned long start, unsigned long nr_pages, |
2161 | unsigned int gup_flags, struct page **pages, | |
2162 | int *locked) | |
acc3c8d1 | 2163 | { |
acc3c8d1 | 2164 | /* |
d3649f68 CH |
2165 | * FIXME: Current FOLL_LONGTERM behavior is incompatible with |
2166 | * FAULT_FLAG_ALLOW_RETRY because of the FS DAX check requirement on | |
2167 | * vmas. As there are no users of this flag in this call we simply | |
2168 | * disallow this option for now. | |
acc3c8d1 | 2169 | */ |
d3649f68 CH |
2170 | if (WARN_ON_ONCE(gup_flags & FOLL_LONGTERM)) |
2171 | return -EINVAL; | |
420c2091 JH |
2172 | /* |
2173 | * FOLL_PIN must only be set internally by the pin_user_pages*() APIs, | |
2174 | * never directly by the caller, so enforce that: | |
2175 | */ | |
2176 | if (WARN_ON_ONCE(gup_flags & FOLL_PIN)) | |
2177 | return -EINVAL; | |
acc3c8d1 | 2178 | |
64019a2e | 2179 | return __get_user_pages_locked(current->mm, start, nr_pages, |
d3649f68 CH |
2180 | pages, NULL, locked, |
2181 | gup_flags | FOLL_TOUCH); | |
acc3c8d1 | 2182 | } |
d3649f68 | 2183 | EXPORT_SYMBOL(get_user_pages_locked); |
acc3c8d1 KS |
2184 | |
2185 | /* | |
d3649f68 | 2186 | * get_user_pages_unlocked() is suitable to replace the form: |
acc3c8d1 | 2187 | * |
3e4e28c5 | 2188 | * mmap_read_lock(mm); |
64019a2e | 2189 | * get_user_pages(mm, ..., pages, NULL); |
3e4e28c5 | 2190 | * mmap_read_unlock(mm); |
d3649f68 CH |
2191 | * |
2192 | * with: | |
2193 | * | |
64019a2e | 2194 | * get_user_pages_unlocked(mm, ..., pages); |
d3649f68 CH |
2195 | * |
2196 | * It is functionally equivalent to get_user_pages_fast so | |
2197 | * get_user_pages_fast should be used instead if specific gup_flags | |
2198 | * (e.g. FOLL_FORCE) are not required. | |
acc3c8d1 | 2199 | */ |
d3649f68 CH |
2200 | long get_user_pages_unlocked(unsigned long start, unsigned long nr_pages, |
2201 | struct page **pages, unsigned int gup_flags) | |
acc3c8d1 KS |
2202 | { |
2203 | struct mm_struct *mm = current->mm; | |
d3649f68 CH |
2204 | int locked = 1; |
2205 | long ret; | |
acc3c8d1 | 2206 | |
d3649f68 CH |
2207 | /* |
2208 | * FIXME: Current FOLL_LONGTERM behavior is incompatible with | |
2209 | * FAULT_FLAG_ALLOW_RETRY because of the FS DAX check requirement on | |
2210 | * vmas. As there are no users of this flag in this call we simply | |
2211 | * disallow this option for now. | |
2212 | */ | |
2213 | if (WARN_ON_ONCE(gup_flags & FOLL_LONGTERM)) | |
2214 | return -EINVAL; | |
acc3c8d1 | 2215 | |
d8ed45c5 | 2216 | mmap_read_lock(mm); |
64019a2e | 2217 | ret = __get_user_pages_locked(mm, start, nr_pages, pages, NULL, |
d3649f68 | 2218 | &locked, gup_flags | FOLL_TOUCH); |
acc3c8d1 | 2219 | if (locked) |
d8ed45c5 | 2220 | mmap_read_unlock(mm); |
d3649f68 | 2221 | return ret; |
4bbd4c77 | 2222 | } |
d3649f68 | 2223 | EXPORT_SYMBOL(get_user_pages_unlocked); |
2667f50e SC |
2224 | |
2225 | /* | |
67a929e0 | 2226 | * Fast GUP |
2667f50e SC |
2227 | * |
2228 | * get_user_pages_fast attempts to pin user pages by walking the page | |
2229 | * tables directly and avoids taking locks. Thus the walker needs to be | |
2230 | * protected from page table pages being freed from under it, and should | |
2231 | * block any THP splits. | |
2232 | * | |
2233 | * One way to achieve this is to have the walker disable interrupts, and | |
2234 | * rely on IPIs from the TLB flushing code blocking before the page table | |
2235 | * pages are freed. This is unsuitable for architectures that do not need | |
2236 | * to broadcast an IPI when invalidating TLBs. | |
2237 | * | |
2238 | * Another way to achieve this is to batch up page table containing pages | |
2239 | * belonging to more than one mm_user, then rcu_sched a callback to free those | |
2240 | * pages. Disabling interrupts will allow the fast_gup walker to both block | |
2241 | * the rcu_sched callback, and an IPI that we broadcast for splitting THPs | |
2242 | * (which is a relatively rare event). The code below adopts this strategy. | |
2243 | * | |
2244 | * Before activating this code, please be aware that the following assumptions | |
2245 | * are currently made: | |
2246 | * | |
ff2e6d72 | 2247 | * *) Either MMU_GATHER_RCU_TABLE_FREE is enabled, and tlb_remove_table() is used to |
e585513b | 2248 | * free pages containing page tables or TLB flushing requires IPI broadcast. |
2667f50e | 2249 | * |
2667f50e SC |
2250 | * *) ptes can be read atomically by the architecture. |
2251 | * | |
2252 | * *) access_ok is sufficient to validate userspace address ranges. | |
2253 | * | |
2254 | * The last two assumptions can be relaxed by the addition of helper functions. | |
2255 | * | |
2256 | * This code is based heavily on the PowerPC implementation by Nick Piggin. | |
2257 | */ | |
67a929e0 | 2258 | #ifdef CONFIG_HAVE_FAST_GUP |
3faa52c0 | 2259 | |
790c7369 | 2260 | static void __maybe_unused undo_dev_pagemap(int *nr, int nr_start, |
3b78d834 | 2261 | unsigned int flags, |
790c7369 | 2262 | struct page **pages) |
b59f65fa KS |
2263 | { |
2264 | while ((*nr) - nr_start) { | |
2265 | struct page *page = pages[--(*nr)]; | |
2266 | ||
2267 | ClearPageReferenced(page); | |
3faa52c0 JH |
2268 | if (flags & FOLL_PIN) |
2269 | unpin_user_page(page); | |
2270 | else | |
2271 | put_page(page); | |
b59f65fa KS |
2272 | } |
2273 | } | |
2274 | ||
3010a5ea | 2275 | #ifdef CONFIG_ARCH_HAS_PTE_SPECIAL |
2667f50e | 2276 | static int gup_pte_range(pmd_t pmd, unsigned long addr, unsigned long end, |
b798bec4 | 2277 | unsigned int flags, struct page **pages, int *nr) |
2667f50e | 2278 | { |
b59f65fa KS |
2279 | struct dev_pagemap *pgmap = NULL; |
2280 | int nr_start = *nr, ret = 0; | |
2667f50e | 2281 | pte_t *ptep, *ptem; |
2667f50e SC |
2282 | |
2283 | ptem = ptep = pte_offset_map(&pmd, addr); | |
2284 | do { | |
2a4a06da | 2285 | pte_t pte = ptep_get_lockless(ptep); |
7aef4172 | 2286 | struct page *head, *page; |
2667f50e SC |
2287 | |
2288 | /* | |
2289 | * Similar to the PMD case below, NUMA hinting must take slow | |
8a0516ed | 2290 | * path using the pte_protnone check. |
2667f50e | 2291 | */ |
e7884f8e KS |
2292 | if (pte_protnone(pte)) |
2293 | goto pte_unmap; | |
2294 | ||
b798bec4 | 2295 | if (!pte_access_permitted(pte, flags & FOLL_WRITE)) |
e7884f8e KS |
2296 | goto pte_unmap; |
2297 | ||
b59f65fa | 2298 | if (pte_devmap(pte)) { |
7af75561 IW |
2299 | if (unlikely(flags & FOLL_LONGTERM)) |
2300 | goto pte_unmap; | |
2301 | ||
b59f65fa KS |
2302 | pgmap = get_dev_pagemap(pte_pfn(pte), pgmap); |
2303 | if (unlikely(!pgmap)) { | |
3b78d834 | 2304 | undo_dev_pagemap(nr, nr_start, flags, pages); |
b59f65fa KS |
2305 | goto pte_unmap; |
2306 | } | |
2307 | } else if (pte_special(pte)) | |
2667f50e SC |
2308 | goto pte_unmap; |
2309 | ||
2310 | VM_BUG_ON(!pfn_valid(pte_pfn(pte))); | |
2311 | page = pte_page(pte); | |
2312 | ||
3faa52c0 | 2313 | head = try_grab_compound_head(page, 1, flags); |
8fde12ca | 2314 | if (!head) |
2667f50e SC |
2315 | goto pte_unmap; |
2316 | ||
1507f512 MR |
2317 | if (unlikely(page_is_secretmem(page))) { |
2318 | put_compound_head(head, 1, flags); | |
2319 | goto pte_unmap; | |
2320 | } | |
2321 | ||
2667f50e | 2322 | if (unlikely(pte_val(pte) != pte_val(*ptep))) { |
3faa52c0 | 2323 | put_compound_head(head, 1, flags); |
2667f50e SC |
2324 | goto pte_unmap; |
2325 | } | |
2326 | ||
7aef4172 | 2327 | VM_BUG_ON_PAGE(compound_head(page) != head, page); |
e9348053 | 2328 | |
f28d4363 CI |
2329 | /* |
2330 | * We need to make the page accessible if and only if we are | |
2331 | * going to access its content (the FOLL_PIN case). Please | |
2332 | * see Documentation/core-api/pin_user_pages.rst for | |
2333 | * details. | |
2334 | */ | |
2335 | if (flags & FOLL_PIN) { | |
2336 | ret = arch_make_page_accessible(page); | |
2337 | if (ret) { | |
2338 | unpin_user_page(page); | |
2339 | goto pte_unmap; | |
2340 | } | |
2341 | } | |
e9348053 | 2342 | SetPageReferenced(page); |
2667f50e SC |
2343 | pages[*nr] = page; |
2344 | (*nr)++; | |
2345 | ||
2346 | } while (ptep++, addr += PAGE_SIZE, addr != end); | |
2347 | ||
2348 | ret = 1; | |
2349 | ||
2350 | pte_unmap: | |
832d7aa0 CH |
2351 | if (pgmap) |
2352 | put_dev_pagemap(pgmap); | |
2667f50e SC |
2353 | pte_unmap(ptem); |
2354 | return ret; | |
2355 | } | |
2356 | #else | |
2357 | ||
2358 | /* | |
2359 | * If we can't determine whether or not a pte is special, then fail immediately | |
2360 | * for ptes. Note, we can still pin HugeTLB and THP as these are guaranteed not | |
2361 | * to be special. | |
2362 | * | |
2363 | * For a futex to be placed on a THP tail page, get_futex_key requires a | |
dadbb612 | 2364 | * get_user_pages_fast_only implementation that can pin pages. Thus it's still |
2667f50e SC |
2365 | * useful to have gup_huge_pmd even if we can't operate on ptes. |
2366 | */ | |
2367 | static int gup_pte_range(pmd_t pmd, unsigned long addr, unsigned long end, | |
b798bec4 | 2368 | unsigned int flags, struct page **pages, int *nr) |
2667f50e SC |
2369 | { |
2370 | return 0; | |
2371 | } | |
3010a5ea | 2372 | #endif /* CONFIG_ARCH_HAS_PTE_SPECIAL */ |
2667f50e | 2373 | |
17596731 | 2374 | #if defined(CONFIG_ARCH_HAS_PTE_DEVMAP) && defined(CONFIG_TRANSPARENT_HUGEPAGE) |
b59f65fa | 2375 | static int __gup_device_huge(unsigned long pfn, unsigned long addr, |
86dfbed4 JH |
2376 | unsigned long end, unsigned int flags, |
2377 | struct page **pages, int *nr) | |
b59f65fa KS |
2378 | { |
2379 | int nr_start = *nr; | |
2380 | struct dev_pagemap *pgmap = NULL; | |
2381 | ||
2382 | do { | |
2383 | struct page *page = pfn_to_page(pfn); | |
2384 | ||
2385 | pgmap = get_dev_pagemap(pfn, pgmap); | |
2386 | if (unlikely(!pgmap)) { | |
3b78d834 | 2387 | undo_dev_pagemap(nr, nr_start, flags, pages); |
6401c4eb | 2388 | break; |
b59f65fa KS |
2389 | } |
2390 | SetPageReferenced(page); | |
2391 | pages[*nr] = page; | |
3faa52c0 JH |
2392 | if (unlikely(!try_grab_page(page, flags))) { |
2393 | undo_dev_pagemap(nr, nr_start, flags, pages); | |
6401c4eb | 2394 | break; |
3faa52c0 | 2395 | } |
b59f65fa KS |
2396 | (*nr)++; |
2397 | pfn++; | |
2398 | } while (addr += PAGE_SIZE, addr != end); | |
832d7aa0 | 2399 | |
6401c4eb | 2400 | put_dev_pagemap(pgmap); |
20b7fee7 | 2401 | return addr == end; |
b59f65fa KS |
2402 | } |
2403 | ||
a9b6de77 | 2404 | static int __gup_device_huge_pmd(pmd_t orig, pmd_t *pmdp, unsigned long addr, |
86dfbed4 JH |
2405 | unsigned long end, unsigned int flags, |
2406 | struct page **pages, int *nr) | |
b59f65fa KS |
2407 | { |
2408 | unsigned long fault_pfn; | |
a9b6de77 DW |
2409 | int nr_start = *nr; |
2410 | ||
2411 | fault_pfn = pmd_pfn(orig) + ((addr & ~PMD_MASK) >> PAGE_SHIFT); | |
86dfbed4 | 2412 | if (!__gup_device_huge(fault_pfn, addr, end, flags, pages, nr)) |
a9b6de77 | 2413 | return 0; |
b59f65fa | 2414 | |
a9b6de77 | 2415 | if (unlikely(pmd_val(orig) != pmd_val(*pmdp))) { |
3b78d834 | 2416 | undo_dev_pagemap(nr, nr_start, flags, pages); |
a9b6de77 DW |
2417 | return 0; |
2418 | } | |
2419 | return 1; | |
b59f65fa KS |
2420 | } |
2421 | ||
a9b6de77 | 2422 | static int __gup_device_huge_pud(pud_t orig, pud_t *pudp, unsigned long addr, |
86dfbed4 JH |
2423 | unsigned long end, unsigned int flags, |
2424 | struct page **pages, int *nr) | |
b59f65fa KS |
2425 | { |
2426 | unsigned long fault_pfn; | |
a9b6de77 DW |
2427 | int nr_start = *nr; |
2428 | ||
2429 | fault_pfn = pud_pfn(orig) + ((addr & ~PUD_MASK) >> PAGE_SHIFT); | |
86dfbed4 | 2430 | if (!__gup_device_huge(fault_pfn, addr, end, flags, pages, nr)) |
a9b6de77 | 2431 | return 0; |
b59f65fa | 2432 | |
a9b6de77 | 2433 | if (unlikely(pud_val(orig) != pud_val(*pudp))) { |
3b78d834 | 2434 | undo_dev_pagemap(nr, nr_start, flags, pages); |
a9b6de77 DW |
2435 | return 0; |
2436 | } | |
2437 | return 1; | |
b59f65fa KS |
2438 | } |
2439 | #else | |
a9b6de77 | 2440 | static int __gup_device_huge_pmd(pmd_t orig, pmd_t *pmdp, unsigned long addr, |
86dfbed4 JH |
2441 | unsigned long end, unsigned int flags, |
2442 | struct page **pages, int *nr) | |
b59f65fa KS |
2443 | { |
2444 | BUILD_BUG(); | |
2445 | return 0; | |
2446 | } | |
2447 | ||
a9b6de77 | 2448 | static int __gup_device_huge_pud(pud_t pud, pud_t *pudp, unsigned long addr, |
86dfbed4 JH |
2449 | unsigned long end, unsigned int flags, |
2450 | struct page **pages, int *nr) | |
b59f65fa KS |
2451 | { |
2452 | BUILD_BUG(); | |
2453 | return 0; | |
2454 | } | |
2455 | #endif | |
2456 | ||
a43e9820 JH |
2457 | static int record_subpages(struct page *page, unsigned long addr, |
2458 | unsigned long end, struct page **pages) | |
2459 | { | |
2460 | int nr; | |
2461 | ||
2462 | for (nr = 0; addr != end; addr += PAGE_SIZE) | |
2463 | pages[nr++] = page++; | |
2464 | ||
2465 | return nr; | |
2466 | } | |
2467 | ||
cbd34da7 CH |
2468 | #ifdef CONFIG_ARCH_HAS_HUGEPD |
2469 | static unsigned long hugepte_addr_end(unsigned long addr, unsigned long end, | |
2470 | unsigned long sz) | |
2471 | { | |
2472 | unsigned long __boundary = (addr + sz) & ~(sz-1); | |
2473 | return (__boundary - 1 < end - 1) ? __boundary : end; | |
2474 | } | |
2475 | ||
2476 | static int gup_hugepte(pte_t *ptep, unsigned long sz, unsigned long addr, | |
0cd22afd JH |
2477 | unsigned long end, unsigned int flags, |
2478 | struct page **pages, int *nr) | |
cbd34da7 CH |
2479 | { |
2480 | unsigned long pte_end; | |
2481 | struct page *head, *page; | |
2482 | pte_t pte; | |
2483 | int refs; | |
2484 | ||
2485 | pte_end = (addr + sz) & ~(sz-1); | |
2486 | if (pte_end < end) | |
2487 | end = pte_end; | |
2488 | ||
55ca2263 | 2489 | pte = huge_ptep_get(ptep); |
cbd34da7 | 2490 | |
0cd22afd | 2491 | if (!pte_access_permitted(pte, flags & FOLL_WRITE)) |
cbd34da7 CH |
2492 | return 0; |
2493 | ||
2494 | /* hugepages are never "special" */ | |
2495 | VM_BUG_ON(!pfn_valid(pte_pfn(pte))); | |
2496 | ||
cbd34da7 | 2497 | head = pte_page(pte); |
cbd34da7 | 2498 | page = head + ((addr & (sz-1)) >> PAGE_SHIFT); |
a43e9820 | 2499 | refs = record_subpages(page, addr, end, pages + *nr); |
cbd34da7 | 2500 | |
3faa52c0 | 2501 | head = try_grab_compound_head(head, refs, flags); |
a43e9820 | 2502 | if (!head) |
cbd34da7 | 2503 | return 0; |
cbd34da7 CH |
2504 | |
2505 | if (unlikely(pte_val(pte) != pte_val(*ptep))) { | |
3b78d834 | 2506 | put_compound_head(head, refs, flags); |
cbd34da7 CH |
2507 | return 0; |
2508 | } | |
2509 | ||
a43e9820 | 2510 | *nr += refs; |
520b4a44 | 2511 | SetPageReferenced(head); |
cbd34da7 CH |
2512 | return 1; |
2513 | } | |
2514 | ||
2515 | static int gup_huge_pd(hugepd_t hugepd, unsigned long addr, | |
0cd22afd | 2516 | unsigned int pdshift, unsigned long end, unsigned int flags, |
cbd34da7 CH |
2517 | struct page **pages, int *nr) |
2518 | { | |
2519 | pte_t *ptep; | |
2520 | unsigned long sz = 1UL << hugepd_shift(hugepd); | |
2521 | unsigned long next; | |
2522 | ||
2523 | ptep = hugepte_offset(hugepd, addr, pdshift); | |
2524 | do { | |
2525 | next = hugepte_addr_end(addr, end, sz); | |
0cd22afd | 2526 | if (!gup_hugepte(ptep, sz, addr, end, flags, pages, nr)) |
cbd34da7 CH |
2527 | return 0; |
2528 | } while (ptep++, addr = next, addr != end); | |
2529 | ||
2530 | return 1; | |
2531 | } | |
2532 | #else | |
2533 | static inline int gup_huge_pd(hugepd_t hugepd, unsigned long addr, | |
0cd22afd | 2534 | unsigned int pdshift, unsigned long end, unsigned int flags, |
cbd34da7 CH |
2535 | struct page **pages, int *nr) |
2536 | { | |
2537 | return 0; | |
2538 | } | |
2539 | #endif /* CONFIG_ARCH_HAS_HUGEPD */ | |
2540 | ||
2667f50e | 2541 | static int gup_huge_pmd(pmd_t orig, pmd_t *pmdp, unsigned long addr, |
0cd22afd JH |
2542 | unsigned long end, unsigned int flags, |
2543 | struct page **pages, int *nr) | |
2667f50e | 2544 | { |
ddc58f27 | 2545 | struct page *head, *page; |
2667f50e SC |
2546 | int refs; |
2547 | ||
b798bec4 | 2548 | if (!pmd_access_permitted(orig, flags & FOLL_WRITE)) |
2667f50e SC |
2549 | return 0; |
2550 | ||
7af75561 IW |
2551 | if (pmd_devmap(orig)) { |
2552 | if (unlikely(flags & FOLL_LONGTERM)) | |
2553 | return 0; | |
86dfbed4 JH |
2554 | return __gup_device_huge_pmd(orig, pmdp, addr, end, flags, |
2555 | pages, nr); | |
7af75561 | 2556 | } |
b59f65fa | 2557 | |
d63206ee | 2558 | page = pmd_page(orig) + ((addr & ~PMD_MASK) >> PAGE_SHIFT); |
a43e9820 | 2559 | refs = record_subpages(page, addr, end, pages + *nr); |
2667f50e | 2560 | |
3faa52c0 | 2561 | head = try_grab_compound_head(pmd_page(orig), refs, flags); |
a43e9820 | 2562 | if (!head) |
2667f50e | 2563 | return 0; |
2667f50e SC |
2564 | |
2565 | if (unlikely(pmd_val(orig) != pmd_val(*pmdp))) { | |
3b78d834 | 2566 | put_compound_head(head, refs, flags); |
2667f50e SC |
2567 | return 0; |
2568 | } | |
2569 | ||
a43e9820 | 2570 | *nr += refs; |
e9348053 | 2571 | SetPageReferenced(head); |
2667f50e SC |
2572 | return 1; |
2573 | } | |
2574 | ||
2575 | static int gup_huge_pud(pud_t orig, pud_t *pudp, unsigned long addr, | |
86dfbed4 JH |
2576 | unsigned long end, unsigned int flags, |
2577 | struct page **pages, int *nr) | |
2667f50e | 2578 | { |
ddc58f27 | 2579 | struct page *head, *page; |
2667f50e SC |
2580 | int refs; |
2581 | ||
b798bec4 | 2582 | if (!pud_access_permitted(orig, flags & FOLL_WRITE)) |
2667f50e SC |
2583 | return 0; |
2584 | ||
7af75561 IW |
2585 | if (pud_devmap(orig)) { |
2586 | if (unlikely(flags & FOLL_LONGTERM)) | |
2587 | return 0; | |
86dfbed4 JH |
2588 | return __gup_device_huge_pud(orig, pudp, addr, end, flags, |
2589 | pages, nr); | |
7af75561 | 2590 | } |
b59f65fa | 2591 | |
d63206ee | 2592 | page = pud_page(orig) + ((addr & ~PUD_MASK) >> PAGE_SHIFT); |
a43e9820 | 2593 | refs = record_subpages(page, addr, end, pages + *nr); |
2667f50e | 2594 | |
3faa52c0 | 2595 | head = try_grab_compound_head(pud_page(orig), refs, flags); |
a43e9820 | 2596 | if (!head) |
2667f50e | 2597 | return 0; |
2667f50e SC |
2598 | |
2599 | if (unlikely(pud_val(orig) != pud_val(*pudp))) { | |
3b78d834 | 2600 | put_compound_head(head, refs, flags); |
2667f50e SC |
2601 | return 0; |
2602 | } | |
2603 | ||
a43e9820 | 2604 | *nr += refs; |
e9348053 | 2605 | SetPageReferenced(head); |
2667f50e SC |
2606 | return 1; |
2607 | } | |
2608 | ||
f30c59e9 | 2609 | static int gup_huge_pgd(pgd_t orig, pgd_t *pgdp, unsigned long addr, |
b798bec4 | 2610 | unsigned long end, unsigned int flags, |
f30c59e9 AK |
2611 | struct page **pages, int *nr) |
2612 | { | |
2613 | int refs; | |
ddc58f27 | 2614 | struct page *head, *page; |
f30c59e9 | 2615 | |
b798bec4 | 2616 | if (!pgd_access_permitted(orig, flags & FOLL_WRITE)) |
f30c59e9 AK |
2617 | return 0; |
2618 | ||
b59f65fa | 2619 | BUILD_BUG_ON(pgd_devmap(orig)); |
a43e9820 | 2620 | |
d63206ee | 2621 | page = pgd_page(orig) + ((addr & ~PGDIR_MASK) >> PAGE_SHIFT); |
a43e9820 | 2622 | refs = record_subpages(page, addr, end, pages + *nr); |
f30c59e9 | 2623 | |
3faa52c0 | 2624 | head = try_grab_compound_head(pgd_page(orig), refs, flags); |
a43e9820 | 2625 | if (!head) |
f30c59e9 | 2626 | return 0; |
f30c59e9 AK |
2627 | |
2628 | if (unlikely(pgd_val(orig) != pgd_val(*pgdp))) { | |
3b78d834 | 2629 | put_compound_head(head, refs, flags); |
f30c59e9 AK |
2630 | return 0; |
2631 | } | |
2632 | ||
a43e9820 | 2633 | *nr += refs; |
e9348053 | 2634 | SetPageReferenced(head); |
f30c59e9 AK |
2635 | return 1; |
2636 | } | |
2637 | ||
d3f7b1bb | 2638 | static int gup_pmd_range(pud_t *pudp, pud_t pud, unsigned long addr, unsigned long end, |
b798bec4 | 2639 | unsigned int flags, struct page **pages, int *nr) |
2667f50e SC |
2640 | { |
2641 | unsigned long next; | |
2642 | pmd_t *pmdp; | |
2643 | ||
d3f7b1bb | 2644 | pmdp = pmd_offset_lockless(pudp, pud, addr); |
2667f50e | 2645 | do { |
38c5ce93 | 2646 | pmd_t pmd = READ_ONCE(*pmdp); |
2667f50e SC |
2647 | |
2648 | next = pmd_addr_end(addr, end); | |
84c3fc4e | 2649 | if (!pmd_present(pmd)) |
2667f50e SC |
2650 | return 0; |
2651 | ||
414fd080 YZ |
2652 | if (unlikely(pmd_trans_huge(pmd) || pmd_huge(pmd) || |
2653 | pmd_devmap(pmd))) { | |
2667f50e SC |
2654 | /* |
2655 | * NUMA hinting faults need to be handled in the GUP | |
2656 | * slowpath for accounting purposes and so that they | |
2657 | * can be serialised against THP migration. | |
2658 | */ | |
8a0516ed | 2659 | if (pmd_protnone(pmd)) |
2667f50e SC |
2660 | return 0; |
2661 | ||
b798bec4 | 2662 | if (!gup_huge_pmd(pmd, pmdp, addr, next, flags, |
2667f50e SC |
2663 | pages, nr)) |
2664 | return 0; | |
2665 | ||
f30c59e9 AK |
2666 | } else if (unlikely(is_hugepd(__hugepd(pmd_val(pmd))))) { |
2667 | /* | |
2668 | * architecture have different format for hugetlbfs | |
2669 | * pmd format and THP pmd format | |
2670 | */ | |
2671 | if (!gup_huge_pd(__hugepd(pmd_val(pmd)), addr, | |
b798bec4 | 2672 | PMD_SHIFT, next, flags, pages, nr)) |
f30c59e9 | 2673 | return 0; |
b798bec4 | 2674 | } else if (!gup_pte_range(pmd, addr, next, flags, pages, nr)) |
2923117b | 2675 | return 0; |
2667f50e SC |
2676 | } while (pmdp++, addr = next, addr != end); |
2677 | ||
2678 | return 1; | |
2679 | } | |
2680 | ||
d3f7b1bb | 2681 | static int gup_pud_range(p4d_t *p4dp, p4d_t p4d, unsigned long addr, unsigned long end, |
b798bec4 | 2682 | unsigned int flags, struct page **pages, int *nr) |
2667f50e SC |
2683 | { |
2684 | unsigned long next; | |
2685 | pud_t *pudp; | |
2686 | ||
d3f7b1bb | 2687 | pudp = pud_offset_lockless(p4dp, p4d, addr); |
2667f50e | 2688 | do { |
e37c6982 | 2689 | pud_t pud = READ_ONCE(*pudp); |
2667f50e SC |
2690 | |
2691 | next = pud_addr_end(addr, end); | |
15494520 | 2692 | if (unlikely(!pud_present(pud))) |
2667f50e | 2693 | return 0; |
f30c59e9 | 2694 | if (unlikely(pud_huge(pud))) { |
b798bec4 | 2695 | if (!gup_huge_pud(pud, pudp, addr, next, flags, |
f30c59e9 AK |
2696 | pages, nr)) |
2697 | return 0; | |
2698 | } else if (unlikely(is_hugepd(__hugepd(pud_val(pud))))) { | |
2699 | if (!gup_huge_pd(__hugepd(pud_val(pud)), addr, | |
b798bec4 | 2700 | PUD_SHIFT, next, flags, pages, nr)) |
2667f50e | 2701 | return 0; |
d3f7b1bb | 2702 | } else if (!gup_pmd_range(pudp, pud, addr, next, flags, pages, nr)) |
2667f50e SC |
2703 | return 0; |
2704 | } while (pudp++, addr = next, addr != end); | |
2705 | ||
2706 | return 1; | |
2707 | } | |
2708 | ||
d3f7b1bb | 2709 | static int gup_p4d_range(pgd_t *pgdp, pgd_t pgd, unsigned long addr, unsigned long end, |
b798bec4 | 2710 | unsigned int flags, struct page **pages, int *nr) |
c2febafc KS |
2711 | { |
2712 | unsigned long next; | |
2713 | p4d_t *p4dp; | |
2714 | ||
d3f7b1bb | 2715 | p4dp = p4d_offset_lockless(pgdp, pgd, addr); |
c2febafc KS |
2716 | do { |
2717 | p4d_t p4d = READ_ONCE(*p4dp); | |
2718 | ||
2719 | next = p4d_addr_end(addr, end); | |
2720 | if (p4d_none(p4d)) | |
2721 | return 0; | |
2722 | BUILD_BUG_ON(p4d_huge(p4d)); | |
2723 | if (unlikely(is_hugepd(__hugepd(p4d_val(p4d))))) { | |
2724 | if (!gup_huge_pd(__hugepd(p4d_val(p4d)), addr, | |
b798bec4 | 2725 | P4D_SHIFT, next, flags, pages, nr)) |
c2febafc | 2726 | return 0; |
d3f7b1bb | 2727 | } else if (!gup_pud_range(p4dp, p4d, addr, next, flags, pages, nr)) |
c2febafc KS |
2728 | return 0; |
2729 | } while (p4dp++, addr = next, addr != end); | |
2730 | ||
2731 | return 1; | |
2732 | } | |
2733 | ||
5b65c467 | 2734 | static void gup_pgd_range(unsigned long addr, unsigned long end, |
b798bec4 | 2735 | unsigned int flags, struct page **pages, int *nr) |
5b65c467 KS |
2736 | { |
2737 | unsigned long next; | |
2738 | pgd_t *pgdp; | |
2739 | ||
2740 | pgdp = pgd_offset(current->mm, addr); | |
2741 | do { | |
2742 | pgd_t pgd = READ_ONCE(*pgdp); | |
2743 | ||
2744 | next = pgd_addr_end(addr, end); | |
2745 | if (pgd_none(pgd)) | |
2746 | return; | |
2747 | if (unlikely(pgd_huge(pgd))) { | |
b798bec4 | 2748 | if (!gup_huge_pgd(pgd, pgdp, addr, next, flags, |
5b65c467 KS |
2749 | pages, nr)) |
2750 | return; | |
2751 | } else if (unlikely(is_hugepd(__hugepd(pgd_val(pgd))))) { | |
2752 | if (!gup_huge_pd(__hugepd(pgd_val(pgd)), addr, | |
b798bec4 | 2753 | PGDIR_SHIFT, next, flags, pages, nr)) |
5b65c467 | 2754 | return; |
d3f7b1bb | 2755 | } else if (!gup_p4d_range(pgdp, pgd, addr, next, flags, pages, nr)) |
5b65c467 KS |
2756 | return; |
2757 | } while (pgdp++, addr = next, addr != end); | |
2758 | } | |
050a9adc CH |
2759 | #else |
2760 | static inline void gup_pgd_range(unsigned long addr, unsigned long end, | |
2761 | unsigned int flags, struct page **pages, int *nr) | |
2762 | { | |
2763 | } | |
2764 | #endif /* CONFIG_HAVE_FAST_GUP */ | |
5b65c467 KS |
2765 | |
2766 | #ifndef gup_fast_permitted | |
2767 | /* | |
dadbb612 | 2768 | * Check if it's allowed to use get_user_pages_fast_only() for the range, or |
5b65c467 KS |
2769 | * we need to fall back to the slow version: |
2770 | */ | |
26f4c328 | 2771 | static bool gup_fast_permitted(unsigned long start, unsigned long end) |
5b65c467 | 2772 | { |
26f4c328 | 2773 | return true; |
5b65c467 KS |
2774 | } |
2775 | #endif | |
2776 | ||
7af75561 IW |
2777 | static int __gup_longterm_unlocked(unsigned long start, int nr_pages, |
2778 | unsigned int gup_flags, struct page **pages) | |
2779 | { | |
2780 | int ret; | |
2781 | ||
2782 | /* | |
2783 | * FIXME: FOLL_LONGTERM does not work with | |
2784 | * get_user_pages_unlocked() (see comments in that function) | |
2785 | */ | |
2786 | if (gup_flags & FOLL_LONGTERM) { | |
d8ed45c5 | 2787 | mmap_read_lock(current->mm); |
64019a2e | 2788 | ret = __gup_longterm_locked(current->mm, |
7af75561 IW |
2789 | start, nr_pages, |
2790 | pages, NULL, gup_flags); | |
d8ed45c5 | 2791 | mmap_read_unlock(current->mm); |
7af75561 IW |
2792 | } else { |
2793 | ret = get_user_pages_unlocked(start, nr_pages, | |
2794 | pages, gup_flags); | |
2795 | } | |
2796 | ||
2797 | return ret; | |
2798 | } | |
2799 | ||
c28b1fc7 JG |
2800 | static unsigned long lockless_pages_from_mm(unsigned long start, |
2801 | unsigned long end, | |
2802 | unsigned int gup_flags, | |
2803 | struct page **pages) | |
2804 | { | |
2805 | unsigned long flags; | |
2806 | int nr_pinned = 0; | |
57efa1fe | 2807 | unsigned seq; |
c28b1fc7 JG |
2808 | |
2809 | if (!IS_ENABLED(CONFIG_HAVE_FAST_GUP) || | |
2810 | !gup_fast_permitted(start, end)) | |
2811 | return 0; | |
2812 | ||
57efa1fe JG |
2813 | if (gup_flags & FOLL_PIN) { |
2814 | seq = raw_read_seqcount(¤t->mm->write_protect_seq); | |
2815 | if (seq & 1) | |
2816 | return 0; | |
2817 | } | |
2818 | ||
c28b1fc7 JG |
2819 | /* |
2820 | * Disable interrupts. The nested form is used, in order to allow full, | |
2821 | * general purpose use of this routine. | |
2822 | * | |
2823 | * With interrupts disabled, we block page table pages from being freed | |
2824 | * from under us. See struct mmu_table_batch comments in | |
2825 | * include/asm-generic/tlb.h for more details. | |
2826 | * | |
2827 | * We do not adopt an rcu_read_lock() here as we also want to block IPIs | |
2828 | * that come from THPs splitting. | |
2829 | */ | |
2830 | local_irq_save(flags); | |
2831 | gup_pgd_range(start, end, gup_flags, pages, &nr_pinned); | |
2832 | local_irq_restore(flags); | |
57efa1fe JG |
2833 | |
2834 | /* | |
2835 | * When pinning pages for DMA there could be a concurrent write protect | |
2836 | * from fork() via copy_page_range(), in this case always fail fast GUP. | |
2837 | */ | |
2838 | if (gup_flags & FOLL_PIN) { | |
2839 | if (read_seqcount_retry(¤t->mm->write_protect_seq, seq)) { | |
2840 | unpin_user_pages(pages, nr_pinned); | |
2841 | return 0; | |
2842 | } | |
2843 | } | |
c28b1fc7 JG |
2844 | return nr_pinned; |
2845 | } | |
2846 | ||
2847 | static int internal_get_user_pages_fast(unsigned long start, | |
2848 | unsigned long nr_pages, | |
eddb1c22 JH |
2849 | unsigned int gup_flags, |
2850 | struct page **pages) | |
2667f50e | 2851 | { |
c28b1fc7 JG |
2852 | unsigned long len, end; |
2853 | unsigned long nr_pinned; | |
2854 | int ret; | |
2667f50e | 2855 | |
f4000fdf | 2856 | if (WARN_ON_ONCE(gup_flags & ~(FOLL_WRITE | FOLL_LONGTERM | |
376a34ef | 2857 | FOLL_FORCE | FOLL_PIN | FOLL_GET | |
55b8fe70 | 2858 | FOLL_FAST_ONLY | FOLL_NOFAULT))) |
817be129 CH |
2859 | return -EINVAL; |
2860 | ||
a458b76a AA |
2861 | if (gup_flags & FOLL_PIN) |
2862 | mm_set_has_pinned_flag(¤t->mm->flags); | |
008cfe44 | 2863 | |
f81cd178 | 2864 | if (!(gup_flags & FOLL_FAST_ONLY)) |
da1c55f1 | 2865 | might_lock_read(¤t->mm->mmap_lock); |
f81cd178 | 2866 | |
f455c854 | 2867 | start = untagged_addr(start) & PAGE_MASK; |
c28b1fc7 JG |
2868 | len = nr_pages << PAGE_SHIFT; |
2869 | if (check_add_overflow(start, len, &end)) | |
c61611f7 | 2870 | return 0; |
96d4f267 | 2871 | if (unlikely(!access_ok((void __user *)start, len))) |
c61611f7 | 2872 | return -EFAULT; |
73e10a61 | 2873 | |
c28b1fc7 JG |
2874 | nr_pinned = lockless_pages_from_mm(start, end, gup_flags, pages); |
2875 | if (nr_pinned == nr_pages || gup_flags & FOLL_FAST_ONLY) | |
2876 | return nr_pinned; | |
2667f50e | 2877 | |
c28b1fc7 JG |
2878 | /* Slow path: try to get the remaining pages with get_user_pages */ |
2879 | start += nr_pinned << PAGE_SHIFT; | |
2880 | pages += nr_pinned; | |
2881 | ret = __gup_longterm_unlocked(start, nr_pages - nr_pinned, gup_flags, | |
2882 | pages); | |
2883 | if (ret < 0) { | |
2884 | /* | |
2885 | * The caller has to unpin the pages we already pinned so | |
2886 | * returning -errno is not an option | |
2887 | */ | |
2888 | if (nr_pinned) | |
2889 | return nr_pinned; | |
2890 | return ret; | |
2667f50e | 2891 | } |
c28b1fc7 | 2892 | return ret + nr_pinned; |
2667f50e | 2893 | } |
c28b1fc7 | 2894 | |
dadbb612 SJ |
2895 | /** |
2896 | * get_user_pages_fast_only() - pin user pages in memory | |
2897 | * @start: starting user address | |
2898 | * @nr_pages: number of pages from start to pin | |
2899 | * @gup_flags: flags modifying pin behaviour | |
2900 | * @pages: array that receives pointers to the pages pinned. | |
2901 | * Should be at least nr_pages long. | |
2902 | * | |
9e1f0580 JH |
2903 | * Like get_user_pages_fast() except it's IRQ-safe in that it won't fall back to |
2904 | * the regular GUP. | |
2905 | * Note a difference with get_user_pages_fast: this always returns the | |
2906 | * number of pages pinned, 0 if no pages were pinned. | |
2907 | * | |
2908 | * If the architecture does not support this function, simply return with no | |
2909 | * pages pinned. | |
2910 | * | |
2911 | * Careful, careful! COW breaking can go either way, so a non-write | |
2912 | * access can get ambiguous page results. If you call this function without | |
2913 | * 'write' set, you'd better be sure that you're ok with that ambiguity. | |
2914 | */ | |
dadbb612 SJ |
2915 | int get_user_pages_fast_only(unsigned long start, int nr_pages, |
2916 | unsigned int gup_flags, struct page **pages) | |
9e1f0580 | 2917 | { |
376a34ef | 2918 | int nr_pinned; |
9e1f0580 JH |
2919 | /* |
2920 | * Internally (within mm/gup.c), gup fast variants must set FOLL_GET, | |
2921 | * because gup fast is always a "pin with a +1 page refcount" request. | |
376a34ef JH |
2922 | * |
2923 | * FOLL_FAST_ONLY is required in order to match the API description of | |
2924 | * this routine: no fall back to regular ("slow") GUP. | |
9e1f0580 | 2925 | */ |
dadbb612 | 2926 | gup_flags |= FOLL_GET | FOLL_FAST_ONLY; |
9e1f0580 | 2927 | |
376a34ef JH |
2928 | nr_pinned = internal_get_user_pages_fast(start, nr_pages, gup_flags, |
2929 | pages); | |
9e1f0580 JH |
2930 | |
2931 | /* | |
376a34ef JH |
2932 | * As specified in the API description above, this routine is not |
2933 | * allowed to return negative values. However, the common core | |
2934 | * routine internal_get_user_pages_fast() *can* return -errno. | |
2935 | * Therefore, correct for that here: | |
9e1f0580 | 2936 | */ |
376a34ef JH |
2937 | if (nr_pinned < 0) |
2938 | nr_pinned = 0; | |
9e1f0580 JH |
2939 | |
2940 | return nr_pinned; | |
2941 | } | |
dadbb612 | 2942 | EXPORT_SYMBOL_GPL(get_user_pages_fast_only); |
9e1f0580 | 2943 | |
eddb1c22 JH |
2944 | /** |
2945 | * get_user_pages_fast() - pin user pages in memory | |
3faa52c0 JH |
2946 | * @start: starting user address |
2947 | * @nr_pages: number of pages from start to pin | |
2948 | * @gup_flags: flags modifying pin behaviour | |
2949 | * @pages: array that receives pointers to the pages pinned. | |
2950 | * Should be at least nr_pages long. | |
eddb1c22 | 2951 | * |
c1e8d7c6 | 2952 | * Attempt to pin user pages in memory without taking mm->mmap_lock. |
eddb1c22 JH |
2953 | * If not successful, it will fall back to taking the lock and |
2954 | * calling get_user_pages(). | |
2955 | * | |
2956 | * Returns number of pages pinned. This may be fewer than the number requested. | |
2957 | * If nr_pages is 0 or negative, returns 0. If no pages were pinned, returns | |
2958 | * -errno. | |
2959 | */ | |
2960 | int get_user_pages_fast(unsigned long start, int nr_pages, | |
2961 | unsigned int gup_flags, struct page **pages) | |
2962 | { | |
447f3e45 | 2963 | if (!is_valid_gup_flags(gup_flags)) |
eddb1c22 JH |
2964 | return -EINVAL; |
2965 | ||
94202f12 JH |
2966 | /* |
2967 | * The caller may or may not have explicitly set FOLL_GET; either way is | |
2968 | * OK. However, internally (within mm/gup.c), gup fast variants must set | |
2969 | * FOLL_GET, because gup fast is always a "pin with a +1 page refcount" | |
2970 | * request. | |
2971 | */ | |
2972 | gup_flags |= FOLL_GET; | |
eddb1c22 JH |
2973 | return internal_get_user_pages_fast(start, nr_pages, gup_flags, pages); |
2974 | } | |
050a9adc | 2975 | EXPORT_SYMBOL_GPL(get_user_pages_fast); |
eddb1c22 JH |
2976 | |
2977 | /** | |
2978 | * pin_user_pages_fast() - pin user pages in memory without taking locks | |
2979 | * | |
3faa52c0 JH |
2980 | * @start: starting user address |
2981 | * @nr_pages: number of pages from start to pin | |
2982 | * @gup_flags: flags modifying pin behaviour | |
2983 | * @pages: array that receives pointers to the pages pinned. | |
2984 | * Should be at least nr_pages long. | |
2985 | * | |
2986 | * Nearly the same as get_user_pages_fast(), except that FOLL_PIN is set. See | |
2987 | * get_user_pages_fast() for documentation on the function arguments, because | |
2988 | * the arguments here are identical. | |
2989 | * | |
2990 | * FOLL_PIN means that the pages must be released via unpin_user_page(). Please | |
72ef5e52 | 2991 | * see Documentation/core-api/pin_user_pages.rst for further details. |
eddb1c22 JH |
2992 | */ |
2993 | int pin_user_pages_fast(unsigned long start, int nr_pages, | |
2994 | unsigned int gup_flags, struct page **pages) | |
2995 | { | |
3faa52c0 JH |
2996 | /* FOLL_GET and FOLL_PIN are mutually exclusive. */ |
2997 | if (WARN_ON_ONCE(gup_flags & FOLL_GET)) | |
2998 | return -EINVAL; | |
2999 | ||
3000 | gup_flags |= FOLL_PIN; | |
3001 | return internal_get_user_pages_fast(start, nr_pages, gup_flags, pages); | |
eddb1c22 JH |
3002 | } |
3003 | EXPORT_SYMBOL_GPL(pin_user_pages_fast); | |
3004 | ||
104acc32 | 3005 | /* |
dadbb612 SJ |
3006 | * This is the FOLL_PIN equivalent of get_user_pages_fast_only(). Behavior |
3007 | * is the same, except that this one sets FOLL_PIN instead of FOLL_GET. | |
104acc32 JH |
3008 | * |
3009 | * The API rules are the same, too: no negative values may be returned. | |
3010 | */ | |
3011 | int pin_user_pages_fast_only(unsigned long start, int nr_pages, | |
3012 | unsigned int gup_flags, struct page **pages) | |
3013 | { | |
3014 | int nr_pinned; | |
3015 | ||
3016 | /* | |
3017 | * FOLL_GET and FOLL_PIN are mutually exclusive. Note that the API | |
3018 | * rules require returning 0, rather than -errno: | |
3019 | */ | |
3020 | if (WARN_ON_ONCE(gup_flags & FOLL_GET)) | |
3021 | return 0; | |
3022 | /* | |
3023 | * FOLL_FAST_ONLY is required in order to match the API description of | |
3024 | * this routine: no fall back to regular ("slow") GUP. | |
3025 | */ | |
3026 | gup_flags |= (FOLL_PIN | FOLL_FAST_ONLY); | |
3027 | nr_pinned = internal_get_user_pages_fast(start, nr_pages, gup_flags, | |
3028 | pages); | |
3029 | /* | |
3030 | * This routine is not allowed to return negative values. However, | |
3031 | * internal_get_user_pages_fast() *can* return -errno. Therefore, | |
3032 | * correct for that here: | |
3033 | */ | |
3034 | if (nr_pinned < 0) | |
3035 | nr_pinned = 0; | |
3036 | ||
3037 | return nr_pinned; | |
3038 | } | |
3039 | EXPORT_SYMBOL_GPL(pin_user_pages_fast_only); | |
3040 | ||
eddb1c22 | 3041 | /** |
64019a2e | 3042 | * pin_user_pages_remote() - pin pages of a remote process |
eddb1c22 | 3043 | * |
3faa52c0 JH |
3044 | * @mm: mm_struct of target mm |
3045 | * @start: starting user address | |
3046 | * @nr_pages: number of pages from start to pin | |
3047 | * @gup_flags: flags modifying lookup behaviour | |
3048 | * @pages: array that receives pointers to the pages pinned. | |
3049 | * Should be at least nr_pages long. Or NULL, if caller | |
3050 | * only intends to ensure the pages are faulted in. | |
3051 | * @vmas: array of pointers to vmas corresponding to each page. | |
3052 | * Or NULL if the caller does not require them. | |
3053 | * @locked: pointer to lock flag indicating whether lock is held and | |
3054 | * subsequently whether VM_FAULT_RETRY functionality can be | |
3055 | * utilised. Lock must initially be held. | |
3056 | * | |
3057 | * Nearly the same as get_user_pages_remote(), except that FOLL_PIN is set. See | |
3058 | * get_user_pages_remote() for documentation on the function arguments, because | |
3059 | * the arguments here are identical. | |
3060 | * | |
3061 | * FOLL_PIN means that the pages must be released via unpin_user_page(). Please | |
72ef5e52 | 3062 | * see Documentation/core-api/pin_user_pages.rst for details. |
eddb1c22 | 3063 | */ |
64019a2e | 3064 | long pin_user_pages_remote(struct mm_struct *mm, |
eddb1c22 JH |
3065 | unsigned long start, unsigned long nr_pages, |
3066 | unsigned int gup_flags, struct page **pages, | |
3067 | struct vm_area_struct **vmas, int *locked) | |
3068 | { | |
3faa52c0 JH |
3069 | /* FOLL_GET and FOLL_PIN are mutually exclusive. */ |
3070 | if (WARN_ON_ONCE(gup_flags & FOLL_GET)) | |
3071 | return -EINVAL; | |
3072 | ||
3073 | gup_flags |= FOLL_PIN; | |
64019a2e | 3074 | return __get_user_pages_remote(mm, start, nr_pages, gup_flags, |
3faa52c0 | 3075 | pages, vmas, locked); |
eddb1c22 JH |
3076 | } |
3077 | EXPORT_SYMBOL(pin_user_pages_remote); | |
3078 | ||
3079 | /** | |
3080 | * pin_user_pages() - pin user pages in memory for use by other devices | |
3081 | * | |
3faa52c0 JH |
3082 | * @start: starting user address |
3083 | * @nr_pages: number of pages from start to pin | |
3084 | * @gup_flags: flags modifying lookup behaviour | |
3085 | * @pages: array that receives pointers to the pages pinned. | |
3086 | * Should be at least nr_pages long. Or NULL, if caller | |
3087 | * only intends to ensure the pages are faulted in. | |
3088 | * @vmas: array of pointers to vmas corresponding to each page. | |
3089 | * Or NULL if the caller does not require them. | |
3090 | * | |
3091 | * Nearly the same as get_user_pages(), except that FOLL_TOUCH is not set, and | |
3092 | * FOLL_PIN is set. | |
3093 | * | |
3094 | * FOLL_PIN means that the pages must be released via unpin_user_page(). Please | |
72ef5e52 | 3095 | * see Documentation/core-api/pin_user_pages.rst for details. |
eddb1c22 JH |
3096 | */ |
3097 | long pin_user_pages(unsigned long start, unsigned long nr_pages, | |
3098 | unsigned int gup_flags, struct page **pages, | |
3099 | struct vm_area_struct **vmas) | |
3100 | { | |
3faa52c0 JH |
3101 | /* FOLL_GET and FOLL_PIN are mutually exclusive. */ |
3102 | if (WARN_ON_ONCE(gup_flags & FOLL_GET)) | |
3103 | return -EINVAL; | |
3104 | ||
3105 | gup_flags |= FOLL_PIN; | |
64019a2e | 3106 | return __gup_longterm_locked(current->mm, start, nr_pages, |
3faa52c0 | 3107 | pages, vmas, gup_flags); |
eddb1c22 JH |
3108 | } |
3109 | EXPORT_SYMBOL(pin_user_pages); | |
91429023 JH |
3110 | |
3111 | /* | |
3112 | * pin_user_pages_unlocked() is the FOLL_PIN variant of | |
3113 | * get_user_pages_unlocked(). Behavior is the same, except that this one sets | |
3114 | * FOLL_PIN and rejects FOLL_GET. | |
3115 | */ | |
3116 | long pin_user_pages_unlocked(unsigned long start, unsigned long nr_pages, | |
3117 | struct page **pages, unsigned int gup_flags) | |
3118 | { | |
3119 | /* FOLL_GET and FOLL_PIN are mutually exclusive. */ | |
3120 | if (WARN_ON_ONCE(gup_flags & FOLL_GET)) | |
3121 | return -EINVAL; | |
3122 | ||
3123 | gup_flags |= FOLL_PIN; | |
3124 | return get_user_pages_unlocked(start, nr_pages, pages, gup_flags); | |
3125 | } | |
3126 | EXPORT_SYMBOL(pin_user_pages_unlocked); | |
420c2091 JH |
3127 | |
3128 | /* | |
3129 | * pin_user_pages_locked() is the FOLL_PIN variant of get_user_pages_locked(). | |
3130 | * Behavior is the same, except that this one sets FOLL_PIN and rejects | |
3131 | * FOLL_GET. | |
3132 | */ | |
3133 | long pin_user_pages_locked(unsigned long start, unsigned long nr_pages, | |
3134 | unsigned int gup_flags, struct page **pages, | |
3135 | int *locked) | |
3136 | { | |
3137 | /* | |
3138 | * FIXME: Current FOLL_LONGTERM behavior is incompatible with | |
3139 | * FAULT_FLAG_ALLOW_RETRY because of the FS DAX check requirement on | |
3140 | * vmas. As there are no users of this flag in this call we simply | |
3141 | * disallow this option for now. | |
3142 | */ | |
3143 | if (WARN_ON_ONCE(gup_flags & FOLL_LONGTERM)) | |
3144 | return -EINVAL; | |
3145 | ||
3146 | /* FOLL_GET and FOLL_PIN are mutually exclusive. */ | |
3147 | if (WARN_ON_ONCE(gup_flags & FOLL_GET)) | |
3148 | return -EINVAL; | |
3149 | ||
3150 | gup_flags |= FOLL_PIN; | |
64019a2e | 3151 | return __get_user_pages_locked(current->mm, start, nr_pages, |
420c2091 JH |
3152 | pages, NULL, locked, |
3153 | gup_flags | FOLL_TOUCH); | |
3154 | } | |
3155 | EXPORT_SYMBOL(pin_user_pages_locked); |