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