x86/mm/gup: Simplify get_user_pages() PTE bit handling
[deliverable/linux.git] / mm / gup.c
CommitLineData
cde70140 1#define __DISABLE_GUP_DEPRECATED 1
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
2667f50e
SC
14#include <linux/sched.h>
15#include <linux/rwsem.h>
f30c59e9 16#include <linux/hugetlb.h>
1027e443 17
2667f50e 18#include <asm/pgtable.h>
1027e443 19#include <asm/tlbflush.h>
2667f50e 20
4bbd4c77
KS
21#include "internal.h"
22
69e68b4f
KS
23static struct page *no_page_table(struct vm_area_struct *vma,
24 unsigned int flags)
4bbd4c77 25{
69e68b4f
KS
26 /*
27 * When core dumping an enormous anonymous area that nobody
28 * has touched so far, we don't want to allocate unnecessary pages or
29 * page tables. Return error instead of NULL to skip handle_mm_fault,
30 * then get_dump_page() will return NULL to leave a hole in the dump.
31 * But we can only make this optimization where a hole would surely
32 * be zero-filled if handle_mm_fault() actually did handle it.
33 */
34 if ((flags & FOLL_DUMP) && (!vma->vm_ops || !vma->vm_ops->fault))
35 return ERR_PTR(-EFAULT);
36 return NULL;
37}
4bbd4c77 38
1027e443
KS
39static int follow_pfn_pte(struct vm_area_struct *vma, unsigned long address,
40 pte_t *pte, unsigned int flags)
41{
42 /* No page to get reference */
43 if (flags & FOLL_GET)
44 return -EFAULT;
45
46 if (flags & FOLL_TOUCH) {
47 pte_t entry = *pte;
48
49 if (flags & FOLL_WRITE)
50 entry = pte_mkdirty(entry);
51 entry = pte_mkyoung(entry);
52
53 if (!pte_same(*pte, entry)) {
54 set_pte_at(vma->vm_mm, address, pte, entry);
55 update_mmu_cache(vma, address, pte);
56 }
57 }
58
59 /* Proper page table entry exists, but no corresponding struct page */
60 return -EEXIST;
61}
62
69e68b4f
KS
63static struct page *follow_page_pte(struct vm_area_struct *vma,
64 unsigned long address, pmd_t *pmd, unsigned int flags)
65{
66 struct mm_struct *mm = vma->vm_mm;
3565fce3 67 struct dev_pagemap *pgmap = NULL;
69e68b4f
KS
68 struct page *page;
69 spinlock_t *ptl;
70 pte_t *ptep, pte;
4bbd4c77 71
69e68b4f 72retry:
4bbd4c77 73 if (unlikely(pmd_bad(*pmd)))
69e68b4f 74 return no_page_table(vma, flags);
4bbd4c77
KS
75
76 ptep = pte_offset_map_lock(mm, pmd, address, &ptl);
4bbd4c77
KS
77 pte = *ptep;
78 if (!pte_present(pte)) {
79 swp_entry_t entry;
80 /*
81 * KSM's break_ksm() relies upon recognizing a ksm page
82 * even while it is being migrated, so for that case we
83 * need migration_entry_wait().
84 */
85 if (likely(!(flags & FOLL_MIGRATION)))
86 goto no_page;
0661a336 87 if (pte_none(pte))
4bbd4c77
KS
88 goto no_page;
89 entry = pte_to_swp_entry(pte);
90 if (!is_migration_entry(entry))
91 goto no_page;
92 pte_unmap_unlock(ptep, ptl);
93 migration_entry_wait(mm, pmd, address);
69e68b4f 94 goto retry;
4bbd4c77 95 }
8a0516ed 96 if ((flags & FOLL_NUMA) && pte_protnone(pte))
4bbd4c77 97 goto no_page;
69e68b4f
KS
98 if ((flags & FOLL_WRITE) && !pte_write(pte)) {
99 pte_unmap_unlock(ptep, ptl);
100 return NULL;
101 }
4bbd4c77
KS
102
103 page = vm_normal_page(vma, address, pte);
3565fce3
DW
104 if (!page && pte_devmap(pte) && (flags & FOLL_GET)) {
105 /*
106 * Only return device mapping pages in the FOLL_GET case since
107 * they are only valid while holding the pgmap reference.
108 */
109 pgmap = get_dev_pagemap(pte_pfn(pte), NULL);
110 if (pgmap)
111 page = pte_page(pte);
112 else
113 goto no_page;
114 } else if (unlikely(!page)) {
1027e443
KS
115 if (flags & FOLL_DUMP) {
116 /* Avoid special (like zero) pages in core dumps */
117 page = ERR_PTR(-EFAULT);
118 goto out;
119 }
120
121 if (is_zero_pfn(pte_pfn(pte))) {
122 page = pte_page(pte);
123 } else {
124 int ret;
125
126 ret = follow_pfn_pte(vma, address, ptep, flags);
127 page = ERR_PTR(ret);
128 goto out;
129 }
4bbd4c77
KS
130 }
131
6742d293
KS
132 if (flags & FOLL_SPLIT && PageTransCompound(page)) {
133 int ret;
134 get_page(page);
135 pte_unmap_unlock(ptep, ptl);
136 lock_page(page);
137 ret = split_huge_page(page);
138 unlock_page(page);
139 put_page(page);
140 if (ret)
141 return ERR_PTR(ret);
142 goto retry;
143 }
144
3565fce3 145 if (flags & FOLL_GET) {
ddc58f27 146 get_page(page);
3565fce3
DW
147
148 /* drop the pgmap reference now that we hold the page */
149 if (pgmap) {
150 put_dev_pagemap(pgmap);
151 pgmap = NULL;
152 }
153 }
4bbd4c77
KS
154 if (flags & FOLL_TOUCH) {
155 if ((flags & FOLL_WRITE) &&
156 !pte_dirty(pte) && !PageDirty(page))
157 set_page_dirty(page);
158 /*
159 * pte_mkyoung() would be more correct here, but atomic care
160 * is needed to avoid losing the dirty bit: it is easier to use
161 * mark_page_accessed().
162 */
163 mark_page_accessed(page);
164 }
de60f5f1 165 if ((flags & FOLL_MLOCK) && (vma->vm_flags & VM_LOCKED)) {
e90309c9
KS
166 /* Do not mlock pte-mapped THP */
167 if (PageTransCompound(page))
168 goto out;
169
4bbd4c77
KS
170 /*
171 * The preliminary mapping check is mainly to avoid the
172 * pointless overhead of lock_page on the ZERO_PAGE
173 * which might bounce very badly if there is contention.
174 *
175 * If the page is already locked, we don't need to
176 * handle it now - vmscan will handle it later if and
177 * when it attempts to reclaim the page.
178 */
179 if (page->mapping && trylock_page(page)) {
180 lru_add_drain(); /* push cached pages to LRU */
181 /*
182 * Because we lock page here, and migration is
183 * blocked by the pte's page reference, and we
184 * know the page is still mapped, we don't even
185 * need to check for file-cache page truncation.
186 */
187 mlock_vma_page(page);
188 unlock_page(page);
189 }
190 }
1027e443 191out:
4bbd4c77 192 pte_unmap_unlock(ptep, ptl);
4bbd4c77 193 return page;
4bbd4c77
KS
194no_page:
195 pte_unmap_unlock(ptep, ptl);
196 if (!pte_none(pte))
69e68b4f
KS
197 return NULL;
198 return no_page_table(vma, flags);
199}
200
201/**
202 * follow_page_mask - look up a page descriptor from a user-virtual address
203 * @vma: vm_area_struct mapping @address
204 * @address: virtual address to look up
205 * @flags: flags modifying lookup behaviour
206 * @page_mask: on output, *page_mask is set according to the size of the page
207 *
208 * @flags can have FOLL_ flags set, defined in <linux/mm.h>
209 *
210 * Returns the mapped (struct page *), %NULL if no mapping exists, or
211 * an error pointer if there is a mapping to something not represented
212 * by a page descriptor (see also vm_normal_page()).
213 */
214struct page *follow_page_mask(struct vm_area_struct *vma,
215 unsigned long address, unsigned int flags,
216 unsigned int *page_mask)
217{
218 pgd_t *pgd;
219 pud_t *pud;
220 pmd_t *pmd;
221 spinlock_t *ptl;
222 struct page *page;
223 struct mm_struct *mm = vma->vm_mm;
224
225 *page_mask = 0;
226
227 page = follow_huge_addr(mm, address, flags & FOLL_WRITE);
228 if (!IS_ERR(page)) {
229 BUG_ON(flags & FOLL_GET);
4bbd4c77 230 return page;
69e68b4f 231 }
4bbd4c77 232
69e68b4f
KS
233 pgd = pgd_offset(mm, address);
234 if (pgd_none(*pgd) || unlikely(pgd_bad(*pgd)))
235 return no_page_table(vma, flags);
236
237 pud = pud_offset(pgd, address);
238 if (pud_none(*pud))
239 return no_page_table(vma, flags);
240 if (pud_huge(*pud) && vma->vm_flags & VM_HUGETLB) {
e66f17ff
NH
241 page = follow_huge_pud(mm, address, pud, flags);
242 if (page)
243 return page;
244 return no_page_table(vma, flags);
69e68b4f
KS
245 }
246 if (unlikely(pud_bad(*pud)))
247 return no_page_table(vma, flags);
248
249 pmd = pmd_offset(pud, address);
250 if (pmd_none(*pmd))
251 return no_page_table(vma, flags);
252 if (pmd_huge(*pmd) && vma->vm_flags & VM_HUGETLB) {
e66f17ff
NH
253 page = follow_huge_pmd(mm, address, pmd, flags);
254 if (page)
255 return page;
256 return no_page_table(vma, flags);
69e68b4f 257 }
8a0516ed 258 if ((flags & FOLL_NUMA) && pmd_protnone(*pmd))
69e68b4f 259 return no_page_table(vma, flags);
3565fce3
DW
260 if (pmd_devmap(*pmd)) {
261 ptl = pmd_lock(mm, pmd);
262 page = follow_devmap_pmd(vma, address, pmd, flags);
263 spin_unlock(ptl);
264 if (page)
265 return page;
266 }
6742d293
KS
267 if (likely(!pmd_trans_huge(*pmd)))
268 return follow_page_pte(vma, address, pmd, flags);
269
270 ptl = pmd_lock(mm, pmd);
271 if (unlikely(!pmd_trans_huge(*pmd))) {
272 spin_unlock(ptl);
273 return follow_page_pte(vma, address, pmd, flags);
274 }
6742d293
KS
275 if (flags & FOLL_SPLIT) {
276 int ret;
277 page = pmd_page(*pmd);
278 if (is_huge_zero_page(page)) {
279 spin_unlock(ptl);
280 ret = 0;
78ddc534 281 split_huge_pmd(vma, pmd, address);
6742d293
KS
282 } else {
283 get_page(page);
69e68b4f 284 spin_unlock(ptl);
6742d293
KS
285 lock_page(page);
286 ret = split_huge_page(page);
287 unlock_page(page);
288 put_page(page);
289 }
290
291 return ret ? ERR_PTR(ret) :
292 follow_page_pte(vma, address, pmd, flags);
69e68b4f 293 }
6742d293
KS
294
295 page = follow_trans_huge_pmd(vma, address, pmd, flags);
296 spin_unlock(ptl);
297 *page_mask = HPAGE_PMD_NR - 1;
298 return page;
4bbd4c77
KS
299}
300
f2b495ca
KS
301static int get_gate_page(struct mm_struct *mm, unsigned long address,
302 unsigned int gup_flags, struct vm_area_struct **vma,
303 struct page **page)
304{
305 pgd_t *pgd;
306 pud_t *pud;
307 pmd_t *pmd;
308 pte_t *pte;
309 int ret = -EFAULT;
310
311 /* user gate pages are read-only */
312 if (gup_flags & FOLL_WRITE)
313 return -EFAULT;
314 if (address > TASK_SIZE)
315 pgd = pgd_offset_k(address);
316 else
317 pgd = pgd_offset_gate(mm, address);
318 BUG_ON(pgd_none(*pgd));
319 pud = pud_offset(pgd, address);
320 BUG_ON(pud_none(*pud));
321 pmd = pmd_offset(pud, address);
322 if (pmd_none(*pmd))
323 return -EFAULT;
324 VM_BUG_ON(pmd_trans_huge(*pmd));
325 pte = pte_offset_map(pmd, address);
326 if (pte_none(*pte))
327 goto unmap;
328 *vma = get_gate_vma(mm);
329 if (!page)
330 goto out;
331 *page = vm_normal_page(*vma, address, *pte);
332 if (!*page) {
333 if ((gup_flags & FOLL_DUMP) || !is_zero_pfn(pte_pfn(*pte)))
334 goto unmap;
335 *page = pte_page(*pte);
336 }
337 get_page(*page);
338out:
339 ret = 0;
340unmap:
341 pte_unmap(pte);
342 return ret;
343}
344
9a95f3cf
PC
345/*
346 * mmap_sem must be held on entry. If @nonblocking != NULL and
347 * *@flags does not include FOLL_NOWAIT, the mmap_sem may be released.
348 * If it is, *@nonblocking will be set to 0 and -EBUSY returned.
349 */
16744483
KS
350static int faultin_page(struct task_struct *tsk, struct vm_area_struct *vma,
351 unsigned long address, unsigned int *flags, int *nonblocking)
352{
353 struct mm_struct *mm = vma->vm_mm;
354 unsigned int fault_flags = 0;
355 int ret;
356
de60f5f1
EM
357 /* mlock all present pages, but do not fault in new pages */
358 if ((*flags & (FOLL_POPULATE | FOLL_MLOCK)) == FOLL_MLOCK)
359 return -ENOENT;
84d33df2
KS
360 /* For mm_populate(), just skip the stack guard page. */
361 if ((*flags & FOLL_POPULATE) &&
16744483
KS
362 (stack_guard_page_start(vma, address) ||
363 stack_guard_page_end(vma, address + PAGE_SIZE)))
364 return -ENOENT;
365 if (*flags & FOLL_WRITE)
366 fault_flags |= FAULT_FLAG_WRITE;
367 if (nonblocking)
368 fault_flags |= FAULT_FLAG_ALLOW_RETRY;
369 if (*flags & FOLL_NOWAIT)
370 fault_flags |= FAULT_FLAG_ALLOW_RETRY | FAULT_FLAG_RETRY_NOWAIT;
234b239b
ALC
371 if (*flags & FOLL_TRIED) {
372 VM_WARN_ON_ONCE(fault_flags & FAULT_FLAG_ALLOW_RETRY);
373 fault_flags |= FAULT_FLAG_TRIED;
374 }
16744483
KS
375
376 ret = handle_mm_fault(mm, vma, address, fault_flags);
377 if (ret & VM_FAULT_ERROR) {
378 if (ret & VM_FAULT_OOM)
379 return -ENOMEM;
380 if (ret & (VM_FAULT_HWPOISON | VM_FAULT_HWPOISON_LARGE))
381 return *flags & FOLL_HWPOISON ? -EHWPOISON : -EFAULT;
33692f27 382 if (ret & (VM_FAULT_SIGBUS | VM_FAULT_SIGSEGV))
16744483
KS
383 return -EFAULT;
384 BUG();
385 }
386
387 if (tsk) {
388 if (ret & VM_FAULT_MAJOR)
389 tsk->maj_flt++;
390 else
391 tsk->min_flt++;
392 }
393
394 if (ret & VM_FAULT_RETRY) {
395 if (nonblocking)
396 *nonblocking = 0;
397 return -EBUSY;
398 }
399
400 /*
401 * The VM_FAULT_WRITE bit tells us that do_wp_page has broken COW when
402 * necessary, even if maybe_mkwrite decided not to set pte_write. We
403 * can thus safely do subsequent page lookups as if they were reads.
404 * But only do so when looping for pte_write is futile: in some cases
405 * userspace may also be wanting to write to the gotten user page,
406 * which a read fault here might prevent (a readonly page might get
407 * reCOWed by userspace write).
408 */
409 if ((ret & VM_FAULT_WRITE) && !(vma->vm_flags & VM_WRITE))
410 *flags &= ~FOLL_WRITE;
411 return 0;
412}
413
fa5bb209
KS
414static int check_vma_flags(struct vm_area_struct *vma, unsigned long gup_flags)
415{
416 vm_flags_t vm_flags = vma->vm_flags;
417
418 if (vm_flags & (VM_IO | VM_PFNMAP))
419 return -EFAULT;
420
421 if (gup_flags & FOLL_WRITE) {
422 if (!(vm_flags & VM_WRITE)) {
423 if (!(gup_flags & FOLL_FORCE))
424 return -EFAULT;
425 /*
426 * We used to let the write,force case do COW in a
427 * VM_MAYWRITE VM_SHARED !VM_WRITE vma, so ptrace could
428 * set a breakpoint in a read-only mapping of an
429 * executable, without corrupting the file (yet only
430 * when that file had been opened for writing!).
431 * Anon pages in shared mappings are surprising: now
432 * just reject it.
433 */
46435364 434 if (!is_cow_mapping(vm_flags))
fa5bb209 435 return -EFAULT;
fa5bb209
KS
436 }
437 } else if (!(vm_flags & VM_READ)) {
438 if (!(gup_flags & FOLL_FORCE))
439 return -EFAULT;
440 /*
441 * Is there actually any vma we can reach here which does not
442 * have VM_MAYREAD set?
443 */
444 if (!(vm_flags & VM_MAYREAD))
445 return -EFAULT;
446 }
447 return 0;
448}
449
4bbd4c77
KS
450/**
451 * __get_user_pages() - pin user pages in memory
452 * @tsk: task_struct of target task
453 * @mm: mm_struct of target mm
454 * @start: starting user address
455 * @nr_pages: number of pages from start to pin
456 * @gup_flags: flags modifying pin behaviour
457 * @pages: array that receives pointers to the pages pinned.
458 * Should be at least nr_pages long. Or NULL, if caller
459 * only intends to ensure the pages are faulted in.
460 * @vmas: array of pointers to vmas corresponding to each page.
461 * Or NULL if the caller does not require them.
462 * @nonblocking: whether waiting for disk IO or mmap_sem contention
463 *
464 * Returns number of pages pinned. This may be fewer than the number
465 * requested. If nr_pages is 0 or negative, returns 0. If no pages
466 * were pinned, returns -errno. Each page returned must be released
467 * with a put_page() call when it is finished with. vmas will only
468 * remain valid while mmap_sem is held.
469 *
9a95f3cf 470 * Must be called with mmap_sem held. It may be released. See below.
4bbd4c77
KS
471 *
472 * __get_user_pages walks a process's page tables and takes a reference to
473 * each struct page that each user address corresponds to at a given
474 * instant. That is, it takes the page that would be accessed if a user
475 * thread accesses the given user virtual address at that instant.
476 *
477 * This does not guarantee that the page exists in the user mappings when
478 * __get_user_pages returns, and there may even be a completely different
479 * page there in some cases (eg. if mmapped pagecache has been invalidated
480 * and subsequently re faulted). However it does guarantee that the page
481 * won't be freed completely. And mostly callers simply care that the page
482 * contains data that was valid *at some point in time*. Typically, an IO
483 * or similar operation cannot guarantee anything stronger anyway because
484 * locks can't be held over the syscall boundary.
485 *
486 * If @gup_flags & FOLL_WRITE == 0, the page must not be written to. If
487 * the page is written to, set_page_dirty (or set_page_dirty_lock, as
488 * appropriate) must be called after the page is finished with, and
489 * before put_page is called.
490 *
491 * If @nonblocking != NULL, __get_user_pages will not wait for disk IO
492 * or mmap_sem contention, and if waiting is needed to pin all pages,
9a95f3cf
PC
493 * *@nonblocking will be set to 0. Further, if @gup_flags does not
494 * include FOLL_NOWAIT, the mmap_sem will be released via up_read() in
495 * this case.
496 *
497 * A caller using such a combination of @nonblocking and @gup_flags
498 * must therefore hold the mmap_sem for reading only, and recognize
499 * when it's been released. Otherwise, it must be held for either
500 * reading or writing and will not be released.
4bbd4c77
KS
501 *
502 * In most cases, get_user_pages or get_user_pages_fast should be used
503 * instead of __get_user_pages. __get_user_pages should be used only if
504 * you need some special @gup_flags.
505 */
506long __get_user_pages(struct task_struct *tsk, struct mm_struct *mm,
507 unsigned long start, unsigned long nr_pages,
508 unsigned int gup_flags, struct page **pages,
509 struct vm_area_struct **vmas, int *nonblocking)
510{
fa5bb209 511 long i = 0;
4bbd4c77 512 unsigned int page_mask;
fa5bb209 513 struct vm_area_struct *vma = NULL;
4bbd4c77
KS
514
515 if (!nr_pages)
516 return 0;
517
518 VM_BUG_ON(!!pages != !!(gup_flags & FOLL_GET));
519
520 /*
521 * If FOLL_FORCE is set then do not force a full fault as the hinting
522 * fault information is unrelated to the reference behaviour of a task
523 * using the address space
524 */
525 if (!(gup_flags & FOLL_FORCE))
526 gup_flags |= FOLL_NUMA;
527
4bbd4c77 528 do {
fa5bb209
KS
529 struct page *page;
530 unsigned int foll_flags = gup_flags;
531 unsigned int page_increm;
532
533 /* first iteration or cross vma bound */
534 if (!vma || start >= vma->vm_end) {
535 vma = find_extend_vma(mm, start);
536 if (!vma && in_gate_area(mm, start)) {
537 int ret;
538 ret = get_gate_page(mm, start & PAGE_MASK,
539 gup_flags, &vma,
540 pages ? &pages[i] : NULL);
541 if (ret)
542 return i ? : ret;
543 page_mask = 0;
544 goto next_page;
545 }
4bbd4c77 546
fa5bb209
KS
547 if (!vma || check_vma_flags(vma, gup_flags))
548 return i ? : -EFAULT;
549 if (is_vm_hugetlb_page(vma)) {
550 i = follow_hugetlb_page(mm, vma, pages, vmas,
551 &start, &nr_pages, i,
552 gup_flags);
553 continue;
4bbd4c77 554 }
fa5bb209
KS
555 }
556retry:
557 /*
558 * If we have a pending SIGKILL, don't keep faulting pages and
559 * potentially allocating memory.
560 */
561 if (unlikely(fatal_signal_pending(current)))
562 return i ? i : -ERESTARTSYS;
563 cond_resched();
564 page = follow_page_mask(vma, start, foll_flags, &page_mask);
565 if (!page) {
566 int ret;
567 ret = faultin_page(tsk, vma, start, &foll_flags,
568 nonblocking);
569 switch (ret) {
570 case 0:
571 goto retry;
572 case -EFAULT:
573 case -ENOMEM:
574 case -EHWPOISON:
575 return i ? i : ret;
576 case -EBUSY:
577 return i;
578 case -ENOENT:
579 goto next_page;
4bbd4c77 580 }
fa5bb209 581 BUG();
1027e443
KS
582 } else if (PTR_ERR(page) == -EEXIST) {
583 /*
584 * Proper page table entry exists, but no corresponding
585 * struct page.
586 */
587 goto next_page;
588 } else if (IS_ERR(page)) {
fa5bb209 589 return i ? i : PTR_ERR(page);
1027e443 590 }
fa5bb209
KS
591 if (pages) {
592 pages[i] = page;
593 flush_anon_page(vma, page, start);
594 flush_dcache_page(page);
595 page_mask = 0;
4bbd4c77 596 }
4bbd4c77 597next_page:
fa5bb209
KS
598 if (vmas) {
599 vmas[i] = vma;
600 page_mask = 0;
601 }
602 page_increm = 1 + (~(start >> PAGE_SHIFT) & page_mask);
603 if (page_increm > nr_pages)
604 page_increm = nr_pages;
605 i += page_increm;
606 start += page_increm * PAGE_SIZE;
607 nr_pages -= page_increm;
4bbd4c77
KS
608 } while (nr_pages);
609 return i;
4bbd4c77
KS
610}
611EXPORT_SYMBOL(__get_user_pages);
612
d4925e00
DH
613bool vma_permits_fault(struct vm_area_struct *vma, unsigned int fault_flags)
614{
615 vm_flags_t vm_flags;
616
617 vm_flags = (fault_flags & FAULT_FLAG_WRITE) ? VM_WRITE : VM_READ;
618
619 if (!(vm_flags & vma->vm_flags))
620 return false;
621
622 return true;
623}
624
4bbd4c77
KS
625/*
626 * fixup_user_fault() - manually resolve a user page fault
627 * @tsk: the task_struct to use for page fault accounting, or
628 * NULL if faults are not to be recorded.
629 * @mm: mm_struct of target mm
630 * @address: user address
631 * @fault_flags:flags to pass down to handle_mm_fault()
4a9e1cda
DD
632 * @unlocked: did we unlock the mmap_sem while retrying, maybe NULL if caller
633 * does not allow retry
4bbd4c77
KS
634 *
635 * This is meant to be called in the specific scenario where for locking reasons
636 * we try to access user memory in atomic context (within a pagefault_disable()
637 * section), this returns -EFAULT, and we want to resolve the user fault before
638 * trying again.
639 *
640 * Typically this is meant to be used by the futex code.
641 *
642 * The main difference with get_user_pages() is that this function will
643 * unconditionally call handle_mm_fault() which will in turn perform all the
644 * necessary SW fixup of the dirty and young bits in the PTE, while
4a9e1cda 645 * get_user_pages() only guarantees to update these in the struct page.
4bbd4c77
KS
646 *
647 * This is important for some architectures where those bits also gate the
648 * access permission to the page because they are maintained in software. On
649 * such architectures, gup() will not be enough to make a subsequent access
650 * succeed.
651 *
4a9e1cda
DD
652 * This function will not return with an unlocked mmap_sem. So it has not the
653 * same semantics wrt the @mm->mmap_sem as does filemap_fault().
4bbd4c77
KS
654 */
655int fixup_user_fault(struct task_struct *tsk, struct mm_struct *mm,
4a9e1cda
DD
656 unsigned long address, unsigned int fault_flags,
657 bool *unlocked)
4bbd4c77
KS
658{
659 struct vm_area_struct *vma;
4a9e1cda
DD
660 int ret, major = 0;
661
662 if (unlocked)
663 fault_flags |= FAULT_FLAG_ALLOW_RETRY;
4bbd4c77 664
4a9e1cda 665retry:
4bbd4c77
KS
666 vma = find_extend_vma(mm, address);
667 if (!vma || address < vma->vm_start)
668 return -EFAULT;
669
d4925e00 670 if (!vma_permits_fault(vma, fault_flags))
4bbd4c77
KS
671 return -EFAULT;
672
673 ret = handle_mm_fault(mm, vma, address, fault_flags);
4a9e1cda 674 major |= ret & VM_FAULT_MAJOR;
4bbd4c77
KS
675 if (ret & VM_FAULT_ERROR) {
676 if (ret & VM_FAULT_OOM)
677 return -ENOMEM;
678 if (ret & (VM_FAULT_HWPOISON | VM_FAULT_HWPOISON_LARGE))
679 return -EHWPOISON;
33692f27 680 if (ret & (VM_FAULT_SIGBUS | VM_FAULT_SIGSEGV))
4bbd4c77
KS
681 return -EFAULT;
682 BUG();
683 }
4a9e1cda
DD
684
685 if (ret & VM_FAULT_RETRY) {
686 down_read(&mm->mmap_sem);
687 if (!(fault_flags & FAULT_FLAG_TRIED)) {
688 *unlocked = true;
689 fault_flags &= ~FAULT_FLAG_ALLOW_RETRY;
690 fault_flags |= FAULT_FLAG_TRIED;
691 goto retry;
692 }
693 }
694
4bbd4c77 695 if (tsk) {
4a9e1cda 696 if (major)
4bbd4c77
KS
697 tsk->maj_flt++;
698 else
699 tsk->min_flt++;
700 }
701 return 0;
702}
703
f0818f47
AA
704static __always_inline long __get_user_pages_locked(struct task_struct *tsk,
705 struct mm_struct *mm,
706 unsigned long start,
707 unsigned long nr_pages,
708 int write, int force,
709 struct page **pages,
710 struct vm_area_struct **vmas,
0fd71a56
AA
711 int *locked, bool notify_drop,
712 unsigned int flags)
f0818f47 713{
f0818f47
AA
714 long ret, pages_done;
715 bool lock_dropped;
716
717 if (locked) {
718 /* if VM_FAULT_RETRY can be returned, vmas become invalid */
719 BUG_ON(vmas);
720 /* check caller initialized locked */
721 BUG_ON(*locked != 1);
722 }
723
724 if (pages)
725 flags |= FOLL_GET;
726 if (write)
727 flags |= FOLL_WRITE;
728 if (force)
729 flags |= FOLL_FORCE;
730
731 pages_done = 0;
732 lock_dropped = false;
733 for (;;) {
734 ret = __get_user_pages(tsk, mm, start, nr_pages, flags, pages,
735 vmas, locked);
736 if (!locked)
737 /* VM_FAULT_RETRY couldn't trigger, bypass */
738 return ret;
739
740 /* VM_FAULT_RETRY cannot return errors */
741 if (!*locked) {
742 BUG_ON(ret < 0);
743 BUG_ON(ret >= nr_pages);
744 }
745
746 if (!pages)
747 /* If it's a prefault don't insist harder */
748 return ret;
749
750 if (ret > 0) {
751 nr_pages -= ret;
752 pages_done += ret;
753 if (!nr_pages)
754 break;
755 }
756 if (*locked) {
757 /* VM_FAULT_RETRY didn't trigger */
758 if (!pages_done)
759 pages_done = ret;
760 break;
761 }
762 /* VM_FAULT_RETRY triggered, so seek to the faulting offset */
763 pages += ret;
764 start += ret << PAGE_SHIFT;
765
766 /*
767 * Repeat on the address that fired VM_FAULT_RETRY
768 * without FAULT_FLAG_ALLOW_RETRY but with
769 * FAULT_FLAG_TRIED.
770 */
771 *locked = 1;
772 lock_dropped = true;
773 down_read(&mm->mmap_sem);
774 ret = __get_user_pages(tsk, mm, start, 1, flags | FOLL_TRIED,
775 pages, NULL, NULL);
776 if (ret != 1) {
777 BUG_ON(ret > 1);
778 if (!pages_done)
779 pages_done = ret;
780 break;
781 }
782 nr_pages--;
783 pages_done++;
784 if (!nr_pages)
785 break;
786 pages++;
787 start += PAGE_SIZE;
788 }
789 if (notify_drop && lock_dropped && *locked) {
790 /*
791 * We must let the caller know we temporarily dropped the lock
792 * and so the critical section protected by it was lost.
793 */
794 up_read(&mm->mmap_sem);
795 *locked = 0;
796 }
797 return pages_done;
798}
799
800/*
801 * We can leverage the VM_FAULT_RETRY functionality in the page fault
802 * paths better by using either get_user_pages_locked() or
803 * get_user_pages_unlocked().
804 *
805 * get_user_pages_locked() is suitable to replace the form:
806 *
807 * down_read(&mm->mmap_sem);
808 * do_something()
809 * get_user_pages(tsk, mm, ..., pages, NULL);
810 * up_read(&mm->mmap_sem);
811 *
812 * to:
813 *
814 * int locked = 1;
815 * down_read(&mm->mmap_sem);
816 * do_something()
817 * get_user_pages_locked(tsk, mm, ..., pages, &locked);
818 * if (locked)
819 * up_read(&mm->mmap_sem);
820 */
cde70140 821long get_user_pages_locked6(unsigned long start, unsigned long nr_pages,
f0818f47
AA
822 int write, int force, struct page **pages,
823 int *locked)
824{
cde70140
DH
825 return __get_user_pages_locked(current, current->mm, start, nr_pages,
826 write, force, pages, NULL, locked, true,
827 FOLL_TOUCH);
f0818f47 828}
cde70140 829EXPORT_SYMBOL(get_user_pages_locked6);
f0818f47 830
0fd71a56
AA
831/*
832 * Same as get_user_pages_unlocked(...., FOLL_TOUCH) but it allows to
833 * pass additional gup_flags as last parameter (like FOLL_HWPOISON).
834 *
835 * NOTE: here FOLL_TOUCH is not set implicitly and must be set by the
836 * caller if required (just like with __get_user_pages). "FOLL_GET",
837 * "FOLL_WRITE" and "FOLL_FORCE" are set implicitly as needed
838 * according to the parameters "pages", "write", "force"
839 * respectively.
840 */
841__always_inline long __get_user_pages_unlocked(struct task_struct *tsk, struct mm_struct *mm,
842 unsigned long start, unsigned long nr_pages,
843 int write, int force, struct page **pages,
844 unsigned int gup_flags)
845{
846 long ret;
847 int locked = 1;
848 down_read(&mm->mmap_sem);
849 ret = __get_user_pages_locked(tsk, mm, start, nr_pages, write, force,
850 pages, NULL, &locked, false, gup_flags);
851 if (locked)
852 up_read(&mm->mmap_sem);
853 return ret;
854}
855EXPORT_SYMBOL(__get_user_pages_unlocked);
856
f0818f47
AA
857/*
858 * get_user_pages_unlocked() is suitable to replace the form:
859 *
860 * down_read(&mm->mmap_sem);
861 * get_user_pages(tsk, mm, ..., pages, NULL);
862 * up_read(&mm->mmap_sem);
863 *
864 * with:
865 *
866 * get_user_pages_unlocked(tsk, mm, ..., pages);
867 *
868 * It is functionally equivalent to get_user_pages_fast so
869 * get_user_pages_fast should be used instead, if the two parameters
870 * "tsk" and "mm" are respectively equal to current and current->mm,
871 * or if "force" shall be set to 1 (get_user_pages_fast misses the
872 * "force" parameter).
873 */
cde70140 874long get_user_pages_unlocked5(unsigned long start, unsigned long nr_pages,
f0818f47
AA
875 int write, int force, struct page **pages)
876{
cde70140
DH
877 return __get_user_pages_unlocked(current, current->mm, start, nr_pages,
878 write, force, pages, FOLL_TOUCH);
f0818f47 879}
cde70140 880EXPORT_SYMBOL(get_user_pages_unlocked5);
f0818f47 881
4bbd4c77 882/*
1e987790 883 * get_user_pages_remote() - pin user pages in memory
4bbd4c77
KS
884 * @tsk: the task_struct to use for page fault accounting, or
885 * NULL if faults are not to be recorded.
886 * @mm: mm_struct of target mm
887 * @start: starting user address
888 * @nr_pages: number of pages from start to pin
889 * @write: whether pages will be written to by the caller
890 * @force: whether to force access even when user mapping is currently
891 * protected (but never forces write access to shared mapping).
892 * @pages: array that receives pointers to the pages pinned.
893 * Should be at least nr_pages long. Or NULL, if caller
894 * only intends to ensure the pages are faulted in.
895 * @vmas: array of pointers to vmas corresponding to each page.
896 * Or NULL if the caller does not require them.
897 *
898 * Returns number of pages pinned. This may be fewer than the number
899 * requested. If nr_pages is 0 or negative, returns 0. If no pages
900 * were pinned, returns -errno. Each page returned must be released
901 * with a put_page() call when it is finished with. vmas will only
902 * remain valid while mmap_sem is held.
903 *
904 * Must be called with mmap_sem held for read or write.
905 *
906 * get_user_pages walks a process's page tables and takes a reference to
907 * each struct page that each user address corresponds to at a given
908 * instant. That is, it takes the page that would be accessed if a user
909 * thread accesses the given user virtual address at that instant.
910 *
911 * This does not guarantee that the page exists in the user mappings when
912 * get_user_pages returns, and there may even be a completely different
913 * page there in some cases (eg. if mmapped pagecache has been invalidated
914 * and subsequently re faulted). However it does guarantee that the page
915 * won't be freed completely. And mostly callers simply care that the page
916 * contains data that was valid *at some point in time*. Typically, an IO
917 * or similar operation cannot guarantee anything stronger anyway because
918 * locks can't be held over the syscall boundary.
919 *
920 * If write=0, the page must not be written to. If the page is written to,
921 * set_page_dirty (or set_page_dirty_lock, as appropriate) must be called
922 * after the page is finished with, and before put_page is called.
923 *
924 * get_user_pages is typically used for fewer-copy IO operations, to get a
925 * handle on the memory by some means other than accesses via the user virtual
926 * addresses. The pages may be submitted for DMA to devices or accessed via
927 * their kernel linear mapping (via the kmap APIs). Care should be taken to
928 * use the correct cache flushing APIs.
929 *
930 * See also get_user_pages_fast, for performance critical applications.
f0818f47
AA
931 *
932 * get_user_pages should be phased out in favor of
933 * get_user_pages_locked|unlocked or get_user_pages_fast. Nothing
934 * should use get_user_pages because it cannot pass
935 * FAULT_FLAG_ALLOW_RETRY to handle_mm_fault.
4bbd4c77 936 */
1e987790
DH
937long get_user_pages_remote(struct task_struct *tsk, struct mm_struct *mm,
938 unsigned long start, unsigned long nr_pages,
939 int write, int force, struct page **pages,
940 struct vm_area_struct **vmas)
4bbd4c77 941{
f0818f47 942 return __get_user_pages_locked(tsk, mm, start, nr_pages, write, force,
1e987790
DH
943 pages, vmas, NULL, false,
944 FOLL_TOUCH | FOLL_REMOTE);
945}
946EXPORT_SYMBOL(get_user_pages_remote);
947
948/*
d4edcf0d
DH
949 * This is the same as get_user_pages_remote(), just with a
950 * less-flexible calling convention where we assume that the task
951 * and mm being operated on are the current task's. We also
952 * obviously don't pass FOLL_REMOTE in here.
1e987790 953 */
cde70140 954long get_user_pages6(unsigned long start, unsigned long nr_pages,
1e987790
DH
955 int write, int force, struct page **pages,
956 struct vm_area_struct **vmas)
957{
cde70140 958 return __get_user_pages_locked(current, current->mm, start, nr_pages,
1e987790
DH
959 write, force, pages, vmas, NULL, false,
960 FOLL_TOUCH);
4bbd4c77 961}
cde70140 962EXPORT_SYMBOL(get_user_pages6);
4bbd4c77 963
acc3c8d1
KS
964/**
965 * populate_vma_page_range() - populate a range of pages in the vma.
966 * @vma: target vma
967 * @start: start address
968 * @end: end address
969 * @nonblocking:
970 *
971 * This takes care of mlocking the pages too if VM_LOCKED is set.
972 *
973 * return 0 on success, negative error code on error.
974 *
975 * vma->vm_mm->mmap_sem must be held.
976 *
977 * If @nonblocking is NULL, it may be held for read or write and will
978 * be unperturbed.
979 *
980 * If @nonblocking is non-NULL, it must held for read only and may be
981 * released. If it's released, *@nonblocking will be set to 0.
982 */
983long populate_vma_page_range(struct vm_area_struct *vma,
984 unsigned long start, unsigned long end, int *nonblocking)
985{
986 struct mm_struct *mm = vma->vm_mm;
987 unsigned long nr_pages = (end - start) / PAGE_SIZE;
988 int gup_flags;
989
990 VM_BUG_ON(start & ~PAGE_MASK);
991 VM_BUG_ON(end & ~PAGE_MASK);
992 VM_BUG_ON_VMA(start < vma->vm_start, vma);
993 VM_BUG_ON_VMA(end > vma->vm_end, vma);
994 VM_BUG_ON_MM(!rwsem_is_locked(&mm->mmap_sem), mm);
995
de60f5f1
EM
996 gup_flags = FOLL_TOUCH | FOLL_POPULATE | FOLL_MLOCK;
997 if (vma->vm_flags & VM_LOCKONFAULT)
998 gup_flags &= ~FOLL_POPULATE;
acc3c8d1
KS
999 /*
1000 * We want to touch writable mappings with a write fault in order
1001 * to break COW, except for shared mappings because these don't COW
1002 * and we would not want to dirty them for nothing.
1003 */
1004 if ((vma->vm_flags & (VM_WRITE | VM_SHARED)) == VM_WRITE)
1005 gup_flags |= FOLL_WRITE;
1006
1007 /*
1008 * We want mlock to succeed for regions that have any permissions
1009 * other than PROT_NONE.
1010 */
1011 if (vma->vm_flags & (VM_READ | VM_WRITE | VM_EXEC))
1012 gup_flags |= FOLL_FORCE;
1013
1014 /*
1015 * We made sure addr is within a VMA, so the following will
1016 * not result in a stack expansion that recurses back here.
1017 */
1018 return __get_user_pages(current, mm, start, nr_pages, gup_flags,
1019 NULL, NULL, nonblocking);
1020}
1021
1022/*
1023 * __mm_populate - populate and/or mlock pages within a range of address space.
1024 *
1025 * This is used to implement mlock() and the MAP_POPULATE / MAP_LOCKED mmap
1026 * flags. VMAs must be already marked with the desired vm_flags, and
1027 * mmap_sem must not be held.
1028 */
1029int __mm_populate(unsigned long start, unsigned long len, int ignore_errors)
1030{
1031 struct mm_struct *mm = current->mm;
1032 unsigned long end, nstart, nend;
1033 struct vm_area_struct *vma = NULL;
1034 int locked = 0;
1035 long ret = 0;
1036
1037 VM_BUG_ON(start & ~PAGE_MASK);
1038 VM_BUG_ON(len != PAGE_ALIGN(len));
1039 end = start + len;
1040
1041 for (nstart = start; nstart < end; nstart = nend) {
1042 /*
1043 * We want to fault in pages for [nstart; end) address range.
1044 * Find first corresponding VMA.
1045 */
1046 if (!locked) {
1047 locked = 1;
1048 down_read(&mm->mmap_sem);
1049 vma = find_vma(mm, nstart);
1050 } else if (nstart >= vma->vm_end)
1051 vma = vma->vm_next;
1052 if (!vma || vma->vm_start >= end)
1053 break;
1054 /*
1055 * Set [nstart; nend) to intersection of desired address
1056 * range with the first VMA. Also, skip undesirable VMA types.
1057 */
1058 nend = min(end, vma->vm_end);
1059 if (vma->vm_flags & (VM_IO | VM_PFNMAP))
1060 continue;
1061 if (nstart < vma->vm_start)
1062 nstart = vma->vm_start;
1063 /*
1064 * Now fault in a range of pages. populate_vma_page_range()
1065 * double checks the vma flags, so that it won't mlock pages
1066 * if the vma was already munlocked.
1067 */
1068 ret = populate_vma_page_range(vma, nstart, nend, &locked);
1069 if (ret < 0) {
1070 if (ignore_errors) {
1071 ret = 0;
1072 continue; /* continue at next VMA */
1073 }
1074 break;
1075 }
1076 nend = nstart + ret * PAGE_SIZE;
1077 ret = 0;
1078 }
1079 if (locked)
1080 up_read(&mm->mmap_sem);
1081 return ret; /* 0 or negative error code */
1082}
1083
4bbd4c77
KS
1084/**
1085 * get_dump_page() - pin user page in memory while writing it to core dump
1086 * @addr: user address
1087 *
1088 * Returns struct page pointer of user page pinned for dump,
1089 * to be freed afterwards by page_cache_release() or put_page().
1090 *
1091 * Returns NULL on any kind of failure - a hole must then be inserted into
1092 * the corefile, to preserve alignment with its headers; and also returns
1093 * NULL wherever the ZERO_PAGE, or an anonymous pte_none, has been found -
1094 * allowing a hole to be left in the corefile to save diskspace.
1095 *
1096 * Called without mmap_sem, but after all other threads have been killed.
1097 */
1098#ifdef CONFIG_ELF_CORE
1099struct page *get_dump_page(unsigned long addr)
1100{
1101 struct vm_area_struct *vma;
1102 struct page *page;
1103
1104 if (__get_user_pages(current, current->mm, addr, 1,
1105 FOLL_FORCE | FOLL_DUMP | FOLL_GET, &page, &vma,
1106 NULL) < 1)
1107 return NULL;
1108 flush_cache_page(vma, addr, page_to_pfn(page));
1109 return page;
1110}
1111#endif /* CONFIG_ELF_CORE */
2667f50e
SC
1112
1113/*
1114 * Generic RCU Fast GUP
1115 *
1116 * get_user_pages_fast attempts to pin user pages by walking the page
1117 * tables directly and avoids taking locks. Thus the walker needs to be
1118 * protected from page table pages being freed from under it, and should
1119 * block any THP splits.
1120 *
1121 * One way to achieve this is to have the walker disable interrupts, and
1122 * rely on IPIs from the TLB flushing code blocking before the page table
1123 * pages are freed. This is unsuitable for architectures that do not need
1124 * to broadcast an IPI when invalidating TLBs.
1125 *
1126 * Another way to achieve this is to batch up page table containing pages
1127 * belonging to more than one mm_user, then rcu_sched a callback to free those
1128 * pages. Disabling interrupts will allow the fast_gup walker to both block
1129 * the rcu_sched callback, and an IPI that we broadcast for splitting THPs
1130 * (which is a relatively rare event). The code below adopts this strategy.
1131 *
1132 * Before activating this code, please be aware that the following assumptions
1133 * are currently made:
1134 *
1135 * *) HAVE_RCU_TABLE_FREE is enabled, and tlb_remove_table is used to free
1136 * pages containing page tables.
1137 *
2667f50e
SC
1138 * *) ptes can be read atomically by the architecture.
1139 *
1140 * *) access_ok is sufficient to validate userspace address ranges.
1141 *
1142 * The last two assumptions can be relaxed by the addition of helper functions.
1143 *
1144 * This code is based heavily on the PowerPC implementation by Nick Piggin.
1145 */
1146#ifdef CONFIG_HAVE_GENERIC_RCU_GUP
1147
1148#ifdef __HAVE_ARCH_PTE_SPECIAL
1149static int gup_pte_range(pmd_t pmd, unsigned long addr, unsigned long end,
1150 int write, struct page **pages, int *nr)
1151{
1152 pte_t *ptep, *ptem;
1153 int ret = 0;
1154
1155 ptem = ptep = pte_offset_map(&pmd, addr);
1156 do {
1157 /*
1158 * In the line below we are assuming that the pte can be read
1159 * atomically. If this is not the case for your architecture,
1160 * please wrap this in a helper function!
1161 *
1162 * for an example see gup_get_pte in arch/x86/mm/gup.c
1163 */
9d8c47e4 1164 pte_t pte = READ_ONCE(*ptep);
7aef4172 1165 struct page *head, *page;
2667f50e
SC
1166
1167 /*
1168 * Similar to the PMD case below, NUMA hinting must take slow
8a0516ed 1169 * path using the pte_protnone check.
2667f50e
SC
1170 */
1171 if (!pte_present(pte) || pte_special(pte) ||
8a0516ed 1172 pte_protnone(pte) || (write && !pte_write(pte)))
2667f50e
SC
1173 goto pte_unmap;
1174
1175 VM_BUG_ON(!pfn_valid(pte_pfn(pte)));
1176 page = pte_page(pte);
7aef4172 1177 head = compound_head(page);
2667f50e 1178
7aef4172 1179 if (!page_cache_get_speculative(head))
2667f50e
SC
1180 goto pte_unmap;
1181
1182 if (unlikely(pte_val(pte) != pte_val(*ptep))) {
7aef4172 1183 put_page(head);
2667f50e
SC
1184 goto pte_unmap;
1185 }
1186
7aef4172 1187 VM_BUG_ON_PAGE(compound_head(page) != head, page);
2667f50e
SC
1188 pages[*nr] = page;
1189 (*nr)++;
1190
1191 } while (ptep++, addr += PAGE_SIZE, addr != end);
1192
1193 ret = 1;
1194
1195pte_unmap:
1196 pte_unmap(ptem);
1197 return ret;
1198}
1199#else
1200
1201/*
1202 * If we can't determine whether or not a pte is special, then fail immediately
1203 * for ptes. Note, we can still pin HugeTLB and THP as these are guaranteed not
1204 * to be special.
1205 *
1206 * For a futex to be placed on a THP tail page, get_futex_key requires a
1207 * __get_user_pages_fast implementation that can pin pages. Thus it's still
1208 * useful to have gup_huge_pmd even if we can't operate on ptes.
1209 */
1210static int gup_pte_range(pmd_t pmd, unsigned long addr, unsigned long end,
1211 int write, struct page **pages, int *nr)
1212{
1213 return 0;
1214}
1215#endif /* __HAVE_ARCH_PTE_SPECIAL */
1216
1217static int gup_huge_pmd(pmd_t orig, pmd_t *pmdp, unsigned long addr,
1218 unsigned long end, int write, struct page **pages, int *nr)
1219{
ddc58f27 1220 struct page *head, *page;
2667f50e
SC
1221 int refs;
1222
1223 if (write && !pmd_write(orig))
1224 return 0;
1225
1226 refs = 0;
1227 head = pmd_page(orig);
1228 page = head + ((addr & ~PMD_MASK) >> PAGE_SHIFT);
2667f50e
SC
1229 do {
1230 VM_BUG_ON_PAGE(compound_head(page) != head, page);
1231 pages[*nr] = page;
1232 (*nr)++;
1233 page++;
1234 refs++;
1235 } while (addr += PAGE_SIZE, addr != end);
1236
1237 if (!page_cache_add_speculative(head, refs)) {
1238 *nr -= refs;
1239 return 0;
1240 }
1241
1242 if (unlikely(pmd_val(orig) != pmd_val(*pmdp))) {
1243 *nr -= refs;
1244 while (refs--)
1245 put_page(head);
1246 return 0;
1247 }
1248
2667f50e
SC
1249 return 1;
1250}
1251
1252static int gup_huge_pud(pud_t orig, pud_t *pudp, unsigned long addr,
1253 unsigned long end, int write, struct page **pages, int *nr)
1254{
ddc58f27 1255 struct page *head, *page;
2667f50e
SC
1256 int refs;
1257
1258 if (write && !pud_write(orig))
1259 return 0;
1260
1261 refs = 0;
1262 head = pud_page(orig);
1263 page = head + ((addr & ~PUD_MASK) >> PAGE_SHIFT);
2667f50e
SC
1264 do {
1265 VM_BUG_ON_PAGE(compound_head(page) != head, page);
1266 pages[*nr] = page;
1267 (*nr)++;
1268 page++;
1269 refs++;
1270 } while (addr += PAGE_SIZE, addr != end);
1271
1272 if (!page_cache_add_speculative(head, refs)) {
1273 *nr -= refs;
1274 return 0;
1275 }
1276
1277 if (unlikely(pud_val(orig) != pud_val(*pudp))) {
1278 *nr -= refs;
1279 while (refs--)
1280 put_page(head);
1281 return 0;
1282 }
1283
2667f50e
SC
1284 return 1;
1285}
1286
f30c59e9
AK
1287static int gup_huge_pgd(pgd_t orig, pgd_t *pgdp, unsigned long addr,
1288 unsigned long end, int write,
1289 struct page **pages, int *nr)
1290{
1291 int refs;
ddc58f27 1292 struct page *head, *page;
f30c59e9
AK
1293
1294 if (write && !pgd_write(orig))
1295 return 0;
1296
1297 refs = 0;
1298 head = pgd_page(orig);
1299 page = head + ((addr & ~PGDIR_MASK) >> PAGE_SHIFT);
f30c59e9
AK
1300 do {
1301 VM_BUG_ON_PAGE(compound_head(page) != head, page);
1302 pages[*nr] = page;
1303 (*nr)++;
1304 page++;
1305 refs++;
1306 } while (addr += PAGE_SIZE, addr != end);
1307
1308 if (!page_cache_add_speculative(head, refs)) {
1309 *nr -= refs;
1310 return 0;
1311 }
1312
1313 if (unlikely(pgd_val(orig) != pgd_val(*pgdp))) {
1314 *nr -= refs;
1315 while (refs--)
1316 put_page(head);
1317 return 0;
1318 }
1319
f30c59e9
AK
1320 return 1;
1321}
1322
2667f50e
SC
1323static int gup_pmd_range(pud_t pud, unsigned long addr, unsigned long end,
1324 int write, struct page **pages, int *nr)
1325{
1326 unsigned long next;
1327 pmd_t *pmdp;
1328
1329 pmdp = pmd_offset(&pud, addr);
1330 do {
38c5ce93 1331 pmd_t pmd = READ_ONCE(*pmdp);
2667f50e
SC
1332
1333 next = pmd_addr_end(addr, end);
4b471e88 1334 if (pmd_none(pmd))
2667f50e
SC
1335 return 0;
1336
1337 if (unlikely(pmd_trans_huge(pmd) || pmd_huge(pmd))) {
1338 /*
1339 * NUMA hinting faults need to be handled in the GUP
1340 * slowpath for accounting purposes and so that they
1341 * can be serialised against THP migration.
1342 */
8a0516ed 1343 if (pmd_protnone(pmd))
2667f50e
SC
1344 return 0;
1345
1346 if (!gup_huge_pmd(pmd, pmdp, addr, next, write,
1347 pages, nr))
1348 return 0;
1349
f30c59e9
AK
1350 } else if (unlikely(is_hugepd(__hugepd(pmd_val(pmd))))) {
1351 /*
1352 * architecture have different format for hugetlbfs
1353 * pmd format and THP pmd format
1354 */
1355 if (!gup_huge_pd(__hugepd(pmd_val(pmd)), addr,
1356 PMD_SHIFT, next, write, pages, nr))
1357 return 0;
2667f50e
SC
1358 } else if (!gup_pte_range(pmd, addr, next, write, pages, nr))
1359 return 0;
1360 } while (pmdp++, addr = next, addr != end);
1361
1362 return 1;
1363}
1364
f30c59e9
AK
1365static int gup_pud_range(pgd_t pgd, unsigned long addr, unsigned long end,
1366 int write, struct page **pages, int *nr)
2667f50e
SC
1367{
1368 unsigned long next;
1369 pud_t *pudp;
1370
f30c59e9 1371 pudp = pud_offset(&pgd, addr);
2667f50e 1372 do {
e37c6982 1373 pud_t pud = READ_ONCE(*pudp);
2667f50e
SC
1374
1375 next = pud_addr_end(addr, end);
1376 if (pud_none(pud))
1377 return 0;
f30c59e9 1378 if (unlikely(pud_huge(pud))) {
2667f50e 1379 if (!gup_huge_pud(pud, pudp, addr, next, write,
f30c59e9
AK
1380 pages, nr))
1381 return 0;
1382 } else if (unlikely(is_hugepd(__hugepd(pud_val(pud))))) {
1383 if (!gup_huge_pd(__hugepd(pud_val(pud)), addr,
1384 PUD_SHIFT, next, write, pages, nr))
2667f50e
SC
1385 return 0;
1386 } else if (!gup_pmd_range(pud, addr, next, write, pages, nr))
1387 return 0;
1388 } while (pudp++, addr = next, addr != end);
1389
1390 return 1;
1391}
1392
1393/*
1394 * Like get_user_pages_fast() except it's IRQ-safe in that it won't fall back to
1395 * the regular GUP. It will only return non-negative values.
1396 */
1397int __get_user_pages_fast(unsigned long start, int nr_pages, int write,
1398 struct page **pages)
1399{
1400 struct mm_struct *mm = current->mm;
1401 unsigned long addr, len, end;
1402 unsigned long next, flags;
1403 pgd_t *pgdp;
1404 int nr = 0;
1405
1406 start &= PAGE_MASK;
1407 addr = start;
1408 len = (unsigned long) nr_pages << PAGE_SHIFT;
1409 end = start + len;
1410
1411 if (unlikely(!access_ok(write ? VERIFY_WRITE : VERIFY_READ,
1412 start, len)))
1413 return 0;
1414
1415 /*
1416 * Disable interrupts. We use the nested form as we can already have
1417 * interrupts disabled by get_futex_key.
1418 *
1419 * With interrupts disabled, we block page table pages from being
1420 * freed from under us. See mmu_gather_tlb in asm-generic/tlb.h
1421 * for more details.
1422 *
1423 * We do not adopt an rcu_read_lock(.) here as we also want to
1424 * block IPIs that come from THPs splitting.
1425 */
1426
1427 local_irq_save(flags);
1428 pgdp = pgd_offset(mm, addr);
1429 do {
9d8c47e4 1430 pgd_t pgd = READ_ONCE(*pgdp);
f30c59e9 1431
2667f50e 1432 next = pgd_addr_end(addr, end);
f30c59e9 1433 if (pgd_none(pgd))
2667f50e 1434 break;
f30c59e9
AK
1435 if (unlikely(pgd_huge(pgd))) {
1436 if (!gup_huge_pgd(pgd, pgdp, addr, next, write,
1437 pages, &nr))
1438 break;
1439 } else if (unlikely(is_hugepd(__hugepd(pgd_val(pgd))))) {
1440 if (!gup_huge_pd(__hugepd(pgd_val(pgd)), addr,
1441 PGDIR_SHIFT, next, write, pages, &nr))
1442 break;
1443 } else if (!gup_pud_range(pgd, addr, next, write, pages, &nr))
2667f50e
SC
1444 break;
1445 } while (pgdp++, addr = next, addr != end);
1446 local_irq_restore(flags);
1447
1448 return nr;
1449}
1450
1451/**
1452 * get_user_pages_fast() - pin user pages in memory
1453 * @start: starting user address
1454 * @nr_pages: number of pages from start to pin
1455 * @write: whether pages will be written to
1456 * @pages: array that receives pointers to the pages pinned.
1457 * Should be at least nr_pages long.
1458 *
1459 * Attempt to pin user pages in memory without taking mm->mmap_sem.
1460 * If not successful, it will fall back to taking the lock and
1461 * calling get_user_pages().
1462 *
1463 * Returns number of pages pinned. This may be fewer than the number
1464 * requested. If nr_pages is 0 or negative, returns 0. If no pages
1465 * were pinned, returns -errno.
1466 */
1467int get_user_pages_fast(unsigned long start, int nr_pages, int write,
1468 struct page **pages)
1469{
1470 struct mm_struct *mm = current->mm;
1471 int nr, ret;
1472
1473 start &= PAGE_MASK;
1474 nr = __get_user_pages_fast(start, nr_pages, write, pages);
1475 ret = nr;
1476
1477 if (nr < nr_pages) {
1478 /* Try to get the remaining pages with get_user_pages */
1479 start += nr << PAGE_SHIFT;
1480 pages += nr;
1481
a7b78075
AA
1482 ret = get_user_pages_unlocked(current, mm, start,
1483 nr_pages - nr, write, 0, pages);
2667f50e
SC
1484
1485 /* Have to be a bit careful with return values */
1486 if (nr > 0) {
1487 if (ret < 0)
1488 ret = nr;
1489 else
1490 ret += nr;
1491 }
1492 }
1493
1494 return ret;
1495}
1496
1497#endif /* CONFIG_HAVE_GENERIC_RCU_GUP */
cde70140
DH
1498
1499long get_user_pages8(struct task_struct *tsk, struct mm_struct *mm,
1500 unsigned long start, unsigned long nr_pages,
1501 int write, int force, struct page **pages,
1502 struct vm_area_struct **vmas)
1503{
1504 WARN_ONCE(tsk != current, "get_user_pages() called on remote task");
1505 WARN_ONCE(mm != current->mm, "get_user_pages() called on remote mm");
1506
1507 return get_user_pages6(start, nr_pages, write, force, pages, vmas);
1508}
1509EXPORT_SYMBOL(get_user_pages8);
1510
1511long get_user_pages_locked8(struct task_struct *tsk, struct mm_struct *mm,
1512 unsigned long start, unsigned long nr_pages,
1513 int write, int force, struct page **pages, int *locked)
1514{
1515 WARN_ONCE(tsk != current, "get_user_pages_locked() called on remote task");
1516 WARN_ONCE(mm != current->mm, "get_user_pages_locked() called on remote mm");
1517
1518 return get_user_pages_locked6(start, nr_pages, write, force, pages, locked);
1519}
1520EXPORT_SYMBOL(get_user_pages_locked8);
1521
1522long get_user_pages_unlocked7(struct task_struct *tsk, struct mm_struct *mm,
1523 unsigned long start, unsigned long nr_pages,
1524 int write, int force, struct page **pages)
1525{
1526 WARN_ONCE(tsk != current, "get_user_pages_unlocked() called on remote task");
1527 WARN_ONCE(mm != current->mm, "get_user_pages_unlocked() called on remote mm");
1528
1529 return get_user_pages_unlocked5(start, nr_pages, write, force, pages);
1530}
1531EXPORT_SYMBOL(get_user_pages_unlocked7);
1532
This page took 0.462622 seconds and 5 git commands to generate.