1457c8996SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
24bbd4c77SKirill A. Shutemov #include <linux/kernel.h>
34bbd4c77SKirill A. Shutemov #include <linux/errno.h>
44bbd4c77SKirill A. Shutemov #include <linux/err.h>
54bbd4c77SKirill A. Shutemov #include <linux/spinlock.h>
64bbd4c77SKirill A. Shutemov
74bbd4c77SKirill A. Shutemov #include <linux/mm.h>
83565fce3SDan Williams #include <linux/memremap.h>
94bbd4c77SKirill A. Shutemov #include <linux/pagemap.h>
104bbd4c77SKirill A. Shutemov #include <linux/rmap.h>
114bbd4c77SKirill A. Shutemov #include <linux/swap.h>
124bbd4c77SKirill A. Shutemov #include <linux/swapops.h>
131507f512SMike Rapoport #include <linux/secretmem.h>
144bbd4c77SKirill A. Shutemov
15174cd4b1SIngo Molnar #include <linux/sched/signal.h>
162667f50eSSteve Capper #include <linux/rwsem.h>
17f30c59e9SAneesh Kumar K.V #include <linux/hugetlb.h>
189a4e9f3bSAneesh Kumar K.V #include <linux/migrate.h>
199a4e9f3bSAneesh Kumar K.V #include <linux/mm_inline.h>
209a4e9f3bSAneesh Kumar K.V #include <linux/sched/mm.h>
21a6e79df9SLorenzo Stoakes #include <linux/shmem_fs.h>
221027e443SKirill A. Shutemov
2333a709b2SDave Hansen #include <asm/mmu_context.h>
241027e443SKirill A. Shutemov #include <asm/tlbflush.h>
252667f50eSSteve Capper
264bbd4c77SKirill A. Shutemov #include "internal.h"
274bbd4c77SKirill A. Shutemov
28df06b37fSKeith Busch struct follow_page_context {
29df06b37fSKeith Busch struct dev_pagemap *pgmap;
30df06b37fSKeith Busch unsigned int page_mask;
31df06b37fSKeith Busch };
32df06b37fSKeith Busch
sanity_check_pinned_pages(struct page ** pages,unsigned long npages)33b6a2619cSDavid Hildenbrand static inline void sanity_check_pinned_pages(struct page **pages,
34b6a2619cSDavid Hildenbrand unsigned long npages)
35b6a2619cSDavid Hildenbrand {
36b6a2619cSDavid Hildenbrand if (!IS_ENABLED(CONFIG_DEBUG_VM))
37b6a2619cSDavid Hildenbrand return;
38b6a2619cSDavid Hildenbrand
39b6a2619cSDavid Hildenbrand /*
40b6a2619cSDavid Hildenbrand * We only pin anonymous pages if they are exclusive. Once pinned, we
41b6a2619cSDavid Hildenbrand * can no longer turn them possibly shared and PageAnonExclusive() will
42b6a2619cSDavid Hildenbrand * stick around until the page is freed.
43b6a2619cSDavid Hildenbrand *
44b6a2619cSDavid Hildenbrand * We'd like to verify that our pinned anonymous pages are still mapped
45b6a2619cSDavid Hildenbrand * exclusively. The issue with anon THP is that we don't know how
46b6a2619cSDavid Hildenbrand * they are/were mapped when pinning them. However, for anon
47b6a2619cSDavid Hildenbrand * THP we can assume that either the given page (PTE-mapped THP) or
48b6a2619cSDavid Hildenbrand * the head page (PMD-mapped THP) should be PageAnonExclusive(). If
49b6a2619cSDavid Hildenbrand * neither is the case, there is certainly something wrong.
50b6a2619cSDavid Hildenbrand */
51b6a2619cSDavid Hildenbrand for (; npages; npages--, pages++) {
52b6a2619cSDavid Hildenbrand struct page *page = *pages;
53b6a2619cSDavid Hildenbrand struct folio *folio = page_folio(page);
54b6a2619cSDavid Hildenbrand
55c8070b78SDavid Howells if (is_zero_page(page) ||
56c8070b78SDavid Howells !folio_test_anon(folio))
57b6a2619cSDavid Hildenbrand continue;
58b6a2619cSDavid Hildenbrand if (!folio_test_large(folio) || folio_test_hugetlb(folio))
59b6a2619cSDavid Hildenbrand VM_BUG_ON_PAGE(!PageAnonExclusive(&folio->page), page);
60b6a2619cSDavid Hildenbrand else
61b6a2619cSDavid Hildenbrand /* Either a PTE-mapped or a PMD-mapped THP. */
62b6a2619cSDavid Hildenbrand VM_BUG_ON_PAGE(!PageAnonExclusive(&folio->page) &&
63b6a2619cSDavid Hildenbrand !PageAnonExclusive(page), page);
64b6a2619cSDavid Hildenbrand }
65b6a2619cSDavid Hildenbrand }
66b6a2619cSDavid Hildenbrand
67c24d3732SJann Horn /*
68ece1ed7bSMatthew Wilcox (Oracle) * Return the folio with ref appropriately incremented,
69cd1adf1bSLinus Torvalds * or NULL if that failed.
70a707cdd5SJohn Hubbard */
try_get_folio(struct page * page,int refs)71ece1ed7bSMatthew Wilcox (Oracle) static inline struct folio *try_get_folio(struct page *page, int refs)
72a707cdd5SJohn Hubbard {
73ece1ed7bSMatthew Wilcox (Oracle) struct folio *folio;
74a707cdd5SJohn Hubbard
7559409373SMatthew Wilcox (Oracle) retry:
76ece1ed7bSMatthew Wilcox (Oracle) folio = page_folio(page);
77ece1ed7bSMatthew Wilcox (Oracle) if (WARN_ON_ONCE(folio_ref_count(folio) < 0))
78a707cdd5SJohn Hubbard return NULL;
7916380f52SYang Shi if (unlikely(!folio_ref_try_add(folio, refs)))
80a707cdd5SJohn Hubbard return NULL;
81c24d3732SJann Horn
82c24d3732SJann Horn /*
83ece1ed7bSMatthew Wilcox (Oracle) * At this point we have a stable reference to the folio; but it
84ece1ed7bSMatthew Wilcox (Oracle) * could be that between calling page_folio() and the refcount
85ece1ed7bSMatthew Wilcox (Oracle) * increment, the folio was split, in which case we'd end up
86ece1ed7bSMatthew Wilcox (Oracle) * holding a reference on a folio that has nothing to do with the page
87c24d3732SJann Horn * we were given anymore.
88ece1ed7bSMatthew Wilcox (Oracle) * So now that the folio is stable, recheck that the page still
89ece1ed7bSMatthew Wilcox (Oracle) * belongs to this folio.
90c24d3732SJann Horn */
91ece1ed7bSMatthew Wilcox (Oracle) if (unlikely(page_folio(page) != folio)) {
92f4f451a1SMuchun Song if (!put_devmap_managed_page_refs(&folio->page, refs))
93ece1ed7bSMatthew Wilcox (Oracle) folio_put_refs(folio, refs);
9459409373SMatthew Wilcox (Oracle) goto retry;
95c24d3732SJann Horn }
96c24d3732SJann Horn
97ece1ed7bSMatthew Wilcox (Oracle) return folio;
98a707cdd5SJohn Hubbard }
99a707cdd5SJohn Hubbard
gup_put_folio(struct folio * folio,int refs,unsigned int flags)100d8ddc099SMatthew Wilcox (Oracle) static void gup_put_folio(struct folio *folio, int refs, unsigned int flags)
1014509b42cSJason Gunthorpe {
1024509b42cSJason Gunthorpe if (flags & FOLL_PIN) {
103c8070b78SDavid Howells if (is_zero_folio(folio))
104c8070b78SDavid Howells return;
105d8ddc099SMatthew Wilcox (Oracle) node_stat_mod_folio(folio, NR_FOLL_PIN_RELEASED, refs);
106d8ddc099SMatthew Wilcox (Oracle) if (folio_test_large(folio))
10794688e8eSMatthew Wilcox (Oracle) atomic_sub(refs, &folio->_pincount);
1084509b42cSJason Gunthorpe else
1094509b42cSJason Gunthorpe refs *= GUP_PIN_COUNTING_BIAS;
1104509b42cSJason Gunthorpe }
1114509b42cSJason Gunthorpe
112f4f451a1SMuchun Song if (!put_devmap_managed_page_refs(&folio->page, refs))
113d8ddc099SMatthew Wilcox (Oracle) folio_put_refs(folio, refs);
1144509b42cSJason Gunthorpe }
1154509b42cSJason Gunthorpe
1163faa52c0SJohn Hubbard /**
11726273f5fSYang Shi * try_grab_folio() - add a folio's refcount by a flag-dependent amount
11826273f5fSYang Shi * @folio: pointer to folio to be grabbed
11926273f5fSYang Shi * @refs: the value to (effectively) add to the folio's refcount
12026273f5fSYang Shi * @flags: gup flags: these are the FOLL_* flag values
1213faa52c0SJohn Hubbard *
1223faa52c0SJohn Hubbard * This might not do anything at all, depending on the flags argument.
1233faa52c0SJohn Hubbard *
1243faa52c0SJohn Hubbard * "grab" names in this file mean, "look at flags to decide whether to use
12526273f5fSYang Shi * FOLL_PIN or FOLL_GET behavior, when incrementing the folio's refcount.
1263faa52c0SJohn Hubbard *
1273faa52c0SJohn Hubbard * Either FOLL_PIN or FOLL_GET (or neither) may be set, but not both at the same
12826273f5fSYang Shi * time.
1293faa52c0SJohn Hubbard *
1300f089235SLogan Gunthorpe * Return: 0 for success, or if no action was required (if neither FOLL_PIN
1310f089235SLogan Gunthorpe * nor FOLL_GET was set, nothing is done). A negative error code for failure:
1320f089235SLogan Gunthorpe *
13326273f5fSYang Shi * -ENOMEM FOLL_GET or FOLL_PIN was set, but the folio could not
1340f089235SLogan Gunthorpe * be grabbed.
13526273f5fSYang Shi *
13626273f5fSYang Shi * It is called when we have a stable reference for the folio, typically in
13726273f5fSYang Shi * GUP slow path.
1383faa52c0SJohn Hubbard */
try_grab_folio(struct folio * folio,int refs,unsigned int flags)13926273f5fSYang Shi int __must_check try_grab_folio(struct folio *folio, int refs,
14026273f5fSYang Shi unsigned int flags)
1413faa52c0SJohn Hubbard {
1425fec0719SMatthew Wilcox (Oracle) if (WARN_ON_ONCE(folio_ref_count(folio) <= 0))
1430f089235SLogan Gunthorpe return -ENOMEM;
144c36c04c2SJohn Hubbard
14526273f5fSYang Shi if (unlikely(!(flags & FOLL_PCI_P2PDMA) && is_pci_p2pdma_page(&folio->page)))
1464003f107SLogan Gunthorpe return -EREMOTEIO;
147c36c04c2SJohn Hubbard
1485fec0719SMatthew Wilcox (Oracle) if (flags & FOLL_GET)
14926273f5fSYang Shi folio_ref_add(folio, refs);
1505fec0719SMatthew Wilcox (Oracle) else if (flags & FOLL_PIN) {
151c36c04c2SJohn Hubbard /*
152c8070b78SDavid Howells * Don't take a pin on the zero page - it's not going anywhere
153c8070b78SDavid Howells * and it is used in a *lot* of places.
154c8070b78SDavid Howells */
15526273f5fSYang Shi if (is_zero_folio(folio))
156c8070b78SDavid Howells return 0;
157c8070b78SDavid Howells
158c8070b78SDavid Howells /*
15926273f5fSYang Shi * Increment the normal page refcount field at least once,
16078d9d6ceSMatthew Wilcox (Oracle) * so that the page really is pinned.
161c36c04c2SJohn Hubbard */
1625fec0719SMatthew Wilcox (Oracle) if (folio_test_large(folio)) {
16326273f5fSYang Shi folio_ref_add(folio, refs);
16426273f5fSYang Shi atomic_add(refs, &folio->_pincount);
1658ea2979cSMatthew Wilcox (Oracle) } else {
16626273f5fSYang Shi folio_ref_add(folio, refs * GUP_PIN_COUNTING_BIAS);
1678ea2979cSMatthew Wilcox (Oracle) }
168c36c04c2SJohn Hubbard
16926273f5fSYang Shi node_stat_mod_folio(folio, NR_FOLL_PIN_ACQUIRED, refs);
170c36c04c2SJohn Hubbard }
171c36c04c2SJohn Hubbard
1720f089235SLogan Gunthorpe return 0;
1733faa52c0SJohn Hubbard }
1743faa52c0SJohn Hubbard
1753faa52c0SJohn Hubbard /**
1763faa52c0SJohn Hubbard * unpin_user_page() - release a dma-pinned page
1773faa52c0SJohn Hubbard * @page: pointer to page to be released
1783faa52c0SJohn Hubbard *
1793faa52c0SJohn Hubbard * Pages that were pinned via pin_user_pages*() must be released via either
1803faa52c0SJohn Hubbard * unpin_user_page(), or one of the unpin_user_pages*() routines. This is so
1813faa52c0SJohn Hubbard * that such pages can be separately tracked and uniquely handled. In
1823faa52c0SJohn Hubbard * particular, interactions with RDMA and filesystems need special handling.
1833faa52c0SJohn Hubbard */
unpin_user_page(struct page * page)1843faa52c0SJohn Hubbard void unpin_user_page(struct page *page)
1853faa52c0SJohn Hubbard {
186b6a2619cSDavid Hildenbrand sanity_check_pinned_pages(&page, 1);
187d8ddc099SMatthew Wilcox (Oracle) gup_put_folio(page_folio(page), 1, FOLL_PIN);
1883faa52c0SJohn Hubbard }
1893faa52c0SJohn Hubbard EXPORT_SYMBOL(unpin_user_page);
1903faa52c0SJohn Hubbard
1911101fb8fSDavid Howells /**
1921101fb8fSDavid Howells * folio_add_pin - Try to get an additional pin on a pinned folio
1931101fb8fSDavid Howells * @folio: The folio to be pinned
1941101fb8fSDavid Howells *
1951101fb8fSDavid Howells * Get an additional pin on a folio we already have a pin on. Makes no change
1961101fb8fSDavid Howells * if the folio is a zero_page.
1971101fb8fSDavid Howells */
folio_add_pin(struct folio * folio)1981101fb8fSDavid Howells void folio_add_pin(struct folio *folio)
1991101fb8fSDavid Howells {
2001101fb8fSDavid Howells if (is_zero_folio(folio))
2011101fb8fSDavid Howells return;
2021101fb8fSDavid Howells
2031101fb8fSDavid Howells /*
2041101fb8fSDavid Howells * Similar to try_grab_folio(): be sure to *also* increment the normal
2051101fb8fSDavid Howells * page refcount field at least once, so that the page really is
2061101fb8fSDavid Howells * pinned.
2071101fb8fSDavid Howells */
2081101fb8fSDavid Howells if (folio_test_large(folio)) {
2091101fb8fSDavid Howells WARN_ON_ONCE(atomic_read(&folio->_pincount) < 1);
2101101fb8fSDavid Howells folio_ref_inc(folio);
2111101fb8fSDavid Howells atomic_inc(&folio->_pincount);
2121101fb8fSDavid Howells } else {
2131101fb8fSDavid Howells WARN_ON_ONCE(folio_ref_count(folio) < GUP_PIN_COUNTING_BIAS);
2141101fb8fSDavid Howells folio_ref_add(folio, GUP_PIN_COUNTING_BIAS);
2151101fb8fSDavid Howells }
2161101fb8fSDavid Howells }
2171101fb8fSDavid Howells
gup_folio_range_next(struct page * start,unsigned long npages,unsigned long i,unsigned int * ntails)218659508f9SMatthew Wilcox (Oracle) static inline struct folio *gup_folio_range_next(struct page *start,
2198f39f5fcSMatthew Wilcox (Oracle) unsigned long npages, unsigned long i, unsigned int *ntails)
220458a4f78SJoao Martins {
221659508f9SMatthew Wilcox (Oracle) struct page *next = nth_page(start, i);
222659508f9SMatthew Wilcox (Oracle) struct folio *folio = page_folio(next);
223458a4f78SJoao Martins unsigned int nr = 1;
224458a4f78SJoao Martins
225659508f9SMatthew Wilcox (Oracle) if (folio_test_large(folio))
2264c654229SMatthew Wilcox (Oracle) nr = min_t(unsigned int, npages - i,
227659508f9SMatthew Wilcox (Oracle) folio_nr_pages(folio) - folio_page_idx(folio, next));
228458a4f78SJoao Martins
229458a4f78SJoao Martins *ntails = nr;
230659508f9SMatthew Wilcox (Oracle) return folio;
231458a4f78SJoao Martins }
232458a4f78SJoao Martins
gup_folio_next(struct page ** list,unsigned long npages,unsigned long i,unsigned int * ntails)23312521c76SMatthew Wilcox (Oracle) static inline struct folio *gup_folio_next(struct page **list,
23428297dbcSMatthew Wilcox (Oracle) unsigned long npages, unsigned long i, unsigned int *ntails)
2358745d7f6SJoao Martins {
23612521c76SMatthew Wilcox (Oracle) struct folio *folio = page_folio(list[i]);
2378745d7f6SJoao Martins unsigned int nr;
2388745d7f6SJoao Martins
2398745d7f6SJoao Martins for (nr = i + 1; nr < npages; nr++) {
24012521c76SMatthew Wilcox (Oracle) if (page_folio(list[nr]) != folio)
2418745d7f6SJoao Martins break;
2428745d7f6SJoao Martins }
2438745d7f6SJoao Martins
2448745d7f6SJoao Martins *ntails = nr - i;
24512521c76SMatthew Wilcox (Oracle) return folio;
2468745d7f6SJoao Martins }
2478745d7f6SJoao Martins
248fc1d8e7cSJohn Hubbard /**
249f1f6a7ddSJohn Hubbard * unpin_user_pages_dirty_lock() - release and optionally dirty gup-pinned pages
2502d15eb31Sakpm@linux-foundation.org * @pages: array of pages to be maybe marked dirty, and definitely released.
251fc1d8e7cSJohn Hubbard * @npages: number of pages in the @pages array.
2522d15eb31Sakpm@linux-foundation.org * @make_dirty: whether to mark the pages dirty
253fc1d8e7cSJohn Hubbard *
254fc1d8e7cSJohn Hubbard * "gup-pinned page" refers to a page that has had one of the get_user_pages()
255fc1d8e7cSJohn Hubbard * variants called on that page.
256fc1d8e7cSJohn Hubbard *
257fc1d8e7cSJohn Hubbard * For each page in the @pages array, make that page (or its head page, if a
2582d15eb31Sakpm@linux-foundation.org * compound page) dirty, if @make_dirty is true, and if the page was previously
259f1f6a7ddSJohn Hubbard * listed as clean. In any case, releases all pages using unpin_user_page(),
260f1f6a7ddSJohn Hubbard * possibly via unpin_user_pages(), for the non-dirty case.
261fc1d8e7cSJohn Hubbard *
262f1f6a7ddSJohn Hubbard * Please see the unpin_user_page() documentation for details.
263fc1d8e7cSJohn Hubbard *
2642d15eb31Sakpm@linux-foundation.org * set_page_dirty_lock() is used internally. If instead, set_page_dirty() is
2652d15eb31Sakpm@linux-foundation.org * required, then the caller should a) verify that this is really correct,
2662d15eb31Sakpm@linux-foundation.org * because _lock() is usually required, and b) hand code it:
267f1f6a7ddSJohn Hubbard * set_page_dirty_lock(), unpin_user_page().
268fc1d8e7cSJohn Hubbard *
269fc1d8e7cSJohn Hubbard */
unpin_user_pages_dirty_lock(struct page ** pages,unsigned long npages,bool make_dirty)270f1f6a7ddSJohn Hubbard void unpin_user_pages_dirty_lock(struct page **pages, unsigned long npages,
2712d15eb31Sakpm@linux-foundation.org bool make_dirty)
272fc1d8e7cSJohn Hubbard {
27312521c76SMatthew Wilcox (Oracle) unsigned long i;
27412521c76SMatthew Wilcox (Oracle) struct folio *folio;
27512521c76SMatthew Wilcox (Oracle) unsigned int nr;
2762d15eb31Sakpm@linux-foundation.org
2772d15eb31Sakpm@linux-foundation.org if (!make_dirty) {
278f1f6a7ddSJohn Hubbard unpin_user_pages(pages, npages);
2792d15eb31Sakpm@linux-foundation.org return;
2802d15eb31Sakpm@linux-foundation.org }
2812d15eb31Sakpm@linux-foundation.org
282b6a2619cSDavid Hildenbrand sanity_check_pinned_pages(pages, npages);
28312521c76SMatthew Wilcox (Oracle) for (i = 0; i < npages; i += nr) {
28412521c76SMatthew Wilcox (Oracle) folio = gup_folio_next(pages, npages, i, &nr);
2852d15eb31Sakpm@linux-foundation.org /*
2862d15eb31Sakpm@linux-foundation.org * Checking PageDirty at this point may race with
2872d15eb31Sakpm@linux-foundation.org * clear_page_dirty_for_io(), but that's OK. Two key
2882d15eb31Sakpm@linux-foundation.org * cases:
2892d15eb31Sakpm@linux-foundation.org *
2902d15eb31Sakpm@linux-foundation.org * 1) This code sees the page as already dirty, so it
2912d15eb31Sakpm@linux-foundation.org * skips the call to set_page_dirty(). That could happen
2922d15eb31Sakpm@linux-foundation.org * because clear_page_dirty_for_io() called
2932d15eb31Sakpm@linux-foundation.org * page_mkclean(), followed by set_page_dirty().
2942d15eb31Sakpm@linux-foundation.org * However, now the page is going to get written back,
2952d15eb31Sakpm@linux-foundation.org * which meets the original intention of setting it
2962d15eb31Sakpm@linux-foundation.org * dirty, so all is well: clear_page_dirty_for_io() goes
2972d15eb31Sakpm@linux-foundation.org * on to call TestClearPageDirty(), and write the page
2982d15eb31Sakpm@linux-foundation.org * back.
2992d15eb31Sakpm@linux-foundation.org *
3002d15eb31Sakpm@linux-foundation.org * 2) This code sees the page as clean, so it calls
3012d15eb31Sakpm@linux-foundation.org * set_page_dirty(). The page stays dirty, despite being
3022d15eb31Sakpm@linux-foundation.org * written back, so it gets written back again in the
3032d15eb31Sakpm@linux-foundation.org * next writeback cycle. This is harmless.
3042d15eb31Sakpm@linux-foundation.org */
30512521c76SMatthew Wilcox (Oracle) if (!folio_test_dirty(folio)) {
30612521c76SMatthew Wilcox (Oracle) folio_lock(folio);
30712521c76SMatthew Wilcox (Oracle) folio_mark_dirty(folio);
30812521c76SMatthew Wilcox (Oracle) folio_unlock(folio);
30912521c76SMatthew Wilcox (Oracle) }
31012521c76SMatthew Wilcox (Oracle) gup_put_folio(folio, nr, FOLL_PIN);
3112d15eb31Sakpm@linux-foundation.org }
312fc1d8e7cSJohn Hubbard }
313f1f6a7ddSJohn Hubbard EXPORT_SYMBOL(unpin_user_pages_dirty_lock);
314fc1d8e7cSJohn Hubbard
315fc1d8e7cSJohn Hubbard /**
316458a4f78SJoao Martins * unpin_user_page_range_dirty_lock() - release and optionally dirty
317458a4f78SJoao Martins * gup-pinned page range
318458a4f78SJoao Martins *
319458a4f78SJoao Martins * @page: the starting page of a range maybe marked dirty, and definitely released.
320458a4f78SJoao Martins * @npages: number of consecutive pages to release.
321458a4f78SJoao Martins * @make_dirty: whether to mark the pages dirty
322458a4f78SJoao Martins *
323458a4f78SJoao Martins * "gup-pinned page range" refers to a range of pages that has had one of the
324458a4f78SJoao Martins * pin_user_pages() variants called on that page.
325458a4f78SJoao Martins *
326458a4f78SJoao Martins * For the page ranges defined by [page .. page+npages], make that range (or
327458a4f78SJoao Martins * its head pages, if a compound page) dirty, if @make_dirty is true, and if the
328458a4f78SJoao Martins * page range was previously listed as clean.
329458a4f78SJoao Martins *
330458a4f78SJoao Martins * set_page_dirty_lock() is used internally. If instead, set_page_dirty() is
331458a4f78SJoao Martins * required, then the caller should a) verify that this is really correct,
332458a4f78SJoao Martins * because _lock() is usually required, and b) hand code it:
333458a4f78SJoao Martins * set_page_dirty_lock(), unpin_user_page().
334458a4f78SJoao Martins *
335458a4f78SJoao Martins */
unpin_user_page_range_dirty_lock(struct page * page,unsigned long npages,bool make_dirty)336458a4f78SJoao Martins void unpin_user_page_range_dirty_lock(struct page *page, unsigned long npages,
337458a4f78SJoao Martins bool make_dirty)
338458a4f78SJoao Martins {
339659508f9SMatthew Wilcox (Oracle) unsigned long i;
340659508f9SMatthew Wilcox (Oracle) struct folio *folio;
341659508f9SMatthew Wilcox (Oracle) unsigned int nr;
342458a4f78SJoao Martins
343659508f9SMatthew Wilcox (Oracle) for (i = 0; i < npages; i += nr) {
344659508f9SMatthew Wilcox (Oracle) folio = gup_folio_range_next(page, npages, i, &nr);
345659508f9SMatthew Wilcox (Oracle) if (make_dirty && !folio_test_dirty(folio)) {
346659508f9SMatthew Wilcox (Oracle) folio_lock(folio);
347659508f9SMatthew Wilcox (Oracle) folio_mark_dirty(folio);
348659508f9SMatthew Wilcox (Oracle) folio_unlock(folio);
349659508f9SMatthew Wilcox (Oracle) }
350659508f9SMatthew Wilcox (Oracle) gup_put_folio(folio, nr, FOLL_PIN);
351458a4f78SJoao Martins }
352458a4f78SJoao Martins }
353458a4f78SJoao Martins EXPORT_SYMBOL(unpin_user_page_range_dirty_lock);
354458a4f78SJoao Martins
unpin_user_pages_lockless(struct page ** pages,unsigned long npages)355b6a2619cSDavid Hildenbrand static void unpin_user_pages_lockless(struct page **pages, unsigned long npages)
356b6a2619cSDavid Hildenbrand {
357b6a2619cSDavid Hildenbrand unsigned long i;
358b6a2619cSDavid Hildenbrand struct folio *folio;
359b6a2619cSDavid Hildenbrand unsigned int nr;
360b6a2619cSDavid Hildenbrand
361b6a2619cSDavid Hildenbrand /*
362b6a2619cSDavid Hildenbrand * Don't perform any sanity checks because we might have raced with
363b6a2619cSDavid Hildenbrand * fork() and some anonymous pages might now actually be shared --
364b6a2619cSDavid Hildenbrand * which is why we're unpinning after all.
365b6a2619cSDavid Hildenbrand */
366b6a2619cSDavid Hildenbrand for (i = 0; i < npages; i += nr) {
367b6a2619cSDavid Hildenbrand folio = gup_folio_next(pages, npages, i, &nr);
368b6a2619cSDavid Hildenbrand gup_put_folio(folio, nr, FOLL_PIN);
369b6a2619cSDavid Hildenbrand }
370b6a2619cSDavid Hildenbrand }
371b6a2619cSDavid Hildenbrand
372458a4f78SJoao Martins /**
373f1f6a7ddSJohn Hubbard * unpin_user_pages() - release an array of gup-pinned pages.
374fc1d8e7cSJohn Hubbard * @pages: array of pages to be marked dirty and released.
375fc1d8e7cSJohn Hubbard * @npages: number of pages in the @pages array.
376fc1d8e7cSJohn Hubbard *
377f1f6a7ddSJohn Hubbard * For each page in the @pages array, release the page using unpin_user_page().
378fc1d8e7cSJohn Hubbard *
379f1f6a7ddSJohn Hubbard * Please see the unpin_user_page() documentation for details.
380fc1d8e7cSJohn Hubbard */
unpin_user_pages(struct page ** pages,unsigned long npages)381f1f6a7ddSJohn Hubbard void unpin_user_pages(struct page **pages, unsigned long npages)
382fc1d8e7cSJohn Hubbard {
38312521c76SMatthew Wilcox (Oracle) unsigned long i;
38412521c76SMatthew Wilcox (Oracle) struct folio *folio;
38512521c76SMatthew Wilcox (Oracle) unsigned int nr;
386fc1d8e7cSJohn Hubbard
387fc1d8e7cSJohn Hubbard /*
388146608bbSJohn Hubbard * If this WARN_ON() fires, then the system *might* be leaking pages (by
389146608bbSJohn Hubbard * leaving them pinned), but probably not. More likely, gup/pup returned
390146608bbSJohn Hubbard * a hard -ERRNO error to the caller, who erroneously passed it here.
391146608bbSJohn Hubbard */
392146608bbSJohn Hubbard if (WARN_ON(IS_ERR_VALUE(npages)))
393146608bbSJohn Hubbard return;
39431b912deSJoao Martins
395b6a2619cSDavid Hildenbrand sanity_check_pinned_pages(pages, npages);
39612521c76SMatthew Wilcox (Oracle) for (i = 0; i < npages; i += nr) {
39712521c76SMatthew Wilcox (Oracle) folio = gup_folio_next(pages, npages, i, &nr);
39812521c76SMatthew Wilcox (Oracle) gup_put_folio(folio, nr, FOLL_PIN);
399fc1d8e7cSJohn Hubbard }
400fc1d8e7cSJohn Hubbard }
401f1f6a7ddSJohn Hubbard EXPORT_SYMBOL(unpin_user_pages);
402fc1d8e7cSJohn Hubbard
403a458b76aSAndrea Arcangeli /*
404a458b76aSAndrea Arcangeli * Set the MMF_HAS_PINNED if not set yet; after set it'll be there for the mm's
405a458b76aSAndrea Arcangeli * lifecycle. Avoid setting the bit unless necessary, or it might cause write
406a458b76aSAndrea Arcangeli * cache bouncing on large SMP machines for concurrent pinned gups.
407a458b76aSAndrea Arcangeli */
mm_set_has_pinned_flag(unsigned long * mm_flags)408a458b76aSAndrea Arcangeli static inline void mm_set_has_pinned_flag(unsigned long *mm_flags)
409a458b76aSAndrea Arcangeli {
410a458b76aSAndrea Arcangeli if (!test_bit(MMF_HAS_PINNED, mm_flags))
411a458b76aSAndrea Arcangeli set_bit(MMF_HAS_PINNED, mm_flags);
412a458b76aSAndrea Arcangeli }
413a458b76aSAndrea Arcangeli
414050a9adcSChristoph Hellwig #ifdef CONFIG_MMU
no_page_table(struct vm_area_struct * vma,unsigned int flags)41569e68b4fSKirill A. Shutemov static struct page *no_page_table(struct vm_area_struct *vma,
41669e68b4fSKirill A. Shutemov unsigned int flags)
4174bbd4c77SKirill A. Shutemov {
4184bbd4c77SKirill A. Shutemov /*
41969e68b4fSKirill A. Shutemov * When core dumping an enormous anonymous area that nobody
42069e68b4fSKirill A. Shutemov * has touched so far, we don't want to allocate unnecessary pages or
42169e68b4fSKirill A. Shutemov * page tables. Return error instead of NULL to skip handle_mm_fault,
42269e68b4fSKirill A. Shutemov * then get_dump_page() will return NULL to leave a hole in the dump.
42369e68b4fSKirill A. Shutemov * But we can only make this optimization where a hole would surely
42469e68b4fSKirill A. Shutemov * be zero-filled if handle_mm_fault() actually did handle it.
4254bbd4c77SKirill A. Shutemov */
426a0137f16SAnshuman Khandual if ((flags & FOLL_DUMP) &&
427a0137f16SAnshuman Khandual (vma_is_anonymous(vma) || !vma->vm_ops->fault))
42869e68b4fSKirill A. Shutemov return ERR_PTR(-EFAULT);
42969e68b4fSKirill A. Shutemov return NULL;
4304bbd4c77SKirill A. Shutemov }
43169e68b4fSKirill A. Shutemov
follow_pfn_pte(struct vm_area_struct * vma,unsigned long address,pte_t * pte,unsigned int flags)4321027e443SKirill A. Shutemov static int follow_pfn_pte(struct vm_area_struct *vma, unsigned long address,
4331027e443SKirill A. Shutemov pte_t *pte, unsigned int flags)
4341027e443SKirill A. Shutemov {
4351027e443SKirill A. Shutemov if (flags & FOLL_TOUCH) {
436c33c7948SRyan Roberts pte_t orig_entry = ptep_get(pte);
437c33c7948SRyan Roberts pte_t entry = orig_entry;
4381027e443SKirill A. Shutemov
4391027e443SKirill A. Shutemov if (flags & FOLL_WRITE)
4401027e443SKirill A. Shutemov entry = pte_mkdirty(entry);
4411027e443SKirill A. Shutemov entry = pte_mkyoung(entry);
4421027e443SKirill A. Shutemov
443c33c7948SRyan Roberts if (!pte_same(orig_entry, entry)) {
4441027e443SKirill A. Shutemov set_pte_at(vma->vm_mm, address, pte, entry);
4451027e443SKirill A. Shutemov update_mmu_cache(vma, address, pte);
4461027e443SKirill A. Shutemov }
4471027e443SKirill A. Shutemov }
4481027e443SKirill A. Shutemov
4491027e443SKirill A. Shutemov /* Proper page table entry exists, but no corresponding struct page */
4501027e443SKirill A. Shutemov return -EEXIST;
4511027e443SKirill A. Shutemov }
4521027e443SKirill A. Shutemov
4535535be30SDavid Hildenbrand /* FOLL_FORCE can write to even unwritable PTEs in COW mappings. */
can_follow_write_pte(pte_t pte,struct page * page,struct vm_area_struct * vma,unsigned int flags)4545535be30SDavid Hildenbrand static inline bool can_follow_write_pte(pte_t pte, struct page *page,
4555535be30SDavid Hildenbrand struct vm_area_struct *vma,
4565535be30SDavid Hildenbrand unsigned int flags)
45719be0eafSLinus Torvalds {
4585535be30SDavid Hildenbrand /* If the pte is writable, we can write to the page. */
4595535be30SDavid Hildenbrand if (pte_write(pte))
4605535be30SDavid Hildenbrand return true;
4615535be30SDavid Hildenbrand
4625535be30SDavid Hildenbrand /* Maybe FOLL_FORCE is set to override it? */
4635535be30SDavid Hildenbrand if (!(flags & FOLL_FORCE))
4645535be30SDavid Hildenbrand return false;
4655535be30SDavid Hildenbrand
4665535be30SDavid Hildenbrand /* But FOLL_FORCE has no effect on shared mappings */
4675535be30SDavid Hildenbrand if (vma->vm_flags & (VM_MAYSHARE | VM_SHARED))
4685535be30SDavid Hildenbrand return false;
4695535be30SDavid Hildenbrand
4705535be30SDavid Hildenbrand /* ... or read-only private ones */
4715535be30SDavid Hildenbrand if (!(vma->vm_flags & VM_MAYWRITE))
4725535be30SDavid Hildenbrand return false;
4735535be30SDavid Hildenbrand
4745535be30SDavid Hildenbrand /* ... or already writable ones that just need to take a write fault */
4755535be30SDavid Hildenbrand if (vma->vm_flags & VM_WRITE)
4765535be30SDavid Hildenbrand return false;
4775535be30SDavid Hildenbrand
4785535be30SDavid Hildenbrand /*
4795535be30SDavid Hildenbrand * See can_change_pte_writable(): we broke COW and could map the page
4805535be30SDavid Hildenbrand * writable if we have an exclusive anonymous page ...
4815535be30SDavid Hildenbrand */
4825535be30SDavid Hildenbrand if (!page || !PageAnon(page) || !PageAnonExclusive(page))
4835535be30SDavid Hildenbrand return false;
4845535be30SDavid Hildenbrand
4855535be30SDavid Hildenbrand /* ... and a write-fault isn't required for other reasons. */
4865535be30SDavid Hildenbrand if (vma_soft_dirty_enabled(vma) && !pte_soft_dirty(pte))
4875535be30SDavid Hildenbrand return false;
4885535be30SDavid Hildenbrand return !userfaultfd_pte_wp(vma, pte);
48919be0eafSLinus Torvalds }
49019be0eafSLinus Torvalds
follow_page_pte(struct vm_area_struct * vma,unsigned long address,pmd_t * pmd,unsigned int flags,struct dev_pagemap ** pgmap)49169e68b4fSKirill A. Shutemov static struct page *follow_page_pte(struct vm_area_struct *vma,
492df06b37fSKeith Busch unsigned long address, pmd_t *pmd, unsigned int flags,
493df06b37fSKeith Busch struct dev_pagemap **pgmap)
49469e68b4fSKirill A. Shutemov {
49569e68b4fSKirill A. Shutemov struct mm_struct *mm = vma->vm_mm;
49669e68b4fSKirill A. Shutemov struct page *page;
49769e68b4fSKirill A. Shutemov spinlock_t *ptl;
49869e68b4fSKirill A. Shutemov pte_t *ptep, pte;
499f28d4363SClaudio Imbrenda int ret;
50069e68b4fSKirill A. Shutemov
501eddb1c22SJohn Hubbard /* FOLL_GET and FOLL_PIN are mutually exclusive. */
502eddb1c22SJohn Hubbard if (WARN_ON_ONCE((flags & (FOLL_PIN | FOLL_GET)) ==
503eddb1c22SJohn Hubbard (FOLL_PIN | FOLL_GET)))
504eddb1c22SJohn Hubbard return ERR_PTR(-EINVAL);
5054bbd4c77SKirill A. Shutemov
5064bbd4c77SKirill A. Shutemov ptep = pte_offset_map_lock(mm, pmd, address, &ptl);
50704dee9e8SHugh Dickins if (!ptep)
50804dee9e8SHugh Dickins return no_page_table(vma, flags);
509c33c7948SRyan Roberts pte = ptep_get(ptep);
510f7355e99SDavid Hildenbrand if (!pte_present(pte))
5114bbd4c77SKirill A. Shutemov goto no_page;
512d74943a2SDavid Hildenbrand if (pte_protnone(pte) && !gup_can_follow_protnone(vma, flags))
5134bbd4c77SKirill A. Shutemov goto no_page;
5144bbd4c77SKirill A. Shutemov
5154bbd4c77SKirill A. Shutemov page = vm_normal_page(vma, address, pte);
5165535be30SDavid Hildenbrand
5175535be30SDavid Hildenbrand /*
5185535be30SDavid Hildenbrand * We only care about anon pages in can_follow_write_pte() and don't
5195535be30SDavid Hildenbrand * have to worry about pte_devmap() because they are never anon.
5205535be30SDavid Hildenbrand */
5215535be30SDavid Hildenbrand if ((flags & FOLL_WRITE) &&
5225535be30SDavid Hildenbrand !can_follow_write_pte(pte, page, vma, flags)) {
5235535be30SDavid Hildenbrand page = NULL;
5245535be30SDavid Hildenbrand goto out;
5255535be30SDavid Hildenbrand }
5265535be30SDavid Hildenbrand
5273faa52c0SJohn Hubbard if (!page && pte_devmap(pte) && (flags & (FOLL_GET | FOLL_PIN))) {
5283565fce3SDan Williams /*
5293faa52c0SJohn Hubbard * Only return device mapping pages in the FOLL_GET or FOLL_PIN
5303faa52c0SJohn Hubbard * case since they are only valid while holding the pgmap
5313faa52c0SJohn Hubbard * reference.
5323565fce3SDan Williams */
533df06b37fSKeith Busch *pgmap = get_dev_pagemap(pte_pfn(pte), *pgmap);
534df06b37fSKeith Busch if (*pgmap)
5353565fce3SDan Williams page = pte_page(pte);
5363565fce3SDan Williams else
5373565fce3SDan Williams goto no_page;
5383565fce3SDan Williams } else if (unlikely(!page)) {
5391027e443SKirill A. Shutemov if (flags & FOLL_DUMP) {
5401027e443SKirill A. Shutemov /* Avoid special (like zero) pages in core dumps */
5411027e443SKirill A. Shutemov page = ERR_PTR(-EFAULT);
5421027e443SKirill A. Shutemov goto out;
5431027e443SKirill A. Shutemov }
5441027e443SKirill A. Shutemov
5451027e443SKirill A. Shutemov if (is_zero_pfn(pte_pfn(pte))) {
5464bbd4c77SKirill A. Shutemov page = pte_page(pte);
5471027e443SKirill A. Shutemov } else {
5481027e443SKirill A. Shutemov ret = follow_pfn_pte(vma, address, ptep, flags);
5491027e443SKirill A. Shutemov page = ERR_PTR(ret);
5501027e443SKirill A. Shutemov goto out;
5511027e443SKirill A. Shutemov }
5524bbd4c77SKirill A. Shutemov }
5534bbd4c77SKirill A. Shutemov
55484209e87SDavid Hildenbrand if (!pte_write(pte) && gup_must_unshare(vma, flags, page)) {
555a7f22660SDavid Hildenbrand page = ERR_PTR(-EMLINK);
556a7f22660SDavid Hildenbrand goto out;
557a7f22660SDavid Hildenbrand }
558b6a2619cSDavid Hildenbrand
559b6a2619cSDavid Hildenbrand VM_BUG_ON_PAGE((flags & FOLL_PIN) && PageAnon(page) &&
560b6a2619cSDavid Hildenbrand !PageAnonExclusive(page), page);
561b6a2619cSDavid Hildenbrand
56226273f5fSYang Shi /* try_grab_folio() does nothing unless FOLL_GET or FOLL_PIN is set. */
56326273f5fSYang Shi ret = try_grab_folio(page_folio(page), 1, flags);
5640f089235SLogan Gunthorpe if (unlikely(ret)) {
5650f089235SLogan Gunthorpe page = ERR_PTR(ret);
5668fde12caSLinus Torvalds goto out;
5678fde12caSLinus Torvalds }
5684003f107SLogan Gunthorpe
569f28d4363SClaudio Imbrenda /*
570f28d4363SClaudio Imbrenda * We need to make the page accessible if and only if we are going
571f28d4363SClaudio Imbrenda * to access its content (the FOLL_PIN case). Please see
572f28d4363SClaudio Imbrenda * Documentation/core-api/pin_user_pages.rst for details.
573f28d4363SClaudio Imbrenda */
574f28d4363SClaudio Imbrenda if (flags & FOLL_PIN) {
575f28d4363SClaudio Imbrenda ret = arch_make_page_accessible(page);
576f28d4363SClaudio Imbrenda if (ret) {
577f28d4363SClaudio Imbrenda unpin_user_page(page);
578f28d4363SClaudio Imbrenda page = ERR_PTR(ret);
579f28d4363SClaudio Imbrenda goto out;
580f28d4363SClaudio Imbrenda }
581f28d4363SClaudio Imbrenda }
5824bbd4c77SKirill A. Shutemov if (flags & FOLL_TOUCH) {
5834bbd4c77SKirill A. Shutemov if ((flags & FOLL_WRITE) &&
5844bbd4c77SKirill A. Shutemov !pte_dirty(pte) && !PageDirty(page))
5854bbd4c77SKirill A. Shutemov set_page_dirty(page);
5864bbd4c77SKirill A. Shutemov /*
5874bbd4c77SKirill A. Shutemov * pte_mkyoung() would be more correct here, but atomic care
5884bbd4c77SKirill A. Shutemov * is needed to avoid losing the dirty bit: it is easier to use
5894bbd4c77SKirill A. Shutemov * mark_page_accessed().
5904bbd4c77SKirill A. Shutemov */
5914bbd4c77SKirill A. Shutemov mark_page_accessed(page);
5924bbd4c77SKirill A. Shutemov }
5931027e443SKirill A. Shutemov out:
5944bbd4c77SKirill A. Shutemov pte_unmap_unlock(ptep, ptl);
5954bbd4c77SKirill A. Shutemov return page;
5964bbd4c77SKirill A. Shutemov no_page:
5974bbd4c77SKirill A. Shutemov pte_unmap_unlock(ptep, ptl);
5984bbd4c77SKirill A. Shutemov if (!pte_none(pte))
59969e68b4fSKirill A. Shutemov return NULL;
60069e68b4fSKirill A. Shutemov return no_page_table(vma, flags);
60169e68b4fSKirill A. Shutemov }
6024bbd4c77SKirill A. Shutemov
follow_pmd_mask(struct vm_area_struct * vma,unsigned long address,pud_t * pudp,unsigned int flags,struct follow_page_context * ctx)603080dbb61SAneesh Kumar K.V static struct page *follow_pmd_mask(struct vm_area_struct *vma,
604080dbb61SAneesh Kumar K.V unsigned long address, pud_t *pudp,
605df06b37fSKeith Busch unsigned int flags,
606df06b37fSKeith Busch struct follow_page_context *ctx)
60769e68b4fSKirill A. Shutemov {
60868827280SHuang Ying pmd_t *pmd, pmdval;
60969e68b4fSKirill A. Shutemov spinlock_t *ptl;
61069e68b4fSKirill A. Shutemov struct page *page;
61169e68b4fSKirill A. Shutemov struct mm_struct *mm = vma->vm_mm;
61269e68b4fSKirill A. Shutemov
613080dbb61SAneesh Kumar K.V pmd = pmd_offset(pudp, address);
61426e1a0c3SHugh Dickins pmdval = pmdp_get_lockless(pmd);
61568827280SHuang Ying if (pmd_none(pmdval))
61669e68b4fSKirill A. Shutemov return no_page_table(vma, flags);
617f7355e99SDavid Hildenbrand if (!pmd_present(pmdval))
618e66f17ffSNaoya Horiguchi return no_page_table(vma, flags);
61968827280SHuang Ying if (pmd_devmap(pmdval)) {
6203565fce3SDan Williams ptl = pmd_lock(mm, pmd);
621df06b37fSKeith Busch page = follow_devmap_pmd(vma, address, pmd, flags, &ctx->pgmap);
6223565fce3SDan Williams spin_unlock(ptl);
6233565fce3SDan Williams if (page)
6243565fce3SDan Williams return page;
6253565fce3SDan Williams }
62668827280SHuang Ying if (likely(!pmd_trans_huge(pmdval)))
627df06b37fSKeith Busch return follow_page_pte(vma, address, pmd, flags, &ctx->pgmap);
6286742d293SKirill A. Shutemov
629d74943a2SDavid Hildenbrand if (pmd_protnone(pmdval) && !gup_can_follow_protnone(vma, flags))
630db08f203SAneesh Kumar K.V return no_page_table(vma, flags);
631db08f203SAneesh Kumar K.V
6326742d293SKirill A. Shutemov ptl = pmd_lock(mm, pmd);
63384c3fc4eSZi Yan if (unlikely(!pmd_present(*pmd))) {
63484c3fc4eSZi Yan spin_unlock(ptl);
63584c3fc4eSZi Yan return no_page_table(vma, flags);
63684c3fc4eSZi Yan }
6376742d293SKirill A. Shutemov if (unlikely(!pmd_trans_huge(*pmd))) {
6386742d293SKirill A. Shutemov spin_unlock(ptl);
639df06b37fSKeith Busch return follow_page_pte(vma, address, pmd, flags, &ctx->pgmap);
64069e68b4fSKirill A. Shutemov }
6414066c119SYang Shi if (flags & FOLL_SPLIT_PMD) {
642bfe7b00dSSong Liu spin_unlock(ptl);
643bfe7b00dSSong Liu split_huge_pmd(vma, pmd, address);
6442378118bSHugh Dickins /* If pmd was left empty, stuff a page table in there quickly */
6452378118bSHugh Dickins return pte_alloc(mm, pmd) ? ERR_PTR(-ENOMEM) :
646df06b37fSKeith Busch follow_page_pte(vma, address, pmd, flags, &ctx->pgmap);
6476742d293SKirill A. Shutemov }
6486742d293SKirill A. Shutemov page = follow_trans_huge_pmd(vma, address, pmd, flags);
64969e68b4fSKirill A. Shutemov spin_unlock(ptl);
650df06b37fSKeith Busch ctx->page_mask = HPAGE_PMD_NR - 1;
65169e68b4fSKirill A. Shutemov return page;
65269e68b4fSKirill A. Shutemov }
65369e68b4fSKirill A. Shutemov
follow_pud_mask(struct vm_area_struct * vma,unsigned long address,p4d_t * p4dp,unsigned int flags,struct follow_page_context * ctx)654080dbb61SAneesh Kumar K.V static struct page *follow_pud_mask(struct vm_area_struct *vma,
655080dbb61SAneesh Kumar K.V unsigned long address, p4d_t *p4dp,
656df06b37fSKeith Busch unsigned int flags,
657df06b37fSKeith Busch struct follow_page_context *ctx)
658080dbb61SAneesh Kumar K.V {
659080dbb61SAneesh Kumar K.V pud_t *pud;
660080dbb61SAneesh Kumar K.V spinlock_t *ptl;
661080dbb61SAneesh Kumar K.V struct page *page;
662080dbb61SAneesh Kumar K.V struct mm_struct *mm = vma->vm_mm;
663080dbb61SAneesh Kumar K.V
664080dbb61SAneesh Kumar K.V pud = pud_offset(p4dp, address);
665080dbb61SAneesh Kumar K.V if (pud_none(*pud))
666080dbb61SAneesh Kumar K.V return no_page_table(vma, flags);
667080dbb61SAneesh Kumar K.V if (pud_devmap(*pud)) {
668080dbb61SAneesh Kumar K.V ptl = pud_lock(mm, pud);
669df06b37fSKeith Busch page = follow_devmap_pud(vma, address, pud, flags, &ctx->pgmap);
670080dbb61SAneesh Kumar K.V spin_unlock(ptl);
671080dbb61SAneesh Kumar K.V if (page)
672080dbb61SAneesh Kumar K.V return page;
673080dbb61SAneesh Kumar K.V }
674080dbb61SAneesh Kumar K.V if (unlikely(pud_bad(*pud)))
675080dbb61SAneesh Kumar K.V return no_page_table(vma, flags);
676080dbb61SAneesh Kumar K.V
677df06b37fSKeith Busch return follow_pmd_mask(vma, address, pud, flags, ctx);
678080dbb61SAneesh Kumar K.V }
679080dbb61SAneesh Kumar K.V
follow_p4d_mask(struct vm_area_struct * vma,unsigned long address,pgd_t * pgdp,unsigned int flags,struct follow_page_context * ctx)680080dbb61SAneesh Kumar K.V static struct page *follow_p4d_mask(struct vm_area_struct *vma,
681080dbb61SAneesh Kumar K.V unsigned long address, pgd_t *pgdp,
682df06b37fSKeith Busch unsigned int flags,
683df06b37fSKeith Busch struct follow_page_context *ctx)
684080dbb61SAneesh Kumar K.V {
685080dbb61SAneesh Kumar K.V p4d_t *p4d;
686080dbb61SAneesh Kumar K.V
687080dbb61SAneesh Kumar K.V p4d = p4d_offset(pgdp, address);
688080dbb61SAneesh Kumar K.V if (p4d_none(*p4d))
689080dbb61SAneesh Kumar K.V return no_page_table(vma, flags);
690080dbb61SAneesh Kumar K.V BUILD_BUG_ON(p4d_huge(*p4d));
691080dbb61SAneesh Kumar K.V if (unlikely(p4d_bad(*p4d)))
692080dbb61SAneesh Kumar K.V return no_page_table(vma, flags);
693080dbb61SAneesh Kumar K.V
694df06b37fSKeith Busch return follow_pud_mask(vma, address, p4d, flags, ctx);
695080dbb61SAneesh Kumar K.V }
696080dbb61SAneesh Kumar K.V
697080dbb61SAneesh Kumar K.V /**
698080dbb61SAneesh Kumar K.V * follow_page_mask - look up a page descriptor from a user-virtual address
699080dbb61SAneesh Kumar K.V * @vma: vm_area_struct mapping @address
700080dbb61SAneesh Kumar K.V * @address: virtual address to look up
701080dbb61SAneesh Kumar K.V * @flags: flags modifying lookup behaviour
70278179556SMike Rapoport * @ctx: contains dev_pagemap for %ZONE_DEVICE memory pinning and a
70378179556SMike Rapoport * pointer to output page_mask
704080dbb61SAneesh Kumar K.V *
705080dbb61SAneesh Kumar K.V * @flags can have FOLL_ flags set, defined in <linux/mm.h>
706080dbb61SAneesh Kumar K.V *
70778179556SMike Rapoport * When getting pages from ZONE_DEVICE memory, the @ctx->pgmap caches
70878179556SMike Rapoport * the device's dev_pagemap metadata to avoid repeating expensive lookups.
70978179556SMike Rapoport *
710a7f22660SDavid Hildenbrand * When getting an anonymous page and the caller has to trigger unsharing
711a7f22660SDavid Hildenbrand * of a shared anonymous page first, -EMLINK is returned. The caller should
712a7f22660SDavid Hildenbrand * trigger a fault with FAULT_FLAG_UNSHARE set. Note that unsharing is only
713a7f22660SDavid Hildenbrand * relevant with FOLL_PIN and !FOLL_WRITE.
714a7f22660SDavid Hildenbrand *
71578179556SMike Rapoport * On output, the @ctx->page_mask is set according to the size of the page.
71678179556SMike Rapoport *
71778179556SMike Rapoport * Return: the mapped (struct page *), %NULL if no mapping exists, or
718080dbb61SAneesh Kumar K.V * an error pointer if there is a mapping to something not represented
719080dbb61SAneesh Kumar K.V * by a page descriptor (see also vm_normal_page()).
720080dbb61SAneesh Kumar K.V */
follow_page_mask(struct vm_area_struct * vma,unsigned long address,unsigned int flags,struct follow_page_context * ctx)721a7030aeaSBharath Vedartham static struct page *follow_page_mask(struct vm_area_struct *vma,
722080dbb61SAneesh Kumar K.V unsigned long address, unsigned int flags,
723df06b37fSKeith Busch struct follow_page_context *ctx)
724080dbb61SAneesh Kumar K.V {
725080dbb61SAneesh Kumar K.V pgd_t *pgd;
726080dbb61SAneesh Kumar K.V struct mm_struct *mm = vma->vm_mm;
727080dbb61SAneesh Kumar K.V
728df06b37fSKeith Busch ctx->page_mask = 0;
729080dbb61SAneesh Kumar K.V
73057a196a5SMike Kravetz /*
73157a196a5SMike Kravetz * Call hugetlb_follow_page_mask for hugetlb vmas as it will use
73257a196a5SMike Kravetz * special hugetlb page table walking code. This eliminates the
73357a196a5SMike Kravetz * need to check for hugetlb entries in the general walking code.
73457a196a5SMike Kravetz */
735dd767aaaSPeter Xu if (is_vm_hugetlb_page(vma))
7365502ea44SPeter Xu return hugetlb_follow_page_mask(vma, address, flags,
7375502ea44SPeter Xu &ctx->page_mask);
738080dbb61SAneesh Kumar K.V
739080dbb61SAneesh Kumar K.V pgd = pgd_offset(mm, address);
740080dbb61SAneesh Kumar K.V
741080dbb61SAneesh Kumar K.V if (pgd_none(*pgd) || unlikely(pgd_bad(*pgd)))
742080dbb61SAneesh Kumar K.V return no_page_table(vma, flags);
743080dbb61SAneesh Kumar K.V
744df06b37fSKeith Busch return follow_p4d_mask(vma, address, pgd, flags, ctx);
745df06b37fSKeith Busch }
746df06b37fSKeith Busch
follow_page(struct vm_area_struct * vma,unsigned long address,unsigned int foll_flags)747df06b37fSKeith Busch struct page *follow_page(struct vm_area_struct *vma, unsigned long address,
748df06b37fSKeith Busch unsigned int foll_flags)
749df06b37fSKeith Busch {
750df06b37fSKeith Busch struct follow_page_context ctx = { NULL };
751df06b37fSKeith Busch struct page *page;
752df06b37fSKeith Busch
7531507f512SMike Rapoport if (vma_is_secretmem(vma))
7541507f512SMike Rapoport return NULL;
7551507f512SMike Rapoport
756d64e2dbcSJason Gunthorpe if (WARN_ON_ONCE(foll_flags & FOLL_PIN))
7578909691bSDavid Hildenbrand return NULL;
7588909691bSDavid Hildenbrand
759d74943a2SDavid Hildenbrand /*
760d74943a2SDavid Hildenbrand * We never set FOLL_HONOR_NUMA_FAULT because callers don't expect
761d74943a2SDavid Hildenbrand * to fail on PROT_NONE-mapped pages.
762d74943a2SDavid Hildenbrand */
763df06b37fSKeith Busch page = follow_page_mask(vma, address, foll_flags, &ctx);
764df06b37fSKeith Busch if (ctx.pgmap)
765df06b37fSKeith Busch put_dev_pagemap(ctx.pgmap);
766df06b37fSKeith Busch return page;
767080dbb61SAneesh Kumar K.V }
768080dbb61SAneesh Kumar K.V
get_gate_page(struct mm_struct * mm,unsigned long address,unsigned int gup_flags,struct vm_area_struct ** vma,struct page ** page)769f2b495caSKirill A. Shutemov static int get_gate_page(struct mm_struct *mm, unsigned long address,
770f2b495caSKirill A. Shutemov unsigned int gup_flags, struct vm_area_struct **vma,
771f2b495caSKirill A. Shutemov struct page **page)
772f2b495caSKirill A. Shutemov {
773f2b495caSKirill A. Shutemov pgd_t *pgd;
774c2febafcSKirill A. Shutemov p4d_t *p4d;
775f2b495caSKirill A. Shutemov pud_t *pud;
776f2b495caSKirill A. Shutemov pmd_t *pmd;
777f2b495caSKirill A. Shutemov pte_t *pte;
778c33c7948SRyan Roberts pte_t entry;
779f2b495caSKirill A. Shutemov int ret = -EFAULT;
780f2b495caSKirill A. Shutemov
781f2b495caSKirill A. Shutemov /* user gate pages are read-only */
782f2b495caSKirill A. Shutemov if (gup_flags & FOLL_WRITE)
783f2b495caSKirill A. Shutemov return -EFAULT;
784f2b495caSKirill A. Shutemov if (address > TASK_SIZE)
785f2b495caSKirill A. Shutemov pgd = pgd_offset_k(address);
786f2b495caSKirill A. Shutemov else
787f2b495caSKirill A. Shutemov pgd = pgd_offset_gate(mm, address);
788b5d1c39fSAndy Lutomirski if (pgd_none(*pgd))
789b5d1c39fSAndy Lutomirski return -EFAULT;
790c2febafcSKirill A. Shutemov p4d = p4d_offset(pgd, address);
791b5d1c39fSAndy Lutomirski if (p4d_none(*p4d))
792b5d1c39fSAndy Lutomirski return -EFAULT;
793c2febafcSKirill A. Shutemov pud = pud_offset(p4d, address);
794b5d1c39fSAndy Lutomirski if (pud_none(*pud))
795b5d1c39fSAndy Lutomirski return -EFAULT;
796f2b495caSKirill A. Shutemov pmd = pmd_offset(pud, address);
79784c3fc4eSZi Yan if (!pmd_present(*pmd))
798f2b495caSKirill A. Shutemov return -EFAULT;
799f2b495caSKirill A. Shutemov pte = pte_offset_map(pmd, address);
80004dee9e8SHugh Dickins if (!pte)
80104dee9e8SHugh Dickins return -EFAULT;
802c33c7948SRyan Roberts entry = ptep_get(pte);
803c33c7948SRyan Roberts if (pte_none(entry))
804f2b495caSKirill A. Shutemov goto unmap;
805f2b495caSKirill A. Shutemov *vma = get_gate_vma(mm);
806f2b495caSKirill A. Shutemov if (!page)
807f2b495caSKirill A. Shutemov goto out;
808c33c7948SRyan Roberts *page = vm_normal_page(*vma, address, entry);
809f2b495caSKirill A. Shutemov if (!*page) {
810c33c7948SRyan Roberts if ((gup_flags & FOLL_DUMP) || !is_zero_pfn(pte_pfn(entry)))
811f2b495caSKirill A. Shutemov goto unmap;
812c33c7948SRyan Roberts *page = pte_page(entry);
813f2b495caSKirill A. Shutemov }
81426273f5fSYang Shi ret = try_grab_folio(page_folio(*page), 1, gup_flags);
8150f089235SLogan Gunthorpe if (unlikely(ret))
8168fde12caSLinus Torvalds goto unmap;
817f2b495caSKirill A. Shutemov out:
818f2b495caSKirill A. Shutemov ret = 0;
819f2b495caSKirill A. Shutemov unmap:
820f2b495caSKirill A. Shutemov pte_unmap(pte);
821f2b495caSKirill A. Shutemov return ret;
822f2b495caSKirill A. Shutemov }
823f2b495caSKirill A. Shutemov
8249a95f3cfSPaul Cassella /*
8259a863a6aSJason Gunthorpe * mmap_lock must be held on entry. If @flags has FOLL_UNLOCKABLE but not
8269a863a6aSJason Gunthorpe * FOLL_NOWAIT, the mmap_lock may be released. If it is, *@locked will be set
8279a863a6aSJason Gunthorpe * to 0 and -EBUSY returned.
8289a95f3cfSPaul Cassella */
faultin_page(struct vm_area_struct * vma,unsigned long address,unsigned int * flags,bool unshare,int * locked)82964019a2eSPeter Xu static int faultin_page(struct vm_area_struct *vma,
830a7f22660SDavid Hildenbrand unsigned long address, unsigned int *flags, bool unshare,
831a7f22660SDavid Hildenbrand int *locked)
83216744483SKirill A. Shutemov {
83316744483SKirill A. Shutemov unsigned int fault_flags = 0;
8342b740303SSouptick Joarder vm_fault_t ret;
83516744483SKirill A. Shutemov
83655b8fe70SAndreas Gruenbacher if (*flags & FOLL_NOFAULT)
83755b8fe70SAndreas Gruenbacher return -EFAULT;
83816744483SKirill A. Shutemov if (*flags & FOLL_WRITE)
83916744483SKirill A. Shutemov fault_flags |= FAULT_FLAG_WRITE;
8401b2ee126SDave Hansen if (*flags & FOLL_REMOTE)
8411b2ee126SDave Hansen fault_flags |= FAULT_FLAG_REMOTE;
842f04740f5SJason Gunthorpe if (*flags & FOLL_UNLOCKABLE) {
84371335f37SPeter Xu fault_flags |= FAULT_FLAG_ALLOW_RETRY | FAULT_FLAG_KILLABLE;
84493c5c61dSPeter Xu /*
84593c5c61dSPeter Xu * FAULT_FLAG_INTERRUPTIBLE is opt-in. GUP callers must set
84693c5c61dSPeter Xu * FOLL_INTERRUPTIBLE to enable FAULT_FLAG_INTERRUPTIBLE.
84793c5c61dSPeter Xu * That's because some callers may not be prepared to
84893c5c61dSPeter Xu * handle early exits caused by non-fatal signals.
84993c5c61dSPeter Xu */
85093c5c61dSPeter Xu if (*flags & FOLL_INTERRUPTIBLE)
85193c5c61dSPeter Xu fault_flags |= FAULT_FLAG_INTERRUPTIBLE;
85293c5c61dSPeter Xu }
85316744483SKirill A. Shutemov if (*flags & FOLL_NOWAIT)
85416744483SKirill A. Shutemov fault_flags |= FAULT_FLAG_ALLOW_RETRY | FAULT_FLAG_RETRY_NOWAIT;
855234b239bSAndres Lagar-Cavilla if (*flags & FOLL_TRIED) {
8564426e945SPeter Xu /*
8574426e945SPeter Xu * Note: FAULT_FLAG_ALLOW_RETRY and FAULT_FLAG_TRIED
8584426e945SPeter Xu * can co-exist
8594426e945SPeter Xu */
860234b239bSAndres Lagar-Cavilla fault_flags |= FAULT_FLAG_TRIED;
861234b239bSAndres Lagar-Cavilla }
862a7f22660SDavid Hildenbrand if (unshare) {
863a7f22660SDavid Hildenbrand fault_flags |= FAULT_FLAG_UNSHARE;
864a7f22660SDavid Hildenbrand /* FAULT_FLAG_WRITE and FAULT_FLAG_UNSHARE are incompatible */
865a7f22660SDavid Hildenbrand VM_BUG_ON(fault_flags & FAULT_FLAG_WRITE);
866a7f22660SDavid Hildenbrand }
86716744483SKirill A. Shutemov
868bce617edSPeter Xu ret = handle_mm_fault(vma, address, fault_flags, NULL);
869d9272525SPeter Xu
870d9272525SPeter Xu if (ret & VM_FAULT_COMPLETED) {
871d9272525SPeter Xu /*
872d9272525SPeter Xu * With FAULT_FLAG_RETRY_NOWAIT we'll never release the
873d9272525SPeter Xu * mmap lock in the page fault handler. Sanity check this.
874d9272525SPeter Xu */
875d9272525SPeter Xu WARN_ON_ONCE(fault_flags & FAULT_FLAG_RETRY_NOWAIT);
876d9272525SPeter Xu *locked = 0;
8779a863a6aSJason Gunthorpe
878d9272525SPeter Xu /*
879d9272525SPeter Xu * We should do the same as VM_FAULT_RETRY, but let's not
880d9272525SPeter Xu * return -EBUSY since that's not reflecting the reality of
881d9272525SPeter Xu * what has happened - we've just fully completed a page
882d9272525SPeter Xu * fault, with the mmap lock released. Use -EAGAIN to show
883d9272525SPeter Xu * that we want to take the mmap lock _again_.
884d9272525SPeter Xu */
885d9272525SPeter Xu return -EAGAIN;
886d9272525SPeter Xu }
887d9272525SPeter Xu
88816744483SKirill A. Shutemov if (ret & VM_FAULT_ERROR) {
8899a291a7cSJames Morse int err = vm_fault_to_errno(ret, *flags);
8909a291a7cSJames Morse
8919a291a7cSJames Morse if (err)
8929a291a7cSJames Morse return err;
89316744483SKirill A. Shutemov BUG();
89416744483SKirill A. Shutemov }
89516744483SKirill A. Shutemov
89616744483SKirill A. Shutemov if (ret & VM_FAULT_RETRY) {
8979a863a6aSJason Gunthorpe if (!(fault_flags & FAULT_FLAG_RETRY_NOWAIT))
8984f6da934SPeter Xu *locked = 0;
89916744483SKirill A. Shutemov return -EBUSY;
90016744483SKirill A. Shutemov }
90116744483SKirill A. Shutemov
90216744483SKirill A. Shutemov return 0;
90316744483SKirill A. Shutemov }
90416744483SKirill A. Shutemov
9058ac26843SLorenzo Stoakes /*
9068ac26843SLorenzo Stoakes * Writing to file-backed mappings which require folio dirty tracking using GUP
9078ac26843SLorenzo Stoakes * is a fundamentally broken operation, as kernel write access to GUP mappings
9088ac26843SLorenzo Stoakes * do not adhere to the semantics expected by a file system.
9098ac26843SLorenzo Stoakes *
9108ac26843SLorenzo Stoakes * Consider the following scenario:-
9118ac26843SLorenzo Stoakes *
9128ac26843SLorenzo Stoakes * 1. A folio is written to via GUP which write-faults the memory, notifying
9138ac26843SLorenzo Stoakes * the file system and dirtying the folio.
9148ac26843SLorenzo Stoakes * 2. Later, writeback is triggered, resulting in the folio being cleaned and
9158ac26843SLorenzo Stoakes * the PTE being marked read-only.
9168ac26843SLorenzo Stoakes * 3. The GUP caller writes to the folio, as it is mapped read/write via the
9178ac26843SLorenzo Stoakes * direct mapping.
9188ac26843SLorenzo Stoakes * 4. The GUP caller, now done with the page, unpins it and sets it dirty
9198ac26843SLorenzo Stoakes * (though it does not have to).
9208ac26843SLorenzo Stoakes *
9218ac26843SLorenzo Stoakes * This results in both data being written to a folio without writenotify, and
9228ac26843SLorenzo Stoakes * the folio being dirtied unexpectedly (if the caller decides to do so).
9238ac26843SLorenzo Stoakes */
writable_file_mapping_allowed(struct vm_area_struct * vma,unsigned long gup_flags)9248ac26843SLorenzo Stoakes static bool writable_file_mapping_allowed(struct vm_area_struct *vma,
9258ac26843SLorenzo Stoakes unsigned long gup_flags)
9268ac26843SLorenzo Stoakes {
9278ac26843SLorenzo Stoakes /*
9288ac26843SLorenzo Stoakes * If we aren't pinning then no problematic write can occur. A long term
9298ac26843SLorenzo Stoakes * pin is the most egregious case so this is the case we disallow.
9308ac26843SLorenzo Stoakes */
9318ac26843SLorenzo Stoakes if ((gup_flags & (FOLL_PIN | FOLL_LONGTERM)) !=
9328ac26843SLorenzo Stoakes (FOLL_PIN | FOLL_LONGTERM))
9338ac26843SLorenzo Stoakes return true;
9348ac26843SLorenzo Stoakes
9358ac26843SLorenzo Stoakes /*
9368ac26843SLorenzo Stoakes * If the VMA does not require dirty tracking then no problematic write
9378ac26843SLorenzo Stoakes * can occur either.
9388ac26843SLorenzo Stoakes */
9398ac26843SLorenzo Stoakes return !vma_needs_dirty_tracking(vma);
9408ac26843SLorenzo Stoakes }
9418ac26843SLorenzo Stoakes
check_vma_flags(struct vm_area_struct * vma,unsigned long gup_flags)942fa5bb209SKirill A. Shutemov static int check_vma_flags(struct vm_area_struct *vma, unsigned long gup_flags)
943fa5bb209SKirill A. Shutemov {
944fa5bb209SKirill A. Shutemov vm_flags_t vm_flags = vma->vm_flags;
9451b2ee126SDave Hansen int write = (gup_flags & FOLL_WRITE);
9461b2ee126SDave Hansen int foreign = (gup_flags & FOLL_REMOTE);
9478ac26843SLorenzo Stoakes bool vma_anon = vma_is_anonymous(vma);
948fa5bb209SKirill A. Shutemov
949fa5bb209SKirill A. Shutemov if (vm_flags & (VM_IO | VM_PFNMAP))
950fa5bb209SKirill A. Shutemov return -EFAULT;
951fa5bb209SKirill A. Shutemov
9528ac26843SLorenzo Stoakes if ((gup_flags & FOLL_ANON) && !vma_anon)
9537f7ccc2cSWilly Tarreau return -EFAULT;
9547f7ccc2cSWilly Tarreau
95552650c8bSJason Gunthorpe if ((gup_flags & FOLL_LONGTERM) && vma_is_fsdax(vma))
95652650c8bSJason Gunthorpe return -EOPNOTSUPP;
95752650c8bSJason Gunthorpe
9581507f512SMike Rapoport if (vma_is_secretmem(vma))
9591507f512SMike Rapoport return -EFAULT;
9601507f512SMike Rapoport
9611b2ee126SDave Hansen if (write) {
9628ac26843SLorenzo Stoakes if (!vma_anon &&
9638ac26843SLorenzo Stoakes !writable_file_mapping_allowed(vma, gup_flags))
9648ac26843SLorenzo Stoakes return -EFAULT;
9658ac26843SLorenzo Stoakes
9666beb9958SRick Edgecombe if (!(vm_flags & VM_WRITE) || (vm_flags & VM_SHADOW_STACK)) {
967fa5bb209SKirill A. Shutemov if (!(gup_flags & FOLL_FORCE))
968fa5bb209SKirill A. Shutemov return -EFAULT;
969f347454dSDavid Hildenbrand /* hugetlb does not support FOLL_FORCE|FOLL_WRITE. */
970f347454dSDavid Hildenbrand if (is_vm_hugetlb_page(vma))
971f347454dSDavid Hildenbrand return -EFAULT;
972fa5bb209SKirill A. Shutemov /*
973fa5bb209SKirill A. Shutemov * We used to let the write,force case do COW in a
974fa5bb209SKirill A. Shutemov * VM_MAYWRITE VM_SHARED !VM_WRITE vma, so ptrace could
975fa5bb209SKirill A. Shutemov * set a breakpoint in a read-only mapping of an
976fa5bb209SKirill A. Shutemov * executable, without corrupting the file (yet only
977fa5bb209SKirill A. Shutemov * when that file had been opened for writing!).
978fa5bb209SKirill A. Shutemov * Anon pages in shared mappings are surprising: now
979fa5bb209SKirill A. Shutemov * just reject it.
980fa5bb209SKirill A. Shutemov */
98146435364SHugh Dickins if (!is_cow_mapping(vm_flags))
982fa5bb209SKirill A. Shutemov return -EFAULT;
983fa5bb209SKirill A. Shutemov }
984fa5bb209SKirill A. Shutemov } else if (!(vm_flags & VM_READ)) {
985fa5bb209SKirill A. Shutemov if (!(gup_flags & FOLL_FORCE))
986fa5bb209SKirill A. Shutemov return -EFAULT;
987fa5bb209SKirill A. Shutemov /*
988fa5bb209SKirill A. Shutemov * Is there actually any vma we can reach here which does not
989fa5bb209SKirill A. Shutemov * have VM_MAYREAD set?
990fa5bb209SKirill A. Shutemov */
991fa5bb209SKirill A. Shutemov if (!(vm_flags & VM_MAYREAD))
992fa5bb209SKirill A. Shutemov return -EFAULT;
993fa5bb209SKirill A. Shutemov }
994d61172b4SDave Hansen /*
995d61172b4SDave Hansen * gups are always data accesses, not instruction
996d61172b4SDave Hansen * fetches, so execute=false here
997d61172b4SDave Hansen */
998d61172b4SDave Hansen if (!arch_vma_access_permitted(vma, write, false, foreign))
99933a709b2SDave Hansen return -EFAULT;
1000fa5bb209SKirill A. Shutemov return 0;
1001fa5bb209SKirill A. Shutemov }
1002fa5bb209SKirill A. Shutemov
10036cd06ab1SLinus Torvalds /*
10046cd06ab1SLinus Torvalds * This is "vma_lookup()", but with a warning if we would have
10056cd06ab1SLinus Torvalds * historically expanded the stack in the GUP code.
10066cd06ab1SLinus Torvalds */
gup_vma_lookup(struct mm_struct * mm,unsigned long addr)10076cd06ab1SLinus Torvalds static struct vm_area_struct *gup_vma_lookup(struct mm_struct *mm,
10086cd06ab1SLinus Torvalds unsigned long addr)
10096cd06ab1SLinus Torvalds {
10106cd06ab1SLinus Torvalds #ifdef CONFIG_STACK_GROWSUP
10116cd06ab1SLinus Torvalds return vma_lookup(mm, addr);
10126cd06ab1SLinus Torvalds #else
10136cd06ab1SLinus Torvalds static volatile unsigned long next_warn;
10146cd06ab1SLinus Torvalds struct vm_area_struct *vma;
10156cd06ab1SLinus Torvalds unsigned long now, next;
10166cd06ab1SLinus Torvalds
10176cd06ab1SLinus Torvalds vma = find_vma(mm, addr);
10186cd06ab1SLinus Torvalds if (!vma || (addr >= vma->vm_start))
10196cd06ab1SLinus Torvalds return vma;
10206cd06ab1SLinus Torvalds
10216cd06ab1SLinus Torvalds /* Only warn for half-way relevant accesses */
10226cd06ab1SLinus Torvalds if (!(vma->vm_flags & VM_GROWSDOWN))
10236cd06ab1SLinus Torvalds return NULL;
10246cd06ab1SLinus Torvalds if (vma->vm_start - addr > 65536)
10256cd06ab1SLinus Torvalds return NULL;
10266cd06ab1SLinus Torvalds
10276cd06ab1SLinus Torvalds /* Let's not warn more than once an hour.. */
10286cd06ab1SLinus Torvalds now = jiffies; next = next_warn;
10296cd06ab1SLinus Torvalds if (next && time_before(now, next))
10306cd06ab1SLinus Torvalds return NULL;
10316cd06ab1SLinus Torvalds next_warn = now + 60*60*HZ;
10326cd06ab1SLinus Torvalds
10336cd06ab1SLinus Torvalds /* Let people know things may have changed. */
10346cd06ab1SLinus Torvalds pr_warn("GUP no longer grows the stack in %s (%d): %lx-%lx (%lx)\n",
10356cd06ab1SLinus Torvalds current->comm, task_pid_nr(current),
10366cd06ab1SLinus Torvalds vma->vm_start, vma->vm_end, addr);
10376cd06ab1SLinus Torvalds dump_stack();
10386cd06ab1SLinus Torvalds return NULL;
10396cd06ab1SLinus Torvalds #endif
10406cd06ab1SLinus Torvalds }
10416cd06ab1SLinus Torvalds
10424bbd4c77SKirill A. Shutemov /**
10434bbd4c77SKirill A. Shutemov * __get_user_pages() - pin user pages in memory
10444bbd4c77SKirill A. Shutemov * @mm: mm_struct of target mm
10454bbd4c77SKirill A. Shutemov * @start: starting user address
10464bbd4c77SKirill A. Shutemov * @nr_pages: number of pages from start to pin
10474bbd4c77SKirill A. Shutemov * @gup_flags: flags modifying pin behaviour
10484bbd4c77SKirill A. Shutemov * @pages: array that receives pointers to the pages pinned.
10494bbd4c77SKirill A. Shutemov * Should be at least nr_pages long. Or NULL, if caller
10504bbd4c77SKirill A. Shutemov * only intends to ensure the pages are faulted in.
1051c1e8d7c6SMichel Lespinasse * @locked: whether we're still with the mmap_lock held
10524bbd4c77SKirill A. Shutemov *
1053d2dfbe47SLiu Xiang * Returns either number of pages pinned (which may be less than the
1054d2dfbe47SLiu Xiang * number requested), or an error. Details about the return value:
1055d2dfbe47SLiu Xiang *
1056d2dfbe47SLiu Xiang * -- If nr_pages is 0, returns 0.
1057d2dfbe47SLiu Xiang * -- If nr_pages is >0, but no pages were pinned, returns -errno.
1058d2dfbe47SLiu Xiang * -- If nr_pages is >0, and some pages were pinned, returns the number of
1059d2dfbe47SLiu Xiang * pages pinned. Again, this may be less than nr_pages.
10602d3a36a4SMichal Hocko * -- 0 return value is possible when the fault would need to be retried.
1061d2dfbe47SLiu Xiang *
1062d2dfbe47SLiu Xiang * The caller is responsible for releasing returned @pages, via put_page().
1063d2dfbe47SLiu Xiang *
1064c1e8d7c6SMichel Lespinasse * Must be called with mmap_lock held. It may be released. See below.
10654bbd4c77SKirill A. Shutemov *
10664bbd4c77SKirill A. Shutemov * __get_user_pages walks a process's page tables and takes a reference to
10674bbd4c77SKirill A. Shutemov * each struct page that each user address corresponds to at a given
10684bbd4c77SKirill A. Shutemov * instant. That is, it takes the page that would be accessed if a user
10694bbd4c77SKirill A. Shutemov * thread accesses the given user virtual address at that instant.
10704bbd4c77SKirill A. Shutemov *
10714bbd4c77SKirill A. Shutemov * This does not guarantee that the page exists in the user mappings when
10724bbd4c77SKirill A. Shutemov * __get_user_pages returns, and there may even be a completely different
10734bbd4c77SKirill A. Shutemov * page there in some cases (eg. if mmapped pagecache has been invalidated
1074c5acf1f6SJongwoo Han * and subsequently re-faulted). However it does guarantee that the page
10754bbd4c77SKirill A. Shutemov * won't be freed completely. And mostly callers simply care that the page
10764bbd4c77SKirill A. Shutemov * contains data that was valid *at some point in time*. Typically, an IO
10774bbd4c77SKirill A. Shutemov * or similar operation cannot guarantee anything stronger anyway because
10784bbd4c77SKirill A. Shutemov * locks can't be held over the syscall boundary.
10794bbd4c77SKirill A. Shutemov *
10804bbd4c77SKirill A. Shutemov * If @gup_flags & FOLL_WRITE == 0, the page must not be written to. If
10814bbd4c77SKirill A. Shutemov * the page is written to, set_page_dirty (or set_page_dirty_lock, as
10824bbd4c77SKirill A. Shutemov * appropriate) must be called after the page is finished with, and
10834bbd4c77SKirill A. Shutemov * before put_page is called.
10844bbd4c77SKirill A. Shutemov *
10859a863a6aSJason Gunthorpe * If FOLL_UNLOCKABLE is set without FOLL_NOWAIT then the mmap_lock may
10869a863a6aSJason Gunthorpe * be released. If this happens *@locked will be set to 0 on return.
10879a95f3cfSPaul Cassella *
10889a863a6aSJason Gunthorpe * A caller using such a combination of @gup_flags must therefore hold the
10899a863a6aSJason Gunthorpe * mmap_lock for reading only, and recognize when it's been released. Otherwise,
10909a863a6aSJason Gunthorpe * it must be held for either reading or writing and will not be released.
10914bbd4c77SKirill A. Shutemov *
10924bbd4c77SKirill A. Shutemov * In most cases, get_user_pages or get_user_pages_fast should be used
10934bbd4c77SKirill A. Shutemov * instead of __get_user_pages. __get_user_pages should be used only if
10944bbd4c77SKirill A. Shutemov * you need some special @gup_flags.
10954bbd4c77SKirill A. Shutemov */
__get_user_pages(struct mm_struct * mm,unsigned long start,unsigned long nr_pages,unsigned int gup_flags,struct page ** pages,int * locked)109664019a2eSPeter Xu static long __get_user_pages(struct mm_struct *mm,
10974bbd4c77SKirill A. Shutemov unsigned long start, unsigned long nr_pages,
10984bbd4c77SKirill A. Shutemov unsigned int gup_flags, struct page **pages,
1099b2cac248SLorenzo Stoakes int *locked)
11004bbd4c77SKirill A. Shutemov {
1101df06b37fSKeith Busch long ret = 0, i = 0;
1102fa5bb209SKirill A. Shutemov struct vm_area_struct *vma = NULL;
1103df06b37fSKeith Busch struct follow_page_context ctx = { NULL };
11044bbd4c77SKirill A. Shutemov
11054bbd4c77SKirill A. Shutemov if (!nr_pages)
11064bbd4c77SKirill A. Shutemov return 0;
11074bbd4c77SKirill A. Shutemov
1108428e106aSKirill A. Shutemov start = untagged_addr_remote(mm, start);
1109f9652594SAndrey Konovalov
1110eddb1c22SJohn Hubbard VM_BUG_ON(!!pages != !!(gup_flags & (FOLL_GET | FOLL_PIN)));
11114bbd4c77SKirill A. Shutemov
11124bbd4c77SKirill A. Shutemov do {
11134bbd4c77SKirill A. Shutemov struct page *page;
11144bbd4c77SKirill A. Shutemov unsigned int foll_flags = gup_flags;
11154bbd4c77SKirill A. Shutemov unsigned int page_increm;
11164bbd4c77SKirill A. Shutemov
1117fa5bb209SKirill A. Shutemov /* first iteration or cross vma bound */
1118fa5bb209SKirill A. Shutemov if (!vma || start >= vma->vm_end) {
11199e898211SDavid Hildenbrand /*
11209e898211SDavid Hildenbrand * MADV_POPULATE_(READ|WRITE) wants to handle VMA
11219e898211SDavid Hildenbrand * lookups+error reporting differently.
11229e898211SDavid Hildenbrand */
11239e898211SDavid Hildenbrand if (gup_flags & FOLL_MADV_POPULATE) {
11249e898211SDavid Hildenbrand vma = vma_lookup(mm, start);
11259e898211SDavid Hildenbrand if (!vma) {
11269e898211SDavid Hildenbrand ret = -ENOMEM;
11279e898211SDavid Hildenbrand goto out;
11289e898211SDavid Hildenbrand }
11299e898211SDavid Hildenbrand if (check_vma_flags(vma, gup_flags)) {
11309e898211SDavid Hildenbrand ret = -EINVAL;
11319e898211SDavid Hildenbrand goto out;
11329e898211SDavid Hildenbrand }
11339e898211SDavid Hildenbrand goto retry;
11349e898211SDavid Hildenbrand }
11356cd06ab1SLinus Torvalds vma = gup_vma_lookup(mm, start);
1136fa5bb209SKirill A. Shutemov if (!vma && in_gate_area(mm, start)) {
1137fa5bb209SKirill A. Shutemov ret = get_gate_page(mm, start & PAGE_MASK,
1138fa5bb209SKirill A. Shutemov gup_flags, &vma,
1139ffe1e786SPeter Xu pages ? &page : NULL);
1140fa5bb209SKirill A. Shutemov if (ret)
114108be37b7SJohn Hubbard goto out;
1142df06b37fSKeith Busch ctx.page_mask = 0;
1143fa5bb209SKirill A. Shutemov goto next_page;
1144fa5bb209SKirill A. Shutemov }
1145fa5bb209SKirill A. Shutemov
114652650c8bSJason Gunthorpe if (!vma) {
1147df06b37fSKeith Busch ret = -EFAULT;
1148df06b37fSKeith Busch goto out;
1149df06b37fSKeith Busch }
115052650c8bSJason Gunthorpe ret = check_vma_flags(vma, gup_flags);
115152650c8bSJason Gunthorpe if (ret)
115252650c8bSJason Gunthorpe goto out;
1153fa5bb209SKirill A. Shutemov }
1154fa5bb209SKirill A. Shutemov retry:
11554bbd4c77SKirill A. Shutemov /*
1156fa5bb209SKirill A. Shutemov * If we have a pending SIGKILL, don't keep faulting pages and
1157fa5bb209SKirill A. Shutemov * potentially allocating memory.
11584bbd4c77SKirill A. Shutemov */
1159fa45f116SDavidlohr Bueso if (fatal_signal_pending(current)) {
1160d180870dSMichal Hocko ret = -EINTR;
1161df06b37fSKeith Busch goto out;
1162df06b37fSKeith Busch }
11634bbd4c77SKirill A. Shutemov cond_resched();
1164df06b37fSKeith Busch
1165df06b37fSKeith Busch page = follow_page_mask(vma, start, foll_flags, &ctx);
1166a7f22660SDavid Hildenbrand if (!page || PTR_ERR(page) == -EMLINK) {
1167a7f22660SDavid Hildenbrand ret = faultin_page(vma, start, &foll_flags,
1168a7f22660SDavid Hildenbrand PTR_ERR(page) == -EMLINK, locked);
116916744483SKirill A. Shutemov switch (ret) {
117016744483SKirill A. Shutemov case 0:
1171fa5bb209SKirill A. Shutemov goto retry;
1172df06b37fSKeith Busch case -EBUSY:
1173d9272525SPeter Xu case -EAGAIN:
1174df06b37fSKeith Busch ret = 0;
1175e4a9bc58SJoe Perches fallthrough;
117616744483SKirill A. Shutemov case -EFAULT:
117716744483SKirill A. Shutemov case -ENOMEM:
117816744483SKirill A. Shutemov case -EHWPOISON:
1179df06b37fSKeith Busch goto out;
11804bbd4c77SKirill A. Shutemov }
1181fa5bb209SKirill A. Shutemov BUG();
11821027e443SKirill A. Shutemov } else if (PTR_ERR(page) == -EEXIST) {
11831027e443SKirill A. Shutemov /*
11841027e443SKirill A. Shutemov * Proper page table entry exists, but no corresponding
118565462462SJohn Hubbard * struct page. If the caller expects **pages to be
118665462462SJohn Hubbard * filled in, bail out now, because that can't be done
118765462462SJohn Hubbard * for this page.
11881027e443SKirill A. Shutemov */
118965462462SJohn Hubbard if (pages) {
119065462462SJohn Hubbard ret = PTR_ERR(page);
119165462462SJohn Hubbard goto out;
119265462462SJohn Hubbard }
11931027e443SKirill A. Shutemov } else if (IS_ERR(page)) {
1194df06b37fSKeith Busch ret = PTR_ERR(page);
1195df06b37fSKeith Busch goto out;
11961027e443SKirill A. Shutemov }
1197ffe1e786SPeter Xu next_page:
1198df06b37fSKeith Busch page_increm = 1 + (~(start >> PAGE_SHIFT) & ctx.page_mask);
11994bbd4c77SKirill A. Shutemov if (page_increm > nr_pages)
12004bbd4c77SKirill A. Shutemov page_increm = nr_pages;
120157edfcfdSPeter Xu
120257edfcfdSPeter Xu if (pages) {
120357edfcfdSPeter Xu struct page *subpage;
120457edfcfdSPeter Xu unsigned int j;
120557edfcfdSPeter Xu
120657edfcfdSPeter Xu /*
120757edfcfdSPeter Xu * This must be a large folio (and doesn't need to
120857edfcfdSPeter Xu * be the whole folio; it can be part of it), do
120957edfcfdSPeter Xu * the refcount work for all the subpages too.
121057edfcfdSPeter Xu *
121157edfcfdSPeter Xu * NOTE: here the page may not be the head page
121257edfcfdSPeter Xu * e.g. when start addr is not thp-size aligned.
121357edfcfdSPeter Xu * try_grab_folio() should have taken care of tail
121457edfcfdSPeter Xu * pages.
121557edfcfdSPeter Xu */
121657edfcfdSPeter Xu if (page_increm > 1) {
121726273f5fSYang Shi struct folio *folio = page_folio(page);
121857edfcfdSPeter Xu
121957edfcfdSPeter Xu /*
122057edfcfdSPeter Xu * Since we already hold refcount on the
122157edfcfdSPeter Xu * large folio, this should never fail.
122257edfcfdSPeter Xu */
122326273f5fSYang Shi if (try_grab_folio(folio, page_increm - 1,
122426273f5fSYang Shi foll_flags)) {
122557edfcfdSPeter Xu /*
122657edfcfdSPeter Xu * Release the 1st page ref if the
122757edfcfdSPeter Xu * folio is problematic, fail hard.
122857edfcfdSPeter Xu */
122926273f5fSYang Shi gup_put_folio(folio, 1,
123057edfcfdSPeter Xu foll_flags);
123157edfcfdSPeter Xu ret = -EFAULT;
123257edfcfdSPeter Xu goto out;
123357edfcfdSPeter Xu }
123457edfcfdSPeter Xu }
123557edfcfdSPeter Xu
123657edfcfdSPeter Xu for (j = 0; j < page_increm; j++) {
123757edfcfdSPeter Xu subpage = nth_page(page, j);
123857edfcfdSPeter Xu pages[i + j] = subpage;
123957edfcfdSPeter Xu flush_anon_page(vma, subpage, start + j * PAGE_SIZE);
124057edfcfdSPeter Xu flush_dcache_page(subpage);
124157edfcfdSPeter Xu }
124257edfcfdSPeter Xu }
124357edfcfdSPeter Xu
12444bbd4c77SKirill A. Shutemov i += page_increm;
12454bbd4c77SKirill A. Shutemov start += page_increm * PAGE_SIZE;
12464bbd4c77SKirill A. Shutemov nr_pages -= page_increm;
12474bbd4c77SKirill A. Shutemov } while (nr_pages);
1248df06b37fSKeith Busch out:
1249df06b37fSKeith Busch if (ctx.pgmap)
1250df06b37fSKeith Busch put_dev_pagemap(ctx.pgmap);
1251df06b37fSKeith Busch return i ? i : ret;
12524bbd4c77SKirill A. Shutemov }
12534bbd4c77SKirill A. Shutemov
vma_permits_fault(struct vm_area_struct * vma,unsigned int fault_flags)1254771ab430STobias Klauser static bool vma_permits_fault(struct vm_area_struct *vma,
1255771ab430STobias Klauser unsigned int fault_flags)
1256d4925e00SDave Hansen {
125733a709b2SDave Hansen bool write = !!(fault_flags & FAULT_FLAG_WRITE);
12581b2ee126SDave Hansen bool foreign = !!(fault_flags & FAULT_FLAG_REMOTE);
125933a709b2SDave Hansen vm_flags_t vm_flags = write ? VM_WRITE : VM_READ;
1260d4925e00SDave Hansen
1261d4925e00SDave Hansen if (!(vm_flags & vma->vm_flags))
1262d4925e00SDave Hansen return false;
1263d4925e00SDave Hansen
126433a709b2SDave Hansen /*
126533a709b2SDave Hansen * The architecture might have a hardware protection
12661b2ee126SDave Hansen * mechanism other than read/write that can deny access.
1267d61172b4SDave Hansen *
1268d61172b4SDave Hansen * gup always represents data access, not instruction
1269d61172b4SDave Hansen * fetches, so execute=false here:
127033a709b2SDave Hansen */
1271d61172b4SDave Hansen if (!arch_vma_access_permitted(vma, write, false, foreign))
127233a709b2SDave Hansen return false;
127333a709b2SDave Hansen
1274d4925e00SDave Hansen return true;
1275d4925e00SDave Hansen }
1276d4925e00SDave Hansen
1277adc8cb40SSouptick Joarder /**
12784bbd4c77SKirill A. Shutemov * fixup_user_fault() - manually resolve a user page fault
12794bbd4c77SKirill A. Shutemov * @mm: mm_struct of target mm
12804bbd4c77SKirill A. Shutemov * @address: user address
12814bbd4c77SKirill A. Shutemov * @fault_flags:flags to pass down to handle_mm_fault()
1282c1e8d7c6SMichel Lespinasse * @unlocked: did we unlock the mmap_lock while retrying, maybe NULL if caller
1283548b6a1eSMiles Chen * does not allow retry. If NULL, the caller must guarantee
1284548b6a1eSMiles Chen * that fault_flags does not contain FAULT_FLAG_ALLOW_RETRY.
12854bbd4c77SKirill A. Shutemov *
12864bbd4c77SKirill A. Shutemov * This is meant to be called in the specific scenario where for locking reasons
12874bbd4c77SKirill A. Shutemov * we try to access user memory in atomic context (within a pagefault_disable()
12884bbd4c77SKirill A. Shutemov * section), this returns -EFAULT, and we want to resolve the user fault before
12894bbd4c77SKirill A. Shutemov * trying again.
12904bbd4c77SKirill A. Shutemov *
12914bbd4c77SKirill A. Shutemov * Typically this is meant to be used by the futex code.
12924bbd4c77SKirill A. Shutemov *
12934bbd4c77SKirill A. Shutemov * The main difference with get_user_pages() is that this function will
12944bbd4c77SKirill A. Shutemov * unconditionally call handle_mm_fault() which will in turn perform all the
12954bbd4c77SKirill A. Shutemov * necessary SW fixup of the dirty and young bits in the PTE, while
12964a9e1cdaSDominik Dingel * get_user_pages() only guarantees to update these in the struct page.
12974bbd4c77SKirill A. Shutemov *
12984bbd4c77SKirill A. Shutemov * This is important for some architectures where those bits also gate the
12994bbd4c77SKirill A. Shutemov * access permission to the page because they are maintained in software. On
13004bbd4c77SKirill A. Shutemov * such architectures, gup() will not be enough to make a subsequent access
13014bbd4c77SKirill A. Shutemov * succeed.
13024bbd4c77SKirill A. Shutemov *
1303c1e8d7c6SMichel Lespinasse * This function will not return with an unlocked mmap_lock. So it has not the
1304c1e8d7c6SMichel Lespinasse * same semantics wrt the @mm->mmap_lock as does filemap_fault().
13054bbd4c77SKirill A. Shutemov */
fixup_user_fault(struct mm_struct * mm,unsigned long address,unsigned int fault_flags,bool * unlocked)130664019a2eSPeter Xu int fixup_user_fault(struct mm_struct *mm,
13074a9e1cdaSDominik Dingel unsigned long address, unsigned int fault_flags,
13084a9e1cdaSDominik Dingel bool *unlocked)
13094bbd4c77SKirill A. Shutemov {
13104bbd4c77SKirill A. Shutemov struct vm_area_struct *vma;
13118fed2f3cSMiaohe Lin vm_fault_t ret;
13124bbd4c77SKirill A. Shutemov
1313428e106aSKirill A. Shutemov address = untagged_addr_remote(mm, address);
1314f9652594SAndrey Konovalov
13154a9e1cdaSDominik Dingel if (unlocked)
131671335f37SPeter Xu fault_flags |= FAULT_FLAG_ALLOW_RETRY | FAULT_FLAG_KILLABLE;
13174a9e1cdaSDominik Dingel
13184a9e1cdaSDominik Dingel retry:
13196cd06ab1SLinus Torvalds vma = gup_vma_lookup(mm, address);
13208d7071afSLinus Torvalds if (!vma)
13214bbd4c77SKirill A. Shutemov return -EFAULT;
13224bbd4c77SKirill A. Shutemov
1323d4925e00SDave Hansen if (!vma_permits_fault(vma, fault_flags))
13244bbd4c77SKirill A. Shutemov return -EFAULT;
13254bbd4c77SKirill A. Shutemov
1326475f4dfcSPeter Xu if ((fault_flags & FAULT_FLAG_KILLABLE) &&
1327475f4dfcSPeter Xu fatal_signal_pending(current))
1328475f4dfcSPeter Xu return -EINTR;
1329475f4dfcSPeter Xu
1330bce617edSPeter Xu ret = handle_mm_fault(vma, address, fault_flags, NULL);
1331d9272525SPeter Xu
1332d9272525SPeter Xu if (ret & VM_FAULT_COMPLETED) {
1333d9272525SPeter Xu /*
1334d9272525SPeter Xu * NOTE: it's a pity that we need to retake the lock here
1335d9272525SPeter Xu * to pair with the unlock() in the callers. Ideally we
1336d9272525SPeter Xu * could tell the callers so they do not need to unlock.
1337d9272525SPeter Xu */
1338d9272525SPeter Xu mmap_read_lock(mm);
1339d9272525SPeter Xu *unlocked = true;
1340d9272525SPeter Xu return 0;
1341d9272525SPeter Xu }
1342d9272525SPeter Xu
13434bbd4c77SKirill A. Shutemov if (ret & VM_FAULT_ERROR) {
13449a291a7cSJames Morse int err = vm_fault_to_errno(ret, 0);
13459a291a7cSJames Morse
13469a291a7cSJames Morse if (err)
13479a291a7cSJames Morse return err;
13484bbd4c77SKirill A. Shutemov BUG();
13494bbd4c77SKirill A. Shutemov }
13504a9e1cdaSDominik Dingel
13514a9e1cdaSDominik Dingel if (ret & VM_FAULT_RETRY) {
1352d8ed45c5SMichel Lespinasse mmap_read_lock(mm);
13534a9e1cdaSDominik Dingel *unlocked = true;
13544a9e1cdaSDominik Dingel fault_flags |= FAULT_FLAG_TRIED;
13554a9e1cdaSDominik Dingel goto retry;
13564a9e1cdaSDominik Dingel }
13574a9e1cdaSDominik Dingel
13584bbd4c77SKirill A. Shutemov return 0;
13594bbd4c77SKirill A. Shutemov }
1360add6a0cdSPaolo Bonzini EXPORT_SYMBOL_GPL(fixup_user_fault);
13614bbd4c77SKirill A. Shutemov
13622d3a36a4SMichal Hocko /*
136393c5c61dSPeter Xu * GUP always responds to fatal signals. When FOLL_INTERRUPTIBLE is
136493c5c61dSPeter Xu * specified, it'll also respond to generic signals. The caller of GUP
136593c5c61dSPeter Xu * that has FOLL_INTERRUPTIBLE should take care of the GUP interruption.
136693c5c61dSPeter Xu */
gup_signal_pending(unsigned int flags)136793c5c61dSPeter Xu static bool gup_signal_pending(unsigned int flags)
136893c5c61dSPeter Xu {
136993c5c61dSPeter Xu if (fatal_signal_pending(current))
137093c5c61dSPeter Xu return true;
137193c5c61dSPeter Xu
137293c5c61dSPeter Xu if (!(flags & FOLL_INTERRUPTIBLE))
137393c5c61dSPeter Xu return false;
137493c5c61dSPeter Xu
137593c5c61dSPeter Xu return signal_pending(current);
137693c5c61dSPeter Xu }
137793c5c61dSPeter Xu
137893c5c61dSPeter Xu /*
1379b2a72dffSJason Gunthorpe * Locking: (*locked == 1) means that the mmap_lock has already been acquired by
1380b2a72dffSJason Gunthorpe * the caller. This function may drop the mmap_lock. If it does so, then it will
1381b2a72dffSJason Gunthorpe * set (*locked = 0).
1382b2a72dffSJason Gunthorpe *
1383b2a72dffSJason Gunthorpe * (*locked == 0) means that the caller expects this function to acquire and
1384b2a72dffSJason Gunthorpe * drop the mmap_lock. Therefore, the value of *locked will still be zero when
1385b2a72dffSJason Gunthorpe * the function returns, even though it may have changed temporarily during
1386b2a72dffSJason Gunthorpe * function execution.
1387b2a72dffSJason Gunthorpe *
1388b2a72dffSJason Gunthorpe * Please note that this function, unlike __get_user_pages(), will not return 0
1389b2a72dffSJason Gunthorpe * for nr_pages > 0, unless FOLL_NOWAIT is used.
13902d3a36a4SMichal Hocko */
__get_user_pages_locked(struct mm_struct * mm,unsigned long start,unsigned long nr_pages,struct page ** pages,int * locked,unsigned int flags)139164019a2eSPeter Xu static __always_inline long __get_user_pages_locked(struct mm_struct *mm,
1392f0818f47SAndrea Arcangeli unsigned long start,
1393f0818f47SAndrea Arcangeli unsigned long nr_pages,
1394f0818f47SAndrea Arcangeli struct page **pages,
1395e716712fSAl Viro int *locked,
13960fd71a56SAndrea Arcangeli unsigned int flags)
1397f0818f47SAndrea Arcangeli {
1398f0818f47SAndrea Arcangeli long ret, pages_done;
1399b2a72dffSJason Gunthorpe bool must_unlock = false;
1400f0818f47SAndrea Arcangeli
1401b2a72dffSJason Gunthorpe /*
1402b2a72dffSJason Gunthorpe * The internal caller expects GUP to manage the lock internally and the
1403b2a72dffSJason Gunthorpe * lock must be released when this returns.
1404b2a72dffSJason Gunthorpe */
14059a863a6aSJason Gunthorpe if (!*locked) {
1406b2a72dffSJason Gunthorpe if (mmap_read_lock_killable(mm))
1407b2a72dffSJason Gunthorpe return -EAGAIN;
1408b2a72dffSJason Gunthorpe must_unlock = true;
1409b2a72dffSJason Gunthorpe *locked = 1;
1410f0818f47SAndrea Arcangeli }
1411961ba472SJason Gunthorpe else
1412961ba472SJason Gunthorpe mmap_assert_locked(mm);
1413f0818f47SAndrea Arcangeli
1414a458b76aSAndrea Arcangeli if (flags & FOLL_PIN)
1415a458b76aSAndrea Arcangeli mm_set_has_pinned_flag(&mm->flags);
1416008cfe44SPeter Xu
1417eddb1c22SJohn Hubbard /*
1418eddb1c22SJohn Hubbard * FOLL_PIN and FOLL_GET are mutually exclusive. Traditional behavior
1419eddb1c22SJohn Hubbard * is to set FOLL_GET if the caller wants pages[] filled in (but has
1420eddb1c22SJohn Hubbard * carelessly failed to specify FOLL_GET), so keep doing that, but only
1421eddb1c22SJohn Hubbard * for FOLL_GET, not for the newer FOLL_PIN.
1422eddb1c22SJohn Hubbard *
1423eddb1c22SJohn Hubbard * FOLL_PIN always expects pages to be non-null, but no need to assert
1424eddb1c22SJohn Hubbard * that here, as any failures will be obvious enough.
1425eddb1c22SJohn Hubbard */
1426eddb1c22SJohn Hubbard if (pages && !(flags & FOLL_PIN))
1427f0818f47SAndrea Arcangeli flags |= FOLL_GET;
1428f0818f47SAndrea Arcangeli
1429f0818f47SAndrea Arcangeli pages_done = 0;
1430f0818f47SAndrea Arcangeli for (;;) {
143164019a2eSPeter Xu ret = __get_user_pages(mm, start, nr_pages, flags, pages,
1432b2cac248SLorenzo Stoakes locked);
1433f04740f5SJason Gunthorpe if (!(flags & FOLL_UNLOCKABLE)) {
1434f0818f47SAndrea Arcangeli /* VM_FAULT_RETRY couldn't trigger, bypass */
1435f04740f5SJason Gunthorpe pages_done = ret;
1436f04740f5SJason Gunthorpe break;
1437f04740f5SJason Gunthorpe }
1438f0818f47SAndrea Arcangeli
1439d9272525SPeter Xu /* VM_FAULT_RETRY or VM_FAULT_COMPLETED cannot return errors */
1440f0818f47SAndrea Arcangeli if (!*locked) {
1441f0818f47SAndrea Arcangeli BUG_ON(ret < 0);
1442f0818f47SAndrea Arcangeli BUG_ON(ret >= nr_pages);
1443f0818f47SAndrea Arcangeli }
1444f0818f47SAndrea Arcangeli
1445f0818f47SAndrea Arcangeli if (ret > 0) {
1446f0818f47SAndrea Arcangeli nr_pages -= ret;
1447f0818f47SAndrea Arcangeli pages_done += ret;
1448f0818f47SAndrea Arcangeli if (!nr_pages)
1449f0818f47SAndrea Arcangeli break;
1450f0818f47SAndrea Arcangeli }
1451f0818f47SAndrea Arcangeli if (*locked) {
145296312e61SAndrea Arcangeli /*
145396312e61SAndrea Arcangeli * VM_FAULT_RETRY didn't trigger or it was a
145496312e61SAndrea Arcangeli * FOLL_NOWAIT.
145596312e61SAndrea Arcangeli */
1456f0818f47SAndrea Arcangeli if (!pages_done)
1457f0818f47SAndrea Arcangeli pages_done = ret;
1458f0818f47SAndrea Arcangeli break;
1459f0818f47SAndrea Arcangeli }
1460df17277bSMike Rapoport /*
1461df17277bSMike Rapoport * VM_FAULT_RETRY triggered, so seek to the faulting offset.
1462df17277bSMike Rapoport * For the prefault case (!pages) we only update counts.
1463df17277bSMike Rapoport */
1464df17277bSMike Rapoport if (likely(pages))
1465f0818f47SAndrea Arcangeli pages += ret;
1466f0818f47SAndrea Arcangeli start += ret << PAGE_SHIFT;
1467b2a72dffSJason Gunthorpe
1468b2a72dffSJason Gunthorpe /* The lock was temporarily dropped, so we must unlock later */
1469b2a72dffSJason Gunthorpe must_unlock = true;
1470f0818f47SAndrea Arcangeli
14714426e945SPeter Xu retry:
1472f0818f47SAndrea Arcangeli /*
1473f0818f47SAndrea Arcangeli * Repeat on the address that fired VM_FAULT_RETRY
14744426e945SPeter Xu * with both FAULT_FLAG_ALLOW_RETRY and
14754426e945SPeter Xu * FAULT_FLAG_TRIED. Note that GUP can be interrupted
147693c5c61dSPeter Xu * by fatal signals of even common signals, depending on
147793c5c61dSPeter Xu * the caller's request. So we need to check it before we
14784426e945SPeter Xu * start trying again otherwise it can loop forever.
1479f0818f47SAndrea Arcangeli */
148093c5c61dSPeter Xu if (gup_signal_pending(flags)) {
1481ae46d2aaSHillf Danton if (!pages_done)
1482ae46d2aaSHillf Danton pages_done = -EINTR;
14834426e945SPeter Xu break;
1484ae46d2aaSHillf Danton }
14854426e945SPeter Xu
1486d8ed45c5SMichel Lespinasse ret = mmap_read_lock_killable(mm);
148771335f37SPeter Xu if (ret) {
148871335f37SPeter Xu BUG_ON(ret > 0);
148971335f37SPeter Xu if (!pages_done)
149071335f37SPeter Xu pages_done = ret;
149171335f37SPeter Xu break;
149271335f37SPeter Xu }
14934426e945SPeter Xu
1494c7b6a566SPeter Xu *locked = 1;
149564019a2eSPeter Xu ret = __get_user_pages(mm, start, 1, flags | FOLL_TRIED,
1496b2cac248SLorenzo Stoakes pages, locked);
14974426e945SPeter Xu if (!*locked) {
14984426e945SPeter Xu /* Continue to retry until we succeeded */
14994426e945SPeter Xu BUG_ON(ret != 0);
15004426e945SPeter Xu goto retry;
15014426e945SPeter Xu }
1502f0818f47SAndrea Arcangeli if (ret != 1) {
1503f0818f47SAndrea Arcangeli BUG_ON(ret > 1);
1504f0818f47SAndrea Arcangeli if (!pages_done)
1505f0818f47SAndrea Arcangeli pages_done = ret;
1506f0818f47SAndrea Arcangeli break;
1507f0818f47SAndrea Arcangeli }
1508f0818f47SAndrea Arcangeli nr_pages--;
1509f0818f47SAndrea Arcangeli pages_done++;
1510f0818f47SAndrea Arcangeli if (!nr_pages)
1511f0818f47SAndrea Arcangeli break;
1512df17277bSMike Rapoport if (likely(pages))
1513f0818f47SAndrea Arcangeli pages++;
1514f0818f47SAndrea Arcangeli start += PAGE_SIZE;
1515f0818f47SAndrea Arcangeli }
1516b2a72dffSJason Gunthorpe if (must_unlock && *locked) {
1517f0818f47SAndrea Arcangeli /*
1518b2a72dffSJason Gunthorpe * We either temporarily dropped the lock, or the caller
1519b2a72dffSJason Gunthorpe * requested that we both acquire and drop the lock. Either way,
1520b2a72dffSJason Gunthorpe * we must now unlock, and notify the caller of that state.
1521f0818f47SAndrea Arcangeli */
1522d8ed45c5SMichel Lespinasse mmap_read_unlock(mm);
1523f0818f47SAndrea Arcangeli *locked = 0;
1524f0818f47SAndrea Arcangeli }
1525f0818f47SAndrea Arcangeli return pages_done;
1526f0818f47SAndrea Arcangeli }
1527f0818f47SAndrea Arcangeli
1528d3649f68SChristoph Hellwig /**
1529d3649f68SChristoph Hellwig * populate_vma_page_range() - populate a range of pages in the vma.
1530d3649f68SChristoph Hellwig * @vma: target vma
1531d3649f68SChristoph Hellwig * @start: start address
1532d3649f68SChristoph Hellwig * @end: end address
1533c1e8d7c6SMichel Lespinasse * @locked: whether the mmap_lock is still held
1534d3649f68SChristoph Hellwig *
1535d3649f68SChristoph Hellwig * This takes care of mlocking the pages too if VM_LOCKED is set.
1536d3649f68SChristoph Hellwig *
15370a36f7f8STang Yizhou * Return either number of pages pinned in the vma, or a negative error
15380a36f7f8STang Yizhou * code on error.
1539d3649f68SChristoph Hellwig *
1540c1e8d7c6SMichel Lespinasse * vma->vm_mm->mmap_lock must be held.
1541d3649f68SChristoph Hellwig *
15424f6da934SPeter Xu * If @locked is NULL, it may be held for read or write and will
1543d3649f68SChristoph Hellwig * be unperturbed.
1544d3649f68SChristoph Hellwig *
15454f6da934SPeter Xu * If @locked is non-NULL, it must held for read only and may be
15464f6da934SPeter Xu * released. If it's released, *@locked will be set to 0.
1547d3649f68SChristoph Hellwig */
populate_vma_page_range(struct vm_area_struct * vma,unsigned long start,unsigned long end,int * locked)1548d3649f68SChristoph Hellwig long populate_vma_page_range(struct vm_area_struct *vma,
15494f6da934SPeter Xu unsigned long start, unsigned long end, int *locked)
1550d3649f68SChristoph Hellwig {
1551d3649f68SChristoph Hellwig struct mm_struct *mm = vma->vm_mm;
1552d3649f68SChristoph Hellwig unsigned long nr_pages = (end - start) / PAGE_SIZE;
15539a863a6aSJason Gunthorpe int local_locked = 1;
1554d3649f68SChristoph Hellwig int gup_flags;
1555ece369c7SHugh Dickins long ret;
1556d3649f68SChristoph Hellwig
1557be51eb18SMiaohe Lin VM_BUG_ON(!PAGE_ALIGNED(start));
1558be51eb18SMiaohe Lin VM_BUG_ON(!PAGE_ALIGNED(end));
1559d3649f68SChristoph Hellwig VM_BUG_ON_VMA(start < vma->vm_start, vma);
1560d3649f68SChristoph Hellwig VM_BUG_ON_VMA(end > vma->vm_end, vma);
156142fc5414SMichel Lespinasse mmap_assert_locked(mm);
1562d3649f68SChristoph Hellwig
1563b67bf49cSHugh Dickins /*
1564b67bf49cSHugh Dickins * Rightly or wrongly, the VM_LOCKONFAULT case has never used
1565b67bf49cSHugh Dickins * faultin_page() to break COW, so it has no work to do here.
1566b67bf49cSHugh Dickins */
1567d3649f68SChristoph Hellwig if (vma->vm_flags & VM_LOCKONFAULT)
1568b67bf49cSHugh Dickins return nr_pages;
1569b67bf49cSHugh Dickins
1570b67bf49cSHugh Dickins gup_flags = FOLL_TOUCH;
1571d3649f68SChristoph Hellwig /*
1572d3649f68SChristoph Hellwig * We want to touch writable mappings with a write fault in order
1573d3649f68SChristoph Hellwig * to break COW, except for shared mappings because these don't COW
1574d3649f68SChristoph Hellwig * and we would not want to dirty them for nothing.
1575d3649f68SChristoph Hellwig */
1576d3649f68SChristoph Hellwig if ((vma->vm_flags & (VM_WRITE | VM_SHARED)) == VM_WRITE)
1577d3649f68SChristoph Hellwig gup_flags |= FOLL_WRITE;
1578d3649f68SChristoph Hellwig
1579d3649f68SChristoph Hellwig /*
1580d3649f68SChristoph Hellwig * We want mlock to succeed for regions that have any permissions
1581d3649f68SChristoph Hellwig * other than PROT_NONE.
1582d3649f68SChristoph Hellwig */
15833122e80eSAnshuman Khandual if (vma_is_accessible(vma))
1584d3649f68SChristoph Hellwig gup_flags |= FOLL_FORCE;
1585d3649f68SChristoph Hellwig
1586f04740f5SJason Gunthorpe if (locked)
1587f04740f5SJason Gunthorpe gup_flags |= FOLL_UNLOCKABLE;
1588f04740f5SJason Gunthorpe
1589d3649f68SChristoph Hellwig /*
1590d3649f68SChristoph Hellwig * We made sure addr is within a VMA, so the following will
1591d3649f68SChristoph Hellwig * not result in a stack expansion that recurses back here.
1592d3649f68SChristoph Hellwig */
1593ece369c7SHugh Dickins ret = __get_user_pages(mm, start, nr_pages, gup_flags,
1594b2cac248SLorenzo Stoakes NULL, locked ? locked : &local_locked);
1595ece369c7SHugh Dickins lru_add_drain();
1596ece369c7SHugh Dickins return ret;
1597d3649f68SChristoph Hellwig }
1598d3649f68SChristoph Hellwig
1599d3649f68SChristoph Hellwig /*
16009e898211SDavid Hildenbrand * faultin_page_range() - populate (prefault) page tables inside the
16019e898211SDavid Hildenbrand * given range readable/writable
16024ca9b385SDavid Hildenbrand *
16034ca9b385SDavid Hildenbrand * This takes care of mlocking the pages, too, if VM_LOCKED is set.
16044ca9b385SDavid Hildenbrand *
16059e898211SDavid Hildenbrand * @mm: the mm to populate page tables in
16064ca9b385SDavid Hildenbrand * @start: start address
16074ca9b385SDavid Hildenbrand * @end: end address
16084ca9b385SDavid Hildenbrand * @write: whether to prefault readable or writable
16094ca9b385SDavid Hildenbrand * @locked: whether the mmap_lock is still held
16104ca9b385SDavid Hildenbrand *
16119e898211SDavid Hildenbrand * Returns either number of processed pages in the MM, or a negative error
16129e898211SDavid Hildenbrand * code on error (see __get_user_pages()). Note that this function reports
16139e898211SDavid Hildenbrand * errors related to VMAs, such as incompatible mappings, as expected by
16149e898211SDavid Hildenbrand * MADV_POPULATE_(READ|WRITE).
16154ca9b385SDavid Hildenbrand *
16169e898211SDavid Hildenbrand * The range must be page-aligned.
16179e898211SDavid Hildenbrand *
16189e898211SDavid Hildenbrand * mm->mmap_lock must be held. If it's released, *@locked will be set to 0.
16194ca9b385SDavid Hildenbrand */
faultin_page_range(struct mm_struct * mm,unsigned long start,unsigned long end,bool write,int * locked)16209e898211SDavid Hildenbrand long faultin_page_range(struct mm_struct *mm, unsigned long start,
16214ca9b385SDavid Hildenbrand unsigned long end, bool write, int *locked)
16224ca9b385SDavid Hildenbrand {
16234ca9b385SDavid Hildenbrand unsigned long nr_pages = (end - start) / PAGE_SIZE;
16244ca9b385SDavid Hildenbrand int gup_flags;
1625ece369c7SHugh Dickins long ret;
16264ca9b385SDavid Hildenbrand
16274ca9b385SDavid Hildenbrand VM_BUG_ON(!PAGE_ALIGNED(start));
16284ca9b385SDavid Hildenbrand VM_BUG_ON(!PAGE_ALIGNED(end));
16294ca9b385SDavid Hildenbrand mmap_assert_locked(mm);
16304ca9b385SDavid Hildenbrand
16314ca9b385SDavid Hildenbrand /*
16324ca9b385SDavid Hildenbrand * FOLL_TOUCH: Mark page accessed and thereby young; will also mark
16334ca9b385SDavid Hildenbrand * the page dirty with FOLL_WRITE -- which doesn't make a
16344ca9b385SDavid Hildenbrand * difference with !FOLL_FORCE, because the page is writable
16354ca9b385SDavid Hildenbrand * in the page table.
16364ca9b385SDavid Hildenbrand * FOLL_HWPOISON: Return -EHWPOISON instead of -EFAULT when we hit
16374ca9b385SDavid Hildenbrand * a poisoned page.
16384ca9b385SDavid Hildenbrand * !FOLL_FORCE: Require proper access permissions.
16394ca9b385SDavid Hildenbrand */
16409e898211SDavid Hildenbrand gup_flags = FOLL_TOUCH | FOLL_HWPOISON | FOLL_UNLOCKABLE |
16419e898211SDavid Hildenbrand FOLL_MADV_POPULATE;
16424ca9b385SDavid Hildenbrand if (write)
16434ca9b385SDavid Hildenbrand gup_flags |= FOLL_WRITE;
16444ca9b385SDavid Hildenbrand
16459e898211SDavid Hildenbrand ret = __get_user_pages_locked(mm, start, nr_pages, NULL, locked,
16469e898211SDavid Hildenbrand gup_flags);
1647ece369c7SHugh Dickins lru_add_drain();
1648ece369c7SHugh Dickins return ret;
16494ca9b385SDavid Hildenbrand }
16504ca9b385SDavid Hildenbrand
16514ca9b385SDavid Hildenbrand /*
1652d3649f68SChristoph Hellwig * __mm_populate - populate and/or mlock pages within a range of address space.
1653d3649f68SChristoph Hellwig *
1654d3649f68SChristoph Hellwig * This is used to implement mlock() and the MAP_POPULATE / MAP_LOCKED mmap
1655d3649f68SChristoph Hellwig * flags. VMAs must be already marked with the desired vm_flags, and
1656c1e8d7c6SMichel Lespinasse * mmap_lock must not be held.
1657d3649f68SChristoph Hellwig */
__mm_populate(unsigned long start,unsigned long len,int ignore_errors)1658d3649f68SChristoph Hellwig int __mm_populate(unsigned long start, unsigned long len, int ignore_errors)
1659d3649f68SChristoph Hellwig {
1660d3649f68SChristoph Hellwig struct mm_struct *mm = current->mm;
1661d3649f68SChristoph Hellwig unsigned long end, nstart, nend;
1662d3649f68SChristoph Hellwig struct vm_area_struct *vma = NULL;
1663d3649f68SChristoph Hellwig int locked = 0;
1664d3649f68SChristoph Hellwig long ret = 0;
1665d3649f68SChristoph Hellwig
1666d3649f68SChristoph Hellwig end = start + len;
1667d3649f68SChristoph Hellwig
1668d3649f68SChristoph Hellwig for (nstart = start; nstart < end; nstart = nend) {
1669d3649f68SChristoph Hellwig /*
1670d3649f68SChristoph Hellwig * We want to fault in pages for [nstart; end) address range.
1671d3649f68SChristoph Hellwig * Find first corresponding VMA.
1672d3649f68SChristoph Hellwig */
1673d3649f68SChristoph Hellwig if (!locked) {
1674d3649f68SChristoph Hellwig locked = 1;
1675d8ed45c5SMichel Lespinasse mmap_read_lock(mm);
1676c4d1a92dSLiam R. Howlett vma = find_vma_intersection(mm, nstart, end);
1677d3649f68SChristoph Hellwig } else if (nstart >= vma->vm_end)
1678c4d1a92dSLiam R. Howlett vma = find_vma_intersection(mm, vma->vm_end, end);
1679c4d1a92dSLiam R. Howlett
1680c4d1a92dSLiam R. Howlett if (!vma)
1681d3649f68SChristoph Hellwig break;
1682d3649f68SChristoph Hellwig /*
1683d3649f68SChristoph Hellwig * Set [nstart; nend) to intersection of desired address
1684d3649f68SChristoph Hellwig * range with the first VMA. Also, skip undesirable VMA types.
1685d3649f68SChristoph Hellwig */
1686d3649f68SChristoph Hellwig nend = min(end, vma->vm_end);
1687d3649f68SChristoph Hellwig if (vma->vm_flags & (VM_IO | VM_PFNMAP))
1688d3649f68SChristoph Hellwig continue;
1689d3649f68SChristoph Hellwig if (nstart < vma->vm_start)
1690d3649f68SChristoph Hellwig nstart = vma->vm_start;
1691d3649f68SChristoph Hellwig /*
1692d3649f68SChristoph Hellwig * Now fault in a range of pages. populate_vma_page_range()
1693d3649f68SChristoph Hellwig * double checks the vma flags, so that it won't mlock pages
1694d3649f68SChristoph Hellwig * if the vma was already munlocked.
1695d3649f68SChristoph Hellwig */
1696d3649f68SChristoph Hellwig ret = populate_vma_page_range(vma, nstart, nend, &locked);
1697d3649f68SChristoph Hellwig if (ret < 0) {
1698d3649f68SChristoph Hellwig if (ignore_errors) {
1699d3649f68SChristoph Hellwig ret = 0;
1700d3649f68SChristoph Hellwig continue; /* continue at next VMA */
1701d3649f68SChristoph Hellwig }
1702d3649f68SChristoph Hellwig break;
1703d3649f68SChristoph Hellwig }
1704d3649f68SChristoph Hellwig nend = nstart + ret * PAGE_SIZE;
1705d3649f68SChristoph Hellwig ret = 0;
1706d3649f68SChristoph Hellwig }
1707d3649f68SChristoph Hellwig if (locked)
1708d8ed45c5SMichel Lespinasse mmap_read_unlock(mm);
1709d3649f68SChristoph Hellwig return ret; /* 0 or negative error code */
1710d3649f68SChristoph Hellwig }
1711050a9adcSChristoph Hellwig #else /* CONFIG_MMU */
__get_user_pages_locked(struct mm_struct * mm,unsigned long start,unsigned long nr_pages,struct page ** pages,int * locked,unsigned int foll_flags)171264019a2eSPeter Xu static long __get_user_pages_locked(struct mm_struct *mm, unsigned long start,
1713050a9adcSChristoph Hellwig unsigned long nr_pages, struct page **pages,
1714b2cac248SLorenzo Stoakes int *locked, unsigned int foll_flags)
1715050a9adcSChristoph Hellwig {
1716050a9adcSChristoph Hellwig struct vm_area_struct *vma;
1717b2a72dffSJason Gunthorpe bool must_unlock = false;
1718050a9adcSChristoph Hellwig unsigned long vm_flags;
171924dc20c7SPavel Tatashin long i;
1720050a9adcSChristoph Hellwig
1721b2a72dffSJason Gunthorpe if (!nr_pages)
1722b2a72dffSJason Gunthorpe return 0;
1723b2a72dffSJason Gunthorpe
1724b2a72dffSJason Gunthorpe /*
1725b2a72dffSJason Gunthorpe * The internal caller expects GUP to manage the lock internally and the
1726b2a72dffSJason Gunthorpe * lock must be released when this returns.
1727b2a72dffSJason Gunthorpe */
17289a863a6aSJason Gunthorpe if (!*locked) {
1729b2a72dffSJason Gunthorpe if (mmap_read_lock_killable(mm))
1730b2a72dffSJason Gunthorpe return -EAGAIN;
1731b2a72dffSJason Gunthorpe must_unlock = true;
1732b2a72dffSJason Gunthorpe *locked = 1;
1733b2a72dffSJason Gunthorpe }
1734b2a72dffSJason Gunthorpe
1735050a9adcSChristoph Hellwig /* calculate required read or write permissions.
1736050a9adcSChristoph Hellwig * If FOLL_FORCE is set, we only require the "MAY" flags.
1737050a9adcSChristoph Hellwig */
1738050a9adcSChristoph Hellwig vm_flags = (foll_flags & FOLL_WRITE) ?
1739050a9adcSChristoph Hellwig (VM_WRITE | VM_MAYWRITE) : (VM_READ | VM_MAYREAD);
1740050a9adcSChristoph Hellwig vm_flags &= (foll_flags & FOLL_FORCE) ?
1741050a9adcSChristoph Hellwig (VM_MAYREAD | VM_MAYWRITE) : (VM_READ | VM_WRITE);
1742050a9adcSChristoph Hellwig
1743050a9adcSChristoph Hellwig for (i = 0; i < nr_pages; i++) {
1744050a9adcSChristoph Hellwig vma = find_vma(mm, start);
1745050a9adcSChristoph Hellwig if (!vma)
1746b2a72dffSJason Gunthorpe break;
1747050a9adcSChristoph Hellwig
1748050a9adcSChristoph Hellwig /* protect what we can, including chardevs */
1749050a9adcSChristoph Hellwig if ((vma->vm_flags & (VM_IO | VM_PFNMAP)) ||
1750050a9adcSChristoph Hellwig !(vm_flags & vma->vm_flags))
1751b2a72dffSJason Gunthorpe break;
1752050a9adcSChristoph Hellwig
1753050a9adcSChristoph Hellwig if (pages) {
1754396a400bSLinus Walleij pages[i] = virt_to_page((void *)start);
1755050a9adcSChristoph Hellwig if (pages[i])
1756050a9adcSChristoph Hellwig get_page(pages[i]);
1757050a9adcSChristoph Hellwig }
1758b2cac248SLorenzo Stoakes
1759050a9adcSChristoph Hellwig start = (start + PAGE_SIZE) & PAGE_MASK;
1760050a9adcSChristoph Hellwig }
1761050a9adcSChristoph Hellwig
1762b2a72dffSJason Gunthorpe if (must_unlock && *locked) {
1763b2a72dffSJason Gunthorpe mmap_read_unlock(mm);
1764b2a72dffSJason Gunthorpe *locked = 0;
1765b2a72dffSJason Gunthorpe }
1766050a9adcSChristoph Hellwig
1767050a9adcSChristoph Hellwig return i ? : -EFAULT;
1768050a9adcSChristoph Hellwig }
1769050a9adcSChristoph Hellwig #endif /* !CONFIG_MMU */
1770d3649f68SChristoph Hellwig
17718f942eeaSJann Horn /**
1772bb523b40SAndreas Gruenbacher * fault_in_writeable - fault in userspace address range for writing
1773bb523b40SAndreas Gruenbacher * @uaddr: start of address range
1774bb523b40SAndreas Gruenbacher * @size: size of address range
1775bb523b40SAndreas Gruenbacher *
1776bb523b40SAndreas Gruenbacher * Returns the number of bytes not faulted in (like copy_to_user() and
1777bb523b40SAndreas Gruenbacher * copy_from_user()).
1778bb523b40SAndreas Gruenbacher */
fault_in_writeable(char __user * uaddr,size_t size)1779bb523b40SAndreas Gruenbacher size_t fault_in_writeable(char __user *uaddr, size_t size)
1780bb523b40SAndreas Gruenbacher {
1781bb523b40SAndreas Gruenbacher char __user *start = uaddr, *end;
1782bb523b40SAndreas Gruenbacher
1783bb523b40SAndreas Gruenbacher if (unlikely(size == 0))
1784bb523b40SAndreas Gruenbacher return 0;
1785677b2a8cSChristophe Leroy if (!user_write_access_begin(uaddr, size))
1786bb523b40SAndreas Gruenbacher return size;
1787677b2a8cSChristophe Leroy if (!PAGE_ALIGNED(uaddr)) {
1788677b2a8cSChristophe Leroy unsafe_put_user(0, uaddr, out);
1789bb523b40SAndreas Gruenbacher uaddr = (char __user *)PAGE_ALIGN((unsigned long)uaddr);
1790bb523b40SAndreas Gruenbacher }
1791bb523b40SAndreas Gruenbacher end = (char __user *)PAGE_ALIGN((unsigned long)start + size);
1792bb523b40SAndreas Gruenbacher if (unlikely(end < start))
1793bb523b40SAndreas Gruenbacher end = NULL;
1794bb523b40SAndreas Gruenbacher while (uaddr != end) {
1795677b2a8cSChristophe Leroy unsafe_put_user(0, uaddr, out);
1796bb523b40SAndreas Gruenbacher uaddr += PAGE_SIZE;
1797bb523b40SAndreas Gruenbacher }
1798bb523b40SAndreas Gruenbacher
1799bb523b40SAndreas Gruenbacher out:
1800677b2a8cSChristophe Leroy user_write_access_end();
1801bb523b40SAndreas Gruenbacher if (size > uaddr - start)
1802bb523b40SAndreas Gruenbacher return size - (uaddr - start);
1803bb523b40SAndreas Gruenbacher return 0;
1804bb523b40SAndreas Gruenbacher }
1805bb523b40SAndreas Gruenbacher EXPORT_SYMBOL(fault_in_writeable);
1806bb523b40SAndreas Gruenbacher
1807da32b581SCatalin Marinas /**
1808da32b581SCatalin Marinas * fault_in_subpage_writeable - fault in an address range for writing
1809da32b581SCatalin Marinas * @uaddr: start of address range
1810da32b581SCatalin Marinas * @size: size of address range
1811da32b581SCatalin Marinas *
1812da32b581SCatalin Marinas * Fault in a user address range for writing while checking for permissions at
1813da32b581SCatalin Marinas * sub-page granularity (e.g. arm64 MTE). This function should be used when
1814da32b581SCatalin Marinas * the caller cannot guarantee forward progress of a copy_to_user() loop.
1815da32b581SCatalin Marinas *
1816da32b581SCatalin Marinas * Returns the number of bytes not faulted in (like copy_to_user() and
1817da32b581SCatalin Marinas * copy_from_user()).
1818da32b581SCatalin Marinas */
fault_in_subpage_writeable(char __user * uaddr,size_t size)1819da32b581SCatalin Marinas size_t fault_in_subpage_writeable(char __user *uaddr, size_t size)
1820da32b581SCatalin Marinas {
1821da32b581SCatalin Marinas size_t faulted_in;
1822da32b581SCatalin Marinas
1823da32b581SCatalin Marinas /*
1824da32b581SCatalin Marinas * Attempt faulting in at page granularity first for page table
1825da32b581SCatalin Marinas * permission checking. The arch-specific probe_subpage_writeable()
1826da32b581SCatalin Marinas * functions may not check for this.
1827da32b581SCatalin Marinas */
1828da32b581SCatalin Marinas faulted_in = size - fault_in_writeable(uaddr, size);
1829da32b581SCatalin Marinas if (faulted_in)
1830da32b581SCatalin Marinas faulted_in -= probe_subpage_writeable(uaddr, faulted_in);
1831da32b581SCatalin Marinas
1832da32b581SCatalin Marinas return size - faulted_in;
1833da32b581SCatalin Marinas }
1834da32b581SCatalin Marinas EXPORT_SYMBOL(fault_in_subpage_writeable);
1835da32b581SCatalin Marinas
1836cdd591fcSAndreas Gruenbacher /*
1837cdd591fcSAndreas Gruenbacher * fault_in_safe_writeable - fault in an address range for writing
1838cdd591fcSAndreas Gruenbacher * @uaddr: start of address range
1839cdd591fcSAndreas Gruenbacher * @size: length of address range
1840cdd591fcSAndreas Gruenbacher *
1841fe673d3fSLinus Torvalds * Faults in an address range for writing. This is primarily useful when we
1842fe673d3fSLinus Torvalds * already know that some or all of the pages in the address range aren't in
1843fe673d3fSLinus Torvalds * memory.
1844cdd591fcSAndreas Gruenbacher *
1845fe673d3fSLinus Torvalds * Unlike fault_in_writeable(), this function is non-destructive.
1846cdd591fcSAndreas Gruenbacher *
1847cdd591fcSAndreas Gruenbacher * Note that we don't pin or otherwise hold the pages referenced that we fault
1848cdd591fcSAndreas Gruenbacher * in. There's no guarantee that they'll stay in memory for any duration of
1849cdd591fcSAndreas Gruenbacher * time.
1850cdd591fcSAndreas Gruenbacher *
1851cdd591fcSAndreas Gruenbacher * Returns the number of bytes not faulted in, like copy_to_user() and
1852cdd591fcSAndreas Gruenbacher * copy_from_user().
1853cdd591fcSAndreas Gruenbacher */
fault_in_safe_writeable(const char __user * uaddr,size_t size)1854cdd591fcSAndreas Gruenbacher size_t fault_in_safe_writeable(const char __user *uaddr, size_t size)
1855cdd591fcSAndreas Gruenbacher {
1856fe673d3fSLinus Torvalds unsigned long start = (unsigned long)uaddr, end;
1857cdd591fcSAndreas Gruenbacher struct mm_struct *mm = current->mm;
1858fe673d3fSLinus Torvalds bool unlocked = false;
1859cdd591fcSAndreas Gruenbacher
1860fe673d3fSLinus Torvalds if (unlikely(size == 0))
1861cdd591fcSAndreas Gruenbacher return 0;
1862fe673d3fSLinus Torvalds end = PAGE_ALIGN(start + size);
1863fe673d3fSLinus Torvalds if (end < start)
1864fe673d3fSLinus Torvalds end = 0;
1865fe673d3fSLinus Torvalds
1866fe673d3fSLinus Torvalds mmap_read_lock(mm);
1867fe673d3fSLinus Torvalds do {
1868fe673d3fSLinus Torvalds if (fixup_user_fault(mm, start, FAULT_FLAG_WRITE, &unlocked))
1869fe673d3fSLinus Torvalds break;
1870fe673d3fSLinus Torvalds start = (start + PAGE_SIZE) & PAGE_MASK;
1871fe673d3fSLinus Torvalds } while (start != end);
1872fe673d3fSLinus Torvalds mmap_read_unlock(mm);
1873fe673d3fSLinus Torvalds
1874fe673d3fSLinus Torvalds if (size > (unsigned long)uaddr - start)
1875fe673d3fSLinus Torvalds return size - ((unsigned long)uaddr - start);
1876fe673d3fSLinus Torvalds return 0;
1877cdd591fcSAndreas Gruenbacher }
1878cdd591fcSAndreas Gruenbacher EXPORT_SYMBOL(fault_in_safe_writeable);
1879cdd591fcSAndreas Gruenbacher
1880bb523b40SAndreas Gruenbacher /**
1881bb523b40SAndreas Gruenbacher * fault_in_readable - fault in userspace address range for reading
1882bb523b40SAndreas Gruenbacher * @uaddr: start of user address range
1883bb523b40SAndreas Gruenbacher * @size: size of user address range
1884bb523b40SAndreas Gruenbacher *
1885bb523b40SAndreas Gruenbacher * Returns the number of bytes not faulted in (like copy_to_user() and
1886bb523b40SAndreas Gruenbacher * copy_from_user()).
1887bb523b40SAndreas Gruenbacher */
fault_in_readable(const char __user * uaddr,size_t size)1888bb523b40SAndreas Gruenbacher size_t fault_in_readable(const char __user *uaddr, size_t size)
1889bb523b40SAndreas Gruenbacher {
1890bb523b40SAndreas Gruenbacher const char __user *start = uaddr, *end;
1891bb523b40SAndreas Gruenbacher volatile char c;
1892bb523b40SAndreas Gruenbacher
1893bb523b40SAndreas Gruenbacher if (unlikely(size == 0))
1894bb523b40SAndreas Gruenbacher return 0;
1895677b2a8cSChristophe Leroy if (!user_read_access_begin(uaddr, size))
1896bb523b40SAndreas Gruenbacher return size;
1897677b2a8cSChristophe Leroy if (!PAGE_ALIGNED(uaddr)) {
1898677b2a8cSChristophe Leroy unsafe_get_user(c, uaddr, out);
1899bb523b40SAndreas Gruenbacher uaddr = (const char __user *)PAGE_ALIGN((unsigned long)uaddr);
1900bb523b40SAndreas Gruenbacher }
1901bb523b40SAndreas Gruenbacher end = (const char __user *)PAGE_ALIGN((unsigned long)start + size);
1902bb523b40SAndreas Gruenbacher if (unlikely(end < start))
1903bb523b40SAndreas Gruenbacher end = NULL;
1904bb523b40SAndreas Gruenbacher while (uaddr != end) {
1905677b2a8cSChristophe Leroy unsafe_get_user(c, uaddr, out);
1906bb523b40SAndreas Gruenbacher uaddr += PAGE_SIZE;
1907bb523b40SAndreas Gruenbacher }
1908bb523b40SAndreas Gruenbacher
1909bb523b40SAndreas Gruenbacher out:
1910677b2a8cSChristophe Leroy user_read_access_end();
1911bb523b40SAndreas Gruenbacher (void)c;
1912bb523b40SAndreas Gruenbacher if (size > uaddr - start)
1913bb523b40SAndreas Gruenbacher return size - (uaddr - start);
1914bb523b40SAndreas Gruenbacher return 0;
1915bb523b40SAndreas Gruenbacher }
1916bb523b40SAndreas Gruenbacher EXPORT_SYMBOL(fault_in_readable);
1917bb523b40SAndreas Gruenbacher
1918bb523b40SAndreas Gruenbacher /**
19198f942eeaSJann Horn * get_dump_page() - pin user page in memory while writing it to core dump
19208f942eeaSJann Horn * @addr: user address
19218f942eeaSJann Horn *
19228f942eeaSJann Horn * Returns struct page pointer of user page pinned for dump,
19238f942eeaSJann Horn * to be freed afterwards by put_page().
19248f942eeaSJann Horn *
19258f942eeaSJann Horn * Returns NULL on any kind of failure - a hole must then be inserted into
19268f942eeaSJann Horn * the corefile, to preserve alignment with its headers; and also returns
19278f942eeaSJann Horn * NULL wherever the ZERO_PAGE, or an anonymous pte_none, has been found -
19288f942eeaSJann Horn * allowing a hole to be left in the corefile to save disk space.
19298f942eeaSJann Horn *
19307f3bfab5SJann Horn * Called without mmap_lock (takes and releases the mmap_lock by itself).
19318f942eeaSJann Horn */
19328f942eeaSJann Horn #ifdef CONFIG_ELF_CORE
get_dump_page(unsigned long addr)19338f942eeaSJann Horn struct page *get_dump_page(unsigned long addr)
19348f942eeaSJann Horn {
19358f942eeaSJann Horn struct page *page;
1936b2a72dffSJason Gunthorpe int locked = 0;
19377f3bfab5SJann Horn int ret;
19388f942eeaSJann Horn
1939b2cac248SLorenzo Stoakes ret = __get_user_pages_locked(current->mm, addr, 1, &page, &locked,
19407f3bfab5SJann Horn FOLL_FORCE | FOLL_DUMP | FOLL_GET);
19417f3bfab5SJann Horn return (ret == 1) ? page : NULL;
19428f942eeaSJann Horn }
19438f942eeaSJann Horn #endif /* CONFIG_ELF_CORE */
19448f942eeaSJann Horn
1945d1e153feSPavel Tatashin #ifdef CONFIG_MIGRATION
1946f68749ecSPavel Tatashin /*
194767e139b0SAlistair Popple * Returns the number of collected pages. Return value is always >= 0.
1948f68749ecSPavel Tatashin */
collect_longterm_unpinnable_pages(struct list_head * movable_page_list,unsigned long nr_pages,struct page ** pages)1949*933b08c0SZhaoyang Huang static void collect_longterm_unpinnable_pages(
195067e139b0SAlistair Popple struct list_head *movable_page_list,
195167e139b0SAlistair Popple unsigned long nr_pages,
1952f6d299ecSAlistair Popple struct page **pages)
19539a4e9f3bSAneesh Kumar K.V {
19541b7f7e58SMatthew Wilcox (Oracle) struct folio *prev_folio = NULL;
195567e139b0SAlistair Popple bool drain_allow = true;
1956*933b08c0SZhaoyang Huang unsigned long i;
19579a4e9f3bSAneesh Kumar K.V
195883c02c23SPavel Tatashin for (i = 0; i < nr_pages; i++) {
19591b7f7e58SMatthew Wilcox (Oracle) struct folio *folio = page_folio(pages[i]);
1960f9f38f78SChristoph Hellwig
19611b7f7e58SMatthew Wilcox (Oracle) if (folio == prev_folio)
196283c02c23SPavel Tatashin continue;
19631b7f7e58SMatthew Wilcox (Oracle) prev_folio = folio;
1964f9f38f78SChristoph Hellwig
19656077c943SAlex Sierra if (folio_is_longterm_pinnable(folio))
1966f9f38f78SChristoph Hellwig continue;
196767e139b0SAlistair Popple
196867e139b0SAlistair Popple if (folio_is_device_coherent(folio))
196967e139b0SAlistair Popple continue;
197067e139b0SAlistair Popple
19711b7f7e58SMatthew Wilcox (Oracle) if (folio_test_hugetlb(folio)) {
19726aa3a920SSidhartha Kumar isolate_hugetlb(folio, movable_page_list);
1973f9f38f78SChristoph Hellwig continue;
1974f9f38f78SChristoph Hellwig }
1975f9f38f78SChristoph Hellwig
19761b7f7e58SMatthew Wilcox (Oracle) if (!folio_test_lru(folio) && drain_allow) {
19779a4e9f3bSAneesh Kumar K.V lru_add_drain_all();
19789a4e9f3bSAneesh Kumar K.V drain_allow = false;
19799a4e9f3bSAneesh Kumar K.V }
19809a4e9f3bSAneesh Kumar K.V
1981be2d5756SBaolin Wang if (!folio_isolate_lru(folio))
19826e7f34ebSPavel Tatashin continue;
198367e139b0SAlistair Popple
198467e139b0SAlistair Popple list_add_tail(&folio->lru, movable_page_list);
19851b7f7e58SMatthew Wilcox (Oracle) node_stat_mod_folio(folio,
19861b7f7e58SMatthew Wilcox (Oracle) NR_ISOLATED_ANON + folio_is_file_lru(folio),
19871b7f7e58SMatthew Wilcox (Oracle) folio_nr_pages(folio));
19889a4e9f3bSAneesh Kumar K.V }
1989f68749ecSPavel Tatashin }
1990f9f38f78SChristoph Hellwig
199167e139b0SAlistair Popple /*
199267e139b0SAlistair Popple * Unpins all pages and migrates device coherent pages and movable_page_list.
199367e139b0SAlistair Popple * Returns -EAGAIN if all pages were successfully migrated or -errno for failure
199467e139b0SAlistair Popple * (or partial success).
199567e139b0SAlistair Popple */
migrate_longterm_unpinnable_pages(struct list_head * movable_page_list,unsigned long nr_pages,struct page ** pages)199667e139b0SAlistair Popple static int migrate_longterm_unpinnable_pages(
199767e139b0SAlistair Popple struct list_head *movable_page_list,
199867e139b0SAlistair Popple unsigned long nr_pages,
199967e139b0SAlistair Popple struct page **pages)
200067e139b0SAlistair Popple {
200167e139b0SAlistair Popple int ret;
200267e139b0SAlistair Popple unsigned long i;
200367e139b0SAlistair Popple
200467e139b0SAlistair Popple for (i = 0; i < nr_pages; i++) {
200567e139b0SAlistair Popple struct folio *folio = page_folio(pages[i]);
200667e139b0SAlistair Popple
200767e139b0SAlistair Popple if (folio_is_device_coherent(folio)) {
200867e139b0SAlistair Popple /*
200967e139b0SAlistair Popple * Migration will fail if the page is pinned, so convert
201067e139b0SAlistair Popple * the pin on the source page to a normal reference.
201167e139b0SAlistair Popple */
201267e139b0SAlistair Popple pages[i] = NULL;
201367e139b0SAlistair Popple folio_get(folio);
201467e139b0SAlistair Popple gup_put_folio(folio, 1, FOLL_PIN);
201567e139b0SAlistair Popple
201667e139b0SAlistair Popple if (migrate_device_coherent_page(&folio->page)) {
201767e139b0SAlistair Popple ret = -EBUSY;
201867e139b0SAlistair Popple goto err;
201967e139b0SAlistair Popple }
202067e139b0SAlistair Popple
202167e139b0SAlistair Popple continue;
202267e139b0SAlistair Popple }
202367e139b0SAlistair Popple
202467e139b0SAlistair Popple /*
202567e139b0SAlistair Popple * We can't migrate pages with unexpected references, so drop
202667e139b0SAlistair Popple * the reference obtained by __get_user_pages_locked().
202767e139b0SAlistair Popple * Migrating pages have been added to movable_page_list after
202867e139b0SAlistair Popple * calling folio_isolate_lru() which takes a reference so the
202967e139b0SAlistair Popple * page won't be freed if it's migrating.
203067e139b0SAlistair Popple */
203167e139b0SAlistair Popple unpin_user_page(pages[i]);
203267e139b0SAlistair Popple pages[i] = NULL;
203367e139b0SAlistair Popple }
203467e139b0SAlistair Popple
203567e139b0SAlistair Popple if (!list_empty(movable_page_list)) {
2036f9f38f78SChristoph Hellwig struct migration_target_control mtc = {
2037f9f38f78SChristoph Hellwig .nid = NUMA_NO_NODE,
2038f9f38f78SChristoph Hellwig .gfp_mask = GFP_USER | __GFP_NOWARN,
2039f9f38f78SChristoph Hellwig };
2040f9f38f78SChristoph Hellwig
204167e139b0SAlistair Popple if (migrate_pages(movable_page_list, alloc_migration_target,
2042f0f44638SPavel Tatashin NULL, (unsigned long)&mtc, MIGRATE_SYNC,
204367e139b0SAlistair Popple MR_LONGTERM_PIN, NULL)) {
2044f9f38f78SChristoph Hellwig ret = -ENOMEM;
204567e139b0SAlistair Popple goto err;
204667e139b0SAlistair Popple }
2047f68749ecSPavel Tatashin }
2048f68749ecSPavel Tatashin
204967e139b0SAlistair Popple putback_movable_pages(movable_page_list);
205024a95998SAlistair Popple
205167e139b0SAlistair Popple return -EAGAIN;
205267e139b0SAlistair Popple
205367e139b0SAlistair Popple err:
205467e139b0SAlistair Popple for (i = 0; i < nr_pages; i++)
205567e139b0SAlistair Popple if (pages[i])
205667e139b0SAlistair Popple unpin_user_page(pages[i]);
205767e139b0SAlistair Popple putback_movable_pages(movable_page_list);
205867e139b0SAlistair Popple
205967e139b0SAlistair Popple return ret;
206067e139b0SAlistair Popple }
206167e139b0SAlistair Popple
206267e139b0SAlistair Popple /*
206367e139b0SAlistair Popple * Check whether all pages are *allowed* to be pinned. Rather confusingly, all
206467e139b0SAlistair Popple * pages in the range are required to be pinned via FOLL_PIN, before calling
206567e139b0SAlistair Popple * this routine.
206667e139b0SAlistair Popple *
206767e139b0SAlistair Popple * If any pages in the range are not allowed to be pinned, then this routine
206867e139b0SAlistair Popple * will migrate those pages away, unpin all the pages in the range and return
206967e139b0SAlistair Popple * -EAGAIN. The caller should re-pin the entire range with FOLL_PIN and then
207067e139b0SAlistair Popple * call this routine again.
207167e139b0SAlistair Popple *
207267e139b0SAlistair Popple * If an error other than -EAGAIN occurs, this indicates a migration failure.
207367e139b0SAlistair Popple * The caller should give up, and propagate the error back up the call stack.
207467e139b0SAlistair Popple *
207567e139b0SAlistair Popple * If everything is OK and all pages in the range are allowed to be pinned, then
207667e139b0SAlistair Popple * this routine leaves all pages pinned and returns zero for success.
207767e139b0SAlistair Popple */
check_and_migrate_movable_pages(unsigned long nr_pages,struct page ** pages)207867e139b0SAlistair Popple static long check_and_migrate_movable_pages(unsigned long nr_pages,
207967e139b0SAlistair Popple struct page **pages)
208067e139b0SAlistair Popple {
208167e139b0SAlistair Popple LIST_HEAD(movable_page_list);
208267e139b0SAlistair Popple
2083*933b08c0SZhaoyang Huang collect_longterm_unpinnable_pages(&movable_page_list, nr_pages, pages);
2084*933b08c0SZhaoyang Huang if (list_empty(&movable_page_list))
208567e139b0SAlistair Popple return 0;
208667e139b0SAlistair Popple
208767e139b0SAlistair Popple return migrate_longterm_unpinnable_pages(&movable_page_list, nr_pages,
208867e139b0SAlistair Popple pages);
20899a4e9f3bSAneesh Kumar K.V }
20909a4e9f3bSAneesh Kumar K.V #else
check_and_migrate_movable_pages(unsigned long nr_pages,struct page ** pages)2091f68749ecSPavel Tatashin static long check_and_migrate_movable_pages(unsigned long nr_pages,
2092f6d299ecSAlistair Popple struct page **pages)
20939a4e9f3bSAneesh Kumar K.V {
209424a95998SAlistair Popple return 0;
20959a4e9f3bSAneesh Kumar K.V }
2096d1e153feSPavel Tatashin #endif /* CONFIG_MIGRATION */
20979a4e9f3bSAneesh Kumar K.V
20982bb6d283SDan Williams /*
2099932f4a63SIra Weiny * __gup_longterm_locked() is a wrapper for __get_user_pages_locked which
2100932f4a63SIra Weiny * allows us to process the FOLL_LONGTERM flag.
21012bb6d283SDan Williams */
__gup_longterm_locked(struct mm_struct * mm,unsigned long start,unsigned long nr_pages,struct page ** pages,int * locked,unsigned int gup_flags)210264019a2eSPeter Xu static long __gup_longterm_locked(struct mm_struct *mm,
2103932f4a63SIra Weiny unsigned long start,
2104932f4a63SIra Weiny unsigned long nr_pages,
2105932f4a63SIra Weiny struct page **pages,
210653b2d09bSJason Gunthorpe int *locked,
2107932f4a63SIra Weiny unsigned int gup_flags)
21082bb6d283SDan Williams {
2109f68749ecSPavel Tatashin unsigned int flags;
211024a95998SAlistair Popple long rc, nr_pinned_pages;
21112bb6d283SDan Williams
2112f68749ecSPavel Tatashin if (!(gup_flags & FOLL_LONGTERM))
2113b2cac248SLorenzo Stoakes return __get_user_pages_locked(mm, start, nr_pages, pages,
211453b2d09bSJason Gunthorpe locked, gup_flags);
211567e139b0SAlistair Popple
21161a08ae36SPavel Tatashin flags = memalloc_pin_save();
2117f68749ecSPavel Tatashin do {
211824a95998SAlistair Popple nr_pinned_pages = __get_user_pages_locked(mm, start, nr_pages,
2119b2cac248SLorenzo Stoakes pages, locked,
212024a95998SAlistair Popple gup_flags);
212124a95998SAlistair Popple if (nr_pinned_pages <= 0) {
212224a95998SAlistair Popple rc = nr_pinned_pages;
2123f68749ecSPavel Tatashin break;
212424a95998SAlistair Popple }
2125d64e2dbcSJason Gunthorpe
2126d64e2dbcSJason Gunthorpe /* FOLL_LONGTERM implies FOLL_PIN */
2127f6d299ecSAlistair Popple rc = check_and_migrate_movable_pages(nr_pinned_pages, pages);
212824a95998SAlistair Popple } while (rc == -EAGAIN);
21291a08ae36SPavel Tatashin memalloc_pin_restore(flags);
213024a95998SAlistair Popple return rc ? rc : nr_pinned_pages;
21312bb6d283SDan Williams }
2132932f4a63SIra Weiny
2133d64e2dbcSJason Gunthorpe /*
2134d64e2dbcSJason Gunthorpe * Check that the given flags are valid for the exported gup/pup interface, and
2135d64e2dbcSJason Gunthorpe * update them with the required flags that the caller must have set.
2136d64e2dbcSJason Gunthorpe */
is_valid_gup_args(struct page ** pages,int * locked,unsigned int * gup_flags_p,unsigned int to_set)2137b2cac248SLorenzo Stoakes static bool is_valid_gup_args(struct page **pages, int *locked,
2138b2cac248SLorenzo Stoakes unsigned int *gup_flags_p, unsigned int to_set)
2139447f3e45SBarry Song {
2140d64e2dbcSJason Gunthorpe unsigned int gup_flags = *gup_flags_p;
2141d64e2dbcSJason Gunthorpe
2142447f3e45SBarry Song /*
2143d64e2dbcSJason Gunthorpe * These flags not allowed to be specified externally to the gup
2144d64e2dbcSJason Gunthorpe * interfaces:
214549db746dSLorenzo Stoakes * - FOLL_TOUCH/FOLL_PIN/FOLL_TRIED/FOLL_FAST_ONLY are internal only
2146d64e2dbcSJason Gunthorpe * - FOLL_REMOTE is internal only and used on follow_page()
2147f04740f5SJason Gunthorpe * - FOLL_UNLOCKABLE is internal only and used if locked is !NULL
2148447f3e45SBarry Song */
214949db746dSLorenzo Stoakes if (WARN_ON_ONCE(gup_flags & INTERNAL_GUP_FLAGS))
2150447f3e45SBarry Song return false;
2151447f3e45SBarry Song
2152d64e2dbcSJason Gunthorpe gup_flags |= to_set;
2153f04740f5SJason Gunthorpe if (locked) {
2154f04740f5SJason Gunthorpe /* At the external interface locked must be set */
2155f04740f5SJason Gunthorpe if (WARN_ON_ONCE(*locked != 1))
2156f04740f5SJason Gunthorpe return false;
2157f04740f5SJason Gunthorpe
2158f04740f5SJason Gunthorpe gup_flags |= FOLL_UNLOCKABLE;
2159f04740f5SJason Gunthorpe }
2160d64e2dbcSJason Gunthorpe
2161d64e2dbcSJason Gunthorpe /* FOLL_GET and FOLL_PIN are mutually exclusive. */
2162d64e2dbcSJason Gunthorpe if (WARN_ON_ONCE((gup_flags & (FOLL_PIN | FOLL_GET)) ==
2163d64e2dbcSJason Gunthorpe (FOLL_PIN | FOLL_GET)))
2164d64e2dbcSJason Gunthorpe return false;
2165d64e2dbcSJason Gunthorpe
2166d64e2dbcSJason Gunthorpe /* LONGTERM can only be specified when pinning */
2167d64e2dbcSJason Gunthorpe if (WARN_ON_ONCE(!(gup_flags & FOLL_PIN) && (gup_flags & FOLL_LONGTERM)))
2168d64e2dbcSJason Gunthorpe return false;
2169d64e2dbcSJason Gunthorpe
2170d64e2dbcSJason Gunthorpe /* Pages input must be given if using GET/PIN */
2171d64e2dbcSJason Gunthorpe if (WARN_ON_ONCE((gup_flags & (FOLL_GET | FOLL_PIN)) && !pages))
2172d64e2dbcSJason Gunthorpe return false;
2173d64e2dbcSJason Gunthorpe
2174d64e2dbcSJason Gunthorpe /* We want to allow the pgmap to be hot-unplugged at all times */
2175d64e2dbcSJason Gunthorpe if (WARN_ON_ONCE((gup_flags & FOLL_LONGTERM) &&
2176d64e2dbcSJason Gunthorpe (gup_flags & FOLL_PCI_P2PDMA)))
2177d64e2dbcSJason Gunthorpe return false;
2178d64e2dbcSJason Gunthorpe
2179d64e2dbcSJason Gunthorpe *gup_flags_p = gup_flags;
2180447f3e45SBarry Song return true;
2181447f3e45SBarry Song }
2182447f3e45SBarry Song
218322bf29b6SJohn Hubbard #ifdef CONFIG_MMU
2184adc8cb40SSouptick Joarder /**
2185c4237f8bSJohn Hubbard * get_user_pages_remote() - pin user pages in memory
2186c4237f8bSJohn Hubbard * @mm: mm_struct of target mm
2187c4237f8bSJohn Hubbard * @start: starting user address
2188c4237f8bSJohn Hubbard * @nr_pages: number of pages from start to pin
2189c4237f8bSJohn Hubbard * @gup_flags: flags modifying lookup behaviour
2190c4237f8bSJohn Hubbard * @pages: array that receives pointers to the pages pinned.
2191c4237f8bSJohn Hubbard * Should be at least nr_pages long. Or NULL, if caller
2192c4237f8bSJohn Hubbard * only intends to ensure the pages are faulted in.
2193c4237f8bSJohn Hubbard * @locked: pointer to lock flag indicating whether lock is held and
2194c4237f8bSJohn Hubbard * subsequently whether VM_FAULT_RETRY functionality can be
2195c4237f8bSJohn Hubbard * utilised. Lock must initially be held.
2196c4237f8bSJohn Hubbard *
2197c4237f8bSJohn Hubbard * Returns either number of pages pinned (which may be less than the
2198c4237f8bSJohn Hubbard * number requested), or an error. Details about the return value:
2199c4237f8bSJohn Hubbard *
2200c4237f8bSJohn Hubbard * -- If nr_pages is 0, returns 0.
2201c4237f8bSJohn Hubbard * -- If nr_pages is >0, but no pages were pinned, returns -errno.
2202c4237f8bSJohn Hubbard * -- If nr_pages is >0, and some pages were pinned, returns the number of
2203c4237f8bSJohn Hubbard * pages pinned. Again, this may be less than nr_pages.
2204c4237f8bSJohn Hubbard *
2205c4237f8bSJohn Hubbard * The caller is responsible for releasing returned @pages, via put_page().
2206c4237f8bSJohn Hubbard *
2207c1e8d7c6SMichel Lespinasse * Must be called with mmap_lock held for read or write.
2208c4237f8bSJohn Hubbard *
2209adc8cb40SSouptick Joarder * get_user_pages_remote walks a process's page tables and takes a reference
2210adc8cb40SSouptick Joarder * to each struct page that each user address corresponds to at a given
2211c4237f8bSJohn Hubbard * instant. That is, it takes the page that would be accessed if a user
2212c4237f8bSJohn Hubbard * thread accesses the given user virtual address at that instant.
2213c4237f8bSJohn Hubbard *
2214c4237f8bSJohn Hubbard * This does not guarantee that the page exists in the user mappings when
2215adc8cb40SSouptick Joarder * get_user_pages_remote returns, and there may even be a completely different
2216c4237f8bSJohn Hubbard * page there in some cases (eg. if mmapped pagecache has been invalidated
22175da1a868SJingyu Wang * and subsequently re-faulted). However it does guarantee that the page
2218c4237f8bSJohn Hubbard * won't be freed completely. And mostly callers simply care that the page
2219c4237f8bSJohn Hubbard * contains data that was valid *at some point in time*. Typically, an IO
2220c4237f8bSJohn Hubbard * or similar operation cannot guarantee anything stronger anyway because
2221c4237f8bSJohn Hubbard * locks can't be held over the syscall boundary.
2222c4237f8bSJohn Hubbard *
2223c4237f8bSJohn Hubbard * If gup_flags & FOLL_WRITE == 0, the page must not be written to. If the page
2224c4237f8bSJohn Hubbard * is written to, set_page_dirty (or set_page_dirty_lock, as appropriate) must
2225c4237f8bSJohn Hubbard * be called after the page is finished with, and before put_page is called.
2226c4237f8bSJohn Hubbard *
2227adc8cb40SSouptick Joarder * get_user_pages_remote is typically used for fewer-copy IO operations,
2228adc8cb40SSouptick Joarder * to get a handle on the memory by some means other than accesses
2229adc8cb40SSouptick Joarder * via the user virtual addresses. The pages may be submitted for
2230adc8cb40SSouptick Joarder * DMA to devices or accessed via their kernel linear mapping (via the
2231adc8cb40SSouptick Joarder * kmap APIs). Care should be taken to use the correct cache flushing APIs.
2232c4237f8bSJohn Hubbard *
2233c4237f8bSJohn Hubbard * See also get_user_pages_fast, for performance critical applications.
2234c4237f8bSJohn Hubbard *
2235adc8cb40SSouptick Joarder * get_user_pages_remote should be phased out in favor of
2236c4237f8bSJohn Hubbard * get_user_pages_locked|unlocked or get_user_pages_fast. Nothing
2237adc8cb40SSouptick Joarder * should use get_user_pages_remote because it cannot pass
2238c4237f8bSJohn Hubbard * FAULT_FLAG_ALLOW_RETRY to handle_mm_fault.
2239c4237f8bSJohn Hubbard */
get_user_pages_remote(struct mm_struct * mm,unsigned long start,unsigned long nr_pages,unsigned int gup_flags,struct page ** pages,int * locked)224064019a2eSPeter Xu long get_user_pages_remote(struct mm_struct *mm,
2241c4237f8bSJohn Hubbard unsigned long start, unsigned long nr_pages,
2242c4237f8bSJohn Hubbard unsigned int gup_flags, struct page **pages,
2243ca5e8632SLorenzo Stoakes int *locked)
2244c4237f8bSJohn Hubbard {
22459a863a6aSJason Gunthorpe int local_locked = 1;
22469a863a6aSJason Gunthorpe
2247b2cac248SLorenzo Stoakes if (!is_valid_gup_args(pages, locked, &gup_flags,
2248d64e2dbcSJason Gunthorpe FOLL_TOUCH | FOLL_REMOTE))
2249eddb1c22SJohn Hubbard return -EINVAL;
2250eddb1c22SJohn Hubbard
2251b2cac248SLorenzo Stoakes return __get_user_pages_locked(mm, start, nr_pages, pages,
22529a863a6aSJason Gunthorpe locked ? locked : &local_locked,
2253d64e2dbcSJason Gunthorpe gup_flags);
2254c4237f8bSJohn Hubbard }
2255c4237f8bSJohn Hubbard EXPORT_SYMBOL(get_user_pages_remote);
2256c4237f8bSJohn Hubbard
2257eddb1c22SJohn Hubbard #else /* CONFIG_MMU */
get_user_pages_remote(struct mm_struct * mm,unsigned long start,unsigned long nr_pages,unsigned int gup_flags,struct page ** pages,int * locked)225864019a2eSPeter Xu long get_user_pages_remote(struct mm_struct *mm,
2259eddb1c22SJohn Hubbard unsigned long start, unsigned long nr_pages,
2260eddb1c22SJohn Hubbard unsigned int gup_flags, struct page **pages,
2261ca5e8632SLorenzo Stoakes int *locked)
2262eddb1c22SJohn Hubbard {
2263eddb1c22SJohn Hubbard return 0;
2264eddb1c22SJohn Hubbard }
2265eddb1c22SJohn Hubbard #endif /* !CONFIG_MMU */
2266eddb1c22SJohn Hubbard
2267adc8cb40SSouptick Joarder /**
2268adc8cb40SSouptick Joarder * get_user_pages() - pin user pages in memory
2269adc8cb40SSouptick Joarder * @start: starting user address
2270adc8cb40SSouptick Joarder * @nr_pages: number of pages from start to pin
2271adc8cb40SSouptick Joarder * @gup_flags: flags modifying lookup behaviour
2272adc8cb40SSouptick Joarder * @pages: array that receives pointers to the pages pinned.
2273adc8cb40SSouptick Joarder * Should be at least nr_pages long. Or NULL, if caller
2274adc8cb40SSouptick Joarder * only intends to ensure the pages are faulted in.
2275adc8cb40SSouptick Joarder *
227664019a2eSPeter Xu * This is the same as get_user_pages_remote(), just with a less-flexible
227764019a2eSPeter Xu * calling convention where we assume that the mm being operated on belongs to
227864019a2eSPeter Xu * the current task, and doesn't allow passing of a locked parameter. We also
227964019a2eSPeter Xu * obviously don't pass FOLL_REMOTE in here.
2280932f4a63SIra Weiny */
get_user_pages(unsigned long start,unsigned long nr_pages,unsigned int gup_flags,struct page ** pages)2281932f4a63SIra Weiny long get_user_pages(unsigned long start, unsigned long nr_pages,
228254d02069SLorenzo Stoakes unsigned int gup_flags, struct page **pages)
2283932f4a63SIra Weiny {
22849a863a6aSJason Gunthorpe int locked = 1;
22859a863a6aSJason Gunthorpe
2286b2cac248SLorenzo Stoakes if (!is_valid_gup_args(pages, NULL, &gup_flags, FOLL_TOUCH))
2287eddb1c22SJohn Hubbard return -EINVAL;
2288eddb1c22SJohn Hubbard
2289afa3c33eSJason Gunthorpe return __get_user_pages_locked(current->mm, start, nr_pages, pages,
2290b2cac248SLorenzo Stoakes &locked, gup_flags);
2291932f4a63SIra Weiny }
2292932f4a63SIra Weiny EXPORT_SYMBOL(get_user_pages);
22932bb6d283SDan Williams
2294acc3c8d1SKirill A. Shutemov /*
2295d3649f68SChristoph Hellwig * get_user_pages_unlocked() is suitable to replace the form:
2296acc3c8d1SKirill A. Shutemov *
22973e4e28c5SMichel Lespinasse * mmap_read_lock(mm);
229864019a2eSPeter Xu * get_user_pages(mm, ..., pages, NULL);
22993e4e28c5SMichel Lespinasse * mmap_read_unlock(mm);
2300d3649f68SChristoph Hellwig *
2301d3649f68SChristoph Hellwig * with:
2302d3649f68SChristoph Hellwig *
230364019a2eSPeter Xu * get_user_pages_unlocked(mm, ..., pages);
2304d3649f68SChristoph Hellwig *
2305d3649f68SChristoph Hellwig * It is functionally equivalent to get_user_pages_fast so
2306d3649f68SChristoph Hellwig * get_user_pages_fast should be used instead if specific gup_flags
2307d3649f68SChristoph Hellwig * (e.g. FOLL_FORCE) are not required.
2308acc3c8d1SKirill A. Shutemov */
get_user_pages_unlocked(unsigned long start,unsigned long nr_pages,struct page ** pages,unsigned int gup_flags)2309d3649f68SChristoph Hellwig long get_user_pages_unlocked(unsigned long start, unsigned long nr_pages,
2310d3649f68SChristoph Hellwig struct page **pages, unsigned int gup_flags)
2311acc3c8d1SKirill A. Shutemov {
2312b2a72dffSJason Gunthorpe int locked = 0;
2313acc3c8d1SKirill A. Shutemov
2314b2cac248SLorenzo Stoakes if (!is_valid_gup_args(pages, NULL, &gup_flags,
2315f04740f5SJason Gunthorpe FOLL_TOUCH | FOLL_UNLOCKABLE))
2316d64e2dbcSJason Gunthorpe return -EINVAL;
2317d64e2dbcSJason Gunthorpe
2318afa3c33eSJason Gunthorpe return __get_user_pages_locked(current->mm, start, nr_pages, pages,
2319b2cac248SLorenzo Stoakes &locked, gup_flags);
2320acc3c8d1SKirill A. Shutemov }
2321d3649f68SChristoph Hellwig EXPORT_SYMBOL(get_user_pages_unlocked);
23222667f50eSSteve Capper
23232667f50eSSteve Capper /*
232467a929e0SChristoph Hellwig * Fast GUP
23252667f50eSSteve Capper *
23262667f50eSSteve Capper * get_user_pages_fast attempts to pin user pages by walking the page
23272667f50eSSteve Capper * tables directly and avoids taking locks. Thus the walker needs to be
23282667f50eSSteve Capper * protected from page table pages being freed from under it, and should
23292667f50eSSteve Capper * block any THP splits.
23302667f50eSSteve Capper *
23312667f50eSSteve Capper * One way to achieve this is to have the walker disable interrupts, and
23322667f50eSSteve Capper * rely on IPIs from the TLB flushing code blocking before the page table
23332667f50eSSteve Capper * pages are freed. This is unsuitable for architectures that do not need
23342667f50eSSteve Capper * to broadcast an IPI when invalidating TLBs.
23352667f50eSSteve Capper *
23362667f50eSSteve Capper * Another way to achieve this is to batch up page table containing pages
23372667f50eSSteve Capper * belonging to more than one mm_user, then rcu_sched a callback to free those
23382667f50eSSteve Capper * pages. Disabling interrupts will allow the fast_gup walker to both block
23392667f50eSSteve Capper * the rcu_sched callback, and an IPI that we broadcast for splitting THPs
23402667f50eSSteve Capper * (which is a relatively rare event). The code below adopts this strategy.
23412667f50eSSteve Capper *
23422667f50eSSteve Capper * Before activating this code, please be aware that the following assumptions
23432667f50eSSteve Capper * are currently made:
23442667f50eSSteve Capper *
2345ff2e6d72SPeter Zijlstra * *) Either MMU_GATHER_RCU_TABLE_FREE is enabled, and tlb_remove_table() is used to
2346e585513bSKirill A. Shutemov * free pages containing page tables or TLB flushing requires IPI broadcast.
23472667f50eSSteve Capper *
23482667f50eSSteve Capper * *) ptes can be read atomically by the architecture.
23492667f50eSSteve Capper *
23502667f50eSSteve Capper * *) access_ok is sufficient to validate userspace address ranges.
23512667f50eSSteve Capper *
23522667f50eSSteve Capper * The last two assumptions can be relaxed by the addition of helper functions.
23532667f50eSSteve Capper *
23542667f50eSSteve Capper * This code is based heavily on the PowerPC implementation by Nick Piggin.
23552667f50eSSteve Capper */
235667a929e0SChristoph Hellwig #ifdef CONFIG_HAVE_FAST_GUP
23573faa52c0SJohn Hubbard
2358a6e79df9SLorenzo Stoakes /*
2359a6e79df9SLorenzo Stoakes * Used in the GUP-fast path to determine whether a pin is permitted for a
2360a6e79df9SLorenzo Stoakes * specific folio.
2361a6e79df9SLorenzo Stoakes *
2362a6e79df9SLorenzo Stoakes * This call assumes the caller has pinned the folio, that the lowest page table
2363a6e79df9SLorenzo Stoakes * level still points to this folio, and that interrupts have been disabled.
2364a6e79df9SLorenzo Stoakes *
2365a6e79df9SLorenzo Stoakes * Writing to pinned file-backed dirty tracked folios is inherently problematic
2366a6e79df9SLorenzo Stoakes * (see comment describing the writable_file_mapping_allowed() function). We
2367a6e79df9SLorenzo Stoakes * therefore try to avoid the most egregious case of a long-term mapping doing
2368a6e79df9SLorenzo Stoakes * so.
2369a6e79df9SLorenzo Stoakes *
2370a6e79df9SLorenzo Stoakes * This function cannot be as thorough as that one as the VMA is not available
2371a6e79df9SLorenzo Stoakes * in the fast path, so instead we whitelist known good cases and if in doubt,
2372a6e79df9SLorenzo Stoakes * fall back to the slow path.
2373a6e79df9SLorenzo Stoakes */
folio_fast_pin_allowed(struct folio * folio,unsigned int flags)2374a6e79df9SLorenzo Stoakes static bool folio_fast_pin_allowed(struct folio *folio, unsigned int flags)
2375a6e79df9SLorenzo Stoakes {
2376a6e79df9SLorenzo Stoakes struct address_space *mapping;
2377a6e79df9SLorenzo Stoakes unsigned long mapping_flags;
2378a6e79df9SLorenzo Stoakes
2379a6e79df9SLorenzo Stoakes /*
2380a6e79df9SLorenzo Stoakes * If we aren't pinning then no problematic write can occur. A long term
2381a6e79df9SLorenzo Stoakes * pin is the most egregious case so this is the one we disallow.
2382a6e79df9SLorenzo Stoakes */
2383a6e79df9SLorenzo Stoakes if ((flags & (FOLL_PIN | FOLL_LONGTERM | FOLL_WRITE)) !=
2384a6e79df9SLorenzo Stoakes (FOLL_PIN | FOLL_LONGTERM | FOLL_WRITE))
2385a6e79df9SLorenzo Stoakes return true;
2386a6e79df9SLorenzo Stoakes
2387a6e79df9SLorenzo Stoakes /* The folio is pinned, so we can safely access folio fields. */
2388a6e79df9SLorenzo Stoakes
2389a6e79df9SLorenzo Stoakes if (WARN_ON_ONCE(folio_test_slab(folio)))
2390a6e79df9SLorenzo Stoakes return false;
2391a6e79df9SLorenzo Stoakes
2392a6e79df9SLorenzo Stoakes /* hugetlb mappings do not require dirty-tracking. */
2393a6e79df9SLorenzo Stoakes if (folio_test_hugetlb(folio))
2394a6e79df9SLorenzo Stoakes return true;
2395a6e79df9SLorenzo Stoakes
2396a6e79df9SLorenzo Stoakes /*
2397a6e79df9SLorenzo Stoakes * GUP-fast disables IRQs. When IRQS are disabled, RCU grace periods
2398a6e79df9SLorenzo Stoakes * cannot proceed, which means no actions performed under RCU can
2399a6e79df9SLorenzo Stoakes * proceed either.
2400a6e79df9SLorenzo Stoakes *
2401a6e79df9SLorenzo Stoakes * inodes and thus their mappings are freed under RCU, which means the
2402a6e79df9SLorenzo Stoakes * mapping cannot be freed beneath us and thus we can safely dereference
2403a6e79df9SLorenzo Stoakes * it.
2404a6e79df9SLorenzo Stoakes */
2405a6e79df9SLorenzo Stoakes lockdep_assert_irqs_disabled();
2406a6e79df9SLorenzo Stoakes
2407a6e79df9SLorenzo Stoakes /*
2408a6e79df9SLorenzo Stoakes * However, there may be operations which _alter_ the mapping, so ensure
2409a6e79df9SLorenzo Stoakes * we read it once and only once.
2410a6e79df9SLorenzo Stoakes */
2411a6e79df9SLorenzo Stoakes mapping = READ_ONCE(folio->mapping);
2412a6e79df9SLorenzo Stoakes
2413a6e79df9SLorenzo Stoakes /*
2414a6e79df9SLorenzo Stoakes * The mapping may have been truncated, in any case we cannot determine
2415a6e79df9SLorenzo Stoakes * if this mapping is safe - fall back to slow path to determine how to
2416a6e79df9SLorenzo Stoakes * proceed.
2417a6e79df9SLorenzo Stoakes */
2418a6e79df9SLorenzo Stoakes if (!mapping)
2419a6e79df9SLorenzo Stoakes return false;
2420a6e79df9SLorenzo Stoakes
2421a6e79df9SLorenzo Stoakes /* Anonymous folios pose no problem. */
2422a6e79df9SLorenzo Stoakes mapping_flags = (unsigned long)mapping & PAGE_MAPPING_FLAGS;
2423a6e79df9SLorenzo Stoakes if (mapping_flags)
2424a6e79df9SLorenzo Stoakes return mapping_flags & PAGE_MAPPING_ANON;
2425a6e79df9SLorenzo Stoakes
2426a6e79df9SLorenzo Stoakes /*
2427a6e79df9SLorenzo Stoakes * At this point, we know the mapping is non-null and points to an
2428a6e79df9SLorenzo Stoakes * address_space object. The only remaining whitelisted file system is
2429a6e79df9SLorenzo Stoakes * shmem.
2430a6e79df9SLorenzo Stoakes */
2431a6e79df9SLorenzo Stoakes return shmem_mapping(mapping);
2432a6e79df9SLorenzo Stoakes }
2433a6e79df9SLorenzo Stoakes
undo_dev_pagemap(int * nr,int nr_start,unsigned int flags,struct page ** pages)2434790c7369SGuenter Roeck static void __maybe_unused undo_dev_pagemap(int *nr, int nr_start,
24353b78d834SJohn Hubbard unsigned int flags,
2436790c7369SGuenter Roeck struct page **pages)
2437b59f65faSKirill A. Shutemov {
2438b59f65faSKirill A. Shutemov while ((*nr) - nr_start) {
2439b59f65faSKirill A. Shutemov struct page *page = pages[--(*nr)];
2440b59f65faSKirill A. Shutemov
2441b59f65faSKirill A. Shutemov ClearPageReferenced(page);
24423faa52c0SJohn Hubbard if (flags & FOLL_PIN)
24433faa52c0SJohn Hubbard unpin_user_page(page);
24443faa52c0SJohn Hubbard else
2445b59f65faSKirill A. Shutemov put_page(page);
2446b59f65faSKirill A. Shutemov }
2447b59f65faSKirill A. Shutemov }
2448b59f65faSKirill A. Shutemov
244926273f5fSYang Shi /**
245026273f5fSYang Shi * try_grab_folio_fast() - Attempt to get or pin a folio in fast path.
245126273f5fSYang Shi * @page: pointer to page to be grabbed
245226273f5fSYang Shi * @refs: the value to (effectively) add to the folio's refcount
245326273f5fSYang Shi * @flags: gup flags: these are the FOLL_* flag values.
245426273f5fSYang Shi *
245526273f5fSYang Shi * "grab" names in this file mean, "look at flags to decide whether to use
245626273f5fSYang Shi * FOLL_PIN or FOLL_GET behavior, when incrementing the folio's refcount.
245726273f5fSYang Shi *
245826273f5fSYang Shi * Either FOLL_PIN or FOLL_GET (or neither) must be set, but not both at the
245926273f5fSYang Shi * same time. (That's true throughout the get_user_pages*() and
246026273f5fSYang Shi * pin_user_pages*() APIs.) Cases:
246126273f5fSYang Shi *
246226273f5fSYang Shi * FOLL_GET: folio's refcount will be incremented by @refs.
246326273f5fSYang Shi *
246426273f5fSYang Shi * FOLL_PIN on large folios: folio's refcount will be incremented by
246526273f5fSYang Shi * @refs, and its pincount will be incremented by @refs.
246626273f5fSYang Shi *
246726273f5fSYang Shi * FOLL_PIN on single-page folios: folio's refcount will be incremented by
246826273f5fSYang Shi * @refs * GUP_PIN_COUNTING_BIAS.
246926273f5fSYang Shi *
247026273f5fSYang Shi * Return: The folio containing @page (with refcount appropriately
247126273f5fSYang Shi * incremented) for success, or NULL upon failure. If neither FOLL_GET
247226273f5fSYang Shi * nor FOLL_PIN was set, that's considered failure, and furthermore,
247326273f5fSYang Shi * a likely bug in the caller, so a warning is also emitted.
247426273f5fSYang Shi *
247526273f5fSYang Shi * It uses add ref unless zero to elevate the folio refcount and must be called
247626273f5fSYang Shi * in fast path only.
247726273f5fSYang Shi */
try_grab_folio_fast(struct page * page,int refs,unsigned int flags)247826273f5fSYang Shi static struct folio *try_grab_folio_fast(struct page *page, int refs,
247926273f5fSYang Shi unsigned int flags)
248026273f5fSYang Shi {
248126273f5fSYang Shi struct folio *folio;
248226273f5fSYang Shi
248326273f5fSYang Shi /* Raise warn if it is not called in fast GUP */
248426273f5fSYang Shi VM_WARN_ON_ONCE(!irqs_disabled());
248526273f5fSYang Shi
248626273f5fSYang Shi if (WARN_ON_ONCE((flags & (FOLL_GET | FOLL_PIN)) == 0))
248726273f5fSYang Shi return NULL;
248826273f5fSYang Shi
248926273f5fSYang Shi if (unlikely(!(flags & FOLL_PCI_P2PDMA) && is_pci_p2pdma_page(page)))
249026273f5fSYang Shi return NULL;
249126273f5fSYang Shi
249226273f5fSYang Shi if (flags & FOLL_GET)
249326273f5fSYang Shi return try_get_folio(page, refs);
249426273f5fSYang Shi
249526273f5fSYang Shi /* FOLL_PIN is set */
249626273f5fSYang Shi
249726273f5fSYang Shi /*
249826273f5fSYang Shi * Don't take a pin on the zero page - it's not going anywhere
249926273f5fSYang Shi * and it is used in a *lot* of places.
250026273f5fSYang Shi */
250126273f5fSYang Shi if (is_zero_page(page))
250226273f5fSYang Shi return page_folio(page);
250326273f5fSYang Shi
250426273f5fSYang Shi folio = try_get_folio(page, refs);
250526273f5fSYang Shi if (!folio)
250626273f5fSYang Shi return NULL;
250726273f5fSYang Shi
250826273f5fSYang Shi /*
250926273f5fSYang Shi * Can't do FOLL_LONGTERM + FOLL_PIN gup fast path if not in a
251026273f5fSYang Shi * right zone, so fail and let the caller fall back to the slow
251126273f5fSYang Shi * path.
251226273f5fSYang Shi */
251326273f5fSYang Shi if (unlikely((flags & FOLL_LONGTERM) &&
251426273f5fSYang Shi !folio_is_longterm_pinnable(folio))) {
251526273f5fSYang Shi if (!put_devmap_managed_page_refs(&folio->page, refs))
251626273f5fSYang Shi folio_put_refs(folio, refs);
251726273f5fSYang Shi return NULL;
251826273f5fSYang Shi }
251926273f5fSYang Shi
252026273f5fSYang Shi /*
252126273f5fSYang Shi * When pinning a large folio, use an exact count to track it.
252226273f5fSYang Shi *
252326273f5fSYang Shi * However, be sure to *also* increment the normal folio
252426273f5fSYang Shi * refcount field at least once, so that the folio really
252526273f5fSYang Shi * is pinned. That's why the refcount from the earlier
252626273f5fSYang Shi * try_get_folio() is left intact.
252726273f5fSYang Shi */
252826273f5fSYang Shi if (folio_test_large(folio))
252926273f5fSYang Shi atomic_add(refs, &folio->_pincount);
253026273f5fSYang Shi else
253126273f5fSYang Shi folio_ref_add(folio,
253226273f5fSYang Shi refs * (GUP_PIN_COUNTING_BIAS - 1));
253326273f5fSYang Shi /*
253426273f5fSYang Shi * Adjust the pincount before re-checking the PTE for changes.
253526273f5fSYang Shi * This is essentially a smp_mb() and is paired with a memory
253626273f5fSYang Shi * barrier in folio_try_share_anon_rmap_*().
253726273f5fSYang Shi */
253826273f5fSYang Shi smp_mb__after_atomic();
253926273f5fSYang Shi
254026273f5fSYang Shi node_stat_mod_folio(folio, NR_FOLL_PIN_ACQUIRED, refs);
254126273f5fSYang Shi
254226273f5fSYang Shi return folio;
254326273f5fSYang Shi }
254426273f5fSYang Shi
25453010a5eaSLaurent Dufour #ifdef CONFIG_ARCH_HAS_PTE_SPECIAL
254670cbc3ccSYang Shi /*
254770cbc3ccSYang Shi * Fast-gup relies on pte change detection to avoid concurrent pgtable
254870cbc3ccSYang Shi * operations.
254970cbc3ccSYang Shi *
255070cbc3ccSYang Shi * To pin the page, fast-gup needs to do below in order:
255170cbc3ccSYang Shi * (1) pin the page (by prefetching pte), then (2) check pte not changed.
255270cbc3ccSYang Shi *
255370cbc3ccSYang Shi * For the rest of pgtable operations where pgtable updates can be racy
255470cbc3ccSYang Shi * with fast-gup, we need to do (1) clear pte, then (2) check whether page
255570cbc3ccSYang Shi * is pinned.
255670cbc3ccSYang Shi *
255770cbc3ccSYang Shi * Above will work for all pte-level operations, including THP split.
255870cbc3ccSYang Shi *
255970cbc3ccSYang Shi * For THP collapse, it's a bit more complicated because fast-gup may be
256070cbc3ccSYang Shi * walking a pgtable page that is being freed (pte is still valid but pmd
256170cbc3ccSYang Shi * can be cleared already). To avoid race in such condition, we need to
256270cbc3ccSYang Shi * also check pmd here to make sure pmd doesn't change (corresponds to
256370cbc3ccSYang Shi * pmdp_collapse_flush() in the THP collapse code path).
256470cbc3ccSYang Shi */
gup_pte_range(pmd_t pmd,pmd_t * pmdp,unsigned long addr,unsigned long end,unsigned int flags,struct page ** pages,int * nr)256570cbc3ccSYang Shi static int gup_pte_range(pmd_t pmd, pmd_t *pmdp, unsigned long addr,
256670cbc3ccSYang Shi unsigned long end, unsigned int flags,
256770cbc3ccSYang Shi struct page **pages, int *nr)
25682667f50eSSteve Capper {
2569b59f65faSKirill A. Shutemov struct dev_pagemap *pgmap = NULL;
2570b59f65faSKirill A. Shutemov int nr_start = *nr, ret = 0;
25712667f50eSSteve Capper pte_t *ptep, *ptem;
25722667f50eSSteve Capper
25732667f50eSSteve Capper ptem = ptep = pte_offset_map(&pmd, addr);
257404dee9e8SHugh Dickins if (!ptep)
257504dee9e8SHugh Dickins return 0;
25762667f50eSSteve Capper do {
25772a4a06daSPeter Zijlstra pte_t pte = ptep_get_lockless(ptep);
2578b0496fe4SMatthew Wilcox (Oracle) struct page *page;
2579b0496fe4SMatthew Wilcox (Oracle) struct folio *folio;
25802667f50eSSteve Capper
2581d74943a2SDavid Hildenbrand /*
2582d74943a2SDavid Hildenbrand * Always fallback to ordinary GUP on PROT_NONE-mapped pages:
2583d74943a2SDavid Hildenbrand * pte_access_permitted() better should reject these pages
2584d74943a2SDavid Hildenbrand * either way: otherwise, GUP-fast might succeed in
2585d74943a2SDavid Hildenbrand * cases where ordinary GUP would fail due to VMA access
2586d74943a2SDavid Hildenbrand * permissions.
2587d74943a2SDavid Hildenbrand */
2588d74943a2SDavid Hildenbrand if (pte_protnone(pte))
2589e7884f8eSKirill A. Shutemov goto pte_unmap;
2590e7884f8eSKirill A. Shutemov
2591b798bec4SIra Weiny if (!pte_access_permitted(pte, flags & FOLL_WRITE))
2592e7884f8eSKirill A. Shutemov goto pte_unmap;
2593e7884f8eSKirill A. Shutemov
2594b59f65faSKirill A. Shutemov if (pte_devmap(pte)) {
25957af75561SIra Weiny if (unlikely(flags & FOLL_LONGTERM))
25967af75561SIra Weiny goto pte_unmap;
25977af75561SIra Weiny
2598b59f65faSKirill A. Shutemov pgmap = get_dev_pagemap(pte_pfn(pte), pgmap);
2599b59f65faSKirill A. Shutemov if (unlikely(!pgmap)) {
26003b78d834SJohn Hubbard undo_dev_pagemap(nr, nr_start, flags, pages);
2601b59f65faSKirill A. Shutemov goto pte_unmap;
2602b59f65faSKirill A. Shutemov }
2603b59f65faSKirill A. Shutemov } else if (pte_special(pte))
26042667f50eSSteve Capper goto pte_unmap;
26052667f50eSSteve Capper
26062667f50eSSteve Capper VM_BUG_ON(!pfn_valid(pte_pfn(pte)));
26072667f50eSSteve Capper page = pte_page(pte);
26082667f50eSSteve Capper
260926273f5fSYang Shi folio = try_grab_folio_fast(page, 1, flags);
2610b0496fe4SMatthew Wilcox (Oracle) if (!folio)
26112667f50eSSteve Capper goto pte_unmap;
26122667f50eSSteve Capper
26138f9ff2deSMatthew Wilcox (Oracle) if (unlikely(folio_is_secretmem(folio))) {
2614b0496fe4SMatthew Wilcox (Oracle) gup_put_folio(folio, 1, flags);
26151507f512SMike Rapoport goto pte_unmap;
26161507f512SMike Rapoport }
26171507f512SMike Rapoport
261870cbc3ccSYang Shi if (unlikely(pmd_val(pmd) != pmd_val(*pmdp)) ||
2619c33c7948SRyan Roberts unlikely(pte_val(pte) != pte_val(ptep_get(ptep)))) {
2620b0496fe4SMatthew Wilcox (Oracle) gup_put_folio(folio, 1, flags);
26212667f50eSSteve Capper goto pte_unmap;
26222667f50eSSteve Capper }
26232667f50eSSteve Capper
2624a6e79df9SLorenzo Stoakes if (!folio_fast_pin_allowed(folio, flags)) {
26252667f50eSSteve Capper gup_put_folio(folio, 1, flags);
26262667f50eSSteve Capper goto pte_unmap;
26272667f50eSSteve Capper }
26282667f50eSSteve Capper
262984209e87SDavid Hildenbrand if (!pte_write(pte) && gup_must_unshare(NULL, flags, page)) {
2630a7f22660SDavid Hildenbrand gup_put_folio(folio, 1, flags);
2631a7f22660SDavid Hildenbrand goto pte_unmap;
2632a7f22660SDavid Hildenbrand }
2633a7f22660SDavid Hildenbrand
2634f28d4363SClaudio Imbrenda /*
2635f28d4363SClaudio Imbrenda * We need to make the page accessible if and only if we are
2636f28d4363SClaudio Imbrenda * going to access its content (the FOLL_PIN case). Please
2637f28d4363SClaudio Imbrenda * see Documentation/core-api/pin_user_pages.rst for
2638f28d4363SClaudio Imbrenda * details.
2639f28d4363SClaudio Imbrenda */
2640f28d4363SClaudio Imbrenda if (flags & FOLL_PIN) {
2641f28d4363SClaudio Imbrenda ret = arch_make_page_accessible(page);
2642f28d4363SClaudio Imbrenda if (ret) {
2643b0496fe4SMatthew Wilcox (Oracle) gup_put_folio(folio, 1, flags);
2644f28d4363SClaudio Imbrenda goto pte_unmap;
2645f28d4363SClaudio Imbrenda }
2646f28d4363SClaudio Imbrenda }
2647b0496fe4SMatthew Wilcox (Oracle) folio_set_referenced(folio);
26482667f50eSSteve Capper pages[*nr] = page;
26492667f50eSSteve Capper (*nr)++;
26502667f50eSSteve Capper } while (ptep++, addr += PAGE_SIZE, addr != end);
26512667f50eSSteve Capper
26522667f50eSSteve Capper ret = 1;
26532667f50eSSteve Capper
26542667f50eSSteve Capper pte_unmap:
2655832d7aa0SChristoph Hellwig if (pgmap)
2656832d7aa0SChristoph Hellwig put_dev_pagemap(pgmap);
26572667f50eSSteve Capper pte_unmap(ptem);
26582667f50eSSteve Capper return ret;
26592667f50eSSteve Capper }
26602667f50eSSteve Capper #else
26612667f50eSSteve Capper
26622667f50eSSteve Capper /*
26632667f50eSSteve Capper * If we can't determine whether or not a pte is special, then fail immediately
26642667f50eSSteve Capper * for ptes. Note, we can still pin HugeTLB and THP as these are guaranteed not
26652667f50eSSteve Capper * to be special.
26662667f50eSSteve Capper *
26672667f50eSSteve Capper * For a futex to be placed on a THP tail page, get_futex_key requires a
2668dadbb612SSouptick Joarder * get_user_pages_fast_only implementation that can pin pages. Thus it's still
26692667f50eSSteve Capper * useful to have gup_huge_pmd even if we can't operate on ptes.
26702667f50eSSteve Capper */
gup_pte_range(pmd_t pmd,pmd_t * pmdp,unsigned long addr,unsigned long end,unsigned int flags,struct page ** pages,int * nr)267170cbc3ccSYang Shi static int gup_pte_range(pmd_t pmd, pmd_t *pmdp, unsigned long addr,
267270cbc3ccSYang Shi unsigned long end, unsigned int flags,
267370cbc3ccSYang Shi struct page **pages, int *nr)
26742667f50eSSteve Capper {
26752667f50eSSteve Capper return 0;
26762667f50eSSteve Capper }
26773010a5eaSLaurent Dufour #endif /* CONFIG_ARCH_HAS_PTE_SPECIAL */
26782667f50eSSteve Capper
267917596731SRobin Murphy #if defined(CONFIG_ARCH_HAS_PTE_DEVMAP) && defined(CONFIG_TRANSPARENT_HUGEPAGE)
__gup_device_huge(unsigned long pfn,unsigned long addr,unsigned long end,unsigned int flags,struct page ** pages,int * nr)2680b59f65faSKirill A. Shutemov static int __gup_device_huge(unsigned long pfn, unsigned long addr,
268186dfbed4SJohn Hubbard unsigned long end, unsigned int flags,
268286dfbed4SJohn Hubbard struct page **pages, int *nr)
2683b59f65faSKirill A. Shutemov {
2684b59f65faSKirill A. Shutemov int nr_start = *nr;
2685b59f65faSKirill A. Shutemov struct dev_pagemap *pgmap = NULL;
2686b59f65faSKirill A. Shutemov
2687b59f65faSKirill A. Shutemov do {
2688b59f65faSKirill A. Shutemov struct page *page = pfn_to_page(pfn);
2689b59f65faSKirill A. Shutemov
2690b59f65faSKirill A. Shutemov pgmap = get_dev_pagemap(pfn, pgmap);
2691b59f65faSKirill A. Shutemov if (unlikely(!pgmap)) {
26923b78d834SJohn Hubbard undo_dev_pagemap(nr, nr_start, flags, pages);
26936401c4ebSMiaohe Lin break;
2694b59f65faSKirill A. Shutemov }
26954003f107SLogan Gunthorpe
26964003f107SLogan Gunthorpe if (!(flags & FOLL_PCI_P2PDMA) && is_pci_p2pdma_page(page)) {
26974003f107SLogan Gunthorpe undo_dev_pagemap(nr, nr_start, flags, pages);
26984003f107SLogan Gunthorpe break;
26994003f107SLogan Gunthorpe }
27004003f107SLogan Gunthorpe
2701b59f65faSKirill A. Shutemov SetPageReferenced(page);
2702b59f65faSKirill A. Shutemov pages[*nr] = page;
270326273f5fSYang Shi if (unlikely(try_grab_folio(page_folio(page), 1, flags))) {
27043faa52c0SJohn Hubbard undo_dev_pagemap(nr, nr_start, flags, pages);
27056401c4ebSMiaohe Lin break;
27063faa52c0SJohn Hubbard }
2707b59f65faSKirill A. Shutemov (*nr)++;
2708b59f65faSKirill A. Shutemov pfn++;
2709b59f65faSKirill A. Shutemov } while (addr += PAGE_SIZE, addr != end);
2710832d7aa0SChristoph Hellwig
2711832d7aa0SChristoph Hellwig put_dev_pagemap(pgmap);
271220b7fee7SJohn Hubbard return addr == end;
2713b59f65faSKirill A. Shutemov }
2714b59f65faSKirill A. Shutemov
__gup_device_huge_pmd(pmd_t orig,pmd_t * pmdp,unsigned long addr,unsigned long end,unsigned int flags,struct page ** pages,int * nr)2715a9b6de77SDan Williams static int __gup_device_huge_pmd(pmd_t orig, pmd_t *pmdp, unsigned long addr,
271686dfbed4SJohn Hubbard unsigned long end, unsigned int flags,
271786dfbed4SJohn Hubbard struct page **pages, int *nr)
2718b59f65faSKirill A. Shutemov {
2719b59f65faSKirill A. Shutemov unsigned long fault_pfn;
2720a9b6de77SDan Williams int nr_start = *nr;
2721b59f65faSKirill A. Shutemov
2722a9b6de77SDan Williams fault_pfn = pmd_pfn(orig) + ((addr & ~PMD_MASK) >> PAGE_SHIFT);
272386dfbed4SJohn Hubbard if (!__gup_device_huge(fault_pfn, addr, end, flags, pages, nr))
2724a9b6de77SDan Williams return 0;
2725a9b6de77SDan Williams
2726a9b6de77SDan Williams if (unlikely(pmd_val(orig) != pmd_val(*pmdp))) {
27273b78d834SJohn Hubbard undo_dev_pagemap(nr, nr_start, flags, pages);
2728a9b6de77SDan Williams return 0;
2729a9b6de77SDan Williams }
2730a9b6de77SDan Williams return 1;
2731b59f65faSKirill A. Shutemov }
2732b59f65faSKirill A. Shutemov
__gup_device_huge_pud(pud_t orig,pud_t * pudp,unsigned long addr,unsigned long end,unsigned int flags,struct page ** pages,int * nr)2733a9b6de77SDan Williams static int __gup_device_huge_pud(pud_t orig, pud_t *pudp, unsigned long addr,
273486dfbed4SJohn Hubbard unsigned long end, unsigned int flags,
273586dfbed4SJohn Hubbard struct page **pages, int *nr)
2736b59f65faSKirill A. Shutemov {
2737b59f65faSKirill A. Shutemov unsigned long fault_pfn;
2738a9b6de77SDan Williams int nr_start = *nr;
2739b59f65faSKirill A. Shutemov
2740a9b6de77SDan Williams fault_pfn = pud_pfn(orig) + ((addr & ~PUD_MASK) >> PAGE_SHIFT);
274186dfbed4SJohn Hubbard if (!__gup_device_huge(fault_pfn, addr, end, flags, pages, nr))
2742a9b6de77SDan Williams return 0;
2743a9b6de77SDan Williams
2744a9b6de77SDan Williams if (unlikely(pud_val(orig) != pud_val(*pudp))) {
27453b78d834SJohn Hubbard undo_dev_pagemap(nr, nr_start, flags, pages);
2746a9b6de77SDan Williams return 0;
2747a9b6de77SDan Williams }
2748a9b6de77SDan Williams return 1;
2749b59f65faSKirill A. Shutemov }
2750b59f65faSKirill A. Shutemov #else
__gup_device_huge_pmd(pmd_t orig,pmd_t * pmdp,unsigned long addr,unsigned long end,unsigned int flags,struct page ** pages,int * nr)2751a9b6de77SDan Williams static int __gup_device_huge_pmd(pmd_t orig, pmd_t *pmdp, unsigned long addr,
275286dfbed4SJohn Hubbard unsigned long end, unsigned int flags,
275386dfbed4SJohn Hubbard struct page **pages, int *nr)
2754b59f65faSKirill A. Shutemov {
2755b59f65faSKirill A. Shutemov BUILD_BUG();
2756b59f65faSKirill A. Shutemov return 0;
2757b59f65faSKirill A. Shutemov }
2758b59f65faSKirill A. Shutemov
__gup_device_huge_pud(pud_t pud,pud_t * pudp,unsigned long addr,unsigned long end,unsigned int flags,struct page ** pages,int * nr)2759a9b6de77SDan Williams static int __gup_device_huge_pud(pud_t pud, pud_t *pudp, unsigned long addr,
276086dfbed4SJohn Hubbard unsigned long end, unsigned int flags,
276186dfbed4SJohn Hubbard struct page **pages, int *nr)
2762b59f65faSKirill A. Shutemov {
2763b59f65faSKirill A. Shutemov BUILD_BUG();
2764b59f65faSKirill A. Shutemov return 0;
2765b59f65faSKirill A. Shutemov }
2766b59f65faSKirill A. Shutemov #endif
2767b59f65faSKirill A. Shutemov
record_subpages(struct page * page,unsigned long addr,unsigned long end,struct page ** pages)2768a43e9820SJohn Hubbard static int record_subpages(struct page *page, unsigned long addr,
2769a43e9820SJohn Hubbard unsigned long end, struct page **pages)
2770a43e9820SJohn Hubbard {
2771a43e9820SJohn Hubbard int nr;
2772a43e9820SJohn Hubbard
2773c228afb1SMatthew Wilcox (Oracle) for (nr = 0; addr != end; nr++, addr += PAGE_SIZE)
2774c228afb1SMatthew Wilcox (Oracle) pages[nr] = nth_page(page, nr);
2775a43e9820SJohn Hubbard
2776a43e9820SJohn Hubbard return nr;
2777a43e9820SJohn Hubbard }
2778a43e9820SJohn Hubbard
2779cbd34da7SChristoph Hellwig #ifdef CONFIG_ARCH_HAS_HUGEPD
hugepte_addr_end(unsigned long addr,unsigned long end,unsigned long sz)2780cbd34da7SChristoph Hellwig static unsigned long hugepte_addr_end(unsigned long addr, unsigned long end,
2781cbd34da7SChristoph Hellwig unsigned long sz)
2782cbd34da7SChristoph Hellwig {
2783cbd34da7SChristoph Hellwig unsigned long __boundary = (addr + sz) & ~(sz-1);
2784cbd34da7SChristoph Hellwig return (__boundary - 1 < end - 1) ? __boundary : end;
2785cbd34da7SChristoph Hellwig }
2786cbd34da7SChristoph Hellwig
gup_hugepte(pte_t * ptep,unsigned long sz,unsigned long addr,unsigned long end,unsigned int flags,struct page ** pages,int * nr)2787cbd34da7SChristoph Hellwig static int gup_hugepte(pte_t *ptep, unsigned long sz, unsigned long addr,
27880cd22afdSJohn Hubbard unsigned long end, unsigned int flags,
27890cd22afdSJohn Hubbard struct page **pages, int *nr)
2790cbd34da7SChristoph Hellwig {
2791cbd34da7SChristoph Hellwig unsigned long pte_end;
279209a1626eSMatthew Wilcox (Oracle) struct page *page;
279309a1626eSMatthew Wilcox (Oracle) struct folio *folio;
2794cbd34da7SChristoph Hellwig pte_t pte;
2795cbd34da7SChristoph Hellwig int refs;
2796cbd34da7SChristoph Hellwig
2797cbd34da7SChristoph Hellwig pte_end = (addr + sz) & ~(sz-1);
2798cbd34da7SChristoph Hellwig if (pte_end < end)
2799cbd34da7SChristoph Hellwig end = pte_end;
2800cbd34da7SChristoph Hellwig
280155ca2263SChristophe Leroy pte = huge_ptep_get(ptep);
2802cbd34da7SChristoph Hellwig
28030cd22afdSJohn Hubbard if (!pte_access_permitted(pte, flags & FOLL_WRITE))
2804cbd34da7SChristoph Hellwig return 0;
2805cbd34da7SChristoph Hellwig
2806cbd34da7SChristoph Hellwig /* hugepages are never "special" */
2807cbd34da7SChristoph Hellwig VM_BUG_ON(!pfn_valid(pte_pfn(pte)));
2808cbd34da7SChristoph Hellwig
280909a1626eSMatthew Wilcox (Oracle) page = nth_page(pte_page(pte), (addr & (sz - 1)) >> PAGE_SHIFT);
2810a43e9820SJohn Hubbard refs = record_subpages(page, addr, end, pages + *nr);
2811cbd34da7SChristoph Hellwig
281226273f5fSYang Shi folio = try_grab_folio_fast(page, refs, flags);
281309a1626eSMatthew Wilcox (Oracle) if (!folio)
2814cbd34da7SChristoph Hellwig return 0;
2815cbd34da7SChristoph Hellwig
2816c33c7948SRyan Roberts if (unlikely(pte_val(pte) != pte_val(ptep_get(ptep)))) {
281709a1626eSMatthew Wilcox (Oracle) gup_put_folio(folio, refs, flags);
2818cbd34da7SChristoph Hellwig return 0;
2819cbd34da7SChristoph Hellwig }
2820cbd34da7SChristoph Hellwig
2821a6e79df9SLorenzo Stoakes if (!folio_fast_pin_allowed(folio, flags)) {
2822cbd34da7SChristoph Hellwig gup_put_folio(folio, refs, flags);
2823cbd34da7SChristoph Hellwig return 0;
2824cbd34da7SChristoph Hellwig }
2825cbd34da7SChristoph Hellwig
282684209e87SDavid Hildenbrand if (!pte_write(pte) && gup_must_unshare(NULL, flags, &folio->page)) {
2827a7f22660SDavid Hildenbrand gup_put_folio(folio, refs, flags);
2828a7f22660SDavid Hildenbrand return 0;
2829a7f22660SDavid Hildenbrand }
2830a7f22660SDavid Hildenbrand
2831a43e9820SJohn Hubbard *nr += refs;
283209a1626eSMatthew Wilcox (Oracle) folio_set_referenced(folio);
2833cbd34da7SChristoph Hellwig return 1;
2834cbd34da7SChristoph Hellwig }
2835cbd34da7SChristoph Hellwig
gup_huge_pd(hugepd_t hugepd,unsigned long addr,unsigned int pdshift,unsigned long end,unsigned int flags,struct page ** pages,int * nr)2836cbd34da7SChristoph Hellwig static int gup_huge_pd(hugepd_t hugepd, unsigned long addr,
28370cd22afdSJohn Hubbard unsigned int pdshift, unsigned long end, unsigned int flags,
2838cbd34da7SChristoph Hellwig struct page **pages, int *nr)
2839cbd34da7SChristoph Hellwig {
2840cbd34da7SChristoph Hellwig pte_t *ptep;
2841cbd34da7SChristoph Hellwig unsigned long sz = 1UL << hugepd_shift(hugepd);
2842cbd34da7SChristoph Hellwig unsigned long next;
2843cbd34da7SChristoph Hellwig
2844cbd34da7SChristoph Hellwig ptep = hugepte_offset(hugepd, addr, pdshift);
2845cbd34da7SChristoph Hellwig do {
2846cbd34da7SChristoph Hellwig next = hugepte_addr_end(addr, end, sz);
28470cd22afdSJohn Hubbard if (!gup_hugepte(ptep, sz, addr, end, flags, pages, nr))
2848cbd34da7SChristoph Hellwig return 0;
2849cbd34da7SChristoph Hellwig } while (ptep++, addr = next, addr != end);
2850cbd34da7SChristoph Hellwig
2851cbd34da7SChristoph Hellwig return 1;
2852cbd34da7SChristoph Hellwig }
2853cbd34da7SChristoph Hellwig #else
gup_huge_pd(hugepd_t hugepd,unsigned long addr,unsigned int pdshift,unsigned long end,unsigned int flags,struct page ** pages,int * nr)2854cbd34da7SChristoph Hellwig static inline int gup_huge_pd(hugepd_t hugepd, unsigned long addr,
28550cd22afdSJohn Hubbard unsigned int pdshift, unsigned long end, unsigned int flags,
2856cbd34da7SChristoph Hellwig struct page **pages, int *nr)
2857cbd34da7SChristoph Hellwig {
2858cbd34da7SChristoph Hellwig return 0;
2859cbd34da7SChristoph Hellwig }
2860cbd34da7SChristoph Hellwig #endif /* CONFIG_ARCH_HAS_HUGEPD */
2861cbd34da7SChristoph Hellwig
gup_huge_pmd(pmd_t orig,pmd_t * pmdp,unsigned long addr,unsigned long end,unsigned int flags,struct page ** pages,int * nr)28622667f50eSSteve Capper static int gup_huge_pmd(pmd_t orig, pmd_t *pmdp, unsigned long addr,
28630cd22afdSJohn Hubbard unsigned long end, unsigned int flags,
28640cd22afdSJohn Hubbard struct page **pages, int *nr)
28652667f50eSSteve Capper {
2866667ed1f7SMatthew Wilcox (Oracle) struct page *page;
2867667ed1f7SMatthew Wilcox (Oracle) struct folio *folio;
28682667f50eSSteve Capper int refs;
28692667f50eSSteve Capper
2870b798bec4SIra Weiny if (!pmd_access_permitted(orig, flags & FOLL_WRITE))
28712667f50eSSteve Capper return 0;
28722667f50eSSteve Capper
28737af75561SIra Weiny if (pmd_devmap(orig)) {
28747af75561SIra Weiny if (unlikely(flags & FOLL_LONGTERM))
28757af75561SIra Weiny return 0;
287686dfbed4SJohn Hubbard return __gup_device_huge_pmd(orig, pmdp, addr, end, flags,
287786dfbed4SJohn Hubbard pages, nr);
28787af75561SIra Weiny }
2879b59f65faSKirill A. Shutemov
2880c228afb1SMatthew Wilcox (Oracle) page = nth_page(pmd_page(orig), (addr & ~PMD_MASK) >> PAGE_SHIFT);
2881a43e9820SJohn Hubbard refs = record_subpages(page, addr, end, pages + *nr);
28822667f50eSSteve Capper
288326273f5fSYang Shi folio = try_grab_folio_fast(page, refs, flags);
2884667ed1f7SMatthew Wilcox (Oracle) if (!folio)
28852667f50eSSteve Capper return 0;
28862667f50eSSteve Capper
28872667f50eSSteve Capper if (unlikely(pmd_val(orig) != pmd_val(*pmdp))) {
2888667ed1f7SMatthew Wilcox (Oracle) gup_put_folio(folio, refs, flags);
28892667f50eSSteve Capper return 0;
28902667f50eSSteve Capper }
28912667f50eSSteve Capper
2892a6e79df9SLorenzo Stoakes if (!folio_fast_pin_allowed(folio, flags)) {
2893a6e79df9SLorenzo Stoakes gup_put_folio(folio, refs, flags);
2894a6e79df9SLorenzo Stoakes return 0;
2895a6e79df9SLorenzo Stoakes }
289684209e87SDavid Hildenbrand if (!pmd_write(orig) && gup_must_unshare(NULL, flags, &folio->page)) {
2897a7f22660SDavid Hildenbrand gup_put_folio(folio, refs, flags);
2898a7f22660SDavid Hildenbrand return 0;
2899a7f22660SDavid Hildenbrand }
2900a7f22660SDavid Hildenbrand
2901a43e9820SJohn Hubbard *nr += refs;
2902667ed1f7SMatthew Wilcox (Oracle) folio_set_referenced(folio);
29032667f50eSSteve Capper return 1;
29042667f50eSSteve Capper }
29052667f50eSSteve Capper
gup_huge_pud(pud_t orig,pud_t * pudp,unsigned long addr,unsigned long end,unsigned int flags,struct page ** pages,int * nr)29062667f50eSSteve Capper static int gup_huge_pud(pud_t orig, pud_t *pudp, unsigned long addr,
290786dfbed4SJohn Hubbard unsigned long end, unsigned int flags,
290886dfbed4SJohn Hubbard struct page **pages, int *nr)
29092667f50eSSteve Capper {
291083afb52eSMatthew Wilcox (Oracle) struct page *page;
291183afb52eSMatthew Wilcox (Oracle) struct folio *folio;
29122667f50eSSteve Capper int refs;
29132667f50eSSteve Capper
2914b798bec4SIra Weiny if (!pud_access_permitted(orig, flags & FOLL_WRITE))
29152667f50eSSteve Capper return 0;
29162667f50eSSteve Capper
29177af75561SIra Weiny if (pud_devmap(orig)) {
29187af75561SIra Weiny if (unlikely(flags & FOLL_LONGTERM))
29197af75561SIra Weiny return 0;
292086dfbed4SJohn Hubbard return __gup_device_huge_pud(orig, pudp, addr, end, flags,
292186dfbed4SJohn Hubbard pages, nr);
29227af75561SIra Weiny }
2923b59f65faSKirill A. Shutemov
2924c228afb1SMatthew Wilcox (Oracle) page = nth_page(pud_page(orig), (addr & ~PUD_MASK) >> PAGE_SHIFT);
2925a43e9820SJohn Hubbard refs = record_subpages(page, addr, end, pages + *nr);
29262667f50eSSteve Capper
292726273f5fSYang Shi folio = try_grab_folio_fast(page, refs, flags);
292883afb52eSMatthew Wilcox (Oracle) if (!folio)
29292667f50eSSteve Capper return 0;
29302667f50eSSteve Capper
29312667f50eSSteve Capper if (unlikely(pud_val(orig) != pud_val(*pudp))) {
293283afb52eSMatthew Wilcox (Oracle) gup_put_folio(folio, refs, flags);
29332667f50eSSteve Capper return 0;
29342667f50eSSteve Capper }
29352667f50eSSteve Capper
2936a6e79df9SLorenzo Stoakes if (!folio_fast_pin_allowed(folio, flags)) {
2937a6e79df9SLorenzo Stoakes gup_put_folio(folio, refs, flags);
2938a6e79df9SLorenzo Stoakes return 0;
2939a6e79df9SLorenzo Stoakes }
2940a6e79df9SLorenzo Stoakes
294184209e87SDavid Hildenbrand if (!pud_write(orig) && gup_must_unshare(NULL, flags, &folio->page)) {
2942a7f22660SDavid Hildenbrand gup_put_folio(folio, refs, flags);
2943a7f22660SDavid Hildenbrand return 0;
2944a7f22660SDavid Hildenbrand }
2945a7f22660SDavid Hildenbrand
2946a43e9820SJohn Hubbard *nr += refs;
294783afb52eSMatthew Wilcox (Oracle) folio_set_referenced(folio);
29482667f50eSSteve Capper return 1;
29492667f50eSSteve Capper }
29502667f50eSSteve Capper
gup_huge_pgd(pgd_t orig,pgd_t * pgdp,unsigned long addr,unsigned long end,unsigned int flags,struct page ** pages,int * nr)2951f30c59e9SAneesh Kumar K.V static int gup_huge_pgd(pgd_t orig, pgd_t *pgdp, unsigned long addr,
2952b798bec4SIra Weiny unsigned long end, unsigned int flags,
2953f30c59e9SAneesh Kumar K.V struct page **pages, int *nr)
2954f30c59e9SAneesh Kumar K.V {
2955f30c59e9SAneesh Kumar K.V int refs;
29562d7919a2SMatthew Wilcox (Oracle) struct page *page;
29572d7919a2SMatthew Wilcox (Oracle) struct folio *folio;
2958f30c59e9SAneesh Kumar K.V
2959b798bec4SIra Weiny if (!pgd_access_permitted(orig, flags & FOLL_WRITE))
2960f30c59e9SAneesh Kumar K.V return 0;
2961f30c59e9SAneesh Kumar K.V
2962b59f65faSKirill A. Shutemov BUILD_BUG_ON(pgd_devmap(orig));
2963a43e9820SJohn Hubbard
2964c228afb1SMatthew Wilcox (Oracle) page = nth_page(pgd_page(orig), (addr & ~PGDIR_MASK) >> PAGE_SHIFT);
2965a43e9820SJohn Hubbard refs = record_subpages(page, addr, end, pages + *nr);
2966f30c59e9SAneesh Kumar K.V
296726273f5fSYang Shi folio = try_grab_folio_fast(page, refs, flags);
29682d7919a2SMatthew Wilcox (Oracle) if (!folio)
2969f30c59e9SAneesh Kumar K.V return 0;
2970f30c59e9SAneesh Kumar K.V
2971f30c59e9SAneesh Kumar K.V if (unlikely(pgd_val(orig) != pgd_val(*pgdp))) {
29722d7919a2SMatthew Wilcox (Oracle) gup_put_folio(folio, refs, flags);
2973f30c59e9SAneesh Kumar K.V return 0;
2974f30c59e9SAneesh Kumar K.V }
2975f30c59e9SAneesh Kumar K.V
297631115034SLorenzo Stoakes if (!pgd_write(orig) && gup_must_unshare(NULL, flags, &folio->page)) {
297731115034SLorenzo Stoakes gup_put_folio(folio, refs, flags);
297831115034SLorenzo Stoakes return 0;
297931115034SLorenzo Stoakes }
298031115034SLorenzo Stoakes
2981a6e79df9SLorenzo Stoakes if (!folio_fast_pin_allowed(folio, flags)) {
2982a6e79df9SLorenzo Stoakes gup_put_folio(folio, refs, flags);
2983a6e79df9SLorenzo Stoakes return 0;
2984a6e79df9SLorenzo Stoakes }
2985a6e79df9SLorenzo Stoakes
2986a43e9820SJohn Hubbard *nr += refs;
29872d7919a2SMatthew Wilcox (Oracle) folio_set_referenced(folio);
2988f30c59e9SAneesh Kumar K.V return 1;
2989f30c59e9SAneesh Kumar K.V }
2990f30c59e9SAneesh Kumar K.V
gup_pmd_range(pud_t * pudp,pud_t pud,unsigned long addr,unsigned long end,unsigned int flags,struct page ** pages,int * nr)2991d3f7b1bbSVasily Gorbik static int gup_pmd_range(pud_t *pudp, pud_t pud, unsigned long addr, unsigned long end,
2992b798bec4SIra Weiny unsigned int flags, struct page **pages, int *nr)
29932667f50eSSteve Capper {
29942667f50eSSteve Capper unsigned long next;
29952667f50eSSteve Capper pmd_t *pmdp;
29962667f50eSSteve Capper
2997d3f7b1bbSVasily Gorbik pmdp = pmd_offset_lockless(pudp, pud, addr);
29982667f50eSSteve Capper do {
29991180e732SPeter Zijlstra pmd_t pmd = pmdp_get_lockless(pmdp);
30002667f50eSSteve Capper
30012667f50eSSteve Capper next = pmd_addr_end(addr, end);
300284c3fc4eSZi Yan if (!pmd_present(pmd))
30032667f50eSSteve Capper return 0;
30042667f50eSSteve Capper
3005414fd080SYu Zhao if (unlikely(pmd_trans_huge(pmd) || pmd_huge(pmd) ||
3006414fd080SYu Zhao pmd_devmap(pmd))) {
3007d74943a2SDavid Hildenbrand /* See gup_pte_range() */
3008d74943a2SDavid Hildenbrand if (pmd_protnone(pmd))
30092667f50eSSteve Capper return 0;
30102667f50eSSteve Capper
3011b798bec4SIra Weiny if (!gup_huge_pmd(pmd, pmdp, addr, next, flags,
30122667f50eSSteve Capper pages, nr))
30132667f50eSSteve Capper return 0;
30142667f50eSSteve Capper
3015f30c59e9SAneesh Kumar K.V } else if (unlikely(is_hugepd(__hugepd(pmd_val(pmd))))) {
3016f30c59e9SAneesh Kumar K.V /*
3017f30c59e9SAneesh Kumar K.V * architecture have different format for hugetlbfs
3018f30c59e9SAneesh Kumar K.V * pmd format and THP pmd format
3019f30c59e9SAneesh Kumar K.V */
3020f30c59e9SAneesh Kumar K.V if (!gup_huge_pd(__hugepd(pmd_val(pmd)), addr,
3021b798bec4SIra Weiny PMD_SHIFT, next, flags, pages, nr))
3022f30c59e9SAneesh Kumar K.V return 0;
302370cbc3ccSYang Shi } else if (!gup_pte_range(pmd, pmdp, addr, next, flags, pages, nr))
30242667f50eSSteve Capper return 0;
30252667f50eSSteve Capper } while (pmdp++, addr = next, addr != end);
30262667f50eSSteve Capper
30272667f50eSSteve Capper return 1;
30282667f50eSSteve Capper }
30292667f50eSSteve Capper
gup_pud_range(p4d_t * p4dp,p4d_t p4d,unsigned long addr,unsigned long end,unsigned int flags,struct page ** pages,int * nr)3030d3f7b1bbSVasily Gorbik static int gup_pud_range(p4d_t *p4dp, p4d_t p4d, unsigned long addr, unsigned long end,
3031b798bec4SIra Weiny unsigned int flags, struct page **pages, int *nr)
30322667f50eSSteve Capper {
30332667f50eSSteve Capper unsigned long next;
30342667f50eSSteve Capper pud_t *pudp;
30352667f50eSSteve Capper
3036d3f7b1bbSVasily Gorbik pudp = pud_offset_lockless(p4dp, p4d, addr);
30372667f50eSSteve Capper do {
3038e37c6982SChristian Borntraeger pud_t pud = READ_ONCE(*pudp);
30392667f50eSSteve Capper
30402667f50eSSteve Capper next = pud_addr_end(addr, end);
304115494520SQiujun Huang if (unlikely(!pud_present(pud)))
30422667f50eSSteve Capper return 0;
3043fcd0ccd8SJohn Starks if (unlikely(pud_huge(pud) || pud_devmap(pud))) {
3044b798bec4SIra Weiny if (!gup_huge_pud(pud, pudp, addr, next, flags,
30452667f50eSSteve Capper pages, nr))
30462667f50eSSteve Capper return 0;
3047f30c59e9SAneesh Kumar K.V } else if (unlikely(is_hugepd(__hugepd(pud_val(pud))))) {
3048f30c59e9SAneesh Kumar K.V if (!gup_huge_pd(__hugepd(pud_val(pud)), addr,
3049b798bec4SIra Weiny PUD_SHIFT, next, flags, pages, nr))
3050f30c59e9SAneesh Kumar K.V return 0;
3051d3f7b1bbSVasily Gorbik } else if (!gup_pmd_range(pudp, pud, addr, next, flags, pages, nr))
30522667f50eSSteve Capper return 0;
30532667f50eSSteve Capper } while (pudp++, addr = next, addr != end);
30542667f50eSSteve Capper
30552667f50eSSteve Capper return 1;
30562667f50eSSteve Capper }
30572667f50eSSteve Capper
gup_p4d_range(pgd_t * pgdp,pgd_t pgd,unsigned long addr,unsigned long end,unsigned int flags,struct page ** pages,int * nr)3058d3f7b1bbSVasily Gorbik static int gup_p4d_range(pgd_t *pgdp, pgd_t pgd, unsigned long addr, unsigned long end,
3059b798bec4SIra Weiny unsigned int flags, struct page **pages, int *nr)
3060c2febafcSKirill A. Shutemov {
3061c2febafcSKirill A. Shutemov unsigned long next;
3062c2febafcSKirill A. Shutemov p4d_t *p4dp;
3063c2febafcSKirill A. Shutemov
3064d3f7b1bbSVasily Gorbik p4dp = p4d_offset_lockless(pgdp, pgd, addr);
3065c2febafcSKirill A. Shutemov do {
3066c2febafcSKirill A. Shutemov p4d_t p4d = READ_ONCE(*p4dp);
3067c2febafcSKirill A. Shutemov
3068c2febafcSKirill A. Shutemov next = p4d_addr_end(addr, end);
3069c2febafcSKirill A. Shutemov if (p4d_none(p4d))
3070c2febafcSKirill A. Shutemov return 0;
3071c2febafcSKirill A. Shutemov BUILD_BUG_ON(p4d_huge(p4d));
3072c2febafcSKirill A. Shutemov if (unlikely(is_hugepd(__hugepd(p4d_val(p4d))))) {
3073c2febafcSKirill A. Shutemov if (!gup_huge_pd(__hugepd(p4d_val(p4d)), addr,
3074b798bec4SIra Weiny P4D_SHIFT, next, flags, pages, nr))
3075c2febafcSKirill A. Shutemov return 0;
3076d3f7b1bbSVasily Gorbik } else if (!gup_pud_range(p4dp, p4d, addr, next, flags, pages, nr))
3077c2febafcSKirill A. Shutemov return 0;
3078c2febafcSKirill A. Shutemov } while (p4dp++, addr = next, addr != end);
3079c2febafcSKirill A. Shutemov
3080c2febafcSKirill A. Shutemov return 1;
3081c2febafcSKirill A. Shutemov }
3082c2febafcSKirill A. Shutemov
gup_pgd_range(unsigned long addr,unsigned long end,unsigned int flags,struct page ** pages,int * nr)30835b65c467SKirill A. Shutemov static void gup_pgd_range(unsigned long addr, unsigned long end,
3084b798bec4SIra Weiny unsigned int flags, struct page **pages, int *nr)
30855b65c467SKirill A. Shutemov {
30865b65c467SKirill A. Shutemov unsigned long next;
30875b65c467SKirill A. Shutemov pgd_t *pgdp;
30885b65c467SKirill A. Shutemov
30895b65c467SKirill A. Shutemov pgdp = pgd_offset(current->mm, addr);
30905b65c467SKirill A. Shutemov do {
30915b65c467SKirill A. Shutemov pgd_t pgd = READ_ONCE(*pgdp);
30925b65c467SKirill A. Shutemov
30935b65c467SKirill A. Shutemov next = pgd_addr_end(addr, end);
30945b65c467SKirill A. Shutemov if (pgd_none(pgd))
30955b65c467SKirill A. Shutemov return;
30965b65c467SKirill A. Shutemov if (unlikely(pgd_huge(pgd))) {
3097b798bec4SIra Weiny if (!gup_huge_pgd(pgd, pgdp, addr, next, flags,
30985b65c467SKirill A. Shutemov pages, nr))
30995b65c467SKirill A. Shutemov return;
31005b65c467SKirill A. Shutemov } else if (unlikely(is_hugepd(__hugepd(pgd_val(pgd))))) {
31015b65c467SKirill A. Shutemov if (!gup_huge_pd(__hugepd(pgd_val(pgd)), addr,
3102b798bec4SIra Weiny PGDIR_SHIFT, next, flags, pages, nr))
31035b65c467SKirill A. Shutemov return;
3104d3f7b1bbSVasily Gorbik } else if (!gup_p4d_range(pgdp, pgd, addr, next, flags, pages, nr))
31055b65c467SKirill A. Shutemov return;
31065b65c467SKirill A. Shutemov } while (pgdp++, addr = next, addr != end);
31075b65c467SKirill A. Shutemov }
3108050a9adcSChristoph Hellwig #else
gup_pgd_range(unsigned long addr,unsigned long end,unsigned int flags,struct page ** pages,int * nr)3109050a9adcSChristoph Hellwig static inline void gup_pgd_range(unsigned long addr, unsigned long end,
3110050a9adcSChristoph Hellwig unsigned int flags, struct page **pages, int *nr)
3111050a9adcSChristoph Hellwig {
3112050a9adcSChristoph Hellwig }
3113050a9adcSChristoph Hellwig #endif /* CONFIG_HAVE_FAST_GUP */
31145b65c467SKirill A. Shutemov
31155b65c467SKirill A. Shutemov #ifndef gup_fast_permitted
31165b65c467SKirill A. Shutemov /*
3117dadbb612SSouptick Joarder * Check if it's allowed to use get_user_pages_fast_only() for the range, or
31185b65c467SKirill A. Shutemov * we need to fall back to the slow version:
31195b65c467SKirill A. Shutemov */
gup_fast_permitted(unsigned long start,unsigned long end)312026f4c328SChristoph Hellwig static bool gup_fast_permitted(unsigned long start, unsigned long end)
31215b65c467SKirill A. Shutemov {
312226f4c328SChristoph Hellwig return true;
31235b65c467SKirill A. Shutemov }
31245b65c467SKirill A. Shutemov #endif
31255b65c467SKirill A. Shutemov
lockless_pages_from_mm(unsigned long start,unsigned long end,unsigned int gup_flags,struct page ** pages)3126c28b1fc7SJason Gunthorpe static unsigned long lockless_pages_from_mm(unsigned long start,
3127c28b1fc7SJason Gunthorpe unsigned long end,
3128eddb1c22SJohn Hubbard unsigned int gup_flags,
3129eddb1c22SJohn Hubbard struct page **pages)
31302667f50eSSteve Capper {
3131376a34efSJohn Hubbard unsigned long flags;
3132c28b1fc7SJason Gunthorpe int nr_pinned = 0;
313357efa1feSJason Gunthorpe unsigned seq;
3134c28b1fc7SJason Gunthorpe
3135c28b1fc7SJason Gunthorpe if (!IS_ENABLED(CONFIG_HAVE_FAST_GUP) ||
3136c28b1fc7SJason Gunthorpe !gup_fast_permitted(start, end))
3137c28b1fc7SJason Gunthorpe return 0;
3138c28b1fc7SJason Gunthorpe
313957efa1feSJason Gunthorpe if (gup_flags & FOLL_PIN) {
314057efa1feSJason Gunthorpe seq = raw_read_seqcount(¤t->mm->write_protect_seq);
314157efa1feSJason Gunthorpe if (seq & 1)
314257efa1feSJason Gunthorpe return 0;
314357efa1feSJason Gunthorpe }
314457efa1feSJason Gunthorpe
3145c28b1fc7SJason Gunthorpe /*
3146c28b1fc7SJason Gunthorpe * Disable interrupts. The nested form is used, in order to allow full,
3147c28b1fc7SJason Gunthorpe * general purpose use of this routine.
3148c28b1fc7SJason Gunthorpe *
3149c28b1fc7SJason Gunthorpe * With interrupts disabled, we block page table pages from being freed
3150c28b1fc7SJason Gunthorpe * from under us. See struct mmu_table_batch comments in
3151c28b1fc7SJason Gunthorpe * include/asm-generic/tlb.h for more details.
3152c28b1fc7SJason Gunthorpe *
3153c28b1fc7SJason Gunthorpe * We do not adopt an rcu_read_lock() here as we also want to block IPIs
3154c28b1fc7SJason Gunthorpe * that come from THPs splitting.
3155c28b1fc7SJason Gunthorpe */
3156c28b1fc7SJason Gunthorpe local_irq_save(flags);
3157c28b1fc7SJason Gunthorpe gup_pgd_range(start, end, gup_flags, pages, &nr_pinned);
3158c28b1fc7SJason Gunthorpe local_irq_restore(flags);
315957efa1feSJason Gunthorpe
316057efa1feSJason Gunthorpe /*
316157efa1feSJason Gunthorpe * When pinning pages for DMA there could be a concurrent write protect
316257efa1feSJason Gunthorpe * from fork() via copy_page_range(), in this case always fail fast GUP.
316357efa1feSJason Gunthorpe */
316457efa1feSJason Gunthorpe if (gup_flags & FOLL_PIN) {
316557efa1feSJason Gunthorpe if (read_seqcount_retry(¤t->mm->write_protect_seq, seq)) {
3166b6a2619cSDavid Hildenbrand unpin_user_pages_lockless(pages, nr_pinned);
316757efa1feSJason Gunthorpe return 0;
3168b6a2619cSDavid Hildenbrand } else {
3169b6a2619cSDavid Hildenbrand sanity_check_pinned_pages(pages, nr_pinned);
317057efa1feSJason Gunthorpe }
317157efa1feSJason Gunthorpe }
3172c28b1fc7SJason Gunthorpe return nr_pinned;
3173c28b1fc7SJason Gunthorpe }
3174c28b1fc7SJason Gunthorpe
internal_get_user_pages_fast(unsigned long start,unsigned long nr_pages,unsigned int gup_flags,struct page ** pages)3175c28b1fc7SJason Gunthorpe static int internal_get_user_pages_fast(unsigned long start,
3176c28b1fc7SJason Gunthorpe unsigned long nr_pages,
3177c28b1fc7SJason Gunthorpe unsigned int gup_flags,
3178c28b1fc7SJason Gunthorpe struct page **pages)
3179c28b1fc7SJason Gunthorpe {
3180c28b1fc7SJason Gunthorpe unsigned long len, end;
3181c28b1fc7SJason Gunthorpe unsigned long nr_pinned;
3182b2a72dffSJason Gunthorpe int locked = 0;
3183c28b1fc7SJason Gunthorpe int ret;
31842667f50eSSteve Capper
3185f4000fdfSJohn Hubbard if (WARN_ON_ONCE(gup_flags & ~(FOLL_WRITE | FOLL_LONGTERM |
3186376a34efSJohn Hubbard FOLL_FORCE | FOLL_PIN | FOLL_GET |
31874003f107SLogan Gunthorpe FOLL_FAST_ONLY | FOLL_NOFAULT |
3188d74943a2SDavid Hildenbrand FOLL_PCI_P2PDMA | FOLL_HONOR_NUMA_FAULT)))
3189817be129SChristoph Hellwig return -EINVAL;
3190817be129SChristoph Hellwig
3191a458b76aSAndrea Arcangeli if (gup_flags & FOLL_PIN)
3192a458b76aSAndrea Arcangeli mm_set_has_pinned_flag(¤t->mm->flags);
3193008cfe44SPeter Xu
3194f81cd178SJohn Hubbard if (!(gup_flags & FOLL_FAST_ONLY))
3195da1c55f1SMichel Lespinasse might_lock_read(¤t->mm->mmap_lock);
3196f81cd178SJohn Hubbard
3197f455c854SChristoph Hellwig start = untagged_addr(start) & PAGE_MASK;
3198c28b1fc7SJason Gunthorpe len = nr_pages << PAGE_SHIFT;
3199c28b1fc7SJason Gunthorpe if (check_add_overflow(start, len, &end))
32009883c7f8SJason Gunthorpe return -EOVERFLOW;
32016014bc27SLinus Torvalds if (end > TASK_SIZE_MAX)
32026014bc27SLinus Torvalds return -EFAULT;
320396d4f267SLinus Torvalds if (unlikely(!access_ok((void __user *)start, len)))
3204c61611f7SMichael S. Tsirkin return -EFAULT;
320573e10a61SKirill A. Shutemov
3206c28b1fc7SJason Gunthorpe nr_pinned = lockless_pages_from_mm(start, end, gup_flags, pages);
3207c28b1fc7SJason Gunthorpe if (nr_pinned == nr_pages || gup_flags & FOLL_FAST_ONLY)
3208c28b1fc7SJason Gunthorpe return nr_pinned;
3209376a34efSJohn Hubbard
3210c28b1fc7SJason Gunthorpe /* Slow path: try to get the remaining pages with get_user_pages */
32114628b063SPingfan Liu start += nr_pinned << PAGE_SHIFT;
32124628b063SPingfan Liu pages += nr_pinned;
3213b2a72dffSJason Gunthorpe ret = __gup_longterm_locked(current->mm, start, nr_pages - nr_pinned,
3214b2cac248SLorenzo Stoakes pages, &locked,
3215f04740f5SJason Gunthorpe gup_flags | FOLL_TOUCH | FOLL_UNLOCKABLE);
3216c28b1fc7SJason Gunthorpe if (ret < 0) {
3217c28b1fc7SJason Gunthorpe /*
3218c28b1fc7SJason Gunthorpe * The caller has to unpin the pages we already pinned so
3219c28b1fc7SJason Gunthorpe * returning -errno is not an option
3220c28b1fc7SJason Gunthorpe */
3221c28b1fc7SJason Gunthorpe if (nr_pinned)
3222c28b1fc7SJason Gunthorpe return nr_pinned;
32232667f50eSSteve Capper return ret;
32242667f50eSSteve Capper }
3225c28b1fc7SJason Gunthorpe return ret + nr_pinned;
3226c28b1fc7SJason Gunthorpe }
3227c28b1fc7SJason Gunthorpe
3228dadbb612SSouptick Joarder /**
3229dadbb612SSouptick Joarder * get_user_pages_fast_only() - pin user pages in memory
3230dadbb612SSouptick Joarder * @start: starting user address
3231dadbb612SSouptick Joarder * @nr_pages: number of pages from start to pin
3232dadbb612SSouptick Joarder * @gup_flags: flags modifying pin behaviour
3233dadbb612SSouptick Joarder * @pages: array that receives pointers to the pages pinned.
3234dadbb612SSouptick Joarder * Should be at least nr_pages long.
3235dadbb612SSouptick Joarder *
32369e1f0580SJohn Hubbard * Like get_user_pages_fast() except it's IRQ-safe in that it won't fall back to
32379e1f0580SJohn Hubbard * the regular GUP.
32389e1f0580SJohn Hubbard *
32399e1f0580SJohn Hubbard * If the architecture does not support this function, simply return with no
32409e1f0580SJohn Hubbard * pages pinned.
32419e1f0580SJohn Hubbard *
32429e1f0580SJohn Hubbard * Careful, careful! COW breaking can go either way, so a non-write
32439e1f0580SJohn Hubbard * access can get ambiguous page results. If you call this function without
32449e1f0580SJohn Hubbard * 'write' set, you'd better be sure that you're ok with that ambiguity.
32459e1f0580SJohn Hubbard */
get_user_pages_fast_only(unsigned long start,int nr_pages,unsigned int gup_flags,struct page ** pages)3246dadbb612SSouptick Joarder int get_user_pages_fast_only(unsigned long start, int nr_pages,
3247dadbb612SSouptick Joarder unsigned int gup_flags, struct page **pages)
32489e1f0580SJohn Hubbard {
32499e1f0580SJohn Hubbard /*
32509e1f0580SJohn Hubbard * Internally (within mm/gup.c), gup fast variants must set FOLL_GET,
32519e1f0580SJohn Hubbard * because gup fast is always a "pin with a +1 page refcount" request.
3252376a34efSJohn Hubbard *
3253376a34efSJohn Hubbard * FOLL_FAST_ONLY is required in order to match the API description of
3254376a34efSJohn Hubbard * this routine: no fall back to regular ("slow") GUP.
32559e1f0580SJohn Hubbard */
3256b2cac248SLorenzo Stoakes if (!is_valid_gup_args(pages, NULL, &gup_flags,
3257d64e2dbcSJason Gunthorpe FOLL_GET | FOLL_FAST_ONLY))
3258d64e2dbcSJason Gunthorpe return -EINVAL;
32599e1f0580SJohn Hubbard
32609198a919SJason Gunthorpe return internal_get_user_pages_fast(start, nr_pages, gup_flags, pages);
32619e1f0580SJohn Hubbard }
3262dadbb612SSouptick Joarder EXPORT_SYMBOL_GPL(get_user_pages_fast_only);
32639e1f0580SJohn Hubbard
3264eddb1c22SJohn Hubbard /**
3265eddb1c22SJohn Hubbard * get_user_pages_fast() - pin user pages in memory
3266eddb1c22SJohn Hubbard * @start: starting user address
3267eddb1c22SJohn Hubbard * @nr_pages: number of pages from start to pin
3268eddb1c22SJohn Hubbard * @gup_flags: flags modifying pin behaviour
3269eddb1c22SJohn Hubbard * @pages: array that receives pointers to the pages pinned.
3270eddb1c22SJohn Hubbard * Should be at least nr_pages long.
3271eddb1c22SJohn Hubbard *
3272c1e8d7c6SMichel Lespinasse * Attempt to pin user pages in memory without taking mm->mmap_lock.
3273eddb1c22SJohn Hubbard * If not successful, it will fall back to taking the lock and
3274eddb1c22SJohn Hubbard * calling get_user_pages().
3275eddb1c22SJohn Hubbard *
3276eddb1c22SJohn Hubbard * Returns number of pages pinned. This may be fewer than the number requested.
3277eddb1c22SJohn Hubbard * If nr_pages is 0 or negative, returns 0. If no pages were pinned, returns
3278eddb1c22SJohn Hubbard * -errno.
3279eddb1c22SJohn Hubbard */
get_user_pages_fast(unsigned long start,int nr_pages,unsigned int gup_flags,struct page ** pages)3280eddb1c22SJohn Hubbard int get_user_pages_fast(unsigned long start, int nr_pages,
3281eddb1c22SJohn Hubbard unsigned int gup_flags, struct page **pages)
3282eddb1c22SJohn Hubbard {
328394202f12SJohn Hubbard /*
328494202f12SJohn Hubbard * The caller may or may not have explicitly set FOLL_GET; either way is
328594202f12SJohn Hubbard * OK. However, internally (within mm/gup.c), gup fast variants must set
328694202f12SJohn Hubbard * FOLL_GET, because gup fast is always a "pin with a +1 page refcount"
328794202f12SJohn Hubbard * request.
328894202f12SJohn Hubbard */
3289b2cac248SLorenzo Stoakes if (!is_valid_gup_args(pages, NULL, &gup_flags, FOLL_GET))
3290d64e2dbcSJason Gunthorpe return -EINVAL;
3291eddb1c22SJohn Hubbard return internal_get_user_pages_fast(start, nr_pages, gup_flags, pages);
3292eddb1c22SJohn Hubbard }
3293050a9adcSChristoph Hellwig EXPORT_SYMBOL_GPL(get_user_pages_fast);
3294eddb1c22SJohn Hubbard
3295eddb1c22SJohn Hubbard /**
3296eddb1c22SJohn Hubbard * pin_user_pages_fast() - pin user pages in memory without taking locks
3297eddb1c22SJohn Hubbard *
32983faa52c0SJohn Hubbard * @start: starting user address
32993faa52c0SJohn Hubbard * @nr_pages: number of pages from start to pin
33003faa52c0SJohn Hubbard * @gup_flags: flags modifying pin behaviour
33013faa52c0SJohn Hubbard * @pages: array that receives pointers to the pages pinned.
33023faa52c0SJohn Hubbard * Should be at least nr_pages long.
33033faa52c0SJohn Hubbard *
33043faa52c0SJohn Hubbard * Nearly the same as get_user_pages_fast(), except that FOLL_PIN is set. See
33053faa52c0SJohn Hubbard * get_user_pages_fast() for documentation on the function arguments, because
33063faa52c0SJohn Hubbard * the arguments here are identical.
33073faa52c0SJohn Hubbard *
33083faa52c0SJohn Hubbard * FOLL_PIN means that the pages must be released via unpin_user_page(). Please
330972ef5e52SMauro Carvalho Chehab * see Documentation/core-api/pin_user_pages.rst for further details.
3310c8070b78SDavid Howells *
3311c8070b78SDavid Howells * Note that if a zero_page is amongst the returned pages, it will not have
3312c8070b78SDavid Howells * pins in it and unpin_user_page() will not remove pins from it.
3313eddb1c22SJohn Hubbard */
pin_user_pages_fast(unsigned long start,int nr_pages,unsigned int gup_flags,struct page ** pages)3314eddb1c22SJohn Hubbard int pin_user_pages_fast(unsigned long start, int nr_pages,
3315eddb1c22SJohn Hubbard unsigned int gup_flags, struct page **pages)
3316eddb1c22SJohn Hubbard {
3317b2cac248SLorenzo Stoakes if (!is_valid_gup_args(pages, NULL, &gup_flags, FOLL_PIN))
33183faa52c0SJohn Hubbard return -EINVAL;
33193faa52c0SJohn Hubbard return internal_get_user_pages_fast(start, nr_pages, gup_flags, pages);
3320eddb1c22SJohn Hubbard }
3321eddb1c22SJohn Hubbard EXPORT_SYMBOL_GPL(pin_user_pages_fast);
3322eddb1c22SJohn Hubbard
3323eddb1c22SJohn Hubbard /**
332464019a2eSPeter Xu * pin_user_pages_remote() - pin pages of a remote process
3325eddb1c22SJohn Hubbard *
33263faa52c0SJohn Hubbard * @mm: mm_struct of target mm
33273faa52c0SJohn Hubbard * @start: starting user address
33283faa52c0SJohn Hubbard * @nr_pages: number of pages from start to pin
33293faa52c0SJohn Hubbard * @gup_flags: flags modifying lookup behaviour
33303faa52c0SJohn Hubbard * @pages: array that receives pointers to the pages pinned.
33310768c8deSYury Norov * Should be at least nr_pages long.
33323faa52c0SJohn Hubbard * @locked: pointer to lock flag indicating whether lock is held and
33333faa52c0SJohn Hubbard * subsequently whether VM_FAULT_RETRY functionality can be
33343faa52c0SJohn Hubbard * utilised. Lock must initially be held.
33353faa52c0SJohn Hubbard *
33363faa52c0SJohn Hubbard * Nearly the same as get_user_pages_remote(), except that FOLL_PIN is set. See
33373faa52c0SJohn Hubbard * get_user_pages_remote() for documentation on the function arguments, because
33383faa52c0SJohn Hubbard * the arguments here are identical.
33393faa52c0SJohn Hubbard *
33403faa52c0SJohn Hubbard * FOLL_PIN means that the pages must be released via unpin_user_page(). Please
334172ef5e52SMauro Carvalho Chehab * see Documentation/core-api/pin_user_pages.rst for details.
3342c8070b78SDavid Howells *
3343c8070b78SDavid Howells * Note that if a zero_page is amongst the returned pages, it will not have
3344c8070b78SDavid Howells * pins in it and unpin_user_page*() will not remove pins from it.
3345eddb1c22SJohn Hubbard */
pin_user_pages_remote(struct mm_struct * mm,unsigned long start,unsigned long nr_pages,unsigned int gup_flags,struct page ** pages,int * locked)334664019a2eSPeter Xu long pin_user_pages_remote(struct mm_struct *mm,
3347eddb1c22SJohn Hubbard unsigned long start, unsigned long nr_pages,
3348eddb1c22SJohn Hubbard unsigned int gup_flags, struct page **pages,
33490b295316SLorenzo Stoakes int *locked)
3350eddb1c22SJohn Hubbard {
33519a863a6aSJason Gunthorpe int local_locked = 1;
33529a863a6aSJason Gunthorpe
3353b2cac248SLorenzo Stoakes if (!is_valid_gup_args(pages, locked, &gup_flags,
3354d64e2dbcSJason Gunthorpe FOLL_PIN | FOLL_TOUCH | FOLL_REMOTE))
3355d64e2dbcSJason Gunthorpe return 0;
3356b2cac248SLorenzo Stoakes return __gup_longterm_locked(mm, start, nr_pages, pages,
33579a863a6aSJason Gunthorpe locked ? locked : &local_locked,
3358d64e2dbcSJason Gunthorpe gup_flags);
3359eddb1c22SJohn Hubbard }
3360eddb1c22SJohn Hubbard EXPORT_SYMBOL(pin_user_pages_remote);
3361eddb1c22SJohn Hubbard
3362eddb1c22SJohn Hubbard /**
3363eddb1c22SJohn Hubbard * pin_user_pages() - pin user pages in memory for use by other devices
3364eddb1c22SJohn Hubbard *
33653faa52c0SJohn Hubbard * @start: starting user address
33663faa52c0SJohn Hubbard * @nr_pages: number of pages from start to pin
33673faa52c0SJohn Hubbard * @gup_flags: flags modifying lookup behaviour
33683faa52c0SJohn Hubbard * @pages: array that receives pointers to the pages pinned.
33690768c8deSYury Norov * Should be at least nr_pages long.
33703faa52c0SJohn Hubbard *
33713faa52c0SJohn Hubbard * Nearly the same as get_user_pages(), except that FOLL_TOUCH is not set, and
33723faa52c0SJohn Hubbard * FOLL_PIN is set.
33733faa52c0SJohn Hubbard *
33743faa52c0SJohn Hubbard * FOLL_PIN means that the pages must be released via unpin_user_page(). Please
337572ef5e52SMauro Carvalho Chehab * see Documentation/core-api/pin_user_pages.rst for details.
3376c8070b78SDavid Howells *
3377c8070b78SDavid Howells * Note that if a zero_page is amongst the returned pages, it will not have
3378c8070b78SDavid Howells * pins in it and unpin_user_page*() will not remove pins from it.
3379eddb1c22SJohn Hubbard */
pin_user_pages(unsigned long start,unsigned long nr_pages,unsigned int gup_flags,struct page ** pages)3380eddb1c22SJohn Hubbard long pin_user_pages(unsigned long start, unsigned long nr_pages,
33814c630f30SLorenzo Stoakes unsigned int gup_flags, struct page **pages)
3382eddb1c22SJohn Hubbard {
33839a863a6aSJason Gunthorpe int locked = 1;
33849a863a6aSJason Gunthorpe
3385b2cac248SLorenzo Stoakes if (!is_valid_gup_args(pages, NULL, &gup_flags, FOLL_PIN))
3386d64e2dbcSJason Gunthorpe return 0;
338764019a2eSPeter Xu return __gup_longterm_locked(current->mm, start, nr_pages,
3388b2cac248SLorenzo Stoakes pages, &locked, gup_flags);
3389eddb1c22SJohn Hubbard }
3390eddb1c22SJohn Hubbard EXPORT_SYMBOL(pin_user_pages);
339191429023SJohn Hubbard
339291429023SJohn Hubbard /*
339391429023SJohn Hubbard * pin_user_pages_unlocked() is the FOLL_PIN variant of
339491429023SJohn Hubbard * get_user_pages_unlocked(). Behavior is the same, except that this one sets
339591429023SJohn Hubbard * FOLL_PIN and rejects FOLL_GET.
3396c8070b78SDavid Howells *
3397c8070b78SDavid Howells * Note that if a zero_page is amongst the returned pages, it will not have
3398c8070b78SDavid Howells * pins in it and unpin_user_page*() will not remove pins from it.
339991429023SJohn Hubbard */
pin_user_pages_unlocked(unsigned long start,unsigned long nr_pages,struct page ** pages,unsigned int gup_flags)340091429023SJohn Hubbard long pin_user_pages_unlocked(unsigned long start, unsigned long nr_pages,
340191429023SJohn Hubbard struct page **pages, unsigned int gup_flags)
340291429023SJohn Hubbard {
3403b2a72dffSJason Gunthorpe int locked = 0;
340491429023SJohn Hubbard
3405b2cac248SLorenzo Stoakes if (!is_valid_gup_args(pages, NULL, &gup_flags,
3406f04740f5SJason Gunthorpe FOLL_PIN | FOLL_TOUCH | FOLL_UNLOCKABLE))
3407d64e2dbcSJason Gunthorpe return 0;
34080768c8deSYury Norov
3409b2cac248SLorenzo Stoakes return __gup_longterm_locked(current->mm, start, nr_pages, pages,
3410b2a72dffSJason Gunthorpe &locked, gup_flags);
341191429023SJohn Hubbard }
341291429023SJohn Hubbard EXPORT_SYMBOL(pin_user_pages_unlocked);
3413