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>
13 #include <linux/secretmem.h>
15 #include <linux/sched/signal.h>
16 #include <linux/rwsem.h>
17 #include <linux/hugetlb.h>
18 #include <linux/migrate.h>
19 #include <linux/mm_inline.h>
20 #include <linux/sched/mm.h>
22 #include <asm/mmu_context.h>
23 #include <asm/tlbflush.h>
27 struct follow_page_context {
28 struct dev_pagemap *pgmap;
29 unsigned int page_mask;
32 static inline void sanity_check_pinned_pages(struct page **pages,
35 if (!IS_ENABLED(CONFIG_DEBUG_VM))
39 * We only pin anonymous pages if they are exclusive. Once pinned, we
40 * can no longer turn them possibly shared and PageAnonExclusive() will
41 * stick around until the page is freed.
43 * We'd like to verify that our pinned anonymous pages are still mapped
44 * exclusively. The issue with anon THP is that we don't know how
45 * they are/were mapped when pinning them. However, for anon
46 * THP we can assume that either the given page (PTE-mapped THP) or
47 * the head page (PMD-mapped THP) should be PageAnonExclusive(). If
48 * neither is the case, there is certainly something wrong.
50 for (; npages; npages--, pages++) {
51 struct page *page = *pages;
52 struct folio *folio = page_folio(page);
54 if (!folio_test_anon(folio))
56 if (!folio_test_large(folio) || folio_test_hugetlb(folio))
57 VM_BUG_ON_PAGE(!PageAnonExclusive(&folio->page), page);
59 /* Either a PTE-mapped or a PMD-mapped THP. */
60 VM_BUG_ON_PAGE(!PageAnonExclusive(&folio->page) &&
61 !PageAnonExclusive(page), page);
66 * Return the folio with ref appropriately incremented,
67 * or NULL if that failed.
69 static inline struct folio *try_get_folio(struct page *page, int refs)
74 folio = page_folio(page);
75 if (WARN_ON_ONCE(folio_ref_count(folio) < 0))
77 if (unlikely(!folio_ref_try_add_rcu(folio, refs)))
81 * At this point we have a stable reference to the folio; but it
82 * could be that between calling page_folio() and the refcount
83 * increment, the folio was split, in which case we'd end up
84 * holding a reference on a folio that has nothing to do with the page
85 * we were given anymore.
86 * So now that the folio is stable, recheck that the page still
87 * belongs to this folio.
89 if (unlikely(page_folio(page) != folio)) {
90 if (!put_devmap_managed_page_refs(&folio->page, refs))
91 folio_put_refs(folio, refs);
99 * try_grab_folio() - Attempt to get or pin a folio.
100 * @page: pointer to page to be grabbed
101 * @refs: the value to (effectively) add to the folio's refcount
102 * @flags: gup flags: these are the FOLL_* flag values.
104 * "grab" names in this file mean, "look at flags to decide whether to use
105 * FOLL_PIN or FOLL_GET behavior, when incrementing the folio's refcount.
107 * Either FOLL_PIN or FOLL_GET (or neither) must be set, but not both at the
108 * same time. (That's true throughout the get_user_pages*() and
109 * pin_user_pages*() APIs.) Cases:
111 * FOLL_GET: folio's refcount will be incremented by @refs.
113 * FOLL_PIN on large folios: folio's refcount will be incremented by
114 * @refs, and its compound_pincount will be incremented by @refs.
116 * FOLL_PIN on single-page folios: folio's refcount will be incremented by
117 * @refs * GUP_PIN_COUNTING_BIAS.
119 * Return: The folio containing @page (with refcount appropriately
120 * incremented) for success, or NULL upon failure. If neither FOLL_GET
121 * nor FOLL_PIN was set, that's considered failure, and furthermore,
122 * a likely bug in the caller, so a warning is also emitted.
124 struct folio *try_grab_folio(struct page *page, int refs, unsigned int flags)
126 if (flags & FOLL_GET)
127 return try_get_folio(page, refs);
128 else if (flags & FOLL_PIN) {
132 * Can't do FOLL_LONGTERM + FOLL_PIN gup fast path if not in a
133 * right zone, so fail and let the caller fall back to the slow
136 if (unlikely((flags & FOLL_LONGTERM) &&
137 !is_pinnable_page(page)))
141 * CAUTION: Don't use compound_head() on the page before this
142 * point, the result won't be stable.
144 folio = try_get_folio(page, refs);
149 * When pinning a large folio, use an exact count to track it.
151 * However, be sure to *also* increment the normal folio
152 * refcount field at least once, so that the folio really
153 * is pinned. That's why the refcount from the earlier
154 * try_get_folio() is left intact.
156 if (folio_test_large(folio))
157 atomic_add(refs, folio_pincount_ptr(folio));
160 refs * (GUP_PIN_COUNTING_BIAS - 1));
161 node_stat_mod_folio(folio, NR_FOLL_PIN_ACQUIRED, refs);
170 static void gup_put_folio(struct folio *folio, int refs, unsigned int flags)
172 if (flags & FOLL_PIN) {
173 node_stat_mod_folio(folio, NR_FOLL_PIN_RELEASED, refs);
174 if (folio_test_large(folio))
175 atomic_sub(refs, folio_pincount_ptr(folio));
177 refs *= GUP_PIN_COUNTING_BIAS;
180 if (!put_devmap_managed_page_refs(&folio->page, refs))
181 folio_put_refs(folio, refs);
185 * try_grab_page() - elevate a page's refcount by a flag-dependent amount
186 * @page: pointer to page to be grabbed
187 * @flags: gup flags: these are the FOLL_* flag values.
189 * This might not do anything at all, depending on the flags argument.
191 * "grab" names in this file mean, "look at flags to decide whether to use
192 * FOLL_PIN or FOLL_GET behavior, when incrementing the page's refcount.
194 * Either FOLL_PIN or FOLL_GET (or neither) may be set, but not both at the same
195 * time. Cases: please see the try_grab_folio() documentation, with
198 * Return: true for success, or if no action was required (if neither FOLL_PIN
199 * nor FOLL_GET was set, nothing is done). False for failure: FOLL_GET or
200 * FOLL_PIN was set, but the page could not be grabbed.
202 bool __must_check try_grab_page(struct page *page, unsigned int flags)
204 struct folio *folio = page_folio(page);
206 WARN_ON_ONCE((flags & (FOLL_GET | FOLL_PIN)) == (FOLL_GET | FOLL_PIN));
207 if (WARN_ON_ONCE(folio_ref_count(folio) <= 0))
210 if (flags & FOLL_GET)
211 folio_ref_inc(folio);
212 else if (flags & FOLL_PIN) {
214 * Similar to try_grab_folio(): be sure to *also*
215 * increment the normal page refcount field at least once,
216 * so that the page really is pinned.
218 if (folio_test_large(folio)) {
219 folio_ref_add(folio, 1);
220 atomic_add(1, folio_pincount_ptr(folio));
222 folio_ref_add(folio, GUP_PIN_COUNTING_BIAS);
225 node_stat_mod_folio(folio, NR_FOLL_PIN_ACQUIRED, 1);
232 * unpin_user_page() - release a dma-pinned page
233 * @page: pointer to page to be released
235 * Pages that were pinned via pin_user_pages*() must be released via either
236 * unpin_user_page(), or one of the unpin_user_pages*() routines. This is so
237 * that such pages can be separately tracked and uniquely handled. In
238 * particular, interactions with RDMA and filesystems need special handling.
240 void unpin_user_page(struct page *page)
242 sanity_check_pinned_pages(&page, 1);
243 gup_put_folio(page_folio(page), 1, FOLL_PIN);
245 EXPORT_SYMBOL(unpin_user_page);
247 static inline struct folio *gup_folio_range_next(struct page *start,
248 unsigned long npages, unsigned long i, unsigned int *ntails)
250 struct page *next = nth_page(start, i);
251 struct folio *folio = page_folio(next);
254 if (folio_test_large(folio))
255 nr = min_t(unsigned int, npages - i,
256 folio_nr_pages(folio) - folio_page_idx(folio, next));
262 static inline struct folio *gup_folio_next(struct page **list,
263 unsigned long npages, unsigned long i, unsigned int *ntails)
265 struct folio *folio = page_folio(list[i]);
268 for (nr = i + 1; nr < npages; nr++) {
269 if (page_folio(list[nr]) != folio)
278 * unpin_user_pages_dirty_lock() - release and optionally dirty gup-pinned pages
279 * @pages: array of pages to be maybe marked dirty, and definitely released.
280 * @npages: number of pages in the @pages array.
281 * @make_dirty: whether to mark the pages dirty
283 * "gup-pinned page" refers to a page that has had one of the get_user_pages()
284 * variants called on that page.
286 * For each page in the @pages array, make that page (or its head page, if a
287 * compound page) dirty, if @make_dirty is true, and if the page was previously
288 * listed as clean. In any case, releases all pages using unpin_user_page(),
289 * possibly via unpin_user_pages(), for the non-dirty case.
291 * Please see the unpin_user_page() documentation for details.
293 * set_page_dirty_lock() is used internally. If instead, set_page_dirty() is
294 * required, then the caller should a) verify that this is really correct,
295 * because _lock() is usually required, and b) hand code it:
296 * set_page_dirty_lock(), unpin_user_page().
299 void unpin_user_pages_dirty_lock(struct page **pages, unsigned long npages,
307 unpin_user_pages(pages, npages);
311 sanity_check_pinned_pages(pages, npages);
312 for (i = 0; i < npages; i += nr) {
313 folio = gup_folio_next(pages, npages, i, &nr);
315 * Checking PageDirty at this point may race with
316 * clear_page_dirty_for_io(), but that's OK. Two key
319 * 1) This code sees the page as already dirty, so it
320 * skips the call to set_page_dirty(). That could happen
321 * because clear_page_dirty_for_io() called
322 * page_mkclean(), followed by set_page_dirty().
323 * However, now the page is going to get written back,
324 * which meets the original intention of setting it
325 * dirty, so all is well: clear_page_dirty_for_io() goes
326 * on to call TestClearPageDirty(), and write the page
329 * 2) This code sees the page as clean, so it calls
330 * set_page_dirty(). The page stays dirty, despite being
331 * written back, so it gets written back again in the
332 * next writeback cycle. This is harmless.
334 if (!folio_test_dirty(folio)) {
336 folio_mark_dirty(folio);
339 gup_put_folio(folio, nr, FOLL_PIN);
342 EXPORT_SYMBOL(unpin_user_pages_dirty_lock);
345 * unpin_user_page_range_dirty_lock() - release and optionally dirty
346 * gup-pinned page range
348 * @page: the starting page of a range maybe marked dirty, and definitely released.
349 * @npages: number of consecutive pages to release.
350 * @make_dirty: whether to mark the pages dirty
352 * "gup-pinned page range" refers to a range of pages that has had one of the
353 * pin_user_pages() variants called on that page.
355 * For the page ranges defined by [page .. page+npages], make that range (or
356 * its head pages, if a compound page) dirty, if @make_dirty is true, and if the
357 * page range was previously listed as clean.
359 * set_page_dirty_lock() is used internally. If instead, set_page_dirty() is
360 * required, then the caller should a) verify that this is really correct,
361 * because _lock() is usually required, and b) hand code it:
362 * set_page_dirty_lock(), unpin_user_page().
365 void unpin_user_page_range_dirty_lock(struct page *page, unsigned long npages,
372 for (i = 0; i < npages; i += nr) {
373 folio = gup_folio_range_next(page, npages, i, &nr);
374 if (make_dirty && !folio_test_dirty(folio)) {
376 folio_mark_dirty(folio);
379 gup_put_folio(folio, nr, FOLL_PIN);
382 EXPORT_SYMBOL(unpin_user_page_range_dirty_lock);
384 static void unpin_user_pages_lockless(struct page **pages, unsigned long npages)
391 * Don't perform any sanity checks because we might have raced with
392 * fork() and some anonymous pages might now actually be shared --
393 * which is why we're unpinning after all.
395 for (i = 0; i < npages; i += nr) {
396 folio = gup_folio_next(pages, npages, i, &nr);
397 gup_put_folio(folio, nr, FOLL_PIN);
402 * unpin_user_pages() - release an array of gup-pinned pages.
403 * @pages: array of pages to be marked dirty and released.
404 * @npages: number of pages in the @pages array.
406 * For each page in the @pages array, release the page using unpin_user_page().
408 * Please see the unpin_user_page() documentation for details.
410 void unpin_user_pages(struct page **pages, unsigned long npages)
417 * If this WARN_ON() fires, then the system *might* be leaking pages (by
418 * leaving them pinned), but probably not. More likely, gup/pup returned
419 * a hard -ERRNO error to the caller, who erroneously passed it here.
421 if (WARN_ON(IS_ERR_VALUE(npages)))
424 sanity_check_pinned_pages(pages, npages);
425 for (i = 0; i < npages; i += nr) {
426 folio = gup_folio_next(pages, npages, i, &nr);
427 gup_put_folio(folio, nr, FOLL_PIN);
430 EXPORT_SYMBOL(unpin_user_pages);
433 * Set the MMF_HAS_PINNED if not set yet; after set it'll be there for the mm's
434 * lifecycle. Avoid setting the bit unless necessary, or it might cause write
435 * cache bouncing on large SMP machines for concurrent pinned gups.
437 static inline void mm_set_has_pinned_flag(unsigned long *mm_flags)
439 if (!test_bit(MMF_HAS_PINNED, mm_flags))
440 set_bit(MMF_HAS_PINNED, mm_flags);
444 static struct page *no_page_table(struct vm_area_struct *vma,
448 * When core dumping an enormous anonymous area that nobody
449 * has touched so far, we don't want to allocate unnecessary pages or
450 * page tables. Return error instead of NULL to skip handle_mm_fault,
451 * then get_dump_page() will return NULL to leave a hole in the dump.
452 * But we can only make this optimization where a hole would surely
453 * be zero-filled if handle_mm_fault() actually did handle it.
455 if ((flags & FOLL_DUMP) &&
456 (vma_is_anonymous(vma) || !vma->vm_ops->fault))
457 return ERR_PTR(-EFAULT);
461 static int follow_pfn_pte(struct vm_area_struct *vma, unsigned long address,
462 pte_t *pte, unsigned int flags)
464 if (flags & FOLL_TOUCH) {
467 if (flags & FOLL_WRITE)
468 entry = pte_mkdirty(entry);
469 entry = pte_mkyoung(entry);
471 if (!pte_same(*pte, entry)) {
472 set_pte_at(vma->vm_mm, address, pte, entry);
473 update_mmu_cache(vma, address, pte);
477 /* Proper page table entry exists, but no corresponding struct page */
482 * FOLL_FORCE can write to even unwritable pte's, but only
483 * after we've gone through a COW cycle and they are dirty.
485 static inline bool can_follow_write_pte(pte_t pte, unsigned int flags)
487 return pte_write(pte) ||
488 ((flags & FOLL_FORCE) && (flags & FOLL_COW) && pte_dirty(pte));
491 static struct page *follow_page_pte(struct vm_area_struct *vma,
492 unsigned long address, pmd_t *pmd, unsigned int flags,
493 struct dev_pagemap **pgmap)
495 struct mm_struct *mm = vma->vm_mm;
501 /* FOLL_GET and FOLL_PIN are mutually exclusive. */
502 if (WARN_ON_ONCE((flags & (FOLL_PIN | FOLL_GET)) ==
503 (FOLL_PIN | FOLL_GET)))
504 return ERR_PTR(-EINVAL);
506 if (unlikely(pmd_bad(*pmd)))
507 return no_page_table(vma, flags);
509 ptep = pte_offset_map_lock(mm, pmd, address, &ptl);
511 if (!pte_present(pte)) {
514 * KSM's break_ksm() relies upon recognizing a ksm page
515 * even while it is being migrated, so for that case we
516 * need migration_entry_wait().
518 if (likely(!(flags & FOLL_MIGRATION)))
522 entry = pte_to_swp_entry(pte);
523 if (!is_migration_entry(entry))
525 pte_unmap_unlock(ptep, ptl);
526 migration_entry_wait(mm, pmd, address);
529 if ((flags & FOLL_NUMA) && pte_protnone(pte))
531 if ((flags & FOLL_WRITE) && !can_follow_write_pte(pte, flags)) {
532 pte_unmap_unlock(ptep, ptl);
536 page = vm_normal_page(vma, address, pte);
537 if (!page && pte_devmap(pte) && (flags & (FOLL_GET | FOLL_PIN))) {
539 * Only return device mapping pages in the FOLL_GET or FOLL_PIN
540 * case since they are only valid while holding the pgmap
543 *pgmap = get_dev_pagemap(pte_pfn(pte), *pgmap);
545 page = pte_page(pte);
548 } else if (unlikely(!page)) {
549 if (flags & FOLL_DUMP) {
550 /* Avoid special (like zero) pages in core dumps */
551 page = ERR_PTR(-EFAULT);
555 if (is_zero_pfn(pte_pfn(pte))) {
556 page = pte_page(pte);
558 ret = follow_pfn_pte(vma, address, ptep, flags);
564 if (!pte_write(pte) && gup_must_unshare(flags, page)) {
565 page = ERR_PTR(-EMLINK);
569 VM_BUG_ON_PAGE((flags & FOLL_PIN) && PageAnon(page) &&
570 !PageAnonExclusive(page), page);
572 /* try_grab_page() does nothing unless FOLL_GET or FOLL_PIN is set. */
573 if (unlikely(!try_grab_page(page, flags))) {
574 page = ERR_PTR(-ENOMEM);
578 * We need to make the page accessible if and only if we are going
579 * to access its content (the FOLL_PIN case). Please see
580 * Documentation/core-api/pin_user_pages.rst for details.
582 if (flags & FOLL_PIN) {
583 ret = arch_make_page_accessible(page);
585 unpin_user_page(page);
590 if (flags & FOLL_TOUCH) {
591 if ((flags & FOLL_WRITE) &&
592 !pte_dirty(pte) && !PageDirty(page))
593 set_page_dirty(page);
595 * pte_mkyoung() would be more correct here, but atomic care
596 * is needed to avoid losing the dirty bit: it is easier to use
597 * mark_page_accessed().
599 mark_page_accessed(page);
602 pte_unmap_unlock(ptep, ptl);
605 pte_unmap_unlock(ptep, ptl);
608 return no_page_table(vma, flags);
611 static struct page *follow_pmd_mask(struct vm_area_struct *vma,
612 unsigned long address, pud_t *pudp,
614 struct follow_page_context *ctx)
619 struct mm_struct *mm = vma->vm_mm;
621 pmd = pmd_offset(pudp, address);
623 * The READ_ONCE() will stabilize the pmdval in a register or
624 * on the stack so that it will stop changing under the code.
626 pmdval = READ_ONCE(*pmd);
627 if (pmd_none(pmdval))
628 return no_page_table(vma, flags);
629 if (pmd_huge(pmdval) && is_vm_hugetlb_page(vma)) {
630 page = follow_huge_pmd(mm, address, pmd, flags);
633 return no_page_table(vma, flags);
635 if (is_hugepd(__hugepd(pmd_val(pmdval)))) {
636 page = follow_huge_pd(vma, address,
637 __hugepd(pmd_val(pmdval)), flags,
641 return no_page_table(vma, flags);
644 if (!pmd_present(pmdval)) {
646 * Should never reach here, if thp migration is not supported;
647 * Otherwise, it must be a thp migration entry.
649 VM_BUG_ON(!thp_migration_supported() ||
650 !is_pmd_migration_entry(pmdval));
652 if (likely(!(flags & FOLL_MIGRATION)))
653 return no_page_table(vma, flags);
655 pmd_migration_entry_wait(mm, pmd);
656 pmdval = READ_ONCE(*pmd);
658 * MADV_DONTNEED may convert the pmd to null because
659 * mmap_lock is held in read mode
661 if (pmd_none(pmdval))
662 return no_page_table(vma, flags);
665 if (pmd_devmap(pmdval)) {
666 ptl = pmd_lock(mm, pmd);
667 page = follow_devmap_pmd(vma, address, pmd, flags, &ctx->pgmap);
672 if (likely(!pmd_trans_huge(pmdval)))
673 return follow_page_pte(vma, address, pmd, flags, &ctx->pgmap);
675 if ((flags & FOLL_NUMA) && pmd_protnone(pmdval))
676 return no_page_table(vma, flags);
679 ptl = pmd_lock(mm, pmd);
680 if (unlikely(pmd_none(*pmd))) {
682 return no_page_table(vma, flags);
684 if (unlikely(!pmd_present(*pmd))) {
686 if (likely(!(flags & FOLL_MIGRATION)))
687 return no_page_table(vma, flags);
688 pmd_migration_entry_wait(mm, pmd);
691 if (unlikely(!pmd_trans_huge(*pmd))) {
693 return follow_page_pte(vma, address, pmd, flags, &ctx->pgmap);
695 if (flags & FOLL_SPLIT_PMD) {
697 page = pmd_page(*pmd);
698 if (is_huge_zero_page(page)) {
701 split_huge_pmd(vma, pmd, address);
702 if (pmd_trans_unstable(pmd))
706 split_huge_pmd(vma, pmd, address);
707 ret = pte_alloc(mm, pmd) ? -ENOMEM : 0;
710 return ret ? ERR_PTR(ret) :
711 follow_page_pte(vma, address, pmd, flags, &ctx->pgmap);
713 page = follow_trans_huge_pmd(vma, address, pmd, flags);
715 ctx->page_mask = HPAGE_PMD_NR - 1;
719 static struct page *follow_pud_mask(struct vm_area_struct *vma,
720 unsigned long address, p4d_t *p4dp,
722 struct follow_page_context *ctx)
727 struct mm_struct *mm = vma->vm_mm;
729 pud = pud_offset(p4dp, address);
731 return no_page_table(vma, flags);
732 if (pud_huge(*pud) && is_vm_hugetlb_page(vma)) {
733 page = follow_huge_pud(mm, address, pud, flags);
736 return no_page_table(vma, flags);
738 if (is_hugepd(__hugepd(pud_val(*pud)))) {
739 page = follow_huge_pd(vma, address,
740 __hugepd(pud_val(*pud)), flags,
744 return no_page_table(vma, flags);
746 if (pud_devmap(*pud)) {
747 ptl = pud_lock(mm, pud);
748 page = follow_devmap_pud(vma, address, pud, flags, &ctx->pgmap);
753 if (unlikely(pud_bad(*pud)))
754 return no_page_table(vma, flags);
756 return follow_pmd_mask(vma, address, pud, flags, ctx);
759 static struct page *follow_p4d_mask(struct vm_area_struct *vma,
760 unsigned long address, pgd_t *pgdp,
762 struct follow_page_context *ctx)
767 p4d = p4d_offset(pgdp, address);
769 return no_page_table(vma, flags);
770 BUILD_BUG_ON(p4d_huge(*p4d));
771 if (unlikely(p4d_bad(*p4d)))
772 return no_page_table(vma, flags);
774 if (is_hugepd(__hugepd(p4d_val(*p4d)))) {
775 page = follow_huge_pd(vma, address,
776 __hugepd(p4d_val(*p4d)), flags,
780 return no_page_table(vma, flags);
782 return follow_pud_mask(vma, address, p4d, flags, ctx);
786 * follow_page_mask - look up a page descriptor from a user-virtual address
787 * @vma: vm_area_struct mapping @address
788 * @address: virtual address to look up
789 * @flags: flags modifying lookup behaviour
790 * @ctx: contains dev_pagemap for %ZONE_DEVICE memory pinning and a
791 * pointer to output page_mask
793 * @flags can have FOLL_ flags set, defined in <linux/mm.h>
795 * When getting pages from ZONE_DEVICE memory, the @ctx->pgmap caches
796 * the device's dev_pagemap metadata to avoid repeating expensive lookups.
798 * When getting an anonymous page and the caller has to trigger unsharing
799 * of a shared anonymous page first, -EMLINK is returned. The caller should
800 * trigger a fault with FAULT_FLAG_UNSHARE set. Note that unsharing is only
801 * relevant with FOLL_PIN and !FOLL_WRITE.
803 * On output, the @ctx->page_mask is set according to the size of the page.
805 * Return: the mapped (struct page *), %NULL if no mapping exists, or
806 * an error pointer if there is a mapping to something not represented
807 * by a page descriptor (see also vm_normal_page()).
809 static struct page *follow_page_mask(struct vm_area_struct *vma,
810 unsigned long address, unsigned int flags,
811 struct follow_page_context *ctx)
815 struct mm_struct *mm = vma->vm_mm;
819 /* make this handle hugepd */
820 page = follow_huge_addr(mm, address, flags & FOLL_WRITE);
822 WARN_ON_ONCE(flags & (FOLL_GET | FOLL_PIN));
826 pgd = pgd_offset(mm, address);
828 if (pgd_none(*pgd) || unlikely(pgd_bad(*pgd)))
829 return no_page_table(vma, flags);
831 if (pgd_huge(*pgd)) {
832 page = follow_huge_pgd(mm, address, pgd, flags);
835 return no_page_table(vma, flags);
837 if (is_hugepd(__hugepd(pgd_val(*pgd)))) {
838 page = follow_huge_pd(vma, address,
839 __hugepd(pgd_val(*pgd)), flags,
843 return no_page_table(vma, flags);
846 return follow_p4d_mask(vma, address, pgd, flags, ctx);
849 struct page *follow_page(struct vm_area_struct *vma, unsigned long address,
850 unsigned int foll_flags)
852 struct follow_page_context ctx = { NULL };
855 if (vma_is_secretmem(vma))
858 if (foll_flags & FOLL_PIN)
861 page = follow_page_mask(vma, address, foll_flags, &ctx);
863 put_dev_pagemap(ctx.pgmap);
867 static int get_gate_page(struct mm_struct *mm, unsigned long address,
868 unsigned int gup_flags, struct vm_area_struct **vma,
878 /* user gate pages are read-only */
879 if (gup_flags & FOLL_WRITE)
881 if (address > TASK_SIZE)
882 pgd = pgd_offset_k(address);
884 pgd = pgd_offset_gate(mm, address);
887 p4d = p4d_offset(pgd, address);
890 pud = pud_offset(p4d, address);
893 pmd = pmd_offset(pud, address);
894 if (!pmd_present(*pmd))
896 VM_BUG_ON(pmd_trans_huge(*pmd));
897 pte = pte_offset_map(pmd, address);
900 *vma = get_gate_vma(mm);
903 *page = vm_normal_page(*vma, address, *pte);
905 if ((gup_flags & FOLL_DUMP) || !is_zero_pfn(pte_pfn(*pte)))
907 *page = pte_page(*pte);
909 if (unlikely(!try_grab_page(*page, gup_flags))) {
921 * mmap_lock must be held on entry. If @locked != NULL and *@flags
922 * does not include FOLL_NOWAIT, the mmap_lock may be released. If it
923 * is, *@locked will be set to 0 and -EBUSY returned.
925 static int faultin_page(struct vm_area_struct *vma,
926 unsigned long address, unsigned int *flags, bool unshare,
929 unsigned int fault_flags = 0;
932 if (*flags & FOLL_NOFAULT)
934 if (*flags & FOLL_WRITE)
935 fault_flags |= FAULT_FLAG_WRITE;
936 if (*flags & FOLL_REMOTE)
937 fault_flags |= FAULT_FLAG_REMOTE;
939 fault_flags |= FAULT_FLAG_ALLOW_RETRY | FAULT_FLAG_KILLABLE;
940 if (*flags & FOLL_NOWAIT)
941 fault_flags |= FAULT_FLAG_ALLOW_RETRY | FAULT_FLAG_RETRY_NOWAIT;
942 if (*flags & FOLL_TRIED) {
944 * Note: FAULT_FLAG_ALLOW_RETRY and FAULT_FLAG_TRIED
947 fault_flags |= FAULT_FLAG_TRIED;
950 fault_flags |= FAULT_FLAG_UNSHARE;
951 /* FAULT_FLAG_WRITE and FAULT_FLAG_UNSHARE are incompatible */
952 VM_BUG_ON(fault_flags & FAULT_FLAG_WRITE);
955 ret = handle_mm_fault(vma, address, fault_flags, NULL);
956 if (ret & VM_FAULT_ERROR) {
957 int err = vm_fault_to_errno(ret, *flags);
964 if (ret & VM_FAULT_RETRY) {
965 if (locked && !(fault_flags & FAULT_FLAG_RETRY_NOWAIT))
971 * The VM_FAULT_WRITE bit tells us that do_wp_page has broken COW when
972 * necessary, even if maybe_mkwrite decided not to set pte_write. We
973 * can thus safely do subsequent page lookups as if they were reads.
974 * But only do so when looping for pte_write is futile: in some cases
975 * userspace may also be wanting to write to the gotten user page,
976 * which a read fault here might prevent (a readonly page might get
977 * reCOWed by userspace write).
979 if ((ret & VM_FAULT_WRITE) && !(vma->vm_flags & VM_WRITE))
984 static int check_vma_flags(struct vm_area_struct *vma, unsigned long gup_flags)
986 vm_flags_t vm_flags = vma->vm_flags;
987 int write = (gup_flags & FOLL_WRITE);
988 int foreign = (gup_flags & FOLL_REMOTE);
990 if (vm_flags & (VM_IO | VM_PFNMAP))
993 if (gup_flags & FOLL_ANON && !vma_is_anonymous(vma))
996 if ((gup_flags & FOLL_LONGTERM) && vma_is_fsdax(vma))
999 if (vma_is_secretmem(vma))
1003 if (!(vm_flags & VM_WRITE)) {
1004 if (!(gup_flags & FOLL_FORCE))
1007 * We used to let the write,force case do COW in a
1008 * VM_MAYWRITE VM_SHARED !VM_WRITE vma, so ptrace could
1009 * set a breakpoint in a read-only mapping of an
1010 * executable, without corrupting the file (yet only
1011 * when that file had been opened for writing!).
1012 * Anon pages in shared mappings are surprising: now
1015 if (!is_cow_mapping(vm_flags))
1018 } else if (!(vm_flags & VM_READ)) {
1019 if (!(gup_flags & FOLL_FORCE))
1022 * Is there actually any vma we can reach here which does not
1023 * have VM_MAYREAD set?
1025 if (!(vm_flags & VM_MAYREAD))
1029 * gups are always data accesses, not instruction
1030 * fetches, so execute=false here
1032 if (!arch_vma_access_permitted(vma, write, false, foreign))
1038 * __get_user_pages() - pin user pages in memory
1039 * @mm: mm_struct of target mm
1040 * @start: starting user address
1041 * @nr_pages: number of pages from start to pin
1042 * @gup_flags: flags modifying pin behaviour
1043 * @pages: array that receives pointers to the pages pinned.
1044 * Should be at least nr_pages long. Or NULL, if caller
1045 * only intends to ensure the pages are faulted in.
1046 * @vmas: array of pointers to vmas corresponding to each page.
1047 * Or NULL if the caller does not require them.
1048 * @locked: whether we're still with the mmap_lock held
1050 * Returns either number of pages pinned (which may be less than the
1051 * number requested), or an error. Details about the return value:
1053 * -- If nr_pages is 0, returns 0.
1054 * -- If nr_pages is >0, but no pages were pinned, returns -errno.
1055 * -- If nr_pages is >0, and some pages were pinned, returns the number of
1056 * pages pinned. Again, this may be less than nr_pages.
1057 * -- 0 return value is possible when the fault would need to be retried.
1059 * The caller is responsible for releasing returned @pages, via put_page().
1061 * @vmas are valid only as long as mmap_lock is held.
1063 * Must be called with mmap_lock held. It may be released. See below.
1065 * __get_user_pages walks a process's page tables and takes a reference to
1066 * each struct page that each user address corresponds to at a given
1067 * instant. That is, it takes the page that would be accessed if a user
1068 * thread accesses the given user virtual address at that instant.
1070 * This does not guarantee that the page exists in the user mappings when
1071 * __get_user_pages returns, and there may even be a completely different
1072 * page there in some cases (eg. if mmapped pagecache has been invalidated
1073 * and subsequently re faulted). However it does guarantee that the page
1074 * won't be freed completely. And mostly callers simply care that the page
1075 * contains data that was valid *at some point in time*. Typically, an IO
1076 * or similar operation cannot guarantee anything stronger anyway because
1077 * locks can't be held over the syscall boundary.
1079 * If @gup_flags & FOLL_WRITE == 0, the page must not be written to. If
1080 * the page is written to, set_page_dirty (or set_page_dirty_lock, as
1081 * appropriate) must be called after the page is finished with, and
1082 * before put_page is called.
1084 * If @locked != NULL, *@locked will be set to 0 when mmap_lock is
1085 * released by an up_read(). That can happen if @gup_flags does not
1088 * A caller using such a combination of @locked and @gup_flags
1089 * must therefore hold the mmap_lock for reading only, and recognize
1090 * when it's been released. Otherwise, it must be held for either
1091 * reading or writing and will not be released.
1093 * In most cases, get_user_pages or get_user_pages_fast should be used
1094 * instead of __get_user_pages. __get_user_pages should be used only if
1095 * you need some special @gup_flags.
1097 static long __get_user_pages(struct mm_struct *mm,
1098 unsigned long start, unsigned long nr_pages,
1099 unsigned int gup_flags, struct page **pages,
1100 struct vm_area_struct **vmas, int *locked)
1102 long ret = 0, i = 0;
1103 struct vm_area_struct *vma = NULL;
1104 struct follow_page_context ctx = { NULL };
1109 start = untagged_addr(start);
1111 VM_BUG_ON(!!pages != !!(gup_flags & (FOLL_GET | FOLL_PIN)));
1114 * If FOLL_FORCE is set then do not force a full fault as the hinting
1115 * fault information is unrelated to the reference behaviour of a task
1116 * using the address space
1118 if (!(gup_flags & FOLL_FORCE))
1119 gup_flags |= FOLL_NUMA;
1123 unsigned int foll_flags = gup_flags;
1124 unsigned int page_increm;
1126 /* first iteration or cross vma bound */
1127 if (!vma || start >= vma->vm_end) {
1128 vma = find_extend_vma(mm, start);
1129 if (!vma && in_gate_area(mm, start)) {
1130 ret = get_gate_page(mm, start & PAGE_MASK,
1132 pages ? &pages[i] : NULL);
1143 ret = check_vma_flags(vma, gup_flags);
1147 if (is_vm_hugetlb_page(vma)) {
1148 i = follow_hugetlb_page(mm, vma, pages, vmas,
1149 &start, &nr_pages, i,
1151 if (locked && *locked == 0) {
1153 * We've got a VM_FAULT_RETRY
1154 * and we've lost mmap_lock.
1155 * We must stop here.
1157 BUG_ON(gup_flags & FOLL_NOWAIT);
1165 * If we have a pending SIGKILL, don't keep faulting pages and
1166 * potentially allocating memory.
1168 if (fatal_signal_pending(current)) {
1174 page = follow_page_mask(vma, start, foll_flags, &ctx);
1175 if (!page || PTR_ERR(page) == -EMLINK) {
1176 ret = faultin_page(vma, start, &foll_flags,
1177 PTR_ERR(page) == -EMLINK, locked);
1190 } else if (PTR_ERR(page) == -EEXIST) {
1192 * Proper page table entry exists, but no corresponding
1193 * struct page. If the caller expects **pages to be
1194 * filled in, bail out now, because that can't be done
1198 ret = PTR_ERR(page);
1203 } else if (IS_ERR(page)) {
1204 ret = PTR_ERR(page);
1209 flush_anon_page(vma, page, start);
1210 flush_dcache_page(page);
1218 page_increm = 1 + (~(start >> PAGE_SHIFT) & ctx.page_mask);
1219 if (page_increm > nr_pages)
1220 page_increm = nr_pages;
1222 start += page_increm * PAGE_SIZE;
1223 nr_pages -= page_increm;
1227 put_dev_pagemap(ctx.pgmap);
1231 static bool vma_permits_fault(struct vm_area_struct *vma,
1232 unsigned int fault_flags)
1234 bool write = !!(fault_flags & FAULT_FLAG_WRITE);
1235 bool foreign = !!(fault_flags & FAULT_FLAG_REMOTE);
1236 vm_flags_t vm_flags = write ? VM_WRITE : VM_READ;
1238 if (!(vm_flags & vma->vm_flags))
1242 * The architecture might have a hardware protection
1243 * mechanism other than read/write that can deny access.
1245 * gup always represents data access, not instruction
1246 * fetches, so execute=false here:
1248 if (!arch_vma_access_permitted(vma, write, false, foreign))
1255 * fixup_user_fault() - manually resolve a user page fault
1256 * @mm: mm_struct of target mm
1257 * @address: user address
1258 * @fault_flags:flags to pass down to handle_mm_fault()
1259 * @unlocked: did we unlock the mmap_lock while retrying, maybe NULL if caller
1260 * does not allow retry. If NULL, the caller must guarantee
1261 * that fault_flags does not contain FAULT_FLAG_ALLOW_RETRY.
1263 * This is meant to be called in the specific scenario where for locking reasons
1264 * we try to access user memory in atomic context (within a pagefault_disable()
1265 * section), this returns -EFAULT, and we want to resolve the user fault before
1268 * Typically this is meant to be used by the futex code.
1270 * The main difference with get_user_pages() is that this function will
1271 * unconditionally call handle_mm_fault() which will in turn perform all the
1272 * necessary SW fixup of the dirty and young bits in the PTE, while
1273 * get_user_pages() only guarantees to update these in the struct page.
1275 * This is important for some architectures where those bits also gate the
1276 * access permission to the page because they are maintained in software. On
1277 * such architectures, gup() will not be enough to make a subsequent access
1280 * This function will not return with an unlocked mmap_lock. So it has not the
1281 * same semantics wrt the @mm->mmap_lock as does filemap_fault().
1283 int fixup_user_fault(struct mm_struct *mm,
1284 unsigned long address, unsigned int fault_flags,
1287 struct vm_area_struct *vma;
1290 address = untagged_addr(address);
1293 fault_flags |= FAULT_FLAG_ALLOW_RETRY | FAULT_FLAG_KILLABLE;
1296 vma = find_extend_vma(mm, address);
1297 if (!vma || address < vma->vm_start)
1300 if (!vma_permits_fault(vma, fault_flags))
1303 if ((fault_flags & FAULT_FLAG_KILLABLE) &&
1304 fatal_signal_pending(current))
1307 ret = handle_mm_fault(vma, address, fault_flags, NULL);
1308 if (ret & VM_FAULT_ERROR) {
1309 int err = vm_fault_to_errno(ret, 0);
1316 if (ret & VM_FAULT_RETRY) {
1319 fault_flags |= FAULT_FLAG_TRIED;
1325 EXPORT_SYMBOL_GPL(fixup_user_fault);
1328 * Please note that this function, unlike __get_user_pages will not
1329 * return 0 for nr_pages > 0 without FOLL_NOWAIT
1331 static __always_inline long __get_user_pages_locked(struct mm_struct *mm,
1332 unsigned long start,
1333 unsigned long nr_pages,
1334 struct page **pages,
1335 struct vm_area_struct **vmas,
1339 long ret, pages_done;
1343 /* if VM_FAULT_RETRY can be returned, vmas become invalid */
1345 /* check caller initialized locked */
1346 BUG_ON(*locked != 1);
1349 if (flags & FOLL_PIN)
1350 mm_set_has_pinned_flag(&mm->flags);
1353 * FOLL_PIN and FOLL_GET are mutually exclusive. Traditional behavior
1354 * is to set FOLL_GET if the caller wants pages[] filled in (but has
1355 * carelessly failed to specify FOLL_GET), so keep doing that, but only
1356 * for FOLL_GET, not for the newer FOLL_PIN.
1358 * FOLL_PIN always expects pages to be non-null, but no need to assert
1359 * that here, as any failures will be obvious enough.
1361 if (pages && !(flags & FOLL_PIN))
1365 lock_dropped = false;
1367 ret = __get_user_pages(mm, start, nr_pages, flags, pages,
1370 /* VM_FAULT_RETRY couldn't trigger, bypass */
1373 /* VM_FAULT_RETRY cannot return errors */
1376 BUG_ON(ret >= nr_pages);
1387 * VM_FAULT_RETRY didn't trigger or it was a
1395 * VM_FAULT_RETRY triggered, so seek to the faulting offset.
1396 * For the prefault case (!pages) we only update counts.
1400 start += ret << PAGE_SHIFT;
1401 lock_dropped = true;
1405 * Repeat on the address that fired VM_FAULT_RETRY
1406 * with both FAULT_FLAG_ALLOW_RETRY and
1407 * FAULT_FLAG_TRIED. Note that GUP can be interrupted
1408 * by fatal signals, so we need to check it before we
1409 * start trying again otherwise it can loop forever.
1412 if (fatal_signal_pending(current)) {
1414 pages_done = -EINTR;
1418 ret = mmap_read_lock_killable(mm);
1427 ret = __get_user_pages(mm, start, 1, flags | FOLL_TRIED,
1428 pages, NULL, locked);
1430 /* Continue to retry until we succeeded */
1448 if (lock_dropped && *locked) {
1450 * We must let the caller know we temporarily dropped the lock
1451 * and so the critical section protected by it was lost.
1453 mmap_read_unlock(mm);
1460 * populate_vma_page_range() - populate a range of pages in the vma.
1462 * @start: start address
1464 * @locked: whether the mmap_lock is still held
1466 * This takes care of mlocking the pages too if VM_LOCKED is set.
1468 * Return either number of pages pinned in the vma, or a negative error
1471 * vma->vm_mm->mmap_lock must be held.
1473 * If @locked is NULL, it may be held for read or write and will
1476 * If @locked is non-NULL, it must held for read only and may be
1477 * released. If it's released, *@locked will be set to 0.
1479 long populate_vma_page_range(struct vm_area_struct *vma,
1480 unsigned long start, unsigned long end, int *locked)
1482 struct mm_struct *mm = vma->vm_mm;
1483 unsigned long nr_pages = (end - start) / PAGE_SIZE;
1487 VM_BUG_ON(!PAGE_ALIGNED(start));
1488 VM_BUG_ON(!PAGE_ALIGNED(end));
1489 VM_BUG_ON_VMA(start < vma->vm_start, vma);
1490 VM_BUG_ON_VMA(end > vma->vm_end, vma);
1491 mmap_assert_locked(mm);
1494 * Rightly or wrongly, the VM_LOCKONFAULT case has never used
1495 * faultin_page() to break COW, so it has no work to do here.
1497 if (vma->vm_flags & VM_LOCKONFAULT)
1500 gup_flags = FOLL_TOUCH;
1502 * We want to touch writable mappings with a write fault in order
1503 * to break COW, except for shared mappings because these don't COW
1504 * and we would not want to dirty them for nothing.
1506 if ((vma->vm_flags & (VM_WRITE | VM_SHARED)) == VM_WRITE)
1507 gup_flags |= FOLL_WRITE;
1510 * We want mlock to succeed for regions that have any permissions
1511 * other than PROT_NONE.
1513 if (vma_is_accessible(vma))
1514 gup_flags |= FOLL_FORCE;
1517 * We made sure addr is within a VMA, so the following will
1518 * not result in a stack expansion that recurses back here.
1520 ret = __get_user_pages(mm, start, nr_pages, gup_flags,
1521 NULL, NULL, locked);
1527 * faultin_vma_page_range() - populate (prefault) page tables inside the
1528 * given VMA range readable/writable
1530 * This takes care of mlocking the pages, too, if VM_LOCKED is set.
1533 * @start: start address
1535 * @write: whether to prefault readable or writable
1536 * @locked: whether the mmap_lock is still held
1538 * Returns either number of processed pages in the vma, or a negative error
1539 * code on error (see __get_user_pages()).
1541 * vma->vm_mm->mmap_lock must be held. The range must be page-aligned and
1542 * covered by the VMA.
1544 * If @locked is NULL, it may be held for read or write and will be unperturbed.
1546 * If @locked is non-NULL, it must held for read only and may be released. If
1547 * it's released, *@locked will be set to 0.
1549 long faultin_vma_page_range(struct vm_area_struct *vma, unsigned long start,
1550 unsigned long end, bool write, int *locked)
1552 struct mm_struct *mm = vma->vm_mm;
1553 unsigned long nr_pages = (end - start) / PAGE_SIZE;
1557 VM_BUG_ON(!PAGE_ALIGNED(start));
1558 VM_BUG_ON(!PAGE_ALIGNED(end));
1559 VM_BUG_ON_VMA(start < vma->vm_start, vma);
1560 VM_BUG_ON_VMA(end > vma->vm_end, vma);
1561 mmap_assert_locked(mm);
1564 * FOLL_TOUCH: Mark page accessed and thereby young; will also mark
1565 * the page dirty with FOLL_WRITE -- which doesn't make a
1566 * difference with !FOLL_FORCE, because the page is writable
1567 * in the page table.
1568 * FOLL_HWPOISON: Return -EHWPOISON instead of -EFAULT when we hit
1570 * !FOLL_FORCE: Require proper access permissions.
1572 gup_flags = FOLL_TOUCH | FOLL_HWPOISON;
1574 gup_flags |= FOLL_WRITE;
1577 * We want to report -EINVAL instead of -EFAULT for any permission
1578 * problems or incompatible mappings.
1580 if (check_vma_flags(vma, gup_flags))
1583 ret = __get_user_pages(mm, start, nr_pages, gup_flags,
1584 NULL, NULL, locked);
1590 * __mm_populate - populate and/or mlock pages within a range of address space.
1592 * This is used to implement mlock() and the MAP_POPULATE / MAP_LOCKED mmap
1593 * flags. VMAs must be already marked with the desired vm_flags, and
1594 * mmap_lock must not be held.
1596 int __mm_populate(unsigned long start, unsigned long len, int ignore_errors)
1598 struct mm_struct *mm = current->mm;
1599 unsigned long end, nstart, nend;
1600 struct vm_area_struct *vma = NULL;
1606 for (nstart = start; nstart < end; nstart = nend) {
1608 * We want to fault in pages for [nstart; end) address range.
1609 * Find first corresponding VMA.
1614 vma = find_vma(mm, nstart);
1615 } else if (nstart >= vma->vm_end)
1617 if (!vma || vma->vm_start >= end)
1620 * Set [nstart; nend) to intersection of desired address
1621 * range with the first VMA. Also, skip undesirable VMA types.
1623 nend = min(end, vma->vm_end);
1624 if (vma->vm_flags & (VM_IO | VM_PFNMAP))
1626 if (nstart < vma->vm_start)
1627 nstart = vma->vm_start;
1629 * Now fault in a range of pages. populate_vma_page_range()
1630 * double checks the vma flags, so that it won't mlock pages
1631 * if the vma was already munlocked.
1633 ret = populate_vma_page_range(vma, nstart, nend, &locked);
1635 if (ignore_errors) {
1637 continue; /* continue at next VMA */
1641 nend = nstart + ret * PAGE_SIZE;
1645 mmap_read_unlock(mm);
1646 return ret; /* 0 or negative error code */
1648 #else /* CONFIG_MMU */
1649 static long __get_user_pages_locked(struct mm_struct *mm, unsigned long start,
1650 unsigned long nr_pages, struct page **pages,
1651 struct vm_area_struct **vmas, int *locked,
1652 unsigned int foll_flags)
1654 struct vm_area_struct *vma;
1655 unsigned long vm_flags;
1658 /* calculate required read or write permissions.
1659 * If FOLL_FORCE is set, we only require the "MAY" flags.
1661 vm_flags = (foll_flags & FOLL_WRITE) ?
1662 (VM_WRITE | VM_MAYWRITE) : (VM_READ | VM_MAYREAD);
1663 vm_flags &= (foll_flags & FOLL_FORCE) ?
1664 (VM_MAYREAD | VM_MAYWRITE) : (VM_READ | VM_WRITE);
1666 for (i = 0; i < nr_pages; i++) {
1667 vma = find_vma(mm, start);
1669 goto finish_or_fault;
1671 /* protect what we can, including chardevs */
1672 if ((vma->vm_flags & (VM_IO | VM_PFNMAP)) ||
1673 !(vm_flags & vma->vm_flags))
1674 goto finish_or_fault;
1677 pages[i] = virt_to_page(start);
1683 start = (start + PAGE_SIZE) & PAGE_MASK;
1689 return i ? : -EFAULT;
1691 #endif /* !CONFIG_MMU */
1694 * fault_in_writeable - fault in userspace address range for writing
1695 * @uaddr: start of address range
1696 * @size: size of address range
1698 * Returns the number of bytes not faulted in (like copy_to_user() and
1699 * copy_from_user()).
1701 size_t fault_in_writeable(char __user *uaddr, size_t size)
1703 char __user *start = uaddr, *end;
1705 if (unlikely(size == 0))
1707 if (!user_write_access_begin(uaddr, size))
1709 if (!PAGE_ALIGNED(uaddr)) {
1710 unsafe_put_user(0, uaddr, out);
1711 uaddr = (char __user *)PAGE_ALIGN((unsigned long)uaddr);
1713 end = (char __user *)PAGE_ALIGN((unsigned long)start + size);
1714 if (unlikely(end < start))
1716 while (uaddr != end) {
1717 unsafe_put_user(0, uaddr, out);
1722 user_write_access_end();
1723 if (size > uaddr - start)
1724 return size - (uaddr - start);
1727 EXPORT_SYMBOL(fault_in_writeable);
1730 * fault_in_subpage_writeable - fault in an address range for writing
1731 * @uaddr: start of address range
1732 * @size: size of address range
1734 * Fault in a user address range for writing while checking for permissions at
1735 * sub-page granularity (e.g. arm64 MTE). This function should be used when
1736 * the caller cannot guarantee forward progress of a copy_to_user() loop.
1738 * Returns the number of bytes not faulted in (like copy_to_user() and
1739 * copy_from_user()).
1741 size_t fault_in_subpage_writeable(char __user *uaddr, size_t size)
1746 * Attempt faulting in at page granularity first for page table
1747 * permission checking. The arch-specific probe_subpage_writeable()
1748 * functions may not check for this.
1750 faulted_in = size - fault_in_writeable(uaddr, size);
1752 faulted_in -= probe_subpage_writeable(uaddr, faulted_in);
1754 return size - faulted_in;
1756 EXPORT_SYMBOL(fault_in_subpage_writeable);
1759 * fault_in_safe_writeable - fault in an address range for writing
1760 * @uaddr: start of address range
1761 * @size: length of address range
1763 * Faults in an address range for writing. This is primarily useful when we
1764 * already know that some or all of the pages in the address range aren't in
1767 * Unlike fault_in_writeable(), this function is non-destructive.
1769 * Note that we don't pin or otherwise hold the pages referenced that we fault
1770 * in. There's no guarantee that they'll stay in memory for any duration of
1773 * Returns the number of bytes not faulted in, like copy_to_user() and
1776 size_t fault_in_safe_writeable(const char __user *uaddr, size_t size)
1778 unsigned long start = (unsigned long)uaddr, end;
1779 struct mm_struct *mm = current->mm;
1780 bool unlocked = false;
1782 if (unlikely(size == 0))
1784 end = PAGE_ALIGN(start + size);
1790 if (fixup_user_fault(mm, start, FAULT_FLAG_WRITE, &unlocked))
1792 start = (start + PAGE_SIZE) & PAGE_MASK;
1793 } while (start != end);
1794 mmap_read_unlock(mm);
1796 if (size > (unsigned long)uaddr - start)
1797 return size - ((unsigned long)uaddr - start);
1800 EXPORT_SYMBOL(fault_in_safe_writeable);
1803 * fault_in_readable - fault in userspace address range for reading
1804 * @uaddr: start of user address range
1805 * @size: size of user address range
1807 * Returns the number of bytes not faulted in (like copy_to_user() and
1808 * copy_from_user()).
1810 size_t fault_in_readable(const char __user *uaddr, size_t size)
1812 const char __user *start = uaddr, *end;
1815 if (unlikely(size == 0))
1817 if (!user_read_access_begin(uaddr, size))
1819 if (!PAGE_ALIGNED(uaddr)) {
1820 unsafe_get_user(c, uaddr, out);
1821 uaddr = (const char __user *)PAGE_ALIGN((unsigned long)uaddr);
1823 end = (const char __user *)PAGE_ALIGN((unsigned long)start + size);
1824 if (unlikely(end < start))
1826 while (uaddr != end) {
1827 unsafe_get_user(c, uaddr, out);
1832 user_read_access_end();
1834 if (size > uaddr - start)
1835 return size - (uaddr - start);
1838 EXPORT_SYMBOL(fault_in_readable);
1841 * get_dump_page() - pin user page in memory while writing it to core dump
1842 * @addr: user address
1844 * Returns struct page pointer of user page pinned for dump,
1845 * to be freed afterwards by put_page().
1847 * Returns NULL on any kind of failure - a hole must then be inserted into
1848 * the corefile, to preserve alignment with its headers; and also returns
1849 * NULL wherever the ZERO_PAGE, or an anonymous pte_none, has been found -
1850 * allowing a hole to be left in the corefile to save disk space.
1852 * Called without mmap_lock (takes and releases the mmap_lock by itself).
1854 #ifdef CONFIG_ELF_CORE
1855 struct page *get_dump_page(unsigned long addr)
1857 struct mm_struct *mm = current->mm;
1862 if (mmap_read_lock_killable(mm))
1864 ret = __get_user_pages_locked(mm, addr, 1, &page, NULL, &locked,
1865 FOLL_FORCE | FOLL_DUMP | FOLL_GET);
1867 mmap_read_unlock(mm);
1868 return (ret == 1) ? page : NULL;
1870 #endif /* CONFIG_ELF_CORE */
1872 #ifdef CONFIG_MIGRATION
1874 * Check whether all pages are pinnable, if so return number of pages. If some
1875 * pages are not pinnable, migrate them, and unpin all pages. Return zero if
1876 * pages were migrated, or if some pages were not successfully isolated.
1877 * Return negative error if migration fails.
1879 static long check_and_migrate_movable_pages(unsigned long nr_pages,
1880 struct page **pages,
1881 unsigned int gup_flags)
1883 unsigned long isolation_error_count = 0, i;
1884 struct folio *prev_folio = NULL;
1885 LIST_HEAD(movable_page_list);
1886 bool drain_allow = true;
1889 for (i = 0; i < nr_pages; i++) {
1890 struct folio *folio = page_folio(pages[i]);
1892 if (folio == prev_folio)
1896 if (folio_is_pinnable(folio))
1900 * Try to move out any movable page before pinning the range.
1902 if (folio_test_hugetlb(folio)) {
1903 if (!isolate_huge_page(&folio->page,
1904 &movable_page_list))
1905 isolation_error_count++;
1909 if (!folio_test_lru(folio) && drain_allow) {
1910 lru_add_drain_all();
1911 drain_allow = false;
1914 if (folio_isolate_lru(folio)) {
1915 isolation_error_count++;
1918 list_add_tail(&folio->lru, &movable_page_list);
1919 node_stat_mod_folio(folio,
1920 NR_ISOLATED_ANON + folio_is_file_lru(folio),
1921 folio_nr_pages(folio));
1924 if (!list_empty(&movable_page_list) || isolation_error_count)
1928 * If list is empty, and no isolation errors, means that all pages are
1929 * in the correct zone.
1934 if (gup_flags & FOLL_PIN) {
1935 unpin_user_pages(pages, nr_pages);
1937 for (i = 0; i < nr_pages; i++)
1941 if (!list_empty(&movable_page_list)) {
1942 struct migration_target_control mtc = {
1943 .nid = NUMA_NO_NODE,
1944 .gfp_mask = GFP_USER | __GFP_NOWARN,
1947 ret = migrate_pages(&movable_page_list, alloc_migration_target,
1948 NULL, (unsigned long)&mtc, MIGRATE_SYNC,
1949 MR_LONGTERM_PIN, NULL);
1950 if (ret > 0) /* number of pages not migrated */
1954 if (ret && !list_empty(&movable_page_list))
1955 putback_movable_pages(&movable_page_list);
1959 static long check_and_migrate_movable_pages(unsigned long nr_pages,
1960 struct page **pages,
1961 unsigned int gup_flags)
1965 #endif /* CONFIG_MIGRATION */
1968 * __gup_longterm_locked() is a wrapper for __get_user_pages_locked which
1969 * allows us to process the FOLL_LONGTERM flag.
1971 static long __gup_longterm_locked(struct mm_struct *mm,
1972 unsigned long start,
1973 unsigned long nr_pages,
1974 struct page **pages,
1975 struct vm_area_struct **vmas,
1976 unsigned int gup_flags)
1981 if (!(gup_flags & FOLL_LONGTERM))
1982 return __get_user_pages_locked(mm, start, nr_pages, pages, vmas,
1984 flags = memalloc_pin_save();
1986 rc = __get_user_pages_locked(mm, start, nr_pages, pages, vmas,
1990 rc = check_and_migrate_movable_pages(rc, pages, gup_flags);
1992 memalloc_pin_restore(flags);
1997 static bool is_valid_gup_flags(unsigned int gup_flags)
2000 * FOLL_PIN must only be set internally by the pin_user_pages*() APIs,
2001 * never directly by the caller, so enforce that with an assertion:
2003 if (WARN_ON_ONCE(gup_flags & FOLL_PIN))
2006 * FOLL_PIN is a prerequisite to FOLL_LONGTERM. Another way of saying
2007 * that is, FOLL_LONGTERM is a specific case, more restrictive case of
2010 if (WARN_ON_ONCE(gup_flags & FOLL_LONGTERM))
2017 static long __get_user_pages_remote(struct mm_struct *mm,
2018 unsigned long start, unsigned long nr_pages,
2019 unsigned int gup_flags, struct page **pages,
2020 struct vm_area_struct **vmas, int *locked)
2023 * Parts of FOLL_LONGTERM behavior are incompatible with
2024 * FAULT_FLAG_ALLOW_RETRY because of the FS DAX check requirement on
2025 * vmas. However, this only comes up if locked is set, and there are
2026 * callers that do request FOLL_LONGTERM, but do not set locked. So,
2027 * allow what we can.
2029 if (gup_flags & FOLL_LONGTERM) {
2030 if (WARN_ON_ONCE(locked))
2033 * This will check the vmas (even if our vmas arg is NULL)
2034 * and return -ENOTSUPP if DAX isn't allowed in this case:
2036 return __gup_longterm_locked(mm, start, nr_pages, pages,
2037 vmas, gup_flags | FOLL_TOUCH |
2041 return __get_user_pages_locked(mm, start, nr_pages, pages, vmas,
2043 gup_flags | FOLL_TOUCH | FOLL_REMOTE);
2047 * get_user_pages_remote() - pin user pages in memory
2048 * @mm: mm_struct of target mm
2049 * @start: starting user address
2050 * @nr_pages: number of pages from start to pin
2051 * @gup_flags: flags modifying lookup behaviour
2052 * @pages: array that receives pointers to the pages pinned.
2053 * Should be at least nr_pages long. Or NULL, if caller
2054 * only intends to ensure the pages are faulted in.
2055 * @vmas: array of pointers to vmas corresponding to each page.
2056 * Or NULL if the caller does not require them.
2057 * @locked: pointer to lock flag indicating whether lock is held and
2058 * subsequently whether VM_FAULT_RETRY functionality can be
2059 * utilised. Lock must initially be held.
2061 * Returns either number of pages pinned (which may be less than the
2062 * number requested), or an error. Details about the return value:
2064 * -- If nr_pages is 0, returns 0.
2065 * -- If nr_pages is >0, but no pages were pinned, returns -errno.
2066 * -- If nr_pages is >0, and some pages were pinned, returns the number of
2067 * pages pinned. Again, this may be less than nr_pages.
2069 * The caller is responsible for releasing returned @pages, via put_page().
2071 * @vmas are valid only as long as mmap_lock is held.
2073 * Must be called with mmap_lock held for read or write.
2075 * get_user_pages_remote walks a process's page tables and takes a reference
2076 * to each struct page that each user address corresponds to at a given
2077 * instant. That is, it takes the page that would be accessed if a user
2078 * thread accesses the given user virtual address at that instant.
2080 * This does not guarantee that the page exists in the user mappings when
2081 * get_user_pages_remote returns, and there may even be a completely different
2082 * page there in some cases (eg. if mmapped pagecache has been invalidated
2083 * and subsequently re faulted). However it does guarantee that the page
2084 * won't be freed completely. And mostly callers simply care that the page
2085 * contains data that was valid *at some point in time*. Typically, an IO
2086 * or similar operation cannot guarantee anything stronger anyway because
2087 * locks can't be held over the syscall boundary.
2089 * If gup_flags & FOLL_WRITE == 0, the page must not be written to. If the page
2090 * is written to, set_page_dirty (or set_page_dirty_lock, as appropriate) must
2091 * be called after the page is finished with, and before put_page is called.
2093 * get_user_pages_remote is typically used for fewer-copy IO operations,
2094 * to get a handle on the memory by some means other than accesses
2095 * via the user virtual addresses. The pages may be submitted for
2096 * DMA to devices or accessed via their kernel linear mapping (via the
2097 * kmap APIs). Care should be taken to use the correct cache flushing APIs.
2099 * See also get_user_pages_fast, for performance critical applications.
2101 * get_user_pages_remote should be phased out in favor of
2102 * get_user_pages_locked|unlocked or get_user_pages_fast. Nothing
2103 * should use get_user_pages_remote because it cannot pass
2104 * FAULT_FLAG_ALLOW_RETRY to handle_mm_fault.
2106 long get_user_pages_remote(struct mm_struct *mm,
2107 unsigned long start, unsigned long nr_pages,
2108 unsigned int gup_flags, struct page **pages,
2109 struct vm_area_struct **vmas, int *locked)
2111 if (!is_valid_gup_flags(gup_flags))
2114 return __get_user_pages_remote(mm, start, nr_pages, gup_flags,
2115 pages, vmas, locked);
2117 EXPORT_SYMBOL(get_user_pages_remote);
2119 #else /* CONFIG_MMU */
2120 long get_user_pages_remote(struct mm_struct *mm,
2121 unsigned long start, unsigned long nr_pages,
2122 unsigned int gup_flags, struct page **pages,
2123 struct vm_area_struct **vmas, int *locked)
2128 static long __get_user_pages_remote(struct mm_struct *mm,
2129 unsigned long start, unsigned long nr_pages,
2130 unsigned int gup_flags, struct page **pages,
2131 struct vm_area_struct **vmas, int *locked)
2135 #endif /* !CONFIG_MMU */
2138 * get_user_pages() - pin user pages in memory
2139 * @start: starting user address
2140 * @nr_pages: number of pages from start to pin
2141 * @gup_flags: flags modifying lookup behaviour
2142 * @pages: array that receives pointers to the pages pinned.
2143 * Should be at least nr_pages long. Or NULL, if caller
2144 * only intends to ensure the pages are faulted in.
2145 * @vmas: array of pointers to vmas corresponding to each page.
2146 * Or NULL if the caller does not require them.
2148 * This is the same as get_user_pages_remote(), just with a less-flexible
2149 * calling convention where we assume that the mm being operated on belongs to
2150 * the current task, and doesn't allow passing of a locked parameter. We also
2151 * obviously don't pass FOLL_REMOTE in here.
2153 long get_user_pages(unsigned long start, unsigned long nr_pages,
2154 unsigned int gup_flags, struct page **pages,
2155 struct vm_area_struct **vmas)
2157 if (!is_valid_gup_flags(gup_flags))
2160 return __gup_longterm_locked(current->mm, start, nr_pages,
2161 pages, vmas, gup_flags | FOLL_TOUCH);
2163 EXPORT_SYMBOL(get_user_pages);
2166 * get_user_pages_unlocked() is suitable to replace the form:
2168 * mmap_read_lock(mm);
2169 * get_user_pages(mm, ..., pages, NULL);
2170 * mmap_read_unlock(mm);
2174 * get_user_pages_unlocked(mm, ..., pages);
2176 * It is functionally equivalent to get_user_pages_fast so
2177 * get_user_pages_fast should be used instead if specific gup_flags
2178 * (e.g. FOLL_FORCE) are not required.
2180 long get_user_pages_unlocked(unsigned long start, unsigned long nr_pages,
2181 struct page **pages, unsigned int gup_flags)
2183 struct mm_struct *mm = current->mm;
2188 * FIXME: Current FOLL_LONGTERM behavior is incompatible with
2189 * FAULT_FLAG_ALLOW_RETRY because of the FS DAX check requirement on
2190 * vmas. As there are no users of this flag in this call we simply
2191 * disallow this option for now.
2193 if (WARN_ON_ONCE(gup_flags & FOLL_LONGTERM))
2197 ret = __get_user_pages_locked(mm, start, nr_pages, pages, NULL,
2198 &locked, gup_flags | FOLL_TOUCH);
2200 mmap_read_unlock(mm);
2203 EXPORT_SYMBOL(get_user_pages_unlocked);
2208 * get_user_pages_fast attempts to pin user pages by walking the page
2209 * tables directly and avoids taking locks. Thus the walker needs to be
2210 * protected from page table pages being freed from under it, and should
2211 * block any THP splits.
2213 * One way to achieve this is to have the walker disable interrupts, and
2214 * rely on IPIs from the TLB flushing code blocking before the page table
2215 * pages are freed. This is unsuitable for architectures that do not need
2216 * to broadcast an IPI when invalidating TLBs.
2218 * Another way to achieve this is to batch up page table containing pages
2219 * belonging to more than one mm_user, then rcu_sched a callback to free those
2220 * pages. Disabling interrupts will allow the fast_gup walker to both block
2221 * the rcu_sched callback, and an IPI that we broadcast for splitting THPs
2222 * (which is a relatively rare event). The code below adopts this strategy.
2224 * Before activating this code, please be aware that the following assumptions
2225 * are currently made:
2227 * *) Either MMU_GATHER_RCU_TABLE_FREE is enabled, and tlb_remove_table() is used to
2228 * free pages containing page tables or TLB flushing requires IPI broadcast.
2230 * *) ptes can be read atomically by the architecture.
2232 * *) access_ok is sufficient to validate userspace address ranges.
2234 * The last two assumptions can be relaxed by the addition of helper functions.
2236 * This code is based heavily on the PowerPC implementation by Nick Piggin.
2238 #ifdef CONFIG_HAVE_FAST_GUP
2240 static void __maybe_unused undo_dev_pagemap(int *nr, int nr_start,
2242 struct page **pages)
2244 while ((*nr) - nr_start) {
2245 struct page *page = pages[--(*nr)];
2247 ClearPageReferenced(page);
2248 if (flags & FOLL_PIN)
2249 unpin_user_page(page);
2255 #ifdef CONFIG_ARCH_HAS_PTE_SPECIAL
2256 static int gup_pte_range(pmd_t pmd, unsigned long addr, unsigned long end,
2257 unsigned int flags, struct page **pages, int *nr)
2259 struct dev_pagemap *pgmap = NULL;
2260 int nr_start = *nr, ret = 0;
2263 ptem = ptep = pte_offset_map(&pmd, addr);
2265 pte_t pte = ptep_get_lockless(ptep);
2267 struct folio *folio;
2270 * Similar to the PMD case below, NUMA hinting must take slow
2271 * path using the pte_protnone check.
2273 if (pte_protnone(pte))
2276 if (!pte_access_permitted(pte, flags & FOLL_WRITE))
2279 if (pte_devmap(pte)) {
2280 if (unlikely(flags & FOLL_LONGTERM))
2283 pgmap = get_dev_pagemap(pte_pfn(pte), pgmap);
2284 if (unlikely(!pgmap)) {
2285 undo_dev_pagemap(nr, nr_start, flags, pages);
2288 } else if (pte_special(pte))
2291 VM_BUG_ON(!pfn_valid(pte_pfn(pte)));
2292 page = pte_page(pte);
2294 folio = try_grab_folio(page, 1, flags);
2298 if (unlikely(page_is_secretmem(page))) {
2299 gup_put_folio(folio, 1, flags);
2303 if (unlikely(pte_val(pte) != pte_val(*ptep))) {
2304 gup_put_folio(folio, 1, flags);
2308 if (!pte_write(pte) && gup_must_unshare(flags, page)) {
2309 gup_put_folio(folio, 1, flags);
2314 * We need to make the page accessible if and only if we are
2315 * going to access its content (the FOLL_PIN case). Please
2316 * see Documentation/core-api/pin_user_pages.rst for
2319 if (flags & FOLL_PIN) {
2320 ret = arch_make_page_accessible(page);
2322 gup_put_folio(folio, 1, flags);
2326 folio_set_referenced(folio);
2329 } while (ptep++, addr += PAGE_SIZE, addr != end);
2335 put_dev_pagemap(pgmap);
2342 * If we can't determine whether or not a pte is special, then fail immediately
2343 * for ptes. Note, we can still pin HugeTLB and THP as these are guaranteed not
2346 * For a futex to be placed on a THP tail page, get_futex_key requires a
2347 * get_user_pages_fast_only implementation that can pin pages. Thus it's still
2348 * useful to have gup_huge_pmd even if we can't operate on ptes.
2350 static int gup_pte_range(pmd_t pmd, unsigned long addr, unsigned long end,
2351 unsigned int flags, struct page **pages, int *nr)
2355 #endif /* CONFIG_ARCH_HAS_PTE_SPECIAL */
2357 #if defined(CONFIG_ARCH_HAS_PTE_DEVMAP) && defined(CONFIG_TRANSPARENT_HUGEPAGE)
2358 static int __gup_device_huge(unsigned long pfn, unsigned long addr,
2359 unsigned long end, unsigned int flags,
2360 struct page **pages, int *nr)
2363 struct dev_pagemap *pgmap = NULL;
2366 struct page *page = pfn_to_page(pfn);
2368 pgmap = get_dev_pagemap(pfn, pgmap);
2369 if (unlikely(!pgmap)) {
2370 undo_dev_pagemap(nr, nr_start, flags, pages);
2373 SetPageReferenced(page);
2375 if (unlikely(!try_grab_page(page, flags))) {
2376 undo_dev_pagemap(nr, nr_start, flags, pages);
2381 } while (addr += PAGE_SIZE, addr != end);
2383 put_dev_pagemap(pgmap);
2387 static int __gup_device_huge_pmd(pmd_t orig, pmd_t *pmdp, unsigned long addr,
2388 unsigned long end, unsigned int flags,
2389 struct page **pages, int *nr)
2391 unsigned long fault_pfn;
2394 fault_pfn = pmd_pfn(orig) + ((addr & ~PMD_MASK) >> PAGE_SHIFT);
2395 if (!__gup_device_huge(fault_pfn, addr, end, flags, pages, nr))
2398 if (unlikely(pmd_val(orig) != pmd_val(*pmdp))) {
2399 undo_dev_pagemap(nr, nr_start, flags, pages);
2405 static int __gup_device_huge_pud(pud_t orig, pud_t *pudp, unsigned long addr,
2406 unsigned long end, unsigned int flags,
2407 struct page **pages, int *nr)
2409 unsigned long fault_pfn;
2412 fault_pfn = pud_pfn(orig) + ((addr & ~PUD_MASK) >> PAGE_SHIFT);
2413 if (!__gup_device_huge(fault_pfn, addr, end, flags, pages, nr))
2416 if (unlikely(pud_val(orig) != pud_val(*pudp))) {
2417 undo_dev_pagemap(nr, nr_start, flags, pages);
2423 static int __gup_device_huge_pmd(pmd_t orig, pmd_t *pmdp, unsigned long addr,
2424 unsigned long end, unsigned int flags,
2425 struct page **pages, int *nr)
2431 static int __gup_device_huge_pud(pud_t pud, pud_t *pudp, unsigned long addr,
2432 unsigned long end, unsigned int flags,
2433 struct page **pages, int *nr)
2440 static int record_subpages(struct page *page, unsigned long addr,
2441 unsigned long end, struct page **pages)
2445 for (nr = 0; addr != end; nr++, addr += PAGE_SIZE)
2446 pages[nr] = nth_page(page, nr);
2451 #ifdef CONFIG_ARCH_HAS_HUGEPD
2452 static unsigned long hugepte_addr_end(unsigned long addr, unsigned long end,
2455 unsigned long __boundary = (addr + sz) & ~(sz-1);
2456 return (__boundary - 1 < end - 1) ? __boundary : end;
2459 static int gup_hugepte(pte_t *ptep, unsigned long sz, unsigned long addr,
2460 unsigned long end, unsigned int flags,
2461 struct page **pages, int *nr)
2463 unsigned long pte_end;
2465 struct folio *folio;
2469 pte_end = (addr + sz) & ~(sz-1);
2473 pte = huge_ptep_get(ptep);
2475 if (!pte_access_permitted(pte, flags & FOLL_WRITE))
2478 /* hugepages are never "special" */
2479 VM_BUG_ON(!pfn_valid(pte_pfn(pte)));
2481 page = nth_page(pte_page(pte), (addr & (sz - 1)) >> PAGE_SHIFT);
2482 refs = record_subpages(page, addr, end, pages + *nr);
2484 folio = try_grab_folio(page, refs, flags);
2488 if (unlikely(pte_val(pte) != pte_val(*ptep))) {
2489 gup_put_folio(folio, refs, flags);
2493 if (!pte_write(pte) && gup_must_unshare(flags, &folio->page)) {
2494 gup_put_folio(folio, refs, flags);
2499 folio_set_referenced(folio);
2503 static int gup_huge_pd(hugepd_t hugepd, unsigned long addr,
2504 unsigned int pdshift, unsigned long end, unsigned int flags,
2505 struct page **pages, int *nr)
2508 unsigned long sz = 1UL << hugepd_shift(hugepd);
2511 ptep = hugepte_offset(hugepd, addr, pdshift);
2513 next = hugepte_addr_end(addr, end, sz);
2514 if (!gup_hugepte(ptep, sz, addr, end, flags, pages, nr))
2516 } while (ptep++, addr = next, addr != end);
2521 static inline int gup_huge_pd(hugepd_t hugepd, unsigned long addr,
2522 unsigned int pdshift, unsigned long end, unsigned int flags,
2523 struct page **pages, int *nr)
2527 #endif /* CONFIG_ARCH_HAS_HUGEPD */
2529 static int gup_huge_pmd(pmd_t orig, pmd_t *pmdp, unsigned long addr,
2530 unsigned long end, unsigned int flags,
2531 struct page **pages, int *nr)
2534 struct folio *folio;
2537 if (!pmd_access_permitted(orig, flags & FOLL_WRITE))
2540 if (pmd_devmap(orig)) {
2541 if (unlikely(flags & FOLL_LONGTERM))
2543 return __gup_device_huge_pmd(orig, pmdp, addr, end, flags,
2547 page = nth_page(pmd_page(orig), (addr & ~PMD_MASK) >> PAGE_SHIFT);
2548 refs = record_subpages(page, addr, end, pages + *nr);
2550 folio = try_grab_folio(page, refs, flags);
2554 if (unlikely(pmd_val(orig) != pmd_val(*pmdp))) {
2555 gup_put_folio(folio, refs, flags);
2559 if (!pmd_write(orig) && gup_must_unshare(flags, &folio->page)) {
2560 gup_put_folio(folio, refs, flags);
2565 folio_set_referenced(folio);
2569 static int gup_huge_pud(pud_t orig, pud_t *pudp, unsigned long addr,
2570 unsigned long end, unsigned int flags,
2571 struct page **pages, int *nr)
2574 struct folio *folio;
2577 if (!pud_access_permitted(orig, flags & FOLL_WRITE))
2580 if (pud_devmap(orig)) {
2581 if (unlikely(flags & FOLL_LONGTERM))
2583 return __gup_device_huge_pud(orig, pudp, addr, end, flags,
2587 page = nth_page(pud_page(orig), (addr & ~PUD_MASK) >> PAGE_SHIFT);
2588 refs = record_subpages(page, addr, end, pages + *nr);
2590 folio = try_grab_folio(page, refs, flags);
2594 if (unlikely(pud_val(orig) != pud_val(*pudp))) {
2595 gup_put_folio(folio, refs, flags);
2599 if (!pud_write(orig) && gup_must_unshare(flags, &folio->page)) {
2600 gup_put_folio(folio, refs, flags);
2605 folio_set_referenced(folio);
2609 static int gup_huge_pgd(pgd_t orig, pgd_t *pgdp, unsigned long addr,
2610 unsigned long end, unsigned int flags,
2611 struct page **pages, int *nr)
2615 struct folio *folio;
2617 if (!pgd_access_permitted(orig, flags & FOLL_WRITE))
2620 BUILD_BUG_ON(pgd_devmap(orig));
2622 page = nth_page(pgd_page(orig), (addr & ~PGDIR_MASK) >> PAGE_SHIFT);
2623 refs = record_subpages(page, addr, end, pages + *nr);
2625 folio = try_grab_folio(page, refs, flags);
2629 if (unlikely(pgd_val(orig) != pgd_val(*pgdp))) {
2630 gup_put_folio(folio, refs, flags);
2635 folio_set_referenced(folio);
2639 static int gup_pmd_range(pud_t *pudp, pud_t pud, unsigned long addr, unsigned long end,
2640 unsigned int flags, struct page **pages, int *nr)
2645 pmdp = pmd_offset_lockless(pudp, pud, addr);
2647 pmd_t pmd = READ_ONCE(*pmdp);
2649 next = pmd_addr_end(addr, end);
2650 if (!pmd_present(pmd))
2653 if (unlikely(pmd_trans_huge(pmd) || pmd_huge(pmd) ||
2656 * NUMA hinting faults need to be handled in the GUP
2657 * slowpath for accounting purposes and so that they
2658 * can be serialised against THP migration.
2660 if (pmd_protnone(pmd))
2663 if (!gup_huge_pmd(pmd, pmdp, addr, next, flags,
2667 } else if (unlikely(is_hugepd(__hugepd(pmd_val(pmd))))) {
2669 * architecture have different format for hugetlbfs
2670 * pmd format and THP pmd format
2672 if (!gup_huge_pd(__hugepd(pmd_val(pmd)), addr,
2673 PMD_SHIFT, next, flags, pages, nr))
2675 } else if (!gup_pte_range(pmd, addr, next, flags, pages, nr))
2677 } while (pmdp++, addr = next, addr != end);
2682 static int gup_pud_range(p4d_t *p4dp, p4d_t p4d, unsigned long addr, unsigned long end,
2683 unsigned int flags, struct page **pages, int *nr)
2688 pudp = pud_offset_lockless(p4dp, p4d, addr);
2690 pud_t pud = READ_ONCE(*pudp);
2692 next = pud_addr_end(addr, end);
2693 if (unlikely(!pud_present(pud)))
2695 if (unlikely(pud_huge(pud))) {
2696 if (!gup_huge_pud(pud, pudp, addr, next, flags,
2699 } else if (unlikely(is_hugepd(__hugepd(pud_val(pud))))) {
2700 if (!gup_huge_pd(__hugepd(pud_val(pud)), addr,
2701 PUD_SHIFT, next, flags, pages, nr))
2703 } else if (!gup_pmd_range(pudp, pud, addr, next, flags, pages, nr))
2705 } while (pudp++, addr = next, addr != end);
2710 static int gup_p4d_range(pgd_t *pgdp, pgd_t pgd, unsigned long addr, unsigned long end,
2711 unsigned int flags, struct page **pages, int *nr)
2716 p4dp = p4d_offset_lockless(pgdp, pgd, addr);
2718 p4d_t p4d = READ_ONCE(*p4dp);
2720 next = p4d_addr_end(addr, end);
2723 BUILD_BUG_ON(p4d_huge(p4d));
2724 if (unlikely(is_hugepd(__hugepd(p4d_val(p4d))))) {
2725 if (!gup_huge_pd(__hugepd(p4d_val(p4d)), addr,
2726 P4D_SHIFT, next, flags, pages, nr))
2728 } else if (!gup_pud_range(p4dp, p4d, addr, next, flags, pages, nr))
2730 } while (p4dp++, addr = next, addr != end);
2735 static void gup_pgd_range(unsigned long addr, unsigned long end,
2736 unsigned int flags, struct page **pages, int *nr)
2741 pgdp = pgd_offset(current->mm, addr);
2743 pgd_t pgd = READ_ONCE(*pgdp);
2745 next = pgd_addr_end(addr, end);
2748 if (unlikely(pgd_huge(pgd))) {
2749 if (!gup_huge_pgd(pgd, pgdp, addr, next, flags,
2752 } else if (unlikely(is_hugepd(__hugepd(pgd_val(pgd))))) {
2753 if (!gup_huge_pd(__hugepd(pgd_val(pgd)), addr,
2754 PGDIR_SHIFT, next, flags, pages, nr))
2756 } else if (!gup_p4d_range(pgdp, pgd, addr, next, flags, pages, nr))
2758 } while (pgdp++, addr = next, addr != end);
2761 static inline void gup_pgd_range(unsigned long addr, unsigned long end,
2762 unsigned int flags, struct page **pages, int *nr)
2765 #endif /* CONFIG_HAVE_FAST_GUP */
2767 #ifndef gup_fast_permitted
2769 * Check if it's allowed to use get_user_pages_fast_only() for the range, or
2770 * we need to fall back to the slow version:
2772 static bool gup_fast_permitted(unsigned long start, unsigned long end)
2778 static int __gup_longterm_unlocked(unsigned long start, int nr_pages,
2779 unsigned int gup_flags, struct page **pages)
2784 * FIXME: FOLL_LONGTERM does not work with
2785 * get_user_pages_unlocked() (see comments in that function)
2787 if (gup_flags & FOLL_LONGTERM) {
2788 mmap_read_lock(current->mm);
2789 ret = __gup_longterm_locked(current->mm,
2791 pages, NULL, gup_flags);
2792 mmap_read_unlock(current->mm);
2794 ret = get_user_pages_unlocked(start, nr_pages,
2801 static unsigned long lockless_pages_from_mm(unsigned long start,
2803 unsigned int gup_flags,
2804 struct page **pages)
2806 unsigned long flags;
2810 if (!IS_ENABLED(CONFIG_HAVE_FAST_GUP) ||
2811 !gup_fast_permitted(start, end))
2814 if (gup_flags & FOLL_PIN) {
2815 seq = raw_read_seqcount(¤t->mm->write_protect_seq);
2821 * Disable interrupts. The nested form is used, in order to allow full,
2822 * general purpose use of this routine.
2824 * With interrupts disabled, we block page table pages from being freed
2825 * from under us. See struct mmu_table_batch comments in
2826 * include/asm-generic/tlb.h for more details.
2828 * We do not adopt an rcu_read_lock() here as we also want to block IPIs
2829 * that come from THPs splitting.
2831 local_irq_save(flags);
2832 gup_pgd_range(start, end, gup_flags, pages, &nr_pinned);
2833 local_irq_restore(flags);
2836 * When pinning pages for DMA there could be a concurrent write protect
2837 * from fork() via copy_page_range(), in this case always fail fast GUP.
2839 if (gup_flags & FOLL_PIN) {
2840 if (read_seqcount_retry(¤t->mm->write_protect_seq, seq)) {
2841 unpin_user_pages_lockless(pages, nr_pinned);
2844 sanity_check_pinned_pages(pages, nr_pinned);
2850 static int internal_get_user_pages_fast(unsigned long start,
2851 unsigned long nr_pages,
2852 unsigned int gup_flags,
2853 struct page **pages)
2855 unsigned long len, end;
2856 unsigned long nr_pinned;
2859 if (WARN_ON_ONCE(gup_flags & ~(FOLL_WRITE | FOLL_LONGTERM |
2860 FOLL_FORCE | FOLL_PIN | FOLL_GET |
2861 FOLL_FAST_ONLY | FOLL_NOFAULT)))
2864 if (gup_flags & FOLL_PIN)
2865 mm_set_has_pinned_flag(¤t->mm->flags);
2867 if (!(gup_flags & FOLL_FAST_ONLY))
2868 might_lock_read(¤t->mm->mmap_lock);
2870 start = untagged_addr(start) & PAGE_MASK;
2871 len = nr_pages << PAGE_SHIFT;
2872 if (check_add_overflow(start, len, &end))
2874 if (unlikely(!access_ok((void __user *)start, len)))
2877 nr_pinned = lockless_pages_from_mm(start, end, gup_flags, pages);
2878 if (nr_pinned == nr_pages || gup_flags & FOLL_FAST_ONLY)
2881 /* Slow path: try to get the remaining pages with get_user_pages */
2882 start += nr_pinned << PAGE_SHIFT;
2884 ret = __gup_longterm_unlocked(start, nr_pages - nr_pinned, gup_flags,
2888 * The caller has to unpin the pages we already pinned so
2889 * returning -errno is not an option
2895 return ret + nr_pinned;
2899 * get_user_pages_fast_only() - pin user pages in memory
2900 * @start: starting user address
2901 * @nr_pages: number of pages from start to pin
2902 * @gup_flags: flags modifying pin behaviour
2903 * @pages: array that receives pointers to the pages pinned.
2904 * Should be at least nr_pages long.
2906 * Like get_user_pages_fast() except it's IRQ-safe in that it won't fall back to
2908 * Note a difference with get_user_pages_fast: this always returns the
2909 * number of pages pinned, 0 if no pages were pinned.
2911 * If the architecture does not support this function, simply return with no
2914 * Careful, careful! COW breaking can go either way, so a non-write
2915 * access can get ambiguous page results. If you call this function without
2916 * 'write' set, you'd better be sure that you're ok with that ambiguity.
2918 int get_user_pages_fast_only(unsigned long start, int nr_pages,
2919 unsigned int gup_flags, struct page **pages)
2923 * Internally (within mm/gup.c), gup fast variants must set FOLL_GET,
2924 * because gup fast is always a "pin with a +1 page refcount" request.
2926 * FOLL_FAST_ONLY is required in order to match the API description of
2927 * this routine: no fall back to regular ("slow") GUP.
2929 gup_flags |= FOLL_GET | FOLL_FAST_ONLY;
2931 nr_pinned = internal_get_user_pages_fast(start, nr_pages, gup_flags,
2935 * As specified in the API description above, this routine is not
2936 * allowed to return negative values. However, the common core
2937 * routine internal_get_user_pages_fast() *can* return -errno.
2938 * Therefore, correct for that here:
2945 EXPORT_SYMBOL_GPL(get_user_pages_fast_only);
2948 * get_user_pages_fast() - pin user pages in memory
2949 * @start: starting user address
2950 * @nr_pages: number of pages from start to pin
2951 * @gup_flags: flags modifying pin behaviour
2952 * @pages: array that receives pointers to the pages pinned.
2953 * Should be at least nr_pages long.
2955 * Attempt to pin user pages in memory without taking mm->mmap_lock.
2956 * If not successful, it will fall back to taking the lock and
2957 * calling get_user_pages().
2959 * Returns number of pages pinned. This may be fewer than the number requested.
2960 * If nr_pages is 0 or negative, returns 0. If no pages were pinned, returns
2963 int get_user_pages_fast(unsigned long start, int nr_pages,
2964 unsigned int gup_flags, struct page **pages)
2966 if (!is_valid_gup_flags(gup_flags))
2970 * The caller may or may not have explicitly set FOLL_GET; either way is
2971 * OK. However, internally (within mm/gup.c), gup fast variants must set
2972 * FOLL_GET, because gup fast is always a "pin with a +1 page refcount"
2975 gup_flags |= FOLL_GET;
2976 return internal_get_user_pages_fast(start, nr_pages, gup_flags, pages);
2978 EXPORT_SYMBOL_GPL(get_user_pages_fast);
2981 * pin_user_pages_fast() - pin user pages in memory without taking locks
2983 * @start: starting user address
2984 * @nr_pages: number of pages from start to pin
2985 * @gup_flags: flags modifying pin behaviour
2986 * @pages: array that receives pointers to the pages pinned.
2987 * Should be at least nr_pages long.
2989 * Nearly the same as get_user_pages_fast(), except that FOLL_PIN is set. See
2990 * get_user_pages_fast() for documentation on the function arguments, because
2991 * the arguments here are identical.
2993 * FOLL_PIN means that the pages must be released via unpin_user_page(). Please
2994 * see Documentation/core-api/pin_user_pages.rst for further details.
2996 int pin_user_pages_fast(unsigned long start, int nr_pages,
2997 unsigned int gup_flags, struct page **pages)
2999 /* FOLL_GET and FOLL_PIN are mutually exclusive. */
3000 if (WARN_ON_ONCE(gup_flags & FOLL_GET))
3003 if (WARN_ON_ONCE(!pages))
3006 gup_flags |= FOLL_PIN;
3007 return internal_get_user_pages_fast(start, nr_pages, gup_flags, pages);
3009 EXPORT_SYMBOL_GPL(pin_user_pages_fast);
3012 * This is the FOLL_PIN equivalent of get_user_pages_fast_only(). Behavior
3013 * is the same, except that this one sets FOLL_PIN instead of FOLL_GET.
3015 * The API rules are the same, too: no negative values may be returned.
3017 int pin_user_pages_fast_only(unsigned long start, int nr_pages,
3018 unsigned int gup_flags, struct page **pages)
3023 * FOLL_GET and FOLL_PIN are mutually exclusive. Note that the API
3024 * rules require returning 0, rather than -errno:
3026 if (WARN_ON_ONCE(gup_flags & FOLL_GET))
3029 if (WARN_ON_ONCE(!pages))
3032 * FOLL_FAST_ONLY is required in order to match the API description of
3033 * this routine: no fall back to regular ("slow") GUP.
3035 gup_flags |= (FOLL_PIN | FOLL_FAST_ONLY);
3036 nr_pinned = internal_get_user_pages_fast(start, nr_pages, gup_flags,
3039 * This routine is not allowed to return negative values. However,
3040 * internal_get_user_pages_fast() *can* return -errno. Therefore,
3041 * correct for that here:
3048 EXPORT_SYMBOL_GPL(pin_user_pages_fast_only);
3051 * pin_user_pages_remote() - pin pages of a remote process
3053 * @mm: mm_struct of target mm
3054 * @start: starting user address
3055 * @nr_pages: number of pages from start to pin
3056 * @gup_flags: flags modifying lookup behaviour
3057 * @pages: array that receives pointers to the pages pinned.
3058 * Should be at least nr_pages long.
3059 * @vmas: array of pointers to vmas corresponding to each page.
3060 * Or NULL if the caller does not require them.
3061 * @locked: pointer to lock flag indicating whether lock is held and
3062 * subsequently whether VM_FAULT_RETRY functionality can be
3063 * utilised. Lock must initially be held.
3065 * Nearly the same as get_user_pages_remote(), except that FOLL_PIN is set. See
3066 * get_user_pages_remote() for documentation on the function arguments, because
3067 * the arguments here are identical.
3069 * FOLL_PIN means that the pages must be released via unpin_user_page(). Please
3070 * see Documentation/core-api/pin_user_pages.rst for details.
3072 long pin_user_pages_remote(struct mm_struct *mm,
3073 unsigned long start, unsigned long nr_pages,
3074 unsigned int gup_flags, struct page **pages,
3075 struct vm_area_struct **vmas, int *locked)
3077 /* FOLL_GET and FOLL_PIN are mutually exclusive. */
3078 if (WARN_ON_ONCE(gup_flags & FOLL_GET))
3081 if (WARN_ON_ONCE(!pages))
3084 gup_flags |= FOLL_PIN;
3085 return __get_user_pages_remote(mm, start, nr_pages, gup_flags,
3086 pages, vmas, locked);
3088 EXPORT_SYMBOL(pin_user_pages_remote);
3091 * pin_user_pages() - pin user pages in memory for use by other devices
3093 * @start: starting user address
3094 * @nr_pages: number of pages from start to pin
3095 * @gup_flags: flags modifying lookup behaviour
3096 * @pages: array that receives pointers to the pages pinned.
3097 * Should be at least nr_pages long.
3098 * @vmas: array of pointers to vmas corresponding to each page.
3099 * Or NULL if the caller does not require them.
3101 * Nearly the same as get_user_pages(), except that FOLL_TOUCH is not set, and
3104 * FOLL_PIN means that the pages must be released via unpin_user_page(). Please
3105 * see Documentation/core-api/pin_user_pages.rst for details.
3107 long pin_user_pages(unsigned long start, unsigned long nr_pages,
3108 unsigned int gup_flags, struct page **pages,
3109 struct vm_area_struct **vmas)
3111 /* FOLL_GET and FOLL_PIN are mutually exclusive. */
3112 if (WARN_ON_ONCE(gup_flags & FOLL_GET))
3115 if (WARN_ON_ONCE(!pages))
3118 gup_flags |= FOLL_PIN;
3119 return __gup_longterm_locked(current->mm, start, nr_pages,
3120 pages, vmas, gup_flags);
3122 EXPORT_SYMBOL(pin_user_pages);
3125 * pin_user_pages_unlocked() is the FOLL_PIN variant of
3126 * get_user_pages_unlocked(). Behavior is the same, except that this one sets
3127 * FOLL_PIN and rejects FOLL_GET.
3129 long pin_user_pages_unlocked(unsigned long start, unsigned long nr_pages,
3130 struct page **pages, unsigned int gup_flags)
3132 /* FOLL_GET and FOLL_PIN are mutually exclusive. */
3133 if (WARN_ON_ONCE(gup_flags & FOLL_GET))
3136 if (WARN_ON_ONCE(!pages))
3139 gup_flags |= FOLL_PIN;
3140 return get_user_pages_unlocked(start, nr_pages, pages, gup_flags);
3142 EXPORT_SYMBOL(pin_user_pages_unlocked);