USB: core: Fix bug caused by duplicate interface PM usage counter
[linux-2.6-block.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 6#include <linux/mm.h>
3565fce3 7#include <linux/memremap.h>
4bbd4c77
KS
8#include <linux/pagemap.h>
9#include <linux/rmap.h>
10#include <linux/swap.h>
11#include <linux/swapops.h>
12
174cd4b1 13#include <linux/sched/signal.h>
2667f50e 14#include <linux/rwsem.h>
f30c59e9 15#include <linux/hugetlb.h>
9a4e9f3b
AK
16#include <linux/migrate.h>
17#include <linux/mm_inline.h>
18#include <linux/sched/mm.h>
1027e443 19
33a709b2 20#include <asm/mmu_context.h>
2667f50e 21#include <asm/pgtable.h>
1027e443 22#include <asm/tlbflush.h>
2667f50e 23
4bbd4c77
KS
24#include "internal.h"
25
df06b37f
KB
26struct follow_page_context {
27 struct dev_pagemap *pgmap;
28 unsigned int page_mask;
29};
30
69e68b4f
KS
31static struct page *no_page_table(struct vm_area_struct *vma,
32 unsigned int flags)
4bbd4c77 33{
69e68b4f
KS
34 /*
35 * When core dumping an enormous anonymous area that nobody
36 * has touched so far, we don't want to allocate unnecessary pages or
37 * page tables. Return error instead of NULL to skip handle_mm_fault,
38 * then get_dump_page() will return NULL to leave a hole in the dump.
39 * But we can only make this optimization where a hole would surely
40 * be zero-filled if handle_mm_fault() actually did handle it.
41 */
42 if ((flags & FOLL_DUMP) && (!vma->vm_ops || !vma->vm_ops->fault))
43 return ERR_PTR(-EFAULT);
44 return NULL;
45}
4bbd4c77 46
1027e443
KS
47static int follow_pfn_pte(struct vm_area_struct *vma, unsigned long address,
48 pte_t *pte, unsigned int flags)
49{
50 /* No page to get reference */
51 if (flags & FOLL_GET)
52 return -EFAULT;
53
54 if (flags & FOLL_TOUCH) {
55 pte_t entry = *pte;
56
57 if (flags & FOLL_WRITE)
58 entry = pte_mkdirty(entry);
59 entry = pte_mkyoung(entry);
60
61 if (!pte_same(*pte, entry)) {
62 set_pte_at(vma->vm_mm, address, pte, entry);
63 update_mmu_cache(vma, address, pte);
64 }
65 }
66
67 /* Proper page table entry exists, but no corresponding struct page */
68 return -EEXIST;
69}
70
19be0eaf
LT
71/*
72 * FOLL_FORCE can write to even unwritable pte's, but only
73 * after we've gone through a COW cycle and they are dirty.
74 */
75static inline bool can_follow_write_pte(pte_t pte, unsigned int flags)
76{
f6f37321 77 return pte_write(pte) ||
19be0eaf
LT
78 ((flags & FOLL_FORCE) && (flags & FOLL_COW) && pte_dirty(pte));
79}
80
69e68b4f 81static struct page *follow_page_pte(struct vm_area_struct *vma,
df06b37f
KB
82 unsigned long address, pmd_t *pmd, unsigned int flags,
83 struct dev_pagemap **pgmap)
69e68b4f
KS
84{
85 struct mm_struct *mm = vma->vm_mm;
86 struct page *page;
87 spinlock_t *ptl;
88 pte_t *ptep, pte;
4bbd4c77 89
69e68b4f 90retry:
4bbd4c77 91 if (unlikely(pmd_bad(*pmd)))
69e68b4f 92 return no_page_table(vma, flags);
4bbd4c77
KS
93
94 ptep = pte_offset_map_lock(mm, pmd, address, &ptl);
4bbd4c77
KS
95 pte = *ptep;
96 if (!pte_present(pte)) {
97 swp_entry_t entry;
98 /*
99 * KSM's break_ksm() relies upon recognizing a ksm page
100 * even while it is being migrated, so for that case we
101 * need migration_entry_wait().
102 */
103 if (likely(!(flags & FOLL_MIGRATION)))
104 goto no_page;
0661a336 105 if (pte_none(pte))
4bbd4c77
KS
106 goto no_page;
107 entry = pte_to_swp_entry(pte);
108 if (!is_migration_entry(entry))
109 goto no_page;
110 pte_unmap_unlock(ptep, ptl);
111 migration_entry_wait(mm, pmd, address);
69e68b4f 112 goto retry;
4bbd4c77 113 }
8a0516ed 114 if ((flags & FOLL_NUMA) && pte_protnone(pte))
4bbd4c77 115 goto no_page;
19be0eaf 116 if ((flags & FOLL_WRITE) && !can_follow_write_pte(pte, flags)) {
69e68b4f
KS
117 pte_unmap_unlock(ptep, ptl);
118 return NULL;
119 }
4bbd4c77
KS
120
121 page = vm_normal_page(vma, address, pte);
3565fce3
DW
122 if (!page && pte_devmap(pte) && (flags & FOLL_GET)) {
123 /*
124 * Only return device mapping pages in the FOLL_GET case since
125 * they are only valid while holding the pgmap reference.
126 */
df06b37f
KB
127 *pgmap = get_dev_pagemap(pte_pfn(pte), *pgmap);
128 if (*pgmap)
3565fce3
DW
129 page = pte_page(pte);
130 else
131 goto no_page;
132 } else if (unlikely(!page)) {
1027e443
KS
133 if (flags & FOLL_DUMP) {
134 /* Avoid special (like zero) pages in core dumps */
135 page = ERR_PTR(-EFAULT);
136 goto out;
137 }
138
139 if (is_zero_pfn(pte_pfn(pte))) {
140 page = pte_page(pte);
141 } else {
142 int ret;
143
144 ret = follow_pfn_pte(vma, address, ptep, flags);
145 page = ERR_PTR(ret);
146 goto out;
147 }
4bbd4c77
KS
148 }
149
6742d293
KS
150 if (flags & FOLL_SPLIT && PageTransCompound(page)) {
151 int ret;
152 get_page(page);
153 pte_unmap_unlock(ptep, ptl);
154 lock_page(page);
155 ret = split_huge_page(page);
156 unlock_page(page);
157 put_page(page);
158 if (ret)
159 return ERR_PTR(ret);
160 goto retry;
161 }
162
df06b37f 163 if (flags & FOLL_GET)
ddc58f27 164 get_page(page);
4bbd4c77
KS
165 if (flags & FOLL_TOUCH) {
166 if ((flags & FOLL_WRITE) &&
167 !pte_dirty(pte) && !PageDirty(page))
168 set_page_dirty(page);
169 /*
170 * pte_mkyoung() would be more correct here, but atomic care
171 * is needed to avoid losing the dirty bit: it is easier to use
172 * mark_page_accessed().
173 */
174 mark_page_accessed(page);
175 }
de60f5f1 176 if ((flags & FOLL_MLOCK) && (vma->vm_flags & VM_LOCKED)) {
e90309c9
KS
177 /* Do not mlock pte-mapped THP */
178 if (PageTransCompound(page))
179 goto out;
180
4bbd4c77
KS
181 /*
182 * The preliminary mapping check is mainly to avoid the
183 * pointless overhead of lock_page on the ZERO_PAGE
184 * which might bounce very badly if there is contention.
185 *
186 * If the page is already locked, we don't need to
187 * handle it now - vmscan will handle it later if and
188 * when it attempts to reclaim the page.
189 */
190 if (page->mapping && trylock_page(page)) {
191 lru_add_drain(); /* push cached pages to LRU */
192 /*
193 * Because we lock page here, and migration is
194 * blocked by the pte's page reference, and we
195 * know the page is still mapped, we don't even
196 * need to check for file-cache page truncation.
197 */
198 mlock_vma_page(page);
199 unlock_page(page);
200 }
201 }
1027e443 202out:
4bbd4c77 203 pte_unmap_unlock(ptep, ptl);
4bbd4c77 204 return page;
4bbd4c77
KS
205no_page:
206 pte_unmap_unlock(ptep, ptl);
207 if (!pte_none(pte))
69e68b4f
KS
208 return NULL;
209 return no_page_table(vma, flags);
210}
211
080dbb61
AK
212static struct page *follow_pmd_mask(struct vm_area_struct *vma,
213 unsigned long address, pud_t *pudp,
df06b37f
KB
214 unsigned int flags,
215 struct follow_page_context *ctx)
69e68b4f 216{
68827280 217 pmd_t *pmd, pmdval;
69e68b4f
KS
218 spinlock_t *ptl;
219 struct page *page;
220 struct mm_struct *mm = vma->vm_mm;
221
080dbb61 222 pmd = pmd_offset(pudp, address);
68827280
HY
223 /*
224 * The READ_ONCE() will stabilize the pmdval in a register or
225 * on the stack so that it will stop changing under the code.
226 */
227 pmdval = READ_ONCE(*pmd);
228 if (pmd_none(pmdval))
69e68b4f 229 return no_page_table(vma, flags);
68827280 230 if (pmd_huge(pmdval) && vma->vm_flags & VM_HUGETLB) {
e66f17ff
NH
231 page = follow_huge_pmd(mm, address, pmd, flags);
232 if (page)
233 return page;
234 return no_page_table(vma, flags);
69e68b4f 235 }
68827280 236 if (is_hugepd(__hugepd(pmd_val(pmdval)))) {
4dc71451 237 page = follow_huge_pd(vma, address,
68827280 238 __hugepd(pmd_val(pmdval)), flags,
4dc71451
AK
239 PMD_SHIFT);
240 if (page)
241 return page;
242 return no_page_table(vma, flags);
243 }
84c3fc4e 244retry:
68827280 245 if (!pmd_present(pmdval)) {
84c3fc4e
ZY
246 if (likely(!(flags & FOLL_MIGRATION)))
247 return no_page_table(vma, flags);
248 VM_BUG_ON(thp_migration_supported() &&
68827280
HY
249 !is_pmd_migration_entry(pmdval));
250 if (is_pmd_migration_entry(pmdval))
84c3fc4e 251 pmd_migration_entry_wait(mm, pmd);
68827280
HY
252 pmdval = READ_ONCE(*pmd);
253 /*
254 * MADV_DONTNEED may convert the pmd to null because
255 * mmap_sem is held in read mode
256 */
257 if (pmd_none(pmdval))
258 return no_page_table(vma, flags);
84c3fc4e
ZY
259 goto retry;
260 }
68827280 261 if (pmd_devmap(pmdval)) {
3565fce3 262 ptl = pmd_lock(mm, pmd);
df06b37f 263 page = follow_devmap_pmd(vma, address, pmd, flags, &ctx->pgmap);
3565fce3
DW
264 spin_unlock(ptl);
265 if (page)
266 return page;
267 }
68827280 268 if (likely(!pmd_trans_huge(pmdval)))
df06b37f 269 return follow_page_pte(vma, address, pmd, flags, &ctx->pgmap);
6742d293 270
68827280 271 if ((flags & FOLL_NUMA) && pmd_protnone(pmdval))
db08f203
AK
272 return no_page_table(vma, flags);
273
84c3fc4e 274retry_locked:
6742d293 275 ptl = pmd_lock(mm, pmd);
68827280
HY
276 if (unlikely(pmd_none(*pmd))) {
277 spin_unlock(ptl);
278 return no_page_table(vma, flags);
279 }
84c3fc4e
ZY
280 if (unlikely(!pmd_present(*pmd))) {
281 spin_unlock(ptl);
282 if (likely(!(flags & FOLL_MIGRATION)))
283 return no_page_table(vma, flags);
284 pmd_migration_entry_wait(mm, pmd);
285 goto retry_locked;
286 }
6742d293
KS
287 if (unlikely(!pmd_trans_huge(*pmd))) {
288 spin_unlock(ptl);
df06b37f 289 return follow_page_pte(vma, address, pmd, flags, &ctx->pgmap);
6742d293 290 }
6742d293
KS
291 if (flags & FOLL_SPLIT) {
292 int ret;
293 page = pmd_page(*pmd);
294 if (is_huge_zero_page(page)) {
295 spin_unlock(ptl);
296 ret = 0;
78ddc534 297 split_huge_pmd(vma, pmd, address);
337d9abf
NH
298 if (pmd_trans_unstable(pmd))
299 ret = -EBUSY;
6742d293
KS
300 } else {
301 get_page(page);
69e68b4f 302 spin_unlock(ptl);
6742d293
KS
303 lock_page(page);
304 ret = split_huge_page(page);
305 unlock_page(page);
306 put_page(page);
baa355fd
KS
307 if (pmd_none(*pmd))
308 return no_page_table(vma, flags);
6742d293
KS
309 }
310
311 return ret ? ERR_PTR(ret) :
df06b37f 312 follow_page_pte(vma, address, pmd, flags, &ctx->pgmap);
69e68b4f 313 }
6742d293
KS
314 page = follow_trans_huge_pmd(vma, address, pmd, flags);
315 spin_unlock(ptl);
df06b37f 316 ctx->page_mask = HPAGE_PMD_NR - 1;
6742d293 317 return page;
4bbd4c77
KS
318}
319
080dbb61
AK
320static struct page *follow_pud_mask(struct vm_area_struct *vma,
321 unsigned long address, p4d_t *p4dp,
df06b37f
KB
322 unsigned int flags,
323 struct follow_page_context *ctx)
080dbb61
AK
324{
325 pud_t *pud;
326 spinlock_t *ptl;
327 struct page *page;
328 struct mm_struct *mm = vma->vm_mm;
329
330 pud = pud_offset(p4dp, address);
331 if (pud_none(*pud))
332 return no_page_table(vma, flags);
333 if (pud_huge(*pud) && vma->vm_flags & VM_HUGETLB) {
334 page = follow_huge_pud(mm, address, pud, flags);
335 if (page)
336 return page;
337 return no_page_table(vma, flags);
338 }
4dc71451
AK
339 if (is_hugepd(__hugepd(pud_val(*pud)))) {
340 page = follow_huge_pd(vma, address,
341 __hugepd(pud_val(*pud)), flags,
342 PUD_SHIFT);
343 if (page)
344 return page;
345 return no_page_table(vma, flags);
346 }
080dbb61
AK
347 if (pud_devmap(*pud)) {
348 ptl = pud_lock(mm, pud);
df06b37f 349 page = follow_devmap_pud(vma, address, pud, flags, &ctx->pgmap);
080dbb61
AK
350 spin_unlock(ptl);
351 if (page)
352 return page;
353 }
354 if (unlikely(pud_bad(*pud)))
355 return no_page_table(vma, flags);
356
df06b37f 357 return follow_pmd_mask(vma, address, pud, flags, ctx);
080dbb61
AK
358}
359
080dbb61
AK
360static struct page *follow_p4d_mask(struct vm_area_struct *vma,
361 unsigned long address, pgd_t *pgdp,
df06b37f
KB
362 unsigned int flags,
363 struct follow_page_context *ctx)
080dbb61
AK
364{
365 p4d_t *p4d;
4dc71451 366 struct page *page;
080dbb61
AK
367
368 p4d = p4d_offset(pgdp, address);
369 if (p4d_none(*p4d))
370 return no_page_table(vma, flags);
371 BUILD_BUG_ON(p4d_huge(*p4d));
372 if (unlikely(p4d_bad(*p4d)))
373 return no_page_table(vma, flags);
374
4dc71451
AK
375 if (is_hugepd(__hugepd(p4d_val(*p4d)))) {
376 page = follow_huge_pd(vma, address,
377 __hugepd(p4d_val(*p4d)), flags,
378 P4D_SHIFT);
379 if (page)
380 return page;
381 return no_page_table(vma, flags);
382 }
df06b37f 383 return follow_pud_mask(vma, address, p4d, flags, ctx);
080dbb61
AK
384}
385
386/**
387 * follow_page_mask - look up a page descriptor from a user-virtual address
388 * @vma: vm_area_struct mapping @address
389 * @address: virtual address to look up
390 * @flags: flags modifying lookup behaviour
78179556
MR
391 * @ctx: contains dev_pagemap for %ZONE_DEVICE memory pinning and a
392 * pointer to output page_mask
080dbb61
AK
393 *
394 * @flags can have FOLL_ flags set, defined in <linux/mm.h>
395 *
78179556
MR
396 * When getting pages from ZONE_DEVICE memory, the @ctx->pgmap caches
397 * the device's dev_pagemap metadata to avoid repeating expensive lookups.
398 *
399 * On output, the @ctx->page_mask is set according to the size of the page.
400 *
401 * Return: the mapped (struct page *), %NULL if no mapping exists, or
080dbb61
AK
402 * an error pointer if there is a mapping to something not represented
403 * by a page descriptor (see also vm_normal_page()).
404 */
405struct page *follow_page_mask(struct vm_area_struct *vma,
406 unsigned long address, unsigned int flags,
df06b37f 407 struct follow_page_context *ctx)
080dbb61
AK
408{
409 pgd_t *pgd;
410 struct page *page;
411 struct mm_struct *mm = vma->vm_mm;
412
df06b37f 413 ctx->page_mask = 0;
080dbb61
AK
414
415 /* make this handle hugepd */
416 page = follow_huge_addr(mm, address, flags & FOLL_WRITE);
417 if (!IS_ERR(page)) {
418 BUG_ON(flags & FOLL_GET);
419 return page;
420 }
421
422 pgd = pgd_offset(mm, address);
423
424 if (pgd_none(*pgd) || unlikely(pgd_bad(*pgd)))
425 return no_page_table(vma, flags);
426
faaa5b62
AK
427 if (pgd_huge(*pgd)) {
428 page = follow_huge_pgd(mm, address, pgd, flags);
429 if (page)
430 return page;
431 return no_page_table(vma, flags);
432 }
4dc71451
AK
433 if (is_hugepd(__hugepd(pgd_val(*pgd)))) {
434 page = follow_huge_pd(vma, address,
435 __hugepd(pgd_val(*pgd)), flags,
436 PGDIR_SHIFT);
437 if (page)
438 return page;
439 return no_page_table(vma, flags);
440 }
faaa5b62 441
df06b37f
KB
442 return follow_p4d_mask(vma, address, pgd, flags, ctx);
443}
444
445struct page *follow_page(struct vm_area_struct *vma, unsigned long address,
446 unsigned int foll_flags)
447{
448 struct follow_page_context ctx = { NULL };
449 struct page *page;
450
451 page = follow_page_mask(vma, address, foll_flags, &ctx);
452 if (ctx.pgmap)
453 put_dev_pagemap(ctx.pgmap);
454 return page;
080dbb61
AK
455}
456
f2b495ca
KS
457static int get_gate_page(struct mm_struct *mm, unsigned long address,
458 unsigned int gup_flags, struct vm_area_struct **vma,
459 struct page **page)
460{
461 pgd_t *pgd;
c2febafc 462 p4d_t *p4d;
f2b495ca
KS
463 pud_t *pud;
464 pmd_t *pmd;
465 pte_t *pte;
466 int ret = -EFAULT;
467
468 /* user gate pages are read-only */
469 if (gup_flags & FOLL_WRITE)
470 return -EFAULT;
471 if (address > TASK_SIZE)
472 pgd = pgd_offset_k(address);
473 else
474 pgd = pgd_offset_gate(mm, address);
475 BUG_ON(pgd_none(*pgd));
c2febafc
KS
476 p4d = p4d_offset(pgd, address);
477 BUG_ON(p4d_none(*p4d));
478 pud = pud_offset(p4d, address);
f2b495ca
KS
479 BUG_ON(pud_none(*pud));
480 pmd = pmd_offset(pud, address);
84c3fc4e 481 if (!pmd_present(*pmd))
f2b495ca
KS
482 return -EFAULT;
483 VM_BUG_ON(pmd_trans_huge(*pmd));
484 pte = pte_offset_map(pmd, address);
485 if (pte_none(*pte))
486 goto unmap;
487 *vma = get_gate_vma(mm);
488 if (!page)
489 goto out;
490 *page = vm_normal_page(*vma, address, *pte);
491 if (!*page) {
492 if ((gup_flags & FOLL_DUMP) || !is_zero_pfn(pte_pfn(*pte)))
493 goto unmap;
494 *page = pte_page(*pte);
df6ad698
JG
495
496 /*
497 * This should never happen (a device public page in the gate
498 * area).
499 */
500 if (is_device_public_page(*page))
501 goto unmap;
f2b495ca
KS
502 }
503 get_page(*page);
504out:
505 ret = 0;
506unmap:
507 pte_unmap(pte);
508 return ret;
509}
510
9a95f3cf
PC
511/*
512 * mmap_sem must be held on entry. If @nonblocking != NULL and
513 * *@flags does not include FOLL_NOWAIT, the mmap_sem may be released.
514 * If it is, *@nonblocking will be set to 0 and -EBUSY returned.
515 */
16744483
KS
516static int faultin_page(struct task_struct *tsk, struct vm_area_struct *vma,
517 unsigned long address, unsigned int *flags, int *nonblocking)
518{
16744483 519 unsigned int fault_flags = 0;
2b740303 520 vm_fault_t ret;
16744483 521
de60f5f1
EM
522 /* mlock all present pages, but do not fault in new pages */
523 if ((*flags & (FOLL_POPULATE | FOLL_MLOCK)) == FOLL_MLOCK)
524 return -ENOENT;
16744483
KS
525 if (*flags & FOLL_WRITE)
526 fault_flags |= FAULT_FLAG_WRITE;
1b2ee126
DH
527 if (*flags & FOLL_REMOTE)
528 fault_flags |= FAULT_FLAG_REMOTE;
16744483
KS
529 if (nonblocking)
530 fault_flags |= FAULT_FLAG_ALLOW_RETRY;
531 if (*flags & FOLL_NOWAIT)
532 fault_flags |= FAULT_FLAG_ALLOW_RETRY | FAULT_FLAG_RETRY_NOWAIT;
234b239b
ALC
533 if (*flags & FOLL_TRIED) {
534 VM_WARN_ON_ONCE(fault_flags & FAULT_FLAG_ALLOW_RETRY);
535 fault_flags |= FAULT_FLAG_TRIED;
536 }
16744483 537
dcddffd4 538 ret = handle_mm_fault(vma, address, fault_flags);
16744483 539 if (ret & VM_FAULT_ERROR) {
9a291a7c
JM
540 int err = vm_fault_to_errno(ret, *flags);
541
542 if (err)
543 return err;
16744483
KS
544 BUG();
545 }
546
547 if (tsk) {
548 if (ret & VM_FAULT_MAJOR)
549 tsk->maj_flt++;
550 else
551 tsk->min_flt++;
552 }
553
554 if (ret & VM_FAULT_RETRY) {
96312e61 555 if (nonblocking && !(fault_flags & FAULT_FLAG_RETRY_NOWAIT))
16744483
KS
556 *nonblocking = 0;
557 return -EBUSY;
558 }
559
560 /*
561 * The VM_FAULT_WRITE bit tells us that do_wp_page has broken COW when
562 * necessary, even if maybe_mkwrite decided not to set pte_write. We
563 * can thus safely do subsequent page lookups as if they were reads.
564 * But only do so when looping for pte_write is futile: in some cases
565 * userspace may also be wanting to write to the gotten user page,
566 * which a read fault here might prevent (a readonly page might get
567 * reCOWed by userspace write).
568 */
569 if ((ret & VM_FAULT_WRITE) && !(vma->vm_flags & VM_WRITE))
2923117b 570 *flags |= FOLL_COW;
16744483
KS
571 return 0;
572}
573
fa5bb209
KS
574static int check_vma_flags(struct vm_area_struct *vma, unsigned long gup_flags)
575{
576 vm_flags_t vm_flags = vma->vm_flags;
1b2ee126
DH
577 int write = (gup_flags & FOLL_WRITE);
578 int foreign = (gup_flags & FOLL_REMOTE);
fa5bb209
KS
579
580 if (vm_flags & (VM_IO | VM_PFNMAP))
581 return -EFAULT;
582
7f7ccc2c
WT
583 if (gup_flags & FOLL_ANON && !vma_is_anonymous(vma))
584 return -EFAULT;
585
1b2ee126 586 if (write) {
fa5bb209
KS
587 if (!(vm_flags & VM_WRITE)) {
588 if (!(gup_flags & FOLL_FORCE))
589 return -EFAULT;
590 /*
591 * We used to let the write,force case do COW in a
592 * VM_MAYWRITE VM_SHARED !VM_WRITE vma, so ptrace could
593 * set a breakpoint in a read-only mapping of an
594 * executable, without corrupting the file (yet only
595 * when that file had been opened for writing!).
596 * Anon pages in shared mappings are surprising: now
597 * just reject it.
598 */
46435364 599 if (!is_cow_mapping(vm_flags))
fa5bb209 600 return -EFAULT;
fa5bb209
KS
601 }
602 } else if (!(vm_flags & VM_READ)) {
603 if (!(gup_flags & FOLL_FORCE))
604 return -EFAULT;
605 /*
606 * Is there actually any vma we can reach here which does not
607 * have VM_MAYREAD set?
608 */
609 if (!(vm_flags & VM_MAYREAD))
610 return -EFAULT;
611 }
d61172b4
DH
612 /*
613 * gups are always data accesses, not instruction
614 * fetches, so execute=false here
615 */
616 if (!arch_vma_access_permitted(vma, write, false, foreign))
33a709b2 617 return -EFAULT;
fa5bb209
KS
618 return 0;
619}
620
4bbd4c77
KS
621/**
622 * __get_user_pages() - pin user pages in memory
623 * @tsk: task_struct of target task
624 * @mm: mm_struct of target mm
625 * @start: starting user address
626 * @nr_pages: number of pages from start to pin
627 * @gup_flags: flags modifying pin behaviour
628 * @pages: array that receives pointers to the pages pinned.
629 * Should be at least nr_pages long. Or NULL, if caller
630 * only intends to ensure the pages are faulted in.
631 * @vmas: array of pointers to vmas corresponding to each page.
632 * Or NULL if the caller does not require them.
633 * @nonblocking: whether waiting for disk IO or mmap_sem contention
634 *
635 * Returns number of pages pinned. This may be fewer than the number
636 * requested. If nr_pages is 0 or negative, returns 0. If no pages
637 * were pinned, returns -errno. Each page returned must be released
638 * with a put_page() call when it is finished with. vmas will only
639 * remain valid while mmap_sem is held.
640 *
9a95f3cf 641 * Must be called with mmap_sem held. It may be released. See below.
4bbd4c77
KS
642 *
643 * __get_user_pages walks a process's page tables and takes a reference to
644 * each struct page that each user address corresponds to at a given
645 * instant. That is, it takes the page that would be accessed if a user
646 * thread accesses the given user virtual address at that instant.
647 *
648 * This does not guarantee that the page exists in the user mappings when
649 * __get_user_pages returns, and there may even be a completely different
650 * page there in some cases (eg. if mmapped pagecache has been invalidated
651 * and subsequently re faulted). However it does guarantee that the page
652 * won't be freed completely. And mostly callers simply care that the page
653 * contains data that was valid *at some point in time*. Typically, an IO
654 * or similar operation cannot guarantee anything stronger anyway because
655 * locks can't be held over the syscall boundary.
656 *
657 * If @gup_flags & FOLL_WRITE == 0, the page must not be written to. If
658 * the page is written to, set_page_dirty (or set_page_dirty_lock, as
659 * appropriate) must be called after the page is finished with, and
660 * before put_page is called.
661 *
662 * If @nonblocking != NULL, __get_user_pages will not wait for disk IO
663 * or mmap_sem contention, and if waiting is needed to pin all pages,
9a95f3cf
PC
664 * *@nonblocking will be set to 0. Further, if @gup_flags does not
665 * include FOLL_NOWAIT, the mmap_sem will be released via up_read() in
666 * this case.
667 *
668 * A caller using such a combination of @nonblocking and @gup_flags
669 * must therefore hold the mmap_sem for reading only, and recognize
670 * when it's been released. Otherwise, it must be held for either
671 * reading or writing and will not be released.
4bbd4c77
KS
672 *
673 * In most cases, get_user_pages or get_user_pages_fast should be used
674 * instead of __get_user_pages. __get_user_pages should be used only if
675 * you need some special @gup_flags.
676 */
0d731759 677static long __get_user_pages(struct task_struct *tsk, struct mm_struct *mm,
4bbd4c77
KS
678 unsigned long start, unsigned long nr_pages,
679 unsigned int gup_flags, struct page **pages,
680 struct vm_area_struct **vmas, int *nonblocking)
681{
df06b37f 682 long ret = 0, i = 0;
fa5bb209 683 struct vm_area_struct *vma = NULL;
df06b37f 684 struct follow_page_context ctx = { NULL };
4bbd4c77
KS
685
686 if (!nr_pages)
687 return 0;
688
689 VM_BUG_ON(!!pages != !!(gup_flags & FOLL_GET));
690
691 /*
692 * If FOLL_FORCE is set then do not force a full fault as the hinting
693 * fault information is unrelated to the reference behaviour of a task
694 * using the address space
695 */
696 if (!(gup_flags & FOLL_FORCE))
697 gup_flags |= FOLL_NUMA;
698
4bbd4c77 699 do {
fa5bb209
KS
700 struct page *page;
701 unsigned int foll_flags = gup_flags;
702 unsigned int page_increm;
703
704 /* first iteration or cross vma bound */
705 if (!vma || start >= vma->vm_end) {
706 vma = find_extend_vma(mm, start);
707 if (!vma && in_gate_area(mm, start)) {
fa5bb209
KS
708 ret = get_gate_page(mm, start & PAGE_MASK,
709 gup_flags, &vma,
710 pages ? &pages[i] : NULL);
711 if (ret)
08be37b7 712 goto out;
df06b37f 713 ctx.page_mask = 0;
fa5bb209
KS
714 goto next_page;
715 }
4bbd4c77 716
df06b37f
KB
717 if (!vma || check_vma_flags(vma, gup_flags)) {
718 ret = -EFAULT;
719 goto out;
720 }
fa5bb209
KS
721 if (is_vm_hugetlb_page(vma)) {
722 i = follow_hugetlb_page(mm, vma, pages, vmas,
723 &start, &nr_pages, i,
87ffc118 724 gup_flags, nonblocking);
fa5bb209 725 continue;
4bbd4c77 726 }
fa5bb209
KS
727 }
728retry:
729 /*
730 * If we have a pending SIGKILL, don't keep faulting pages and
731 * potentially allocating memory.
732 */
fa45f116 733 if (fatal_signal_pending(current)) {
df06b37f
KB
734 ret = -ERESTARTSYS;
735 goto out;
736 }
fa5bb209 737 cond_resched();
df06b37f
KB
738
739 page = follow_page_mask(vma, start, foll_flags, &ctx);
fa5bb209 740 if (!page) {
fa5bb209
KS
741 ret = faultin_page(tsk, vma, start, &foll_flags,
742 nonblocking);
743 switch (ret) {
744 case 0:
745 goto retry;
df06b37f
KB
746 case -EBUSY:
747 ret = 0;
748 /* FALLTHRU */
fa5bb209
KS
749 case -EFAULT:
750 case -ENOMEM:
751 case -EHWPOISON:
df06b37f 752 goto out;
fa5bb209
KS
753 case -ENOENT:
754 goto next_page;
4bbd4c77 755 }
fa5bb209 756 BUG();
1027e443
KS
757 } else if (PTR_ERR(page) == -EEXIST) {
758 /*
759 * Proper page table entry exists, but no corresponding
760 * struct page.
761 */
762 goto next_page;
763 } else if (IS_ERR(page)) {
df06b37f
KB
764 ret = PTR_ERR(page);
765 goto out;
1027e443 766 }
fa5bb209
KS
767 if (pages) {
768 pages[i] = page;
769 flush_anon_page(vma, page, start);
770 flush_dcache_page(page);
df06b37f 771 ctx.page_mask = 0;
4bbd4c77 772 }
4bbd4c77 773next_page:
fa5bb209
KS
774 if (vmas) {
775 vmas[i] = vma;
df06b37f 776 ctx.page_mask = 0;
fa5bb209 777 }
df06b37f 778 page_increm = 1 + (~(start >> PAGE_SHIFT) & ctx.page_mask);
fa5bb209
KS
779 if (page_increm > nr_pages)
780 page_increm = nr_pages;
781 i += page_increm;
782 start += page_increm * PAGE_SIZE;
783 nr_pages -= page_increm;
4bbd4c77 784 } while (nr_pages);
df06b37f
KB
785out:
786 if (ctx.pgmap)
787 put_dev_pagemap(ctx.pgmap);
788 return i ? i : ret;
4bbd4c77 789}
4bbd4c77 790
771ab430
TK
791static bool vma_permits_fault(struct vm_area_struct *vma,
792 unsigned int fault_flags)
d4925e00 793{
1b2ee126
DH
794 bool write = !!(fault_flags & FAULT_FLAG_WRITE);
795 bool foreign = !!(fault_flags & FAULT_FLAG_REMOTE);
33a709b2 796 vm_flags_t vm_flags = write ? VM_WRITE : VM_READ;
d4925e00
DH
797
798 if (!(vm_flags & vma->vm_flags))
799 return false;
800
33a709b2
DH
801 /*
802 * The architecture might have a hardware protection
1b2ee126 803 * mechanism other than read/write that can deny access.
d61172b4
DH
804 *
805 * gup always represents data access, not instruction
806 * fetches, so execute=false here:
33a709b2 807 */
d61172b4 808 if (!arch_vma_access_permitted(vma, write, false, foreign))
33a709b2
DH
809 return false;
810
d4925e00
DH
811 return true;
812}
813
4bbd4c77
KS
814/*
815 * fixup_user_fault() - manually resolve a user page fault
816 * @tsk: the task_struct to use for page fault accounting, or
817 * NULL if faults are not to be recorded.
818 * @mm: mm_struct of target mm
819 * @address: user address
820 * @fault_flags:flags to pass down to handle_mm_fault()
4a9e1cda
DD
821 * @unlocked: did we unlock the mmap_sem while retrying, maybe NULL if caller
822 * does not allow retry
4bbd4c77
KS
823 *
824 * This is meant to be called in the specific scenario where for locking reasons
825 * we try to access user memory in atomic context (within a pagefault_disable()
826 * section), this returns -EFAULT, and we want to resolve the user fault before
827 * trying again.
828 *
829 * Typically this is meant to be used by the futex code.
830 *
831 * The main difference with get_user_pages() is that this function will
832 * unconditionally call handle_mm_fault() which will in turn perform all the
833 * necessary SW fixup of the dirty and young bits in the PTE, while
4a9e1cda 834 * get_user_pages() only guarantees to update these in the struct page.
4bbd4c77
KS
835 *
836 * This is important for some architectures where those bits also gate the
837 * access permission to the page because they are maintained in software. On
838 * such architectures, gup() will not be enough to make a subsequent access
839 * succeed.
840 *
4a9e1cda
DD
841 * This function will not return with an unlocked mmap_sem. So it has not the
842 * same semantics wrt the @mm->mmap_sem as does filemap_fault().
4bbd4c77
KS
843 */
844int fixup_user_fault(struct task_struct *tsk, struct mm_struct *mm,
4a9e1cda
DD
845 unsigned long address, unsigned int fault_flags,
846 bool *unlocked)
4bbd4c77
KS
847{
848 struct vm_area_struct *vma;
2b740303 849 vm_fault_t ret, major = 0;
4a9e1cda
DD
850
851 if (unlocked)
852 fault_flags |= FAULT_FLAG_ALLOW_RETRY;
4bbd4c77 853
4a9e1cda 854retry:
4bbd4c77
KS
855 vma = find_extend_vma(mm, address);
856 if (!vma || address < vma->vm_start)
857 return -EFAULT;
858
d4925e00 859 if (!vma_permits_fault(vma, fault_flags))
4bbd4c77
KS
860 return -EFAULT;
861
dcddffd4 862 ret = handle_mm_fault(vma, address, fault_flags);
4a9e1cda 863 major |= ret & VM_FAULT_MAJOR;
4bbd4c77 864 if (ret & VM_FAULT_ERROR) {
9a291a7c
JM
865 int err = vm_fault_to_errno(ret, 0);
866
867 if (err)
868 return err;
4bbd4c77
KS
869 BUG();
870 }
4a9e1cda
DD
871
872 if (ret & VM_FAULT_RETRY) {
873 down_read(&mm->mmap_sem);
874 if (!(fault_flags & FAULT_FLAG_TRIED)) {
875 *unlocked = true;
876 fault_flags &= ~FAULT_FLAG_ALLOW_RETRY;
877 fault_flags |= FAULT_FLAG_TRIED;
878 goto retry;
879 }
880 }
881
4bbd4c77 882 if (tsk) {
4a9e1cda 883 if (major)
4bbd4c77
KS
884 tsk->maj_flt++;
885 else
886 tsk->min_flt++;
887 }
888 return 0;
889}
add6a0cd 890EXPORT_SYMBOL_GPL(fixup_user_fault);
4bbd4c77 891
f0818f47
AA
892static __always_inline long __get_user_pages_locked(struct task_struct *tsk,
893 struct mm_struct *mm,
894 unsigned long start,
895 unsigned long nr_pages,
f0818f47
AA
896 struct page **pages,
897 struct vm_area_struct **vmas,
e716712f 898 int *locked,
0fd71a56 899 unsigned int flags)
f0818f47 900{
f0818f47
AA
901 long ret, pages_done;
902 bool lock_dropped;
903
904 if (locked) {
905 /* if VM_FAULT_RETRY can be returned, vmas become invalid */
906 BUG_ON(vmas);
907 /* check caller initialized locked */
908 BUG_ON(*locked != 1);
909 }
910
911 if (pages)
912 flags |= FOLL_GET;
f0818f47
AA
913
914 pages_done = 0;
915 lock_dropped = false;
916 for (;;) {
917 ret = __get_user_pages(tsk, mm, start, nr_pages, flags, pages,
918 vmas, locked);
919 if (!locked)
920 /* VM_FAULT_RETRY couldn't trigger, bypass */
921 return ret;
922
923 /* VM_FAULT_RETRY cannot return errors */
924 if (!*locked) {
925 BUG_ON(ret < 0);
926 BUG_ON(ret >= nr_pages);
927 }
928
929 if (!pages)
930 /* If it's a prefault don't insist harder */
931 return ret;
932
933 if (ret > 0) {
934 nr_pages -= ret;
935 pages_done += ret;
936 if (!nr_pages)
937 break;
938 }
939 if (*locked) {
96312e61
AA
940 /*
941 * VM_FAULT_RETRY didn't trigger or it was a
942 * FOLL_NOWAIT.
943 */
f0818f47
AA
944 if (!pages_done)
945 pages_done = ret;
946 break;
947 }
948 /* VM_FAULT_RETRY triggered, so seek to the faulting offset */
949 pages += ret;
950 start += ret << PAGE_SHIFT;
951
952 /*
953 * Repeat on the address that fired VM_FAULT_RETRY
954 * without FAULT_FLAG_ALLOW_RETRY but with
955 * FAULT_FLAG_TRIED.
956 */
957 *locked = 1;
958 lock_dropped = true;
959 down_read(&mm->mmap_sem);
960 ret = __get_user_pages(tsk, mm, start, 1, flags | FOLL_TRIED,
961 pages, NULL, NULL);
962 if (ret != 1) {
963 BUG_ON(ret > 1);
964 if (!pages_done)
965 pages_done = ret;
966 break;
967 }
968 nr_pages--;
969 pages_done++;
970 if (!nr_pages)
971 break;
972 pages++;
973 start += PAGE_SIZE;
974 }
e716712f 975 if (lock_dropped && *locked) {
f0818f47
AA
976 /*
977 * We must let the caller know we temporarily dropped the lock
978 * and so the critical section protected by it was lost.
979 */
980 up_read(&mm->mmap_sem);
981 *locked = 0;
982 }
983 return pages_done;
984}
985
986/*
987 * We can leverage the VM_FAULT_RETRY functionality in the page fault
988 * paths better by using either get_user_pages_locked() or
989 * get_user_pages_unlocked().
990 *
991 * get_user_pages_locked() is suitable to replace the form:
992 *
993 * down_read(&mm->mmap_sem);
994 * do_something()
995 * get_user_pages(tsk, mm, ..., pages, NULL);
996 * up_read(&mm->mmap_sem);
997 *
998 * to:
999 *
1000 * int locked = 1;
1001 * down_read(&mm->mmap_sem);
1002 * do_something()
1003 * get_user_pages_locked(tsk, mm, ..., pages, &locked);
1004 * if (locked)
1005 * up_read(&mm->mmap_sem);
1006 */
c12d2da5 1007long get_user_pages_locked(unsigned long start, unsigned long nr_pages,
3b913179 1008 unsigned int gup_flags, struct page **pages,
f0818f47
AA
1009 int *locked)
1010{
cde70140 1011 return __get_user_pages_locked(current, current->mm, start, nr_pages,
e716712f 1012 pages, NULL, locked,
3b913179 1013 gup_flags | FOLL_TOUCH);
f0818f47 1014}
c12d2da5 1015EXPORT_SYMBOL(get_user_pages_locked);
f0818f47
AA
1016
1017/*
1018 * get_user_pages_unlocked() is suitable to replace the form:
1019 *
1020 * down_read(&mm->mmap_sem);
1021 * get_user_pages(tsk, mm, ..., pages, NULL);
1022 * up_read(&mm->mmap_sem);
1023 *
1024 * with:
1025 *
1026 * get_user_pages_unlocked(tsk, mm, ..., pages);
1027 *
1028 * It is functionally equivalent to get_user_pages_fast so
80a79516
LS
1029 * get_user_pages_fast should be used instead if specific gup_flags
1030 * (e.g. FOLL_FORCE) are not required.
f0818f47 1031 */
c12d2da5 1032long get_user_pages_unlocked(unsigned long start, unsigned long nr_pages,
c164154f 1033 struct page **pages, unsigned int gup_flags)
f0818f47 1034{
c803c9c6
AV
1035 struct mm_struct *mm = current->mm;
1036 int locked = 1;
1037 long ret;
1038
1039 down_read(&mm->mmap_sem);
1040 ret = __get_user_pages_locked(current, mm, start, nr_pages, pages, NULL,
e716712f 1041 &locked, gup_flags | FOLL_TOUCH);
c803c9c6
AV
1042 if (locked)
1043 up_read(&mm->mmap_sem);
1044 return ret;
f0818f47 1045}
c12d2da5 1046EXPORT_SYMBOL(get_user_pages_unlocked);
f0818f47 1047
4bbd4c77 1048/*
1e987790 1049 * get_user_pages_remote() - pin user pages in memory
4bbd4c77
KS
1050 * @tsk: the task_struct to use for page fault accounting, or
1051 * NULL if faults are not to be recorded.
1052 * @mm: mm_struct of target mm
1053 * @start: starting user address
1054 * @nr_pages: number of pages from start to pin
9beae1ea 1055 * @gup_flags: flags modifying lookup behaviour
4bbd4c77
KS
1056 * @pages: array that receives pointers to the pages pinned.
1057 * Should be at least nr_pages long. Or NULL, if caller
1058 * only intends to ensure the pages are faulted in.
1059 * @vmas: array of pointers to vmas corresponding to each page.
1060 * Or NULL if the caller does not require them.
5b56d49f
LS
1061 * @locked: pointer to lock flag indicating whether lock is held and
1062 * subsequently whether VM_FAULT_RETRY functionality can be
1063 * utilised. Lock must initially be held.
4bbd4c77
KS
1064 *
1065 * Returns number of pages pinned. This may be fewer than the number
1066 * requested. If nr_pages is 0 or negative, returns 0. If no pages
1067 * were pinned, returns -errno. Each page returned must be released
1068 * with a put_page() call when it is finished with. vmas will only
1069 * remain valid while mmap_sem is held.
1070 *
1071 * Must be called with mmap_sem held for read or write.
1072 *
1073 * get_user_pages walks a process's page tables and takes a reference to
1074 * each struct page that each user address corresponds to at a given
1075 * instant. That is, it takes the page that would be accessed if a user
1076 * thread accesses the given user virtual address at that instant.
1077 *
1078 * This does not guarantee that the page exists in the user mappings when
1079 * get_user_pages returns, and there may even be a completely different
1080 * page there in some cases (eg. if mmapped pagecache has been invalidated
1081 * and subsequently re faulted). However it does guarantee that the page
1082 * won't be freed completely. And mostly callers simply care that the page
1083 * contains data that was valid *at some point in time*. Typically, an IO
1084 * or similar operation cannot guarantee anything stronger anyway because
1085 * locks can't be held over the syscall boundary.
1086 *
9beae1ea
LS
1087 * If gup_flags & FOLL_WRITE == 0, the page must not be written to. If the page
1088 * is written to, set_page_dirty (or set_page_dirty_lock, as appropriate) must
1089 * be called after the page is finished with, and before put_page is called.
4bbd4c77
KS
1090 *
1091 * get_user_pages is typically used for fewer-copy IO operations, to get a
1092 * handle on the memory by some means other than accesses via the user virtual
1093 * addresses. The pages may be submitted for DMA to devices or accessed via
1094 * their kernel linear mapping (via the kmap APIs). Care should be taken to
1095 * use the correct cache flushing APIs.
1096 *
1097 * See also get_user_pages_fast, for performance critical applications.
f0818f47
AA
1098 *
1099 * get_user_pages should be phased out in favor of
1100 * get_user_pages_locked|unlocked or get_user_pages_fast. Nothing
1101 * should use get_user_pages because it cannot pass
1102 * FAULT_FLAG_ALLOW_RETRY to handle_mm_fault.
4bbd4c77 1103 */
1e987790
DH
1104long get_user_pages_remote(struct task_struct *tsk, struct mm_struct *mm,
1105 unsigned long start, unsigned long nr_pages,
9beae1ea 1106 unsigned int gup_flags, struct page **pages,
5b56d49f 1107 struct vm_area_struct **vmas, int *locked)
4bbd4c77 1108{
859110d7 1109 return __get_user_pages_locked(tsk, mm, start, nr_pages, pages, vmas,
e716712f 1110 locked,
9beae1ea 1111 gup_flags | FOLL_TOUCH | FOLL_REMOTE);
1e987790
DH
1112}
1113EXPORT_SYMBOL(get_user_pages_remote);
1114
1115/*
d4edcf0d
DH
1116 * This is the same as get_user_pages_remote(), just with a
1117 * less-flexible calling convention where we assume that the task
5b56d49f
LS
1118 * and mm being operated on are the current task's and don't allow
1119 * passing of a locked parameter. We also obviously don't pass
1120 * FOLL_REMOTE in here.
1e987790 1121 */
c12d2da5 1122long get_user_pages(unsigned long start, unsigned long nr_pages,
768ae309 1123 unsigned int gup_flags, struct page **pages,
1e987790
DH
1124 struct vm_area_struct **vmas)
1125{
cde70140 1126 return __get_user_pages_locked(current, current->mm, start, nr_pages,
e716712f 1127 pages, vmas, NULL,
768ae309 1128 gup_flags | FOLL_TOUCH);
4bbd4c77 1129}
c12d2da5 1130EXPORT_SYMBOL(get_user_pages);
4bbd4c77 1131
9a4e9f3b
AK
1132#if defined(CONFIG_FS_DAX) || defined (CONFIG_CMA)
1133
2bb6d283 1134#ifdef CONFIG_FS_DAX
9a4e9f3b
AK
1135static bool check_dax_vmas(struct vm_area_struct **vmas, long nr_pages)
1136{
1137 long i;
1138 struct vm_area_struct *vma_prev = NULL;
1139
1140 for (i = 0; i < nr_pages; i++) {
1141 struct vm_area_struct *vma = vmas[i];
1142
1143 if (vma == vma_prev)
1144 continue;
1145
1146 vma_prev = vma;
1147
1148 if (vma_is_fsdax(vma))
1149 return true;
1150 }
1151 return false;
1152}
1153#else
1154static inline bool check_dax_vmas(struct vm_area_struct **vmas, long nr_pages)
1155{
1156 return false;
1157}
1158#endif
1159
1160#ifdef CONFIG_CMA
1161static struct page *new_non_cma_page(struct page *page, unsigned long private)
1162{
1163 /*
1164 * We want to make sure we allocate the new page from the same node
1165 * as the source page.
1166 */
1167 int nid = page_to_nid(page);
1168 /*
1169 * Trying to allocate a page for migration. Ignore allocation
1170 * failure warnings. We don't force __GFP_THISNODE here because
1171 * this node here is the node where we have CMA reservation and
1172 * in some case these nodes will have really less non movable
1173 * allocation memory.
1174 */
1175 gfp_t gfp_mask = GFP_USER | __GFP_NOWARN;
1176
1177 if (PageHighMem(page))
1178 gfp_mask |= __GFP_HIGHMEM;
1179
1180#ifdef CONFIG_HUGETLB_PAGE
1181 if (PageHuge(page)) {
1182 struct hstate *h = page_hstate(page);
1183 /*
1184 * We don't want to dequeue from the pool because pool pages will
1185 * mostly be from the CMA region.
1186 */
1187 return alloc_migrate_huge_page(h, gfp_mask, nid, NULL);
1188 }
1189#endif
1190 if (PageTransHuge(page)) {
1191 struct page *thp;
1192 /*
1193 * ignore allocation failure warnings
1194 */
1195 gfp_t thp_gfpmask = GFP_TRANSHUGE | __GFP_NOWARN;
1196
1197 /*
1198 * Remove the movable mask so that we don't allocate from
1199 * CMA area again.
1200 */
1201 thp_gfpmask &= ~__GFP_MOVABLE;
1202 thp = __alloc_pages_node(nid, thp_gfpmask, HPAGE_PMD_ORDER);
1203 if (!thp)
1204 return NULL;
1205 prep_transhuge_page(thp);
1206 return thp;
1207 }
1208
1209 return __alloc_pages_node(nid, gfp_mask, 0);
1210}
1211
1212static long check_and_migrate_cma_pages(unsigned long start, long nr_pages,
1213 unsigned int gup_flags,
1214 struct page **pages,
1215 struct vm_area_struct **vmas)
1216{
1217 long i;
1218 bool drain_allow = true;
1219 bool migrate_allow = true;
1220 LIST_HEAD(cma_page_list);
1221
1222check_again:
1223 for (i = 0; i < nr_pages; i++) {
1224 /*
1225 * If we get a page from the CMA zone, since we are going to
1226 * be pinning these entries, we might as well move them out
1227 * of the CMA zone if possible.
1228 */
1229 if (is_migrate_cma_page(pages[i])) {
1230
1231 struct page *head = compound_head(pages[i]);
1232
1233 if (PageHuge(head)) {
1234 isolate_huge_page(head, &cma_page_list);
1235 } else {
1236 if (!PageLRU(head) && drain_allow) {
1237 lru_add_drain_all();
1238 drain_allow = false;
1239 }
1240
1241 if (!isolate_lru_page(head)) {
1242 list_add_tail(&head->lru, &cma_page_list);
1243 mod_node_page_state(page_pgdat(head),
1244 NR_ISOLATED_ANON +
1245 page_is_file_cache(head),
1246 hpage_nr_pages(head));
1247 }
1248 }
1249 }
1250 }
1251
1252 if (!list_empty(&cma_page_list)) {
1253 /*
1254 * drop the above get_user_pages reference.
1255 */
1256 for (i = 0; i < nr_pages; i++)
1257 put_page(pages[i]);
1258
1259 if (migrate_pages(&cma_page_list, new_non_cma_page,
1260 NULL, 0, MIGRATE_SYNC, MR_CONTIG_RANGE)) {
1261 /*
1262 * some of the pages failed migration. Do get_user_pages
1263 * without migration.
1264 */
1265 migrate_allow = false;
1266
1267 if (!list_empty(&cma_page_list))
1268 putback_movable_pages(&cma_page_list);
1269 }
1270 /*
1271 * We did migrate all the pages, Try to get the page references again
1272 * migrating any new CMA pages which we failed to isolate earlier.
1273 */
1274 nr_pages = get_user_pages(start, nr_pages, gup_flags, pages, vmas);
1275 if ((nr_pages > 0) && migrate_allow) {
1276 drain_allow = true;
1277 goto check_again;
1278 }
1279 }
1280
1281 return nr_pages;
1282}
1283#else
1284static inline long check_and_migrate_cma_pages(unsigned long start, long nr_pages,
1285 unsigned int gup_flags,
1286 struct page **pages,
1287 struct vm_area_struct **vmas)
1288{
1289 return nr_pages;
1290}
1291#endif
1292
2bb6d283
DW
1293/*
1294 * This is the same as get_user_pages() in that it assumes we are
1295 * operating on the current task's mm, but it goes further to validate
1296 * that the vmas associated with the address range are suitable for
1297 * longterm elevated page reference counts. For example, filesystem-dax
1298 * mappings are subject to the lifetime enforced by the filesystem and
1299 * we need guarantees that longterm users like RDMA and V4L2 only
1300 * establish mappings that have a kernel enforced revocation mechanism.
1301 *
1302 * "longterm" == userspace controlled elevated page count lifetime.
1303 * Contrast this to iov_iter_get_pages() usages which are transient.
1304 */
1305long get_user_pages_longterm(unsigned long start, unsigned long nr_pages,
9a4e9f3b
AK
1306 unsigned int gup_flags, struct page **pages,
1307 struct vm_area_struct **vmas_arg)
2bb6d283
DW
1308{
1309 struct vm_area_struct **vmas = vmas_arg;
9a4e9f3b 1310 unsigned long flags;
2bb6d283
DW
1311 long rc, i;
1312
1313 if (!pages)
1314 return -EINVAL;
1315
1316 if (!vmas) {
1317 vmas = kcalloc(nr_pages, sizeof(struct vm_area_struct *),
1318 GFP_KERNEL);
1319 if (!vmas)
1320 return -ENOMEM;
1321 }
1322
9a4e9f3b 1323 flags = memalloc_nocma_save();
2bb6d283 1324 rc = get_user_pages(start, nr_pages, gup_flags, pages, vmas);
9a4e9f3b
AK
1325 memalloc_nocma_restore(flags);
1326 if (rc < 0)
1327 goto out;
2bb6d283 1328
9a4e9f3b
AK
1329 if (check_dax_vmas(vmas, rc)) {
1330 for (i = 0; i < rc; i++)
1331 put_page(pages[i]);
1332 rc = -EOPNOTSUPP;
2bb6d283 1333 goto out;
9a4e9f3b 1334 }
2bb6d283 1335
9a4e9f3b 1336 rc = check_and_migrate_cma_pages(start, rc, gup_flags, pages, vmas);
2bb6d283
DW
1337out:
1338 if (vmas != vmas_arg)
1339 kfree(vmas);
1340 return rc;
1341}
1342EXPORT_SYMBOL(get_user_pages_longterm);
1343#endif /* CONFIG_FS_DAX */
1344
acc3c8d1
KS
1345/**
1346 * populate_vma_page_range() - populate a range of pages in the vma.
1347 * @vma: target vma
1348 * @start: start address
1349 * @end: end address
1350 * @nonblocking:
1351 *
1352 * This takes care of mlocking the pages too if VM_LOCKED is set.
1353 *
1354 * return 0 on success, negative error code on error.
1355 *
1356 * vma->vm_mm->mmap_sem must be held.
1357 *
1358 * If @nonblocking is NULL, it may be held for read or write and will
1359 * be unperturbed.
1360 *
1361 * If @nonblocking is non-NULL, it must held for read only and may be
1362 * released. If it's released, *@nonblocking will be set to 0.
1363 */
1364long populate_vma_page_range(struct vm_area_struct *vma,
1365 unsigned long start, unsigned long end, int *nonblocking)
1366{
1367 struct mm_struct *mm = vma->vm_mm;
1368 unsigned long nr_pages = (end - start) / PAGE_SIZE;
1369 int gup_flags;
1370
1371 VM_BUG_ON(start & ~PAGE_MASK);
1372 VM_BUG_ON(end & ~PAGE_MASK);
1373 VM_BUG_ON_VMA(start < vma->vm_start, vma);
1374 VM_BUG_ON_VMA(end > vma->vm_end, vma);
1375 VM_BUG_ON_MM(!rwsem_is_locked(&mm->mmap_sem), mm);
1376
de60f5f1
EM
1377 gup_flags = FOLL_TOUCH | FOLL_POPULATE | FOLL_MLOCK;
1378 if (vma->vm_flags & VM_LOCKONFAULT)
1379 gup_flags &= ~FOLL_POPULATE;
acc3c8d1
KS
1380 /*
1381 * We want to touch writable mappings with a write fault in order
1382 * to break COW, except for shared mappings because these don't COW
1383 * and we would not want to dirty them for nothing.
1384 */
1385 if ((vma->vm_flags & (VM_WRITE | VM_SHARED)) == VM_WRITE)
1386 gup_flags |= FOLL_WRITE;
1387
1388 /*
1389 * We want mlock to succeed for regions that have any permissions
1390 * other than PROT_NONE.
1391 */
1392 if (vma->vm_flags & (VM_READ | VM_WRITE | VM_EXEC))
1393 gup_flags |= FOLL_FORCE;
1394
1395 /*
1396 * We made sure addr is within a VMA, so the following will
1397 * not result in a stack expansion that recurses back here.
1398 */
1399 return __get_user_pages(current, mm, start, nr_pages, gup_flags,
1400 NULL, NULL, nonblocking);
1401}
1402
1403/*
1404 * __mm_populate - populate and/or mlock pages within a range of address space.
1405 *
1406 * This is used to implement mlock() and the MAP_POPULATE / MAP_LOCKED mmap
1407 * flags. VMAs must be already marked with the desired vm_flags, and
1408 * mmap_sem must not be held.
1409 */
1410int __mm_populate(unsigned long start, unsigned long len, int ignore_errors)
1411{
1412 struct mm_struct *mm = current->mm;
1413 unsigned long end, nstart, nend;
1414 struct vm_area_struct *vma = NULL;
1415 int locked = 0;
1416 long ret = 0;
1417
acc3c8d1
KS
1418 end = start + len;
1419
1420 for (nstart = start; nstart < end; nstart = nend) {
1421 /*
1422 * We want to fault in pages for [nstart; end) address range.
1423 * Find first corresponding VMA.
1424 */
1425 if (!locked) {
1426 locked = 1;
1427 down_read(&mm->mmap_sem);
1428 vma = find_vma(mm, nstart);
1429 } else if (nstart >= vma->vm_end)
1430 vma = vma->vm_next;
1431 if (!vma || vma->vm_start >= end)
1432 break;
1433 /*
1434 * Set [nstart; nend) to intersection of desired address
1435 * range with the first VMA. Also, skip undesirable VMA types.
1436 */
1437 nend = min(end, vma->vm_end);
1438 if (vma->vm_flags & (VM_IO | VM_PFNMAP))
1439 continue;
1440 if (nstart < vma->vm_start)
1441 nstart = vma->vm_start;
1442 /*
1443 * Now fault in a range of pages. populate_vma_page_range()
1444 * double checks the vma flags, so that it won't mlock pages
1445 * if the vma was already munlocked.
1446 */
1447 ret = populate_vma_page_range(vma, nstart, nend, &locked);
1448 if (ret < 0) {
1449 if (ignore_errors) {
1450 ret = 0;
1451 continue; /* continue at next VMA */
1452 }
1453 break;
1454 }
1455 nend = nstart + ret * PAGE_SIZE;
1456 ret = 0;
1457 }
1458 if (locked)
1459 up_read(&mm->mmap_sem);
1460 return ret; /* 0 or negative error code */
1461}
1462
4bbd4c77
KS
1463/**
1464 * get_dump_page() - pin user page in memory while writing it to core dump
1465 * @addr: user address
1466 *
1467 * Returns struct page pointer of user page pinned for dump,
ea1754a0 1468 * to be freed afterwards by put_page().
4bbd4c77
KS
1469 *
1470 * Returns NULL on any kind of failure - a hole must then be inserted into
1471 * the corefile, to preserve alignment with its headers; and also returns
1472 * NULL wherever the ZERO_PAGE, or an anonymous pte_none, has been found -
1473 * allowing a hole to be left in the corefile to save diskspace.
1474 *
1475 * Called without mmap_sem, but after all other threads have been killed.
1476 */
1477#ifdef CONFIG_ELF_CORE
1478struct page *get_dump_page(unsigned long addr)
1479{
1480 struct vm_area_struct *vma;
1481 struct page *page;
1482
1483 if (__get_user_pages(current, current->mm, addr, 1,
1484 FOLL_FORCE | FOLL_DUMP | FOLL_GET, &page, &vma,
1485 NULL) < 1)
1486 return NULL;
1487 flush_cache_page(vma, addr, page_to_pfn(page));
1488 return page;
1489}
1490#endif /* CONFIG_ELF_CORE */
2667f50e
SC
1491
1492/*
e585513b 1493 * Generic Fast GUP
2667f50e
SC
1494 *
1495 * get_user_pages_fast attempts to pin user pages by walking the page
1496 * tables directly and avoids taking locks. Thus the walker needs to be
1497 * protected from page table pages being freed from under it, and should
1498 * block any THP splits.
1499 *
1500 * One way to achieve this is to have the walker disable interrupts, and
1501 * rely on IPIs from the TLB flushing code blocking before the page table
1502 * pages are freed. This is unsuitable for architectures that do not need
1503 * to broadcast an IPI when invalidating TLBs.
1504 *
1505 * Another way to achieve this is to batch up page table containing pages
1506 * belonging to more than one mm_user, then rcu_sched a callback to free those
1507 * pages. Disabling interrupts will allow the fast_gup walker to both block
1508 * the rcu_sched callback, and an IPI that we broadcast for splitting THPs
1509 * (which is a relatively rare event). The code below adopts this strategy.
1510 *
1511 * Before activating this code, please be aware that the following assumptions
1512 * are currently made:
1513 *
e585513b
KS
1514 * *) Either HAVE_RCU_TABLE_FREE is enabled, and tlb_remove_table() is used to
1515 * free pages containing page tables or TLB flushing requires IPI broadcast.
2667f50e 1516 *
2667f50e
SC
1517 * *) ptes can be read atomically by the architecture.
1518 *
1519 * *) access_ok is sufficient to validate userspace address ranges.
1520 *
1521 * The last two assumptions can be relaxed by the addition of helper functions.
1522 *
1523 * This code is based heavily on the PowerPC implementation by Nick Piggin.
1524 */
e585513b 1525#ifdef CONFIG_HAVE_GENERIC_GUP
2667f50e 1526
0005d20b
KS
1527#ifndef gup_get_pte
1528/*
1529 * We assume that the PTE can be read atomically. If this is not the case for
1530 * your architecture, please provide the helper.
1531 */
1532static inline pte_t gup_get_pte(pte_t *ptep)
1533{
1534 return READ_ONCE(*ptep);
1535}
1536#endif
1537
b59f65fa
KS
1538static void undo_dev_pagemap(int *nr, int nr_start, struct page **pages)
1539{
1540 while ((*nr) - nr_start) {
1541 struct page *page = pages[--(*nr)];
1542
1543 ClearPageReferenced(page);
1544 put_page(page);
1545 }
1546}
1547
3010a5ea 1548#ifdef CONFIG_ARCH_HAS_PTE_SPECIAL
2667f50e
SC
1549static int gup_pte_range(pmd_t pmd, unsigned long addr, unsigned long end,
1550 int write, struct page **pages, int *nr)
1551{
b59f65fa
KS
1552 struct dev_pagemap *pgmap = NULL;
1553 int nr_start = *nr, ret = 0;
2667f50e 1554 pte_t *ptep, *ptem;
2667f50e
SC
1555
1556 ptem = ptep = pte_offset_map(&pmd, addr);
1557 do {
0005d20b 1558 pte_t pte = gup_get_pte(ptep);
7aef4172 1559 struct page *head, *page;
2667f50e
SC
1560
1561 /*
1562 * Similar to the PMD case below, NUMA hinting must take slow
8a0516ed 1563 * path using the pte_protnone check.
2667f50e 1564 */
e7884f8e
KS
1565 if (pte_protnone(pte))
1566 goto pte_unmap;
1567
1568 if (!pte_access_permitted(pte, write))
1569 goto pte_unmap;
1570
b59f65fa
KS
1571 if (pte_devmap(pte)) {
1572 pgmap = get_dev_pagemap(pte_pfn(pte), pgmap);
1573 if (unlikely(!pgmap)) {
1574 undo_dev_pagemap(nr, nr_start, pages);
1575 goto pte_unmap;
1576 }
1577 } else if (pte_special(pte))
2667f50e
SC
1578 goto pte_unmap;
1579
1580 VM_BUG_ON(!pfn_valid(pte_pfn(pte)));
1581 page = pte_page(pte);
7aef4172 1582 head = compound_head(page);
2667f50e 1583
7aef4172 1584 if (!page_cache_get_speculative(head))
2667f50e
SC
1585 goto pte_unmap;
1586
1587 if (unlikely(pte_val(pte) != pte_val(*ptep))) {
7aef4172 1588 put_page(head);
2667f50e
SC
1589 goto pte_unmap;
1590 }
1591
7aef4172 1592 VM_BUG_ON_PAGE(compound_head(page) != head, page);
e9348053
KS
1593
1594 SetPageReferenced(page);
2667f50e
SC
1595 pages[*nr] = page;
1596 (*nr)++;
1597
1598 } while (ptep++, addr += PAGE_SIZE, addr != end);
1599
1600 ret = 1;
1601
1602pte_unmap:
832d7aa0
CH
1603 if (pgmap)
1604 put_dev_pagemap(pgmap);
2667f50e
SC
1605 pte_unmap(ptem);
1606 return ret;
1607}
1608#else
1609
1610/*
1611 * If we can't determine whether or not a pte is special, then fail immediately
1612 * for ptes. Note, we can still pin HugeTLB and THP as these are guaranteed not
1613 * to be special.
1614 *
1615 * For a futex to be placed on a THP tail page, get_futex_key requires a
1616 * __get_user_pages_fast implementation that can pin pages. Thus it's still
1617 * useful to have gup_huge_pmd even if we can't operate on ptes.
1618 */
1619static int gup_pte_range(pmd_t pmd, unsigned long addr, unsigned long end,
1620 int write, struct page **pages, int *nr)
1621{
1622 return 0;
1623}
3010a5ea 1624#endif /* CONFIG_ARCH_HAS_PTE_SPECIAL */
2667f50e 1625
09180ca4 1626#if defined(__HAVE_ARCH_PTE_DEVMAP) && defined(CONFIG_TRANSPARENT_HUGEPAGE)
b59f65fa
KS
1627static int __gup_device_huge(unsigned long pfn, unsigned long addr,
1628 unsigned long end, struct page **pages, int *nr)
1629{
1630 int nr_start = *nr;
1631 struct dev_pagemap *pgmap = NULL;
1632
1633 do {
1634 struct page *page = pfn_to_page(pfn);
1635
1636 pgmap = get_dev_pagemap(pfn, pgmap);
1637 if (unlikely(!pgmap)) {
1638 undo_dev_pagemap(nr, nr_start, pages);
1639 return 0;
1640 }
1641 SetPageReferenced(page);
1642 pages[*nr] = page;
1643 get_page(page);
b59f65fa
KS
1644 (*nr)++;
1645 pfn++;
1646 } while (addr += PAGE_SIZE, addr != end);
832d7aa0
CH
1647
1648 if (pgmap)
1649 put_dev_pagemap(pgmap);
b59f65fa
KS
1650 return 1;
1651}
1652
a9b6de77 1653static int __gup_device_huge_pmd(pmd_t orig, pmd_t *pmdp, unsigned long addr,
b59f65fa
KS
1654 unsigned long end, struct page **pages, int *nr)
1655{
1656 unsigned long fault_pfn;
a9b6de77
DW
1657 int nr_start = *nr;
1658
1659 fault_pfn = pmd_pfn(orig) + ((addr & ~PMD_MASK) >> PAGE_SHIFT);
1660 if (!__gup_device_huge(fault_pfn, addr, end, pages, nr))
1661 return 0;
b59f65fa 1662
a9b6de77
DW
1663 if (unlikely(pmd_val(orig) != pmd_val(*pmdp))) {
1664 undo_dev_pagemap(nr, nr_start, pages);
1665 return 0;
1666 }
1667 return 1;
b59f65fa
KS
1668}
1669
a9b6de77 1670static int __gup_device_huge_pud(pud_t orig, pud_t *pudp, unsigned long addr,
b59f65fa
KS
1671 unsigned long end, struct page **pages, int *nr)
1672{
1673 unsigned long fault_pfn;
a9b6de77
DW
1674 int nr_start = *nr;
1675
1676 fault_pfn = pud_pfn(orig) + ((addr & ~PUD_MASK) >> PAGE_SHIFT);
1677 if (!__gup_device_huge(fault_pfn, addr, end, pages, nr))
1678 return 0;
b59f65fa 1679
a9b6de77
DW
1680 if (unlikely(pud_val(orig) != pud_val(*pudp))) {
1681 undo_dev_pagemap(nr, nr_start, pages);
1682 return 0;
1683 }
1684 return 1;
b59f65fa
KS
1685}
1686#else
a9b6de77 1687static int __gup_device_huge_pmd(pmd_t orig, pmd_t *pmdp, unsigned long addr,
b59f65fa
KS
1688 unsigned long end, struct page **pages, int *nr)
1689{
1690 BUILD_BUG();
1691 return 0;
1692}
1693
a9b6de77 1694static int __gup_device_huge_pud(pud_t pud, pud_t *pudp, unsigned long addr,
b59f65fa
KS
1695 unsigned long end, struct page **pages, int *nr)
1696{
1697 BUILD_BUG();
1698 return 0;
1699}
1700#endif
1701
2667f50e
SC
1702static int gup_huge_pmd(pmd_t orig, pmd_t *pmdp, unsigned long addr,
1703 unsigned long end, int write, struct page **pages, int *nr)
1704{
ddc58f27 1705 struct page *head, *page;
2667f50e
SC
1706 int refs;
1707
e7884f8e 1708 if (!pmd_access_permitted(orig, write))
2667f50e
SC
1709 return 0;
1710
b59f65fa 1711 if (pmd_devmap(orig))
a9b6de77 1712 return __gup_device_huge_pmd(orig, pmdp, addr, end, pages, nr);
b59f65fa 1713
2667f50e 1714 refs = 0;
d63206ee 1715 page = pmd_page(orig) + ((addr & ~PMD_MASK) >> PAGE_SHIFT);
2667f50e 1716 do {
2667f50e
SC
1717 pages[*nr] = page;
1718 (*nr)++;
1719 page++;
1720 refs++;
1721 } while (addr += PAGE_SIZE, addr != end);
1722
d63206ee 1723 head = compound_head(pmd_page(orig));
2667f50e
SC
1724 if (!page_cache_add_speculative(head, refs)) {
1725 *nr -= refs;
1726 return 0;
1727 }
1728
1729 if (unlikely(pmd_val(orig) != pmd_val(*pmdp))) {
1730 *nr -= refs;
1731 while (refs--)
1732 put_page(head);
1733 return 0;
1734 }
1735
e9348053 1736 SetPageReferenced(head);
2667f50e
SC
1737 return 1;
1738}
1739
1740static int gup_huge_pud(pud_t orig, pud_t *pudp, unsigned long addr,
1741 unsigned long end, int write, struct page **pages, int *nr)
1742{
ddc58f27 1743 struct page *head, *page;
2667f50e
SC
1744 int refs;
1745
e7884f8e 1746 if (!pud_access_permitted(orig, write))
2667f50e
SC
1747 return 0;
1748
b59f65fa 1749 if (pud_devmap(orig))
a9b6de77 1750 return __gup_device_huge_pud(orig, pudp, addr, end, pages, nr);
b59f65fa 1751
2667f50e 1752 refs = 0;
d63206ee 1753 page = pud_page(orig) + ((addr & ~PUD_MASK) >> PAGE_SHIFT);
2667f50e 1754 do {
2667f50e
SC
1755 pages[*nr] = page;
1756 (*nr)++;
1757 page++;
1758 refs++;
1759 } while (addr += PAGE_SIZE, addr != end);
1760
d63206ee 1761 head = compound_head(pud_page(orig));
2667f50e
SC
1762 if (!page_cache_add_speculative(head, refs)) {
1763 *nr -= refs;
1764 return 0;
1765 }
1766
1767 if (unlikely(pud_val(orig) != pud_val(*pudp))) {
1768 *nr -= refs;
1769 while (refs--)
1770 put_page(head);
1771 return 0;
1772 }
1773
e9348053 1774 SetPageReferenced(head);
2667f50e
SC
1775 return 1;
1776}
1777
f30c59e9
AK
1778static int gup_huge_pgd(pgd_t orig, pgd_t *pgdp, unsigned long addr,
1779 unsigned long end, int write,
1780 struct page **pages, int *nr)
1781{
1782 int refs;
ddc58f27 1783 struct page *head, *page;
f30c59e9 1784
e7884f8e 1785 if (!pgd_access_permitted(orig, write))
f30c59e9
AK
1786 return 0;
1787
b59f65fa 1788 BUILD_BUG_ON(pgd_devmap(orig));
f30c59e9 1789 refs = 0;
d63206ee 1790 page = pgd_page(orig) + ((addr & ~PGDIR_MASK) >> PAGE_SHIFT);
f30c59e9 1791 do {
f30c59e9
AK
1792 pages[*nr] = page;
1793 (*nr)++;
1794 page++;
1795 refs++;
1796 } while (addr += PAGE_SIZE, addr != end);
1797
d63206ee 1798 head = compound_head(pgd_page(orig));
f30c59e9
AK
1799 if (!page_cache_add_speculative(head, refs)) {
1800 *nr -= refs;
1801 return 0;
1802 }
1803
1804 if (unlikely(pgd_val(orig) != pgd_val(*pgdp))) {
1805 *nr -= refs;
1806 while (refs--)
1807 put_page(head);
1808 return 0;
1809 }
1810
e9348053 1811 SetPageReferenced(head);
f30c59e9
AK
1812 return 1;
1813}
1814
2667f50e
SC
1815static int gup_pmd_range(pud_t pud, unsigned long addr, unsigned long end,
1816 int write, struct page **pages, int *nr)
1817{
1818 unsigned long next;
1819 pmd_t *pmdp;
1820
1821 pmdp = pmd_offset(&pud, addr);
1822 do {
38c5ce93 1823 pmd_t pmd = READ_ONCE(*pmdp);
2667f50e
SC
1824
1825 next = pmd_addr_end(addr, end);
84c3fc4e 1826 if (!pmd_present(pmd))
2667f50e
SC
1827 return 0;
1828
414fd080
YZ
1829 if (unlikely(pmd_trans_huge(pmd) || pmd_huge(pmd) ||
1830 pmd_devmap(pmd))) {
2667f50e
SC
1831 /*
1832 * NUMA hinting faults need to be handled in the GUP
1833 * slowpath for accounting purposes and so that they
1834 * can be serialised against THP migration.
1835 */
8a0516ed 1836 if (pmd_protnone(pmd))
2667f50e
SC
1837 return 0;
1838
1839 if (!gup_huge_pmd(pmd, pmdp, addr, next, write,
1840 pages, nr))
1841 return 0;
1842
f30c59e9
AK
1843 } else if (unlikely(is_hugepd(__hugepd(pmd_val(pmd))))) {
1844 /*
1845 * architecture have different format for hugetlbfs
1846 * pmd format and THP pmd format
1847 */
1848 if (!gup_huge_pd(__hugepd(pmd_val(pmd)), addr,
1849 PMD_SHIFT, next, write, pages, nr))
1850 return 0;
2667f50e 1851 } else if (!gup_pte_range(pmd, addr, next, write, pages, nr))
2923117b 1852 return 0;
2667f50e
SC
1853 } while (pmdp++, addr = next, addr != end);
1854
1855 return 1;
1856}
1857
c2febafc 1858static int gup_pud_range(p4d_t p4d, unsigned long addr, unsigned long end,
f30c59e9 1859 int write, struct page **pages, int *nr)
2667f50e
SC
1860{
1861 unsigned long next;
1862 pud_t *pudp;
1863
c2febafc 1864 pudp = pud_offset(&p4d, addr);
2667f50e 1865 do {
e37c6982 1866 pud_t pud = READ_ONCE(*pudp);
2667f50e
SC
1867
1868 next = pud_addr_end(addr, end);
1869 if (pud_none(pud))
1870 return 0;
f30c59e9 1871 if (unlikely(pud_huge(pud))) {
2667f50e 1872 if (!gup_huge_pud(pud, pudp, addr, next, write,
f30c59e9
AK
1873 pages, nr))
1874 return 0;
1875 } else if (unlikely(is_hugepd(__hugepd(pud_val(pud))))) {
1876 if (!gup_huge_pd(__hugepd(pud_val(pud)), addr,
1877 PUD_SHIFT, next, write, pages, nr))
2667f50e
SC
1878 return 0;
1879 } else if (!gup_pmd_range(pud, addr, next, write, pages, nr))
1880 return 0;
1881 } while (pudp++, addr = next, addr != end);
1882
1883 return 1;
1884}
1885
c2febafc
KS
1886static int gup_p4d_range(pgd_t pgd, unsigned long addr, unsigned long end,
1887 int write, struct page **pages, int *nr)
1888{
1889 unsigned long next;
1890 p4d_t *p4dp;
1891
1892 p4dp = p4d_offset(&pgd, addr);
1893 do {
1894 p4d_t p4d = READ_ONCE(*p4dp);
1895
1896 next = p4d_addr_end(addr, end);
1897 if (p4d_none(p4d))
1898 return 0;
1899 BUILD_BUG_ON(p4d_huge(p4d));
1900 if (unlikely(is_hugepd(__hugepd(p4d_val(p4d))))) {
1901 if (!gup_huge_pd(__hugepd(p4d_val(p4d)), addr,
1902 P4D_SHIFT, next, write, pages, nr))
1903 return 0;
ce70df08 1904 } else if (!gup_pud_range(p4d, addr, next, write, pages, nr))
c2febafc
KS
1905 return 0;
1906 } while (p4dp++, addr = next, addr != end);
1907
1908 return 1;
1909}
1910
5b65c467
KS
1911static void gup_pgd_range(unsigned long addr, unsigned long end,
1912 int write, struct page **pages, int *nr)
1913{
1914 unsigned long next;
1915 pgd_t *pgdp;
1916
1917 pgdp = pgd_offset(current->mm, addr);
1918 do {
1919 pgd_t pgd = READ_ONCE(*pgdp);
1920
1921 next = pgd_addr_end(addr, end);
1922 if (pgd_none(pgd))
1923 return;
1924 if (unlikely(pgd_huge(pgd))) {
1925 if (!gup_huge_pgd(pgd, pgdp, addr, next, write,
1926 pages, nr))
1927 return;
1928 } else if (unlikely(is_hugepd(__hugepd(pgd_val(pgd))))) {
1929 if (!gup_huge_pd(__hugepd(pgd_val(pgd)), addr,
1930 PGDIR_SHIFT, next, write, pages, nr))
1931 return;
1932 } else if (!gup_p4d_range(pgd, addr, next, write, pages, nr))
1933 return;
1934 } while (pgdp++, addr = next, addr != end);
1935}
1936
1937#ifndef gup_fast_permitted
1938/*
1939 * Check if it's allowed to use __get_user_pages_fast() for the range, or
1940 * we need to fall back to the slow version:
1941 */
ad8cfb9c 1942bool gup_fast_permitted(unsigned long start, int nr_pages)
5b65c467
KS
1943{
1944 unsigned long len, end;
1945
1946 len = (unsigned long) nr_pages << PAGE_SHIFT;
1947 end = start + len;
1948 return end >= start;
1949}
1950#endif
1951
2667f50e
SC
1952/*
1953 * Like get_user_pages_fast() except it's IRQ-safe in that it won't fall back to
d0811078
MT
1954 * the regular GUP.
1955 * Note a difference with get_user_pages_fast: this always returns the
1956 * number of pages pinned, 0 if no pages were pinned.
2667f50e
SC
1957 */
1958int __get_user_pages_fast(unsigned long start, int nr_pages, int write,
1959 struct page **pages)
1960{
d4faa402 1961 unsigned long len, end;
5b65c467 1962 unsigned long flags;
2667f50e
SC
1963 int nr = 0;
1964
1965 start &= PAGE_MASK;
2667f50e
SC
1966 len = (unsigned long) nr_pages << PAGE_SHIFT;
1967 end = start + len;
1968
96d4f267 1969 if (unlikely(!access_ok((void __user *)start, len)))
2667f50e
SC
1970 return 0;
1971
1972 /*
1973 * Disable interrupts. We use the nested form as we can already have
1974 * interrupts disabled by get_futex_key.
1975 *
1976 * With interrupts disabled, we block page table pages from being
2ebe8228
FW
1977 * freed from under us. See struct mmu_table_batch comments in
1978 * include/asm-generic/tlb.h for more details.
2667f50e
SC
1979 *
1980 * We do not adopt an rcu_read_lock(.) here as we also want to
1981 * block IPIs that come from THPs splitting.
1982 */
1983
ad8cfb9c 1984 if (gup_fast_permitted(start, nr_pages)) {
5b65c467 1985 local_irq_save(flags);
d4faa402 1986 gup_pgd_range(start, end, write, pages, &nr);
5b65c467
KS
1987 local_irq_restore(flags);
1988 }
2667f50e
SC
1989
1990 return nr;
1991}
1992
1993/**
1994 * get_user_pages_fast() - pin user pages in memory
1995 * @start: starting user address
1996 * @nr_pages: number of pages from start to pin
1997 * @write: whether pages will be written to
1998 * @pages: array that receives pointers to the pages pinned.
1999 * Should be at least nr_pages long.
2000 *
2001 * Attempt to pin user pages in memory without taking mm->mmap_sem.
2002 * If not successful, it will fall back to taking the lock and
2003 * calling get_user_pages().
2004 *
2005 * Returns number of pages pinned. This may be fewer than the number
2006 * requested. If nr_pages is 0 or negative, returns 0. If no pages
2007 * were pinned, returns -errno.
2008 */
2009int get_user_pages_fast(unsigned long start, int nr_pages, int write,
2010 struct page **pages)
2011{
5b65c467 2012 unsigned long addr, len, end;
73e10a61 2013 int nr = 0, ret = 0;
2667f50e
SC
2014
2015 start &= PAGE_MASK;
5b65c467
KS
2016 addr = start;
2017 len = (unsigned long) nr_pages << PAGE_SHIFT;
2018 end = start + len;
2019
c61611f7
MT
2020 if (nr_pages <= 0)
2021 return 0;
2022
96d4f267 2023 if (unlikely(!access_ok((void __user *)start, len)))
c61611f7 2024 return -EFAULT;
73e10a61 2025
ad8cfb9c 2026 if (gup_fast_permitted(start, nr_pages)) {
5b65c467
KS
2027 local_irq_disable();
2028 gup_pgd_range(addr, end, write, pages, &nr);
2029 local_irq_enable();
73e10a61
KS
2030 ret = nr;
2031 }
2667f50e
SC
2032
2033 if (nr < nr_pages) {
2034 /* Try to get the remaining pages with get_user_pages */
2035 start += nr << PAGE_SHIFT;
2036 pages += nr;
2037
c164154f
LS
2038 ret = get_user_pages_unlocked(start, nr_pages - nr, pages,
2039 write ? FOLL_WRITE : 0);
2667f50e
SC
2040
2041 /* Have to be a bit careful with return values */
2042 if (nr > 0) {
2043 if (ret < 0)
2044 ret = nr;
2045 else
2046 ret += nr;
2047 }
2048 }
2049
2050 return ret;
2051}
2052
e585513b 2053#endif /* CONFIG_HAVE_GENERIC_GUP */