blob: f0af462ac1e2b6695dda14c085a856b498188e4b [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>
Mike Rapoport1507f512021-07-07 18:08:03 -070013#include <linux/secretmem.h>
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -070014
Ingo Molnar174cd4b2017-02-02 19:15:33 +010015#include <linux/sched/signal.h>
Steve Capper2667f502014-10-09 15:29:14 -070016#include <linux/rwsem.h>
Aneesh Kumar K.Vf30c59e2014-11-05 21:57:40 +053017#include <linux/hugetlb.h>
Aneesh Kumar K.V9a4e9f32019-03-05 15:47:44 -080018#include <linux/migrate.h>
19#include <linux/mm_inline.h>
20#include <linux/sched/mm.h>
Kirill A. Shutemov1027e442015-09-04 15:47:55 -070021
Dave Hansen33a709b2016-02-12 13:02:19 -080022#include <asm/mmu_context.h>
Kirill A. Shutemov1027e442015-09-04 15:47:55 -070023#include <asm/tlbflush.h>
Steve Capper2667f502014-10-09 15:29:14 -070024
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -070025#include "internal.h"
26
Keith Buschdf06b372018-10-26 15:10:28 -070027struct follow_page_context {
28 struct dev_pagemap *pgmap;
29 unsigned int page_mask;
30};
31
John Hubbard47e29d32020-04-01 21:05:33 -070032static void hpage_pincount_add(struct page *page, int refs)
33{
34 VM_BUG_ON_PAGE(!hpage_pincount_available(page), page);
35 VM_BUG_ON_PAGE(page != compound_head(page), page);
36
37 atomic_add(refs, compound_pincount_ptr(page));
38}
39
40static void hpage_pincount_sub(struct page *page, int refs)
41{
42 VM_BUG_ON_PAGE(!hpage_pincount_available(page), page);
43 VM_BUG_ON_PAGE(page != compound_head(page), page);
44
45 atomic_sub(refs, compound_pincount_ptr(page));
46}
47
Jann Hornc24d3732021-06-28 19:33:23 -070048/* Equivalent to calling put_page() @refs times. */
49static void put_page_refs(struct page *page, int refs)
50{
51#ifdef CONFIG_DEBUG_VM
52 if (VM_WARN_ON_ONCE_PAGE(page_ref_count(page) < refs, page))
53 return;
54#endif
55
56 /*
57 * Calling put_page() for each ref is unnecessarily slow. Only the last
58 * ref needs a put_page().
59 */
60 if (refs > 1)
61 page_ref_sub(page, refs - 1);
62 put_page(page);
63}
64
Linus Torvaldscd1adf12021-09-07 11:03:45 -070065/*
66 * Return the compound head page with ref appropriately incremented,
67 * or NULL if that failed.
John Hubbarda707cdd2020-01-30 22:12:21 -080068 */
Linus Torvaldscd1adf12021-09-07 11:03:45 -070069static inline struct page *try_get_compound_head(struct page *page, int refs)
John Hubbarda707cdd2020-01-30 22:12:21 -080070{
71 struct page *head = compound_head(page);
72
73 if (WARN_ON_ONCE(page_ref_count(head) < 0))
74 return NULL;
75 if (unlikely(!page_cache_add_speculative(head, refs)))
76 return NULL;
Jann Hornc24d3732021-06-28 19:33:23 -070077
78 /*
79 * At this point we have a stable reference to the head page; but it
80 * could be that between the compound_head() lookup and the refcount
81 * increment, the compound page was split, in which case we'd end up
82 * holding a reference on a page that has nothing to do with the page
83 * we were given anymore.
84 * So now that the head page is stable, recheck that the pages still
85 * belong together.
86 */
87 if (unlikely(compound_head(page) != head)) {
88 put_page_refs(head, refs);
89 return NULL;
90 }
91
John Hubbarda707cdd2020-01-30 22:12:21 -080092 return head;
93}
94
John Hubbard3967db22021-09-02 14:53:48 -070095/**
John Hubbard3faa52c2020-04-01 21:05:29 -070096 * try_grab_compound_head() - attempt to elevate a page's refcount, by a
97 * flags-dependent amount.
98 *
John Hubbard3967db22021-09-02 14:53:48 -070099 * Even though the name includes "compound_head", this function is still
100 * appropriate for callers that have a non-compound @page to get.
101 *
102 * @page: pointer to page to be grabbed
103 * @refs: the value to (effectively) add to the page's refcount
104 * @flags: gup flags: these are the FOLL_* flag values.
105 *
John Hubbard3faa52c2020-04-01 21:05:29 -0700106 * "grab" names in this file mean, "look at flags to decide whether to use
107 * FOLL_PIN or FOLL_GET behavior, when incrementing the page's refcount.
108 *
109 * Either FOLL_PIN or FOLL_GET (or neither) must be set, but not both at the
110 * same time. (That's true throughout the get_user_pages*() and
111 * pin_user_pages*() APIs.) Cases:
112 *
John Hubbard3967db22021-09-02 14:53:48 -0700113 * FOLL_GET: page's refcount will be incremented by @refs.
114 *
115 * FOLL_PIN on compound pages that are > two pages long: page's refcount will
116 * be incremented by @refs, and page[2].hpage_pinned_refcount will be
117 * incremented by @refs * GUP_PIN_COUNTING_BIAS.
118 *
119 * FOLL_PIN on normal pages, or compound pages that are two pages long:
120 * page's refcount will be incremented by @refs * GUP_PIN_COUNTING_BIAS.
John Hubbard3faa52c2020-04-01 21:05:29 -0700121 *
122 * Return: head page (with refcount appropriately incremented) for success, or
123 * NULL upon failure. If neither FOLL_GET nor FOLL_PIN was set, that's
124 * considered failure, and furthermore, a likely bug in the caller, so a warning
125 * is also emitted.
126 */
John Hubbard54d516b2021-09-02 14:53:51 -0700127struct page *try_grab_compound_head(struct page *page,
128 int refs, unsigned int flags)
John Hubbard3faa52c2020-04-01 21:05:29 -0700129{
130 if (flags & FOLL_GET)
131 return try_get_compound_head(page, refs);
132 else if (flags & FOLL_PIN) {
John Hubbard47e29d32020-04-01 21:05:33 -0700133 /*
Pavel Tatashind1e153f2021-05-04 18:39:08 -0700134 * Can't do FOLL_LONGTERM + FOLL_PIN gup fast path if not in a
135 * right zone, so fail and let the caller fall back to the slow
136 * path.
Pingfan Liudf3a0a22020-04-01 21:06:04 -0700137 */
Pavel Tatashind1e153f2021-05-04 18:39:08 -0700138 if (unlikely((flags & FOLL_LONGTERM) &&
139 !is_pinnable_page(page)))
Pingfan Liudf3a0a22020-04-01 21:06:04 -0700140 return NULL;
141
142 /*
Jann Hornc24d3732021-06-28 19:33:23 -0700143 * CAUTION: Don't use compound_head() on the page before this
144 * point, the result won't be stable.
145 */
146 page = try_get_compound_head(page, refs);
147 if (!page)
148 return NULL;
149
150 /*
John Hubbard47e29d32020-04-01 21:05:33 -0700151 * When pinning a compound page of order > 1 (which is what
152 * hpage_pincount_available() checks for), use an exact count to
153 * track it, via hpage_pincount_add/_sub().
154 *
155 * However, be sure to *also* increment the normal page refcount
156 * field at least once, so that the page really is pinned.
John Hubbard3967db22021-09-02 14:53:48 -0700157 * That's why the refcount from the earlier
158 * try_get_compound_head() is left intact.
John Hubbard47e29d32020-04-01 21:05:33 -0700159 */
John Hubbard47e29d32020-04-01 21:05:33 -0700160 if (hpage_pincount_available(page))
161 hpage_pincount_add(page, refs);
Jann Hornc24d3732021-06-28 19:33:23 -0700162 else
163 page_ref_add(page, refs * (GUP_PIN_COUNTING_BIAS - 1));
John Hubbard47e29d32020-04-01 21:05:33 -0700164
John Hubbard1970dc62020-04-01 21:05:37 -0700165 mod_node_page_state(page_pgdat(page), NR_FOLL_PIN_ACQUIRED,
Miaohe Lin0fef1472021-09-02 14:53:36 -0700166 refs);
John Hubbard1970dc62020-04-01 21:05:37 -0700167
John Hubbard47e29d32020-04-01 21:05:33 -0700168 return page;
John Hubbard3faa52c2020-04-01 21:05:29 -0700169 }
170
171 WARN_ON_ONCE(1);
172 return NULL;
173}
174
Jason Gunthorpe4509b422020-12-14 19:05:51 -0800175static void put_compound_head(struct page *page, int refs, unsigned int flags)
176{
177 if (flags & FOLL_PIN) {
178 mod_node_page_state(page_pgdat(page), NR_FOLL_PIN_RELEASED,
179 refs);
180
181 if (hpage_pincount_available(page))
182 hpage_pincount_sub(page, refs);
183 else
184 refs *= GUP_PIN_COUNTING_BIAS;
185 }
186
Jann Hornc24d3732021-06-28 19:33:23 -0700187 put_page_refs(page, refs);
Jason Gunthorpe4509b422020-12-14 19:05:51 -0800188}
189
John Hubbard3faa52c2020-04-01 21:05:29 -0700190/**
191 * try_grab_page() - elevate a page's refcount by a flag-dependent amount
192 *
193 * This might not do anything at all, depending on the flags argument.
194 *
195 * "grab" names in this file mean, "look at flags to decide whether to use
196 * FOLL_PIN or FOLL_GET behavior, when incrementing the page's refcount.
197 *
198 * @page: pointer to page to be grabbed
199 * @flags: gup flags: these are the FOLL_* flag values.
200 *
201 * Either FOLL_PIN or FOLL_GET (or neither) may be set, but not both at the same
John Hubbard3967db22021-09-02 14:53:48 -0700202 * time. Cases: please see the try_grab_compound_head() documentation, with
203 * "refs=1".
John Hubbard3faa52c2020-04-01 21:05:29 -0700204 *
205 * Return: true for success, or if no action was required (if neither FOLL_PIN
206 * nor FOLL_GET was set, nothing is done). False for failure: FOLL_GET or
207 * FOLL_PIN was set, but the page could not be grabbed.
208 */
209bool __must_check try_grab_page(struct page *page, unsigned int flags)
210{
John Hubbard54d516b2021-09-02 14:53:51 -0700211 if (!(flags & (FOLL_GET | FOLL_PIN)))
212 return true;
John Hubbard3faa52c2020-04-01 21:05:29 -0700213
John Hubbard54d516b2021-09-02 14:53:51 -0700214 return try_grab_compound_head(page, 1, flags);
John Hubbard3faa52c2020-04-01 21:05:29 -0700215}
216
John Hubbard3faa52c2020-04-01 21:05:29 -0700217/**
218 * unpin_user_page() - release a dma-pinned page
219 * @page: pointer to page to be released
220 *
221 * Pages that were pinned via pin_user_pages*() must be released via either
222 * unpin_user_page(), or one of the unpin_user_pages*() routines. This is so
223 * that such pages can be separately tracked and uniquely handled. In
224 * particular, interactions with RDMA and filesystems need special handling.
225 */
226void unpin_user_page(struct page *page)
227{
Jason Gunthorpe4509b422020-12-14 19:05:51 -0800228 put_compound_head(compound_head(page), 1, FOLL_PIN);
John Hubbard3faa52c2020-04-01 21:05:29 -0700229}
230EXPORT_SYMBOL(unpin_user_page);
231
Joao Martins458a4f782021-04-29 22:55:50 -0700232static inline void compound_range_next(unsigned long i, unsigned long npages,
233 struct page **list, struct page **head,
234 unsigned int *ntails)
235{
236 struct page *next, *page;
237 unsigned int nr = 1;
238
239 if (i >= npages)
240 return;
241
242 next = *list + i;
243 page = compound_head(next);
244 if (PageCompound(page) && compound_order(page) >= 1)
245 nr = min_t(unsigned int,
246 page + compound_nr(page) - next, npages - i);
247
248 *head = page;
249 *ntails = nr;
250}
251
252#define for_each_compound_range(__i, __list, __npages, __head, __ntails) \
253 for (__i = 0, \
254 compound_range_next(__i, __npages, __list, &(__head), &(__ntails)); \
255 __i < __npages; __i += __ntails, \
256 compound_range_next(__i, __npages, __list, &(__head), &(__ntails)))
257
Joao Martins8745d7f2021-04-29 22:55:44 -0700258static inline void compound_next(unsigned long i, unsigned long npages,
259 struct page **list, struct page **head,
260 unsigned int *ntails)
261{
262 struct page *page;
263 unsigned int nr;
264
265 if (i >= npages)
266 return;
267
268 page = compound_head(list[i]);
269 for (nr = i + 1; nr < npages; nr++) {
270 if (compound_head(list[nr]) != page)
271 break;
272 }
273
274 *head = page;
275 *ntails = nr - i;
276}
277
278#define for_each_compound_head(__i, __list, __npages, __head, __ntails) \
279 for (__i = 0, \
280 compound_next(__i, __npages, __list, &(__head), &(__ntails)); \
281 __i < __npages; __i += __ntails, \
282 compound_next(__i, __npages, __list, &(__head), &(__ntails)))
283
John Hubbardfc1d8e72019-05-13 17:19:08 -0700284/**
John Hubbardf1f6a7d2020-01-30 22:13:35 -0800285 * unpin_user_pages_dirty_lock() - release and optionally dirty gup-pinned pages
akpm@linux-foundation.org2d15eb32019-09-23 15:35:04 -0700286 * @pages: array of pages to be maybe marked dirty, and definitely released.
John Hubbardfc1d8e72019-05-13 17:19:08 -0700287 * @npages: number of pages in the @pages array.
akpm@linux-foundation.org2d15eb32019-09-23 15:35:04 -0700288 * @make_dirty: whether to mark the pages dirty
John Hubbardfc1d8e72019-05-13 17:19:08 -0700289 *
290 * "gup-pinned page" refers to a page that has had one of the get_user_pages()
291 * variants called on that page.
292 *
293 * For each page in the @pages array, make that page (or its head page, if a
akpm@linux-foundation.org2d15eb32019-09-23 15:35:04 -0700294 * compound page) dirty, if @make_dirty is true, and if the page was previously
John Hubbardf1f6a7d2020-01-30 22:13:35 -0800295 * listed as clean. In any case, releases all pages using unpin_user_page(),
296 * possibly via unpin_user_pages(), for the non-dirty case.
John Hubbardfc1d8e72019-05-13 17:19:08 -0700297 *
John Hubbardf1f6a7d2020-01-30 22:13:35 -0800298 * Please see the unpin_user_page() documentation for details.
John Hubbardfc1d8e72019-05-13 17:19:08 -0700299 *
akpm@linux-foundation.org2d15eb32019-09-23 15:35:04 -0700300 * set_page_dirty_lock() is used internally. If instead, set_page_dirty() is
301 * required, then the caller should a) verify that this is really correct,
302 * because _lock() is usually required, and b) hand code it:
John Hubbardf1f6a7d2020-01-30 22:13:35 -0800303 * set_page_dirty_lock(), unpin_user_page().
John Hubbardfc1d8e72019-05-13 17:19:08 -0700304 *
305 */
John Hubbardf1f6a7d2020-01-30 22:13:35 -0800306void unpin_user_pages_dirty_lock(struct page **pages, unsigned long npages,
307 bool make_dirty)
John Hubbardfc1d8e72019-05-13 17:19:08 -0700308{
akpm@linux-foundation.org2d15eb32019-09-23 15:35:04 -0700309 unsigned long index;
Joao Martins31b912d2021-04-29 22:55:47 -0700310 struct page *head;
311 unsigned int ntails;
akpm@linux-foundation.org2d15eb32019-09-23 15:35:04 -0700312
313 if (!make_dirty) {
John Hubbardf1f6a7d2020-01-30 22:13:35 -0800314 unpin_user_pages(pages, npages);
akpm@linux-foundation.org2d15eb32019-09-23 15:35:04 -0700315 return;
316 }
317
Joao Martins31b912d2021-04-29 22:55:47 -0700318 for_each_compound_head(index, pages, npages, head, ntails) {
akpm@linux-foundation.org2d15eb32019-09-23 15:35:04 -0700319 /*
320 * Checking PageDirty at this point may race with
321 * clear_page_dirty_for_io(), but that's OK. Two key
322 * cases:
323 *
324 * 1) This code sees the page as already dirty, so it
325 * skips the call to set_page_dirty(). That could happen
326 * because clear_page_dirty_for_io() called
327 * page_mkclean(), followed by set_page_dirty().
328 * However, now the page is going to get written back,
329 * which meets the original intention of setting it
330 * dirty, so all is well: clear_page_dirty_for_io() goes
331 * on to call TestClearPageDirty(), and write the page
332 * back.
333 *
334 * 2) This code sees the page as clean, so it calls
335 * set_page_dirty(). The page stays dirty, despite being
336 * written back, so it gets written back again in the
337 * next writeback cycle. This is harmless.
338 */
Joao Martins31b912d2021-04-29 22:55:47 -0700339 if (!PageDirty(head))
340 set_page_dirty_lock(head);
341 put_compound_head(head, ntails, FOLL_PIN);
akpm@linux-foundation.org2d15eb32019-09-23 15:35:04 -0700342 }
John Hubbardfc1d8e72019-05-13 17:19:08 -0700343}
John Hubbardf1f6a7d2020-01-30 22:13:35 -0800344EXPORT_SYMBOL(unpin_user_pages_dirty_lock);
John Hubbardfc1d8e72019-05-13 17:19:08 -0700345
346/**
Joao Martins458a4f782021-04-29 22:55:50 -0700347 * unpin_user_page_range_dirty_lock() - release and optionally dirty
348 * gup-pinned page range
349 *
350 * @page: the starting page of a range maybe marked dirty, and definitely released.
351 * @npages: number of consecutive pages to release.
352 * @make_dirty: whether to mark the pages dirty
353 *
354 * "gup-pinned page range" refers to a range of pages that has had one of the
355 * pin_user_pages() variants called on that page.
356 *
357 * For the page ranges defined by [page .. page+npages], make that range (or
358 * its head pages, if a compound page) dirty, if @make_dirty is true, and if the
359 * page range was previously listed as clean.
360 *
361 * set_page_dirty_lock() is used internally. If instead, set_page_dirty() is
362 * required, then the caller should a) verify that this is really correct,
363 * because _lock() is usually required, and b) hand code it:
364 * set_page_dirty_lock(), unpin_user_page().
365 *
366 */
367void unpin_user_page_range_dirty_lock(struct page *page, unsigned long npages,
368 bool make_dirty)
369{
370 unsigned long index;
371 struct page *head;
372 unsigned int ntails;
373
374 for_each_compound_range(index, &page, npages, head, ntails) {
375 if (make_dirty && !PageDirty(head))
376 set_page_dirty_lock(head);
377 put_compound_head(head, ntails, FOLL_PIN);
378 }
379}
380EXPORT_SYMBOL(unpin_user_page_range_dirty_lock);
381
382/**
John Hubbardf1f6a7d2020-01-30 22:13:35 -0800383 * unpin_user_pages() - release an array of gup-pinned pages.
John Hubbardfc1d8e72019-05-13 17:19:08 -0700384 * @pages: array of pages to be marked dirty and released.
385 * @npages: number of pages in the @pages array.
386 *
John Hubbardf1f6a7d2020-01-30 22:13:35 -0800387 * For each page in the @pages array, release the page using unpin_user_page().
John Hubbardfc1d8e72019-05-13 17:19:08 -0700388 *
John Hubbardf1f6a7d2020-01-30 22:13:35 -0800389 * Please see the unpin_user_page() documentation for details.
John Hubbardfc1d8e72019-05-13 17:19:08 -0700390 */
John Hubbardf1f6a7d2020-01-30 22:13:35 -0800391void unpin_user_pages(struct page **pages, unsigned long npages)
John Hubbardfc1d8e72019-05-13 17:19:08 -0700392{
393 unsigned long index;
Joao Martins31b912d2021-04-29 22:55:47 -0700394 struct page *head;
395 unsigned int ntails;
John Hubbardfc1d8e72019-05-13 17:19:08 -0700396
397 /*
John Hubbard146608bb2020-10-13 16:52:01 -0700398 * If this WARN_ON() fires, then the system *might* be leaking pages (by
399 * leaving them pinned), but probably not. More likely, gup/pup returned
400 * a hard -ERRNO error to the caller, who erroneously passed it here.
401 */
402 if (WARN_ON(IS_ERR_VALUE(npages)))
403 return;
Joao Martins31b912d2021-04-29 22:55:47 -0700404
405 for_each_compound_head(index, pages, npages, head, ntails)
406 put_compound_head(head, ntails, FOLL_PIN);
John Hubbardfc1d8e72019-05-13 17:19:08 -0700407}
John Hubbardf1f6a7d2020-01-30 22:13:35 -0800408EXPORT_SYMBOL(unpin_user_pages);
John Hubbardfc1d8e72019-05-13 17:19:08 -0700409
Andrea Arcangelia458b762021-06-28 19:36:40 -0700410/*
411 * Set the MMF_HAS_PINNED if not set yet; after set it'll be there for the mm's
412 * lifecycle. Avoid setting the bit unless necessary, or it might cause write
413 * cache bouncing on large SMP machines for concurrent pinned gups.
414 */
415static inline void mm_set_has_pinned_flag(unsigned long *mm_flags)
416{
417 if (!test_bit(MMF_HAS_PINNED, mm_flags))
418 set_bit(MMF_HAS_PINNED, mm_flags);
419}
420
Christoph Hellwig050a9ad2019-07-11 20:57:21 -0700421#ifdef CONFIG_MMU
Kirill A. Shutemov69e68b42014-06-04 16:08:11 -0700422static struct page *no_page_table(struct vm_area_struct *vma,
423 unsigned int flags)
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700424{
Kirill A. Shutemov69e68b42014-06-04 16:08:11 -0700425 /*
426 * When core dumping an enormous anonymous area that nobody
427 * has touched so far, we don't want to allocate unnecessary pages or
428 * page tables. Return error instead of NULL to skip handle_mm_fault,
429 * then get_dump_page() will return NULL to leave a hole in the dump.
430 * But we can only make this optimization where a hole would surely
431 * be zero-filled if handle_mm_fault() actually did handle it.
432 */
Anshuman Khanduala0137f12020-04-06 20:03:55 -0700433 if ((flags & FOLL_DUMP) &&
434 (vma_is_anonymous(vma) || !vma->vm_ops->fault))
Kirill A. Shutemov69e68b42014-06-04 16:08:11 -0700435 return ERR_PTR(-EFAULT);
436 return NULL;
437}
438
Kirill A. Shutemov1027e442015-09-04 15:47:55 -0700439static int follow_pfn_pte(struct vm_area_struct *vma, unsigned long address,
440 pte_t *pte, unsigned int flags)
441{
442 /* No page to get reference */
443 if (flags & FOLL_GET)
444 return -EFAULT;
445
446 if (flags & FOLL_TOUCH) {
447 pte_t entry = *pte;
448
449 if (flags & FOLL_WRITE)
450 entry = pte_mkdirty(entry);
451 entry = pte_mkyoung(entry);
452
453 if (!pte_same(*pte, entry)) {
454 set_pte_at(vma->vm_mm, address, pte, entry);
455 update_mmu_cache(vma, address, pte);
456 }
457 }
458
459 /* Proper page table entry exists, but no corresponding struct page */
460 return -EEXIST;
461}
462
Linus Torvalds19be0ea2016-10-13 13:07:36 -0700463/*
Peter Xua308c712020-08-21 19:49:57 -0400464 * FOLL_FORCE can write to even unwritable pte's, but only
465 * after we've gone through a COW cycle and they are dirty.
Linus Torvalds19be0ea2016-10-13 13:07:36 -0700466 */
467static inline bool can_follow_write_pte(pte_t pte, unsigned int flags)
468{
Peter Xua308c712020-08-21 19:49:57 -0400469 return pte_write(pte) ||
470 ((flags & FOLL_FORCE) && (flags & FOLL_COW) && pte_dirty(pte));
Linus Torvalds19be0ea2016-10-13 13:07:36 -0700471}
472
Kirill A. Shutemov69e68b42014-06-04 16:08:11 -0700473static struct page *follow_page_pte(struct vm_area_struct *vma,
Keith Buschdf06b372018-10-26 15:10:28 -0700474 unsigned long address, pmd_t *pmd, unsigned int flags,
475 struct dev_pagemap **pgmap)
Kirill A. Shutemov69e68b42014-06-04 16:08:11 -0700476{
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700477 struct mm_struct *mm = vma->vm_mm;
Kirill A. Shutemov69e68b42014-06-04 16:08:11 -0700478 struct page *page;
479 spinlock_t *ptl;
480 pte_t *ptep, pte;
Claudio Imbrendaf28d4362020-04-01 21:05:56 -0700481 int ret;
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700482
John Hubbardeddb1c22020-01-30 22:12:54 -0800483 /* FOLL_GET and FOLL_PIN are mutually exclusive. */
484 if (WARN_ON_ONCE((flags & (FOLL_PIN | FOLL_GET)) ==
485 (FOLL_PIN | FOLL_GET)))
486 return ERR_PTR(-EINVAL);
Kirill A. Shutemov69e68b42014-06-04 16:08:11 -0700487retry:
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700488 if (unlikely(pmd_bad(*pmd)))
Kirill A. Shutemov69e68b42014-06-04 16:08:11 -0700489 return no_page_table(vma, flags);
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700490
491 ptep = pte_offset_map_lock(mm, pmd, address, &ptl);
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700492 pte = *ptep;
493 if (!pte_present(pte)) {
494 swp_entry_t entry;
495 /*
496 * KSM's break_ksm() relies upon recognizing a ksm page
497 * even while it is being migrated, so for that case we
498 * need migration_entry_wait().
499 */
500 if (likely(!(flags & FOLL_MIGRATION)))
501 goto no_page;
Kirill A. Shutemov0661a332015-02-10 14:10:04 -0800502 if (pte_none(pte))
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700503 goto no_page;
504 entry = pte_to_swp_entry(pte);
505 if (!is_migration_entry(entry))
506 goto no_page;
507 pte_unmap_unlock(ptep, ptl);
508 migration_entry_wait(mm, pmd, address);
Kirill A. Shutemov69e68b42014-06-04 16:08:11 -0700509 goto retry;
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700510 }
Mel Gorman8a0516e2015-02-12 14:58:22 -0800511 if ((flags & FOLL_NUMA) && pte_protnone(pte))
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700512 goto no_page;
Linus Torvalds19be0ea2016-10-13 13:07:36 -0700513 if ((flags & FOLL_WRITE) && !can_follow_write_pte(pte, flags)) {
Kirill A. Shutemov69e68b42014-06-04 16:08:11 -0700514 pte_unmap_unlock(ptep, ptl);
515 return NULL;
516 }
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700517
518 page = vm_normal_page(vma, address, pte);
John Hubbard3faa52c2020-04-01 21:05:29 -0700519 if (!page && pte_devmap(pte) && (flags & (FOLL_GET | FOLL_PIN))) {
Dan Williams3565fce2016-01-15 16:56:55 -0800520 /*
John Hubbard3faa52c2020-04-01 21:05:29 -0700521 * Only return device mapping pages in the FOLL_GET or FOLL_PIN
522 * case since they are only valid while holding the pgmap
523 * reference.
Dan Williams3565fce2016-01-15 16:56:55 -0800524 */
Keith Buschdf06b372018-10-26 15:10:28 -0700525 *pgmap = get_dev_pagemap(pte_pfn(pte), *pgmap);
526 if (*pgmap)
Dan Williams3565fce2016-01-15 16:56:55 -0800527 page = pte_page(pte);
528 else
529 goto no_page;
530 } else if (unlikely(!page)) {
Kirill A. Shutemov1027e442015-09-04 15:47:55 -0700531 if (flags & FOLL_DUMP) {
532 /* Avoid special (like zero) pages in core dumps */
533 page = ERR_PTR(-EFAULT);
534 goto out;
535 }
536
537 if (is_zero_pfn(pte_pfn(pte))) {
538 page = pte_page(pte);
539 } else {
Kirill A. Shutemov1027e442015-09-04 15:47:55 -0700540 ret = follow_pfn_pte(vma, address, ptep, flags);
541 page = ERR_PTR(ret);
542 goto out;
543 }
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700544 }
545
John Hubbard3faa52c2020-04-01 21:05:29 -0700546 /* try_grab_page() does nothing unless FOLL_GET or FOLL_PIN is set. */
547 if (unlikely(!try_grab_page(page, flags))) {
548 page = ERR_PTR(-ENOMEM);
549 goto out;
Linus Torvalds8fde12c2019-04-11 10:49:19 -0700550 }
Claudio Imbrendaf28d4362020-04-01 21:05:56 -0700551 /*
552 * We need to make the page accessible if and only if we are going
553 * to access its content (the FOLL_PIN case). Please see
554 * Documentation/core-api/pin_user_pages.rst for details.
555 */
556 if (flags & FOLL_PIN) {
557 ret = arch_make_page_accessible(page);
558 if (ret) {
559 unpin_user_page(page);
560 page = ERR_PTR(ret);
561 goto out;
562 }
563 }
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700564 if (flags & FOLL_TOUCH) {
565 if ((flags & FOLL_WRITE) &&
566 !pte_dirty(pte) && !PageDirty(page))
567 set_page_dirty(page);
568 /*
569 * pte_mkyoung() would be more correct here, but atomic care
570 * is needed to avoid losing the dirty bit: it is easier to use
571 * mark_page_accessed().
572 */
573 mark_page_accessed(page);
574 }
Eric B Munsonde60f5f2015-11-05 18:51:36 -0800575 if ((flags & FOLL_MLOCK) && (vma->vm_flags & VM_LOCKED)) {
Kirill A. Shutemove90309c2016-01-15 16:54:33 -0800576 /* Do not mlock pte-mapped THP */
577 if (PageTransCompound(page))
578 goto out;
579
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700580 /*
581 * The preliminary mapping check is mainly to avoid the
582 * pointless overhead of lock_page on the ZERO_PAGE
583 * which might bounce very badly if there is contention.
584 *
585 * If the page is already locked, we don't need to
586 * handle it now - vmscan will handle it later if and
587 * when it attempts to reclaim the page.
588 */
589 if (page->mapping && trylock_page(page)) {
590 lru_add_drain(); /* push cached pages to LRU */
591 /*
592 * Because we lock page here, and migration is
593 * blocked by the pte's page reference, and we
594 * know the page is still mapped, we don't even
595 * need to check for file-cache page truncation.
596 */
597 mlock_vma_page(page);
598 unlock_page(page);
599 }
600 }
Kirill A. Shutemov1027e442015-09-04 15:47:55 -0700601out:
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700602 pte_unmap_unlock(ptep, ptl);
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700603 return page;
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700604no_page:
605 pte_unmap_unlock(ptep, ptl);
606 if (!pte_none(pte))
Kirill A. Shutemov69e68b42014-06-04 16:08:11 -0700607 return NULL;
608 return no_page_table(vma, flags);
609}
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700610
Aneesh Kumar K.V080dbb62017-07-06 15:38:44 -0700611static struct page *follow_pmd_mask(struct vm_area_struct *vma,
612 unsigned long address, pud_t *pudp,
Keith Buschdf06b372018-10-26 15:10:28 -0700613 unsigned int flags,
614 struct follow_page_context *ctx)
Kirill A. Shutemov69e68b42014-06-04 16:08:11 -0700615{
Huang Ying68827282018-06-07 17:06:34 -0700616 pmd_t *pmd, pmdval;
Kirill A. Shutemov69e68b42014-06-04 16:08:11 -0700617 spinlock_t *ptl;
618 struct page *page;
619 struct mm_struct *mm = vma->vm_mm;
620
Aneesh Kumar K.V080dbb62017-07-06 15:38:44 -0700621 pmd = pmd_offset(pudp, address);
Huang Ying68827282018-06-07 17:06:34 -0700622 /*
623 * The READ_ONCE() will stabilize the pmdval in a register or
624 * on the stack so that it will stop changing under the code.
625 */
626 pmdval = READ_ONCE(*pmd);
627 if (pmd_none(pmdval))
Kirill A. Shutemov69e68b42014-06-04 16:08:11 -0700628 return no_page_table(vma, flags);
Wei Yangbe9d3042020-01-30 22:12:14 -0800629 if (pmd_huge(pmdval) && is_vm_hugetlb_page(vma)) {
Naoya Horiguchie66f17f2015-02-11 15:25:22 -0800630 page = follow_huge_pmd(mm, address, pmd, flags);
631 if (page)
632 return page;
633 return no_page_table(vma, flags);
Kirill A. Shutemov69e68b42014-06-04 16:08:11 -0700634 }
Huang Ying68827282018-06-07 17:06:34 -0700635 if (is_hugepd(__hugepd(pmd_val(pmdval)))) {
Aneesh Kumar K.V4dc71452017-07-06 15:38:56 -0700636 page = follow_huge_pd(vma, address,
Huang Ying68827282018-06-07 17:06:34 -0700637 __hugepd(pmd_val(pmdval)), flags,
Aneesh Kumar K.V4dc71452017-07-06 15:38:56 -0700638 PMD_SHIFT);
639 if (page)
640 return page;
641 return no_page_table(vma, flags);
642 }
Zi Yan84c3fc42017-09-08 16:11:01 -0700643retry:
Huang Ying68827282018-06-07 17:06:34 -0700644 if (!pmd_present(pmdval)) {
Li Xinhai28b0ee32022-01-14 14:05:16 -0800645 /*
646 * Should never reach here, if thp migration is not supported;
647 * Otherwise, it must be a thp migration entry.
648 */
649 VM_BUG_ON(!thp_migration_supported() ||
650 !is_pmd_migration_entry(pmdval));
651
Zi Yan84c3fc42017-09-08 16:11:01 -0700652 if (likely(!(flags & FOLL_MIGRATION)))
653 return no_page_table(vma, flags);
Li Xinhai28b0ee32022-01-14 14:05:16 -0800654
655 pmd_migration_entry_wait(mm, pmd);
Huang Ying68827282018-06-07 17:06:34 -0700656 pmdval = READ_ONCE(*pmd);
657 /*
658 * MADV_DONTNEED may convert the pmd to null because
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -0700659 * mmap_lock is held in read mode
Huang Ying68827282018-06-07 17:06:34 -0700660 */
661 if (pmd_none(pmdval))
662 return no_page_table(vma, flags);
Zi Yan84c3fc42017-09-08 16:11:01 -0700663 goto retry;
664 }
Huang Ying68827282018-06-07 17:06:34 -0700665 if (pmd_devmap(pmdval)) {
Dan Williams3565fce2016-01-15 16:56:55 -0800666 ptl = pmd_lock(mm, pmd);
Keith Buschdf06b372018-10-26 15:10:28 -0700667 page = follow_devmap_pmd(vma, address, pmd, flags, &ctx->pgmap);
Dan Williams3565fce2016-01-15 16:56:55 -0800668 spin_unlock(ptl);
669 if (page)
670 return page;
671 }
Huang Ying68827282018-06-07 17:06:34 -0700672 if (likely(!pmd_trans_huge(pmdval)))
Keith Buschdf06b372018-10-26 15:10:28 -0700673 return follow_page_pte(vma, address, pmd, flags, &ctx->pgmap);
Kirill A. Shutemov6742d292016-01-15 16:52:28 -0800674
Huang Ying68827282018-06-07 17:06:34 -0700675 if ((flags & FOLL_NUMA) && pmd_protnone(pmdval))
Aneesh Kumar K.Vdb08f202017-02-24 14:59:53 -0800676 return no_page_table(vma, flags);
677
Zi Yan84c3fc42017-09-08 16:11:01 -0700678retry_locked:
Kirill A. Shutemov6742d292016-01-15 16:52:28 -0800679 ptl = pmd_lock(mm, pmd);
Huang Ying68827282018-06-07 17:06:34 -0700680 if (unlikely(pmd_none(*pmd))) {
681 spin_unlock(ptl);
682 return no_page_table(vma, flags);
683 }
Zi Yan84c3fc42017-09-08 16:11:01 -0700684 if (unlikely(!pmd_present(*pmd))) {
685 spin_unlock(ptl);
686 if (likely(!(flags & FOLL_MIGRATION)))
687 return no_page_table(vma, flags);
688 pmd_migration_entry_wait(mm, pmd);
689 goto retry_locked;
690 }
Kirill A. Shutemov6742d292016-01-15 16:52:28 -0800691 if (unlikely(!pmd_trans_huge(*pmd))) {
692 spin_unlock(ptl);
Keith Buschdf06b372018-10-26 15:10:28 -0700693 return follow_page_pte(vma, address, pmd, flags, &ctx->pgmap);
Kirill A. Shutemov69e68b42014-06-04 16:08:11 -0700694 }
Yang Shi4066c112021-04-29 22:55:56 -0700695 if (flags & FOLL_SPLIT_PMD) {
Kirill A. Shutemov6742d292016-01-15 16:52:28 -0800696 int ret;
697 page = pmd_page(*pmd);
698 if (is_huge_zero_page(page)) {
699 spin_unlock(ptl);
700 ret = 0;
Kirill A. Shutemov78ddc532016-01-15 16:52:42 -0800701 split_huge_pmd(vma, pmd, address);
Naoya Horiguchi337d9ab2016-07-26 15:24:03 -0700702 if (pmd_trans_unstable(pmd))
703 ret = -EBUSY;
Yang Shi4066c112021-04-29 22:55:56 -0700704 } else {
Song Liubfe7b002019-09-23 15:38:25 -0700705 spin_unlock(ptl);
706 split_huge_pmd(vma, pmd, address);
707 ret = pte_alloc(mm, pmd) ? -ENOMEM : 0;
Kirill A. Shutemov6742d292016-01-15 16:52:28 -0800708 }
709
710 return ret ? ERR_PTR(ret) :
Keith Buschdf06b372018-10-26 15:10:28 -0700711 follow_page_pte(vma, address, pmd, flags, &ctx->pgmap);
Kirill A. Shutemov6742d292016-01-15 16:52:28 -0800712 }
Kirill A. Shutemov6742d292016-01-15 16:52:28 -0800713 page = follow_trans_huge_pmd(vma, address, pmd, flags);
714 spin_unlock(ptl);
Keith Buschdf06b372018-10-26 15:10:28 -0700715 ctx->page_mask = HPAGE_PMD_NR - 1;
Kirill A. Shutemov6742d292016-01-15 16:52:28 -0800716 return page;
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700717}
718
Aneesh Kumar K.V080dbb62017-07-06 15:38:44 -0700719static struct page *follow_pud_mask(struct vm_area_struct *vma,
720 unsigned long address, p4d_t *p4dp,
Keith Buschdf06b372018-10-26 15:10:28 -0700721 unsigned int flags,
722 struct follow_page_context *ctx)
Aneesh Kumar K.V080dbb62017-07-06 15:38:44 -0700723{
724 pud_t *pud;
725 spinlock_t *ptl;
726 struct page *page;
727 struct mm_struct *mm = vma->vm_mm;
728
729 pud = pud_offset(p4dp, address);
730 if (pud_none(*pud))
731 return no_page_table(vma, flags);
Wei Yangbe9d3042020-01-30 22:12:14 -0800732 if (pud_huge(*pud) && is_vm_hugetlb_page(vma)) {
Aneesh Kumar K.V080dbb62017-07-06 15:38:44 -0700733 page = follow_huge_pud(mm, address, pud, flags);
734 if (page)
735 return page;
736 return no_page_table(vma, flags);
737 }
Aneesh Kumar K.V4dc71452017-07-06 15:38:56 -0700738 if (is_hugepd(__hugepd(pud_val(*pud)))) {
739 page = follow_huge_pd(vma, address,
740 __hugepd(pud_val(*pud)), flags,
741 PUD_SHIFT);
742 if (page)
743 return page;
744 return no_page_table(vma, flags);
745 }
Aneesh Kumar K.V080dbb62017-07-06 15:38:44 -0700746 if (pud_devmap(*pud)) {
747 ptl = pud_lock(mm, pud);
Keith Buschdf06b372018-10-26 15:10:28 -0700748 page = follow_devmap_pud(vma, address, pud, flags, &ctx->pgmap);
Aneesh Kumar K.V080dbb62017-07-06 15:38:44 -0700749 spin_unlock(ptl);
750 if (page)
751 return page;
752 }
753 if (unlikely(pud_bad(*pud)))
754 return no_page_table(vma, flags);
755
Keith Buschdf06b372018-10-26 15:10:28 -0700756 return follow_pmd_mask(vma, address, pud, flags, ctx);
Aneesh Kumar K.V080dbb62017-07-06 15:38:44 -0700757}
758
Aneesh Kumar K.V080dbb62017-07-06 15:38:44 -0700759static struct page *follow_p4d_mask(struct vm_area_struct *vma,
760 unsigned long address, pgd_t *pgdp,
Keith Buschdf06b372018-10-26 15:10:28 -0700761 unsigned int flags,
762 struct follow_page_context *ctx)
Aneesh Kumar K.V080dbb62017-07-06 15:38:44 -0700763{
764 p4d_t *p4d;
Aneesh Kumar K.V4dc71452017-07-06 15:38:56 -0700765 struct page *page;
Aneesh Kumar K.V080dbb62017-07-06 15:38:44 -0700766
767 p4d = p4d_offset(pgdp, address);
768 if (p4d_none(*p4d))
769 return no_page_table(vma, flags);
770 BUILD_BUG_ON(p4d_huge(*p4d));
771 if (unlikely(p4d_bad(*p4d)))
772 return no_page_table(vma, flags);
773
Aneesh Kumar K.V4dc71452017-07-06 15:38:56 -0700774 if (is_hugepd(__hugepd(p4d_val(*p4d)))) {
775 page = follow_huge_pd(vma, address,
776 __hugepd(p4d_val(*p4d)), flags,
777 P4D_SHIFT);
778 if (page)
779 return page;
780 return no_page_table(vma, flags);
781 }
Keith Buschdf06b372018-10-26 15:10:28 -0700782 return follow_pud_mask(vma, address, p4d, flags, ctx);
Aneesh Kumar K.V080dbb62017-07-06 15:38:44 -0700783}
784
785/**
786 * follow_page_mask - look up a page descriptor from a user-virtual address
787 * @vma: vm_area_struct mapping @address
788 * @address: virtual address to look up
789 * @flags: flags modifying lookup behaviour
Mike Rapoport78179552018-11-16 15:08:29 -0800790 * @ctx: contains dev_pagemap for %ZONE_DEVICE memory pinning and a
791 * pointer to output page_mask
Aneesh Kumar K.V080dbb62017-07-06 15:38:44 -0700792 *
793 * @flags can have FOLL_ flags set, defined in <linux/mm.h>
794 *
Mike Rapoport78179552018-11-16 15:08:29 -0800795 * When getting pages from ZONE_DEVICE memory, the @ctx->pgmap caches
796 * the device's dev_pagemap metadata to avoid repeating expensive lookups.
797 *
798 * On output, the @ctx->page_mask is set according to the size of the page.
799 *
800 * Return: the mapped (struct page *), %NULL if no mapping exists, or
Aneesh Kumar K.V080dbb62017-07-06 15:38:44 -0700801 * an error pointer if there is a mapping to something not represented
802 * by a page descriptor (see also vm_normal_page()).
803 */
Bharath Vedarthama7030ae2019-07-11 20:54:34 -0700804static struct page *follow_page_mask(struct vm_area_struct *vma,
Aneesh Kumar K.V080dbb62017-07-06 15:38:44 -0700805 unsigned long address, unsigned int flags,
Keith Buschdf06b372018-10-26 15:10:28 -0700806 struct follow_page_context *ctx)
Aneesh Kumar K.V080dbb62017-07-06 15:38:44 -0700807{
808 pgd_t *pgd;
809 struct page *page;
810 struct mm_struct *mm = vma->vm_mm;
811
Keith Buschdf06b372018-10-26 15:10:28 -0700812 ctx->page_mask = 0;
Aneesh Kumar K.V080dbb62017-07-06 15:38:44 -0700813
814 /* make this handle hugepd */
815 page = follow_huge_addr(mm, address, flags & FOLL_WRITE);
816 if (!IS_ERR(page)) {
John Hubbard3faa52c2020-04-01 21:05:29 -0700817 WARN_ON_ONCE(flags & (FOLL_GET | FOLL_PIN));
Aneesh Kumar K.V080dbb62017-07-06 15:38:44 -0700818 return page;
819 }
820
821 pgd = pgd_offset(mm, address);
822
823 if (pgd_none(*pgd) || unlikely(pgd_bad(*pgd)))
824 return no_page_table(vma, flags);
825
Anshuman Khandualfaaa5b62017-07-06 15:38:50 -0700826 if (pgd_huge(*pgd)) {
827 page = follow_huge_pgd(mm, address, pgd, flags);
828 if (page)
829 return page;
830 return no_page_table(vma, flags);
831 }
Aneesh Kumar K.V4dc71452017-07-06 15:38:56 -0700832 if (is_hugepd(__hugepd(pgd_val(*pgd)))) {
833 page = follow_huge_pd(vma, address,
834 __hugepd(pgd_val(*pgd)), flags,
835 PGDIR_SHIFT);
836 if (page)
837 return page;
838 return no_page_table(vma, flags);
839 }
Anshuman Khandualfaaa5b62017-07-06 15:38:50 -0700840
Keith Buschdf06b372018-10-26 15:10:28 -0700841 return follow_p4d_mask(vma, address, pgd, flags, ctx);
842}
843
844struct page *follow_page(struct vm_area_struct *vma, unsigned long address,
845 unsigned int foll_flags)
846{
847 struct follow_page_context ctx = { NULL };
848 struct page *page;
849
Mike Rapoport1507f512021-07-07 18:08:03 -0700850 if (vma_is_secretmem(vma))
851 return NULL;
852
Keith Buschdf06b372018-10-26 15:10:28 -0700853 page = follow_page_mask(vma, address, foll_flags, &ctx);
854 if (ctx.pgmap)
855 put_dev_pagemap(ctx.pgmap);
856 return page;
Aneesh Kumar K.V080dbb62017-07-06 15:38:44 -0700857}
858
Kirill A. Shutemovf2b495c2014-06-04 16:08:11 -0700859static int get_gate_page(struct mm_struct *mm, unsigned long address,
860 unsigned int gup_flags, struct vm_area_struct **vma,
861 struct page **page)
862{
863 pgd_t *pgd;
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +0300864 p4d_t *p4d;
Kirill A. Shutemovf2b495c2014-06-04 16:08:11 -0700865 pud_t *pud;
866 pmd_t *pmd;
867 pte_t *pte;
868 int ret = -EFAULT;
869
870 /* user gate pages are read-only */
871 if (gup_flags & FOLL_WRITE)
872 return -EFAULT;
873 if (address > TASK_SIZE)
874 pgd = pgd_offset_k(address);
875 else
876 pgd = pgd_offset_gate(mm, address);
Andy Lutomirskib5d1c392019-07-11 20:57:43 -0700877 if (pgd_none(*pgd))
878 return -EFAULT;
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +0300879 p4d = p4d_offset(pgd, address);
Andy Lutomirskib5d1c392019-07-11 20:57:43 -0700880 if (p4d_none(*p4d))
881 return -EFAULT;
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +0300882 pud = pud_offset(p4d, address);
Andy Lutomirskib5d1c392019-07-11 20:57:43 -0700883 if (pud_none(*pud))
884 return -EFAULT;
Kirill A. Shutemovf2b495c2014-06-04 16:08:11 -0700885 pmd = pmd_offset(pud, address);
Zi Yan84c3fc42017-09-08 16:11:01 -0700886 if (!pmd_present(*pmd))
Kirill A. Shutemovf2b495c2014-06-04 16:08:11 -0700887 return -EFAULT;
888 VM_BUG_ON(pmd_trans_huge(*pmd));
889 pte = pte_offset_map(pmd, address);
890 if (pte_none(*pte))
891 goto unmap;
892 *vma = get_gate_vma(mm);
893 if (!page)
894 goto out;
895 *page = vm_normal_page(*vma, address, *pte);
896 if (!*page) {
897 if ((gup_flags & FOLL_DUMP) || !is_zero_pfn(pte_pfn(*pte)))
898 goto unmap;
899 *page = pte_page(*pte);
900 }
Dave Hansen9fa2dd92020-09-03 13:40:28 -0700901 if (unlikely(!try_grab_page(*page, gup_flags))) {
Linus Torvalds8fde12c2019-04-11 10:49:19 -0700902 ret = -ENOMEM;
903 goto unmap;
904 }
Kirill A. Shutemovf2b495c2014-06-04 16:08:11 -0700905out:
906 ret = 0;
907unmap:
908 pte_unmap(pte);
909 return ret;
910}
911
Paul Cassella9a95f3c2014-08-06 16:07:24 -0700912/*
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -0700913 * mmap_lock must be held on entry. If @locked != NULL and *@flags
914 * does not include FOLL_NOWAIT, the mmap_lock may be released. If it
Peter Xu4f6da932020-04-01 21:07:58 -0700915 * is, *@locked will be set to 0 and -EBUSY returned.
Paul Cassella9a95f3c2014-08-06 16:07:24 -0700916 */
Peter Xu64019a22020-08-11 18:39:01 -0700917static int faultin_page(struct vm_area_struct *vma,
Peter Xu4f6da932020-04-01 21:07:58 -0700918 unsigned long address, unsigned int *flags, int *locked)
Kirill A. Shutemov16744482014-06-04 16:08:12 -0700919{
Kirill A. Shutemov16744482014-06-04 16:08:12 -0700920 unsigned int fault_flags = 0;
Souptick Joarder2b740302018-08-23 17:01:36 -0700921 vm_fault_t ret;
Kirill A. Shutemov16744482014-06-04 16:08:12 -0700922
Eric B Munsonde60f5f2015-11-05 18:51:36 -0800923 /* mlock all present pages, but do not fault in new pages */
924 if ((*flags & (FOLL_POPULATE | FOLL_MLOCK)) == FOLL_MLOCK)
925 return -ENOENT;
Andreas Gruenbacher55b8fe72021-08-17 22:52:08 +0200926 if (*flags & FOLL_NOFAULT)
927 return -EFAULT;
Kirill A. Shutemov16744482014-06-04 16:08:12 -0700928 if (*flags & FOLL_WRITE)
929 fault_flags |= FAULT_FLAG_WRITE;
Dave Hansen1b2ee122016-02-12 13:02:21 -0800930 if (*flags & FOLL_REMOTE)
931 fault_flags |= FAULT_FLAG_REMOTE;
Peter Xu4f6da932020-04-01 21:07:58 -0700932 if (locked)
Peter Xu71335f32020-04-01 21:08:53 -0700933 fault_flags |= FAULT_FLAG_ALLOW_RETRY | FAULT_FLAG_KILLABLE;
Kirill A. Shutemov16744482014-06-04 16:08:12 -0700934 if (*flags & FOLL_NOWAIT)
935 fault_flags |= FAULT_FLAG_ALLOW_RETRY | FAULT_FLAG_RETRY_NOWAIT;
Andres Lagar-Cavilla234b2392014-09-17 10:51:48 -0700936 if (*flags & FOLL_TRIED) {
Peter Xu4426e942020-04-01 21:08:49 -0700937 /*
938 * Note: FAULT_FLAG_ALLOW_RETRY and FAULT_FLAG_TRIED
939 * can co-exist
940 */
Andres Lagar-Cavilla234b2392014-09-17 10:51:48 -0700941 fault_flags |= FAULT_FLAG_TRIED;
942 }
Kirill A. Shutemov16744482014-06-04 16:08:12 -0700943
Peter Xubce617e2020-08-11 18:37:44 -0700944 ret = handle_mm_fault(vma, address, fault_flags, NULL);
Kirill A. Shutemov16744482014-06-04 16:08:12 -0700945 if (ret & VM_FAULT_ERROR) {
James Morse9a291a72017-06-02 14:46:46 -0700946 int err = vm_fault_to_errno(ret, *flags);
947
948 if (err)
949 return err;
Kirill A. Shutemov16744482014-06-04 16:08:12 -0700950 BUG();
951 }
952
Kirill A. Shutemov16744482014-06-04 16:08:12 -0700953 if (ret & VM_FAULT_RETRY) {
Peter Xu4f6da932020-04-01 21:07:58 -0700954 if (locked && !(fault_flags & FAULT_FLAG_RETRY_NOWAIT))
955 *locked = 0;
Kirill A. Shutemov16744482014-06-04 16:08:12 -0700956 return -EBUSY;
957 }
958
959 /*
960 * The VM_FAULT_WRITE bit tells us that do_wp_page has broken COW when
961 * necessary, even if maybe_mkwrite decided not to set pte_write. We
962 * can thus safely do subsequent page lookups as if they were reads.
963 * But only do so when looping for pte_write is futile: in some cases
964 * userspace may also be wanting to write to the gotten user page,
965 * which a read fault here might prevent (a readonly page might get
966 * reCOWed by userspace write).
967 */
968 if ((ret & VM_FAULT_WRITE) && !(vma->vm_flags & VM_WRITE))
Mario Leinweber29231172018-04-05 16:24:18 -0700969 *flags |= FOLL_COW;
Kirill A. Shutemov16744482014-06-04 16:08:12 -0700970 return 0;
971}
972
Kirill A. Shutemovfa5bb202014-06-04 16:08:13 -0700973static int check_vma_flags(struct vm_area_struct *vma, unsigned long gup_flags)
974{
975 vm_flags_t vm_flags = vma->vm_flags;
Dave Hansen1b2ee122016-02-12 13:02:21 -0800976 int write = (gup_flags & FOLL_WRITE);
977 int foreign = (gup_flags & FOLL_REMOTE);
Kirill A. Shutemovfa5bb202014-06-04 16:08:13 -0700978
979 if (vm_flags & (VM_IO | VM_PFNMAP))
980 return -EFAULT;
981
Willy Tarreau7f7ccc22018-05-11 08:11:44 +0200982 if (gup_flags & FOLL_ANON && !vma_is_anonymous(vma))
983 return -EFAULT;
984
Jason Gunthorpe52650c82020-12-14 19:05:48 -0800985 if ((gup_flags & FOLL_LONGTERM) && vma_is_fsdax(vma))
986 return -EOPNOTSUPP;
987
Mike Rapoport1507f512021-07-07 18:08:03 -0700988 if (vma_is_secretmem(vma))
989 return -EFAULT;
990
Dave Hansen1b2ee122016-02-12 13:02:21 -0800991 if (write) {
Kirill A. Shutemovfa5bb202014-06-04 16:08:13 -0700992 if (!(vm_flags & VM_WRITE)) {
993 if (!(gup_flags & FOLL_FORCE))
994 return -EFAULT;
995 /*
996 * We used to let the write,force case do COW in a
997 * VM_MAYWRITE VM_SHARED !VM_WRITE vma, so ptrace could
998 * set a breakpoint in a read-only mapping of an
999 * executable, without corrupting the file (yet only
1000 * when that file had been opened for writing!).
1001 * Anon pages in shared mappings are surprising: now
1002 * just reject it.
1003 */
Hugh Dickins46435362016-01-30 18:03:16 -08001004 if (!is_cow_mapping(vm_flags))
Kirill A. Shutemovfa5bb202014-06-04 16:08:13 -07001005 return -EFAULT;
Kirill A. Shutemovfa5bb202014-06-04 16:08:13 -07001006 }
1007 } else if (!(vm_flags & VM_READ)) {
1008 if (!(gup_flags & FOLL_FORCE))
1009 return -EFAULT;
1010 /*
1011 * Is there actually any vma we can reach here which does not
1012 * have VM_MAYREAD set?
1013 */
1014 if (!(vm_flags & VM_MAYREAD))
1015 return -EFAULT;
1016 }
Dave Hansend61172b2016-02-12 13:02:24 -08001017 /*
1018 * gups are always data accesses, not instruction
1019 * fetches, so execute=false here
1020 */
1021 if (!arch_vma_access_permitted(vma, write, false, foreign))
Dave Hansen33a709b2016-02-12 13:02:19 -08001022 return -EFAULT;
Kirill A. Shutemovfa5bb202014-06-04 16:08:13 -07001023 return 0;
1024}
1025
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -07001026/**
1027 * __get_user_pages() - pin user pages in memory
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -07001028 * @mm: mm_struct of target mm
1029 * @start: starting user address
1030 * @nr_pages: number of pages from start to pin
1031 * @gup_flags: flags modifying pin behaviour
1032 * @pages: array that receives pointers to the pages pinned.
1033 * Should be at least nr_pages long. Or NULL, if caller
1034 * only intends to ensure the pages are faulted in.
1035 * @vmas: array of pointers to vmas corresponding to each page.
1036 * Or NULL if the caller does not require them.
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -07001037 * @locked: whether we're still with the mmap_lock held
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -07001038 *
Liu Xiangd2dfbe42019-11-30 17:49:53 -08001039 * Returns either number of pages pinned (which may be less than the
1040 * number requested), or an error. Details about the return value:
1041 *
1042 * -- If nr_pages is 0, returns 0.
1043 * -- If nr_pages is >0, but no pages were pinned, returns -errno.
1044 * -- If nr_pages is >0, and some pages were pinned, returns the number of
1045 * pages pinned. Again, this may be less than nr_pages.
Michal Hocko2d3a36a2020-06-03 16:03:25 -07001046 * -- 0 return value is possible when the fault would need to be retried.
Liu Xiangd2dfbe42019-11-30 17:49:53 -08001047 *
1048 * The caller is responsible for releasing returned @pages, via put_page().
1049 *
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -07001050 * @vmas are valid only as long as mmap_lock is held.
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -07001051 *
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -07001052 * Must be called with mmap_lock held. It may be released. See below.
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -07001053 *
1054 * __get_user_pages walks a process's page tables and takes a reference to
1055 * each struct page that each user address corresponds to at a given
1056 * instant. That is, it takes the page that would be accessed if a user
1057 * thread accesses the given user virtual address at that instant.
1058 *
1059 * This does not guarantee that the page exists in the user mappings when
1060 * __get_user_pages returns, and there may even be a completely different
1061 * page there in some cases (eg. if mmapped pagecache has been invalidated
1062 * and subsequently re faulted). However it does guarantee that the page
1063 * won't be freed completely. And mostly callers simply care that the page
1064 * contains data that was valid *at some point in time*. Typically, an IO
1065 * or similar operation cannot guarantee anything stronger anyway because
1066 * locks can't be held over the syscall boundary.
1067 *
1068 * If @gup_flags & FOLL_WRITE == 0, the page must not be written to. If
1069 * the page is written to, set_page_dirty (or set_page_dirty_lock, as
1070 * appropriate) must be called after the page is finished with, and
1071 * before put_page is called.
1072 *
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -07001073 * If @locked != NULL, *@locked will be set to 0 when mmap_lock is
Peter Xu4f6da932020-04-01 21:07:58 -07001074 * released by an up_read(). That can happen if @gup_flags does not
1075 * have FOLL_NOWAIT.
Paul Cassella9a95f3c2014-08-06 16:07:24 -07001076 *
Peter Xu4f6da932020-04-01 21:07:58 -07001077 * A caller using such a combination of @locked and @gup_flags
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -07001078 * must therefore hold the mmap_lock for reading only, and recognize
Paul Cassella9a95f3c2014-08-06 16:07:24 -07001079 * when it's been released. Otherwise, it must be held for either
1080 * reading or writing and will not be released.
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -07001081 *
1082 * In most cases, get_user_pages or get_user_pages_fast should be used
1083 * instead of __get_user_pages. __get_user_pages should be used only if
1084 * you need some special @gup_flags.
1085 */
Peter Xu64019a22020-08-11 18:39:01 -07001086static long __get_user_pages(struct mm_struct *mm,
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -07001087 unsigned long start, unsigned long nr_pages,
1088 unsigned int gup_flags, struct page **pages,
Peter Xu4f6da932020-04-01 21:07:58 -07001089 struct vm_area_struct **vmas, int *locked)
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -07001090{
Keith Buschdf06b372018-10-26 15:10:28 -07001091 long ret = 0, i = 0;
Kirill A. Shutemovfa5bb202014-06-04 16:08:13 -07001092 struct vm_area_struct *vma = NULL;
Keith Buschdf06b372018-10-26 15:10:28 -07001093 struct follow_page_context ctx = { NULL };
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -07001094
1095 if (!nr_pages)
1096 return 0;
1097
Andrey Konovalovf9652592019-09-25 16:48:34 -07001098 start = untagged_addr(start);
1099
John Hubbardeddb1c22020-01-30 22:12:54 -08001100 VM_BUG_ON(!!pages != !!(gup_flags & (FOLL_GET | FOLL_PIN)));
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -07001101
1102 /*
1103 * If FOLL_FORCE is set then do not force a full fault as the hinting
1104 * fault information is unrelated to the reference behaviour of a task
1105 * using the address space
1106 */
1107 if (!(gup_flags & FOLL_FORCE))
1108 gup_flags |= FOLL_NUMA;
1109
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -07001110 do {
Kirill A. Shutemovfa5bb202014-06-04 16:08:13 -07001111 struct page *page;
1112 unsigned int foll_flags = gup_flags;
1113 unsigned int page_increm;
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -07001114
Kirill A. Shutemovfa5bb202014-06-04 16:08:13 -07001115 /* first iteration or cross vma bound */
1116 if (!vma || start >= vma->vm_end) {
1117 vma = find_extend_vma(mm, start);
1118 if (!vma && in_gate_area(mm, start)) {
Kirill A. Shutemovfa5bb202014-06-04 16:08:13 -07001119 ret = get_gate_page(mm, start & PAGE_MASK,
1120 gup_flags, &vma,
1121 pages ? &pages[i] : NULL);
1122 if (ret)
John Hubbard08be37b2018-11-30 14:08:53 -08001123 goto out;
Keith Buschdf06b372018-10-26 15:10:28 -07001124 ctx.page_mask = 0;
Kirill A. Shutemovfa5bb202014-06-04 16:08:13 -07001125 goto next_page;
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -07001126 }
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -07001127
Jason Gunthorpe52650c82020-12-14 19:05:48 -08001128 if (!vma) {
Keith Buschdf06b372018-10-26 15:10:28 -07001129 ret = -EFAULT;
1130 goto out;
1131 }
Jason Gunthorpe52650c82020-12-14 19:05:48 -08001132 ret = check_vma_flags(vma, gup_flags);
1133 if (ret)
1134 goto out;
1135
Kirill A. Shutemovfa5bb202014-06-04 16:08:13 -07001136 if (is_vm_hugetlb_page(vma)) {
1137 i = follow_hugetlb_page(mm, vma, pages, vmas,
1138 &start, &nr_pages, i,
Peter Xua308c712020-08-21 19:49:57 -04001139 gup_flags, locked);
Peter Xuad415db2020-04-01 21:08:02 -07001140 if (locked && *locked == 0) {
1141 /*
1142 * We've got a VM_FAULT_RETRY
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -07001143 * and we've lost mmap_lock.
Peter Xuad415db2020-04-01 21:08:02 -07001144 * We must stop here.
1145 */
1146 BUG_ON(gup_flags & FOLL_NOWAIT);
Peter Xuad415db2020-04-01 21:08:02 -07001147 goto out;
1148 }
Kirill A. Shutemovfa5bb202014-06-04 16:08:13 -07001149 continue;
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -07001150 }
Kirill A. Shutemovfa5bb202014-06-04 16:08:13 -07001151 }
1152retry:
1153 /*
1154 * If we have a pending SIGKILL, don't keep faulting pages and
1155 * potentially allocating memory.
1156 */
Davidlohr Buesofa45f112019-01-03 15:28:55 -08001157 if (fatal_signal_pending(current)) {
Michal Hockod180870d2020-04-20 18:13:55 -07001158 ret = -EINTR;
Keith Buschdf06b372018-10-26 15:10:28 -07001159 goto out;
1160 }
Kirill A. Shutemovfa5bb202014-06-04 16:08:13 -07001161 cond_resched();
Keith Buschdf06b372018-10-26 15:10:28 -07001162
1163 page = follow_page_mask(vma, start, foll_flags, &ctx);
Kirill A. Shutemovfa5bb202014-06-04 16:08:13 -07001164 if (!page) {
Peter Xu64019a22020-08-11 18:39:01 -07001165 ret = faultin_page(vma, start, &foll_flags, locked);
Kirill A. Shutemovfa5bb202014-06-04 16:08:13 -07001166 switch (ret) {
1167 case 0:
1168 goto retry;
Keith Buschdf06b372018-10-26 15:10:28 -07001169 case -EBUSY:
1170 ret = 0;
Joe Perchese4a9bc52020-04-06 20:08:39 -07001171 fallthrough;
Kirill A. Shutemovfa5bb202014-06-04 16:08:13 -07001172 case -EFAULT:
1173 case -ENOMEM:
1174 case -EHWPOISON:
Keith Buschdf06b372018-10-26 15:10:28 -07001175 goto out;
Kirill A. Shutemovfa5bb202014-06-04 16:08:13 -07001176 case -ENOENT:
1177 goto next_page;
1178 }
1179 BUG();
Kirill A. Shutemov1027e442015-09-04 15:47:55 -07001180 } else if (PTR_ERR(page) == -EEXIST) {
1181 /*
1182 * Proper page table entry exists, but no corresponding
1183 * struct page.
1184 */
1185 goto next_page;
1186 } else if (IS_ERR(page)) {
Keith Buschdf06b372018-10-26 15:10:28 -07001187 ret = PTR_ERR(page);
1188 goto out;
Kirill A. Shutemov1027e442015-09-04 15:47:55 -07001189 }
Kirill A. Shutemovfa5bb202014-06-04 16:08:13 -07001190 if (pages) {
1191 pages[i] = page;
1192 flush_anon_page(vma, page, start);
1193 flush_dcache_page(page);
Keith Buschdf06b372018-10-26 15:10:28 -07001194 ctx.page_mask = 0;
Kirill A. Shutemovfa5bb202014-06-04 16:08:13 -07001195 }
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -07001196next_page:
Kirill A. Shutemovfa5bb202014-06-04 16:08:13 -07001197 if (vmas) {
1198 vmas[i] = vma;
Keith Buschdf06b372018-10-26 15:10:28 -07001199 ctx.page_mask = 0;
Kirill A. Shutemovfa5bb202014-06-04 16:08:13 -07001200 }
Keith Buschdf06b372018-10-26 15:10:28 -07001201 page_increm = 1 + (~(start >> PAGE_SHIFT) & ctx.page_mask);
Kirill A. Shutemovfa5bb202014-06-04 16:08:13 -07001202 if (page_increm > nr_pages)
1203 page_increm = nr_pages;
1204 i += page_increm;
1205 start += page_increm * PAGE_SIZE;
1206 nr_pages -= page_increm;
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -07001207 } while (nr_pages);
Keith Buschdf06b372018-10-26 15:10:28 -07001208out:
1209 if (ctx.pgmap)
1210 put_dev_pagemap(ctx.pgmap);
1211 return i ? i : ret;
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -07001212}
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -07001213
Tobias Klauser771ab432016-12-12 16:41:53 -08001214static bool vma_permits_fault(struct vm_area_struct *vma,
1215 unsigned int fault_flags)
Dave Hansend4925e02016-02-12 13:02:16 -08001216{
Dave Hansen1b2ee122016-02-12 13:02:21 -08001217 bool write = !!(fault_flags & FAULT_FLAG_WRITE);
1218 bool foreign = !!(fault_flags & FAULT_FLAG_REMOTE);
Dave Hansen33a709b2016-02-12 13:02:19 -08001219 vm_flags_t vm_flags = write ? VM_WRITE : VM_READ;
Dave Hansend4925e02016-02-12 13:02:16 -08001220
1221 if (!(vm_flags & vma->vm_flags))
1222 return false;
1223
Dave Hansen33a709b2016-02-12 13:02:19 -08001224 /*
1225 * The architecture might have a hardware protection
Dave Hansen1b2ee122016-02-12 13:02:21 -08001226 * mechanism other than read/write that can deny access.
Dave Hansend61172b2016-02-12 13:02:24 -08001227 *
1228 * gup always represents data access, not instruction
1229 * fetches, so execute=false here:
Dave Hansen33a709b2016-02-12 13:02:19 -08001230 */
Dave Hansend61172b2016-02-12 13:02:24 -08001231 if (!arch_vma_access_permitted(vma, write, false, foreign))
Dave Hansen33a709b2016-02-12 13:02:19 -08001232 return false;
1233
Dave Hansend4925e02016-02-12 13:02:16 -08001234 return true;
1235}
1236
Souptick Joarderadc8cb42020-06-01 21:48:24 -07001237/**
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -07001238 * fixup_user_fault() - manually resolve a user page fault
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -07001239 * @mm: mm_struct of target mm
1240 * @address: user address
1241 * @fault_flags:flags to pass down to handle_mm_fault()
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -07001242 * @unlocked: did we unlock the mmap_lock while retrying, maybe NULL if caller
Miles Chen548b6a12020-06-01 21:48:33 -07001243 * does not allow retry. If NULL, the caller must guarantee
1244 * that fault_flags does not contain FAULT_FLAG_ALLOW_RETRY.
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -07001245 *
1246 * This is meant to be called in the specific scenario where for locking reasons
1247 * we try to access user memory in atomic context (within a pagefault_disable()
1248 * section), this returns -EFAULT, and we want to resolve the user fault before
1249 * trying again.
1250 *
1251 * Typically this is meant to be used by the futex code.
1252 *
1253 * The main difference with get_user_pages() is that this function will
1254 * unconditionally call handle_mm_fault() which will in turn perform all the
1255 * necessary SW fixup of the dirty and young bits in the PTE, while
Dominik Dingel4a9e1cd2016-01-15 16:57:04 -08001256 * get_user_pages() only guarantees to update these in the struct page.
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -07001257 *
1258 * This is important for some architectures where those bits also gate the
1259 * access permission to the page because they are maintained in software. On
1260 * such architectures, gup() will not be enough to make a subsequent access
1261 * succeed.
1262 *
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -07001263 * This function will not return with an unlocked mmap_lock. So it has not the
1264 * same semantics wrt the @mm->mmap_lock as does filemap_fault().
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -07001265 */
Peter Xu64019a22020-08-11 18:39:01 -07001266int fixup_user_fault(struct mm_struct *mm,
Dominik Dingel4a9e1cd2016-01-15 16:57:04 -08001267 unsigned long address, unsigned int fault_flags,
1268 bool *unlocked)
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -07001269{
1270 struct vm_area_struct *vma;
Miaohe Lin8fed2f32021-09-02 14:53:33 -07001271 vm_fault_t ret;
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -07001272
Andrey Konovalovf9652592019-09-25 16:48:34 -07001273 address = untagged_addr(address);
1274
Dominik Dingel4a9e1cd2016-01-15 16:57:04 -08001275 if (unlocked)
Peter Xu71335f32020-04-01 21:08:53 -07001276 fault_flags |= FAULT_FLAG_ALLOW_RETRY | FAULT_FLAG_KILLABLE;
Dominik Dingel4a9e1cd2016-01-15 16:57:04 -08001277
1278retry:
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -07001279 vma = find_extend_vma(mm, address);
1280 if (!vma || address < vma->vm_start)
1281 return -EFAULT;
1282
Dave Hansend4925e02016-02-12 13:02:16 -08001283 if (!vma_permits_fault(vma, fault_flags))
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -07001284 return -EFAULT;
1285
Peter Xu475f4dfc2020-05-13 17:50:41 -07001286 if ((fault_flags & FAULT_FLAG_KILLABLE) &&
1287 fatal_signal_pending(current))
1288 return -EINTR;
1289
Peter Xubce617e2020-08-11 18:37:44 -07001290 ret = handle_mm_fault(vma, address, fault_flags, NULL);
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -07001291 if (ret & VM_FAULT_ERROR) {
James Morse9a291a72017-06-02 14:46:46 -07001292 int err = vm_fault_to_errno(ret, 0);
1293
1294 if (err)
1295 return err;
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -07001296 BUG();
1297 }
Dominik Dingel4a9e1cd2016-01-15 16:57:04 -08001298
1299 if (ret & VM_FAULT_RETRY) {
Michel Lespinassed8ed45c2020-06-08 21:33:25 -07001300 mmap_read_lock(mm);
Peter Xu475f4dfc2020-05-13 17:50:41 -07001301 *unlocked = true;
1302 fault_flags |= FAULT_FLAG_TRIED;
1303 goto retry;
Dominik Dingel4a9e1cd2016-01-15 16:57:04 -08001304 }
1305
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -07001306 return 0;
1307}
Paolo Bonziniadd6a0c2016-06-07 17:51:18 +02001308EXPORT_SYMBOL_GPL(fixup_user_fault);
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -07001309
Michal Hocko2d3a36a2020-06-03 16:03:25 -07001310/*
1311 * Please note that this function, unlike __get_user_pages will not
1312 * return 0 for nr_pages > 0 without FOLL_NOWAIT
1313 */
Peter Xu64019a22020-08-11 18:39:01 -07001314static __always_inline long __get_user_pages_locked(struct mm_struct *mm,
Andrea Arcangelif0818f42015-02-11 15:27:17 -08001315 unsigned long start,
1316 unsigned long nr_pages,
Andrea Arcangelif0818f42015-02-11 15:27:17 -08001317 struct page **pages,
1318 struct vm_area_struct **vmas,
Al Viroe7167122017-11-19 11:32:05 -05001319 int *locked,
Andrea Arcangeli0fd71a52015-02-11 15:27:20 -08001320 unsigned int flags)
Andrea Arcangelif0818f42015-02-11 15:27:17 -08001321{
Andrea Arcangelif0818f42015-02-11 15:27:17 -08001322 long ret, pages_done;
1323 bool lock_dropped;
1324
1325 if (locked) {
1326 /* if VM_FAULT_RETRY can be returned, vmas become invalid */
1327 BUG_ON(vmas);
1328 /* check caller initialized locked */
1329 BUG_ON(*locked != 1);
1330 }
1331
Andrea Arcangelia458b762021-06-28 19:36:40 -07001332 if (flags & FOLL_PIN)
1333 mm_set_has_pinned_flag(&mm->flags);
Peter Xu008cfe42020-09-25 18:25:57 -04001334
John Hubbardeddb1c22020-01-30 22:12:54 -08001335 /*
1336 * FOLL_PIN and FOLL_GET are mutually exclusive. Traditional behavior
1337 * is to set FOLL_GET if the caller wants pages[] filled in (but has
1338 * carelessly failed to specify FOLL_GET), so keep doing that, but only
1339 * for FOLL_GET, not for the newer FOLL_PIN.
1340 *
1341 * FOLL_PIN always expects pages to be non-null, but no need to assert
1342 * that here, as any failures will be obvious enough.
1343 */
1344 if (pages && !(flags & FOLL_PIN))
Andrea Arcangelif0818f42015-02-11 15:27:17 -08001345 flags |= FOLL_GET;
Andrea Arcangelif0818f42015-02-11 15:27:17 -08001346
1347 pages_done = 0;
1348 lock_dropped = false;
1349 for (;;) {
Peter Xu64019a22020-08-11 18:39:01 -07001350 ret = __get_user_pages(mm, start, nr_pages, flags, pages,
Andrea Arcangelif0818f42015-02-11 15:27:17 -08001351 vmas, locked);
1352 if (!locked)
1353 /* VM_FAULT_RETRY couldn't trigger, bypass */
1354 return ret;
1355
1356 /* VM_FAULT_RETRY cannot return errors */
1357 if (!*locked) {
1358 BUG_ON(ret < 0);
1359 BUG_ON(ret >= nr_pages);
1360 }
1361
Andrea Arcangelif0818f42015-02-11 15:27:17 -08001362 if (ret > 0) {
1363 nr_pages -= ret;
1364 pages_done += ret;
1365 if (!nr_pages)
1366 break;
1367 }
1368 if (*locked) {
Andrea Arcangeli96312e62018-03-09 15:51:06 -08001369 /*
1370 * VM_FAULT_RETRY didn't trigger or it was a
1371 * FOLL_NOWAIT.
1372 */
Andrea Arcangelif0818f42015-02-11 15:27:17 -08001373 if (!pages_done)
1374 pages_done = ret;
1375 break;
1376 }
Mike Rapoportdf172772019-05-31 22:30:33 -07001377 /*
1378 * VM_FAULT_RETRY triggered, so seek to the faulting offset.
1379 * For the prefault case (!pages) we only update counts.
1380 */
1381 if (likely(pages))
1382 pages += ret;
Andrea Arcangelif0818f42015-02-11 15:27:17 -08001383 start += ret << PAGE_SHIFT;
Peter Xu4426e942020-04-01 21:08:49 -07001384 lock_dropped = true;
Andrea Arcangelif0818f42015-02-11 15:27:17 -08001385
Peter Xu4426e942020-04-01 21:08:49 -07001386retry:
Andrea Arcangelif0818f42015-02-11 15:27:17 -08001387 /*
1388 * Repeat on the address that fired VM_FAULT_RETRY
Peter Xu4426e942020-04-01 21:08:49 -07001389 * with both FAULT_FLAG_ALLOW_RETRY and
1390 * FAULT_FLAG_TRIED. Note that GUP can be interrupted
1391 * by fatal signals, so we need to check it before we
1392 * start trying again otherwise it can loop forever.
Andrea Arcangelif0818f42015-02-11 15:27:17 -08001393 */
Peter Xu4426e942020-04-01 21:08:49 -07001394
Hillf Dantonae46d2a2020-04-08 11:59:24 -04001395 if (fatal_signal_pending(current)) {
1396 if (!pages_done)
1397 pages_done = -EINTR;
Peter Xu4426e942020-04-01 21:08:49 -07001398 break;
Hillf Dantonae46d2a2020-04-08 11:59:24 -04001399 }
Peter Xu4426e942020-04-01 21:08:49 -07001400
Michel Lespinassed8ed45c2020-06-08 21:33:25 -07001401 ret = mmap_read_lock_killable(mm);
Peter Xu71335f32020-04-01 21:08:53 -07001402 if (ret) {
1403 BUG_ON(ret > 0);
1404 if (!pages_done)
1405 pages_done = ret;
1406 break;
1407 }
Peter Xu4426e942020-04-01 21:08:49 -07001408
Peter Xuc7b6a562020-04-07 21:40:10 -04001409 *locked = 1;
Peter Xu64019a22020-08-11 18:39:01 -07001410 ret = __get_user_pages(mm, start, 1, flags | FOLL_TRIED,
Peter Xu4426e942020-04-01 21:08:49 -07001411 pages, NULL, locked);
1412 if (!*locked) {
1413 /* Continue to retry until we succeeded */
1414 BUG_ON(ret != 0);
1415 goto retry;
1416 }
Andrea Arcangelif0818f42015-02-11 15:27:17 -08001417 if (ret != 1) {
1418 BUG_ON(ret > 1);
1419 if (!pages_done)
1420 pages_done = ret;
1421 break;
1422 }
1423 nr_pages--;
1424 pages_done++;
1425 if (!nr_pages)
1426 break;
Mike Rapoportdf172772019-05-31 22:30:33 -07001427 if (likely(pages))
1428 pages++;
Andrea Arcangelif0818f42015-02-11 15:27:17 -08001429 start += PAGE_SIZE;
1430 }
Al Viroe7167122017-11-19 11:32:05 -05001431 if (lock_dropped && *locked) {
Andrea Arcangelif0818f42015-02-11 15:27:17 -08001432 /*
1433 * We must let the caller know we temporarily dropped the lock
1434 * and so the critical section protected by it was lost.
1435 */
Michel Lespinassed8ed45c2020-06-08 21:33:25 -07001436 mmap_read_unlock(mm);
Andrea Arcangelif0818f42015-02-11 15:27:17 -08001437 *locked = 0;
1438 }
1439 return pages_done;
1440}
1441
Christoph Hellwigd3649f62019-07-11 20:57:18 -07001442/**
1443 * populate_vma_page_range() - populate a range of pages in the vma.
1444 * @vma: target vma
1445 * @start: start address
1446 * @end: end address
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -07001447 * @locked: whether the mmap_lock is still held
Christoph Hellwigd3649f62019-07-11 20:57:18 -07001448 *
1449 * This takes care of mlocking the pages too if VM_LOCKED is set.
1450 *
Tang Yizhou0a36f7f2020-08-06 23:20:01 -07001451 * Return either number of pages pinned in the vma, or a negative error
1452 * code on error.
Christoph Hellwigd3649f62019-07-11 20:57:18 -07001453 *
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -07001454 * vma->vm_mm->mmap_lock must be held.
Christoph Hellwigd3649f62019-07-11 20:57:18 -07001455 *
Peter Xu4f6da932020-04-01 21:07:58 -07001456 * If @locked is NULL, it may be held for read or write and will
Christoph Hellwigd3649f62019-07-11 20:57:18 -07001457 * be unperturbed.
1458 *
Peter Xu4f6da932020-04-01 21:07:58 -07001459 * If @locked is non-NULL, it must held for read only and may be
1460 * released. If it's released, *@locked will be set to 0.
Christoph Hellwigd3649f62019-07-11 20:57:18 -07001461 */
1462long populate_vma_page_range(struct vm_area_struct *vma,
Peter Xu4f6da932020-04-01 21:07:58 -07001463 unsigned long start, unsigned long end, int *locked)
Christoph Hellwigd3649f62019-07-11 20:57:18 -07001464{
1465 struct mm_struct *mm = vma->vm_mm;
1466 unsigned long nr_pages = (end - start) / PAGE_SIZE;
1467 int gup_flags;
1468
Miaohe Linbe51eb12021-09-02 14:53:45 -07001469 VM_BUG_ON(!PAGE_ALIGNED(start));
1470 VM_BUG_ON(!PAGE_ALIGNED(end));
Christoph Hellwigd3649f62019-07-11 20:57:18 -07001471 VM_BUG_ON_VMA(start < vma->vm_start, vma);
1472 VM_BUG_ON_VMA(end > vma->vm_end, vma);
Michel Lespinasse42fc5412020-06-08 21:33:44 -07001473 mmap_assert_locked(mm);
Christoph Hellwigd3649f62019-07-11 20:57:18 -07001474
1475 gup_flags = FOLL_TOUCH | FOLL_POPULATE | FOLL_MLOCK;
1476 if (vma->vm_flags & VM_LOCKONFAULT)
1477 gup_flags &= ~FOLL_POPULATE;
1478 /*
1479 * We want to touch writable mappings with a write fault in order
1480 * to break COW, except for shared mappings because these don't COW
1481 * and we would not want to dirty them for nothing.
1482 */
1483 if ((vma->vm_flags & (VM_WRITE | VM_SHARED)) == VM_WRITE)
1484 gup_flags |= FOLL_WRITE;
1485
1486 /*
1487 * We want mlock to succeed for regions that have any permissions
1488 * other than PROT_NONE.
1489 */
Anshuman Khandual3122e802020-04-06 20:03:47 -07001490 if (vma_is_accessible(vma))
Christoph Hellwigd3649f62019-07-11 20:57:18 -07001491 gup_flags |= FOLL_FORCE;
1492
1493 /*
1494 * We made sure addr is within a VMA, so the following will
1495 * not result in a stack expansion that recurses back here.
1496 */
Peter Xu64019a22020-08-11 18:39:01 -07001497 return __get_user_pages(mm, start, nr_pages, gup_flags,
Peter Xu4f6da932020-04-01 21:07:58 -07001498 NULL, NULL, locked);
Christoph Hellwigd3649f62019-07-11 20:57:18 -07001499}
1500
1501/*
David Hildenbrand4ca9b3852021-06-30 18:52:28 -07001502 * faultin_vma_page_range() - populate (prefault) page tables inside the
1503 * given VMA range readable/writable
1504 *
1505 * This takes care of mlocking the pages, too, if VM_LOCKED is set.
1506 *
1507 * @vma: target vma
1508 * @start: start address
1509 * @end: end address
1510 * @write: whether to prefault readable or writable
1511 * @locked: whether the mmap_lock is still held
1512 *
1513 * Returns either number of processed pages in the vma, or a negative error
1514 * code on error (see __get_user_pages()).
1515 *
1516 * vma->vm_mm->mmap_lock must be held. The range must be page-aligned and
1517 * covered by the VMA.
1518 *
1519 * If @locked is NULL, it may be held for read or write and will be unperturbed.
1520 *
1521 * If @locked is non-NULL, it must held for read only and may be released. If
1522 * it's released, *@locked will be set to 0.
1523 */
1524long faultin_vma_page_range(struct vm_area_struct *vma, unsigned long start,
1525 unsigned long end, bool write, int *locked)
1526{
1527 struct mm_struct *mm = vma->vm_mm;
1528 unsigned long nr_pages = (end - start) / PAGE_SIZE;
1529 int gup_flags;
1530
1531 VM_BUG_ON(!PAGE_ALIGNED(start));
1532 VM_BUG_ON(!PAGE_ALIGNED(end));
1533 VM_BUG_ON_VMA(start < vma->vm_start, vma);
1534 VM_BUG_ON_VMA(end > vma->vm_end, vma);
1535 mmap_assert_locked(mm);
1536
1537 /*
1538 * FOLL_TOUCH: Mark page accessed and thereby young; will also mark
1539 * the page dirty with FOLL_WRITE -- which doesn't make a
1540 * difference with !FOLL_FORCE, because the page is writable
1541 * in the page table.
1542 * FOLL_HWPOISON: Return -EHWPOISON instead of -EFAULT when we hit
1543 * a poisoned page.
1544 * FOLL_POPULATE: Always populate memory with VM_LOCKONFAULT.
1545 * !FOLL_FORCE: Require proper access permissions.
1546 */
1547 gup_flags = FOLL_TOUCH | FOLL_POPULATE | FOLL_MLOCK | FOLL_HWPOISON;
1548 if (write)
1549 gup_flags |= FOLL_WRITE;
1550
1551 /*
David Hildenbrandeb2faa52021-08-13 16:54:37 -07001552 * We want to report -EINVAL instead of -EFAULT for any permission
1553 * problems or incompatible mappings.
David Hildenbrand4ca9b3852021-06-30 18:52:28 -07001554 */
David Hildenbrandeb2faa52021-08-13 16:54:37 -07001555 if (check_vma_flags(vma, gup_flags))
1556 return -EINVAL;
1557
David Hildenbrand4ca9b3852021-06-30 18:52:28 -07001558 return __get_user_pages(mm, start, nr_pages, gup_flags,
1559 NULL, NULL, locked);
1560}
1561
1562/*
Christoph Hellwigd3649f62019-07-11 20:57:18 -07001563 * __mm_populate - populate and/or mlock pages within a range of address space.
1564 *
1565 * This is used to implement mlock() and the MAP_POPULATE / MAP_LOCKED mmap
1566 * flags. VMAs must be already marked with the desired vm_flags, and
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -07001567 * mmap_lock must not be held.
Christoph Hellwigd3649f62019-07-11 20:57:18 -07001568 */
1569int __mm_populate(unsigned long start, unsigned long len, int ignore_errors)
1570{
1571 struct mm_struct *mm = current->mm;
1572 unsigned long end, nstart, nend;
1573 struct vm_area_struct *vma = NULL;
1574 int locked = 0;
1575 long ret = 0;
1576
1577 end = start + len;
1578
1579 for (nstart = start; nstart < end; nstart = nend) {
1580 /*
1581 * We want to fault in pages for [nstart; end) address range.
1582 * Find first corresponding VMA.
1583 */
1584 if (!locked) {
1585 locked = 1;
Michel Lespinassed8ed45c2020-06-08 21:33:25 -07001586 mmap_read_lock(mm);
Christoph Hellwigd3649f62019-07-11 20:57:18 -07001587 vma = find_vma(mm, nstart);
1588 } else if (nstart >= vma->vm_end)
1589 vma = vma->vm_next;
1590 if (!vma || vma->vm_start >= end)
1591 break;
1592 /*
1593 * Set [nstart; nend) to intersection of desired address
1594 * range with the first VMA. Also, skip undesirable VMA types.
1595 */
1596 nend = min(end, vma->vm_end);
1597 if (vma->vm_flags & (VM_IO | VM_PFNMAP))
1598 continue;
1599 if (nstart < vma->vm_start)
1600 nstart = vma->vm_start;
1601 /*
1602 * Now fault in a range of pages. populate_vma_page_range()
1603 * double checks the vma flags, so that it won't mlock pages
1604 * if the vma was already munlocked.
1605 */
1606 ret = populate_vma_page_range(vma, nstart, nend, &locked);
1607 if (ret < 0) {
1608 if (ignore_errors) {
1609 ret = 0;
1610 continue; /* continue at next VMA */
1611 }
1612 break;
1613 }
1614 nend = nstart + ret * PAGE_SIZE;
1615 ret = 0;
1616 }
1617 if (locked)
Michel Lespinassed8ed45c2020-06-08 21:33:25 -07001618 mmap_read_unlock(mm);
Christoph Hellwigd3649f62019-07-11 20:57:18 -07001619 return ret; /* 0 or negative error code */
1620}
Christoph Hellwig050a9ad2019-07-11 20:57:21 -07001621#else /* CONFIG_MMU */
Peter Xu64019a22020-08-11 18:39:01 -07001622static long __get_user_pages_locked(struct mm_struct *mm, unsigned long start,
Christoph Hellwig050a9ad2019-07-11 20:57:21 -07001623 unsigned long nr_pages, struct page **pages,
1624 struct vm_area_struct **vmas, int *locked,
1625 unsigned int foll_flags)
1626{
1627 struct vm_area_struct *vma;
1628 unsigned long vm_flags;
Pavel Tatashin24dc20c2021-05-04 18:39:15 -07001629 long i;
Christoph Hellwig050a9ad2019-07-11 20:57:21 -07001630
1631 /* calculate required read or write permissions.
1632 * If FOLL_FORCE is set, we only require the "MAY" flags.
1633 */
1634 vm_flags = (foll_flags & FOLL_WRITE) ?
1635 (VM_WRITE | VM_MAYWRITE) : (VM_READ | VM_MAYREAD);
1636 vm_flags &= (foll_flags & FOLL_FORCE) ?
1637 (VM_MAYREAD | VM_MAYWRITE) : (VM_READ | VM_WRITE);
1638
1639 for (i = 0; i < nr_pages; i++) {
1640 vma = find_vma(mm, start);
1641 if (!vma)
1642 goto finish_or_fault;
1643
1644 /* protect what we can, including chardevs */
1645 if ((vma->vm_flags & (VM_IO | VM_PFNMAP)) ||
1646 !(vm_flags & vma->vm_flags))
1647 goto finish_or_fault;
1648
1649 if (pages) {
1650 pages[i] = virt_to_page(start);
1651 if (pages[i])
1652 get_page(pages[i]);
1653 }
1654 if (vmas)
1655 vmas[i] = vma;
1656 start = (start + PAGE_SIZE) & PAGE_MASK;
1657 }
1658
1659 return i;
1660
1661finish_or_fault:
1662 return i ? : -EFAULT;
1663}
1664#endif /* !CONFIG_MMU */
Christoph Hellwigd3649f62019-07-11 20:57:18 -07001665
Jann Horn8f942ee2020-10-15 20:12:40 -07001666/**
Andreas Gruenbacherbb523b42021-08-02 13:44:20 +02001667 * fault_in_writeable - fault in userspace address range for writing
1668 * @uaddr: start of address range
1669 * @size: size of address range
1670 *
1671 * Returns the number of bytes not faulted in (like copy_to_user() and
1672 * copy_from_user()).
1673 */
1674size_t fault_in_writeable(char __user *uaddr, size_t size)
1675{
1676 char __user *start = uaddr, *end;
1677
1678 if (unlikely(size == 0))
1679 return 0;
Christophe Leroy677b2a82022-01-14 14:05:13 -08001680 if (!user_write_access_begin(uaddr, size))
1681 return size;
Andreas Gruenbacherbb523b42021-08-02 13:44:20 +02001682 if (!PAGE_ALIGNED(uaddr)) {
Christophe Leroy677b2a82022-01-14 14:05:13 -08001683 unsafe_put_user(0, uaddr, out);
Andreas Gruenbacherbb523b42021-08-02 13:44:20 +02001684 uaddr = (char __user *)PAGE_ALIGN((unsigned long)uaddr);
1685 }
1686 end = (char __user *)PAGE_ALIGN((unsigned long)start + size);
1687 if (unlikely(end < start))
1688 end = NULL;
1689 while (uaddr != end) {
Christophe Leroy677b2a82022-01-14 14:05:13 -08001690 unsafe_put_user(0, uaddr, out);
Andreas Gruenbacherbb523b42021-08-02 13:44:20 +02001691 uaddr += PAGE_SIZE;
1692 }
1693
1694out:
Christophe Leroy677b2a82022-01-14 14:05:13 -08001695 user_write_access_end();
Andreas Gruenbacherbb523b42021-08-02 13:44:20 +02001696 if (size > uaddr - start)
1697 return size - (uaddr - start);
1698 return 0;
1699}
1700EXPORT_SYMBOL(fault_in_writeable);
1701
Andreas Gruenbachercdd591f2021-07-05 17:26:28 +02001702/*
1703 * fault_in_safe_writeable - fault in an address range for writing
1704 * @uaddr: start of address range
1705 * @size: length of address range
1706 *
1707 * Faults in an address range using get_user_pages, i.e., without triggering
1708 * hardware page faults. This is primarily useful when we already know that
1709 * some or all of the pages in the address range aren't in memory.
1710 *
1711 * Other than fault_in_writeable(), this function is non-destructive.
1712 *
1713 * Note that we don't pin or otherwise hold the pages referenced that we fault
1714 * in. There's no guarantee that they'll stay in memory for any duration of
1715 * time.
1716 *
1717 * Returns the number of bytes not faulted in, like copy_to_user() and
1718 * copy_from_user().
1719 */
1720size_t fault_in_safe_writeable(const char __user *uaddr, size_t size)
1721{
1722 unsigned long start = (unsigned long)untagged_addr(uaddr);
1723 unsigned long end, nstart, nend;
1724 struct mm_struct *mm = current->mm;
1725 struct vm_area_struct *vma = NULL;
1726 int locked = 0;
1727
1728 nstart = start & PAGE_MASK;
1729 end = PAGE_ALIGN(start + size);
1730 if (end < nstart)
1731 end = 0;
1732 for (; nstart != end; nstart = nend) {
1733 unsigned long nr_pages;
1734 long ret;
1735
1736 if (!locked) {
1737 locked = 1;
1738 mmap_read_lock(mm);
1739 vma = find_vma(mm, nstart);
1740 } else if (nstart >= vma->vm_end)
1741 vma = vma->vm_next;
1742 if (!vma || vma->vm_start >= end)
1743 break;
1744 nend = end ? min(end, vma->vm_end) : vma->vm_end;
1745 if (vma->vm_flags & (VM_IO | VM_PFNMAP))
1746 continue;
1747 if (nstart < vma->vm_start)
1748 nstart = vma->vm_start;
1749 nr_pages = (nend - nstart) / PAGE_SIZE;
1750 ret = __get_user_pages_locked(mm, nstart, nr_pages,
1751 NULL, NULL, &locked,
1752 FOLL_TOUCH | FOLL_WRITE);
1753 if (ret <= 0)
1754 break;
1755 nend = nstart + ret * PAGE_SIZE;
1756 }
1757 if (locked)
1758 mmap_read_unlock(mm);
1759 if (nstart == end)
1760 return 0;
1761 return size - min_t(size_t, nstart - start, size);
1762}
1763EXPORT_SYMBOL(fault_in_safe_writeable);
1764
Andreas Gruenbacherbb523b42021-08-02 13:44:20 +02001765/**
1766 * fault_in_readable - fault in userspace address range for reading
1767 * @uaddr: start of user address range
1768 * @size: size of user address range
1769 *
1770 * Returns the number of bytes not faulted in (like copy_to_user() and
1771 * copy_from_user()).
1772 */
1773size_t fault_in_readable(const char __user *uaddr, size_t size)
1774{
1775 const char __user *start = uaddr, *end;
1776 volatile char c;
1777
1778 if (unlikely(size == 0))
1779 return 0;
Christophe Leroy677b2a82022-01-14 14:05:13 -08001780 if (!user_read_access_begin(uaddr, size))
1781 return size;
Andreas Gruenbacherbb523b42021-08-02 13:44:20 +02001782 if (!PAGE_ALIGNED(uaddr)) {
Christophe Leroy677b2a82022-01-14 14:05:13 -08001783 unsafe_get_user(c, uaddr, out);
Andreas Gruenbacherbb523b42021-08-02 13:44:20 +02001784 uaddr = (const char __user *)PAGE_ALIGN((unsigned long)uaddr);
1785 }
1786 end = (const char __user *)PAGE_ALIGN((unsigned long)start + size);
1787 if (unlikely(end < start))
1788 end = NULL;
1789 while (uaddr != end) {
Christophe Leroy677b2a82022-01-14 14:05:13 -08001790 unsafe_get_user(c, uaddr, out);
Andreas Gruenbacherbb523b42021-08-02 13:44:20 +02001791 uaddr += PAGE_SIZE;
1792 }
1793
1794out:
Christophe Leroy677b2a82022-01-14 14:05:13 -08001795 user_read_access_end();
Andreas Gruenbacherbb523b42021-08-02 13:44:20 +02001796 (void)c;
1797 if (size > uaddr - start)
1798 return size - (uaddr - start);
1799 return 0;
1800}
1801EXPORT_SYMBOL(fault_in_readable);
1802
1803/**
Jann Horn8f942ee2020-10-15 20:12:40 -07001804 * get_dump_page() - pin user page in memory while writing it to core dump
1805 * @addr: user address
1806 *
1807 * Returns struct page pointer of user page pinned for dump,
1808 * to be freed afterwards by put_page().
1809 *
1810 * Returns NULL on any kind of failure - a hole must then be inserted into
1811 * the corefile, to preserve alignment with its headers; and also returns
1812 * NULL wherever the ZERO_PAGE, or an anonymous pte_none, has been found -
Ingo Molnarf0953a12021-05-06 18:06:47 -07001813 * allowing a hole to be left in the corefile to save disk space.
Jann Horn8f942ee2020-10-15 20:12:40 -07001814 *
Jann Horn7f3bfab2020-10-15 20:12:57 -07001815 * Called without mmap_lock (takes and releases the mmap_lock by itself).
Jann Horn8f942ee2020-10-15 20:12:40 -07001816 */
1817#ifdef CONFIG_ELF_CORE
1818struct page *get_dump_page(unsigned long addr)
1819{
Jann Horn7f3bfab2020-10-15 20:12:57 -07001820 struct mm_struct *mm = current->mm;
Jann Horn8f942ee2020-10-15 20:12:40 -07001821 struct page *page;
Jann Horn7f3bfab2020-10-15 20:12:57 -07001822 int locked = 1;
1823 int ret;
Jann Horn8f942ee2020-10-15 20:12:40 -07001824
Jann Horn7f3bfab2020-10-15 20:12:57 -07001825 if (mmap_read_lock_killable(mm))
Jann Horn8f942ee2020-10-15 20:12:40 -07001826 return NULL;
Jann Horn7f3bfab2020-10-15 20:12:57 -07001827 ret = __get_user_pages_locked(mm, addr, 1, &page, NULL, &locked,
1828 FOLL_FORCE | FOLL_DUMP | FOLL_GET);
1829 if (locked)
1830 mmap_read_unlock(mm);
1831 return (ret == 1) ? page : NULL;
Jann Horn8f942ee2020-10-15 20:12:40 -07001832}
1833#endif /* CONFIG_ELF_CORE */
1834
Pavel Tatashind1e153f2021-05-04 18:39:08 -07001835#ifdef CONFIG_MIGRATION
Pavel Tatashinf68749e2021-05-04 18:39:19 -07001836/*
1837 * Check whether all pages are pinnable, if so return number of pages. If some
1838 * pages are not pinnable, migrate them, and unpin all pages. Return zero if
1839 * pages were migrated, or if some pages were not successfully isolated.
1840 * Return negative error if migration fails.
1841 */
1842static long check_and_migrate_movable_pages(unsigned long nr_pages,
Pavel Tatashind1e153f2021-05-04 18:39:08 -07001843 struct page **pages,
Pavel Tatashind1e153f2021-05-04 18:39:08 -07001844 unsigned int gup_flags)
Aneesh Kumar K.V9a4e9f32019-03-05 15:47:44 -08001845{
Pavel Tatashinf68749e2021-05-04 18:39:19 -07001846 unsigned long i;
1847 unsigned long isolation_error_count = 0;
1848 bool drain_allow = true;
Pavel Tatashind1e153f2021-05-04 18:39:08 -07001849 LIST_HEAD(movable_page_list);
Pavel Tatashinf68749e2021-05-04 18:39:19 -07001850 long ret = 0;
1851 struct page *prev_head = NULL;
1852 struct page *head;
Joonsoo Kimed03d922020-08-11 18:37:41 -07001853 struct migration_target_control mtc = {
1854 .nid = NUMA_NO_NODE,
Pavel Tatashinc991ffe2021-05-04 18:38:38 -07001855 .gfp_mask = GFP_USER | __GFP_NOWARN,
Joonsoo Kimed03d922020-08-11 18:37:41 -07001856 };
Aneesh Kumar K.V9a4e9f32019-03-05 15:47:44 -08001857
Pavel Tatashin83c02c22021-05-04 18:38:42 -07001858 for (i = 0; i < nr_pages; i++) {
1859 head = compound_head(pages[i]);
1860 if (head == prev_head)
1861 continue;
1862 prev_head = head;
Aneesh Kumar K.V9a4e9f32019-03-05 15:47:44 -08001863 /*
Pavel Tatashind1e153f2021-05-04 18:39:08 -07001864 * If we get a movable page, since we are going to be pinning
1865 * these entries, try to move them out if possible.
Aneesh Kumar K.V9a4e9f32019-03-05 15:47:44 -08001866 */
Pavel Tatashind1e153f2021-05-04 18:39:08 -07001867 if (!is_pinnable_page(head)) {
Pavel Tatashin6e7f34e2021-05-04 18:38:49 -07001868 if (PageHuge(head)) {
Pavel Tatashind1e153f2021-05-04 18:39:08 -07001869 if (!isolate_huge_page(head, &movable_page_list))
Pavel Tatashin6e7f34e2021-05-04 18:38:49 -07001870 isolation_error_count++;
1871 } else {
Aneesh Kumar K.V9a4e9f32019-03-05 15:47:44 -08001872 if (!PageLRU(head) && drain_allow) {
1873 lru_add_drain_all();
1874 drain_allow = false;
1875 }
1876
Pavel Tatashin6e7f34e2021-05-04 18:38:49 -07001877 if (isolate_lru_page(head)) {
1878 isolation_error_count++;
1879 continue;
Aneesh Kumar K.V9a4e9f32019-03-05 15:47:44 -08001880 }
Pavel Tatashind1e153f2021-05-04 18:39:08 -07001881 list_add_tail(&head->lru, &movable_page_list);
Pavel Tatashin6e7f34e2021-05-04 18:38:49 -07001882 mod_node_page_state(page_pgdat(head),
1883 NR_ISOLATED_ANON +
1884 page_is_file_lru(head),
1885 thp_nr_pages(head));
Aneesh Kumar K.V9a4e9f32019-03-05 15:47:44 -08001886 }
1887 }
1888 }
1889
Pavel Tatashin6e7f34e2021-05-04 18:38:49 -07001890 /*
1891 * If list is empty, and no isolation errors, means that all pages are
1892 * in the correct zone.
1893 */
Pavel Tatashind1e153f2021-05-04 18:39:08 -07001894 if (list_empty(&movable_page_list) && !isolation_error_count)
Pavel Tatashinf68749e2021-05-04 18:39:19 -07001895 return nr_pages;
Pavel Tatashin6e7f34e2021-05-04 18:38:49 -07001896
Pavel Tatashinf68749e2021-05-04 18:39:19 -07001897 if (gup_flags & FOLL_PIN) {
1898 unpin_user_pages(pages, nr_pages);
1899 } else {
1900 for (i = 0; i < nr_pages; i++)
1901 put_page(pages[i]);
1902 }
Pavel Tatashind1e153f2021-05-04 18:39:08 -07001903 if (!list_empty(&movable_page_list)) {
Pavel Tatashind1e153f2021-05-04 18:39:08 -07001904 ret = migrate_pages(&movable_page_list, alloc_migration_target,
Pavel Tatashinf0f44632021-05-04 18:38:46 -07001905 NULL, (unsigned long)&mtc, MIGRATE_SYNC,
Yang Shi5ac95882021-09-02 14:59:13 -07001906 MR_LONGTERM_PIN, NULL);
Pavel Tatashinf68749e2021-05-04 18:39:19 -07001907 if (ret && !list_empty(&movable_page_list))
1908 putback_movable_pages(&movable_page_list);
Aneesh Kumar K.V9a4e9f32019-03-05 15:47:44 -08001909 }
1910
Pavel Tatashinf68749e2021-05-04 18:39:19 -07001911 return ret > 0 ? -ENOMEM : ret;
Aneesh Kumar K.V9a4e9f32019-03-05 15:47:44 -08001912}
1913#else
Pavel Tatashinf68749e2021-05-04 18:39:19 -07001914static long check_and_migrate_movable_pages(unsigned long nr_pages,
Pavel Tatashind1e153f2021-05-04 18:39:08 -07001915 struct page **pages,
Pavel Tatashind1e153f2021-05-04 18:39:08 -07001916 unsigned int gup_flags)
Aneesh Kumar K.V9a4e9f32019-03-05 15:47:44 -08001917{
1918 return nr_pages;
1919}
Pavel Tatashind1e153f2021-05-04 18:39:08 -07001920#endif /* CONFIG_MIGRATION */
Aneesh Kumar K.V9a4e9f32019-03-05 15:47:44 -08001921
Dan Williams2bb6d282017-11-29 16:10:35 -08001922/*
Ira Weiny932f4a62019-05-13 17:17:03 -07001923 * __gup_longterm_locked() is a wrapper for __get_user_pages_locked which
1924 * allows us to process the FOLL_LONGTERM flag.
Dan Williams2bb6d282017-11-29 16:10:35 -08001925 */
Peter Xu64019a22020-08-11 18:39:01 -07001926static long __gup_longterm_locked(struct mm_struct *mm,
Ira Weiny932f4a62019-05-13 17:17:03 -07001927 unsigned long start,
1928 unsigned long nr_pages,
1929 struct page **pages,
1930 struct vm_area_struct **vmas,
1931 unsigned int gup_flags)
Dan Williams2bb6d282017-11-29 16:10:35 -08001932{
Pavel Tatashinf68749e2021-05-04 18:39:19 -07001933 unsigned int flags;
Jason Gunthorpe52650c82020-12-14 19:05:48 -08001934 long rc;
Dan Williams2bb6d282017-11-29 16:10:35 -08001935
Pavel Tatashinf68749e2021-05-04 18:39:19 -07001936 if (!(gup_flags & FOLL_LONGTERM))
1937 return __get_user_pages_locked(mm, start, nr_pages, pages, vmas,
1938 NULL, gup_flags);
1939 flags = memalloc_pin_save();
1940 do {
1941 rc = __get_user_pages_locked(mm, start, nr_pages, pages, vmas,
1942 NULL, gup_flags);
1943 if (rc <= 0)
1944 break;
1945 rc = check_and_migrate_movable_pages(rc, pages, gup_flags);
1946 } while (!rc);
1947 memalloc_pin_restore(flags);
Dan Williams2bb6d282017-11-29 16:10:35 -08001948
Dan Williams2bb6d282017-11-29 16:10:35 -08001949 return rc;
1950}
Ira Weiny932f4a62019-05-13 17:17:03 -07001951
Barry Song447f3e42020-10-13 16:51:58 -07001952static bool is_valid_gup_flags(unsigned int gup_flags)
1953{
1954 /*
1955 * FOLL_PIN must only be set internally by the pin_user_pages*() APIs,
1956 * never directly by the caller, so enforce that with an assertion:
1957 */
1958 if (WARN_ON_ONCE(gup_flags & FOLL_PIN))
1959 return false;
1960 /*
1961 * FOLL_PIN is a prerequisite to FOLL_LONGTERM. Another way of saying
1962 * that is, FOLL_LONGTERM is a specific case, more restrictive case of
1963 * FOLL_PIN.
1964 */
1965 if (WARN_ON_ONCE(gup_flags & FOLL_LONGTERM))
1966 return false;
1967
1968 return true;
1969}
1970
John Hubbard22bf29b2020-04-01 21:05:10 -07001971#ifdef CONFIG_MMU
Peter Xu64019a22020-08-11 18:39:01 -07001972static long __get_user_pages_remote(struct mm_struct *mm,
John Hubbard22bf29b2020-04-01 21:05:10 -07001973 unsigned long start, unsigned long nr_pages,
1974 unsigned int gup_flags, struct page **pages,
1975 struct vm_area_struct **vmas, int *locked)
1976{
1977 /*
1978 * Parts of FOLL_LONGTERM behavior are incompatible with
1979 * FAULT_FLAG_ALLOW_RETRY because of the FS DAX check requirement on
1980 * vmas. However, this only comes up if locked is set, and there are
1981 * callers that do request FOLL_LONGTERM, but do not set locked. So,
1982 * allow what we can.
1983 */
1984 if (gup_flags & FOLL_LONGTERM) {
1985 if (WARN_ON_ONCE(locked))
1986 return -EINVAL;
1987 /*
1988 * This will check the vmas (even if our vmas arg is NULL)
1989 * and return -ENOTSUPP if DAX isn't allowed in this case:
1990 */
Peter Xu64019a22020-08-11 18:39:01 -07001991 return __gup_longterm_locked(mm, start, nr_pages, pages,
John Hubbard22bf29b2020-04-01 21:05:10 -07001992 vmas, gup_flags | FOLL_TOUCH |
1993 FOLL_REMOTE);
1994 }
1995
Peter Xu64019a22020-08-11 18:39:01 -07001996 return __get_user_pages_locked(mm, start, nr_pages, pages, vmas,
John Hubbard22bf29b2020-04-01 21:05:10 -07001997 locked,
1998 gup_flags | FOLL_TOUCH | FOLL_REMOTE);
1999}
2000
Souptick Joarderadc8cb42020-06-01 21:48:24 -07002001/**
John Hubbardc4237f82020-01-30 22:12:36 -08002002 * get_user_pages_remote() - pin user pages in memory
John Hubbardc4237f82020-01-30 22:12:36 -08002003 * @mm: mm_struct of target mm
2004 * @start: starting user address
2005 * @nr_pages: number of pages from start to pin
2006 * @gup_flags: flags modifying lookup behaviour
2007 * @pages: array that receives pointers to the pages pinned.
2008 * Should be at least nr_pages long. Or NULL, if caller
2009 * only intends to ensure the pages are faulted in.
2010 * @vmas: array of pointers to vmas corresponding to each page.
2011 * Or NULL if the caller does not require them.
2012 * @locked: pointer to lock flag indicating whether lock is held and
2013 * subsequently whether VM_FAULT_RETRY functionality can be
2014 * utilised. Lock must initially be held.
2015 *
2016 * Returns either number of pages pinned (which may be less than the
2017 * number requested), or an error. Details about the return value:
2018 *
2019 * -- If nr_pages is 0, returns 0.
2020 * -- If nr_pages is >0, but no pages were pinned, returns -errno.
2021 * -- If nr_pages is >0, and some pages were pinned, returns the number of
2022 * pages pinned. Again, this may be less than nr_pages.
2023 *
2024 * The caller is responsible for releasing returned @pages, via put_page().
2025 *
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -07002026 * @vmas are valid only as long as mmap_lock is held.
John Hubbardc4237f82020-01-30 22:12:36 -08002027 *
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -07002028 * Must be called with mmap_lock held for read or write.
John Hubbardc4237f82020-01-30 22:12:36 -08002029 *
Souptick Joarderadc8cb42020-06-01 21:48:24 -07002030 * get_user_pages_remote walks a process's page tables and takes a reference
2031 * to each struct page that each user address corresponds to at a given
John Hubbardc4237f82020-01-30 22:12:36 -08002032 * instant. That is, it takes the page that would be accessed if a user
2033 * thread accesses the given user virtual address at that instant.
2034 *
2035 * This does not guarantee that the page exists in the user mappings when
Souptick Joarderadc8cb42020-06-01 21:48:24 -07002036 * get_user_pages_remote returns, and there may even be a completely different
John Hubbardc4237f82020-01-30 22:12:36 -08002037 * page there in some cases (eg. if mmapped pagecache has been invalidated
2038 * and subsequently re faulted). However it does guarantee that the page
2039 * won't be freed completely. And mostly callers simply care that the page
2040 * contains data that was valid *at some point in time*. Typically, an IO
2041 * or similar operation cannot guarantee anything stronger anyway because
2042 * locks can't be held over the syscall boundary.
2043 *
2044 * If gup_flags & FOLL_WRITE == 0, the page must not be written to. If the page
2045 * is written to, set_page_dirty (or set_page_dirty_lock, as appropriate) must
2046 * be called after the page is finished with, and before put_page is called.
2047 *
Souptick Joarderadc8cb42020-06-01 21:48:24 -07002048 * get_user_pages_remote is typically used for fewer-copy IO operations,
2049 * to get a handle on the memory by some means other than accesses
2050 * via the user virtual addresses. The pages may be submitted for
2051 * DMA to devices or accessed via their kernel linear mapping (via the
2052 * kmap APIs). Care should be taken to use the correct cache flushing APIs.
John Hubbardc4237f82020-01-30 22:12:36 -08002053 *
2054 * See also get_user_pages_fast, for performance critical applications.
2055 *
Souptick Joarderadc8cb42020-06-01 21:48:24 -07002056 * get_user_pages_remote should be phased out in favor of
John Hubbardc4237f82020-01-30 22:12:36 -08002057 * get_user_pages_locked|unlocked or get_user_pages_fast. Nothing
Souptick Joarderadc8cb42020-06-01 21:48:24 -07002058 * should use get_user_pages_remote because it cannot pass
John Hubbardc4237f82020-01-30 22:12:36 -08002059 * FAULT_FLAG_ALLOW_RETRY to handle_mm_fault.
2060 */
Peter Xu64019a22020-08-11 18:39:01 -07002061long get_user_pages_remote(struct mm_struct *mm,
John Hubbardc4237f82020-01-30 22:12:36 -08002062 unsigned long start, unsigned long nr_pages,
2063 unsigned int gup_flags, struct page **pages,
2064 struct vm_area_struct **vmas, int *locked)
2065{
Barry Song447f3e42020-10-13 16:51:58 -07002066 if (!is_valid_gup_flags(gup_flags))
John Hubbardeddb1c22020-01-30 22:12:54 -08002067 return -EINVAL;
2068
Peter Xu64019a22020-08-11 18:39:01 -07002069 return __get_user_pages_remote(mm, start, nr_pages, gup_flags,
John Hubbard22bf29b2020-04-01 21:05:10 -07002070 pages, vmas, locked);
John Hubbardc4237f82020-01-30 22:12:36 -08002071}
2072EXPORT_SYMBOL(get_user_pages_remote);
2073
John Hubbardeddb1c22020-01-30 22:12:54 -08002074#else /* CONFIG_MMU */
Peter Xu64019a22020-08-11 18:39:01 -07002075long get_user_pages_remote(struct mm_struct *mm,
John Hubbardeddb1c22020-01-30 22:12:54 -08002076 unsigned long start, unsigned long nr_pages,
2077 unsigned int gup_flags, struct page **pages,
2078 struct vm_area_struct **vmas, int *locked)
2079{
2080 return 0;
2081}
John Hubbard3faa52c2020-04-01 21:05:29 -07002082
Peter Xu64019a22020-08-11 18:39:01 -07002083static long __get_user_pages_remote(struct mm_struct *mm,
John Hubbard3faa52c2020-04-01 21:05:29 -07002084 unsigned long start, unsigned long nr_pages,
2085 unsigned int gup_flags, struct page **pages,
2086 struct vm_area_struct **vmas, int *locked)
2087{
2088 return 0;
2089}
John Hubbardeddb1c22020-01-30 22:12:54 -08002090#endif /* !CONFIG_MMU */
2091
Souptick Joarderadc8cb42020-06-01 21:48:24 -07002092/**
2093 * get_user_pages() - pin user pages in memory
2094 * @start: starting user address
2095 * @nr_pages: number of pages from start to pin
2096 * @gup_flags: flags modifying lookup behaviour
2097 * @pages: array that receives pointers to the pages pinned.
2098 * Should be at least nr_pages long. Or NULL, if caller
2099 * only intends to ensure the pages are faulted in.
2100 * @vmas: array of pointers to vmas corresponding to each page.
2101 * Or NULL if the caller does not require them.
2102 *
Peter Xu64019a22020-08-11 18:39:01 -07002103 * This is the same as get_user_pages_remote(), just with a less-flexible
2104 * calling convention where we assume that the mm being operated on belongs to
2105 * the current task, and doesn't allow passing of a locked parameter. We also
2106 * obviously don't pass FOLL_REMOTE in here.
Ira Weiny932f4a62019-05-13 17:17:03 -07002107 */
2108long get_user_pages(unsigned long start, unsigned long nr_pages,
2109 unsigned int gup_flags, struct page **pages,
2110 struct vm_area_struct **vmas)
2111{
Barry Song447f3e42020-10-13 16:51:58 -07002112 if (!is_valid_gup_flags(gup_flags))
John Hubbardeddb1c22020-01-30 22:12:54 -08002113 return -EINVAL;
2114
Peter Xu64019a22020-08-11 18:39:01 -07002115 return __gup_longterm_locked(current->mm, start, nr_pages,
Ira Weiny932f4a62019-05-13 17:17:03 -07002116 pages, vmas, gup_flags | FOLL_TOUCH);
2117}
2118EXPORT_SYMBOL(get_user_pages);
Dan Williams2bb6d282017-11-29 16:10:35 -08002119
Souptick Joarderadc8cb42020-06-01 21:48:24 -07002120/**
Mauro Carvalho Chehaba00cda32020-12-14 19:14:39 -08002121 * get_user_pages_locked() - variant of get_user_pages()
2122 *
2123 * @start: starting user address
2124 * @nr_pages: number of pages from start to pin
2125 * @gup_flags: flags modifying lookup behaviour
2126 * @pages: array that receives pointers to the pages pinned.
2127 * Should be at least nr_pages long. Or NULL, if caller
2128 * only intends to ensure the pages are faulted in.
2129 * @locked: pointer to lock flag indicating whether lock is held and
2130 * subsequently whether VM_FAULT_RETRY functionality can be
2131 * utilised. Lock must initially be held.
2132 *
2133 * It is suitable to replace the form:
Kirill A. Shutemovacc3c8d2015-04-14 15:44:45 -07002134 *
Michel Lespinasse3e4e28c2020-06-08 21:33:51 -07002135 * mmap_read_lock(mm);
Christoph Hellwigd3649f62019-07-11 20:57:18 -07002136 * do_something()
Peter Xu64019a22020-08-11 18:39:01 -07002137 * get_user_pages(mm, ..., pages, NULL);
Michel Lespinasse3e4e28c2020-06-08 21:33:51 -07002138 * mmap_read_unlock(mm);
Kirill A. Shutemovacc3c8d2015-04-14 15:44:45 -07002139 *
Christoph Hellwigd3649f62019-07-11 20:57:18 -07002140 * to:
Kirill A. Shutemovacc3c8d2015-04-14 15:44:45 -07002141 *
Christoph Hellwigd3649f62019-07-11 20:57:18 -07002142 * int locked = 1;
Michel Lespinasse3e4e28c2020-06-08 21:33:51 -07002143 * mmap_read_lock(mm);
Christoph Hellwigd3649f62019-07-11 20:57:18 -07002144 * do_something()
Peter Xu64019a22020-08-11 18:39:01 -07002145 * get_user_pages_locked(mm, ..., pages, &locked);
Christoph Hellwigd3649f62019-07-11 20:57:18 -07002146 * if (locked)
Michel Lespinasse3e4e28c2020-06-08 21:33:51 -07002147 * mmap_read_unlock(mm);
Souptick Joarderadc8cb42020-06-01 21:48:24 -07002148 *
Souptick Joarderadc8cb42020-06-01 21:48:24 -07002149 * We can leverage the VM_FAULT_RETRY functionality in the page fault
2150 * paths better by using either get_user_pages_locked() or
2151 * get_user_pages_unlocked().
2152 *
Kirill A. Shutemovacc3c8d2015-04-14 15:44:45 -07002153 */
Christoph Hellwigd3649f62019-07-11 20:57:18 -07002154long get_user_pages_locked(unsigned long start, unsigned long nr_pages,
2155 unsigned int gup_flags, struct page **pages,
2156 int *locked)
Kirill A. Shutemovacc3c8d2015-04-14 15:44:45 -07002157{
Kirill A. Shutemovacc3c8d2015-04-14 15:44:45 -07002158 /*
Christoph Hellwigd3649f62019-07-11 20:57:18 -07002159 * FIXME: Current FOLL_LONGTERM behavior is incompatible with
2160 * FAULT_FLAG_ALLOW_RETRY because of the FS DAX check requirement on
2161 * vmas. As there are no users of this flag in this call we simply
2162 * disallow this option for now.
Kirill A. Shutemovacc3c8d2015-04-14 15:44:45 -07002163 */
Christoph Hellwigd3649f62019-07-11 20:57:18 -07002164 if (WARN_ON_ONCE(gup_flags & FOLL_LONGTERM))
2165 return -EINVAL;
John Hubbard420c2092020-06-07 21:41:02 -07002166 /*
2167 * FOLL_PIN must only be set internally by the pin_user_pages*() APIs,
2168 * never directly by the caller, so enforce that:
2169 */
2170 if (WARN_ON_ONCE(gup_flags & FOLL_PIN))
2171 return -EINVAL;
Kirill A. Shutemovacc3c8d2015-04-14 15:44:45 -07002172
Peter Xu64019a22020-08-11 18:39:01 -07002173 return __get_user_pages_locked(current->mm, start, nr_pages,
Christoph Hellwigd3649f62019-07-11 20:57:18 -07002174 pages, NULL, locked,
2175 gup_flags | FOLL_TOUCH);
Kirill A. Shutemovacc3c8d2015-04-14 15:44:45 -07002176}
Christoph Hellwigd3649f62019-07-11 20:57:18 -07002177EXPORT_SYMBOL(get_user_pages_locked);
Kirill A. Shutemovacc3c8d2015-04-14 15:44:45 -07002178
2179/*
Christoph Hellwigd3649f62019-07-11 20:57:18 -07002180 * get_user_pages_unlocked() is suitable to replace the form:
Kirill A. Shutemovacc3c8d2015-04-14 15:44:45 -07002181 *
Michel Lespinasse3e4e28c2020-06-08 21:33:51 -07002182 * mmap_read_lock(mm);
Peter Xu64019a22020-08-11 18:39:01 -07002183 * get_user_pages(mm, ..., pages, NULL);
Michel Lespinasse3e4e28c2020-06-08 21:33:51 -07002184 * mmap_read_unlock(mm);
Christoph Hellwigd3649f62019-07-11 20:57:18 -07002185 *
2186 * with:
2187 *
Peter Xu64019a22020-08-11 18:39:01 -07002188 * get_user_pages_unlocked(mm, ..., pages);
Christoph Hellwigd3649f62019-07-11 20:57:18 -07002189 *
2190 * It is functionally equivalent to get_user_pages_fast so
2191 * get_user_pages_fast should be used instead if specific gup_flags
2192 * (e.g. FOLL_FORCE) are not required.
Kirill A. Shutemovacc3c8d2015-04-14 15:44:45 -07002193 */
Christoph Hellwigd3649f62019-07-11 20:57:18 -07002194long get_user_pages_unlocked(unsigned long start, unsigned long nr_pages,
2195 struct page **pages, unsigned int gup_flags)
Kirill A. Shutemovacc3c8d2015-04-14 15:44:45 -07002196{
2197 struct mm_struct *mm = current->mm;
Christoph Hellwigd3649f62019-07-11 20:57:18 -07002198 int locked = 1;
2199 long ret;
Kirill A. Shutemovacc3c8d2015-04-14 15:44:45 -07002200
Christoph Hellwigd3649f62019-07-11 20:57:18 -07002201 /*
2202 * FIXME: Current FOLL_LONGTERM behavior is incompatible with
2203 * FAULT_FLAG_ALLOW_RETRY because of the FS DAX check requirement on
2204 * vmas. As there are no users of this flag in this call we simply
2205 * disallow this option for now.
2206 */
2207 if (WARN_ON_ONCE(gup_flags & FOLL_LONGTERM))
2208 return -EINVAL;
Kirill A. Shutemovacc3c8d2015-04-14 15:44:45 -07002209
Michel Lespinassed8ed45c2020-06-08 21:33:25 -07002210 mmap_read_lock(mm);
Peter Xu64019a22020-08-11 18:39:01 -07002211 ret = __get_user_pages_locked(mm, start, nr_pages, pages, NULL,
Christoph Hellwigd3649f62019-07-11 20:57:18 -07002212 &locked, gup_flags | FOLL_TOUCH);
Kirill A. Shutemovacc3c8d2015-04-14 15:44:45 -07002213 if (locked)
Michel Lespinassed8ed45c2020-06-08 21:33:25 -07002214 mmap_read_unlock(mm);
Christoph Hellwigd3649f62019-07-11 20:57:18 -07002215 return ret;
Kirill A. Shutemovacc3c8d2015-04-14 15:44:45 -07002216}
Christoph Hellwigd3649f62019-07-11 20:57:18 -07002217EXPORT_SYMBOL(get_user_pages_unlocked);
Steve Capper2667f502014-10-09 15:29:14 -07002218
2219/*
Christoph Hellwig67a929e2019-07-11 20:57:14 -07002220 * Fast GUP
Steve Capper2667f502014-10-09 15:29:14 -07002221 *
2222 * get_user_pages_fast attempts to pin user pages by walking the page
2223 * tables directly and avoids taking locks. Thus the walker needs to be
2224 * protected from page table pages being freed from under it, and should
2225 * block any THP splits.
2226 *
2227 * One way to achieve this is to have the walker disable interrupts, and
2228 * rely on IPIs from the TLB flushing code blocking before the page table
2229 * pages are freed. This is unsuitable for architectures that do not need
2230 * to broadcast an IPI when invalidating TLBs.
2231 *
2232 * Another way to achieve this is to batch up page table containing pages
2233 * belonging to more than one mm_user, then rcu_sched a callback to free those
2234 * pages. Disabling interrupts will allow the fast_gup walker to both block
2235 * the rcu_sched callback, and an IPI that we broadcast for splitting THPs
2236 * (which is a relatively rare event). The code below adopts this strategy.
2237 *
2238 * Before activating this code, please be aware that the following assumptions
2239 * are currently made:
2240 *
Peter Zijlstraff2e6d722020-02-03 17:37:02 -08002241 * *) Either MMU_GATHER_RCU_TABLE_FREE is enabled, and tlb_remove_table() is used to
Kirill A. Shutemove5855132017-06-06 14:31:20 +03002242 * free pages containing page tables or TLB flushing requires IPI broadcast.
Steve Capper2667f502014-10-09 15:29:14 -07002243 *
Steve Capper2667f502014-10-09 15:29:14 -07002244 * *) ptes can be read atomically by the architecture.
2245 *
2246 * *) access_ok is sufficient to validate userspace address ranges.
2247 *
2248 * The last two assumptions can be relaxed by the addition of helper functions.
2249 *
2250 * This code is based heavily on the PowerPC implementation by Nick Piggin.
2251 */
Christoph Hellwig67a929e2019-07-11 20:57:14 -07002252#ifdef CONFIG_HAVE_FAST_GUP
John Hubbard3faa52c2020-04-01 21:05:29 -07002253
Guenter Roeck790c7362019-07-11 20:57:46 -07002254static void __maybe_unused undo_dev_pagemap(int *nr, int nr_start,
John Hubbard3b78d832020-04-01 21:05:22 -07002255 unsigned int flags,
Guenter Roeck790c7362019-07-11 20:57:46 -07002256 struct page **pages)
Kirill A. Shutemovb59f65f2017-03-16 18:26:53 +03002257{
2258 while ((*nr) - nr_start) {
2259 struct page *page = pages[--(*nr)];
2260
2261 ClearPageReferenced(page);
John Hubbard3faa52c2020-04-01 21:05:29 -07002262 if (flags & FOLL_PIN)
2263 unpin_user_page(page);
2264 else
2265 put_page(page);
Kirill A. Shutemovb59f65f2017-03-16 18:26:53 +03002266 }
2267}
2268
Laurent Dufour3010a5e2018-06-07 17:06:08 -07002269#ifdef CONFIG_ARCH_HAS_PTE_SPECIAL
Steve Capper2667f502014-10-09 15:29:14 -07002270static int gup_pte_range(pmd_t pmd, unsigned long addr, unsigned long end,
Ira Weinyb798bec2019-05-13 17:17:07 -07002271 unsigned int flags, struct page **pages, int *nr)
Steve Capper2667f502014-10-09 15:29:14 -07002272{
Kirill A. Shutemovb59f65f2017-03-16 18:26:53 +03002273 struct dev_pagemap *pgmap = NULL;
2274 int nr_start = *nr, ret = 0;
Steve Capper2667f502014-10-09 15:29:14 -07002275 pte_t *ptep, *ptem;
Steve Capper2667f502014-10-09 15:29:14 -07002276
2277 ptem = ptep = pte_offset_map(&pmd, addr);
2278 do {
Peter Zijlstra2a4a06d2020-11-13 11:41:40 +01002279 pte_t pte = ptep_get_lockless(ptep);
Kirill A. Shutemov7aef4172016-01-15 16:52:32 -08002280 struct page *head, *page;
Steve Capper2667f502014-10-09 15:29:14 -07002281
2282 /*
2283 * Similar to the PMD case below, NUMA hinting must take slow
Mel Gorman8a0516e2015-02-12 14:58:22 -08002284 * path using the pte_protnone check.
Steve Capper2667f502014-10-09 15:29:14 -07002285 */
Kirill A. Shutemove7884f82017-03-16 18:26:50 +03002286 if (pte_protnone(pte))
2287 goto pte_unmap;
2288
Ira Weinyb798bec2019-05-13 17:17:07 -07002289 if (!pte_access_permitted(pte, flags & FOLL_WRITE))
Kirill A. Shutemove7884f82017-03-16 18:26:50 +03002290 goto pte_unmap;
2291
Kirill A. Shutemovb59f65f2017-03-16 18:26:53 +03002292 if (pte_devmap(pte)) {
Ira Weiny7af75562019-05-13 17:17:14 -07002293 if (unlikely(flags & FOLL_LONGTERM))
2294 goto pte_unmap;
2295
Kirill A. Shutemovb59f65f2017-03-16 18:26:53 +03002296 pgmap = get_dev_pagemap(pte_pfn(pte), pgmap);
2297 if (unlikely(!pgmap)) {
John Hubbard3b78d832020-04-01 21:05:22 -07002298 undo_dev_pagemap(nr, nr_start, flags, pages);
Kirill A. Shutemovb59f65f2017-03-16 18:26:53 +03002299 goto pte_unmap;
2300 }
2301 } else if (pte_special(pte))
Steve Capper2667f502014-10-09 15:29:14 -07002302 goto pte_unmap;
2303
2304 VM_BUG_ON(!pfn_valid(pte_pfn(pte)));
2305 page = pte_page(pte);
2306
John Hubbard3faa52c2020-04-01 21:05:29 -07002307 head = try_grab_compound_head(page, 1, flags);
Linus Torvalds8fde12c2019-04-11 10:49:19 -07002308 if (!head)
Steve Capper2667f502014-10-09 15:29:14 -07002309 goto pte_unmap;
2310
Mike Rapoport1507f512021-07-07 18:08:03 -07002311 if (unlikely(page_is_secretmem(page))) {
2312 put_compound_head(head, 1, flags);
2313 goto pte_unmap;
2314 }
2315
Steve Capper2667f502014-10-09 15:29:14 -07002316 if (unlikely(pte_val(pte) != pte_val(*ptep))) {
John Hubbard3faa52c2020-04-01 21:05:29 -07002317 put_compound_head(head, 1, flags);
Steve Capper2667f502014-10-09 15:29:14 -07002318 goto pte_unmap;
2319 }
2320
Kirill A. Shutemov7aef4172016-01-15 16:52:32 -08002321 VM_BUG_ON_PAGE(compound_head(page) != head, page);
Kirill A. Shutemove9348052017-03-16 18:26:52 +03002322
Claudio Imbrendaf28d4362020-04-01 21:05:56 -07002323 /*
2324 * We need to make the page accessible if and only if we are
2325 * going to access its content (the FOLL_PIN case). Please
2326 * see Documentation/core-api/pin_user_pages.rst for
2327 * details.
2328 */
2329 if (flags & FOLL_PIN) {
2330 ret = arch_make_page_accessible(page);
2331 if (ret) {
2332 unpin_user_page(page);
2333 goto pte_unmap;
2334 }
2335 }
Kirill A. Shutemove9348052017-03-16 18:26:52 +03002336 SetPageReferenced(page);
Steve Capper2667f502014-10-09 15:29:14 -07002337 pages[*nr] = page;
2338 (*nr)++;
2339
2340 } while (ptep++, addr += PAGE_SIZE, addr != end);
2341
2342 ret = 1;
2343
2344pte_unmap:
Christoph Hellwig832d7aa2017-12-29 08:54:01 +01002345 if (pgmap)
2346 put_dev_pagemap(pgmap);
Steve Capper2667f502014-10-09 15:29:14 -07002347 pte_unmap(ptem);
2348 return ret;
2349}
2350#else
2351
2352/*
2353 * If we can't determine whether or not a pte is special, then fail immediately
2354 * for ptes. Note, we can still pin HugeTLB and THP as these are guaranteed not
2355 * to be special.
2356 *
2357 * For a futex to be placed on a THP tail page, get_futex_key requires a
Souptick Joarderdadbb612020-06-07 21:40:55 -07002358 * get_user_pages_fast_only implementation that can pin pages. Thus it's still
Steve Capper2667f502014-10-09 15:29:14 -07002359 * useful to have gup_huge_pmd even if we can't operate on ptes.
2360 */
2361static int gup_pte_range(pmd_t pmd, unsigned long addr, unsigned long end,
Ira Weinyb798bec2019-05-13 17:17:07 -07002362 unsigned int flags, struct page **pages, int *nr)
Steve Capper2667f502014-10-09 15:29:14 -07002363{
2364 return 0;
2365}
Laurent Dufour3010a5e2018-06-07 17:06:08 -07002366#endif /* CONFIG_ARCH_HAS_PTE_SPECIAL */
Steve Capper2667f502014-10-09 15:29:14 -07002367
Robin Murphy17596732019-07-16 16:30:47 -07002368#if defined(CONFIG_ARCH_HAS_PTE_DEVMAP) && defined(CONFIG_TRANSPARENT_HUGEPAGE)
Kirill A. Shutemovb59f65f2017-03-16 18:26:53 +03002369static int __gup_device_huge(unsigned long pfn, unsigned long addr,
John Hubbard86dfbed2020-04-01 21:05:14 -07002370 unsigned long end, unsigned int flags,
2371 struct page **pages, int *nr)
Kirill A. Shutemovb59f65f2017-03-16 18:26:53 +03002372{
2373 int nr_start = *nr;
2374 struct dev_pagemap *pgmap = NULL;
2375
2376 do {
2377 struct page *page = pfn_to_page(pfn);
2378
2379 pgmap = get_dev_pagemap(pfn, pgmap);
2380 if (unlikely(!pgmap)) {
John Hubbard3b78d832020-04-01 21:05:22 -07002381 undo_dev_pagemap(nr, nr_start, flags, pages);
Miaohe Lin6401c4e2021-09-02 14:53:42 -07002382 break;
Kirill A. Shutemovb59f65f2017-03-16 18:26:53 +03002383 }
2384 SetPageReferenced(page);
2385 pages[*nr] = page;
John Hubbard3faa52c2020-04-01 21:05:29 -07002386 if (unlikely(!try_grab_page(page, flags))) {
2387 undo_dev_pagemap(nr, nr_start, flags, pages);
Miaohe Lin6401c4e2021-09-02 14:53:42 -07002388 break;
John Hubbard3faa52c2020-04-01 21:05:29 -07002389 }
Kirill A. Shutemovb59f65f2017-03-16 18:26:53 +03002390 (*nr)++;
2391 pfn++;
2392 } while (addr += PAGE_SIZE, addr != end);
Christoph Hellwig832d7aa2017-12-29 08:54:01 +01002393
Miaohe Lin6401c4e2021-09-02 14:53:42 -07002394 put_dev_pagemap(pgmap);
John Hubbard20b7fee2021-11-05 13:37:16 -07002395 return addr == end;
Kirill A. Shutemovb59f65f2017-03-16 18:26:53 +03002396}
2397
Dan Williamsa9b6de72018-04-19 21:32:19 -07002398static int __gup_device_huge_pmd(pmd_t orig, pmd_t *pmdp, unsigned long addr,
John Hubbard86dfbed2020-04-01 21:05:14 -07002399 unsigned long end, unsigned int flags,
2400 struct page **pages, int *nr)
Kirill A. Shutemovb59f65f2017-03-16 18:26:53 +03002401{
2402 unsigned long fault_pfn;
Dan Williamsa9b6de72018-04-19 21:32:19 -07002403 int nr_start = *nr;
Kirill A. Shutemovb59f65f2017-03-16 18:26:53 +03002404
Dan Williamsa9b6de72018-04-19 21:32:19 -07002405 fault_pfn = pmd_pfn(orig) + ((addr & ~PMD_MASK) >> PAGE_SHIFT);
John Hubbard86dfbed2020-04-01 21:05:14 -07002406 if (!__gup_device_huge(fault_pfn, addr, end, flags, pages, nr))
Dan Williamsa9b6de72018-04-19 21:32:19 -07002407 return 0;
2408
2409 if (unlikely(pmd_val(orig) != pmd_val(*pmdp))) {
John Hubbard3b78d832020-04-01 21:05:22 -07002410 undo_dev_pagemap(nr, nr_start, flags, pages);
Dan Williamsa9b6de72018-04-19 21:32:19 -07002411 return 0;
2412 }
2413 return 1;
Kirill A. Shutemovb59f65f2017-03-16 18:26:53 +03002414}
2415
Dan Williamsa9b6de72018-04-19 21:32:19 -07002416static int __gup_device_huge_pud(pud_t orig, pud_t *pudp, unsigned long addr,
John Hubbard86dfbed2020-04-01 21:05:14 -07002417 unsigned long end, unsigned int flags,
2418 struct page **pages, int *nr)
Kirill A. Shutemovb59f65f2017-03-16 18:26:53 +03002419{
2420 unsigned long fault_pfn;
Dan Williamsa9b6de72018-04-19 21:32:19 -07002421 int nr_start = *nr;
Kirill A. Shutemovb59f65f2017-03-16 18:26:53 +03002422
Dan Williamsa9b6de72018-04-19 21:32:19 -07002423 fault_pfn = pud_pfn(orig) + ((addr & ~PUD_MASK) >> PAGE_SHIFT);
John Hubbard86dfbed2020-04-01 21:05:14 -07002424 if (!__gup_device_huge(fault_pfn, addr, end, flags, pages, nr))
Dan Williamsa9b6de72018-04-19 21:32:19 -07002425 return 0;
2426
2427 if (unlikely(pud_val(orig) != pud_val(*pudp))) {
John Hubbard3b78d832020-04-01 21:05:22 -07002428 undo_dev_pagemap(nr, nr_start, flags, pages);
Dan Williamsa9b6de72018-04-19 21:32:19 -07002429 return 0;
2430 }
2431 return 1;
Kirill A. Shutemovb59f65f2017-03-16 18:26:53 +03002432}
2433#else
Dan Williamsa9b6de72018-04-19 21:32:19 -07002434static int __gup_device_huge_pmd(pmd_t orig, pmd_t *pmdp, unsigned long addr,
John Hubbard86dfbed2020-04-01 21:05:14 -07002435 unsigned long end, unsigned int flags,
2436 struct page **pages, int *nr)
Kirill A. Shutemovb59f65f2017-03-16 18:26:53 +03002437{
2438 BUILD_BUG();
2439 return 0;
2440}
2441
Dan Williamsa9b6de72018-04-19 21:32:19 -07002442static int __gup_device_huge_pud(pud_t pud, pud_t *pudp, unsigned long addr,
John Hubbard86dfbed2020-04-01 21:05:14 -07002443 unsigned long end, unsigned int flags,
2444 struct page **pages, int *nr)
Kirill A. Shutemovb59f65f2017-03-16 18:26:53 +03002445{
2446 BUILD_BUG();
2447 return 0;
2448}
2449#endif
2450
John Hubbarda43e9822020-01-30 22:12:17 -08002451static int record_subpages(struct page *page, unsigned long addr,
2452 unsigned long end, struct page **pages)
2453{
2454 int nr;
2455
2456 for (nr = 0; addr != end; addr += PAGE_SIZE)
2457 pages[nr++] = page++;
2458
2459 return nr;
2460}
2461
Christoph Hellwigcbd34da2019-07-11 20:57:28 -07002462#ifdef CONFIG_ARCH_HAS_HUGEPD
2463static unsigned long hugepte_addr_end(unsigned long addr, unsigned long end,
2464 unsigned long sz)
2465{
2466 unsigned long __boundary = (addr + sz) & ~(sz-1);
2467 return (__boundary - 1 < end - 1) ? __boundary : end;
2468}
2469
2470static int gup_hugepte(pte_t *ptep, unsigned long sz, unsigned long addr,
John Hubbard0cd22af2019-10-18 20:19:53 -07002471 unsigned long end, unsigned int flags,
2472 struct page **pages, int *nr)
Christoph Hellwigcbd34da2019-07-11 20:57:28 -07002473{
2474 unsigned long pte_end;
2475 struct page *head, *page;
2476 pte_t pte;
2477 int refs;
2478
2479 pte_end = (addr + sz) & ~(sz-1);
2480 if (pte_end < end)
2481 end = pte_end;
2482
Christophe Leroy55ca2262020-06-15 12:57:57 +00002483 pte = huge_ptep_get(ptep);
Christoph Hellwigcbd34da2019-07-11 20:57:28 -07002484
John Hubbard0cd22af2019-10-18 20:19:53 -07002485 if (!pte_access_permitted(pte, flags & FOLL_WRITE))
Christoph Hellwigcbd34da2019-07-11 20:57:28 -07002486 return 0;
2487
2488 /* hugepages are never "special" */
2489 VM_BUG_ON(!pfn_valid(pte_pfn(pte)));
2490
Christoph Hellwigcbd34da2019-07-11 20:57:28 -07002491 head = pte_page(pte);
Christoph Hellwigcbd34da2019-07-11 20:57:28 -07002492 page = head + ((addr & (sz-1)) >> PAGE_SHIFT);
John Hubbarda43e9822020-01-30 22:12:17 -08002493 refs = record_subpages(page, addr, end, pages + *nr);
Christoph Hellwigcbd34da2019-07-11 20:57:28 -07002494
John Hubbard3faa52c2020-04-01 21:05:29 -07002495 head = try_grab_compound_head(head, refs, flags);
John Hubbarda43e9822020-01-30 22:12:17 -08002496 if (!head)
Christoph Hellwigcbd34da2019-07-11 20:57:28 -07002497 return 0;
Christoph Hellwigcbd34da2019-07-11 20:57:28 -07002498
2499 if (unlikely(pte_val(pte) != pte_val(*ptep))) {
John Hubbard3b78d832020-04-01 21:05:22 -07002500 put_compound_head(head, refs, flags);
Christoph Hellwigcbd34da2019-07-11 20:57:28 -07002501 return 0;
2502 }
2503
John Hubbarda43e9822020-01-30 22:12:17 -08002504 *nr += refs;
Christoph Hellwig520b4a42019-07-11 20:57:36 -07002505 SetPageReferenced(head);
Christoph Hellwigcbd34da2019-07-11 20:57:28 -07002506 return 1;
2507}
2508
2509static int gup_huge_pd(hugepd_t hugepd, unsigned long addr,
John Hubbard0cd22af2019-10-18 20:19:53 -07002510 unsigned int pdshift, unsigned long end, unsigned int flags,
Christoph Hellwigcbd34da2019-07-11 20:57:28 -07002511 struct page **pages, int *nr)
2512{
2513 pte_t *ptep;
2514 unsigned long sz = 1UL << hugepd_shift(hugepd);
2515 unsigned long next;
2516
2517 ptep = hugepte_offset(hugepd, addr, pdshift);
2518 do {
2519 next = hugepte_addr_end(addr, end, sz);
John Hubbard0cd22af2019-10-18 20:19:53 -07002520 if (!gup_hugepte(ptep, sz, addr, end, flags, pages, nr))
Christoph Hellwigcbd34da2019-07-11 20:57:28 -07002521 return 0;
2522 } while (ptep++, addr = next, addr != end);
2523
2524 return 1;
2525}
2526#else
2527static inline int gup_huge_pd(hugepd_t hugepd, unsigned long addr,
John Hubbard0cd22af2019-10-18 20:19:53 -07002528 unsigned int pdshift, unsigned long end, unsigned int flags,
Christoph Hellwigcbd34da2019-07-11 20:57:28 -07002529 struct page **pages, int *nr)
2530{
2531 return 0;
2532}
2533#endif /* CONFIG_ARCH_HAS_HUGEPD */
2534
Steve Capper2667f502014-10-09 15:29:14 -07002535static int gup_huge_pmd(pmd_t orig, pmd_t *pmdp, unsigned long addr,
John Hubbard0cd22af2019-10-18 20:19:53 -07002536 unsigned long end, unsigned int flags,
2537 struct page **pages, int *nr)
Steve Capper2667f502014-10-09 15:29:14 -07002538{
Kirill A. Shutemovddc58f22016-01-15 16:52:56 -08002539 struct page *head, *page;
Steve Capper2667f502014-10-09 15:29:14 -07002540 int refs;
2541
Ira Weinyb798bec2019-05-13 17:17:07 -07002542 if (!pmd_access_permitted(orig, flags & FOLL_WRITE))
Steve Capper2667f502014-10-09 15:29:14 -07002543 return 0;
2544
Ira Weiny7af75562019-05-13 17:17:14 -07002545 if (pmd_devmap(orig)) {
2546 if (unlikely(flags & FOLL_LONGTERM))
2547 return 0;
John Hubbard86dfbed2020-04-01 21:05:14 -07002548 return __gup_device_huge_pmd(orig, pmdp, addr, end, flags,
2549 pages, nr);
Ira Weiny7af75562019-05-13 17:17:14 -07002550 }
Kirill A. Shutemovb59f65f2017-03-16 18:26:53 +03002551
Punit Agrawald63206e2017-07-06 15:39:39 -07002552 page = pmd_page(orig) + ((addr & ~PMD_MASK) >> PAGE_SHIFT);
John Hubbarda43e9822020-01-30 22:12:17 -08002553 refs = record_subpages(page, addr, end, pages + *nr);
Steve Capper2667f502014-10-09 15:29:14 -07002554
John Hubbard3faa52c2020-04-01 21:05:29 -07002555 head = try_grab_compound_head(pmd_page(orig), refs, flags);
John Hubbarda43e9822020-01-30 22:12:17 -08002556 if (!head)
Steve Capper2667f502014-10-09 15:29:14 -07002557 return 0;
Steve Capper2667f502014-10-09 15:29:14 -07002558
2559 if (unlikely(pmd_val(orig) != pmd_val(*pmdp))) {
John Hubbard3b78d832020-04-01 21:05:22 -07002560 put_compound_head(head, refs, flags);
Steve Capper2667f502014-10-09 15:29:14 -07002561 return 0;
2562 }
2563
John Hubbarda43e9822020-01-30 22:12:17 -08002564 *nr += refs;
Kirill A. Shutemove9348052017-03-16 18:26:52 +03002565 SetPageReferenced(head);
Steve Capper2667f502014-10-09 15:29:14 -07002566 return 1;
2567}
2568
2569static int gup_huge_pud(pud_t orig, pud_t *pudp, unsigned long addr,
John Hubbard86dfbed2020-04-01 21:05:14 -07002570 unsigned long end, unsigned int flags,
2571 struct page **pages, int *nr)
Steve Capper2667f502014-10-09 15:29:14 -07002572{
Kirill A. Shutemovddc58f22016-01-15 16:52:56 -08002573 struct page *head, *page;
Steve Capper2667f502014-10-09 15:29:14 -07002574 int refs;
2575
Ira Weinyb798bec2019-05-13 17:17:07 -07002576 if (!pud_access_permitted(orig, flags & FOLL_WRITE))
Steve Capper2667f502014-10-09 15:29:14 -07002577 return 0;
2578
Ira Weiny7af75562019-05-13 17:17:14 -07002579 if (pud_devmap(orig)) {
2580 if (unlikely(flags & FOLL_LONGTERM))
2581 return 0;
John Hubbard86dfbed2020-04-01 21:05:14 -07002582 return __gup_device_huge_pud(orig, pudp, addr, end, flags,
2583 pages, nr);
Ira Weiny7af75562019-05-13 17:17:14 -07002584 }
Kirill A. Shutemovb59f65f2017-03-16 18:26:53 +03002585
Punit Agrawald63206e2017-07-06 15:39:39 -07002586 page = pud_page(orig) + ((addr & ~PUD_MASK) >> PAGE_SHIFT);
John Hubbarda43e9822020-01-30 22:12:17 -08002587 refs = record_subpages(page, addr, end, pages + *nr);
Steve Capper2667f502014-10-09 15:29:14 -07002588
John Hubbard3faa52c2020-04-01 21:05:29 -07002589 head = try_grab_compound_head(pud_page(orig), refs, flags);
John Hubbarda43e9822020-01-30 22:12:17 -08002590 if (!head)
Steve Capper2667f502014-10-09 15:29:14 -07002591 return 0;
Steve Capper2667f502014-10-09 15:29:14 -07002592
2593 if (unlikely(pud_val(orig) != pud_val(*pudp))) {
John Hubbard3b78d832020-04-01 21:05:22 -07002594 put_compound_head(head, refs, flags);
Steve Capper2667f502014-10-09 15:29:14 -07002595 return 0;
2596 }
2597
John Hubbarda43e9822020-01-30 22:12:17 -08002598 *nr += refs;
Kirill A. Shutemove9348052017-03-16 18:26:52 +03002599 SetPageReferenced(head);
Steve Capper2667f502014-10-09 15:29:14 -07002600 return 1;
2601}
2602
Aneesh Kumar K.Vf30c59e2014-11-05 21:57:40 +05302603static int gup_huge_pgd(pgd_t orig, pgd_t *pgdp, unsigned long addr,
Ira Weinyb798bec2019-05-13 17:17:07 -07002604 unsigned long end, unsigned int flags,
Aneesh Kumar K.Vf30c59e2014-11-05 21:57:40 +05302605 struct page **pages, int *nr)
2606{
2607 int refs;
Kirill A. Shutemovddc58f22016-01-15 16:52:56 -08002608 struct page *head, *page;
Aneesh Kumar K.Vf30c59e2014-11-05 21:57:40 +05302609
Ira Weinyb798bec2019-05-13 17:17:07 -07002610 if (!pgd_access_permitted(orig, flags & FOLL_WRITE))
Aneesh Kumar K.Vf30c59e2014-11-05 21:57:40 +05302611 return 0;
2612
Kirill A. Shutemovb59f65f2017-03-16 18:26:53 +03002613 BUILD_BUG_ON(pgd_devmap(orig));
John Hubbarda43e9822020-01-30 22:12:17 -08002614
Punit Agrawald63206e2017-07-06 15:39:39 -07002615 page = pgd_page(orig) + ((addr & ~PGDIR_MASK) >> PAGE_SHIFT);
John Hubbarda43e9822020-01-30 22:12:17 -08002616 refs = record_subpages(page, addr, end, pages + *nr);
Aneesh Kumar K.Vf30c59e2014-11-05 21:57:40 +05302617
John Hubbard3faa52c2020-04-01 21:05:29 -07002618 head = try_grab_compound_head(pgd_page(orig), refs, flags);
John Hubbarda43e9822020-01-30 22:12:17 -08002619 if (!head)
Aneesh Kumar K.Vf30c59e2014-11-05 21:57:40 +05302620 return 0;
Aneesh Kumar K.Vf30c59e2014-11-05 21:57:40 +05302621
2622 if (unlikely(pgd_val(orig) != pgd_val(*pgdp))) {
John Hubbard3b78d832020-04-01 21:05:22 -07002623 put_compound_head(head, refs, flags);
Aneesh Kumar K.Vf30c59e2014-11-05 21:57:40 +05302624 return 0;
2625 }
2626
John Hubbarda43e9822020-01-30 22:12:17 -08002627 *nr += refs;
Kirill A. Shutemove9348052017-03-16 18:26:52 +03002628 SetPageReferenced(head);
Aneesh Kumar K.Vf30c59e2014-11-05 21:57:40 +05302629 return 1;
2630}
2631
Vasily Gorbikd3f7b1b2020-09-25 21:19:10 -07002632static int gup_pmd_range(pud_t *pudp, pud_t pud, unsigned long addr, unsigned long end,
Ira Weinyb798bec2019-05-13 17:17:07 -07002633 unsigned int flags, struct page **pages, int *nr)
Steve Capper2667f502014-10-09 15:29:14 -07002634{
2635 unsigned long next;
2636 pmd_t *pmdp;
2637
Vasily Gorbikd3f7b1b2020-09-25 21:19:10 -07002638 pmdp = pmd_offset_lockless(pudp, pud, addr);
Steve Capper2667f502014-10-09 15:29:14 -07002639 do {
Christian Borntraeger38c5ce92015-01-06 22:54:46 +01002640 pmd_t pmd = READ_ONCE(*pmdp);
Steve Capper2667f502014-10-09 15:29:14 -07002641
2642 next = pmd_addr_end(addr, end);
Zi Yan84c3fc42017-09-08 16:11:01 -07002643 if (!pmd_present(pmd))
Steve Capper2667f502014-10-09 15:29:14 -07002644 return 0;
2645
Yu Zhao414fd082019-02-12 15:35:58 -08002646 if (unlikely(pmd_trans_huge(pmd) || pmd_huge(pmd) ||
2647 pmd_devmap(pmd))) {
Steve Capper2667f502014-10-09 15:29:14 -07002648 /*
2649 * NUMA hinting faults need to be handled in the GUP
2650 * slowpath for accounting purposes and so that they
2651 * can be serialised against THP migration.
2652 */
Mel Gorman8a0516e2015-02-12 14:58:22 -08002653 if (pmd_protnone(pmd))
Steve Capper2667f502014-10-09 15:29:14 -07002654 return 0;
2655
Ira Weinyb798bec2019-05-13 17:17:07 -07002656 if (!gup_huge_pmd(pmd, pmdp, addr, next, flags,
Steve Capper2667f502014-10-09 15:29:14 -07002657 pages, nr))
2658 return 0;
2659
Aneesh Kumar K.Vf30c59e2014-11-05 21:57:40 +05302660 } else if (unlikely(is_hugepd(__hugepd(pmd_val(pmd))))) {
2661 /*
2662 * architecture have different format for hugetlbfs
2663 * pmd format and THP pmd format
2664 */
2665 if (!gup_huge_pd(__hugepd(pmd_val(pmd)), addr,
Ira Weinyb798bec2019-05-13 17:17:07 -07002666 PMD_SHIFT, next, flags, pages, nr))
Aneesh Kumar K.Vf30c59e2014-11-05 21:57:40 +05302667 return 0;
Ira Weinyb798bec2019-05-13 17:17:07 -07002668 } else if (!gup_pte_range(pmd, addr, next, flags, pages, nr))
Mario Leinweber29231172018-04-05 16:24:18 -07002669 return 0;
Steve Capper2667f502014-10-09 15:29:14 -07002670 } while (pmdp++, addr = next, addr != end);
2671
2672 return 1;
2673}
2674
Vasily Gorbikd3f7b1b2020-09-25 21:19:10 -07002675static int gup_pud_range(p4d_t *p4dp, p4d_t p4d, unsigned long addr, unsigned long end,
Ira Weinyb798bec2019-05-13 17:17:07 -07002676 unsigned int flags, struct page **pages, int *nr)
Steve Capper2667f502014-10-09 15:29:14 -07002677{
2678 unsigned long next;
2679 pud_t *pudp;
2680
Vasily Gorbikd3f7b1b2020-09-25 21:19:10 -07002681 pudp = pud_offset_lockless(p4dp, p4d, addr);
Steve Capper2667f502014-10-09 15:29:14 -07002682 do {
Christian Borntraegere37c6982014-12-07 21:41:33 +01002683 pud_t pud = READ_ONCE(*pudp);
Steve Capper2667f502014-10-09 15:29:14 -07002684
2685 next = pud_addr_end(addr, end);
Qiujun Huang154945202020-01-30 22:12:10 -08002686 if (unlikely(!pud_present(pud)))
Steve Capper2667f502014-10-09 15:29:14 -07002687 return 0;
Aneesh Kumar K.Vf30c59e2014-11-05 21:57:40 +05302688 if (unlikely(pud_huge(pud))) {
Ira Weinyb798bec2019-05-13 17:17:07 -07002689 if (!gup_huge_pud(pud, pudp, addr, next, flags,
Aneesh Kumar K.Vf30c59e2014-11-05 21:57:40 +05302690 pages, nr))
2691 return 0;
2692 } else if (unlikely(is_hugepd(__hugepd(pud_val(pud))))) {
2693 if (!gup_huge_pd(__hugepd(pud_val(pud)), addr,
Ira Weinyb798bec2019-05-13 17:17:07 -07002694 PUD_SHIFT, next, flags, pages, nr))
Steve Capper2667f502014-10-09 15:29:14 -07002695 return 0;
Vasily Gorbikd3f7b1b2020-09-25 21:19:10 -07002696 } else if (!gup_pmd_range(pudp, pud, addr, next, flags, pages, nr))
Steve Capper2667f502014-10-09 15:29:14 -07002697 return 0;
2698 } while (pudp++, addr = next, addr != end);
2699
2700 return 1;
2701}
2702
Vasily Gorbikd3f7b1b2020-09-25 21:19:10 -07002703static int gup_p4d_range(pgd_t *pgdp, pgd_t pgd, unsigned long addr, unsigned long end,
Ira Weinyb798bec2019-05-13 17:17:07 -07002704 unsigned int flags, struct page **pages, int *nr)
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +03002705{
2706 unsigned long next;
2707 p4d_t *p4dp;
2708
Vasily Gorbikd3f7b1b2020-09-25 21:19:10 -07002709 p4dp = p4d_offset_lockless(pgdp, pgd, addr);
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +03002710 do {
2711 p4d_t p4d = READ_ONCE(*p4dp);
2712
2713 next = p4d_addr_end(addr, end);
2714 if (p4d_none(p4d))
2715 return 0;
2716 BUILD_BUG_ON(p4d_huge(p4d));
2717 if (unlikely(is_hugepd(__hugepd(p4d_val(p4d))))) {
2718 if (!gup_huge_pd(__hugepd(p4d_val(p4d)), addr,
Ira Weinyb798bec2019-05-13 17:17:07 -07002719 P4D_SHIFT, next, flags, pages, nr))
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +03002720 return 0;
Vasily Gorbikd3f7b1b2020-09-25 21:19:10 -07002721 } else if (!gup_pud_range(p4dp, p4d, addr, next, flags, pages, nr))
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +03002722 return 0;
2723 } while (p4dp++, addr = next, addr != end);
2724
2725 return 1;
2726}
2727
Kirill A. Shutemov5b65c4672017-09-09 00:56:03 +03002728static void gup_pgd_range(unsigned long addr, unsigned long end,
Ira Weinyb798bec2019-05-13 17:17:07 -07002729 unsigned int flags, struct page **pages, int *nr)
Kirill A. Shutemov5b65c4672017-09-09 00:56:03 +03002730{
2731 unsigned long next;
2732 pgd_t *pgdp;
2733
2734 pgdp = pgd_offset(current->mm, addr);
2735 do {
2736 pgd_t pgd = READ_ONCE(*pgdp);
2737
2738 next = pgd_addr_end(addr, end);
2739 if (pgd_none(pgd))
2740 return;
2741 if (unlikely(pgd_huge(pgd))) {
Ira Weinyb798bec2019-05-13 17:17:07 -07002742 if (!gup_huge_pgd(pgd, pgdp, addr, next, flags,
Kirill A. Shutemov5b65c4672017-09-09 00:56:03 +03002743 pages, nr))
2744 return;
2745 } else if (unlikely(is_hugepd(__hugepd(pgd_val(pgd))))) {
2746 if (!gup_huge_pd(__hugepd(pgd_val(pgd)), addr,
Ira Weinyb798bec2019-05-13 17:17:07 -07002747 PGDIR_SHIFT, next, flags, pages, nr))
Kirill A. Shutemov5b65c4672017-09-09 00:56:03 +03002748 return;
Vasily Gorbikd3f7b1b2020-09-25 21:19:10 -07002749 } else if (!gup_p4d_range(pgdp, pgd, addr, next, flags, pages, nr))
Kirill A. Shutemov5b65c4672017-09-09 00:56:03 +03002750 return;
2751 } while (pgdp++, addr = next, addr != end);
2752}
Christoph Hellwig050a9ad2019-07-11 20:57:21 -07002753#else
2754static inline void gup_pgd_range(unsigned long addr, unsigned long end,
2755 unsigned int flags, struct page **pages, int *nr)
2756{
2757}
2758#endif /* CONFIG_HAVE_FAST_GUP */
Kirill A. Shutemov5b65c4672017-09-09 00:56:03 +03002759
2760#ifndef gup_fast_permitted
2761/*
Souptick Joarderdadbb612020-06-07 21:40:55 -07002762 * Check if it's allowed to use get_user_pages_fast_only() for the range, or
Kirill A. Shutemov5b65c4672017-09-09 00:56:03 +03002763 * we need to fall back to the slow version:
2764 */
Christoph Hellwig26f4c322019-07-11 20:56:45 -07002765static bool gup_fast_permitted(unsigned long start, unsigned long end)
Kirill A. Shutemov5b65c4672017-09-09 00:56:03 +03002766{
Christoph Hellwig26f4c322019-07-11 20:56:45 -07002767 return true;
Kirill A. Shutemov5b65c4672017-09-09 00:56:03 +03002768}
2769#endif
2770
Ira Weiny7af75562019-05-13 17:17:14 -07002771static int __gup_longterm_unlocked(unsigned long start, int nr_pages,
2772 unsigned int gup_flags, struct page **pages)
2773{
2774 int ret;
2775
2776 /*
2777 * FIXME: FOLL_LONGTERM does not work with
2778 * get_user_pages_unlocked() (see comments in that function)
2779 */
2780 if (gup_flags & FOLL_LONGTERM) {
Michel Lespinassed8ed45c2020-06-08 21:33:25 -07002781 mmap_read_lock(current->mm);
Peter Xu64019a22020-08-11 18:39:01 -07002782 ret = __gup_longterm_locked(current->mm,
Ira Weiny7af75562019-05-13 17:17:14 -07002783 start, nr_pages,
2784 pages, NULL, gup_flags);
Michel Lespinassed8ed45c2020-06-08 21:33:25 -07002785 mmap_read_unlock(current->mm);
Ira Weiny7af75562019-05-13 17:17:14 -07002786 } else {
2787 ret = get_user_pages_unlocked(start, nr_pages,
2788 pages, gup_flags);
2789 }
2790
2791 return ret;
2792}
2793
Jason Gunthorpec28b1fc2020-12-14 19:05:41 -08002794static unsigned long lockless_pages_from_mm(unsigned long start,
2795 unsigned long end,
2796 unsigned int gup_flags,
2797 struct page **pages)
2798{
2799 unsigned long flags;
2800 int nr_pinned = 0;
Jason Gunthorpe57efa1f2020-12-14 19:05:44 -08002801 unsigned seq;
Jason Gunthorpec28b1fc2020-12-14 19:05:41 -08002802
2803 if (!IS_ENABLED(CONFIG_HAVE_FAST_GUP) ||
2804 !gup_fast_permitted(start, end))
2805 return 0;
2806
Jason Gunthorpe57efa1f2020-12-14 19:05:44 -08002807 if (gup_flags & FOLL_PIN) {
2808 seq = raw_read_seqcount(&current->mm->write_protect_seq);
2809 if (seq & 1)
2810 return 0;
2811 }
2812
Jason Gunthorpec28b1fc2020-12-14 19:05:41 -08002813 /*
2814 * Disable interrupts. The nested form is used, in order to allow full,
2815 * general purpose use of this routine.
2816 *
2817 * With interrupts disabled, we block page table pages from being freed
2818 * from under us. See struct mmu_table_batch comments in
2819 * include/asm-generic/tlb.h for more details.
2820 *
2821 * We do not adopt an rcu_read_lock() here as we also want to block IPIs
2822 * that come from THPs splitting.
2823 */
2824 local_irq_save(flags);
2825 gup_pgd_range(start, end, gup_flags, pages, &nr_pinned);
2826 local_irq_restore(flags);
Jason Gunthorpe57efa1f2020-12-14 19:05:44 -08002827
2828 /*
2829 * When pinning pages for DMA there could be a concurrent write protect
2830 * from fork() via copy_page_range(), in this case always fail fast GUP.
2831 */
2832 if (gup_flags & FOLL_PIN) {
2833 if (read_seqcount_retry(&current->mm->write_protect_seq, seq)) {
2834 unpin_user_pages(pages, nr_pinned);
2835 return 0;
2836 }
2837 }
Jason Gunthorpec28b1fc2020-12-14 19:05:41 -08002838 return nr_pinned;
2839}
2840
2841static int internal_get_user_pages_fast(unsigned long start,
2842 unsigned long nr_pages,
John Hubbardeddb1c22020-01-30 22:12:54 -08002843 unsigned int gup_flags,
2844 struct page **pages)
Steve Capper2667f502014-10-09 15:29:14 -07002845{
Jason Gunthorpec28b1fc2020-12-14 19:05:41 -08002846 unsigned long len, end;
2847 unsigned long nr_pinned;
2848 int ret;
Steve Capper2667f502014-10-09 15:29:14 -07002849
John Hubbardf4000fd2020-01-30 22:12:43 -08002850 if (WARN_ON_ONCE(gup_flags & ~(FOLL_WRITE | FOLL_LONGTERM |
John Hubbard376a34ef2020-06-03 15:56:30 -07002851 FOLL_FORCE | FOLL_PIN | FOLL_GET |
Andreas Gruenbacher55b8fe72021-08-17 22:52:08 +02002852 FOLL_FAST_ONLY | FOLL_NOFAULT)))
Christoph Hellwig817be122019-07-11 20:57:25 -07002853 return -EINVAL;
2854
Andrea Arcangelia458b762021-06-28 19:36:40 -07002855 if (gup_flags & FOLL_PIN)
2856 mm_set_has_pinned_flag(&current->mm->flags);
Peter Xu008cfe42020-09-25 18:25:57 -04002857
John Hubbardf81cd172020-06-03 15:56:40 -07002858 if (!(gup_flags & FOLL_FAST_ONLY))
Michel Lespinasseda1c55f2020-06-08 21:33:47 -07002859 might_lock_read(&current->mm->mmap_lock);
John Hubbardf81cd172020-06-03 15:56:40 -07002860
Christoph Hellwigf455c8542019-07-11 20:56:41 -07002861 start = untagged_addr(start) & PAGE_MASK;
Jason Gunthorpec28b1fc2020-12-14 19:05:41 -08002862 len = nr_pages << PAGE_SHIFT;
2863 if (check_add_overflow(start, len, &end))
Michael S. Tsirkinc61611f2018-04-13 15:35:20 -07002864 return 0;
Linus Torvalds96d4f262019-01-03 18:57:57 -08002865 if (unlikely(!access_ok((void __user *)start, len)))
Michael S. Tsirkinc61611f2018-04-13 15:35:20 -07002866 return -EFAULT;
Kirill A. Shutemov73e10a62017-03-16 18:26:54 +03002867
Jason Gunthorpec28b1fc2020-12-14 19:05:41 -08002868 nr_pinned = lockless_pages_from_mm(start, end, gup_flags, pages);
2869 if (nr_pinned == nr_pages || gup_flags & FOLL_FAST_ONLY)
2870 return nr_pinned;
John Hubbard376a34ef2020-06-03 15:56:30 -07002871
Jason Gunthorpec28b1fc2020-12-14 19:05:41 -08002872 /* Slow path: try to get the remaining pages with get_user_pages */
2873 start += nr_pinned << PAGE_SHIFT;
2874 pages += nr_pinned;
2875 ret = __gup_longterm_unlocked(start, nr_pages - nr_pinned, gup_flags,
2876 pages);
2877 if (ret < 0) {
2878 /*
2879 * The caller has to unpin the pages we already pinned so
2880 * returning -errno is not an option
2881 */
2882 if (nr_pinned)
2883 return nr_pinned;
2884 return ret;
Kirill A. Shutemov73e10a62017-03-16 18:26:54 +03002885 }
Jason Gunthorpec28b1fc2020-12-14 19:05:41 -08002886 return ret + nr_pinned;
Steve Capper2667f502014-10-09 15:29:14 -07002887}
Jason Gunthorpec28b1fc2020-12-14 19:05:41 -08002888
Souptick Joarderdadbb612020-06-07 21:40:55 -07002889/**
2890 * get_user_pages_fast_only() - pin user pages in memory
2891 * @start: starting user address
2892 * @nr_pages: number of pages from start to pin
2893 * @gup_flags: flags modifying pin behaviour
2894 * @pages: array that receives pointers to the pages pinned.
2895 * Should be at least nr_pages long.
2896 *
John Hubbard9e1f0582020-06-03 15:56:27 -07002897 * Like get_user_pages_fast() except it's IRQ-safe in that it won't fall back to
2898 * the regular GUP.
2899 * Note a difference with get_user_pages_fast: this always returns the
2900 * number of pages pinned, 0 if no pages were pinned.
2901 *
2902 * If the architecture does not support this function, simply return with no
2903 * pages pinned.
2904 *
2905 * Careful, careful! COW breaking can go either way, so a non-write
2906 * access can get ambiguous page results. If you call this function without
2907 * 'write' set, you'd better be sure that you're ok with that ambiguity.
2908 */
Souptick Joarderdadbb612020-06-07 21:40:55 -07002909int get_user_pages_fast_only(unsigned long start, int nr_pages,
2910 unsigned int gup_flags, struct page **pages)
John Hubbard9e1f0582020-06-03 15:56:27 -07002911{
John Hubbard376a34ef2020-06-03 15:56:30 -07002912 int nr_pinned;
John Hubbard9e1f0582020-06-03 15:56:27 -07002913 /*
2914 * Internally (within mm/gup.c), gup fast variants must set FOLL_GET,
2915 * because gup fast is always a "pin with a +1 page refcount" request.
John Hubbard376a34ef2020-06-03 15:56:30 -07002916 *
2917 * FOLL_FAST_ONLY is required in order to match the API description of
2918 * this routine: no fall back to regular ("slow") GUP.
John Hubbard9e1f0582020-06-03 15:56:27 -07002919 */
Souptick Joarderdadbb612020-06-07 21:40:55 -07002920 gup_flags |= FOLL_GET | FOLL_FAST_ONLY;
John Hubbard9e1f0582020-06-03 15:56:27 -07002921
John Hubbard376a34ef2020-06-03 15:56:30 -07002922 nr_pinned = internal_get_user_pages_fast(start, nr_pages, gup_flags,
2923 pages);
John Hubbard9e1f0582020-06-03 15:56:27 -07002924
2925 /*
John Hubbard376a34ef2020-06-03 15:56:30 -07002926 * As specified in the API description above, this routine is not
2927 * allowed to return negative values. However, the common core
2928 * routine internal_get_user_pages_fast() *can* return -errno.
2929 * Therefore, correct for that here:
John Hubbard9e1f0582020-06-03 15:56:27 -07002930 */
John Hubbard376a34ef2020-06-03 15:56:30 -07002931 if (nr_pinned < 0)
2932 nr_pinned = 0;
John Hubbard9e1f0582020-06-03 15:56:27 -07002933
2934 return nr_pinned;
2935}
Souptick Joarderdadbb612020-06-07 21:40:55 -07002936EXPORT_SYMBOL_GPL(get_user_pages_fast_only);
John Hubbard9e1f0582020-06-03 15:56:27 -07002937
John Hubbardeddb1c22020-01-30 22:12:54 -08002938/**
2939 * get_user_pages_fast() - pin user pages in memory
John Hubbard3faa52c2020-04-01 21:05:29 -07002940 * @start: starting user address
2941 * @nr_pages: number of pages from start to pin
2942 * @gup_flags: flags modifying pin behaviour
2943 * @pages: array that receives pointers to the pages pinned.
2944 * Should be at least nr_pages long.
John Hubbardeddb1c22020-01-30 22:12:54 -08002945 *
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -07002946 * Attempt to pin user pages in memory without taking mm->mmap_lock.
John Hubbardeddb1c22020-01-30 22:12:54 -08002947 * If not successful, it will fall back to taking the lock and
2948 * calling get_user_pages().
2949 *
2950 * Returns number of pages pinned. This may be fewer than the number requested.
2951 * If nr_pages is 0 or negative, returns 0. If no pages were pinned, returns
2952 * -errno.
2953 */
2954int get_user_pages_fast(unsigned long start, int nr_pages,
2955 unsigned int gup_flags, struct page **pages)
2956{
Barry Song447f3e42020-10-13 16:51:58 -07002957 if (!is_valid_gup_flags(gup_flags))
John Hubbardeddb1c22020-01-30 22:12:54 -08002958 return -EINVAL;
2959
John Hubbard94202f12020-04-01 21:05:25 -07002960 /*
2961 * The caller may or may not have explicitly set FOLL_GET; either way is
2962 * OK. However, internally (within mm/gup.c), gup fast variants must set
2963 * FOLL_GET, because gup fast is always a "pin with a +1 page refcount"
2964 * request.
2965 */
2966 gup_flags |= FOLL_GET;
John Hubbardeddb1c22020-01-30 22:12:54 -08002967 return internal_get_user_pages_fast(start, nr_pages, gup_flags, pages);
2968}
Christoph Hellwig050a9ad2019-07-11 20:57:21 -07002969EXPORT_SYMBOL_GPL(get_user_pages_fast);
John Hubbardeddb1c22020-01-30 22:12:54 -08002970
2971/**
2972 * pin_user_pages_fast() - pin user pages in memory without taking locks
2973 *
John Hubbard3faa52c2020-04-01 21:05:29 -07002974 * @start: starting user address
2975 * @nr_pages: number of pages from start to pin
2976 * @gup_flags: flags modifying pin behaviour
2977 * @pages: array that receives pointers to the pages pinned.
2978 * Should be at least nr_pages long.
2979 *
2980 * Nearly the same as get_user_pages_fast(), except that FOLL_PIN is set. See
2981 * get_user_pages_fast() for documentation on the function arguments, because
2982 * the arguments here are identical.
2983 *
2984 * FOLL_PIN means that the pages must be released via unpin_user_page(). Please
Mauro Carvalho Chehab72ef5e52020-04-14 18:48:35 +02002985 * see Documentation/core-api/pin_user_pages.rst for further details.
John Hubbardeddb1c22020-01-30 22:12:54 -08002986 */
2987int pin_user_pages_fast(unsigned long start, int nr_pages,
2988 unsigned int gup_flags, struct page **pages)
2989{
John Hubbard3faa52c2020-04-01 21:05:29 -07002990 /* FOLL_GET and FOLL_PIN are mutually exclusive. */
2991 if (WARN_ON_ONCE(gup_flags & FOLL_GET))
2992 return -EINVAL;
2993
2994 gup_flags |= FOLL_PIN;
2995 return internal_get_user_pages_fast(start, nr_pages, gup_flags, pages);
John Hubbardeddb1c22020-01-30 22:12:54 -08002996}
2997EXPORT_SYMBOL_GPL(pin_user_pages_fast);
2998
John Hubbard104acc32020-06-03 15:56:34 -07002999/*
Souptick Joarderdadbb612020-06-07 21:40:55 -07003000 * This is the FOLL_PIN equivalent of get_user_pages_fast_only(). Behavior
3001 * is the same, except that this one sets FOLL_PIN instead of FOLL_GET.
John Hubbard104acc32020-06-03 15:56:34 -07003002 *
3003 * The API rules are the same, too: no negative values may be returned.
3004 */
3005int pin_user_pages_fast_only(unsigned long start, int nr_pages,
3006 unsigned int gup_flags, struct page **pages)
3007{
3008 int nr_pinned;
3009
3010 /*
3011 * FOLL_GET and FOLL_PIN are mutually exclusive. Note that the API
3012 * rules require returning 0, rather than -errno:
3013 */
3014 if (WARN_ON_ONCE(gup_flags & FOLL_GET))
3015 return 0;
3016 /*
3017 * FOLL_FAST_ONLY is required in order to match the API description of
3018 * this routine: no fall back to regular ("slow") GUP.
3019 */
3020 gup_flags |= (FOLL_PIN | FOLL_FAST_ONLY);
3021 nr_pinned = internal_get_user_pages_fast(start, nr_pages, gup_flags,
3022 pages);
3023 /*
3024 * This routine is not allowed to return negative values. However,
3025 * internal_get_user_pages_fast() *can* return -errno. Therefore,
3026 * correct for that here:
3027 */
3028 if (nr_pinned < 0)
3029 nr_pinned = 0;
3030
3031 return nr_pinned;
3032}
3033EXPORT_SYMBOL_GPL(pin_user_pages_fast_only);
3034
John Hubbardeddb1c22020-01-30 22:12:54 -08003035/**
Peter Xu64019a22020-08-11 18:39:01 -07003036 * pin_user_pages_remote() - pin pages of a remote process
John Hubbardeddb1c22020-01-30 22:12:54 -08003037 *
John Hubbard3faa52c2020-04-01 21:05:29 -07003038 * @mm: mm_struct of target mm
3039 * @start: starting user address
3040 * @nr_pages: number of pages from start to pin
3041 * @gup_flags: flags modifying lookup behaviour
3042 * @pages: array that receives pointers to the pages pinned.
3043 * Should be at least nr_pages long. Or NULL, if caller
3044 * only intends to ensure the pages are faulted in.
3045 * @vmas: array of pointers to vmas corresponding to each page.
3046 * Or NULL if the caller does not require them.
3047 * @locked: pointer to lock flag indicating whether lock is held and
3048 * subsequently whether VM_FAULT_RETRY functionality can be
3049 * utilised. Lock must initially be held.
3050 *
3051 * Nearly the same as get_user_pages_remote(), except that FOLL_PIN is set. See
3052 * get_user_pages_remote() for documentation on the function arguments, because
3053 * the arguments here are identical.
3054 *
3055 * FOLL_PIN means that the pages must be released via unpin_user_page(). Please
Mauro Carvalho Chehab72ef5e52020-04-14 18:48:35 +02003056 * see Documentation/core-api/pin_user_pages.rst for details.
John Hubbardeddb1c22020-01-30 22:12:54 -08003057 */
Peter Xu64019a22020-08-11 18:39:01 -07003058long pin_user_pages_remote(struct mm_struct *mm,
John Hubbardeddb1c22020-01-30 22:12:54 -08003059 unsigned long start, unsigned long nr_pages,
3060 unsigned int gup_flags, struct page **pages,
3061 struct vm_area_struct **vmas, int *locked)
3062{
John Hubbard3faa52c2020-04-01 21:05:29 -07003063 /* FOLL_GET and FOLL_PIN are mutually exclusive. */
3064 if (WARN_ON_ONCE(gup_flags & FOLL_GET))
3065 return -EINVAL;
3066
3067 gup_flags |= FOLL_PIN;
Peter Xu64019a22020-08-11 18:39:01 -07003068 return __get_user_pages_remote(mm, start, nr_pages, gup_flags,
John Hubbard3faa52c2020-04-01 21:05:29 -07003069 pages, vmas, locked);
John Hubbardeddb1c22020-01-30 22:12:54 -08003070}
3071EXPORT_SYMBOL(pin_user_pages_remote);
3072
3073/**
3074 * pin_user_pages() - pin user pages in memory for use by other devices
3075 *
John Hubbard3faa52c2020-04-01 21:05:29 -07003076 * @start: starting user address
3077 * @nr_pages: number of pages from start to pin
3078 * @gup_flags: flags modifying lookup behaviour
3079 * @pages: array that receives pointers to the pages pinned.
3080 * Should be at least nr_pages long. Or NULL, if caller
3081 * only intends to ensure the pages are faulted in.
3082 * @vmas: array of pointers to vmas corresponding to each page.
3083 * Or NULL if the caller does not require them.
3084 *
3085 * Nearly the same as get_user_pages(), except that FOLL_TOUCH is not set, and
3086 * FOLL_PIN is set.
3087 *
3088 * FOLL_PIN means that the pages must be released via unpin_user_page(). Please
Mauro Carvalho Chehab72ef5e52020-04-14 18:48:35 +02003089 * see Documentation/core-api/pin_user_pages.rst for details.
John Hubbardeddb1c22020-01-30 22:12:54 -08003090 */
3091long pin_user_pages(unsigned long start, unsigned long nr_pages,
3092 unsigned int gup_flags, struct page **pages,
3093 struct vm_area_struct **vmas)
3094{
John Hubbard3faa52c2020-04-01 21:05:29 -07003095 /* FOLL_GET and FOLL_PIN are mutually exclusive. */
3096 if (WARN_ON_ONCE(gup_flags & FOLL_GET))
3097 return -EINVAL;
3098
3099 gup_flags |= FOLL_PIN;
Peter Xu64019a22020-08-11 18:39:01 -07003100 return __gup_longterm_locked(current->mm, start, nr_pages,
John Hubbard3faa52c2020-04-01 21:05:29 -07003101 pages, vmas, gup_flags);
John Hubbardeddb1c22020-01-30 22:12:54 -08003102}
3103EXPORT_SYMBOL(pin_user_pages);
John Hubbard91429022020-06-01 21:48:27 -07003104
3105/*
3106 * pin_user_pages_unlocked() is the FOLL_PIN variant of
3107 * get_user_pages_unlocked(). Behavior is the same, except that this one sets
3108 * FOLL_PIN and rejects FOLL_GET.
3109 */
3110long pin_user_pages_unlocked(unsigned long start, unsigned long nr_pages,
3111 struct page **pages, unsigned int gup_flags)
3112{
3113 /* FOLL_GET and FOLL_PIN are mutually exclusive. */
3114 if (WARN_ON_ONCE(gup_flags & FOLL_GET))
3115 return -EINVAL;
3116
3117 gup_flags |= FOLL_PIN;
3118 return get_user_pages_unlocked(start, nr_pages, pages, gup_flags);
3119}
3120EXPORT_SYMBOL(pin_user_pages_unlocked);
John Hubbard420c2092020-06-07 21:41:02 -07003121
3122/*
3123 * pin_user_pages_locked() is the FOLL_PIN variant of get_user_pages_locked().
3124 * Behavior is the same, except that this one sets FOLL_PIN and rejects
3125 * FOLL_GET.
3126 */
3127long pin_user_pages_locked(unsigned long start, unsigned long nr_pages,
3128 unsigned int gup_flags, struct page **pages,
3129 int *locked)
3130{
3131 /*
3132 * FIXME: Current FOLL_LONGTERM behavior is incompatible with
3133 * FAULT_FLAG_ALLOW_RETRY because of the FS DAX check requirement on
3134 * vmas. As there are no users of this flag in this call we simply
3135 * disallow this option for now.
3136 */
3137 if (WARN_ON_ONCE(gup_flags & FOLL_LONGTERM))
3138 return -EINVAL;
3139
3140 /* FOLL_GET and FOLL_PIN are mutually exclusive. */
3141 if (WARN_ON_ONCE(gup_flags & FOLL_GET))
3142 return -EINVAL;
3143
3144 gup_flags |= FOLL_PIN;
Peter Xu64019a22020-08-11 18:39:01 -07003145 return __get_user_pages_locked(current->mm, start, nr_pages,
John Hubbard420c2092020-06-07 21:41:02 -07003146 pages, NULL, locked,
3147 gup_flags | FOLL_TOUCH);
3148}
3149EXPORT_SYMBOL(pin_user_pages_locked);