]>
Commit | Line | Data |
---|---|---|
457c8996 | 1 | // SPDX-License-Identifier: GPL-2.0-only |
4bbd4c77 KS |
2 | #include <linux/kernel.h> |
3 | #include <linux/errno.h> | |
4 | #include <linux/err.h> | |
5 | #include <linux/spinlock.h> | |
6 | ||
4bbd4c77 | 7 | #include <linux/mm.h> |
3565fce3 | 8 | #include <linux/memremap.h> |
4bbd4c77 KS |
9 | #include <linux/pagemap.h> |
10 | #include <linux/rmap.h> | |
11 | #include <linux/swap.h> | |
12 | #include <linux/swapops.h> | |
13 | ||
174cd4b1 | 14 | #include <linux/sched/signal.h> |
2667f50e | 15 | #include <linux/rwsem.h> |
f30c59e9 | 16 | #include <linux/hugetlb.h> |
9a4e9f3b AK |
17 | #include <linux/migrate.h> |
18 | #include <linux/mm_inline.h> | |
19 | #include <linux/sched/mm.h> | |
1027e443 | 20 | |
33a709b2 | 21 | #include <asm/mmu_context.h> |
2667f50e | 22 | #include <asm/pgtable.h> |
1027e443 | 23 | #include <asm/tlbflush.h> |
2667f50e | 24 | |
4bbd4c77 KS |
25 | #include "internal.h" |
26 | ||
df06b37f KB |
27 | struct follow_page_context { |
28 | struct dev_pagemap *pgmap; | |
29 | unsigned int page_mask; | |
30 | }; | |
31 | ||
47e29d32 JH |
32 | static void hpage_pincount_add(struct page *page, int refs) |
33 | { | |
34 | VM_BUG_ON_PAGE(!hpage_pincount_available(page), page); | |
35 | VM_BUG_ON_PAGE(page != compound_head(page), page); | |
36 | ||
37 | atomic_add(refs, compound_pincount_ptr(page)); | |
38 | } | |
39 | ||
40 | static void hpage_pincount_sub(struct page *page, int refs) | |
41 | { | |
42 | VM_BUG_ON_PAGE(!hpage_pincount_available(page), page); | |
43 | VM_BUG_ON_PAGE(page != compound_head(page), page); | |
44 | ||
45 | atomic_sub(refs, compound_pincount_ptr(page)); | |
46 | } | |
47 | ||
a707cdd5 JH |
48 | /* |
49 | * Return the compound head page with ref appropriately incremented, | |
50 | * or NULL if that failed. | |
51 | */ | |
52 | static inline struct page *try_get_compound_head(struct page *page, int refs) | |
53 | { | |
54 | struct page *head = compound_head(page); | |
55 | ||
56 | if (WARN_ON_ONCE(page_ref_count(head) < 0)) | |
57 | return NULL; | |
58 | if (unlikely(!page_cache_add_speculative(head, refs))) | |
59 | return NULL; | |
60 | return head; | |
61 | } | |
62 | ||
3faa52c0 JH |
63 | /* |
64 | * try_grab_compound_head() - attempt to elevate a page's refcount, by a | |
65 | * flags-dependent amount. | |
66 | * | |
67 | * "grab" names in this file mean, "look at flags to decide whether to use | |
68 | * FOLL_PIN or FOLL_GET behavior, when incrementing the page's refcount. | |
69 | * | |
70 | * Either FOLL_PIN or FOLL_GET (or neither) must be set, but not both at the | |
71 | * same time. (That's true throughout the get_user_pages*() and | |
72 | * pin_user_pages*() APIs.) Cases: | |
73 | * | |
74 | * FOLL_GET: page's refcount will be incremented by 1. | |
75 | * FOLL_PIN: page's refcount will be incremented by GUP_PIN_COUNTING_BIAS. | |
76 | * | |
77 | * Return: head page (with refcount appropriately incremented) for success, or | |
78 | * NULL upon failure. If neither FOLL_GET nor FOLL_PIN was set, that's | |
79 | * considered failure, and furthermore, a likely bug in the caller, so a warning | |
80 | * is also emitted. | |
81 | */ | |
82 | static __maybe_unused struct page *try_grab_compound_head(struct page *page, | |
83 | int refs, | |
84 | unsigned int flags) | |
85 | { | |
86 | if (flags & FOLL_GET) | |
87 | return try_get_compound_head(page, refs); | |
88 | else if (flags & FOLL_PIN) { | |
47e29d32 JH |
89 | /* |
90 | * When pinning a compound page of order > 1 (which is what | |
91 | * hpage_pincount_available() checks for), use an exact count to | |
92 | * track it, via hpage_pincount_add/_sub(). | |
93 | * | |
94 | * However, be sure to *also* increment the normal page refcount | |
95 | * field at least once, so that the page really is pinned. | |
96 | */ | |
97 | if (!hpage_pincount_available(page)) | |
98 | refs *= GUP_PIN_COUNTING_BIAS; | |
99 | ||
100 | page = try_get_compound_head(page, refs); | |
101 | if (!page) | |
102 | return NULL; | |
103 | ||
104 | if (hpage_pincount_available(page)) | |
105 | hpage_pincount_add(page, refs); | |
106 | ||
107 | return page; | |
3faa52c0 JH |
108 | } |
109 | ||
110 | WARN_ON_ONCE(1); | |
111 | return NULL; | |
112 | } | |
113 | ||
114 | /** | |
115 | * try_grab_page() - elevate a page's refcount by a flag-dependent amount | |
116 | * | |
117 | * This might not do anything at all, depending on the flags argument. | |
118 | * | |
119 | * "grab" names in this file mean, "look at flags to decide whether to use | |
120 | * FOLL_PIN or FOLL_GET behavior, when incrementing the page's refcount. | |
121 | * | |
122 | * @page: pointer to page to be grabbed | |
123 | * @flags: gup flags: these are the FOLL_* flag values. | |
124 | * | |
125 | * Either FOLL_PIN or FOLL_GET (or neither) may be set, but not both at the same | |
126 | * time. Cases: | |
127 | * | |
128 | * FOLL_GET: page's refcount will be incremented by 1. | |
129 | * FOLL_PIN: page's refcount will be incremented by GUP_PIN_COUNTING_BIAS. | |
130 | * | |
131 | * Return: true for success, or if no action was required (if neither FOLL_PIN | |
132 | * nor FOLL_GET was set, nothing is done). False for failure: FOLL_GET or | |
133 | * FOLL_PIN was set, but the page could not be grabbed. | |
134 | */ | |
135 | bool __must_check try_grab_page(struct page *page, unsigned int flags) | |
136 | { | |
137 | WARN_ON_ONCE((flags & (FOLL_GET | FOLL_PIN)) == (FOLL_GET | FOLL_PIN)); | |
138 | ||
139 | if (flags & FOLL_GET) | |
140 | return try_get_page(page); | |
141 | else if (flags & FOLL_PIN) { | |
47e29d32 JH |
142 | int refs = 1; |
143 | ||
3faa52c0 JH |
144 | page = compound_head(page); |
145 | ||
146 | if (WARN_ON_ONCE(page_ref_count(page) <= 0)) | |
147 | return false; | |
148 | ||
47e29d32 JH |
149 | if (hpage_pincount_available(page)) |
150 | hpage_pincount_add(page, 1); | |
151 | else | |
152 | refs = GUP_PIN_COUNTING_BIAS; | |
153 | ||
154 | /* | |
155 | * Similar to try_grab_compound_head(): even if using the | |
156 | * hpage_pincount_add/_sub() routines, be sure to | |
157 | * *also* increment the normal page refcount field at least | |
158 | * once, so that the page really is pinned. | |
159 | */ | |
160 | page_ref_add(page, refs); | |
3faa52c0 JH |
161 | } |
162 | ||
163 | return true; | |
164 | } | |
165 | ||
166 | #ifdef CONFIG_DEV_PAGEMAP_OPS | |
167 | static bool __unpin_devmap_managed_user_page(struct page *page) | |
168 | { | |
47e29d32 | 169 | int count, refs = 1; |
3faa52c0 JH |
170 | |
171 | if (!page_is_devmap_managed(page)) | |
172 | return false; | |
173 | ||
47e29d32 JH |
174 | if (hpage_pincount_available(page)) |
175 | hpage_pincount_sub(page, 1); | |
176 | else | |
177 | refs = GUP_PIN_COUNTING_BIAS; | |
178 | ||
179 | count = page_ref_sub_return(page, refs); | |
3faa52c0 JH |
180 | |
181 | /* | |
182 | * devmap page refcounts are 1-based, rather than 0-based: if | |
183 | * refcount is 1, then the page is free and the refcount is | |
184 | * stable because nobody holds a reference on the page. | |
185 | */ | |
186 | if (count == 1) | |
187 | free_devmap_managed_page(page); | |
188 | else if (!count) | |
189 | __put_page(page); | |
190 | ||
191 | return true; | |
192 | } | |
193 | #else | |
194 | static bool __unpin_devmap_managed_user_page(struct page *page) | |
195 | { | |
196 | return false; | |
197 | } | |
198 | #endif /* CONFIG_DEV_PAGEMAP_OPS */ | |
199 | ||
200 | /** | |
201 | * unpin_user_page() - release a dma-pinned page | |
202 | * @page: pointer to page to be released | |
203 | * | |
204 | * Pages that were pinned via pin_user_pages*() must be released via either | |
205 | * unpin_user_page(), or one of the unpin_user_pages*() routines. This is so | |
206 | * that such pages can be separately tracked and uniquely handled. In | |
207 | * particular, interactions with RDMA and filesystems need special handling. | |
208 | */ | |
209 | void unpin_user_page(struct page *page) | |
210 | { | |
47e29d32 JH |
211 | int refs = 1; |
212 | ||
3faa52c0 JH |
213 | page = compound_head(page); |
214 | ||
215 | /* | |
216 | * For devmap managed pages we need to catch refcount transition from | |
217 | * GUP_PIN_COUNTING_BIAS to 1, when refcount reach one it means the | |
218 | * page is free and we need to inform the device driver through | |
219 | * callback. See include/linux/memremap.h and HMM for details. | |
220 | */ | |
221 | if (__unpin_devmap_managed_user_page(page)) | |
222 | return; | |
223 | ||
47e29d32 JH |
224 | if (hpage_pincount_available(page)) |
225 | hpage_pincount_sub(page, 1); | |
226 | else | |
227 | refs = GUP_PIN_COUNTING_BIAS; | |
228 | ||
229 | if (page_ref_sub_and_test(page, refs)) | |
3faa52c0 JH |
230 | __put_page(page); |
231 | } | |
232 | EXPORT_SYMBOL(unpin_user_page); | |
233 | ||
fc1d8e7c | 234 | /** |
f1f6a7dd | 235 | * unpin_user_pages_dirty_lock() - release and optionally dirty gup-pinned pages |
2d15eb31 | 236 | * @pages: array of pages to be maybe marked dirty, and definitely released. |
fc1d8e7c | 237 | * @npages: number of pages in the @pages array. |
2d15eb31 | 238 | * @make_dirty: whether to mark the pages dirty |
fc1d8e7c JH |
239 | * |
240 | * "gup-pinned page" refers to a page that has had one of the get_user_pages() | |
241 | * variants called on that page. | |
242 | * | |
243 | * For each page in the @pages array, make that page (or its head page, if a | |
2d15eb31 | 244 | * compound page) dirty, if @make_dirty is true, and if the page was previously |
f1f6a7dd JH |
245 | * listed as clean. In any case, releases all pages using unpin_user_page(), |
246 | * possibly via unpin_user_pages(), for the non-dirty case. | |
fc1d8e7c | 247 | * |
f1f6a7dd | 248 | * Please see the unpin_user_page() documentation for details. |
fc1d8e7c | 249 | * |
2d15eb31 AM |
250 | * set_page_dirty_lock() is used internally. If instead, set_page_dirty() is |
251 | * required, then the caller should a) verify that this is really correct, | |
252 | * because _lock() is usually required, and b) hand code it: | |
f1f6a7dd | 253 | * set_page_dirty_lock(), unpin_user_page(). |
fc1d8e7c JH |
254 | * |
255 | */ | |
f1f6a7dd JH |
256 | void unpin_user_pages_dirty_lock(struct page **pages, unsigned long npages, |
257 | bool make_dirty) | |
fc1d8e7c | 258 | { |
2d15eb31 | 259 | unsigned long index; |
fc1d8e7c | 260 | |
2d15eb31 AM |
261 | /* |
262 | * TODO: this can be optimized for huge pages: if a series of pages is | |
263 | * physically contiguous and part of the same compound page, then a | |
264 | * single operation to the head page should suffice. | |
265 | */ | |
266 | ||
267 | if (!make_dirty) { | |
f1f6a7dd | 268 | unpin_user_pages(pages, npages); |
2d15eb31 AM |
269 | return; |
270 | } | |
271 | ||
272 | for (index = 0; index < npages; index++) { | |
273 | struct page *page = compound_head(pages[index]); | |
274 | /* | |
275 | * Checking PageDirty at this point may race with | |
276 | * clear_page_dirty_for_io(), but that's OK. Two key | |
277 | * cases: | |
278 | * | |
279 | * 1) This code sees the page as already dirty, so it | |
280 | * skips the call to set_page_dirty(). That could happen | |
281 | * because clear_page_dirty_for_io() called | |
282 | * page_mkclean(), followed by set_page_dirty(). | |
283 | * However, now the page is going to get written back, | |
284 | * which meets the original intention of setting it | |
285 | * dirty, so all is well: clear_page_dirty_for_io() goes | |
286 | * on to call TestClearPageDirty(), and write the page | |
287 | * back. | |
288 | * | |
289 | * 2) This code sees the page as clean, so it calls | |
290 | * set_page_dirty(). The page stays dirty, despite being | |
291 | * written back, so it gets written back again in the | |
292 | * next writeback cycle. This is harmless. | |
293 | */ | |
294 | if (!PageDirty(page)) | |
295 | set_page_dirty_lock(page); | |
f1f6a7dd | 296 | unpin_user_page(page); |
2d15eb31 | 297 | } |
fc1d8e7c | 298 | } |
f1f6a7dd | 299 | EXPORT_SYMBOL(unpin_user_pages_dirty_lock); |
fc1d8e7c JH |
300 | |
301 | /** | |
f1f6a7dd | 302 | * unpin_user_pages() - release an array of gup-pinned pages. |
fc1d8e7c JH |
303 | * @pages: array of pages to be marked dirty and released. |
304 | * @npages: number of pages in the @pages array. | |
305 | * | |
f1f6a7dd | 306 | * For each page in the @pages array, release the page using unpin_user_page(). |
fc1d8e7c | 307 | * |
f1f6a7dd | 308 | * Please see the unpin_user_page() documentation for details. |
fc1d8e7c | 309 | */ |
f1f6a7dd | 310 | void unpin_user_pages(struct page **pages, unsigned long npages) |
fc1d8e7c JH |
311 | { |
312 | unsigned long index; | |
313 | ||
314 | /* | |
315 | * TODO: this can be optimized for huge pages: if a series of pages is | |
316 | * physically contiguous and part of the same compound page, then a | |
317 | * single operation to the head page should suffice. | |
318 | */ | |
319 | for (index = 0; index < npages; index++) | |
f1f6a7dd | 320 | unpin_user_page(pages[index]); |
fc1d8e7c | 321 | } |
f1f6a7dd | 322 | EXPORT_SYMBOL(unpin_user_pages); |
fc1d8e7c | 323 | |
050a9adc | 324 | #ifdef CONFIG_MMU |
69e68b4f KS |
325 | static struct page *no_page_table(struct vm_area_struct *vma, |
326 | unsigned int flags) | |
4bbd4c77 | 327 | { |
69e68b4f KS |
328 | /* |
329 | * When core dumping an enormous anonymous area that nobody | |
330 | * has touched so far, we don't want to allocate unnecessary pages or | |
331 | * page tables. Return error instead of NULL to skip handle_mm_fault, | |
332 | * then get_dump_page() will return NULL to leave a hole in the dump. | |
333 | * But we can only make this optimization where a hole would surely | |
334 | * be zero-filled if handle_mm_fault() actually did handle it. | |
335 | */ | |
336 | if ((flags & FOLL_DUMP) && (!vma->vm_ops || !vma->vm_ops->fault)) | |
337 | return ERR_PTR(-EFAULT); | |
338 | return NULL; | |
339 | } | |
4bbd4c77 | 340 | |
1027e443 KS |
341 | static int follow_pfn_pte(struct vm_area_struct *vma, unsigned long address, |
342 | pte_t *pte, unsigned int flags) | |
343 | { | |
344 | /* No page to get reference */ | |
345 | if (flags & FOLL_GET) | |
346 | return -EFAULT; | |
347 | ||
348 | if (flags & FOLL_TOUCH) { | |
349 | pte_t entry = *pte; | |
350 | ||
351 | if (flags & FOLL_WRITE) | |
352 | entry = pte_mkdirty(entry); | |
353 | entry = pte_mkyoung(entry); | |
354 | ||
355 | if (!pte_same(*pte, entry)) { | |
356 | set_pte_at(vma->vm_mm, address, pte, entry); | |
357 | update_mmu_cache(vma, address, pte); | |
358 | } | |
359 | } | |
360 | ||
361 | /* Proper page table entry exists, but no corresponding struct page */ | |
362 | return -EEXIST; | |
363 | } | |
364 | ||
19be0eaf LT |
365 | /* |
366 | * FOLL_FORCE can write to even unwritable pte's, but only | |
367 | * after we've gone through a COW cycle and they are dirty. | |
368 | */ | |
369 | static inline bool can_follow_write_pte(pte_t pte, unsigned int flags) | |
370 | { | |
f6f37321 | 371 | return pte_write(pte) || |
19be0eaf LT |
372 | ((flags & FOLL_FORCE) && (flags & FOLL_COW) && pte_dirty(pte)); |
373 | } | |
374 | ||
69e68b4f | 375 | static struct page *follow_page_pte(struct vm_area_struct *vma, |
df06b37f KB |
376 | unsigned long address, pmd_t *pmd, unsigned int flags, |
377 | struct dev_pagemap **pgmap) | |
69e68b4f KS |
378 | { |
379 | struct mm_struct *mm = vma->vm_mm; | |
380 | struct page *page; | |
381 | spinlock_t *ptl; | |
382 | pte_t *ptep, pte; | |
4bbd4c77 | 383 | |
eddb1c22 JH |
384 | /* FOLL_GET and FOLL_PIN are mutually exclusive. */ |
385 | if (WARN_ON_ONCE((flags & (FOLL_PIN | FOLL_GET)) == | |
386 | (FOLL_PIN | FOLL_GET))) | |
387 | return ERR_PTR(-EINVAL); | |
69e68b4f | 388 | retry: |
4bbd4c77 | 389 | if (unlikely(pmd_bad(*pmd))) |
69e68b4f | 390 | return no_page_table(vma, flags); |
4bbd4c77 KS |
391 | |
392 | ptep = pte_offset_map_lock(mm, pmd, address, &ptl); | |
4bbd4c77 KS |
393 | pte = *ptep; |
394 | if (!pte_present(pte)) { | |
395 | swp_entry_t entry; | |
396 | /* | |
397 | * KSM's break_ksm() relies upon recognizing a ksm page | |
398 | * even while it is being migrated, so for that case we | |
399 | * need migration_entry_wait(). | |
400 | */ | |
401 | if (likely(!(flags & FOLL_MIGRATION))) | |
402 | goto no_page; | |
0661a336 | 403 | if (pte_none(pte)) |
4bbd4c77 KS |
404 | goto no_page; |
405 | entry = pte_to_swp_entry(pte); | |
406 | if (!is_migration_entry(entry)) | |
407 | goto no_page; | |
408 | pte_unmap_unlock(ptep, ptl); | |
409 | migration_entry_wait(mm, pmd, address); | |
69e68b4f | 410 | goto retry; |
4bbd4c77 | 411 | } |
8a0516ed | 412 | if ((flags & FOLL_NUMA) && pte_protnone(pte)) |
4bbd4c77 | 413 | goto no_page; |
19be0eaf | 414 | if ((flags & FOLL_WRITE) && !can_follow_write_pte(pte, flags)) { |
69e68b4f KS |
415 | pte_unmap_unlock(ptep, ptl); |
416 | return NULL; | |
417 | } | |
4bbd4c77 KS |
418 | |
419 | page = vm_normal_page(vma, address, pte); | |
3faa52c0 | 420 | if (!page && pte_devmap(pte) && (flags & (FOLL_GET | FOLL_PIN))) { |
3565fce3 | 421 | /* |
3faa52c0 JH |
422 | * Only return device mapping pages in the FOLL_GET or FOLL_PIN |
423 | * case since they are only valid while holding the pgmap | |
424 | * reference. | |
3565fce3 | 425 | */ |
df06b37f KB |
426 | *pgmap = get_dev_pagemap(pte_pfn(pte), *pgmap); |
427 | if (*pgmap) | |
3565fce3 DW |
428 | page = pte_page(pte); |
429 | else | |
430 | goto no_page; | |
431 | } else if (unlikely(!page)) { | |
1027e443 KS |
432 | if (flags & FOLL_DUMP) { |
433 | /* Avoid special (like zero) pages in core dumps */ | |
434 | page = ERR_PTR(-EFAULT); | |
435 | goto out; | |
436 | } | |
437 | ||
438 | if (is_zero_pfn(pte_pfn(pte))) { | |
439 | page = pte_page(pte); | |
440 | } else { | |
441 | int ret; | |
442 | ||
443 | ret = follow_pfn_pte(vma, address, ptep, flags); | |
444 | page = ERR_PTR(ret); | |
445 | goto out; | |
446 | } | |
4bbd4c77 KS |
447 | } |
448 | ||
6742d293 KS |
449 | if (flags & FOLL_SPLIT && PageTransCompound(page)) { |
450 | int ret; | |
451 | get_page(page); | |
452 | pte_unmap_unlock(ptep, ptl); | |
453 | lock_page(page); | |
454 | ret = split_huge_page(page); | |
455 | unlock_page(page); | |
456 | put_page(page); | |
457 | if (ret) | |
458 | return ERR_PTR(ret); | |
459 | goto retry; | |
460 | } | |
461 | ||
3faa52c0 JH |
462 | /* try_grab_page() does nothing unless FOLL_GET or FOLL_PIN is set. */ |
463 | if (unlikely(!try_grab_page(page, flags))) { | |
464 | page = ERR_PTR(-ENOMEM); | |
465 | goto out; | |
8fde12ca | 466 | } |
4bbd4c77 KS |
467 | if (flags & FOLL_TOUCH) { |
468 | if ((flags & FOLL_WRITE) && | |
469 | !pte_dirty(pte) && !PageDirty(page)) | |
470 | set_page_dirty(page); | |
471 | /* | |
472 | * pte_mkyoung() would be more correct here, but atomic care | |
473 | * is needed to avoid losing the dirty bit: it is easier to use | |
474 | * mark_page_accessed(). | |
475 | */ | |
476 | mark_page_accessed(page); | |
477 | } | |
de60f5f1 | 478 | if ((flags & FOLL_MLOCK) && (vma->vm_flags & VM_LOCKED)) { |
e90309c9 KS |
479 | /* Do not mlock pte-mapped THP */ |
480 | if (PageTransCompound(page)) | |
481 | goto out; | |
482 | ||
4bbd4c77 KS |
483 | /* |
484 | * The preliminary mapping check is mainly to avoid the | |
485 | * pointless overhead of lock_page on the ZERO_PAGE | |
486 | * which might bounce very badly if there is contention. | |
487 | * | |
488 | * If the page is already locked, we don't need to | |
489 | * handle it now - vmscan will handle it later if and | |
490 | * when it attempts to reclaim the page. | |
491 | */ | |
492 | if (page->mapping && trylock_page(page)) { | |
493 | lru_add_drain(); /* push cached pages to LRU */ | |
494 | /* | |
495 | * Because we lock page here, and migration is | |
496 | * blocked by the pte's page reference, and we | |
497 | * know the page is still mapped, we don't even | |
498 | * need to check for file-cache page truncation. | |
499 | */ | |
500 | mlock_vma_page(page); | |
501 | unlock_page(page); | |
502 | } | |
503 | } | |
1027e443 | 504 | out: |
4bbd4c77 | 505 | pte_unmap_unlock(ptep, ptl); |
4bbd4c77 | 506 | return page; |
4bbd4c77 KS |
507 | no_page: |
508 | pte_unmap_unlock(ptep, ptl); | |
509 | if (!pte_none(pte)) | |
69e68b4f KS |
510 | return NULL; |
511 | return no_page_table(vma, flags); | |
512 | } | |
513 | ||
080dbb61 AK |
514 | static struct page *follow_pmd_mask(struct vm_area_struct *vma, |
515 | unsigned long address, pud_t *pudp, | |
df06b37f KB |
516 | unsigned int flags, |
517 | struct follow_page_context *ctx) | |
69e68b4f | 518 | { |
68827280 | 519 | pmd_t *pmd, pmdval; |
69e68b4f KS |
520 | spinlock_t *ptl; |
521 | struct page *page; | |
522 | struct mm_struct *mm = vma->vm_mm; | |
523 | ||
080dbb61 | 524 | pmd = pmd_offset(pudp, address); |
68827280 HY |
525 | /* |
526 | * The READ_ONCE() will stabilize the pmdval in a register or | |
527 | * on the stack so that it will stop changing under the code. | |
528 | */ | |
529 | pmdval = READ_ONCE(*pmd); | |
530 | if (pmd_none(pmdval)) | |
69e68b4f | 531 | return no_page_table(vma, flags); |
be9d3045 | 532 | if (pmd_huge(pmdval) && is_vm_hugetlb_page(vma)) { |
e66f17ff NH |
533 | page = follow_huge_pmd(mm, address, pmd, flags); |
534 | if (page) | |
535 | return page; | |
536 | return no_page_table(vma, flags); | |
69e68b4f | 537 | } |
68827280 | 538 | if (is_hugepd(__hugepd(pmd_val(pmdval)))) { |
4dc71451 | 539 | page = follow_huge_pd(vma, address, |
68827280 | 540 | __hugepd(pmd_val(pmdval)), flags, |
4dc71451 AK |
541 | PMD_SHIFT); |
542 | if (page) | |
543 | return page; | |
544 | return no_page_table(vma, flags); | |
545 | } | |
84c3fc4e | 546 | retry: |
68827280 | 547 | if (!pmd_present(pmdval)) { |
84c3fc4e ZY |
548 | if (likely(!(flags & FOLL_MIGRATION))) |
549 | return no_page_table(vma, flags); | |
550 | VM_BUG_ON(thp_migration_supported() && | |
68827280 HY |
551 | !is_pmd_migration_entry(pmdval)); |
552 | if (is_pmd_migration_entry(pmdval)) | |
84c3fc4e | 553 | pmd_migration_entry_wait(mm, pmd); |
68827280 HY |
554 | pmdval = READ_ONCE(*pmd); |
555 | /* | |
556 | * MADV_DONTNEED may convert the pmd to null because | |
557 | * mmap_sem is held in read mode | |
558 | */ | |
559 | if (pmd_none(pmdval)) | |
560 | return no_page_table(vma, flags); | |
84c3fc4e ZY |
561 | goto retry; |
562 | } | |
68827280 | 563 | if (pmd_devmap(pmdval)) { |
3565fce3 | 564 | ptl = pmd_lock(mm, pmd); |
df06b37f | 565 | page = follow_devmap_pmd(vma, address, pmd, flags, &ctx->pgmap); |
3565fce3 DW |
566 | spin_unlock(ptl); |
567 | if (page) | |
568 | return page; | |
569 | } | |
68827280 | 570 | if (likely(!pmd_trans_huge(pmdval))) |
df06b37f | 571 | return follow_page_pte(vma, address, pmd, flags, &ctx->pgmap); |
6742d293 | 572 | |
68827280 | 573 | if ((flags & FOLL_NUMA) && pmd_protnone(pmdval)) |
db08f203 AK |
574 | return no_page_table(vma, flags); |
575 | ||
84c3fc4e | 576 | retry_locked: |
6742d293 | 577 | ptl = pmd_lock(mm, pmd); |
68827280 HY |
578 | if (unlikely(pmd_none(*pmd))) { |
579 | spin_unlock(ptl); | |
580 | return no_page_table(vma, flags); | |
581 | } | |
84c3fc4e ZY |
582 | if (unlikely(!pmd_present(*pmd))) { |
583 | spin_unlock(ptl); | |
584 | if (likely(!(flags & FOLL_MIGRATION))) | |
585 | return no_page_table(vma, flags); | |
586 | pmd_migration_entry_wait(mm, pmd); | |
587 | goto retry_locked; | |
588 | } | |
6742d293 KS |
589 | if (unlikely(!pmd_trans_huge(*pmd))) { |
590 | spin_unlock(ptl); | |
df06b37f | 591 | return follow_page_pte(vma, address, pmd, flags, &ctx->pgmap); |
6742d293 | 592 | } |
bfe7b00d | 593 | if (flags & (FOLL_SPLIT | FOLL_SPLIT_PMD)) { |
6742d293 KS |
594 | int ret; |
595 | page = pmd_page(*pmd); | |
596 | if (is_huge_zero_page(page)) { | |
597 | spin_unlock(ptl); | |
598 | ret = 0; | |
78ddc534 | 599 | split_huge_pmd(vma, pmd, address); |
337d9abf NH |
600 | if (pmd_trans_unstable(pmd)) |
601 | ret = -EBUSY; | |
bfe7b00d | 602 | } else if (flags & FOLL_SPLIT) { |
8fde12ca LT |
603 | if (unlikely(!try_get_page(page))) { |
604 | spin_unlock(ptl); | |
605 | return ERR_PTR(-ENOMEM); | |
606 | } | |
69e68b4f | 607 | spin_unlock(ptl); |
6742d293 KS |
608 | lock_page(page); |
609 | ret = split_huge_page(page); | |
610 | unlock_page(page); | |
611 | put_page(page); | |
baa355fd KS |
612 | if (pmd_none(*pmd)) |
613 | return no_page_table(vma, flags); | |
bfe7b00d SL |
614 | } else { /* flags & FOLL_SPLIT_PMD */ |
615 | spin_unlock(ptl); | |
616 | split_huge_pmd(vma, pmd, address); | |
617 | ret = pte_alloc(mm, pmd) ? -ENOMEM : 0; | |
6742d293 KS |
618 | } |
619 | ||
620 | return ret ? ERR_PTR(ret) : | |
df06b37f | 621 | follow_page_pte(vma, address, pmd, flags, &ctx->pgmap); |
69e68b4f | 622 | } |
6742d293 KS |
623 | page = follow_trans_huge_pmd(vma, address, pmd, flags); |
624 | spin_unlock(ptl); | |
df06b37f | 625 | ctx->page_mask = HPAGE_PMD_NR - 1; |
6742d293 | 626 | return page; |
4bbd4c77 KS |
627 | } |
628 | ||
080dbb61 AK |
629 | static struct page *follow_pud_mask(struct vm_area_struct *vma, |
630 | unsigned long address, p4d_t *p4dp, | |
df06b37f KB |
631 | unsigned int flags, |
632 | struct follow_page_context *ctx) | |
080dbb61 AK |
633 | { |
634 | pud_t *pud; | |
635 | spinlock_t *ptl; | |
636 | struct page *page; | |
637 | struct mm_struct *mm = vma->vm_mm; | |
638 | ||
639 | pud = pud_offset(p4dp, address); | |
640 | if (pud_none(*pud)) | |
641 | return no_page_table(vma, flags); | |
be9d3045 | 642 | if (pud_huge(*pud) && is_vm_hugetlb_page(vma)) { |
080dbb61 AK |
643 | page = follow_huge_pud(mm, address, pud, flags); |
644 | if (page) | |
645 | return page; | |
646 | return no_page_table(vma, flags); | |
647 | } | |
4dc71451 AK |
648 | if (is_hugepd(__hugepd(pud_val(*pud)))) { |
649 | page = follow_huge_pd(vma, address, | |
650 | __hugepd(pud_val(*pud)), flags, | |
651 | PUD_SHIFT); | |
652 | if (page) | |
653 | return page; | |
654 | return no_page_table(vma, flags); | |
655 | } | |
080dbb61 AK |
656 | if (pud_devmap(*pud)) { |
657 | ptl = pud_lock(mm, pud); | |
df06b37f | 658 | page = follow_devmap_pud(vma, address, pud, flags, &ctx->pgmap); |
080dbb61 AK |
659 | spin_unlock(ptl); |
660 | if (page) | |
661 | return page; | |
662 | } | |
663 | if (unlikely(pud_bad(*pud))) | |
664 | return no_page_table(vma, flags); | |
665 | ||
df06b37f | 666 | return follow_pmd_mask(vma, address, pud, flags, ctx); |
080dbb61 AK |
667 | } |
668 | ||
080dbb61 AK |
669 | static struct page *follow_p4d_mask(struct vm_area_struct *vma, |
670 | unsigned long address, pgd_t *pgdp, | |
df06b37f KB |
671 | unsigned int flags, |
672 | struct follow_page_context *ctx) | |
080dbb61 AK |
673 | { |
674 | p4d_t *p4d; | |
4dc71451 | 675 | struct page *page; |
080dbb61 AK |
676 | |
677 | p4d = p4d_offset(pgdp, address); | |
678 | if (p4d_none(*p4d)) | |
679 | return no_page_table(vma, flags); | |
680 | BUILD_BUG_ON(p4d_huge(*p4d)); | |
681 | if (unlikely(p4d_bad(*p4d))) | |
682 | return no_page_table(vma, flags); | |
683 | ||
4dc71451 AK |
684 | if (is_hugepd(__hugepd(p4d_val(*p4d)))) { |
685 | page = follow_huge_pd(vma, address, | |
686 | __hugepd(p4d_val(*p4d)), flags, | |
687 | P4D_SHIFT); | |
688 | if (page) | |
689 | return page; | |
690 | return no_page_table(vma, flags); | |
691 | } | |
df06b37f | 692 | return follow_pud_mask(vma, address, p4d, flags, ctx); |
080dbb61 AK |
693 | } |
694 | ||
695 | /** | |
696 | * follow_page_mask - look up a page descriptor from a user-virtual address | |
697 | * @vma: vm_area_struct mapping @address | |
698 | * @address: virtual address to look up | |
699 | * @flags: flags modifying lookup behaviour | |
78179556 MR |
700 | * @ctx: contains dev_pagemap for %ZONE_DEVICE memory pinning and a |
701 | * pointer to output page_mask | |
080dbb61 AK |
702 | * |
703 | * @flags can have FOLL_ flags set, defined in <linux/mm.h> | |
704 | * | |
78179556 MR |
705 | * When getting pages from ZONE_DEVICE memory, the @ctx->pgmap caches |
706 | * the device's dev_pagemap metadata to avoid repeating expensive lookups. | |
707 | * | |
708 | * On output, the @ctx->page_mask is set according to the size of the page. | |
709 | * | |
710 | * Return: the mapped (struct page *), %NULL if no mapping exists, or | |
080dbb61 AK |
711 | * an error pointer if there is a mapping to something not represented |
712 | * by a page descriptor (see also vm_normal_page()). | |
713 | */ | |
a7030aea | 714 | static struct page *follow_page_mask(struct vm_area_struct *vma, |
080dbb61 | 715 | unsigned long address, unsigned int flags, |
df06b37f | 716 | struct follow_page_context *ctx) |
080dbb61 AK |
717 | { |
718 | pgd_t *pgd; | |
719 | struct page *page; | |
720 | struct mm_struct *mm = vma->vm_mm; | |
721 | ||
df06b37f | 722 | ctx->page_mask = 0; |
080dbb61 AK |
723 | |
724 | /* make this handle hugepd */ | |
725 | page = follow_huge_addr(mm, address, flags & FOLL_WRITE); | |
726 | if (!IS_ERR(page)) { | |
3faa52c0 | 727 | WARN_ON_ONCE(flags & (FOLL_GET | FOLL_PIN)); |
080dbb61 AK |
728 | return page; |
729 | } | |
730 | ||
731 | pgd = pgd_offset(mm, address); | |
732 | ||
733 | if (pgd_none(*pgd) || unlikely(pgd_bad(*pgd))) | |
734 | return no_page_table(vma, flags); | |
735 | ||
faaa5b62 AK |
736 | if (pgd_huge(*pgd)) { |
737 | page = follow_huge_pgd(mm, address, pgd, flags); | |
738 | if (page) | |
739 | return page; | |
740 | return no_page_table(vma, flags); | |
741 | } | |
4dc71451 AK |
742 | if (is_hugepd(__hugepd(pgd_val(*pgd)))) { |
743 | page = follow_huge_pd(vma, address, | |
744 | __hugepd(pgd_val(*pgd)), flags, | |
745 | PGDIR_SHIFT); | |
746 | if (page) | |
747 | return page; | |
748 | return no_page_table(vma, flags); | |
749 | } | |
faaa5b62 | 750 | |
df06b37f KB |
751 | return follow_p4d_mask(vma, address, pgd, flags, ctx); |
752 | } | |
753 | ||
754 | struct page *follow_page(struct vm_area_struct *vma, unsigned long address, | |
755 | unsigned int foll_flags) | |
756 | { | |
757 | struct follow_page_context ctx = { NULL }; | |
758 | struct page *page; | |
759 | ||
760 | page = follow_page_mask(vma, address, foll_flags, &ctx); | |
761 | if (ctx.pgmap) | |
762 | put_dev_pagemap(ctx.pgmap); | |
763 | return page; | |
080dbb61 AK |
764 | } |
765 | ||
f2b495ca KS |
766 | static int get_gate_page(struct mm_struct *mm, unsigned long address, |
767 | unsigned int gup_flags, struct vm_area_struct **vma, | |
768 | struct page **page) | |
769 | { | |
770 | pgd_t *pgd; | |
c2febafc | 771 | p4d_t *p4d; |
f2b495ca KS |
772 | pud_t *pud; |
773 | pmd_t *pmd; | |
774 | pte_t *pte; | |
775 | int ret = -EFAULT; | |
776 | ||
777 | /* user gate pages are read-only */ | |
778 | if (gup_flags & FOLL_WRITE) | |
779 | return -EFAULT; | |
780 | if (address > TASK_SIZE) | |
781 | pgd = pgd_offset_k(address); | |
782 | else | |
783 | pgd = pgd_offset_gate(mm, address); | |
b5d1c39f AL |
784 | if (pgd_none(*pgd)) |
785 | return -EFAULT; | |
c2febafc | 786 | p4d = p4d_offset(pgd, address); |
b5d1c39f AL |
787 | if (p4d_none(*p4d)) |
788 | return -EFAULT; | |
c2febafc | 789 | pud = pud_offset(p4d, address); |
b5d1c39f AL |
790 | if (pud_none(*pud)) |
791 | return -EFAULT; | |
f2b495ca | 792 | pmd = pmd_offset(pud, address); |
84c3fc4e | 793 | if (!pmd_present(*pmd)) |
f2b495ca KS |
794 | return -EFAULT; |
795 | VM_BUG_ON(pmd_trans_huge(*pmd)); | |
796 | pte = pte_offset_map(pmd, address); | |
797 | if (pte_none(*pte)) | |
798 | goto unmap; | |
799 | *vma = get_gate_vma(mm); | |
800 | if (!page) | |
801 | goto out; | |
802 | *page = vm_normal_page(*vma, address, *pte); | |
803 | if (!*page) { | |
804 | if ((gup_flags & FOLL_DUMP) || !is_zero_pfn(pte_pfn(*pte))) | |
805 | goto unmap; | |
806 | *page = pte_page(*pte); | |
807 | } | |
8fde12ca LT |
808 | if (unlikely(!try_get_page(*page))) { |
809 | ret = -ENOMEM; | |
810 | goto unmap; | |
811 | } | |
f2b495ca KS |
812 | out: |
813 | ret = 0; | |
814 | unmap: | |
815 | pte_unmap(pte); | |
816 | return ret; | |
817 | } | |
818 | ||
9a95f3cf PC |
819 | /* |
820 | * mmap_sem must be held on entry. If @nonblocking != NULL and | |
821 | * *@flags does not include FOLL_NOWAIT, the mmap_sem may be released. | |
822 | * If it is, *@nonblocking will be set to 0 and -EBUSY returned. | |
823 | */ | |
16744483 KS |
824 | static int faultin_page(struct task_struct *tsk, struct vm_area_struct *vma, |
825 | unsigned long address, unsigned int *flags, int *nonblocking) | |
826 | { | |
16744483 | 827 | unsigned int fault_flags = 0; |
2b740303 | 828 | vm_fault_t ret; |
16744483 | 829 | |
de60f5f1 EM |
830 | /* mlock all present pages, but do not fault in new pages */ |
831 | if ((*flags & (FOLL_POPULATE | FOLL_MLOCK)) == FOLL_MLOCK) | |
832 | return -ENOENT; | |
16744483 KS |
833 | if (*flags & FOLL_WRITE) |
834 | fault_flags |= FAULT_FLAG_WRITE; | |
1b2ee126 DH |
835 | if (*flags & FOLL_REMOTE) |
836 | fault_flags |= FAULT_FLAG_REMOTE; | |
16744483 KS |
837 | if (nonblocking) |
838 | fault_flags |= FAULT_FLAG_ALLOW_RETRY; | |
839 | if (*flags & FOLL_NOWAIT) | |
840 | fault_flags |= FAULT_FLAG_ALLOW_RETRY | FAULT_FLAG_RETRY_NOWAIT; | |
234b239b ALC |
841 | if (*flags & FOLL_TRIED) { |
842 | VM_WARN_ON_ONCE(fault_flags & FAULT_FLAG_ALLOW_RETRY); | |
843 | fault_flags |= FAULT_FLAG_TRIED; | |
844 | } | |
16744483 | 845 | |
dcddffd4 | 846 | ret = handle_mm_fault(vma, address, fault_flags); |
16744483 | 847 | if (ret & VM_FAULT_ERROR) { |
9a291a7c JM |
848 | int err = vm_fault_to_errno(ret, *flags); |
849 | ||
850 | if (err) | |
851 | return err; | |
16744483 KS |
852 | BUG(); |
853 | } | |
854 | ||
855 | if (tsk) { | |
856 | if (ret & VM_FAULT_MAJOR) | |
857 | tsk->maj_flt++; | |
858 | else | |
859 | tsk->min_flt++; | |
860 | } | |
861 | ||
862 | if (ret & VM_FAULT_RETRY) { | |
96312e61 | 863 | if (nonblocking && !(fault_flags & FAULT_FLAG_RETRY_NOWAIT)) |
16744483 KS |
864 | *nonblocking = 0; |
865 | return -EBUSY; | |
866 | } | |
867 | ||
868 | /* | |
869 | * The VM_FAULT_WRITE bit tells us that do_wp_page has broken COW when | |
870 | * necessary, even if maybe_mkwrite decided not to set pte_write. We | |
871 | * can thus safely do subsequent page lookups as if they were reads. | |
872 | * But only do so when looping for pte_write is futile: in some cases | |
873 | * userspace may also be wanting to write to the gotten user page, | |
874 | * which a read fault here might prevent (a readonly page might get | |
875 | * reCOWed by userspace write). | |
876 | */ | |
877 | if ((ret & VM_FAULT_WRITE) && !(vma->vm_flags & VM_WRITE)) | |
2923117b | 878 | *flags |= FOLL_COW; |
16744483 KS |
879 | return 0; |
880 | } | |
881 | ||
fa5bb209 KS |
882 | static int check_vma_flags(struct vm_area_struct *vma, unsigned long gup_flags) |
883 | { | |
884 | vm_flags_t vm_flags = vma->vm_flags; | |
1b2ee126 DH |
885 | int write = (gup_flags & FOLL_WRITE); |
886 | int foreign = (gup_flags & FOLL_REMOTE); | |
fa5bb209 KS |
887 | |
888 | if (vm_flags & (VM_IO | VM_PFNMAP)) | |
889 | return -EFAULT; | |
890 | ||
7f7ccc2c WT |
891 | if (gup_flags & FOLL_ANON && !vma_is_anonymous(vma)) |
892 | return -EFAULT; | |
893 | ||
1b2ee126 | 894 | if (write) { |
fa5bb209 KS |
895 | if (!(vm_flags & VM_WRITE)) { |
896 | if (!(gup_flags & FOLL_FORCE)) | |
897 | return -EFAULT; | |
898 | /* | |
899 | * We used to let the write,force case do COW in a | |
900 | * VM_MAYWRITE VM_SHARED !VM_WRITE vma, so ptrace could | |
901 | * set a breakpoint in a read-only mapping of an | |
902 | * executable, without corrupting the file (yet only | |
903 | * when that file had been opened for writing!). | |
904 | * Anon pages in shared mappings are surprising: now | |
905 | * just reject it. | |
906 | */ | |
46435364 | 907 | if (!is_cow_mapping(vm_flags)) |
fa5bb209 | 908 | return -EFAULT; |
fa5bb209 KS |
909 | } |
910 | } else if (!(vm_flags & VM_READ)) { | |
911 | if (!(gup_flags & FOLL_FORCE)) | |
912 | return -EFAULT; | |
913 | /* | |
914 | * Is there actually any vma we can reach here which does not | |
915 | * have VM_MAYREAD set? | |
916 | */ | |
917 | if (!(vm_flags & VM_MAYREAD)) | |
918 | return -EFAULT; | |
919 | } | |
d61172b4 DH |
920 | /* |
921 | * gups are always data accesses, not instruction | |
922 | * fetches, so execute=false here | |
923 | */ | |
924 | if (!arch_vma_access_permitted(vma, write, false, foreign)) | |
33a709b2 | 925 | return -EFAULT; |
fa5bb209 KS |
926 | return 0; |
927 | } | |
928 | ||
4bbd4c77 KS |
929 | /** |
930 | * __get_user_pages() - pin user pages in memory | |
931 | * @tsk: task_struct of target task | |
932 | * @mm: mm_struct of target mm | |
933 | * @start: starting user address | |
934 | * @nr_pages: number of pages from start to pin | |
935 | * @gup_flags: flags modifying pin behaviour | |
936 | * @pages: array that receives pointers to the pages pinned. | |
937 | * Should be at least nr_pages long. Or NULL, if caller | |
938 | * only intends to ensure the pages are faulted in. | |
939 | * @vmas: array of pointers to vmas corresponding to each page. | |
940 | * Or NULL if the caller does not require them. | |
941 | * @nonblocking: whether waiting for disk IO or mmap_sem contention | |
942 | * | |
d2dfbe47 LX |
943 | * Returns either number of pages pinned (which may be less than the |
944 | * number requested), or an error. Details about the return value: | |
945 | * | |
946 | * -- If nr_pages is 0, returns 0. | |
947 | * -- If nr_pages is >0, but no pages were pinned, returns -errno. | |
948 | * -- If nr_pages is >0, and some pages were pinned, returns the number of | |
949 | * pages pinned. Again, this may be less than nr_pages. | |
950 | * | |
951 | * The caller is responsible for releasing returned @pages, via put_page(). | |
952 | * | |
953 | * @vmas are valid only as long as mmap_sem is held. | |
4bbd4c77 | 954 | * |
9a95f3cf | 955 | * Must be called with mmap_sem held. It may be released. See below. |
4bbd4c77 KS |
956 | * |
957 | * __get_user_pages walks a process's page tables and takes a reference to | |
958 | * each struct page that each user address corresponds to at a given | |
959 | * instant. That is, it takes the page that would be accessed if a user | |
960 | * thread accesses the given user virtual address at that instant. | |
961 | * | |
962 | * This does not guarantee that the page exists in the user mappings when | |
963 | * __get_user_pages returns, and there may even be a completely different | |
964 | * page there in some cases (eg. if mmapped pagecache has been invalidated | |
965 | * and subsequently re faulted). However it does guarantee that the page | |
966 | * won't be freed completely. And mostly callers simply care that the page | |
967 | * contains data that was valid *at some point in time*. Typically, an IO | |
968 | * or similar operation cannot guarantee anything stronger anyway because | |
969 | * locks can't be held over the syscall boundary. | |
970 | * | |
971 | * If @gup_flags & FOLL_WRITE == 0, the page must not be written to. If | |
972 | * the page is written to, set_page_dirty (or set_page_dirty_lock, as | |
973 | * appropriate) must be called after the page is finished with, and | |
974 | * before put_page is called. | |
975 | * | |
976 | * If @nonblocking != NULL, __get_user_pages will not wait for disk IO | |
977 | * or mmap_sem contention, and if waiting is needed to pin all pages, | |
9a95f3cf PC |
978 | * *@nonblocking will be set to 0. Further, if @gup_flags does not |
979 | * include FOLL_NOWAIT, the mmap_sem will be released via up_read() in | |
980 | * this case. | |
981 | * | |
982 | * A caller using such a combination of @nonblocking and @gup_flags | |
983 | * must therefore hold the mmap_sem for reading only, and recognize | |
984 | * when it's been released. Otherwise, it must be held for either | |
985 | * reading or writing and will not be released. | |
4bbd4c77 KS |
986 | * |
987 | * In most cases, get_user_pages or get_user_pages_fast should be used | |
988 | * instead of __get_user_pages. __get_user_pages should be used only if | |
989 | * you need some special @gup_flags. | |
990 | */ | |
0d731759 | 991 | static long __get_user_pages(struct task_struct *tsk, struct mm_struct *mm, |
4bbd4c77 KS |
992 | unsigned long start, unsigned long nr_pages, |
993 | unsigned int gup_flags, struct page **pages, | |
994 | struct vm_area_struct **vmas, int *nonblocking) | |
995 | { | |
df06b37f | 996 | long ret = 0, i = 0; |
fa5bb209 | 997 | struct vm_area_struct *vma = NULL; |
df06b37f | 998 | struct follow_page_context ctx = { NULL }; |
4bbd4c77 KS |
999 | |
1000 | if (!nr_pages) | |
1001 | return 0; | |
1002 | ||
f9652594 AK |
1003 | start = untagged_addr(start); |
1004 | ||
eddb1c22 | 1005 | VM_BUG_ON(!!pages != !!(gup_flags & (FOLL_GET | FOLL_PIN))); |
4bbd4c77 KS |
1006 | |
1007 | /* | |
1008 | * If FOLL_FORCE is set then do not force a full fault as the hinting | |
1009 | * fault information is unrelated to the reference behaviour of a task | |
1010 | * using the address space | |
1011 | */ | |
1012 | if (!(gup_flags & FOLL_FORCE)) | |
1013 | gup_flags |= FOLL_NUMA; | |
1014 | ||
4bbd4c77 | 1015 | do { |
fa5bb209 KS |
1016 | struct page *page; |
1017 | unsigned int foll_flags = gup_flags; | |
1018 | unsigned int page_increm; | |
1019 | ||
1020 | /* first iteration or cross vma bound */ | |
1021 | if (!vma || start >= vma->vm_end) { | |
1022 | vma = find_extend_vma(mm, start); | |
1023 | if (!vma && in_gate_area(mm, start)) { | |
fa5bb209 KS |
1024 | ret = get_gate_page(mm, start & PAGE_MASK, |
1025 | gup_flags, &vma, | |
1026 | pages ? &pages[i] : NULL); | |
1027 | if (ret) | |
08be37b7 | 1028 | goto out; |
df06b37f | 1029 | ctx.page_mask = 0; |
fa5bb209 KS |
1030 | goto next_page; |
1031 | } | |
4bbd4c77 | 1032 | |
df06b37f KB |
1033 | if (!vma || check_vma_flags(vma, gup_flags)) { |
1034 | ret = -EFAULT; | |
1035 | goto out; | |
1036 | } | |
fa5bb209 KS |
1037 | if (is_vm_hugetlb_page(vma)) { |
1038 | i = follow_hugetlb_page(mm, vma, pages, vmas, | |
1039 | &start, &nr_pages, i, | |
87ffc118 | 1040 | gup_flags, nonblocking); |
fa5bb209 | 1041 | continue; |
4bbd4c77 | 1042 | } |
fa5bb209 KS |
1043 | } |
1044 | retry: | |
1045 | /* | |
1046 | * If we have a pending SIGKILL, don't keep faulting pages and | |
1047 | * potentially allocating memory. | |
1048 | */ | |
fa45f116 | 1049 | if (fatal_signal_pending(current)) { |
df06b37f KB |
1050 | ret = -ERESTARTSYS; |
1051 | goto out; | |
1052 | } | |
fa5bb209 | 1053 | cond_resched(); |
df06b37f KB |
1054 | |
1055 | page = follow_page_mask(vma, start, foll_flags, &ctx); | |
fa5bb209 | 1056 | if (!page) { |
fa5bb209 KS |
1057 | ret = faultin_page(tsk, vma, start, &foll_flags, |
1058 | nonblocking); | |
1059 | switch (ret) { | |
1060 | case 0: | |
1061 | goto retry; | |
df06b37f KB |
1062 | case -EBUSY: |
1063 | ret = 0; | |
1064 | /* FALLTHRU */ | |
fa5bb209 KS |
1065 | case -EFAULT: |
1066 | case -ENOMEM: | |
1067 | case -EHWPOISON: | |
df06b37f | 1068 | goto out; |
fa5bb209 KS |
1069 | case -ENOENT: |
1070 | goto next_page; | |
4bbd4c77 | 1071 | } |
fa5bb209 | 1072 | BUG(); |
1027e443 KS |
1073 | } else if (PTR_ERR(page) == -EEXIST) { |
1074 | /* | |
1075 | * Proper page table entry exists, but no corresponding | |
1076 | * struct page. | |
1077 | */ | |
1078 | goto next_page; | |
1079 | } else if (IS_ERR(page)) { | |
df06b37f KB |
1080 | ret = PTR_ERR(page); |
1081 | goto out; | |
1027e443 | 1082 | } |
fa5bb209 KS |
1083 | if (pages) { |
1084 | pages[i] = page; | |
1085 | flush_anon_page(vma, page, start); | |
1086 | flush_dcache_page(page); | |
df06b37f | 1087 | ctx.page_mask = 0; |
4bbd4c77 | 1088 | } |
4bbd4c77 | 1089 | next_page: |
fa5bb209 KS |
1090 | if (vmas) { |
1091 | vmas[i] = vma; | |
df06b37f | 1092 | ctx.page_mask = 0; |
fa5bb209 | 1093 | } |
df06b37f | 1094 | page_increm = 1 + (~(start >> PAGE_SHIFT) & ctx.page_mask); |
fa5bb209 KS |
1095 | if (page_increm > nr_pages) |
1096 | page_increm = nr_pages; | |
1097 | i += page_increm; | |
1098 | start += page_increm * PAGE_SIZE; | |
1099 | nr_pages -= page_increm; | |
4bbd4c77 | 1100 | } while (nr_pages); |
df06b37f KB |
1101 | out: |
1102 | if (ctx.pgmap) | |
1103 | put_dev_pagemap(ctx.pgmap); | |
1104 | return i ? i : ret; | |
4bbd4c77 | 1105 | } |
4bbd4c77 | 1106 | |
771ab430 TK |
1107 | static bool vma_permits_fault(struct vm_area_struct *vma, |
1108 | unsigned int fault_flags) | |
d4925e00 | 1109 | { |
1b2ee126 DH |
1110 | bool write = !!(fault_flags & FAULT_FLAG_WRITE); |
1111 | bool foreign = !!(fault_flags & FAULT_FLAG_REMOTE); | |
33a709b2 | 1112 | vm_flags_t vm_flags = write ? VM_WRITE : VM_READ; |
d4925e00 DH |
1113 | |
1114 | if (!(vm_flags & vma->vm_flags)) | |
1115 | return false; | |
1116 | ||
33a709b2 DH |
1117 | /* |
1118 | * The architecture might have a hardware protection | |
1b2ee126 | 1119 | * mechanism other than read/write that can deny access. |
d61172b4 DH |
1120 | * |
1121 | * gup always represents data access, not instruction | |
1122 | * fetches, so execute=false here: | |
33a709b2 | 1123 | */ |
d61172b4 | 1124 | if (!arch_vma_access_permitted(vma, write, false, foreign)) |
33a709b2 DH |
1125 | return false; |
1126 | ||
d4925e00 DH |
1127 | return true; |
1128 | } | |
1129 | ||
4bbd4c77 KS |
1130 | /* |
1131 | * fixup_user_fault() - manually resolve a user page fault | |
1132 | * @tsk: the task_struct to use for page fault accounting, or | |
1133 | * NULL if faults are not to be recorded. | |
1134 | * @mm: mm_struct of target mm | |
1135 | * @address: user address | |
1136 | * @fault_flags:flags to pass down to handle_mm_fault() | |
4a9e1cda DD |
1137 | * @unlocked: did we unlock the mmap_sem while retrying, maybe NULL if caller |
1138 | * does not allow retry | |
4bbd4c77 KS |
1139 | * |
1140 | * This is meant to be called in the specific scenario where for locking reasons | |
1141 | * we try to access user memory in atomic context (within a pagefault_disable() | |
1142 | * section), this returns -EFAULT, and we want to resolve the user fault before | |
1143 | * trying again. | |
1144 | * | |
1145 | * Typically this is meant to be used by the futex code. | |
1146 | * | |
1147 | * The main difference with get_user_pages() is that this function will | |
1148 | * unconditionally call handle_mm_fault() which will in turn perform all the | |
1149 | * necessary SW fixup of the dirty and young bits in the PTE, while | |
4a9e1cda | 1150 | * get_user_pages() only guarantees to update these in the struct page. |
4bbd4c77 KS |
1151 | * |
1152 | * This is important for some architectures where those bits also gate the | |
1153 | * access permission to the page because they are maintained in software. On | |
1154 | * such architectures, gup() will not be enough to make a subsequent access | |
1155 | * succeed. | |
1156 | * | |
4a9e1cda DD |
1157 | * This function will not return with an unlocked mmap_sem. So it has not the |
1158 | * same semantics wrt the @mm->mmap_sem as does filemap_fault(). | |
4bbd4c77 KS |
1159 | */ |
1160 | int fixup_user_fault(struct task_struct *tsk, struct mm_struct *mm, | |
4a9e1cda DD |
1161 | unsigned long address, unsigned int fault_flags, |
1162 | bool *unlocked) | |
4bbd4c77 KS |
1163 | { |
1164 | struct vm_area_struct *vma; | |
2b740303 | 1165 | vm_fault_t ret, major = 0; |
4a9e1cda | 1166 | |
f9652594 AK |
1167 | address = untagged_addr(address); |
1168 | ||
4a9e1cda DD |
1169 | if (unlocked) |
1170 | fault_flags |= FAULT_FLAG_ALLOW_RETRY; | |
4bbd4c77 | 1171 | |
4a9e1cda | 1172 | retry: |
4bbd4c77 KS |
1173 | vma = find_extend_vma(mm, address); |
1174 | if (!vma || address < vma->vm_start) | |
1175 | return -EFAULT; | |
1176 | ||
d4925e00 | 1177 | if (!vma_permits_fault(vma, fault_flags)) |
4bbd4c77 KS |
1178 | return -EFAULT; |
1179 | ||
dcddffd4 | 1180 | ret = handle_mm_fault(vma, address, fault_flags); |
4a9e1cda | 1181 | major |= ret & VM_FAULT_MAJOR; |
4bbd4c77 | 1182 | if (ret & VM_FAULT_ERROR) { |
9a291a7c JM |
1183 | int err = vm_fault_to_errno(ret, 0); |
1184 | ||
1185 | if (err) | |
1186 | return err; | |
4bbd4c77 KS |
1187 | BUG(); |
1188 | } | |
4a9e1cda DD |
1189 | |
1190 | if (ret & VM_FAULT_RETRY) { | |
1191 | down_read(&mm->mmap_sem); | |
1192 | if (!(fault_flags & FAULT_FLAG_TRIED)) { | |
1193 | *unlocked = true; | |
1194 | fault_flags &= ~FAULT_FLAG_ALLOW_RETRY; | |
1195 | fault_flags |= FAULT_FLAG_TRIED; | |
1196 | goto retry; | |
1197 | } | |
1198 | } | |
1199 | ||
4bbd4c77 | 1200 | if (tsk) { |
4a9e1cda | 1201 | if (major) |
4bbd4c77 KS |
1202 | tsk->maj_flt++; |
1203 | else | |
1204 | tsk->min_flt++; | |
1205 | } | |
1206 | return 0; | |
1207 | } | |
add6a0cd | 1208 | EXPORT_SYMBOL_GPL(fixup_user_fault); |
4bbd4c77 | 1209 | |
f0818f47 AA |
1210 | static __always_inline long __get_user_pages_locked(struct task_struct *tsk, |
1211 | struct mm_struct *mm, | |
1212 | unsigned long start, | |
1213 | unsigned long nr_pages, | |
f0818f47 AA |
1214 | struct page **pages, |
1215 | struct vm_area_struct **vmas, | |
e716712f | 1216 | int *locked, |
0fd71a56 | 1217 | unsigned int flags) |
f0818f47 | 1218 | { |
f0818f47 AA |
1219 | long ret, pages_done; |
1220 | bool lock_dropped; | |
1221 | ||
1222 | if (locked) { | |
1223 | /* if VM_FAULT_RETRY can be returned, vmas become invalid */ | |
1224 | BUG_ON(vmas); | |
1225 | /* check caller initialized locked */ | |
1226 | BUG_ON(*locked != 1); | |
1227 | } | |
1228 | ||
eddb1c22 JH |
1229 | /* |
1230 | * FOLL_PIN and FOLL_GET are mutually exclusive. Traditional behavior | |
1231 | * is to set FOLL_GET if the caller wants pages[] filled in (but has | |
1232 | * carelessly failed to specify FOLL_GET), so keep doing that, but only | |
1233 | * for FOLL_GET, not for the newer FOLL_PIN. | |
1234 | * | |
1235 | * FOLL_PIN always expects pages to be non-null, but no need to assert | |
1236 | * that here, as any failures will be obvious enough. | |
1237 | */ | |
1238 | if (pages && !(flags & FOLL_PIN)) | |
f0818f47 | 1239 | flags |= FOLL_GET; |
f0818f47 AA |
1240 | |
1241 | pages_done = 0; | |
1242 | lock_dropped = false; | |
1243 | for (;;) { | |
1244 | ret = __get_user_pages(tsk, mm, start, nr_pages, flags, pages, | |
1245 | vmas, locked); | |
1246 | if (!locked) | |
1247 | /* VM_FAULT_RETRY couldn't trigger, bypass */ | |
1248 | return ret; | |
1249 | ||
1250 | /* VM_FAULT_RETRY cannot return errors */ | |
1251 | if (!*locked) { | |
1252 | BUG_ON(ret < 0); | |
1253 | BUG_ON(ret >= nr_pages); | |
1254 | } | |
1255 | ||
f0818f47 AA |
1256 | if (ret > 0) { |
1257 | nr_pages -= ret; | |
1258 | pages_done += ret; | |
1259 | if (!nr_pages) | |
1260 | break; | |
1261 | } | |
1262 | if (*locked) { | |
96312e61 AA |
1263 | /* |
1264 | * VM_FAULT_RETRY didn't trigger or it was a | |
1265 | * FOLL_NOWAIT. | |
1266 | */ | |
f0818f47 AA |
1267 | if (!pages_done) |
1268 | pages_done = ret; | |
1269 | break; | |
1270 | } | |
df17277b MR |
1271 | /* |
1272 | * VM_FAULT_RETRY triggered, so seek to the faulting offset. | |
1273 | * For the prefault case (!pages) we only update counts. | |
1274 | */ | |
1275 | if (likely(pages)) | |
1276 | pages += ret; | |
f0818f47 AA |
1277 | start += ret << PAGE_SHIFT; |
1278 | ||
1279 | /* | |
1280 | * Repeat on the address that fired VM_FAULT_RETRY | |
1281 | * without FAULT_FLAG_ALLOW_RETRY but with | |
1282 | * FAULT_FLAG_TRIED. | |
1283 | */ | |
1284 | *locked = 1; | |
1285 | lock_dropped = true; | |
1286 | down_read(&mm->mmap_sem); | |
1287 | ret = __get_user_pages(tsk, mm, start, 1, flags | FOLL_TRIED, | |
1288 | pages, NULL, NULL); | |
1289 | if (ret != 1) { | |
1290 | BUG_ON(ret > 1); | |
1291 | if (!pages_done) | |
1292 | pages_done = ret; | |
1293 | break; | |
1294 | } | |
1295 | nr_pages--; | |
1296 | pages_done++; | |
1297 | if (!nr_pages) | |
1298 | break; | |
df17277b MR |
1299 | if (likely(pages)) |
1300 | pages++; | |
f0818f47 AA |
1301 | start += PAGE_SIZE; |
1302 | } | |
e716712f | 1303 | if (lock_dropped && *locked) { |
f0818f47 AA |
1304 | /* |
1305 | * We must let the caller know we temporarily dropped the lock | |
1306 | * and so the critical section protected by it was lost. | |
1307 | */ | |
1308 | up_read(&mm->mmap_sem); | |
1309 | *locked = 0; | |
1310 | } | |
1311 | return pages_done; | |
1312 | } | |
1313 | ||
d3649f68 CH |
1314 | /** |
1315 | * populate_vma_page_range() - populate a range of pages in the vma. | |
1316 | * @vma: target vma | |
1317 | * @start: start address | |
1318 | * @end: end address | |
1319 | * @nonblocking: | |
1320 | * | |
1321 | * This takes care of mlocking the pages too if VM_LOCKED is set. | |
1322 | * | |
1323 | * return 0 on success, negative error code on error. | |
1324 | * | |
1325 | * vma->vm_mm->mmap_sem must be held. | |
1326 | * | |
1327 | * If @nonblocking is NULL, it may be held for read or write and will | |
1328 | * be unperturbed. | |
1329 | * | |
1330 | * If @nonblocking is non-NULL, it must held for read only and may be | |
1331 | * released. If it's released, *@nonblocking will be set to 0. | |
1332 | */ | |
1333 | long populate_vma_page_range(struct vm_area_struct *vma, | |
1334 | unsigned long start, unsigned long end, int *nonblocking) | |
1335 | { | |
1336 | struct mm_struct *mm = vma->vm_mm; | |
1337 | unsigned long nr_pages = (end - start) / PAGE_SIZE; | |
1338 | int gup_flags; | |
1339 | ||
1340 | VM_BUG_ON(start & ~PAGE_MASK); | |
1341 | VM_BUG_ON(end & ~PAGE_MASK); | |
1342 | VM_BUG_ON_VMA(start < vma->vm_start, vma); | |
1343 | VM_BUG_ON_VMA(end > vma->vm_end, vma); | |
1344 | VM_BUG_ON_MM(!rwsem_is_locked(&mm->mmap_sem), mm); | |
1345 | ||
1346 | gup_flags = FOLL_TOUCH | FOLL_POPULATE | FOLL_MLOCK; | |
1347 | if (vma->vm_flags & VM_LOCKONFAULT) | |
1348 | gup_flags &= ~FOLL_POPULATE; | |
1349 | /* | |
1350 | * We want to touch writable mappings with a write fault in order | |
1351 | * to break COW, except for shared mappings because these don't COW | |
1352 | * and we would not want to dirty them for nothing. | |
1353 | */ | |
1354 | if ((vma->vm_flags & (VM_WRITE | VM_SHARED)) == VM_WRITE) | |
1355 | gup_flags |= FOLL_WRITE; | |
1356 | ||
1357 | /* | |
1358 | * We want mlock to succeed for regions that have any permissions | |
1359 | * other than PROT_NONE. | |
1360 | */ | |
1361 | if (vma->vm_flags & (VM_READ | VM_WRITE | VM_EXEC)) | |
1362 | gup_flags |= FOLL_FORCE; | |
1363 | ||
1364 | /* | |
1365 | * We made sure addr is within a VMA, so the following will | |
1366 | * not result in a stack expansion that recurses back here. | |
1367 | */ | |
1368 | return __get_user_pages(current, mm, start, nr_pages, gup_flags, | |
1369 | NULL, NULL, nonblocking); | |
1370 | } | |
1371 | ||
1372 | /* | |
1373 | * __mm_populate - populate and/or mlock pages within a range of address space. | |
1374 | * | |
1375 | * This is used to implement mlock() and the MAP_POPULATE / MAP_LOCKED mmap | |
1376 | * flags. VMAs must be already marked with the desired vm_flags, and | |
1377 | * mmap_sem must not be held. | |
1378 | */ | |
1379 | int __mm_populate(unsigned long start, unsigned long len, int ignore_errors) | |
1380 | { | |
1381 | struct mm_struct *mm = current->mm; | |
1382 | unsigned long end, nstart, nend; | |
1383 | struct vm_area_struct *vma = NULL; | |
1384 | int locked = 0; | |
1385 | long ret = 0; | |
1386 | ||
1387 | end = start + len; | |
1388 | ||
1389 | for (nstart = start; nstart < end; nstart = nend) { | |
1390 | /* | |
1391 | * We want to fault in pages for [nstart; end) address range. | |
1392 | * Find first corresponding VMA. | |
1393 | */ | |
1394 | if (!locked) { | |
1395 | locked = 1; | |
1396 | down_read(&mm->mmap_sem); | |
1397 | vma = find_vma(mm, nstart); | |
1398 | } else if (nstart >= vma->vm_end) | |
1399 | vma = vma->vm_next; | |
1400 | if (!vma || vma->vm_start >= end) | |
1401 | break; | |
1402 | /* | |
1403 | * Set [nstart; nend) to intersection of desired address | |
1404 | * range with the first VMA. Also, skip undesirable VMA types. | |
1405 | */ | |
1406 | nend = min(end, vma->vm_end); | |
1407 | if (vma->vm_flags & (VM_IO | VM_PFNMAP)) | |
1408 | continue; | |
1409 | if (nstart < vma->vm_start) | |
1410 | nstart = vma->vm_start; | |
1411 | /* | |
1412 | * Now fault in a range of pages. populate_vma_page_range() | |
1413 | * double checks the vma flags, so that it won't mlock pages | |
1414 | * if the vma was already munlocked. | |
1415 | */ | |
1416 | ret = populate_vma_page_range(vma, nstart, nend, &locked); | |
1417 | if (ret < 0) { | |
1418 | if (ignore_errors) { | |
1419 | ret = 0; | |
1420 | continue; /* continue at next VMA */ | |
1421 | } | |
1422 | break; | |
1423 | } | |
1424 | nend = nstart + ret * PAGE_SIZE; | |
1425 | ret = 0; | |
1426 | } | |
1427 | if (locked) | |
1428 | up_read(&mm->mmap_sem); | |
1429 | return ret; /* 0 or negative error code */ | |
1430 | } | |
1431 | ||
1432 | /** | |
1433 | * get_dump_page() - pin user page in memory while writing it to core dump | |
1434 | * @addr: user address | |
1435 | * | |
1436 | * Returns struct page pointer of user page pinned for dump, | |
1437 | * to be freed afterwards by put_page(). | |
1438 | * | |
1439 | * Returns NULL on any kind of failure - a hole must then be inserted into | |
1440 | * the corefile, to preserve alignment with its headers; and also returns | |
1441 | * NULL wherever the ZERO_PAGE, or an anonymous pte_none, has been found - | |
1442 | * allowing a hole to be left in the corefile to save diskspace. | |
1443 | * | |
1444 | * Called without mmap_sem, but after all other threads have been killed. | |
1445 | */ | |
1446 | #ifdef CONFIG_ELF_CORE | |
1447 | struct page *get_dump_page(unsigned long addr) | |
1448 | { | |
1449 | struct vm_area_struct *vma; | |
1450 | struct page *page; | |
1451 | ||
1452 | if (__get_user_pages(current, current->mm, addr, 1, | |
1453 | FOLL_FORCE | FOLL_DUMP | FOLL_GET, &page, &vma, | |
1454 | NULL) < 1) | |
1455 | return NULL; | |
1456 | flush_cache_page(vma, addr, page_to_pfn(page)); | |
1457 | return page; | |
1458 | } | |
1459 | #endif /* CONFIG_ELF_CORE */ | |
050a9adc CH |
1460 | #else /* CONFIG_MMU */ |
1461 | static long __get_user_pages_locked(struct task_struct *tsk, | |
1462 | struct mm_struct *mm, unsigned long start, | |
1463 | unsigned long nr_pages, struct page **pages, | |
1464 | struct vm_area_struct **vmas, int *locked, | |
1465 | unsigned int foll_flags) | |
1466 | { | |
1467 | struct vm_area_struct *vma; | |
1468 | unsigned long vm_flags; | |
1469 | int i; | |
1470 | ||
1471 | /* calculate required read or write permissions. | |
1472 | * If FOLL_FORCE is set, we only require the "MAY" flags. | |
1473 | */ | |
1474 | vm_flags = (foll_flags & FOLL_WRITE) ? | |
1475 | (VM_WRITE | VM_MAYWRITE) : (VM_READ | VM_MAYREAD); | |
1476 | vm_flags &= (foll_flags & FOLL_FORCE) ? | |
1477 | (VM_MAYREAD | VM_MAYWRITE) : (VM_READ | VM_WRITE); | |
1478 | ||
1479 | for (i = 0; i < nr_pages; i++) { | |
1480 | vma = find_vma(mm, start); | |
1481 | if (!vma) | |
1482 | goto finish_or_fault; | |
1483 | ||
1484 | /* protect what we can, including chardevs */ | |
1485 | if ((vma->vm_flags & (VM_IO | VM_PFNMAP)) || | |
1486 | !(vm_flags & vma->vm_flags)) | |
1487 | goto finish_or_fault; | |
1488 | ||
1489 | if (pages) { | |
1490 | pages[i] = virt_to_page(start); | |
1491 | if (pages[i]) | |
1492 | get_page(pages[i]); | |
1493 | } | |
1494 | if (vmas) | |
1495 | vmas[i] = vma; | |
1496 | start = (start + PAGE_SIZE) & PAGE_MASK; | |
1497 | } | |
1498 | ||
1499 | return i; | |
1500 | ||
1501 | finish_or_fault: | |
1502 | return i ? : -EFAULT; | |
1503 | } | |
1504 | #endif /* !CONFIG_MMU */ | |
d3649f68 | 1505 | |
9a4e9f3b | 1506 | #if defined(CONFIG_FS_DAX) || defined (CONFIG_CMA) |
9a4e9f3b AK |
1507 | static bool check_dax_vmas(struct vm_area_struct **vmas, long nr_pages) |
1508 | { | |
1509 | long i; | |
1510 | struct vm_area_struct *vma_prev = NULL; | |
1511 | ||
1512 | for (i = 0; i < nr_pages; i++) { | |
1513 | struct vm_area_struct *vma = vmas[i]; | |
1514 | ||
1515 | if (vma == vma_prev) | |
1516 | continue; | |
1517 | ||
1518 | vma_prev = vma; | |
1519 | ||
1520 | if (vma_is_fsdax(vma)) | |
1521 | return true; | |
1522 | } | |
1523 | return false; | |
1524 | } | |
9a4e9f3b AK |
1525 | |
1526 | #ifdef CONFIG_CMA | |
1527 | static struct page *new_non_cma_page(struct page *page, unsigned long private) | |
1528 | { | |
1529 | /* | |
1530 | * We want to make sure we allocate the new page from the same node | |
1531 | * as the source page. | |
1532 | */ | |
1533 | int nid = page_to_nid(page); | |
1534 | /* | |
1535 | * Trying to allocate a page for migration. Ignore allocation | |
1536 | * failure warnings. We don't force __GFP_THISNODE here because | |
1537 | * this node here is the node where we have CMA reservation and | |
1538 | * in some case these nodes will have really less non movable | |
1539 | * allocation memory. | |
1540 | */ | |
1541 | gfp_t gfp_mask = GFP_USER | __GFP_NOWARN; | |
1542 | ||
1543 | if (PageHighMem(page)) | |
1544 | gfp_mask |= __GFP_HIGHMEM; | |
1545 | ||
1546 | #ifdef CONFIG_HUGETLB_PAGE | |
1547 | if (PageHuge(page)) { | |
1548 | struct hstate *h = page_hstate(page); | |
1549 | /* | |
1550 | * We don't want to dequeue from the pool because pool pages will | |
1551 | * mostly be from the CMA region. | |
1552 | */ | |
1553 | return alloc_migrate_huge_page(h, gfp_mask, nid, NULL); | |
1554 | } | |
1555 | #endif | |
1556 | if (PageTransHuge(page)) { | |
1557 | struct page *thp; | |
1558 | /* | |
1559 | * ignore allocation failure warnings | |
1560 | */ | |
1561 | gfp_t thp_gfpmask = GFP_TRANSHUGE | __GFP_NOWARN; | |
1562 | ||
1563 | /* | |
1564 | * Remove the movable mask so that we don't allocate from | |
1565 | * CMA area again. | |
1566 | */ | |
1567 | thp_gfpmask &= ~__GFP_MOVABLE; | |
1568 | thp = __alloc_pages_node(nid, thp_gfpmask, HPAGE_PMD_ORDER); | |
1569 | if (!thp) | |
1570 | return NULL; | |
1571 | prep_transhuge_page(thp); | |
1572 | return thp; | |
1573 | } | |
1574 | ||
1575 | return __alloc_pages_node(nid, gfp_mask, 0); | |
1576 | } | |
1577 | ||
932f4a63 IW |
1578 | static long check_and_migrate_cma_pages(struct task_struct *tsk, |
1579 | struct mm_struct *mm, | |
1580 | unsigned long start, | |
1581 | unsigned long nr_pages, | |
9a4e9f3b | 1582 | struct page **pages, |
932f4a63 IW |
1583 | struct vm_area_struct **vmas, |
1584 | unsigned int gup_flags) | |
9a4e9f3b | 1585 | { |
aa712399 PL |
1586 | unsigned long i; |
1587 | unsigned long step; | |
9a4e9f3b AK |
1588 | bool drain_allow = true; |
1589 | bool migrate_allow = true; | |
1590 | LIST_HEAD(cma_page_list); | |
b96cc655 | 1591 | long ret = nr_pages; |
9a4e9f3b AK |
1592 | |
1593 | check_again: | |
aa712399 PL |
1594 | for (i = 0; i < nr_pages;) { |
1595 | ||
1596 | struct page *head = compound_head(pages[i]); | |
1597 | ||
1598 | /* | |
1599 | * gup may start from a tail page. Advance step by the left | |
1600 | * part. | |
1601 | */ | |
d8c6546b | 1602 | step = compound_nr(head) - (pages[i] - head); |
9a4e9f3b AK |
1603 | /* |
1604 | * If we get a page from the CMA zone, since we are going to | |
1605 | * be pinning these entries, we might as well move them out | |
1606 | * of the CMA zone if possible. | |
1607 | */ | |
aa712399 PL |
1608 | if (is_migrate_cma_page(head)) { |
1609 | if (PageHuge(head)) | |
9a4e9f3b | 1610 | isolate_huge_page(head, &cma_page_list); |
aa712399 | 1611 | else { |
9a4e9f3b AK |
1612 | if (!PageLRU(head) && drain_allow) { |
1613 | lru_add_drain_all(); | |
1614 | drain_allow = false; | |
1615 | } | |
1616 | ||
1617 | if (!isolate_lru_page(head)) { | |
1618 | list_add_tail(&head->lru, &cma_page_list); | |
1619 | mod_node_page_state(page_pgdat(head), | |
1620 | NR_ISOLATED_ANON + | |
1621 | page_is_file_cache(head), | |
1622 | hpage_nr_pages(head)); | |
1623 | } | |
1624 | } | |
1625 | } | |
aa712399 PL |
1626 | |
1627 | i += step; | |
9a4e9f3b AK |
1628 | } |
1629 | ||
1630 | if (!list_empty(&cma_page_list)) { | |
1631 | /* | |
1632 | * drop the above get_user_pages reference. | |
1633 | */ | |
1634 | for (i = 0; i < nr_pages; i++) | |
1635 | put_page(pages[i]); | |
1636 | ||
1637 | if (migrate_pages(&cma_page_list, new_non_cma_page, | |
1638 | NULL, 0, MIGRATE_SYNC, MR_CONTIG_RANGE)) { | |
1639 | /* | |
1640 | * some of the pages failed migration. Do get_user_pages | |
1641 | * without migration. | |
1642 | */ | |
1643 | migrate_allow = false; | |
1644 | ||
1645 | if (!list_empty(&cma_page_list)) | |
1646 | putback_movable_pages(&cma_page_list); | |
1647 | } | |
1648 | /* | |
932f4a63 IW |
1649 | * We did migrate all the pages, Try to get the page references |
1650 | * again migrating any new CMA pages which we failed to isolate | |
1651 | * earlier. | |
9a4e9f3b | 1652 | */ |
b96cc655 | 1653 | ret = __get_user_pages_locked(tsk, mm, start, nr_pages, |
932f4a63 IW |
1654 | pages, vmas, NULL, |
1655 | gup_flags); | |
1656 | ||
b96cc655 | 1657 | if ((ret > 0) && migrate_allow) { |
1658 | nr_pages = ret; | |
9a4e9f3b AK |
1659 | drain_allow = true; |
1660 | goto check_again; | |
1661 | } | |
1662 | } | |
1663 | ||
b96cc655 | 1664 | return ret; |
9a4e9f3b AK |
1665 | } |
1666 | #else | |
932f4a63 IW |
1667 | static long check_and_migrate_cma_pages(struct task_struct *tsk, |
1668 | struct mm_struct *mm, | |
1669 | unsigned long start, | |
1670 | unsigned long nr_pages, | |
1671 | struct page **pages, | |
1672 | struct vm_area_struct **vmas, | |
1673 | unsigned int gup_flags) | |
9a4e9f3b AK |
1674 | { |
1675 | return nr_pages; | |
1676 | } | |
050a9adc | 1677 | #endif /* CONFIG_CMA */ |
9a4e9f3b | 1678 | |
2bb6d283 | 1679 | /* |
932f4a63 IW |
1680 | * __gup_longterm_locked() is a wrapper for __get_user_pages_locked which |
1681 | * allows us to process the FOLL_LONGTERM flag. | |
2bb6d283 | 1682 | */ |
932f4a63 IW |
1683 | static long __gup_longterm_locked(struct task_struct *tsk, |
1684 | struct mm_struct *mm, | |
1685 | unsigned long start, | |
1686 | unsigned long nr_pages, | |
1687 | struct page **pages, | |
1688 | struct vm_area_struct **vmas, | |
1689 | unsigned int gup_flags) | |
2bb6d283 | 1690 | { |
932f4a63 IW |
1691 | struct vm_area_struct **vmas_tmp = vmas; |
1692 | unsigned long flags = 0; | |
2bb6d283 DW |
1693 | long rc, i; |
1694 | ||
932f4a63 IW |
1695 | if (gup_flags & FOLL_LONGTERM) { |
1696 | if (!pages) | |
1697 | return -EINVAL; | |
1698 | ||
1699 | if (!vmas_tmp) { | |
1700 | vmas_tmp = kcalloc(nr_pages, | |
1701 | sizeof(struct vm_area_struct *), | |
1702 | GFP_KERNEL); | |
1703 | if (!vmas_tmp) | |
1704 | return -ENOMEM; | |
1705 | } | |
1706 | flags = memalloc_nocma_save(); | |
2bb6d283 DW |
1707 | } |
1708 | ||
932f4a63 IW |
1709 | rc = __get_user_pages_locked(tsk, mm, start, nr_pages, pages, |
1710 | vmas_tmp, NULL, gup_flags); | |
2bb6d283 | 1711 | |
932f4a63 IW |
1712 | if (gup_flags & FOLL_LONGTERM) { |
1713 | memalloc_nocma_restore(flags); | |
1714 | if (rc < 0) | |
1715 | goto out; | |
1716 | ||
1717 | if (check_dax_vmas(vmas_tmp, rc)) { | |
1718 | for (i = 0; i < rc; i++) | |
1719 | put_page(pages[i]); | |
1720 | rc = -EOPNOTSUPP; | |
1721 | goto out; | |
1722 | } | |
1723 | ||
1724 | rc = check_and_migrate_cma_pages(tsk, mm, start, rc, pages, | |
1725 | vmas_tmp, gup_flags); | |
9a4e9f3b | 1726 | } |
2bb6d283 | 1727 | |
2bb6d283 | 1728 | out: |
932f4a63 IW |
1729 | if (vmas_tmp != vmas) |
1730 | kfree(vmas_tmp); | |
2bb6d283 DW |
1731 | return rc; |
1732 | } | |
932f4a63 IW |
1733 | #else /* !CONFIG_FS_DAX && !CONFIG_CMA */ |
1734 | static __always_inline long __gup_longterm_locked(struct task_struct *tsk, | |
1735 | struct mm_struct *mm, | |
1736 | unsigned long start, | |
1737 | unsigned long nr_pages, | |
1738 | struct page **pages, | |
1739 | struct vm_area_struct **vmas, | |
1740 | unsigned int flags) | |
1741 | { | |
1742 | return __get_user_pages_locked(tsk, mm, start, nr_pages, pages, vmas, | |
1743 | NULL, flags); | |
1744 | } | |
1745 | #endif /* CONFIG_FS_DAX || CONFIG_CMA */ | |
1746 | ||
22bf29b6 JH |
1747 | #ifdef CONFIG_MMU |
1748 | static long __get_user_pages_remote(struct task_struct *tsk, | |
1749 | struct mm_struct *mm, | |
1750 | unsigned long start, unsigned long nr_pages, | |
1751 | unsigned int gup_flags, struct page **pages, | |
1752 | struct vm_area_struct **vmas, int *locked) | |
1753 | { | |
1754 | /* | |
1755 | * Parts of FOLL_LONGTERM behavior are incompatible with | |
1756 | * FAULT_FLAG_ALLOW_RETRY because of the FS DAX check requirement on | |
1757 | * vmas. However, this only comes up if locked is set, and there are | |
1758 | * callers that do request FOLL_LONGTERM, but do not set locked. So, | |
1759 | * allow what we can. | |
1760 | */ | |
1761 | if (gup_flags & FOLL_LONGTERM) { | |
1762 | if (WARN_ON_ONCE(locked)) | |
1763 | return -EINVAL; | |
1764 | /* | |
1765 | * This will check the vmas (even if our vmas arg is NULL) | |
1766 | * and return -ENOTSUPP if DAX isn't allowed in this case: | |
1767 | */ | |
1768 | return __gup_longterm_locked(tsk, mm, start, nr_pages, pages, | |
1769 | vmas, gup_flags | FOLL_TOUCH | | |
1770 | FOLL_REMOTE); | |
1771 | } | |
1772 | ||
1773 | return __get_user_pages_locked(tsk, mm, start, nr_pages, pages, vmas, | |
1774 | locked, | |
1775 | gup_flags | FOLL_TOUCH | FOLL_REMOTE); | |
1776 | } | |
1777 | ||
c4237f8b JH |
1778 | /* |
1779 | * get_user_pages_remote() - pin user pages in memory | |
1780 | * @tsk: the task_struct to use for page fault accounting, or | |
1781 | * NULL if faults are not to be recorded. | |
1782 | * @mm: mm_struct of target mm | |
1783 | * @start: starting user address | |
1784 | * @nr_pages: number of pages from start to pin | |
1785 | * @gup_flags: flags modifying lookup behaviour | |
1786 | * @pages: array that receives pointers to the pages pinned. | |
1787 | * Should be at least nr_pages long. Or NULL, if caller | |
1788 | * only intends to ensure the pages are faulted in. | |
1789 | * @vmas: array of pointers to vmas corresponding to each page. | |
1790 | * Or NULL if the caller does not require them. | |
1791 | * @locked: pointer to lock flag indicating whether lock is held and | |
1792 | * subsequently whether VM_FAULT_RETRY functionality can be | |
1793 | * utilised. Lock must initially be held. | |
1794 | * | |
1795 | * Returns either number of pages pinned (which may be less than the | |
1796 | * number requested), or an error. Details about the return value: | |
1797 | * | |
1798 | * -- If nr_pages is 0, returns 0. | |
1799 | * -- If nr_pages is >0, but no pages were pinned, returns -errno. | |
1800 | * -- If nr_pages is >0, and some pages were pinned, returns the number of | |
1801 | * pages pinned. Again, this may be less than nr_pages. | |
1802 | * | |
1803 | * The caller is responsible for releasing returned @pages, via put_page(). | |
1804 | * | |
1805 | * @vmas are valid only as long as mmap_sem is held. | |
1806 | * | |
1807 | * Must be called with mmap_sem held for read or write. | |
1808 | * | |
1809 | * get_user_pages walks a process's page tables and takes a reference to | |
1810 | * each struct page that each user address corresponds to at a given | |
1811 | * instant. That is, it takes the page that would be accessed if a user | |
1812 | * thread accesses the given user virtual address at that instant. | |
1813 | * | |
1814 | * This does not guarantee that the page exists in the user mappings when | |
1815 | * get_user_pages returns, and there may even be a completely different | |
1816 | * page there in some cases (eg. if mmapped pagecache has been invalidated | |
1817 | * and subsequently re faulted). However it does guarantee that the page | |
1818 | * won't be freed completely. And mostly callers simply care that the page | |
1819 | * contains data that was valid *at some point in time*. Typically, an IO | |
1820 | * or similar operation cannot guarantee anything stronger anyway because | |
1821 | * locks can't be held over the syscall boundary. | |
1822 | * | |
1823 | * If gup_flags & FOLL_WRITE == 0, the page must not be written to. If the page | |
1824 | * is written to, set_page_dirty (or set_page_dirty_lock, as appropriate) must | |
1825 | * be called after the page is finished with, and before put_page is called. | |
1826 | * | |
1827 | * get_user_pages is typically used for fewer-copy IO operations, to get a | |
1828 | * handle on the memory by some means other than accesses via the user virtual | |
1829 | * addresses. The pages may be submitted for DMA to devices or accessed via | |
1830 | * their kernel linear mapping (via the kmap APIs). Care should be taken to | |
1831 | * use the correct cache flushing APIs. | |
1832 | * | |
1833 | * See also get_user_pages_fast, for performance critical applications. | |
1834 | * | |
1835 | * get_user_pages should be phased out in favor of | |
1836 | * get_user_pages_locked|unlocked or get_user_pages_fast. Nothing | |
1837 | * should use get_user_pages because it cannot pass | |
1838 | * FAULT_FLAG_ALLOW_RETRY to handle_mm_fault. | |
1839 | */ | |
1840 | long get_user_pages_remote(struct task_struct *tsk, struct mm_struct *mm, | |
1841 | unsigned long start, unsigned long nr_pages, | |
1842 | unsigned int gup_flags, struct page **pages, | |
1843 | struct vm_area_struct **vmas, int *locked) | |
1844 | { | |
eddb1c22 JH |
1845 | /* |
1846 | * FOLL_PIN must only be set internally by the pin_user_pages*() APIs, | |
1847 | * never directly by the caller, so enforce that with an assertion: | |
1848 | */ | |
1849 | if (WARN_ON_ONCE(gup_flags & FOLL_PIN)) | |
1850 | return -EINVAL; | |
1851 | ||
22bf29b6 JH |
1852 | return __get_user_pages_remote(tsk, mm, start, nr_pages, gup_flags, |
1853 | pages, vmas, locked); | |
c4237f8b JH |
1854 | } |
1855 | EXPORT_SYMBOL(get_user_pages_remote); | |
1856 | ||
eddb1c22 JH |
1857 | #else /* CONFIG_MMU */ |
1858 | long get_user_pages_remote(struct task_struct *tsk, struct mm_struct *mm, | |
1859 | unsigned long start, unsigned long nr_pages, | |
1860 | unsigned int gup_flags, struct page **pages, | |
1861 | struct vm_area_struct **vmas, int *locked) | |
1862 | { | |
1863 | return 0; | |
1864 | } | |
3faa52c0 JH |
1865 | |
1866 | static long __get_user_pages_remote(struct task_struct *tsk, | |
1867 | struct mm_struct *mm, | |
1868 | unsigned long start, unsigned long nr_pages, | |
1869 | unsigned int gup_flags, struct page **pages, | |
1870 | struct vm_area_struct **vmas, int *locked) | |
1871 | { | |
1872 | return 0; | |
1873 | } | |
eddb1c22 JH |
1874 | #endif /* !CONFIG_MMU */ |
1875 | ||
932f4a63 IW |
1876 | /* |
1877 | * This is the same as get_user_pages_remote(), just with a | |
1878 | * less-flexible calling convention where we assume that the task | |
1879 | * and mm being operated on are the current task's and don't allow | |
1880 | * passing of a locked parameter. We also obviously don't pass | |
1881 | * FOLL_REMOTE in here. | |
1882 | */ | |
1883 | long get_user_pages(unsigned long start, unsigned long nr_pages, | |
1884 | unsigned int gup_flags, struct page **pages, | |
1885 | struct vm_area_struct **vmas) | |
1886 | { | |
eddb1c22 JH |
1887 | /* |
1888 | * FOLL_PIN must only be set internally by the pin_user_pages*() APIs, | |
1889 | * never directly by the caller, so enforce that with an assertion: | |
1890 | */ | |
1891 | if (WARN_ON_ONCE(gup_flags & FOLL_PIN)) | |
1892 | return -EINVAL; | |
1893 | ||
932f4a63 IW |
1894 | return __gup_longterm_locked(current, current->mm, start, nr_pages, |
1895 | pages, vmas, gup_flags | FOLL_TOUCH); | |
1896 | } | |
1897 | EXPORT_SYMBOL(get_user_pages); | |
2bb6d283 | 1898 | |
d3649f68 CH |
1899 | /* |
1900 | * We can leverage the VM_FAULT_RETRY functionality in the page fault | |
1901 | * paths better by using either get_user_pages_locked() or | |
1902 | * get_user_pages_unlocked(). | |
acc3c8d1 | 1903 | * |
d3649f68 | 1904 | * get_user_pages_locked() is suitable to replace the form: |
acc3c8d1 | 1905 | * |
d3649f68 CH |
1906 | * down_read(&mm->mmap_sem); |
1907 | * do_something() | |
1908 | * get_user_pages(tsk, mm, ..., pages, NULL); | |
1909 | * up_read(&mm->mmap_sem); | |
acc3c8d1 | 1910 | * |
d3649f68 | 1911 | * to: |
acc3c8d1 | 1912 | * |
d3649f68 CH |
1913 | * int locked = 1; |
1914 | * down_read(&mm->mmap_sem); | |
1915 | * do_something() | |
1916 | * get_user_pages_locked(tsk, mm, ..., pages, &locked); | |
1917 | * if (locked) | |
1918 | * up_read(&mm->mmap_sem); | |
acc3c8d1 | 1919 | */ |
d3649f68 CH |
1920 | long get_user_pages_locked(unsigned long start, unsigned long nr_pages, |
1921 | unsigned int gup_flags, struct page **pages, | |
1922 | int *locked) | |
acc3c8d1 | 1923 | { |
acc3c8d1 | 1924 | /* |
d3649f68 CH |
1925 | * FIXME: Current FOLL_LONGTERM behavior is incompatible with |
1926 | * FAULT_FLAG_ALLOW_RETRY because of the FS DAX check requirement on | |
1927 | * vmas. As there are no users of this flag in this call we simply | |
1928 | * disallow this option for now. | |
acc3c8d1 | 1929 | */ |
d3649f68 CH |
1930 | if (WARN_ON_ONCE(gup_flags & FOLL_LONGTERM)) |
1931 | return -EINVAL; | |
acc3c8d1 | 1932 | |
d3649f68 CH |
1933 | return __get_user_pages_locked(current, current->mm, start, nr_pages, |
1934 | pages, NULL, locked, | |
1935 | gup_flags | FOLL_TOUCH); | |
acc3c8d1 | 1936 | } |
d3649f68 | 1937 | EXPORT_SYMBOL(get_user_pages_locked); |
acc3c8d1 KS |
1938 | |
1939 | /* | |
d3649f68 | 1940 | * get_user_pages_unlocked() is suitable to replace the form: |
acc3c8d1 | 1941 | * |
d3649f68 CH |
1942 | * down_read(&mm->mmap_sem); |
1943 | * get_user_pages(tsk, mm, ..., pages, NULL); | |
1944 | * up_read(&mm->mmap_sem); | |
1945 | * | |
1946 | * with: | |
1947 | * | |
1948 | * get_user_pages_unlocked(tsk, mm, ..., pages); | |
1949 | * | |
1950 | * It is functionally equivalent to get_user_pages_fast so | |
1951 | * get_user_pages_fast should be used instead if specific gup_flags | |
1952 | * (e.g. FOLL_FORCE) are not required. | |
acc3c8d1 | 1953 | */ |
d3649f68 CH |
1954 | long get_user_pages_unlocked(unsigned long start, unsigned long nr_pages, |
1955 | struct page **pages, unsigned int gup_flags) | |
acc3c8d1 KS |
1956 | { |
1957 | struct mm_struct *mm = current->mm; | |
d3649f68 CH |
1958 | int locked = 1; |
1959 | long ret; | |
acc3c8d1 | 1960 | |
d3649f68 CH |
1961 | /* |
1962 | * FIXME: Current FOLL_LONGTERM behavior is incompatible with | |
1963 | * FAULT_FLAG_ALLOW_RETRY because of the FS DAX check requirement on | |
1964 | * vmas. As there are no users of this flag in this call we simply | |
1965 | * disallow this option for now. | |
1966 | */ | |
1967 | if (WARN_ON_ONCE(gup_flags & FOLL_LONGTERM)) | |
1968 | return -EINVAL; | |
acc3c8d1 | 1969 | |
d3649f68 CH |
1970 | down_read(&mm->mmap_sem); |
1971 | ret = __get_user_pages_locked(current, mm, start, nr_pages, pages, NULL, | |
1972 | &locked, gup_flags | FOLL_TOUCH); | |
acc3c8d1 KS |
1973 | if (locked) |
1974 | up_read(&mm->mmap_sem); | |
d3649f68 | 1975 | return ret; |
4bbd4c77 | 1976 | } |
d3649f68 | 1977 | EXPORT_SYMBOL(get_user_pages_unlocked); |
2667f50e SC |
1978 | |
1979 | /* | |
67a929e0 | 1980 | * Fast GUP |
2667f50e SC |
1981 | * |
1982 | * get_user_pages_fast attempts to pin user pages by walking the page | |
1983 | * tables directly and avoids taking locks. Thus the walker needs to be | |
1984 | * protected from page table pages being freed from under it, and should | |
1985 | * block any THP splits. | |
1986 | * | |
1987 | * One way to achieve this is to have the walker disable interrupts, and | |
1988 | * rely on IPIs from the TLB flushing code blocking before the page table | |
1989 | * pages are freed. This is unsuitable for architectures that do not need | |
1990 | * to broadcast an IPI when invalidating TLBs. | |
1991 | * | |
1992 | * Another way to achieve this is to batch up page table containing pages | |
1993 | * belonging to more than one mm_user, then rcu_sched a callback to free those | |
1994 | * pages. Disabling interrupts will allow the fast_gup walker to both block | |
1995 | * the rcu_sched callback, and an IPI that we broadcast for splitting THPs | |
1996 | * (which is a relatively rare event). The code below adopts this strategy. | |
1997 | * | |
1998 | * Before activating this code, please be aware that the following assumptions | |
1999 | * are currently made: | |
2000 | * | |
ff2e6d72 | 2001 | * *) Either MMU_GATHER_RCU_TABLE_FREE is enabled, and tlb_remove_table() is used to |
e585513b | 2002 | * free pages containing page tables or TLB flushing requires IPI broadcast. |
2667f50e | 2003 | * |
2667f50e SC |
2004 | * *) ptes can be read atomically by the architecture. |
2005 | * | |
2006 | * *) access_ok is sufficient to validate userspace address ranges. | |
2007 | * | |
2008 | * The last two assumptions can be relaxed by the addition of helper functions. | |
2009 | * | |
2010 | * This code is based heavily on the PowerPC implementation by Nick Piggin. | |
2011 | */ | |
67a929e0 | 2012 | #ifdef CONFIG_HAVE_FAST_GUP |
3faa52c0 JH |
2013 | |
2014 | static void put_compound_head(struct page *page, int refs, unsigned int flags) | |
2015 | { | |
47e29d32 JH |
2016 | if (flags & FOLL_PIN) { |
2017 | if (hpage_pincount_available(page)) | |
2018 | hpage_pincount_sub(page, refs); | |
2019 | else | |
2020 | refs *= GUP_PIN_COUNTING_BIAS; | |
2021 | } | |
3faa52c0 JH |
2022 | |
2023 | VM_BUG_ON_PAGE(page_ref_count(page) < refs, page); | |
2024 | /* | |
2025 | * Calling put_page() for each ref is unnecessarily slow. Only the last | |
2026 | * ref needs a put_page(). | |
2027 | */ | |
2028 | if (refs > 1) | |
2029 | page_ref_sub(page, refs - 1); | |
2030 | put_page(page); | |
2031 | } | |
2032 | ||
39656e83 | 2033 | #ifdef CONFIG_GUP_GET_PTE_LOW_HIGH |
3faa52c0 | 2034 | |
39656e83 CH |
2035 | /* |
2036 | * WARNING: only to be used in the get_user_pages_fast() implementation. | |
2037 | * | |
2038 | * With get_user_pages_fast(), we walk down the pagetables without taking any | |
2039 | * locks. For this we would like to load the pointers atomically, but sometimes | |
2040 | * that is not possible (e.g. without expensive cmpxchg8b on x86_32 PAE). What | |
2041 | * we do have is the guarantee that a PTE will only either go from not present | |
2042 | * to present, or present to not present or both -- it will not switch to a | |
2043 | * completely different present page without a TLB flush in between; something | |
2044 | * that we are blocking by holding interrupts off. | |
2045 | * | |
2046 | * Setting ptes from not present to present goes: | |
2047 | * | |
2048 | * ptep->pte_high = h; | |
2049 | * smp_wmb(); | |
2050 | * ptep->pte_low = l; | |
2051 | * | |
2052 | * And present to not present goes: | |
2053 | * | |
2054 | * ptep->pte_low = 0; | |
2055 | * smp_wmb(); | |
2056 | * ptep->pte_high = 0; | |
2057 | * | |
2058 | * We must ensure here that the load of pte_low sees 'l' IFF pte_high sees 'h'. | |
2059 | * We load pte_high *after* loading pte_low, which ensures we don't see an older | |
2060 | * value of pte_high. *Then* we recheck pte_low, which ensures that we haven't | |
2061 | * picked up a changed pte high. We might have gotten rubbish values from | |
2062 | * pte_low and pte_high, but we are guaranteed that pte_low will not have the | |
2063 | * present bit set *unless* it is 'l'. Because get_user_pages_fast() only | |
2064 | * operates on present ptes we're safe. | |
2065 | */ | |
2066 | static inline pte_t gup_get_pte(pte_t *ptep) | |
2067 | { | |
2068 | pte_t pte; | |
2667f50e | 2069 | |
39656e83 CH |
2070 | do { |
2071 | pte.pte_low = ptep->pte_low; | |
2072 | smp_rmb(); | |
2073 | pte.pte_high = ptep->pte_high; | |
2074 | smp_rmb(); | |
2075 | } while (unlikely(pte.pte_low != ptep->pte_low)); | |
2076 | ||
2077 | return pte; | |
2078 | } | |
2079 | #else /* CONFIG_GUP_GET_PTE_LOW_HIGH */ | |
0005d20b | 2080 | /* |
39656e83 | 2081 | * We require that the PTE can be read atomically. |
0005d20b KS |
2082 | */ |
2083 | static inline pte_t gup_get_pte(pte_t *ptep) | |
2084 | { | |
2085 | return READ_ONCE(*ptep); | |
2086 | } | |
39656e83 | 2087 | #endif /* CONFIG_GUP_GET_PTE_LOW_HIGH */ |
0005d20b | 2088 | |
790c7369 | 2089 | static void __maybe_unused undo_dev_pagemap(int *nr, int nr_start, |
3b78d834 | 2090 | unsigned int flags, |
790c7369 | 2091 | struct page **pages) |
b59f65fa KS |
2092 | { |
2093 | while ((*nr) - nr_start) { | |
2094 | struct page *page = pages[--(*nr)]; | |
2095 | ||
2096 | ClearPageReferenced(page); | |
3faa52c0 JH |
2097 | if (flags & FOLL_PIN) |
2098 | unpin_user_page(page); | |
2099 | else | |
2100 | put_page(page); | |
b59f65fa KS |
2101 | } |
2102 | } | |
2103 | ||
3010a5ea | 2104 | #ifdef CONFIG_ARCH_HAS_PTE_SPECIAL |
2667f50e | 2105 | static int gup_pte_range(pmd_t pmd, unsigned long addr, unsigned long end, |
b798bec4 | 2106 | unsigned int flags, struct page **pages, int *nr) |
2667f50e | 2107 | { |
b59f65fa KS |
2108 | struct dev_pagemap *pgmap = NULL; |
2109 | int nr_start = *nr, ret = 0; | |
2667f50e | 2110 | pte_t *ptep, *ptem; |
2667f50e SC |
2111 | |
2112 | ptem = ptep = pte_offset_map(&pmd, addr); | |
2113 | do { | |
0005d20b | 2114 | pte_t pte = gup_get_pte(ptep); |
7aef4172 | 2115 | struct page *head, *page; |
2667f50e SC |
2116 | |
2117 | /* | |
2118 | * Similar to the PMD case below, NUMA hinting must take slow | |
8a0516ed | 2119 | * path using the pte_protnone check. |
2667f50e | 2120 | */ |
e7884f8e KS |
2121 | if (pte_protnone(pte)) |
2122 | goto pte_unmap; | |
2123 | ||
b798bec4 | 2124 | if (!pte_access_permitted(pte, flags & FOLL_WRITE)) |
e7884f8e KS |
2125 | goto pte_unmap; |
2126 | ||
b59f65fa | 2127 | if (pte_devmap(pte)) { |
7af75561 IW |
2128 | if (unlikely(flags & FOLL_LONGTERM)) |
2129 | goto pte_unmap; | |
2130 | ||
b59f65fa KS |
2131 | pgmap = get_dev_pagemap(pte_pfn(pte), pgmap); |
2132 | if (unlikely(!pgmap)) { | |
3b78d834 | 2133 | undo_dev_pagemap(nr, nr_start, flags, pages); |
b59f65fa KS |
2134 | goto pte_unmap; |
2135 | } | |
2136 | } else if (pte_special(pte)) | |
2667f50e SC |
2137 | goto pte_unmap; |
2138 | ||
2139 | VM_BUG_ON(!pfn_valid(pte_pfn(pte))); | |
2140 | page = pte_page(pte); | |
2141 | ||
3faa52c0 | 2142 | head = try_grab_compound_head(page, 1, flags); |
8fde12ca | 2143 | if (!head) |
2667f50e SC |
2144 | goto pte_unmap; |
2145 | ||
2146 | if (unlikely(pte_val(pte) != pte_val(*ptep))) { | |
3faa52c0 | 2147 | put_compound_head(head, 1, flags); |
2667f50e SC |
2148 | goto pte_unmap; |
2149 | } | |
2150 | ||
7aef4172 | 2151 | VM_BUG_ON_PAGE(compound_head(page) != head, page); |
e9348053 KS |
2152 | |
2153 | SetPageReferenced(page); | |
2667f50e SC |
2154 | pages[*nr] = page; |
2155 | (*nr)++; | |
2156 | ||
2157 | } while (ptep++, addr += PAGE_SIZE, addr != end); | |
2158 | ||
2159 | ret = 1; | |
2160 | ||
2161 | pte_unmap: | |
832d7aa0 CH |
2162 | if (pgmap) |
2163 | put_dev_pagemap(pgmap); | |
2667f50e SC |
2164 | pte_unmap(ptem); |
2165 | return ret; | |
2166 | } | |
2167 | #else | |
2168 | ||
2169 | /* | |
2170 | * If we can't determine whether or not a pte is special, then fail immediately | |
2171 | * for ptes. Note, we can still pin HugeTLB and THP as these are guaranteed not | |
2172 | * to be special. | |
2173 | * | |
2174 | * For a futex to be placed on a THP tail page, get_futex_key requires a | |
2175 | * __get_user_pages_fast implementation that can pin pages. Thus it's still | |
2176 | * useful to have gup_huge_pmd even if we can't operate on ptes. | |
2177 | */ | |
2178 | static int gup_pte_range(pmd_t pmd, unsigned long addr, unsigned long end, | |
b798bec4 | 2179 | unsigned int flags, struct page **pages, int *nr) |
2667f50e SC |
2180 | { |
2181 | return 0; | |
2182 | } | |
3010a5ea | 2183 | #endif /* CONFIG_ARCH_HAS_PTE_SPECIAL */ |
2667f50e | 2184 | |
17596731 | 2185 | #if defined(CONFIG_ARCH_HAS_PTE_DEVMAP) && defined(CONFIG_TRANSPARENT_HUGEPAGE) |
b59f65fa | 2186 | static int __gup_device_huge(unsigned long pfn, unsigned long addr, |
86dfbed4 JH |
2187 | unsigned long end, unsigned int flags, |
2188 | struct page **pages, int *nr) | |
b59f65fa KS |
2189 | { |
2190 | int nr_start = *nr; | |
2191 | struct dev_pagemap *pgmap = NULL; | |
2192 | ||
2193 | do { | |
2194 | struct page *page = pfn_to_page(pfn); | |
2195 | ||
2196 | pgmap = get_dev_pagemap(pfn, pgmap); | |
2197 | if (unlikely(!pgmap)) { | |
3b78d834 | 2198 | undo_dev_pagemap(nr, nr_start, flags, pages); |
b59f65fa KS |
2199 | return 0; |
2200 | } | |
2201 | SetPageReferenced(page); | |
2202 | pages[*nr] = page; | |
3faa52c0 JH |
2203 | if (unlikely(!try_grab_page(page, flags))) { |
2204 | undo_dev_pagemap(nr, nr_start, flags, pages); | |
2205 | return 0; | |
2206 | } | |
b59f65fa KS |
2207 | (*nr)++; |
2208 | pfn++; | |
2209 | } while (addr += PAGE_SIZE, addr != end); | |
832d7aa0 CH |
2210 | |
2211 | if (pgmap) | |
2212 | put_dev_pagemap(pgmap); | |
b59f65fa KS |
2213 | return 1; |
2214 | } | |
2215 | ||
a9b6de77 | 2216 | static int __gup_device_huge_pmd(pmd_t orig, pmd_t *pmdp, unsigned long addr, |
86dfbed4 JH |
2217 | unsigned long end, unsigned int flags, |
2218 | struct page **pages, int *nr) | |
b59f65fa KS |
2219 | { |
2220 | unsigned long fault_pfn; | |
a9b6de77 DW |
2221 | int nr_start = *nr; |
2222 | ||
2223 | fault_pfn = pmd_pfn(orig) + ((addr & ~PMD_MASK) >> PAGE_SHIFT); | |
86dfbed4 | 2224 | if (!__gup_device_huge(fault_pfn, addr, end, flags, pages, nr)) |
a9b6de77 | 2225 | return 0; |
b59f65fa | 2226 | |
a9b6de77 | 2227 | if (unlikely(pmd_val(orig) != pmd_val(*pmdp))) { |
3b78d834 | 2228 | undo_dev_pagemap(nr, nr_start, flags, pages); |
a9b6de77 DW |
2229 | return 0; |
2230 | } | |
2231 | return 1; | |
b59f65fa KS |
2232 | } |
2233 | ||
a9b6de77 | 2234 | static int __gup_device_huge_pud(pud_t orig, pud_t *pudp, unsigned long addr, |
86dfbed4 JH |
2235 | unsigned long end, unsigned int flags, |
2236 | struct page **pages, int *nr) | |
b59f65fa KS |
2237 | { |
2238 | unsigned long fault_pfn; | |
a9b6de77 DW |
2239 | int nr_start = *nr; |
2240 | ||
2241 | fault_pfn = pud_pfn(orig) + ((addr & ~PUD_MASK) >> PAGE_SHIFT); | |
86dfbed4 | 2242 | if (!__gup_device_huge(fault_pfn, addr, end, flags, pages, nr)) |
a9b6de77 | 2243 | return 0; |
b59f65fa | 2244 | |
a9b6de77 | 2245 | if (unlikely(pud_val(orig) != pud_val(*pudp))) { |
3b78d834 | 2246 | undo_dev_pagemap(nr, nr_start, flags, pages); |
a9b6de77 DW |
2247 | return 0; |
2248 | } | |
2249 | return 1; | |
b59f65fa KS |
2250 | } |
2251 | #else | |
a9b6de77 | 2252 | static int __gup_device_huge_pmd(pmd_t orig, pmd_t *pmdp, unsigned long addr, |
86dfbed4 JH |
2253 | unsigned long end, unsigned int flags, |
2254 | struct page **pages, int *nr) | |
b59f65fa KS |
2255 | { |
2256 | BUILD_BUG(); | |
2257 | return 0; | |
2258 | } | |
2259 | ||
a9b6de77 | 2260 | static int __gup_device_huge_pud(pud_t pud, pud_t *pudp, unsigned long addr, |
86dfbed4 JH |
2261 | unsigned long end, unsigned int flags, |
2262 | struct page **pages, int *nr) | |
b59f65fa KS |
2263 | { |
2264 | BUILD_BUG(); | |
2265 | return 0; | |
2266 | } | |
2267 | #endif | |
2268 | ||
a43e9820 JH |
2269 | static int record_subpages(struct page *page, unsigned long addr, |
2270 | unsigned long end, struct page **pages) | |
2271 | { | |
2272 | int nr; | |
2273 | ||
2274 | for (nr = 0; addr != end; addr += PAGE_SIZE) | |
2275 | pages[nr++] = page++; | |
2276 | ||
2277 | return nr; | |
2278 | } | |
2279 | ||
cbd34da7 CH |
2280 | #ifdef CONFIG_ARCH_HAS_HUGEPD |
2281 | static unsigned long hugepte_addr_end(unsigned long addr, unsigned long end, | |
2282 | unsigned long sz) | |
2283 | { | |
2284 | unsigned long __boundary = (addr + sz) & ~(sz-1); | |
2285 | return (__boundary - 1 < end - 1) ? __boundary : end; | |
2286 | } | |
2287 | ||
2288 | static int gup_hugepte(pte_t *ptep, unsigned long sz, unsigned long addr, | |
0cd22afd JH |
2289 | unsigned long end, unsigned int flags, |
2290 | struct page **pages, int *nr) | |
cbd34da7 CH |
2291 | { |
2292 | unsigned long pte_end; | |
2293 | struct page *head, *page; | |
2294 | pte_t pte; | |
2295 | int refs; | |
2296 | ||
2297 | pte_end = (addr + sz) & ~(sz-1); | |
2298 | if (pte_end < end) | |
2299 | end = pte_end; | |
2300 | ||
2301 | pte = READ_ONCE(*ptep); | |
2302 | ||
0cd22afd | 2303 | if (!pte_access_permitted(pte, flags & FOLL_WRITE)) |
cbd34da7 CH |
2304 | return 0; |
2305 | ||
2306 | /* hugepages are never "special" */ | |
2307 | VM_BUG_ON(!pfn_valid(pte_pfn(pte))); | |
2308 | ||
cbd34da7 | 2309 | head = pte_page(pte); |
cbd34da7 | 2310 | page = head + ((addr & (sz-1)) >> PAGE_SHIFT); |
a43e9820 | 2311 | refs = record_subpages(page, addr, end, pages + *nr); |
cbd34da7 | 2312 | |
3faa52c0 | 2313 | head = try_grab_compound_head(head, refs, flags); |
a43e9820 | 2314 | if (!head) |
cbd34da7 | 2315 | return 0; |
cbd34da7 CH |
2316 | |
2317 | if (unlikely(pte_val(pte) != pte_val(*ptep))) { | |
3b78d834 | 2318 | put_compound_head(head, refs, flags); |
cbd34da7 CH |
2319 | return 0; |
2320 | } | |
2321 | ||
a43e9820 | 2322 | *nr += refs; |
520b4a44 | 2323 | SetPageReferenced(head); |
cbd34da7 CH |
2324 | return 1; |
2325 | } | |
2326 | ||
2327 | static int gup_huge_pd(hugepd_t hugepd, unsigned long addr, | |
0cd22afd | 2328 | unsigned int pdshift, unsigned long end, unsigned int flags, |
cbd34da7 CH |
2329 | struct page **pages, int *nr) |
2330 | { | |
2331 | pte_t *ptep; | |
2332 | unsigned long sz = 1UL << hugepd_shift(hugepd); | |
2333 | unsigned long next; | |
2334 | ||
2335 | ptep = hugepte_offset(hugepd, addr, pdshift); | |
2336 | do { | |
2337 | next = hugepte_addr_end(addr, end, sz); | |
0cd22afd | 2338 | if (!gup_hugepte(ptep, sz, addr, end, flags, pages, nr)) |
cbd34da7 CH |
2339 | return 0; |
2340 | } while (ptep++, addr = next, addr != end); | |
2341 | ||
2342 | return 1; | |
2343 | } | |
2344 | #else | |
2345 | static inline int gup_huge_pd(hugepd_t hugepd, unsigned long addr, | |
0cd22afd | 2346 | unsigned int pdshift, unsigned long end, unsigned int flags, |
cbd34da7 CH |
2347 | struct page **pages, int *nr) |
2348 | { | |
2349 | return 0; | |
2350 | } | |
2351 | #endif /* CONFIG_ARCH_HAS_HUGEPD */ | |
2352 | ||
2667f50e | 2353 | static int gup_huge_pmd(pmd_t orig, pmd_t *pmdp, unsigned long addr, |
0cd22afd JH |
2354 | unsigned long end, unsigned int flags, |
2355 | struct page **pages, int *nr) | |
2667f50e | 2356 | { |
ddc58f27 | 2357 | struct page *head, *page; |
2667f50e SC |
2358 | int refs; |
2359 | ||
b798bec4 | 2360 | if (!pmd_access_permitted(orig, flags & FOLL_WRITE)) |
2667f50e SC |
2361 | return 0; |
2362 | ||
7af75561 IW |
2363 | if (pmd_devmap(orig)) { |
2364 | if (unlikely(flags & FOLL_LONGTERM)) | |
2365 | return 0; | |
86dfbed4 JH |
2366 | return __gup_device_huge_pmd(orig, pmdp, addr, end, flags, |
2367 | pages, nr); | |
7af75561 | 2368 | } |
b59f65fa | 2369 | |
d63206ee | 2370 | page = pmd_page(orig) + ((addr & ~PMD_MASK) >> PAGE_SHIFT); |
a43e9820 | 2371 | refs = record_subpages(page, addr, end, pages + *nr); |
2667f50e | 2372 | |
3faa52c0 | 2373 | head = try_grab_compound_head(pmd_page(orig), refs, flags); |
a43e9820 | 2374 | if (!head) |
2667f50e | 2375 | return 0; |
2667f50e SC |
2376 | |
2377 | if (unlikely(pmd_val(orig) != pmd_val(*pmdp))) { | |
3b78d834 | 2378 | put_compound_head(head, refs, flags); |
2667f50e SC |
2379 | return 0; |
2380 | } | |
2381 | ||
a43e9820 | 2382 | *nr += refs; |
e9348053 | 2383 | SetPageReferenced(head); |
2667f50e SC |
2384 | return 1; |
2385 | } | |
2386 | ||
2387 | static int gup_huge_pud(pud_t orig, pud_t *pudp, unsigned long addr, | |
86dfbed4 JH |
2388 | unsigned long end, unsigned int flags, |
2389 | struct page **pages, int *nr) | |
2667f50e | 2390 | { |
ddc58f27 | 2391 | struct page *head, *page; |
2667f50e SC |
2392 | int refs; |
2393 | ||
b798bec4 | 2394 | if (!pud_access_permitted(orig, flags & FOLL_WRITE)) |
2667f50e SC |
2395 | return 0; |
2396 | ||
7af75561 IW |
2397 | if (pud_devmap(orig)) { |
2398 | if (unlikely(flags & FOLL_LONGTERM)) | |
2399 | return 0; | |
86dfbed4 JH |
2400 | return __gup_device_huge_pud(orig, pudp, addr, end, flags, |
2401 | pages, nr); | |
7af75561 | 2402 | } |
b59f65fa | 2403 | |
d63206ee | 2404 | page = pud_page(orig) + ((addr & ~PUD_MASK) >> PAGE_SHIFT); |
a43e9820 | 2405 | refs = record_subpages(page, addr, end, pages + *nr); |
2667f50e | 2406 | |
3faa52c0 | 2407 | head = try_grab_compound_head(pud_page(orig), refs, flags); |
a43e9820 | 2408 | if (!head) |
2667f50e | 2409 | return 0; |
2667f50e SC |
2410 | |
2411 | if (unlikely(pud_val(orig) != pud_val(*pudp))) { | |
3b78d834 | 2412 | put_compound_head(head, refs, flags); |
2667f50e SC |
2413 | return 0; |
2414 | } | |
2415 | ||
a43e9820 | 2416 | *nr += refs; |
e9348053 | 2417 | SetPageReferenced(head); |
2667f50e SC |
2418 | return 1; |
2419 | } | |
2420 | ||
f30c59e9 | 2421 | static int gup_huge_pgd(pgd_t orig, pgd_t *pgdp, unsigned long addr, |
b798bec4 | 2422 | unsigned long end, unsigned int flags, |
f30c59e9 AK |
2423 | struct page **pages, int *nr) |
2424 | { | |
2425 | int refs; | |
ddc58f27 | 2426 | struct page *head, *page; |
f30c59e9 | 2427 | |
b798bec4 | 2428 | if (!pgd_access_permitted(orig, flags & FOLL_WRITE)) |
f30c59e9 AK |
2429 | return 0; |
2430 | ||
b59f65fa | 2431 | BUILD_BUG_ON(pgd_devmap(orig)); |
a43e9820 | 2432 | |
d63206ee | 2433 | page = pgd_page(orig) + ((addr & ~PGDIR_MASK) >> PAGE_SHIFT); |
a43e9820 | 2434 | refs = record_subpages(page, addr, end, pages + *nr); |
f30c59e9 | 2435 | |
3faa52c0 | 2436 | head = try_grab_compound_head(pgd_page(orig), refs, flags); |
a43e9820 | 2437 | if (!head) |
f30c59e9 | 2438 | return 0; |
f30c59e9 AK |
2439 | |
2440 | if (unlikely(pgd_val(orig) != pgd_val(*pgdp))) { | |
3b78d834 | 2441 | put_compound_head(head, refs, flags); |
f30c59e9 AK |
2442 | return 0; |
2443 | } | |
2444 | ||
a43e9820 | 2445 | *nr += refs; |
e9348053 | 2446 | SetPageReferenced(head); |
f30c59e9 AK |
2447 | return 1; |
2448 | } | |
2449 | ||
2667f50e | 2450 | static int gup_pmd_range(pud_t pud, unsigned long addr, unsigned long end, |
b798bec4 | 2451 | unsigned int flags, struct page **pages, int *nr) |
2667f50e SC |
2452 | { |
2453 | unsigned long next; | |
2454 | pmd_t *pmdp; | |
2455 | ||
2456 | pmdp = pmd_offset(&pud, addr); | |
2457 | do { | |
38c5ce93 | 2458 | pmd_t pmd = READ_ONCE(*pmdp); |
2667f50e SC |
2459 | |
2460 | next = pmd_addr_end(addr, end); | |
84c3fc4e | 2461 | if (!pmd_present(pmd)) |
2667f50e SC |
2462 | return 0; |
2463 | ||
414fd080 YZ |
2464 | if (unlikely(pmd_trans_huge(pmd) || pmd_huge(pmd) || |
2465 | pmd_devmap(pmd))) { | |
2667f50e SC |
2466 | /* |
2467 | * NUMA hinting faults need to be handled in the GUP | |
2468 | * slowpath for accounting purposes and so that they | |
2469 | * can be serialised against THP migration. | |
2470 | */ | |
8a0516ed | 2471 | if (pmd_protnone(pmd)) |
2667f50e SC |
2472 | return 0; |
2473 | ||
b798bec4 | 2474 | if (!gup_huge_pmd(pmd, pmdp, addr, next, flags, |
2667f50e SC |
2475 | pages, nr)) |
2476 | return 0; | |
2477 | ||
f30c59e9 AK |
2478 | } else if (unlikely(is_hugepd(__hugepd(pmd_val(pmd))))) { |
2479 | /* | |
2480 | * architecture have different format for hugetlbfs | |
2481 | * pmd format and THP pmd format | |
2482 | */ | |
2483 | if (!gup_huge_pd(__hugepd(pmd_val(pmd)), addr, | |
b798bec4 | 2484 | PMD_SHIFT, next, flags, pages, nr)) |
f30c59e9 | 2485 | return 0; |
b798bec4 | 2486 | } else if (!gup_pte_range(pmd, addr, next, flags, pages, nr)) |
2923117b | 2487 | return 0; |
2667f50e SC |
2488 | } while (pmdp++, addr = next, addr != end); |
2489 | ||
2490 | return 1; | |
2491 | } | |
2492 | ||
c2febafc | 2493 | static int gup_pud_range(p4d_t p4d, unsigned long addr, unsigned long end, |
b798bec4 | 2494 | unsigned int flags, struct page **pages, int *nr) |
2667f50e SC |
2495 | { |
2496 | unsigned long next; | |
2497 | pud_t *pudp; | |
2498 | ||
c2febafc | 2499 | pudp = pud_offset(&p4d, addr); |
2667f50e | 2500 | do { |
e37c6982 | 2501 | pud_t pud = READ_ONCE(*pudp); |
2667f50e SC |
2502 | |
2503 | next = pud_addr_end(addr, end); | |
15494520 | 2504 | if (unlikely(!pud_present(pud))) |
2667f50e | 2505 | return 0; |
f30c59e9 | 2506 | if (unlikely(pud_huge(pud))) { |
b798bec4 | 2507 | if (!gup_huge_pud(pud, pudp, addr, next, flags, |
f30c59e9 AK |
2508 | pages, nr)) |
2509 | return 0; | |
2510 | } else if (unlikely(is_hugepd(__hugepd(pud_val(pud))))) { | |
2511 | if (!gup_huge_pd(__hugepd(pud_val(pud)), addr, | |
b798bec4 | 2512 | PUD_SHIFT, next, flags, pages, nr)) |
2667f50e | 2513 | return 0; |
b798bec4 | 2514 | } else if (!gup_pmd_range(pud, addr, next, flags, pages, nr)) |
2667f50e SC |
2515 | return 0; |
2516 | } while (pudp++, addr = next, addr != end); | |
2517 | ||
2518 | return 1; | |
2519 | } | |
2520 | ||
c2febafc | 2521 | static int gup_p4d_range(pgd_t pgd, unsigned long addr, unsigned long end, |
b798bec4 | 2522 | unsigned int flags, struct page **pages, int *nr) |
c2febafc KS |
2523 | { |
2524 | unsigned long next; | |
2525 | p4d_t *p4dp; | |
2526 | ||
2527 | p4dp = p4d_offset(&pgd, addr); | |
2528 | do { | |
2529 | p4d_t p4d = READ_ONCE(*p4dp); | |
2530 | ||
2531 | next = p4d_addr_end(addr, end); | |
2532 | if (p4d_none(p4d)) | |
2533 | return 0; | |
2534 | BUILD_BUG_ON(p4d_huge(p4d)); | |
2535 | if (unlikely(is_hugepd(__hugepd(p4d_val(p4d))))) { | |
2536 | if (!gup_huge_pd(__hugepd(p4d_val(p4d)), addr, | |
b798bec4 | 2537 | P4D_SHIFT, next, flags, pages, nr)) |
c2febafc | 2538 | return 0; |
b798bec4 | 2539 | } else if (!gup_pud_range(p4d, addr, next, flags, pages, nr)) |
c2febafc KS |
2540 | return 0; |
2541 | } while (p4dp++, addr = next, addr != end); | |
2542 | ||
2543 | return 1; | |
2544 | } | |
2545 | ||
5b65c467 | 2546 | static void gup_pgd_range(unsigned long addr, unsigned long end, |
b798bec4 | 2547 | unsigned int flags, struct page **pages, int *nr) |
5b65c467 KS |
2548 | { |
2549 | unsigned long next; | |
2550 | pgd_t *pgdp; | |
2551 | ||
2552 | pgdp = pgd_offset(current->mm, addr); | |
2553 | do { | |
2554 | pgd_t pgd = READ_ONCE(*pgdp); | |
2555 | ||
2556 | next = pgd_addr_end(addr, end); | |
2557 | if (pgd_none(pgd)) | |
2558 | return; | |
2559 | if (unlikely(pgd_huge(pgd))) { | |
b798bec4 | 2560 | if (!gup_huge_pgd(pgd, pgdp, addr, next, flags, |
5b65c467 KS |
2561 | pages, nr)) |
2562 | return; | |
2563 | } else if (unlikely(is_hugepd(__hugepd(pgd_val(pgd))))) { | |
2564 | if (!gup_huge_pd(__hugepd(pgd_val(pgd)), addr, | |
b798bec4 | 2565 | PGDIR_SHIFT, next, flags, pages, nr)) |
5b65c467 | 2566 | return; |
b798bec4 | 2567 | } else if (!gup_p4d_range(pgd, addr, next, flags, pages, nr)) |
5b65c467 KS |
2568 | return; |
2569 | } while (pgdp++, addr = next, addr != end); | |
2570 | } | |
050a9adc CH |
2571 | #else |
2572 | static inline void gup_pgd_range(unsigned long addr, unsigned long end, | |
2573 | unsigned int flags, struct page **pages, int *nr) | |
2574 | { | |
2575 | } | |
2576 | #endif /* CONFIG_HAVE_FAST_GUP */ | |
5b65c467 KS |
2577 | |
2578 | #ifndef gup_fast_permitted | |
2579 | /* | |
2580 | * Check if it's allowed to use __get_user_pages_fast() for the range, or | |
2581 | * we need to fall back to the slow version: | |
2582 | */ | |
26f4c328 | 2583 | static bool gup_fast_permitted(unsigned long start, unsigned long end) |
5b65c467 | 2584 | { |
26f4c328 | 2585 | return true; |
5b65c467 KS |
2586 | } |
2587 | #endif | |
2588 | ||
2667f50e SC |
2589 | /* |
2590 | * Like get_user_pages_fast() except it's IRQ-safe in that it won't fall back to | |
d0811078 MT |
2591 | * the regular GUP. |
2592 | * Note a difference with get_user_pages_fast: this always returns the | |
2593 | * number of pages pinned, 0 if no pages were pinned. | |
050a9adc CH |
2594 | * |
2595 | * If the architecture does not support this function, simply return with no | |
2596 | * pages pinned. | |
2667f50e SC |
2597 | */ |
2598 | int __get_user_pages_fast(unsigned long start, int nr_pages, int write, | |
2599 | struct page **pages) | |
2600 | { | |
d4faa402 | 2601 | unsigned long len, end; |
5b65c467 | 2602 | unsigned long flags; |
2667f50e | 2603 | int nr = 0; |
94202f12 JH |
2604 | /* |
2605 | * Internally (within mm/gup.c), gup fast variants must set FOLL_GET, | |
2606 | * because gup fast is always a "pin with a +1 page refcount" request. | |
2607 | */ | |
2608 | unsigned int gup_flags = FOLL_GET; | |
2609 | ||
2610 | if (write) | |
2611 | gup_flags |= FOLL_WRITE; | |
2667f50e | 2612 | |
f455c854 | 2613 | start = untagged_addr(start) & PAGE_MASK; |
2667f50e SC |
2614 | len = (unsigned long) nr_pages << PAGE_SHIFT; |
2615 | end = start + len; | |
2616 | ||
26f4c328 CH |
2617 | if (end <= start) |
2618 | return 0; | |
96d4f267 | 2619 | if (unlikely(!access_ok((void __user *)start, len))) |
2667f50e SC |
2620 | return 0; |
2621 | ||
2622 | /* | |
2623 | * Disable interrupts. We use the nested form as we can already have | |
2624 | * interrupts disabled by get_futex_key. | |
2625 | * | |
2626 | * With interrupts disabled, we block page table pages from being | |
2ebe8228 FW |
2627 | * freed from under us. See struct mmu_table_batch comments in |
2628 | * include/asm-generic/tlb.h for more details. | |
2667f50e SC |
2629 | * |
2630 | * We do not adopt an rcu_read_lock(.) here as we also want to | |
2631 | * block IPIs that come from THPs splitting. | |
2632 | */ | |
2633 | ||
050a9adc CH |
2634 | if (IS_ENABLED(CONFIG_HAVE_FAST_GUP) && |
2635 | gup_fast_permitted(start, end)) { | |
5b65c467 | 2636 | local_irq_save(flags); |
94202f12 | 2637 | gup_pgd_range(start, end, gup_flags, pages, &nr); |
5b65c467 KS |
2638 | local_irq_restore(flags); |
2639 | } | |
2667f50e SC |
2640 | |
2641 | return nr; | |
2642 | } | |
050a9adc | 2643 | EXPORT_SYMBOL_GPL(__get_user_pages_fast); |
2667f50e | 2644 | |
7af75561 IW |
2645 | static int __gup_longterm_unlocked(unsigned long start, int nr_pages, |
2646 | unsigned int gup_flags, struct page **pages) | |
2647 | { | |
2648 | int ret; | |
2649 | ||
2650 | /* | |
2651 | * FIXME: FOLL_LONGTERM does not work with | |
2652 | * get_user_pages_unlocked() (see comments in that function) | |
2653 | */ | |
2654 | if (gup_flags & FOLL_LONGTERM) { | |
2655 | down_read(¤t->mm->mmap_sem); | |
2656 | ret = __gup_longterm_locked(current, current->mm, | |
2657 | start, nr_pages, | |
2658 | pages, NULL, gup_flags); | |
2659 | up_read(¤t->mm->mmap_sem); | |
2660 | } else { | |
2661 | ret = get_user_pages_unlocked(start, nr_pages, | |
2662 | pages, gup_flags); | |
2663 | } | |
2664 | ||
2665 | return ret; | |
2666 | } | |
2667 | ||
eddb1c22 JH |
2668 | static int internal_get_user_pages_fast(unsigned long start, int nr_pages, |
2669 | unsigned int gup_flags, | |
2670 | struct page **pages) | |
2667f50e | 2671 | { |
5b65c467 | 2672 | unsigned long addr, len, end; |
73e10a61 | 2673 | int nr = 0, ret = 0; |
2667f50e | 2674 | |
f4000fdf | 2675 | if (WARN_ON_ONCE(gup_flags & ~(FOLL_WRITE | FOLL_LONGTERM | |
94202f12 | 2676 | FOLL_FORCE | FOLL_PIN | FOLL_GET))) |
817be129 CH |
2677 | return -EINVAL; |
2678 | ||
f455c854 | 2679 | start = untagged_addr(start) & PAGE_MASK; |
5b65c467 KS |
2680 | addr = start; |
2681 | len = (unsigned long) nr_pages << PAGE_SHIFT; | |
2682 | end = start + len; | |
2683 | ||
26f4c328 | 2684 | if (end <= start) |
c61611f7 | 2685 | return 0; |
96d4f267 | 2686 | if (unlikely(!access_ok((void __user *)start, len))) |
c61611f7 | 2687 | return -EFAULT; |
73e10a61 | 2688 | |
050a9adc CH |
2689 | if (IS_ENABLED(CONFIG_HAVE_FAST_GUP) && |
2690 | gup_fast_permitted(start, end)) { | |
5b65c467 | 2691 | local_irq_disable(); |
73b0140b | 2692 | gup_pgd_range(addr, end, gup_flags, pages, &nr); |
5b65c467 | 2693 | local_irq_enable(); |
73e10a61 KS |
2694 | ret = nr; |
2695 | } | |
2667f50e SC |
2696 | |
2697 | if (nr < nr_pages) { | |
2698 | /* Try to get the remaining pages with get_user_pages */ | |
2699 | start += nr << PAGE_SHIFT; | |
2700 | pages += nr; | |
2701 | ||
7af75561 IW |
2702 | ret = __gup_longterm_unlocked(start, nr_pages - nr, |
2703 | gup_flags, pages); | |
2667f50e SC |
2704 | |
2705 | /* Have to be a bit careful with return values */ | |
2706 | if (nr > 0) { | |
2707 | if (ret < 0) | |
2708 | ret = nr; | |
2709 | else | |
2710 | ret += nr; | |
2711 | } | |
2712 | } | |
2713 | ||
2714 | return ret; | |
2715 | } | |
eddb1c22 JH |
2716 | |
2717 | /** | |
2718 | * get_user_pages_fast() - pin user pages in memory | |
3faa52c0 JH |
2719 | * @start: starting user address |
2720 | * @nr_pages: number of pages from start to pin | |
2721 | * @gup_flags: flags modifying pin behaviour | |
2722 | * @pages: array that receives pointers to the pages pinned. | |
2723 | * Should be at least nr_pages long. | |
eddb1c22 JH |
2724 | * |
2725 | * Attempt to pin user pages in memory without taking mm->mmap_sem. | |
2726 | * If not successful, it will fall back to taking the lock and | |
2727 | * calling get_user_pages(). | |
2728 | * | |
2729 | * Returns number of pages pinned. This may be fewer than the number requested. | |
2730 | * If nr_pages is 0 or negative, returns 0. If no pages were pinned, returns | |
2731 | * -errno. | |
2732 | */ | |
2733 | int get_user_pages_fast(unsigned long start, int nr_pages, | |
2734 | unsigned int gup_flags, struct page **pages) | |
2735 | { | |
2736 | /* | |
2737 | * FOLL_PIN must only be set internally by the pin_user_pages*() APIs, | |
2738 | * never directly by the caller, so enforce that: | |
2739 | */ | |
2740 | if (WARN_ON_ONCE(gup_flags & FOLL_PIN)) | |
2741 | return -EINVAL; | |
2742 | ||
94202f12 JH |
2743 | /* |
2744 | * The caller may or may not have explicitly set FOLL_GET; either way is | |
2745 | * OK. However, internally (within mm/gup.c), gup fast variants must set | |
2746 | * FOLL_GET, because gup fast is always a "pin with a +1 page refcount" | |
2747 | * request. | |
2748 | */ | |
2749 | gup_flags |= FOLL_GET; | |
eddb1c22 JH |
2750 | return internal_get_user_pages_fast(start, nr_pages, gup_flags, pages); |
2751 | } | |
050a9adc | 2752 | EXPORT_SYMBOL_GPL(get_user_pages_fast); |
eddb1c22 JH |
2753 | |
2754 | /** | |
2755 | * pin_user_pages_fast() - pin user pages in memory without taking locks | |
2756 | * | |
3faa52c0 JH |
2757 | * @start: starting user address |
2758 | * @nr_pages: number of pages from start to pin | |
2759 | * @gup_flags: flags modifying pin behaviour | |
2760 | * @pages: array that receives pointers to the pages pinned. | |
2761 | * Should be at least nr_pages long. | |
2762 | * | |
2763 | * Nearly the same as get_user_pages_fast(), except that FOLL_PIN is set. See | |
2764 | * get_user_pages_fast() for documentation on the function arguments, because | |
2765 | * the arguments here are identical. | |
2766 | * | |
2767 | * FOLL_PIN means that the pages must be released via unpin_user_page(). Please | |
2768 | * see Documentation/vm/pin_user_pages.rst for further details. | |
eddb1c22 JH |
2769 | * |
2770 | * This is intended for Case 1 (DIO) in Documentation/vm/pin_user_pages.rst. It | |
2771 | * is NOT intended for Case 2 (RDMA: long-term pins). | |
2772 | */ | |
2773 | int pin_user_pages_fast(unsigned long start, int nr_pages, | |
2774 | unsigned int gup_flags, struct page **pages) | |
2775 | { | |
3faa52c0 JH |
2776 | /* FOLL_GET and FOLL_PIN are mutually exclusive. */ |
2777 | if (WARN_ON_ONCE(gup_flags & FOLL_GET)) | |
2778 | return -EINVAL; | |
2779 | ||
2780 | gup_flags |= FOLL_PIN; | |
2781 | return internal_get_user_pages_fast(start, nr_pages, gup_flags, pages); | |
eddb1c22 JH |
2782 | } |
2783 | EXPORT_SYMBOL_GPL(pin_user_pages_fast); | |
2784 | ||
2785 | /** | |
2786 | * pin_user_pages_remote() - pin pages of a remote process (task != current) | |
2787 | * | |
3faa52c0 JH |
2788 | * @tsk: the task_struct to use for page fault accounting, or |
2789 | * NULL if faults are not to be recorded. | |
2790 | * @mm: mm_struct of target mm | |
2791 | * @start: starting user address | |
2792 | * @nr_pages: number of pages from start to pin | |
2793 | * @gup_flags: flags modifying lookup behaviour | |
2794 | * @pages: array that receives pointers to the pages pinned. | |
2795 | * Should be at least nr_pages long. Or NULL, if caller | |
2796 | * only intends to ensure the pages are faulted in. | |
2797 | * @vmas: array of pointers to vmas corresponding to each page. | |
2798 | * Or NULL if the caller does not require them. | |
2799 | * @locked: pointer to lock flag indicating whether lock is held and | |
2800 | * subsequently whether VM_FAULT_RETRY functionality can be | |
2801 | * utilised. Lock must initially be held. | |
2802 | * | |
2803 | * Nearly the same as get_user_pages_remote(), except that FOLL_PIN is set. See | |
2804 | * get_user_pages_remote() for documentation on the function arguments, because | |
2805 | * the arguments here are identical. | |
2806 | * | |
2807 | * FOLL_PIN means that the pages must be released via unpin_user_page(). Please | |
2808 | * see Documentation/vm/pin_user_pages.rst for details. | |
eddb1c22 JH |
2809 | * |
2810 | * This is intended for Case 1 (DIO) in Documentation/vm/pin_user_pages.rst. It | |
2811 | * is NOT intended for Case 2 (RDMA: long-term pins). | |
2812 | */ | |
2813 | long pin_user_pages_remote(struct task_struct *tsk, struct mm_struct *mm, | |
2814 | unsigned long start, unsigned long nr_pages, | |
2815 | unsigned int gup_flags, struct page **pages, | |
2816 | struct vm_area_struct **vmas, int *locked) | |
2817 | { | |
3faa52c0 JH |
2818 | /* FOLL_GET and FOLL_PIN are mutually exclusive. */ |
2819 | if (WARN_ON_ONCE(gup_flags & FOLL_GET)) | |
2820 | return -EINVAL; | |
2821 | ||
2822 | gup_flags |= FOLL_PIN; | |
2823 | return __get_user_pages_remote(tsk, mm, start, nr_pages, gup_flags, | |
2824 | pages, vmas, locked); | |
eddb1c22 JH |
2825 | } |
2826 | EXPORT_SYMBOL(pin_user_pages_remote); | |
2827 | ||
2828 | /** | |
2829 | * pin_user_pages() - pin user pages in memory for use by other devices | |
2830 | * | |
3faa52c0 JH |
2831 | * @start: starting user address |
2832 | * @nr_pages: number of pages from start to pin | |
2833 | * @gup_flags: flags modifying lookup behaviour | |
2834 | * @pages: array that receives pointers to the pages pinned. | |
2835 | * Should be at least nr_pages long. Or NULL, if caller | |
2836 | * only intends to ensure the pages are faulted in. | |
2837 | * @vmas: array of pointers to vmas corresponding to each page. | |
2838 | * Or NULL if the caller does not require them. | |
2839 | * | |
2840 | * Nearly the same as get_user_pages(), except that FOLL_TOUCH is not set, and | |
2841 | * FOLL_PIN is set. | |
2842 | * | |
2843 | * FOLL_PIN means that the pages must be released via unpin_user_page(). Please | |
2844 | * see Documentation/vm/pin_user_pages.rst for details. | |
eddb1c22 JH |
2845 | * |
2846 | * This is intended for Case 1 (DIO) in Documentation/vm/pin_user_pages.rst. It | |
2847 | * is NOT intended for Case 2 (RDMA: long-term pins). | |
2848 | */ | |
2849 | long pin_user_pages(unsigned long start, unsigned long nr_pages, | |
2850 | unsigned int gup_flags, struct page **pages, | |
2851 | struct vm_area_struct **vmas) | |
2852 | { | |
3faa52c0 JH |
2853 | /* FOLL_GET and FOLL_PIN are mutually exclusive. */ |
2854 | if (WARN_ON_ONCE(gup_flags & FOLL_GET)) | |
2855 | return -EINVAL; | |
2856 | ||
2857 | gup_flags |= FOLL_PIN; | |
2858 | return __gup_longterm_locked(current, current->mm, start, nr_pages, | |
2859 | pages, vmas, gup_flags); | |
eddb1c22 JH |
2860 | } |
2861 | EXPORT_SYMBOL(pin_user_pages); |