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.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 can write to even unwritable pte's, but only
65 * 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) ||
70 ((flags & FOLL_FORCE) && (flags & FOLL_COW) && pte_dirty(pte));
73 static struct page *follow_page_pte(struct vm_area_struct *vma,
74 unsigned long address, pmd_t *pmd, unsigned int flags)
76 struct mm_struct *mm = vma->vm_mm;
77 struct dev_pagemap *pgmap = NULL;
83 if (unlikely(pmd_bad(*pmd)))
84 return no_page_table(vma, flags);
86 ptep = pte_offset_map_lock(mm, pmd, address, &ptl);
88 if (!pte_present(pte)) {
91 * KSM's break_ksm() relies upon recognizing a ksm page
92 * even while it is being migrated, so for that case we
93 * need migration_entry_wait().
95 if (likely(!(flags & FOLL_MIGRATION)))
99 entry = pte_to_swp_entry(pte);
100 if (!is_migration_entry(entry))
102 pte_unmap_unlock(ptep, ptl);
103 migration_entry_wait(mm, pmd, address);
106 if ((flags & FOLL_NUMA) && pte_protnone(pte))
108 if ((flags & FOLL_WRITE) && !can_follow_write_pte(pte, flags)) {
109 pte_unmap_unlock(ptep, ptl);
113 page = vm_normal_page(vma, address, pte);
114 if (!page && pte_devmap(pte) && (flags & FOLL_GET)) {
116 * Only return device mapping pages in the FOLL_GET case since
117 * they are only valid while holding the pgmap reference.
119 pgmap = get_dev_pagemap(pte_pfn(pte), NULL);
121 page = pte_page(pte);
124 } else if (unlikely(!page)) {
125 if (flags & FOLL_DUMP) {
126 /* Avoid special (like zero) pages in core dumps */
127 page = ERR_PTR(-EFAULT);
131 if (is_zero_pfn(pte_pfn(pte))) {
132 page = pte_page(pte);
136 ret = follow_pfn_pte(vma, address, ptep, flags);
142 if (flags & FOLL_SPLIT && PageTransCompound(page)) {
145 pte_unmap_unlock(ptep, ptl);
147 ret = split_huge_page(page);
155 if (flags & FOLL_GET) {
156 if (unlikely(!try_get_page(page))) {
157 page = ERR_PTR(-ENOMEM);
161 /* drop the pgmap reference now that we hold the page */
163 put_dev_pagemap(pgmap);
167 if (flags & FOLL_TOUCH) {
168 if ((flags & FOLL_WRITE) &&
169 !pte_dirty(pte) && !PageDirty(page))
170 set_page_dirty(page);
172 * pte_mkyoung() would be more correct here, but atomic care
173 * is needed to avoid losing the dirty bit: it is easier to use
174 * mark_page_accessed().
176 mark_page_accessed(page);
178 if ((flags & FOLL_MLOCK) && (vma->vm_flags & VM_LOCKED)) {
179 /* Do not mlock pte-mapped THP */
180 if (PageTransCompound(page))
184 * The preliminary mapping check is mainly to avoid the
185 * pointless overhead of lock_page on the ZERO_PAGE
186 * which might bounce very badly if there is contention.
188 * If the page is already locked, we don't need to
189 * handle it now - vmscan will handle it later if and
190 * when it attempts to reclaim the page.
192 if (page->mapping && trylock_page(page)) {
193 lru_add_drain(); /* push cached pages to LRU */
195 * Because we lock page here, and migration is
196 * blocked by the pte's page reference, and we
197 * know the page is still mapped, we don't even
198 * need to check for file-cache page truncation.
200 mlock_vma_page(page);
205 pte_unmap_unlock(ptep, ptl);
208 pte_unmap_unlock(ptep, ptl);
211 return no_page_table(vma, flags);
215 * follow_page_mask - look up a page descriptor from a user-virtual address
216 * @vma: vm_area_struct mapping @address
217 * @address: virtual address to look up
218 * @flags: flags modifying lookup behaviour
219 * @page_mask: on output, *page_mask is set according to the size of the page
221 * @flags can have FOLL_ flags set, defined in <linux/mm.h>
223 * Returns the mapped (struct page *), %NULL if no mapping exists, or
224 * an error pointer if there is a mapping to something not represented
225 * by a page descriptor (see also vm_normal_page()).
227 struct page *follow_page_mask(struct vm_area_struct *vma,
228 unsigned long address, unsigned int flags,
229 unsigned int *page_mask)
236 struct mm_struct *mm = vma->vm_mm;
240 page = follow_huge_addr(mm, address, flags & FOLL_WRITE);
242 BUG_ON(flags & FOLL_GET);
246 pgd = pgd_offset(mm, address);
247 if (pgd_none(*pgd) || unlikely(pgd_bad(*pgd)))
248 return no_page_table(vma, flags);
250 pud = pud_offset(pgd, address);
252 return no_page_table(vma, flags);
253 if (pud_huge(*pud) && vma->vm_flags & VM_HUGETLB) {
254 page = follow_huge_pud(mm, address, pud, flags);
257 return no_page_table(vma, flags);
259 if (unlikely(pud_bad(*pud)))
260 return no_page_table(vma, flags);
262 pmd = pmd_offset(pud, address);
264 return no_page_table(vma, flags);
265 if (pmd_huge(*pmd) && vma->vm_flags & VM_HUGETLB) {
266 page = follow_huge_pmd(mm, address, pmd, flags);
269 return no_page_table(vma, flags);
271 if ((flags & FOLL_NUMA) && pmd_protnone(*pmd))
272 return no_page_table(vma, flags);
273 if (pmd_devmap(*pmd)) {
274 ptl = pmd_lock(mm, pmd);
275 page = follow_devmap_pmd(vma, address, pmd, flags);
280 if (likely(!pmd_trans_huge(*pmd)))
281 return follow_page_pte(vma, address, pmd, flags);
283 ptl = pmd_lock(mm, pmd);
284 if (unlikely(!pmd_trans_huge(*pmd))) {
286 return follow_page_pte(vma, address, pmd, flags);
288 if (flags & FOLL_SPLIT) {
290 page = pmd_page(*pmd);
291 if (is_huge_zero_page(page)) {
294 split_huge_pmd(vma, pmd, address);
295 if (pmd_trans_unstable(pmd))
298 if (unlikely(!try_get_page(page))) {
300 return ERR_PTR(-ENOMEM);
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);
315 page = follow_trans_huge_pmd(vma, address, pmd, flags);
317 *page_mask = HPAGE_PMD_NR - 1;
321 static int get_gate_page(struct mm_struct *mm, unsigned long address,
322 unsigned int gup_flags, struct vm_area_struct **vma,
331 /* user gate pages are read-only */
332 if (gup_flags & FOLL_WRITE)
334 if (address > TASK_SIZE)
335 pgd = pgd_offset_k(address);
337 pgd = pgd_offset_gate(mm, address);
338 BUG_ON(pgd_none(*pgd));
339 pud = pud_offset(pgd, address);
340 BUG_ON(pud_none(*pud));
341 pmd = pmd_offset(pud, address);
344 VM_BUG_ON(pmd_trans_huge(*pmd));
345 pte = pte_offset_map(pmd, address);
348 *vma = get_gate_vma(mm);
351 *page = vm_normal_page(*vma, address, *pte);
353 if ((gup_flags & FOLL_DUMP) || !is_zero_pfn(pte_pfn(*pte)))
355 *page = pte_page(*pte);
357 if (unlikely(!try_get_page(*page))) {
369 * mmap_sem must be held on entry. If @nonblocking != NULL and
370 * *@flags does not include FOLL_NOWAIT, the mmap_sem may be released.
371 * If it is, *@nonblocking will be set to 0 and -EBUSY returned.
373 static int faultin_page(struct task_struct *tsk, struct vm_area_struct *vma,
374 unsigned long address, unsigned int *flags, int *nonblocking)
376 unsigned int fault_flags = 0;
379 /* mlock all present pages, but do not fault in new pages */
380 if ((*flags & (FOLL_POPULATE | FOLL_MLOCK)) == FOLL_MLOCK)
382 if (*flags & FOLL_WRITE)
383 fault_flags |= FAULT_FLAG_WRITE;
384 if (*flags & FOLL_REMOTE)
385 fault_flags |= FAULT_FLAG_REMOTE;
387 fault_flags |= FAULT_FLAG_ALLOW_RETRY;
388 if (*flags & FOLL_NOWAIT)
389 fault_flags |= FAULT_FLAG_ALLOW_RETRY | FAULT_FLAG_RETRY_NOWAIT;
390 if (*flags & FOLL_TRIED) {
391 VM_WARN_ON_ONCE(fault_flags & FAULT_FLAG_ALLOW_RETRY);
392 fault_flags |= FAULT_FLAG_TRIED;
395 ret = handle_mm_fault(vma, address, fault_flags);
396 if (ret & VM_FAULT_ERROR) {
397 if (ret & VM_FAULT_OOM)
399 if (ret & (VM_FAULT_HWPOISON | VM_FAULT_HWPOISON_LARGE))
400 return *flags & FOLL_HWPOISON ? -EHWPOISON : -EFAULT;
401 if (ret & (VM_FAULT_SIGBUS | VM_FAULT_SIGSEGV))
407 if (ret & VM_FAULT_MAJOR)
413 if (ret & VM_FAULT_RETRY) {
420 * The VM_FAULT_WRITE bit tells us that do_wp_page has broken COW when
421 * necessary, even if maybe_mkwrite decided not to set pte_write. We
422 * can thus safely do subsequent page lookups as if they were reads.
423 * But only do so when looping for pte_write is futile: in some cases
424 * userspace may also be wanting to write to the gotten user page,
425 * which a read fault here might prevent (a readonly page might get
426 * reCOWed by userspace write).
428 if ((ret & VM_FAULT_WRITE) && !(vma->vm_flags & VM_WRITE))
433 static int check_vma_flags(struct vm_area_struct *vma, unsigned long gup_flags)
435 vm_flags_t vm_flags = vma->vm_flags;
436 int write = (gup_flags & FOLL_WRITE);
437 int foreign = (gup_flags & FOLL_REMOTE);
439 if (vm_flags & (VM_IO | VM_PFNMAP))
442 if (gup_flags & FOLL_ANON && !vma_is_anonymous(vma))
446 if (!(vm_flags & VM_WRITE)) {
447 if (!(gup_flags & FOLL_FORCE))
450 * We used to let the write,force case do COW in a
451 * VM_MAYWRITE VM_SHARED !VM_WRITE vma, so ptrace could
452 * set a breakpoint in a read-only mapping of an
453 * executable, without corrupting the file (yet only
454 * when that file had been opened for writing!).
455 * Anon pages in shared mappings are surprising: now
458 if (!is_cow_mapping(vm_flags))
461 } else if (!(vm_flags & VM_READ)) {
462 if (!(gup_flags & FOLL_FORCE))
465 * Is there actually any vma we can reach here which does not
466 * have VM_MAYREAD set?
468 if (!(vm_flags & VM_MAYREAD))
472 * gups are always data accesses, not instruction
473 * fetches, so execute=false here
475 if (!arch_vma_access_permitted(vma, write, false, foreign))
481 * __get_user_pages() - pin user pages in memory
482 * @tsk: task_struct of target task
483 * @mm: mm_struct of target mm
484 * @start: starting user address
485 * @nr_pages: number of pages from start to pin
486 * @gup_flags: flags modifying pin behaviour
487 * @pages: array that receives pointers to the pages pinned.
488 * Should be at least nr_pages long. Or NULL, if caller
489 * only intends to ensure the pages are faulted in.
490 * @vmas: array of pointers to vmas corresponding to each page.
491 * Or NULL if the caller does not require them.
492 * @nonblocking: whether waiting for disk IO or mmap_sem contention
494 * Returns number of pages pinned. This may be fewer than the number
495 * requested. If nr_pages is 0 or negative, returns 0. If no pages
496 * were pinned, returns -errno. Each page returned must be released
497 * with a put_page() call when it is finished with. vmas will only
498 * remain valid while mmap_sem is held.
500 * Must be called with mmap_sem held. It may be released. See below.
502 * __get_user_pages walks a process's page tables and takes a reference to
503 * each struct page that each user address corresponds to at a given
504 * instant. That is, it takes the page that would be accessed if a user
505 * thread accesses the given user virtual address at that instant.
507 * This does not guarantee that the page exists in the user mappings when
508 * __get_user_pages returns, and there may even be a completely different
509 * page there in some cases (eg. if mmapped pagecache has been invalidated
510 * and subsequently re faulted). However it does guarantee that the page
511 * won't be freed completely. And mostly callers simply care that the page
512 * contains data that was valid *at some point in time*. Typically, an IO
513 * or similar operation cannot guarantee anything stronger anyway because
514 * locks can't be held over the syscall boundary.
516 * If @gup_flags & FOLL_WRITE == 0, the page must not be written to. If
517 * the page is written to, set_page_dirty (or set_page_dirty_lock, as
518 * appropriate) must be called after the page is finished with, and
519 * before put_page is called.
521 * If @nonblocking != NULL, __get_user_pages will not wait for disk IO
522 * or mmap_sem contention, and if waiting is needed to pin all pages,
523 * *@nonblocking will be set to 0. Further, if @gup_flags does not
524 * include FOLL_NOWAIT, the mmap_sem will be released via up_read() in
527 * A caller using such a combination of @nonblocking and @gup_flags
528 * must therefore hold the mmap_sem for reading only, and recognize
529 * when it's been released. Otherwise, it must be held for either
530 * reading or writing and will not be released.
532 * In most cases, get_user_pages or get_user_pages_fast should be used
533 * instead of __get_user_pages. __get_user_pages should be used only if
534 * you need some special @gup_flags.
536 static long __get_user_pages(struct task_struct *tsk, struct mm_struct *mm,
537 unsigned long start, unsigned long nr_pages,
538 unsigned int gup_flags, struct page **pages,
539 struct vm_area_struct **vmas, int *nonblocking)
542 unsigned int page_mask;
543 struct vm_area_struct *vma = NULL;
548 VM_BUG_ON(!!pages != !!(gup_flags & FOLL_GET));
551 * If FOLL_FORCE is set then do not force a full fault as the hinting
552 * fault information is unrelated to the reference behaviour of a task
553 * using the address space
555 if (!(gup_flags & FOLL_FORCE))
556 gup_flags |= FOLL_NUMA;
560 unsigned int foll_flags = gup_flags;
561 unsigned int page_increm;
563 /* first iteration or cross vma bound */
564 if (!vma || start >= vma->vm_end) {
565 vma = find_extend_vma(mm, start);
566 if (!vma && in_gate_area(mm, start)) {
568 ret = get_gate_page(mm, start & PAGE_MASK,
570 pages ? &pages[i] : NULL);
577 if (!vma || check_vma_flags(vma, gup_flags))
578 return i ? : -EFAULT;
579 if (is_vm_hugetlb_page(vma)) {
580 i = follow_hugetlb_page(mm, vma, pages, vmas,
581 &start, &nr_pages, i,
588 * If we have a pending SIGKILL, don't keep faulting pages and
589 * potentially allocating memory.
591 if (unlikely(fatal_signal_pending(current)))
592 return i ? i : -ERESTARTSYS;
594 page = follow_page_mask(vma, start, foll_flags, &page_mask);
597 ret = faultin_page(tsk, vma, start, &foll_flags,
612 } else if (PTR_ERR(page) == -EEXIST) {
614 * Proper page table entry exists, but no corresponding
618 } else if (IS_ERR(page)) {
619 return i ? i : PTR_ERR(page);
623 flush_anon_page(vma, page, start);
624 flush_dcache_page(page);
632 page_increm = 1 + (~(start >> PAGE_SHIFT) & page_mask);
633 if (page_increm > nr_pages)
634 page_increm = nr_pages;
636 start += page_increm * PAGE_SIZE;
637 nr_pages -= page_increm;
642 bool vma_permits_fault(struct vm_area_struct *vma, unsigned int fault_flags)
644 bool write = !!(fault_flags & FAULT_FLAG_WRITE);
645 bool foreign = !!(fault_flags & FAULT_FLAG_REMOTE);
646 vm_flags_t vm_flags = write ? VM_WRITE : VM_READ;
648 if (!(vm_flags & vma->vm_flags))
652 * The architecture might have a hardware protection
653 * mechanism other than read/write that can deny access.
655 * gup always represents data access, not instruction
656 * fetches, so execute=false here:
658 if (!arch_vma_access_permitted(vma, write, false, foreign))
665 * fixup_user_fault() - manually resolve a user page fault
666 * @tsk: the task_struct to use for page fault accounting, or
667 * NULL if faults are not to be recorded.
668 * @mm: mm_struct of target mm
669 * @address: user address
670 * @fault_flags:flags to pass down to handle_mm_fault()
671 * @unlocked: did we unlock the mmap_sem while retrying, maybe NULL if caller
672 * does not allow retry
674 * This is meant to be called in the specific scenario where for locking reasons
675 * we try to access user memory in atomic context (within a pagefault_disable()
676 * section), this returns -EFAULT, and we want to resolve the user fault before
679 * Typically this is meant to be used by the futex code.
681 * The main difference with get_user_pages() is that this function will
682 * unconditionally call handle_mm_fault() which will in turn perform all the
683 * necessary SW fixup of the dirty and young bits in the PTE, while
684 * get_user_pages() only guarantees to update these in the struct page.
686 * This is important for some architectures where those bits also gate the
687 * access permission to the page because they are maintained in software. On
688 * such architectures, gup() will not be enough to make a subsequent access
691 * This function will not return with an unlocked mmap_sem. So it has not the
692 * same semantics wrt the @mm->mmap_sem as does filemap_fault().
694 int fixup_user_fault(struct task_struct *tsk, struct mm_struct *mm,
695 unsigned long address, unsigned int fault_flags,
698 struct vm_area_struct *vma;
702 fault_flags |= FAULT_FLAG_ALLOW_RETRY;
705 vma = find_extend_vma(mm, address);
706 if (!vma || address < vma->vm_start)
709 if (!vma_permits_fault(vma, fault_flags))
712 ret = handle_mm_fault(vma, address, fault_flags);
713 major |= ret & VM_FAULT_MAJOR;
714 if (ret & VM_FAULT_ERROR) {
715 if (ret & VM_FAULT_OOM)
717 if (ret & (VM_FAULT_HWPOISON | VM_FAULT_HWPOISON_LARGE))
719 if (ret & (VM_FAULT_SIGBUS | VM_FAULT_SIGSEGV))
724 if (ret & VM_FAULT_RETRY) {
725 down_read(&mm->mmap_sem);
726 if (!(fault_flags & FAULT_FLAG_TRIED)) {
728 fault_flags &= ~FAULT_FLAG_ALLOW_RETRY;
729 fault_flags |= FAULT_FLAG_TRIED;
742 EXPORT_SYMBOL_GPL(fixup_user_fault);
744 static __always_inline long __get_user_pages_locked(struct task_struct *tsk,
745 struct mm_struct *mm,
747 unsigned long nr_pages,
749 struct vm_area_struct **vmas,
750 int *locked, bool notify_drop,
753 long ret, pages_done;
757 /* if VM_FAULT_RETRY can be returned, vmas become invalid */
759 /* check caller initialized locked */
760 BUG_ON(*locked != 1);
767 lock_dropped = false;
769 ret = __get_user_pages(tsk, mm, start, nr_pages, flags, pages,
772 /* VM_FAULT_RETRY couldn't trigger, bypass */
775 /* VM_FAULT_RETRY cannot return errors */
778 BUG_ON(ret >= nr_pages);
782 /* If it's a prefault don't insist harder */
792 /* VM_FAULT_RETRY didn't trigger */
797 /* VM_FAULT_RETRY triggered, so seek to the faulting offset */
799 start += ret << PAGE_SHIFT;
802 * Repeat on the address that fired VM_FAULT_RETRY
803 * without FAULT_FLAG_ALLOW_RETRY but with
808 down_read(&mm->mmap_sem);
809 ret = __get_user_pages(tsk, mm, start, 1, flags | FOLL_TRIED,
824 if (notify_drop && lock_dropped && *locked) {
826 * We must let the caller know we temporarily dropped the lock
827 * and so the critical section protected by it was lost.
829 up_read(&mm->mmap_sem);
836 * We can leverage the VM_FAULT_RETRY functionality in the page fault
837 * paths better by using either get_user_pages_locked() or
838 * get_user_pages_unlocked().
840 * get_user_pages_locked() is suitable to replace the form:
842 * down_read(&mm->mmap_sem);
844 * get_user_pages(tsk, mm, ..., pages, NULL);
845 * up_read(&mm->mmap_sem);
850 * down_read(&mm->mmap_sem);
852 * get_user_pages_locked(tsk, mm, ..., pages, &locked);
854 * up_read(&mm->mmap_sem);
856 long get_user_pages_locked(unsigned long start, unsigned long nr_pages,
857 unsigned int gup_flags, struct page **pages,
860 return __get_user_pages_locked(current, current->mm, start, nr_pages,
861 pages, NULL, locked, true,
862 gup_flags | FOLL_TOUCH);
864 EXPORT_SYMBOL(get_user_pages_locked);
867 * Same as get_user_pages_unlocked(...., FOLL_TOUCH) but it allows to
868 * pass additional gup_flags as last parameter (like FOLL_HWPOISON).
870 * NOTE: here FOLL_TOUCH is not set implicitly and must be set by the
871 * caller if required (just like with __get_user_pages). "FOLL_GET",
872 * "FOLL_WRITE" and "FOLL_FORCE" are set implicitly as needed
873 * according to the parameters "pages", "write", "force"
876 __always_inline long __get_user_pages_unlocked(struct task_struct *tsk, struct mm_struct *mm,
877 unsigned long start, unsigned long nr_pages,
878 struct page **pages, unsigned int gup_flags)
883 down_read(&mm->mmap_sem);
884 ret = __get_user_pages_locked(tsk, mm, start, nr_pages, pages, NULL,
885 &locked, false, gup_flags);
887 up_read(&mm->mmap_sem);
890 EXPORT_SYMBOL(__get_user_pages_unlocked);
893 * get_user_pages_unlocked() is suitable to replace the form:
895 * down_read(&mm->mmap_sem);
896 * get_user_pages(tsk, mm, ..., pages, NULL);
897 * up_read(&mm->mmap_sem);
901 * get_user_pages_unlocked(tsk, mm, ..., pages);
903 * It is functionally equivalent to get_user_pages_fast so
904 * get_user_pages_fast should be used instead, if the two parameters
905 * "tsk" and "mm" are respectively equal to current and current->mm,
906 * or if "force" shall be set to 1 (get_user_pages_fast misses the
907 * "force" parameter).
909 long get_user_pages_unlocked(unsigned long start, unsigned long nr_pages,
910 struct page **pages, unsigned int gup_flags)
912 return __get_user_pages_unlocked(current, current->mm, start, nr_pages,
913 pages, gup_flags | FOLL_TOUCH);
915 EXPORT_SYMBOL(get_user_pages_unlocked);
918 * get_user_pages_remote() - pin user pages in memory
919 * @tsk: the task_struct to use for page fault accounting, or
920 * NULL if faults are not to be recorded.
921 * @mm: mm_struct of target mm
922 * @start: starting user address
923 * @nr_pages: number of pages from start to pin
924 * @gup_flags: flags modifying lookup behaviour
925 * @pages: array that receives pointers to the pages pinned.
926 * Should be at least nr_pages long. Or NULL, if caller
927 * only intends to ensure the pages are faulted in.
928 * @vmas: array of pointers to vmas corresponding to each page.
929 * Or NULL if the caller does not require them.
931 * Returns number of pages pinned. This may be fewer than the number
932 * requested. If nr_pages is 0 or negative, returns 0. If no pages
933 * were pinned, returns -errno. Each page returned must be released
934 * with a put_page() call when it is finished with. vmas will only
935 * remain valid while mmap_sem is held.
937 * Must be called with mmap_sem held for read or write.
939 * get_user_pages walks a process's page tables and takes a reference to
940 * each struct page that each user address corresponds to at a given
941 * instant. That is, it takes the page that would be accessed if a user
942 * thread accesses the given user virtual address at that instant.
944 * This does not guarantee that the page exists in the user mappings when
945 * get_user_pages returns, and there may even be a completely different
946 * page there in some cases (eg. if mmapped pagecache has been invalidated
947 * and subsequently re faulted). However it does guarantee that the page
948 * won't be freed completely. And mostly callers simply care that the page
949 * contains data that was valid *at some point in time*. Typically, an IO
950 * or similar operation cannot guarantee anything stronger anyway because
951 * locks can't be held over the syscall boundary.
953 * If gup_flags & FOLL_WRITE == 0, the page must not be written to. If the page
954 * is written to, set_page_dirty (or set_page_dirty_lock, as appropriate) must
955 * be called after the page is finished with, and before put_page is called.
957 * get_user_pages is typically used for fewer-copy IO operations, to get a
958 * handle on the memory by some means other than accesses via the user virtual
959 * addresses. The pages may be submitted for DMA to devices or accessed via
960 * their kernel linear mapping (via the kmap APIs). Care should be taken to
961 * use the correct cache flushing APIs.
963 * See also get_user_pages_fast, for performance critical applications.
965 * get_user_pages should be phased out in favor of
966 * get_user_pages_locked|unlocked or get_user_pages_fast. Nothing
967 * should use get_user_pages because it cannot pass
968 * FAULT_FLAG_ALLOW_RETRY to handle_mm_fault.
970 long get_user_pages_remote(struct task_struct *tsk, struct mm_struct *mm,
971 unsigned long start, unsigned long nr_pages,
972 unsigned int gup_flags, struct page **pages,
973 struct vm_area_struct **vmas)
975 return __get_user_pages_locked(tsk, mm, start, nr_pages, pages, vmas,
977 gup_flags | FOLL_TOUCH | FOLL_REMOTE);
979 EXPORT_SYMBOL(get_user_pages_remote);
982 * This is the same as get_user_pages_remote(), just with a
983 * less-flexible calling convention where we assume that the task
984 * and mm being operated on are the current task's. We also
985 * obviously don't pass FOLL_REMOTE in here.
987 long get_user_pages(unsigned long start, unsigned long nr_pages,
988 unsigned int gup_flags, struct page **pages,
989 struct vm_area_struct **vmas)
991 return __get_user_pages_locked(current, current->mm, start, nr_pages,
992 pages, vmas, NULL, false,
993 gup_flags | FOLL_TOUCH);
995 EXPORT_SYMBOL(get_user_pages);
999 * This is the same as get_user_pages() in that it assumes we are
1000 * operating on the current task's mm, but it goes further to validate
1001 * that the vmas associated with the address range are suitable for
1002 * longterm elevated page reference counts. For example, filesystem-dax
1003 * mappings are subject to the lifetime enforced by the filesystem and
1004 * we need guarantees that longterm users like RDMA and V4L2 only
1005 * establish mappings that have a kernel enforced revocation mechanism.
1007 * "longterm" == userspace controlled elevated page count lifetime.
1008 * Contrast this to iov_iter_get_pages() usages which are transient.
1010 long get_user_pages_longterm(unsigned long start, unsigned long nr_pages,
1011 unsigned int gup_flags, struct page **pages,
1012 struct vm_area_struct **vmas_arg)
1014 struct vm_area_struct **vmas = vmas_arg;
1015 struct vm_area_struct *vma_prev = NULL;
1022 vmas = kcalloc(nr_pages, sizeof(struct vm_area_struct *),
1028 rc = get_user_pages(start, nr_pages, gup_flags, pages, vmas);
1030 for (i = 0; i < rc; i++) {
1031 struct vm_area_struct *vma = vmas[i];
1033 if (vma == vma_prev)
1038 if (vma_is_fsdax(vma))
1043 * Either get_user_pages() failed, or the vma validation
1044 * succeeded, in either case we don't need to put_page() before
1050 for (i = 0; i < rc; i++)
1054 if (vmas != vmas_arg)
1058 EXPORT_SYMBOL(get_user_pages_longterm);
1059 #endif /* CONFIG_FS_DAX */
1062 * populate_vma_page_range() - populate a range of pages in the vma.
1064 * @start: start address
1068 * This takes care of mlocking the pages too if VM_LOCKED is set.
1070 * return 0 on success, negative error code on error.
1072 * vma->vm_mm->mmap_sem must be held.
1074 * If @nonblocking is NULL, it may be held for read or write and will
1077 * If @nonblocking is non-NULL, it must held for read only and may be
1078 * released. If it's released, *@nonblocking will be set to 0.
1080 long populate_vma_page_range(struct vm_area_struct *vma,
1081 unsigned long start, unsigned long end, int *nonblocking)
1083 struct mm_struct *mm = vma->vm_mm;
1084 unsigned long nr_pages = (end - start) / PAGE_SIZE;
1087 VM_BUG_ON(start & ~PAGE_MASK);
1088 VM_BUG_ON(end & ~PAGE_MASK);
1089 VM_BUG_ON_VMA(start < vma->vm_start, vma);
1090 VM_BUG_ON_VMA(end > vma->vm_end, vma);
1091 VM_BUG_ON_MM(!rwsem_is_locked(&mm->mmap_sem), mm);
1093 gup_flags = FOLL_TOUCH | FOLL_POPULATE | FOLL_MLOCK;
1094 if (vma->vm_flags & VM_LOCKONFAULT)
1095 gup_flags &= ~FOLL_POPULATE;
1097 * We want to touch writable mappings with a write fault in order
1098 * to break COW, except for shared mappings because these don't COW
1099 * and we would not want to dirty them for nothing.
1101 if ((vma->vm_flags & (VM_WRITE | VM_SHARED)) == VM_WRITE)
1102 gup_flags |= FOLL_WRITE;
1105 * We want mlock to succeed for regions that have any permissions
1106 * other than PROT_NONE.
1108 if (vma->vm_flags & (VM_READ | VM_WRITE | VM_EXEC))
1109 gup_flags |= FOLL_FORCE;
1112 * We made sure addr is within a VMA, so the following will
1113 * not result in a stack expansion that recurses back here.
1115 return __get_user_pages(current, mm, start, nr_pages, gup_flags,
1116 NULL, NULL, nonblocking);
1120 * __mm_populate - populate and/or mlock pages within a range of address space.
1122 * This is used to implement mlock() and the MAP_POPULATE / MAP_LOCKED mmap
1123 * flags. VMAs must be already marked with the desired vm_flags, and
1124 * mmap_sem must not be held.
1126 int __mm_populate(unsigned long start, unsigned long len, int ignore_errors)
1128 struct mm_struct *mm = current->mm;
1129 unsigned long end, nstart, nend;
1130 struct vm_area_struct *vma = NULL;
1136 for (nstart = start; nstart < end; nstart = nend) {
1138 * We want to fault in pages for [nstart; end) address range.
1139 * Find first corresponding VMA.
1143 down_read(&mm->mmap_sem);
1144 vma = find_vma(mm, nstart);
1145 } else if (nstart >= vma->vm_end)
1147 if (!vma || vma->vm_start >= end)
1150 * Set [nstart; nend) to intersection of desired address
1151 * range with the first VMA. Also, skip undesirable VMA types.
1153 nend = min(end, vma->vm_end);
1154 if (vma->vm_flags & (VM_IO | VM_PFNMAP))
1156 if (nstart < vma->vm_start)
1157 nstart = vma->vm_start;
1159 * Now fault in a range of pages. populate_vma_page_range()
1160 * double checks the vma flags, so that it won't mlock pages
1161 * if the vma was already munlocked.
1163 ret = populate_vma_page_range(vma, nstart, nend, &locked);
1165 if (ignore_errors) {
1167 continue; /* continue at next VMA */
1171 nend = nstart + ret * PAGE_SIZE;
1175 up_read(&mm->mmap_sem);
1176 return ret; /* 0 or negative error code */
1180 * get_dump_page() - pin user page in memory while writing it to core dump
1181 * @addr: user address
1183 * Returns struct page pointer of user page pinned for dump,
1184 * to be freed afterwards by put_page().
1186 * Returns NULL on any kind of failure - a hole must then be inserted into
1187 * the corefile, to preserve alignment with its headers; and also returns
1188 * NULL wherever the ZERO_PAGE, or an anonymous pte_none, has been found -
1189 * allowing a hole to be left in the corefile to save diskspace.
1191 * Called without mmap_sem, but after all other threads have been killed.
1193 #ifdef CONFIG_ELF_CORE
1194 struct page *get_dump_page(unsigned long addr)
1196 struct vm_area_struct *vma;
1199 if (__get_user_pages(current, current->mm, addr, 1,
1200 FOLL_FORCE | FOLL_DUMP | FOLL_GET, &page, &vma,
1203 flush_cache_page(vma, addr, page_to_pfn(page));
1206 #endif /* CONFIG_ELF_CORE */
1209 * Generic RCU Fast GUP
1211 * get_user_pages_fast attempts to pin user pages by walking the page
1212 * tables directly and avoids taking locks. Thus the walker needs to be
1213 * protected from page table pages being freed from under it, and should
1214 * block any THP splits.
1216 * One way to achieve this is to have the walker disable interrupts, and
1217 * rely on IPIs from the TLB flushing code blocking before the page table
1218 * pages are freed. This is unsuitable for architectures that do not need
1219 * to broadcast an IPI when invalidating TLBs.
1221 * Another way to achieve this is to batch up page table containing pages
1222 * belonging to more than one mm_user, then rcu_sched a callback to free those
1223 * pages. Disabling interrupts will allow the fast_gup walker to both block
1224 * the rcu_sched callback, and an IPI that we broadcast for splitting THPs
1225 * (which is a relatively rare event). The code below adopts this strategy.
1227 * Before activating this code, please be aware that the following assumptions
1228 * are currently made:
1230 * *) HAVE_RCU_TABLE_FREE is enabled, and tlb_remove_table is used to free
1231 * pages containing page tables.
1233 * *) ptes can be read atomically by the architecture.
1235 * *) access_ok is sufficient to validate userspace address ranges.
1237 * The last two assumptions can be relaxed by the addition of helper functions.
1239 * This code is based heavily on the PowerPC implementation by Nick Piggin.
1241 #ifdef CONFIG_HAVE_GENERIC_RCU_GUP
1244 * Return the compund head page with ref appropriately incremented,
1245 * or NULL if that failed.
1247 static inline struct page *try_get_compound_head(struct page *page, int refs)
1249 struct page *head = compound_head(page);
1250 if (WARN_ON_ONCE(page_ref_count(head) < 0))
1252 if (unlikely(!page_cache_add_speculative(head, refs)))
1257 #ifdef __HAVE_ARCH_PTE_SPECIAL
1258 static int gup_pte_range(pmd_t pmd, unsigned long addr, unsigned long end,
1259 int write, struct page **pages, int *nr)
1264 ptem = ptep = pte_offset_map(&pmd, addr);
1267 * In the line below we are assuming that the pte can be read
1268 * atomically. If this is not the case for your architecture,
1269 * please wrap this in a helper function!
1271 * for an example see gup_get_pte in arch/x86/mm/gup.c
1273 pte_t pte = READ_ONCE(*ptep);
1274 struct page *head, *page;
1277 * Similar to the PMD case below, NUMA hinting must take slow
1278 * path using the pte_protnone check.
1280 if (!pte_present(pte) || pte_special(pte) ||
1281 pte_protnone(pte) || (write && !pte_write(pte)))
1284 if (!arch_pte_access_permitted(pte, write))
1287 VM_BUG_ON(!pfn_valid(pte_pfn(pte)));
1288 page = pte_page(pte);
1290 head = try_get_compound_head(page, 1);
1294 if (unlikely(pte_val(pte) != pte_val(*ptep))) {
1299 VM_BUG_ON_PAGE(compound_head(page) != head, page);
1303 } while (ptep++, addr += PAGE_SIZE, addr != end);
1314 * If we can't determine whether or not a pte is special, then fail immediately
1315 * for ptes. Note, we can still pin HugeTLB and THP as these are guaranteed not
1318 * For a futex to be placed on a THP tail page, get_futex_key requires a
1319 * __get_user_pages_fast implementation that can pin pages. Thus it's still
1320 * useful to have gup_huge_pmd even if we can't operate on ptes.
1322 static int gup_pte_range(pmd_t pmd, unsigned long addr, unsigned long end,
1323 int write, struct page **pages, int *nr)
1327 #endif /* __HAVE_ARCH_PTE_SPECIAL */
1329 static int gup_huge_pmd(pmd_t orig, pmd_t *pmdp, unsigned long addr,
1330 unsigned long end, int write, struct page **pages, int *nr)
1332 struct page *head, *page;
1335 if (write && !pmd_write(orig))
1339 page = pmd_page(orig) + ((addr & ~PMD_MASK) >> PAGE_SHIFT);
1345 } while (addr += PAGE_SIZE, addr != end);
1347 head = try_get_compound_head(pmd_page(orig), refs);
1353 if (unlikely(pmd_val(orig) != pmd_val(*pmdp))) {
1363 static int gup_huge_pud(pud_t orig, pud_t *pudp, unsigned long addr,
1364 unsigned long end, int write, struct page **pages, int *nr)
1366 struct page *head, *page;
1369 if (write && !pud_write(orig))
1373 page = pud_page(orig) + ((addr & ~PUD_MASK) >> PAGE_SHIFT);
1379 } while (addr += PAGE_SIZE, addr != end);
1381 head = try_get_compound_head(pud_page(orig), refs);
1387 if (unlikely(pud_val(orig) != pud_val(*pudp))) {
1397 static int gup_huge_pgd(pgd_t orig, pgd_t *pgdp, unsigned long addr,
1398 unsigned long end, int write,
1399 struct page **pages, int *nr)
1402 struct page *head, *page;
1404 if (write && !pgd_write(orig))
1408 page = pgd_page(orig) + ((addr & ~PGDIR_MASK) >> PAGE_SHIFT);
1414 } while (addr += PAGE_SIZE, addr != end);
1416 head = try_get_compound_head(pgd_page(orig), refs);
1422 if (unlikely(pgd_val(orig) != pgd_val(*pgdp))) {
1432 static int gup_pmd_range(pud_t pud, unsigned long addr, unsigned long end,
1433 int write, struct page **pages, int *nr)
1438 pmdp = pmd_offset(&pud, addr);
1440 pmd_t pmd = READ_ONCE(*pmdp);
1442 next = pmd_addr_end(addr, end);
1446 if (unlikely(pmd_trans_huge(pmd) || pmd_huge(pmd) ||
1449 * NUMA hinting faults need to be handled in the GUP
1450 * slowpath for accounting purposes and so that they
1451 * can be serialised against THP migration.
1453 if (pmd_protnone(pmd))
1456 if (!gup_huge_pmd(pmd, pmdp, addr, next, write,
1460 } else if (unlikely(is_hugepd(__hugepd(pmd_val(pmd))))) {
1462 * architecture have different format for hugetlbfs
1463 * pmd format and THP pmd format
1465 if (!gup_huge_pd(__hugepd(pmd_val(pmd)), addr,
1466 PMD_SHIFT, next, write, pages, nr))
1468 } else if (!gup_pte_range(pmd, addr, next, write, pages, nr))
1470 } while (pmdp++, addr = next, addr != end);
1475 static int gup_pud_range(pgd_t pgd, unsigned long addr, unsigned long end,
1476 int write, struct page **pages, int *nr)
1481 pudp = pud_offset(&pgd, addr);
1483 pud_t pud = READ_ONCE(*pudp);
1485 next = pud_addr_end(addr, end);
1488 if (unlikely(pud_huge(pud))) {
1489 if (!gup_huge_pud(pud, pudp, addr, next, write,
1492 } else if (unlikely(is_hugepd(__hugepd(pud_val(pud))))) {
1493 if (!gup_huge_pd(__hugepd(pud_val(pud)), addr,
1494 PUD_SHIFT, next, write, pages, nr))
1496 } else if (!gup_pmd_range(pud, addr, next, write, pages, nr))
1498 } while (pudp++, addr = next, addr != end);
1504 * Like get_user_pages_fast() except it's IRQ-safe in that it won't fall back to
1505 * the regular GUP. It will only return non-negative values.
1507 int __get_user_pages_fast(unsigned long start, int nr_pages, int write,
1508 struct page **pages)
1510 struct mm_struct *mm = current->mm;
1511 unsigned long addr, len, end;
1512 unsigned long next, flags;
1518 len = (unsigned long) nr_pages << PAGE_SHIFT;
1521 if (unlikely(!access_ok(write ? VERIFY_WRITE : VERIFY_READ,
1526 * Disable interrupts. We use the nested form as we can already have
1527 * interrupts disabled by get_futex_key.
1529 * With interrupts disabled, we block page table pages from being
1530 * freed from under us. See mmu_gather_tlb in asm-generic/tlb.h
1533 * We do not adopt an rcu_read_lock(.) here as we also want to
1534 * block IPIs that come from THPs splitting.
1537 local_irq_save(flags);
1538 pgdp = pgd_offset(mm, addr);
1540 pgd_t pgd = READ_ONCE(*pgdp);
1542 next = pgd_addr_end(addr, end);
1545 if (unlikely(pgd_huge(pgd))) {
1546 if (!gup_huge_pgd(pgd, pgdp, addr, next, write,
1549 } else if (unlikely(is_hugepd(__hugepd(pgd_val(pgd))))) {
1550 if (!gup_huge_pd(__hugepd(pgd_val(pgd)), addr,
1551 PGDIR_SHIFT, next, write, pages, &nr))
1553 } else if (!gup_pud_range(pgd, addr, next, write, pages, &nr))
1555 } while (pgdp++, addr = next, addr != end);
1556 local_irq_restore(flags);
1562 * get_user_pages_fast() - pin user pages in memory
1563 * @start: starting user address
1564 * @nr_pages: number of pages from start to pin
1565 * @write: whether pages will be written to
1566 * @pages: array that receives pointers to the pages pinned.
1567 * Should be at least nr_pages long.
1569 * Attempt to pin user pages in memory without taking mm->mmap_sem.
1570 * If not successful, it will fall back to taking the lock and
1571 * calling get_user_pages().
1573 * Returns number of pages pinned. This may be fewer than the number
1574 * requested. If nr_pages is 0 or negative, returns 0. If no pages
1575 * were pinned, returns -errno.
1577 int get_user_pages_fast(unsigned long start, int nr_pages, int write,
1578 struct page **pages)
1583 nr = __get_user_pages_fast(start, nr_pages, write, pages);
1586 if (nr < nr_pages) {
1587 /* Try to get the remaining pages with get_user_pages */
1588 start += nr << PAGE_SHIFT;
1591 ret = get_user_pages_unlocked(start, nr_pages - nr, pages,
1592 write ? FOLL_WRITE : 0);
1594 /* Have to be a bit careful with return values */
1606 #endif /* CONFIG_HAVE_GENERIC_RCU_GUP */