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 typedef int (*set_dirty_func_t)(struct page *page);
33 static void __put_user_pages_dirty(struct page **pages,
39 for (index = 0; index < npages; index++) {
40 struct page *page = compound_head(pages[index]);
43 * Checking PageDirty at this point may race with
44 * clear_page_dirty_for_io(), but that's OK. Two key cases:
46 * 1) This code sees the page as already dirty, so it skips
47 * the call to sdf(). That could happen because
48 * clear_page_dirty_for_io() called page_mkclean(),
49 * followed by set_page_dirty(). However, now the page is
50 * going to get written back, which meets the original
51 * intention of setting it dirty, so all is well:
52 * clear_page_dirty_for_io() goes on to call
53 * TestClearPageDirty(), and write the page back.
55 * 2) This code sees the page as clean, so it calls sdf().
56 * The page stays dirty, despite being written back, so it
57 * gets written back again in the next writeback cycle.
68 * put_user_pages_dirty() - release and dirty an array of gup-pinned pages
69 * @pages: array of pages to be marked dirty and released.
70 * @npages: number of pages in the @pages array.
72 * "gup-pinned page" refers to a page that has had one of the get_user_pages()
73 * variants called on that page.
75 * For each page in the @pages array, make that page (or its head page, if a
76 * compound page) dirty, if it was previously listed as clean. Then, release
77 * the page using put_user_page().
79 * Please see the put_user_page() documentation for details.
81 * set_page_dirty(), which does not lock the page, is used here.
82 * Therefore, it is the caller's responsibility to ensure that this is
83 * safe. If not, then put_user_pages_dirty_lock() should be called instead.
86 void put_user_pages_dirty(struct page **pages, unsigned long npages)
88 __put_user_pages_dirty(pages, npages, set_page_dirty);
90 EXPORT_SYMBOL(put_user_pages_dirty);
93 * put_user_pages_dirty_lock() - release and dirty an array of gup-pinned pages
94 * @pages: array of pages to be marked dirty and released.
95 * @npages: number of pages in the @pages array.
97 * For each page in the @pages array, make that page (or its head page, if a
98 * compound page) dirty, if it was previously listed as clean. Then, release
99 * the page using put_user_page().
101 * Please see the put_user_page() documentation for details.
103 * This is just like put_user_pages_dirty(), except that it invokes
104 * set_page_dirty_lock(), instead of set_page_dirty().
107 void put_user_pages_dirty_lock(struct page **pages, unsigned long npages)
109 __put_user_pages_dirty(pages, npages, set_page_dirty_lock);
111 EXPORT_SYMBOL(put_user_pages_dirty_lock);
114 * put_user_pages() - release an array of gup-pinned pages.
115 * @pages: array of pages to be marked dirty and released.
116 * @npages: number of pages in the @pages array.
118 * For each page in the @pages array, release the page using put_user_page().
120 * Please see the put_user_page() documentation for details.
122 void put_user_pages(struct page **pages, unsigned long npages)
127 * TODO: this can be optimized for huge pages: if a series of pages is
128 * physically contiguous and part of the same compound page, then a
129 * single operation to the head page should suffice.
131 for (index = 0; index < npages; index++)
132 put_user_page(pages[index]);
134 EXPORT_SYMBOL(put_user_pages);
136 static struct page *no_page_table(struct vm_area_struct *vma,
140 * When core dumping an enormous anonymous area that nobody
141 * has touched so far, we don't want to allocate unnecessary pages or
142 * page tables. Return error instead of NULL to skip handle_mm_fault,
143 * then get_dump_page() will return NULL to leave a hole in the dump.
144 * But we can only make this optimization where a hole would surely
145 * be zero-filled if handle_mm_fault() actually did handle it.
147 if ((flags & FOLL_DUMP) && (!vma->vm_ops || !vma->vm_ops->fault))
148 return ERR_PTR(-EFAULT);
152 static int follow_pfn_pte(struct vm_area_struct *vma, unsigned long address,
153 pte_t *pte, unsigned int flags)
155 /* No page to get reference */
156 if (flags & FOLL_GET)
159 if (flags & FOLL_TOUCH) {
162 if (flags & FOLL_WRITE)
163 entry = pte_mkdirty(entry);
164 entry = pte_mkyoung(entry);
166 if (!pte_same(*pte, entry)) {
167 set_pte_at(vma->vm_mm, address, pte, entry);
168 update_mmu_cache(vma, address, pte);
172 /* Proper page table entry exists, but no corresponding struct page */
177 * FOLL_FORCE can write to even unwritable pte's, but only
178 * after we've gone through a COW cycle and they are dirty.
180 static inline bool can_follow_write_pte(pte_t pte, unsigned int flags)
182 return pte_write(pte) ||
183 ((flags & FOLL_FORCE) && (flags & FOLL_COW) && pte_dirty(pte));
186 static struct page *follow_page_pte(struct vm_area_struct *vma,
187 unsigned long address, pmd_t *pmd, unsigned int flags,
188 struct dev_pagemap **pgmap)
190 struct mm_struct *mm = vma->vm_mm;
196 if (unlikely(pmd_bad(*pmd)))
197 return no_page_table(vma, flags);
199 ptep = pte_offset_map_lock(mm, pmd, address, &ptl);
201 if (!pte_present(pte)) {
204 * KSM's break_ksm() relies upon recognizing a ksm page
205 * even while it is being migrated, so for that case we
206 * need migration_entry_wait().
208 if (likely(!(flags & FOLL_MIGRATION)))
212 entry = pte_to_swp_entry(pte);
213 if (!is_migration_entry(entry))
215 pte_unmap_unlock(ptep, ptl);
216 migration_entry_wait(mm, pmd, address);
219 if ((flags & FOLL_NUMA) && pte_protnone(pte))
221 if ((flags & FOLL_WRITE) && !can_follow_write_pte(pte, flags)) {
222 pte_unmap_unlock(ptep, ptl);
226 page = vm_normal_page(vma, address, pte);
227 if (!page && pte_devmap(pte) && (flags & FOLL_GET)) {
229 * Only return device mapping pages in the FOLL_GET case since
230 * they are only valid while holding the pgmap reference.
232 *pgmap = get_dev_pagemap(pte_pfn(pte), *pgmap);
234 page = pte_page(pte);
237 } else if (unlikely(!page)) {
238 if (flags & FOLL_DUMP) {
239 /* Avoid special (like zero) pages in core dumps */
240 page = ERR_PTR(-EFAULT);
244 if (is_zero_pfn(pte_pfn(pte))) {
245 page = pte_page(pte);
249 ret = follow_pfn_pte(vma, address, ptep, flags);
255 if (flags & FOLL_SPLIT && PageTransCompound(page)) {
258 pte_unmap_unlock(ptep, ptl);
260 ret = split_huge_page(page);
268 if (flags & FOLL_GET) {
269 if (unlikely(!try_get_page(page))) {
270 page = ERR_PTR(-ENOMEM);
274 if (flags & FOLL_TOUCH) {
275 if ((flags & FOLL_WRITE) &&
276 !pte_dirty(pte) && !PageDirty(page))
277 set_page_dirty(page);
279 * pte_mkyoung() would be more correct here, but atomic care
280 * is needed to avoid losing the dirty bit: it is easier to use
281 * mark_page_accessed().
283 mark_page_accessed(page);
285 if ((flags & FOLL_MLOCK) && (vma->vm_flags & VM_LOCKED)) {
286 /* Do not mlock pte-mapped THP */
287 if (PageTransCompound(page))
291 * The preliminary mapping check is mainly to avoid the
292 * pointless overhead of lock_page on the ZERO_PAGE
293 * which might bounce very badly if there is contention.
295 * If the page is already locked, we don't need to
296 * handle it now - vmscan will handle it later if and
297 * when it attempts to reclaim the page.
299 if (page->mapping && trylock_page(page)) {
300 lru_add_drain(); /* push cached pages to LRU */
302 * Because we lock page here, and migration is
303 * blocked by the pte's page reference, and we
304 * know the page is still mapped, we don't even
305 * need to check for file-cache page truncation.
307 mlock_vma_page(page);
312 pte_unmap_unlock(ptep, ptl);
315 pte_unmap_unlock(ptep, ptl);
318 return no_page_table(vma, flags);
321 static struct page *follow_pmd_mask(struct vm_area_struct *vma,
322 unsigned long address, pud_t *pudp,
324 struct follow_page_context *ctx)
329 struct mm_struct *mm = vma->vm_mm;
331 pmd = pmd_offset(pudp, address);
333 * The READ_ONCE() will stabilize the pmdval in a register or
334 * on the stack so that it will stop changing under the code.
336 pmdval = READ_ONCE(*pmd);
337 if (pmd_none(pmdval))
338 return no_page_table(vma, flags);
339 if (pmd_huge(pmdval) && vma->vm_flags & VM_HUGETLB) {
340 page = follow_huge_pmd(mm, address, pmd, flags);
343 return no_page_table(vma, flags);
345 if (is_hugepd(__hugepd(pmd_val(pmdval)))) {
346 page = follow_huge_pd(vma, address,
347 __hugepd(pmd_val(pmdval)), flags,
351 return no_page_table(vma, flags);
354 if (!pmd_present(pmdval)) {
355 if (likely(!(flags & FOLL_MIGRATION)))
356 return no_page_table(vma, flags);
357 VM_BUG_ON(thp_migration_supported() &&
358 !is_pmd_migration_entry(pmdval));
359 if (is_pmd_migration_entry(pmdval))
360 pmd_migration_entry_wait(mm, pmd);
361 pmdval = READ_ONCE(*pmd);
363 * MADV_DONTNEED may convert the pmd to null because
364 * mmap_sem is held in read mode
366 if (pmd_none(pmdval))
367 return no_page_table(vma, flags);
370 if (pmd_devmap(pmdval)) {
371 ptl = pmd_lock(mm, pmd);
372 page = follow_devmap_pmd(vma, address, pmd, flags, &ctx->pgmap);
377 if (likely(!pmd_trans_huge(pmdval)))
378 return follow_page_pte(vma, address, pmd, flags, &ctx->pgmap);
380 if ((flags & FOLL_NUMA) && pmd_protnone(pmdval))
381 return no_page_table(vma, flags);
384 ptl = pmd_lock(mm, pmd);
385 if (unlikely(pmd_none(*pmd))) {
387 return no_page_table(vma, flags);
389 if (unlikely(!pmd_present(*pmd))) {
391 if (likely(!(flags & FOLL_MIGRATION)))
392 return no_page_table(vma, flags);
393 pmd_migration_entry_wait(mm, pmd);
396 if (unlikely(!pmd_trans_huge(*pmd))) {
398 return follow_page_pte(vma, address, pmd, flags, &ctx->pgmap);
400 if (flags & FOLL_SPLIT) {
402 page = pmd_page(*pmd);
403 if (is_huge_zero_page(page)) {
406 split_huge_pmd(vma, pmd, address);
407 if (pmd_trans_unstable(pmd))
410 if (unlikely(!try_get_page(page))) {
412 return ERR_PTR(-ENOMEM);
416 ret = split_huge_page(page);
420 return no_page_table(vma, flags);
423 return ret ? ERR_PTR(ret) :
424 follow_page_pte(vma, address, pmd, flags, &ctx->pgmap);
426 page = follow_trans_huge_pmd(vma, address, pmd, flags);
428 ctx->page_mask = HPAGE_PMD_NR - 1;
432 static struct page *follow_pud_mask(struct vm_area_struct *vma,
433 unsigned long address, p4d_t *p4dp,
435 struct follow_page_context *ctx)
440 struct mm_struct *mm = vma->vm_mm;
442 pud = pud_offset(p4dp, address);
444 return no_page_table(vma, flags);
445 if (pud_huge(*pud) && vma->vm_flags & VM_HUGETLB) {
446 page = follow_huge_pud(mm, address, pud, flags);
449 return no_page_table(vma, flags);
451 if (is_hugepd(__hugepd(pud_val(*pud)))) {
452 page = follow_huge_pd(vma, address,
453 __hugepd(pud_val(*pud)), flags,
457 return no_page_table(vma, flags);
459 if (pud_devmap(*pud)) {
460 ptl = pud_lock(mm, pud);
461 page = follow_devmap_pud(vma, address, pud, flags, &ctx->pgmap);
466 if (unlikely(pud_bad(*pud)))
467 return no_page_table(vma, flags);
469 return follow_pmd_mask(vma, address, pud, flags, ctx);
472 static struct page *follow_p4d_mask(struct vm_area_struct *vma,
473 unsigned long address, pgd_t *pgdp,
475 struct follow_page_context *ctx)
480 p4d = p4d_offset(pgdp, address);
482 return no_page_table(vma, flags);
483 BUILD_BUG_ON(p4d_huge(*p4d));
484 if (unlikely(p4d_bad(*p4d)))
485 return no_page_table(vma, flags);
487 if (is_hugepd(__hugepd(p4d_val(*p4d)))) {
488 page = follow_huge_pd(vma, address,
489 __hugepd(p4d_val(*p4d)), flags,
493 return no_page_table(vma, flags);
495 return follow_pud_mask(vma, address, p4d, flags, ctx);
499 * follow_page_mask - look up a page descriptor from a user-virtual address
500 * @vma: vm_area_struct mapping @address
501 * @address: virtual address to look up
502 * @flags: flags modifying lookup behaviour
503 * @ctx: contains dev_pagemap for %ZONE_DEVICE memory pinning and a
504 * pointer to output page_mask
506 * @flags can have FOLL_ flags set, defined in <linux/mm.h>
508 * When getting pages from ZONE_DEVICE memory, the @ctx->pgmap caches
509 * the device's dev_pagemap metadata to avoid repeating expensive lookups.
511 * On output, the @ctx->page_mask is set according to the size of the page.
513 * Return: the mapped (struct page *), %NULL if no mapping exists, or
514 * an error pointer if there is a mapping to something not represented
515 * by a page descriptor (see also vm_normal_page()).
517 struct page *follow_page_mask(struct vm_area_struct *vma,
518 unsigned long address, unsigned int flags,
519 struct follow_page_context *ctx)
523 struct mm_struct *mm = vma->vm_mm;
527 /* make this handle hugepd */
528 page = follow_huge_addr(mm, address, flags & FOLL_WRITE);
530 BUG_ON(flags & FOLL_GET);
534 pgd = pgd_offset(mm, address);
536 if (pgd_none(*pgd) || unlikely(pgd_bad(*pgd)))
537 return no_page_table(vma, flags);
539 if (pgd_huge(*pgd)) {
540 page = follow_huge_pgd(mm, address, pgd, flags);
543 return no_page_table(vma, flags);
545 if (is_hugepd(__hugepd(pgd_val(*pgd)))) {
546 page = follow_huge_pd(vma, address,
547 __hugepd(pgd_val(*pgd)), flags,
551 return no_page_table(vma, flags);
554 return follow_p4d_mask(vma, address, pgd, flags, ctx);
557 struct page *follow_page(struct vm_area_struct *vma, unsigned long address,
558 unsigned int foll_flags)
560 struct follow_page_context ctx = { NULL };
563 page = follow_page_mask(vma, address, foll_flags, &ctx);
565 put_dev_pagemap(ctx.pgmap);
569 static int get_gate_page(struct mm_struct *mm, unsigned long address,
570 unsigned int gup_flags, struct vm_area_struct **vma,
580 /* user gate pages are read-only */
581 if (gup_flags & FOLL_WRITE)
583 if (address > TASK_SIZE)
584 pgd = pgd_offset_k(address);
586 pgd = pgd_offset_gate(mm, address);
587 BUG_ON(pgd_none(*pgd));
588 p4d = p4d_offset(pgd, address);
589 BUG_ON(p4d_none(*p4d));
590 pud = pud_offset(p4d, address);
591 BUG_ON(pud_none(*pud));
592 pmd = pmd_offset(pud, address);
593 if (!pmd_present(*pmd))
595 VM_BUG_ON(pmd_trans_huge(*pmd));
596 pte = pte_offset_map(pmd, address);
599 *vma = get_gate_vma(mm);
602 *page = vm_normal_page(*vma, address, *pte);
604 if ((gup_flags & FOLL_DUMP) || !is_zero_pfn(pte_pfn(*pte)))
606 *page = pte_page(*pte);
609 * This should never happen (a device public page in the gate
612 if (is_device_public_page(*page))
615 if (unlikely(!try_get_page(*page))) {
627 * mmap_sem must be held on entry. If @nonblocking != NULL and
628 * *@flags does not include FOLL_NOWAIT, the mmap_sem may be released.
629 * If it is, *@nonblocking will be set to 0 and -EBUSY returned.
631 static int faultin_page(struct task_struct *tsk, struct vm_area_struct *vma,
632 unsigned long address, unsigned int *flags, int *nonblocking)
634 unsigned int fault_flags = 0;
637 /* mlock all present pages, but do not fault in new pages */
638 if ((*flags & (FOLL_POPULATE | FOLL_MLOCK)) == FOLL_MLOCK)
640 if (*flags & FOLL_WRITE)
641 fault_flags |= FAULT_FLAG_WRITE;
642 if (*flags & FOLL_REMOTE)
643 fault_flags |= FAULT_FLAG_REMOTE;
645 fault_flags |= FAULT_FLAG_ALLOW_RETRY;
646 if (*flags & FOLL_NOWAIT)
647 fault_flags |= FAULT_FLAG_ALLOW_RETRY | FAULT_FLAG_RETRY_NOWAIT;
648 if (*flags & FOLL_TRIED) {
649 VM_WARN_ON_ONCE(fault_flags & FAULT_FLAG_ALLOW_RETRY);
650 fault_flags |= FAULT_FLAG_TRIED;
653 ret = handle_mm_fault(vma, address, fault_flags);
654 if (ret & VM_FAULT_ERROR) {
655 int err = vm_fault_to_errno(ret, *flags);
663 if (ret & VM_FAULT_MAJOR)
669 if (ret & VM_FAULT_RETRY) {
670 if (nonblocking && !(fault_flags & FAULT_FLAG_RETRY_NOWAIT))
676 * The VM_FAULT_WRITE bit tells us that do_wp_page has broken COW when
677 * necessary, even if maybe_mkwrite decided not to set pte_write. We
678 * can thus safely do subsequent page lookups as if they were reads.
679 * But only do so when looping for pte_write is futile: in some cases
680 * userspace may also be wanting to write to the gotten user page,
681 * which a read fault here might prevent (a readonly page might get
682 * reCOWed by userspace write).
684 if ((ret & VM_FAULT_WRITE) && !(vma->vm_flags & VM_WRITE))
689 static int check_vma_flags(struct vm_area_struct *vma, unsigned long gup_flags)
691 vm_flags_t vm_flags = vma->vm_flags;
692 int write = (gup_flags & FOLL_WRITE);
693 int foreign = (gup_flags & FOLL_REMOTE);
695 if (vm_flags & (VM_IO | VM_PFNMAP))
698 if (gup_flags & FOLL_ANON && !vma_is_anonymous(vma))
702 if (!(vm_flags & VM_WRITE)) {
703 if (!(gup_flags & FOLL_FORCE))
706 * We used to let the write,force case do COW in a
707 * VM_MAYWRITE VM_SHARED !VM_WRITE vma, so ptrace could
708 * set a breakpoint in a read-only mapping of an
709 * executable, without corrupting the file (yet only
710 * when that file had been opened for writing!).
711 * Anon pages in shared mappings are surprising: now
714 if (!is_cow_mapping(vm_flags))
717 } else if (!(vm_flags & VM_READ)) {
718 if (!(gup_flags & FOLL_FORCE))
721 * Is there actually any vma we can reach here which does not
722 * have VM_MAYREAD set?
724 if (!(vm_flags & VM_MAYREAD))
728 * gups are always data accesses, not instruction
729 * fetches, so execute=false here
731 if (!arch_vma_access_permitted(vma, write, false, foreign))
737 * __get_user_pages() - pin user pages in memory
738 * @tsk: task_struct of target task
739 * @mm: mm_struct of target mm
740 * @start: starting user address
741 * @nr_pages: number of pages from start to pin
742 * @gup_flags: flags modifying pin behaviour
743 * @pages: array that receives pointers to the pages pinned.
744 * Should be at least nr_pages long. Or NULL, if caller
745 * only intends to ensure the pages are faulted in.
746 * @vmas: array of pointers to vmas corresponding to each page.
747 * Or NULL if the caller does not require them.
748 * @nonblocking: whether waiting for disk IO or mmap_sem contention
750 * Returns number of pages pinned. This may be fewer than the number
751 * requested. If nr_pages is 0 or negative, returns 0. If no pages
752 * were pinned, returns -errno. Each page returned must be released
753 * with a put_page() call when it is finished with. vmas will only
754 * remain valid while mmap_sem is held.
756 * Must be called with mmap_sem held. It may be released. See below.
758 * __get_user_pages walks a process's page tables and takes a reference to
759 * each struct page that each user address corresponds to at a given
760 * instant. That is, it takes the page that would be accessed if a user
761 * thread accesses the given user virtual address at that instant.
763 * This does not guarantee that the page exists in the user mappings when
764 * __get_user_pages returns, and there may even be a completely different
765 * page there in some cases (eg. if mmapped pagecache has been invalidated
766 * and subsequently re faulted). However it does guarantee that the page
767 * won't be freed completely. And mostly callers simply care that the page
768 * contains data that was valid *at some point in time*. Typically, an IO
769 * or similar operation cannot guarantee anything stronger anyway because
770 * locks can't be held over the syscall boundary.
772 * If @gup_flags & FOLL_WRITE == 0, the page must not be written to. If
773 * the page is written to, set_page_dirty (or set_page_dirty_lock, as
774 * appropriate) must be called after the page is finished with, and
775 * before put_page is called.
777 * If @nonblocking != NULL, __get_user_pages will not wait for disk IO
778 * or mmap_sem contention, and if waiting is needed to pin all pages,
779 * *@nonblocking will be set to 0. Further, if @gup_flags does not
780 * include FOLL_NOWAIT, the mmap_sem will be released via up_read() in
783 * A caller using such a combination of @nonblocking and @gup_flags
784 * must therefore hold the mmap_sem for reading only, and recognize
785 * when it's been released. Otherwise, it must be held for either
786 * reading or writing and will not be released.
788 * In most cases, get_user_pages or get_user_pages_fast should be used
789 * instead of __get_user_pages. __get_user_pages should be used only if
790 * you need some special @gup_flags.
792 static long __get_user_pages(struct task_struct *tsk, struct mm_struct *mm,
793 unsigned long start, unsigned long nr_pages,
794 unsigned int gup_flags, struct page **pages,
795 struct vm_area_struct **vmas, int *nonblocking)
798 struct vm_area_struct *vma = NULL;
799 struct follow_page_context ctx = { NULL };
804 VM_BUG_ON(!!pages != !!(gup_flags & FOLL_GET));
807 * If FOLL_FORCE is set then do not force a full fault as the hinting
808 * fault information is unrelated to the reference behaviour of a task
809 * using the address space
811 if (!(gup_flags & FOLL_FORCE))
812 gup_flags |= FOLL_NUMA;
816 unsigned int foll_flags = gup_flags;
817 unsigned int page_increm;
819 /* first iteration or cross vma bound */
820 if (!vma || start >= vma->vm_end) {
821 vma = find_extend_vma(mm, start);
822 if (!vma && in_gate_area(mm, start)) {
823 ret = get_gate_page(mm, start & PAGE_MASK,
825 pages ? &pages[i] : NULL);
832 if (!vma || check_vma_flags(vma, gup_flags)) {
836 if (is_vm_hugetlb_page(vma)) {
837 i = follow_hugetlb_page(mm, vma, pages, vmas,
838 &start, &nr_pages, i,
839 gup_flags, nonblocking);
845 * If we have a pending SIGKILL, don't keep faulting pages and
846 * potentially allocating memory.
848 if (fatal_signal_pending(current)) {
854 page = follow_page_mask(vma, start, foll_flags, &ctx);
856 ret = faultin_page(tsk, vma, start, &foll_flags,
872 } else if (PTR_ERR(page) == -EEXIST) {
874 * Proper page table entry exists, but no corresponding
878 } else if (IS_ERR(page)) {
884 flush_anon_page(vma, page, start);
885 flush_dcache_page(page);
893 page_increm = 1 + (~(start >> PAGE_SHIFT) & ctx.page_mask);
894 if (page_increm > nr_pages)
895 page_increm = nr_pages;
897 start += page_increm * PAGE_SIZE;
898 nr_pages -= page_increm;
902 put_dev_pagemap(ctx.pgmap);
906 static bool vma_permits_fault(struct vm_area_struct *vma,
907 unsigned int fault_flags)
909 bool write = !!(fault_flags & FAULT_FLAG_WRITE);
910 bool foreign = !!(fault_flags & FAULT_FLAG_REMOTE);
911 vm_flags_t vm_flags = write ? VM_WRITE : VM_READ;
913 if (!(vm_flags & vma->vm_flags))
917 * The architecture might have a hardware protection
918 * mechanism other than read/write that can deny access.
920 * gup always represents data access, not instruction
921 * fetches, so execute=false here:
923 if (!arch_vma_access_permitted(vma, write, false, foreign))
930 * fixup_user_fault() - manually resolve a user page fault
931 * @tsk: the task_struct to use for page fault accounting, or
932 * NULL if faults are not to be recorded.
933 * @mm: mm_struct of target mm
934 * @address: user address
935 * @fault_flags:flags to pass down to handle_mm_fault()
936 * @unlocked: did we unlock the mmap_sem while retrying, maybe NULL if caller
937 * does not allow retry
939 * This is meant to be called in the specific scenario where for locking reasons
940 * we try to access user memory in atomic context (within a pagefault_disable()
941 * section), this returns -EFAULT, and we want to resolve the user fault before
944 * Typically this is meant to be used by the futex code.
946 * The main difference with get_user_pages() is that this function will
947 * unconditionally call handle_mm_fault() which will in turn perform all the
948 * necessary SW fixup of the dirty and young bits in the PTE, while
949 * get_user_pages() only guarantees to update these in the struct page.
951 * This is important for some architectures where those bits also gate the
952 * access permission to the page because they are maintained in software. On
953 * such architectures, gup() will not be enough to make a subsequent access
956 * This function will not return with an unlocked mmap_sem. So it has not the
957 * same semantics wrt the @mm->mmap_sem as does filemap_fault().
959 int fixup_user_fault(struct task_struct *tsk, struct mm_struct *mm,
960 unsigned long address, unsigned int fault_flags,
963 struct vm_area_struct *vma;
964 vm_fault_t ret, major = 0;
967 fault_flags |= FAULT_FLAG_ALLOW_RETRY;
970 vma = find_extend_vma(mm, address);
971 if (!vma || address < vma->vm_start)
974 if (!vma_permits_fault(vma, fault_flags))
977 ret = handle_mm_fault(vma, address, fault_flags);
978 major |= ret & VM_FAULT_MAJOR;
979 if (ret & VM_FAULT_ERROR) {
980 int err = vm_fault_to_errno(ret, 0);
987 if (ret & VM_FAULT_RETRY) {
988 down_read(&mm->mmap_sem);
989 if (!(fault_flags & FAULT_FLAG_TRIED)) {
991 fault_flags &= ~FAULT_FLAG_ALLOW_RETRY;
992 fault_flags |= FAULT_FLAG_TRIED;
1005 EXPORT_SYMBOL_GPL(fixup_user_fault);
1007 static __always_inline long __get_user_pages_locked(struct task_struct *tsk,
1008 struct mm_struct *mm,
1009 unsigned long start,
1010 unsigned long nr_pages,
1011 struct page **pages,
1012 struct vm_area_struct **vmas,
1016 long ret, pages_done;
1020 /* if VM_FAULT_RETRY can be returned, vmas become invalid */
1022 /* check caller initialized locked */
1023 BUG_ON(*locked != 1);
1030 lock_dropped = false;
1032 ret = __get_user_pages(tsk, mm, start, nr_pages, flags, pages,
1035 /* VM_FAULT_RETRY couldn't trigger, bypass */
1038 /* VM_FAULT_RETRY cannot return errors */
1041 BUG_ON(ret >= nr_pages);
1045 /* If it's a prefault don't insist harder */
1056 * VM_FAULT_RETRY didn't trigger or it was a
1063 /* VM_FAULT_RETRY triggered, so seek to the faulting offset */
1065 start += ret << PAGE_SHIFT;
1068 * Repeat on the address that fired VM_FAULT_RETRY
1069 * without FAULT_FLAG_ALLOW_RETRY but with
1073 lock_dropped = true;
1074 down_read(&mm->mmap_sem);
1075 ret = __get_user_pages(tsk, mm, start, 1, flags | FOLL_TRIED,
1090 if (lock_dropped && *locked) {
1092 * We must let the caller know we temporarily dropped the lock
1093 * and so the critical section protected by it was lost.
1095 up_read(&mm->mmap_sem);
1102 * We can leverage the VM_FAULT_RETRY functionality in the page fault
1103 * paths better by using either get_user_pages_locked() or
1104 * get_user_pages_unlocked().
1106 * get_user_pages_locked() is suitable to replace the form:
1108 * down_read(&mm->mmap_sem);
1110 * get_user_pages(tsk, mm, ..., pages, NULL);
1111 * up_read(&mm->mmap_sem);
1116 * down_read(&mm->mmap_sem);
1118 * get_user_pages_locked(tsk, mm, ..., pages, &locked);
1120 * up_read(&mm->mmap_sem);
1122 long get_user_pages_locked(unsigned long start, unsigned long nr_pages,
1123 unsigned int gup_flags, struct page **pages,
1127 * FIXME: Current FOLL_LONGTERM behavior is incompatible with
1128 * FAULT_FLAG_ALLOW_RETRY because of the FS DAX check requirement on
1129 * vmas. As there are no users of this flag in this call we simply
1130 * disallow this option for now.
1132 if (WARN_ON_ONCE(gup_flags & FOLL_LONGTERM))
1135 return __get_user_pages_locked(current, current->mm, start, nr_pages,
1136 pages, NULL, locked,
1137 gup_flags | FOLL_TOUCH);
1139 EXPORT_SYMBOL(get_user_pages_locked);
1142 * get_user_pages_unlocked() is suitable to replace the form:
1144 * down_read(&mm->mmap_sem);
1145 * get_user_pages(tsk, mm, ..., pages, NULL);
1146 * up_read(&mm->mmap_sem);
1150 * get_user_pages_unlocked(tsk, mm, ..., pages);
1152 * It is functionally equivalent to get_user_pages_fast so
1153 * get_user_pages_fast should be used instead if specific gup_flags
1154 * (e.g. FOLL_FORCE) are not required.
1156 long get_user_pages_unlocked(unsigned long start, unsigned long nr_pages,
1157 struct page **pages, unsigned int gup_flags)
1159 struct mm_struct *mm = current->mm;
1164 * FIXME: Current FOLL_LONGTERM behavior is incompatible with
1165 * FAULT_FLAG_ALLOW_RETRY because of the FS DAX check requirement on
1166 * vmas. As there are no users of this flag in this call we simply
1167 * disallow this option for now.
1169 if (WARN_ON_ONCE(gup_flags & FOLL_LONGTERM))
1172 down_read(&mm->mmap_sem);
1173 ret = __get_user_pages_locked(current, mm, start, nr_pages, pages, NULL,
1174 &locked, gup_flags | FOLL_TOUCH);
1176 up_read(&mm->mmap_sem);
1179 EXPORT_SYMBOL(get_user_pages_unlocked);
1182 * get_user_pages_remote() - pin user pages in memory
1183 * @tsk: the task_struct to use for page fault accounting, or
1184 * NULL if faults are not to be recorded.
1185 * @mm: mm_struct of target mm
1186 * @start: starting user address
1187 * @nr_pages: number of pages from start to pin
1188 * @gup_flags: flags modifying lookup behaviour
1189 * @pages: array that receives pointers to the pages pinned.
1190 * Should be at least nr_pages long. Or NULL, if caller
1191 * only intends to ensure the pages are faulted in.
1192 * @vmas: array of pointers to vmas corresponding to each page.
1193 * Or NULL if the caller does not require them.
1194 * @locked: pointer to lock flag indicating whether lock is held and
1195 * subsequently whether VM_FAULT_RETRY functionality can be
1196 * utilised. Lock must initially be held.
1198 * Returns number of pages pinned. This may be fewer than the number
1199 * requested. If nr_pages is 0 or negative, returns 0. If no pages
1200 * were pinned, returns -errno. Each page returned must be released
1201 * with a put_page() call when it is finished with. vmas will only
1202 * remain valid while mmap_sem is held.
1204 * Must be called with mmap_sem held for read or write.
1206 * get_user_pages walks a process's page tables and takes a reference to
1207 * each struct page that each user address corresponds to at a given
1208 * instant. That is, it takes the page that would be accessed if a user
1209 * thread accesses the given user virtual address at that instant.
1211 * This does not guarantee that the page exists in the user mappings when
1212 * get_user_pages returns, and there may even be a completely different
1213 * page there in some cases (eg. if mmapped pagecache has been invalidated
1214 * and subsequently re faulted). However it does guarantee that the page
1215 * won't be freed completely. And mostly callers simply care that the page
1216 * contains data that was valid *at some point in time*. Typically, an IO
1217 * or similar operation cannot guarantee anything stronger anyway because
1218 * locks can't be held over the syscall boundary.
1220 * If gup_flags & FOLL_WRITE == 0, the page must not be written to. If the page
1221 * is written to, set_page_dirty (or set_page_dirty_lock, as appropriate) must
1222 * be called after the page is finished with, and before put_page is called.
1224 * get_user_pages is typically used for fewer-copy IO operations, to get a
1225 * handle on the memory by some means other than accesses via the user virtual
1226 * addresses. The pages may be submitted for DMA to devices or accessed via
1227 * their kernel linear mapping (via the kmap APIs). Care should be taken to
1228 * use the correct cache flushing APIs.
1230 * See also get_user_pages_fast, for performance critical applications.
1232 * get_user_pages should be phased out in favor of
1233 * get_user_pages_locked|unlocked or get_user_pages_fast. Nothing
1234 * should use get_user_pages because it cannot pass
1235 * FAULT_FLAG_ALLOW_RETRY to handle_mm_fault.
1237 long get_user_pages_remote(struct task_struct *tsk, struct mm_struct *mm,
1238 unsigned long start, unsigned long nr_pages,
1239 unsigned int gup_flags, struct page **pages,
1240 struct vm_area_struct **vmas, int *locked)
1243 * FIXME: Current FOLL_LONGTERM behavior is incompatible with
1244 * FAULT_FLAG_ALLOW_RETRY because of the FS DAX check requirement on
1245 * vmas. As there are no users of this flag in this call we simply
1246 * disallow this option for now.
1248 if (WARN_ON_ONCE(gup_flags & FOLL_LONGTERM))
1251 return __get_user_pages_locked(tsk, mm, start, nr_pages, pages, vmas,
1253 gup_flags | FOLL_TOUCH | FOLL_REMOTE);
1255 EXPORT_SYMBOL(get_user_pages_remote);
1257 #if defined(CONFIG_FS_DAX) || defined (CONFIG_CMA)
1258 static bool check_dax_vmas(struct vm_area_struct **vmas, long nr_pages)
1261 struct vm_area_struct *vma_prev = NULL;
1263 for (i = 0; i < nr_pages; i++) {
1264 struct vm_area_struct *vma = vmas[i];
1266 if (vma == vma_prev)
1271 if (vma_is_fsdax(vma))
1278 static struct page *new_non_cma_page(struct page *page, unsigned long private)
1281 * We want to make sure we allocate the new page from the same node
1282 * as the source page.
1284 int nid = page_to_nid(page);
1286 * Trying to allocate a page for migration. Ignore allocation
1287 * failure warnings. We don't force __GFP_THISNODE here because
1288 * this node here is the node where we have CMA reservation and
1289 * in some case these nodes will have really less non movable
1290 * allocation memory.
1292 gfp_t gfp_mask = GFP_USER | __GFP_NOWARN;
1294 if (PageHighMem(page))
1295 gfp_mask |= __GFP_HIGHMEM;
1297 #ifdef CONFIG_HUGETLB_PAGE
1298 if (PageHuge(page)) {
1299 struct hstate *h = page_hstate(page);
1301 * We don't want to dequeue from the pool because pool pages will
1302 * mostly be from the CMA region.
1304 return alloc_migrate_huge_page(h, gfp_mask, nid, NULL);
1307 if (PageTransHuge(page)) {
1310 * ignore allocation failure warnings
1312 gfp_t thp_gfpmask = GFP_TRANSHUGE | __GFP_NOWARN;
1315 * Remove the movable mask so that we don't allocate from
1318 thp_gfpmask &= ~__GFP_MOVABLE;
1319 thp = __alloc_pages_node(nid, thp_gfpmask, HPAGE_PMD_ORDER);
1322 prep_transhuge_page(thp);
1326 return __alloc_pages_node(nid, gfp_mask, 0);
1329 static long check_and_migrate_cma_pages(struct task_struct *tsk,
1330 struct mm_struct *mm,
1331 unsigned long start,
1332 unsigned long nr_pages,
1333 struct page **pages,
1334 struct vm_area_struct **vmas,
1335 unsigned int gup_flags)
1338 bool drain_allow = true;
1339 bool migrate_allow = true;
1340 LIST_HEAD(cma_page_list);
1343 for (i = 0; i < nr_pages; i++) {
1345 * If we get a page from the CMA zone, since we are going to
1346 * be pinning these entries, we might as well move them out
1347 * of the CMA zone if possible.
1349 if (is_migrate_cma_page(pages[i])) {
1351 struct page *head = compound_head(pages[i]);
1353 if (PageHuge(head)) {
1354 isolate_huge_page(head, &cma_page_list);
1356 if (!PageLRU(head) && drain_allow) {
1357 lru_add_drain_all();
1358 drain_allow = false;
1361 if (!isolate_lru_page(head)) {
1362 list_add_tail(&head->lru, &cma_page_list);
1363 mod_node_page_state(page_pgdat(head),
1365 page_is_file_cache(head),
1366 hpage_nr_pages(head));
1372 if (!list_empty(&cma_page_list)) {
1374 * drop the above get_user_pages reference.
1376 for (i = 0; i < nr_pages; i++)
1379 if (migrate_pages(&cma_page_list, new_non_cma_page,
1380 NULL, 0, MIGRATE_SYNC, MR_CONTIG_RANGE)) {
1382 * some of the pages failed migration. Do get_user_pages
1383 * without migration.
1385 migrate_allow = false;
1387 if (!list_empty(&cma_page_list))
1388 putback_movable_pages(&cma_page_list);
1391 * We did migrate all the pages, Try to get the page references
1392 * again migrating any new CMA pages which we failed to isolate
1395 nr_pages = __get_user_pages_locked(tsk, mm, start, nr_pages,
1399 if ((nr_pages > 0) && migrate_allow) {
1408 static long check_and_migrate_cma_pages(struct task_struct *tsk,
1409 struct mm_struct *mm,
1410 unsigned long start,
1411 unsigned long nr_pages,
1412 struct page **pages,
1413 struct vm_area_struct **vmas,
1414 unsigned int gup_flags)
1421 * __gup_longterm_locked() is a wrapper for __get_user_pages_locked which
1422 * allows us to process the FOLL_LONGTERM flag.
1424 static long __gup_longterm_locked(struct task_struct *tsk,
1425 struct mm_struct *mm,
1426 unsigned long start,
1427 unsigned long nr_pages,
1428 struct page **pages,
1429 struct vm_area_struct **vmas,
1430 unsigned int gup_flags)
1432 struct vm_area_struct **vmas_tmp = vmas;
1433 unsigned long flags = 0;
1436 if (gup_flags & FOLL_LONGTERM) {
1441 vmas_tmp = kcalloc(nr_pages,
1442 sizeof(struct vm_area_struct *),
1447 flags = memalloc_nocma_save();
1450 rc = __get_user_pages_locked(tsk, mm, start, nr_pages, pages,
1451 vmas_tmp, NULL, gup_flags);
1453 if (gup_flags & FOLL_LONGTERM) {
1454 memalloc_nocma_restore(flags);
1458 if (check_dax_vmas(vmas_tmp, rc)) {
1459 for (i = 0; i < rc; i++)
1465 rc = check_and_migrate_cma_pages(tsk, mm, start, rc, pages,
1466 vmas_tmp, gup_flags);
1470 if (vmas_tmp != vmas)
1474 #else /* !CONFIG_FS_DAX && !CONFIG_CMA */
1475 static __always_inline long __gup_longterm_locked(struct task_struct *tsk,
1476 struct mm_struct *mm,
1477 unsigned long start,
1478 unsigned long nr_pages,
1479 struct page **pages,
1480 struct vm_area_struct **vmas,
1483 return __get_user_pages_locked(tsk, mm, start, nr_pages, pages, vmas,
1486 #endif /* CONFIG_FS_DAX || CONFIG_CMA */
1489 * This is the same as get_user_pages_remote(), just with a
1490 * less-flexible calling convention where we assume that the task
1491 * and mm being operated on are the current task's and don't allow
1492 * passing of a locked parameter. We also obviously don't pass
1493 * FOLL_REMOTE in here.
1495 long get_user_pages(unsigned long start, unsigned long nr_pages,
1496 unsigned int gup_flags, struct page **pages,
1497 struct vm_area_struct **vmas)
1499 return __gup_longterm_locked(current, current->mm, start, nr_pages,
1500 pages, vmas, gup_flags | FOLL_TOUCH);
1502 EXPORT_SYMBOL(get_user_pages);
1505 * populate_vma_page_range() - populate a range of pages in the vma.
1507 * @start: start address
1511 * This takes care of mlocking the pages too if VM_LOCKED is set.
1513 * return 0 on success, negative error code on error.
1515 * vma->vm_mm->mmap_sem must be held.
1517 * If @nonblocking is NULL, it may be held for read or write and will
1520 * If @nonblocking is non-NULL, it must held for read only and may be
1521 * released. If it's released, *@nonblocking will be set to 0.
1523 long populate_vma_page_range(struct vm_area_struct *vma,
1524 unsigned long start, unsigned long end, int *nonblocking)
1526 struct mm_struct *mm = vma->vm_mm;
1527 unsigned long nr_pages = (end - start) / PAGE_SIZE;
1530 VM_BUG_ON(start & ~PAGE_MASK);
1531 VM_BUG_ON(end & ~PAGE_MASK);
1532 VM_BUG_ON_VMA(start < vma->vm_start, vma);
1533 VM_BUG_ON_VMA(end > vma->vm_end, vma);
1534 VM_BUG_ON_MM(!rwsem_is_locked(&mm->mmap_sem), mm);
1536 gup_flags = FOLL_TOUCH | FOLL_POPULATE | FOLL_MLOCK;
1537 if (vma->vm_flags & VM_LOCKONFAULT)
1538 gup_flags &= ~FOLL_POPULATE;
1540 * We want to touch writable mappings with a write fault in order
1541 * to break COW, except for shared mappings because these don't COW
1542 * and we would not want to dirty them for nothing.
1544 if ((vma->vm_flags & (VM_WRITE | VM_SHARED)) == VM_WRITE)
1545 gup_flags |= FOLL_WRITE;
1548 * We want mlock to succeed for regions that have any permissions
1549 * other than PROT_NONE.
1551 if (vma->vm_flags & (VM_READ | VM_WRITE | VM_EXEC))
1552 gup_flags |= FOLL_FORCE;
1555 * We made sure addr is within a VMA, so the following will
1556 * not result in a stack expansion that recurses back here.
1558 return __get_user_pages(current, mm, start, nr_pages, gup_flags,
1559 NULL, NULL, nonblocking);
1563 * __mm_populate - populate and/or mlock pages within a range of address space.
1565 * This is used to implement mlock() and the MAP_POPULATE / MAP_LOCKED mmap
1566 * flags. VMAs must be already marked with the desired vm_flags, and
1567 * mmap_sem must not be held.
1569 int __mm_populate(unsigned long start, unsigned long len, int ignore_errors)
1571 struct mm_struct *mm = current->mm;
1572 unsigned long end, nstart, nend;
1573 struct vm_area_struct *vma = NULL;
1579 for (nstart = start; nstart < end; nstart = nend) {
1581 * We want to fault in pages for [nstart; end) address range.
1582 * Find first corresponding VMA.
1586 down_read(&mm->mmap_sem);
1587 vma = find_vma(mm, nstart);
1588 } else if (nstart >= vma->vm_end)
1590 if (!vma || vma->vm_start >= end)
1593 * Set [nstart; nend) to intersection of desired address
1594 * range with the first VMA. Also, skip undesirable VMA types.
1596 nend = min(end, vma->vm_end);
1597 if (vma->vm_flags & (VM_IO | VM_PFNMAP))
1599 if (nstart < vma->vm_start)
1600 nstart = vma->vm_start;
1602 * Now fault in a range of pages. populate_vma_page_range()
1603 * double checks the vma flags, so that it won't mlock pages
1604 * if the vma was already munlocked.
1606 ret = populate_vma_page_range(vma, nstart, nend, &locked);
1608 if (ignore_errors) {
1610 continue; /* continue at next VMA */
1614 nend = nstart + ret * PAGE_SIZE;
1618 up_read(&mm->mmap_sem);
1619 return ret; /* 0 or negative error code */
1623 * get_dump_page() - pin user page in memory while writing it to core dump
1624 * @addr: user address
1626 * Returns struct page pointer of user page pinned for dump,
1627 * to be freed afterwards by put_page().
1629 * Returns NULL on any kind of failure - a hole must then be inserted into
1630 * the corefile, to preserve alignment with its headers; and also returns
1631 * NULL wherever the ZERO_PAGE, or an anonymous pte_none, has been found -
1632 * allowing a hole to be left in the corefile to save diskspace.
1634 * Called without mmap_sem, but after all other threads have been killed.
1636 #ifdef CONFIG_ELF_CORE
1637 struct page *get_dump_page(unsigned long addr)
1639 struct vm_area_struct *vma;
1642 if (__get_user_pages(current, current->mm, addr, 1,
1643 FOLL_FORCE | FOLL_DUMP | FOLL_GET, &page, &vma,
1646 flush_cache_page(vma, addr, page_to_pfn(page));
1649 #endif /* CONFIG_ELF_CORE */
1654 * get_user_pages_fast attempts to pin user pages by walking the page
1655 * tables directly and avoids taking locks. Thus the walker needs to be
1656 * protected from page table pages being freed from under it, and should
1657 * block any THP splits.
1659 * One way to achieve this is to have the walker disable interrupts, and
1660 * rely on IPIs from the TLB flushing code blocking before the page table
1661 * pages are freed. This is unsuitable for architectures that do not need
1662 * to broadcast an IPI when invalidating TLBs.
1664 * Another way to achieve this is to batch up page table containing pages
1665 * belonging to more than one mm_user, then rcu_sched a callback to free those
1666 * pages. Disabling interrupts will allow the fast_gup walker to both block
1667 * the rcu_sched callback, and an IPI that we broadcast for splitting THPs
1668 * (which is a relatively rare event). The code below adopts this strategy.
1670 * Before activating this code, please be aware that the following assumptions
1671 * are currently made:
1673 * *) Either HAVE_RCU_TABLE_FREE is enabled, and tlb_remove_table() is used to
1674 * free pages containing page tables or TLB flushing requires IPI broadcast.
1676 * *) ptes can be read atomically by the architecture.
1678 * *) access_ok is sufficient to validate userspace address ranges.
1680 * The last two assumptions can be relaxed by the addition of helper functions.
1682 * This code is based heavily on the PowerPC implementation by Nick Piggin.
1684 #ifdef CONFIG_HAVE_GENERIC_GUP
1688 * We assume that the PTE can be read atomically. If this is not the case for
1689 * your architecture, please provide the helper.
1691 static inline pte_t gup_get_pte(pte_t *ptep)
1693 return READ_ONCE(*ptep);
1697 static void undo_dev_pagemap(int *nr, int nr_start, struct page **pages)
1699 while ((*nr) - nr_start) {
1700 struct page *page = pages[--(*nr)];
1702 ClearPageReferenced(page);
1708 * Return the compund head page with ref appropriately incremented,
1709 * or NULL if that failed.
1711 static inline struct page *try_get_compound_head(struct page *page, int refs)
1713 struct page *head = compound_head(page);
1714 if (WARN_ON_ONCE(page_ref_count(head) < 0))
1716 if (unlikely(!page_cache_add_speculative(head, refs)))
1721 #ifdef CONFIG_ARCH_HAS_PTE_SPECIAL
1722 static int gup_pte_range(pmd_t pmd, unsigned long addr, unsigned long end,
1723 unsigned int flags, struct page **pages, int *nr)
1725 struct dev_pagemap *pgmap = NULL;
1726 int nr_start = *nr, ret = 0;
1729 ptem = ptep = pte_offset_map(&pmd, addr);
1731 pte_t pte = gup_get_pte(ptep);
1732 struct page *head, *page;
1735 * Similar to the PMD case below, NUMA hinting must take slow
1736 * path using the pte_protnone check.
1738 if (pte_protnone(pte))
1741 if (!pte_access_permitted(pte, flags & FOLL_WRITE))
1744 if (pte_devmap(pte)) {
1745 if (unlikely(flags & FOLL_LONGTERM))
1748 pgmap = get_dev_pagemap(pte_pfn(pte), pgmap);
1749 if (unlikely(!pgmap)) {
1750 undo_dev_pagemap(nr, nr_start, pages);
1753 } else if (pte_special(pte))
1756 VM_BUG_ON(!pfn_valid(pte_pfn(pte)));
1757 page = pte_page(pte);
1759 head = try_get_compound_head(page, 1);
1763 if (unlikely(pte_val(pte) != pte_val(*ptep))) {
1768 VM_BUG_ON_PAGE(compound_head(page) != head, page);
1770 SetPageReferenced(page);
1774 } while (ptep++, addr += PAGE_SIZE, addr != end);
1780 put_dev_pagemap(pgmap);
1787 * If we can't determine whether or not a pte is special, then fail immediately
1788 * for ptes. Note, we can still pin HugeTLB and THP as these are guaranteed not
1791 * For a futex to be placed on a THP tail page, get_futex_key requires a
1792 * __get_user_pages_fast implementation that can pin pages. Thus it's still
1793 * useful to have gup_huge_pmd even if we can't operate on ptes.
1795 static int gup_pte_range(pmd_t pmd, unsigned long addr, unsigned long end,
1796 unsigned int flags, struct page **pages, int *nr)
1800 #endif /* CONFIG_ARCH_HAS_PTE_SPECIAL */
1802 #if defined(__HAVE_ARCH_PTE_DEVMAP) && defined(CONFIG_TRANSPARENT_HUGEPAGE)
1803 static int __gup_device_huge(unsigned long pfn, unsigned long addr,
1804 unsigned long end, struct page **pages, int *nr)
1807 struct dev_pagemap *pgmap = NULL;
1810 struct page *page = pfn_to_page(pfn);
1812 pgmap = get_dev_pagemap(pfn, pgmap);
1813 if (unlikely(!pgmap)) {
1814 undo_dev_pagemap(nr, nr_start, pages);
1817 SetPageReferenced(page);
1822 } while (addr += PAGE_SIZE, addr != end);
1825 put_dev_pagemap(pgmap);
1829 static int __gup_device_huge_pmd(pmd_t orig, pmd_t *pmdp, unsigned long addr,
1830 unsigned long end, struct page **pages, int *nr)
1832 unsigned long fault_pfn;
1835 fault_pfn = pmd_pfn(orig) + ((addr & ~PMD_MASK) >> PAGE_SHIFT);
1836 if (!__gup_device_huge(fault_pfn, addr, end, pages, nr))
1839 if (unlikely(pmd_val(orig) != pmd_val(*pmdp))) {
1840 undo_dev_pagemap(nr, nr_start, pages);
1846 static int __gup_device_huge_pud(pud_t orig, pud_t *pudp, unsigned long addr,
1847 unsigned long end, struct page **pages, int *nr)
1849 unsigned long fault_pfn;
1852 fault_pfn = pud_pfn(orig) + ((addr & ~PUD_MASK) >> PAGE_SHIFT);
1853 if (!__gup_device_huge(fault_pfn, addr, end, pages, nr))
1856 if (unlikely(pud_val(orig) != pud_val(*pudp))) {
1857 undo_dev_pagemap(nr, nr_start, pages);
1863 static int __gup_device_huge_pmd(pmd_t orig, pmd_t *pmdp, unsigned long addr,
1864 unsigned long end, struct page **pages, int *nr)
1870 static int __gup_device_huge_pud(pud_t pud, pud_t *pudp, unsigned long addr,
1871 unsigned long end, struct page **pages, int *nr)
1878 static int gup_huge_pmd(pmd_t orig, pmd_t *pmdp, unsigned long addr,
1879 unsigned long end, unsigned int flags, struct page **pages, int *nr)
1881 struct page *head, *page;
1884 if (!pmd_access_permitted(orig, flags & FOLL_WRITE))
1887 if (pmd_devmap(orig)) {
1888 if (unlikely(flags & FOLL_LONGTERM))
1890 return __gup_device_huge_pmd(orig, pmdp, addr, end, pages, nr);
1894 page = pmd_page(orig) + ((addr & ~PMD_MASK) >> PAGE_SHIFT);
1900 } while (addr += PAGE_SIZE, addr != end);
1902 head = try_get_compound_head(pmd_page(orig), refs);
1908 if (unlikely(pmd_val(orig) != pmd_val(*pmdp))) {
1915 SetPageReferenced(head);
1919 static int gup_huge_pud(pud_t orig, pud_t *pudp, unsigned long addr,
1920 unsigned long end, unsigned int flags, struct page **pages, int *nr)
1922 struct page *head, *page;
1925 if (!pud_access_permitted(orig, flags & FOLL_WRITE))
1928 if (pud_devmap(orig)) {
1929 if (unlikely(flags & FOLL_LONGTERM))
1931 return __gup_device_huge_pud(orig, pudp, addr, end, pages, nr);
1935 page = pud_page(orig) + ((addr & ~PUD_MASK) >> PAGE_SHIFT);
1941 } while (addr += PAGE_SIZE, addr != end);
1943 head = try_get_compound_head(pud_page(orig), refs);
1949 if (unlikely(pud_val(orig) != pud_val(*pudp))) {
1956 SetPageReferenced(head);
1960 static int gup_huge_pgd(pgd_t orig, pgd_t *pgdp, unsigned long addr,
1961 unsigned long end, unsigned int flags,
1962 struct page **pages, int *nr)
1965 struct page *head, *page;
1967 if (!pgd_access_permitted(orig, flags & FOLL_WRITE))
1970 BUILD_BUG_ON(pgd_devmap(orig));
1972 page = pgd_page(orig) + ((addr & ~PGDIR_MASK) >> PAGE_SHIFT);
1978 } while (addr += PAGE_SIZE, addr != end);
1980 head = try_get_compound_head(pgd_page(orig), refs);
1986 if (unlikely(pgd_val(orig) != pgd_val(*pgdp))) {
1993 SetPageReferenced(head);
1997 static int gup_pmd_range(pud_t pud, unsigned long addr, unsigned long end,
1998 unsigned int flags, struct page **pages, int *nr)
2003 pmdp = pmd_offset(&pud, addr);
2005 pmd_t pmd = READ_ONCE(*pmdp);
2007 next = pmd_addr_end(addr, end);
2008 if (!pmd_present(pmd))
2011 if (unlikely(pmd_trans_huge(pmd) || pmd_huge(pmd) ||
2014 * NUMA hinting faults need to be handled in the GUP
2015 * slowpath for accounting purposes and so that they
2016 * can be serialised against THP migration.
2018 if (pmd_protnone(pmd))
2021 if (!gup_huge_pmd(pmd, pmdp, addr, next, flags,
2025 } else if (unlikely(is_hugepd(__hugepd(pmd_val(pmd))))) {
2027 * architecture have different format for hugetlbfs
2028 * pmd format and THP pmd format
2030 if (!gup_huge_pd(__hugepd(pmd_val(pmd)), addr,
2031 PMD_SHIFT, next, flags, pages, nr))
2033 } else if (!gup_pte_range(pmd, addr, next, flags, pages, nr))
2035 } while (pmdp++, addr = next, addr != end);
2040 static int gup_pud_range(p4d_t p4d, unsigned long addr, unsigned long end,
2041 unsigned int flags, struct page **pages, int *nr)
2046 pudp = pud_offset(&p4d, addr);
2048 pud_t pud = READ_ONCE(*pudp);
2050 next = pud_addr_end(addr, end);
2053 if (unlikely(pud_huge(pud))) {
2054 if (!gup_huge_pud(pud, pudp, addr, next, flags,
2057 } else if (unlikely(is_hugepd(__hugepd(pud_val(pud))))) {
2058 if (!gup_huge_pd(__hugepd(pud_val(pud)), addr,
2059 PUD_SHIFT, next, flags, pages, nr))
2061 } else if (!gup_pmd_range(pud, addr, next, flags, pages, nr))
2063 } while (pudp++, addr = next, addr != end);
2068 static int gup_p4d_range(pgd_t pgd, unsigned long addr, unsigned long end,
2069 unsigned int flags, struct page **pages, int *nr)
2074 p4dp = p4d_offset(&pgd, addr);
2076 p4d_t p4d = READ_ONCE(*p4dp);
2078 next = p4d_addr_end(addr, end);
2081 BUILD_BUG_ON(p4d_huge(p4d));
2082 if (unlikely(is_hugepd(__hugepd(p4d_val(p4d))))) {
2083 if (!gup_huge_pd(__hugepd(p4d_val(p4d)), addr,
2084 P4D_SHIFT, next, flags, pages, nr))
2086 } else if (!gup_pud_range(p4d, addr, next, flags, pages, nr))
2088 } while (p4dp++, addr = next, addr != end);
2093 static void gup_pgd_range(unsigned long addr, unsigned long end,
2094 unsigned int flags, struct page **pages, int *nr)
2099 pgdp = pgd_offset(current->mm, addr);
2101 pgd_t pgd = READ_ONCE(*pgdp);
2103 next = pgd_addr_end(addr, end);
2106 if (unlikely(pgd_huge(pgd))) {
2107 if (!gup_huge_pgd(pgd, pgdp, addr, next, flags,
2110 } else if (unlikely(is_hugepd(__hugepd(pgd_val(pgd))))) {
2111 if (!gup_huge_pd(__hugepd(pgd_val(pgd)), addr,
2112 PGDIR_SHIFT, next, flags, pages, nr))
2114 } else if (!gup_p4d_range(pgd, addr, next, flags, pages, nr))
2116 } while (pgdp++, addr = next, addr != end);
2119 #ifndef gup_fast_permitted
2121 * Check if it's allowed to use __get_user_pages_fast() for the range, or
2122 * we need to fall back to the slow version:
2124 bool gup_fast_permitted(unsigned long start, int nr_pages)
2126 unsigned long len, end;
2128 len = (unsigned long) nr_pages << PAGE_SHIFT;
2130 return end >= start;
2135 * Like get_user_pages_fast() except it's IRQ-safe in that it won't fall back to
2137 * Note a difference with get_user_pages_fast: this always returns the
2138 * number of pages pinned, 0 if no pages were pinned.
2140 int __get_user_pages_fast(unsigned long start, int nr_pages, int write,
2141 struct page **pages)
2143 unsigned long len, end;
2144 unsigned long flags;
2148 len = (unsigned long) nr_pages << PAGE_SHIFT;
2151 if (unlikely(!access_ok((void __user *)start, len)))
2155 * Disable interrupts. We use the nested form as we can already have
2156 * interrupts disabled by get_futex_key.
2158 * With interrupts disabled, we block page table pages from being
2159 * freed from under us. See struct mmu_table_batch comments in
2160 * include/asm-generic/tlb.h for more details.
2162 * We do not adopt an rcu_read_lock(.) here as we also want to
2163 * block IPIs that come from THPs splitting.
2166 if (gup_fast_permitted(start, nr_pages)) {
2167 local_irq_save(flags);
2168 gup_pgd_range(start, end, write ? FOLL_WRITE : 0, pages, &nr);
2169 local_irq_restore(flags);
2175 static int __gup_longterm_unlocked(unsigned long start, int nr_pages,
2176 unsigned int gup_flags, struct page **pages)
2181 * FIXME: FOLL_LONGTERM does not work with
2182 * get_user_pages_unlocked() (see comments in that function)
2184 if (gup_flags & FOLL_LONGTERM) {
2185 down_read(¤t->mm->mmap_sem);
2186 ret = __gup_longterm_locked(current, current->mm,
2188 pages, NULL, gup_flags);
2189 up_read(¤t->mm->mmap_sem);
2191 ret = get_user_pages_unlocked(start, nr_pages,
2199 * get_user_pages_fast() - pin user pages in memory
2200 * @start: starting user address
2201 * @nr_pages: number of pages from start to pin
2202 * @gup_flags: flags modifying pin behaviour
2203 * @pages: array that receives pointers to the pages pinned.
2204 * Should be at least nr_pages long.
2206 * Attempt to pin user pages in memory without taking mm->mmap_sem.
2207 * If not successful, it will fall back to taking the lock and
2208 * calling get_user_pages().
2210 * Returns number of pages pinned. This may be fewer than the number
2211 * requested. If nr_pages is 0 or negative, returns 0. If no pages
2212 * were pinned, returns -errno.
2214 int get_user_pages_fast(unsigned long start, int nr_pages,
2215 unsigned int gup_flags, struct page **pages)
2217 unsigned long addr, len, end;
2218 int nr = 0, ret = 0;
2222 len = (unsigned long) nr_pages << PAGE_SHIFT;
2228 if (unlikely(!access_ok((void __user *)start, len)))
2231 if (gup_fast_permitted(start, nr_pages)) {
2232 local_irq_disable();
2233 gup_pgd_range(addr, end, gup_flags, pages, &nr);
2238 if (nr < nr_pages) {
2239 /* Try to get the remaining pages with get_user_pages */
2240 start += nr << PAGE_SHIFT;
2243 ret = __gup_longterm_unlocked(start, nr_pages - nr,
2246 /* Have to be a bit careful with return values */
2258 #endif /* CONFIG_HAVE_GENERIC_GUP */