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