1 // SPDX-License-Identifier: GPL-2.0-only
2 #include <linux/kernel.h>
3 #include <linux/errno.h>
5 #include <linux/spinlock.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>
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>
21 #include <asm/mmu_context.h>
22 #include <asm/tlbflush.h>
26 struct follow_page_context {
27 struct dev_pagemap *pgmap;
28 unsigned int page_mask;
31 static void hpage_pincount_add(struct page *page, int refs)
33 VM_BUG_ON_PAGE(!hpage_pincount_available(page), page);
34 VM_BUG_ON_PAGE(page != compound_head(page), page);
36 atomic_add(refs, compound_pincount_ptr(page));
39 static void hpage_pincount_sub(struct page *page, int refs)
41 VM_BUG_ON_PAGE(!hpage_pincount_available(page), page);
42 VM_BUG_ON_PAGE(page != compound_head(page), page);
44 atomic_sub(refs, compound_pincount_ptr(page));
48 * Return the compound head page with ref appropriately incremented,
49 * or NULL if that failed.
51 static inline struct page *try_get_compound_head(struct page *page, int refs)
53 struct page *head = compound_head(page);
55 if (WARN_ON_ONCE(page_ref_count(head) < 0))
57 if (unlikely(!page_cache_add_speculative(head, refs)))
63 * try_grab_compound_head() - attempt to elevate a page's refcount, by a
64 * flags-dependent amount.
66 * "grab" names in this file mean, "look at flags to decide whether to use
67 * FOLL_PIN or FOLL_GET behavior, when incrementing the page's refcount.
69 * Either FOLL_PIN or FOLL_GET (or neither) must be set, but not both at the
70 * same time. (That's true throughout the get_user_pages*() and
71 * pin_user_pages*() APIs.) Cases:
73 * FOLL_GET: page's refcount will be incremented by 1.
74 * FOLL_PIN: page's refcount will be incremented by GUP_PIN_COUNTING_BIAS.
76 * Return: head page (with refcount appropriately incremented) for success, or
77 * NULL upon failure. If neither FOLL_GET nor FOLL_PIN was set, that's
78 * considered failure, and furthermore, a likely bug in the caller, so a warning
81 __maybe_unused struct page *try_grab_compound_head(struct page *page,
82 int refs, unsigned int flags)
85 return try_get_compound_head(page, refs);
86 else if (flags & FOLL_PIN) {
90 * Can't do FOLL_LONGTERM + FOLL_PIN with CMA in the gup fast
91 * path, so fail and let the caller fall back to the slow path.
93 if (unlikely(flags & FOLL_LONGTERM) &&
94 is_migrate_cma_page(page))
98 * When pinning a compound page of order > 1 (which is what
99 * hpage_pincount_available() checks for), use an exact count to
100 * track it, via hpage_pincount_add/_sub().
102 * However, be sure to *also* increment the normal page refcount
103 * field at least once, so that the page really is pinned.
105 if (!hpage_pincount_available(page))
106 refs *= GUP_PIN_COUNTING_BIAS;
108 page = try_get_compound_head(page, refs);
112 if (hpage_pincount_available(page))
113 hpage_pincount_add(page, refs);
115 mod_node_page_state(page_pgdat(page), NR_FOLL_PIN_ACQUIRED,
125 static void put_compound_head(struct page *page, int refs, unsigned int flags)
127 if (flags & FOLL_PIN) {
128 mod_node_page_state(page_pgdat(page), NR_FOLL_PIN_RELEASED,
131 if (hpage_pincount_available(page))
132 hpage_pincount_sub(page, refs);
134 refs *= GUP_PIN_COUNTING_BIAS;
137 VM_BUG_ON_PAGE(page_ref_count(page) < refs, page);
139 * Calling put_page() for each ref is unnecessarily slow. Only the last
140 * ref needs a put_page().
143 page_ref_sub(page, refs - 1);
148 * try_grab_page() - elevate a page's refcount by a flag-dependent amount
150 * This might not do anything at all, depending on the flags argument.
152 * "grab" names in this file mean, "look at flags to decide whether to use
153 * FOLL_PIN or FOLL_GET behavior, when incrementing the page's refcount.
155 * @page: pointer to page to be grabbed
156 * @flags: gup flags: these are the FOLL_* flag values.
158 * Either FOLL_PIN or FOLL_GET (or neither) may be set, but not both at the same
161 * FOLL_GET: page's refcount will be incremented by 1.
162 * FOLL_PIN: page's refcount will be incremented by GUP_PIN_COUNTING_BIAS.
164 * Return: true for success, or if no action was required (if neither FOLL_PIN
165 * nor FOLL_GET was set, nothing is done). False for failure: FOLL_GET or
166 * FOLL_PIN was set, but the page could not be grabbed.
168 bool __must_check try_grab_page(struct page *page, unsigned int flags)
170 WARN_ON_ONCE((flags & (FOLL_GET | FOLL_PIN)) == (FOLL_GET | FOLL_PIN));
172 if (flags & FOLL_GET)
173 return try_get_page(page);
174 else if (flags & FOLL_PIN) {
177 page = compound_head(page);
179 if (WARN_ON_ONCE(page_ref_count(page) <= 0))
182 if (hpage_pincount_available(page))
183 hpage_pincount_add(page, 1);
185 refs = GUP_PIN_COUNTING_BIAS;
188 * Similar to try_grab_compound_head(): even if using the
189 * hpage_pincount_add/_sub() routines, be sure to
190 * *also* increment the normal page refcount field at least
191 * once, so that the page really is pinned.
193 page_ref_add(page, refs);
195 mod_node_page_state(page_pgdat(page), NR_FOLL_PIN_ACQUIRED, 1);
202 * unpin_user_page() - release a dma-pinned page
203 * @page: pointer to page to be released
205 * Pages that were pinned via pin_user_pages*() must be released via either
206 * unpin_user_page(), or one of the unpin_user_pages*() routines. This is so
207 * that such pages can be separately tracked and uniquely handled. In
208 * particular, interactions with RDMA and filesystems need special handling.
210 void unpin_user_page(struct page *page)
212 put_compound_head(compound_head(page), 1, FOLL_PIN);
214 EXPORT_SYMBOL(unpin_user_page);
217 * unpin_user_pages_dirty_lock() - release and optionally dirty gup-pinned pages
218 * @pages: array of pages to be maybe marked dirty, and definitely released.
219 * @npages: number of pages in the @pages array.
220 * @make_dirty: whether to mark the pages dirty
222 * "gup-pinned page" refers to a page that has had one of the get_user_pages()
223 * variants called on that page.
225 * For each page in the @pages array, make that page (or its head page, if a
226 * compound page) dirty, if @make_dirty is true, and if the page was previously
227 * listed as clean. In any case, releases all pages using unpin_user_page(),
228 * possibly via unpin_user_pages(), for the non-dirty case.
230 * Please see the unpin_user_page() documentation for details.
232 * set_page_dirty_lock() is used internally. If instead, set_page_dirty() is
233 * required, then the caller should a) verify that this is really correct,
234 * because _lock() is usually required, and b) hand code it:
235 * set_page_dirty_lock(), unpin_user_page().
238 void unpin_user_pages_dirty_lock(struct page **pages, unsigned long npages,
244 * TODO: this can be optimized for huge pages: if a series of pages is
245 * physically contiguous and part of the same compound page, then a
246 * single operation to the head page should suffice.
250 unpin_user_pages(pages, npages);
254 for (index = 0; index < npages; index++) {
255 struct page *page = compound_head(pages[index]);
257 * Checking PageDirty at this point may race with
258 * clear_page_dirty_for_io(), but that's OK. Two key
261 * 1) This code sees the page as already dirty, so it
262 * skips the call to set_page_dirty(). That could happen
263 * because clear_page_dirty_for_io() called
264 * page_mkclean(), followed by set_page_dirty().
265 * However, now the page is going to get written back,
266 * which meets the original intention of setting it
267 * dirty, so all is well: clear_page_dirty_for_io() goes
268 * on to call TestClearPageDirty(), and write the page
271 * 2) This code sees the page as clean, so it calls
272 * set_page_dirty(). The page stays dirty, despite being
273 * written back, so it gets written back again in the
274 * next writeback cycle. This is harmless.
276 if (!PageDirty(page))
277 set_page_dirty_lock(page);
278 unpin_user_page(page);
281 EXPORT_SYMBOL(unpin_user_pages_dirty_lock);
284 * unpin_user_pages() - release an array of gup-pinned pages.
285 * @pages: array of pages to be marked dirty and released.
286 * @npages: number of pages in the @pages array.
288 * For each page in the @pages array, release the page using unpin_user_page().
290 * Please see the unpin_user_page() documentation for details.
292 void unpin_user_pages(struct page **pages, unsigned long npages)
297 * If this WARN_ON() fires, then the system *might* be leaking pages (by
298 * leaving them pinned), but probably not. More likely, gup/pup returned
299 * a hard -ERRNO error to the caller, who erroneously passed it here.
301 if (WARN_ON(IS_ERR_VALUE(npages)))
304 * TODO: this can be optimized for huge pages: if a series of pages is
305 * physically contiguous and part of the same compound page, then a
306 * single operation to the head page should suffice.
308 for (index = 0; index < npages; index++)
309 unpin_user_page(pages[index]);
311 EXPORT_SYMBOL(unpin_user_pages);
314 static struct page *no_page_table(struct vm_area_struct *vma,
318 * When core dumping an enormous anonymous area that nobody
319 * has touched so far, we don't want to allocate unnecessary pages or
320 * page tables. Return error instead of NULL to skip handle_mm_fault,
321 * then get_dump_page() will return NULL to leave a hole in the dump.
322 * But we can only make this optimization where a hole would surely
323 * be zero-filled if handle_mm_fault() actually did handle it.
325 if ((flags & FOLL_DUMP) &&
326 (vma_is_anonymous(vma) || !vma->vm_ops->fault))
327 return ERR_PTR(-EFAULT);
331 static int follow_pfn_pte(struct vm_area_struct *vma, unsigned long address,
332 pte_t *pte, unsigned int flags)
334 /* No page to get reference */
335 if (flags & FOLL_GET)
338 if (flags & FOLL_TOUCH) {
341 if (flags & FOLL_WRITE)
342 entry = pte_mkdirty(entry);
343 entry = pte_mkyoung(entry);
345 if (!pte_same(*pte, entry)) {
346 set_pte_at(vma->vm_mm, address, pte, entry);
347 update_mmu_cache(vma, address, pte);
351 /* Proper page table entry exists, but no corresponding struct page */
356 * FOLL_FORCE can write to even unwritable pte's, but only
357 * after we've gone through a COW cycle and they are dirty.
359 static inline bool can_follow_write_pte(pte_t pte, unsigned int flags)
361 return pte_write(pte) ||
362 ((flags & FOLL_FORCE) && (flags & FOLL_COW) && pte_dirty(pte));
365 static struct page *follow_page_pte(struct vm_area_struct *vma,
366 unsigned long address, pmd_t *pmd, unsigned int flags,
367 struct dev_pagemap **pgmap)
369 struct mm_struct *mm = vma->vm_mm;
375 /* FOLL_GET and FOLL_PIN are mutually exclusive. */
376 if (WARN_ON_ONCE((flags & (FOLL_PIN | FOLL_GET)) ==
377 (FOLL_PIN | FOLL_GET)))
378 return ERR_PTR(-EINVAL);
380 if (unlikely(pmd_bad(*pmd)))
381 return no_page_table(vma, flags);
383 ptep = pte_offset_map_lock(mm, pmd, address, &ptl);
385 if (!pte_present(pte)) {
388 * KSM's break_ksm() relies upon recognizing a ksm page
389 * even while it is being migrated, so for that case we
390 * need migration_entry_wait().
392 if (likely(!(flags & FOLL_MIGRATION)))
396 entry = pte_to_swp_entry(pte);
397 if (!is_migration_entry(entry))
399 pte_unmap_unlock(ptep, ptl);
400 migration_entry_wait(mm, pmd, address);
403 if ((flags & FOLL_NUMA) && pte_protnone(pte))
405 if ((flags & FOLL_WRITE) && !can_follow_write_pte(pte, flags)) {
406 pte_unmap_unlock(ptep, ptl);
410 page = vm_normal_page(vma, address, pte);
411 if (!page && pte_devmap(pte) && (flags & (FOLL_GET | FOLL_PIN))) {
413 * Only return device mapping pages in the FOLL_GET or FOLL_PIN
414 * case since they are only valid while holding the pgmap
417 *pgmap = get_dev_pagemap(pte_pfn(pte), *pgmap);
419 page = pte_page(pte);
422 } else if (unlikely(!page)) {
423 if (flags & FOLL_DUMP) {
424 /* Avoid special (like zero) pages in core dumps */
425 page = ERR_PTR(-EFAULT);
429 if (is_zero_pfn(pte_pfn(pte))) {
430 page = pte_page(pte);
432 ret = follow_pfn_pte(vma, address, ptep, flags);
438 if (flags & FOLL_SPLIT && PageTransCompound(page)) {
440 pte_unmap_unlock(ptep, ptl);
442 ret = split_huge_page(page);
450 /* try_grab_page() does nothing unless FOLL_GET or FOLL_PIN is set. */
451 if (unlikely(!try_grab_page(page, flags))) {
452 page = ERR_PTR(-ENOMEM);
456 * We need to make the page accessible if and only if we are going
457 * to access its content (the FOLL_PIN case). Please see
458 * Documentation/core-api/pin_user_pages.rst for details.
460 if (flags & FOLL_PIN) {
461 ret = arch_make_page_accessible(page);
463 unpin_user_page(page);
468 if (flags & FOLL_TOUCH) {
469 if ((flags & FOLL_WRITE) &&
470 !pte_dirty(pte) && !PageDirty(page))
471 set_page_dirty(page);
473 * pte_mkyoung() would be more correct here, but atomic care
474 * is needed to avoid losing the dirty bit: it is easier to use
475 * mark_page_accessed().
477 mark_page_accessed(page);
479 if ((flags & FOLL_MLOCK) && (vma->vm_flags & VM_LOCKED)) {
480 /* Do not mlock pte-mapped THP */
481 if (PageTransCompound(page))
485 * The preliminary mapping check is mainly to avoid the
486 * pointless overhead of lock_page on the ZERO_PAGE
487 * which might bounce very badly if there is contention.
489 * If the page is already locked, we don't need to
490 * handle it now - vmscan will handle it later if and
491 * when it attempts to reclaim the page.
493 if (page->mapping && trylock_page(page)) {
494 lru_add_drain(); /* push cached pages to LRU */
496 * Because we lock page here, and migration is
497 * blocked by the pte's page reference, and we
498 * know the page is still mapped, we don't even
499 * need to check for file-cache page truncation.
501 mlock_vma_page(page);
506 pte_unmap_unlock(ptep, ptl);
509 pte_unmap_unlock(ptep, ptl);
512 return no_page_table(vma, flags);
515 static struct page *follow_pmd_mask(struct vm_area_struct *vma,
516 unsigned long address, pud_t *pudp,
518 struct follow_page_context *ctx)
523 struct mm_struct *mm = vma->vm_mm;
525 pmd = pmd_offset(pudp, address);
527 * The READ_ONCE() will stabilize the pmdval in a register or
528 * on the stack so that it will stop changing under the code.
530 pmdval = READ_ONCE(*pmd);
531 if (pmd_none(pmdval))
532 return no_page_table(vma, flags);
533 if (pmd_huge(pmdval) && is_vm_hugetlb_page(vma)) {
534 page = follow_huge_pmd(mm, address, pmd, flags);
537 return no_page_table(vma, flags);
539 if (is_hugepd(__hugepd(pmd_val(pmdval)))) {
540 page = follow_huge_pd(vma, address,
541 __hugepd(pmd_val(pmdval)), flags,
545 return no_page_table(vma, flags);
548 if (!pmd_present(pmdval)) {
549 if (likely(!(flags & FOLL_MIGRATION)))
550 return no_page_table(vma, flags);
551 VM_BUG_ON(thp_migration_supported() &&
552 !is_pmd_migration_entry(pmdval));
553 if (is_pmd_migration_entry(pmdval))
554 pmd_migration_entry_wait(mm, pmd);
555 pmdval = READ_ONCE(*pmd);
557 * MADV_DONTNEED may convert the pmd to null because
558 * mmap_lock is held in read mode
560 if (pmd_none(pmdval))
561 return no_page_table(vma, flags);
564 if (pmd_devmap(pmdval)) {
565 ptl = pmd_lock(mm, pmd);
566 page = follow_devmap_pmd(vma, address, pmd, flags, &ctx->pgmap);
571 if (likely(!pmd_trans_huge(pmdval)))
572 return follow_page_pte(vma, address, pmd, flags, &ctx->pgmap);
574 if ((flags & FOLL_NUMA) && pmd_protnone(pmdval))
575 return no_page_table(vma, flags);
578 ptl = pmd_lock(mm, pmd);
579 if (unlikely(pmd_none(*pmd))) {
581 return no_page_table(vma, flags);
583 if (unlikely(!pmd_present(*pmd))) {
585 if (likely(!(flags & FOLL_MIGRATION)))
586 return no_page_table(vma, flags);
587 pmd_migration_entry_wait(mm, pmd);
590 if (unlikely(!pmd_trans_huge(*pmd))) {
592 return follow_page_pte(vma, address, pmd, flags, &ctx->pgmap);
594 if (flags & (FOLL_SPLIT | FOLL_SPLIT_PMD)) {
596 page = pmd_page(*pmd);
597 if (is_huge_zero_page(page)) {
600 split_huge_pmd(vma, pmd, address);
601 if (pmd_trans_unstable(pmd))
603 } else if (flags & FOLL_SPLIT) {
604 if (unlikely(!try_get_page(page))) {
606 return ERR_PTR(-ENOMEM);
610 ret = split_huge_page(page);
614 return no_page_table(vma, flags);
615 } else { /* flags & FOLL_SPLIT_PMD */
617 split_huge_pmd(vma, pmd, address);
618 ret = pte_alloc(mm, pmd) ? -ENOMEM : 0;
621 return ret ? ERR_PTR(ret) :
622 follow_page_pte(vma, address, pmd, flags, &ctx->pgmap);
624 page = follow_trans_huge_pmd(vma, address, pmd, flags);
626 ctx->page_mask = HPAGE_PMD_NR - 1;
630 static struct page *follow_pud_mask(struct vm_area_struct *vma,
631 unsigned long address, p4d_t *p4dp,
633 struct follow_page_context *ctx)
638 struct mm_struct *mm = vma->vm_mm;
640 pud = pud_offset(p4dp, address);
642 return no_page_table(vma, flags);
643 if (pud_huge(*pud) && is_vm_hugetlb_page(vma)) {
644 page = follow_huge_pud(mm, address, pud, flags);
647 return no_page_table(vma, flags);
649 if (is_hugepd(__hugepd(pud_val(*pud)))) {
650 page = follow_huge_pd(vma, address,
651 __hugepd(pud_val(*pud)), flags,
655 return no_page_table(vma, flags);
657 if (pud_devmap(*pud)) {
658 ptl = pud_lock(mm, pud);
659 page = follow_devmap_pud(vma, address, pud, flags, &ctx->pgmap);
664 if (unlikely(pud_bad(*pud)))
665 return no_page_table(vma, flags);
667 return follow_pmd_mask(vma, address, pud, flags, ctx);
670 static struct page *follow_p4d_mask(struct vm_area_struct *vma,
671 unsigned long address, pgd_t *pgdp,
673 struct follow_page_context *ctx)
678 p4d = p4d_offset(pgdp, address);
680 return no_page_table(vma, flags);
681 BUILD_BUG_ON(p4d_huge(*p4d));
682 if (unlikely(p4d_bad(*p4d)))
683 return no_page_table(vma, flags);
685 if (is_hugepd(__hugepd(p4d_val(*p4d)))) {
686 page = follow_huge_pd(vma, address,
687 __hugepd(p4d_val(*p4d)), flags,
691 return no_page_table(vma, flags);
693 return follow_pud_mask(vma, address, p4d, flags, ctx);
697 * follow_page_mask - look up a page descriptor from a user-virtual address
698 * @vma: vm_area_struct mapping @address
699 * @address: virtual address to look up
700 * @flags: flags modifying lookup behaviour
701 * @ctx: contains dev_pagemap for %ZONE_DEVICE memory pinning and a
702 * pointer to output page_mask
704 * @flags can have FOLL_ flags set, defined in <linux/mm.h>
706 * When getting pages from ZONE_DEVICE memory, the @ctx->pgmap caches
707 * the device's dev_pagemap metadata to avoid repeating expensive lookups.
709 * On output, the @ctx->page_mask is set according to the size of the page.
711 * Return: the mapped (struct page *), %NULL if no mapping exists, or
712 * an error pointer if there is a mapping to something not represented
713 * by a page descriptor (see also vm_normal_page()).
715 static struct page *follow_page_mask(struct vm_area_struct *vma,
716 unsigned long address, unsigned int flags,
717 struct follow_page_context *ctx)
721 struct mm_struct *mm = vma->vm_mm;
725 /* make this handle hugepd */
726 page = follow_huge_addr(mm, address, flags & FOLL_WRITE);
728 WARN_ON_ONCE(flags & (FOLL_GET | FOLL_PIN));
732 pgd = pgd_offset(mm, address);
734 if (pgd_none(*pgd) || unlikely(pgd_bad(*pgd)))
735 return no_page_table(vma, flags);
737 if (pgd_huge(*pgd)) {
738 page = follow_huge_pgd(mm, address, pgd, flags);
741 return no_page_table(vma, flags);
743 if (is_hugepd(__hugepd(pgd_val(*pgd)))) {
744 page = follow_huge_pd(vma, address,
745 __hugepd(pgd_val(*pgd)), flags,
749 return no_page_table(vma, flags);
752 return follow_p4d_mask(vma, address, pgd, flags, ctx);
755 struct page *follow_page(struct vm_area_struct *vma, unsigned long address,
756 unsigned int foll_flags)
758 struct follow_page_context ctx = { NULL };
761 page = follow_page_mask(vma, address, foll_flags, &ctx);
763 put_dev_pagemap(ctx.pgmap);
767 static int get_gate_page(struct mm_struct *mm, unsigned long address,
768 unsigned int gup_flags, struct vm_area_struct **vma,
778 /* user gate pages are read-only */
779 if (gup_flags & FOLL_WRITE)
781 if (address > TASK_SIZE)
782 pgd = pgd_offset_k(address);
784 pgd = pgd_offset_gate(mm, address);
787 p4d = p4d_offset(pgd, address);
790 pud = pud_offset(p4d, address);
793 pmd = pmd_offset(pud, address);
794 if (!pmd_present(*pmd))
796 VM_BUG_ON(pmd_trans_huge(*pmd));
797 pte = pte_offset_map(pmd, address);
800 *vma = get_gate_vma(mm);
803 *page = vm_normal_page(*vma, address, *pte);
805 if ((gup_flags & FOLL_DUMP) || !is_zero_pfn(pte_pfn(*pte)))
807 *page = pte_page(*pte);
809 if (unlikely(!try_grab_page(*page, gup_flags))) {
821 * mmap_lock must be held on entry. If @locked != NULL and *@flags
822 * does not include FOLL_NOWAIT, the mmap_lock may be released. If it
823 * is, *@locked will be set to 0 and -EBUSY returned.
825 static int faultin_page(struct vm_area_struct *vma,
826 unsigned long address, unsigned int *flags, int *locked)
828 unsigned int fault_flags = 0;
831 /* mlock all present pages, but do not fault in new pages */
832 if ((*flags & (FOLL_POPULATE | FOLL_MLOCK)) == FOLL_MLOCK)
834 if (*flags & FOLL_WRITE)
835 fault_flags |= FAULT_FLAG_WRITE;
836 if (*flags & FOLL_REMOTE)
837 fault_flags |= FAULT_FLAG_REMOTE;
839 fault_flags |= FAULT_FLAG_ALLOW_RETRY | FAULT_FLAG_KILLABLE;
840 if (*flags & FOLL_NOWAIT)
841 fault_flags |= FAULT_FLAG_ALLOW_RETRY | FAULT_FLAG_RETRY_NOWAIT;
842 if (*flags & FOLL_TRIED) {
844 * Note: FAULT_FLAG_ALLOW_RETRY and FAULT_FLAG_TRIED
847 fault_flags |= FAULT_FLAG_TRIED;
850 ret = handle_mm_fault(vma, address, fault_flags, NULL);
851 if (ret & VM_FAULT_ERROR) {
852 int err = vm_fault_to_errno(ret, *flags);
859 if (ret & VM_FAULT_RETRY) {
860 if (locked && !(fault_flags & FAULT_FLAG_RETRY_NOWAIT))
866 * The VM_FAULT_WRITE bit tells us that do_wp_page has broken COW when
867 * necessary, even if maybe_mkwrite decided not to set pte_write. We
868 * can thus safely do subsequent page lookups as if they were reads.
869 * But only do so when looping for pte_write is futile: in some cases
870 * userspace may also be wanting to write to the gotten user page,
871 * which a read fault here might prevent (a readonly page might get
872 * reCOWed by userspace write).
874 if ((ret & VM_FAULT_WRITE) && !(vma->vm_flags & VM_WRITE))
879 static int check_vma_flags(struct vm_area_struct *vma, unsigned long gup_flags)
881 vm_flags_t vm_flags = vma->vm_flags;
882 int write = (gup_flags & FOLL_WRITE);
883 int foreign = (gup_flags & FOLL_REMOTE);
885 if (vm_flags & (VM_IO | VM_PFNMAP))
888 if (gup_flags & FOLL_ANON && !vma_is_anonymous(vma))
891 if ((gup_flags & FOLL_LONGTERM) && vma_is_fsdax(vma))
895 if (!(vm_flags & VM_WRITE)) {
896 if (!(gup_flags & FOLL_FORCE))
899 * We used to let the write,force case do COW in a
900 * VM_MAYWRITE VM_SHARED !VM_WRITE vma, so ptrace could
901 * set a breakpoint in a read-only mapping of an
902 * executable, without corrupting the file (yet only
903 * when that file had been opened for writing!).
904 * Anon pages in shared mappings are surprising: now
907 if (!is_cow_mapping(vm_flags))
910 } else if (!(vm_flags & VM_READ)) {
911 if (!(gup_flags & FOLL_FORCE))
914 * Is there actually any vma we can reach here which does not
915 * have VM_MAYREAD set?
917 if (!(vm_flags & VM_MAYREAD))
921 * gups are always data accesses, not instruction
922 * fetches, so execute=false here
924 if (!arch_vma_access_permitted(vma, write, false, foreign))
930 * __get_user_pages() - pin user pages in memory
931 * @mm: mm_struct of target mm
932 * @start: starting user address
933 * @nr_pages: number of pages from start to pin
934 * @gup_flags: flags modifying pin behaviour
935 * @pages: array that receives pointers to the pages pinned.
936 * Should be at least nr_pages long. Or NULL, if caller
937 * only intends to ensure the pages are faulted in.
938 * @vmas: array of pointers to vmas corresponding to each page.
939 * Or NULL if the caller does not require them.
940 * @locked: whether we're still with the mmap_lock held
942 * Returns either number of pages pinned (which may be less than the
943 * number requested), or an error. Details about the return value:
945 * -- If nr_pages is 0, returns 0.
946 * -- If nr_pages is >0, but no pages were pinned, returns -errno.
947 * -- If nr_pages is >0, and some pages were pinned, returns the number of
948 * pages pinned. Again, this may be less than nr_pages.
949 * -- 0 return value is possible when the fault would need to be retried.
951 * The caller is responsible for releasing returned @pages, via put_page().
953 * @vmas are valid only as long as mmap_lock is held.
955 * Must be called with mmap_lock held. It may be released. See below.
957 * __get_user_pages walks a process's page tables and takes a reference to
958 * each struct page that each user address corresponds to at a given
959 * instant. That is, it takes the page that would be accessed if a user
960 * thread accesses the given user virtual address at that instant.
962 * This does not guarantee that the page exists in the user mappings when
963 * __get_user_pages returns, and there may even be a completely different
964 * page there in some cases (eg. if mmapped pagecache has been invalidated
965 * and subsequently re faulted). However it does guarantee that the page
966 * won't be freed completely. And mostly callers simply care that the page
967 * contains data that was valid *at some point in time*. Typically, an IO
968 * or similar operation cannot guarantee anything stronger anyway because
969 * locks can't be held over the syscall boundary.
971 * If @gup_flags & FOLL_WRITE == 0, the page must not be written to. If
972 * the page is written to, set_page_dirty (or set_page_dirty_lock, as
973 * appropriate) must be called after the page is finished with, and
974 * before put_page is called.
976 * If @locked != NULL, *@locked will be set to 0 when mmap_lock is
977 * released by an up_read(). That can happen if @gup_flags does not
980 * A caller using such a combination of @locked and @gup_flags
981 * must therefore hold the mmap_lock for reading only, and recognize
982 * when it's been released. Otherwise, it must be held for either
983 * reading or writing and will not be released.
985 * In most cases, get_user_pages or get_user_pages_fast should be used
986 * instead of __get_user_pages. __get_user_pages should be used only if
987 * you need some special @gup_flags.
989 static long __get_user_pages(struct mm_struct *mm,
990 unsigned long start, unsigned long nr_pages,
991 unsigned int gup_flags, struct page **pages,
992 struct vm_area_struct **vmas, int *locked)
995 struct vm_area_struct *vma = NULL;
996 struct follow_page_context ctx = { NULL };
1001 start = untagged_addr(start);
1003 VM_BUG_ON(!!pages != !!(gup_flags & (FOLL_GET | FOLL_PIN)));
1006 * If FOLL_FORCE is set then do not force a full fault as the hinting
1007 * fault information is unrelated to the reference behaviour of a task
1008 * using the address space
1010 if (!(gup_flags & FOLL_FORCE))
1011 gup_flags |= FOLL_NUMA;
1015 unsigned int foll_flags = gup_flags;
1016 unsigned int page_increm;
1018 /* first iteration or cross vma bound */
1019 if (!vma || start >= vma->vm_end) {
1020 vma = find_extend_vma(mm, start);
1021 if (!vma && in_gate_area(mm, start)) {
1022 ret = get_gate_page(mm, start & PAGE_MASK,
1024 pages ? &pages[i] : NULL);
1035 ret = check_vma_flags(vma, gup_flags);
1039 if (is_vm_hugetlb_page(vma)) {
1040 i = follow_hugetlb_page(mm, vma, pages, vmas,
1041 &start, &nr_pages, i,
1043 if (locked && *locked == 0) {
1045 * We've got a VM_FAULT_RETRY
1046 * and we've lost mmap_lock.
1047 * We must stop here.
1049 BUG_ON(gup_flags & FOLL_NOWAIT);
1058 * If we have a pending SIGKILL, don't keep faulting pages and
1059 * potentially allocating memory.
1061 if (fatal_signal_pending(current)) {
1067 page = follow_page_mask(vma, start, foll_flags, &ctx);
1069 ret = faultin_page(vma, start, &foll_flags, locked);
1084 } else if (PTR_ERR(page) == -EEXIST) {
1086 * Proper page table entry exists, but no corresponding
1090 } else if (IS_ERR(page)) {
1091 ret = PTR_ERR(page);
1096 flush_anon_page(vma, page, start);
1097 flush_dcache_page(page);
1105 page_increm = 1 + (~(start >> PAGE_SHIFT) & ctx.page_mask);
1106 if (page_increm > nr_pages)
1107 page_increm = nr_pages;
1109 start += page_increm * PAGE_SIZE;
1110 nr_pages -= page_increm;
1114 put_dev_pagemap(ctx.pgmap);
1118 static bool vma_permits_fault(struct vm_area_struct *vma,
1119 unsigned int fault_flags)
1121 bool write = !!(fault_flags & FAULT_FLAG_WRITE);
1122 bool foreign = !!(fault_flags & FAULT_FLAG_REMOTE);
1123 vm_flags_t vm_flags = write ? VM_WRITE : VM_READ;
1125 if (!(vm_flags & vma->vm_flags))
1129 * The architecture might have a hardware protection
1130 * mechanism other than read/write that can deny access.
1132 * gup always represents data access, not instruction
1133 * fetches, so execute=false here:
1135 if (!arch_vma_access_permitted(vma, write, false, foreign))
1142 * fixup_user_fault() - manually resolve a user page fault
1143 * @mm: mm_struct of target mm
1144 * @address: user address
1145 * @fault_flags:flags to pass down to handle_mm_fault()
1146 * @unlocked: did we unlock the mmap_lock while retrying, maybe NULL if caller
1147 * does not allow retry. If NULL, the caller must guarantee
1148 * that fault_flags does not contain FAULT_FLAG_ALLOW_RETRY.
1150 * This is meant to be called in the specific scenario where for locking reasons
1151 * we try to access user memory in atomic context (within a pagefault_disable()
1152 * section), this returns -EFAULT, and we want to resolve the user fault before
1155 * Typically this is meant to be used by the futex code.
1157 * The main difference with get_user_pages() is that this function will
1158 * unconditionally call handle_mm_fault() which will in turn perform all the
1159 * necessary SW fixup of the dirty and young bits in the PTE, while
1160 * get_user_pages() only guarantees to update these in the struct page.
1162 * This is important for some architectures where those bits also gate the
1163 * access permission to the page because they are maintained in software. On
1164 * such architectures, gup() will not be enough to make a subsequent access
1167 * This function will not return with an unlocked mmap_lock. So it has not the
1168 * same semantics wrt the @mm->mmap_lock as does filemap_fault().
1170 int fixup_user_fault(struct mm_struct *mm,
1171 unsigned long address, unsigned int fault_flags,
1174 struct vm_area_struct *vma;
1175 vm_fault_t ret, major = 0;
1177 address = untagged_addr(address);
1180 fault_flags |= FAULT_FLAG_ALLOW_RETRY | FAULT_FLAG_KILLABLE;
1183 vma = find_extend_vma(mm, address);
1184 if (!vma || address < vma->vm_start)
1187 if (!vma_permits_fault(vma, fault_flags))
1190 if ((fault_flags & FAULT_FLAG_KILLABLE) &&
1191 fatal_signal_pending(current))
1194 ret = handle_mm_fault(vma, address, fault_flags, NULL);
1195 major |= ret & VM_FAULT_MAJOR;
1196 if (ret & VM_FAULT_ERROR) {
1197 int err = vm_fault_to_errno(ret, 0);
1204 if (ret & VM_FAULT_RETRY) {
1207 fault_flags |= FAULT_FLAG_TRIED;
1213 EXPORT_SYMBOL_GPL(fixup_user_fault);
1216 * Please note that this function, unlike __get_user_pages will not
1217 * return 0 for nr_pages > 0 without FOLL_NOWAIT
1219 static __always_inline long __get_user_pages_locked(struct mm_struct *mm,
1220 unsigned long start,
1221 unsigned long nr_pages,
1222 struct page **pages,
1223 struct vm_area_struct **vmas,
1227 long ret, pages_done;
1231 /* if VM_FAULT_RETRY can be returned, vmas become invalid */
1233 /* check caller initialized locked */
1234 BUG_ON(*locked != 1);
1237 if (flags & FOLL_PIN)
1238 atomic_set(&mm->has_pinned, 1);
1241 * FOLL_PIN and FOLL_GET are mutually exclusive. Traditional behavior
1242 * is to set FOLL_GET if the caller wants pages[] filled in (but has
1243 * carelessly failed to specify FOLL_GET), so keep doing that, but only
1244 * for FOLL_GET, not for the newer FOLL_PIN.
1246 * FOLL_PIN always expects pages to be non-null, but no need to assert
1247 * that here, as any failures will be obvious enough.
1249 if (pages && !(flags & FOLL_PIN))
1253 lock_dropped = false;
1255 ret = __get_user_pages(mm, start, nr_pages, flags, pages,
1258 /* VM_FAULT_RETRY couldn't trigger, bypass */
1261 /* VM_FAULT_RETRY cannot return errors */
1264 BUG_ON(ret >= nr_pages);
1275 * VM_FAULT_RETRY didn't trigger or it was a
1283 * VM_FAULT_RETRY triggered, so seek to the faulting offset.
1284 * For the prefault case (!pages) we only update counts.
1288 start += ret << PAGE_SHIFT;
1289 lock_dropped = true;
1293 * Repeat on the address that fired VM_FAULT_RETRY
1294 * with both FAULT_FLAG_ALLOW_RETRY and
1295 * FAULT_FLAG_TRIED. Note that GUP can be interrupted
1296 * by fatal signals, so we need to check it before we
1297 * start trying again otherwise it can loop forever.
1300 if (fatal_signal_pending(current)) {
1302 pages_done = -EINTR;
1306 ret = mmap_read_lock_killable(mm);
1315 ret = __get_user_pages(mm, start, 1, flags | FOLL_TRIED,
1316 pages, NULL, locked);
1318 /* Continue to retry until we succeeded */
1336 if (lock_dropped && *locked) {
1338 * We must let the caller know we temporarily dropped the lock
1339 * and so the critical section protected by it was lost.
1341 mmap_read_unlock(mm);
1348 * populate_vma_page_range() - populate a range of pages in the vma.
1350 * @start: start address
1352 * @locked: whether the mmap_lock is still held
1354 * This takes care of mlocking the pages too if VM_LOCKED is set.
1356 * Return either number of pages pinned in the vma, or a negative error
1359 * vma->vm_mm->mmap_lock must be held.
1361 * If @locked is NULL, it may be held for read or write and will
1364 * If @locked is non-NULL, it must held for read only and may be
1365 * released. If it's released, *@locked will be set to 0.
1367 long populate_vma_page_range(struct vm_area_struct *vma,
1368 unsigned long start, unsigned long end, int *locked)
1370 struct mm_struct *mm = vma->vm_mm;
1371 unsigned long nr_pages = (end - start) / PAGE_SIZE;
1374 VM_BUG_ON(start & ~PAGE_MASK);
1375 VM_BUG_ON(end & ~PAGE_MASK);
1376 VM_BUG_ON_VMA(start < vma->vm_start, vma);
1377 VM_BUG_ON_VMA(end > vma->vm_end, vma);
1378 mmap_assert_locked(mm);
1380 gup_flags = FOLL_TOUCH | FOLL_POPULATE | FOLL_MLOCK;
1381 if (vma->vm_flags & VM_LOCKONFAULT)
1382 gup_flags &= ~FOLL_POPULATE;
1384 * We want to touch writable mappings with a write fault in order
1385 * to break COW, except for shared mappings because these don't COW
1386 * and we would not want to dirty them for nothing.
1388 if ((vma->vm_flags & (VM_WRITE | VM_SHARED)) == VM_WRITE)
1389 gup_flags |= FOLL_WRITE;
1392 * We want mlock to succeed for regions that have any permissions
1393 * other than PROT_NONE.
1395 if (vma_is_accessible(vma))
1396 gup_flags |= FOLL_FORCE;
1399 * We made sure addr is within a VMA, so the following will
1400 * not result in a stack expansion that recurses back here.
1402 return __get_user_pages(mm, start, nr_pages, gup_flags,
1403 NULL, NULL, locked);
1407 * __mm_populate - populate and/or mlock pages within a range of address space.
1409 * This is used to implement mlock() and the MAP_POPULATE / MAP_LOCKED mmap
1410 * flags. VMAs must be already marked with the desired vm_flags, and
1411 * mmap_lock must not be held.
1413 int __mm_populate(unsigned long start, unsigned long len, int ignore_errors)
1415 struct mm_struct *mm = current->mm;
1416 unsigned long end, nstart, nend;
1417 struct vm_area_struct *vma = NULL;
1423 for (nstart = start; nstart < end; nstart = nend) {
1425 * We want to fault in pages for [nstart; end) address range.
1426 * Find first corresponding VMA.
1431 vma = find_vma(mm, nstart);
1432 } else if (nstart >= vma->vm_end)
1434 if (!vma || vma->vm_start >= end)
1437 * Set [nstart; nend) to intersection of desired address
1438 * range with the first VMA. Also, skip undesirable VMA types.
1440 nend = min(end, vma->vm_end);
1441 if (vma->vm_flags & (VM_IO | VM_PFNMAP))
1443 if (nstart < vma->vm_start)
1444 nstart = vma->vm_start;
1446 * Now fault in a range of pages. populate_vma_page_range()
1447 * double checks the vma flags, so that it won't mlock pages
1448 * if the vma was already munlocked.
1450 ret = populate_vma_page_range(vma, nstart, nend, &locked);
1452 if (ignore_errors) {
1454 continue; /* continue at next VMA */
1458 nend = nstart + ret * PAGE_SIZE;
1462 mmap_read_unlock(mm);
1463 return ret; /* 0 or negative error code */
1465 #else /* CONFIG_MMU */
1466 static long __get_user_pages_locked(struct mm_struct *mm, unsigned long start,
1467 unsigned long nr_pages, struct page **pages,
1468 struct vm_area_struct **vmas, int *locked,
1469 unsigned int foll_flags)
1471 struct vm_area_struct *vma;
1472 unsigned long vm_flags;
1475 /* calculate required read or write permissions.
1476 * If FOLL_FORCE is set, we only require the "MAY" flags.
1478 vm_flags = (foll_flags & FOLL_WRITE) ?
1479 (VM_WRITE | VM_MAYWRITE) : (VM_READ | VM_MAYREAD);
1480 vm_flags &= (foll_flags & FOLL_FORCE) ?
1481 (VM_MAYREAD | VM_MAYWRITE) : (VM_READ | VM_WRITE);
1483 for (i = 0; i < nr_pages; i++) {
1484 vma = find_vma(mm, start);
1486 goto finish_or_fault;
1488 /* protect what we can, including chardevs */
1489 if ((vma->vm_flags & (VM_IO | VM_PFNMAP)) ||
1490 !(vm_flags & vma->vm_flags))
1491 goto finish_or_fault;
1494 pages[i] = virt_to_page(start);
1500 start = (start + PAGE_SIZE) & PAGE_MASK;
1506 return i ? : -EFAULT;
1508 #endif /* !CONFIG_MMU */
1511 * get_dump_page() - pin user page in memory while writing it to core dump
1512 * @addr: user address
1514 * Returns struct page pointer of user page pinned for dump,
1515 * to be freed afterwards by put_page().
1517 * Returns NULL on any kind of failure - a hole must then be inserted into
1518 * the corefile, to preserve alignment with its headers; and also returns
1519 * NULL wherever the ZERO_PAGE, or an anonymous pte_none, has been found -
1520 * allowing a hole to be left in the corefile to save diskspace.
1522 * Called without mmap_lock (takes and releases the mmap_lock by itself).
1524 #ifdef CONFIG_ELF_CORE
1525 struct page *get_dump_page(unsigned long addr)
1527 struct mm_struct *mm = current->mm;
1532 if (mmap_read_lock_killable(mm))
1534 ret = __get_user_pages_locked(mm, addr, 1, &page, NULL, &locked,
1535 FOLL_FORCE | FOLL_DUMP | FOLL_GET);
1537 mmap_read_unlock(mm);
1538 return (ret == 1) ? page : NULL;
1540 #endif /* CONFIG_ELF_CORE */
1543 static long check_and_migrate_cma_pages(struct mm_struct *mm,
1544 unsigned long start,
1545 unsigned long nr_pages,
1546 struct page **pages,
1547 struct vm_area_struct **vmas,
1548 unsigned int gup_flags)
1552 bool drain_allow = true;
1553 bool migrate_allow = true;
1554 LIST_HEAD(cma_page_list);
1555 long ret = nr_pages;
1556 struct migration_target_control mtc = {
1557 .nid = NUMA_NO_NODE,
1558 .gfp_mask = GFP_USER | __GFP_MOVABLE | __GFP_NOWARN,
1562 for (i = 0; i < nr_pages;) {
1564 struct page *head = compound_head(pages[i]);
1567 * gup may start from a tail page. Advance step by the left
1570 step = compound_nr(head) - (pages[i] - head);
1572 * If we get a page from the CMA zone, since we are going to
1573 * be pinning these entries, we might as well move them out
1574 * of the CMA zone if possible.
1576 if (is_migrate_cma_page(head)) {
1578 isolate_huge_page(head, &cma_page_list);
1580 if (!PageLRU(head) && drain_allow) {
1581 lru_add_drain_all();
1582 drain_allow = false;
1585 if (!isolate_lru_page(head)) {
1586 list_add_tail(&head->lru, &cma_page_list);
1587 mod_node_page_state(page_pgdat(head),
1589 page_is_file_lru(head),
1590 thp_nr_pages(head));
1598 if (!list_empty(&cma_page_list)) {
1600 * drop the above get_user_pages reference.
1602 if (gup_flags & FOLL_PIN)
1603 unpin_user_pages(pages, nr_pages);
1605 for (i = 0; i < nr_pages; i++)
1608 if (migrate_pages(&cma_page_list, alloc_migration_target, NULL,
1609 (unsigned long)&mtc, MIGRATE_SYNC, MR_CONTIG_RANGE)) {
1611 * some of the pages failed migration. Do get_user_pages
1612 * without migration.
1614 migrate_allow = false;
1616 if (!list_empty(&cma_page_list))
1617 putback_movable_pages(&cma_page_list);
1620 * We did migrate all the pages, Try to get the page references
1621 * again migrating any new CMA pages which we failed to isolate
1624 ret = __get_user_pages_locked(mm, start, nr_pages,
1628 if ((ret > 0) && migrate_allow) {
1638 static long check_and_migrate_cma_pages(struct mm_struct *mm,
1639 unsigned long start,
1640 unsigned long nr_pages,
1641 struct page **pages,
1642 struct vm_area_struct **vmas,
1643 unsigned int gup_flags)
1647 #endif /* CONFIG_CMA */
1650 * __gup_longterm_locked() is a wrapper for __get_user_pages_locked which
1651 * allows us to process the FOLL_LONGTERM flag.
1653 static long __gup_longterm_locked(struct mm_struct *mm,
1654 unsigned long start,
1655 unsigned long nr_pages,
1656 struct page **pages,
1657 struct vm_area_struct **vmas,
1658 unsigned int gup_flags)
1660 unsigned long flags = 0;
1663 if (gup_flags & FOLL_LONGTERM)
1664 flags = memalloc_nocma_save();
1666 rc = __get_user_pages_locked(mm, start, nr_pages, pages, vmas, NULL,
1669 if (gup_flags & FOLL_LONGTERM) {
1671 rc = check_and_migrate_cma_pages(mm, start, rc, pages,
1673 memalloc_nocma_restore(flags);
1678 static bool is_valid_gup_flags(unsigned int gup_flags)
1681 * FOLL_PIN must only be set internally by the pin_user_pages*() APIs,
1682 * never directly by the caller, so enforce that with an assertion:
1684 if (WARN_ON_ONCE(gup_flags & FOLL_PIN))
1687 * FOLL_PIN is a prerequisite to FOLL_LONGTERM. Another way of saying
1688 * that is, FOLL_LONGTERM is a specific case, more restrictive case of
1691 if (WARN_ON_ONCE(gup_flags & FOLL_LONGTERM))
1698 static long __get_user_pages_remote(struct mm_struct *mm,
1699 unsigned long start, unsigned long nr_pages,
1700 unsigned int gup_flags, struct page **pages,
1701 struct vm_area_struct **vmas, int *locked)
1704 * Parts of FOLL_LONGTERM behavior are incompatible with
1705 * FAULT_FLAG_ALLOW_RETRY because of the FS DAX check requirement on
1706 * vmas. However, this only comes up if locked is set, and there are
1707 * callers that do request FOLL_LONGTERM, but do not set locked. So,
1708 * allow what we can.
1710 if (gup_flags & FOLL_LONGTERM) {
1711 if (WARN_ON_ONCE(locked))
1714 * This will check the vmas (even if our vmas arg is NULL)
1715 * and return -ENOTSUPP if DAX isn't allowed in this case:
1717 return __gup_longterm_locked(mm, start, nr_pages, pages,
1718 vmas, gup_flags | FOLL_TOUCH |
1722 return __get_user_pages_locked(mm, start, nr_pages, pages, vmas,
1724 gup_flags | FOLL_TOUCH | FOLL_REMOTE);
1728 * get_user_pages_remote() - pin user pages in memory
1729 * @mm: mm_struct of target mm
1730 * @start: starting user address
1731 * @nr_pages: number of pages from start to pin
1732 * @gup_flags: flags modifying lookup behaviour
1733 * @pages: array that receives pointers to the pages pinned.
1734 * Should be at least nr_pages long. Or NULL, if caller
1735 * only intends to ensure the pages are faulted in.
1736 * @vmas: array of pointers to vmas corresponding to each page.
1737 * Or NULL if the caller does not require them.
1738 * @locked: pointer to lock flag indicating whether lock is held and
1739 * subsequently whether VM_FAULT_RETRY functionality can be
1740 * utilised. Lock must initially be held.
1742 * Returns either number of pages pinned (which may be less than the
1743 * number requested), or an error. Details about the return value:
1745 * -- If nr_pages is 0, returns 0.
1746 * -- If nr_pages is >0, but no pages were pinned, returns -errno.
1747 * -- If nr_pages is >0, and some pages were pinned, returns the number of
1748 * pages pinned. Again, this may be less than nr_pages.
1750 * The caller is responsible for releasing returned @pages, via put_page().
1752 * @vmas are valid only as long as mmap_lock is held.
1754 * Must be called with mmap_lock held for read or write.
1756 * get_user_pages_remote walks a process's page tables and takes a reference
1757 * to each struct page that each user address corresponds to at a given
1758 * instant. That is, it takes the page that would be accessed if a user
1759 * thread accesses the given user virtual address at that instant.
1761 * This does not guarantee that the page exists in the user mappings when
1762 * get_user_pages_remote returns, and there may even be a completely different
1763 * page there in some cases (eg. if mmapped pagecache has been invalidated
1764 * and subsequently re faulted). However it does guarantee that the page
1765 * won't be freed completely. And mostly callers simply care that the page
1766 * contains data that was valid *at some point in time*. Typically, an IO
1767 * or similar operation cannot guarantee anything stronger anyway because
1768 * locks can't be held over the syscall boundary.
1770 * If gup_flags & FOLL_WRITE == 0, the page must not be written to. If the page
1771 * is written to, set_page_dirty (or set_page_dirty_lock, as appropriate) must
1772 * be called after the page is finished with, and before put_page is called.
1774 * get_user_pages_remote is typically used for fewer-copy IO operations,
1775 * to get a handle on the memory by some means other than accesses
1776 * via the user virtual addresses. The pages may be submitted for
1777 * DMA to devices or accessed via their kernel linear mapping (via the
1778 * kmap APIs). Care should be taken to use the correct cache flushing APIs.
1780 * See also get_user_pages_fast, for performance critical applications.
1782 * get_user_pages_remote should be phased out in favor of
1783 * get_user_pages_locked|unlocked or get_user_pages_fast. Nothing
1784 * should use get_user_pages_remote because it cannot pass
1785 * FAULT_FLAG_ALLOW_RETRY to handle_mm_fault.
1787 long get_user_pages_remote(struct mm_struct *mm,
1788 unsigned long start, unsigned long nr_pages,
1789 unsigned int gup_flags, struct page **pages,
1790 struct vm_area_struct **vmas, int *locked)
1792 if (!is_valid_gup_flags(gup_flags))
1795 return __get_user_pages_remote(mm, start, nr_pages, gup_flags,
1796 pages, vmas, locked);
1798 EXPORT_SYMBOL(get_user_pages_remote);
1800 #else /* CONFIG_MMU */
1801 long get_user_pages_remote(struct mm_struct *mm,
1802 unsigned long start, unsigned long nr_pages,
1803 unsigned int gup_flags, struct page **pages,
1804 struct vm_area_struct **vmas, int *locked)
1809 static long __get_user_pages_remote(struct mm_struct *mm,
1810 unsigned long start, unsigned long nr_pages,
1811 unsigned int gup_flags, struct page **pages,
1812 struct vm_area_struct **vmas, int *locked)
1816 #endif /* !CONFIG_MMU */
1819 * get_user_pages() - pin user pages in memory
1820 * @start: starting user address
1821 * @nr_pages: number of pages from start to pin
1822 * @gup_flags: flags modifying lookup behaviour
1823 * @pages: array that receives pointers to the pages pinned.
1824 * Should be at least nr_pages long. Or NULL, if caller
1825 * only intends to ensure the pages are faulted in.
1826 * @vmas: array of pointers to vmas corresponding to each page.
1827 * Or NULL if the caller does not require them.
1829 * This is the same as get_user_pages_remote(), just with a less-flexible
1830 * calling convention where we assume that the mm being operated on belongs to
1831 * the current task, and doesn't allow passing of a locked parameter. We also
1832 * obviously don't pass FOLL_REMOTE in here.
1834 long get_user_pages(unsigned long start, unsigned long nr_pages,
1835 unsigned int gup_flags, struct page **pages,
1836 struct vm_area_struct **vmas)
1838 if (!is_valid_gup_flags(gup_flags))
1841 return __gup_longterm_locked(current->mm, start, nr_pages,
1842 pages, vmas, gup_flags | FOLL_TOUCH);
1844 EXPORT_SYMBOL(get_user_pages);
1847 * get_user_pages_locked() - variant of get_user_pages()
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 * @locked: pointer to lock flag indicating whether lock is held and
1856 * subsequently whether VM_FAULT_RETRY functionality can be
1857 * utilised. Lock must initially be held.
1859 * It is suitable to replace the form:
1861 * mmap_read_lock(mm);
1863 * get_user_pages(mm, ..., pages, NULL);
1864 * mmap_read_unlock(mm);
1869 * mmap_read_lock(mm);
1871 * get_user_pages_locked(mm, ..., pages, &locked);
1873 * mmap_read_unlock(mm);
1875 * We can leverage the VM_FAULT_RETRY functionality in the page fault
1876 * paths better by using either get_user_pages_locked() or
1877 * get_user_pages_unlocked().
1880 long get_user_pages_locked(unsigned long start, unsigned long nr_pages,
1881 unsigned int gup_flags, struct page **pages,
1885 * FIXME: Current FOLL_LONGTERM behavior is incompatible with
1886 * FAULT_FLAG_ALLOW_RETRY because of the FS DAX check requirement on
1887 * vmas. As there are no users of this flag in this call we simply
1888 * disallow this option for now.
1890 if (WARN_ON_ONCE(gup_flags & FOLL_LONGTERM))
1893 * FOLL_PIN must only be set internally by the pin_user_pages*() APIs,
1894 * never directly by the caller, so enforce that:
1896 if (WARN_ON_ONCE(gup_flags & FOLL_PIN))
1899 return __get_user_pages_locked(current->mm, start, nr_pages,
1900 pages, NULL, locked,
1901 gup_flags | FOLL_TOUCH);
1903 EXPORT_SYMBOL(get_user_pages_locked);
1906 * get_user_pages_unlocked() is suitable to replace the form:
1908 * mmap_read_lock(mm);
1909 * get_user_pages(mm, ..., pages, NULL);
1910 * mmap_read_unlock(mm);
1914 * get_user_pages_unlocked(mm, ..., pages);
1916 * It is functionally equivalent to get_user_pages_fast so
1917 * get_user_pages_fast should be used instead if specific gup_flags
1918 * (e.g. FOLL_FORCE) are not required.
1920 long get_user_pages_unlocked(unsigned long start, unsigned long nr_pages,
1921 struct page **pages, unsigned int gup_flags)
1923 struct mm_struct *mm = current->mm;
1928 * FIXME: Current FOLL_LONGTERM behavior is incompatible with
1929 * FAULT_FLAG_ALLOW_RETRY because of the FS DAX check requirement on
1930 * vmas. As there are no users of this flag in this call we simply
1931 * disallow this option for now.
1933 if (WARN_ON_ONCE(gup_flags & FOLL_LONGTERM))
1937 ret = __get_user_pages_locked(mm, start, nr_pages, pages, NULL,
1938 &locked, gup_flags | FOLL_TOUCH);
1940 mmap_read_unlock(mm);
1943 EXPORT_SYMBOL(get_user_pages_unlocked);
1948 * get_user_pages_fast attempts to pin user pages by walking the page
1949 * tables directly and avoids taking locks. Thus the walker needs to be
1950 * protected from page table pages being freed from under it, and should
1951 * block any THP splits.
1953 * One way to achieve this is to have the walker disable interrupts, and
1954 * rely on IPIs from the TLB flushing code blocking before the page table
1955 * pages are freed. This is unsuitable for architectures that do not need
1956 * to broadcast an IPI when invalidating TLBs.
1958 * Another way to achieve this is to batch up page table containing pages
1959 * belonging to more than one mm_user, then rcu_sched a callback to free those
1960 * pages. Disabling interrupts will allow the fast_gup walker to both block
1961 * the rcu_sched callback, and an IPI that we broadcast for splitting THPs
1962 * (which is a relatively rare event). The code below adopts this strategy.
1964 * Before activating this code, please be aware that the following assumptions
1965 * are currently made:
1967 * *) Either MMU_GATHER_RCU_TABLE_FREE is enabled, and tlb_remove_table() is used to
1968 * free pages containing page tables or TLB flushing requires IPI broadcast.
1970 * *) ptes can be read atomically by the architecture.
1972 * *) access_ok is sufficient to validate userspace address ranges.
1974 * The last two assumptions can be relaxed by the addition of helper functions.
1976 * This code is based heavily on the PowerPC implementation by Nick Piggin.
1978 #ifdef CONFIG_HAVE_FAST_GUP
1980 static void __maybe_unused undo_dev_pagemap(int *nr, int nr_start,
1982 struct page **pages)
1984 while ((*nr) - nr_start) {
1985 struct page *page = pages[--(*nr)];
1987 ClearPageReferenced(page);
1988 if (flags & FOLL_PIN)
1989 unpin_user_page(page);
1995 #ifdef CONFIG_ARCH_HAS_PTE_SPECIAL
1996 static int gup_pte_range(pmd_t pmd, unsigned long addr, unsigned long end,
1997 unsigned int flags, struct page **pages, int *nr)
1999 struct dev_pagemap *pgmap = NULL;
2000 int nr_start = *nr, ret = 0;
2003 ptem = ptep = pte_offset_map(&pmd, addr);
2005 pte_t pte = ptep_get_lockless(ptep);
2006 struct page *head, *page;
2009 * Similar to the PMD case below, NUMA hinting must take slow
2010 * path using the pte_protnone check.
2012 if (pte_protnone(pte))
2015 if (!pte_access_permitted(pte, flags & FOLL_WRITE))
2018 if (pte_devmap(pte)) {
2019 if (unlikely(flags & FOLL_LONGTERM))
2022 pgmap = get_dev_pagemap(pte_pfn(pte), pgmap);
2023 if (unlikely(!pgmap)) {
2024 undo_dev_pagemap(nr, nr_start, flags, pages);
2027 } else if (pte_special(pte))
2030 VM_BUG_ON(!pfn_valid(pte_pfn(pte)));
2031 page = pte_page(pte);
2033 head = try_grab_compound_head(page, 1, flags);
2037 if (unlikely(pte_val(pte) != pte_val(*ptep))) {
2038 put_compound_head(head, 1, flags);
2042 VM_BUG_ON_PAGE(compound_head(page) != head, page);
2045 * We need to make the page accessible if and only if we are
2046 * going to access its content (the FOLL_PIN case). Please
2047 * see Documentation/core-api/pin_user_pages.rst for
2050 if (flags & FOLL_PIN) {
2051 ret = arch_make_page_accessible(page);
2053 unpin_user_page(page);
2057 SetPageReferenced(page);
2061 } while (ptep++, addr += PAGE_SIZE, addr != end);
2067 put_dev_pagemap(pgmap);
2074 * If we can't determine whether or not a pte is special, then fail immediately
2075 * for ptes. Note, we can still pin HugeTLB and THP as these are guaranteed not
2078 * For a futex to be placed on a THP tail page, get_futex_key requires a
2079 * get_user_pages_fast_only implementation that can pin pages. Thus it's still
2080 * useful to have gup_huge_pmd even if we can't operate on ptes.
2082 static int gup_pte_range(pmd_t pmd, unsigned long addr, unsigned long end,
2083 unsigned int flags, struct page **pages, int *nr)
2087 #endif /* CONFIG_ARCH_HAS_PTE_SPECIAL */
2089 #if defined(CONFIG_ARCH_HAS_PTE_DEVMAP) && defined(CONFIG_TRANSPARENT_HUGEPAGE)
2090 static int __gup_device_huge(unsigned long pfn, unsigned long addr,
2091 unsigned long end, unsigned int flags,
2092 struct page **pages, int *nr)
2095 struct dev_pagemap *pgmap = NULL;
2098 struct page *page = pfn_to_page(pfn);
2100 pgmap = get_dev_pagemap(pfn, pgmap);
2101 if (unlikely(!pgmap)) {
2102 undo_dev_pagemap(nr, nr_start, flags, pages);
2105 SetPageReferenced(page);
2107 if (unlikely(!try_grab_page(page, flags))) {
2108 undo_dev_pagemap(nr, nr_start, flags, pages);
2113 } while (addr += PAGE_SIZE, addr != end);
2116 put_dev_pagemap(pgmap);
2120 static int __gup_device_huge_pmd(pmd_t orig, pmd_t *pmdp, unsigned long addr,
2121 unsigned long end, unsigned int flags,
2122 struct page **pages, int *nr)
2124 unsigned long fault_pfn;
2127 fault_pfn = pmd_pfn(orig) + ((addr & ~PMD_MASK) >> PAGE_SHIFT);
2128 if (!__gup_device_huge(fault_pfn, addr, end, flags, pages, nr))
2131 if (unlikely(pmd_val(orig) != pmd_val(*pmdp))) {
2132 undo_dev_pagemap(nr, nr_start, flags, pages);
2138 static int __gup_device_huge_pud(pud_t orig, pud_t *pudp, unsigned long addr,
2139 unsigned long end, unsigned int flags,
2140 struct page **pages, int *nr)
2142 unsigned long fault_pfn;
2145 fault_pfn = pud_pfn(orig) + ((addr & ~PUD_MASK) >> PAGE_SHIFT);
2146 if (!__gup_device_huge(fault_pfn, addr, end, flags, pages, nr))
2149 if (unlikely(pud_val(orig) != pud_val(*pudp))) {
2150 undo_dev_pagemap(nr, nr_start, flags, pages);
2156 static int __gup_device_huge_pmd(pmd_t orig, pmd_t *pmdp, unsigned long addr,
2157 unsigned long end, unsigned int flags,
2158 struct page **pages, int *nr)
2164 static int __gup_device_huge_pud(pud_t pud, pud_t *pudp, unsigned long addr,
2165 unsigned long end, unsigned int flags,
2166 struct page **pages, int *nr)
2173 static int record_subpages(struct page *page, unsigned long addr,
2174 unsigned long end, struct page **pages)
2178 for (nr = 0; addr != end; addr += PAGE_SIZE)
2179 pages[nr++] = page++;
2184 #ifdef CONFIG_ARCH_HAS_HUGEPD
2185 static unsigned long hugepte_addr_end(unsigned long addr, unsigned long end,
2188 unsigned long __boundary = (addr + sz) & ~(sz-1);
2189 return (__boundary - 1 < end - 1) ? __boundary : end;
2192 static int gup_hugepte(pte_t *ptep, unsigned long sz, unsigned long addr,
2193 unsigned long end, unsigned int flags,
2194 struct page **pages, int *nr)
2196 unsigned long pte_end;
2197 struct page *head, *page;
2201 pte_end = (addr + sz) & ~(sz-1);
2205 pte = huge_ptep_get(ptep);
2207 if (!pte_access_permitted(pte, flags & FOLL_WRITE))
2210 /* hugepages are never "special" */
2211 VM_BUG_ON(!pfn_valid(pte_pfn(pte)));
2213 head = pte_page(pte);
2214 page = head + ((addr & (sz-1)) >> PAGE_SHIFT);
2215 refs = record_subpages(page, addr, end, pages + *nr);
2217 head = try_grab_compound_head(head, refs, flags);
2221 if (unlikely(pte_val(pte) != pte_val(*ptep))) {
2222 put_compound_head(head, refs, flags);
2227 SetPageReferenced(head);
2231 static int gup_huge_pd(hugepd_t hugepd, unsigned long addr,
2232 unsigned int pdshift, unsigned long end, unsigned int flags,
2233 struct page **pages, int *nr)
2236 unsigned long sz = 1UL << hugepd_shift(hugepd);
2239 ptep = hugepte_offset(hugepd, addr, pdshift);
2241 next = hugepte_addr_end(addr, end, sz);
2242 if (!gup_hugepte(ptep, sz, addr, end, flags, pages, nr))
2244 } while (ptep++, addr = next, addr != end);
2249 static inline int gup_huge_pd(hugepd_t hugepd, unsigned long addr,
2250 unsigned int pdshift, unsigned long end, unsigned int flags,
2251 struct page **pages, int *nr)
2255 #endif /* CONFIG_ARCH_HAS_HUGEPD */
2257 static int gup_huge_pmd(pmd_t orig, pmd_t *pmdp, unsigned long addr,
2258 unsigned long end, unsigned int flags,
2259 struct page **pages, int *nr)
2261 struct page *head, *page;
2264 if (!pmd_access_permitted(orig, flags & FOLL_WRITE))
2267 if (pmd_devmap(orig)) {
2268 if (unlikely(flags & FOLL_LONGTERM))
2270 return __gup_device_huge_pmd(orig, pmdp, addr, end, flags,
2274 page = pmd_page(orig) + ((addr & ~PMD_MASK) >> PAGE_SHIFT);
2275 refs = record_subpages(page, addr, end, pages + *nr);
2277 head = try_grab_compound_head(pmd_page(orig), refs, flags);
2281 if (unlikely(pmd_val(orig) != pmd_val(*pmdp))) {
2282 put_compound_head(head, refs, flags);
2287 SetPageReferenced(head);
2291 static int gup_huge_pud(pud_t orig, pud_t *pudp, unsigned long addr,
2292 unsigned long end, unsigned int flags,
2293 struct page **pages, int *nr)
2295 struct page *head, *page;
2298 if (!pud_access_permitted(orig, flags & FOLL_WRITE))
2301 if (pud_devmap(orig)) {
2302 if (unlikely(flags & FOLL_LONGTERM))
2304 return __gup_device_huge_pud(orig, pudp, addr, end, flags,
2308 page = pud_page(orig) + ((addr & ~PUD_MASK) >> PAGE_SHIFT);
2309 refs = record_subpages(page, addr, end, pages + *nr);
2311 head = try_grab_compound_head(pud_page(orig), refs, flags);
2315 if (unlikely(pud_val(orig) != pud_val(*pudp))) {
2316 put_compound_head(head, refs, flags);
2321 SetPageReferenced(head);
2325 static int gup_huge_pgd(pgd_t orig, pgd_t *pgdp, unsigned long addr,
2326 unsigned long end, unsigned int flags,
2327 struct page **pages, int *nr)
2330 struct page *head, *page;
2332 if (!pgd_access_permitted(orig, flags & FOLL_WRITE))
2335 BUILD_BUG_ON(pgd_devmap(orig));
2337 page = pgd_page(orig) + ((addr & ~PGDIR_MASK) >> PAGE_SHIFT);
2338 refs = record_subpages(page, addr, end, pages + *nr);
2340 head = try_grab_compound_head(pgd_page(orig), refs, flags);
2344 if (unlikely(pgd_val(orig) != pgd_val(*pgdp))) {
2345 put_compound_head(head, refs, flags);
2350 SetPageReferenced(head);
2354 static int gup_pmd_range(pud_t *pudp, pud_t pud, unsigned long addr, unsigned long end,
2355 unsigned int flags, struct page **pages, int *nr)
2360 pmdp = pmd_offset_lockless(pudp, pud, addr);
2362 pmd_t pmd = READ_ONCE(*pmdp);
2364 next = pmd_addr_end(addr, end);
2365 if (!pmd_present(pmd))
2368 if (unlikely(pmd_trans_huge(pmd) || pmd_huge(pmd) ||
2371 * NUMA hinting faults need to be handled in the GUP
2372 * slowpath for accounting purposes and so that they
2373 * can be serialised against THP migration.
2375 if (pmd_protnone(pmd))
2378 if (!gup_huge_pmd(pmd, pmdp, addr, next, flags,
2382 } else if (unlikely(is_hugepd(__hugepd(pmd_val(pmd))))) {
2384 * architecture have different format for hugetlbfs
2385 * pmd format and THP pmd format
2387 if (!gup_huge_pd(__hugepd(pmd_val(pmd)), addr,
2388 PMD_SHIFT, next, flags, pages, nr))
2390 } else if (!gup_pte_range(pmd, addr, next, flags, pages, nr))
2392 } while (pmdp++, addr = next, addr != end);
2397 static int gup_pud_range(p4d_t *p4dp, p4d_t p4d, unsigned long addr, unsigned long end,
2398 unsigned int flags, struct page **pages, int *nr)
2403 pudp = pud_offset_lockless(p4dp, p4d, addr);
2405 pud_t pud = READ_ONCE(*pudp);
2407 next = pud_addr_end(addr, end);
2408 if (unlikely(!pud_present(pud)))
2410 if (unlikely(pud_huge(pud))) {
2411 if (!gup_huge_pud(pud, pudp, addr, next, flags,
2414 } else if (unlikely(is_hugepd(__hugepd(pud_val(pud))))) {
2415 if (!gup_huge_pd(__hugepd(pud_val(pud)), addr,
2416 PUD_SHIFT, next, flags, pages, nr))
2418 } else if (!gup_pmd_range(pudp, pud, addr, next, flags, pages, nr))
2420 } while (pudp++, addr = next, addr != end);
2425 static int gup_p4d_range(pgd_t *pgdp, pgd_t pgd, unsigned long addr, unsigned long end,
2426 unsigned int flags, struct page **pages, int *nr)
2431 p4dp = p4d_offset_lockless(pgdp, pgd, addr);
2433 p4d_t p4d = READ_ONCE(*p4dp);
2435 next = p4d_addr_end(addr, end);
2438 BUILD_BUG_ON(p4d_huge(p4d));
2439 if (unlikely(is_hugepd(__hugepd(p4d_val(p4d))))) {
2440 if (!gup_huge_pd(__hugepd(p4d_val(p4d)), addr,
2441 P4D_SHIFT, next, flags, pages, nr))
2443 } else if (!gup_pud_range(p4dp, p4d, addr, next, flags, pages, nr))
2445 } while (p4dp++, addr = next, addr != end);
2450 static void gup_pgd_range(unsigned long addr, unsigned long end,
2451 unsigned int flags, struct page **pages, int *nr)
2456 pgdp = pgd_offset(current->mm, addr);
2458 pgd_t pgd = READ_ONCE(*pgdp);
2460 next = pgd_addr_end(addr, end);
2463 if (unlikely(pgd_huge(pgd))) {
2464 if (!gup_huge_pgd(pgd, pgdp, addr, next, flags,
2467 } else if (unlikely(is_hugepd(__hugepd(pgd_val(pgd))))) {
2468 if (!gup_huge_pd(__hugepd(pgd_val(pgd)), addr,
2469 PGDIR_SHIFT, next, flags, pages, nr))
2471 } else if (!gup_p4d_range(pgdp, pgd, addr, next, flags, pages, nr))
2473 } while (pgdp++, addr = next, addr != end);
2476 static inline void gup_pgd_range(unsigned long addr, unsigned long end,
2477 unsigned int flags, struct page **pages, int *nr)
2480 #endif /* CONFIG_HAVE_FAST_GUP */
2482 #ifndef gup_fast_permitted
2484 * Check if it's allowed to use get_user_pages_fast_only() for the range, or
2485 * we need to fall back to the slow version:
2487 static bool gup_fast_permitted(unsigned long start, unsigned long end)
2493 static int __gup_longterm_unlocked(unsigned long start, int nr_pages,
2494 unsigned int gup_flags, struct page **pages)
2499 * FIXME: FOLL_LONGTERM does not work with
2500 * get_user_pages_unlocked() (see comments in that function)
2502 if (gup_flags & FOLL_LONGTERM) {
2503 mmap_read_lock(current->mm);
2504 ret = __gup_longterm_locked(current->mm,
2506 pages, NULL, gup_flags);
2507 mmap_read_unlock(current->mm);
2509 ret = get_user_pages_unlocked(start, nr_pages,
2516 static unsigned long lockless_pages_from_mm(unsigned long start,
2518 unsigned int gup_flags,
2519 struct page **pages)
2521 unsigned long flags;
2525 if (!IS_ENABLED(CONFIG_HAVE_FAST_GUP) ||
2526 !gup_fast_permitted(start, end))
2529 if (gup_flags & FOLL_PIN) {
2530 seq = raw_read_seqcount(¤t->mm->write_protect_seq);
2536 * Disable interrupts. The nested form is used, in order to allow full,
2537 * general purpose use of this routine.
2539 * With interrupts disabled, we block page table pages from being freed
2540 * from under us. See struct mmu_table_batch comments in
2541 * include/asm-generic/tlb.h for more details.
2543 * We do not adopt an rcu_read_lock() here as we also want to block IPIs
2544 * that come from THPs splitting.
2546 local_irq_save(flags);
2547 gup_pgd_range(start, end, gup_flags, pages, &nr_pinned);
2548 local_irq_restore(flags);
2551 * When pinning pages for DMA there could be a concurrent write protect
2552 * from fork() via copy_page_range(), in this case always fail fast GUP.
2554 if (gup_flags & FOLL_PIN) {
2555 if (read_seqcount_retry(¤t->mm->write_protect_seq, seq)) {
2556 unpin_user_pages(pages, nr_pinned);
2563 static int internal_get_user_pages_fast(unsigned long start,
2564 unsigned long nr_pages,
2565 unsigned int gup_flags,
2566 struct page **pages)
2568 unsigned long len, end;
2569 unsigned long nr_pinned;
2572 if (WARN_ON_ONCE(gup_flags & ~(FOLL_WRITE | FOLL_LONGTERM |
2573 FOLL_FORCE | FOLL_PIN | FOLL_GET |
2577 if (gup_flags & FOLL_PIN)
2578 atomic_set(¤t->mm->has_pinned, 1);
2580 if (!(gup_flags & FOLL_FAST_ONLY))
2581 might_lock_read(¤t->mm->mmap_lock);
2583 start = untagged_addr(start) & PAGE_MASK;
2584 len = nr_pages << PAGE_SHIFT;
2585 if (check_add_overflow(start, len, &end))
2587 if (unlikely(!access_ok((void __user *)start, len)))
2590 nr_pinned = lockless_pages_from_mm(start, end, gup_flags, pages);
2591 if (nr_pinned == nr_pages || gup_flags & FOLL_FAST_ONLY)
2594 /* Slow path: try to get the remaining pages with get_user_pages */
2595 start += nr_pinned << PAGE_SHIFT;
2597 ret = __gup_longterm_unlocked(start, nr_pages - nr_pinned, gup_flags,
2601 * The caller has to unpin the pages we already pinned so
2602 * returning -errno is not an option
2608 return ret + nr_pinned;
2612 * get_user_pages_fast_only() - pin user pages in memory
2613 * @start: starting user address
2614 * @nr_pages: number of pages from start to pin
2615 * @gup_flags: flags modifying pin behaviour
2616 * @pages: array that receives pointers to the pages pinned.
2617 * Should be at least nr_pages long.
2619 * Like get_user_pages_fast() except it's IRQ-safe in that it won't fall back to
2621 * Note a difference with get_user_pages_fast: this always returns the
2622 * number of pages pinned, 0 if no pages were pinned.
2624 * If the architecture does not support this function, simply return with no
2627 * Careful, careful! COW breaking can go either way, so a non-write
2628 * access can get ambiguous page results. If you call this function without
2629 * 'write' set, you'd better be sure that you're ok with that ambiguity.
2631 int get_user_pages_fast_only(unsigned long start, int nr_pages,
2632 unsigned int gup_flags, struct page **pages)
2636 * Internally (within mm/gup.c), gup fast variants must set FOLL_GET,
2637 * because gup fast is always a "pin with a +1 page refcount" request.
2639 * FOLL_FAST_ONLY is required in order to match the API description of
2640 * this routine: no fall back to regular ("slow") GUP.
2642 gup_flags |= FOLL_GET | FOLL_FAST_ONLY;
2644 nr_pinned = internal_get_user_pages_fast(start, nr_pages, gup_flags,
2648 * As specified in the API description above, this routine is not
2649 * allowed to return negative values. However, the common core
2650 * routine internal_get_user_pages_fast() *can* return -errno.
2651 * Therefore, correct for that here:
2658 EXPORT_SYMBOL_GPL(get_user_pages_fast_only);
2661 * get_user_pages_fast() - pin user pages in memory
2662 * @start: starting user address
2663 * @nr_pages: number of pages from start to pin
2664 * @gup_flags: flags modifying pin behaviour
2665 * @pages: array that receives pointers to the pages pinned.
2666 * Should be at least nr_pages long.
2668 * Attempt to pin user pages in memory without taking mm->mmap_lock.
2669 * If not successful, it will fall back to taking the lock and
2670 * calling get_user_pages().
2672 * Returns number of pages pinned. This may be fewer than the number requested.
2673 * If nr_pages is 0 or negative, returns 0. If no pages were pinned, returns
2676 int get_user_pages_fast(unsigned long start, int nr_pages,
2677 unsigned int gup_flags, struct page **pages)
2679 if (!is_valid_gup_flags(gup_flags))
2683 * The caller may or may not have explicitly set FOLL_GET; either way is
2684 * OK. However, internally (within mm/gup.c), gup fast variants must set
2685 * FOLL_GET, because gup fast is always a "pin with a +1 page refcount"
2688 gup_flags |= FOLL_GET;
2689 return internal_get_user_pages_fast(start, nr_pages, gup_flags, pages);
2691 EXPORT_SYMBOL_GPL(get_user_pages_fast);
2694 * pin_user_pages_fast() - pin user pages in memory without taking locks
2696 * @start: starting user address
2697 * @nr_pages: number of pages from start to pin
2698 * @gup_flags: flags modifying pin behaviour
2699 * @pages: array that receives pointers to the pages pinned.
2700 * Should be at least nr_pages long.
2702 * Nearly the same as get_user_pages_fast(), except that FOLL_PIN is set. See
2703 * get_user_pages_fast() for documentation on the function arguments, because
2704 * the arguments here are identical.
2706 * FOLL_PIN means that the pages must be released via unpin_user_page(). Please
2707 * see Documentation/core-api/pin_user_pages.rst for further details.
2709 int pin_user_pages_fast(unsigned long start, int nr_pages,
2710 unsigned int gup_flags, struct page **pages)
2712 /* FOLL_GET and FOLL_PIN are mutually exclusive. */
2713 if (WARN_ON_ONCE(gup_flags & FOLL_GET))
2716 gup_flags |= FOLL_PIN;
2717 return internal_get_user_pages_fast(start, nr_pages, gup_flags, pages);
2719 EXPORT_SYMBOL_GPL(pin_user_pages_fast);
2722 * This is the FOLL_PIN equivalent of get_user_pages_fast_only(). Behavior
2723 * is the same, except that this one sets FOLL_PIN instead of FOLL_GET.
2725 * The API rules are the same, too: no negative values may be returned.
2727 int pin_user_pages_fast_only(unsigned long start, int nr_pages,
2728 unsigned int gup_flags, struct page **pages)
2733 * FOLL_GET and FOLL_PIN are mutually exclusive. Note that the API
2734 * rules require returning 0, rather than -errno:
2736 if (WARN_ON_ONCE(gup_flags & FOLL_GET))
2739 * FOLL_FAST_ONLY is required in order to match the API description of
2740 * this routine: no fall back to regular ("slow") GUP.
2742 gup_flags |= (FOLL_PIN | FOLL_FAST_ONLY);
2743 nr_pinned = internal_get_user_pages_fast(start, nr_pages, gup_flags,
2746 * This routine is not allowed to return negative values. However,
2747 * internal_get_user_pages_fast() *can* return -errno. Therefore,
2748 * correct for that here:
2755 EXPORT_SYMBOL_GPL(pin_user_pages_fast_only);
2758 * pin_user_pages_remote() - pin pages of a remote process
2760 * @mm: mm_struct of target mm
2761 * @start: starting user address
2762 * @nr_pages: number of pages from start to pin
2763 * @gup_flags: flags modifying lookup behaviour
2764 * @pages: array that receives pointers to the pages pinned.
2765 * Should be at least nr_pages long. Or NULL, if caller
2766 * only intends to ensure the pages are faulted in.
2767 * @vmas: array of pointers to vmas corresponding to each page.
2768 * Or NULL if the caller does not require them.
2769 * @locked: pointer to lock flag indicating whether lock is held and
2770 * subsequently whether VM_FAULT_RETRY functionality can be
2771 * utilised. Lock must initially be held.
2773 * Nearly the same as get_user_pages_remote(), except that FOLL_PIN is set. See
2774 * get_user_pages_remote() for documentation on the function arguments, because
2775 * the arguments here are identical.
2777 * FOLL_PIN means that the pages must be released via unpin_user_page(). Please
2778 * see Documentation/core-api/pin_user_pages.rst for details.
2780 long pin_user_pages_remote(struct mm_struct *mm,
2781 unsigned long start, unsigned long nr_pages,
2782 unsigned int gup_flags, struct page **pages,
2783 struct vm_area_struct **vmas, int *locked)
2785 /* FOLL_GET and FOLL_PIN are mutually exclusive. */
2786 if (WARN_ON_ONCE(gup_flags & FOLL_GET))
2789 gup_flags |= FOLL_PIN;
2790 return __get_user_pages_remote(mm, start, nr_pages, gup_flags,
2791 pages, vmas, locked);
2793 EXPORT_SYMBOL(pin_user_pages_remote);
2796 * pin_user_pages() - pin user pages in memory for use by other devices
2798 * @start: starting user address
2799 * @nr_pages: number of pages from start to pin
2800 * @gup_flags: flags modifying lookup behaviour
2801 * @pages: array that receives pointers to the pages pinned.
2802 * Should be at least nr_pages long. Or NULL, if caller
2803 * only intends to ensure the pages are faulted in.
2804 * @vmas: array of pointers to vmas corresponding to each page.
2805 * Or NULL if the caller does not require them.
2807 * Nearly the same as get_user_pages(), except that FOLL_TOUCH is not set, and
2810 * FOLL_PIN means that the pages must be released via unpin_user_page(). Please
2811 * see Documentation/core-api/pin_user_pages.rst for details.
2813 long pin_user_pages(unsigned long start, unsigned long nr_pages,
2814 unsigned int gup_flags, struct page **pages,
2815 struct vm_area_struct **vmas)
2817 /* FOLL_GET and FOLL_PIN are mutually exclusive. */
2818 if (WARN_ON_ONCE(gup_flags & FOLL_GET))
2821 gup_flags |= FOLL_PIN;
2822 return __gup_longterm_locked(current->mm, start, nr_pages,
2823 pages, vmas, gup_flags);
2825 EXPORT_SYMBOL(pin_user_pages);
2828 * pin_user_pages_unlocked() is the FOLL_PIN variant of
2829 * get_user_pages_unlocked(). Behavior is the same, except that this one sets
2830 * FOLL_PIN and rejects FOLL_GET.
2832 long pin_user_pages_unlocked(unsigned long start, unsigned long nr_pages,
2833 struct page **pages, unsigned int gup_flags)
2835 /* FOLL_GET and FOLL_PIN are mutually exclusive. */
2836 if (WARN_ON_ONCE(gup_flags & FOLL_GET))
2839 gup_flags |= FOLL_PIN;
2840 return get_user_pages_unlocked(start, nr_pages, pages, gup_flags);
2842 EXPORT_SYMBOL(pin_user_pages_unlocked);
2845 * pin_user_pages_locked() is the FOLL_PIN variant of get_user_pages_locked().
2846 * Behavior is the same, except that this one sets FOLL_PIN and rejects
2849 long pin_user_pages_locked(unsigned long start, unsigned long nr_pages,
2850 unsigned int gup_flags, struct page **pages,
2854 * FIXME: Current FOLL_LONGTERM behavior is incompatible with
2855 * FAULT_FLAG_ALLOW_RETRY because of the FS DAX check requirement on
2856 * vmas. As there are no users of this flag in this call we simply
2857 * disallow this option for now.
2859 if (WARN_ON_ONCE(gup_flags & FOLL_LONGTERM))
2862 /* FOLL_GET and FOLL_PIN are mutually exclusive. */
2863 if (WARN_ON_ONCE(gup_flags & FOLL_GET))
2866 gup_flags |= FOLL_PIN;
2867 return __get_user_pages_locked(current->mm, start, nr_pages,
2868 pages, NULL, locked,
2869 gup_flags | FOLL_TOUCH);
2871 EXPORT_SYMBOL(pin_user_pages_locked);