1 #include <linux/kernel.h>
2 #include <linux/errno.h>
4 #include <linux/spinlock.h>
7 #include <linux/memremap.h>
8 #include <linux/pagemap.h>
9 #include <linux/rmap.h>
10 #include <linux/swap.h>
11 #include <linux/swapops.h>
13 #include <linux/sched/signal.h>
14 #include <linux/rwsem.h>
15 #include <linux/hugetlb.h>
17 #include <asm/mmu_context.h>
18 #include <asm/pgtable.h>
19 #include <asm/tlbflush.h>
21 #include <linux/migrate.h>
22 #include <linux/mm_inline.h>
23 #include <linux/mmu_notifier.h>
24 #include <asm/tlbflush.h>
29 static struct page
*__alloc_nonmovable_userpage(struct page
*page
,
30 unsigned long private, int **result
)
32 return alloc_page(GFP_HIGHUSER
);
35 static bool __need_migrate_cma_page(struct page
*page
,
36 struct vm_area_struct
*vma
,
37 unsigned long start
, unsigned int flags
)
39 if (!(flags
& FOLL_GET
) || !(flags
& FOLL_CMA
))
42 if (!is_migrate_cma_page(page
))
45 if ((vma
->vm_flags
& VM_STACK_INCOMPLETE_SETUP
) ==
46 VM_STACK_INCOMPLETE_SETUP
)
51 if (WARN_ON(!PageLRU(page
))) {
52 __dump_page(page
, "non-lru cma page");
60 static int __isolate_cma_pinpage(struct page
*page
)
62 struct zone
*zone
= page_zone(page
);
63 struct lruvec
*lruvec
;
65 spin_lock_irq(zone_lru_lock(zone
));
66 if (__isolate_lru_page(page
, 0) != 0) {
67 spin_unlock_irq(zone_lru_lock(zone
));
68 dump_page(page
, "failed to isolate lru page");
71 lruvec
= mem_cgroup_page_lruvec(page
, zone
->zone_pgdat
);
72 del_page_from_lru_list(page
, lruvec
, page_lru(page
));
74 spin_unlock_irq(zone_lru_lock(zone
));
79 static int __migrate_cma_pinpage(struct page
*page
, struct vm_area_struct
*vma
)
81 struct list_head migratepages
;
85 INIT_LIST_HEAD(&migratepages
);
87 list_add(&page
->lru
, &migratepages
);
88 inc_zone_page_state(page
, NR_ISOLATED_ANON
+ page_is_file_cache(page
));
90 while (!list_empty(&migratepages
) && tries
++ < 5) {
91 ret
= migrate_pages(&migratepages
, __alloc_nonmovable_userpage
,
92 NULL
, 0, MIGRATE_SYNC
, MR_CMA
);
96 putback_movable_pages(&migratepages
);
97 pr_err("%s: migration failed %p[%#lx]\n", __func__
,
98 page
, page_to_pfn(page
));
105 static bool __need_migrate_cma_page(struct page
*page
,
106 struct vm_area_struct
*vma
,
107 unsigned long start
, unsigned int flags
)
111 static int __migrate_cma_pinpage(struct page
*page
, struct vm_area_struct
*vma
)
117 static struct page
*no_page_table(struct vm_area_struct
*vma
,
121 * When core dumping an enormous anonymous area that nobody
122 * has touched so far, we don't want to allocate unnecessary pages or
123 * page tables. Return error instead of NULL to skip handle_mm_fault,
124 * then get_dump_page() will return NULL to leave a hole in the dump.
125 * But we can only make this optimization where a hole would surely
126 * be zero-filled if handle_mm_fault() actually did handle it.
128 if ((flags
& FOLL_DUMP
) && (!vma
->vm_ops
|| !vma
->vm_ops
->fault
))
129 return ERR_PTR(-EFAULT
);
133 static int follow_pfn_pte(struct vm_area_struct
*vma
, unsigned long address
,
134 pte_t
*pte
, unsigned int flags
)
136 /* No page to get reference */
137 if (flags
& FOLL_GET
)
140 if (flags
& FOLL_TOUCH
) {
143 if (flags
& FOLL_WRITE
)
144 entry
= pte_mkdirty(entry
);
145 entry
= pte_mkyoung(entry
);
147 if (!pte_same(*pte
, entry
)) {
148 set_pte_at(vma
->vm_mm
, address
, pte
, entry
);
149 update_mmu_cache(vma
, address
, pte
);
153 /* Proper page table entry exists, but no corresponding struct page */
158 * FOLL_FORCE can write to even unwritable pte's, but only
159 * after we've gone through a COW cycle and they are dirty.
161 static inline bool can_follow_write_pte(pte_t pte
, unsigned int flags
)
163 return pte_write(pte
) ||
164 ((flags
& FOLL_FORCE
) && (flags
& FOLL_COW
) && pte_dirty(pte
));
167 static struct page
*follow_page_pte(struct vm_area_struct
*vma
,
168 unsigned long address
, pmd_t
*pmd
, unsigned int flags
)
170 struct mm_struct
*mm
= vma
->vm_mm
;
171 struct dev_pagemap
*pgmap
= NULL
;
177 if (unlikely(pmd_bad(*pmd
)))
178 return no_page_table(vma
, flags
);
180 ptep
= pte_offset_map_lock(mm
, pmd
, address
, &ptl
);
182 if (!pte_present(pte
)) {
185 * KSM's break_ksm() relies upon recognizing a ksm page
186 * even while it is being migrated, so for that case we
187 * need migration_entry_wait().
189 if (likely(!(flags
& FOLL_MIGRATION
)))
193 entry
= pte_to_swp_entry(pte
);
194 if (!is_migration_entry(entry
))
196 pte_unmap_unlock(ptep
, ptl
);
197 migration_entry_wait(mm
, pmd
, address
);
200 if ((flags
& FOLL_NUMA
) && pte_protnone(pte
))
202 if ((flags
& FOLL_WRITE
) && !can_follow_write_pte(pte
, flags
)) {
203 pte_unmap_unlock(ptep
, ptl
);
207 page
= vm_normal_page(vma
, address
, pte
);
208 if (!page
&& pte_devmap(pte
) && (flags
& FOLL_GET
)) {
210 * Only return device mapping pages in the FOLL_GET case since
211 * they are only valid while holding the pgmap reference.
213 pgmap
= get_dev_pagemap(pte_pfn(pte
), NULL
);
215 page
= pte_page(pte
);
218 } else if (unlikely(!page
)) {
219 if (flags
& FOLL_DUMP
) {
220 /* Avoid special (like zero) pages in core dumps */
221 page
= ERR_PTR(-EFAULT
);
225 if (is_zero_pfn(pte_pfn(pte
))) {
226 page
= pte_page(pte
);
230 ret
= follow_pfn_pte(vma
, address
, ptep
, flags
);
236 if (__need_migrate_cma_page(page
, vma
, address
, flags
)) {
237 if (__isolate_cma_pinpage(page
)) {
238 pr_warn("%s: Failed to migrate a cma page\n", __func__
);
239 pr_warn("because of racing with compaction.\n");
240 WARN(1, "Please try again get_user_pages()\n");
241 page
= ERR_PTR(-EBUSY
);
244 pte_unmap_unlock(ptep
, ptl
);
245 if (__migrate_cma_pinpage(page
, vma
)) {
246 ptep
= pte_offset_map_lock(mm
, pmd
, address
, &ptl
);
248 struct page
*old_page
= page
;
250 migration_entry_wait(mm
, pmd
, address
);
251 ptep
= pte_offset_map_lock(mm
, pmd
, address
, &ptl
);
252 update_mmu_cache(vma
, address
, ptep
);
254 set_pte_at_notify(mm
, address
, ptep
, pte
);
255 page
= vm_normal_page(vma
, address
, pte
);
258 pr_debug("cma: cma page %p[%#lx] migrated to new "
259 "page %p[%#lx]\n", old_page
,
260 page_to_pfn(old_page
),
261 page
, page_to_pfn(page
));
265 if (flags
& FOLL_SPLIT
&& PageTransCompound(page
)) {
268 pte_unmap_unlock(ptep
, ptl
);
270 ret
= split_huge_page(page
);
278 if (flags
& FOLL_GET
) {
281 /* drop the pgmap reference now that we hold the page */
283 put_dev_pagemap(pgmap
);
287 if (flags
& FOLL_TOUCH
) {
288 if ((flags
& FOLL_WRITE
) &&
289 !pte_dirty(pte
) && !PageDirty(page
))
290 set_page_dirty(page
);
292 * pte_mkyoung() would be more correct here, but atomic care
293 * is needed to avoid losing the dirty bit: it is easier to use
294 * mark_page_accessed().
296 mark_page_accessed(page
);
298 if ((flags
& FOLL_MLOCK
) && (vma
->vm_flags
& VM_LOCKED
)) {
299 /* Do not mlock pte-mapped THP */
300 if (PageTransCompound(page
))
304 * The preliminary mapping check is mainly to avoid the
305 * pointless overhead of lock_page on the ZERO_PAGE
306 * which might bounce very badly if there is contention.
308 * If the page is already locked, we don't need to
309 * handle it now - vmscan will handle it later if and
310 * when it attempts to reclaim the page.
312 if (page
->mapping
&& trylock_page(page
)) {
313 lru_add_drain(); /* push cached pages to LRU */
315 * Because we lock page here, and migration is
316 * blocked by the pte's page reference, and we
317 * know the page is still mapped, we don't even
318 * need to check for file-cache page truncation.
320 mlock_vma_page(page
);
325 pte_unmap_unlock(ptep
, ptl
);
328 pte_unmap_unlock(ptep
, ptl
);
331 return no_page_table(vma
, flags
);
334 static struct page
*follow_pmd_mask(struct vm_area_struct
*vma
,
335 unsigned long address
, pud_t
*pudp
,
336 unsigned int flags
, unsigned int *page_mask
)
341 struct mm_struct
*mm
= vma
->vm_mm
;
343 pmd
= pmd_offset(pudp
, address
);
345 return no_page_table(vma
, flags
);
346 if (pmd_huge(*pmd
) && vma
->vm_flags
& VM_HUGETLB
) {
347 page
= follow_huge_pmd(mm
, address
, pmd
, flags
);
350 return no_page_table(vma
, flags
);
352 if (is_hugepd(__hugepd(pmd_val(*pmd
)))) {
353 page
= follow_huge_pd(vma
, address
,
354 __hugepd(pmd_val(*pmd
)), flags
,
358 return no_page_table(vma
, flags
);
361 if (!pmd_present(*pmd
)) {
362 if (likely(!(flags
& FOLL_MIGRATION
)))
363 return no_page_table(vma
, flags
);
364 VM_BUG_ON(thp_migration_supported() &&
365 !is_pmd_migration_entry(*pmd
));
366 if (is_pmd_migration_entry(*pmd
))
367 pmd_migration_entry_wait(mm
, pmd
);
370 if (pmd_devmap(*pmd
)) {
371 ptl
= pmd_lock(mm
, pmd
);
372 page
= follow_devmap_pmd(vma
, address
, pmd
, flags
);
377 if (likely(!pmd_trans_huge(*pmd
)))
378 return follow_page_pte(vma
, address
, pmd
, flags
);
380 if ((flags
& FOLL_NUMA
) && pmd_protnone(*pmd
))
381 return no_page_table(vma
, flags
);
384 ptl
= pmd_lock(mm
, pmd
);
385 if (unlikely(!pmd_present(*pmd
))) {
387 if (likely(!(flags
& FOLL_MIGRATION
)))
388 return no_page_table(vma
, flags
);
389 pmd_migration_entry_wait(mm
, pmd
);
392 if (unlikely(!pmd_trans_huge(*pmd
))) {
394 return follow_page_pte(vma
, address
, pmd
, flags
);
396 if (flags
& FOLL_SPLIT
) {
398 page
= pmd_page(*pmd
);
399 if (is_huge_zero_page(page
)) {
402 split_huge_pmd(vma
, pmd
, address
);
403 if (pmd_trans_unstable(pmd
))
409 ret
= split_huge_page(page
);
413 return no_page_table(vma
, flags
);
416 return ret
? ERR_PTR(ret
) :
417 follow_page_pte(vma
, address
, pmd
, flags
);
419 page
= follow_trans_huge_pmd(vma
, address
, pmd
, flags
);
421 *page_mask
= HPAGE_PMD_NR
- 1;
426 static struct page
*follow_pud_mask(struct vm_area_struct
*vma
,
427 unsigned long address
, p4d_t
*p4dp
,
428 unsigned int flags
, unsigned int *page_mask
)
433 struct mm_struct
*mm
= vma
->vm_mm
;
435 pud
= pud_offset(p4dp
, address
);
437 return no_page_table(vma
, flags
);
438 if (pud_huge(*pud
) && vma
->vm_flags
& VM_HUGETLB
) {
439 page
= follow_huge_pud(mm
, address
, pud
, flags
);
442 return no_page_table(vma
, flags
);
444 if (is_hugepd(__hugepd(pud_val(*pud
)))) {
445 page
= follow_huge_pd(vma
, address
,
446 __hugepd(pud_val(*pud
)), flags
,
450 return no_page_table(vma
, flags
);
452 if (pud_devmap(*pud
)) {
453 ptl
= pud_lock(mm
, pud
);
454 page
= follow_devmap_pud(vma
, address
, pud
, flags
);
459 if (unlikely(pud_bad(*pud
)))
460 return no_page_table(vma
, flags
);
462 return follow_pmd_mask(vma
, address
, pud
, flags
, page_mask
);
466 static struct page
*follow_p4d_mask(struct vm_area_struct
*vma
,
467 unsigned long address
, pgd_t
*pgdp
,
468 unsigned int flags
, unsigned int *page_mask
)
473 p4d
= p4d_offset(pgdp
, address
);
475 return no_page_table(vma
, flags
);
476 BUILD_BUG_ON(p4d_huge(*p4d
));
477 if (unlikely(p4d_bad(*p4d
)))
478 return no_page_table(vma
, flags
);
480 if (is_hugepd(__hugepd(p4d_val(*p4d
)))) {
481 page
= follow_huge_pd(vma
, address
,
482 __hugepd(p4d_val(*p4d
)), flags
,
486 return no_page_table(vma
, flags
);
488 return follow_pud_mask(vma
, address
, p4d
, flags
, page_mask
);
492 * follow_page_mask - look up a page descriptor from a user-virtual address
493 * @vma: vm_area_struct mapping @address
494 * @address: virtual address to look up
495 * @flags: flags modifying lookup behaviour
496 * @page_mask: on output, *page_mask is set according to the size of the page
498 * @flags can have FOLL_ flags set, defined in <linux/mm.h>
500 * Returns the mapped (struct page *), %NULL if no mapping exists, or
501 * an error pointer if there is a mapping to something not represented
502 * by a page descriptor (see also vm_normal_page()).
504 struct page
*follow_page_mask(struct vm_area_struct
*vma
,
505 unsigned long address
, unsigned int flags
,
506 unsigned int *page_mask
)
510 struct mm_struct
*mm
= vma
->vm_mm
;
514 /* make this handle hugepd */
515 page
= follow_huge_addr(mm
, address
, flags
& FOLL_WRITE
);
517 BUG_ON(flags
& FOLL_GET
);
521 pgd
= pgd_offset(mm
, address
);
523 if (pgd_none(*pgd
) || unlikely(pgd_bad(*pgd
)))
524 return no_page_table(vma
, flags
);
526 if (pgd_huge(*pgd
)) {
527 page
= follow_huge_pgd(mm
, address
, pgd
, flags
);
530 return no_page_table(vma
, flags
);
532 if (is_hugepd(__hugepd(pgd_val(*pgd
)))) {
533 page
= follow_huge_pd(vma
, address
,
534 __hugepd(pgd_val(*pgd
)), flags
,
538 return no_page_table(vma
, flags
);
541 return follow_p4d_mask(vma
, address
, pgd
, flags
, page_mask
);
544 static int get_gate_page(struct mm_struct
*mm
, unsigned long address
,
545 unsigned int gup_flags
, struct vm_area_struct
**vma
,
555 /* user gate pages are read-only */
556 if (gup_flags
& FOLL_WRITE
)
558 if (address
> TASK_SIZE
)
559 pgd
= pgd_offset_k(address
);
561 pgd
= pgd_offset_gate(mm
, address
);
562 BUG_ON(pgd_none(*pgd
));
563 p4d
= p4d_offset(pgd
, address
);
564 BUG_ON(p4d_none(*p4d
));
565 pud
= pud_offset(p4d
, address
);
566 BUG_ON(pud_none(*pud
));
567 pmd
= pmd_offset(pud
, address
);
568 if (!pmd_present(*pmd
))
570 VM_BUG_ON(pmd_trans_huge(*pmd
));
571 pte
= pte_offset_map(pmd
, address
);
574 *vma
= get_gate_vma(mm
);
577 *page
= vm_normal_page(*vma
, address
, *pte
);
579 if ((gup_flags
& FOLL_DUMP
) || !is_zero_pfn(pte_pfn(*pte
)))
581 *page
= pte_page(*pte
);
584 * This should never happen (a device public page in the gate
587 if (is_device_public_page(*page
))
599 * mmap_sem must be held on entry. If @nonblocking != NULL and
600 * *@flags does not include FOLL_NOWAIT, the mmap_sem may be released.
601 * If it is, *@nonblocking will be set to 0 and -EBUSY returned.
603 static int faultin_page(struct task_struct
*tsk
, struct vm_area_struct
*vma
,
604 unsigned long address
, unsigned int *flags
, int *nonblocking
)
606 unsigned int fault_flags
= 0;
609 /* mlock all present pages, but do not fault in new pages */
610 if ((*flags
& (FOLL_POPULATE
| FOLL_MLOCK
)) == FOLL_MLOCK
)
612 if (*flags
& FOLL_WRITE
)
613 fault_flags
|= FAULT_FLAG_WRITE
;
614 if (*flags
& FOLL_REMOTE
)
615 fault_flags
|= FAULT_FLAG_REMOTE
;
617 fault_flags
|= FAULT_FLAG_ALLOW_RETRY
;
618 if (*flags
& FOLL_NOWAIT
)
619 fault_flags
|= FAULT_FLAG_ALLOW_RETRY
| FAULT_FLAG_RETRY_NOWAIT
;
620 if (*flags
& FOLL_TRIED
) {
621 VM_WARN_ON_ONCE(fault_flags
& FAULT_FLAG_ALLOW_RETRY
);
622 fault_flags
|= FAULT_FLAG_TRIED
;
625 ret
= handle_mm_fault(vma
, address
, fault_flags
);
626 if (ret
& VM_FAULT_ERROR
) {
627 int err
= vm_fault_to_errno(ret
, *flags
);
635 if (ret
& VM_FAULT_MAJOR
)
641 if (ret
& VM_FAULT_RETRY
) {
648 * The VM_FAULT_WRITE bit tells us that do_wp_page has broken COW when
649 * necessary, even if maybe_mkwrite decided not to set pte_write. We
650 * can thus safely do subsequent page lookups as if they were reads.
651 * But only do so when looping for pte_write is futile: in some cases
652 * userspace may also be wanting to write to the gotten user page,
653 * which a read fault here might prevent (a readonly page might get
654 * reCOWed by userspace write).
656 if ((ret
& VM_FAULT_WRITE
) && !(vma
->vm_flags
& VM_WRITE
))
661 static int check_vma_flags(struct vm_area_struct
*vma
, unsigned long gup_flags
)
663 vm_flags_t vm_flags
= vma
->vm_flags
;
664 int write
= (gup_flags
& FOLL_WRITE
);
665 int foreign
= (gup_flags
& FOLL_REMOTE
);
667 if (vm_flags
& (VM_IO
| VM_PFNMAP
))
670 if (gup_flags
& FOLL_ANON
&& !vma_is_anonymous(vma
))
674 if (!(vm_flags
& VM_WRITE
)) {
675 if (!(gup_flags
& FOLL_FORCE
))
678 * We used to let the write,force case do COW in a
679 * VM_MAYWRITE VM_SHARED !VM_WRITE vma, so ptrace could
680 * set a breakpoint in a read-only mapping of an
681 * executable, without corrupting the file (yet only
682 * when that file had been opened for writing!).
683 * Anon pages in shared mappings are surprising: now
686 if (!is_cow_mapping(vm_flags
))
689 } else if (!(vm_flags
& VM_READ
)) {
690 if (!(gup_flags
& FOLL_FORCE
))
693 * Is there actually any vma we can reach here which does not
694 * have VM_MAYREAD set?
696 if (!(vm_flags
& VM_MAYREAD
))
700 * gups are always data accesses, not instruction
701 * fetches, so execute=false here
703 if (!arch_vma_access_permitted(vma
, write
, false, foreign
))
709 * __get_user_pages() - pin user pages in memory
710 * @tsk: task_struct of target task
711 * @mm: mm_struct of target mm
712 * @start: starting user address
713 * @nr_pages: number of pages from start to pin
714 * @gup_flags: flags modifying pin behaviour
715 * @pages: array that receives pointers to the pages pinned.
716 * Should be at least nr_pages long. Or NULL, if caller
717 * only intends to ensure the pages are faulted in.
718 * @vmas: array of pointers to vmas corresponding to each page.
719 * Or NULL if the caller does not require them.
720 * @nonblocking: whether waiting for disk IO or mmap_sem contention
722 * Returns number of pages pinned. This may be fewer than the number
723 * requested. If nr_pages is 0 or negative, returns 0. If no pages
724 * were pinned, returns -errno. Each page returned must be released
725 * with a put_page() call when it is finished with. vmas will only
726 * remain valid while mmap_sem is held.
728 * Must be called with mmap_sem held. It may be released. See below.
730 * __get_user_pages walks a process's page tables and takes a reference to
731 * each struct page that each user address corresponds to at a given
732 * instant. That is, it takes the page that would be accessed if a user
733 * thread accesses the given user virtual address at that instant.
735 * This does not guarantee that the page exists in the user mappings when
736 * __get_user_pages returns, and there may even be a completely different
737 * page there in some cases (eg. if mmapped pagecache has been invalidated
738 * and subsequently re faulted). However it does guarantee that the page
739 * won't be freed completely. And mostly callers simply care that the page
740 * contains data that was valid *at some point in time*. Typically, an IO
741 * or similar operation cannot guarantee anything stronger anyway because
742 * locks can't be held over the syscall boundary.
744 * If @gup_flags & FOLL_WRITE == 0, the page must not be written to. If
745 * the page is written to, set_page_dirty (or set_page_dirty_lock, as
746 * appropriate) must be called after the page is finished with, and
747 * before put_page is called.
749 * If @nonblocking != NULL, __get_user_pages will not wait for disk IO
750 * or mmap_sem contention, and if waiting is needed to pin all pages,
751 * *@nonblocking will be set to 0. Further, if @gup_flags does not
752 * include FOLL_NOWAIT, the mmap_sem will be released via up_read() in
755 * A caller using such a combination of @nonblocking and @gup_flags
756 * must therefore hold the mmap_sem for reading only, and recognize
757 * when it's been released. Otherwise, it must be held for either
758 * reading or writing and will not be released.
760 * In most cases, get_user_pages or get_user_pages_fast should be used
761 * instead of __get_user_pages. __get_user_pages should be used only if
762 * you need some special @gup_flags.
764 static long __get_user_pages(struct task_struct
*tsk
, struct mm_struct
*mm
,
765 unsigned long start
, unsigned long nr_pages
,
766 unsigned int gup_flags
, struct page
**pages
,
767 struct vm_area_struct
**vmas
, int *nonblocking
)
770 unsigned int page_mask
;
771 struct vm_area_struct
*vma
= NULL
;
776 VM_BUG_ON(!!pages
!= !!(gup_flags
& FOLL_GET
));
779 * If FOLL_FORCE is set then do not force a full fault as the hinting
780 * fault information is unrelated to the reference behaviour of a task
781 * using the address space
783 if (!(gup_flags
& FOLL_FORCE
))
784 gup_flags
|= FOLL_NUMA
;
786 if ((gup_flags
& FOLL_CMA
) != 0)
791 unsigned int foll_flags
= gup_flags
;
792 unsigned int page_increm
;
794 /* first iteration or cross vma bound */
795 if (!vma
|| start
>= vma
->vm_end
) {
796 vma
= find_extend_vma(mm
, start
);
797 if (!vma
&& in_gate_area(mm
, start
)) {
799 ret
= get_gate_page(mm
, start
& PAGE_MASK
,
801 pages
? &pages
[i
] : NULL
);
808 if (!vma
|| check_vma_flags(vma
, gup_flags
))
809 return i
? : -EFAULT
;
810 if (is_vm_hugetlb_page(vma
)) {
811 i
= follow_hugetlb_page(mm
, vma
, pages
, vmas
,
812 &start
, &nr_pages
, i
,
813 gup_flags
, nonblocking
);
819 * If we have a pending SIGKILL, don't keep faulting pages and
820 * potentially allocating memory.
822 if (unlikely(fatal_signal_pending(current
)))
823 return i
? i
: -ERESTARTSYS
;
825 page
= follow_page_mask(vma
, start
, foll_flags
, &page_mask
);
828 ret
= faultin_page(tsk
, vma
, start
, &foll_flags
,
843 } else if (PTR_ERR(page
) == -EEXIST
) {
845 * Proper page table entry exists, but no corresponding
849 } else if (IS_ERR(page
)) {
850 return i
? i
: PTR_ERR(page
);
854 flush_anon_page(vma
, page
, start
);
855 flush_dcache_page(page
);
863 page_increm
= 1 + (~(start
>> PAGE_SHIFT
) & page_mask
);
864 if (page_increm
> nr_pages
)
865 page_increm
= nr_pages
;
867 start
+= page_increm
* PAGE_SIZE
;
868 nr_pages
-= page_increm
;
873 static bool vma_permits_fault(struct vm_area_struct
*vma
,
874 unsigned int fault_flags
)
876 bool write
= !!(fault_flags
& FAULT_FLAG_WRITE
);
877 bool foreign
= !!(fault_flags
& FAULT_FLAG_REMOTE
);
878 vm_flags_t vm_flags
= write
? VM_WRITE
: VM_READ
;
880 if (!(vm_flags
& vma
->vm_flags
))
884 * The architecture might have a hardware protection
885 * mechanism other than read/write that can deny access.
887 * gup always represents data access, not instruction
888 * fetches, so execute=false here:
890 if (!arch_vma_access_permitted(vma
, write
, false, foreign
))
897 * fixup_user_fault() - manually resolve a user page fault
898 * @tsk: the task_struct to use for page fault accounting, or
899 * NULL if faults are not to be recorded.
900 * @mm: mm_struct of target mm
901 * @address: user address
902 * @fault_flags:flags to pass down to handle_mm_fault()
903 * @unlocked: did we unlock the mmap_sem while retrying, maybe NULL if caller
904 * does not allow retry
906 * This is meant to be called in the specific scenario where for locking reasons
907 * we try to access user memory in atomic context (within a pagefault_disable()
908 * section), this returns -EFAULT, and we want to resolve the user fault before
911 * Typically this is meant to be used by the futex code.
913 * The main difference with get_user_pages() is that this function will
914 * unconditionally call handle_mm_fault() which will in turn perform all the
915 * necessary SW fixup of the dirty and young bits in the PTE, while
916 * get_user_pages() only guarantees to update these in the struct page.
918 * This is important for some architectures where those bits also gate the
919 * access permission to the page because they are maintained in software. On
920 * such architectures, gup() will not be enough to make a subsequent access
923 * This function will not return with an unlocked mmap_sem. So it has not the
924 * same semantics wrt the @mm->mmap_sem as does filemap_fault().
926 int fixup_user_fault(struct task_struct
*tsk
, struct mm_struct
*mm
,
927 unsigned long address
, unsigned int fault_flags
,
930 struct vm_area_struct
*vma
;
934 fault_flags
|= FAULT_FLAG_ALLOW_RETRY
;
937 vma
= find_extend_vma(mm
, address
);
938 if (!vma
|| address
< vma
->vm_start
)
941 if (!vma_permits_fault(vma
, fault_flags
))
944 ret
= handle_mm_fault(vma
, address
, fault_flags
);
945 major
|= ret
& VM_FAULT_MAJOR
;
946 if (ret
& VM_FAULT_ERROR
) {
947 int err
= vm_fault_to_errno(ret
, 0);
954 if (ret
& VM_FAULT_RETRY
) {
955 down_read(&mm
->mmap_sem
);
956 if (!(fault_flags
& FAULT_FLAG_TRIED
)) {
958 fault_flags
&= ~FAULT_FLAG_ALLOW_RETRY
;
959 fault_flags
|= FAULT_FLAG_TRIED
;
972 EXPORT_SYMBOL_GPL(fixup_user_fault
);
974 static __always_inline
long __get_user_pages_locked(struct task_struct
*tsk
,
975 struct mm_struct
*mm
,
977 unsigned long nr_pages
,
979 struct vm_area_struct
**vmas
,
980 int *locked
, bool notify_drop
,
983 long ret
, pages_done
;
987 /* if VM_FAULT_RETRY can be returned, vmas become invalid */
989 /* check caller initialized locked */
990 BUG_ON(*locked
!= 1);
997 lock_dropped
= false;
999 ret
= __get_user_pages(tsk
, mm
, start
, nr_pages
, flags
, pages
,
1002 /* VM_FAULT_RETRY couldn't trigger, bypass */
1005 /* VM_FAULT_RETRY cannot return errors */
1008 BUG_ON(ret
>= nr_pages
);
1012 /* If it's a prefault don't insist harder */
1022 /* VM_FAULT_RETRY didn't trigger */
1027 /* VM_FAULT_RETRY triggered, so seek to the faulting offset */
1029 start
+= ret
<< PAGE_SHIFT
;
1032 * Repeat on the address that fired VM_FAULT_RETRY
1033 * without FAULT_FLAG_ALLOW_RETRY but with
1037 lock_dropped
= true;
1038 down_read(&mm
->mmap_sem
);
1039 ret
= __get_user_pages(tsk
, mm
, start
, 1, flags
| FOLL_TRIED
,
1054 if (notify_drop
&& lock_dropped
&& *locked
) {
1056 * We must let the caller know we temporarily dropped the lock
1057 * and so the critical section protected by it was lost.
1059 up_read(&mm
->mmap_sem
);
1066 * We can leverage the VM_FAULT_RETRY functionality in the page fault
1067 * paths better by using either get_user_pages_locked() or
1068 * get_user_pages_unlocked().
1070 * get_user_pages_locked() is suitable to replace the form:
1072 * down_read(&mm->mmap_sem);
1074 * get_user_pages(tsk, mm, ..., pages, NULL);
1075 * up_read(&mm->mmap_sem);
1080 * down_read(&mm->mmap_sem);
1082 * get_user_pages_locked(tsk, mm, ..., pages, &locked);
1084 * up_read(&mm->mmap_sem);
1086 long get_user_pages_locked(unsigned long start
, unsigned long nr_pages
,
1087 unsigned int gup_flags
, struct page
**pages
,
1090 return __get_user_pages_locked(current
, current
->mm
, start
, nr_pages
,
1091 pages
, NULL
, locked
, true,
1092 gup_flags
| FOLL_TOUCH
);
1094 EXPORT_SYMBOL(get_user_pages_locked
);
1097 * Same as get_user_pages_unlocked(...., FOLL_TOUCH) but it allows for
1098 * tsk, mm to be specified.
1100 * NOTE: here FOLL_TOUCH is not set implicitly and must be set by the
1101 * caller if required (just like with __get_user_pages). "FOLL_GET"
1102 * is set implicitly if "pages" is non-NULL.
1104 static __always_inline
long __get_user_pages_unlocked(struct task_struct
*tsk
,
1105 struct mm_struct
*mm
, unsigned long start
,
1106 unsigned long nr_pages
, struct page
**pages
,
1107 unsigned int gup_flags
)
1112 down_read(&mm
->mmap_sem
);
1113 ret
= __get_user_pages_locked(tsk
, mm
, start
, nr_pages
, pages
, NULL
,
1114 &locked
, false, gup_flags
);
1116 up_read(&mm
->mmap_sem
);
1121 * get_user_pages_unlocked() is suitable to replace the form:
1123 * down_read(&mm->mmap_sem);
1124 * get_user_pages(tsk, mm, ..., pages, NULL);
1125 * up_read(&mm->mmap_sem);
1129 * get_user_pages_unlocked(tsk, mm, ..., pages);
1131 * It is functionally equivalent to get_user_pages_fast so
1132 * get_user_pages_fast should be used instead if specific gup_flags
1133 * (e.g. FOLL_FORCE) are not required.
1135 long get_user_pages_unlocked(unsigned long start
, unsigned long nr_pages
,
1136 struct page
**pages
, unsigned int gup_flags
)
1138 return __get_user_pages_unlocked(current
, current
->mm
, start
, nr_pages
,
1139 pages
, gup_flags
| FOLL_TOUCH
);
1141 EXPORT_SYMBOL(get_user_pages_unlocked
);
1144 * get_user_pages_remote() - pin user pages in memory
1145 * @tsk: the task_struct to use for page fault accounting, or
1146 * NULL if faults are not to be recorded.
1147 * @mm: mm_struct of target mm
1148 * @start: starting user address
1149 * @nr_pages: number of pages from start to pin
1150 * @gup_flags: flags modifying lookup behaviour
1151 * @pages: array that receives pointers to the pages pinned.
1152 * Should be at least nr_pages long. Or NULL, if caller
1153 * only intends to ensure the pages are faulted in.
1154 * @vmas: array of pointers to vmas corresponding to each page.
1155 * Or NULL if the caller does not require them.
1156 * @locked: pointer to lock flag indicating whether lock is held and
1157 * subsequently whether VM_FAULT_RETRY functionality can be
1158 * utilised. Lock must initially be held.
1160 * Returns number of pages pinned. This may be fewer than the number
1161 * requested. If nr_pages is 0 or negative, returns 0. If no pages
1162 * were pinned, returns -errno. Each page returned must be released
1163 * with a put_page() call when it is finished with. vmas will only
1164 * remain valid while mmap_sem is held.
1166 * Must be called with mmap_sem held for read or write.
1168 * get_user_pages walks a process's page tables and takes a reference to
1169 * each struct page that each user address corresponds to at a given
1170 * instant. That is, it takes the page that would be accessed if a user
1171 * thread accesses the given user virtual address at that instant.
1173 * This does not guarantee that the page exists in the user mappings when
1174 * get_user_pages returns, and there may even be a completely different
1175 * page there in some cases (eg. if mmapped pagecache has been invalidated
1176 * and subsequently re faulted). However it does guarantee that the page
1177 * won't be freed completely. And mostly callers simply care that the page
1178 * contains data that was valid *at some point in time*. Typically, an IO
1179 * or similar operation cannot guarantee anything stronger anyway because
1180 * locks can't be held over the syscall boundary.
1182 * If gup_flags & FOLL_WRITE == 0, the page must not be written to. If the page
1183 * is written to, set_page_dirty (or set_page_dirty_lock, as appropriate) must
1184 * be called after the page is finished with, and before put_page is called.
1186 * get_user_pages is typically used for fewer-copy IO operations, to get a
1187 * handle on the memory by some means other than accesses via the user virtual
1188 * addresses. The pages may be submitted for DMA to devices or accessed via
1189 * their kernel linear mapping (via the kmap APIs). Care should be taken to
1190 * use the correct cache flushing APIs.
1192 * See also get_user_pages_fast, for performance critical applications.
1194 * get_user_pages should be phased out in favor of
1195 * get_user_pages_locked|unlocked or get_user_pages_fast. Nothing
1196 * should use get_user_pages because it cannot pass
1197 * FAULT_FLAG_ALLOW_RETRY to handle_mm_fault.
1199 long get_user_pages_remote(struct task_struct
*tsk
, struct mm_struct
*mm
,
1200 unsigned long start
, unsigned long nr_pages
,
1201 unsigned int gup_flags
, struct page
**pages
,
1202 struct vm_area_struct
**vmas
, int *locked
)
1204 return __get_user_pages_locked(tsk
, mm
, start
, nr_pages
, pages
, vmas
,
1206 gup_flags
| FOLL_TOUCH
| FOLL_REMOTE
);
1208 EXPORT_SYMBOL(get_user_pages_remote
);
1211 * This is the same as get_user_pages_remote(), just with a
1212 * less-flexible calling convention where we assume that the task
1213 * and mm being operated on are the current task's and don't allow
1214 * passing of a locked parameter. We also obviously don't pass
1215 * FOLL_REMOTE in here.
1217 long get_user_pages(unsigned long start
, unsigned long nr_pages
,
1218 unsigned int gup_flags
, struct page
**pages
,
1219 struct vm_area_struct
**vmas
)
1221 return __get_user_pages_locked(current
, current
->mm
, start
, nr_pages
,
1222 pages
, vmas
, NULL
, false,
1223 gup_flags
| FOLL_TOUCH
);
1225 EXPORT_SYMBOL(get_user_pages
);
1227 #ifdef CONFIG_FS_DAX
1229 * This is the same as get_user_pages() in that it assumes we are
1230 * operating on the current task's mm, but it goes further to validate
1231 * that the vmas associated with the address range are suitable for
1232 * longterm elevated page reference counts. For example, filesystem-dax
1233 * mappings are subject to the lifetime enforced by the filesystem and
1234 * we need guarantees that longterm users like RDMA and V4L2 only
1235 * establish mappings that have a kernel enforced revocation mechanism.
1237 * "longterm" == userspace controlled elevated page count lifetime.
1238 * Contrast this to iov_iter_get_pages() usages which are transient.
1240 long get_user_pages_longterm(unsigned long start
, unsigned long nr_pages
,
1241 unsigned int gup_flags
, struct page
**pages
,
1242 struct vm_area_struct
**vmas_arg
)
1244 struct vm_area_struct
**vmas
= vmas_arg
;
1245 struct vm_area_struct
*vma_prev
= NULL
;
1252 vmas
= kcalloc(nr_pages
, sizeof(struct vm_area_struct
*),
1258 rc
= get_user_pages(start
, nr_pages
, gup_flags
, pages
, vmas
);
1260 for (i
= 0; i
< rc
; i
++) {
1261 struct vm_area_struct
*vma
= vmas
[i
];
1263 if (vma
== vma_prev
)
1268 if (vma_is_fsdax(vma
))
1273 * Either get_user_pages() failed, or the vma validation
1274 * succeeded, in either case we don't need to put_page() before
1280 for (i
= 0; i
< rc
; i
++)
1284 if (vmas
!= vmas_arg
)
1288 EXPORT_SYMBOL(get_user_pages_longterm
);
1289 #endif /* CONFIG_FS_DAX */
1292 * populate_vma_page_range() - populate a range of pages in the vma.
1294 * @start: start address
1298 * This takes care of mlocking the pages too if VM_LOCKED is set.
1300 * return 0 on success, negative error code on error.
1302 * vma->vm_mm->mmap_sem must be held.
1304 * If @nonblocking is NULL, it may be held for read or write and will
1307 * If @nonblocking is non-NULL, it must held for read only and may be
1308 * released. If it's released, *@nonblocking will be set to 0.
1310 long populate_vma_page_range(struct vm_area_struct
*vma
,
1311 unsigned long start
, unsigned long end
, int *nonblocking
)
1313 struct mm_struct
*mm
= vma
->vm_mm
;
1314 unsigned long nr_pages
= (end
- start
) / PAGE_SIZE
;
1317 VM_BUG_ON(start
& ~PAGE_MASK
);
1318 VM_BUG_ON(end
& ~PAGE_MASK
);
1319 VM_BUG_ON_VMA(start
< vma
->vm_start
, vma
);
1320 VM_BUG_ON_VMA(end
> vma
->vm_end
, vma
);
1321 VM_BUG_ON_MM(!rwsem_is_locked(&mm
->mmap_sem
), mm
);
1323 gup_flags
= FOLL_TOUCH
| FOLL_POPULATE
| FOLL_MLOCK
;
1324 if (vma
->vm_flags
& VM_LOCKONFAULT
)
1325 gup_flags
&= ~FOLL_POPULATE
;
1327 * We want to touch writable mappings with a write fault in order
1328 * to break COW, except for shared mappings because these don't COW
1329 * and we would not want to dirty them for nothing.
1331 if ((vma
->vm_flags
& (VM_WRITE
| VM_SHARED
)) == VM_WRITE
)
1332 gup_flags
|= FOLL_WRITE
;
1335 * We want mlock to succeed for regions that have any permissions
1336 * other than PROT_NONE.
1338 if (vma
->vm_flags
& (VM_READ
| VM_WRITE
| VM_EXEC
))
1339 gup_flags
|= FOLL_FORCE
;
1342 * We made sure addr is within a VMA, so the following will
1343 * not result in a stack expansion that recurses back here.
1345 return __get_user_pages(current
, mm
, start
, nr_pages
, gup_flags
,
1346 NULL
, NULL
, nonblocking
);
1350 * __mm_populate - populate and/or mlock pages within a range of address space.
1352 * This is used to implement mlock() and the MAP_POPULATE / MAP_LOCKED mmap
1353 * flags. VMAs must be already marked with the desired vm_flags, and
1354 * mmap_sem must not be held.
1356 int __mm_populate(unsigned long start
, unsigned long len
, int ignore_errors
)
1358 struct mm_struct
*mm
= current
->mm
;
1359 unsigned long end
, nstart
, nend
;
1360 struct vm_area_struct
*vma
= NULL
;
1366 for (nstart
= start
; nstart
< end
; nstart
= nend
) {
1368 * We want to fault in pages for [nstart; end) address range.
1369 * Find first corresponding VMA.
1373 down_read(&mm
->mmap_sem
);
1374 vma
= find_vma(mm
, nstart
);
1375 } else if (nstart
>= vma
->vm_end
)
1377 if (!vma
|| vma
->vm_start
>= end
)
1380 * Set [nstart; nend) to intersection of desired address
1381 * range with the first VMA. Also, skip undesirable VMA types.
1383 nend
= min(end
, vma
->vm_end
);
1384 if (vma
->vm_flags
& (VM_IO
| VM_PFNMAP
))
1386 if (nstart
< vma
->vm_start
)
1387 nstart
= vma
->vm_start
;
1389 * Now fault in a range of pages. populate_vma_page_range()
1390 * double checks the vma flags, so that it won't mlock pages
1391 * if the vma was already munlocked.
1393 ret
= populate_vma_page_range(vma
, nstart
, nend
, &locked
);
1395 if (ignore_errors
) {
1397 continue; /* continue at next VMA */
1401 nend
= nstart
+ ret
* PAGE_SIZE
;
1405 up_read(&mm
->mmap_sem
);
1406 return ret
; /* 0 or negative error code */
1410 * get_dump_page() - pin user page in memory while writing it to core dump
1411 * @addr: user address
1413 * Returns struct page pointer of user page pinned for dump,
1414 * to be freed afterwards by put_page().
1416 * Returns NULL on any kind of failure - a hole must then be inserted into
1417 * the corefile, to preserve alignment with its headers; and also returns
1418 * NULL wherever the ZERO_PAGE, or an anonymous pte_none, has been found -
1419 * allowing a hole to be left in the corefile to save diskspace.
1421 * Called without mmap_sem, but after all other threads have been killed.
1423 #ifdef CONFIG_ELF_CORE
1424 struct page
*get_dump_page(unsigned long addr
)
1426 struct vm_area_struct
*vma
;
1429 if (__get_user_pages(current
, current
->mm
, addr
, 1,
1430 FOLL_FORCE
| FOLL_DUMP
| FOLL_GET
, &page
, &vma
,
1433 flush_cache_page(vma
, addr
, page_to_pfn(page
));
1436 #endif /* CONFIG_ELF_CORE */
1441 * get_user_pages_fast attempts to pin user pages by walking the page
1442 * tables directly and avoids taking locks. Thus the walker needs to be
1443 * protected from page table pages being freed from under it, and should
1444 * block any THP splits.
1446 * One way to achieve this is to have the walker disable interrupts, and
1447 * rely on IPIs from the TLB flushing code blocking before the page table
1448 * pages are freed. This is unsuitable for architectures that do not need
1449 * to broadcast an IPI when invalidating TLBs.
1451 * Another way to achieve this is to batch up page table containing pages
1452 * belonging to more than one mm_user, then rcu_sched a callback to free those
1453 * pages. Disabling interrupts will allow the fast_gup walker to both block
1454 * the rcu_sched callback, and an IPI that we broadcast for splitting THPs
1455 * (which is a relatively rare event). The code below adopts this strategy.
1457 * Before activating this code, please be aware that the following assumptions
1458 * are currently made:
1460 * *) Either HAVE_RCU_TABLE_FREE is enabled, and tlb_remove_table() is used to
1461 * free pages containing page tables or TLB flushing requires IPI broadcast.
1463 * *) ptes can be read atomically by the architecture.
1465 * *) access_ok is sufficient to validate userspace address ranges.
1467 * The last two assumptions can be relaxed by the addition of helper functions.
1469 * This code is based heavily on the PowerPC implementation by Nick Piggin.
1471 #ifdef CONFIG_HAVE_GENERIC_GUP
1475 * We assume that the PTE can be read atomically. If this is not the case for
1476 * your architecture, please provide the helper.
1478 static inline pte_t
gup_get_pte(pte_t
*ptep
)
1480 return READ_ONCE(*ptep
);
1484 static void undo_dev_pagemap(int *nr
, int nr_start
, struct page
**pages
)
1486 while ((*nr
) - nr_start
) {
1487 struct page
*page
= pages
[--(*nr
)];
1489 ClearPageReferenced(page
);
1494 #ifdef __HAVE_ARCH_PTE_SPECIAL
1495 static int gup_pte_range(pmd_t pmd
, unsigned long addr
, unsigned long end
,
1496 int write
, struct page
**pages
, int *nr
)
1498 struct dev_pagemap
*pgmap
= NULL
;
1499 int nr_start
= *nr
, ret
= 0;
1502 ptem
= ptep
= pte_offset_map(&pmd
, addr
);
1504 pte_t pte
= gup_get_pte(ptep
);
1505 struct page
*head
, *page
;
1508 * Similar to the PMD case below, NUMA hinting must take slow
1509 * path using the pte_protnone check.
1511 if (pte_protnone(pte
))
1514 if (!pte_access_permitted(pte
, write
))
1517 if (pte_devmap(pte
)) {
1518 pgmap
= get_dev_pagemap(pte_pfn(pte
), pgmap
);
1519 if (unlikely(!pgmap
)) {
1520 undo_dev_pagemap(nr
, nr_start
, pages
);
1523 } else if (pte_special(pte
))
1526 VM_BUG_ON(!pfn_valid(pte_pfn(pte
)));
1527 page
= pte_page(pte
);
1528 head
= compound_head(page
);
1530 if (!page_cache_get_speculative(head
))
1533 if (unlikely(pte_val(pte
) != pte_val(*ptep
))) {
1538 VM_BUG_ON_PAGE(compound_head(page
) != head
, page
);
1540 put_dev_pagemap(pgmap
);
1541 SetPageReferenced(page
);
1545 } while (ptep
++, addr
+= PAGE_SIZE
, addr
!= end
);
1556 * If we can't determine whether or not a pte is special, then fail immediately
1557 * for ptes. Note, we can still pin HugeTLB and THP as these are guaranteed not
1560 * For a futex to be placed on a THP tail page, get_futex_key requires a
1561 * __get_user_pages_fast implementation that can pin pages. Thus it's still
1562 * useful to have gup_huge_pmd even if we can't operate on ptes.
1564 static int gup_pte_range(pmd_t pmd
, unsigned long addr
, unsigned long end
,
1565 int write
, struct page
**pages
, int *nr
)
1569 #endif /* __HAVE_ARCH_PTE_SPECIAL */
1571 #if defined(__HAVE_ARCH_PTE_DEVMAP) && defined(CONFIG_TRANSPARENT_HUGEPAGE)
1572 static int __gup_device_huge(unsigned long pfn
, unsigned long addr
,
1573 unsigned long end
, struct page
**pages
, int *nr
)
1576 struct dev_pagemap
*pgmap
= NULL
;
1579 struct page
*page
= pfn_to_page(pfn
);
1581 pgmap
= get_dev_pagemap(pfn
, pgmap
);
1582 if (unlikely(!pgmap
)) {
1583 undo_dev_pagemap(nr
, nr_start
, pages
);
1586 SetPageReferenced(page
);
1589 put_dev_pagemap(pgmap
);
1592 } while (addr
+= PAGE_SIZE
, addr
!= end
);
1596 static int __gup_device_huge_pmd(pmd_t orig
, pmd_t
*pmdp
, unsigned long addr
,
1597 unsigned long end
, struct page
**pages
, int *nr
)
1599 unsigned long fault_pfn
;
1602 fault_pfn
= pmd_pfn(orig
) + ((addr
& ~PMD_MASK
) >> PAGE_SHIFT
);
1603 if (!__gup_device_huge(fault_pfn
, addr
, end
, pages
, nr
))
1606 if (unlikely(pmd_val(orig
) != pmd_val(*pmdp
))) {
1607 undo_dev_pagemap(nr
, nr_start
, pages
);
1613 static int __gup_device_huge_pud(pud_t orig
, pud_t
*pudp
, unsigned long addr
,
1614 unsigned long end
, struct page
**pages
, int *nr
)
1616 unsigned long fault_pfn
;
1619 fault_pfn
= pud_pfn(orig
) + ((addr
& ~PUD_MASK
) >> PAGE_SHIFT
);
1620 if (!__gup_device_huge(fault_pfn
, addr
, end
, pages
, nr
))
1623 if (unlikely(pud_val(orig
) != pud_val(*pudp
))) {
1624 undo_dev_pagemap(nr
, nr_start
, pages
);
1630 static int __gup_device_huge_pmd(pmd_t orig
, pmd_t
*pmdp
, unsigned long addr
,
1631 unsigned long end
, struct page
**pages
, int *nr
)
1637 static int __gup_device_huge_pud(pud_t pud
, pud_t
*pudp
, unsigned long addr
,
1638 unsigned long end
, struct page
**pages
, int *nr
)
1645 static int gup_huge_pmd(pmd_t orig
, pmd_t
*pmdp
, unsigned long addr
,
1646 unsigned long end
, int write
, struct page
**pages
, int *nr
)
1648 struct page
*head
, *page
;
1651 if (!pmd_access_permitted(orig
, write
))
1654 if (pmd_devmap(orig
))
1655 return __gup_device_huge_pmd(orig
, pmdp
, addr
, end
, pages
, nr
);
1658 page
= pmd_page(orig
) + ((addr
& ~PMD_MASK
) >> PAGE_SHIFT
);
1664 } while (addr
+= PAGE_SIZE
, addr
!= end
);
1666 head
= compound_head(pmd_page(orig
));
1667 if (!page_cache_add_speculative(head
, refs
)) {
1672 if (unlikely(pmd_val(orig
) != pmd_val(*pmdp
))) {
1679 SetPageReferenced(head
);
1683 static int gup_huge_pud(pud_t orig
, pud_t
*pudp
, unsigned long addr
,
1684 unsigned long end
, int write
, struct page
**pages
, int *nr
)
1686 struct page
*head
, *page
;
1689 if (!pud_access_permitted(orig
, write
))
1692 if (pud_devmap(orig
))
1693 return __gup_device_huge_pud(orig
, pudp
, addr
, end
, pages
, nr
);
1696 page
= pud_page(orig
) + ((addr
& ~PUD_MASK
) >> PAGE_SHIFT
);
1702 } while (addr
+= PAGE_SIZE
, addr
!= end
);
1704 head
= compound_head(pud_page(orig
));
1705 if (!page_cache_add_speculative(head
, refs
)) {
1710 if (unlikely(pud_val(orig
) != pud_val(*pudp
))) {
1717 SetPageReferenced(head
);
1721 static int gup_huge_pgd(pgd_t orig
, pgd_t
*pgdp
, unsigned long addr
,
1722 unsigned long end
, int write
,
1723 struct page
**pages
, int *nr
)
1726 struct page
*head
, *page
;
1728 if (!pgd_access_permitted(orig
, write
))
1731 BUILD_BUG_ON(pgd_devmap(orig
));
1733 page
= pgd_page(orig
) + ((addr
& ~PGDIR_MASK
) >> PAGE_SHIFT
);
1739 } while (addr
+= PAGE_SIZE
, addr
!= end
);
1741 head
= compound_head(pgd_page(orig
));
1742 if (!page_cache_add_speculative(head
, refs
)) {
1747 if (unlikely(pgd_val(orig
) != pgd_val(*pgdp
))) {
1754 SetPageReferenced(head
);
1758 static int gup_pmd_range(pud_t pud
, unsigned long addr
, unsigned long end
,
1759 int write
, struct page
**pages
, int *nr
)
1764 pmdp
= pmd_offset(&pud
, addr
);
1766 pmd_t pmd
= READ_ONCE(*pmdp
);
1768 next
= pmd_addr_end(addr
, end
);
1769 if (!pmd_present(pmd
))
1772 if (unlikely(pmd_trans_huge(pmd
) || pmd_huge(pmd
) ||
1775 * NUMA hinting faults need to be handled in the GUP
1776 * slowpath for accounting purposes and so that they
1777 * can be serialised against THP migration.
1779 if (pmd_protnone(pmd
))
1782 if (!gup_huge_pmd(pmd
, pmdp
, addr
, next
, write
,
1786 } else if (unlikely(is_hugepd(__hugepd(pmd_val(pmd
))))) {
1788 * architecture have different format for hugetlbfs
1789 * pmd format and THP pmd format
1791 if (!gup_huge_pd(__hugepd(pmd_val(pmd
)), addr
,
1792 PMD_SHIFT
, next
, write
, pages
, nr
))
1794 } else if (!gup_pte_range(pmd
, addr
, next
, write
, pages
, nr
))
1796 } while (pmdp
++, addr
= next
, addr
!= end
);
1801 static int gup_pud_range(p4d_t p4d
, unsigned long addr
, unsigned long end
,
1802 int write
, struct page
**pages
, int *nr
)
1807 pudp
= pud_offset(&p4d
, addr
);
1809 pud_t pud
= READ_ONCE(*pudp
);
1811 next
= pud_addr_end(addr
, end
);
1814 if (unlikely(pud_huge(pud
))) {
1815 if (!gup_huge_pud(pud
, pudp
, addr
, next
, write
,
1818 } else if (unlikely(is_hugepd(__hugepd(pud_val(pud
))))) {
1819 if (!gup_huge_pd(__hugepd(pud_val(pud
)), addr
,
1820 PUD_SHIFT
, next
, write
, pages
, nr
))
1822 } else if (!gup_pmd_range(pud
, addr
, next
, write
, pages
, nr
))
1824 } while (pudp
++, addr
= next
, addr
!= end
);
1829 static int gup_p4d_range(pgd_t pgd
, unsigned long addr
, unsigned long end
,
1830 int write
, struct page
**pages
, int *nr
)
1835 p4dp
= p4d_offset(&pgd
, addr
);
1837 p4d_t p4d
= READ_ONCE(*p4dp
);
1839 next
= p4d_addr_end(addr
, end
);
1842 BUILD_BUG_ON(p4d_huge(p4d
));
1843 if (unlikely(is_hugepd(__hugepd(p4d_val(p4d
))))) {
1844 if (!gup_huge_pd(__hugepd(p4d_val(p4d
)), addr
,
1845 P4D_SHIFT
, next
, write
, pages
, nr
))
1847 } else if (!gup_pud_range(p4d
, addr
, next
, write
, pages
, nr
))
1849 } while (p4dp
++, addr
= next
, addr
!= end
);
1854 static void gup_pgd_range(unsigned long addr
, unsigned long end
,
1855 int write
, struct page
**pages
, int *nr
)
1860 pgdp
= pgd_offset(current
->mm
, addr
);
1862 pgd_t pgd
= READ_ONCE(*pgdp
);
1864 next
= pgd_addr_end(addr
, end
);
1867 if (unlikely(pgd_huge(pgd
))) {
1868 if (!gup_huge_pgd(pgd
, pgdp
, addr
, next
, write
,
1871 } else if (unlikely(is_hugepd(__hugepd(pgd_val(pgd
))))) {
1872 if (!gup_huge_pd(__hugepd(pgd_val(pgd
)), addr
,
1873 PGDIR_SHIFT
, next
, write
, pages
, nr
))
1875 } else if (!gup_p4d_range(pgd
, addr
, next
, write
, pages
, nr
))
1877 } while (pgdp
++, addr
= next
, addr
!= end
);
1880 #ifndef gup_fast_permitted
1882 * Check if it's allowed to use __get_user_pages_fast() for the range, or
1883 * we need to fall back to the slow version:
1885 bool gup_fast_permitted(unsigned long start
, int nr_pages
, int write
)
1887 unsigned long len
, end
;
1889 len
= (unsigned long) nr_pages
<< PAGE_SHIFT
;
1891 return end
>= start
;
1896 * Like get_user_pages_fast() except it's IRQ-safe in that it won't fall back to
1897 * the regular GUP. It will only return non-negative values.
1899 int __get_user_pages_fast(unsigned long start
, int nr_pages
, int write
,
1900 struct page
**pages
)
1902 unsigned long addr
, len
, end
;
1903 unsigned long flags
;
1908 len
= (unsigned long) nr_pages
<< PAGE_SHIFT
;
1911 if (unlikely(!access_ok(write
? VERIFY_WRITE
: VERIFY_READ
,
1912 (void __user
*)start
, len
)))
1916 * Disable interrupts. We use the nested form as we can already have
1917 * interrupts disabled by get_futex_key.
1919 * With interrupts disabled, we block page table pages from being
1920 * freed from under us. See mmu_gather_tlb in asm-generic/tlb.h
1923 * We do not adopt an rcu_read_lock(.) here as we also want to
1924 * block IPIs that come from THPs splitting.
1927 if (gup_fast_permitted(start
, nr_pages
, write
)) {
1928 local_irq_save(flags
);
1929 gup_pgd_range(addr
, end
, write
, pages
, &nr
);
1930 local_irq_restore(flags
);
1937 * get_user_pages_fast() - pin user pages in memory
1938 * @start: starting user address
1939 * @nr_pages: number of pages from start to pin
1940 * @write: whether pages will be written to
1941 * @pages: array that receives pointers to the pages pinned.
1942 * Should be at least nr_pages long.
1944 * Attempt to pin user pages in memory without taking mm->mmap_sem.
1945 * If not successful, it will fall back to taking the lock and
1946 * calling get_user_pages().
1948 * Returns number of pages pinned. This may be fewer than the number
1949 * requested. If nr_pages is 0 or negative, returns 0. If no pages
1950 * were pinned, returns -errno.
1952 int get_user_pages_fast(unsigned long start
, int nr_pages
, int write
,
1953 struct page
**pages
)
1955 unsigned long addr
, len
, end
;
1956 int nr
= 0, ret
= 0;
1960 len
= (unsigned long) nr_pages
<< PAGE_SHIFT
;
1966 if (unlikely(!access_ok(write
? VERIFY_WRITE
: VERIFY_READ
,
1967 (void __user
*)start
, len
)))
1970 if (gup_fast_permitted(start
, nr_pages
, write
)) {
1971 local_irq_disable();
1972 gup_pgd_range(addr
, end
, write
, pages
, &nr
);
1977 if (nr
< nr_pages
) {
1978 /* Try to get the remaining pages with get_user_pages */
1979 start
+= nr
<< PAGE_SHIFT
;
1982 ret
= get_user_pages_unlocked(start
, nr_pages
- nr
, pages
,
1983 write
? FOLL_WRITE
: 0);
1985 /* Have to be a bit careful with return values */
1997 #endif /* CONFIG_HAVE_GENERIC_GUP */