]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - mm/gup.c
mm, dax: dax-pmd vs thp-pmd vs hugetlbfs-pmd
[mirror_ubuntu-artful-kernel.git] / mm / gup.c
CommitLineData
4bbd4c77
KS
1#include <linux/kernel.h>
2#include <linux/errno.h>
3#include <linux/err.h>
4#include <linux/spinlock.h>
5
4bbd4c77
KS
6#include <linux/mm.h>
7#include <linux/pagemap.h>
8#include <linux/rmap.h>
9#include <linux/swap.h>
10#include <linux/swapops.h>
11
2667f50e
SC
12#include <linux/sched.h>
13#include <linux/rwsem.h>
f30c59e9 14#include <linux/hugetlb.h>
1027e443 15
2667f50e 16#include <asm/pgtable.h>
1027e443 17#include <asm/tlbflush.h>
2667f50e 18
4bbd4c77
KS
19#include "internal.h"
20
69e68b4f
KS
21static struct page *no_page_table(struct vm_area_struct *vma,
22 unsigned int flags)
4bbd4c77 23{
69e68b4f
KS
24 /*
25 * When core dumping an enormous anonymous area that nobody
26 * has touched so far, we don't want to allocate unnecessary pages or
27 * page tables. Return error instead of NULL to skip handle_mm_fault,
28 * then get_dump_page() will return NULL to leave a hole in the dump.
29 * But we can only make this optimization where a hole would surely
30 * be zero-filled if handle_mm_fault() actually did handle it.
31 */
32 if ((flags & FOLL_DUMP) && (!vma->vm_ops || !vma->vm_ops->fault))
33 return ERR_PTR(-EFAULT);
34 return NULL;
35}
4bbd4c77 36
1027e443
KS
37static int follow_pfn_pte(struct vm_area_struct *vma, unsigned long address,
38 pte_t *pte, unsigned int flags)
39{
40 /* No page to get reference */
41 if (flags & FOLL_GET)
42 return -EFAULT;
43
44 if (flags & FOLL_TOUCH) {
45 pte_t entry = *pte;
46
47 if (flags & FOLL_WRITE)
48 entry = pte_mkdirty(entry);
49 entry = pte_mkyoung(entry);
50
51 if (!pte_same(*pte, entry)) {
52 set_pte_at(vma->vm_mm, address, pte, entry);
53 update_mmu_cache(vma, address, pte);
54 }
55 }
56
57 /* Proper page table entry exists, but no corresponding struct page */
58 return -EEXIST;
59}
60
69e68b4f
KS
61static struct page *follow_page_pte(struct vm_area_struct *vma,
62 unsigned long address, pmd_t *pmd, unsigned int flags)
63{
64 struct mm_struct *mm = vma->vm_mm;
65 struct page *page;
66 spinlock_t *ptl;
67 pte_t *ptep, pte;
4bbd4c77 68
69e68b4f 69retry:
4bbd4c77 70 if (unlikely(pmd_bad(*pmd)))
69e68b4f 71 return no_page_table(vma, flags);
4bbd4c77
KS
72
73 ptep = pte_offset_map_lock(mm, pmd, address, &ptl);
4bbd4c77
KS
74 pte = *ptep;
75 if (!pte_present(pte)) {
76 swp_entry_t entry;
77 /*
78 * KSM's break_ksm() relies upon recognizing a ksm page
79 * even while it is being migrated, so for that case we
80 * need migration_entry_wait().
81 */
82 if (likely(!(flags & FOLL_MIGRATION)))
83 goto no_page;
0661a336 84 if (pte_none(pte))
4bbd4c77
KS
85 goto no_page;
86 entry = pte_to_swp_entry(pte);
87 if (!is_migration_entry(entry))
88 goto no_page;
89 pte_unmap_unlock(ptep, ptl);
90 migration_entry_wait(mm, pmd, address);
69e68b4f 91 goto retry;
4bbd4c77 92 }
8a0516ed 93 if ((flags & FOLL_NUMA) && pte_protnone(pte))
4bbd4c77 94 goto no_page;
69e68b4f
KS
95 if ((flags & FOLL_WRITE) && !pte_write(pte)) {
96 pte_unmap_unlock(ptep, ptl);
97 return NULL;
98 }
4bbd4c77
KS
99
100 page = vm_normal_page(vma, address, pte);
101 if (unlikely(!page)) {
1027e443
KS
102 if (flags & FOLL_DUMP) {
103 /* Avoid special (like zero) pages in core dumps */
104 page = ERR_PTR(-EFAULT);
105 goto out;
106 }
107
108 if (is_zero_pfn(pte_pfn(pte))) {
109 page = pte_page(pte);
110 } else {
111 int ret;
112
113 ret = follow_pfn_pte(vma, address, ptep, flags);
114 page = ERR_PTR(ret);
115 goto out;
116 }
4bbd4c77
KS
117 }
118
6742d293
KS
119 if (flags & FOLL_SPLIT && PageTransCompound(page)) {
120 int ret;
121 get_page(page);
122 pte_unmap_unlock(ptep, ptl);
123 lock_page(page);
124 ret = split_huge_page(page);
125 unlock_page(page);
126 put_page(page);
127 if (ret)
128 return ERR_PTR(ret);
129 goto retry;
130 }
131
4bbd4c77 132 if (flags & FOLL_GET)
ddc58f27 133 get_page(page);
4bbd4c77
KS
134 if (flags & FOLL_TOUCH) {
135 if ((flags & FOLL_WRITE) &&
136 !pte_dirty(pte) && !PageDirty(page))
137 set_page_dirty(page);
138 /*
139 * pte_mkyoung() would be more correct here, but atomic care
140 * is needed to avoid losing the dirty bit: it is easier to use
141 * mark_page_accessed().
142 */
143 mark_page_accessed(page);
144 }
de60f5f1 145 if ((flags & FOLL_MLOCK) && (vma->vm_flags & VM_LOCKED)) {
e90309c9
KS
146 /* Do not mlock pte-mapped THP */
147 if (PageTransCompound(page))
148 goto out;
149
4bbd4c77
KS
150 /*
151 * The preliminary mapping check is mainly to avoid the
152 * pointless overhead of lock_page on the ZERO_PAGE
153 * which might bounce very badly if there is contention.
154 *
155 * If the page is already locked, we don't need to
156 * handle it now - vmscan will handle it later if and
157 * when it attempts to reclaim the page.
158 */
159 if (page->mapping && trylock_page(page)) {
160 lru_add_drain(); /* push cached pages to LRU */
161 /*
162 * Because we lock page here, and migration is
163 * blocked by the pte's page reference, and we
164 * know the page is still mapped, we don't even
165 * need to check for file-cache page truncation.
166 */
167 mlock_vma_page(page);
168 unlock_page(page);
169 }
170 }
1027e443 171out:
4bbd4c77 172 pte_unmap_unlock(ptep, ptl);
4bbd4c77 173 return page;
4bbd4c77
KS
174no_page:
175 pte_unmap_unlock(ptep, ptl);
176 if (!pte_none(pte))
69e68b4f
KS
177 return NULL;
178 return no_page_table(vma, flags);
179}
180
181/**
182 * follow_page_mask - look up a page descriptor from a user-virtual address
183 * @vma: vm_area_struct mapping @address
184 * @address: virtual address to look up
185 * @flags: flags modifying lookup behaviour
186 * @page_mask: on output, *page_mask is set according to the size of the page
187 *
188 * @flags can have FOLL_ flags set, defined in <linux/mm.h>
189 *
190 * Returns the mapped (struct page *), %NULL if no mapping exists, or
191 * an error pointer if there is a mapping to something not represented
192 * by a page descriptor (see also vm_normal_page()).
193 */
194struct page *follow_page_mask(struct vm_area_struct *vma,
195 unsigned long address, unsigned int flags,
196 unsigned int *page_mask)
197{
198 pgd_t *pgd;
199 pud_t *pud;
200 pmd_t *pmd;
201 spinlock_t *ptl;
202 struct page *page;
203 struct mm_struct *mm = vma->vm_mm;
204
205 *page_mask = 0;
206
207 page = follow_huge_addr(mm, address, flags & FOLL_WRITE);
208 if (!IS_ERR(page)) {
209 BUG_ON(flags & FOLL_GET);
4bbd4c77 210 return page;
69e68b4f 211 }
4bbd4c77 212
69e68b4f
KS
213 pgd = pgd_offset(mm, address);
214 if (pgd_none(*pgd) || unlikely(pgd_bad(*pgd)))
215 return no_page_table(vma, flags);
216
217 pud = pud_offset(pgd, address);
218 if (pud_none(*pud))
219 return no_page_table(vma, flags);
220 if (pud_huge(*pud) && vma->vm_flags & VM_HUGETLB) {
e66f17ff
NH
221 page = follow_huge_pud(mm, address, pud, flags);
222 if (page)
223 return page;
224 return no_page_table(vma, flags);
69e68b4f
KS
225 }
226 if (unlikely(pud_bad(*pud)))
227 return no_page_table(vma, flags);
228
229 pmd = pmd_offset(pud, address);
230 if (pmd_none(*pmd))
231 return no_page_table(vma, flags);
232 if (pmd_huge(*pmd) && vma->vm_flags & VM_HUGETLB) {
e66f17ff
NH
233 page = follow_huge_pmd(mm, address, pmd, flags);
234 if (page)
235 return page;
236 return no_page_table(vma, flags);
69e68b4f 237 }
8a0516ed 238 if ((flags & FOLL_NUMA) && pmd_protnone(*pmd))
69e68b4f 239 return no_page_table(vma, flags);
6742d293
KS
240 if (likely(!pmd_trans_huge(*pmd)))
241 return follow_page_pte(vma, address, pmd, flags);
242
243 ptl = pmd_lock(mm, pmd);
244 if (unlikely(!pmd_trans_huge(*pmd))) {
245 spin_unlock(ptl);
246 return follow_page_pte(vma, address, pmd, flags);
247 }
6742d293
KS
248 if (flags & FOLL_SPLIT) {
249 int ret;
250 page = pmd_page(*pmd);
251 if (is_huge_zero_page(page)) {
252 spin_unlock(ptl);
253 ret = 0;
78ddc534 254 split_huge_pmd(vma, pmd, address);
6742d293
KS
255 } else {
256 get_page(page);
69e68b4f 257 spin_unlock(ptl);
6742d293
KS
258 lock_page(page);
259 ret = split_huge_page(page);
260 unlock_page(page);
261 put_page(page);
262 }
263
264 return ret ? ERR_PTR(ret) :
265 follow_page_pte(vma, address, pmd, flags);
69e68b4f 266 }
6742d293
KS
267
268 page = follow_trans_huge_pmd(vma, address, pmd, flags);
269 spin_unlock(ptl);
270 *page_mask = HPAGE_PMD_NR - 1;
271 return page;
4bbd4c77
KS
272}
273
f2b495ca
KS
274static int get_gate_page(struct mm_struct *mm, unsigned long address,
275 unsigned int gup_flags, struct vm_area_struct **vma,
276 struct page **page)
277{
278 pgd_t *pgd;
279 pud_t *pud;
280 pmd_t *pmd;
281 pte_t *pte;
282 int ret = -EFAULT;
283
284 /* user gate pages are read-only */
285 if (gup_flags & FOLL_WRITE)
286 return -EFAULT;
287 if (address > TASK_SIZE)
288 pgd = pgd_offset_k(address);
289 else
290 pgd = pgd_offset_gate(mm, address);
291 BUG_ON(pgd_none(*pgd));
292 pud = pud_offset(pgd, address);
293 BUG_ON(pud_none(*pud));
294 pmd = pmd_offset(pud, address);
295 if (pmd_none(*pmd))
296 return -EFAULT;
297 VM_BUG_ON(pmd_trans_huge(*pmd));
298 pte = pte_offset_map(pmd, address);
299 if (pte_none(*pte))
300 goto unmap;
301 *vma = get_gate_vma(mm);
302 if (!page)
303 goto out;
304 *page = vm_normal_page(*vma, address, *pte);
305 if (!*page) {
306 if ((gup_flags & FOLL_DUMP) || !is_zero_pfn(pte_pfn(*pte)))
307 goto unmap;
308 *page = pte_page(*pte);
309 }
310 get_page(*page);
311out:
312 ret = 0;
313unmap:
314 pte_unmap(pte);
315 return ret;
316}
317
9a95f3cf
PC
318/*
319 * mmap_sem must be held on entry. If @nonblocking != NULL and
320 * *@flags does not include FOLL_NOWAIT, the mmap_sem may be released.
321 * If it is, *@nonblocking will be set to 0 and -EBUSY returned.
322 */
16744483
KS
323static int faultin_page(struct task_struct *tsk, struct vm_area_struct *vma,
324 unsigned long address, unsigned int *flags, int *nonblocking)
325{
326 struct mm_struct *mm = vma->vm_mm;
327 unsigned int fault_flags = 0;
328 int ret;
329
de60f5f1
EM
330 /* mlock all present pages, but do not fault in new pages */
331 if ((*flags & (FOLL_POPULATE | FOLL_MLOCK)) == FOLL_MLOCK)
332 return -ENOENT;
84d33df2
KS
333 /* For mm_populate(), just skip the stack guard page. */
334 if ((*flags & FOLL_POPULATE) &&
16744483
KS
335 (stack_guard_page_start(vma, address) ||
336 stack_guard_page_end(vma, address + PAGE_SIZE)))
337 return -ENOENT;
338 if (*flags & FOLL_WRITE)
339 fault_flags |= FAULT_FLAG_WRITE;
340 if (nonblocking)
341 fault_flags |= FAULT_FLAG_ALLOW_RETRY;
342 if (*flags & FOLL_NOWAIT)
343 fault_flags |= FAULT_FLAG_ALLOW_RETRY | FAULT_FLAG_RETRY_NOWAIT;
234b239b
ALC
344 if (*flags & FOLL_TRIED) {
345 VM_WARN_ON_ONCE(fault_flags & FAULT_FLAG_ALLOW_RETRY);
346 fault_flags |= FAULT_FLAG_TRIED;
347 }
16744483
KS
348
349 ret = handle_mm_fault(mm, vma, address, fault_flags);
350 if (ret & VM_FAULT_ERROR) {
351 if (ret & VM_FAULT_OOM)
352 return -ENOMEM;
353 if (ret & (VM_FAULT_HWPOISON | VM_FAULT_HWPOISON_LARGE))
354 return *flags & FOLL_HWPOISON ? -EHWPOISON : -EFAULT;
33692f27 355 if (ret & (VM_FAULT_SIGBUS | VM_FAULT_SIGSEGV))
16744483
KS
356 return -EFAULT;
357 BUG();
358 }
359
360 if (tsk) {
361 if (ret & VM_FAULT_MAJOR)
362 tsk->maj_flt++;
363 else
364 tsk->min_flt++;
365 }
366
367 if (ret & VM_FAULT_RETRY) {
368 if (nonblocking)
369 *nonblocking = 0;
370 return -EBUSY;
371 }
372
373 /*
374 * The VM_FAULT_WRITE bit tells us that do_wp_page has broken COW when
375 * necessary, even if maybe_mkwrite decided not to set pte_write. We
376 * can thus safely do subsequent page lookups as if they were reads.
377 * But only do so when looping for pte_write is futile: in some cases
378 * userspace may also be wanting to write to the gotten user page,
379 * which a read fault here might prevent (a readonly page might get
380 * reCOWed by userspace write).
381 */
382 if ((ret & VM_FAULT_WRITE) && !(vma->vm_flags & VM_WRITE))
383 *flags &= ~FOLL_WRITE;
384 return 0;
385}
386
fa5bb209
KS
387static int check_vma_flags(struct vm_area_struct *vma, unsigned long gup_flags)
388{
389 vm_flags_t vm_flags = vma->vm_flags;
390
391 if (vm_flags & (VM_IO | VM_PFNMAP))
392 return -EFAULT;
393
394 if (gup_flags & FOLL_WRITE) {
395 if (!(vm_flags & VM_WRITE)) {
396 if (!(gup_flags & FOLL_FORCE))
397 return -EFAULT;
398 /*
399 * We used to let the write,force case do COW in a
400 * VM_MAYWRITE VM_SHARED !VM_WRITE vma, so ptrace could
401 * set a breakpoint in a read-only mapping of an
402 * executable, without corrupting the file (yet only
403 * when that file had been opened for writing!).
404 * Anon pages in shared mappings are surprising: now
405 * just reject it.
406 */
407 if (!is_cow_mapping(vm_flags)) {
408 WARN_ON_ONCE(vm_flags & VM_MAYWRITE);
409 return -EFAULT;
410 }
411 }
412 } else if (!(vm_flags & VM_READ)) {
413 if (!(gup_flags & FOLL_FORCE))
414 return -EFAULT;
415 /*
416 * Is there actually any vma we can reach here which does not
417 * have VM_MAYREAD set?
418 */
419 if (!(vm_flags & VM_MAYREAD))
420 return -EFAULT;
421 }
422 return 0;
423}
424
4bbd4c77
KS
425/**
426 * __get_user_pages() - pin user pages in memory
427 * @tsk: task_struct of target task
428 * @mm: mm_struct of target mm
429 * @start: starting user address
430 * @nr_pages: number of pages from start to pin
431 * @gup_flags: flags modifying pin behaviour
432 * @pages: array that receives pointers to the pages pinned.
433 * Should be at least nr_pages long. Or NULL, if caller
434 * only intends to ensure the pages are faulted in.
435 * @vmas: array of pointers to vmas corresponding to each page.
436 * Or NULL if the caller does not require them.
437 * @nonblocking: whether waiting for disk IO or mmap_sem contention
438 *
439 * Returns number of pages pinned. This may be fewer than the number
440 * requested. If nr_pages is 0 or negative, returns 0. If no pages
441 * were pinned, returns -errno. Each page returned must be released
442 * with a put_page() call when it is finished with. vmas will only
443 * remain valid while mmap_sem is held.
444 *
9a95f3cf 445 * Must be called with mmap_sem held. It may be released. See below.
4bbd4c77
KS
446 *
447 * __get_user_pages walks a process's page tables and takes a reference to
448 * each struct page that each user address corresponds to at a given
449 * instant. That is, it takes the page that would be accessed if a user
450 * thread accesses the given user virtual address at that instant.
451 *
452 * This does not guarantee that the page exists in the user mappings when
453 * __get_user_pages returns, and there may even be a completely different
454 * page there in some cases (eg. if mmapped pagecache has been invalidated
455 * and subsequently re faulted). However it does guarantee that the page
456 * won't be freed completely. And mostly callers simply care that the page
457 * contains data that was valid *at some point in time*. Typically, an IO
458 * or similar operation cannot guarantee anything stronger anyway because
459 * locks can't be held over the syscall boundary.
460 *
461 * If @gup_flags & FOLL_WRITE == 0, the page must not be written to. If
462 * the page is written to, set_page_dirty (or set_page_dirty_lock, as
463 * appropriate) must be called after the page is finished with, and
464 * before put_page is called.
465 *
466 * If @nonblocking != NULL, __get_user_pages will not wait for disk IO
467 * or mmap_sem contention, and if waiting is needed to pin all pages,
9a95f3cf
PC
468 * *@nonblocking will be set to 0. Further, if @gup_flags does not
469 * include FOLL_NOWAIT, the mmap_sem will be released via up_read() in
470 * this case.
471 *
472 * A caller using such a combination of @nonblocking and @gup_flags
473 * must therefore hold the mmap_sem for reading only, and recognize
474 * when it's been released. Otherwise, it must be held for either
475 * reading or writing and will not be released.
4bbd4c77
KS
476 *
477 * In most cases, get_user_pages or get_user_pages_fast should be used
478 * instead of __get_user_pages. __get_user_pages should be used only if
479 * you need some special @gup_flags.
480 */
481long __get_user_pages(struct task_struct *tsk, struct mm_struct *mm,
482 unsigned long start, unsigned long nr_pages,
483 unsigned int gup_flags, struct page **pages,
484 struct vm_area_struct **vmas, int *nonblocking)
485{
fa5bb209 486 long i = 0;
4bbd4c77 487 unsigned int page_mask;
fa5bb209 488 struct vm_area_struct *vma = NULL;
4bbd4c77
KS
489
490 if (!nr_pages)
491 return 0;
492
493 VM_BUG_ON(!!pages != !!(gup_flags & FOLL_GET));
494
495 /*
496 * If FOLL_FORCE is set then do not force a full fault as the hinting
497 * fault information is unrelated to the reference behaviour of a task
498 * using the address space
499 */
500 if (!(gup_flags & FOLL_FORCE))
501 gup_flags |= FOLL_NUMA;
502
4bbd4c77 503 do {
fa5bb209
KS
504 struct page *page;
505 unsigned int foll_flags = gup_flags;
506 unsigned int page_increm;
507
508 /* first iteration or cross vma bound */
509 if (!vma || start >= vma->vm_end) {
510 vma = find_extend_vma(mm, start);
511 if (!vma && in_gate_area(mm, start)) {
512 int ret;
513 ret = get_gate_page(mm, start & PAGE_MASK,
514 gup_flags, &vma,
515 pages ? &pages[i] : NULL);
516 if (ret)
517 return i ? : ret;
518 page_mask = 0;
519 goto next_page;
520 }
4bbd4c77 521
fa5bb209
KS
522 if (!vma || check_vma_flags(vma, gup_flags))
523 return i ? : -EFAULT;
524 if (is_vm_hugetlb_page(vma)) {
525 i = follow_hugetlb_page(mm, vma, pages, vmas,
526 &start, &nr_pages, i,
527 gup_flags);
528 continue;
4bbd4c77 529 }
fa5bb209
KS
530 }
531retry:
532 /*
533 * If we have a pending SIGKILL, don't keep faulting pages and
534 * potentially allocating memory.
535 */
536 if (unlikely(fatal_signal_pending(current)))
537 return i ? i : -ERESTARTSYS;
538 cond_resched();
539 page = follow_page_mask(vma, start, foll_flags, &page_mask);
540 if (!page) {
541 int ret;
542 ret = faultin_page(tsk, vma, start, &foll_flags,
543 nonblocking);
544 switch (ret) {
545 case 0:
546 goto retry;
547 case -EFAULT:
548 case -ENOMEM:
549 case -EHWPOISON:
550 return i ? i : ret;
551 case -EBUSY:
552 return i;
553 case -ENOENT:
554 goto next_page;
4bbd4c77 555 }
fa5bb209 556 BUG();
1027e443
KS
557 } else if (PTR_ERR(page) == -EEXIST) {
558 /*
559 * Proper page table entry exists, but no corresponding
560 * struct page.
561 */
562 goto next_page;
563 } else if (IS_ERR(page)) {
fa5bb209 564 return i ? i : PTR_ERR(page);
1027e443 565 }
fa5bb209
KS
566 if (pages) {
567 pages[i] = page;
568 flush_anon_page(vma, page, start);
569 flush_dcache_page(page);
570 page_mask = 0;
4bbd4c77 571 }
4bbd4c77 572next_page:
fa5bb209
KS
573 if (vmas) {
574 vmas[i] = vma;
575 page_mask = 0;
576 }
577 page_increm = 1 + (~(start >> PAGE_SHIFT) & page_mask);
578 if (page_increm > nr_pages)
579 page_increm = nr_pages;
580 i += page_increm;
581 start += page_increm * PAGE_SIZE;
582 nr_pages -= page_increm;
4bbd4c77
KS
583 } while (nr_pages);
584 return i;
4bbd4c77
KS
585}
586EXPORT_SYMBOL(__get_user_pages);
587
588/*
589 * fixup_user_fault() - manually resolve a user page fault
590 * @tsk: the task_struct to use for page fault accounting, or
591 * NULL if faults are not to be recorded.
592 * @mm: mm_struct of target mm
593 * @address: user address
594 * @fault_flags:flags to pass down to handle_mm_fault()
595 *
596 * This is meant to be called in the specific scenario where for locking reasons
597 * we try to access user memory in atomic context (within a pagefault_disable()
598 * section), this returns -EFAULT, and we want to resolve the user fault before
599 * trying again.
600 *
601 * Typically this is meant to be used by the futex code.
602 *
603 * The main difference with get_user_pages() is that this function will
604 * unconditionally call handle_mm_fault() which will in turn perform all the
605 * necessary SW fixup of the dirty and young bits in the PTE, while
606 * handle_mm_fault() only guarantees to update these in the struct page.
607 *
608 * This is important for some architectures where those bits also gate the
609 * access permission to the page because they are maintained in software. On
610 * such architectures, gup() will not be enough to make a subsequent access
611 * succeed.
612 *
9a95f3cf 613 * This has the same semantics wrt the @mm->mmap_sem as does filemap_fault().
4bbd4c77
KS
614 */
615int fixup_user_fault(struct task_struct *tsk, struct mm_struct *mm,
616 unsigned long address, unsigned int fault_flags)
617{
618 struct vm_area_struct *vma;
619 vm_flags_t vm_flags;
620 int ret;
621
622 vma = find_extend_vma(mm, address);
623 if (!vma || address < vma->vm_start)
624 return -EFAULT;
625
626 vm_flags = (fault_flags & FAULT_FLAG_WRITE) ? VM_WRITE : VM_READ;
627 if (!(vm_flags & vma->vm_flags))
628 return -EFAULT;
629
630 ret = handle_mm_fault(mm, vma, address, fault_flags);
631 if (ret & VM_FAULT_ERROR) {
632 if (ret & VM_FAULT_OOM)
633 return -ENOMEM;
634 if (ret & (VM_FAULT_HWPOISON | VM_FAULT_HWPOISON_LARGE))
635 return -EHWPOISON;
33692f27 636 if (ret & (VM_FAULT_SIGBUS | VM_FAULT_SIGSEGV))
4bbd4c77
KS
637 return -EFAULT;
638 BUG();
639 }
640 if (tsk) {
641 if (ret & VM_FAULT_MAJOR)
642 tsk->maj_flt++;
643 else
644 tsk->min_flt++;
645 }
646 return 0;
647}
648
f0818f47
AA
649static __always_inline long __get_user_pages_locked(struct task_struct *tsk,
650 struct mm_struct *mm,
651 unsigned long start,
652 unsigned long nr_pages,
653 int write, int force,
654 struct page **pages,
655 struct vm_area_struct **vmas,
0fd71a56
AA
656 int *locked, bool notify_drop,
657 unsigned int flags)
f0818f47 658{
f0818f47
AA
659 long ret, pages_done;
660 bool lock_dropped;
661
662 if (locked) {
663 /* if VM_FAULT_RETRY can be returned, vmas become invalid */
664 BUG_ON(vmas);
665 /* check caller initialized locked */
666 BUG_ON(*locked != 1);
667 }
668
669 if (pages)
670 flags |= FOLL_GET;
671 if (write)
672 flags |= FOLL_WRITE;
673 if (force)
674 flags |= FOLL_FORCE;
675
676 pages_done = 0;
677 lock_dropped = false;
678 for (;;) {
679 ret = __get_user_pages(tsk, mm, start, nr_pages, flags, pages,
680 vmas, locked);
681 if (!locked)
682 /* VM_FAULT_RETRY couldn't trigger, bypass */
683 return ret;
684
685 /* VM_FAULT_RETRY cannot return errors */
686 if (!*locked) {
687 BUG_ON(ret < 0);
688 BUG_ON(ret >= nr_pages);
689 }
690
691 if (!pages)
692 /* If it's a prefault don't insist harder */
693 return ret;
694
695 if (ret > 0) {
696 nr_pages -= ret;
697 pages_done += ret;
698 if (!nr_pages)
699 break;
700 }
701 if (*locked) {
702 /* VM_FAULT_RETRY didn't trigger */
703 if (!pages_done)
704 pages_done = ret;
705 break;
706 }
707 /* VM_FAULT_RETRY triggered, so seek to the faulting offset */
708 pages += ret;
709 start += ret << PAGE_SHIFT;
710
711 /*
712 * Repeat on the address that fired VM_FAULT_RETRY
713 * without FAULT_FLAG_ALLOW_RETRY but with
714 * FAULT_FLAG_TRIED.
715 */
716 *locked = 1;
717 lock_dropped = true;
718 down_read(&mm->mmap_sem);
719 ret = __get_user_pages(tsk, mm, start, 1, flags | FOLL_TRIED,
720 pages, NULL, NULL);
721 if (ret != 1) {
722 BUG_ON(ret > 1);
723 if (!pages_done)
724 pages_done = ret;
725 break;
726 }
727 nr_pages--;
728 pages_done++;
729 if (!nr_pages)
730 break;
731 pages++;
732 start += PAGE_SIZE;
733 }
734 if (notify_drop && lock_dropped && *locked) {
735 /*
736 * We must let the caller know we temporarily dropped the lock
737 * and so the critical section protected by it was lost.
738 */
739 up_read(&mm->mmap_sem);
740 *locked = 0;
741 }
742 return pages_done;
743}
744
745/*
746 * We can leverage the VM_FAULT_RETRY functionality in the page fault
747 * paths better by using either get_user_pages_locked() or
748 * get_user_pages_unlocked().
749 *
750 * get_user_pages_locked() is suitable to replace the form:
751 *
752 * down_read(&mm->mmap_sem);
753 * do_something()
754 * get_user_pages(tsk, mm, ..., pages, NULL);
755 * up_read(&mm->mmap_sem);
756 *
757 * to:
758 *
759 * int locked = 1;
760 * down_read(&mm->mmap_sem);
761 * do_something()
762 * get_user_pages_locked(tsk, mm, ..., pages, &locked);
763 * if (locked)
764 * up_read(&mm->mmap_sem);
765 */
766long get_user_pages_locked(struct task_struct *tsk, struct mm_struct *mm,
767 unsigned long start, unsigned long nr_pages,
768 int write, int force, struct page **pages,
769 int *locked)
770{
771 return __get_user_pages_locked(tsk, mm, start, nr_pages, write, force,
0fd71a56 772 pages, NULL, locked, true, FOLL_TOUCH);
f0818f47
AA
773}
774EXPORT_SYMBOL(get_user_pages_locked);
775
0fd71a56
AA
776/*
777 * Same as get_user_pages_unlocked(...., FOLL_TOUCH) but it allows to
778 * pass additional gup_flags as last parameter (like FOLL_HWPOISON).
779 *
780 * NOTE: here FOLL_TOUCH is not set implicitly and must be set by the
781 * caller if required (just like with __get_user_pages). "FOLL_GET",
782 * "FOLL_WRITE" and "FOLL_FORCE" are set implicitly as needed
783 * according to the parameters "pages", "write", "force"
784 * respectively.
785 */
786__always_inline long __get_user_pages_unlocked(struct task_struct *tsk, struct mm_struct *mm,
787 unsigned long start, unsigned long nr_pages,
788 int write, int force, struct page **pages,
789 unsigned int gup_flags)
790{
791 long ret;
792 int locked = 1;
793 down_read(&mm->mmap_sem);
794 ret = __get_user_pages_locked(tsk, mm, start, nr_pages, write, force,
795 pages, NULL, &locked, false, gup_flags);
796 if (locked)
797 up_read(&mm->mmap_sem);
798 return ret;
799}
800EXPORT_SYMBOL(__get_user_pages_unlocked);
801
f0818f47
AA
802/*
803 * get_user_pages_unlocked() is suitable to replace the form:
804 *
805 * down_read(&mm->mmap_sem);
806 * get_user_pages(tsk, mm, ..., pages, NULL);
807 * up_read(&mm->mmap_sem);
808 *
809 * with:
810 *
811 * get_user_pages_unlocked(tsk, mm, ..., pages);
812 *
813 * It is functionally equivalent to get_user_pages_fast so
814 * get_user_pages_fast should be used instead, if the two parameters
815 * "tsk" and "mm" are respectively equal to current and current->mm,
816 * or if "force" shall be set to 1 (get_user_pages_fast misses the
817 * "force" parameter).
818 */
819long get_user_pages_unlocked(struct task_struct *tsk, struct mm_struct *mm,
820 unsigned long start, unsigned long nr_pages,
821 int write, int force, struct page **pages)
822{
0fd71a56
AA
823 return __get_user_pages_unlocked(tsk, mm, start, nr_pages, write,
824 force, pages, FOLL_TOUCH);
f0818f47
AA
825}
826EXPORT_SYMBOL(get_user_pages_unlocked);
827
4bbd4c77
KS
828/*
829 * get_user_pages() - pin user pages in memory
830 * @tsk: the task_struct to use for page fault accounting, or
831 * NULL if faults are not to be recorded.
832 * @mm: mm_struct of target mm
833 * @start: starting user address
834 * @nr_pages: number of pages from start to pin
835 * @write: whether pages will be written to by the caller
836 * @force: whether to force access even when user mapping is currently
837 * protected (but never forces write access to shared mapping).
838 * @pages: array that receives pointers to the pages pinned.
839 * Should be at least nr_pages long. Or NULL, if caller
840 * only intends to ensure the pages are faulted in.
841 * @vmas: array of pointers to vmas corresponding to each page.
842 * Or NULL if the caller does not require them.
843 *
844 * Returns number of pages pinned. This may be fewer than the number
845 * requested. If nr_pages is 0 or negative, returns 0. If no pages
846 * were pinned, returns -errno. Each page returned must be released
847 * with a put_page() call when it is finished with. vmas will only
848 * remain valid while mmap_sem is held.
849 *
850 * Must be called with mmap_sem held for read or write.
851 *
852 * get_user_pages walks a process's page tables and takes a reference to
853 * each struct page that each user address corresponds to at a given
854 * instant. That is, it takes the page that would be accessed if a user
855 * thread accesses the given user virtual address at that instant.
856 *
857 * This does not guarantee that the page exists in the user mappings when
858 * get_user_pages returns, and there may even be a completely different
859 * page there in some cases (eg. if mmapped pagecache has been invalidated
860 * and subsequently re faulted). However it does guarantee that the page
861 * won't be freed completely. And mostly callers simply care that the page
862 * contains data that was valid *at some point in time*. Typically, an IO
863 * or similar operation cannot guarantee anything stronger anyway because
864 * locks can't be held over the syscall boundary.
865 *
866 * If write=0, the page must not be written to. If the page is written to,
867 * set_page_dirty (or set_page_dirty_lock, as appropriate) must be called
868 * after the page is finished with, and before put_page is called.
869 *
870 * get_user_pages is typically used for fewer-copy IO operations, to get a
871 * handle on the memory by some means other than accesses via the user virtual
872 * addresses. The pages may be submitted for DMA to devices or accessed via
873 * their kernel linear mapping (via the kmap APIs). Care should be taken to
874 * use the correct cache flushing APIs.
875 *
876 * See also get_user_pages_fast, for performance critical applications.
f0818f47
AA
877 *
878 * get_user_pages should be phased out in favor of
879 * get_user_pages_locked|unlocked or get_user_pages_fast. Nothing
880 * should use get_user_pages because it cannot pass
881 * FAULT_FLAG_ALLOW_RETRY to handle_mm_fault.
4bbd4c77
KS
882 */
883long get_user_pages(struct task_struct *tsk, struct mm_struct *mm,
884 unsigned long start, unsigned long nr_pages, int write,
885 int force, struct page **pages, struct vm_area_struct **vmas)
886{
f0818f47 887 return __get_user_pages_locked(tsk, mm, start, nr_pages, write, force,
0fd71a56 888 pages, vmas, NULL, false, FOLL_TOUCH);
4bbd4c77
KS
889}
890EXPORT_SYMBOL(get_user_pages);
891
acc3c8d1
KS
892/**
893 * populate_vma_page_range() - populate a range of pages in the vma.
894 * @vma: target vma
895 * @start: start address
896 * @end: end address
897 * @nonblocking:
898 *
899 * This takes care of mlocking the pages too if VM_LOCKED is set.
900 *
901 * return 0 on success, negative error code on error.
902 *
903 * vma->vm_mm->mmap_sem must be held.
904 *
905 * If @nonblocking is NULL, it may be held for read or write and will
906 * be unperturbed.
907 *
908 * If @nonblocking is non-NULL, it must held for read only and may be
909 * released. If it's released, *@nonblocking will be set to 0.
910 */
911long populate_vma_page_range(struct vm_area_struct *vma,
912 unsigned long start, unsigned long end, int *nonblocking)
913{
914 struct mm_struct *mm = vma->vm_mm;
915 unsigned long nr_pages = (end - start) / PAGE_SIZE;
916 int gup_flags;
917
918 VM_BUG_ON(start & ~PAGE_MASK);
919 VM_BUG_ON(end & ~PAGE_MASK);
920 VM_BUG_ON_VMA(start < vma->vm_start, vma);
921 VM_BUG_ON_VMA(end > vma->vm_end, vma);
922 VM_BUG_ON_MM(!rwsem_is_locked(&mm->mmap_sem), mm);
923
de60f5f1
EM
924 gup_flags = FOLL_TOUCH | FOLL_POPULATE | FOLL_MLOCK;
925 if (vma->vm_flags & VM_LOCKONFAULT)
926 gup_flags &= ~FOLL_POPULATE;
acc3c8d1
KS
927 /*
928 * We want to touch writable mappings with a write fault in order
929 * to break COW, except for shared mappings because these don't COW
930 * and we would not want to dirty them for nothing.
931 */
932 if ((vma->vm_flags & (VM_WRITE | VM_SHARED)) == VM_WRITE)
933 gup_flags |= FOLL_WRITE;
934
935 /*
936 * We want mlock to succeed for regions that have any permissions
937 * other than PROT_NONE.
938 */
939 if (vma->vm_flags & (VM_READ | VM_WRITE | VM_EXEC))
940 gup_flags |= FOLL_FORCE;
941
942 /*
943 * We made sure addr is within a VMA, so the following will
944 * not result in a stack expansion that recurses back here.
945 */
946 return __get_user_pages(current, mm, start, nr_pages, gup_flags,
947 NULL, NULL, nonblocking);
948}
949
950/*
951 * __mm_populate - populate and/or mlock pages within a range of address space.
952 *
953 * This is used to implement mlock() and the MAP_POPULATE / MAP_LOCKED mmap
954 * flags. VMAs must be already marked with the desired vm_flags, and
955 * mmap_sem must not be held.
956 */
957int __mm_populate(unsigned long start, unsigned long len, int ignore_errors)
958{
959 struct mm_struct *mm = current->mm;
960 unsigned long end, nstart, nend;
961 struct vm_area_struct *vma = NULL;
962 int locked = 0;
963 long ret = 0;
964
965 VM_BUG_ON(start & ~PAGE_MASK);
966 VM_BUG_ON(len != PAGE_ALIGN(len));
967 end = start + len;
968
969 for (nstart = start; nstart < end; nstart = nend) {
970 /*
971 * We want to fault in pages for [nstart; end) address range.
972 * Find first corresponding VMA.
973 */
974 if (!locked) {
975 locked = 1;
976 down_read(&mm->mmap_sem);
977 vma = find_vma(mm, nstart);
978 } else if (nstart >= vma->vm_end)
979 vma = vma->vm_next;
980 if (!vma || vma->vm_start >= end)
981 break;
982 /*
983 * Set [nstart; nend) to intersection of desired address
984 * range with the first VMA. Also, skip undesirable VMA types.
985 */
986 nend = min(end, vma->vm_end);
987 if (vma->vm_flags & (VM_IO | VM_PFNMAP))
988 continue;
989 if (nstart < vma->vm_start)
990 nstart = vma->vm_start;
991 /*
992 * Now fault in a range of pages. populate_vma_page_range()
993 * double checks the vma flags, so that it won't mlock pages
994 * if the vma was already munlocked.
995 */
996 ret = populate_vma_page_range(vma, nstart, nend, &locked);
997 if (ret < 0) {
998 if (ignore_errors) {
999 ret = 0;
1000 continue; /* continue at next VMA */
1001 }
1002 break;
1003 }
1004 nend = nstart + ret * PAGE_SIZE;
1005 ret = 0;
1006 }
1007 if (locked)
1008 up_read(&mm->mmap_sem);
1009 return ret; /* 0 or negative error code */
1010}
1011
4bbd4c77
KS
1012/**
1013 * get_dump_page() - pin user page in memory while writing it to core dump
1014 * @addr: user address
1015 *
1016 * Returns struct page pointer of user page pinned for dump,
1017 * to be freed afterwards by page_cache_release() or put_page().
1018 *
1019 * Returns NULL on any kind of failure - a hole must then be inserted into
1020 * the corefile, to preserve alignment with its headers; and also returns
1021 * NULL wherever the ZERO_PAGE, or an anonymous pte_none, has been found -
1022 * allowing a hole to be left in the corefile to save diskspace.
1023 *
1024 * Called without mmap_sem, but after all other threads have been killed.
1025 */
1026#ifdef CONFIG_ELF_CORE
1027struct page *get_dump_page(unsigned long addr)
1028{
1029 struct vm_area_struct *vma;
1030 struct page *page;
1031
1032 if (__get_user_pages(current, current->mm, addr, 1,
1033 FOLL_FORCE | FOLL_DUMP | FOLL_GET, &page, &vma,
1034 NULL) < 1)
1035 return NULL;
1036 flush_cache_page(vma, addr, page_to_pfn(page));
1037 return page;
1038}
1039#endif /* CONFIG_ELF_CORE */
2667f50e
SC
1040
1041/*
1042 * Generic RCU Fast GUP
1043 *
1044 * get_user_pages_fast attempts to pin user pages by walking the page
1045 * tables directly and avoids taking locks. Thus the walker needs to be
1046 * protected from page table pages being freed from under it, and should
1047 * block any THP splits.
1048 *
1049 * One way to achieve this is to have the walker disable interrupts, and
1050 * rely on IPIs from the TLB flushing code blocking before the page table
1051 * pages are freed. This is unsuitable for architectures that do not need
1052 * to broadcast an IPI when invalidating TLBs.
1053 *
1054 * Another way to achieve this is to batch up page table containing pages
1055 * belonging to more than one mm_user, then rcu_sched a callback to free those
1056 * pages. Disabling interrupts will allow the fast_gup walker to both block
1057 * the rcu_sched callback, and an IPI that we broadcast for splitting THPs
1058 * (which is a relatively rare event). The code below adopts this strategy.
1059 *
1060 * Before activating this code, please be aware that the following assumptions
1061 * are currently made:
1062 *
1063 * *) HAVE_RCU_TABLE_FREE is enabled, and tlb_remove_table is used to free
1064 * pages containing page tables.
1065 *
2667f50e
SC
1066 * *) ptes can be read atomically by the architecture.
1067 *
1068 * *) access_ok is sufficient to validate userspace address ranges.
1069 *
1070 * The last two assumptions can be relaxed by the addition of helper functions.
1071 *
1072 * This code is based heavily on the PowerPC implementation by Nick Piggin.
1073 */
1074#ifdef CONFIG_HAVE_GENERIC_RCU_GUP
1075
1076#ifdef __HAVE_ARCH_PTE_SPECIAL
1077static int gup_pte_range(pmd_t pmd, unsigned long addr, unsigned long end,
1078 int write, struct page **pages, int *nr)
1079{
1080 pte_t *ptep, *ptem;
1081 int ret = 0;
1082
1083 ptem = ptep = pte_offset_map(&pmd, addr);
1084 do {
1085 /*
1086 * In the line below we are assuming that the pte can be read
1087 * atomically. If this is not the case for your architecture,
1088 * please wrap this in a helper function!
1089 *
1090 * for an example see gup_get_pte in arch/x86/mm/gup.c
1091 */
9d8c47e4 1092 pte_t pte = READ_ONCE(*ptep);
7aef4172 1093 struct page *head, *page;
2667f50e
SC
1094
1095 /*
1096 * Similar to the PMD case below, NUMA hinting must take slow
8a0516ed 1097 * path using the pte_protnone check.
2667f50e
SC
1098 */
1099 if (!pte_present(pte) || pte_special(pte) ||
8a0516ed 1100 pte_protnone(pte) || (write && !pte_write(pte)))
2667f50e
SC
1101 goto pte_unmap;
1102
1103 VM_BUG_ON(!pfn_valid(pte_pfn(pte)));
1104 page = pte_page(pte);
7aef4172 1105 head = compound_head(page);
2667f50e 1106
7aef4172 1107 if (!page_cache_get_speculative(head))
2667f50e
SC
1108 goto pte_unmap;
1109
1110 if (unlikely(pte_val(pte) != pte_val(*ptep))) {
7aef4172 1111 put_page(head);
2667f50e
SC
1112 goto pte_unmap;
1113 }
1114
7aef4172 1115 VM_BUG_ON_PAGE(compound_head(page) != head, page);
2667f50e
SC
1116 pages[*nr] = page;
1117 (*nr)++;
1118
1119 } while (ptep++, addr += PAGE_SIZE, addr != end);
1120
1121 ret = 1;
1122
1123pte_unmap:
1124 pte_unmap(ptem);
1125 return ret;
1126}
1127#else
1128
1129/*
1130 * If we can't determine whether or not a pte is special, then fail immediately
1131 * for ptes. Note, we can still pin HugeTLB and THP as these are guaranteed not
1132 * to be special.
1133 *
1134 * For a futex to be placed on a THP tail page, get_futex_key requires a
1135 * __get_user_pages_fast implementation that can pin pages. Thus it's still
1136 * useful to have gup_huge_pmd even if we can't operate on ptes.
1137 */
1138static int gup_pte_range(pmd_t pmd, unsigned long addr, unsigned long end,
1139 int write, struct page **pages, int *nr)
1140{
1141 return 0;
1142}
1143#endif /* __HAVE_ARCH_PTE_SPECIAL */
1144
1145static int gup_huge_pmd(pmd_t orig, pmd_t *pmdp, unsigned long addr,
1146 unsigned long end, int write, struct page **pages, int *nr)
1147{
ddc58f27 1148 struct page *head, *page;
2667f50e
SC
1149 int refs;
1150
1151 if (write && !pmd_write(orig))
1152 return 0;
1153
1154 refs = 0;
1155 head = pmd_page(orig);
1156 page = head + ((addr & ~PMD_MASK) >> PAGE_SHIFT);
2667f50e
SC
1157 do {
1158 VM_BUG_ON_PAGE(compound_head(page) != head, page);
1159 pages[*nr] = page;
1160 (*nr)++;
1161 page++;
1162 refs++;
1163 } while (addr += PAGE_SIZE, addr != end);
1164
1165 if (!page_cache_add_speculative(head, refs)) {
1166 *nr -= refs;
1167 return 0;
1168 }
1169
1170 if (unlikely(pmd_val(orig) != pmd_val(*pmdp))) {
1171 *nr -= refs;
1172 while (refs--)
1173 put_page(head);
1174 return 0;
1175 }
1176
2667f50e
SC
1177 return 1;
1178}
1179
1180static int gup_huge_pud(pud_t orig, pud_t *pudp, unsigned long addr,
1181 unsigned long end, int write, struct page **pages, int *nr)
1182{
ddc58f27 1183 struct page *head, *page;
2667f50e
SC
1184 int refs;
1185
1186 if (write && !pud_write(orig))
1187 return 0;
1188
1189 refs = 0;
1190 head = pud_page(orig);
1191 page = head + ((addr & ~PUD_MASK) >> PAGE_SHIFT);
2667f50e
SC
1192 do {
1193 VM_BUG_ON_PAGE(compound_head(page) != head, page);
1194 pages[*nr] = page;
1195 (*nr)++;
1196 page++;
1197 refs++;
1198 } while (addr += PAGE_SIZE, addr != end);
1199
1200 if (!page_cache_add_speculative(head, refs)) {
1201 *nr -= refs;
1202 return 0;
1203 }
1204
1205 if (unlikely(pud_val(orig) != pud_val(*pudp))) {
1206 *nr -= refs;
1207 while (refs--)
1208 put_page(head);
1209 return 0;
1210 }
1211
2667f50e
SC
1212 return 1;
1213}
1214
f30c59e9
AK
1215static int gup_huge_pgd(pgd_t orig, pgd_t *pgdp, unsigned long addr,
1216 unsigned long end, int write,
1217 struct page **pages, int *nr)
1218{
1219 int refs;
ddc58f27 1220 struct page *head, *page;
f30c59e9
AK
1221
1222 if (write && !pgd_write(orig))
1223 return 0;
1224
1225 refs = 0;
1226 head = pgd_page(orig);
1227 page = head + ((addr & ~PGDIR_MASK) >> PAGE_SHIFT);
f30c59e9
AK
1228 do {
1229 VM_BUG_ON_PAGE(compound_head(page) != head, page);
1230 pages[*nr] = page;
1231 (*nr)++;
1232 page++;
1233 refs++;
1234 } while (addr += PAGE_SIZE, addr != end);
1235
1236 if (!page_cache_add_speculative(head, refs)) {
1237 *nr -= refs;
1238 return 0;
1239 }
1240
1241 if (unlikely(pgd_val(orig) != pgd_val(*pgdp))) {
1242 *nr -= refs;
1243 while (refs--)
1244 put_page(head);
1245 return 0;
1246 }
1247
f30c59e9
AK
1248 return 1;
1249}
1250
2667f50e
SC
1251static int gup_pmd_range(pud_t pud, unsigned long addr, unsigned long end,
1252 int write, struct page **pages, int *nr)
1253{
1254 unsigned long next;
1255 pmd_t *pmdp;
1256
1257 pmdp = pmd_offset(&pud, addr);
1258 do {
38c5ce93 1259 pmd_t pmd = READ_ONCE(*pmdp);
2667f50e
SC
1260
1261 next = pmd_addr_end(addr, end);
4b471e88 1262 if (pmd_none(pmd))
2667f50e
SC
1263 return 0;
1264
1265 if (unlikely(pmd_trans_huge(pmd) || pmd_huge(pmd))) {
1266 /*
1267 * NUMA hinting faults need to be handled in the GUP
1268 * slowpath for accounting purposes and so that they
1269 * can be serialised against THP migration.
1270 */
8a0516ed 1271 if (pmd_protnone(pmd))
2667f50e
SC
1272 return 0;
1273
1274 if (!gup_huge_pmd(pmd, pmdp, addr, next, write,
1275 pages, nr))
1276 return 0;
1277
f30c59e9
AK
1278 } else if (unlikely(is_hugepd(__hugepd(pmd_val(pmd))))) {
1279 /*
1280 * architecture have different format for hugetlbfs
1281 * pmd format and THP pmd format
1282 */
1283 if (!gup_huge_pd(__hugepd(pmd_val(pmd)), addr,
1284 PMD_SHIFT, next, write, pages, nr))
1285 return 0;
2667f50e
SC
1286 } else if (!gup_pte_range(pmd, addr, next, write, pages, nr))
1287 return 0;
1288 } while (pmdp++, addr = next, addr != end);
1289
1290 return 1;
1291}
1292
f30c59e9
AK
1293static int gup_pud_range(pgd_t pgd, unsigned long addr, unsigned long end,
1294 int write, struct page **pages, int *nr)
2667f50e
SC
1295{
1296 unsigned long next;
1297 pud_t *pudp;
1298
f30c59e9 1299 pudp = pud_offset(&pgd, addr);
2667f50e 1300 do {
e37c6982 1301 pud_t pud = READ_ONCE(*pudp);
2667f50e
SC
1302
1303 next = pud_addr_end(addr, end);
1304 if (pud_none(pud))
1305 return 0;
f30c59e9 1306 if (unlikely(pud_huge(pud))) {
2667f50e 1307 if (!gup_huge_pud(pud, pudp, addr, next, write,
f30c59e9
AK
1308 pages, nr))
1309 return 0;
1310 } else if (unlikely(is_hugepd(__hugepd(pud_val(pud))))) {
1311 if (!gup_huge_pd(__hugepd(pud_val(pud)), addr,
1312 PUD_SHIFT, next, write, pages, nr))
2667f50e
SC
1313 return 0;
1314 } else if (!gup_pmd_range(pud, addr, next, write, pages, nr))
1315 return 0;
1316 } while (pudp++, addr = next, addr != end);
1317
1318 return 1;
1319}
1320
1321/*
1322 * Like get_user_pages_fast() except it's IRQ-safe in that it won't fall back to
1323 * the regular GUP. It will only return non-negative values.
1324 */
1325int __get_user_pages_fast(unsigned long start, int nr_pages, int write,
1326 struct page **pages)
1327{
1328 struct mm_struct *mm = current->mm;
1329 unsigned long addr, len, end;
1330 unsigned long next, flags;
1331 pgd_t *pgdp;
1332 int nr = 0;
1333
1334 start &= PAGE_MASK;
1335 addr = start;
1336 len = (unsigned long) nr_pages << PAGE_SHIFT;
1337 end = start + len;
1338
1339 if (unlikely(!access_ok(write ? VERIFY_WRITE : VERIFY_READ,
1340 start, len)))
1341 return 0;
1342
1343 /*
1344 * Disable interrupts. We use the nested form as we can already have
1345 * interrupts disabled by get_futex_key.
1346 *
1347 * With interrupts disabled, we block page table pages from being
1348 * freed from under us. See mmu_gather_tlb in asm-generic/tlb.h
1349 * for more details.
1350 *
1351 * We do not adopt an rcu_read_lock(.) here as we also want to
1352 * block IPIs that come from THPs splitting.
1353 */
1354
1355 local_irq_save(flags);
1356 pgdp = pgd_offset(mm, addr);
1357 do {
9d8c47e4 1358 pgd_t pgd = READ_ONCE(*pgdp);
f30c59e9 1359
2667f50e 1360 next = pgd_addr_end(addr, end);
f30c59e9 1361 if (pgd_none(pgd))
2667f50e 1362 break;
f30c59e9
AK
1363 if (unlikely(pgd_huge(pgd))) {
1364 if (!gup_huge_pgd(pgd, pgdp, addr, next, write,
1365 pages, &nr))
1366 break;
1367 } else if (unlikely(is_hugepd(__hugepd(pgd_val(pgd))))) {
1368 if (!gup_huge_pd(__hugepd(pgd_val(pgd)), addr,
1369 PGDIR_SHIFT, next, write, pages, &nr))
1370 break;
1371 } else if (!gup_pud_range(pgd, addr, next, write, pages, &nr))
2667f50e
SC
1372 break;
1373 } while (pgdp++, addr = next, addr != end);
1374 local_irq_restore(flags);
1375
1376 return nr;
1377}
1378
1379/**
1380 * get_user_pages_fast() - pin user pages in memory
1381 * @start: starting user address
1382 * @nr_pages: number of pages from start to pin
1383 * @write: whether pages will be written to
1384 * @pages: array that receives pointers to the pages pinned.
1385 * Should be at least nr_pages long.
1386 *
1387 * Attempt to pin user pages in memory without taking mm->mmap_sem.
1388 * If not successful, it will fall back to taking the lock and
1389 * calling get_user_pages().
1390 *
1391 * Returns number of pages pinned. This may be fewer than the number
1392 * requested. If nr_pages is 0 or negative, returns 0. If no pages
1393 * were pinned, returns -errno.
1394 */
1395int get_user_pages_fast(unsigned long start, int nr_pages, int write,
1396 struct page **pages)
1397{
1398 struct mm_struct *mm = current->mm;
1399 int nr, ret;
1400
1401 start &= PAGE_MASK;
1402 nr = __get_user_pages_fast(start, nr_pages, write, pages);
1403 ret = nr;
1404
1405 if (nr < nr_pages) {
1406 /* Try to get the remaining pages with get_user_pages */
1407 start += nr << PAGE_SHIFT;
1408 pages += nr;
1409
a7b78075
AA
1410 ret = get_user_pages_unlocked(current, mm, start,
1411 nr_pages - nr, write, 0, pages);
2667f50e
SC
1412
1413 /* Have to be a bit careful with return values */
1414 if (nr > 0) {
1415 if (ret < 0)
1416 ret = nr;
1417 else
1418 ret += nr;
1419 }
1420 }
1421
1422 return ret;
1423}
1424
1425#endif /* CONFIG_HAVE_GENERIC_RCU_GUP */