blob: 6d5e4fd55d320a13074c5b994ef5b081e66e1981 [file] [log] [blame]
Thomas Gleixner457c8992019-05-19 13:08:55 +01001// SPDX-License-Identifier: GPL-2.0-only
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -07002#include <linux/kernel.h>
3#include <linux/errno.h>
4#include <linux/err.h>
5#include <linux/spinlock.h>
6
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -07007#include <linux/mm.h>
Dan Williams3565fce2016-01-15 16:56:55 -08008#include <linux/memremap.h>
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -07009#include <linux/pagemap.h>
10#include <linux/rmap.h>
11#include <linux/swap.h>
12#include <linux/swapops.h>
13
Ingo Molnar174cd4b2017-02-02 19:15:33 +010014#include <linux/sched/signal.h>
Steve Capper2667f502014-10-09 15:29:14 -070015#include <linux/rwsem.h>
Aneesh Kumar K.Vf30c59e2014-11-05 21:57:40 +053016#include <linux/hugetlb.h>
Aneesh Kumar K.V9a4e9f32019-03-05 15:47:44 -080017#include <linux/migrate.h>
18#include <linux/mm_inline.h>
19#include <linux/sched/mm.h>
Kirill A. Shutemov1027e442015-09-04 15:47:55 -070020
Dave Hansen33a709b2016-02-12 13:02:19 -080021#include <asm/mmu_context.h>
Kirill A. Shutemov1027e442015-09-04 15:47:55 -070022#include <asm/tlbflush.h>
Steve Capper2667f502014-10-09 15:29:14 -070023
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -070024#include "internal.h"
25
Keith Buschdf06b372018-10-26 15:10:28 -070026struct follow_page_context {
27 struct dev_pagemap *pgmap;
28 unsigned int page_mask;
29};
30
John Hubbard47e29d32020-04-01 21:05:33 -070031static void hpage_pincount_add(struct page *page, int refs)
32{
33 VM_BUG_ON_PAGE(!hpage_pincount_available(page), page);
34 VM_BUG_ON_PAGE(page != compound_head(page), page);
35
36 atomic_add(refs, compound_pincount_ptr(page));
37}
38
39static void hpage_pincount_sub(struct page *page, int refs)
40{
41 VM_BUG_ON_PAGE(!hpage_pincount_available(page), page);
42 VM_BUG_ON_PAGE(page != compound_head(page), page);
43
44 atomic_sub(refs, compound_pincount_ptr(page));
45}
46
Jann Horn9109e152021-06-28 19:33:23 -070047/* Equivalent to calling put_page() @refs times. */
48static void put_page_refs(struct page *page, int refs)
49{
50#ifdef CONFIG_DEBUG_VM
51 if (VM_WARN_ON_ONCE_PAGE(page_ref_count(page) < refs, page))
52 return;
53#endif
54
55 /*
56 * Calling put_page() for each ref is unnecessarily slow. Only the last
57 * ref needs a put_page().
58 */
59 if (refs > 1)
60 page_ref_sub(page, refs - 1);
61 put_page(page);
62}
63
John Hubbarda707cdd2020-01-30 22:12:21 -080064/*
65 * Return the compound head page with ref appropriately incremented,
66 * or NULL if that failed.
67 */
68static inline struct page *try_get_compound_head(struct page *page, int refs)
69{
70 struct page *head = compound_head(page);
71
72 if (WARN_ON_ONCE(page_ref_count(head) < 0))
73 return NULL;
74 if (unlikely(!page_cache_add_speculative(head, refs)))
75 return NULL;
Jann Horn9109e152021-06-28 19:33:23 -070076
77 /*
78 * At this point we have a stable reference to the head page; but it
79 * could be that between the compound_head() lookup and the refcount
80 * increment, the compound page was split, in which case we'd end up
81 * holding a reference on a page that has nothing to do with the page
82 * we were given anymore.
83 * So now that the head page is stable, recheck that the pages still
84 * belong together.
85 */
86 if (unlikely(compound_head(page) != head)) {
87 put_page_refs(head, refs);
88 return NULL;
89 }
90
John Hubbarda707cdd2020-01-30 22:12:21 -080091 return head;
92}
93
John Hubbard3faa52c2020-04-01 21:05:29 -070094/*
95 * try_grab_compound_head() - attempt to elevate a page's refcount, by a
96 * flags-dependent amount.
97 *
98 * "grab" names in this file mean, "look at flags to decide whether to use
99 * FOLL_PIN or FOLL_GET behavior, when incrementing the page's refcount.
100 *
101 * Either FOLL_PIN or FOLL_GET (or neither) must be set, but not both at the
102 * same time. (That's true throughout the get_user_pages*() and
103 * pin_user_pages*() APIs.) Cases:
104 *
105 * FOLL_GET: page's refcount will be incremented by 1.
106 * FOLL_PIN: page's refcount will be incremented by GUP_PIN_COUNTING_BIAS.
107 *
108 * Return: head page (with refcount appropriately incremented) for success, or
109 * NULL upon failure. If neither FOLL_GET nor FOLL_PIN was set, that's
110 * considered failure, and furthermore, a likely bug in the caller, so a warning
111 * is also emitted.
112 */
113static __maybe_unused struct page *try_grab_compound_head(struct page *page,
114 int refs,
115 unsigned int flags)
116{
117 if (flags & FOLL_GET)
118 return try_get_compound_head(page, refs);
119 else if (flags & FOLL_PIN) {
John Hubbard1970dc62020-04-01 21:05:37 -0700120 int orig_refs = refs;
121
John Hubbard47e29d32020-04-01 21:05:33 -0700122 /*
Pingfan Liudf3a0a22020-04-01 21:06:04 -0700123 * Can't do FOLL_LONGTERM + FOLL_PIN with CMA in the gup fast
124 * path, so fail and let the caller fall back to the slow path.
125 */
126 if (unlikely(flags & FOLL_LONGTERM) &&
127 is_migrate_cma_page(page))
128 return NULL;
129
130 /*
Jann Horn9109e152021-06-28 19:33:23 -0700131 * CAUTION: Don't use compound_head() on the page before this
132 * point, the result won't be stable.
133 */
134 page = try_get_compound_head(page, refs);
135 if (!page)
136 return NULL;
137
138 /*
John Hubbard47e29d32020-04-01 21:05:33 -0700139 * When pinning a compound page of order > 1 (which is what
140 * hpage_pincount_available() checks for), use an exact count to
141 * track it, via hpage_pincount_add/_sub().
142 *
143 * However, be sure to *also* increment the normal page refcount
144 * field at least once, so that the page really is pinned.
145 */
John Hubbard47e29d32020-04-01 21:05:33 -0700146 if (hpage_pincount_available(page))
147 hpage_pincount_add(page, refs);
Jann Horn9109e152021-06-28 19:33:23 -0700148 else
149 page_ref_add(page, refs * (GUP_PIN_COUNTING_BIAS - 1));
John Hubbard47e29d32020-04-01 21:05:33 -0700150
John Hubbard1970dc62020-04-01 21:05:37 -0700151 mod_node_page_state(page_pgdat(page), NR_FOLL_PIN_ACQUIRED,
152 orig_refs);
153
John Hubbard47e29d32020-04-01 21:05:33 -0700154 return page;
John Hubbard3faa52c2020-04-01 21:05:29 -0700155 }
156
157 WARN_ON_ONCE(1);
158 return NULL;
159}
160
Jason Gunthorpecfde6c12020-12-14 19:05:51 -0800161static void put_compound_head(struct page *page, int refs, unsigned int flags)
162{
163 if (flags & FOLL_PIN) {
164 mod_node_page_state(page_pgdat(page), NR_FOLL_PIN_RELEASED,
165 refs);
166
167 if (hpage_pincount_available(page))
168 hpage_pincount_sub(page, refs);
169 else
170 refs *= GUP_PIN_COUNTING_BIAS;
171 }
172
Jann Horn9109e152021-06-28 19:33:23 -0700173 put_page_refs(page, refs);
Jason Gunthorpecfde6c12020-12-14 19:05:51 -0800174}
175
John Hubbard3faa52c2020-04-01 21:05:29 -0700176/**
177 * try_grab_page() - elevate a page's refcount by a flag-dependent amount
178 *
179 * This might not do anything at all, depending on the flags argument.
180 *
181 * "grab" names in this file mean, "look at flags to decide whether to use
182 * FOLL_PIN or FOLL_GET behavior, when incrementing the page's refcount.
183 *
184 * @page: pointer to page to be grabbed
185 * @flags: gup flags: these are the FOLL_* flag values.
186 *
187 * Either FOLL_PIN or FOLL_GET (or neither) may be set, but not both at the same
188 * time. Cases:
189 *
190 * FOLL_GET: page's refcount will be incremented by 1.
191 * FOLL_PIN: page's refcount will be incremented by GUP_PIN_COUNTING_BIAS.
192 *
193 * Return: true for success, or if no action was required (if neither FOLL_PIN
194 * nor FOLL_GET was set, nothing is done). False for failure: FOLL_GET or
195 * FOLL_PIN was set, but the page could not be grabbed.
196 */
197bool __must_check try_grab_page(struct page *page, unsigned int flags)
198{
199 WARN_ON_ONCE((flags & (FOLL_GET | FOLL_PIN)) == (FOLL_GET | FOLL_PIN));
200
201 if (flags & FOLL_GET)
202 return try_get_page(page);
203 else if (flags & FOLL_PIN) {
John Hubbard47e29d32020-04-01 21:05:33 -0700204 int refs = 1;
205
John Hubbard3faa52c2020-04-01 21:05:29 -0700206 page = compound_head(page);
207
208 if (WARN_ON_ONCE(page_ref_count(page) <= 0))
209 return false;
210
John Hubbard47e29d32020-04-01 21:05:33 -0700211 if (hpage_pincount_available(page))
212 hpage_pincount_add(page, 1);
213 else
214 refs = GUP_PIN_COUNTING_BIAS;
215
216 /*
217 * Similar to try_grab_compound_head(): even if using the
218 * hpage_pincount_add/_sub() routines, be sure to
219 * *also* increment the normal page refcount field at least
220 * once, so that the page really is pinned.
221 */
222 page_ref_add(page, refs);
John Hubbard1970dc62020-04-01 21:05:37 -0700223
224 mod_node_page_state(page_pgdat(page), NR_FOLL_PIN_ACQUIRED, 1);
John Hubbard3faa52c2020-04-01 21:05:29 -0700225 }
226
227 return true;
228}
229
John Hubbard3faa52c2020-04-01 21:05:29 -0700230/**
231 * unpin_user_page() - release a dma-pinned page
232 * @page: pointer to page to be released
233 *
234 * Pages that were pinned via pin_user_pages*() must be released via either
235 * unpin_user_page(), or one of the unpin_user_pages*() routines. This is so
236 * that such pages can be separately tracked and uniquely handled. In
237 * particular, interactions with RDMA and filesystems need special handling.
238 */
239void unpin_user_page(struct page *page)
240{
Jason Gunthorpecfde6c12020-12-14 19:05:51 -0800241 put_compound_head(compound_head(page), 1, FOLL_PIN);
John Hubbard3faa52c2020-04-01 21:05:29 -0700242}
243EXPORT_SYMBOL(unpin_user_page);
244
John Hubbardfc1d8e72019-05-13 17:19:08 -0700245/**
John Hubbardf1f6a7d2020-01-30 22:13:35 -0800246 * unpin_user_pages_dirty_lock() - release and optionally dirty gup-pinned pages
akpm@linux-foundation.org2d15eb32019-09-23 15:35:04 -0700247 * @pages: array of pages to be maybe marked dirty, and definitely released.
John Hubbardfc1d8e72019-05-13 17:19:08 -0700248 * @npages: number of pages in the @pages array.
akpm@linux-foundation.org2d15eb32019-09-23 15:35:04 -0700249 * @make_dirty: whether to mark the pages dirty
John Hubbardfc1d8e72019-05-13 17:19:08 -0700250 *
251 * "gup-pinned page" refers to a page that has had one of the get_user_pages()
252 * variants called on that page.
253 *
254 * For each page in the @pages array, make that page (or its head page, if a
akpm@linux-foundation.org2d15eb32019-09-23 15:35:04 -0700255 * compound page) dirty, if @make_dirty is true, and if the page was previously
John Hubbardf1f6a7d2020-01-30 22:13:35 -0800256 * listed as clean. In any case, releases all pages using unpin_user_page(),
257 * possibly via unpin_user_pages(), for the non-dirty case.
John Hubbardfc1d8e72019-05-13 17:19:08 -0700258 *
John Hubbardf1f6a7d2020-01-30 22:13:35 -0800259 * Please see the unpin_user_page() documentation for details.
John Hubbardfc1d8e72019-05-13 17:19:08 -0700260 *
akpm@linux-foundation.org2d15eb32019-09-23 15:35:04 -0700261 * set_page_dirty_lock() is used internally. If instead, set_page_dirty() is
262 * required, then the caller should a) verify that this is really correct,
263 * because _lock() is usually required, and b) hand code it:
John Hubbardf1f6a7d2020-01-30 22:13:35 -0800264 * set_page_dirty_lock(), unpin_user_page().
John Hubbardfc1d8e72019-05-13 17:19:08 -0700265 *
266 */
John Hubbardf1f6a7d2020-01-30 22:13:35 -0800267void unpin_user_pages_dirty_lock(struct page **pages, unsigned long npages,
268 bool make_dirty)
John Hubbardfc1d8e72019-05-13 17:19:08 -0700269{
akpm@linux-foundation.org2d15eb32019-09-23 15:35:04 -0700270 unsigned long index;
John Hubbardfc1d8e72019-05-13 17:19:08 -0700271
akpm@linux-foundation.org2d15eb32019-09-23 15:35:04 -0700272 /*
273 * TODO: this can be optimized for huge pages: if a series of pages is
274 * physically contiguous and part of the same compound page, then a
275 * single operation to the head page should suffice.
276 */
277
278 if (!make_dirty) {
John Hubbardf1f6a7d2020-01-30 22:13:35 -0800279 unpin_user_pages(pages, npages);
akpm@linux-foundation.org2d15eb32019-09-23 15:35:04 -0700280 return;
281 }
282
283 for (index = 0; index < npages; index++) {
284 struct page *page = compound_head(pages[index]);
285 /*
286 * Checking PageDirty at this point may race with
287 * clear_page_dirty_for_io(), but that's OK. Two key
288 * cases:
289 *
290 * 1) This code sees the page as already dirty, so it
291 * skips the call to set_page_dirty(). That could happen
292 * because clear_page_dirty_for_io() called
293 * page_mkclean(), followed by set_page_dirty().
294 * However, now the page is going to get written back,
295 * which meets the original intention of setting it
296 * dirty, so all is well: clear_page_dirty_for_io() goes
297 * on to call TestClearPageDirty(), and write the page
298 * back.
299 *
300 * 2) This code sees the page as clean, so it calls
301 * set_page_dirty(). The page stays dirty, despite being
302 * written back, so it gets written back again in the
303 * next writeback cycle. This is harmless.
304 */
305 if (!PageDirty(page))
306 set_page_dirty_lock(page);
John Hubbardf1f6a7d2020-01-30 22:13:35 -0800307 unpin_user_page(page);
akpm@linux-foundation.org2d15eb32019-09-23 15:35:04 -0700308 }
John Hubbardfc1d8e72019-05-13 17:19:08 -0700309}
John Hubbardf1f6a7d2020-01-30 22:13:35 -0800310EXPORT_SYMBOL(unpin_user_pages_dirty_lock);
John Hubbardfc1d8e72019-05-13 17:19:08 -0700311
312/**
John Hubbardf1f6a7d2020-01-30 22:13:35 -0800313 * unpin_user_pages() - release an array of gup-pinned pages.
John Hubbardfc1d8e72019-05-13 17:19:08 -0700314 * @pages: array of pages to be marked dirty and released.
315 * @npages: number of pages in the @pages array.
316 *
John Hubbardf1f6a7d2020-01-30 22:13:35 -0800317 * For each page in the @pages array, release the page using unpin_user_page().
John Hubbardfc1d8e72019-05-13 17:19:08 -0700318 *
John Hubbardf1f6a7d2020-01-30 22:13:35 -0800319 * Please see the unpin_user_page() documentation for details.
John Hubbardfc1d8e72019-05-13 17:19:08 -0700320 */
John Hubbardf1f6a7d2020-01-30 22:13:35 -0800321void unpin_user_pages(struct page **pages, unsigned long npages)
John Hubbardfc1d8e72019-05-13 17:19:08 -0700322{
323 unsigned long index;
324
325 /*
John Hubbard146608bb2020-10-13 16:52:01 -0700326 * If this WARN_ON() fires, then the system *might* be leaking pages (by
327 * leaving them pinned), but probably not. More likely, gup/pup returned
328 * a hard -ERRNO error to the caller, who erroneously passed it here.
329 */
330 if (WARN_ON(IS_ERR_VALUE(npages)))
331 return;
332 /*
John Hubbardfc1d8e72019-05-13 17:19:08 -0700333 * TODO: this can be optimized for huge pages: if a series of pages is
334 * physically contiguous and part of the same compound page, then a
335 * single operation to the head page should suffice.
336 */
337 for (index = 0; index < npages; index++)
John Hubbardf1f6a7d2020-01-30 22:13:35 -0800338 unpin_user_page(pages[index]);
John Hubbardfc1d8e72019-05-13 17:19:08 -0700339}
John Hubbardf1f6a7d2020-01-30 22:13:35 -0800340EXPORT_SYMBOL(unpin_user_pages);
John Hubbardfc1d8e72019-05-13 17:19:08 -0700341
Christoph Hellwig050a9ad2019-07-11 20:57:21 -0700342#ifdef CONFIG_MMU
Kirill A. Shutemov69e68b42014-06-04 16:08:11 -0700343static struct page *no_page_table(struct vm_area_struct *vma,
344 unsigned int flags)
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700345{
Kirill A. Shutemov69e68b42014-06-04 16:08:11 -0700346 /*
347 * When core dumping an enormous anonymous area that nobody
348 * has touched so far, we don't want to allocate unnecessary pages or
349 * page tables. Return error instead of NULL to skip handle_mm_fault,
350 * then get_dump_page() will return NULL to leave a hole in the dump.
351 * But we can only make this optimization where a hole would surely
352 * be zero-filled if handle_mm_fault() actually did handle it.
353 */
Anshuman Khanduala0137f12020-04-06 20:03:55 -0700354 if ((flags & FOLL_DUMP) &&
355 (vma_is_anonymous(vma) || !vma->vm_ops->fault))
Kirill A. Shutemov69e68b42014-06-04 16:08:11 -0700356 return ERR_PTR(-EFAULT);
357 return NULL;
358}
359
Kirill A. Shutemov1027e442015-09-04 15:47:55 -0700360static int follow_pfn_pte(struct vm_area_struct *vma, unsigned long address,
361 pte_t *pte, unsigned int flags)
362{
363 /* No page to get reference */
364 if (flags & FOLL_GET)
365 return -EFAULT;
366
367 if (flags & FOLL_TOUCH) {
368 pte_t entry = *pte;
369
370 if (flags & FOLL_WRITE)
371 entry = pte_mkdirty(entry);
372 entry = pte_mkyoung(entry);
373
374 if (!pte_same(*pte, entry)) {
375 set_pte_at(vma->vm_mm, address, pte, entry);
376 update_mmu_cache(vma, address, pte);
377 }
378 }
379
380 /* Proper page table entry exists, but no corresponding struct page */
381 return -EEXIST;
382}
383
Linus Torvalds19be0ea2016-10-13 13:07:36 -0700384/*
Peter Xua308c712020-08-21 19:49:57 -0400385 * FOLL_FORCE can write to even unwritable pte's, but only
386 * after we've gone through a COW cycle and they are dirty.
Linus Torvalds19be0ea2016-10-13 13:07:36 -0700387 */
388static inline bool can_follow_write_pte(pte_t pte, unsigned int flags)
389{
Peter Xua308c712020-08-21 19:49:57 -0400390 return pte_write(pte) ||
391 ((flags & FOLL_FORCE) && (flags & FOLL_COW) && pte_dirty(pte));
Linus Torvalds19be0ea2016-10-13 13:07:36 -0700392}
393
Kirill A. Shutemov69e68b42014-06-04 16:08:11 -0700394static struct page *follow_page_pte(struct vm_area_struct *vma,
Keith Buschdf06b372018-10-26 15:10:28 -0700395 unsigned long address, pmd_t *pmd, unsigned int flags,
396 struct dev_pagemap **pgmap)
Kirill A. Shutemov69e68b42014-06-04 16:08:11 -0700397{
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700398 struct mm_struct *mm = vma->vm_mm;
Kirill A. Shutemov69e68b42014-06-04 16:08:11 -0700399 struct page *page;
400 spinlock_t *ptl;
401 pte_t *ptep, pte;
Claudio Imbrendaf28d4362020-04-01 21:05:56 -0700402 int ret;
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700403
John Hubbardeddb1c22020-01-30 22:12:54 -0800404 /* FOLL_GET and FOLL_PIN are mutually exclusive. */
405 if (WARN_ON_ONCE((flags & (FOLL_PIN | FOLL_GET)) ==
406 (FOLL_PIN | FOLL_GET)))
407 return ERR_PTR(-EINVAL);
Baolin Wangfccee932022-09-01 18:41:31 +0800408
409 /*
410 * Considering PTE level hugetlb, like continuous-PTE hugetlb on
411 * ARM64 architecture.
412 */
413 if (is_vm_hugetlb_page(vma)) {
414 page = follow_huge_pmd_pte(vma, address, flags);
415 if (page)
416 return page;
417 return no_page_table(vma, flags);
418 }
419
Kirill A. Shutemov69e68b42014-06-04 16:08:11 -0700420retry:
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700421 if (unlikely(pmd_bad(*pmd)))
Kirill A. Shutemov69e68b42014-06-04 16:08:11 -0700422 return no_page_table(vma, flags);
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700423
424 ptep = pte_offset_map_lock(mm, pmd, address, &ptl);
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700425 pte = *ptep;
426 if (!pte_present(pte)) {
427 swp_entry_t entry;
428 /*
429 * KSM's break_ksm() relies upon recognizing a ksm page
430 * even while it is being migrated, so for that case we
431 * need migration_entry_wait().
432 */
433 if (likely(!(flags & FOLL_MIGRATION)))
434 goto no_page;
Kirill A. Shutemov0661a332015-02-10 14:10:04 -0800435 if (pte_none(pte))
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700436 goto no_page;
437 entry = pte_to_swp_entry(pte);
438 if (!is_migration_entry(entry))
439 goto no_page;
440 pte_unmap_unlock(ptep, ptl);
441 migration_entry_wait(mm, pmd, address);
Kirill A. Shutemov69e68b42014-06-04 16:08:11 -0700442 goto retry;
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700443 }
Mel Gorman8a0516e2015-02-12 14:58:22 -0800444 if ((flags & FOLL_NUMA) && pte_protnone(pte))
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700445 goto no_page;
Linus Torvalds19be0ea2016-10-13 13:07:36 -0700446 if ((flags & FOLL_WRITE) && !can_follow_write_pte(pte, flags)) {
Kirill A. Shutemov69e68b42014-06-04 16:08:11 -0700447 pte_unmap_unlock(ptep, ptl);
448 return NULL;
449 }
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700450
451 page = vm_normal_page(vma, address, pte);
John Hubbard3faa52c2020-04-01 21:05:29 -0700452 if (!page && pte_devmap(pte) && (flags & (FOLL_GET | FOLL_PIN))) {
Dan Williams3565fce2016-01-15 16:56:55 -0800453 /*
John Hubbard3faa52c2020-04-01 21:05:29 -0700454 * Only return device mapping pages in the FOLL_GET or FOLL_PIN
455 * case since they are only valid while holding the pgmap
456 * reference.
Dan Williams3565fce2016-01-15 16:56:55 -0800457 */
Keith Buschdf06b372018-10-26 15:10:28 -0700458 *pgmap = get_dev_pagemap(pte_pfn(pte), *pgmap);
459 if (*pgmap)
Dan Williams3565fce2016-01-15 16:56:55 -0800460 page = pte_page(pte);
461 else
462 goto no_page;
463 } else if (unlikely(!page)) {
Kirill A. Shutemov1027e442015-09-04 15:47:55 -0700464 if (flags & FOLL_DUMP) {
465 /* Avoid special (like zero) pages in core dumps */
466 page = ERR_PTR(-EFAULT);
467 goto out;
468 }
469
470 if (is_zero_pfn(pte_pfn(pte))) {
471 page = pte_page(pte);
472 } else {
Kirill A. Shutemov1027e442015-09-04 15:47:55 -0700473 ret = follow_pfn_pte(vma, address, ptep, flags);
474 page = ERR_PTR(ret);
475 goto out;
476 }
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700477 }
478
Kirill A. Shutemov6742d292016-01-15 16:52:28 -0800479 if (flags & FOLL_SPLIT && PageTransCompound(page)) {
Kirill A. Shutemov6742d292016-01-15 16:52:28 -0800480 get_page(page);
481 pte_unmap_unlock(ptep, ptl);
482 lock_page(page);
483 ret = split_huge_page(page);
484 unlock_page(page);
485 put_page(page);
486 if (ret)
487 return ERR_PTR(ret);
488 goto retry;
489 }
490
John Hubbard3faa52c2020-04-01 21:05:29 -0700491 /* try_grab_page() does nothing unless FOLL_GET or FOLL_PIN is set. */
492 if (unlikely(!try_grab_page(page, flags))) {
493 page = ERR_PTR(-ENOMEM);
494 goto out;
Linus Torvalds8fde12c2019-04-11 10:49:19 -0700495 }
Claudio Imbrendaf28d4362020-04-01 21:05:56 -0700496 /*
497 * We need to make the page accessible if and only if we are going
498 * to access its content (the FOLL_PIN case). Please see
499 * Documentation/core-api/pin_user_pages.rst for details.
500 */
501 if (flags & FOLL_PIN) {
502 ret = arch_make_page_accessible(page);
503 if (ret) {
504 unpin_user_page(page);
505 page = ERR_PTR(ret);
506 goto out;
507 }
508 }
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700509 if (flags & FOLL_TOUCH) {
510 if ((flags & FOLL_WRITE) &&
511 !pte_dirty(pte) && !PageDirty(page))
512 set_page_dirty(page);
513 /*
514 * pte_mkyoung() would be more correct here, but atomic care
515 * is needed to avoid losing the dirty bit: it is easier to use
516 * mark_page_accessed().
517 */
518 mark_page_accessed(page);
519 }
Eric B Munsonde60f5f2015-11-05 18:51:36 -0800520 if ((flags & FOLL_MLOCK) && (vma->vm_flags & VM_LOCKED)) {
Kirill A. Shutemove90309c2016-01-15 16:54:33 -0800521 /* Do not mlock pte-mapped THP */
522 if (PageTransCompound(page))
523 goto out;
524
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700525 /*
526 * The preliminary mapping check is mainly to avoid the
527 * pointless overhead of lock_page on the ZERO_PAGE
528 * which might bounce very badly if there is contention.
529 *
530 * If the page is already locked, we don't need to
531 * handle it now - vmscan will handle it later if and
532 * when it attempts to reclaim the page.
533 */
534 if (page->mapping && trylock_page(page)) {
535 lru_add_drain(); /* push cached pages to LRU */
536 /*
537 * Because we lock page here, and migration is
538 * blocked by the pte's page reference, and we
539 * know the page is still mapped, we don't even
540 * need to check for file-cache page truncation.
541 */
542 mlock_vma_page(page);
543 unlock_page(page);
544 }
545 }
Kirill A. Shutemov1027e442015-09-04 15:47:55 -0700546out:
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700547 pte_unmap_unlock(ptep, ptl);
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700548 return page;
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700549no_page:
550 pte_unmap_unlock(ptep, ptl);
551 if (!pte_none(pte))
Kirill A. Shutemov69e68b42014-06-04 16:08:11 -0700552 return NULL;
553 return no_page_table(vma, flags);
554}
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700555
Aneesh Kumar K.V080dbb62017-07-06 15:38:44 -0700556static struct page *follow_pmd_mask(struct vm_area_struct *vma,
557 unsigned long address, pud_t *pudp,
Keith Buschdf06b372018-10-26 15:10:28 -0700558 unsigned int flags,
559 struct follow_page_context *ctx)
Kirill A. Shutemov69e68b42014-06-04 16:08:11 -0700560{
Huang Ying68827282018-06-07 17:06:34 -0700561 pmd_t *pmd, pmdval;
Kirill A. Shutemov69e68b42014-06-04 16:08:11 -0700562 spinlock_t *ptl;
563 struct page *page;
564 struct mm_struct *mm = vma->vm_mm;
565
Aneesh Kumar K.V080dbb62017-07-06 15:38:44 -0700566 pmd = pmd_offset(pudp, address);
Huang Ying68827282018-06-07 17:06:34 -0700567 /*
568 * The READ_ONCE() will stabilize the pmdval in a register or
569 * on the stack so that it will stop changing under the code.
570 */
571 pmdval = READ_ONCE(*pmd);
572 if (pmd_none(pmdval))
Kirill A. Shutemov69e68b42014-06-04 16:08:11 -0700573 return no_page_table(vma, flags);
Wei Yangbe9d3042020-01-30 22:12:14 -0800574 if (pmd_huge(pmdval) && is_vm_hugetlb_page(vma)) {
Baolin Wangfccee932022-09-01 18:41:31 +0800575 page = follow_huge_pmd_pte(vma, address, flags);
Naoya Horiguchie66f17f2015-02-11 15:25:22 -0800576 if (page)
577 return page;
578 return no_page_table(vma, flags);
Kirill A. Shutemov69e68b42014-06-04 16:08:11 -0700579 }
Huang Ying68827282018-06-07 17:06:34 -0700580 if (is_hugepd(__hugepd(pmd_val(pmdval)))) {
Aneesh Kumar K.V4dc71452017-07-06 15:38:56 -0700581 page = follow_huge_pd(vma, address,
Huang Ying68827282018-06-07 17:06:34 -0700582 __hugepd(pmd_val(pmdval)), flags,
Aneesh Kumar K.V4dc71452017-07-06 15:38:56 -0700583 PMD_SHIFT);
584 if (page)
585 return page;
586 return no_page_table(vma, flags);
587 }
Zi Yan84c3fc42017-09-08 16:11:01 -0700588retry:
Huang Ying68827282018-06-07 17:06:34 -0700589 if (!pmd_present(pmdval)) {
Zi Yan84c3fc42017-09-08 16:11:01 -0700590 if (likely(!(flags & FOLL_MIGRATION)))
591 return no_page_table(vma, flags);
592 VM_BUG_ON(thp_migration_supported() &&
Huang Ying68827282018-06-07 17:06:34 -0700593 !is_pmd_migration_entry(pmdval));
594 if (is_pmd_migration_entry(pmdval))
Zi Yan84c3fc42017-09-08 16:11:01 -0700595 pmd_migration_entry_wait(mm, pmd);
Huang Ying68827282018-06-07 17:06:34 -0700596 pmdval = READ_ONCE(*pmd);
597 /*
598 * MADV_DONTNEED may convert the pmd to null because
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -0700599 * mmap_lock is held in read mode
Huang Ying68827282018-06-07 17:06:34 -0700600 */
601 if (pmd_none(pmdval))
602 return no_page_table(vma, flags);
Zi Yan84c3fc42017-09-08 16:11:01 -0700603 goto retry;
604 }
Huang Ying68827282018-06-07 17:06:34 -0700605 if (pmd_devmap(pmdval)) {
Dan Williams3565fce2016-01-15 16:56:55 -0800606 ptl = pmd_lock(mm, pmd);
Keith Buschdf06b372018-10-26 15:10:28 -0700607 page = follow_devmap_pmd(vma, address, pmd, flags, &ctx->pgmap);
Dan Williams3565fce2016-01-15 16:56:55 -0800608 spin_unlock(ptl);
609 if (page)
610 return page;
611 }
Huang Ying68827282018-06-07 17:06:34 -0700612 if (likely(!pmd_trans_huge(pmdval)))
Keith Buschdf06b372018-10-26 15:10:28 -0700613 return follow_page_pte(vma, address, pmd, flags, &ctx->pgmap);
Kirill A. Shutemov6742d292016-01-15 16:52:28 -0800614
Huang Ying68827282018-06-07 17:06:34 -0700615 if ((flags & FOLL_NUMA) && pmd_protnone(pmdval))
Aneesh Kumar K.Vdb08f202017-02-24 14:59:53 -0800616 return no_page_table(vma, flags);
617
Zi Yan84c3fc42017-09-08 16:11:01 -0700618retry_locked:
Kirill A. Shutemov6742d292016-01-15 16:52:28 -0800619 ptl = pmd_lock(mm, pmd);
Huang Ying68827282018-06-07 17:06:34 -0700620 if (unlikely(pmd_none(*pmd))) {
621 spin_unlock(ptl);
622 return no_page_table(vma, flags);
623 }
Zi Yan84c3fc42017-09-08 16:11:01 -0700624 if (unlikely(!pmd_present(*pmd))) {
625 spin_unlock(ptl);
626 if (likely(!(flags & FOLL_MIGRATION)))
627 return no_page_table(vma, flags);
628 pmd_migration_entry_wait(mm, pmd);
629 goto retry_locked;
630 }
Kirill A. Shutemov6742d292016-01-15 16:52:28 -0800631 if (unlikely(!pmd_trans_huge(*pmd))) {
632 spin_unlock(ptl);
Keith Buschdf06b372018-10-26 15:10:28 -0700633 return follow_page_pte(vma, address, pmd, flags, &ctx->pgmap);
Kirill A. Shutemov69e68b42014-06-04 16:08:11 -0700634 }
Song Liubfe7b002019-09-23 15:38:25 -0700635 if (flags & (FOLL_SPLIT | FOLL_SPLIT_PMD)) {
Kirill A. Shutemov6742d292016-01-15 16:52:28 -0800636 int ret;
637 page = pmd_page(*pmd);
638 if (is_huge_zero_page(page)) {
639 spin_unlock(ptl);
640 ret = 0;
Kirill A. Shutemov78ddc532016-01-15 16:52:42 -0800641 split_huge_pmd(vma, pmd, address);
Naoya Horiguchi337d9ab2016-07-26 15:24:03 -0700642 if (pmd_trans_unstable(pmd))
643 ret = -EBUSY;
Song Liubfe7b002019-09-23 15:38:25 -0700644 } else if (flags & FOLL_SPLIT) {
Linus Torvalds8fde12c2019-04-11 10:49:19 -0700645 if (unlikely(!try_get_page(page))) {
646 spin_unlock(ptl);
647 return ERR_PTR(-ENOMEM);
648 }
Kirill A. Shutemov6742d292016-01-15 16:52:28 -0800649 spin_unlock(ptl);
650 lock_page(page);
651 ret = split_huge_page(page);
652 unlock_page(page);
653 put_page(page);
Kirill A. Shutemovbaa355f2016-07-26 15:25:51 -0700654 if (pmd_none(*pmd))
655 return no_page_table(vma, flags);
Song Liubfe7b002019-09-23 15:38:25 -0700656 } else { /* flags & FOLL_SPLIT_PMD */
657 spin_unlock(ptl);
658 split_huge_pmd(vma, pmd, address);
659 ret = pte_alloc(mm, pmd) ? -ENOMEM : 0;
Kirill A. Shutemov6742d292016-01-15 16:52:28 -0800660 }
661
662 return ret ? ERR_PTR(ret) :
Keith Buschdf06b372018-10-26 15:10:28 -0700663 follow_page_pte(vma, address, pmd, flags, &ctx->pgmap);
Kirill A. Shutemov6742d292016-01-15 16:52:28 -0800664 }
Kirill A. Shutemov6742d292016-01-15 16:52:28 -0800665 page = follow_trans_huge_pmd(vma, address, pmd, flags);
666 spin_unlock(ptl);
Keith Buschdf06b372018-10-26 15:10:28 -0700667 ctx->page_mask = HPAGE_PMD_NR - 1;
Kirill A. Shutemov6742d292016-01-15 16:52:28 -0800668 return page;
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700669}
670
Aneesh Kumar K.V080dbb62017-07-06 15:38:44 -0700671static struct page *follow_pud_mask(struct vm_area_struct *vma,
672 unsigned long address, p4d_t *p4dp,
Keith Buschdf06b372018-10-26 15:10:28 -0700673 unsigned int flags,
674 struct follow_page_context *ctx)
Aneesh Kumar K.V080dbb62017-07-06 15:38:44 -0700675{
676 pud_t *pud;
677 spinlock_t *ptl;
678 struct page *page;
679 struct mm_struct *mm = vma->vm_mm;
680
681 pud = pud_offset(p4dp, address);
682 if (pud_none(*pud))
683 return no_page_table(vma, flags);
Wei Yangbe9d3042020-01-30 22:12:14 -0800684 if (pud_huge(*pud) && is_vm_hugetlb_page(vma)) {
Aneesh Kumar K.V080dbb62017-07-06 15:38:44 -0700685 page = follow_huge_pud(mm, address, pud, flags);
686 if (page)
687 return page;
688 return no_page_table(vma, flags);
689 }
Aneesh Kumar K.V4dc71452017-07-06 15:38:56 -0700690 if (is_hugepd(__hugepd(pud_val(*pud)))) {
691 page = follow_huge_pd(vma, address,
692 __hugepd(pud_val(*pud)), flags,
693 PUD_SHIFT);
694 if (page)
695 return page;
696 return no_page_table(vma, flags);
697 }
Aneesh Kumar K.V080dbb62017-07-06 15:38:44 -0700698 if (pud_devmap(*pud)) {
699 ptl = pud_lock(mm, pud);
Keith Buschdf06b372018-10-26 15:10:28 -0700700 page = follow_devmap_pud(vma, address, pud, flags, &ctx->pgmap);
Aneesh Kumar K.V080dbb62017-07-06 15:38:44 -0700701 spin_unlock(ptl);
702 if (page)
703 return page;
704 }
705 if (unlikely(pud_bad(*pud)))
706 return no_page_table(vma, flags);
707
Keith Buschdf06b372018-10-26 15:10:28 -0700708 return follow_pmd_mask(vma, address, pud, flags, ctx);
Aneesh Kumar K.V080dbb62017-07-06 15:38:44 -0700709}
710
Aneesh Kumar K.V080dbb62017-07-06 15:38:44 -0700711static struct page *follow_p4d_mask(struct vm_area_struct *vma,
712 unsigned long address, pgd_t *pgdp,
Keith Buschdf06b372018-10-26 15:10:28 -0700713 unsigned int flags,
714 struct follow_page_context *ctx)
Aneesh Kumar K.V080dbb62017-07-06 15:38:44 -0700715{
716 p4d_t *p4d;
Aneesh Kumar K.V4dc71452017-07-06 15:38:56 -0700717 struct page *page;
Aneesh Kumar K.V080dbb62017-07-06 15:38:44 -0700718
719 p4d = p4d_offset(pgdp, address);
720 if (p4d_none(*p4d))
721 return no_page_table(vma, flags);
722 BUILD_BUG_ON(p4d_huge(*p4d));
723 if (unlikely(p4d_bad(*p4d)))
724 return no_page_table(vma, flags);
725
Aneesh Kumar K.V4dc71452017-07-06 15:38:56 -0700726 if (is_hugepd(__hugepd(p4d_val(*p4d)))) {
727 page = follow_huge_pd(vma, address,
728 __hugepd(p4d_val(*p4d)), flags,
729 P4D_SHIFT);
730 if (page)
731 return page;
732 return no_page_table(vma, flags);
733 }
Keith Buschdf06b372018-10-26 15:10:28 -0700734 return follow_pud_mask(vma, address, p4d, flags, ctx);
Aneesh Kumar K.V080dbb62017-07-06 15:38:44 -0700735}
736
737/**
738 * follow_page_mask - look up a page descriptor from a user-virtual address
739 * @vma: vm_area_struct mapping @address
740 * @address: virtual address to look up
741 * @flags: flags modifying lookup behaviour
Mike Rapoport78179552018-11-16 15:08:29 -0800742 * @ctx: contains dev_pagemap for %ZONE_DEVICE memory pinning and a
743 * pointer to output page_mask
Aneesh Kumar K.V080dbb62017-07-06 15:38:44 -0700744 *
745 * @flags can have FOLL_ flags set, defined in <linux/mm.h>
746 *
Mike Rapoport78179552018-11-16 15:08:29 -0800747 * When getting pages from ZONE_DEVICE memory, the @ctx->pgmap caches
748 * the device's dev_pagemap metadata to avoid repeating expensive lookups.
749 *
750 * On output, the @ctx->page_mask is set according to the size of the page.
751 *
752 * Return: the mapped (struct page *), %NULL if no mapping exists, or
Aneesh Kumar K.V080dbb62017-07-06 15:38:44 -0700753 * an error pointer if there is a mapping to something not represented
754 * by a page descriptor (see also vm_normal_page()).
755 */
Bharath Vedarthama7030ae2019-07-11 20:54:34 -0700756static struct page *follow_page_mask(struct vm_area_struct *vma,
Aneesh Kumar K.V080dbb62017-07-06 15:38:44 -0700757 unsigned long address, unsigned int flags,
Keith Buschdf06b372018-10-26 15:10:28 -0700758 struct follow_page_context *ctx)
Aneesh Kumar K.V080dbb62017-07-06 15:38:44 -0700759{
760 pgd_t *pgd;
761 struct page *page;
762 struct mm_struct *mm = vma->vm_mm;
763
Keith Buschdf06b372018-10-26 15:10:28 -0700764 ctx->page_mask = 0;
Aneesh Kumar K.V080dbb62017-07-06 15:38:44 -0700765
766 /* make this handle hugepd */
767 page = follow_huge_addr(mm, address, flags & FOLL_WRITE);
768 if (!IS_ERR(page)) {
John Hubbard3faa52c2020-04-01 21:05:29 -0700769 WARN_ON_ONCE(flags & (FOLL_GET | FOLL_PIN));
Aneesh Kumar K.V080dbb62017-07-06 15:38:44 -0700770 return page;
771 }
772
773 pgd = pgd_offset(mm, address);
774
775 if (pgd_none(*pgd) || unlikely(pgd_bad(*pgd)))
776 return no_page_table(vma, flags);
777
Anshuman Khandualfaaa5b62017-07-06 15:38:50 -0700778 if (pgd_huge(*pgd)) {
779 page = follow_huge_pgd(mm, address, pgd, flags);
780 if (page)
781 return page;
782 return no_page_table(vma, flags);
783 }
Aneesh Kumar K.V4dc71452017-07-06 15:38:56 -0700784 if (is_hugepd(__hugepd(pgd_val(*pgd)))) {
785 page = follow_huge_pd(vma, address,
786 __hugepd(pgd_val(*pgd)), flags,
787 PGDIR_SHIFT);
788 if (page)
789 return page;
790 return no_page_table(vma, flags);
791 }
Anshuman Khandualfaaa5b62017-07-06 15:38:50 -0700792
Keith Buschdf06b372018-10-26 15:10:28 -0700793 return follow_p4d_mask(vma, address, pgd, flags, ctx);
794}
795
796struct page *follow_page(struct vm_area_struct *vma, unsigned long address,
797 unsigned int foll_flags)
798{
799 struct follow_page_context ctx = { NULL };
800 struct page *page;
801
802 page = follow_page_mask(vma, address, foll_flags, &ctx);
803 if (ctx.pgmap)
804 put_dev_pagemap(ctx.pgmap);
805 return page;
Aneesh Kumar K.V080dbb62017-07-06 15:38:44 -0700806}
807
Kirill A. Shutemovf2b495c2014-06-04 16:08:11 -0700808static int get_gate_page(struct mm_struct *mm, unsigned long address,
809 unsigned int gup_flags, struct vm_area_struct **vma,
810 struct page **page)
811{
812 pgd_t *pgd;
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +0300813 p4d_t *p4d;
Kirill A. Shutemovf2b495c2014-06-04 16:08:11 -0700814 pud_t *pud;
815 pmd_t *pmd;
816 pte_t *pte;
817 int ret = -EFAULT;
818
819 /* user gate pages are read-only */
820 if (gup_flags & FOLL_WRITE)
821 return -EFAULT;
822 if (address > TASK_SIZE)
823 pgd = pgd_offset_k(address);
824 else
825 pgd = pgd_offset_gate(mm, address);
Andy Lutomirskib5d1c392019-07-11 20:57:43 -0700826 if (pgd_none(*pgd))
827 return -EFAULT;
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +0300828 p4d = p4d_offset(pgd, address);
Andy Lutomirskib5d1c392019-07-11 20:57:43 -0700829 if (p4d_none(*p4d))
830 return -EFAULT;
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +0300831 pud = pud_offset(p4d, address);
Andy Lutomirskib5d1c392019-07-11 20:57:43 -0700832 if (pud_none(*pud))
833 return -EFAULT;
Kirill A. Shutemovf2b495c2014-06-04 16:08:11 -0700834 pmd = pmd_offset(pud, address);
Zi Yan84c3fc42017-09-08 16:11:01 -0700835 if (!pmd_present(*pmd))
Kirill A. Shutemovf2b495c2014-06-04 16:08:11 -0700836 return -EFAULT;
837 VM_BUG_ON(pmd_trans_huge(*pmd));
838 pte = pte_offset_map(pmd, address);
839 if (pte_none(*pte))
840 goto unmap;
841 *vma = get_gate_vma(mm);
842 if (!page)
843 goto out;
844 *page = vm_normal_page(*vma, address, *pte);
845 if (!*page) {
846 if ((gup_flags & FOLL_DUMP) || !is_zero_pfn(pte_pfn(*pte)))
847 goto unmap;
848 *page = pte_page(*pte);
849 }
Dave Hansen9fa2dd92020-09-03 13:40:28 -0700850 if (unlikely(!try_grab_page(*page, gup_flags))) {
Linus Torvalds8fde12c2019-04-11 10:49:19 -0700851 ret = -ENOMEM;
852 goto unmap;
853 }
Kirill A. Shutemovf2b495c2014-06-04 16:08:11 -0700854out:
855 ret = 0;
856unmap:
857 pte_unmap(pte);
858 return ret;
859}
860
Paul Cassella9a95f3c2014-08-06 16:07:24 -0700861/*
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -0700862 * mmap_lock must be held on entry. If @locked != NULL and *@flags
863 * does not include FOLL_NOWAIT, the mmap_lock may be released. If it
Peter Xu4f6da932020-04-01 21:07:58 -0700864 * is, *@locked will be set to 0 and -EBUSY returned.
Paul Cassella9a95f3c2014-08-06 16:07:24 -0700865 */
Peter Xu64019a22020-08-11 18:39:01 -0700866static int faultin_page(struct vm_area_struct *vma,
Peter Xu4f6da932020-04-01 21:07:58 -0700867 unsigned long address, unsigned int *flags, int *locked)
Kirill A. Shutemov16744482014-06-04 16:08:12 -0700868{
Kirill A. Shutemov16744482014-06-04 16:08:12 -0700869 unsigned int fault_flags = 0;
Souptick Joarder2b740302018-08-23 17:01:36 -0700870 vm_fault_t ret;
Kirill A. Shutemov16744482014-06-04 16:08:12 -0700871
Eric B Munsonde60f5f2015-11-05 18:51:36 -0800872 /* mlock all present pages, but do not fault in new pages */
873 if ((*flags & (FOLL_POPULATE | FOLL_MLOCK)) == FOLL_MLOCK)
874 return -ENOENT;
Kirill A. Shutemov16744482014-06-04 16:08:12 -0700875 if (*flags & FOLL_WRITE)
876 fault_flags |= FAULT_FLAG_WRITE;
Dave Hansen1b2ee122016-02-12 13:02:21 -0800877 if (*flags & FOLL_REMOTE)
878 fault_flags |= FAULT_FLAG_REMOTE;
Peter Xu4f6da932020-04-01 21:07:58 -0700879 if (locked)
Peter Xu71335f32020-04-01 21:08:53 -0700880 fault_flags |= FAULT_FLAG_ALLOW_RETRY | FAULT_FLAG_KILLABLE;
Kirill A. Shutemov16744482014-06-04 16:08:12 -0700881 if (*flags & FOLL_NOWAIT)
882 fault_flags |= FAULT_FLAG_ALLOW_RETRY | FAULT_FLAG_RETRY_NOWAIT;
Andres Lagar-Cavilla234b2392014-09-17 10:51:48 -0700883 if (*flags & FOLL_TRIED) {
Peter Xu4426e942020-04-01 21:08:49 -0700884 /*
885 * Note: FAULT_FLAG_ALLOW_RETRY and FAULT_FLAG_TRIED
886 * can co-exist
887 */
Andres Lagar-Cavilla234b2392014-09-17 10:51:48 -0700888 fault_flags |= FAULT_FLAG_TRIED;
889 }
Kirill A. Shutemov16744482014-06-04 16:08:12 -0700890
Peter Xubce617e2020-08-11 18:37:44 -0700891 ret = handle_mm_fault(vma, address, fault_flags, NULL);
Kirill A. Shutemov16744482014-06-04 16:08:12 -0700892 if (ret & VM_FAULT_ERROR) {
James Morse9a291a72017-06-02 14:46:46 -0700893 int err = vm_fault_to_errno(ret, *flags);
894
895 if (err)
896 return err;
Kirill A. Shutemov16744482014-06-04 16:08:12 -0700897 BUG();
898 }
899
Kirill A. Shutemov16744482014-06-04 16:08:12 -0700900 if (ret & VM_FAULT_RETRY) {
Peter Xu4f6da932020-04-01 21:07:58 -0700901 if (locked && !(fault_flags & FAULT_FLAG_RETRY_NOWAIT))
902 *locked = 0;
Kirill A. Shutemov16744482014-06-04 16:08:12 -0700903 return -EBUSY;
904 }
905
906 /*
907 * The VM_FAULT_WRITE bit tells us that do_wp_page has broken COW when
908 * necessary, even if maybe_mkwrite decided not to set pte_write. We
909 * can thus safely do subsequent page lookups as if they were reads.
910 * But only do so when looping for pte_write is futile: in some cases
911 * userspace may also be wanting to write to the gotten user page,
912 * which a read fault here might prevent (a readonly page might get
913 * reCOWed by userspace write).
914 */
915 if ((ret & VM_FAULT_WRITE) && !(vma->vm_flags & VM_WRITE))
Mario Leinweber29231172018-04-05 16:24:18 -0700916 *flags |= FOLL_COW;
Kirill A. Shutemov16744482014-06-04 16:08:12 -0700917 return 0;
918}
919
Kirill A. Shutemovfa5bb202014-06-04 16:08:13 -0700920static int check_vma_flags(struct vm_area_struct *vma, unsigned long gup_flags)
921{
922 vm_flags_t vm_flags = vma->vm_flags;
Dave Hansen1b2ee122016-02-12 13:02:21 -0800923 int write = (gup_flags & FOLL_WRITE);
924 int foreign = (gup_flags & FOLL_REMOTE);
Kirill A. Shutemovfa5bb202014-06-04 16:08:13 -0700925
926 if (vm_flags & (VM_IO | VM_PFNMAP))
927 return -EFAULT;
928
Willy Tarreau7f7ccc22018-05-11 08:11:44 +0200929 if (gup_flags & FOLL_ANON && !vma_is_anonymous(vma))
930 return -EFAULT;
931
Dave Hansen1b2ee122016-02-12 13:02:21 -0800932 if (write) {
Kirill A. Shutemovfa5bb202014-06-04 16:08:13 -0700933 if (!(vm_flags & VM_WRITE)) {
934 if (!(gup_flags & FOLL_FORCE))
935 return -EFAULT;
936 /*
937 * We used to let the write,force case do COW in a
938 * VM_MAYWRITE VM_SHARED !VM_WRITE vma, so ptrace could
939 * set a breakpoint in a read-only mapping of an
940 * executable, without corrupting the file (yet only
941 * when that file had been opened for writing!).
942 * Anon pages in shared mappings are surprising: now
943 * just reject it.
944 */
Hugh Dickins46435362016-01-30 18:03:16 -0800945 if (!is_cow_mapping(vm_flags))
Kirill A. Shutemovfa5bb202014-06-04 16:08:13 -0700946 return -EFAULT;
Kirill A. Shutemovfa5bb202014-06-04 16:08:13 -0700947 }
948 } else if (!(vm_flags & VM_READ)) {
949 if (!(gup_flags & FOLL_FORCE))
950 return -EFAULT;
951 /*
952 * Is there actually any vma we can reach here which does not
953 * have VM_MAYREAD set?
954 */
955 if (!(vm_flags & VM_MAYREAD))
956 return -EFAULT;
957 }
Dave Hansend61172b2016-02-12 13:02:24 -0800958 /*
959 * gups are always data accesses, not instruction
960 * fetches, so execute=false here
961 */
962 if (!arch_vma_access_permitted(vma, write, false, foreign))
Dave Hansen33a709b2016-02-12 13:02:19 -0800963 return -EFAULT;
Kirill A. Shutemovfa5bb202014-06-04 16:08:13 -0700964 return 0;
965}
966
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700967/**
968 * __get_user_pages() - pin user pages in memory
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700969 * @mm: mm_struct of target mm
970 * @start: starting user address
971 * @nr_pages: number of pages from start to pin
972 * @gup_flags: flags modifying pin behaviour
973 * @pages: array that receives pointers to the pages pinned.
974 * Should be at least nr_pages long. Or NULL, if caller
975 * only intends to ensure the pages are faulted in.
976 * @vmas: array of pointers to vmas corresponding to each page.
977 * Or NULL if the caller does not require them.
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -0700978 * @locked: whether we're still with the mmap_lock held
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700979 *
Liu Xiangd2dfbe42019-11-30 17:49:53 -0800980 * Returns either number of pages pinned (which may be less than the
981 * number requested), or an error. Details about the return value:
982 *
983 * -- If nr_pages is 0, returns 0.
984 * -- If nr_pages is >0, but no pages were pinned, returns -errno.
985 * -- If nr_pages is >0, and some pages were pinned, returns the number of
986 * pages pinned. Again, this may be less than nr_pages.
Michal Hocko2d3a36a2020-06-03 16:03:25 -0700987 * -- 0 return value is possible when the fault would need to be retried.
Liu Xiangd2dfbe42019-11-30 17:49:53 -0800988 *
989 * The caller is responsible for releasing returned @pages, via put_page().
990 *
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -0700991 * @vmas are valid only as long as mmap_lock is held.
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700992 *
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -0700993 * Must be called with mmap_lock held. It may be released. See below.
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700994 *
995 * __get_user_pages walks a process's page tables and takes a reference to
996 * each struct page that each user address corresponds to at a given
997 * instant. That is, it takes the page that would be accessed if a user
998 * thread accesses the given user virtual address at that instant.
999 *
1000 * This does not guarantee that the page exists in the user mappings when
1001 * __get_user_pages returns, and there may even be a completely different
1002 * page there in some cases (eg. if mmapped pagecache has been invalidated
1003 * and subsequently re faulted). However it does guarantee that the page
1004 * won't be freed completely. And mostly callers simply care that the page
1005 * contains data that was valid *at some point in time*. Typically, an IO
1006 * or similar operation cannot guarantee anything stronger anyway because
1007 * locks can't be held over the syscall boundary.
1008 *
1009 * If @gup_flags & FOLL_WRITE == 0, the page must not be written to. If
1010 * the page is written to, set_page_dirty (or set_page_dirty_lock, as
1011 * appropriate) must be called after the page is finished with, and
1012 * before put_page is called.
1013 *
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -07001014 * If @locked != NULL, *@locked will be set to 0 when mmap_lock is
Peter Xu4f6da932020-04-01 21:07:58 -07001015 * released by an up_read(). That can happen if @gup_flags does not
1016 * have FOLL_NOWAIT.
Paul Cassella9a95f3c2014-08-06 16:07:24 -07001017 *
Peter Xu4f6da932020-04-01 21:07:58 -07001018 * A caller using such a combination of @locked and @gup_flags
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -07001019 * must therefore hold the mmap_lock for reading only, and recognize
Paul Cassella9a95f3c2014-08-06 16:07:24 -07001020 * when it's been released. Otherwise, it must be held for either
1021 * reading or writing and will not be released.
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -07001022 *
1023 * In most cases, get_user_pages or get_user_pages_fast should be used
1024 * instead of __get_user_pages. __get_user_pages should be used only if
1025 * you need some special @gup_flags.
1026 */
Peter Xu64019a22020-08-11 18:39:01 -07001027static long __get_user_pages(struct mm_struct *mm,
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -07001028 unsigned long start, unsigned long nr_pages,
1029 unsigned int gup_flags, struct page **pages,
Peter Xu4f6da932020-04-01 21:07:58 -07001030 struct vm_area_struct **vmas, int *locked)
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -07001031{
Keith Buschdf06b372018-10-26 15:10:28 -07001032 long ret = 0, i = 0;
Kirill A. Shutemovfa5bb202014-06-04 16:08:13 -07001033 struct vm_area_struct *vma = NULL;
Keith Buschdf06b372018-10-26 15:10:28 -07001034 struct follow_page_context ctx = { NULL };
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -07001035
1036 if (!nr_pages)
1037 return 0;
1038
Andrey Konovalovf9652592019-09-25 16:48:34 -07001039 start = untagged_addr(start);
1040
John Hubbardeddb1c22020-01-30 22:12:54 -08001041 VM_BUG_ON(!!pages != !!(gup_flags & (FOLL_GET | FOLL_PIN)));
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -07001042
1043 /*
1044 * If FOLL_FORCE is set then do not force a full fault as the hinting
1045 * fault information is unrelated to the reference behaviour of a task
1046 * using the address space
1047 */
1048 if (!(gup_flags & FOLL_FORCE))
1049 gup_flags |= FOLL_NUMA;
1050
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -07001051 do {
Kirill A. Shutemovfa5bb202014-06-04 16:08:13 -07001052 struct page *page;
1053 unsigned int foll_flags = gup_flags;
1054 unsigned int page_increm;
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -07001055
Kirill A. Shutemovfa5bb202014-06-04 16:08:13 -07001056 /* first iteration or cross vma bound */
1057 if (!vma || start >= vma->vm_end) {
1058 vma = find_extend_vma(mm, start);
1059 if (!vma && in_gate_area(mm, start)) {
Kirill A. Shutemovfa5bb202014-06-04 16:08:13 -07001060 ret = get_gate_page(mm, start & PAGE_MASK,
1061 gup_flags, &vma,
1062 pages ? &pages[i] : NULL);
1063 if (ret)
John Hubbard08be37b2018-11-30 14:08:53 -08001064 goto out;
Keith Buschdf06b372018-10-26 15:10:28 -07001065 ctx.page_mask = 0;
Kirill A. Shutemovfa5bb202014-06-04 16:08:13 -07001066 goto next_page;
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -07001067 }
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -07001068
Keith Buschdf06b372018-10-26 15:10:28 -07001069 if (!vma || check_vma_flags(vma, gup_flags)) {
1070 ret = -EFAULT;
1071 goto out;
1072 }
Kirill A. Shutemovfa5bb202014-06-04 16:08:13 -07001073 if (is_vm_hugetlb_page(vma)) {
1074 i = follow_hugetlb_page(mm, vma, pages, vmas,
1075 &start, &nr_pages, i,
Peter Xua308c712020-08-21 19:49:57 -04001076 gup_flags, locked);
Peter Xuad415db2020-04-01 21:08:02 -07001077 if (locked && *locked == 0) {
1078 /*
1079 * We've got a VM_FAULT_RETRY
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -07001080 * and we've lost mmap_lock.
Peter Xuad415db2020-04-01 21:08:02 -07001081 * We must stop here.
1082 */
1083 BUG_ON(gup_flags & FOLL_NOWAIT);
1084 BUG_ON(ret != 0);
1085 goto out;
1086 }
Kirill A. Shutemovfa5bb202014-06-04 16:08:13 -07001087 continue;
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -07001088 }
Kirill A. Shutemovfa5bb202014-06-04 16:08:13 -07001089 }
1090retry:
1091 /*
1092 * If we have a pending SIGKILL, don't keep faulting pages and
1093 * potentially allocating memory.
1094 */
Davidlohr Buesofa45f112019-01-03 15:28:55 -08001095 if (fatal_signal_pending(current)) {
Michal Hockod180870d2020-04-20 18:13:55 -07001096 ret = -EINTR;
Keith Buschdf06b372018-10-26 15:10:28 -07001097 goto out;
1098 }
Kirill A. Shutemovfa5bb202014-06-04 16:08:13 -07001099 cond_resched();
Keith Buschdf06b372018-10-26 15:10:28 -07001100
1101 page = follow_page_mask(vma, start, foll_flags, &ctx);
Kirill A. Shutemovfa5bb202014-06-04 16:08:13 -07001102 if (!page) {
Peter Xu64019a22020-08-11 18:39:01 -07001103 ret = faultin_page(vma, start, &foll_flags, locked);
Kirill A. Shutemovfa5bb202014-06-04 16:08:13 -07001104 switch (ret) {
1105 case 0:
1106 goto retry;
Keith Buschdf06b372018-10-26 15:10:28 -07001107 case -EBUSY:
1108 ret = 0;
Joe Perchese4a9bc52020-04-06 20:08:39 -07001109 fallthrough;
Kirill A. Shutemovfa5bb202014-06-04 16:08:13 -07001110 case -EFAULT:
1111 case -ENOMEM:
1112 case -EHWPOISON:
Keith Buschdf06b372018-10-26 15:10:28 -07001113 goto out;
Kirill A. Shutemovfa5bb202014-06-04 16:08:13 -07001114 case -ENOENT:
1115 goto next_page;
1116 }
1117 BUG();
Kirill A. Shutemov1027e442015-09-04 15:47:55 -07001118 } else if (PTR_ERR(page) == -EEXIST) {
1119 /*
1120 * Proper page table entry exists, but no corresponding
1121 * struct page.
1122 */
1123 goto next_page;
1124 } else if (IS_ERR(page)) {
Keith Buschdf06b372018-10-26 15:10:28 -07001125 ret = PTR_ERR(page);
1126 goto out;
Kirill A. Shutemov1027e442015-09-04 15:47:55 -07001127 }
Kirill A. Shutemovfa5bb202014-06-04 16:08:13 -07001128 if (pages) {
1129 pages[i] = page;
1130 flush_anon_page(vma, page, start);
1131 flush_dcache_page(page);
Keith Buschdf06b372018-10-26 15:10:28 -07001132 ctx.page_mask = 0;
Kirill A. Shutemovfa5bb202014-06-04 16:08:13 -07001133 }
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -07001134next_page:
Kirill A. Shutemovfa5bb202014-06-04 16:08:13 -07001135 if (vmas) {
1136 vmas[i] = vma;
Keith Buschdf06b372018-10-26 15:10:28 -07001137 ctx.page_mask = 0;
Kirill A. Shutemovfa5bb202014-06-04 16:08:13 -07001138 }
Keith Buschdf06b372018-10-26 15:10:28 -07001139 page_increm = 1 + (~(start >> PAGE_SHIFT) & ctx.page_mask);
Kirill A. Shutemovfa5bb202014-06-04 16:08:13 -07001140 if (page_increm > nr_pages)
1141 page_increm = nr_pages;
1142 i += page_increm;
1143 start += page_increm * PAGE_SIZE;
1144 nr_pages -= page_increm;
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -07001145 } while (nr_pages);
Keith Buschdf06b372018-10-26 15:10:28 -07001146out:
1147 if (ctx.pgmap)
1148 put_dev_pagemap(ctx.pgmap);
1149 return i ? i : ret;
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -07001150}
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -07001151
Tobias Klauser771ab432016-12-12 16:41:53 -08001152static bool vma_permits_fault(struct vm_area_struct *vma,
1153 unsigned int fault_flags)
Dave Hansend4925e02016-02-12 13:02:16 -08001154{
Dave Hansen1b2ee122016-02-12 13:02:21 -08001155 bool write = !!(fault_flags & FAULT_FLAG_WRITE);
1156 bool foreign = !!(fault_flags & FAULT_FLAG_REMOTE);
Dave Hansen33a709b2016-02-12 13:02:19 -08001157 vm_flags_t vm_flags = write ? VM_WRITE : VM_READ;
Dave Hansend4925e02016-02-12 13:02:16 -08001158
1159 if (!(vm_flags & vma->vm_flags))
1160 return false;
1161
Dave Hansen33a709b2016-02-12 13:02:19 -08001162 /*
1163 * The architecture might have a hardware protection
Dave Hansen1b2ee122016-02-12 13:02:21 -08001164 * mechanism other than read/write that can deny access.
Dave Hansend61172b2016-02-12 13:02:24 -08001165 *
1166 * gup always represents data access, not instruction
1167 * fetches, so execute=false here:
Dave Hansen33a709b2016-02-12 13:02:19 -08001168 */
Dave Hansend61172b2016-02-12 13:02:24 -08001169 if (!arch_vma_access_permitted(vma, write, false, foreign))
Dave Hansen33a709b2016-02-12 13:02:19 -08001170 return false;
1171
Dave Hansend4925e02016-02-12 13:02:16 -08001172 return true;
1173}
1174
Souptick Joarderadc8cb42020-06-01 21:48:24 -07001175/**
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -07001176 * fixup_user_fault() - manually resolve a user page fault
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -07001177 * @mm: mm_struct of target mm
1178 * @address: user address
1179 * @fault_flags:flags to pass down to handle_mm_fault()
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -07001180 * @unlocked: did we unlock the mmap_lock while retrying, maybe NULL if caller
Miles Chen548b6a12020-06-01 21:48:33 -07001181 * does not allow retry. If NULL, the caller must guarantee
1182 * that fault_flags does not contain FAULT_FLAG_ALLOW_RETRY.
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -07001183 *
1184 * This is meant to be called in the specific scenario where for locking reasons
1185 * we try to access user memory in atomic context (within a pagefault_disable()
1186 * section), this returns -EFAULT, and we want to resolve the user fault before
1187 * trying again.
1188 *
1189 * Typically this is meant to be used by the futex code.
1190 *
1191 * The main difference with get_user_pages() is that this function will
1192 * unconditionally call handle_mm_fault() which will in turn perform all the
1193 * necessary SW fixup of the dirty and young bits in the PTE, while
Dominik Dingel4a9e1cd2016-01-15 16:57:04 -08001194 * get_user_pages() only guarantees to update these in the struct page.
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -07001195 *
1196 * This is important for some architectures where those bits also gate the
1197 * access permission to the page because they are maintained in software. On
1198 * such architectures, gup() will not be enough to make a subsequent access
1199 * succeed.
1200 *
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -07001201 * This function will not return with an unlocked mmap_lock. So it has not the
1202 * same semantics wrt the @mm->mmap_lock as does filemap_fault().
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -07001203 */
Peter Xu64019a22020-08-11 18:39:01 -07001204int fixup_user_fault(struct mm_struct *mm,
Dominik Dingel4a9e1cd2016-01-15 16:57:04 -08001205 unsigned long address, unsigned int fault_flags,
1206 bool *unlocked)
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -07001207{
1208 struct vm_area_struct *vma;
Souptick Joarder2b740302018-08-23 17:01:36 -07001209 vm_fault_t ret, major = 0;
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -07001210
Andrey Konovalovf9652592019-09-25 16:48:34 -07001211 address = untagged_addr(address);
1212
Dominik Dingel4a9e1cd2016-01-15 16:57:04 -08001213 if (unlocked)
Peter Xu71335f32020-04-01 21:08:53 -07001214 fault_flags |= FAULT_FLAG_ALLOW_RETRY | FAULT_FLAG_KILLABLE;
Dominik Dingel4a9e1cd2016-01-15 16:57:04 -08001215
1216retry:
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -07001217 vma = find_extend_vma(mm, address);
1218 if (!vma || address < vma->vm_start)
1219 return -EFAULT;
1220
Dave Hansend4925e02016-02-12 13:02:16 -08001221 if (!vma_permits_fault(vma, fault_flags))
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -07001222 return -EFAULT;
1223
Peter Xu475f4dfc2020-05-13 17:50:41 -07001224 if ((fault_flags & FAULT_FLAG_KILLABLE) &&
1225 fatal_signal_pending(current))
1226 return -EINTR;
1227
Peter Xubce617e2020-08-11 18:37:44 -07001228 ret = handle_mm_fault(vma, address, fault_flags, NULL);
Dominik Dingel4a9e1cd2016-01-15 16:57:04 -08001229 major |= ret & VM_FAULT_MAJOR;
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -07001230 if (ret & VM_FAULT_ERROR) {
James Morse9a291a72017-06-02 14:46:46 -07001231 int err = vm_fault_to_errno(ret, 0);
1232
1233 if (err)
1234 return err;
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -07001235 BUG();
1236 }
Dominik Dingel4a9e1cd2016-01-15 16:57:04 -08001237
1238 if (ret & VM_FAULT_RETRY) {
Michel Lespinassed8ed45c2020-06-08 21:33:25 -07001239 mmap_read_lock(mm);
Peter Xu475f4dfc2020-05-13 17:50:41 -07001240 *unlocked = true;
1241 fault_flags |= FAULT_FLAG_TRIED;
1242 goto retry;
Dominik Dingel4a9e1cd2016-01-15 16:57:04 -08001243 }
1244
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -07001245 return 0;
1246}
Paolo Bonziniadd6a0c2016-06-07 17:51:18 +02001247EXPORT_SYMBOL_GPL(fixup_user_fault);
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -07001248
Michal Hocko2d3a36a2020-06-03 16:03:25 -07001249/*
1250 * Please note that this function, unlike __get_user_pages will not
1251 * return 0 for nr_pages > 0 without FOLL_NOWAIT
1252 */
Peter Xu64019a22020-08-11 18:39:01 -07001253static __always_inline long __get_user_pages_locked(struct mm_struct *mm,
Andrea Arcangelif0818f42015-02-11 15:27:17 -08001254 unsigned long start,
1255 unsigned long nr_pages,
Andrea Arcangelif0818f42015-02-11 15:27:17 -08001256 struct page **pages,
1257 struct vm_area_struct **vmas,
Al Viroe7167122017-11-19 11:32:05 -05001258 int *locked,
Andrea Arcangeli0fd71a52015-02-11 15:27:20 -08001259 unsigned int flags)
Andrea Arcangelif0818f42015-02-11 15:27:17 -08001260{
Andrea Arcangelif0818f42015-02-11 15:27:17 -08001261 long ret, pages_done;
1262 bool lock_dropped;
1263
1264 if (locked) {
1265 /* if VM_FAULT_RETRY can be returned, vmas become invalid */
1266 BUG_ON(vmas);
1267 /* check caller initialized locked */
1268 BUG_ON(*locked != 1);
1269 }
1270
Peter Xu008cfe42020-09-25 18:25:57 -04001271 if (flags & FOLL_PIN)
Jason A. Donenfelda4d63c32020-09-28 12:35:07 +02001272 atomic_set(&mm->has_pinned, 1);
Peter Xu008cfe42020-09-25 18:25:57 -04001273
John Hubbardeddb1c22020-01-30 22:12:54 -08001274 /*
1275 * FOLL_PIN and FOLL_GET are mutually exclusive. Traditional behavior
1276 * is to set FOLL_GET if the caller wants pages[] filled in (but has
1277 * carelessly failed to specify FOLL_GET), so keep doing that, but only
1278 * for FOLL_GET, not for the newer FOLL_PIN.
1279 *
1280 * FOLL_PIN always expects pages to be non-null, but no need to assert
1281 * that here, as any failures will be obvious enough.
1282 */
1283 if (pages && !(flags & FOLL_PIN))
Andrea Arcangelif0818f42015-02-11 15:27:17 -08001284 flags |= FOLL_GET;
Andrea Arcangelif0818f42015-02-11 15:27:17 -08001285
1286 pages_done = 0;
1287 lock_dropped = false;
1288 for (;;) {
Peter Xu64019a22020-08-11 18:39:01 -07001289 ret = __get_user_pages(mm, start, nr_pages, flags, pages,
Andrea Arcangelif0818f42015-02-11 15:27:17 -08001290 vmas, locked);
1291 if (!locked)
1292 /* VM_FAULT_RETRY couldn't trigger, bypass */
1293 return ret;
1294
1295 /* VM_FAULT_RETRY cannot return errors */
1296 if (!*locked) {
1297 BUG_ON(ret < 0);
1298 BUG_ON(ret >= nr_pages);
1299 }
1300
Andrea Arcangelif0818f42015-02-11 15:27:17 -08001301 if (ret > 0) {
1302 nr_pages -= ret;
1303 pages_done += ret;
1304 if (!nr_pages)
1305 break;
1306 }
1307 if (*locked) {
Andrea Arcangeli96312e62018-03-09 15:51:06 -08001308 /*
1309 * VM_FAULT_RETRY didn't trigger or it was a
1310 * FOLL_NOWAIT.
1311 */
Andrea Arcangelif0818f42015-02-11 15:27:17 -08001312 if (!pages_done)
1313 pages_done = ret;
1314 break;
1315 }
Mike Rapoportdf172772019-05-31 22:30:33 -07001316 /*
1317 * VM_FAULT_RETRY triggered, so seek to the faulting offset.
1318 * For the prefault case (!pages) we only update counts.
1319 */
1320 if (likely(pages))
1321 pages += ret;
Andrea Arcangelif0818f42015-02-11 15:27:17 -08001322 start += ret << PAGE_SHIFT;
Peter Xu4426e942020-04-01 21:08:49 -07001323 lock_dropped = true;
Andrea Arcangelif0818f42015-02-11 15:27:17 -08001324
Peter Xu4426e942020-04-01 21:08:49 -07001325retry:
Andrea Arcangelif0818f42015-02-11 15:27:17 -08001326 /*
1327 * Repeat on the address that fired VM_FAULT_RETRY
Peter Xu4426e942020-04-01 21:08:49 -07001328 * with both FAULT_FLAG_ALLOW_RETRY and
1329 * FAULT_FLAG_TRIED. Note that GUP can be interrupted
1330 * by fatal signals, so we need to check it before we
1331 * start trying again otherwise it can loop forever.
Andrea Arcangelif0818f42015-02-11 15:27:17 -08001332 */
Peter Xu4426e942020-04-01 21:08:49 -07001333
Hillf Dantonae46d2a2020-04-08 11:59:24 -04001334 if (fatal_signal_pending(current)) {
1335 if (!pages_done)
1336 pages_done = -EINTR;
Peter Xu4426e942020-04-01 21:08:49 -07001337 break;
Hillf Dantonae46d2a2020-04-08 11:59:24 -04001338 }
Peter Xu4426e942020-04-01 21:08:49 -07001339
Michel Lespinassed8ed45c2020-06-08 21:33:25 -07001340 ret = mmap_read_lock_killable(mm);
Peter Xu71335f32020-04-01 21:08:53 -07001341 if (ret) {
1342 BUG_ON(ret > 0);
1343 if (!pages_done)
1344 pages_done = ret;
1345 break;
1346 }
Peter Xu4426e942020-04-01 21:08:49 -07001347
Peter Xuc7b6a562020-04-07 21:40:10 -04001348 *locked = 1;
Peter Xu64019a22020-08-11 18:39:01 -07001349 ret = __get_user_pages(mm, start, 1, flags | FOLL_TRIED,
Peter Xu4426e942020-04-01 21:08:49 -07001350 pages, NULL, locked);
1351 if (!*locked) {
1352 /* Continue to retry until we succeeded */
1353 BUG_ON(ret != 0);
1354 goto retry;
1355 }
Andrea Arcangelif0818f42015-02-11 15:27:17 -08001356 if (ret != 1) {
1357 BUG_ON(ret > 1);
1358 if (!pages_done)
1359 pages_done = ret;
1360 break;
1361 }
1362 nr_pages--;
1363 pages_done++;
1364 if (!nr_pages)
1365 break;
Mike Rapoportdf172772019-05-31 22:30:33 -07001366 if (likely(pages))
1367 pages++;
Andrea Arcangelif0818f42015-02-11 15:27:17 -08001368 start += PAGE_SIZE;
1369 }
Al Viroe7167122017-11-19 11:32:05 -05001370 if (lock_dropped && *locked) {
Andrea Arcangelif0818f42015-02-11 15:27:17 -08001371 /*
1372 * We must let the caller know we temporarily dropped the lock
1373 * and so the critical section protected by it was lost.
1374 */
Michel Lespinassed8ed45c2020-06-08 21:33:25 -07001375 mmap_read_unlock(mm);
Andrea Arcangelif0818f42015-02-11 15:27:17 -08001376 *locked = 0;
1377 }
1378 return pages_done;
1379}
1380
Christoph Hellwigd3649f62019-07-11 20:57:18 -07001381/**
1382 * populate_vma_page_range() - populate a range of pages in the vma.
1383 * @vma: target vma
1384 * @start: start address
1385 * @end: end address
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -07001386 * @locked: whether the mmap_lock is still held
Christoph Hellwigd3649f62019-07-11 20:57:18 -07001387 *
1388 * This takes care of mlocking the pages too if VM_LOCKED is set.
1389 *
Tang Yizhou0a36f7f2020-08-06 23:20:01 -07001390 * Return either number of pages pinned in the vma, or a negative error
1391 * code on error.
Christoph Hellwigd3649f62019-07-11 20:57:18 -07001392 *
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -07001393 * vma->vm_mm->mmap_lock must be held.
Christoph Hellwigd3649f62019-07-11 20:57:18 -07001394 *
Peter Xu4f6da932020-04-01 21:07:58 -07001395 * If @locked is NULL, it may be held for read or write and will
Christoph Hellwigd3649f62019-07-11 20:57:18 -07001396 * be unperturbed.
1397 *
Peter Xu4f6da932020-04-01 21:07:58 -07001398 * If @locked is non-NULL, it must held for read only and may be
1399 * released. If it's released, *@locked will be set to 0.
Christoph Hellwigd3649f62019-07-11 20:57:18 -07001400 */
1401long populate_vma_page_range(struct vm_area_struct *vma,
Peter Xu4f6da932020-04-01 21:07:58 -07001402 unsigned long start, unsigned long end, int *locked)
Christoph Hellwigd3649f62019-07-11 20:57:18 -07001403{
1404 struct mm_struct *mm = vma->vm_mm;
1405 unsigned long nr_pages = (end - start) / PAGE_SIZE;
1406 int gup_flags;
1407
1408 VM_BUG_ON(start & ~PAGE_MASK);
1409 VM_BUG_ON(end & ~PAGE_MASK);
1410 VM_BUG_ON_VMA(start < vma->vm_start, vma);
1411 VM_BUG_ON_VMA(end > vma->vm_end, vma);
Michel Lespinasse42fc5412020-06-08 21:33:44 -07001412 mmap_assert_locked(mm);
Christoph Hellwigd3649f62019-07-11 20:57:18 -07001413
1414 gup_flags = FOLL_TOUCH | FOLL_POPULATE | FOLL_MLOCK;
1415 if (vma->vm_flags & VM_LOCKONFAULT)
1416 gup_flags &= ~FOLL_POPULATE;
1417 /*
1418 * We want to touch writable mappings with a write fault in order
1419 * to break COW, except for shared mappings because these don't COW
1420 * and we would not want to dirty them for nothing.
1421 */
1422 if ((vma->vm_flags & (VM_WRITE | VM_SHARED)) == VM_WRITE)
1423 gup_flags |= FOLL_WRITE;
1424
1425 /*
1426 * We want mlock to succeed for regions that have any permissions
1427 * other than PROT_NONE.
1428 */
Anshuman Khandual3122e802020-04-06 20:03:47 -07001429 if (vma_is_accessible(vma))
Christoph Hellwigd3649f62019-07-11 20:57:18 -07001430 gup_flags |= FOLL_FORCE;
1431
1432 /*
1433 * We made sure addr is within a VMA, so the following will
1434 * not result in a stack expansion that recurses back here.
1435 */
Peter Xu64019a22020-08-11 18:39:01 -07001436 return __get_user_pages(mm, start, nr_pages, gup_flags,
Peter Xu4f6da932020-04-01 21:07:58 -07001437 NULL, NULL, locked);
Christoph Hellwigd3649f62019-07-11 20:57:18 -07001438}
1439
1440/*
1441 * __mm_populate - populate and/or mlock pages within a range of address space.
1442 *
1443 * This is used to implement mlock() and the MAP_POPULATE / MAP_LOCKED mmap
1444 * flags. VMAs must be already marked with the desired vm_flags, and
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -07001445 * mmap_lock must not be held.
Christoph Hellwigd3649f62019-07-11 20:57:18 -07001446 */
1447int __mm_populate(unsigned long start, unsigned long len, int ignore_errors)
1448{
1449 struct mm_struct *mm = current->mm;
1450 unsigned long end, nstart, nend;
1451 struct vm_area_struct *vma = NULL;
1452 int locked = 0;
1453 long ret = 0;
1454
1455 end = start + len;
1456
1457 for (nstart = start; nstart < end; nstart = nend) {
1458 /*
1459 * We want to fault in pages for [nstart; end) address range.
1460 * Find first corresponding VMA.
1461 */
1462 if (!locked) {
1463 locked = 1;
Michel Lespinassed8ed45c2020-06-08 21:33:25 -07001464 mmap_read_lock(mm);
Christoph Hellwigd3649f62019-07-11 20:57:18 -07001465 vma = find_vma(mm, nstart);
1466 } else if (nstart >= vma->vm_end)
1467 vma = vma->vm_next;
1468 if (!vma || vma->vm_start >= end)
1469 break;
1470 /*
1471 * Set [nstart; nend) to intersection of desired address
1472 * range with the first VMA. Also, skip undesirable VMA types.
1473 */
1474 nend = min(end, vma->vm_end);
1475 if (vma->vm_flags & (VM_IO | VM_PFNMAP))
1476 continue;
1477 if (nstart < vma->vm_start)
1478 nstart = vma->vm_start;
1479 /*
1480 * Now fault in a range of pages. populate_vma_page_range()
1481 * double checks the vma flags, so that it won't mlock pages
1482 * if the vma was already munlocked.
1483 */
1484 ret = populate_vma_page_range(vma, nstart, nend, &locked);
1485 if (ret < 0) {
1486 if (ignore_errors) {
1487 ret = 0;
1488 continue; /* continue at next VMA */
1489 }
1490 break;
1491 }
1492 nend = nstart + ret * PAGE_SIZE;
1493 ret = 0;
1494 }
1495 if (locked)
Michel Lespinassed8ed45c2020-06-08 21:33:25 -07001496 mmap_read_unlock(mm);
Christoph Hellwigd3649f62019-07-11 20:57:18 -07001497 return ret; /* 0 or negative error code */
1498}
Christoph Hellwig050a9ad2019-07-11 20:57:21 -07001499#else /* CONFIG_MMU */
Peter Xu64019a22020-08-11 18:39:01 -07001500static long __get_user_pages_locked(struct mm_struct *mm, unsigned long start,
Christoph Hellwig050a9ad2019-07-11 20:57:21 -07001501 unsigned long nr_pages, struct page **pages,
1502 struct vm_area_struct **vmas, int *locked,
1503 unsigned int foll_flags)
1504{
1505 struct vm_area_struct *vma;
1506 unsigned long vm_flags;
1507 int i;
1508
1509 /* calculate required read or write permissions.
1510 * If FOLL_FORCE is set, we only require the "MAY" flags.
1511 */
1512 vm_flags = (foll_flags & FOLL_WRITE) ?
1513 (VM_WRITE | VM_MAYWRITE) : (VM_READ | VM_MAYREAD);
1514 vm_flags &= (foll_flags & FOLL_FORCE) ?
1515 (VM_MAYREAD | VM_MAYWRITE) : (VM_READ | VM_WRITE);
1516
1517 for (i = 0; i < nr_pages; i++) {
1518 vma = find_vma(mm, start);
1519 if (!vma)
1520 goto finish_or_fault;
1521
1522 /* protect what we can, including chardevs */
1523 if ((vma->vm_flags & (VM_IO | VM_PFNMAP)) ||
1524 !(vm_flags & vma->vm_flags))
1525 goto finish_or_fault;
1526
1527 if (pages) {
1528 pages[i] = virt_to_page(start);
1529 if (pages[i])
1530 get_page(pages[i]);
1531 }
1532 if (vmas)
1533 vmas[i] = vma;
1534 start = (start + PAGE_SIZE) & PAGE_MASK;
1535 }
1536
1537 return i;
1538
1539finish_or_fault:
1540 return i ? : -EFAULT;
1541}
1542#endif /* !CONFIG_MMU */
Christoph Hellwigd3649f62019-07-11 20:57:18 -07001543
Jann Horn8f942ee2020-10-15 20:12:40 -07001544/**
1545 * get_dump_page() - pin user page in memory while writing it to core dump
1546 * @addr: user address
1547 *
1548 * Returns struct page pointer of user page pinned for dump,
1549 * to be freed afterwards by put_page().
1550 *
1551 * Returns NULL on any kind of failure - a hole must then be inserted into
1552 * the corefile, to preserve alignment with its headers; and also returns
1553 * NULL wherever the ZERO_PAGE, or an anonymous pte_none, has been found -
1554 * allowing a hole to be left in the corefile to save diskspace.
1555 *
Jann Horn7f3bfab2020-10-15 20:12:57 -07001556 * Called without mmap_lock (takes and releases the mmap_lock by itself).
Jann Horn8f942ee2020-10-15 20:12:40 -07001557 */
1558#ifdef CONFIG_ELF_CORE
1559struct page *get_dump_page(unsigned long addr)
1560{
Jann Horn7f3bfab2020-10-15 20:12:57 -07001561 struct mm_struct *mm = current->mm;
Jann Horn8f942ee2020-10-15 20:12:40 -07001562 struct page *page;
Jann Horn7f3bfab2020-10-15 20:12:57 -07001563 int locked = 1;
1564 int ret;
Jann Horn8f942ee2020-10-15 20:12:40 -07001565
Jann Horn7f3bfab2020-10-15 20:12:57 -07001566 if (mmap_read_lock_killable(mm))
Jann Horn8f942ee2020-10-15 20:12:40 -07001567 return NULL;
Jann Horn7f3bfab2020-10-15 20:12:57 -07001568 ret = __get_user_pages_locked(mm, addr, 1, &page, NULL, &locked,
1569 FOLL_FORCE | FOLL_DUMP | FOLL_GET);
1570 if (locked)
1571 mmap_read_unlock(mm);
1572 return (ret == 1) ? page : NULL;
Jann Horn8f942ee2020-10-15 20:12:40 -07001573}
1574#endif /* CONFIG_ELF_CORE */
1575
Aneesh Kumar K.V9a4e9f32019-03-05 15:47:44 -08001576#if defined(CONFIG_FS_DAX) || defined (CONFIG_CMA)
Aneesh Kumar K.V9a4e9f32019-03-05 15:47:44 -08001577static bool check_dax_vmas(struct vm_area_struct **vmas, long nr_pages)
1578{
1579 long i;
1580 struct vm_area_struct *vma_prev = NULL;
1581
1582 for (i = 0; i < nr_pages; i++) {
1583 struct vm_area_struct *vma = vmas[i];
1584
1585 if (vma == vma_prev)
1586 continue;
1587
1588 vma_prev = vma;
1589
1590 if (vma_is_fsdax(vma))
1591 return true;
1592 }
1593 return false;
1594}
Aneesh Kumar K.V9a4e9f32019-03-05 15:47:44 -08001595
1596#ifdef CONFIG_CMA
Peter Xu64019a22020-08-11 18:39:01 -07001597static long check_and_migrate_cma_pages(struct mm_struct *mm,
Ira Weiny932f4a62019-05-13 17:17:03 -07001598 unsigned long start,
1599 unsigned long nr_pages,
Aneesh Kumar K.V9a4e9f32019-03-05 15:47:44 -08001600 struct page **pages,
Ira Weiny932f4a62019-05-13 17:17:03 -07001601 struct vm_area_struct **vmas,
1602 unsigned int gup_flags)
Aneesh Kumar K.V9a4e9f32019-03-05 15:47:44 -08001603{
Pavel Tatashin673422b2021-05-04 18:38:49 -07001604 unsigned long i, isolation_error_count;
1605 bool drain_allow;
Aneesh Kumar K.V9a4e9f32019-03-05 15:47:44 -08001606 LIST_HEAD(cma_page_list);
zhong jiangb96cc652019-11-30 17:49:50 -08001607 long ret = nr_pages;
Pavel Tatashin7df511e2021-05-04 18:38:42 -07001608 struct page *prev_head, *head;
Joonsoo Kimed03d922020-08-11 18:37:41 -07001609 struct migration_target_control mtc = {
1610 .nid = NUMA_NO_NODE,
1611 .gfp_mask = GFP_USER | __GFP_MOVABLE | __GFP_NOWARN,
1612 };
Aneesh Kumar K.V9a4e9f32019-03-05 15:47:44 -08001613
1614check_again:
Pavel Tatashin7df511e2021-05-04 18:38:42 -07001615 prev_head = NULL;
Pavel Tatashin673422b2021-05-04 18:38:49 -07001616 isolation_error_count = 0;
1617 drain_allow = true;
Pavel Tatashin7df511e2021-05-04 18:38:42 -07001618 for (i = 0; i < nr_pages; i++) {
1619 head = compound_head(pages[i]);
1620 if (head == prev_head)
1621 continue;
1622 prev_head = head;
Aneesh Kumar K.V9a4e9f32019-03-05 15:47:44 -08001623 /*
1624 * If we get a page from the CMA zone, since we are going to
1625 * be pinning these entries, we might as well move them out
1626 * of the CMA zone if possible.
1627 */
Pingfan Liuaa712392019-07-11 20:57:39 -07001628 if (is_migrate_cma_page(head)) {
Pavel Tatashin673422b2021-05-04 18:38:49 -07001629 if (PageHuge(head)) {
1630 if (!isolate_huge_page(head, &cma_page_list))
1631 isolation_error_count++;
1632 } else {
Aneesh Kumar K.V9a4e9f32019-03-05 15:47:44 -08001633 if (!PageLRU(head) && drain_allow) {
1634 lru_add_drain_all();
1635 drain_allow = false;
1636 }
1637
Pavel Tatashin673422b2021-05-04 18:38:49 -07001638 if (isolate_lru_page(head)) {
1639 isolation_error_count++;
1640 continue;
Aneesh Kumar K.V9a4e9f32019-03-05 15:47:44 -08001641 }
Pavel Tatashin673422b2021-05-04 18:38:49 -07001642 list_add_tail(&head->lru, &cma_page_list);
1643 mod_node_page_state(page_pgdat(head),
1644 NR_ISOLATED_ANON +
1645 page_is_file_lru(head),
1646 thp_nr_pages(head));
Aneesh Kumar K.V9a4e9f32019-03-05 15:47:44 -08001647 }
1648 }
1649 }
1650
Pavel Tatashin673422b2021-05-04 18:38:49 -07001651 /*
1652 * If list is empty, and no isolation errors, means that all pages are
1653 * in the correct zone.
1654 */
1655 if (list_empty(&cma_page_list) && !isolation_error_count)
1656 return ret;
1657
Aneesh Kumar K.V9a4e9f32019-03-05 15:47:44 -08001658 if (!list_empty(&cma_page_list)) {
1659 /*
1660 * drop the above get_user_pages reference.
1661 */
Jason Gunthorpe96e1fac2020-11-13 22:51:56 -08001662 if (gup_flags & FOLL_PIN)
1663 unpin_user_pages(pages, nr_pages);
1664 else
1665 for (i = 0; i < nr_pages; i++)
1666 put_page(pages[i]);
Aneesh Kumar K.V9a4e9f32019-03-05 15:47:44 -08001667
Pavel Tatashin096c9482021-05-04 18:38:46 -07001668 ret = migrate_pages(&cma_page_list, alloc_migration_target,
1669 NULL, (unsigned long)&mtc, MIGRATE_SYNC,
1670 MR_CONTIG_RANGE);
1671 if (ret) {
Aneesh Kumar K.V9a4e9f32019-03-05 15:47:44 -08001672 if (!list_empty(&cma_page_list))
1673 putback_movable_pages(&cma_page_list);
Pavel Tatashin096c9482021-05-04 18:38:46 -07001674 return ret > 0 ? -ENOMEM : ret;
Aneesh Kumar K.V9a4e9f32019-03-05 15:47:44 -08001675 }
Pavel Tatashin096c9482021-05-04 18:38:46 -07001676
Pavel Tatashin673422b2021-05-04 18:38:49 -07001677 /* We unpinned pages before migration, pin them again */
1678 ret = __get_user_pages_locked(mm, start, nr_pages, pages, vmas,
1679 NULL, gup_flags);
1680 if (ret <= 0)
1681 return ret;
1682 nr_pages = ret;
Aneesh Kumar K.V9a4e9f32019-03-05 15:47:44 -08001683 }
1684
Pavel Tatashin673422b2021-05-04 18:38:49 -07001685 /*
1686 * check again because pages were unpinned, and we also might have
1687 * had isolation errors and need more pages to migrate.
1688 */
1689 goto check_again;
Aneesh Kumar K.V9a4e9f32019-03-05 15:47:44 -08001690}
1691#else
Peter Xu64019a22020-08-11 18:39:01 -07001692static long check_and_migrate_cma_pages(struct mm_struct *mm,
Ira Weiny932f4a62019-05-13 17:17:03 -07001693 unsigned long start,
1694 unsigned long nr_pages,
1695 struct page **pages,
1696 struct vm_area_struct **vmas,
1697 unsigned int gup_flags)
Aneesh Kumar K.V9a4e9f32019-03-05 15:47:44 -08001698{
1699 return nr_pages;
1700}
Christoph Hellwig050a9ad2019-07-11 20:57:21 -07001701#endif /* CONFIG_CMA */
Aneesh Kumar K.V9a4e9f32019-03-05 15:47:44 -08001702
Dan Williams2bb6d282017-11-29 16:10:35 -08001703/*
Ira Weiny932f4a62019-05-13 17:17:03 -07001704 * __gup_longterm_locked() is a wrapper for __get_user_pages_locked which
1705 * allows us to process the FOLL_LONGTERM flag.
Dan Williams2bb6d282017-11-29 16:10:35 -08001706 */
Peter Xu64019a22020-08-11 18:39:01 -07001707static long __gup_longterm_locked(struct mm_struct *mm,
Ira Weiny932f4a62019-05-13 17:17:03 -07001708 unsigned long start,
1709 unsigned long nr_pages,
1710 struct page **pages,
1711 struct vm_area_struct **vmas,
1712 unsigned int gup_flags)
Dan Williams2bb6d282017-11-29 16:10:35 -08001713{
Ira Weiny932f4a62019-05-13 17:17:03 -07001714 struct vm_area_struct **vmas_tmp = vmas;
1715 unsigned long flags = 0;
Dan Williams2bb6d282017-11-29 16:10:35 -08001716 long rc, i;
1717
Ira Weiny932f4a62019-05-13 17:17:03 -07001718 if (gup_flags & FOLL_LONGTERM) {
1719 if (!pages)
1720 return -EINVAL;
Dan Williams2bb6d282017-11-29 16:10:35 -08001721
Ira Weiny932f4a62019-05-13 17:17:03 -07001722 if (!vmas_tmp) {
1723 vmas_tmp = kcalloc(nr_pages,
1724 sizeof(struct vm_area_struct *),
1725 GFP_KERNEL);
1726 if (!vmas_tmp)
1727 return -ENOMEM;
1728 }
1729 flags = memalloc_nocma_save();
Dan Williams2bb6d282017-11-29 16:10:35 -08001730 }
1731
Peter Xu64019a22020-08-11 18:39:01 -07001732 rc = __get_user_pages_locked(mm, start, nr_pages, pages,
Ira Weiny932f4a62019-05-13 17:17:03 -07001733 vmas_tmp, NULL, gup_flags);
Dan Williams2bb6d282017-11-29 16:10:35 -08001734
Ira Weiny932f4a62019-05-13 17:17:03 -07001735 if (gup_flags & FOLL_LONGTERM) {
Ira Weiny932f4a62019-05-13 17:17:03 -07001736 if (rc < 0)
1737 goto out;
1738
1739 if (check_dax_vmas(vmas_tmp, rc)) {
Jason Gunthorpe96e1fac2020-11-13 22:51:56 -08001740 if (gup_flags & FOLL_PIN)
1741 unpin_user_pages(pages, rc);
1742 else
1743 for (i = 0; i < rc; i++)
1744 put_page(pages[i]);
Ira Weiny932f4a62019-05-13 17:17:03 -07001745 rc = -EOPNOTSUPP;
1746 goto out;
1747 }
1748
Peter Xu64019a22020-08-11 18:39:01 -07001749 rc = check_and_migrate_cma_pages(mm, start, rc, pages,
Ira Weiny932f4a62019-05-13 17:17:03 -07001750 vmas_tmp, gup_flags);
Joonsoo Kim41b4dc12020-08-11 18:37:34 -07001751out:
1752 memalloc_nocma_restore(flags);
Aneesh Kumar K.V9a4e9f32019-03-05 15:47:44 -08001753 }
1754
Ira Weiny932f4a62019-05-13 17:17:03 -07001755 if (vmas_tmp != vmas)
1756 kfree(vmas_tmp);
Dan Williams2bb6d282017-11-29 16:10:35 -08001757 return rc;
1758}
Ira Weiny932f4a62019-05-13 17:17:03 -07001759#else /* !CONFIG_FS_DAX && !CONFIG_CMA */
Peter Xu64019a22020-08-11 18:39:01 -07001760static __always_inline long __gup_longterm_locked(struct mm_struct *mm,
Ira Weiny932f4a62019-05-13 17:17:03 -07001761 unsigned long start,
1762 unsigned long nr_pages,
1763 struct page **pages,
1764 struct vm_area_struct **vmas,
1765 unsigned int flags)
1766{
Peter Xu64019a22020-08-11 18:39:01 -07001767 return __get_user_pages_locked(mm, start, nr_pages, pages, vmas,
Ira Weiny932f4a62019-05-13 17:17:03 -07001768 NULL, flags);
1769}
1770#endif /* CONFIG_FS_DAX || CONFIG_CMA */
1771
Barry Song447f3e42020-10-13 16:51:58 -07001772static bool is_valid_gup_flags(unsigned int gup_flags)
1773{
1774 /*
1775 * FOLL_PIN must only be set internally by the pin_user_pages*() APIs,
1776 * never directly by the caller, so enforce that with an assertion:
1777 */
1778 if (WARN_ON_ONCE(gup_flags & FOLL_PIN))
1779 return false;
1780 /*
1781 * FOLL_PIN is a prerequisite to FOLL_LONGTERM. Another way of saying
1782 * that is, FOLL_LONGTERM is a specific case, more restrictive case of
1783 * FOLL_PIN.
1784 */
1785 if (WARN_ON_ONCE(gup_flags & FOLL_LONGTERM))
1786 return false;
1787
1788 return true;
1789}
1790
John Hubbard22bf29b2020-04-01 21:05:10 -07001791#ifdef CONFIG_MMU
Peter Xu64019a22020-08-11 18:39:01 -07001792static long __get_user_pages_remote(struct mm_struct *mm,
John Hubbard22bf29b2020-04-01 21:05:10 -07001793 unsigned long start, unsigned long nr_pages,
1794 unsigned int gup_flags, struct page **pages,
1795 struct vm_area_struct **vmas, int *locked)
1796{
1797 /*
1798 * Parts of FOLL_LONGTERM behavior are incompatible with
1799 * FAULT_FLAG_ALLOW_RETRY because of the FS DAX check requirement on
1800 * vmas. However, this only comes up if locked is set, and there are
1801 * callers that do request FOLL_LONGTERM, but do not set locked. So,
1802 * allow what we can.
1803 */
1804 if (gup_flags & FOLL_LONGTERM) {
1805 if (WARN_ON_ONCE(locked))
1806 return -EINVAL;
1807 /*
1808 * This will check the vmas (even if our vmas arg is NULL)
1809 * and return -ENOTSUPP if DAX isn't allowed in this case:
1810 */
Peter Xu64019a22020-08-11 18:39:01 -07001811 return __gup_longterm_locked(mm, start, nr_pages, pages,
John Hubbard22bf29b2020-04-01 21:05:10 -07001812 vmas, gup_flags | FOLL_TOUCH |
1813 FOLL_REMOTE);
1814 }
1815
Peter Xu64019a22020-08-11 18:39:01 -07001816 return __get_user_pages_locked(mm, start, nr_pages, pages, vmas,
John Hubbard22bf29b2020-04-01 21:05:10 -07001817 locked,
1818 gup_flags | FOLL_TOUCH | FOLL_REMOTE);
1819}
1820
Souptick Joarderadc8cb42020-06-01 21:48:24 -07001821/**
John Hubbardc4237f82020-01-30 22:12:36 -08001822 * get_user_pages_remote() - pin user pages in memory
John Hubbardc4237f82020-01-30 22:12:36 -08001823 * @mm: mm_struct of target mm
1824 * @start: starting user address
1825 * @nr_pages: number of pages from start to pin
1826 * @gup_flags: flags modifying lookup behaviour
1827 * @pages: array that receives pointers to the pages pinned.
1828 * Should be at least nr_pages long. Or NULL, if caller
1829 * only intends to ensure the pages are faulted in.
1830 * @vmas: array of pointers to vmas corresponding to each page.
1831 * Or NULL if the caller does not require them.
1832 * @locked: pointer to lock flag indicating whether lock is held and
1833 * subsequently whether VM_FAULT_RETRY functionality can be
1834 * utilised. Lock must initially be held.
1835 *
1836 * Returns either number of pages pinned (which may be less than the
1837 * number requested), or an error. Details about the return value:
1838 *
1839 * -- If nr_pages is 0, returns 0.
1840 * -- If nr_pages is >0, but no pages were pinned, returns -errno.
1841 * -- If nr_pages is >0, and some pages were pinned, returns the number of
1842 * pages pinned. Again, this may be less than nr_pages.
1843 *
1844 * The caller is responsible for releasing returned @pages, via put_page().
1845 *
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -07001846 * @vmas are valid only as long as mmap_lock is held.
John Hubbardc4237f82020-01-30 22:12:36 -08001847 *
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -07001848 * Must be called with mmap_lock held for read or write.
John Hubbardc4237f82020-01-30 22:12:36 -08001849 *
Souptick Joarderadc8cb42020-06-01 21:48:24 -07001850 * get_user_pages_remote walks a process's page tables and takes a reference
1851 * to each struct page that each user address corresponds to at a given
John Hubbardc4237f82020-01-30 22:12:36 -08001852 * instant. That is, it takes the page that would be accessed if a user
1853 * thread accesses the given user virtual address at that instant.
1854 *
1855 * This does not guarantee that the page exists in the user mappings when
Souptick Joarderadc8cb42020-06-01 21:48:24 -07001856 * get_user_pages_remote returns, and there may even be a completely different
John Hubbardc4237f82020-01-30 22:12:36 -08001857 * page there in some cases (eg. if mmapped pagecache has been invalidated
1858 * and subsequently re faulted). However it does guarantee that the page
1859 * won't be freed completely. And mostly callers simply care that the page
1860 * contains data that was valid *at some point in time*. Typically, an IO
1861 * or similar operation cannot guarantee anything stronger anyway because
1862 * locks can't be held over the syscall boundary.
1863 *
1864 * If gup_flags & FOLL_WRITE == 0, the page must not be written to. If the page
1865 * is written to, set_page_dirty (or set_page_dirty_lock, as appropriate) must
1866 * be called after the page is finished with, and before put_page is called.
1867 *
Souptick Joarderadc8cb42020-06-01 21:48:24 -07001868 * get_user_pages_remote is typically used for fewer-copy IO operations,
1869 * to get a handle on the memory by some means other than accesses
1870 * via the user virtual addresses. The pages may be submitted for
1871 * DMA to devices or accessed via their kernel linear mapping (via the
1872 * kmap APIs). Care should be taken to use the correct cache flushing APIs.
John Hubbardc4237f82020-01-30 22:12:36 -08001873 *
1874 * See also get_user_pages_fast, for performance critical applications.
1875 *
Souptick Joarderadc8cb42020-06-01 21:48:24 -07001876 * get_user_pages_remote should be phased out in favor of
John Hubbardc4237f82020-01-30 22:12:36 -08001877 * get_user_pages_locked|unlocked or get_user_pages_fast. Nothing
Souptick Joarderadc8cb42020-06-01 21:48:24 -07001878 * should use get_user_pages_remote because it cannot pass
John Hubbardc4237f82020-01-30 22:12:36 -08001879 * FAULT_FLAG_ALLOW_RETRY to handle_mm_fault.
1880 */
Peter Xu64019a22020-08-11 18:39:01 -07001881long get_user_pages_remote(struct mm_struct *mm,
John Hubbardc4237f82020-01-30 22:12:36 -08001882 unsigned long start, unsigned long nr_pages,
1883 unsigned int gup_flags, struct page **pages,
1884 struct vm_area_struct **vmas, int *locked)
1885{
Barry Song447f3e42020-10-13 16:51:58 -07001886 if (!is_valid_gup_flags(gup_flags))
John Hubbardeddb1c22020-01-30 22:12:54 -08001887 return -EINVAL;
1888
Peter Xu64019a22020-08-11 18:39:01 -07001889 return __get_user_pages_remote(mm, start, nr_pages, gup_flags,
John Hubbard22bf29b2020-04-01 21:05:10 -07001890 pages, vmas, locked);
John Hubbardc4237f82020-01-30 22:12:36 -08001891}
1892EXPORT_SYMBOL(get_user_pages_remote);
1893
John Hubbardeddb1c22020-01-30 22:12:54 -08001894#else /* CONFIG_MMU */
Peter Xu64019a22020-08-11 18:39:01 -07001895long get_user_pages_remote(struct mm_struct *mm,
John Hubbardeddb1c22020-01-30 22:12:54 -08001896 unsigned long start, unsigned long nr_pages,
1897 unsigned int gup_flags, struct page **pages,
1898 struct vm_area_struct **vmas, int *locked)
1899{
1900 return 0;
1901}
John Hubbard3faa52c2020-04-01 21:05:29 -07001902
Peter Xu64019a22020-08-11 18:39:01 -07001903static long __get_user_pages_remote(struct mm_struct *mm,
John Hubbard3faa52c2020-04-01 21:05:29 -07001904 unsigned long start, unsigned long nr_pages,
1905 unsigned int gup_flags, struct page **pages,
1906 struct vm_area_struct **vmas, int *locked)
1907{
1908 return 0;
1909}
John Hubbardeddb1c22020-01-30 22:12:54 -08001910#endif /* !CONFIG_MMU */
1911
Souptick Joarderadc8cb42020-06-01 21:48:24 -07001912/**
1913 * get_user_pages() - pin user pages in memory
1914 * @start: starting user address
1915 * @nr_pages: number of pages from start to pin
1916 * @gup_flags: flags modifying lookup behaviour
1917 * @pages: array that receives pointers to the pages pinned.
1918 * Should be at least nr_pages long. Or NULL, if caller
1919 * only intends to ensure the pages are faulted in.
1920 * @vmas: array of pointers to vmas corresponding to each page.
1921 * Or NULL if the caller does not require them.
1922 *
Peter Xu64019a22020-08-11 18:39:01 -07001923 * This is the same as get_user_pages_remote(), just with a less-flexible
1924 * calling convention where we assume that the mm being operated on belongs to
1925 * the current task, and doesn't allow passing of a locked parameter. We also
1926 * obviously don't pass FOLL_REMOTE in here.
Ira Weiny932f4a62019-05-13 17:17:03 -07001927 */
1928long get_user_pages(unsigned long start, unsigned long nr_pages,
1929 unsigned int gup_flags, struct page **pages,
1930 struct vm_area_struct **vmas)
1931{
Barry Song447f3e42020-10-13 16:51:58 -07001932 if (!is_valid_gup_flags(gup_flags))
John Hubbardeddb1c22020-01-30 22:12:54 -08001933 return -EINVAL;
1934
Peter Xu64019a22020-08-11 18:39:01 -07001935 return __gup_longterm_locked(current->mm, start, nr_pages,
Ira Weiny932f4a62019-05-13 17:17:03 -07001936 pages, vmas, gup_flags | FOLL_TOUCH);
1937}
1938EXPORT_SYMBOL(get_user_pages);
Dan Williams2bb6d282017-11-29 16:10:35 -08001939
Souptick Joarderadc8cb42020-06-01 21:48:24 -07001940/**
Christoph Hellwigd3649f62019-07-11 20:57:18 -07001941 * get_user_pages_locked() is suitable to replace the form:
Kirill A. Shutemovacc3c8d2015-04-14 15:44:45 -07001942 *
Michel Lespinasse3e4e28c2020-06-08 21:33:51 -07001943 * mmap_read_lock(mm);
Christoph Hellwigd3649f62019-07-11 20:57:18 -07001944 * do_something()
Peter Xu64019a22020-08-11 18:39:01 -07001945 * get_user_pages(mm, ..., pages, NULL);
Michel Lespinasse3e4e28c2020-06-08 21:33:51 -07001946 * mmap_read_unlock(mm);
Kirill A. Shutemovacc3c8d2015-04-14 15:44:45 -07001947 *
Christoph Hellwigd3649f62019-07-11 20:57:18 -07001948 * to:
Kirill A. Shutemovacc3c8d2015-04-14 15:44:45 -07001949 *
Christoph Hellwigd3649f62019-07-11 20:57:18 -07001950 * int locked = 1;
Michel Lespinasse3e4e28c2020-06-08 21:33:51 -07001951 * mmap_read_lock(mm);
Christoph Hellwigd3649f62019-07-11 20:57:18 -07001952 * do_something()
Peter Xu64019a22020-08-11 18:39:01 -07001953 * get_user_pages_locked(mm, ..., pages, &locked);
Christoph Hellwigd3649f62019-07-11 20:57:18 -07001954 * if (locked)
Michel Lespinasse3e4e28c2020-06-08 21:33:51 -07001955 * mmap_read_unlock(mm);
Souptick Joarderadc8cb42020-06-01 21:48:24 -07001956 *
1957 * @start: starting user address
1958 * @nr_pages: number of pages from start to pin
1959 * @gup_flags: flags modifying lookup behaviour
1960 * @pages: array that receives pointers to the pages pinned.
1961 * Should be at least nr_pages long. Or NULL, if caller
1962 * only intends to ensure the pages are faulted in.
1963 * @locked: pointer to lock flag indicating whether lock is held and
1964 * subsequently whether VM_FAULT_RETRY functionality can be
1965 * utilised. Lock must initially be held.
1966 *
1967 * We can leverage the VM_FAULT_RETRY functionality in the page fault
1968 * paths better by using either get_user_pages_locked() or
1969 * get_user_pages_unlocked().
1970 *
Kirill A. Shutemovacc3c8d2015-04-14 15:44:45 -07001971 */
Christoph Hellwigd3649f62019-07-11 20:57:18 -07001972long get_user_pages_locked(unsigned long start, unsigned long nr_pages,
1973 unsigned int gup_flags, struct page **pages,
1974 int *locked)
Kirill A. Shutemovacc3c8d2015-04-14 15:44:45 -07001975{
Kirill A. Shutemovacc3c8d2015-04-14 15:44:45 -07001976 /*
Christoph Hellwigd3649f62019-07-11 20:57:18 -07001977 * FIXME: Current FOLL_LONGTERM behavior is incompatible with
1978 * FAULT_FLAG_ALLOW_RETRY because of the FS DAX check requirement on
1979 * vmas. As there are no users of this flag in this call we simply
1980 * disallow this option for now.
Kirill A. Shutemovacc3c8d2015-04-14 15:44:45 -07001981 */
Christoph Hellwigd3649f62019-07-11 20:57:18 -07001982 if (WARN_ON_ONCE(gup_flags & FOLL_LONGTERM))
1983 return -EINVAL;
John Hubbard420c2092020-06-07 21:41:02 -07001984 /*
1985 * FOLL_PIN must only be set internally by the pin_user_pages*() APIs,
1986 * never directly by the caller, so enforce that:
1987 */
1988 if (WARN_ON_ONCE(gup_flags & FOLL_PIN))
1989 return -EINVAL;
Kirill A. Shutemovacc3c8d2015-04-14 15:44:45 -07001990
Peter Xu64019a22020-08-11 18:39:01 -07001991 return __get_user_pages_locked(current->mm, start, nr_pages,
Christoph Hellwigd3649f62019-07-11 20:57:18 -07001992 pages, NULL, locked,
1993 gup_flags | FOLL_TOUCH);
Kirill A. Shutemovacc3c8d2015-04-14 15:44:45 -07001994}
Christoph Hellwigd3649f62019-07-11 20:57:18 -07001995EXPORT_SYMBOL(get_user_pages_locked);
Kirill A. Shutemovacc3c8d2015-04-14 15:44:45 -07001996
1997/*
Christoph Hellwigd3649f62019-07-11 20:57:18 -07001998 * get_user_pages_unlocked() is suitable to replace the form:
Kirill A. Shutemovacc3c8d2015-04-14 15:44:45 -07001999 *
Michel Lespinasse3e4e28c2020-06-08 21:33:51 -07002000 * mmap_read_lock(mm);
Peter Xu64019a22020-08-11 18:39:01 -07002001 * get_user_pages(mm, ..., pages, NULL);
Michel Lespinasse3e4e28c2020-06-08 21:33:51 -07002002 * mmap_read_unlock(mm);
Christoph Hellwigd3649f62019-07-11 20:57:18 -07002003 *
2004 * with:
2005 *
Peter Xu64019a22020-08-11 18:39:01 -07002006 * get_user_pages_unlocked(mm, ..., pages);
Christoph Hellwigd3649f62019-07-11 20:57:18 -07002007 *
2008 * It is functionally equivalent to get_user_pages_fast so
2009 * get_user_pages_fast should be used instead if specific gup_flags
2010 * (e.g. FOLL_FORCE) are not required.
Kirill A. Shutemovacc3c8d2015-04-14 15:44:45 -07002011 */
Christoph Hellwigd3649f62019-07-11 20:57:18 -07002012long get_user_pages_unlocked(unsigned long start, unsigned long nr_pages,
2013 struct page **pages, unsigned int gup_flags)
Kirill A. Shutemovacc3c8d2015-04-14 15:44:45 -07002014{
2015 struct mm_struct *mm = current->mm;
Christoph Hellwigd3649f62019-07-11 20:57:18 -07002016 int locked = 1;
2017 long ret;
Kirill A. Shutemovacc3c8d2015-04-14 15:44:45 -07002018
Christoph Hellwigd3649f62019-07-11 20:57:18 -07002019 /*
2020 * FIXME: Current FOLL_LONGTERM behavior is incompatible with
2021 * FAULT_FLAG_ALLOW_RETRY because of the FS DAX check requirement on
2022 * vmas. As there are no users of this flag in this call we simply
2023 * disallow this option for now.
2024 */
2025 if (WARN_ON_ONCE(gup_flags & FOLL_LONGTERM))
2026 return -EINVAL;
Kirill A. Shutemovacc3c8d2015-04-14 15:44:45 -07002027
Michel Lespinassed8ed45c2020-06-08 21:33:25 -07002028 mmap_read_lock(mm);
Peter Xu64019a22020-08-11 18:39:01 -07002029 ret = __get_user_pages_locked(mm, start, nr_pages, pages, NULL,
Christoph Hellwigd3649f62019-07-11 20:57:18 -07002030 &locked, gup_flags | FOLL_TOUCH);
Kirill A. Shutemovacc3c8d2015-04-14 15:44:45 -07002031 if (locked)
Michel Lespinassed8ed45c2020-06-08 21:33:25 -07002032 mmap_read_unlock(mm);
Christoph Hellwigd3649f62019-07-11 20:57:18 -07002033 return ret;
Kirill A. Shutemovacc3c8d2015-04-14 15:44:45 -07002034}
Christoph Hellwigd3649f62019-07-11 20:57:18 -07002035EXPORT_SYMBOL(get_user_pages_unlocked);
Steve Capper2667f502014-10-09 15:29:14 -07002036
2037/*
Christoph Hellwig67a929e2019-07-11 20:57:14 -07002038 * Fast GUP
Steve Capper2667f502014-10-09 15:29:14 -07002039 *
2040 * get_user_pages_fast attempts to pin user pages by walking the page
2041 * tables directly and avoids taking locks. Thus the walker needs to be
2042 * protected from page table pages being freed from under it, and should
2043 * block any THP splits.
2044 *
2045 * One way to achieve this is to have the walker disable interrupts, and
2046 * rely on IPIs from the TLB flushing code blocking before the page table
2047 * pages are freed. This is unsuitable for architectures that do not need
2048 * to broadcast an IPI when invalidating TLBs.
2049 *
2050 * Another way to achieve this is to batch up page table containing pages
2051 * belonging to more than one mm_user, then rcu_sched a callback to free those
2052 * pages. Disabling interrupts will allow the fast_gup walker to both block
2053 * the rcu_sched callback, and an IPI that we broadcast for splitting THPs
2054 * (which is a relatively rare event). The code below adopts this strategy.
2055 *
2056 * Before activating this code, please be aware that the following assumptions
2057 * are currently made:
2058 *
Peter Zijlstraff2e6d722020-02-03 17:37:02 -08002059 * *) Either MMU_GATHER_RCU_TABLE_FREE is enabled, and tlb_remove_table() is used to
Kirill A. Shutemove5855132017-06-06 14:31:20 +03002060 * free pages containing page tables or TLB flushing requires IPI broadcast.
Steve Capper2667f502014-10-09 15:29:14 -07002061 *
Steve Capper2667f502014-10-09 15:29:14 -07002062 * *) ptes can be read atomically by the architecture.
2063 *
2064 * *) access_ok is sufficient to validate userspace address ranges.
2065 *
2066 * The last two assumptions can be relaxed by the addition of helper functions.
2067 *
2068 * This code is based heavily on the PowerPC implementation by Nick Piggin.
2069 */
Christoph Hellwig67a929e2019-07-11 20:57:14 -07002070#ifdef CONFIG_HAVE_FAST_GUP
Christoph Hellwig39656e82019-07-11 20:56:49 -07002071#ifdef CONFIG_GUP_GET_PTE_LOW_HIGH
John Hubbard3faa52c2020-04-01 21:05:29 -07002072
Kirill A. Shutemov0005d202017-03-16 18:26:51 +03002073/*
Christoph Hellwig39656e82019-07-11 20:56:49 -07002074 * WARNING: only to be used in the get_user_pages_fast() implementation.
2075 *
2076 * With get_user_pages_fast(), we walk down the pagetables without taking any
2077 * locks. For this we would like to load the pointers atomically, but sometimes
2078 * that is not possible (e.g. without expensive cmpxchg8b on x86_32 PAE). What
2079 * we do have is the guarantee that a PTE will only either go from not present
2080 * to present, or present to not present or both -- it will not switch to a
2081 * completely different present page without a TLB flush in between; something
2082 * that we are blocking by holding interrupts off.
2083 *
2084 * Setting ptes from not present to present goes:
2085 *
2086 * ptep->pte_high = h;
2087 * smp_wmb();
2088 * ptep->pte_low = l;
2089 *
2090 * And present to not present goes:
2091 *
2092 * ptep->pte_low = 0;
2093 * smp_wmb();
2094 * ptep->pte_high = 0;
2095 *
2096 * We must ensure here that the load of pte_low sees 'l' IFF pte_high sees 'h'.
2097 * We load pte_high *after* loading pte_low, which ensures we don't see an older
2098 * value of pte_high. *Then* we recheck pte_low, which ensures that we haven't
2099 * picked up a changed pte high. We might have gotten rubbish values from
2100 * pte_low and pte_high, but we are guaranteed that pte_low will not have the
2101 * present bit set *unless* it is 'l'. Because get_user_pages_fast() only
2102 * operates on present ptes we're safe.
2103 */
2104static inline pte_t gup_get_pte(pte_t *ptep)
2105{
2106 pte_t pte;
2107
2108 do {
2109 pte.pte_low = ptep->pte_low;
2110 smp_rmb();
2111 pte.pte_high = ptep->pte_high;
2112 smp_rmb();
2113 } while (unlikely(pte.pte_low != ptep->pte_low));
2114
2115 return pte;
2116}
2117#else /* CONFIG_GUP_GET_PTE_LOW_HIGH */
2118/*
2119 * We require that the PTE can be read atomically.
Kirill A. Shutemov0005d202017-03-16 18:26:51 +03002120 */
2121static inline pte_t gup_get_pte(pte_t *ptep)
2122{
Christophe Leroy481e9802020-06-15 12:57:58 +00002123 return ptep_get(ptep);
Kirill A. Shutemov0005d202017-03-16 18:26:51 +03002124}
Christoph Hellwig39656e82019-07-11 20:56:49 -07002125#endif /* CONFIG_GUP_GET_PTE_LOW_HIGH */
Kirill A. Shutemov0005d202017-03-16 18:26:51 +03002126
Guenter Roeck790c7362019-07-11 20:57:46 -07002127static void __maybe_unused undo_dev_pagemap(int *nr, int nr_start,
John Hubbard3b78d832020-04-01 21:05:22 -07002128 unsigned int flags,
Guenter Roeck790c7362019-07-11 20:57:46 -07002129 struct page **pages)
Kirill A. Shutemovb59f65f2017-03-16 18:26:53 +03002130{
2131 while ((*nr) - nr_start) {
2132 struct page *page = pages[--(*nr)];
2133
2134 ClearPageReferenced(page);
John Hubbard3faa52c2020-04-01 21:05:29 -07002135 if (flags & FOLL_PIN)
2136 unpin_user_page(page);
2137 else
2138 put_page(page);
Kirill A. Shutemovb59f65f2017-03-16 18:26:53 +03002139 }
2140}
2141
Laurent Dufour3010a5e2018-06-07 17:06:08 -07002142#ifdef CONFIG_ARCH_HAS_PTE_SPECIAL
Yang Shi377c60dd2022-09-07 11:01:43 -07002143/*
2144 * Fast-gup relies on pte change detection to avoid concurrent pgtable
2145 * operations.
2146 *
2147 * To pin the page, fast-gup needs to do below in order:
2148 * (1) pin the page (by prefetching pte), then (2) check pte not changed.
2149 *
2150 * For the rest of pgtable operations where pgtable updates can be racy
2151 * with fast-gup, we need to do (1) clear pte, then (2) check whether page
2152 * is pinned.
2153 *
2154 * Above will work for all pte-level operations, including THP split.
2155 *
2156 * For THP collapse, it's a bit more complicated because fast-gup may be
2157 * walking a pgtable page that is being freed (pte is still valid but pmd
2158 * can be cleared already). To avoid race in such condition, we need to
2159 * also check pmd here to make sure pmd doesn't change (corresponds to
2160 * pmdp_collapse_flush() in the THP collapse code path).
2161 */
2162static int gup_pte_range(pmd_t pmd, pmd_t *pmdp, unsigned long addr,
2163 unsigned long end, unsigned int flags,
2164 struct page **pages, int *nr)
Steve Capper2667f502014-10-09 15:29:14 -07002165{
Kirill A. Shutemovb59f65f2017-03-16 18:26:53 +03002166 struct dev_pagemap *pgmap = NULL;
2167 int nr_start = *nr, ret = 0;
Steve Capper2667f502014-10-09 15:29:14 -07002168 pte_t *ptep, *ptem;
Steve Capper2667f502014-10-09 15:29:14 -07002169
2170 ptem = ptep = pte_offset_map(&pmd, addr);
2171 do {
Kirill A. Shutemov0005d202017-03-16 18:26:51 +03002172 pte_t pte = gup_get_pte(ptep);
Kirill A. Shutemov7aef4172016-01-15 16:52:32 -08002173 struct page *head, *page;
Steve Capper2667f502014-10-09 15:29:14 -07002174
2175 /*
2176 * Similar to the PMD case below, NUMA hinting must take slow
Mel Gorman8a0516e2015-02-12 14:58:22 -08002177 * path using the pte_protnone check.
Steve Capper2667f502014-10-09 15:29:14 -07002178 */
Kirill A. Shutemove7884f82017-03-16 18:26:50 +03002179 if (pte_protnone(pte))
2180 goto pte_unmap;
2181
Ira Weinyb798bec2019-05-13 17:17:07 -07002182 if (!pte_access_permitted(pte, flags & FOLL_WRITE))
Kirill A. Shutemove7884f82017-03-16 18:26:50 +03002183 goto pte_unmap;
2184
Kirill A. Shutemovb59f65f2017-03-16 18:26:53 +03002185 if (pte_devmap(pte)) {
Ira Weiny7af75562019-05-13 17:17:14 -07002186 if (unlikely(flags & FOLL_LONGTERM))
2187 goto pte_unmap;
2188
Kirill A. Shutemovb59f65f2017-03-16 18:26:53 +03002189 pgmap = get_dev_pagemap(pte_pfn(pte), pgmap);
2190 if (unlikely(!pgmap)) {
John Hubbard3b78d832020-04-01 21:05:22 -07002191 undo_dev_pagemap(nr, nr_start, flags, pages);
Kirill A. Shutemovb59f65f2017-03-16 18:26:53 +03002192 goto pte_unmap;
2193 }
2194 } else if (pte_special(pte))
Steve Capper2667f502014-10-09 15:29:14 -07002195 goto pte_unmap;
2196
2197 VM_BUG_ON(!pfn_valid(pte_pfn(pte)));
2198 page = pte_page(pte);
2199
John Hubbard3faa52c2020-04-01 21:05:29 -07002200 head = try_grab_compound_head(page, 1, flags);
Linus Torvalds8fde12c2019-04-11 10:49:19 -07002201 if (!head)
Steve Capper2667f502014-10-09 15:29:14 -07002202 goto pte_unmap;
2203
Yang Shi377c60dd2022-09-07 11:01:43 -07002204 if (unlikely(pmd_val(pmd) != pmd_val(*pmdp)) ||
2205 unlikely(pte_val(pte) != pte_val(*ptep))) {
John Hubbard3faa52c2020-04-01 21:05:29 -07002206 put_compound_head(head, 1, flags);
Steve Capper2667f502014-10-09 15:29:14 -07002207 goto pte_unmap;
2208 }
2209
Kirill A. Shutemov7aef4172016-01-15 16:52:32 -08002210 VM_BUG_ON_PAGE(compound_head(page) != head, page);
Kirill A. Shutemove9348052017-03-16 18:26:52 +03002211
Claudio Imbrendaf28d4362020-04-01 21:05:56 -07002212 /*
2213 * We need to make the page accessible if and only if we are
2214 * going to access its content (the FOLL_PIN case). Please
2215 * see Documentation/core-api/pin_user_pages.rst for
2216 * details.
2217 */
2218 if (flags & FOLL_PIN) {
2219 ret = arch_make_page_accessible(page);
2220 if (ret) {
2221 unpin_user_page(page);
2222 goto pte_unmap;
2223 }
2224 }
Kirill A. Shutemove9348052017-03-16 18:26:52 +03002225 SetPageReferenced(page);
Steve Capper2667f502014-10-09 15:29:14 -07002226 pages[*nr] = page;
2227 (*nr)++;
2228
2229 } while (ptep++, addr += PAGE_SIZE, addr != end);
2230
2231 ret = 1;
2232
2233pte_unmap:
Christoph Hellwig832d7aa2017-12-29 08:54:01 +01002234 if (pgmap)
2235 put_dev_pagemap(pgmap);
Steve Capper2667f502014-10-09 15:29:14 -07002236 pte_unmap(ptem);
2237 return ret;
2238}
2239#else
2240
2241/*
2242 * If we can't determine whether or not a pte is special, then fail immediately
2243 * for ptes. Note, we can still pin HugeTLB and THP as these are guaranteed not
2244 * to be special.
2245 *
2246 * For a futex to be placed on a THP tail page, get_futex_key requires a
Souptick Joarderdadbb612020-06-07 21:40:55 -07002247 * get_user_pages_fast_only implementation that can pin pages. Thus it's still
Steve Capper2667f502014-10-09 15:29:14 -07002248 * useful to have gup_huge_pmd even if we can't operate on ptes.
2249 */
Yang Shi377c60dd2022-09-07 11:01:43 -07002250static int gup_pte_range(pmd_t pmd, pmd_t *pmdp, unsigned long addr,
2251 unsigned long end, unsigned int flags,
2252 struct page **pages, int *nr)
Steve Capper2667f502014-10-09 15:29:14 -07002253{
2254 return 0;
2255}
Laurent Dufour3010a5e2018-06-07 17:06:08 -07002256#endif /* CONFIG_ARCH_HAS_PTE_SPECIAL */
Steve Capper2667f502014-10-09 15:29:14 -07002257
Robin Murphy17596732019-07-16 16:30:47 -07002258#if defined(CONFIG_ARCH_HAS_PTE_DEVMAP) && defined(CONFIG_TRANSPARENT_HUGEPAGE)
Kirill A. Shutemovb59f65f2017-03-16 18:26:53 +03002259static int __gup_device_huge(unsigned long pfn, unsigned long addr,
John Hubbard86dfbed2020-04-01 21:05:14 -07002260 unsigned long end, unsigned int flags,
2261 struct page **pages, int *nr)
Kirill A. Shutemovb59f65f2017-03-16 18:26:53 +03002262{
2263 int nr_start = *nr;
2264 struct dev_pagemap *pgmap = NULL;
2265
2266 do {
2267 struct page *page = pfn_to_page(pfn);
2268
2269 pgmap = get_dev_pagemap(pfn, pgmap);
2270 if (unlikely(!pgmap)) {
John Hubbard3b78d832020-04-01 21:05:22 -07002271 undo_dev_pagemap(nr, nr_start, flags, pages);
Kirill A. Shutemovb59f65f2017-03-16 18:26:53 +03002272 return 0;
2273 }
2274 SetPageReferenced(page);
2275 pages[*nr] = page;
John Hubbard3faa52c2020-04-01 21:05:29 -07002276 if (unlikely(!try_grab_page(page, flags))) {
2277 undo_dev_pagemap(nr, nr_start, flags, pages);
2278 return 0;
2279 }
Kirill A. Shutemovb59f65f2017-03-16 18:26:53 +03002280 (*nr)++;
2281 pfn++;
2282 } while (addr += PAGE_SIZE, addr != end);
Christoph Hellwig832d7aa2017-12-29 08:54:01 +01002283
2284 if (pgmap)
2285 put_dev_pagemap(pgmap);
Kirill A. Shutemovb59f65f2017-03-16 18:26:53 +03002286 return 1;
2287}
2288
Dan Williamsa9b6de72018-04-19 21:32:19 -07002289static int __gup_device_huge_pmd(pmd_t orig, pmd_t *pmdp, unsigned long addr,
John Hubbard86dfbed2020-04-01 21:05:14 -07002290 unsigned long end, unsigned int flags,
2291 struct page **pages, int *nr)
Kirill A. Shutemovb59f65f2017-03-16 18:26:53 +03002292{
2293 unsigned long fault_pfn;
Dan Williamsa9b6de72018-04-19 21:32:19 -07002294 int nr_start = *nr;
Kirill A. Shutemovb59f65f2017-03-16 18:26:53 +03002295
Dan Williamsa9b6de72018-04-19 21:32:19 -07002296 fault_pfn = pmd_pfn(orig) + ((addr & ~PMD_MASK) >> PAGE_SHIFT);
John Hubbard86dfbed2020-04-01 21:05:14 -07002297 if (!__gup_device_huge(fault_pfn, addr, end, flags, pages, nr))
Dan Williamsa9b6de72018-04-19 21:32:19 -07002298 return 0;
2299
2300 if (unlikely(pmd_val(orig) != pmd_val(*pmdp))) {
John Hubbard3b78d832020-04-01 21:05:22 -07002301 undo_dev_pagemap(nr, nr_start, flags, pages);
Dan Williamsa9b6de72018-04-19 21:32:19 -07002302 return 0;
2303 }
2304 return 1;
Kirill A. Shutemovb59f65f2017-03-16 18:26:53 +03002305}
2306
Dan Williamsa9b6de72018-04-19 21:32:19 -07002307static int __gup_device_huge_pud(pud_t orig, pud_t *pudp, unsigned long addr,
John Hubbard86dfbed2020-04-01 21:05:14 -07002308 unsigned long end, unsigned int flags,
2309 struct page **pages, int *nr)
Kirill A. Shutemovb59f65f2017-03-16 18:26:53 +03002310{
2311 unsigned long fault_pfn;
Dan Williamsa9b6de72018-04-19 21:32:19 -07002312 int nr_start = *nr;
Kirill A. Shutemovb59f65f2017-03-16 18:26:53 +03002313
Dan Williamsa9b6de72018-04-19 21:32:19 -07002314 fault_pfn = pud_pfn(orig) + ((addr & ~PUD_MASK) >> PAGE_SHIFT);
John Hubbard86dfbed2020-04-01 21:05:14 -07002315 if (!__gup_device_huge(fault_pfn, addr, end, flags, pages, nr))
Dan Williamsa9b6de72018-04-19 21:32:19 -07002316 return 0;
2317
2318 if (unlikely(pud_val(orig) != pud_val(*pudp))) {
John Hubbard3b78d832020-04-01 21:05:22 -07002319 undo_dev_pagemap(nr, nr_start, flags, pages);
Dan Williamsa9b6de72018-04-19 21:32:19 -07002320 return 0;
2321 }
2322 return 1;
Kirill A. Shutemovb59f65f2017-03-16 18:26:53 +03002323}
2324#else
Dan Williamsa9b6de72018-04-19 21:32:19 -07002325static int __gup_device_huge_pmd(pmd_t orig, pmd_t *pmdp, unsigned long addr,
John Hubbard86dfbed2020-04-01 21:05:14 -07002326 unsigned long end, unsigned int flags,
2327 struct page **pages, int *nr)
Kirill A. Shutemovb59f65f2017-03-16 18:26:53 +03002328{
2329 BUILD_BUG();
2330 return 0;
2331}
2332
Dan Williamsa9b6de72018-04-19 21:32:19 -07002333static int __gup_device_huge_pud(pud_t pud, pud_t *pudp, unsigned long addr,
John Hubbard86dfbed2020-04-01 21:05:14 -07002334 unsigned long end, unsigned int flags,
2335 struct page **pages, int *nr)
Kirill A. Shutemovb59f65f2017-03-16 18:26:53 +03002336{
2337 BUILD_BUG();
2338 return 0;
2339}
2340#endif
2341
John Hubbarda43e9822020-01-30 22:12:17 -08002342static int record_subpages(struct page *page, unsigned long addr,
2343 unsigned long end, struct page **pages)
2344{
2345 int nr;
2346
2347 for (nr = 0; addr != end; addr += PAGE_SIZE)
2348 pages[nr++] = page++;
2349
2350 return nr;
2351}
2352
Christoph Hellwigcbd34da2019-07-11 20:57:28 -07002353#ifdef CONFIG_ARCH_HAS_HUGEPD
2354static unsigned long hugepte_addr_end(unsigned long addr, unsigned long end,
2355 unsigned long sz)
2356{
2357 unsigned long __boundary = (addr + sz) & ~(sz-1);
2358 return (__boundary - 1 < end - 1) ? __boundary : end;
2359}
2360
2361static int gup_hugepte(pte_t *ptep, unsigned long sz, unsigned long addr,
John Hubbard0cd22af2019-10-18 20:19:53 -07002362 unsigned long end, unsigned int flags,
2363 struct page **pages, int *nr)
Christoph Hellwigcbd34da2019-07-11 20:57:28 -07002364{
2365 unsigned long pte_end;
2366 struct page *head, *page;
2367 pte_t pte;
2368 int refs;
2369
2370 pte_end = (addr + sz) & ~(sz-1);
2371 if (pte_end < end)
2372 end = pte_end;
2373
Christophe Leroy55ca2262020-06-15 12:57:57 +00002374 pte = huge_ptep_get(ptep);
Christoph Hellwigcbd34da2019-07-11 20:57:28 -07002375
John Hubbard0cd22af2019-10-18 20:19:53 -07002376 if (!pte_access_permitted(pte, flags & FOLL_WRITE))
Christoph Hellwigcbd34da2019-07-11 20:57:28 -07002377 return 0;
2378
2379 /* hugepages are never "special" */
2380 VM_BUG_ON(!pfn_valid(pte_pfn(pte)));
2381
Christoph Hellwigcbd34da2019-07-11 20:57:28 -07002382 head = pte_page(pte);
Christoph Hellwigcbd34da2019-07-11 20:57:28 -07002383 page = head + ((addr & (sz-1)) >> PAGE_SHIFT);
John Hubbarda43e9822020-01-30 22:12:17 -08002384 refs = record_subpages(page, addr, end, pages + *nr);
Christoph Hellwigcbd34da2019-07-11 20:57:28 -07002385
John Hubbard3faa52c2020-04-01 21:05:29 -07002386 head = try_grab_compound_head(head, refs, flags);
John Hubbarda43e9822020-01-30 22:12:17 -08002387 if (!head)
Christoph Hellwigcbd34da2019-07-11 20:57:28 -07002388 return 0;
Christoph Hellwigcbd34da2019-07-11 20:57:28 -07002389
2390 if (unlikely(pte_val(pte) != pte_val(*ptep))) {
John Hubbard3b78d832020-04-01 21:05:22 -07002391 put_compound_head(head, refs, flags);
Christoph Hellwigcbd34da2019-07-11 20:57:28 -07002392 return 0;
2393 }
2394
John Hubbarda43e9822020-01-30 22:12:17 -08002395 *nr += refs;
Christoph Hellwig520b4a42019-07-11 20:57:36 -07002396 SetPageReferenced(head);
Christoph Hellwigcbd34da2019-07-11 20:57:28 -07002397 return 1;
2398}
2399
2400static int gup_huge_pd(hugepd_t hugepd, unsigned long addr,
John Hubbard0cd22af2019-10-18 20:19:53 -07002401 unsigned int pdshift, unsigned long end, unsigned int flags,
Christoph Hellwigcbd34da2019-07-11 20:57:28 -07002402 struct page **pages, int *nr)
2403{
2404 pte_t *ptep;
2405 unsigned long sz = 1UL << hugepd_shift(hugepd);
2406 unsigned long next;
2407
2408 ptep = hugepte_offset(hugepd, addr, pdshift);
2409 do {
2410 next = hugepte_addr_end(addr, end, sz);
John Hubbard0cd22af2019-10-18 20:19:53 -07002411 if (!gup_hugepte(ptep, sz, addr, end, flags, pages, nr))
Christoph Hellwigcbd34da2019-07-11 20:57:28 -07002412 return 0;
2413 } while (ptep++, addr = next, addr != end);
2414
2415 return 1;
2416}
2417#else
2418static inline int gup_huge_pd(hugepd_t hugepd, unsigned long addr,
John Hubbard0cd22af2019-10-18 20:19:53 -07002419 unsigned int pdshift, unsigned long end, unsigned int flags,
Christoph Hellwigcbd34da2019-07-11 20:57:28 -07002420 struct page **pages, int *nr)
2421{
2422 return 0;
2423}
2424#endif /* CONFIG_ARCH_HAS_HUGEPD */
2425
Steve Capper2667f502014-10-09 15:29:14 -07002426static int gup_huge_pmd(pmd_t orig, pmd_t *pmdp, unsigned long addr,
John Hubbard0cd22af2019-10-18 20:19:53 -07002427 unsigned long end, unsigned int flags,
2428 struct page **pages, int *nr)
Steve Capper2667f502014-10-09 15:29:14 -07002429{
Kirill A. Shutemovddc58f22016-01-15 16:52:56 -08002430 struct page *head, *page;
Steve Capper2667f502014-10-09 15:29:14 -07002431 int refs;
2432
Ira Weinyb798bec2019-05-13 17:17:07 -07002433 if (!pmd_access_permitted(orig, flags & FOLL_WRITE))
Steve Capper2667f502014-10-09 15:29:14 -07002434 return 0;
2435
Ira Weiny7af75562019-05-13 17:17:14 -07002436 if (pmd_devmap(orig)) {
2437 if (unlikely(flags & FOLL_LONGTERM))
2438 return 0;
John Hubbard86dfbed2020-04-01 21:05:14 -07002439 return __gup_device_huge_pmd(orig, pmdp, addr, end, flags,
2440 pages, nr);
Ira Weiny7af75562019-05-13 17:17:14 -07002441 }
Kirill A. Shutemovb59f65f2017-03-16 18:26:53 +03002442
Punit Agrawald63206e2017-07-06 15:39:39 -07002443 page = pmd_page(orig) + ((addr & ~PMD_MASK) >> PAGE_SHIFT);
John Hubbarda43e9822020-01-30 22:12:17 -08002444 refs = record_subpages(page, addr, end, pages + *nr);
Steve Capper2667f502014-10-09 15:29:14 -07002445
John Hubbard3faa52c2020-04-01 21:05:29 -07002446 head = try_grab_compound_head(pmd_page(orig), refs, flags);
John Hubbarda43e9822020-01-30 22:12:17 -08002447 if (!head)
Steve Capper2667f502014-10-09 15:29:14 -07002448 return 0;
Steve Capper2667f502014-10-09 15:29:14 -07002449
2450 if (unlikely(pmd_val(orig) != pmd_val(*pmdp))) {
John Hubbard3b78d832020-04-01 21:05:22 -07002451 put_compound_head(head, refs, flags);
Steve Capper2667f502014-10-09 15:29:14 -07002452 return 0;
2453 }
2454
John Hubbarda43e9822020-01-30 22:12:17 -08002455 *nr += refs;
Kirill A. Shutemove9348052017-03-16 18:26:52 +03002456 SetPageReferenced(head);
Steve Capper2667f502014-10-09 15:29:14 -07002457 return 1;
2458}
2459
2460static int gup_huge_pud(pud_t orig, pud_t *pudp, unsigned long addr,
John Hubbard86dfbed2020-04-01 21:05:14 -07002461 unsigned long end, unsigned int flags,
2462 struct page **pages, int *nr)
Steve Capper2667f502014-10-09 15:29:14 -07002463{
Kirill A. Shutemovddc58f22016-01-15 16:52:56 -08002464 struct page *head, *page;
Steve Capper2667f502014-10-09 15:29:14 -07002465 int refs;
2466
Ira Weinyb798bec2019-05-13 17:17:07 -07002467 if (!pud_access_permitted(orig, flags & FOLL_WRITE))
Steve Capper2667f502014-10-09 15:29:14 -07002468 return 0;
2469
Ira Weiny7af75562019-05-13 17:17:14 -07002470 if (pud_devmap(orig)) {
2471 if (unlikely(flags & FOLL_LONGTERM))
2472 return 0;
John Hubbard86dfbed2020-04-01 21:05:14 -07002473 return __gup_device_huge_pud(orig, pudp, addr, end, flags,
2474 pages, nr);
Ira Weiny7af75562019-05-13 17:17:14 -07002475 }
Kirill A. Shutemovb59f65f2017-03-16 18:26:53 +03002476
Punit Agrawald63206e2017-07-06 15:39:39 -07002477 page = pud_page(orig) + ((addr & ~PUD_MASK) >> PAGE_SHIFT);
John Hubbarda43e9822020-01-30 22:12:17 -08002478 refs = record_subpages(page, addr, end, pages + *nr);
Steve Capper2667f502014-10-09 15:29:14 -07002479
John Hubbard3faa52c2020-04-01 21:05:29 -07002480 head = try_grab_compound_head(pud_page(orig), refs, flags);
John Hubbarda43e9822020-01-30 22:12:17 -08002481 if (!head)
Steve Capper2667f502014-10-09 15:29:14 -07002482 return 0;
Steve Capper2667f502014-10-09 15:29:14 -07002483
2484 if (unlikely(pud_val(orig) != pud_val(*pudp))) {
John Hubbard3b78d832020-04-01 21:05:22 -07002485 put_compound_head(head, refs, flags);
Steve Capper2667f502014-10-09 15:29:14 -07002486 return 0;
2487 }
2488
John Hubbarda43e9822020-01-30 22:12:17 -08002489 *nr += refs;
Kirill A. Shutemove9348052017-03-16 18:26:52 +03002490 SetPageReferenced(head);
Steve Capper2667f502014-10-09 15:29:14 -07002491 return 1;
2492}
2493
Aneesh Kumar K.Vf30c59e2014-11-05 21:57:40 +05302494static int gup_huge_pgd(pgd_t orig, pgd_t *pgdp, unsigned long addr,
Ira Weinyb798bec2019-05-13 17:17:07 -07002495 unsigned long end, unsigned int flags,
Aneesh Kumar K.Vf30c59e2014-11-05 21:57:40 +05302496 struct page **pages, int *nr)
2497{
2498 int refs;
Kirill A. Shutemovddc58f22016-01-15 16:52:56 -08002499 struct page *head, *page;
Aneesh Kumar K.Vf30c59e2014-11-05 21:57:40 +05302500
Ira Weinyb798bec2019-05-13 17:17:07 -07002501 if (!pgd_access_permitted(orig, flags & FOLL_WRITE))
Aneesh Kumar K.Vf30c59e2014-11-05 21:57:40 +05302502 return 0;
2503
Kirill A. Shutemovb59f65f2017-03-16 18:26:53 +03002504 BUILD_BUG_ON(pgd_devmap(orig));
John Hubbarda43e9822020-01-30 22:12:17 -08002505
Punit Agrawald63206e2017-07-06 15:39:39 -07002506 page = pgd_page(orig) + ((addr & ~PGDIR_MASK) >> PAGE_SHIFT);
John Hubbarda43e9822020-01-30 22:12:17 -08002507 refs = record_subpages(page, addr, end, pages + *nr);
Aneesh Kumar K.Vf30c59e2014-11-05 21:57:40 +05302508
John Hubbard3faa52c2020-04-01 21:05:29 -07002509 head = try_grab_compound_head(pgd_page(orig), refs, flags);
John Hubbarda43e9822020-01-30 22:12:17 -08002510 if (!head)
Aneesh Kumar K.Vf30c59e2014-11-05 21:57:40 +05302511 return 0;
Aneesh Kumar K.Vf30c59e2014-11-05 21:57:40 +05302512
2513 if (unlikely(pgd_val(orig) != pgd_val(*pgdp))) {
John Hubbard3b78d832020-04-01 21:05:22 -07002514 put_compound_head(head, refs, flags);
Aneesh Kumar K.Vf30c59e2014-11-05 21:57:40 +05302515 return 0;
2516 }
2517
John Hubbarda43e9822020-01-30 22:12:17 -08002518 *nr += refs;
Kirill A. Shutemove9348052017-03-16 18:26:52 +03002519 SetPageReferenced(head);
Aneesh Kumar K.Vf30c59e2014-11-05 21:57:40 +05302520 return 1;
2521}
2522
Vasily Gorbikd3f7b1b2020-09-25 21:19:10 -07002523static int gup_pmd_range(pud_t *pudp, pud_t pud, unsigned long addr, unsigned long end,
Ira Weinyb798bec2019-05-13 17:17:07 -07002524 unsigned int flags, struct page **pages, int *nr)
Steve Capper2667f502014-10-09 15:29:14 -07002525{
2526 unsigned long next;
2527 pmd_t *pmdp;
2528
Vasily Gorbikd3f7b1b2020-09-25 21:19:10 -07002529 pmdp = pmd_offset_lockless(pudp, pud, addr);
Steve Capper2667f502014-10-09 15:29:14 -07002530 do {
Christian Borntraeger38c5ce92015-01-06 22:54:46 +01002531 pmd_t pmd = READ_ONCE(*pmdp);
Steve Capper2667f502014-10-09 15:29:14 -07002532
2533 next = pmd_addr_end(addr, end);
Zi Yan84c3fc42017-09-08 16:11:01 -07002534 if (!pmd_present(pmd))
Steve Capper2667f502014-10-09 15:29:14 -07002535 return 0;
2536
Yu Zhao414fd082019-02-12 15:35:58 -08002537 if (unlikely(pmd_trans_huge(pmd) || pmd_huge(pmd) ||
2538 pmd_devmap(pmd))) {
Steve Capper2667f502014-10-09 15:29:14 -07002539 /*
2540 * NUMA hinting faults need to be handled in the GUP
2541 * slowpath for accounting purposes and so that they
2542 * can be serialised against THP migration.
2543 */
Mel Gorman8a0516e2015-02-12 14:58:22 -08002544 if (pmd_protnone(pmd))
Steve Capper2667f502014-10-09 15:29:14 -07002545 return 0;
2546
Ira Weinyb798bec2019-05-13 17:17:07 -07002547 if (!gup_huge_pmd(pmd, pmdp, addr, next, flags,
Steve Capper2667f502014-10-09 15:29:14 -07002548 pages, nr))
2549 return 0;
2550
Aneesh Kumar K.Vf30c59e2014-11-05 21:57:40 +05302551 } else if (unlikely(is_hugepd(__hugepd(pmd_val(pmd))))) {
2552 /*
2553 * architecture have different format for hugetlbfs
2554 * pmd format and THP pmd format
2555 */
2556 if (!gup_huge_pd(__hugepd(pmd_val(pmd)), addr,
Ira Weinyb798bec2019-05-13 17:17:07 -07002557 PMD_SHIFT, next, flags, pages, nr))
Aneesh Kumar K.Vf30c59e2014-11-05 21:57:40 +05302558 return 0;
Yang Shi377c60dd2022-09-07 11:01:43 -07002559 } else if (!gup_pte_range(pmd, pmdp, addr, next, flags, pages, nr))
Mario Leinweber29231172018-04-05 16:24:18 -07002560 return 0;
Steve Capper2667f502014-10-09 15:29:14 -07002561 } while (pmdp++, addr = next, addr != end);
2562
2563 return 1;
2564}
2565
Vasily Gorbikd3f7b1b2020-09-25 21:19:10 -07002566static int gup_pud_range(p4d_t *p4dp, p4d_t p4d, unsigned long addr, unsigned long end,
Ira Weinyb798bec2019-05-13 17:17:07 -07002567 unsigned int flags, struct page **pages, int *nr)
Steve Capper2667f502014-10-09 15:29:14 -07002568{
2569 unsigned long next;
2570 pud_t *pudp;
2571
Vasily Gorbikd3f7b1b2020-09-25 21:19:10 -07002572 pudp = pud_offset_lockless(p4dp, p4d, addr);
Steve Capper2667f502014-10-09 15:29:14 -07002573 do {
Christian Borntraegere37c6982014-12-07 21:41:33 +01002574 pud_t pud = READ_ONCE(*pudp);
Steve Capper2667f502014-10-09 15:29:14 -07002575
2576 next = pud_addr_end(addr, end);
Qiujun Huang154945202020-01-30 22:12:10 -08002577 if (unlikely(!pud_present(pud)))
Steve Capper2667f502014-10-09 15:29:14 -07002578 return 0;
John Starksf1cf8562022-12-06 22:00:53 -08002579 if (unlikely(pud_huge(pud) || pud_devmap(pud))) {
Ira Weinyb798bec2019-05-13 17:17:07 -07002580 if (!gup_huge_pud(pud, pudp, addr, next, flags,
Aneesh Kumar K.Vf30c59e2014-11-05 21:57:40 +05302581 pages, nr))
2582 return 0;
2583 } else if (unlikely(is_hugepd(__hugepd(pud_val(pud))))) {
2584 if (!gup_huge_pd(__hugepd(pud_val(pud)), addr,
Ira Weinyb798bec2019-05-13 17:17:07 -07002585 PUD_SHIFT, next, flags, pages, nr))
Steve Capper2667f502014-10-09 15:29:14 -07002586 return 0;
Vasily Gorbikd3f7b1b2020-09-25 21:19:10 -07002587 } else if (!gup_pmd_range(pudp, pud, addr, next, flags, pages, nr))
Steve Capper2667f502014-10-09 15:29:14 -07002588 return 0;
2589 } while (pudp++, addr = next, addr != end);
2590
2591 return 1;
2592}
2593
Vasily Gorbikd3f7b1b2020-09-25 21:19:10 -07002594static int gup_p4d_range(pgd_t *pgdp, pgd_t pgd, unsigned long addr, unsigned long end,
Ira Weinyb798bec2019-05-13 17:17:07 -07002595 unsigned int flags, struct page **pages, int *nr)
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +03002596{
2597 unsigned long next;
2598 p4d_t *p4dp;
2599
Vasily Gorbikd3f7b1b2020-09-25 21:19:10 -07002600 p4dp = p4d_offset_lockless(pgdp, pgd, addr);
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +03002601 do {
2602 p4d_t p4d = READ_ONCE(*p4dp);
2603
2604 next = p4d_addr_end(addr, end);
2605 if (p4d_none(p4d))
2606 return 0;
2607 BUILD_BUG_ON(p4d_huge(p4d));
2608 if (unlikely(is_hugepd(__hugepd(p4d_val(p4d))))) {
2609 if (!gup_huge_pd(__hugepd(p4d_val(p4d)), addr,
Ira Weinyb798bec2019-05-13 17:17:07 -07002610 P4D_SHIFT, next, flags, pages, nr))
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +03002611 return 0;
Vasily Gorbikd3f7b1b2020-09-25 21:19:10 -07002612 } else if (!gup_pud_range(p4dp, p4d, addr, next, flags, pages, nr))
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +03002613 return 0;
2614 } while (p4dp++, addr = next, addr != end);
2615
2616 return 1;
2617}
2618
Kirill A. Shutemov5b65c4672017-09-09 00:56:03 +03002619static void gup_pgd_range(unsigned long addr, unsigned long end,
Ira Weinyb798bec2019-05-13 17:17:07 -07002620 unsigned int flags, struct page **pages, int *nr)
Kirill A. Shutemov5b65c4672017-09-09 00:56:03 +03002621{
2622 unsigned long next;
2623 pgd_t *pgdp;
2624
2625 pgdp = pgd_offset(current->mm, addr);
2626 do {
2627 pgd_t pgd = READ_ONCE(*pgdp);
2628
2629 next = pgd_addr_end(addr, end);
2630 if (pgd_none(pgd))
2631 return;
2632 if (unlikely(pgd_huge(pgd))) {
Ira Weinyb798bec2019-05-13 17:17:07 -07002633 if (!gup_huge_pgd(pgd, pgdp, addr, next, flags,
Kirill A. Shutemov5b65c4672017-09-09 00:56:03 +03002634 pages, nr))
2635 return;
2636 } else if (unlikely(is_hugepd(__hugepd(pgd_val(pgd))))) {
2637 if (!gup_huge_pd(__hugepd(pgd_val(pgd)), addr,
Ira Weinyb798bec2019-05-13 17:17:07 -07002638 PGDIR_SHIFT, next, flags, pages, nr))
Kirill A. Shutemov5b65c4672017-09-09 00:56:03 +03002639 return;
Vasily Gorbikd3f7b1b2020-09-25 21:19:10 -07002640 } else if (!gup_p4d_range(pgdp, pgd, addr, next, flags, pages, nr))
Kirill A. Shutemov5b65c4672017-09-09 00:56:03 +03002641 return;
2642 } while (pgdp++, addr = next, addr != end);
2643}
Christoph Hellwig050a9ad2019-07-11 20:57:21 -07002644#else
2645static inline void gup_pgd_range(unsigned long addr, unsigned long end,
2646 unsigned int flags, struct page **pages, int *nr)
2647{
2648}
2649#endif /* CONFIG_HAVE_FAST_GUP */
Kirill A. Shutemov5b65c4672017-09-09 00:56:03 +03002650
2651#ifndef gup_fast_permitted
2652/*
Souptick Joarderdadbb612020-06-07 21:40:55 -07002653 * Check if it's allowed to use get_user_pages_fast_only() for the range, or
Kirill A. Shutemov5b65c4672017-09-09 00:56:03 +03002654 * we need to fall back to the slow version:
2655 */
Christoph Hellwig26f4c322019-07-11 20:56:45 -07002656static bool gup_fast_permitted(unsigned long start, unsigned long end)
Kirill A. Shutemov5b65c4672017-09-09 00:56:03 +03002657{
Christoph Hellwig26f4c322019-07-11 20:56:45 -07002658 return true;
Kirill A. Shutemov5b65c4672017-09-09 00:56:03 +03002659}
2660#endif
2661
Ira Weiny7af75562019-05-13 17:17:14 -07002662static int __gup_longterm_unlocked(unsigned long start, int nr_pages,
2663 unsigned int gup_flags, struct page **pages)
2664{
2665 int ret;
2666
2667 /*
2668 * FIXME: FOLL_LONGTERM does not work with
2669 * get_user_pages_unlocked() (see comments in that function)
2670 */
2671 if (gup_flags & FOLL_LONGTERM) {
Michel Lespinassed8ed45c2020-06-08 21:33:25 -07002672 mmap_read_lock(current->mm);
Peter Xu64019a22020-08-11 18:39:01 -07002673 ret = __gup_longterm_locked(current->mm,
Ira Weiny7af75562019-05-13 17:17:14 -07002674 start, nr_pages,
2675 pages, NULL, gup_flags);
Michel Lespinassed8ed45c2020-06-08 21:33:25 -07002676 mmap_read_unlock(current->mm);
Ira Weiny7af75562019-05-13 17:17:14 -07002677 } else {
2678 ret = get_user_pages_unlocked(start, nr_pages,
2679 pages, gup_flags);
2680 }
2681
2682 return ret;
2683}
2684
Jason Gunthorpebcb0f642020-12-14 19:05:41 -08002685static unsigned long lockless_pages_from_mm(unsigned long start,
2686 unsigned long end,
2687 unsigned int gup_flags,
2688 struct page **pages)
2689{
2690 unsigned long flags;
2691 int nr_pinned = 0;
Jason Gunthorpe53794652020-12-14 19:05:44 -08002692 unsigned seq;
Jason Gunthorpebcb0f642020-12-14 19:05:41 -08002693
2694 if (!IS_ENABLED(CONFIG_HAVE_FAST_GUP) ||
2695 !gup_fast_permitted(start, end))
2696 return 0;
2697
Jason Gunthorpe53794652020-12-14 19:05:44 -08002698 if (gup_flags & FOLL_PIN) {
2699 seq = raw_read_seqcount(&current->mm->write_protect_seq);
2700 if (seq & 1)
2701 return 0;
2702 }
2703
Jason Gunthorpebcb0f642020-12-14 19:05:41 -08002704 /*
2705 * Disable interrupts. The nested form is used, in order to allow full,
2706 * general purpose use of this routine.
2707 *
2708 * With interrupts disabled, we block page table pages from being freed
2709 * from under us. See struct mmu_table_batch comments in
2710 * include/asm-generic/tlb.h for more details.
2711 *
2712 * We do not adopt an rcu_read_lock() here as we also want to block IPIs
2713 * that come from THPs splitting.
2714 */
2715 local_irq_save(flags);
2716 gup_pgd_range(start, end, gup_flags, pages, &nr_pinned);
2717 local_irq_restore(flags);
Jason Gunthorpe53794652020-12-14 19:05:44 -08002718
2719 /*
2720 * When pinning pages for DMA there could be a concurrent write protect
2721 * from fork() via copy_page_range(), in this case always fail fast GUP.
2722 */
2723 if (gup_flags & FOLL_PIN) {
2724 if (read_seqcount_retry(&current->mm->write_protect_seq, seq)) {
2725 unpin_user_pages(pages, nr_pinned);
2726 return 0;
2727 }
2728 }
Jason Gunthorpebcb0f642020-12-14 19:05:41 -08002729 return nr_pinned;
2730}
2731
2732static int internal_get_user_pages_fast(unsigned long start,
2733 unsigned long nr_pages,
John Hubbardeddb1c22020-01-30 22:12:54 -08002734 unsigned int gup_flags,
2735 struct page **pages)
Steve Capper2667f502014-10-09 15:29:14 -07002736{
Jason Gunthorpebcb0f642020-12-14 19:05:41 -08002737 unsigned long len, end;
2738 unsigned long nr_pinned;
2739 int ret;
Steve Capper2667f502014-10-09 15:29:14 -07002740
John Hubbardf4000fd2020-01-30 22:12:43 -08002741 if (WARN_ON_ONCE(gup_flags & ~(FOLL_WRITE | FOLL_LONGTERM |
John Hubbard376a34ef2020-06-03 15:56:30 -07002742 FOLL_FORCE | FOLL_PIN | FOLL_GET |
2743 FOLL_FAST_ONLY)))
Christoph Hellwig817be122019-07-11 20:57:25 -07002744 return -EINVAL;
2745
Peter Xu008cfe42020-09-25 18:25:57 -04002746 if (gup_flags & FOLL_PIN)
2747 atomic_set(&current->mm->has_pinned, 1);
2748
John Hubbardf81cd172020-06-03 15:56:40 -07002749 if (!(gup_flags & FOLL_FAST_ONLY))
Michel Lespinasseda1c55f2020-06-08 21:33:47 -07002750 might_lock_read(&current->mm->mmap_lock);
John Hubbardf81cd172020-06-03 15:56:40 -07002751
Christoph Hellwigf455c8542019-07-11 20:56:41 -07002752 start = untagged_addr(start) & PAGE_MASK;
Jason Gunthorpebcb0f642020-12-14 19:05:41 -08002753 len = nr_pages << PAGE_SHIFT;
2754 if (check_add_overflow(start, len, &end))
Michael S. Tsirkinc61611f2018-04-13 15:35:20 -07002755 return 0;
Linus Torvalds96d4f262019-01-03 18:57:57 -08002756 if (unlikely(!access_ok((void __user *)start, len)))
Michael S. Tsirkinc61611f2018-04-13 15:35:20 -07002757 return -EFAULT;
Kirill A. Shutemov73e10a62017-03-16 18:26:54 +03002758
Jason Gunthorpebcb0f642020-12-14 19:05:41 -08002759 nr_pinned = lockless_pages_from_mm(start, end, gup_flags, pages);
2760 if (nr_pinned == nr_pages || gup_flags & FOLL_FAST_ONLY)
2761 return nr_pinned;
John Hubbard376a34ef2020-06-03 15:56:30 -07002762
Jason Gunthorpebcb0f642020-12-14 19:05:41 -08002763 /* Slow path: try to get the remaining pages with get_user_pages */
2764 start += nr_pinned << PAGE_SHIFT;
2765 pages += nr_pinned;
2766 ret = __gup_longterm_unlocked(start, nr_pages - nr_pinned, gup_flags,
2767 pages);
2768 if (ret < 0) {
2769 /*
2770 * The caller has to unpin the pages we already pinned so
2771 * returning -errno is not an option
2772 */
2773 if (nr_pinned)
2774 return nr_pinned;
2775 return ret;
Kirill A. Shutemov73e10a62017-03-16 18:26:54 +03002776 }
Jason Gunthorpebcb0f642020-12-14 19:05:41 -08002777 return ret + nr_pinned;
Steve Capper2667f502014-10-09 15:29:14 -07002778}
Jason Gunthorpebcb0f642020-12-14 19:05:41 -08002779
Souptick Joarderdadbb612020-06-07 21:40:55 -07002780/**
2781 * get_user_pages_fast_only() - pin user pages in memory
2782 * @start: starting user address
2783 * @nr_pages: number of pages from start to pin
2784 * @gup_flags: flags modifying pin behaviour
2785 * @pages: array that receives pointers to the pages pinned.
2786 * Should be at least nr_pages long.
2787 *
John Hubbard9e1f0582020-06-03 15:56:27 -07002788 * Like get_user_pages_fast() except it's IRQ-safe in that it won't fall back to
2789 * the regular GUP.
2790 * Note a difference with get_user_pages_fast: this always returns the
2791 * number of pages pinned, 0 if no pages were pinned.
2792 *
2793 * If the architecture does not support this function, simply return with no
2794 * pages pinned.
2795 *
2796 * Careful, careful! COW breaking can go either way, so a non-write
2797 * access can get ambiguous page results. If you call this function without
2798 * 'write' set, you'd better be sure that you're ok with that ambiguity.
2799 */
Souptick Joarderdadbb612020-06-07 21:40:55 -07002800int get_user_pages_fast_only(unsigned long start, int nr_pages,
2801 unsigned int gup_flags, struct page **pages)
John Hubbard9e1f0582020-06-03 15:56:27 -07002802{
John Hubbard376a34ef2020-06-03 15:56:30 -07002803 int nr_pinned;
John Hubbard9e1f0582020-06-03 15:56:27 -07002804 /*
2805 * Internally (within mm/gup.c), gup fast variants must set FOLL_GET,
2806 * because gup fast is always a "pin with a +1 page refcount" request.
John Hubbard376a34ef2020-06-03 15:56:30 -07002807 *
2808 * FOLL_FAST_ONLY is required in order to match the API description of
2809 * this routine: no fall back to regular ("slow") GUP.
John Hubbard9e1f0582020-06-03 15:56:27 -07002810 */
Souptick Joarderdadbb612020-06-07 21:40:55 -07002811 gup_flags |= FOLL_GET | FOLL_FAST_ONLY;
John Hubbard9e1f0582020-06-03 15:56:27 -07002812
John Hubbard376a34ef2020-06-03 15:56:30 -07002813 nr_pinned = internal_get_user_pages_fast(start, nr_pages, gup_flags,
2814 pages);
John Hubbard9e1f0582020-06-03 15:56:27 -07002815
2816 /*
John Hubbard376a34ef2020-06-03 15:56:30 -07002817 * As specified in the API description above, this routine is not
2818 * allowed to return negative values. However, the common core
2819 * routine internal_get_user_pages_fast() *can* return -errno.
2820 * Therefore, correct for that here:
John Hubbard9e1f0582020-06-03 15:56:27 -07002821 */
John Hubbard376a34ef2020-06-03 15:56:30 -07002822 if (nr_pinned < 0)
2823 nr_pinned = 0;
John Hubbard9e1f0582020-06-03 15:56:27 -07002824
2825 return nr_pinned;
2826}
Souptick Joarderdadbb612020-06-07 21:40:55 -07002827EXPORT_SYMBOL_GPL(get_user_pages_fast_only);
John Hubbard9e1f0582020-06-03 15:56:27 -07002828
John Hubbardeddb1c22020-01-30 22:12:54 -08002829/**
2830 * get_user_pages_fast() - pin user pages in memory
John Hubbard3faa52c2020-04-01 21:05:29 -07002831 * @start: starting user address
2832 * @nr_pages: number of pages from start to pin
2833 * @gup_flags: flags modifying pin behaviour
2834 * @pages: array that receives pointers to the pages pinned.
2835 * Should be at least nr_pages long.
John Hubbardeddb1c22020-01-30 22:12:54 -08002836 *
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -07002837 * Attempt to pin user pages in memory without taking mm->mmap_lock.
John Hubbardeddb1c22020-01-30 22:12:54 -08002838 * If not successful, it will fall back to taking the lock and
2839 * calling get_user_pages().
2840 *
2841 * Returns number of pages pinned. This may be fewer than the number requested.
2842 * If nr_pages is 0 or negative, returns 0. If no pages were pinned, returns
2843 * -errno.
2844 */
2845int get_user_pages_fast(unsigned long start, int nr_pages,
2846 unsigned int gup_flags, struct page **pages)
2847{
Barry Song447f3e42020-10-13 16:51:58 -07002848 if (!is_valid_gup_flags(gup_flags))
John Hubbardeddb1c22020-01-30 22:12:54 -08002849 return -EINVAL;
2850
John Hubbard94202f12020-04-01 21:05:25 -07002851 /*
2852 * The caller may or may not have explicitly set FOLL_GET; either way is
2853 * OK. However, internally (within mm/gup.c), gup fast variants must set
2854 * FOLL_GET, because gup fast is always a "pin with a +1 page refcount"
2855 * request.
2856 */
2857 gup_flags |= FOLL_GET;
John Hubbardeddb1c22020-01-30 22:12:54 -08002858 return internal_get_user_pages_fast(start, nr_pages, gup_flags, pages);
2859}
Christoph Hellwig050a9ad2019-07-11 20:57:21 -07002860EXPORT_SYMBOL_GPL(get_user_pages_fast);
John Hubbardeddb1c22020-01-30 22:12:54 -08002861
2862/**
2863 * pin_user_pages_fast() - pin user pages in memory without taking locks
2864 *
John Hubbard3faa52c2020-04-01 21:05:29 -07002865 * @start: starting user address
2866 * @nr_pages: number of pages from start to pin
2867 * @gup_flags: flags modifying pin behaviour
2868 * @pages: array that receives pointers to the pages pinned.
2869 * Should be at least nr_pages long.
2870 *
2871 * Nearly the same as get_user_pages_fast(), except that FOLL_PIN is set. See
2872 * get_user_pages_fast() for documentation on the function arguments, because
2873 * the arguments here are identical.
2874 *
2875 * FOLL_PIN means that the pages must be released via unpin_user_page(). Please
Mauro Carvalho Chehab72ef5e52020-04-14 18:48:35 +02002876 * see Documentation/core-api/pin_user_pages.rst for further details.
John Hubbardeddb1c22020-01-30 22:12:54 -08002877 */
2878int pin_user_pages_fast(unsigned long start, int nr_pages,
2879 unsigned int gup_flags, struct page **pages)
2880{
John Hubbard3faa52c2020-04-01 21:05:29 -07002881 /* FOLL_GET and FOLL_PIN are mutually exclusive. */
2882 if (WARN_ON_ONCE(gup_flags & FOLL_GET))
2883 return -EINVAL;
2884
2885 gup_flags |= FOLL_PIN;
2886 return internal_get_user_pages_fast(start, nr_pages, gup_flags, pages);
John Hubbardeddb1c22020-01-30 22:12:54 -08002887}
2888EXPORT_SYMBOL_GPL(pin_user_pages_fast);
2889
John Hubbard104acc32020-06-03 15:56:34 -07002890/*
Souptick Joarderdadbb612020-06-07 21:40:55 -07002891 * This is the FOLL_PIN equivalent of get_user_pages_fast_only(). Behavior
2892 * is the same, except that this one sets FOLL_PIN instead of FOLL_GET.
John Hubbard104acc32020-06-03 15:56:34 -07002893 *
2894 * The API rules are the same, too: no negative values may be returned.
2895 */
2896int pin_user_pages_fast_only(unsigned long start, int nr_pages,
2897 unsigned int gup_flags, struct page **pages)
2898{
2899 int nr_pinned;
2900
2901 /*
2902 * FOLL_GET and FOLL_PIN are mutually exclusive. Note that the API
2903 * rules require returning 0, rather than -errno:
2904 */
2905 if (WARN_ON_ONCE(gup_flags & FOLL_GET))
2906 return 0;
2907 /*
2908 * FOLL_FAST_ONLY is required in order to match the API description of
2909 * this routine: no fall back to regular ("slow") GUP.
2910 */
2911 gup_flags |= (FOLL_PIN | FOLL_FAST_ONLY);
2912 nr_pinned = internal_get_user_pages_fast(start, nr_pages, gup_flags,
2913 pages);
2914 /*
2915 * This routine is not allowed to return negative values. However,
2916 * internal_get_user_pages_fast() *can* return -errno. Therefore,
2917 * correct for that here:
2918 */
2919 if (nr_pinned < 0)
2920 nr_pinned = 0;
2921
2922 return nr_pinned;
2923}
2924EXPORT_SYMBOL_GPL(pin_user_pages_fast_only);
2925
John Hubbardeddb1c22020-01-30 22:12:54 -08002926/**
Peter Xu64019a22020-08-11 18:39:01 -07002927 * pin_user_pages_remote() - pin pages of a remote process
John Hubbardeddb1c22020-01-30 22:12:54 -08002928 *
John Hubbard3faa52c2020-04-01 21:05:29 -07002929 * @mm: mm_struct of target mm
2930 * @start: starting user address
2931 * @nr_pages: number of pages from start to pin
2932 * @gup_flags: flags modifying lookup behaviour
2933 * @pages: array that receives pointers to the pages pinned.
2934 * Should be at least nr_pages long. Or NULL, if caller
2935 * only intends to ensure the pages are faulted in.
2936 * @vmas: array of pointers to vmas corresponding to each page.
2937 * Or NULL if the caller does not require them.
2938 * @locked: pointer to lock flag indicating whether lock is held and
2939 * subsequently whether VM_FAULT_RETRY functionality can be
2940 * utilised. Lock must initially be held.
2941 *
2942 * Nearly the same as get_user_pages_remote(), except that FOLL_PIN is set. See
2943 * get_user_pages_remote() for documentation on the function arguments, because
2944 * the arguments here are identical.
2945 *
2946 * FOLL_PIN means that the pages must be released via unpin_user_page(). Please
Mauro Carvalho Chehab72ef5e52020-04-14 18:48:35 +02002947 * see Documentation/core-api/pin_user_pages.rst for details.
John Hubbardeddb1c22020-01-30 22:12:54 -08002948 */
Peter Xu64019a22020-08-11 18:39:01 -07002949long pin_user_pages_remote(struct mm_struct *mm,
John Hubbardeddb1c22020-01-30 22:12:54 -08002950 unsigned long start, unsigned long nr_pages,
2951 unsigned int gup_flags, struct page **pages,
2952 struct vm_area_struct **vmas, int *locked)
2953{
John Hubbard3faa52c2020-04-01 21:05:29 -07002954 /* FOLL_GET and FOLL_PIN are mutually exclusive. */
2955 if (WARN_ON_ONCE(gup_flags & FOLL_GET))
2956 return -EINVAL;
2957
2958 gup_flags |= FOLL_PIN;
Peter Xu64019a22020-08-11 18:39:01 -07002959 return __get_user_pages_remote(mm, start, nr_pages, gup_flags,
John Hubbard3faa52c2020-04-01 21:05:29 -07002960 pages, vmas, locked);
John Hubbardeddb1c22020-01-30 22:12:54 -08002961}
2962EXPORT_SYMBOL(pin_user_pages_remote);
2963
2964/**
2965 * pin_user_pages() - pin user pages in memory for use by other devices
2966 *
John Hubbard3faa52c2020-04-01 21:05:29 -07002967 * @start: starting user address
2968 * @nr_pages: number of pages from start to pin
2969 * @gup_flags: flags modifying lookup behaviour
2970 * @pages: array that receives pointers to the pages pinned.
2971 * Should be at least nr_pages long. Or NULL, if caller
2972 * only intends to ensure the pages are faulted in.
2973 * @vmas: array of pointers to vmas corresponding to each page.
2974 * Or NULL if the caller does not require them.
2975 *
2976 * Nearly the same as get_user_pages(), except that FOLL_TOUCH is not set, and
2977 * FOLL_PIN is set.
2978 *
2979 * FOLL_PIN means that the pages must be released via unpin_user_page(). Please
Mauro Carvalho Chehab72ef5e52020-04-14 18:48:35 +02002980 * see Documentation/core-api/pin_user_pages.rst for details.
John Hubbardeddb1c22020-01-30 22:12:54 -08002981 */
2982long pin_user_pages(unsigned long start, unsigned long nr_pages,
2983 unsigned int gup_flags, struct page **pages,
2984 struct vm_area_struct **vmas)
2985{
John Hubbard3faa52c2020-04-01 21:05:29 -07002986 /* FOLL_GET and FOLL_PIN are mutually exclusive. */
2987 if (WARN_ON_ONCE(gup_flags & FOLL_GET))
2988 return -EINVAL;
2989
2990 gup_flags |= FOLL_PIN;
Peter Xu64019a22020-08-11 18:39:01 -07002991 return __gup_longterm_locked(current->mm, start, nr_pages,
John Hubbard3faa52c2020-04-01 21:05:29 -07002992 pages, vmas, gup_flags);
John Hubbardeddb1c22020-01-30 22:12:54 -08002993}
2994EXPORT_SYMBOL(pin_user_pages);
John Hubbard91429022020-06-01 21:48:27 -07002995
2996/*
2997 * pin_user_pages_unlocked() is the FOLL_PIN variant of
2998 * get_user_pages_unlocked(). Behavior is the same, except that this one sets
2999 * FOLL_PIN and rejects FOLL_GET.
3000 */
3001long pin_user_pages_unlocked(unsigned long start, unsigned long nr_pages,
3002 struct page **pages, unsigned int gup_flags)
3003{
3004 /* FOLL_GET and FOLL_PIN are mutually exclusive. */
3005 if (WARN_ON_ONCE(gup_flags & FOLL_GET))
3006 return -EINVAL;
3007
3008 gup_flags |= FOLL_PIN;
3009 return get_user_pages_unlocked(start, nr_pages, pages, gup_flags);
3010}
3011EXPORT_SYMBOL(pin_user_pages_unlocked);
John Hubbard420c2092020-06-07 21:41:02 -07003012
3013/*
3014 * pin_user_pages_locked() is the FOLL_PIN variant of get_user_pages_locked().
3015 * Behavior is the same, except that this one sets FOLL_PIN and rejects
3016 * FOLL_GET.
3017 */
3018long pin_user_pages_locked(unsigned long start, unsigned long nr_pages,
3019 unsigned int gup_flags, struct page **pages,
3020 int *locked)
3021{
3022 /*
3023 * FIXME: Current FOLL_LONGTERM behavior is incompatible with
3024 * FAULT_FLAG_ALLOW_RETRY because of the FS DAX check requirement on
3025 * vmas. As there are no users of this flag in this call we simply
3026 * disallow this option for now.
3027 */
3028 if (WARN_ON_ONCE(gup_flags & FOLL_LONGTERM))
3029 return -EINVAL;
3030
3031 /* FOLL_GET and FOLL_PIN are mutually exclusive. */
3032 if (WARN_ON_ONCE(gup_flags & FOLL_GET))
3033 return -EINVAL;
3034
3035 gup_flags |= FOLL_PIN;
Peter Xu64019a22020-08-11 18:39:01 -07003036 return __get_user_pages_locked(current->mm, start, nr_pages,
John Hubbard420c2092020-06-07 21:41:02 -07003037 pages, NULL, locked,
3038 gup_flags | FOLL_TOUCH);
3039}
3040EXPORT_SYMBOL(pin_user_pages_locked);