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