aboutsummaryrefslogtreecommitdiffstats
path: root/mm
diff options
context:
space:
mode:
authorKirill A. Shutemov <kirill.shutemov@linux.intel.com>2014-06-04 19:08:10 -0400
committerLinus Torvalds <torvalds@linux-foundation.org>2014-06-04 19:54:04 -0400
commit4bbd4c776a63a063546552de42f6a535395f6d9e (patch)
tree2a722c3bde3f3dabf85030b391b44c2cb3972df2 /mm
parentf4527c90868d8fa175c68ccf216cf9b67a7d8a1a (diff)
mm: move get_user_pages()-related code to separate file
mm/memory.c is overloaded: over 4k lines. get_user_pages() code is pretty much self-contained let's move it to separate file. No other changes made. Signed-off-by: Kirill A. Shutemov <kirill.shutemov@linux.intel.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Diffstat (limited to 'mm')
-rw-r--r--mm/Makefile2
-rw-r--r--mm/gup.c649
-rw-r--r--mm/internal.h5
-rw-r--r--mm/memory.c641
4 files changed, 655 insertions, 642 deletions
diff --git a/mm/Makefile b/mm/Makefile
index 0173940407f6..4064f3ec145e 100644
--- a/mm/Makefile
+++ b/mm/Makefile
@@ -3,7 +3,7 @@
3# 3#
4 4
5mmu-y := nommu.o 5mmu-y := nommu.o
6mmu-$(CONFIG_MMU) := fremap.o highmem.o madvise.o memory.o mincore.o \ 6mmu-$(CONFIG_MMU) := fremap.o gup.o highmem.o madvise.o memory.o mincore.o \
7 mlock.o mmap.o mprotect.o mremap.o msync.o rmap.o \ 7 mlock.o mmap.o mprotect.o mremap.o msync.o rmap.o \
8 vmalloc.o pagewalk.o pgtable-generic.o 8 vmalloc.o pagewalk.o pgtable-generic.o
9 9
diff --git a/mm/gup.c b/mm/gup.c
new file mode 100644
index 000000000000..ea88b65f264d
--- /dev/null
+++ b/mm/gup.c
@@ -0,0 +1,649 @@
1#include <linux/kernel.h>
2#include <linux/errno.h>
3#include <linux/err.h>
4#include <linux/spinlock.h>
5
6#include <linux/hugetlb.h>
7#include <linux/mm.h>
8#include <linux/pagemap.h>
9#include <linux/rmap.h>
10#include <linux/swap.h>
11#include <linux/swapops.h>
12
13#include "internal.h"
14
15/**
16 * follow_page_mask - look up a page descriptor from a user-virtual address
17 * @vma: vm_area_struct mapping @address
18 * @address: virtual address to look up
19 * @flags: flags modifying lookup behaviour
20 * @page_mask: on output, *page_mask is set according to the size of the page
21 *
22 * @flags can have FOLL_ flags set, defined in <linux/mm.h>
23 *
24 * Returns the mapped (struct page *), %NULL if no mapping exists, or
25 * an error pointer if there is a mapping to something not represented
26 * by a page descriptor (see also vm_normal_page()).
27 */
28struct page *follow_page_mask(struct vm_area_struct *vma,
29 unsigned long address, unsigned int flags,
30 unsigned int *page_mask)
31{
32 pgd_t *pgd;
33 pud_t *pud;
34 pmd_t *pmd;
35 pte_t *ptep, pte;
36 spinlock_t *ptl;
37 struct page *page;
38 struct mm_struct *mm = vma->vm_mm;
39
40 *page_mask = 0;
41
42 page = follow_huge_addr(mm, address, flags & FOLL_WRITE);
43 if (!IS_ERR(page)) {
44 BUG_ON(flags & FOLL_GET);
45 goto out;
46 }
47
48 page = NULL;
49 pgd = pgd_offset(mm, address);
50 if (pgd_none(*pgd) || unlikely(pgd_bad(*pgd)))
51 goto no_page_table;
52
53 pud = pud_offset(pgd, address);
54 if (pud_none(*pud))
55 goto no_page_table;
56 if (pud_huge(*pud) && vma->vm_flags & VM_HUGETLB) {
57 if (flags & FOLL_GET)
58 goto out;
59 page = follow_huge_pud(mm, address, pud, flags & FOLL_WRITE);
60 goto out;
61 }
62 if (unlikely(pud_bad(*pud)))
63 goto no_page_table;
64
65 pmd = pmd_offset(pud, address);
66 if (pmd_none(*pmd))
67 goto no_page_table;
68 if (pmd_huge(*pmd) && vma->vm_flags & VM_HUGETLB) {
69 page = follow_huge_pmd(mm, address, pmd, flags & FOLL_WRITE);
70 if (flags & FOLL_GET) {
71 /*
72 * Refcount on tail pages are not well-defined and
73 * shouldn't be taken. The caller should handle a NULL
74 * return when trying to follow tail pages.
75 */
76 if (PageHead(page))
77 get_page(page);
78 else {
79 page = NULL;
80 goto out;
81 }
82 }
83 goto out;
84 }
85 if ((flags & FOLL_NUMA) && pmd_numa(*pmd))
86 goto no_page_table;
87 if (pmd_trans_huge(*pmd)) {
88 if (flags & FOLL_SPLIT) {
89 split_huge_page_pmd(vma, address, pmd);
90 goto split_fallthrough;
91 }
92 ptl = pmd_lock(mm, pmd);
93 if (likely(pmd_trans_huge(*pmd))) {
94 if (unlikely(pmd_trans_splitting(*pmd))) {
95 spin_unlock(ptl);
96 wait_split_huge_page(vma->anon_vma, pmd);
97 } else {
98 page = follow_trans_huge_pmd(vma, address,
99 pmd, flags);
100 spin_unlock(ptl);
101 *page_mask = HPAGE_PMD_NR - 1;
102 goto out;
103 }
104 } else
105 spin_unlock(ptl);
106 /* fall through */
107 }
108split_fallthrough:
109 if (unlikely(pmd_bad(*pmd)))
110 goto no_page_table;
111
112 ptep = pte_offset_map_lock(mm, pmd, address, &ptl);
113
114 pte = *ptep;
115 if (!pte_present(pte)) {
116 swp_entry_t entry;
117 /*
118 * KSM's break_ksm() relies upon recognizing a ksm page
119 * even while it is being migrated, so for that case we
120 * need migration_entry_wait().
121 */
122 if (likely(!(flags & FOLL_MIGRATION)))
123 goto no_page;
124 if (pte_none(pte) || pte_file(pte))
125 goto no_page;
126 entry = pte_to_swp_entry(pte);
127 if (!is_migration_entry(entry))
128 goto no_page;
129 pte_unmap_unlock(ptep, ptl);
130 migration_entry_wait(mm, pmd, address);
131 goto split_fallthrough;
132 }
133 if ((flags & FOLL_NUMA) && pte_numa(pte))
134 goto no_page;
135 if ((flags & FOLL_WRITE) && !pte_write(pte))
136 goto unlock;
137
138 page = vm_normal_page(vma, address, pte);
139 if (unlikely(!page)) {
140 if ((flags & FOLL_DUMP) ||
141 !is_zero_pfn(pte_pfn(pte)))
142 goto bad_page;
143 page = pte_page(pte);
144 }
145
146 if (flags & FOLL_GET)
147 get_page_foll(page);
148 if (flags & FOLL_TOUCH) {
149 if ((flags & FOLL_WRITE) &&
150 !pte_dirty(pte) && !PageDirty(page))
151 set_page_dirty(page);
152 /*
153 * pte_mkyoung() would be more correct here, but atomic care
154 * is needed to avoid losing the dirty bit: it is easier to use
155 * mark_page_accessed().
156 */
157 mark_page_accessed(page);
158 }
159 if ((flags & FOLL_MLOCK) && (vma->vm_flags & VM_LOCKED)) {
160 /*
161 * The preliminary mapping check is mainly to avoid the
162 * pointless overhead of lock_page on the ZERO_PAGE
163 * which might bounce very badly if there is contention.
164 *
165 * If the page is already locked, we don't need to
166 * handle it now - vmscan will handle it later if and
167 * when it attempts to reclaim the page.
168 */
169 if (page->mapping && trylock_page(page)) {
170 lru_add_drain(); /* push cached pages to LRU */
171 /*
172 * Because we lock page here, and migration is
173 * blocked by the pte's page reference, and we
174 * know the page is still mapped, we don't even
175 * need to check for file-cache page truncation.
176 */
177 mlock_vma_page(page);
178 unlock_page(page);
179 }
180 }
181unlock:
182 pte_unmap_unlock(ptep, ptl);
183out:
184 return page;
185
186bad_page:
187 pte_unmap_unlock(ptep, ptl);
188 return ERR_PTR(-EFAULT);
189
190no_page:
191 pte_unmap_unlock(ptep, ptl);
192 if (!pte_none(pte))
193 return page;
194
195no_page_table:
196 /*
197 * When core dumping an enormous anonymous area that nobody
198 * has touched so far, we don't want to allocate unnecessary pages or
199 * page tables. Return error instead of NULL to skip handle_mm_fault,
200 * then get_dump_page() will return NULL to leave a hole in the dump.
201 * But we can only make this optimization where a hole would surely
202 * be zero-filled if handle_mm_fault() actually did handle it.
203 */
204 if ((flags & FOLL_DUMP) &&
205 (!vma->vm_ops || !vma->vm_ops->fault))
206 return ERR_PTR(-EFAULT);
207 return page;
208}
209
210static inline int stack_guard_page(struct vm_area_struct *vma, unsigned long addr)
211{
212 return stack_guard_page_start(vma, addr) ||
213 stack_guard_page_end(vma, addr+PAGE_SIZE);
214}
215
216/**
217 * __get_user_pages() - pin user pages in memory
218 * @tsk: task_struct of target task
219 * @mm: mm_struct of target mm
220 * @start: starting user address
221 * @nr_pages: number of pages from start to pin
222 * @gup_flags: flags modifying pin behaviour
223 * @pages: array that receives pointers to the pages pinned.
224 * Should be at least nr_pages long. Or NULL, if caller
225 * only intends to ensure the pages are faulted in.
226 * @vmas: array of pointers to vmas corresponding to each page.
227 * Or NULL if the caller does not require them.
228 * @nonblocking: whether waiting for disk IO or mmap_sem contention
229 *
230 * Returns number of pages pinned. This may be fewer than the number
231 * requested. If nr_pages is 0 or negative, returns 0. If no pages
232 * were pinned, returns -errno. Each page returned must be released
233 * with a put_page() call when it is finished with. vmas will only
234 * remain valid while mmap_sem is held.
235 *
236 * Must be called with mmap_sem held for read or write.
237 *
238 * __get_user_pages walks a process's page tables and takes a reference to
239 * each struct page that each user address corresponds to at a given
240 * instant. That is, it takes the page that would be accessed if a user
241 * thread accesses the given user virtual address at that instant.
242 *
243 * This does not guarantee that the page exists in the user mappings when
244 * __get_user_pages returns, and there may even be a completely different
245 * page there in some cases (eg. if mmapped pagecache has been invalidated
246 * and subsequently re faulted). However it does guarantee that the page
247 * won't be freed completely. And mostly callers simply care that the page
248 * contains data that was valid *at some point in time*. Typically, an IO
249 * or similar operation cannot guarantee anything stronger anyway because
250 * locks can't be held over the syscall boundary.
251 *
252 * If @gup_flags & FOLL_WRITE == 0, the page must not be written to. If
253 * the page is written to, set_page_dirty (or set_page_dirty_lock, as
254 * appropriate) must be called after the page is finished with, and
255 * before put_page is called.
256 *
257 * If @nonblocking != NULL, __get_user_pages will not wait for disk IO
258 * or mmap_sem contention, and if waiting is needed to pin all pages,
259 * *@nonblocking will be set to 0.
260 *
261 * In most cases, get_user_pages or get_user_pages_fast should be used
262 * instead of __get_user_pages. __get_user_pages should be used only if
263 * you need some special @gup_flags.
264 */
265long __get_user_pages(struct task_struct *tsk, struct mm_struct *mm,
266 unsigned long start, unsigned long nr_pages,
267 unsigned int gup_flags, struct page **pages,
268 struct vm_area_struct **vmas, int *nonblocking)
269{
270 long i;
271 unsigned long vm_flags;
272 unsigned int page_mask;
273
274 if (!nr_pages)
275 return 0;
276
277 VM_BUG_ON(!!pages != !!(gup_flags & FOLL_GET));
278
279 /*
280 * If FOLL_FORCE is set then do not force a full fault as the hinting
281 * fault information is unrelated to the reference behaviour of a task
282 * using the address space
283 */
284 if (!(gup_flags & FOLL_FORCE))
285 gup_flags |= FOLL_NUMA;
286
287 i = 0;
288
289 do {
290 struct vm_area_struct *vma;
291
292 vma = find_extend_vma(mm, start);
293 if (!vma && in_gate_area(mm, start)) {
294 unsigned long pg = start & PAGE_MASK;
295 pgd_t *pgd;
296 pud_t *pud;
297 pmd_t *pmd;
298 pte_t *pte;
299
300 /* user gate pages are read-only */
301 if (gup_flags & FOLL_WRITE)
302 goto efault;
303 if (pg > TASK_SIZE)
304 pgd = pgd_offset_k(pg);
305 else
306 pgd = pgd_offset_gate(mm, pg);
307 BUG_ON(pgd_none(*pgd));
308 pud = pud_offset(pgd, pg);
309 BUG_ON(pud_none(*pud));
310 pmd = pmd_offset(pud, pg);
311 if (pmd_none(*pmd))
312 goto efault;
313 VM_BUG_ON(pmd_trans_huge(*pmd));
314 pte = pte_offset_map(pmd, pg);
315 if (pte_none(*pte)) {
316 pte_unmap(pte);
317 goto efault;
318 }
319 vma = get_gate_vma(mm);
320 if (pages) {
321 struct page *page;
322
323 page = vm_normal_page(vma, start, *pte);
324 if (!page) {
325 if (!(gup_flags & FOLL_DUMP) &&
326 is_zero_pfn(pte_pfn(*pte)))
327 page = pte_page(*pte);
328 else {
329 pte_unmap(pte);
330 goto efault;
331 }
332 }
333 pages[i] = page;
334 get_page(page);
335 }
336 pte_unmap(pte);
337 page_mask = 0;
338 goto next_page;
339 }
340
341 if (!vma)
342 goto efault;
343 vm_flags = vma->vm_flags;
344 if (vm_flags & (VM_IO | VM_PFNMAP))
345 goto efault;
346
347 if (gup_flags & FOLL_WRITE) {
348 if (!(vm_flags & VM_WRITE)) {
349 if (!(gup_flags & FOLL_FORCE))
350 goto efault;
351 /*
352 * We used to let the write,force case do COW
353 * in a VM_MAYWRITE VM_SHARED !VM_WRITE vma, so
354 * ptrace could set a breakpoint in a read-only
355 * mapping of an executable, without corrupting
356 * the file (yet only when that file had been
357 * opened for writing!). Anon pages in shared
358 * mappings are surprising: now just reject it.
359 */
360 if (!is_cow_mapping(vm_flags)) {
361 WARN_ON_ONCE(vm_flags & VM_MAYWRITE);
362 goto efault;
363 }
364 }
365 } else {
366 if (!(vm_flags & VM_READ)) {
367 if (!(gup_flags & FOLL_FORCE))
368 goto efault;
369 /*
370 * Is there actually any vma we can reach here
371 * which does not have VM_MAYREAD set?
372 */
373 if (!(vm_flags & VM_MAYREAD))
374 goto efault;
375 }
376 }
377
378 if (is_vm_hugetlb_page(vma)) {
379 i = follow_hugetlb_page(mm, vma, pages, vmas,
380 &start, &nr_pages, i, gup_flags);
381 continue;
382 }
383
384 do {
385 struct page *page;
386 unsigned int foll_flags = gup_flags;
387 unsigned int page_increm;
388
389 /*
390 * If we have a pending SIGKILL, don't keep faulting
391 * pages and potentially allocating memory.
392 */
393 if (unlikely(fatal_signal_pending(current)))
394 return i ? i : -ERESTARTSYS;
395
396 cond_resched();
397 while (!(page = follow_page_mask(vma, start,
398 foll_flags, &page_mask))) {
399 int ret;
400 unsigned int fault_flags = 0;
401
402 /* For mlock, just skip the stack guard page. */
403 if (foll_flags & FOLL_MLOCK) {
404 if (stack_guard_page(vma, start))
405 goto next_page;
406 }
407 if (foll_flags & FOLL_WRITE)
408 fault_flags |= FAULT_FLAG_WRITE;
409 if (nonblocking)
410 fault_flags |= FAULT_FLAG_ALLOW_RETRY;
411 if (foll_flags & FOLL_NOWAIT)
412 fault_flags |= (FAULT_FLAG_ALLOW_RETRY | FAULT_FLAG_RETRY_NOWAIT);
413
414 ret = handle_mm_fault(mm, vma, start,
415 fault_flags);
416
417 if (ret & VM_FAULT_ERROR) {
418 if (ret & VM_FAULT_OOM)
419 return i ? i : -ENOMEM;
420 if (ret & (VM_FAULT_HWPOISON |
421 VM_FAULT_HWPOISON_LARGE)) {
422 if (i)
423 return i;
424 else if (gup_flags & FOLL_HWPOISON)
425 return -EHWPOISON;
426 else
427 return -EFAULT;
428 }
429 if (ret & VM_FAULT_SIGBUS)
430 goto efault;
431 BUG();
432 }
433
434 if (tsk) {
435 if (ret & VM_FAULT_MAJOR)
436 tsk->maj_flt++;
437 else
438 tsk->min_flt++;
439 }
440
441 if (ret & VM_FAULT_RETRY) {
442 if (nonblocking)
443 *nonblocking = 0;
444 return i;
445 }
446
447 /*
448 * The VM_FAULT_WRITE bit tells us that
449 * do_wp_page has broken COW when necessary,
450 * even if maybe_mkwrite decided not to set
451 * pte_write. We can thus safely do subsequent
452 * page lookups as if they were reads. But only
453 * do so when looping for pte_write is futile:
454 * in some cases userspace may also be wanting
455 * to write to the gotten user page, which a
456 * read fault here might prevent (a readonly
457 * page might get reCOWed by userspace write).
458 */
459 if ((ret & VM_FAULT_WRITE) &&
460 !(vma->vm_flags & VM_WRITE))
461 foll_flags &= ~FOLL_WRITE;
462
463 cond_resched();
464 }
465 if (IS_ERR(page))
466 return i ? i : PTR_ERR(page);
467 if (pages) {
468 pages[i] = page;
469
470 flush_anon_page(vma, page, start);
471 flush_dcache_page(page);
472 page_mask = 0;
473 }
474next_page:
475 if (vmas) {
476 vmas[i] = vma;
477 page_mask = 0;
478 }
479 page_increm = 1 + (~(start >> PAGE_SHIFT) & page_mask);
480 if (page_increm > nr_pages)
481 page_increm = nr_pages;
482 i += page_increm;
483 start += page_increm * PAGE_SIZE;
484 nr_pages -= page_increm;
485 } while (nr_pages && start < vma->vm_end);
486 } while (nr_pages);
487 return i;
488efault:
489 return i ? : -EFAULT;
490}
491EXPORT_SYMBOL(__get_user_pages);
492
493/*
494 * fixup_user_fault() - manually resolve a user page fault
495 * @tsk: the task_struct to use for page fault accounting, or
496 * NULL if faults are not to be recorded.
497 * @mm: mm_struct of target mm
498 * @address: user address
499 * @fault_flags:flags to pass down to handle_mm_fault()
500 *
501 * This is meant to be called in the specific scenario where for locking reasons
502 * we try to access user memory in atomic context (within a pagefault_disable()
503 * section), this returns -EFAULT, and we want to resolve the user fault before
504 * trying again.
505 *
506 * Typically this is meant to be used by the futex code.
507 *
508 * The main difference with get_user_pages() is that this function will
509 * unconditionally call handle_mm_fault() which will in turn perform all the
510 * necessary SW fixup of the dirty and young bits in the PTE, while
511 * handle_mm_fault() only guarantees to update these in the struct page.
512 *
513 * This is important for some architectures where those bits also gate the
514 * access permission to the page because they are maintained in software. On
515 * such architectures, gup() will not be enough to make a subsequent access
516 * succeed.
517 *
518 * This should be called with the mm_sem held for read.
519 */
520int fixup_user_fault(struct task_struct *tsk, struct mm_struct *mm,
521 unsigned long address, unsigned int fault_flags)
522{
523 struct vm_area_struct *vma;
524 vm_flags_t vm_flags;
525 int ret;
526
527 vma = find_extend_vma(mm, address);
528 if (!vma || address < vma->vm_start)
529 return -EFAULT;
530
531 vm_flags = (fault_flags & FAULT_FLAG_WRITE) ? VM_WRITE : VM_READ;
532 if (!(vm_flags & vma->vm_flags))
533 return -EFAULT;
534
535 ret = handle_mm_fault(mm, vma, address, fault_flags);
536 if (ret & VM_FAULT_ERROR) {
537 if (ret & VM_FAULT_OOM)
538 return -ENOMEM;
539 if (ret & (VM_FAULT_HWPOISON | VM_FAULT_HWPOISON_LARGE))
540 return -EHWPOISON;
541 if (ret & VM_FAULT_SIGBUS)
542 return -EFAULT;
543 BUG();
544 }
545 if (tsk) {
546 if (ret & VM_FAULT_MAJOR)
547 tsk->maj_flt++;
548 else
549 tsk->min_flt++;
550 }
551 return 0;
552}
553
554/*
555 * get_user_pages() - pin user pages in memory
556 * @tsk: the task_struct to use for page fault accounting, or
557 * NULL if faults are not to be recorded.
558 * @mm: mm_struct of target mm
559 * @start: starting user address
560 * @nr_pages: number of pages from start to pin
561 * @write: whether pages will be written to by the caller
562 * @force: whether to force access even when user mapping is currently
563 * protected (but never forces write access to shared mapping).
564 * @pages: array that receives pointers to the pages pinned.
565 * Should be at least nr_pages long. Or NULL, if caller
566 * only intends to ensure the pages are faulted in.
567 * @vmas: array of pointers to vmas corresponding to each page.
568 * Or NULL if the caller does not require them.
569 *
570 * Returns number of pages pinned. This may be fewer than the number
571 * requested. If nr_pages is 0 or negative, returns 0. If no pages
572 * were pinned, returns -errno. Each page returned must be released
573 * with a put_page() call when it is finished with. vmas will only
574 * remain valid while mmap_sem is held.
575 *
576 * Must be called with mmap_sem held for read or write.
577 *
578 * get_user_pages walks a process's page tables and takes a reference to
579 * each struct page that each user address corresponds to at a given
580 * instant. That is, it takes the page that would be accessed if a user
581 * thread accesses the given user virtual address at that instant.
582 *
583 * This does not guarantee that the page exists in the user mappings when
584 * get_user_pages returns, and there may even be a completely different
585 * page there in some cases (eg. if mmapped pagecache has been invalidated
586 * and subsequently re faulted). However it does guarantee that the page
587 * won't be freed completely. And mostly callers simply care that the page
588 * contains data that was valid *at some point in time*. Typically, an IO
589 * or similar operation cannot guarantee anything stronger anyway because
590 * locks can't be held over the syscall boundary.
591 *
592 * If write=0, the page must not be written to. If the page is written to,
593 * set_page_dirty (or set_page_dirty_lock, as appropriate) must be called
594 * after the page is finished with, and before put_page is called.
595 *
596 * get_user_pages is typically used for fewer-copy IO operations, to get a
597 * handle on the memory by some means other than accesses via the user virtual
598 * addresses. The pages may be submitted for DMA to devices or accessed via
599 * their kernel linear mapping (via the kmap APIs). Care should be taken to
600 * use the correct cache flushing APIs.
601 *
602 * See also get_user_pages_fast, for performance critical applications.
603 */
604long get_user_pages(struct task_struct *tsk, struct mm_struct *mm,
605 unsigned long start, unsigned long nr_pages, int write,
606 int force, struct page **pages, struct vm_area_struct **vmas)
607{
608 int flags = FOLL_TOUCH;
609
610 if (pages)
611 flags |= FOLL_GET;
612 if (write)
613 flags |= FOLL_WRITE;
614 if (force)
615 flags |= FOLL_FORCE;
616
617 return __get_user_pages(tsk, mm, start, nr_pages, flags, pages, vmas,
618 NULL);
619}
620EXPORT_SYMBOL(get_user_pages);
621
622/**
623 * get_dump_page() - pin user page in memory while writing it to core dump
624 * @addr: user address
625 *
626 * Returns struct page pointer of user page pinned for dump,
627 * to be freed afterwards by page_cache_release() or put_page().
628 *
629 * Returns NULL on any kind of failure - a hole must then be inserted into
630 * the corefile, to preserve alignment with its headers; and also returns
631 * NULL wherever the ZERO_PAGE, or an anonymous pte_none, has been found -
632 * allowing a hole to be left in the corefile to save diskspace.
633 *
634 * Called without mmap_sem, but after all other threads have been killed.
635 */
636#ifdef CONFIG_ELF_CORE
637struct page *get_dump_page(unsigned long addr)
638{
639 struct vm_area_struct *vma;
640 struct page *page;
641
642 if (__get_user_pages(current, current->mm, addr, 1,
643 FOLL_FORCE | FOLL_DUMP | FOLL_GET, &page, &vma,
644 NULL) < 1)
645 return NULL;
646 flush_cache_page(vma, addr, page_to_pfn(page));
647 return page;
648}
649#endif /* CONFIG_ELF_CORE */
diff --git a/mm/internal.h b/mm/internal.h
index 07b67361a40a..6ee580d69ddd 100644
--- a/mm/internal.h
+++ b/mm/internal.h
@@ -169,6 +169,11 @@ static inline unsigned long page_order(struct page *page)
169 return page_private(page); 169 return page_private(page);
170} 170}
171 171
172static inline bool is_cow_mapping(vm_flags_t flags)
173{
174 return (flags & (VM_SHARED | VM_MAYWRITE)) == VM_MAYWRITE;
175}
176
172/* mm/util.c */ 177/* mm/util.c */
173void __vma_link_list(struct mm_struct *mm, struct vm_area_struct *vma, 178void __vma_link_list(struct mm_struct *mm, struct vm_area_struct *vma,
174 struct vm_area_struct *prev, struct rb_node *rb_parent); 179 struct vm_area_struct *prev, struct rb_node *rb_parent);
diff --git a/mm/memory.c b/mm/memory.c
index 0897830011f3..7049d394fa07 100644
--- a/mm/memory.c
+++ b/mm/memory.c
@@ -698,11 +698,6 @@ static void print_bad_pte(struct vm_area_struct *vma, unsigned long addr,
698 add_taint(TAINT_BAD_PAGE, LOCKDEP_NOW_UNRELIABLE); 698 add_taint(TAINT_BAD_PAGE, LOCKDEP_NOW_UNRELIABLE);
699} 699}
700 700
701static inline bool is_cow_mapping(vm_flags_t flags)
702{
703 return (flags & (VM_SHARED | VM_MAYWRITE)) == VM_MAYWRITE;
704}
705
706/* 701/*
707 * vm_normal_page -- This function gets the "struct page" associated with a pte. 702 * vm_normal_page -- This function gets the "struct page" associated with a pte.
708 * 703 *
@@ -1458,642 +1453,6 @@ int zap_vma_ptes(struct vm_area_struct *vma, unsigned long address,
1458} 1453}
1459EXPORT_SYMBOL_GPL(zap_vma_ptes); 1454EXPORT_SYMBOL_GPL(zap_vma_ptes);
1460 1455
1461/**
1462 * follow_page_mask - look up a page descriptor from a user-virtual address
1463 * @vma: vm_area_struct mapping @address
1464 * @address: virtual address to look up
1465 * @flags: flags modifying lookup behaviour
1466 * @page_mask: on output, *page_mask is set according to the size of the page
1467 *
1468 * @flags can have FOLL_ flags set, defined in <linux/mm.h>
1469 *
1470 * Returns the mapped (struct page *), %NULL if no mapping exists, or
1471 * an error pointer if there is a mapping to something not represented
1472 * by a page descriptor (see also vm_normal_page()).
1473 */
1474struct page *follow_page_mask(struct vm_area_struct *vma,
1475 unsigned long address, unsigned int flags,
1476 unsigned int *page_mask)
1477{
1478 pgd_t *pgd;
1479 pud_t *pud;
1480 pmd_t *pmd;
1481 pte_t *ptep, pte;
1482 spinlock_t *ptl;
1483 struct page *page;
1484 struct mm_struct *mm = vma->vm_mm;
1485
1486 *page_mask = 0;
1487
1488 page = follow_huge_addr(mm, address, flags & FOLL_WRITE);
1489 if (!IS_ERR(page)) {
1490 BUG_ON(flags & FOLL_GET);
1491 goto out;
1492 }
1493
1494 page = NULL;
1495 pgd = pgd_offset(mm, address);
1496 if (pgd_none(*pgd) || unlikely(pgd_bad(*pgd)))
1497 goto no_page_table;
1498
1499 pud = pud_offset(pgd, address);
1500 if (pud_none(*pud))
1501 goto no_page_table;
1502 if (pud_huge(*pud) && vma->vm_flags & VM_HUGETLB) {
1503 if (flags & FOLL_GET)
1504 goto out;
1505 page = follow_huge_pud(mm, address, pud, flags & FOLL_WRITE);
1506 goto out;
1507 }
1508 if (unlikely(pud_bad(*pud)))
1509 goto no_page_table;
1510
1511 pmd = pmd_offset(pud, address);
1512 if (pmd_none(*pmd))
1513 goto no_page_table;
1514 if (pmd_huge(*pmd) && vma->vm_flags & VM_HUGETLB) {
1515 page = follow_huge_pmd(mm, address, pmd, flags & FOLL_WRITE);
1516 if (flags & FOLL_GET) {
1517 /*
1518 * Refcount on tail pages are not well-defined and
1519 * shouldn't be taken. The caller should handle a NULL
1520 * return when trying to follow tail pages.
1521 */
1522 if (PageHead(page))
1523 get_page(page);
1524 else {
1525 page = NULL;
1526 goto out;
1527 }
1528 }
1529 goto out;
1530 }
1531 if ((flags & FOLL_NUMA) && pmd_numa(*pmd))
1532 goto no_page_table;
1533 if (pmd_trans_huge(*pmd)) {
1534 if (flags & FOLL_SPLIT) {
1535 split_huge_page_pmd(vma, address, pmd);
1536 goto split_fallthrough;
1537 }
1538 ptl = pmd_lock(mm, pmd);
1539 if (likely(pmd_trans_huge(*pmd))) {
1540 if (unlikely(pmd_trans_splitting(*pmd))) {
1541 spin_unlock(ptl);
1542 wait_split_huge_page(vma->anon_vma, pmd);
1543 } else {
1544 page = follow_trans_huge_pmd(vma, address,
1545 pmd, flags);
1546 spin_unlock(ptl);
1547 *page_mask = HPAGE_PMD_NR - 1;
1548 goto out;
1549 }
1550 } else
1551 spin_unlock(ptl);
1552 /* fall through */
1553 }
1554split_fallthrough:
1555 if (unlikely(pmd_bad(*pmd)))
1556 goto no_page_table;
1557
1558 ptep = pte_offset_map_lock(mm, pmd, address, &ptl);
1559
1560 pte = *ptep;
1561 if (!pte_present(pte)) {
1562 swp_entry_t entry;
1563 /*
1564 * KSM's break_ksm() relies upon recognizing a ksm page
1565 * even while it is being migrated, so for that case we
1566 * need migration_entry_wait().
1567 */
1568 if (likely(!(flags & FOLL_MIGRATION)))
1569 goto no_page;
1570 if (pte_none(pte) || pte_file(pte))
1571 goto no_page;
1572 entry = pte_to_swp_entry(pte);
1573 if (!is_migration_entry(entry))
1574 goto no_page;
1575 pte_unmap_unlock(ptep, ptl);
1576 migration_entry_wait(mm, pmd, address);
1577 goto split_fallthrough;
1578 }
1579 if ((flags & FOLL_NUMA) && pte_numa(pte))
1580 goto no_page;
1581 if ((flags & FOLL_WRITE) && !pte_write(pte))
1582 goto unlock;
1583
1584 page = vm_normal_page(vma, address, pte);
1585 if (unlikely(!page)) {
1586 if ((flags & FOLL_DUMP) ||
1587 !is_zero_pfn(pte_pfn(pte)))
1588 goto bad_page;
1589 page = pte_page(pte);
1590 }
1591
1592 if (flags & FOLL_GET)
1593 get_page_foll(page);
1594 if (flags & FOLL_TOUCH) {
1595 if ((flags & FOLL_WRITE) &&
1596 !pte_dirty(pte) && !PageDirty(page))
1597 set_page_dirty(page);
1598 /*
1599 * pte_mkyoung() would be more correct here, but atomic care
1600 * is needed to avoid losing the dirty bit: it is easier to use
1601 * mark_page_accessed().
1602 */
1603 mark_page_accessed(page);
1604 }
1605 if ((flags & FOLL_MLOCK) && (vma->vm_flags & VM_LOCKED)) {
1606 /*
1607 * The preliminary mapping check is mainly to avoid the
1608 * pointless overhead of lock_page on the ZERO_PAGE
1609 * which might bounce very badly if there is contention.
1610 *
1611 * If the page is already locked, we don't need to
1612 * handle it now - vmscan will handle it later if and
1613 * when it attempts to reclaim the page.
1614 */
1615 if (page->mapping && trylock_page(page)) {
1616 lru_add_drain(); /* push cached pages to LRU */
1617 /*
1618 * Because we lock page here, and migration is
1619 * blocked by the pte's page reference, and we
1620 * know the page is still mapped, we don't even
1621 * need to check for file-cache page truncation.
1622 */
1623 mlock_vma_page(page);
1624 unlock_page(page);
1625 }
1626 }
1627unlock:
1628 pte_unmap_unlock(ptep, ptl);
1629out:
1630 return page;
1631
1632bad_page:
1633 pte_unmap_unlock(ptep, ptl);
1634 return ERR_PTR(-EFAULT);
1635
1636no_page:
1637 pte_unmap_unlock(ptep, ptl);
1638 if (!pte_none(pte))
1639 return page;
1640
1641no_page_table:
1642 /*
1643 * When core dumping an enormous anonymous area that nobody
1644 * has touched so far, we don't want to allocate unnecessary pages or
1645 * page tables. Return error instead of NULL to skip handle_mm_fault,
1646 * then get_dump_page() will return NULL to leave a hole in the dump.
1647 * But we can only make this optimization where a hole would surely
1648 * be zero-filled if handle_mm_fault() actually did handle it.
1649 */
1650 if ((flags & FOLL_DUMP) &&
1651 (!vma->vm_ops || !vma->vm_ops->fault))
1652 return ERR_PTR(-EFAULT);
1653 return page;
1654}
1655
1656static inline int stack_guard_page(struct vm_area_struct *vma, unsigned long addr)
1657{
1658 return stack_guard_page_start(vma, addr) ||
1659 stack_guard_page_end(vma, addr+PAGE_SIZE);
1660}
1661
1662/**
1663 * __get_user_pages() - pin user pages in memory
1664 * @tsk: task_struct of target task
1665 * @mm: mm_struct of target mm
1666 * @start: starting user address
1667 * @nr_pages: number of pages from start to pin
1668 * @gup_flags: flags modifying pin behaviour
1669 * @pages: array that receives pointers to the pages pinned.
1670 * Should be at least nr_pages long. Or NULL, if caller
1671 * only intends to ensure the pages are faulted in.
1672 * @vmas: array of pointers to vmas corresponding to each page.
1673 * Or NULL if the caller does not require them.
1674 * @nonblocking: whether waiting for disk IO or mmap_sem contention
1675 *
1676 * Returns number of pages pinned. This may be fewer than the number
1677 * requested. If nr_pages is 0 or negative, returns 0. If no pages
1678 * were pinned, returns -errno. Each page returned must be released
1679 * with a put_page() call when it is finished with. vmas will only
1680 * remain valid while mmap_sem is held.
1681 *
1682 * Must be called with mmap_sem held for read or write.
1683 *
1684 * __get_user_pages walks a process's page tables and takes a reference to
1685 * each struct page that each user address corresponds to at a given
1686 * instant. That is, it takes the page that would be accessed if a user
1687 * thread accesses the given user virtual address at that instant.
1688 *
1689 * This does not guarantee that the page exists in the user mappings when
1690 * __get_user_pages returns, and there may even be a completely different
1691 * page there in some cases (eg. if mmapped pagecache has been invalidated
1692 * and subsequently re faulted). However it does guarantee that the page
1693 * won't be freed completely. And mostly callers simply care that the page
1694 * contains data that was valid *at some point in time*. Typically, an IO
1695 * or similar operation cannot guarantee anything stronger anyway because
1696 * locks can't be held over the syscall boundary.
1697 *
1698 * If @gup_flags & FOLL_WRITE == 0, the page must not be written to. If
1699 * the page is written to, set_page_dirty (or set_page_dirty_lock, as
1700 * appropriate) must be called after the page is finished with, and
1701 * before put_page is called.
1702 *
1703 * If @nonblocking != NULL, __get_user_pages will not wait for disk IO
1704 * or mmap_sem contention, and if waiting is needed to pin all pages,
1705 * *@nonblocking will be set to 0.
1706 *
1707 * In most cases, get_user_pages or get_user_pages_fast should be used
1708 * instead of __get_user_pages. __get_user_pages should be used only if
1709 * you need some special @gup_flags.
1710 */
1711long __get_user_pages(struct task_struct *tsk, struct mm_struct *mm,
1712 unsigned long start, unsigned long nr_pages,
1713 unsigned int gup_flags, struct page **pages,
1714 struct vm_area_struct **vmas, int *nonblocking)
1715{
1716 long i;
1717 unsigned long vm_flags;
1718 unsigned int page_mask;
1719
1720 if (!nr_pages)
1721 return 0;
1722
1723 VM_BUG_ON(!!pages != !!(gup_flags & FOLL_GET));
1724
1725 /*
1726 * If FOLL_FORCE is set then do not force a full fault as the hinting
1727 * fault information is unrelated to the reference behaviour of a task
1728 * using the address space
1729 */
1730 if (!(gup_flags & FOLL_FORCE))
1731 gup_flags |= FOLL_NUMA;
1732
1733 i = 0;
1734
1735 do {
1736 struct vm_area_struct *vma;
1737
1738 vma = find_extend_vma(mm, start);
1739 if (!vma && in_gate_area(mm, start)) {
1740 unsigned long pg = start & PAGE_MASK;
1741 pgd_t *pgd;
1742 pud_t *pud;
1743 pmd_t *pmd;
1744 pte_t *pte;
1745
1746 /* user gate pages are read-only */
1747 if (gup_flags & FOLL_WRITE)
1748 goto efault;
1749 if (pg > TASK_SIZE)
1750 pgd = pgd_offset_k(pg);
1751 else
1752 pgd = pgd_offset_gate(mm, pg);
1753 BUG_ON(pgd_none(*pgd));
1754 pud = pud_offset(pgd, pg);
1755 BUG_ON(pud_none(*pud));
1756 pmd = pmd_offset(pud, pg);
1757 if (pmd_none(*pmd))
1758 goto efault;
1759 VM_BUG_ON(pmd_trans_huge(*pmd));
1760 pte = pte_offset_map(pmd, pg);
1761 if (pte_none(*pte)) {
1762 pte_unmap(pte);
1763 goto efault;
1764 }
1765 vma = get_gate_vma(mm);
1766 if (pages) {
1767 struct page *page;
1768
1769 page = vm_normal_page(vma, start, *pte);
1770 if (!page) {
1771 if (!(gup_flags & FOLL_DUMP) &&
1772 is_zero_pfn(pte_pfn(*pte)))
1773 page = pte_page(*pte);
1774 else {
1775 pte_unmap(pte);
1776 goto efault;
1777 }
1778 }
1779 pages[i] = page;
1780 get_page(page);
1781 }
1782 pte_unmap(pte);
1783 page_mask = 0;
1784 goto next_page;
1785 }
1786
1787 if (!vma)
1788 goto efault;
1789 vm_flags = vma->vm_flags;
1790 if (vm_flags & (VM_IO | VM_PFNMAP))
1791 goto efault;
1792
1793 if (gup_flags & FOLL_WRITE) {
1794 if (!(vm_flags & VM_WRITE)) {
1795 if (!(gup_flags & FOLL_FORCE))
1796 goto efault;
1797 /*
1798 * We used to let the write,force case do COW
1799 * in a VM_MAYWRITE VM_SHARED !VM_WRITE vma, so
1800 * ptrace could set a breakpoint in a read-only
1801 * mapping of an executable, without corrupting
1802 * the file (yet only when that file had been
1803 * opened for writing!). Anon pages in shared
1804 * mappings are surprising: now just reject it.
1805 */
1806 if (!is_cow_mapping(vm_flags)) {
1807 WARN_ON_ONCE(vm_flags & VM_MAYWRITE);
1808 goto efault;
1809 }
1810 }
1811 } else {
1812 if (!(vm_flags & VM_READ)) {
1813 if (!(gup_flags & FOLL_FORCE))
1814 goto efault;
1815 /*
1816 * Is there actually any vma we can reach here
1817 * which does not have VM_MAYREAD set?
1818 */
1819 if (!(vm_flags & VM_MAYREAD))
1820 goto efault;
1821 }
1822 }
1823
1824 if (is_vm_hugetlb_page(vma)) {
1825 i = follow_hugetlb_page(mm, vma, pages, vmas,
1826 &start, &nr_pages, i, gup_flags);
1827 continue;
1828 }
1829
1830 do {
1831 struct page *page;
1832 unsigned int foll_flags = gup_flags;
1833 unsigned int page_increm;
1834
1835 /*
1836 * If we have a pending SIGKILL, don't keep faulting
1837 * pages and potentially allocating memory.
1838 */
1839 if (unlikely(fatal_signal_pending(current)))
1840 return i ? i : -ERESTARTSYS;
1841
1842 cond_resched();
1843 while (!(page = follow_page_mask(vma, start,
1844 foll_flags, &page_mask))) {
1845 int ret;
1846 unsigned int fault_flags = 0;
1847
1848 /* For mlock, just skip the stack guard page. */
1849 if (foll_flags & FOLL_MLOCK) {
1850 if (stack_guard_page(vma, start))
1851 goto next_page;
1852 }
1853 if (foll_flags & FOLL_WRITE)
1854 fault_flags |= FAULT_FLAG_WRITE;
1855 if (nonblocking)
1856 fault_flags |= FAULT_FLAG_ALLOW_RETRY;
1857 if (foll_flags & FOLL_NOWAIT)
1858 fault_flags |= (FAULT_FLAG_ALLOW_RETRY | FAULT_FLAG_RETRY_NOWAIT);
1859
1860 ret = handle_mm_fault(mm, vma, start,
1861 fault_flags);
1862
1863 if (ret & VM_FAULT_ERROR) {
1864 if (ret & VM_FAULT_OOM)
1865 return i ? i : -ENOMEM;
1866 if (ret & (VM_FAULT_HWPOISON |
1867 VM_FAULT_HWPOISON_LARGE)) {
1868 if (i)
1869 return i;
1870 else if (gup_flags & FOLL_HWPOISON)
1871 return -EHWPOISON;
1872 else
1873 return -EFAULT;
1874 }
1875 if (ret & VM_FAULT_SIGBUS)
1876 goto efault;
1877 BUG();
1878 }
1879
1880 if (tsk) {
1881 if (ret & VM_FAULT_MAJOR)
1882 tsk->maj_flt++;
1883 else
1884 tsk->min_flt++;
1885 }
1886
1887 if (ret & VM_FAULT_RETRY) {
1888 if (nonblocking)
1889 *nonblocking = 0;
1890 return i;
1891 }
1892
1893 /*
1894 * The VM_FAULT_WRITE bit tells us that
1895 * do_wp_page has broken COW when necessary,
1896 * even if maybe_mkwrite decided not to set
1897 * pte_write. We can thus safely do subsequent
1898 * page lookups as if they were reads. But only
1899 * do so when looping for pte_write is futile:
1900 * in some cases userspace may also be wanting
1901 * to write to the gotten user page, which a
1902 * read fault here might prevent (a readonly
1903 * page might get reCOWed by userspace write).
1904 */
1905 if ((ret & VM_FAULT_WRITE) &&
1906 !(vma->vm_flags & VM_WRITE))
1907 foll_flags &= ~FOLL_WRITE;
1908
1909 cond_resched();
1910 }
1911 if (IS_ERR(page))
1912 return i ? i : PTR_ERR(page);
1913 if (pages) {
1914 pages[i] = page;
1915
1916 flush_anon_page(vma, page, start);
1917 flush_dcache_page(page);
1918 page_mask = 0;
1919 }
1920next_page:
1921 if (vmas) {
1922 vmas[i] = vma;
1923 page_mask = 0;
1924 }
1925 page_increm = 1 + (~(start >> PAGE_SHIFT) & page_mask);
1926 if (page_increm > nr_pages)
1927 page_increm = nr_pages;
1928 i += page_increm;
1929 start += page_increm * PAGE_SIZE;
1930 nr_pages -= page_increm;
1931 } while (nr_pages && start < vma->vm_end);
1932 } while (nr_pages);
1933 return i;
1934efault:
1935 return i ? : -EFAULT;
1936}
1937EXPORT_SYMBOL(__get_user_pages);
1938
1939/*
1940 * fixup_user_fault() - manually resolve a user page fault
1941 * @tsk: the task_struct to use for page fault accounting, or
1942 * NULL if faults are not to be recorded.
1943 * @mm: mm_struct of target mm
1944 * @address: user address
1945 * @fault_flags:flags to pass down to handle_mm_fault()
1946 *
1947 * This is meant to be called in the specific scenario where for locking reasons
1948 * we try to access user memory in atomic context (within a pagefault_disable()
1949 * section), this returns -EFAULT, and we want to resolve the user fault before
1950 * trying again.
1951 *
1952 * Typically this is meant to be used by the futex code.
1953 *
1954 * The main difference with get_user_pages() is that this function will
1955 * unconditionally call handle_mm_fault() which will in turn perform all the
1956 * necessary SW fixup of the dirty and young bits in the PTE, while
1957 * handle_mm_fault() only guarantees to update these in the struct page.
1958 *
1959 * This is important for some architectures where those bits also gate the
1960 * access permission to the page because they are maintained in software. On
1961 * such architectures, gup() will not be enough to make a subsequent access
1962 * succeed.
1963 *
1964 * This should be called with the mm_sem held for read.
1965 */
1966int fixup_user_fault(struct task_struct *tsk, struct mm_struct *mm,
1967 unsigned long address, unsigned int fault_flags)
1968{
1969 struct vm_area_struct *vma;
1970 vm_flags_t vm_flags;
1971 int ret;
1972
1973 vma = find_extend_vma(mm, address);
1974 if (!vma || address < vma->vm_start)
1975 return -EFAULT;
1976
1977 vm_flags = (fault_flags & FAULT_FLAG_WRITE) ? VM_WRITE : VM_READ;
1978 if (!(vm_flags & vma->vm_flags))
1979 return -EFAULT;
1980
1981 ret = handle_mm_fault(mm, vma, address, fault_flags);
1982 if (ret & VM_FAULT_ERROR) {
1983 if (ret & VM_FAULT_OOM)
1984 return -ENOMEM;
1985 if (ret & (VM_FAULT_HWPOISON | VM_FAULT_HWPOISON_LARGE))
1986 return -EHWPOISON;
1987 if (ret & VM_FAULT_SIGBUS)
1988 return -EFAULT;
1989 BUG();
1990 }
1991 if (tsk) {
1992 if (ret & VM_FAULT_MAJOR)
1993 tsk->maj_flt++;
1994 else
1995 tsk->min_flt++;
1996 }
1997 return 0;
1998}
1999
2000/*
2001 * get_user_pages() - pin user pages in memory
2002 * @tsk: the task_struct to use for page fault accounting, or
2003 * NULL if faults are not to be recorded.
2004 * @mm: mm_struct of target mm
2005 * @start: starting user address
2006 * @nr_pages: number of pages from start to pin
2007 * @write: whether pages will be written to by the caller
2008 * @force: whether to force access even when user mapping is currently
2009 * protected (but never forces write access to shared mapping).
2010 * @pages: array that receives pointers to the pages pinned.
2011 * Should be at least nr_pages long. Or NULL, if caller
2012 * only intends to ensure the pages are faulted in.
2013 * @vmas: array of pointers to vmas corresponding to each page.
2014 * Or NULL if the caller does not require them.
2015 *
2016 * Returns number of pages pinned. This may be fewer than the number
2017 * requested. If nr_pages is 0 or negative, returns 0. If no pages
2018 * were pinned, returns -errno. Each page returned must be released
2019 * with a put_page() call when it is finished with. vmas will only
2020 * remain valid while mmap_sem is held.
2021 *
2022 * Must be called with mmap_sem held for read or write.
2023 *
2024 * get_user_pages walks a process's page tables and takes a reference to
2025 * each struct page that each user address corresponds to at a given
2026 * instant. That is, it takes the page that would be accessed if a user
2027 * thread accesses the given user virtual address at that instant.
2028 *
2029 * This does not guarantee that the page exists in the user mappings when
2030 * get_user_pages returns, and there may even be a completely different
2031 * page there in some cases (eg. if mmapped pagecache has been invalidated
2032 * and subsequently re faulted). However it does guarantee that the page
2033 * won't be freed completely. And mostly callers simply care that the page
2034 * contains data that was valid *at some point in time*. Typically, an IO
2035 * or similar operation cannot guarantee anything stronger anyway because
2036 * locks can't be held over the syscall boundary.
2037 *
2038 * If write=0, the page must not be written to. If the page is written to,
2039 * set_page_dirty (or set_page_dirty_lock, as appropriate) must be called
2040 * after the page is finished with, and before put_page is called.
2041 *
2042 * get_user_pages is typically used for fewer-copy IO operations, to get a
2043 * handle on the memory by some means other than accesses via the user virtual
2044 * addresses. The pages may be submitted for DMA to devices or accessed via
2045 * their kernel linear mapping (via the kmap APIs). Care should be taken to
2046 * use the correct cache flushing APIs.
2047 *
2048 * See also get_user_pages_fast, for performance critical applications.
2049 */
2050long get_user_pages(struct task_struct *tsk, struct mm_struct *mm,
2051 unsigned long start, unsigned long nr_pages, int write,
2052 int force, struct page **pages, struct vm_area_struct **vmas)
2053{
2054 int flags = FOLL_TOUCH;
2055
2056 if (pages)
2057 flags |= FOLL_GET;
2058 if (write)
2059 flags |= FOLL_WRITE;
2060 if (force)
2061 flags |= FOLL_FORCE;
2062
2063 return __get_user_pages(tsk, mm, start, nr_pages, flags, pages, vmas,
2064 NULL);
2065}
2066EXPORT_SYMBOL(get_user_pages);
2067
2068/**
2069 * get_dump_page() - pin user page in memory while writing it to core dump
2070 * @addr: user address
2071 *
2072 * Returns struct page pointer of user page pinned for dump,
2073 * to be freed afterwards by page_cache_release() or put_page().
2074 *
2075 * Returns NULL on any kind of failure - a hole must then be inserted into
2076 * the corefile, to preserve alignment with its headers; and also returns
2077 * NULL wherever the ZERO_PAGE, or an anonymous pte_none, has been found -
2078 * allowing a hole to be left in the corefile to save diskspace.
2079 *
2080 * Called without mmap_sem, but after all other threads have been killed.
2081 */
2082#ifdef CONFIG_ELF_CORE
2083struct page *get_dump_page(unsigned long addr)
2084{
2085 struct vm_area_struct *vma;
2086 struct page *page;
2087
2088 if (__get_user_pages(current, current->mm, addr, 1,
2089 FOLL_FORCE | FOLL_DUMP | FOLL_GET, &page, &vma,
2090 NULL) < 1)
2091 return NULL;
2092 flush_cache_page(vma, addr, page_to_pfn(page));
2093 return page;
2094}
2095#endif /* CONFIG_ELF_CORE */
2096
2097pte_t *__get_locked_pte(struct mm_struct *mm, unsigned long addr, 1456pte_t *__get_locked_pte(struct mm_struct *mm, unsigned long addr,
2098 spinlock_t **ptl) 1457 spinlock_t **ptl)
2099{ 1458{