1 #include <linux/kernel.h>
2 #include <linux/errno.h>
4 #include <linux/spinlock.h>
6 #include <linux/hugetlb.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 <asm/pgtable.h>
19 static struct page
*no_page_table(struct vm_area_struct
*vma
,
23 * When core dumping an enormous anonymous area that nobody
24 * has touched so far, we don't want to allocate unnecessary pages or
25 * page tables. Return error instead of NULL to skip handle_mm_fault,
26 * then get_dump_page() will return NULL to leave a hole in the dump.
27 * But we can only make this optimization where a hole would surely
28 * be zero-filled if handle_mm_fault() actually did handle it.
30 if ((flags
& FOLL_DUMP
) && (!vma
->vm_ops
|| !vma
->vm_ops
->fault
))
31 return ERR_PTR(-EFAULT
);
35 static struct page
*follow_page_pte(struct vm_area_struct
*vma
,
36 unsigned long address
, pmd_t
*pmd
, unsigned int flags
)
38 struct mm_struct
*mm
= vma
->vm_mm
;
44 if (unlikely(pmd_bad(*pmd
)))
45 return no_page_table(vma
, flags
);
47 ptep
= pte_offset_map_lock(mm
, pmd
, address
, &ptl
);
49 if (!pte_present(pte
)) {
52 * KSM's break_ksm() relies upon recognizing a ksm page
53 * even while it is being migrated, so for that case we
54 * need migration_entry_wait().
56 if (likely(!(flags
& FOLL_MIGRATION
)))
58 if (pte_none(pte
) || pte_file(pte
))
60 entry
= pte_to_swp_entry(pte
);
61 if (!is_migration_entry(entry
))
63 pte_unmap_unlock(ptep
, ptl
);
64 migration_entry_wait(mm
, pmd
, address
);
67 if ((flags
& FOLL_NUMA
) && pte_numa(pte
))
69 if ((flags
& FOLL_WRITE
) && !pte_write(pte
)) {
70 pte_unmap_unlock(ptep
, ptl
);
74 page
= vm_normal_page(vma
, address
, pte
);
75 if (unlikely(!page
)) {
76 if ((flags
& FOLL_DUMP
) ||
77 !is_zero_pfn(pte_pfn(pte
)))
84 if (flags
& FOLL_TOUCH
) {
85 if ((flags
& FOLL_WRITE
) &&
86 !pte_dirty(pte
) && !PageDirty(page
))
89 * pte_mkyoung() would be more correct here, but atomic care
90 * is needed to avoid losing the dirty bit: it is easier to use
91 * mark_page_accessed().
93 mark_page_accessed(page
);
95 if ((flags
& FOLL_MLOCK
) && (vma
->vm_flags
& VM_LOCKED
)) {
97 * The preliminary mapping check is mainly to avoid the
98 * pointless overhead of lock_page on the ZERO_PAGE
99 * which might bounce very badly if there is contention.
101 * If the page is already locked, we don't need to
102 * handle it now - vmscan will handle it later if and
103 * when it attempts to reclaim the page.
105 if (page
->mapping
&& trylock_page(page
)) {
106 lru_add_drain(); /* push cached pages to LRU */
108 * Because we lock page here, and migration is
109 * blocked by the pte's page reference, and we
110 * know the page is still mapped, we don't even
111 * need to check for file-cache page truncation.
113 mlock_vma_page(page
);
117 pte_unmap_unlock(ptep
, ptl
);
120 pte_unmap_unlock(ptep
, ptl
);
121 return ERR_PTR(-EFAULT
);
124 pte_unmap_unlock(ptep
, ptl
);
127 return no_page_table(vma
, flags
);
131 * follow_page_mask - look up a page descriptor from a user-virtual address
132 * @vma: vm_area_struct mapping @address
133 * @address: virtual address to look up
134 * @flags: flags modifying lookup behaviour
135 * @page_mask: on output, *page_mask is set according to the size of the page
137 * @flags can have FOLL_ flags set, defined in <linux/mm.h>
139 * Returns the mapped (struct page *), %NULL if no mapping exists, or
140 * an error pointer if there is a mapping to something not represented
141 * by a page descriptor (see also vm_normal_page()).
143 struct page
*follow_page_mask(struct vm_area_struct
*vma
,
144 unsigned long address
, unsigned int flags
,
145 unsigned int *page_mask
)
152 struct mm_struct
*mm
= vma
->vm_mm
;
156 page
= follow_huge_addr(mm
, address
, flags
& FOLL_WRITE
);
158 BUG_ON(flags
& FOLL_GET
);
162 pgd
= pgd_offset(mm
, address
);
163 if (pgd_none(*pgd
) || unlikely(pgd_bad(*pgd
)))
164 return no_page_table(vma
, flags
);
166 pud
= pud_offset(pgd
, address
);
168 return no_page_table(vma
, flags
);
169 if (pud_huge(*pud
) && vma
->vm_flags
& VM_HUGETLB
) {
170 page
= follow_huge_pud(mm
, address
, pud
, flags
);
173 return no_page_table(vma
, flags
);
175 if (unlikely(pud_bad(*pud
)))
176 return no_page_table(vma
, flags
);
178 pmd
= pmd_offset(pud
, address
);
180 return no_page_table(vma
, flags
);
181 if (pmd_huge(*pmd
) && vma
->vm_flags
& VM_HUGETLB
) {
182 page
= follow_huge_pmd(mm
, address
, pmd
, flags
);
185 return no_page_table(vma
, flags
);
187 if ((flags
& FOLL_NUMA
) && pmd_numa(*pmd
))
188 return no_page_table(vma
, flags
);
189 if (pmd_trans_huge(*pmd
)) {
190 if (flags
& FOLL_SPLIT
) {
191 split_huge_page_pmd(vma
, address
, pmd
);
192 return follow_page_pte(vma
, address
, pmd
, flags
);
194 ptl
= pmd_lock(mm
, pmd
);
195 if (likely(pmd_trans_huge(*pmd
))) {
196 if (unlikely(pmd_trans_splitting(*pmd
))) {
198 wait_split_huge_page(vma
->anon_vma
, pmd
);
200 page
= follow_trans_huge_pmd(vma
, address
,
203 *page_mask
= HPAGE_PMD_NR
- 1;
209 return follow_page_pte(vma
, address
, pmd
, flags
);
212 static int get_gate_page(struct mm_struct
*mm
, unsigned long address
,
213 unsigned int gup_flags
, struct vm_area_struct
**vma
,
222 /* user gate pages are read-only */
223 if (gup_flags
& FOLL_WRITE
)
225 if (address
> TASK_SIZE
)
226 pgd
= pgd_offset_k(address
);
228 pgd
= pgd_offset_gate(mm
, address
);
229 BUG_ON(pgd_none(*pgd
));
230 pud
= pud_offset(pgd
, address
);
231 BUG_ON(pud_none(*pud
));
232 pmd
= pmd_offset(pud
, address
);
235 VM_BUG_ON(pmd_trans_huge(*pmd
));
236 pte
= pte_offset_map(pmd
, address
);
239 *vma
= get_gate_vma(mm
);
242 *page
= vm_normal_page(*vma
, address
, *pte
);
244 if ((gup_flags
& FOLL_DUMP
) || !is_zero_pfn(pte_pfn(*pte
)))
246 *page
= pte_page(*pte
);
257 * mmap_sem must be held on entry. If @nonblocking != NULL and
258 * *@flags does not include FOLL_NOWAIT, the mmap_sem may be released.
259 * If it is, *@nonblocking will be set to 0 and -EBUSY returned.
261 static int faultin_page(struct task_struct
*tsk
, struct vm_area_struct
*vma
,
262 unsigned long address
, unsigned int *flags
, int *nonblocking
)
264 struct mm_struct
*mm
= vma
->vm_mm
;
265 unsigned int fault_flags
= 0;
268 /* For mlock, just skip the stack guard page. */
269 if ((*flags
& FOLL_MLOCK
) &&
270 (stack_guard_page_start(vma
, address
) ||
271 stack_guard_page_end(vma
, address
+ PAGE_SIZE
)))
273 if (*flags
& FOLL_WRITE
)
274 fault_flags
|= FAULT_FLAG_WRITE
;
276 fault_flags
|= FAULT_FLAG_ALLOW_RETRY
;
277 if (*flags
& FOLL_NOWAIT
)
278 fault_flags
|= FAULT_FLAG_ALLOW_RETRY
| FAULT_FLAG_RETRY_NOWAIT
;
279 if (*flags
& FOLL_TRIED
) {
280 VM_WARN_ON_ONCE(fault_flags
& FAULT_FLAG_ALLOW_RETRY
);
281 fault_flags
|= FAULT_FLAG_TRIED
;
284 ret
= handle_mm_fault(mm
, vma
, address
, fault_flags
);
285 if (ret
& VM_FAULT_ERROR
) {
286 if (ret
& VM_FAULT_OOM
)
288 if (ret
& (VM_FAULT_HWPOISON
| VM_FAULT_HWPOISON_LARGE
))
289 return *flags
& FOLL_HWPOISON
? -EHWPOISON
: -EFAULT
;
290 if (ret
& (VM_FAULT_SIGBUS
| VM_FAULT_SIGSEGV
))
296 if (ret
& VM_FAULT_MAJOR
)
302 if (ret
& VM_FAULT_RETRY
) {
309 * The VM_FAULT_WRITE bit tells us that do_wp_page has broken COW when
310 * necessary, even if maybe_mkwrite decided not to set pte_write. We
311 * can thus safely do subsequent page lookups as if they were reads.
312 * But only do so when looping for pte_write is futile: in some cases
313 * userspace may also be wanting to write to the gotten user page,
314 * which a read fault here might prevent (a readonly page might get
315 * reCOWed by userspace write).
317 if ((ret
& VM_FAULT_WRITE
) && !(vma
->vm_flags
& VM_WRITE
))
318 *flags
&= ~FOLL_WRITE
;
322 static int check_vma_flags(struct vm_area_struct
*vma
, unsigned long gup_flags
)
324 vm_flags_t vm_flags
= vma
->vm_flags
;
326 if (vm_flags
& (VM_IO
| VM_PFNMAP
))
329 if (gup_flags
& FOLL_WRITE
) {
330 if (!(vm_flags
& VM_WRITE
)) {
331 if (!(gup_flags
& FOLL_FORCE
))
334 * We used to let the write,force case do COW in a
335 * VM_MAYWRITE VM_SHARED !VM_WRITE vma, so ptrace could
336 * set a breakpoint in a read-only mapping of an
337 * executable, without corrupting the file (yet only
338 * when that file had been opened for writing!).
339 * Anon pages in shared mappings are surprising: now
342 if (!is_cow_mapping(vm_flags
)) {
343 WARN_ON_ONCE(vm_flags
& VM_MAYWRITE
);
347 } else if (!(vm_flags
& VM_READ
)) {
348 if (!(gup_flags
& FOLL_FORCE
))
351 * Is there actually any vma we can reach here which does not
352 * have VM_MAYREAD set?
354 if (!(vm_flags
& VM_MAYREAD
))
361 * __get_user_pages() - pin user pages in memory
362 * @tsk: task_struct of target task
363 * @mm: mm_struct of target mm
364 * @start: starting user address
365 * @nr_pages: number of pages from start to pin
366 * @gup_flags: flags modifying pin behaviour
367 * @pages: array that receives pointers to the pages pinned.
368 * Should be at least nr_pages long. Or NULL, if caller
369 * only intends to ensure the pages are faulted in.
370 * @vmas: array of pointers to vmas corresponding to each page.
371 * Or NULL if the caller does not require them.
372 * @nonblocking: whether waiting for disk IO or mmap_sem contention
374 * Returns number of pages pinned. This may be fewer than the number
375 * requested. If nr_pages is 0 or negative, returns 0. If no pages
376 * were pinned, returns -errno. Each page returned must be released
377 * with a put_page() call when it is finished with. vmas will only
378 * remain valid while mmap_sem is held.
380 * Must be called with mmap_sem held. It may be released. See below.
382 * __get_user_pages walks a process's page tables and takes a reference to
383 * each struct page that each user address corresponds to at a given
384 * instant. That is, it takes the page that would be accessed if a user
385 * thread accesses the given user virtual address at that instant.
387 * This does not guarantee that the page exists in the user mappings when
388 * __get_user_pages returns, and there may even be a completely different
389 * page there in some cases (eg. if mmapped pagecache has been invalidated
390 * and subsequently re faulted). However it does guarantee that the page
391 * won't be freed completely. And mostly callers simply care that the page
392 * contains data that was valid *at some point in time*. Typically, an IO
393 * or similar operation cannot guarantee anything stronger anyway because
394 * locks can't be held over the syscall boundary.
396 * If @gup_flags & FOLL_WRITE == 0, the page must not be written to. If
397 * the page is written to, set_page_dirty (or set_page_dirty_lock, as
398 * appropriate) must be called after the page is finished with, and
399 * before put_page is called.
401 * If @nonblocking != NULL, __get_user_pages will not wait for disk IO
402 * or mmap_sem contention, and if waiting is needed to pin all pages,
403 * *@nonblocking will be set to 0. Further, if @gup_flags does not
404 * include FOLL_NOWAIT, the mmap_sem will be released via up_read() in
407 * A caller using such a combination of @nonblocking and @gup_flags
408 * must therefore hold the mmap_sem for reading only, and recognize
409 * when it's been released. Otherwise, it must be held for either
410 * reading or writing and will not be released.
412 * In most cases, get_user_pages or get_user_pages_fast should be used
413 * instead of __get_user_pages. __get_user_pages should be used only if
414 * you need some special @gup_flags.
416 long __get_user_pages(struct task_struct
*tsk
, struct mm_struct
*mm
,
417 unsigned long start
, unsigned long nr_pages
,
418 unsigned int gup_flags
, struct page
**pages
,
419 struct vm_area_struct
**vmas
, int *nonblocking
)
422 unsigned int page_mask
;
423 struct vm_area_struct
*vma
= NULL
;
428 VM_BUG_ON(!!pages
!= !!(gup_flags
& FOLL_GET
));
431 * If FOLL_FORCE is set then do not force a full fault as the hinting
432 * fault information is unrelated to the reference behaviour of a task
433 * using the address space
435 if (!(gup_flags
& FOLL_FORCE
))
436 gup_flags
|= FOLL_NUMA
;
440 unsigned int foll_flags
= gup_flags
;
441 unsigned int page_increm
;
443 /* first iteration or cross vma bound */
444 if (!vma
|| start
>= vma
->vm_end
) {
445 vma
= find_extend_vma(mm
, start
);
446 if (!vma
&& in_gate_area(mm
, start
)) {
448 ret
= get_gate_page(mm
, start
& PAGE_MASK
,
450 pages
? &pages
[i
] : NULL
);
457 if (!vma
|| check_vma_flags(vma
, gup_flags
))
458 return i
? : -EFAULT
;
459 if (is_vm_hugetlb_page(vma
)) {
460 i
= follow_hugetlb_page(mm
, vma
, pages
, vmas
,
461 &start
, &nr_pages
, i
,
468 * If we have a pending SIGKILL, don't keep faulting pages and
469 * potentially allocating memory.
471 if (unlikely(fatal_signal_pending(current
)))
472 return i
? i
: -ERESTARTSYS
;
474 page
= follow_page_mask(vma
, start
, foll_flags
, &page_mask
);
477 ret
= faultin_page(tsk
, vma
, start
, &foll_flags
,
494 return i
? i
: PTR_ERR(page
);
497 flush_anon_page(vma
, page
, start
);
498 flush_dcache_page(page
);
506 page_increm
= 1 + (~(start
>> PAGE_SHIFT
) & page_mask
);
507 if (page_increm
> nr_pages
)
508 page_increm
= nr_pages
;
510 start
+= page_increm
* PAGE_SIZE
;
511 nr_pages
-= page_increm
;
515 EXPORT_SYMBOL(__get_user_pages
);
518 * fixup_user_fault() - manually resolve a user page fault
519 * @tsk: the task_struct to use for page fault accounting, or
520 * NULL if faults are not to be recorded.
521 * @mm: mm_struct of target mm
522 * @address: user address
523 * @fault_flags:flags to pass down to handle_mm_fault()
525 * This is meant to be called in the specific scenario where for locking reasons
526 * we try to access user memory in atomic context (within a pagefault_disable()
527 * section), this returns -EFAULT, and we want to resolve the user fault before
530 * Typically this is meant to be used by the futex code.
532 * The main difference with get_user_pages() is that this function will
533 * unconditionally call handle_mm_fault() which will in turn perform all the
534 * necessary SW fixup of the dirty and young bits in the PTE, while
535 * handle_mm_fault() only guarantees to update these in the struct page.
537 * This is important for some architectures where those bits also gate the
538 * access permission to the page because they are maintained in software. On
539 * such architectures, gup() will not be enough to make a subsequent access
542 * This has the same semantics wrt the @mm->mmap_sem as does filemap_fault().
544 int fixup_user_fault(struct task_struct
*tsk
, struct mm_struct
*mm
,
545 unsigned long address
, unsigned int fault_flags
)
547 struct vm_area_struct
*vma
;
551 vma
= find_extend_vma(mm
, address
);
552 if (!vma
|| address
< vma
->vm_start
)
555 vm_flags
= (fault_flags
& FAULT_FLAG_WRITE
) ? VM_WRITE
: VM_READ
;
556 if (!(vm_flags
& vma
->vm_flags
))
559 ret
= handle_mm_fault(mm
, vma
, address
, fault_flags
);
560 if (ret
& VM_FAULT_ERROR
) {
561 if (ret
& VM_FAULT_OOM
)
563 if (ret
& (VM_FAULT_HWPOISON
| VM_FAULT_HWPOISON_LARGE
))
565 if (ret
& (VM_FAULT_SIGBUS
| VM_FAULT_SIGSEGV
))
570 if (ret
& VM_FAULT_MAJOR
)
579 * get_user_pages() - pin user pages in memory
580 * @tsk: the task_struct to use for page fault accounting, or
581 * NULL if faults are not to be recorded.
582 * @mm: mm_struct of target mm
583 * @start: starting user address
584 * @nr_pages: number of pages from start to pin
585 * @write: whether pages will be written to by the caller
586 * @force: whether to force access even when user mapping is currently
587 * protected (but never forces write access to shared mapping).
588 * @pages: array that receives pointers to the pages pinned.
589 * Should be at least nr_pages long. Or NULL, if caller
590 * only intends to ensure the pages are faulted in.
591 * @vmas: array of pointers to vmas corresponding to each page.
592 * Or NULL if the caller does not require them.
594 * Returns number of pages pinned. This may be fewer than the number
595 * requested. If nr_pages is 0 or negative, returns 0. If no pages
596 * were pinned, returns -errno. Each page returned must be released
597 * with a put_page() call when it is finished with. vmas will only
598 * remain valid while mmap_sem is held.
600 * Must be called with mmap_sem held for read or write.
602 * get_user_pages walks a process's page tables and takes a reference to
603 * each struct page that each user address corresponds to at a given
604 * instant. That is, it takes the page that would be accessed if a user
605 * thread accesses the given user virtual address at that instant.
607 * This does not guarantee that the page exists in the user mappings when
608 * get_user_pages returns, and there may even be a completely different
609 * page there in some cases (eg. if mmapped pagecache has been invalidated
610 * and subsequently re faulted). However it does guarantee that the page
611 * won't be freed completely. And mostly callers simply care that the page
612 * contains data that was valid *at some point in time*. Typically, an IO
613 * or similar operation cannot guarantee anything stronger anyway because
614 * locks can't be held over the syscall boundary.
616 * If write=0, the page must not be written to. If the page is written to,
617 * set_page_dirty (or set_page_dirty_lock, as appropriate) must be called
618 * after the page is finished with, and before put_page is called.
620 * get_user_pages is typically used for fewer-copy IO operations, to get a
621 * handle on the memory by some means other than accesses via the user virtual
622 * addresses. The pages may be submitted for DMA to devices or accessed via
623 * their kernel linear mapping (via the kmap APIs). Care should be taken to
624 * use the correct cache flushing APIs.
626 * See also get_user_pages_fast, for performance critical applications.
628 long get_user_pages(struct task_struct
*tsk
, struct mm_struct
*mm
,
629 unsigned long start
, unsigned long nr_pages
, int write
,
630 int force
, struct page
**pages
, struct vm_area_struct
**vmas
)
632 int flags
= FOLL_TOUCH
;
641 return __get_user_pages(tsk
, mm
, start
, nr_pages
, flags
, pages
, vmas
,
644 EXPORT_SYMBOL(get_user_pages
);
647 * get_dump_page() - pin user page in memory while writing it to core dump
648 * @addr: user address
650 * Returns struct page pointer of user page pinned for dump,
651 * to be freed afterwards by page_cache_release() or put_page().
653 * Returns NULL on any kind of failure - a hole must then be inserted into
654 * the corefile, to preserve alignment with its headers; and also returns
655 * NULL wherever the ZERO_PAGE, or an anonymous pte_none, has been found -
656 * allowing a hole to be left in the corefile to save diskspace.
658 * Called without mmap_sem, but after all other threads have been killed.
660 #ifdef CONFIG_ELF_CORE
661 struct page
*get_dump_page(unsigned long addr
)
663 struct vm_area_struct
*vma
;
666 if (__get_user_pages(current
, current
->mm
, addr
, 1,
667 FOLL_FORCE
| FOLL_DUMP
| FOLL_GET
, &page
, &vma
,
670 flush_cache_page(vma
, addr
, page_to_pfn(page
));
673 #endif /* CONFIG_ELF_CORE */
676 * Generic RCU Fast GUP
678 * get_user_pages_fast attempts to pin user pages by walking the page
679 * tables directly and avoids taking locks. Thus the walker needs to be
680 * protected from page table pages being freed from under it, and should
681 * block any THP splits.
683 * One way to achieve this is to have the walker disable interrupts, and
684 * rely on IPIs from the TLB flushing code blocking before the page table
685 * pages are freed. This is unsuitable for architectures that do not need
686 * to broadcast an IPI when invalidating TLBs.
688 * Another way to achieve this is to batch up page table containing pages
689 * belonging to more than one mm_user, then rcu_sched a callback to free those
690 * pages. Disabling interrupts will allow the fast_gup walker to both block
691 * the rcu_sched callback, and an IPI that we broadcast for splitting THPs
692 * (which is a relatively rare event). The code below adopts this strategy.
694 * Before activating this code, please be aware that the following assumptions
695 * are currently made:
697 * *) HAVE_RCU_TABLE_FREE is enabled, and tlb_remove_table is used to free
698 * pages containing page tables.
700 * *) THP splits will broadcast an IPI, this can be achieved by overriding
701 * pmdp_splitting_flush.
703 * *) ptes can be read atomically by the architecture.
705 * *) access_ok is sufficient to validate userspace address ranges.
707 * The last two assumptions can be relaxed by the addition of helper functions.
709 * This code is based heavily on the PowerPC implementation by Nick Piggin.
711 #ifdef CONFIG_HAVE_GENERIC_RCU_GUP
713 #ifdef __HAVE_ARCH_PTE_SPECIAL
714 static int gup_pte_range(pmd_t pmd
, unsigned long addr
, unsigned long end
,
715 int write
, struct page
**pages
, int *nr
)
720 ptem
= ptep
= pte_offset_map(&pmd
, addr
);
723 * In the line below we are assuming that the pte can be read
724 * atomically. If this is not the case for your architecture,
725 * please wrap this in a helper function!
727 * for an example see gup_get_pte in arch/x86/mm/gup.c
729 pte_t pte
= ACCESS_ONCE(*ptep
);
733 * Similar to the PMD case below, NUMA hinting must take slow
736 if (!pte_present(pte
) || pte_special(pte
) ||
737 pte_numa(pte
) || (write
&& !pte_write(pte
)))
740 VM_BUG_ON(!pfn_valid(pte_pfn(pte
)));
741 page
= pte_page(pte
);
743 if (!page_cache_get_speculative(page
))
746 if (unlikely(pte_val(pte
) != pte_val(*ptep
))) {
754 } while (ptep
++, addr
+= PAGE_SIZE
, addr
!= end
);
765 * If we can't determine whether or not a pte is special, then fail immediately
766 * for ptes. Note, we can still pin HugeTLB and THP as these are guaranteed not
769 * For a futex to be placed on a THP tail page, get_futex_key requires a
770 * __get_user_pages_fast implementation that can pin pages. Thus it's still
771 * useful to have gup_huge_pmd even if we can't operate on ptes.
773 static int gup_pte_range(pmd_t pmd
, unsigned long addr
, unsigned long end
,
774 int write
, struct page
**pages
, int *nr
)
778 #endif /* __HAVE_ARCH_PTE_SPECIAL */
780 static int gup_huge_pmd(pmd_t orig
, pmd_t
*pmdp
, unsigned long addr
,
781 unsigned long end
, int write
, struct page
**pages
, int *nr
)
783 struct page
*head
, *page
, *tail
;
786 if (write
&& !pmd_write(orig
))
790 head
= pmd_page(orig
);
791 page
= head
+ ((addr
& ~PMD_MASK
) >> PAGE_SHIFT
);
794 VM_BUG_ON_PAGE(compound_head(page
) != head
, page
);
799 } while (addr
+= PAGE_SIZE
, addr
!= end
);
801 if (!page_cache_add_speculative(head
, refs
)) {
806 if (unlikely(pmd_val(orig
) != pmd_val(*pmdp
))) {
814 * Any tail pages need their mapcount reference taken before we
815 * return. (This allows the THP code to bump their ref count when
816 * they are split into base pages).
820 get_huge_page_tail(tail
);
827 static int gup_huge_pud(pud_t orig
, pud_t
*pudp
, unsigned long addr
,
828 unsigned long end
, int write
, struct page
**pages
, int *nr
)
830 struct page
*head
, *page
, *tail
;
833 if (write
&& !pud_write(orig
))
837 head
= pud_page(orig
);
838 page
= head
+ ((addr
& ~PUD_MASK
) >> PAGE_SHIFT
);
841 VM_BUG_ON_PAGE(compound_head(page
) != head
, page
);
846 } while (addr
+= PAGE_SIZE
, addr
!= end
);
848 if (!page_cache_add_speculative(head
, refs
)) {
853 if (unlikely(pud_val(orig
) != pud_val(*pudp
))) {
862 get_huge_page_tail(tail
);
869 static int gup_pmd_range(pud_t pud
, unsigned long addr
, unsigned long end
,
870 int write
, struct page
**pages
, int *nr
)
875 pmdp
= pmd_offset(&pud
, addr
);
877 pmd_t pmd
= ACCESS_ONCE(*pmdp
);
879 next
= pmd_addr_end(addr
, end
);
880 if (pmd_none(pmd
) || pmd_trans_splitting(pmd
))
883 if (unlikely(pmd_trans_huge(pmd
) || pmd_huge(pmd
))) {
885 * NUMA hinting faults need to be handled in the GUP
886 * slowpath for accounting purposes and so that they
887 * can be serialised against THP migration.
892 if (!gup_huge_pmd(pmd
, pmdp
, addr
, next
, write
,
896 } else if (!gup_pte_range(pmd
, addr
, next
, write
, pages
, nr
))
898 } while (pmdp
++, addr
= next
, addr
!= end
);
903 static int gup_pud_range(pgd_t
*pgdp
, unsigned long addr
, unsigned long end
,
904 int write
, struct page
**pages
, int *nr
)
909 pudp
= pud_offset(pgdp
, addr
);
911 pud_t pud
= ACCESS_ONCE(*pudp
);
913 next
= pud_addr_end(addr
, end
);
917 if (!gup_huge_pud(pud
, pudp
, addr
, next
, write
,
920 } else if (!gup_pmd_range(pud
, addr
, next
, write
, pages
, nr
))
922 } while (pudp
++, addr
= next
, addr
!= end
);
928 * Like get_user_pages_fast() except it's IRQ-safe in that it won't fall back to
929 * the regular GUP. It will only return non-negative values.
931 int __get_user_pages_fast(unsigned long start
, int nr_pages
, int write
,
934 struct mm_struct
*mm
= current
->mm
;
935 unsigned long addr
, len
, end
;
936 unsigned long next
, flags
;
942 len
= (unsigned long) nr_pages
<< PAGE_SHIFT
;
945 if (unlikely(!access_ok(write
? VERIFY_WRITE
: VERIFY_READ
,
950 * Disable interrupts. We use the nested form as we can already have
951 * interrupts disabled by get_futex_key.
953 * With interrupts disabled, we block page table pages from being
954 * freed from under us. See mmu_gather_tlb in asm-generic/tlb.h
957 * We do not adopt an rcu_read_lock(.) here as we also want to
958 * block IPIs that come from THPs splitting.
961 local_irq_save(flags
);
962 pgdp
= pgd_offset(mm
, addr
);
964 next
= pgd_addr_end(addr
, end
);
967 else if (!gup_pud_range(pgdp
, addr
, next
, write
, pages
, &nr
))
969 } while (pgdp
++, addr
= next
, addr
!= end
);
970 local_irq_restore(flags
);
976 * get_user_pages_fast() - pin user pages in memory
977 * @start: starting user address
978 * @nr_pages: number of pages from start to pin
979 * @write: whether pages will be written to
980 * @pages: array that receives pointers to the pages pinned.
981 * Should be at least nr_pages long.
983 * Attempt to pin user pages in memory without taking mm->mmap_sem.
984 * If not successful, it will fall back to taking the lock and
985 * calling get_user_pages().
987 * Returns number of pages pinned. This may be fewer than the number
988 * requested. If nr_pages is 0 or negative, returns 0. If no pages
989 * were pinned, returns -errno.
991 int get_user_pages_fast(unsigned long start
, int nr_pages
, int write
,
994 struct mm_struct
*mm
= current
->mm
;
998 nr
= __get_user_pages_fast(start
, nr_pages
, write
, pages
);
1001 if (nr
< nr_pages
) {
1002 /* Try to get the remaining pages with get_user_pages */
1003 start
+= nr
<< PAGE_SHIFT
;
1006 down_read(&mm
->mmap_sem
);
1007 ret
= get_user_pages(current
, mm
, start
,
1008 nr_pages
- nr
, write
, 0, pages
, NULL
);
1009 up_read(&mm
->mmap_sem
);
1011 /* Have to be a bit careful with return values */
1023 #endif /* CONFIG_HAVE_GENERIC_RCU_GUP */