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
));
47 /* Equivalent to calling put_page() @refs times. */
48 static void put_page_refs(struct page
*page
, int refs
)
50 #ifdef CONFIG_DEBUG_VM
51 if (VM_WARN_ON_ONCE_PAGE(page_ref_count(page
) < refs
, page
))
56 * Calling put_page() for each ref is unnecessarily slow. Only the last
57 * ref needs a put_page().
60 page_ref_sub(page
, refs
- 1);
65 * Return the compound head page with ref appropriately incremented,
66 * or NULL if that failed.
68 static inline struct page
*try_get_compound_head(struct page
*page
, int refs
)
70 struct page
*head
= compound_head(page
);
72 if (WARN_ON_ONCE(page_ref_count(head
) < 0))
74 if (unlikely(!page_cache_add_speculative(head
, refs
)))
78 * At this point we have a stable reference to the head page; but it
79 * could be that between the compound_head() lookup and the refcount
80 * increment, the compound page was split, in which case we'd end up
81 * holding a reference on a page that has nothing to do with the page
82 * we were given anymore.
83 * So now that the head page is stable, recheck that the pages still
86 if (unlikely(compound_head(page
) != head
)) {
87 put_page_refs(head
, refs
);
95 * try_grab_compound_head() - attempt to elevate a page's refcount, by a
96 * flags-dependent amount.
98 * "grab" names in this file mean, "look at flags to decide whether to use
99 * FOLL_PIN or FOLL_GET behavior, when incrementing the page's refcount.
101 * Either FOLL_PIN or FOLL_GET (or neither) must be set, but not both at the
102 * same time. (That's true throughout the get_user_pages*() and
103 * pin_user_pages*() APIs.) Cases:
105 * FOLL_GET: page's refcount will be incremented by 1.
106 * FOLL_PIN: page's refcount will be incremented by GUP_PIN_COUNTING_BIAS.
108 * Return: head page (with refcount appropriately incremented) for success, or
109 * NULL upon failure. If neither FOLL_GET nor FOLL_PIN was set, that's
110 * considered failure, and furthermore, a likely bug in the caller, so a warning
113 static __maybe_unused
struct page
*try_grab_compound_head(struct page
*page
,
117 if (flags
& FOLL_GET
)
118 return try_get_compound_head(page
, refs
);
119 else if (flags
& FOLL_PIN
) {
120 int orig_refs
= refs
;
123 * Can't do FOLL_LONGTERM + FOLL_PIN with CMA in the gup fast
124 * path, so fail and let the caller fall back to the slow path.
126 if (unlikely(flags
& FOLL_LONGTERM
) &&
127 is_migrate_cma_page(page
))
131 * CAUTION: Don't use compound_head() on the page before this
132 * point, the result won't be stable.
134 page
= try_get_compound_head(page
, refs
);
139 * When pinning a compound page of order > 1 (which is what
140 * hpage_pincount_available() checks for), use an exact count to
141 * track it, via hpage_pincount_add/_sub().
143 * However, be sure to *also* increment the normal page refcount
144 * field at least once, so that the page really is pinned.
146 if (hpage_pincount_available(page
))
147 hpage_pincount_add(page
, refs
);
149 page_ref_add(page
, refs
* (GUP_PIN_COUNTING_BIAS
- 1));
151 mod_node_page_state(page_pgdat(page
), NR_FOLL_PIN_ACQUIRED
,
161 static void put_compound_head(struct page
*page
, int refs
, unsigned int flags
)
163 if (flags
& FOLL_PIN
) {
164 mod_node_page_state(page_pgdat(page
), NR_FOLL_PIN_RELEASED
,
167 if (hpage_pincount_available(page
))
168 hpage_pincount_sub(page
, refs
);
170 refs
*= GUP_PIN_COUNTING_BIAS
;
173 put_page_refs(page
, refs
);
177 * try_grab_page() - elevate a page's refcount by a flag-dependent amount
179 * This might not do anything at all, depending on the flags argument.
181 * "grab" names in this file mean, "look at flags to decide whether to use
182 * FOLL_PIN or FOLL_GET behavior, when incrementing the page's refcount.
184 * @page: pointer to page to be grabbed
185 * @flags: gup flags: these are the FOLL_* flag values.
187 * Either FOLL_PIN or FOLL_GET (or neither) may be set, but not both at the same
190 * FOLL_GET: page's refcount will be incremented by 1.
191 * FOLL_PIN: page's refcount will be incremented by GUP_PIN_COUNTING_BIAS.
193 * Return: true for success, or if no action was required (if neither FOLL_PIN
194 * nor FOLL_GET was set, nothing is done). False for failure: FOLL_GET or
195 * FOLL_PIN was set, but the page could not be grabbed.
197 bool __must_check
try_grab_page(struct page
*page
, unsigned int flags
)
199 WARN_ON_ONCE((flags
& (FOLL_GET
| FOLL_PIN
)) == (FOLL_GET
| FOLL_PIN
));
201 if (flags
& FOLL_GET
)
202 return try_get_page(page
);
203 else if (flags
& FOLL_PIN
) {
206 page
= compound_head(page
);
208 if (WARN_ON_ONCE(page_ref_count(page
) <= 0))
211 if (hpage_pincount_available(page
))
212 hpage_pincount_add(page
, 1);
214 refs
= GUP_PIN_COUNTING_BIAS
;
217 * Similar to try_grab_compound_head(): even if using the
218 * hpage_pincount_add/_sub() routines, be sure to
219 * *also* increment the normal page refcount field at least
220 * once, so that the page really is pinned.
222 page_ref_add(page
, refs
);
224 mod_node_page_state(page_pgdat(page
), NR_FOLL_PIN_ACQUIRED
, 1);
231 * unpin_user_page() - release a dma-pinned page
232 * @page: pointer to page to be released
234 * Pages that were pinned via pin_user_pages*() must be released via either
235 * unpin_user_page(), or one of the unpin_user_pages*() routines. This is so
236 * that such pages can be separately tracked and uniquely handled. In
237 * particular, interactions with RDMA and filesystems need special handling.
239 void unpin_user_page(struct page
*page
)
241 put_compound_head(compound_head(page
), 1, FOLL_PIN
);
243 EXPORT_SYMBOL(unpin_user_page
);
246 * unpin_user_pages_dirty_lock() - release and optionally dirty gup-pinned pages
247 * @pages: array of pages to be maybe marked dirty, and definitely released.
248 * @npages: number of pages in the @pages array.
249 * @make_dirty: whether to mark the pages dirty
251 * "gup-pinned page" refers to a page that has had one of the get_user_pages()
252 * variants called on that page.
254 * For each page in the @pages array, make that page (or its head page, if a
255 * compound page) dirty, if @make_dirty is true, and if the page was previously
256 * listed as clean. In any case, releases all pages using unpin_user_page(),
257 * possibly via unpin_user_pages(), for the non-dirty case.
259 * Please see the unpin_user_page() documentation for details.
261 * set_page_dirty_lock() is used internally. If instead, set_page_dirty() is
262 * required, then the caller should a) verify that this is really correct,
263 * because _lock() is usually required, and b) hand code it:
264 * set_page_dirty_lock(), unpin_user_page().
267 void unpin_user_pages_dirty_lock(struct page
**pages
, unsigned long npages
,
273 * TODO: this can be optimized for huge pages: if a series of pages is
274 * physically contiguous and part of the same compound page, then a
275 * single operation to the head page should suffice.
279 unpin_user_pages(pages
, npages
);
283 for (index
= 0; index
< npages
; index
++) {
284 struct page
*page
= compound_head(pages
[index
]);
286 * Checking PageDirty at this point may race with
287 * clear_page_dirty_for_io(), but that's OK. Two key
290 * 1) This code sees the page as already dirty, so it
291 * skips the call to set_page_dirty(). That could happen
292 * because clear_page_dirty_for_io() called
293 * page_mkclean(), followed by set_page_dirty().
294 * However, now the page is going to get written back,
295 * which meets the original intention of setting it
296 * dirty, so all is well: clear_page_dirty_for_io() goes
297 * on to call TestClearPageDirty(), and write the page
300 * 2) This code sees the page as clean, so it calls
301 * set_page_dirty(). The page stays dirty, despite being
302 * written back, so it gets written back again in the
303 * next writeback cycle. This is harmless.
305 if (!PageDirty(page
))
306 set_page_dirty_lock(page
);
307 unpin_user_page(page
);
310 EXPORT_SYMBOL(unpin_user_pages_dirty_lock
);
313 * unpin_user_pages() - release an array of gup-pinned pages.
314 * @pages: array of pages to be marked dirty and released.
315 * @npages: number of pages in the @pages array.
317 * For each page in the @pages array, release the page using unpin_user_page().
319 * Please see the unpin_user_page() documentation for details.
321 void unpin_user_pages(struct page
**pages
, unsigned long npages
)
326 * If this WARN_ON() fires, then the system *might* be leaking pages (by
327 * leaving them pinned), but probably not. More likely, gup/pup returned
328 * a hard -ERRNO error to the caller, who erroneously passed it here.
330 if (WARN_ON(IS_ERR_VALUE(npages
)))
333 * TODO: this can be optimized for huge pages: if a series of pages is
334 * physically contiguous and part of the same compound page, then a
335 * single operation to the head page should suffice.
337 for (index
= 0; index
< npages
; index
++)
338 unpin_user_page(pages
[index
]);
340 EXPORT_SYMBOL(unpin_user_pages
);
343 static struct page
*no_page_table(struct vm_area_struct
*vma
,
347 * When core dumping an enormous anonymous area that nobody
348 * has touched so far, we don't want to allocate unnecessary pages or
349 * page tables. Return error instead of NULL to skip handle_mm_fault,
350 * then get_dump_page() will return NULL to leave a hole in the dump.
351 * But we can only make this optimization where a hole would surely
352 * be zero-filled if handle_mm_fault() actually did handle it.
354 if ((flags
& FOLL_DUMP
) &&
355 (vma_is_anonymous(vma
) || !vma
->vm_ops
->fault
))
356 return ERR_PTR(-EFAULT
);
360 static int follow_pfn_pte(struct vm_area_struct
*vma
, unsigned long address
,
361 pte_t
*pte
, unsigned int flags
)
363 /* No page to get reference */
364 if (flags
& FOLL_GET
)
367 if (flags
& FOLL_TOUCH
) {
370 if (flags
& FOLL_WRITE
)
371 entry
= pte_mkdirty(entry
);
372 entry
= pte_mkyoung(entry
);
374 if (!pte_same(*pte
, entry
)) {
375 set_pte_at(vma
->vm_mm
, address
, pte
, entry
);
376 update_mmu_cache(vma
, address
, pte
);
380 /* Proper page table entry exists, but no corresponding struct page */
385 * FOLL_FORCE can write to even unwritable pte's, but only
386 * after we've gone through a COW cycle and they are dirty.
388 static inline bool can_follow_write_pte(pte_t pte
, unsigned int flags
)
390 return pte_write(pte
) ||
391 ((flags
& FOLL_FORCE
) && (flags
& FOLL_COW
) && pte_dirty(pte
));
394 static struct page
*follow_page_pte(struct vm_area_struct
*vma
,
395 unsigned long address
, pmd_t
*pmd
, unsigned int flags
,
396 struct dev_pagemap
**pgmap
)
398 struct mm_struct
*mm
= vma
->vm_mm
;
404 /* FOLL_GET and FOLL_PIN are mutually exclusive. */
405 if (WARN_ON_ONCE((flags
& (FOLL_PIN
| FOLL_GET
)) ==
406 (FOLL_PIN
| FOLL_GET
)))
407 return ERR_PTR(-EINVAL
);
409 if (unlikely(pmd_bad(*pmd
)))
410 return no_page_table(vma
, flags
);
412 ptep
= pte_offset_map_lock(mm
, pmd
, address
, &ptl
);
414 if (!pte_present(pte
)) {
417 * KSM's break_ksm() relies upon recognizing a ksm page
418 * even while it is being migrated, so for that case we
419 * need migration_entry_wait().
421 if (likely(!(flags
& FOLL_MIGRATION
)))
425 entry
= pte_to_swp_entry(pte
);
426 if (!is_migration_entry(entry
))
428 pte_unmap_unlock(ptep
, ptl
);
429 migration_entry_wait(mm
, pmd
, address
);
432 if ((flags
& FOLL_NUMA
) && pte_protnone(pte
))
434 if ((flags
& FOLL_WRITE
) && !can_follow_write_pte(pte
, flags
)) {
435 pte_unmap_unlock(ptep
, ptl
);
439 page
= vm_normal_page(vma
, address
, pte
);
440 if (!page
&& pte_devmap(pte
) && (flags
& (FOLL_GET
| FOLL_PIN
))) {
442 * Only return device mapping pages in the FOLL_GET or FOLL_PIN
443 * case since they are only valid while holding the pgmap
446 *pgmap
= get_dev_pagemap(pte_pfn(pte
), *pgmap
);
448 page
= pte_page(pte
);
451 } else if (unlikely(!page
)) {
452 if (flags
& FOLL_DUMP
) {
453 /* Avoid special (like zero) pages in core dumps */
454 page
= ERR_PTR(-EFAULT
);
458 if (is_zero_pfn(pte_pfn(pte
))) {
459 page
= pte_page(pte
);
461 ret
= follow_pfn_pte(vma
, address
, ptep
, flags
);
467 if (flags
& FOLL_SPLIT
&& PageTransCompound(page
)) {
469 pte_unmap_unlock(ptep
, ptl
);
471 ret
= split_huge_page(page
);
479 /* try_grab_page() does nothing unless FOLL_GET or FOLL_PIN is set. */
480 if (unlikely(!try_grab_page(page
, flags
))) {
481 page
= ERR_PTR(-ENOMEM
);
485 * We need to make the page accessible if and only if we are going
486 * to access its content (the FOLL_PIN case). Please see
487 * Documentation/core-api/pin_user_pages.rst for details.
489 if (flags
& FOLL_PIN
) {
490 ret
= arch_make_page_accessible(page
);
492 unpin_user_page(page
);
497 if (flags
& FOLL_TOUCH
) {
498 if ((flags
& FOLL_WRITE
) &&
499 !pte_dirty(pte
) && !PageDirty(page
))
500 set_page_dirty(page
);
502 * pte_mkyoung() would be more correct here, but atomic care
503 * is needed to avoid losing the dirty bit: it is easier to use
504 * mark_page_accessed().
506 mark_page_accessed(page
);
508 if ((flags
& FOLL_MLOCK
) && (vma
->vm_flags
& VM_LOCKED
)) {
509 /* Do not mlock pte-mapped THP */
510 if (PageTransCompound(page
))
514 * The preliminary mapping check is mainly to avoid the
515 * pointless overhead of lock_page on the ZERO_PAGE
516 * which might bounce very badly if there is contention.
518 * If the page is already locked, we don't need to
519 * handle it now - vmscan will handle it later if and
520 * when it attempts to reclaim the page.
522 if (page
->mapping
&& trylock_page(page
)) {
523 lru_add_drain(); /* push cached pages to LRU */
525 * Because we lock page here, and migration is
526 * blocked by the pte's page reference, and we
527 * know the page is still mapped, we don't even
528 * need to check for file-cache page truncation.
530 mlock_vma_page(page
);
535 pte_unmap_unlock(ptep
, ptl
);
538 pte_unmap_unlock(ptep
, ptl
);
541 return no_page_table(vma
, flags
);
544 static struct page
*follow_pmd_mask(struct vm_area_struct
*vma
,
545 unsigned long address
, pud_t
*pudp
,
547 struct follow_page_context
*ctx
)
552 struct mm_struct
*mm
= vma
->vm_mm
;
554 pmd
= pmd_offset(pudp
, address
);
556 * The READ_ONCE() will stabilize the pmdval in a register or
557 * on the stack so that it will stop changing under the code.
559 pmdval
= READ_ONCE(*pmd
);
560 if (pmd_none(pmdval
))
561 return no_page_table(vma
, flags
);
562 if (pmd_huge(pmdval
) && is_vm_hugetlb_page(vma
)) {
563 page
= follow_huge_pmd(mm
, address
, pmd
, flags
);
566 return no_page_table(vma
, flags
);
568 if (is_hugepd(__hugepd(pmd_val(pmdval
)))) {
569 page
= follow_huge_pd(vma
, address
,
570 __hugepd(pmd_val(pmdval
)), flags
,
574 return no_page_table(vma
, flags
);
577 if (!pmd_present(pmdval
)) {
578 if (likely(!(flags
& FOLL_MIGRATION
)))
579 return no_page_table(vma
, flags
);
580 VM_BUG_ON(thp_migration_supported() &&
581 !is_pmd_migration_entry(pmdval
));
582 if (is_pmd_migration_entry(pmdval
))
583 pmd_migration_entry_wait(mm
, pmd
);
584 pmdval
= READ_ONCE(*pmd
);
586 * MADV_DONTNEED may convert the pmd to null because
587 * mmap_lock is held in read mode
589 if (pmd_none(pmdval
))
590 return no_page_table(vma
, flags
);
593 if (pmd_devmap(pmdval
)) {
594 ptl
= pmd_lock(mm
, pmd
);
595 page
= follow_devmap_pmd(vma
, address
, pmd
, flags
, &ctx
->pgmap
);
600 if (likely(!pmd_trans_huge(pmdval
)))
601 return follow_page_pte(vma
, address
, pmd
, flags
, &ctx
->pgmap
);
603 if ((flags
& FOLL_NUMA
) && pmd_protnone(pmdval
))
604 return no_page_table(vma
, flags
);
607 ptl
= pmd_lock(mm
, pmd
);
608 if (unlikely(pmd_none(*pmd
))) {
610 return no_page_table(vma
, flags
);
612 if (unlikely(!pmd_present(*pmd
))) {
614 if (likely(!(flags
& FOLL_MIGRATION
)))
615 return no_page_table(vma
, flags
);
616 pmd_migration_entry_wait(mm
, pmd
);
619 if (unlikely(!pmd_trans_huge(*pmd
))) {
621 return follow_page_pte(vma
, address
, pmd
, flags
, &ctx
->pgmap
);
623 if (flags
& (FOLL_SPLIT
| FOLL_SPLIT_PMD
)) {
625 page
= pmd_page(*pmd
);
626 if (is_huge_zero_page(page
)) {
629 split_huge_pmd(vma
, pmd
, address
);
630 if (pmd_trans_unstable(pmd
))
632 } else if (flags
& FOLL_SPLIT
) {
633 if (unlikely(!try_get_page(page
))) {
635 return ERR_PTR(-ENOMEM
);
639 ret
= split_huge_page(page
);
643 return no_page_table(vma
, flags
);
644 } else { /* flags & FOLL_SPLIT_PMD */
646 split_huge_pmd(vma
, pmd
, address
);
647 ret
= pte_alloc(mm
, pmd
) ? -ENOMEM
: 0;
650 return ret
? ERR_PTR(ret
) :
651 follow_page_pte(vma
, address
, pmd
, flags
, &ctx
->pgmap
);
653 page
= follow_trans_huge_pmd(vma
, address
, pmd
, flags
);
655 ctx
->page_mask
= HPAGE_PMD_NR
- 1;
659 static struct page
*follow_pud_mask(struct vm_area_struct
*vma
,
660 unsigned long address
, p4d_t
*p4dp
,
662 struct follow_page_context
*ctx
)
667 struct mm_struct
*mm
= vma
->vm_mm
;
669 pud
= pud_offset(p4dp
, address
);
671 return no_page_table(vma
, flags
);
672 if (pud_huge(*pud
) && is_vm_hugetlb_page(vma
)) {
673 page
= follow_huge_pud(mm
, address
, pud
, flags
);
676 return no_page_table(vma
, flags
);
678 if (is_hugepd(__hugepd(pud_val(*pud
)))) {
679 page
= follow_huge_pd(vma
, address
,
680 __hugepd(pud_val(*pud
)), flags
,
684 return no_page_table(vma
, flags
);
686 if (pud_devmap(*pud
)) {
687 ptl
= pud_lock(mm
, pud
);
688 page
= follow_devmap_pud(vma
, address
, pud
, flags
, &ctx
->pgmap
);
693 if (unlikely(pud_bad(*pud
)))
694 return no_page_table(vma
, flags
);
696 return follow_pmd_mask(vma
, address
, pud
, flags
, ctx
);
699 static struct page
*follow_p4d_mask(struct vm_area_struct
*vma
,
700 unsigned long address
, pgd_t
*pgdp
,
702 struct follow_page_context
*ctx
)
707 p4d
= p4d_offset(pgdp
, address
);
709 return no_page_table(vma
, flags
);
710 BUILD_BUG_ON(p4d_huge(*p4d
));
711 if (unlikely(p4d_bad(*p4d
)))
712 return no_page_table(vma
, flags
);
714 if (is_hugepd(__hugepd(p4d_val(*p4d
)))) {
715 page
= follow_huge_pd(vma
, address
,
716 __hugepd(p4d_val(*p4d
)), flags
,
720 return no_page_table(vma
, flags
);
722 return follow_pud_mask(vma
, address
, p4d
, flags
, ctx
);
726 * follow_page_mask - look up a page descriptor from a user-virtual address
727 * @vma: vm_area_struct mapping @address
728 * @address: virtual address to look up
729 * @flags: flags modifying lookup behaviour
730 * @ctx: contains dev_pagemap for %ZONE_DEVICE memory pinning and a
731 * pointer to output page_mask
733 * @flags can have FOLL_ flags set, defined in <linux/mm.h>
735 * When getting pages from ZONE_DEVICE memory, the @ctx->pgmap caches
736 * the device's dev_pagemap metadata to avoid repeating expensive lookups.
738 * On output, the @ctx->page_mask is set according to the size of the page.
740 * Return: the mapped (struct page *), %NULL if no mapping exists, or
741 * an error pointer if there is a mapping to something not represented
742 * by a page descriptor (see also vm_normal_page()).
744 static struct page
*follow_page_mask(struct vm_area_struct
*vma
,
745 unsigned long address
, unsigned int flags
,
746 struct follow_page_context
*ctx
)
750 struct mm_struct
*mm
= vma
->vm_mm
;
754 /* make this handle hugepd */
755 page
= follow_huge_addr(mm
, address
, flags
& FOLL_WRITE
);
757 WARN_ON_ONCE(flags
& (FOLL_GET
| FOLL_PIN
));
761 pgd
= pgd_offset(mm
, address
);
763 if (pgd_none(*pgd
) || unlikely(pgd_bad(*pgd
)))
764 return no_page_table(vma
, flags
);
766 if (pgd_huge(*pgd
)) {
767 page
= follow_huge_pgd(mm
, address
, pgd
, flags
);
770 return no_page_table(vma
, flags
);
772 if (is_hugepd(__hugepd(pgd_val(*pgd
)))) {
773 page
= follow_huge_pd(vma
, address
,
774 __hugepd(pgd_val(*pgd
)), flags
,
778 return no_page_table(vma
, flags
);
781 return follow_p4d_mask(vma
, address
, pgd
, flags
, ctx
);
784 struct page
*follow_page(struct vm_area_struct
*vma
, unsigned long address
,
785 unsigned int foll_flags
)
787 struct follow_page_context ctx
= { NULL
};
790 page
= follow_page_mask(vma
, address
, foll_flags
, &ctx
);
792 put_dev_pagemap(ctx
.pgmap
);
796 static int get_gate_page(struct mm_struct
*mm
, unsigned long address
,
797 unsigned int gup_flags
, struct vm_area_struct
**vma
,
807 /* user gate pages are read-only */
808 if (gup_flags
& FOLL_WRITE
)
810 if (address
> TASK_SIZE
)
811 pgd
= pgd_offset_k(address
);
813 pgd
= pgd_offset_gate(mm
, address
);
816 p4d
= p4d_offset(pgd
, address
);
819 pud
= pud_offset(p4d
, address
);
822 pmd
= pmd_offset(pud
, address
);
823 if (!pmd_present(*pmd
))
825 VM_BUG_ON(pmd_trans_huge(*pmd
));
826 pte
= pte_offset_map(pmd
, address
);
829 *vma
= get_gate_vma(mm
);
832 *page
= vm_normal_page(*vma
, address
, *pte
);
834 if ((gup_flags
& FOLL_DUMP
) || !is_zero_pfn(pte_pfn(*pte
)))
836 *page
= pte_page(*pte
);
838 if (unlikely(!try_grab_page(*page
, gup_flags
))) {
850 * mmap_lock must be held on entry. If @locked != NULL and *@flags
851 * does not include FOLL_NOWAIT, the mmap_lock may be released. If it
852 * is, *@locked will be set to 0 and -EBUSY returned.
854 static int faultin_page(struct vm_area_struct
*vma
,
855 unsigned long address
, unsigned int *flags
, int *locked
)
857 unsigned int fault_flags
= 0;
860 /* mlock all present pages, but do not fault in new pages */
861 if ((*flags
& (FOLL_POPULATE
| FOLL_MLOCK
)) == FOLL_MLOCK
)
863 if (*flags
& FOLL_WRITE
)
864 fault_flags
|= FAULT_FLAG_WRITE
;
865 if (*flags
& FOLL_REMOTE
)
866 fault_flags
|= FAULT_FLAG_REMOTE
;
868 fault_flags
|= FAULT_FLAG_ALLOW_RETRY
| FAULT_FLAG_KILLABLE
;
869 if (*flags
& FOLL_NOWAIT
)
870 fault_flags
|= FAULT_FLAG_ALLOW_RETRY
| FAULT_FLAG_RETRY_NOWAIT
;
871 if (*flags
& FOLL_TRIED
) {
873 * Note: FAULT_FLAG_ALLOW_RETRY and FAULT_FLAG_TRIED
876 fault_flags
|= FAULT_FLAG_TRIED
;
879 ret
= handle_mm_fault(vma
, address
, fault_flags
, NULL
);
880 if (ret
& VM_FAULT_ERROR
) {
881 int err
= vm_fault_to_errno(ret
, *flags
);
888 if (ret
& VM_FAULT_RETRY
) {
889 if (locked
&& !(fault_flags
& FAULT_FLAG_RETRY_NOWAIT
))
895 * The VM_FAULT_WRITE bit tells us that do_wp_page has broken COW when
896 * necessary, even if maybe_mkwrite decided not to set pte_write. We
897 * can thus safely do subsequent page lookups as if they were reads.
898 * But only do so when looping for pte_write is futile: in some cases
899 * userspace may also be wanting to write to the gotten user page,
900 * which a read fault here might prevent (a readonly page might get
901 * reCOWed by userspace write).
903 if ((ret
& VM_FAULT_WRITE
) && !(vma
->vm_flags
& VM_WRITE
))
908 static int check_vma_flags(struct vm_area_struct
*vma
, unsigned long gup_flags
)
910 vm_flags_t vm_flags
= vma
->vm_flags
;
911 int write
= (gup_flags
& FOLL_WRITE
);
912 int foreign
= (gup_flags
& FOLL_REMOTE
);
914 if (vm_flags
& (VM_IO
| VM_PFNMAP
))
917 if (gup_flags
& FOLL_ANON
&& !vma_is_anonymous(vma
))
920 if ((gup_flags
& FOLL_LONGTERM
) && vma_is_fsdax(vma
))
924 if (!(vm_flags
& VM_WRITE
)) {
925 if (!(gup_flags
& FOLL_FORCE
))
928 * We used to let the write,force case do COW in a
929 * VM_MAYWRITE VM_SHARED !VM_WRITE vma, so ptrace could
930 * set a breakpoint in a read-only mapping of an
931 * executable, without corrupting the file (yet only
932 * when that file had been opened for writing!).
933 * Anon pages in shared mappings are surprising: now
936 if (!is_cow_mapping(vm_flags
))
939 } else if (!(vm_flags
& VM_READ
)) {
940 if (!(gup_flags
& FOLL_FORCE
))
943 * Is there actually any vma we can reach here which does not
944 * have VM_MAYREAD set?
946 if (!(vm_flags
& VM_MAYREAD
))
950 * gups are always data accesses, not instruction
951 * fetches, so execute=false here
953 if (!arch_vma_access_permitted(vma
, write
, false, foreign
))
959 * __get_user_pages() - pin user pages in memory
960 * @mm: mm_struct of target mm
961 * @start: starting user address
962 * @nr_pages: number of pages from start to pin
963 * @gup_flags: flags modifying pin behaviour
964 * @pages: array that receives pointers to the pages pinned.
965 * Should be at least nr_pages long. Or NULL, if caller
966 * only intends to ensure the pages are faulted in.
967 * @vmas: array of pointers to vmas corresponding to each page.
968 * Or NULL if the caller does not require them.
969 * @locked: whether we're still with the mmap_lock held
971 * Returns either number of pages pinned (which may be less than the
972 * number requested), or an error. Details about the return value:
974 * -- If nr_pages is 0, returns 0.
975 * -- If nr_pages is >0, but no pages were pinned, returns -errno.
976 * -- If nr_pages is >0, and some pages were pinned, returns the number of
977 * pages pinned. Again, this may be less than nr_pages.
978 * -- 0 return value is possible when the fault would need to be retried.
980 * The caller is responsible for releasing returned @pages, via put_page().
982 * @vmas are valid only as long as mmap_lock is held.
984 * Must be called with mmap_lock held. It may be released. See below.
986 * __get_user_pages walks a process's page tables and takes a reference to
987 * each struct page that each user address corresponds to at a given
988 * instant. That is, it takes the page that would be accessed if a user
989 * thread accesses the given user virtual address at that instant.
991 * This does not guarantee that the page exists in the user mappings when
992 * __get_user_pages returns, and there may even be a completely different
993 * page there in some cases (eg. if mmapped pagecache has been invalidated
994 * and subsequently re faulted). However it does guarantee that the page
995 * won't be freed completely. And mostly callers simply care that the page
996 * contains data that was valid *at some point in time*. Typically, an IO
997 * or similar operation cannot guarantee anything stronger anyway because
998 * locks can't be held over the syscall boundary.
1000 * If @gup_flags & FOLL_WRITE == 0, the page must not be written to. If
1001 * the page is written to, set_page_dirty (or set_page_dirty_lock, as
1002 * appropriate) must be called after the page is finished with, and
1003 * before put_page is called.
1005 * If @locked != NULL, *@locked will be set to 0 when mmap_lock is
1006 * released by an up_read(). That can happen if @gup_flags does not
1009 * A caller using such a combination of @locked and @gup_flags
1010 * must therefore hold the mmap_lock for reading only, and recognize
1011 * when it's been released. Otherwise, it must be held for either
1012 * reading or writing and will not be released.
1014 * In most cases, get_user_pages or get_user_pages_fast should be used
1015 * instead of __get_user_pages. __get_user_pages should be used only if
1016 * you need some special @gup_flags.
1018 static long __get_user_pages(struct mm_struct
*mm
,
1019 unsigned long start
, unsigned long nr_pages
,
1020 unsigned int gup_flags
, struct page
**pages
,
1021 struct vm_area_struct
**vmas
, int *locked
)
1023 long ret
= 0, i
= 0;
1024 struct vm_area_struct
*vma
= NULL
;
1025 struct follow_page_context ctx
= { NULL
};
1030 start
= untagged_addr(start
);
1032 VM_BUG_ON(!!pages
!= !!(gup_flags
& (FOLL_GET
| FOLL_PIN
)));
1035 * If FOLL_FORCE is set then do not force a full fault as the hinting
1036 * fault information is unrelated to the reference behaviour of a task
1037 * using the address space
1039 if (!(gup_flags
& FOLL_FORCE
))
1040 gup_flags
|= FOLL_NUMA
;
1044 unsigned int foll_flags
= gup_flags
;
1045 unsigned int page_increm
;
1047 /* first iteration or cross vma bound */
1048 if (!vma
|| start
>= vma
->vm_end
) {
1049 vma
= find_extend_vma(mm
, start
);
1050 if (!vma
&& in_gate_area(mm
, start
)) {
1051 ret
= get_gate_page(mm
, start
& PAGE_MASK
,
1053 pages
? &pages
[i
] : NULL
);
1064 ret
= check_vma_flags(vma
, gup_flags
);
1068 if (is_vm_hugetlb_page(vma
)) {
1069 i
= follow_hugetlb_page(mm
, vma
, pages
, vmas
,
1070 &start
, &nr_pages
, i
,
1072 if (locked
&& *locked
== 0) {
1074 * We've got a VM_FAULT_RETRY
1075 * and we've lost mmap_lock.
1076 * We must stop here.
1078 BUG_ON(gup_flags
& FOLL_NOWAIT
);
1087 * If we have a pending SIGKILL, don't keep faulting pages and
1088 * potentially allocating memory.
1090 if (fatal_signal_pending(current
)) {
1096 page
= follow_page_mask(vma
, start
, foll_flags
, &ctx
);
1098 ret
= faultin_page(vma
, start
, &foll_flags
, locked
);
1113 } else if (PTR_ERR(page
) == -EEXIST
) {
1115 * Proper page table entry exists, but no corresponding
1119 } else if (IS_ERR(page
)) {
1120 ret
= PTR_ERR(page
);
1125 flush_anon_page(vma
, page
, start
);
1126 flush_dcache_page(page
);
1134 page_increm
= 1 + (~(start
>> PAGE_SHIFT
) & ctx
.page_mask
);
1135 if (page_increm
> nr_pages
)
1136 page_increm
= nr_pages
;
1138 start
+= page_increm
* PAGE_SIZE
;
1139 nr_pages
-= page_increm
;
1143 put_dev_pagemap(ctx
.pgmap
);
1147 static bool vma_permits_fault(struct vm_area_struct
*vma
,
1148 unsigned int fault_flags
)
1150 bool write
= !!(fault_flags
& FAULT_FLAG_WRITE
);
1151 bool foreign
= !!(fault_flags
& FAULT_FLAG_REMOTE
);
1152 vm_flags_t vm_flags
= write
? VM_WRITE
: VM_READ
;
1154 if (!(vm_flags
& vma
->vm_flags
))
1158 * The architecture might have a hardware protection
1159 * mechanism other than read/write that can deny access.
1161 * gup always represents data access, not instruction
1162 * fetches, so execute=false here:
1164 if (!arch_vma_access_permitted(vma
, write
, false, foreign
))
1171 * fixup_user_fault() - manually resolve a user page fault
1172 * @mm: mm_struct of target mm
1173 * @address: user address
1174 * @fault_flags:flags to pass down to handle_mm_fault()
1175 * @unlocked: did we unlock the mmap_lock while retrying, maybe NULL if caller
1176 * does not allow retry. If NULL, the caller must guarantee
1177 * that fault_flags does not contain FAULT_FLAG_ALLOW_RETRY.
1179 * This is meant to be called in the specific scenario where for locking reasons
1180 * we try to access user memory in atomic context (within a pagefault_disable()
1181 * section), this returns -EFAULT, and we want to resolve the user fault before
1184 * Typically this is meant to be used by the futex code.
1186 * The main difference with get_user_pages() is that this function will
1187 * unconditionally call handle_mm_fault() which will in turn perform all the
1188 * necessary SW fixup of the dirty and young bits in the PTE, while
1189 * get_user_pages() only guarantees to update these in the struct page.
1191 * This is important for some architectures where those bits also gate the
1192 * access permission to the page because they are maintained in software. On
1193 * such architectures, gup() will not be enough to make a subsequent access
1196 * This function will not return with an unlocked mmap_lock. So it has not the
1197 * same semantics wrt the @mm->mmap_lock as does filemap_fault().
1199 int fixup_user_fault(struct mm_struct
*mm
,
1200 unsigned long address
, unsigned int fault_flags
,
1203 struct vm_area_struct
*vma
;
1204 vm_fault_t ret
, major
= 0;
1206 address
= untagged_addr(address
);
1209 fault_flags
|= FAULT_FLAG_ALLOW_RETRY
| FAULT_FLAG_KILLABLE
;
1212 vma
= find_extend_vma(mm
, address
);
1213 if (!vma
|| address
< vma
->vm_start
)
1216 if (!vma_permits_fault(vma
, fault_flags
))
1219 if ((fault_flags
& FAULT_FLAG_KILLABLE
) &&
1220 fatal_signal_pending(current
))
1223 ret
= handle_mm_fault(vma
, address
, fault_flags
, NULL
);
1224 major
|= ret
& VM_FAULT_MAJOR
;
1225 if (ret
& VM_FAULT_ERROR
) {
1226 int err
= vm_fault_to_errno(ret
, 0);
1233 if (ret
& VM_FAULT_RETRY
) {
1236 fault_flags
|= FAULT_FLAG_TRIED
;
1242 EXPORT_SYMBOL_GPL(fixup_user_fault
);
1245 * Please note that this function, unlike __get_user_pages will not
1246 * return 0 for nr_pages > 0 without FOLL_NOWAIT
1248 static __always_inline
long __get_user_pages_locked(struct mm_struct
*mm
,
1249 unsigned long start
,
1250 unsigned long nr_pages
,
1251 struct page
**pages
,
1252 struct vm_area_struct
**vmas
,
1256 long ret
, pages_done
;
1260 /* if VM_FAULT_RETRY can be returned, vmas become invalid */
1262 /* check caller initialized locked */
1263 BUG_ON(*locked
!= 1);
1266 if (flags
& FOLL_PIN
)
1267 atomic_set(&mm
->has_pinned
, 1);
1270 * FOLL_PIN and FOLL_GET are mutually exclusive. Traditional behavior
1271 * is to set FOLL_GET if the caller wants pages[] filled in (but has
1272 * carelessly failed to specify FOLL_GET), so keep doing that, but only
1273 * for FOLL_GET, not for the newer FOLL_PIN.
1275 * FOLL_PIN always expects pages to be non-null, but no need to assert
1276 * that here, as any failures will be obvious enough.
1278 if (pages
&& !(flags
& FOLL_PIN
))
1282 lock_dropped
= false;
1284 ret
= __get_user_pages(mm
, start
, nr_pages
, flags
, pages
,
1287 /* VM_FAULT_RETRY couldn't trigger, bypass */
1290 /* VM_FAULT_RETRY cannot return errors */
1293 BUG_ON(ret
>= nr_pages
);
1304 * VM_FAULT_RETRY didn't trigger or it was a
1312 * VM_FAULT_RETRY triggered, so seek to the faulting offset.
1313 * For the prefault case (!pages) we only update counts.
1317 start
+= ret
<< PAGE_SHIFT
;
1318 lock_dropped
= true;
1322 * Repeat on the address that fired VM_FAULT_RETRY
1323 * with both FAULT_FLAG_ALLOW_RETRY and
1324 * FAULT_FLAG_TRIED. Note that GUP can be interrupted
1325 * by fatal signals, so we need to check it before we
1326 * start trying again otherwise it can loop forever.
1329 if (fatal_signal_pending(current
)) {
1331 pages_done
= -EINTR
;
1335 ret
= mmap_read_lock_killable(mm
);
1344 ret
= __get_user_pages(mm
, start
, 1, flags
| FOLL_TRIED
,
1345 pages
, NULL
, locked
);
1347 /* Continue to retry until we succeeded */
1365 if (lock_dropped
&& *locked
) {
1367 * We must let the caller know we temporarily dropped the lock
1368 * and so the critical section protected by it was lost.
1370 mmap_read_unlock(mm
);
1377 * populate_vma_page_range() - populate a range of pages in the vma.
1379 * @start: start address
1381 * @locked: whether the mmap_lock is still held
1383 * This takes care of mlocking the pages too if VM_LOCKED is set.
1385 * Return either number of pages pinned in the vma, or a negative error
1388 * vma->vm_mm->mmap_lock must be held.
1390 * If @locked is NULL, it may be held for read or write and will
1393 * If @locked is non-NULL, it must held for read only and may be
1394 * released. If it's released, *@locked will be set to 0.
1396 long populate_vma_page_range(struct vm_area_struct
*vma
,
1397 unsigned long start
, unsigned long end
, int *locked
)
1399 struct mm_struct
*mm
= vma
->vm_mm
;
1400 unsigned long nr_pages
= (end
- start
) / PAGE_SIZE
;
1403 VM_BUG_ON(start
& ~PAGE_MASK
);
1404 VM_BUG_ON(end
& ~PAGE_MASK
);
1405 VM_BUG_ON_VMA(start
< vma
->vm_start
, vma
);
1406 VM_BUG_ON_VMA(end
> vma
->vm_end
, vma
);
1407 mmap_assert_locked(mm
);
1409 gup_flags
= FOLL_TOUCH
| FOLL_POPULATE
| FOLL_MLOCK
;
1410 if (vma
->vm_flags
& VM_LOCKONFAULT
)
1411 gup_flags
&= ~FOLL_POPULATE
;
1413 * We want to touch writable mappings with a write fault in order
1414 * to break COW, except for shared mappings because these don't COW
1415 * and we would not want to dirty them for nothing.
1417 if ((vma
->vm_flags
& (VM_WRITE
| VM_SHARED
)) == VM_WRITE
)
1418 gup_flags
|= FOLL_WRITE
;
1421 * We want mlock to succeed for regions that have any permissions
1422 * other than PROT_NONE.
1424 if (vma_is_accessible(vma
))
1425 gup_flags
|= FOLL_FORCE
;
1428 * We made sure addr is within a VMA, so the following will
1429 * not result in a stack expansion that recurses back here.
1431 return __get_user_pages(mm
, start
, nr_pages
, gup_flags
,
1432 NULL
, NULL
, locked
);
1436 * __mm_populate - populate and/or mlock pages within a range of address space.
1438 * This is used to implement mlock() and the MAP_POPULATE / MAP_LOCKED mmap
1439 * flags. VMAs must be already marked with the desired vm_flags, and
1440 * mmap_lock must not be held.
1442 int __mm_populate(unsigned long start
, unsigned long len
, int ignore_errors
)
1444 struct mm_struct
*mm
= current
->mm
;
1445 unsigned long end
, nstart
, nend
;
1446 struct vm_area_struct
*vma
= NULL
;
1452 for (nstart
= start
; nstart
< end
; nstart
= nend
) {
1454 * We want to fault in pages for [nstart; end) address range.
1455 * Find first corresponding VMA.
1460 vma
= find_vma(mm
, nstart
);
1461 } else if (nstart
>= vma
->vm_end
)
1463 if (!vma
|| vma
->vm_start
>= end
)
1466 * Set [nstart; nend) to intersection of desired address
1467 * range with the first VMA. Also, skip undesirable VMA types.
1469 nend
= min(end
, vma
->vm_end
);
1470 if (vma
->vm_flags
& (VM_IO
| VM_PFNMAP
))
1472 if (nstart
< vma
->vm_start
)
1473 nstart
= vma
->vm_start
;
1475 * Now fault in a range of pages. populate_vma_page_range()
1476 * double checks the vma flags, so that it won't mlock pages
1477 * if the vma was already munlocked.
1479 ret
= populate_vma_page_range(vma
, nstart
, nend
, &locked
);
1481 if (ignore_errors
) {
1483 continue; /* continue at next VMA */
1487 nend
= nstart
+ ret
* PAGE_SIZE
;
1491 mmap_read_unlock(mm
);
1492 return ret
; /* 0 or negative error code */
1494 #else /* CONFIG_MMU */
1495 static long __get_user_pages_locked(struct mm_struct
*mm
, unsigned long start
,
1496 unsigned long nr_pages
, struct page
**pages
,
1497 struct vm_area_struct
**vmas
, int *locked
,
1498 unsigned int foll_flags
)
1500 struct vm_area_struct
*vma
;
1501 unsigned long vm_flags
;
1504 /* calculate required read or write permissions.
1505 * If FOLL_FORCE is set, we only require the "MAY" flags.
1507 vm_flags
= (foll_flags
& FOLL_WRITE
) ?
1508 (VM_WRITE
| VM_MAYWRITE
) : (VM_READ
| VM_MAYREAD
);
1509 vm_flags
&= (foll_flags
& FOLL_FORCE
) ?
1510 (VM_MAYREAD
| VM_MAYWRITE
) : (VM_READ
| VM_WRITE
);
1512 for (i
= 0; i
< nr_pages
; i
++) {
1513 vma
= find_vma(mm
, start
);
1515 goto finish_or_fault
;
1517 /* protect what we can, including chardevs */
1518 if ((vma
->vm_flags
& (VM_IO
| VM_PFNMAP
)) ||
1519 !(vm_flags
& vma
->vm_flags
))
1520 goto finish_or_fault
;
1523 pages
[i
] = virt_to_page(start
);
1529 start
= (start
+ PAGE_SIZE
) & PAGE_MASK
;
1535 return i
? : -EFAULT
;
1537 #endif /* !CONFIG_MMU */
1540 * get_dump_page() - pin user page in memory while writing it to core dump
1541 * @addr: user address
1543 * Returns struct page pointer of user page pinned for dump,
1544 * to be freed afterwards by put_page().
1546 * Returns NULL on any kind of failure - a hole must then be inserted into
1547 * the corefile, to preserve alignment with its headers; and also returns
1548 * NULL wherever the ZERO_PAGE, or an anonymous pte_none, has been found -
1549 * allowing a hole to be left in the corefile to save diskspace.
1551 * Called without mmap_lock (takes and releases the mmap_lock by itself).
1553 #ifdef CONFIG_ELF_CORE
1554 struct page
*get_dump_page(unsigned long addr
)
1556 struct mm_struct
*mm
= current
->mm
;
1561 if (mmap_read_lock_killable(mm
))
1563 ret
= __get_user_pages_locked(mm
, addr
, 1, &page
, NULL
, &locked
,
1564 FOLL_FORCE
| FOLL_DUMP
| FOLL_GET
);
1566 mmap_read_unlock(mm
);
1567 return (ret
== 1) ? page
: NULL
;
1569 #endif /* CONFIG_ELF_CORE */
1572 static long check_and_migrate_cma_pages(struct mm_struct
*mm
,
1573 unsigned long start
,
1574 unsigned long nr_pages
,
1575 struct page
**pages
,
1576 struct vm_area_struct
**vmas
,
1577 unsigned int gup_flags
)
1579 unsigned long i
, isolation_error_count
;
1581 LIST_HEAD(cma_page_list
);
1582 long ret
= nr_pages
;
1583 struct page
*prev_head
, *head
;
1584 struct migration_target_control mtc
= {
1585 .nid
= NUMA_NO_NODE
,
1586 .gfp_mask
= GFP_USER
| __GFP_MOVABLE
| __GFP_NOWARN
,
1591 isolation_error_count
= 0;
1593 for (i
= 0; i
< nr_pages
; i
++) {
1594 head
= compound_head(pages
[i
]);
1595 if (head
== prev_head
)
1599 * If we get a page from the CMA zone, since we are going to
1600 * be pinning these entries, we might as well move them out
1601 * of the CMA zone if possible.
1603 if (is_migrate_cma_page(head
)) {
1604 if (PageHuge(head
)) {
1605 if (!isolate_huge_page(head
, &cma_page_list
))
1606 isolation_error_count
++;
1608 if (!PageLRU(head
) && drain_allow
) {
1609 lru_add_drain_all();
1610 drain_allow
= false;
1613 if (isolate_lru_page(head
)) {
1614 isolation_error_count
++;
1617 list_add_tail(&head
->lru
, &cma_page_list
);
1618 mod_node_page_state(page_pgdat(head
),
1620 page_is_file_lru(head
),
1621 thp_nr_pages(head
));
1627 * If list is empty, and no isolation errors, means that all pages are
1628 * in the correct zone.
1630 if (list_empty(&cma_page_list
) && !isolation_error_count
)
1633 if (!list_empty(&cma_page_list
)) {
1635 * drop the above get_user_pages reference.
1637 if (gup_flags
& FOLL_PIN
)
1638 unpin_user_pages(pages
, nr_pages
);
1640 for (i
= 0; i
< nr_pages
; i
++)
1643 ret
= migrate_pages(&cma_page_list
, alloc_migration_target
,
1644 NULL
, (unsigned long)&mtc
, MIGRATE_SYNC
,
1647 if (!list_empty(&cma_page_list
))
1648 putback_movable_pages(&cma_page_list
);
1649 return ret
> 0 ? -ENOMEM
: ret
;
1652 /* We unpinned pages before migration, pin them again */
1653 ret
= __get_user_pages_locked(mm
, start
, nr_pages
, pages
, vmas
,
1661 * check again because pages were unpinned, and we also might have
1662 * had isolation errors and need more pages to migrate.
1667 static long check_and_migrate_cma_pages(struct mm_struct
*mm
,
1668 unsigned long start
,
1669 unsigned long nr_pages
,
1670 struct page
**pages
,
1671 struct vm_area_struct
**vmas
,
1672 unsigned int gup_flags
)
1676 #endif /* CONFIG_CMA */
1679 * __gup_longterm_locked() is a wrapper for __get_user_pages_locked which
1680 * allows us to process the FOLL_LONGTERM flag.
1682 static long __gup_longterm_locked(struct mm_struct
*mm
,
1683 unsigned long start
,
1684 unsigned long nr_pages
,
1685 struct page
**pages
,
1686 struct vm_area_struct
**vmas
,
1687 unsigned int gup_flags
)
1689 unsigned long flags
= 0;
1692 if (gup_flags
& FOLL_LONGTERM
)
1693 flags
= memalloc_nocma_save();
1695 rc
= __get_user_pages_locked(mm
, start
, nr_pages
, pages
, vmas
, NULL
,
1698 if (gup_flags
& FOLL_LONGTERM
) {
1700 rc
= check_and_migrate_cma_pages(mm
, start
, rc
, pages
,
1702 memalloc_nocma_restore(flags
);
1707 static bool is_valid_gup_flags(unsigned int gup_flags
)
1710 * FOLL_PIN must only be set internally by the pin_user_pages*() APIs,
1711 * never directly by the caller, so enforce that with an assertion:
1713 if (WARN_ON_ONCE(gup_flags
& FOLL_PIN
))
1716 * FOLL_PIN is a prerequisite to FOLL_LONGTERM. Another way of saying
1717 * that is, FOLL_LONGTERM is a specific case, more restrictive case of
1720 if (WARN_ON_ONCE(gup_flags
& FOLL_LONGTERM
))
1727 static long __get_user_pages_remote(struct mm_struct
*mm
,
1728 unsigned long start
, unsigned long nr_pages
,
1729 unsigned int gup_flags
, struct page
**pages
,
1730 struct vm_area_struct
**vmas
, int *locked
)
1733 * Parts of FOLL_LONGTERM behavior are incompatible with
1734 * FAULT_FLAG_ALLOW_RETRY because of the FS DAX check requirement on
1735 * vmas. However, this only comes up if locked is set, and there are
1736 * callers that do request FOLL_LONGTERM, but do not set locked. So,
1737 * allow what we can.
1739 if (gup_flags
& FOLL_LONGTERM
) {
1740 if (WARN_ON_ONCE(locked
))
1743 * This will check the vmas (even if our vmas arg is NULL)
1744 * and return -ENOTSUPP if DAX isn't allowed in this case:
1746 return __gup_longterm_locked(mm
, start
, nr_pages
, pages
,
1747 vmas
, gup_flags
| FOLL_TOUCH
|
1751 return __get_user_pages_locked(mm
, start
, nr_pages
, pages
, vmas
,
1753 gup_flags
| FOLL_TOUCH
| FOLL_REMOTE
);
1757 * get_user_pages_remote() - pin user pages in memory
1758 * @mm: mm_struct of target mm
1759 * @start: starting user address
1760 * @nr_pages: number of pages from start to pin
1761 * @gup_flags: flags modifying lookup behaviour
1762 * @pages: array that receives pointers to the pages pinned.
1763 * Should be at least nr_pages long. Or NULL, if caller
1764 * only intends to ensure the pages are faulted in.
1765 * @vmas: array of pointers to vmas corresponding to each page.
1766 * Or NULL if the caller does not require them.
1767 * @locked: pointer to lock flag indicating whether lock is held and
1768 * subsequently whether VM_FAULT_RETRY functionality can be
1769 * utilised. Lock must initially be held.
1771 * Returns either number of pages pinned (which may be less than the
1772 * number requested), or an error. Details about the return value:
1774 * -- If nr_pages is 0, returns 0.
1775 * -- If nr_pages is >0, but no pages were pinned, returns -errno.
1776 * -- If nr_pages is >0, and some pages were pinned, returns the number of
1777 * pages pinned. Again, this may be less than nr_pages.
1779 * The caller is responsible for releasing returned @pages, via put_page().
1781 * @vmas are valid only as long as mmap_lock is held.
1783 * Must be called with mmap_lock held for read or write.
1785 * get_user_pages_remote walks a process's page tables and takes a reference
1786 * to each struct page that each user address corresponds to at a given
1787 * instant. That is, it takes the page that would be accessed if a user
1788 * thread accesses the given user virtual address at that instant.
1790 * This does not guarantee that the page exists in the user mappings when
1791 * get_user_pages_remote returns, and there may even be a completely different
1792 * page there in some cases (eg. if mmapped pagecache has been invalidated
1793 * and subsequently re faulted). However it does guarantee that the page
1794 * won't be freed completely. And mostly callers simply care that the page
1795 * contains data that was valid *at some point in time*. Typically, an IO
1796 * or similar operation cannot guarantee anything stronger anyway because
1797 * locks can't be held over the syscall boundary.
1799 * If gup_flags & FOLL_WRITE == 0, the page must not be written to. If the page
1800 * is written to, set_page_dirty (or set_page_dirty_lock, as appropriate) must
1801 * be called after the page is finished with, and before put_page is called.
1803 * get_user_pages_remote is typically used for fewer-copy IO operations,
1804 * to get a handle on the memory by some means other than accesses
1805 * via the user virtual addresses. The pages may be submitted for
1806 * DMA to devices or accessed via their kernel linear mapping (via the
1807 * kmap APIs). Care should be taken to use the correct cache flushing APIs.
1809 * See also get_user_pages_fast, for performance critical applications.
1811 * get_user_pages_remote should be phased out in favor of
1812 * get_user_pages_locked|unlocked or get_user_pages_fast. Nothing
1813 * should use get_user_pages_remote because it cannot pass
1814 * FAULT_FLAG_ALLOW_RETRY to handle_mm_fault.
1816 long get_user_pages_remote(struct mm_struct
*mm
,
1817 unsigned long start
, unsigned long nr_pages
,
1818 unsigned int gup_flags
, struct page
**pages
,
1819 struct vm_area_struct
**vmas
, int *locked
)
1821 if (!is_valid_gup_flags(gup_flags
))
1824 return __get_user_pages_remote(mm
, start
, nr_pages
, gup_flags
,
1825 pages
, vmas
, locked
);
1827 EXPORT_SYMBOL(get_user_pages_remote
);
1829 #else /* CONFIG_MMU */
1830 long get_user_pages_remote(struct mm_struct
*mm
,
1831 unsigned long start
, unsigned long nr_pages
,
1832 unsigned int gup_flags
, struct page
**pages
,
1833 struct vm_area_struct
**vmas
, int *locked
)
1838 static long __get_user_pages_remote(struct mm_struct
*mm
,
1839 unsigned long start
, unsigned long nr_pages
,
1840 unsigned int gup_flags
, struct page
**pages
,
1841 struct vm_area_struct
**vmas
, int *locked
)
1845 #endif /* !CONFIG_MMU */
1848 * get_user_pages() - pin user pages in memory
1849 * @start: starting user address
1850 * @nr_pages: number of pages from start to pin
1851 * @gup_flags: flags modifying lookup behaviour
1852 * @pages: array that receives pointers to the pages pinned.
1853 * Should be at least nr_pages long. Or NULL, if caller
1854 * only intends to ensure the pages are faulted in.
1855 * @vmas: array of pointers to vmas corresponding to each page.
1856 * Or NULL if the caller does not require them.
1858 * This is the same as get_user_pages_remote(), just with a less-flexible
1859 * calling convention where we assume that the mm being operated on belongs to
1860 * the current task, and doesn't allow passing of a locked parameter. We also
1861 * obviously don't pass FOLL_REMOTE in here.
1863 long get_user_pages(unsigned long start
, unsigned long nr_pages
,
1864 unsigned int gup_flags
, struct page
**pages
,
1865 struct vm_area_struct
**vmas
)
1867 if (!is_valid_gup_flags(gup_flags
))
1870 return __gup_longterm_locked(current
->mm
, start
, nr_pages
,
1871 pages
, vmas
, gup_flags
| FOLL_TOUCH
);
1873 EXPORT_SYMBOL(get_user_pages
);
1876 * get_user_pages_locked() - variant of get_user_pages()
1878 * @start: starting user address
1879 * @nr_pages: number of pages from start to pin
1880 * @gup_flags: flags modifying lookup behaviour
1881 * @pages: array that receives pointers to the pages pinned.
1882 * Should be at least nr_pages long. Or NULL, if caller
1883 * only intends to ensure the pages are faulted in.
1884 * @locked: pointer to lock flag indicating whether lock is held and
1885 * subsequently whether VM_FAULT_RETRY functionality can be
1886 * utilised. Lock must initially be held.
1888 * It is suitable to replace the form:
1890 * mmap_read_lock(mm);
1892 * get_user_pages(mm, ..., pages, NULL);
1893 * mmap_read_unlock(mm);
1898 * mmap_read_lock(mm);
1900 * get_user_pages_locked(mm, ..., pages, &locked);
1902 * mmap_read_unlock(mm);
1904 * We can leverage the VM_FAULT_RETRY functionality in the page fault
1905 * paths better by using either get_user_pages_locked() or
1906 * get_user_pages_unlocked().
1909 long get_user_pages_locked(unsigned long start
, unsigned long nr_pages
,
1910 unsigned int gup_flags
, struct page
**pages
,
1914 * FIXME: Current FOLL_LONGTERM behavior is incompatible with
1915 * FAULT_FLAG_ALLOW_RETRY because of the FS DAX check requirement on
1916 * vmas. As there are no users of this flag in this call we simply
1917 * disallow this option for now.
1919 if (WARN_ON_ONCE(gup_flags
& FOLL_LONGTERM
))
1922 * FOLL_PIN must only be set internally by the pin_user_pages*() APIs,
1923 * never directly by the caller, so enforce that:
1925 if (WARN_ON_ONCE(gup_flags
& FOLL_PIN
))
1928 return __get_user_pages_locked(current
->mm
, start
, nr_pages
,
1929 pages
, NULL
, locked
,
1930 gup_flags
| FOLL_TOUCH
);
1932 EXPORT_SYMBOL(get_user_pages_locked
);
1935 * get_user_pages_unlocked() is suitable to replace the form:
1937 * mmap_read_lock(mm);
1938 * get_user_pages(mm, ..., pages, NULL);
1939 * mmap_read_unlock(mm);
1943 * get_user_pages_unlocked(mm, ..., pages);
1945 * It is functionally equivalent to get_user_pages_fast so
1946 * get_user_pages_fast should be used instead if specific gup_flags
1947 * (e.g. FOLL_FORCE) are not required.
1949 long get_user_pages_unlocked(unsigned long start
, unsigned long nr_pages
,
1950 struct page
**pages
, unsigned int gup_flags
)
1952 struct mm_struct
*mm
= current
->mm
;
1957 * FIXME: Current FOLL_LONGTERM behavior is incompatible with
1958 * FAULT_FLAG_ALLOW_RETRY because of the FS DAX check requirement on
1959 * vmas. As there are no users of this flag in this call we simply
1960 * disallow this option for now.
1962 if (WARN_ON_ONCE(gup_flags
& FOLL_LONGTERM
))
1966 ret
= __get_user_pages_locked(mm
, start
, nr_pages
, pages
, NULL
,
1967 &locked
, gup_flags
| FOLL_TOUCH
);
1969 mmap_read_unlock(mm
);
1972 EXPORT_SYMBOL(get_user_pages_unlocked
);
1977 * get_user_pages_fast attempts to pin user pages by walking the page
1978 * tables directly and avoids taking locks. Thus the walker needs to be
1979 * protected from page table pages being freed from under it, and should
1980 * block any THP splits.
1982 * One way to achieve this is to have the walker disable interrupts, and
1983 * rely on IPIs from the TLB flushing code blocking before the page table
1984 * pages are freed. This is unsuitable for architectures that do not need
1985 * to broadcast an IPI when invalidating TLBs.
1987 * Another way to achieve this is to batch up page table containing pages
1988 * belonging to more than one mm_user, then rcu_sched a callback to free those
1989 * pages. Disabling interrupts will allow the fast_gup walker to both block
1990 * the rcu_sched callback, and an IPI that we broadcast for splitting THPs
1991 * (which is a relatively rare event). The code below adopts this strategy.
1993 * Before activating this code, please be aware that the following assumptions
1994 * are currently made:
1996 * *) Either MMU_GATHER_RCU_TABLE_FREE is enabled, and tlb_remove_table() is used to
1997 * free pages containing page tables or TLB flushing requires IPI broadcast.
1999 * *) ptes can be read atomically by the architecture.
2001 * *) access_ok is sufficient to validate userspace address ranges.
2003 * The last two assumptions can be relaxed by the addition of helper functions.
2005 * This code is based heavily on the PowerPC implementation by Nick Piggin.
2007 #ifdef CONFIG_HAVE_FAST_GUP
2009 static void __maybe_unused
undo_dev_pagemap(int *nr
, int nr_start
,
2011 struct page
**pages
)
2013 while ((*nr
) - nr_start
) {
2014 struct page
*page
= pages
[--(*nr
)];
2016 ClearPageReferenced(page
);
2017 if (flags
& FOLL_PIN
)
2018 unpin_user_page(page
);
2024 #ifdef CONFIG_ARCH_HAS_PTE_SPECIAL
2025 static int gup_pte_range(pmd_t pmd
, unsigned long addr
, unsigned long end
,
2026 unsigned int flags
, struct page
**pages
, int *nr
)
2028 struct dev_pagemap
*pgmap
= NULL
;
2029 int nr_start
= *nr
, ret
= 0;
2032 ptem
= ptep
= pte_offset_map(&pmd
, addr
);
2034 pte_t pte
= ptep_get_lockless(ptep
);
2035 struct page
*head
, *page
;
2038 * Similar to the PMD case below, NUMA hinting must take slow
2039 * path using the pte_protnone check.
2041 if (pte_protnone(pte
))
2044 if (!pte_access_permitted(pte
, flags
& FOLL_WRITE
))
2047 if (pte_devmap(pte
)) {
2048 if (unlikely(flags
& FOLL_LONGTERM
))
2051 pgmap
= get_dev_pagemap(pte_pfn(pte
), pgmap
);
2052 if (unlikely(!pgmap
)) {
2053 undo_dev_pagemap(nr
, nr_start
, flags
, pages
);
2056 } else if (pte_special(pte
))
2059 VM_BUG_ON(!pfn_valid(pte_pfn(pte
)));
2060 page
= pte_page(pte
);
2062 head
= try_grab_compound_head(page
, 1, flags
);
2066 if (unlikely(pte_val(pte
) != pte_val(*ptep
))) {
2067 put_compound_head(head
, 1, flags
);
2071 VM_BUG_ON_PAGE(compound_head(page
) != head
, page
);
2074 * We need to make the page accessible if and only if we are
2075 * going to access its content (the FOLL_PIN case). Please
2076 * see Documentation/core-api/pin_user_pages.rst for
2079 if (flags
& FOLL_PIN
) {
2080 ret
= arch_make_page_accessible(page
);
2082 unpin_user_page(page
);
2086 SetPageReferenced(page
);
2090 } while (ptep
++, addr
+= PAGE_SIZE
, addr
!= end
);
2096 put_dev_pagemap(pgmap
);
2103 * If we can't determine whether or not a pte is special, then fail immediately
2104 * for ptes. Note, we can still pin HugeTLB and THP as these are guaranteed not
2107 * For a futex to be placed on a THP tail page, get_futex_key requires a
2108 * get_user_pages_fast_only implementation that can pin pages. Thus it's still
2109 * useful to have gup_huge_pmd even if we can't operate on ptes.
2111 static int gup_pte_range(pmd_t pmd
, unsigned long addr
, unsigned long end
,
2112 unsigned int flags
, struct page
**pages
, int *nr
)
2116 #endif /* CONFIG_ARCH_HAS_PTE_SPECIAL */
2118 #if defined(CONFIG_ARCH_HAS_PTE_DEVMAP) && defined(CONFIG_TRANSPARENT_HUGEPAGE)
2119 static int __gup_device_huge(unsigned long pfn
, unsigned long addr
,
2120 unsigned long end
, unsigned int flags
,
2121 struct page
**pages
, int *nr
)
2124 struct dev_pagemap
*pgmap
= NULL
;
2127 struct page
*page
= pfn_to_page(pfn
);
2129 pgmap
= get_dev_pagemap(pfn
, pgmap
);
2130 if (unlikely(!pgmap
)) {
2131 undo_dev_pagemap(nr
, nr_start
, flags
, pages
);
2134 SetPageReferenced(page
);
2136 if (unlikely(!try_grab_page(page
, flags
))) {
2137 undo_dev_pagemap(nr
, nr_start
, flags
, pages
);
2142 } while (addr
+= PAGE_SIZE
, addr
!= end
);
2145 put_dev_pagemap(pgmap
);
2149 static int __gup_device_huge_pmd(pmd_t orig
, pmd_t
*pmdp
, unsigned long addr
,
2150 unsigned long end
, unsigned int flags
,
2151 struct page
**pages
, int *nr
)
2153 unsigned long fault_pfn
;
2156 fault_pfn
= pmd_pfn(orig
) + ((addr
& ~PMD_MASK
) >> PAGE_SHIFT
);
2157 if (!__gup_device_huge(fault_pfn
, addr
, end
, flags
, pages
, nr
))
2160 if (unlikely(pmd_val(orig
) != pmd_val(*pmdp
))) {
2161 undo_dev_pagemap(nr
, nr_start
, flags
, pages
);
2167 static int __gup_device_huge_pud(pud_t orig
, pud_t
*pudp
, unsigned long addr
,
2168 unsigned long end
, unsigned int flags
,
2169 struct page
**pages
, int *nr
)
2171 unsigned long fault_pfn
;
2174 fault_pfn
= pud_pfn(orig
) + ((addr
& ~PUD_MASK
) >> PAGE_SHIFT
);
2175 if (!__gup_device_huge(fault_pfn
, addr
, end
, flags
, pages
, nr
))
2178 if (unlikely(pud_val(orig
) != pud_val(*pudp
))) {
2179 undo_dev_pagemap(nr
, nr_start
, flags
, pages
);
2185 static int __gup_device_huge_pmd(pmd_t orig
, pmd_t
*pmdp
, unsigned long addr
,
2186 unsigned long end
, unsigned int flags
,
2187 struct page
**pages
, int *nr
)
2193 static int __gup_device_huge_pud(pud_t pud
, pud_t
*pudp
, unsigned long addr
,
2194 unsigned long end
, unsigned int flags
,
2195 struct page
**pages
, int *nr
)
2202 static int record_subpages(struct page
*page
, unsigned long addr
,
2203 unsigned long end
, struct page
**pages
)
2207 for (nr
= 0; addr
!= end
; addr
+= PAGE_SIZE
)
2208 pages
[nr
++] = page
++;
2213 #ifdef CONFIG_ARCH_HAS_HUGEPD
2214 static unsigned long hugepte_addr_end(unsigned long addr
, unsigned long end
,
2217 unsigned long __boundary
= (addr
+ sz
) & ~(sz
-1);
2218 return (__boundary
- 1 < end
- 1) ? __boundary
: end
;
2221 static int gup_hugepte(pte_t
*ptep
, unsigned long sz
, unsigned long addr
,
2222 unsigned long end
, unsigned int flags
,
2223 struct page
**pages
, int *nr
)
2225 unsigned long pte_end
;
2226 struct page
*head
, *page
;
2230 pte_end
= (addr
+ sz
) & ~(sz
-1);
2234 pte
= huge_ptep_get(ptep
);
2236 if (!pte_access_permitted(pte
, flags
& FOLL_WRITE
))
2239 /* hugepages are never "special" */
2240 VM_BUG_ON(!pfn_valid(pte_pfn(pte
)));
2242 head
= pte_page(pte
);
2243 page
= head
+ ((addr
& (sz
-1)) >> PAGE_SHIFT
);
2244 refs
= record_subpages(page
, addr
, end
, pages
+ *nr
);
2246 head
= try_grab_compound_head(head
, refs
, flags
);
2250 if (unlikely(pte_val(pte
) != pte_val(*ptep
))) {
2251 put_compound_head(head
, refs
, flags
);
2256 SetPageReferenced(head
);
2260 static int gup_huge_pd(hugepd_t hugepd
, unsigned long addr
,
2261 unsigned int pdshift
, unsigned long end
, unsigned int flags
,
2262 struct page
**pages
, int *nr
)
2265 unsigned long sz
= 1UL << hugepd_shift(hugepd
);
2268 ptep
= hugepte_offset(hugepd
, addr
, pdshift
);
2270 next
= hugepte_addr_end(addr
, end
, sz
);
2271 if (!gup_hugepte(ptep
, sz
, addr
, end
, flags
, pages
, nr
))
2273 } while (ptep
++, addr
= next
, addr
!= end
);
2278 static inline int gup_huge_pd(hugepd_t hugepd
, unsigned long addr
,
2279 unsigned int pdshift
, unsigned long end
, unsigned int flags
,
2280 struct page
**pages
, int *nr
)
2284 #endif /* CONFIG_ARCH_HAS_HUGEPD */
2286 static int gup_huge_pmd(pmd_t orig
, pmd_t
*pmdp
, unsigned long addr
,
2287 unsigned long end
, unsigned int flags
,
2288 struct page
**pages
, int *nr
)
2290 struct page
*head
, *page
;
2293 if (!pmd_access_permitted(orig
, flags
& FOLL_WRITE
))
2296 if (pmd_devmap(orig
)) {
2297 if (unlikely(flags
& FOLL_LONGTERM
))
2299 return __gup_device_huge_pmd(orig
, pmdp
, addr
, end
, flags
,
2303 page
= pmd_page(orig
) + ((addr
& ~PMD_MASK
) >> PAGE_SHIFT
);
2304 refs
= record_subpages(page
, addr
, end
, pages
+ *nr
);
2306 head
= try_grab_compound_head(pmd_page(orig
), refs
, flags
);
2310 if (unlikely(pmd_val(orig
) != pmd_val(*pmdp
))) {
2311 put_compound_head(head
, refs
, flags
);
2316 SetPageReferenced(head
);
2320 static int gup_huge_pud(pud_t orig
, pud_t
*pudp
, unsigned long addr
,
2321 unsigned long end
, unsigned int flags
,
2322 struct page
**pages
, int *nr
)
2324 struct page
*head
, *page
;
2327 if (!pud_access_permitted(orig
, flags
& FOLL_WRITE
))
2330 if (pud_devmap(orig
)) {
2331 if (unlikely(flags
& FOLL_LONGTERM
))
2333 return __gup_device_huge_pud(orig
, pudp
, addr
, end
, flags
,
2337 page
= pud_page(orig
) + ((addr
& ~PUD_MASK
) >> PAGE_SHIFT
);
2338 refs
= record_subpages(page
, addr
, end
, pages
+ *nr
);
2340 head
= try_grab_compound_head(pud_page(orig
), refs
, flags
);
2344 if (unlikely(pud_val(orig
) != pud_val(*pudp
))) {
2345 put_compound_head(head
, refs
, flags
);
2350 SetPageReferenced(head
);
2354 static int gup_huge_pgd(pgd_t orig
, pgd_t
*pgdp
, unsigned long addr
,
2355 unsigned long end
, unsigned int flags
,
2356 struct page
**pages
, int *nr
)
2359 struct page
*head
, *page
;
2361 if (!pgd_access_permitted(orig
, flags
& FOLL_WRITE
))
2364 BUILD_BUG_ON(pgd_devmap(orig
));
2366 page
= pgd_page(orig
) + ((addr
& ~PGDIR_MASK
) >> PAGE_SHIFT
);
2367 refs
= record_subpages(page
, addr
, end
, pages
+ *nr
);
2369 head
= try_grab_compound_head(pgd_page(orig
), refs
, flags
);
2373 if (unlikely(pgd_val(orig
) != pgd_val(*pgdp
))) {
2374 put_compound_head(head
, refs
, flags
);
2379 SetPageReferenced(head
);
2383 static int gup_pmd_range(pud_t
*pudp
, pud_t pud
, unsigned long addr
, unsigned long end
,
2384 unsigned int flags
, struct page
**pages
, int *nr
)
2389 pmdp
= pmd_offset_lockless(pudp
, pud
, addr
);
2391 pmd_t pmd
= READ_ONCE(*pmdp
);
2393 next
= pmd_addr_end(addr
, end
);
2394 if (!pmd_present(pmd
))
2397 if (unlikely(pmd_trans_huge(pmd
) || pmd_huge(pmd
) ||
2400 * NUMA hinting faults need to be handled in the GUP
2401 * slowpath for accounting purposes and so that they
2402 * can be serialised against THP migration.
2404 if (pmd_protnone(pmd
))
2407 if (!gup_huge_pmd(pmd
, pmdp
, addr
, next
, flags
,
2411 } else if (unlikely(is_hugepd(__hugepd(pmd_val(pmd
))))) {
2413 * architecture have different format for hugetlbfs
2414 * pmd format and THP pmd format
2416 if (!gup_huge_pd(__hugepd(pmd_val(pmd
)), addr
,
2417 PMD_SHIFT
, next
, flags
, pages
, nr
))
2419 } else if (!gup_pte_range(pmd
, addr
, next
, flags
, pages
, nr
))
2421 } while (pmdp
++, addr
= next
, addr
!= end
);
2426 static int gup_pud_range(p4d_t
*p4dp
, p4d_t p4d
, unsigned long addr
, unsigned long end
,
2427 unsigned int flags
, struct page
**pages
, int *nr
)
2432 pudp
= pud_offset_lockless(p4dp
, p4d
, addr
);
2434 pud_t pud
= READ_ONCE(*pudp
);
2436 next
= pud_addr_end(addr
, end
);
2437 if (unlikely(!pud_present(pud
)))
2439 if (unlikely(pud_huge(pud
))) {
2440 if (!gup_huge_pud(pud
, pudp
, addr
, next
, flags
,
2443 } else if (unlikely(is_hugepd(__hugepd(pud_val(pud
))))) {
2444 if (!gup_huge_pd(__hugepd(pud_val(pud
)), addr
,
2445 PUD_SHIFT
, next
, flags
, pages
, nr
))
2447 } else if (!gup_pmd_range(pudp
, pud
, addr
, next
, flags
, pages
, nr
))
2449 } while (pudp
++, addr
= next
, addr
!= end
);
2454 static int gup_p4d_range(pgd_t
*pgdp
, pgd_t pgd
, unsigned long addr
, unsigned long end
,
2455 unsigned int flags
, struct page
**pages
, int *nr
)
2460 p4dp
= p4d_offset_lockless(pgdp
, pgd
, addr
);
2462 p4d_t p4d
= READ_ONCE(*p4dp
);
2464 next
= p4d_addr_end(addr
, end
);
2467 BUILD_BUG_ON(p4d_huge(p4d
));
2468 if (unlikely(is_hugepd(__hugepd(p4d_val(p4d
))))) {
2469 if (!gup_huge_pd(__hugepd(p4d_val(p4d
)), addr
,
2470 P4D_SHIFT
, next
, flags
, pages
, nr
))
2472 } else if (!gup_pud_range(p4dp
, p4d
, addr
, next
, flags
, pages
, nr
))
2474 } while (p4dp
++, addr
= next
, addr
!= end
);
2479 static void gup_pgd_range(unsigned long addr
, unsigned long end
,
2480 unsigned int flags
, struct page
**pages
, int *nr
)
2485 pgdp
= pgd_offset(current
->mm
, addr
);
2487 pgd_t pgd
= READ_ONCE(*pgdp
);
2489 next
= pgd_addr_end(addr
, end
);
2492 if (unlikely(pgd_huge(pgd
))) {
2493 if (!gup_huge_pgd(pgd
, pgdp
, addr
, next
, flags
,
2496 } else if (unlikely(is_hugepd(__hugepd(pgd_val(pgd
))))) {
2497 if (!gup_huge_pd(__hugepd(pgd_val(pgd
)), addr
,
2498 PGDIR_SHIFT
, next
, flags
, pages
, nr
))
2500 } else if (!gup_p4d_range(pgdp
, pgd
, addr
, next
, flags
, pages
, nr
))
2502 } while (pgdp
++, addr
= next
, addr
!= end
);
2505 static inline void gup_pgd_range(unsigned long addr
, unsigned long end
,
2506 unsigned int flags
, struct page
**pages
, int *nr
)
2509 #endif /* CONFIG_HAVE_FAST_GUP */
2511 #ifndef gup_fast_permitted
2513 * Check if it's allowed to use get_user_pages_fast_only() for the range, or
2514 * we need to fall back to the slow version:
2516 static bool gup_fast_permitted(unsigned long start
, unsigned long end
)
2522 static int __gup_longterm_unlocked(unsigned long start
, int nr_pages
,
2523 unsigned int gup_flags
, struct page
**pages
)
2528 * FIXME: FOLL_LONGTERM does not work with
2529 * get_user_pages_unlocked() (see comments in that function)
2531 if (gup_flags
& FOLL_LONGTERM
) {
2532 mmap_read_lock(current
->mm
);
2533 ret
= __gup_longterm_locked(current
->mm
,
2535 pages
, NULL
, gup_flags
);
2536 mmap_read_unlock(current
->mm
);
2538 ret
= get_user_pages_unlocked(start
, nr_pages
,
2545 static unsigned long lockless_pages_from_mm(unsigned long start
,
2547 unsigned int gup_flags
,
2548 struct page
**pages
)
2550 unsigned long flags
;
2554 if (!IS_ENABLED(CONFIG_HAVE_FAST_GUP
) ||
2555 !gup_fast_permitted(start
, end
))
2558 if (gup_flags
& FOLL_PIN
) {
2559 seq
= raw_read_seqcount(¤t
->mm
->write_protect_seq
);
2565 * Disable interrupts. The nested form is used, in order to allow full,
2566 * general purpose use of this routine.
2568 * With interrupts disabled, we block page table pages from being freed
2569 * from under us. See struct mmu_table_batch comments in
2570 * include/asm-generic/tlb.h for more details.
2572 * We do not adopt an rcu_read_lock() here as we also want to block IPIs
2573 * that come from THPs splitting.
2575 local_irq_save(flags
);
2576 gup_pgd_range(start
, end
, gup_flags
, pages
, &nr_pinned
);
2577 local_irq_restore(flags
);
2580 * When pinning pages for DMA there could be a concurrent write protect
2581 * from fork() via copy_page_range(), in this case always fail fast GUP.
2583 if (gup_flags
& FOLL_PIN
) {
2584 if (read_seqcount_retry(¤t
->mm
->write_protect_seq
, seq
)) {
2585 unpin_user_pages(pages
, nr_pinned
);
2592 static int internal_get_user_pages_fast(unsigned long start
,
2593 unsigned long nr_pages
,
2594 unsigned int gup_flags
,
2595 struct page
**pages
)
2597 unsigned long len
, end
;
2598 unsigned long nr_pinned
;
2601 if (WARN_ON_ONCE(gup_flags
& ~(FOLL_WRITE
| FOLL_LONGTERM
|
2602 FOLL_FORCE
| FOLL_PIN
| FOLL_GET
|
2606 if (gup_flags
& FOLL_PIN
)
2607 atomic_set(¤t
->mm
->has_pinned
, 1);
2609 if (!(gup_flags
& FOLL_FAST_ONLY
))
2610 might_lock_read(¤t
->mm
->mmap_lock
);
2612 start
= untagged_addr(start
) & PAGE_MASK
;
2613 len
= nr_pages
<< PAGE_SHIFT
;
2614 if (check_add_overflow(start
, len
, &end
))
2616 if (unlikely(!access_ok((void __user
*)start
, len
)))
2619 nr_pinned
= lockless_pages_from_mm(start
, end
, gup_flags
, pages
);
2620 if (nr_pinned
== nr_pages
|| gup_flags
& FOLL_FAST_ONLY
)
2623 /* Slow path: try to get the remaining pages with get_user_pages */
2624 start
+= nr_pinned
<< PAGE_SHIFT
;
2626 ret
= __gup_longterm_unlocked(start
, nr_pages
- nr_pinned
, gup_flags
,
2630 * The caller has to unpin the pages we already pinned so
2631 * returning -errno is not an option
2637 return ret
+ nr_pinned
;
2641 * get_user_pages_fast_only() - pin user pages in memory
2642 * @start: starting user address
2643 * @nr_pages: number of pages from start to pin
2644 * @gup_flags: flags modifying pin behaviour
2645 * @pages: array that receives pointers to the pages pinned.
2646 * Should be at least nr_pages long.
2648 * Like get_user_pages_fast() except it's IRQ-safe in that it won't fall back to
2650 * Note a difference with get_user_pages_fast: this always returns the
2651 * number of pages pinned, 0 if no pages were pinned.
2653 * If the architecture does not support this function, simply return with no
2656 * Careful, careful! COW breaking can go either way, so a non-write
2657 * access can get ambiguous page results. If you call this function without
2658 * 'write' set, you'd better be sure that you're ok with that ambiguity.
2660 int get_user_pages_fast_only(unsigned long start
, int nr_pages
,
2661 unsigned int gup_flags
, struct page
**pages
)
2665 * Internally (within mm/gup.c), gup fast variants must set FOLL_GET,
2666 * because gup fast is always a "pin with a +1 page refcount" request.
2668 * FOLL_FAST_ONLY is required in order to match the API description of
2669 * this routine: no fall back to regular ("slow") GUP.
2671 gup_flags
|= FOLL_GET
| FOLL_FAST_ONLY
;
2673 nr_pinned
= internal_get_user_pages_fast(start
, nr_pages
, gup_flags
,
2677 * As specified in the API description above, this routine is not
2678 * allowed to return negative values. However, the common core
2679 * routine internal_get_user_pages_fast() *can* return -errno.
2680 * Therefore, correct for that here:
2687 EXPORT_SYMBOL_GPL(get_user_pages_fast_only
);
2690 * get_user_pages_fast() - pin user pages in memory
2691 * @start: starting user address
2692 * @nr_pages: number of pages from start to pin
2693 * @gup_flags: flags modifying pin behaviour
2694 * @pages: array that receives pointers to the pages pinned.
2695 * Should be at least nr_pages long.
2697 * Attempt to pin user pages in memory without taking mm->mmap_lock.
2698 * If not successful, it will fall back to taking the lock and
2699 * calling get_user_pages().
2701 * Returns number of pages pinned. This may be fewer than the number requested.
2702 * If nr_pages is 0 or negative, returns 0. If no pages were pinned, returns
2705 int get_user_pages_fast(unsigned long start
, int nr_pages
,
2706 unsigned int gup_flags
, struct page
**pages
)
2708 if (!is_valid_gup_flags(gup_flags
))
2712 * The caller may or may not have explicitly set FOLL_GET; either way is
2713 * OK. However, internally (within mm/gup.c), gup fast variants must set
2714 * FOLL_GET, because gup fast is always a "pin with a +1 page refcount"
2717 gup_flags
|= FOLL_GET
;
2718 return internal_get_user_pages_fast(start
, nr_pages
, gup_flags
, pages
);
2720 EXPORT_SYMBOL_GPL(get_user_pages_fast
);
2723 * pin_user_pages_fast() - pin user pages in memory without taking locks
2725 * @start: starting user address
2726 * @nr_pages: number of pages from start to pin
2727 * @gup_flags: flags modifying pin behaviour
2728 * @pages: array that receives pointers to the pages pinned.
2729 * Should be at least nr_pages long.
2731 * Nearly the same as get_user_pages_fast(), except that FOLL_PIN is set. See
2732 * get_user_pages_fast() for documentation on the function arguments, because
2733 * the arguments here are identical.
2735 * FOLL_PIN means that the pages must be released via unpin_user_page(). Please
2736 * see Documentation/core-api/pin_user_pages.rst for further details.
2738 int pin_user_pages_fast(unsigned long start
, int nr_pages
,
2739 unsigned int gup_flags
, struct page
**pages
)
2741 /* FOLL_GET and FOLL_PIN are mutually exclusive. */
2742 if (WARN_ON_ONCE(gup_flags
& FOLL_GET
))
2745 gup_flags
|= FOLL_PIN
;
2746 return internal_get_user_pages_fast(start
, nr_pages
, gup_flags
, pages
);
2748 EXPORT_SYMBOL_GPL(pin_user_pages_fast
);
2751 * This is the FOLL_PIN equivalent of get_user_pages_fast_only(). Behavior
2752 * is the same, except that this one sets FOLL_PIN instead of FOLL_GET.
2754 * The API rules are the same, too: no negative values may be returned.
2756 int pin_user_pages_fast_only(unsigned long start
, int nr_pages
,
2757 unsigned int gup_flags
, struct page
**pages
)
2762 * FOLL_GET and FOLL_PIN are mutually exclusive. Note that the API
2763 * rules require returning 0, rather than -errno:
2765 if (WARN_ON_ONCE(gup_flags
& FOLL_GET
))
2768 * FOLL_FAST_ONLY is required in order to match the API description of
2769 * this routine: no fall back to regular ("slow") GUP.
2771 gup_flags
|= (FOLL_PIN
| FOLL_FAST_ONLY
);
2772 nr_pinned
= internal_get_user_pages_fast(start
, nr_pages
, gup_flags
,
2775 * This routine is not allowed to return negative values. However,
2776 * internal_get_user_pages_fast() *can* return -errno. Therefore,
2777 * correct for that here:
2784 EXPORT_SYMBOL_GPL(pin_user_pages_fast_only
);
2787 * pin_user_pages_remote() - pin pages of a remote process
2789 * @mm: mm_struct of target mm
2790 * @start: starting user address
2791 * @nr_pages: number of pages from start to pin
2792 * @gup_flags: flags modifying lookup behaviour
2793 * @pages: array that receives pointers to the pages pinned.
2794 * Should be at least nr_pages long. Or NULL, if caller
2795 * only intends to ensure the pages are faulted in.
2796 * @vmas: array of pointers to vmas corresponding to each page.
2797 * Or NULL if the caller does not require them.
2798 * @locked: pointer to lock flag indicating whether lock is held and
2799 * subsequently whether VM_FAULT_RETRY functionality can be
2800 * utilised. Lock must initially be held.
2802 * Nearly the same as get_user_pages_remote(), except that FOLL_PIN is set. See
2803 * get_user_pages_remote() for documentation on the function arguments, because
2804 * the arguments here are identical.
2806 * FOLL_PIN means that the pages must be released via unpin_user_page(). Please
2807 * see Documentation/core-api/pin_user_pages.rst for details.
2809 long pin_user_pages_remote(struct mm_struct
*mm
,
2810 unsigned long start
, unsigned long nr_pages
,
2811 unsigned int gup_flags
, struct page
**pages
,
2812 struct vm_area_struct
**vmas
, int *locked
)
2814 /* FOLL_GET and FOLL_PIN are mutually exclusive. */
2815 if (WARN_ON_ONCE(gup_flags
& FOLL_GET
))
2818 gup_flags
|= FOLL_PIN
;
2819 return __get_user_pages_remote(mm
, start
, nr_pages
, gup_flags
,
2820 pages
, vmas
, locked
);
2822 EXPORT_SYMBOL(pin_user_pages_remote
);
2825 * pin_user_pages() - pin user pages in memory for use by other devices
2827 * @start: starting user address
2828 * @nr_pages: number of pages from start to pin
2829 * @gup_flags: flags modifying lookup behaviour
2830 * @pages: array that receives pointers to the pages pinned.
2831 * Should be at least nr_pages long. Or NULL, if caller
2832 * only intends to ensure the pages are faulted in.
2833 * @vmas: array of pointers to vmas corresponding to each page.
2834 * Or NULL if the caller does not require them.
2836 * Nearly the same as get_user_pages(), except that FOLL_TOUCH is not set, and
2839 * FOLL_PIN means that the pages must be released via unpin_user_page(). Please
2840 * see Documentation/core-api/pin_user_pages.rst for details.
2842 long pin_user_pages(unsigned long start
, unsigned long nr_pages
,
2843 unsigned int gup_flags
, struct page
**pages
,
2844 struct vm_area_struct
**vmas
)
2846 /* FOLL_GET and FOLL_PIN are mutually exclusive. */
2847 if (WARN_ON_ONCE(gup_flags
& FOLL_GET
))
2850 gup_flags
|= FOLL_PIN
;
2851 return __gup_longterm_locked(current
->mm
, start
, nr_pages
,
2852 pages
, vmas
, gup_flags
);
2854 EXPORT_SYMBOL(pin_user_pages
);
2857 * pin_user_pages_unlocked() is the FOLL_PIN variant of
2858 * get_user_pages_unlocked(). Behavior is the same, except that this one sets
2859 * FOLL_PIN and rejects FOLL_GET.
2861 long pin_user_pages_unlocked(unsigned long start
, unsigned long nr_pages
,
2862 struct page
**pages
, unsigned int gup_flags
)
2864 /* FOLL_GET and FOLL_PIN are mutually exclusive. */
2865 if (WARN_ON_ONCE(gup_flags
& FOLL_GET
))
2868 gup_flags
|= FOLL_PIN
;
2869 return get_user_pages_unlocked(start
, nr_pages
, pages
, gup_flags
);
2871 EXPORT_SYMBOL(pin_user_pages_unlocked
);
2874 * pin_user_pages_locked() is the FOLL_PIN variant of get_user_pages_locked().
2875 * Behavior is the same, except that this one sets FOLL_PIN and rejects
2878 long pin_user_pages_locked(unsigned long start
, unsigned long nr_pages
,
2879 unsigned int gup_flags
, struct page
**pages
,
2883 * FIXME: Current FOLL_LONGTERM behavior is incompatible with
2884 * FAULT_FLAG_ALLOW_RETRY because of the FS DAX check requirement on
2885 * vmas. As there are no users of this flag in this call we simply
2886 * disallow this option for now.
2888 if (WARN_ON_ONCE(gup_flags
& FOLL_LONGTERM
))
2891 /* FOLL_GET and FOLL_PIN are mutually exclusive. */
2892 if (WARN_ON_ONCE(gup_flags
& FOLL_GET
))
2895 gup_flags
|= FOLL_PIN
;
2896 return __get_user_pages_locked(current
->mm
, start
, nr_pages
,
2897 pages
, NULL
, locked
,
2898 gup_flags
| FOLL_TOUCH
);
2900 EXPORT_SYMBOL(pin_user_pages_locked
);