blob: 6601df4c76828e3663b084026806eeeb918a9e00 [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>
Steve Capper2667f502014-10-09 15:29:14 -070022#include <asm/pgtable.h>
Kirill A. Shutemov1027e442015-09-04 15:47:55 -070023#include <asm/tlbflush.h>
Steve Capper2667f502014-10-09 15:29:14 -070024
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -070025#include "internal.h"
26
Keith Buschdf06b372018-10-26 15:10:28 -070027struct follow_page_context {
28 struct dev_pagemap *pgmap;
29 unsigned int page_mask;
30};
31
John Hubbard47e29d32020-04-01 21:05:33 -070032static void hpage_pincount_add(struct page *page, int refs)
33{
34 VM_BUG_ON_PAGE(!hpage_pincount_available(page), page);
35 VM_BUG_ON_PAGE(page != compound_head(page), page);
36
37 atomic_add(refs, compound_pincount_ptr(page));
38}
39
40static void hpage_pincount_sub(struct page *page, int refs)
41{
42 VM_BUG_ON_PAGE(!hpage_pincount_available(page), page);
43 VM_BUG_ON_PAGE(page != compound_head(page), page);
44
45 atomic_sub(refs, compound_pincount_ptr(page));
46}
47
John Hubbarda707cdd2020-01-30 22:12:21 -080048/*
49 * Return the compound head page with ref appropriately incremented,
50 * or NULL if that failed.
51 */
52static inline struct page *try_get_compound_head(struct page *page, int refs)
53{
54 struct page *head = compound_head(page);
55
56 if (WARN_ON_ONCE(page_ref_count(head) < 0))
57 return NULL;
58 if (unlikely(!page_cache_add_speculative(head, refs)))
59 return NULL;
60 return head;
61}
62
John Hubbard3faa52c2020-04-01 21:05:29 -070063/*
64 * try_grab_compound_head() - attempt to elevate a page's refcount, by a
65 * flags-dependent amount.
66 *
67 * "grab" names in this file mean, "look at flags to decide whether to use
68 * FOLL_PIN or FOLL_GET behavior, when incrementing the page's refcount.
69 *
70 * Either FOLL_PIN or FOLL_GET (or neither) must be set, but not both at the
71 * same time. (That's true throughout the get_user_pages*() and
72 * pin_user_pages*() APIs.) Cases:
73 *
74 * FOLL_GET: page's refcount will be incremented by 1.
75 * FOLL_PIN: page's refcount will be incremented by GUP_PIN_COUNTING_BIAS.
76 *
77 * Return: head page (with refcount appropriately incremented) for success, or
78 * NULL upon failure. If neither FOLL_GET nor FOLL_PIN was set, that's
79 * considered failure, and furthermore, a likely bug in the caller, so a warning
80 * is also emitted.
81 */
82static __maybe_unused struct page *try_grab_compound_head(struct page *page,
83 int refs,
84 unsigned int flags)
85{
86 if (flags & FOLL_GET)
87 return try_get_compound_head(page, refs);
88 else if (flags & FOLL_PIN) {
John Hubbard47e29d32020-04-01 21:05:33 -070089 /*
90 * When pinning a compound page of order > 1 (which is what
91 * hpage_pincount_available() checks for), use an exact count to
92 * track it, via hpage_pincount_add/_sub().
93 *
94 * However, be sure to *also* increment the normal page refcount
95 * field at least once, so that the page really is pinned.
96 */
97 if (!hpage_pincount_available(page))
98 refs *= GUP_PIN_COUNTING_BIAS;
99
100 page = try_get_compound_head(page, refs);
101 if (!page)
102 return NULL;
103
104 if (hpage_pincount_available(page))
105 hpage_pincount_add(page, refs);
106
107 return page;
John Hubbard3faa52c2020-04-01 21:05:29 -0700108 }
109
110 WARN_ON_ONCE(1);
111 return NULL;
112}
113
114/**
115 * try_grab_page() - elevate a page's refcount by a flag-dependent amount
116 *
117 * This might not do anything at all, depending on the flags argument.
118 *
119 * "grab" names in this file mean, "look at flags to decide whether to use
120 * FOLL_PIN or FOLL_GET behavior, when incrementing the page's refcount.
121 *
122 * @page: pointer to page to be grabbed
123 * @flags: gup flags: these are the FOLL_* flag values.
124 *
125 * Either FOLL_PIN or FOLL_GET (or neither) may be set, but not both at the same
126 * time. Cases:
127 *
128 * FOLL_GET: page's refcount will be incremented by 1.
129 * FOLL_PIN: page's refcount will be incremented by GUP_PIN_COUNTING_BIAS.
130 *
131 * Return: true for success, or if no action was required (if neither FOLL_PIN
132 * nor FOLL_GET was set, nothing is done). False for failure: FOLL_GET or
133 * FOLL_PIN was set, but the page could not be grabbed.
134 */
135bool __must_check try_grab_page(struct page *page, unsigned int flags)
136{
137 WARN_ON_ONCE((flags & (FOLL_GET | FOLL_PIN)) == (FOLL_GET | FOLL_PIN));
138
139 if (flags & FOLL_GET)
140 return try_get_page(page);
141 else if (flags & FOLL_PIN) {
John Hubbard47e29d32020-04-01 21:05:33 -0700142 int refs = 1;
143
John Hubbard3faa52c2020-04-01 21:05:29 -0700144 page = compound_head(page);
145
146 if (WARN_ON_ONCE(page_ref_count(page) <= 0))
147 return false;
148
John Hubbard47e29d32020-04-01 21:05:33 -0700149 if (hpage_pincount_available(page))
150 hpage_pincount_add(page, 1);
151 else
152 refs = GUP_PIN_COUNTING_BIAS;
153
154 /*
155 * Similar to try_grab_compound_head(): even if using the
156 * hpage_pincount_add/_sub() routines, be sure to
157 * *also* increment the normal page refcount field at least
158 * once, so that the page really is pinned.
159 */
160 page_ref_add(page, refs);
John Hubbard3faa52c2020-04-01 21:05:29 -0700161 }
162
163 return true;
164}
165
166#ifdef CONFIG_DEV_PAGEMAP_OPS
167static bool __unpin_devmap_managed_user_page(struct page *page)
168{
John Hubbard47e29d32020-04-01 21:05:33 -0700169 int count, refs = 1;
John Hubbard3faa52c2020-04-01 21:05:29 -0700170
171 if (!page_is_devmap_managed(page))
172 return false;
173
John Hubbard47e29d32020-04-01 21:05:33 -0700174 if (hpage_pincount_available(page))
175 hpage_pincount_sub(page, 1);
176 else
177 refs = GUP_PIN_COUNTING_BIAS;
178
179 count = page_ref_sub_return(page, refs);
John Hubbard3faa52c2020-04-01 21:05:29 -0700180
181 /*
182 * devmap page refcounts are 1-based, rather than 0-based: if
183 * refcount is 1, then the page is free and the refcount is
184 * stable because nobody holds a reference on the page.
185 */
186 if (count == 1)
187 free_devmap_managed_page(page);
188 else if (!count)
189 __put_page(page);
190
191 return true;
192}
193#else
194static bool __unpin_devmap_managed_user_page(struct page *page)
195{
196 return false;
197}
198#endif /* CONFIG_DEV_PAGEMAP_OPS */
199
200/**
201 * unpin_user_page() - release a dma-pinned page
202 * @page: pointer to page to be released
203 *
204 * Pages that were pinned via pin_user_pages*() must be released via either
205 * unpin_user_page(), or one of the unpin_user_pages*() routines. This is so
206 * that such pages can be separately tracked and uniquely handled. In
207 * particular, interactions with RDMA and filesystems need special handling.
208 */
209void unpin_user_page(struct page *page)
210{
John Hubbard47e29d32020-04-01 21:05:33 -0700211 int refs = 1;
212
John Hubbard3faa52c2020-04-01 21:05:29 -0700213 page = compound_head(page);
214
215 /*
216 * For devmap managed pages we need to catch refcount transition from
217 * GUP_PIN_COUNTING_BIAS to 1, when refcount reach one it means the
218 * page is free and we need to inform the device driver through
219 * callback. See include/linux/memremap.h and HMM for details.
220 */
221 if (__unpin_devmap_managed_user_page(page))
222 return;
223
John Hubbard47e29d32020-04-01 21:05:33 -0700224 if (hpage_pincount_available(page))
225 hpage_pincount_sub(page, 1);
226 else
227 refs = GUP_PIN_COUNTING_BIAS;
228
229 if (page_ref_sub_and_test(page, refs))
John Hubbard3faa52c2020-04-01 21:05:29 -0700230 __put_page(page);
231}
232EXPORT_SYMBOL(unpin_user_page);
233
John Hubbardfc1d8e72019-05-13 17:19:08 -0700234/**
John Hubbardf1f6a7d2020-01-30 22:13:35 -0800235 * unpin_user_pages_dirty_lock() - release and optionally dirty gup-pinned pages
akpm@linux-foundation.org2d15eb32019-09-23 15:35:04 -0700236 * @pages: array of pages to be maybe marked dirty, and definitely released.
John Hubbardfc1d8e72019-05-13 17:19:08 -0700237 * @npages: number of pages in the @pages array.
akpm@linux-foundation.org2d15eb32019-09-23 15:35:04 -0700238 * @make_dirty: whether to mark the pages dirty
John Hubbardfc1d8e72019-05-13 17:19:08 -0700239 *
240 * "gup-pinned page" refers to a page that has had one of the get_user_pages()
241 * variants called on that page.
242 *
243 * 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 -0700244 * compound page) dirty, if @make_dirty is true, and if the page was previously
John Hubbardf1f6a7d2020-01-30 22:13:35 -0800245 * listed as clean. In any case, releases all pages using unpin_user_page(),
246 * possibly via unpin_user_pages(), for the non-dirty case.
John Hubbardfc1d8e72019-05-13 17:19:08 -0700247 *
John Hubbardf1f6a7d2020-01-30 22:13:35 -0800248 * Please see the unpin_user_page() documentation for details.
John Hubbardfc1d8e72019-05-13 17:19:08 -0700249 *
akpm@linux-foundation.org2d15eb32019-09-23 15:35:04 -0700250 * set_page_dirty_lock() is used internally. If instead, set_page_dirty() is
251 * required, then the caller should a) verify that this is really correct,
252 * because _lock() is usually required, and b) hand code it:
John Hubbardf1f6a7d2020-01-30 22:13:35 -0800253 * set_page_dirty_lock(), unpin_user_page().
John Hubbardfc1d8e72019-05-13 17:19:08 -0700254 *
255 */
John Hubbardf1f6a7d2020-01-30 22:13:35 -0800256void unpin_user_pages_dirty_lock(struct page **pages, unsigned long npages,
257 bool make_dirty)
John Hubbardfc1d8e72019-05-13 17:19:08 -0700258{
akpm@linux-foundation.org2d15eb32019-09-23 15:35:04 -0700259 unsigned long index;
John Hubbardfc1d8e72019-05-13 17:19:08 -0700260
akpm@linux-foundation.org2d15eb32019-09-23 15:35:04 -0700261 /*
262 * TODO: this can be optimized for huge pages: if a series of pages is
263 * physically contiguous and part of the same compound page, then a
264 * single operation to the head page should suffice.
265 */
266
267 if (!make_dirty) {
John Hubbardf1f6a7d2020-01-30 22:13:35 -0800268 unpin_user_pages(pages, npages);
akpm@linux-foundation.org2d15eb32019-09-23 15:35:04 -0700269 return;
270 }
271
272 for (index = 0; index < npages; index++) {
273 struct page *page = compound_head(pages[index]);
274 /*
275 * Checking PageDirty at this point may race with
276 * clear_page_dirty_for_io(), but that's OK. Two key
277 * cases:
278 *
279 * 1) This code sees the page as already dirty, so it
280 * skips the call to set_page_dirty(). That could happen
281 * because clear_page_dirty_for_io() called
282 * page_mkclean(), followed by set_page_dirty().
283 * However, now the page is going to get written back,
284 * which meets the original intention of setting it
285 * dirty, so all is well: clear_page_dirty_for_io() goes
286 * on to call TestClearPageDirty(), and write the page
287 * back.
288 *
289 * 2) This code sees the page as clean, so it calls
290 * set_page_dirty(). The page stays dirty, despite being
291 * written back, so it gets written back again in the
292 * next writeback cycle. This is harmless.
293 */
294 if (!PageDirty(page))
295 set_page_dirty_lock(page);
John Hubbardf1f6a7d2020-01-30 22:13:35 -0800296 unpin_user_page(page);
akpm@linux-foundation.org2d15eb32019-09-23 15:35:04 -0700297 }
John Hubbardfc1d8e72019-05-13 17:19:08 -0700298}
John Hubbardf1f6a7d2020-01-30 22:13:35 -0800299EXPORT_SYMBOL(unpin_user_pages_dirty_lock);
John Hubbardfc1d8e72019-05-13 17:19:08 -0700300
301/**
John Hubbardf1f6a7d2020-01-30 22:13:35 -0800302 * unpin_user_pages() - release an array of gup-pinned pages.
John Hubbardfc1d8e72019-05-13 17:19:08 -0700303 * @pages: array of pages to be marked dirty and released.
304 * @npages: number of pages in the @pages array.
305 *
John Hubbardf1f6a7d2020-01-30 22:13:35 -0800306 * For each page in the @pages array, release the page using unpin_user_page().
John Hubbardfc1d8e72019-05-13 17:19:08 -0700307 *
John Hubbardf1f6a7d2020-01-30 22:13:35 -0800308 * Please see the unpin_user_page() documentation for details.
John Hubbardfc1d8e72019-05-13 17:19:08 -0700309 */
John Hubbardf1f6a7d2020-01-30 22:13:35 -0800310void unpin_user_pages(struct page **pages, unsigned long npages)
John Hubbardfc1d8e72019-05-13 17:19:08 -0700311{
312 unsigned long index;
313
314 /*
315 * TODO: this can be optimized for huge pages: if a series of pages is
316 * physically contiguous and part of the same compound page, then a
317 * single operation to the head page should suffice.
318 */
319 for (index = 0; index < npages; index++)
John Hubbardf1f6a7d2020-01-30 22:13:35 -0800320 unpin_user_page(pages[index]);
John Hubbardfc1d8e72019-05-13 17:19:08 -0700321}
John Hubbardf1f6a7d2020-01-30 22:13:35 -0800322EXPORT_SYMBOL(unpin_user_pages);
John Hubbardfc1d8e72019-05-13 17:19:08 -0700323
Christoph Hellwig050a9ad2019-07-11 20:57:21 -0700324#ifdef CONFIG_MMU
Kirill A. Shutemov69e68b42014-06-04 16:08:11 -0700325static struct page *no_page_table(struct vm_area_struct *vma,
326 unsigned int flags)
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700327{
Kirill A. Shutemov69e68b42014-06-04 16:08:11 -0700328 /*
329 * When core dumping an enormous anonymous area that nobody
330 * has touched so far, we don't want to allocate unnecessary pages or
331 * page tables. Return error instead of NULL to skip handle_mm_fault,
332 * then get_dump_page() will return NULL to leave a hole in the dump.
333 * But we can only make this optimization where a hole would surely
334 * be zero-filled if handle_mm_fault() actually did handle it.
335 */
336 if ((flags & FOLL_DUMP) && (!vma->vm_ops || !vma->vm_ops->fault))
337 return ERR_PTR(-EFAULT);
338 return NULL;
339}
340
Kirill A. Shutemov1027e442015-09-04 15:47:55 -0700341static int follow_pfn_pte(struct vm_area_struct *vma, unsigned long address,
342 pte_t *pte, unsigned int flags)
343{
344 /* No page to get reference */
345 if (flags & FOLL_GET)
346 return -EFAULT;
347
348 if (flags & FOLL_TOUCH) {
349 pte_t entry = *pte;
350
351 if (flags & FOLL_WRITE)
352 entry = pte_mkdirty(entry);
353 entry = pte_mkyoung(entry);
354
355 if (!pte_same(*pte, entry)) {
356 set_pte_at(vma->vm_mm, address, pte, entry);
357 update_mmu_cache(vma, address, pte);
358 }
359 }
360
361 /* Proper page table entry exists, but no corresponding struct page */
362 return -EEXIST;
363}
364
Linus Torvalds19be0ea2016-10-13 13:07:36 -0700365/*
366 * FOLL_FORCE can write to even unwritable pte's, but only
367 * after we've gone through a COW cycle and they are dirty.
368 */
369static inline bool can_follow_write_pte(pte_t pte, unsigned int flags)
370{
Linus Torvaldsf6f37322017-12-15 18:53:22 -0800371 return pte_write(pte) ||
Linus Torvalds19be0ea2016-10-13 13:07:36 -0700372 ((flags & FOLL_FORCE) && (flags & FOLL_COW) && pte_dirty(pte));
373}
374
Kirill A. Shutemov69e68b42014-06-04 16:08:11 -0700375static struct page *follow_page_pte(struct vm_area_struct *vma,
Keith Buschdf06b372018-10-26 15:10:28 -0700376 unsigned long address, pmd_t *pmd, unsigned int flags,
377 struct dev_pagemap **pgmap)
Kirill A. Shutemov69e68b42014-06-04 16:08:11 -0700378{
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700379 struct mm_struct *mm = vma->vm_mm;
Kirill A. Shutemov69e68b42014-06-04 16:08:11 -0700380 struct page *page;
381 spinlock_t *ptl;
382 pte_t *ptep, pte;
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700383
John Hubbardeddb1c22020-01-30 22:12:54 -0800384 /* FOLL_GET and FOLL_PIN are mutually exclusive. */
385 if (WARN_ON_ONCE((flags & (FOLL_PIN | FOLL_GET)) ==
386 (FOLL_PIN | FOLL_GET)))
387 return ERR_PTR(-EINVAL);
Kirill A. Shutemov69e68b42014-06-04 16:08:11 -0700388retry:
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700389 if (unlikely(pmd_bad(*pmd)))
Kirill A. Shutemov69e68b42014-06-04 16:08:11 -0700390 return no_page_table(vma, flags);
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700391
392 ptep = pte_offset_map_lock(mm, pmd, address, &ptl);
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700393 pte = *ptep;
394 if (!pte_present(pte)) {
395 swp_entry_t entry;
396 /*
397 * KSM's break_ksm() relies upon recognizing a ksm page
398 * even while it is being migrated, so for that case we
399 * need migration_entry_wait().
400 */
401 if (likely(!(flags & FOLL_MIGRATION)))
402 goto no_page;
Kirill A. Shutemov0661a332015-02-10 14:10:04 -0800403 if (pte_none(pte))
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700404 goto no_page;
405 entry = pte_to_swp_entry(pte);
406 if (!is_migration_entry(entry))
407 goto no_page;
408 pte_unmap_unlock(ptep, ptl);
409 migration_entry_wait(mm, pmd, address);
Kirill A. Shutemov69e68b42014-06-04 16:08:11 -0700410 goto retry;
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700411 }
Mel Gorman8a0516e2015-02-12 14:58:22 -0800412 if ((flags & FOLL_NUMA) && pte_protnone(pte))
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700413 goto no_page;
Linus Torvalds19be0ea2016-10-13 13:07:36 -0700414 if ((flags & FOLL_WRITE) && !can_follow_write_pte(pte, flags)) {
Kirill A. Shutemov69e68b42014-06-04 16:08:11 -0700415 pte_unmap_unlock(ptep, ptl);
416 return NULL;
417 }
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700418
419 page = vm_normal_page(vma, address, pte);
John Hubbard3faa52c2020-04-01 21:05:29 -0700420 if (!page && pte_devmap(pte) && (flags & (FOLL_GET | FOLL_PIN))) {
Dan Williams3565fce2016-01-15 16:56:55 -0800421 /*
John Hubbard3faa52c2020-04-01 21:05:29 -0700422 * Only return device mapping pages in the FOLL_GET or FOLL_PIN
423 * case since they are only valid while holding the pgmap
424 * reference.
Dan Williams3565fce2016-01-15 16:56:55 -0800425 */
Keith Buschdf06b372018-10-26 15:10:28 -0700426 *pgmap = get_dev_pagemap(pte_pfn(pte), *pgmap);
427 if (*pgmap)
Dan Williams3565fce2016-01-15 16:56:55 -0800428 page = pte_page(pte);
429 else
430 goto no_page;
431 } else if (unlikely(!page)) {
Kirill A. Shutemov1027e442015-09-04 15:47:55 -0700432 if (flags & FOLL_DUMP) {
433 /* Avoid special (like zero) pages in core dumps */
434 page = ERR_PTR(-EFAULT);
435 goto out;
436 }
437
438 if (is_zero_pfn(pte_pfn(pte))) {
439 page = pte_page(pte);
440 } else {
441 int ret;
442
443 ret = follow_pfn_pte(vma, address, ptep, flags);
444 page = ERR_PTR(ret);
445 goto out;
446 }
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700447 }
448
Kirill A. Shutemov6742d292016-01-15 16:52:28 -0800449 if (flags & FOLL_SPLIT && PageTransCompound(page)) {
450 int ret;
451 get_page(page);
452 pte_unmap_unlock(ptep, ptl);
453 lock_page(page);
454 ret = split_huge_page(page);
455 unlock_page(page);
456 put_page(page);
457 if (ret)
458 return ERR_PTR(ret);
459 goto retry;
460 }
461
John Hubbard3faa52c2020-04-01 21:05:29 -0700462 /* try_grab_page() does nothing unless FOLL_GET or FOLL_PIN is set. */
463 if (unlikely(!try_grab_page(page, flags))) {
464 page = ERR_PTR(-ENOMEM);
465 goto out;
Linus Torvalds8fde12c2019-04-11 10:49:19 -0700466 }
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700467 if (flags & FOLL_TOUCH) {
468 if ((flags & FOLL_WRITE) &&
469 !pte_dirty(pte) && !PageDirty(page))
470 set_page_dirty(page);
471 /*
472 * pte_mkyoung() would be more correct here, but atomic care
473 * is needed to avoid losing the dirty bit: it is easier to use
474 * mark_page_accessed().
475 */
476 mark_page_accessed(page);
477 }
Eric B Munsonde60f5f2015-11-05 18:51:36 -0800478 if ((flags & FOLL_MLOCK) && (vma->vm_flags & VM_LOCKED)) {
Kirill A. Shutemove90309c2016-01-15 16:54:33 -0800479 /* Do not mlock pte-mapped THP */
480 if (PageTransCompound(page))
481 goto out;
482
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700483 /*
484 * The preliminary mapping check is mainly to avoid the
485 * pointless overhead of lock_page on the ZERO_PAGE
486 * which might bounce very badly if there is contention.
487 *
488 * If the page is already locked, we don't need to
489 * handle it now - vmscan will handle it later if and
490 * when it attempts to reclaim the page.
491 */
492 if (page->mapping && trylock_page(page)) {
493 lru_add_drain(); /* push cached pages to LRU */
494 /*
495 * Because we lock page here, and migration is
496 * blocked by the pte's page reference, and we
497 * know the page is still mapped, we don't even
498 * need to check for file-cache page truncation.
499 */
500 mlock_vma_page(page);
501 unlock_page(page);
502 }
503 }
Kirill A. Shutemov1027e442015-09-04 15:47:55 -0700504out:
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700505 pte_unmap_unlock(ptep, ptl);
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700506 return page;
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700507no_page:
508 pte_unmap_unlock(ptep, ptl);
509 if (!pte_none(pte))
Kirill A. Shutemov69e68b42014-06-04 16:08:11 -0700510 return NULL;
511 return no_page_table(vma, flags);
512}
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700513
Aneesh Kumar K.V080dbb62017-07-06 15:38:44 -0700514static struct page *follow_pmd_mask(struct vm_area_struct *vma,
515 unsigned long address, pud_t *pudp,
Keith Buschdf06b372018-10-26 15:10:28 -0700516 unsigned int flags,
517 struct follow_page_context *ctx)
Kirill A. Shutemov69e68b42014-06-04 16:08:11 -0700518{
Huang Ying68827282018-06-07 17:06:34 -0700519 pmd_t *pmd, pmdval;
Kirill A. Shutemov69e68b42014-06-04 16:08:11 -0700520 spinlock_t *ptl;
521 struct page *page;
522 struct mm_struct *mm = vma->vm_mm;
523
Aneesh Kumar K.V080dbb62017-07-06 15:38:44 -0700524 pmd = pmd_offset(pudp, address);
Huang Ying68827282018-06-07 17:06:34 -0700525 /*
526 * The READ_ONCE() will stabilize the pmdval in a register or
527 * on the stack so that it will stop changing under the code.
528 */
529 pmdval = READ_ONCE(*pmd);
530 if (pmd_none(pmdval))
Kirill A. Shutemov69e68b42014-06-04 16:08:11 -0700531 return no_page_table(vma, flags);
Wei Yangbe9d3042020-01-30 22:12:14 -0800532 if (pmd_huge(pmdval) && is_vm_hugetlb_page(vma)) {
Naoya Horiguchie66f17f2015-02-11 15:25:22 -0800533 page = follow_huge_pmd(mm, address, pmd, flags);
534 if (page)
535 return page;
536 return no_page_table(vma, flags);
Kirill A. Shutemov69e68b42014-06-04 16:08:11 -0700537 }
Huang Ying68827282018-06-07 17:06:34 -0700538 if (is_hugepd(__hugepd(pmd_val(pmdval)))) {
Aneesh Kumar K.V4dc71452017-07-06 15:38:56 -0700539 page = follow_huge_pd(vma, address,
Huang Ying68827282018-06-07 17:06:34 -0700540 __hugepd(pmd_val(pmdval)), flags,
Aneesh Kumar K.V4dc71452017-07-06 15:38:56 -0700541 PMD_SHIFT);
542 if (page)
543 return page;
544 return no_page_table(vma, flags);
545 }
Zi Yan84c3fc42017-09-08 16:11:01 -0700546retry:
Huang Ying68827282018-06-07 17:06:34 -0700547 if (!pmd_present(pmdval)) {
Zi Yan84c3fc42017-09-08 16:11:01 -0700548 if (likely(!(flags & FOLL_MIGRATION)))
549 return no_page_table(vma, flags);
550 VM_BUG_ON(thp_migration_supported() &&
Huang Ying68827282018-06-07 17:06:34 -0700551 !is_pmd_migration_entry(pmdval));
552 if (is_pmd_migration_entry(pmdval))
Zi Yan84c3fc42017-09-08 16:11:01 -0700553 pmd_migration_entry_wait(mm, pmd);
Huang Ying68827282018-06-07 17:06:34 -0700554 pmdval = READ_ONCE(*pmd);
555 /*
556 * MADV_DONTNEED may convert the pmd to null because
557 * mmap_sem is held in read mode
558 */
559 if (pmd_none(pmdval))
560 return no_page_table(vma, flags);
Zi Yan84c3fc42017-09-08 16:11:01 -0700561 goto retry;
562 }
Huang Ying68827282018-06-07 17:06:34 -0700563 if (pmd_devmap(pmdval)) {
Dan Williams3565fce2016-01-15 16:56:55 -0800564 ptl = pmd_lock(mm, pmd);
Keith Buschdf06b372018-10-26 15:10:28 -0700565 page = follow_devmap_pmd(vma, address, pmd, flags, &ctx->pgmap);
Dan Williams3565fce2016-01-15 16:56:55 -0800566 spin_unlock(ptl);
567 if (page)
568 return page;
569 }
Huang Ying68827282018-06-07 17:06:34 -0700570 if (likely(!pmd_trans_huge(pmdval)))
Keith Buschdf06b372018-10-26 15:10:28 -0700571 return follow_page_pte(vma, address, pmd, flags, &ctx->pgmap);
Kirill A. Shutemov6742d292016-01-15 16:52:28 -0800572
Huang Ying68827282018-06-07 17:06:34 -0700573 if ((flags & FOLL_NUMA) && pmd_protnone(pmdval))
Aneesh Kumar K.Vdb08f202017-02-24 14:59:53 -0800574 return no_page_table(vma, flags);
575
Zi Yan84c3fc42017-09-08 16:11:01 -0700576retry_locked:
Kirill A. Shutemov6742d292016-01-15 16:52:28 -0800577 ptl = pmd_lock(mm, pmd);
Huang Ying68827282018-06-07 17:06:34 -0700578 if (unlikely(pmd_none(*pmd))) {
579 spin_unlock(ptl);
580 return no_page_table(vma, flags);
581 }
Zi Yan84c3fc42017-09-08 16:11:01 -0700582 if (unlikely(!pmd_present(*pmd))) {
583 spin_unlock(ptl);
584 if (likely(!(flags & FOLL_MIGRATION)))
585 return no_page_table(vma, flags);
586 pmd_migration_entry_wait(mm, pmd);
587 goto retry_locked;
588 }
Kirill A. Shutemov6742d292016-01-15 16:52:28 -0800589 if (unlikely(!pmd_trans_huge(*pmd))) {
590 spin_unlock(ptl);
Keith Buschdf06b372018-10-26 15:10:28 -0700591 return follow_page_pte(vma, address, pmd, flags, &ctx->pgmap);
Kirill A. Shutemov69e68b42014-06-04 16:08:11 -0700592 }
Song Liubfe7b002019-09-23 15:38:25 -0700593 if (flags & (FOLL_SPLIT | FOLL_SPLIT_PMD)) {
Kirill A. Shutemov6742d292016-01-15 16:52:28 -0800594 int ret;
595 page = pmd_page(*pmd);
596 if (is_huge_zero_page(page)) {
597 spin_unlock(ptl);
598 ret = 0;
Kirill A. Shutemov78ddc532016-01-15 16:52:42 -0800599 split_huge_pmd(vma, pmd, address);
Naoya Horiguchi337d9ab2016-07-26 15:24:03 -0700600 if (pmd_trans_unstable(pmd))
601 ret = -EBUSY;
Song Liubfe7b002019-09-23 15:38:25 -0700602 } else if (flags & FOLL_SPLIT) {
Linus Torvalds8fde12c2019-04-11 10:49:19 -0700603 if (unlikely(!try_get_page(page))) {
604 spin_unlock(ptl);
605 return ERR_PTR(-ENOMEM);
606 }
Kirill A. Shutemov6742d292016-01-15 16:52:28 -0800607 spin_unlock(ptl);
608 lock_page(page);
609 ret = split_huge_page(page);
610 unlock_page(page);
611 put_page(page);
Kirill A. Shutemovbaa355f2016-07-26 15:25:51 -0700612 if (pmd_none(*pmd))
613 return no_page_table(vma, flags);
Song Liubfe7b002019-09-23 15:38:25 -0700614 } else { /* flags & FOLL_SPLIT_PMD */
615 spin_unlock(ptl);
616 split_huge_pmd(vma, pmd, address);
617 ret = pte_alloc(mm, pmd) ? -ENOMEM : 0;
Kirill A. Shutemov6742d292016-01-15 16:52:28 -0800618 }
619
620 return ret ? ERR_PTR(ret) :
Keith Buschdf06b372018-10-26 15:10:28 -0700621 follow_page_pte(vma, address, pmd, flags, &ctx->pgmap);
Kirill A. Shutemov6742d292016-01-15 16:52:28 -0800622 }
Kirill A. Shutemov6742d292016-01-15 16:52:28 -0800623 page = follow_trans_huge_pmd(vma, address, pmd, flags);
624 spin_unlock(ptl);
Keith Buschdf06b372018-10-26 15:10:28 -0700625 ctx->page_mask = HPAGE_PMD_NR - 1;
Kirill A. Shutemov6742d292016-01-15 16:52:28 -0800626 return page;
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700627}
628
Aneesh Kumar K.V080dbb62017-07-06 15:38:44 -0700629static struct page *follow_pud_mask(struct vm_area_struct *vma,
630 unsigned long address, p4d_t *p4dp,
Keith Buschdf06b372018-10-26 15:10:28 -0700631 unsigned int flags,
632 struct follow_page_context *ctx)
Aneesh Kumar K.V080dbb62017-07-06 15:38:44 -0700633{
634 pud_t *pud;
635 spinlock_t *ptl;
636 struct page *page;
637 struct mm_struct *mm = vma->vm_mm;
638
639 pud = pud_offset(p4dp, address);
640 if (pud_none(*pud))
641 return no_page_table(vma, flags);
Wei Yangbe9d3042020-01-30 22:12:14 -0800642 if (pud_huge(*pud) && is_vm_hugetlb_page(vma)) {
Aneesh Kumar K.V080dbb62017-07-06 15:38:44 -0700643 page = follow_huge_pud(mm, address, pud, flags);
644 if (page)
645 return page;
646 return no_page_table(vma, flags);
647 }
Aneesh Kumar K.V4dc71452017-07-06 15:38:56 -0700648 if (is_hugepd(__hugepd(pud_val(*pud)))) {
649 page = follow_huge_pd(vma, address,
650 __hugepd(pud_val(*pud)), flags,
651 PUD_SHIFT);
652 if (page)
653 return page;
654 return no_page_table(vma, flags);
655 }
Aneesh Kumar K.V080dbb62017-07-06 15:38:44 -0700656 if (pud_devmap(*pud)) {
657 ptl = pud_lock(mm, pud);
Keith Buschdf06b372018-10-26 15:10:28 -0700658 page = follow_devmap_pud(vma, address, pud, flags, &ctx->pgmap);
Aneesh Kumar K.V080dbb62017-07-06 15:38:44 -0700659 spin_unlock(ptl);
660 if (page)
661 return page;
662 }
663 if (unlikely(pud_bad(*pud)))
664 return no_page_table(vma, flags);
665
Keith Buschdf06b372018-10-26 15:10:28 -0700666 return follow_pmd_mask(vma, address, pud, flags, ctx);
Aneesh Kumar K.V080dbb62017-07-06 15:38:44 -0700667}
668
Aneesh Kumar K.V080dbb62017-07-06 15:38:44 -0700669static struct page *follow_p4d_mask(struct vm_area_struct *vma,
670 unsigned long address, pgd_t *pgdp,
Keith Buschdf06b372018-10-26 15:10:28 -0700671 unsigned int flags,
672 struct follow_page_context *ctx)
Aneesh Kumar K.V080dbb62017-07-06 15:38:44 -0700673{
674 p4d_t *p4d;
Aneesh Kumar K.V4dc71452017-07-06 15:38:56 -0700675 struct page *page;
Aneesh Kumar K.V080dbb62017-07-06 15:38:44 -0700676
677 p4d = p4d_offset(pgdp, address);
678 if (p4d_none(*p4d))
679 return no_page_table(vma, flags);
680 BUILD_BUG_ON(p4d_huge(*p4d));
681 if (unlikely(p4d_bad(*p4d)))
682 return no_page_table(vma, flags);
683
Aneesh Kumar K.V4dc71452017-07-06 15:38:56 -0700684 if (is_hugepd(__hugepd(p4d_val(*p4d)))) {
685 page = follow_huge_pd(vma, address,
686 __hugepd(p4d_val(*p4d)), flags,
687 P4D_SHIFT);
688 if (page)
689 return page;
690 return no_page_table(vma, flags);
691 }
Keith Buschdf06b372018-10-26 15:10:28 -0700692 return follow_pud_mask(vma, address, p4d, flags, ctx);
Aneesh Kumar K.V080dbb62017-07-06 15:38:44 -0700693}
694
695/**
696 * follow_page_mask - look up a page descriptor from a user-virtual address
697 * @vma: vm_area_struct mapping @address
698 * @address: virtual address to look up
699 * @flags: flags modifying lookup behaviour
Mike Rapoport78179552018-11-16 15:08:29 -0800700 * @ctx: contains dev_pagemap for %ZONE_DEVICE memory pinning and a
701 * pointer to output page_mask
Aneesh Kumar K.V080dbb62017-07-06 15:38:44 -0700702 *
703 * @flags can have FOLL_ flags set, defined in <linux/mm.h>
704 *
Mike Rapoport78179552018-11-16 15:08:29 -0800705 * When getting pages from ZONE_DEVICE memory, the @ctx->pgmap caches
706 * the device's dev_pagemap metadata to avoid repeating expensive lookups.
707 *
708 * On output, the @ctx->page_mask is set according to the size of the page.
709 *
710 * Return: the mapped (struct page *), %NULL if no mapping exists, or
Aneesh Kumar K.V080dbb62017-07-06 15:38:44 -0700711 * an error pointer if there is a mapping to something not represented
712 * by a page descriptor (see also vm_normal_page()).
713 */
Bharath Vedarthama7030ae2019-07-11 20:54:34 -0700714static struct page *follow_page_mask(struct vm_area_struct *vma,
Aneesh Kumar K.V080dbb62017-07-06 15:38:44 -0700715 unsigned long address, unsigned int flags,
Keith Buschdf06b372018-10-26 15:10:28 -0700716 struct follow_page_context *ctx)
Aneesh Kumar K.V080dbb62017-07-06 15:38:44 -0700717{
718 pgd_t *pgd;
719 struct page *page;
720 struct mm_struct *mm = vma->vm_mm;
721
Keith Buschdf06b372018-10-26 15:10:28 -0700722 ctx->page_mask = 0;
Aneesh Kumar K.V080dbb62017-07-06 15:38:44 -0700723
724 /* make this handle hugepd */
725 page = follow_huge_addr(mm, address, flags & FOLL_WRITE);
726 if (!IS_ERR(page)) {
John Hubbard3faa52c2020-04-01 21:05:29 -0700727 WARN_ON_ONCE(flags & (FOLL_GET | FOLL_PIN));
Aneesh Kumar K.V080dbb62017-07-06 15:38:44 -0700728 return page;
729 }
730
731 pgd = pgd_offset(mm, address);
732
733 if (pgd_none(*pgd) || unlikely(pgd_bad(*pgd)))
734 return no_page_table(vma, flags);
735
Anshuman Khandualfaaa5b62017-07-06 15:38:50 -0700736 if (pgd_huge(*pgd)) {
737 page = follow_huge_pgd(mm, address, pgd, flags);
738 if (page)
739 return page;
740 return no_page_table(vma, flags);
741 }
Aneesh Kumar K.V4dc71452017-07-06 15:38:56 -0700742 if (is_hugepd(__hugepd(pgd_val(*pgd)))) {
743 page = follow_huge_pd(vma, address,
744 __hugepd(pgd_val(*pgd)), flags,
745 PGDIR_SHIFT);
746 if (page)
747 return page;
748 return no_page_table(vma, flags);
749 }
Anshuman Khandualfaaa5b62017-07-06 15:38:50 -0700750
Keith Buschdf06b372018-10-26 15:10:28 -0700751 return follow_p4d_mask(vma, address, pgd, flags, ctx);
752}
753
754struct page *follow_page(struct vm_area_struct *vma, unsigned long address,
755 unsigned int foll_flags)
756{
757 struct follow_page_context ctx = { NULL };
758 struct page *page;
759
760 page = follow_page_mask(vma, address, foll_flags, &ctx);
761 if (ctx.pgmap)
762 put_dev_pagemap(ctx.pgmap);
763 return page;
Aneesh Kumar K.V080dbb62017-07-06 15:38:44 -0700764}
765
Kirill A. Shutemovf2b495c2014-06-04 16:08:11 -0700766static int get_gate_page(struct mm_struct *mm, unsigned long address,
767 unsigned int gup_flags, struct vm_area_struct **vma,
768 struct page **page)
769{
770 pgd_t *pgd;
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +0300771 p4d_t *p4d;
Kirill A. Shutemovf2b495c2014-06-04 16:08:11 -0700772 pud_t *pud;
773 pmd_t *pmd;
774 pte_t *pte;
775 int ret = -EFAULT;
776
777 /* user gate pages are read-only */
778 if (gup_flags & FOLL_WRITE)
779 return -EFAULT;
780 if (address > TASK_SIZE)
781 pgd = pgd_offset_k(address);
782 else
783 pgd = pgd_offset_gate(mm, address);
Andy Lutomirskib5d1c392019-07-11 20:57:43 -0700784 if (pgd_none(*pgd))
785 return -EFAULT;
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +0300786 p4d = p4d_offset(pgd, address);
Andy Lutomirskib5d1c392019-07-11 20:57:43 -0700787 if (p4d_none(*p4d))
788 return -EFAULT;
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +0300789 pud = pud_offset(p4d, address);
Andy Lutomirskib5d1c392019-07-11 20:57:43 -0700790 if (pud_none(*pud))
791 return -EFAULT;
Kirill A. Shutemovf2b495c2014-06-04 16:08:11 -0700792 pmd = pmd_offset(pud, address);
Zi Yan84c3fc42017-09-08 16:11:01 -0700793 if (!pmd_present(*pmd))
Kirill A. Shutemovf2b495c2014-06-04 16:08:11 -0700794 return -EFAULT;
795 VM_BUG_ON(pmd_trans_huge(*pmd));
796 pte = pte_offset_map(pmd, address);
797 if (pte_none(*pte))
798 goto unmap;
799 *vma = get_gate_vma(mm);
800 if (!page)
801 goto out;
802 *page = vm_normal_page(*vma, address, *pte);
803 if (!*page) {
804 if ((gup_flags & FOLL_DUMP) || !is_zero_pfn(pte_pfn(*pte)))
805 goto unmap;
806 *page = pte_page(*pte);
807 }
Linus Torvalds8fde12c2019-04-11 10:49:19 -0700808 if (unlikely(!try_get_page(*page))) {
809 ret = -ENOMEM;
810 goto unmap;
811 }
Kirill A. Shutemovf2b495c2014-06-04 16:08:11 -0700812out:
813 ret = 0;
814unmap:
815 pte_unmap(pte);
816 return ret;
817}
818
Paul Cassella9a95f3c2014-08-06 16:07:24 -0700819/*
820 * mmap_sem must be held on entry. If @nonblocking != NULL and
821 * *@flags does not include FOLL_NOWAIT, the mmap_sem may be released.
822 * If it is, *@nonblocking will be set to 0 and -EBUSY returned.
823 */
Kirill A. Shutemov16744482014-06-04 16:08:12 -0700824static int faultin_page(struct task_struct *tsk, struct vm_area_struct *vma,
825 unsigned long address, unsigned int *flags, int *nonblocking)
826{
Kirill A. Shutemov16744482014-06-04 16:08:12 -0700827 unsigned int fault_flags = 0;
Souptick Joarder2b740302018-08-23 17:01:36 -0700828 vm_fault_t ret;
Kirill A. Shutemov16744482014-06-04 16:08:12 -0700829
Eric B Munsonde60f5f2015-11-05 18:51:36 -0800830 /* mlock all present pages, but do not fault in new pages */
831 if ((*flags & (FOLL_POPULATE | FOLL_MLOCK)) == FOLL_MLOCK)
832 return -ENOENT;
Kirill A. Shutemov16744482014-06-04 16:08:12 -0700833 if (*flags & FOLL_WRITE)
834 fault_flags |= FAULT_FLAG_WRITE;
Dave Hansen1b2ee122016-02-12 13:02:21 -0800835 if (*flags & FOLL_REMOTE)
836 fault_flags |= FAULT_FLAG_REMOTE;
Kirill A. Shutemov16744482014-06-04 16:08:12 -0700837 if (nonblocking)
838 fault_flags |= FAULT_FLAG_ALLOW_RETRY;
839 if (*flags & FOLL_NOWAIT)
840 fault_flags |= FAULT_FLAG_ALLOW_RETRY | FAULT_FLAG_RETRY_NOWAIT;
Andres Lagar-Cavilla234b2392014-09-17 10:51:48 -0700841 if (*flags & FOLL_TRIED) {
842 VM_WARN_ON_ONCE(fault_flags & FAULT_FLAG_ALLOW_RETRY);
843 fault_flags |= FAULT_FLAG_TRIED;
844 }
Kirill A. Shutemov16744482014-06-04 16:08:12 -0700845
Kirill A. Shutemovdcddffd2016-07-26 15:25:18 -0700846 ret = handle_mm_fault(vma, address, fault_flags);
Kirill A. Shutemov16744482014-06-04 16:08:12 -0700847 if (ret & VM_FAULT_ERROR) {
James Morse9a291a72017-06-02 14:46:46 -0700848 int err = vm_fault_to_errno(ret, *flags);
849
850 if (err)
851 return err;
Kirill A. Shutemov16744482014-06-04 16:08:12 -0700852 BUG();
853 }
854
855 if (tsk) {
856 if (ret & VM_FAULT_MAJOR)
857 tsk->maj_flt++;
858 else
859 tsk->min_flt++;
860 }
861
862 if (ret & VM_FAULT_RETRY) {
Andrea Arcangeli96312e62018-03-09 15:51:06 -0800863 if (nonblocking && !(fault_flags & FAULT_FLAG_RETRY_NOWAIT))
Kirill A. Shutemov16744482014-06-04 16:08:12 -0700864 *nonblocking = 0;
865 return -EBUSY;
866 }
867
868 /*
869 * The VM_FAULT_WRITE bit tells us that do_wp_page has broken COW when
870 * necessary, even if maybe_mkwrite decided not to set pte_write. We
871 * can thus safely do subsequent page lookups as if they were reads.
872 * But only do so when looping for pte_write is futile: in some cases
873 * userspace may also be wanting to write to the gotten user page,
874 * which a read fault here might prevent (a readonly page might get
875 * reCOWed by userspace write).
876 */
877 if ((ret & VM_FAULT_WRITE) && !(vma->vm_flags & VM_WRITE))
Mario Leinweber29231172018-04-05 16:24:18 -0700878 *flags |= FOLL_COW;
Kirill A. Shutemov16744482014-06-04 16:08:12 -0700879 return 0;
880}
881
Kirill A. Shutemovfa5bb202014-06-04 16:08:13 -0700882static int check_vma_flags(struct vm_area_struct *vma, unsigned long gup_flags)
883{
884 vm_flags_t vm_flags = vma->vm_flags;
Dave Hansen1b2ee122016-02-12 13:02:21 -0800885 int write = (gup_flags & FOLL_WRITE);
886 int foreign = (gup_flags & FOLL_REMOTE);
Kirill A. Shutemovfa5bb202014-06-04 16:08:13 -0700887
888 if (vm_flags & (VM_IO | VM_PFNMAP))
889 return -EFAULT;
890
Willy Tarreau7f7ccc22018-05-11 08:11:44 +0200891 if (gup_flags & FOLL_ANON && !vma_is_anonymous(vma))
892 return -EFAULT;
893
Dave Hansen1b2ee122016-02-12 13:02:21 -0800894 if (write) {
Kirill A. Shutemovfa5bb202014-06-04 16:08:13 -0700895 if (!(vm_flags & VM_WRITE)) {
896 if (!(gup_flags & FOLL_FORCE))
897 return -EFAULT;
898 /*
899 * We used to let the write,force case do COW in a
900 * VM_MAYWRITE VM_SHARED !VM_WRITE vma, so ptrace could
901 * set a breakpoint in a read-only mapping of an
902 * executable, without corrupting the file (yet only
903 * when that file had been opened for writing!).
904 * Anon pages in shared mappings are surprising: now
905 * just reject it.
906 */
Hugh Dickins46435362016-01-30 18:03:16 -0800907 if (!is_cow_mapping(vm_flags))
Kirill A. Shutemovfa5bb202014-06-04 16:08:13 -0700908 return -EFAULT;
Kirill A. Shutemovfa5bb202014-06-04 16:08:13 -0700909 }
910 } else if (!(vm_flags & VM_READ)) {
911 if (!(gup_flags & FOLL_FORCE))
912 return -EFAULT;
913 /*
914 * Is there actually any vma we can reach here which does not
915 * have VM_MAYREAD set?
916 */
917 if (!(vm_flags & VM_MAYREAD))
918 return -EFAULT;
919 }
Dave Hansend61172b2016-02-12 13:02:24 -0800920 /*
921 * gups are always data accesses, not instruction
922 * fetches, so execute=false here
923 */
924 if (!arch_vma_access_permitted(vma, write, false, foreign))
Dave Hansen33a709b2016-02-12 13:02:19 -0800925 return -EFAULT;
Kirill A. Shutemovfa5bb202014-06-04 16:08:13 -0700926 return 0;
927}
928
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700929/**
930 * __get_user_pages() - pin user pages in memory
931 * @tsk: task_struct of target task
932 * @mm: mm_struct of target mm
933 * @start: starting user address
934 * @nr_pages: number of pages from start to pin
935 * @gup_flags: flags modifying pin behaviour
936 * @pages: array that receives pointers to the pages pinned.
937 * Should be at least nr_pages long. Or NULL, if caller
938 * only intends to ensure the pages are faulted in.
939 * @vmas: array of pointers to vmas corresponding to each page.
940 * Or NULL if the caller does not require them.
941 * @nonblocking: whether waiting for disk IO or mmap_sem contention
942 *
Liu Xiangd2dfbe42019-11-30 17:49:53 -0800943 * Returns either number of pages pinned (which may be less than the
944 * number requested), or an error. Details about the return value:
945 *
946 * -- If nr_pages is 0, returns 0.
947 * -- If nr_pages is >0, but no pages were pinned, returns -errno.
948 * -- If nr_pages is >0, and some pages were pinned, returns the number of
949 * pages pinned. Again, this may be less than nr_pages.
950 *
951 * The caller is responsible for releasing returned @pages, via put_page().
952 *
953 * @vmas are valid only as long as mmap_sem is held.
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700954 *
Paul Cassella9a95f3c2014-08-06 16:07:24 -0700955 * Must be called with mmap_sem held. It may be released. See below.
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700956 *
957 * __get_user_pages walks a process's page tables and takes a reference to
958 * each struct page that each user address corresponds to at a given
959 * instant. That is, it takes the page that would be accessed if a user
960 * thread accesses the given user virtual address at that instant.
961 *
962 * This does not guarantee that the page exists in the user mappings when
963 * __get_user_pages returns, and there may even be a completely different
964 * page there in some cases (eg. if mmapped pagecache has been invalidated
965 * and subsequently re faulted). However it does guarantee that the page
966 * won't be freed completely. And mostly callers simply care that the page
967 * contains data that was valid *at some point in time*. Typically, an IO
968 * or similar operation cannot guarantee anything stronger anyway because
969 * locks can't be held over the syscall boundary.
970 *
971 * If @gup_flags & FOLL_WRITE == 0, the page must not be written to. If
972 * the page is written to, set_page_dirty (or set_page_dirty_lock, as
973 * appropriate) must be called after the page is finished with, and
974 * before put_page is called.
975 *
976 * If @nonblocking != NULL, __get_user_pages will not wait for disk IO
977 * or mmap_sem contention, and if waiting is needed to pin all pages,
Paul Cassella9a95f3c2014-08-06 16:07:24 -0700978 * *@nonblocking will be set to 0. Further, if @gup_flags does not
979 * include FOLL_NOWAIT, the mmap_sem will be released via up_read() in
980 * this case.
981 *
982 * A caller using such a combination of @nonblocking and @gup_flags
983 * must therefore hold the mmap_sem for reading only, and recognize
984 * when it's been released. Otherwise, it must be held for either
985 * reading or writing and will not be released.
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700986 *
987 * In most cases, get_user_pages or get_user_pages_fast should be used
988 * instead of __get_user_pages. __get_user_pages should be used only if
989 * you need some special @gup_flags.
990 */
Lorenzo Stoakes0d731752016-10-24 10:57:25 +0100991static long __get_user_pages(struct task_struct *tsk, struct mm_struct *mm,
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700992 unsigned long start, unsigned long nr_pages,
993 unsigned int gup_flags, struct page **pages,
994 struct vm_area_struct **vmas, int *nonblocking)
995{
Keith Buschdf06b372018-10-26 15:10:28 -0700996 long ret = 0, i = 0;
Kirill A. Shutemovfa5bb202014-06-04 16:08:13 -0700997 struct vm_area_struct *vma = NULL;
Keith Buschdf06b372018-10-26 15:10:28 -0700998 struct follow_page_context ctx = { NULL };
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700999
1000 if (!nr_pages)
1001 return 0;
1002
Andrey Konovalovf9652592019-09-25 16:48:34 -07001003 start = untagged_addr(start);
1004
John Hubbardeddb1c22020-01-30 22:12:54 -08001005 VM_BUG_ON(!!pages != !!(gup_flags & (FOLL_GET | FOLL_PIN)));
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -07001006
1007 /*
1008 * If FOLL_FORCE is set then do not force a full fault as the hinting
1009 * fault information is unrelated to the reference behaviour of a task
1010 * using the address space
1011 */
1012 if (!(gup_flags & FOLL_FORCE))
1013 gup_flags |= FOLL_NUMA;
1014
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -07001015 do {
Kirill A. Shutemovfa5bb202014-06-04 16:08:13 -07001016 struct page *page;
1017 unsigned int foll_flags = gup_flags;
1018 unsigned int page_increm;
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -07001019
Kirill A. Shutemovfa5bb202014-06-04 16:08:13 -07001020 /* first iteration or cross vma bound */
1021 if (!vma || start >= vma->vm_end) {
1022 vma = find_extend_vma(mm, start);
1023 if (!vma && in_gate_area(mm, start)) {
Kirill A. Shutemovfa5bb202014-06-04 16:08:13 -07001024 ret = get_gate_page(mm, start & PAGE_MASK,
1025 gup_flags, &vma,
1026 pages ? &pages[i] : NULL);
1027 if (ret)
John Hubbard08be37b2018-11-30 14:08:53 -08001028 goto out;
Keith Buschdf06b372018-10-26 15:10:28 -07001029 ctx.page_mask = 0;
Kirill A. Shutemovfa5bb202014-06-04 16:08:13 -07001030 goto next_page;
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -07001031 }
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -07001032
Keith Buschdf06b372018-10-26 15:10:28 -07001033 if (!vma || check_vma_flags(vma, gup_flags)) {
1034 ret = -EFAULT;
1035 goto out;
1036 }
Kirill A. Shutemovfa5bb202014-06-04 16:08:13 -07001037 if (is_vm_hugetlb_page(vma)) {
1038 i = follow_hugetlb_page(mm, vma, pages, vmas,
1039 &start, &nr_pages, i,
Andrea Arcangeli87ffc112017-02-22 15:43:13 -08001040 gup_flags, nonblocking);
Kirill A. Shutemovfa5bb202014-06-04 16:08:13 -07001041 continue;
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -07001042 }
Kirill A. Shutemovfa5bb202014-06-04 16:08:13 -07001043 }
1044retry:
1045 /*
1046 * If we have a pending SIGKILL, don't keep faulting pages and
1047 * potentially allocating memory.
1048 */
Davidlohr Buesofa45f112019-01-03 15:28:55 -08001049 if (fatal_signal_pending(current)) {
Keith Buschdf06b372018-10-26 15:10:28 -07001050 ret = -ERESTARTSYS;
1051 goto out;
1052 }
Kirill A. Shutemovfa5bb202014-06-04 16:08:13 -07001053 cond_resched();
Keith Buschdf06b372018-10-26 15:10:28 -07001054
1055 page = follow_page_mask(vma, start, foll_flags, &ctx);
Kirill A. Shutemovfa5bb202014-06-04 16:08:13 -07001056 if (!page) {
Kirill A. Shutemovfa5bb202014-06-04 16:08:13 -07001057 ret = faultin_page(tsk, vma, start, &foll_flags,
1058 nonblocking);
1059 switch (ret) {
1060 case 0:
1061 goto retry;
Keith Buschdf06b372018-10-26 15:10:28 -07001062 case -EBUSY:
1063 ret = 0;
1064 /* FALLTHRU */
Kirill A. Shutemovfa5bb202014-06-04 16:08:13 -07001065 case -EFAULT:
1066 case -ENOMEM:
1067 case -EHWPOISON:
Keith Buschdf06b372018-10-26 15:10:28 -07001068 goto out;
Kirill A. Shutemovfa5bb202014-06-04 16:08:13 -07001069 case -ENOENT:
1070 goto next_page;
1071 }
1072 BUG();
Kirill A. Shutemov1027e442015-09-04 15:47:55 -07001073 } else if (PTR_ERR(page) == -EEXIST) {
1074 /*
1075 * Proper page table entry exists, but no corresponding
1076 * struct page.
1077 */
1078 goto next_page;
1079 } else if (IS_ERR(page)) {
Keith Buschdf06b372018-10-26 15:10:28 -07001080 ret = PTR_ERR(page);
1081 goto out;
Kirill A. Shutemov1027e442015-09-04 15:47:55 -07001082 }
Kirill A. Shutemovfa5bb202014-06-04 16:08:13 -07001083 if (pages) {
1084 pages[i] = page;
1085 flush_anon_page(vma, page, start);
1086 flush_dcache_page(page);
Keith Buschdf06b372018-10-26 15:10:28 -07001087 ctx.page_mask = 0;
Kirill A. Shutemovfa5bb202014-06-04 16:08:13 -07001088 }
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -07001089next_page:
Kirill A. Shutemovfa5bb202014-06-04 16:08:13 -07001090 if (vmas) {
1091 vmas[i] = vma;
Keith Buschdf06b372018-10-26 15:10:28 -07001092 ctx.page_mask = 0;
Kirill A. Shutemovfa5bb202014-06-04 16:08:13 -07001093 }
Keith Buschdf06b372018-10-26 15:10:28 -07001094 page_increm = 1 + (~(start >> PAGE_SHIFT) & ctx.page_mask);
Kirill A. Shutemovfa5bb202014-06-04 16:08:13 -07001095 if (page_increm > nr_pages)
1096 page_increm = nr_pages;
1097 i += page_increm;
1098 start += page_increm * PAGE_SIZE;
1099 nr_pages -= page_increm;
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -07001100 } while (nr_pages);
Keith Buschdf06b372018-10-26 15:10:28 -07001101out:
1102 if (ctx.pgmap)
1103 put_dev_pagemap(ctx.pgmap);
1104 return i ? i : ret;
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -07001105}
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -07001106
Tobias Klauser771ab432016-12-12 16:41:53 -08001107static bool vma_permits_fault(struct vm_area_struct *vma,
1108 unsigned int fault_flags)
Dave Hansend4925e02016-02-12 13:02:16 -08001109{
Dave Hansen1b2ee122016-02-12 13:02:21 -08001110 bool write = !!(fault_flags & FAULT_FLAG_WRITE);
1111 bool foreign = !!(fault_flags & FAULT_FLAG_REMOTE);
Dave Hansen33a709b2016-02-12 13:02:19 -08001112 vm_flags_t vm_flags = write ? VM_WRITE : VM_READ;
Dave Hansend4925e02016-02-12 13:02:16 -08001113
1114 if (!(vm_flags & vma->vm_flags))
1115 return false;
1116
Dave Hansen33a709b2016-02-12 13:02:19 -08001117 /*
1118 * The architecture might have a hardware protection
Dave Hansen1b2ee122016-02-12 13:02:21 -08001119 * mechanism other than read/write that can deny access.
Dave Hansend61172b2016-02-12 13:02:24 -08001120 *
1121 * gup always represents data access, not instruction
1122 * fetches, so execute=false here:
Dave Hansen33a709b2016-02-12 13:02:19 -08001123 */
Dave Hansend61172b2016-02-12 13:02:24 -08001124 if (!arch_vma_access_permitted(vma, write, false, foreign))
Dave Hansen33a709b2016-02-12 13:02:19 -08001125 return false;
1126
Dave Hansend4925e02016-02-12 13:02:16 -08001127 return true;
1128}
1129
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -07001130/*
1131 * fixup_user_fault() - manually resolve a user page fault
1132 * @tsk: the task_struct to use for page fault accounting, or
1133 * NULL if faults are not to be recorded.
1134 * @mm: mm_struct of target mm
1135 * @address: user address
1136 * @fault_flags:flags to pass down to handle_mm_fault()
Dominik Dingel4a9e1cd2016-01-15 16:57:04 -08001137 * @unlocked: did we unlock the mmap_sem while retrying, maybe NULL if caller
1138 * does not allow retry
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -07001139 *
1140 * This is meant to be called in the specific scenario where for locking reasons
1141 * we try to access user memory in atomic context (within a pagefault_disable()
1142 * section), this returns -EFAULT, and we want to resolve the user fault before
1143 * trying again.
1144 *
1145 * Typically this is meant to be used by the futex code.
1146 *
1147 * The main difference with get_user_pages() is that this function will
1148 * unconditionally call handle_mm_fault() which will in turn perform all the
1149 * necessary SW fixup of the dirty and young bits in the PTE, while
Dominik Dingel4a9e1cd2016-01-15 16:57:04 -08001150 * get_user_pages() only guarantees to update these in the struct page.
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -07001151 *
1152 * This is important for some architectures where those bits also gate the
1153 * access permission to the page because they are maintained in software. On
1154 * such architectures, gup() will not be enough to make a subsequent access
1155 * succeed.
1156 *
Dominik Dingel4a9e1cd2016-01-15 16:57:04 -08001157 * This function will not return with an unlocked mmap_sem. So it has not the
1158 * same semantics wrt the @mm->mmap_sem as does filemap_fault().
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -07001159 */
1160int fixup_user_fault(struct task_struct *tsk, struct mm_struct *mm,
Dominik Dingel4a9e1cd2016-01-15 16:57:04 -08001161 unsigned long address, unsigned int fault_flags,
1162 bool *unlocked)
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -07001163{
1164 struct vm_area_struct *vma;
Souptick Joarder2b740302018-08-23 17:01:36 -07001165 vm_fault_t ret, major = 0;
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -07001166
Andrey Konovalovf9652592019-09-25 16:48:34 -07001167 address = untagged_addr(address);
1168
Dominik Dingel4a9e1cd2016-01-15 16:57:04 -08001169 if (unlocked)
1170 fault_flags |= FAULT_FLAG_ALLOW_RETRY;
1171
1172retry:
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -07001173 vma = find_extend_vma(mm, address);
1174 if (!vma || address < vma->vm_start)
1175 return -EFAULT;
1176
Dave Hansend4925e02016-02-12 13:02:16 -08001177 if (!vma_permits_fault(vma, fault_flags))
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -07001178 return -EFAULT;
1179
Kirill A. Shutemovdcddffd2016-07-26 15:25:18 -07001180 ret = handle_mm_fault(vma, address, fault_flags);
Dominik Dingel4a9e1cd2016-01-15 16:57:04 -08001181 major |= ret & VM_FAULT_MAJOR;
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -07001182 if (ret & VM_FAULT_ERROR) {
James Morse9a291a72017-06-02 14:46:46 -07001183 int err = vm_fault_to_errno(ret, 0);
1184
1185 if (err)
1186 return err;
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -07001187 BUG();
1188 }
Dominik Dingel4a9e1cd2016-01-15 16:57:04 -08001189
1190 if (ret & VM_FAULT_RETRY) {
1191 down_read(&mm->mmap_sem);
1192 if (!(fault_flags & FAULT_FLAG_TRIED)) {
1193 *unlocked = true;
1194 fault_flags &= ~FAULT_FLAG_ALLOW_RETRY;
1195 fault_flags |= FAULT_FLAG_TRIED;
1196 goto retry;
1197 }
1198 }
1199
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -07001200 if (tsk) {
Dominik Dingel4a9e1cd2016-01-15 16:57:04 -08001201 if (major)
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -07001202 tsk->maj_flt++;
1203 else
1204 tsk->min_flt++;
1205 }
1206 return 0;
1207}
Paolo Bonziniadd6a0c2016-06-07 17:51:18 +02001208EXPORT_SYMBOL_GPL(fixup_user_fault);
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -07001209
Andrea Arcangelif0818f42015-02-11 15:27:17 -08001210static __always_inline long __get_user_pages_locked(struct task_struct *tsk,
1211 struct mm_struct *mm,
1212 unsigned long start,
1213 unsigned long nr_pages,
Andrea Arcangelif0818f42015-02-11 15:27:17 -08001214 struct page **pages,
1215 struct vm_area_struct **vmas,
Al Viroe7167122017-11-19 11:32:05 -05001216 int *locked,
Andrea Arcangeli0fd71a52015-02-11 15:27:20 -08001217 unsigned int flags)
Andrea Arcangelif0818f42015-02-11 15:27:17 -08001218{
Andrea Arcangelif0818f42015-02-11 15:27:17 -08001219 long ret, pages_done;
1220 bool lock_dropped;
1221
1222 if (locked) {
1223 /* if VM_FAULT_RETRY can be returned, vmas become invalid */
1224 BUG_ON(vmas);
1225 /* check caller initialized locked */
1226 BUG_ON(*locked != 1);
1227 }
1228
John Hubbardeddb1c22020-01-30 22:12:54 -08001229 /*
1230 * FOLL_PIN and FOLL_GET are mutually exclusive. Traditional behavior
1231 * is to set FOLL_GET if the caller wants pages[] filled in (but has
1232 * carelessly failed to specify FOLL_GET), so keep doing that, but only
1233 * for FOLL_GET, not for the newer FOLL_PIN.
1234 *
1235 * FOLL_PIN always expects pages to be non-null, but no need to assert
1236 * that here, as any failures will be obvious enough.
1237 */
1238 if (pages && !(flags & FOLL_PIN))
Andrea Arcangelif0818f42015-02-11 15:27:17 -08001239 flags |= FOLL_GET;
Andrea Arcangelif0818f42015-02-11 15:27:17 -08001240
1241 pages_done = 0;
1242 lock_dropped = false;
1243 for (;;) {
1244 ret = __get_user_pages(tsk, mm, start, nr_pages, flags, pages,
1245 vmas, locked);
1246 if (!locked)
1247 /* VM_FAULT_RETRY couldn't trigger, bypass */
1248 return ret;
1249
1250 /* VM_FAULT_RETRY cannot return errors */
1251 if (!*locked) {
1252 BUG_ON(ret < 0);
1253 BUG_ON(ret >= nr_pages);
1254 }
1255
Andrea Arcangelif0818f42015-02-11 15:27:17 -08001256 if (ret > 0) {
1257 nr_pages -= ret;
1258 pages_done += ret;
1259 if (!nr_pages)
1260 break;
1261 }
1262 if (*locked) {
Andrea Arcangeli96312e62018-03-09 15:51:06 -08001263 /*
1264 * VM_FAULT_RETRY didn't trigger or it was a
1265 * FOLL_NOWAIT.
1266 */
Andrea Arcangelif0818f42015-02-11 15:27:17 -08001267 if (!pages_done)
1268 pages_done = ret;
1269 break;
1270 }
Mike Rapoportdf172772019-05-31 22:30:33 -07001271 /*
1272 * VM_FAULT_RETRY triggered, so seek to the faulting offset.
1273 * For the prefault case (!pages) we only update counts.
1274 */
1275 if (likely(pages))
1276 pages += ret;
Andrea Arcangelif0818f42015-02-11 15:27:17 -08001277 start += ret << PAGE_SHIFT;
1278
1279 /*
1280 * Repeat on the address that fired VM_FAULT_RETRY
1281 * without FAULT_FLAG_ALLOW_RETRY but with
1282 * FAULT_FLAG_TRIED.
1283 */
1284 *locked = 1;
1285 lock_dropped = true;
1286 down_read(&mm->mmap_sem);
1287 ret = __get_user_pages(tsk, mm, start, 1, flags | FOLL_TRIED,
1288 pages, NULL, NULL);
1289 if (ret != 1) {
1290 BUG_ON(ret > 1);
1291 if (!pages_done)
1292 pages_done = ret;
1293 break;
1294 }
1295 nr_pages--;
1296 pages_done++;
1297 if (!nr_pages)
1298 break;
Mike Rapoportdf172772019-05-31 22:30:33 -07001299 if (likely(pages))
1300 pages++;
Andrea Arcangelif0818f42015-02-11 15:27:17 -08001301 start += PAGE_SIZE;
1302 }
Al Viroe7167122017-11-19 11:32:05 -05001303 if (lock_dropped && *locked) {
Andrea Arcangelif0818f42015-02-11 15:27:17 -08001304 /*
1305 * We must let the caller know we temporarily dropped the lock
1306 * and so the critical section protected by it was lost.
1307 */
1308 up_read(&mm->mmap_sem);
1309 *locked = 0;
1310 }
1311 return pages_done;
1312}
1313
Christoph Hellwigd3649f62019-07-11 20:57:18 -07001314/**
1315 * populate_vma_page_range() - populate a range of pages in the vma.
1316 * @vma: target vma
1317 * @start: start address
1318 * @end: end address
1319 * @nonblocking:
1320 *
1321 * This takes care of mlocking the pages too if VM_LOCKED is set.
1322 *
1323 * return 0 on success, negative error code on error.
1324 *
1325 * vma->vm_mm->mmap_sem must be held.
1326 *
1327 * If @nonblocking is NULL, it may be held for read or write and will
1328 * be unperturbed.
1329 *
1330 * If @nonblocking is non-NULL, it must held for read only and may be
1331 * released. If it's released, *@nonblocking will be set to 0.
1332 */
1333long populate_vma_page_range(struct vm_area_struct *vma,
1334 unsigned long start, unsigned long end, int *nonblocking)
1335{
1336 struct mm_struct *mm = vma->vm_mm;
1337 unsigned long nr_pages = (end - start) / PAGE_SIZE;
1338 int gup_flags;
1339
1340 VM_BUG_ON(start & ~PAGE_MASK);
1341 VM_BUG_ON(end & ~PAGE_MASK);
1342 VM_BUG_ON_VMA(start < vma->vm_start, vma);
1343 VM_BUG_ON_VMA(end > vma->vm_end, vma);
1344 VM_BUG_ON_MM(!rwsem_is_locked(&mm->mmap_sem), mm);
1345
1346 gup_flags = FOLL_TOUCH | FOLL_POPULATE | FOLL_MLOCK;
1347 if (vma->vm_flags & VM_LOCKONFAULT)
1348 gup_flags &= ~FOLL_POPULATE;
1349 /*
1350 * We want to touch writable mappings with a write fault in order
1351 * to break COW, except for shared mappings because these don't COW
1352 * and we would not want to dirty them for nothing.
1353 */
1354 if ((vma->vm_flags & (VM_WRITE | VM_SHARED)) == VM_WRITE)
1355 gup_flags |= FOLL_WRITE;
1356
1357 /*
1358 * We want mlock to succeed for regions that have any permissions
1359 * other than PROT_NONE.
1360 */
1361 if (vma->vm_flags & (VM_READ | VM_WRITE | VM_EXEC))
1362 gup_flags |= FOLL_FORCE;
1363
1364 /*
1365 * We made sure addr is within a VMA, so the following will
1366 * not result in a stack expansion that recurses back here.
1367 */
1368 return __get_user_pages(current, mm, start, nr_pages, gup_flags,
1369 NULL, NULL, nonblocking);
1370}
1371
1372/*
1373 * __mm_populate - populate and/or mlock pages within a range of address space.
1374 *
1375 * This is used to implement mlock() and the MAP_POPULATE / MAP_LOCKED mmap
1376 * flags. VMAs must be already marked with the desired vm_flags, and
1377 * mmap_sem must not be held.
1378 */
1379int __mm_populate(unsigned long start, unsigned long len, int ignore_errors)
1380{
1381 struct mm_struct *mm = current->mm;
1382 unsigned long end, nstart, nend;
1383 struct vm_area_struct *vma = NULL;
1384 int locked = 0;
1385 long ret = 0;
1386
1387 end = start + len;
1388
1389 for (nstart = start; nstart < end; nstart = nend) {
1390 /*
1391 * We want to fault in pages for [nstart; end) address range.
1392 * Find first corresponding VMA.
1393 */
1394 if (!locked) {
1395 locked = 1;
1396 down_read(&mm->mmap_sem);
1397 vma = find_vma(mm, nstart);
1398 } else if (nstart >= vma->vm_end)
1399 vma = vma->vm_next;
1400 if (!vma || vma->vm_start >= end)
1401 break;
1402 /*
1403 * Set [nstart; nend) to intersection of desired address
1404 * range with the first VMA. Also, skip undesirable VMA types.
1405 */
1406 nend = min(end, vma->vm_end);
1407 if (vma->vm_flags & (VM_IO | VM_PFNMAP))
1408 continue;
1409 if (nstart < vma->vm_start)
1410 nstart = vma->vm_start;
1411 /*
1412 * Now fault in a range of pages. populate_vma_page_range()
1413 * double checks the vma flags, so that it won't mlock pages
1414 * if the vma was already munlocked.
1415 */
1416 ret = populate_vma_page_range(vma, nstart, nend, &locked);
1417 if (ret < 0) {
1418 if (ignore_errors) {
1419 ret = 0;
1420 continue; /* continue at next VMA */
1421 }
1422 break;
1423 }
1424 nend = nstart + ret * PAGE_SIZE;
1425 ret = 0;
1426 }
1427 if (locked)
1428 up_read(&mm->mmap_sem);
1429 return ret; /* 0 or negative error code */
1430}
1431
1432/**
1433 * get_dump_page() - pin user page in memory while writing it to core dump
1434 * @addr: user address
1435 *
1436 * Returns struct page pointer of user page pinned for dump,
1437 * to be freed afterwards by put_page().
1438 *
1439 * Returns NULL on any kind of failure - a hole must then be inserted into
1440 * the corefile, to preserve alignment with its headers; and also returns
1441 * NULL wherever the ZERO_PAGE, or an anonymous pte_none, has been found -
1442 * allowing a hole to be left in the corefile to save diskspace.
1443 *
1444 * Called without mmap_sem, but after all other threads have been killed.
1445 */
1446#ifdef CONFIG_ELF_CORE
1447struct page *get_dump_page(unsigned long addr)
1448{
1449 struct vm_area_struct *vma;
1450 struct page *page;
1451
1452 if (__get_user_pages(current, current->mm, addr, 1,
1453 FOLL_FORCE | FOLL_DUMP | FOLL_GET, &page, &vma,
1454 NULL) < 1)
1455 return NULL;
1456 flush_cache_page(vma, addr, page_to_pfn(page));
1457 return page;
1458}
1459#endif /* CONFIG_ELF_CORE */
Christoph Hellwig050a9ad2019-07-11 20:57:21 -07001460#else /* CONFIG_MMU */
1461static long __get_user_pages_locked(struct task_struct *tsk,
1462 struct mm_struct *mm, unsigned long start,
1463 unsigned long nr_pages, struct page **pages,
1464 struct vm_area_struct **vmas, int *locked,
1465 unsigned int foll_flags)
1466{
1467 struct vm_area_struct *vma;
1468 unsigned long vm_flags;
1469 int i;
1470
1471 /* calculate required read or write permissions.
1472 * If FOLL_FORCE is set, we only require the "MAY" flags.
1473 */
1474 vm_flags = (foll_flags & FOLL_WRITE) ?
1475 (VM_WRITE | VM_MAYWRITE) : (VM_READ | VM_MAYREAD);
1476 vm_flags &= (foll_flags & FOLL_FORCE) ?
1477 (VM_MAYREAD | VM_MAYWRITE) : (VM_READ | VM_WRITE);
1478
1479 for (i = 0; i < nr_pages; i++) {
1480 vma = find_vma(mm, start);
1481 if (!vma)
1482 goto finish_or_fault;
1483
1484 /* protect what we can, including chardevs */
1485 if ((vma->vm_flags & (VM_IO | VM_PFNMAP)) ||
1486 !(vm_flags & vma->vm_flags))
1487 goto finish_or_fault;
1488
1489 if (pages) {
1490 pages[i] = virt_to_page(start);
1491 if (pages[i])
1492 get_page(pages[i]);
1493 }
1494 if (vmas)
1495 vmas[i] = vma;
1496 start = (start + PAGE_SIZE) & PAGE_MASK;
1497 }
1498
1499 return i;
1500
1501finish_or_fault:
1502 return i ? : -EFAULT;
1503}
1504#endif /* !CONFIG_MMU */
Christoph Hellwigd3649f62019-07-11 20:57:18 -07001505
Aneesh Kumar K.V9a4e9f32019-03-05 15:47:44 -08001506#if defined(CONFIG_FS_DAX) || defined (CONFIG_CMA)
Aneesh Kumar K.V9a4e9f32019-03-05 15:47:44 -08001507static bool check_dax_vmas(struct vm_area_struct **vmas, long nr_pages)
1508{
1509 long i;
1510 struct vm_area_struct *vma_prev = NULL;
1511
1512 for (i = 0; i < nr_pages; i++) {
1513 struct vm_area_struct *vma = vmas[i];
1514
1515 if (vma == vma_prev)
1516 continue;
1517
1518 vma_prev = vma;
1519
1520 if (vma_is_fsdax(vma))
1521 return true;
1522 }
1523 return false;
1524}
Aneesh Kumar K.V9a4e9f32019-03-05 15:47:44 -08001525
1526#ifdef CONFIG_CMA
1527static struct page *new_non_cma_page(struct page *page, unsigned long private)
1528{
1529 /*
1530 * We want to make sure we allocate the new page from the same node
1531 * as the source page.
1532 */
1533 int nid = page_to_nid(page);
1534 /*
1535 * Trying to allocate a page for migration. Ignore allocation
1536 * failure warnings. We don't force __GFP_THISNODE here because
1537 * this node here is the node where we have CMA reservation and
1538 * in some case these nodes will have really less non movable
1539 * allocation memory.
1540 */
1541 gfp_t gfp_mask = GFP_USER | __GFP_NOWARN;
1542
1543 if (PageHighMem(page))
1544 gfp_mask |= __GFP_HIGHMEM;
1545
1546#ifdef CONFIG_HUGETLB_PAGE
1547 if (PageHuge(page)) {
1548 struct hstate *h = page_hstate(page);
1549 /*
1550 * We don't want to dequeue from the pool because pool pages will
1551 * mostly be from the CMA region.
1552 */
1553 return alloc_migrate_huge_page(h, gfp_mask, nid, NULL);
1554 }
1555#endif
1556 if (PageTransHuge(page)) {
1557 struct page *thp;
1558 /*
1559 * ignore allocation failure warnings
1560 */
1561 gfp_t thp_gfpmask = GFP_TRANSHUGE | __GFP_NOWARN;
1562
1563 /*
1564 * Remove the movable mask so that we don't allocate from
1565 * CMA area again.
1566 */
1567 thp_gfpmask &= ~__GFP_MOVABLE;
1568 thp = __alloc_pages_node(nid, thp_gfpmask, HPAGE_PMD_ORDER);
1569 if (!thp)
1570 return NULL;
1571 prep_transhuge_page(thp);
1572 return thp;
1573 }
1574
1575 return __alloc_pages_node(nid, gfp_mask, 0);
1576}
1577
Ira Weiny932f4a62019-05-13 17:17:03 -07001578static long check_and_migrate_cma_pages(struct task_struct *tsk,
1579 struct mm_struct *mm,
1580 unsigned long start,
1581 unsigned long nr_pages,
Aneesh Kumar K.V9a4e9f32019-03-05 15:47:44 -08001582 struct page **pages,
Ira Weiny932f4a62019-05-13 17:17:03 -07001583 struct vm_area_struct **vmas,
1584 unsigned int gup_flags)
Aneesh Kumar K.V9a4e9f32019-03-05 15:47:44 -08001585{
Pingfan Liuaa712392019-07-11 20:57:39 -07001586 unsigned long i;
1587 unsigned long step;
Aneesh Kumar K.V9a4e9f32019-03-05 15:47:44 -08001588 bool drain_allow = true;
1589 bool migrate_allow = true;
1590 LIST_HEAD(cma_page_list);
zhong jiangb96cc652019-11-30 17:49:50 -08001591 long ret = nr_pages;
Aneesh Kumar K.V9a4e9f32019-03-05 15:47:44 -08001592
1593check_again:
Pingfan Liuaa712392019-07-11 20:57:39 -07001594 for (i = 0; i < nr_pages;) {
1595
1596 struct page *head = compound_head(pages[i]);
1597
1598 /*
1599 * gup may start from a tail page. Advance step by the left
1600 * part.
1601 */
Matthew Wilcox (Oracle)d8c65462019-09-23 15:34:30 -07001602 step = compound_nr(head) - (pages[i] - head);
Aneesh Kumar K.V9a4e9f32019-03-05 15:47:44 -08001603 /*
1604 * If we get a page from the CMA zone, since we are going to
1605 * be pinning these entries, we might as well move them out
1606 * of the CMA zone if possible.
1607 */
Pingfan Liuaa712392019-07-11 20:57:39 -07001608 if (is_migrate_cma_page(head)) {
1609 if (PageHuge(head))
Aneesh Kumar K.V9a4e9f32019-03-05 15:47:44 -08001610 isolate_huge_page(head, &cma_page_list);
Pingfan Liuaa712392019-07-11 20:57:39 -07001611 else {
Aneesh Kumar K.V9a4e9f32019-03-05 15:47:44 -08001612 if (!PageLRU(head) && drain_allow) {
1613 lru_add_drain_all();
1614 drain_allow = false;
1615 }
1616
1617 if (!isolate_lru_page(head)) {
1618 list_add_tail(&head->lru, &cma_page_list);
1619 mod_node_page_state(page_pgdat(head),
1620 NR_ISOLATED_ANON +
1621 page_is_file_cache(head),
1622 hpage_nr_pages(head));
1623 }
1624 }
1625 }
Pingfan Liuaa712392019-07-11 20:57:39 -07001626
1627 i += step;
Aneesh Kumar K.V9a4e9f32019-03-05 15:47:44 -08001628 }
1629
1630 if (!list_empty(&cma_page_list)) {
1631 /*
1632 * drop the above get_user_pages reference.
1633 */
1634 for (i = 0; i < nr_pages; i++)
1635 put_page(pages[i]);
1636
1637 if (migrate_pages(&cma_page_list, new_non_cma_page,
1638 NULL, 0, MIGRATE_SYNC, MR_CONTIG_RANGE)) {
1639 /*
1640 * some of the pages failed migration. Do get_user_pages
1641 * without migration.
1642 */
1643 migrate_allow = false;
1644
1645 if (!list_empty(&cma_page_list))
1646 putback_movable_pages(&cma_page_list);
1647 }
1648 /*
Ira Weiny932f4a62019-05-13 17:17:03 -07001649 * We did migrate all the pages, Try to get the page references
1650 * again migrating any new CMA pages which we failed to isolate
1651 * earlier.
Aneesh Kumar K.V9a4e9f32019-03-05 15:47:44 -08001652 */
zhong jiangb96cc652019-11-30 17:49:50 -08001653 ret = __get_user_pages_locked(tsk, mm, start, nr_pages,
Ira Weiny932f4a62019-05-13 17:17:03 -07001654 pages, vmas, NULL,
1655 gup_flags);
1656
zhong jiangb96cc652019-11-30 17:49:50 -08001657 if ((ret > 0) && migrate_allow) {
1658 nr_pages = ret;
Aneesh Kumar K.V9a4e9f32019-03-05 15:47:44 -08001659 drain_allow = true;
1660 goto check_again;
1661 }
1662 }
1663
zhong jiangb96cc652019-11-30 17:49:50 -08001664 return ret;
Aneesh Kumar K.V9a4e9f32019-03-05 15:47:44 -08001665}
1666#else
Ira Weiny932f4a62019-05-13 17:17:03 -07001667static long check_and_migrate_cma_pages(struct task_struct *tsk,
1668 struct mm_struct *mm,
1669 unsigned long start,
1670 unsigned long nr_pages,
1671 struct page **pages,
1672 struct vm_area_struct **vmas,
1673 unsigned int gup_flags)
Aneesh Kumar K.V9a4e9f32019-03-05 15:47:44 -08001674{
1675 return nr_pages;
1676}
Christoph Hellwig050a9ad2019-07-11 20:57:21 -07001677#endif /* CONFIG_CMA */
Aneesh Kumar K.V9a4e9f32019-03-05 15:47:44 -08001678
Dan Williams2bb6d282017-11-29 16:10:35 -08001679/*
Ira Weiny932f4a62019-05-13 17:17:03 -07001680 * __gup_longterm_locked() is a wrapper for __get_user_pages_locked which
1681 * allows us to process the FOLL_LONGTERM flag.
Dan Williams2bb6d282017-11-29 16:10:35 -08001682 */
Ira Weiny932f4a62019-05-13 17:17:03 -07001683static long __gup_longterm_locked(struct task_struct *tsk,
1684 struct mm_struct *mm,
1685 unsigned long start,
1686 unsigned long nr_pages,
1687 struct page **pages,
1688 struct vm_area_struct **vmas,
1689 unsigned int gup_flags)
Dan Williams2bb6d282017-11-29 16:10:35 -08001690{
Ira Weiny932f4a62019-05-13 17:17:03 -07001691 struct vm_area_struct **vmas_tmp = vmas;
1692 unsigned long flags = 0;
Dan Williams2bb6d282017-11-29 16:10:35 -08001693 long rc, i;
1694
Ira Weiny932f4a62019-05-13 17:17:03 -07001695 if (gup_flags & FOLL_LONGTERM) {
1696 if (!pages)
1697 return -EINVAL;
Dan Williams2bb6d282017-11-29 16:10:35 -08001698
Ira Weiny932f4a62019-05-13 17:17:03 -07001699 if (!vmas_tmp) {
1700 vmas_tmp = kcalloc(nr_pages,
1701 sizeof(struct vm_area_struct *),
1702 GFP_KERNEL);
1703 if (!vmas_tmp)
1704 return -ENOMEM;
1705 }
1706 flags = memalloc_nocma_save();
Dan Williams2bb6d282017-11-29 16:10:35 -08001707 }
1708
Ira Weiny932f4a62019-05-13 17:17:03 -07001709 rc = __get_user_pages_locked(tsk, mm, start, nr_pages, pages,
1710 vmas_tmp, NULL, gup_flags);
Dan Williams2bb6d282017-11-29 16:10:35 -08001711
Ira Weiny932f4a62019-05-13 17:17:03 -07001712 if (gup_flags & FOLL_LONGTERM) {
1713 memalloc_nocma_restore(flags);
1714 if (rc < 0)
1715 goto out;
1716
1717 if (check_dax_vmas(vmas_tmp, rc)) {
1718 for (i = 0; i < rc; i++)
1719 put_page(pages[i]);
1720 rc = -EOPNOTSUPP;
1721 goto out;
1722 }
1723
1724 rc = check_and_migrate_cma_pages(tsk, mm, start, rc, pages,
1725 vmas_tmp, gup_flags);
Aneesh Kumar K.V9a4e9f32019-03-05 15:47:44 -08001726 }
1727
Dan Williams2bb6d282017-11-29 16:10:35 -08001728out:
Ira Weiny932f4a62019-05-13 17:17:03 -07001729 if (vmas_tmp != vmas)
1730 kfree(vmas_tmp);
Dan Williams2bb6d282017-11-29 16:10:35 -08001731 return rc;
1732}
Ira Weiny932f4a62019-05-13 17:17:03 -07001733#else /* !CONFIG_FS_DAX && !CONFIG_CMA */
1734static __always_inline long __gup_longterm_locked(struct task_struct *tsk,
1735 struct mm_struct *mm,
1736 unsigned long start,
1737 unsigned long nr_pages,
1738 struct page **pages,
1739 struct vm_area_struct **vmas,
1740 unsigned int flags)
1741{
1742 return __get_user_pages_locked(tsk, mm, start, nr_pages, pages, vmas,
1743 NULL, flags);
1744}
1745#endif /* CONFIG_FS_DAX || CONFIG_CMA */
1746
John Hubbard22bf29b2020-04-01 21:05:10 -07001747#ifdef CONFIG_MMU
1748static long __get_user_pages_remote(struct task_struct *tsk,
1749 struct mm_struct *mm,
1750 unsigned long start, unsigned long nr_pages,
1751 unsigned int gup_flags, struct page **pages,
1752 struct vm_area_struct **vmas, int *locked)
1753{
1754 /*
1755 * Parts of FOLL_LONGTERM behavior are incompatible with
1756 * FAULT_FLAG_ALLOW_RETRY because of the FS DAX check requirement on
1757 * vmas. However, this only comes up if locked is set, and there are
1758 * callers that do request FOLL_LONGTERM, but do not set locked. So,
1759 * allow what we can.
1760 */
1761 if (gup_flags & FOLL_LONGTERM) {
1762 if (WARN_ON_ONCE(locked))
1763 return -EINVAL;
1764 /*
1765 * This will check the vmas (even if our vmas arg is NULL)
1766 * and return -ENOTSUPP if DAX isn't allowed in this case:
1767 */
1768 return __gup_longterm_locked(tsk, mm, start, nr_pages, pages,
1769 vmas, gup_flags | FOLL_TOUCH |
1770 FOLL_REMOTE);
1771 }
1772
1773 return __get_user_pages_locked(tsk, mm, start, nr_pages, pages, vmas,
1774 locked,
1775 gup_flags | FOLL_TOUCH | FOLL_REMOTE);
1776}
1777
Ira Weiny932f4a62019-05-13 17:17:03 -07001778/*
John Hubbardc4237f82020-01-30 22:12:36 -08001779 * get_user_pages_remote() - pin user pages in memory
1780 * @tsk: the task_struct to use for page fault accounting, or
1781 * NULL if faults are not to be recorded.
1782 * @mm: mm_struct of target mm
1783 * @start: starting user address
1784 * @nr_pages: number of pages from start to pin
1785 * @gup_flags: flags modifying lookup behaviour
1786 * @pages: array that receives pointers to the pages pinned.
1787 * Should be at least nr_pages long. Or NULL, if caller
1788 * only intends to ensure the pages are faulted in.
1789 * @vmas: array of pointers to vmas corresponding to each page.
1790 * Or NULL if the caller does not require them.
1791 * @locked: pointer to lock flag indicating whether lock is held and
1792 * subsequently whether VM_FAULT_RETRY functionality can be
1793 * utilised. Lock must initially be held.
1794 *
1795 * Returns either number of pages pinned (which may be less than the
1796 * number requested), or an error. Details about the return value:
1797 *
1798 * -- If nr_pages is 0, returns 0.
1799 * -- If nr_pages is >0, but no pages were pinned, returns -errno.
1800 * -- If nr_pages is >0, and some pages were pinned, returns the number of
1801 * pages pinned. Again, this may be less than nr_pages.
1802 *
1803 * The caller is responsible for releasing returned @pages, via put_page().
1804 *
1805 * @vmas are valid only as long as mmap_sem is held.
1806 *
1807 * Must be called with mmap_sem held for read or write.
1808 *
1809 * get_user_pages walks a process's page tables and takes a reference to
1810 * each struct page that each user address corresponds to at a given
1811 * instant. That is, it takes the page that would be accessed if a user
1812 * thread accesses the given user virtual address at that instant.
1813 *
1814 * This does not guarantee that the page exists in the user mappings when
1815 * get_user_pages returns, and there may even be a completely different
1816 * page there in some cases (eg. if mmapped pagecache has been invalidated
1817 * and subsequently re faulted). However it does guarantee that the page
1818 * won't be freed completely. And mostly callers simply care that the page
1819 * contains data that was valid *at some point in time*. Typically, an IO
1820 * or similar operation cannot guarantee anything stronger anyway because
1821 * locks can't be held over the syscall boundary.
1822 *
1823 * If gup_flags & FOLL_WRITE == 0, the page must not be written to. If the page
1824 * is written to, set_page_dirty (or set_page_dirty_lock, as appropriate) must
1825 * be called after the page is finished with, and before put_page is called.
1826 *
1827 * get_user_pages is typically used for fewer-copy IO operations, to get a
1828 * handle on the memory by some means other than accesses via the user virtual
1829 * addresses. The pages may be submitted for DMA to devices or accessed via
1830 * their kernel linear mapping (via the kmap APIs). Care should be taken to
1831 * use the correct cache flushing APIs.
1832 *
1833 * See also get_user_pages_fast, for performance critical applications.
1834 *
1835 * get_user_pages should be phased out in favor of
1836 * get_user_pages_locked|unlocked or get_user_pages_fast. Nothing
1837 * should use get_user_pages because it cannot pass
1838 * FAULT_FLAG_ALLOW_RETRY to handle_mm_fault.
1839 */
1840long get_user_pages_remote(struct task_struct *tsk, struct mm_struct *mm,
1841 unsigned long start, unsigned long nr_pages,
1842 unsigned int gup_flags, struct page **pages,
1843 struct vm_area_struct **vmas, int *locked)
1844{
1845 /*
John Hubbardeddb1c22020-01-30 22:12:54 -08001846 * FOLL_PIN must only be set internally by the pin_user_pages*() APIs,
1847 * never directly by the caller, so enforce that with an assertion:
1848 */
1849 if (WARN_ON_ONCE(gup_flags & FOLL_PIN))
1850 return -EINVAL;
1851
John Hubbard22bf29b2020-04-01 21:05:10 -07001852 return __get_user_pages_remote(tsk, mm, start, nr_pages, gup_flags,
1853 pages, vmas, locked);
John Hubbardc4237f82020-01-30 22:12:36 -08001854}
1855EXPORT_SYMBOL(get_user_pages_remote);
1856
John Hubbardeddb1c22020-01-30 22:12:54 -08001857#else /* CONFIG_MMU */
1858long get_user_pages_remote(struct task_struct *tsk, struct mm_struct *mm,
1859 unsigned long start, unsigned long nr_pages,
1860 unsigned int gup_flags, struct page **pages,
1861 struct vm_area_struct **vmas, int *locked)
1862{
1863 return 0;
1864}
John Hubbard3faa52c2020-04-01 21:05:29 -07001865
1866static long __get_user_pages_remote(struct task_struct *tsk,
1867 struct mm_struct *mm,
1868 unsigned long start, unsigned long nr_pages,
1869 unsigned int gup_flags, struct page **pages,
1870 struct vm_area_struct **vmas, int *locked)
1871{
1872 return 0;
1873}
John Hubbardeddb1c22020-01-30 22:12:54 -08001874#endif /* !CONFIG_MMU */
1875
John Hubbardc4237f82020-01-30 22:12:36 -08001876/*
Ira Weiny932f4a62019-05-13 17:17:03 -07001877 * This is the same as get_user_pages_remote(), just with a
1878 * less-flexible calling convention where we assume that the task
1879 * and mm being operated on are the current task's and don't allow
1880 * passing of a locked parameter. We also obviously don't pass
1881 * FOLL_REMOTE in here.
1882 */
1883long get_user_pages(unsigned long start, unsigned long nr_pages,
1884 unsigned int gup_flags, struct page **pages,
1885 struct vm_area_struct **vmas)
1886{
John Hubbardeddb1c22020-01-30 22:12:54 -08001887 /*
1888 * FOLL_PIN must only be set internally by the pin_user_pages*() APIs,
1889 * never directly by the caller, so enforce that with an assertion:
1890 */
1891 if (WARN_ON_ONCE(gup_flags & FOLL_PIN))
1892 return -EINVAL;
1893
Ira Weiny932f4a62019-05-13 17:17:03 -07001894 return __gup_longterm_locked(current, current->mm, start, nr_pages,
1895 pages, vmas, gup_flags | FOLL_TOUCH);
1896}
1897EXPORT_SYMBOL(get_user_pages);
Dan Williams2bb6d282017-11-29 16:10:35 -08001898
Christoph Hellwigd3649f62019-07-11 20:57:18 -07001899/*
1900 * We can leverage the VM_FAULT_RETRY functionality in the page fault
1901 * paths better by using either get_user_pages_locked() or
1902 * get_user_pages_unlocked().
Kirill A. Shutemovacc3c8d2015-04-14 15:44:45 -07001903 *
Christoph Hellwigd3649f62019-07-11 20:57:18 -07001904 * get_user_pages_locked() is suitable to replace the form:
Kirill A. Shutemovacc3c8d2015-04-14 15:44:45 -07001905 *
Christoph Hellwigd3649f62019-07-11 20:57:18 -07001906 * down_read(&mm->mmap_sem);
1907 * do_something()
1908 * get_user_pages(tsk, mm, ..., pages, NULL);
1909 * up_read(&mm->mmap_sem);
Kirill A. Shutemovacc3c8d2015-04-14 15:44:45 -07001910 *
Christoph Hellwigd3649f62019-07-11 20:57:18 -07001911 * to:
Kirill A. Shutemovacc3c8d2015-04-14 15:44:45 -07001912 *
Christoph Hellwigd3649f62019-07-11 20:57:18 -07001913 * int locked = 1;
1914 * down_read(&mm->mmap_sem);
1915 * do_something()
1916 * get_user_pages_locked(tsk, mm, ..., pages, &locked);
1917 * if (locked)
1918 * up_read(&mm->mmap_sem);
Kirill A. Shutemovacc3c8d2015-04-14 15:44:45 -07001919 */
Christoph Hellwigd3649f62019-07-11 20:57:18 -07001920long get_user_pages_locked(unsigned long start, unsigned long nr_pages,
1921 unsigned int gup_flags, struct page **pages,
1922 int *locked)
Kirill A. Shutemovacc3c8d2015-04-14 15:44:45 -07001923{
Kirill A. Shutemovacc3c8d2015-04-14 15:44:45 -07001924 /*
Christoph Hellwigd3649f62019-07-11 20:57:18 -07001925 * FIXME: Current FOLL_LONGTERM behavior is incompatible with
1926 * FAULT_FLAG_ALLOW_RETRY because of the FS DAX check requirement on
1927 * vmas. As there are no users of this flag in this call we simply
1928 * disallow this option for now.
Kirill A. Shutemovacc3c8d2015-04-14 15:44:45 -07001929 */
Christoph Hellwigd3649f62019-07-11 20:57:18 -07001930 if (WARN_ON_ONCE(gup_flags & FOLL_LONGTERM))
1931 return -EINVAL;
Kirill A. Shutemovacc3c8d2015-04-14 15:44:45 -07001932
Christoph Hellwigd3649f62019-07-11 20:57:18 -07001933 return __get_user_pages_locked(current, current->mm, start, nr_pages,
1934 pages, NULL, locked,
1935 gup_flags | FOLL_TOUCH);
Kirill A. Shutemovacc3c8d2015-04-14 15:44:45 -07001936}
Christoph Hellwigd3649f62019-07-11 20:57:18 -07001937EXPORT_SYMBOL(get_user_pages_locked);
Kirill A. Shutemovacc3c8d2015-04-14 15:44:45 -07001938
1939/*
Christoph Hellwigd3649f62019-07-11 20:57:18 -07001940 * get_user_pages_unlocked() is suitable to replace the form:
Kirill A. Shutemovacc3c8d2015-04-14 15:44:45 -07001941 *
Christoph Hellwigd3649f62019-07-11 20:57:18 -07001942 * down_read(&mm->mmap_sem);
1943 * get_user_pages(tsk, mm, ..., pages, NULL);
1944 * up_read(&mm->mmap_sem);
1945 *
1946 * with:
1947 *
1948 * get_user_pages_unlocked(tsk, mm, ..., pages);
1949 *
1950 * It is functionally equivalent to get_user_pages_fast so
1951 * get_user_pages_fast should be used instead if specific gup_flags
1952 * (e.g. FOLL_FORCE) are not required.
Kirill A. Shutemovacc3c8d2015-04-14 15:44:45 -07001953 */
Christoph Hellwigd3649f62019-07-11 20:57:18 -07001954long get_user_pages_unlocked(unsigned long start, unsigned long nr_pages,
1955 struct page **pages, unsigned int gup_flags)
Kirill A. Shutemovacc3c8d2015-04-14 15:44:45 -07001956{
1957 struct mm_struct *mm = current->mm;
Christoph Hellwigd3649f62019-07-11 20:57:18 -07001958 int locked = 1;
1959 long ret;
Kirill A. Shutemovacc3c8d2015-04-14 15:44:45 -07001960
Christoph Hellwigd3649f62019-07-11 20:57:18 -07001961 /*
1962 * FIXME: Current FOLL_LONGTERM behavior is incompatible with
1963 * FAULT_FLAG_ALLOW_RETRY because of the FS DAX check requirement on
1964 * vmas. As there are no users of this flag in this call we simply
1965 * disallow this option for now.
1966 */
1967 if (WARN_ON_ONCE(gup_flags & FOLL_LONGTERM))
1968 return -EINVAL;
Kirill A. Shutemovacc3c8d2015-04-14 15:44:45 -07001969
Christoph Hellwigd3649f62019-07-11 20:57:18 -07001970 down_read(&mm->mmap_sem);
1971 ret = __get_user_pages_locked(current, mm, start, nr_pages, pages, NULL,
1972 &locked, gup_flags | FOLL_TOUCH);
Kirill A. Shutemovacc3c8d2015-04-14 15:44:45 -07001973 if (locked)
1974 up_read(&mm->mmap_sem);
Christoph Hellwigd3649f62019-07-11 20:57:18 -07001975 return ret;
Kirill A. Shutemovacc3c8d2015-04-14 15:44:45 -07001976}
Christoph Hellwigd3649f62019-07-11 20:57:18 -07001977EXPORT_SYMBOL(get_user_pages_unlocked);
Steve Capper2667f502014-10-09 15:29:14 -07001978
1979/*
Christoph Hellwig67a929e2019-07-11 20:57:14 -07001980 * Fast GUP
Steve Capper2667f502014-10-09 15:29:14 -07001981 *
1982 * get_user_pages_fast attempts to pin user pages by walking the page
1983 * tables directly and avoids taking locks. Thus the walker needs to be
1984 * protected from page table pages being freed from under it, and should
1985 * block any THP splits.
1986 *
1987 * One way to achieve this is to have the walker disable interrupts, and
1988 * rely on IPIs from the TLB flushing code blocking before the page table
1989 * pages are freed. This is unsuitable for architectures that do not need
1990 * to broadcast an IPI when invalidating TLBs.
1991 *
1992 * Another way to achieve this is to batch up page table containing pages
1993 * belonging to more than one mm_user, then rcu_sched a callback to free those
1994 * pages. Disabling interrupts will allow the fast_gup walker to both block
1995 * the rcu_sched callback, and an IPI that we broadcast for splitting THPs
1996 * (which is a relatively rare event). The code below adopts this strategy.
1997 *
1998 * Before activating this code, please be aware that the following assumptions
1999 * are currently made:
2000 *
Peter Zijlstraff2e6d722020-02-03 17:37:02 -08002001 * *) Either MMU_GATHER_RCU_TABLE_FREE is enabled, and tlb_remove_table() is used to
Kirill A. Shutemove5855132017-06-06 14:31:20 +03002002 * free pages containing page tables or TLB flushing requires IPI broadcast.
Steve Capper2667f502014-10-09 15:29:14 -07002003 *
Steve Capper2667f502014-10-09 15:29:14 -07002004 * *) ptes can be read atomically by the architecture.
2005 *
2006 * *) access_ok is sufficient to validate userspace address ranges.
2007 *
2008 * The last two assumptions can be relaxed by the addition of helper functions.
2009 *
2010 * This code is based heavily on the PowerPC implementation by Nick Piggin.
2011 */
Christoph Hellwig67a929e2019-07-11 20:57:14 -07002012#ifdef CONFIG_HAVE_FAST_GUP
John Hubbard3faa52c2020-04-01 21:05:29 -07002013
2014static void put_compound_head(struct page *page, int refs, unsigned int flags)
2015{
John Hubbard47e29d32020-04-01 21:05:33 -07002016 if (flags & FOLL_PIN) {
2017 if (hpage_pincount_available(page))
2018 hpage_pincount_sub(page, refs);
2019 else
2020 refs *= GUP_PIN_COUNTING_BIAS;
2021 }
John Hubbard3faa52c2020-04-01 21:05:29 -07002022
2023 VM_BUG_ON_PAGE(page_ref_count(page) < refs, page);
2024 /*
2025 * Calling put_page() for each ref is unnecessarily slow. Only the last
2026 * ref needs a put_page().
2027 */
2028 if (refs > 1)
2029 page_ref_sub(page, refs - 1);
2030 put_page(page);
2031}
2032
Christoph Hellwig39656e82019-07-11 20:56:49 -07002033#ifdef CONFIG_GUP_GET_PTE_LOW_HIGH
John Hubbard3faa52c2020-04-01 21:05:29 -07002034
Kirill A. Shutemov0005d202017-03-16 18:26:51 +03002035/*
Christoph Hellwig39656e82019-07-11 20:56:49 -07002036 * WARNING: only to be used in the get_user_pages_fast() implementation.
2037 *
2038 * With get_user_pages_fast(), we walk down the pagetables without taking any
2039 * locks. For this we would like to load the pointers atomically, but sometimes
2040 * that is not possible (e.g. without expensive cmpxchg8b on x86_32 PAE). What
2041 * we do have is the guarantee that a PTE will only either go from not present
2042 * to present, or present to not present or both -- it will not switch to a
2043 * completely different present page without a TLB flush in between; something
2044 * that we are blocking by holding interrupts off.
2045 *
2046 * Setting ptes from not present to present goes:
2047 *
2048 * ptep->pte_high = h;
2049 * smp_wmb();
2050 * ptep->pte_low = l;
2051 *
2052 * And present to not present goes:
2053 *
2054 * ptep->pte_low = 0;
2055 * smp_wmb();
2056 * ptep->pte_high = 0;
2057 *
2058 * We must ensure here that the load of pte_low sees 'l' IFF pte_high sees 'h'.
2059 * We load pte_high *after* loading pte_low, which ensures we don't see an older
2060 * value of pte_high. *Then* we recheck pte_low, which ensures that we haven't
2061 * picked up a changed pte high. We might have gotten rubbish values from
2062 * pte_low and pte_high, but we are guaranteed that pte_low will not have the
2063 * present bit set *unless* it is 'l'. Because get_user_pages_fast() only
2064 * operates on present ptes we're safe.
2065 */
2066static inline pte_t gup_get_pte(pte_t *ptep)
2067{
2068 pte_t pte;
2069
2070 do {
2071 pte.pte_low = ptep->pte_low;
2072 smp_rmb();
2073 pte.pte_high = ptep->pte_high;
2074 smp_rmb();
2075 } while (unlikely(pte.pte_low != ptep->pte_low));
2076
2077 return pte;
2078}
2079#else /* CONFIG_GUP_GET_PTE_LOW_HIGH */
2080/*
2081 * We require that the PTE can be read atomically.
Kirill A. Shutemov0005d202017-03-16 18:26:51 +03002082 */
2083static inline pte_t gup_get_pte(pte_t *ptep)
2084{
2085 return READ_ONCE(*ptep);
2086}
Christoph Hellwig39656e82019-07-11 20:56:49 -07002087#endif /* CONFIG_GUP_GET_PTE_LOW_HIGH */
Kirill A. Shutemov0005d202017-03-16 18:26:51 +03002088
Guenter Roeck790c7362019-07-11 20:57:46 -07002089static void __maybe_unused undo_dev_pagemap(int *nr, int nr_start,
John Hubbard3b78d832020-04-01 21:05:22 -07002090 unsigned int flags,
Guenter Roeck790c7362019-07-11 20:57:46 -07002091 struct page **pages)
Kirill A. Shutemovb59f65f2017-03-16 18:26:53 +03002092{
2093 while ((*nr) - nr_start) {
2094 struct page *page = pages[--(*nr)];
2095
2096 ClearPageReferenced(page);
John Hubbard3faa52c2020-04-01 21:05:29 -07002097 if (flags & FOLL_PIN)
2098 unpin_user_page(page);
2099 else
2100 put_page(page);
Kirill A. Shutemovb59f65f2017-03-16 18:26:53 +03002101 }
2102}
2103
Laurent Dufour3010a5e2018-06-07 17:06:08 -07002104#ifdef CONFIG_ARCH_HAS_PTE_SPECIAL
Steve Capper2667f502014-10-09 15:29:14 -07002105static int gup_pte_range(pmd_t pmd, unsigned long addr, unsigned long end,
Ira Weinyb798bec2019-05-13 17:17:07 -07002106 unsigned int flags, struct page **pages, int *nr)
Steve Capper2667f502014-10-09 15:29:14 -07002107{
Kirill A. Shutemovb59f65f2017-03-16 18:26:53 +03002108 struct dev_pagemap *pgmap = NULL;
2109 int nr_start = *nr, ret = 0;
Steve Capper2667f502014-10-09 15:29:14 -07002110 pte_t *ptep, *ptem;
Steve Capper2667f502014-10-09 15:29:14 -07002111
2112 ptem = ptep = pte_offset_map(&pmd, addr);
2113 do {
Kirill A. Shutemov0005d202017-03-16 18:26:51 +03002114 pte_t pte = gup_get_pte(ptep);
Kirill A. Shutemov7aef4172016-01-15 16:52:32 -08002115 struct page *head, *page;
Steve Capper2667f502014-10-09 15:29:14 -07002116
2117 /*
2118 * Similar to the PMD case below, NUMA hinting must take slow
Mel Gorman8a0516e2015-02-12 14:58:22 -08002119 * path using the pte_protnone check.
Steve Capper2667f502014-10-09 15:29:14 -07002120 */
Kirill A. Shutemove7884f82017-03-16 18:26:50 +03002121 if (pte_protnone(pte))
2122 goto pte_unmap;
2123
Ira Weinyb798bec2019-05-13 17:17:07 -07002124 if (!pte_access_permitted(pte, flags & FOLL_WRITE))
Kirill A. Shutemove7884f82017-03-16 18:26:50 +03002125 goto pte_unmap;
2126
Kirill A. Shutemovb59f65f2017-03-16 18:26:53 +03002127 if (pte_devmap(pte)) {
Ira Weiny7af75562019-05-13 17:17:14 -07002128 if (unlikely(flags & FOLL_LONGTERM))
2129 goto pte_unmap;
2130
Kirill A. Shutemovb59f65f2017-03-16 18:26:53 +03002131 pgmap = get_dev_pagemap(pte_pfn(pte), pgmap);
2132 if (unlikely(!pgmap)) {
John Hubbard3b78d832020-04-01 21:05:22 -07002133 undo_dev_pagemap(nr, nr_start, flags, pages);
Kirill A. Shutemovb59f65f2017-03-16 18:26:53 +03002134 goto pte_unmap;
2135 }
2136 } else if (pte_special(pte))
Steve Capper2667f502014-10-09 15:29:14 -07002137 goto pte_unmap;
2138
2139 VM_BUG_ON(!pfn_valid(pte_pfn(pte)));
2140 page = pte_page(pte);
2141
John Hubbard3faa52c2020-04-01 21:05:29 -07002142 head = try_grab_compound_head(page, 1, flags);
Linus Torvalds8fde12c2019-04-11 10:49:19 -07002143 if (!head)
Steve Capper2667f502014-10-09 15:29:14 -07002144 goto pte_unmap;
2145
2146 if (unlikely(pte_val(pte) != pte_val(*ptep))) {
John Hubbard3faa52c2020-04-01 21:05:29 -07002147 put_compound_head(head, 1, flags);
Steve Capper2667f502014-10-09 15:29:14 -07002148 goto pte_unmap;
2149 }
2150
Kirill A. Shutemov7aef4172016-01-15 16:52:32 -08002151 VM_BUG_ON_PAGE(compound_head(page) != head, page);
Kirill A. Shutemove9348052017-03-16 18:26:52 +03002152
2153 SetPageReferenced(page);
Steve Capper2667f502014-10-09 15:29:14 -07002154 pages[*nr] = page;
2155 (*nr)++;
2156
2157 } while (ptep++, addr += PAGE_SIZE, addr != end);
2158
2159 ret = 1;
2160
2161pte_unmap:
Christoph Hellwig832d7aa2017-12-29 08:54:01 +01002162 if (pgmap)
2163 put_dev_pagemap(pgmap);
Steve Capper2667f502014-10-09 15:29:14 -07002164 pte_unmap(ptem);
2165 return ret;
2166}
2167#else
2168
2169/*
2170 * If we can't determine whether or not a pte is special, then fail immediately
2171 * for ptes. Note, we can still pin HugeTLB and THP as these are guaranteed not
2172 * to be special.
2173 *
2174 * For a futex to be placed on a THP tail page, get_futex_key requires a
2175 * __get_user_pages_fast implementation that can pin pages. Thus it's still
2176 * useful to have gup_huge_pmd even if we can't operate on ptes.
2177 */
2178static int gup_pte_range(pmd_t pmd, unsigned long addr, unsigned long end,
Ira Weinyb798bec2019-05-13 17:17:07 -07002179 unsigned int flags, struct page **pages, int *nr)
Steve Capper2667f502014-10-09 15:29:14 -07002180{
2181 return 0;
2182}
Laurent Dufour3010a5e2018-06-07 17:06:08 -07002183#endif /* CONFIG_ARCH_HAS_PTE_SPECIAL */
Steve Capper2667f502014-10-09 15:29:14 -07002184
Robin Murphy17596732019-07-16 16:30:47 -07002185#if defined(CONFIG_ARCH_HAS_PTE_DEVMAP) && defined(CONFIG_TRANSPARENT_HUGEPAGE)
Kirill A. Shutemovb59f65f2017-03-16 18:26:53 +03002186static int __gup_device_huge(unsigned long pfn, unsigned long addr,
John Hubbard86dfbed2020-04-01 21:05:14 -07002187 unsigned long end, unsigned int flags,
2188 struct page **pages, int *nr)
Kirill A. Shutemovb59f65f2017-03-16 18:26:53 +03002189{
2190 int nr_start = *nr;
2191 struct dev_pagemap *pgmap = NULL;
2192
2193 do {
2194 struct page *page = pfn_to_page(pfn);
2195
2196 pgmap = get_dev_pagemap(pfn, pgmap);
2197 if (unlikely(!pgmap)) {
John Hubbard3b78d832020-04-01 21:05:22 -07002198 undo_dev_pagemap(nr, nr_start, flags, pages);
Kirill A. Shutemovb59f65f2017-03-16 18:26:53 +03002199 return 0;
2200 }
2201 SetPageReferenced(page);
2202 pages[*nr] = page;
John Hubbard3faa52c2020-04-01 21:05:29 -07002203 if (unlikely(!try_grab_page(page, flags))) {
2204 undo_dev_pagemap(nr, nr_start, flags, pages);
2205 return 0;
2206 }
Kirill A. Shutemovb59f65f2017-03-16 18:26:53 +03002207 (*nr)++;
2208 pfn++;
2209 } while (addr += PAGE_SIZE, addr != end);
Christoph Hellwig832d7aa2017-12-29 08:54:01 +01002210
2211 if (pgmap)
2212 put_dev_pagemap(pgmap);
Kirill A. Shutemovb59f65f2017-03-16 18:26:53 +03002213 return 1;
2214}
2215
Dan Williamsa9b6de72018-04-19 21:32:19 -07002216static int __gup_device_huge_pmd(pmd_t orig, pmd_t *pmdp, unsigned long addr,
John Hubbard86dfbed2020-04-01 21:05:14 -07002217 unsigned long end, unsigned int flags,
2218 struct page **pages, int *nr)
Kirill A. Shutemovb59f65f2017-03-16 18:26:53 +03002219{
2220 unsigned long fault_pfn;
Dan Williamsa9b6de72018-04-19 21:32:19 -07002221 int nr_start = *nr;
Kirill A. Shutemovb59f65f2017-03-16 18:26:53 +03002222
Dan Williamsa9b6de72018-04-19 21:32:19 -07002223 fault_pfn = pmd_pfn(orig) + ((addr & ~PMD_MASK) >> PAGE_SHIFT);
John Hubbard86dfbed2020-04-01 21:05:14 -07002224 if (!__gup_device_huge(fault_pfn, addr, end, flags, pages, nr))
Dan Williamsa9b6de72018-04-19 21:32:19 -07002225 return 0;
2226
2227 if (unlikely(pmd_val(orig) != pmd_val(*pmdp))) {
John Hubbard3b78d832020-04-01 21:05:22 -07002228 undo_dev_pagemap(nr, nr_start, flags, pages);
Dan Williamsa9b6de72018-04-19 21:32:19 -07002229 return 0;
2230 }
2231 return 1;
Kirill A. Shutemovb59f65f2017-03-16 18:26:53 +03002232}
2233
Dan Williamsa9b6de72018-04-19 21:32:19 -07002234static int __gup_device_huge_pud(pud_t orig, pud_t *pudp, unsigned long addr,
John Hubbard86dfbed2020-04-01 21:05:14 -07002235 unsigned long end, unsigned int flags,
2236 struct page **pages, int *nr)
Kirill A. Shutemovb59f65f2017-03-16 18:26:53 +03002237{
2238 unsigned long fault_pfn;
Dan Williamsa9b6de72018-04-19 21:32:19 -07002239 int nr_start = *nr;
Kirill A. Shutemovb59f65f2017-03-16 18:26:53 +03002240
Dan Williamsa9b6de72018-04-19 21:32:19 -07002241 fault_pfn = pud_pfn(orig) + ((addr & ~PUD_MASK) >> PAGE_SHIFT);
John Hubbard86dfbed2020-04-01 21:05:14 -07002242 if (!__gup_device_huge(fault_pfn, addr, end, flags, pages, nr))
Dan Williamsa9b6de72018-04-19 21:32:19 -07002243 return 0;
2244
2245 if (unlikely(pud_val(orig) != pud_val(*pudp))) {
John Hubbard3b78d832020-04-01 21:05:22 -07002246 undo_dev_pagemap(nr, nr_start, flags, pages);
Dan Williamsa9b6de72018-04-19 21:32:19 -07002247 return 0;
2248 }
2249 return 1;
Kirill A. Shutemovb59f65f2017-03-16 18:26:53 +03002250}
2251#else
Dan Williamsa9b6de72018-04-19 21:32:19 -07002252static int __gup_device_huge_pmd(pmd_t orig, pmd_t *pmdp, unsigned long addr,
John Hubbard86dfbed2020-04-01 21:05:14 -07002253 unsigned long end, unsigned int flags,
2254 struct page **pages, int *nr)
Kirill A. Shutemovb59f65f2017-03-16 18:26:53 +03002255{
2256 BUILD_BUG();
2257 return 0;
2258}
2259
Dan Williamsa9b6de72018-04-19 21:32:19 -07002260static int __gup_device_huge_pud(pud_t pud, pud_t *pudp, unsigned long addr,
John Hubbard86dfbed2020-04-01 21:05:14 -07002261 unsigned long end, unsigned int flags,
2262 struct page **pages, int *nr)
Kirill A. Shutemovb59f65f2017-03-16 18:26:53 +03002263{
2264 BUILD_BUG();
2265 return 0;
2266}
2267#endif
2268
John Hubbarda43e9822020-01-30 22:12:17 -08002269static int record_subpages(struct page *page, unsigned long addr,
2270 unsigned long end, struct page **pages)
2271{
2272 int nr;
2273
2274 for (nr = 0; addr != end; addr += PAGE_SIZE)
2275 pages[nr++] = page++;
2276
2277 return nr;
2278}
2279
Christoph Hellwigcbd34da2019-07-11 20:57:28 -07002280#ifdef CONFIG_ARCH_HAS_HUGEPD
2281static unsigned long hugepte_addr_end(unsigned long addr, unsigned long end,
2282 unsigned long sz)
2283{
2284 unsigned long __boundary = (addr + sz) & ~(sz-1);
2285 return (__boundary - 1 < end - 1) ? __boundary : end;
2286}
2287
2288static int gup_hugepte(pte_t *ptep, unsigned long sz, unsigned long addr,
John Hubbard0cd22af2019-10-18 20:19:53 -07002289 unsigned long end, unsigned int flags,
2290 struct page **pages, int *nr)
Christoph Hellwigcbd34da2019-07-11 20:57:28 -07002291{
2292 unsigned long pte_end;
2293 struct page *head, *page;
2294 pte_t pte;
2295 int refs;
2296
2297 pte_end = (addr + sz) & ~(sz-1);
2298 if (pte_end < end)
2299 end = pte_end;
2300
2301 pte = READ_ONCE(*ptep);
2302
John Hubbard0cd22af2019-10-18 20:19:53 -07002303 if (!pte_access_permitted(pte, flags & FOLL_WRITE))
Christoph Hellwigcbd34da2019-07-11 20:57:28 -07002304 return 0;
2305
2306 /* hugepages are never "special" */
2307 VM_BUG_ON(!pfn_valid(pte_pfn(pte)));
2308
Christoph Hellwigcbd34da2019-07-11 20:57:28 -07002309 head = pte_page(pte);
Christoph Hellwigcbd34da2019-07-11 20:57:28 -07002310 page = head + ((addr & (sz-1)) >> PAGE_SHIFT);
John Hubbarda43e9822020-01-30 22:12:17 -08002311 refs = record_subpages(page, addr, end, pages + *nr);
Christoph Hellwigcbd34da2019-07-11 20:57:28 -07002312
John Hubbard3faa52c2020-04-01 21:05:29 -07002313 head = try_grab_compound_head(head, refs, flags);
John Hubbarda43e9822020-01-30 22:12:17 -08002314 if (!head)
Christoph Hellwigcbd34da2019-07-11 20:57:28 -07002315 return 0;
Christoph Hellwigcbd34da2019-07-11 20:57:28 -07002316
2317 if (unlikely(pte_val(pte) != pte_val(*ptep))) {
John Hubbard3b78d832020-04-01 21:05:22 -07002318 put_compound_head(head, refs, flags);
Christoph Hellwigcbd34da2019-07-11 20:57:28 -07002319 return 0;
2320 }
2321
John Hubbarda43e9822020-01-30 22:12:17 -08002322 *nr += refs;
Christoph Hellwig520b4a42019-07-11 20:57:36 -07002323 SetPageReferenced(head);
Christoph Hellwigcbd34da2019-07-11 20:57:28 -07002324 return 1;
2325}
2326
2327static int gup_huge_pd(hugepd_t hugepd, unsigned long addr,
John Hubbard0cd22af2019-10-18 20:19:53 -07002328 unsigned int pdshift, unsigned long end, unsigned int flags,
Christoph Hellwigcbd34da2019-07-11 20:57:28 -07002329 struct page **pages, int *nr)
2330{
2331 pte_t *ptep;
2332 unsigned long sz = 1UL << hugepd_shift(hugepd);
2333 unsigned long next;
2334
2335 ptep = hugepte_offset(hugepd, addr, pdshift);
2336 do {
2337 next = hugepte_addr_end(addr, end, sz);
John Hubbard0cd22af2019-10-18 20:19:53 -07002338 if (!gup_hugepte(ptep, sz, addr, end, flags, pages, nr))
Christoph Hellwigcbd34da2019-07-11 20:57:28 -07002339 return 0;
2340 } while (ptep++, addr = next, addr != end);
2341
2342 return 1;
2343}
2344#else
2345static inline int gup_huge_pd(hugepd_t hugepd, unsigned long addr,
John Hubbard0cd22af2019-10-18 20:19:53 -07002346 unsigned int pdshift, unsigned long end, unsigned int flags,
Christoph Hellwigcbd34da2019-07-11 20:57:28 -07002347 struct page **pages, int *nr)
2348{
2349 return 0;
2350}
2351#endif /* CONFIG_ARCH_HAS_HUGEPD */
2352
Steve Capper2667f502014-10-09 15:29:14 -07002353static int gup_huge_pmd(pmd_t orig, pmd_t *pmdp, unsigned long addr,
John Hubbard0cd22af2019-10-18 20:19:53 -07002354 unsigned long end, unsigned int flags,
2355 struct page **pages, int *nr)
Steve Capper2667f502014-10-09 15:29:14 -07002356{
Kirill A. Shutemovddc58f22016-01-15 16:52:56 -08002357 struct page *head, *page;
Steve Capper2667f502014-10-09 15:29:14 -07002358 int refs;
2359
Ira Weinyb798bec2019-05-13 17:17:07 -07002360 if (!pmd_access_permitted(orig, flags & FOLL_WRITE))
Steve Capper2667f502014-10-09 15:29:14 -07002361 return 0;
2362
Ira Weiny7af75562019-05-13 17:17:14 -07002363 if (pmd_devmap(orig)) {
2364 if (unlikely(flags & FOLL_LONGTERM))
2365 return 0;
John Hubbard86dfbed2020-04-01 21:05:14 -07002366 return __gup_device_huge_pmd(orig, pmdp, addr, end, flags,
2367 pages, nr);
Ira Weiny7af75562019-05-13 17:17:14 -07002368 }
Kirill A. Shutemovb59f65f2017-03-16 18:26:53 +03002369
Punit Agrawald63206e2017-07-06 15:39:39 -07002370 page = pmd_page(orig) + ((addr & ~PMD_MASK) >> PAGE_SHIFT);
John Hubbarda43e9822020-01-30 22:12:17 -08002371 refs = record_subpages(page, addr, end, pages + *nr);
Steve Capper2667f502014-10-09 15:29:14 -07002372
John Hubbard3faa52c2020-04-01 21:05:29 -07002373 head = try_grab_compound_head(pmd_page(orig), refs, flags);
John Hubbarda43e9822020-01-30 22:12:17 -08002374 if (!head)
Steve Capper2667f502014-10-09 15:29:14 -07002375 return 0;
Steve Capper2667f502014-10-09 15:29:14 -07002376
2377 if (unlikely(pmd_val(orig) != pmd_val(*pmdp))) {
John Hubbard3b78d832020-04-01 21:05:22 -07002378 put_compound_head(head, refs, flags);
Steve Capper2667f502014-10-09 15:29:14 -07002379 return 0;
2380 }
2381
John Hubbarda43e9822020-01-30 22:12:17 -08002382 *nr += refs;
Kirill A. Shutemove9348052017-03-16 18:26:52 +03002383 SetPageReferenced(head);
Steve Capper2667f502014-10-09 15:29:14 -07002384 return 1;
2385}
2386
2387static int gup_huge_pud(pud_t orig, pud_t *pudp, unsigned long addr,
John Hubbard86dfbed2020-04-01 21:05:14 -07002388 unsigned long end, unsigned int flags,
2389 struct page **pages, int *nr)
Steve Capper2667f502014-10-09 15:29:14 -07002390{
Kirill A. Shutemovddc58f22016-01-15 16:52:56 -08002391 struct page *head, *page;
Steve Capper2667f502014-10-09 15:29:14 -07002392 int refs;
2393
Ira Weinyb798bec2019-05-13 17:17:07 -07002394 if (!pud_access_permitted(orig, flags & FOLL_WRITE))
Steve Capper2667f502014-10-09 15:29:14 -07002395 return 0;
2396
Ira Weiny7af75562019-05-13 17:17:14 -07002397 if (pud_devmap(orig)) {
2398 if (unlikely(flags & FOLL_LONGTERM))
2399 return 0;
John Hubbard86dfbed2020-04-01 21:05:14 -07002400 return __gup_device_huge_pud(orig, pudp, addr, end, flags,
2401 pages, nr);
Ira Weiny7af75562019-05-13 17:17:14 -07002402 }
Kirill A. Shutemovb59f65f2017-03-16 18:26:53 +03002403
Punit Agrawald63206e2017-07-06 15:39:39 -07002404 page = pud_page(orig) + ((addr & ~PUD_MASK) >> PAGE_SHIFT);
John Hubbarda43e9822020-01-30 22:12:17 -08002405 refs = record_subpages(page, addr, end, pages + *nr);
Steve Capper2667f502014-10-09 15:29:14 -07002406
John Hubbard3faa52c2020-04-01 21:05:29 -07002407 head = try_grab_compound_head(pud_page(orig), refs, flags);
John Hubbarda43e9822020-01-30 22:12:17 -08002408 if (!head)
Steve Capper2667f502014-10-09 15:29:14 -07002409 return 0;
Steve Capper2667f502014-10-09 15:29:14 -07002410
2411 if (unlikely(pud_val(orig) != pud_val(*pudp))) {
John Hubbard3b78d832020-04-01 21:05:22 -07002412 put_compound_head(head, refs, flags);
Steve Capper2667f502014-10-09 15:29:14 -07002413 return 0;
2414 }
2415
John Hubbarda43e9822020-01-30 22:12:17 -08002416 *nr += refs;
Kirill A. Shutemove9348052017-03-16 18:26:52 +03002417 SetPageReferenced(head);
Steve Capper2667f502014-10-09 15:29:14 -07002418 return 1;
2419}
2420
Aneesh Kumar K.Vf30c59e2014-11-05 21:57:40 +05302421static int gup_huge_pgd(pgd_t orig, pgd_t *pgdp, unsigned long addr,
Ira Weinyb798bec2019-05-13 17:17:07 -07002422 unsigned long end, unsigned int flags,
Aneesh Kumar K.Vf30c59e2014-11-05 21:57:40 +05302423 struct page **pages, int *nr)
2424{
2425 int refs;
Kirill A. Shutemovddc58f22016-01-15 16:52:56 -08002426 struct page *head, *page;
Aneesh Kumar K.Vf30c59e2014-11-05 21:57:40 +05302427
Ira Weinyb798bec2019-05-13 17:17:07 -07002428 if (!pgd_access_permitted(orig, flags & FOLL_WRITE))
Aneesh Kumar K.Vf30c59e2014-11-05 21:57:40 +05302429 return 0;
2430
Kirill A. Shutemovb59f65f2017-03-16 18:26:53 +03002431 BUILD_BUG_ON(pgd_devmap(orig));
John Hubbarda43e9822020-01-30 22:12:17 -08002432
Punit Agrawald63206e2017-07-06 15:39:39 -07002433 page = pgd_page(orig) + ((addr & ~PGDIR_MASK) >> PAGE_SHIFT);
John Hubbarda43e9822020-01-30 22:12:17 -08002434 refs = record_subpages(page, addr, end, pages + *nr);
Aneesh Kumar K.Vf30c59e2014-11-05 21:57:40 +05302435
John Hubbard3faa52c2020-04-01 21:05:29 -07002436 head = try_grab_compound_head(pgd_page(orig), refs, flags);
John Hubbarda43e9822020-01-30 22:12:17 -08002437 if (!head)
Aneesh Kumar K.Vf30c59e2014-11-05 21:57:40 +05302438 return 0;
Aneesh Kumar K.Vf30c59e2014-11-05 21:57:40 +05302439
2440 if (unlikely(pgd_val(orig) != pgd_val(*pgdp))) {
John Hubbard3b78d832020-04-01 21:05:22 -07002441 put_compound_head(head, refs, flags);
Aneesh Kumar K.Vf30c59e2014-11-05 21:57:40 +05302442 return 0;
2443 }
2444
John Hubbarda43e9822020-01-30 22:12:17 -08002445 *nr += refs;
Kirill A. Shutemove9348052017-03-16 18:26:52 +03002446 SetPageReferenced(head);
Aneesh Kumar K.Vf30c59e2014-11-05 21:57:40 +05302447 return 1;
2448}
2449
Steve Capper2667f502014-10-09 15:29:14 -07002450static int gup_pmd_range(pud_t pud, unsigned long addr, unsigned long end,
Ira Weinyb798bec2019-05-13 17:17:07 -07002451 unsigned int flags, struct page **pages, int *nr)
Steve Capper2667f502014-10-09 15:29:14 -07002452{
2453 unsigned long next;
2454 pmd_t *pmdp;
2455
2456 pmdp = pmd_offset(&pud, addr);
2457 do {
Christian Borntraeger38c5ce92015-01-06 22:54:46 +01002458 pmd_t pmd = READ_ONCE(*pmdp);
Steve Capper2667f502014-10-09 15:29:14 -07002459
2460 next = pmd_addr_end(addr, end);
Zi Yan84c3fc42017-09-08 16:11:01 -07002461 if (!pmd_present(pmd))
Steve Capper2667f502014-10-09 15:29:14 -07002462 return 0;
2463
Yu Zhao414fd082019-02-12 15:35:58 -08002464 if (unlikely(pmd_trans_huge(pmd) || pmd_huge(pmd) ||
2465 pmd_devmap(pmd))) {
Steve Capper2667f502014-10-09 15:29:14 -07002466 /*
2467 * NUMA hinting faults need to be handled in the GUP
2468 * slowpath for accounting purposes and so that they
2469 * can be serialised against THP migration.
2470 */
Mel Gorman8a0516e2015-02-12 14:58:22 -08002471 if (pmd_protnone(pmd))
Steve Capper2667f502014-10-09 15:29:14 -07002472 return 0;
2473
Ira Weinyb798bec2019-05-13 17:17:07 -07002474 if (!gup_huge_pmd(pmd, pmdp, addr, next, flags,
Steve Capper2667f502014-10-09 15:29:14 -07002475 pages, nr))
2476 return 0;
2477
Aneesh Kumar K.Vf30c59e2014-11-05 21:57:40 +05302478 } else if (unlikely(is_hugepd(__hugepd(pmd_val(pmd))))) {
2479 /*
2480 * architecture have different format for hugetlbfs
2481 * pmd format and THP pmd format
2482 */
2483 if (!gup_huge_pd(__hugepd(pmd_val(pmd)), addr,
Ira Weinyb798bec2019-05-13 17:17:07 -07002484 PMD_SHIFT, next, flags, pages, nr))
Aneesh Kumar K.Vf30c59e2014-11-05 21:57:40 +05302485 return 0;
Ira Weinyb798bec2019-05-13 17:17:07 -07002486 } else if (!gup_pte_range(pmd, addr, next, flags, pages, nr))
Mario Leinweber29231172018-04-05 16:24:18 -07002487 return 0;
Steve Capper2667f502014-10-09 15:29:14 -07002488 } while (pmdp++, addr = next, addr != end);
2489
2490 return 1;
2491}
2492
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +03002493static int gup_pud_range(p4d_t p4d, unsigned long addr, unsigned long end,
Ira Weinyb798bec2019-05-13 17:17:07 -07002494 unsigned int flags, struct page **pages, int *nr)
Steve Capper2667f502014-10-09 15:29:14 -07002495{
2496 unsigned long next;
2497 pud_t *pudp;
2498
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +03002499 pudp = pud_offset(&p4d, addr);
Steve Capper2667f502014-10-09 15:29:14 -07002500 do {
Christian Borntraegere37c6982014-12-07 21:41:33 +01002501 pud_t pud = READ_ONCE(*pudp);
Steve Capper2667f502014-10-09 15:29:14 -07002502
2503 next = pud_addr_end(addr, end);
Qiujun Huang154945202020-01-30 22:12:10 -08002504 if (unlikely(!pud_present(pud)))
Steve Capper2667f502014-10-09 15:29:14 -07002505 return 0;
Aneesh Kumar K.Vf30c59e2014-11-05 21:57:40 +05302506 if (unlikely(pud_huge(pud))) {
Ira Weinyb798bec2019-05-13 17:17:07 -07002507 if (!gup_huge_pud(pud, pudp, addr, next, flags,
Aneesh Kumar K.Vf30c59e2014-11-05 21:57:40 +05302508 pages, nr))
2509 return 0;
2510 } else if (unlikely(is_hugepd(__hugepd(pud_val(pud))))) {
2511 if (!gup_huge_pd(__hugepd(pud_val(pud)), addr,
Ira Weinyb798bec2019-05-13 17:17:07 -07002512 PUD_SHIFT, next, flags, pages, nr))
Steve Capper2667f502014-10-09 15:29:14 -07002513 return 0;
Ira Weinyb798bec2019-05-13 17:17:07 -07002514 } else if (!gup_pmd_range(pud, addr, next, flags, pages, nr))
Steve Capper2667f502014-10-09 15:29:14 -07002515 return 0;
2516 } while (pudp++, addr = next, addr != end);
2517
2518 return 1;
2519}
2520
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +03002521static int gup_p4d_range(pgd_t pgd, unsigned long addr, unsigned long end,
Ira Weinyb798bec2019-05-13 17:17:07 -07002522 unsigned int flags, struct page **pages, int *nr)
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +03002523{
2524 unsigned long next;
2525 p4d_t *p4dp;
2526
2527 p4dp = p4d_offset(&pgd, addr);
2528 do {
2529 p4d_t p4d = READ_ONCE(*p4dp);
2530
2531 next = p4d_addr_end(addr, end);
2532 if (p4d_none(p4d))
2533 return 0;
2534 BUILD_BUG_ON(p4d_huge(p4d));
2535 if (unlikely(is_hugepd(__hugepd(p4d_val(p4d))))) {
2536 if (!gup_huge_pd(__hugepd(p4d_val(p4d)), addr,
Ira Weinyb798bec2019-05-13 17:17:07 -07002537 P4D_SHIFT, next, flags, pages, nr))
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +03002538 return 0;
Ira Weinyb798bec2019-05-13 17:17:07 -07002539 } else if (!gup_pud_range(p4d, addr, next, flags, pages, nr))
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +03002540 return 0;
2541 } while (p4dp++, addr = next, addr != end);
2542
2543 return 1;
2544}
2545
Kirill A. Shutemov5b65c4672017-09-09 00:56:03 +03002546static void gup_pgd_range(unsigned long addr, unsigned long end,
Ira Weinyb798bec2019-05-13 17:17:07 -07002547 unsigned int flags, struct page **pages, int *nr)
Kirill A. Shutemov5b65c4672017-09-09 00:56:03 +03002548{
2549 unsigned long next;
2550 pgd_t *pgdp;
2551
2552 pgdp = pgd_offset(current->mm, addr);
2553 do {
2554 pgd_t pgd = READ_ONCE(*pgdp);
2555
2556 next = pgd_addr_end(addr, end);
2557 if (pgd_none(pgd))
2558 return;
2559 if (unlikely(pgd_huge(pgd))) {
Ira Weinyb798bec2019-05-13 17:17:07 -07002560 if (!gup_huge_pgd(pgd, pgdp, addr, next, flags,
Kirill A. Shutemov5b65c4672017-09-09 00:56:03 +03002561 pages, nr))
2562 return;
2563 } else if (unlikely(is_hugepd(__hugepd(pgd_val(pgd))))) {
2564 if (!gup_huge_pd(__hugepd(pgd_val(pgd)), addr,
Ira Weinyb798bec2019-05-13 17:17:07 -07002565 PGDIR_SHIFT, next, flags, pages, nr))
Kirill A. Shutemov5b65c4672017-09-09 00:56:03 +03002566 return;
Ira Weinyb798bec2019-05-13 17:17:07 -07002567 } else if (!gup_p4d_range(pgd, addr, next, flags, pages, nr))
Kirill A. Shutemov5b65c4672017-09-09 00:56:03 +03002568 return;
2569 } while (pgdp++, addr = next, addr != end);
2570}
Christoph Hellwig050a9ad2019-07-11 20:57:21 -07002571#else
2572static inline void gup_pgd_range(unsigned long addr, unsigned long end,
2573 unsigned int flags, struct page **pages, int *nr)
2574{
2575}
2576#endif /* CONFIG_HAVE_FAST_GUP */
Kirill A. Shutemov5b65c4672017-09-09 00:56:03 +03002577
2578#ifndef gup_fast_permitted
2579/*
2580 * Check if it's allowed to use __get_user_pages_fast() for the range, or
2581 * we need to fall back to the slow version:
2582 */
Christoph Hellwig26f4c322019-07-11 20:56:45 -07002583static bool gup_fast_permitted(unsigned long start, unsigned long end)
Kirill A. Shutemov5b65c4672017-09-09 00:56:03 +03002584{
Christoph Hellwig26f4c322019-07-11 20:56:45 -07002585 return true;
Kirill A. Shutemov5b65c4672017-09-09 00:56:03 +03002586}
2587#endif
2588
Steve Capper2667f502014-10-09 15:29:14 -07002589/*
2590 * Like get_user_pages_fast() except it's IRQ-safe in that it won't fall back to
Michael S. Tsirkind0811072018-04-13 15:35:23 -07002591 * the regular GUP.
2592 * Note a difference with get_user_pages_fast: this always returns the
2593 * number of pages pinned, 0 if no pages were pinned.
Christoph Hellwig050a9ad2019-07-11 20:57:21 -07002594 *
2595 * If the architecture does not support this function, simply return with no
2596 * pages pinned.
Steve Capper2667f502014-10-09 15:29:14 -07002597 */
2598int __get_user_pages_fast(unsigned long start, int nr_pages, int write,
2599 struct page **pages)
2600{
Wei Yangd4faa402018-10-26 15:07:55 -07002601 unsigned long len, end;
Kirill A. Shutemov5b65c4672017-09-09 00:56:03 +03002602 unsigned long flags;
Steve Capper2667f502014-10-09 15:29:14 -07002603 int nr = 0;
John Hubbard94202f12020-04-01 21:05:25 -07002604 /*
2605 * Internally (within mm/gup.c), gup fast variants must set FOLL_GET,
2606 * because gup fast is always a "pin with a +1 page refcount" request.
2607 */
2608 unsigned int gup_flags = FOLL_GET;
2609
2610 if (write)
2611 gup_flags |= FOLL_WRITE;
Steve Capper2667f502014-10-09 15:29:14 -07002612
Christoph Hellwigf455c8542019-07-11 20:56:41 -07002613 start = untagged_addr(start) & PAGE_MASK;
Steve Capper2667f502014-10-09 15:29:14 -07002614 len = (unsigned long) nr_pages << PAGE_SHIFT;
2615 end = start + len;
2616
Christoph Hellwig26f4c322019-07-11 20:56:45 -07002617 if (end <= start)
2618 return 0;
Linus Torvalds96d4f262019-01-03 18:57:57 -08002619 if (unlikely(!access_ok((void __user *)start, len)))
Steve Capper2667f502014-10-09 15:29:14 -07002620 return 0;
2621
2622 /*
2623 * Disable interrupts. We use the nested form as we can already have
2624 * interrupts disabled by get_futex_key.
2625 *
2626 * With interrupts disabled, we block page table pages from being
Fengguang Wu2ebe8222018-10-30 15:10:51 -07002627 * freed from under us. See struct mmu_table_batch comments in
2628 * include/asm-generic/tlb.h for more details.
Steve Capper2667f502014-10-09 15:29:14 -07002629 *
2630 * We do not adopt an rcu_read_lock(.) here as we also want to
2631 * block IPIs that come from THPs splitting.
2632 */
2633
Christoph Hellwig050a9ad2019-07-11 20:57:21 -07002634 if (IS_ENABLED(CONFIG_HAVE_FAST_GUP) &&
2635 gup_fast_permitted(start, end)) {
Kirill A. Shutemov5b65c4672017-09-09 00:56:03 +03002636 local_irq_save(flags);
John Hubbard94202f12020-04-01 21:05:25 -07002637 gup_pgd_range(start, end, gup_flags, pages, &nr);
Kirill A. Shutemov5b65c4672017-09-09 00:56:03 +03002638 local_irq_restore(flags);
2639 }
Steve Capper2667f502014-10-09 15:29:14 -07002640
2641 return nr;
2642}
Christoph Hellwig050a9ad2019-07-11 20:57:21 -07002643EXPORT_SYMBOL_GPL(__get_user_pages_fast);
Steve Capper2667f502014-10-09 15:29:14 -07002644
Ira Weiny7af75562019-05-13 17:17:14 -07002645static int __gup_longterm_unlocked(unsigned long start, int nr_pages,
2646 unsigned int gup_flags, struct page **pages)
2647{
2648 int ret;
2649
2650 /*
2651 * FIXME: FOLL_LONGTERM does not work with
2652 * get_user_pages_unlocked() (see comments in that function)
2653 */
2654 if (gup_flags & FOLL_LONGTERM) {
2655 down_read(&current->mm->mmap_sem);
2656 ret = __gup_longterm_locked(current, current->mm,
2657 start, nr_pages,
2658 pages, NULL, gup_flags);
2659 up_read(&current->mm->mmap_sem);
2660 } else {
2661 ret = get_user_pages_unlocked(start, nr_pages,
2662 pages, gup_flags);
2663 }
2664
2665 return ret;
2666}
2667
John Hubbardeddb1c22020-01-30 22:12:54 -08002668static int internal_get_user_pages_fast(unsigned long start, int nr_pages,
2669 unsigned int gup_flags,
2670 struct page **pages)
Steve Capper2667f502014-10-09 15:29:14 -07002671{
Kirill A. Shutemov5b65c4672017-09-09 00:56:03 +03002672 unsigned long addr, len, end;
Kirill A. Shutemov73e10a62017-03-16 18:26:54 +03002673 int nr = 0, ret = 0;
Steve Capper2667f502014-10-09 15:29:14 -07002674
John Hubbardf4000fd2020-01-30 22:12:43 -08002675 if (WARN_ON_ONCE(gup_flags & ~(FOLL_WRITE | FOLL_LONGTERM |
John Hubbard94202f12020-04-01 21:05:25 -07002676 FOLL_FORCE | FOLL_PIN | FOLL_GET)))
Christoph Hellwig817be122019-07-11 20:57:25 -07002677 return -EINVAL;
2678
Christoph Hellwigf455c8542019-07-11 20:56:41 -07002679 start = untagged_addr(start) & PAGE_MASK;
Kirill A. Shutemov5b65c4672017-09-09 00:56:03 +03002680 addr = start;
2681 len = (unsigned long) nr_pages << PAGE_SHIFT;
2682 end = start + len;
2683
Christoph Hellwig26f4c322019-07-11 20:56:45 -07002684 if (end <= start)
Michael S. Tsirkinc61611f2018-04-13 15:35:20 -07002685 return 0;
Linus Torvalds96d4f262019-01-03 18:57:57 -08002686 if (unlikely(!access_ok((void __user *)start, len)))
Michael S. Tsirkinc61611f2018-04-13 15:35:20 -07002687 return -EFAULT;
Kirill A. Shutemov73e10a62017-03-16 18:26:54 +03002688
Christoph Hellwig050a9ad2019-07-11 20:57:21 -07002689 if (IS_ENABLED(CONFIG_HAVE_FAST_GUP) &&
2690 gup_fast_permitted(start, end)) {
Kirill A. Shutemov5b65c4672017-09-09 00:56:03 +03002691 local_irq_disable();
Ira Weiny73b01402019-05-13 17:17:11 -07002692 gup_pgd_range(addr, end, gup_flags, pages, &nr);
Kirill A. Shutemov5b65c4672017-09-09 00:56:03 +03002693 local_irq_enable();
Kirill A. Shutemov73e10a62017-03-16 18:26:54 +03002694 ret = nr;
2695 }
Steve Capper2667f502014-10-09 15:29:14 -07002696
2697 if (nr < nr_pages) {
2698 /* Try to get the remaining pages with get_user_pages */
2699 start += nr << PAGE_SHIFT;
2700 pages += nr;
2701
Ira Weiny7af75562019-05-13 17:17:14 -07002702 ret = __gup_longterm_unlocked(start, nr_pages - nr,
2703 gup_flags, pages);
Steve Capper2667f502014-10-09 15:29:14 -07002704
2705 /* Have to be a bit careful with return values */
2706 if (nr > 0) {
2707 if (ret < 0)
2708 ret = nr;
2709 else
2710 ret += nr;
2711 }
2712 }
2713
2714 return ret;
2715}
John Hubbardeddb1c22020-01-30 22:12:54 -08002716
2717/**
2718 * get_user_pages_fast() - pin user pages in memory
John Hubbard3faa52c2020-04-01 21:05:29 -07002719 * @start: starting user address
2720 * @nr_pages: number of pages from start to pin
2721 * @gup_flags: flags modifying pin behaviour
2722 * @pages: array that receives pointers to the pages pinned.
2723 * Should be at least nr_pages long.
John Hubbardeddb1c22020-01-30 22:12:54 -08002724 *
2725 * Attempt to pin user pages in memory without taking mm->mmap_sem.
2726 * If not successful, it will fall back to taking the lock and
2727 * calling get_user_pages().
2728 *
2729 * Returns number of pages pinned. This may be fewer than the number requested.
2730 * If nr_pages is 0 or negative, returns 0. If no pages were pinned, returns
2731 * -errno.
2732 */
2733int get_user_pages_fast(unsigned long start, int nr_pages,
2734 unsigned int gup_flags, struct page **pages)
2735{
2736 /*
2737 * FOLL_PIN must only be set internally by the pin_user_pages*() APIs,
2738 * never directly by the caller, so enforce that:
2739 */
2740 if (WARN_ON_ONCE(gup_flags & FOLL_PIN))
2741 return -EINVAL;
2742
John Hubbard94202f12020-04-01 21:05:25 -07002743 /*
2744 * The caller may or may not have explicitly set FOLL_GET; either way is
2745 * OK. However, internally (within mm/gup.c), gup fast variants must set
2746 * FOLL_GET, because gup fast is always a "pin with a +1 page refcount"
2747 * request.
2748 */
2749 gup_flags |= FOLL_GET;
John Hubbardeddb1c22020-01-30 22:12:54 -08002750 return internal_get_user_pages_fast(start, nr_pages, gup_flags, pages);
2751}
Christoph Hellwig050a9ad2019-07-11 20:57:21 -07002752EXPORT_SYMBOL_GPL(get_user_pages_fast);
John Hubbardeddb1c22020-01-30 22:12:54 -08002753
2754/**
2755 * pin_user_pages_fast() - pin user pages in memory without taking locks
2756 *
John Hubbard3faa52c2020-04-01 21:05:29 -07002757 * @start: starting user address
2758 * @nr_pages: number of pages from start to pin
2759 * @gup_flags: flags modifying pin behaviour
2760 * @pages: array that receives pointers to the pages pinned.
2761 * Should be at least nr_pages long.
2762 *
2763 * Nearly the same as get_user_pages_fast(), except that FOLL_PIN is set. See
2764 * get_user_pages_fast() for documentation on the function arguments, because
2765 * the arguments here are identical.
2766 *
2767 * FOLL_PIN means that the pages must be released via unpin_user_page(). Please
2768 * see Documentation/vm/pin_user_pages.rst for further details.
John Hubbardeddb1c22020-01-30 22:12:54 -08002769 *
2770 * This is intended for Case 1 (DIO) in Documentation/vm/pin_user_pages.rst. It
2771 * is NOT intended for Case 2 (RDMA: long-term pins).
2772 */
2773int pin_user_pages_fast(unsigned long start, int nr_pages,
2774 unsigned int gup_flags, struct page **pages)
2775{
John Hubbard3faa52c2020-04-01 21:05:29 -07002776 /* FOLL_GET and FOLL_PIN are mutually exclusive. */
2777 if (WARN_ON_ONCE(gup_flags & FOLL_GET))
2778 return -EINVAL;
2779
2780 gup_flags |= FOLL_PIN;
2781 return internal_get_user_pages_fast(start, nr_pages, gup_flags, pages);
John Hubbardeddb1c22020-01-30 22:12:54 -08002782}
2783EXPORT_SYMBOL_GPL(pin_user_pages_fast);
2784
2785/**
2786 * pin_user_pages_remote() - pin pages of a remote process (task != current)
2787 *
John Hubbard3faa52c2020-04-01 21:05:29 -07002788 * @tsk: the task_struct to use for page fault accounting, or
2789 * NULL if faults are not to be recorded.
2790 * @mm: mm_struct of target mm
2791 * @start: starting user address
2792 * @nr_pages: number of pages from start to pin
2793 * @gup_flags: flags modifying lookup behaviour
2794 * @pages: array that receives pointers to the pages pinned.
2795 * Should be at least nr_pages long. Or NULL, if caller
2796 * only intends to ensure the pages are faulted in.
2797 * @vmas: array of pointers to vmas corresponding to each page.
2798 * Or NULL if the caller does not require them.
2799 * @locked: pointer to lock flag indicating whether lock is held and
2800 * subsequently whether VM_FAULT_RETRY functionality can be
2801 * utilised. Lock must initially be held.
2802 *
2803 * Nearly the same as get_user_pages_remote(), except that FOLL_PIN is set. See
2804 * get_user_pages_remote() for documentation on the function arguments, because
2805 * the arguments here are identical.
2806 *
2807 * FOLL_PIN means that the pages must be released via unpin_user_page(). Please
2808 * see Documentation/vm/pin_user_pages.rst for details.
John Hubbardeddb1c22020-01-30 22:12:54 -08002809 *
2810 * This is intended for Case 1 (DIO) in Documentation/vm/pin_user_pages.rst. It
2811 * is NOT intended for Case 2 (RDMA: long-term pins).
2812 */
2813long pin_user_pages_remote(struct task_struct *tsk, struct mm_struct *mm,
2814 unsigned long start, unsigned long nr_pages,
2815 unsigned int gup_flags, struct page **pages,
2816 struct vm_area_struct **vmas, int *locked)
2817{
John Hubbard3faa52c2020-04-01 21:05:29 -07002818 /* FOLL_GET and FOLL_PIN are mutually exclusive. */
2819 if (WARN_ON_ONCE(gup_flags & FOLL_GET))
2820 return -EINVAL;
2821
2822 gup_flags |= FOLL_PIN;
2823 return __get_user_pages_remote(tsk, mm, start, nr_pages, gup_flags,
2824 pages, vmas, locked);
John Hubbardeddb1c22020-01-30 22:12:54 -08002825}
2826EXPORT_SYMBOL(pin_user_pages_remote);
2827
2828/**
2829 * pin_user_pages() - pin user pages in memory for use by other devices
2830 *
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 lookup behaviour
2834 * @pages: array that receives pointers to the pages pinned.
2835 * Should be at least nr_pages long. Or NULL, if caller
2836 * only intends to ensure the pages are faulted in.
2837 * @vmas: array of pointers to vmas corresponding to each page.
2838 * Or NULL if the caller does not require them.
2839 *
2840 * Nearly the same as get_user_pages(), except that FOLL_TOUCH is not set, and
2841 * FOLL_PIN is set.
2842 *
2843 * FOLL_PIN means that the pages must be released via unpin_user_page(). Please
2844 * see Documentation/vm/pin_user_pages.rst for details.
John Hubbardeddb1c22020-01-30 22:12:54 -08002845 *
2846 * This is intended for Case 1 (DIO) in Documentation/vm/pin_user_pages.rst. It
2847 * is NOT intended for Case 2 (RDMA: long-term pins).
2848 */
2849long pin_user_pages(unsigned long start, unsigned long nr_pages,
2850 unsigned int gup_flags, struct page **pages,
2851 struct vm_area_struct **vmas)
2852{
John Hubbard3faa52c2020-04-01 21:05:29 -07002853 /* FOLL_GET and FOLL_PIN are mutually exclusive. */
2854 if (WARN_ON_ONCE(gup_flags & FOLL_GET))
2855 return -EINVAL;
2856
2857 gup_flags |= FOLL_PIN;
2858 return __gup_longterm_locked(current, current->mm, start, nr_pages,
2859 pages, vmas, gup_flags);
John Hubbardeddb1c22020-01-30 22:12:54 -08002860}
2861EXPORT_SYMBOL(pin_user_pages);