blob: ee4f14f108fe29335c5d58eaf574c1b459eaee74 [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 Hubbarda707cdd2020-01-30 22:12:21 -080032/*
33 * Return the compound head page with ref appropriately incremented,
34 * or NULL if that failed.
35 */
36static inline struct page *try_get_compound_head(struct page *page, int refs)
37{
38 struct page *head = compound_head(page);
39
40 if (WARN_ON_ONCE(page_ref_count(head) < 0))
41 return NULL;
42 if (unlikely(!page_cache_add_speculative(head, refs)))
43 return NULL;
44 return head;
45}
46
John Hubbard3faa52c2020-04-01 21:05:29 -070047/*
48 * try_grab_compound_head() - attempt to elevate a page's refcount, by a
49 * flags-dependent amount.
50 *
51 * "grab" names in this file mean, "look at flags to decide whether to use
52 * FOLL_PIN or FOLL_GET behavior, when incrementing the page's refcount.
53 *
54 * Either FOLL_PIN or FOLL_GET (or neither) must be set, but not both at the
55 * same time. (That's true throughout the get_user_pages*() and
56 * pin_user_pages*() APIs.) Cases:
57 *
58 * FOLL_GET: page's refcount will be incremented by 1.
59 * FOLL_PIN: page's refcount will be incremented by GUP_PIN_COUNTING_BIAS.
60 *
61 * Return: head page (with refcount appropriately incremented) for success, or
62 * NULL upon failure. If neither FOLL_GET nor FOLL_PIN was set, that's
63 * considered failure, and furthermore, a likely bug in the caller, so a warning
64 * is also emitted.
65 */
66static __maybe_unused struct page *try_grab_compound_head(struct page *page,
67 int refs,
68 unsigned int flags)
69{
70 if (flags & FOLL_GET)
71 return try_get_compound_head(page, refs);
72 else if (flags & FOLL_PIN) {
73 refs *= GUP_PIN_COUNTING_BIAS;
74 return try_get_compound_head(page, refs);
75 }
76
77 WARN_ON_ONCE(1);
78 return NULL;
79}
80
81/**
82 * try_grab_page() - elevate a page's refcount by a flag-dependent amount
83 *
84 * This might not do anything at all, depending on the flags argument.
85 *
86 * "grab" names in this file mean, "look at flags to decide whether to use
87 * FOLL_PIN or FOLL_GET behavior, when incrementing the page's refcount.
88 *
89 * @page: pointer to page to be grabbed
90 * @flags: gup flags: these are the FOLL_* flag values.
91 *
92 * Either FOLL_PIN or FOLL_GET (or neither) may be set, but not both at the same
93 * time. Cases:
94 *
95 * FOLL_GET: page's refcount will be incremented by 1.
96 * FOLL_PIN: page's refcount will be incremented by GUP_PIN_COUNTING_BIAS.
97 *
98 * Return: true for success, or if no action was required (if neither FOLL_PIN
99 * nor FOLL_GET was set, nothing is done). False for failure: FOLL_GET or
100 * FOLL_PIN was set, but the page could not be grabbed.
101 */
102bool __must_check try_grab_page(struct page *page, unsigned int flags)
103{
104 WARN_ON_ONCE((flags & (FOLL_GET | FOLL_PIN)) == (FOLL_GET | FOLL_PIN));
105
106 if (flags & FOLL_GET)
107 return try_get_page(page);
108 else if (flags & FOLL_PIN) {
109 page = compound_head(page);
110
111 if (WARN_ON_ONCE(page_ref_count(page) <= 0))
112 return false;
113
114 page_ref_add(page, GUP_PIN_COUNTING_BIAS);
115 }
116
117 return true;
118}
119
120#ifdef CONFIG_DEV_PAGEMAP_OPS
121static bool __unpin_devmap_managed_user_page(struct page *page)
122{
123 int count;
124
125 if (!page_is_devmap_managed(page))
126 return false;
127
128 count = page_ref_sub_return(page, GUP_PIN_COUNTING_BIAS);
129
130 /*
131 * devmap page refcounts are 1-based, rather than 0-based: if
132 * refcount is 1, then the page is free and the refcount is
133 * stable because nobody holds a reference on the page.
134 */
135 if (count == 1)
136 free_devmap_managed_page(page);
137 else if (!count)
138 __put_page(page);
139
140 return true;
141}
142#else
143static bool __unpin_devmap_managed_user_page(struct page *page)
144{
145 return false;
146}
147#endif /* CONFIG_DEV_PAGEMAP_OPS */
148
149/**
150 * unpin_user_page() - release a dma-pinned page
151 * @page: pointer to page to be released
152 *
153 * Pages that were pinned via pin_user_pages*() must be released via either
154 * unpin_user_page(), or one of the unpin_user_pages*() routines. This is so
155 * that such pages can be separately tracked and uniquely handled. In
156 * particular, interactions with RDMA and filesystems need special handling.
157 */
158void unpin_user_page(struct page *page)
159{
160 page = compound_head(page);
161
162 /*
163 * For devmap managed pages we need to catch refcount transition from
164 * GUP_PIN_COUNTING_BIAS to 1, when refcount reach one it means the
165 * page is free and we need to inform the device driver through
166 * callback. See include/linux/memremap.h and HMM for details.
167 */
168 if (__unpin_devmap_managed_user_page(page))
169 return;
170
171 if (page_ref_sub_and_test(page, GUP_PIN_COUNTING_BIAS))
172 __put_page(page);
173}
174EXPORT_SYMBOL(unpin_user_page);
175
John Hubbardfc1d8e72019-05-13 17:19:08 -0700176/**
John Hubbardf1f6a7d2020-01-30 22:13:35 -0800177 * unpin_user_pages_dirty_lock() - release and optionally dirty gup-pinned pages
akpm@linux-foundation.org2d15eb32019-09-23 15:35:04 -0700178 * @pages: array of pages to be maybe marked dirty, and definitely released.
John Hubbardfc1d8e72019-05-13 17:19:08 -0700179 * @npages: number of pages in the @pages array.
akpm@linux-foundation.org2d15eb32019-09-23 15:35:04 -0700180 * @make_dirty: whether to mark the pages dirty
John Hubbardfc1d8e72019-05-13 17:19:08 -0700181 *
182 * "gup-pinned page" refers to a page that has had one of the get_user_pages()
183 * variants called on that page.
184 *
185 * 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 -0700186 * compound page) dirty, if @make_dirty is true, and if the page was previously
John Hubbardf1f6a7d2020-01-30 22:13:35 -0800187 * listed as clean. In any case, releases all pages using unpin_user_page(),
188 * possibly via unpin_user_pages(), for the non-dirty case.
John Hubbardfc1d8e72019-05-13 17:19:08 -0700189 *
John Hubbardf1f6a7d2020-01-30 22:13:35 -0800190 * Please see the unpin_user_page() documentation for details.
John Hubbardfc1d8e72019-05-13 17:19:08 -0700191 *
akpm@linux-foundation.org2d15eb32019-09-23 15:35:04 -0700192 * set_page_dirty_lock() is used internally. If instead, set_page_dirty() is
193 * required, then the caller should a) verify that this is really correct,
194 * because _lock() is usually required, and b) hand code it:
John Hubbardf1f6a7d2020-01-30 22:13:35 -0800195 * set_page_dirty_lock(), unpin_user_page().
John Hubbardfc1d8e72019-05-13 17:19:08 -0700196 *
197 */
John Hubbardf1f6a7d2020-01-30 22:13:35 -0800198void unpin_user_pages_dirty_lock(struct page **pages, unsigned long npages,
199 bool make_dirty)
John Hubbardfc1d8e72019-05-13 17:19:08 -0700200{
akpm@linux-foundation.org2d15eb32019-09-23 15:35:04 -0700201 unsigned long index;
John Hubbardfc1d8e72019-05-13 17:19:08 -0700202
akpm@linux-foundation.org2d15eb32019-09-23 15:35:04 -0700203 /*
204 * TODO: this can be optimized for huge pages: if a series of pages is
205 * physically contiguous and part of the same compound page, then a
206 * single operation to the head page should suffice.
207 */
208
209 if (!make_dirty) {
John Hubbardf1f6a7d2020-01-30 22:13:35 -0800210 unpin_user_pages(pages, npages);
akpm@linux-foundation.org2d15eb32019-09-23 15:35:04 -0700211 return;
212 }
213
214 for (index = 0; index < npages; index++) {
215 struct page *page = compound_head(pages[index]);
216 /*
217 * Checking PageDirty at this point may race with
218 * clear_page_dirty_for_io(), but that's OK. Two key
219 * cases:
220 *
221 * 1) This code sees the page as already dirty, so it
222 * skips the call to set_page_dirty(). That could happen
223 * because clear_page_dirty_for_io() called
224 * page_mkclean(), followed by set_page_dirty().
225 * However, now the page is going to get written back,
226 * which meets the original intention of setting it
227 * dirty, so all is well: clear_page_dirty_for_io() goes
228 * on to call TestClearPageDirty(), and write the page
229 * back.
230 *
231 * 2) This code sees the page as clean, so it calls
232 * set_page_dirty(). The page stays dirty, despite being
233 * written back, so it gets written back again in the
234 * next writeback cycle. This is harmless.
235 */
236 if (!PageDirty(page))
237 set_page_dirty_lock(page);
John Hubbardf1f6a7d2020-01-30 22:13:35 -0800238 unpin_user_page(page);
akpm@linux-foundation.org2d15eb32019-09-23 15:35:04 -0700239 }
John Hubbardfc1d8e72019-05-13 17:19:08 -0700240}
John Hubbardf1f6a7d2020-01-30 22:13:35 -0800241EXPORT_SYMBOL(unpin_user_pages_dirty_lock);
John Hubbardfc1d8e72019-05-13 17:19:08 -0700242
243/**
John Hubbardf1f6a7d2020-01-30 22:13:35 -0800244 * unpin_user_pages() - release an array of gup-pinned pages.
John Hubbardfc1d8e72019-05-13 17:19:08 -0700245 * @pages: array of pages to be marked dirty and released.
246 * @npages: number of pages in the @pages array.
247 *
John Hubbardf1f6a7d2020-01-30 22:13:35 -0800248 * For each page in the @pages array, release the page using unpin_user_page().
John Hubbardfc1d8e72019-05-13 17:19:08 -0700249 *
John Hubbardf1f6a7d2020-01-30 22:13:35 -0800250 * Please see the unpin_user_page() documentation for details.
John Hubbardfc1d8e72019-05-13 17:19:08 -0700251 */
John Hubbardf1f6a7d2020-01-30 22:13:35 -0800252void unpin_user_pages(struct page **pages, unsigned long npages)
John Hubbardfc1d8e72019-05-13 17:19:08 -0700253{
254 unsigned long index;
255
256 /*
257 * TODO: this can be optimized for huge pages: if a series of pages is
258 * physically contiguous and part of the same compound page, then a
259 * single operation to the head page should suffice.
260 */
261 for (index = 0; index < npages; index++)
John Hubbardf1f6a7d2020-01-30 22:13:35 -0800262 unpin_user_page(pages[index]);
John Hubbardfc1d8e72019-05-13 17:19:08 -0700263}
John Hubbardf1f6a7d2020-01-30 22:13:35 -0800264EXPORT_SYMBOL(unpin_user_pages);
John Hubbardfc1d8e72019-05-13 17:19:08 -0700265
Christoph Hellwig050a9ad2019-07-11 20:57:21 -0700266#ifdef CONFIG_MMU
Kirill A. Shutemov69e68b42014-06-04 16:08:11 -0700267static struct page *no_page_table(struct vm_area_struct *vma,
268 unsigned int flags)
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700269{
Kirill A. Shutemov69e68b42014-06-04 16:08:11 -0700270 /*
271 * When core dumping an enormous anonymous area that nobody
272 * has touched so far, we don't want to allocate unnecessary pages or
273 * page tables. Return error instead of NULL to skip handle_mm_fault,
274 * then get_dump_page() will return NULL to leave a hole in the dump.
275 * But we can only make this optimization where a hole would surely
276 * be zero-filled if handle_mm_fault() actually did handle it.
277 */
278 if ((flags & FOLL_DUMP) && (!vma->vm_ops || !vma->vm_ops->fault))
279 return ERR_PTR(-EFAULT);
280 return NULL;
281}
282
Kirill A. Shutemov1027e442015-09-04 15:47:55 -0700283static int follow_pfn_pte(struct vm_area_struct *vma, unsigned long address,
284 pte_t *pte, unsigned int flags)
285{
286 /* No page to get reference */
287 if (flags & FOLL_GET)
288 return -EFAULT;
289
290 if (flags & FOLL_TOUCH) {
291 pte_t entry = *pte;
292
293 if (flags & FOLL_WRITE)
294 entry = pte_mkdirty(entry);
295 entry = pte_mkyoung(entry);
296
297 if (!pte_same(*pte, entry)) {
298 set_pte_at(vma->vm_mm, address, pte, entry);
299 update_mmu_cache(vma, address, pte);
300 }
301 }
302
303 /* Proper page table entry exists, but no corresponding struct page */
304 return -EEXIST;
305}
306
Linus Torvalds19be0ea2016-10-13 13:07:36 -0700307/*
308 * FOLL_FORCE can write to even unwritable pte's, but only
309 * after we've gone through a COW cycle and they are dirty.
310 */
311static inline bool can_follow_write_pte(pte_t pte, unsigned int flags)
312{
Linus Torvaldsf6f37322017-12-15 18:53:22 -0800313 return pte_write(pte) ||
Linus Torvalds19be0ea2016-10-13 13:07:36 -0700314 ((flags & FOLL_FORCE) && (flags & FOLL_COW) && pte_dirty(pte));
315}
316
Kirill A. Shutemov69e68b42014-06-04 16:08:11 -0700317static struct page *follow_page_pte(struct vm_area_struct *vma,
Keith Buschdf06b372018-10-26 15:10:28 -0700318 unsigned long address, pmd_t *pmd, unsigned int flags,
319 struct dev_pagemap **pgmap)
Kirill A. Shutemov69e68b42014-06-04 16:08:11 -0700320{
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700321 struct mm_struct *mm = vma->vm_mm;
Kirill A. Shutemov69e68b42014-06-04 16:08:11 -0700322 struct page *page;
323 spinlock_t *ptl;
324 pte_t *ptep, pte;
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700325
John Hubbardeddb1c22020-01-30 22:12:54 -0800326 /* FOLL_GET and FOLL_PIN are mutually exclusive. */
327 if (WARN_ON_ONCE((flags & (FOLL_PIN | FOLL_GET)) ==
328 (FOLL_PIN | FOLL_GET)))
329 return ERR_PTR(-EINVAL);
Kirill A. Shutemov69e68b42014-06-04 16:08:11 -0700330retry:
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700331 if (unlikely(pmd_bad(*pmd)))
Kirill A. Shutemov69e68b42014-06-04 16:08:11 -0700332 return no_page_table(vma, flags);
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700333
334 ptep = pte_offset_map_lock(mm, pmd, address, &ptl);
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700335 pte = *ptep;
336 if (!pte_present(pte)) {
337 swp_entry_t entry;
338 /*
339 * KSM's break_ksm() relies upon recognizing a ksm page
340 * even while it is being migrated, so for that case we
341 * need migration_entry_wait().
342 */
343 if (likely(!(flags & FOLL_MIGRATION)))
344 goto no_page;
Kirill A. Shutemov0661a332015-02-10 14:10:04 -0800345 if (pte_none(pte))
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700346 goto no_page;
347 entry = pte_to_swp_entry(pte);
348 if (!is_migration_entry(entry))
349 goto no_page;
350 pte_unmap_unlock(ptep, ptl);
351 migration_entry_wait(mm, pmd, address);
Kirill A. Shutemov69e68b42014-06-04 16:08:11 -0700352 goto retry;
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700353 }
Mel Gorman8a0516e2015-02-12 14:58:22 -0800354 if ((flags & FOLL_NUMA) && pte_protnone(pte))
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700355 goto no_page;
Linus Torvalds19be0ea2016-10-13 13:07:36 -0700356 if ((flags & FOLL_WRITE) && !can_follow_write_pte(pte, flags)) {
Kirill A. Shutemov69e68b42014-06-04 16:08:11 -0700357 pte_unmap_unlock(ptep, ptl);
358 return NULL;
359 }
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700360
361 page = vm_normal_page(vma, address, pte);
John Hubbard3faa52c2020-04-01 21:05:29 -0700362 if (!page && pte_devmap(pte) && (flags & (FOLL_GET | FOLL_PIN))) {
Dan Williams3565fce2016-01-15 16:56:55 -0800363 /*
John Hubbard3faa52c2020-04-01 21:05:29 -0700364 * Only return device mapping pages in the FOLL_GET or FOLL_PIN
365 * case since they are only valid while holding the pgmap
366 * reference.
Dan Williams3565fce2016-01-15 16:56:55 -0800367 */
Keith Buschdf06b372018-10-26 15:10:28 -0700368 *pgmap = get_dev_pagemap(pte_pfn(pte), *pgmap);
369 if (*pgmap)
Dan Williams3565fce2016-01-15 16:56:55 -0800370 page = pte_page(pte);
371 else
372 goto no_page;
373 } else if (unlikely(!page)) {
Kirill A. Shutemov1027e442015-09-04 15:47:55 -0700374 if (flags & FOLL_DUMP) {
375 /* Avoid special (like zero) pages in core dumps */
376 page = ERR_PTR(-EFAULT);
377 goto out;
378 }
379
380 if (is_zero_pfn(pte_pfn(pte))) {
381 page = pte_page(pte);
382 } else {
383 int ret;
384
385 ret = follow_pfn_pte(vma, address, ptep, flags);
386 page = ERR_PTR(ret);
387 goto out;
388 }
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700389 }
390
Kirill A. Shutemov6742d292016-01-15 16:52:28 -0800391 if (flags & FOLL_SPLIT && PageTransCompound(page)) {
392 int ret;
393 get_page(page);
394 pte_unmap_unlock(ptep, ptl);
395 lock_page(page);
396 ret = split_huge_page(page);
397 unlock_page(page);
398 put_page(page);
399 if (ret)
400 return ERR_PTR(ret);
401 goto retry;
402 }
403
John Hubbard3faa52c2020-04-01 21:05:29 -0700404 /* try_grab_page() does nothing unless FOLL_GET or FOLL_PIN is set. */
405 if (unlikely(!try_grab_page(page, flags))) {
406 page = ERR_PTR(-ENOMEM);
407 goto out;
Linus Torvalds8fde12c2019-04-11 10:49:19 -0700408 }
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700409 if (flags & FOLL_TOUCH) {
410 if ((flags & FOLL_WRITE) &&
411 !pte_dirty(pte) && !PageDirty(page))
412 set_page_dirty(page);
413 /*
414 * pte_mkyoung() would be more correct here, but atomic care
415 * is needed to avoid losing the dirty bit: it is easier to use
416 * mark_page_accessed().
417 */
418 mark_page_accessed(page);
419 }
Eric B Munsonde60f5f2015-11-05 18:51:36 -0800420 if ((flags & FOLL_MLOCK) && (vma->vm_flags & VM_LOCKED)) {
Kirill A. Shutemove90309c2016-01-15 16:54:33 -0800421 /* Do not mlock pte-mapped THP */
422 if (PageTransCompound(page))
423 goto out;
424
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700425 /*
426 * The preliminary mapping check is mainly to avoid the
427 * pointless overhead of lock_page on the ZERO_PAGE
428 * which might bounce very badly if there is contention.
429 *
430 * If the page is already locked, we don't need to
431 * handle it now - vmscan will handle it later if and
432 * when it attempts to reclaim the page.
433 */
434 if (page->mapping && trylock_page(page)) {
435 lru_add_drain(); /* push cached pages to LRU */
436 /*
437 * Because we lock page here, and migration is
438 * blocked by the pte's page reference, and we
439 * know the page is still mapped, we don't even
440 * need to check for file-cache page truncation.
441 */
442 mlock_vma_page(page);
443 unlock_page(page);
444 }
445 }
Kirill A. Shutemov1027e442015-09-04 15:47:55 -0700446out:
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700447 pte_unmap_unlock(ptep, ptl);
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700448 return page;
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700449no_page:
450 pte_unmap_unlock(ptep, ptl);
451 if (!pte_none(pte))
Kirill A. Shutemov69e68b42014-06-04 16:08:11 -0700452 return NULL;
453 return no_page_table(vma, flags);
454}
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700455
Aneesh Kumar K.V080dbb62017-07-06 15:38:44 -0700456static struct page *follow_pmd_mask(struct vm_area_struct *vma,
457 unsigned long address, pud_t *pudp,
Keith Buschdf06b372018-10-26 15:10:28 -0700458 unsigned int flags,
459 struct follow_page_context *ctx)
Kirill A. Shutemov69e68b42014-06-04 16:08:11 -0700460{
Huang Ying68827282018-06-07 17:06:34 -0700461 pmd_t *pmd, pmdval;
Kirill A. Shutemov69e68b42014-06-04 16:08:11 -0700462 spinlock_t *ptl;
463 struct page *page;
464 struct mm_struct *mm = vma->vm_mm;
465
Aneesh Kumar K.V080dbb62017-07-06 15:38:44 -0700466 pmd = pmd_offset(pudp, address);
Huang Ying68827282018-06-07 17:06:34 -0700467 /*
468 * The READ_ONCE() will stabilize the pmdval in a register or
469 * on the stack so that it will stop changing under the code.
470 */
471 pmdval = READ_ONCE(*pmd);
472 if (pmd_none(pmdval))
Kirill A. Shutemov69e68b42014-06-04 16:08:11 -0700473 return no_page_table(vma, flags);
Wei Yangbe9d3042020-01-30 22:12:14 -0800474 if (pmd_huge(pmdval) && is_vm_hugetlb_page(vma)) {
Naoya Horiguchie66f17f2015-02-11 15:25:22 -0800475 page = follow_huge_pmd(mm, address, pmd, flags);
476 if (page)
477 return page;
478 return no_page_table(vma, flags);
Kirill A. Shutemov69e68b42014-06-04 16:08:11 -0700479 }
Huang Ying68827282018-06-07 17:06:34 -0700480 if (is_hugepd(__hugepd(pmd_val(pmdval)))) {
Aneesh Kumar K.V4dc71452017-07-06 15:38:56 -0700481 page = follow_huge_pd(vma, address,
Huang Ying68827282018-06-07 17:06:34 -0700482 __hugepd(pmd_val(pmdval)), flags,
Aneesh Kumar K.V4dc71452017-07-06 15:38:56 -0700483 PMD_SHIFT);
484 if (page)
485 return page;
486 return no_page_table(vma, flags);
487 }
Zi Yan84c3fc42017-09-08 16:11:01 -0700488retry:
Huang Ying68827282018-06-07 17:06:34 -0700489 if (!pmd_present(pmdval)) {
Zi Yan84c3fc42017-09-08 16:11:01 -0700490 if (likely(!(flags & FOLL_MIGRATION)))
491 return no_page_table(vma, flags);
492 VM_BUG_ON(thp_migration_supported() &&
Huang Ying68827282018-06-07 17:06:34 -0700493 !is_pmd_migration_entry(pmdval));
494 if (is_pmd_migration_entry(pmdval))
Zi Yan84c3fc42017-09-08 16:11:01 -0700495 pmd_migration_entry_wait(mm, pmd);
Huang Ying68827282018-06-07 17:06:34 -0700496 pmdval = READ_ONCE(*pmd);
497 /*
498 * MADV_DONTNEED may convert the pmd to null because
499 * mmap_sem is held in read mode
500 */
501 if (pmd_none(pmdval))
502 return no_page_table(vma, flags);
Zi Yan84c3fc42017-09-08 16:11:01 -0700503 goto retry;
504 }
Huang Ying68827282018-06-07 17:06:34 -0700505 if (pmd_devmap(pmdval)) {
Dan Williams3565fce2016-01-15 16:56:55 -0800506 ptl = pmd_lock(mm, pmd);
Keith Buschdf06b372018-10-26 15:10:28 -0700507 page = follow_devmap_pmd(vma, address, pmd, flags, &ctx->pgmap);
Dan Williams3565fce2016-01-15 16:56:55 -0800508 spin_unlock(ptl);
509 if (page)
510 return page;
511 }
Huang Ying68827282018-06-07 17:06:34 -0700512 if (likely(!pmd_trans_huge(pmdval)))
Keith Buschdf06b372018-10-26 15:10:28 -0700513 return follow_page_pte(vma, address, pmd, flags, &ctx->pgmap);
Kirill A. Shutemov6742d292016-01-15 16:52:28 -0800514
Huang Ying68827282018-06-07 17:06:34 -0700515 if ((flags & FOLL_NUMA) && pmd_protnone(pmdval))
Aneesh Kumar K.Vdb08f202017-02-24 14:59:53 -0800516 return no_page_table(vma, flags);
517
Zi Yan84c3fc42017-09-08 16:11:01 -0700518retry_locked:
Kirill A. Shutemov6742d292016-01-15 16:52:28 -0800519 ptl = pmd_lock(mm, pmd);
Huang Ying68827282018-06-07 17:06:34 -0700520 if (unlikely(pmd_none(*pmd))) {
521 spin_unlock(ptl);
522 return no_page_table(vma, flags);
523 }
Zi Yan84c3fc42017-09-08 16:11:01 -0700524 if (unlikely(!pmd_present(*pmd))) {
525 spin_unlock(ptl);
526 if (likely(!(flags & FOLL_MIGRATION)))
527 return no_page_table(vma, flags);
528 pmd_migration_entry_wait(mm, pmd);
529 goto retry_locked;
530 }
Kirill A. Shutemov6742d292016-01-15 16:52:28 -0800531 if (unlikely(!pmd_trans_huge(*pmd))) {
532 spin_unlock(ptl);
Keith Buschdf06b372018-10-26 15:10:28 -0700533 return follow_page_pte(vma, address, pmd, flags, &ctx->pgmap);
Kirill A. Shutemov69e68b42014-06-04 16:08:11 -0700534 }
Song Liubfe7b002019-09-23 15:38:25 -0700535 if (flags & (FOLL_SPLIT | FOLL_SPLIT_PMD)) {
Kirill A. Shutemov6742d292016-01-15 16:52:28 -0800536 int ret;
537 page = pmd_page(*pmd);
538 if (is_huge_zero_page(page)) {
539 spin_unlock(ptl);
540 ret = 0;
Kirill A. Shutemov78ddc532016-01-15 16:52:42 -0800541 split_huge_pmd(vma, pmd, address);
Naoya Horiguchi337d9ab2016-07-26 15:24:03 -0700542 if (pmd_trans_unstable(pmd))
543 ret = -EBUSY;
Song Liubfe7b002019-09-23 15:38:25 -0700544 } else if (flags & FOLL_SPLIT) {
Linus Torvalds8fde12c2019-04-11 10:49:19 -0700545 if (unlikely(!try_get_page(page))) {
546 spin_unlock(ptl);
547 return ERR_PTR(-ENOMEM);
548 }
Kirill A. Shutemov6742d292016-01-15 16:52:28 -0800549 spin_unlock(ptl);
550 lock_page(page);
551 ret = split_huge_page(page);
552 unlock_page(page);
553 put_page(page);
Kirill A. Shutemovbaa355f2016-07-26 15:25:51 -0700554 if (pmd_none(*pmd))
555 return no_page_table(vma, flags);
Song Liubfe7b002019-09-23 15:38:25 -0700556 } else { /* flags & FOLL_SPLIT_PMD */
557 spin_unlock(ptl);
558 split_huge_pmd(vma, pmd, address);
559 ret = pte_alloc(mm, pmd) ? -ENOMEM : 0;
Kirill A. Shutemov6742d292016-01-15 16:52:28 -0800560 }
561
562 return ret ? ERR_PTR(ret) :
Keith Buschdf06b372018-10-26 15:10:28 -0700563 follow_page_pte(vma, address, pmd, flags, &ctx->pgmap);
Kirill A. Shutemov6742d292016-01-15 16:52:28 -0800564 }
Kirill A. Shutemov6742d292016-01-15 16:52:28 -0800565 page = follow_trans_huge_pmd(vma, address, pmd, flags);
566 spin_unlock(ptl);
Keith Buschdf06b372018-10-26 15:10:28 -0700567 ctx->page_mask = HPAGE_PMD_NR - 1;
Kirill A. Shutemov6742d292016-01-15 16:52:28 -0800568 return page;
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700569}
570
Aneesh Kumar K.V080dbb62017-07-06 15:38:44 -0700571static struct page *follow_pud_mask(struct vm_area_struct *vma,
572 unsigned long address, p4d_t *p4dp,
Keith Buschdf06b372018-10-26 15:10:28 -0700573 unsigned int flags,
574 struct follow_page_context *ctx)
Aneesh Kumar K.V080dbb62017-07-06 15:38:44 -0700575{
576 pud_t *pud;
577 spinlock_t *ptl;
578 struct page *page;
579 struct mm_struct *mm = vma->vm_mm;
580
581 pud = pud_offset(p4dp, address);
582 if (pud_none(*pud))
583 return no_page_table(vma, flags);
Wei Yangbe9d3042020-01-30 22:12:14 -0800584 if (pud_huge(*pud) && is_vm_hugetlb_page(vma)) {
Aneesh Kumar K.V080dbb62017-07-06 15:38:44 -0700585 page = follow_huge_pud(mm, address, pud, flags);
586 if (page)
587 return page;
588 return no_page_table(vma, flags);
589 }
Aneesh Kumar K.V4dc71452017-07-06 15:38:56 -0700590 if (is_hugepd(__hugepd(pud_val(*pud)))) {
591 page = follow_huge_pd(vma, address,
592 __hugepd(pud_val(*pud)), flags,
593 PUD_SHIFT);
594 if (page)
595 return page;
596 return no_page_table(vma, flags);
597 }
Aneesh Kumar K.V080dbb62017-07-06 15:38:44 -0700598 if (pud_devmap(*pud)) {
599 ptl = pud_lock(mm, pud);
Keith Buschdf06b372018-10-26 15:10:28 -0700600 page = follow_devmap_pud(vma, address, pud, flags, &ctx->pgmap);
Aneesh Kumar K.V080dbb62017-07-06 15:38:44 -0700601 spin_unlock(ptl);
602 if (page)
603 return page;
604 }
605 if (unlikely(pud_bad(*pud)))
606 return no_page_table(vma, flags);
607
Keith Buschdf06b372018-10-26 15:10:28 -0700608 return follow_pmd_mask(vma, address, pud, flags, ctx);
Aneesh Kumar K.V080dbb62017-07-06 15:38:44 -0700609}
610
Aneesh Kumar K.V080dbb62017-07-06 15:38:44 -0700611static struct page *follow_p4d_mask(struct vm_area_struct *vma,
612 unsigned long address, pgd_t *pgdp,
Keith Buschdf06b372018-10-26 15:10:28 -0700613 unsigned int flags,
614 struct follow_page_context *ctx)
Aneesh Kumar K.V080dbb62017-07-06 15:38:44 -0700615{
616 p4d_t *p4d;
Aneesh Kumar K.V4dc71452017-07-06 15:38:56 -0700617 struct page *page;
Aneesh Kumar K.V080dbb62017-07-06 15:38:44 -0700618
619 p4d = p4d_offset(pgdp, address);
620 if (p4d_none(*p4d))
621 return no_page_table(vma, flags);
622 BUILD_BUG_ON(p4d_huge(*p4d));
623 if (unlikely(p4d_bad(*p4d)))
624 return no_page_table(vma, flags);
625
Aneesh Kumar K.V4dc71452017-07-06 15:38:56 -0700626 if (is_hugepd(__hugepd(p4d_val(*p4d)))) {
627 page = follow_huge_pd(vma, address,
628 __hugepd(p4d_val(*p4d)), flags,
629 P4D_SHIFT);
630 if (page)
631 return page;
632 return no_page_table(vma, flags);
633 }
Keith Buschdf06b372018-10-26 15:10:28 -0700634 return follow_pud_mask(vma, address, p4d, flags, ctx);
Aneesh Kumar K.V080dbb62017-07-06 15:38:44 -0700635}
636
637/**
638 * follow_page_mask - look up a page descriptor from a user-virtual address
639 * @vma: vm_area_struct mapping @address
640 * @address: virtual address to look up
641 * @flags: flags modifying lookup behaviour
Mike Rapoport78179552018-11-16 15:08:29 -0800642 * @ctx: contains dev_pagemap for %ZONE_DEVICE memory pinning and a
643 * pointer to output page_mask
Aneesh Kumar K.V080dbb62017-07-06 15:38:44 -0700644 *
645 * @flags can have FOLL_ flags set, defined in <linux/mm.h>
646 *
Mike Rapoport78179552018-11-16 15:08:29 -0800647 * When getting pages from ZONE_DEVICE memory, the @ctx->pgmap caches
648 * the device's dev_pagemap metadata to avoid repeating expensive lookups.
649 *
650 * On output, the @ctx->page_mask is set according to the size of the page.
651 *
652 * Return: the mapped (struct page *), %NULL if no mapping exists, or
Aneesh Kumar K.V080dbb62017-07-06 15:38:44 -0700653 * an error pointer if there is a mapping to something not represented
654 * by a page descriptor (see also vm_normal_page()).
655 */
Bharath Vedarthama7030ae2019-07-11 20:54:34 -0700656static struct page *follow_page_mask(struct vm_area_struct *vma,
Aneesh Kumar K.V080dbb62017-07-06 15:38:44 -0700657 unsigned long address, unsigned int flags,
Keith Buschdf06b372018-10-26 15:10:28 -0700658 struct follow_page_context *ctx)
Aneesh Kumar K.V080dbb62017-07-06 15:38:44 -0700659{
660 pgd_t *pgd;
661 struct page *page;
662 struct mm_struct *mm = vma->vm_mm;
663
Keith Buschdf06b372018-10-26 15:10:28 -0700664 ctx->page_mask = 0;
Aneesh Kumar K.V080dbb62017-07-06 15:38:44 -0700665
666 /* make this handle hugepd */
667 page = follow_huge_addr(mm, address, flags & FOLL_WRITE);
668 if (!IS_ERR(page)) {
John Hubbard3faa52c2020-04-01 21:05:29 -0700669 WARN_ON_ONCE(flags & (FOLL_GET | FOLL_PIN));
Aneesh Kumar K.V080dbb62017-07-06 15:38:44 -0700670 return page;
671 }
672
673 pgd = pgd_offset(mm, address);
674
675 if (pgd_none(*pgd) || unlikely(pgd_bad(*pgd)))
676 return no_page_table(vma, flags);
677
Anshuman Khandualfaaa5b62017-07-06 15:38:50 -0700678 if (pgd_huge(*pgd)) {
679 page = follow_huge_pgd(mm, address, pgd, flags);
680 if (page)
681 return page;
682 return no_page_table(vma, flags);
683 }
Aneesh Kumar K.V4dc71452017-07-06 15:38:56 -0700684 if (is_hugepd(__hugepd(pgd_val(*pgd)))) {
685 page = follow_huge_pd(vma, address,
686 __hugepd(pgd_val(*pgd)), flags,
687 PGDIR_SHIFT);
688 if (page)
689 return page;
690 return no_page_table(vma, flags);
691 }
Anshuman Khandualfaaa5b62017-07-06 15:38:50 -0700692
Keith Buschdf06b372018-10-26 15:10:28 -0700693 return follow_p4d_mask(vma, address, pgd, flags, ctx);
694}
695
696struct page *follow_page(struct vm_area_struct *vma, unsigned long address,
697 unsigned int foll_flags)
698{
699 struct follow_page_context ctx = { NULL };
700 struct page *page;
701
702 page = follow_page_mask(vma, address, foll_flags, &ctx);
703 if (ctx.pgmap)
704 put_dev_pagemap(ctx.pgmap);
705 return page;
Aneesh Kumar K.V080dbb62017-07-06 15:38:44 -0700706}
707
Kirill A. Shutemovf2b495c2014-06-04 16:08:11 -0700708static int get_gate_page(struct mm_struct *mm, unsigned long address,
709 unsigned int gup_flags, struct vm_area_struct **vma,
710 struct page **page)
711{
712 pgd_t *pgd;
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +0300713 p4d_t *p4d;
Kirill A. Shutemovf2b495c2014-06-04 16:08:11 -0700714 pud_t *pud;
715 pmd_t *pmd;
716 pte_t *pte;
717 int ret = -EFAULT;
718
719 /* user gate pages are read-only */
720 if (gup_flags & FOLL_WRITE)
721 return -EFAULT;
722 if (address > TASK_SIZE)
723 pgd = pgd_offset_k(address);
724 else
725 pgd = pgd_offset_gate(mm, address);
Andy Lutomirskib5d1c392019-07-11 20:57:43 -0700726 if (pgd_none(*pgd))
727 return -EFAULT;
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +0300728 p4d = p4d_offset(pgd, address);
Andy Lutomirskib5d1c392019-07-11 20:57:43 -0700729 if (p4d_none(*p4d))
730 return -EFAULT;
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +0300731 pud = pud_offset(p4d, address);
Andy Lutomirskib5d1c392019-07-11 20:57:43 -0700732 if (pud_none(*pud))
733 return -EFAULT;
Kirill A. Shutemovf2b495c2014-06-04 16:08:11 -0700734 pmd = pmd_offset(pud, address);
Zi Yan84c3fc42017-09-08 16:11:01 -0700735 if (!pmd_present(*pmd))
Kirill A. Shutemovf2b495c2014-06-04 16:08:11 -0700736 return -EFAULT;
737 VM_BUG_ON(pmd_trans_huge(*pmd));
738 pte = pte_offset_map(pmd, address);
739 if (pte_none(*pte))
740 goto unmap;
741 *vma = get_gate_vma(mm);
742 if (!page)
743 goto out;
744 *page = vm_normal_page(*vma, address, *pte);
745 if (!*page) {
746 if ((gup_flags & FOLL_DUMP) || !is_zero_pfn(pte_pfn(*pte)))
747 goto unmap;
748 *page = pte_page(*pte);
749 }
Linus Torvalds8fde12c2019-04-11 10:49:19 -0700750 if (unlikely(!try_get_page(*page))) {
751 ret = -ENOMEM;
752 goto unmap;
753 }
Kirill A. Shutemovf2b495c2014-06-04 16:08:11 -0700754out:
755 ret = 0;
756unmap:
757 pte_unmap(pte);
758 return ret;
759}
760
Paul Cassella9a95f3c2014-08-06 16:07:24 -0700761/*
762 * mmap_sem must be held on entry. If @nonblocking != NULL and
763 * *@flags does not include FOLL_NOWAIT, the mmap_sem may be released.
764 * If it is, *@nonblocking will be set to 0 and -EBUSY returned.
765 */
Kirill A. Shutemov16744482014-06-04 16:08:12 -0700766static int faultin_page(struct task_struct *tsk, struct vm_area_struct *vma,
767 unsigned long address, unsigned int *flags, int *nonblocking)
768{
Kirill A. Shutemov16744482014-06-04 16:08:12 -0700769 unsigned int fault_flags = 0;
Souptick Joarder2b740302018-08-23 17:01:36 -0700770 vm_fault_t ret;
Kirill A. Shutemov16744482014-06-04 16:08:12 -0700771
Eric B Munsonde60f5f2015-11-05 18:51:36 -0800772 /* mlock all present pages, but do not fault in new pages */
773 if ((*flags & (FOLL_POPULATE | FOLL_MLOCK)) == FOLL_MLOCK)
774 return -ENOENT;
Kirill A. Shutemov16744482014-06-04 16:08:12 -0700775 if (*flags & FOLL_WRITE)
776 fault_flags |= FAULT_FLAG_WRITE;
Dave Hansen1b2ee122016-02-12 13:02:21 -0800777 if (*flags & FOLL_REMOTE)
778 fault_flags |= FAULT_FLAG_REMOTE;
Kirill A. Shutemov16744482014-06-04 16:08:12 -0700779 if (nonblocking)
780 fault_flags |= FAULT_FLAG_ALLOW_RETRY;
781 if (*flags & FOLL_NOWAIT)
782 fault_flags |= FAULT_FLAG_ALLOW_RETRY | FAULT_FLAG_RETRY_NOWAIT;
Andres Lagar-Cavilla234b2392014-09-17 10:51:48 -0700783 if (*flags & FOLL_TRIED) {
784 VM_WARN_ON_ONCE(fault_flags & FAULT_FLAG_ALLOW_RETRY);
785 fault_flags |= FAULT_FLAG_TRIED;
786 }
Kirill A. Shutemov16744482014-06-04 16:08:12 -0700787
Kirill A. Shutemovdcddffd2016-07-26 15:25:18 -0700788 ret = handle_mm_fault(vma, address, fault_flags);
Kirill A. Shutemov16744482014-06-04 16:08:12 -0700789 if (ret & VM_FAULT_ERROR) {
James Morse9a291a72017-06-02 14:46:46 -0700790 int err = vm_fault_to_errno(ret, *flags);
791
792 if (err)
793 return err;
Kirill A. Shutemov16744482014-06-04 16:08:12 -0700794 BUG();
795 }
796
797 if (tsk) {
798 if (ret & VM_FAULT_MAJOR)
799 tsk->maj_flt++;
800 else
801 tsk->min_flt++;
802 }
803
804 if (ret & VM_FAULT_RETRY) {
Andrea Arcangeli96312e62018-03-09 15:51:06 -0800805 if (nonblocking && !(fault_flags & FAULT_FLAG_RETRY_NOWAIT))
Kirill A. Shutemov16744482014-06-04 16:08:12 -0700806 *nonblocking = 0;
807 return -EBUSY;
808 }
809
810 /*
811 * The VM_FAULT_WRITE bit tells us that do_wp_page has broken COW when
812 * necessary, even if maybe_mkwrite decided not to set pte_write. We
813 * can thus safely do subsequent page lookups as if they were reads.
814 * But only do so when looping for pte_write is futile: in some cases
815 * userspace may also be wanting to write to the gotten user page,
816 * which a read fault here might prevent (a readonly page might get
817 * reCOWed by userspace write).
818 */
819 if ((ret & VM_FAULT_WRITE) && !(vma->vm_flags & VM_WRITE))
Mario Leinweber29231172018-04-05 16:24:18 -0700820 *flags |= FOLL_COW;
Kirill A. Shutemov16744482014-06-04 16:08:12 -0700821 return 0;
822}
823
Kirill A. Shutemovfa5bb202014-06-04 16:08:13 -0700824static int check_vma_flags(struct vm_area_struct *vma, unsigned long gup_flags)
825{
826 vm_flags_t vm_flags = vma->vm_flags;
Dave Hansen1b2ee122016-02-12 13:02:21 -0800827 int write = (gup_flags & FOLL_WRITE);
828 int foreign = (gup_flags & FOLL_REMOTE);
Kirill A. Shutemovfa5bb202014-06-04 16:08:13 -0700829
830 if (vm_flags & (VM_IO | VM_PFNMAP))
831 return -EFAULT;
832
Willy Tarreau7f7ccc22018-05-11 08:11:44 +0200833 if (gup_flags & FOLL_ANON && !vma_is_anonymous(vma))
834 return -EFAULT;
835
Dave Hansen1b2ee122016-02-12 13:02:21 -0800836 if (write) {
Kirill A. Shutemovfa5bb202014-06-04 16:08:13 -0700837 if (!(vm_flags & VM_WRITE)) {
838 if (!(gup_flags & FOLL_FORCE))
839 return -EFAULT;
840 /*
841 * We used to let the write,force case do COW in a
842 * VM_MAYWRITE VM_SHARED !VM_WRITE vma, so ptrace could
843 * set a breakpoint in a read-only mapping of an
844 * executable, without corrupting the file (yet only
845 * when that file had been opened for writing!).
846 * Anon pages in shared mappings are surprising: now
847 * just reject it.
848 */
Hugh Dickins46435362016-01-30 18:03:16 -0800849 if (!is_cow_mapping(vm_flags))
Kirill A. Shutemovfa5bb202014-06-04 16:08:13 -0700850 return -EFAULT;
Kirill A. Shutemovfa5bb202014-06-04 16:08:13 -0700851 }
852 } else if (!(vm_flags & VM_READ)) {
853 if (!(gup_flags & FOLL_FORCE))
854 return -EFAULT;
855 /*
856 * Is there actually any vma we can reach here which does not
857 * have VM_MAYREAD set?
858 */
859 if (!(vm_flags & VM_MAYREAD))
860 return -EFAULT;
861 }
Dave Hansend61172b2016-02-12 13:02:24 -0800862 /*
863 * gups are always data accesses, not instruction
864 * fetches, so execute=false here
865 */
866 if (!arch_vma_access_permitted(vma, write, false, foreign))
Dave Hansen33a709b2016-02-12 13:02:19 -0800867 return -EFAULT;
Kirill A. Shutemovfa5bb202014-06-04 16:08:13 -0700868 return 0;
869}
870
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700871/**
872 * __get_user_pages() - pin user pages in memory
873 * @tsk: task_struct of target task
874 * @mm: mm_struct of target mm
875 * @start: starting user address
876 * @nr_pages: number of pages from start to pin
877 * @gup_flags: flags modifying pin behaviour
878 * @pages: array that receives pointers to the pages pinned.
879 * Should be at least nr_pages long. Or NULL, if caller
880 * only intends to ensure the pages are faulted in.
881 * @vmas: array of pointers to vmas corresponding to each page.
882 * Or NULL if the caller does not require them.
883 * @nonblocking: whether waiting for disk IO or mmap_sem contention
884 *
Liu Xiangd2dfbe42019-11-30 17:49:53 -0800885 * Returns either number of pages pinned (which may be less than the
886 * number requested), or an error. Details about the return value:
887 *
888 * -- If nr_pages is 0, returns 0.
889 * -- If nr_pages is >0, but no pages were pinned, returns -errno.
890 * -- If nr_pages is >0, and some pages were pinned, returns the number of
891 * pages pinned. Again, this may be less than nr_pages.
892 *
893 * The caller is responsible for releasing returned @pages, via put_page().
894 *
895 * @vmas are valid only as long as mmap_sem is held.
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700896 *
Paul Cassella9a95f3c2014-08-06 16:07:24 -0700897 * Must be called with mmap_sem held. It may be released. See below.
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700898 *
899 * __get_user_pages walks a process's page tables and takes a reference to
900 * each struct page that each user address corresponds to at a given
901 * instant. That is, it takes the page that would be accessed if a user
902 * thread accesses the given user virtual address at that instant.
903 *
904 * This does not guarantee that the page exists in the user mappings when
905 * __get_user_pages returns, and there may even be a completely different
906 * page there in some cases (eg. if mmapped pagecache has been invalidated
907 * and subsequently re faulted). However it does guarantee that the page
908 * won't be freed completely. And mostly callers simply care that the page
909 * contains data that was valid *at some point in time*. Typically, an IO
910 * or similar operation cannot guarantee anything stronger anyway because
911 * locks can't be held over the syscall boundary.
912 *
913 * If @gup_flags & FOLL_WRITE == 0, the page must not be written to. If
914 * the page is written to, set_page_dirty (or set_page_dirty_lock, as
915 * appropriate) must be called after the page is finished with, and
916 * before put_page is called.
917 *
918 * If @nonblocking != NULL, __get_user_pages will not wait for disk IO
919 * or mmap_sem contention, and if waiting is needed to pin all pages,
Paul Cassella9a95f3c2014-08-06 16:07:24 -0700920 * *@nonblocking will be set to 0. Further, if @gup_flags does not
921 * include FOLL_NOWAIT, the mmap_sem will be released via up_read() in
922 * this case.
923 *
924 * A caller using such a combination of @nonblocking and @gup_flags
925 * must therefore hold the mmap_sem for reading only, and recognize
926 * when it's been released. Otherwise, it must be held for either
927 * reading or writing and will not be released.
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700928 *
929 * In most cases, get_user_pages or get_user_pages_fast should be used
930 * instead of __get_user_pages. __get_user_pages should be used only if
931 * you need some special @gup_flags.
932 */
Lorenzo Stoakes0d731752016-10-24 10:57:25 +0100933static long __get_user_pages(struct task_struct *tsk, struct mm_struct *mm,
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700934 unsigned long start, unsigned long nr_pages,
935 unsigned int gup_flags, struct page **pages,
936 struct vm_area_struct **vmas, int *nonblocking)
937{
Keith Buschdf06b372018-10-26 15:10:28 -0700938 long ret = 0, i = 0;
Kirill A. Shutemovfa5bb202014-06-04 16:08:13 -0700939 struct vm_area_struct *vma = NULL;
Keith Buschdf06b372018-10-26 15:10:28 -0700940 struct follow_page_context ctx = { NULL };
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700941
942 if (!nr_pages)
943 return 0;
944
Andrey Konovalovf9652592019-09-25 16:48:34 -0700945 start = untagged_addr(start);
946
John Hubbardeddb1c22020-01-30 22:12:54 -0800947 VM_BUG_ON(!!pages != !!(gup_flags & (FOLL_GET | FOLL_PIN)));
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700948
949 /*
950 * If FOLL_FORCE is set then do not force a full fault as the hinting
951 * fault information is unrelated to the reference behaviour of a task
952 * using the address space
953 */
954 if (!(gup_flags & FOLL_FORCE))
955 gup_flags |= FOLL_NUMA;
956
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700957 do {
Kirill A. Shutemovfa5bb202014-06-04 16:08:13 -0700958 struct page *page;
959 unsigned int foll_flags = gup_flags;
960 unsigned int page_increm;
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700961
Kirill A. Shutemovfa5bb202014-06-04 16:08:13 -0700962 /* first iteration or cross vma bound */
963 if (!vma || start >= vma->vm_end) {
964 vma = find_extend_vma(mm, start);
965 if (!vma && in_gate_area(mm, start)) {
Kirill A. Shutemovfa5bb202014-06-04 16:08:13 -0700966 ret = get_gate_page(mm, start & PAGE_MASK,
967 gup_flags, &vma,
968 pages ? &pages[i] : NULL);
969 if (ret)
John Hubbard08be37b2018-11-30 14:08:53 -0800970 goto out;
Keith Buschdf06b372018-10-26 15:10:28 -0700971 ctx.page_mask = 0;
Kirill A. Shutemovfa5bb202014-06-04 16:08:13 -0700972 goto next_page;
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700973 }
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700974
Keith Buschdf06b372018-10-26 15:10:28 -0700975 if (!vma || check_vma_flags(vma, gup_flags)) {
976 ret = -EFAULT;
977 goto out;
978 }
Kirill A. Shutemovfa5bb202014-06-04 16:08:13 -0700979 if (is_vm_hugetlb_page(vma)) {
980 i = follow_hugetlb_page(mm, vma, pages, vmas,
981 &start, &nr_pages, i,
Andrea Arcangeli87ffc112017-02-22 15:43:13 -0800982 gup_flags, nonblocking);
Kirill A. Shutemovfa5bb202014-06-04 16:08:13 -0700983 continue;
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700984 }
Kirill A. Shutemovfa5bb202014-06-04 16:08:13 -0700985 }
986retry:
987 /*
988 * If we have a pending SIGKILL, don't keep faulting pages and
989 * potentially allocating memory.
990 */
Davidlohr Buesofa45f112019-01-03 15:28:55 -0800991 if (fatal_signal_pending(current)) {
Keith Buschdf06b372018-10-26 15:10:28 -0700992 ret = -ERESTARTSYS;
993 goto out;
994 }
Kirill A. Shutemovfa5bb202014-06-04 16:08:13 -0700995 cond_resched();
Keith Buschdf06b372018-10-26 15:10:28 -0700996
997 page = follow_page_mask(vma, start, foll_flags, &ctx);
Kirill A. Shutemovfa5bb202014-06-04 16:08:13 -0700998 if (!page) {
Kirill A. Shutemovfa5bb202014-06-04 16:08:13 -0700999 ret = faultin_page(tsk, vma, start, &foll_flags,
1000 nonblocking);
1001 switch (ret) {
1002 case 0:
1003 goto retry;
Keith Buschdf06b372018-10-26 15:10:28 -07001004 case -EBUSY:
1005 ret = 0;
1006 /* FALLTHRU */
Kirill A. Shutemovfa5bb202014-06-04 16:08:13 -07001007 case -EFAULT:
1008 case -ENOMEM:
1009 case -EHWPOISON:
Keith Buschdf06b372018-10-26 15:10:28 -07001010 goto out;
Kirill A. Shutemovfa5bb202014-06-04 16:08:13 -07001011 case -ENOENT:
1012 goto next_page;
1013 }
1014 BUG();
Kirill A. Shutemov1027e442015-09-04 15:47:55 -07001015 } else if (PTR_ERR(page) == -EEXIST) {
1016 /*
1017 * Proper page table entry exists, but no corresponding
1018 * struct page.
1019 */
1020 goto next_page;
1021 } else if (IS_ERR(page)) {
Keith Buschdf06b372018-10-26 15:10:28 -07001022 ret = PTR_ERR(page);
1023 goto out;
Kirill A. Shutemov1027e442015-09-04 15:47:55 -07001024 }
Kirill A. Shutemovfa5bb202014-06-04 16:08:13 -07001025 if (pages) {
1026 pages[i] = page;
1027 flush_anon_page(vma, page, start);
1028 flush_dcache_page(page);
Keith Buschdf06b372018-10-26 15:10:28 -07001029 ctx.page_mask = 0;
Kirill A. Shutemovfa5bb202014-06-04 16:08:13 -07001030 }
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -07001031next_page:
Kirill A. Shutemovfa5bb202014-06-04 16:08:13 -07001032 if (vmas) {
1033 vmas[i] = vma;
Keith Buschdf06b372018-10-26 15:10:28 -07001034 ctx.page_mask = 0;
Kirill A. Shutemovfa5bb202014-06-04 16:08:13 -07001035 }
Keith Buschdf06b372018-10-26 15:10:28 -07001036 page_increm = 1 + (~(start >> PAGE_SHIFT) & ctx.page_mask);
Kirill A. Shutemovfa5bb202014-06-04 16:08:13 -07001037 if (page_increm > nr_pages)
1038 page_increm = nr_pages;
1039 i += page_increm;
1040 start += page_increm * PAGE_SIZE;
1041 nr_pages -= page_increm;
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -07001042 } while (nr_pages);
Keith Buschdf06b372018-10-26 15:10:28 -07001043out:
1044 if (ctx.pgmap)
1045 put_dev_pagemap(ctx.pgmap);
1046 return i ? i : ret;
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -07001047}
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -07001048
Tobias Klauser771ab432016-12-12 16:41:53 -08001049static bool vma_permits_fault(struct vm_area_struct *vma,
1050 unsigned int fault_flags)
Dave Hansend4925e02016-02-12 13:02:16 -08001051{
Dave Hansen1b2ee122016-02-12 13:02:21 -08001052 bool write = !!(fault_flags & FAULT_FLAG_WRITE);
1053 bool foreign = !!(fault_flags & FAULT_FLAG_REMOTE);
Dave Hansen33a709b2016-02-12 13:02:19 -08001054 vm_flags_t vm_flags = write ? VM_WRITE : VM_READ;
Dave Hansend4925e02016-02-12 13:02:16 -08001055
1056 if (!(vm_flags & vma->vm_flags))
1057 return false;
1058
Dave Hansen33a709b2016-02-12 13:02:19 -08001059 /*
1060 * The architecture might have a hardware protection
Dave Hansen1b2ee122016-02-12 13:02:21 -08001061 * mechanism other than read/write that can deny access.
Dave Hansend61172b2016-02-12 13:02:24 -08001062 *
1063 * gup always represents data access, not instruction
1064 * fetches, so execute=false here:
Dave Hansen33a709b2016-02-12 13:02:19 -08001065 */
Dave Hansend61172b2016-02-12 13:02:24 -08001066 if (!arch_vma_access_permitted(vma, write, false, foreign))
Dave Hansen33a709b2016-02-12 13:02:19 -08001067 return false;
1068
Dave Hansend4925e02016-02-12 13:02:16 -08001069 return true;
1070}
1071
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -07001072/*
1073 * fixup_user_fault() - manually resolve a user page fault
1074 * @tsk: the task_struct to use for page fault accounting, or
1075 * NULL if faults are not to be recorded.
1076 * @mm: mm_struct of target mm
1077 * @address: user address
1078 * @fault_flags:flags to pass down to handle_mm_fault()
Dominik Dingel4a9e1cd2016-01-15 16:57:04 -08001079 * @unlocked: did we unlock the mmap_sem while retrying, maybe NULL if caller
1080 * does not allow retry
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -07001081 *
1082 * This is meant to be called in the specific scenario where for locking reasons
1083 * we try to access user memory in atomic context (within a pagefault_disable()
1084 * section), this returns -EFAULT, and we want to resolve the user fault before
1085 * trying again.
1086 *
1087 * Typically this is meant to be used by the futex code.
1088 *
1089 * The main difference with get_user_pages() is that this function will
1090 * unconditionally call handle_mm_fault() which will in turn perform all the
1091 * necessary SW fixup of the dirty and young bits in the PTE, while
Dominik Dingel4a9e1cd2016-01-15 16:57:04 -08001092 * get_user_pages() only guarantees to update these in the struct page.
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -07001093 *
1094 * This is important for some architectures where those bits also gate the
1095 * access permission to the page because they are maintained in software. On
1096 * such architectures, gup() will not be enough to make a subsequent access
1097 * succeed.
1098 *
Dominik Dingel4a9e1cd2016-01-15 16:57:04 -08001099 * This function will not return with an unlocked mmap_sem. So it has not the
1100 * same semantics wrt the @mm->mmap_sem as does filemap_fault().
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -07001101 */
1102int fixup_user_fault(struct task_struct *tsk, struct mm_struct *mm,
Dominik Dingel4a9e1cd2016-01-15 16:57:04 -08001103 unsigned long address, unsigned int fault_flags,
1104 bool *unlocked)
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -07001105{
1106 struct vm_area_struct *vma;
Souptick Joarder2b740302018-08-23 17:01:36 -07001107 vm_fault_t ret, major = 0;
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -07001108
Andrey Konovalovf9652592019-09-25 16:48:34 -07001109 address = untagged_addr(address);
1110
Dominik Dingel4a9e1cd2016-01-15 16:57:04 -08001111 if (unlocked)
1112 fault_flags |= FAULT_FLAG_ALLOW_RETRY;
1113
1114retry:
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -07001115 vma = find_extend_vma(mm, address);
1116 if (!vma || address < vma->vm_start)
1117 return -EFAULT;
1118
Dave Hansend4925e02016-02-12 13:02:16 -08001119 if (!vma_permits_fault(vma, fault_flags))
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -07001120 return -EFAULT;
1121
Kirill A. Shutemovdcddffd2016-07-26 15:25:18 -07001122 ret = handle_mm_fault(vma, address, fault_flags);
Dominik Dingel4a9e1cd2016-01-15 16:57:04 -08001123 major |= ret & VM_FAULT_MAJOR;
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -07001124 if (ret & VM_FAULT_ERROR) {
James Morse9a291a72017-06-02 14:46:46 -07001125 int err = vm_fault_to_errno(ret, 0);
1126
1127 if (err)
1128 return err;
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -07001129 BUG();
1130 }
Dominik Dingel4a9e1cd2016-01-15 16:57:04 -08001131
1132 if (ret & VM_FAULT_RETRY) {
1133 down_read(&mm->mmap_sem);
1134 if (!(fault_flags & FAULT_FLAG_TRIED)) {
1135 *unlocked = true;
1136 fault_flags &= ~FAULT_FLAG_ALLOW_RETRY;
1137 fault_flags |= FAULT_FLAG_TRIED;
1138 goto retry;
1139 }
1140 }
1141
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -07001142 if (tsk) {
Dominik Dingel4a9e1cd2016-01-15 16:57:04 -08001143 if (major)
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -07001144 tsk->maj_flt++;
1145 else
1146 tsk->min_flt++;
1147 }
1148 return 0;
1149}
Paolo Bonziniadd6a0c2016-06-07 17:51:18 +02001150EXPORT_SYMBOL_GPL(fixup_user_fault);
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -07001151
Andrea Arcangelif0818f42015-02-11 15:27:17 -08001152static __always_inline long __get_user_pages_locked(struct task_struct *tsk,
1153 struct mm_struct *mm,
1154 unsigned long start,
1155 unsigned long nr_pages,
Andrea Arcangelif0818f42015-02-11 15:27:17 -08001156 struct page **pages,
1157 struct vm_area_struct **vmas,
Al Viroe7167122017-11-19 11:32:05 -05001158 int *locked,
Andrea Arcangeli0fd71a52015-02-11 15:27:20 -08001159 unsigned int flags)
Andrea Arcangelif0818f42015-02-11 15:27:17 -08001160{
Andrea Arcangelif0818f42015-02-11 15:27:17 -08001161 long ret, pages_done;
1162 bool lock_dropped;
1163
1164 if (locked) {
1165 /* if VM_FAULT_RETRY can be returned, vmas become invalid */
1166 BUG_ON(vmas);
1167 /* check caller initialized locked */
1168 BUG_ON(*locked != 1);
1169 }
1170
John Hubbardeddb1c22020-01-30 22:12:54 -08001171 /*
1172 * FOLL_PIN and FOLL_GET are mutually exclusive. Traditional behavior
1173 * is to set FOLL_GET if the caller wants pages[] filled in (but has
1174 * carelessly failed to specify FOLL_GET), so keep doing that, but only
1175 * for FOLL_GET, not for the newer FOLL_PIN.
1176 *
1177 * FOLL_PIN always expects pages to be non-null, but no need to assert
1178 * that here, as any failures will be obvious enough.
1179 */
1180 if (pages && !(flags & FOLL_PIN))
Andrea Arcangelif0818f42015-02-11 15:27:17 -08001181 flags |= FOLL_GET;
Andrea Arcangelif0818f42015-02-11 15:27:17 -08001182
1183 pages_done = 0;
1184 lock_dropped = false;
1185 for (;;) {
1186 ret = __get_user_pages(tsk, mm, start, nr_pages, flags, pages,
1187 vmas, locked);
1188 if (!locked)
1189 /* VM_FAULT_RETRY couldn't trigger, bypass */
1190 return ret;
1191
1192 /* VM_FAULT_RETRY cannot return errors */
1193 if (!*locked) {
1194 BUG_ON(ret < 0);
1195 BUG_ON(ret >= nr_pages);
1196 }
1197
Andrea Arcangelif0818f42015-02-11 15:27:17 -08001198 if (ret > 0) {
1199 nr_pages -= ret;
1200 pages_done += ret;
1201 if (!nr_pages)
1202 break;
1203 }
1204 if (*locked) {
Andrea Arcangeli96312e62018-03-09 15:51:06 -08001205 /*
1206 * VM_FAULT_RETRY didn't trigger or it was a
1207 * FOLL_NOWAIT.
1208 */
Andrea Arcangelif0818f42015-02-11 15:27:17 -08001209 if (!pages_done)
1210 pages_done = ret;
1211 break;
1212 }
Mike Rapoportdf172772019-05-31 22:30:33 -07001213 /*
1214 * VM_FAULT_RETRY triggered, so seek to the faulting offset.
1215 * For the prefault case (!pages) we only update counts.
1216 */
1217 if (likely(pages))
1218 pages += ret;
Andrea Arcangelif0818f42015-02-11 15:27:17 -08001219 start += ret << PAGE_SHIFT;
1220
1221 /*
1222 * Repeat on the address that fired VM_FAULT_RETRY
1223 * without FAULT_FLAG_ALLOW_RETRY but with
1224 * FAULT_FLAG_TRIED.
1225 */
1226 *locked = 1;
1227 lock_dropped = true;
1228 down_read(&mm->mmap_sem);
1229 ret = __get_user_pages(tsk, mm, start, 1, flags | FOLL_TRIED,
1230 pages, NULL, NULL);
1231 if (ret != 1) {
1232 BUG_ON(ret > 1);
1233 if (!pages_done)
1234 pages_done = ret;
1235 break;
1236 }
1237 nr_pages--;
1238 pages_done++;
1239 if (!nr_pages)
1240 break;
Mike Rapoportdf172772019-05-31 22:30:33 -07001241 if (likely(pages))
1242 pages++;
Andrea Arcangelif0818f42015-02-11 15:27:17 -08001243 start += PAGE_SIZE;
1244 }
Al Viroe7167122017-11-19 11:32:05 -05001245 if (lock_dropped && *locked) {
Andrea Arcangelif0818f42015-02-11 15:27:17 -08001246 /*
1247 * We must let the caller know we temporarily dropped the lock
1248 * and so the critical section protected by it was lost.
1249 */
1250 up_read(&mm->mmap_sem);
1251 *locked = 0;
1252 }
1253 return pages_done;
1254}
1255
Christoph Hellwigd3649f62019-07-11 20:57:18 -07001256/**
1257 * populate_vma_page_range() - populate a range of pages in the vma.
1258 * @vma: target vma
1259 * @start: start address
1260 * @end: end address
1261 * @nonblocking:
1262 *
1263 * This takes care of mlocking the pages too if VM_LOCKED is set.
1264 *
1265 * return 0 on success, negative error code on error.
1266 *
1267 * vma->vm_mm->mmap_sem must be held.
1268 *
1269 * If @nonblocking is NULL, it may be held for read or write and will
1270 * be unperturbed.
1271 *
1272 * If @nonblocking is non-NULL, it must held for read only and may be
1273 * released. If it's released, *@nonblocking will be set to 0.
1274 */
1275long populate_vma_page_range(struct vm_area_struct *vma,
1276 unsigned long start, unsigned long end, int *nonblocking)
1277{
1278 struct mm_struct *mm = vma->vm_mm;
1279 unsigned long nr_pages = (end - start) / PAGE_SIZE;
1280 int gup_flags;
1281
1282 VM_BUG_ON(start & ~PAGE_MASK);
1283 VM_BUG_ON(end & ~PAGE_MASK);
1284 VM_BUG_ON_VMA(start < vma->vm_start, vma);
1285 VM_BUG_ON_VMA(end > vma->vm_end, vma);
1286 VM_BUG_ON_MM(!rwsem_is_locked(&mm->mmap_sem), mm);
1287
1288 gup_flags = FOLL_TOUCH | FOLL_POPULATE | FOLL_MLOCK;
1289 if (vma->vm_flags & VM_LOCKONFAULT)
1290 gup_flags &= ~FOLL_POPULATE;
1291 /*
1292 * We want to touch writable mappings with a write fault in order
1293 * to break COW, except for shared mappings because these don't COW
1294 * and we would not want to dirty them for nothing.
1295 */
1296 if ((vma->vm_flags & (VM_WRITE | VM_SHARED)) == VM_WRITE)
1297 gup_flags |= FOLL_WRITE;
1298
1299 /*
1300 * We want mlock to succeed for regions that have any permissions
1301 * other than PROT_NONE.
1302 */
1303 if (vma->vm_flags & (VM_READ | VM_WRITE | VM_EXEC))
1304 gup_flags |= FOLL_FORCE;
1305
1306 /*
1307 * We made sure addr is within a VMA, so the following will
1308 * not result in a stack expansion that recurses back here.
1309 */
1310 return __get_user_pages(current, mm, start, nr_pages, gup_flags,
1311 NULL, NULL, nonblocking);
1312}
1313
1314/*
1315 * __mm_populate - populate and/or mlock pages within a range of address space.
1316 *
1317 * This is used to implement mlock() and the MAP_POPULATE / MAP_LOCKED mmap
1318 * flags. VMAs must be already marked with the desired vm_flags, and
1319 * mmap_sem must not be held.
1320 */
1321int __mm_populate(unsigned long start, unsigned long len, int ignore_errors)
1322{
1323 struct mm_struct *mm = current->mm;
1324 unsigned long end, nstart, nend;
1325 struct vm_area_struct *vma = NULL;
1326 int locked = 0;
1327 long ret = 0;
1328
1329 end = start + len;
1330
1331 for (nstart = start; nstart < end; nstart = nend) {
1332 /*
1333 * We want to fault in pages for [nstart; end) address range.
1334 * Find first corresponding VMA.
1335 */
1336 if (!locked) {
1337 locked = 1;
1338 down_read(&mm->mmap_sem);
1339 vma = find_vma(mm, nstart);
1340 } else if (nstart >= vma->vm_end)
1341 vma = vma->vm_next;
1342 if (!vma || vma->vm_start >= end)
1343 break;
1344 /*
1345 * Set [nstart; nend) to intersection of desired address
1346 * range with the first VMA. Also, skip undesirable VMA types.
1347 */
1348 nend = min(end, vma->vm_end);
1349 if (vma->vm_flags & (VM_IO | VM_PFNMAP))
1350 continue;
1351 if (nstart < vma->vm_start)
1352 nstart = vma->vm_start;
1353 /*
1354 * Now fault in a range of pages. populate_vma_page_range()
1355 * double checks the vma flags, so that it won't mlock pages
1356 * if the vma was already munlocked.
1357 */
1358 ret = populate_vma_page_range(vma, nstart, nend, &locked);
1359 if (ret < 0) {
1360 if (ignore_errors) {
1361 ret = 0;
1362 continue; /* continue at next VMA */
1363 }
1364 break;
1365 }
1366 nend = nstart + ret * PAGE_SIZE;
1367 ret = 0;
1368 }
1369 if (locked)
1370 up_read(&mm->mmap_sem);
1371 return ret; /* 0 or negative error code */
1372}
1373
1374/**
1375 * get_dump_page() - pin user page in memory while writing it to core dump
1376 * @addr: user address
1377 *
1378 * Returns struct page pointer of user page pinned for dump,
1379 * to be freed afterwards by put_page().
1380 *
1381 * Returns NULL on any kind of failure - a hole must then be inserted into
1382 * the corefile, to preserve alignment with its headers; and also returns
1383 * NULL wherever the ZERO_PAGE, or an anonymous pte_none, has been found -
1384 * allowing a hole to be left in the corefile to save diskspace.
1385 *
1386 * Called without mmap_sem, but after all other threads have been killed.
1387 */
1388#ifdef CONFIG_ELF_CORE
1389struct page *get_dump_page(unsigned long addr)
1390{
1391 struct vm_area_struct *vma;
1392 struct page *page;
1393
1394 if (__get_user_pages(current, current->mm, addr, 1,
1395 FOLL_FORCE | FOLL_DUMP | FOLL_GET, &page, &vma,
1396 NULL) < 1)
1397 return NULL;
1398 flush_cache_page(vma, addr, page_to_pfn(page));
1399 return page;
1400}
1401#endif /* CONFIG_ELF_CORE */
Christoph Hellwig050a9ad2019-07-11 20:57:21 -07001402#else /* CONFIG_MMU */
1403static long __get_user_pages_locked(struct task_struct *tsk,
1404 struct mm_struct *mm, unsigned long start,
1405 unsigned long nr_pages, struct page **pages,
1406 struct vm_area_struct **vmas, int *locked,
1407 unsigned int foll_flags)
1408{
1409 struct vm_area_struct *vma;
1410 unsigned long vm_flags;
1411 int i;
1412
1413 /* calculate required read or write permissions.
1414 * If FOLL_FORCE is set, we only require the "MAY" flags.
1415 */
1416 vm_flags = (foll_flags & FOLL_WRITE) ?
1417 (VM_WRITE | VM_MAYWRITE) : (VM_READ | VM_MAYREAD);
1418 vm_flags &= (foll_flags & FOLL_FORCE) ?
1419 (VM_MAYREAD | VM_MAYWRITE) : (VM_READ | VM_WRITE);
1420
1421 for (i = 0; i < nr_pages; i++) {
1422 vma = find_vma(mm, start);
1423 if (!vma)
1424 goto finish_or_fault;
1425
1426 /* protect what we can, including chardevs */
1427 if ((vma->vm_flags & (VM_IO | VM_PFNMAP)) ||
1428 !(vm_flags & vma->vm_flags))
1429 goto finish_or_fault;
1430
1431 if (pages) {
1432 pages[i] = virt_to_page(start);
1433 if (pages[i])
1434 get_page(pages[i]);
1435 }
1436 if (vmas)
1437 vmas[i] = vma;
1438 start = (start + PAGE_SIZE) & PAGE_MASK;
1439 }
1440
1441 return i;
1442
1443finish_or_fault:
1444 return i ? : -EFAULT;
1445}
1446#endif /* !CONFIG_MMU */
Christoph Hellwigd3649f62019-07-11 20:57:18 -07001447
Aneesh Kumar K.V9a4e9f32019-03-05 15:47:44 -08001448#if defined(CONFIG_FS_DAX) || defined (CONFIG_CMA)
Aneesh Kumar K.V9a4e9f32019-03-05 15:47:44 -08001449static bool check_dax_vmas(struct vm_area_struct **vmas, long nr_pages)
1450{
1451 long i;
1452 struct vm_area_struct *vma_prev = NULL;
1453
1454 for (i = 0; i < nr_pages; i++) {
1455 struct vm_area_struct *vma = vmas[i];
1456
1457 if (vma == vma_prev)
1458 continue;
1459
1460 vma_prev = vma;
1461
1462 if (vma_is_fsdax(vma))
1463 return true;
1464 }
1465 return false;
1466}
Aneesh Kumar K.V9a4e9f32019-03-05 15:47:44 -08001467
1468#ifdef CONFIG_CMA
1469static struct page *new_non_cma_page(struct page *page, unsigned long private)
1470{
1471 /*
1472 * We want to make sure we allocate the new page from the same node
1473 * as the source page.
1474 */
1475 int nid = page_to_nid(page);
1476 /*
1477 * Trying to allocate a page for migration. Ignore allocation
1478 * failure warnings. We don't force __GFP_THISNODE here because
1479 * this node here is the node where we have CMA reservation and
1480 * in some case these nodes will have really less non movable
1481 * allocation memory.
1482 */
1483 gfp_t gfp_mask = GFP_USER | __GFP_NOWARN;
1484
1485 if (PageHighMem(page))
1486 gfp_mask |= __GFP_HIGHMEM;
1487
1488#ifdef CONFIG_HUGETLB_PAGE
1489 if (PageHuge(page)) {
1490 struct hstate *h = page_hstate(page);
1491 /*
1492 * We don't want to dequeue from the pool because pool pages will
1493 * mostly be from the CMA region.
1494 */
1495 return alloc_migrate_huge_page(h, gfp_mask, nid, NULL);
1496 }
1497#endif
1498 if (PageTransHuge(page)) {
1499 struct page *thp;
1500 /*
1501 * ignore allocation failure warnings
1502 */
1503 gfp_t thp_gfpmask = GFP_TRANSHUGE | __GFP_NOWARN;
1504
1505 /*
1506 * Remove the movable mask so that we don't allocate from
1507 * CMA area again.
1508 */
1509 thp_gfpmask &= ~__GFP_MOVABLE;
1510 thp = __alloc_pages_node(nid, thp_gfpmask, HPAGE_PMD_ORDER);
1511 if (!thp)
1512 return NULL;
1513 prep_transhuge_page(thp);
1514 return thp;
1515 }
1516
1517 return __alloc_pages_node(nid, gfp_mask, 0);
1518}
1519
Ira Weiny932f4a62019-05-13 17:17:03 -07001520static long check_and_migrate_cma_pages(struct task_struct *tsk,
1521 struct mm_struct *mm,
1522 unsigned long start,
1523 unsigned long nr_pages,
Aneesh Kumar K.V9a4e9f32019-03-05 15:47:44 -08001524 struct page **pages,
Ira Weiny932f4a62019-05-13 17:17:03 -07001525 struct vm_area_struct **vmas,
1526 unsigned int gup_flags)
Aneesh Kumar K.V9a4e9f32019-03-05 15:47:44 -08001527{
Pingfan Liuaa712392019-07-11 20:57:39 -07001528 unsigned long i;
1529 unsigned long step;
Aneesh Kumar K.V9a4e9f32019-03-05 15:47:44 -08001530 bool drain_allow = true;
1531 bool migrate_allow = true;
1532 LIST_HEAD(cma_page_list);
zhong jiangb96cc652019-11-30 17:49:50 -08001533 long ret = nr_pages;
Aneesh Kumar K.V9a4e9f32019-03-05 15:47:44 -08001534
1535check_again:
Pingfan Liuaa712392019-07-11 20:57:39 -07001536 for (i = 0; i < nr_pages;) {
1537
1538 struct page *head = compound_head(pages[i]);
1539
1540 /*
1541 * gup may start from a tail page. Advance step by the left
1542 * part.
1543 */
Matthew Wilcox (Oracle)d8c65462019-09-23 15:34:30 -07001544 step = compound_nr(head) - (pages[i] - head);
Aneesh Kumar K.V9a4e9f32019-03-05 15:47:44 -08001545 /*
1546 * If we get a page from the CMA zone, since we are going to
1547 * be pinning these entries, we might as well move them out
1548 * of the CMA zone if possible.
1549 */
Pingfan Liuaa712392019-07-11 20:57:39 -07001550 if (is_migrate_cma_page(head)) {
1551 if (PageHuge(head))
Aneesh Kumar K.V9a4e9f32019-03-05 15:47:44 -08001552 isolate_huge_page(head, &cma_page_list);
Pingfan Liuaa712392019-07-11 20:57:39 -07001553 else {
Aneesh Kumar K.V9a4e9f32019-03-05 15:47:44 -08001554 if (!PageLRU(head) && drain_allow) {
1555 lru_add_drain_all();
1556 drain_allow = false;
1557 }
1558
1559 if (!isolate_lru_page(head)) {
1560 list_add_tail(&head->lru, &cma_page_list);
1561 mod_node_page_state(page_pgdat(head),
1562 NR_ISOLATED_ANON +
1563 page_is_file_cache(head),
1564 hpage_nr_pages(head));
1565 }
1566 }
1567 }
Pingfan Liuaa712392019-07-11 20:57:39 -07001568
1569 i += step;
Aneesh Kumar K.V9a4e9f32019-03-05 15:47:44 -08001570 }
1571
1572 if (!list_empty(&cma_page_list)) {
1573 /*
1574 * drop the above get_user_pages reference.
1575 */
1576 for (i = 0; i < nr_pages; i++)
1577 put_page(pages[i]);
1578
1579 if (migrate_pages(&cma_page_list, new_non_cma_page,
1580 NULL, 0, MIGRATE_SYNC, MR_CONTIG_RANGE)) {
1581 /*
1582 * some of the pages failed migration. Do get_user_pages
1583 * without migration.
1584 */
1585 migrate_allow = false;
1586
1587 if (!list_empty(&cma_page_list))
1588 putback_movable_pages(&cma_page_list);
1589 }
1590 /*
Ira Weiny932f4a62019-05-13 17:17:03 -07001591 * We did migrate all the pages, Try to get the page references
1592 * again migrating any new CMA pages which we failed to isolate
1593 * earlier.
Aneesh Kumar K.V9a4e9f32019-03-05 15:47:44 -08001594 */
zhong jiangb96cc652019-11-30 17:49:50 -08001595 ret = __get_user_pages_locked(tsk, mm, start, nr_pages,
Ira Weiny932f4a62019-05-13 17:17:03 -07001596 pages, vmas, NULL,
1597 gup_flags);
1598
zhong jiangb96cc652019-11-30 17:49:50 -08001599 if ((ret > 0) && migrate_allow) {
1600 nr_pages = ret;
Aneesh Kumar K.V9a4e9f32019-03-05 15:47:44 -08001601 drain_allow = true;
1602 goto check_again;
1603 }
1604 }
1605
zhong jiangb96cc652019-11-30 17:49:50 -08001606 return ret;
Aneesh Kumar K.V9a4e9f32019-03-05 15:47:44 -08001607}
1608#else
Ira Weiny932f4a62019-05-13 17:17:03 -07001609static long check_and_migrate_cma_pages(struct task_struct *tsk,
1610 struct mm_struct *mm,
1611 unsigned long start,
1612 unsigned long nr_pages,
1613 struct page **pages,
1614 struct vm_area_struct **vmas,
1615 unsigned int gup_flags)
Aneesh Kumar K.V9a4e9f32019-03-05 15:47:44 -08001616{
1617 return nr_pages;
1618}
Christoph Hellwig050a9ad2019-07-11 20:57:21 -07001619#endif /* CONFIG_CMA */
Aneesh Kumar K.V9a4e9f32019-03-05 15:47:44 -08001620
Dan Williams2bb6d282017-11-29 16:10:35 -08001621/*
Ira Weiny932f4a62019-05-13 17:17:03 -07001622 * __gup_longterm_locked() is a wrapper for __get_user_pages_locked which
1623 * allows us to process the FOLL_LONGTERM flag.
Dan Williams2bb6d282017-11-29 16:10:35 -08001624 */
Ira Weiny932f4a62019-05-13 17:17:03 -07001625static long __gup_longterm_locked(struct task_struct *tsk,
1626 struct mm_struct *mm,
1627 unsigned long start,
1628 unsigned long nr_pages,
1629 struct page **pages,
1630 struct vm_area_struct **vmas,
1631 unsigned int gup_flags)
Dan Williams2bb6d282017-11-29 16:10:35 -08001632{
Ira Weiny932f4a62019-05-13 17:17:03 -07001633 struct vm_area_struct **vmas_tmp = vmas;
1634 unsigned long flags = 0;
Dan Williams2bb6d282017-11-29 16:10:35 -08001635 long rc, i;
1636
Ira Weiny932f4a62019-05-13 17:17:03 -07001637 if (gup_flags & FOLL_LONGTERM) {
1638 if (!pages)
1639 return -EINVAL;
Dan Williams2bb6d282017-11-29 16:10:35 -08001640
Ira Weiny932f4a62019-05-13 17:17:03 -07001641 if (!vmas_tmp) {
1642 vmas_tmp = kcalloc(nr_pages,
1643 sizeof(struct vm_area_struct *),
1644 GFP_KERNEL);
1645 if (!vmas_tmp)
1646 return -ENOMEM;
1647 }
1648 flags = memalloc_nocma_save();
Dan Williams2bb6d282017-11-29 16:10:35 -08001649 }
1650
Ira Weiny932f4a62019-05-13 17:17:03 -07001651 rc = __get_user_pages_locked(tsk, mm, start, nr_pages, pages,
1652 vmas_tmp, NULL, gup_flags);
Dan Williams2bb6d282017-11-29 16:10:35 -08001653
Ira Weiny932f4a62019-05-13 17:17:03 -07001654 if (gup_flags & FOLL_LONGTERM) {
1655 memalloc_nocma_restore(flags);
1656 if (rc < 0)
1657 goto out;
1658
1659 if (check_dax_vmas(vmas_tmp, rc)) {
1660 for (i = 0; i < rc; i++)
1661 put_page(pages[i]);
1662 rc = -EOPNOTSUPP;
1663 goto out;
1664 }
1665
1666 rc = check_and_migrate_cma_pages(tsk, mm, start, rc, pages,
1667 vmas_tmp, gup_flags);
Aneesh Kumar K.V9a4e9f32019-03-05 15:47:44 -08001668 }
1669
Dan Williams2bb6d282017-11-29 16:10:35 -08001670out:
Ira Weiny932f4a62019-05-13 17:17:03 -07001671 if (vmas_tmp != vmas)
1672 kfree(vmas_tmp);
Dan Williams2bb6d282017-11-29 16:10:35 -08001673 return rc;
1674}
Ira Weiny932f4a62019-05-13 17:17:03 -07001675#else /* !CONFIG_FS_DAX && !CONFIG_CMA */
1676static __always_inline long __gup_longterm_locked(struct task_struct *tsk,
1677 struct mm_struct *mm,
1678 unsigned long start,
1679 unsigned long nr_pages,
1680 struct page **pages,
1681 struct vm_area_struct **vmas,
1682 unsigned int flags)
1683{
1684 return __get_user_pages_locked(tsk, mm, start, nr_pages, pages, vmas,
1685 NULL, flags);
1686}
1687#endif /* CONFIG_FS_DAX || CONFIG_CMA */
1688
John Hubbard22bf29b2020-04-01 21:05:10 -07001689#ifdef CONFIG_MMU
1690static long __get_user_pages_remote(struct task_struct *tsk,
1691 struct mm_struct *mm,
1692 unsigned long start, unsigned long nr_pages,
1693 unsigned int gup_flags, struct page **pages,
1694 struct vm_area_struct **vmas, int *locked)
1695{
1696 /*
1697 * Parts of FOLL_LONGTERM behavior are incompatible with
1698 * FAULT_FLAG_ALLOW_RETRY because of the FS DAX check requirement on
1699 * vmas. However, this only comes up if locked is set, and there are
1700 * callers that do request FOLL_LONGTERM, but do not set locked. So,
1701 * allow what we can.
1702 */
1703 if (gup_flags & FOLL_LONGTERM) {
1704 if (WARN_ON_ONCE(locked))
1705 return -EINVAL;
1706 /*
1707 * This will check the vmas (even if our vmas arg is NULL)
1708 * and return -ENOTSUPP if DAX isn't allowed in this case:
1709 */
1710 return __gup_longterm_locked(tsk, mm, start, nr_pages, pages,
1711 vmas, gup_flags | FOLL_TOUCH |
1712 FOLL_REMOTE);
1713 }
1714
1715 return __get_user_pages_locked(tsk, mm, start, nr_pages, pages, vmas,
1716 locked,
1717 gup_flags | FOLL_TOUCH | FOLL_REMOTE);
1718}
1719
Ira Weiny932f4a62019-05-13 17:17:03 -07001720/*
John Hubbardc4237f82020-01-30 22:12:36 -08001721 * get_user_pages_remote() - pin user pages in memory
1722 * @tsk: the task_struct to use for page fault accounting, or
1723 * NULL if faults are not to be recorded.
1724 * @mm: mm_struct of target mm
1725 * @start: starting user address
1726 * @nr_pages: number of pages from start to pin
1727 * @gup_flags: flags modifying lookup behaviour
1728 * @pages: array that receives pointers to the pages pinned.
1729 * Should be at least nr_pages long. Or NULL, if caller
1730 * only intends to ensure the pages are faulted in.
1731 * @vmas: array of pointers to vmas corresponding to each page.
1732 * Or NULL if the caller does not require them.
1733 * @locked: pointer to lock flag indicating whether lock is held and
1734 * subsequently whether VM_FAULT_RETRY functionality can be
1735 * utilised. Lock must initially be held.
1736 *
1737 * Returns either number of pages pinned (which may be less than the
1738 * number requested), or an error. Details about the return value:
1739 *
1740 * -- If nr_pages is 0, returns 0.
1741 * -- If nr_pages is >0, but no pages were pinned, returns -errno.
1742 * -- If nr_pages is >0, and some pages were pinned, returns the number of
1743 * pages pinned. Again, this may be less than nr_pages.
1744 *
1745 * The caller is responsible for releasing returned @pages, via put_page().
1746 *
1747 * @vmas are valid only as long as mmap_sem is held.
1748 *
1749 * Must be called with mmap_sem held for read or write.
1750 *
1751 * get_user_pages walks a process's page tables and takes a reference to
1752 * each struct page that each user address corresponds to at a given
1753 * instant. That is, it takes the page that would be accessed if a user
1754 * thread accesses the given user virtual address at that instant.
1755 *
1756 * This does not guarantee that the page exists in the user mappings when
1757 * get_user_pages returns, and there may even be a completely different
1758 * page there in some cases (eg. if mmapped pagecache has been invalidated
1759 * and subsequently re faulted). However it does guarantee that the page
1760 * won't be freed completely. And mostly callers simply care that the page
1761 * contains data that was valid *at some point in time*. Typically, an IO
1762 * or similar operation cannot guarantee anything stronger anyway because
1763 * locks can't be held over the syscall boundary.
1764 *
1765 * If gup_flags & FOLL_WRITE == 0, the page must not be written to. If the page
1766 * is written to, set_page_dirty (or set_page_dirty_lock, as appropriate) must
1767 * be called after the page is finished with, and before put_page is called.
1768 *
1769 * get_user_pages is typically used for fewer-copy IO operations, to get a
1770 * handle on the memory by some means other than accesses via the user virtual
1771 * addresses. The pages may be submitted for DMA to devices or accessed via
1772 * their kernel linear mapping (via the kmap APIs). Care should be taken to
1773 * use the correct cache flushing APIs.
1774 *
1775 * See also get_user_pages_fast, for performance critical applications.
1776 *
1777 * get_user_pages should be phased out in favor of
1778 * get_user_pages_locked|unlocked or get_user_pages_fast. Nothing
1779 * should use get_user_pages because it cannot pass
1780 * FAULT_FLAG_ALLOW_RETRY to handle_mm_fault.
1781 */
1782long get_user_pages_remote(struct task_struct *tsk, struct mm_struct *mm,
1783 unsigned long start, unsigned long nr_pages,
1784 unsigned int gup_flags, struct page **pages,
1785 struct vm_area_struct **vmas, int *locked)
1786{
1787 /*
John Hubbardeddb1c22020-01-30 22:12:54 -08001788 * FOLL_PIN must only be set internally by the pin_user_pages*() APIs,
1789 * never directly by the caller, so enforce that with an assertion:
1790 */
1791 if (WARN_ON_ONCE(gup_flags & FOLL_PIN))
1792 return -EINVAL;
1793
John Hubbard22bf29b2020-04-01 21:05:10 -07001794 return __get_user_pages_remote(tsk, mm, start, nr_pages, gup_flags,
1795 pages, vmas, locked);
John Hubbardc4237f82020-01-30 22:12:36 -08001796}
1797EXPORT_SYMBOL(get_user_pages_remote);
1798
John Hubbardeddb1c22020-01-30 22:12:54 -08001799#else /* CONFIG_MMU */
1800long get_user_pages_remote(struct task_struct *tsk, struct mm_struct *mm,
1801 unsigned long start, unsigned long nr_pages,
1802 unsigned int gup_flags, struct page **pages,
1803 struct vm_area_struct **vmas, int *locked)
1804{
1805 return 0;
1806}
John Hubbard3faa52c2020-04-01 21:05:29 -07001807
1808static long __get_user_pages_remote(struct task_struct *tsk,
1809 struct mm_struct *mm,
1810 unsigned long start, unsigned long nr_pages,
1811 unsigned int gup_flags, struct page **pages,
1812 struct vm_area_struct **vmas, int *locked)
1813{
1814 return 0;
1815}
John Hubbardeddb1c22020-01-30 22:12:54 -08001816#endif /* !CONFIG_MMU */
1817
John Hubbardc4237f82020-01-30 22:12:36 -08001818/*
Ira Weiny932f4a62019-05-13 17:17:03 -07001819 * This is the same as get_user_pages_remote(), just with a
1820 * less-flexible calling convention where we assume that the task
1821 * and mm being operated on are the current task's and don't allow
1822 * passing of a locked parameter. We also obviously don't pass
1823 * FOLL_REMOTE in here.
1824 */
1825long get_user_pages(unsigned long start, unsigned long nr_pages,
1826 unsigned int gup_flags, struct page **pages,
1827 struct vm_area_struct **vmas)
1828{
John Hubbardeddb1c22020-01-30 22:12:54 -08001829 /*
1830 * FOLL_PIN must only be set internally by the pin_user_pages*() APIs,
1831 * never directly by the caller, so enforce that with an assertion:
1832 */
1833 if (WARN_ON_ONCE(gup_flags & FOLL_PIN))
1834 return -EINVAL;
1835
Ira Weiny932f4a62019-05-13 17:17:03 -07001836 return __gup_longterm_locked(current, current->mm, start, nr_pages,
1837 pages, vmas, gup_flags | FOLL_TOUCH);
1838}
1839EXPORT_SYMBOL(get_user_pages);
Dan Williams2bb6d282017-11-29 16:10:35 -08001840
Christoph Hellwigd3649f62019-07-11 20:57:18 -07001841/*
1842 * We can leverage the VM_FAULT_RETRY functionality in the page fault
1843 * paths better by using either get_user_pages_locked() or
1844 * get_user_pages_unlocked().
Kirill A. Shutemovacc3c8d2015-04-14 15:44:45 -07001845 *
Christoph Hellwigd3649f62019-07-11 20:57:18 -07001846 * get_user_pages_locked() is suitable to replace the form:
Kirill A. Shutemovacc3c8d2015-04-14 15:44:45 -07001847 *
Christoph Hellwigd3649f62019-07-11 20:57:18 -07001848 * down_read(&mm->mmap_sem);
1849 * do_something()
1850 * get_user_pages(tsk, mm, ..., pages, NULL);
1851 * up_read(&mm->mmap_sem);
Kirill A. Shutemovacc3c8d2015-04-14 15:44:45 -07001852 *
Christoph Hellwigd3649f62019-07-11 20:57:18 -07001853 * to:
Kirill A. Shutemovacc3c8d2015-04-14 15:44:45 -07001854 *
Christoph Hellwigd3649f62019-07-11 20:57:18 -07001855 * int locked = 1;
1856 * down_read(&mm->mmap_sem);
1857 * do_something()
1858 * get_user_pages_locked(tsk, mm, ..., pages, &locked);
1859 * if (locked)
1860 * up_read(&mm->mmap_sem);
Kirill A. Shutemovacc3c8d2015-04-14 15:44:45 -07001861 */
Christoph Hellwigd3649f62019-07-11 20:57:18 -07001862long get_user_pages_locked(unsigned long start, unsigned long nr_pages,
1863 unsigned int gup_flags, struct page **pages,
1864 int *locked)
Kirill A. Shutemovacc3c8d2015-04-14 15:44:45 -07001865{
Kirill A. Shutemovacc3c8d2015-04-14 15:44:45 -07001866 /*
Christoph Hellwigd3649f62019-07-11 20:57:18 -07001867 * FIXME: Current FOLL_LONGTERM behavior is incompatible with
1868 * FAULT_FLAG_ALLOW_RETRY because of the FS DAX check requirement on
1869 * vmas. As there are no users of this flag in this call we simply
1870 * disallow this option for now.
Kirill A. Shutemovacc3c8d2015-04-14 15:44:45 -07001871 */
Christoph Hellwigd3649f62019-07-11 20:57:18 -07001872 if (WARN_ON_ONCE(gup_flags & FOLL_LONGTERM))
1873 return -EINVAL;
Kirill A. Shutemovacc3c8d2015-04-14 15:44:45 -07001874
Christoph Hellwigd3649f62019-07-11 20:57:18 -07001875 return __get_user_pages_locked(current, current->mm, start, nr_pages,
1876 pages, NULL, locked,
1877 gup_flags | FOLL_TOUCH);
Kirill A. Shutemovacc3c8d2015-04-14 15:44:45 -07001878}
Christoph Hellwigd3649f62019-07-11 20:57:18 -07001879EXPORT_SYMBOL(get_user_pages_locked);
Kirill A. Shutemovacc3c8d2015-04-14 15:44:45 -07001880
1881/*
Christoph Hellwigd3649f62019-07-11 20:57:18 -07001882 * get_user_pages_unlocked() is suitable to replace the form:
Kirill A. Shutemovacc3c8d2015-04-14 15:44:45 -07001883 *
Christoph Hellwigd3649f62019-07-11 20:57:18 -07001884 * down_read(&mm->mmap_sem);
1885 * get_user_pages(tsk, mm, ..., pages, NULL);
1886 * up_read(&mm->mmap_sem);
1887 *
1888 * with:
1889 *
1890 * get_user_pages_unlocked(tsk, mm, ..., pages);
1891 *
1892 * It is functionally equivalent to get_user_pages_fast so
1893 * get_user_pages_fast should be used instead if specific gup_flags
1894 * (e.g. FOLL_FORCE) are not required.
Kirill A. Shutemovacc3c8d2015-04-14 15:44:45 -07001895 */
Christoph Hellwigd3649f62019-07-11 20:57:18 -07001896long get_user_pages_unlocked(unsigned long start, unsigned long nr_pages,
1897 struct page **pages, unsigned int gup_flags)
Kirill A. Shutemovacc3c8d2015-04-14 15:44:45 -07001898{
1899 struct mm_struct *mm = current->mm;
Christoph Hellwigd3649f62019-07-11 20:57:18 -07001900 int locked = 1;
1901 long ret;
Kirill A. Shutemovacc3c8d2015-04-14 15:44:45 -07001902
Christoph Hellwigd3649f62019-07-11 20:57:18 -07001903 /*
1904 * FIXME: Current FOLL_LONGTERM behavior is incompatible with
1905 * FAULT_FLAG_ALLOW_RETRY because of the FS DAX check requirement on
1906 * vmas. As there are no users of this flag in this call we simply
1907 * disallow this option for now.
1908 */
1909 if (WARN_ON_ONCE(gup_flags & FOLL_LONGTERM))
1910 return -EINVAL;
Kirill A. Shutemovacc3c8d2015-04-14 15:44:45 -07001911
Christoph Hellwigd3649f62019-07-11 20:57:18 -07001912 down_read(&mm->mmap_sem);
1913 ret = __get_user_pages_locked(current, mm, start, nr_pages, pages, NULL,
1914 &locked, gup_flags | FOLL_TOUCH);
Kirill A. Shutemovacc3c8d2015-04-14 15:44:45 -07001915 if (locked)
1916 up_read(&mm->mmap_sem);
Christoph Hellwigd3649f62019-07-11 20:57:18 -07001917 return ret;
Kirill A. Shutemovacc3c8d2015-04-14 15:44:45 -07001918}
Christoph Hellwigd3649f62019-07-11 20:57:18 -07001919EXPORT_SYMBOL(get_user_pages_unlocked);
Steve Capper2667f502014-10-09 15:29:14 -07001920
1921/*
Christoph Hellwig67a929e2019-07-11 20:57:14 -07001922 * Fast GUP
Steve Capper2667f502014-10-09 15:29:14 -07001923 *
1924 * get_user_pages_fast attempts to pin user pages by walking the page
1925 * tables directly and avoids taking locks. Thus the walker needs to be
1926 * protected from page table pages being freed from under it, and should
1927 * block any THP splits.
1928 *
1929 * One way to achieve this is to have the walker disable interrupts, and
1930 * rely on IPIs from the TLB flushing code blocking before the page table
1931 * pages are freed. This is unsuitable for architectures that do not need
1932 * to broadcast an IPI when invalidating TLBs.
1933 *
1934 * Another way to achieve this is to batch up page table containing pages
1935 * belonging to more than one mm_user, then rcu_sched a callback to free those
1936 * pages. Disabling interrupts will allow the fast_gup walker to both block
1937 * the rcu_sched callback, and an IPI that we broadcast for splitting THPs
1938 * (which is a relatively rare event). The code below adopts this strategy.
1939 *
1940 * Before activating this code, please be aware that the following assumptions
1941 * are currently made:
1942 *
Peter Zijlstraff2e6d722020-02-03 17:37:02 -08001943 * *) Either MMU_GATHER_RCU_TABLE_FREE is enabled, and tlb_remove_table() is used to
Kirill A. Shutemove5855132017-06-06 14:31:20 +03001944 * free pages containing page tables or TLB flushing requires IPI broadcast.
Steve Capper2667f502014-10-09 15:29:14 -07001945 *
Steve Capper2667f502014-10-09 15:29:14 -07001946 * *) ptes can be read atomically by the architecture.
1947 *
1948 * *) access_ok is sufficient to validate userspace address ranges.
1949 *
1950 * The last two assumptions can be relaxed by the addition of helper functions.
1951 *
1952 * This code is based heavily on the PowerPC implementation by Nick Piggin.
1953 */
Christoph Hellwig67a929e2019-07-11 20:57:14 -07001954#ifdef CONFIG_HAVE_FAST_GUP
John Hubbard3faa52c2020-04-01 21:05:29 -07001955
1956static void put_compound_head(struct page *page, int refs, unsigned int flags)
1957{
1958 if (flags & FOLL_PIN)
1959 refs *= GUP_PIN_COUNTING_BIAS;
1960
1961 VM_BUG_ON_PAGE(page_ref_count(page) < refs, page);
1962 /*
1963 * Calling put_page() for each ref is unnecessarily slow. Only the last
1964 * ref needs a put_page().
1965 */
1966 if (refs > 1)
1967 page_ref_sub(page, refs - 1);
1968 put_page(page);
1969}
1970
Christoph Hellwig39656e82019-07-11 20:56:49 -07001971#ifdef CONFIG_GUP_GET_PTE_LOW_HIGH
John Hubbard3faa52c2020-04-01 21:05:29 -07001972
Kirill A. Shutemov0005d202017-03-16 18:26:51 +03001973/*
Christoph Hellwig39656e82019-07-11 20:56:49 -07001974 * WARNING: only to be used in the get_user_pages_fast() implementation.
1975 *
1976 * With get_user_pages_fast(), we walk down the pagetables without taking any
1977 * locks. For this we would like to load the pointers atomically, but sometimes
1978 * that is not possible (e.g. without expensive cmpxchg8b on x86_32 PAE). What
1979 * we do have is the guarantee that a PTE will only either go from not present
1980 * to present, or present to not present or both -- it will not switch to a
1981 * completely different present page without a TLB flush in between; something
1982 * that we are blocking by holding interrupts off.
1983 *
1984 * Setting ptes from not present to present goes:
1985 *
1986 * ptep->pte_high = h;
1987 * smp_wmb();
1988 * ptep->pte_low = l;
1989 *
1990 * And present to not present goes:
1991 *
1992 * ptep->pte_low = 0;
1993 * smp_wmb();
1994 * ptep->pte_high = 0;
1995 *
1996 * We must ensure here that the load of pte_low sees 'l' IFF pte_high sees 'h'.
1997 * We load pte_high *after* loading pte_low, which ensures we don't see an older
1998 * value of pte_high. *Then* we recheck pte_low, which ensures that we haven't
1999 * picked up a changed pte high. We might have gotten rubbish values from
2000 * pte_low and pte_high, but we are guaranteed that pte_low will not have the
2001 * present bit set *unless* it is 'l'. Because get_user_pages_fast() only
2002 * operates on present ptes we're safe.
2003 */
2004static inline pte_t gup_get_pte(pte_t *ptep)
2005{
2006 pte_t pte;
2007
2008 do {
2009 pte.pte_low = ptep->pte_low;
2010 smp_rmb();
2011 pte.pte_high = ptep->pte_high;
2012 smp_rmb();
2013 } while (unlikely(pte.pte_low != ptep->pte_low));
2014
2015 return pte;
2016}
2017#else /* CONFIG_GUP_GET_PTE_LOW_HIGH */
2018/*
2019 * We require that the PTE can be read atomically.
Kirill A. Shutemov0005d202017-03-16 18:26:51 +03002020 */
2021static inline pte_t gup_get_pte(pte_t *ptep)
2022{
2023 return READ_ONCE(*ptep);
2024}
Christoph Hellwig39656e82019-07-11 20:56:49 -07002025#endif /* CONFIG_GUP_GET_PTE_LOW_HIGH */
Kirill A. Shutemov0005d202017-03-16 18:26:51 +03002026
Guenter Roeck790c7362019-07-11 20:57:46 -07002027static void __maybe_unused undo_dev_pagemap(int *nr, int nr_start,
John Hubbard3b78d832020-04-01 21:05:22 -07002028 unsigned int flags,
Guenter Roeck790c7362019-07-11 20:57:46 -07002029 struct page **pages)
Kirill A. Shutemovb59f65f2017-03-16 18:26:53 +03002030{
2031 while ((*nr) - nr_start) {
2032 struct page *page = pages[--(*nr)];
2033
2034 ClearPageReferenced(page);
John Hubbard3faa52c2020-04-01 21:05:29 -07002035 if (flags & FOLL_PIN)
2036 unpin_user_page(page);
2037 else
2038 put_page(page);
Kirill A. Shutemovb59f65f2017-03-16 18:26:53 +03002039 }
2040}
2041
Laurent Dufour3010a5e2018-06-07 17:06:08 -07002042#ifdef CONFIG_ARCH_HAS_PTE_SPECIAL
Steve Capper2667f502014-10-09 15:29:14 -07002043static int gup_pte_range(pmd_t pmd, unsigned long addr, unsigned long end,
Ira Weinyb798bec2019-05-13 17:17:07 -07002044 unsigned int flags, struct page **pages, int *nr)
Steve Capper2667f502014-10-09 15:29:14 -07002045{
Kirill A. Shutemovb59f65f2017-03-16 18:26:53 +03002046 struct dev_pagemap *pgmap = NULL;
2047 int nr_start = *nr, ret = 0;
Steve Capper2667f502014-10-09 15:29:14 -07002048 pte_t *ptep, *ptem;
Steve Capper2667f502014-10-09 15:29:14 -07002049
2050 ptem = ptep = pte_offset_map(&pmd, addr);
2051 do {
Kirill A. Shutemov0005d202017-03-16 18:26:51 +03002052 pte_t pte = gup_get_pte(ptep);
Kirill A. Shutemov7aef4172016-01-15 16:52:32 -08002053 struct page *head, *page;
Steve Capper2667f502014-10-09 15:29:14 -07002054
2055 /*
2056 * Similar to the PMD case below, NUMA hinting must take slow
Mel Gorman8a0516e2015-02-12 14:58:22 -08002057 * path using the pte_protnone check.
Steve Capper2667f502014-10-09 15:29:14 -07002058 */
Kirill A. Shutemove7884f82017-03-16 18:26:50 +03002059 if (pte_protnone(pte))
2060 goto pte_unmap;
2061
Ira Weinyb798bec2019-05-13 17:17:07 -07002062 if (!pte_access_permitted(pte, flags & FOLL_WRITE))
Kirill A. Shutemove7884f82017-03-16 18:26:50 +03002063 goto pte_unmap;
2064
Kirill A. Shutemovb59f65f2017-03-16 18:26:53 +03002065 if (pte_devmap(pte)) {
Ira Weiny7af75562019-05-13 17:17:14 -07002066 if (unlikely(flags & FOLL_LONGTERM))
2067 goto pte_unmap;
2068
Kirill A. Shutemovb59f65f2017-03-16 18:26:53 +03002069 pgmap = get_dev_pagemap(pte_pfn(pte), pgmap);
2070 if (unlikely(!pgmap)) {
John Hubbard3b78d832020-04-01 21:05:22 -07002071 undo_dev_pagemap(nr, nr_start, flags, pages);
Kirill A. Shutemovb59f65f2017-03-16 18:26:53 +03002072 goto pte_unmap;
2073 }
2074 } else if (pte_special(pte))
Steve Capper2667f502014-10-09 15:29:14 -07002075 goto pte_unmap;
2076
2077 VM_BUG_ON(!pfn_valid(pte_pfn(pte)));
2078 page = pte_page(pte);
2079
John Hubbard3faa52c2020-04-01 21:05:29 -07002080 head = try_grab_compound_head(page, 1, flags);
Linus Torvalds8fde12c2019-04-11 10:49:19 -07002081 if (!head)
Steve Capper2667f502014-10-09 15:29:14 -07002082 goto pte_unmap;
2083
2084 if (unlikely(pte_val(pte) != pte_val(*ptep))) {
John Hubbard3faa52c2020-04-01 21:05:29 -07002085 put_compound_head(head, 1, flags);
Steve Capper2667f502014-10-09 15:29:14 -07002086 goto pte_unmap;
2087 }
2088
Kirill A. Shutemov7aef4172016-01-15 16:52:32 -08002089 VM_BUG_ON_PAGE(compound_head(page) != head, page);
Kirill A. Shutemove9348052017-03-16 18:26:52 +03002090
2091 SetPageReferenced(page);
Steve Capper2667f502014-10-09 15:29:14 -07002092 pages[*nr] = page;
2093 (*nr)++;
2094
2095 } while (ptep++, addr += PAGE_SIZE, addr != end);
2096
2097 ret = 1;
2098
2099pte_unmap:
Christoph Hellwig832d7aa2017-12-29 08:54:01 +01002100 if (pgmap)
2101 put_dev_pagemap(pgmap);
Steve Capper2667f502014-10-09 15:29:14 -07002102 pte_unmap(ptem);
2103 return ret;
2104}
2105#else
2106
2107/*
2108 * If we can't determine whether or not a pte is special, then fail immediately
2109 * for ptes. Note, we can still pin HugeTLB and THP as these are guaranteed not
2110 * to be special.
2111 *
2112 * For a futex to be placed on a THP tail page, get_futex_key requires a
2113 * __get_user_pages_fast implementation that can pin pages. Thus it's still
2114 * useful to have gup_huge_pmd even if we can't operate on ptes.
2115 */
2116static int gup_pte_range(pmd_t pmd, unsigned long addr, unsigned long end,
Ira Weinyb798bec2019-05-13 17:17:07 -07002117 unsigned int flags, struct page **pages, int *nr)
Steve Capper2667f502014-10-09 15:29:14 -07002118{
2119 return 0;
2120}
Laurent Dufour3010a5e2018-06-07 17:06:08 -07002121#endif /* CONFIG_ARCH_HAS_PTE_SPECIAL */
Steve Capper2667f502014-10-09 15:29:14 -07002122
Robin Murphy17596732019-07-16 16:30:47 -07002123#if defined(CONFIG_ARCH_HAS_PTE_DEVMAP) && defined(CONFIG_TRANSPARENT_HUGEPAGE)
Kirill A. Shutemovb59f65f2017-03-16 18:26:53 +03002124static int __gup_device_huge(unsigned long pfn, unsigned long addr,
John Hubbard86dfbed2020-04-01 21:05:14 -07002125 unsigned long end, unsigned int flags,
2126 struct page **pages, int *nr)
Kirill A. Shutemovb59f65f2017-03-16 18:26:53 +03002127{
2128 int nr_start = *nr;
2129 struct dev_pagemap *pgmap = NULL;
2130
2131 do {
2132 struct page *page = pfn_to_page(pfn);
2133
2134 pgmap = get_dev_pagemap(pfn, pgmap);
2135 if (unlikely(!pgmap)) {
John Hubbard3b78d832020-04-01 21:05:22 -07002136 undo_dev_pagemap(nr, nr_start, flags, pages);
Kirill A. Shutemovb59f65f2017-03-16 18:26:53 +03002137 return 0;
2138 }
2139 SetPageReferenced(page);
2140 pages[*nr] = page;
John Hubbard3faa52c2020-04-01 21:05:29 -07002141 if (unlikely(!try_grab_page(page, flags))) {
2142 undo_dev_pagemap(nr, nr_start, flags, pages);
2143 return 0;
2144 }
Kirill A. Shutemovb59f65f2017-03-16 18:26:53 +03002145 (*nr)++;
2146 pfn++;
2147 } while (addr += PAGE_SIZE, addr != end);
Christoph Hellwig832d7aa2017-12-29 08:54:01 +01002148
2149 if (pgmap)
2150 put_dev_pagemap(pgmap);
Kirill A. Shutemovb59f65f2017-03-16 18:26:53 +03002151 return 1;
2152}
2153
Dan Williamsa9b6de72018-04-19 21:32:19 -07002154static int __gup_device_huge_pmd(pmd_t orig, pmd_t *pmdp, unsigned long addr,
John Hubbard86dfbed2020-04-01 21:05:14 -07002155 unsigned long end, unsigned int flags,
2156 struct page **pages, int *nr)
Kirill A. Shutemovb59f65f2017-03-16 18:26:53 +03002157{
2158 unsigned long fault_pfn;
Dan Williamsa9b6de72018-04-19 21:32:19 -07002159 int nr_start = *nr;
Kirill A. Shutemovb59f65f2017-03-16 18:26:53 +03002160
Dan Williamsa9b6de72018-04-19 21:32:19 -07002161 fault_pfn = pmd_pfn(orig) + ((addr & ~PMD_MASK) >> PAGE_SHIFT);
John Hubbard86dfbed2020-04-01 21:05:14 -07002162 if (!__gup_device_huge(fault_pfn, addr, end, flags, pages, nr))
Dan Williamsa9b6de72018-04-19 21:32:19 -07002163 return 0;
2164
2165 if (unlikely(pmd_val(orig) != pmd_val(*pmdp))) {
John Hubbard3b78d832020-04-01 21:05:22 -07002166 undo_dev_pagemap(nr, nr_start, flags, pages);
Dan Williamsa9b6de72018-04-19 21:32:19 -07002167 return 0;
2168 }
2169 return 1;
Kirill A. Shutemovb59f65f2017-03-16 18:26:53 +03002170}
2171
Dan Williamsa9b6de72018-04-19 21:32:19 -07002172static int __gup_device_huge_pud(pud_t orig, pud_t *pudp, unsigned long addr,
John Hubbard86dfbed2020-04-01 21:05:14 -07002173 unsigned long end, unsigned int flags,
2174 struct page **pages, int *nr)
Kirill A. Shutemovb59f65f2017-03-16 18:26:53 +03002175{
2176 unsigned long fault_pfn;
Dan Williamsa9b6de72018-04-19 21:32:19 -07002177 int nr_start = *nr;
Kirill A. Shutemovb59f65f2017-03-16 18:26:53 +03002178
Dan Williamsa9b6de72018-04-19 21:32:19 -07002179 fault_pfn = pud_pfn(orig) + ((addr & ~PUD_MASK) >> PAGE_SHIFT);
John Hubbard86dfbed2020-04-01 21:05:14 -07002180 if (!__gup_device_huge(fault_pfn, addr, end, flags, pages, nr))
Dan Williamsa9b6de72018-04-19 21:32:19 -07002181 return 0;
2182
2183 if (unlikely(pud_val(orig) != pud_val(*pudp))) {
John Hubbard3b78d832020-04-01 21:05:22 -07002184 undo_dev_pagemap(nr, nr_start, flags, pages);
Dan Williamsa9b6de72018-04-19 21:32:19 -07002185 return 0;
2186 }
2187 return 1;
Kirill A. Shutemovb59f65f2017-03-16 18:26:53 +03002188}
2189#else
Dan Williamsa9b6de72018-04-19 21:32:19 -07002190static int __gup_device_huge_pmd(pmd_t orig, pmd_t *pmdp, unsigned long addr,
John Hubbard86dfbed2020-04-01 21:05:14 -07002191 unsigned long end, unsigned int flags,
2192 struct page **pages, int *nr)
Kirill A. Shutemovb59f65f2017-03-16 18:26:53 +03002193{
2194 BUILD_BUG();
2195 return 0;
2196}
2197
Dan Williamsa9b6de72018-04-19 21:32:19 -07002198static int __gup_device_huge_pud(pud_t pud, pud_t *pudp, unsigned long addr,
John Hubbard86dfbed2020-04-01 21:05:14 -07002199 unsigned long end, unsigned int flags,
2200 struct page **pages, int *nr)
Kirill A. Shutemovb59f65f2017-03-16 18:26:53 +03002201{
2202 BUILD_BUG();
2203 return 0;
2204}
2205#endif
2206
John Hubbarda43e9822020-01-30 22:12:17 -08002207static int record_subpages(struct page *page, unsigned long addr,
2208 unsigned long end, struct page **pages)
2209{
2210 int nr;
2211
2212 for (nr = 0; addr != end; addr += PAGE_SIZE)
2213 pages[nr++] = page++;
2214
2215 return nr;
2216}
2217
Christoph Hellwigcbd34da2019-07-11 20:57:28 -07002218#ifdef CONFIG_ARCH_HAS_HUGEPD
2219static unsigned long hugepte_addr_end(unsigned long addr, unsigned long end,
2220 unsigned long sz)
2221{
2222 unsigned long __boundary = (addr + sz) & ~(sz-1);
2223 return (__boundary - 1 < end - 1) ? __boundary : end;
2224}
2225
2226static int gup_hugepte(pte_t *ptep, unsigned long sz, unsigned long addr,
John Hubbard0cd22af2019-10-18 20:19:53 -07002227 unsigned long end, unsigned int flags,
2228 struct page **pages, int *nr)
Christoph Hellwigcbd34da2019-07-11 20:57:28 -07002229{
2230 unsigned long pte_end;
2231 struct page *head, *page;
2232 pte_t pte;
2233 int refs;
2234
2235 pte_end = (addr + sz) & ~(sz-1);
2236 if (pte_end < end)
2237 end = pte_end;
2238
2239 pte = READ_ONCE(*ptep);
2240
John Hubbard0cd22af2019-10-18 20:19:53 -07002241 if (!pte_access_permitted(pte, flags & FOLL_WRITE))
Christoph Hellwigcbd34da2019-07-11 20:57:28 -07002242 return 0;
2243
2244 /* hugepages are never "special" */
2245 VM_BUG_ON(!pfn_valid(pte_pfn(pte)));
2246
Christoph Hellwigcbd34da2019-07-11 20:57:28 -07002247 head = pte_page(pte);
Christoph Hellwigcbd34da2019-07-11 20:57:28 -07002248 page = head + ((addr & (sz-1)) >> PAGE_SHIFT);
John Hubbarda43e9822020-01-30 22:12:17 -08002249 refs = record_subpages(page, addr, end, pages + *nr);
Christoph Hellwigcbd34da2019-07-11 20:57:28 -07002250
John Hubbard3faa52c2020-04-01 21:05:29 -07002251 head = try_grab_compound_head(head, refs, flags);
John Hubbarda43e9822020-01-30 22:12:17 -08002252 if (!head)
Christoph Hellwigcbd34da2019-07-11 20:57:28 -07002253 return 0;
Christoph Hellwigcbd34da2019-07-11 20:57:28 -07002254
2255 if (unlikely(pte_val(pte) != pte_val(*ptep))) {
John Hubbard3b78d832020-04-01 21:05:22 -07002256 put_compound_head(head, refs, flags);
Christoph Hellwigcbd34da2019-07-11 20:57:28 -07002257 return 0;
2258 }
2259
John Hubbarda43e9822020-01-30 22:12:17 -08002260 *nr += refs;
Christoph Hellwig520b4a42019-07-11 20:57:36 -07002261 SetPageReferenced(head);
Christoph Hellwigcbd34da2019-07-11 20:57:28 -07002262 return 1;
2263}
2264
2265static int gup_huge_pd(hugepd_t hugepd, unsigned long addr,
John Hubbard0cd22af2019-10-18 20:19:53 -07002266 unsigned int pdshift, unsigned long end, unsigned int flags,
Christoph Hellwigcbd34da2019-07-11 20:57:28 -07002267 struct page **pages, int *nr)
2268{
2269 pte_t *ptep;
2270 unsigned long sz = 1UL << hugepd_shift(hugepd);
2271 unsigned long next;
2272
2273 ptep = hugepte_offset(hugepd, addr, pdshift);
2274 do {
2275 next = hugepte_addr_end(addr, end, sz);
John Hubbard0cd22af2019-10-18 20:19:53 -07002276 if (!gup_hugepte(ptep, sz, addr, end, flags, pages, nr))
Christoph Hellwigcbd34da2019-07-11 20:57:28 -07002277 return 0;
2278 } while (ptep++, addr = next, addr != end);
2279
2280 return 1;
2281}
2282#else
2283static inline int gup_huge_pd(hugepd_t hugepd, unsigned long addr,
John Hubbard0cd22af2019-10-18 20:19:53 -07002284 unsigned int pdshift, unsigned long end, unsigned int flags,
Christoph Hellwigcbd34da2019-07-11 20:57:28 -07002285 struct page **pages, int *nr)
2286{
2287 return 0;
2288}
2289#endif /* CONFIG_ARCH_HAS_HUGEPD */
2290
Steve Capper2667f502014-10-09 15:29:14 -07002291static int gup_huge_pmd(pmd_t orig, pmd_t *pmdp, unsigned long addr,
John Hubbard0cd22af2019-10-18 20:19:53 -07002292 unsigned long end, unsigned int flags,
2293 struct page **pages, int *nr)
Steve Capper2667f502014-10-09 15:29:14 -07002294{
Kirill A. Shutemovddc58f22016-01-15 16:52:56 -08002295 struct page *head, *page;
Steve Capper2667f502014-10-09 15:29:14 -07002296 int refs;
2297
Ira Weinyb798bec2019-05-13 17:17:07 -07002298 if (!pmd_access_permitted(orig, flags & FOLL_WRITE))
Steve Capper2667f502014-10-09 15:29:14 -07002299 return 0;
2300
Ira Weiny7af75562019-05-13 17:17:14 -07002301 if (pmd_devmap(orig)) {
2302 if (unlikely(flags & FOLL_LONGTERM))
2303 return 0;
John Hubbard86dfbed2020-04-01 21:05:14 -07002304 return __gup_device_huge_pmd(orig, pmdp, addr, end, flags,
2305 pages, nr);
Ira Weiny7af75562019-05-13 17:17:14 -07002306 }
Kirill A. Shutemovb59f65f2017-03-16 18:26:53 +03002307
Punit Agrawald63206e2017-07-06 15:39:39 -07002308 page = pmd_page(orig) + ((addr & ~PMD_MASK) >> PAGE_SHIFT);
John Hubbarda43e9822020-01-30 22:12:17 -08002309 refs = record_subpages(page, addr, end, pages + *nr);
Steve Capper2667f502014-10-09 15:29:14 -07002310
John Hubbard3faa52c2020-04-01 21:05:29 -07002311 head = try_grab_compound_head(pmd_page(orig), refs, flags);
John Hubbarda43e9822020-01-30 22:12:17 -08002312 if (!head)
Steve Capper2667f502014-10-09 15:29:14 -07002313 return 0;
Steve Capper2667f502014-10-09 15:29:14 -07002314
2315 if (unlikely(pmd_val(orig) != pmd_val(*pmdp))) {
John Hubbard3b78d832020-04-01 21:05:22 -07002316 put_compound_head(head, refs, flags);
Steve Capper2667f502014-10-09 15:29:14 -07002317 return 0;
2318 }
2319
John Hubbarda43e9822020-01-30 22:12:17 -08002320 *nr += refs;
Kirill A. Shutemove9348052017-03-16 18:26:52 +03002321 SetPageReferenced(head);
Steve Capper2667f502014-10-09 15:29:14 -07002322 return 1;
2323}
2324
2325static int gup_huge_pud(pud_t orig, pud_t *pudp, unsigned long addr,
John Hubbard86dfbed2020-04-01 21:05:14 -07002326 unsigned long end, unsigned int flags,
2327 struct page **pages, int *nr)
Steve Capper2667f502014-10-09 15:29:14 -07002328{
Kirill A. Shutemovddc58f22016-01-15 16:52:56 -08002329 struct page *head, *page;
Steve Capper2667f502014-10-09 15:29:14 -07002330 int refs;
2331
Ira Weinyb798bec2019-05-13 17:17:07 -07002332 if (!pud_access_permitted(orig, flags & FOLL_WRITE))
Steve Capper2667f502014-10-09 15:29:14 -07002333 return 0;
2334
Ira Weiny7af75562019-05-13 17:17:14 -07002335 if (pud_devmap(orig)) {
2336 if (unlikely(flags & FOLL_LONGTERM))
2337 return 0;
John Hubbard86dfbed2020-04-01 21:05:14 -07002338 return __gup_device_huge_pud(orig, pudp, addr, end, flags,
2339 pages, nr);
Ira Weiny7af75562019-05-13 17:17:14 -07002340 }
Kirill A. Shutemovb59f65f2017-03-16 18:26:53 +03002341
Punit Agrawald63206e2017-07-06 15:39:39 -07002342 page = pud_page(orig) + ((addr & ~PUD_MASK) >> PAGE_SHIFT);
John Hubbarda43e9822020-01-30 22:12:17 -08002343 refs = record_subpages(page, addr, end, pages + *nr);
Steve Capper2667f502014-10-09 15:29:14 -07002344
John Hubbard3faa52c2020-04-01 21:05:29 -07002345 head = try_grab_compound_head(pud_page(orig), refs, flags);
John Hubbarda43e9822020-01-30 22:12:17 -08002346 if (!head)
Steve Capper2667f502014-10-09 15:29:14 -07002347 return 0;
Steve Capper2667f502014-10-09 15:29:14 -07002348
2349 if (unlikely(pud_val(orig) != pud_val(*pudp))) {
John Hubbard3b78d832020-04-01 21:05:22 -07002350 put_compound_head(head, refs, flags);
Steve Capper2667f502014-10-09 15:29:14 -07002351 return 0;
2352 }
2353
John Hubbarda43e9822020-01-30 22:12:17 -08002354 *nr += refs;
Kirill A. Shutemove9348052017-03-16 18:26:52 +03002355 SetPageReferenced(head);
Steve Capper2667f502014-10-09 15:29:14 -07002356 return 1;
2357}
2358
Aneesh Kumar K.Vf30c59e2014-11-05 21:57:40 +05302359static int gup_huge_pgd(pgd_t orig, pgd_t *pgdp, unsigned long addr,
Ira Weinyb798bec2019-05-13 17:17:07 -07002360 unsigned long end, unsigned int flags,
Aneesh Kumar K.Vf30c59e2014-11-05 21:57:40 +05302361 struct page **pages, int *nr)
2362{
2363 int refs;
Kirill A. Shutemovddc58f22016-01-15 16:52:56 -08002364 struct page *head, *page;
Aneesh Kumar K.Vf30c59e2014-11-05 21:57:40 +05302365
Ira Weinyb798bec2019-05-13 17:17:07 -07002366 if (!pgd_access_permitted(orig, flags & FOLL_WRITE))
Aneesh Kumar K.Vf30c59e2014-11-05 21:57:40 +05302367 return 0;
2368
Kirill A. Shutemovb59f65f2017-03-16 18:26:53 +03002369 BUILD_BUG_ON(pgd_devmap(orig));
John Hubbarda43e9822020-01-30 22:12:17 -08002370
Punit Agrawald63206e2017-07-06 15:39:39 -07002371 page = pgd_page(orig) + ((addr & ~PGDIR_MASK) >> PAGE_SHIFT);
John Hubbarda43e9822020-01-30 22:12:17 -08002372 refs = record_subpages(page, addr, end, pages + *nr);
Aneesh Kumar K.Vf30c59e2014-11-05 21:57:40 +05302373
John Hubbard3faa52c2020-04-01 21:05:29 -07002374 head = try_grab_compound_head(pgd_page(orig), refs, flags);
John Hubbarda43e9822020-01-30 22:12:17 -08002375 if (!head)
Aneesh Kumar K.Vf30c59e2014-11-05 21:57:40 +05302376 return 0;
Aneesh Kumar K.Vf30c59e2014-11-05 21:57:40 +05302377
2378 if (unlikely(pgd_val(orig) != pgd_val(*pgdp))) {
John Hubbard3b78d832020-04-01 21:05:22 -07002379 put_compound_head(head, refs, flags);
Aneesh Kumar K.Vf30c59e2014-11-05 21:57:40 +05302380 return 0;
2381 }
2382
John Hubbarda43e9822020-01-30 22:12:17 -08002383 *nr += refs;
Kirill A. Shutemove9348052017-03-16 18:26:52 +03002384 SetPageReferenced(head);
Aneesh Kumar K.Vf30c59e2014-11-05 21:57:40 +05302385 return 1;
2386}
2387
Steve Capper2667f502014-10-09 15:29:14 -07002388static int gup_pmd_range(pud_t pud, unsigned long addr, unsigned long end,
Ira Weinyb798bec2019-05-13 17:17:07 -07002389 unsigned int flags, struct page **pages, int *nr)
Steve Capper2667f502014-10-09 15:29:14 -07002390{
2391 unsigned long next;
2392 pmd_t *pmdp;
2393
2394 pmdp = pmd_offset(&pud, addr);
2395 do {
Christian Borntraeger38c5ce92015-01-06 22:54:46 +01002396 pmd_t pmd = READ_ONCE(*pmdp);
Steve Capper2667f502014-10-09 15:29:14 -07002397
2398 next = pmd_addr_end(addr, end);
Zi Yan84c3fc42017-09-08 16:11:01 -07002399 if (!pmd_present(pmd))
Steve Capper2667f502014-10-09 15:29:14 -07002400 return 0;
2401
Yu Zhao414fd082019-02-12 15:35:58 -08002402 if (unlikely(pmd_trans_huge(pmd) || pmd_huge(pmd) ||
2403 pmd_devmap(pmd))) {
Steve Capper2667f502014-10-09 15:29:14 -07002404 /*
2405 * NUMA hinting faults need to be handled in the GUP
2406 * slowpath for accounting purposes and so that they
2407 * can be serialised against THP migration.
2408 */
Mel Gorman8a0516e2015-02-12 14:58:22 -08002409 if (pmd_protnone(pmd))
Steve Capper2667f502014-10-09 15:29:14 -07002410 return 0;
2411
Ira Weinyb798bec2019-05-13 17:17:07 -07002412 if (!gup_huge_pmd(pmd, pmdp, addr, next, flags,
Steve Capper2667f502014-10-09 15:29:14 -07002413 pages, nr))
2414 return 0;
2415
Aneesh Kumar K.Vf30c59e2014-11-05 21:57:40 +05302416 } else if (unlikely(is_hugepd(__hugepd(pmd_val(pmd))))) {
2417 /*
2418 * architecture have different format for hugetlbfs
2419 * pmd format and THP pmd format
2420 */
2421 if (!gup_huge_pd(__hugepd(pmd_val(pmd)), addr,
Ira Weinyb798bec2019-05-13 17:17:07 -07002422 PMD_SHIFT, next, flags, pages, nr))
Aneesh Kumar K.Vf30c59e2014-11-05 21:57:40 +05302423 return 0;
Ira Weinyb798bec2019-05-13 17:17:07 -07002424 } else if (!gup_pte_range(pmd, addr, next, flags, pages, nr))
Mario Leinweber29231172018-04-05 16:24:18 -07002425 return 0;
Steve Capper2667f502014-10-09 15:29:14 -07002426 } while (pmdp++, addr = next, addr != end);
2427
2428 return 1;
2429}
2430
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +03002431static int gup_pud_range(p4d_t p4d, unsigned long addr, unsigned long end,
Ira Weinyb798bec2019-05-13 17:17:07 -07002432 unsigned int flags, struct page **pages, int *nr)
Steve Capper2667f502014-10-09 15:29:14 -07002433{
2434 unsigned long next;
2435 pud_t *pudp;
2436
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +03002437 pudp = pud_offset(&p4d, addr);
Steve Capper2667f502014-10-09 15:29:14 -07002438 do {
Christian Borntraegere37c6982014-12-07 21:41:33 +01002439 pud_t pud = READ_ONCE(*pudp);
Steve Capper2667f502014-10-09 15:29:14 -07002440
2441 next = pud_addr_end(addr, end);
Qiujun Huang154945202020-01-30 22:12:10 -08002442 if (unlikely(!pud_present(pud)))
Steve Capper2667f502014-10-09 15:29:14 -07002443 return 0;
Aneesh Kumar K.Vf30c59e2014-11-05 21:57:40 +05302444 if (unlikely(pud_huge(pud))) {
Ira Weinyb798bec2019-05-13 17:17:07 -07002445 if (!gup_huge_pud(pud, pudp, addr, next, flags,
Aneesh Kumar K.Vf30c59e2014-11-05 21:57:40 +05302446 pages, nr))
2447 return 0;
2448 } else if (unlikely(is_hugepd(__hugepd(pud_val(pud))))) {
2449 if (!gup_huge_pd(__hugepd(pud_val(pud)), addr,
Ira Weinyb798bec2019-05-13 17:17:07 -07002450 PUD_SHIFT, next, flags, pages, nr))
Steve Capper2667f502014-10-09 15:29:14 -07002451 return 0;
Ira Weinyb798bec2019-05-13 17:17:07 -07002452 } else if (!gup_pmd_range(pud, addr, next, flags, pages, nr))
Steve Capper2667f502014-10-09 15:29:14 -07002453 return 0;
2454 } while (pudp++, addr = next, addr != end);
2455
2456 return 1;
2457}
2458
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +03002459static int gup_p4d_range(pgd_t pgd, unsigned long addr, unsigned long end,
Ira Weinyb798bec2019-05-13 17:17:07 -07002460 unsigned int flags, struct page **pages, int *nr)
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +03002461{
2462 unsigned long next;
2463 p4d_t *p4dp;
2464
2465 p4dp = p4d_offset(&pgd, addr);
2466 do {
2467 p4d_t p4d = READ_ONCE(*p4dp);
2468
2469 next = p4d_addr_end(addr, end);
2470 if (p4d_none(p4d))
2471 return 0;
2472 BUILD_BUG_ON(p4d_huge(p4d));
2473 if (unlikely(is_hugepd(__hugepd(p4d_val(p4d))))) {
2474 if (!gup_huge_pd(__hugepd(p4d_val(p4d)), addr,
Ira Weinyb798bec2019-05-13 17:17:07 -07002475 P4D_SHIFT, next, flags, pages, nr))
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +03002476 return 0;
Ira Weinyb798bec2019-05-13 17:17:07 -07002477 } else if (!gup_pud_range(p4d, addr, next, flags, pages, nr))
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +03002478 return 0;
2479 } while (p4dp++, addr = next, addr != end);
2480
2481 return 1;
2482}
2483
Kirill A. Shutemov5b65c4672017-09-09 00:56:03 +03002484static void gup_pgd_range(unsigned long addr, unsigned long end,
Ira Weinyb798bec2019-05-13 17:17:07 -07002485 unsigned int flags, struct page **pages, int *nr)
Kirill A. Shutemov5b65c4672017-09-09 00:56:03 +03002486{
2487 unsigned long next;
2488 pgd_t *pgdp;
2489
2490 pgdp = pgd_offset(current->mm, addr);
2491 do {
2492 pgd_t pgd = READ_ONCE(*pgdp);
2493
2494 next = pgd_addr_end(addr, end);
2495 if (pgd_none(pgd))
2496 return;
2497 if (unlikely(pgd_huge(pgd))) {
Ira Weinyb798bec2019-05-13 17:17:07 -07002498 if (!gup_huge_pgd(pgd, pgdp, addr, next, flags,
Kirill A. Shutemov5b65c4672017-09-09 00:56:03 +03002499 pages, nr))
2500 return;
2501 } else if (unlikely(is_hugepd(__hugepd(pgd_val(pgd))))) {
2502 if (!gup_huge_pd(__hugepd(pgd_val(pgd)), addr,
Ira Weinyb798bec2019-05-13 17:17:07 -07002503 PGDIR_SHIFT, next, flags, pages, nr))
Kirill A. Shutemov5b65c4672017-09-09 00:56:03 +03002504 return;
Ira Weinyb798bec2019-05-13 17:17:07 -07002505 } else if (!gup_p4d_range(pgd, addr, next, flags, pages, nr))
Kirill A. Shutemov5b65c4672017-09-09 00:56:03 +03002506 return;
2507 } while (pgdp++, addr = next, addr != end);
2508}
Christoph Hellwig050a9ad2019-07-11 20:57:21 -07002509#else
2510static inline void gup_pgd_range(unsigned long addr, unsigned long end,
2511 unsigned int flags, struct page **pages, int *nr)
2512{
2513}
2514#endif /* CONFIG_HAVE_FAST_GUP */
Kirill A. Shutemov5b65c4672017-09-09 00:56:03 +03002515
2516#ifndef gup_fast_permitted
2517/*
2518 * Check if it's allowed to use __get_user_pages_fast() for the range, or
2519 * we need to fall back to the slow version:
2520 */
Christoph Hellwig26f4c322019-07-11 20:56:45 -07002521static bool gup_fast_permitted(unsigned long start, unsigned long end)
Kirill A. Shutemov5b65c4672017-09-09 00:56:03 +03002522{
Christoph Hellwig26f4c322019-07-11 20:56:45 -07002523 return true;
Kirill A. Shutemov5b65c4672017-09-09 00:56:03 +03002524}
2525#endif
2526
Steve Capper2667f502014-10-09 15:29:14 -07002527/*
2528 * 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 -07002529 * the regular GUP.
2530 * Note a difference with get_user_pages_fast: this always returns the
2531 * number of pages pinned, 0 if no pages were pinned.
Christoph Hellwig050a9ad2019-07-11 20:57:21 -07002532 *
2533 * If the architecture does not support this function, simply return with no
2534 * pages pinned.
Steve Capper2667f502014-10-09 15:29:14 -07002535 */
2536int __get_user_pages_fast(unsigned long start, int nr_pages, int write,
2537 struct page **pages)
2538{
Wei Yangd4faa402018-10-26 15:07:55 -07002539 unsigned long len, end;
Kirill A. Shutemov5b65c4672017-09-09 00:56:03 +03002540 unsigned long flags;
Steve Capper2667f502014-10-09 15:29:14 -07002541 int nr = 0;
John Hubbard94202f12020-04-01 21:05:25 -07002542 /*
2543 * Internally (within mm/gup.c), gup fast variants must set FOLL_GET,
2544 * because gup fast is always a "pin with a +1 page refcount" request.
2545 */
2546 unsigned int gup_flags = FOLL_GET;
2547
2548 if (write)
2549 gup_flags |= FOLL_WRITE;
Steve Capper2667f502014-10-09 15:29:14 -07002550
Christoph Hellwigf455c8542019-07-11 20:56:41 -07002551 start = untagged_addr(start) & PAGE_MASK;
Steve Capper2667f502014-10-09 15:29:14 -07002552 len = (unsigned long) nr_pages << PAGE_SHIFT;
2553 end = start + len;
2554
Christoph Hellwig26f4c322019-07-11 20:56:45 -07002555 if (end <= start)
2556 return 0;
Linus Torvalds96d4f262019-01-03 18:57:57 -08002557 if (unlikely(!access_ok((void __user *)start, len)))
Steve Capper2667f502014-10-09 15:29:14 -07002558 return 0;
2559
2560 /*
2561 * Disable interrupts. We use the nested form as we can already have
2562 * interrupts disabled by get_futex_key.
2563 *
2564 * With interrupts disabled, we block page table pages from being
Fengguang Wu2ebe8222018-10-30 15:10:51 -07002565 * freed from under us. See struct mmu_table_batch comments in
2566 * include/asm-generic/tlb.h for more details.
Steve Capper2667f502014-10-09 15:29:14 -07002567 *
2568 * We do not adopt an rcu_read_lock(.) here as we also want to
2569 * block IPIs that come from THPs splitting.
2570 */
2571
Christoph Hellwig050a9ad2019-07-11 20:57:21 -07002572 if (IS_ENABLED(CONFIG_HAVE_FAST_GUP) &&
2573 gup_fast_permitted(start, end)) {
Kirill A. Shutemov5b65c4672017-09-09 00:56:03 +03002574 local_irq_save(flags);
John Hubbard94202f12020-04-01 21:05:25 -07002575 gup_pgd_range(start, end, gup_flags, pages, &nr);
Kirill A. Shutemov5b65c4672017-09-09 00:56:03 +03002576 local_irq_restore(flags);
2577 }
Steve Capper2667f502014-10-09 15:29:14 -07002578
2579 return nr;
2580}
Christoph Hellwig050a9ad2019-07-11 20:57:21 -07002581EXPORT_SYMBOL_GPL(__get_user_pages_fast);
Steve Capper2667f502014-10-09 15:29:14 -07002582
Ira Weiny7af75562019-05-13 17:17:14 -07002583static int __gup_longterm_unlocked(unsigned long start, int nr_pages,
2584 unsigned int gup_flags, struct page **pages)
2585{
2586 int ret;
2587
2588 /*
2589 * FIXME: FOLL_LONGTERM does not work with
2590 * get_user_pages_unlocked() (see comments in that function)
2591 */
2592 if (gup_flags & FOLL_LONGTERM) {
2593 down_read(&current->mm->mmap_sem);
2594 ret = __gup_longterm_locked(current, current->mm,
2595 start, nr_pages,
2596 pages, NULL, gup_flags);
2597 up_read(&current->mm->mmap_sem);
2598 } else {
2599 ret = get_user_pages_unlocked(start, nr_pages,
2600 pages, gup_flags);
2601 }
2602
2603 return ret;
2604}
2605
John Hubbardeddb1c22020-01-30 22:12:54 -08002606static int internal_get_user_pages_fast(unsigned long start, int nr_pages,
2607 unsigned int gup_flags,
2608 struct page **pages)
Steve Capper2667f502014-10-09 15:29:14 -07002609{
Kirill A. Shutemov5b65c4672017-09-09 00:56:03 +03002610 unsigned long addr, len, end;
Kirill A. Shutemov73e10a62017-03-16 18:26:54 +03002611 int nr = 0, ret = 0;
Steve Capper2667f502014-10-09 15:29:14 -07002612
John Hubbardf4000fd2020-01-30 22:12:43 -08002613 if (WARN_ON_ONCE(gup_flags & ~(FOLL_WRITE | FOLL_LONGTERM |
John Hubbard94202f12020-04-01 21:05:25 -07002614 FOLL_FORCE | FOLL_PIN | FOLL_GET)))
Christoph Hellwig817be122019-07-11 20:57:25 -07002615 return -EINVAL;
2616
Christoph Hellwigf455c8542019-07-11 20:56:41 -07002617 start = untagged_addr(start) & PAGE_MASK;
Kirill A. Shutemov5b65c4672017-09-09 00:56:03 +03002618 addr = start;
2619 len = (unsigned long) nr_pages << PAGE_SHIFT;
2620 end = start + len;
2621
Christoph Hellwig26f4c322019-07-11 20:56:45 -07002622 if (end <= start)
Michael S. Tsirkinc61611f2018-04-13 15:35:20 -07002623 return 0;
Linus Torvalds96d4f262019-01-03 18:57:57 -08002624 if (unlikely(!access_ok((void __user *)start, len)))
Michael S. Tsirkinc61611f2018-04-13 15:35:20 -07002625 return -EFAULT;
Kirill A. Shutemov73e10a62017-03-16 18:26:54 +03002626
Christoph Hellwig050a9ad2019-07-11 20:57:21 -07002627 if (IS_ENABLED(CONFIG_HAVE_FAST_GUP) &&
2628 gup_fast_permitted(start, end)) {
Kirill A. Shutemov5b65c4672017-09-09 00:56:03 +03002629 local_irq_disable();
Ira Weiny73b01402019-05-13 17:17:11 -07002630 gup_pgd_range(addr, end, gup_flags, pages, &nr);
Kirill A. Shutemov5b65c4672017-09-09 00:56:03 +03002631 local_irq_enable();
Kirill A. Shutemov73e10a62017-03-16 18:26:54 +03002632 ret = nr;
2633 }
Steve Capper2667f502014-10-09 15:29:14 -07002634
2635 if (nr < nr_pages) {
2636 /* Try to get the remaining pages with get_user_pages */
2637 start += nr << PAGE_SHIFT;
2638 pages += nr;
2639
Ira Weiny7af75562019-05-13 17:17:14 -07002640 ret = __gup_longterm_unlocked(start, nr_pages - nr,
2641 gup_flags, pages);
Steve Capper2667f502014-10-09 15:29:14 -07002642
2643 /* Have to be a bit careful with return values */
2644 if (nr > 0) {
2645 if (ret < 0)
2646 ret = nr;
2647 else
2648 ret += nr;
2649 }
2650 }
2651
2652 return ret;
2653}
John Hubbardeddb1c22020-01-30 22:12:54 -08002654
2655/**
2656 * get_user_pages_fast() - pin user pages in memory
John Hubbard3faa52c2020-04-01 21:05:29 -07002657 * @start: starting user address
2658 * @nr_pages: number of pages from start to pin
2659 * @gup_flags: flags modifying pin behaviour
2660 * @pages: array that receives pointers to the pages pinned.
2661 * Should be at least nr_pages long.
John Hubbardeddb1c22020-01-30 22:12:54 -08002662 *
2663 * Attempt to pin user pages in memory without taking mm->mmap_sem.
2664 * If not successful, it will fall back to taking the lock and
2665 * calling get_user_pages().
2666 *
2667 * Returns number of pages pinned. This may be fewer than the number requested.
2668 * If nr_pages is 0 or negative, returns 0. If no pages were pinned, returns
2669 * -errno.
2670 */
2671int get_user_pages_fast(unsigned long start, int nr_pages,
2672 unsigned int gup_flags, struct page **pages)
2673{
2674 /*
2675 * FOLL_PIN must only be set internally by the pin_user_pages*() APIs,
2676 * never directly by the caller, so enforce that:
2677 */
2678 if (WARN_ON_ONCE(gup_flags & FOLL_PIN))
2679 return -EINVAL;
2680
John Hubbard94202f12020-04-01 21:05:25 -07002681 /*
2682 * The caller may or may not have explicitly set FOLL_GET; either way is
2683 * OK. However, internally (within mm/gup.c), gup fast variants must set
2684 * FOLL_GET, because gup fast is always a "pin with a +1 page refcount"
2685 * request.
2686 */
2687 gup_flags |= FOLL_GET;
John Hubbardeddb1c22020-01-30 22:12:54 -08002688 return internal_get_user_pages_fast(start, nr_pages, gup_flags, pages);
2689}
Christoph Hellwig050a9ad2019-07-11 20:57:21 -07002690EXPORT_SYMBOL_GPL(get_user_pages_fast);
John Hubbardeddb1c22020-01-30 22:12:54 -08002691
2692/**
2693 * pin_user_pages_fast() - pin user pages in memory without taking locks
2694 *
John Hubbard3faa52c2020-04-01 21:05:29 -07002695 * @start: starting user address
2696 * @nr_pages: number of pages from start to pin
2697 * @gup_flags: flags modifying pin behaviour
2698 * @pages: array that receives pointers to the pages pinned.
2699 * Should be at least nr_pages long.
2700 *
2701 * Nearly the same as get_user_pages_fast(), except that FOLL_PIN is set. See
2702 * get_user_pages_fast() for documentation on the function arguments, because
2703 * the arguments here are identical.
2704 *
2705 * FOLL_PIN means that the pages must be released via unpin_user_page(). Please
2706 * see Documentation/vm/pin_user_pages.rst for further details.
John Hubbardeddb1c22020-01-30 22:12:54 -08002707 *
2708 * This is intended for Case 1 (DIO) in Documentation/vm/pin_user_pages.rst. It
2709 * is NOT intended for Case 2 (RDMA: long-term pins).
2710 */
2711int pin_user_pages_fast(unsigned long start, int nr_pages,
2712 unsigned int gup_flags, struct page **pages)
2713{
John Hubbard3faa52c2020-04-01 21:05:29 -07002714 /* FOLL_GET and FOLL_PIN are mutually exclusive. */
2715 if (WARN_ON_ONCE(gup_flags & FOLL_GET))
2716 return -EINVAL;
2717
2718 gup_flags |= FOLL_PIN;
2719 return internal_get_user_pages_fast(start, nr_pages, gup_flags, pages);
John Hubbardeddb1c22020-01-30 22:12:54 -08002720}
2721EXPORT_SYMBOL_GPL(pin_user_pages_fast);
2722
2723/**
2724 * pin_user_pages_remote() - pin pages of a remote process (task != current)
2725 *
John Hubbard3faa52c2020-04-01 21:05:29 -07002726 * @tsk: the task_struct to use for page fault accounting, or
2727 * NULL if faults are not to be recorded.
2728 * @mm: mm_struct of target mm
2729 * @start: starting user address
2730 * @nr_pages: number of pages from start to pin
2731 * @gup_flags: flags modifying lookup behaviour
2732 * @pages: array that receives pointers to the pages pinned.
2733 * Should be at least nr_pages long. Or NULL, if caller
2734 * only intends to ensure the pages are faulted in.
2735 * @vmas: array of pointers to vmas corresponding to each page.
2736 * Or NULL if the caller does not require them.
2737 * @locked: pointer to lock flag indicating whether lock is held and
2738 * subsequently whether VM_FAULT_RETRY functionality can be
2739 * utilised. Lock must initially be held.
2740 *
2741 * Nearly the same as get_user_pages_remote(), except that FOLL_PIN is set. See
2742 * get_user_pages_remote() for documentation on the function arguments, because
2743 * the arguments here are identical.
2744 *
2745 * FOLL_PIN means that the pages must be released via unpin_user_page(). Please
2746 * see Documentation/vm/pin_user_pages.rst for details.
John Hubbardeddb1c22020-01-30 22:12:54 -08002747 *
2748 * This is intended for Case 1 (DIO) in Documentation/vm/pin_user_pages.rst. It
2749 * is NOT intended for Case 2 (RDMA: long-term pins).
2750 */
2751long pin_user_pages_remote(struct task_struct *tsk, struct mm_struct *mm,
2752 unsigned long start, unsigned long nr_pages,
2753 unsigned int gup_flags, struct page **pages,
2754 struct vm_area_struct **vmas, int *locked)
2755{
John Hubbard3faa52c2020-04-01 21:05:29 -07002756 /* FOLL_GET and FOLL_PIN are mutually exclusive. */
2757 if (WARN_ON_ONCE(gup_flags & FOLL_GET))
2758 return -EINVAL;
2759
2760 gup_flags |= FOLL_PIN;
2761 return __get_user_pages_remote(tsk, mm, start, nr_pages, gup_flags,
2762 pages, vmas, locked);
John Hubbardeddb1c22020-01-30 22:12:54 -08002763}
2764EXPORT_SYMBOL(pin_user_pages_remote);
2765
2766/**
2767 * pin_user_pages() - pin user pages in memory for use by other devices
2768 *
John Hubbard3faa52c2020-04-01 21:05:29 -07002769 * @start: starting user address
2770 * @nr_pages: number of pages from start to pin
2771 * @gup_flags: flags modifying lookup behaviour
2772 * @pages: array that receives pointers to the pages pinned.
2773 * Should be at least nr_pages long. Or NULL, if caller
2774 * only intends to ensure the pages are faulted in.
2775 * @vmas: array of pointers to vmas corresponding to each page.
2776 * Or NULL if the caller does not require them.
2777 *
2778 * Nearly the same as get_user_pages(), except that FOLL_TOUCH is not set, and
2779 * FOLL_PIN is set.
2780 *
2781 * FOLL_PIN means that the pages must be released via unpin_user_page(). Please
2782 * see Documentation/vm/pin_user_pages.rst for details.
John Hubbardeddb1c22020-01-30 22:12:54 -08002783 *
2784 * This is intended for Case 1 (DIO) in Documentation/vm/pin_user_pages.rst. It
2785 * is NOT intended for Case 2 (RDMA: long-term pins).
2786 */
2787long pin_user_pages(unsigned long start, unsigned long nr_pages,
2788 unsigned int gup_flags, struct page **pages,
2789 struct vm_area_struct **vmas)
2790{
John Hubbard3faa52c2020-04-01 21:05:29 -07002791 /* FOLL_GET and FOLL_PIN are mutually exclusive. */
2792 if (WARN_ON_ONCE(gup_flags & FOLL_GET))
2793 return -EINVAL;
2794
2795 gup_flags |= FOLL_PIN;
2796 return __gup_longterm_locked(current, current->mm, start, nr_pages,
2797 pages, vmas, gup_flags);
John Hubbardeddb1c22020-01-30 22:12:54 -08002798}
2799EXPORT_SYMBOL(pin_user_pages);