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>
23 static struct page *no_page_table(struct vm_area_struct *vma,
27 * When core dumping an enormous anonymous area that nobody
28 * has touched so far, we don't want to allocate unnecessary pages or
29 * page tables. Return error instead of NULL to skip handle_mm_fault,
30 * then get_dump_page() will return NULL to leave a hole in the dump.
31 * But we can only make this optimization where a hole would surely
32 * be zero-filled if handle_mm_fault() actually did handle it.
34 if ((flags & FOLL_DUMP) && (!vma->vm_ops || !vma->vm_ops->fault))
35 return ERR_PTR(-EFAULT);
39 static int follow_pfn_pte(struct vm_area_struct *vma, unsigned long address,
40 pte_t *pte, unsigned int flags)
42 /* No page to get reference */
46 if (flags & FOLL_TOUCH) {
49 if (flags & FOLL_WRITE)
50 entry = pte_mkdirty(entry);
51 entry = pte_mkyoung(entry);
53 if (!pte_same(*pte, entry)) {
54 set_pte_at(vma->vm_mm, address, pte, entry);
55 update_mmu_cache(vma, address, pte);
59 /* Proper page table entry exists, but no corresponding struct page */
64 * FOLL_FORCE or a forced COW break can write even to unwritable pte's,
65 * but only after we've gone through a COW cycle and they are dirty.
67 static inline bool can_follow_write_pte(pte_t pte, unsigned int flags)
69 return pte_write(pte) || ((flags & FOLL_COW) && pte_dirty(pte));
73 * A (separate) COW fault might break the page the other way and
74 * get_user_pages() would return the page from what is now the wrong
75 * VM. So we need to force a COW break at GUP time even for reads.
77 static inline bool should_force_cow_break(struct vm_area_struct *vma, unsigned int flags)
79 return is_cow_mapping(vma->vm_flags) && (flags & FOLL_GET);
82 static struct page *follow_page_pte(struct vm_area_struct *vma,
83 unsigned long address, pmd_t *pmd, unsigned int flags)
85 struct mm_struct *mm = vma->vm_mm;
86 struct dev_pagemap *pgmap = NULL;
92 if (unlikely(pmd_bad(*pmd)))
93 return no_page_table(vma, flags);
95 ptep = pte_offset_map_lock(mm, pmd, address, &ptl);
97 if (!pte_present(pte)) {
100 * KSM's break_ksm() relies upon recognizing a ksm page
101 * even while it is being migrated, so for that case we
102 * need migration_entry_wait().
104 if (likely(!(flags & FOLL_MIGRATION)))
108 entry = pte_to_swp_entry(pte);
109 if (!is_migration_entry(entry))
111 pte_unmap_unlock(ptep, ptl);
112 migration_entry_wait(mm, pmd, address);
115 if ((flags & FOLL_NUMA) && pte_protnone(pte))
117 if ((flags & FOLL_WRITE) && !can_follow_write_pte(pte, flags)) {
118 pte_unmap_unlock(ptep, ptl);
122 page = vm_normal_page(vma, address, pte);
123 if (!page && pte_devmap(pte) && (flags & FOLL_GET)) {
125 * Only return device mapping pages in the FOLL_GET case since
126 * they are only valid while holding the pgmap reference.
128 pgmap = get_dev_pagemap(pte_pfn(pte), NULL);
130 page = pte_page(pte);
133 } else if (unlikely(!page)) {
134 if (flags & FOLL_DUMP) {
135 /* Avoid special (like zero) pages in core dumps */
136 page = ERR_PTR(-EFAULT);
140 if (is_zero_pfn(pte_pfn(pte))) {
141 page = pte_page(pte);
145 ret = follow_pfn_pte(vma, address, ptep, flags);
151 if (flags & FOLL_SPLIT && PageTransCompound(page)) {
154 pte_unmap_unlock(ptep, ptl);
156 ret = split_huge_page(page);
164 if (flags & FOLL_GET) {
165 if (unlikely(!try_get_page(page))) {
166 page = ERR_PTR(-ENOMEM);
170 /* drop the pgmap reference now that we hold the page */
172 put_dev_pagemap(pgmap);
176 if (flags & FOLL_TOUCH) {
177 if ((flags & FOLL_WRITE) &&
178 !pte_dirty(pte) && !PageDirty(page))
179 set_page_dirty(page);
181 * pte_mkyoung() would be more correct here, but atomic care
182 * is needed to avoid losing the dirty bit: it is easier to use
183 * mark_page_accessed().
185 mark_page_accessed(page);
187 if ((flags & FOLL_MLOCK) && (vma->vm_flags & VM_LOCKED)) {
188 /* Do not mlock pte-mapped THP */
189 if (PageTransCompound(page))
193 * The preliminary mapping check is mainly to avoid the
194 * pointless overhead of lock_page on the ZERO_PAGE
195 * which might bounce very badly if there is contention.
197 * If the page is already locked, we don't need to
198 * handle it now - vmscan will handle it later if and
199 * when it attempts to reclaim the page.
201 if (page->mapping && trylock_page(page)) {
202 lru_add_drain(); /* push cached pages to LRU */
204 * Because we lock page here, and migration is
205 * blocked by the pte's page reference, and we
206 * know the page is still mapped, we don't even
207 * need to check for file-cache page truncation.
209 mlock_vma_page(page);
214 pte_unmap_unlock(ptep, ptl);
217 pte_unmap_unlock(ptep, ptl);
220 return no_page_table(vma, flags);
223 static struct page *follow_pmd_mask(struct vm_area_struct *vma,
224 unsigned long address, pud_t *pudp,
225 unsigned int flags, unsigned int *page_mask)
230 struct mm_struct *mm = vma->vm_mm;
232 pmd = pmd_offset(pudp, address);
234 return no_page_table(vma, flags);
235 if (pmd_huge(*pmd) && vma->vm_flags & VM_HUGETLB) {
236 page = follow_huge_pmd(mm, address, pmd, flags);
239 return no_page_table(vma, flags);
241 if (is_hugepd(__hugepd(pmd_val(*pmd)))) {
242 page = follow_huge_pd(vma, address,
243 __hugepd(pmd_val(*pmd)), flags,
247 return no_page_table(vma, flags);
250 if (!pmd_present(*pmd)) {
251 if (likely(!(flags & FOLL_MIGRATION)))
252 return no_page_table(vma, flags);
253 VM_BUG_ON(thp_migration_supported() &&
254 !is_pmd_migration_entry(*pmd));
255 if (is_pmd_migration_entry(*pmd))
256 pmd_migration_entry_wait(mm, pmd);
259 if (pmd_devmap(*pmd)) {
260 ptl = pmd_lock(mm, pmd);
261 page = follow_devmap_pmd(vma, address, pmd, flags);
266 if (likely(!pmd_trans_huge(*pmd)))
267 return follow_page_pte(vma, address, pmd, flags);
269 if ((flags & FOLL_NUMA) && pmd_protnone(*pmd))
270 return no_page_table(vma, flags);
273 ptl = pmd_lock(mm, pmd);
274 if (unlikely(!pmd_present(*pmd))) {
276 if (likely(!(flags & FOLL_MIGRATION)))
277 return no_page_table(vma, flags);
278 pmd_migration_entry_wait(mm, pmd);
281 if (unlikely(!pmd_trans_huge(*pmd))) {
283 return follow_page_pte(vma, address, pmd, flags);
285 if (flags & FOLL_SPLIT) {
287 page = pmd_page(*pmd);
288 if (is_huge_zero_page(page)) {
291 split_huge_pmd(vma, pmd, address);
292 if (pmd_trans_unstable(pmd))
295 if (unlikely(!try_get_page(page))) {
297 return ERR_PTR(-ENOMEM);
301 ret = split_huge_page(page);
305 return no_page_table(vma, flags);
308 return ret ? ERR_PTR(ret) :
309 follow_page_pte(vma, address, pmd, flags);
311 page = follow_trans_huge_pmd(vma, address, pmd, flags);
313 *page_mask = HPAGE_PMD_NR - 1;
318 static struct page *follow_pud_mask(struct vm_area_struct *vma,
319 unsigned long address, p4d_t *p4dp,
320 unsigned int flags, unsigned int *page_mask)
325 struct mm_struct *mm = vma->vm_mm;
327 pud = pud_offset(p4dp, address);
329 return no_page_table(vma, flags);
330 if (pud_huge(*pud) && vma->vm_flags & VM_HUGETLB) {
331 page = follow_huge_pud(mm, address, pud, flags);
334 return no_page_table(vma, flags);
336 if (is_hugepd(__hugepd(pud_val(*pud)))) {
337 page = follow_huge_pd(vma, address,
338 __hugepd(pud_val(*pud)), flags,
342 return no_page_table(vma, flags);
344 if (pud_devmap(*pud)) {
345 ptl = pud_lock(mm, pud);
346 page = follow_devmap_pud(vma, address, pud, flags);
351 if (unlikely(pud_bad(*pud)))
352 return no_page_table(vma, flags);
354 return follow_pmd_mask(vma, address, pud, flags, page_mask);
358 static struct page *follow_p4d_mask(struct vm_area_struct *vma,
359 unsigned long address, pgd_t *pgdp,
360 unsigned int flags, unsigned int *page_mask)
365 p4d = p4d_offset(pgdp, address);
367 return no_page_table(vma, flags);
368 BUILD_BUG_ON(p4d_huge(*p4d));
369 if (unlikely(p4d_bad(*p4d)))
370 return no_page_table(vma, flags);
372 if (is_hugepd(__hugepd(p4d_val(*p4d)))) {
373 page = follow_huge_pd(vma, address,
374 __hugepd(p4d_val(*p4d)), flags,
378 return no_page_table(vma, flags);
380 return follow_pud_mask(vma, address, p4d, flags, page_mask);
384 * follow_page_mask - look up a page descriptor from a user-virtual address
385 * @vma: vm_area_struct mapping @address
386 * @address: virtual address to look up
387 * @flags: flags modifying lookup behaviour
388 * @page_mask: on output, *page_mask is set according to the size of the page
390 * @flags can have FOLL_ flags set, defined in <linux/mm.h>
392 * Returns the mapped (struct page *), %NULL if no mapping exists, or
393 * an error pointer if there is a mapping to something not represented
394 * by a page descriptor (see also vm_normal_page()).
396 struct page *follow_page_mask(struct vm_area_struct *vma,
397 unsigned long address, unsigned int flags,
398 unsigned int *page_mask)
402 struct mm_struct *mm = vma->vm_mm;
406 /* make this handle hugepd */
407 page = follow_huge_addr(mm, address, flags & FOLL_WRITE);
409 BUG_ON(flags & FOLL_GET);
413 pgd = pgd_offset(mm, address);
415 if (pgd_none(*pgd) || unlikely(pgd_bad(*pgd)))
416 return no_page_table(vma, flags);
418 if (pgd_huge(*pgd)) {
419 page = follow_huge_pgd(mm, address, pgd, flags);
422 return no_page_table(vma, flags);
424 if (is_hugepd(__hugepd(pgd_val(*pgd)))) {
425 page = follow_huge_pd(vma, address,
426 __hugepd(pgd_val(*pgd)), flags,
430 return no_page_table(vma, flags);
433 return follow_p4d_mask(vma, address, pgd, flags, page_mask);
436 static int get_gate_page(struct mm_struct *mm, unsigned long address,
437 unsigned int gup_flags, struct vm_area_struct **vma,
447 /* user gate pages are read-only */
448 if (gup_flags & FOLL_WRITE)
450 if (address > TASK_SIZE)
451 pgd = pgd_offset_k(address);
453 pgd = pgd_offset_gate(mm, address);
456 p4d = p4d_offset(pgd, address);
459 pud = pud_offset(p4d, address);
462 pmd = pmd_offset(pud, address);
463 if (!pmd_present(*pmd))
465 VM_BUG_ON(pmd_trans_huge(*pmd));
466 pte = pte_offset_map(pmd, address);
469 *vma = get_gate_vma(mm);
472 *page = vm_normal_page(*vma, address, *pte);
474 if ((gup_flags & FOLL_DUMP) || !is_zero_pfn(pte_pfn(*pte)))
476 *page = pte_page(*pte);
479 * This should never happen (a device public page in the gate
482 if (is_device_public_page(*page))
485 if (unlikely(!try_get_page(*page))) {
497 * mmap_sem must be held on entry. If @nonblocking != NULL and
498 * *@flags does not include FOLL_NOWAIT, the mmap_sem may be released.
499 * If it is, *@nonblocking will be set to 0 and -EBUSY returned.
501 static int faultin_page(struct task_struct *tsk, struct vm_area_struct *vma,
502 unsigned long address, unsigned int *flags, int *nonblocking)
504 unsigned int fault_flags = 0;
507 /* mlock all present pages, but do not fault in new pages */
508 if ((*flags & (FOLL_POPULATE | FOLL_MLOCK)) == FOLL_MLOCK)
510 if (*flags & FOLL_WRITE)
511 fault_flags |= FAULT_FLAG_WRITE;
512 if (*flags & FOLL_REMOTE)
513 fault_flags |= FAULT_FLAG_REMOTE;
515 fault_flags |= FAULT_FLAG_ALLOW_RETRY;
516 if (*flags & FOLL_NOWAIT)
517 fault_flags |= FAULT_FLAG_ALLOW_RETRY | FAULT_FLAG_RETRY_NOWAIT;
518 if (*flags & FOLL_TRIED) {
519 VM_WARN_ON_ONCE(fault_flags & FAULT_FLAG_ALLOW_RETRY);
520 fault_flags |= FAULT_FLAG_TRIED;
523 ret = handle_mm_fault(vma, address, fault_flags);
524 if (ret & VM_FAULT_ERROR) {
525 int err = vm_fault_to_errno(ret, *flags);
533 if (ret & VM_FAULT_MAJOR)
539 if (ret & VM_FAULT_RETRY) {
546 * The VM_FAULT_WRITE bit tells us that do_wp_page has broken COW when
547 * necessary, even if maybe_mkwrite decided not to set pte_write. We
548 * can thus safely do subsequent page lookups as if they were reads.
549 * But only do so when looping for pte_write is futile: in some cases
550 * userspace may also be wanting to write to the gotten user page,
551 * which a read fault here might prevent (a readonly page might get
552 * reCOWed by userspace write).
554 if ((ret & VM_FAULT_WRITE) && !(vma->vm_flags & VM_WRITE))
559 static int check_vma_flags(struct vm_area_struct *vma, unsigned long gup_flags)
561 vm_flags_t vm_flags = vma->vm_flags;
562 int write = (gup_flags & FOLL_WRITE);
563 int foreign = (gup_flags & FOLL_REMOTE);
565 if (vm_flags & (VM_IO | VM_PFNMAP))
568 if (gup_flags & FOLL_ANON && !vma_is_anonymous(vma))
572 if (!(vm_flags & VM_WRITE)) {
573 if (!(gup_flags & FOLL_FORCE))
576 * We used to let the write,force case do COW in a
577 * VM_MAYWRITE VM_SHARED !VM_WRITE vma, so ptrace could
578 * set a breakpoint in a read-only mapping of an
579 * executable, without corrupting the file (yet only
580 * when that file had been opened for writing!).
581 * Anon pages in shared mappings are surprising: now
584 if (!is_cow_mapping(vm_flags))
587 } else if (!(vm_flags & VM_READ)) {
588 if (!(gup_flags & FOLL_FORCE))
591 * Is there actually any vma we can reach here which does not
592 * have VM_MAYREAD set?
594 if (!(vm_flags & VM_MAYREAD))
598 * gups are always data accesses, not instruction
599 * fetches, so execute=false here
601 if (!arch_vma_access_permitted(vma, write, false, foreign))
607 * __get_user_pages() - pin user pages in memory
608 * @tsk: task_struct of target task
609 * @mm: mm_struct of target mm
610 * @start: starting user address
611 * @nr_pages: number of pages from start to pin
612 * @gup_flags: flags modifying pin behaviour
613 * @pages: array that receives pointers to the pages pinned.
614 * Should be at least nr_pages long. Or NULL, if caller
615 * only intends to ensure the pages are faulted in.
616 * @vmas: array of pointers to vmas corresponding to each page.
617 * Or NULL if the caller does not require them.
618 * @nonblocking: whether waiting for disk IO or mmap_sem contention
620 * Returns number of pages pinned. This may be fewer than the number
621 * requested. If nr_pages is 0 or negative, returns 0. If no pages
622 * were pinned, returns -errno. Each page returned must be released
623 * with a put_page() call when it is finished with. vmas will only
624 * remain valid while mmap_sem is held.
626 * Must be called with mmap_sem held. It may be released. See below.
628 * __get_user_pages walks a process's page tables and takes a reference to
629 * each struct page that each user address corresponds to at a given
630 * instant. That is, it takes the page that would be accessed if a user
631 * thread accesses the given user virtual address at that instant.
633 * This does not guarantee that the page exists in the user mappings when
634 * __get_user_pages returns, and there may even be a completely different
635 * page there in some cases (eg. if mmapped pagecache has been invalidated
636 * and subsequently re faulted). However it does guarantee that the page
637 * won't be freed completely. And mostly callers simply care that the page
638 * contains data that was valid *at some point in time*. Typically, an IO
639 * or similar operation cannot guarantee anything stronger anyway because
640 * locks can't be held over the syscall boundary.
642 * If @gup_flags & FOLL_WRITE == 0, the page must not be written to. If
643 * the page is written to, set_page_dirty (or set_page_dirty_lock, as
644 * appropriate) must be called after the page is finished with, and
645 * before put_page is called.
647 * If @nonblocking != NULL, __get_user_pages will not wait for disk IO
648 * or mmap_sem contention, and if waiting is needed to pin all pages,
649 * *@nonblocking will be set to 0. Further, if @gup_flags does not
650 * include FOLL_NOWAIT, the mmap_sem will be released via up_read() in
653 * A caller using such a combination of @nonblocking and @gup_flags
654 * must therefore hold the mmap_sem for reading only, and recognize
655 * when it's been released. Otherwise, it must be held for either
656 * reading or writing and will not be released.
658 * In most cases, get_user_pages or get_user_pages_fast should be used
659 * instead of __get_user_pages. __get_user_pages should be used only if
660 * you need some special @gup_flags.
662 static long __get_user_pages(struct task_struct *tsk, struct mm_struct *mm,
663 unsigned long start, unsigned long nr_pages,
664 unsigned int gup_flags, struct page **pages,
665 struct vm_area_struct **vmas, int *nonblocking)
668 unsigned int page_mask;
669 struct vm_area_struct *vma = NULL;
674 VM_BUG_ON(!!pages != !!(gup_flags & FOLL_GET));
677 * If FOLL_FORCE is set then do not force a full fault as the hinting
678 * fault information is unrelated to the reference behaviour of a task
679 * using the address space
681 if (!(gup_flags & FOLL_FORCE))
682 gup_flags |= FOLL_NUMA;
686 unsigned int foll_flags = gup_flags;
687 unsigned int page_increm;
689 /* first iteration or cross vma bound */
690 if (!vma || start >= vma->vm_end) {
691 vma = find_extend_vma(mm, start);
692 if (!vma && in_gate_area(mm, start)) {
694 ret = get_gate_page(mm, start & PAGE_MASK,
696 pages ? &pages[i] : NULL);
703 if (!vma || check_vma_flags(vma, gup_flags))
704 return i ? : -EFAULT;
705 if (is_vm_hugetlb_page(vma)) {
706 if (should_force_cow_break(vma, foll_flags))
707 foll_flags |= FOLL_WRITE;
708 i = follow_hugetlb_page(mm, vma, pages, vmas,
709 &start, &nr_pages, i,
710 foll_flags, nonblocking);
715 if (should_force_cow_break(vma, foll_flags))
716 foll_flags |= FOLL_WRITE;
720 * If we have a pending SIGKILL, don't keep faulting pages and
721 * potentially allocating memory.
723 if (unlikely(fatal_signal_pending(current)))
724 return i ? i : -ERESTARTSYS;
726 page = follow_page_mask(vma, start, foll_flags, &page_mask);
729 ret = faultin_page(tsk, vma, start, &foll_flags,
744 } else if (PTR_ERR(page) == -EEXIST) {
746 * Proper page table entry exists, but no corresponding
750 } else if (IS_ERR(page)) {
751 return i ? i : PTR_ERR(page);
755 flush_anon_page(vma, page, start);
756 flush_dcache_page(page);
764 page_increm = 1 + (~(start >> PAGE_SHIFT) & page_mask);
765 if (page_increm > nr_pages)
766 page_increm = nr_pages;
768 start += page_increm * PAGE_SIZE;
769 nr_pages -= page_increm;
774 static bool vma_permits_fault(struct vm_area_struct *vma,
775 unsigned int fault_flags)
777 bool write = !!(fault_flags & FAULT_FLAG_WRITE);
778 bool foreign = !!(fault_flags & FAULT_FLAG_REMOTE);
779 vm_flags_t vm_flags = write ? VM_WRITE : VM_READ;
781 if (!(vm_flags & vma->vm_flags))
785 * The architecture might have a hardware protection
786 * mechanism other than read/write that can deny access.
788 * gup always represents data access, not instruction
789 * fetches, so execute=false here:
791 if (!arch_vma_access_permitted(vma, write, false, foreign))
798 * fixup_user_fault() - manually resolve a user page fault
799 * @tsk: the task_struct to use for page fault accounting, or
800 * NULL if faults are not to be recorded.
801 * @mm: mm_struct of target mm
802 * @address: user address
803 * @fault_flags:flags to pass down to handle_mm_fault()
804 * @unlocked: did we unlock the mmap_sem while retrying, maybe NULL if caller
805 * does not allow retry
807 * This is meant to be called in the specific scenario where for locking reasons
808 * we try to access user memory in atomic context (within a pagefault_disable()
809 * section), this returns -EFAULT, and we want to resolve the user fault before
812 * Typically this is meant to be used by the futex code.
814 * The main difference with get_user_pages() is that this function will
815 * unconditionally call handle_mm_fault() which will in turn perform all the
816 * necessary SW fixup of the dirty and young bits in the PTE, while
817 * get_user_pages() only guarantees to update these in the struct page.
819 * This is important for some architectures where those bits also gate the
820 * access permission to the page because they are maintained in software. On
821 * such architectures, gup() will not be enough to make a subsequent access
824 * This function will not return with an unlocked mmap_sem. So it has not the
825 * same semantics wrt the @mm->mmap_sem as does filemap_fault().
827 int fixup_user_fault(struct task_struct *tsk, struct mm_struct *mm,
828 unsigned long address, unsigned int fault_flags,
831 struct vm_area_struct *vma;
835 fault_flags |= FAULT_FLAG_ALLOW_RETRY;
838 vma = find_extend_vma(mm, address);
839 if (!vma || address < vma->vm_start)
842 if (!vma_permits_fault(vma, fault_flags))
845 ret = handle_mm_fault(vma, address, fault_flags);
846 major |= ret & VM_FAULT_MAJOR;
847 if (ret & VM_FAULT_ERROR) {
848 int err = vm_fault_to_errno(ret, 0);
855 if (ret & VM_FAULT_RETRY) {
856 down_read(&mm->mmap_sem);
857 if (!(fault_flags & FAULT_FLAG_TRIED)) {
859 fault_flags &= ~FAULT_FLAG_ALLOW_RETRY;
860 fault_flags |= FAULT_FLAG_TRIED;
873 EXPORT_SYMBOL_GPL(fixup_user_fault);
875 static __always_inline long __get_user_pages_locked(struct task_struct *tsk,
876 struct mm_struct *mm,
878 unsigned long nr_pages,
880 struct vm_area_struct **vmas,
881 int *locked, bool notify_drop,
884 long ret, pages_done;
888 /* if VM_FAULT_RETRY can be returned, vmas become invalid */
890 /* check caller initialized locked */
891 BUG_ON(*locked != 1);
898 lock_dropped = false;
900 ret = __get_user_pages(tsk, mm, start, nr_pages, flags, pages,
903 /* VM_FAULT_RETRY couldn't trigger, bypass */
906 /* VM_FAULT_RETRY cannot return errors */
909 BUG_ON(ret >= nr_pages);
913 /* If it's a prefault don't insist harder */
923 /* VM_FAULT_RETRY didn't trigger */
928 /* VM_FAULT_RETRY triggered, so seek to the faulting offset */
930 start += ret << PAGE_SHIFT;
933 * Repeat on the address that fired VM_FAULT_RETRY
934 * without FAULT_FLAG_ALLOW_RETRY but with
939 down_read(&mm->mmap_sem);
940 ret = __get_user_pages(tsk, mm, start, 1, flags | FOLL_TRIED,
955 if (notify_drop && lock_dropped && *locked) {
957 * We must let the caller know we temporarily dropped the lock
958 * and so the critical section protected by it was lost.
960 up_read(&mm->mmap_sem);
967 * We can leverage the VM_FAULT_RETRY functionality in the page fault
968 * paths better by using either get_user_pages_locked() or
969 * get_user_pages_unlocked().
971 * get_user_pages_locked() is suitable to replace the form:
973 * down_read(&mm->mmap_sem);
975 * get_user_pages(tsk, mm, ..., pages, NULL);
976 * up_read(&mm->mmap_sem);
981 * down_read(&mm->mmap_sem);
983 * get_user_pages_locked(tsk, mm, ..., pages, &locked);
985 * up_read(&mm->mmap_sem);
987 long get_user_pages_locked(unsigned long start, unsigned long nr_pages,
988 unsigned int gup_flags, struct page **pages,
991 return __get_user_pages_locked(current, current->mm, start, nr_pages,
992 pages, NULL, locked, true,
993 gup_flags | FOLL_TOUCH);
995 EXPORT_SYMBOL(get_user_pages_locked);
998 * Same as get_user_pages_unlocked(...., FOLL_TOUCH) but it allows for
999 * tsk, mm to be specified.
1001 * NOTE: here FOLL_TOUCH is not set implicitly and must be set by the
1002 * caller if required (just like with __get_user_pages). "FOLL_GET"
1003 * is set implicitly if "pages" is non-NULL.
1005 static __always_inline long __get_user_pages_unlocked(struct task_struct *tsk,
1006 struct mm_struct *mm, unsigned long start,
1007 unsigned long nr_pages, struct page **pages,
1008 unsigned int gup_flags)
1013 down_read(&mm->mmap_sem);
1014 ret = __get_user_pages_locked(tsk, mm, start, nr_pages, pages, NULL,
1015 &locked, false, gup_flags);
1017 up_read(&mm->mmap_sem);
1022 * get_user_pages_unlocked() is suitable to replace the form:
1024 * down_read(&mm->mmap_sem);
1025 * get_user_pages(tsk, mm, ..., pages, NULL);
1026 * up_read(&mm->mmap_sem);
1030 * get_user_pages_unlocked(tsk, mm, ..., pages);
1032 * It is functionally equivalent to get_user_pages_fast so
1033 * get_user_pages_fast should be used instead if specific gup_flags
1034 * (e.g. FOLL_FORCE) are not required.
1036 long get_user_pages_unlocked(unsigned long start, unsigned long nr_pages,
1037 struct page **pages, unsigned int gup_flags)
1039 return __get_user_pages_unlocked(current, current->mm, start, nr_pages,
1040 pages, gup_flags | FOLL_TOUCH);
1042 EXPORT_SYMBOL(get_user_pages_unlocked);
1045 * get_user_pages_remote() - pin user pages in memory
1046 * @tsk: the task_struct to use for page fault accounting, or
1047 * NULL if faults are not to be recorded.
1048 * @mm: mm_struct of target mm
1049 * @start: starting user address
1050 * @nr_pages: number of pages from start to pin
1051 * @gup_flags: flags modifying lookup behaviour
1052 * @pages: array that receives pointers to the pages pinned.
1053 * Should be at least nr_pages long. Or NULL, if caller
1054 * only intends to ensure the pages are faulted in.
1055 * @vmas: array of pointers to vmas corresponding to each page.
1056 * Or NULL if the caller does not require them.
1057 * @locked: pointer to lock flag indicating whether lock is held and
1058 * subsequently whether VM_FAULT_RETRY functionality can be
1059 * utilised. Lock must initially be held.
1061 * Returns number of pages pinned. This may be fewer than the number
1062 * requested. If nr_pages is 0 or negative, returns 0. If no pages
1063 * were pinned, returns -errno. Each page returned must be released
1064 * with a put_page() call when it is finished with. vmas will only
1065 * remain valid while mmap_sem is held.
1067 * Must be called with mmap_sem held for read or write.
1069 * get_user_pages walks a process's page tables and takes a reference to
1070 * each struct page that each user address corresponds to at a given
1071 * instant. That is, it takes the page that would be accessed if a user
1072 * thread accesses the given user virtual address at that instant.
1074 * This does not guarantee that the page exists in the user mappings when
1075 * get_user_pages returns, and there may even be a completely different
1076 * page there in some cases (eg. if mmapped pagecache has been invalidated
1077 * and subsequently re faulted). However it does guarantee that the page
1078 * won't be freed completely. And mostly callers simply care that the page
1079 * contains data that was valid *at some point in time*. Typically, an IO
1080 * or similar operation cannot guarantee anything stronger anyway because
1081 * locks can't be held over the syscall boundary.
1083 * If gup_flags & FOLL_WRITE == 0, the page must not be written to. If the page
1084 * is written to, set_page_dirty (or set_page_dirty_lock, as appropriate) must
1085 * be called after the page is finished with, and before put_page is called.
1087 * get_user_pages is typically used for fewer-copy IO operations, to get a
1088 * handle on the memory by some means other than accesses via the user virtual
1089 * addresses. The pages may be submitted for DMA to devices or accessed via
1090 * their kernel linear mapping (via the kmap APIs). Care should be taken to
1091 * use the correct cache flushing APIs.
1093 * See also get_user_pages_fast, for performance critical applications.
1095 * get_user_pages should be phased out in favor of
1096 * get_user_pages_locked|unlocked or get_user_pages_fast. Nothing
1097 * should use get_user_pages because it cannot pass
1098 * FAULT_FLAG_ALLOW_RETRY to handle_mm_fault.
1100 long get_user_pages_remote(struct task_struct *tsk, struct mm_struct *mm,
1101 unsigned long start, unsigned long nr_pages,
1102 unsigned int gup_flags, struct page **pages,
1103 struct vm_area_struct **vmas, int *locked)
1105 return __get_user_pages_locked(tsk, mm, start, nr_pages, pages, vmas,
1107 gup_flags | FOLL_TOUCH | FOLL_REMOTE);
1109 EXPORT_SYMBOL(get_user_pages_remote);
1112 * This is the same as get_user_pages_remote(), just with a
1113 * less-flexible calling convention where we assume that the task
1114 * and mm being operated on are the current task's and don't allow
1115 * passing of a locked parameter. We also obviously don't pass
1116 * FOLL_REMOTE in here.
1118 long get_user_pages(unsigned long start, unsigned long nr_pages,
1119 unsigned int gup_flags, struct page **pages,
1120 struct vm_area_struct **vmas)
1122 return __get_user_pages_locked(current, current->mm, start, nr_pages,
1123 pages, vmas, NULL, false,
1124 gup_flags | FOLL_TOUCH);
1126 EXPORT_SYMBOL(get_user_pages);
1128 #ifdef CONFIG_FS_DAX
1130 * This is the same as get_user_pages() in that it assumes we are
1131 * operating on the current task's mm, but it goes further to validate
1132 * that the vmas associated with the address range are suitable for
1133 * longterm elevated page reference counts. For example, filesystem-dax
1134 * mappings are subject to the lifetime enforced by the filesystem and
1135 * we need guarantees that longterm users like RDMA and V4L2 only
1136 * establish mappings that have a kernel enforced revocation mechanism.
1138 * "longterm" == userspace controlled elevated page count lifetime.
1139 * Contrast this to iov_iter_get_pages() usages which are transient.
1141 long get_user_pages_longterm(unsigned long start, unsigned long nr_pages,
1142 unsigned int gup_flags, struct page **pages,
1143 struct vm_area_struct **vmas_arg)
1145 struct vm_area_struct **vmas = vmas_arg;
1146 struct vm_area_struct *vma_prev = NULL;
1153 vmas = kcalloc(nr_pages, sizeof(struct vm_area_struct *),
1159 rc = get_user_pages(start, nr_pages, gup_flags, pages, vmas);
1161 for (i = 0; i < rc; i++) {
1162 struct vm_area_struct *vma = vmas[i];
1164 if (vma == vma_prev)
1169 if (vma_is_fsdax(vma))
1174 * Either get_user_pages() failed, or the vma validation
1175 * succeeded, in either case we don't need to put_page() before
1181 for (i = 0; i < rc; i++)
1185 if (vmas != vmas_arg)
1189 EXPORT_SYMBOL(get_user_pages_longterm);
1190 #endif /* CONFIG_FS_DAX */
1193 * populate_vma_page_range() - populate a range of pages in the vma.
1195 * @start: start address
1199 * This takes care of mlocking the pages too if VM_LOCKED is set.
1201 * return 0 on success, negative error code on error.
1203 * vma->vm_mm->mmap_sem must be held.
1205 * If @nonblocking is NULL, it may be held for read or write and will
1208 * If @nonblocking is non-NULL, it must held for read only and may be
1209 * released. If it's released, *@nonblocking will be set to 0.
1211 long populate_vma_page_range(struct vm_area_struct *vma,
1212 unsigned long start, unsigned long end, int *nonblocking)
1214 struct mm_struct *mm = vma->vm_mm;
1215 unsigned long nr_pages = (end - start) / PAGE_SIZE;
1218 VM_BUG_ON(start & ~PAGE_MASK);
1219 VM_BUG_ON(end & ~PAGE_MASK);
1220 VM_BUG_ON_VMA(start < vma->vm_start, vma);
1221 VM_BUG_ON_VMA(end > vma->vm_end, vma);
1222 VM_BUG_ON_MM(!rwsem_is_locked(&mm->mmap_sem), mm);
1224 gup_flags = FOLL_TOUCH | FOLL_POPULATE | FOLL_MLOCK;
1225 if (vma->vm_flags & VM_LOCKONFAULT)
1226 gup_flags &= ~FOLL_POPULATE;
1228 * We want to touch writable mappings with a write fault in order
1229 * to break COW, except for shared mappings because these don't COW
1230 * and we would not want to dirty them for nothing.
1232 if ((vma->vm_flags & (VM_WRITE | VM_SHARED)) == VM_WRITE)
1233 gup_flags |= FOLL_WRITE;
1236 * We want mlock to succeed for regions that have any permissions
1237 * other than PROT_NONE.
1239 if (vma->vm_flags & (VM_READ | VM_WRITE | VM_EXEC))
1240 gup_flags |= FOLL_FORCE;
1243 * We made sure addr is within a VMA, so the following will
1244 * not result in a stack expansion that recurses back here.
1246 return __get_user_pages(current, mm, start, nr_pages, gup_flags,
1247 NULL, NULL, nonblocking);
1251 * __mm_populate - populate and/or mlock pages within a range of address space.
1253 * This is used to implement mlock() and the MAP_POPULATE / MAP_LOCKED mmap
1254 * flags. VMAs must be already marked with the desired vm_flags, and
1255 * mmap_sem must not be held.
1257 int __mm_populate(unsigned long start, unsigned long len, int ignore_errors)
1259 struct mm_struct *mm = current->mm;
1260 unsigned long end, nstart, nend;
1261 struct vm_area_struct *vma = NULL;
1267 for (nstart = start; nstart < end; nstart = nend) {
1269 * We want to fault in pages for [nstart; end) address range.
1270 * Find first corresponding VMA.
1274 down_read(&mm->mmap_sem);
1275 vma = find_vma(mm, nstart);
1276 } else if (nstart >= vma->vm_end)
1278 if (!vma || vma->vm_start >= end)
1281 * Set [nstart; nend) to intersection of desired address
1282 * range with the first VMA. Also, skip undesirable VMA types.
1284 nend = min(end, vma->vm_end);
1285 if (vma->vm_flags & (VM_IO | VM_PFNMAP))
1287 if (nstart < vma->vm_start)
1288 nstart = vma->vm_start;
1290 * Now fault in a range of pages. populate_vma_page_range()
1291 * double checks the vma flags, so that it won't mlock pages
1292 * if the vma was already munlocked.
1294 ret = populate_vma_page_range(vma, nstart, nend, &locked);
1296 if (ignore_errors) {
1298 continue; /* continue at next VMA */
1302 nend = nstart + ret * PAGE_SIZE;
1306 up_read(&mm->mmap_sem);
1307 return ret; /* 0 or negative error code */
1311 * get_dump_page() - pin user page in memory while writing it to core dump
1312 * @addr: user address
1314 * Returns struct page pointer of user page pinned for dump,
1315 * to be freed afterwards by put_page().
1317 * Returns NULL on any kind of failure - a hole must then be inserted into
1318 * the corefile, to preserve alignment with its headers; and also returns
1319 * NULL wherever the ZERO_PAGE, or an anonymous pte_none, has been found -
1320 * allowing a hole to be left in the corefile to save diskspace.
1322 * Called without mmap_sem, but after all other threads have been killed.
1324 #ifdef CONFIG_ELF_CORE
1325 struct page *get_dump_page(unsigned long addr)
1327 struct vm_area_struct *vma;
1330 if (__get_user_pages(current, current->mm, addr, 1,
1331 FOLL_FORCE | FOLL_DUMP | FOLL_GET, &page, &vma,
1334 flush_cache_page(vma, addr, page_to_pfn(page));
1337 #endif /* CONFIG_ELF_CORE */
1342 * get_user_pages_fast attempts to pin user pages by walking the page
1343 * tables directly and avoids taking locks. Thus the walker needs to be
1344 * protected from page table pages being freed from under it, and should
1345 * block any THP splits.
1347 * One way to achieve this is to have the walker disable interrupts, and
1348 * rely on IPIs from the TLB flushing code blocking before the page table
1349 * pages are freed. This is unsuitable for architectures that do not need
1350 * to broadcast an IPI when invalidating TLBs.
1352 * Another way to achieve this is to batch up page table containing pages
1353 * belonging to more than one mm_user, then rcu_sched a callback to free those
1354 * pages. Disabling interrupts will allow the fast_gup walker to both block
1355 * the rcu_sched callback, and an IPI that we broadcast for splitting THPs
1356 * (which is a relatively rare event). The code below adopts this strategy.
1358 * Before activating this code, please be aware that the following assumptions
1359 * are currently made:
1361 * *) Either HAVE_RCU_TABLE_FREE is enabled, and tlb_remove_table() is used to
1362 * free pages containing page tables or TLB flushing requires IPI broadcast.
1364 * *) ptes can be read atomically by the architecture.
1366 * *) access_ok is sufficient to validate userspace address ranges.
1368 * The last two assumptions can be relaxed by the addition of helper functions.
1370 * This code is based heavily on the PowerPC implementation by Nick Piggin.
1372 #ifdef CONFIG_HAVE_GENERIC_GUP
1376 * We assume that the PTE can be read atomically. If this is not the case for
1377 * your architecture, please provide the helper.
1379 static inline pte_t gup_get_pte(pte_t *ptep)
1381 return READ_ONCE(*ptep);
1385 static void __maybe_unused undo_dev_pagemap(int *nr, int nr_start,
1386 struct page **pages)
1388 while ((*nr) - nr_start) {
1389 struct page *page = pages[--(*nr)];
1391 ClearPageReferenced(page);
1397 * Return the compund head page with ref appropriately incremented,
1398 * or NULL if that failed.
1400 static inline struct page *try_get_compound_head(struct page *page, int refs)
1402 struct page *head = compound_head(page);
1403 if (WARN_ON_ONCE(page_ref_count(head) < 0))
1405 if (unlikely(!page_cache_add_speculative(head, refs)))
1410 #ifdef __HAVE_ARCH_PTE_SPECIAL
1411 static int gup_pte_range(pmd_t pmd, unsigned long addr, unsigned long end,
1412 int write, struct page **pages, int *nr)
1414 struct dev_pagemap *pgmap = NULL;
1415 int nr_start = *nr, ret = 0;
1418 ptem = ptep = pte_offset_map(&pmd, addr);
1420 pte_t pte = gup_get_pte(ptep);
1421 struct page *head, *page;
1424 * Similar to the PMD case below, NUMA hinting must take slow
1425 * path using the pte_protnone check.
1427 if (pte_protnone(pte))
1430 if (!pte_access_permitted(pte, write))
1433 if (pte_devmap(pte)) {
1434 pgmap = get_dev_pagemap(pte_pfn(pte), pgmap);
1435 if (unlikely(!pgmap)) {
1436 undo_dev_pagemap(nr, nr_start, pages);
1439 } else if (pte_special(pte))
1442 VM_BUG_ON(!pfn_valid(pte_pfn(pte)));
1443 page = pte_page(pte);
1445 head = try_get_compound_head(page, 1);
1449 if (unlikely(pte_val(pte) != pte_val(*ptep))) {
1454 VM_BUG_ON_PAGE(compound_head(page) != head, page);
1456 put_dev_pagemap(pgmap);
1457 SetPageReferenced(page);
1461 } while (ptep++, addr += PAGE_SIZE, addr != end);
1472 * If we can't determine whether or not a pte is special, then fail immediately
1473 * for ptes. Note, we can still pin HugeTLB and THP as these are guaranteed not
1476 * For a futex to be placed on a THP tail page, get_futex_key requires a
1477 * __get_user_pages_fast implementation that can pin pages. Thus it's still
1478 * useful to have gup_huge_pmd even if we can't operate on ptes.
1480 static int gup_pte_range(pmd_t pmd, unsigned long addr, unsigned long end,
1481 int write, struct page **pages, int *nr)
1485 #endif /* __HAVE_ARCH_PTE_SPECIAL */
1487 #if defined(__HAVE_ARCH_PTE_DEVMAP) && defined(CONFIG_TRANSPARENT_HUGEPAGE)
1488 static int __gup_device_huge(unsigned long pfn, unsigned long addr,
1489 unsigned long end, struct page **pages, int *nr)
1492 struct dev_pagemap *pgmap = NULL;
1495 struct page *page = pfn_to_page(pfn);
1497 pgmap = get_dev_pagemap(pfn, pgmap);
1498 if (unlikely(!pgmap)) {
1499 undo_dev_pagemap(nr, nr_start, pages);
1502 SetPageReferenced(page);
1505 put_dev_pagemap(pgmap);
1508 } while (addr += PAGE_SIZE, addr != end);
1512 static int __gup_device_huge_pmd(pmd_t orig, pmd_t *pmdp, unsigned long addr,
1513 unsigned long end, struct page **pages, int *nr)
1515 unsigned long fault_pfn;
1518 fault_pfn = pmd_pfn(orig) + ((addr & ~PMD_MASK) >> PAGE_SHIFT);
1519 if (!__gup_device_huge(fault_pfn, addr, end, pages, nr))
1522 if (unlikely(pmd_val(orig) != pmd_val(*pmdp))) {
1523 undo_dev_pagemap(nr, nr_start, pages);
1529 static int __gup_device_huge_pud(pud_t orig, pud_t *pudp, unsigned long addr,
1530 unsigned long end, struct page **pages, int *nr)
1532 unsigned long fault_pfn;
1535 fault_pfn = pud_pfn(orig) + ((addr & ~PUD_MASK) >> PAGE_SHIFT);
1536 if (!__gup_device_huge(fault_pfn, addr, end, pages, nr))
1539 if (unlikely(pud_val(orig) != pud_val(*pudp))) {
1540 undo_dev_pagemap(nr, nr_start, pages);
1546 static int __gup_device_huge_pmd(pmd_t orig, pmd_t *pmdp, unsigned long addr,
1547 unsigned long end, struct page **pages, int *nr)
1553 static int __gup_device_huge_pud(pud_t pud, pud_t *pudp, unsigned long addr,
1554 unsigned long end, struct page **pages, int *nr)
1561 static int gup_huge_pmd(pmd_t orig, pmd_t *pmdp, unsigned long addr,
1562 unsigned long end, int write, struct page **pages, int *nr)
1564 struct page *head, *page;
1567 if (!pmd_access_permitted(orig, write))
1570 if (pmd_devmap(orig))
1571 return __gup_device_huge_pmd(orig, pmdp, addr, end, pages, nr);
1574 page = pmd_page(orig) + ((addr & ~PMD_MASK) >> PAGE_SHIFT);
1580 } while (addr += PAGE_SIZE, addr != end);
1582 head = try_get_compound_head(pmd_page(orig), refs);
1588 if (unlikely(pmd_val(orig) != pmd_val(*pmdp))) {
1595 SetPageReferenced(head);
1599 static int gup_huge_pud(pud_t orig, pud_t *pudp, unsigned long addr,
1600 unsigned long end, int write, struct page **pages, int *nr)
1602 struct page *head, *page;
1605 if (!pud_access_permitted(orig, write))
1608 if (pud_devmap(orig))
1609 return __gup_device_huge_pud(orig, pudp, addr, end, pages, nr);
1612 page = pud_page(orig) + ((addr & ~PUD_MASK) >> PAGE_SHIFT);
1618 } while (addr += PAGE_SIZE, addr != end);
1620 head = try_get_compound_head(pud_page(orig), refs);
1626 if (unlikely(pud_val(orig) != pud_val(*pudp))) {
1633 SetPageReferenced(head);
1637 static int gup_huge_pgd(pgd_t orig, pgd_t *pgdp, unsigned long addr,
1638 unsigned long end, int write,
1639 struct page **pages, int *nr)
1642 struct page *head, *page;
1644 if (!pgd_access_permitted(orig, write))
1647 BUILD_BUG_ON(pgd_devmap(orig));
1649 page = pgd_page(orig) + ((addr & ~PGDIR_MASK) >> PAGE_SHIFT);
1655 } while (addr += PAGE_SIZE, addr != end);
1657 head = try_get_compound_head(pgd_page(orig), refs);
1663 if (unlikely(pgd_val(orig) != pgd_val(*pgdp))) {
1670 SetPageReferenced(head);
1674 static int gup_pmd_range(pud_t pud, unsigned long addr, unsigned long end,
1675 int write, struct page **pages, int *nr)
1680 pmdp = pmd_offset(&pud, addr);
1682 pmd_t pmd = READ_ONCE(*pmdp);
1684 next = pmd_addr_end(addr, end);
1685 if (!pmd_present(pmd))
1688 if (unlikely(pmd_trans_huge(pmd) || pmd_huge(pmd) ||
1691 * NUMA hinting faults need to be handled in the GUP
1692 * slowpath for accounting purposes and so that they
1693 * can be serialised against THP migration.
1695 if (pmd_protnone(pmd))
1698 if (!gup_huge_pmd(pmd, pmdp, addr, next, write,
1702 } else if (unlikely(is_hugepd(__hugepd(pmd_val(pmd))))) {
1704 * architecture have different format for hugetlbfs
1705 * pmd format and THP pmd format
1707 if (!gup_huge_pd(__hugepd(pmd_val(pmd)), addr,
1708 PMD_SHIFT, next, write, pages, nr))
1710 } else if (!gup_pte_range(pmd, addr, next, write, pages, nr))
1712 } while (pmdp++, addr = next, addr != end);
1717 static int gup_pud_range(p4d_t p4d, unsigned long addr, unsigned long end,
1718 int write, struct page **pages, int *nr)
1723 pudp = pud_offset(&p4d, addr);
1725 pud_t pud = READ_ONCE(*pudp);
1727 next = pud_addr_end(addr, end);
1730 if (unlikely(pud_huge(pud))) {
1731 if (!gup_huge_pud(pud, pudp, addr, next, write,
1734 } else if (unlikely(is_hugepd(__hugepd(pud_val(pud))))) {
1735 if (!gup_huge_pd(__hugepd(pud_val(pud)), addr,
1736 PUD_SHIFT, next, write, pages, nr))
1738 } else if (!gup_pmd_range(pud, addr, next, write, pages, nr))
1740 } while (pudp++, addr = next, addr != end);
1745 static int gup_p4d_range(pgd_t pgd, unsigned long addr, unsigned long end,
1746 int write, struct page **pages, int *nr)
1751 p4dp = p4d_offset(&pgd, addr);
1753 p4d_t p4d = READ_ONCE(*p4dp);
1755 next = p4d_addr_end(addr, end);
1758 BUILD_BUG_ON(p4d_huge(p4d));
1759 if (unlikely(is_hugepd(__hugepd(p4d_val(p4d))))) {
1760 if (!gup_huge_pd(__hugepd(p4d_val(p4d)), addr,
1761 P4D_SHIFT, next, write, pages, nr))
1763 } else if (!gup_pud_range(p4d, addr, next, write, pages, nr))
1765 } while (p4dp++, addr = next, addr != end);
1770 static void gup_pgd_range(unsigned long addr, unsigned long end,
1771 int write, struct page **pages, int *nr)
1776 pgdp = pgd_offset(current->mm, addr);
1778 pgd_t pgd = READ_ONCE(*pgdp);
1780 next = pgd_addr_end(addr, end);
1783 if (unlikely(pgd_huge(pgd))) {
1784 if (!gup_huge_pgd(pgd, pgdp, addr, next, write,
1787 } else if (unlikely(is_hugepd(__hugepd(pgd_val(pgd))))) {
1788 if (!gup_huge_pd(__hugepd(pgd_val(pgd)), addr,
1789 PGDIR_SHIFT, next, write, pages, nr))
1791 } else if (!gup_p4d_range(pgd, addr, next, write, pages, nr))
1793 } while (pgdp++, addr = next, addr != end);
1796 #ifndef gup_fast_permitted
1798 * Check if it's allowed to use __get_user_pages_fast() for the range, or
1799 * we need to fall back to the slow version:
1801 bool gup_fast_permitted(unsigned long start, int nr_pages, int write)
1803 unsigned long len, end;
1805 len = (unsigned long) nr_pages << PAGE_SHIFT;
1807 return end >= start;
1812 * Like get_user_pages_fast() except it's IRQ-safe in that it won't fall back to
1813 * the regular GUP. It will only return non-negative values.
1815 * Careful, careful! COW breaking can go either way, so a non-write
1816 * access can get ambiguous page results. If you call this function without
1817 * 'write' set, you'd better be sure that you're ok with that ambiguity.
1819 int __get_user_pages_fast(unsigned long start, int nr_pages, int write,
1820 struct page **pages)
1822 unsigned long addr, len, end;
1823 unsigned long flags;
1828 len = (unsigned long) nr_pages << PAGE_SHIFT;
1831 if (unlikely(!access_ok(write ? VERIFY_WRITE : VERIFY_READ,
1832 (void __user *)start, len)))
1836 * Disable interrupts. We use the nested form as we can already have
1837 * interrupts disabled by get_futex_key.
1839 * With interrupts disabled, we block page table pages from being
1840 * freed from under us. See mmu_gather_tlb in asm-generic/tlb.h
1843 * We do not adopt an rcu_read_lock(.) here as we also want to
1844 * block IPIs that come from THPs splitting.
1846 * NOTE! We allow read-only gup_fast() here, but you'd better be
1847 * careful about possible COW pages. You'll get _a_ COW page, but
1848 * not necessarily the one you intended to get depending on what
1849 * COW event happens after this. COW may break the page copy in a
1853 if (gup_fast_permitted(start, nr_pages, write)) {
1854 local_irq_save(flags);
1855 gup_pgd_range(addr, end, write, pages, &nr);
1856 local_irq_restore(flags);
1863 * get_user_pages_fast() - pin user pages in memory
1864 * @start: starting user address
1865 * @nr_pages: number of pages from start to pin
1866 * @write: whether pages will be written to
1867 * @pages: array that receives pointers to the pages pinned.
1868 * Should be at least nr_pages long.
1870 * Attempt to pin user pages in memory without taking mm->mmap_sem.
1871 * If not successful, it will fall back to taking the lock and
1872 * calling get_user_pages().
1874 * Returns number of pages pinned. This may be fewer than the number
1875 * requested. If nr_pages is 0 or negative, returns 0. If no pages
1876 * were pinned, returns -errno.
1878 int get_user_pages_fast(unsigned long start, int nr_pages, int write,
1879 struct page **pages)
1881 unsigned long addr, len, end;
1882 int nr = 0, ret = 0;
1886 len = (unsigned long) nr_pages << PAGE_SHIFT;
1892 if (unlikely(!access_ok(write ? VERIFY_WRITE : VERIFY_READ,
1893 (void __user *)start, len)))
1897 * The FAST_GUP case requires FOLL_WRITE even for pure reads,
1898 * because get_user_pages() may need to cause an early COW in
1899 * order to avoid confusing the normal COW routines. So only
1900 * targets that are already writable are safe to do by just
1901 * looking at the page tables.
1903 if (gup_fast_permitted(start, nr_pages, write)) {
1904 local_irq_disable();
1905 gup_pgd_range(addr, end, 1, pages, &nr);
1910 if (nr < nr_pages) {
1911 /* Try to get the remaining pages with get_user_pages */
1912 start += nr << PAGE_SHIFT;
1915 ret = get_user_pages_unlocked(start, nr_pages - nr, pages,
1916 write ? FOLL_WRITE : 0);
1918 /* Have to be a bit careful with return values */
1930 #endif /* CONFIG_HAVE_GENERIC_GUP */