blob: a9d4d724aef7497aba6ae6a787f20ff8dc8fa371 [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 Hubbardc36c04c2022-02-01 19:23:17 -0800127__maybe_unused struct 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 Hubbardc36c04c2022-02-01 19:23:17 -0800211 WARN_ON_ONCE((flags & (FOLL_GET | FOLL_PIN)) == (FOLL_GET | FOLL_PIN));
John Hubbard3faa52c2020-04-01 21:05:29 -0700212
John Hubbardc36c04c2022-02-01 19:23:17 -0800213 if (flags & FOLL_GET)
214 return try_get_page(page);
215 else if (flags & FOLL_PIN) {
216 int refs = 1;
217
218 page = compound_head(page);
219
220 if (WARN_ON_ONCE(page_ref_count(page) <= 0))
221 return false;
222
223 if (hpage_pincount_available(page))
224 hpage_pincount_add(page, 1);
225 else
226 refs = GUP_PIN_COUNTING_BIAS;
227
228 /*
229 * Similar to try_grab_compound_head(): even if using the
230 * hpage_pincount_add/_sub() routines, be sure to
231 * *also* increment the normal page refcount field at least
232 * once, so that the page really is pinned.
233 */
234 page_ref_add(page, refs);
235
236 mod_node_page_state(page_pgdat(page), NR_FOLL_PIN_ACQUIRED, 1);
237 }
238
239 return true;
John Hubbard3faa52c2020-04-01 21:05:29 -0700240}
241
John Hubbard3faa52c2020-04-01 21:05:29 -0700242/**
243 * unpin_user_page() - release a dma-pinned page
244 * @page: pointer to page to be released
245 *
246 * Pages that were pinned via pin_user_pages*() must be released via either
247 * unpin_user_page(), or one of the unpin_user_pages*() routines. This is so
248 * that such pages can be separately tracked and uniquely handled. In
249 * particular, interactions with RDMA and filesystems need special handling.
250 */
251void unpin_user_page(struct page *page)
252{
Jason Gunthorpe4509b422020-12-14 19:05:51 -0800253 put_compound_head(compound_head(page), 1, FOLL_PIN);
John Hubbard3faa52c2020-04-01 21:05:29 -0700254}
255EXPORT_SYMBOL(unpin_user_page);
256
Joao Martins458a4f782021-04-29 22:55:50 -0700257static inline void compound_range_next(unsigned long i, unsigned long npages,
258 struct page **list, struct page **head,
259 unsigned int *ntails)
260{
261 struct page *next, *page;
262 unsigned int nr = 1;
263
264 if (i >= npages)
265 return;
266
267 next = *list + i;
268 page = compound_head(next);
269 if (PageCompound(page) && compound_order(page) >= 1)
270 nr = min_t(unsigned int,
271 page + compound_nr(page) - next, npages - i);
272
273 *head = page;
274 *ntails = nr;
275}
276
277#define for_each_compound_range(__i, __list, __npages, __head, __ntails) \
278 for (__i = 0, \
279 compound_range_next(__i, __npages, __list, &(__head), &(__ntails)); \
280 __i < __npages; __i += __ntails, \
281 compound_range_next(__i, __npages, __list, &(__head), &(__ntails)))
282
Joao Martins8745d7f2021-04-29 22:55:44 -0700283static inline void compound_next(unsigned long i, unsigned long npages,
284 struct page **list, struct page **head,
285 unsigned int *ntails)
286{
287 struct page *page;
288 unsigned int nr;
289
290 if (i >= npages)
291 return;
292
293 page = compound_head(list[i]);
294 for (nr = i + 1; nr < npages; nr++) {
295 if (compound_head(list[nr]) != page)
296 break;
297 }
298
299 *head = page;
300 *ntails = nr - i;
301}
302
303#define for_each_compound_head(__i, __list, __npages, __head, __ntails) \
304 for (__i = 0, \
305 compound_next(__i, __npages, __list, &(__head), &(__ntails)); \
306 __i < __npages; __i += __ntails, \
307 compound_next(__i, __npages, __list, &(__head), &(__ntails)))
308
John Hubbardfc1d8e72019-05-13 17:19:08 -0700309/**
John Hubbardf1f6a7d2020-01-30 22:13:35 -0800310 * unpin_user_pages_dirty_lock() - release and optionally dirty gup-pinned pages
akpm@linux-foundation.org2d15eb32019-09-23 15:35:04 -0700311 * @pages: array of pages to be maybe marked dirty, and definitely released.
John Hubbardfc1d8e72019-05-13 17:19:08 -0700312 * @npages: number of pages in the @pages array.
akpm@linux-foundation.org2d15eb32019-09-23 15:35:04 -0700313 * @make_dirty: whether to mark the pages dirty
John Hubbardfc1d8e72019-05-13 17:19:08 -0700314 *
315 * "gup-pinned page" refers to a page that has had one of the get_user_pages()
316 * variants called on that page.
317 *
318 * 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 -0700319 * compound page) dirty, if @make_dirty is true, and if the page was previously
John Hubbardf1f6a7d2020-01-30 22:13:35 -0800320 * listed as clean. In any case, releases all pages using unpin_user_page(),
321 * possibly via unpin_user_pages(), for the non-dirty case.
John Hubbardfc1d8e72019-05-13 17:19:08 -0700322 *
John Hubbardf1f6a7d2020-01-30 22:13:35 -0800323 * Please see the unpin_user_page() documentation for details.
John Hubbardfc1d8e72019-05-13 17:19:08 -0700324 *
akpm@linux-foundation.org2d15eb32019-09-23 15:35:04 -0700325 * set_page_dirty_lock() is used internally. If instead, set_page_dirty() is
326 * required, then the caller should a) verify that this is really correct,
327 * because _lock() is usually required, and b) hand code it:
John Hubbardf1f6a7d2020-01-30 22:13:35 -0800328 * set_page_dirty_lock(), unpin_user_page().
John Hubbardfc1d8e72019-05-13 17:19:08 -0700329 *
330 */
John Hubbardf1f6a7d2020-01-30 22:13:35 -0800331void unpin_user_pages_dirty_lock(struct page **pages, unsigned long npages,
332 bool make_dirty)
John Hubbardfc1d8e72019-05-13 17:19:08 -0700333{
akpm@linux-foundation.org2d15eb32019-09-23 15:35:04 -0700334 unsigned long index;
Joao Martins31b912d2021-04-29 22:55:47 -0700335 struct page *head;
336 unsigned int ntails;
akpm@linux-foundation.org2d15eb32019-09-23 15:35:04 -0700337
338 if (!make_dirty) {
John Hubbardf1f6a7d2020-01-30 22:13:35 -0800339 unpin_user_pages(pages, npages);
akpm@linux-foundation.org2d15eb32019-09-23 15:35:04 -0700340 return;
341 }
342
Joao Martins31b912d2021-04-29 22:55:47 -0700343 for_each_compound_head(index, pages, npages, head, ntails) {
akpm@linux-foundation.org2d15eb32019-09-23 15:35:04 -0700344 /*
345 * Checking PageDirty at this point may race with
346 * clear_page_dirty_for_io(), but that's OK. Two key
347 * cases:
348 *
349 * 1) This code sees the page as already dirty, so it
350 * skips the call to set_page_dirty(). That could happen
351 * because clear_page_dirty_for_io() called
352 * page_mkclean(), followed by set_page_dirty().
353 * However, now the page is going to get written back,
354 * which meets the original intention of setting it
355 * dirty, so all is well: clear_page_dirty_for_io() goes
356 * on to call TestClearPageDirty(), and write the page
357 * back.
358 *
359 * 2) This code sees the page as clean, so it calls
360 * set_page_dirty(). The page stays dirty, despite being
361 * written back, so it gets written back again in the
362 * next writeback cycle. This is harmless.
363 */
Joao Martins31b912d2021-04-29 22:55:47 -0700364 if (!PageDirty(head))
365 set_page_dirty_lock(head);
366 put_compound_head(head, ntails, FOLL_PIN);
akpm@linux-foundation.org2d15eb32019-09-23 15:35:04 -0700367 }
John Hubbardfc1d8e72019-05-13 17:19:08 -0700368}
John Hubbardf1f6a7d2020-01-30 22:13:35 -0800369EXPORT_SYMBOL(unpin_user_pages_dirty_lock);
John Hubbardfc1d8e72019-05-13 17:19:08 -0700370
371/**
Joao Martins458a4f782021-04-29 22:55:50 -0700372 * unpin_user_page_range_dirty_lock() - release and optionally dirty
373 * gup-pinned page range
374 *
375 * @page: the starting page of a range maybe marked dirty, and definitely released.
376 * @npages: number of consecutive pages to release.
377 * @make_dirty: whether to mark the pages dirty
378 *
379 * "gup-pinned page range" refers to a range of pages that has had one of the
380 * pin_user_pages() variants called on that page.
381 *
382 * For the page ranges defined by [page .. page+npages], make that range (or
383 * its head pages, if a compound page) dirty, if @make_dirty is true, and if the
384 * page range was previously listed as clean.
385 *
386 * set_page_dirty_lock() is used internally. If instead, set_page_dirty() is
387 * required, then the caller should a) verify that this is really correct,
388 * because _lock() is usually required, and b) hand code it:
389 * set_page_dirty_lock(), unpin_user_page().
390 *
391 */
392void unpin_user_page_range_dirty_lock(struct page *page, unsigned long npages,
393 bool make_dirty)
394{
395 unsigned long index;
396 struct page *head;
397 unsigned int ntails;
398
399 for_each_compound_range(index, &page, npages, head, ntails) {
400 if (make_dirty && !PageDirty(head))
401 set_page_dirty_lock(head);
402 put_compound_head(head, ntails, FOLL_PIN);
403 }
404}
405EXPORT_SYMBOL(unpin_user_page_range_dirty_lock);
406
407/**
John Hubbardf1f6a7d2020-01-30 22:13:35 -0800408 * unpin_user_pages() - release an array of gup-pinned pages.
John Hubbardfc1d8e72019-05-13 17:19:08 -0700409 * @pages: array of pages to be marked dirty and released.
410 * @npages: number of pages in the @pages array.
411 *
John Hubbardf1f6a7d2020-01-30 22:13:35 -0800412 * For each page in the @pages array, release the page using unpin_user_page().
John Hubbardfc1d8e72019-05-13 17:19:08 -0700413 *
John Hubbardf1f6a7d2020-01-30 22:13:35 -0800414 * Please see the unpin_user_page() documentation for details.
John Hubbardfc1d8e72019-05-13 17:19:08 -0700415 */
John Hubbardf1f6a7d2020-01-30 22:13:35 -0800416void unpin_user_pages(struct page **pages, unsigned long npages)
John Hubbardfc1d8e72019-05-13 17:19:08 -0700417{
418 unsigned long index;
Joao Martins31b912d2021-04-29 22:55:47 -0700419 struct page *head;
420 unsigned int ntails;
John Hubbardfc1d8e72019-05-13 17:19:08 -0700421
422 /*
John Hubbard146608bb2020-10-13 16:52:01 -0700423 * If this WARN_ON() fires, then the system *might* be leaking pages (by
424 * leaving them pinned), but probably not. More likely, gup/pup returned
425 * a hard -ERRNO error to the caller, who erroneously passed it here.
426 */
427 if (WARN_ON(IS_ERR_VALUE(npages)))
428 return;
Joao Martins31b912d2021-04-29 22:55:47 -0700429
430 for_each_compound_head(index, pages, npages, head, ntails)
431 put_compound_head(head, ntails, FOLL_PIN);
John Hubbardfc1d8e72019-05-13 17:19:08 -0700432}
John Hubbardf1f6a7d2020-01-30 22:13:35 -0800433EXPORT_SYMBOL(unpin_user_pages);
John Hubbardfc1d8e72019-05-13 17:19:08 -0700434
Andrea Arcangelia458b762021-06-28 19:36:40 -0700435/*
436 * Set the MMF_HAS_PINNED if not set yet; after set it'll be there for the mm's
437 * lifecycle. Avoid setting the bit unless necessary, or it might cause write
438 * cache bouncing on large SMP machines for concurrent pinned gups.
439 */
440static inline void mm_set_has_pinned_flag(unsigned long *mm_flags)
441{
442 if (!test_bit(MMF_HAS_PINNED, mm_flags))
443 set_bit(MMF_HAS_PINNED, mm_flags);
444}
445
Christoph Hellwig050a9ad2019-07-11 20:57:21 -0700446#ifdef CONFIG_MMU
Kirill A. Shutemov69e68b42014-06-04 16:08:11 -0700447static struct page *no_page_table(struct vm_area_struct *vma,
448 unsigned int flags)
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700449{
Kirill A. Shutemov69e68b42014-06-04 16:08:11 -0700450 /*
451 * When core dumping an enormous anonymous area that nobody
452 * has touched so far, we don't want to allocate unnecessary pages or
453 * page tables. Return error instead of NULL to skip handle_mm_fault,
454 * then get_dump_page() will return NULL to leave a hole in the dump.
455 * But we can only make this optimization where a hole would surely
456 * be zero-filled if handle_mm_fault() actually did handle it.
457 */
Anshuman Khanduala0137f12020-04-06 20:03:55 -0700458 if ((flags & FOLL_DUMP) &&
459 (vma_is_anonymous(vma) || !vma->vm_ops->fault))
Kirill A. Shutemov69e68b42014-06-04 16:08:11 -0700460 return ERR_PTR(-EFAULT);
461 return NULL;
462}
463
Kirill A. Shutemov1027e442015-09-04 15:47:55 -0700464static int follow_pfn_pte(struct vm_area_struct *vma, unsigned long address,
465 pte_t *pte, unsigned int flags)
466{
467 /* No page to get reference */
468 if (flags & FOLL_GET)
469 return -EFAULT;
470
471 if (flags & FOLL_TOUCH) {
472 pte_t entry = *pte;
473
474 if (flags & FOLL_WRITE)
475 entry = pte_mkdirty(entry);
476 entry = pte_mkyoung(entry);
477
478 if (!pte_same(*pte, entry)) {
479 set_pte_at(vma->vm_mm, address, pte, entry);
480 update_mmu_cache(vma, address, pte);
481 }
482 }
483
484 /* Proper page table entry exists, but no corresponding struct page */
485 return -EEXIST;
486}
487
Linus Torvalds19be0ea2016-10-13 13:07:36 -0700488/*
Peter Xua308c712020-08-21 19:49:57 -0400489 * FOLL_FORCE can write to even unwritable pte's, but only
490 * after we've gone through a COW cycle and they are dirty.
Linus Torvalds19be0ea2016-10-13 13:07:36 -0700491 */
492static inline bool can_follow_write_pte(pte_t pte, unsigned int flags)
493{
Peter Xua308c712020-08-21 19:49:57 -0400494 return pte_write(pte) ||
495 ((flags & FOLL_FORCE) && (flags & FOLL_COW) && pte_dirty(pte));
Linus Torvalds19be0ea2016-10-13 13:07:36 -0700496}
497
Kirill A. Shutemov69e68b42014-06-04 16:08:11 -0700498static struct page *follow_page_pte(struct vm_area_struct *vma,
Keith Buschdf06b372018-10-26 15:10:28 -0700499 unsigned long address, pmd_t *pmd, unsigned int flags,
500 struct dev_pagemap **pgmap)
Kirill A. Shutemov69e68b42014-06-04 16:08:11 -0700501{
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700502 struct mm_struct *mm = vma->vm_mm;
Kirill A. Shutemov69e68b42014-06-04 16:08:11 -0700503 struct page *page;
504 spinlock_t *ptl;
505 pte_t *ptep, pte;
Claudio Imbrendaf28d4362020-04-01 21:05:56 -0700506 int ret;
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700507
John Hubbardeddb1c22020-01-30 22:12:54 -0800508 /* FOLL_GET and FOLL_PIN are mutually exclusive. */
509 if (WARN_ON_ONCE((flags & (FOLL_PIN | FOLL_GET)) ==
510 (FOLL_PIN | FOLL_GET)))
511 return ERR_PTR(-EINVAL);
Kirill A. Shutemov69e68b42014-06-04 16:08:11 -0700512retry:
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700513 if (unlikely(pmd_bad(*pmd)))
Kirill A. Shutemov69e68b42014-06-04 16:08:11 -0700514 return no_page_table(vma, flags);
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700515
516 ptep = pte_offset_map_lock(mm, pmd, address, &ptl);
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700517 pte = *ptep;
518 if (!pte_present(pte)) {
519 swp_entry_t entry;
520 /*
521 * KSM's break_ksm() relies upon recognizing a ksm page
522 * even while it is being migrated, so for that case we
523 * need migration_entry_wait().
524 */
525 if (likely(!(flags & FOLL_MIGRATION)))
526 goto no_page;
Kirill A. Shutemov0661a332015-02-10 14:10:04 -0800527 if (pte_none(pte))
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700528 goto no_page;
529 entry = pte_to_swp_entry(pte);
530 if (!is_migration_entry(entry))
531 goto no_page;
532 pte_unmap_unlock(ptep, ptl);
533 migration_entry_wait(mm, pmd, address);
Kirill A. Shutemov69e68b42014-06-04 16:08:11 -0700534 goto retry;
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700535 }
Mel Gorman8a0516e2015-02-12 14:58:22 -0800536 if ((flags & FOLL_NUMA) && pte_protnone(pte))
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700537 goto no_page;
Linus Torvalds19be0ea2016-10-13 13:07:36 -0700538 if ((flags & FOLL_WRITE) && !can_follow_write_pte(pte, flags)) {
Kirill A. Shutemov69e68b42014-06-04 16:08:11 -0700539 pte_unmap_unlock(ptep, ptl);
540 return NULL;
541 }
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700542
543 page = vm_normal_page(vma, address, pte);
John Hubbard3faa52c2020-04-01 21:05:29 -0700544 if (!page && pte_devmap(pte) && (flags & (FOLL_GET | FOLL_PIN))) {
Dan Williams3565fce2016-01-15 16:56:55 -0800545 /*
John Hubbard3faa52c2020-04-01 21:05:29 -0700546 * Only return device mapping pages in the FOLL_GET or FOLL_PIN
547 * case since they are only valid while holding the pgmap
548 * reference.
Dan Williams3565fce2016-01-15 16:56:55 -0800549 */
Keith Buschdf06b372018-10-26 15:10:28 -0700550 *pgmap = get_dev_pagemap(pte_pfn(pte), *pgmap);
551 if (*pgmap)
Dan Williams3565fce2016-01-15 16:56:55 -0800552 page = pte_page(pte);
553 else
554 goto no_page;
555 } else if (unlikely(!page)) {
Kirill A. Shutemov1027e442015-09-04 15:47:55 -0700556 if (flags & FOLL_DUMP) {
557 /* Avoid special (like zero) pages in core dumps */
558 page = ERR_PTR(-EFAULT);
559 goto out;
560 }
561
562 if (is_zero_pfn(pte_pfn(pte))) {
563 page = pte_page(pte);
564 } else {
Kirill A. Shutemov1027e442015-09-04 15:47:55 -0700565 ret = follow_pfn_pte(vma, address, ptep, flags);
566 page = ERR_PTR(ret);
567 goto out;
568 }
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700569 }
570
John Hubbard3faa52c2020-04-01 21:05:29 -0700571 /* try_grab_page() does nothing unless FOLL_GET or FOLL_PIN is set. */
572 if (unlikely(!try_grab_page(page, flags))) {
573 page = ERR_PTR(-ENOMEM);
574 goto out;
Linus Torvalds8fde12c2019-04-11 10:49:19 -0700575 }
Claudio Imbrendaf28d4362020-04-01 21:05:56 -0700576 /*
577 * We need to make the page accessible if and only if we are going
578 * to access its content (the FOLL_PIN case). Please see
579 * Documentation/core-api/pin_user_pages.rst for details.
580 */
581 if (flags & FOLL_PIN) {
582 ret = arch_make_page_accessible(page);
583 if (ret) {
584 unpin_user_page(page);
585 page = ERR_PTR(ret);
586 goto out;
587 }
588 }
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700589 if (flags & FOLL_TOUCH) {
590 if ((flags & FOLL_WRITE) &&
591 !pte_dirty(pte) && !PageDirty(page))
592 set_page_dirty(page);
593 /*
594 * pte_mkyoung() would be more correct here, but atomic care
595 * is needed to avoid losing the dirty bit: it is easier to use
596 * mark_page_accessed().
597 */
598 mark_page_accessed(page);
599 }
Eric B Munsonde60f5f2015-11-05 18:51:36 -0800600 if ((flags & FOLL_MLOCK) && (vma->vm_flags & VM_LOCKED)) {
Kirill A. Shutemove90309c2016-01-15 16:54:33 -0800601 /* Do not mlock pte-mapped THP */
602 if (PageTransCompound(page))
603 goto out;
604
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700605 /*
606 * The preliminary mapping check is mainly to avoid the
607 * pointless overhead of lock_page on the ZERO_PAGE
608 * which might bounce very badly if there is contention.
609 *
610 * If the page is already locked, we don't need to
611 * handle it now - vmscan will handle it later if and
612 * when it attempts to reclaim the page.
613 */
614 if (page->mapping && trylock_page(page)) {
615 lru_add_drain(); /* push cached pages to LRU */
616 /*
617 * Because we lock page here, and migration is
618 * blocked by the pte's page reference, and we
619 * know the page is still mapped, we don't even
620 * need to check for file-cache page truncation.
621 */
622 mlock_vma_page(page);
623 unlock_page(page);
624 }
625 }
Kirill A. Shutemov1027e442015-09-04 15:47:55 -0700626out:
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700627 pte_unmap_unlock(ptep, ptl);
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700628 return page;
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700629no_page:
630 pte_unmap_unlock(ptep, ptl);
631 if (!pte_none(pte))
Kirill A. Shutemov69e68b42014-06-04 16:08:11 -0700632 return NULL;
633 return no_page_table(vma, flags);
634}
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700635
Aneesh Kumar K.V080dbb62017-07-06 15:38:44 -0700636static struct page *follow_pmd_mask(struct vm_area_struct *vma,
637 unsigned long address, pud_t *pudp,
Keith Buschdf06b372018-10-26 15:10:28 -0700638 unsigned int flags,
639 struct follow_page_context *ctx)
Kirill A. Shutemov69e68b42014-06-04 16:08:11 -0700640{
Huang Ying68827282018-06-07 17:06:34 -0700641 pmd_t *pmd, pmdval;
Kirill A. Shutemov69e68b42014-06-04 16:08:11 -0700642 spinlock_t *ptl;
643 struct page *page;
644 struct mm_struct *mm = vma->vm_mm;
645
Aneesh Kumar K.V080dbb62017-07-06 15:38:44 -0700646 pmd = pmd_offset(pudp, address);
Huang Ying68827282018-06-07 17:06:34 -0700647 /*
648 * The READ_ONCE() will stabilize the pmdval in a register or
649 * on the stack so that it will stop changing under the code.
650 */
651 pmdval = READ_ONCE(*pmd);
652 if (pmd_none(pmdval))
Kirill A. Shutemov69e68b42014-06-04 16:08:11 -0700653 return no_page_table(vma, flags);
Wei Yangbe9d3042020-01-30 22:12:14 -0800654 if (pmd_huge(pmdval) && is_vm_hugetlb_page(vma)) {
Naoya Horiguchie66f17f2015-02-11 15:25:22 -0800655 page = follow_huge_pmd(mm, address, pmd, flags);
656 if (page)
657 return page;
658 return no_page_table(vma, flags);
Kirill A. Shutemov69e68b42014-06-04 16:08:11 -0700659 }
Huang Ying68827282018-06-07 17:06:34 -0700660 if (is_hugepd(__hugepd(pmd_val(pmdval)))) {
Aneesh Kumar K.V4dc71452017-07-06 15:38:56 -0700661 page = follow_huge_pd(vma, address,
Huang Ying68827282018-06-07 17:06:34 -0700662 __hugepd(pmd_val(pmdval)), flags,
Aneesh Kumar K.V4dc71452017-07-06 15:38:56 -0700663 PMD_SHIFT);
664 if (page)
665 return page;
666 return no_page_table(vma, flags);
667 }
Zi Yan84c3fc42017-09-08 16:11:01 -0700668retry:
Huang Ying68827282018-06-07 17:06:34 -0700669 if (!pmd_present(pmdval)) {
Li Xinhai28b0ee32022-01-14 14:05:16 -0800670 /*
671 * Should never reach here, if thp migration is not supported;
672 * Otherwise, it must be a thp migration entry.
673 */
674 VM_BUG_ON(!thp_migration_supported() ||
675 !is_pmd_migration_entry(pmdval));
676
Zi Yan84c3fc42017-09-08 16:11:01 -0700677 if (likely(!(flags & FOLL_MIGRATION)))
678 return no_page_table(vma, flags);
Li Xinhai28b0ee32022-01-14 14:05:16 -0800679
680 pmd_migration_entry_wait(mm, pmd);
Huang Ying68827282018-06-07 17:06:34 -0700681 pmdval = READ_ONCE(*pmd);
682 /*
683 * MADV_DONTNEED may convert the pmd to null because
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -0700684 * mmap_lock is held in read mode
Huang Ying68827282018-06-07 17:06:34 -0700685 */
686 if (pmd_none(pmdval))
687 return no_page_table(vma, flags);
Zi Yan84c3fc42017-09-08 16:11:01 -0700688 goto retry;
689 }
Huang Ying68827282018-06-07 17:06:34 -0700690 if (pmd_devmap(pmdval)) {
Dan Williams3565fce2016-01-15 16:56:55 -0800691 ptl = pmd_lock(mm, pmd);
Keith Buschdf06b372018-10-26 15:10:28 -0700692 page = follow_devmap_pmd(vma, address, pmd, flags, &ctx->pgmap);
Dan Williams3565fce2016-01-15 16:56:55 -0800693 spin_unlock(ptl);
694 if (page)
695 return page;
696 }
Huang Ying68827282018-06-07 17:06:34 -0700697 if (likely(!pmd_trans_huge(pmdval)))
Keith Buschdf06b372018-10-26 15:10:28 -0700698 return follow_page_pte(vma, address, pmd, flags, &ctx->pgmap);
Kirill A. Shutemov6742d292016-01-15 16:52:28 -0800699
Huang Ying68827282018-06-07 17:06:34 -0700700 if ((flags & FOLL_NUMA) && pmd_protnone(pmdval))
Aneesh Kumar K.Vdb08f202017-02-24 14:59:53 -0800701 return no_page_table(vma, flags);
702
Zi Yan84c3fc42017-09-08 16:11:01 -0700703retry_locked:
Kirill A. Shutemov6742d292016-01-15 16:52:28 -0800704 ptl = pmd_lock(mm, pmd);
Huang Ying68827282018-06-07 17:06:34 -0700705 if (unlikely(pmd_none(*pmd))) {
706 spin_unlock(ptl);
707 return no_page_table(vma, flags);
708 }
Zi Yan84c3fc42017-09-08 16:11:01 -0700709 if (unlikely(!pmd_present(*pmd))) {
710 spin_unlock(ptl);
711 if (likely(!(flags & FOLL_MIGRATION)))
712 return no_page_table(vma, flags);
713 pmd_migration_entry_wait(mm, pmd);
714 goto retry_locked;
715 }
Kirill A. Shutemov6742d292016-01-15 16:52:28 -0800716 if (unlikely(!pmd_trans_huge(*pmd))) {
717 spin_unlock(ptl);
Keith Buschdf06b372018-10-26 15:10:28 -0700718 return follow_page_pte(vma, address, pmd, flags, &ctx->pgmap);
Kirill A. Shutemov69e68b42014-06-04 16:08:11 -0700719 }
Yang Shi4066c112021-04-29 22:55:56 -0700720 if (flags & FOLL_SPLIT_PMD) {
Kirill A. Shutemov6742d292016-01-15 16:52:28 -0800721 int ret;
722 page = pmd_page(*pmd);
723 if (is_huge_zero_page(page)) {
724 spin_unlock(ptl);
725 ret = 0;
Kirill A. Shutemov78ddc532016-01-15 16:52:42 -0800726 split_huge_pmd(vma, pmd, address);
Naoya Horiguchi337d9ab2016-07-26 15:24:03 -0700727 if (pmd_trans_unstable(pmd))
728 ret = -EBUSY;
Yang Shi4066c112021-04-29 22:55:56 -0700729 } else {
Song Liubfe7b002019-09-23 15:38:25 -0700730 spin_unlock(ptl);
731 split_huge_pmd(vma, pmd, address);
732 ret = pte_alloc(mm, pmd) ? -ENOMEM : 0;
Kirill A. Shutemov6742d292016-01-15 16:52:28 -0800733 }
734
735 return ret ? ERR_PTR(ret) :
Keith Buschdf06b372018-10-26 15:10:28 -0700736 follow_page_pte(vma, address, pmd, flags, &ctx->pgmap);
Kirill A. Shutemov6742d292016-01-15 16:52:28 -0800737 }
Kirill A. Shutemov6742d292016-01-15 16:52:28 -0800738 page = follow_trans_huge_pmd(vma, address, pmd, flags);
739 spin_unlock(ptl);
Keith Buschdf06b372018-10-26 15:10:28 -0700740 ctx->page_mask = HPAGE_PMD_NR - 1;
Kirill A. Shutemov6742d292016-01-15 16:52:28 -0800741 return page;
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700742}
743
Aneesh Kumar K.V080dbb62017-07-06 15:38:44 -0700744static struct page *follow_pud_mask(struct vm_area_struct *vma,
745 unsigned long address, p4d_t *p4dp,
Keith Buschdf06b372018-10-26 15:10:28 -0700746 unsigned int flags,
747 struct follow_page_context *ctx)
Aneesh Kumar K.V080dbb62017-07-06 15:38:44 -0700748{
749 pud_t *pud;
750 spinlock_t *ptl;
751 struct page *page;
752 struct mm_struct *mm = vma->vm_mm;
753
754 pud = pud_offset(p4dp, address);
755 if (pud_none(*pud))
756 return no_page_table(vma, flags);
Wei Yangbe9d3042020-01-30 22:12:14 -0800757 if (pud_huge(*pud) && is_vm_hugetlb_page(vma)) {
Aneesh Kumar K.V080dbb62017-07-06 15:38:44 -0700758 page = follow_huge_pud(mm, address, pud, flags);
759 if (page)
760 return page;
761 return no_page_table(vma, flags);
762 }
Aneesh Kumar K.V4dc71452017-07-06 15:38:56 -0700763 if (is_hugepd(__hugepd(pud_val(*pud)))) {
764 page = follow_huge_pd(vma, address,
765 __hugepd(pud_val(*pud)), flags,
766 PUD_SHIFT);
767 if (page)
768 return page;
769 return no_page_table(vma, flags);
770 }
Aneesh Kumar K.V080dbb62017-07-06 15:38:44 -0700771 if (pud_devmap(*pud)) {
772 ptl = pud_lock(mm, pud);
Keith Buschdf06b372018-10-26 15:10:28 -0700773 page = follow_devmap_pud(vma, address, pud, flags, &ctx->pgmap);
Aneesh Kumar K.V080dbb62017-07-06 15:38:44 -0700774 spin_unlock(ptl);
775 if (page)
776 return page;
777 }
778 if (unlikely(pud_bad(*pud)))
779 return no_page_table(vma, flags);
780
Keith Buschdf06b372018-10-26 15:10:28 -0700781 return follow_pmd_mask(vma, address, pud, flags, ctx);
Aneesh Kumar K.V080dbb62017-07-06 15:38:44 -0700782}
783
Aneesh Kumar K.V080dbb62017-07-06 15:38:44 -0700784static struct page *follow_p4d_mask(struct vm_area_struct *vma,
785 unsigned long address, pgd_t *pgdp,
Keith Buschdf06b372018-10-26 15:10:28 -0700786 unsigned int flags,
787 struct follow_page_context *ctx)
Aneesh Kumar K.V080dbb62017-07-06 15:38:44 -0700788{
789 p4d_t *p4d;
Aneesh Kumar K.V4dc71452017-07-06 15:38:56 -0700790 struct page *page;
Aneesh Kumar K.V080dbb62017-07-06 15:38:44 -0700791
792 p4d = p4d_offset(pgdp, address);
793 if (p4d_none(*p4d))
794 return no_page_table(vma, flags);
795 BUILD_BUG_ON(p4d_huge(*p4d));
796 if (unlikely(p4d_bad(*p4d)))
797 return no_page_table(vma, flags);
798
Aneesh Kumar K.V4dc71452017-07-06 15:38:56 -0700799 if (is_hugepd(__hugepd(p4d_val(*p4d)))) {
800 page = follow_huge_pd(vma, address,
801 __hugepd(p4d_val(*p4d)), flags,
802 P4D_SHIFT);
803 if (page)
804 return page;
805 return no_page_table(vma, flags);
806 }
Keith Buschdf06b372018-10-26 15:10:28 -0700807 return follow_pud_mask(vma, address, p4d, flags, ctx);
Aneesh Kumar K.V080dbb62017-07-06 15:38:44 -0700808}
809
810/**
811 * follow_page_mask - look up a page descriptor from a user-virtual address
812 * @vma: vm_area_struct mapping @address
813 * @address: virtual address to look up
814 * @flags: flags modifying lookup behaviour
Mike Rapoport78179552018-11-16 15:08:29 -0800815 * @ctx: contains dev_pagemap for %ZONE_DEVICE memory pinning and a
816 * pointer to output page_mask
Aneesh Kumar K.V080dbb62017-07-06 15:38:44 -0700817 *
818 * @flags can have FOLL_ flags set, defined in <linux/mm.h>
819 *
Mike Rapoport78179552018-11-16 15:08:29 -0800820 * When getting pages from ZONE_DEVICE memory, the @ctx->pgmap caches
821 * the device's dev_pagemap metadata to avoid repeating expensive lookups.
822 *
823 * On output, the @ctx->page_mask is set according to the size of the page.
824 *
825 * Return: the mapped (struct page *), %NULL if no mapping exists, or
Aneesh Kumar K.V080dbb62017-07-06 15:38:44 -0700826 * an error pointer if there is a mapping to something not represented
827 * by a page descriptor (see also vm_normal_page()).
828 */
Bharath Vedarthama7030ae2019-07-11 20:54:34 -0700829static struct page *follow_page_mask(struct vm_area_struct *vma,
Aneesh Kumar K.V080dbb62017-07-06 15:38:44 -0700830 unsigned long address, unsigned int flags,
Keith Buschdf06b372018-10-26 15:10:28 -0700831 struct follow_page_context *ctx)
Aneesh Kumar K.V080dbb62017-07-06 15:38:44 -0700832{
833 pgd_t *pgd;
834 struct page *page;
835 struct mm_struct *mm = vma->vm_mm;
836
Keith Buschdf06b372018-10-26 15:10:28 -0700837 ctx->page_mask = 0;
Aneesh Kumar K.V080dbb62017-07-06 15:38:44 -0700838
839 /* make this handle hugepd */
840 page = follow_huge_addr(mm, address, flags & FOLL_WRITE);
841 if (!IS_ERR(page)) {
John Hubbard3faa52c2020-04-01 21:05:29 -0700842 WARN_ON_ONCE(flags & (FOLL_GET | FOLL_PIN));
Aneesh Kumar K.V080dbb62017-07-06 15:38:44 -0700843 return page;
844 }
845
846 pgd = pgd_offset(mm, address);
847
848 if (pgd_none(*pgd) || unlikely(pgd_bad(*pgd)))
849 return no_page_table(vma, flags);
850
Anshuman Khandualfaaa5b62017-07-06 15:38:50 -0700851 if (pgd_huge(*pgd)) {
852 page = follow_huge_pgd(mm, address, pgd, flags);
853 if (page)
854 return page;
855 return no_page_table(vma, flags);
856 }
Aneesh Kumar K.V4dc71452017-07-06 15:38:56 -0700857 if (is_hugepd(__hugepd(pgd_val(*pgd)))) {
858 page = follow_huge_pd(vma, address,
859 __hugepd(pgd_val(*pgd)), flags,
860 PGDIR_SHIFT);
861 if (page)
862 return page;
863 return no_page_table(vma, flags);
864 }
Anshuman Khandualfaaa5b62017-07-06 15:38:50 -0700865
Keith Buschdf06b372018-10-26 15:10:28 -0700866 return follow_p4d_mask(vma, address, pgd, flags, ctx);
867}
868
869struct page *follow_page(struct vm_area_struct *vma, unsigned long address,
870 unsigned int foll_flags)
871{
872 struct follow_page_context ctx = { NULL };
873 struct page *page;
874
Mike Rapoport1507f512021-07-07 18:08:03 -0700875 if (vma_is_secretmem(vma))
876 return NULL;
877
Keith Buschdf06b372018-10-26 15:10:28 -0700878 page = follow_page_mask(vma, address, foll_flags, &ctx);
879 if (ctx.pgmap)
880 put_dev_pagemap(ctx.pgmap);
881 return page;
Aneesh Kumar K.V080dbb62017-07-06 15:38:44 -0700882}
883
Kirill A. Shutemovf2b495c2014-06-04 16:08:11 -0700884static int get_gate_page(struct mm_struct *mm, unsigned long address,
885 unsigned int gup_flags, struct vm_area_struct **vma,
886 struct page **page)
887{
888 pgd_t *pgd;
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +0300889 p4d_t *p4d;
Kirill A. Shutemovf2b495c2014-06-04 16:08:11 -0700890 pud_t *pud;
891 pmd_t *pmd;
892 pte_t *pte;
893 int ret = -EFAULT;
894
895 /* user gate pages are read-only */
896 if (gup_flags & FOLL_WRITE)
897 return -EFAULT;
898 if (address > TASK_SIZE)
899 pgd = pgd_offset_k(address);
900 else
901 pgd = pgd_offset_gate(mm, address);
Andy Lutomirskib5d1c392019-07-11 20:57:43 -0700902 if (pgd_none(*pgd))
903 return -EFAULT;
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +0300904 p4d = p4d_offset(pgd, address);
Andy Lutomirskib5d1c392019-07-11 20:57:43 -0700905 if (p4d_none(*p4d))
906 return -EFAULT;
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +0300907 pud = pud_offset(p4d, address);
Andy Lutomirskib5d1c392019-07-11 20:57:43 -0700908 if (pud_none(*pud))
909 return -EFAULT;
Kirill A. Shutemovf2b495c2014-06-04 16:08:11 -0700910 pmd = pmd_offset(pud, address);
Zi Yan84c3fc42017-09-08 16:11:01 -0700911 if (!pmd_present(*pmd))
Kirill A. Shutemovf2b495c2014-06-04 16:08:11 -0700912 return -EFAULT;
913 VM_BUG_ON(pmd_trans_huge(*pmd));
914 pte = pte_offset_map(pmd, address);
915 if (pte_none(*pte))
916 goto unmap;
917 *vma = get_gate_vma(mm);
918 if (!page)
919 goto out;
920 *page = vm_normal_page(*vma, address, *pte);
921 if (!*page) {
922 if ((gup_flags & FOLL_DUMP) || !is_zero_pfn(pte_pfn(*pte)))
923 goto unmap;
924 *page = pte_page(*pte);
925 }
Dave Hansen9fa2dd92020-09-03 13:40:28 -0700926 if (unlikely(!try_grab_page(*page, gup_flags))) {
Linus Torvalds8fde12c2019-04-11 10:49:19 -0700927 ret = -ENOMEM;
928 goto unmap;
929 }
Kirill A. Shutemovf2b495c2014-06-04 16:08:11 -0700930out:
931 ret = 0;
932unmap:
933 pte_unmap(pte);
934 return ret;
935}
936
Paul Cassella9a95f3c2014-08-06 16:07:24 -0700937/*
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -0700938 * mmap_lock must be held on entry. If @locked != NULL and *@flags
939 * does not include FOLL_NOWAIT, the mmap_lock may be released. If it
Peter Xu4f6da932020-04-01 21:07:58 -0700940 * is, *@locked will be set to 0 and -EBUSY returned.
Paul Cassella9a95f3c2014-08-06 16:07:24 -0700941 */
Peter Xu64019a22020-08-11 18:39:01 -0700942static int faultin_page(struct vm_area_struct *vma,
Peter Xu4f6da932020-04-01 21:07:58 -0700943 unsigned long address, unsigned int *flags, int *locked)
Kirill A. Shutemov16744482014-06-04 16:08:12 -0700944{
Kirill A. Shutemov16744482014-06-04 16:08:12 -0700945 unsigned int fault_flags = 0;
Souptick Joarder2b740302018-08-23 17:01:36 -0700946 vm_fault_t ret;
Kirill A. Shutemov16744482014-06-04 16:08:12 -0700947
Eric B Munsonde60f5f2015-11-05 18:51:36 -0800948 /* mlock all present pages, but do not fault in new pages */
949 if ((*flags & (FOLL_POPULATE | FOLL_MLOCK)) == FOLL_MLOCK)
950 return -ENOENT;
Andreas Gruenbacher55b8fe72021-08-17 22:52:08 +0200951 if (*flags & FOLL_NOFAULT)
952 return -EFAULT;
Kirill A. Shutemov16744482014-06-04 16:08:12 -0700953 if (*flags & FOLL_WRITE)
954 fault_flags |= FAULT_FLAG_WRITE;
Dave Hansen1b2ee122016-02-12 13:02:21 -0800955 if (*flags & FOLL_REMOTE)
956 fault_flags |= FAULT_FLAG_REMOTE;
Peter Xu4f6da932020-04-01 21:07:58 -0700957 if (locked)
Peter Xu71335f32020-04-01 21:08:53 -0700958 fault_flags |= FAULT_FLAG_ALLOW_RETRY | FAULT_FLAG_KILLABLE;
Kirill A. Shutemov16744482014-06-04 16:08:12 -0700959 if (*flags & FOLL_NOWAIT)
960 fault_flags |= FAULT_FLAG_ALLOW_RETRY | FAULT_FLAG_RETRY_NOWAIT;
Andres Lagar-Cavilla234b2392014-09-17 10:51:48 -0700961 if (*flags & FOLL_TRIED) {
Peter Xu4426e942020-04-01 21:08:49 -0700962 /*
963 * Note: FAULT_FLAG_ALLOW_RETRY and FAULT_FLAG_TRIED
964 * can co-exist
965 */
Andres Lagar-Cavilla234b2392014-09-17 10:51:48 -0700966 fault_flags |= FAULT_FLAG_TRIED;
967 }
Kirill A. Shutemov16744482014-06-04 16:08:12 -0700968
Peter Xubce617e2020-08-11 18:37:44 -0700969 ret = handle_mm_fault(vma, address, fault_flags, NULL);
Kirill A. Shutemov16744482014-06-04 16:08:12 -0700970 if (ret & VM_FAULT_ERROR) {
James Morse9a291a72017-06-02 14:46:46 -0700971 int err = vm_fault_to_errno(ret, *flags);
972
973 if (err)
974 return err;
Kirill A. Shutemov16744482014-06-04 16:08:12 -0700975 BUG();
976 }
977
Kirill A. Shutemov16744482014-06-04 16:08:12 -0700978 if (ret & VM_FAULT_RETRY) {
Peter Xu4f6da932020-04-01 21:07:58 -0700979 if (locked && !(fault_flags & FAULT_FLAG_RETRY_NOWAIT))
980 *locked = 0;
Kirill A. Shutemov16744482014-06-04 16:08:12 -0700981 return -EBUSY;
982 }
983
984 /*
985 * The VM_FAULT_WRITE bit tells us that do_wp_page has broken COW when
986 * necessary, even if maybe_mkwrite decided not to set pte_write. We
987 * can thus safely do subsequent page lookups as if they were reads.
988 * But only do so when looping for pte_write is futile: in some cases
989 * userspace may also be wanting to write to the gotten user page,
990 * which a read fault here might prevent (a readonly page might get
991 * reCOWed by userspace write).
992 */
993 if ((ret & VM_FAULT_WRITE) && !(vma->vm_flags & VM_WRITE))
Mario Leinweber29231172018-04-05 16:24:18 -0700994 *flags |= FOLL_COW;
Kirill A. Shutemov16744482014-06-04 16:08:12 -0700995 return 0;
996}
997
Kirill A. Shutemovfa5bb202014-06-04 16:08:13 -0700998static int check_vma_flags(struct vm_area_struct *vma, unsigned long gup_flags)
999{
1000 vm_flags_t vm_flags = vma->vm_flags;
Dave Hansen1b2ee122016-02-12 13:02:21 -08001001 int write = (gup_flags & FOLL_WRITE);
1002 int foreign = (gup_flags & FOLL_REMOTE);
Kirill A. Shutemovfa5bb202014-06-04 16:08:13 -07001003
1004 if (vm_flags & (VM_IO | VM_PFNMAP))
1005 return -EFAULT;
1006
Willy Tarreau7f7ccc22018-05-11 08:11:44 +02001007 if (gup_flags & FOLL_ANON && !vma_is_anonymous(vma))
1008 return -EFAULT;
1009
Jason Gunthorpe52650c82020-12-14 19:05:48 -08001010 if ((gup_flags & FOLL_LONGTERM) && vma_is_fsdax(vma))
1011 return -EOPNOTSUPP;
1012
Mike Rapoport1507f512021-07-07 18:08:03 -07001013 if (vma_is_secretmem(vma))
1014 return -EFAULT;
1015
Dave Hansen1b2ee122016-02-12 13:02:21 -08001016 if (write) {
Kirill A. Shutemovfa5bb202014-06-04 16:08:13 -07001017 if (!(vm_flags & VM_WRITE)) {
1018 if (!(gup_flags & FOLL_FORCE))
1019 return -EFAULT;
1020 /*
1021 * We used to let the write,force case do COW in a
1022 * VM_MAYWRITE VM_SHARED !VM_WRITE vma, so ptrace could
1023 * set a breakpoint in a read-only mapping of an
1024 * executable, without corrupting the file (yet only
1025 * when that file had been opened for writing!).
1026 * Anon pages in shared mappings are surprising: now
1027 * just reject it.
1028 */
Hugh Dickins46435362016-01-30 18:03:16 -08001029 if (!is_cow_mapping(vm_flags))
Kirill A. Shutemovfa5bb202014-06-04 16:08:13 -07001030 return -EFAULT;
Kirill A. Shutemovfa5bb202014-06-04 16:08:13 -07001031 }
1032 } else if (!(vm_flags & VM_READ)) {
1033 if (!(gup_flags & FOLL_FORCE))
1034 return -EFAULT;
1035 /*
1036 * Is there actually any vma we can reach here which does not
1037 * have VM_MAYREAD set?
1038 */
1039 if (!(vm_flags & VM_MAYREAD))
1040 return -EFAULT;
1041 }
Dave Hansend61172b2016-02-12 13:02:24 -08001042 /*
1043 * gups are always data accesses, not instruction
1044 * fetches, so execute=false here
1045 */
1046 if (!arch_vma_access_permitted(vma, write, false, foreign))
Dave Hansen33a709b2016-02-12 13:02:19 -08001047 return -EFAULT;
Kirill A. Shutemovfa5bb202014-06-04 16:08:13 -07001048 return 0;
1049}
1050
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -07001051/**
1052 * __get_user_pages() - pin user pages in memory
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -07001053 * @mm: mm_struct of target mm
1054 * @start: starting user address
1055 * @nr_pages: number of pages from start to pin
1056 * @gup_flags: flags modifying pin behaviour
1057 * @pages: array that receives pointers to the pages pinned.
1058 * Should be at least nr_pages long. Or NULL, if caller
1059 * only intends to ensure the pages are faulted in.
1060 * @vmas: array of pointers to vmas corresponding to each page.
1061 * Or NULL if the caller does not require them.
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -07001062 * @locked: whether we're still with the mmap_lock held
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -07001063 *
Liu Xiangd2dfbe42019-11-30 17:49:53 -08001064 * Returns either number of pages pinned (which may be less than the
1065 * number requested), or an error. Details about the return value:
1066 *
1067 * -- If nr_pages is 0, returns 0.
1068 * -- If nr_pages is >0, but no pages were pinned, returns -errno.
1069 * -- If nr_pages is >0, and some pages were pinned, returns the number of
1070 * pages pinned. Again, this may be less than nr_pages.
Michal Hocko2d3a36a2020-06-03 16:03:25 -07001071 * -- 0 return value is possible when the fault would need to be retried.
Liu Xiangd2dfbe42019-11-30 17:49:53 -08001072 *
1073 * The caller is responsible for releasing returned @pages, via put_page().
1074 *
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -07001075 * @vmas are valid only as long as mmap_lock is held.
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -07001076 *
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -07001077 * Must be called with mmap_lock held. It may be released. See below.
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -07001078 *
1079 * __get_user_pages walks a process's page tables and takes a reference to
1080 * each struct page that each user address corresponds to at a given
1081 * instant. That is, it takes the page that would be accessed if a user
1082 * thread accesses the given user virtual address at that instant.
1083 *
1084 * This does not guarantee that the page exists in the user mappings when
1085 * __get_user_pages returns, and there may even be a completely different
1086 * page there in some cases (eg. if mmapped pagecache has been invalidated
1087 * and subsequently re faulted). However it does guarantee that the page
1088 * won't be freed completely. And mostly callers simply care that the page
1089 * contains data that was valid *at some point in time*. Typically, an IO
1090 * or similar operation cannot guarantee anything stronger anyway because
1091 * locks can't be held over the syscall boundary.
1092 *
1093 * If @gup_flags & FOLL_WRITE == 0, the page must not be written to. If
1094 * the page is written to, set_page_dirty (or set_page_dirty_lock, as
1095 * appropriate) must be called after the page is finished with, and
1096 * before put_page is called.
1097 *
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -07001098 * If @locked != NULL, *@locked will be set to 0 when mmap_lock is
Peter Xu4f6da932020-04-01 21:07:58 -07001099 * released by an up_read(). That can happen if @gup_flags does not
1100 * have FOLL_NOWAIT.
Paul Cassella9a95f3c2014-08-06 16:07:24 -07001101 *
Peter Xu4f6da932020-04-01 21:07:58 -07001102 * A caller using such a combination of @locked and @gup_flags
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -07001103 * must therefore hold the mmap_lock for reading only, and recognize
Paul Cassella9a95f3c2014-08-06 16:07:24 -07001104 * when it's been released. Otherwise, it must be held for either
1105 * reading or writing and will not be released.
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -07001106 *
1107 * In most cases, get_user_pages or get_user_pages_fast should be used
1108 * instead of __get_user_pages. __get_user_pages should be used only if
1109 * you need some special @gup_flags.
1110 */
Peter Xu64019a22020-08-11 18:39:01 -07001111static long __get_user_pages(struct mm_struct *mm,
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -07001112 unsigned long start, unsigned long nr_pages,
1113 unsigned int gup_flags, struct page **pages,
Peter Xu4f6da932020-04-01 21:07:58 -07001114 struct vm_area_struct **vmas, int *locked)
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -07001115{
Keith Buschdf06b372018-10-26 15:10:28 -07001116 long ret = 0, i = 0;
Kirill A. Shutemovfa5bb202014-06-04 16:08:13 -07001117 struct vm_area_struct *vma = NULL;
Keith Buschdf06b372018-10-26 15:10:28 -07001118 struct follow_page_context ctx = { NULL };
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -07001119
1120 if (!nr_pages)
1121 return 0;
1122
Andrey Konovalovf9652592019-09-25 16:48:34 -07001123 start = untagged_addr(start);
1124
John Hubbardeddb1c22020-01-30 22:12:54 -08001125 VM_BUG_ON(!!pages != !!(gup_flags & (FOLL_GET | FOLL_PIN)));
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -07001126
1127 /*
1128 * If FOLL_FORCE is set then do not force a full fault as the hinting
1129 * fault information is unrelated to the reference behaviour of a task
1130 * using the address space
1131 */
1132 if (!(gup_flags & FOLL_FORCE))
1133 gup_flags |= FOLL_NUMA;
1134
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -07001135 do {
Kirill A. Shutemovfa5bb202014-06-04 16:08:13 -07001136 struct page *page;
1137 unsigned int foll_flags = gup_flags;
1138 unsigned int page_increm;
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -07001139
Kirill A. Shutemovfa5bb202014-06-04 16:08:13 -07001140 /* first iteration or cross vma bound */
1141 if (!vma || start >= vma->vm_end) {
1142 vma = find_extend_vma(mm, start);
1143 if (!vma && in_gate_area(mm, start)) {
Kirill A. Shutemovfa5bb202014-06-04 16:08:13 -07001144 ret = get_gate_page(mm, start & PAGE_MASK,
1145 gup_flags, &vma,
1146 pages ? &pages[i] : NULL);
1147 if (ret)
John Hubbard08be37b2018-11-30 14:08:53 -08001148 goto out;
Keith Buschdf06b372018-10-26 15:10:28 -07001149 ctx.page_mask = 0;
Kirill A. Shutemovfa5bb202014-06-04 16:08:13 -07001150 goto next_page;
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -07001151 }
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -07001152
Jason Gunthorpe52650c82020-12-14 19:05:48 -08001153 if (!vma) {
Keith Buschdf06b372018-10-26 15:10:28 -07001154 ret = -EFAULT;
1155 goto out;
1156 }
Jason Gunthorpe52650c82020-12-14 19:05:48 -08001157 ret = check_vma_flags(vma, gup_flags);
1158 if (ret)
1159 goto out;
1160
Kirill A. Shutemovfa5bb202014-06-04 16:08:13 -07001161 if (is_vm_hugetlb_page(vma)) {
1162 i = follow_hugetlb_page(mm, vma, pages, vmas,
1163 &start, &nr_pages, i,
Peter Xua308c712020-08-21 19:49:57 -04001164 gup_flags, locked);
Peter Xuad415db2020-04-01 21:08:02 -07001165 if (locked && *locked == 0) {
1166 /*
1167 * We've got a VM_FAULT_RETRY
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -07001168 * and we've lost mmap_lock.
Peter Xuad415db2020-04-01 21:08:02 -07001169 * We must stop here.
1170 */
1171 BUG_ON(gup_flags & FOLL_NOWAIT);
Peter Xuad415db2020-04-01 21:08:02 -07001172 goto out;
1173 }
Kirill A. Shutemovfa5bb202014-06-04 16:08:13 -07001174 continue;
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -07001175 }
Kirill A. Shutemovfa5bb202014-06-04 16:08:13 -07001176 }
1177retry:
1178 /*
1179 * If we have a pending SIGKILL, don't keep faulting pages and
1180 * potentially allocating memory.
1181 */
Davidlohr Buesofa45f112019-01-03 15:28:55 -08001182 if (fatal_signal_pending(current)) {
Michal Hockod180870d2020-04-20 18:13:55 -07001183 ret = -EINTR;
Keith Buschdf06b372018-10-26 15:10:28 -07001184 goto out;
1185 }
Kirill A. Shutemovfa5bb202014-06-04 16:08:13 -07001186 cond_resched();
Keith Buschdf06b372018-10-26 15:10:28 -07001187
1188 page = follow_page_mask(vma, start, foll_flags, &ctx);
Kirill A. Shutemovfa5bb202014-06-04 16:08:13 -07001189 if (!page) {
Peter Xu64019a22020-08-11 18:39:01 -07001190 ret = faultin_page(vma, start, &foll_flags, locked);
Kirill A. Shutemovfa5bb202014-06-04 16:08:13 -07001191 switch (ret) {
1192 case 0:
1193 goto retry;
Keith Buschdf06b372018-10-26 15:10:28 -07001194 case -EBUSY:
1195 ret = 0;
Joe Perchese4a9bc52020-04-06 20:08:39 -07001196 fallthrough;
Kirill A. Shutemovfa5bb202014-06-04 16:08:13 -07001197 case -EFAULT:
1198 case -ENOMEM:
1199 case -EHWPOISON:
Keith Buschdf06b372018-10-26 15:10:28 -07001200 goto out;
Kirill A. Shutemovfa5bb202014-06-04 16:08:13 -07001201 case -ENOENT:
1202 goto next_page;
1203 }
1204 BUG();
Kirill A. Shutemov1027e442015-09-04 15:47:55 -07001205 } else if (PTR_ERR(page) == -EEXIST) {
1206 /*
1207 * Proper page table entry exists, but no corresponding
1208 * struct page.
1209 */
1210 goto next_page;
1211 } else if (IS_ERR(page)) {
Keith Buschdf06b372018-10-26 15:10:28 -07001212 ret = PTR_ERR(page);
1213 goto out;
Kirill A. Shutemov1027e442015-09-04 15:47:55 -07001214 }
Kirill A. Shutemovfa5bb202014-06-04 16:08:13 -07001215 if (pages) {
1216 pages[i] = page;
1217 flush_anon_page(vma, page, start);
1218 flush_dcache_page(page);
Keith Buschdf06b372018-10-26 15:10:28 -07001219 ctx.page_mask = 0;
Kirill A. Shutemovfa5bb202014-06-04 16:08:13 -07001220 }
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -07001221next_page:
Kirill A. Shutemovfa5bb202014-06-04 16:08:13 -07001222 if (vmas) {
1223 vmas[i] = vma;
Keith Buschdf06b372018-10-26 15:10:28 -07001224 ctx.page_mask = 0;
Kirill A. Shutemovfa5bb202014-06-04 16:08:13 -07001225 }
Keith Buschdf06b372018-10-26 15:10:28 -07001226 page_increm = 1 + (~(start >> PAGE_SHIFT) & ctx.page_mask);
Kirill A. Shutemovfa5bb202014-06-04 16:08:13 -07001227 if (page_increm > nr_pages)
1228 page_increm = nr_pages;
1229 i += page_increm;
1230 start += page_increm * PAGE_SIZE;
1231 nr_pages -= page_increm;
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -07001232 } while (nr_pages);
Keith Buschdf06b372018-10-26 15:10:28 -07001233out:
1234 if (ctx.pgmap)
1235 put_dev_pagemap(ctx.pgmap);
1236 return i ? i : ret;
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -07001237}
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -07001238
Tobias Klauser771ab432016-12-12 16:41:53 -08001239static bool vma_permits_fault(struct vm_area_struct *vma,
1240 unsigned int fault_flags)
Dave Hansend4925e02016-02-12 13:02:16 -08001241{
Dave Hansen1b2ee122016-02-12 13:02:21 -08001242 bool write = !!(fault_flags & FAULT_FLAG_WRITE);
1243 bool foreign = !!(fault_flags & FAULT_FLAG_REMOTE);
Dave Hansen33a709b2016-02-12 13:02:19 -08001244 vm_flags_t vm_flags = write ? VM_WRITE : VM_READ;
Dave Hansend4925e02016-02-12 13:02:16 -08001245
1246 if (!(vm_flags & vma->vm_flags))
1247 return false;
1248
Dave Hansen33a709b2016-02-12 13:02:19 -08001249 /*
1250 * The architecture might have a hardware protection
Dave Hansen1b2ee122016-02-12 13:02:21 -08001251 * mechanism other than read/write that can deny access.
Dave Hansend61172b2016-02-12 13:02:24 -08001252 *
1253 * gup always represents data access, not instruction
1254 * fetches, so execute=false here:
Dave Hansen33a709b2016-02-12 13:02:19 -08001255 */
Dave Hansend61172b2016-02-12 13:02:24 -08001256 if (!arch_vma_access_permitted(vma, write, false, foreign))
Dave Hansen33a709b2016-02-12 13:02:19 -08001257 return false;
1258
Dave Hansend4925e02016-02-12 13:02:16 -08001259 return true;
1260}
1261
Souptick Joarderadc8cb42020-06-01 21:48:24 -07001262/**
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -07001263 * fixup_user_fault() - manually resolve a user page fault
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -07001264 * @mm: mm_struct of target mm
1265 * @address: user address
1266 * @fault_flags:flags to pass down to handle_mm_fault()
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -07001267 * @unlocked: did we unlock the mmap_lock while retrying, maybe NULL if caller
Miles Chen548b6a12020-06-01 21:48:33 -07001268 * does not allow retry. If NULL, the caller must guarantee
1269 * that fault_flags does not contain FAULT_FLAG_ALLOW_RETRY.
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -07001270 *
1271 * This is meant to be called in the specific scenario where for locking reasons
1272 * we try to access user memory in atomic context (within a pagefault_disable()
1273 * section), this returns -EFAULT, and we want to resolve the user fault before
1274 * trying again.
1275 *
1276 * Typically this is meant to be used by the futex code.
1277 *
1278 * The main difference with get_user_pages() is that this function will
1279 * unconditionally call handle_mm_fault() which will in turn perform all the
1280 * necessary SW fixup of the dirty and young bits in the PTE, while
Dominik Dingel4a9e1cd2016-01-15 16:57:04 -08001281 * get_user_pages() only guarantees to update these in the struct page.
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -07001282 *
1283 * This is important for some architectures where those bits also gate the
1284 * access permission to the page because they are maintained in software. On
1285 * such architectures, gup() will not be enough to make a subsequent access
1286 * succeed.
1287 *
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -07001288 * This function will not return with an unlocked mmap_lock. So it has not the
1289 * same semantics wrt the @mm->mmap_lock as does filemap_fault().
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -07001290 */
Peter Xu64019a22020-08-11 18:39:01 -07001291int fixup_user_fault(struct mm_struct *mm,
Dominik Dingel4a9e1cd2016-01-15 16:57:04 -08001292 unsigned long address, unsigned int fault_flags,
1293 bool *unlocked)
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -07001294{
1295 struct vm_area_struct *vma;
Miaohe Lin8fed2f32021-09-02 14:53:33 -07001296 vm_fault_t ret;
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -07001297
Andrey Konovalovf9652592019-09-25 16:48:34 -07001298 address = untagged_addr(address);
1299
Dominik Dingel4a9e1cd2016-01-15 16:57:04 -08001300 if (unlocked)
Peter Xu71335f32020-04-01 21:08:53 -07001301 fault_flags |= FAULT_FLAG_ALLOW_RETRY | FAULT_FLAG_KILLABLE;
Dominik Dingel4a9e1cd2016-01-15 16:57:04 -08001302
1303retry:
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -07001304 vma = find_extend_vma(mm, address);
1305 if (!vma || address < vma->vm_start)
1306 return -EFAULT;
1307
Dave Hansend4925e02016-02-12 13:02:16 -08001308 if (!vma_permits_fault(vma, fault_flags))
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -07001309 return -EFAULT;
1310
Peter Xu475f4dfc2020-05-13 17:50:41 -07001311 if ((fault_flags & FAULT_FLAG_KILLABLE) &&
1312 fatal_signal_pending(current))
1313 return -EINTR;
1314
Peter Xubce617e2020-08-11 18:37:44 -07001315 ret = handle_mm_fault(vma, address, fault_flags, NULL);
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -07001316 if (ret & VM_FAULT_ERROR) {
James Morse9a291a72017-06-02 14:46:46 -07001317 int err = vm_fault_to_errno(ret, 0);
1318
1319 if (err)
1320 return err;
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -07001321 BUG();
1322 }
Dominik Dingel4a9e1cd2016-01-15 16:57:04 -08001323
1324 if (ret & VM_FAULT_RETRY) {
Michel Lespinassed8ed45c2020-06-08 21:33:25 -07001325 mmap_read_lock(mm);
Peter Xu475f4dfc2020-05-13 17:50:41 -07001326 *unlocked = true;
1327 fault_flags |= FAULT_FLAG_TRIED;
1328 goto retry;
Dominik Dingel4a9e1cd2016-01-15 16:57:04 -08001329 }
1330
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -07001331 return 0;
1332}
Paolo Bonziniadd6a0c2016-06-07 17:51:18 +02001333EXPORT_SYMBOL_GPL(fixup_user_fault);
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -07001334
Michal Hocko2d3a36a2020-06-03 16:03:25 -07001335/*
1336 * Please note that this function, unlike __get_user_pages will not
1337 * return 0 for nr_pages > 0 without FOLL_NOWAIT
1338 */
Peter Xu64019a22020-08-11 18:39:01 -07001339static __always_inline long __get_user_pages_locked(struct mm_struct *mm,
Andrea Arcangelif0818f42015-02-11 15:27:17 -08001340 unsigned long start,
1341 unsigned long nr_pages,
Andrea Arcangelif0818f42015-02-11 15:27:17 -08001342 struct page **pages,
1343 struct vm_area_struct **vmas,
Al Viroe7167122017-11-19 11:32:05 -05001344 int *locked,
Andrea Arcangeli0fd71a52015-02-11 15:27:20 -08001345 unsigned int flags)
Andrea Arcangelif0818f42015-02-11 15:27:17 -08001346{
Andrea Arcangelif0818f42015-02-11 15:27:17 -08001347 long ret, pages_done;
1348 bool lock_dropped;
1349
1350 if (locked) {
1351 /* if VM_FAULT_RETRY can be returned, vmas become invalid */
1352 BUG_ON(vmas);
1353 /* check caller initialized locked */
1354 BUG_ON(*locked != 1);
1355 }
1356
Andrea Arcangelia458b762021-06-28 19:36:40 -07001357 if (flags & FOLL_PIN)
1358 mm_set_has_pinned_flag(&mm->flags);
Peter Xu008cfe42020-09-25 18:25:57 -04001359
John Hubbardeddb1c22020-01-30 22:12:54 -08001360 /*
1361 * FOLL_PIN and FOLL_GET are mutually exclusive. Traditional behavior
1362 * is to set FOLL_GET if the caller wants pages[] filled in (but has
1363 * carelessly failed to specify FOLL_GET), so keep doing that, but only
1364 * for FOLL_GET, not for the newer FOLL_PIN.
1365 *
1366 * FOLL_PIN always expects pages to be non-null, but no need to assert
1367 * that here, as any failures will be obvious enough.
1368 */
1369 if (pages && !(flags & FOLL_PIN))
Andrea Arcangelif0818f42015-02-11 15:27:17 -08001370 flags |= FOLL_GET;
Andrea Arcangelif0818f42015-02-11 15:27:17 -08001371
1372 pages_done = 0;
1373 lock_dropped = false;
1374 for (;;) {
Peter Xu64019a22020-08-11 18:39:01 -07001375 ret = __get_user_pages(mm, start, nr_pages, flags, pages,
Andrea Arcangelif0818f42015-02-11 15:27:17 -08001376 vmas, locked);
1377 if (!locked)
1378 /* VM_FAULT_RETRY couldn't trigger, bypass */
1379 return ret;
1380
1381 /* VM_FAULT_RETRY cannot return errors */
1382 if (!*locked) {
1383 BUG_ON(ret < 0);
1384 BUG_ON(ret >= nr_pages);
1385 }
1386
Andrea Arcangelif0818f42015-02-11 15:27:17 -08001387 if (ret > 0) {
1388 nr_pages -= ret;
1389 pages_done += ret;
1390 if (!nr_pages)
1391 break;
1392 }
1393 if (*locked) {
Andrea Arcangeli96312e62018-03-09 15:51:06 -08001394 /*
1395 * VM_FAULT_RETRY didn't trigger or it was a
1396 * FOLL_NOWAIT.
1397 */
Andrea Arcangelif0818f42015-02-11 15:27:17 -08001398 if (!pages_done)
1399 pages_done = ret;
1400 break;
1401 }
Mike Rapoportdf172772019-05-31 22:30:33 -07001402 /*
1403 * VM_FAULT_RETRY triggered, so seek to the faulting offset.
1404 * For the prefault case (!pages) we only update counts.
1405 */
1406 if (likely(pages))
1407 pages += ret;
Andrea Arcangelif0818f42015-02-11 15:27:17 -08001408 start += ret << PAGE_SHIFT;
Peter Xu4426e942020-04-01 21:08:49 -07001409 lock_dropped = true;
Andrea Arcangelif0818f42015-02-11 15:27:17 -08001410
Peter Xu4426e942020-04-01 21:08:49 -07001411retry:
Andrea Arcangelif0818f42015-02-11 15:27:17 -08001412 /*
1413 * Repeat on the address that fired VM_FAULT_RETRY
Peter Xu4426e942020-04-01 21:08:49 -07001414 * with both FAULT_FLAG_ALLOW_RETRY and
1415 * FAULT_FLAG_TRIED. Note that GUP can be interrupted
1416 * by fatal signals, so we need to check it before we
1417 * start trying again otherwise it can loop forever.
Andrea Arcangelif0818f42015-02-11 15:27:17 -08001418 */
Peter Xu4426e942020-04-01 21:08:49 -07001419
Hillf Dantonae46d2a2020-04-08 11:59:24 -04001420 if (fatal_signal_pending(current)) {
1421 if (!pages_done)
1422 pages_done = -EINTR;
Peter Xu4426e942020-04-01 21:08:49 -07001423 break;
Hillf Dantonae46d2a2020-04-08 11:59:24 -04001424 }
Peter Xu4426e942020-04-01 21:08:49 -07001425
Michel Lespinassed8ed45c2020-06-08 21:33:25 -07001426 ret = mmap_read_lock_killable(mm);
Peter Xu71335f32020-04-01 21:08:53 -07001427 if (ret) {
1428 BUG_ON(ret > 0);
1429 if (!pages_done)
1430 pages_done = ret;
1431 break;
1432 }
Peter Xu4426e942020-04-01 21:08:49 -07001433
Peter Xuc7b6a562020-04-07 21:40:10 -04001434 *locked = 1;
Peter Xu64019a22020-08-11 18:39:01 -07001435 ret = __get_user_pages(mm, start, 1, flags | FOLL_TRIED,
Peter Xu4426e942020-04-01 21:08:49 -07001436 pages, NULL, locked);
1437 if (!*locked) {
1438 /* Continue to retry until we succeeded */
1439 BUG_ON(ret != 0);
1440 goto retry;
1441 }
Andrea Arcangelif0818f42015-02-11 15:27:17 -08001442 if (ret != 1) {
1443 BUG_ON(ret > 1);
1444 if (!pages_done)
1445 pages_done = ret;
1446 break;
1447 }
1448 nr_pages--;
1449 pages_done++;
1450 if (!nr_pages)
1451 break;
Mike Rapoportdf172772019-05-31 22:30:33 -07001452 if (likely(pages))
1453 pages++;
Andrea Arcangelif0818f42015-02-11 15:27:17 -08001454 start += PAGE_SIZE;
1455 }
Al Viroe7167122017-11-19 11:32:05 -05001456 if (lock_dropped && *locked) {
Andrea Arcangelif0818f42015-02-11 15:27:17 -08001457 /*
1458 * We must let the caller know we temporarily dropped the lock
1459 * and so the critical section protected by it was lost.
1460 */
Michel Lespinassed8ed45c2020-06-08 21:33:25 -07001461 mmap_read_unlock(mm);
Andrea Arcangelif0818f42015-02-11 15:27:17 -08001462 *locked = 0;
1463 }
1464 return pages_done;
1465}
1466
Christoph Hellwigd3649f62019-07-11 20:57:18 -07001467/**
1468 * populate_vma_page_range() - populate a range of pages in the vma.
1469 * @vma: target vma
1470 * @start: start address
1471 * @end: end address
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -07001472 * @locked: whether the mmap_lock is still held
Christoph Hellwigd3649f62019-07-11 20:57:18 -07001473 *
1474 * This takes care of mlocking the pages too if VM_LOCKED is set.
1475 *
Tang Yizhou0a36f7f2020-08-06 23:20:01 -07001476 * Return either number of pages pinned in the vma, or a negative error
1477 * code on error.
Christoph Hellwigd3649f62019-07-11 20:57:18 -07001478 *
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -07001479 * vma->vm_mm->mmap_lock must be held.
Christoph Hellwigd3649f62019-07-11 20:57:18 -07001480 *
Peter Xu4f6da932020-04-01 21:07:58 -07001481 * If @locked is NULL, it may be held for read or write and will
Christoph Hellwigd3649f62019-07-11 20:57:18 -07001482 * be unperturbed.
1483 *
Peter Xu4f6da932020-04-01 21:07:58 -07001484 * If @locked is non-NULL, it must held for read only and may be
1485 * released. If it's released, *@locked will be set to 0.
Christoph Hellwigd3649f62019-07-11 20:57:18 -07001486 */
1487long populate_vma_page_range(struct vm_area_struct *vma,
Peter Xu4f6da932020-04-01 21:07:58 -07001488 unsigned long start, unsigned long end, int *locked)
Christoph Hellwigd3649f62019-07-11 20:57:18 -07001489{
1490 struct mm_struct *mm = vma->vm_mm;
1491 unsigned long nr_pages = (end - start) / PAGE_SIZE;
1492 int gup_flags;
1493
Miaohe Linbe51eb12021-09-02 14:53:45 -07001494 VM_BUG_ON(!PAGE_ALIGNED(start));
1495 VM_BUG_ON(!PAGE_ALIGNED(end));
Christoph Hellwigd3649f62019-07-11 20:57:18 -07001496 VM_BUG_ON_VMA(start < vma->vm_start, vma);
1497 VM_BUG_ON_VMA(end > vma->vm_end, vma);
Michel Lespinasse42fc5412020-06-08 21:33:44 -07001498 mmap_assert_locked(mm);
Christoph Hellwigd3649f62019-07-11 20:57:18 -07001499
1500 gup_flags = FOLL_TOUCH | FOLL_POPULATE | FOLL_MLOCK;
1501 if (vma->vm_flags & VM_LOCKONFAULT)
1502 gup_flags &= ~FOLL_POPULATE;
1503 /*
1504 * We want to touch writable mappings with a write fault in order
1505 * to break COW, except for shared mappings because these don't COW
1506 * and we would not want to dirty them for nothing.
1507 */
1508 if ((vma->vm_flags & (VM_WRITE | VM_SHARED)) == VM_WRITE)
1509 gup_flags |= FOLL_WRITE;
1510
1511 /*
1512 * We want mlock to succeed for regions that have any permissions
1513 * other than PROT_NONE.
1514 */
Anshuman Khandual3122e802020-04-06 20:03:47 -07001515 if (vma_is_accessible(vma))
Christoph Hellwigd3649f62019-07-11 20:57:18 -07001516 gup_flags |= FOLL_FORCE;
1517
1518 /*
1519 * We made sure addr is within a VMA, so the following will
1520 * not result in a stack expansion that recurses back here.
1521 */
Peter Xu64019a22020-08-11 18:39:01 -07001522 return __get_user_pages(mm, start, nr_pages, gup_flags,
Peter Xu4f6da932020-04-01 21:07:58 -07001523 NULL, NULL, locked);
Christoph Hellwigd3649f62019-07-11 20:57:18 -07001524}
1525
1526/*
David Hildenbrand4ca9b3852021-06-30 18:52:28 -07001527 * faultin_vma_page_range() - populate (prefault) page tables inside the
1528 * given VMA range readable/writable
1529 *
1530 * This takes care of mlocking the pages, too, if VM_LOCKED is set.
1531 *
1532 * @vma: target vma
1533 * @start: start address
1534 * @end: end address
1535 * @write: whether to prefault readable or writable
1536 * @locked: whether the mmap_lock is still held
1537 *
1538 * Returns either number of processed pages in the vma, or a negative error
1539 * code on error (see __get_user_pages()).
1540 *
1541 * vma->vm_mm->mmap_lock must be held. The range must be page-aligned and
1542 * covered by the VMA.
1543 *
1544 * If @locked is NULL, it may be held for read or write and will be unperturbed.
1545 *
1546 * If @locked is non-NULL, it must held for read only and may be released. If
1547 * it's released, *@locked will be set to 0.
1548 */
1549long faultin_vma_page_range(struct vm_area_struct *vma, unsigned long start,
1550 unsigned long end, bool write, int *locked)
1551{
1552 struct mm_struct *mm = vma->vm_mm;
1553 unsigned long nr_pages = (end - start) / PAGE_SIZE;
1554 int gup_flags;
1555
1556 VM_BUG_ON(!PAGE_ALIGNED(start));
1557 VM_BUG_ON(!PAGE_ALIGNED(end));
1558 VM_BUG_ON_VMA(start < vma->vm_start, vma);
1559 VM_BUG_ON_VMA(end > vma->vm_end, vma);
1560 mmap_assert_locked(mm);
1561
1562 /*
1563 * FOLL_TOUCH: Mark page accessed and thereby young; will also mark
1564 * the page dirty with FOLL_WRITE -- which doesn't make a
1565 * difference with !FOLL_FORCE, because the page is writable
1566 * in the page table.
1567 * FOLL_HWPOISON: Return -EHWPOISON instead of -EFAULT when we hit
1568 * a poisoned page.
1569 * FOLL_POPULATE: Always populate memory with VM_LOCKONFAULT.
1570 * !FOLL_FORCE: Require proper access permissions.
1571 */
1572 gup_flags = FOLL_TOUCH | FOLL_POPULATE | FOLL_MLOCK | FOLL_HWPOISON;
1573 if (write)
1574 gup_flags |= FOLL_WRITE;
1575
1576 /*
David Hildenbrandeb2faa52021-08-13 16:54:37 -07001577 * We want to report -EINVAL instead of -EFAULT for any permission
1578 * problems or incompatible mappings.
David Hildenbrand4ca9b3852021-06-30 18:52:28 -07001579 */
David Hildenbrandeb2faa52021-08-13 16:54:37 -07001580 if (check_vma_flags(vma, gup_flags))
1581 return -EINVAL;
1582
David Hildenbrand4ca9b3852021-06-30 18:52:28 -07001583 return __get_user_pages(mm, start, nr_pages, gup_flags,
1584 NULL, NULL, locked);
1585}
1586
1587/*
Christoph Hellwigd3649f62019-07-11 20:57:18 -07001588 * __mm_populate - populate and/or mlock pages within a range of address space.
1589 *
1590 * This is used to implement mlock() and the MAP_POPULATE / MAP_LOCKED mmap
1591 * flags. VMAs must be already marked with the desired vm_flags, and
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -07001592 * mmap_lock must not be held.
Christoph Hellwigd3649f62019-07-11 20:57:18 -07001593 */
1594int __mm_populate(unsigned long start, unsigned long len, int ignore_errors)
1595{
1596 struct mm_struct *mm = current->mm;
1597 unsigned long end, nstart, nend;
1598 struct vm_area_struct *vma = NULL;
1599 int locked = 0;
1600 long ret = 0;
1601
1602 end = start + len;
1603
1604 for (nstart = start; nstart < end; nstart = nend) {
1605 /*
1606 * We want to fault in pages for [nstart; end) address range.
1607 * Find first corresponding VMA.
1608 */
1609 if (!locked) {
1610 locked = 1;
Michel Lespinassed8ed45c2020-06-08 21:33:25 -07001611 mmap_read_lock(mm);
Christoph Hellwigd3649f62019-07-11 20:57:18 -07001612 vma = find_vma(mm, nstart);
1613 } else if (nstart >= vma->vm_end)
1614 vma = vma->vm_next;
1615 if (!vma || vma->vm_start >= end)
1616 break;
1617 /*
1618 * Set [nstart; nend) to intersection of desired address
1619 * range with the first VMA. Also, skip undesirable VMA types.
1620 */
1621 nend = min(end, vma->vm_end);
1622 if (vma->vm_flags & (VM_IO | VM_PFNMAP))
1623 continue;
1624 if (nstart < vma->vm_start)
1625 nstart = vma->vm_start;
1626 /*
1627 * Now fault in a range of pages. populate_vma_page_range()
1628 * double checks the vma flags, so that it won't mlock pages
1629 * if the vma was already munlocked.
1630 */
1631 ret = populate_vma_page_range(vma, nstart, nend, &locked);
1632 if (ret < 0) {
1633 if (ignore_errors) {
1634 ret = 0;
1635 continue; /* continue at next VMA */
1636 }
1637 break;
1638 }
1639 nend = nstart + ret * PAGE_SIZE;
1640 ret = 0;
1641 }
1642 if (locked)
Michel Lespinassed8ed45c2020-06-08 21:33:25 -07001643 mmap_read_unlock(mm);
Christoph Hellwigd3649f62019-07-11 20:57:18 -07001644 return ret; /* 0 or negative error code */
1645}
Christoph Hellwig050a9ad2019-07-11 20:57:21 -07001646#else /* CONFIG_MMU */
Peter Xu64019a22020-08-11 18:39:01 -07001647static long __get_user_pages_locked(struct mm_struct *mm, unsigned long start,
Christoph Hellwig050a9ad2019-07-11 20:57:21 -07001648 unsigned long nr_pages, struct page **pages,
1649 struct vm_area_struct **vmas, int *locked,
1650 unsigned int foll_flags)
1651{
1652 struct vm_area_struct *vma;
1653 unsigned long vm_flags;
Pavel Tatashin24dc20c2021-05-04 18:39:15 -07001654 long i;
Christoph Hellwig050a9ad2019-07-11 20:57:21 -07001655
1656 /* calculate required read or write permissions.
1657 * If FOLL_FORCE is set, we only require the "MAY" flags.
1658 */
1659 vm_flags = (foll_flags & FOLL_WRITE) ?
1660 (VM_WRITE | VM_MAYWRITE) : (VM_READ | VM_MAYREAD);
1661 vm_flags &= (foll_flags & FOLL_FORCE) ?
1662 (VM_MAYREAD | VM_MAYWRITE) : (VM_READ | VM_WRITE);
1663
1664 for (i = 0; i < nr_pages; i++) {
1665 vma = find_vma(mm, start);
1666 if (!vma)
1667 goto finish_or_fault;
1668
1669 /* protect what we can, including chardevs */
1670 if ((vma->vm_flags & (VM_IO | VM_PFNMAP)) ||
1671 !(vm_flags & vma->vm_flags))
1672 goto finish_or_fault;
1673
1674 if (pages) {
1675 pages[i] = virt_to_page(start);
1676 if (pages[i])
1677 get_page(pages[i]);
1678 }
1679 if (vmas)
1680 vmas[i] = vma;
1681 start = (start + PAGE_SIZE) & PAGE_MASK;
1682 }
1683
1684 return i;
1685
1686finish_or_fault:
1687 return i ? : -EFAULT;
1688}
1689#endif /* !CONFIG_MMU */
Christoph Hellwigd3649f62019-07-11 20:57:18 -07001690
Jann Horn8f942ee2020-10-15 20:12:40 -07001691/**
Andreas Gruenbacherbb523b42021-08-02 13:44:20 +02001692 * fault_in_writeable - fault in userspace address range for writing
1693 * @uaddr: start of address range
1694 * @size: size of address range
1695 *
1696 * Returns the number of bytes not faulted in (like copy_to_user() and
1697 * copy_from_user()).
1698 */
1699size_t fault_in_writeable(char __user *uaddr, size_t size)
1700{
1701 char __user *start = uaddr, *end;
1702
1703 if (unlikely(size == 0))
1704 return 0;
Christophe Leroy677b2a82022-01-14 14:05:13 -08001705 if (!user_write_access_begin(uaddr, size))
1706 return size;
Andreas Gruenbacherbb523b42021-08-02 13:44:20 +02001707 if (!PAGE_ALIGNED(uaddr)) {
Christophe Leroy677b2a82022-01-14 14:05:13 -08001708 unsafe_put_user(0, uaddr, out);
Andreas Gruenbacherbb523b42021-08-02 13:44:20 +02001709 uaddr = (char __user *)PAGE_ALIGN((unsigned long)uaddr);
1710 }
1711 end = (char __user *)PAGE_ALIGN((unsigned long)start + size);
1712 if (unlikely(end < start))
1713 end = NULL;
1714 while (uaddr != end) {
Christophe Leroy677b2a82022-01-14 14:05:13 -08001715 unsafe_put_user(0, uaddr, out);
Andreas Gruenbacherbb523b42021-08-02 13:44:20 +02001716 uaddr += PAGE_SIZE;
1717 }
1718
1719out:
Christophe Leroy677b2a82022-01-14 14:05:13 -08001720 user_write_access_end();
Andreas Gruenbacherbb523b42021-08-02 13:44:20 +02001721 if (size > uaddr - start)
1722 return size - (uaddr - start);
1723 return 0;
1724}
1725EXPORT_SYMBOL(fault_in_writeable);
1726
Andreas Gruenbachercdd591f2021-07-05 17:26:28 +02001727/*
1728 * fault_in_safe_writeable - fault in an address range for writing
1729 * @uaddr: start of address range
1730 * @size: length of address range
1731 *
1732 * Faults in an address range using get_user_pages, i.e., without triggering
1733 * hardware page faults. This is primarily useful when we already know that
1734 * some or all of the pages in the address range aren't in memory.
1735 *
1736 * Other than fault_in_writeable(), this function is non-destructive.
1737 *
1738 * Note that we don't pin or otherwise hold the pages referenced that we fault
1739 * in. There's no guarantee that they'll stay in memory for any duration of
1740 * time.
1741 *
1742 * Returns the number of bytes not faulted in, like copy_to_user() and
1743 * copy_from_user().
1744 */
1745size_t fault_in_safe_writeable(const char __user *uaddr, size_t size)
1746{
1747 unsigned long start = (unsigned long)untagged_addr(uaddr);
1748 unsigned long end, nstart, nend;
1749 struct mm_struct *mm = current->mm;
1750 struct vm_area_struct *vma = NULL;
1751 int locked = 0;
1752
1753 nstart = start & PAGE_MASK;
1754 end = PAGE_ALIGN(start + size);
1755 if (end < nstart)
1756 end = 0;
1757 for (; nstart != end; nstart = nend) {
1758 unsigned long nr_pages;
1759 long ret;
1760
1761 if (!locked) {
1762 locked = 1;
1763 mmap_read_lock(mm);
1764 vma = find_vma(mm, nstart);
1765 } else if (nstart >= vma->vm_end)
1766 vma = vma->vm_next;
1767 if (!vma || vma->vm_start >= end)
1768 break;
1769 nend = end ? min(end, vma->vm_end) : vma->vm_end;
1770 if (vma->vm_flags & (VM_IO | VM_PFNMAP))
1771 continue;
1772 if (nstart < vma->vm_start)
1773 nstart = vma->vm_start;
1774 nr_pages = (nend - nstart) / PAGE_SIZE;
1775 ret = __get_user_pages_locked(mm, nstart, nr_pages,
1776 NULL, NULL, &locked,
1777 FOLL_TOUCH | FOLL_WRITE);
1778 if (ret <= 0)
1779 break;
1780 nend = nstart + ret * PAGE_SIZE;
1781 }
1782 if (locked)
1783 mmap_read_unlock(mm);
1784 if (nstart == end)
1785 return 0;
1786 return size - min_t(size_t, nstart - start, size);
1787}
1788EXPORT_SYMBOL(fault_in_safe_writeable);
1789
Andreas Gruenbacherbb523b42021-08-02 13:44:20 +02001790/**
1791 * fault_in_readable - fault in userspace address range for reading
1792 * @uaddr: start of user address range
1793 * @size: size of user address range
1794 *
1795 * Returns the number of bytes not faulted in (like copy_to_user() and
1796 * copy_from_user()).
1797 */
1798size_t fault_in_readable(const char __user *uaddr, size_t size)
1799{
1800 const char __user *start = uaddr, *end;
1801 volatile char c;
1802
1803 if (unlikely(size == 0))
1804 return 0;
Christophe Leroy677b2a82022-01-14 14:05:13 -08001805 if (!user_read_access_begin(uaddr, size))
1806 return size;
Andreas Gruenbacherbb523b42021-08-02 13:44:20 +02001807 if (!PAGE_ALIGNED(uaddr)) {
Christophe Leroy677b2a82022-01-14 14:05:13 -08001808 unsafe_get_user(c, uaddr, out);
Andreas Gruenbacherbb523b42021-08-02 13:44:20 +02001809 uaddr = (const char __user *)PAGE_ALIGN((unsigned long)uaddr);
1810 }
1811 end = (const char __user *)PAGE_ALIGN((unsigned long)start + size);
1812 if (unlikely(end < start))
1813 end = NULL;
1814 while (uaddr != end) {
Christophe Leroy677b2a82022-01-14 14:05:13 -08001815 unsafe_get_user(c, uaddr, out);
Andreas Gruenbacherbb523b42021-08-02 13:44:20 +02001816 uaddr += PAGE_SIZE;
1817 }
1818
1819out:
Christophe Leroy677b2a82022-01-14 14:05:13 -08001820 user_read_access_end();
Andreas Gruenbacherbb523b42021-08-02 13:44:20 +02001821 (void)c;
1822 if (size > uaddr - start)
1823 return size - (uaddr - start);
1824 return 0;
1825}
1826EXPORT_SYMBOL(fault_in_readable);
1827
1828/**
Jann Horn8f942ee2020-10-15 20:12:40 -07001829 * get_dump_page() - pin user page in memory while writing it to core dump
1830 * @addr: user address
1831 *
1832 * Returns struct page pointer of user page pinned for dump,
1833 * to be freed afterwards by put_page().
1834 *
1835 * Returns NULL on any kind of failure - a hole must then be inserted into
1836 * the corefile, to preserve alignment with its headers; and also returns
1837 * NULL wherever the ZERO_PAGE, or an anonymous pte_none, has been found -
Ingo Molnarf0953a12021-05-06 18:06:47 -07001838 * allowing a hole to be left in the corefile to save disk space.
Jann Horn8f942ee2020-10-15 20:12:40 -07001839 *
Jann Horn7f3bfab2020-10-15 20:12:57 -07001840 * Called without mmap_lock (takes and releases the mmap_lock by itself).
Jann Horn8f942ee2020-10-15 20:12:40 -07001841 */
1842#ifdef CONFIG_ELF_CORE
1843struct page *get_dump_page(unsigned long addr)
1844{
Jann Horn7f3bfab2020-10-15 20:12:57 -07001845 struct mm_struct *mm = current->mm;
Jann Horn8f942ee2020-10-15 20:12:40 -07001846 struct page *page;
Jann Horn7f3bfab2020-10-15 20:12:57 -07001847 int locked = 1;
1848 int ret;
Jann Horn8f942ee2020-10-15 20:12:40 -07001849
Jann Horn7f3bfab2020-10-15 20:12:57 -07001850 if (mmap_read_lock_killable(mm))
Jann Horn8f942ee2020-10-15 20:12:40 -07001851 return NULL;
Jann Horn7f3bfab2020-10-15 20:12:57 -07001852 ret = __get_user_pages_locked(mm, addr, 1, &page, NULL, &locked,
1853 FOLL_FORCE | FOLL_DUMP | FOLL_GET);
1854 if (locked)
1855 mmap_read_unlock(mm);
1856 return (ret == 1) ? page : NULL;
Jann Horn8f942ee2020-10-15 20:12:40 -07001857}
1858#endif /* CONFIG_ELF_CORE */
1859
Pavel Tatashind1e153f2021-05-04 18:39:08 -07001860#ifdef CONFIG_MIGRATION
Pavel Tatashinf68749e2021-05-04 18:39:19 -07001861/*
1862 * Check whether all pages are pinnable, if so return number of pages. If some
1863 * pages are not pinnable, migrate them, and unpin all pages. Return zero if
1864 * pages were migrated, or if some pages were not successfully isolated.
1865 * Return negative error if migration fails.
1866 */
1867static long check_and_migrate_movable_pages(unsigned long nr_pages,
Pavel Tatashind1e153f2021-05-04 18:39:08 -07001868 struct page **pages,
Pavel Tatashind1e153f2021-05-04 18:39:08 -07001869 unsigned int gup_flags)
Aneesh Kumar K.V9a4e9f32019-03-05 15:47:44 -08001870{
Pavel Tatashinf68749e2021-05-04 18:39:19 -07001871 unsigned long i;
1872 unsigned long isolation_error_count = 0;
1873 bool drain_allow = true;
Pavel Tatashind1e153f2021-05-04 18:39:08 -07001874 LIST_HEAD(movable_page_list);
Pavel Tatashinf68749e2021-05-04 18:39:19 -07001875 long ret = 0;
1876 struct page *prev_head = NULL;
1877 struct page *head;
Joonsoo Kimed03d922020-08-11 18:37:41 -07001878 struct migration_target_control mtc = {
1879 .nid = NUMA_NO_NODE,
Pavel Tatashinc991ffe2021-05-04 18:38:38 -07001880 .gfp_mask = GFP_USER | __GFP_NOWARN,
Joonsoo Kimed03d922020-08-11 18:37:41 -07001881 };
Aneesh Kumar K.V9a4e9f32019-03-05 15:47:44 -08001882
Pavel Tatashin83c02c22021-05-04 18:38:42 -07001883 for (i = 0; i < nr_pages; i++) {
1884 head = compound_head(pages[i]);
1885 if (head == prev_head)
1886 continue;
1887 prev_head = head;
Aneesh Kumar K.V9a4e9f32019-03-05 15:47:44 -08001888 /*
Pavel Tatashind1e153f2021-05-04 18:39:08 -07001889 * If we get a movable page, since we are going to be pinning
1890 * these entries, try to move them out if possible.
Aneesh Kumar K.V9a4e9f32019-03-05 15:47:44 -08001891 */
Pavel Tatashind1e153f2021-05-04 18:39:08 -07001892 if (!is_pinnable_page(head)) {
Pavel Tatashin6e7f34e2021-05-04 18:38:49 -07001893 if (PageHuge(head)) {
Pavel Tatashind1e153f2021-05-04 18:39:08 -07001894 if (!isolate_huge_page(head, &movable_page_list))
Pavel Tatashin6e7f34e2021-05-04 18:38:49 -07001895 isolation_error_count++;
1896 } else {
Aneesh Kumar K.V9a4e9f32019-03-05 15:47:44 -08001897 if (!PageLRU(head) && drain_allow) {
1898 lru_add_drain_all();
1899 drain_allow = false;
1900 }
1901
Pavel Tatashin6e7f34e2021-05-04 18:38:49 -07001902 if (isolate_lru_page(head)) {
1903 isolation_error_count++;
1904 continue;
Aneesh Kumar K.V9a4e9f32019-03-05 15:47:44 -08001905 }
Pavel Tatashind1e153f2021-05-04 18:39:08 -07001906 list_add_tail(&head->lru, &movable_page_list);
Pavel Tatashin6e7f34e2021-05-04 18:38:49 -07001907 mod_node_page_state(page_pgdat(head),
1908 NR_ISOLATED_ANON +
1909 page_is_file_lru(head),
1910 thp_nr_pages(head));
Aneesh Kumar K.V9a4e9f32019-03-05 15:47:44 -08001911 }
1912 }
1913 }
1914
Pavel Tatashin6e7f34e2021-05-04 18:38:49 -07001915 /*
1916 * If list is empty, and no isolation errors, means that all pages are
1917 * in the correct zone.
1918 */
Pavel Tatashind1e153f2021-05-04 18:39:08 -07001919 if (list_empty(&movable_page_list) && !isolation_error_count)
Pavel Tatashinf68749e2021-05-04 18:39:19 -07001920 return nr_pages;
Pavel Tatashin6e7f34e2021-05-04 18:38:49 -07001921
Pavel Tatashinf68749e2021-05-04 18:39:19 -07001922 if (gup_flags & FOLL_PIN) {
1923 unpin_user_pages(pages, nr_pages);
1924 } else {
1925 for (i = 0; i < nr_pages; i++)
1926 put_page(pages[i]);
1927 }
Pavel Tatashind1e153f2021-05-04 18:39:08 -07001928 if (!list_empty(&movable_page_list)) {
Pavel Tatashind1e153f2021-05-04 18:39:08 -07001929 ret = migrate_pages(&movable_page_list, alloc_migration_target,
Pavel Tatashinf0f44632021-05-04 18:38:46 -07001930 NULL, (unsigned long)&mtc, MIGRATE_SYNC,
Yang Shi5ac95882021-09-02 14:59:13 -07001931 MR_LONGTERM_PIN, NULL);
Pavel Tatashinf68749e2021-05-04 18:39:19 -07001932 if (ret && !list_empty(&movable_page_list))
1933 putback_movable_pages(&movable_page_list);
Aneesh Kumar K.V9a4e9f32019-03-05 15:47:44 -08001934 }
1935
Pavel Tatashinf68749e2021-05-04 18:39:19 -07001936 return ret > 0 ? -ENOMEM : ret;
Aneesh Kumar K.V9a4e9f32019-03-05 15:47:44 -08001937}
1938#else
Pavel Tatashinf68749e2021-05-04 18:39:19 -07001939static long check_and_migrate_movable_pages(unsigned long nr_pages,
Pavel Tatashind1e153f2021-05-04 18:39:08 -07001940 struct page **pages,
Pavel Tatashind1e153f2021-05-04 18:39:08 -07001941 unsigned int gup_flags)
Aneesh Kumar K.V9a4e9f32019-03-05 15:47:44 -08001942{
1943 return nr_pages;
1944}
Pavel Tatashind1e153f2021-05-04 18:39:08 -07001945#endif /* CONFIG_MIGRATION */
Aneesh Kumar K.V9a4e9f32019-03-05 15:47:44 -08001946
Dan Williams2bb6d282017-11-29 16:10:35 -08001947/*
Ira Weiny932f4a62019-05-13 17:17:03 -07001948 * __gup_longterm_locked() is a wrapper for __get_user_pages_locked which
1949 * allows us to process the FOLL_LONGTERM flag.
Dan Williams2bb6d282017-11-29 16:10:35 -08001950 */
Peter Xu64019a22020-08-11 18:39:01 -07001951static long __gup_longterm_locked(struct mm_struct *mm,
Ira Weiny932f4a62019-05-13 17:17:03 -07001952 unsigned long start,
1953 unsigned long nr_pages,
1954 struct page **pages,
1955 struct vm_area_struct **vmas,
1956 unsigned int gup_flags)
Dan Williams2bb6d282017-11-29 16:10:35 -08001957{
Pavel Tatashinf68749e2021-05-04 18:39:19 -07001958 unsigned int flags;
Jason Gunthorpe52650c82020-12-14 19:05:48 -08001959 long rc;
Dan Williams2bb6d282017-11-29 16:10:35 -08001960
Pavel Tatashinf68749e2021-05-04 18:39:19 -07001961 if (!(gup_flags & FOLL_LONGTERM))
1962 return __get_user_pages_locked(mm, start, nr_pages, pages, vmas,
1963 NULL, gup_flags);
1964 flags = memalloc_pin_save();
1965 do {
1966 rc = __get_user_pages_locked(mm, start, nr_pages, pages, vmas,
1967 NULL, gup_flags);
1968 if (rc <= 0)
1969 break;
1970 rc = check_and_migrate_movable_pages(rc, pages, gup_flags);
1971 } while (!rc);
1972 memalloc_pin_restore(flags);
Dan Williams2bb6d282017-11-29 16:10:35 -08001973
Dan Williams2bb6d282017-11-29 16:10:35 -08001974 return rc;
1975}
Ira Weiny932f4a62019-05-13 17:17:03 -07001976
Barry Song447f3e42020-10-13 16:51:58 -07001977static bool is_valid_gup_flags(unsigned int gup_flags)
1978{
1979 /*
1980 * FOLL_PIN must only be set internally by the pin_user_pages*() APIs,
1981 * never directly by the caller, so enforce that with an assertion:
1982 */
1983 if (WARN_ON_ONCE(gup_flags & FOLL_PIN))
1984 return false;
1985 /*
1986 * FOLL_PIN is a prerequisite to FOLL_LONGTERM. Another way of saying
1987 * that is, FOLL_LONGTERM is a specific case, more restrictive case of
1988 * FOLL_PIN.
1989 */
1990 if (WARN_ON_ONCE(gup_flags & FOLL_LONGTERM))
1991 return false;
1992
1993 return true;
1994}
1995
John Hubbard22bf29b2020-04-01 21:05:10 -07001996#ifdef CONFIG_MMU
Peter Xu64019a22020-08-11 18:39:01 -07001997static long __get_user_pages_remote(struct mm_struct *mm,
John Hubbard22bf29b2020-04-01 21:05:10 -07001998 unsigned long start, unsigned long nr_pages,
1999 unsigned int gup_flags, struct page **pages,
2000 struct vm_area_struct **vmas, int *locked)
2001{
2002 /*
2003 * Parts of FOLL_LONGTERM behavior are incompatible with
2004 * FAULT_FLAG_ALLOW_RETRY because of the FS DAX check requirement on
2005 * vmas. However, this only comes up if locked is set, and there are
2006 * callers that do request FOLL_LONGTERM, but do not set locked. So,
2007 * allow what we can.
2008 */
2009 if (gup_flags & FOLL_LONGTERM) {
2010 if (WARN_ON_ONCE(locked))
2011 return -EINVAL;
2012 /*
2013 * This will check the vmas (even if our vmas arg is NULL)
2014 * and return -ENOTSUPP if DAX isn't allowed in this case:
2015 */
Peter Xu64019a22020-08-11 18:39:01 -07002016 return __gup_longterm_locked(mm, start, nr_pages, pages,
John Hubbard22bf29b2020-04-01 21:05:10 -07002017 vmas, gup_flags | FOLL_TOUCH |
2018 FOLL_REMOTE);
2019 }
2020
Peter Xu64019a22020-08-11 18:39:01 -07002021 return __get_user_pages_locked(mm, start, nr_pages, pages, vmas,
John Hubbard22bf29b2020-04-01 21:05:10 -07002022 locked,
2023 gup_flags | FOLL_TOUCH | FOLL_REMOTE);
2024}
2025
Souptick Joarderadc8cb42020-06-01 21:48:24 -07002026/**
John Hubbardc4237f82020-01-30 22:12:36 -08002027 * get_user_pages_remote() - pin user pages in memory
John Hubbardc4237f82020-01-30 22:12:36 -08002028 * @mm: mm_struct of target mm
2029 * @start: starting user address
2030 * @nr_pages: number of pages from start to pin
2031 * @gup_flags: flags modifying lookup behaviour
2032 * @pages: array that receives pointers to the pages pinned.
2033 * Should be at least nr_pages long. Or NULL, if caller
2034 * only intends to ensure the pages are faulted in.
2035 * @vmas: array of pointers to vmas corresponding to each page.
2036 * Or NULL if the caller does not require them.
2037 * @locked: pointer to lock flag indicating whether lock is held and
2038 * subsequently whether VM_FAULT_RETRY functionality can be
2039 * utilised. Lock must initially be held.
2040 *
2041 * Returns either number of pages pinned (which may be less than the
2042 * number requested), or an error. Details about the return value:
2043 *
2044 * -- If nr_pages is 0, returns 0.
2045 * -- If nr_pages is >0, but no pages were pinned, returns -errno.
2046 * -- If nr_pages is >0, and some pages were pinned, returns the number of
2047 * pages pinned. Again, this may be less than nr_pages.
2048 *
2049 * The caller is responsible for releasing returned @pages, via put_page().
2050 *
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -07002051 * @vmas are valid only as long as mmap_lock is held.
John Hubbardc4237f82020-01-30 22:12:36 -08002052 *
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -07002053 * Must be called with mmap_lock held for read or write.
John Hubbardc4237f82020-01-30 22:12:36 -08002054 *
Souptick Joarderadc8cb42020-06-01 21:48:24 -07002055 * get_user_pages_remote walks a process's page tables and takes a reference
2056 * to each struct page that each user address corresponds to at a given
John Hubbardc4237f82020-01-30 22:12:36 -08002057 * instant. That is, it takes the page that would be accessed if a user
2058 * thread accesses the given user virtual address at that instant.
2059 *
2060 * This does not guarantee that the page exists in the user mappings when
Souptick Joarderadc8cb42020-06-01 21:48:24 -07002061 * get_user_pages_remote returns, and there may even be a completely different
John Hubbardc4237f82020-01-30 22:12:36 -08002062 * page there in some cases (eg. if mmapped pagecache has been invalidated
2063 * and subsequently re faulted). However it does guarantee that the page
2064 * won't be freed completely. And mostly callers simply care that the page
2065 * contains data that was valid *at some point in time*. Typically, an IO
2066 * or similar operation cannot guarantee anything stronger anyway because
2067 * locks can't be held over the syscall boundary.
2068 *
2069 * If gup_flags & FOLL_WRITE == 0, the page must not be written to. If the page
2070 * is written to, set_page_dirty (or set_page_dirty_lock, as appropriate) must
2071 * be called after the page is finished with, and before put_page is called.
2072 *
Souptick Joarderadc8cb42020-06-01 21:48:24 -07002073 * get_user_pages_remote is typically used for fewer-copy IO operations,
2074 * to get a handle on the memory by some means other than accesses
2075 * via the user virtual addresses. The pages may be submitted for
2076 * DMA to devices or accessed via their kernel linear mapping (via the
2077 * kmap APIs). Care should be taken to use the correct cache flushing APIs.
John Hubbardc4237f82020-01-30 22:12:36 -08002078 *
2079 * See also get_user_pages_fast, for performance critical applications.
2080 *
Souptick Joarderadc8cb42020-06-01 21:48:24 -07002081 * get_user_pages_remote should be phased out in favor of
John Hubbardc4237f82020-01-30 22:12:36 -08002082 * get_user_pages_locked|unlocked or get_user_pages_fast. Nothing
Souptick Joarderadc8cb42020-06-01 21:48:24 -07002083 * should use get_user_pages_remote because it cannot pass
John Hubbardc4237f82020-01-30 22:12:36 -08002084 * FAULT_FLAG_ALLOW_RETRY to handle_mm_fault.
2085 */
Peter Xu64019a22020-08-11 18:39:01 -07002086long get_user_pages_remote(struct mm_struct *mm,
John Hubbardc4237f82020-01-30 22:12:36 -08002087 unsigned long start, unsigned long nr_pages,
2088 unsigned int gup_flags, struct page **pages,
2089 struct vm_area_struct **vmas, int *locked)
2090{
Barry Song447f3e42020-10-13 16:51:58 -07002091 if (!is_valid_gup_flags(gup_flags))
John Hubbardeddb1c22020-01-30 22:12:54 -08002092 return -EINVAL;
2093
Peter Xu64019a22020-08-11 18:39:01 -07002094 return __get_user_pages_remote(mm, start, nr_pages, gup_flags,
John Hubbard22bf29b2020-04-01 21:05:10 -07002095 pages, vmas, locked);
John Hubbardc4237f82020-01-30 22:12:36 -08002096}
2097EXPORT_SYMBOL(get_user_pages_remote);
2098
John Hubbardeddb1c22020-01-30 22:12:54 -08002099#else /* CONFIG_MMU */
Peter Xu64019a22020-08-11 18:39:01 -07002100long get_user_pages_remote(struct mm_struct *mm,
John Hubbardeddb1c22020-01-30 22:12:54 -08002101 unsigned long start, unsigned long nr_pages,
2102 unsigned int gup_flags, struct page **pages,
2103 struct vm_area_struct **vmas, int *locked)
2104{
2105 return 0;
2106}
John Hubbard3faa52c2020-04-01 21:05:29 -07002107
Peter Xu64019a22020-08-11 18:39:01 -07002108static long __get_user_pages_remote(struct mm_struct *mm,
John Hubbard3faa52c2020-04-01 21:05:29 -07002109 unsigned long start, unsigned long nr_pages,
2110 unsigned int gup_flags, struct page **pages,
2111 struct vm_area_struct **vmas, int *locked)
2112{
2113 return 0;
2114}
John Hubbardeddb1c22020-01-30 22:12:54 -08002115#endif /* !CONFIG_MMU */
2116
Souptick Joarderadc8cb42020-06-01 21:48:24 -07002117/**
2118 * get_user_pages() - pin user pages in memory
2119 * @start: starting user address
2120 * @nr_pages: number of pages from start to pin
2121 * @gup_flags: flags modifying lookup behaviour
2122 * @pages: array that receives pointers to the pages pinned.
2123 * Should be at least nr_pages long. Or NULL, if caller
2124 * only intends to ensure the pages are faulted in.
2125 * @vmas: array of pointers to vmas corresponding to each page.
2126 * Or NULL if the caller does not require them.
2127 *
Peter Xu64019a22020-08-11 18:39:01 -07002128 * This is the same as get_user_pages_remote(), just with a less-flexible
2129 * calling convention where we assume that the mm being operated on belongs to
2130 * the current task, and doesn't allow passing of a locked parameter. We also
2131 * obviously don't pass FOLL_REMOTE in here.
Ira Weiny932f4a62019-05-13 17:17:03 -07002132 */
2133long get_user_pages(unsigned long start, unsigned long nr_pages,
2134 unsigned int gup_flags, struct page **pages,
2135 struct vm_area_struct **vmas)
2136{
Barry Song447f3e42020-10-13 16:51:58 -07002137 if (!is_valid_gup_flags(gup_flags))
John Hubbardeddb1c22020-01-30 22:12:54 -08002138 return -EINVAL;
2139
Peter Xu64019a22020-08-11 18:39:01 -07002140 return __gup_longterm_locked(current->mm, start, nr_pages,
Ira Weiny932f4a62019-05-13 17:17:03 -07002141 pages, vmas, gup_flags | FOLL_TOUCH);
2142}
2143EXPORT_SYMBOL(get_user_pages);
Dan Williams2bb6d282017-11-29 16:10:35 -08002144
Souptick Joarderadc8cb42020-06-01 21:48:24 -07002145/**
Mauro Carvalho Chehaba00cda32020-12-14 19:14:39 -08002146 * get_user_pages_locked() - variant of get_user_pages()
2147 *
2148 * @start: starting user address
2149 * @nr_pages: number of pages from start to pin
2150 * @gup_flags: flags modifying lookup behaviour
2151 * @pages: array that receives pointers to the pages pinned.
2152 * Should be at least nr_pages long. Or NULL, if caller
2153 * only intends to ensure the pages are faulted in.
2154 * @locked: pointer to lock flag indicating whether lock is held and
2155 * subsequently whether VM_FAULT_RETRY functionality can be
2156 * utilised. Lock must initially be held.
2157 *
2158 * It is suitable to replace the form:
Kirill A. Shutemovacc3c8d2015-04-14 15:44:45 -07002159 *
Michel Lespinasse3e4e28c2020-06-08 21:33:51 -07002160 * mmap_read_lock(mm);
Christoph Hellwigd3649f62019-07-11 20:57:18 -07002161 * do_something()
Peter Xu64019a22020-08-11 18:39:01 -07002162 * get_user_pages(mm, ..., pages, NULL);
Michel Lespinasse3e4e28c2020-06-08 21:33:51 -07002163 * mmap_read_unlock(mm);
Kirill A. Shutemovacc3c8d2015-04-14 15:44:45 -07002164 *
Christoph Hellwigd3649f62019-07-11 20:57:18 -07002165 * to:
Kirill A. Shutemovacc3c8d2015-04-14 15:44:45 -07002166 *
Christoph Hellwigd3649f62019-07-11 20:57:18 -07002167 * int locked = 1;
Michel Lespinasse3e4e28c2020-06-08 21:33:51 -07002168 * mmap_read_lock(mm);
Christoph Hellwigd3649f62019-07-11 20:57:18 -07002169 * do_something()
Peter Xu64019a22020-08-11 18:39:01 -07002170 * get_user_pages_locked(mm, ..., pages, &locked);
Christoph Hellwigd3649f62019-07-11 20:57:18 -07002171 * if (locked)
Michel Lespinasse3e4e28c2020-06-08 21:33:51 -07002172 * mmap_read_unlock(mm);
Souptick Joarderadc8cb42020-06-01 21:48:24 -07002173 *
Souptick Joarderadc8cb42020-06-01 21:48:24 -07002174 * We can leverage the VM_FAULT_RETRY functionality in the page fault
2175 * paths better by using either get_user_pages_locked() or
2176 * get_user_pages_unlocked().
2177 *
Kirill A. Shutemovacc3c8d2015-04-14 15:44:45 -07002178 */
Christoph Hellwigd3649f62019-07-11 20:57:18 -07002179long get_user_pages_locked(unsigned long start, unsigned long nr_pages,
2180 unsigned int gup_flags, struct page **pages,
2181 int *locked)
Kirill A. Shutemovacc3c8d2015-04-14 15:44:45 -07002182{
Kirill A. Shutemovacc3c8d2015-04-14 15:44:45 -07002183 /*
Christoph Hellwigd3649f62019-07-11 20:57:18 -07002184 * FIXME: Current FOLL_LONGTERM behavior is incompatible with
2185 * FAULT_FLAG_ALLOW_RETRY because of the FS DAX check requirement on
2186 * vmas. As there are no users of this flag in this call we simply
2187 * disallow this option for now.
Kirill A. Shutemovacc3c8d2015-04-14 15:44:45 -07002188 */
Christoph Hellwigd3649f62019-07-11 20:57:18 -07002189 if (WARN_ON_ONCE(gup_flags & FOLL_LONGTERM))
2190 return -EINVAL;
John Hubbard420c2092020-06-07 21:41:02 -07002191 /*
2192 * FOLL_PIN must only be set internally by the pin_user_pages*() APIs,
2193 * never directly by the caller, so enforce that:
2194 */
2195 if (WARN_ON_ONCE(gup_flags & FOLL_PIN))
2196 return -EINVAL;
Kirill A. Shutemovacc3c8d2015-04-14 15:44:45 -07002197
Peter Xu64019a22020-08-11 18:39:01 -07002198 return __get_user_pages_locked(current->mm, start, nr_pages,
Christoph Hellwigd3649f62019-07-11 20:57:18 -07002199 pages, NULL, locked,
2200 gup_flags | FOLL_TOUCH);
Kirill A. Shutemovacc3c8d2015-04-14 15:44:45 -07002201}
Christoph Hellwigd3649f62019-07-11 20:57:18 -07002202EXPORT_SYMBOL(get_user_pages_locked);
Kirill A. Shutemovacc3c8d2015-04-14 15:44:45 -07002203
2204/*
Christoph Hellwigd3649f62019-07-11 20:57:18 -07002205 * get_user_pages_unlocked() is suitable to replace the form:
Kirill A. Shutemovacc3c8d2015-04-14 15:44:45 -07002206 *
Michel Lespinasse3e4e28c2020-06-08 21:33:51 -07002207 * mmap_read_lock(mm);
Peter Xu64019a22020-08-11 18:39:01 -07002208 * get_user_pages(mm, ..., pages, NULL);
Michel Lespinasse3e4e28c2020-06-08 21:33:51 -07002209 * mmap_read_unlock(mm);
Christoph Hellwigd3649f62019-07-11 20:57:18 -07002210 *
2211 * with:
2212 *
Peter Xu64019a22020-08-11 18:39:01 -07002213 * get_user_pages_unlocked(mm, ..., pages);
Christoph Hellwigd3649f62019-07-11 20:57:18 -07002214 *
2215 * It is functionally equivalent to get_user_pages_fast so
2216 * get_user_pages_fast should be used instead if specific gup_flags
2217 * (e.g. FOLL_FORCE) are not required.
Kirill A. Shutemovacc3c8d2015-04-14 15:44:45 -07002218 */
Christoph Hellwigd3649f62019-07-11 20:57:18 -07002219long get_user_pages_unlocked(unsigned long start, unsigned long nr_pages,
2220 struct page **pages, unsigned int gup_flags)
Kirill A. Shutemovacc3c8d2015-04-14 15:44:45 -07002221{
2222 struct mm_struct *mm = current->mm;
Christoph Hellwigd3649f62019-07-11 20:57:18 -07002223 int locked = 1;
2224 long ret;
Kirill A. Shutemovacc3c8d2015-04-14 15:44:45 -07002225
Christoph Hellwigd3649f62019-07-11 20:57:18 -07002226 /*
2227 * FIXME: Current FOLL_LONGTERM behavior is incompatible with
2228 * FAULT_FLAG_ALLOW_RETRY because of the FS DAX check requirement on
2229 * vmas. As there are no users of this flag in this call we simply
2230 * disallow this option for now.
2231 */
2232 if (WARN_ON_ONCE(gup_flags & FOLL_LONGTERM))
2233 return -EINVAL;
Kirill A. Shutemovacc3c8d2015-04-14 15:44:45 -07002234
Michel Lespinassed8ed45c2020-06-08 21:33:25 -07002235 mmap_read_lock(mm);
Peter Xu64019a22020-08-11 18:39:01 -07002236 ret = __get_user_pages_locked(mm, start, nr_pages, pages, NULL,
Christoph Hellwigd3649f62019-07-11 20:57:18 -07002237 &locked, gup_flags | FOLL_TOUCH);
Kirill A. Shutemovacc3c8d2015-04-14 15:44:45 -07002238 if (locked)
Michel Lespinassed8ed45c2020-06-08 21:33:25 -07002239 mmap_read_unlock(mm);
Christoph Hellwigd3649f62019-07-11 20:57:18 -07002240 return ret;
Kirill A. Shutemovacc3c8d2015-04-14 15:44:45 -07002241}
Christoph Hellwigd3649f62019-07-11 20:57:18 -07002242EXPORT_SYMBOL(get_user_pages_unlocked);
Steve Capper2667f502014-10-09 15:29:14 -07002243
2244/*
Christoph Hellwig67a929e2019-07-11 20:57:14 -07002245 * Fast GUP
Steve Capper2667f502014-10-09 15:29:14 -07002246 *
2247 * get_user_pages_fast attempts to pin user pages by walking the page
2248 * tables directly and avoids taking locks. Thus the walker needs to be
2249 * protected from page table pages being freed from under it, and should
2250 * block any THP splits.
2251 *
2252 * One way to achieve this is to have the walker disable interrupts, and
2253 * rely on IPIs from the TLB flushing code blocking before the page table
2254 * pages are freed. This is unsuitable for architectures that do not need
2255 * to broadcast an IPI when invalidating TLBs.
2256 *
2257 * Another way to achieve this is to batch up page table containing pages
2258 * belonging to more than one mm_user, then rcu_sched a callback to free those
2259 * pages. Disabling interrupts will allow the fast_gup walker to both block
2260 * the rcu_sched callback, and an IPI that we broadcast for splitting THPs
2261 * (which is a relatively rare event). The code below adopts this strategy.
2262 *
2263 * Before activating this code, please be aware that the following assumptions
2264 * are currently made:
2265 *
Peter Zijlstraff2e6d722020-02-03 17:37:02 -08002266 * *) Either MMU_GATHER_RCU_TABLE_FREE is enabled, and tlb_remove_table() is used to
Kirill A. Shutemove5855132017-06-06 14:31:20 +03002267 * free pages containing page tables or TLB flushing requires IPI broadcast.
Steve Capper2667f502014-10-09 15:29:14 -07002268 *
Steve Capper2667f502014-10-09 15:29:14 -07002269 * *) ptes can be read atomically by the architecture.
2270 *
2271 * *) access_ok is sufficient to validate userspace address ranges.
2272 *
2273 * The last two assumptions can be relaxed by the addition of helper functions.
2274 *
2275 * This code is based heavily on the PowerPC implementation by Nick Piggin.
2276 */
Christoph Hellwig67a929e2019-07-11 20:57:14 -07002277#ifdef CONFIG_HAVE_FAST_GUP
John Hubbard3faa52c2020-04-01 21:05:29 -07002278
Guenter Roeck790c7362019-07-11 20:57:46 -07002279static void __maybe_unused undo_dev_pagemap(int *nr, int nr_start,
John Hubbard3b78d832020-04-01 21:05:22 -07002280 unsigned int flags,
Guenter Roeck790c7362019-07-11 20:57:46 -07002281 struct page **pages)
Kirill A. Shutemovb59f65f2017-03-16 18:26:53 +03002282{
2283 while ((*nr) - nr_start) {
2284 struct page *page = pages[--(*nr)];
2285
2286 ClearPageReferenced(page);
John Hubbard3faa52c2020-04-01 21:05:29 -07002287 if (flags & FOLL_PIN)
2288 unpin_user_page(page);
2289 else
2290 put_page(page);
Kirill A. Shutemovb59f65f2017-03-16 18:26:53 +03002291 }
2292}
2293
Laurent Dufour3010a5e2018-06-07 17:06:08 -07002294#ifdef CONFIG_ARCH_HAS_PTE_SPECIAL
Steve Capper2667f502014-10-09 15:29:14 -07002295static int gup_pte_range(pmd_t pmd, unsigned long addr, unsigned long end,
Ira Weinyb798bec2019-05-13 17:17:07 -07002296 unsigned int flags, struct page **pages, int *nr)
Steve Capper2667f502014-10-09 15:29:14 -07002297{
Kirill A. Shutemovb59f65f2017-03-16 18:26:53 +03002298 struct dev_pagemap *pgmap = NULL;
2299 int nr_start = *nr, ret = 0;
Steve Capper2667f502014-10-09 15:29:14 -07002300 pte_t *ptep, *ptem;
Steve Capper2667f502014-10-09 15:29:14 -07002301
2302 ptem = ptep = pte_offset_map(&pmd, addr);
2303 do {
Peter Zijlstra2a4a06d2020-11-13 11:41:40 +01002304 pte_t pte = ptep_get_lockless(ptep);
Kirill A. Shutemov7aef4172016-01-15 16:52:32 -08002305 struct page *head, *page;
Steve Capper2667f502014-10-09 15:29:14 -07002306
2307 /*
2308 * Similar to the PMD case below, NUMA hinting must take slow
Mel Gorman8a0516e2015-02-12 14:58:22 -08002309 * path using the pte_protnone check.
Steve Capper2667f502014-10-09 15:29:14 -07002310 */
Kirill A. Shutemove7884f82017-03-16 18:26:50 +03002311 if (pte_protnone(pte))
2312 goto pte_unmap;
2313
Ira Weinyb798bec2019-05-13 17:17:07 -07002314 if (!pte_access_permitted(pte, flags & FOLL_WRITE))
Kirill A. Shutemove7884f82017-03-16 18:26:50 +03002315 goto pte_unmap;
2316
Kirill A. Shutemovb59f65f2017-03-16 18:26:53 +03002317 if (pte_devmap(pte)) {
Ira Weiny7af75562019-05-13 17:17:14 -07002318 if (unlikely(flags & FOLL_LONGTERM))
2319 goto pte_unmap;
2320
Kirill A. Shutemovb59f65f2017-03-16 18:26:53 +03002321 pgmap = get_dev_pagemap(pte_pfn(pte), pgmap);
2322 if (unlikely(!pgmap)) {
John Hubbard3b78d832020-04-01 21:05:22 -07002323 undo_dev_pagemap(nr, nr_start, flags, pages);
Kirill A. Shutemovb59f65f2017-03-16 18:26:53 +03002324 goto pte_unmap;
2325 }
2326 } else if (pte_special(pte))
Steve Capper2667f502014-10-09 15:29:14 -07002327 goto pte_unmap;
2328
2329 VM_BUG_ON(!pfn_valid(pte_pfn(pte)));
2330 page = pte_page(pte);
2331
John Hubbard3faa52c2020-04-01 21:05:29 -07002332 head = try_grab_compound_head(page, 1, flags);
Linus Torvalds8fde12c2019-04-11 10:49:19 -07002333 if (!head)
Steve Capper2667f502014-10-09 15:29:14 -07002334 goto pte_unmap;
2335
Mike Rapoport1507f512021-07-07 18:08:03 -07002336 if (unlikely(page_is_secretmem(page))) {
2337 put_compound_head(head, 1, flags);
2338 goto pte_unmap;
2339 }
2340
Steve Capper2667f502014-10-09 15:29:14 -07002341 if (unlikely(pte_val(pte) != pte_val(*ptep))) {
John Hubbard3faa52c2020-04-01 21:05:29 -07002342 put_compound_head(head, 1, flags);
Steve Capper2667f502014-10-09 15:29:14 -07002343 goto pte_unmap;
2344 }
2345
Kirill A. Shutemov7aef4172016-01-15 16:52:32 -08002346 VM_BUG_ON_PAGE(compound_head(page) != head, page);
Kirill A. Shutemove9348052017-03-16 18:26:52 +03002347
Claudio Imbrendaf28d4362020-04-01 21:05:56 -07002348 /*
2349 * We need to make the page accessible if and only if we are
2350 * going to access its content (the FOLL_PIN case). Please
2351 * see Documentation/core-api/pin_user_pages.rst for
2352 * details.
2353 */
2354 if (flags & FOLL_PIN) {
2355 ret = arch_make_page_accessible(page);
2356 if (ret) {
2357 unpin_user_page(page);
2358 goto pte_unmap;
2359 }
2360 }
Kirill A. Shutemove9348052017-03-16 18:26:52 +03002361 SetPageReferenced(page);
Steve Capper2667f502014-10-09 15:29:14 -07002362 pages[*nr] = page;
2363 (*nr)++;
2364
2365 } while (ptep++, addr += PAGE_SIZE, addr != end);
2366
2367 ret = 1;
2368
2369pte_unmap:
Christoph Hellwig832d7aa2017-12-29 08:54:01 +01002370 if (pgmap)
2371 put_dev_pagemap(pgmap);
Steve Capper2667f502014-10-09 15:29:14 -07002372 pte_unmap(ptem);
2373 return ret;
2374}
2375#else
2376
2377/*
2378 * If we can't determine whether or not a pte is special, then fail immediately
2379 * for ptes. Note, we can still pin HugeTLB and THP as these are guaranteed not
2380 * to be special.
2381 *
2382 * For a futex to be placed on a THP tail page, get_futex_key requires a
Souptick Joarderdadbb612020-06-07 21:40:55 -07002383 * get_user_pages_fast_only implementation that can pin pages. Thus it's still
Steve Capper2667f502014-10-09 15:29:14 -07002384 * useful to have gup_huge_pmd even if we can't operate on ptes.
2385 */
2386static int gup_pte_range(pmd_t pmd, unsigned long addr, unsigned long end,
Ira Weinyb798bec2019-05-13 17:17:07 -07002387 unsigned int flags, struct page **pages, int *nr)
Steve Capper2667f502014-10-09 15:29:14 -07002388{
2389 return 0;
2390}
Laurent Dufour3010a5e2018-06-07 17:06:08 -07002391#endif /* CONFIG_ARCH_HAS_PTE_SPECIAL */
Steve Capper2667f502014-10-09 15:29:14 -07002392
Robin Murphy17596732019-07-16 16:30:47 -07002393#if defined(CONFIG_ARCH_HAS_PTE_DEVMAP) && defined(CONFIG_TRANSPARENT_HUGEPAGE)
Kirill A. Shutemovb59f65f2017-03-16 18:26:53 +03002394static int __gup_device_huge(unsigned long pfn, unsigned long addr,
John Hubbard86dfbed2020-04-01 21:05:14 -07002395 unsigned long end, unsigned int flags,
2396 struct page **pages, int *nr)
Kirill A. Shutemovb59f65f2017-03-16 18:26:53 +03002397{
2398 int nr_start = *nr;
2399 struct dev_pagemap *pgmap = NULL;
2400
2401 do {
2402 struct page *page = pfn_to_page(pfn);
2403
2404 pgmap = get_dev_pagemap(pfn, pgmap);
2405 if (unlikely(!pgmap)) {
John Hubbard3b78d832020-04-01 21:05:22 -07002406 undo_dev_pagemap(nr, nr_start, flags, pages);
Miaohe Lin6401c4e2021-09-02 14:53:42 -07002407 break;
Kirill A. Shutemovb59f65f2017-03-16 18:26:53 +03002408 }
2409 SetPageReferenced(page);
2410 pages[*nr] = page;
John Hubbard3faa52c2020-04-01 21:05:29 -07002411 if (unlikely(!try_grab_page(page, flags))) {
2412 undo_dev_pagemap(nr, nr_start, flags, pages);
Miaohe Lin6401c4e2021-09-02 14:53:42 -07002413 break;
John Hubbard3faa52c2020-04-01 21:05:29 -07002414 }
Kirill A. Shutemovb59f65f2017-03-16 18:26:53 +03002415 (*nr)++;
2416 pfn++;
2417 } while (addr += PAGE_SIZE, addr != end);
Christoph Hellwig832d7aa2017-12-29 08:54:01 +01002418
Miaohe Lin6401c4e2021-09-02 14:53:42 -07002419 put_dev_pagemap(pgmap);
John Hubbard20b7fee2021-11-05 13:37:16 -07002420 return addr == end;
Kirill A. Shutemovb59f65f2017-03-16 18:26:53 +03002421}
2422
Dan Williamsa9b6de72018-04-19 21:32:19 -07002423static int __gup_device_huge_pmd(pmd_t orig, pmd_t *pmdp, unsigned long addr,
John Hubbard86dfbed2020-04-01 21:05:14 -07002424 unsigned long end, unsigned int flags,
2425 struct page **pages, int *nr)
Kirill A. Shutemovb59f65f2017-03-16 18:26:53 +03002426{
2427 unsigned long fault_pfn;
Dan Williamsa9b6de72018-04-19 21:32:19 -07002428 int nr_start = *nr;
Kirill A. Shutemovb59f65f2017-03-16 18:26:53 +03002429
Dan Williamsa9b6de72018-04-19 21:32:19 -07002430 fault_pfn = pmd_pfn(orig) + ((addr & ~PMD_MASK) >> PAGE_SHIFT);
John Hubbard86dfbed2020-04-01 21:05:14 -07002431 if (!__gup_device_huge(fault_pfn, addr, end, flags, pages, nr))
Dan Williamsa9b6de72018-04-19 21:32:19 -07002432 return 0;
2433
2434 if (unlikely(pmd_val(orig) != pmd_val(*pmdp))) {
John Hubbard3b78d832020-04-01 21:05:22 -07002435 undo_dev_pagemap(nr, nr_start, flags, pages);
Dan Williamsa9b6de72018-04-19 21:32:19 -07002436 return 0;
2437 }
2438 return 1;
Kirill A. Shutemovb59f65f2017-03-16 18:26:53 +03002439}
2440
Dan Williamsa9b6de72018-04-19 21:32:19 -07002441static int __gup_device_huge_pud(pud_t orig, pud_t *pudp, unsigned long addr,
John Hubbard86dfbed2020-04-01 21:05:14 -07002442 unsigned long end, unsigned int flags,
2443 struct page **pages, int *nr)
Kirill A. Shutemovb59f65f2017-03-16 18:26:53 +03002444{
2445 unsigned long fault_pfn;
Dan Williamsa9b6de72018-04-19 21:32:19 -07002446 int nr_start = *nr;
Kirill A. Shutemovb59f65f2017-03-16 18:26:53 +03002447
Dan Williamsa9b6de72018-04-19 21:32:19 -07002448 fault_pfn = pud_pfn(orig) + ((addr & ~PUD_MASK) >> PAGE_SHIFT);
John Hubbard86dfbed2020-04-01 21:05:14 -07002449 if (!__gup_device_huge(fault_pfn, addr, end, flags, pages, nr))
Dan Williamsa9b6de72018-04-19 21:32:19 -07002450 return 0;
2451
2452 if (unlikely(pud_val(orig) != pud_val(*pudp))) {
John Hubbard3b78d832020-04-01 21:05:22 -07002453 undo_dev_pagemap(nr, nr_start, flags, pages);
Dan Williamsa9b6de72018-04-19 21:32:19 -07002454 return 0;
2455 }
2456 return 1;
Kirill A. Shutemovb59f65f2017-03-16 18:26:53 +03002457}
2458#else
Dan Williamsa9b6de72018-04-19 21:32:19 -07002459static int __gup_device_huge_pmd(pmd_t orig, pmd_t *pmdp, unsigned long addr,
John Hubbard86dfbed2020-04-01 21:05:14 -07002460 unsigned long end, unsigned int flags,
2461 struct page **pages, int *nr)
Kirill A. Shutemovb59f65f2017-03-16 18:26:53 +03002462{
2463 BUILD_BUG();
2464 return 0;
2465}
2466
Dan Williamsa9b6de72018-04-19 21:32:19 -07002467static int __gup_device_huge_pud(pud_t pud, pud_t *pudp, unsigned long addr,
John Hubbard86dfbed2020-04-01 21:05:14 -07002468 unsigned long end, unsigned int flags,
2469 struct page **pages, int *nr)
Kirill A. Shutemovb59f65f2017-03-16 18:26:53 +03002470{
2471 BUILD_BUG();
2472 return 0;
2473}
2474#endif
2475
John Hubbarda43e9822020-01-30 22:12:17 -08002476static int record_subpages(struct page *page, unsigned long addr,
2477 unsigned long end, struct page **pages)
2478{
2479 int nr;
2480
2481 for (nr = 0; addr != end; addr += PAGE_SIZE)
2482 pages[nr++] = page++;
2483
2484 return nr;
2485}
2486
Christoph Hellwigcbd34da2019-07-11 20:57:28 -07002487#ifdef CONFIG_ARCH_HAS_HUGEPD
2488static unsigned long hugepte_addr_end(unsigned long addr, unsigned long end,
2489 unsigned long sz)
2490{
2491 unsigned long __boundary = (addr + sz) & ~(sz-1);
2492 return (__boundary - 1 < end - 1) ? __boundary : end;
2493}
2494
2495static int gup_hugepte(pte_t *ptep, unsigned long sz, unsigned long addr,
John Hubbard0cd22af2019-10-18 20:19:53 -07002496 unsigned long end, unsigned int flags,
2497 struct page **pages, int *nr)
Christoph Hellwigcbd34da2019-07-11 20:57:28 -07002498{
2499 unsigned long pte_end;
2500 struct page *head, *page;
2501 pte_t pte;
2502 int refs;
2503
2504 pte_end = (addr + sz) & ~(sz-1);
2505 if (pte_end < end)
2506 end = pte_end;
2507
Christophe Leroy55ca2262020-06-15 12:57:57 +00002508 pte = huge_ptep_get(ptep);
Christoph Hellwigcbd34da2019-07-11 20:57:28 -07002509
John Hubbard0cd22af2019-10-18 20:19:53 -07002510 if (!pte_access_permitted(pte, flags & FOLL_WRITE))
Christoph Hellwigcbd34da2019-07-11 20:57:28 -07002511 return 0;
2512
2513 /* hugepages are never "special" */
2514 VM_BUG_ON(!pfn_valid(pte_pfn(pte)));
2515
Christoph Hellwigcbd34da2019-07-11 20:57:28 -07002516 head = pte_page(pte);
Christoph Hellwigcbd34da2019-07-11 20:57:28 -07002517 page = head + ((addr & (sz-1)) >> PAGE_SHIFT);
John Hubbarda43e9822020-01-30 22:12:17 -08002518 refs = record_subpages(page, addr, end, pages + *nr);
Christoph Hellwigcbd34da2019-07-11 20:57:28 -07002519
John Hubbard3faa52c2020-04-01 21:05:29 -07002520 head = try_grab_compound_head(head, refs, flags);
John Hubbarda43e9822020-01-30 22:12:17 -08002521 if (!head)
Christoph Hellwigcbd34da2019-07-11 20:57:28 -07002522 return 0;
Christoph Hellwigcbd34da2019-07-11 20:57:28 -07002523
2524 if (unlikely(pte_val(pte) != pte_val(*ptep))) {
John Hubbard3b78d832020-04-01 21:05:22 -07002525 put_compound_head(head, refs, flags);
Christoph Hellwigcbd34da2019-07-11 20:57:28 -07002526 return 0;
2527 }
2528
John Hubbarda43e9822020-01-30 22:12:17 -08002529 *nr += refs;
Christoph Hellwig520b4a42019-07-11 20:57:36 -07002530 SetPageReferenced(head);
Christoph Hellwigcbd34da2019-07-11 20:57:28 -07002531 return 1;
2532}
2533
2534static int gup_huge_pd(hugepd_t hugepd, unsigned long addr,
John Hubbard0cd22af2019-10-18 20:19:53 -07002535 unsigned int pdshift, unsigned long end, unsigned int flags,
Christoph Hellwigcbd34da2019-07-11 20:57:28 -07002536 struct page **pages, int *nr)
2537{
2538 pte_t *ptep;
2539 unsigned long sz = 1UL << hugepd_shift(hugepd);
2540 unsigned long next;
2541
2542 ptep = hugepte_offset(hugepd, addr, pdshift);
2543 do {
2544 next = hugepte_addr_end(addr, end, sz);
John Hubbard0cd22af2019-10-18 20:19:53 -07002545 if (!gup_hugepte(ptep, sz, addr, end, flags, pages, nr))
Christoph Hellwigcbd34da2019-07-11 20:57:28 -07002546 return 0;
2547 } while (ptep++, addr = next, addr != end);
2548
2549 return 1;
2550}
2551#else
2552static inline int gup_huge_pd(hugepd_t hugepd, unsigned long addr,
John Hubbard0cd22af2019-10-18 20:19:53 -07002553 unsigned int pdshift, unsigned long end, unsigned int flags,
Christoph Hellwigcbd34da2019-07-11 20:57:28 -07002554 struct page **pages, int *nr)
2555{
2556 return 0;
2557}
2558#endif /* CONFIG_ARCH_HAS_HUGEPD */
2559
Steve Capper2667f502014-10-09 15:29:14 -07002560static int gup_huge_pmd(pmd_t orig, pmd_t *pmdp, unsigned long addr,
John Hubbard0cd22af2019-10-18 20:19:53 -07002561 unsigned long end, unsigned int flags,
2562 struct page **pages, int *nr)
Steve Capper2667f502014-10-09 15:29:14 -07002563{
Kirill A. Shutemovddc58f22016-01-15 16:52:56 -08002564 struct page *head, *page;
Steve Capper2667f502014-10-09 15:29:14 -07002565 int refs;
2566
Ira Weinyb798bec2019-05-13 17:17:07 -07002567 if (!pmd_access_permitted(orig, flags & FOLL_WRITE))
Steve Capper2667f502014-10-09 15:29:14 -07002568 return 0;
2569
Ira Weiny7af75562019-05-13 17:17:14 -07002570 if (pmd_devmap(orig)) {
2571 if (unlikely(flags & FOLL_LONGTERM))
2572 return 0;
John Hubbard86dfbed2020-04-01 21:05:14 -07002573 return __gup_device_huge_pmd(orig, pmdp, addr, end, flags,
2574 pages, nr);
Ira Weiny7af75562019-05-13 17:17:14 -07002575 }
Kirill A. Shutemovb59f65f2017-03-16 18:26:53 +03002576
Punit Agrawald63206e2017-07-06 15:39:39 -07002577 page = pmd_page(orig) + ((addr & ~PMD_MASK) >> PAGE_SHIFT);
John Hubbarda43e9822020-01-30 22:12:17 -08002578 refs = record_subpages(page, addr, end, pages + *nr);
Steve Capper2667f502014-10-09 15:29:14 -07002579
John Hubbard3faa52c2020-04-01 21:05:29 -07002580 head = try_grab_compound_head(pmd_page(orig), refs, flags);
John Hubbarda43e9822020-01-30 22:12:17 -08002581 if (!head)
Steve Capper2667f502014-10-09 15:29:14 -07002582 return 0;
Steve Capper2667f502014-10-09 15:29:14 -07002583
2584 if (unlikely(pmd_val(orig) != pmd_val(*pmdp))) {
John Hubbard3b78d832020-04-01 21:05:22 -07002585 put_compound_head(head, refs, flags);
Steve Capper2667f502014-10-09 15:29:14 -07002586 return 0;
2587 }
2588
John Hubbarda43e9822020-01-30 22:12:17 -08002589 *nr += refs;
Kirill A. Shutemove9348052017-03-16 18:26:52 +03002590 SetPageReferenced(head);
Steve Capper2667f502014-10-09 15:29:14 -07002591 return 1;
2592}
2593
2594static int gup_huge_pud(pud_t orig, pud_t *pudp, unsigned long addr,
John Hubbard86dfbed2020-04-01 21:05:14 -07002595 unsigned long end, unsigned int flags,
2596 struct page **pages, int *nr)
Steve Capper2667f502014-10-09 15:29:14 -07002597{
Kirill A. Shutemovddc58f22016-01-15 16:52:56 -08002598 struct page *head, *page;
Steve Capper2667f502014-10-09 15:29:14 -07002599 int refs;
2600
Ira Weinyb798bec2019-05-13 17:17:07 -07002601 if (!pud_access_permitted(orig, flags & FOLL_WRITE))
Steve Capper2667f502014-10-09 15:29:14 -07002602 return 0;
2603
Ira Weiny7af75562019-05-13 17:17:14 -07002604 if (pud_devmap(orig)) {
2605 if (unlikely(flags & FOLL_LONGTERM))
2606 return 0;
John Hubbard86dfbed2020-04-01 21:05:14 -07002607 return __gup_device_huge_pud(orig, pudp, addr, end, flags,
2608 pages, nr);
Ira Weiny7af75562019-05-13 17:17:14 -07002609 }
Kirill A. Shutemovb59f65f2017-03-16 18:26:53 +03002610
Punit Agrawald63206e2017-07-06 15:39:39 -07002611 page = pud_page(orig) + ((addr & ~PUD_MASK) >> PAGE_SHIFT);
John Hubbarda43e9822020-01-30 22:12:17 -08002612 refs = record_subpages(page, addr, end, pages + *nr);
Steve Capper2667f502014-10-09 15:29:14 -07002613
John Hubbard3faa52c2020-04-01 21:05:29 -07002614 head = try_grab_compound_head(pud_page(orig), refs, flags);
John Hubbarda43e9822020-01-30 22:12:17 -08002615 if (!head)
Steve Capper2667f502014-10-09 15:29:14 -07002616 return 0;
Steve Capper2667f502014-10-09 15:29:14 -07002617
2618 if (unlikely(pud_val(orig) != pud_val(*pudp))) {
John Hubbard3b78d832020-04-01 21:05:22 -07002619 put_compound_head(head, refs, flags);
Steve Capper2667f502014-10-09 15:29:14 -07002620 return 0;
2621 }
2622
John Hubbarda43e9822020-01-30 22:12:17 -08002623 *nr += refs;
Kirill A. Shutemove9348052017-03-16 18:26:52 +03002624 SetPageReferenced(head);
Steve Capper2667f502014-10-09 15:29:14 -07002625 return 1;
2626}
2627
Aneesh Kumar K.Vf30c59e2014-11-05 21:57:40 +05302628static int gup_huge_pgd(pgd_t orig, pgd_t *pgdp, unsigned long addr,
Ira Weinyb798bec2019-05-13 17:17:07 -07002629 unsigned long end, unsigned int flags,
Aneesh Kumar K.Vf30c59e2014-11-05 21:57:40 +05302630 struct page **pages, int *nr)
2631{
2632 int refs;
Kirill A. Shutemovddc58f22016-01-15 16:52:56 -08002633 struct page *head, *page;
Aneesh Kumar K.Vf30c59e2014-11-05 21:57:40 +05302634
Ira Weinyb798bec2019-05-13 17:17:07 -07002635 if (!pgd_access_permitted(orig, flags & FOLL_WRITE))
Aneesh Kumar K.Vf30c59e2014-11-05 21:57:40 +05302636 return 0;
2637
Kirill A. Shutemovb59f65f2017-03-16 18:26:53 +03002638 BUILD_BUG_ON(pgd_devmap(orig));
John Hubbarda43e9822020-01-30 22:12:17 -08002639
Punit Agrawald63206e2017-07-06 15:39:39 -07002640 page = pgd_page(orig) + ((addr & ~PGDIR_MASK) >> PAGE_SHIFT);
John Hubbarda43e9822020-01-30 22:12:17 -08002641 refs = record_subpages(page, addr, end, pages + *nr);
Aneesh Kumar K.Vf30c59e2014-11-05 21:57:40 +05302642
John Hubbard3faa52c2020-04-01 21:05:29 -07002643 head = try_grab_compound_head(pgd_page(orig), refs, flags);
John Hubbarda43e9822020-01-30 22:12:17 -08002644 if (!head)
Aneesh Kumar K.Vf30c59e2014-11-05 21:57:40 +05302645 return 0;
Aneesh Kumar K.Vf30c59e2014-11-05 21:57:40 +05302646
2647 if (unlikely(pgd_val(orig) != pgd_val(*pgdp))) {
John Hubbard3b78d832020-04-01 21:05:22 -07002648 put_compound_head(head, refs, flags);
Aneesh Kumar K.Vf30c59e2014-11-05 21:57:40 +05302649 return 0;
2650 }
2651
John Hubbarda43e9822020-01-30 22:12:17 -08002652 *nr += refs;
Kirill A. Shutemove9348052017-03-16 18:26:52 +03002653 SetPageReferenced(head);
Aneesh Kumar K.Vf30c59e2014-11-05 21:57:40 +05302654 return 1;
2655}
2656
Vasily Gorbikd3f7b1b2020-09-25 21:19:10 -07002657static int gup_pmd_range(pud_t *pudp, pud_t pud, unsigned long addr, unsigned long end,
Ira Weinyb798bec2019-05-13 17:17:07 -07002658 unsigned int flags, struct page **pages, int *nr)
Steve Capper2667f502014-10-09 15:29:14 -07002659{
2660 unsigned long next;
2661 pmd_t *pmdp;
2662
Vasily Gorbikd3f7b1b2020-09-25 21:19:10 -07002663 pmdp = pmd_offset_lockless(pudp, pud, addr);
Steve Capper2667f502014-10-09 15:29:14 -07002664 do {
Christian Borntraeger38c5ce92015-01-06 22:54:46 +01002665 pmd_t pmd = READ_ONCE(*pmdp);
Steve Capper2667f502014-10-09 15:29:14 -07002666
2667 next = pmd_addr_end(addr, end);
Zi Yan84c3fc42017-09-08 16:11:01 -07002668 if (!pmd_present(pmd))
Steve Capper2667f502014-10-09 15:29:14 -07002669 return 0;
2670
Yu Zhao414fd082019-02-12 15:35:58 -08002671 if (unlikely(pmd_trans_huge(pmd) || pmd_huge(pmd) ||
2672 pmd_devmap(pmd))) {
Steve Capper2667f502014-10-09 15:29:14 -07002673 /*
2674 * NUMA hinting faults need to be handled in the GUP
2675 * slowpath for accounting purposes and so that they
2676 * can be serialised against THP migration.
2677 */
Mel Gorman8a0516e2015-02-12 14:58:22 -08002678 if (pmd_protnone(pmd))
Steve Capper2667f502014-10-09 15:29:14 -07002679 return 0;
2680
Ira Weinyb798bec2019-05-13 17:17:07 -07002681 if (!gup_huge_pmd(pmd, pmdp, addr, next, flags,
Steve Capper2667f502014-10-09 15:29:14 -07002682 pages, nr))
2683 return 0;
2684
Aneesh Kumar K.Vf30c59e2014-11-05 21:57:40 +05302685 } else if (unlikely(is_hugepd(__hugepd(pmd_val(pmd))))) {
2686 /*
2687 * architecture have different format for hugetlbfs
2688 * pmd format and THP pmd format
2689 */
2690 if (!gup_huge_pd(__hugepd(pmd_val(pmd)), addr,
Ira Weinyb798bec2019-05-13 17:17:07 -07002691 PMD_SHIFT, next, flags, pages, nr))
Aneesh Kumar K.Vf30c59e2014-11-05 21:57:40 +05302692 return 0;
Ira Weinyb798bec2019-05-13 17:17:07 -07002693 } else if (!gup_pte_range(pmd, addr, next, flags, pages, nr))
Mario Leinweber29231172018-04-05 16:24:18 -07002694 return 0;
Steve Capper2667f502014-10-09 15:29:14 -07002695 } while (pmdp++, addr = next, addr != end);
2696
2697 return 1;
2698}
2699
Vasily Gorbikd3f7b1b2020-09-25 21:19:10 -07002700static int gup_pud_range(p4d_t *p4dp, p4d_t p4d, unsigned long addr, unsigned long end,
Ira Weinyb798bec2019-05-13 17:17:07 -07002701 unsigned int flags, struct page **pages, int *nr)
Steve Capper2667f502014-10-09 15:29:14 -07002702{
2703 unsigned long next;
2704 pud_t *pudp;
2705
Vasily Gorbikd3f7b1b2020-09-25 21:19:10 -07002706 pudp = pud_offset_lockless(p4dp, p4d, addr);
Steve Capper2667f502014-10-09 15:29:14 -07002707 do {
Christian Borntraegere37c6982014-12-07 21:41:33 +01002708 pud_t pud = READ_ONCE(*pudp);
Steve Capper2667f502014-10-09 15:29:14 -07002709
2710 next = pud_addr_end(addr, end);
Qiujun Huang154945202020-01-30 22:12:10 -08002711 if (unlikely(!pud_present(pud)))
Steve Capper2667f502014-10-09 15:29:14 -07002712 return 0;
Aneesh Kumar K.Vf30c59e2014-11-05 21:57:40 +05302713 if (unlikely(pud_huge(pud))) {
Ira Weinyb798bec2019-05-13 17:17:07 -07002714 if (!gup_huge_pud(pud, pudp, addr, next, flags,
Aneesh Kumar K.Vf30c59e2014-11-05 21:57:40 +05302715 pages, nr))
2716 return 0;
2717 } else if (unlikely(is_hugepd(__hugepd(pud_val(pud))))) {
2718 if (!gup_huge_pd(__hugepd(pud_val(pud)), addr,
Ira Weinyb798bec2019-05-13 17:17:07 -07002719 PUD_SHIFT, next, flags, pages, nr))
Steve Capper2667f502014-10-09 15:29:14 -07002720 return 0;
Vasily Gorbikd3f7b1b2020-09-25 21:19:10 -07002721 } else if (!gup_pmd_range(pudp, pud, addr, next, flags, pages, nr))
Steve Capper2667f502014-10-09 15:29:14 -07002722 return 0;
2723 } while (pudp++, addr = next, addr != end);
2724
2725 return 1;
2726}
2727
Vasily Gorbikd3f7b1b2020-09-25 21:19:10 -07002728static int gup_p4d_range(pgd_t *pgdp, pgd_t pgd, unsigned long addr, unsigned long end,
Ira Weinyb798bec2019-05-13 17:17:07 -07002729 unsigned int flags, struct page **pages, int *nr)
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +03002730{
2731 unsigned long next;
2732 p4d_t *p4dp;
2733
Vasily Gorbikd3f7b1b2020-09-25 21:19:10 -07002734 p4dp = p4d_offset_lockless(pgdp, pgd, addr);
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +03002735 do {
2736 p4d_t p4d = READ_ONCE(*p4dp);
2737
2738 next = p4d_addr_end(addr, end);
2739 if (p4d_none(p4d))
2740 return 0;
2741 BUILD_BUG_ON(p4d_huge(p4d));
2742 if (unlikely(is_hugepd(__hugepd(p4d_val(p4d))))) {
2743 if (!gup_huge_pd(__hugepd(p4d_val(p4d)), addr,
Ira Weinyb798bec2019-05-13 17:17:07 -07002744 P4D_SHIFT, next, flags, pages, nr))
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +03002745 return 0;
Vasily Gorbikd3f7b1b2020-09-25 21:19:10 -07002746 } else if (!gup_pud_range(p4dp, p4d, addr, next, flags, pages, nr))
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +03002747 return 0;
2748 } while (p4dp++, addr = next, addr != end);
2749
2750 return 1;
2751}
2752
Kirill A. Shutemov5b65c4672017-09-09 00:56:03 +03002753static void gup_pgd_range(unsigned long addr, unsigned long end,
Ira Weinyb798bec2019-05-13 17:17:07 -07002754 unsigned int flags, struct page **pages, int *nr)
Kirill A. Shutemov5b65c4672017-09-09 00:56:03 +03002755{
2756 unsigned long next;
2757 pgd_t *pgdp;
2758
2759 pgdp = pgd_offset(current->mm, addr);
2760 do {
2761 pgd_t pgd = READ_ONCE(*pgdp);
2762
2763 next = pgd_addr_end(addr, end);
2764 if (pgd_none(pgd))
2765 return;
2766 if (unlikely(pgd_huge(pgd))) {
Ira Weinyb798bec2019-05-13 17:17:07 -07002767 if (!gup_huge_pgd(pgd, pgdp, addr, next, flags,
Kirill A. Shutemov5b65c4672017-09-09 00:56:03 +03002768 pages, nr))
2769 return;
2770 } else if (unlikely(is_hugepd(__hugepd(pgd_val(pgd))))) {
2771 if (!gup_huge_pd(__hugepd(pgd_val(pgd)), addr,
Ira Weinyb798bec2019-05-13 17:17:07 -07002772 PGDIR_SHIFT, next, flags, pages, nr))
Kirill A. Shutemov5b65c4672017-09-09 00:56:03 +03002773 return;
Vasily Gorbikd3f7b1b2020-09-25 21:19:10 -07002774 } else if (!gup_p4d_range(pgdp, pgd, addr, next, flags, pages, nr))
Kirill A. Shutemov5b65c4672017-09-09 00:56:03 +03002775 return;
2776 } while (pgdp++, addr = next, addr != end);
2777}
Christoph Hellwig050a9ad2019-07-11 20:57:21 -07002778#else
2779static inline void gup_pgd_range(unsigned long addr, unsigned long end,
2780 unsigned int flags, struct page **pages, int *nr)
2781{
2782}
2783#endif /* CONFIG_HAVE_FAST_GUP */
Kirill A. Shutemov5b65c4672017-09-09 00:56:03 +03002784
2785#ifndef gup_fast_permitted
2786/*
Souptick Joarderdadbb612020-06-07 21:40:55 -07002787 * Check if it's allowed to use get_user_pages_fast_only() for the range, or
Kirill A. Shutemov5b65c4672017-09-09 00:56:03 +03002788 * we need to fall back to the slow version:
2789 */
Christoph Hellwig26f4c322019-07-11 20:56:45 -07002790static bool gup_fast_permitted(unsigned long start, unsigned long end)
Kirill A. Shutemov5b65c4672017-09-09 00:56:03 +03002791{
Christoph Hellwig26f4c322019-07-11 20:56:45 -07002792 return true;
Kirill A. Shutemov5b65c4672017-09-09 00:56:03 +03002793}
2794#endif
2795
Ira Weiny7af75562019-05-13 17:17:14 -07002796static int __gup_longterm_unlocked(unsigned long start, int nr_pages,
2797 unsigned int gup_flags, struct page **pages)
2798{
2799 int ret;
2800
2801 /*
2802 * FIXME: FOLL_LONGTERM does not work with
2803 * get_user_pages_unlocked() (see comments in that function)
2804 */
2805 if (gup_flags & FOLL_LONGTERM) {
Michel Lespinassed8ed45c2020-06-08 21:33:25 -07002806 mmap_read_lock(current->mm);
Peter Xu64019a22020-08-11 18:39:01 -07002807 ret = __gup_longterm_locked(current->mm,
Ira Weiny7af75562019-05-13 17:17:14 -07002808 start, nr_pages,
2809 pages, NULL, gup_flags);
Michel Lespinassed8ed45c2020-06-08 21:33:25 -07002810 mmap_read_unlock(current->mm);
Ira Weiny7af75562019-05-13 17:17:14 -07002811 } else {
2812 ret = get_user_pages_unlocked(start, nr_pages,
2813 pages, gup_flags);
2814 }
2815
2816 return ret;
2817}
2818
Jason Gunthorpec28b1fc2020-12-14 19:05:41 -08002819static unsigned long lockless_pages_from_mm(unsigned long start,
2820 unsigned long end,
2821 unsigned int gup_flags,
2822 struct page **pages)
2823{
2824 unsigned long flags;
2825 int nr_pinned = 0;
Jason Gunthorpe57efa1f2020-12-14 19:05:44 -08002826 unsigned seq;
Jason Gunthorpec28b1fc2020-12-14 19:05:41 -08002827
2828 if (!IS_ENABLED(CONFIG_HAVE_FAST_GUP) ||
2829 !gup_fast_permitted(start, end))
2830 return 0;
2831
Jason Gunthorpe57efa1f2020-12-14 19:05:44 -08002832 if (gup_flags & FOLL_PIN) {
2833 seq = raw_read_seqcount(&current->mm->write_protect_seq);
2834 if (seq & 1)
2835 return 0;
2836 }
2837
Jason Gunthorpec28b1fc2020-12-14 19:05:41 -08002838 /*
2839 * Disable interrupts. The nested form is used, in order to allow full,
2840 * general purpose use of this routine.
2841 *
2842 * With interrupts disabled, we block page table pages from being freed
2843 * from under us. See struct mmu_table_batch comments in
2844 * include/asm-generic/tlb.h for more details.
2845 *
2846 * We do not adopt an rcu_read_lock() here as we also want to block IPIs
2847 * that come from THPs splitting.
2848 */
2849 local_irq_save(flags);
2850 gup_pgd_range(start, end, gup_flags, pages, &nr_pinned);
2851 local_irq_restore(flags);
Jason Gunthorpe57efa1f2020-12-14 19:05:44 -08002852
2853 /*
2854 * When pinning pages for DMA there could be a concurrent write protect
2855 * from fork() via copy_page_range(), in this case always fail fast GUP.
2856 */
2857 if (gup_flags & FOLL_PIN) {
2858 if (read_seqcount_retry(&current->mm->write_protect_seq, seq)) {
2859 unpin_user_pages(pages, nr_pinned);
2860 return 0;
2861 }
2862 }
Jason Gunthorpec28b1fc2020-12-14 19:05:41 -08002863 return nr_pinned;
2864}
2865
2866static int internal_get_user_pages_fast(unsigned long start,
2867 unsigned long nr_pages,
John Hubbardeddb1c22020-01-30 22:12:54 -08002868 unsigned int gup_flags,
2869 struct page **pages)
Steve Capper2667f502014-10-09 15:29:14 -07002870{
Jason Gunthorpec28b1fc2020-12-14 19:05:41 -08002871 unsigned long len, end;
2872 unsigned long nr_pinned;
2873 int ret;
Steve Capper2667f502014-10-09 15:29:14 -07002874
John Hubbardf4000fd2020-01-30 22:12:43 -08002875 if (WARN_ON_ONCE(gup_flags & ~(FOLL_WRITE | FOLL_LONGTERM |
John Hubbard376a34ef2020-06-03 15:56:30 -07002876 FOLL_FORCE | FOLL_PIN | FOLL_GET |
Andreas Gruenbacher55b8fe72021-08-17 22:52:08 +02002877 FOLL_FAST_ONLY | FOLL_NOFAULT)))
Christoph Hellwig817be122019-07-11 20:57:25 -07002878 return -EINVAL;
2879
Andrea Arcangelia458b762021-06-28 19:36:40 -07002880 if (gup_flags & FOLL_PIN)
2881 mm_set_has_pinned_flag(&current->mm->flags);
Peter Xu008cfe42020-09-25 18:25:57 -04002882
John Hubbardf81cd172020-06-03 15:56:40 -07002883 if (!(gup_flags & FOLL_FAST_ONLY))
Michel Lespinasseda1c55f2020-06-08 21:33:47 -07002884 might_lock_read(&current->mm->mmap_lock);
John Hubbardf81cd172020-06-03 15:56:40 -07002885
Christoph Hellwigf455c8542019-07-11 20:56:41 -07002886 start = untagged_addr(start) & PAGE_MASK;
Jason Gunthorpec28b1fc2020-12-14 19:05:41 -08002887 len = nr_pages << PAGE_SHIFT;
2888 if (check_add_overflow(start, len, &end))
Michael S. Tsirkinc61611f2018-04-13 15:35:20 -07002889 return 0;
Linus Torvalds96d4f262019-01-03 18:57:57 -08002890 if (unlikely(!access_ok((void __user *)start, len)))
Michael S. Tsirkinc61611f2018-04-13 15:35:20 -07002891 return -EFAULT;
Kirill A. Shutemov73e10a62017-03-16 18:26:54 +03002892
Jason Gunthorpec28b1fc2020-12-14 19:05:41 -08002893 nr_pinned = lockless_pages_from_mm(start, end, gup_flags, pages);
2894 if (nr_pinned == nr_pages || gup_flags & FOLL_FAST_ONLY)
2895 return nr_pinned;
John Hubbard376a34ef2020-06-03 15:56:30 -07002896
Jason Gunthorpec28b1fc2020-12-14 19:05:41 -08002897 /* Slow path: try to get the remaining pages with get_user_pages */
2898 start += nr_pinned << PAGE_SHIFT;
2899 pages += nr_pinned;
2900 ret = __gup_longterm_unlocked(start, nr_pages - nr_pinned, gup_flags,
2901 pages);
2902 if (ret < 0) {
2903 /*
2904 * The caller has to unpin the pages we already pinned so
2905 * returning -errno is not an option
2906 */
2907 if (nr_pinned)
2908 return nr_pinned;
2909 return ret;
Kirill A. Shutemov73e10a62017-03-16 18:26:54 +03002910 }
Jason Gunthorpec28b1fc2020-12-14 19:05:41 -08002911 return ret + nr_pinned;
Steve Capper2667f502014-10-09 15:29:14 -07002912}
Jason Gunthorpec28b1fc2020-12-14 19:05:41 -08002913
Souptick Joarderdadbb612020-06-07 21:40:55 -07002914/**
2915 * get_user_pages_fast_only() - pin user pages in memory
2916 * @start: starting user address
2917 * @nr_pages: number of pages from start to pin
2918 * @gup_flags: flags modifying pin behaviour
2919 * @pages: array that receives pointers to the pages pinned.
2920 * Should be at least nr_pages long.
2921 *
John Hubbard9e1f0582020-06-03 15:56:27 -07002922 * Like get_user_pages_fast() except it's IRQ-safe in that it won't fall back to
2923 * the regular GUP.
2924 * Note a difference with get_user_pages_fast: this always returns the
2925 * number of pages pinned, 0 if no pages were pinned.
2926 *
2927 * If the architecture does not support this function, simply return with no
2928 * pages pinned.
2929 *
2930 * Careful, careful! COW breaking can go either way, so a non-write
2931 * access can get ambiguous page results. If you call this function without
2932 * 'write' set, you'd better be sure that you're ok with that ambiguity.
2933 */
Souptick Joarderdadbb612020-06-07 21:40:55 -07002934int get_user_pages_fast_only(unsigned long start, int nr_pages,
2935 unsigned int gup_flags, struct page **pages)
John Hubbard9e1f0582020-06-03 15:56:27 -07002936{
John Hubbard376a34ef2020-06-03 15:56:30 -07002937 int nr_pinned;
John Hubbard9e1f0582020-06-03 15:56:27 -07002938 /*
2939 * Internally (within mm/gup.c), gup fast variants must set FOLL_GET,
2940 * because gup fast is always a "pin with a +1 page refcount" request.
John Hubbard376a34ef2020-06-03 15:56:30 -07002941 *
2942 * FOLL_FAST_ONLY is required in order to match the API description of
2943 * this routine: no fall back to regular ("slow") GUP.
John Hubbard9e1f0582020-06-03 15:56:27 -07002944 */
Souptick Joarderdadbb612020-06-07 21:40:55 -07002945 gup_flags |= FOLL_GET | FOLL_FAST_ONLY;
John Hubbard9e1f0582020-06-03 15:56:27 -07002946
John Hubbard376a34ef2020-06-03 15:56:30 -07002947 nr_pinned = internal_get_user_pages_fast(start, nr_pages, gup_flags,
2948 pages);
John Hubbard9e1f0582020-06-03 15:56:27 -07002949
2950 /*
John Hubbard376a34ef2020-06-03 15:56:30 -07002951 * As specified in the API description above, this routine is not
2952 * allowed to return negative values. However, the common core
2953 * routine internal_get_user_pages_fast() *can* return -errno.
2954 * Therefore, correct for that here:
John Hubbard9e1f0582020-06-03 15:56:27 -07002955 */
John Hubbard376a34ef2020-06-03 15:56:30 -07002956 if (nr_pinned < 0)
2957 nr_pinned = 0;
John Hubbard9e1f0582020-06-03 15:56:27 -07002958
2959 return nr_pinned;
2960}
Souptick Joarderdadbb612020-06-07 21:40:55 -07002961EXPORT_SYMBOL_GPL(get_user_pages_fast_only);
John Hubbard9e1f0582020-06-03 15:56:27 -07002962
John Hubbardeddb1c22020-01-30 22:12:54 -08002963/**
2964 * get_user_pages_fast() - pin user pages in memory
John Hubbard3faa52c2020-04-01 21:05:29 -07002965 * @start: starting user address
2966 * @nr_pages: number of pages from start to pin
2967 * @gup_flags: flags modifying pin behaviour
2968 * @pages: array that receives pointers to the pages pinned.
2969 * Should be at least nr_pages long.
John Hubbardeddb1c22020-01-30 22:12:54 -08002970 *
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -07002971 * Attempt to pin user pages in memory without taking mm->mmap_lock.
John Hubbardeddb1c22020-01-30 22:12:54 -08002972 * If not successful, it will fall back to taking the lock and
2973 * calling get_user_pages().
2974 *
2975 * Returns number of pages pinned. This may be fewer than the number requested.
2976 * If nr_pages is 0 or negative, returns 0. If no pages were pinned, returns
2977 * -errno.
2978 */
2979int get_user_pages_fast(unsigned long start, int nr_pages,
2980 unsigned int gup_flags, struct page **pages)
2981{
Barry Song447f3e42020-10-13 16:51:58 -07002982 if (!is_valid_gup_flags(gup_flags))
John Hubbardeddb1c22020-01-30 22:12:54 -08002983 return -EINVAL;
2984
John Hubbard94202f12020-04-01 21:05:25 -07002985 /*
2986 * The caller may or may not have explicitly set FOLL_GET; either way is
2987 * OK. However, internally (within mm/gup.c), gup fast variants must set
2988 * FOLL_GET, because gup fast is always a "pin with a +1 page refcount"
2989 * request.
2990 */
2991 gup_flags |= FOLL_GET;
John Hubbardeddb1c22020-01-30 22:12:54 -08002992 return internal_get_user_pages_fast(start, nr_pages, gup_flags, pages);
2993}
Christoph Hellwig050a9ad2019-07-11 20:57:21 -07002994EXPORT_SYMBOL_GPL(get_user_pages_fast);
John Hubbardeddb1c22020-01-30 22:12:54 -08002995
2996/**
2997 * pin_user_pages_fast() - pin user pages in memory without taking locks
2998 *
John Hubbard3faa52c2020-04-01 21:05:29 -07002999 * @start: starting user address
3000 * @nr_pages: number of pages from start to pin
3001 * @gup_flags: flags modifying pin behaviour
3002 * @pages: array that receives pointers to the pages pinned.
3003 * Should be at least nr_pages long.
3004 *
3005 * Nearly the same as get_user_pages_fast(), except that FOLL_PIN is set. See
3006 * get_user_pages_fast() for documentation on the function arguments, because
3007 * the arguments here are identical.
3008 *
3009 * FOLL_PIN means that the pages must be released via unpin_user_page(). Please
Mauro Carvalho Chehab72ef5e52020-04-14 18:48:35 +02003010 * see Documentation/core-api/pin_user_pages.rst for further details.
John Hubbardeddb1c22020-01-30 22:12:54 -08003011 */
3012int pin_user_pages_fast(unsigned long start, int nr_pages,
3013 unsigned int gup_flags, struct page **pages)
3014{
John Hubbard3faa52c2020-04-01 21:05:29 -07003015 /* FOLL_GET and FOLL_PIN are mutually exclusive. */
3016 if (WARN_ON_ONCE(gup_flags & FOLL_GET))
3017 return -EINVAL;
3018
3019 gup_flags |= FOLL_PIN;
3020 return internal_get_user_pages_fast(start, nr_pages, gup_flags, pages);
John Hubbardeddb1c22020-01-30 22:12:54 -08003021}
3022EXPORT_SYMBOL_GPL(pin_user_pages_fast);
3023
John Hubbard104acc32020-06-03 15:56:34 -07003024/*
Souptick Joarderdadbb612020-06-07 21:40:55 -07003025 * This is the FOLL_PIN equivalent of get_user_pages_fast_only(). Behavior
3026 * is the same, except that this one sets FOLL_PIN instead of FOLL_GET.
John Hubbard104acc32020-06-03 15:56:34 -07003027 *
3028 * The API rules are the same, too: no negative values may be returned.
3029 */
3030int pin_user_pages_fast_only(unsigned long start, int nr_pages,
3031 unsigned int gup_flags, struct page **pages)
3032{
3033 int nr_pinned;
3034
3035 /*
3036 * FOLL_GET and FOLL_PIN are mutually exclusive. Note that the API
3037 * rules require returning 0, rather than -errno:
3038 */
3039 if (WARN_ON_ONCE(gup_flags & FOLL_GET))
3040 return 0;
3041 /*
3042 * FOLL_FAST_ONLY is required in order to match the API description of
3043 * this routine: no fall back to regular ("slow") GUP.
3044 */
3045 gup_flags |= (FOLL_PIN | FOLL_FAST_ONLY);
3046 nr_pinned = internal_get_user_pages_fast(start, nr_pages, gup_flags,
3047 pages);
3048 /*
3049 * This routine is not allowed to return negative values. However,
3050 * internal_get_user_pages_fast() *can* return -errno. Therefore,
3051 * correct for that here:
3052 */
3053 if (nr_pinned < 0)
3054 nr_pinned = 0;
3055
3056 return nr_pinned;
3057}
3058EXPORT_SYMBOL_GPL(pin_user_pages_fast_only);
3059
John Hubbardeddb1c22020-01-30 22:12:54 -08003060/**
Peter Xu64019a22020-08-11 18:39:01 -07003061 * pin_user_pages_remote() - pin pages of a remote process
John Hubbardeddb1c22020-01-30 22:12:54 -08003062 *
John Hubbard3faa52c2020-04-01 21:05:29 -07003063 * @mm: mm_struct of target mm
3064 * @start: starting user address
3065 * @nr_pages: number of pages from start to pin
3066 * @gup_flags: flags modifying lookup behaviour
3067 * @pages: array that receives pointers to the pages pinned.
3068 * Should be at least nr_pages long. Or NULL, if caller
3069 * only intends to ensure the pages are faulted in.
3070 * @vmas: array of pointers to vmas corresponding to each page.
3071 * Or NULL if the caller does not require them.
3072 * @locked: pointer to lock flag indicating whether lock is held and
3073 * subsequently whether VM_FAULT_RETRY functionality can be
3074 * utilised. Lock must initially be held.
3075 *
3076 * Nearly the same as get_user_pages_remote(), except that FOLL_PIN is set. See
3077 * get_user_pages_remote() for documentation on the function arguments, because
3078 * the arguments here are identical.
3079 *
3080 * FOLL_PIN means that the pages must be released via unpin_user_page(). Please
Mauro Carvalho Chehab72ef5e52020-04-14 18:48:35 +02003081 * see Documentation/core-api/pin_user_pages.rst for details.
John Hubbardeddb1c22020-01-30 22:12:54 -08003082 */
Peter Xu64019a22020-08-11 18:39:01 -07003083long pin_user_pages_remote(struct mm_struct *mm,
John Hubbardeddb1c22020-01-30 22:12:54 -08003084 unsigned long start, unsigned long nr_pages,
3085 unsigned int gup_flags, struct page **pages,
3086 struct vm_area_struct **vmas, int *locked)
3087{
John Hubbard3faa52c2020-04-01 21:05:29 -07003088 /* FOLL_GET and FOLL_PIN are mutually exclusive. */
3089 if (WARN_ON_ONCE(gup_flags & FOLL_GET))
3090 return -EINVAL;
3091
3092 gup_flags |= FOLL_PIN;
Peter Xu64019a22020-08-11 18:39:01 -07003093 return __get_user_pages_remote(mm, start, nr_pages, gup_flags,
John Hubbard3faa52c2020-04-01 21:05:29 -07003094 pages, vmas, locked);
John Hubbardeddb1c22020-01-30 22:12:54 -08003095}
3096EXPORT_SYMBOL(pin_user_pages_remote);
3097
3098/**
3099 * pin_user_pages() - pin user pages in memory for use by other devices
3100 *
John Hubbard3faa52c2020-04-01 21:05:29 -07003101 * @start: starting user address
3102 * @nr_pages: number of pages from start to pin
3103 * @gup_flags: flags modifying lookup behaviour
3104 * @pages: array that receives pointers to the pages pinned.
3105 * Should be at least nr_pages long. Or NULL, if caller
3106 * only intends to ensure the pages are faulted in.
3107 * @vmas: array of pointers to vmas corresponding to each page.
3108 * Or NULL if the caller does not require them.
3109 *
3110 * Nearly the same as get_user_pages(), except that FOLL_TOUCH is not set, and
3111 * FOLL_PIN is set.
3112 *
3113 * FOLL_PIN means that the pages must be released via unpin_user_page(). Please
Mauro Carvalho Chehab72ef5e52020-04-14 18:48:35 +02003114 * see Documentation/core-api/pin_user_pages.rst for details.
John Hubbardeddb1c22020-01-30 22:12:54 -08003115 */
3116long pin_user_pages(unsigned long start, unsigned long nr_pages,
3117 unsigned int gup_flags, struct page **pages,
3118 struct vm_area_struct **vmas)
3119{
John Hubbard3faa52c2020-04-01 21:05:29 -07003120 /* FOLL_GET and FOLL_PIN are mutually exclusive. */
3121 if (WARN_ON_ONCE(gup_flags & FOLL_GET))
3122 return -EINVAL;
3123
3124 gup_flags |= FOLL_PIN;
Peter Xu64019a22020-08-11 18:39:01 -07003125 return __gup_longterm_locked(current->mm, start, nr_pages,
John Hubbard3faa52c2020-04-01 21:05:29 -07003126 pages, vmas, gup_flags);
John Hubbardeddb1c22020-01-30 22:12:54 -08003127}
3128EXPORT_SYMBOL(pin_user_pages);
John Hubbard91429022020-06-01 21:48:27 -07003129
3130/*
3131 * pin_user_pages_unlocked() is the FOLL_PIN variant of
3132 * get_user_pages_unlocked(). Behavior is the same, except that this one sets
3133 * FOLL_PIN and rejects FOLL_GET.
3134 */
3135long pin_user_pages_unlocked(unsigned long start, unsigned long nr_pages,
3136 struct page **pages, unsigned int gup_flags)
3137{
3138 /* FOLL_GET and FOLL_PIN are mutually exclusive. */
3139 if (WARN_ON_ONCE(gup_flags & FOLL_GET))
3140 return -EINVAL;
3141
3142 gup_flags |= FOLL_PIN;
3143 return get_user_pages_unlocked(start, nr_pages, pages, gup_flags);
3144}
3145EXPORT_SYMBOL(pin_user_pages_unlocked);
John Hubbard420c2092020-06-07 21:41:02 -07003146
3147/*
3148 * pin_user_pages_locked() is the FOLL_PIN variant of get_user_pages_locked().
3149 * Behavior is the same, except that this one sets FOLL_PIN and rejects
3150 * FOLL_GET.
3151 */
3152long pin_user_pages_locked(unsigned long start, unsigned long nr_pages,
3153 unsigned int gup_flags, struct page **pages,
3154 int *locked)
3155{
3156 /*
3157 * FIXME: Current FOLL_LONGTERM behavior is incompatible with
3158 * FAULT_FLAG_ALLOW_RETRY because of the FS DAX check requirement on
3159 * vmas. As there are no users of this flag in this call we simply
3160 * disallow this option for now.
3161 */
3162 if (WARN_ON_ONCE(gup_flags & FOLL_LONGTERM))
3163 return -EINVAL;
3164
3165 /* FOLL_GET and FOLL_PIN are mutually exclusive. */
3166 if (WARN_ON_ONCE(gup_flags & FOLL_GET))
3167 return -EINVAL;
3168
3169 gup_flags |= FOLL_PIN;
Peter Xu64019a22020-08-11 18:39:01 -07003170 return __get_user_pages_locked(current->mm, start, nr_pages,
John Hubbard420c2092020-06-07 21:41:02 -07003171 pages, NULL, locked,
3172 gup_flags | FOLL_TOUCH);
3173}
3174EXPORT_SYMBOL(pin_user_pages_locked);