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;
79ece1ed7bSMatthew Wilcox (Oracle) if (unlikely(!folio_ref_try_add_rcu(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
1003967db22SJohn Hubbard /**
101ece1ed7bSMatthew Wilcox (Oracle) * try_grab_folio() - Attempt to get or pin a folio.
1023967db22SJohn Hubbard * @page: pointer to page to be grabbed
103ece1ed7bSMatthew Wilcox (Oracle) * @refs: the value to (effectively) add to the folio's refcount
1043967db22SJohn Hubbard * @flags: gup flags: these are the FOLL_* flag values.
1053967db22SJohn Hubbard *
1063faa52c0SJohn Hubbard * "grab" names in this file mean, "look at flags to decide whether to use
107ece1ed7bSMatthew Wilcox (Oracle) * FOLL_PIN or FOLL_GET behavior, when incrementing the folio's refcount.
1083faa52c0SJohn Hubbard *
1093faa52c0SJohn Hubbard * Either FOLL_PIN or FOLL_GET (or neither) must be set, but not both at the
1103faa52c0SJohn Hubbard * same time. (That's true throughout the get_user_pages*() and
1113faa52c0SJohn Hubbard * pin_user_pages*() APIs.) Cases:
1123faa52c0SJohn Hubbard *
113ece1ed7bSMatthew Wilcox (Oracle) * FOLL_GET: folio's refcount will be incremented by @refs.
1143967db22SJohn Hubbard *
115ece1ed7bSMatthew Wilcox (Oracle) * FOLL_PIN on large folios: folio's refcount will be incremented by
11694688e8eSMatthew Wilcox (Oracle) * @refs, and its pincount will be incremented by @refs.
1173967db22SJohn Hubbard *
118ece1ed7bSMatthew Wilcox (Oracle) * FOLL_PIN on single-page folios: folio's refcount will be incremented by
1195232c63fSMatthew Wilcox (Oracle) * @refs * GUP_PIN_COUNTING_BIAS.
1203faa52c0SJohn Hubbard *
121ece1ed7bSMatthew Wilcox (Oracle) * Return: The folio containing @page (with refcount appropriately
122ece1ed7bSMatthew Wilcox (Oracle) * incremented) for success, or NULL upon failure. If neither FOLL_GET
123ece1ed7bSMatthew Wilcox (Oracle) * nor FOLL_PIN was set, that's considered failure, and furthermore,
124ece1ed7bSMatthew Wilcox (Oracle) * a likely bug in the caller, so a warning is also emitted.
1253faa52c0SJohn Hubbard */
try_grab_folio(struct page * page,int refs,unsigned int flags)126ece1ed7bSMatthew Wilcox (Oracle) struct folio *try_grab_folio(struct page *page, int refs, unsigned int flags)
1273faa52c0SJohn Hubbard {
128503670eeSVishal Moola (Oracle) struct folio *folio;
129503670eeSVishal Moola (Oracle)
130503670eeSVishal Moola (Oracle) if (WARN_ON_ONCE((flags & (FOLL_GET | FOLL_PIN)) == 0))
131503670eeSVishal Moola (Oracle) return NULL;
132503670eeSVishal Moola (Oracle)
1334003f107SLogan Gunthorpe if (unlikely(!(flags & FOLL_PCI_P2PDMA) && is_pci_p2pdma_page(page)))
1344003f107SLogan Gunthorpe return NULL;
1354003f107SLogan Gunthorpe
1363faa52c0SJohn Hubbard if (flags & FOLL_GET)
137ece1ed7bSMatthew Wilcox (Oracle) return try_get_folio(page, refs);
138503670eeSVishal Moola (Oracle)
139503670eeSVishal Moola (Oracle) /* FOLL_PIN is set */
140ece1ed7bSMatthew Wilcox (Oracle)
14147e29d32SJohn Hubbard /*
142c8070b78SDavid Howells * Don't take a pin on the zero page - it's not going anywhere
143c8070b78SDavid Howells * and it is used in a *lot* of places.
144c8070b78SDavid Howells */
145c8070b78SDavid Howells if (is_zero_page(page))
146c8070b78SDavid Howells return page_folio(page);
147c8070b78SDavid Howells
1486e17c6deSLinus Torvalds folio = try_get_folio(page, refs);
149503670eeSVishal Moola (Oracle) if (!folio)
150503670eeSVishal Moola (Oracle) return NULL;
1513faa52c0SJohn Hubbard
152c8070b78SDavid Howells /*
153d1e153feSPavel Tatashin * Can't do FOLL_LONGTERM + FOLL_PIN gup fast path if not in a
154d1e153feSPavel Tatashin * right zone, so fail and let the caller fall back to the slow
155d1e153feSPavel Tatashin * path.
156df3a0a21SPingfan Liu */
157d1e153feSPavel Tatashin if (unlikely((flags & FOLL_LONGTERM) &&
158503670eeSVishal Moola (Oracle) !folio_is_longterm_pinnable(folio))) {
159503670eeSVishal Moola (Oracle) if (!put_devmap_managed_page_refs(&folio->page, refs))
160503670eeSVishal Moola (Oracle) folio_put_refs(folio, refs);
161df3a0a21SPingfan Liu return NULL;
162503670eeSVishal Moola (Oracle) }
163c24d3732SJann Horn
164c24d3732SJann Horn /*
165ece1ed7bSMatthew Wilcox (Oracle) * When pinning a large folio, use an exact count to track it.
16647e29d32SJohn Hubbard *
167ece1ed7bSMatthew Wilcox (Oracle) * However, be sure to *also* increment the normal folio
168ece1ed7bSMatthew Wilcox (Oracle) * refcount field at least once, so that the folio really
16978d9d6ceSMatthew Wilcox (Oracle) * is pinned. That's why the refcount from the earlier
170ece1ed7bSMatthew Wilcox (Oracle) * try_get_folio() is left intact.
17147e29d32SJohn Hubbard */
172ece1ed7bSMatthew Wilcox (Oracle) if (folio_test_large(folio))
17394688e8eSMatthew Wilcox (Oracle) atomic_add(refs, &folio->_pincount);
174c24d3732SJann Horn else
175ece1ed7bSMatthew Wilcox (Oracle) folio_ref_add(folio,
176ece1ed7bSMatthew Wilcox (Oracle) refs * (GUP_PIN_COUNTING_BIAS - 1));
177088b8aa5SDavid Hildenbrand /*
178088b8aa5SDavid Hildenbrand * Adjust the pincount before re-checking the PTE for changes.
179088b8aa5SDavid Hildenbrand * This is essentially a smp_mb() and is paired with a memory
180088b8aa5SDavid Hildenbrand * barrier in page_try_share_anon_rmap().
181088b8aa5SDavid Hildenbrand */
182088b8aa5SDavid Hildenbrand smp_mb__after_atomic();
183088b8aa5SDavid Hildenbrand
184ece1ed7bSMatthew Wilcox (Oracle) node_stat_mod_folio(folio, NR_FOLL_PIN_ACQUIRED, refs);
18547e29d32SJohn Hubbard
186ece1ed7bSMatthew Wilcox (Oracle) return folio;
1873faa52c0SJohn Hubbard }
1883faa52c0SJohn Hubbard
gup_put_folio(struct folio * folio,int refs,unsigned int flags)189d8ddc099SMatthew Wilcox (Oracle) static void gup_put_folio(struct folio *folio, int refs, unsigned int flags)
1904509b42cSJason Gunthorpe {
1914509b42cSJason Gunthorpe if (flags & FOLL_PIN) {
192c8070b78SDavid Howells if (is_zero_folio(folio))
193c8070b78SDavid Howells return;
194d8ddc099SMatthew Wilcox (Oracle) node_stat_mod_folio(folio, NR_FOLL_PIN_RELEASED, refs);
195d8ddc099SMatthew Wilcox (Oracle) if (folio_test_large(folio))
19694688e8eSMatthew Wilcox (Oracle) atomic_sub(refs, &folio->_pincount);
1974509b42cSJason Gunthorpe else
1984509b42cSJason Gunthorpe refs *= GUP_PIN_COUNTING_BIAS;
1994509b42cSJason Gunthorpe }
2004509b42cSJason Gunthorpe
201f4f451a1SMuchun Song if (!put_devmap_managed_page_refs(&folio->page, refs))
202d8ddc099SMatthew Wilcox (Oracle) folio_put_refs(folio, refs);
2034509b42cSJason Gunthorpe }
2044509b42cSJason Gunthorpe
2053faa52c0SJohn Hubbard /**
2063faa52c0SJohn Hubbard * try_grab_page() - elevate a page's refcount by a flag-dependent amount
2075fec0719SMatthew Wilcox (Oracle) * @page: pointer to page to be grabbed
2085fec0719SMatthew Wilcox (Oracle) * @flags: gup flags: these are the FOLL_* flag values.
2093faa52c0SJohn Hubbard *
2103faa52c0SJohn Hubbard * This might not do anything at all, depending on the flags argument.
2113faa52c0SJohn Hubbard *
2123faa52c0SJohn Hubbard * "grab" names in this file mean, "look at flags to decide whether to use
2133faa52c0SJohn Hubbard * FOLL_PIN or FOLL_GET behavior, when incrementing the page's refcount.
2143faa52c0SJohn Hubbard *
2153faa52c0SJohn Hubbard * Either FOLL_PIN or FOLL_GET (or neither) may be set, but not both at the same
216ece1ed7bSMatthew Wilcox (Oracle) * time. Cases: please see the try_grab_folio() documentation, with
2173967db22SJohn Hubbard * "refs=1".
2183faa52c0SJohn Hubbard *
2190f089235SLogan Gunthorpe * Return: 0 for success, or if no action was required (if neither FOLL_PIN
2200f089235SLogan Gunthorpe * nor FOLL_GET was set, nothing is done). A negative error code for failure:
2210f089235SLogan Gunthorpe *
2220f089235SLogan Gunthorpe * -ENOMEM FOLL_GET or FOLL_PIN was set, but the page could not
2230f089235SLogan Gunthorpe * be grabbed.
2243faa52c0SJohn Hubbard */
try_grab_page(struct page * page,unsigned int flags)2250f089235SLogan Gunthorpe int __must_check try_grab_page(struct page *page, unsigned int flags)
2263faa52c0SJohn Hubbard {
2275fec0719SMatthew Wilcox (Oracle) struct folio *folio = page_folio(page);
2285fec0719SMatthew Wilcox (Oracle)
2295fec0719SMatthew Wilcox (Oracle) if (WARN_ON_ONCE(folio_ref_count(folio) <= 0))
2300f089235SLogan Gunthorpe return -ENOMEM;
231c36c04c2SJohn Hubbard
2324003f107SLogan Gunthorpe if (unlikely(!(flags & FOLL_PCI_P2PDMA) && is_pci_p2pdma_page(page)))
2334003f107SLogan Gunthorpe return -EREMOTEIO;
234c36c04c2SJohn Hubbard
2355fec0719SMatthew Wilcox (Oracle) if (flags & FOLL_GET)
2365fec0719SMatthew Wilcox (Oracle) folio_ref_inc(folio);
2375fec0719SMatthew Wilcox (Oracle) else if (flags & FOLL_PIN) {
238c36c04c2SJohn Hubbard /*
239c8070b78SDavid Howells * Don't take a pin on the zero page - it's not going anywhere
240c8070b78SDavid Howells * and it is used in a *lot* of places.
241c8070b78SDavid Howells */
242c8070b78SDavid Howells if (is_zero_page(page))
243c8070b78SDavid Howells return 0;
244c8070b78SDavid Howells
245c8070b78SDavid Howells /*
2465fec0719SMatthew Wilcox (Oracle) * Similar to try_grab_folio(): be sure to *also*
24778d9d6ceSMatthew Wilcox (Oracle) * increment the normal page refcount field at least once,
24878d9d6ceSMatthew Wilcox (Oracle) * so that the page really is pinned.
249c36c04c2SJohn Hubbard */
2505fec0719SMatthew Wilcox (Oracle) if (folio_test_large(folio)) {
2515fec0719SMatthew Wilcox (Oracle) folio_ref_add(folio, 1);
25294688e8eSMatthew Wilcox (Oracle) atomic_add(1, &folio->_pincount);
2538ea2979cSMatthew Wilcox (Oracle) } else {
2545fec0719SMatthew Wilcox (Oracle) folio_ref_add(folio, GUP_PIN_COUNTING_BIAS);
2558ea2979cSMatthew Wilcox (Oracle) }
256c36c04c2SJohn Hubbard
2575fec0719SMatthew Wilcox (Oracle) node_stat_mod_folio(folio, NR_FOLL_PIN_ACQUIRED, 1);
258c36c04c2SJohn Hubbard }
259c36c04c2SJohn Hubbard
2600f089235SLogan Gunthorpe return 0;
2613faa52c0SJohn Hubbard }
2623faa52c0SJohn Hubbard
2633faa52c0SJohn Hubbard /**
2643faa52c0SJohn Hubbard * unpin_user_page() - release a dma-pinned page
2653faa52c0SJohn Hubbard * @page: pointer to page to be released
2663faa52c0SJohn Hubbard *
2673faa52c0SJohn Hubbard * Pages that were pinned via pin_user_pages*() must be released via either
2683faa52c0SJohn Hubbard * unpin_user_page(), or one of the unpin_user_pages*() routines. This is so
2693faa52c0SJohn Hubbard * that such pages can be separately tracked and uniquely handled. In
2703faa52c0SJohn Hubbard * particular, interactions with RDMA and filesystems need special handling.
2713faa52c0SJohn Hubbard */
unpin_user_page(struct page * page)2723faa52c0SJohn Hubbard void unpin_user_page(struct page *page)
2733faa52c0SJohn Hubbard {
274b6a2619cSDavid Hildenbrand sanity_check_pinned_pages(&page, 1);
275d8ddc099SMatthew Wilcox (Oracle) gup_put_folio(page_folio(page), 1, FOLL_PIN);
2763faa52c0SJohn Hubbard }
2773faa52c0SJohn Hubbard EXPORT_SYMBOL(unpin_user_page);
2783faa52c0SJohn Hubbard
2791101fb8fSDavid Howells /**
2801101fb8fSDavid Howells * folio_add_pin - Try to get an additional pin on a pinned folio
2811101fb8fSDavid Howells * @folio: The folio to be pinned
2821101fb8fSDavid Howells *
2831101fb8fSDavid Howells * Get an additional pin on a folio we already have a pin on. Makes no change
2841101fb8fSDavid Howells * if the folio is a zero_page.
2851101fb8fSDavid Howells */
folio_add_pin(struct folio * folio)2861101fb8fSDavid Howells void folio_add_pin(struct folio *folio)
2871101fb8fSDavid Howells {
2881101fb8fSDavid Howells if (is_zero_folio(folio))
2891101fb8fSDavid Howells return;
2901101fb8fSDavid Howells
2911101fb8fSDavid Howells /*
2921101fb8fSDavid Howells * Similar to try_grab_folio(): be sure to *also* increment the normal
2931101fb8fSDavid Howells * page refcount field at least once, so that the page really is
2941101fb8fSDavid Howells * pinned.
2951101fb8fSDavid Howells */
2961101fb8fSDavid Howells if (folio_test_large(folio)) {
2971101fb8fSDavid Howells WARN_ON_ONCE(atomic_read(&folio->_pincount) < 1);
2981101fb8fSDavid Howells folio_ref_inc(folio);
2991101fb8fSDavid Howells atomic_inc(&folio->_pincount);
3001101fb8fSDavid Howells } else {
3011101fb8fSDavid Howells WARN_ON_ONCE(folio_ref_count(folio) < GUP_PIN_COUNTING_BIAS);
3021101fb8fSDavid Howells folio_ref_add(folio, GUP_PIN_COUNTING_BIAS);
3031101fb8fSDavid Howells }
3041101fb8fSDavid Howells }
3051101fb8fSDavid Howells
gup_folio_range_next(struct page * start,unsigned long npages,unsigned long i,unsigned int * ntails)306659508f9SMatthew Wilcox (Oracle) static inline struct folio *gup_folio_range_next(struct page *start,
3078f39f5fcSMatthew Wilcox (Oracle) unsigned long npages, unsigned long i, unsigned int *ntails)
308458a4f78SJoao Martins {
309659508f9SMatthew Wilcox (Oracle) struct page *next = nth_page(start, i);
310659508f9SMatthew Wilcox (Oracle) struct folio *folio = page_folio(next);
311458a4f78SJoao Martins unsigned int nr = 1;
312458a4f78SJoao Martins
313659508f9SMatthew Wilcox (Oracle) if (folio_test_large(folio))
3144c654229SMatthew Wilcox (Oracle) nr = min_t(unsigned int, npages - i,
315659508f9SMatthew Wilcox (Oracle) folio_nr_pages(folio) - folio_page_idx(folio, next));
316458a4f78SJoao Martins
317458a4f78SJoao Martins *ntails = nr;
318659508f9SMatthew Wilcox (Oracle) return folio;
319458a4f78SJoao Martins }
320458a4f78SJoao Martins
gup_folio_next(struct page ** list,unsigned long npages,unsigned long i,unsigned int * ntails)32112521c76SMatthew Wilcox (Oracle) static inline struct folio *gup_folio_next(struct page **list,
32228297dbcSMatthew Wilcox (Oracle) unsigned long npages, unsigned long i, unsigned int *ntails)
3238745d7f6SJoao Martins {
32412521c76SMatthew Wilcox (Oracle) struct folio *folio = page_folio(list[i]);
3258745d7f6SJoao Martins unsigned int nr;
3268745d7f6SJoao Martins
3278745d7f6SJoao Martins for (nr = i + 1; nr < npages; nr++) {
32812521c76SMatthew Wilcox (Oracle) if (page_folio(list[nr]) != folio)
3298745d7f6SJoao Martins break;
3308745d7f6SJoao Martins }
3318745d7f6SJoao Martins
3328745d7f6SJoao Martins *ntails = nr - i;
33312521c76SMatthew Wilcox (Oracle) return folio;
3348745d7f6SJoao Martins }
3358745d7f6SJoao Martins
336fc1d8e7cSJohn Hubbard /**
337f1f6a7ddSJohn Hubbard * unpin_user_pages_dirty_lock() - release and optionally dirty gup-pinned pages
3382d15eb31Sakpm@linux-foundation.org * @pages: array of pages to be maybe marked dirty, and definitely released.
339fc1d8e7cSJohn Hubbard * @npages: number of pages in the @pages array.
3402d15eb31Sakpm@linux-foundation.org * @make_dirty: whether to mark the pages dirty
341fc1d8e7cSJohn Hubbard *
342fc1d8e7cSJohn Hubbard * "gup-pinned page" refers to a page that has had one of the get_user_pages()
343fc1d8e7cSJohn Hubbard * variants called on that page.
344fc1d8e7cSJohn Hubbard *
345fc1d8e7cSJohn Hubbard * For each page in the @pages array, make that page (or its head page, if a
3462d15eb31Sakpm@linux-foundation.org * compound page) dirty, if @make_dirty is true, and if the page was previously
347f1f6a7ddSJohn Hubbard * listed as clean. In any case, releases all pages using unpin_user_page(),
348f1f6a7ddSJohn Hubbard * possibly via unpin_user_pages(), for the non-dirty case.
349fc1d8e7cSJohn Hubbard *
350f1f6a7ddSJohn Hubbard * Please see the unpin_user_page() documentation for details.
351fc1d8e7cSJohn Hubbard *
3522d15eb31Sakpm@linux-foundation.org * set_page_dirty_lock() is used internally. If instead, set_page_dirty() is
3532d15eb31Sakpm@linux-foundation.org * required, then the caller should a) verify that this is really correct,
3542d15eb31Sakpm@linux-foundation.org * because _lock() is usually required, and b) hand code it:
355f1f6a7ddSJohn Hubbard * set_page_dirty_lock(), unpin_user_page().
356fc1d8e7cSJohn Hubbard *
357fc1d8e7cSJohn Hubbard */
unpin_user_pages_dirty_lock(struct page ** pages,unsigned long npages,bool make_dirty)358f1f6a7ddSJohn Hubbard void unpin_user_pages_dirty_lock(struct page **pages, unsigned long npages,
3592d15eb31Sakpm@linux-foundation.org bool make_dirty)
360fc1d8e7cSJohn Hubbard {
36112521c76SMatthew Wilcox (Oracle) unsigned long i;
36212521c76SMatthew Wilcox (Oracle) struct folio *folio;
36312521c76SMatthew Wilcox (Oracle) unsigned int nr;
3642d15eb31Sakpm@linux-foundation.org
3652d15eb31Sakpm@linux-foundation.org if (!make_dirty) {
366f1f6a7ddSJohn Hubbard unpin_user_pages(pages, npages);
3672d15eb31Sakpm@linux-foundation.org return;
3682d15eb31Sakpm@linux-foundation.org }
3692d15eb31Sakpm@linux-foundation.org
370b6a2619cSDavid Hildenbrand sanity_check_pinned_pages(pages, npages);
37112521c76SMatthew Wilcox (Oracle) for (i = 0; i < npages; i += nr) {
37212521c76SMatthew Wilcox (Oracle) folio = gup_folio_next(pages, npages, i, &nr);
3732d15eb31Sakpm@linux-foundation.org /*
3742d15eb31Sakpm@linux-foundation.org * Checking PageDirty at this point may race with
3752d15eb31Sakpm@linux-foundation.org * clear_page_dirty_for_io(), but that's OK. Two key
3762d15eb31Sakpm@linux-foundation.org * cases:
3772d15eb31Sakpm@linux-foundation.org *
3782d15eb31Sakpm@linux-foundation.org * 1) This code sees the page as already dirty, so it
3792d15eb31Sakpm@linux-foundation.org * skips the call to set_page_dirty(). That could happen
3802d15eb31Sakpm@linux-foundation.org * because clear_page_dirty_for_io() called
3812d15eb31Sakpm@linux-foundation.org * page_mkclean(), followed by set_page_dirty().
3822d15eb31Sakpm@linux-foundation.org * However, now the page is going to get written back,
3832d15eb31Sakpm@linux-foundation.org * which meets the original intention of setting it
3842d15eb31Sakpm@linux-foundation.org * dirty, so all is well: clear_page_dirty_for_io() goes
3852d15eb31Sakpm@linux-foundation.org * on to call TestClearPageDirty(), and write the page
3862d15eb31Sakpm@linux-foundation.org * back.
3872d15eb31Sakpm@linux-foundation.org *
3882d15eb31Sakpm@linux-foundation.org * 2) This code sees the page as clean, so it calls
3892d15eb31Sakpm@linux-foundation.org * set_page_dirty(). The page stays dirty, despite being
3902d15eb31Sakpm@linux-foundation.org * written back, so it gets written back again in the
3912d15eb31Sakpm@linux-foundation.org * next writeback cycle. This is harmless.
3922d15eb31Sakpm@linux-foundation.org */
39312521c76SMatthew Wilcox (Oracle) if (!folio_test_dirty(folio)) {
39412521c76SMatthew Wilcox (Oracle) folio_lock(folio);
39512521c76SMatthew Wilcox (Oracle) folio_mark_dirty(folio);
39612521c76SMatthew Wilcox (Oracle) folio_unlock(folio);
39712521c76SMatthew Wilcox (Oracle) }
39812521c76SMatthew Wilcox (Oracle) gup_put_folio(folio, nr, FOLL_PIN);
3992d15eb31Sakpm@linux-foundation.org }
400fc1d8e7cSJohn Hubbard }
401f1f6a7ddSJohn Hubbard EXPORT_SYMBOL(unpin_user_pages_dirty_lock);
402fc1d8e7cSJohn Hubbard
403fc1d8e7cSJohn Hubbard /**
404458a4f78SJoao Martins * unpin_user_page_range_dirty_lock() - release and optionally dirty
405458a4f78SJoao Martins * gup-pinned page range
406458a4f78SJoao Martins *
407458a4f78SJoao Martins * @page: the starting page of a range maybe marked dirty, and definitely released.
408458a4f78SJoao Martins * @npages: number of consecutive pages to release.
409458a4f78SJoao Martins * @make_dirty: whether to mark the pages dirty
410458a4f78SJoao Martins *
411458a4f78SJoao Martins * "gup-pinned page range" refers to a range of pages that has had one of the
412458a4f78SJoao Martins * pin_user_pages() variants called on that page.
413458a4f78SJoao Martins *
414458a4f78SJoao Martins * For the page ranges defined by [page .. page+npages], make that range (or
415458a4f78SJoao Martins * its head pages, if a compound page) dirty, if @make_dirty is true, and if the
416458a4f78SJoao Martins * page range was previously listed as clean.
417458a4f78SJoao Martins *
418458a4f78SJoao Martins * set_page_dirty_lock() is used internally. If instead, set_page_dirty() is
419458a4f78SJoao Martins * required, then the caller should a) verify that this is really correct,
420458a4f78SJoao Martins * because _lock() is usually required, and b) hand code it:
421458a4f78SJoao Martins * set_page_dirty_lock(), unpin_user_page().
422458a4f78SJoao Martins *
423458a4f78SJoao Martins */
unpin_user_page_range_dirty_lock(struct page * page,unsigned long npages,bool make_dirty)424458a4f78SJoao Martins void unpin_user_page_range_dirty_lock(struct page *page, unsigned long npages,
425458a4f78SJoao Martins bool make_dirty)
426458a4f78SJoao Martins {
427659508f9SMatthew Wilcox (Oracle) unsigned long i;
428659508f9SMatthew Wilcox (Oracle) struct folio *folio;
429659508f9SMatthew Wilcox (Oracle) unsigned int nr;
430458a4f78SJoao Martins
431659508f9SMatthew Wilcox (Oracle) for (i = 0; i < npages; i += nr) {
432659508f9SMatthew Wilcox (Oracle) folio = gup_folio_range_next(page, npages, i, &nr);
433659508f9SMatthew Wilcox (Oracle) if (make_dirty && !folio_test_dirty(folio)) {
434659508f9SMatthew Wilcox (Oracle) folio_lock(folio);
435659508f9SMatthew Wilcox (Oracle) folio_mark_dirty(folio);
436659508f9SMatthew Wilcox (Oracle) folio_unlock(folio);
437659508f9SMatthew Wilcox (Oracle) }
438659508f9SMatthew Wilcox (Oracle) gup_put_folio(folio, nr, FOLL_PIN);
439458a4f78SJoao Martins }
440458a4f78SJoao Martins }
441458a4f78SJoao Martins EXPORT_SYMBOL(unpin_user_page_range_dirty_lock);
442458a4f78SJoao Martins
unpin_user_pages_lockless(struct page ** pages,unsigned long npages)443b6a2619cSDavid Hildenbrand static void unpin_user_pages_lockless(struct page **pages, unsigned long npages)
444b6a2619cSDavid Hildenbrand {
445b6a2619cSDavid Hildenbrand unsigned long i;
446b6a2619cSDavid Hildenbrand struct folio *folio;
447b6a2619cSDavid Hildenbrand unsigned int nr;
448b6a2619cSDavid Hildenbrand
449b6a2619cSDavid Hildenbrand /*
450b6a2619cSDavid Hildenbrand * Don't perform any sanity checks because we might have raced with
451b6a2619cSDavid Hildenbrand * fork() and some anonymous pages might now actually be shared --
452b6a2619cSDavid Hildenbrand * which is why we're unpinning after all.
453b6a2619cSDavid Hildenbrand */
454b6a2619cSDavid Hildenbrand for (i = 0; i < npages; i += nr) {
455b6a2619cSDavid Hildenbrand folio = gup_folio_next(pages, npages, i, &nr);
456b6a2619cSDavid Hildenbrand gup_put_folio(folio, nr, FOLL_PIN);
457b6a2619cSDavid Hildenbrand }
458b6a2619cSDavid Hildenbrand }
459b6a2619cSDavid Hildenbrand
460458a4f78SJoao Martins /**
461f1f6a7ddSJohn Hubbard * unpin_user_pages() - release an array of gup-pinned pages.
462fc1d8e7cSJohn Hubbard * @pages: array of pages to be marked dirty and released.
463fc1d8e7cSJohn Hubbard * @npages: number of pages in the @pages array.
464fc1d8e7cSJohn Hubbard *
465f1f6a7ddSJohn Hubbard * For each page in the @pages array, release the page using unpin_user_page().
466fc1d8e7cSJohn Hubbard *
467f1f6a7ddSJohn Hubbard * Please see the unpin_user_page() documentation for details.
468fc1d8e7cSJohn Hubbard */
unpin_user_pages(struct page ** pages,unsigned long npages)469f1f6a7ddSJohn Hubbard void unpin_user_pages(struct page **pages, unsigned long npages)
470fc1d8e7cSJohn Hubbard {
47112521c76SMatthew Wilcox (Oracle) unsigned long i;
47212521c76SMatthew Wilcox (Oracle) struct folio *folio;
47312521c76SMatthew Wilcox (Oracle) unsigned int nr;
474fc1d8e7cSJohn Hubbard
475fc1d8e7cSJohn Hubbard /*
476146608bbSJohn Hubbard * If this WARN_ON() fires, then the system *might* be leaking pages (by
477146608bbSJohn Hubbard * leaving them pinned), but probably not. More likely, gup/pup returned
478146608bbSJohn Hubbard * a hard -ERRNO error to the caller, who erroneously passed it here.
479146608bbSJohn Hubbard */
480146608bbSJohn Hubbard if (WARN_ON(IS_ERR_VALUE(npages)))
481146608bbSJohn Hubbard return;
48231b912deSJoao Martins
483b6a2619cSDavid Hildenbrand sanity_check_pinned_pages(pages, npages);
48412521c76SMatthew Wilcox (Oracle) for (i = 0; i < npages; i += nr) {
48512521c76SMatthew Wilcox (Oracle) folio = gup_folio_next(pages, npages, i, &nr);
48612521c76SMatthew Wilcox (Oracle) gup_put_folio(folio, nr, FOLL_PIN);
487fc1d8e7cSJohn Hubbard }
488fc1d8e7cSJohn Hubbard }
489f1f6a7ddSJohn Hubbard EXPORT_SYMBOL(unpin_user_pages);
490fc1d8e7cSJohn Hubbard
491a458b76aSAndrea Arcangeli /*
492a458b76aSAndrea Arcangeli * Set the MMF_HAS_PINNED if not set yet; after set it'll be there for the mm's
493a458b76aSAndrea Arcangeli * lifecycle. Avoid setting the bit unless necessary, or it might cause write
494a458b76aSAndrea Arcangeli * cache bouncing on large SMP machines for concurrent pinned gups.
495a458b76aSAndrea Arcangeli */
mm_set_has_pinned_flag(unsigned long * mm_flags)496a458b76aSAndrea Arcangeli static inline void mm_set_has_pinned_flag(unsigned long *mm_flags)
497a458b76aSAndrea Arcangeli {
498a458b76aSAndrea Arcangeli if (!test_bit(MMF_HAS_PINNED, mm_flags))
499a458b76aSAndrea Arcangeli set_bit(MMF_HAS_PINNED, mm_flags);
500a458b76aSAndrea Arcangeli }
501a458b76aSAndrea Arcangeli
502050a9adcSChristoph Hellwig #ifdef CONFIG_MMU
no_page_table(struct vm_area_struct * vma,unsigned int flags)50369e68b4fSKirill A. Shutemov static struct page *no_page_table(struct vm_area_struct *vma,
50469e68b4fSKirill A. Shutemov unsigned int flags)
5054bbd4c77SKirill A. Shutemov {
5064bbd4c77SKirill A. Shutemov /*
50769e68b4fSKirill A. Shutemov * When core dumping an enormous anonymous area that nobody
50869e68b4fSKirill A. Shutemov * has touched so far, we don't want to allocate unnecessary pages or
50969e68b4fSKirill A. Shutemov * page tables. Return error instead of NULL to skip handle_mm_fault,
51069e68b4fSKirill A. Shutemov * then get_dump_page() will return NULL to leave a hole in the dump.
51169e68b4fSKirill A. Shutemov * But we can only make this optimization where a hole would surely
51269e68b4fSKirill A. Shutemov * be zero-filled if handle_mm_fault() actually did handle it.
5134bbd4c77SKirill A. Shutemov */
514a0137f16SAnshuman Khandual if ((flags & FOLL_DUMP) &&
515a0137f16SAnshuman Khandual (vma_is_anonymous(vma) || !vma->vm_ops->fault))
51669e68b4fSKirill A. Shutemov return ERR_PTR(-EFAULT);
51769e68b4fSKirill A. Shutemov return NULL;
5184bbd4c77SKirill A. Shutemov }
51969e68b4fSKirill A. Shutemov
follow_pfn_pte(struct vm_area_struct * vma,unsigned long address,pte_t * pte,unsigned int flags)5201027e443SKirill A. Shutemov static int follow_pfn_pte(struct vm_area_struct *vma, unsigned long address,
5211027e443SKirill A. Shutemov pte_t *pte, unsigned int flags)
5221027e443SKirill A. Shutemov {
5231027e443SKirill A. Shutemov if (flags & FOLL_TOUCH) {
524c33c7948SRyan Roberts pte_t orig_entry = ptep_get(pte);
525c33c7948SRyan Roberts pte_t entry = orig_entry;
5261027e443SKirill A. Shutemov
5271027e443SKirill A. Shutemov if (flags & FOLL_WRITE)
5281027e443SKirill A. Shutemov entry = pte_mkdirty(entry);
5291027e443SKirill A. Shutemov entry = pte_mkyoung(entry);
5301027e443SKirill A. Shutemov
531c33c7948SRyan Roberts if (!pte_same(orig_entry, entry)) {
5321027e443SKirill A. Shutemov set_pte_at(vma->vm_mm, address, pte, entry);
5331027e443SKirill A. Shutemov update_mmu_cache(vma, address, pte);
5341027e443SKirill A. Shutemov }
5351027e443SKirill A. Shutemov }
5361027e443SKirill A. Shutemov
5371027e443SKirill A. Shutemov /* Proper page table entry exists, but no corresponding struct page */
5381027e443SKirill A. Shutemov return -EEXIST;
5391027e443SKirill A. Shutemov }
5401027e443SKirill A. Shutemov
5415535be30SDavid 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)5425535be30SDavid Hildenbrand static inline bool can_follow_write_pte(pte_t pte, struct page *page,
5435535be30SDavid Hildenbrand struct vm_area_struct *vma,
5445535be30SDavid Hildenbrand unsigned int flags)
54519be0eafSLinus Torvalds {
5465535be30SDavid Hildenbrand /* If the pte is writable, we can write to the page. */
5475535be30SDavid Hildenbrand if (pte_write(pte))
5485535be30SDavid Hildenbrand return true;
5495535be30SDavid Hildenbrand
5505535be30SDavid Hildenbrand /* Maybe FOLL_FORCE is set to override it? */
5515535be30SDavid Hildenbrand if (!(flags & FOLL_FORCE))
5525535be30SDavid Hildenbrand return false;
5535535be30SDavid Hildenbrand
5545535be30SDavid Hildenbrand /* But FOLL_FORCE has no effect on shared mappings */
5555535be30SDavid Hildenbrand if (vma->vm_flags & (VM_MAYSHARE | VM_SHARED))
5565535be30SDavid Hildenbrand return false;
5575535be30SDavid Hildenbrand
5585535be30SDavid Hildenbrand /* ... or read-only private ones */
5595535be30SDavid Hildenbrand if (!(vma->vm_flags & VM_MAYWRITE))
5605535be30SDavid Hildenbrand return false;
5615535be30SDavid Hildenbrand
5625535be30SDavid Hildenbrand /* ... or already writable ones that just need to take a write fault */
5635535be30SDavid Hildenbrand if (vma->vm_flags & VM_WRITE)
5645535be30SDavid Hildenbrand return false;
5655535be30SDavid Hildenbrand
5665535be30SDavid Hildenbrand /*
5675535be30SDavid Hildenbrand * See can_change_pte_writable(): we broke COW and could map the page
5685535be30SDavid Hildenbrand * writable if we have an exclusive anonymous page ...
5695535be30SDavid Hildenbrand */
5705535be30SDavid Hildenbrand if (!page || !PageAnon(page) || !PageAnonExclusive(page))
5715535be30SDavid Hildenbrand return false;
5725535be30SDavid Hildenbrand
5735535be30SDavid Hildenbrand /* ... and a write-fault isn't required for other reasons. */
5745535be30SDavid Hildenbrand if (vma_soft_dirty_enabled(vma) && !pte_soft_dirty(pte))
5755535be30SDavid Hildenbrand return false;
5765535be30SDavid Hildenbrand return !userfaultfd_pte_wp(vma, pte);
57719be0eafSLinus Torvalds }
57819be0eafSLinus Torvalds
follow_page_pte(struct vm_area_struct * vma,unsigned long address,pmd_t * pmd,unsigned int flags,struct dev_pagemap ** pgmap)57969e68b4fSKirill A. Shutemov static struct page *follow_page_pte(struct vm_area_struct *vma,
580df06b37fSKeith Busch unsigned long address, pmd_t *pmd, unsigned int flags,
581df06b37fSKeith Busch struct dev_pagemap **pgmap)
58269e68b4fSKirill A. Shutemov {
58369e68b4fSKirill A. Shutemov struct mm_struct *mm = vma->vm_mm;
58469e68b4fSKirill A. Shutemov struct page *page;
58569e68b4fSKirill A. Shutemov spinlock_t *ptl;
58669e68b4fSKirill A. Shutemov pte_t *ptep, pte;
587f28d4363SClaudio Imbrenda int ret;
58869e68b4fSKirill A. Shutemov
589eddb1c22SJohn Hubbard /* FOLL_GET and FOLL_PIN are mutually exclusive. */
590eddb1c22SJohn Hubbard if (WARN_ON_ONCE((flags & (FOLL_PIN | FOLL_GET)) ==
591eddb1c22SJohn Hubbard (FOLL_PIN | FOLL_GET)))
592eddb1c22SJohn Hubbard return ERR_PTR(-EINVAL);
5934bbd4c77SKirill A. Shutemov
5944bbd4c77SKirill A. Shutemov ptep = pte_offset_map_lock(mm, pmd, address, &ptl);
59504dee9e8SHugh Dickins if (!ptep)
59604dee9e8SHugh Dickins return no_page_table(vma, flags);
597c33c7948SRyan Roberts pte = ptep_get(ptep);
598f7355e99SDavid Hildenbrand if (!pte_present(pte))
5994bbd4c77SKirill A. Shutemov goto no_page;
600d74943a2SDavid Hildenbrand if (pte_protnone(pte) && !gup_can_follow_protnone(vma, flags))
6014bbd4c77SKirill A. Shutemov goto no_page;
6024bbd4c77SKirill A. Shutemov
6034bbd4c77SKirill A. Shutemov page = vm_normal_page(vma, address, pte);
6045535be30SDavid Hildenbrand
6055535be30SDavid Hildenbrand /*
6065535be30SDavid Hildenbrand * We only care about anon pages in can_follow_write_pte() and don't
6075535be30SDavid Hildenbrand * have to worry about pte_devmap() because they are never anon.
6085535be30SDavid Hildenbrand */
6095535be30SDavid Hildenbrand if ((flags & FOLL_WRITE) &&
6105535be30SDavid Hildenbrand !can_follow_write_pte(pte, page, vma, flags)) {
6115535be30SDavid Hildenbrand page = NULL;
6125535be30SDavid Hildenbrand goto out;
6135535be30SDavid Hildenbrand }
6145535be30SDavid Hildenbrand
6153faa52c0SJohn Hubbard if (!page && pte_devmap(pte) && (flags & (FOLL_GET | FOLL_PIN))) {
6163565fce3SDan Williams /*
6173faa52c0SJohn Hubbard * Only return device mapping pages in the FOLL_GET or FOLL_PIN
6183faa52c0SJohn Hubbard * case since they are only valid while holding the pgmap
6193faa52c0SJohn Hubbard * reference.
6203565fce3SDan Williams */
621df06b37fSKeith Busch *pgmap = get_dev_pagemap(pte_pfn(pte), *pgmap);
622df06b37fSKeith Busch if (*pgmap)
6233565fce3SDan Williams page = pte_page(pte);
6243565fce3SDan Williams else
6253565fce3SDan Williams goto no_page;
6263565fce3SDan Williams } else if (unlikely(!page)) {
6271027e443SKirill A. Shutemov if (flags & FOLL_DUMP) {
6281027e443SKirill A. Shutemov /* Avoid special (like zero) pages in core dumps */
6291027e443SKirill A. Shutemov page = ERR_PTR(-EFAULT);
6301027e443SKirill A. Shutemov goto out;
6311027e443SKirill A. Shutemov }
6321027e443SKirill A. Shutemov
6331027e443SKirill A. Shutemov if (is_zero_pfn(pte_pfn(pte))) {
6344bbd4c77SKirill A. Shutemov page = pte_page(pte);
6351027e443SKirill A. Shutemov } else {
6361027e443SKirill A. Shutemov ret = follow_pfn_pte(vma, address, ptep, flags);
6371027e443SKirill A. Shutemov page = ERR_PTR(ret);
6381027e443SKirill A. Shutemov goto out;
6391027e443SKirill A. Shutemov }
6404bbd4c77SKirill A. Shutemov }
6414bbd4c77SKirill A. Shutemov
64284209e87SDavid Hildenbrand if (!pte_write(pte) && gup_must_unshare(vma, flags, page)) {
643a7f22660SDavid Hildenbrand page = ERR_PTR(-EMLINK);
644a7f22660SDavid Hildenbrand goto out;
645a7f22660SDavid Hildenbrand }
646b6a2619cSDavid Hildenbrand
647b6a2619cSDavid Hildenbrand VM_BUG_ON_PAGE((flags & FOLL_PIN) && PageAnon(page) &&
648b6a2619cSDavid Hildenbrand !PageAnonExclusive(page), page);
649b6a2619cSDavid Hildenbrand
6503faa52c0SJohn Hubbard /* try_grab_page() does nothing unless FOLL_GET or FOLL_PIN is set. */
6510f089235SLogan Gunthorpe ret = try_grab_page(page, flags);
6520f089235SLogan Gunthorpe if (unlikely(ret)) {
6530f089235SLogan Gunthorpe page = ERR_PTR(ret);
6548fde12caSLinus Torvalds goto out;
6558fde12caSLinus Torvalds }
6564003f107SLogan Gunthorpe
657f28d4363SClaudio Imbrenda /*
658f28d4363SClaudio Imbrenda * We need to make the page accessible if and only if we are going
659f28d4363SClaudio Imbrenda * to access its content (the FOLL_PIN case). Please see
660f28d4363SClaudio Imbrenda * Documentation/core-api/pin_user_pages.rst for details.
661f28d4363SClaudio Imbrenda */
662f28d4363SClaudio Imbrenda if (flags & FOLL_PIN) {
663f28d4363SClaudio Imbrenda ret = arch_make_page_accessible(page);
664f28d4363SClaudio Imbrenda if (ret) {
665f28d4363SClaudio Imbrenda unpin_user_page(page);
666f28d4363SClaudio Imbrenda page = ERR_PTR(ret);
667f28d4363SClaudio Imbrenda goto out;
668f28d4363SClaudio Imbrenda }
669f28d4363SClaudio Imbrenda }
6704bbd4c77SKirill A. Shutemov if (flags & FOLL_TOUCH) {
6714bbd4c77SKirill A. Shutemov if ((flags & FOLL_WRITE) &&
6724bbd4c77SKirill A. Shutemov !pte_dirty(pte) && !PageDirty(page))
6734bbd4c77SKirill A. Shutemov set_page_dirty(page);
6744bbd4c77SKirill A. Shutemov /*
6754bbd4c77SKirill A. Shutemov * pte_mkyoung() would be more correct here, but atomic care
6764bbd4c77SKirill A. Shutemov * is needed to avoid losing the dirty bit: it is easier to use
6774bbd4c77SKirill A. Shutemov * mark_page_accessed().
6784bbd4c77SKirill A. Shutemov */
6794bbd4c77SKirill A. Shutemov mark_page_accessed(page);
6804bbd4c77SKirill A. Shutemov }
6811027e443SKirill A. Shutemov out:
6824bbd4c77SKirill A. Shutemov pte_unmap_unlock(ptep, ptl);
6834bbd4c77SKirill A. Shutemov return page;
6844bbd4c77SKirill A. Shutemov no_page:
6854bbd4c77SKirill A. Shutemov pte_unmap_unlock(ptep, ptl);
6864bbd4c77SKirill A. Shutemov if (!pte_none(pte))
68769e68b4fSKirill A. Shutemov return NULL;
68869e68b4fSKirill A. Shutemov return no_page_table(vma, flags);
68969e68b4fSKirill A. Shutemov }
6904bbd4c77SKirill A. Shutemov
follow_pmd_mask(struct vm_area_struct * vma,unsigned long address,pud_t * pudp,unsigned int flags,struct follow_page_context * ctx)691080dbb61SAneesh Kumar K.V static struct page *follow_pmd_mask(struct vm_area_struct *vma,
692080dbb61SAneesh Kumar K.V unsigned long address, pud_t *pudp,
693df06b37fSKeith Busch unsigned int flags,
694df06b37fSKeith Busch struct follow_page_context *ctx)
69569e68b4fSKirill A. Shutemov {
69668827280SHuang Ying pmd_t *pmd, pmdval;
69769e68b4fSKirill A. Shutemov spinlock_t *ptl;
69869e68b4fSKirill A. Shutemov struct page *page;
69969e68b4fSKirill A. Shutemov struct mm_struct *mm = vma->vm_mm;
70069e68b4fSKirill A. Shutemov
701080dbb61SAneesh Kumar K.V pmd = pmd_offset(pudp, address);
70226e1a0c3SHugh Dickins pmdval = pmdp_get_lockless(pmd);
70368827280SHuang Ying if (pmd_none(pmdval))
70469e68b4fSKirill A. Shutemov return no_page_table(vma, flags);
705f7355e99SDavid Hildenbrand if (!pmd_present(pmdval))
706e66f17ffSNaoya Horiguchi return no_page_table(vma, flags);
70768827280SHuang Ying if (pmd_devmap(pmdval)) {
7083565fce3SDan Williams ptl = pmd_lock(mm, pmd);
709df06b37fSKeith Busch page = follow_devmap_pmd(vma, address, pmd, flags, &ctx->pgmap);
7103565fce3SDan Williams spin_unlock(ptl);
7113565fce3SDan Williams if (page)
7123565fce3SDan Williams return page;
7133565fce3SDan Williams }
71468827280SHuang Ying if (likely(!pmd_trans_huge(pmdval)))
715df06b37fSKeith Busch return follow_page_pte(vma, address, pmd, flags, &ctx->pgmap);
7166742d293SKirill A. Shutemov
717d74943a2SDavid Hildenbrand if (pmd_protnone(pmdval) && !gup_can_follow_protnone(vma, flags))
718db08f203SAneesh Kumar K.V return no_page_table(vma, flags);
719db08f203SAneesh Kumar K.V
7206742d293SKirill A. Shutemov ptl = pmd_lock(mm, pmd);
72184c3fc4eSZi Yan if (unlikely(!pmd_present(*pmd))) {
72284c3fc4eSZi Yan spin_unlock(ptl);
72384c3fc4eSZi Yan return no_page_table(vma, flags);
72484c3fc4eSZi Yan }
7256742d293SKirill A. Shutemov if (unlikely(!pmd_trans_huge(*pmd))) {
7266742d293SKirill A. Shutemov spin_unlock(ptl);
727df06b37fSKeith Busch return follow_page_pte(vma, address, pmd, flags, &ctx->pgmap);
72869e68b4fSKirill A. Shutemov }
7294066c119SYang Shi if (flags & FOLL_SPLIT_PMD) {
730bfe7b00dSSong Liu spin_unlock(ptl);
731bfe7b00dSSong Liu split_huge_pmd(vma, pmd, address);
7322378118bSHugh Dickins /* If pmd was left empty, stuff a page table in there quickly */
7332378118bSHugh Dickins return pte_alloc(mm, pmd) ? ERR_PTR(-ENOMEM) :
734df06b37fSKeith Busch follow_page_pte(vma, address, pmd, flags, &ctx->pgmap);
7356742d293SKirill A. Shutemov }
7366742d293SKirill A. Shutemov page = follow_trans_huge_pmd(vma, address, pmd, flags);
73769e68b4fSKirill A. Shutemov spin_unlock(ptl);
738df06b37fSKeith Busch ctx->page_mask = HPAGE_PMD_NR - 1;
73969e68b4fSKirill A. Shutemov return page;
74069e68b4fSKirill A. Shutemov }
74169e68b4fSKirill A. Shutemov
follow_pud_mask(struct vm_area_struct * vma,unsigned long address,p4d_t * p4dp,unsigned int flags,struct follow_page_context * ctx)742080dbb61SAneesh Kumar K.V static struct page *follow_pud_mask(struct vm_area_struct *vma,
743080dbb61SAneesh Kumar K.V unsigned long address, p4d_t *p4dp,
744df06b37fSKeith Busch unsigned int flags,
745df06b37fSKeith Busch struct follow_page_context *ctx)
746080dbb61SAneesh Kumar K.V {
747080dbb61SAneesh Kumar K.V pud_t *pud;
748080dbb61SAneesh Kumar K.V spinlock_t *ptl;
749080dbb61SAneesh Kumar K.V struct page *page;
750080dbb61SAneesh Kumar K.V struct mm_struct *mm = vma->vm_mm;
751080dbb61SAneesh Kumar K.V
752080dbb61SAneesh Kumar K.V pud = pud_offset(p4dp, address);
753080dbb61SAneesh Kumar K.V if (pud_none(*pud))
754080dbb61SAneesh Kumar K.V return no_page_table(vma, flags);
755080dbb61SAneesh Kumar K.V if (pud_devmap(*pud)) {
756080dbb61SAneesh Kumar K.V ptl = pud_lock(mm, pud);
757df06b37fSKeith Busch page = follow_devmap_pud(vma, address, pud, flags, &ctx->pgmap);
758080dbb61SAneesh Kumar K.V spin_unlock(ptl);
759080dbb61SAneesh Kumar K.V if (page)
760080dbb61SAneesh Kumar K.V return page;
761080dbb61SAneesh Kumar K.V }
762080dbb61SAneesh Kumar K.V if (unlikely(pud_bad(*pud)))
763080dbb61SAneesh Kumar K.V return no_page_table(vma, flags);
764080dbb61SAneesh Kumar K.V
765df06b37fSKeith Busch return follow_pmd_mask(vma, address, pud, flags, ctx);
766080dbb61SAneesh Kumar K.V }
767080dbb61SAneesh 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)768080dbb61SAneesh Kumar K.V static struct page *follow_p4d_mask(struct vm_area_struct *vma,
769080dbb61SAneesh Kumar K.V unsigned long address, pgd_t *pgdp,
770df06b37fSKeith Busch unsigned int flags,
771df06b37fSKeith Busch struct follow_page_context *ctx)
772080dbb61SAneesh Kumar K.V {
773080dbb61SAneesh Kumar K.V p4d_t *p4d;
774080dbb61SAneesh Kumar K.V
775080dbb61SAneesh Kumar K.V p4d = p4d_offset(pgdp, address);
776080dbb61SAneesh Kumar K.V if (p4d_none(*p4d))
777080dbb61SAneesh Kumar K.V return no_page_table(vma, flags);
778080dbb61SAneesh Kumar K.V BUILD_BUG_ON(p4d_huge(*p4d));
779080dbb61SAneesh Kumar K.V if (unlikely(p4d_bad(*p4d)))
780080dbb61SAneesh Kumar K.V return no_page_table(vma, flags);
781080dbb61SAneesh Kumar K.V
782df06b37fSKeith Busch return follow_pud_mask(vma, address, p4d, flags, ctx);
783080dbb61SAneesh Kumar K.V }
784080dbb61SAneesh Kumar K.V
785080dbb61SAneesh Kumar K.V /**
786080dbb61SAneesh Kumar K.V * follow_page_mask - look up a page descriptor from a user-virtual address
787080dbb61SAneesh Kumar K.V * @vma: vm_area_struct mapping @address
788080dbb61SAneesh Kumar K.V * @address: virtual address to look up
789080dbb61SAneesh Kumar K.V * @flags: flags modifying lookup behaviour
79078179556SMike Rapoport * @ctx: contains dev_pagemap for %ZONE_DEVICE memory pinning and a
79178179556SMike Rapoport * pointer to output page_mask
792080dbb61SAneesh Kumar K.V *
793080dbb61SAneesh Kumar K.V * @flags can have FOLL_ flags set, defined in <linux/mm.h>
794080dbb61SAneesh Kumar K.V *
79578179556SMike Rapoport * When getting pages from ZONE_DEVICE memory, the @ctx->pgmap caches
79678179556SMike Rapoport * the device's dev_pagemap metadata to avoid repeating expensive lookups.
79778179556SMike Rapoport *
798a7f22660SDavid Hildenbrand * When getting an anonymous page and the caller has to trigger unsharing
799a7f22660SDavid Hildenbrand * of a shared anonymous page first, -EMLINK is returned. The caller should
800a7f22660SDavid Hildenbrand * trigger a fault with FAULT_FLAG_UNSHARE set. Note that unsharing is only
801a7f22660SDavid Hildenbrand * relevant with FOLL_PIN and !FOLL_WRITE.
802a7f22660SDavid Hildenbrand *
80378179556SMike Rapoport * On output, the @ctx->page_mask is set according to the size of the page.
80478179556SMike Rapoport *
80578179556SMike Rapoport * Return: the mapped (struct page *), %NULL if no mapping exists, or
806080dbb61SAneesh Kumar K.V * an error pointer if there is a mapping to something not represented
807080dbb61SAneesh Kumar K.V * by a page descriptor (see also vm_normal_page()).
808080dbb61SAneesh Kumar K.V */
follow_page_mask(struct vm_area_struct * vma,unsigned long address,unsigned int flags,struct follow_page_context * ctx)809a7030aeaSBharath Vedartham static struct page *follow_page_mask(struct vm_area_struct *vma,
810080dbb61SAneesh Kumar K.V unsigned long address, unsigned int flags,
811df06b37fSKeith Busch struct follow_page_context *ctx)
812080dbb61SAneesh Kumar K.V {
813080dbb61SAneesh Kumar K.V pgd_t *pgd;
814080dbb61SAneesh Kumar K.V struct mm_struct *mm = vma->vm_mm;
815080dbb61SAneesh Kumar K.V
816df06b37fSKeith Busch ctx->page_mask = 0;
817080dbb61SAneesh Kumar K.V
81857a196a5SMike Kravetz /*
81957a196a5SMike Kravetz * Call hugetlb_follow_page_mask for hugetlb vmas as it will use
82057a196a5SMike Kravetz * special hugetlb page table walking code. This eliminates the
82157a196a5SMike Kravetz * need to check for hugetlb entries in the general walking code.
82257a196a5SMike Kravetz */
823dd767aaaSPeter Xu if (is_vm_hugetlb_page(vma))
8245502ea44SPeter Xu return hugetlb_follow_page_mask(vma, address, flags,
8255502ea44SPeter Xu &ctx->page_mask);
826080dbb61SAneesh Kumar K.V
827080dbb61SAneesh Kumar K.V pgd = pgd_offset(mm, address);
828080dbb61SAneesh Kumar K.V
829080dbb61SAneesh Kumar K.V if (pgd_none(*pgd) || unlikely(pgd_bad(*pgd)))
830080dbb61SAneesh Kumar K.V return no_page_table(vma, flags);
831080dbb61SAneesh Kumar K.V
832df06b37fSKeith Busch return follow_p4d_mask(vma, address, pgd, flags, ctx);
833df06b37fSKeith Busch }
834df06b37fSKeith Busch
follow_page(struct vm_area_struct * vma,unsigned long address,unsigned int foll_flags)835df06b37fSKeith Busch struct page *follow_page(struct vm_area_struct *vma, unsigned long address,
836df06b37fSKeith Busch unsigned int foll_flags)
837df06b37fSKeith Busch {
838df06b37fSKeith Busch struct follow_page_context ctx = { NULL };
839df06b37fSKeith Busch struct page *page;
840df06b37fSKeith Busch
8411507f512SMike Rapoport if (vma_is_secretmem(vma))
8421507f512SMike Rapoport return NULL;
8431507f512SMike Rapoport
844d64e2dbcSJason Gunthorpe if (WARN_ON_ONCE(foll_flags & FOLL_PIN))
8458909691bSDavid Hildenbrand return NULL;
8468909691bSDavid Hildenbrand
847d74943a2SDavid Hildenbrand /*
848d74943a2SDavid Hildenbrand * We never set FOLL_HONOR_NUMA_FAULT because callers don't expect
849d74943a2SDavid Hildenbrand * to fail on PROT_NONE-mapped pages.
850d74943a2SDavid Hildenbrand */
851df06b37fSKeith Busch page = follow_page_mask(vma, address, foll_flags, &ctx);
852df06b37fSKeith Busch if (ctx.pgmap)
853df06b37fSKeith Busch put_dev_pagemap(ctx.pgmap);
854df06b37fSKeith Busch return page;
855080dbb61SAneesh Kumar K.V }
856080dbb61SAneesh 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)857f2b495caSKirill A. Shutemov static int get_gate_page(struct mm_struct *mm, unsigned long address,
858f2b495caSKirill A. Shutemov unsigned int gup_flags, struct vm_area_struct **vma,
859f2b495caSKirill A. Shutemov struct page **page)
860f2b495caSKirill A. Shutemov {
861f2b495caSKirill A. Shutemov pgd_t *pgd;
862c2febafcSKirill A. Shutemov p4d_t *p4d;
863f2b495caSKirill A. Shutemov pud_t *pud;
864f2b495caSKirill A. Shutemov pmd_t *pmd;
865f2b495caSKirill A. Shutemov pte_t *pte;
866c33c7948SRyan Roberts pte_t entry;
867f2b495caSKirill A. Shutemov int ret = -EFAULT;
868f2b495caSKirill A. Shutemov
869f2b495caSKirill A. Shutemov /* user gate pages are read-only */
870f2b495caSKirill A. Shutemov if (gup_flags & FOLL_WRITE)
871f2b495caSKirill A. Shutemov return -EFAULT;
872f2b495caSKirill A. Shutemov if (address > TASK_SIZE)
873f2b495caSKirill A. Shutemov pgd = pgd_offset_k(address);
874f2b495caSKirill A. Shutemov else
875f2b495caSKirill A. Shutemov pgd = pgd_offset_gate(mm, address);
876b5d1c39fSAndy Lutomirski if (pgd_none(*pgd))
877b5d1c39fSAndy Lutomirski return -EFAULT;
878c2febafcSKirill A. Shutemov p4d = p4d_offset(pgd, address);
879b5d1c39fSAndy Lutomirski if (p4d_none(*p4d))
880b5d1c39fSAndy Lutomirski return -EFAULT;
881c2febafcSKirill A. Shutemov pud = pud_offset(p4d, address);
882b5d1c39fSAndy Lutomirski if (pud_none(*pud))
883b5d1c39fSAndy Lutomirski return -EFAULT;
884f2b495caSKirill A. Shutemov pmd = pmd_offset(pud, address);
88584c3fc4eSZi Yan if (!pmd_present(*pmd))
886f2b495caSKirill A. Shutemov return -EFAULT;
887f2b495caSKirill A. Shutemov pte = pte_offset_map(pmd, address);
88804dee9e8SHugh Dickins if (!pte)
88904dee9e8SHugh Dickins return -EFAULT;
890c33c7948SRyan Roberts entry = ptep_get(pte);
891c33c7948SRyan Roberts if (pte_none(entry))
892f2b495caSKirill A. Shutemov goto unmap;
893f2b495caSKirill A. Shutemov *vma = get_gate_vma(mm);
894f2b495caSKirill A. Shutemov if (!page)
895f2b495caSKirill A. Shutemov goto out;
896c33c7948SRyan Roberts *page = vm_normal_page(*vma, address, entry);
897f2b495caSKirill A. Shutemov if (!*page) {
898c33c7948SRyan Roberts if ((gup_flags & FOLL_DUMP) || !is_zero_pfn(pte_pfn(entry)))
899f2b495caSKirill A. Shutemov goto unmap;
900c33c7948SRyan Roberts *page = pte_page(entry);
901f2b495caSKirill A. Shutemov }
9020f089235SLogan Gunthorpe ret = try_grab_page(*page, gup_flags);
9030f089235SLogan Gunthorpe if (unlikely(ret))
9048fde12caSLinus Torvalds goto unmap;
905f2b495caSKirill A. Shutemov out:
906f2b495caSKirill A. Shutemov ret = 0;
907f2b495caSKirill A. Shutemov unmap:
908f2b495caSKirill A. Shutemov pte_unmap(pte);
909f2b495caSKirill A. Shutemov return ret;
910f2b495caSKirill A. Shutemov }
911f2b495caSKirill A. Shutemov
9129a95f3cfSPaul Cassella /*
9139a863a6aSJason Gunthorpe * mmap_lock must be held on entry. If @flags has FOLL_UNLOCKABLE but not
9149a863a6aSJason Gunthorpe * FOLL_NOWAIT, the mmap_lock may be released. If it is, *@locked will be set
9159a863a6aSJason Gunthorpe * to 0 and -EBUSY returned.
9169a95f3cfSPaul Cassella */
faultin_page(struct vm_area_struct * vma,unsigned long address,unsigned int * flags,bool unshare,int * locked)91764019a2eSPeter Xu static int faultin_page(struct vm_area_struct *vma,
918a7f22660SDavid Hildenbrand unsigned long address, unsigned int *flags, bool unshare,
919a7f22660SDavid Hildenbrand int *locked)
92016744483SKirill A. Shutemov {
92116744483SKirill A. Shutemov unsigned int fault_flags = 0;
9222b740303SSouptick Joarder vm_fault_t ret;
92316744483SKirill A. Shutemov
92455b8fe70SAndreas Gruenbacher if (*flags & FOLL_NOFAULT)
92555b8fe70SAndreas Gruenbacher return -EFAULT;
92616744483SKirill A. Shutemov if (*flags & FOLL_WRITE)
92716744483SKirill A. Shutemov fault_flags |= FAULT_FLAG_WRITE;
9281b2ee126SDave Hansen if (*flags & FOLL_REMOTE)
9291b2ee126SDave Hansen fault_flags |= FAULT_FLAG_REMOTE;
930f04740f5SJason Gunthorpe if (*flags & FOLL_UNLOCKABLE) {
93171335f37SPeter Xu fault_flags |= FAULT_FLAG_ALLOW_RETRY | FAULT_FLAG_KILLABLE;
93293c5c61dSPeter Xu /*
93393c5c61dSPeter Xu * FAULT_FLAG_INTERRUPTIBLE is opt-in. GUP callers must set
93493c5c61dSPeter Xu * FOLL_INTERRUPTIBLE to enable FAULT_FLAG_INTERRUPTIBLE.
93593c5c61dSPeter Xu * That's because some callers may not be prepared to
93693c5c61dSPeter Xu * handle early exits caused by non-fatal signals.
93793c5c61dSPeter Xu */
93893c5c61dSPeter Xu if (*flags & FOLL_INTERRUPTIBLE)
93993c5c61dSPeter Xu fault_flags |= FAULT_FLAG_INTERRUPTIBLE;
94093c5c61dSPeter Xu }
94116744483SKirill A. Shutemov if (*flags & FOLL_NOWAIT)
94216744483SKirill A. Shutemov fault_flags |= FAULT_FLAG_ALLOW_RETRY | FAULT_FLAG_RETRY_NOWAIT;
943234b239bSAndres Lagar-Cavilla if (*flags & FOLL_TRIED) {
9444426e945SPeter Xu /*
9454426e945SPeter Xu * Note: FAULT_FLAG_ALLOW_RETRY and FAULT_FLAG_TRIED
9464426e945SPeter Xu * can co-exist
9474426e945SPeter Xu */
948234b239bSAndres Lagar-Cavilla fault_flags |= FAULT_FLAG_TRIED;
949234b239bSAndres Lagar-Cavilla }
950a7f22660SDavid Hildenbrand if (unshare) {
951a7f22660SDavid Hildenbrand fault_flags |= FAULT_FLAG_UNSHARE;
952a7f22660SDavid Hildenbrand /* FAULT_FLAG_WRITE and FAULT_FLAG_UNSHARE are incompatible */
953a7f22660SDavid Hildenbrand VM_BUG_ON(fault_flags & FAULT_FLAG_WRITE);
954a7f22660SDavid Hildenbrand }
95516744483SKirill A. Shutemov
956bce617edSPeter Xu ret = handle_mm_fault(vma, address, fault_flags, NULL);
957d9272525SPeter Xu
958d9272525SPeter Xu if (ret & VM_FAULT_COMPLETED) {
959d9272525SPeter Xu /*
960d9272525SPeter Xu * With FAULT_FLAG_RETRY_NOWAIT we'll never release the
961d9272525SPeter Xu * mmap lock in the page fault handler. Sanity check this.
962d9272525SPeter Xu */
963d9272525SPeter Xu WARN_ON_ONCE(fault_flags & FAULT_FLAG_RETRY_NOWAIT);
964d9272525SPeter Xu *locked = 0;
9659a863a6aSJason Gunthorpe
966d9272525SPeter Xu /*
967d9272525SPeter Xu * We should do the same as VM_FAULT_RETRY, but let's not
968d9272525SPeter Xu * return -EBUSY since that's not reflecting the reality of
969d9272525SPeter Xu * what has happened - we've just fully completed a page
970d9272525SPeter Xu * fault, with the mmap lock released. Use -EAGAIN to show
971d9272525SPeter Xu * that we want to take the mmap lock _again_.
972d9272525SPeter Xu */
973d9272525SPeter Xu return -EAGAIN;
974d9272525SPeter Xu }
975d9272525SPeter Xu
97616744483SKirill A. Shutemov if (ret & VM_FAULT_ERROR) {
9779a291a7cSJames Morse int err = vm_fault_to_errno(ret, *flags);
9789a291a7cSJames Morse
9799a291a7cSJames Morse if (err)
9809a291a7cSJames Morse return err;
98116744483SKirill A. Shutemov BUG();
98216744483SKirill A. Shutemov }
98316744483SKirill A. Shutemov
98416744483SKirill A. Shutemov if (ret & VM_FAULT_RETRY) {
9859a863a6aSJason Gunthorpe if (!(fault_flags & FAULT_FLAG_RETRY_NOWAIT))
9864f6da934SPeter Xu *locked = 0;
98716744483SKirill A. Shutemov return -EBUSY;
98816744483SKirill A. Shutemov }
98916744483SKirill A. Shutemov
99016744483SKirill A. Shutemov return 0;
99116744483SKirill A. Shutemov }
99216744483SKirill A. Shutemov
9938ac26843SLorenzo Stoakes /*
9948ac26843SLorenzo Stoakes * Writing to file-backed mappings which require folio dirty tracking using GUP
9958ac26843SLorenzo Stoakes * is a fundamentally broken operation, as kernel write access to GUP mappings
9968ac26843SLorenzo Stoakes * do not adhere to the semantics expected by a file system.
9978ac26843SLorenzo Stoakes *
9988ac26843SLorenzo Stoakes * Consider the following scenario:-
9998ac26843SLorenzo Stoakes *
10008ac26843SLorenzo Stoakes * 1. A folio is written to via GUP which write-faults the memory, notifying
10018ac26843SLorenzo Stoakes * the file system and dirtying the folio.
10028ac26843SLorenzo Stoakes * 2. Later, writeback is triggered, resulting in the folio being cleaned and
10038ac26843SLorenzo Stoakes * the PTE being marked read-only.
10048ac26843SLorenzo Stoakes * 3. The GUP caller writes to the folio, as it is mapped read/write via the
10058ac26843SLorenzo Stoakes * direct mapping.
10068ac26843SLorenzo Stoakes * 4. The GUP caller, now done with the page, unpins it and sets it dirty
10078ac26843SLorenzo Stoakes * (though it does not have to).
10088ac26843SLorenzo Stoakes *
10098ac26843SLorenzo Stoakes * This results in both data being written to a folio without writenotify, and
10108ac26843SLorenzo Stoakes * the folio being dirtied unexpectedly (if the caller decides to do so).
10118ac26843SLorenzo Stoakes */
writable_file_mapping_allowed(struct vm_area_struct * vma,unsigned long gup_flags)10128ac26843SLorenzo Stoakes static bool writable_file_mapping_allowed(struct vm_area_struct *vma,
10138ac26843SLorenzo Stoakes unsigned long gup_flags)
10148ac26843SLorenzo Stoakes {
10158ac26843SLorenzo Stoakes /*
10168ac26843SLorenzo Stoakes * If we aren't pinning then no problematic write can occur. A long term
10178ac26843SLorenzo Stoakes * pin is the most egregious case so this is the case we disallow.
10188ac26843SLorenzo Stoakes */
10198ac26843SLorenzo Stoakes if ((gup_flags & (FOLL_PIN | FOLL_LONGTERM)) !=
10208ac26843SLorenzo Stoakes (FOLL_PIN | FOLL_LONGTERM))
10218ac26843SLorenzo Stoakes return true;
10228ac26843SLorenzo Stoakes
10238ac26843SLorenzo Stoakes /*
10248ac26843SLorenzo Stoakes * If the VMA does not require dirty tracking then no problematic write
10258ac26843SLorenzo Stoakes * can occur either.
10268ac26843SLorenzo Stoakes */
10278ac26843SLorenzo Stoakes return !vma_needs_dirty_tracking(vma);
10288ac26843SLorenzo Stoakes }
10298ac26843SLorenzo Stoakes
check_vma_flags(struct vm_area_struct * vma,unsigned long gup_flags)1030fa5bb209SKirill A. Shutemov static int check_vma_flags(struct vm_area_struct *vma, unsigned long gup_flags)
1031fa5bb209SKirill A. Shutemov {
1032fa5bb209SKirill A. Shutemov vm_flags_t vm_flags = vma->vm_flags;
10331b2ee126SDave Hansen int write = (gup_flags & FOLL_WRITE);
10341b2ee126SDave Hansen int foreign = (gup_flags & FOLL_REMOTE);
10358ac26843SLorenzo Stoakes bool vma_anon = vma_is_anonymous(vma);
1036fa5bb209SKirill A. Shutemov
1037fa5bb209SKirill A. Shutemov if (vm_flags & (VM_IO | VM_PFNMAP))
1038fa5bb209SKirill A. Shutemov return -EFAULT;
1039fa5bb209SKirill A. Shutemov
10408ac26843SLorenzo Stoakes if ((gup_flags & FOLL_ANON) && !vma_anon)
10417f7ccc2cSWilly Tarreau return -EFAULT;
10427f7ccc2cSWilly Tarreau
104352650c8bSJason Gunthorpe if ((gup_flags & FOLL_LONGTERM) && vma_is_fsdax(vma))
104452650c8bSJason Gunthorpe return -EOPNOTSUPP;
104552650c8bSJason Gunthorpe
10461507f512SMike Rapoport if (vma_is_secretmem(vma))
10471507f512SMike Rapoport return -EFAULT;
10481507f512SMike Rapoport
10491b2ee126SDave Hansen if (write) {
10508ac26843SLorenzo Stoakes if (!vma_anon &&
10518ac26843SLorenzo Stoakes !writable_file_mapping_allowed(vma, gup_flags))
10528ac26843SLorenzo Stoakes return -EFAULT;
10538ac26843SLorenzo Stoakes
10546beb9958SRick Edgecombe if (!(vm_flags & VM_WRITE) || (vm_flags & VM_SHADOW_STACK)) {
1055fa5bb209SKirill A. Shutemov if (!(gup_flags & FOLL_FORCE))
1056fa5bb209SKirill A. Shutemov return -EFAULT;
1057f347454dSDavid Hildenbrand /* hugetlb does not support FOLL_FORCE|FOLL_WRITE. */
1058f347454dSDavid Hildenbrand if (is_vm_hugetlb_page(vma))
1059f347454dSDavid Hildenbrand return -EFAULT;
1060fa5bb209SKirill A. Shutemov /*
1061fa5bb209SKirill A. Shutemov * We used to let the write,force case do COW in a
1062fa5bb209SKirill A. Shutemov * VM_MAYWRITE VM_SHARED !VM_WRITE vma, so ptrace could
1063fa5bb209SKirill A. Shutemov * set a breakpoint in a read-only mapping of an
1064fa5bb209SKirill A. Shutemov * executable, without corrupting the file (yet only
1065fa5bb209SKirill A. Shutemov * when that file had been opened for writing!).
1066fa5bb209SKirill A. Shutemov * Anon pages in shared mappings are surprising: now
1067fa5bb209SKirill A. Shutemov * just reject it.
1068fa5bb209SKirill A. Shutemov */
106946435364SHugh Dickins if (!is_cow_mapping(vm_flags))
1070fa5bb209SKirill A. Shutemov return -EFAULT;
1071fa5bb209SKirill A. Shutemov }
1072fa5bb209SKirill A. Shutemov } else if (!(vm_flags & VM_READ)) {
1073fa5bb209SKirill A. Shutemov if (!(gup_flags & FOLL_FORCE))
1074fa5bb209SKirill A. Shutemov return -EFAULT;
1075fa5bb209SKirill A. Shutemov /*
1076fa5bb209SKirill A. Shutemov * Is there actually any vma we can reach here which does not
1077fa5bb209SKirill A. Shutemov * have VM_MAYREAD set?
1078fa5bb209SKirill A. Shutemov */
1079fa5bb209SKirill A. Shutemov if (!(vm_flags & VM_MAYREAD))
1080fa5bb209SKirill A. Shutemov return -EFAULT;
1081fa5bb209SKirill A. Shutemov }
1082d61172b4SDave Hansen /*
1083d61172b4SDave Hansen * gups are always data accesses, not instruction
1084d61172b4SDave Hansen * fetches, so execute=false here
1085d61172b4SDave Hansen */
1086d61172b4SDave Hansen if (!arch_vma_access_permitted(vma, write, false, foreign))
108733a709b2SDave Hansen return -EFAULT;
1088fa5bb209SKirill A. Shutemov return 0;
1089fa5bb209SKirill A. Shutemov }
1090fa5bb209SKirill A. Shutemov
10916cd06ab1SLinus Torvalds /*
10926cd06ab1SLinus Torvalds * This is "vma_lookup()", but with a warning if we would have
10936cd06ab1SLinus Torvalds * historically expanded the stack in the GUP code.
10946cd06ab1SLinus Torvalds */
gup_vma_lookup(struct mm_struct * mm,unsigned long addr)10956cd06ab1SLinus Torvalds static struct vm_area_struct *gup_vma_lookup(struct mm_struct *mm,
10966cd06ab1SLinus Torvalds unsigned long addr)
10976cd06ab1SLinus Torvalds {
10986cd06ab1SLinus Torvalds #ifdef CONFIG_STACK_GROWSUP
10996cd06ab1SLinus Torvalds return vma_lookup(mm, addr);
11006cd06ab1SLinus Torvalds #else
11016cd06ab1SLinus Torvalds static volatile unsigned long next_warn;
11026cd06ab1SLinus Torvalds struct vm_area_struct *vma;
11036cd06ab1SLinus Torvalds unsigned long now, next;
11046cd06ab1SLinus Torvalds
11056cd06ab1SLinus Torvalds vma = find_vma(mm, addr);
11066cd06ab1SLinus Torvalds if (!vma || (addr >= vma->vm_start))
11076cd06ab1SLinus Torvalds return vma;
11086cd06ab1SLinus Torvalds
11096cd06ab1SLinus Torvalds /* Only warn for half-way relevant accesses */
11106cd06ab1SLinus Torvalds if (!(vma->vm_flags & VM_GROWSDOWN))
11116cd06ab1SLinus Torvalds return NULL;
11126cd06ab1SLinus Torvalds if (vma->vm_start - addr > 65536)
11136cd06ab1SLinus Torvalds return NULL;
11146cd06ab1SLinus Torvalds
11156cd06ab1SLinus Torvalds /* Let's not warn more than once an hour.. */
11166cd06ab1SLinus Torvalds now = jiffies; next = next_warn;
11176cd06ab1SLinus Torvalds if (next && time_before(now, next))
11186cd06ab1SLinus Torvalds return NULL;
11196cd06ab1SLinus Torvalds next_warn = now + 60*60*HZ;
11206cd06ab1SLinus Torvalds
11216cd06ab1SLinus Torvalds /* Let people know things may have changed. */
11226cd06ab1SLinus Torvalds pr_warn("GUP no longer grows the stack in %s (%d): %lx-%lx (%lx)\n",
11236cd06ab1SLinus Torvalds current->comm, task_pid_nr(current),
11246cd06ab1SLinus Torvalds vma->vm_start, vma->vm_end, addr);
11256cd06ab1SLinus Torvalds dump_stack();
11266cd06ab1SLinus Torvalds return NULL;
11276cd06ab1SLinus Torvalds #endif
11286cd06ab1SLinus Torvalds }
11296cd06ab1SLinus Torvalds
11304bbd4c77SKirill A. Shutemov /**
11314bbd4c77SKirill A. Shutemov * __get_user_pages() - pin user pages in memory
11324bbd4c77SKirill A. Shutemov * @mm: mm_struct of target mm
11334bbd4c77SKirill A. Shutemov * @start: starting user address
11344bbd4c77SKirill A. Shutemov * @nr_pages: number of pages from start to pin
11354bbd4c77SKirill A. Shutemov * @gup_flags: flags modifying pin behaviour
11364bbd4c77SKirill A. Shutemov * @pages: array that receives pointers to the pages pinned.
11374bbd4c77SKirill A. Shutemov * Should be at least nr_pages long. Or NULL, if caller
11384bbd4c77SKirill A. Shutemov * only intends to ensure the pages are faulted in.
1139c1e8d7c6SMichel Lespinasse * @locked: whether we're still with the mmap_lock held
11404bbd4c77SKirill A. Shutemov *
1141d2dfbe47SLiu Xiang * Returns either number of pages pinned (which may be less than the
1142d2dfbe47SLiu Xiang * number requested), or an error. Details about the return value:
1143d2dfbe47SLiu Xiang *
1144d2dfbe47SLiu Xiang * -- If nr_pages is 0, returns 0.
1145d2dfbe47SLiu Xiang * -- If nr_pages is >0, but no pages were pinned, returns -errno.
1146d2dfbe47SLiu Xiang * -- If nr_pages is >0, and some pages were pinned, returns the number of
1147d2dfbe47SLiu Xiang * pages pinned. Again, this may be less than nr_pages.
11482d3a36a4SMichal Hocko * -- 0 return value is possible when the fault would need to be retried.
1149d2dfbe47SLiu Xiang *
1150d2dfbe47SLiu Xiang * The caller is responsible for releasing returned @pages, via put_page().
1151d2dfbe47SLiu Xiang *
1152c1e8d7c6SMichel Lespinasse * Must be called with mmap_lock held. It may be released. See below.
11534bbd4c77SKirill A. Shutemov *
11544bbd4c77SKirill A. Shutemov * __get_user_pages walks a process's page tables and takes a reference to
11554bbd4c77SKirill A. Shutemov * each struct page that each user address corresponds to at a given
11564bbd4c77SKirill A. Shutemov * instant. That is, it takes the page that would be accessed if a user
11574bbd4c77SKirill A. Shutemov * thread accesses the given user virtual address at that instant.
11584bbd4c77SKirill A. Shutemov *
11594bbd4c77SKirill A. Shutemov * This does not guarantee that the page exists in the user mappings when
11604bbd4c77SKirill A. Shutemov * __get_user_pages returns, and there may even be a completely different
11614bbd4c77SKirill A. Shutemov * page there in some cases (eg. if mmapped pagecache has been invalidated
1162c5acf1f6SJongwoo Han * and subsequently re-faulted). However it does guarantee that the page
11634bbd4c77SKirill A. Shutemov * won't be freed completely. And mostly callers simply care that the page
11644bbd4c77SKirill A. Shutemov * contains data that was valid *at some point in time*. Typically, an IO
11654bbd4c77SKirill A. Shutemov * or similar operation cannot guarantee anything stronger anyway because
11664bbd4c77SKirill A. Shutemov * locks can't be held over the syscall boundary.
11674bbd4c77SKirill A. Shutemov *
11684bbd4c77SKirill A. Shutemov * If @gup_flags & FOLL_WRITE == 0, the page must not be written to. If
11694bbd4c77SKirill A. Shutemov * the page is written to, set_page_dirty (or set_page_dirty_lock, as
11704bbd4c77SKirill A. Shutemov * appropriate) must be called after the page is finished with, and
11714bbd4c77SKirill A. Shutemov * before put_page is called.
11724bbd4c77SKirill A. Shutemov *
11739a863a6aSJason Gunthorpe * If FOLL_UNLOCKABLE is set without FOLL_NOWAIT then the mmap_lock may
11749a863a6aSJason Gunthorpe * be released. If this happens *@locked will be set to 0 on return.
11759a95f3cfSPaul Cassella *
11769a863a6aSJason Gunthorpe * A caller using such a combination of @gup_flags must therefore hold the
11779a863a6aSJason Gunthorpe * mmap_lock for reading only, and recognize when it's been released. Otherwise,
11789a863a6aSJason Gunthorpe * it must be held for either reading or writing and will not be released.
11794bbd4c77SKirill A. Shutemov *
11804bbd4c77SKirill A. Shutemov * In most cases, get_user_pages or get_user_pages_fast should be used
11814bbd4c77SKirill A. Shutemov * instead of __get_user_pages. __get_user_pages should be used only if
11824bbd4c77SKirill A. Shutemov * you need some special @gup_flags.
11834bbd4c77SKirill 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)118464019a2eSPeter Xu static long __get_user_pages(struct mm_struct *mm,
11854bbd4c77SKirill A. Shutemov unsigned long start, unsigned long nr_pages,
11864bbd4c77SKirill A. Shutemov unsigned int gup_flags, struct page **pages,
1187b2cac248SLorenzo Stoakes int *locked)
11884bbd4c77SKirill A. Shutemov {
1189df06b37fSKeith Busch long ret = 0, i = 0;
1190fa5bb209SKirill A. Shutemov struct vm_area_struct *vma = NULL;
1191df06b37fSKeith Busch struct follow_page_context ctx = { NULL };
11924bbd4c77SKirill A. Shutemov
11934bbd4c77SKirill A. Shutemov if (!nr_pages)
11944bbd4c77SKirill A. Shutemov return 0;
11954bbd4c77SKirill A. Shutemov
1196428e106aSKirill A. Shutemov start = untagged_addr_remote(mm, start);
1197f9652594SAndrey Konovalov
1198eddb1c22SJohn Hubbard VM_BUG_ON(!!pages != !!(gup_flags & (FOLL_GET | FOLL_PIN)));
11994bbd4c77SKirill A. Shutemov
12004bbd4c77SKirill A. Shutemov do {
12014bbd4c77SKirill A. Shutemov struct page *page;
12024bbd4c77SKirill A. Shutemov unsigned int foll_flags = gup_flags;
12034bbd4c77SKirill A. Shutemov unsigned int page_increm;
12044bbd4c77SKirill A. Shutemov
1205fa5bb209SKirill A. Shutemov /* first iteration or cross vma bound */
1206fa5bb209SKirill A. Shutemov if (!vma || start >= vma->vm_end) {
12079e898211SDavid Hildenbrand /*
12089e898211SDavid Hildenbrand * MADV_POPULATE_(READ|WRITE) wants to handle VMA
12099e898211SDavid Hildenbrand * lookups+error reporting differently.
12109e898211SDavid Hildenbrand */
12119e898211SDavid Hildenbrand if (gup_flags & FOLL_MADV_POPULATE) {
12129e898211SDavid Hildenbrand vma = vma_lookup(mm, start);
12139e898211SDavid Hildenbrand if (!vma) {
12149e898211SDavid Hildenbrand ret = -ENOMEM;
12159e898211SDavid Hildenbrand goto out;
12169e898211SDavid Hildenbrand }
12179e898211SDavid Hildenbrand if (check_vma_flags(vma, gup_flags)) {
12189e898211SDavid Hildenbrand ret = -EINVAL;
12199e898211SDavid Hildenbrand goto out;
12209e898211SDavid Hildenbrand }
12219e898211SDavid Hildenbrand goto retry;
12229e898211SDavid Hildenbrand }
12236cd06ab1SLinus Torvalds vma = gup_vma_lookup(mm, start);
1224fa5bb209SKirill A. Shutemov if (!vma && in_gate_area(mm, start)) {
1225fa5bb209SKirill A. Shutemov ret = get_gate_page(mm, start & PAGE_MASK,
1226fa5bb209SKirill A. Shutemov gup_flags, &vma,
1227ffe1e786SPeter Xu pages ? &page : NULL);
1228fa5bb209SKirill A. Shutemov if (ret)
122908be37b7SJohn Hubbard goto out;
1230df06b37fSKeith Busch ctx.page_mask = 0;
1231fa5bb209SKirill A. Shutemov goto next_page;
1232fa5bb209SKirill A. Shutemov }
1233fa5bb209SKirill A. Shutemov
123452650c8bSJason Gunthorpe if (!vma) {
1235df06b37fSKeith Busch ret = -EFAULT;
1236df06b37fSKeith Busch goto out;
1237df06b37fSKeith Busch }
123852650c8bSJason Gunthorpe ret = check_vma_flags(vma, gup_flags);
123952650c8bSJason Gunthorpe if (ret)
124052650c8bSJason Gunthorpe goto out;
1241fa5bb209SKirill A. Shutemov }
1242fa5bb209SKirill A. Shutemov retry:
12434bbd4c77SKirill A. Shutemov /*
1244fa5bb209SKirill A. Shutemov * If we have a pending SIGKILL, don't keep faulting pages and
1245fa5bb209SKirill A. Shutemov * potentially allocating memory.
12464bbd4c77SKirill A. Shutemov */
1247fa45f116SDavidlohr Bueso if (fatal_signal_pending(current)) {
1248d180870dSMichal Hocko ret = -EINTR;
1249df06b37fSKeith Busch goto out;
1250df06b37fSKeith Busch }
12514bbd4c77SKirill A. Shutemov cond_resched();
1252df06b37fSKeith Busch
1253df06b37fSKeith Busch page = follow_page_mask(vma, start, foll_flags, &ctx);
1254a7f22660SDavid Hildenbrand if (!page || PTR_ERR(page) == -EMLINK) {
1255a7f22660SDavid Hildenbrand ret = faultin_page(vma, start, &foll_flags,
1256a7f22660SDavid Hildenbrand PTR_ERR(page) == -EMLINK, locked);
125716744483SKirill A. Shutemov switch (ret) {
125816744483SKirill A. Shutemov case 0:
1259fa5bb209SKirill A. Shutemov goto retry;
1260df06b37fSKeith Busch case -EBUSY:
1261d9272525SPeter Xu case -EAGAIN:
1262df06b37fSKeith Busch ret = 0;
1263e4a9bc58SJoe Perches fallthrough;
126416744483SKirill A. Shutemov case -EFAULT:
126516744483SKirill A. Shutemov case -ENOMEM:
126616744483SKirill A. Shutemov case -EHWPOISON:
1267df06b37fSKeith Busch goto out;
12684bbd4c77SKirill A. Shutemov }
1269fa5bb209SKirill A. Shutemov BUG();
12701027e443SKirill A. Shutemov } else if (PTR_ERR(page) == -EEXIST) {
12711027e443SKirill A. Shutemov /*
12721027e443SKirill A. Shutemov * Proper page table entry exists, but no corresponding
127365462462SJohn Hubbard * struct page. If the caller expects **pages to be
127465462462SJohn Hubbard * filled in, bail out now, because that can't be done
127565462462SJohn Hubbard * for this page.
12761027e443SKirill A. Shutemov */
127765462462SJohn Hubbard if (pages) {
127865462462SJohn Hubbard ret = PTR_ERR(page);
127965462462SJohn Hubbard goto out;
128065462462SJohn Hubbard }
12811027e443SKirill A. Shutemov } else if (IS_ERR(page)) {
1282df06b37fSKeith Busch ret = PTR_ERR(page);
1283df06b37fSKeith Busch goto out;
12841027e443SKirill A. Shutemov }
1285ffe1e786SPeter Xu next_page:
1286df06b37fSKeith Busch page_increm = 1 + (~(start >> PAGE_SHIFT) & ctx.page_mask);
12874bbd4c77SKirill A. Shutemov if (page_increm > nr_pages)
12884bbd4c77SKirill A. Shutemov page_increm = nr_pages;
128957edfcfdSPeter Xu
129057edfcfdSPeter Xu if (pages) {
129157edfcfdSPeter Xu struct page *subpage;
129257edfcfdSPeter Xu unsigned int j;
129357edfcfdSPeter Xu
129457edfcfdSPeter Xu /*
129557edfcfdSPeter Xu * This must be a large folio (and doesn't need to
129657edfcfdSPeter Xu * be the whole folio; it can be part of it), do
129757edfcfdSPeter Xu * the refcount work for all the subpages too.
129857edfcfdSPeter Xu *
129957edfcfdSPeter Xu * NOTE: here the page may not be the head page
130057edfcfdSPeter Xu * e.g. when start addr is not thp-size aligned.
130157edfcfdSPeter Xu * try_grab_folio() should have taken care of tail
130257edfcfdSPeter Xu * pages.
130357edfcfdSPeter Xu */
130457edfcfdSPeter Xu if (page_increm > 1) {
130557edfcfdSPeter Xu struct folio *folio;
130657edfcfdSPeter Xu
130757edfcfdSPeter Xu /*
130857edfcfdSPeter Xu * Since we already hold refcount on the
130957edfcfdSPeter Xu * large folio, this should never fail.
131057edfcfdSPeter Xu */
131157edfcfdSPeter Xu folio = try_grab_folio(page, page_increm - 1,
131257edfcfdSPeter Xu foll_flags);
131357edfcfdSPeter Xu if (WARN_ON_ONCE(!folio)) {
131457edfcfdSPeter Xu /*
131557edfcfdSPeter Xu * Release the 1st page ref if the
131657edfcfdSPeter Xu * folio is problematic, fail hard.
131757edfcfdSPeter Xu */
131857edfcfdSPeter Xu gup_put_folio(page_folio(page), 1,
131957edfcfdSPeter Xu foll_flags);
132057edfcfdSPeter Xu ret = -EFAULT;
132157edfcfdSPeter Xu goto out;
132257edfcfdSPeter Xu }
132357edfcfdSPeter Xu }
132457edfcfdSPeter Xu
132557edfcfdSPeter Xu for (j = 0; j < page_increm; j++) {
132657edfcfdSPeter Xu subpage = nth_page(page, j);
132757edfcfdSPeter Xu pages[i + j] = subpage;
132857edfcfdSPeter Xu flush_anon_page(vma, subpage, start + j * PAGE_SIZE);
132957edfcfdSPeter Xu flush_dcache_page(subpage);
133057edfcfdSPeter Xu }
133157edfcfdSPeter Xu }
133257edfcfdSPeter Xu
13334bbd4c77SKirill A. Shutemov i += page_increm;
13344bbd4c77SKirill A. Shutemov start += page_increm * PAGE_SIZE;
13354bbd4c77SKirill A. Shutemov nr_pages -= page_increm;
13364bbd4c77SKirill A. Shutemov } while (nr_pages);
1337df06b37fSKeith Busch out:
1338df06b37fSKeith Busch if (ctx.pgmap)
1339df06b37fSKeith Busch put_dev_pagemap(ctx.pgmap);
1340df06b37fSKeith Busch return i ? i : ret;
13414bbd4c77SKirill A. Shutemov }
13424bbd4c77SKirill A. Shutemov
vma_permits_fault(struct vm_area_struct * vma,unsigned int fault_flags)1343771ab430STobias Klauser static bool vma_permits_fault(struct vm_area_struct *vma,
1344771ab430STobias Klauser unsigned int fault_flags)
1345d4925e00SDave Hansen {
134633a709b2SDave Hansen bool write = !!(fault_flags & FAULT_FLAG_WRITE);
13471b2ee126SDave Hansen bool foreign = !!(fault_flags & FAULT_FLAG_REMOTE);
134833a709b2SDave Hansen vm_flags_t vm_flags = write ? VM_WRITE : VM_READ;
1349d4925e00SDave Hansen
1350d4925e00SDave Hansen if (!(vm_flags & vma->vm_flags))
1351d4925e00SDave Hansen return false;
1352d4925e00SDave Hansen
135333a709b2SDave Hansen /*
135433a709b2SDave Hansen * The architecture might have a hardware protection
13551b2ee126SDave Hansen * mechanism other than read/write that can deny access.
1356d61172b4SDave Hansen *
1357d61172b4SDave Hansen * gup always represents data access, not instruction
1358d61172b4SDave Hansen * fetches, so execute=false here:
135933a709b2SDave Hansen */
1360d61172b4SDave Hansen if (!arch_vma_access_permitted(vma, write, false, foreign))
136133a709b2SDave Hansen return false;
136233a709b2SDave Hansen
1363d4925e00SDave Hansen return true;
1364d4925e00SDave Hansen }
1365d4925e00SDave Hansen
1366adc8cb40SSouptick Joarder /**
13674bbd4c77SKirill A. Shutemov * fixup_user_fault() - manually resolve a user page fault
13684bbd4c77SKirill A. Shutemov * @mm: mm_struct of target mm
13694bbd4c77SKirill A. Shutemov * @address: user address
13704bbd4c77SKirill A. Shutemov * @fault_flags:flags to pass down to handle_mm_fault()
1371c1e8d7c6SMichel Lespinasse * @unlocked: did we unlock the mmap_lock while retrying, maybe NULL if caller
1372548b6a1eSMiles Chen * does not allow retry. If NULL, the caller must guarantee
1373548b6a1eSMiles Chen * that fault_flags does not contain FAULT_FLAG_ALLOW_RETRY.
13744bbd4c77SKirill A. Shutemov *
13754bbd4c77SKirill A. Shutemov * This is meant to be called in the specific scenario where for locking reasons
13764bbd4c77SKirill A. Shutemov * we try to access user memory in atomic context (within a pagefault_disable()
13774bbd4c77SKirill A. Shutemov * section), this returns -EFAULT, and we want to resolve the user fault before
13784bbd4c77SKirill A. Shutemov * trying again.
13794bbd4c77SKirill A. Shutemov *
13804bbd4c77SKirill A. Shutemov * Typically this is meant to be used by the futex code.
13814bbd4c77SKirill A. Shutemov *
13824bbd4c77SKirill A. Shutemov * The main difference with get_user_pages() is that this function will
13834bbd4c77SKirill A. Shutemov * unconditionally call handle_mm_fault() which will in turn perform all the
13844bbd4c77SKirill A. Shutemov * necessary SW fixup of the dirty and young bits in the PTE, while
13854a9e1cdaSDominik Dingel * get_user_pages() only guarantees to update these in the struct page.
13864bbd4c77SKirill A. Shutemov *
13874bbd4c77SKirill A. Shutemov * This is important for some architectures where those bits also gate the
13884bbd4c77SKirill A. Shutemov * access permission to the page because they are maintained in software. On
13894bbd4c77SKirill A. Shutemov * such architectures, gup() will not be enough to make a subsequent access
13904bbd4c77SKirill A. Shutemov * succeed.
13914bbd4c77SKirill A. Shutemov *
1392c1e8d7c6SMichel Lespinasse * This function will not return with an unlocked mmap_lock. So it has not the
1393c1e8d7c6SMichel Lespinasse * same semantics wrt the @mm->mmap_lock as does filemap_fault().
13944bbd4c77SKirill A. Shutemov */
fixup_user_fault(struct mm_struct * mm,unsigned long address,unsigned int fault_flags,bool * unlocked)139564019a2eSPeter Xu int fixup_user_fault(struct mm_struct *mm,
13964a9e1cdaSDominik Dingel unsigned long address, unsigned int fault_flags,
13974a9e1cdaSDominik Dingel bool *unlocked)
13984bbd4c77SKirill A. Shutemov {
13994bbd4c77SKirill A. Shutemov struct vm_area_struct *vma;
14008fed2f3cSMiaohe Lin vm_fault_t ret;
14014bbd4c77SKirill A. Shutemov
1402428e106aSKirill A. Shutemov address = untagged_addr_remote(mm, address);
1403f9652594SAndrey Konovalov
14044a9e1cdaSDominik Dingel if (unlocked)
140571335f37SPeter Xu fault_flags |= FAULT_FLAG_ALLOW_RETRY | FAULT_FLAG_KILLABLE;
14064a9e1cdaSDominik Dingel
14074a9e1cdaSDominik Dingel retry:
14086cd06ab1SLinus Torvalds vma = gup_vma_lookup(mm, address);
14098d7071afSLinus Torvalds if (!vma)
14104bbd4c77SKirill A. Shutemov return -EFAULT;
14114bbd4c77SKirill A. Shutemov
1412d4925e00SDave Hansen if (!vma_permits_fault(vma, fault_flags))
14134bbd4c77SKirill A. Shutemov return -EFAULT;
14144bbd4c77SKirill A. Shutemov
1415475f4dfcSPeter Xu if ((fault_flags & FAULT_FLAG_KILLABLE) &&
1416475f4dfcSPeter Xu fatal_signal_pending(current))
1417475f4dfcSPeter Xu return -EINTR;
1418475f4dfcSPeter Xu
1419bce617edSPeter Xu ret = handle_mm_fault(vma, address, fault_flags, NULL);
1420d9272525SPeter Xu
1421d9272525SPeter Xu if (ret & VM_FAULT_COMPLETED) {
1422d9272525SPeter Xu /*
1423d9272525SPeter Xu * NOTE: it's a pity that we need to retake the lock here
1424d9272525SPeter Xu * to pair with the unlock() in the callers. Ideally we
1425d9272525SPeter Xu * could tell the callers so they do not need to unlock.
1426d9272525SPeter Xu */
1427d9272525SPeter Xu mmap_read_lock(mm);
1428d9272525SPeter Xu *unlocked = true;
1429d9272525SPeter Xu return 0;
1430d9272525SPeter Xu }
1431d9272525SPeter Xu
14324bbd4c77SKirill A. Shutemov if (ret & VM_FAULT_ERROR) {
14339a291a7cSJames Morse int err = vm_fault_to_errno(ret, 0);
14349a291a7cSJames Morse
14359a291a7cSJames Morse if (err)
14369a291a7cSJames Morse return err;
14374bbd4c77SKirill A. Shutemov BUG();
14384bbd4c77SKirill A. Shutemov }
14394a9e1cdaSDominik Dingel
14404a9e1cdaSDominik Dingel if (ret & VM_FAULT_RETRY) {
1441d8ed45c5SMichel Lespinasse mmap_read_lock(mm);
14424a9e1cdaSDominik Dingel *unlocked = true;
14434a9e1cdaSDominik Dingel fault_flags |= FAULT_FLAG_TRIED;
14444a9e1cdaSDominik Dingel goto retry;
14454a9e1cdaSDominik Dingel }
14464a9e1cdaSDominik Dingel
14474bbd4c77SKirill A. Shutemov return 0;
14484bbd4c77SKirill A. Shutemov }
1449add6a0cdSPaolo Bonzini EXPORT_SYMBOL_GPL(fixup_user_fault);
14504bbd4c77SKirill A. Shutemov
14512d3a36a4SMichal Hocko /*
145293c5c61dSPeter Xu * GUP always responds to fatal signals. When FOLL_INTERRUPTIBLE is
145393c5c61dSPeter Xu * specified, it'll also respond to generic signals. The caller of GUP
145493c5c61dSPeter Xu * that has FOLL_INTERRUPTIBLE should take care of the GUP interruption.
145593c5c61dSPeter Xu */
gup_signal_pending(unsigned int flags)145693c5c61dSPeter Xu static bool gup_signal_pending(unsigned int flags)
145793c5c61dSPeter Xu {
145893c5c61dSPeter Xu if (fatal_signal_pending(current))
145993c5c61dSPeter Xu return true;
146093c5c61dSPeter Xu
146193c5c61dSPeter Xu if (!(flags & FOLL_INTERRUPTIBLE))
146293c5c61dSPeter Xu return false;
146393c5c61dSPeter Xu
146493c5c61dSPeter Xu return signal_pending(current);
146593c5c61dSPeter Xu }
146693c5c61dSPeter Xu
146793c5c61dSPeter Xu /*
1468b2a72dffSJason Gunthorpe * Locking: (*locked == 1) means that the mmap_lock has already been acquired by
1469b2a72dffSJason Gunthorpe * the caller. This function may drop the mmap_lock. If it does so, then it will
1470b2a72dffSJason Gunthorpe * set (*locked = 0).
1471b2a72dffSJason Gunthorpe *
1472b2a72dffSJason Gunthorpe * (*locked == 0) means that the caller expects this function to acquire and
1473b2a72dffSJason Gunthorpe * drop the mmap_lock. Therefore, the value of *locked will still be zero when
1474b2a72dffSJason Gunthorpe * the function returns, even though it may have changed temporarily during
1475b2a72dffSJason Gunthorpe * function execution.
1476b2a72dffSJason Gunthorpe *
1477b2a72dffSJason Gunthorpe * Please note that this function, unlike __get_user_pages(), will not return 0
1478b2a72dffSJason Gunthorpe * for nr_pages > 0, unless FOLL_NOWAIT is used.
14792d3a36a4SMichal Hocko */
__get_user_pages_locked(struct mm_struct * mm,unsigned long start,unsigned long nr_pages,struct page ** pages,int * locked,unsigned int flags)148064019a2eSPeter Xu static __always_inline long __get_user_pages_locked(struct mm_struct *mm,
1481f0818f47SAndrea Arcangeli unsigned long start,
1482f0818f47SAndrea Arcangeli unsigned long nr_pages,
1483f0818f47SAndrea Arcangeli struct page **pages,
1484e716712fSAl Viro int *locked,
14850fd71a56SAndrea Arcangeli unsigned int flags)
1486f0818f47SAndrea Arcangeli {
1487f0818f47SAndrea Arcangeli long ret, pages_done;
1488b2a72dffSJason Gunthorpe bool must_unlock = false;
1489f0818f47SAndrea Arcangeli
1490b2a72dffSJason Gunthorpe /*
1491b2a72dffSJason Gunthorpe * The internal caller expects GUP to manage the lock internally and the
1492b2a72dffSJason Gunthorpe * lock must be released when this returns.
1493b2a72dffSJason Gunthorpe */
14949a863a6aSJason Gunthorpe if (!*locked) {
1495b2a72dffSJason Gunthorpe if (mmap_read_lock_killable(mm))
1496b2a72dffSJason Gunthorpe return -EAGAIN;
1497b2a72dffSJason Gunthorpe must_unlock = true;
1498b2a72dffSJason Gunthorpe *locked = 1;
1499f0818f47SAndrea Arcangeli }
1500961ba472SJason Gunthorpe else
1501961ba472SJason Gunthorpe mmap_assert_locked(mm);
1502f0818f47SAndrea Arcangeli
1503a458b76aSAndrea Arcangeli if (flags & FOLL_PIN)
1504a458b76aSAndrea Arcangeli mm_set_has_pinned_flag(&mm->flags);
1505008cfe44SPeter Xu
1506eddb1c22SJohn Hubbard /*
1507eddb1c22SJohn Hubbard * FOLL_PIN and FOLL_GET are mutually exclusive. Traditional behavior
1508eddb1c22SJohn Hubbard * is to set FOLL_GET if the caller wants pages[] filled in (but has
1509eddb1c22SJohn Hubbard * carelessly failed to specify FOLL_GET), so keep doing that, but only
1510eddb1c22SJohn Hubbard * for FOLL_GET, not for the newer FOLL_PIN.
1511eddb1c22SJohn Hubbard *
1512eddb1c22SJohn Hubbard * FOLL_PIN always expects pages to be non-null, but no need to assert
1513eddb1c22SJohn Hubbard * that here, as any failures will be obvious enough.
1514eddb1c22SJohn Hubbard */
1515eddb1c22SJohn Hubbard if (pages && !(flags & FOLL_PIN))
1516f0818f47SAndrea Arcangeli flags |= FOLL_GET;
1517f0818f47SAndrea Arcangeli
1518f0818f47SAndrea Arcangeli pages_done = 0;
1519f0818f47SAndrea Arcangeli for (;;) {
152064019a2eSPeter Xu ret = __get_user_pages(mm, start, nr_pages, flags, pages,
1521b2cac248SLorenzo Stoakes locked);
1522f04740f5SJason Gunthorpe if (!(flags & FOLL_UNLOCKABLE)) {
1523f0818f47SAndrea Arcangeli /* VM_FAULT_RETRY couldn't trigger, bypass */
1524f04740f5SJason Gunthorpe pages_done = ret;
1525f04740f5SJason Gunthorpe break;
1526f04740f5SJason Gunthorpe }
1527f0818f47SAndrea Arcangeli
1528d9272525SPeter Xu /* VM_FAULT_RETRY or VM_FAULT_COMPLETED cannot return errors */
1529f0818f47SAndrea Arcangeli if (!*locked) {
1530f0818f47SAndrea Arcangeli BUG_ON(ret < 0);
1531f0818f47SAndrea Arcangeli BUG_ON(ret >= nr_pages);
1532f0818f47SAndrea Arcangeli }
1533f0818f47SAndrea Arcangeli
1534f0818f47SAndrea Arcangeli if (ret > 0) {
1535f0818f47SAndrea Arcangeli nr_pages -= ret;
1536f0818f47SAndrea Arcangeli pages_done += ret;
1537f0818f47SAndrea Arcangeli if (!nr_pages)
1538f0818f47SAndrea Arcangeli break;
1539f0818f47SAndrea Arcangeli }
1540f0818f47SAndrea Arcangeli if (*locked) {
154196312e61SAndrea Arcangeli /*
154296312e61SAndrea Arcangeli * VM_FAULT_RETRY didn't trigger or it was a
154396312e61SAndrea Arcangeli * FOLL_NOWAIT.
154496312e61SAndrea Arcangeli */
1545f0818f47SAndrea Arcangeli if (!pages_done)
1546f0818f47SAndrea Arcangeli pages_done = ret;
1547f0818f47SAndrea Arcangeli break;
1548f0818f47SAndrea Arcangeli }
1549df17277bSMike Rapoport /*
1550df17277bSMike Rapoport * VM_FAULT_RETRY triggered, so seek to the faulting offset.
1551df17277bSMike Rapoport * For the prefault case (!pages) we only update counts.
1552df17277bSMike Rapoport */
1553df17277bSMike Rapoport if (likely(pages))
1554f0818f47SAndrea Arcangeli pages += ret;
1555f0818f47SAndrea Arcangeli start += ret << PAGE_SHIFT;
1556b2a72dffSJason Gunthorpe
1557b2a72dffSJason Gunthorpe /* The lock was temporarily dropped, so we must unlock later */
1558b2a72dffSJason Gunthorpe must_unlock = true;
1559f0818f47SAndrea Arcangeli
15604426e945SPeter Xu retry:
1561f0818f47SAndrea Arcangeli /*
1562f0818f47SAndrea Arcangeli * Repeat on the address that fired VM_FAULT_RETRY
15634426e945SPeter Xu * with both FAULT_FLAG_ALLOW_RETRY and
15644426e945SPeter Xu * FAULT_FLAG_TRIED. Note that GUP can be interrupted
156593c5c61dSPeter Xu * by fatal signals of even common signals, depending on
156693c5c61dSPeter Xu * the caller's request. So we need to check it before we
15674426e945SPeter Xu * start trying again otherwise it can loop forever.
1568f0818f47SAndrea Arcangeli */
156993c5c61dSPeter Xu if (gup_signal_pending(flags)) {
1570ae46d2aaSHillf Danton if (!pages_done)
1571ae46d2aaSHillf Danton pages_done = -EINTR;
15724426e945SPeter Xu break;
1573ae46d2aaSHillf Danton }
15744426e945SPeter Xu
1575d8ed45c5SMichel Lespinasse ret = mmap_read_lock_killable(mm);
157671335f37SPeter Xu if (ret) {
157771335f37SPeter Xu BUG_ON(ret > 0);
157871335f37SPeter Xu if (!pages_done)
157971335f37SPeter Xu pages_done = ret;
158071335f37SPeter Xu break;
158171335f37SPeter Xu }
15824426e945SPeter Xu
1583c7b6a566SPeter Xu *locked = 1;
158464019a2eSPeter Xu ret = __get_user_pages(mm, start, 1, flags | FOLL_TRIED,
1585b2cac248SLorenzo Stoakes pages, locked);
15864426e945SPeter Xu if (!*locked) {
15874426e945SPeter Xu /* Continue to retry until we succeeded */
15884426e945SPeter Xu BUG_ON(ret != 0);
15894426e945SPeter Xu goto retry;
15904426e945SPeter Xu }
1591f0818f47SAndrea Arcangeli if (ret != 1) {
1592f0818f47SAndrea Arcangeli BUG_ON(ret > 1);
1593f0818f47SAndrea Arcangeli if (!pages_done)
1594f0818f47SAndrea Arcangeli pages_done = ret;
1595f0818f47SAndrea Arcangeli break;
1596f0818f47SAndrea Arcangeli }
1597f0818f47SAndrea Arcangeli nr_pages--;
1598f0818f47SAndrea Arcangeli pages_done++;
1599f0818f47SAndrea Arcangeli if (!nr_pages)
1600f0818f47SAndrea Arcangeli break;
1601df17277bSMike Rapoport if (likely(pages))
1602f0818f47SAndrea Arcangeli pages++;
1603f0818f47SAndrea Arcangeli start += PAGE_SIZE;
1604f0818f47SAndrea Arcangeli }
1605b2a72dffSJason Gunthorpe if (must_unlock && *locked) {
1606f0818f47SAndrea Arcangeli /*
1607b2a72dffSJason Gunthorpe * We either temporarily dropped the lock, or the caller
1608b2a72dffSJason Gunthorpe * requested that we both acquire and drop the lock. Either way,
1609b2a72dffSJason Gunthorpe * we must now unlock, and notify the caller of that state.
1610f0818f47SAndrea Arcangeli */
1611d8ed45c5SMichel Lespinasse mmap_read_unlock(mm);
1612f0818f47SAndrea Arcangeli *locked = 0;
1613f0818f47SAndrea Arcangeli }
1614f0818f47SAndrea Arcangeli return pages_done;
1615f0818f47SAndrea Arcangeli }
1616f0818f47SAndrea Arcangeli
1617d3649f68SChristoph Hellwig /**
1618d3649f68SChristoph Hellwig * populate_vma_page_range() - populate a range of pages in the vma.
1619d3649f68SChristoph Hellwig * @vma: target vma
1620d3649f68SChristoph Hellwig * @start: start address
1621d3649f68SChristoph Hellwig * @end: end address
1622c1e8d7c6SMichel Lespinasse * @locked: whether the mmap_lock is still held
1623d3649f68SChristoph Hellwig *
1624d3649f68SChristoph Hellwig * This takes care of mlocking the pages too if VM_LOCKED is set.
1625d3649f68SChristoph Hellwig *
16260a36f7f8STang Yizhou * Return either number of pages pinned in the vma, or a negative error
16270a36f7f8STang Yizhou * code on error.
1628d3649f68SChristoph Hellwig *
1629c1e8d7c6SMichel Lespinasse * vma->vm_mm->mmap_lock must be held.
1630d3649f68SChristoph Hellwig *
16314f6da934SPeter Xu * If @locked is NULL, it may be held for read or write and will
1632d3649f68SChristoph Hellwig * be unperturbed.
1633d3649f68SChristoph Hellwig *
16344f6da934SPeter Xu * If @locked is non-NULL, it must held for read only and may be
16354f6da934SPeter Xu * released. If it's released, *@locked will be set to 0.
1636d3649f68SChristoph Hellwig */
populate_vma_page_range(struct vm_area_struct * vma,unsigned long start,unsigned long end,int * locked)1637d3649f68SChristoph Hellwig long populate_vma_page_range(struct vm_area_struct *vma,
16384f6da934SPeter Xu unsigned long start, unsigned long end, int *locked)
1639d3649f68SChristoph Hellwig {
1640d3649f68SChristoph Hellwig struct mm_struct *mm = vma->vm_mm;
1641d3649f68SChristoph Hellwig unsigned long nr_pages = (end - start) / PAGE_SIZE;
16429a863a6aSJason Gunthorpe int local_locked = 1;
1643d3649f68SChristoph Hellwig int gup_flags;
1644ece369c7SHugh Dickins long ret;
1645d3649f68SChristoph Hellwig
1646be51eb18SMiaohe Lin VM_BUG_ON(!PAGE_ALIGNED(start));
1647be51eb18SMiaohe Lin VM_BUG_ON(!PAGE_ALIGNED(end));
1648d3649f68SChristoph Hellwig VM_BUG_ON_VMA(start < vma->vm_start, vma);
1649d3649f68SChristoph Hellwig VM_BUG_ON_VMA(end > vma->vm_end, vma);
165042fc5414SMichel Lespinasse mmap_assert_locked(mm);
1651d3649f68SChristoph Hellwig
1652b67bf49cSHugh Dickins /*
1653b67bf49cSHugh Dickins * Rightly or wrongly, the VM_LOCKONFAULT case has never used
1654b67bf49cSHugh Dickins * faultin_page() to break COW, so it has no work to do here.
1655b67bf49cSHugh Dickins */
1656d3649f68SChristoph Hellwig if (vma->vm_flags & VM_LOCKONFAULT)
1657b67bf49cSHugh Dickins return nr_pages;
1658b67bf49cSHugh Dickins
1659b67bf49cSHugh Dickins gup_flags = FOLL_TOUCH;
1660d3649f68SChristoph Hellwig /*
1661d3649f68SChristoph Hellwig * We want to touch writable mappings with a write fault in order
1662d3649f68SChristoph Hellwig * to break COW, except for shared mappings because these don't COW
1663d3649f68SChristoph Hellwig * and we would not want to dirty them for nothing.
1664d3649f68SChristoph Hellwig */
1665d3649f68SChristoph Hellwig if ((vma->vm_flags & (VM_WRITE | VM_SHARED)) == VM_WRITE)
1666d3649f68SChristoph Hellwig gup_flags |= FOLL_WRITE;
1667d3649f68SChristoph Hellwig
1668d3649f68SChristoph Hellwig /*
1669d3649f68SChristoph Hellwig * We want mlock to succeed for regions that have any permissions
1670d3649f68SChristoph Hellwig * other than PROT_NONE.
1671d3649f68SChristoph Hellwig */
16723122e80eSAnshuman Khandual if (vma_is_accessible(vma))
1673d3649f68SChristoph Hellwig gup_flags |= FOLL_FORCE;
1674d3649f68SChristoph Hellwig
1675f04740f5SJason Gunthorpe if (locked)
1676f04740f5SJason Gunthorpe gup_flags |= FOLL_UNLOCKABLE;
1677f04740f5SJason Gunthorpe
1678d3649f68SChristoph Hellwig /*
1679d3649f68SChristoph Hellwig * We made sure addr is within a VMA, so the following will
1680d3649f68SChristoph Hellwig * not result in a stack expansion that recurses back here.
1681d3649f68SChristoph Hellwig */
1682ece369c7SHugh Dickins ret = __get_user_pages(mm, start, nr_pages, gup_flags,
1683b2cac248SLorenzo Stoakes NULL, locked ? locked : &local_locked);
1684ece369c7SHugh Dickins lru_add_drain();
1685ece369c7SHugh Dickins return ret;
1686d3649f68SChristoph Hellwig }
1687d3649f68SChristoph Hellwig
1688d3649f68SChristoph Hellwig /*
16899e898211SDavid Hildenbrand * faultin_page_range() - populate (prefault) page tables inside the
16909e898211SDavid Hildenbrand * given range readable/writable
16914ca9b385SDavid Hildenbrand *
16924ca9b385SDavid Hildenbrand * This takes care of mlocking the pages, too, if VM_LOCKED is set.
16934ca9b385SDavid Hildenbrand *
16949e898211SDavid Hildenbrand * @mm: the mm to populate page tables in
16954ca9b385SDavid Hildenbrand * @start: start address
16964ca9b385SDavid Hildenbrand * @end: end address
16974ca9b385SDavid Hildenbrand * @write: whether to prefault readable or writable
16984ca9b385SDavid Hildenbrand * @locked: whether the mmap_lock is still held
16994ca9b385SDavid Hildenbrand *
17009e898211SDavid Hildenbrand * Returns either number of processed pages in the MM, or a negative error
17019e898211SDavid Hildenbrand * code on error (see __get_user_pages()). Note that this function reports
17029e898211SDavid Hildenbrand * errors related to VMAs, such as incompatible mappings, as expected by
17039e898211SDavid Hildenbrand * MADV_POPULATE_(READ|WRITE).
17044ca9b385SDavid Hildenbrand *
17059e898211SDavid Hildenbrand * The range must be page-aligned.
17069e898211SDavid Hildenbrand *
17079e898211SDavid Hildenbrand * mm->mmap_lock must be held. If it's released, *@locked will be set to 0.
17084ca9b385SDavid Hildenbrand */
faultin_page_range(struct mm_struct * mm,unsigned long start,unsigned long end,bool write,int * locked)17099e898211SDavid Hildenbrand long faultin_page_range(struct mm_struct *mm, unsigned long start,
17104ca9b385SDavid Hildenbrand unsigned long end, bool write, int *locked)
17114ca9b385SDavid Hildenbrand {
17124ca9b385SDavid Hildenbrand unsigned long nr_pages = (end - start) / PAGE_SIZE;
17134ca9b385SDavid Hildenbrand int gup_flags;
1714ece369c7SHugh Dickins long ret;
17154ca9b385SDavid Hildenbrand
17164ca9b385SDavid Hildenbrand VM_BUG_ON(!PAGE_ALIGNED(start));
17174ca9b385SDavid Hildenbrand VM_BUG_ON(!PAGE_ALIGNED(end));
17184ca9b385SDavid Hildenbrand mmap_assert_locked(mm);
17194ca9b385SDavid Hildenbrand
17204ca9b385SDavid Hildenbrand /*
17214ca9b385SDavid Hildenbrand * FOLL_TOUCH: Mark page accessed and thereby young; will also mark
17224ca9b385SDavid Hildenbrand * the page dirty with FOLL_WRITE -- which doesn't make a
17234ca9b385SDavid Hildenbrand * difference with !FOLL_FORCE, because the page is writable
17244ca9b385SDavid Hildenbrand * in the page table.
17254ca9b385SDavid Hildenbrand * FOLL_HWPOISON: Return -EHWPOISON instead of -EFAULT when we hit
17264ca9b385SDavid Hildenbrand * a poisoned page.
17274ca9b385SDavid Hildenbrand * !FOLL_FORCE: Require proper access permissions.
17284ca9b385SDavid Hildenbrand */
17299e898211SDavid Hildenbrand gup_flags = FOLL_TOUCH | FOLL_HWPOISON | FOLL_UNLOCKABLE |
17309e898211SDavid Hildenbrand FOLL_MADV_POPULATE;
17314ca9b385SDavid Hildenbrand if (write)
17324ca9b385SDavid Hildenbrand gup_flags |= FOLL_WRITE;
17334ca9b385SDavid Hildenbrand
17349e898211SDavid Hildenbrand ret = __get_user_pages_locked(mm, start, nr_pages, NULL, locked,
17359e898211SDavid Hildenbrand gup_flags);
1736ece369c7SHugh Dickins lru_add_drain();
1737ece369c7SHugh Dickins return ret;
17384ca9b385SDavid Hildenbrand }
17394ca9b385SDavid Hildenbrand
17404ca9b385SDavid Hildenbrand /*
1741d3649f68SChristoph Hellwig * __mm_populate - populate and/or mlock pages within a range of address space.
1742d3649f68SChristoph Hellwig *
1743d3649f68SChristoph Hellwig * This is used to implement mlock() and the MAP_POPULATE / MAP_LOCKED mmap
1744d3649f68SChristoph Hellwig * flags. VMAs must be already marked with the desired vm_flags, and
1745c1e8d7c6SMichel Lespinasse * mmap_lock must not be held.
1746d3649f68SChristoph Hellwig */
__mm_populate(unsigned long start,unsigned long len,int ignore_errors)1747d3649f68SChristoph Hellwig int __mm_populate(unsigned long start, unsigned long len, int ignore_errors)
1748d3649f68SChristoph Hellwig {
1749d3649f68SChristoph Hellwig struct mm_struct *mm = current->mm;
1750d3649f68SChristoph Hellwig unsigned long end, nstart, nend;
1751d3649f68SChristoph Hellwig struct vm_area_struct *vma = NULL;
1752d3649f68SChristoph Hellwig int locked = 0;
1753d3649f68SChristoph Hellwig long ret = 0;
1754d3649f68SChristoph Hellwig
1755d3649f68SChristoph Hellwig end = start + len;
1756d3649f68SChristoph Hellwig
1757d3649f68SChristoph Hellwig for (nstart = start; nstart < end; nstart = nend) {
1758d3649f68SChristoph Hellwig /*
1759d3649f68SChristoph Hellwig * We want to fault in pages for [nstart; end) address range.
1760d3649f68SChristoph Hellwig * Find first corresponding VMA.
1761d3649f68SChristoph Hellwig */
1762d3649f68SChristoph Hellwig if (!locked) {
1763d3649f68SChristoph Hellwig locked = 1;
1764d8ed45c5SMichel Lespinasse mmap_read_lock(mm);
1765c4d1a92dSLiam R. Howlett vma = find_vma_intersection(mm, nstart, end);
1766d3649f68SChristoph Hellwig } else if (nstart >= vma->vm_end)
1767c4d1a92dSLiam R. Howlett vma = find_vma_intersection(mm, vma->vm_end, end);
1768c4d1a92dSLiam R. Howlett
1769c4d1a92dSLiam R. Howlett if (!vma)
1770d3649f68SChristoph Hellwig break;
1771d3649f68SChristoph Hellwig /*
1772d3649f68SChristoph Hellwig * Set [nstart; nend) to intersection of desired address
1773d3649f68SChristoph Hellwig * range with the first VMA. Also, skip undesirable VMA types.
1774d3649f68SChristoph Hellwig */
1775d3649f68SChristoph Hellwig nend = min(end, vma->vm_end);
1776d3649f68SChristoph Hellwig if (vma->vm_flags & (VM_IO | VM_PFNMAP))
1777d3649f68SChristoph Hellwig continue;
1778d3649f68SChristoph Hellwig if (nstart < vma->vm_start)
1779d3649f68SChristoph Hellwig nstart = vma->vm_start;
1780d3649f68SChristoph Hellwig /*
1781d3649f68SChristoph Hellwig * Now fault in a range of pages. populate_vma_page_range()
1782d3649f68SChristoph Hellwig * double checks the vma flags, so that it won't mlock pages
1783d3649f68SChristoph Hellwig * if the vma was already munlocked.
1784d3649f68SChristoph Hellwig */
1785d3649f68SChristoph Hellwig ret = populate_vma_page_range(vma, nstart, nend, &locked);
1786d3649f68SChristoph Hellwig if (ret < 0) {
1787d3649f68SChristoph Hellwig if (ignore_errors) {
1788d3649f68SChristoph Hellwig ret = 0;
1789d3649f68SChristoph Hellwig continue; /* continue at next VMA */
1790d3649f68SChristoph Hellwig }
1791d3649f68SChristoph Hellwig break;
1792d3649f68SChristoph Hellwig }
1793d3649f68SChristoph Hellwig nend = nstart + ret * PAGE_SIZE;
1794d3649f68SChristoph Hellwig ret = 0;
1795d3649f68SChristoph Hellwig }
1796d3649f68SChristoph Hellwig if (locked)
1797d8ed45c5SMichel Lespinasse mmap_read_unlock(mm);
1798d3649f68SChristoph Hellwig return ret; /* 0 or negative error code */
1799d3649f68SChristoph Hellwig }
1800050a9adcSChristoph 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)180164019a2eSPeter Xu static long __get_user_pages_locked(struct mm_struct *mm, unsigned long start,
1802050a9adcSChristoph Hellwig unsigned long nr_pages, struct page **pages,
1803b2cac248SLorenzo Stoakes int *locked, unsigned int foll_flags)
1804050a9adcSChristoph Hellwig {
1805050a9adcSChristoph Hellwig struct vm_area_struct *vma;
1806b2a72dffSJason Gunthorpe bool must_unlock = false;
1807050a9adcSChristoph Hellwig unsigned long vm_flags;
180824dc20c7SPavel Tatashin long i;
1809050a9adcSChristoph Hellwig
1810b2a72dffSJason Gunthorpe if (!nr_pages)
1811b2a72dffSJason Gunthorpe return 0;
1812b2a72dffSJason Gunthorpe
1813b2a72dffSJason Gunthorpe /*
1814b2a72dffSJason Gunthorpe * The internal caller expects GUP to manage the lock internally and the
1815b2a72dffSJason Gunthorpe * lock must be released when this returns.
1816b2a72dffSJason Gunthorpe */
18179a863a6aSJason Gunthorpe if (!*locked) {
1818b2a72dffSJason Gunthorpe if (mmap_read_lock_killable(mm))
1819b2a72dffSJason Gunthorpe return -EAGAIN;
1820b2a72dffSJason Gunthorpe must_unlock = true;
1821b2a72dffSJason Gunthorpe *locked = 1;
1822b2a72dffSJason Gunthorpe }
1823b2a72dffSJason Gunthorpe
1824050a9adcSChristoph Hellwig /* calculate required read or write permissions.
1825050a9adcSChristoph Hellwig * If FOLL_FORCE is set, we only require the "MAY" flags.
1826050a9adcSChristoph Hellwig */
1827050a9adcSChristoph Hellwig vm_flags = (foll_flags & FOLL_WRITE) ?
1828050a9adcSChristoph Hellwig (VM_WRITE | VM_MAYWRITE) : (VM_READ | VM_MAYREAD);
1829050a9adcSChristoph Hellwig vm_flags &= (foll_flags & FOLL_FORCE) ?
1830050a9adcSChristoph Hellwig (VM_MAYREAD | VM_MAYWRITE) : (VM_READ | VM_WRITE);
1831050a9adcSChristoph Hellwig
1832050a9adcSChristoph Hellwig for (i = 0; i < nr_pages; i++) {
1833050a9adcSChristoph Hellwig vma = find_vma(mm, start);
1834050a9adcSChristoph Hellwig if (!vma)
1835b2a72dffSJason Gunthorpe break;
1836050a9adcSChristoph Hellwig
1837050a9adcSChristoph Hellwig /* protect what we can, including chardevs */
1838050a9adcSChristoph Hellwig if ((vma->vm_flags & (VM_IO | VM_PFNMAP)) ||
1839050a9adcSChristoph Hellwig !(vm_flags & vma->vm_flags))
1840b2a72dffSJason Gunthorpe break;
1841050a9adcSChristoph Hellwig
1842050a9adcSChristoph Hellwig if (pages) {
1843396a400bSLinus Walleij pages[i] = virt_to_page((void *)start);
1844050a9adcSChristoph Hellwig if (pages[i])
1845050a9adcSChristoph Hellwig get_page(pages[i]);
1846050a9adcSChristoph Hellwig }
1847b2cac248SLorenzo Stoakes
1848050a9adcSChristoph Hellwig start = (start + PAGE_SIZE) & PAGE_MASK;
1849050a9adcSChristoph Hellwig }
1850050a9adcSChristoph Hellwig
1851b2a72dffSJason Gunthorpe if (must_unlock && *locked) {
1852b2a72dffSJason Gunthorpe mmap_read_unlock(mm);
1853b2a72dffSJason Gunthorpe *locked = 0;
1854b2a72dffSJason Gunthorpe }
1855050a9adcSChristoph Hellwig
1856050a9adcSChristoph Hellwig return i ? : -EFAULT;
1857050a9adcSChristoph Hellwig }
1858050a9adcSChristoph Hellwig #endif /* !CONFIG_MMU */
1859d3649f68SChristoph Hellwig
18608f942eeaSJann Horn /**
1861bb523b40SAndreas Gruenbacher * fault_in_writeable - fault in userspace address range for writing
1862bb523b40SAndreas Gruenbacher * @uaddr: start of address range
1863bb523b40SAndreas Gruenbacher * @size: size of address range
1864bb523b40SAndreas Gruenbacher *
1865bb523b40SAndreas Gruenbacher * Returns the number of bytes not faulted in (like copy_to_user() and
1866bb523b40SAndreas Gruenbacher * copy_from_user()).
1867bb523b40SAndreas Gruenbacher */
fault_in_writeable(char __user * uaddr,size_t size)1868bb523b40SAndreas Gruenbacher size_t fault_in_writeable(char __user *uaddr, size_t size)
1869bb523b40SAndreas Gruenbacher {
1870bb523b40SAndreas Gruenbacher char __user *start = uaddr, *end;
1871bb523b40SAndreas Gruenbacher
1872bb523b40SAndreas Gruenbacher if (unlikely(size == 0))
1873bb523b40SAndreas Gruenbacher return 0;
1874677b2a8cSChristophe Leroy if (!user_write_access_begin(uaddr, size))
1875bb523b40SAndreas Gruenbacher return size;
1876677b2a8cSChristophe Leroy if (!PAGE_ALIGNED(uaddr)) {
1877677b2a8cSChristophe Leroy unsafe_put_user(0, uaddr, out);
1878bb523b40SAndreas Gruenbacher uaddr = (char __user *)PAGE_ALIGN((unsigned long)uaddr);
1879bb523b40SAndreas Gruenbacher }
1880bb523b40SAndreas Gruenbacher end = (char __user *)PAGE_ALIGN((unsigned long)start + size);
1881bb523b40SAndreas Gruenbacher if (unlikely(end < start))
1882bb523b40SAndreas Gruenbacher end = NULL;
1883bb523b40SAndreas Gruenbacher while (uaddr != end) {
1884677b2a8cSChristophe Leroy unsafe_put_user(0, uaddr, out);
1885bb523b40SAndreas Gruenbacher uaddr += PAGE_SIZE;
1886bb523b40SAndreas Gruenbacher }
1887bb523b40SAndreas Gruenbacher
1888bb523b40SAndreas Gruenbacher out:
1889677b2a8cSChristophe Leroy user_write_access_end();
1890bb523b40SAndreas Gruenbacher if (size > uaddr - start)
1891bb523b40SAndreas Gruenbacher return size - (uaddr - start);
1892bb523b40SAndreas Gruenbacher return 0;
1893bb523b40SAndreas Gruenbacher }
1894bb523b40SAndreas Gruenbacher EXPORT_SYMBOL(fault_in_writeable);
1895bb523b40SAndreas Gruenbacher
1896da32b581SCatalin Marinas /**
1897da32b581SCatalin Marinas * fault_in_subpage_writeable - fault in an address range for writing
1898da32b581SCatalin Marinas * @uaddr: start of address range
1899da32b581SCatalin Marinas * @size: size of address range
1900da32b581SCatalin Marinas *
1901da32b581SCatalin Marinas * Fault in a user address range for writing while checking for permissions at
1902da32b581SCatalin Marinas * sub-page granularity (e.g. arm64 MTE). This function should be used when
1903da32b581SCatalin Marinas * the caller cannot guarantee forward progress of a copy_to_user() loop.
1904da32b581SCatalin Marinas *
1905da32b581SCatalin Marinas * Returns the number of bytes not faulted in (like copy_to_user() and
1906da32b581SCatalin Marinas * copy_from_user()).
1907da32b581SCatalin Marinas */
fault_in_subpage_writeable(char __user * uaddr,size_t size)1908da32b581SCatalin Marinas size_t fault_in_subpage_writeable(char __user *uaddr, size_t size)
1909da32b581SCatalin Marinas {
1910da32b581SCatalin Marinas size_t faulted_in;
1911da32b581SCatalin Marinas
1912da32b581SCatalin Marinas /*
1913da32b581SCatalin Marinas * Attempt faulting in at page granularity first for page table
1914da32b581SCatalin Marinas * permission checking. The arch-specific probe_subpage_writeable()
1915da32b581SCatalin Marinas * functions may not check for this.
1916da32b581SCatalin Marinas */
1917da32b581SCatalin Marinas faulted_in = size - fault_in_writeable(uaddr, size);
1918da32b581SCatalin Marinas if (faulted_in)
1919da32b581SCatalin Marinas faulted_in -= probe_subpage_writeable(uaddr, faulted_in);
1920da32b581SCatalin Marinas
1921da32b581SCatalin Marinas return size - faulted_in;
1922da32b581SCatalin Marinas }
1923da32b581SCatalin Marinas EXPORT_SYMBOL(fault_in_subpage_writeable);
1924da32b581SCatalin Marinas
1925cdd591fcSAndreas Gruenbacher /*
1926cdd591fcSAndreas Gruenbacher * fault_in_safe_writeable - fault in an address range for writing
1927cdd591fcSAndreas Gruenbacher * @uaddr: start of address range
1928cdd591fcSAndreas Gruenbacher * @size: length of address range
1929cdd591fcSAndreas Gruenbacher *
1930fe673d3fSLinus Torvalds * Faults in an address range for writing. This is primarily useful when we
1931fe673d3fSLinus Torvalds * already know that some or all of the pages in the address range aren't in
1932fe673d3fSLinus Torvalds * memory.
1933cdd591fcSAndreas Gruenbacher *
1934fe673d3fSLinus Torvalds * Unlike fault_in_writeable(), this function is non-destructive.
1935cdd591fcSAndreas Gruenbacher *
1936cdd591fcSAndreas Gruenbacher * Note that we don't pin or otherwise hold the pages referenced that we fault
1937cdd591fcSAndreas Gruenbacher * in. There's no guarantee that they'll stay in memory for any duration of
1938cdd591fcSAndreas Gruenbacher * time.
1939cdd591fcSAndreas Gruenbacher *
1940cdd591fcSAndreas Gruenbacher * Returns the number of bytes not faulted in, like copy_to_user() and
1941cdd591fcSAndreas Gruenbacher * copy_from_user().
1942cdd591fcSAndreas Gruenbacher */
fault_in_safe_writeable(const char __user * uaddr,size_t size)1943cdd591fcSAndreas Gruenbacher size_t fault_in_safe_writeable(const char __user *uaddr, size_t size)
1944cdd591fcSAndreas Gruenbacher {
1945fe673d3fSLinus Torvalds unsigned long start = (unsigned long)uaddr, end;
1946cdd591fcSAndreas Gruenbacher struct mm_struct *mm = current->mm;
1947fe673d3fSLinus Torvalds bool unlocked = false;
1948cdd591fcSAndreas Gruenbacher
1949fe673d3fSLinus Torvalds if (unlikely(size == 0))
1950cdd591fcSAndreas Gruenbacher return 0;
1951fe673d3fSLinus Torvalds end = PAGE_ALIGN(start + size);
1952fe673d3fSLinus Torvalds if (end < start)
1953fe673d3fSLinus Torvalds end = 0;
1954fe673d3fSLinus Torvalds
1955fe673d3fSLinus Torvalds mmap_read_lock(mm);
1956fe673d3fSLinus Torvalds do {
1957fe673d3fSLinus Torvalds if (fixup_user_fault(mm, start, FAULT_FLAG_WRITE, &unlocked))
1958fe673d3fSLinus Torvalds break;
1959fe673d3fSLinus Torvalds start = (start + PAGE_SIZE) & PAGE_MASK;
1960fe673d3fSLinus Torvalds } while (start != end);
1961fe673d3fSLinus Torvalds mmap_read_unlock(mm);
1962fe673d3fSLinus Torvalds
1963fe673d3fSLinus Torvalds if (size > (unsigned long)uaddr - start)
1964fe673d3fSLinus Torvalds return size - ((unsigned long)uaddr - start);
1965fe673d3fSLinus Torvalds return 0;
1966cdd591fcSAndreas Gruenbacher }
1967cdd591fcSAndreas Gruenbacher EXPORT_SYMBOL(fault_in_safe_writeable);
1968cdd591fcSAndreas Gruenbacher
1969bb523b40SAndreas Gruenbacher /**
1970bb523b40SAndreas Gruenbacher * fault_in_readable - fault in userspace address range for reading
1971bb523b40SAndreas Gruenbacher * @uaddr: start of user address range
1972bb523b40SAndreas Gruenbacher * @size: size of user address range
1973bb523b40SAndreas Gruenbacher *
1974bb523b40SAndreas Gruenbacher * Returns the number of bytes not faulted in (like copy_to_user() and
1975bb523b40SAndreas Gruenbacher * copy_from_user()).
1976bb523b40SAndreas Gruenbacher */
fault_in_readable(const char __user * uaddr,size_t size)1977bb523b40SAndreas Gruenbacher size_t fault_in_readable(const char __user *uaddr, size_t size)
1978bb523b40SAndreas Gruenbacher {
1979bb523b40SAndreas Gruenbacher const char __user *start = uaddr, *end;
1980bb523b40SAndreas Gruenbacher volatile char c;
1981bb523b40SAndreas Gruenbacher
1982bb523b40SAndreas Gruenbacher if (unlikely(size == 0))
1983bb523b40SAndreas Gruenbacher return 0;
1984677b2a8cSChristophe Leroy if (!user_read_access_begin(uaddr, size))
1985bb523b40SAndreas Gruenbacher return size;
1986677b2a8cSChristophe Leroy if (!PAGE_ALIGNED(uaddr)) {
1987677b2a8cSChristophe Leroy unsafe_get_user(c, uaddr, out);
1988bb523b40SAndreas Gruenbacher uaddr = (const char __user *)PAGE_ALIGN((unsigned long)uaddr);
1989bb523b40SAndreas Gruenbacher }
1990bb523b40SAndreas Gruenbacher end = (const char __user *)PAGE_ALIGN((unsigned long)start + size);
1991bb523b40SAndreas Gruenbacher if (unlikely(end < start))
1992bb523b40SAndreas Gruenbacher end = NULL;
1993bb523b40SAndreas Gruenbacher while (uaddr != end) {
1994677b2a8cSChristophe Leroy unsafe_get_user(c, uaddr, out);
1995bb523b40SAndreas Gruenbacher uaddr += PAGE_SIZE;
1996bb523b40SAndreas Gruenbacher }
1997bb523b40SAndreas Gruenbacher
1998bb523b40SAndreas Gruenbacher out:
1999677b2a8cSChristophe Leroy user_read_access_end();
2000bb523b40SAndreas Gruenbacher (void)c;
2001bb523b40SAndreas Gruenbacher if (size > uaddr - start)
2002bb523b40SAndreas Gruenbacher return size - (uaddr - start);
2003bb523b40SAndreas Gruenbacher return 0;
2004bb523b40SAndreas Gruenbacher }
2005bb523b40SAndreas Gruenbacher EXPORT_SYMBOL(fault_in_readable);
2006bb523b40SAndreas Gruenbacher
2007bb523b40SAndreas Gruenbacher /**
20088f942eeaSJann Horn * get_dump_page() - pin user page in memory while writing it to core dump
20098f942eeaSJann Horn * @addr: user address
20108f942eeaSJann Horn *
20118f942eeaSJann Horn * Returns struct page pointer of user page pinned for dump,
20128f942eeaSJann Horn * to be freed afterwards by put_page().
20138f942eeaSJann Horn *
20148f942eeaSJann Horn * Returns NULL on any kind of failure - a hole must then be inserted into
20158f942eeaSJann Horn * the corefile, to preserve alignment with its headers; and also returns
20168f942eeaSJann Horn * NULL wherever the ZERO_PAGE, or an anonymous pte_none, has been found -
20178f942eeaSJann Horn * allowing a hole to be left in the corefile to save disk space.
20188f942eeaSJann Horn *
20197f3bfab5SJann Horn * Called without mmap_lock (takes and releases the mmap_lock by itself).
20208f942eeaSJann Horn */
20218f942eeaSJann Horn #ifdef CONFIG_ELF_CORE
get_dump_page(unsigned long addr)20228f942eeaSJann Horn struct page *get_dump_page(unsigned long addr)
20238f942eeaSJann Horn {
20248f942eeaSJann Horn struct page *page;
2025b2a72dffSJason Gunthorpe int locked = 0;
20267f3bfab5SJann Horn int ret;
20278f942eeaSJann Horn
2028b2cac248SLorenzo Stoakes ret = __get_user_pages_locked(current->mm, addr, 1, &page, &locked,
20297f3bfab5SJann Horn FOLL_FORCE | FOLL_DUMP | FOLL_GET);
20307f3bfab5SJann Horn return (ret == 1) ? page : NULL;
20318f942eeaSJann Horn }
20328f942eeaSJann Horn #endif /* CONFIG_ELF_CORE */
20338f942eeaSJann Horn
2034d1e153feSPavel Tatashin #ifdef CONFIG_MIGRATION
2035f68749ecSPavel Tatashin /*
203667e139b0SAlistair Popple * Returns the number of collected pages. Return value is always >= 0.
2037f68749ecSPavel Tatashin */
collect_longterm_unpinnable_pages(struct list_head * movable_page_list,unsigned long nr_pages,struct page ** pages)203867e139b0SAlistair Popple static unsigned long collect_longterm_unpinnable_pages(
203967e139b0SAlistair Popple struct list_head *movable_page_list,
204067e139b0SAlistair Popple unsigned long nr_pages,
2041f6d299ecSAlistair Popple struct page **pages)
20429a4e9f3bSAneesh Kumar K.V {
204367e139b0SAlistair Popple unsigned long i, collected = 0;
20441b7f7e58SMatthew Wilcox (Oracle) struct folio *prev_folio = NULL;
204567e139b0SAlistair Popple bool drain_allow = true;
20469a4e9f3bSAneesh Kumar K.V
204783c02c23SPavel Tatashin for (i = 0; i < nr_pages; i++) {
20481b7f7e58SMatthew Wilcox (Oracle) struct folio *folio = page_folio(pages[i]);
2049f9f38f78SChristoph Hellwig
20501b7f7e58SMatthew Wilcox (Oracle) if (folio == prev_folio)
205183c02c23SPavel Tatashin continue;
20521b7f7e58SMatthew Wilcox (Oracle) prev_folio = folio;
2053f9f38f78SChristoph Hellwig
20546077c943SAlex Sierra if (folio_is_longterm_pinnable(folio))
2055f9f38f78SChristoph Hellwig continue;
205667e139b0SAlistair Popple
205767e139b0SAlistair Popple collected++;
205867e139b0SAlistair Popple
205967e139b0SAlistair Popple if (folio_is_device_coherent(folio))
206067e139b0SAlistair Popple continue;
206167e139b0SAlistair Popple
20621b7f7e58SMatthew Wilcox (Oracle) if (folio_test_hugetlb(folio)) {
20636aa3a920SSidhartha Kumar isolate_hugetlb(folio, movable_page_list);
2064f9f38f78SChristoph Hellwig continue;
2065f9f38f78SChristoph Hellwig }
2066f9f38f78SChristoph Hellwig
20671b7f7e58SMatthew Wilcox (Oracle) if (!folio_test_lru(folio) && drain_allow) {
20689a4e9f3bSAneesh Kumar K.V lru_add_drain_all();
20699a4e9f3bSAneesh Kumar K.V drain_allow = false;
20709a4e9f3bSAneesh Kumar K.V }
20719a4e9f3bSAneesh Kumar K.V
2072be2d5756SBaolin Wang if (!folio_isolate_lru(folio))
20736e7f34ebSPavel Tatashin continue;
207467e139b0SAlistair Popple
207567e139b0SAlistair Popple list_add_tail(&folio->lru, movable_page_list);
20761b7f7e58SMatthew Wilcox (Oracle) node_stat_mod_folio(folio,
20771b7f7e58SMatthew Wilcox (Oracle) NR_ISOLATED_ANON + folio_is_file_lru(folio),
20781b7f7e58SMatthew Wilcox (Oracle) folio_nr_pages(folio));
20799a4e9f3bSAneesh Kumar K.V }
2080f9f38f78SChristoph Hellwig
208167e139b0SAlistair Popple return collected;
2082f68749ecSPavel Tatashin }
2083f9f38f78SChristoph Hellwig
208467e139b0SAlistair Popple /*
208567e139b0SAlistair Popple * Unpins all pages and migrates device coherent pages and movable_page_list.
208667e139b0SAlistair Popple * Returns -EAGAIN if all pages were successfully migrated or -errno for failure
208767e139b0SAlistair Popple * (or partial success).
208867e139b0SAlistair Popple */
migrate_longterm_unpinnable_pages(struct list_head * movable_page_list,unsigned long nr_pages,struct page ** pages)208967e139b0SAlistair Popple static int migrate_longterm_unpinnable_pages(
209067e139b0SAlistair Popple struct list_head *movable_page_list,
209167e139b0SAlistair Popple unsigned long nr_pages,
209267e139b0SAlistair Popple struct page **pages)
209367e139b0SAlistair Popple {
209467e139b0SAlistair Popple int ret;
209567e139b0SAlistair Popple unsigned long i;
209667e139b0SAlistair Popple
209767e139b0SAlistair Popple for (i = 0; i < nr_pages; i++) {
209867e139b0SAlistair Popple struct folio *folio = page_folio(pages[i]);
209967e139b0SAlistair Popple
210067e139b0SAlistair Popple if (folio_is_device_coherent(folio)) {
210167e139b0SAlistair Popple /*
210267e139b0SAlistair Popple * Migration will fail if the page is pinned, so convert
210367e139b0SAlistair Popple * the pin on the source page to a normal reference.
210467e139b0SAlistair Popple */
210567e139b0SAlistair Popple pages[i] = NULL;
210667e139b0SAlistair Popple folio_get(folio);
210767e139b0SAlistair Popple gup_put_folio(folio, 1, FOLL_PIN);
210867e139b0SAlistair Popple
210967e139b0SAlistair Popple if (migrate_device_coherent_page(&folio->page)) {
211067e139b0SAlistair Popple ret = -EBUSY;
211167e139b0SAlistair Popple goto err;
211267e139b0SAlistair Popple }
211367e139b0SAlistair Popple
211467e139b0SAlistair Popple continue;
211567e139b0SAlistair Popple }
211667e139b0SAlistair Popple
211767e139b0SAlistair Popple /*
211867e139b0SAlistair Popple * We can't migrate pages with unexpected references, so drop
211967e139b0SAlistair Popple * the reference obtained by __get_user_pages_locked().
212067e139b0SAlistair Popple * Migrating pages have been added to movable_page_list after
212167e139b0SAlistair Popple * calling folio_isolate_lru() which takes a reference so the
212267e139b0SAlistair Popple * page won't be freed if it's migrating.
212367e139b0SAlistair Popple */
212467e139b0SAlistair Popple unpin_user_page(pages[i]);
212567e139b0SAlistair Popple pages[i] = NULL;
212667e139b0SAlistair Popple }
212767e139b0SAlistair Popple
212867e139b0SAlistair Popple if (!list_empty(movable_page_list)) {
2129f9f38f78SChristoph Hellwig struct migration_target_control mtc = {
2130f9f38f78SChristoph Hellwig .nid = NUMA_NO_NODE,
2131f9f38f78SChristoph Hellwig .gfp_mask = GFP_USER | __GFP_NOWARN,
2132f9f38f78SChristoph Hellwig };
2133f9f38f78SChristoph Hellwig
213467e139b0SAlistair Popple if (migrate_pages(movable_page_list, alloc_migration_target,
2135f0f44638SPavel Tatashin NULL, (unsigned long)&mtc, MIGRATE_SYNC,
213667e139b0SAlistair Popple MR_LONGTERM_PIN, NULL)) {
2137f9f38f78SChristoph Hellwig ret = -ENOMEM;
213867e139b0SAlistair Popple goto err;
213967e139b0SAlistair Popple }
2140f68749ecSPavel Tatashin }
2141f68749ecSPavel Tatashin
214267e139b0SAlistair Popple putback_movable_pages(movable_page_list);
214324a95998SAlistair Popple
214467e139b0SAlistair Popple return -EAGAIN;
214567e139b0SAlistair Popple
214667e139b0SAlistair Popple err:
214767e139b0SAlistair Popple for (i = 0; i < nr_pages; i++)
214867e139b0SAlistair Popple if (pages[i])
214967e139b0SAlistair Popple unpin_user_page(pages[i]);
215067e139b0SAlistair Popple putback_movable_pages(movable_page_list);
215167e139b0SAlistair Popple
215267e139b0SAlistair Popple return ret;
215367e139b0SAlistair Popple }
215467e139b0SAlistair Popple
215567e139b0SAlistair Popple /*
215667e139b0SAlistair Popple * Check whether all pages are *allowed* to be pinned. Rather confusingly, all
215767e139b0SAlistair Popple * pages in the range are required to be pinned via FOLL_PIN, before calling
215867e139b0SAlistair Popple * this routine.
215967e139b0SAlistair Popple *
216067e139b0SAlistair Popple * If any pages in the range are not allowed to be pinned, then this routine
216167e139b0SAlistair Popple * will migrate those pages away, unpin all the pages in the range and return
216267e139b0SAlistair Popple * -EAGAIN. The caller should re-pin the entire range with FOLL_PIN and then
216367e139b0SAlistair Popple * call this routine again.
216467e139b0SAlistair Popple *
216567e139b0SAlistair Popple * If an error other than -EAGAIN occurs, this indicates a migration failure.
216667e139b0SAlistair Popple * The caller should give up, and propagate the error back up the call stack.
216767e139b0SAlistair Popple *
216867e139b0SAlistair Popple * If everything is OK and all pages in the range are allowed to be pinned, then
216967e139b0SAlistair Popple * this routine leaves all pages pinned and returns zero for success.
217067e139b0SAlistair Popple */
check_and_migrate_movable_pages(unsigned long nr_pages,struct page ** pages)217167e139b0SAlistair Popple static long check_and_migrate_movable_pages(unsigned long nr_pages,
217267e139b0SAlistair Popple struct page **pages)
217367e139b0SAlistair Popple {
217467e139b0SAlistair Popple unsigned long collected;
217567e139b0SAlistair Popple LIST_HEAD(movable_page_list);
217667e139b0SAlistair Popple
217767e139b0SAlistair Popple collected = collect_longterm_unpinnable_pages(&movable_page_list,
217867e139b0SAlistair Popple nr_pages, pages);
217967e139b0SAlistair Popple if (!collected)
218067e139b0SAlistair Popple return 0;
218167e139b0SAlistair Popple
218267e139b0SAlistair Popple return migrate_longterm_unpinnable_pages(&movable_page_list, nr_pages,
218367e139b0SAlistair Popple pages);
21849a4e9f3bSAneesh Kumar K.V }
21859a4e9f3bSAneesh Kumar K.V #else
check_and_migrate_movable_pages(unsigned long nr_pages,struct page ** pages)2186f68749ecSPavel Tatashin static long check_and_migrate_movable_pages(unsigned long nr_pages,
2187f6d299ecSAlistair Popple struct page **pages)
21889a4e9f3bSAneesh Kumar K.V {
218924a95998SAlistair Popple return 0;
21909a4e9f3bSAneesh Kumar K.V }
2191d1e153feSPavel Tatashin #endif /* CONFIG_MIGRATION */
21929a4e9f3bSAneesh Kumar K.V
21932bb6d283SDan Williams /*
2194932f4a63SIra Weiny * __gup_longterm_locked() is a wrapper for __get_user_pages_locked which
2195932f4a63SIra Weiny * allows us to process the FOLL_LONGTERM flag.
21962bb6d283SDan Williams */
__gup_longterm_locked(struct mm_struct * mm,unsigned long start,unsigned long nr_pages,struct page ** pages,int * locked,unsigned int gup_flags)219764019a2eSPeter Xu static long __gup_longterm_locked(struct mm_struct *mm,
2198932f4a63SIra Weiny unsigned long start,
2199932f4a63SIra Weiny unsigned long nr_pages,
2200932f4a63SIra Weiny struct page **pages,
220153b2d09bSJason Gunthorpe int *locked,
2202932f4a63SIra Weiny unsigned int gup_flags)
22032bb6d283SDan Williams {
2204f68749ecSPavel Tatashin unsigned int flags;
220524a95998SAlistair Popple long rc, nr_pinned_pages;
22062bb6d283SDan Williams
2207f68749ecSPavel Tatashin if (!(gup_flags & FOLL_LONGTERM))
2208b2cac248SLorenzo Stoakes return __get_user_pages_locked(mm, start, nr_pages, pages,
220953b2d09bSJason Gunthorpe locked, gup_flags);
221067e139b0SAlistair Popple
22111a08ae36SPavel Tatashin flags = memalloc_pin_save();
2212f68749ecSPavel Tatashin do {
221324a95998SAlistair Popple nr_pinned_pages = __get_user_pages_locked(mm, start, nr_pages,
2214b2cac248SLorenzo Stoakes pages, locked,
221524a95998SAlistair Popple gup_flags);
221624a95998SAlistair Popple if (nr_pinned_pages <= 0) {
221724a95998SAlistair Popple rc = nr_pinned_pages;
2218f68749ecSPavel Tatashin break;
221924a95998SAlistair Popple }
2220d64e2dbcSJason Gunthorpe
2221d64e2dbcSJason Gunthorpe /* FOLL_LONGTERM implies FOLL_PIN */
2222f6d299ecSAlistair Popple rc = check_and_migrate_movable_pages(nr_pinned_pages, pages);
222324a95998SAlistair Popple } while (rc == -EAGAIN);
22241a08ae36SPavel Tatashin memalloc_pin_restore(flags);
222524a95998SAlistair Popple return rc ? rc : nr_pinned_pages;
22262bb6d283SDan Williams }
2227932f4a63SIra Weiny
2228d64e2dbcSJason Gunthorpe /*
2229d64e2dbcSJason Gunthorpe * Check that the given flags are valid for the exported gup/pup interface, and
2230d64e2dbcSJason Gunthorpe * update them with the required flags that the caller must have set.
2231d64e2dbcSJason Gunthorpe */
is_valid_gup_args(struct page ** pages,int * locked,unsigned int * gup_flags_p,unsigned int to_set)2232b2cac248SLorenzo Stoakes static bool is_valid_gup_args(struct page **pages, int *locked,
2233b2cac248SLorenzo Stoakes unsigned int *gup_flags_p, unsigned int to_set)
2234447f3e45SBarry Song {
2235d64e2dbcSJason Gunthorpe unsigned int gup_flags = *gup_flags_p;
2236d64e2dbcSJason Gunthorpe
2237447f3e45SBarry Song /*
2238d64e2dbcSJason Gunthorpe * These flags not allowed to be specified externally to the gup
2239d64e2dbcSJason Gunthorpe * interfaces:
224049db746dSLorenzo Stoakes * - FOLL_TOUCH/FOLL_PIN/FOLL_TRIED/FOLL_FAST_ONLY are internal only
2241d64e2dbcSJason Gunthorpe * - FOLL_REMOTE is internal only and used on follow_page()
2242f04740f5SJason Gunthorpe * - FOLL_UNLOCKABLE is internal only and used if locked is !NULL
2243447f3e45SBarry Song */
224449db746dSLorenzo Stoakes if (WARN_ON_ONCE(gup_flags & INTERNAL_GUP_FLAGS))
2245447f3e45SBarry Song return false;
2246447f3e45SBarry Song
2247d64e2dbcSJason Gunthorpe gup_flags |= to_set;
2248f04740f5SJason Gunthorpe if (locked) {
2249f04740f5SJason Gunthorpe /* At the external interface locked must be set */
2250f04740f5SJason Gunthorpe if (WARN_ON_ONCE(*locked != 1))
2251f04740f5SJason Gunthorpe return false;
2252f04740f5SJason Gunthorpe
2253f04740f5SJason Gunthorpe gup_flags |= FOLL_UNLOCKABLE;
2254f04740f5SJason Gunthorpe }
2255d64e2dbcSJason Gunthorpe
2256d64e2dbcSJason Gunthorpe /* FOLL_GET and FOLL_PIN are mutually exclusive. */
2257d64e2dbcSJason Gunthorpe if (WARN_ON_ONCE((gup_flags & (FOLL_PIN | FOLL_GET)) ==
2258d64e2dbcSJason Gunthorpe (FOLL_PIN | FOLL_GET)))
2259d64e2dbcSJason Gunthorpe return false;
2260d64e2dbcSJason Gunthorpe
2261d64e2dbcSJason Gunthorpe /* LONGTERM can only be specified when pinning */
2262d64e2dbcSJason Gunthorpe if (WARN_ON_ONCE(!(gup_flags & FOLL_PIN) && (gup_flags & FOLL_LONGTERM)))
2263d64e2dbcSJason Gunthorpe return false;
2264d64e2dbcSJason Gunthorpe
2265d64e2dbcSJason Gunthorpe /* Pages input must be given if using GET/PIN */
2266d64e2dbcSJason Gunthorpe if (WARN_ON_ONCE((gup_flags & (FOLL_GET | FOLL_PIN)) && !pages))
2267d64e2dbcSJason Gunthorpe return false;
2268d64e2dbcSJason Gunthorpe
2269d64e2dbcSJason Gunthorpe /* We want to allow the pgmap to be hot-unplugged at all times */
2270d64e2dbcSJason Gunthorpe if (WARN_ON_ONCE((gup_flags & FOLL_LONGTERM) &&
2271d64e2dbcSJason Gunthorpe (gup_flags & FOLL_PCI_P2PDMA)))
2272d64e2dbcSJason Gunthorpe return false;
2273d64e2dbcSJason Gunthorpe
2274d64e2dbcSJason Gunthorpe *gup_flags_p = gup_flags;
2275447f3e45SBarry Song return true;
2276447f3e45SBarry Song }
2277447f3e45SBarry Song
227822bf29b6SJohn Hubbard #ifdef CONFIG_MMU
2279adc8cb40SSouptick Joarder /**
2280c4237f8bSJohn Hubbard * get_user_pages_remote() - pin user pages in memory
2281c4237f8bSJohn Hubbard * @mm: mm_struct of target mm
2282c4237f8bSJohn Hubbard * @start: starting user address
2283c4237f8bSJohn Hubbard * @nr_pages: number of pages from start to pin
2284c4237f8bSJohn Hubbard * @gup_flags: flags modifying lookup behaviour
2285c4237f8bSJohn Hubbard * @pages: array that receives pointers to the pages pinned.
2286c4237f8bSJohn Hubbard * Should be at least nr_pages long. Or NULL, if caller
2287c4237f8bSJohn Hubbard * only intends to ensure the pages are faulted in.
2288c4237f8bSJohn Hubbard * @locked: pointer to lock flag indicating whether lock is held and
2289c4237f8bSJohn Hubbard * subsequently whether VM_FAULT_RETRY functionality can be
2290c4237f8bSJohn Hubbard * utilised. Lock must initially be held.
2291c4237f8bSJohn Hubbard *
2292c4237f8bSJohn Hubbard * Returns either number of pages pinned (which may be less than the
2293c4237f8bSJohn Hubbard * number requested), or an error. Details about the return value:
2294c4237f8bSJohn Hubbard *
2295c4237f8bSJohn Hubbard * -- If nr_pages is 0, returns 0.
2296c4237f8bSJohn Hubbard * -- If nr_pages is >0, but no pages were pinned, returns -errno.
2297c4237f8bSJohn Hubbard * -- If nr_pages is >0, and some pages were pinned, returns the number of
2298c4237f8bSJohn Hubbard * pages pinned. Again, this may be less than nr_pages.
2299c4237f8bSJohn Hubbard *
2300c4237f8bSJohn Hubbard * The caller is responsible for releasing returned @pages, via put_page().
2301c4237f8bSJohn Hubbard *
2302c1e8d7c6SMichel Lespinasse * Must be called with mmap_lock held for read or write.
2303c4237f8bSJohn Hubbard *
2304adc8cb40SSouptick Joarder * get_user_pages_remote walks a process's page tables and takes a reference
2305adc8cb40SSouptick Joarder * to each struct page that each user address corresponds to at a given
2306c4237f8bSJohn Hubbard * instant. That is, it takes the page that would be accessed if a user
2307c4237f8bSJohn Hubbard * thread accesses the given user virtual address at that instant.
2308c4237f8bSJohn Hubbard *
2309c4237f8bSJohn Hubbard * This does not guarantee that the page exists in the user mappings when
2310adc8cb40SSouptick Joarder * get_user_pages_remote returns, and there may even be a completely different
2311c4237f8bSJohn Hubbard * page there in some cases (eg. if mmapped pagecache has been invalidated
23125da1a868SJingyu Wang * and subsequently re-faulted). However it does guarantee that the page
2313c4237f8bSJohn Hubbard * won't be freed completely. And mostly callers simply care that the page
2314c4237f8bSJohn Hubbard * contains data that was valid *at some point in time*. Typically, an IO
2315c4237f8bSJohn Hubbard * or similar operation cannot guarantee anything stronger anyway because
2316c4237f8bSJohn Hubbard * locks can't be held over the syscall boundary.
2317c4237f8bSJohn Hubbard *
2318c4237f8bSJohn Hubbard * If gup_flags & FOLL_WRITE == 0, the page must not be written to. If the page
2319c4237f8bSJohn Hubbard * is written to, set_page_dirty (or set_page_dirty_lock, as appropriate) must
2320c4237f8bSJohn Hubbard * be called after the page is finished with, and before put_page is called.
2321c4237f8bSJohn Hubbard *
2322adc8cb40SSouptick Joarder * get_user_pages_remote is typically used for fewer-copy IO operations,
2323adc8cb40SSouptick Joarder * to get a handle on the memory by some means other than accesses
2324adc8cb40SSouptick Joarder * via the user virtual addresses. The pages may be submitted for
2325adc8cb40SSouptick Joarder * DMA to devices or accessed via their kernel linear mapping (via the
2326adc8cb40SSouptick Joarder * kmap APIs). Care should be taken to use the correct cache flushing APIs.
2327c4237f8bSJohn Hubbard *
2328c4237f8bSJohn Hubbard * See also get_user_pages_fast, for performance critical applications.
2329c4237f8bSJohn Hubbard *
2330adc8cb40SSouptick Joarder * get_user_pages_remote should be phased out in favor of
2331c4237f8bSJohn Hubbard * get_user_pages_locked|unlocked or get_user_pages_fast. Nothing
2332adc8cb40SSouptick Joarder * should use get_user_pages_remote because it cannot pass
2333c4237f8bSJohn Hubbard * FAULT_FLAG_ALLOW_RETRY to handle_mm_fault.
2334c4237f8bSJohn 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)233564019a2eSPeter Xu long get_user_pages_remote(struct mm_struct *mm,
2336c4237f8bSJohn Hubbard unsigned long start, unsigned long nr_pages,
2337c4237f8bSJohn Hubbard unsigned int gup_flags, struct page **pages,
2338ca5e8632SLorenzo Stoakes int *locked)
2339c4237f8bSJohn Hubbard {
23409a863a6aSJason Gunthorpe int local_locked = 1;
23419a863a6aSJason Gunthorpe
2342b2cac248SLorenzo Stoakes if (!is_valid_gup_args(pages, locked, &gup_flags,
2343d64e2dbcSJason Gunthorpe FOLL_TOUCH | FOLL_REMOTE))
2344eddb1c22SJohn Hubbard return -EINVAL;
2345eddb1c22SJohn Hubbard
2346b2cac248SLorenzo Stoakes return __get_user_pages_locked(mm, start, nr_pages, pages,
23479a863a6aSJason Gunthorpe locked ? locked : &local_locked,
2348d64e2dbcSJason Gunthorpe gup_flags);
2349c4237f8bSJohn Hubbard }
2350c4237f8bSJohn Hubbard EXPORT_SYMBOL(get_user_pages_remote);
2351c4237f8bSJohn Hubbard
2352eddb1c22SJohn 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)235364019a2eSPeter Xu long get_user_pages_remote(struct mm_struct *mm,
2354eddb1c22SJohn Hubbard unsigned long start, unsigned long nr_pages,
2355eddb1c22SJohn Hubbard unsigned int gup_flags, struct page **pages,
2356ca5e8632SLorenzo Stoakes int *locked)
2357eddb1c22SJohn Hubbard {
2358eddb1c22SJohn Hubbard return 0;
2359eddb1c22SJohn Hubbard }
2360eddb1c22SJohn Hubbard #endif /* !CONFIG_MMU */
2361eddb1c22SJohn Hubbard
2362adc8cb40SSouptick Joarder /**
2363adc8cb40SSouptick Joarder * get_user_pages() - pin user pages in memory
2364adc8cb40SSouptick Joarder * @start: starting user address
2365adc8cb40SSouptick Joarder * @nr_pages: number of pages from start to pin
2366adc8cb40SSouptick Joarder * @gup_flags: flags modifying lookup behaviour
2367adc8cb40SSouptick Joarder * @pages: array that receives pointers to the pages pinned.
2368adc8cb40SSouptick Joarder * Should be at least nr_pages long. Or NULL, if caller
2369adc8cb40SSouptick Joarder * only intends to ensure the pages are faulted in.
2370adc8cb40SSouptick Joarder *
237164019a2eSPeter Xu * This is the same as get_user_pages_remote(), just with a less-flexible
237264019a2eSPeter Xu * calling convention where we assume that the mm being operated on belongs to
237364019a2eSPeter Xu * the current task, and doesn't allow passing of a locked parameter. We also
237464019a2eSPeter Xu * obviously don't pass FOLL_REMOTE in here.
2375932f4a63SIra Weiny */
get_user_pages(unsigned long start,unsigned long nr_pages,unsigned int gup_flags,struct page ** pages)2376932f4a63SIra Weiny long get_user_pages(unsigned long start, unsigned long nr_pages,
237754d02069SLorenzo Stoakes unsigned int gup_flags, struct page **pages)
2378932f4a63SIra Weiny {
23799a863a6aSJason Gunthorpe int locked = 1;
23809a863a6aSJason Gunthorpe
2381b2cac248SLorenzo Stoakes if (!is_valid_gup_args(pages, NULL, &gup_flags, FOLL_TOUCH))
2382eddb1c22SJohn Hubbard return -EINVAL;
2383eddb1c22SJohn Hubbard
2384afa3c33eSJason Gunthorpe return __get_user_pages_locked(current->mm, start, nr_pages, pages,
2385b2cac248SLorenzo Stoakes &locked, gup_flags);
2386932f4a63SIra Weiny }
2387932f4a63SIra Weiny EXPORT_SYMBOL(get_user_pages);
23882bb6d283SDan Williams
2389acc3c8d1SKirill A. Shutemov /*
2390d3649f68SChristoph Hellwig * get_user_pages_unlocked() is suitable to replace the form:
2391acc3c8d1SKirill A. Shutemov *
23923e4e28c5SMichel Lespinasse * mmap_read_lock(mm);
239364019a2eSPeter Xu * get_user_pages(mm, ..., pages, NULL);
23943e4e28c5SMichel Lespinasse * mmap_read_unlock(mm);
2395d3649f68SChristoph Hellwig *
2396d3649f68SChristoph Hellwig * with:
2397d3649f68SChristoph Hellwig *
239864019a2eSPeter Xu * get_user_pages_unlocked(mm, ..., pages);
2399d3649f68SChristoph Hellwig *
2400d3649f68SChristoph Hellwig * It is functionally equivalent to get_user_pages_fast so
2401d3649f68SChristoph Hellwig * get_user_pages_fast should be used instead if specific gup_flags
2402d3649f68SChristoph Hellwig * (e.g. FOLL_FORCE) are not required.
2403acc3c8d1SKirill A. Shutemov */
get_user_pages_unlocked(unsigned long start,unsigned long nr_pages,struct page ** pages,unsigned int gup_flags)2404d3649f68SChristoph Hellwig long get_user_pages_unlocked(unsigned long start, unsigned long nr_pages,
2405d3649f68SChristoph Hellwig struct page **pages, unsigned int gup_flags)
2406acc3c8d1SKirill A. Shutemov {
2407b2a72dffSJason Gunthorpe int locked = 0;
2408acc3c8d1SKirill A. Shutemov
2409b2cac248SLorenzo Stoakes if (!is_valid_gup_args(pages, NULL, &gup_flags,
2410f04740f5SJason Gunthorpe FOLL_TOUCH | FOLL_UNLOCKABLE))
2411d64e2dbcSJason Gunthorpe return -EINVAL;
2412d64e2dbcSJason Gunthorpe
2413afa3c33eSJason Gunthorpe return __get_user_pages_locked(current->mm, start, nr_pages, pages,
2414b2cac248SLorenzo Stoakes &locked, gup_flags);
2415acc3c8d1SKirill A. Shutemov }
2416d3649f68SChristoph Hellwig EXPORT_SYMBOL(get_user_pages_unlocked);
24172667f50eSSteve Capper
24182667f50eSSteve Capper /*
241967a929e0SChristoph Hellwig * Fast GUP
24202667f50eSSteve Capper *
24212667f50eSSteve Capper * get_user_pages_fast attempts to pin user pages by walking the page
24222667f50eSSteve Capper * tables directly and avoids taking locks. Thus the walker needs to be
24232667f50eSSteve Capper * protected from page table pages being freed from under it, and should
24242667f50eSSteve Capper * block any THP splits.
24252667f50eSSteve Capper *
24262667f50eSSteve Capper * One way to achieve this is to have the walker disable interrupts, and
24272667f50eSSteve Capper * rely on IPIs from the TLB flushing code blocking before the page table
24282667f50eSSteve Capper * pages are freed. This is unsuitable for architectures that do not need
24292667f50eSSteve Capper * to broadcast an IPI when invalidating TLBs.
24302667f50eSSteve Capper *
24312667f50eSSteve Capper * Another way to achieve this is to batch up page table containing pages
24322667f50eSSteve Capper * belonging to more than one mm_user, then rcu_sched a callback to free those
24332667f50eSSteve Capper * pages. Disabling interrupts will allow the fast_gup walker to both block
24342667f50eSSteve Capper * the rcu_sched callback, and an IPI that we broadcast for splitting THPs
24352667f50eSSteve Capper * (which is a relatively rare event). The code below adopts this strategy.
24362667f50eSSteve Capper *
24372667f50eSSteve Capper * Before activating this code, please be aware that the following assumptions
24382667f50eSSteve Capper * are currently made:
24392667f50eSSteve Capper *
2440ff2e6d72SPeter Zijlstra * *) Either MMU_GATHER_RCU_TABLE_FREE is enabled, and tlb_remove_table() is used to
2441e585513bSKirill A. Shutemov * free pages containing page tables or TLB flushing requires IPI broadcast.
24422667f50eSSteve Capper *
24432667f50eSSteve Capper * *) ptes can be read atomically by the architecture.
24442667f50eSSteve Capper *
24452667f50eSSteve Capper * *) access_ok is sufficient to validate userspace address ranges.
24462667f50eSSteve Capper *
24472667f50eSSteve Capper * The last two assumptions can be relaxed by the addition of helper functions.
24482667f50eSSteve Capper *
24492667f50eSSteve Capper * This code is based heavily on the PowerPC implementation by Nick Piggin.
24502667f50eSSteve Capper */
245167a929e0SChristoph Hellwig #ifdef CONFIG_HAVE_FAST_GUP
24523faa52c0SJohn Hubbard
2453a6e79df9SLorenzo Stoakes /*
2454a6e79df9SLorenzo Stoakes * Used in the GUP-fast path to determine whether a pin is permitted for a
2455a6e79df9SLorenzo Stoakes * specific folio.
2456a6e79df9SLorenzo Stoakes *
2457a6e79df9SLorenzo Stoakes * This call assumes the caller has pinned the folio, that the lowest page table
2458a6e79df9SLorenzo Stoakes * level still points to this folio, and that interrupts have been disabled.
2459a6e79df9SLorenzo Stoakes *
2460a6e79df9SLorenzo Stoakes * Writing to pinned file-backed dirty tracked folios is inherently problematic
2461a6e79df9SLorenzo Stoakes * (see comment describing the writable_file_mapping_allowed() function). We
2462a6e79df9SLorenzo Stoakes * therefore try to avoid the most egregious case of a long-term mapping doing
2463a6e79df9SLorenzo Stoakes * so.
2464a6e79df9SLorenzo Stoakes *
2465a6e79df9SLorenzo Stoakes * This function cannot be as thorough as that one as the VMA is not available
2466a6e79df9SLorenzo Stoakes * in the fast path, so instead we whitelist known good cases and if in doubt,
2467a6e79df9SLorenzo Stoakes * fall back to the slow path.
2468a6e79df9SLorenzo Stoakes */
folio_fast_pin_allowed(struct folio * folio,unsigned int flags)2469a6e79df9SLorenzo Stoakes static bool folio_fast_pin_allowed(struct folio *folio, unsigned int flags)
2470a6e79df9SLorenzo Stoakes {
2471a6e79df9SLorenzo Stoakes struct address_space *mapping;
2472a6e79df9SLorenzo Stoakes unsigned long mapping_flags;
2473a6e79df9SLorenzo Stoakes
2474a6e79df9SLorenzo Stoakes /*
2475a6e79df9SLorenzo Stoakes * If we aren't pinning then no problematic write can occur. A long term
2476a6e79df9SLorenzo Stoakes * pin is the most egregious case so this is the one we disallow.
2477a6e79df9SLorenzo Stoakes */
2478a6e79df9SLorenzo Stoakes if ((flags & (FOLL_PIN | FOLL_LONGTERM | FOLL_WRITE)) !=
2479a6e79df9SLorenzo Stoakes (FOLL_PIN | FOLL_LONGTERM | FOLL_WRITE))
2480a6e79df9SLorenzo Stoakes return true;
2481a6e79df9SLorenzo Stoakes
2482a6e79df9SLorenzo Stoakes /* The folio is pinned, so we can safely access folio fields. */
2483a6e79df9SLorenzo Stoakes
2484a6e79df9SLorenzo Stoakes if (WARN_ON_ONCE(folio_test_slab(folio)))
2485a6e79df9SLorenzo Stoakes return false;
2486a6e79df9SLorenzo Stoakes
2487a6e79df9SLorenzo Stoakes /* hugetlb mappings do not require dirty-tracking. */
2488a6e79df9SLorenzo Stoakes if (folio_test_hugetlb(folio))
2489a6e79df9SLorenzo Stoakes return true;
2490a6e79df9SLorenzo Stoakes
2491a6e79df9SLorenzo Stoakes /*
2492a6e79df9SLorenzo Stoakes * GUP-fast disables IRQs. When IRQS are disabled, RCU grace periods
2493a6e79df9SLorenzo Stoakes * cannot proceed, which means no actions performed under RCU can
2494a6e79df9SLorenzo Stoakes * proceed either.
2495a6e79df9SLorenzo Stoakes *
2496a6e79df9SLorenzo Stoakes * inodes and thus their mappings are freed under RCU, which means the
2497a6e79df9SLorenzo Stoakes * mapping cannot be freed beneath us and thus we can safely dereference
2498a6e79df9SLorenzo Stoakes * it.
2499a6e79df9SLorenzo Stoakes */
2500a6e79df9SLorenzo Stoakes lockdep_assert_irqs_disabled();
2501a6e79df9SLorenzo Stoakes
2502a6e79df9SLorenzo Stoakes /*
2503a6e79df9SLorenzo Stoakes * However, there may be operations which _alter_ the mapping, so ensure
2504a6e79df9SLorenzo Stoakes * we read it once and only once.
2505a6e79df9SLorenzo Stoakes */
2506a6e79df9SLorenzo Stoakes mapping = READ_ONCE(folio->mapping);
2507a6e79df9SLorenzo Stoakes
2508a6e79df9SLorenzo Stoakes /*
2509a6e79df9SLorenzo Stoakes * The mapping may have been truncated, in any case we cannot determine
2510a6e79df9SLorenzo Stoakes * if this mapping is safe - fall back to slow path to determine how to
2511a6e79df9SLorenzo Stoakes * proceed.
2512a6e79df9SLorenzo Stoakes */
2513a6e79df9SLorenzo Stoakes if (!mapping)
2514a6e79df9SLorenzo Stoakes return false;
2515a6e79df9SLorenzo Stoakes
2516a6e79df9SLorenzo Stoakes /* Anonymous folios pose no problem. */
2517a6e79df9SLorenzo Stoakes mapping_flags = (unsigned long)mapping & PAGE_MAPPING_FLAGS;
2518a6e79df9SLorenzo Stoakes if (mapping_flags)
2519a6e79df9SLorenzo Stoakes return mapping_flags & PAGE_MAPPING_ANON;
2520a6e79df9SLorenzo Stoakes
2521a6e79df9SLorenzo Stoakes /*
2522a6e79df9SLorenzo Stoakes * At this point, we know the mapping is non-null and points to an
2523a6e79df9SLorenzo Stoakes * address_space object. The only remaining whitelisted file system is
2524a6e79df9SLorenzo Stoakes * shmem.
2525a6e79df9SLorenzo Stoakes */
2526a6e79df9SLorenzo Stoakes return shmem_mapping(mapping);
2527a6e79df9SLorenzo Stoakes }
2528a6e79df9SLorenzo Stoakes
undo_dev_pagemap(int * nr,int nr_start,unsigned int flags,struct page ** pages)2529790c7369SGuenter Roeck static void __maybe_unused undo_dev_pagemap(int *nr, int nr_start,
25303b78d834SJohn Hubbard unsigned int flags,
2531790c7369SGuenter Roeck struct page **pages)
2532b59f65faSKirill A. Shutemov {
2533b59f65faSKirill A. Shutemov while ((*nr) - nr_start) {
2534b59f65faSKirill A. Shutemov struct page *page = pages[--(*nr)];
2535b59f65faSKirill A. Shutemov
2536b59f65faSKirill A. Shutemov ClearPageReferenced(page);
25373faa52c0SJohn Hubbard if (flags & FOLL_PIN)
25383faa52c0SJohn Hubbard unpin_user_page(page);
25393faa52c0SJohn Hubbard else
2540b59f65faSKirill A. Shutemov put_page(page);
2541b59f65faSKirill A. Shutemov }
2542b59f65faSKirill A. Shutemov }
2543b59f65faSKirill A. Shutemov
25443010a5eaSLaurent Dufour #ifdef CONFIG_ARCH_HAS_PTE_SPECIAL
254570cbc3ccSYang Shi /*
254670cbc3ccSYang Shi * Fast-gup relies on pte change detection to avoid concurrent pgtable
254770cbc3ccSYang Shi * operations.
254870cbc3ccSYang Shi *
254970cbc3ccSYang Shi * To pin the page, fast-gup needs to do below in order:
255070cbc3ccSYang Shi * (1) pin the page (by prefetching pte), then (2) check pte not changed.
255170cbc3ccSYang Shi *
255270cbc3ccSYang Shi * For the rest of pgtable operations where pgtable updates can be racy
255370cbc3ccSYang Shi * with fast-gup, we need to do (1) clear pte, then (2) check whether page
255470cbc3ccSYang Shi * is pinned.
255570cbc3ccSYang Shi *
255670cbc3ccSYang Shi * Above will work for all pte-level operations, including THP split.
255770cbc3ccSYang Shi *
255870cbc3ccSYang Shi * For THP collapse, it's a bit more complicated because fast-gup may be
255970cbc3ccSYang Shi * walking a pgtable page that is being freed (pte is still valid but pmd
256070cbc3ccSYang Shi * can be cleared already). To avoid race in such condition, we need to
256170cbc3ccSYang Shi * also check pmd here to make sure pmd doesn't change (corresponds to
256270cbc3ccSYang Shi * pmdp_collapse_flush() in the THP collapse code path).
256370cbc3ccSYang Shi */
gup_pte_range(pmd_t pmd,pmd_t * pmdp,unsigned long addr,unsigned long end,unsigned int flags,struct page ** pages,int * nr)256470cbc3ccSYang Shi static int gup_pte_range(pmd_t pmd, pmd_t *pmdp, unsigned long addr,
256570cbc3ccSYang Shi unsigned long end, unsigned int flags,
256670cbc3ccSYang Shi struct page **pages, int *nr)
25672667f50eSSteve Capper {
2568b59f65faSKirill A. Shutemov struct dev_pagemap *pgmap = NULL;
2569b59f65faSKirill A. Shutemov int nr_start = *nr, ret = 0;
25702667f50eSSteve Capper pte_t *ptep, *ptem;
25712667f50eSSteve Capper
25722667f50eSSteve Capper ptem = ptep = pte_offset_map(&pmd, addr);
257304dee9e8SHugh Dickins if (!ptep)
257404dee9e8SHugh Dickins return 0;
25752667f50eSSteve Capper do {
25762a4a06daSPeter Zijlstra pte_t pte = ptep_get_lockless(ptep);
2577b0496fe4SMatthew Wilcox (Oracle) struct page *page;
2578b0496fe4SMatthew Wilcox (Oracle) struct folio *folio;
25792667f50eSSteve Capper
2580d74943a2SDavid Hildenbrand /*
2581d74943a2SDavid Hildenbrand * Always fallback to ordinary GUP on PROT_NONE-mapped pages:
2582d74943a2SDavid Hildenbrand * pte_access_permitted() better should reject these pages
2583d74943a2SDavid Hildenbrand * either way: otherwise, GUP-fast might succeed in
2584d74943a2SDavid Hildenbrand * cases where ordinary GUP would fail due to VMA access
2585d74943a2SDavid Hildenbrand * permissions.
2586d74943a2SDavid Hildenbrand */
2587d74943a2SDavid Hildenbrand if (pte_protnone(pte))
2588e7884f8eSKirill A. Shutemov goto pte_unmap;
2589e7884f8eSKirill A. Shutemov
2590b798bec4SIra Weiny if (!pte_access_permitted(pte, flags & FOLL_WRITE))
2591e7884f8eSKirill A. Shutemov goto pte_unmap;
2592e7884f8eSKirill A. Shutemov
2593b59f65faSKirill A. Shutemov if (pte_devmap(pte)) {
25947af75561SIra Weiny if (unlikely(flags & FOLL_LONGTERM))
25957af75561SIra Weiny goto pte_unmap;
25967af75561SIra Weiny
2597b59f65faSKirill A. Shutemov pgmap = get_dev_pagemap(pte_pfn(pte), pgmap);
2598b59f65faSKirill A. Shutemov if (unlikely(!pgmap)) {
25993b78d834SJohn Hubbard undo_dev_pagemap(nr, nr_start, flags, pages);
2600b59f65faSKirill A. Shutemov goto pte_unmap;
2601b59f65faSKirill A. Shutemov }
2602b59f65faSKirill A. Shutemov } else if (pte_special(pte))
26032667f50eSSteve Capper goto pte_unmap;
26042667f50eSSteve Capper
26052667f50eSSteve Capper VM_BUG_ON(!pfn_valid(pte_pfn(pte)));
26062667f50eSSteve Capper page = pte_page(pte);
26072667f50eSSteve Capper
2608b0496fe4SMatthew Wilcox (Oracle) folio = try_grab_folio(page, 1, flags);
2609b0496fe4SMatthew Wilcox (Oracle) if (!folio)
26102667f50eSSteve Capper goto pte_unmap;
26112667f50eSSteve Capper
26128f9ff2deSMatthew Wilcox (Oracle) if (unlikely(folio_is_secretmem(folio))) {
2613b0496fe4SMatthew Wilcox (Oracle) gup_put_folio(folio, 1, flags);
26141507f512SMike Rapoport goto pte_unmap;
26151507f512SMike Rapoport }
26161507f512SMike Rapoport
261770cbc3ccSYang Shi if (unlikely(pmd_val(pmd) != pmd_val(*pmdp)) ||
2618c33c7948SRyan Roberts unlikely(pte_val(pte) != pte_val(ptep_get(ptep)))) {
2619b0496fe4SMatthew Wilcox (Oracle) gup_put_folio(folio, 1, flags);
26202667f50eSSteve Capper goto pte_unmap;
26212667f50eSSteve Capper }
26222667f50eSSteve Capper
2623a6e79df9SLorenzo Stoakes if (!folio_fast_pin_allowed(folio, flags)) {
26242667f50eSSteve Capper gup_put_folio(folio, 1, flags);
26252667f50eSSteve Capper goto pte_unmap;
26262667f50eSSteve Capper }
26272667f50eSSteve Capper
262884209e87SDavid Hildenbrand if (!pte_write(pte) && gup_must_unshare(NULL, flags, page)) {
2629a7f22660SDavid Hildenbrand gup_put_folio(folio, 1, flags);
2630a7f22660SDavid Hildenbrand goto pte_unmap;
2631a7f22660SDavid Hildenbrand }
2632a7f22660SDavid Hildenbrand
2633f28d4363SClaudio Imbrenda /*
2634f28d4363SClaudio Imbrenda * We need to make the page accessible if and only if we are
2635f28d4363SClaudio Imbrenda * going to access its content (the FOLL_PIN case). Please
2636f28d4363SClaudio Imbrenda * see Documentation/core-api/pin_user_pages.rst for
2637f28d4363SClaudio Imbrenda * details.
2638f28d4363SClaudio Imbrenda */
2639f28d4363SClaudio Imbrenda if (flags & FOLL_PIN) {
2640f28d4363SClaudio Imbrenda ret = arch_make_page_accessible(page);
2641f28d4363SClaudio Imbrenda if (ret) {
2642b0496fe4SMatthew Wilcox (Oracle) gup_put_folio(folio, 1, flags);
2643f28d4363SClaudio Imbrenda goto pte_unmap;
2644f28d4363SClaudio Imbrenda }
2645f28d4363SClaudio Imbrenda }
2646b0496fe4SMatthew Wilcox (Oracle) folio_set_referenced(folio);
26472667f50eSSteve Capper pages[*nr] = page;
26482667f50eSSteve Capper (*nr)++;
26492667f50eSSteve Capper } while (ptep++, addr += PAGE_SIZE, addr != end);
26502667f50eSSteve Capper
26512667f50eSSteve Capper ret = 1;
26522667f50eSSteve Capper
26532667f50eSSteve Capper pte_unmap:
2654832d7aa0SChristoph Hellwig if (pgmap)
2655832d7aa0SChristoph Hellwig put_dev_pagemap(pgmap);
26562667f50eSSteve Capper pte_unmap(ptem);
26572667f50eSSteve Capper return ret;
26582667f50eSSteve Capper }
26592667f50eSSteve Capper #else
26602667f50eSSteve Capper
26612667f50eSSteve Capper /*
26622667f50eSSteve Capper * If we can't determine whether or not a pte is special, then fail immediately
26632667f50eSSteve Capper * for ptes. Note, we can still pin HugeTLB and THP as these are guaranteed not
26642667f50eSSteve Capper * to be special.
26652667f50eSSteve Capper *
26662667f50eSSteve Capper * For a futex to be placed on a THP tail page, get_futex_key requires a
2667dadbb612SSouptick Joarder * get_user_pages_fast_only implementation that can pin pages. Thus it's still
26682667f50eSSteve Capper * useful to have gup_huge_pmd even if we can't operate on ptes.
26692667f50eSSteve Capper */
gup_pte_range(pmd_t pmd,pmd_t * pmdp,unsigned long addr,unsigned long end,unsigned int flags,struct page ** pages,int * nr)267070cbc3ccSYang Shi static int gup_pte_range(pmd_t pmd, pmd_t *pmdp, unsigned long addr,
267170cbc3ccSYang Shi unsigned long end, unsigned int flags,
267270cbc3ccSYang Shi struct page **pages, int *nr)
26732667f50eSSteve Capper {
26742667f50eSSteve Capper return 0;
26752667f50eSSteve Capper }
26763010a5eaSLaurent Dufour #endif /* CONFIG_ARCH_HAS_PTE_SPECIAL */
26772667f50eSSteve Capper
267817596731SRobin 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)2679b59f65faSKirill A. Shutemov static int __gup_device_huge(unsigned long pfn, unsigned long addr,
268086dfbed4SJohn Hubbard unsigned long end, unsigned int flags,
268186dfbed4SJohn Hubbard struct page **pages, int *nr)
2682b59f65faSKirill A. Shutemov {
2683b59f65faSKirill A. Shutemov int nr_start = *nr;
2684b59f65faSKirill A. Shutemov struct dev_pagemap *pgmap = NULL;
2685b59f65faSKirill A. Shutemov
2686b59f65faSKirill A. Shutemov do {
2687b59f65faSKirill A. Shutemov struct page *page = pfn_to_page(pfn);
2688b59f65faSKirill A. Shutemov
2689b59f65faSKirill A. Shutemov pgmap = get_dev_pagemap(pfn, pgmap);
2690b59f65faSKirill A. Shutemov if (unlikely(!pgmap)) {
26913b78d834SJohn Hubbard undo_dev_pagemap(nr, nr_start, flags, pages);
26926401c4ebSMiaohe Lin break;
2693b59f65faSKirill A. Shutemov }
26944003f107SLogan Gunthorpe
26954003f107SLogan Gunthorpe if (!(flags & FOLL_PCI_P2PDMA) && is_pci_p2pdma_page(page)) {
26964003f107SLogan Gunthorpe undo_dev_pagemap(nr, nr_start, flags, pages);
26974003f107SLogan Gunthorpe break;
26984003f107SLogan Gunthorpe }
26994003f107SLogan Gunthorpe
2700b59f65faSKirill A. Shutemov SetPageReferenced(page);
2701b59f65faSKirill A. Shutemov pages[*nr] = page;
27020f089235SLogan Gunthorpe if (unlikely(try_grab_page(page, flags))) {
27033faa52c0SJohn Hubbard undo_dev_pagemap(nr, nr_start, flags, pages);
27046401c4ebSMiaohe Lin break;
27053faa52c0SJohn Hubbard }
2706b59f65faSKirill A. Shutemov (*nr)++;
2707b59f65faSKirill A. Shutemov pfn++;
2708b59f65faSKirill A. Shutemov } while (addr += PAGE_SIZE, addr != end);
2709832d7aa0SChristoph Hellwig
2710832d7aa0SChristoph Hellwig put_dev_pagemap(pgmap);
271120b7fee7SJohn Hubbard return addr == end;
2712b59f65faSKirill A. Shutemov }
2713b59f65faSKirill 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)2714a9b6de77SDan Williams static int __gup_device_huge_pmd(pmd_t orig, pmd_t *pmdp, unsigned long addr,
271586dfbed4SJohn Hubbard unsigned long end, unsigned int flags,
271686dfbed4SJohn Hubbard struct page **pages, int *nr)
2717b59f65faSKirill A. Shutemov {
2718b59f65faSKirill A. Shutemov unsigned long fault_pfn;
2719a9b6de77SDan Williams int nr_start = *nr;
2720b59f65faSKirill A. Shutemov
2721a9b6de77SDan Williams fault_pfn = pmd_pfn(orig) + ((addr & ~PMD_MASK) >> PAGE_SHIFT);
272286dfbed4SJohn Hubbard if (!__gup_device_huge(fault_pfn, addr, end, flags, pages, nr))
2723a9b6de77SDan Williams return 0;
2724a9b6de77SDan Williams
2725a9b6de77SDan Williams if (unlikely(pmd_val(orig) != pmd_val(*pmdp))) {
27263b78d834SJohn Hubbard undo_dev_pagemap(nr, nr_start, flags, pages);
2727a9b6de77SDan Williams return 0;
2728a9b6de77SDan Williams }
2729a9b6de77SDan Williams return 1;
2730b59f65faSKirill A. Shutemov }
2731b59f65faSKirill 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)2732a9b6de77SDan Williams static int __gup_device_huge_pud(pud_t orig, pud_t *pudp, unsigned long addr,
273386dfbed4SJohn Hubbard unsigned long end, unsigned int flags,
273486dfbed4SJohn Hubbard struct page **pages, int *nr)
2735b59f65faSKirill A. Shutemov {
2736b59f65faSKirill A. Shutemov unsigned long fault_pfn;
2737a9b6de77SDan Williams int nr_start = *nr;
2738b59f65faSKirill A. Shutemov
2739a9b6de77SDan Williams fault_pfn = pud_pfn(orig) + ((addr & ~PUD_MASK) >> PAGE_SHIFT);
274086dfbed4SJohn Hubbard if (!__gup_device_huge(fault_pfn, addr, end, flags, pages, nr))
2741a9b6de77SDan Williams return 0;
2742a9b6de77SDan Williams
2743a9b6de77SDan Williams if (unlikely(pud_val(orig) != pud_val(*pudp))) {
27443b78d834SJohn Hubbard undo_dev_pagemap(nr, nr_start, flags, pages);
2745a9b6de77SDan Williams return 0;
2746a9b6de77SDan Williams }
2747a9b6de77SDan Williams return 1;
2748b59f65faSKirill A. Shutemov }
2749b59f65faSKirill 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)2750a9b6de77SDan Williams static int __gup_device_huge_pmd(pmd_t orig, pmd_t *pmdp, unsigned long addr,
275186dfbed4SJohn Hubbard unsigned long end, unsigned int flags,
275286dfbed4SJohn Hubbard struct page **pages, int *nr)
2753b59f65faSKirill A. Shutemov {
2754b59f65faSKirill A. Shutemov BUILD_BUG();
2755b59f65faSKirill A. Shutemov return 0;
2756b59f65faSKirill A. Shutemov }
2757b59f65faSKirill 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)2758a9b6de77SDan Williams static int __gup_device_huge_pud(pud_t pud, pud_t *pudp, unsigned long addr,
275986dfbed4SJohn Hubbard unsigned long end, unsigned int flags,
276086dfbed4SJohn Hubbard struct page **pages, int *nr)
2761b59f65faSKirill A. Shutemov {
2762b59f65faSKirill A. Shutemov BUILD_BUG();
2763b59f65faSKirill A. Shutemov return 0;
2764b59f65faSKirill A. Shutemov }
2765b59f65faSKirill A. Shutemov #endif
2766b59f65faSKirill A. Shutemov
record_subpages(struct page * page,unsigned long addr,unsigned long end,struct page ** pages)2767a43e9820SJohn Hubbard static int record_subpages(struct page *page, unsigned long addr,
2768a43e9820SJohn Hubbard unsigned long end, struct page **pages)
2769a43e9820SJohn Hubbard {
2770a43e9820SJohn Hubbard int nr;
2771a43e9820SJohn Hubbard
2772c228afb1SMatthew Wilcox (Oracle) for (nr = 0; addr != end; nr++, addr += PAGE_SIZE)
2773c228afb1SMatthew Wilcox (Oracle) pages[nr] = nth_page(page, nr);
2774a43e9820SJohn Hubbard
2775a43e9820SJohn Hubbard return nr;
2776a43e9820SJohn Hubbard }
2777a43e9820SJohn Hubbard
2778cbd34da7SChristoph Hellwig #ifdef CONFIG_ARCH_HAS_HUGEPD
hugepte_addr_end(unsigned long addr,unsigned long end,unsigned long sz)2779cbd34da7SChristoph Hellwig static unsigned long hugepte_addr_end(unsigned long addr, unsigned long end,
2780cbd34da7SChristoph Hellwig unsigned long sz)
2781cbd34da7SChristoph Hellwig {
2782cbd34da7SChristoph Hellwig unsigned long __boundary = (addr + sz) & ~(sz-1);
2783cbd34da7SChristoph Hellwig return (__boundary - 1 < end - 1) ? __boundary : end;
2784cbd34da7SChristoph Hellwig }
2785cbd34da7SChristoph Hellwig
gup_hugepte(pte_t * ptep,unsigned long sz,unsigned long addr,unsigned long end,unsigned int flags,struct page ** pages,int * nr)2786cbd34da7SChristoph Hellwig static int gup_hugepte(pte_t *ptep, unsigned long sz, unsigned long addr,
27870cd22afdSJohn Hubbard unsigned long end, unsigned int flags,
27880cd22afdSJohn Hubbard struct page **pages, int *nr)
2789cbd34da7SChristoph Hellwig {
2790cbd34da7SChristoph Hellwig unsigned long pte_end;
279109a1626eSMatthew Wilcox (Oracle) struct page *page;
279209a1626eSMatthew Wilcox (Oracle) struct folio *folio;
2793cbd34da7SChristoph Hellwig pte_t pte;
2794cbd34da7SChristoph Hellwig int refs;
2795cbd34da7SChristoph Hellwig
2796cbd34da7SChristoph Hellwig pte_end = (addr + sz) & ~(sz-1);
2797cbd34da7SChristoph Hellwig if (pte_end < end)
2798cbd34da7SChristoph Hellwig end = pte_end;
2799cbd34da7SChristoph Hellwig
280055ca2263SChristophe Leroy pte = huge_ptep_get(ptep);
2801cbd34da7SChristoph Hellwig
28020cd22afdSJohn Hubbard if (!pte_access_permitted(pte, flags & FOLL_WRITE))
2803cbd34da7SChristoph Hellwig return 0;
2804cbd34da7SChristoph Hellwig
2805cbd34da7SChristoph Hellwig /* hugepages are never "special" */
2806cbd34da7SChristoph Hellwig VM_BUG_ON(!pfn_valid(pte_pfn(pte)));
2807cbd34da7SChristoph Hellwig
280809a1626eSMatthew Wilcox (Oracle) page = nth_page(pte_page(pte), (addr & (sz - 1)) >> PAGE_SHIFT);
2809a43e9820SJohn Hubbard refs = record_subpages(page, addr, end, pages + *nr);
2810cbd34da7SChristoph Hellwig
281109a1626eSMatthew Wilcox (Oracle) folio = try_grab_folio(page, refs, flags);
281209a1626eSMatthew Wilcox (Oracle) if (!folio)
2813cbd34da7SChristoph Hellwig return 0;
2814cbd34da7SChristoph Hellwig
2815c33c7948SRyan Roberts if (unlikely(pte_val(pte) != pte_val(ptep_get(ptep)))) {
281609a1626eSMatthew Wilcox (Oracle) gup_put_folio(folio, refs, flags);
2817cbd34da7SChristoph Hellwig return 0;
2818cbd34da7SChristoph Hellwig }
2819cbd34da7SChristoph Hellwig
2820a6e79df9SLorenzo Stoakes if (!folio_fast_pin_allowed(folio, flags)) {
2821cbd34da7SChristoph Hellwig gup_put_folio(folio, refs, flags);
2822cbd34da7SChristoph Hellwig return 0;
2823cbd34da7SChristoph Hellwig }
2824cbd34da7SChristoph Hellwig
282584209e87SDavid Hildenbrand if (!pte_write(pte) && gup_must_unshare(NULL, flags, &folio->page)) {
2826a7f22660SDavid Hildenbrand gup_put_folio(folio, refs, flags);
2827a7f22660SDavid Hildenbrand return 0;
2828a7f22660SDavid Hildenbrand }
2829a7f22660SDavid Hildenbrand
2830a43e9820SJohn Hubbard *nr += refs;
283109a1626eSMatthew Wilcox (Oracle) folio_set_referenced(folio);
2832cbd34da7SChristoph Hellwig return 1;
2833cbd34da7SChristoph Hellwig }
2834cbd34da7SChristoph Hellwig
gup_huge_pd(hugepd_t hugepd,unsigned long addr,unsigned int pdshift,unsigned long end,unsigned int flags,struct page ** pages,int * nr)2835cbd34da7SChristoph Hellwig static int gup_huge_pd(hugepd_t hugepd, unsigned long addr,
28360cd22afdSJohn Hubbard unsigned int pdshift, unsigned long end, unsigned int flags,
2837cbd34da7SChristoph Hellwig struct page **pages, int *nr)
2838cbd34da7SChristoph Hellwig {
2839cbd34da7SChristoph Hellwig pte_t *ptep;
2840cbd34da7SChristoph Hellwig unsigned long sz = 1UL << hugepd_shift(hugepd);
2841cbd34da7SChristoph Hellwig unsigned long next;
2842cbd34da7SChristoph Hellwig
2843cbd34da7SChristoph Hellwig ptep = hugepte_offset(hugepd, addr, pdshift);
2844cbd34da7SChristoph Hellwig do {
2845cbd34da7SChristoph Hellwig next = hugepte_addr_end(addr, end, sz);
28460cd22afdSJohn Hubbard if (!gup_hugepte(ptep, sz, addr, end, flags, pages, nr))
2847cbd34da7SChristoph Hellwig return 0;
2848cbd34da7SChristoph Hellwig } while (ptep++, addr = next, addr != end);
2849cbd34da7SChristoph Hellwig
2850cbd34da7SChristoph Hellwig return 1;
2851cbd34da7SChristoph Hellwig }
2852cbd34da7SChristoph 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)2853cbd34da7SChristoph Hellwig static inline int gup_huge_pd(hugepd_t hugepd, unsigned long addr,
28540cd22afdSJohn Hubbard unsigned int pdshift, unsigned long end, unsigned int flags,
2855cbd34da7SChristoph Hellwig struct page **pages, int *nr)
2856cbd34da7SChristoph Hellwig {
2857cbd34da7SChristoph Hellwig return 0;
2858cbd34da7SChristoph Hellwig }
2859cbd34da7SChristoph Hellwig #endif /* CONFIG_ARCH_HAS_HUGEPD */
2860cbd34da7SChristoph Hellwig
gup_huge_pmd(pmd_t orig,pmd_t * pmdp,unsigned long addr,unsigned long end,unsigned int flags,struct page ** pages,int * nr)28612667f50eSSteve Capper static int gup_huge_pmd(pmd_t orig, pmd_t *pmdp, unsigned long addr,
28620cd22afdSJohn Hubbard unsigned long end, unsigned int flags,
28630cd22afdSJohn Hubbard struct page **pages, int *nr)
28642667f50eSSteve Capper {
2865667ed1f7SMatthew Wilcox (Oracle) struct page *page;
2866667ed1f7SMatthew Wilcox (Oracle) struct folio *folio;
28672667f50eSSteve Capper int refs;
28682667f50eSSteve Capper
2869b798bec4SIra Weiny if (!pmd_access_permitted(orig, flags & FOLL_WRITE))
28702667f50eSSteve Capper return 0;
28712667f50eSSteve Capper
28727af75561SIra Weiny if (pmd_devmap(orig)) {
28737af75561SIra Weiny if (unlikely(flags & FOLL_LONGTERM))
28747af75561SIra Weiny return 0;
287586dfbed4SJohn Hubbard return __gup_device_huge_pmd(orig, pmdp, addr, end, flags,
287686dfbed4SJohn Hubbard pages, nr);
28777af75561SIra Weiny }
2878b59f65faSKirill A. Shutemov
2879c228afb1SMatthew Wilcox (Oracle) page = nth_page(pmd_page(orig), (addr & ~PMD_MASK) >> PAGE_SHIFT);
2880a43e9820SJohn Hubbard refs = record_subpages(page, addr, end, pages + *nr);
28812667f50eSSteve Capper
2882667ed1f7SMatthew Wilcox (Oracle) folio = try_grab_folio(page, refs, flags);
2883667ed1f7SMatthew Wilcox (Oracle) if (!folio)
28842667f50eSSteve Capper return 0;
28852667f50eSSteve Capper
28862667f50eSSteve Capper if (unlikely(pmd_val(orig) != pmd_val(*pmdp))) {
2887667ed1f7SMatthew Wilcox (Oracle) gup_put_folio(folio, refs, flags);
28882667f50eSSteve Capper return 0;
28892667f50eSSteve Capper }
28902667f50eSSteve Capper
2891a6e79df9SLorenzo Stoakes if (!folio_fast_pin_allowed(folio, flags)) {
2892a6e79df9SLorenzo Stoakes gup_put_folio(folio, refs, flags);
2893a6e79df9SLorenzo Stoakes return 0;
2894a6e79df9SLorenzo Stoakes }
289584209e87SDavid Hildenbrand if (!pmd_write(orig) && gup_must_unshare(NULL, flags, &folio->page)) {
2896a7f22660SDavid Hildenbrand gup_put_folio(folio, refs, flags);
2897a7f22660SDavid Hildenbrand return 0;
2898a7f22660SDavid Hildenbrand }
2899a7f22660SDavid Hildenbrand
2900a43e9820SJohn Hubbard *nr += refs;
2901667ed1f7SMatthew Wilcox (Oracle) folio_set_referenced(folio);
29022667f50eSSteve Capper return 1;
29032667f50eSSteve Capper }
29042667f50eSSteve Capper
gup_huge_pud(pud_t orig,pud_t * pudp,unsigned long addr,unsigned long end,unsigned int flags,struct page ** pages,int * nr)29052667f50eSSteve Capper static int gup_huge_pud(pud_t orig, pud_t *pudp, unsigned long addr,
290686dfbed4SJohn Hubbard unsigned long end, unsigned int flags,
290786dfbed4SJohn Hubbard struct page **pages, int *nr)
29082667f50eSSteve Capper {
290983afb52eSMatthew Wilcox (Oracle) struct page *page;
291083afb52eSMatthew Wilcox (Oracle) struct folio *folio;
29112667f50eSSteve Capper int refs;
29122667f50eSSteve Capper
2913b798bec4SIra Weiny if (!pud_access_permitted(orig, flags & FOLL_WRITE))
29142667f50eSSteve Capper return 0;
29152667f50eSSteve Capper
29167af75561SIra Weiny if (pud_devmap(orig)) {
29177af75561SIra Weiny if (unlikely(flags & FOLL_LONGTERM))
29187af75561SIra Weiny return 0;
291986dfbed4SJohn Hubbard return __gup_device_huge_pud(orig, pudp, addr, end, flags,
292086dfbed4SJohn Hubbard pages, nr);
29217af75561SIra Weiny }
2922b59f65faSKirill A. Shutemov
2923c228afb1SMatthew Wilcox (Oracle) page = nth_page(pud_page(orig), (addr & ~PUD_MASK) >> PAGE_SHIFT);
2924a43e9820SJohn Hubbard refs = record_subpages(page, addr, end, pages + *nr);
29252667f50eSSteve Capper
292683afb52eSMatthew Wilcox (Oracle) folio = try_grab_folio(page, refs, flags);
292783afb52eSMatthew Wilcox (Oracle) if (!folio)
29282667f50eSSteve Capper return 0;
29292667f50eSSteve Capper
29302667f50eSSteve Capper if (unlikely(pud_val(orig) != pud_val(*pudp))) {
293183afb52eSMatthew Wilcox (Oracle) gup_put_folio(folio, refs, flags);
29322667f50eSSteve Capper return 0;
29332667f50eSSteve Capper }
29342667f50eSSteve Capper
2935a6e79df9SLorenzo Stoakes if (!folio_fast_pin_allowed(folio, flags)) {
2936a6e79df9SLorenzo Stoakes gup_put_folio(folio, refs, flags);
2937a6e79df9SLorenzo Stoakes return 0;
2938a6e79df9SLorenzo Stoakes }
2939a6e79df9SLorenzo Stoakes
294084209e87SDavid Hildenbrand if (!pud_write(orig) && gup_must_unshare(NULL, flags, &folio->page)) {
2941a7f22660SDavid Hildenbrand gup_put_folio(folio, refs, flags);
2942a7f22660SDavid Hildenbrand return 0;
2943a7f22660SDavid Hildenbrand }
2944a7f22660SDavid Hildenbrand
2945a43e9820SJohn Hubbard *nr += refs;
294683afb52eSMatthew Wilcox (Oracle) folio_set_referenced(folio);
29472667f50eSSteve Capper return 1;
29482667f50eSSteve Capper }
29492667f50eSSteve Capper
gup_huge_pgd(pgd_t orig,pgd_t * pgdp,unsigned long addr,unsigned long end,unsigned int flags,struct page ** pages,int * nr)2950f30c59e9SAneesh Kumar K.V static int gup_huge_pgd(pgd_t orig, pgd_t *pgdp, unsigned long addr,
2951b798bec4SIra Weiny unsigned long end, unsigned int flags,
2952f30c59e9SAneesh Kumar K.V struct page **pages, int *nr)
2953f30c59e9SAneesh Kumar K.V {
2954f30c59e9SAneesh Kumar K.V int refs;
29552d7919a2SMatthew Wilcox (Oracle) struct page *page;
29562d7919a2SMatthew Wilcox (Oracle) struct folio *folio;
2957f30c59e9SAneesh Kumar K.V
2958b798bec4SIra Weiny if (!pgd_access_permitted(orig, flags & FOLL_WRITE))
2959f30c59e9SAneesh Kumar K.V return 0;
2960f30c59e9SAneesh Kumar K.V
2961b59f65faSKirill A. Shutemov BUILD_BUG_ON(pgd_devmap(orig));
2962a43e9820SJohn Hubbard
2963c228afb1SMatthew Wilcox (Oracle) page = nth_page(pgd_page(orig), (addr & ~PGDIR_MASK) >> PAGE_SHIFT);
2964a43e9820SJohn Hubbard refs = record_subpages(page, addr, end, pages + *nr);
2965f30c59e9SAneesh Kumar K.V
29662d7919a2SMatthew Wilcox (Oracle) folio = try_grab_folio(page, refs, flags);
29672d7919a2SMatthew Wilcox (Oracle) if (!folio)
2968f30c59e9SAneesh Kumar K.V return 0;
2969f30c59e9SAneesh Kumar K.V
2970f30c59e9SAneesh Kumar K.V if (unlikely(pgd_val(orig) != pgd_val(*pgdp))) {
29712d7919a2SMatthew Wilcox (Oracle) gup_put_folio(folio, refs, flags);
2972f30c59e9SAneesh Kumar K.V return 0;
2973f30c59e9SAneesh Kumar K.V }
2974f30c59e9SAneesh Kumar K.V
297531115034SLorenzo Stoakes if (!pgd_write(orig) && gup_must_unshare(NULL, flags, &folio->page)) {
297631115034SLorenzo Stoakes gup_put_folio(folio, refs, flags);
297731115034SLorenzo Stoakes return 0;
297831115034SLorenzo Stoakes }
297931115034SLorenzo Stoakes
2980a6e79df9SLorenzo Stoakes if (!folio_fast_pin_allowed(folio, flags)) {
2981a6e79df9SLorenzo Stoakes gup_put_folio(folio, refs, flags);
2982a6e79df9SLorenzo Stoakes return 0;
2983a6e79df9SLorenzo Stoakes }
2984a6e79df9SLorenzo Stoakes
2985a43e9820SJohn Hubbard *nr += refs;
29862d7919a2SMatthew Wilcox (Oracle) folio_set_referenced(folio);
2987f30c59e9SAneesh Kumar K.V return 1;
2988f30c59e9SAneesh Kumar K.V }
2989f30c59e9SAneesh 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)2990d3f7b1bbSVasily Gorbik static int gup_pmd_range(pud_t *pudp, pud_t pud, unsigned long addr, unsigned long end,
2991b798bec4SIra Weiny unsigned int flags, struct page **pages, int *nr)
29922667f50eSSteve Capper {
29932667f50eSSteve Capper unsigned long next;
29942667f50eSSteve Capper pmd_t *pmdp;
29952667f50eSSteve Capper
2996d3f7b1bbSVasily Gorbik pmdp = pmd_offset_lockless(pudp, pud, addr);
29972667f50eSSteve Capper do {
29981180e732SPeter Zijlstra pmd_t pmd = pmdp_get_lockless(pmdp);
29992667f50eSSteve Capper
30002667f50eSSteve Capper next = pmd_addr_end(addr, end);
300184c3fc4eSZi Yan if (!pmd_present(pmd))
30022667f50eSSteve Capper return 0;
30032667f50eSSteve Capper
3004414fd080SYu Zhao if (unlikely(pmd_trans_huge(pmd) || pmd_huge(pmd) ||
3005414fd080SYu Zhao pmd_devmap(pmd))) {
3006d74943a2SDavid Hildenbrand /* See gup_pte_range() */
3007d74943a2SDavid Hildenbrand if (pmd_protnone(pmd))
30082667f50eSSteve Capper return 0;
30092667f50eSSteve Capper
3010b798bec4SIra Weiny if (!gup_huge_pmd(pmd, pmdp, addr, next, flags,
30112667f50eSSteve Capper pages, nr))
30122667f50eSSteve Capper return 0;
30132667f50eSSteve Capper
3014f30c59e9SAneesh Kumar K.V } else if (unlikely(is_hugepd(__hugepd(pmd_val(pmd))))) {
3015f30c59e9SAneesh Kumar K.V /*
3016f30c59e9SAneesh Kumar K.V * architecture have different format for hugetlbfs
3017f30c59e9SAneesh Kumar K.V * pmd format and THP pmd format
3018f30c59e9SAneesh Kumar K.V */
3019f30c59e9SAneesh Kumar K.V if (!gup_huge_pd(__hugepd(pmd_val(pmd)), addr,
3020b798bec4SIra Weiny PMD_SHIFT, next, flags, pages, nr))
3021f30c59e9SAneesh Kumar K.V return 0;
302270cbc3ccSYang Shi } else if (!gup_pte_range(pmd, pmdp, addr, next, flags, pages, nr))
30232667f50eSSteve Capper return 0;
30242667f50eSSteve Capper } while (pmdp++, addr = next, addr != end);
30252667f50eSSteve Capper
30262667f50eSSteve Capper return 1;
30272667f50eSSteve Capper }
30282667f50eSSteve Capper
gup_pud_range(p4d_t * p4dp,p4d_t p4d,unsigned long addr,unsigned long end,unsigned int flags,struct page ** pages,int * nr)3029d3f7b1bbSVasily Gorbik static int gup_pud_range(p4d_t *p4dp, p4d_t p4d, unsigned long addr, unsigned long end,
3030b798bec4SIra Weiny unsigned int flags, struct page **pages, int *nr)
30312667f50eSSteve Capper {
30322667f50eSSteve Capper unsigned long next;
30332667f50eSSteve Capper pud_t *pudp;
30342667f50eSSteve Capper
3035d3f7b1bbSVasily Gorbik pudp = pud_offset_lockless(p4dp, p4d, addr);
30362667f50eSSteve Capper do {
3037e37c6982SChristian Borntraeger pud_t pud = READ_ONCE(*pudp);
30382667f50eSSteve Capper
30392667f50eSSteve Capper next = pud_addr_end(addr, end);
304015494520SQiujun Huang if (unlikely(!pud_present(pud)))
30412667f50eSSteve Capper return 0;
3042fcd0ccd8SJohn Starks if (unlikely(pud_huge(pud) || pud_devmap(pud))) {
3043b798bec4SIra Weiny if (!gup_huge_pud(pud, pudp, addr, next, flags,
30442667f50eSSteve Capper pages, nr))
30452667f50eSSteve Capper return 0;
3046f30c59e9SAneesh Kumar K.V } else if (unlikely(is_hugepd(__hugepd(pud_val(pud))))) {
3047f30c59e9SAneesh Kumar K.V if (!gup_huge_pd(__hugepd(pud_val(pud)), addr,
3048b798bec4SIra Weiny PUD_SHIFT, next, flags, pages, nr))
3049f30c59e9SAneesh Kumar K.V return 0;
3050d3f7b1bbSVasily Gorbik } else if (!gup_pmd_range(pudp, pud, addr, next, flags, pages, nr))
30512667f50eSSteve Capper return 0;
30522667f50eSSteve Capper } while (pudp++, addr = next, addr != end);
30532667f50eSSteve Capper
30542667f50eSSteve Capper return 1;
30552667f50eSSteve Capper }
30562667f50eSSteve Capper
gup_p4d_range(pgd_t * pgdp,pgd_t pgd,unsigned long addr,unsigned long end,unsigned int flags,struct page ** pages,int * nr)3057d3f7b1bbSVasily Gorbik static int gup_p4d_range(pgd_t *pgdp, pgd_t pgd, unsigned long addr, unsigned long end,
3058b798bec4SIra Weiny unsigned int flags, struct page **pages, int *nr)
3059c2febafcSKirill A. Shutemov {
3060c2febafcSKirill A. Shutemov unsigned long next;
3061c2febafcSKirill A. Shutemov p4d_t *p4dp;
3062c2febafcSKirill A. Shutemov
3063d3f7b1bbSVasily Gorbik p4dp = p4d_offset_lockless(pgdp, pgd, addr);
3064c2febafcSKirill A. Shutemov do {
3065c2febafcSKirill A. Shutemov p4d_t p4d = READ_ONCE(*p4dp);
3066c2febafcSKirill A. Shutemov
3067c2febafcSKirill A. Shutemov next = p4d_addr_end(addr, end);
3068c2febafcSKirill A. Shutemov if (p4d_none(p4d))
3069c2febafcSKirill A. Shutemov return 0;
3070c2febafcSKirill A. Shutemov BUILD_BUG_ON(p4d_huge(p4d));
3071c2febafcSKirill A. Shutemov if (unlikely(is_hugepd(__hugepd(p4d_val(p4d))))) {
3072c2febafcSKirill A. Shutemov if (!gup_huge_pd(__hugepd(p4d_val(p4d)), addr,
3073b798bec4SIra Weiny P4D_SHIFT, next, flags, pages, nr))
3074c2febafcSKirill A. Shutemov return 0;
3075d3f7b1bbSVasily Gorbik } else if (!gup_pud_range(p4dp, p4d, addr, next, flags, pages, nr))
3076c2febafcSKirill A. Shutemov return 0;
3077c2febafcSKirill A. Shutemov } while (p4dp++, addr = next, addr != end);
3078c2febafcSKirill A. Shutemov
3079c2febafcSKirill A. Shutemov return 1;
3080c2febafcSKirill A. Shutemov }
3081c2febafcSKirill A. Shutemov
gup_pgd_range(unsigned long addr,unsigned long end,unsigned int flags,struct page ** pages,int * nr)30825b65c467SKirill A. Shutemov static void gup_pgd_range(unsigned long addr, unsigned long end,
3083b798bec4SIra Weiny unsigned int flags, struct page **pages, int *nr)
30845b65c467SKirill A. Shutemov {
30855b65c467SKirill A. Shutemov unsigned long next;
30865b65c467SKirill A. Shutemov pgd_t *pgdp;
30875b65c467SKirill A. Shutemov
30885b65c467SKirill A. Shutemov pgdp = pgd_offset(current->mm, addr);
30895b65c467SKirill A. Shutemov do {
30905b65c467SKirill A. Shutemov pgd_t pgd = READ_ONCE(*pgdp);
30915b65c467SKirill A. Shutemov
30925b65c467SKirill A. Shutemov next = pgd_addr_end(addr, end);
30935b65c467SKirill A. Shutemov if (pgd_none(pgd))
30945b65c467SKirill A. Shutemov return;
30955b65c467SKirill A. Shutemov if (unlikely(pgd_huge(pgd))) {
3096b798bec4SIra Weiny if (!gup_huge_pgd(pgd, pgdp, addr, next, flags,
30975b65c467SKirill A. Shutemov pages, nr))
30985b65c467SKirill A. Shutemov return;
30995b65c467SKirill A. Shutemov } else if (unlikely(is_hugepd(__hugepd(pgd_val(pgd))))) {
31005b65c467SKirill A. Shutemov if (!gup_huge_pd(__hugepd(pgd_val(pgd)), addr,
3101b798bec4SIra Weiny PGDIR_SHIFT, next, flags, pages, nr))
31025b65c467SKirill A. Shutemov return;
3103d3f7b1bbSVasily Gorbik } else if (!gup_p4d_range(pgdp, pgd, addr, next, flags, pages, nr))
31045b65c467SKirill A. Shutemov return;
31055b65c467SKirill A. Shutemov } while (pgdp++, addr = next, addr != end);
31065b65c467SKirill A. Shutemov }
3107050a9adcSChristoph Hellwig #else
gup_pgd_range(unsigned long addr,unsigned long end,unsigned int flags,struct page ** pages,int * nr)3108050a9adcSChristoph Hellwig static inline void gup_pgd_range(unsigned long addr, unsigned long end,
3109050a9adcSChristoph Hellwig unsigned int flags, struct page **pages, int *nr)
3110050a9adcSChristoph Hellwig {
3111050a9adcSChristoph Hellwig }
3112050a9adcSChristoph Hellwig #endif /* CONFIG_HAVE_FAST_GUP */
31135b65c467SKirill A. Shutemov
31145b65c467SKirill A. Shutemov #ifndef gup_fast_permitted
31155b65c467SKirill A. Shutemov /*
3116dadbb612SSouptick Joarder * Check if it's allowed to use get_user_pages_fast_only() for the range, or
31175b65c467SKirill A. Shutemov * we need to fall back to the slow version:
31185b65c467SKirill A. Shutemov */
gup_fast_permitted(unsigned long start,unsigned long end)311926f4c328SChristoph Hellwig static bool gup_fast_permitted(unsigned long start, unsigned long end)
31205b65c467SKirill A. Shutemov {
312126f4c328SChristoph Hellwig return true;
31225b65c467SKirill A. Shutemov }
31235b65c467SKirill A. Shutemov #endif
31245b65c467SKirill A. Shutemov
lockless_pages_from_mm(unsigned long start,unsigned long end,unsigned int gup_flags,struct page ** pages)3125c28b1fc7SJason Gunthorpe static unsigned long lockless_pages_from_mm(unsigned long start,
3126c28b1fc7SJason Gunthorpe unsigned long end,
3127eddb1c22SJohn Hubbard unsigned int gup_flags,
3128eddb1c22SJohn Hubbard struct page **pages)
31292667f50eSSteve Capper {
3130376a34efSJohn Hubbard unsigned long flags;
3131c28b1fc7SJason Gunthorpe int nr_pinned = 0;
313257efa1feSJason Gunthorpe unsigned seq;
3133c28b1fc7SJason Gunthorpe
3134c28b1fc7SJason Gunthorpe if (!IS_ENABLED(CONFIG_HAVE_FAST_GUP) ||
3135c28b1fc7SJason Gunthorpe !gup_fast_permitted(start, end))
3136c28b1fc7SJason Gunthorpe return 0;
3137c28b1fc7SJason Gunthorpe
313857efa1feSJason Gunthorpe if (gup_flags & FOLL_PIN) {
313957efa1feSJason Gunthorpe seq = raw_read_seqcount(¤t->mm->write_protect_seq);
314057efa1feSJason Gunthorpe if (seq & 1)
314157efa1feSJason Gunthorpe return 0;
314257efa1feSJason Gunthorpe }
314357efa1feSJason Gunthorpe
3144c28b1fc7SJason Gunthorpe /*
3145c28b1fc7SJason Gunthorpe * Disable interrupts. The nested form is used, in order to allow full,
3146c28b1fc7SJason Gunthorpe * general purpose use of this routine.
3147c28b1fc7SJason Gunthorpe *
3148c28b1fc7SJason Gunthorpe * With interrupts disabled, we block page table pages from being freed
3149c28b1fc7SJason Gunthorpe * from under us. See struct mmu_table_batch comments in
3150c28b1fc7SJason Gunthorpe * include/asm-generic/tlb.h for more details.
3151c28b1fc7SJason Gunthorpe *
3152c28b1fc7SJason Gunthorpe * We do not adopt an rcu_read_lock() here as we also want to block IPIs
3153c28b1fc7SJason Gunthorpe * that come from THPs splitting.
3154c28b1fc7SJason Gunthorpe */
3155c28b1fc7SJason Gunthorpe local_irq_save(flags);
3156c28b1fc7SJason Gunthorpe gup_pgd_range(start, end, gup_flags, pages, &nr_pinned);
3157c28b1fc7SJason Gunthorpe local_irq_restore(flags);
315857efa1feSJason Gunthorpe
315957efa1feSJason Gunthorpe /*
316057efa1feSJason Gunthorpe * When pinning pages for DMA there could be a concurrent write protect
316157efa1feSJason Gunthorpe * from fork() via copy_page_range(), in this case always fail fast GUP.
316257efa1feSJason Gunthorpe */
316357efa1feSJason Gunthorpe if (gup_flags & FOLL_PIN) {
316457efa1feSJason Gunthorpe if (read_seqcount_retry(¤t->mm->write_protect_seq, seq)) {
3165b6a2619cSDavid Hildenbrand unpin_user_pages_lockless(pages, nr_pinned);
316657efa1feSJason Gunthorpe return 0;
3167b6a2619cSDavid Hildenbrand } else {
3168b6a2619cSDavid Hildenbrand sanity_check_pinned_pages(pages, nr_pinned);
316957efa1feSJason Gunthorpe }
317057efa1feSJason Gunthorpe }
3171c28b1fc7SJason Gunthorpe return nr_pinned;
3172c28b1fc7SJason Gunthorpe }
3173c28b1fc7SJason Gunthorpe
internal_get_user_pages_fast(unsigned long start,unsigned long nr_pages,unsigned int gup_flags,struct page ** pages)3174c28b1fc7SJason Gunthorpe static int internal_get_user_pages_fast(unsigned long start,
3175c28b1fc7SJason Gunthorpe unsigned long nr_pages,
3176c28b1fc7SJason Gunthorpe unsigned int gup_flags,
3177c28b1fc7SJason Gunthorpe struct page **pages)
3178c28b1fc7SJason Gunthorpe {
3179c28b1fc7SJason Gunthorpe unsigned long len, end;
3180c28b1fc7SJason Gunthorpe unsigned long nr_pinned;
3181b2a72dffSJason Gunthorpe int locked = 0;
3182c28b1fc7SJason Gunthorpe int ret;
31832667f50eSSteve Capper
3184f4000fdfSJohn Hubbard if (WARN_ON_ONCE(gup_flags & ~(FOLL_WRITE | FOLL_LONGTERM |
3185376a34efSJohn Hubbard FOLL_FORCE | FOLL_PIN | FOLL_GET |
31864003f107SLogan Gunthorpe FOLL_FAST_ONLY | FOLL_NOFAULT |
3187d74943a2SDavid Hildenbrand FOLL_PCI_P2PDMA | FOLL_HONOR_NUMA_FAULT)))
3188817be129SChristoph Hellwig return -EINVAL;
3189817be129SChristoph Hellwig
3190a458b76aSAndrea Arcangeli if (gup_flags & FOLL_PIN)
3191a458b76aSAndrea Arcangeli mm_set_has_pinned_flag(¤t->mm->flags);
3192008cfe44SPeter Xu
3193f81cd178SJohn Hubbard if (!(gup_flags & FOLL_FAST_ONLY))
3194da1c55f1SMichel Lespinasse might_lock_read(¤t->mm->mmap_lock);
3195f81cd178SJohn Hubbard
3196f455c854SChristoph Hellwig start = untagged_addr(start) & PAGE_MASK;
3197c28b1fc7SJason Gunthorpe len = nr_pages << PAGE_SHIFT;
3198c28b1fc7SJason Gunthorpe if (check_add_overflow(start, len, &end))
31999883c7f8SJason Gunthorpe return -EOVERFLOW;
32006014bc27SLinus Torvalds if (end > TASK_SIZE_MAX)
32016014bc27SLinus Torvalds return -EFAULT;
320296d4f267SLinus Torvalds if (unlikely(!access_ok((void __user *)start, len)))
3203c61611f7SMichael S. Tsirkin return -EFAULT;
320473e10a61SKirill A. Shutemov
3205c28b1fc7SJason Gunthorpe nr_pinned = lockless_pages_from_mm(start, end, gup_flags, pages);
3206c28b1fc7SJason Gunthorpe if (nr_pinned == nr_pages || gup_flags & FOLL_FAST_ONLY)
3207c28b1fc7SJason Gunthorpe return nr_pinned;
3208376a34efSJohn Hubbard
3209c28b1fc7SJason Gunthorpe /* Slow path: try to get the remaining pages with get_user_pages */
32104628b063SPingfan Liu start += nr_pinned << PAGE_SHIFT;
32114628b063SPingfan Liu pages += nr_pinned;
3212b2a72dffSJason Gunthorpe ret = __gup_longterm_locked(current->mm, start, nr_pages - nr_pinned,
3213b2cac248SLorenzo Stoakes pages, &locked,
3214f04740f5SJason Gunthorpe gup_flags | FOLL_TOUCH | FOLL_UNLOCKABLE);
3215c28b1fc7SJason Gunthorpe if (ret < 0) {
3216c28b1fc7SJason Gunthorpe /*
3217c28b1fc7SJason Gunthorpe * The caller has to unpin the pages we already pinned so
3218c28b1fc7SJason Gunthorpe * returning -errno is not an option
3219c28b1fc7SJason Gunthorpe */
3220c28b1fc7SJason Gunthorpe if (nr_pinned)
3221c28b1fc7SJason Gunthorpe return nr_pinned;
32222667f50eSSteve Capper return ret;
32232667f50eSSteve Capper }
3224c28b1fc7SJason Gunthorpe return ret + nr_pinned;
3225c28b1fc7SJason Gunthorpe }
3226c28b1fc7SJason Gunthorpe
3227dadbb612SSouptick Joarder /**
3228dadbb612SSouptick Joarder * get_user_pages_fast_only() - pin user pages in memory
3229dadbb612SSouptick Joarder * @start: starting user address
3230dadbb612SSouptick Joarder * @nr_pages: number of pages from start to pin
3231dadbb612SSouptick Joarder * @gup_flags: flags modifying pin behaviour
3232dadbb612SSouptick Joarder * @pages: array that receives pointers to the pages pinned.
3233dadbb612SSouptick Joarder * Should be at least nr_pages long.
3234dadbb612SSouptick Joarder *
32359e1f0580SJohn Hubbard * Like get_user_pages_fast() except it's IRQ-safe in that it won't fall back to
32369e1f0580SJohn Hubbard * the regular GUP.
32379e1f0580SJohn Hubbard *
32389e1f0580SJohn Hubbard * If the architecture does not support this function, simply return with no
32399e1f0580SJohn Hubbard * pages pinned.
32409e1f0580SJohn Hubbard *
32419e1f0580SJohn Hubbard * Careful, careful! COW breaking can go either way, so a non-write
32429e1f0580SJohn Hubbard * access can get ambiguous page results. If you call this function without
32439e1f0580SJohn Hubbard * 'write' set, you'd better be sure that you're ok with that ambiguity.
32449e1f0580SJohn Hubbard */
get_user_pages_fast_only(unsigned long start,int nr_pages,unsigned int gup_flags,struct page ** pages)3245dadbb612SSouptick Joarder int get_user_pages_fast_only(unsigned long start, int nr_pages,
3246dadbb612SSouptick Joarder unsigned int gup_flags, struct page **pages)
32479e1f0580SJohn Hubbard {
32489e1f0580SJohn Hubbard /*
32499e1f0580SJohn Hubbard * Internally (within mm/gup.c), gup fast variants must set FOLL_GET,
32509e1f0580SJohn Hubbard * because gup fast is always a "pin with a +1 page refcount" request.
3251376a34efSJohn Hubbard *
3252376a34efSJohn Hubbard * FOLL_FAST_ONLY is required in order to match the API description of
3253376a34efSJohn Hubbard * this routine: no fall back to regular ("slow") GUP.
32549e1f0580SJohn Hubbard */
3255b2cac248SLorenzo Stoakes if (!is_valid_gup_args(pages, NULL, &gup_flags,
3256d64e2dbcSJason Gunthorpe FOLL_GET | FOLL_FAST_ONLY))
3257d64e2dbcSJason Gunthorpe return -EINVAL;
32589e1f0580SJohn Hubbard
32599198a919SJason Gunthorpe return internal_get_user_pages_fast(start, nr_pages, gup_flags, pages);
32609e1f0580SJohn Hubbard }
3261dadbb612SSouptick Joarder EXPORT_SYMBOL_GPL(get_user_pages_fast_only);
32629e1f0580SJohn Hubbard
3263eddb1c22SJohn Hubbard /**
3264eddb1c22SJohn Hubbard * get_user_pages_fast() - pin user pages in memory
3265eddb1c22SJohn Hubbard * @start: starting user address
3266eddb1c22SJohn Hubbard * @nr_pages: number of pages from start to pin
3267eddb1c22SJohn Hubbard * @gup_flags: flags modifying pin behaviour
3268eddb1c22SJohn Hubbard * @pages: array that receives pointers to the pages pinned.
3269eddb1c22SJohn Hubbard * Should be at least nr_pages long.
3270eddb1c22SJohn Hubbard *
3271c1e8d7c6SMichel Lespinasse * Attempt to pin user pages in memory without taking mm->mmap_lock.
3272eddb1c22SJohn Hubbard * If not successful, it will fall back to taking the lock and
3273eddb1c22SJohn Hubbard * calling get_user_pages().
3274eddb1c22SJohn Hubbard *
3275eddb1c22SJohn Hubbard * Returns number of pages pinned. This may be fewer than the number requested.
3276eddb1c22SJohn Hubbard * If nr_pages is 0 or negative, returns 0. If no pages were pinned, returns
3277eddb1c22SJohn Hubbard * -errno.
3278eddb1c22SJohn Hubbard */
get_user_pages_fast(unsigned long start,int nr_pages,unsigned int gup_flags,struct page ** pages)3279eddb1c22SJohn Hubbard int get_user_pages_fast(unsigned long start, int nr_pages,
3280eddb1c22SJohn Hubbard unsigned int gup_flags, struct page **pages)
3281eddb1c22SJohn Hubbard {
328294202f12SJohn Hubbard /*
328394202f12SJohn Hubbard * The caller may or may not have explicitly set FOLL_GET; either way is
328494202f12SJohn Hubbard * OK. However, internally (within mm/gup.c), gup fast variants must set
328594202f12SJohn Hubbard * FOLL_GET, because gup fast is always a "pin with a +1 page refcount"
328694202f12SJohn Hubbard * request.
328794202f12SJohn Hubbard */
3288b2cac248SLorenzo Stoakes if (!is_valid_gup_args(pages, NULL, &gup_flags, FOLL_GET))
3289d64e2dbcSJason Gunthorpe return -EINVAL;
3290eddb1c22SJohn Hubbard return internal_get_user_pages_fast(start, nr_pages, gup_flags, pages);
3291eddb1c22SJohn Hubbard }
3292050a9adcSChristoph Hellwig EXPORT_SYMBOL_GPL(get_user_pages_fast);
3293eddb1c22SJohn Hubbard
3294eddb1c22SJohn Hubbard /**
3295eddb1c22SJohn Hubbard * pin_user_pages_fast() - pin user pages in memory without taking locks
3296eddb1c22SJohn Hubbard *
32973faa52c0SJohn Hubbard * @start: starting user address
32983faa52c0SJohn Hubbard * @nr_pages: number of pages from start to pin
32993faa52c0SJohn Hubbard * @gup_flags: flags modifying pin behaviour
33003faa52c0SJohn Hubbard * @pages: array that receives pointers to the pages pinned.
33013faa52c0SJohn Hubbard * Should be at least nr_pages long.
33023faa52c0SJohn Hubbard *
33033faa52c0SJohn Hubbard * Nearly the same as get_user_pages_fast(), except that FOLL_PIN is set. See
33043faa52c0SJohn Hubbard * get_user_pages_fast() for documentation on the function arguments, because
33053faa52c0SJohn Hubbard * the arguments here are identical.
33063faa52c0SJohn Hubbard *
33073faa52c0SJohn Hubbard * FOLL_PIN means that the pages must be released via unpin_user_page(). Please
330872ef5e52SMauro Carvalho Chehab * see Documentation/core-api/pin_user_pages.rst for further details.
3309c8070b78SDavid Howells *
3310c8070b78SDavid Howells * Note that if a zero_page is amongst the returned pages, it will not have
3311c8070b78SDavid Howells * pins in it and unpin_user_page() will not remove pins from it.
3312eddb1c22SJohn Hubbard */
pin_user_pages_fast(unsigned long start,int nr_pages,unsigned int gup_flags,struct page ** pages)3313eddb1c22SJohn Hubbard int pin_user_pages_fast(unsigned long start, int nr_pages,
3314eddb1c22SJohn Hubbard unsigned int gup_flags, struct page **pages)
3315eddb1c22SJohn Hubbard {
3316b2cac248SLorenzo Stoakes if (!is_valid_gup_args(pages, NULL, &gup_flags, FOLL_PIN))
33173faa52c0SJohn Hubbard return -EINVAL;
33183faa52c0SJohn Hubbard return internal_get_user_pages_fast(start, nr_pages, gup_flags, pages);
3319eddb1c22SJohn Hubbard }
3320eddb1c22SJohn Hubbard EXPORT_SYMBOL_GPL(pin_user_pages_fast);
3321eddb1c22SJohn Hubbard
3322eddb1c22SJohn Hubbard /**
332364019a2eSPeter Xu * pin_user_pages_remote() - pin pages of a remote process
3324eddb1c22SJohn Hubbard *
33253faa52c0SJohn Hubbard * @mm: mm_struct of target mm
33263faa52c0SJohn Hubbard * @start: starting user address
33273faa52c0SJohn Hubbard * @nr_pages: number of pages from start to pin
33283faa52c0SJohn Hubbard * @gup_flags: flags modifying lookup behaviour
33293faa52c0SJohn Hubbard * @pages: array that receives pointers to the pages pinned.
33300768c8deSYury Norov * Should be at least nr_pages long.
33313faa52c0SJohn Hubbard * @locked: pointer to lock flag indicating whether lock is held and
33323faa52c0SJohn Hubbard * subsequently whether VM_FAULT_RETRY functionality can be
33333faa52c0SJohn Hubbard * utilised. Lock must initially be held.
33343faa52c0SJohn Hubbard *
33353faa52c0SJohn Hubbard * Nearly the same as get_user_pages_remote(), except that FOLL_PIN is set. See
33363faa52c0SJohn Hubbard * get_user_pages_remote() for documentation on the function arguments, because
33373faa52c0SJohn Hubbard * the arguments here are identical.
33383faa52c0SJohn Hubbard *
33393faa52c0SJohn Hubbard * FOLL_PIN means that the pages must be released via unpin_user_page(). Please
334072ef5e52SMauro Carvalho Chehab * see Documentation/core-api/pin_user_pages.rst for details.
3341c8070b78SDavid Howells *
3342c8070b78SDavid Howells * Note that if a zero_page is amongst the returned pages, it will not have
3343c8070b78SDavid Howells * pins in it and unpin_user_page*() will not remove pins from it.
3344eddb1c22SJohn 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)334564019a2eSPeter Xu long pin_user_pages_remote(struct mm_struct *mm,
3346eddb1c22SJohn Hubbard unsigned long start, unsigned long nr_pages,
3347eddb1c22SJohn Hubbard unsigned int gup_flags, struct page **pages,
33480b295316SLorenzo Stoakes int *locked)
3349eddb1c22SJohn Hubbard {
33509a863a6aSJason Gunthorpe int local_locked = 1;
33519a863a6aSJason Gunthorpe
3352b2cac248SLorenzo Stoakes if (!is_valid_gup_args(pages, locked, &gup_flags,
3353d64e2dbcSJason Gunthorpe FOLL_PIN | FOLL_TOUCH | FOLL_REMOTE))
3354d64e2dbcSJason Gunthorpe return 0;
3355b2cac248SLorenzo Stoakes return __gup_longterm_locked(mm, start, nr_pages, pages,
33569a863a6aSJason Gunthorpe locked ? locked : &local_locked,
3357d64e2dbcSJason Gunthorpe gup_flags);
3358eddb1c22SJohn Hubbard }
3359eddb1c22SJohn Hubbard EXPORT_SYMBOL(pin_user_pages_remote);
3360eddb1c22SJohn Hubbard
3361eddb1c22SJohn Hubbard /**
3362eddb1c22SJohn Hubbard * pin_user_pages() - pin user pages in memory for use by other devices
3363eddb1c22SJohn Hubbard *
33643faa52c0SJohn Hubbard * @start: starting user address
33653faa52c0SJohn Hubbard * @nr_pages: number of pages from start to pin
33663faa52c0SJohn Hubbard * @gup_flags: flags modifying lookup behaviour
33673faa52c0SJohn Hubbard * @pages: array that receives pointers to the pages pinned.
33680768c8deSYury Norov * Should be at least nr_pages long.
33693faa52c0SJohn Hubbard *
33703faa52c0SJohn Hubbard * Nearly the same as get_user_pages(), except that FOLL_TOUCH is not set, and
33713faa52c0SJohn Hubbard * FOLL_PIN is set.
33723faa52c0SJohn Hubbard *
33733faa52c0SJohn Hubbard * FOLL_PIN means that the pages must be released via unpin_user_page(). Please
337472ef5e52SMauro Carvalho Chehab * see Documentation/core-api/pin_user_pages.rst for details.
3375c8070b78SDavid Howells *
3376c8070b78SDavid Howells * Note that if a zero_page is amongst the returned pages, it will not have
3377c8070b78SDavid Howells * pins in it and unpin_user_page*() will not remove pins from it.
3378eddb1c22SJohn Hubbard */
pin_user_pages(unsigned long start,unsigned long nr_pages,unsigned int gup_flags,struct page ** pages)3379eddb1c22SJohn Hubbard long pin_user_pages(unsigned long start, unsigned long nr_pages,
33804c630f30SLorenzo Stoakes unsigned int gup_flags, struct page **pages)
3381eddb1c22SJohn Hubbard {
33829a863a6aSJason Gunthorpe int locked = 1;
33839a863a6aSJason Gunthorpe
3384b2cac248SLorenzo Stoakes if (!is_valid_gup_args(pages, NULL, &gup_flags, FOLL_PIN))
3385d64e2dbcSJason Gunthorpe return 0;
338664019a2eSPeter Xu return __gup_longterm_locked(current->mm, start, nr_pages,
3387b2cac248SLorenzo Stoakes pages, &locked, gup_flags);
3388eddb1c22SJohn Hubbard }
3389eddb1c22SJohn Hubbard EXPORT_SYMBOL(pin_user_pages);
339091429023SJohn Hubbard
339191429023SJohn Hubbard /*
339291429023SJohn Hubbard * pin_user_pages_unlocked() is the FOLL_PIN variant of
339391429023SJohn Hubbard * get_user_pages_unlocked(). Behavior is the same, except that this one sets
339491429023SJohn Hubbard * FOLL_PIN and rejects FOLL_GET.
3395c8070b78SDavid Howells *
3396c8070b78SDavid Howells * Note that if a zero_page is amongst the returned pages, it will not have
3397c8070b78SDavid Howells * pins in it and unpin_user_page*() will not remove pins from it.
339891429023SJohn Hubbard */
pin_user_pages_unlocked(unsigned long start,unsigned long nr_pages,struct page ** pages,unsigned int gup_flags)339991429023SJohn Hubbard long pin_user_pages_unlocked(unsigned long start, unsigned long nr_pages,
340091429023SJohn Hubbard struct page **pages, unsigned int gup_flags)
340191429023SJohn Hubbard {
3402b2a72dffSJason Gunthorpe int locked = 0;
340391429023SJohn Hubbard
3404b2cac248SLorenzo Stoakes if (!is_valid_gup_args(pages, NULL, &gup_flags,
3405f04740f5SJason Gunthorpe FOLL_PIN | FOLL_TOUCH | FOLL_UNLOCKABLE))
3406d64e2dbcSJason Gunthorpe return 0;
34070768c8deSYury Norov
3408b2cac248SLorenzo Stoakes return __gup_longterm_locked(current->mm, start, nr_pages, pages,
3409b2a72dffSJason Gunthorpe &locked, gup_flags);
341091429023SJohn Hubbard }
341191429023SJohn Hubbard EXPORT_SYMBOL(pin_user_pages_unlocked);
3412