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