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