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