blob: c40e977f33c932ee27c9e0030dd0f77423cfc760 [file] [log] [blame]
Thomas Gleixner457c8992019-05-19 13:08:55 +01001// SPDX-License-Identifier: GPL-2.0-only
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -07002#include <linux/kernel.h>
3#include <linux/errno.h>
4#include <linux/err.h>
5#include <linux/spinlock.h>
6
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -07007#include <linux/mm.h>
Dan Williams3565fce2016-01-15 16:56:55 -08008#include <linux/memremap.h>
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -07009#include <linux/pagemap.h>
10#include <linux/rmap.h>
11#include <linux/swap.h>
12#include <linux/swapops.h>
13
Ingo Molnar174cd4b2017-02-02 19:15:33 +010014#include <linux/sched/signal.h>
Steve Capper2667f502014-10-09 15:29:14 -070015#include <linux/rwsem.h>
Aneesh Kumar K.Vf30c59e2014-11-05 21:57:40 +053016#include <linux/hugetlb.h>
Aneesh Kumar K.V9a4e9f32019-03-05 15:47:44 -080017#include <linux/migrate.h>
18#include <linux/mm_inline.h>
19#include <linux/sched/mm.h>
Kirill A. Shutemov1027e442015-09-04 15:47:55 -070020
Dave Hansen33a709b2016-02-12 13:02:19 -080021#include <asm/mmu_context.h>
Steve Capper2667f502014-10-09 15:29:14 -070022#include <asm/pgtable.h>
Kirill A. Shutemov1027e442015-09-04 15:47:55 -070023#include <asm/tlbflush.h>
Steve Capper2667f502014-10-09 15:29:14 -070024
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -070025#include "internal.h"
26
Keith Buschdf06b372018-10-26 15:10:28 -070027struct follow_page_context {
28 struct dev_pagemap *pgmap;
29 unsigned int page_mask;
30};
31
John Hubbarda707cdd2020-01-30 22:12:21 -080032/*
33 * Return the compound head page with ref appropriately incremented,
34 * or NULL if that failed.
35 */
36static inline struct page *try_get_compound_head(struct page *page, int refs)
37{
38 struct page *head = compound_head(page);
39
40 if (WARN_ON_ONCE(page_ref_count(head) < 0))
41 return NULL;
42 if (unlikely(!page_cache_add_speculative(head, refs)))
43 return NULL;
44 return head;
45}
46
John Hubbardfc1d8e72019-05-13 17:19:08 -070047/**
akpm@linux-foundation.org2d15eb32019-09-23 15:35:04 -070048 * put_user_pages_dirty_lock() - release and optionally dirty gup-pinned pages
49 * @pages: array of pages to be maybe marked dirty, and definitely released.
John Hubbardfc1d8e72019-05-13 17:19:08 -070050 * @npages: number of pages in the @pages array.
akpm@linux-foundation.org2d15eb32019-09-23 15:35:04 -070051 * @make_dirty: whether to mark the pages dirty
John Hubbardfc1d8e72019-05-13 17:19:08 -070052 *
53 * "gup-pinned page" refers to a page that has had one of the get_user_pages()
54 * variants called on that page.
55 *
56 * For each page in the @pages array, make that page (or its head page, if a
akpm@linux-foundation.org2d15eb32019-09-23 15:35:04 -070057 * compound page) dirty, if @make_dirty is true, and if the page was previously
58 * listed as clean. In any case, releases all pages using put_user_page(),
59 * possibly via put_user_pages(), for the non-dirty case.
John Hubbardfc1d8e72019-05-13 17:19:08 -070060 *
61 * Please see the put_user_page() documentation for details.
62 *
akpm@linux-foundation.org2d15eb32019-09-23 15:35:04 -070063 * set_page_dirty_lock() is used internally. If instead, set_page_dirty() is
64 * required, then the caller should a) verify that this is really correct,
65 * because _lock() is usually required, and b) hand code it:
66 * set_page_dirty_lock(), put_user_page().
John Hubbardfc1d8e72019-05-13 17:19:08 -070067 *
68 */
akpm@linux-foundation.org2d15eb32019-09-23 15:35:04 -070069void put_user_pages_dirty_lock(struct page **pages, unsigned long npages,
70 bool make_dirty)
John Hubbardfc1d8e72019-05-13 17:19:08 -070071{
akpm@linux-foundation.org2d15eb32019-09-23 15:35:04 -070072 unsigned long index;
John Hubbardfc1d8e72019-05-13 17:19:08 -070073
akpm@linux-foundation.org2d15eb32019-09-23 15:35:04 -070074 /*
75 * TODO: this can be optimized for huge pages: if a series of pages is
76 * physically contiguous and part of the same compound page, then a
77 * single operation to the head page should suffice.
78 */
79
80 if (!make_dirty) {
81 put_user_pages(pages, npages);
82 return;
83 }
84
85 for (index = 0; index < npages; index++) {
86 struct page *page = compound_head(pages[index]);
87 /*
88 * Checking PageDirty at this point may race with
89 * clear_page_dirty_for_io(), but that's OK. Two key
90 * cases:
91 *
92 * 1) This code sees the page as already dirty, so it
93 * skips the call to set_page_dirty(). That could happen
94 * because clear_page_dirty_for_io() called
95 * page_mkclean(), followed by set_page_dirty().
96 * However, now the page is going to get written back,
97 * which meets the original intention of setting it
98 * dirty, so all is well: clear_page_dirty_for_io() goes
99 * on to call TestClearPageDirty(), and write the page
100 * back.
101 *
102 * 2) This code sees the page as clean, so it calls
103 * set_page_dirty(). The page stays dirty, despite being
104 * written back, so it gets written back again in the
105 * next writeback cycle. This is harmless.
106 */
107 if (!PageDirty(page))
108 set_page_dirty_lock(page);
109 put_user_page(page);
110 }
John Hubbardfc1d8e72019-05-13 17:19:08 -0700111}
112EXPORT_SYMBOL(put_user_pages_dirty_lock);
113
114/**
115 * put_user_pages() - release an array of gup-pinned pages.
116 * @pages: array of pages to be marked dirty and released.
117 * @npages: number of pages in the @pages array.
118 *
119 * For each page in the @pages array, release the page using put_user_page().
120 *
121 * Please see the put_user_page() documentation for details.
122 */
123void put_user_pages(struct page **pages, unsigned long npages)
124{
125 unsigned long index;
126
127 /*
128 * TODO: this can be optimized for huge pages: if a series of pages is
129 * physically contiguous and part of the same compound page, then a
130 * single operation to the head page should suffice.
131 */
132 for (index = 0; index < npages; index++)
133 put_user_page(pages[index]);
134}
135EXPORT_SYMBOL(put_user_pages);
136
Christoph Hellwig050a9ad2019-07-11 20:57:21 -0700137#ifdef CONFIG_MMU
Kirill A. Shutemov69e68b42014-06-04 16:08:11 -0700138static struct page *no_page_table(struct vm_area_struct *vma,
139 unsigned int flags)
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700140{
Kirill A. Shutemov69e68b42014-06-04 16:08:11 -0700141 /*
142 * When core dumping an enormous anonymous area that nobody
143 * has touched so far, we don't want to allocate unnecessary pages or
144 * page tables. Return error instead of NULL to skip handle_mm_fault,
145 * then get_dump_page() will return NULL to leave a hole in the dump.
146 * But we can only make this optimization where a hole would surely
147 * be zero-filled if handle_mm_fault() actually did handle it.
148 */
149 if ((flags & FOLL_DUMP) && (!vma->vm_ops || !vma->vm_ops->fault))
150 return ERR_PTR(-EFAULT);
151 return NULL;
152}
153
Kirill A. Shutemov1027e442015-09-04 15:47:55 -0700154static int follow_pfn_pte(struct vm_area_struct *vma, unsigned long address,
155 pte_t *pte, unsigned int flags)
156{
157 /* No page to get reference */
158 if (flags & FOLL_GET)
159 return -EFAULT;
160
161 if (flags & FOLL_TOUCH) {
162 pte_t entry = *pte;
163
164 if (flags & FOLL_WRITE)
165 entry = pte_mkdirty(entry);
166 entry = pte_mkyoung(entry);
167
168 if (!pte_same(*pte, entry)) {
169 set_pte_at(vma->vm_mm, address, pte, entry);
170 update_mmu_cache(vma, address, pte);
171 }
172 }
173
174 /* Proper page table entry exists, but no corresponding struct page */
175 return -EEXIST;
176}
177
Linus Torvalds19be0ea2016-10-13 13:07:36 -0700178/*
179 * FOLL_FORCE can write to even unwritable pte's, but only
180 * after we've gone through a COW cycle and they are dirty.
181 */
182static inline bool can_follow_write_pte(pte_t pte, unsigned int flags)
183{
Linus Torvaldsf6f37322017-12-15 18:53:22 -0800184 return pte_write(pte) ||
Linus Torvalds19be0ea2016-10-13 13:07:36 -0700185 ((flags & FOLL_FORCE) && (flags & FOLL_COW) && pte_dirty(pte));
186}
187
Kirill A. Shutemov69e68b42014-06-04 16:08:11 -0700188static struct page *follow_page_pte(struct vm_area_struct *vma,
Keith Buschdf06b372018-10-26 15:10:28 -0700189 unsigned long address, pmd_t *pmd, unsigned int flags,
190 struct dev_pagemap **pgmap)
Kirill A. Shutemov69e68b42014-06-04 16:08:11 -0700191{
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700192 struct mm_struct *mm = vma->vm_mm;
Kirill A. Shutemov69e68b42014-06-04 16:08:11 -0700193 struct page *page;
194 spinlock_t *ptl;
195 pte_t *ptep, pte;
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700196
Kirill A. Shutemov69e68b42014-06-04 16:08:11 -0700197retry:
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700198 if (unlikely(pmd_bad(*pmd)))
Kirill A. Shutemov69e68b42014-06-04 16:08:11 -0700199 return no_page_table(vma, flags);
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700200
201 ptep = pte_offset_map_lock(mm, pmd, address, &ptl);
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700202 pte = *ptep;
203 if (!pte_present(pte)) {
204 swp_entry_t entry;
205 /*
206 * KSM's break_ksm() relies upon recognizing a ksm page
207 * even while it is being migrated, so for that case we
208 * need migration_entry_wait().
209 */
210 if (likely(!(flags & FOLL_MIGRATION)))
211 goto no_page;
Kirill A. Shutemov0661a332015-02-10 14:10:04 -0800212 if (pte_none(pte))
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700213 goto no_page;
214 entry = pte_to_swp_entry(pte);
215 if (!is_migration_entry(entry))
216 goto no_page;
217 pte_unmap_unlock(ptep, ptl);
218 migration_entry_wait(mm, pmd, address);
Kirill A. Shutemov69e68b42014-06-04 16:08:11 -0700219 goto retry;
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700220 }
Mel Gorman8a0516e2015-02-12 14:58:22 -0800221 if ((flags & FOLL_NUMA) && pte_protnone(pte))
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700222 goto no_page;
Linus Torvalds19be0ea2016-10-13 13:07:36 -0700223 if ((flags & FOLL_WRITE) && !can_follow_write_pte(pte, flags)) {
Kirill A. Shutemov69e68b42014-06-04 16:08:11 -0700224 pte_unmap_unlock(ptep, ptl);
225 return NULL;
226 }
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700227
228 page = vm_normal_page(vma, address, pte);
Dan Williams3565fce2016-01-15 16:56:55 -0800229 if (!page && pte_devmap(pte) && (flags & FOLL_GET)) {
230 /*
231 * Only return device mapping pages in the FOLL_GET case since
232 * they are only valid while holding the pgmap reference.
233 */
Keith Buschdf06b372018-10-26 15:10:28 -0700234 *pgmap = get_dev_pagemap(pte_pfn(pte), *pgmap);
235 if (*pgmap)
Dan Williams3565fce2016-01-15 16:56:55 -0800236 page = pte_page(pte);
237 else
238 goto no_page;
239 } else if (unlikely(!page)) {
Kirill A. Shutemov1027e442015-09-04 15:47:55 -0700240 if (flags & FOLL_DUMP) {
241 /* Avoid special (like zero) pages in core dumps */
242 page = ERR_PTR(-EFAULT);
243 goto out;
244 }
245
246 if (is_zero_pfn(pte_pfn(pte))) {
247 page = pte_page(pte);
248 } else {
249 int ret;
250
251 ret = follow_pfn_pte(vma, address, ptep, flags);
252 page = ERR_PTR(ret);
253 goto out;
254 }
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700255 }
256
Kirill A. Shutemov6742d292016-01-15 16:52:28 -0800257 if (flags & FOLL_SPLIT && PageTransCompound(page)) {
258 int ret;
259 get_page(page);
260 pte_unmap_unlock(ptep, ptl);
261 lock_page(page);
262 ret = split_huge_page(page);
263 unlock_page(page);
264 put_page(page);
265 if (ret)
266 return ERR_PTR(ret);
267 goto retry;
268 }
269
Linus Torvalds8fde12c2019-04-11 10:49:19 -0700270 if (flags & FOLL_GET) {
271 if (unlikely(!try_get_page(page))) {
272 page = ERR_PTR(-ENOMEM);
273 goto out;
274 }
275 }
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700276 if (flags & FOLL_TOUCH) {
277 if ((flags & FOLL_WRITE) &&
278 !pte_dirty(pte) && !PageDirty(page))
279 set_page_dirty(page);
280 /*
281 * pte_mkyoung() would be more correct here, but atomic care
282 * is needed to avoid losing the dirty bit: it is easier to use
283 * mark_page_accessed().
284 */
285 mark_page_accessed(page);
286 }
Eric B Munsonde60f5f2015-11-05 18:51:36 -0800287 if ((flags & FOLL_MLOCK) && (vma->vm_flags & VM_LOCKED)) {
Kirill A. Shutemove90309c2016-01-15 16:54:33 -0800288 /* Do not mlock pte-mapped THP */
289 if (PageTransCompound(page))
290 goto out;
291
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700292 /*
293 * The preliminary mapping check is mainly to avoid the
294 * pointless overhead of lock_page on the ZERO_PAGE
295 * which might bounce very badly if there is contention.
296 *
297 * If the page is already locked, we don't need to
298 * handle it now - vmscan will handle it later if and
299 * when it attempts to reclaim the page.
300 */
301 if (page->mapping && trylock_page(page)) {
302 lru_add_drain(); /* push cached pages to LRU */
303 /*
304 * Because we lock page here, and migration is
305 * blocked by the pte's page reference, and we
306 * know the page is still mapped, we don't even
307 * need to check for file-cache page truncation.
308 */
309 mlock_vma_page(page);
310 unlock_page(page);
311 }
312 }
Kirill A. Shutemov1027e442015-09-04 15:47:55 -0700313out:
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700314 pte_unmap_unlock(ptep, ptl);
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700315 return page;
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700316no_page:
317 pte_unmap_unlock(ptep, ptl);
318 if (!pte_none(pte))
Kirill A. Shutemov69e68b42014-06-04 16:08:11 -0700319 return NULL;
320 return no_page_table(vma, flags);
321}
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700322
Aneesh Kumar K.V080dbb62017-07-06 15:38:44 -0700323static struct page *follow_pmd_mask(struct vm_area_struct *vma,
324 unsigned long address, pud_t *pudp,
Keith Buschdf06b372018-10-26 15:10:28 -0700325 unsigned int flags,
326 struct follow_page_context *ctx)
Kirill A. Shutemov69e68b42014-06-04 16:08:11 -0700327{
Huang Ying68827282018-06-07 17:06:34 -0700328 pmd_t *pmd, pmdval;
Kirill A. Shutemov69e68b42014-06-04 16:08:11 -0700329 spinlock_t *ptl;
330 struct page *page;
331 struct mm_struct *mm = vma->vm_mm;
332
Aneesh Kumar K.V080dbb62017-07-06 15:38:44 -0700333 pmd = pmd_offset(pudp, address);
Huang Ying68827282018-06-07 17:06:34 -0700334 /*
335 * The READ_ONCE() will stabilize the pmdval in a register or
336 * on the stack so that it will stop changing under the code.
337 */
338 pmdval = READ_ONCE(*pmd);
339 if (pmd_none(pmdval))
Kirill A. Shutemov69e68b42014-06-04 16:08:11 -0700340 return no_page_table(vma, flags);
Wei Yangbe9d3042020-01-30 22:12:14 -0800341 if (pmd_huge(pmdval) && is_vm_hugetlb_page(vma)) {
Naoya Horiguchie66f17f2015-02-11 15:25:22 -0800342 page = follow_huge_pmd(mm, address, pmd, flags);
343 if (page)
344 return page;
345 return no_page_table(vma, flags);
Kirill A. Shutemov69e68b42014-06-04 16:08:11 -0700346 }
Huang Ying68827282018-06-07 17:06:34 -0700347 if (is_hugepd(__hugepd(pmd_val(pmdval)))) {
Aneesh Kumar K.V4dc71452017-07-06 15:38:56 -0700348 page = follow_huge_pd(vma, address,
Huang Ying68827282018-06-07 17:06:34 -0700349 __hugepd(pmd_val(pmdval)), flags,
Aneesh Kumar K.V4dc71452017-07-06 15:38:56 -0700350 PMD_SHIFT);
351 if (page)
352 return page;
353 return no_page_table(vma, flags);
354 }
Zi Yan84c3fc42017-09-08 16:11:01 -0700355retry:
Huang Ying68827282018-06-07 17:06:34 -0700356 if (!pmd_present(pmdval)) {
Zi Yan84c3fc42017-09-08 16:11:01 -0700357 if (likely(!(flags & FOLL_MIGRATION)))
358 return no_page_table(vma, flags);
359 VM_BUG_ON(thp_migration_supported() &&
Huang Ying68827282018-06-07 17:06:34 -0700360 !is_pmd_migration_entry(pmdval));
361 if (is_pmd_migration_entry(pmdval))
Zi Yan84c3fc42017-09-08 16:11:01 -0700362 pmd_migration_entry_wait(mm, pmd);
Huang Ying68827282018-06-07 17:06:34 -0700363 pmdval = READ_ONCE(*pmd);
364 /*
365 * MADV_DONTNEED may convert the pmd to null because
366 * mmap_sem is held in read mode
367 */
368 if (pmd_none(pmdval))
369 return no_page_table(vma, flags);
Zi Yan84c3fc42017-09-08 16:11:01 -0700370 goto retry;
371 }
Huang Ying68827282018-06-07 17:06:34 -0700372 if (pmd_devmap(pmdval)) {
Dan Williams3565fce2016-01-15 16:56:55 -0800373 ptl = pmd_lock(mm, pmd);
Keith Buschdf06b372018-10-26 15:10:28 -0700374 page = follow_devmap_pmd(vma, address, pmd, flags, &ctx->pgmap);
Dan Williams3565fce2016-01-15 16:56:55 -0800375 spin_unlock(ptl);
376 if (page)
377 return page;
378 }
Huang Ying68827282018-06-07 17:06:34 -0700379 if (likely(!pmd_trans_huge(pmdval)))
Keith Buschdf06b372018-10-26 15:10:28 -0700380 return follow_page_pte(vma, address, pmd, flags, &ctx->pgmap);
Kirill A. Shutemov6742d292016-01-15 16:52:28 -0800381
Huang Ying68827282018-06-07 17:06:34 -0700382 if ((flags & FOLL_NUMA) && pmd_protnone(pmdval))
Aneesh Kumar K.Vdb08f202017-02-24 14:59:53 -0800383 return no_page_table(vma, flags);
384
Zi Yan84c3fc42017-09-08 16:11:01 -0700385retry_locked:
Kirill A. Shutemov6742d292016-01-15 16:52:28 -0800386 ptl = pmd_lock(mm, pmd);
Huang Ying68827282018-06-07 17:06:34 -0700387 if (unlikely(pmd_none(*pmd))) {
388 spin_unlock(ptl);
389 return no_page_table(vma, flags);
390 }
Zi Yan84c3fc42017-09-08 16:11:01 -0700391 if (unlikely(!pmd_present(*pmd))) {
392 spin_unlock(ptl);
393 if (likely(!(flags & FOLL_MIGRATION)))
394 return no_page_table(vma, flags);
395 pmd_migration_entry_wait(mm, pmd);
396 goto retry_locked;
397 }
Kirill A. Shutemov6742d292016-01-15 16:52:28 -0800398 if (unlikely(!pmd_trans_huge(*pmd))) {
399 spin_unlock(ptl);
Keith Buschdf06b372018-10-26 15:10:28 -0700400 return follow_page_pte(vma, address, pmd, flags, &ctx->pgmap);
Kirill A. Shutemov69e68b42014-06-04 16:08:11 -0700401 }
Song Liubfe7b002019-09-23 15:38:25 -0700402 if (flags & (FOLL_SPLIT | FOLL_SPLIT_PMD)) {
Kirill A. Shutemov6742d292016-01-15 16:52:28 -0800403 int ret;
404 page = pmd_page(*pmd);
405 if (is_huge_zero_page(page)) {
406 spin_unlock(ptl);
407 ret = 0;
Kirill A. Shutemov78ddc532016-01-15 16:52:42 -0800408 split_huge_pmd(vma, pmd, address);
Naoya Horiguchi337d9ab2016-07-26 15:24:03 -0700409 if (pmd_trans_unstable(pmd))
410 ret = -EBUSY;
Song Liubfe7b002019-09-23 15:38:25 -0700411 } else if (flags & FOLL_SPLIT) {
Linus Torvalds8fde12c2019-04-11 10:49:19 -0700412 if (unlikely(!try_get_page(page))) {
413 spin_unlock(ptl);
414 return ERR_PTR(-ENOMEM);
415 }
Kirill A. Shutemov6742d292016-01-15 16:52:28 -0800416 spin_unlock(ptl);
417 lock_page(page);
418 ret = split_huge_page(page);
419 unlock_page(page);
420 put_page(page);
Kirill A. Shutemovbaa355f2016-07-26 15:25:51 -0700421 if (pmd_none(*pmd))
422 return no_page_table(vma, flags);
Song Liubfe7b002019-09-23 15:38:25 -0700423 } else { /* flags & FOLL_SPLIT_PMD */
424 spin_unlock(ptl);
425 split_huge_pmd(vma, pmd, address);
426 ret = pte_alloc(mm, pmd) ? -ENOMEM : 0;
Kirill A. Shutemov6742d292016-01-15 16:52:28 -0800427 }
428
429 return ret ? ERR_PTR(ret) :
Keith Buschdf06b372018-10-26 15:10:28 -0700430 follow_page_pte(vma, address, pmd, flags, &ctx->pgmap);
Kirill A. Shutemov6742d292016-01-15 16:52:28 -0800431 }
Kirill A. Shutemov6742d292016-01-15 16:52:28 -0800432 page = follow_trans_huge_pmd(vma, address, pmd, flags);
433 spin_unlock(ptl);
Keith Buschdf06b372018-10-26 15:10:28 -0700434 ctx->page_mask = HPAGE_PMD_NR - 1;
Kirill A. Shutemov6742d292016-01-15 16:52:28 -0800435 return page;
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700436}
437
Aneesh Kumar K.V080dbb62017-07-06 15:38:44 -0700438static struct page *follow_pud_mask(struct vm_area_struct *vma,
439 unsigned long address, p4d_t *p4dp,
Keith Buschdf06b372018-10-26 15:10:28 -0700440 unsigned int flags,
441 struct follow_page_context *ctx)
Aneesh Kumar K.V080dbb62017-07-06 15:38:44 -0700442{
443 pud_t *pud;
444 spinlock_t *ptl;
445 struct page *page;
446 struct mm_struct *mm = vma->vm_mm;
447
448 pud = pud_offset(p4dp, address);
449 if (pud_none(*pud))
450 return no_page_table(vma, flags);
Wei Yangbe9d3042020-01-30 22:12:14 -0800451 if (pud_huge(*pud) && is_vm_hugetlb_page(vma)) {
Aneesh Kumar K.V080dbb62017-07-06 15:38:44 -0700452 page = follow_huge_pud(mm, address, pud, flags);
453 if (page)
454 return page;
455 return no_page_table(vma, flags);
456 }
Aneesh Kumar K.V4dc71452017-07-06 15:38:56 -0700457 if (is_hugepd(__hugepd(pud_val(*pud)))) {
458 page = follow_huge_pd(vma, address,
459 __hugepd(pud_val(*pud)), flags,
460 PUD_SHIFT);
461 if (page)
462 return page;
463 return no_page_table(vma, flags);
464 }
Aneesh Kumar K.V080dbb62017-07-06 15:38:44 -0700465 if (pud_devmap(*pud)) {
466 ptl = pud_lock(mm, pud);
Keith Buschdf06b372018-10-26 15:10:28 -0700467 page = follow_devmap_pud(vma, address, pud, flags, &ctx->pgmap);
Aneesh Kumar K.V080dbb62017-07-06 15:38:44 -0700468 spin_unlock(ptl);
469 if (page)
470 return page;
471 }
472 if (unlikely(pud_bad(*pud)))
473 return no_page_table(vma, flags);
474
Keith Buschdf06b372018-10-26 15:10:28 -0700475 return follow_pmd_mask(vma, address, pud, flags, ctx);
Aneesh Kumar K.V080dbb62017-07-06 15:38:44 -0700476}
477
Aneesh Kumar K.V080dbb62017-07-06 15:38:44 -0700478static struct page *follow_p4d_mask(struct vm_area_struct *vma,
479 unsigned long address, pgd_t *pgdp,
Keith Buschdf06b372018-10-26 15:10:28 -0700480 unsigned int flags,
481 struct follow_page_context *ctx)
Aneesh Kumar K.V080dbb62017-07-06 15:38:44 -0700482{
483 p4d_t *p4d;
Aneesh Kumar K.V4dc71452017-07-06 15:38:56 -0700484 struct page *page;
Aneesh Kumar K.V080dbb62017-07-06 15:38:44 -0700485
486 p4d = p4d_offset(pgdp, address);
487 if (p4d_none(*p4d))
488 return no_page_table(vma, flags);
489 BUILD_BUG_ON(p4d_huge(*p4d));
490 if (unlikely(p4d_bad(*p4d)))
491 return no_page_table(vma, flags);
492
Aneesh Kumar K.V4dc71452017-07-06 15:38:56 -0700493 if (is_hugepd(__hugepd(p4d_val(*p4d)))) {
494 page = follow_huge_pd(vma, address,
495 __hugepd(p4d_val(*p4d)), flags,
496 P4D_SHIFT);
497 if (page)
498 return page;
499 return no_page_table(vma, flags);
500 }
Keith Buschdf06b372018-10-26 15:10:28 -0700501 return follow_pud_mask(vma, address, p4d, flags, ctx);
Aneesh Kumar K.V080dbb62017-07-06 15:38:44 -0700502}
503
504/**
505 * follow_page_mask - look up a page descriptor from a user-virtual address
506 * @vma: vm_area_struct mapping @address
507 * @address: virtual address to look up
508 * @flags: flags modifying lookup behaviour
Mike Rapoport78179552018-11-16 15:08:29 -0800509 * @ctx: contains dev_pagemap for %ZONE_DEVICE memory pinning and a
510 * pointer to output page_mask
Aneesh Kumar K.V080dbb62017-07-06 15:38:44 -0700511 *
512 * @flags can have FOLL_ flags set, defined in <linux/mm.h>
513 *
Mike Rapoport78179552018-11-16 15:08:29 -0800514 * When getting pages from ZONE_DEVICE memory, the @ctx->pgmap caches
515 * the device's dev_pagemap metadata to avoid repeating expensive lookups.
516 *
517 * On output, the @ctx->page_mask is set according to the size of the page.
518 *
519 * Return: the mapped (struct page *), %NULL if no mapping exists, or
Aneesh Kumar K.V080dbb62017-07-06 15:38:44 -0700520 * an error pointer if there is a mapping to something not represented
521 * by a page descriptor (see also vm_normal_page()).
522 */
Bharath Vedarthama7030ae2019-07-11 20:54:34 -0700523static struct page *follow_page_mask(struct vm_area_struct *vma,
Aneesh Kumar K.V080dbb62017-07-06 15:38:44 -0700524 unsigned long address, unsigned int flags,
Keith Buschdf06b372018-10-26 15:10:28 -0700525 struct follow_page_context *ctx)
Aneesh Kumar K.V080dbb62017-07-06 15:38:44 -0700526{
527 pgd_t *pgd;
528 struct page *page;
529 struct mm_struct *mm = vma->vm_mm;
530
Keith Buschdf06b372018-10-26 15:10:28 -0700531 ctx->page_mask = 0;
Aneesh Kumar K.V080dbb62017-07-06 15:38:44 -0700532
533 /* make this handle hugepd */
534 page = follow_huge_addr(mm, address, flags & FOLL_WRITE);
535 if (!IS_ERR(page)) {
536 BUG_ON(flags & FOLL_GET);
537 return page;
538 }
539
540 pgd = pgd_offset(mm, address);
541
542 if (pgd_none(*pgd) || unlikely(pgd_bad(*pgd)))
543 return no_page_table(vma, flags);
544
Anshuman Khandualfaaa5b62017-07-06 15:38:50 -0700545 if (pgd_huge(*pgd)) {
546 page = follow_huge_pgd(mm, address, pgd, flags);
547 if (page)
548 return page;
549 return no_page_table(vma, flags);
550 }
Aneesh Kumar K.V4dc71452017-07-06 15:38:56 -0700551 if (is_hugepd(__hugepd(pgd_val(*pgd)))) {
552 page = follow_huge_pd(vma, address,
553 __hugepd(pgd_val(*pgd)), flags,
554 PGDIR_SHIFT);
555 if (page)
556 return page;
557 return no_page_table(vma, flags);
558 }
Anshuman Khandualfaaa5b62017-07-06 15:38:50 -0700559
Keith Buschdf06b372018-10-26 15:10:28 -0700560 return follow_p4d_mask(vma, address, pgd, flags, ctx);
561}
562
563struct page *follow_page(struct vm_area_struct *vma, unsigned long address,
564 unsigned int foll_flags)
565{
566 struct follow_page_context ctx = { NULL };
567 struct page *page;
568
569 page = follow_page_mask(vma, address, foll_flags, &ctx);
570 if (ctx.pgmap)
571 put_dev_pagemap(ctx.pgmap);
572 return page;
Aneesh Kumar K.V080dbb62017-07-06 15:38:44 -0700573}
574
Kirill A. Shutemovf2b495c2014-06-04 16:08:11 -0700575static int get_gate_page(struct mm_struct *mm, unsigned long address,
576 unsigned int gup_flags, struct vm_area_struct **vma,
577 struct page **page)
578{
579 pgd_t *pgd;
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +0300580 p4d_t *p4d;
Kirill A. Shutemovf2b495c2014-06-04 16:08:11 -0700581 pud_t *pud;
582 pmd_t *pmd;
583 pte_t *pte;
584 int ret = -EFAULT;
585
586 /* user gate pages are read-only */
587 if (gup_flags & FOLL_WRITE)
588 return -EFAULT;
589 if (address > TASK_SIZE)
590 pgd = pgd_offset_k(address);
591 else
592 pgd = pgd_offset_gate(mm, address);
Andy Lutomirskib5d1c392019-07-11 20:57:43 -0700593 if (pgd_none(*pgd))
594 return -EFAULT;
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +0300595 p4d = p4d_offset(pgd, address);
Andy Lutomirskib5d1c392019-07-11 20:57:43 -0700596 if (p4d_none(*p4d))
597 return -EFAULT;
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +0300598 pud = pud_offset(p4d, address);
Andy Lutomirskib5d1c392019-07-11 20:57:43 -0700599 if (pud_none(*pud))
600 return -EFAULT;
Kirill A. Shutemovf2b495c2014-06-04 16:08:11 -0700601 pmd = pmd_offset(pud, address);
Zi Yan84c3fc42017-09-08 16:11:01 -0700602 if (!pmd_present(*pmd))
Kirill A. Shutemovf2b495c2014-06-04 16:08:11 -0700603 return -EFAULT;
604 VM_BUG_ON(pmd_trans_huge(*pmd));
605 pte = pte_offset_map(pmd, address);
606 if (pte_none(*pte))
607 goto unmap;
608 *vma = get_gate_vma(mm);
609 if (!page)
610 goto out;
611 *page = vm_normal_page(*vma, address, *pte);
612 if (!*page) {
613 if ((gup_flags & FOLL_DUMP) || !is_zero_pfn(pte_pfn(*pte)))
614 goto unmap;
615 *page = pte_page(*pte);
616 }
Linus Torvalds8fde12c2019-04-11 10:49:19 -0700617 if (unlikely(!try_get_page(*page))) {
618 ret = -ENOMEM;
619 goto unmap;
620 }
Kirill A. Shutemovf2b495c2014-06-04 16:08:11 -0700621out:
622 ret = 0;
623unmap:
624 pte_unmap(pte);
625 return ret;
626}
627
Paul Cassella9a95f3c2014-08-06 16:07:24 -0700628/*
629 * mmap_sem must be held on entry. If @nonblocking != NULL and
630 * *@flags does not include FOLL_NOWAIT, the mmap_sem may be released.
631 * If it is, *@nonblocking will be set to 0 and -EBUSY returned.
632 */
Kirill A. Shutemov16744482014-06-04 16:08:12 -0700633static int faultin_page(struct task_struct *tsk, struct vm_area_struct *vma,
634 unsigned long address, unsigned int *flags, int *nonblocking)
635{
Kirill A. Shutemov16744482014-06-04 16:08:12 -0700636 unsigned int fault_flags = 0;
Souptick Joarder2b740302018-08-23 17:01:36 -0700637 vm_fault_t ret;
Kirill A. Shutemov16744482014-06-04 16:08:12 -0700638
Eric B Munsonde60f5f2015-11-05 18:51:36 -0800639 /* mlock all present pages, but do not fault in new pages */
640 if ((*flags & (FOLL_POPULATE | FOLL_MLOCK)) == FOLL_MLOCK)
641 return -ENOENT;
Kirill A. Shutemov16744482014-06-04 16:08:12 -0700642 if (*flags & FOLL_WRITE)
643 fault_flags |= FAULT_FLAG_WRITE;
Dave Hansen1b2ee122016-02-12 13:02:21 -0800644 if (*flags & FOLL_REMOTE)
645 fault_flags |= FAULT_FLAG_REMOTE;
Kirill A. Shutemov16744482014-06-04 16:08:12 -0700646 if (nonblocking)
647 fault_flags |= FAULT_FLAG_ALLOW_RETRY;
648 if (*flags & FOLL_NOWAIT)
649 fault_flags |= FAULT_FLAG_ALLOW_RETRY | FAULT_FLAG_RETRY_NOWAIT;
Andres Lagar-Cavilla234b2392014-09-17 10:51:48 -0700650 if (*flags & FOLL_TRIED) {
651 VM_WARN_ON_ONCE(fault_flags & FAULT_FLAG_ALLOW_RETRY);
652 fault_flags |= FAULT_FLAG_TRIED;
653 }
Kirill A. Shutemov16744482014-06-04 16:08:12 -0700654
Kirill A. Shutemovdcddffd2016-07-26 15:25:18 -0700655 ret = handle_mm_fault(vma, address, fault_flags);
Kirill A. Shutemov16744482014-06-04 16:08:12 -0700656 if (ret & VM_FAULT_ERROR) {
James Morse9a291a72017-06-02 14:46:46 -0700657 int err = vm_fault_to_errno(ret, *flags);
658
659 if (err)
660 return err;
Kirill A. Shutemov16744482014-06-04 16:08:12 -0700661 BUG();
662 }
663
664 if (tsk) {
665 if (ret & VM_FAULT_MAJOR)
666 tsk->maj_flt++;
667 else
668 tsk->min_flt++;
669 }
670
671 if (ret & VM_FAULT_RETRY) {
Andrea Arcangeli96312e62018-03-09 15:51:06 -0800672 if (nonblocking && !(fault_flags & FAULT_FLAG_RETRY_NOWAIT))
Kirill A. Shutemov16744482014-06-04 16:08:12 -0700673 *nonblocking = 0;
674 return -EBUSY;
675 }
676
677 /*
678 * The VM_FAULT_WRITE bit tells us that do_wp_page has broken COW when
679 * necessary, even if maybe_mkwrite decided not to set pte_write. We
680 * can thus safely do subsequent page lookups as if they were reads.
681 * But only do so when looping for pte_write is futile: in some cases
682 * userspace may also be wanting to write to the gotten user page,
683 * which a read fault here might prevent (a readonly page might get
684 * reCOWed by userspace write).
685 */
686 if ((ret & VM_FAULT_WRITE) && !(vma->vm_flags & VM_WRITE))
Mario Leinweber29231172018-04-05 16:24:18 -0700687 *flags |= FOLL_COW;
Kirill A. Shutemov16744482014-06-04 16:08:12 -0700688 return 0;
689}
690
Kirill A. Shutemovfa5bb202014-06-04 16:08:13 -0700691static int check_vma_flags(struct vm_area_struct *vma, unsigned long gup_flags)
692{
693 vm_flags_t vm_flags = vma->vm_flags;
Dave Hansen1b2ee122016-02-12 13:02:21 -0800694 int write = (gup_flags & FOLL_WRITE);
695 int foreign = (gup_flags & FOLL_REMOTE);
Kirill A. Shutemovfa5bb202014-06-04 16:08:13 -0700696
697 if (vm_flags & (VM_IO | VM_PFNMAP))
698 return -EFAULT;
699
Willy Tarreau7f7ccc22018-05-11 08:11:44 +0200700 if (gup_flags & FOLL_ANON && !vma_is_anonymous(vma))
701 return -EFAULT;
702
Dave Hansen1b2ee122016-02-12 13:02:21 -0800703 if (write) {
Kirill A. Shutemovfa5bb202014-06-04 16:08:13 -0700704 if (!(vm_flags & VM_WRITE)) {
705 if (!(gup_flags & FOLL_FORCE))
706 return -EFAULT;
707 /*
708 * We used to let the write,force case do COW in a
709 * VM_MAYWRITE VM_SHARED !VM_WRITE vma, so ptrace could
710 * set a breakpoint in a read-only mapping of an
711 * executable, without corrupting the file (yet only
712 * when that file had been opened for writing!).
713 * Anon pages in shared mappings are surprising: now
714 * just reject it.
715 */
Hugh Dickins46435362016-01-30 18:03:16 -0800716 if (!is_cow_mapping(vm_flags))
Kirill A. Shutemovfa5bb202014-06-04 16:08:13 -0700717 return -EFAULT;
Kirill A. Shutemovfa5bb202014-06-04 16:08:13 -0700718 }
719 } else if (!(vm_flags & VM_READ)) {
720 if (!(gup_flags & FOLL_FORCE))
721 return -EFAULT;
722 /*
723 * Is there actually any vma we can reach here which does not
724 * have VM_MAYREAD set?
725 */
726 if (!(vm_flags & VM_MAYREAD))
727 return -EFAULT;
728 }
Dave Hansend61172b2016-02-12 13:02:24 -0800729 /*
730 * gups are always data accesses, not instruction
731 * fetches, so execute=false here
732 */
733 if (!arch_vma_access_permitted(vma, write, false, foreign))
Dave Hansen33a709b2016-02-12 13:02:19 -0800734 return -EFAULT;
Kirill A. Shutemovfa5bb202014-06-04 16:08:13 -0700735 return 0;
736}
737
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700738/**
739 * __get_user_pages() - pin user pages in memory
740 * @tsk: task_struct of target task
741 * @mm: mm_struct of target mm
742 * @start: starting user address
743 * @nr_pages: number of pages from start to pin
744 * @gup_flags: flags modifying pin behaviour
745 * @pages: array that receives pointers to the pages pinned.
746 * Should be at least nr_pages long. Or NULL, if caller
747 * only intends to ensure the pages are faulted in.
748 * @vmas: array of pointers to vmas corresponding to each page.
749 * Or NULL if the caller does not require them.
750 * @nonblocking: whether waiting for disk IO or mmap_sem contention
751 *
Liu Xiangd2dfbe42019-11-30 17:49:53 -0800752 * Returns either number of pages pinned (which may be less than the
753 * number requested), or an error. Details about the return value:
754 *
755 * -- If nr_pages is 0, returns 0.
756 * -- If nr_pages is >0, but no pages were pinned, returns -errno.
757 * -- If nr_pages is >0, and some pages were pinned, returns the number of
758 * pages pinned. Again, this may be less than nr_pages.
759 *
760 * The caller is responsible for releasing returned @pages, via put_page().
761 *
762 * @vmas are valid only as long as mmap_sem is held.
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700763 *
Paul Cassella9a95f3c2014-08-06 16:07:24 -0700764 * Must be called with mmap_sem held. It may be released. See below.
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700765 *
766 * __get_user_pages walks a process's page tables and takes a reference to
767 * each struct page that each user address corresponds to at a given
768 * instant. That is, it takes the page that would be accessed if a user
769 * thread accesses the given user virtual address at that instant.
770 *
771 * This does not guarantee that the page exists in the user mappings when
772 * __get_user_pages returns, and there may even be a completely different
773 * page there in some cases (eg. if mmapped pagecache has been invalidated
774 * and subsequently re faulted). However it does guarantee that the page
775 * won't be freed completely. And mostly callers simply care that the page
776 * contains data that was valid *at some point in time*. Typically, an IO
777 * or similar operation cannot guarantee anything stronger anyway because
778 * locks can't be held over the syscall boundary.
779 *
780 * If @gup_flags & FOLL_WRITE == 0, the page must not be written to. If
781 * the page is written to, set_page_dirty (or set_page_dirty_lock, as
782 * appropriate) must be called after the page is finished with, and
783 * before put_page is called.
784 *
785 * If @nonblocking != NULL, __get_user_pages will not wait for disk IO
786 * or mmap_sem contention, and if waiting is needed to pin all pages,
Paul Cassella9a95f3c2014-08-06 16:07:24 -0700787 * *@nonblocking will be set to 0. Further, if @gup_flags does not
788 * include FOLL_NOWAIT, the mmap_sem will be released via up_read() in
789 * this case.
790 *
791 * A caller using such a combination of @nonblocking and @gup_flags
792 * must therefore hold the mmap_sem for reading only, and recognize
793 * when it's been released. Otherwise, it must be held for either
794 * reading or writing and will not be released.
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700795 *
796 * In most cases, get_user_pages or get_user_pages_fast should be used
797 * instead of __get_user_pages. __get_user_pages should be used only if
798 * you need some special @gup_flags.
799 */
Lorenzo Stoakes0d731752016-10-24 10:57:25 +0100800static long __get_user_pages(struct task_struct *tsk, struct mm_struct *mm,
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700801 unsigned long start, unsigned long nr_pages,
802 unsigned int gup_flags, struct page **pages,
803 struct vm_area_struct **vmas, int *nonblocking)
804{
Keith Buschdf06b372018-10-26 15:10:28 -0700805 long ret = 0, i = 0;
Kirill A. Shutemovfa5bb202014-06-04 16:08:13 -0700806 struct vm_area_struct *vma = NULL;
Keith Buschdf06b372018-10-26 15:10:28 -0700807 struct follow_page_context ctx = { NULL };
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700808
809 if (!nr_pages)
810 return 0;
811
Andrey Konovalovf9652592019-09-25 16:48:34 -0700812 start = untagged_addr(start);
813
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700814 VM_BUG_ON(!!pages != !!(gup_flags & FOLL_GET));
815
816 /*
817 * If FOLL_FORCE is set then do not force a full fault as the hinting
818 * fault information is unrelated to the reference behaviour of a task
819 * using the address space
820 */
821 if (!(gup_flags & FOLL_FORCE))
822 gup_flags |= FOLL_NUMA;
823
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700824 do {
Kirill A. Shutemovfa5bb202014-06-04 16:08:13 -0700825 struct page *page;
826 unsigned int foll_flags = gup_flags;
827 unsigned int page_increm;
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700828
Kirill A. Shutemovfa5bb202014-06-04 16:08:13 -0700829 /* first iteration or cross vma bound */
830 if (!vma || start >= vma->vm_end) {
831 vma = find_extend_vma(mm, start);
832 if (!vma && in_gate_area(mm, start)) {
Kirill A. Shutemovfa5bb202014-06-04 16:08:13 -0700833 ret = get_gate_page(mm, start & PAGE_MASK,
834 gup_flags, &vma,
835 pages ? &pages[i] : NULL);
836 if (ret)
John Hubbard08be37b2018-11-30 14:08:53 -0800837 goto out;
Keith Buschdf06b372018-10-26 15:10:28 -0700838 ctx.page_mask = 0;
Kirill A. Shutemovfa5bb202014-06-04 16:08:13 -0700839 goto next_page;
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700840 }
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700841
Keith Buschdf06b372018-10-26 15:10:28 -0700842 if (!vma || check_vma_flags(vma, gup_flags)) {
843 ret = -EFAULT;
844 goto out;
845 }
Kirill A. Shutemovfa5bb202014-06-04 16:08:13 -0700846 if (is_vm_hugetlb_page(vma)) {
847 i = follow_hugetlb_page(mm, vma, pages, vmas,
848 &start, &nr_pages, i,
Andrea Arcangeli87ffc112017-02-22 15:43:13 -0800849 gup_flags, nonblocking);
Kirill A. Shutemovfa5bb202014-06-04 16:08:13 -0700850 continue;
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700851 }
Kirill A. Shutemovfa5bb202014-06-04 16:08:13 -0700852 }
853retry:
854 /*
855 * If we have a pending SIGKILL, don't keep faulting pages and
856 * potentially allocating memory.
857 */
Davidlohr Buesofa45f112019-01-03 15:28:55 -0800858 if (fatal_signal_pending(current)) {
Keith Buschdf06b372018-10-26 15:10:28 -0700859 ret = -ERESTARTSYS;
860 goto out;
861 }
Kirill A. Shutemovfa5bb202014-06-04 16:08:13 -0700862 cond_resched();
Keith Buschdf06b372018-10-26 15:10:28 -0700863
864 page = follow_page_mask(vma, start, foll_flags, &ctx);
Kirill A. Shutemovfa5bb202014-06-04 16:08:13 -0700865 if (!page) {
Kirill A. Shutemovfa5bb202014-06-04 16:08:13 -0700866 ret = faultin_page(tsk, vma, start, &foll_flags,
867 nonblocking);
868 switch (ret) {
869 case 0:
870 goto retry;
Keith Buschdf06b372018-10-26 15:10:28 -0700871 case -EBUSY:
872 ret = 0;
873 /* FALLTHRU */
Kirill A. Shutemovfa5bb202014-06-04 16:08:13 -0700874 case -EFAULT:
875 case -ENOMEM:
876 case -EHWPOISON:
Keith Buschdf06b372018-10-26 15:10:28 -0700877 goto out;
Kirill A. Shutemovfa5bb202014-06-04 16:08:13 -0700878 case -ENOENT:
879 goto next_page;
880 }
881 BUG();
Kirill A. Shutemov1027e442015-09-04 15:47:55 -0700882 } else if (PTR_ERR(page) == -EEXIST) {
883 /*
884 * Proper page table entry exists, but no corresponding
885 * struct page.
886 */
887 goto next_page;
888 } else if (IS_ERR(page)) {
Keith Buschdf06b372018-10-26 15:10:28 -0700889 ret = PTR_ERR(page);
890 goto out;
Kirill A. Shutemov1027e442015-09-04 15:47:55 -0700891 }
Kirill A. Shutemovfa5bb202014-06-04 16:08:13 -0700892 if (pages) {
893 pages[i] = page;
894 flush_anon_page(vma, page, start);
895 flush_dcache_page(page);
Keith Buschdf06b372018-10-26 15:10:28 -0700896 ctx.page_mask = 0;
Kirill A. Shutemovfa5bb202014-06-04 16:08:13 -0700897 }
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700898next_page:
Kirill A. Shutemovfa5bb202014-06-04 16:08:13 -0700899 if (vmas) {
900 vmas[i] = vma;
Keith Buschdf06b372018-10-26 15:10:28 -0700901 ctx.page_mask = 0;
Kirill A. Shutemovfa5bb202014-06-04 16:08:13 -0700902 }
Keith Buschdf06b372018-10-26 15:10:28 -0700903 page_increm = 1 + (~(start >> PAGE_SHIFT) & ctx.page_mask);
Kirill A. Shutemovfa5bb202014-06-04 16:08:13 -0700904 if (page_increm > nr_pages)
905 page_increm = nr_pages;
906 i += page_increm;
907 start += page_increm * PAGE_SIZE;
908 nr_pages -= page_increm;
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700909 } while (nr_pages);
Keith Buschdf06b372018-10-26 15:10:28 -0700910out:
911 if (ctx.pgmap)
912 put_dev_pagemap(ctx.pgmap);
913 return i ? i : ret;
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700914}
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700915
Tobias Klauser771ab432016-12-12 16:41:53 -0800916static bool vma_permits_fault(struct vm_area_struct *vma,
917 unsigned int fault_flags)
Dave Hansend4925e02016-02-12 13:02:16 -0800918{
Dave Hansen1b2ee122016-02-12 13:02:21 -0800919 bool write = !!(fault_flags & FAULT_FLAG_WRITE);
920 bool foreign = !!(fault_flags & FAULT_FLAG_REMOTE);
Dave Hansen33a709b2016-02-12 13:02:19 -0800921 vm_flags_t vm_flags = write ? VM_WRITE : VM_READ;
Dave Hansend4925e02016-02-12 13:02:16 -0800922
923 if (!(vm_flags & vma->vm_flags))
924 return false;
925
Dave Hansen33a709b2016-02-12 13:02:19 -0800926 /*
927 * The architecture might have a hardware protection
Dave Hansen1b2ee122016-02-12 13:02:21 -0800928 * mechanism other than read/write that can deny access.
Dave Hansend61172b2016-02-12 13:02:24 -0800929 *
930 * gup always represents data access, not instruction
931 * fetches, so execute=false here:
Dave Hansen33a709b2016-02-12 13:02:19 -0800932 */
Dave Hansend61172b2016-02-12 13:02:24 -0800933 if (!arch_vma_access_permitted(vma, write, false, foreign))
Dave Hansen33a709b2016-02-12 13:02:19 -0800934 return false;
935
Dave Hansend4925e02016-02-12 13:02:16 -0800936 return true;
937}
938
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700939/*
940 * fixup_user_fault() - manually resolve a user page fault
941 * @tsk: the task_struct to use for page fault accounting, or
942 * NULL if faults are not to be recorded.
943 * @mm: mm_struct of target mm
944 * @address: user address
945 * @fault_flags:flags to pass down to handle_mm_fault()
Dominik Dingel4a9e1cd2016-01-15 16:57:04 -0800946 * @unlocked: did we unlock the mmap_sem while retrying, maybe NULL if caller
947 * does not allow retry
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700948 *
949 * This is meant to be called in the specific scenario where for locking reasons
950 * we try to access user memory in atomic context (within a pagefault_disable()
951 * section), this returns -EFAULT, and we want to resolve the user fault before
952 * trying again.
953 *
954 * Typically this is meant to be used by the futex code.
955 *
956 * The main difference with get_user_pages() is that this function will
957 * unconditionally call handle_mm_fault() which will in turn perform all the
958 * necessary SW fixup of the dirty and young bits in the PTE, while
Dominik Dingel4a9e1cd2016-01-15 16:57:04 -0800959 * get_user_pages() only guarantees to update these in the struct page.
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700960 *
961 * This is important for some architectures where those bits also gate the
962 * access permission to the page because they are maintained in software. On
963 * such architectures, gup() will not be enough to make a subsequent access
964 * succeed.
965 *
Dominik Dingel4a9e1cd2016-01-15 16:57:04 -0800966 * This function will not return with an unlocked mmap_sem. So it has not the
967 * same semantics wrt the @mm->mmap_sem as does filemap_fault().
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700968 */
969int fixup_user_fault(struct task_struct *tsk, struct mm_struct *mm,
Dominik Dingel4a9e1cd2016-01-15 16:57:04 -0800970 unsigned long address, unsigned int fault_flags,
971 bool *unlocked)
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700972{
973 struct vm_area_struct *vma;
Souptick Joarder2b740302018-08-23 17:01:36 -0700974 vm_fault_t ret, major = 0;
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700975
Andrey Konovalovf9652592019-09-25 16:48:34 -0700976 address = untagged_addr(address);
977
Dominik Dingel4a9e1cd2016-01-15 16:57:04 -0800978 if (unlocked)
979 fault_flags |= FAULT_FLAG_ALLOW_RETRY;
980
981retry:
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700982 vma = find_extend_vma(mm, address);
983 if (!vma || address < vma->vm_start)
984 return -EFAULT;
985
Dave Hansend4925e02016-02-12 13:02:16 -0800986 if (!vma_permits_fault(vma, fault_flags))
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700987 return -EFAULT;
988
Kirill A. Shutemovdcddffd2016-07-26 15:25:18 -0700989 ret = handle_mm_fault(vma, address, fault_flags);
Dominik Dingel4a9e1cd2016-01-15 16:57:04 -0800990 major |= ret & VM_FAULT_MAJOR;
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700991 if (ret & VM_FAULT_ERROR) {
James Morse9a291a72017-06-02 14:46:46 -0700992 int err = vm_fault_to_errno(ret, 0);
993
994 if (err)
995 return err;
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700996 BUG();
997 }
Dominik Dingel4a9e1cd2016-01-15 16:57:04 -0800998
999 if (ret & VM_FAULT_RETRY) {
1000 down_read(&mm->mmap_sem);
1001 if (!(fault_flags & FAULT_FLAG_TRIED)) {
1002 *unlocked = true;
1003 fault_flags &= ~FAULT_FLAG_ALLOW_RETRY;
1004 fault_flags |= FAULT_FLAG_TRIED;
1005 goto retry;
1006 }
1007 }
1008
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -07001009 if (tsk) {
Dominik Dingel4a9e1cd2016-01-15 16:57:04 -08001010 if (major)
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -07001011 tsk->maj_flt++;
1012 else
1013 tsk->min_flt++;
1014 }
1015 return 0;
1016}
Paolo Bonziniadd6a0c2016-06-07 17:51:18 +02001017EXPORT_SYMBOL_GPL(fixup_user_fault);
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -07001018
Andrea Arcangelif0818f42015-02-11 15:27:17 -08001019static __always_inline long __get_user_pages_locked(struct task_struct *tsk,
1020 struct mm_struct *mm,
1021 unsigned long start,
1022 unsigned long nr_pages,
Andrea Arcangelif0818f42015-02-11 15:27:17 -08001023 struct page **pages,
1024 struct vm_area_struct **vmas,
Al Viroe7167122017-11-19 11:32:05 -05001025 int *locked,
Andrea Arcangeli0fd71a52015-02-11 15:27:20 -08001026 unsigned int flags)
Andrea Arcangelif0818f42015-02-11 15:27:17 -08001027{
Andrea Arcangelif0818f42015-02-11 15:27:17 -08001028 long ret, pages_done;
1029 bool lock_dropped;
1030
1031 if (locked) {
1032 /* if VM_FAULT_RETRY can be returned, vmas become invalid */
1033 BUG_ON(vmas);
1034 /* check caller initialized locked */
1035 BUG_ON(*locked != 1);
1036 }
1037
1038 if (pages)
1039 flags |= FOLL_GET;
Andrea Arcangelif0818f42015-02-11 15:27:17 -08001040
1041 pages_done = 0;
1042 lock_dropped = false;
1043 for (;;) {
1044 ret = __get_user_pages(tsk, mm, start, nr_pages, flags, pages,
1045 vmas, locked);
1046 if (!locked)
1047 /* VM_FAULT_RETRY couldn't trigger, bypass */
1048 return ret;
1049
1050 /* VM_FAULT_RETRY cannot return errors */
1051 if (!*locked) {
1052 BUG_ON(ret < 0);
1053 BUG_ON(ret >= nr_pages);
1054 }
1055
Andrea Arcangelif0818f42015-02-11 15:27:17 -08001056 if (ret > 0) {
1057 nr_pages -= ret;
1058 pages_done += ret;
1059 if (!nr_pages)
1060 break;
1061 }
1062 if (*locked) {
Andrea Arcangeli96312e62018-03-09 15:51:06 -08001063 /*
1064 * VM_FAULT_RETRY didn't trigger or it was a
1065 * FOLL_NOWAIT.
1066 */
Andrea Arcangelif0818f42015-02-11 15:27:17 -08001067 if (!pages_done)
1068 pages_done = ret;
1069 break;
1070 }
Mike Rapoportdf172772019-05-31 22:30:33 -07001071 /*
1072 * VM_FAULT_RETRY triggered, so seek to the faulting offset.
1073 * For the prefault case (!pages) we only update counts.
1074 */
1075 if (likely(pages))
1076 pages += ret;
Andrea Arcangelif0818f42015-02-11 15:27:17 -08001077 start += ret << PAGE_SHIFT;
1078
1079 /*
1080 * Repeat on the address that fired VM_FAULT_RETRY
1081 * without FAULT_FLAG_ALLOW_RETRY but with
1082 * FAULT_FLAG_TRIED.
1083 */
1084 *locked = 1;
1085 lock_dropped = true;
1086 down_read(&mm->mmap_sem);
1087 ret = __get_user_pages(tsk, mm, start, 1, flags | FOLL_TRIED,
1088 pages, NULL, NULL);
1089 if (ret != 1) {
1090 BUG_ON(ret > 1);
1091 if (!pages_done)
1092 pages_done = ret;
1093 break;
1094 }
1095 nr_pages--;
1096 pages_done++;
1097 if (!nr_pages)
1098 break;
Mike Rapoportdf172772019-05-31 22:30:33 -07001099 if (likely(pages))
1100 pages++;
Andrea Arcangelif0818f42015-02-11 15:27:17 -08001101 start += PAGE_SIZE;
1102 }
Al Viroe7167122017-11-19 11:32:05 -05001103 if (lock_dropped && *locked) {
Andrea Arcangelif0818f42015-02-11 15:27:17 -08001104 /*
1105 * We must let the caller know we temporarily dropped the lock
1106 * and so the critical section protected by it was lost.
1107 */
1108 up_read(&mm->mmap_sem);
1109 *locked = 0;
1110 }
1111 return pages_done;
1112}
1113
1114/*
Dave Hansen1e987792016-02-12 13:01:54 -08001115 * get_user_pages_remote() - pin user pages in memory
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -07001116 * @tsk: the task_struct to use for page fault accounting, or
1117 * NULL if faults are not to be recorded.
1118 * @mm: mm_struct of target mm
1119 * @start: starting user address
1120 * @nr_pages: number of pages from start to pin
Lorenzo Stoakes9beae1e2016-10-13 01:20:17 +01001121 * @gup_flags: flags modifying lookup behaviour
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -07001122 * @pages: array that receives pointers to the pages pinned.
1123 * Should be at least nr_pages long. Or NULL, if caller
1124 * only intends to ensure the pages are faulted in.
1125 * @vmas: array of pointers to vmas corresponding to each page.
1126 * Or NULL if the caller does not require them.
Lorenzo Stoakes5b56d492016-12-14 15:06:52 -08001127 * @locked: pointer to lock flag indicating whether lock is held and
1128 * subsequently whether VM_FAULT_RETRY functionality can be
1129 * utilised. Lock must initially be held.
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -07001130 *
Liu Xiangd2dfbe42019-11-30 17:49:53 -08001131 * Returns either number of pages pinned (which may be less than the
1132 * number requested), or an error. Details about the return value:
1133 *
1134 * -- If nr_pages is 0, returns 0.
1135 * -- If nr_pages is >0, but no pages were pinned, returns -errno.
1136 * -- If nr_pages is >0, and some pages were pinned, returns the number of
1137 * pages pinned. Again, this may be less than nr_pages.
1138 *
1139 * The caller is responsible for releasing returned @pages, via put_page().
1140 *
1141 * @vmas are valid only as long as mmap_sem is held.
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -07001142 *
1143 * Must be called with mmap_sem held for read or write.
1144 *
1145 * get_user_pages walks a process's page tables and takes a reference to
1146 * each struct page that each user address corresponds to at a given
1147 * instant. That is, it takes the page that would be accessed if a user
1148 * thread accesses the given user virtual address at that instant.
1149 *
1150 * This does not guarantee that the page exists in the user mappings when
1151 * get_user_pages returns, and there may even be a completely different
1152 * page there in some cases (eg. if mmapped pagecache has been invalidated
1153 * and subsequently re faulted). However it does guarantee that the page
1154 * won't be freed completely. And mostly callers simply care that the page
1155 * contains data that was valid *at some point in time*. Typically, an IO
1156 * or similar operation cannot guarantee anything stronger anyway because
1157 * locks can't be held over the syscall boundary.
1158 *
Lorenzo Stoakes9beae1e2016-10-13 01:20:17 +01001159 * If gup_flags & FOLL_WRITE == 0, the page must not be written to. If the page
1160 * is written to, set_page_dirty (or set_page_dirty_lock, as appropriate) must
1161 * be called after the page is finished with, and before put_page is called.
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -07001162 *
1163 * get_user_pages is typically used for fewer-copy IO operations, to get a
1164 * handle on the memory by some means other than accesses via the user virtual
1165 * addresses. The pages may be submitted for DMA to devices or accessed via
1166 * their kernel linear mapping (via the kmap APIs). Care should be taken to
1167 * use the correct cache flushing APIs.
1168 *
1169 * See also get_user_pages_fast, for performance critical applications.
Andrea Arcangelif0818f42015-02-11 15:27:17 -08001170 *
1171 * get_user_pages should be phased out in favor of
1172 * get_user_pages_locked|unlocked or get_user_pages_fast. Nothing
1173 * should use get_user_pages because it cannot pass
1174 * FAULT_FLAG_ALLOW_RETRY to handle_mm_fault.
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -07001175 */
Dave Hansen1e987792016-02-12 13:01:54 -08001176long get_user_pages_remote(struct task_struct *tsk, struct mm_struct *mm,
1177 unsigned long start, unsigned long nr_pages,
Lorenzo Stoakes9beae1e2016-10-13 01:20:17 +01001178 unsigned int gup_flags, struct page **pages,
Lorenzo Stoakes5b56d492016-12-14 15:06:52 -08001179 struct vm_area_struct **vmas, int *locked)
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -07001180{
Ira Weiny932f4a62019-05-13 17:17:03 -07001181 /*
1182 * FIXME: Current FOLL_LONGTERM behavior is incompatible with
1183 * FAULT_FLAG_ALLOW_RETRY because of the FS DAX check requirement on
1184 * vmas. As there are no users of this flag in this call we simply
1185 * disallow this option for now.
1186 */
1187 if (WARN_ON_ONCE(gup_flags & FOLL_LONGTERM))
1188 return -EINVAL;
1189
Lorenzo Stoakes859110d2016-10-13 01:20:11 +01001190 return __get_user_pages_locked(tsk, mm, start, nr_pages, pages, vmas,
Al Viroe7167122017-11-19 11:32:05 -05001191 locked,
Lorenzo Stoakes9beae1e2016-10-13 01:20:17 +01001192 gup_flags | FOLL_TOUCH | FOLL_REMOTE);
Dave Hansen1e987792016-02-12 13:01:54 -08001193}
1194EXPORT_SYMBOL(get_user_pages_remote);
1195
Christoph Hellwigd3649f62019-07-11 20:57:18 -07001196/**
1197 * populate_vma_page_range() - populate a range of pages in the vma.
1198 * @vma: target vma
1199 * @start: start address
1200 * @end: end address
1201 * @nonblocking:
1202 *
1203 * This takes care of mlocking the pages too if VM_LOCKED is set.
1204 *
1205 * return 0 on success, negative error code on error.
1206 *
1207 * vma->vm_mm->mmap_sem must be held.
1208 *
1209 * If @nonblocking is NULL, it may be held for read or write and will
1210 * be unperturbed.
1211 *
1212 * If @nonblocking is non-NULL, it must held for read only and may be
1213 * released. If it's released, *@nonblocking will be set to 0.
1214 */
1215long populate_vma_page_range(struct vm_area_struct *vma,
1216 unsigned long start, unsigned long end, int *nonblocking)
1217{
1218 struct mm_struct *mm = vma->vm_mm;
1219 unsigned long nr_pages = (end - start) / PAGE_SIZE;
1220 int gup_flags;
1221
1222 VM_BUG_ON(start & ~PAGE_MASK);
1223 VM_BUG_ON(end & ~PAGE_MASK);
1224 VM_BUG_ON_VMA(start < vma->vm_start, vma);
1225 VM_BUG_ON_VMA(end > vma->vm_end, vma);
1226 VM_BUG_ON_MM(!rwsem_is_locked(&mm->mmap_sem), mm);
1227
1228 gup_flags = FOLL_TOUCH | FOLL_POPULATE | FOLL_MLOCK;
1229 if (vma->vm_flags & VM_LOCKONFAULT)
1230 gup_flags &= ~FOLL_POPULATE;
1231 /*
1232 * We want to touch writable mappings with a write fault in order
1233 * to break COW, except for shared mappings because these don't COW
1234 * and we would not want to dirty them for nothing.
1235 */
1236 if ((vma->vm_flags & (VM_WRITE | VM_SHARED)) == VM_WRITE)
1237 gup_flags |= FOLL_WRITE;
1238
1239 /*
1240 * We want mlock to succeed for regions that have any permissions
1241 * other than PROT_NONE.
1242 */
1243 if (vma->vm_flags & (VM_READ | VM_WRITE | VM_EXEC))
1244 gup_flags |= FOLL_FORCE;
1245
1246 /*
1247 * We made sure addr is within a VMA, so the following will
1248 * not result in a stack expansion that recurses back here.
1249 */
1250 return __get_user_pages(current, mm, start, nr_pages, gup_flags,
1251 NULL, NULL, nonblocking);
1252}
1253
1254/*
1255 * __mm_populate - populate and/or mlock pages within a range of address space.
1256 *
1257 * This is used to implement mlock() and the MAP_POPULATE / MAP_LOCKED mmap
1258 * flags. VMAs must be already marked with the desired vm_flags, and
1259 * mmap_sem must not be held.
1260 */
1261int __mm_populate(unsigned long start, unsigned long len, int ignore_errors)
1262{
1263 struct mm_struct *mm = current->mm;
1264 unsigned long end, nstart, nend;
1265 struct vm_area_struct *vma = NULL;
1266 int locked = 0;
1267 long ret = 0;
1268
1269 end = start + len;
1270
1271 for (nstart = start; nstart < end; nstart = nend) {
1272 /*
1273 * We want to fault in pages for [nstart; end) address range.
1274 * Find first corresponding VMA.
1275 */
1276 if (!locked) {
1277 locked = 1;
1278 down_read(&mm->mmap_sem);
1279 vma = find_vma(mm, nstart);
1280 } else if (nstart >= vma->vm_end)
1281 vma = vma->vm_next;
1282 if (!vma || vma->vm_start >= end)
1283 break;
1284 /*
1285 * Set [nstart; nend) to intersection of desired address
1286 * range with the first VMA. Also, skip undesirable VMA types.
1287 */
1288 nend = min(end, vma->vm_end);
1289 if (vma->vm_flags & (VM_IO | VM_PFNMAP))
1290 continue;
1291 if (nstart < vma->vm_start)
1292 nstart = vma->vm_start;
1293 /*
1294 * Now fault in a range of pages. populate_vma_page_range()
1295 * double checks the vma flags, so that it won't mlock pages
1296 * if the vma was already munlocked.
1297 */
1298 ret = populate_vma_page_range(vma, nstart, nend, &locked);
1299 if (ret < 0) {
1300 if (ignore_errors) {
1301 ret = 0;
1302 continue; /* continue at next VMA */
1303 }
1304 break;
1305 }
1306 nend = nstart + ret * PAGE_SIZE;
1307 ret = 0;
1308 }
1309 if (locked)
1310 up_read(&mm->mmap_sem);
1311 return ret; /* 0 or negative error code */
1312}
1313
1314/**
1315 * get_dump_page() - pin user page in memory while writing it to core dump
1316 * @addr: user address
1317 *
1318 * Returns struct page pointer of user page pinned for dump,
1319 * to be freed afterwards by put_page().
1320 *
1321 * Returns NULL on any kind of failure - a hole must then be inserted into
1322 * the corefile, to preserve alignment with its headers; and also returns
1323 * NULL wherever the ZERO_PAGE, or an anonymous pte_none, has been found -
1324 * allowing a hole to be left in the corefile to save diskspace.
1325 *
1326 * Called without mmap_sem, but after all other threads have been killed.
1327 */
1328#ifdef CONFIG_ELF_CORE
1329struct page *get_dump_page(unsigned long addr)
1330{
1331 struct vm_area_struct *vma;
1332 struct page *page;
1333
1334 if (__get_user_pages(current, current->mm, addr, 1,
1335 FOLL_FORCE | FOLL_DUMP | FOLL_GET, &page, &vma,
1336 NULL) < 1)
1337 return NULL;
1338 flush_cache_page(vma, addr, page_to_pfn(page));
1339 return page;
1340}
1341#endif /* CONFIG_ELF_CORE */
Christoph Hellwig050a9ad2019-07-11 20:57:21 -07001342#else /* CONFIG_MMU */
1343static long __get_user_pages_locked(struct task_struct *tsk,
1344 struct mm_struct *mm, unsigned long start,
1345 unsigned long nr_pages, struct page **pages,
1346 struct vm_area_struct **vmas, int *locked,
1347 unsigned int foll_flags)
1348{
1349 struct vm_area_struct *vma;
1350 unsigned long vm_flags;
1351 int i;
1352
1353 /* calculate required read or write permissions.
1354 * If FOLL_FORCE is set, we only require the "MAY" flags.
1355 */
1356 vm_flags = (foll_flags & FOLL_WRITE) ?
1357 (VM_WRITE | VM_MAYWRITE) : (VM_READ | VM_MAYREAD);
1358 vm_flags &= (foll_flags & FOLL_FORCE) ?
1359 (VM_MAYREAD | VM_MAYWRITE) : (VM_READ | VM_WRITE);
1360
1361 for (i = 0; i < nr_pages; i++) {
1362 vma = find_vma(mm, start);
1363 if (!vma)
1364 goto finish_or_fault;
1365
1366 /* protect what we can, including chardevs */
1367 if ((vma->vm_flags & (VM_IO | VM_PFNMAP)) ||
1368 !(vm_flags & vma->vm_flags))
1369 goto finish_or_fault;
1370
1371 if (pages) {
1372 pages[i] = virt_to_page(start);
1373 if (pages[i])
1374 get_page(pages[i]);
1375 }
1376 if (vmas)
1377 vmas[i] = vma;
1378 start = (start + PAGE_SIZE) & PAGE_MASK;
1379 }
1380
1381 return i;
1382
1383finish_or_fault:
1384 return i ? : -EFAULT;
1385}
1386#endif /* !CONFIG_MMU */
Christoph Hellwigd3649f62019-07-11 20:57:18 -07001387
Aneesh Kumar K.V9a4e9f32019-03-05 15:47:44 -08001388#if defined(CONFIG_FS_DAX) || defined (CONFIG_CMA)
Aneesh Kumar K.V9a4e9f32019-03-05 15:47:44 -08001389static bool check_dax_vmas(struct vm_area_struct **vmas, long nr_pages)
1390{
1391 long i;
1392 struct vm_area_struct *vma_prev = NULL;
1393
1394 for (i = 0; i < nr_pages; i++) {
1395 struct vm_area_struct *vma = vmas[i];
1396
1397 if (vma == vma_prev)
1398 continue;
1399
1400 vma_prev = vma;
1401
1402 if (vma_is_fsdax(vma))
1403 return true;
1404 }
1405 return false;
1406}
Aneesh Kumar K.V9a4e9f32019-03-05 15:47:44 -08001407
1408#ifdef CONFIG_CMA
1409static struct page *new_non_cma_page(struct page *page, unsigned long private)
1410{
1411 /*
1412 * We want to make sure we allocate the new page from the same node
1413 * as the source page.
1414 */
1415 int nid = page_to_nid(page);
1416 /*
1417 * Trying to allocate a page for migration. Ignore allocation
1418 * failure warnings. We don't force __GFP_THISNODE here because
1419 * this node here is the node where we have CMA reservation and
1420 * in some case these nodes will have really less non movable
1421 * allocation memory.
1422 */
1423 gfp_t gfp_mask = GFP_USER | __GFP_NOWARN;
1424
1425 if (PageHighMem(page))
1426 gfp_mask |= __GFP_HIGHMEM;
1427
1428#ifdef CONFIG_HUGETLB_PAGE
1429 if (PageHuge(page)) {
1430 struct hstate *h = page_hstate(page);
1431 /*
1432 * We don't want to dequeue from the pool because pool pages will
1433 * mostly be from the CMA region.
1434 */
1435 return alloc_migrate_huge_page(h, gfp_mask, nid, NULL);
1436 }
1437#endif
1438 if (PageTransHuge(page)) {
1439 struct page *thp;
1440 /*
1441 * ignore allocation failure warnings
1442 */
1443 gfp_t thp_gfpmask = GFP_TRANSHUGE | __GFP_NOWARN;
1444
1445 /*
1446 * Remove the movable mask so that we don't allocate from
1447 * CMA area again.
1448 */
1449 thp_gfpmask &= ~__GFP_MOVABLE;
1450 thp = __alloc_pages_node(nid, thp_gfpmask, HPAGE_PMD_ORDER);
1451 if (!thp)
1452 return NULL;
1453 prep_transhuge_page(thp);
1454 return thp;
1455 }
1456
1457 return __alloc_pages_node(nid, gfp_mask, 0);
1458}
1459
Ira Weiny932f4a62019-05-13 17:17:03 -07001460static long check_and_migrate_cma_pages(struct task_struct *tsk,
1461 struct mm_struct *mm,
1462 unsigned long start,
1463 unsigned long nr_pages,
Aneesh Kumar K.V9a4e9f32019-03-05 15:47:44 -08001464 struct page **pages,
Ira Weiny932f4a62019-05-13 17:17:03 -07001465 struct vm_area_struct **vmas,
1466 unsigned int gup_flags)
Aneesh Kumar K.V9a4e9f32019-03-05 15:47:44 -08001467{
Pingfan Liuaa712392019-07-11 20:57:39 -07001468 unsigned long i;
1469 unsigned long step;
Aneesh Kumar K.V9a4e9f32019-03-05 15:47:44 -08001470 bool drain_allow = true;
1471 bool migrate_allow = true;
1472 LIST_HEAD(cma_page_list);
zhong jiangb96cc652019-11-30 17:49:50 -08001473 long ret = nr_pages;
Aneesh Kumar K.V9a4e9f32019-03-05 15:47:44 -08001474
1475check_again:
Pingfan Liuaa712392019-07-11 20:57:39 -07001476 for (i = 0; i < nr_pages;) {
1477
1478 struct page *head = compound_head(pages[i]);
1479
1480 /*
1481 * gup may start from a tail page. Advance step by the left
1482 * part.
1483 */
Matthew Wilcox (Oracle)d8c65462019-09-23 15:34:30 -07001484 step = compound_nr(head) - (pages[i] - head);
Aneesh Kumar K.V9a4e9f32019-03-05 15:47:44 -08001485 /*
1486 * If we get a page from the CMA zone, since we are going to
1487 * be pinning these entries, we might as well move them out
1488 * of the CMA zone if possible.
1489 */
Pingfan Liuaa712392019-07-11 20:57:39 -07001490 if (is_migrate_cma_page(head)) {
1491 if (PageHuge(head))
Aneesh Kumar K.V9a4e9f32019-03-05 15:47:44 -08001492 isolate_huge_page(head, &cma_page_list);
Pingfan Liuaa712392019-07-11 20:57:39 -07001493 else {
Aneesh Kumar K.V9a4e9f32019-03-05 15:47:44 -08001494 if (!PageLRU(head) && drain_allow) {
1495 lru_add_drain_all();
1496 drain_allow = false;
1497 }
1498
1499 if (!isolate_lru_page(head)) {
1500 list_add_tail(&head->lru, &cma_page_list);
1501 mod_node_page_state(page_pgdat(head),
1502 NR_ISOLATED_ANON +
1503 page_is_file_cache(head),
1504 hpage_nr_pages(head));
1505 }
1506 }
1507 }
Pingfan Liuaa712392019-07-11 20:57:39 -07001508
1509 i += step;
Aneesh Kumar K.V9a4e9f32019-03-05 15:47:44 -08001510 }
1511
1512 if (!list_empty(&cma_page_list)) {
1513 /*
1514 * drop the above get_user_pages reference.
1515 */
1516 for (i = 0; i < nr_pages; i++)
1517 put_page(pages[i]);
1518
1519 if (migrate_pages(&cma_page_list, new_non_cma_page,
1520 NULL, 0, MIGRATE_SYNC, MR_CONTIG_RANGE)) {
1521 /*
1522 * some of the pages failed migration. Do get_user_pages
1523 * without migration.
1524 */
1525 migrate_allow = false;
1526
1527 if (!list_empty(&cma_page_list))
1528 putback_movable_pages(&cma_page_list);
1529 }
1530 /*
Ira Weiny932f4a62019-05-13 17:17:03 -07001531 * We did migrate all the pages, Try to get the page references
1532 * again migrating any new CMA pages which we failed to isolate
1533 * earlier.
Aneesh Kumar K.V9a4e9f32019-03-05 15:47:44 -08001534 */
zhong jiangb96cc652019-11-30 17:49:50 -08001535 ret = __get_user_pages_locked(tsk, mm, start, nr_pages,
Ira Weiny932f4a62019-05-13 17:17:03 -07001536 pages, vmas, NULL,
1537 gup_flags);
1538
zhong jiangb96cc652019-11-30 17:49:50 -08001539 if ((ret > 0) && migrate_allow) {
1540 nr_pages = ret;
Aneesh Kumar K.V9a4e9f32019-03-05 15:47:44 -08001541 drain_allow = true;
1542 goto check_again;
1543 }
1544 }
1545
zhong jiangb96cc652019-11-30 17:49:50 -08001546 return ret;
Aneesh Kumar K.V9a4e9f32019-03-05 15:47:44 -08001547}
1548#else
Ira Weiny932f4a62019-05-13 17:17:03 -07001549static long check_and_migrate_cma_pages(struct task_struct *tsk,
1550 struct mm_struct *mm,
1551 unsigned long start,
1552 unsigned long nr_pages,
1553 struct page **pages,
1554 struct vm_area_struct **vmas,
1555 unsigned int gup_flags)
Aneesh Kumar K.V9a4e9f32019-03-05 15:47:44 -08001556{
1557 return nr_pages;
1558}
Christoph Hellwig050a9ad2019-07-11 20:57:21 -07001559#endif /* CONFIG_CMA */
Aneesh Kumar K.V9a4e9f32019-03-05 15:47:44 -08001560
Dan Williams2bb6d282017-11-29 16:10:35 -08001561/*
Ira Weiny932f4a62019-05-13 17:17:03 -07001562 * __gup_longterm_locked() is a wrapper for __get_user_pages_locked which
1563 * allows us to process the FOLL_LONGTERM flag.
Dan Williams2bb6d282017-11-29 16:10:35 -08001564 */
Ira Weiny932f4a62019-05-13 17:17:03 -07001565static long __gup_longterm_locked(struct task_struct *tsk,
1566 struct mm_struct *mm,
1567 unsigned long start,
1568 unsigned long nr_pages,
1569 struct page **pages,
1570 struct vm_area_struct **vmas,
1571 unsigned int gup_flags)
Dan Williams2bb6d282017-11-29 16:10:35 -08001572{
Ira Weiny932f4a62019-05-13 17:17:03 -07001573 struct vm_area_struct **vmas_tmp = vmas;
1574 unsigned long flags = 0;
Dan Williams2bb6d282017-11-29 16:10:35 -08001575 long rc, i;
1576
Ira Weiny932f4a62019-05-13 17:17:03 -07001577 if (gup_flags & FOLL_LONGTERM) {
1578 if (!pages)
1579 return -EINVAL;
Dan Williams2bb6d282017-11-29 16:10:35 -08001580
Ira Weiny932f4a62019-05-13 17:17:03 -07001581 if (!vmas_tmp) {
1582 vmas_tmp = kcalloc(nr_pages,
1583 sizeof(struct vm_area_struct *),
1584 GFP_KERNEL);
1585 if (!vmas_tmp)
1586 return -ENOMEM;
1587 }
1588 flags = memalloc_nocma_save();
Dan Williams2bb6d282017-11-29 16:10:35 -08001589 }
1590
Ira Weiny932f4a62019-05-13 17:17:03 -07001591 rc = __get_user_pages_locked(tsk, mm, start, nr_pages, pages,
1592 vmas_tmp, NULL, gup_flags);
Dan Williams2bb6d282017-11-29 16:10:35 -08001593
Ira Weiny932f4a62019-05-13 17:17:03 -07001594 if (gup_flags & FOLL_LONGTERM) {
1595 memalloc_nocma_restore(flags);
1596 if (rc < 0)
1597 goto out;
1598
1599 if (check_dax_vmas(vmas_tmp, rc)) {
1600 for (i = 0; i < rc; i++)
1601 put_page(pages[i]);
1602 rc = -EOPNOTSUPP;
1603 goto out;
1604 }
1605
1606 rc = check_and_migrate_cma_pages(tsk, mm, start, rc, pages,
1607 vmas_tmp, gup_flags);
Aneesh Kumar K.V9a4e9f32019-03-05 15:47:44 -08001608 }
1609
Dan Williams2bb6d282017-11-29 16:10:35 -08001610out:
Ira Weiny932f4a62019-05-13 17:17:03 -07001611 if (vmas_tmp != vmas)
1612 kfree(vmas_tmp);
Dan Williams2bb6d282017-11-29 16:10:35 -08001613 return rc;
1614}
Ira Weiny932f4a62019-05-13 17:17:03 -07001615#else /* !CONFIG_FS_DAX && !CONFIG_CMA */
1616static __always_inline long __gup_longterm_locked(struct task_struct *tsk,
1617 struct mm_struct *mm,
1618 unsigned long start,
1619 unsigned long nr_pages,
1620 struct page **pages,
1621 struct vm_area_struct **vmas,
1622 unsigned int flags)
1623{
1624 return __get_user_pages_locked(tsk, mm, start, nr_pages, pages, vmas,
1625 NULL, flags);
1626}
1627#endif /* CONFIG_FS_DAX || CONFIG_CMA */
1628
1629/*
1630 * This is the same as get_user_pages_remote(), just with a
1631 * less-flexible calling convention where we assume that the task
1632 * and mm being operated on are the current task's and don't allow
1633 * passing of a locked parameter. We also obviously don't pass
1634 * FOLL_REMOTE in here.
1635 */
1636long get_user_pages(unsigned long start, unsigned long nr_pages,
1637 unsigned int gup_flags, struct page **pages,
1638 struct vm_area_struct **vmas)
1639{
1640 return __gup_longterm_locked(current, current->mm, start, nr_pages,
1641 pages, vmas, gup_flags | FOLL_TOUCH);
1642}
1643EXPORT_SYMBOL(get_user_pages);
Dan Williams2bb6d282017-11-29 16:10:35 -08001644
Christoph Hellwigd3649f62019-07-11 20:57:18 -07001645/*
1646 * We can leverage the VM_FAULT_RETRY functionality in the page fault
1647 * paths better by using either get_user_pages_locked() or
1648 * get_user_pages_unlocked().
Kirill A. Shutemovacc3c8d2015-04-14 15:44:45 -07001649 *
Christoph Hellwigd3649f62019-07-11 20:57:18 -07001650 * get_user_pages_locked() is suitable to replace the form:
Kirill A. Shutemovacc3c8d2015-04-14 15:44:45 -07001651 *
Christoph Hellwigd3649f62019-07-11 20:57:18 -07001652 * down_read(&mm->mmap_sem);
1653 * do_something()
1654 * get_user_pages(tsk, mm, ..., pages, NULL);
1655 * up_read(&mm->mmap_sem);
Kirill A. Shutemovacc3c8d2015-04-14 15:44:45 -07001656 *
Christoph Hellwigd3649f62019-07-11 20:57:18 -07001657 * to:
Kirill A. Shutemovacc3c8d2015-04-14 15:44:45 -07001658 *
Christoph Hellwigd3649f62019-07-11 20:57:18 -07001659 * int locked = 1;
1660 * down_read(&mm->mmap_sem);
1661 * do_something()
1662 * get_user_pages_locked(tsk, mm, ..., pages, &locked);
1663 * if (locked)
1664 * up_read(&mm->mmap_sem);
Kirill A. Shutemovacc3c8d2015-04-14 15:44:45 -07001665 */
Christoph Hellwigd3649f62019-07-11 20:57:18 -07001666long get_user_pages_locked(unsigned long start, unsigned long nr_pages,
1667 unsigned int gup_flags, struct page **pages,
1668 int *locked)
Kirill A. Shutemovacc3c8d2015-04-14 15:44:45 -07001669{
Kirill A. Shutemovacc3c8d2015-04-14 15:44:45 -07001670 /*
Christoph Hellwigd3649f62019-07-11 20:57:18 -07001671 * FIXME: Current FOLL_LONGTERM behavior is incompatible with
1672 * FAULT_FLAG_ALLOW_RETRY because of the FS DAX check requirement on
1673 * vmas. As there are no users of this flag in this call we simply
1674 * disallow this option for now.
Kirill A. Shutemovacc3c8d2015-04-14 15:44:45 -07001675 */
Christoph Hellwigd3649f62019-07-11 20:57:18 -07001676 if (WARN_ON_ONCE(gup_flags & FOLL_LONGTERM))
1677 return -EINVAL;
Kirill A. Shutemovacc3c8d2015-04-14 15:44:45 -07001678
Christoph Hellwigd3649f62019-07-11 20:57:18 -07001679 return __get_user_pages_locked(current, current->mm, start, nr_pages,
1680 pages, NULL, locked,
1681 gup_flags | FOLL_TOUCH);
Kirill A. Shutemovacc3c8d2015-04-14 15:44:45 -07001682}
Christoph Hellwigd3649f62019-07-11 20:57:18 -07001683EXPORT_SYMBOL(get_user_pages_locked);
Kirill A. Shutemovacc3c8d2015-04-14 15:44:45 -07001684
1685/*
Christoph Hellwigd3649f62019-07-11 20:57:18 -07001686 * get_user_pages_unlocked() is suitable to replace the form:
Kirill A. Shutemovacc3c8d2015-04-14 15:44:45 -07001687 *
Christoph Hellwigd3649f62019-07-11 20:57:18 -07001688 * down_read(&mm->mmap_sem);
1689 * get_user_pages(tsk, mm, ..., pages, NULL);
1690 * up_read(&mm->mmap_sem);
1691 *
1692 * with:
1693 *
1694 * get_user_pages_unlocked(tsk, mm, ..., pages);
1695 *
1696 * It is functionally equivalent to get_user_pages_fast so
1697 * get_user_pages_fast should be used instead if specific gup_flags
1698 * (e.g. FOLL_FORCE) are not required.
Kirill A. Shutemovacc3c8d2015-04-14 15:44:45 -07001699 */
Christoph Hellwigd3649f62019-07-11 20:57:18 -07001700long get_user_pages_unlocked(unsigned long start, unsigned long nr_pages,
1701 struct page **pages, unsigned int gup_flags)
Kirill A. Shutemovacc3c8d2015-04-14 15:44:45 -07001702{
1703 struct mm_struct *mm = current->mm;
Christoph Hellwigd3649f62019-07-11 20:57:18 -07001704 int locked = 1;
1705 long ret;
Kirill A. Shutemovacc3c8d2015-04-14 15:44:45 -07001706
Christoph Hellwigd3649f62019-07-11 20:57:18 -07001707 /*
1708 * FIXME: Current FOLL_LONGTERM behavior is incompatible with
1709 * FAULT_FLAG_ALLOW_RETRY because of the FS DAX check requirement on
1710 * vmas. As there are no users of this flag in this call we simply
1711 * disallow this option for now.
1712 */
1713 if (WARN_ON_ONCE(gup_flags & FOLL_LONGTERM))
1714 return -EINVAL;
Kirill A. Shutemovacc3c8d2015-04-14 15:44:45 -07001715
Christoph Hellwigd3649f62019-07-11 20:57:18 -07001716 down_read(&mm->mmap_sem);
1717 ret = __get_user_pages_locked(current, mm, start, nr_pages, pages, NULL,
1718 &locked, gup_flags | FOLL_TOUCH);
Kirill A. Shutemovacc3c8d2015-04-14 15:44:45 -07001719 if (locked)
1720 up_read(&mm->mmap_sem);
Christoph Hellwigd3649f62019-07-11 20:57:18 -07001721 return ret;
Kirill A. Shutemovacc3c8d2015-04-14 15:44:45 -07001722}
Christoph Hellwigd3649f62019-07-11 20:57:18 -07001723EXPORT_SYMBOL(get_user_pages_unlocked);
Steve Capper2667f502014-10-09 15:29:14 -07001724
1725/*
Christoph Hellwig67a929e2019-07-11 20:57:14 -07001726 * Fast GUP
Steve Capper2667f502014-10-09 15:29:14 -07001727 *
1728 * get_user_pages_fast attempts to pin user pages by walking the page
1729 * tables directly and avoids taking locks. Thus the walker needs to be
1730 * protected from page table pages being freed from under it, and should
1731 * block any THP splits.
1732 *
1733 * One way to achieve this is to have the walker disable interrupts, and
1734 * rely on IPIs from the TLB flushing code blocking before the page table
1735 * pages are freed. This is unsuitable for architectures that do not need
1736 * to broadcast an IPI when invalidating TLBs.
1737 *
1738 * Another way to achieve this is to batch up page table containing pages
1739 * belonging to more than one mm_user, then rcu_sched a callback to free those
1740 * pages. Disabling interrupts will allow the fast_gup walker to both block
1741 * the rcu_sched callback, and an IPI that we broadcast for splitting THPs
1742 * (which is a relatively rare event). The code below adopts this strategy.
1743 *
1744 * Before activating this code, please be aware that the following assumptions
1745 * are currently made:
1746 *
Kirill A. Shutemove5855132017-06-06 14:31:20 +03001747 * *) Either HAVE_RCU_TABLE_FREE is enabled, and tlb_remove_table() is used to
1748 * free pages containing page tables or TLB flushing requires IPI broadcast.
Steve Capper2667f502014-10-09 15:29:14 -07001749 *
Steve Capper2667f502014-10-09 15:29:14 -07001750 * *) ptes can be read atomically by the architecture.
1751 *
1752 * *) access_ok is sufficient to validate userspace address ranges.
1753 *
1754 * The last two assumptions can be relaxed by the addition of helper functions.
1755 *
1756 * This code is based heavily on the PowerPC implementation by Nick Piggin.
1757 */
Christoph Hellwig67a929e2019-07-11 20:57:14 -07001758#ifdef CONFIG_HAVE_FAST_GUP
Christoph Hellwig39656e82019-07-11 20:56:49 -07001759#ifdef CONFIG_GUP_GET_PTE_LOW_HIGH
Kirill A. Shutemov0005d202017-03-16 18:26:51 +03001760/*
Christoph Hellwig39656e82019-07-11 20:56:49 -07001761 * WARNING: only to be used in the get_user_pages_fast() implementation.
1762 *
1763 * With get_user_pages_fast(), we walk down the pagetables without taking any
1764 * locks. For this we would like to load the pointers atomically, but sometimes
1765 * that is not possible (e.g. without expensive cmpxchg8b on x86_32 PAE). What
1766 * we do have is the guarantee that a PTE will only either go from not present
1767 * to present, or present to not present or both -- it will not switch to a
1768 * completely different present page without a TLB flush in between; something
1769 * that we are blocking by holding interrupts off.
1770 *
1771 * Setting ptes from not present to present goes:
1772 *
1773 * ptep->pte_high = h;
1774 * smp_wmb();
1775 * ptep->pte_low = l;
1776 *
1777 * And present to not present goes:
1778 *
1779 * ptep->pte_low = 0;
1780 * smp_wmb();
1781 * ptep->pte_high = 0;
1782 *
1783 * We must ensure here that the load of pte_low sees 'l' IFF pte_high sees 'h'.
1784 * We load pte_high *after* loading pte_low, which ensures we don't see an older
1785 * value of pte_high. *Then* we recheck pte_low, which ensures that we haven't
1786 * picked up a changed pte high. We might have gotten rubbish values from
1787 * pte_low and pte_high, but we are guaranteed that pte_low will not have the
1788 * present bit set *unless* it is 'l'. Because get_user_pages_fast() only
1789 * operates on present ptes we're safe.
1790 */
1791static inline pte_t gup_get_pte(pte_t *ptep)
1792{
1793 pte_t pte;
1794
1795 do {
1796 pte.pte_low = ptep->pte_low;
1797 smp_rmb();
1798 pte.pte_high = ptep->pte_high;
1799 smp_rmb();
1800 } while (unlikely(pte.pte_low != ptep->pte_low));
1801
1802 return pte;
1803}
1804#else /* CONFIG_GUP_GET_PTE_LOW_HIGH */
1805/*
1806 * We require that the PTE can be read atomically.
Kirill A. Shutemov0005d202017-03-16 18:26:51 +03001807 */
1808static inline pte_t gup_get_pte(pte_t *ptep)
1809{
1810 return READ_ONCE(*ptep);
1811}
Christoph Hellwig39656e82019-07-11 20:56:49 -07001812#endif /* CONFIG_GUP_GET_PTE_LOW_HIGH */
Kirill A. Shutemov0005d202017-03-16 18:26:51 +03001813
Guenter Roeck790c7362019-07-11 20:57:46 -07001814static void __maybe_unused undo_dev_pagemap(int *nr, int nr_start,
1815 struct page **pages)
Kirill A. Shutemovb59f65f2017-03-16 18:26:53 +03001816{
1817 while ((*nr) - nr_start) {
1818 struct page *page = pages[--(*nr)];
1819
1820 ClearPageReferenced(page);
1821 put_page(page);
1822 }
1823}
1824
Laurent Dufour3010a5e2018-06-07 17:06:08 -07001825#ifdef CONFIG_ARCH_HAS_PTE_SPECIAL
Steve Capper2667f502014-10-09 15:29:14 -07001826static int gup_pte_range(pmd_t pmd, unsigned long addr, unsigned long end,
Ira Weinyb798bec2019-05-13 17:17:07 -07001827 unsigned int flags, struct page **pages, int *nr)
Steve Capper2667f502014-10-09 15:29:14 -07001828{
Kirill A. Shutemovb59f65f2017-03-16 18:26:53 +03001829 struct dev_pagemap *pgmap = NULL;
1830 int nr_start = *nr, ret = 0;
Steve Capper2667f502014-10-09 15:29:14 -07001831 pte_t *ptep, *ptem;
Steve Capper2667f502014-10-09 15:29:14 -07001832
1833 ptem = ptep = pte_offset_map(&pmd, addr);
1834 do {
Kirill A. Shutemov0005d202017-03-16 18:26:51 +03001835 pte_t pte = gup_get_pte(ptep);
Kirill A. Shutemov7aef4172016-01-15 16:52:32 -08001836 struct page *head, *page;
Steve Capper2667f502014-10-09 15:29:14 -07001837
1838 /*
1839 * Similar to the PMD case below, NUMA hinting must take slow
Mel Gorman8a0516e2015-02-12 14:58:22 -08001840 * path using the pte_protnone check.
Steve Capper2667f502014-10-09 15:29:14 -07001841 */
Kirill A. Shutemove7884f82017-03-16 18:26:50 +03001842 if (pte_protnone(pte))
1843 goto pte_unmap;
1844
Ira Weinyb798bec2019-05-13 17:17:07 -07001845 if (!pte_access_permitted(pte, flags & FOLL_WRITE))
Kirill A. Shutemove7884f82017-03-16 18:26:50 +03001846 goto pte_unmap;
1847
Kirill A. Shutemovb59f65f2017-03-16 18:26:53 +03001848 if (pte_devmap(pte)) {
Ira Weiny7af75562019-05-13 17:17:14 -07001849 if (unlikely(flags & FOLL_LONGTERM))
1850 goto pte_unmap;
1851
Kirill A. Shutemovb59f65f2017-03-16 18:26:53 +03001852 pgmap = get_dev_pagemap(pte_pfn(pte), pgmap);
1853 if (unlikely(!pgmap)) {
1854 undo_dev_pagemap(nr, nr_start, pages);
1855 goto pte_unmap;
1856 }
1857 } else if (pte_special(pte))
Steve Capper2667f502014-10-09 15:29:14 -07001858 goto pte_unmap;
1859
1860 VM_BUG_ON(!pfn_valid(pte_pfn(pte)));
1861 page = pte_page(pte);
1862
Linus Torvalds8fde12c2019-04-11 10:49:19 -07001863 head = try_get_compound_head(page, 1);
1864 if (!head)
Steve Capper2667f502014-10-09 15:29:14 -07001865 goto pte_unmap;
1866
1867 if (unlikely(pte_val(pte) != pte_val(*ptep))) {
Kirill A. Shutemov7aef4172016-01-15 16:52:32 -08001868 put_page(head);
Steve Capper2667f502014-10-09 15:29:14 -07001869 goto pte_unmap;
1870 }
1871
Kirill A. Shutemov7aef4172016-01-15 16:52:32 -08001872 VM_BUG_ON_PAGE(compound_head(page) != head, page);
Kirill A. Shutemove9348052017-03-16 18:26:52 +03001873
1874 SetPageReferenced(page);
Steve Capper2667f502014-10-09 15:29:14 -07001875 pages[*nr] = page;
1876 (*nr)++;
1877
1878 } while (ptep++, addr += PAGE_SIZE, addr != end);
1879
1880 ret = 1;
1881
1882pte_unmap:
Christoph Hellwig832d7aa2017-12-29 08:54:01 +01001883 if (pgmap)
1884 put_dev_pagemap(pgmap);
Steve Capper2667f502014-10-09 15:29:14 -07001885 pte_unmap(ptem);
1886 return ret;
1887}
1888#else
1889
1890/*
1891 * If we can't determine whether or not a pte is special, then fail immediately
1892 * for ptes. Note, we can still pin HugeTLB and THP as these are guaranteed not
1893 * to be special.
1894 *
1895 * For a futex to be placed on a THP tail page, get_futex_key requires a
1896 * __get_user_pages_fast implementation that can pin pages. Thus it's still
1897 * useful to have gup_huge_pmd even if we can't operate on ptes.
1898 */
1899static int gup_pte_range(pmd_t pmd, unsigned long addr, unsigned long end,
Ira Weinyb798bec2019-05-13 17:17:07 -07001900 unsigned int flags, struct page **pages, int *nr)
Steve Capper2667f502014-10-09 15:29:14 -07001901{
1902 return 0;
1903}
Laurent Dufour3010a5e2018-06-07 17:06:08 -07001904#endif /* CONFIG_ARCH_HAS_PTE_SPECIAL */
Steve Capper2667f502014-10-09 15:29:14 -07001905
Robin Murphy17596732019-07-16 16:30:47 -07001906#if defined(CONFIG_ARCH_HAS_PTE_DEVMAP) && defined(CONFIG_TRANSPARENT_HUGEPAGE)
Kirill A. Shutemovb59f65f2017-03-16 18:26:53 +03001907static int __gup_device_huge(unsigned long pfn, unsigned long addr,
1908 unsigned long end, struct page **pages, int *nr)
1909{
1910 int nr_start = *nr;
1911 struct dev_pagemap *pgmap = NULL;
1912
1913 do {
1914 struct page *page = pfn_to_page(pfn);
1915
1916 pgmap = get_dev_pagemap(pfn, pgmap);
1917 if (unlikely(!pgmap)) {
1918 undo_dev_pagemap(nr, nr_start, pages);
1919 return 0;
1920 }
1921 SetPageReferenced(page);
1922 pages[*nr] = page;
1923 get_page(page);
Kirill A. Shutemovb59f65f2017-03-16 18:26:53 +03001924 (*nr)++;
1925 pfn++;
1926 } while (addr += PAGE_SIZE, addr != end);
Christoph Hellwig832d7aa2017-12-29 08:54:01 +01001927
1928 if (pgmap)
1929 put_dev_pagemap(pgmap);
Kirill A. Shutemovb59f65f2017-03-16 18:26:53 +03001930 return 1;
1931}
1932
Dan Williamsa9b6de72018-04-19 21:32:19 -07001933static int __gup_device_huge_pmd(pmd_t orig, pmd_t *pmdp, unsigned long addr,
Kirill A. Shutemovb59f65f2017-03-16 18:26:53 +03001934 unsigned long end, struct page **pages, int *nr)
1935{
1936 unsigned long fault_pfn;
Dan Williamsa9b6de72018-04-19 21:32:19 -07001937 int nr_start = *nr;
Kirill A. Shutemovb59f65f2017-03-16 18:26:53 +03001938
Dan Williamsa9b6de72018-04-19 21:32:19 -07001939 fault_pfn = pmd_pfn(orig) + ((addr & ~PMD_MASK) >> PAGE_SHIFT);
1940 if (!__gup_device_huge(fault_pfn, addr, end, pages, nr))
1941 return 0;
1942
1943 if (unlikely(pmd_val(orig) != pmd_val(*pmdp))) {
1944 undo_dev_pagemap(nr, nr_start, pages);
1945 return 0;
1946 }
1947 return 1;
Kirill A. Shutemovb59f65f2017-03-16 18:26:53 +03001948}
1949
Dan Williamsa9b6de72018-04-19 21:32:19 -07001950static int __gup_device_huge_pud(pud_t orig, pud_t *pudp, unsigned long addr,
Kirill A. Shutemovb59f65f2017-03-16 18:26:53 +03001951 unsigned long end, struct page **pages, int *nr)
1952{
1953 unsigned long fault_pfn;
Dan Williamsa9b6de72018-04-19 21:32:19 -07001954 int nr_start = *nr;
Kirill A. Shutemovb59f65f2017-03-16 18:26:53 +03001955
Dan Williamsa9b6de72018-04-19 21:32:19 -07001956 fault_pfn = pud_pfn(orig) + ((addr & ~PUD_MASK) >> PAGE_SHIFT);
1957 if (!__gup_device_huge(fault_pfn, addr, end, pages, nr))
1958 return 0;
1959
1960 if (unlikely(pud_val(orig) != pud_val(*pudp))) {
1961 undo_dev_pagemap(nr, nr_start, pages);
1962 return 0;
1963 }
1964 return 1;
Kirill A. Shutemovb59f65f2017-03-16 18:26:53 +03001965}
1966#else
Dan Williamsa9b6de72018-04-19 21:32:19 -07001967static int __gup_device_huge_pmd(pmd_t orig, pmd_t *pmdp, unsigned long addr,
Kirill A. Shutemovb59f65f2017-03-16 18:26:53 +03001968 unsigned long end, struct page **pages, int *nr)
1969{
1970 BUILD_BUG();
1971 return 0;
1972}
1973
Dan Williamsa9b6de72018-04-19 21:32:19 -07001974static int __gup_device_huge_pud(pud_t pud, pud_t *pudp, unsigned long addr,
Kirill A. Shutemovb59f65f2017-03-16 18:26:53 +03001975 unsigned long end, struct page **pages, int *nr)
1976{
1977 BUILD_BUG();
1978 return 0;
1979}
1980#endif
1981
John Hubbarda43e9822020-01-30 22:12:17 -08001982static int record_subpages(struct page *page, unsigned long addr,
1983 unsigned long end, struct page **pages)
1984{
1985 int nr;
1986
1987 for (nr = 0; addr != end; addr += PAGE_SIZE)
1988 pages[nr++] = page++;
1989
1990 return nr;
1991}
1992
1993static void put_compound_head(struct page *page, int refs)
1994{
1995 VM_BUG_ON_PAGE(page_ref_count(page) < refs, page);
1996 /*
1997 * Calling put_page() for each ref is unnecessarily slow. Only the last
1998 * ref needs a put_page().
1999 */
2000 if (refs > 1)
2001 page_ref_sub(page, refs - 1);
2002 put_page(page);
2003}
2004
Christoph Hellwigcbd34da2019-07-11 20:57:28 -07002005#ifdef CONFIG_ARCH_HAS_HUGEPD
2006static unsigned long hugepte_addr_end(unsigned long addr, unsigned long end,
2007 unsigned long sz)
2008{
2009 unsigned long __boundary = (addr + sz) & ~(sz-1);
2010 return (__boundary - 1 < end - 1) ? __boundary : end;
2011}
2012
2013static int gup_hugepte(pte_t *ptep, unsigned long sz, unsigned long addr,
John Hubbard0cd22af2019-10-18 20:19:53 -07002014 unsigned long end, unsigned int flags,
2015 struct page **pages, int *nr)
Christoph Hellwigcbd34da2019-07-11 20:57:28 -07002016{
2017 unsigned long pte_end;
2018 struct page *head, *page;
2019 pte_t pte;
2020 int refs;
2021
2022 pte_end = (addr + sz) & ~(sz-1);
2023 if (pte_end < end)
2024 end = pte_end;
2025
2026 pte = READ_ONCE(*ptep);
2027
John Hubbard0cd22af2019-10-18 20:19:53 -07002028 if (!pte_access_permitted(pte, flags & FOLL_WRITE))
Christoph Hellwigcbd34da2019-07-11 20:57:28 -07002029 return 0;
2030
2031 /* hugepages are never "special" */
2032 VM_BUG_ON(!pfn_valid(pte_pfn(pte)));
2033
Christoph Hellwigcbd34da2019-07-11 20:57:28 -07002034 head = pte_page(pte);
Christoph Hellwigcbd34da2019-07-11 20:57:28 -07002035 page = head + ((addr & (sz-1)) >> PAGE_SHIFT);
John Hubbarda43e9822020-01-30 22:12:17 -08002036 refs = record_subpages(page, addr, end, pages + *nr);
Christoph Hellwigcbd34da2019-07-11 20:57:28 -07002037
Christoph Hellwig01a36912019-07-11 20:57:32 -07002038 head = try_get_compound_head(head, refs);
John Hubbarda43e9822020-01-30 22:12:17 -08002039 if (!head)
Christoph Hellwigcbd34da2019-07-11 20:57:28 -07002040 return 0;
Christoph Hellwigcbd34da2019-07-11 20:57:28 -07002041
2042 if (unlikely(pte_val(pte) != pte_val(*ptep))) {
John Hubbarda43e9822020-01-30 22:12:17 -08002043 put_compound_head(head, refs);
Christoph Hellwigcbd34da2019-07-11 20:57:28 -07002044 return 0;
2045 }
2046
John Hubbarda43e9822020-01-30 22:12:17 -08002047 *nr += refs;
Christoph Hellwig520b4a42019-07-11 20:57:36 -07002048 SetPageReferenced(head);
Christoph Hellwigcbd34da2019-07-11 20:57:28 -07002049 return 1;
2050}
2051
2052static int gup_huge_pd(hugepd_t hugepd, unsigned long addr,
John Hubbard0cd22af2019-10-18 20:19:53 -07002053 unsigned int pdshift, unsigned long end, unsigned int flags,
Christoph Hellwigcbd34da2019-07-11 20:57:28 -07002054 struct page **pages, int *nr)
2055{
2056 pte_t *ptep;
2057 unsigned long sz = 1UL << hugepd_shift(hugepd);
2058 unsigned long next;
2059
2060 ptep = hugepte_offset(hugepd, addr, pdshift);
2061 do {
2062 next = hugepte_addr_end(addr, end, sz);
John Hubbard0cd22af2019-10-18 20:19:53 -07002063 if (!gup_hugepte(ptep, sz, addr, end, flags, pages, nr))
Christoph Hellwigcbd34da2019-07-11 20:57:28 -07002064 return 0;
2065 } while (ptep++, addr = next, addr != end);
2066
2067 return 1;
2068}
2069#else
2070static inline int gup_huge_pd(hugepd_t hugepd, unsigned long addr,
John Hubbard0cd22af2019-10-18 20:19:53 -07002071 unsigned int pdshift, unsigned long end, unsigned int flags,
Christoph Hellwigcbd34da2019-07-11 20:57:28 -07002072 struct page **pages, int *nr)
2073{
2074 return 0;
2075}
2076#endif /* CONFIG_ARCH_HAS_HUGEPD */
2077
Steve Capper2667f502014-10-09 15:29:14 -07002078static int gup_huge_pmd(pmd_t orig, pmd_t *pmdp, unsigned long addr,
John Hubbard0cd22af2019-10-18 20:19:53 -07002079 unsigned long end, unsigned int flags,
2080 struct page **pages, int *nr)
Steve Capper2667f502014-10-09 15:29:14 -07002081{
Kirill A. Shutemovddc58f22016-01-15 16:52:56 -08002082 struct page *head, *page;
Steve Capper2667f502014-10-09 15:29:14 -07002083 int refs;
2084
Ira Weinyb798bec2019-05-13 17:17:07 -07002085 if (!pmd_access_permitted(orig, flags & FOLL_WRITE))
Steve Capper2667f502014-10-09 15:29:14 -07002086 return 0;
2087
Ira Weiny7af75562019-05-13 17:17:14 -07002088 if (pmd_devmap(orig)) {
2089 if (unlikely(flags & FOLL_LONGTERM))
2090 return 0;
Dan Williamsa9b6de72018-04-19 21:32:19 -07002091 return __gup_device_huge_pmd(orig, pmdp, addr, end, pages, nr);
Ira Weiny7af75562019-05-13 17:17:14 -07002092 }
Kirill A. Shutemovb59f65f2017-03-16 18:26:53 +03002093
Punit Agrawald63206e2017-07-06 15:39:39 -07002094 page = pmd_page(orig) + ((addr & ~PMD_MASK) >> PAGE_SHIFT);
John Hubbarda43e9822020-01-30 22:12:17 -08002095 refs = record_subpages(page, addr, end, pages + *nr);
Steve Capper2667f502014-10-09 15:29:14 -07002096
Linus Torvalds8fde12c2019-04-11 10:49:19 -07002097 head = try_get_compound_head(pmd_page(orig), refs);
John Hubbarda43e9822020-01-30 22:12:17 -08002098 if (!head)
Steve Capper2667f502014-10-09 15:29:14 -07002099 return 0;
Steve Capper2667f502014-10-09 15:29:14 -07002100
2101 if (unlikely(pmd_val(orig) != pmd_val(*pmdp))) {
John Hubbarda43e9822020-01-30 22:12:17 -08002102 put_compound_head(head, refs);
Steve Capper2667f502014-10-09 15:29:14 -07002103 return 0;
2104 }
2105
John Hubbarda43e9822020-01-30 22:12:17 -08002106 *nr += refs;
Kirill A. Shutemove9348052017-03-16 18:26:52 +03002107 SetPageReferenced(head);
Steve Capper2667f502014-10-09 15:29:14 -07002108 return 1;
2109}
2110
2111static int gup_huge_pud(pud_t orig, pud_t *pudp, unsigned long addr,
Ira Weinyb798bec2019-05-13 17:17:07 -07002112 unsigned long end, unsigned int flags, struct page **pages, int *nr)
Steve Capper2667f502014-10-09 15:29:14 -07002113{
Kirill A. Shutemovddc58f22016-01-15 16:52:56 -08002114 struct page *head, *page;
Steve Capper2667f502014-10-09 15:29:14 -07002115 int refs;
2116
Ira Weinyb798bec2019-05-13 17:17:07 -07002117 if (!pud_access_permitted(orig, flags & FOLL_WRITE))
Steve Capper2667f502014-10-09 15:29:14 -07002118 return 0;
2119
Ira Weiny7af75562019-05-13 17:17:14 -07002120 if (pud_devmap(orig)) {
2121 if (unlikely(flags & FOLL_LONGTERM))
2122 return 0;
Dan Williamsa9b6de72018-04-19 21:32:19 -07002123 return __gup_device_huge_pud(orig, pudp, addr, end, pages, nr);
Ira Weiny7af75562019-05-13 17:17:14 -07002124 }
Kirill A. Shutemovb59f65f2017-03-16 18:26:53 +03002125
Punit Agrawald63206e2017-07-06 15:39:39 -07002126 page = pud_page(orig) + ((addr & ~PUD_MASK) >> PAGE_SHIFT);
John Hubbarda43e9822020-01-30 22:12:17 -08002127 refs = record_subpages(page, addr, end, pages + *nr);
Steve Capper2667f502014-10-09 15:29:14 -07002128
Linus Torvalds8fde12c2019-04-11 10:49:19 -07002129 head = try_get_compound_head(pud_page(orig), refs);
John Hubbarda43e9822020-01-30 22:12:17 -08002130 if (!head)
Steve Capper2667f502014-10-09 15:29:14 -07002131 return 0;
Steve Capper2667f502014-10-09 15:29:14 -07002132
2133 if (unlikely(pud_val(orig) != pud_val(*pudp))) {
John Hubbarda43e9822020-01-30 22:12:17 -08002134 put_compound_head(head, refs);
Steve Capper2667f502014-10-09 15:29:14 -07002135 return 0;
2136 }
2137
John Hubbarda43e9822020-01-30 22:12:17 -08002138 *nr += refs;
Kirill A. Shutemove9348052017-03-16 18:26:52 +03002139 SetPageReferenced(head);
Steve Capper2667f502014-10-09 15:29:14 -07002140 return 1;
2141}
2142
Aneesh Kumar K.Vf30c59e2014-11-05 21:57:40 +05302143static int gup_huge_pgd(pgd_t orig, pgd_t *pgdp, unsigned long addr,
Ira Weinyb798bec2019-05-13 17:17:07 -07002144 unsigned long end, unsigned int flags,
Aneesh Kumar K.Vf30c59e2014-11-05 21:57:40 +05302145 struct page **pages, int *nr)
2146{
2147 int refs;
Kirill A. Shutemovddc58f22016-01-15 16:52:56 -08002148 struct page *head, *page;
Aneesh Kumar K.Vf30c59e2014-11-05 21:57:40 +05302149
Ira Weinyb798bec2019-05-13 17:17:07 -07002150 if (!pgd_access_permitted(orig, flags & FOLL_WRITE))
Aneesh Kumar K.Vf30c59e2014-11-05 21:57:40 +05302151 return 0;
2152
Kirill A. Shutemovb59f65f2017-03-16 18:26:53 +03002153 BUILD_BUG_ON(pgd_devmap(orig));
John Hubbarda43e9822020-01-30 22:12:17 -08002154
Punit Agrawald63206e2017-07-06 15:39:39 -07002155 page = pgd_page(orig) + ((addr & ~PGDIR_MASK) >> PAGE_SHIFT);
John Hubbarda43e9822020-01-30 22:12:17 -08002156 refs = record_subpages(page, addr, end, pages + *nr);
Aneesh Kumar K.Vf30c59e2014-11-05 21:57:40 +05302157
Linus Torvalds8fde12c2019-04-11 10:49:19 -07002158 head = try_get_compound_head(pgd_page(orig), refs);
John Hubbarda43e9822020-01-30 22:12:17 -08002159 if (!head)
Aneesh Kumar K.Vf30c59e2014-11-05 21:57:40 +05302160 return 0;
Aneesh Kumar K.Vf30c59e2014-11-05 21:57:40 +05302161
2162 if (unlikely(pgd_val(orig) != pgd_val(*pgdp))) {
John Hubbarda43e9822020-01-30 22:12:17 -08002163 put_compound_head(head, refs);
Aneesh Kumar K.Vf30c59e2014-11-05 21:57:40 +05302164 return 0;
2165 }
2166
John Hubbarda43e9822020-01-30 22:12:17 -08002167 *nr += refs;
Kirill A. Shutemove9348052017-03-16 18:26:52 +03002168 SetPageReferenced(head);
Aneesh Kumar K.Vf30c59e2014-11-05 21:57:40 +05302169 return 1;
2170}
2171
Steve Capper2667f502014-10-09 15:29:14 -07002172static int gup_pmd_range(pud_t pud, unsigned long addr, unsigned long end,
Ira Weinyb798bec2019-05-13 17:17:07 -07002173 unsigned int flags, struct page **pages, int *nr)
Steve Capper2667f502014-10-09 15:29:14 -07002174{
2175 unsigned long next;
2176 pmd_t *pmdp;
2177
2178 pmdp = pmd_offset(&pud, addr);
2179 do {
Christian Borntraeger38c5ce92015-01-06 22:54:46 +01002180 pmd_t pmd = READ_ONCE(*pmdp);
Steve Capper2667f502014-10-09 15:29:14 -07002181
2182 next = pmd_addr_end(addr, end);
Zi Yan84c3fc42017-09-08 16:11:01 -07002183 if (!pmd_present(pmd))
Steve Capper2667f502014-10-09 15:29:14 -07002184 return 0;
2185
Yu Zhao414fd082019-02-12 15:35:58 -08002186 if (unlikely(pmd_trans_huge(pmd) || pmd_huge(pmd) ||
2187 pmd_devmap(pmd))) {
Steve Capper2667f502014-10-09 15:29:14 -07002188 /*
2189 * NUMA hinting faults need to be handled in the GUP
2190 * slowpath for accounting purposes and so that they
2191 * can be serialised against THP migration.
2192 */
Mel Gorman8a0516e2015-02-12 14:58:22 -08002193 if (pmd_protnone(pmd))
Steve Capper2667f502014-10-09 15:29:14 -07002194 return 0;
2195
Ira Weinyb798bec2019-05-13 17:17:07 -07002196 if (!gup_huge_pmd(pmd, pmdp, addr, next, flags,
Steve Capper2667f502014-10-09 15:29:14 -07002197 pages, nr))
2198 return 0;
2199
Aneesh Kumar K.Vf30c59e2014-11-05 21:57:40 +05302200 } else if (unlikely(is_hugepd(__hugepd(pmd_val(pmd))))) {
2201 /*
2202 * architecture have different format for hugetlbfs
2203 * pmd format and THP pmd format
2204 */
2205 if (!gup_huge_pd(__hugepd(pmd_val(pmd)), addr,
Ira Weinyb798bec2019-05-13 17:17:07 -07002206 PMD_SHIFT, next, flags, pages, nr))
Aneesh Kumar K.Vf30c59e2014-11-05 21:57:40 +05302207 return 0;
Ira Weinyb798bec2019-05-13 17:17:07 -07002208 } else if (!gup_pte_range(pmd, addr, next, flags, pages, nr))
Mario Leinweber29231172018-04-05 16:24:18 -07002209 return 0;
Steve Capper2667f502014-10-09 15:29:14 -07002210 } while (pmdp++, addr = next, addr != end);
2211
2212 return 1;
2213}
2214
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +03002215static int gup_pud_range(p4d_t p4d, unsigned long addr, unsigned long end,
Ira Weinyb798bec2019-05-13 17:17:07 -07002216 unsigned int flags, struct page **pages, int *nr)
Steve Capper2667f502014-10-09 15:29:14 -07002217{
2218 unsigned long next;
2219 pud_t *pudp;
2220
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +03002221 pudp = pud_offset(&p4d, addr);
Steve Capper2667f502014-10-09 15:29:14 -07002222 do {
Christian Borntraegere37c6982014-12-07 21:41:33 +01002223 pud_t pud = READ_ONCE(*pudp);
Steve Capper2667f502014-10-09 15:29:14 -07002224
2225 next = pud_addr_end(addr, end);
Qiujun Huang154945202020-01-30 22:12:10 -08002226 if (unlikely(!pud_present(pud)))
Steve Capper2667f502014-10-09 15:29:14 -07002227 return 0;
Aneesh Kumar K.Vf30c59e2014-11-05 21:57:40 +05302228 if (unlikely(pud_huge(pud))) {
Ira Weinyb798bec2019-05-13 17:17:07 -07002229 if (!gup_huge_pud(pud, pudp, addr, next, flags,
Aneesh Kumar K.Vf30c59e2014-11-05 21:57:40 +05302230 pages, nr))
2231 return 0;
2232 } else if (unlikely(is_hugepd(__hugepd(pud_val(pud))))) {
2233 if (!gup_huge_pd(__hugepd(pud_val(pud)), addr,
Ira Weinyb798bec2019-05-13 17:17:07 -07002234 PUD_SHIFT, next, flags, pages, nr))
Steve Capper2667f502014-10-09 15:29:14 -07002235 return 0;
Ira Weinyb798bec2019-05-13 17:17:07 -07002236 } else if (!gup_pmd_range(pud, addr, next, flags, pages, nr))
Steve Capper2667f502014-10-09 15:29:14 -07002237 return 0;
2238 } while (pudp++, addr = next, addr != end);
2239
2240 return 1;
2241}
2242
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +03002243static int gup_p4d_range(pgd_t pgd, unsigned long addr, unsigned long end,
Ira Weinyb798bec2019-05-13 17:17:07 -07002244 unsigned int flags, struct page **pages, int *nr)
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +03002245{
2246 unsigned long next;
2247 p4d_t *p4dp;
2248
2249 p4dp = p4d_offset(&pgd, addr);
2250 do {
2251 p4d_t p4d = READ_ONCE(*p4dp);
2252
2253 next = p4d_addr_end(addr, end);
2254 if (p4d_none(p4d))
2255 return 0;
2256 BUILD_BUG_ON(p4d_huge(p4d));
2257 if (unlikely(is_hugepd(__hugepd(p4d_val(p4d))))) {
2258 if (!gup_huge_pd(__hugepd(p4d_val(p4d)), addr,
Ira Weinyb798bec2019-05-13 17:17:07 -07002259 P4D_SHIFT, next, flags, pages, nr))
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +03002260 return 0;
Ira Weinyb798bec2019-05-13 17:17:07 -07002261 } else if (!gup_pud_range(p4d, addr, next, flags, pages, nr))
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +03002262 return 0;
2263 } while (p4dp++, addr = next, addr != end);
2264
2265 return 1;
2266}
2267
Kirill A. Shutemov5b65c4672017-09-09 00:56:03 +03002268static void gup_pgd_range(unsigned long addr, unsigned long end,
Ira Weinyb798bec2019-05-13 17:17:07 -07002269 unsigned int flags, struct page **pages, int *nr)
Kirill A. Shutemov5b65c4672017-09-09 00:56:03 +03002270{
2271 unsigned long next;
2272 pgd_t *pgdp;
2273
2274 pgdp = pgd_offset(current->mm, addr);
2275 do {
2276 pgd_t pgd = READ_ONCE(*pgdp);
2277
2278 next = pgd_addr_end(addr, end);
2279 if (pgd_none(pgd))
2280 return;
2281 if (unlikely(pgd_huge(pgd))) {
Ira Weinyb798bec2019-05-13 17:17:07 -07002282 if (!gup_huge_pgd(pgd, pgdp, addr, next, flags,
Kirill A. Shutemov5b65c4672017-09-09 00:56:03 +03002283 pages, nr))
2284 return;
2285 } else if (unlikely(is_hugepd(__hugepd(pgd_val(pgd))))) {
2286 if (!gup_huge_pd(__hugepd(pgd_val(pgd)), addr,
Ira Weinyb798bec2019-05-13 17:17:07 -07002287 PGDIR_SHIFT, next, flags, pages, nr))
Kirill A. Shutemov5b65c4672017-09-09 00:56:03 +03002288 return;
Ira Weinyb798bec2019-05-13 17:17:07 -07002289 } else if (!gup_p4d_range(pgd, addr, next, flags, pages, nr))
Kirill A. Shutemov5b65c4672017-09-09 00:56:03 +03002290 return;
2291 } while (pgdp++, addr = next, addr != end);
2292}
Christoph Hellwig050a9ad2019-07-11 20:57:21 -07002293#else
2294static inline void gup_pgd_range(unsigned long addr, unsigned long end,
2295 unsigned int flags, struct page **pages, int *nr)
2296{
2297}
2298#endif /* CONFIG_HAVE_FAST_GUP */
Kirill A. Shutemov5b65c4672017-09-09 00:56:03 +03002299
2300#ifndef gup_fast_permitted
2301/*
2302 * Check if it's allowed to use __get_user_pages_fast() for the range, or
2303 * we need to fall back to the slow version:
2304 */
Christoph Hellwig26f4c322019-07-11 20:56:45 -07002305static bool gup_fast_permitted(unsigned long start, unsigned long end)
Kirill A. Shutemov5b65c4672017-09-09 00:56:03 +03002306{
Christoph Hellwig26f4c322019-07-11 20:56:45 -07002307 return true;
Kirill A. Shutemov5b65c4672017-09-09 00:56:03 +03002308}
2309#endif
2310
Steve Capper2667f502014-10-09 15:29:14 -07002311/*
2312 * Like get_user_pages_fast() except it's IRQ-safe in that it won't fall back to
Michael S. Tsirkind0811072018-04-13 15:35:23 -07002313 * the regular GUP.
2314 * Note a difference with get_user_pages_fast: this always returns the
2315 * number of pages pinned, 0 if no pages were pinned.
Christoph Hellwig050a9ad2019-07-11 20:57:21 -07002316 *
2317 * If the architecture does not support this function, simply return with no
2318 * pages pinned.
Steve Capper2667f502014-10-09 15:29:14 -07002319 */
2320int __get_user_pages_fast(unsigned long start, int nr_pages, int write,
2321 struct page **pages)
2322{
Wei Yangd4faa402018-10-26 15:07:55 -07002323 unsigned long len, end;
Kirill A. Shutemov5b65c4672017-09-09 00:56:03 +03002324 unsigned long flags;
Steve Capper2667f502014-10-09 15:29:14 -07002325 int nr = 0;
2326
Christoph Hellwigf455c8542019-07-11 20:56:41 -07002327 start = untagged_addr(start) & PAGE_MASK;
Steve Capper2667f502014-10-09 15:29:14 -07002328 len = (unsigned long) nr_pages << PAGE_SHIFT;
2329 end = start + len;
2330
Christoph Hellwig26f4c322019-07-11 20:56:45 -07002331 if (end <= start)
2332 return 0;
Linus Torvalds96d4f262019-01-03 18:57:57 -08002333 if (unlikely(!access_ok((void __user *)start, len)))
Steve Capper2667f502014-10-09 15:29:14 -07002334 return 0;
2335
2336 /*
2337 * Disable interrupts. We use the nested form as we can already have
2338 * interrupts disabled by get_futex_key.
2339 *
2340 * With interrupts disabled, we block page table pages from being
Fengguang Wu2ebe8222018-10-30 15:10:51 -07002341 * freed from under us. See struct mmu_table_batch comments in
2342 * include/asm-generic/tlb.h for more details.
Steve Capper2667f502014-10-09 15:29:14 -07002343 *
2344 * We do not adopt an rcu_read_lock(.) here as we also want to
2345 * block IPIs that come from THPs splitting.
2346 */
2347
Christoph Hellwig050a9ad2019-07-11 20:57:21 -07002348 if (IS_ENABLED(CONFIG_HAVE_FAST_GUP) &&
2349 gup_fast_permitted(start, end)) {
Kirill A. Shutemov5b65c4672017-09-09 00:56:03 +03002350 local_irq_save(flags);
Ira Weinyb798bec2019-05-13 17:17:07 -07002351 gup_pgd_range(start, end, write ? FOLL_WRITE : 0, pages, &nr);
Kirill A. Shutemov5b65c4672017-09-09 00:56:03 +03002352 local_irq_restore(flags);
2353 }
Steve Capper2667f502014-10-09 15:29:14 -07002354
2355 return nr;
2356}
Christoph Hellwig050a9ad2019-07-11 20:57:21 -07002357EXPORT_SYMBOL_GPL(__get_user_pages_fast);
Steve Capper2667f502014-10-09 15:29:14 -07002358
Ira Weiny7af75562019-05-13 17:17:14 -07002359static int __gup_longterm_unlocked(unsigned long start, int nr_pages,
2360 unsigned int gup_flags, struct page **pages)
2361{
2362 int ret;
2363
2364 /*
2365 * FIXME: FOLL_LONGTERM does not work with
2366 * get_user_pages_unlocked() (see comments in that function)
2367 */
2368 if (gup_flags & FOLL_LONGTERM) {
2369 down_read(&current->mm->mmap_sem);
2370 ret = __gup_longterm_locked(current, current->mm,
2371 start, nr_pages,
2372 pages, NULL, gup_flags);
2373 up_read(&current->mm->mmap_sem);
2374 } else {
2375 ret = get_user_pages_unlocked(start, nr_pages,
2376 pages, gup_flags);
2377 }
2378
2379 return ret;
2380}
2381
Steve Capper2667f502014-10-09 15:29:14 -07002382/**
2383 * get_user_pages_fast() - pin user pages in memory
2384 * @start: starting user address
2385 * @nr_pages: number of pages from start to pin
Ira Weiny73b01402019-05-13 17:17:11 -07002386 * @gup_flags: flags modifying pin behaviour
Steve Capper2667f502014-10-09 15:29:14 -07002387 * @pages: array that receives pointers to the pages pinned.
2388 * Should be at least nr_pages long.
2389 *
2390 * Attempt to pin user pages in memory without taking mm->mmap_sem.
2391 * If not successful, it will fall back to taking the lock and
2392 * calling get_user_pages().
2393 *
2394 * Returns number of pages pinned. This may be fewer than the number
2395 * requested. If nr_pages is 0 or negative, returns 0. If no pages
2396 * were pinned, returns -errno.
2397 */
Ira Weiny73b01402019-05-13 17:17:11 -07002398int get_user_pages_fast(unsigned long start, int nr_pages,
2399 unsigned int gup_flags, struct page **pages)
Steve Capper2667f502014-10-09 15:29:14 -07002400{
Kirill A. Shutemov5b65c4672017-09-09 00:56:03 +03002401 unsigned long addr, len, end;
Kirill A. Shutemov73e10a62017-03-16 18:26:54 +03002402 int nr = 0, ret = 0;
Steve Capper2667f502014-10-09 15:29:14 -07002403
Christoph Hellwig817be122019-07-11 20:57:25 -07002404 if (WARN_ON_ONCE(gup_flags & ~(FOLL_WRITE | FOLL_LONGTERM)))
2405 return -EINVAL;
2406
Christoph Hellwigf455c8542019-07-11 20:56:41 -07002407 start = untagged_addr(start) & PAGE_MASK;
Kirill A. Shutemov5b65c4672017-09-09 00:56:03 +03002408 addr = start;
2409 len = (unsigned long) nr_pages << PAGE_SHIFT;
2410 end = start + len;
2411
Christoph Hellwig26f4c322019-07-11 20:56:45 -07002412 if (end <= start)
Michael S. Tsirkinc61611f2018-04-13 15:35:20 -07002413 return 0;
Linus Torvalds96d4f262019-01-03 18:57:57 -08002414 if (unlikely(!access_ok((void __user *)start, len)))
Michael S. Tsirkinc61611f2018-04-13 15:35:20 -07002415 return -EFAULT;
Kirill A. Shutemov73e10a62017-03-16 18:26:54 +03002416
Christoph Hellwig050a9ad2019-07-11 20:57:21 -07002417 if (IS_ENABLED(CONFIG_HAVE_FAST_GUP) &&
2418 gup_fast_permitted(start, end)) {
Kirill A. Shutemov5b65c4672017-09-09 00:56:03 +03002419 local_irq_disable();
Ira Weiny73b01402019-05-13 17:17:11 -07002420 gup_pgd_range(addr, end, gup_flags, pages, &nr);
Kirill A. Shutemov5b65c4672017-09-09 00:56:03 +03002421 local_irq_enable();
Kirill A. Shutemov73e10a62017-03-16 18:26:54 +03002422 ret = nr;
2423 }
Steve Capper2667f502014-10-09 15:29:14 -07002424
2425 if (nr < nr_pages) {
2426 /* Try to get the remaining pages with get_user_pages */
2427 start += nr << PAGE_SHIFT;
2428 pages += nr;
2429
Ira Weiny7af75562019-05-13 17:17:14 -07002430 ret = __gup_longterm_unlocked(start, nr_pages - nr,
2431 gup_flags, pages);
Steve Capper2667f502014-10-09 15:29:14 -07002432
2433 /* Have to be a bit careful with return values */
2434 if (nr > 0) {
2435 if (ret < 0)
2436 ret = nr;
2437 else
2438 ret += nr;
2439 }
2440 }
2441
2442 return ret;
2443}
Christoph Hellwig050a9ad2019-07-11 20:57:21 -07002444EXPORT_SYMBOL_GPL(get_user_pages_fast);