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>
16 #include <linux/migrate.h>
17 #include <linux/mm_inline.h>
18 #include <linux/sched/mm.h>
20 #include <asm/mmu_context.h>
21 #include <asm/pgtable.h>
22 #include <asm/tlbflush.h>
26 struct follow_page_context
{
27 struct dev_pagemap
*pgmap
;
28 unsigned int page_mask
;
31 static struct page
*no_page_table(struct vm_area_struct
*vma
,
35 * When core dumping an enormous anonymous area that nobody
36 * has touched so far, we don't want to allocate unnecessary pages or
37 * page tables. Return error instead of NULL to skip handle_mm_fault,
38 * then get_dump_page() will return NULL to leave a hole in the dump.
39 * But we can only make this optimization where a hole would surely
40 * be zero-filled if handle_mm_fault() actually did handle it.
42 if ((flags
& FOLL_DUMP
) && (!vma
->vm_ops
|| !vma
->vm_ops
->fault
))
43 return ERR_PTR(-EFAULT
);
47 static int follow_pfn_pte(struct vm_area_struct
*vma
, unsigned long address
,
48 pte_t
*pte
, unsigned int flags
)
50 /* No page to get reference */
54 if (flags
& FOLL_TOUCH
) {
57 if (flags
& FOLL_WRITE
)
58 entry
= pte_mkdirty(entry
);
59 entry
= pte_mkyoung(entry
);
61 if (!pte_same(*pte
, entry
)) {
62 set_pte_at(vma
->vm_mm
, address
, pte
, entry
);
63 update_mmu_cache(vma
, address
, pte
);
67 /* Proper page table entry exists, but no corresponding struct page */
72 * FOLL_FORCE can write to even unwritable pte's, but only
73 * after we've gone through a COW cycle and they are dirty.
75 static inline bool can_follow_write_pte(pte_t pte
, unsigned int flags
)
77 return pte_write(pte
) ||
78 ((flags
& FOLL_FORCE
) && (flags
& FOLL_COW
) && pte_dirty(pte
));
81 static struct page
*follow_page_pte(struct vm_area_struct
*vma
,
82 unsigned long address
, pmd_t
*pmd
, unsigned int flags
,
83 struct dev_pagemap
**pgmap
)
85 struct mm_struct
*mm
= vma
->vm_mm
;
91 if (unlikely(pmd_bad(*pmd
)))
92 return no_page_table(vma
, flags
);
94 ptep
= pte_offset_map_lock(mm
, pmd
, address
, &ptl
);
96 if (!pte_present(pte
)) {
99 * KSM's break_ksm() relies upon recognizing a ksm page
100 * even while it is being migrated, so for that case we
101 * need migration_entry_wait().
103 if (likely(!(flags
& FOLL_MIGRATION
)))
107 entry
= pte_to_swp_entry(pte
);
108 if (!is_migration_entry(entry
))
110 pte_unmap_unlock(ptep
, ptl
);
111 migration_entry_wait(mm
, pmd
, address
);
114 if ((flags
& FOLL_NUMA
) && pte_protnone(pte
))
116 if ((flags
& FOLL_WRITE
) && !can_follow_write_pte(pte
, flags
)) {
117 pte_unmap_unlock(ptep
, ptl
);
121 page
= vm_normal_page(vma
, address
, pte
);
122 if (!page
&& pte_devmap(pte
) && (flags
& FOLL_GET
)) {
124 * Only return device mapping pages in the FOLL_GET case since
125 * they are only valid while holding the pgmap reference.
127 *pgmap
= get_dev_pagemap(pte_pfn(pte
), *pgmap
);
129 page
= pte_page(pte
);
132 } else if (unlikely(!page
)) {
133 if (flags
& FOLL_DUMP
) {
134 /* Avoid special (like zero) pages in core dumps */
135 page
= ERR_PTR(-EFAULT
);
139 if (is_zero_pfn(pte_pfn(pte
))) {
140 page
= pte_page(pte
);
144 ret
= follow_pfn_pte(vma
, address
, ptep
, flags
);
150 if (flags
& FOLL_SPLIT
&& PageTransCompound(page
)) {
153 pte_unmap_unlock(ptep
, ptl
);
155 ret
= split_huge_page(page
);
163 if (flags
& FOLL_GET
)
165 if (flags
& FOLL_TOUCH
) {
166 if ((flags
& FOLL_WRITE
) &&
167 !pte_dirty(pte
) && !PageDirty(page
))
168 set_page_dirty(page
);
170 * pte_mkyoung() would be more correct here, but atomic care
171 * is needed to avoid losing the dirty bit: it is easier to use
172 * mark_page_accessed().
174 mark_page_accessed(page
);
176 if ((flags
& FOLL_MLOCK
) && (vma
->vm_flags
& VM_LOCKED
)) {
177 /* Do not mlock pte-mapped THP */
178 if (PageTransCompound(page
))
182 * The preliminary mapping check is mainly to avoid the
183 * pointless overhead of lock_page on the ZERO_PAGE
184 * which might bounce very badly if there is contention.
186 * If the page is already locked, we don't need to
187 * handle it now - vmscan will handle it later if and
188 * when it attempts to reclaim the page.
190 if (page
->mapping
&& trylock_page(page
)) {
191 lru_add_drain(); /* push cached pages to LRU */
193 * Because we lock page here, and migration is
194 * blocked by the pte's page reference, and we
195 * know the page is still mapped, we don't even
196 * need to check for file-cache page truncation.
198 mlock_vma_page(page
);
203 pte_unmap_unlock(ptep
, ptl
);
206 pte_unmap_unlock(ptep
, ptl
);
209 return no_page_table(vma
, flags
);
212 static struct page
*follow_pmd_mask(struct vm_area_struct
*vma
,
213 unsigned long address
, pud_t
*pudp
,
215 struct follow_page_context
*ctx
)
220 struct mm_struct
*mm
= vma
->vm_mm
;
222 pmd
= pmd_offset(pudp
, address
);
224 * The READ_ONCE() will stabilize the pmdval in a register or
225 * on the stack so that it will stop changing under the code.
227 pmdval
= READ_ONCE(*pmd
);
228 if (pmd_none(pmdval
))
229 return no_page_table(vma
, flags
);
230 if (pmd_huge(pmdval
) && vma
->vm_flags
& VM_HUGETLB
) {
231 page
= follow_huge_pmd(mm
, address
, pmd
, flags
);
234 return no_page_table(vma
, flags
);
236 if (is_hugepd(__hugepd(pmd_val(pmdval
)))) {
237 page
= follow_huge_pd(vma
, address
,
238 __hugepd(pmd_val(pmdval
)), flags
,
242 return no_page_table(vma
, flags
);
245 if (!pmd_present(pmdval
)) {
246 if (likely(!(flags
& FOLL_MIGRATION
)))
247 return no_page_table(vma
, flags
);
248 VM_BUG_ON(thp_migration_supported() &&
249 !is_pmd_migration_entry(pmdval
));
250 if (is_pmd_migration_entry(pmdval
))
251 pmd_migration_entry_wait(mm
, pmd
);
252 pmdval
= READ_ONCE(*pmd
);
254 * MADV_DONTNEED may convert the pmd to null because
255 * mmap_sem is held in read mode
257 if (pmd_none(pmdval
))
258 return no_page_table(vma
, flags
);
261 if (pmd_devmap(pmdval
)) {
262 ptl
= pmd_lock(mm
, pmd
);
263 page
= follow_devmap_pmd(vma
, address
, pmd
, flags
, &ctx
->pgmap
);
268 if (likely(!pmd_trans_huge(pmdval
)))
269 return follow_page_pte(vma
, address
, pmd
, flags
, &ctx
->pgmap
);
271 if ((flags
& FOLL_NUMA
) && pmd_protnone(pmdval
))
272 return no_page_table(vma
, flags
);
275 ptl
= pmd_lock(mm
, pmd
);
276 if (unlikely(pmd_none(*pmd
))) {
278 return no_page_table(vma
, flags
);
280 if (unlikely(!pmd_present(*pmd
))) {
282 if (likely(!(flags
& FOLL_MIGRATION
)))
283 return no_page_table(vma
, flags
);
284 pmd_migration_entry_wait(mm
, pmd
);
287 if (unlikely(!pmd_trans_huge(*pmd
))) {
289 return follow_page_pte(vma
, address
, pmd
, flags
, &ctx
->pgmap
);
291 if (flags
& FOLL_SPLIT
) {
293 page
= pmd_page(*pmd
);
294 if (is_huge_zero_page(page
)) {
297 split_huge_pmd(vma
, pmd
, address
);
298 if (pmd_trans_unstable(pmd
))
304 ret
= split_huge_page(page
);
308 return no_page_table(vma
, flags
);
311 return ret
? ERR_PTR(ret
) :
312 follow_page_pte(vma
, address
, pmd
, flags
, &ctx
->pgmap
);
314 page
= follow_trans_huge_pmd(vma
, address
, pmd
, flags
);
316 ctx
->page_mask
= HPAGE_PMD_NR
- 1;
320 static struct page
*follow_pud_mask(struct vm_area_struct
*vma
,
321 unsigned long address
, p4d_t
*p4dp
,
323 struct follow_page_context
*ctx
)
328 struct mm_struct
*mm
= vma
->vm_mm
;
330 pud
= pud_offset(p4dp
, address
);
332 return no_page_table(vma
, flags
);
333 if (pud_huge(*pud
) && vma
->vm_flags
& VM_HUGETLB
) {
334 page
= follow_huge_pud(mm
, address
, pud
, flags
);
337 return no_page_table(vma
, flags
);
339 if (is_hugepd(__hugepd(pud_val(*pud
)))) {
340 page
= follow_huge_pd(vma
, address
,
341 __hugepd(pud_val(*pud
)), flags
,
345 return no_page_table(vma
, flags
);
347 if (pud_devmap(*pud
)) {
348 ptl
= pud_lock(mm
, pud
);
349 page
= follow_devmap_pud(vma
, address
, pud
, flags
, &ctx
->pgmap
);
354 if (unlikely(pud_bad(*pud
)))
355 return no_page_table(vma
, flags
);
357 return follow_pmd_mask(vma
, address
, pud
, flags
, ctx
);
360 static struct page
*follow_p4d_mask(struct vm_area_struct
*vma
,
361 unsigned long address
, pgd_t
*pgdp
,
363 struct follow_page_context
*ctx
)
368 p4d
= p4d_offset(pgdp
, address
);
370 return no_page_table(vma
, flags
);
371 BUILD_BUG_ON(p4d_huge(*p4d
));
372 if (unlikely(p4d_bad(*p4d
)))
373 return no_page_table(vma
, flags
);
375 if (is_hugepd(__hugepd(p4d_val(*p4d
)))) {
376 page
= follow_huge_pd(vma
, address
,
377 __hugepd(p4d_val(*p4d
)), flags
,
381 return no_page_table(vma
, flags
);
383 return follow_pud_mask(vma
, address
, p4d
, flags
, ctx
);
387 * follow_page_mask - look up a page descriptor from a user-virtual address
388 * @vma: vm_area_struct mapping @address
389 * @address: virtual address to look up
390 * @flags: flags modifying lookup behaviour
391 * @ctx: contains dev_pagemap for %ZONE_DEVICE memory pinning and a
392 * pointer to output page_mask
394 * @flags can have FOLL_ flags set, defined in <linux/mm.h>
396 * When getting pages from ZONE_DEVICE memory, the @ctx->pgmap caches
397 * the device's dev_pagemap metadata to avoid repeating expensive lookups.
399 * On output, the @ctx->page_mask is set according to the size of the page.
401 * Return: the mapped (struct page *), %NULL if no mapping exists, or
402 * an error pointer if there is a mapping to something not represented
403 * by a page descriptor (see also vm_normal_page()).
405 struct page
*follow_page_mask(struct vm_area_struct
*vma
,
406 unsigned long address
, unsigned int flags
,
407 struct follow_page_context
*ctx
)
411 struct mm_struct
*mm
= vma
->vm_mm
;
415 /* make this handle hugepd */
416 page
= follow_huge_addr(mm
, address
, flags
& FOLL_WRITE
);
418 BUG_ON(flags
& FOLL_GET
);
422 pgd
= pgd_offset(mm
, address
);
424 if (pgd_none(*pgd
) || unlikely(pgd_bad(*pgd
)))
425 return no_page_table(vma
, flags
);
427 if (pgd_huge(*pgd
)) {
428 page
= follow_huge_pgd(mm
, address
, pgd
, flags
);
431 return no_page_table(vma
, flags
);
433 if (is_hugepd(__hugepd(pgd_val(*pgd
)))) {
434 page
= follow_huge_pd(vma
, address
,
435 __hugepd(pgd_val(*pgd
)), flags
,
439 return no_page_table(vma
, flags
);
442 return follow_p4d_mask(vma
, address
, pgd
, flags
, ctx
);
445 struct page
*follow_page(struct vm_area_struct
*vma
, unsigned long address
,
446 unsigned int foll_flags
)
448 struct follow_page_context ctx
= { NULL
};
451 page
= follow_page_mask(vma
, address
, foll_flags
, &ctx
);
453 put_dev_pagemap(ctx
.pgmap
);
457 static int get_gate_page(struct mm_struct
*mm
, unsigned long address
,
458 unsigned int gup_flags
, struct vm_area_struct
**vma
,
468 /* user gate pages are read-only */
469 if (gup_flags
& FOLL_WRITE
)
471 if (address
> TASK_SIZE
)
472 pgd
= pgd_offset_k(address
);
474 pgd
= pgd_offset_gate(mm
, address
);
475 BUG_ON(pgd_none(*pgd
));
476 p4d
= p4d_offset(pgd
, address
);
477 BUG_ON(p4d_none(*p4d
));
478 pud
= pud_offset(p4d
, address
);
479 BUG_ON(pud_none(*pud
));
480 pmd
= pmd_offset(pud
, address
);
481 if (!pmd_present(*pmd
))
483 VM_BUG_ON(pmd_trans_huge(*pmd
));
484 pte
= pte_offset_map(pmd
, address
);
487 *vma
= get_gate_vma(mm
);
490 *page
= vm_normal_page(*vma
, address
, *pte
);
492 if ((gup_flags
& FOLL_DUMP
) || !is_zero_pfn(pte_pfn(*pte
)))
494 *page
= pte_page(*pte
);
497 * This should never happen (a device public page in the gate
500 if (is_device_public_page(*page
))
512 * mmap_sem must be held on entry. If @nonblocking != NULL and
513 * *@flags does not include FOLL_NOWAIT, the mmap_sem may be released.
514 * If it is, *@nonblocking will be set to 0 and -EBUSY returned.
516 static int faultin_page(struct task_struct
*tsk
, struct vm_area_struct
*vma
,
517 unsigned long address
, unsigned int *flags
, int *nonblocking
)
519 unsigned int fault_flags
= 0;
522 /* mlock all present pages, but do not fault in new pages */
523 if ((*flags
& (FOLL_POPULATE
| FOLL_MLOCK
)) == FOLL_MLOCK
)
525 if (*flags
& FOLL_WRITE
)
526 fault_flags
|= FAULT_FLAG_WRITE
;
527 if (*flags
& FOLL_REMOTE
)
528 fault_flags
|= FAULT_FLAG_REMOTE
;
530 fault_flags
|= FAULT_FLAG_ALLOW_RETRY
;
531 if (*flags
& FOLL_NOWAIT
)
532 fault_flags
|= FAULT_FLAG_ALLOW_RETRY
| FAULT_FLAG_RETRY_NOWAIT
;
533 if (*flags
& FOLL_TRIED
) {
534 VM_WARN_ON_ONCE(fault_flags
& FAULT_FLAG_ALLOW_RETRY
);
535 fault_flags
|= FAULT_FLAG_TRIED
;
538 ret
= handle_mm_fault(vma
, address
, fault_flags
);
539 if (ret
& VM_FAULT_ERROR
) {
540 int err
= vm_fault_to_errno(ret
, *flags
);
548 if (ret
& VM_FAULT_MAJOR
)
554 if (ret
& VM_FAULT_RETRY
) {
555 if (nonblocking
&& !(fault_flags
& FAULT_FLAG_RETRY_NOWAIT
))
561 * The VM_FAULT_WRITE bit tells us that do_wp_page has broken COW when
562 * necessary, even if maybe_mkwrite decided not to set pte_write. We
563 * can thus safely do subsequent page lookups as if they were reads.
564 * But only do so when looping for pte_write is futile: in some cases
565 * userspace may also be wanting to write to the gotten user page,
566 * which a read fault here might prevent (a readonly page might get
567 * reCOWed by userspace write).
569 if ((ret
& VM_FAULT_WRITE
) && !(vma
->vm_flags
& VM_WRITE
))
574 static int check_vma_flags(struct vm_area_struct
*vma
, unsigned long gup_flags
)
576 vm_flags_t vm_flags
= vma
->vm_flags
;
577 int write
= (gup_flags
& FOLL_WRITE
);
578 int foreign
= (gup_flags
& FOLL_REMOTE
);
580 if (vm_flags
& (VM_IO
| VM_PFNMAP
))
583 if (gup_flags
& FOLL_ANON
&& !vma_is_anonymous(vma
))
587 if (!(vm_flags
& VM_WRITE
)) {
588 if (!(gup_flags
& FOLL_FORCE
))
591 * We used to let the write,force case do COW in a
592 * VM_MAYWRITE VM_SHARED !VM_WRITE vma, so ptrace could
593 * set a breakpoint in a read-only mapping of an
594 * executable, without corrupting the file (yet only
595 * when that file had been opened for writing!).
596 * Anon pages in shared mappings are surprising: now
599 if (!is_cow_mapping(vm_flags
))
602 } else if (!(vm_flags
& VM_READ
)) {
603 if (!(gup_flags
& FOLL_FORCE
))
606 * Is there actually any vma we can reach here which does not
607 * have VM_MAYREAD set?
609 if (!(vm_flags
& VM_MAYREAD
))
613 * gups are always data accesses, not instruction
614 * fetches, so execute=false here
616 if (!arch_vma_access_permitted(vma
, write
, false, foreign
))
622 * __get_user_pages() - pin user pages in memory
623 * @tsk: task_struct of target task
624 * @mm: mm_struct of target mm
625 * @start: starting user address
626 * @nr_pages: number of pages from start to pin
627 * @gup_flags: flags modifying pin behaviour
628 * @pages: array that receives pointers to the pages pinned.
629 * Should be at least nr_pages long. Or NULL, if caller
630 * only intends to ensure the pages are faulted in.
631 * @vmas: array of pointers to vmas corresponding to each page.
632 * Or NULL if the caller does not require them.
633 * @nonblocking: whether waiting for disk IO or mmap_sem contention
635 * Returns number of pages pinned. This may be fewer than the number
636 * requested. If nr_pages is 0 or negative, returns 0. If no pages
637 * were pinned, returns -errno. Each page returned must be released
638 * with a put_page() call when it is finished with. vmas will only
639 * remain valid while mmap_sem is held.
641 * Must be called with mmap_sem held. It may be released. See below.
643 * __get_user_pages walks a process's page tables and takes a reference to
644 * each struct page that each user address corresponds to at a given
645 * instant. That is, it takes the page that would be accessed if a user
646 * thread accesses the given user virtual address at that instant.
648 * This does not guarantee that the page exists in the user mappings when
649 * __get_user_pages returns, and there may even be a completely different
650 * page there in some cases (eg. if mmapped pagecache has been invalidated
651 * and subsequently re faulted). However it does guarantee that the page
652 * won't be freed completely. And mostly callers simply care that the page
653 * contains data that was valid *at some point in time*. Typically, an IO
654 * or similar operation cannot guarantee anything stronger anyway because
655 * locks can't be held over the syscall boundary.
657 * If @gup_flags & FOLL_WRITE == 0, the page must not be written to. If
658 * the page is written to, set_page_dirty (or set_page_dirty_lock, as
659 * appropriate) must be called after the page is finished with, and
660 * before put_page is called.
662 * If @nonblocking != NULL, __get_user_pages will not wait for disk IO
663 * or mmap_sem contention, and if waiting is needed to pin all pages,
664 * *@nonblocking will be set to 0. Further, if @gup_flags does not
665 * include FOLL_NOWAIT, the mmap_sem will be released via up_read() in
668 * A caller using such a combination of @nonblocking and @gup_flags
669 * must therefore hold the mmap_sem for reading only, and recognize
670 * when it's been released. Otherwise, it must be held for either
671 * reading or writing and will not be released.
673 * In most cases, get_user_pages or get_user_pages_fast should be used
674 * instead of __get_user_pages. __get_user_pages should be used only if
675 * you need some special @gup_flags.
677 static long __get_user_pages(struct task_struct
*tsk
, struct mm_struct
*mm
,
678 unsigned long start
, unsigned long nr_pages
,
679 unsigned int gup_flags
, struct page
**pages
,
680 struct vm_area_struct
**vmas
, int *nonblocking
)
683 struct vm_area_struct
*vma
= NULL
;
684 struct follow_page_context ctx
= { NULL
};
689 VM_BUG_ON(!!pages
!= !!(gup_flags
& FOLL_GET
));
692 * If FOLL_FORCE is set then do not force a full fault as the hinting
693 * fault information is unrelated to the reference behaviour of a task
694 * using the address space
696 if (!(gup_flags
& FOLL_FORCE
))
697 gup_flags
|= FOLL_NUMA
;
701 unsigned int foll_flags
= gup_flags
;
702 unsigned int page_increm
;
704 /* first iteration or cross vma bound */
705 if (!vma
|| start
>= vma
->vm_end
) {
706 vma
= find_extend_vma(mm
, start
);
707 if (!vma
&& in_gate_area(mm
, start
)) {
708 ret
= get_gate_page(mm
, start
& PAGE_MASK
,
710 pages
? &pages
[i
] : NULL
);
717 if (!vma
|| check_vma_flags(vma
, gup_flags
)) {
721 if (is_vm_hugetlb_page(vma
)) {
722 i
= follow_hugetlb_page(mm
, vma
, pages
, vmas
,
723 &start
, &nr_pages
, i
,
724 gup_flags
, nonblocking
);
730 * If we have a pending SIGKILL, don't keep faulting pages and
731 * potentially allocating memory.
733 if (fatal_signal_pending(current
)) {
739 page
= follow_page_mask(vma
, start
, foll_flags
, &ctx
);
741 ret
= faultin_page(tsk
, vma
, start
, &foll_flags
,
757 } else if (PTR_ERR(page
) == -EEXIST
) {
759 * Proper page table entry exists, but no corresponding
763 } else if (IS_ERR(page
)) {
769 flush_anon_page(vma
, page
, start
);
770 flush_dcache_page(page
);
778 page_increm
= 1 + (~(start
>> PAGE_SHIFT
) & ctx
.page_mask
);
779 if (page_increm
> nr_pages
)
780 page_increm
= nr_pages
;
782 start
+= page_increm
* PAGE_SIZE
;
783 nr_pages
-= page_increm
;
787 put_dev_pagemap(ctx
.pgmap
);
791 static bool vma_permits_fault(struct vm_area_struct
*vma
,
792 unsigned int fault_flags
)
794 bool write
= !!(fault_flags
& FAULT_FLAG_WRITE
);
795 bool foreign
= !!(fault_flags
& FAULT_FLAG_REMOTE
);
796 vm_flags_t vm_flags
= write
? VM_WRITE
: VM_READ
;
798 if (!(vm_flags
& vma
->vm_flags
))
802 * The architecture might have a hardware protection
803 * mechanism other than read/write that can deny access.
805 * gup always represents data access, not instruction
806 * fetches, so execute=false here:
808 if (!arch_vma_access_permitted(vma
, write
, false, foreign
))
815 * fixup_user_fault() - manually resolve a user page fault
816 * @tsk: the task_struct to use for page fault accounting, or
817 * NULL if faults are not to be recorded.
818 * @mm: mm_struct of target mm
819 * @address: user address
820 * @fault_flags:flags to pass down to handle_mm_fault()
821 * @unlocked: did we unlock the mmap_sem while retrying, maybe NULL if caller
822 * does not allow retry
824 * This is meant to be called in the specific scenario where for locking reasons
825 * we try to access user memory in atomic context (within a pagefault_disable()
826 * section), this returns -EFAULT, and we want to resolve the user fault before
829 * Typically this is meant to be used by the futex code.
831 * The main difference with get_user_pages() is that this function will
832 * unconditionally call handle_mm_fault() which will in turn perform all the
833 * necessary SW fixup of the dirty and young bits in the PTE, while
834 * get_user_pages() only guarantees to update these in the struct page.
836 * This is important for some architectures where those bits also gate the
837 * access permission to the page because they are maintained in software. On
838 * such architectures, gup() will not be enough to make a subsequent access
841 * This function will not return with an unlocked mmap_sem. So it has not the
842 * same semantics wrt the @mm->mmap_sem as does filemap_fault().
844 int fixup_user_fault(struct task_struct
*tsk
, struct mm_struct
*mm
,
845 unsigned long address
, unsigned int fault_flags
,
848 struct vm_area_struct
*vma
;
849 vm_fault_t ret
, major
= 0;
852 fault_flags
|= FAULT_FLAG_ALLOW_RETRY
;
855 vma
= find_extend_vma(mm
, address
);
856 if (!vma
|| address
< vma
->vm_start
)
859 if (!vma_permits_fault(vma
, fault_flags
))
862 ret
= handle_mm_fault(vma
, address
, fault_flags
);
863 major
|= ret
& VM_FAULT_MAJOR
;
864 if (ret
& VM_FAULT_ERROR
) {
865 int err
= vm_fault_to_errno(ret
, 0);
872 if (ret
& VM_FAULT_RETRY
) {
873 down_read(&mm
->mmap_sem
);
874 if (!(fault_flags
& FAULT_FLAG_TRIED
)) {
876 fault_flags
&= ~FAULT_FLAG_ALLOW_RETRY
;
877 fault_flags
|= FAULT_FLAG_TRIED
;
890 EXPORT_SYMBOL_GPL(fixup_user_fault
);
892 static __always_inline
long __get_user_pages_locked(struct task_struct
*tsk
,
893 struct mm_struct
*mm
,
895 unsigned long nr_pages
,
897 struct vm_area_struct
**vmas
,
901 long ret
, pages_done
;
905 /* if VM_FAULT_RETRY can be returned, vmas become invalid */
907 /* check caller initialized locked */
908 BUG_ON(*locked
!= 1);
915 lock_dropped
= false;
917 ret
= __get_user_pages(tsk
, mm
, start
, nr_pages
, flags
, pages
,
920 /* VM_FAULT_RETRY couldn't trigger, bypass */
923 /* VM_FAULT_RETRY cannot return errors */
926 BUG_ON(ret
>= nr_pages
);
930 /* If it's a prefault don't insist harder */
941 * VM_FAULT_RETRY didn't trigger or it was a
948 /* VM_FAULT_RETRY triggered, so seek to the faulting offset */
950 start
+= ret
<< PAGE_SHIFT
;
953 * Repeat on the address that fired VM_FAULT_RETRY
954 * without FAULT_FLAG_ALLOW_RETRY but with
959 down_read(&mm
->mmap_sem
);
960 ret
= __get_user_pages(tsk
, mm
, start
, 1, flags
| FOLL_TRIED
,
975 if (lock_dropped
&& *locked
) {
977 * We must let the caller know we temporarily dropped the lock
978 * and so the critical section protected by it was lost.
980 up_read(&mm
->mmap_sem
);
987 * We can leverage the VM_FAULT_RETRY functionality in the page fault
988 * paths better by using either get_user_pages_locked() or
989 * get_user_pages_unlocked().
991 * get_user_pages_locked() is suitable to replace the form:
993 * down_read(&mm->mmap_sem);
995 * get_user_pages(tsk, mm, ..., pages, NULL);
996 * up_read(&mm->mmap_sem);
1001 * down_read(&mm->mmap_sem);
1003 * get_user_pages_locked(tsk, mm, ..., pages, &locked);
1005 * up_read(&mm->mmap_sem);
1007 long get_user_pages_locked(unsigned long start
, unsigned long nr_pages
,
1008 unsigned int gup_flags
, struct page
**pages
,
1011 return __get_user_pages_locked(current
, current
->mm
, start
, nr_pages
,
1012 pages
, NULL
, locked
,
1013 gup_flags
| FOLL_TOUCH
);
1015 EXPORT_SYMBOL(get_user_pages_locked
);
1018 * get_user_pages_unlocked() is suitable to replace the form:
1020 * down_read(&mm->mmap_sem);
1021 * get_user_pages(tsk, mm, ..., pages, NULL);
1022 * up_read(&mm->mmap_sem);
1026 * get_user_pages_unlocked(tsk, mm, ..., pages);
1028 * It is functionally equivalent to get_user_pages_fast so
1029 * get_user_pages_fast should be used instead if specific gup_flags
1030 * (e.g. FOLL_FORCE) are not required.
1032 long get_user_pages_unlocked(unsigned long start
, unsigned long nr_pages
,
1033 struct page
**pages
, unsigned int gup_flags
)
1035 struct mm_struct
*mm
= current
->mm
;
1039 down_read(&mm
->mmap_sem
);
1040 ret
= __get_user_pages_locked(current
, mm
, start
, nr_pages
, pages
, NULL
,
1041 &locked
, gup_flags
| FOLL_TOUCH
);
1043 up_read(&mm
->mmap_sem
);
1046 EXPORT_SYMBOL(get_user_pages_unlocked
);
1049 * get_user_pages_remote() - pin user pages in memory
1050 * @tsk: the task_struct to use for page fault accounting, or
1051 * NULL if faults are not to be recorded.
1052 * @mm: mm_struct of target mm
1053 * @start: starting user address
1054 * @nr_pages: number of pages from start to pin
1055 * @gup_flags: flags modifying lookup behaviour
1056 * @pages: array that receives pointers to the pages pinned.
1057 * Should be at least nr_pages long. Or NULL, if caller
1058 * only intends to ensure the pages are faulted in.
1059 * @vmas: array of pointers to vmas corresponding to each page.
1060 * Or NULL if the caller does not require them.
1061 * @locked: pointer to lock flag indicating whether lock is held and
1062 * subsequently whether VM_FAULT_RETRY functionality can be
1063 * utilised. Lock must initially be held.
1065 * Returns number of pages pinned. This may be fewer than the number
1066 * requested. If nr_pages is 0 or negative, returns 0. If no pages
1067 * were pinned, returns -errno. Each page returned must be released
1068 * with a put_page() call when it is finished with. vmas will only
1069 * remain valid while mmap_sem is held.
1071 * Must be called with mmap_sem held for read or write.
1073 * get_user_pages walks a process's page tables and takes a reference to
1074 * each struct page that each user address corresponds to at a given
1075 * instant. That is, it takes the page that would be accessed if a user
1076 * thread accesses the given user virtual address at that instant.
1078 * This does not guarantee that the page exists in the user mappings when
1079 * get_user_pages returns, and there may even be a completely different
1080 * page there in some cases (eg. if mmapped pagecache has been invalidated
1081 * and subsequently re faulted). However it does guarantee that the page
1082 * won't be freed completely. And mostly callers simply care that the page
1083 * contains data that was valid *at some point in time*. Typically, an IO
1084 * or similar operation cannot guarantee anything stronger anyway because
1085 * locks can't be held over the syscall boundary.
1087 * If gup_flags & FOLL_WRITE == 0, the page must not be written to. If the page
1088 * is written to, set_page_dirty (or set_page_dirty_lock, as appropriate) must
1089 * be called after the page is finished with, and before put_page is called.
1091 * get_user_pages is typically used for fewer-copy IO operations, to get a
1092 * handle on the memory by some means other than accesses via the user virtual
1093 * addresses. The pages may be submitted for DMA to devices or accessed via
1094 * their kernel linear mapping (via the kmap APIs). Care should be taken to
1095 * use the correct cache flushing APIs.
1097 * See also get_user_pages_fast, for performance critical applications.
1099 * get_user_pages should be phased out in favor of
1100 * get_user_pages_locked|unlocked or get_user_pages_fast. Nothing
1101 * should use get_user_pages because it cannot pass
1102 * FAULT_FLAG_ALLOW_RETRY to handle_mm_fault.
1104 long get_user_pages_remote(struct task_struct
*tsk
, struct mm_struct
*mm
,
1105 unsigned long start
, unsigned long nr_pages
,
1106 unsigned int gup_flags
, struct page
**pages
,
1107 struct vm_area_struct
**vmas
, int *locked
)
1109 return __get_user_pages_locked(tsk
, mm
, start
, nr_pages
, pages
, vmas
,
1111 gup_flags
| FOLL_TOUCH
| FOLL_REMOTE
);
1113 EXPORT_SYMBOL(get_user_pages_remote
);
1116 * This is the same as get_user_pages_remote(), just with a
1117 * less-flexible calling convention where we assume that the task
1118 * and mm being operated on are the current task's and don't allow
1119 * passing of a locked parameter. We also obviously don't pass
1120 * FOLL_REMOTE in here.
1122 long get_user_pages(unsigned long start
, unsigned long nr_pages
,
1123 unsigned int gup_flags
, struct page
**pages
,
1124 struct vm_area_struct
**vmas
)
1126 return __get_user_pages_locked(current
, current
->mm
, start
, nr_pages
,
1128 gup_flags
| FOLL_TOUCH
);
1130 EXPORT_SYMBOL(get_user_pages
);
1132 #if defined(CONFIG_FS_DAX) || defined (CONFIG_CMA)
1134 #ifdef CONFIG_FS_DAX
1135 static bool check_dax_vmas(struct vm_area_struct
**vmas
, long nr_pages
)
1138 struct vm_area_struct
*vma_prev
= NULL
;
1140 for (i
= 0; i
< nr_pages
; i
++) {
1141 struct vm_area_struct
*vma
= vmas
[i
];
1143 if (vma
== vma_prev
)
1148 if (vma_is_fsdax(vma
))
1154 static inline bool check_dax_vmas(struct vm_area_struct
**vmas
, long nr_pages
)
1161 static struct page
*new_non_cma_page(struct page
*page
, unsigned long private)
1164 * We want to make sure we allocate the new page from the same node
1165 * as the source page.
1167 int nid
= page_to_nid(page
);
1169 * Trying to allocate a page for migration. Ignore allocation
1170 * failure warnings. We don't force __GFP_THISNODE here because
1171 * this node here is the node where we have CMA reservation and
1172 * in some case these nodes will have really less non movable
1173 * allocation memory.
1175 gfp_t gfp_mask
= GFP_USER
| __GFP_NOWARN
;
1177 if (PageHighMem(page
))
1178 gfp_mask
|= __GFP_HIGHMEM
;
1180 #ifdef CONFIG_HUGETLB_PAGE
1181 if (PageHuge(page
)) {
1182 struct hstate
*h
= page_hstate(page
);
1184 * We don't want to dequeue from the pool because pool pages will
1185 * mostly be from the CMA region.
1187 return alloc_migrate_huge_page(h
, gfp_mask
, nid
, NULL
);
1190 if (PageTransHuge(page
)) {
1193 * ignore allocation failure warnings
1195 gfp_t thp_gfpmask
= GFP_TRANSHUGE
| __GFP_NOWARN
;
1198 * Remove the movable mask so that we don't allocate from
1201 thp_gfpmask
&= ~__GFP_MOVABLE
;
1202 thp
= __alloc_pages_node(nid
, thp_gfpmask
, HPAGE_PMD_ORDER
);
1205 prep_transhuge_page(thp
);
1209 return __alloc_pages_node(nid
, gfp_mask
, 0);
1212 static long check_and_migrate_cma_pages(unsigned long start
, long nr_pages
,
1213 unsigned int gup_flags
,
1214 struct page
**pages
,
1215 struct vm_area_struct
**vmas
)
1218 bool drain_allow
= true;
1219 bool migrate_allow
= true;
1220 LIST_HEAD(cma_page_list
);
1223 for (i
= 0; i
< nr_pages
; i
++) {
1225 * If we get a page from the CMA zone, since we are going to
1226 * be pinning these entries, we might as well move them out
1227 * of the CMA zone if possible.
1229 if (is_migrate_cma_page(pages
[i
])) {
1231 struct page
*head
= compound_head(pages
[i
]);
1233 if (PageHuge(head
)) {
1234 isolate_huge_page(head
, &cma_page_list
);
1236 if (!PageLRU(head
) && drain_allow
) {
1237 lru_add_drain_all();
1238 drain_allow
= false;
1241 if (!isolate_lru_page(head
)) {
1242 list_add_tail(&head
->lru
, &cma_page_list
);
1243 mod_node_page_state(page_pgdat(head
),
1245 page_is_file_cache(head
),
1246 hpage_nr_pages(head
));
1252 if (!list_empty(&cma_page_list
)) {
1254 * drop the above get_user_pages reference.
1256 for (i
= 0; i
< nr_pages
; i
++)
1259 if (migrate_pages(&cma_page_list
, new_non_cma_page
,
1260 NULL
, 0, MIGRATE_SYNC
, MR_CONTIG_RANGE
)) {
1262 * some of the pages failed migration. Do get_user_pages
1263 * without migration.
1265 migrate_allow
= false;
1267 if (!list_empty(&cma_page_list
))
1268 putback_movable_pages(&cma_page_list
);
1271 * We did migrate all the pages, Try to get the page references again
1272 * migrating any new CMA pages which we failed to isolate earlier.
1274 nr_pages
= get_user_pages(start
, nr_pages
, gup_flags
, pages
, vmas
);
1275 if ((nr_pages
> 0) && migrate_allow
) {
1284 static inline long check_and_migrate_cma_pages(unsigned long start
, long nr_pages
,
1285 unsigned int gup_flags
,
1286 struct page
**pages
,
1287 struct vm_area_struct
**vmas
)
1294 * This is the same as get_user_pages() in that it assumes we are
1295 * operating on the current task's mm, but it goes further to validate
1296 * that the vmas associated with the address range are suitable for
1297 * longterm elevated page reference counts. For example, filesystem-dax
1298 * mappings are subject to the lifetime enforced by the filesystem and
1299 * we need guarantees that longterm users like RDMA and V4L2 only
1300 * establish mappings that have a kernel enforced revocation mechanism.
1302 * "longterm" == userspace controlled elevated page count lifetime.
1303 * Contrast this to iov_iter_get_pages() usages which are transient.
1305 long get_user_pages_longterm(unsigned long start
, unsigned long nr_pages
,
1306 unsigned int gup_flags
, struct page
**pages
,
1307 struct vm_area_struct
**vmas_arg
)
1309 struct vm_area_struct
**vmas
= vmas_arg
;
1310 unsigned long flags
;
1317 vmas
= kcalloc(nr_pages
, sizeof(struct vm_area_struct
*),
1323 flags
= memalloc_nocma_save();
1324 rc
= get_user_pages(start
, nr_pages
, gup_flags
, pages
, vmas
);
1325 memalloc_nocma_restore(flags
);
1329 if (check_dax_vmas(vmas
, rc
)) {
1330 for (i
= 0; i
< rc
; i
++)
1336 rc
= check_and_migrate_cma_pages(start
, rc
, gup_flags
, pages
, vmas
);
1338 if (vmas
!= vmas_arg
)
1342 EXPORT_SYMBOL(get_user_pages_longterm
);
1343 #endif /* CONFIG_FS_DAX */
1346 * populate_vma_page_range() - populate a range of pages in the vma.
1348 * @start: start address
1352 * This takes care of mlocking the pages too if VM_LOCKED is set.
1354 * return 0 on success, negative error code on error.
1356 * vma->vm_mm->mmap_sem must be held.
1358 * If @nonblocking is NULL, it may be held for read or write and will
1361 * If @nonblocking is non-NULL, it must held for read only and may be
1362 * released. If it's released, *@nonblocking will be set to 0.
1364 long populate_vma_page_range(struct vm_area_struct
*vma
,
1365 unsigned long start
, unsigned long end
, int *nonblocking
)
1367 struct mm_struct
*mm
= vma
->vm_mm
;
1368 unsigned long nr_pages
= (end
- start
) / PAGE_SIZE
;
1371 VM_BUG_ON(start
& ~PAGE_MASK
);
1372 VM_BUG_ON(end
& ~PAGE_MASK
);
1373 VM_BUG_ON_VMA(start
< vma
->vm_start
, vma
);
1374 VM_BUG_ON_VMA(end
> vma
->vm_end
, vma
);
1375 VM_BUG_ON_MM(!rwsem_is_locked(&mm
->mmap_sem
), mm
);
1377 gup_flags
= FOLL_TOUCH
| FOLL_POPULATE
| FOLL_MLOCK
;
1378 if (vma
->vm_flags
& VM_LOCKONFAULT
)
1379 gup_flags
&= ~FOLL_POPULATE
;
1381 * We want to touch writable mappings with a write fault in order
1382 * to break COW, except for shared mappings because these don't COW
1383 * and we would not want to dirty them for nothing.
1385 if ((vma
->vm_flags
& (VM_WRITE
| VM_SHARED
)) == VM_WRITE
)
1386 gup_flags
|= FOLL_WRITE
;
1389 * We want mlock to succeed for regions that have any permissions
1390 * other than PROT_NONE.
1392 if (vma
->vm_flags
& (VM_READ
| VM_WRITE
| VM_EXEC
))
1393 gup_flags
|= FOLL_FORCE
;
1396 * We made sure addr is within a VMA, so the following will
1397 * not result in a stack expansion that recurses back here.
1399 return __get_user_pages(current
, mm
, start
, nr_pages
, gup_flags
,
1400 NULL
, NULL
, nonblocking
);
1404 * __mm_populate - populate and/or mlock pages within a range of address space.
1406 * This is used to implement mlock() and the MAP_POPULATE / MAP_LOCKED mmap
1407 * flags. VMAs must be already marked with the desired vm_flags, and
1408 * mmap_sem must not be held.
1410 int __mm_populate(unsigned long start
, unsigned long len
, int ignore_errors
)
1412 struct mm_struct
*mm
= current
->mm
;
1413 unsigned long end
, nstart
, nend
;
1414 struct vm_area_struct
*vma
= NULL
;
1420 for (nstart
= start
; nstart
< end
; nstart
= nend
) {
1422 * We want to fault in pages for [nstart; end) address range.
1423 * Find first corresponding VMA.
1427 down_read(&mm
->mmap_sem
);
1428 vma
= find_vma(mm
, nstart
);
1429 } else if (nstart
>= vma
->vm_end
)
1431 if (!vma
|| vma
->vm_start
>= end
)
1434 * Set [nstart; nend) to intersection of desired address
1435 * range with the first VMA. Also, skip undesirable VMA types.
1437 nend
= min(end
, vma
->vm_end
);
1438 if (vma
->vm_flags
& (VM_IO
| VM_PFNMAP
))
1440 if (nstart
< vma
->vm_start
)
1441 nstart
= vma
->vm_start
;
1443 * Now fault in a range of pages. populate_vma_page_range()
1444 * double checks the vma flags, so that it won't mlock pages
1445 * if the vma was already munlocked.
1447 ret
= populate_vma_page_range(vma
, nstart
, nend
, &locked
);
1449 if (ignore_errors
) {
1451 continue; /* continue at next VMA */
1455 nend
= nstart
+ ret
* PAGE_SIZE
;
1459 up_read(&mm
->mmap_sem
);
1460 return ret
; /* 0 or negative error code */
1464 * get_dump_page() - pin user page in memory while writing it to core dump
1465 * @addr: user address
1467 * Returns struct page pointer of user page pinned for dump,
1468 * to be freed afterwards by put_page().
1470 * Returns NULL on any kind of failure - a hole must then be inserted into
1471 * the corefile, to preserve alignment with its headers; and also returns
1472 * NULL wherever the ZERO_PAGE, or an anonymous pte_none, has been found -
1473 * allowing a hole to be left in the corefile to save diskspace.
1475 * Called without mmap_sem, but after all other threads have been killed.
1477 #ifdef CONFIG_ELF_CORE
1478 struct page
*get_dump_page(unsigned long addr
)
1480 struct vm_area_struct
*vma
;
1483 if (__get_user_pages(current
, current
->mm
, addr
, 1,
1484 FOLL_FORCE
| FOLL_DUMP
| FOLL_GET
, &page
, &vma
,
1487 flush_cache_page(vma
, addr
, page_to_pfn(page
));
1490 #endif /* CONFIG_ELF_CORE */
1495 * get_user_pages_fast attempts to pin user pages by walking the page
1496 * tables directly and avoids taking locks. Thus the walker needs to be
1497 * protected from page table pages being freed from under it, and should
1498 * block any THP splits.
1500 * One way to achieve this is to have the walker disable interrupts, and
1501 * rely on IPIs from the TLB flushing code blocking before the page table
1502 * pages are freed. This is unsuitable for architectures that do not need
1503 * to broadcast an IPI when invalidating TLBs.
1505 * Another way to achieve this is to batch up page table containing pages
1506 * belonging to more than one mm_user, then rcu_sched a callback to free those
1507 * pages. Disabling interrupts will allow the fast_gup walker to both block
1508 * the rcu_sched callback, and an IPI that we broadcast for splitting THPs
1509 * (which is a relatively rare event). The code below adopts this strategy.
1511 * Before activating this code, please be aware that the following assumptions
1512 * are currently made:
1514 * *) Either HAVE_RCU_TABLE_FREE is enabled, and tlb_remove_table() is used to
1515 * free pages containing page tables or TLB flushing requires IPI broadcast.
1517 * *) ptes can be read atomically by the architecture.
1519 * *) access_ok is sufficient to validate userspace address ranges.
1521 * The last two assumptions can be relaxed by the addition of helper functions.
1523 * This code is based heavily on the PowerPC implementation by Nick Piggin.
1525 #ifdef CONFIG_HAVE_GENERIC_GUP
1529 * We assume that the PTE can be read atomically. If this is not the case for
1530 * your architecture, please provide the helper.
1532 static inline pte_t
gup_get_pte(pte_t
*ptep
)
1534 return READ_ONCE(*ptep
);
1538 static void undo_dev_pagemap(int *nr
, int nr_start
, struct page
**pages
)
1540 while ((*nr
) - nr_start
) {
1541 struct page
*page
= pages
[--(*nr
)];
1543 ClearPageReferenced(page
);
1548 #ifdef CONFIG_ARCH_HAS_PTE_SPECIAL
1549 static int gup_pte_range(pmd_t pmd
, unsigned long addr
, unsigned long end
,
1550 int write
, struct page
**pages
, int *nr
)
1552 struct dev_pagemap
*pgmap
= NULL
;
1553 int nr_start
= *nr
, ret
= 0;
1556 ptem
= ptep
= pte_offset_map(&pmd
, addr
);
1558 pte_t pte
= gup_get_pte(ptep
);
1559 struct page
*head
, *page
;
1562 * Similar to the PMD case below, NUMA hinting must take slow
1563 * path using the pte_protnone check.
1565 if (pte_protnone(pte
))
1568 if (!pte_access_permitted(pte
, write
))
1571 if (pte_devmap(pte
)) {
1572 pgmap
= get_dev_pagemap(pte_pfn(pte
), pgmap
);
1573 if (unlikely(!pgmap
)) {
1574 undo_dev_pagemap(nr
, nr_start
, pages
);
1577 } else if (pte_special(pte
))
1580 VM_BUG_ON(!pfn_valid(pte_pfn(pte
)));
1581 page
= pte_page(pte
);
1582 head
= compound_head(page
);
1584 if (!page_cache_get_speculative(head
))
1587 if (unlikely(pte_val(pte
) != pte_val(*ptep
))) {
1592 VM_BUG_ON_PAGE(compound_head(page
) != head
, page
);
1594 SetPageReferenced(page
);
1598 } while (ptep
++, addr
+= PAGE_SIZE
, addr
!= end
);
1604 put_dev_pagemap(pgmap
);
1611 * If we can't determine whether or not a pte is special, then fail immediately
1612 * for ptes. Note, we can still pin HugeTLB and THP as these are guaranteed not
1615 * For a futex to be placed on a THP tail page, get_futex_key requires a
1616 * __get_user_pages_fast implementation that can pin pages. Thus it's still
1617 * useful to have gup_huge_pmd even if we can't operate on ptes.
1619 static int gup_pte_range(pmd_t pmd
, unsigned long addr
, unsigned long end
,
1620 int write
, struct page
**pages
, int *nr
)
1624 #endif /* CONFIG_ARCH_HAS_PTE_SPECIAL */
1626 #if defined(__HAVE_ARCH_PTE_DEVMAP) && defined(CONFIG_TRANSPARENT_HUGEPAGE)
1627 static int __gup_device_huge(unsigned long pfn
, unsigned long addr
,
1628 unsigned long end
, struct page
**pages
, int *nr
)
1631 struct dev_pagemap
*pgmap
= NULL
;
1634 struct page
*page
= pfn_to_page(pfn
);
1636 pgmap
= get_dev_pagemap(pfn
, pgmap
);
1637 if (unlikely(!pgmap
)) {
1638 undo_dev_pagemap(nr
, nr_start
, pages
);
1641 SetPageReferenced(page
);
1646 } while (addr
+= PAGE_SIZE
, addr
!= end
);
1649 put_dev_pagemap(pgmap
);
1653 static int __gup_device_huge_pmd(pmd_t orig
, pmd_t
*pmdp
, unsigned long addr
,
1654 unsigned long end
, struct page
**pages
, int *nr
)
1656 unsigned long fault_pfn
;
1659 fault_pfn
= pmd_pfn(orig
) + ((addr
& ~PMD_MASK
) >> PAGE_SHIFT
);
1660 if (!__gup_device_huge(fault_pfn
, addr
, end
, pages
, nr
))
1663 if (unlikely(pmd_val(orig
) != pmd_val(*pmdp
))) {
1664 undo_dev_pagemap(nr
, nr_start
, pages
);
1670 static int __gup_device_huge_pud(pud_t orig
, pud_t
*pudp
, unsigned long addr
,
1671 unsigned long end
, struct page
**pages
, int *nr
)
1673 unsigned long fault_pfn
;
1676 fault_pfn
= pud_pfn(orig
) + ((addr
& ~PUD_MASK
) >> PAGE_SHIFT
);
1677 if (!__gup_device_huge(fault_pfn
, addr
, end
, pages
, nr
))
1680 if (unlikely(pud_val(orig
) != pud_val(*pudp
))) {
1681 undo_dev_pagemap(nr
, nr_start
, pages
);
1687 static int __gup_device_huge_pmd(pmd_t orig
, pmd_t
*pmdp
, unsigned long addr
,
1688 unsigned long end
, struct page
**pages
, int *nr
)
1694 static int __gup_device_huge_pud(pud_t pud
, pud_t
*pudp
, unsigned long addr
,
1695 unsigned long end
, struct page
**pages
, int *nr
)
1702 static int gup_huge_pmd(pmd_t orig
, pmd_t
*pmdp
, unsigned long addr
,
1703 unsigned long end
, int write
, struct page
**pages
, int *nr
)
1705 struct page
*head
, *page
;
1708 if (!pmd_access_permitted(orig
, write
))
1711 if (pmd_devmap(orig
))
1712 return __gup_device_huge_pmd(orig
, pmdp
, addr
, end
, pages
, nr
);
1715 page
= pmd_page(orig
) + ((addr
& ~PMD_MASK
) >> PAGE_SHIFT
);
1721 } while (addr
+= PAGE_SIZE
, addr
!= end
);
1723 head
= compound_head(pmd_page(orig
));
1724 if (!page_cache_add_speculative(head
, refs
)) {
1729 if (unlikely(pmd_val(orig
) != pmd_val(*pmdp
))) {
1736 SetPageReferenced(head
);
1740 static int gup_huge_pud(pud_t orig
, pud_t
*pudp
, unsigned long addr
,
1741 unsigned long end
, int write
, struct page
**pages
, int *nr
)
1743 struct page
*head
, *page
;
1746 if (!pud_access_permitted(orig
, write
))
1749 if (pud_devmap(orig
))
1750 return __gup_device_huge_pud(orig
, pudp
, addr
, end
, pages
, nr
);
1753 page
= pud_page(orig
) + ((addr
& ~PUD_MASK
) >> PAGE_SHIFT
);
1759 } while (addr
+= PAGE_SIZE
, addr
!= end
);
1761 head
= compound_head(pud_page(orig
));
1762 if (!page_cache_add_speculative(head
, refs
)) {
1767 if (unlikely(pud_val(orig
) != pud_val(*pudp
))) {
1774 SetPageReferenced(head
);
1778 static int gup_huge_pgd(pgd_t orig
, pgd_t
*pgdp
, unsigned long addr
,
1779 unsigned long end
, int write
,
1780 struct page
**pages
, int *nr
)
1783 struct page
*head
, *page
;
1785 if (!pgd_access_permitted(orig
, write
))
1788 BUILD_BUG_ON(pgd_devmap(orig
));
1790 page
= pgd_page(orig
) + ((addr
& ~PGDIR_MASK
) >> PAGE_SHIFT
);
1796 } while (addr
+= PAGE_SIZE
, addr
!= end
);
1798 head
= compound_head(pgd_page(orig
));
1799 if (!page_cache_add_speculative(head
, refs
)) {
1804 if (unlikely(pgd_val(orig
) != pgd_val(*pgdp
))) {
1811 SetPageReferenced(head
);
1815 static int gup_pmd_range(pud_t pud
, unsigned long addr
, unsigned long end
,
1816 int write
, struct page
**pages
, int *nr
)
1821 pmdp
= pmd_offset(&pud
, addr
);
1823 pmd_t pmd
= READ_ONCE(*pmdp
);
1825 next
= pmd_addr_end(addr
, end
);
1826 if (!pmd_present(pmd
))
1829 if (unlikely(pmd_trans_huge(pmd
) || pmd_huge(pmd
) ||
1832 * NUMA hinting faults need to be handled in the GUP
1833 * slowpath for accounting purposes and so that they
1834 * can be serialised against THP migration.
1836 if (pmd_protnone(pmd
))
1839 if (!gup_huge_pmd(pmd
, pmdp
, addr
, next
, write
,
1843 } else if (unlikely(is_hugepd(__hugepd(pmd_val(pmd
))))) {
1845 * architecture have different format for hugetlbfs
1846 * pmd format and THP pmd format
1848 if (!gup_huge_pd(__hugepd(pmd_val(pmd
)), addr
,
1849 PMD_SHIFT
, next
, write
, pages
, nr
))
1851 } else if (!gup_pte_range(pmd
, addr
, next
, write
, pages
, nr
))
1853 } while (pmdp
++, addr
= next
, addr
!= end
);
1858 static int gup_pud_range(p4d_t p4d
, unsigned long addr
, unsigned long end
,
1859 int write
, struct page
**pages
, int *nr
)
1864 pudp
= pud_offset(&p4d
, addr
);
1866 pud_t pud
= READ_ONCE(*pudp
);
1868 next
= pud_addr_end(addr
, end
);
1871 if (unlikely(pud_huge(pud
))) {
1872 if (!gup_huge_pud(pud
, pudp
, addr
, next
, write
,
1875 } else if (unlikely(is_hugepd(__hugepd(pud_val(pud
))))) {
1876 if (!gup_huge_pd(__hugepd(pud_val(pud
)), addr
,
1877 PUD_SHIFT
, next
, write
, pages
, nr
))
1879 } else if (!gup_pmd_range(pud
, addr
, next
, write
, pages
, nr
))
1881 } while (pudp
++, addr
= next
, addr
!= end
);
1886 static int gup_p4d_range(pgd_t pgd
, unsigned long addr
, unsigned long end
,
1887 int write
, struct page
**pages
, int *nr
)
1892 p4dp
= p4d_offset(&pgd
, addr
);
1894 p4d_t p4d
= READ_ONCE(*p4dp
);
1896 next
= p4d_addr_end(addr
, end
);
1899 BUILD_BUG_ON(p4d_huge(p4d
));
1900 if (unlikely(is_hugepd(__hugepd(p4d_val(p4d
))))) {
1901 if (!gup_huge_pd(__hugepd(p4d_val(p4d
)), addr
,
1902 P4D_SHIFT
, next
, write
, pages
, nr
))
1904 } else if (!gup_pud_range(p4d
, addr
, next
, write
, pages
, nr
))
1906 } while (p4dp
++, addr
= next
, addr
!= end
);
1911 static void gup_pgd_range(unsigned long addr
, unsigned long end
,
1912 int write
, struct page
**pages
, int *nr
)
1917 pgdp
= pgd_offset(current
->mm
, addr
);
1919 pgd_t pgd
= READ_ONCE(*pgdp
);
1921 next
= pgd_addr_end(addr
, end
);
1924 if (unlikely(pgd_huge(pgd
))) {
1925 if (!gup_huge_pgd(pgd
, pgdp
, addr
, next
, write
,
1928 } else if (unlikely(is_hugepd(__hugepd(pgd_val(pgd
))))) {
1929 if (!gup_huge_pd(__hugepd(pgd_val(pgd
)), addr
,
1930 PGDIR_SHIFT
, next
, write
, pages
, nr
))
1932 } else if (!gup_p4d_range(pgd
, addr
, next
, write
, pages
, nr
))
1934 } while (pgdp
++, addr
= next
, addr
!= end
);
1937 #ifndef gup_fast_permitted
1939 * Check if it's allowed to use __get_user_pages_fast() for the range, or
1940 * we need to fall back to the slow version:
1942 bool gup_fast_permitted(unsigned long start
, int nr_pages
)
1944 unsigned long len
, end
;
1946 len
= (unsigned long) nr_pages
<< PAGE_SHIFT
;
1948 return end
>= start
;
1953 * Like get_user_pages_fast() except it's IRQ-safe in that it won't fall back to
1955 * Note a difference with get_user_pages_fast: this always returns the
1956 * number of pages pinned, 0 if no pages were pinned.
1958 int __get_user_pages_fast(unsigned long start
, int nr_pages
, int write
,
1959 struct page
**pages
)
1961 unsigned long len
, end
;
1962 unsigned long flags
;
1966 len
= (unsigned long) nr_pages
<< PAGE_SHIFT
;
1969 if (unlikely(!access_ok((void __user
*)start
, len
)))
1973 * Disable interrupts. We use the nested form as we can already have
1974 * interrupts disabled by get_futex_key.
1976 * With interrupts disabled, we block page table pages from being
1977 * freed from under us. See struct mmu_table_batch comments in
1978 * include/asm-generic/tlb.h for more details.
1980 * We do not adopt an rcu_read_lock(.) here as we also want to
1981 * block IPIs that come from THPs splitting.
1984 if (gup_fast_permitted(start
, nr_pages
)) {
1985 local_irq_save(flags
);
1986 gup_pgd_range(start
, end
, write
, pages
, &nr
);
1987 local_irq_restore(flags
);
1994 * get_user_pages_fast() - pin user pages in memory
1995 * @start: starting user address
1996 * @nr_pages: number of pages from start to pin
1997 * @write: whether pages will be written to
1998 * @pages: array that receives pointers to the pages pinned.
1999 * Should be at least nr_pages long.
2001 * Attempt to pin user pages in memory without taking mm->mmap_sem.
2002 * If not successful, it will fall back to taking the lock and
2003 * calling get_user_pages().
2005 * Returns number of pages pinned. This may be fewer than the number
2006 * requested. If nr_pages is 0 or negative, returns 0. If no pages
2007 * were pinned, returns -errno.
2009 int get_user_pages_fast(unsigned long start
, int nr_pages
, int write
,
2010 struct page
**pages
)
2012 unsigned long addr
, len
, end
;
2013 int nr
= 0, ret
= 0;
2017 len
= (unsigned long) nr_pages
<< PAGE_SHIFT
;
2023 if (unlikely(!access_ok((void __user
*)start
, len
)))
2026 if (gup_fast_permitted(start
, nr_pages
)) {
2027 local_irq_disable();
2028 gup_pgd_range(addr
, end
, write
, pages
, &nr
);
2033 if (nr
< nr_pages
) {
2034 /* Try to get the remaining pages with get_user_pages */
2035 start
+= nr
<< PAGE_SHIFT
;
2038 ret
= get_user_pages_unlocked(start
, nr_pages
- nr
, pages
,
2039 write
? FOLL_WRITE
: 0);
2041 /* Have to be a bit careful with return values */
2053 #endif /* CONFIG_HAVE_GENERIC_GUP */