gup: avoid multiple user access locking/unlocking in fault_in_{read/write}able
[linux-2.6-microblaze.git] / mm / gup.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 #include <linux/kernel.h>
3 #include <linux/errno.h>
4 #include <linux/err.h>
5 #include <linux/spinlock.h>
6
7 #include <linux/mm.h>
8 #include <linux/memremap.h>
9 #include <linux/pagemap.h>
10 #include <linux/rmap.h>
11 #include <linux/swap.h>
12 #include <linux/swapops.h>
13 #include <linux/secretmem.h>
14
15 #include <linux/sched/signal.h>
16 #include <linux/rwsem.h>
17 #include <linux/hugetlb.h>
18 #include <linux/migrate.h>
19 #include <linux/mm_inline.h>
20 #include <linux/sched/mm.h>
21
22 #include <asm/mmu_context.h>
23 #include <asm/tlbflush.h>
24
25 #include "internal.h"
26
27 struct follow_page_context {
28         struct dev_pagemap *pgmap;
29         unsigned int page_mask;
30 };
31
32 static void hpage_pincount_add(struct page *page, int refs)
33 {
34         VM_BUG_ON_PAGE(!hpage_pincount_available(page), page);
35         VM_BUG_ON_PAGE(page != compound_head(page), page);
36
37         atomic_add(refs, compound_pincount_ptr(page));
38 }
39
40 static void hpage_pincount_sub(struct page *page, int refs)
41 {
42         VM_BUG_ON_PAGE(!hpage_pincount_available(page), page);
43         VM_BUG_ON_PAGE(page != compound_head(page), page);
44
45         atomic_sub(refs, compound_pincount_ptr(page));
46 }
47
48 /* Equivalent to calling put_page() @refs times. */
49 static void put_page_refs(struct page *page, int refs)
50 {
51 #ifdef CONFIG_DEBUG_VM
52         if (VM_WARN_ON_ONCE_PAGE(page_ref_count(page) < refs, page))
53                 return;
54 #endif
55
56         /*
57          * Calling put_page() for each ref is unnecessarily slow. Only the last
58          * ref needs a put_page().
59          */
60         if (refs > 1)
61                 page_ref_sub(page, refs - 1);
62         put_page(page);
63 }
64
65 /*
66  * Return the compound head page with ref appropriately incremented,
67  * or NULL if that failed.
68  */
69 static inline struct page *try_get_compound_head(struct page *page, int refs)
70 {
71         struct page *head = compound_head(page);
72
73         if (WARN_ON_ONCE(page_ref_count(head) < 0))
74                 return NULL;
75         if (unlikely(!page_cache_add_speculative(head, refs)))
76                 return NULL;
77
78         /*
79          * At this point we have a stable reference to the head page; but it
80          * could be that between the compound_head() lookup and the refcount
81          * increment, the compound page was split, in which case we'd end up
82          * holding a reference on a page that has nothing to do with the page
83          * we were given anymore.
84          * So now that the head page is stable, recheck that the pages still
85          * belong together.
86          */
87         if (unlikely(compound_head(page) != head)) {
88                 put_page_refs(head, refs);
89                 return NULL;
90         }
91
92         return head;
93 }
94
95 /**
96  * try_grab_compound_head() - attempt to elevate a page's refcount, by a
97  * flags-dependent amount.
98  *
99  * Even though the name includes "compound_head", this function is still
100  * appropriate for callers that have a non-compound @page to get.
101  *
102  * @page:  pointer to page to be grabbed
103  * @refs:  the value to (effectively) add to the page's refcount
104  * @flags: gup flags: these are the FOLL_* flag values.
105  *
106  * "grab" names in this file mean, "look at flags to decide whether to use
107  * FOLL_PIN or FOLL_GET behavior, when incrementing the page's refcount.
108  *
109  * Either FOLL_PIN or FOLL_GET (or neither) must be set, but not both at the
110  * same time. (That's true throughout the get_user_pages*() and
111  * pin_user_pages*() APIs.) Cases:
112  *
113  *    FOLL_GET: page's refcount will be incremented by @refs.
114  *
115  *    FOLL_PIN on compound pages that are > two pages long: page's refcount will
116  *    be incremented by @refs, and page[2].hpage_pinned_refcount will be
117  *    incremented by @refs * GUP_PIN_COUNTING_BIAS.
118  *
119  *    FOLL_PIN on normal pages, or compound pages that are two pages long:
120  *    page's refcount will be incremented by @refs * GUP_PIN_COUNTING_BIAS.
121  *
122  * Return: head page (with refcount appropriately incremented) for success, or
123  * NULL upon failure. If neither FOLL_GET nor FOLL_PIN was set, that's
124  * considered failure, and furthermore, a likely bug in the caller, so a warning
125  * is also emitted.
126  */
127 struct page *try_grab_compound_head(struct page *page,
128                                     int refs, unsigned int flags)
129 {
130         if (flags & FOLL_GET)
131                 return try_get_compound_head(page, refs);
132         else if (flags & FOLL_PIN) {
133                 /*
134                  * Can't do FOLL_LONGTERM + FOLL_PIN gup fast path if not in a
135                  * right zone, so fail and let the caller fall back to the slow
136                  * path.
137                  */
138                 if (unlikely((flags & FOLL_LONGTERM) &&
139                              !is_pinnable_page(page)))
140                         return NULL;
141
142                 /*
143                  * CAUTION: Don't use compound_head() on the page before this
144                  * point, the result won't be stable.
145                  */
146                 page = try_get_compound_head(page, refs);
147                 if (!page)
148                         return NULL;
149
150                 /*
151                  * When pinning a compound page of order > 1 (which is what
152                  * hpage_pincount_available() checks for), use an exact count to
153                  * track it, via hpage_pincount_add/_sub().
154                  *
155                  * However, be sure to *also* increment the normal page refcount
156                  * field at least once, so that the page really is pinned.
157                  * That's why the refcount from the earlier
158                  * try_get_compound_head() is left intact.
159                  */
160                 if (hpage_pincount_available(page))
161                         hpage_pincount_add(page, refs);
162                 else
163                         page_ref_add(page, refs * (GUP_PIN_COUNTING_BIAS - 1));
164
165                 mod_node_page_state(page_pgdat(page), NR_FOLL_PIN_ACQUIRED,
166                                     refs);
167
168                 return page;
169         }
170
171         WARN_ON_ONCE(1);
172         return NULL;
173 }
174
175 static void put_compound_head(struct page *page, int refs, unsigned int flags)
176 {
177         if (flags & FOLL_PIN) {
178                 mod_node_page_state(page_pgdat(page), NR_FOLL_PIN_RELEASED,
179                                     refs);
180
181                 if (hpage_pincount_available(page))
182                         hpage_pincount_sub(page, refs);
183                 else
184                         refs *= GUP_PIN_COUNTING_BIAS;
185         }
186
187         put_page_refs(page, refs);
188 }
189
190 /**
191  * try_grab_page() - elevate a page's refcount by a flag-dependent amount
192  *
193  * This might not do anything at all, depending on the flags argument.
194  *
195  * "grab" names in this file mean, "look at flags to decide whether to use
196  * FOLL_PIN or FOLL_GET behavior, when incrementing the page's refcount.
197  *
198  * @page:    pointer to page to be grabbed
199  * @flags:   gup flags: these are the FOLL_* flag values.
200  *
201  * Either FOLL_PIN or FOLL_GET (or neither) may be set, but not both at the same
202  * time. Cases: please see the try_grab_compound_head() documentation, with
203  * "refs=1".
204  *
205  * Return: true for success, or if no action was required (if neither FOLL_PIN
206  * nor FOLL_GET was set, nothing is done). False for failure: FOLL_GET or
207  * FOLL_PIN was set, but the page could not be grabbed.
208  */
209 bool __must_check try_grab_page(struct page *page, unsigned int flags)
210 {
211         if (!(flags & (FOLL_GET | FOLL_PIN)))
212                 return true;
213
214         return try_grab_compound_head(page, 1, flags);
215 }
216
217 /**
218  * unpin_user_page() - release a dma-pinned page
219  * @page:            pointer to page to be released
220  *
221  * Pages that were pinned via pin_user_pages*() must be released via either
222  * unpin_user_page(), or one of the unpin_user_pages*() routines. This is so
223  * that such pages can be separately tracked and uniquely handled. In
224  * particular, interactions with RDMA and filesystems need special handling.
225  */
226 void unpin_user_page(struct page *page)
227 {
228         put_compound_head(compound_head(page), 1, FOLL_PIN);
229 }
230 EXPORT_SYMBOL(unpin_user_page);
231
232 static inline void compound_range_next(unsigned long i, unsigned long npages,
233                                        struct page **list, struct page **head,
234                                        unsigned int *ntails)
235 {
236         struct page *next, *page;
237         unsigned int nr = 1;
238
239         if (i >= npages)
240                 return;
241
242         next = *list + i;
243         page = compound_head(next);
244         if (PageCompound(page) && compound_order(page) >= 1)
245                 nr = min_t(unsigned int,
246                            page + compound_nr(page) - next, npages - i);
247
248         *head = page;
249         *ntails = nr;
250 }
251
252 #define for_each_compound_range(__i, __list, __npages, __head, __ntails) \
253         for (__i = 0, \
254              compound_range_next(__i, __npages, __list, &(__head), &(__ntails)); \
255              __i < __npages; __i += __ntails, \
256              compound_range_next(__i, __npages, __list, &(__head), &(__ntails)))
257
258 static inline void compound_next(unsigned long i, unsigned long npages,
259                                  struct page **list, struct page **head,
260                                  unsigned int *ntails)
261 {
262         struct page *page;
263         unsigned int nr;
264
265         if (i >= npages)
266                 return;
267
268         page = compound_head(list[i]);
269         for (nr = i + 1; nr < npages; nr++) {
270                 if (compound_head(list[nr]) != page)
271                         break;
272         }
273
274         *head = page;
275         *ntails = nr - i;
276 }
277
278 #define for_each_compound_head(__i, __list, __npages, __head, __ntails) \
279         for (__i = 0, \
280              compound_next(__i, __npages, __list, &(__head), &(__ntails)); \
281              __i < __npages; __i += __ntails, \
282              compound_next(__i, __npages, __list, &(__head), &(__ntails)))
283
284 /**
285  * unpin_user_pages_dirty_lock() - release and optionally dirty gup-pinned pages
286  * @pages:  array of pages to be maybe marked dirty, and definitely released.
287  * @npages: number of pages in the @pages array.
288  * @make_dirty: whether to mark the pages dirty
289  *
290  * "gup-pinned page" refers to a page that has had one of the get_user_pages()
291  * variants called on that page.
292  *
293  * For each page in the @pages array, make that page (or its head page, if a
294  * compound page) dirty, if @make_dirty is true, and if the page was previously
295  * listed as clean. In any case, releases all pages using unpin_user_page(),
296  * possibly via unpin_user_pages(), for the non-dirty case.
297  *
298  * Please see the unpin_user_page() documentation for details.
299  *
300  * set_page_dirty_lock() is used internally. If instead, set_page_dirty() is
301  * required, then the caller should a) verify that this is really correct,
302  * because _lock() is usually required, and b) hand code it:
303  * set_page_dirty_lock(), unpin_user_page().
304  *
305  */
306 void unpin_user_pages_dirty_lock(struct page **pages, unsigned long npages,
307                                  bool make_dirty)
308 {
309         unsigned long index;
310         struct page *head;
311         unsigned int ntails;
312
313         if (!make_dirty) {
314                 unpin_user_pages(pages, npages);
315                 return;
316         }
317
318         for_each_compound_head(index, pages, npages, head, ntails) {
319                 /*
320                  * Checking PageDirty at this point may race with
321                  * clear_page_dirty_for_io(), but that's OK. Two key
322                  * cases:
323                  *
324                  * 1) This code sees the page as already dirty, so it
325                  * skips the call to set_page_dirty(). That could happen
326                  * because clear_page_dirty_for_io() called
327                  * page_mkclean(), followed by set_page_dirty().
328                  * However, now the page is going to get written back,
329                  * which meets the original intention of setting it
330                  * dirty, so all is well: clear_page_dirty_for_io() goes
331                  * on to call TestClearPageDirty(), and write the page
332                  * back.
333                  *
334                  * 2) This code sees the page as clean, so it calls
335                  * set_page_dirty(). The page stays dirty, despite being
336                  * written back, so it gets written back again in the
337                  * next writeback cycle. This is harmless.
338                  */
339                 if (!PageDirty(head))
340                         set_page_dirty_lock(head);
341                 put_compound_head(head, ntails, FOLL_PIN);
342         }
343 }
344 EXPORT_SYMBOL(unpin_user_pages_dirty_lock);
345
346 /**
347  * unpin_user_page_range_dirty_lock() - release and optionally dirty
348  * gup-pinned page range
349  *
350  * @page:  the starting page of a range maybe marked dirty, and definitely released.
351  * @npages: number of consecutive pages to release.
352  * @make_dirty: whether to mark the pages dirty
353  *
354  * "gup-pinned page range" refers to a range of pages that has had one of the
355  * pin_user_pages() variants called on that page.
356  *
357  * For the page ranges defined by [page .. page+npages], make that range (or
358  * its head pages, if a compound page) dirty, if @make_dirty is true, and if the
359  * page range was previously listed as clean.
360  *
361  * set_page_dirty_lock() is used internally. If instead, set_page_dirty() is
362  * required, then the caller should a) verify that this is really correct,
363  * because _lock() is usually required, and b) hand code it:
364  * set_page_dirty_lock(), unpin_user_page().
365  *
366  */
367 void unpin_user_page_range_dirty_lock(struct page *page, unsigned long npages,
368                                       bool make_dirty)
369 {
370         unsigned long index;
371         struct page *head;
372         unsigned int ntails;
373
374         for_each_compound_range(index, &page, npages, head, ntails) {
375                 if (make_dirty && !PageDirty(head))
376                         set_page_dirty_lock(head);
377                 put_compound_head(head, ntails, FOLL_PIN);
378         }
379 }
380 EXPORT_SYMBOL(unpin_user_page_range_dirty_lock);
381
382 /**
383  * unpin_user_pages() - release an array of gup-pinned pages.
384  * @pages:  array of pages to be marked dirty and released.
385  * @npages: number of pages in the @pages array.
386  *
387  * For each page in the @pages array, release the page using unpin_user_page().
388  *
389  * Please see the unpin_user_page() documentation for details.
390  */
391 void unpin_user_pages(struct page **pages, unsigned long npages)
392 {
393         unsigned long index;
394         struct page *head;
395         unsigned int ntails;
396
397         /*
398          * If this WARN_ON() fires, then the system *might* be leaking pages (by
399          * leaving them pinned), but probably not. More likely, gup/pup returned
400          * a hard -ERRNO error to the caller, who erroneously passed it here.
401          */
402         if (WARN_ON(IS_ERR_VALUE(npages)))
403                 return;
404
405         for_each_compound_head(index, pages, npages, head, ntails)
406                 put_compound_head(head, ntails, FOLL_PIN);
407 }
408 EXPORT_SYMBOL(unpin_user_pages);
409
410 /*
411  * Set the MMF_HAS_PINNED if not set yet; after set it'll be there for the mm's
412  * lifecycle.  Avoid setting the bit unless necessary, or it might cause write
413  * cache bouncing on large SMP machines for concurrent pinned gups.
414  */
415 static inline void mm_set_has_pinned_flag(unsigned long *mm_flags)
416 {
417         if (!test_bit(MMF_HAS_PINNED, mm_flags))
418                 set_bit(MMF_HAS_PINNED, mm_flags);
419 }
420
421 #ifdef CONFIG_MMU
422 static struct page *no_page_table(struct vm_area_struct *vma,
423                 unsigned int flags)
424 {
425         /*
426          * When core dumping an enormous anonymous area that nobody
427          * has touched so far, we don't want to allocate unnecessary pages or
428          * page tables.  Return error instead of NULL to skip handle_mm_fault,
429          * then get_dump_page() will return NULL to leave a hole in the dump.
430          * But we can only make this optimization where a hole would surely
431          * be zero-filled if handle_mm_fault() actually did handle it.
432          */
433         if ((flags & FOLL_DUMP) &&
434                         (vma_is_anonymous(vma) || !vma->vm_ops->fault))
435                 return ERR_PTR(-EFAULT);
436         return NULL;
437 }
438
439 static int follow_pfn_pte(struct vm_area_struct *vma, unsigned long address,
440                 pte_t *pte, unsigned int flags)
441 {
442         /* No page to get reference */
443         if (flags & FOLL_GET)
444                 return -EFAULT;
445
446         if (flags & FOLL_TOUCH) {
447                 pte_t entry = *pte;
448
449                 if (flags & FOLL_WRITE)
450                         entry = pte_mkdirty(entry);
451                 entry = pte_mkyoung(entry);
452
453                 if (!pte_same(*pte, entry)) {
454                         set_pte_at(vma->vm_mm, address, pte, entry);
455                         update_mmu_cache(vma, address, pte);
456                 }
457         }
458
459         /* Proper page table entry exists, but no corresponding struct page */
460         return -EEXIST;
461 }
462
463 /*
464  * FOLL_FORCE can write to even unwritable pte's, but only
465  * after we've gone through a COW cycle and they are dirty.
466  */
467 static inline bool can_follow_write_pte(pte_t pte, unsigned int flags)
468 {
469         return pte_write(pte) ||
470                 ((flags & FOLL_FORCE) && (flags & FOLL_COW) && pte_dirty(pte));
471 }
472
473 static struct page *follow_page_pte(struct vm_area_struct *vma,
474                 unsigned long address, pmd_t *pmd, unsigned int flags,
475                 struct dev_pagemap **pgmap)
476 {
477         struct mm_struct *mm = vma->vm_mm;
478         struct page *page;
479         spinlock_t *ptl;
480         pte_t *ptep, pte;
481         int ret;
482
483         /* FOLL_GET and FOLL_PIN are mutually exclusive. */
484         if (WARN_ON_ONCE((flags & (FOLL_PIN | FOLL_GET)) ==
485                          (FOLL_PIN | FOLL_GET)))
486                 return ERR_PTR(-EINVAL);
487 retry:
488         if (unlikely(pmd_bad(*pmd)))
489                 return no_page_table(vma, flags);
490
491         ptep = pte_offset_map_lock(mm, pmd, address, &ptl);
492         pte = *ptep;
493         if (!pte_present(pte)) {
494                 swp_entry_t entry;
495                 /*
496                  * KSM's break_ksm() relies upon recognizing a ksm page
497                  * even while it is being migrated, so for that case we
498                  * need migration_entry_wait().
499                  */
500                 if (likely(!(flags & FOLL_MIGRATION)))
501                         goto no_page;
502                 if (pte_none(pte))
503                         goto no_page;
504                 entry = pte_to_swp_entry(pte);
505                 if (!is_migration_entry(entry))
506                         goto no_page;
507                 pte_unmap_unlock(ptep, ptl);
508                 migration_entry_wait(mm, pmd, address);
509                 goto retry;
510         }
511         if ((flags & FOLL_NUMA) && pte_protnone(pte))
512                 goto no_page;
513         if ((flags & FOLL_WRITE) && !can_follow_write_pte(pte, flags)) {
514                 pte_unmap_unlock(ptep, ptl);
515                 return NULL;
516         }
517
518         page = vm_normal_page(vma, address, pte);
519         if (!page && pte_devmap(pte) && (flags & (FOLL_GET | FOLL_PIN))) {
520                 /*
521                  * Only return device mapping pages in the FOLL_GET or FOLL_PIN
522                  * case since they are only valid while holding the pgmap
523                  * reference.
524                  */
525                 *pgmap = get_dev_pagemap(pte_pfn(pte), *pgmap);
526                 if (*pgmap)
527                         page = pte_page(pte);
528                 else
529                         goto no_page;
530         } else if (unlikely(!page)) {
531                 if (flags & FOLL_DUMP) {
532                         /* Avoid special (like zero) pages in core dumps */
533                         page = ERR_PTR(-EFAULT);
534                         goto out;
535                 }
536
537                 if (is_zero_pfn(pte_pfn(pte))) {
538                         page = pte_page(pte);
539                 } else {
540                         ret = follow_pfn_pte(vma, address, ptep, flags);
541                         page = ERR_PTR(ret);
542                         goto out;
543                 }
544         }
545
546         /* try_grab_page() does nothing unless FOLL_GET or FOLL_PIN is set. */
547         if (unlikely(!try_grab_page(page, flags))) {
548                 page = ERR_PTR(-ENOMEM);
549                 goto out;
550         }
551         /*
552          * We need to make the page accessible if and only if we are going
553          * to access its content (the FOLL_PIN case).  Please see
554          * Documentation/core-api/pin_user_pages.rst for details.
555          */
556         if (flags & FOLL_PIN) {
557                 ret = arch_make_page_accessible(page);
558                 if (ret) {
559                         unpin_user_page(page);
560                         page = ERR_PTR(ret);
561                         goto out;
562                 }
563         }
564         if (flags & FOLL_TOUCH) {
565                 if ((flags & FOLL_WRITE) &&
566                     !pte_dirty(pte) && !PageDirty(page))
567                         set_page_dirty(page);
568                 /*
569                  * pte_mkyoung() would be more correct here, but atomic care
570                  * is needed to avoid losing the dirty bit: it is easier to use
571                  * mark_page_accessed().
572                  */
573                 mark_page_accessed(page);
574         }
575         if ((flags & FOLL_MLOCK) && (vma->vm_flags & VM_LOCKED)) {
576                 /* Do not mlock pte-mapped THP */
577                 if (PageTransCompound(page))
578                         goto out;
579
580                 /*
581                  * The preliminary mapping check is mainly to avoid the
582                  * pointless overhead of lock_page on the ZERO_PAGE
583                  * which might bounce very badly if there is contention.
584                  *
585                  * If the page is already locked, we don't need to
586                  * handle it now - vmscan will handle it later if and
587                  * when it attempts to reclaim the page.
588                  */
589                 if (page->mapping && trylock_page(page)) {
590                         lru_add_drain();  /* push cached pages to LRU */
591                         /*
592                          * Because we lock page here, and migration is
593                          * blocked by the pte's page reference, and we
594                          * know the page is still mapped, we don't even
595                          * need to check for file-cache page truncation.
596                          */
597                         mlock_vma_page(page);
598                         unlock_page(page);
599                 }
600         }
601 out:
602         pte_unmap_unlock(ptep, ptl);
603         return page;
604 no_page:
605         pte_unmap_unlock(ptep, ptl);
606         if (!pte_none(pte))
607                 return NULL;
608         return no_page_table(vma, flags);
609 }
610
611 static struct page *follow_pmd_mask(struct vm_area_struct *vma,
612                                     unsigned long address, pud_t *pudp,
613                                     unsigned int flags,
614                                     struct follow_page_context *ctx)
615 {
616         pmd_t *pmd, pmdval;
617         spinlock_t *ptl;
618         struct page *page;
619         struct mm_struct *mm = vma->vm_mm;
620
621         pmd = pmd_offset(pudp, address);
622         /*
623          * The READ_ONCE() will stabilize the pmdval in a register or
624          * on the stack so that it will stop changing under the code.
625          */
626         pmdval = READ_ONCE(*pmd);
627         if (pmd_none(pmdval))
628                 return no_page_table(vma, flags);
629         if (pmd_huge(pmdval) && is_vm_hugetlb_page(vma)) {
630                 page = follow_huge_pmd(mm, address, pmd, flags);
631                 if (page)
632                         return page;
633                 return no_page_table(vma, flags);
634         }
635         if (is_hugepd(__hugepd(pmd_val(pmdval)))) {
636                 page = follow_huge_pd(vma, address,
637                                       __hugepd(pmd_val(pmdval)), flags,
638                                       PMD_SHIFT);
639                 if (page)
640                         return page;
641                 return no_page_table(vma, flags);
642         }
643 retry:
644         if (!pmd_present(pmdval)) {
645                 if (likely(!(flags & FOLL_MIGRATION)))
646                         return no_page_table(vma, flags);
647                 VM_BUG_ON(thp_migration_supported() &&
648                                   !is_pmd_migration_entry(pmdval));
649                 if (is_pmd_migration_entry(pmdval))
650                         pmd_migration_entry_wait(mm, pmd);
651                 pmdval = READ_ONCE(*pmd);
652                 /*
653                  * MADV_DONTNEED may convert the pmd to null because
654                  * mmap_lock is held in read mode
655                  */
656                 if (pmd_none(pmdval))
657                         return no_page_table(vma, flags);
658                 goto retry;
659         }
660         if (pmd_devmap(pmdval)) {
661                 ptl = pmd_lock(mm, pmd);
662                 page = follow_devmap_pmd(vma, address, pmd, flags, &ctx->pgmap);
663                 spin_unlock(ptl);
664                 if (page)
665                         return page;
666         }
667         if (likely(!pmd_trans_huge(pmdval)))
668                 return follow_page_pte(vma, address, pmd, flags, &ctx->pgmap);
669
670         if ((flags & FOLL_NUMA) && pmd_protnone(pmdval))
671                 return no_page_table(vma, flags);
672
673 retry_locked:
674         ptl = pmd_lock(mm, pmd);
675         if (unlikely(pmd_none(*pmd))) {
676                 spin_unlock(ptl);
677                 return no_page_table(vma, flags);
678         }
679         if (unlikely(!pmd_present(*pmd))) {
680                 spin_unlock(ptl);
681                 if (likely(!(flags & FOLL_MIGRATION)))
682                         return no_page_table(vma, flags);
683                 pmd_migration_entry_wait(mm, pmd);
684                 goto retry_locked;
685         }
686         if (unlikely(!pmd_trans_huge(*pmd))) {
687                 spin_unlock(ptl);
688                 return follow_page_pte(vma, address, pmd, flags, &ctx->pgmap);
689         }
690         if (flags & FOLL_SPLIT_PMD) {
691                 int ret;
692                 page = pmd_page(*pmd);
693                 if (is_huge_zero_page(page)) {
694                         spin_unlock(ptl);
695                         ret = 0;
696                         split_huge_pmd(vma, pmd, address);
697                         if (pmd_trans_unstable(pmd))
698                                 ret = -EBUSY;
699                 } else {
700                         spin_unlock(ptl);
701                         split_huge_pmd(vma, pmd, address);
702                         ret = pte_alloc(mm, pmd) ? -ENOMEM : 0;
703                 }
704
705                 return ret ? ERR_PTR(ret) :
706                         follow_page_pte(vma, address, pmd, flags, &ctx->pgmap);
707         }
708         page = follow_trans_huge_pmd(vma, address, pmd, flags);
709         spin_unlock(ptl);
710         ctx->page_mask = HPAGE_PMD_NR - 1;
711         return page;
712 }
713
714 static struct page *follow_pud_mask(struct vm_area_struct *vma,
715                                     unsigned long address, p4d_t *p4dp,
716                                     unsigned int flags,
717                                     struct follow_page_context *ctx)
718 {
719         pud_t *pud;
720         spinlock_t *ptl;
721         struct page *page;
722         struct mm_struct *mm = vma->vm_mm;
723
724         pud = pud_offset(p4dp, address);
725         if (pud_none(*pud))
726                 return no_page_table(vma, flags);
727         if (pud_huge(*pud) && is_vm_hugetlb_page(vma)) {
728                 page = follow_huge_pud(mm, address, pud, flags);
729                 if (page)
730                         return page;
731                 return no_page_table(vma, flags);
732         }
733         if (is_hugepd(__hugepd(pud_val(*pud)))) {
734                 page = follow_huge_pd(vma, address,
735                                       __hugepd(pud_val(*pud)), flags,
736                                       PUD_SHIFT);
737                 if (page)
738                         return page;
739                 return no_page_table(vma, flags);
740         }
741         if (pud_devmap(*pud)) {
742                 ptl = pud_lock(mm, pud);
743                 page = follow_devmap_pud(vma, address, pud, flags, &ctx->pgmap);
744                 spin_unlock(ptl);
745                 if (page)
746                         return page;
747         }
748         if (unlikely(pud_bad(*pud)))
749                 return no_page_table(vma, flags);
750
751         return follow_pmd_mask(vma, address, pud, flags, ctx);
752 }
753
754 static struct page *follow_p4d_mask(struct vm_area_struct *vma,
755                                     unsigned long address, pgd_t *pgdp,
756                                     unsigned int flags,
757                                     struct follow_page_context *ctx)
758 {
759         p4d_t *p4d;
760         struct page *page;
761
762         p4d = p4d_offset(pgdp, address);
763         if (p4d_none(*p4d))
764                 return no_page_table(vma, flags);
765         BUILD_BUG_ON(p4d_huge(*p4d));
766         if (unlikely(p4d_bad(*p4d)))
767                 return no_page_table(vma, flags);
768
769         if (is_hugepd(__hugepd(p4d_val(*p4d)))) {
770                 page = follow_huge_pd(vma, address,
771                                       __hugepd(p4d_val(*p4d)), flags,
772                                       P4D_SHIFT);
773                 if (page)
774                         return page;
775                 return no_page_table(vma, flags);
776         }
777         return follow_pud_mask(vma, address, p4d, flags, ctx);
778 }
779
780 /**
781  * follow_page_mask - look up a page descriptor from a user-virtual address
782  * @vma: vm_area_struct mapping @address
783  * @address: virtual address to look up
784  * @flags: flags modifying lookup behaviour
785  * @ctx: contains dev_pagemap for %ZONE_DEVICE memory pinning and a
786  *       pointer to output page_mask
787  *
788  * @flags can have FOLL_ flags set, defined in <linux/mm.h>
789  *
790  * When getting pages from ZONE_DEVICE memory, the @ctx->pgmap caches
791  * the device's dev_pagemap metadata to avoid repeating expensive lookups.
792  *
793  * On output, the @ctx->page_mask is set according to the size of the page.
794  *
795  * Return: the mapped (struct page *), %NULL if no mapping exists, or
796  * an error pointer if there is a mapping to something not represented
797  * by a page descriptor (see also vm_normal_page()).
798  */
799 static struct page *follow_page_mask(struct vm_area_struct *vma,
800                               unsigned long address, unsigned int flags,
801                               struct follow_page_context *ctx)
802 {
803         pgd_t *pgd;
804         struct page *page;
805         struct mm_struct *mm = vma->vm_mm;
806
807         ctx->page_mask = 0;
808
809         /* make this handle hugepd */
810         page = follow_huge_addr(mm, address, flags & FOLL_WRITE);
811         if (!IS_ERR(page)) {
812                 WARN_ON_ONCE(flags & (FOLL_GET | FOLL_PIN));
813                 return page;
814         }
815
816         pgd = pgd_offset(mm, address);
817
818         if (pgd_none(*pgd) || unlikely(pgd_bad(*pgd)))
819                 return no_page_table(vma, flags);
820
821         if (pgd_huge(*pgd)) {
822                 page = follow_huge_pgd(mm, address, pgd, flags);
823                 if (page)
824                         return page;
825                 return no_page_table(vma, flags);
826         }
827         if (is_hugepd(__hugepd(pgd_val(*pgd)))) {
828                 page = follow_huge_pd(vma, address,
829                                       __hugepd(pgd_val(*pgd)), flags,
830                                       PGDIR_SHIFT);
831                 if (page)
832                         return page;
833                 return no_page_table(vma, flags);
834         }
835
836         return follow_p4d_mask(vma, address, pgd, flags, ctx);
837 }
838
839 struct page *follow_page(struct vm_area_struct *vma, unsigned long address,
840                          unsigned int foll_flags)
841 {
842         struct follow_page_context ctx = { NULL };
843         struct page *page;
844
845         if (vma_is_secretmem(vma))
846                 return NULL;
847
848         page = follow_page_mask(vma, address, foll_flags, &ctx);
849         if (ctx.pgmap)
850                 put_dev_pagemap(ctx.pgmap);
851         return page;
852 }
853
854 static int get_gate_page(struct mm_struct *mm, unsigned long address,
855                 unsigned int gup_flags, struct vm_area_struct **vma,
856                 struct page **page)
857 {
858         pgd_t *pgd;
859         p4d_t *p4d;
860         pud_t *pud;
861         pmd_t *pmd;
862         pte_t *pte;
863         int ret = -EFAULT;
864
865         /* user gate pages are read-only */
866         if (gup_flags & FOLL_WRITE)
867                 return -EFAULT;
868         if (address > TASK_SIZE)
869                 pgd = pgd_offset_k(address);
870         else
871                 pgd = pgd_offset_gate(mm, address);
872         if (pgd_none(*pgd))
873                 return -EFAULT;
874         p4d = p4d_offset(pgd, address);
875         if (p4d_none(*p4d))
876                 return -EFAULT;
877         pud = pud_offset(p4d, address);
878         if (pud_none(*pud))
879                 return -EFAULT;
880         pmd = pmd_offset(pud, address);
881         if (!pmd_present(*pmd))
882                 return -EFAULT;
883         VM_BUG_ON(pmd_trans_huge(*pmd));
884         pte = pte_offset_map(pmd, address);
885         if (pte_none(*pte))
886                 goto unmap;
887         *vma = get_gate_vma(mm);
888         if (!page)
889                 goto out;
890         *page = vm_normal_page(*vma, address, *pte);
891         if (!*page) {
892                 if ((gup_flags & FOLL_DUMP) || !is_zero_pfn(pte_pfn(*pte)))
893                         goto unmap;
894                 *page = pte_page(*pte);
895         }
896         if (unlikely(!try_grab_page(*page, gup_flags))) {
897                 ret = -ENOMEM;
898                 goto unmap;
899         }
900 out:
901         ret = 0;
902 unmap:
903         pte_unmap(pte);
904         return ret;
905 }
906
907 /*
908  * mmap_lock must be held on entry.  If @locked != NULL and *@flags
909  * does not include FOLL_NOWAIT, the mmap_lock may be released.  If it
910  * is, *@locked will be set to 0 and -EBUSY returned.
911  */
912 static int faultin_page(struct vm_area_struct *vma,
913                 unsigned long address, unsigned int *flags, int *locked)
914 {
915         unsigned int fault_flags = 0;
916         vm_fault_t ret;
917
918         /* mlock all present pages, but do not fault in new pages */
919         if ((*flags & (FOLL_POPULATE | FOLL_MLOCK)) == FOLL_MLOCK)
920                 return -ENOENT;
921         if (*flags & FOLL_NOFAULT)
922                 return -EFAULT;
923         if (*flags & FOLL_WRITE)
924                 fault_flags |= FAULT_FLAG_WRITE;
925         if (*flags & FOLL_REMOTE)
926                 fault_flags |= FAULT_FLAG_REMOTE;
927         if (locked)
928                 fault_flags |= FAULT_FLAG_ALLOW_RETRY | FAULT_FLAG_KILLABLE;
929         if (*flags & FOLL_NOWAIT)
930                 fault_flags |= FAULT_FLAG_ALLOW_RETRY | FAULT_FLAG_RETRY_NOWAIT;
931         if (*flags & FOLL_TRIED) {
932                 /*
933                  * Note: FAULT_FLAG_ALLOW_RETRY and FAULT_FLAG_TRIED
934                  * can co-exist
935                  */
936                 fault_flags |= FAULT_FLAG_TRIED;
937         }
938
939         ret = handle_mm_fault(vma, address, fault_flags, NULL);
940         if (ret & VM_FAULT_ERROR) {
941                 int err = vm_fault_to_errno(ret, *flags);
942
943                 if (err)
944                         return err;
945                 BUG();
946         }
947
948         if (ret & VM_FAULT_RETRY) {
949                 if (locked && !(fault_flags & FAULT_FLAG_RETRY_NOWAIT))
950                         *locked = 0;
951                 return -EBUSY;
952         }
953
954         /*
955          * The VM_FAULT_WRITE bit tells us that do_wp_page has broken COW when
956          * necessary, even if maybe_mkwrite decided not to set pte_write. We
957          * can thus safely do subsequent page lookups as if they were reads.
958          * But only do so when looping for pte_write is futile: in some cases
959          * userspace may also be wanting to write to the gotten user page,
960          * which a read fault here might prevent (a readonly page might get
961          * reCOWed by userspace write).
962          */
963         if ((ret & VM_FAULT_WRITE) && !(vma->vm_flags & VM_WRITE))
964                 *flags |= FOLL_COW;
965         return 0;
966 }
967
968 static int check_vma_flags(struct vm_area_struct *vma, unsigned long gup_flags)
969 {
970         vm_flags_t vm_flags = vma->vm_flags;
971         int write = (gup_flags & FOLL_WRITE);
972         int foreign = (gup_flags & FOLL_REMOTE);
973
974         if (vm_flags & (VM_IO | VM_PFNMAP))
975                 return -EFAULT;
976
977         if (gup_flags & FOLL_ANON && !vma_is_anonymous(vma))
978                 return -EFAULT;
979
980         if ((gup_flags & FOLL_LONGTERM) && vma_is_fsdax(vma))
981                 return -EOPNOTSUPP;
982
983         if (vma_is_secretmem(vma))
984                 return -EFAULT;
985
986         if (write) {
987                 if (!(vm_flags & VM_WRITE)) {
988                         if (!(gup_flags & FOLL_FORCE))
989                                 return -EFAULT;
990                         /*
991                          * We used to let the write,force case do COW in a
992                          * VM_MAYWRITE VM_SHARED !VM_WRITE vma, so ptrace could
993                          * set a breakpoint in a read-only mapping of an
994                          * executable, without corrupting the file (yet only
995                          * when that file had been opened for writing!).
996                          * Anon pages in shared mappings are surprising: now
997                          * just reject it.
998                          */
999                         if (!is_cow_mapping(vm_flags))
1000                                 return -EFAULT;
1001                 }
1002         } else if (!(vm_flags & VM_READ)) {
1003                 if (!(gup_flags & FOLL_FORCE))
1004                         return -EFAULT;
1005                 /*
1006                  * Is there actually any vma we can reach here which does not
1007                  * have VM_MAYREAD set?
1008                  */
1009                 if (!(vm_flags & VM_MAYREAD))
1010                         return -EFAULT;
1011         }
1012         /*
1013          * gups are always data accesses, not instruction
1014          * fetches, so execute=false here
1015          */
1016         if (!arch_vma_access_permitted(vma, write, false, foreign))
1017                 return -EFAULT;
1018         return 0;
1019 }
1020
1021 /**
1022  * __get_user_pages() - pin user pages in memory
1023  * @mm:         mm_struct of target mm
1024  * @start:      starting user address
1025  * @nr_pages:   number of pages from start to pin
1026  * @gup_flags:  flags modifying pin behaviour
1027  * @pages:      array that receives pointers to the pages pinned.
1028  *              Should be at least nr_pages long. Or NULL, if caller
1029  *              only intends to ensure the pages are faulted in.
1030  * @vmas:       array of pointers to vmas corresponding to each page.
1031  *              Or NULL if the caller does not require them.
1032  * @locked:     whether we're still with the mmap_lock held
1033  *
1034  * Returns either number of pages pinned (which may be less than the
1035  * number requested), or an error. Details about the return value:
1036  *
1037  * -- If nr_pages is 0, returns 0.
1038  * -- If nr_pages is >0, but no pages were pinned, returns -errno.
1039  * -- If nr_pages is >0, and some pages were pinned, returns the number of
1040  *    pages pinned. Again, this may be less than nr_pages.
1041  * -- 0 return value is possible when the fault would need to be retried.
1042  *
1043  * The caller is responsible for releasing returned @pages, via put_page().
1044  *
1045  * @vmas are valid only as long as mmap_lock is held.
1046  *
1047  * Must be called with mmap_lock held.  It may be released.  See below.
1048  *
1049  * __get_user_pages walks a process's page tables and takes a reference to
1050  * each struct page that each user address corresponds to at a given
1051  * instant. That is, it takes the page that would be accessed if a user
1052  * thread accesses the given user virtual address at that instant.
1053  *
1054  * This does not guarantee that the page exists in the user mappings when
1055  * __get_user_pages returns, and there may even be a completely different
1056  * page there in some cases (eg. if mmapped pagecache has been invalidated
1057  * and subsequently re faulted). However it does guarantee that the page
1058  * won't be freed completely. And mostly callers simply care that the page
1059  * contains data that was valid *at some point in time*. Typically, an IO
1060  * or similar operation cannot guarantee anything stronger anyway because
1061  * locks can't be held over the syscall boundary.
1062  *
1063  * If @gup_flags & FOLL_WRITE == 0, the page must not be written to. If
1064  * the page is written to, set_page_dirty (or set_page_dirty_lock, as
1065  * appropriate) must be called after the page is finished with, and
1066  * before put_page is called.
1067  *
1068  * If @locked != NULL, *@locked will be set to 0 when mmap_lock is
1069  * released by an up_read().  That can happen if @gup_flags does not
1070  * have FOLL_NOWAIT.
1071  *
1072  * A caller using such a combination of @locked and @gup_flags
1073  * must therefore hold the mmap_lock for reading only, and recognize
1074  * when it's been released.  Otherwise, it must be held for either
1075  * reading or writing and will not be released.
1076  *
1077  * In most cases, get_user_pages or get_user_pages_fast should be used
1078  * instead of __get_user_pages. __get_user_pages should be used only if
1079  * you need some special @gup_flags.
1080  */
1081 static long __get_user_pages(struct mm_struct *mm,
1082                 unsigned long start, unsigned long nr_pages,
1083                 unsigned int gup_flags, struct page **pages,
1084                 struct vm_area_struct **vmas, int *locked)
1085 {
1086         long ret = 0, i = 0;
1087         struct vm_area_struct *vma = NULL;
1088         struct follow_page_context ctx = { NULL };
1089
1090         if (!nr_pages)
1091                 return 0;
1092
1093         start = untagged_addr(start);
1094
1095         VM_BUG_ON(!!pages != !!(gup_flags & (FOLL_GET | FOLL_PIN)));
1096
1097         /*
1098          * If FOLL_FORCE is set then do not force a full fault as the hinting
1099          * fault information is unrelated to the reference behaviour of a task
1100          * using the address space
1101          */
1102         if (!(gup_flags & FOLL_FORCE))
1103                 gup_flags |= FOLL_NUMA;
1104
1105         do {
1106                 struct page *page;
1107                 unsigned int foll_flags = gup_flags;
1108                 unsigned int page_increm;
1109
1110                 /* first iteration or cross vma bound */
1111                 if (!vma || start >= vma->vm_end) {
1112                         vma = find_extend_vma(mm, start);
1113                         if (!vma && in_gate_area(mm, start)) {
1114                                 ret = get_gate_page(mm, start & PAGE_MASK,
1115                                                 gup_flags, &vma,
1116                                                 pages ? &pages[i] : NULL);
1117                                 if (ret)
1118                                         goto out;
1119                                 ctx.page_mask = 0;
1120                                 goto next_page;
1121                         }
1122
1123                         if (!vma) {
1124                                 ret = -EFAULT;
1125                                 goto out;
1126                         }
1127                         ret = check_vma_flags(vma, gup_flags);
1128                         if (ret)
1129                                 goto out;
1130
1131                         if (is_vm_hugetlb_page(vma)) {
1132                                 i = follow_hugetlb_page(mm, vma, pages, vmas,
1133                                                 &start, &nr_pages, i,
1134                                                 gup_flags, locked);
1135                                 if (locked && *locked == 0) {
1136                                         /*
1137                                          * We've got a VM_FAULT_RETRY
1138                                          * and we've lost mmap_lock.
1139                                          * We must stop here.
1140                                          */
1141                                         BUG_ON(gup_flags & FOLL_NOWAIT);
1142                                         goto out;
1143                                 }
1144                                 continue;
1145                         }
1146                 }
1147 retry:
1148                 /*
1149                  * If we have a pending SIGKILL, don't keep faulting pages and
1150                  * potentially allocating memory.
1151                  */
1152                 if (fatal_signal_pending(current)) {
1153                         ret = -EINTR;
1154                         goto out;
1155                 }
1156                 cond_resched();
1157
1158                 page = follow_page_mask(vma, start, foll_flags, &ctx);
1159                 if (!page) {
1160                         ret = faultin_page(vma, start, &foll_flags, locked);
1161                         switch (ret) {
1162                         case 0:
1163                                 goto retry;
1164                         case -EBUSY:
1165                                 ret = 0;
1166                                 fallthrough;
1167                         case -EFAULT:
1168                         case -ENOMEM:
1169                         case -EHWPOISON:
1170                                 goto out;
1171                         case -ENOENT:
1172                                 goto next_page;
1173                         }
1174                         BUG();
1175                 } else if (PTR_ERR(page) == -EEXIST) {
1176                         /*
1177                          * Proper page table entry exists, but no corresponding
1178                          * struct page.
1179                          */
1180                         goto next_page;
1181                 } else if (IS_ERR(page)) {
1182                         ret = PTR_ERR(page);
1183                         goto out;
1184                 }
1185                 if (pages) {
1186                         pages[i] = page;
1187                         flush_anon_page(vma, page, start);
1188                         flush_dcache_page(page);
1189                         ctx.page_mask = 0;
1190                 }
1191 next_page:
1192                 if (vmas) {
1193                         vmas[i] = vma;
1194                         ctx.page_mask = 0;
1195                 }
1196                 page_increm = 1 + (~(start >> PAGE_SHIFT) & ctx.page_mask);
1197                 if (page_increm > nr_pages)
1198                         page_increm = nr_pages;
1199                 i += page_increm;
1200                 start += page_increm * PAGE_SIZE;
1201                 nr_pages -= page_increm;
1202         } while (nr_pages);
1203 out:
1204         if (ctx.pgmap)
1205                 put_dev_pagemap(ctx.pgmap);
1206         return i ? i : ret;
1207 }
1208
1209 static bool vma_permits_fault(struct vm_area_struct *vma,
1210                               unsigned int fault_flags)
1211 {
1212         bool write   = !!(fault_flags & FAULT_FLAG_WRITE);
1213         bool foreign = !!(fault_flags & FAULT_FLAG_REMOTE);
1214         vm_flags_t vm_flags = write ? VM_WRITE : VM_READ;
1215
1216         if (!(vm_flags & vma->vm_flags))
1217                 return false;
1218
1219         /*
1220          * The architecture might have a hardware protection
1221          * mechanism other than read/write that can deny access.
1222          *
1223          * gup always represents data access, not instruction
1224          * fetches, so execute=false here:
1225          */
1226         if (!arch_vma_access_permitted(vma, write, false, foreign))
1227                 return false;
1228
1229         return true;
1230 }
1231
1232 /**
1233  * fixup_user_fault() - manually resolve a user page fault
1234  * @mm:         mm_struct of target mm
1235  * @address:    user address
1236  * @fault_flags:flags to pass down to handle_mm_fault()
1237  * @unlocked:   did we unlock the mmap_lock while retrying, maybe NULL if caller
1238  *              does not allow retry. If NULL, the caller must guarantee
1239  *              that fault_flags does not contain FAULT_FLAG_ALLOW_RETRY.
1240  *
1241  * This is meant to be called in the specific scenario where for locking reasons
1242  * we try to access user memory in atomic context (within a pagefault_disable()
1243  * section), this returns -EFAULT, and we want to resolve the user fault before
1244  * trying again.
1245  *
1246  * Typically this is meant to be used by the futex code.
1247  *
1248  * The main difference with get_user_pages() is that this function will
1249  * unconditionally call handle_mm_fault() which will in turn perform all the
1250  * necessary SW fixup of the dirty and young bits in the PTE, while
1251  * get_user_pages() only guarantees to update these in the struct page.
1252  *
1253  * This is important for some architectures where those bits also gate the
1254  * access permission to the page because they are maintained in software.  On
1255  * such architectures, gup() will not be enough to make a subsequent access
1256  * succeed.
1257  *
1258  * This function will not return with an unlocked mmap_lock. So it has not the
1259  * same semantics wrt the @mm->mmap_lock as does filemap_fault().
1260  */
1261 int fixup_user_fault(struct mm_struct *mm,
1262                      unsigned long address, unsigned int fault_flags,
1263                      bool *unlocked)
1264 {
1265         struct vm_area_struct *vma;
1266         vm_fault_t ret;
1267
1268         address = untagged_addr(address);
1269
1270         if (unlocked)
1271                 fault_flags |= FAULT_FLAG_ALLOW_RETRY | FAULT_FLAG_KILLABLE;
1272
1273 retry:
1274         vma = find_extend_vma(mm, address);
1275         if (!vma || address < vma->vm_start)
1276                 return -EFAULT;
1277
1278         if (!vma_permits_fault(vma, fault_flags))
1279                 return -EFAULT;
1280
1281         if ((fault_flags & FAULT_FLAG_KILLABLE) &&
1282             fatal_signal_pending(current))
1283                 return -EINTR;
1284
1285         ret = handle_mm_fault(vma, address, fault_flags, NULL);
1286         if (ret & VM_FAULT_ERROR) {
1287                 int err = vm_fault_to_errno(ret, 0);
1288
1289                 if (err)
1290                         return err;
1291                 BUG();
1292         }
1293
1294         if (ret & VM_FAULT_RETRY) {
1295                 mmap_read_lock(mm);
1296                 *unlocked = true;
1297                 fault_flags |= FAULT_FLAG_TRIED;
1298                 goto retry;
1299         }
1300
1301         return 0;
1302 }
1303 EXPORT_SYMBOL_GPL(fixup_user_fault);
1304
1305 /*
1306  * Please note that this function, unlike __get_user_pages will not
1307  * return 0 for nr_pages > 0 without FOLL_NOWAIT
1308  */
1309 static __always_inline long __get_user_pages_locked(struct mm_struct *mm,
1310                                                 unsigned long start,
1311                                                 unsigned long nr_pages,
1312                                                 struct page **pages,
1313                                                 struct vm_area_struct **vmas,
1314                                                 int *locked,
1315                                                 unsigned int flags)
1316 {
1317         long ret, pages_done;
1318         bool lock_dropped;
1319
1320         if (locked) {
1321                 /* if VM_FAULT_RETRY can be returned, vmas become invalid */
1322                 BUG_ON(vmas);
1323                 /* check caller initialized locked */
1324                 BUG_ON(*locked != 1);
1325         }
1326
1327         if (flags & FOLL_PIN)
1328                 mm_set_has_pinned_flag(&mm->flags);
1329
1330         /*
1331          * FOLL_PIN and FOLL_GET are mutually exclusive. Traditional behavior
1332          * is to set FOLL_GET if the caller wants pages[] filled in (but has
1333          * carelessly failed to specify FOLL_GET), so keep doing that, but only
1334          * for FOLL_GET, not for the newer FOLL_PIN.
1335          *
1336          * FOLL_PIN always expects pages to be non-null, but no need to assert
1337          * that here, as any failures will be obvious enough.
1338          */
1339         if (pages && !(flags & FOLL_PIN))
1340                 flags |= FOLL_GET;
1341
1342         pages_done = 0;
1343         lock_dropped = false;
1344         for (;;) {
1345                 ret = __get_user_pages(mm, start, nr_pages, flags, pages,
1346                                        vmas, locked);
1347                 if (!locked)
1348                         /* VM_FAULT_RETRY couldn't trigger, bypass */
1349                         return ret;
1350
1351                 /* VM_FAULT_RETRY cannot return errors */
1352                 if (!*locked) {
1353                         BUG_ON(ret < 0);
1354                         BUG_ON(ret >= nr_pages);
1355                 }
1356
1357                 if (ret > 0) {
1358                         nr_pages -= ret;
1359                         pages_done += ret;
1360                         if (!nr_pages)
1361                                 break;
1362                 }
1363                 if (*locked) {
1364                         /*
1365                          * VM_FAULT_RETRY didn't trigger or it was a
1366                          * FOLL_NOWAIT.
1367                          */
1368                         if (!pages_done)
1369                                 pages_done = ret;
1370                         break;
1371                 }
1372                 /*
1373                  * VM_FAULT_RETRY triggered, so seek to the faulting offset.
1374                  * For the prefault case (!pages) we only update counts.
1375                  */
1376                 if (likely(pages))
1377                         pages += ret;
1378                 start += ret << PAGE_SHIFT;
1379                 lock_dropped = true;
1380
1381 retry:
1382                 /*
1383                  * Repeat on the address that fired VM_FAULT_RETRY
1384                  * with both FAULT_FLAG_ALLOW_RETRY and
1385                  * FAULT_FLAG_TRIED.  Note that GUP can be interrupted
1386                  * by fatal signals, so we need to check it before we
1387                  * start trying again otherwise it can loop forever.
1388                  */
1389
1390                 if (fatal_signal_pending(current)) {
1391                         if (!pages_done)
1392                                 pages_done = -EINTR;
1393                         break;
1394                 }
1395
1396                 ret = mmap_read_lock_killable(mm);
1397                 if (ret) {
1398                         BUG_ON(ret > 0);
1399                         if (!pages_done)
1400                                 pages_done = ret;
1401                         break;
1402                 }
1403
1404                 *locked = 1;
1405                 ret = __get_user_pages(mm, start, 1, flags | FOLL_TRIED,
1406                                        pages, NULL, locked);
1407                 if (!*locked) {
1408                         /* Continue to retry until we succeeded */
1409                         BUG_ON(ret != 0);
1410                         goto retry;
1411                 }
1412                 if (ret != 1) {
1413                         BUG_ON(ret > 1);
1414                         if (!pages_done)
1415                                 pages_done = ret;
1416                         break;
1417                 }
1418                 nr_pages--;
1419                 pages_done++;
1420                 if (!nr_pages)
1421                         break;
1422                 if (likely(pages))
1423                         pages++;
1424                 start += PAGE_SIZE;
1425         }
1426         if (lock_dropped && *locked) {
1427                 /*
1428                  * We must let the caller know we temporarily dropped the lock
1429                  * and so the critical section protected by it was lost.
1430                  */
1431                 mmap_read_unlock(mm);
1432                 *locked = 0;
1433         }
1434         return pages_done;
1435 }
1436
1437 /**
1438  * populate_vma_page_range() -  populate a range of pages in the vma.
1439  * @vma:   target vma
1440  * @start: start address
1441  * @end:   end address
1442  * @locked: whether the mmap_lock is still held
1443  *
1444  * This takes care of mlocking the pages too if VM_LOCKED is set.
1445  *
1446  * Return either number of pages pinned in the vma, or a negative error
1447  * code on error.
1448  *
1449  * vma->vm_mm->mmap_lock must be held.
1450  *
1451  * If @locked is NULL, it may be held for read or write and will
1452  * be unperturbed.
1453  *
1454  * If @locked is non-NULL, it must held for read only and may be
1455  * released.  If it's released, *@locked will be set to 0.
1456  */
1457 long populate_vma_page_range(struct vm_area_struct *vma,
1458                 unsigned long start, unsigned long end, int *locked)
1459 {
1460         struct mm_struct *mm = vma->vm_mm;
1461         unsigned long nr_pages = (end - start) / PAGE_SIZE;
1462         int gup_flags;
1463
1464         VM_BUG_ON(!PAGE_ALIGNED(start));
1465         VM_BUG_ON(!PAGE_ALIGNED(end));
1466         VM_BUG_ON_VMA(start < vma->vm_start, vma);
1467         VM_BUG_ON_VMA(end   > vma->vm_end, vma);
1468         mmap_assert_locked(mm);
1469
1470         gup_flags = FOLL_TOUCH | FOLL_POPULATE | FOLL_MLOCK;
1471         if (vma->vm_flags & VM_LOCKONFAULT)
1472                 gup_flags &= ~FOLL_POPULATE;
1473         /*
1474          * We want to touch writable mappings with a write fault in order
1475          * to break COW, except for shared mappings because these don't COW
1476          * and we would not want to dirty them for nothing.
1477          */
1478         if ((vma->vm_flags & (VM_WRITE | VM_SHARED)) == VM_WRITE)
1479                 gup_flags |= FOLL_WRITE;
1480
1481         /*
1482          * We want mlock to succeed for regions that have any permissions
1483          * other than PROT_NONE.
1484          */
1485         if (vma_is_accessible(vma))
1486                 gup_flags |= FOLL_FORCE;
1487
1488         /*
1489          * We made sure addr is within a VMA, so the following will
1490          * not result in a stack expansion that recurses back here.
1491          */
1492         return __get_user_pages(mm, start, nr_pages, gup_flags,
1493                                 NULL, NULL, locked);
1494 }
1495
1496 /*
1497  * faultin_vma_page_range() - populate (prefault) page tables inside the
1498  *                            given VMA range readable/writable
1499  *
1500  * This takes care of mlocking the pages, too, if VM_LOCKED is set.
1501  *
1502  * @vma: target vma
1503  * @start: start address
1504  * @end: end address
1505  * @write: whether to prefault readable or writable
1506  * @locked: whether the mmap_lock is still held
1507  *
1508  * Returns either number of processed pages in the vma, or a negative error
1509  * code on error (see __get_user_pages()).
1510  *
1511  * vma->vm_mm->mmap_lock must be held. The range must be page-aligned and
1512  * covered by the VMA.
1513  *
1514  * If @locked is NULL, it may be held for read or write and will be unperturbed.
1515  *
1516  * If @locked is non-NULL, it must held for read only and may be released.  If
1517  * it's released, *@locked will be set to 0.
1518  */
1519 long faultin_vma_page_range(struct vm_area_struct *vma, unsigned long start,
1520                             unsigned long end, bool write, int *locked)
1521 {
1522         struct mm_struct *mm = vma->vm_mm;
1523         unsigned long nr_pages = (end - start) / PAGE_SIZE;
1524         int gup_flags;
1525
1526         VM_BUG_ON(!PAGE_ALIGNED(start));
1527         VM_BUG_ON(!PAGE_ALIGNED(end));
1528         VM_BUG_ON_VMA(start < vma->vm_start, vma);
1529         VM_BUG_ON_VMA(end > vma->vm_end, vma);
1530         mmap_assert_locked(mm);
1531
1532         /*
1533          * FOLL_TOUCH: Mark page accessed and thereby young; will also mark
1534          *             the page dirty with FOLL_WRITE -- which doesn't make a
1535          *             difference with !FOLL_FORCE, because the page is writable
1536          *             in the page table.
1537          * FOLL_HWPOISON: Return -EHWPOISON instead of -EFAULT when we hit
1538          *                a poisoned page.
1539          * FOLL_POPULATE: Always populate memory with VM_LOCKONFAULT.
1540          * !FOLL_FORCE: Require proper access permissions.
1541          */
1542         gup_flags = FOLL_TOUCH | FOLL_POPULATE | FOLL_MLOCK | FOLL_HWPOISON;
1543         if (write)
1544                 gup_flags |= FOLL_WRITE;
1545
1546         /*
1547          * We want to report -EINVAL instead of -EFAULT for any permission
1548          * problems or incompatible mappings.
1549          */
1550         if (check_vma_flags(vma, gup_flags))
1551                 return -EINVAL;
1552
1553         return __get_user_pages(mm, start, nr_pages, gup_flags,
1554                                 NULL, NULL, locked);
1555 }
1556
1557 /*
1558  * __mm_populate - populate and/or mlock pages within a range of address space.
1559  *
1560  * This is used to implement mlock() and the MAP_POPULATE / MAP_LOCKED mmap
1561  * flags. VMAs must be already marked with the desired vm_flags, and
1562  * mmap_lock must not be held.
1563  */
1564 int __mm_populate(unsigned long start, unsigned long len, int ignore_errors)
1565 {
1566         struct mm_struct *mm = current->mm;
1567         unsigned long end, nstart, nend;
1568         struct vm_area_struct *vma = NULL;
1569         int locked = 0;
1570         long ret = 0;
1571
1572         end = start + len;
1573
1574         for (nstart = start; nstart < end; nstart = nend) {
1575                 /*
1576                  * We want to fault in pages for [nstart; end) address range.
1577                  * Find first corresponding VMA.
1578                  */
1579                 if (!locked) {
1580                         locked = 1;
1581                         mmap_read_lock(mm);
1582                         vma = find_vma(mm, nstart);
1583                 } else if (nstart >= vma->vm_end)
1584                         vma = vma->vm_next;
1585                 if (!vma || vma->vm_start >= end)
1586                         break;
1587                 /*
1588                  * Set [nstart; nend) to intersection of desired address
1589                  * range with the first VMA. Also, skip undesirable VMA types.
1590                  */
1591                 nend = min(end, vma->vm_end);
1592                 if (vma->vm_flags & (VM_IO | VM_PFNMAP))
1593                         continue;
1594                 if (nstart < vma->vm_start)
1595                         nstart = vma->vm_start;
1596                 /*
1597                  * Now fault in a range of pages. populate_vma_page_range()
1598                  * double checks the vma flags, so that it won't mlock pages
1599                  * if the vma was already munlocked.
1600                  */
1601                 ret = populate_vma_page_range(vma, nstart, nend, &locked);
1602                 if (ret < 0) {
1603                         if (ignore_errors) {
1604                                 ret = 0;
1605                                 continue;       /* continue at next VMA */
1606                         }
1607                         break;
1608                 }
1609                 nend = nstart + ret * PAGE_SIZE;
1610                 ret = 0;
1611         }
1612         if (locked)
1613                 mmap_read_unlock(mm);
1614         return ret;     /* 0 or negative error code */
1615 }
1616 #else /* CONFIG_MMU */
1617 static long __get_user_pages_locked(struct mm_struct *mm, unsigned long start,
1618                 unsigned long nr_pages, struct page **pages,
1619                 struct vm_area_struct **vmas, int *locked,
1620                 unsigned int foll_flags)
1621 {
1622         struct vm_area_struct *vma;
1623         unsigned long vm_flags;
1624         long i;
1625
1626         /* calculate required read or write permissions.
1627          * If FOLL_FORCE is set, we only require the "MAY" flags.
1628          */
1629         vm_flags  = (foll_flags & FOLL_WRITE) ?
1630                         (VM_WRITE | VM_MAYWRITE) : (VM_READ | VM_MAYREAD);
1631         vm_flags &= (foll_flags & FOLL_FORCE) ?
1632                         (VM_MAYREAD | VM_MAYWRITE) : (VM_READ | VM_WRITE);
1633
1634         for (i = 0; i < nr_pages; i++) {
1635                 vma = find_vma(mm, start);
1636                 if (!vma)
1637                         goto finish_or_fault;
1638
1639                 /* protect what we can, including chardevs */
1640                 if ((vma->vm_flags & (VM_IO | VM_PFNMAP)) ||
1641                     !(vm_flags & vma->vm_flags))
1642                         goto finish_or_fault;
1643
1644                 if (pages) {
1645                         pages[i] = virt_to_page(start);
1646                         if (pages[i])
1647                                 get_page(pages[i]);
1648                 }
1649                 if (vmas)
1650                         vmas[i] = vma;
1651                 start = (start + PAGE_SIZE) & PAGE_MASK;
1652         }
1653
1654         return i;
1655
1656 finish_or_fault:
1657         return i ? : -EFAULT;
1658 }
1659 #endif /* !CONFIG_MMU */
1660
1661 /**
1662  * fault_in_writeable - fault in userspace address range for writing
1663  * @uaddr: start of address range
1664  * @size: size of address range
1665  *
1666  * Returns the number of bytes not faulted in (like copy_to_user() and
1667  * copy_from_user()).
1668  */
1669 size_t fault_in_writeable(char __user *uaddr, size_t size)
1670 {
1671         char __user *start = uaddr, *end;
1672
1673         if (unlikely(size == 0))
1674                 return 0;
1675         if (!user_write_access_begin(uaddr, size))
1676                 return size;
1677         if (!PAGE_ALIGNED(uaddr)) {
1678                 unsafe_put_user(0, uaddr, out);
1679                 uaddr = (char __user *)PAGE_ALIGN((unsigned long)uaddr);
1680         }
1681         end = (char __user *)PAGE_ALIGN((unsigned long)start + size);
1682         if (unlikely(end < start))
1683                 end = NULL;
1684         while (uaddr != end) {
1685                 unsafe_put_user(0, uaddr, out);
1686                 uaddr += PAGE_SIZE;
1687         }
1688
1689 out:
1690         user_write_access_end();
1691         if (size > uaddr - start)
1692                 return size - (uaddr - start);
1693         return 0;
1694 }
1695 EXPORT_SYMBOL(fault_in_writeable);
1696
1697 /*
1698  * fault_in_safe_writeable - fault in an address range for writing
1699  * @uaddr: start of address range
1700  * @size: length of address range
1701  *
1702  * Faults in an address range using get_user_pages, i.e., without triggering
1703  * hardware page faults.  This is primarily useful when we already know that
1704  * some or all of the pages in the address range aren't in memory.
1705  *
1706  * Other than fault_in_writeable(), this function is non-destructive.
1707  *
1708  * Note that we don't pin or otherwise hold the pages referenced that we fault
1709  * in.  There's no guarantee that they'll stay in memory for any duration of
1710  * time.
1711  *
1712  * Returns the number of bytes not faulted in, like copy_to_user() and
1713  * copy_from_user().
1714  */
1715 size_t fault_in_safe_writeable(const char __user *uaddr, size_t size)
1716 {
1717         unsigned long start = (unsigned long)untagged_addr(uaddr);
1718         unsigned long end, nstart, nend;
1719         struct mm_struct *mm = current->mm;
1720         struct vm_area_struct *vma = NULL;
1721         int locked = 0;
1722
1723         nstart = start & PAGE_MASK;
1724         end = PAGE_ALIGN(start + size);
1725         if (end < nstart)
1726                 end = 0;
1727         for (; nstart != end; nstart = nend) {
1728                 unsigned long nr_pages;
1729                 long ret;
1730
1731                 if (!locked) {
1732                         locked = 1;
1733                         mmap_read_lock(mm);
1734                         vma = find_vma(mm, nstart);
1735                 } else if (nstart >= vma->vm_end)
1736                         vma = vma->vm_next;
1737                 if (!vma || vma->vm_start >= end)
1738                         break;
1739                 nend = end ? min(end, vma->vm_end) : vma->vm_end;
1740                 if (vma->vm_flags & (VM_IO | VM_PFNMAP))
1741                         continue;
1742                 if (nstart < vma->vm_start)
1743                         nstart = vma->vm_start;
1744                 nr_pages = (nend - nstart) / PAGE_SIZE;
1745                 ret = __get_user_pages_locked(mm, nstart, nr_pages,
1746                                               NULL, NULL, &locked,
1747                                               FOLL_TOUCH | FOLL_WRITE);
1748                 if (ret <= 0)
1749                         break;
1750                 nend = nstart + ret * PAGE_SIZE;
1751         }
1752         if (locked)
1753                 mmap_read_unlock(mm);
1754         if (nstart == end)
1755                 return 0;
1756         return size - min_t(size_t, nstart - start, size);
1757 }
1758 EXPORT_SYMBOL(fault_in_safe_writeable);
1759
1760 /**
1761  * fault_in_readable - fault in userspace address range for reading
1762  * @uaddr: start of user address range
1763  * @size: size of user address range
1764  *
1765  * Returns the number of bytes not faulted in (like copy_to_user() and
1766  * copy_from_user()).
1767  */
1768 size_t fault_in_readable(const char __user *uaddr, size_t size)
1769 {
1770         const char __user *start = uaddr, *end;
1771         volatile char c;
1772
1773         if (unlikely(size == 0))
1774                 return 0;
1775         if (!user_read_access_begin(uaddr, size))
1776                 return size;
1777         if (!PAGE_ALIGNED(uaddr)) {
1778                 unsafe_get_user(c, uaddr, out);
1779                 uaddr = (const char __user *)PAGE_ALIGN((unsigned long)uaddr);
1780         }
1781         end = (const char __user *)PAGE_ALIGN((unsigned long)start + size);
1782         if (unlikely(end < start))
1783                 end = NULL;
1784         while (uaddr != end) {
1785                 unsafe_get_user(c, uaddr, out);
1786                 uaddr += PAGE_SIZE;
1787         }
1788
1789 out:
1790         user_read_access_end();
1791         (void)c;
1792         if (size > uaddr - start)
1793                 return size - (uaddr - start);
1794         return 0;
1795 }
1796 EXPORT_SYMBOL(fault_in_readable);
1797
1798 /**
1799  * get_dump_page() - pin user page in memory while writing it to core dump
1800  * @addr: user address
1801  *
1802  * Returns struct page pointer of user page pinned for dump,
1803  * to be freed afterwards by put_page().
1804  *
1805  * Returns NULL on any kind of failure - a hole must then be inserted into
1806  * the corefile, to preserve alignment with its headers; and also returns
1807  * NULL wherever the ZERO_PAGE, or an anonymous pte_none, has been found -
1808  * allowing a hole to be left in the corefile to save disk space.
1809  *
1810  * Called without mmap_lock (takes and releases the mmap_lock by itself).
1811  */
1812 #ifdef CONFIG_ELF_CORE
1813 struct page *get_dump_page(unsigned long addr)
1814 {
1815         struct mm_struct *mm = current->mm;
1816         struct page *page;
1817         int locked = 1;
1818         int ret;
1819
1820         if (mmap_read_lock_killable(mm))
1821                 return NULL;
1822         ret = __get_user_pages_locked(mm, addr, 1, &page, NULL, &locked,
1823                                       FOLL_FORCE | FOLL_DUMP | FOLL_GET);
1824         if (locked)
1825                 mmap_read_unlock(mm);
1826         return (ret == 1) ? page : NULL;
1827 }
1828 #endif /* CONFIG_ELF_CORE */
1829
1830 #ifdef CONFIG_MIGRATION
1831 /*
1832  * Check whether all pages are pinnable, if so return number of pages.  If some
1833  * pages are not pinnable, migrate them, and unpin all pages. Return zero if
1834  * pages were migrated, or if some pages were not successfully isolated.
1835  * Return negative error if migration fails.
1836  */
1837 static long check_and_migrate_movable_pages(unsigned long nr_pages,
1838                                             struct page **pages,
1839                                             unsigned int gup_flags)
1840 {
1841         unsigned long i;
1842         unsigned long isolation_error_count = 0;
1843         bool drain_allow = true;
1844         LIST_HEAD(movable_page_list);
1845         long ret = 0;
1846         struct page *prev_head = NULL;
1847         struct page *head;
1848         struct migration_target_control mtc = {
1849                 .nid = NUMA_NO_NODE,
1850                 .gfp_mask = GFP_USER | __GFP_NOWARN,
1851         };
1852
1853         for (i = 0; i < nr_pages; i++) {
1854                 head = compound_head(pages[i]);
1855                 if (head == prev_head)
1856                         continue;
1857                 prev_head = head;
1858                 /*
1859                  * If we get a movable page, since we are going to be pinning
1860                  * these entries, try to move them out if possible.
1861                  */
1862                 if (!is_pinnable_page(head)) {
1863                         if (PageHuge(head)) {
1864                                 if (!isolate_huge_page(head, &movable_page_list))
1865                                         isolation_error_count++;
1866                         } else {
1867                                 if (!PageLRU(head) && drain_allow) {
1868                                         lru_add_drain_all();
1869                                         drain_allow = false;
1870                                 }
1871
1872                                 if (isolate_lru_page(head)) {
1873                                         isolation_error_count++;
1874                                         continue;
1875                                 }
1876                                 list_add_tail(&head->lru, &movable_page_list);
1877                                 mod_node_page_state(page_pgdat(head),
1878                                                     NR_ISOLATED_ANON +
1879                                                     page_is_file_lru(head),
1880                                                     thp_nr_pages(head));
1881                         }
1882                 }
1883         }
1884
1885         /*
1886          * If list is empty, and no isolation errors, means that all pages are
1887          * in the correct zone.
1888          */
1889         if (list_empty(&movable_page_list) && !isolation_error_count)
1890                 return nr_pages;
1891
1892         if (gup_flags & FOLL_PIN) {
1893                 unpin_user_pages(pages, nr_pages);
1894         } else {
1895                 for (i = 0; i < nr_pages; i++)
1896                         put_page(pages[i]);
1897         }
1898         if (!list_empty(&movable_page_list)) {
1899                 ret = migrate_pages(&movable_page_list, alloc_migration_target,
1900                                     NULL, (unsigned long)&mtc, MIGRATE_SYNC,
1901                                     MR_LONGTERM_PIN, NULL);
1902                 if (ret && !list_empty(&movable_page_list))
1903                         putback_movable_pages(&movable_page_list);
1904         }
1905
1906         return ret > 0 ? -ENOMEM : ret;
1907 }
1908 #else
1909 static long check_and_migrate_movable_pages(unsigned long nr_pages,
1910                                             struct page **pages,
1911                                             unsigned int gup_flags)
1912 {
1913         return nr_pages;
1914 }
1915 #endif /* CONFIG_MIGRATION */
1916
1917 /*
1918  * __gup_longterm_locked() is a wrapper for __get_user_pages_locked which
1919  * allows us to process the FOLL_LONGTERM flag.
1920  */
1921 static long __gup_longterm_locked(struct mm_struct *mm,
1922                                   unsigned long start,
1923                                   unsigned long nr_pages,
1924                                   struct page **pages,
1925                                   struct vm_area_struct **vmas,
1926                                   unsigned int gup_flags)
1927 {
1928         unsigned int flags;
1929         long rc;
1930
1931         if (!(gup_flags & FOLL_LONGTERM))
1932                 return __get_user_pages_locked(mm, start, nr_pages, pages, vmas,
1933                                                NULL, gup_flags);
1934         flags = memalloc_pin_save();
1935         do {
1936                 rc = __get_user_pages_locked(mm, start, nr_pages, pages, vmas,
1937                                              NULL, gup_flags);
1938                 if (rc <= 0)
1939                         break;
1940                 rc = check_and_migrate_movable_pages(rc, pages, gup_flags);
1941         } while (!rc);
1942         memalloc_pin_restore(flags);
1943
1944         return rc;
1945 }
1946
1947 static bool is_valid_gup_flags(unsigned int gup_flags)
1948 {
1949         /*
1950          * FOLL_PIN must only be set internally by the pin_user_pages*() APIs,
1951          * never directly by the caller, so enforce that with an assertion:
1952          */
1953         if (WARN_ON_ONCE(gup_flags & FOLL_PIN))
1954                 return false;
1955         /*
1956          * FOLL_PIN is a prerequisite to FOLL_LONGTERM. Another way of saying
1957          * that is, FOLL_LONGTERM is a specific case, more restrictive case of
1958          * FOLL_PIN.
1959          */
1960         if (WARN_ON_ONCE(gup_flags & FOLL_LONGTERM))
1961                 return false;
1962
1963         return true;
1964 }
1965
1966 #ifdef CONFIG_MMU
1967 static long __get_user_pages_remote(struct mm_struct *mm,
1968                                     unsigned long start, unsigned long nr_pages,
1969                                     unsigned int gup_flags, struct page **pages,
1970                                     struct vm_area_struct **vmas, int *locked)
1971 {
1972         /*
1973          * Parts of FOLL_LONGTERM behavior are incompatible with
1974          * FAULT_FLAG_ALLOW_RETRY because of the FS DAX check requirement on
1975          * vmas. However, this only comes up if locked is set, and there are
1976          * callers that do request FOLL_LONGTERM, but do not set locked. So,
1977          * allow what we can.
1978          */
1979         if (gup_flags & FOLL_LONGTERM) {
1980                 if (WARN_ON_ONCE(locked))
1981                         return -EINVAL;
1982                 /*
1983                  * This will check the vmas (even if our vmas arg is NULL)
1984                  * and return -ENOTSUPP if DAX isn't allowed in this case:
1985                  */
1986                 return __gup_longterm_locked(mm, start, nr_pages, pages,
1987                                              vmas, gup_flags | FOLL_TOUCH |
1988                                              FOLL_REMOTE);
1989         }
1990
1991         return __get_user_pages_locked(mm, start, nr_pages, pages, vmas,
1992                                        locked,
1993                                        gup_flags | FOLL_TOUCH | FOLL_REMOTE);
1994 }
1995
1996 /**
1997  * get_user_pages_remote() - pin user pages in memory
1998  * @mm:         mm_struct of target mm
1999  * @start:      starting user address
2000  * @nr_pages:   number of pages from start to pin
2001  * @gup_flags:  flags modifying lookup behaviour
2002  * @pages:      array that receives pointers to the pages pinned.
2003  *              Should be at least nr_pages long. Or NULL, if caller
2004  *              only intends to ensure the pages are faulted in.
2005  * @vmas:       array of pointers to vmas corresponding to each page.
2006  *              Or NULL if the caller does not require them.
2007  * @locked:     pointer to lock flag indicating whether lock is held and
2008  *              subsequently whether VM_FAULT_RETRY functionality can be
2009  *              utilised. Lock must initially be held.
2010  *
2011  * Returns either number of pages pinned (which may be less than the
2012  * number requested), or an error. Details about the return value:
2013  *
2014  * -- If nr_pages is 0, returns 0.
2015  * -- If nr_pages is >0, but no pages were pinned, returns -errno.
2016  * -- If nr_pages is >0, and some pages were pinned, returns the number of
2017  *    pages pinned. Again, this may be less than nr_pages.
2018  *
2019  * The caller is responsible for releasing returned @pages, via put_page().
2020  *
2021  * @vmas are valid only as long as mmap_lock is held.
2022  *
2023  * Must be called with mmap_lock held for read or write.
2024  *
2025  * get_user_pages_remote walks a process's page tables and takes a reference
2026  * to each struct page that each user address corresponds to at a given
2027  * instant. That is, it takes the page that would be accessed if a user
2028  * thread accesses the given user virtual address at that instant.
2029  *
2030  * This does not guarantee that the page exists in the user mappings when
2031  * get_user_pages_remote returns, and there may even be a completely different
2032  * page there in some cases (eg. if mmapped pagecache has been invalidated
2033  * and subsequently re faulted). However it does guarantee that the page
2034  * won't be freed completely. And mostly callers simply care that the page
2035  * contains data that was valid *at some point in time*. Typically, an IO
2036  * or similar operation cannot guarantee anything stronger anyway because
2037  * locks can't be held over the syscall boundary.
2038  *
2039  * If gup_flags & FOLL_WRITE == 0, the page must not be written to. If the page
2040  * is written to, set_page_dirty (or set_page_dirty_lock, as appropriate) must
2041  * be called after the page is finished with, and before put_page is called.
2042  *
2043  * get_user_pages_remote is typically used for fewer-copy IO operations,
2044  * to get a handle on the memory by some means other than accesses
2045  * via the user virtual addresses. The pages may be submitted for
2046  * DMA to devices or accessed via their kernel linear mapping (via the
2047  * kmap APIs). Care should be taken to use the correct cache flushing APIs.
2048  *
2049  * See also get_user_pages_fast, for performance critical applications.
2050  *
2051  * get_user_pages_remote should be phased out in favor of
2052  * get_user_pages_locked|unlocked or get_user_pages_fast. Nothing
2053  * should use get_user_pages_remote because it cannot pass
2054  * FAULT_FLAG_ALLOW_RETRY to handle_mm_fault.
2055  */
2056 long get_user_pages_remote(struct mm_struct *mm,
2057                 unsigned long start, unsigned long nr_pages,
2058                 unsigned int gup_flags, struct page **pages,
2059                 struct vm_area_struct **vmas, int *locked)
2060 {
2061         if (!is_valid_gup_flags(gup_flags))
2062                 return -EINVAL;
2063
2064         return __get_user_pages_remote(mm, start, nr_pages, gup_flags,
2065                                        pages, vmas, locked);
2066 }
2067 EXPORT_SYMBOL(get_user_pages_remote);
2068
2069 #else /* CONFIG_MMU */
2070 long get_user_pages_remote(struct mm_struct *mm,
2071                            unsigned long start, unsigned long nr_pages,
2072                            unsigned int gup_flags, struct page **pages,
2073                            struct vm_area_struct **vmas, int *locked)
2074 {
2075         return 0;
2076 }
2077
2078 static long __get_user_pages_remote(struct mm_struct *mm,
2079                                     unsigned long start, unsigned long nr_pages,
2080                                     unsigned int gup_flags, struct page **pages,
2081                                     struct vm_area_struct **vmas, int *locked)
2082 {
2083         return 0;
2084 }
2085 #endif /* !CONFIG_MMU */
2086
2087 /**
2088  * get_user_pages() - pin user pages in memory
2089  * @start:      starting user address
2090  * @nr_pages:   number of pages from start to pin
2091  * @gup_flags:  flags modifying lookup behaviour
2092  * @pages:      array that receives pointers to the pages pinned.
2093  *              Should be at least nr_pages long. Or NULL, if caller
2094  *              only intends to ensure the pages are faulted in.
2095  * @vmas:       array of pointers to vmas corresponding to each page.
2096  *              Or NULL if the caller does not require them.
2097  *
2098  * This is the same as get_user_pages_remote(), just with a less-flexible
2099  * calling convention where we assume that the mm being operated on belongs to
2100  * the current task, and doesn't allow passing of a locked parameter.  We also
2101  * obviously don't pass FOLL_REMOTE in here.
2102  */
2103 long get_user_pages(unsigned long start, unsigned long nr_pages,
2104                 unsigned int gup_flags, struct page **pages,
2105                 struct vm_area_struct **vmas)
2106 {
2107         if (!is_valid_gup_flags(gup_flags))
2108                 return -EINVAL;
2109
2110         return __gup_longterm_locked(current->mm, start, nr_pages,
2111                                      pages, vmas, gup_flags | FOLL_TOUCH);
2112 }
2113 EXPORT_SYMBOL(get_user_pages);
2114
2115 /**
2116  * get_user_pages_locked() - variant of get_user_pages()
2117  *
2118  * @start:      starting user address
2119  * @nr_pages:   number of pages from start to pin
2120  * @gup_flags:  flags modifying lookup behaviour
2121  * @pages:      array that receives pointers to the pages pinned.
2122  *              Should be at least nr_pages long. Or NULL, if caller
2123  *              only intends to ensure the pages are faulted in.
2124  * @locked:     pointer to lock flag indicating whether lock is held and
2125  *              subsequently whether VM_FAULT_RETRY functionality can be
2126  *              utilised. Lock must initially be held.
2127  *
2128  * It is suitable to replace the form:
2129  *
2130  *      mmap_read_lock(mm);
2131  *      do_something()
2132  *      get_user_pages(mm, ..., pages, NULL);
2133  *      mmap_read_unlock(mm);
2134  *
2135  *  to:
2136  *
2137  *      int locked = 1;
2138  *      mmap_read_lock(mm);
2139  *      do_something()
2140  *      get_user_pages_locked(mm, ..., pages, &locked);
2141  *      if (locked)
2142  *          mmap_read_unlock(mm);
2143  *
2144  * We can leverage the VM_FAULT_RETRY functionality in the page fault
2145  * paths better by using either get_user_pages_locked() or
2146  * get_user_pages_unlocked().
2147  *
2148  */
2149 long get_user_pages_locked(unsigned long start, unsigned long nr_pages,
2150                            unsigned int gup_flags, struct page **pages,
2151                            int *locked)
2152 {
2153         /*
2154          * FIXME: Current FOLL_LONGTERM behavior is incompatible with
2155          * FAULT_FLAG_ALLOW_RETRY because of the FS DAX check requirement on
2156          * vmas.  As there are no users of this flag in this call we simply
2157          * disallow this option for now.
2158          */
2159         if (WARN_ON_ONCE(gup_flags & FOLL_LONGTERM))
2160                 return -EINVAL;
2161         /*
2162          * FOLL_PIN must only be set internally by the pin_user_pages*() APIs,
2163          * never directly by the caller, so enforce that:
2164          */
2165         if (WARN_ON_ONCE(gup_flags & FOLL_PIN))
2166                 return -EINVAL;
2167
2168         return __get_user_pages_locked(current->mm, start, nr_pages,
2169                                        pages, NULL, locked,
2170                                        gup_flags | FOLL_TOUCH);
2171 }
2172 EXPORT_SYMBOL(get_user_pages_locked);
2173
2174 /*
2175  * get_user_pages_unlocked() is suitable to replace the form:
2176  *
2177  *      mmap_read_lock(mm);
2178  *      get_user_pages(mm, ..., pages, NULL);
2179  *      mmap_read_unlock(mm);
2180  *
2181  *  with:
2182  *
2183  *      get_user_pages_unlocked(mm, ..., pages);
2184  *
2185  * It is functionally equivalent to get_user_pages_fast so
2186  * get_user_pages_fast should be used instead if specific gup_flags
2187  * (e.g. FOLL_FORCE) are not required.
2188  */
2189 long get_user_pages_unlocked(unsigned long start, unsigned long nr_pages,
2190                              struct page **pages, unsigned int gup_flags)
2191 {
2192         struct mm_struct *mm = current->mm;
2193         int locked = 1;
2194         long ret;
2195
2196         /*
2197          * FIXME: Current FOLL_LONGTERM behavior is incompatible with
2198          * FAULT_FLAG_ALLOW_RETRY because of the FS DAX check requirement on
2199          * vmas.  As there are no users of this flag in this call we simply
2200          * disallow this option for now.
2201          */
2202         if (WARN_ON_ONCE(gup_flags & FOLL_LONGTERM))
2203                 return -EINVAL;
2204
2205         mmap_read_lock(mm);
2206         ret = __get_user_pages_locked(mm, start, nr_pages, pages, NULL,
2207                                       &locked, gup_flags | FOLL_TOUCH);
2208         if (locked)
2209                 mmap_read_unlock(mm);
2210         return ret;
2211 }
2212 EXPORT_SYMBOL(get_user_pages_unlocked);
2213
2214 /*
2215  * Fast GUP
2216  *
2217  * get_user_pages_fast attempts to pin user pages by walking the page
2218  * tables directly and avoids taking locks. Thus the walker needs to be
2219  * protected from page table pages being freed from under it, and should
2220  * block any THP splits.
2221  *
2222  * One way to achieve this is to have the walker disable interrupts, and
2223  * rely on IPIs from the TLB flushing code blocking before the page table
2224  * pages are freed. This is unsuitable for architectures that do not need
2225  * to broadcast an IPI when invalidating TLBs.
2226  *
2227  * Another way to achieve this is to batch up page table containing pages
2228  * belonging to more than one mm_user, then rcu_sched a callback to free those
2229  * pages. Disabling interrupts will allow the fast_gup walker to both block
2230  * the rcu_sched callback, and an IPI that we broadcast for splitting THPs
2231  * (which is a relatively rare event). The code below adopts this strategy.
2232  *
2233  * Before activating this code, please be aware that the following assumptions
2234  * are currently made:
2235  *
2236  *  *) Either MMU_GATHER_RCU_TABLE_FREE is enabled, and tlb_remove_table() is used to
2237  *  free pages containing page tables or TLB flushing requires IPI broadcast.
2238  *
2239  *  *) ptes can be read atomically by the architecture.
2240  *
2241  *  *) access_ok is sufficient to validate userspace address ranges.
2242  *
2243  * The last two assumptions can be relaxed by the addition of helper functions.
2244  *
2245  * This code is based heavily on the PowerPC implementation by Nick Piggin.
2246  */
2247 #ifdef CONFIG_HAVE_FAST_GUP
2248
2249 static void __maybe_unused undo_dev_pagemap(int *nr, int nr_start,
2250                                             unsigned int flags,
2251                                             struct page **pages)
2252 {
2253         while ((*nr) - nr_start) {
2254                 struct page *page = pages[--(*nr)];
2255
2256                 ClearPageReferenced(page);
2257                 if (flags & FOLL_PIN)
2258                         unpin_user_page(page);
2259                 else
2260                         put_page(page);
2261         }
2262 }
2263
2264 #ifdef CONFIG_ARCH_HAS_PTE_SPECIAL
2265 static int gup_pte_range(pmd_t pmd, unsigned long addr, unsigned long end,
2266                          unsigned int flags, struct page **pages, int *nr)
2267 {
2268         struct dev_pagemap *pgmap = NULL;
2269         int nr_start = *nr, ret = 0;
2270         pte_t *ptep, *ptem;
2271
2272         ptem = ptep = pte_offset_map(&pmd, addr);
2273         do {
2274                 pte_t pte = ptep_get_lockless(ptep);
2275                 struct page *head, *page;
2276
2277                 /*
2278                  * Similar to the PMD case below, NUMA hinting must take slow
2279                  * path using the pte_protnone check.
2280                  */
2281                 if (pte_protnone(pte))
2282                         goto pte_unmap;
2283
2284                 if (!pte_access_permitted(pte, flags & FOLL_WRITE))
2285                         goto pte_unmap;
2286
2287                 if (pte_devmap(pte)) {
2288                         if (unlikely(flags & FOLL_LONGTERM))
2289                                 goto pte_unmap;
2290
2291                         pgmap = get_dev_pagemap(pte_pfn(pte), pgmap);
2292                         if (unlikely(!pgmap)) {
2293                                 undo_dev_pagemap(nr, nr_start, flags, pages);
2294                                 goto pte_unmap;
2295                         }
2296                 } else if (pte_special(pte))
2297                         goto pte_unmap;
2298
2299                 VM_BUG_ON(!pfn_valid(pte_pfn(pte)));
2300                 page = pte_page(pte);
2301
2302                 head = try_grab_compound_head(page, 1, flags);
2303                 if (!head)
2304                         goto pte_unmap;
2305
2306                 if (unlikely(page_is_secretmem(page))) {
2307                         put_compound_head(head, 1, flags);
2308                         goto pte_unmap;
2309                 }
2310
2311                 if (unlikely(pte_val(pte) != pte_val(*ptep))) {
2312                         put_compound_head(head, 1, flags);
2313                         goto pte_unmap;
2314                 }
2315
2316                 VM_BUG_ON_PAGE(compound_head(page) != head, page);
2317
2318                 /*
2319                  * We need to make the page accessible if and only if we are
2320                  * going to access its content (the FOLL_PIN case).  Please
2321                  * see Documentation/core-api/pin_user_pages.rst for
2322                  * details.
2323                  */
2324                 if (flags & FOLL_PIN) {
2325                         ret = arch_make_page_accessible(page);
2326                         if (ret) {
2327                                 unpin_user_page(page);
2328                                 goto pte_unmap;
2329                         }
2330                 }
2331                 SetPageReferenced(page);
2332                 pages[*nr] = page;
2333                 (*nr)++;
2334
2335         } while (ptep++, addr += PAGE_SIZE, addr != end);
2336
2337         ret = 1;
2338
2339 pte_unmap:
2340         if (pgmap)
2341                 put_dev_pagemap(pgmap);
2342         pte_unmap(ptem);
2343         return ret;
2344 }
2345 #else
2346
2347 /*
2348  * If we can't determine whether or not a pte is special, then fail immediately
2349  * for ptes. Note, we can still pin HugeTLB and THP as these are guaranteed not
2350  * to be special.
2351  *
2352  * For a futex to be placed on a THP tail page, get_futex_key requires a
2353  * get_user_pages_fast_only implementation that can pin pages. Thus it's still
2354  * useful to have gup_huge_pmd even if we can't operate on ptes.
2355  */
2356 static int gup_pte_range(pmd_t pmd, unsigned long addr, unsigned long end,
2357                          unsigned int flags, struct page **pages, int *nr)
2358 {
2359         return 0;
2360 }
2361 #endif /* CONFIG_ARCH_HAS_PTE_SPECIAL */
2362
2363 #if defined(CONFIG_ARCH_HAS_PTE_DEVMAP) && defined(CONFIG_TRANSPARENT_HUGEPAGE)
2364 static int __gup_device_huge(unsigned long pfn, unsigned long addr,
2365                              unsigned long end, unsigned int flags,
2366                              struct page **pages, int *nr)
2367 {
2368         int nr_start = *nr;
2369         struct dev_pagemap *pgmap = NULL;
2370
2371         do {
2372                 struct page *page = pfn_to_page(pfn);
2373
2374                 pgmap = get_dev_pagemap(pfn, pgmap);
2375                 if (unlikely(!pgmap)) {
2376                         undo_dev_pagemap(nr, nr_start, flags, pages);
2377                         break;
2378                 }
2379                 SetPageReferenced(page);
2380                 pages[*nr] = page;
2381                 if (unlikely(!try_grab_page(page, flags))) {
2382                         undo_dev_pagemap(nr, nr_start, flags, pages);
2383                         break;
2384                 }
2385                 (*nr)++;
2386                 pfn++;
2387         } while (addr += PAGE_SIZE, addr != end);
2388
2389         put_dev_pagemap(pgmap);
2390         return addr == end;
2391 }
2392
2393 static int __gup_device_huge_pmd(pmd_t orig, pmd_t *pmdp, unsigned long addr,
2394                                  unsigned long end, unsigned int flags,
2395                                  struct page **pages, int *nr)
2396 {
2397         unsigned long fault_pfn;
2398         int nr_start = *nr;
2399
2400         fault_pfn = pmd_pfn(orig) + ((addr & ~PMD_MASK) >> PAGE_SHIFT);
2401         if (!__gup_device_huge(fault_pfn, addr, end, flags, pages, nr))
2402                 return 0;
2403
2404         if (unlikely(pmd_val(orig) != pmd_val(*pmdp))) {
2405                 undo_dev_pagemap(nr, nr_start, flags, pages);
2406                 return 0;
2407         }
2408         return 1;
2409 }
2410
2411 static int __gup_device_huge_pud(pud_t orig, pud_t *pudp, unsigned long addr,
2412                                  unsigned long end, unsigned int flags,
2413                                  struct page **pages, int *nr)
2414 {
2415         unsigned long fault_pfn;
2416         int nr_start = *nr;
2417
2418         fault_pfn = pud_pfn(orig) + ((addr & ~PUD_MASK) >> PAGE_SHIFT);
2419         if (!__gup_device_huge(fault_pfn, addr, end, flags, pages, nr))
2420                 return 0;
2421
2422         if (unlikely(pud_val(orig) != pud_val(*pudp))) {
2423                 undo_dev_pagemap(nr, nr_start, flags, pages);
2424                 return 0;
2425         }
2426         return 1;
2427 }
2428 #else
2429 static int __gup_device_huge_pmd(pmd_t orig, pmd_t *pmdp, unsigned long addr,
2430                                  unsigned long end, unsigned int flags,
2431                                  struct page **pages, int *nr)
2432 {
2433         BUILD_BUG();
2434         return 0;
2435 }
2436
2437 static int __gup_device_huge_pud(pud_t pud, pud_t *pudp, unsigned long addr,
2438                                  unsigned long end, unsigned int flags,
2439                                  struct page **pages, int *nr)
2440 {
2441         BUILD_BUG();
2442         return 0;
2443 }
2444 #endif
2445
2446 static int record_subpages(struct page *page, unsigned long addr,
2447                            unsigned long end, struct page **pages)
2448 {
2449         int nr;
2450
2451         for (nr = 0; addr != end; addr += PAGE_SIZE)
2452                 pages[nr++] = page++;
2453
2454         return nr;
2455 }
2456
2457 #ifdef CONFIG_ARCH_HAS_HUGEPD
2458 static unsigned long hugepte_addr_end(unsigned long addr, unsigned long end,
2459                                       unsigned long sz)
2460 {
2461         unsigned long __boundary = (addr + sz) & ~(sz-1);
2462         return (__boundary - 1 < end - 1) ? __boundary : end;
2463 }
2464
2465 static int gup_hugepte(pte_t *ptep, unsigned long sz, unsigned long addr,
2466                        unsigned long end, unsigned int flags,
2467                        struct page **pages, int *nr)
2468 {
2469         unsigned long pte_end;
2470         struct page *head, *page;
2471         pte_t pte;
2472         int refs;
2473
2474         pte_end = (addr + sz) & ~(sz-1);
2475         if (pte_end < end)
2476                 end = pte_end;
2477
2478         pte = huge_ptep_get(ptep);
2479
2480         if (!pte_access_permitted(pte, flags & FOLL_WRITE))
2481                 return 0;
2482
2483         /* hugepages are never "special" */
2484         VM_BUG_ON(!pfn_valid(pte_pfn(pte)));
2485
2486         head = pte_page(pte);
2487         page = head + ((addr & (sz-1)) >> PAGE_SHIFT);
2488         refs = record_subpages(page, addr, end, pages + *nr);
2489
2490         head = try_grab_compound_head(head, refs, flags);
2491         if (!head)
2492                 return 0;
2493
2494         if (unlikely(pte_val(pte) != pte_val(*ptep))) {
2495                 put_compound_head(head, refs, flags);
2496                 return 0;
2497         }
2498
2499         *nr += refs;
2500         SetPageReferenced(head);
2501         return 1;
2502 }
2503
2504 static int gup_huge_pd(hugepd_t hugepd, unsigned long addr,
2505                 unsigned int pdshift, unsigned long end, unsigned int flags,
2506                 struct page **pages, int *nr)
2507 {
2508         pte_t *ptep;
2509         unsigned long sz = 1UL << hugepd_shift(hugepd);
2510         unsigned long next;
2511
2512         ptep = hugepte_offset(hugepd, addr, pdshift);
2513         do {
2514                 next = hugepte_addr_end(addr, end, sz);
2515                 if (!gup_hugepte(ptep, sz, addr, end, flags, pages, nr))
2516                         return 0;
2517         } while (ptep++, addr = next, addr != end);
2518
2519         return 1;
2520 }
2521 #else
2522 static inline int gup_huge_pd(hugepd_t hugepd, unsigned long addr,
2523                 unsigned int pdshift, unsigned long end, unsigned int flags,
2524                 struct page **pages, int *nr)
2525 {
2526         return 0;
2527 }
2528 #endif /* CONFIG_ARCH_HAS_HUGEPD */
2529
2530 static int gup_huge_pmd(pmd_t orig, pmd_t *pmdp, unsigned long addr,
2531                         unsigned long end, unsigned int flags,
2532                         struct page **pages, int *nr)
2533 {
2534         struct page *head, *page;
2535         int refs;
2536
2537         if (!pmd_access_permitted(orig, flags & FOLL_WRITE))
2538                 return 0;
2539
2540         if (pmd_devmap(orig)) {
2541                 if (unlikely(flags & FOLL_LONGTERM))
2542                         return 0;
2543                 return __gup_device_huge_pmd(orig, pmdp, addr, end, flags,
2544                                              pages, nr);
2545         }
2546
2547         page = pmd_page(orig) + ((addr & ~PMD_MASK) >> PAGE_SHIFT);
2548         refs = record_subpages(page, addr, end, pages + *nr);
2549
2550         head = try_grab_compound_head(pmd_page(orig), refs, flags);
2551         if (!head)
2552                 return 0;
2553
2554         if (unlikely(pmd_val(orig) != pmd_val(*pmdp))) {
2555                 put_compound_head(head, refs, flags);
2556                 return 0;
2557         }
2558
2559         *nr += refs;
2560         SetPageReferenced(head);
2561         return 1;
2562 }
2563
2564 static int gup_huge_pud(pud_t orig, pud_t *pudp, unsigned long addr,
2565                         unsigned long end, unsigned int flags,
2566                         struct page **pages, int *nr)
2567 {
2568         struct page *head, *page;
2569         int refs;
2570
2571         if (!pud_access_permitted(orig, flags & FOLL_WRITE))
2572                 return 0;
2573
2574         if (pud_devmap(orig)) {
2575                 if (unlikely(flags & FOLL_LONGTERM))
2576                         return 0;
2577                 return __gup_device_huge_pud(orig, pudp, addr, end, flags,
2578                                              pages, nr);
2579         }
2580
2581         page = pud_page(orig) + ((addr & ~PUD_MASK) >> PAGE_SHIFT);
2582         refs = record_subpages(page, addr, end, pages + *nr);
2583
2584         head = try_grab_compound_head(pud_page(orig), refs, flags);
2585         if (!head)
2586                 return 0;
2587
2588         if (unlikely(pud_val(orig) != pud_val(*pudp))) {
2589                 put_compound_head(head, refs, flags);
2590                 return 0;
2591         }
2592
2593         *nr += refs;
2594         SetPageReferenced(head);
2595         return 1;
2596 }
2597
2598 static int gup_huge_pgd(pgd_t orig, pgd_t *pgdp, unsigned long addr,
2599                         unsigned long end, unsigned int flags,
2600                         struct page **pages, int *nr)
2601 {
2602         int refs;
2603         struct page *head, *page;
2604
2605         if (!pgd_access_permitted(orig, flags & FOLL_WRITE))
2606                 return 0;
2607
2608         BUILD_BUG_ON(pgd_devmap(orig));
2609
2610         page = pgd_page(orig) + ((addr & ~PGDIR_MASK) >> PAGE_SHIFT);
2611         refs = record_subpages(page, addr, end, pages + *nr);
2612
2613         head = try_grab_compound_head(pgd_page(orig), refs, flags);
2614         if (!head)
2615                 return 0;
2616
2617         if (unlikely(pgd_val(orig) != pgd_val(*pgdp))) {
2618                 put_compound_head(head, refs, flags);
2619                 return 0;
2620         }
2621
2622         *nr += refs;
2623         SetPageReferenced(head);
2624         return 1;
2625 }
2626
2627 static int gup_pmd_range(pud_t *pudp, pud_t pud, unsigned long addr, unsigned long end,
2628                 unsigned int flags, struct page **pages, int *nr)
2629 {
2630         unsigned long next;
2631         pmd_t *pmdp;
2632
2633         pmdp = pmd_offset_lockless(pudp, pud, addr);
2634         do {
2635                 pmd_t pmd = READ_ONCE(*pmdp);
2636
2637                 next = pmd_addr_end(addr, end);
2638                 if (!pmd_present(pmd))
2639                         return 0;
2640
2641                 if (unlikely(pmd_trans_huge(pmd) || pmd_huge(pmd) ||
2642                              pmd_devmap(pmd))) {
2643                         /*
2644                          * NUMA hinting faults need to be handled in the GUP
2645                          * slowpath for accounting purposes and so that they
2646                          * can be serialised against THP migration.
2647                          */
2648                         if (pmd_protnone(pmd))
2649                                 return 0;
2650
2651                         if (!gup_huge_pmd(pmd, pmdp, addr, next, flags,
2652                                 pages, nr))
2653                                 return 0;
2654
2655                 } else if (unlikely(is_hugepd(__hugepd(pmd_val(pmd))))) {
2656                         /*
2657                          * architecture have different format for hugetlbfs
2658                          * pmd format and THP pmd format
2659                          */
2660                         if (!gup_huge_pd(__hugepd(pmd_val(pmd)), addr,
2661                                          PMD_SHIFT, next, flags, pages, nr))
2662                                 return 0;
2663                 } else if (!gup_pte_range(pmd, addr, next, flags, pages, nr))
2664                         return 0;
2665         } while (pmdp++, addr = next, addr != end);
2666
2667         return 1;
2668 }
2669
2670 static int gup_pud_range(p4d_t *p4dp, p4d_t p4d, unsigned long addr, unsigned long end,
2671                          unsigned int flags, struct page **pages, int *nr)
2672 {
2673         unsigned long next;
2674         pud_t *pudp;
2675
2676         pudp = pud_offset_lockless(p4dp, p4d, addr);
2677         do {
2678                 pud_t pud = READ_ONCE(*pudp);
2679
2680                 next = pud_addr_end(addr, end);
2681                 if (unlikely(!pud_present(pud)))
2682                         return 0;
2683                 if (unlikely(pud_huge(pud))) {
2684                         if (!gup_huge_pud(pud, pudp, addr, next, flags,
2685                                           pages, nr))
2686                                 return 0;
2687                 } else if (unlikely(is_hugepd(__hugepd(pud_val(pud))))) {
2688                         if (!gup_huge_pd(__hugepd(pud_val(pud)), addr,
2689                                          PUD_SHIFT, next, flags, pages, nr))
2690                                 return 0;
2691                 } else if (!gup_pmd_range(pudp, pud, addr, next, flags, pages, nr))
2692                         return 0;
2693         } while (pudp++, addr = next, addr != end);
2694
2695         return 1;
2696 }
2697
2698 static int gup_p4d_range(pgd_t *pgdp, pgd_t pgd, unsigned long addr, unsigned long end,
2699                          unsigned int flags, struct page **pages, int *nr)
2700 {
2701         unsigned long next;
2702         p4d_t *p4dp;
2703
2704         p4dp = p4d_offset_lockless(pgdp, pgd, addr);
2705         do {
2706                 p4d_t p4d = READ_ONCE(*p4dp);
2707
2708                 next = p4d_addr_end(addr, end);
2709                 if (p4d_none(p4d))
2710                         return 0;
2711                 BUILD_BUG_ON(p4d_huge(p4d));
2712                 if (unlikely(is_hugepd(__hugepd(p4d_val(p4d))))) {
2713                         if (!gup_huge_pd(__hugepd(p4d_val(p4d)), addr,
2714                                          P4D_SHIFT, next, flags, pages, nr))
2715                                 return 0;
2716                 } else if (!gup_pud_range(p4dp, p4d, addr, next, flags, pages, nr))
2717                         return 0;
2718         } while (p4dp++, addr = next, addr != end);
2719
2720         return 1;
2721 }
2722
2723 static void gup_pgd_range(unsigned long addr, unsigned long end,
2724                 unsigned int flags, struct page **pages, int *nr)
2725 {
2726         unsigned long next;
2727         pgd_t *pgdp;
2728
2729         pgdp = pgd_offset(current->mm, addr);
2730         do {
2731                 pgd_t pgd = READ_ONCE(*pgdp);
2732
2733                 next = pgd_addr_end(addr, end);
2734                 if (pgd_none(pgd))
2735                         return;
2736                 if (unlikely(pgd_huge(pgd))) {
2737                         if (!gup_huge_pgd(pgd, pgdp, addr, next, flags,
2738                                           pages, nr))
2739                                 return;
2740                 } else if (unlikely(is_hugepd(__hugepd(pgd_val(pgd))))) {
2741                         if (!gup_huge_pd(__hugepd(pgd_val(pgd)), addr,
2742                                          PGDIR_SHIFT, next, flags, pages, nr))
2743                                 return;
2744                 } else if (!gup_p4d_range(pgdp, pgd, addr, next, flags, pages, nr))
2745                         return;
2746         } while (pgdp++, addr = next, addr != end);
2747 }
2748 #else
2749 static inline void gup_pgd_range(unsigned long addr, unsigned long end,
2750                 unsigned int flags, struct page **pages, int *nr)
2751 {
2752 }
2753 #endif /* CONFIG_HAVE_FAST_GUP */
2754
2755 #ifndef gup_fast_permitted
2756 /*
2757  * Check if it's allowed to use get_user_pages_fast_only() for the range, or
2758  * we need to fall back to the slow version:
2759  */
2760 static bool gup_fast_permitted(unsigned long start, unsigned long end)
2761 {
2762         return true;
2763 }
2764 #endif
2765
2766 static int __gup_longterm_unlocked(unsigned long start, int nr_pages,
2767                                    unsigned int gup_flags, struct page **pages)
2768 {
2769         int ret;
2770
2771         /*
2772          * FIXME: FOLL_LONGTERM does not work with
2773          * get_user_pages_unlocked() (see comments in that function)
2774          */
2775         if (gup_flags & FOLL_LONGTERM) {
2776                 mmap_read_lock(current->mm);
2777                 ret = __gup_longterm_locked(current->mm,
2778                                             start, nr_pages,
2779                                             pages, NULL, gup_flags);
2780                 mmap_read_unlock(current->mm);
2781         } else {
2782                 ret = get_user_pages_unlocked(start, nr_pages,
2783                                               pages, gup_flags);
2784         }
2785
2786         return ret;
2787 }
2788
2789 static unsigned long lockless_pages_from_mm(unsigned long start,
2790                                             unsigned long end,
2791                                             unsigned int gup_flags,
2792                                             struct page **pages)
2793 {
2794         unsigned long flags;
2795         int nr_pinned = 0;
2796         unsigned seq;
2797
2798         if (!IS_ENABLED(CONFIG_HAVE_FAST_GUP) ||
2799             !gup_fast_permitted(start, end))
2800                 return 0;
2801
2802         if (gup_flags & FOLL_PIN) {
2803                 seq = raw_read_seqcount(&current->mm->write_protect_seq);
2804                 if (seq & 1)
2805                         return 0;
2806         }
2807
2808         /*
2809          * Disable interrupts. The nested form is used, in order to allow full,
2810          * general purpose use of this routine.
2811          *
2812          * With interrupts disabled, we block page table pages from being freed
2813          * from under us. See struct mmu_table_batch comments in
2814          * include/asm-generic/tlb.h for more details.
2815          *
2816          * We do not adopt an rcu_read_lock() here as we also want to block IPIs
2817          * that come from THPs splitting.
2818          */
2819         local_irq_save(flags);
2820         gup_pgd_range(start, end, gup_flags, pages, &nr_pinned);
2821         local_irq_restore(flags);
2822
2823         /*
2824          * When pinning pages for DMA there could be a concurrent write protect
2825          * from fork() via copy_page_range(), in this case always fail fast GUP.
2826          */
2827         if (gup_flags & FOLL_PIN) {
2828                 if (read_seqcount_retry(&current->mm->write_protect_seq, seq)) {
2829                         unpin_user_pages(pages, nr_pinned);
2830                         return 0;
2831                 }
2832         }
2833         return nr_pinned;
2834 }
2835
2836 static int internal_get_user_pages_fast(unsigned long start,
2837                                         unsigned long nr_pages,
2838                                         unsigned int gup_flags,
2839                                         struct page **pages)
2840 {
2841         unsigned long len, end;
2842         unsigned long nr_pinned;
2843         int ret;
2844
2845         if (WARN_ON_ONCE(gup_flags & ~(FOLL_WRITE | FOLL_LONGTERM |
2846                                        FOLL_FORCE | FOLL_PIN | FOLL_GET |
2847                                        FOLL_FAST_ONLY | FOLL_NOFAULT)))
2848                 return -EINVAL;
2849
2850         if (gup_flags & FOLL_PIN)
2851                 mm_set_has_pinned_flag(&current->mm->flags);
2852
2853         if (!(gup_flags & FOLL_FAST_ONLY))
2854                 might_lock_read(&current->mm->mmap_lock);
2855
2856         start = untagged_addr(start) & PAGE_MASK;
2857         len = nr_pages << PAGE_SHIFT;
2858         if (check_add_overflow(start, len, &end))
2859                 return 0;
2860         if (unlikely(!access_ok((void __user *)start, len)))
2861                 return -EFAULT;
2862
2863         nr_pinned = lockless_pages_from_mm(start, end, gup_flags, pages);
2864         if (nr_pinned == nr_pages || gup_flags & FOLL_FAST_ONLY)
2865                 return nr_pinned;
2866
2867         /* Slow path: try to get the remaining pages with get_user_pages */
2868         start += nr_pinned << PAGE_SHIFT;
2869         pages += nr_pinned;
2870         ret = __gup_longterm_unlocked(start, nr_pages - nr_pinned, gup_flags,
2871                                       pages);
2872         if (ret < 0) {
2873                 /*
2874                  * The caller has to unpin the pages we already pinned so
2875                  * returning -errno is not an option
2876                  */
2877                 if (nr_pinned)
2878                         return nr_pinned;
2879                 return ret;
2880         }
2881         return ret + nr_pinned;
2882 }
2883
2884 /**
2885  * get_user_pages_fast_only() - pin user pages in memory
2886  * @start:      starting user address
2887  * @nr_pages:   number of pages from start to pin
2888  * @gup_flags:  flags modifying pin behaviour
2889  * @pages:      array that receives pointers to the pages pinned.
2890  *              Should be at least nr_pages long.
2891  *
2892  * Like get_user_pages_fast() except it's IRQ-safe in that it won't fall back to
2893  * the regular GUP.
2894  * Note a difference with get_user_pages_fast: this always returns the
2895  * number of pages pinned, 0 if no pages were pinned.
2896  *
2897  * If the architecture does not support this function, simply return with no
2898  * pages pinned.
2899  *
2900  * Careful, careful! COW breaking can go either way, so a non-write
2901  * access can get ambiguous page results. If you call this function without
2902  * 'write' set, you'd better be sure that you're ok with that ambiguity.
2903  */
2904 int get_user_pages_fast_only(unsigned long start, int nr_pages,
2905                              unsigned int gup_flags, struct page **pages)
2906 {
2907         int nr_pinned;
2908         /*
2909          * Internally (within mm/gup.c), gup fast variants must set FOLL_GET,
2910          * because gup fast is always a "pin with a +1 page refcount" request.
2911          *
2912          * FOLL_FAST_ONLY is required in order to match the API description of
2913          * this routine: no fall back to regular ("slow") GUP.
2914          */
2915         gup_flags |= FOLL_GET | FOLL_FAST_ONLY;
2916
2917         nr_pinned = internal_get_user_pages_fast(start, nr_pages, gup_flags,
2918                                                  pages);
2919
2920         /*
2921          * As specified in the API description above, this routine is not
2922          * allowed to return negative values. However, the common core
2923          * routine internal_get_user_pages_fast() *can* return -errno.
2924          * Therefore, correct for that here:
2925          */
2926         if (nr_pinned < 0)
2927                 nr_pinned = 0;
2928
2929         return nr_pinned;
2930 }
2931 EXPORT_SYMBOL_GPL(get_user_pages_fast_only);
2932
2933 /**
2934  * get_user_pages_fast() - pin user pages in memory
2935  * @start:      starting user address
2936  * @nr_pages:   number of pages from start to pin
2937  * @gup_flags:  flags modifying pin behaviour
2938  * @pages:      array that receives pointers to the pages pinned.
2939  *              Should be at least nr_pages long.
2940  *
2941  * Attempt to pin user pages in memory without taking mm->mmap_lock.
2942  * If not successful, it will fall back to taking the lock and
2943  * calling get_user_pages().
2944  *
2945  * Returns number of pages pinned. This may be fewer than the number requested.
2946  * If nr_pages is 0 or negative, returns 0. If no pages were pinned, returns
2947  * -errno.
2948  */
2949 int get_user_pages_fast(unsigned long start, int nr_pages,
2950                         unsigned int gup_flags, struct page **pages)
2951 {
2952         if (!is_valid_gup_flags(gup_flags))
2953                 return -EINVAL;
2954
2955         /*
2956          * The caller may or may not have explicitly set FOLL_GET; either way is
2957          * OK. However, internally (within mm/gup.c), gup fast variants must set
2958          * FOLL_GET, because gup fast is always a "pin with a +1 page refcount"
2959          * request.
2960          */
2961         gup_flags |= FOLL_GET;
2962         return internal_get_user_pages_fast(start, nr_pages, gup_flags, pages);
2963 }
2964 EXPORT_SYMBOL_GPL(get_user_pages_fast);
2965
2966 /**
2967  * pin_user_pages_fast() - pin user pages in memory without taking locks
2968  *
2969  * @start:      starting user address
2970  * @nr_pages:   number of pages from start to pin
2971  * @gup_flags:  flags modifying pin behaviour
2972  * @pages:      array that receives pointers to the pages pinned.
2973  *              Should be at least nr_pages long.
2974  *
2975  * Nearly the same as get_user_pages_fast(), except that FOLL_PIN is set. See
2976  * get_user_pages_fast() for documentation on the function arguments, because
2977  * the arguments here are identical.
2978  *
2979  * FOLL_PIN means that the pages must be released via unpin_user_page(). Please
2980  * see Documentation/core-api/pin_user_pages.rst for further details.
2981  */
2982 int pin_user_pages_fast(unsigned long start, int nr_pages,
2983                         unsigned int gup_flags, struct page **pages)
2984 {
2985         /* FOLL_GET and FOLL_PIN are mutually exclusive. */
2986         if (WARN_ON_ONCE(gup_flags & FOLL_GET))
2987                 return -EINVAL;
2988
2989         gup_flags |= FOLL_PIN;
2990         return internal_get_user_pages_fast(start, nr_pages, gup_flags, pages);
2991 }
2992 EXPORT_SYMBOL_GPL(pin_user_pages_fast);
2993
2994 /*
2995  * This is the FOLL_PIN equivalent of get_user_pages_fast_only(). Behavior
2996  * is the same, except that this one sets FOLL_PIN instead of FOLL_GET.
2997  *
2998  * The API rules are the same, too: no negative values may be returned.
2999  */
3000 int pin_user_pages_fast_only(unsigned long start, int nr_pages,
3001                              unsigned int gup_flags, struct page **pages)
3002 {
3003         int nr_pinned;
3004
3005         /*
3006          * FOLL_GET and FOLL_PIN are mutually exclusive. Note that the API
3007          * rules require returning 0, rather than -errno:
3008          */
3009         if (WARN_ON_ONCE(gup_flags & FOLL_GET))
3010                 return 0;
3011         /*
3012          * FOLL_FAST_ONLY is required in order to match the API description of
3013          * this routine: no fall back to regular ("slow") GUP.
3014          */
3015         gup_flags |= (FOLL_PIN | FOLL_FAST_ONLY);
3016         nr_pinned = internal_get_user_pages_fast(start, nr_pages, gup_flags,
3017                                                  pages);
3018         /*
3019          * This routine is not allowed to return negative values. However,
3020          * internal_get_user_pages_fast() *can* return -errno. Therefore,
3021          * correct for that here:
3022          */
3023         if (nr_pinned < 0)
3024                 nr_pinned = 0;
3025
3026         return nr_pinned;
3027 }
3028 EXPORT_SYMBOL_GPL(pin_user_pages_fast_only);
3029
3030 /**
3031  * pin_user_pages_remote() - pin pages of a remote process
3032  *
3033  * @mm:         mm_struct of target mm
3034  * @start:      starting user address
3035  * @nr_pages:   number of pages from start to pin
3036  * @gup_flags:  flags modifying lookup behaviour
3037  * @pages:      array that receives pointers to the pages pinned.
3038  *              Should be at least nr_pages long. Or NULL, if caller
3039  *              only intends to ensure the pages are faulted in.
3040  * @vmas:       array of pointers to vmas corresponding to each page.
3041  *              Or NULL if the caller does not require them.
3042  * @locked:     pointer to lock flag indicating whether lock is held and
3043  *              subsequently whether VM_FAULT_RETRY functionality can be
3044  *              utilised. Lock must initially be held.
3045  *
3046  * Nearly the same as get_user_pages_remote(), except that FOLL_PIN is set. See
3047  * get_user_pages_remote() for documentation on the function arguments, because
3048  * the arguments here are identical.
3049  *
3050  * FOLL_PIN means that the pages must be released via unpin_user_page(). Please
3051  * see Documentation/core-api/pin_user_pages.rst for details.
3052  */
3053 long pin_user_pages_remote(struct mm_struct *mm,
3054                            unsigned long start, unsigned long nr_pages,
3055                            unsigned int gup_flags, struct page **pages,
3056                            struct vm_area_struct **vmas, int *locked)
3057 {
3058         /* FOLL_GET and FOLL_PIN are mutually exclusive. */
3059         if (WARN_ON_ONCE(gup_flags & FOLL_GET))
3060                 return -EINVAL;
3061
3062         gup_flags |= FOLL_PIN;
3063         return __get_user_pages_remote(mm, start, nr_pages, gup_flags,
3064                                        pages, vmas, locked);
3065 }
3066 EXPORT_SYMBOL(pin_user_pages_remote);
3067
3068 /**
3069  * pin_user_pages() - pin user pages in memory for use by other devices
3070  *
3071  * @start:      starting user address
3072  * @nr_pages:   number of pages from start to pin
3073  * @gup_flags:  flags modifying lookup behaviour
3074  * @pages:      array that receives pointers to the pages pinned.
3075  *              Should be at least nr_pages long. Or NULL, if caller
3076  *              only intends to ensure the pages are faulted in.
3077  * @vmas:       array of pointers to vmas corresponding to each page.
3078  *              Or NULL if the caller does not require them.
3079  *
3080  * Nearly the same as get_user_pages(), except that FOLL_TOUCH is not set, and
3081  * FOLL_PIN is set.
3082  *
3083  * FOLL_PIN means that the pages must be released via unpin_user_page(). Please
3084  * see Documentation/core-api/pin_user_pages.rst for details.
3085  */
3086 long pin_user_pages(unsigned long start, unsigned long nr_pages,
3087                     unsigned int gup_flags, struct page **pages,
3088                     struct vm_area_struct **vmas)
3089 {
3090         /* FOLL_GET and FOLL_PIN are mutually exclusive. */
3091         if (WARN_ON_ONCE(gup_flags & FOLL_GET))
3092                 return -EINVAL;
3093
3094         gup_flags |= FOLL_PIN;
3095         return __gup_longterm_locked(current->mm, start, nr_pages,
3096                                      pages, vmas, gup_flags);
3097 }
3098 EXPORT_SYMBOL(pin_user_pages);
3099
3100 /*
3101  * pin_user_pages_unlocked() is the FOLL_PIN variant of
3102  * get_user_pages_unlocked(). Behavior is the same, except that this one sets
3103  * FOLL_PIN and rejects FOLL_GET.
3104  */
3105 long pin_user_pages_unlocked(unsigned long start, unsigned long nr_pages,
3106                              struct page **pages, unsigned int gup_flags)
3107 {
3108         /* FOLL_GET and FOLL_PIN are mutually exclusive. */
3109         if (WARN_ON_ONCE(gup_flags & FOLL_GET))
3110                 return -EINVAL;
3111
3112         gup_flags |= FOLL_PIN;
3113         return get_user_pages_unlocked(start, nr_pages, pages, gup_flags);
3114 }
3115 EXPORT_SYMBOL(pin_user_pages_unlocked);
3116
3117 /*
3118  * pin_user_pages_locked() is the FOLL_PIN variant of get_user_pages_locked().
3119  * Behavior is the same, except that this one sets FOLL_PIN and rejects
3120  * FOLL_GET.
3121  */
3122 long pin_user_pages_locked(unsigned long start, unsigned long nr_pages,
3123                            unsigned int gup_flags, struct page **pages,
3124                            int *locked)
3125 {
3126         /*
3127          * FIXME: Current FOLL_LONGTERM behavior is incompatible with
3128          * FAULT_FLAG_ALLOW_RETRY because of the FS DAX check requirement on
3129          * vmas.  As there are no users of this flag in this call we simply
3130          * disallow this option for now.
3131          */
3132         if (WARN_ON_ONCE(gup_flags & FOLL_LONGTERM))
3133                 return -EINVAL;
3134
3135         /* FOLL_GET and FOLL_PIN are mutually exclusive. */
3136         if (WARN_ON_ONCE(gup_flags & FOLL_GET))
3137                 return -EINVAL;
3138
3139         gup_flags |= FOLL_PIN;
3140         return __get_user_pages_locked(current->mm, start, nr_pages,
3141                                        pages, NULL, locked,
3142                                        gup_flags | FOLL_TOUCH);
3143 }
3144 EXPORT_SYMBOL(pin_user_pages_locked);