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> |
89c1905d | 8 | #include <linux/memfd.h> |
3565fce3 | 9 | #include <linux/memremap.h> |
4bbd4c77 KS |
10 | #include <linux/pagemap.h> |
11 | #include <linux/rmap.h> | |
12 | #include <linux/swap.h> | |
13 | #include <linux/swapops.h> | |
1507f512 | 14 | #include <linux/secretmem.h> |
4bbd4c77 | 15 | |
174cd4b1 | 16 | #include <linux/sched/signal.h> |
2667f50e | 17 | #include <linux/rwsem.h> |
f30c59e9 | 18 | #include <linux/hugetlb.h> |
9a4e9f3b AK |
19 | #include <linux/migrate.h> |
20 | #include <linux/mm_inline.h> | |
89c1905d | 21 | #include <linux/pagevec.h> |
9a4e9f3b | 22 | #include <linux/sched/mm.h> |
a6e79df9 | 23 | #include <linux/shmem_fs.h> |
1027e443 | 24 | |
33a709b2 | 25 | #include <asm/mmu_context.h> |
1027e443 | 26 | #include <asm/tlbflush.h> |
2667f50e | 27 | |
4bbd4c77 | 28 | #include "internal.h" |
7d0f0f06 | 29 | #include "swap.h" |
4bbd4c77 | 30 | |
df06b37f KB |
31 | struct follow_page_context { |
32 | struct dev_pagemap *pgmap; | |
33 | unsigned int page_mask; | |
34 | }; | |
35 | ||
b6a2619c DH |
36 | static inline void sanity_check_pinned_pages(struct page **pages, |
37 | unsigned long npages) | |
38 | { | |
39 | if (!IS_ENABLED(CONFIG_DEBUG_VM)) | |
40 | return; | |
41 | ||
42 | /* | |
43 | * We only pin anonymous pages if they are exclusive. Once pinned, we | |
44 | * can no longer turn them possibly shared and PageAnonExclusive() will | |
45 | * stick around until the page is freed. | |
46 | * | |
47 | * We'd like to verify that our pinned anonymous pages are still mapped | |
48 | * exclusively. The issue with anon THP is that we don't know how | |
49 | * they are/were mapped when pinning them. However, for anon | |
50 | * THP we can assume that either the given page (PTE-mapped THP) or | |
51 | * the head page (PMD-mapped THP) should be PageAnonExclusive(). If | |
52 | * neither is the case, there is certainly something wrong. | |
53 | */ | |
54 | for (; npages; npages--, pages++) { | |
55 | struct page *page = *pages; | |
a1268be2 JH |
56 | struct folio *folio; |
57 | ||
58 | if (!page) | |
59 | continue; | |
60 | ||
61 | folio = page_folio(page); | |
b6a2619c | 62 | |
c8070b78 DH |
63 | if (is_zero_page(page) || |
64 | !folio_test_anon(folio)) | |
b6a2619c DH |
65 | continue; |
66 | if (!folio_test_large(folio) || folio_test_hugetlb(folio)) | |
67 | VM_BUG_ON_PAGE(!PageAnonExclusive(&folio->page), page); | |
68 | else | |
69 | /* Either a PTE-mapped or a PMD-mapped THP. */ | |
70 | VM_BUG_ON_PAGE(!PageAnonExclusive(&folio->page) && | |
71 | !PageAnonExclusive(page), page); | |
72 | } | |
73 | } | |
74 | ||
cd1adf1b | 75 | /* |
ece1ed7b | 76 | * Return the folio with ref appropriately incremented, |
cd1adf1b | 77 | * or NULL if that failed. |
a707cdd5 | 78 | */ |
ece1ed7b | 79 | static inline struct folio *try_get_folio(struct page *page, int refs) |
a707cdd5 | 80 | { |
ece1ed7b | 81 | struct folio *folio; |
a707cdd5 | 82 | |
59409373 | 83 | retry: |
ece1ed7b MWO |
84 | folio = page_folio(page); |
85 | if (WARN_ON_ONCE(folio_ref_count(folio) < 0)) | |
a707cdd5 | 86 | return NULL; |
fa2690af | 87 | if (unlikely(!folio_ref_try_add(folio, refs))) |
a707cdd5 | 88 | return NULL; |
c24d3732 JH |
89 | |
90 | /* | |
ece1ed7b MWO |
91 | * At this point we have a stable reference to the folio; but it |
92 | * could be that between calling page_folio() and the refcount | |
93 | * increment, the folio was split, in which case we'd end up | |
94 | * holding a reference on a folio that has nothing to do with the page | |
c24d3732 | 95 | * we were given anymore. |
ece1ed7b MWO |
96 | * So now that the folio is stable, recheck that the page still |
97 | * belongs to this folio. | |
c24d3732 | 98 | */ |
ece1ed7b | 99 | if (unlikely(page_folio(page) != folio)) { |
38607c62 | 100 | folio_put_refs(folio, refs); |
59409373 | 101 | goto retry; |
c24d3732 JH |
102 | } |
103 | ||
ece1ed7b | 104 | return folio; |
a707cdd5 JH |
105 | } |
106 | ||
d8ddc099 | 107 | static void gup_put_folio(struct folio *folio, int refs, unsigned int flags) |
4509b42c JG |
108 | { |
109 | if (flags & FOLL_PIN) { | |
c8070b78 DH |
110 | if (is_zero_folio(folio)) |
111 | return; | |
d8ddc099 | 112 | node_stat_mod_folio(folio, NR_FOLL_PIN_RELEASED, refs); |
31a31da8 | 113 | if (folio_has_pincount(folio)) |
94688e8e | 114 | atomic_sub(refs, &folio->_pincount); |
4509b42c JG |
115 | else |
116 | refs *= GUP_PIN_COUNTING_BIAS; | |
117 | } | |
118 | ||
38607c62 | 119 | folio_put_refs(folio, refs); |
4509b42c JG |
120 | } |
121 | ||
3faa52c0 | 122 | /** |
f442fa61 YS |
123 | * try_grab_folio() - add a folio's refcount by a flag-dependent amount |
124 | * @folio: pointer to folio to be grabbed | |
125 | * @refs: the value to (effectively) add to the folio's refcount | |
126 | * @flags: gup flags: these are the FOLL_* flag values | |
3faa52c0 JH |
127 | * |
128 | * This might not do anything at all, depending on the flags argument. | |
129 | * | |
130 | * "grab" names in this file mean, "look at flags to decide whether to use | |
f442fa61 | 131 | * FOLL_PIN or FOLL_GET behavior, when incrementing the folio's refcount. |
3faa52c0 | 132 | * |
3faa52c0 | 133 | * Either FOLL_PIN or FOLL_GET (or neither) may be set, but not both at the same |
f442fa61 | 134 | * time. |
3faa52c0 | 135 | * |
0f089235 LG |
136 | * Return: 0 for success, or if no action was required (if neither FOLL_PIN |
137 | * nor FOLL_GET was set, nothing is done). A negative error code for failure: | |
138 | * | |
f442fa61 | 139 | * -ENOMEM FOLL_GET or FOLL_PIN was set, but the folio could not |
0f089235 | 140 | * be grabbed. |
f442fa61 YS |
141 | * |
142 | * It is called when we have a stable reference for the folio, typically in | |
143 | * GUP slow path. | |
3faa52c0 | 144 | */ |
f442fa61 YS |
145 | int __must_check try_grab_folio(struct folio *folio, int refs, |
146 | unsigned int flags) | |
3faa52c0 | 147 | { |
5fec0719 | 148 | if (WARN_ON_ONCE(folio_ref_count(folio) <= 0)) |
0f089235 | 149 | return -ENOMEM; |
3faa52c0 | 150 | |
f442fa61 | 151 | if (unlikely(!(flags & FOLL_PCI_P2PDMA) && is_pci_p2pdma_page(&folio->page))) |
4003f107 | 152 | return -EREMOTEIO; |
3faa52c0 | 153 | |
c36c04c2 | 154 | if (flags & FOLL_GET) |
f442fa61 | 155 | folio_ref_add(folio, refs); |
c36c04c2 | 156 | else if (flags & FOLL_PIN) { |
c8070b78 DH |
157 | /* |
158 | * Don't take a pin on the zero page - it's not going anywhere | |
159 | * and it is used in a *lot* of places. | |
160 | */ | |
f442fa61 | 161 | if (is_zero_folio(folio)) |
c8070b78 DH |
162 | return 0; |
163 | ||
c36c04c2 | 164 | /* |
f442fa61 | 165 | * Increment the normal page refcount field at least once, |
78d9d6ce | 166 | * so that the page really is pinned. |
c36c04c2 | 167 | */ |
31a31da8 | 168 | if (folio_has_pincount(folio)) { |
f442fa61 YS |
169 | folio_ref_add(folio, refs); |
170 | atomic_add(refs, &folio->_pincount); | |
8ea2979c | 171 | } else { |
f442fa61 | 172 | folio_ref_add(folio, refs * GUP_PIN_COUNTING_BIAS); |
8ea2979c | 173 | } |
c36c04c2 | 174 | |
f442fa61 | 175 | node_stat_mod_folio(folio, NR_FOLL_PIN_ACQUIRED, refs); |
c36c04c2 JH |
176 | } |
177 | ||
0f089235 | 178 | return 0; |
3faa52c0 JH |
179 | } |
180 | ||
3faa52c0 JH |
181 | /** |
182 | * unpin_user_page() - release a dma-pinned page | |
183 | * @page: pointer to page to be released | |
184 | * | |
185 | * Pages that were pinned via pin_user_pages*() must be released via either | |
186 | * unpin_user_page(), or one of the unpin_user_pages*() routines. This is so | |
187 | * that such pages can be separately tracked and uniquely handled. In | |
188 | * particular, interactions with RDMA and filesystems need special handling. | |
189 | */ | |
190 | void unpin_user_page(struct page *page) | |
191 | { | |
b6a2619c | 192 | sanity_check_pinned_pages(&page, 1); |
d8ddc099 | 193 | gup_put_folio(page_folio(page), 1, FOLL_PIN); |
3faa52c0 JH |
194 | } |
195 | EXPORT_SYMBOL(unpin_user_page); | |
196 | ||
6cc04054 VK |
197 | /** |
198 | * unpin_folio() - release a dma-pinned folio | |
199 | * @folio: pointer to folio to be released | |
200 | * | |
201 | * Folios that were pinned via memfd_pin_folios() or other similar routines | |
202 | * must be released either using unpin_folio() or unpin_folios(). | |
203 | */ | |
204 | void unpin_folio(struct folio *folio) | |
205 | { | |
206 | gup_put_folio(folio, 1, FOLL_PIN); | |
207 | } | |
208 | EXPORT_SYMBOL_GPL(unpin_folio); | |
209 | ||
1101fb8f DH |
210 | /** |
211 | * folio_add_pin - Try to get an additional pin on a pinned folio | |
212 | * @folio: The folio to be pinned | |
213 | * | |
214 | * Get an additional pin on a folio we already have a pin on. Makes no change | |
215 | * if the folio is a zero_page. | |
216 | */ | |
217 | void folio_add_pin(struct folio *folio) | |
218 | { | |
219 | if (is_zero_folio(folio)) | |
220 | return; | |
221 | ||
222 | /* | |
223 | * Similar to try_grab_folio(): be sure to *also* increment the normal | |
224 | * page refcount field at least once, so that the page really is | |
225 | * pinned. | |
226 | */ | |
31a31da8 | 227 | if (folio_has_pincount(folio)) { |
1101fb8f DH |
228 | WARN_ON_ONCE(atomic_read(&folio->_pincount) < 1); |
229 | folio_ref_inc(folio); | |
230 | atomic_inc(&folio->_pincount); | |
231 | } else { | |
232 | WARN_ON_ONCE(folio_ref_count(folio) < GUP_PIN_COUNTING_BIAS); | |
233 | folio_ref_add(folio, GUP_PIN_COUNTING_BIAS); | |
234 | } | |
235 | } | |
236 | ||
659508f9 | 237 | static inline struct folio *gup_folio_range_next(struct page *start, |
8f39f5fc | 238 | unsigned long npages, unsigned long i, unsigned int *ntails) |
458a4f78 | 239 | { |
659508f9 MWO |
240 | struct page *next = nth_page(start, i); |
241 | struct folio *folio = page_folio(next); | |
458a4f78 JM |
242 | unsigned int nr = 1; |
243 | ||
659508f9 | 244 | if (folio_test_large(folio)) |
4c654229 | 245 | nr = min_t(unsigned int, npages - i, |
659508f9 | 246 | folio_nr_pages(folio) - folio_page_idx(folio, next)); |
458a4f78 | 247 | |
458a4f78 | 248 | *ntails = nr; |
659508f9 | 249 | return folio; |
458a4f78 JM |
250 | } |
251 | ||
12521c76 | 252 | static inline struct folio *gup_folio_next(struct page **list, |
28297dbc | 253 | unsigned long npages, unsigned long i, unsigned int *ntails) |
8745d7f6 | 254 | { |
12521c76 | 255 | struct folio *folio = page_folio(list[i]); |
8745d7f6 JM |
256 | unsigned int nr; |
257 | ||
8745d7f6 | 258 | for (nr = i + 1; nr < npages; nr++) { |
12521c76 | 259 | if (page_folio(list[nr]) != folio) |
8745d7f6 JM |
260 | break; |
261 | } | |
262 | ||
8745d7f6 | 263 | *ntails = nr - i; |
12521c76 | 264 | return folio; |
8745d7f6 JM |
265 | } |
266 | ||
fc1d8e7c | 267 | /** |
f1f6a7dd | 268 | * unpin_user_pages_dirty_lock() - release and optionally dirty gup-pinned pages |
2d15eb31 | 269 | * @pages: array of pages to be maybe marked dirty, and definitely released. |
fc1d8e7c | 270 | * @npages: number of pages in the @pages array. |
2d15eb31 | 271 | * @make_dirty: whether to mark the pages dirty |
fc1d8e7c JH |
272 | * |
273 | * "gup-pinned page" refers to a page that has had one of the get_user_pages() | |
274 | * variants called on that page. | |
275 | * | |
276 | * For each page in the @pages array, make that page (or its head page, if a | |
2d15eb31 | 277 | * compound page) dirty, if @make_dirty is true, and if the page was previously |
f1f6a7dd JH |
278 | * listed as clean. In any case, releases all pages using unpin_user_page(), |
279 | * possibly via unpin_user_pages(), for the non-dirty case. | |
fc1d8e7c | 280 | * |
f1f6a7dd | 281 | * Please see the unpin_user_page() documentation for details. |
fc1d8e7c | 282 | * |
2d15eb31 | 283 | * set_page_dirty_lock() is used internally. If instead, set_page_dirty() is |
284 | * required, then the caller should a) verify that this is really correct, | |
285 | * because _lock() is usually required, and b) hand code it: | |
f1f6a7dd | 286 | * set_page_dirty_lock(), unpin_user_page(). |
fc1d8e7c JH |
287 | * |
288 | */ | |
f1f6a7dd JH |
289 | void unpin_user_pages_dirty_lock(struct page **pages, unsigned long npages, |
290 | bool make_dirty) | |
fc1d8e7c | 291 | { |
12521c76 MWO |
292 | unsigned long i; |
293 | struct folio *folio; | |
294 | unsigned int nr; | |
2d15eb31 | 295 | |
296 | if (!make_dirty) { | |
f1f6a7dd | 297 | unpin_user_pages(pages, npages); |
2d15eb31 | 298 | return; |
299 | } | |
300 | ||
b6a2619c | 301 | sanity_check_pinned_pages(pages, npages); |
12521c76 MWO |
302 | for (i = 0; i < npages; i += nr) { |
303 | folio = gup_folio_next(pages, npages, i, &nr); | |
2d15eb31 | 304 | /* |
305 | * Checking PageDirty at this point may race with | |
306 | * clear_page_dirty_for_io(), but that's OK. Two key | |
307 | * cases: | |
308 | * | |
309 | * 1) This code sees the page as already dirty, so it | |
310 | * skips the call to set_page_dirty(). That could happen | |
311 | * because clear_page_dirty_for_io() called | |
a929e0d1 | 312 | * folio_mkclean(), followed by set_page_dirty(). |
2d15eb31 | 313 | * However, now the page is going to get written back, |
314 | * which meets the original intention of setting it | |
315 | * dirty, so all is well: clear_page_dirty_for_io() goes | |
316 | * on to call TestClearPageDirty(), and write the page | |
317 | * back. | |
318 | * | |
319 | * 2) This code sees the page as clean, so it calls | |
320 | * set_page_dirty(). The page stays dirty, despite being | |
321 | * written back, so it gets written back again in the | |
322 | * next writeback cycle. This is harmless. | |
323 | */ | |
12521c76 MWO |
324 | if (!folio_test_dirty(folio)) { |
325 | folio_lock(folio); | |
326 | folio_mark_dirty(folio); | |
327 | folio_unlock(folio); | |
328 | } | |
329 | gup_put_folio(folio, nr, FOLL_PIN); | |
2d15eb31 | 330 | } |
fc1d8e7c | 331 | } |
f1f6a7dd | 332 | EXPORT_SYMBOL(unpin_user_pages_dirty_lock); |
fc1d8e7c | 333 | |
458a4f78 JM |
334 | /** |
335 | * unpin_user_page_range_dirty_lock() - release and optionally dirty | |
336 | * gup-pinned page range | |
337 | * | |
338 | * @page: the starting page of a range maybe marked dirty, and definitely released. | |
339 | * @npages: number of consecutive pages to release. | |
340 | * @make_dirty: whether to mark the pages dirty | |
341 | * | |
342 | * "gup-pinned page range" refers to a range of pages that has had one of the | |
343 | * pin_user_pages() variants called on that page. | |
344 | * | |
345 | * For the page ranges defined by [page .. page+npages], make that range (or | |
346 | * its head pages, if a compound page) dirty, if @make_dirty is true, and if the | |
347 | * page range was previously listed as clean. | |
348 | * | |
349 | * set_page_dirty_lock() is used internally. If instead, set_page_dirty() is | |
350 | * required, then the caller should a) verify that this is really correct, | |
351 | * because _lock() is usually required, and b) hand code it: | |
352 | * set_page_dirty_lock(), unpin_user_page(). | |
353 | * | |
354 | */ | |
355 | void unpin_user_page_range_dirty_lock(struct page *page, unsigned long npages, | |
356 | bool make_dirty) | |
357 | { | |
659508f9 MWO |
358 | unsigned long i; |
359 | struct folio *folio; | |
360 | unsigned int nr; | |
361 | ||
362 | for (i = 0; i < npages; i += nr) { | |
363 | folio = gup_folio_range_next(page, npages, i, &nr); | |
364 | if (make_dirty && !folio_test_dirty(folio)) { | |
365 | folio_lock(folio); | |
366 | folio_mark_dirty(folio); | |
367 | folio_unlock(folio); | |
368 | } | |
369 | gup_put_folio(folio, nr, FOLL_PIN); | |
458a4f78 JM |
370 | } |
371 | } | |
372 | EXPORT_SYMBOL(unpin_user_page_range_dirty_lock); | |
373 | ||
23babe19 | 374 | static void gup_fast_unpin_user_pages(struct page **pages, unsigned long npages) |
b6a2619c DH |
375 | { |
376 | unsigned long i; | |
377 | struct folio *folio; | |
378 | unsigned int nr; | |
379 | ||
380 | /* | |
381 | * Don't perform any sanity checks because we might have raced with | |
382 | * fork() and some anonymous pages might now actually be shared -- | |
383 | * which is why we're unpinning after all. | |
384 | */ | |
385 | for (i = 0; i < npages; i += nr) { | |
386 | folio = gup_folio_next(pages, npages, i, &nr); | |
387 | gup_put_folio(folio, nr, FOLL_PIN); | |
388 | } | |
389 | } | |
390 | ||
fc1d8e7c | 391 | /** |
f1f6a7dd | 392 | * unpin_user_pages() - release an array of gup-pinned pages. |
fc1d8e7c JH |
393 | * @pages: array of pages to be marked dirty and released. |
394 | * @npages: number of pages in the @pages array. | |
395 | * | |
f1f6a7dd | 396 | * For each page in the @pages array, release the page using unpin_user_page(). |
fc1d8e7c | 397 | * |
f1f6a7dd | 398 | * Please see the unpin_user_page() documentation for details. |
fc1d8e7c | 399 | */ |
f1f6a7dd | 400 | void unpin_user_pages(struct page **pages, unsigned long npages) |
fc1d8e7c | 401 | { |
12521c76 MWO |
402 | unsigned long i; |
403 | struct folio *folio; | |
404 | unsigned int nr; | |
fc1d8e7c | 405 | |
146608bb JH |
406 | /* |
407 | * If this WARN_ON() fires, then the system *might* be leaking pages (by | |
408 | * leaving them pinned), but probably not. More likely, gup/pup returned | |
409 | * a hard -ERRNO error to the caller, who erroneously passed it here. | |
410 | */ | |
411 | if (WARN_ON(IS_ERR_VALUE(npages))) | |
412 | return; | |
31b912de | 413 | |
b6a2619c | 414 | sanity_check_pinned_pages(pages, npages); |
12521c76 | 415 | for (i = 0; i < npages; i += nr) { |
a1268be2 JH |
416 | if (!pages[i]) { |
417 | nr = 1; | |
418 | continue; | |
419 | } | |
12521c76 MWO |
420 | folio = gup_folio_next(pages, npages, i, &nr); |
421 | gup_put_folio(folio, nr, FOLL_PIN); | |
e7602748 | 422 | } |
fc1d8e7c | 423 | } |
f1f6a7dd | 424 | EXPORT_SYMBOL(unpin_user_pages); |
fc1d8e7c | 425 | |
d3bfbfb1 KK |
426 | /** |
427 | * unpin_user_folio() - release pages of a folio | |
428 | * @folio: pointer to folio to be released | |
429 | * @npages: number of pages of same folio | |
430 | * | |
431 | * Release npages of the folio | |
432 | */ | |
433 | void unpin_user_folio(struct folio *folio, unsigned long npages) | |
434 | { | |
435 | gup_put_folio(folio, npages, FOLL_PIN); | |
436 | } | |
437 | EXPORT_SYMBOL(unpin_user_folio); | |
438 | ||
6cc04054 VK |
439 | /** |
440 | * unpin_folios() - release an array of gup-pinned folios. | |
441 | * @folios: array of folios to be marked dirty and released. | |
442 | * @nfolios: number of folios in the @folios array. | |
443 | * | |
444 | * For each folio in the @folios array, release the folio using gup_put_folio. | |
445 | * | |
446 | * Please see the unpin_folio() documentation for details. | |
447 | */ | |
448 | void unpin_folios(struct folio **folios, unsigned long nfolios) | |
449 | { | |
450 | unsigned long i = 0, j; | |
451 | ||
452 | /* | |
453 | * If this WARN_ON() fires, then the system *might* be leaking folios | |
454 | * (by leaving them pinned), but probably not. More likely, gup/pup | |
455 | * returned a hard -ERRNO error to the caller, who erroneously passed | |
456 | * it here. | |
457 | */ | |
458 | if (WARN_ON(IS_ERR_VALUE(nfolios))) | |
459 | return; | |
460 | ||
461 | while (i < nfolios) { | |
462 | for (j = i + 1; j < nfolios; j++) | |
463 | if (folios[i] != folios[j]) | |
464 | break; | |
465 | ||
466 | if (folios[i]) | |
467 | gup_put_folio(folios[i], j - i, FOLL_PIN); | |
468 | i = j; | |
469 | } | |
470 | } | |
471 | EXPORT_SYMBOL_GPL(unpin_folios); | |
472 | ||
a458b76a AA |
473 | /* |
474 | * Set the MMF_HAS_PINNED if not set yet; after set it'll be there for the mm's | |
475 | * lifecycle. Avoid setting the bit unless necessary, or it might cause write | |
476 | * cache bouncing on large SMP machines for concurrent pinned gups. | |
477 | */ | |
478 | static inline void mm_set_has_pinned_flag(unsigned long *mm_flags) | |
479 | { | |
480 | if (!test_bit(MMF_HAS_PINNED, mm_flags)) | |
481 | set_bit(MMF_HAS_PINNED, mm_flags); | |
482 | } | |
483 | ||
050a9adc | 484 | #ifdef CONFIG_MMU |
a12083d7 | 485 | |
8268614b | 486 | #ifdef CONFIG_HAVE_GUP_FAST |
a12083d7 PX |
487 | static int record_subpages(struct page *page, unsigned long sz, |
488 | unsigned long addr, unsigned long end, | |
489 | struct page **pages) | |
490 | { | |
491 | struct page *start_page; | |
492 | int nr; | |
493 | ||
494 | start_page = nth_page(page, (addr & (sz - 1)) >> PAGE_SHIFT); | |
495 | for (nr = 0; addr != end; nr++, addr += PAGE_SIZE) | |
496 | pages[nr] = nth_page(start_page, nr); | |
497 | ||
498 | return nr; | |
499 | } | |
f442fa61 YS |
500 | |
501 | /** | |
502 | * try_grab_folio_fast() - Attempt to get or pin a folio in fast path. | |
503 | * @page: pointer to page to be grabbed | |
504 | * @refs: the value to (effectively) add to the folio's refcount | |
505 | * @flags: gup flags: these are the FOLL_* flag values. | |
506 | * | |
507 | * "grab" names in this file mean, "look at flags to decide whether to use | |
508 | * FOLL_PIN or FOLL_GET behavior, when incrementing the folio's refcount. | |
509 | * | |
510 | * Either FOLL_PIN or FOLL_GET (or neither) must be set, but not both at the | |
511 | * same time. (That's true throughout the get_user_pages*() and | |
512 | * pin_user_pages*() APIs.) Cases: | |
513 | * | |
514 | * FOLL_GET: folio's refcount will be incremented by @refs. | |
515 | * | |
516 | * FOLL_PIN on large folios: folio's refcount will be incremented by | |
517 | * @refs, and its pincount will be incremented by @refs. | |
518 | * | |
519 | * FOLL_PIN on single-page folios: folio's refcount will be incremented by | |
520 | * @refs * GUP_PIN_COUNTING_BIAS. | |
521 | * | |
522 | * Return: The folio containing @page (with refcount appropriately | |
523 | * incremented) for success, or NULL upon failure. If neither FOLL_GET | |
524 | * nor FOLL_PIN was set, that's considered failure, and furthermore, | |
525 | * a likely bug in the caller, so a warning is also emitted. | |
526 | * | |
527 | * It uses add ref unless zero to elevate the folio refcount and must be called | |
528 | * in fast path only. | |
529 | */ | |
530 | static struct folio *try_grab_folio_fast(struct page *page, int refs, | |
531 | unsigned int flags) | |
532 | { | |
533 | struct folio *folio; | |
534 | ||
535 | /* Raise warn if it is not called in fast GUP */ | |
536 | VM_WARN_ON_ONCE(!irqs_disabled()); | |
537 | ||
538 | if (WARN_ON_ONCE((flags & (FOLL_GET | FOLL_PIN)) == 0)) | |
539 | return NULL; | |
540 | ||
541 | if (unlikely(!(flags & FOLL_PCI_P2PDMA) && is_pci_p2pdma_page(page))) | |
542 | return NULL; | |
543 | ||
544 | if (flags & FOLL_GET) | |
545 | return try_get_folio(page, refs); | |
546 | ||
547 | /* FOLL_PIN is set */ | |
548 | ||
549 | /* | |
550 | * Don't take a pin on the zero page - it's not going anywhere | |
551 | * and it is used in a *lot* of places. | |
552 | */ | |
553 | if (is_zero_page(page)) | |
554 | return page_folio(page); | |
555 | ||
556 | folio = try_get_folio(page, refs); | |
557 | if (!folio) | |
558 | return NULL; | |
559 | ||
560 | /* | |
561 | * Can't do FOLL_LONGTERM + FOLL_PIN gup fast path if not in a | |
562 | * right zone, so fail and let the caller fall back to the slow | |
563 | * path. | |
564 | */ | |
565 | if (unlikely((flags & FOLL_LONGTERM) && | |
566 | !folio_is_longterm_pinnable(folio))) { | |
38607c62 | 567 | folio_put_refs(folio, refs); |
f442fa61 YS |
568 | return NULL; |
569 | } | |
570 | ||
571 | /* | |
572 | * When pinning a large folio, use an exact count to track it. | |
573 | * | |
574 | * However, be sure to *also* increment the normal folio | |
575 | * refcount field at least once, so that the folio really | |
576 | * is pinned. That's why the refcount from the earlier | |
577 | * try_get_folio() is left intact. | |
578 | */ | |
31a31da8 | 579 | if (folio_has_pincount(folio)) |
f442fa61 YS |
580 | atomic_add(refs, &folio->_pincount); |
581 | else | |
582 | folio_ref_add(folio, | |
583 | refs * (GUP_PIN_COUNTING_BIAS - 1)); | |
584 | /* | |
585 | * Adjust the pincount before re-checking the PTE for changes. | |
586 | * This is essentially a smp_mb() and is paired with a memory | |
587 | * barrier in folio_try_share_anon_rmap_*(). | |
588 | */ | |
589 | smp_mb__after_atomic(); | |
590 | ||
591 | node_stat_mod_folio(folio, NR_FOLL_PIN_ACQUIRED, refs); | |
592 | ||
593 | return folio; | |
594 | } | |
8268614b | 595 | #endif /* CONFIG_HAVE_GUP_FAST */ |
a12083d7 | 596 | |
052ccfbc GM |
597 | /* Common code for can_follow_write_* */ |
598 | static inline bool can_follow_write_common(struct page *page, | |
599 | struct vm_area_struct *vma, unsigned int flags) | |
600 | { | |
601 | /* Maybe FOLL_FORCE is set to override it? */ | |
602 | if (!(flags & FOLL_FORCE)) | |
603 | return false; | |
604 | ||
605 | /* But FOLL_FORCE has no effect on shared mappings */ | |
606 | if (vma->vm_flags & (VM_MAYSHARE | VM_SHARED)) | |
607 | return false; | |
608 | ||
609 | /* ... or read-only private ones */ | |
610 | if (!(vma->vm_flags & VM_MAYWRITE)) | |
611 | return false; | |
612 | ||
613 | /* ... or already writable ones that just need to take a write fault */ | |
614 | if (vma->vm_flags & VM_WRITE) | |
615 | return false; | |
616 | ||
617 | /* | |
618 | * See can_change_pte_writable(): we broke COW and could map the page | |
619 | * writable if we have an exclusive anonymous page ... | |
620 | */ | |
621 | return page && PageAnon(page) && PageAnonExclusive(page); | |
622 | } | |
623 | ||
69e68b4f | 624 | static struct page *no_page_table(struct vm_area_struct *vma, |
878b0c45 | 625 | unsigned int flags, unsigned long address) |
4bbd4c77 | 626 | { |
878b0c45 PX |
627 | if (!(flags & FOLL_DUMP)) |
628 | return NULL; | |
629 | ||
69e68b4f | 630 | /* |
878b0c45 | 631 | * When core dumping, we don't want to allocate unnecessary pages or |
69e68b4f KS |
632 | * page tables. Return error instead of NULL to skip handle_mm_fault, |
633 | * then get_dump_page() will return NULL to leave a hole in the dump. | |
634 | * But we can only make this optimization where a hole would surely | |
635 | * be zero-filled if handle_mm_fault() actually did handle it. | |
636 | */ | |
878b0c45 PX |
637 | if (is_vm_hugetlb_page(vma)) { |
638 | struct hstate *h = hstate_vma(vma); | |
639 | ||
640 | if (!hugetlbfs_pagecache_present(h, vma, address)) | |
641 | return ERR_PTR(-EFAULT); | |
642 | } else if ((vma_is_anonymous(vma) || !vma->vm_ops->fault)) { | |
69e68b4f | 643 | return ERR_PTR(-EFAULT); |
878b0c45 PX |
644 | } |
645 | ||
69e68b4f KS |
646 | return NULL; |
647 | } | |
4bbd4c77 | 648 | |
1b167618 | 649 | #ifdef CONFIG_PGTABLE_HAS_HUGE_LEAVES |
052ccfbc GM |
650 | /* FOLL_FORCE can write to even unwritable PUDs in COW mappings. */ |
651 | static inline bool can_follow_write_pud(pud_t pud, struct page *page, | |
652 | struct vm_area_struct *vma, | |
653 | unsigned int flags) | |
654 | { | |
655 | /* If the pud is writable, we can write to the page. */ | |
656 | if (pud_write(pud)) | |
657 | return true; | |
658 | ||
659 | return can_follow_write_common(page, vma, flags); | |
660 | } | |
661 | ||
1b167618 PX |
662 | static struct page *follow_huge_pud(struct vm_area_struct *vma, |
663 | unsigned long addr, pud_t *pudp, | |
664 | int flags, struct follow_page_context *ctx) | |
665 | { | |
666 | struct mm_struct *mm = vma->vm_mm; | |
667 | struct page *page; | |
668 | pud_t pud = *pudp; | |
669 | unsigned long pfn = pud_pfn(pud); | |
670 | int ret; | |
671 | ||
672 | assert_spin_locked(pud_lockptr(mm, pudp)); | |
673 | ||
052ccfbc | 674 | if (!pud_present(pud)) |
1b167618 PX |
675 | return NULL; |
676 | ||
052ccfbc GM |
677 | if ((flags & FOLL_WRITE) && |
678 | !can_follow_write_pud(pud, pfn_to_page(pfn), vma, flags)) | |
1b167618 PX |
679 | return NULL; |
680 | ||
681 | pfn += (addr & ~PUD_MASK) >> PAGE_SHIFT; | |
682 | ||
683 | if (IS_ENABLED(CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD) && | |
684 | pud_devmap(pud)) { | |
685 | /* | |
686 | * device mapped pages can only be returned if the caller | |
687 | * will manage the page reference count. | |
688 | * | |
689 | * At least one of FOLL_GET | FOLL_PIN must be set, so | |
690 | * assert that here: | |
691 | */ | |
692 | if (!(flags & (FOLL_GET | FOLL_PIN))) | |
693 | return ERR_PTR(-EEXIST); | |
694 | ||
695 | if (flags & FOLL_TOUCH) | |
696 | touch_pud(vma, addr, pudp, flags & FOLL_WRITE); | |
697 | ||
698 | ctx->pgmap = get_dev_pagemap(pfn, ctx->pgmap); | |
699 | if (!ctx->pgmap) | |
700 | return ERR_PTR(-EFAULT); | |
701 | } | |
702 | ||
703 | page = pfn_to_page(pfn); | |
704 | ||
705 | if (!pud_devmap(pud) && !pud_write(pud) && | |
706 | gup_must_unshare(vma, flags, page)) | |
707 | return ERR_PTR(-EMLINK); | |
708 | ||
f442fa61 | 709 | ret = try_grab_folio(page_folio(page), 1, flags); |
1b167618 PX |
710 | if (ret) |
711 | page = ERR_PTR(ret); | |
712 | else | |
713 | ctx->page_mask = HPAGE_PUD_NR - 1; | |
714 | ||
715 | return page; | |
716 | } | |
4418c522 PX |
717 | |
718 | /* FOLL_FORCE can write to even unwritable PMDs in COW mappings. */ | |
719 | static inline bool can_follow_write_pmd(pmd_t pmd, struct page *page, | |
720 | struct vm_area_struct *vma, | |
721 | unsigned int flags) | |
722 | { | |
723 | /* If the pmd is writable, we can write to the page. */ | |
724 | if (pmd_write(pmd)) | |
725 | return true; | |
726 | ||
052ccfbc | 727 | if (!can_follow_write_common(page, vma, flags)) |
4418c522 PX |
728 | return false; |
729 | ||
730 | /* ... and a write-fault isn't required for other reasons. */ | |
f38ee285 | 731 | if (pmd_needs_soft_dirty_wp(vma, pmd)) |
4418c522 PX |
732 | return false; |
733 | return !userfaultfd_huge_pmd_wp(vma, pmd); | |
734 | } | |
735 | ||
736 | static struct page *follow_huge_pmd(struct vm_area_struct *vma, | |
737 | unsigned long addr, pmd_t *pmd, | |
738 | unsigned int flags, | |
739 | struct follow_page_context *ctx) | |
740 | { | |
741 | struct mm_struct *mm = vma->vm_mm; | |
742 | pmd_t pmdval = *pmd; | |
743 | struct page *page; | |
744 | int ret; | |
745 | ||
746 | assert_spin_locked(pmd_lockptr(mm, pmd)); | |
747 | ||
748 | page = pmd_page(pmdval); | |
749 | if ((flags & FOLL_WRITE) && | |
750 | !can_follow_write_pmd(pmdval, page, vma, flags)) | |
751 | return NULL; | |
752 | ||
753 | /* Avoid dumping huge zero page */ | |
754 | if ((flags & FOLL_DUMP) && is_huge_zero_pmd(pmdval)) | |
755 | return ERR_PTR(-EFAULT); | |
756 | ||
757 | if (pmd_protnone(*pmd) && !gup_can_follow_protnone(vma, flags)) | |
758 | return NULL; | |
759 | ||
760 | if (!pmd_write(pmdval) && gup_must_unshare(vma, flags, page)) | |
761 | return ERR_PTR(-EMLINK); | |
762 | ||
763 | VM_BUG_ON_PAGE((flags & FOLL_PIN) && PageAnon(page) && | |
764 | !PageAnonExclusive(page), page); | |
765 | ||
f442fa61 | 766 | ret = try_grab_folio(page_folio(page), 1, flags); |
4418c522 PX |
767 | if (ret) |
768 | return ERR_PTR(ret); | |
769 | ||
770 | #ifdef CONFIG_TRANSPARENT_HUGEPAGE | |
771 | if (pmd_trans_huge(pmdval) && (flags & FOLL_TOUCH)) | |
772 | touch_pmd(vma, addr, pmd, flags & FOLL_WRITE); | |
773 | #endif /* CONFIG_TRANSPARENT_HUGEPAGE */ | |
774 | ||
775 | page += (addr & ~HPAGE_PMD_MASK) >> PAGE_SHIFT; | |
776 | ctx->page_mask = HPAGE_PMD_NR - 1; | |
777 | ||
778 | return page; | |
779 | } | |
780 | ||
1b167618 PX |
781 | #else /* CONFIG_PGTABLE_HAS_HUGE_LEAVES */ |
782 | static struct page *follow_huge_pud(struct vm_area_struct *vma, | |
783 | unsigned long addr, pud_t *pudp, | |
784 | int flags, struct follow_page_context *ctx) | |
785 | { | |
786 | return NULL; | |
787 | } | |
4418c522 PX |
788 | |
789 | static struct page *follow_huge_pmd(struct vm_area_struct *vma, | |
790 | unsigned long addr, pmd_t *pmd, | |
791 | unsigned int flags, | |
792 | struct follow_page_context *ctx) | |
793 | { | |
794 | return NULL; | |
795 | } | |
1b167618 PX |
796 | #endif /* CONFIG_PGTABLE_HAS_HUGE_LEAVES */ |
797 | ||
1027e443 KS |
798 | static int follow_pfn_pte(struct vm_area_struct *vma, unsigned long address, |
799 | pte_t *pte, unsigned int flags) | |
800 | { | |
1027e443 | 801 | if (flags & FOLL_TOUCH) { |
c33c7948 RR |
802 | pte_t orig_entry = ptep_get(pte); |
803 | pte_t entry = orig_entry; | |
1027e443 KS |
804 | |
805 | if (flags & FOLL_WRITE) | |
806 | entry = pte_mkdirty(entry); | |
807 | entry = pte_mkyoung(entry); | |
808 | ||
c33c7948 | 809 | if (!pte_same(orig_entry, entry)) { |
1027e443 KS |
810 | set_pte_at(vma->vm_mm, address, pte, entry); |
811 | update_mmu_cache(vma, address, pte); | |
812 | } | |
813 | } | |
814 | ||
815 | /* Proper page table entry exists, but no corresponding struct page */ | |
816 | return -EEXIST; | |
817 | } | |
818 | ||
5535be30 DH |
819 | /* FOLL_FORCE can write to even unwritable PTEs in COW mappings. */ |
820 | static inline bool can_follow_write_pte(pte_t pte, struct page *page, | |
821 | struct vm_area_struct *vma, | |
822 | unsigned int flags) | |
19be0eaf | 823 | { |
5535be30 DH |
824 | /* If the pte is writable, we can write to the page. */ |
825 | if (pte_write(pte)) | |
826 | return true; | |
827 | ||
052ccfbc | 828 | if (!can_follow_write_common(page, vma, flags)) |
5535be30 DH |
829 | return false; |
830 | ||
831 | /* ... and a write-fault isn't required for other reasons. */ | |
f38ee285 | 832 | if (pte_needs_soft_dirty_wp(vma, pte)) |
5535be30 DH |
833 | return false; |
834 | return !userfaultfd_pte_wp(vma, pte); | |
19be0eaf LT |
835 | } |
836 | ||
69e68b4f | 837 | static struct page *follow_page_pte(struct vm_area_struct *vma, |
df06b37f KB |
838 | unsigned long address, pmd_t *pmd, unsigned int flags, |
839 | struct dev_pagemap **pgmap) | |
69e68b4f KS |
840 | { |
841 | struct mm_struct *mm = vma->vm_mm; | |
b967c648 | 842 | struct folio *folio; |
69e68b4f KS |
843 | struct page *page; |
844 | spinlock_t *ptl; | |
845 | pte_t *ptep, pte; | |
f28d4363 | 846 | int ret; |
4bbd4c77 | 847 | |
4bbd4c77 | 848 | ptep = pte_offset_map_lock(mm, pmd, address, &ptl); |
04dee9e8 | 849 | if (!ptep) |
878b0c45 | 850 | return no_page_table(vma, flags, address); |
c33c7948 | 851 | pte = ptep_get(ptep); |
f7355e99 DH |
852 | if (!pte_present(pte)) |
853 | goto no_page; | |
d74943a2 | 854 | if (pte_protnone(pte) && !gup_can_follow_protnone(vma, flags)) |
4bbd4c77 | 855 | goto no_page; |
4bbd4c77 KS |
856 | |
857 | page = vm_normal_page(vma, address, pte); | |
5535be30 DH |
858 | |
859 | /* | |
860 | * We only care about anon pages in can_follow_write_pte() and don't | |
861 | * have to worry about pte_devmap() because they are never anon. | |
862 | */ | |
863 | if ((flags & FOLL_WRITE) && | |
864 | !can_follow_write_pte(pte, page, vma, flags)) { | |
865 | page = NULL; | |
866 | goto out; | |
867 | } | |
868 | ||
3faa52c0 | 869 | if (!page && pte_devmap(pte) && (flags & (FOLL_GET | FOLL_PIN))) { |
3565fce3 | 870 | /* |
3faa52c0 JH |
871 | * Only return device mapping pages in the FOLL_GET or FOLL_PIN |
872 | * case since they are only valid while holding the pgmap | |
873 | * reference. | |
3565fce3 | 874 | */ |
df06b37f KB |
875 | *pgmap = get_dev_pagemap(pte_pfn(pte), *pgmap); |
876 | if (*pgmap) | |
3565fce3 DW |
877 | page = pte_page(pte); |
878 | else | |
879 | goto no_page; | |
880 | } else if (unlikely(!page)) { | |
1027e443 KS |
881 | if (flags & FOLL_DUMP) { |
882 | /* Avoid special (like zero) pages in core dumps */ | |
883 | page = ERR_PTR(-EFAULT); | |
884 | goto out; | |
885 | } | |
886 | ||
887 | if (is_zero_pfn(pte_pfn(pte))) { | |
888 | page = pte_page(pte); | |
889 | } else { | |
1027e443 KS |
890 | ret = follow_pfn_pte(vma, address, ptep, flags); |
891 | page = ERR_PTR(ret); | |
892 | goto out; | |
893 | } | |
4bbd4c77 | 894 | } |
b967c648 | 895 | folio = page_folio(page); |
4bbd4c77 | 896 | |
84209e87 | 897 | if (!pte_write(pte) && gup_must_unshare(vma, flags, page)) { |
a7f22660 DH |
898 | page = ERR_PTR(-EMLINK); |
899 | goto out; | |
900 | } | |
b6a2619c DH |
901 | |
902 | VM_BUG_ON_PAGE((flags & FOLL_PIN) && PageAnon(page) && | |
903 | !PageAnonExclusive(page), page); | |
904 | ||
f442fa61 | 905 | /* try_grab_folio() does nothing unless FOLL_GET or FOLL_PIN is set. */ |
b967c648 | 906 | ret = try_grab_folio(folio, 1, flags); |
0f089235 LG |
907 | if (unlikely(ret)) { |
908 | page = ERR_PTR(ret); | |
3faa52c0 | 909 | goto out; |
8fde12ca | 910 | } |
4003f107 | 911 | |
f28d4363 CI |
912 | /* |
913 | * We need to make the page accessible if and only if we are going | |
914 | * to access its content (the FOLL_PIN case). Please see | |
915 | * Documentation/core-api/pin_user_pages.rst for details. | |
916 | */ | |
917 | if (flags & FOLL_PIN) { | |
b967c648 | 918 | ret = arch_make_folio_accessible(folio); |
f28d4363 CI |
919 | if (ret) { |
920 | unpin_user_page(page); | |
921 | page = ERR_PTR(ret); | |
922 | goto out; | |
923 | } | |
924 | } | |
4bbd4c77 KS |
925 | if (flags & FOLL_TOUCH) { |
926 | if ((flags & FOLL_WRITE) && | |
f0327de7 MWO |
927 | !pte_dirty(pte) && !folio_test_dirty(folio)) |
928 | folio_mark_dirty(folio); | |
4bbd4c77 KS |
929 | /* |
930 | * pte_mkyoung() would be more correct here, but atomic care | |
931 | * is needed to avoid losing the dirty bit: it is easier to use | |
f0327de7 | 932 | * folio_mark_accessed(). |
4bbd4c77 | 933 | */ |
f0327de7 | 934 | folio_mark_accessed(folio); |
4bbd4c77 | 935 | } |
1027e443 | 936 | out: |
4bbd4c77 | 937 | pte_unmap_unlock(ptep, ptl); |
4bbd4c77 | 938 | return page; |
4bbd4c77 KS |
939 | no_page: |
940 | pte_unmap_unlock(ptep, ptl); | |
941 | if (!pte_none(pte)) | |
69e68b4f | 942 | return NULL; |
878b0c45 | 943 | return no_page_table(vma, flags, address); |
69e68b4f KS |
944 | } |
945 | ||
080dbb61 AK |
946 | static struct page *follow_pmd_mask(struct vm_area_struct *vma, |
947 | unsigned long address, pud_t *pudp, | |
df06b37f KB |
948 | unsigned int flags, |
949 | struct follow_page_context *ctx) | |
69e68b4f | 950 | { |
68827280 | 951 | pmd_t *pmd, pmdval; |
69e68b4f KS |
952 | spinlock_t *ptl; |
953 | struct page *page; | |
954 | struct mm_struct *mm = vma->vm_mm; | |
955 | ||
080dbb61 | 956 | pmd = pmd_offset(pudp, address); |
26e1a0c3 | 957 | pmdval = pmdp_get_lockless(pmd); |
68827280 | 958 | if (pmd_none(pmdval)) |
878b0c45 | 959 | return no_page_table(vma, flags, address); |
f7355e99 | 960 | if (!pmd_present(pmdval)) |
878b0c45 | 961 | return no_page_table(vma, flags, address); |
68827280 | 962 | if (pmd_devmap(pmdval)) { |
3565fce3 | 963 | ptl = pmd_lock(mm, pmd); |
df06b37f | 964 | page = follow_devmap_pmd(vma, address, pmd, flags, &ctx->pgmap); |
3565fce3 DW |
965 | spin_unlock(ptl); |
966 | if (page) | |
967 | return page; | |
878b0c45 | 968 | return no_page_table(vma, flags, address); |
3565fce3 | 969 | } |
4418c522 | 970 | if (likely(!pmd_leaf(pmdval))) |
df06b37f | 971 | return follow_page_pte(vma, address, pmd, flags, &ctx->pgmap); |
6742d293 | 972 | |
d74943a2 | 973 | if (pmd_protnone(pmdval) && !gup_can_follow_protnone(vma, flags)) |
878b0c45 | 974 | return no_page_table(vma, flags, address); |
db08f203 | 975 | |
6742d293 | 976 | ptl = pmd_lock(mm, pmd); |
4418c522 PX |
977 | pmdval = *pmd; |
978 | if (unlikely(!pmd_present(pmdval))) { | |
84c3fc4e | 979 | spin_unlock(ptl); |
878b0c45 | 980 | return no_page_table(vma, flags, address); |
84c3fc4e | 981 | } |
4418c522 | 982 | if (unlikely(!pmd_leaf(pmdval))) { |
6742d293 | 983 | spin_unlock(ptl); |
df06b37f | 984 | return follow_page_pte(vma, address, pmd, flags, &ctx->pgmap); |
6742d293 | 985 | } |
4418c522 | 986 | if (pmd_trans_huge(pmdval) && (flags & FOLL_SPLIT_PMD)) { |
2378118b HD |
987 | spin_unlock(ptl); |
988 | split_huge_pmd(vma, pmd, address); | |
989 | /* If pmd was left empty, stuff a page table in there quickly */ | |
990 | return pte_alloc(mm, pmd) ? ERR_PTR(-ENOMEM) : | |
df06b37f | 991 | follow_page_pte(vma, address, pmd, flags, &ctx->pgmap); |
69e68b4f | 992 | } |
4418c522 | 993 | page = follow_huge_pmd(vma, address, pmd, flags, ctx); |
6742d293 | 994 | spin_unlock(ptl); |
6742d293 | 995 | return page; |
4bbd4c77 KS |
996 | } |
997 | ||
080dbb61 AK |
998 | static struct page *follow_pud_mask(struct vm_area_struct *vma, |
999 | unsigned long address, p4d_t *p4dp, | |
df06b37f KB |
1000 | unsigned int flags, |
1001 | struct follow_page_context *ctx) | |
080dbb61 | 1002 | { |
caf8cab7 | 1003 | pud_t *pudp, pud; |
080dbb61 AK |
1004 | spinlock_t *ptl; |
1005 | struct page *page; | |
1006 | struct mm_struct *mm = vma->vm_mm; | |
1007 | ||
caf8cab7 PX |
1008 | pudp = pud_offset(p4dp, address); |
1009 | pud = READ_ONCE(*pudp); | |
1b167618 | 1010 | if (!pud_present(pud)) |
878b0c45 | 1011 | return no_page_table(vma, flags, address); |
1b167618 | 1012 | if (pud_leaf(pud)) { |
caf8cab7 | 1013 | ptl = pud_lock(mm, pudp); |
1b167618 | 1014 | page = follow_huge_pud(vma, address, pudp, flags, ctx); |
080dbb61 AK |
1015 | spin_unlock(ptl); |
1016 | if (page) | |
1017 | return page; | |
878b0c45 | 1018 | return no_page_table(vma, flags, address); |
080dbb61 | 1019 | } |
caf8cab7 | 1020 | if (unlikely(pud_bad(pud))) |
878b0c45 | 1021 | return no_page_table(vma, flags, address); |
080dbb61 | 1022 | |
caf8cab7 | 1023 | return follow_pmd_mask(vma, address, pudp, flags, ctx); |
080dbb61 AK |
1024 | } |
1025 | ||
080dbb61 AK |
1026 | static struct page *follow_p4d_mask(struct vm_area_struct *vma, |
1027 | unsigned long address, pgd_t *pgdp, | |
df06b37f KB |
1028 | unsigned int flags, |
1029 | struct follow_page_context *ctx) | |
080dbb61 | 1030 | { |
e6fd5564 | 1031 | p4d_t *p4dp, p4d; |
080dbb61 | 1032 | |
e6fd5564 PX |
1033 | p4dp = p4d_offset(pgdp, address); |
1034 | p4d = READ_ONCE(*p4dp); | |
1965e933 | 1035 | BUILD_BUG_ON(p4d_leaf(p4d)); |
a12083d7 | 1036 | |
a12083d7 | 1037 | if (!p4d_present(p4d) || p4d_bad(p4d)) |
878b0c45 | 1038 | return no_page_table(vma, flags, address); |
080dbb61 | 1039 | |
e6fd5564 | 1040 | return follow_pud_mask(vma, address, p4dp, flags, ctx); |
080dbb61 AK |
1041 | } |
1042 | ||
1043 | /** | |
1044 | * follow_page_mask - look up a page descriptor from a user-virtual address | |
1045 | * @vma: vm_area_struct mapping @address | |
1046 | * @address: virtual address to look up | |
1047 | * @flags: flags modifying lookup behaviour | |
78179556 MR |
1048 | * @ctx: contains dev_pagemap for %ZONE_DEVICE memory pinning and a |
1049 | * pointer to output page_mask | |
080dbb61 AK |
1050 | * |
1051 | * @flags can have FOLL_ flags set, defined in <linux/mm.h> | |
1052 | * | |
78179556 MR |
1053 | * When getting pages from ZONE_DEVICE memory, the @ctx->pgmap caches |
1054 | * the device's dev_pagemap metadata to avoid repeating expensive lookups. | |
1055 | * | |
a7f22660 DH |
1056 | * When getting an anonymous page and the caller has to trigger unsharing |
1057 | * of a shared anonymous page first, -EMLINK is returned. The caller should | |
1058 | * trigger a fault with FAULT_FLAG_UNSHARE set. Note that unsharing is only | |
1059 | * relevant with FOLL_PIN and !FOLL_WRITE. | |
1060 | * | |
78179556 MR |
1061 | * On output, the @ctx->page_mask is set according to the size of the page. |
1062 | * | |
1063 | * Return: the mapped (struct page *), %NULL if no mapping exists, or | |
080dbb61 AK |
1064 | * an error pointer if there is a mapping to something not represented |
1065 | * by a page descriptor (see also vm_normal_page()). | |
1066 | */ | |
a7030aea | 1067 | static struct page *follow_page_mask(struct vm_area_struct *vma, |
080dbb61 | 1068 | unsigned long address, unsigned int flags, |
df06b37f | 1069 | struct follow_page_context *ctx) |
080dbb61 AK |
1070 | { |
1071 | pgd_t *pgd; | |
080dbb61 | 1072 | struct mm_struct *mm = vma->vm_mm; |
9cb28da5 | 1073 | struct page *page; |
080dbb61 | 1074 | |
9cb28da5 | 1075 | vma_pgtable_walk_begin(vma); |
080dbb61 | 1076 | |
9cb28da5 | 1077 | ctx->page_mask = 0; |
080dbb61 AK |
1078 | pgd = pgd_offset(mm, address); |
1079 | ||
8268614b | 1080 | if (pgd_none(*pgd) || unlikely(pgd_bad(*pgd))) |
a12083d7 PX |
1081 | page = no_page_table(vma, flags, address); |
1082 | else | |
1083 | page = follow_p4d_mask(vma, address, pgd, flags, ctx); | |
080dbb61 | 1084 | |
9cb28da5 PX |
1085 | vma_pgtable_walk_end(vma); |
1086 | ||
a12083d7 | 1087 | return page; |
df06b37f KB |
1088 | } |
1089 | ||
f2b495ca KS |
1090 | static int get_gate_page(struct mm_struct *mm, unsigned long address, |
1091 | unsigned int gup_flags, struct vm_area_struct **vma, | |
1092 | struct page **page) | |
1093 | { | |
1094 | pgd_t *pgd; | |
c2febafc | 1095 | p4d_t *p4d; |
f2b495ca KS |
1096 | pud_t *pud; |
1097 | pmd_t *pmd; | |
1098 | pte_t *pte; | |
c33c7948 | 1099 | pte_t entry; |
f2b495ca KS |
1100 | int ret = -EFAULT; |
1101 | ||
1102 | /* user gate pages are read-only */ | |
1103 | if (gup_flags & FOLL_WRITE) | |
1104 | return -EFAULT; | |
0cad6736 | 1105 | pgd = pgd_offset(mm, address); |
b5d1c39f AL |
1106 | if (pgd_none(*pgd)) |
1107 | return -EFAULT; | |
c2febafc | 1108 | p4d = p4d_offset(pgd, address); |
b5d1c39f AL |
1109 | if (p4d_none(*p4d)) |
1110 | return -EFAULT; | |
c2febafc | 1111 | pud = pud_offset(p4d, address); |
b5d1c39f AL |
1112 | if (pud_none(*pud)) |
1113 | return -EFAULT; | |
f2b495ca | 1114 | pmd = pmd_offset(pud, address); |
84c3fc4e | 1115 | if (!pmd_present(*pmd)) |
f2b495ca | 1116 | return -EFAULT; |
f2b495ca | 1117 | pte = pte_offset_map(pmd, address); |
04dee9e8 HD |
1118 | if (!pte) |
1119 | return -EFAULT; | |
c33c7948 RR |
1120 | entry = ptep_get(pte); |
1121 | if (pte_none(entry)) | |
f2b495ca KS |
1122 | goto unmap; |
1123 | *vma = get_gate_vma(mm); | |
1124 | if (!page) | |
1125 | goto out; | |
c33c7948 | 1126 | *page = vm_normal_page(*vma, address, entry); |
f2b495ca | 1127 | if (!*page) { |
c33c7948 | 1128 | if ((gup_flags & FOLL_DUMP) || !is_zero_pfn(pte_pfn(entry))) |
f2b495ca | 1129 | goto unmap; |
c33c7948 | 1130 | *page = pte_page(entry); |
f2b495ca | 1131 | } |
f442fa61 | 1132 | ret = try_grab_folio(page_folio(*page), 1, gup_flags); |
0f089235 | 1133 | if (unlikely(ret)) |
8fde12ca | 1134 | goto unmap; |
f2b495ca KS |
1135 | out: |
1136 | ret = 0; | |
1137 | unmap: | |
1138 | pte_unmap(pte); | |
1139 | return ret; | |
1140 | } | |
1141 | ||
9a95f3cf | 1142 | /* |
9a863a6a JG |
1143 | * mmap_lock must be held on entry. If @flags has FOLL_UNLOCKABLE but not |
1144 | * FOLL_NOWAIT, the mmap_lock may be released. If it is, *@locked will be set | |
1145 | * to 0 and -EBUSY returned. | |
9a95f3cf | 1146 | */ |
64019a2e | 1147 | static int faultin_page(struct vm_area_struct *vma, |
47872953 | 1148 | unsigned long address, unsigned int flags, bool unshare, |
a7f22660 | 1149 | int *locked) |
16744483 | 1150 | { |
16744483 | 1151 | unsigned int fault_flags = 0; |
2b740303 | 1152 | vm_fault_t ret; |
16744483 | 1153 | |
47872953 | 1154 | if (flags & FOLL_NOFAULT) |
55b8fe70 | 1155 | return -EFAULT; |
47872953 | 1156 | if (flags & FOLL_WRITE) |
16744483 | 1157 | fault_flags |= FAULT_FLAG_WRITE; |
47872953 | 1158 | if (flags & FOLL_REMOTE) |
1b2ee126 | 1159 | fault_flags |= FAULT_FLAG_REMOTE; |
47872953 | 1160 | if (flags & FOLL_UNLOCKABLE) { |
71335f37 | 1161 | fault_flags |= FAULT_FLAG_ALLOW_RETRY | FAULT_FLAG_KILLABLE; |
93c5c61d PX |
1162 | /* |
1163 | * FAULT_FLAG_INTERRUPTIBLE is opt-in. GUP callers must set | |
1164 | * FOLL_INTERRUPTIBLE to enable FAULT_FLAG_INTERRUPTIBLE. | |
1165 | * That's because some callers may not be prepared to | |
1166 | * handle early exits caused by non-fatal signals. | |
1167 | */ | |
47872953 | 1168 | if (flags & FOLL_INTERRUPTIBLE) |
93c5c61d PX |
1169 | fault_flags |= FAULT_FLAG_INTERRUPTIBLE; |
1170 | } | |
47872953 | 1171 | if (flags & FOLL_NOWAIT) |
16744483 | 1172 | fault_flags |= FAULT_FLAG_ALLOW_RETRY | FAULT_FLAG_RETRY_NOWAIT; |
47872953 | 1173 | if (flags & FOLL_TRIED) { |
4426e945 PX |
1174 | /* |
1175 | * Note: FAULT_FLAG_ALLOW_RETRY and FAULT_FLAG_TRIED | |
1176 | * can co-exist | |
1177 | */ | |
234b239b ALC |
1178 | fault_flags |= FAULT_FLAG_TRIED; |
1179 | } | |
a7f22660 DH |
1180 | if (unshare) { |
1181 | fault_flags |= FAULT_FLAG_UNSHARE; | |
1182 | /* FAULT_FLAG_WRITE and FAULT_FLAG_UNSHARE are incompatible */ | |
1183 | VM_BUG_ON(fault_flags & FAULT_FLAG_WRITE); | |
1184 | } | |
16744483 | 1185 | |
bce617ed | 1186 | ret = handle_mm_fault(vma, address, fault_flags, NULL); |
d9272525 PX |
1187 | |
1188 | if (ret & VM_FAULT_COMPLETED) { | |
1189 | /* | |
1190 | * With FAULT_FLAG_RETRY_NOWAIT we'll never release the | |
1191 | * mmap lock in the page fault handler. Sanity check this. | |
1192 | */ | |
1193 | WARN_ON_ONCE(fault_flags & FAULT_FLAG_RETRY_NOWAIT); | |
9a863a6a JG |
1194 | *locked = 0; |
1195 | ||
d9272525 PX |
1196 | /* |
1197 | * We should do the same as VM_FAULT_RETRY, but let's not | |
1198 | * return -EBUSY since that's not reflecting the reality of | |
1199 | * what has happened - we've just fully completed a page | |
1200 | * fault, with the mmap lock released. Use -EAGAIN to show | |
1201 | * that we want to take the mmap lock _again_. | |
1202 | */ | |
1203 | return -EAGAIN; | |
1204 | } | |
1205 | ||
16744483 | 1206 | if (ret & VM_FAULT_ERROR) { |
47872953 | 1207 | int err = vm_fault_to_errno(ret, flags); |
9a291a7c JM |
1208 | |
1209 | if (err) | |
1210 | return err; | |
16744483 KS |
1211 | BUG(); |
1212 | } | |
1213 | ||
16744483 | 1214 | if (ret & VM_FAULT_RETRY) { |
9a863a6a | 1215 | if (!(fault_flags & FAULT_FLAG_RETRY_NOWAIT)) |
4f6da934 | 1216 | *locked = 0; |
16744483 KS |
1217 | return -EBUSY; |
1218 | } | |
1219 | ||
16744483 KS |
1220 | return 0; |
1221 | } | |
1222 | ||
8ac26843 LS |
1223 | /* |
1224 | * Writing to file-backed mappings which require folio dirty tracking using GUP | |
1225 | * is a fundamentally broken operation, as kernel write access to GUP mappings | |
1226 | * do not adhere to the semantics expected by a file system. | |
1227 | * | |
1228 | * Consider the following scenario:- | |
1229 | * | |
1230 | * 1. A folio is written to via GUP which write-faults the memory, notifying | |
1231 | * the file system and dirtying the folio. | |
1232 | * 2. Later, writeback is triggered, resulting in the folio being cleaned and | |
1233 | * the PTE being marked read-only. | |
1234 | * 3. The GUP caller writes to the folio, as it is mapped read/write via the | |
1235 | * direct mapping. | |
1236 | * 4. The GUP caller, now done with the page, unpins it and sets it dirty | |
1237 | * (though it does not have to). | |
1238 | * | |
1239 | * This results in both data being written to a folio without writenotify, and | |
1240 | * the folio being dirtied unexpectedly (if the caller decides to do so). | |
1241 | */ | |
1242 | static bool writable_file_mapping_allowed(struct vm_area_struct *vma, | |
1243 | unsigned long gup_flags) | |
1244 | { | |
1245 | /* | |
1246 | * If we aren't pinning then no problematic write can occur. A long term | |
1247 | * pin is the most egregious case so this is the case we disallow. | |
1248 | */ | |
1249 | if ((gup_flags & (FOLL_PIN | FOLL_LONGTERM)) != | |
1250 | (FOLL_PIN | FOLL_LONGTERM)) | |
1251 | return true; | |
1252 | ||
1253 | /* | |
1254 | * If the VMA does not require dirty tracking then no problematic write | |
1255 | * can occur either. | |
1256 | */ | |
1257 | return !vma_needs_dirty_tracking(vma); | |
1258 | } | |
1259 | ||
fa5bb209 KS |
1260 | static int check_vma_flags(struct vm_area_struct *vma, unsigned long gup_flags) |
1261 | { | |
1262 | vm_flags_t vm_flags = vma->vm_flags; | |
1b2ee126 DH |
1263 | int write = (gup_flags & FOLL_WRITE); |
1264 | int foreign = (gup_flags & FOLL_REMOTE); | |
8ac26843 | 1265 | bool vma_anon = vma_is_anonymous(vma); |
fa5bb209 KS |
1266 | |
1267 | if (vm_flags & (VM_IO | VM_PFNMAP)) | |
1268 | return -EFAULT; | |
1269 | ||
8ac26843 | 1270 | if ((gup_flags & FOLL_ANON) && !vma_anon) |
7f7ccc2c WT |
1271 | return -EFAULT; |
1272 | ||
52650c8b JG |
1273 | if ((gup_flags & FOLL_LONGTERM) && vma_is_fsdax(vma)) |
1274 | return -EOPNOTSUPP; | |
1275 | ||
8977752c DH |
1276 | if ((gup_flags & FOLL_SPLIT_PMD) && is_vm_hugetlb_page(vma)) |
1277 | return -EOPNOTSUPP; | |
1278 | ||
1507f512 MR |
1279 | if (vma_is_secretmem(vma)) |
1280 | return -EFAULT; | |
1281 | ||
1b2ee126 | 1282 | if (write) { |
8ac26843 LS |
1283 | if (!vma_anon && |
1284 | !writable_file_mapping_allowed(vma, gup_flags)) | |
1285 | return -EFAULT; | |
1286 | ||
6beb9958 | 1287 | if (!(vm_flags & VM_WRITE) || (vm_flags & VM_SHADOW_STACK)) { |
fa5bb209 KS |
1288 | if (!(gup_flags & FOLL_FORCE)) |
1289 | return -EFAULT; | |
1290 | /* | |
1291 | * We used to let the write,force case do COW in a | |
1292 | * VM_MAYWRITE VM_SHARED !VM_WRITE vma, so ptrace could | |
1293 | * set a breakpoint in a read-only mapping of an | |
1294 | * executable, without corrupting the file (yet only | |
1295 | * when that file had been opened for writing!). | |
1296 | * Anon pages in shared mappings are surprising: now | |
1297 | * just reject it. | |
1298 | */ | |
46435364 | 1299 | if (!is_cow_mapping(vm_flags)) |
fa5bb209 | 1300 | return -EFAULT; |
fa5bb209 KS |
1301 | } |
1302 | } else if (!(vm_flags & VM_READ)) { | |
1303 | if (!(gup_flags & FOLL_FORCE)) | |
1304 | return -EFAULT; | |
1305 | /* | |
1306 | * Is there actually any vma we can reach here which does not | |
1307 | * have VM_MAYREAD set? | |
1308 | */ | |
1309 | if (!(vm_flags & VM_MAYREAD)) | |
1310 | return -EFAULT; | |
1311 | } | |
d61172b4 DH |
1312 | /* |
1313 | * gups are always data accesses, not instruction | |
1314 | * fetches, so execute=false here | |
1315 | */ | |
1316 | if (!arch_vma_access_permitted(vma, write, false, foreign)) | |
33a709b2 | 1317 | return -EFAULT; |
fa5bb209 KS |
1318 | return 0; |
1319 | } | |
1320 | ||
6cd06ab1 LT |
1321 | /* |
1322 | * This is "vma_lookup()", but with a warning if we would have | |
1323 | * historically expanded the stack in the GUP code. | |
1324 | */ | |
1325 | static struct vm_area_struct *gup_vma_lookup(struct mm_struct *mm, | |
1326 | unsigned long addr) | |
1327 | { | |
1328 | #ifdef CONFIG_STACK_GROWSUP | |
1329 | return vma_lookup(mm, addr); | |
1330 | #else | |
1331 | static volatile unsigned long next_warn; | |
1332 | struct vm_area_struct *vma; | |
1333 | unsigned long now, next; | |
1334 | ||
1335 | vma = find_vma(mm, addr); | |
1336 | if (!vma || (addr >= vma->vm_start)) | |
1337 | return vma; | |
1338 | ||
1339 | /* Only warn for half-way relevant accesses */ | |
1340 | if (!(vma->vm_flags & VM_GROWSDOWN)) | |
1341 | return NULL; | |
1342 | if (vma->vm_start - addr > 65536) | |
1343 | return NULL; | |
1344 | ||
1345 | /* Let's not warn more than once an hour.. */ | |
1346 | now = jiffies; next = next_warn; | |
1347 | if (next && time_before(now, next)) | |
1348 | return NULL; | |
1349 | next_warn = now + 60*60*HZ; | |
1350 | ||
1351 | /* Let people know things may have changed. */ | |
1352 | pr_warn("GUP no longer grows the stack in %s (%d): %lx-%lx (%lx)\n", | |
1353 | current->comm, task_pid_nr(current), | |
1354 | vma->vm_start, vma->vm_end, addr); | |
1355 | dump_stack(); | |
1356 | return NULL; | |
1357 | #endif | |
1358 | } | |
1359 | ||
4bbd4c77 KS |
1360 | /** |
1361 | * __get_user_pages() - pin user pages in memory | |
4bbd4c77 KS |
1362 | * @mm: mm_struct of target mm |
1363 | * @start: starting user address | |
1364 | * @nr_pages: number of pages from start to pin | |
1365 | * @gup_flags: flags modifying pin behaviour | |
1366 | * @pages: array that receives pointers to the pages pinned. | |
1367 | * Should be at least nr_pages long. Or NULL, if caller | |
1368 | * only intends to ensure the pages are faulted in. | |
c1e8d7c6 | 1369 | * @locked: whether we're still with the mmap_lock held |
4bbd4c77 | 1370 | * |
d2dfbe47 LX |
1371 | * Returns either number of pages pinned (which may be less than the |
1372 | * number requested), or an error. Details about the return value: | |
1373 | * | |
1374 | * -- If nr_pages is 0, returns 0. | |
1375 | * -- If nr_pages is >0, but no pages were pinned, returns -errno. | |
1376 | * -- If nr_pages is >0, and some pages were pinned, returns the number of | |
1377 | * pages pinned. Again, this may be less than nr_pages. | |
2d3a36a4 | 1378 | * -- 0 return value is possible when the fault would need to be retried. |
d2dfbe47 LX |
1379 | * |
1380 | * The caller is responsible for releasing returned @pages, via put_page(). | |
1381 | * | |
c1e8d7c6 | 1382 | * Must be called with mmap_lock held. It may be released. See below. |
4bbd4c77 KS |
1383 | * |
1384 | * __get_user_pages walks a process's page tables and takes a reference to | |
1385 | * each struct page that each user address corresponds to at a given | |
1386 | * instant. That is, it takes the page that would be accessed if a user | |
1387 | * thread accesses the given user virtual address at that instant. | |
1388 | * | |
1389 | * This does not guarantee that the page exists in the user mappings when | |
1390 | * __get_user_pages returns, and there may even be a completely different | |
1391 | * page there in some cases (eg. if mmapped pagecache has been invalidated | |
c5acf1f6 | 1392 | * and subsequently re-faulted). However it does guarantee that the page |
4bbd4c77 KS |
1393 | * won't be freed completely. And mostly callers simply care that the page |
1394 | * contains data that was valid *at some point in time*. Typically, an IO | |
1395 | * or similar operation cannot guarantee anything stronger anyway because | |
1396 | * locks can't be held over the syscall boundary. | |
1397 | * | |
1398 | * If @gup_flags & FOLL_WRITE == 0, the page must not be written to. If | |
1399 | * the page is written to, set_page_dirty (or set_page_dirty_lock, as | |
1400 | * appropriate) must be called after the page is finished with, and | |
1401 | * before put_page is called. | |
1402 | * | |
9a863a6a JG |
1403 | * If FOLL_UNLOCKABLE is set without FOLL_NOWAIT then the mmap_lock may |
1404 | * be released. If this happens *@locked will be set to 0 on return. | |
9a95f3cf | 1405 | * |
9a863a6a JG |
1406 | * A caller using such a combination of @gup_flags must therefore hold the |
1407 | * mmap_lock for reading only, and recognize when it's been released. Otherwise, | |
1408 | * it must be held for either reading or writing and will not be released. | |
4bbd4c77 KS |
1409 | * |
1410 | * In most cases, get_user_pages or get_user_pages_fast should be used | |
1411 | * instead of __get_user_pages. __get_user_pages should be used only if | |
1412 | * you need some special @gup_flags. | |
1413 | */ | |
64019a2e | 1414 | static long __get_user_pages(struct mm_struct *mm, |
4bbd4c77 KS |
1415 | unsigned long start, unsigned long nr_pages, |
1416 | unsigned int gup_flags, struct page **pages, | |
b2cac248 | 1417 | int *locked) |
4bbd4c77 | 1418 | { |
df06b37f | 1419 | long ret = 0, i = 0; |
fa5bb209 | 1420 | struct vm_area_struct *vma = NULL; |
df06b37f | 1421 | struct follow_page_context ctx = { NULL }; |
4bbd4c77 KS |
1422 | |
1423 | if (!nr_pages) | |
1424 | return 0; | |
1425 | ||
428e106a | 1426 | start = untagged_addr_remote(mm, start); |
f9652594 | 1427 | |
ede27b7e BH |
1428 | VM_WARN_ON_ONCE(!!pages != !!(gup_flags & (FOLL_GET | FOLL_PIN))); |
1429 | ||
1430 | /* FOLL_GET and FOLL_PIN are mutually exclusive. */ | |
1431 | VM_WARN_ON_ONCE((gup_flags & (FOLL_PIN | FOLL_GET)) == | |
1432 | (FOLL_PIN | FOLL_GET)); | |
4bbd4c77 | 1433 | |
4bbd4c77 | 1434 | do { |
fa5bb209 | 1435 | struct page *page; |
fa5bb209 KS |
1436 | unsigned int page_increm; |
1437 | ||
1438 | /* first iteration or cross vma bound */ | |
1439 | if (!vma || start >= vma->vm_end) { | |
631426ba DH |
1440 | /* |
1441 | * MADV_POPULATE_(READ|WRITE) wants to handle VMA | |
1442 | * lookups+error reporting differently. | |
1443 | */ | |
1444 | if (gup_flags & FOLL_MADV_POPULATE) { | |
1445 | vma = vma_lookup(mm, start); | |
1446 | if (!vma) { | |
1447 | ret = -ENOMEM; | |
1448 | goto out; | |
1449 | } | |
1450 | if (check_vma_flags(vma, gup_flags)) { | |
1451 | ret = -EINVAL; | |
1452 | goto out; | |
1453 | } | |
1454 | goto retry; | |
1455 | } | |
6cd06ab1 | 1456 | vma = gup_vma_lookup(mm, start); |
fa5bb209 | 1457 | if (!vma && in_gate_area(mm, start)) { |
fa5bb209 KS |
1458 | ret = get_gate_page(mm, start & PAGE_MASK, |
1459 | gup_flags, &vma, | |
ffe1e786 | 1460 | pages ? &page : NULL); |
fa5bb209 | 1461 | if (ret) |
08be37b7 | 1462 | goto out; |
df06b37f | 1463 | ctx.page_mask = 0; |
fa5bb209 KS |
1464 | goto next_page; |
1465 | } | |
4bbd4c77 | 1466 | |
52650c8b | 1467 | if (!vma) { |
df06b37f KB |
1468 | ret = -EFAULT; |
1469 | goto out; | |
1470 | } | |
52650c8b JG |
1471 | ret = check_vma_flags(vma, gup_flags); |
1472 | if (ret) | |
1473 | goto out; | |
fa5bb209 KS |
1474 | } |
1475 | retry: | |
1476 | /* | |
1477 | * If we have a pending SIGKILL, don't keep faulting pages and | |
1478 | * potentially allocating memory. | |
1479 | */ | |
fa45f116 | 1480 | if (fatal_signal_pending(current)) { |
d180870d | 1481 | ret = -EINTR; |
df06b37f KB |
1482 | goto out; |
1483 | } | |
fa5bb209 | 1484 | cond_resched(); |
df06b37f | 1485 | |
dc21e700 | 1486 | page = follow_page_mask(vma, start, gup_flags, &ctx); |
a7f22660 | 1487 | if (!page || PTR_ERR(page) == -EMLINK) { |
dc21e700 | 1488 | ret = faultin_page(vma, start, gup_flags, |
a7f22660 | 1489 | PTR_ERR(page) == -EMLINK, locked); |
fa5bb209 KS |
1490 | switch (ret) { |
1491 | case 0: | |
1492 | goto retry; | |
df06b37f | 1493 | case -EBUSY: |
d9272525 | 1494 | case -EAGAIN: |
df06b37f | 1495 | ret = 0; |
e4a9bc58 | 1496 | fallthrough; |
fa5bb209 KS |
1497 | case -EFAULT: |
1498 | case -ENOMEM: | |
1499 | case -EHWPOISON: | |
df06b37f | 1500 | goto out; |
4bbd4c77 | 1501 | } |
fa5bb209 | 1502 | BUG(); |
1027e443 KS |
1503 | } else if (PTR_ERR(page) == -EEXIST) { |
1504 | /* | |
1505 | * Proper page table entry exists, but no corresponding | |
65462462 JH |
1506 | * struct page. If the caller expects **pages to be |
1507 | * filled in, bail out now, because that can't be done | |
1508 | * for this page. | |
1027e443 | 1509 | */ |
65462462 JH |
1510 | if (pages) { |
1511 | ret = PTR_ERR(page); | |
1512 | goto out; | |
1513 | } | |
1027e443 | 1514 | } else if (IS_ERR(page)) { |
df06b37f KB |
1515 | ret = PTR_ERR(page); |
1516 | goto out; | |
1027e443 | 1517 | } |
ffe1e786 | 1518 | next_page: |
df06b37f | 1519 | page_increm = 1 + (~(start >> PAGE_SHIFT) & ctx.page_mask); |
fa5bb209 KS |
1520 | if (page_increm > nr_pages) |
1521 | page_increm = nr_pages; | |
57edfcfd PX |
1522 | |
1523 | if (pages) { | |
1524 | struct page *subpage; | |
1525 | unsigned int j; | |
1526 | ||
1527 | /* | |
1528 | * This must be a large folio (and doesn't need to | |
1529 | * be the whole folio; it can be part of it), do | |
1530 | * the refcount work for all the subpages too. | |
1531 | * | |
1532 | * NOTE: here the page may not be the head page | |
1533 | * e.g. when start addr is not thp-size aligned. | |
1534 | * try_grab_folio() should have taken care of tail | |
1535 | * pages. | |
1536 | */ | |
1537 | if (page_increm > 1) { | |
f442fa61 | 1538 | struct folio *folio = page_folio(page); |
57edfcfd PX |
1539 | |
1540 | /* | |
1541 | * Since we already hold refcount on the | |
1542 | * large folio, this should never fail. | |
1543 | */ | |
f442fa61 | 1544 | if (try_grab_folio(folio, page_increm - 1, |
dc21e700 | 1545 | gup_flags)) { |
57edfcfd PX |
1546 | /* |
1547 | * Release the 1st page ref if the | |
1548 | * folio is problematic, fail hard. | |
1549 | */ | |
dc21e700 | 1550 | gup_put_folio(folio, 1, gup_flags); |
57edfcfd PX |
1551 | ret = -EFAULT; |
1552 | goto out; | |
1553 | } | |
1554 | } | |
1555 | ||
1556 | for (j = 0; j < page_increm; j++) { | |
1557 | subpage = nth_page(page, j); | |
1558 | pages[i + j] = subpage; | |
1559 | flush_anon_page(vma, subpage, start + j * PAGE_SIZE); | |
1560 | flush_dcache_page(subpage); | |
1561 | } | |
1562 | } | |
1563 | ||
fa5bb209 KS |
1564 | i += page_increm; |
1565 | start += page_increm * PAGE_SIZE; | |
1566 | nr_pages -= page_increm; | |
4bbd4c77 | 1567 | } while (nr_pages); |
df06b37f KB |
1568 | out: |
1569 | if (ctx.pgmap) | |
1570 | put_dev_pagemap(ctx.pgmap); | |
1571 | return i ? i : ret; | |
4bbd4c77 | 1572 | } |
4bbd4c77 | 1573 | |
771ab430 TK |
1574 | static bool vma_permits_fault(struct vm_area_struct *vma, |
1575 | unsigned int fault_flags) | |
d4925e00 | 1576 | { |
1b2ee126 DH |
1577 | bool write = !!(fault_flags & FAULT_FLAG_WRITE); |
1578 | bool foreign = !!(fault_flags & FAULT_FLAG_REMOTE); | |
33a709b2 | 1579 | vm_flags_t vm_flags = write ? VM_WRITE : VM_READ; |
d4925e00 DH |
1580 | |
1581 | if (!(vm_flags & vma->vm_flags)) | |
1582 | return false; | |
1583 | ||
33a709b2 DH |
1584 | /* |
1585 | * The architecture might have a hardware protection | |
1b2ee126 | 1586 | * mechanism other than read/write that can deny access. |
d61172b4 DH |
1587 | * |
1588 | * gup always represents data access, not instruction | |
1589 | * fetches, so execute=false here: | |
33a709b2 | 1590 | */ |
d61172b4 | 1591 | if (!arch_vma_access_permitted(vma, write, false, foreign)) |
33a709b2 DH |
1592 | return false; |
1593 | ||
d4925e00 DH |
1594 | return true; |
1595 | } | |
1596 | ||
adc8cb40 | 1597 | /** |
4bbd4c77 | 1598 | * fixup_user_fault() - manually resolve a user page fault |
4bbd4c77 KS |
1599 | * @mm: mm_struct of target mm |
1600 | * @address: user address | |
1601 | * @fault_flags:flags to pass down to handle_mm_fault() | |
c1e8d7c6 | 1602 | * @unlocked: did we unlock the mmap_lock while retrying, maybe NULL if caller |
548b6a1e MC |
1603 | * does not allow retry. If NULL, the caller must guarantee |
1604 | * that fault_flags does not contain FAULT_FLAG_ALLOW_RETRY. | |
4bbd4c77 KS |
1605 | * |
1606 | * This is meant to be called in the specific scenario where for locking reasons | |
1607 | * we try to access user memory in atomic context (within a pagefault_disable() | |
1608 | * section), this returns -EFAULT, and we want to resolve the user fault before | |
1609 | * trying again. | |
1610 | * | |
1611 | * Typically this is meant to be used by the futex code. | |
1612 | * | |
1613 | * The main difference with get_user_pages() is that this function will | |
1614 | * unconditionally call handle_mm_fault() which will in turn perform all the | |
1615 | * necessary SW fixup of the dirty and young bits in the PTE, while | |
4a9e1cda | 1616 | * get_user_pages() only guarantees to update these in the struct page. |
4bbd4c77 KS |
1617 | * |
1618 | * This is important for some architectures where those bits also gate the | |
1619 | * access permission to the page because they are maintained in software. On | |
1620 | * such architectures, gup() will not be enough to make a subsequent access | |
1621 | * succeed. | |
1622 | * | |
c1e8d7c6 ML |
1623 | * This function will not return with an unlocked mmap_lock. So it has not the |
1624 | * same semantics wrt the @mm->mmap_lock as does filemap_fault(). | |
4bbd4c77 | 1625 | */ |
64019a2e | 1626 | int fixup_user_fault(struct mm_struct *mm, |
4a9e1cda DD |
1627 | unsigned long address, unsigned int fault_flags, |
1628 | bool *unlocked) | |
4bbd4c77 KS |
1629 | { |
1630 | struct vm_area_struct *vma; | |
8fed2f3c | 1631 | vm_fault_t ret; |
4a9e1cda | 1632 | |
428e106a | 1633 | address = untagged_addr_remote(mm, address); |
f9652594 | 1634 | |
4a9e1cda | 1635 | if (unlocked) |
71335f37 | 1636 | fault_flags |= FAULT_FLAG_ALLOW_RETRY | FAULT_FLAG_KILLABLE; |
4bbd4c77 | 1637 | |
4a9e1cda | 1638 | retry: |
6cd06ab1 | 1639 | vma = gup_vma_lookup(mm, address); |
8d7071af | 1640 | if (!vma) |
4bbd4c77 KS |
1641 | return -EFAULT; |
1642 | ||
d4925e00 | 1643 | if (!vma_permits_fault(vma, fault_flags)) |
4bbd4c77 KS |
1644 | return -EFAULT; |
1645 | ||
475f4dfc PX |
1646 | if ((fault_flags & FAULT_FLAG_KILLABLE) && |
1647 | fatal_signal_pending(current)) | |
1648 | return -EINTR; | |
1649 | ||
bce617ed | 1650 | ret = handle_mm_fault(vma, address, fault_flags, NULL); |
d9272525 PX |
1651 | |
1652 | if (ret & VM_FAULT_COMPLETED) { | |
1653 | /* | |
1654 | * NOTE: it's a pity that we need to retake the lock here | |
1655 | * to pair with the unlock() in the callers. Ideally we | |
1656 | * could tell the callers so they do not need to unlock. | |
1657 | */ | |
1658 | mmap_read_lock(mm); | |
1659 | *unlocked = true; | |
1660 | return 0; | |
1661 | } | |
1662 | ||
4bbd4c77 | 1663 | if (ret & VM_FAULT_ERROR) { |
9a291a7c JM |
1664 | int err = vm_fault_to_errno(ret, 0); |
1665 | ||
1666 | if (err) | |
1667 | return err; | |
4bbd4c77 KS |
1668 | BUG(); |
1669 | } | |
4a9e1cda DD |
1670 | |
1671 | if (ret & VM_FAULT_RETRY) { | |
d8ed45c5 | 1672 | mmap_read_lock(mm); |
475f4dfc PX |
1673 | *unlocked = true; |
1674 | fault_flags |= FAULT_FLAG_TRIED; | |
1675 | goto retry; | |
4a9e1cda DD |
1676 | } |
1677 | ||
4bbd4c77 KS |
1678 | return 0; |
1679 | } | |
add6a0cd | 1680 | EXPORT_SYMBOL_GPL(fixup_user_fault); |
4bbd4c77 | 1681 | |
93c5c61d PX |
1682 | /* |
1683 | * GUP always responds to fatal signals. When FOLL_INTERRUPTIBLE is | |
1684 | * specified, it'll also respond to generic signals. The caller of GUP | |
1685 | * that has FOLL_INTERRUPTIBLE should take care of the GUP interruption. | |
1686 | */ | |
1687 | static bool gup_signal_pending(unsigned int flags) | |
1688 | { | |
1689 | if (fatal_signal_pending(current)) | |
1690 | return true; | |
1691 | ||
1692 | if (!(flags & FOLL_INTERRUPTIBLE)) | |
1693 | return false; | |
1694 | ||
1695 | return signal_pending(current); | |
1696 | } | |
1697 | ||
2d3a36a4 | 1698 | /* |
b2a72dff JG |
1699 | * Locking: (*locked == 1) means that the mmap_lock has already been acquired by |
1700 | * the caller. This function may drop the mmap_lock. If it does so, then it will | |
1701 | * set (*locked = 0). | |
1702 | * | |
1703 | * (*locked == 0) means that the caller expects this function to acquire and | |
1704 | * drop the mmap_lock. Therefore, the value of *locked will still be zero when | |
1705 | * the function returns, even though it may have changed temporarily during | |
1706 | * function execution. | |
1707 | * | |
1708 | * Please note that this function, unlike __get_user_pages(), will not return 0 | |
1709 | * for nr_pages > 0, unless FOLL_NOWAIT is used. | |
2d3a36a4 | 1710 | */ |
64019a2e | 1711 | static __always_inline long __get_user_pages_locked(struct mm_struct *mm, |
f0818f47 AA |
1712 | unsigned long start, |
1713 | unsigned long nr_pages, | |
f0818f47 | 1714 | struct page **pages, |
e716712f | 1715 | int *locked, |
0fd71a56 | 1716 | unsigned int flags) |
f0818f47 | 1717 | { |
f0818f47 | 1718 | long ret, pages_done; |
b2a72dff | 1719 | bool must_unlock = false; |
f0818f47 | 1720 | |
9c4b2142 LS |
1721 | if (!nr_pages) |
1722 | return 0; | |
1723 | ||
b2a72dff JG |
1724 | /* |
1725 | * The internal caller expects GUP to manage the lock internally and the | |
1726 | * lock must be released when this returns. | |
1727 | */ | |
9a863a6a | 1728 | if (!*locked) { |
b2a72dff JG |
1729 | if (mmap_read_lock_killable(mm)) |
1730 | return -EAGAIN; | |
1731 | must_unlock = true; | |
1732 | *locked = 1; | |
f0818f47 | 1733 | } |
961ba472 JG |
1734 | else |
1735 | mmap_assert_locked(mm); | |
f0818f47 | 1736 | |
a458b76a AA |
1737 | if (flags & FOLL_PIN) |
1738 | mm_set_has_pinned_flag(&mm->flags); | |
008cfe44 | 1739 | |
eddb1c22 JH |
1740 | /* |
1741 | * FOLL_PIN and FOLL_GET are mutually exclusive. Traditional behavior | |
1742 | * is to set FOLL_GET if the caller wants pages[] filled in (but has | |
1743 | * carelessly failed to specify FOLL_GET), so keep doing that, but only | |
1744 | * for FOLL_GET, not for the newer FOLL_PIN. | |
1745 | * | |
1746 | * FOLL_PIN always expects pages to be non-null, but no need to assert | |
1747 | * that here, as any failures will be obvious enough. | |
1748 | */ | |
1749 | if (pages && !(flags & FOLL_PIN)) | |
f0818f47 | 1750 | flags |= FOLL_GET; |
f0818f47 AA |
1751 | |
1752 | pages_done = 0; | |
f0818f47 | 1753 | for (;;) { |
64019a2e | 1754 | ret = __get_user_pages(mm, start, nr_pages, flags, pages, |
b2cac248 | 1755 | locked); |
f04740f5 | 1756 | if (!(flags & FOLL_UNLOCKABLE)) { |
f0818f47 | 1757 | /* VM_FAULT_RETRY couldn't trigger, bypass */ |
f04740f5 JG |
1758 | pages_done = ret; |
1759 | break; | |
1760 | } | |
f0818f47 | 1761 | |
d9272525 | 1762 | /* VM_FAULT_RETRY or VM_FAULT_COMPLETED cannot return errors */ |
f0818f47 AA |
1763 | if (!*locked) { |
1764 | BUG_ON(ret < 0); | |
1765 | BUG_ON(ret >= nr_pages); | |
1766 | } | |
1767 | ||
f0818f47 AA |
1768 | if (ret > 0) { |
1769 | nr_pages -= ret; | |
1770 | pages_done += ret; | |
1771 | if (!nr_pages) | |
1772 | break; | |
1773 | } | |
1774 | if (*locked) { | |
96312e61 AA |
1775 | /* |
1776 | * VM_FAULT_RETRY didn't trigger or it was a | |
1777 | * FOLL_NOWAIT. | |
1778 | */ | |
f0818f47 AA |
1779 | if (!pages_done) |
1780 | pages_done = ret; | |
1781 | break; | |
1782 | } | |
df17277b MR |
1783 | /* |
1784 | * VM_FAULT_RETRY triggered, so seek to the faulting offset. | |
1785 | * For the prefault case (!pages) we only update counts. | |
1786 | */ | |
1787 | if (likely(pages)) | |
1788 | pages += ret; | |
f0818f47 | 1789 | start += ret << PAGE_SHIFT; |
b2a72dff JG |
1790 | |
1791 | /* The lock was temporarily dropped, so we must unlock later */ | |
1792 | must_unlock = true; | |
f0818f47 | 1793 | |
4426e945 | 1794 | retry: |
f0818f47 AA |
1795 | /* |
1796 | * Repeat on the address that fired VM_FAULT_RETRY | |
4426e945 PX |
1797 | * with both FAULT_FLAG_ALLOW_RETRY and |
1798 | * FAULT_FLAG_TRIED. Note that GUP can be interrupted | |
93c5c61d PX |
1799 | * by fatal signals of even common signals, depending on |
1800 | * the caller's request. So we need to check it before we | |
4426e945 | 1801 | * start trying again otherwise it can loop forever. |
f0818f47 | 1802 | */ |
93c5c61d | 1803 | if (gup_signal_pending(flags)) { |
ae46d2aa HD |
1804 | if (!pages_done) |
1805 | pages_done = -EINTR; | |
4426e945 | 1806 | break; |
ae46d2aa | 1807 | } |
4426e945 | 1808 | |
d8ed45c5 | 1809 | ret = mmap_read_lock_killable(mm); |
71335f37 PX |
1810 | if (ret) { |
1811 | BUG_ON(ret > 0); | |
1812 | if (!pages_done) | |
1813 | pages_done = ret; | |
1814 | break; | |
1815 | } | |
4426e945 | 1816 | |
c7b6a566 | 1817 | *locked = 1; |
64019a2e | 1818 | ret = __get_user_pages(mm, start, 1, flags | FOLL_TRIED, |
b2cac248 | 1819 | pages, locked); |
4426e945 PX |
1820 | if (!*locked) { |
1821 | /* Continue to retry until we succeeded */ | |
1822 | BUG_ON(ret != 0); | |
1823 | goto retry; | |
1824 | } | |
f0818f47 AA |
1825 | if (ret != 1) { |
1826 | BUG_ON(ret > 1); | |
1827 | if (!pages_done) | |
1828 | pages_done = ret; | |
1829 | break; | |
1830 | } | |
1831 | nr_pages--; | |
1832 | pages_done++; | |
1833 | if (!nr_pages) | |
1834 | break; | |
df17277b MR |
1835 | if (likely(pages)) |
1836 | pages++; | |
f0818f47 AA |
1837 | start += PAGE_SIZE; |
1838 | } | |
b2a72dff | 1839 | if (must_unlock && *locked) { |
f0818f47 | 1840 | /* |
b2a72dff JG |
1841 | * We either temporarily dropped the lock, or the caller |
1842 | * requested that we both acquire and drop the lock. Either way, | |
1843 | * we must now unlock, and notify the caller of that state. | |
f0818f47 | 1844 | */ |
d8ed45c5 | 1845 | mmap_read_unlock(mm); |
f0818f47 AA |
1846 | *locked = 0; |
1847 | } | |
9c4b2142 LS |
1848 | |
1849 | /* | |
1850 | * Failing to pin anything implies something has gone wrong (except when | |
1851 | * FOLL_NOWAIT is specified). | |
1852 | */ | |
1853 | if (WARN_ON_ONCE(pages_done == 0 && !(flags & FOLL_NOWAIT))) | |
1854 | return -EFAULT; | |
1855 | ||
f0818f47 AA |
1856 | return pages_done; |
1857 | } | |
1858 | ||
d3649f68 CH |
1859 | /** |
1860 | * populate_vma_page_range() - populate a range of pages in the vma. | |
1861 | * @vma: target vma | |
1862 | * @start: start address | |
1863 | * @end: end address | |
c1e8d7c6 | 1864 | * @locked: whether the mmap_lock is still held |
d3649f68 CH |
1865 | * |
1866 | * This takes care of mlocking the pages too if VM_LOCKED is set. | |
1867 | * | |
0a36f7f8 TY |
1868 | * Return either number of pages pinned in the vma, or a negative error |
1869 | * code on error. | |
d3649f68 | 1870 | * |
c1e8d7c6 | 1871 | * vma->vm_mm->mmap_lock must be held. |
d3649f68 | 1872 | * |
4f6da934 | 1873 | * If @locked is NULL, it may be held for read or write and will |
d3649f68 CH |
1874 | * be unperturbed. |
1875 | * | |
4f6da934 PX |
1876 | * If @locked is non-NULL, it must held for read only and may be |
1877 | * released. If it's released, *@locked will be set to 0. | |
d3649f68 CH |
1878 | */ |
1879 | long populate_vma_page_range(struct vm_area_struct *vma, | |
4f6da934 | 1880 | unsigned long start, unsigned long end, int *locked) |
d3649f68 CH |
1881 | { |
1882 | struct mm_struct *mm = vma->vm_mm; | |
1883 | unsigned long nr_pages = (end - start) / PAGE_SIZE; | |
9a863a6a | 1884 | int local_locked = 1; |
d3649f68 | 1885 | int gup_flags; |
ece369c7 | 1886 | long ret; |
d3649f68 | 1887 | |
be51eb18 ML |
1888 | VM_BUG_ON(!PAGE_ALIGNED(start)); |
1889 | VM_BUG_ON(!PAGE_ALIGNED(end)); | |
d3649f68 CH |
1890 | VM_BUG_ON_VMA(start < vma->vm_start, vma); |
1891 | VM_BUG_ON_VMA(end > vma->vm_end, vma); | |
42fc5414 | 1892 | mmap_assert_locked(mm); |
d3649f68 | 1893 | |
b67bf49c HD |
1894 | /* |
1895 | * Rightly or wrongly, the VM_LOCKONFAULT case has never used | |
1896 | * faultin_page() to break COW, so it has no work to do here. | |
1897 | */ | |
d3649f68 | 1898 | if (vma->vm_flags & VM_LOCKONFAULT) |
b67bf49c HD |
1899 | return nr_pages; |
1900 | ||
1096bc93 LT |
1901 | /* ... similarly, we've never faulted in PROT_NONE pages */ |
1902 | if (!vma_is_accessible(vma)) | |
1903 | return -EFAULT; | |
1904 | ||
b67bf49c | 1905 | gup_flags = FOLL_TOUCH; |
d3649f68 CH |
1906 | /* |
1907 | * We want to touch writable mappings with a write fault in order | |
1908 | * to break COW, except for shared mappings because these don't COW | |
1909 | * and we would not want to dirty them for nothing. | |
1096bc93 LT |
1910 | * |
1911 | * Otherwise, do a read fault, and use FOLL_FORCE in case it's not | |
1912 | * readable (ie write-only or executable). | |
d3649f68 CH |
1913 | */ |
1914 | if ((vma->vm_flags & (VM_WRITE | VM_SHARED)) == VM_WRITE) | |
1915 | gup_flags |= FOLL_WRITE; | |
1096bc93 | 1916 | else |
d3649f68 CH |
1917 | gup_flags |= FOLL_FORCE; |
1918 | ||
f04740f5 JG |
1919 | if (locked) |
1920 | gup_flags |= FOLL_UNLOCKABLE; | |
1921 | ||
d3649f68 CH |
1922 | /* |
1923 | * We made sure addr is within a VMA, so the following will | |
1924 | * not result in a stack expansion that recurses back here. | |
1925 | */ | |
ece369c7 | 1926 | ret = __get_user_pages(mm, start, nr_pages, gup_flags, |
b2cac248 | 1927 | NULL, locked ? locked : &local_locked); |
ece369c7 HD |
1928 | lru_add_drain(); |
1929 | return ret; | |
d3649f68 CH |
1930 | } |
1931 | ||
4ca9b385 | 1932 | /* |
631426ba DH |
1933 | * faultin_page_range() - populate (prefault) page tables inside the |
1934 | * given range readable/writable | |
4ca9b385 DH |
1935 | * |
1936 | * This takes care of mlocking the pages, too, if VM_LOCKED is set. | |
1937 | * | |
631426ba | 1938 | * @mm: the mm to populate page tables in |
4ca9b385 DH |
1939 | * @start: start address |
1940 | * @end: end address | |
1941 | * @write: whether to prefault readable or writable | |
1942 | * @locked: whether the mmap_lock is still held | |
1943 | * | |
631426ba DH |
1944 | * Returns either number of processed pages in the MM, or a negative error |
1945 | * code on error (see __get_user_pages()). Note that this function reports | |
1946 | * errors related to VMAs, such as incompatible mappings, as expected by | |
1947 | * MADV_POPULATE_(READ|WRITE). | |
4ca9b385 | 1948 | * |
631426ba DH |
1949 | * The range must be page-aligned. |
1950 | * | |
1951 | * mm->mmap_lock must be held. If it's released, *@locked will be set to 0. | |
4ca9b385 | 1952 | */ |
631426ba DH |
1953 | long faultin_page_range(struct mm_struct *mm, unsigned long start, |
1954 | unsigned long end, bool write, int *locked) | |
4ca9b385 | 1955 | { |
4ca9b385 DH |
1956 | unsigned long nr_pages = (end - start) / PAGE_SIZE; |
1957 | int gup_flags; | |
ece369c7 | 1958 | long ret; |
4ca9b385 DH |
1959 | |
1960 | VM_BUG_ON(!PAGE_ALIGNED(start)); | |
1961 | VM_BUG_ON(!PAGE_ALIGNED(end)); | |
4ca9b385 DH |
1962 | mmap_assert_locked(mm); |
1963 | ||
1964 | /* | |
1965 | * FOLL_TOUCH: Mark page accessed and thereby young; will also mark | |
1966 | * the page dirty with FOLL_WRITE -- which doesn't make a | |
1967 | * difference with !FOLL_FORCE, because the page is writable | |
1968 | * in the page table. | |
1969 | * FOLL_HWPOISON: Return -EHWPOISON instead of -EFAULT when we hit | |
1970 | * a poisoned page. | |
4ca9b385 DH |
1971 | * !FOLL_FORCE: Require proper access permissions. |
1972 | */ | |
631426ba DH |
1973 | gup_flags = FOLL_TOUCH | FOLL_HWPOISON | FOLL_UNLOCKABLE | |
1974 | FOLL_MADV_POPULATE; | |
4ca9b385 DH |
1975 | if (write) |
1976 | gup_flags |= FOLL_WRITE; | |
1977 | ||
631426ba DH |
1978 | ret = __get_user_pages_locked(mm, start, nr_pages, NULL, locked, |
1979 | gup_flags); | |
ece369c7 HD |
1980 | lru_add_drain(); |
1981 | return ret; | |
4ca9b385 DH |
1982 | } |
1983 | ||
d3649f68 CH |
1984 | /* |
1985 | * __mm_populate - populate and/or mlock pages within a range of address space. | |
1986 | * | |
1987 | * This is used to implement mlock() and the MAP_POPULATE / MAP_LOCKED mmap | |
1988 | * flags. VMAs must be already marked with the desired vm_flags, and | |
c1e8d7c6 | 1989 | * mmap_lock must not be held. |
d3649f68 CH |
1990 | */ |
1991 | int __mm_populate(unsigned long start, unsigned long len, int ignore_errors) | |
1992 | { | |
1993 | struct mm_struct *mm = current->mm; | |
1994 | unsigned long end, nstart, nend; | |
1995 | struct vm_area_struct *vma = NULL; | |
1996 | int locked = 0; | |
1997 | long ret = 0; | |
1998 | ||
1999 | end = start + len; | |
2000 | ||
2001 | for (nstart = start; nstart < end; nstart = nend) { | |
2002 | /* | |
2003 | * We want to fault in pages for [nstart; end) address range. | |
2004 | * Find first corresponding VMA. | |
2005 | */ | |
2006 | if (!locked) { | |
2007 | locked = 1; | |
d8ed45c5 | 2008 | mmap_read_lock(mm); |
c4d1a92d | 2009 | vma = find_vma_intersection(mm, nstart, end); |
d3649f68 | 2010 | } else if (nstart >= vma->vm_end) |
c4d1a92d LH |
2011 | vma = find_vma_intersection(mm, vma->vm_end, end); |
2012 | ||
2013 | if (!vma) | |
d3649f68 CH |
2014 | break; |
2015 | /* | |
2016 | * Set [nstart; nend) to intersection of desired address | |
2017 | * range with the first VMA. Also, skip undesirable VMA types. | |
2018 | */ | |
2019 | nend = min(end, vma->vm_end); | |
2020 | if (vma->vm_flags & (VM_IO | VM_PFNMAP)) | |
2021 | continue; | |
2022 | if (nstart < vma->vm_start) | |
2023 | nstart = vma->vm_start; | |
2024 | /* | |
2025 | * Now fault in a range of pages. populate_vma_page_range() | |
2026 | * double checks the vma flags, so that it won't mlock pages | |
2027 | * if the vma was already munlocked. | |
2028 | */ | |
2029 | ret = populate_vma_page_range(vma, nstart, nend, &locked); | |
2030 | if (ret < 0) { | |
2031 | if (ignore_errors) { | |
2032 | ret = 0; | |
2033 | continue; /* continue at next VMA */ | |
2034 | } | |
2035 | break; | |
2036 | } | |
2037 | nend = nstart + ret * PAGE_SIZE; | |
2038 | ret = 0; | |
2039 | } | |
2040 | if (locked) | |
d8ed45c5 | 2041 | mmap_read_unlock(mm); |
d3649f68 CH |
2042 | return ret; /* 0 or negative error code */ |
2043 | } | |
050a9adc | 2044 | #else /* CONFIG_MMU */ |
64019a2e | 2045 | static long __get_user_pages_locked(struct mm_struct *mm, unsigned long start, |
050a9adc | 2046 | unsigned long nr_pages, struct page **pages, |
b2cac248 | 2047 | int *locked, unsigned int foll_flags) |
050a9adc CH |
2048 | { |
2049 | struct vm_area_struct *vma; | |
b2a72dff | 2050 | bool must_unlock = false; |
050a9adc | 2051 | unsigned long vm_flags; |
24dc20c7 | 2052 | long i; |
050a9adc | 2053 | |
b2a72dff JG |
2054 | if (!nr_pages) |
2055 | return 0; | |
2056 | ||
2057 | /* | |
2058 | * The internal caller expects GUP to manage the lock internally and the | |
2059 | * lock must be released when this returns. | |
2060 | */ | |
9a863a6a | 2061 | if (!*locked) { |
b2a72dff JG |
2062 | if (mmap_read_lock_killable(mm)) |
2063 | return -EAGAIN; | |
2064 | must_unlock = true; | |
2065 | *locked = 1; | |
2066 | } | |
2067 | ||
050a9adc CH |
2068 | /* calculate required read or write permissions. |
2069 | * If FOLL_FORCE is set, we only require the "MAY" flags. | |
2070 | */ | |
2071 | vm_flags = (foll_flags & FOLL_WRITE) ? | |
2072 | (VM_WRITE | VM_MAYWRITE) : (VM_READ | VM_MAYREAD); | |
2073 | vm_flags &= (foll_flags & FOLL_FORCE) ? | |
2074 | (VM_MAYREAD | VM_MAYWRITE) : (VM_READ | VM_WRITE); | |
2075 | ||
2076 | for (i = 0; i < nr_pages; i++) { | |
2077 | vma = find_vma(mm, start); | |
2078 | if (!vma) | |
b2a72dff | 2079 | break; |
050a9adc CH |
2080 | |
2081 | /* protect what we can, including chardevs */ | |
2082 | if ((vma->vm_flags & (VM_IO | VM_PFNMAP)) || | |
2083 | !(vm_flags & vma->vm_flags)) | |
b2a72dff | 2084 | break; |
050a9adc CH |
2085 | |
2086 | if (pages) { | |
396a400b | 2087 | pages[i] = virt_to_page((void *)start); |
050a9adc CH |
2088 | if (pages[i]) |
2089 | get_page(pages[i]); | |
2090 | } | |
b2cac248 | 2091 | |
050a9adc CH |
2092 | start = (start + PAGE_SIZE) & PAGE_MASK; |
2093 | } | |
2094 | ||
b2a72dff JG |
2095 | if (must_unlock && *locked) { |
2096 | mmap_read_unlock(mm); | |
2097 | *locked = 0; | |
2098 | } | |
050a9adc | 2099 | |
050a9adc CH |
2100 | return i ? : -EFAULT; |
2101 | } | |
2102 | #endif /* !CONFIG_MMU */ | |
d3649f68 | 2103 | |
bb523b40 AG |
2104 | /** |
2105 | * fault_in_writeable - fault in userspace address range for writing | |
2106 | * @uaddr: start of address range | |
2107 | * @size: size of address range | |
2108 | * | |
2109 | * Returns the number of bytes not faulted in (like copy_to_user() and | |
2110 | * copy_from_user()). | |
2111 | */ | |
2112 | size_t fault_in_writeable(char __user *uaddr, size_t size) | |
2113 | { | |
a7797e74 BH |
2114 | const unsigned long start = (unsigned long)uaddr; |
2115 | const unsigned long end = start + size; | |
2116 | unsigned long cur; | |
bb523b40 AG |
2117 | |
2118 | if (unlikely(size == 0)) | |
2119 | return 0; | |
677b2a8c CL |
2120 | if (!user_write_access_begin(uaddr, size)) |
2121 | return size; | |
bb523b40 | 2122 | |
a7797e74 BH |
2123 | /* Stop once we overflow to 0. */ |
2124 | for (cur = start; cur && cur < end; cur = PAGE_ALIGN_DOWN(cur + PAGE_SIZE)) | |
2125 | unsafe_put_user(0, (char __user *)cur, out); | |
bb523b40 | 2126 | out: |
677b2a8c | 2127 | user_write_access_end(); |
a7797e74 BH |
2128 | if (size > cur - start) |
2129 | return size - (cur - start); | |
bb523b40 AG |
2130 | return 0; |
2131 | } | |
2132 | EXPORT_SYMBOL(fault_in_writeable); | |
2133 | ||
da32b581 CM |
2134 | /** |
2135 | * fault_in_subpage_writeable - fault in an address range for writing | |
2136 | * @uaddr: start of address range | |
2137 | * @size: size of address range | |
2138 | * | |
2139 | * Fault in a user address range for writing while checking for permissions at | |
2140 | * sub-page granularity (e.g. arm64 MTE). This function should be used when | |
2141 | * the caller cannot guarantee forward progress of a copy_to_user() loop. | |
2142 | * | |
2143 | * Returns the number of bytes not faulted in (like copy_to_user() and | |
2144 | * copy_from_user()). | |
2145 | */ | |
2146 | size_t fault_in_subpage_writeable(char __user *uaddr, size_t size) | |
2147 | { | |
2148 | size_t faulted_in; | |
2149 | ||
2150 | /* | |
2151 | * Attempt faulting in at page granularity first for page table | |
2152 | * permission checking. The arch-specific probe_subpage_writeable() | |
2153 | * functions may not check for this. | |
2154 | */ | |
2155 | faulted_in = size - fault_in_writeable(uaddr, size); | |
2156 | if (faulted_in) | |
2157 | faulted_in -= probe_subpage_writeable(uaddr, faulted_in); | |
2158 | ||
2159 | return size - faulted_in; | |
2160 | } | |
2161 | EXPORT_SYMBOL(fault_in_subpage_writeable); | |
2162 | ||
cdd591fc AG |
2163 | /* |
2164 | * fault_in_safe_writeable - fault in an address range for writing | |
2165 | * @uaddr: start of address range | |
2166 | * @size: length of address range | |
2167 | * | |
fe673d3f LT |
2168 | * Faults in an address range for writing. This is primarily useful when we |
2169 | * already know that some or all of the pages in the address range aren't in | |
2170 | * memory. | |
cdd591fc | 2171 | * |
fe673d3f | 2172 | * Unlike fault_in_writeable(), this function is non-destructive. |
cdd591fc AG |
2173 | * |
2174 | * Note that we don't pin or otherwise hold the pages referenced that we fault | |
2175 | * in. There's no guarantee that they'll stay in memory for any duration of | |
2176 | * time. | |
2177 | * | |
2178 | * Returns the number of bytes not faulted in, like copy_to_user() and | |
2179 | * copy_from_user(). | |
2180 | */ | |
2181 | size_t fault_in_safe_writeable(const char __user *uaddr, size_t size) | |
2182 | { | |
a7797e74 BH |
2183 | const unsigned long start = (unsigned long)uaddr; |
2184 | const unsigned long end = start + size; | |
2185 | unsigned long cur; | |
cdd591fc | 2186 | struct mm_struct *mm = current->mm; |
fe673d3f | 2187 | bool unlocked = false; |
cdd591fc | 2188 | |
fe673d3f LT |
2189 | if (unlikely(size == 0)) |
2190 | return 0; | |
cdd591fc | 2191 | |
fe673d3f | 2192 | mmap_read_lock(mm); |
a7797e74 BH |
2193 | /* Stop once we overflow to 0. */ |
2194 | for (cur = start; cur && cur < end; cur = PAGE_ALIGN_DOWN(cur + PAGE_SIZE)) | |
2195 | if (fixup_user_fault(mm, cur, FAULT_FLAG_WRITE, &unlocked)) | |
cdd591fc | 2196 | break; |
fe673d3f LT |
2197 | mmap_read_unlock(mm); |
2198 | ||
a7797e74 BH |
2199 | if (size > cur - start) |
2200 | return size - (cur - start); | |
fe673d3f | 2201 | return 0; |
cdd591fc AG |
2202 | } |
2203 | EXPORT_SYMBOL(fault_in_safe_writeable); | |
2204 | ||
bb523b40 AG |
2205 | /** |
2206 | * fault_in_readable - fault in userspace address range for reading | |
2207 | * @uaddr: start of user address range | |
2208 | * @size: size of user address range | |
2209 | * | |
2210 | * Returns the number of bytes not faulted in (like copy_to_user() and | |
2211 | * copy_from_user()). | |
2212 | */ | |
2213 | size_t fault_in_readable(const char __user *uaddr, size_t size) | |
2214 | { | |
a7797e74 BH |
2215 | const unsigned long start = (unsigned long)uaddr; |
2216 | const unsigned long end = start + size; | |
2217 | unsigned long cur; | |
bb523b40 AG |
2218 | volatile char c; |
2219 | ||
2220 | if (unlikely(size == 0)) | |
2221 | return 0; | |
677b2a8c CL |
2222 | if (!user_read_access_begin(uaddr, size)) |
2223 | return size; | |
bb523b40 | 2224 | |
a7797e74 BH |
2225 | /* Stop once we overflow to 0. */ |
2226 | for (cur = start; cur && cur < end; cur = PAGE_ALIGN_DOWN(cur + PAGE_SIZE)) | |
2227 | unsafe_get_user(c, (const char __user *)cur, out); | |
bb523b40 | 2228 | out: |
677b2a8c | 2229 | user_read_access_end(); |
bb523b40 | 2230 | (void)c; |
a7797e74 BH |
2231 | if (size > cur - start) |
2232 | return size - (cur - start); | |
bb523b40 AG |
2233 | return 0; |
2234 | } | |
2235 | EXPORT_SYMBOL(fault_in_readable); | |
2236 | ||
8f942eea JH |
2237 | /** |
2238 | * get_dump_page() - pin user page in memory while writing it to core dump | |
2239 | * @addr: user address | |
d6ff4c8f | 2240 | * @locked: a pointer to an int denoting whether the mmap sem is held |
8f942eea JH |
2241 | * |
2242 | * Returns struct page pointer of user page pinned for dump, | |
2243 | * to be freed afterwards by put_page(). | |
2244 | * | |
2245 | * Returns NULL on any kind of failure - a hole must then be inserted into | |
2246 | * the corefile, to preserve alignment with its headers; and also returns | |
2247 | * NULL wherever the ZERO_PAGE, or an anonymous pte_none, has been found - | |
f0953a1b | 2248 | * allowing a hole to be left in the corefile to save disk space. |
8f942eea | 2249 | * |
7f3bfab5 | 2250 | * Called without mmap_lock (takes and releases the mmap_lock by itself). |
8f942eea JH |
2251 | */ |
2252 | #ifdef CONFIG_ELF_CORE | |
d6ff4c8f | 2253 | struct page *get_dump_page(unsigned long addr, int *locked) |
8f942eea | 2254 | { |
8f942eea | 2255 | struct page *page; |
7f3bfab5 | 2256 | int ret; |
8f942eea | 2257 | |
d6ff4c8f | 2258 | ret = __get_user_pages_locked(current->mm, addr, 1, &page, locked, |
7f3bfab5 | 2259 | FOLL_FORCE | FOLL_DUMP | FOLL_GET); |
7f3bfab5 | 2260 | return (ret == 1) ? page : NULL; |
8f942eea JH |
2261 | } |
2262 | #endif /* CONFIG_ELF_CORE */ | |
2263 | ||
d1e153fe | 2264 | #ifdef CONFIG_MIGRATION |
94efde1d JH |
2265 | |
2266 | /* | |
2267 | * An array of either pages or folios ("pofs"). Although it may seem tempting to | |
2268 | * avoid this complication, by simply interpreting a list of folios as a list of | |
2269 | * pages, that approach won't work in the longer term, because eventually the | |
2270 | * layouts of struct page and struct folio will become completely different. | |
2271 | * Furthermore, this pof approach avoids excessive page_folio() calls. | |
2272 | */ | |
2273 | struct pages_or_folios { | |
2274 | union { | |
2275 | struct page **pages; | |
2276 | struct folio **folios; | |
2277 | void **entries; | |
2278 | }; | |
2279 | bool has_folios; | |
2280 | long nr_entries; | |
2281 | }; | |
2282 | ||
2283 | static struct folio *pofs_get_folio(struct pages_or_folios *pofs, long i) | |
2284 | { | |
2285 | if (pofs->has_folios) | |
2286 | return pofs->folios[i]; | |
2287 | return page_folio(pofs->pages[i]); | |
2288 | } | |
2289 | ||
2290 | static void pofs_clear_entry(struct pages_or_folios *pofs, long i) | |
2291 | { | |
2292 | pofs->entries[i] = NULL; | |
2293 | } | |
2294 | ||
2295 | static void pofs_unpin(struct pages_or_folios *pofs) | |
2296 | { | |
2297 | if (pofs->has_folios) | |
2298 | unpin_folios(pofs->folios, pofs->nr_entries); | |
2299 | else | |
2300 | unpin_user_pages(pofs->pages, pofs->nr_entries); | |
2301 | } | |
2302 | ||
f68749ec | 2303 | /* |
53ba78de | 2304 | * Returns the number of collected folios. Return value is always >= 0. |
f68749ec | 2305 | */ |
517f496e | 2306 | static unsigned long collect_longterm_unpinnable_folios( |
94efde1d JH |
2307 | struct list_head *movable_folio_list, |
2308 | struct pages_or_folios *pofs) | |
9a4e9f3b | 2309 | { |
517f496e | 2310 | unsigned long i, collected = 0; |
1b7f7e58 | 2311 | struct folio *prev_folio = NULL; |
67e139b0 | 2312 | bool drain_allow = true; |
9a4e9f3b | 2313 | |
94efde1d JH |
2314 | for (i = 0; i < pofs->nr_entries; i++) { |
2315 | struct folio *folio = pofs_get_folio(pofs, i); | |
f9f38f78 | 2316 | |
1b7f7e58 | 2317 | if (folio == prev_folio) |
83c02c23 | 2318 | continue; |
1b7f7e58 | 2319 | prev_folio = folio; |
f9f38f78 | 2320 | |
67e139b0 AP |
2321 | if (folio_is_longterm_pinnable(folio)) |
2322 | continue; | |
b05a79d4 | 2323 | |
517f496e DH |
2324 | collected++; |
2325 | ||
67e139b0 | 2326 | if (folio_is_device_coherent(folio)) |
f9f38f78 CH |
2327 | continue; |
2328 | ||
1b7f7e58 | 2329 | if (folio_test_hugetlb(folio)) { |
4c640f12 | 2330 | folio_isolate_hugetlb(folio, movable_folio_list); |
f9f38f78 CH |
2331 | continue; |
2332 | } | |
9a4e9f3b | 2333 | |
1b7f7e58 | 2334 | if (!folio_test_lru(folio) && drain_allow) { |
f9f38f78 CH |
2335 | lru_add_drain_all(); |
2336 | drain_allow = false; | |
2337 | } | |
2338 | ||
be2d5756 | 2339 | if (!folio_isolate_lru(folio)) |
f9f38f78 | 2340 | continue; |
67e139b0 | 2341 | |
53ba78de | 2342 | list_add_tail(&folio->lru, movable_folio_list); |
1b7f7e58 MWO |
2343 | node_stat_mod_folio(folio, |
2344 | NR_ISOLATED_ANON + folio_is_file_lru(folio), | |
2345 | folio_nr_pages(folio)); | |
9a4e9f3b | 2346 | } |
517f496e DH |
2347 | |
2348 | return collected; | |
67e139b0 AP |
2349 | } |
2350 | ||
2351 | /* | |
53ba78de VK |
2352 | * Unpins all folios and migrates device coherent folios and movable_folio_list. |
2353 | * Returns -EAGAIN if all folios were successfully migrated or -errno for | |
2354 | * failure (or partial success). | |
67e139b0 | 2355 | */ |
94efde1d JH |
2356 | static int |
2357 | migrate_longterm_unpinnable_folios(struct list_head *movable_folio_list, | |
2358 | struct pages_or_folios *pofs) | |
67e139b0 AP |
2359 | { |
2360 | int ret; | |
2361 | unsigned long i; | |
6e7f34eb | 2362 | |
94efde1d JH |
2363 | for (i = 0; i < pofs->nr_entries; i++) { |
2364 | struct folio *folio = pofs_get_folio(pofs, i); | |
67e139b0 AP |
2365 | |
2366 | if (folio_is_device_coherent(folio)) { | |
2367 | /* | |
53ba78de VK |
2368 | * Migration will fail if the folio is pinned, so |
2369 | * convert the pin on the source folio to a normal | |
2370 | * reference. | |
67e139b0 | 2371 | */ |
94efde1d | 2372 | pofs_clear_entry(pofs, i); |
67e139b0 AP |
2373 | folio_get(folio); |
2374 | gup_put_folio(folio, 1, FOLL_PIN); | |
2375 | ||
5c8525a3 | 2376 | if (migrate_device_coherent_folio(folio)) { |
67e139b0 AP |
2377 | ret = -EBUSY; |
2378 | goto err; | |
2379 | } | |
2380 | ||
b05a79d4 | 2381 | continue; |
67e139b0 | 2382 | } |
b05a79d4 | 2383 | |
67e139b0 | 2384 | /* |
53ba78de | 2385 | * We can't migrate folios with unexpected references, so drop |
67e139b0 | 2386 | * the reference obtained by __get_user_pages_locked(). |
53ba78de | 2387 | * Migrating folios have been added to movable_folio_list after |
67e139b0 | 2388 | * calling folio_isolate_lru() which takes a reference so the |
53ba78de | 2389 | * folio won't be freed if it's migrating. |
67e139b0 | 2390 | */ |
94efde1d JH |
2391 | unpin_folio(folio); |
2392 | pofs_clear_entry(pofs, i); | |
f68749ec | 2393 | } |
f9f38f78 | 2394 | |
53ba78de | 2395 | if (!list_empty(movable_folio_list)) { |
f9f38f78 CH |
2396 | struct migration_target_control mtc = { |
2397 | .nid = NUMA_NO_NODE, | |
2398 | .gfp_mask = GFP_USER | __GFP_NOWARN, | |
e42dfe4e | 2399 | .reason = MR_LONGTERM_PIN, |
f9f38f78 CH |
2400 | }; |
2401 | ||
53ba78de | 2402 | if (migrate_pages(movable_folio_list, alloc_migration_target, |
67e139b0 AP |
2403 | NULL, (unsigned long)&mtc, MIGRATE_SYNC, |
2404 | MR_LONGTERM_PIN, NULL)) { | |
f9f38f78 | 2405 | ret = -ENOMEM; |
67e139b0 AP |
2406 | goto err; |
2407 | } | |
9a4e9f3b AK |
2408 | } |
2409 | ||
53ba78de | 2410 | putback_movable_pages(movable_folio_list); |
67e139b0 AP |
2411 | |
2412 | return -EAGAIN; | |
2413 | ||
2414 | err: | |
94efde1d | 2415 | pofs_unpin(pofs); |
53ba78de | 2416 | putback_movable_pages(movable_folio_list); |
24a95998 | 2417 | |
67e139b0 AP |
2418 | return ret; |
2419 | } | |
2420 | ||
94efde1d JH |
2421 | static long |
2422 | check_and_migrate_movable_pages_or_folios(struct pages_or_folios *pofs) | |
2423 | { | |
2424 | LIST_HEAD(movable_folio_list); | |
517f496e | 2425 | unsigned long collected; |
94efde1d | 2426 | |
517f496e DH |
2427 | collected = collect_longterm_unpinnable_folios(&movable_folio_list, |
2428 | pofs); | |
2429 | if (!collected) | |
94efde1d JH |
2430 | return 0; |
2431 | ||
2432 | return migrate_longterm_unpinnable_folios(&movable_folio_list, pofs); | |
2433 | } | |
2434 | ||
67e139b0 | 2435 | /* |
aa6f8b25 | 2436 | * Check whether all folios are *allowed* to be pinned indefinitely (long term). |
53ba78de VK |
2437 | * Rather confusingly, all folios in the range are required to be pinned via |
2438 | * FOLL_PIN, before calling this routine. | |
67e139b0 | 2439 | * |
aa6f8b25 | 2440 | * Return values: |
67e139b0 | 2441 | * |
aa6f8b25 | 2442 | * 0: if everything is OK and all folios in the range are allowed to be pinned, |
53ba78de | 2443 | * then this routine leaves all folios pinned and returns zero for success. |
aa6f8b25 JH |
2444 | * |
2445 | * -EAGAIN: if any folios in the range are not allowed to be pinned, then this | |
2446 | * routine will migrate those folios away, unpin all the folios in the range. If | |
2447 | * migration of the entire set of folios succeeds, then -EAGAIN is returned. The | |
2448 | * caller should re-pin the entire range with FOLL_PIN and then call this | |
2449 | * routine again. | |
2450 | * | |
2451 | * -ENOMEM, or any other -errno: if an error *other* than -EAGAIN occurs, this | |
2452 | * indicates a migration failure. The caller should give up, and propagate the | |
2453 | * error back up the call stack. The caller does not need to unpin any folios in | |
2454 | * that case, because this routine will do the unpinning. | |
67e139b0 | 2455 | */ |
53ba78de VK |
2456 | static long check_and_migrate_movable_folios(unsigned long nr_folios, |
2457 | struct folio **folios) | |
67e139b0 | 2458 | { |
94efde1d JH |
2459 | struct pages_or_folios pofs = { |
2460 | .folios = folios, | |
2461 | .has_folios = true, | |
2462 | .nr_entries = nr_folios, | |
2463 | }; | |
67e139b0 | 2464 | |
94efde1d | 2465 | return check_and_migrate_movable_pages_or_folios(&pofs); |
53ba78de VK |
2466 | } |
2467 | ||
2468 | /* | |
aa6f8b25 JH |
2469 | * Return values and behavior are the same as those for |
2470 | * check_and_migrate_movable_folios(). | |
53ba78de VK |
2471 | */ |
2472 | static long check_and_migrate_movable_pages(unsigned long nr_pages, | |
2473 | struct page **pages) | |
2474 | { | |
94efde1d JH |
2475 | struct pages_or_folios pofs = { |
2476 | .pages = pages, | |
2477 | .has_folios = false, | |
2478 | .nr_entries = nr_pages, | |
2479 | }; | |
53ba78de | 2480 | |
94efde1d | 2481 | return check_and_migrate_movable_pages_or_folios(&pofs); |
9a4e9f3b AK |
2482 | } |
2483 | #else | |
f68749ec | 2484 | static long check_and_migrate_movable_pages(unsigned long nr_pages, |
f6d299ec | 2485 | struct page **pages) |
9a4e9f3b | 2486 | { |
24a95998 | 2487 | return 0; |
9a4e9f3b | 2488 | } |
53ba78de VK |
2489 | |
2490 | static long check_and_migrate_movable_folios(unsigned long nr_folios, | |
2491 | struct folio **folios) | |
2492 | { | |
2493 | return 0; | |
2494 | } | |
d1e153fe | 2495 | #endif /* CONFIG_MIGRATION */ |
9a4e9f3b | 2496 | |
2bb6d283 | 2497 | /* |
932f4a63 IW |
2498 | * __gup_longterm_locked() is a wrapper for __get_user_pages_locked which |
2499 | * allows us to process the FOLL_LONGTERM flag. | |
2bb6d283 | 2500 | */ |
64019a2e | 2501 | static long __gup_longterm_locked(struct mm_struct *mm, |
932f4a63 IW |
2502 | unsigned long start, |
2503 | unsigned long nr_pages, | |
2504 | struct page **pages, | |
53b2d09b | 2505 | int *locked, |
932f4a63 | 2506 | unsigned int gup_flags) |
2bb6d283 | 2507 | { |
f68749ec | 2508 | unsigned int flags; |
24a95998 | 2509 | long rc, nr_pinned_pages; |
2bb6d283 | 2510 | |
f68749ec | 2511 | if (!(gup_flags & FOLL_LONGTERM)) |
b2cac248 | 2512 | return __get_user_pages_locked(mm, start, nr_pages, pages, |
53b2d09b | 2513 | locked, gup_flags); |
67e139b0 | 2514 | |
f68749ec PT |
2515 | flags = memalloc_pin_save(); |
2516 | do { | |
24a95998 | 2517 | nr_pinned_pages = __get_user_pages_locked(mm, start, nr_pages, |
b2cac248 | 2518 | pages, locked, |
24a95998 AP |
2519 | gup_flags); |
2520 | if (nr_pinned_pages <= 0) { | |
2521 | rc = nr_pinned_pages; | |
f68749ec | 2522 | break; |
24a95998 | 2523 | } |
d64e2dbc JG |
2524 | |
2525 | /* FOLL_LONGTERM implies FOLL_PIN */ | |
f6d299ec | 2526 | rc = check_and_migrate_movable_pages(nr_pinned_pages, pages); |
24a95998 | 2527 | } while (rc == -EAGAIN); |
f68749ec | 2528 | memalloc_pin_restore(flags); |
24a95998 | 2529 | return rc ? rc : nr_pinned_pages; |
2bb6d283 | 2530 | } |
932f4a63 | 2531 | |
d64e2dbc JG |
2532 | /* |
2533 | * Check that the given flags are valid for the exported gup/pup interface, and | |
2534 | * update them with the required flags that the caller must have set. | |
2535 | */ | |
b2cac248 LS |
2536 | static bool is_valid_gup_args(struct page **pages, int *locked, |
2537 | unsigned int *gup_flags_p, unsigned int to_set) | |
447f3e45 | 2538 | { |
d64e2dbc JG |
2539 | unsigned int gup_flags = *gup_flags_p; |
2540 | ||
447f3e45 | 2541 | /* |
d64e2dbc JG |
2542 | * These flags not allowed to be specified externally to the gup |
2543 | * interfaces: | |
0f20bba1 | 2544 | * - FOLL_TOUCH/FOLL_PIN/FOLL_TRIED/FOLL_FAST_ONLY are internal only |
7290840d | 2545 | * - FOLL_REMOTE is internal only, set in (get|pin)_user_pages_remote() |
f04740f5 | 2546 | * - FOLL_UNLOCKABLE is internal only and used if locked is !NULL |
447f3e45 | 2547 | */ |
0f20bba1 | 2548 | if (WARN_ON_ONCE(gup_flags & INTERNAL_GUP_FLAGS)) |
d64e2dbc JG |
2549 | return false; |
2550 | ||
2551 | gup_flags |= to_set; | |
f04740f5 JG |
2552 | if (locked) { |
2553 | /* At the external interface locked must be set */ | |
2554 | if (WARN_ON_ONCE(*locked != 1)) | |
2555 | return false; | |
2556 | ||
2557 | gup_flags |= FOLL_UNLOCKABLE; | |
2558 | } | |
d64e2dbc JG |
2559 | |
2560 | /* FOLL_GET and FOLL_PIN are mutually exclusive. */ | |
2561 | if (WARN_ON_ONCE((gup_flags & (FOLL_PIN | FOLL_GET)) == | |
2562 | (FOLL_PIN | FOLL_GET))) | |
2563 | return false; | |
2564 | ||
2565 | /* LONGTERM can only be specified when pinning */ | |
2566 | if (WARN_ON_ONCE(!(gup_flags & FOLL_PIN) && (gup_flags & FOLL_LONGTERM))) | |
2567 | return false; | |
2568 | ||
2569 | /* Pages input must be given if using GET/PIN */ | |
2570 | if (WARN_ON_ONCE((gup_flags & (FOLL_GET | FOLL_PIN)) && !pages)) | |
447f3e45 | 2571 | return false; |
d64e2dbc | 2572 | |
d64e2dbc JG |
2573 | /* We want to allow the pgmap to be hot-unplugged at all times */ |
2574 | if (WARN_ON_ONCE((gup_flags & FOLL_LONGTERM) && | |
2575 | (gup_flags & FOLL_PCI_P2PDMA))) | |
2576 | return false; | |
2577 | ||
d64e2dbc | 2578 | *gup_flags_p = gup_flags; |
447f3e45 BS |
2579 | return true; |
2580 | } | |
2581 | ||
22bf29b6 | 2582 | #ifdef CONFIG_MMU |
adc8cb40 | 2583 | /** |
c4237f8b | 2584 | * get_user_pages_remote() - pin user pages in memory |
c4237f8b JH |
2585 | * @mm: mm_struct of target mm |
2586 | * @start: starting user address | |
2587 | * @nr_pages: number of pages from start to pin | |
2588 | * @gup_flags: flags modifying lookup behaviour | |
2589 | * @pages: array that receives pointers to the pages pinned. | |
2590 | * Should be at least nr_pages long. Or NULL, if caller | |
2591 | * only intends to ensure the pages are faulted in. | |
c4237f8b JH |
2592 | * @locked: pointer to lock flag indicating whether lock is held and |
2593 | * subsequently whether VM_FAULT_RETRY functionality can be | |
2594 | * utilised. Lock must initially be held. | |
2595 | * | |
2596 | * Returns either number of pages pinned (which may be less than the | |
2597 | * number requested), or an error. Details about the return value: | |
2598 | * | |
2599 | * -- If nr_pages is 0, returns 0. | |
2600 | * -- If nr_pages is >0, but no pages were pinned, returns -errno. | |
2601 | * -- If nr_pages is >0, and some pages were pinned, returns the number of | |
2602 | * pages pinned. Again, this may be less than nr_pages. | |
2603 | * | |
2604 | * The caller is responsible for releasing returned @pages, via put_page(). | |
2605 | * | |
c1e8d7c6 | 2606 | * Must be called with mmap_lock held for read or write. |
c4237f8b | 2607 | * |
adc8cb40 SJ |
2608 | * get_user_pages_remote walks a process's page tables and takes a reference |
2609 | * to each struct page that each user address corresponds to at a given | |
c4237f8b JH |
2610 | * instant. That is, it takes the page that would be accessed if a user |
2611 | * thread accesses the given user virtual address at that instant. | |
2612 | * | |
2613 | * This does not guarantee that the page exists in the user mappings when | |
adc8cb40 | 2614 | * get_user_pages_remote returns, and there may even be a completely different |
c4237f8b | 2615 | * page there in some cases (eg. if mmapped pagecache has been invalidated |
5da1a868 | 2616 | * and subsequently re-faulted). However it does guarantee that the page |
c4237f8b JH |
2617 | * won't be freed completely. And mostly callers simply care that the page |
2618 | * contains data that was valid *at some point in time*. Typically, an IO | |
2619 | * or similar operation cannot guarantee anything stronger anyway because | |
2620 | * locks can't be held over the syscall boundary. | |
2621 | * | |
2622 | * If gup_flags & FOLL_WRITE == 0, the page must not be written to. If the page | |
2623 | * is written to, set_page_dirty (or set_page_dirty_lock, as appropriate) must | |
2624 | * be called after the page is finished with, and before put_page is called. | |
2625 | * | |
adc8cb40 SJ |
2626 | * get_user_pages_remote is typically used for fewer-copy IO operations, |
2627 | * to get a handle on the memory by some means other than accesses | |
2628 | * via the user virtual addresses. The pages may be submitted for | |
2629 | * DMA to devices or accessed via their kernel linear mapping (via the | |
2630 | * kmap APIs). Care should be taken to use the correct cache flushing APIs. | |
c4237f8b JH |
2631 | * |
2632 | * See also get_user_pages_fast, for performance critical applications. | |
2633 | * | |
adc8cb40 | 2634 | * get_user_pages_remote should be phased out in favor of |
c4237f8b | 2635 | * get_user_pages_locked|unlocked or get_user_pages_fast. Nothing |
adc8cb40 | 2636 | * should use get_user_pages_remote because it cannot pass |
c4237f8b JH |
2637 | * FAULT_FLAG_ALLOW_RETRY to handle_mm_fault. |
2638 | */ | |
64019a2e | 2639 | long get_user_pages_remote(struct mm_struct *mm, |
c4237f8b JH |
2640 | unsigned long start, unsigned long nr_pages, |
2641 | unsigned int gup_flags, struct page **pages, | |
ca5e8632 | 2642 | int *locked) |
c4237f8b | 2643 | { |
9a863a6a JG |
2644 | int local_locked = 1; |
2645 | ||
b2cac248 | 2646 | if (!is_valid_gup_args(pages, locked, &gup_flags, |
d64e2dbc | 2647 | FOLL_TOUCH | FOLL_REMOTE)) |
eddb1c22 JH |
2648 | return -EINVAL; |
2649 | ||
b2cac248 | 2650 | return __get_user_pages_locked(mm, start, nr_pages, pages, |
9a863a6a | 2651 | locked ? locked : &local_locked, |
d64e2dbc | 2652 | gup_flags); |
c4237f8b JH |
2653 | } |
2654 | EXPORT_SYMBOL(get_user_pages_remote); | |
2655 | ||
eddb1c22 | 2656 | #else /* CONFIG_MMU */ |
64019a2e | 2657 | long get_user_pages_remote(struct mm_struct *mm, |
eddb1c22 JH |
2658 | unsigned long start, unsigned long nr_pages, |
2659 | unsigned int gup_flags, struct page **pages, | |
ca5e8632 | 2660 | int *locked) |
eddb1c22 JH |
2661 | { |
2662 | return 0; | |
2663 | } | |
2664 | #endif /* !CONFIG_MMU */ | |
2665 | ||
adc8cb40 SJ |
2666 | /** |
2667 | * get_user_pages() - pin user pages in memory | |
2668 | * @start: starting user address | |
2669 | * @nr_pages: number of pages from start to pin | |
2670 | * @gup_flags: flags modifying lookup behaviour | |
2671 | * @pages: array that receives pointers to the pages pinned. | |
2672 | * Should be at least nr_pages long. Or NULL, if caller | |
2673 | * only intends to ensure the pages are faulted in. | |
adc8cb40 | 2674 | * |
64019a2e PX |
2675 | * This is the same as get_user_pages_remote(), just with a less-flexible |
2676 | * calling convention where we assume that the mm being operated on belongs to | |
2677 | * the current task, and doesn't allow passing of a locked parameter. We also | |
2678 | * obviously don't pass FOLL_REMOTE in here. | |
932f4a63 IW |
2679 | */ |
2680 | long get_user_pages(unsigned long start, unsigned long nr_pages, | |
54d02069 | 2681 | unsigned int gup_flags, struct page **pages) |
932f4a63 | 2682 | { |
9a863a6a JG |
2683 | int locked = 1; |
2684 | ||
b2cac248 | 2685 | if (!is_valid_gup_args(pages, NULL, &gup_flags, FOLL_TOUCH)) |
eddb1c22 JH |
2686 | return -EINVAL; |
2687 | ||
afa3c33e | 2688 | return __get_user_pages_locked(current->mm, start, nr_pages, pages, |
b2cac248 | 2689 | &locked, gup_flags); |
932f4a63 IW |
2690 | } |
2691 | EXPORT_SYMBOL(get_user_pages); | |
2bb6d283 | 2692 | |
acc3c8d1 | 2693 | /* |
d3649f68 | 2694 | * get_user_pages_unlocked() is suitable to replace the form: |
acc3c8d1 | 2695 | * |
3e4e28c5 | 2696 | * mmap_read_lock(mm); |
64019a2e | 2697 | * get_user_pages(mm, ..., pages, NULL); |
3e4e28c5 | 2698 | * mmap_read_unlock(mm); |
d3649f68 CH |
2699 | * |
2700 | * with: | |
2701 | * | |
64019a2e | 2702 | * get_user_pages_unlocked(mm, ..., pages); |
d3649f68 CH |
2703 | * |
2704 | * It is functionally equivalent to get_user_pages_fast so | |
2705 | * get_user_pages_fast should be used instead if specific gup_flags | |
2706 | * (e.g. FOLL_FORCE) are not required. | |
acc3c8d1 | 2707 | */ |
d3649f68 CH |
2708 | long get_user_pages_unlocked(unsigned long start, unsigned long nr_pages, |
2709 | struct page **pages, unsigned int gup_flags) | |
acc3c8d1 | 2710 | { |
b2a72dff | 2711 | int locked = 0; |
acc3c8d1 | 2712 | |
b2cac248 | 2713 | if (!is_valid_gup_args(pages, NULL, &gup_flags, |
f04740f5 | 2714 | FOLL_TOUCH | FOLL_UNLOCKABLE)) |
d64e2dbc JG |
2715 | return -EINVAL; |
2716 | ||
afa3c33e | 2717 | return __get_user_pages_locked(current->mm, start, nr_pages, pages, |
b2cac248 | 2718 | &locked, gup_flags); |
4bbd4c77 | 2719 | } |
d3649f68 | 2720 | EXPORT_SYMBOL(get_user_pages_unlocked); |
2667f50e SC |
2721 | |
2722 | /* | |
23babe19 | 2723 | * GUP-fast |
2667f50e SC |
2724 | * |
2725 | * get_user_pages_fast attempts to pin user pages by walking the page | |
2726 | * tables directly and avoids taking locks. Thus the walker needs to be | |
2727 | * protected from page table pages being freed from under it, and should | |
2728 | * block any THP splits. | |
2729 | * | |
2730 | * One way to achieve this is to have the walker disable interrupts, and | |
2731 | * rely on IPIs from the TLB flushing code blocking before the page table | |
2732 | * pages are freed. This is unsuitable for architectures that do not need | |
2733 | * to broadcast an IPI when invalidating TLBs. | |
2734 | * | |
2735 | * Another way to achieve this is to batch up page table containing pages | |
2736 | * belonging to more than one mm_user, then rcu_sched a callback to free those | |
23babe19 | 2737 | * pages. Disabling interrupts will allow the gup_fast() walker to both block |
2667f50e SC |
2738 | * the rcu_sched callback, and an IPI that we broadcast for splitting THPs |
2739 | * (which is a relatively rare event). The code below adopts this strategy. | |
2740 | * | |
2741 | * Before activating this code, please be aware that the following assumptions | |
2742 | * are currently made: | |
2743 | * | |
ff2e6d72 | 2744 | * *) Either MMU_GATHER_RCU_TABLE_FREE is enabled, and tlb_remove_table() is used to |
e585513b | 2745 | * free pages containing page tables or TLB flushing requires IPI broadcast. |
2667f50e | 2746 | * |
2667f50e SC |
2747 | * *) ptes can be read atomically by the architecture. |
2748 | * | |
c2661f5f | 2749 | * *) valid user addesses are below TASK_MAX_SIZE |
2667f50e SC |
2750 | * |
2751 | * The last two assumptions can be relaxed by the addition of helper functions. | |
2752 | * | |
2753 | * This code is based heavily on the PowerPC implementation by Nick Piggin. | |
2754 | */ | |
25176ad0 | 2755 | #ifdef CONFIG_HAVE_GUP_FAST |
a6e79df9 | 2756 | /* |
f002882c DH |
2757 | * Used in the GUP-fast path to determine whether GUP is permitted to work on |
2758 | * a specific folio. | |
a6e79df9 LS |
2759 | * |
2760 | * This call assumes the caller has pinned the folio, that the lowest page table | |
2761 | * level still points to this folio, and that interrupts have been disabled. | |
2762 | * | |
f002882c DH |
2763 | * GUP-fast must reject all secretmem folios. |
2764 | * | |
a6e79df9 LS |
2765 | * Writing to pinned file-backed dirty tracked folios is inherently problematic |
2766 | * (see comment describing the writable_file_mapping_allowed() function). We | |
2767 | * therefore try to avoid the most egregious case of a long-term mapping doing | |
2768 | * so. | |
2769 | * | |
2770 | * This function cannot be as thorough as that one as the VMA is not available | |
2771 | * in the fast path, so instead we whitelist known good cases and if in doubt, | |
2772 | * fall back to the slow path. | |
2773 | */ | |
f002882c | 2774 | static bool gup_fast_folio_allowed(struct folio *folio, unsigned int flags) |
a6e79df9 | 2775 | { |
f002882c | 2776 | bool reject_file_backed = false; |
a6e79df9 | 2777 | struct address_space *mapping; |
f002882c | 2778 | bool check_secretmem = false; |
a6e79df9 LS |
2779 | unsigned long mapping_flags; |
2780 | ||
2781 | /* | |
2782 | * If we aren't pinning then no problematic write can occur. A long term | |
2783 | * pin is the most egregious case so this is the one we disallow. | |
2784 | */ | |
f002882c | 2785 | if ((flags & (FOLL_PIN | FOLL_LONGTERM | FOLL_WRITE)) == |
a6e79df9 | 2786 | (FOLL_PIN | FOLL_LONGTERM | FOLL_WRITE)) |
f002882c DH |
2787 | reject_file_backed = true; |
2788 | ||
2789 | /* We hold a folio reference, so we can safely access folio fields. */ | |
a6e79df9 | 2790 | |
f002882c DH |
2791 | /* secretmem folios are always order-0 folios. */ |
2792 | if (IS_ENABLED(CONFIG_SECRETMEM) && !folio_test_large(folio)) | |
2793 | check_secretmem = true; | |
2794 | ||
2795 | if (!reject_file_backed && !check_secretmem) | |
2796 | return true; | |
a6e79df9 LS |
2797 | |
2798 | if (WARN_ON_ONCE(folio_test_slab(folio))) | |
2799 | return false; | |
2800 | ||
f002882c | 2801 | /* hugetlb neither requires dirty-tracking nor can be secretmem. */ |
a6e79df9 LS |
2802 | if (folio_test_hugetlb(folio)) |
2803 | return true; | |
2804 | ||
2805 | /* | |
2806 | * GUP-fast disables IRQs. When IRQS are disabled, RCU grace periods | |
2807 | * cannot proceed, which means no actions performed under RCU can | |
2808 | * proceed either. | |
2809 | * | |
2810 | * inodes and thus their mappings are freed under RCU, which means the | |
2811 | * mapping cannot be freed beneath us and thus we can safely dereference | |
2812 | * it. | |
2813 | */ | |
2814 | lockdep_assert_irqs_disabled(); | |
2815 | ||
2816 | /* | |
2817 | * However, there may be operations which _alter_ the mapping, so ensure | |
2818 | * we read it once and only once. | |
2819 | */ | |
2820 | mapping = READ_ONCE(folio->mapping); | |
2821 | ||
2822 | /* | |
2823 | * The mapping may have been truncated, in any case we cannot determine | |
2824 | * if this mapping is safe - fall back to slow path to determine how to | |
2825 | * proceed. | |
2826 | */ | |
2827 | if (!mapping) | |
2828 | return false; | |
2829 | ||
2830 | /* Anonymous folios pose no problem. */ | |
2831 | mapping_flags = (unsigned long)mapping & PAGE_MAPPING_FLAGS; | |
2832 | if (mapping_flags) | |
2833 | return mapping_flags & PAGE_MAPPING_ANON; | |
2834 | ||
2835 | /* | |
2836 | * At this point, we know the mapping is non-null and points to an | |
f002882c | 2837 | * address_space object. |
a6e79df9 | 2838 | */ |
f002882c DH |
2839 | if (check_secretmem && secretmem_mapping(mapping)) |
2840 | return false; | |
2841 | /* The only remaining allowed file system is shmem. */ | |
2842 | return !reject_file_backed || shmem_mapping(mapping); | |
a6e79df9 LS |
2843 | } |
2844 | ||
23babe19 DH |
2845 | static void __maybe_unused gup_fast_undo_dev_pagemap(int *nr, int nr_start, |
2846 | unsigned int flags, struct page **pages) | |
b59f65fa KS |
2847 | { |
2848 | while ((*nr) - nr_start) { | |
9cbe4954 | 2849 | struct folio *folio = page_folio(pages[--(*nr)]); |
b59f65fa | 2850 | |
9cbe4954 MWO |
2851 | folio_clear_referenced(folio); |
2852 | gup_put_folio(folio, 1, flags); | |
b59f65fa KS |
2853 | } |
2854 | } | |
2855 | ||
3010a5ea | 2856 | #ifdef CONFIG_ARCH_HAS_PTE_SPECIAL |
70cbc3cc | 2857 | /* |
23babe19 | 2858 | * GUP-fast relies on pte change detection to avoid concurrent pgtable |
70cbc3cc YS |
2859 | * operations. |
2860 | * | |
23babe19 | 2861 | * To pin the page, GUP-fast needs to do below in order: |
70cbc3cc YS |
2862 | * (1) pin the page (by prefetching pte), then (2) check pte not changed. |
2863 | * | |
2864 | * For the rest of pgtable operations where pgtable updates can be racy | |
23babe19 | 2865 | * with GUP-fast, we need to do (1) clear pte, then (2) check whether page |
70cbc3cc YS |
2866 | * is pinned. |
2867 | * | |
2868 | * Above will work for all pte-level operations, including THP split. | |
2869 | * | |
23babe19 | 2870 | * For THP collapse, it's a bit more complicated because GUP-fast may be |
70cbc3cc YS |
2871 | * walking a pgtable page that is being freed (pte is still valid but pmd |
2872 | * can be cleared already). To avoid race in such condition, we need to | |
2873 | * also check pmd here to make sure pmd doesn't change (corresponds to | |
2874 | * pmdp_collapse_flush() in the THP collapse code path). | |
2875 | */ | |
23babe19 DH |
2876 | static int gup_fast_pte_range(pmd_t pmd, pmd_t *pmdp, unsigned long addr, |
2877 | unsigned long end, unsigned int flags, struct page **pages, | |
2878 | int *nr) | |
2667f50e | 2879 | { |
b59f65fa KS |
2880 | struct dev_pagemap *pgmap = NULL; |
2881 | int nr_start = *nr, ret = 0; | |
2667f50e | 2882 | pte_t *ptep, *ptem; |
2667f50e SC |
2883 | |
2884 | ptem = ptep = pte_offset_map(&pmd, addr); | |
04dee9e8 HD |
2885 | if (!ptep) |
2886 | return 0; | |
2667f50e | 2887 | do { |
2a4a06da | 2888 | pte_t pte = ptep_get_lockless(ptep); |
b0496fe4 MWO |
2889 | struct page *page; |
2890 | struct folio *folio; | |
2667f50e | 2891 | |
d74943a2 DH |
2892 | /* |
2893 | * Always fallback to ordinary GUP on PROT_NONE-mapped pages: | |
2894 | * pte_access_permitted() better should reject these pages | |
2895 | * either way: otherwise, GUP-fast might succeed in | |
2896 | * cases where ordinary GUP would fail due to VMA access | |
2897 | * permissions. | |
2898 | */ | |
2899 | if (pte_protnone(pte)) | |
e7884f8e KS |
2900 | goto pte_unmap; |
2901 | ||
b798bec4 | 2902 | if (!pte_access_permitted(pte, flags & FOLL_WRITE)) |
e7884f8e KS |
2903 | goto pte_unmap; |
2904 | ||
b59f65fa | 2905 | if (pte_devmap(pte)) { |
7af75561 IW |
2906 | if (unlikely(flags & FOLL_LONGTERM)) |
2907 | goto pte_unmap; | |
2908 | ||
b59f65fa KS |
2909 | pgmap = get_dev_pagemap(pte_pfn(pte), pgmap); |
2910 | if (unlikely(!pgmap)) { | |
23babe19 | 2911 | gup_fast_undo_dev_pagemap(nr, nr_start, flags, pages); |
b59f65fa KS |
2912 | goto pte_unmap; |
2913 | } | |
2914 | } else if (pte_special(pte)) | |
2667f50e SC |
2915 | goto pte_unmap; |
2916 | ||
2917 | VM_BUG_ON(!pfn_valid(pte_pfn(pte))); | |
2918 | page = pte_page(pte); | |
2919 | ||
f442fa61 | 2920 | folio = try_grab_folio_fast(page, 1, flags); |
b0496fe4 | 2921 | if (!folio) |
2667f50e SC |
2922 | goto pte_unmap; |
2923 | ||
70cbc3cc | 2924 | if (unlikely(pmd_val(pmd) != pmd_val(*pmdp)) || |
c33c7948 | 2925 | unlikely(pte_val(pte) != pte_val(ptep_get(ptep)))) { |
b0496fe4 | 2926 | gup_put_folio(folio, 1, flags); |
2667f50e SC |
2927 | goto pte_unmap; |
2928 | } | |
2929 | ||
f002882c | 2930 | if (!gup_fast_folio_allowed(folio, flags)) { |
b0496fe4 | 2931 | gup_put_folio(folio, 1, flags); |
2667f50e SC |
2932 | goto pte_unmap; |
2933 | } | |
2934 | ||
84209e87 | 2935 | if (!pte_write(pte) && gup_must_unshare(NULL, flags, page)) { |
a7f22660 DH |
2936 | gup_put_folio(folio, 1, flags); |
2937 | goto pte_unmap; | |
2938 | } | |
2939 | ||
f28d4363 CI |
2940 | /* |
2941 | * We need to make the page accessible if and only if we are | |
2942 | * going to access its content (the FOLL_PIN case). Please | |
2943 | * see Documentation/core-api/pin_user_pages.rst for | |
2944 | * details. | |
2945 | */ | |
2946 | if (flags & FOLL_PIN) { | |
b967c648 | 2947 | ret = arch_make_folio_accessible(folio); |
f28d4363 | 2948 | if (ret) { |
b0496fe4 | 2949 | gup_put_folio(folio, 1, flags); |
f28d4363 CI |
2950 | goto pte_unmap; |
2951 | } | |
2952 | } | |
b0496fe4 | 2953 | folio_set_referenced(folio); |
2667f50e SC |
2954 | pages[*nr] = page; |
2955 | (*nr)++; | |
2667f50e SC |
2956 | } while (ptep++, addr += PAGE_SIZE, addr != end); |
2957 | ||
2958 | ret = 1; | |
2959 | ||
2960 | pte_unmap: | |
832d7aa0 CH |
2961 | if (pgmap) |
2962 | put_dev_pagemap(pgmap); | |
2667f50e SC |
2963 | pte_unmap(ptem); |
2964 | return ret; | |
2965 | } | |
2966 | #else | |
2967 | ||
2968 | /* | |
2969 | * If we can't determine whether or not a pte is special, then fail immediately | |
2970 | * for ptes. Note, we can still pin HugeTLB and THP as these are guaranteed not | |
2971 | * to be special. | |
2972 | * | |
2973 | * For a futex to be placed on a THP tail page, get_futex_key requires a | |
dadbb612 | 2974 | * get_user_pages_fast_only implementation that can pin pages. Thus it's still |
23babe19 | 2975 | * useful to have gup_fast_pmd_leaf even if we can't operate on ptes. |
2667f50e | 2976 | */ |
23babe19 DH |
2977 | static int gup_fast_pte_range(pmd_t pmd, pmd_t *pmdp, unsigned long addr, |
2978 | unsigned long end, unsigned int flags, struct page **pages, | |
2979 | int *nr) | |
2667f50e SC |
2980 | { |
2981 | return 0; | |
2982 | } | |
3010a5ea | 2983 | #endif /* CONFIG_ARCH_HAS_PTE_SPECIAL */ |
2667f50e | 2984 | |
17596731 | 2985 | #if defined(CONFIG_ARCH_HAS_PTE_DEVMAP) && defined(CONFIG_TRANSPARENT_HUGEPAGE) |
23babe19 DH |
2986 | static int gup_fast_devmap_leaf(unsigned long pfn, unsigned long addr, |
2987 | unsigned long end, unsigned int flags, struct page **pages, int *nr) | |
b59f65fa KS |
2988 | { |
2989 | int nr_start = *nr; | |
2990 | struct dev_pagemap *pgmap = NULL; | |
2991 | ||
2992 | do { | |
9cbe4954 | 2993 | struct folio *folio; |
b59f65fa KS |
2994 | struct page *page = pfn_to_page(pfn); |
2995 | ||
2996 | pgmap = get_dev_pagemap(pfn, pgmap); | |
2997 | if (unlikely(!pgmap)) { | |
23babe19 | 2998 | gup_fast_undo_dev_pagemap(nr, nr_start, flags, pages); |
6401c4eb | 2999 | break; |
b59f65fa | 3000 | } |
4003f107 | 3001 | |
f442fa61 | 3002 | folio = try_grab_folio_fast(page, 1, flags); |
9cbe4954 | 3003 | if (!folio) { |
23babe19 | 3004 | gup_fast_undo_dev_pagemap(nr, nr_start, flags, pages); |
6401c4eb | 3005 | break; |
3faa52c0 | 3006 | } |
9cbe4954 MWO |
3007 | folio_set_referenced(folio); |
3008 | pages[*nr] = page; | |
b59f65fa KS |
3009 | (*nr)++; |
3010 | pfn++; | |
3011 | } while (addr += PAGE_SIZE, addr != end); | |
832d7aa0 | 3012 | |
6401c4eb | 3013 | put_dev_pagemap(pgmap); |
20b7fee7 | 3014 | return addr == end; |
b59f65fa KS |
3015 | } |
3016 | ||
23babe19 DH |
3017 | static int gup_fast_devmap_pmd_leaf(pmd_t orig, pmd_t *pmdp, unsigned long addr, |
3018 | unsigned long end, unsigned int flags, struct page **pages, | |
3019 | int *nr) | |
b59f65fa KS |
3020 | { |
3021 | unsigned long fault_pfn; | |
a9b6de77 DW |
3022 | int nr_start = *nr; |
3023 | ||
3024 | fault_pfn = pmd_pfn(orig) + ((addr & ~PMD_MASK) >> PAGE_SHIFT); | |
23babe19 | 3025 | if (!gup_fast_devmap_leaf(fault_pfn, addr, end, flags, pages, nr)) |
a9b6de77 | 3026 | return 0; |
b59f65fa | 3027 | |
a9b6de77 | 3028 | if (unlikely(pmd_val(orig) != pmd_val(*pmdp))) { |
23babe19 | 3029 | gup_fast_undo_dev_pagemap(nr, nr_start, flags, pages); |
a9b6de77 DW |
3030 | return 0; |
3031 | } | |
3032 | return 1; | |
b59f65fa KS |
3033 | } |
3034 | ||
23babe19 DH |
3035 | static int gup_fast_devmap_pud_leaf(pud_t orig, pud_t *pudp, unsigned long addr, |
3036 | unsigned long end, unsigned int flags, struct page **pages, | |
3037 | int *nr) | |
b59f65fa KS |
3038 | { |
3039 | unsigned long fault_pfn; | |
a9b6de77 DW |
3040 | int nr_start = *nr; |
3041 | ||
3042 | fault_pfn = pud_pfn(orig) + ((addr & ~PUD_MASK) >> PAGE_SHIFT); | |
23babe19 | 3043 | if (!gup_fast_devmap_leaf(fault_pfn, addr, end, flags, pages, nr)) |
a9b6de77 | 3044 | return 0; |
b59f65fa | 3045 | |
a9b6de77 | 3046 | if (unlikely(pud_val(orig) != pud_val(*pudp))) { |
23babe19 | 3047 | gup_fast_undo_dev_pagemap(nr, nr_start, flags, pages); |
a9b6de77 DW |
3048 | return 0; |
3049 | } | |
3050 | return 1; | |
b59f65fa KS |
3051 | } |
3052 | #else | |
23babe19 DH |
3053 | static int gup_fast_devmap_pmd_leaf(pmd_t orig, pmd_t *pmdp, unsigned long addr, |
3054 | unsigned long end, unsigned int flags, struct page **pages, | |
3055 | int *nr) | |
b59f65fa KS |
3056 | { |
3057 | BUILD_BUG(); | |
3058 | return 0; | |
3059 | } | |
3060 | ||
23babe19 DH |
3061 | static int gup_fast_devmap_pud_leaf(pud_t pud, pud_t *pudp, unsigned long addr, |
3062 | unsigned long end, unsigned int flags, struct page **pages, | |
3063 | int *nr) | |
b59f65fa KS |
3064 | { |
3065 | BUILD_BUG(); | |
3066 | return 0; | |
3067 | } | |
3068 | #endif | |
3069 | ||
23babe19 DH |
3070 | static int gup_fast_pmd_leaf(pmd_t orig, pmd_t *pmdp, unsigned long addr, |
3071 | unsigned long end, unsigned int flags, struct page **pages, | |
3072 | int *nr) | |
2667f50e | 3073 | { |
667ed1f7 MWO |
3074 | struct page *page; |
3075 | struct folio *folio; | |
2667f50e SC |
3076 | int refs; |
3077 | ||
b798bec4 | 3078 | if (!pmd_access_permitted(orig, flags & FOLL_WRITE)) |
2667f50e SC |
3079 | return 0; |
3080 | ||
ae3c99e6 PX |
3081 | if (pmd_special(orig)) |
3082 | return 0; | |
3083 | ||
7af75561 IW |
3084 | if (pmd_devmap(orig)) { |
3085 | if (unlikely(flags & FOLL_LONGTERM)) | |
3086 | return 0; | |
23babe19 DH |
3087 | return gup_fast_devmap_pmd_leaf(orig, pmdp, addr, end, flags, |
3088 | pages, nr); | |
7af75561 | 3089 | } |
b59f65fa | 3090 | |
f3c94c62 PX |
3091 | page = pmd_page(orig); |
3092 | refs = record_subpages(page, PMD_SIZE, addr, end, pages + *nr); | |
2667f50e | 3093 | |
f442fa61 | 3094 | folio = try_grab_folio_fast(page, refs, flags); |
667ed1f7 | 3095 | if (!folio) |
2667f50e | 3096 | return 0; |
2667f50e SC |
3097 | |
3098 | if (unlikely(pmd_val(orig) != pmd_val(*pmdp))) { | |
667ed1f7 | 3099 | gup_put_folio(folio, refs, flags); |
2667f50e SC |
3100 | return 0; |
3101 | } | |
3102 | ||
f002882c | 3103 | if (!gup_fast_folio_allowed(folio, flags)) { |
a6e79df9 LS |
3104 | gup_put_folio(folio, refs, flags); |
3105 | return 0; | |
3106 | } | |
84209e87 | 3107 | if (!pmd_write(orig) && gup_must_unshare(NULL, flags, &folio->page)) { |
a7f22660 DH |
3108 | gup_put_folio(folio, refs, flags); |
3109 | return 0; | |
3110 | } | |
3111 | ||
a43e9820 | 3112 | *nr += refs; |
667ed1f7 | 3113 | folio_set_referenced(folio); |
2667f50e SC |
3114 | return 1; |
3115 | } | |
3116 | ||
23babe19 DH |
3117 | static int gup_fast_pud_leaf(pud_t orig, pud_t *pudp, unsigned long addr, |
3118 | unsigned long end, unsigned int flags, struct page **pages, | |
3119 | int *nr) | |
2667f50e | 3120 | { |
83afb52e MWO |
3121 | struct page *page; |
3122 | struct folio *folio; | |
2667f50e SC |
3123 | int refs; |
3124 | ||
b798bec4 | 3125 | if (!pud_access_permitted(orig, flags & FOLL_WRITE)) |
2667f50e SC |
3126 | return 0; |
3127 | ||
ae3c99e6 PX |
3128 | if (pud_special(orig)) |
3129 | return 0; | |
3130 | ||
7af75561 IW |
3131 | if (pud_devmap(orig)) { |
3132 | if (unlikely(flags & FOLL_LONGTERM)) | |
3133 | return 0; | |
23babe19 DH |
3134 | return gup_fast_devmap_pud_leaf(orig, pudp, addr, end, flags, |
3135 | pages, nr); | |
7af75561 | 3136 | } |
b59f65fa | 3137 | |
f3c94c62 PX |
3138 | page = pud_page(orig); |
3139 | refs = record_subpages(page, PUD_SIZE, addr, end, pages + *nr); | |
2667f50e | 3140 | |
f442fa61 | 3141 | folio = try_grab_folio_fast(page, refs, flags); |
83afb52e | 3142 | if (!folio) |
2667f50e | 3143 | return 0; |
2667f50e SC |
3144 | |
3145 | if (unlikely(pud_val(orig) != pud_val(*pudp))) { | |
83afb52e | 3146 | gup_put_folio(folio, refs, flags); |
2667f50e SC |
3147 | return 0; |
3148 | } | |
3149 | ||
f002882c | 3150 | if (!gup_fast_folio_allowed(folio, flags)) { |
a6e79df9 LS |
3151 | gup_put_folio(folio, refs, flags); |
3152 | return 0; | |
3153 | } | |
3154 | ||
84209e87 | 3155 | if (!pud_write(orig) && gup_must_unshare(NULL, flags, &folio->page)) { |
a7f22660 DH |
3156 | gup_put_folio(folio, refs, flags); |
3157 | return 0; | |
3158 | } | |
3159 | ||
a43e9820 | 3160 | *nr += refs; |
83afb52e | 3161 | folio_set_referenced(folio); |
2667f50e SC |
3162 | return 1; |
3163 | } | |
3164 | ||
23babe19 DH |
3165 | static int gup_fast_pmd_range(pud_t *pudp, pud_t pud, unsigned long addr, |
3166 | unsigned long end, unsigned int flags, struct page **pages, | |
3167 | int *nr) | |
2667f50e SC |
3168 | { |
3169 | unsigned long next; | |
3170 | pmd_t *pmdp; | |
3171 | ||
d3f7b1bb | 3172 | pmdp = pmd_offset_lockless(pudp, pud, addr); |
2667f50e | 3173 | do { |
1180e732 | 3174 | pmd_t pmd = pmdp_get_lockless(pmdp); |
2667f50e SC |
3175 | |
3176 | next = pmd_addr_end(addr, end); | |
84c3fc4e | 3177 | if (!pmd_present(pmd)) |
2667f50e SC |
3178 | return 0; |
3179 | ||
7db86dc3 | 3180 | if (unlikely(pmd_leaf(pmd))) { |
23babe19 | 3181 | /* See gup_fast_pte_range() */ |
d74943a2 | 3182 | if (pmd_protnone(pmd)) |
2667f50e SC |
3183 | return 0; |
3184 | ||
23babe19 | 3185 | if (!gup_fast_pmd_leaf(pmd, pmdp, addr, next, flags, |
2667f50e SC |
3186 | pages, nr)) |
3187 | return 0; | |
3188 | ||
23babe19 DH |
3189 | } else if (!gup_fast_pte_range(pmd, pmdp, addr, next, flags, |
3190 | pages, nr)) | |
2923117b | 3191 | return 0; |
2667f50e SC |
3192 | } while (pmdp++, addr = next, addr != end); |
3193 | ||
3194 | return 1; | |
3195 | } | |
3196 | ||
23babe19 DH |
3197 | static int gup_fast_pud_range(p4d_t *p4dp, p4d_t p4d, unsigned long addr, |
3198 | unsigned long end, unsigned int flags, struct page **pages, | |
3199 | int *nr) | |
2667f50e SC |
3200 | { |
3201 | unsigned long next; | |
3202 | pud_t *pudp; | |
3203 | ||
d3f7b1bb | 3204 | pudp = pud_offset_lockless(p4dp, p4d, addr); |
2667f50e | 3205 | do { |
e37c6982 | 3206 | pud_t pud = READ_ONCE(*pudp); |
2667f50e SC |
3207 | |
3208 | next = pud_addr_end(addr, end); | |
15494520 | 3209 | if (unlikely(!pud_present(pud))) |
2667f50e | 3210 | return 0; |
7db86dc3 | 3211 | if (unlikely(pud_leaf(pud))) { |
23babe19 DH |
3212 | if (!gup_fast_pud_leaf(pud, pudp, addr, next, flags, |
3213 | pages, nr)) | |
f30c59e9 | 3214 | return 0; |
23babe19 DH |
3215 | } else if (!gup_fast_pmd_range(pudp, pud, addr, next, flags, |
3216 | pages, nr)) | |
2667f50e SC |
3217 | return 0; |
3218 | } while (pudp++, addr = next, addr != end); | |
3219 | ||
3220 | return 1; | |
3221 | } | |
3222 | ||
23babe19 DH |
3223 | static int gup_fast_p4d_range(pgd_t *pgdp, pgd_t pgd, unsigned long addr, |
3224 | unsigned long end, unsigned int flags, struct page **pages, | |
3225 | int *nr) | |
c2febafc KS |
3226 | { |
3227 | unsigned long next; | |
3228 | p4d_t *p4dp; | |
3229 | ||
d3f7b1bb | 3230 | p4dp = p4d_offset_lockless(pgdp, pgd, addr); |
c2febafc KS |
3231 | do { |
3232 | p4d_t p4d = READ_ONCE(*p4dp); | |
3233 | ||
3234 | next = p4d_addr_end(addr, end); | |
089f9214 | 3235 | if (!p4d_present(p4d)) |
c2febafc | 3236 | return 0; |
1965e933 | 3237 | BUILD_BUG_ON(p4d_leaf(p4d)); |
8268614b CL |
3238 | if (!gup_fast_pud_range(p4dp, p4d, addr, next, flags, |
3239 | pages, nr)) | |
c2febafc KS |
3240 | return 0; |
3241 | } while (p4dp++, addr = next, addr != end); | |
3242 | ||
3243 | return 1; | |
3244 | } | |
3245 | ||
23babe19 | 3246 | static void gup_fast_pgd_range(unsigned long addr, unsigned long end, |
b798bec4 | 3247 | unsigned int flags, struct page **pages, int *nr) |
5b65c467 KS |
3248 | { |
3249 | unsigned long next; | |
3250 | pgd_t *pgdp; | |
3251 | ||
3252 | pgdp = pgd_offset(current->mm, addr); | |
3253 | do { | |
3254 | pgd_t pgd = READ_ONCE(*pgdp); | |
3255 | ||
3256 | next = pgd_addr_end(addr, end); | |
3257 | if (pgd_none(pgd)) | |
3258 | return; | |
339122ab BH |
3259 | BUILD_BUG_ON(pgd_leaf(pgd)); |
3260 | if (!gup_fast_p4d_range(pgdp, pgd, addr, next, flags, | |
3261 | pages, nr)) | |
5b65c467 KS |
3262 | return; |
3263 | } while (pgdp++, addr = next, addr != end); | |
3264 | } | |
050a9adc | 3265 | #else |
23babe19 | 3266 | static inline void gup_fast_pgd_range(unsigned long addr, unsigned long end, |
050a9adc CH |
3267 | unsigned int flags, struct page **pages, int *nr) |
3268 | { | |
3269 | } | |
25176ad0 | 3270 | #endif /* CONFIG_HAVE_GUP_FAST */ |
5b65c467 KS |
3271 | |
3272 | #ifndef gup_fast_permitted | |
3273 | /* | |
dadbb612 | 3274 | * Check if it's allowed to use get_user_pages_fast_only() for the range, or |
5b65c467 KS |
3275 | * we need to fall back to the slow version: |
3276 | */ | |
26f4c328 | 3277 | static bool gup_fast_permitted(unsigned long start, unsigned long end) |
5b65c467 | 3278 | { |
26f4c328 | 3279 | return true; |
5b65c467 KS |
3280 | } |
3281 | #endif | |
3282 | ||
23babe19 DH |
3283 | static unsigned long gup_fast(unsigned long start, unsigned long end, |
3284 | unsigned int gup_flags, struct page **pages) | |
c28b1fc7 JG |
3285 | { |
3286 | unsigned long flags; | |
3287 | int nr_pinned = 0; | |
57efa1fe | 3288 | unsigned seq; |
c28b1fc7 | 3289 | |
25176ad0 | 3290 | if (!IS_ENABLED(CONFIG_HAVE_GUP_FAST) || |
c28b1fc7 JG |
3291 | !gup_fast_permitted(start, end)) |
3292 | return 0; | |
3293 | ||
57efa1fe | 3294 | if (gup_flags & FOLL_PIN) { |
75285852 | 3295 | if (!raw_seqcount_try_begin(¤t->mm->write_protect_seq, seq)) |
57efa1fe JG |
3296 | return 0; |
3297 | } | |
3298 | ||
c28b1fc7 JG |
3299 | /* |
3300 | * Disable interrupts. The nested form is used, in order to allow full, | |
3301 | * general purpose use of this routine. | |
3302 | * | |
3303 | * With interrupts disabled, we block page table pages from being freed | |
3304 | * from under us. See struct mmu_table_batch comments in | |
3305 | * include/asm-generic/tlb.h for more details. | |
3306 | * | |
3307 | * We do not adopt an rcu_read_lock() here as we also want to block IPIs | |
52084f25 | 3308 | * that come from callers of tlb_remove_table_sync_one(). |
c28b1fc7 JG |
3309 | */ |
3310 | local_irq_save(flags); | |
23babe19 | 3311 | gup_fast_pgd_range(start, end, gup_flags, pages, &nr_pinned); |
c28b1fc7 | 3312 | local_irq_restore(flags); |
57efa1fe JG |
3313 | |
3314 | /* | |
3315 | * When pinning pages for DMA there could be a concurrent write protect | |
23babe19 | 3316 | * from fork() via copy_page_range(), in this case always fail GUP-fast. |
57efa1fe JG |
3317 | */ |
3318 | if (gup_flags & FOLL_PIN) { | |
3319 | if (read_seqcount_retry(¤t->mm->write_protect_seq, seq)) { | |
23babe19 | 3320 | gup_fast_unpin_user_pages(pages, nr_pinned); |
57efa1fe | 3321 | return 0; |
b6a2619c DH |
3322 | } else { |
3323 | sanity_check_pinned_pages(pages, nr_pinned); | |
57efa1fe JG |
3324 | } |
3325 | } | |
c28b1fc7 JG |
3326 | return nr_pinned; |
3327 | } | |
3328 | ||
23babe19 DH |
3329 | static int gup_fast_fallback(unsigned long start, unsigned long nr_pages, |
3330 | unsigned int gup_flags, struct page **pages) | |
2667f50e | 3331 | { |
c28b1fc7 JG |
3332 | unsigned long len, end; |
3333 | unsigned long nr_pinned; | |
b2a72dff | 3334 | int locked = 0; |
c28b1fc7 | 3335 | int ret; |
2667f50e | 3336 | |
f4000fdf | 3337 | if (WARN_ON_ONCE(gup_flags & ~(FOLL_WRITE | FOLL_LONGTERM | |
376a34ef | 3338 | FOLL_FORCE | FOLL_PIN | FOLL_GET | |
4003f107 | 3339 | FOLL_FAST_ONLY | FOLL_NOFAULT | |
d74943a2 | 3340 | FOLL_PCI_P2PDMA | FOLL_HONOR_NUMA_FAULT))) |
817be129 CH |
3341 | return -EINVAL; |
3342 | ||
a458b76a AA |
3343 | if (gup_flags & FOLL_PIN) |
3344 | mm_set_has_pinned_flag(¤t->mm->flags); | |
008cfe44 | 3345 | |
f81cd178 | 3346 | if (!(gup_flags & FOLL_FAST_ONLY)) |
da1c55f1 | 3347 | might_lock_read(¤t->mm->mmap_lock); |
f81cd178 | 3348 | |
f455c854 | 3349 | start = untagged_addr(start) & PAGE_MASK; |
c28b1fc7 JG |
3350 | len = nr_pages << PAGE_SHIFT; |
3351 | if (check_add_overflow(start, len, &end)) | |
9883c7f8 | 3352 | return -EOVERFLOW; |
6014bc27 LT |
3353 | if (end > TASK_SIZE_MAX) |
3354 | return -EFAULT; | |
73e10a61 | 3355 | |
23babe19 | 3356 | nr_pinned = gup_fast(start, end, gup_flags, pages); |
c28b1fc7 JG |
3357 | if (nr_pinned == nr_pages || gup_flags & FOLL_FAST_ONLY) |
3358 | return nr_pinned; | |
2667f50e | 3359 | |
c28b1fc7 JG |
3360 | /* Slow path: try to get the remaining pages with get_user_pages */ |
3361 | start += nr_pinned << PAGE_SHIFT; | |
3362 | pages += nr_pinned; | |
b2a72dff | 3363 | ret = __gup_longterm_locked(current->mm, start, nr_pages - nr_pinned, |
b2cac248 | 3364 | pages, &locked, |
f04740f5 | 3365 | gup_flags | FOLL_TOUCH | FOLL_UNLOCKABLE); |
c28b1fc7 JG |
3366 | if (ret < 0) { |
3367 | /* | |
3368 | * The caller has to unpin the pages we already pinned so | |
3369 | * returning -errno is not an option | |
3370 | */ | |
3371 | if (nr_pinned) | |
3372 | return nr_pinned; | |
3373 | return ret; | |
2667f50e | 3374 | } |
c28b1fc7 | 3375 | return ret + nr_pinned; |
2667f50e | 3376 | } |
c28b1fc7 | 3377 | |
dadbb612 SJ |
3378 | /** |
3379 | * get_user_pages_fast_only() - pin user pages in memory | |
3380 | * @start: starting user address | |
3381 | * @nr_pages: number of pages from start to pin | |
3382 | * @gup_flags: flags modifying pin behaviour | |
3383 | * @pages: array that receives pointers to the pages pinned. | |
3384 | * Should be at least nr_pages long. | |
3385 | * | |
9e1f0580 JH |
3386 | * Like get_user_pages_fast() except it's IRQ-safe in that it won't fall back to |
3387 | * the regular GUP. | |
9e1f0580 JH |
3388 | * |
3389 | * If the architecture does not support this function, simply return with no | |
3390 | * pages pinned. | |
3391 | * | |
3392 | * Careful, careful! COW breaking can go either way, so a non-write | |
3393 | * access can get ambiguous page results. If you call this function without | |
3394 | * 'write' set, you'd better be sure that you're ok with that ambiguity. | |
3395 | */ | |
dadbb612 SJ |
3396 | int get_user_pages_fast_only(unsigned long start, int nr_pages, |
3397 | unsigned int gup_flags, struct page **pages) | |
9e1f0580 | 3398 | { |
9e1f0580 JH |
3399 | /* |
3400 | * Internally (within mm/gup.c), gup fast variants must set FOLL_GET, | |
3401 | * because gup fast is always a "pin with a +1 page refcount" request. | |
376a34ef JH |
3402 | * |
3403 | * FOLL_FAST_ONLY is required in order to match the API description of | |
3404 | * this routine: no fall back to regular ("slow") GUP. | |
9e1f0580 | 3405 | */ |
b2cac248 | 3406 | if (!is_valid_gup_args(pages, NULL, &gup_flags, |
d64e2dbc JG |
3407 | FOLL_GET | FOLL_FAST_ONLY)) |
3408 | return -EINVAL; | |
9e1f0580 | 3409 | |
23babe19 | 3410 | return gup_fast_fallback(start, nr_pages, gup_flags, pages); |
9e1f0580 | 3411 | } |
dadbb612 | 3412 | EXPORT_SYMBOL_GPL(get_user_pages_fast_only); |
9e1f0580 | 3413 | |
eddb1c22 JH |
3414 | /** |
3415 | * get_user_pages_fast() - pin user pages in memory | |
3faa52c0 JH |
3416 | * @start: starting user address |
3417 | * @nr_pages: number of pages from start to pin | |
3418 | * @gup_flags: flags modifying pin behaviour | |
3419 | * @pages: array that receives pointers to the pages pinned. | |
3420 | * Should be at least nr_pages long. | |
eddb1c22 | 3421 | * |
c1e8d7c6 | 3422 | * Attempt to pin user pages in memory without taking mm->mmap_lock. |
eddb1c22 JH |
3423 | * If not successful, it will fall back to taking the lock and |
3424 | * calling get_user_pages(). | |
3425 | * | |
3426 | * Returns number of pages pinned. This may be fewer than the number requested. | |
3427 | * If nr_pages is 0 or negative, returns 0. If no pages were pinned, returns | |
3428 | * -errno. | |
3429 | */ | |
3430 | int get_user_pages_fast(unsigned long start, int nr_pages, | |
3431 | unsigned int gup_flags, struct page **pages) | |
3432 | { | |
94202f12 JH |
3433 | /* |
3434 | * The caller may or may not have explicitly set FOLL_GET; either way is | |
3435 | * OK. However, internally (within mm/gup.c), gup fast variants must set | |
3436 | * FOLL_GET, because gup fast is always a "pin with a +1 page refcount" | |
3437 | * request. | |
3438 | */ | |
b2cac248 | 3439 | if (!is_valid_gup_args(pages, NULL, &gup_flags, FOLL_GET)) |
d64e2dbc | 3440 | return -EINVAL; |
23babe19 | 3441 | return gup_fast_fallback(start, nr_pages, gup_flags, pages); |
eddb1c22 | 3442 | } |
050a9adc | 3443 | EXPORT_SYMBOL_GPL(get_user_pages_fast); |
eddb1c22 JH |
3444 | |
3445 | /** | |
3446 | * pin_user_pages_fast() - pin user pages in memory without taking locks | |
3447 | * | |
3faa52c0 JH |
3448 | * @start: starting user address |
3449 | * @nr_pages: number of pages from start to pin | |
3450 | * @gup_flags: flags modifying pin behaviour | |
3451 | * @pages: array that receives pointers to the pages pinned. | |
3452 | * Should be at least nr_pages long. | |
3453 | * | |
3454 | * Nearly the same as get_user_pages_fast(), except that FOLL_PIN is set. See | |
3455 | * get_user_pages_fast() for documentation on the function arguments, because | |
3456 | * the arguments here are identical. | |
3457 | * | |
3458 | * FOLL_PIN means that the pages must be released via unpin_user_page(). Please | |
72ef5e52 | 3459 | * see Documentation/core-api/pin_user_pages.rst for further details. |
c8070b78 DH |
3460 | * |
3461 | * Note that if a zero_page is amongst the returned pages, it will not have | |
3462 | * pins in it and unpin_user_page() will not remove pins from it. | |
eddb1c22 JH |
3463 | */ |
3464 | int pin_user_pages_fast(unsigned long start, int nr_pages, | |
3465 | unsigned int gup_flags, struct page **pages) | |
3466 | { | |
b2cac248 | 3467 | if (!is_valid_gup_args(pages, NULL, &gup_flags, FOLL_PIN)) |
3faa52c0 | 3468 | return -EINVAL; |
23babe19 | 3469 | return gup_fast_fallback(start, nr_pages, gup_flags, pages); |
eddb1c22 JH |
3470 | } |
3471 | EXPORT_SYMBOL_GPL(pin_user_pages_fast); | |
3472 | ||
3473 | /** | |
64019a2e | 3474 | * pin_user_pages_remote() - pin pages of a remote process |
eddb1c22 | 3475 | * |
3faa52c0 JH |
3476 | * @mm: mm_struct of target mm |
3477 | * @start: starting user address | |
3478 | * @nr_pages: number of pages from start to pin | |
3479 | * @gup_flags: flags modifying lookup behaviour | |
3480 | * @pages: array that receives pointers to the pages pinned. | |
0768c8de | 3481 | * Should be at least nr_pages long. |
3faa52c0 JH |
3482 | * @locked: pointer to lock flag indicating whether lock is held and |
3483 | * subsequently whether VM_FAULT_RETRY functionality can be | |
3484 | * utilised. Lock must initially be held. | |
3485 | * | |
3486 | * Nearly the same as get_user_pages_remote(), except that FOLL_PIN is set. See | |
3487 | * get_user_pages_remote() for documentation on the function arguments, because | |
3488 | * the arguments here are identical. | |
3489 | * | |
3490 | * FOLL_PIN means that the pages must be released via unpin_user_page(). Please | |
72ef5e52 | 3491 | * see Documentation/core-api/pin_user_pages.rst for details. |
c8070b78 DH |
3492 | * |
3493 | * Note that if a zero_page is amongst the returned pages, it will not have | |
3494 | * pins in it and unpin_user_page*() will not remove pins from it. | |
eddb1c22 | 3495 | */ |
64019a2e | 3496 | long pin_user_pages_remote(struct mm_struct *mm, |
eddb1c22 JH |
3497 | unsigned long start, unsigned long nr_pages, |
3498 | unsigned int gup_flags, struct page **pages, | |
0b295316 | 3499 | int *locked) |
eddb1c22 | 3500 | { |
9a863a6a JG |
3501 | int local_locked = 1; |
3502 | ||
b2cac248 | 3503 | if (!is_valid_gup_args(pages, locked, &gup_flags, |
d64e2dbc JG |
3504 | FOLL_PIN | FOLL_TOUCH | FOLL_REMOTE)) |
3505 | return 0; | |
b2cac248 | 3506 | return __gup_longterm_locked(mm, start, nr_pages, pages, |
9a863a6a | 3507 | locked ? locked : &local_locked, |
d64e2dbc | 3508 | gup_flags); |
eddb1c22 JH |
3509 | } |
3510 | EXPORT_SYMBOL(pin_user_pages_remote); | |
3511 | ||
3512 | /** | |
3513 | * pin_user_pages() - pin user pages in memory for use by other devices | |
3514 | * | |
3faa52c0 JH |
3515 | * @start: starting user address |
3516 | * @nr_pages: number of pages from start to pin | |
3517 | * @gup_flags: flags modifying lookup behaviour | |
3518 | * @pages: array that receives pointers to the pages pinned. | |
0768c8de | 3519 | * Should be at least nr_pages long. |
3faa52c0 JH |
3520 | * |
3521 | * Nearly the same as get_user_pages(), except that FOLL_TOUCH is not set, and | |
3522 | * FOLL_PIN is set. | |
3523 | * | |
3524 | * FOLL_PIN means that the pages must be released via unpin_user_page(). Please | |
72ef5e52 | 3525 | * see Documentation/core-api/pin_user_pages.rst for details. |
c8070b78 DH |
3526 | * |
3527 | * Note that if a zero_page is amongst the returned pages, it will not have | |
3528 | * pins in it and unpin_user_page*() will not remove pins from it. | |
eddb1c22 JH |
3529 | */ |
3530 | long pin_user_pages(unsigned long start, unsigned long nr_pages, | |
4c630f30 | 3531 | unsigned int gup_flags, struct page **pages) |
eddb1c22 | 3532 | { |
9a863a6a JG |
3533 | int locked = 1; |
3534 | ||
b2cac248 | 3535 | if (!is_valid_gup_args(pages, NULL, &gup_flags, FOLL_PIN)) |
d64e2dbc | 3536 | return 0; |
64019a2e | 3537 | return __gup_longterm_locked(current->mm, start, nr_pages, |
b2cac248 | 3538 | pages, &locked, gup_flags); |
eddb1c22 JH |
3539 | } |
3540 | EXPORT_SYMBOL(pin_user_pages); | |
91429023 JH |
3541 | |
3542 | /* | |
3543 | * pin_user_pages_unlocked() is the FOLL_PIN variant of | |
3544 | * get_user_pages_unlocked(). Behavior is the same, except that this one sets | |
3545 | * FOLL_PIN and rejects FOLL_GET. | |
c8070b78 DH |
3546 | * |
3547 | * Note that if a zero_page is amongst the returned pages, it will not have | |
3548 | * pins in it and unpin_user_page*() will not remove pins from it. | |
91429023 JH |
3549 | */ |
3550 | long pin_user_pages_unlocked(unsigned long start, unsigned long nr_pages, | |
3551 | struct page **pages, unsigned int gup_flags) | |
3552 | { | |
b2a72dff | 3553 | int locked = 0; |
91429023 | 3554 | |
b2cac248 | 3555 | if (!is_valid_gup_args(pages, NULL, &gup_flags, |
f04740f5 | 3556 | FOLL_PIN | FOLL_TOUCH | FOLL_UNLOCKABLE)) |
d64e2dbc | 3557 | return 0; |
0768c8de | 3558 | |
b2cac248 | 3559 | return __gup_longterm_locked(current->mm, start, nr_pages, pages, |
b2a72dff | 3560 | &locked, gup_flags); |
91429023 JH |
3561 | } |
3562 | EXPORT_SYMBOL(pin_user_pages_unlocked); | |
89c1905d VK |
3563 | |
3564 | /** | |
3565 | * memfd_pin_folios() - pin folios associated with a memfd | |
3566 | * @memfd: the memfd whose folios are to be pinned | |
3567 | * @start: the first memfd offset | |
3568 | * @end: the last memfd offset (inclusive) | |
3569 | * @folios: array that receives pointers to the folios pinned | |
3570 | * @max_folios: maximum number of entries in @folios | |
3571 | * @offset: the offset into the first folio | |
3572 | * | |
3573 | * Attempt to pin folios associated with a memfd in the contiguous range | |
3574 | * [start, end]. Given that a memfd is either backed by shmem or hugetlb, | |
3575 | * the folios can either be found in the page cache or need to be allocated | |
3576 | * if necessary. Once the folios are located, they are all pinned via | |
3577 | * FOLL_PIN and @offset is populatedwith the offset into the first folio. | |
3578 | * And, eventually, these pinned folios must be released either using | |
3579 | * unpin_folios() or unpin_folio(). | |
3580 | * | |
3581 | * It must be noted that the folios may be pinned for an indefinite amount | |
3582 | * of time. And, in most cases, the duration of time they may stay pinned | |
3583 | * would be controlled by the userspace. This behavior is effectively the | |
3584 | * same as using FOLL_LONGTERM with other GUP APIs. | |
3585 | * | |
3586 | * Returns number of folios pinned, which could be less than @max_folios | |
3587 | * as it depends on the folio sizes that cover the range [start, end]. | |
3588 | * If no folios were pinned, it returns -errno. | |
3589 | */ | |
3590 | long memfd_pin_folios(struct file *memfd, loff_t start, loff_t end, | |
3591 | struct folio **folios, unsigned int max_folios, | |
3592 | pgoff_t *offset) | |
3593 | { | |
3594 | unsigned int flags, nr_folios, nr_found; | |
3595 | unsigned int i, pgshift = PAGE_SHIFT; | |
30f62b92 | 3596 | pgoff_t start_idx, end_idx; |
89c1905d VK |
3597 | struct folio *folio = NULL; |
3598 | struct folio_batch fbatch; | |
dc677b5f | 3599 | struct hstate *h; |
89c1905d VK |
3600 | long ret = -EINVAL; |
3601 | ||
3602 | if (start < 0 || start > end || !max_folios) | |
3603 | return -EINVAL; | |
3604 | ||
3605 | if (!memfd) | |
3606 | return -EINVAL; | |
3607 | ||
3608 | if (!shmem_file(memfd) && !is_file_hugepages(memfd)) | |
3609 | return -EINVAL; | |
3610 | ||
3611 | if (end >= i_size_read(file_inode(memfd))) | |
3612 | return -EINVAL; | |
3613 | ||
3614 | if (is_file_hugepages(memfd)) { | |
3615 | h = hstate_file(memfd); | |
3616 | pgshift = huge_page_shift(h); | |
3617 | } | |
3618 | ||
3619 | flags = memalloc_pin_save(); | |
3620 | do { | |
3621 | nr_folios = 0; | |
3622 | start_idx = start >> pgshift; | |
3623 | end_idx = end >> pgshift; | |
3624 | if (is_file_hugepages(memfd)) { | |
3625 | start_idx <<= huge_page_order(h); | |
3626 | end_idx <<= huge_page_order(h); | |
3627 | } | |
3628 | ||
3629 | folio_batch_init(&fbatch); | |
3630 | while (start_idx <= end_idx && nr_folios < max_folios) { | |
3631 | /* | |
3632 | * In most cases, we should be able to find the folios | |
3633 | * in the page cache. If we cannot find them for some | |
3634 | * reason, we try to allocate them and add them to the | |
3635 | * page cache. | |
3636 | */ | |
3637 | nr_found = filemap_get_folios_contig(memfd->f_mapping, | |
3638 | &start_idx, | |
3639 | end_idx, | |
3640 | &fbatch); | |
3641 | if (folio) { | |
3642 | folio_put(folio); | |
3643 | folio = NULL; | |
3644 | } | |
3645 | ||
89c1905d | 3646 | for (i = 0; i < nr_found; i++) { |
fe488d34 | 3647 | folio = fbatch.folios[i]; |
89c1905d VK |
3648 | |
3649 | if (try_grab_folio(folio, 1, FOLL_PIN)) { | |
3650 | folio_batch_release(&fbatch); | |
3651 | ret = -EINVAL; | |
3652 | goto err; | |
3653 | } | |
3654 | ||
3655 | if (nr_folios == 0) | |
3656 | *offset = offset_in_folio(folio, start); | |
3657 | ||
3658 | folios[nr_folios] = folio; | |
89c1905d VK |
3659 | if (++nr_folios == max_folios) |
3660 | break; | |
3661 | } | |
3662 | ||
3663 | folio = NULL; | |
3664 | folio_batch_release(&fbatch); | |
3665 | if (!nr_found) { | |
3666 | folio = memfd_alloc_folio(memfd, start_idx); | |
3667 | if (IS_ERR(folio)) { | |
3668 | ret = PTR_ERR(folio); | |
3669 | if (ret != -EEXIST) | |
3670 | goto err; | |
ce645b9f | 3671 | folio = NULL; |
89c1905d VK |
3672 | } |
3673 | } | |
3674 | } | |
3675 | ||
3676 | ret = check_and_migrate_movable_folios(nr_folios, folios); | |
3677 | } while (ret == -EAGAIN); | |
3678 | ||
3679 | memalloc_pin_restore(flags); | |
3680 | return ret ? ret : nr_folios; | |
3681 | err: | |
3682 | memalloc_pin_restore(flags); | |
3683 | unpin_folios(folios, nr_folios); | |
3684 | ||
3685 | return ret; | |
3686 | } | |
3687 | EXPORT_SYMBOL_GPL(memfd_pin_folios); | |
a2ad1b81 SS |
3688 | |
3689 | /** | |
3690 | * folio_add_pins() - add pins to an already-pinned folio | |
3691 | * @folio: the folio to add more pins to | |
3692 | * @pins: number of pins to add | |
3693 | * | |
3694 | * Try to add more pins to an already-pinned folio. The semantics | |
3695 | * of the pin (e.g., FOLL_WRITE) follow any existing pin and cannot | |
3696 | * be changed. | |
3697 | * | |
3698 | * This function is helpful when having obtained a pin on a large folio | |
3699 | * using memfd_pin_folios(), but wanting to logically unpin parts | |
3700 | * (e.g., individual pages) of the folio later, for example, using | |
3701 | * unpin_user_page_range_dirty_lock(). | |
3702 | * | |
3703 | * This is not the right interface to initially pin a folio. | |
3704 | */ | |
3705 | int folio_add_pins(struct folio *folio, unsigned int pins) | |
3706 | { | |
3707 | VM_WARN_ON_ONCE(!folio_maybe_dma_pinned(folio)); | |
3708 | ||
3709 | return try_grab_folio(folio, pins, FOLL_PIN); | |
3710 | } | |
3711 | EXPORT_SYMBOL_GPL(folio_add_pins); |