blob: de24ef4cd1af84c592ec0cfd641e11f983eacf08 [file] [log] [blame]
Dave Hansencde70142016-02-12 13:01:55 -08001#define __DISABLE_GUP_DEPRECATED 1
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -07002#include <linux/kernel.h>
3#include <linux/errno.h>
4#include <linux/err.h>
5#include <linux/spinlock.h>
6
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -07007#include <linux/mm.h>
Dan Williams3565fce2016-01-15 16:56:55 -08008#include <linux/memremap.h>
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -07009#include <linux/pagemap.h>
10#include <linux/rmap.h>
11#include <linux/swap.h>
12#include <linux/swapops.h>
13
Steve Capper2667f502014-10-09 15:29:14 -070014#include <linux/sched.h>
15#include <linux/rwsem.h>
Aneesh Kumar K.Vf30c59e2014-11-05 21:57:40 +053016#include <linux/hugetlb.h>
Kirill A. Shutemov1027e442015-09-04 15:47:55 -070017
Steve Capper2667f502014-10-09 15:29:14 -070018#include <asm/pgtable.h>
Kirill A. Shutemov1027e442015-09-04 15:47:55 -070019#include <asm/tlbflush.h>
Steve Capper2667f502014-10-09 15:29:14 -070020
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -070021#include "internal.h"
22
Kirill A. Shutemov69e68b42014-06-04 16:08:11 -070023static struct page *no_page_table(struct vm_area_struct *vma,
24 unsigned int flags)
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -070025{
Kirill A. Shutemov69e68b42014-06-04 16:08:11 -070026 /*
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}
38
Kirill A. Shutemov1027e442015-09-04 15:47:55 -070039static 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
Kirill A. Shutemov69e68b42014-06-04 16:08:11 -070063static struct page *follow_page_pte(struct vm_area_struct *vma,
64 unsigned long address, pmd_t *pmd, unsigned int flags)
65{
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -070066 struct mm_struct *mm = vma->vm_mm;
Dan Williams3565fce2016-01-15 16:56:55 -080067 struct dev_pagemap *pgmap = NULL;
Kirill A. Shutemov69e68b42014-06-04 16:08:11 -070068 struct page *page;
69 spinlock_t *ptl;
70 pte_t *ptep, pte;
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -070071
Kirill A. Shutemov69e68b42014-06-04 16:08:11 -070072retry:
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -070073 if (unlikely(pmd_bad(*pmd)))
Kirill A. Shutemov69e68b42014-06-04 16:08:11 -070074 return no_page_table(vma, flags);
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -070075
76 ptep = pte_offset_map_lock(mm, pmd, address, &ptl);
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -070077 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;
Kirill A. Shutemov0661a332015-02-10 14:10:04 -080087 if (pte_none(pte))
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -070088 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);
Kirill A. Shutemov69e68b42014-06-04 16:08:11 -070094 goto retry;
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -070095 }
Mel Gorman8a0516e2015-02-12 14:58:22 -080096 if ((flags & FOLL_NUMA) && pte_protnone(pte))
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -070097 goto no_page;
Kirill A. Shutemov69e68b42014-06-04 16:08:11 -070098 if ((flags & FOLL_WRITE) && !pte_write(pte)) {
99 pte_unmap_unlock(ptep, ptl);
100 return NULL;
101 }
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700102
103 page = vm_normal_page(vma, address, pte);
Dan Williams3565fce2016-01-15 16:56:55 -0800104 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)) {
Kirill A. Shutemov1027e442015-09-04 15:47:55 -0700115 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 }
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700130 }
131
Kirill A. Shutemov6742d292016-01-15 16:52:28 -0800132 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
Dan Williams3565fce2016-01-15 16:56:55 -0800145 if (flags & FOLL_GET) {
Kirill A. Shutemovddc58f22016-01-15 16:52:56 -0800146 get_page(page);
Dan Williams3565fce2016-01-15 16:56:55 -0800147
148 /* drop the pgmap reference now that we hold the page */
149 if (pgmap) {
150 put_dev_pagemap(pgmap);
151 pgmap = NULL;
152 }
153 }
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700154 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 }
Eric B Munsonde60f5f2015-11-05 18:51:36 -0800165 if ((flags & FOLL_MLOCK) && (vma->vm_flags & VM_LOCKED)) {
Kirill A. Shutemove90309c2016-01-15 16:54:33 -0800166 /* Do not mlock pte-mapped THP */
167 if (PageTransCompound(page))
168 goto out;
169
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700170 /*
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 }
Kirill A. Shutemov1027e442015-09-04 15:47:55 -0700191out:
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700192 pte_unmap_unlock(ptep, ptl);
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700193 return page;
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700194no_page:
195 pte_unmap_unlock(ptep, ptl);
196 if (!pte_none(pte))
Kirill A. Shutemov69e68b42014-06-04 16:08:11 -0700197 return NULL;
198 return no_page_table(vma, flags);
199}
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700200
Kirill A. Shutemov69e68b42014-06-04 16:08:11 -0700201/**
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);
230 return page;
231 }
232
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) {
Naoya Horiguchie66f17f2015-02-11 15:25:22 -0800241 page = follow_huge_pud(mm, address, pud, flags);
242 if (page)
243 return page;
244 return no_page_table(vma, flags);
Kirill A. Shutemov69e68b42014-06-04 16:08:11 -0700245 }
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) {
Naoya Horiguchie66f17f2015-02-11 15:25:22 -0800253 page = follow_huge_pmd(mm, address, pmd, flags);
254 if (page)
255 return page;
256 return no_page_table(vma, flags);
Kirill A. Shutemov69e68b42014-06-04 16:08:11 -0700257 }
Mel Gorman8a0516e2015-02-12 14:58:22 -0800258 if ((flags & FOLL_NUMA) && pmd_protnone(*pmd))
Kirill A. Shutemov69e68b42014-06-04 16:08:11 -0700259 return no_page_table(vma, flags);
Dan Williams3565fce2016-01-15 16:56:55 -0800260 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 }
Kirill A. Shutemov6742d292016-01-15 16:52:28 -0800267 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);
Kirill A. Shutemov69e68b42014-06-04 16:08:11 -0700274 }
Kirill A. Shutemov6742d292016-01-15 16:52:28 -0800275 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;
Kirill A. Shutemov78ddc532016-01-15 16:52:42 -0800281 split_huge_pmd(vma, pmd, address);
Kirill A. Shutemov6742d292016-01-15 16:52:28 -0800282 } else {
283 get_page(page);
284 spin_unlock(ptl);
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);
293 }
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;
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700299}
300
Kirill A. Shutemovf2b495c2014-06-04 16:08:11 -0700301static 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
Paul Cassella9a95f3c2014-08-06 16:07:24 -0700345/*
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 */
Kirill A. Shutemov16744482014-06-04 16:08:12 -0700350static 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
Eric B Munsonde60f5f2015-11-05 18:51:36 -0800357 /* mlock all present pages, but do not fault in new pages */
358 if ((*flags & (FOLL_POPULATE | FOLL_MLOCK)) == FOLL_MLOCK)
359 return -ENOENT;
Kirill A. Shutemov84d33df2015-04-14 15:44:37 -0700360 /* For mm_populate(), just skip the stack guard page. */
361 if ((*flags & FOLL_POPULATE) &&
Kirill A. Shutemov16744482014-06-04 16:08:12 -0700362 (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;
Andres Lagar-Cavilla234b2392014-09-17 10:51:48 -0700371 if (*flags & FOLL_TRIED) {
372 VM_WARN_ON_ONCE(fault_flags & FAULT_FLAG_ALLOW_RETRY);
373 fault_flags |= FAULT_FLAG_TRIED;
374 }
Kirill A. Shutemov16744482014-06-04 16:08:12 -0700375
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;
Linus Torvalds33692f22015-01-29 10:51:32 -0800382 if (ret & (VM_FAULT_SIGBUS | VM_FAULT_SIGSEGV))
Kirill A. Shutemov16744482014-06-04 16:08:12 -0700383 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
Kirill A. Shutemovfa5bb202014-06-04 16:08:13 -0700414static 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 */
Hugh Dickins46435362016-01-30 18:03:16 -0800434 if (!is_cow_mapping(vm_flags))
Kirill A. Shutemovfa5bb202014-06-04 16:08:13 -0700435 return -EFAULT;
Kirill A. Shutemovfa5bb202014-06-04 16:08:13 -0700436 }
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
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700450/**
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 *
Paul Cassella9a95f3c2014-08-06 16:07:24 -0700470 * Must be called with mmap_sem held. It may be released. See below.
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700471 *
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,
Paul Cassella9a95f3c2014-08-06 16:07:24 -0700493 * *@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.
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700501 *
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{
Kirill A. Shutemovfa5bb202014-06-04 16:08:13 -0700511 long i = 0;
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700512 unsigned int page_mask;
Kirill A. Shutemovfa5bb202014-06-04 16:08:13 -0700513 struct vm_area_struct *vma = NULL;
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700514
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
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700528 do {
Kirill A. Shutemovfa5bb202014-06-04 16:08:13 -0700529 struct page *page;
530 unsigned int foll_flags = gup_flags;
531 unsigned int page_increm;
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700532
Kirill A. Shutemovfa5bb202014-06-04 16:08:13 -0700533 /* 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)) {
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700537 int ret;
Kirill A. Shutemovfa5bb202014-06-04 16:08:13 -0700538 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;
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700545 }
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700546
Kirill A. Shutemovfa5bb202014-06-04 16:08:13 -0700547 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;
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700554 }
Kirill A. Shutemovfa5bb202014-06-04 16:08:13 -0700555 }
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;
580 }
581 BUG();
Kirill A. Shutemov1027e442015-09-04 15:47:55 -0700582 } 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)) {
Kirill A. Shutemovfa5bb202014-06-04 16:08:13 -0700589 return i ? i : PTR_ERR(page);
Kirill A. Shutemov1027e442015-09-04 15:47:55 -0700590 }
Kirill A. Shutemovfa5bb202014-06-04 16:08:13 -0700591 if (pages) {
592 pages[i] = page;
593 flush_anon_page(vma, page, start);
594 flush_dcache_page(page);
595 page_mask = 0;
596 }
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700597next_page:
Kirill A. Shutemovfa5bb202014-06-04 16:08:13 -0700598 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;
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700608 } while (nr_pages);
609 return i;
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700610}
611EXPORT_SYMBOL(__get_user_pages);
612
613/*
614 * fixup_user_fault() - manually resolve a user page fault
615 * @tsk: the task_struct to use for page fault accounting, or
616 * NULL if faults are not to be recorded.
617 * @mm: mm_struct of target mm
618 * @address: user address
619 * @fault_flags:flags to pass down to handle_mm_fault()
Dominik Dingel4a9e1cd2016-01-15 16:57:04 -0800620 * @unlocked: did we unlock the mmap_sem while retrying, maybe NULL if caller
621 * does not allow retry
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700622 *
623 * This is meant to be called in the specific scenario where for locking reasons
624 * we try to access user memory in atomic context (within a pagefault_disable()
625 * section), this returns -EFAULT, and we want to resolve the user fault before
626 * trying again.
627 *
628 * Typically this is meant to be used by the futex code.
629 *
630 * The main difference with get_user_pages() is that this function will
631 * unconditionally call handle_mm_fault() which will in turn perform all the
632 * necessary SW fixup of the dirty and young bits in the PTE, while
Dominik Dingel4a9e1cd2016-01-15 16:57:04 -0800633 * get_user_pages() only guarantees to update these in the struct page.
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700634 *
635 * This is important for some architectures where those bits also gate the
636 * access permission to the page because they are maintained in software. On
637 * such architectures, gup() will not be enough to make a subsequent access
638 * succeed.
639 *
Dominik Dingel4a9e1cd2016-01-15 16:57:04 -0800640 * This function will not return with an unlocked mmap_sem. So it has not the
641 * same semantics wrt the @mm->mmap_sem as does filemap_fault().
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700642 */
643int fixup_user_fault(struct task_struct *tsk, struct mm_struct *mm,
Dominik Dingel4a9e1cd2016-01-15 16:57:04 -0800644 unsigned long address, unsigned int fault_flags,
645 bool *unlocked)
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700646{
647 struct vm_area_struct *vma;
648 vm_flags_t vm_flags;
Dominik Dingel4a9e1cd2016-01-15 16:57:04 -0800649 int ret, major = 0;
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700650
Dominik Dingel4a9e1cd2016-01-15 16:57:04 -0800651 if (unlocked)
652 fault_flags |= FAULT_FLAG_ALLOW_RETRY;
653
654retry:
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700655 vma = find_extend_vma(mm, address);
656 if (!vma || address < vma->vm_start)
657 return -EFAULT;
658
659 vm_flags = (fault_flags & FAULT_FLAG_WRITE) ? VM_WRITE : VM_READ;
660 if (!(vm_flags & vma->vm_flags))
661 return -EFAULT;
662
663 ret = handle_mm_fault(mm, vma, address, fault_flags);
Dominik Dingel4a9e1cd2016-01-15 16:57:04 -0800664 major |= ret & VM_FAULT_MAJOR;
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700665 if (ret & VM_FAULT_ERROR) {
666 if (ret & VM_FAULT_OOM)
667 return -ENOMEM;
668 if (ret & (VM_FAULT_HWPOISON | VM_FAULT_HWPOISON_LARGE))
669 return -EHWPOISON;
Linus Torvalds33692f22015-01-29 10:51:32 -0800670 if (ret & (VM_FAULT_SIGBUS | VM_FAULT_SIGSEGV))
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700671 return -EFAULT;
672 BUG();
673 }
Dominik Dingel4a9e1cd2016-01-15 16:57:04 -0800674
675 if (ret & VM_FAULT_RETRY) {
676 down_read(&mm->mmap_sem);
677 if (!(fault_flags & FAULT_FLAG_TRIED)) {
678 *unlocked = true;
679 fault_flags &= ~FAULT_FLAG_ALLOW_RETRY;
680 fault_flags |= FAULT_FLAG_TRIED;
681 goto retry;
682 }
683 }
684
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700685 if (tsk) {
Dominik Dingel4a9e1cd2016-01-15 16:57:04 -0800686 if (major)
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700687 tsk->maj_flt++;
688 else
689 tsk->min_flt++;
690 }
691 return 0;
692}
693
Andrea Arcangelif0818f42015-02-11 15:27:17 -0800694static __always_inline long __get_user_pages_locked(struct task_struct *tsk,
695 struct mm_struct *mm,
696 unsigned long start,
697 unsigned long nr_pages,
698 int write, int force,
699 struct page **pages,
700 struct vm_area_struct **vmas,
Andrea Arcangeli0fd71a52015-02-11 15:27:20 -0800701 int *locked, bool notify_drop,
702 unsigned int flags)
Andrea Arcangelif0818f42015-02-11 15:27:17 -0800703{
Andrea Arcangelif0818f42015-02-11 15:27:17 -0800704 long ret, pages_done;
705 bool lock_dropped;
706
707 if (locked) {
708 /* if VM_FAULT_RETRY can be returned, vmas become invalid */
709 BUG_ON(vmas);
710 /* check caller initialized locked */
711 BUG_ON(*locked != 1);
712 }
713
714 if (pages)
715 flags |= FOLL_GET;
716 if (write)
717 flags |= FOLL_WRITE;
718 if (force)
719 flags |= FOLL_FORCE;
720
721 pages_done = 0;
722 lock_dropped = false;
723 for (;;) {
724 ret = __get_user_pages(tsk, mm, start, nr_pages, flags, pages,
725 vmas, locked);
726 if (!locked)
727 /* VM_FAULT_RETRY couldn't trigger, bypass */
728 return ret;
729
730 /* VM_FAULT_RETRY cannot return errors */
731 if (!*locked) {
732 BUG_ON(ret < 0);
733 BUG_ON(ret >= nr_pages);
734 }
735
736 if (!pages)
737 /* If it's a prefault don't insist harder */
738 return ret;
739
740 if (ret > 0) {
741 nr_pages -= ret;
742 pages_done += ret;
743 if (!nr_pages)
744 break;
745 }
746 if (*locked) {
747 /* VM_FAULT_RETRY didn't trigger */
748 if (!pages_done)
749 pages_done = ret;
750 break;
751 }
752 /* VM_FAULT_RETRY triggered, so seek to the faulting offset */
753 pages += ret;
754 start += ret << PAGE_SHIFT;
755
756 /*
757 * Repeat on the address that fired VM_FAULT_RETRY
758 * without FAULT_FLAG_ALLOW_RETRY but with
759 * FAULT_FLAG_TRIED.
760 */
761 *locked = 1;
762 lock_dropped = true;
763 down_read(&mm->mmap_sem);
764 ret = __get_user_pages(tsk, mm, start, 1, flags | FOLL_TRIED,
765 pages, NULL, NULL);
766 if (ret != 1) {
767 BUG_ON(ret > 1);
768 if (!pages_done)
769 pages_done = ret;
770 break;
771 }
772 nr_pages--;
773 pages_done++;
774 if (!nr_pages)
775 break;
776 pages++;
777 start += PAGE_SIZE;
778 }
779 if (notify_drop && lock_dropped && *locked) {
780 /*
781 * We must let the caller know we temporarily dropped the lock
782 * and so the critical section protected by it was lost.
783 */
784 up_read(&mm->mmap_sem);
785 *locked = 0;
786 }
787 return pages_done;
788}
789
790/*
791 * We can leverage the VM_FAULT_RETRY functionality in the page fault
792 * paths better by using either get_user_pages_locked() or
793 * get_user_pages_unlocked().
794 *
795 * get_user_pages_locked() is suitable to replace the form:
796 *
797 * down_read(&mm->mmap_sem);
798 * do_something()
799 * get_user_pages(tsk, mm, ..., pages, NULL);
800 * up_read(&mm->mmap_sem);
801 *
802 * to:
803 *
804 * int locked = 1;
805 * down_read(&mm->mmap_sem);
806 * do_something()
807 * get_user_pages_locked(tsk, mm, ..., pages, &locked);
808 * if (locked)
809 * up_read(&mm->mmap_sem);
810 */
Dave Hansencde70142016-02-12 13:01:55 -0800811long get_user_pages_locked6(unsigned long start, unsigned long nr_pages,
Andrea Arcangelif0818f42015-02-11 15:27:17 -0800812 int write, int force, struct page **pages,
813 int *locked)
814{
Dave Hansencde70142016-02-12 13:01:55 -0800815 return __get_user_pages_locked(current, current->mm, start, nr_pages,
816 write, force, pages, NULL, locked, true,
817 FOLL_TOUCH);
Andrea Arcangelif0818f42015-02-11 15:27:17 -0800818}
Dave Hansencde70142016-02-12 13:01:55 -0800819EXPORT_SYMBOL(get_user_pages_locked6);
Andrea Arcangelif0818f42015-02-11 15:27:17 -0800820
821/*
Andrea Arcangeli0fd71a52015-02-11 15:27:20 -0800822 * Same as get_user_pages_unlocked(...., FOLL_TOUCH) but it allows to
823 * pass additional gup_flags as last parameter (like FOLL_HWPOISON).
824 *
825 * NOTE: here FOLL_TOUCH is not set implicitly and must be set by the
826 * caller if required (just like with __get_user_pages). "FOLL_GET",
827 * "FOLL_WRITE" and "FOLL_FORCE" are set implicitly as needed
828 * according to the parameters "pages", "write", "force"
829 * respectively.
830 */
831__always_inline long __get_user_pages_unlocked(struct task_struct *tsk, struct mm_struct *mm,
832 unsigned long start, unsigned long nr_pages,
833 int write, int force, struct page **pages,
834 unsigned int gup_flags)
835{
836 long ret;
837 int locked = 1;
838 down_read(&mm->mmap_sem);
839 ret = __get_user_pages_locked(tsk, mm, start, nr_pages, write, force,
840 pages, NULL, &locked, false, gup_flags);
841 if (locked)
842 up_read(&mm->mmap_sem);
843 return ret;
844}
845EXPORT_SYMBOL(__get_user_pages_unlocked);
846
847/*
Andrea Arcangelif0818f42015-02-11 15:27:17 -0800848 * get_user_pages_unlocked() is suitable to replace the form:
849 *
850 * down_read(&mm->mmap_sem);
851 * get_user_pages(tsk, mm, ..., pages, NULL);
852 * up_read(&mm->mmap_sem);
853 *
854 * with:
855 *
856 * get_user_pages_unlocked(tsk, mm, ..., pages);
857 *
858 * It is functionally equivalent to get_user_pages_fast so
859 * get_user_pages_fast should be used instead, if the two parameters
860 * "tsk" and "mm" are respectively equal to current and current->mm,
861 * or if "force" shall be set to 1 (get_user_pages_fast misses the
862 * "force" parameter).
863 */
Dave Hansencde70142016-02-12 13:01:55 -0800864long get_user_pages_unlocked5(unsigned long start, unsigned long nr_pages,
Andrea Arcangelif0818f42015-02-11 15:27:17 -0800865 int write, int force, struct page **pages)
866{
Dave Hansencde70142016-02-12 13:01:55 -0800867 return __get_user_pages_unlocked(current, current->mm, start, nr_pages,
868 write, force, pages, FOLL_TOUCH);
Andrea Arcangelif0818f42015-02-11 15:27:17 -0800869}
Dave Hansencde70142016-02-12 13:01:55 -0800870EXPORT_SYMBOL(get_user_pages_unlocked5);
Andrea Arcangelif0818f42015-02-11 15:27:17 -0800871
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700872/*
Dave Hansen1e987792016-02-12 13:01:54 -0800873 * get_user_pages_remote() - pin user pages in memory
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700874 * @tsk: the task_struct to use for page fault accounting, or
875 * NULL if faults are not to be recorded.
876 * @mm: mm_struct of target mm
877 * @start: starting user address
878 * @nr_pages: number of pages from start to pin
879 * @write: whether pages will be written to by the caller
880 * @force: whether to force access even when user mapping is currently
881 * protected (but never forces write access to shared mapping).
882 * @pages: array that receives pointers to the pages pinned.
883 * Should be at least nr_pages long. Or NULL, if caller
884 * only intends to ensure the pages are faulted in.
885 * @vmas: array of pointers to vmas corresponding to each page.
886 * Or NULL if the caller does not require them.
887 *
888 * Returns number of pages pinned. This may be fewer than the number
889 * requested. If nr_pages is 0 or negative, returns 0. If no pages
890 * were pinned, returns -errno. Each page returned must be released
891 * with a put_page() call when it is finished with. vmas will only
892 * remain valid while mmap_sem is held.
893 *
894 * Must be called with mmap_sem held for read or write.
895 *
896 * get_user_pages walks a process's page tables and takes a reference to
897 * each struct page that each user address corresponds to at a given
898 * instant. That is, it takes the page that would be accessed if a user
899 * thread accesses the given user virtual address at that instant.
900 *
901 * This does not guarantee that the page exists in the user mappings when
902 * get_user_pages returns, and there may even be a completely different
903 * page there in some cases (eg. if mmapped pagecache has been invalidated
904 * and subsequently re faulted). However it does guarantee that the page
905 * won't be freed completely. And mostly callers simply care that the page
906 * contains data that was valid *at some point in time*. Typically, an IO
907 * or similar operation cannot guarantee anything stronger anyway because
908 * locks can't be held over the syscall boundary.
909 *
910 * If write=0, the page must not be written to. If the page is written to,
911 * set_page_dirty (or set_page_dirty_lock, as appropriate) must be called
912 * after the page is finished with, and before put_page is called.
913 *
914 * get_user_pages is typically used for fewer-copy IO operations, to get a
915 * handle on the memory by some means other than accesses via the user virtual
916 * addresses. The pages may be submitted for DMA to devices or accessed via
917 * their kernel linear mapping (via the kmap APIs). Care should be taken to
918 * use the correct cache flushing APIs.
919 *
920 * See also get_user_pages_fast, for performance critical applications.
Andrea Arcangelif0818f42015-02-11 15:27:17 -0800921 *
922 * get_user_pages should be phased out in favor of
923 * get_user_pages_locked|unlocked or get_user_pages_fast. Nothing
924 * should use get_user_pages because it cannot pass
925 * FAULT_FLAG_ALLOW_RETRY to handle_mm_fault.
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700926 */
Dave Hansen1e987792016-02-12 13:01:54 -0800927long get_user_pages_remote(struct task_struct *tsk, struct mm_struct *mm,
928 unsigned long start, unsigned long nr_pages,
929 int write, int force, struct page **pages,
930 struct vm_area_struct **vmas)
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700931{
Andrea Arcangelif0818f42015-02-11 15:27:17 -0800932 return __get_user_pages_locked(tsk, mm, start, nr_pages, write, force,
Dave Hansen1e987792016-02-12 13:01:54 -0800933 pages, vmas, NULL, false,
934 FOLL_TOUCH | FOLL_REMOTE);
935}
936EXPORT_SYMBOL(get_user_pages_remote);
937
938/*
Dave Hansend4edcf02016-02-12 13:01:56 -0800939 * This is the same as get_user_pages_remote(), just with a
940 * less-flexible calling convention where we assume that the task
941 * and mm being operated on are the current task's. We also
942 * obviously don't pass FOLL_REMOTE in here.
Dave Hansen1e987792016-02-12 13:01:54 -0800943 */
Dave Hansencde70142016-02-12 13:01:55 -0800944long get_user_pages6(unsigned long start, unsigned long nr_pages,
Dave Hansen1e987792016-02-12 13:01:54 -0800945 int write, int force, struct page **pages,
946 struct vm_area_struct **vmas)
947{
Dave Hansencde70142016-02-12 13:01:55 -0800948 return __get_user_pages_locked(current, current->mm, start, nr_pages,
Dave Hansen1e987792016-02-12 13:01:54 -0800949 write, force, pages, vmas, NULL, false,
950 FOLL_TOUCH);
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700951}
Dave Hansencde70142016-02-12 13:01:55 -0800952EXPORT_SYMBOL(get_user_pages6);
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700953
954/**
Kirill A. Shutemovacc3c8d2015-04-14 15:44:45 -0700955 * populate_vma_page_range() - populate a range of pages in the vma.
956 * @vma: target vma
957 * @start: start address
958 * @end: end address
959 * @nonblocking:
960 *
961 * This takes care of mlocking the pages too if VM_LOCKED is set.
962 *
963 * return 0 on success, negative error code on error.
964 *
965 * vma->vm_mm->mmap_sem must be held.
966 *
967 * If @nonblocking is NULL, it may be held for read or write and will
968 * be unperturbed.
969 *
970 * If @nonblocking is non-NULL, it must held for read only and may be
971 * released. If it's released, *@nonblocking will be set to 0.
972 */
973long populate_vma_page_range(struct vm_area_struct *vma,
974 unsigned long start, unsigned long end, int *nonblocking)
975{
976 struct mm_struct *mm = vma->vm_mm;
977 unsigned long nr_pages = (end - start) / PAGE_SIZE;
978 int gup_flags;
979
980 VM_BUG_ON(start & ~PAGE_MASK);
981 VM_BUG_ON(end & ~PAGE_MASK);
982 VM_BUG_ON_VMA(start < vma->vm_start, vma);
983 VM_BUG_ON_VMA(end > vma->vm_end, vma);
984 VM_BUG_ON_MM(!rwsem_is_locked(&mm->mmap_sem), mm);
985
Eric B Munsonde60f5f2015-11-05 18:51:36 -0800986 gup_flags = FOLL_TOUCH | FOLL_POPULATE | FOLL_MLOCK;
987 if (vma->vm_flags & VM_LOCKONFAULT)
988 gup_flags &= ~FOLL_POPULATE;
Kirill A. Shutemovacc3c8d2015-04-14 15:44:45 -0700989 /*
990 * We want to touch writable mappings with a write fault in order
991 * to break COW, except for shared mappings because these don't COW
992 * and we would not want to dirty them for nothing.
993 */
994 if ((vma->vm_flags & (VM_WRITE | VM_SHARED)) == VM_WRITE)
995 gup_flags |= FOLL_WRITE;
996
997 /*
998 * We want mlock to succeed for regions that have any permissions
999 * other than PROT_NONE.
1000 */
1001 if (vma->vm_flags & (VM_READ | VM_WRITE | VM_EXEC))
1002 gup_flags |= FOLL_FORCE;
1003
1004 /*
1005 * We made sure addr is within a VMA, so the following will
1006 * not result in a stack expansion that recurses back here.
1007 */
1008 return __get_user_pages(current, mm, start, nr_pages, gup_flags,
1009 NULL, NULL, nonblocking);
1010}
1011
1012/*
1013 * __mm_populate - populate and/or mlock pages within a range of address space.
1014 *
1015 * This is used to implement mlock() and the MAP_POPULATE / MAP_LOCKED mmap
1016 * flags. VMAs must be already marked with the desired vm_flags, and
1017 * mmap_sem must not be held.
1018 */
1019int __mm_populate(unsigned long start, unsigned long len, int ignore_errors)
1020{
1021 struct mm_struct *mm = current->mm;
1022 unsigned long end, nstart, nend;
1023 struct vm_area_struct *vma = NULL;
1024 int locked = 0;
1025 long ret = 0;
1026
1027 VM_BUG_ON(start & ~PAGE_MASK);
1028 VM_BUG_ON(len != PAGE_ALIGN(len));
1029 end = start + len;
1030
1031 for (nstart = start; nstart < end; nstart = nend) {
1032 /*
1033 * We want to fault in pages for [nstart; end) address range.
1034 * Find first corresponding VMA.
1035 */
1036 if (!locked) {
1037 locked = 1;
1038 down_read(&mm->mmap_sem);
1039 vma = find_vma(mm, nstart);
1040 } else if (nstart >= vma->vm_end)
1041 vma = vma->vm_next;
1042 if (!vma || vma->vm_start >= end)
1043 break;
1044 /*
1045 * Set [nstart; nend) to intersection of desired address
1046 * range with the first VMA. Also, skip undesirable VMA types.
1047 */
1048 nend = min(end, vma->vm_end);
1049 if (vma->vm_flags & (VM_IO | VM_PFNMAP))
1050 continue;
1051 if (nstart < vma->vm_start)
1052 nstart = vma->vm_start;
1053 /*
1054 * Now fault in a range of pages. populate_vma_page_range()
1055 * double checks the vma flags, so that it won't mlock pages
1056 * if the vma was already munlocked.
1057 */
1058 ret = populate_vma_page_range(vma, nstart, nend, &locked);
1059 if (ret < 0) {
1060 if (ignore_errors) {
1061 ret = 0;
1062 continue; /* continue at next VMA */
1063 }
1064 break;
1065 }
1066 nend = nstart + ret * PAGE_SIZE;
1067 ret = 0;
1068 }
1069 if (locked)
1070 up_read(&mm->mmap_sem);
1071 return ret; /* 0 or negative error code */
1072}
1073
1074/**
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -07001075 * get_dump_page() - pin user page in memory while writing it to core dump
1076 * @addr: user address
1077 *
1078 * Returns struct page pointer of user page pinned for dump,
1079 * to be freed afterwards by page_cache_release() or put_page().
1080 *
1081 * Returns NULL on any kind of failure - a hole must then be inserted into
1082 * the corefile, to preserve alignment with its headers; and also returns
1083 * NULL wherever the ZERO_PAGE, or an anonymous pte_none, has been found -
1084 * allowing a hole to be left in the corefile to save diskspace.
1085 *
1086 * Called without mmap_sem, but after all other threads have been killed.
1087 */
1088#ifdef CONFIG_ELF_CORE
1089struct page *get_dump_page(unsigned long addr)
1090{
1091 struct vm_area_struct *vma;
1092 struct page *page;
1093
1094 if (__get_user_pages(current, current->mm, addr, 1,
1095 FOLL_FORCE | FOLL_DUMP | FOLL_GET, &page, &vma,
1096 NULL) < 1)
1097 return NULL;
1098 flush_cache_page(vma, addr, page_to_pfn(page));
1099 return page;
1100}
1101#endif /* CONFIG_ELF_CORE */
Steve Capper2667f502014-10-09 15:29:14 -07001102
1103/*
1104 * Generic RCU Fast GUP
1105 *
1106 * get_user_pages_fast attempts to pin user pages by walking the page
1107 * tables directly and avoids taking locks. Thus the walker needs to be
1108 * protected from page table pages being freed from under it, and should
1109 * block any THP splits.
1110 *
1111 * One way to achieve this is to have the walker disable interrupts, and
1112 * rely on IPIs from the TLB flushing code blocking before the page table
1113 * pages are freed. This is unsuitable for architectures that do not need
1114 * to broadcast an IPI when invalidating TLBs.
1115 *
1116 * Another way to achieve this is to batch up page table containing pages
1117 * belonging to more than one mm_user, then rcu_sched a callback to free those
1118 * pages. Disabling interrupts will allow the fast_gup walker to both block
1119 * the rcu_sched callback, and an IPI that we broadcast for splitting THPs
1120 * (which is a relatively rare event). The code below adopts this strategy.
1121 *
1122 * Before activating this code, please be aware that the following assumptions
1123 * are currently made:
1124 *
1125 * *) HAVE_RCU_TABLE_FREE is enabled, and tlb_remove_table is used to free
1126 * pages containing page tables.
1127 *
Steve Capper2667f502014-10-09 15:29:14 -07001128 * *) ptes can be read atomically by the architecture.
1129 *
1130 * *) access_ok is sufficient to validate userspace address ranges.
1131 *
1132 * The last two assumptions can be relaxed by the addition of helper functions.
1133 *
1134 * This code is based heavily on the PowerPC implementation by Nick Piggin.
1135 */
1136#ifdef CONFIG_HAVE_GENERIC_RCU_GUP
1137
1138#ifdef __HAVE_ARCH_PTE_SPECIAL
1139static int gup_pte_range(pmd_t pmd, unsigned long addr, unsigned long end,
1140 int write, struct page **pages, int *nr)
1141{
1142 pte_t *ptep, *ptem;
1143 int ret = 0;
1144
1145 ptem = ptep = pte_offset_map(&pmd, addr);
1146 do {
1147 /*
1148 * In the line below we are assuming that the pte can be read
1149 * atomically. If this is not the case for your architecture,
1150 * please wrap this in a helper function!
1151 *
1152 * for an example see gup_get_pte in arch/x86/mm/gup.c
1153 */
Jason Low9d8c47e2015-04-15 16:14:05 -07001154 pte_t pte = READ_ONCE(*ptep);
Kirill A. Shutemov7aef4172016-01-15 16:52:32 -08001155 struct page *head, *page;
Steve Capper2667f502014-10-09 15:29:14 -07001156
1157 /*
1158 * Similar to the PMD case below, NUMA hinting must take slow
Mel Gorman8a0516e2015-02-12 14:58:22 -08001159 * path using the pte_protnone check.
Steve Capper2667f502014-10-09 15:29:14 -07001160 */
1161 if (!pte_present(pte) || pte_special(pte) ||
Mel Gorman8a0516e2015-02-12 14:58:22 -08001162 pte_protnone(pte) || (write && !pte_write(pte)))
Steve Capper2667f502014-10-09 15:29:14 -07001163 goto pte_unmap;
1164
1165 VM_BUG_ON(!pfn_valid(pte_pfn(pte)));
1166 page = pte_page(pte);
Kirill A. Shutemov7aef4172016-01-15 16:52:32 -08001167 head = compound_head(page);
Steve Capper2667f502014-10-09 15:29:14 -07001168
Kirill A. Shutemov7aef4172016-01-15 16:52:32 -08001169 if (!page_cache_get_speculative(head))
Steve Capper2667f502014-10-09 15:29:14 -07001170 goto pte_unmap;
1171
1172 if (unlikely(pte_val(pte) != pte_val(*ptep))) {
Kirill A. Shutemov7aef4172016-01-15 16:52:32 -08001173 put_page(head);
Steve Capper2667f502014-10-09 15:29:14 -07001174 goto pte_unmap;
1175 }
1176
Kirill A. Shutemov7aef4172016-01-15 16:52:32 -08001177 VM_BUG_ON_PAGE(compound_head(page) != head, page);
Steve Capper2667f502014-10-09 15:29:14 -07001178 pages[*nr] = page;
1179 (*nr)++;
1180
1181 } while (ptep++, addr += PAGE_SIZE, addr != end);
1182
1183 ret = 1;
1184
1185pte_unmap:
1186 pte_unmap(ptem);
1187 return ret;
1188}
1189#else
1190
1191/*
1192 * If we can't determine whether or not a pte is special, then fail immediately
1193 * for ptes. Note, we can still pin HugeTLB and THP as these are guaranteed not
1194 * to be special.
1195 *
1196 * For a futex to be placed on a THP tail page, get_futex_key requires a
1197 * __get_user_pages_fast implementation that can pin pages. Thus it's still
1198 * useful to have gup_huge_pmd even if we can't operate on ptes.
1199 */
1200static int gup_pte_range(pmd_t pmd, unsigned long addr, unsigned long end,
1201 int write, struct page **pages, int *nr)
1202{
1203 return 0;
1204}
1205#endif /* __HAVE_ARCH_PTE_SPECIAL */
1206
1207static int gup_huge_pmd(pmd_t orig, pmd_t *pmdp, unsigned long addr,
1208 unsigned long end, int write, struct page **pages, int *nr)
1209{
Kirill A. Shutemovddc58f22016-01-15 16:52:56 -08001210 struct page *head, *page;
Steve Capper2667f502014-10-09 15:29:14 -07001211 int refs;
1212
1213 if (write && !pmd_write(orig))
1214 return 0;
1215
1216 refs = 0;
1217 head = pmd_page(orig);
1218 page = head + ((addr & ~PMD_MASK) >> PAGE_SHIFT);
Steve Capper2667f502014-10-09 15:29:14 -07001219 do {
1220 VM_BUG_ON_PAGE(compound_head(page) != head, page);
1221 pages[*nr] = page;
1222 (*nr)++;
1223 page++;
1224 refs++;
1225 } while (addr += PAGE_SIZE, addr != end);
1226
1227 if (!page_cache_add_speculative(head, refs)) {
1228 *nr -= refs;
1229 return 0;
1230 }
1231
1232 if (unlikely(pmd_val(orig) != pmd_val(*pmdp))) {
1233 *nr -= refs;
1234 while (refs--)
1235 put_page(head);
1236 return 0;
1237 }
1238
Steve Capper2667f502014-10-09 15:29:14 -07001239 return 1;
1240}
1241
1242static int gup_huge_pud(pud_t orig, pud_t *pudp, unsigned long addr,
1243 unsigned long end, int write, struct page **pages, int *nr)
1244{
Kirill A. Shutemovddc58f22016-01-15 16:52:56 -08001245 struct page *head, *page;
Steve Capper2667f502014-10-09 15:29:14 -07001246 int refs;
1247
1248 if (write && !pud_write(orig))
1249 return 0;
1250
1251 refs = 0;
1252 head = pud_page(orig);
1253 page = head + ((addr & ~PUD_MASK) >> PAGE_SHIFT);
Steve Capper2667f502014-10-09 15:29:14 -07001254 do {
1255 VM_BUG_ON_PAGE(compound_head(page) != head, page);
1256 pages[*nr] = page;
1257 (*nr)++;
1258 page++;
1259 refs++;
1260 } while (addr += PAGE_SIZE, addr != end);
1261
1262 if (!page_cache_add_speculative(head, refs)) {
1263 *nr -= refs;
1264 return 0;
1265 }
1266
1267 if (unlikely(pud_val(orig) != pud_val(*pudp))) {
1268 *nr -= refs;
1269 while (refs--)
1270 put_page(head);
1271 return 0;
1272 }
1273
Steve Capper2667f502014-10-09 15:29:14 -07001274 return 1;
1275}
1276
Aneesh Kumar K.Vf30c59e2014-11-05 21:57:40 +05301277static int gup_huge_pgd(pgd_t orig, pgd_t *pgdp, unsigned long addr,
1278 unsigned long end, int write,
1279 struct page **pages, int *nr)
1280{
1281 int refs;
Kirill A. Shutemovddc58f22016-01-15 16:52:56 -08001282 struct page *head, *page;
Aneesh Kumar K.Vf30c59e2014-11-05 21:57:40 +05301283
1284 if (write && !pgd_write(orig))
1285 return 0;
1286
1287 refs = 0;
1288 head = pgd_page(orig);
1289 page = head + ((addr & ~PGDIR_MASK) >> PAGE_SHIFT);
Aneesh Kumar K.Vf30c59e2014-11-05 21:57:40 +05301290 do {
1291 VM_BUG_ON_PAGE(compound_head(page) != head, page);
1292 pages[*nr] = page;
1293 (*nr)++;
1294 page++;
1295 refs++;
1296 } while (addr += PAGE_SIZE, addr != end);
1297
1298 if (!page_cache_add_speculative(head, refs)) {
1299 *nr -= refs;
1300 return 0;
1301 }
1302
1303 if (unlikely(pgd_val(orig) != pgd_val(*pgdp))) {
1304 *nr -= refs;
1305 while (refs--)
1306 put_page(head);
1307 return 0;
1308 }
1309
Aneesh Kumar K.Vf30c59e2014-11-05 21:57:40 +05301310 return 1;
1311}
1312
Steve Capper2667f502014-10-09 15:29:14 -07001313static int gup_pmd_range(pud_t pud, unsigned long addr, unsigned long end,
1314 int write, struct page **pages, int *nr)
1315{
1316 unsigned long next;
1317 pmd_t *pmdp;
1318
1319 pmdp = pmd_offset(&pud, addr);
1320 do {
Christian Borntraeger38c5ce92015-01-06 22:54:46 +01001321 pmd_t pmd = READ_ONCE(*pmdp);
Steve Capper2667f502014-10-09 15:29:14 -07001322
1323 next = pmd_addr_end(addr, end);
Kirill A. Shutemov4b471e82016-01-15 16:53:39 -08001324 if (pmd_none(pmd))
Steve Capper2667f502014-10-09 15:29:14 -07001325 return 0;
1326
1327 if (unlikely(pmd_trans_huge(pmd) || pmd_huge(pmd))) {
1328 /*
1329 * NUMA hinting faults need to be handled in the GUP
1330 * slowpath for accounting purposes and so that they
1331 * can be serialised against THP migration.
1332 */
Mel Gorman8a0516e2015-02-12 14:58:22 -08001333 if (pmd_protnone(pmd))
Steve Capper2667f502014-10-09 15:29:14 -07001334 return 0;
1335
1336 if (!gup_huge_pmd(pmd, pmdp, addr, next, write,
1337 pages, nr))
1338 return 0;
1339
Aneesh Kumar K.Vf30c59e2014-11-05 21:57:40 +05301340 } else if (unlikely(is_hugepd(__hugepd(pmd_val(pmd))))) {
1341 /*
1342 * architecture have different format for hugetlbfs
1343 * pmd format and THP pmd format
1344 */
1345 if (!gup_huge_pd(__hugepd(pmd_val(pmd)), addr,
1346 PMD_SHIFT, next, write, pages, nr))
1347 return 0;
Steve Capper2667f502014-10-09 15:29:14 -07001348 } else if (!gup_pte_range(pmd, addr, next, write, pages, nr))
1349 return 0;
1350 } while (pmdp++, addr = next, addr != end);
1351
1352 return 1;
1353}
1354
Aneesh Kumar K.Vf30c59e2014-11-05 21:57:40 +05301355static int gup_pud_range(pgd_t pgd, unsigned long addr, unsigned long end,
1356 int write, struct page **pages, int *nr)
Steve Capper2667f502014-10-09 15:29:14 -07001357{
1358 unsigned long next;
1359 pud_t *pudp;
1360
Aneesh Kumar K.Vf30c59e2014-11-05 21:57:40 +05301361 pudp = pud_offset(&pgd, addr);
Steve Capper2667f502014-10-09 15:29:14 -07001362 do {
Christian Borntraegere37c6982014-12-07 21:41:33 +01001363 pud_t pud = READ_ONCE(*pudp);
Steve Capper2667f502014-10-09 15:29:14 -07001364
1365 next = pud_addr_end(addr, end);
1366 if (pud_none(pud))
1367 return 0;
Aneesh Kumar K.Vf30c59e2014-11-05 21:57:40 +05301368 if (unlikely(pud_huge(pud))) {
Steve Capper2667f502014-10-09 15:29:14 -07001369 if (!gup_huge_pud(pud, pudp, addr, next, write,
Aneesh Kumar K.Vf30c59e2014-11-05 21:57:40 +05301370 pages, nr))
1371 return 0;
1372 } else if (unlikely(is_hugepd(__hugepd(pud_val(pud))))) {
1373 if (!gup_huge_pd(__hugepd(pud_val(pud)), addr,
1374 PUD_SHIFT, next, write, pages, nr))
Steve Capper2667f502014-10-09 15:29:14 -07001375 return 0;
1376 } else if (!gup_pmd_range(pud, addr, next, write, pages, nr))
1377 return 0;
1378 } while (pudp++, addr = next, addr != end);
1379
1380 return 1;
1381}
1382
1383/*
1384 * Like get_user_pages_fast() except it's IRQ-safe in that it won't fall back to
1385 * the regular GUP. It will only return non-negative values.
1386 */
1387int __get_user_pages_fast(unsigned long start, int nr_pages, int write,
1388 struct page **pages)
1389{
1390 struct mm_struct *mm = current->mm;
1391 unsigned long addr, len, end;
1392 unsigned long next, flags;
1393 pgd_t *pgdp;
1394 int nr = 0;
1395
1396 start &= PAGE_MASK;
1397 addr = start;
1398 len = (unsigned long) nr_pages << PAGE_SHIFT;
1399 end = start + len;
1400
1401 if (unlikely(!access_ok(write ? VERIFY_WRITE : VERIFY_READ,
1402 start, len)))
1403 return 0;
1404
1405 /*
1406 * Disable interrupts. We use the nested form as we can already have
1407 * interrupts disabled by get_futex_key.
1408 *
1409 * With interrupts disabled, we block page table pages from being
1410 * freed from under us. See mmu_gather_tlb in asm-generic/tlb.h
1411 * for more details.
1412 *
1413 * We do not adopt an rcu_read_lock(.) here as we also want to
1414 * block IPIs that come from THPs splitting.
1415 */
1416
1417 local_irq_save(flags);
1418 pgdp = pgd_offset(mm, addr);
1419 do {
Jason Low9d8c47e2015-04-15 16:14:05 -07001420 pgd_t pgd = READ_ONCE(*pgdp);
Aneesh Kumar K.Vf30c59e2014-11-05 21:57:40 +05301421
Steve Capper2667f502014-10-09 15:29:14 -07001422 next = pgd_addr_end(addr, end);
Aneesh Kumar K.Vf30c59e2014-11-05 21:57:40 +05301423 if (pgd_none(pgd))
Steve Capper2667f502014-10-09 15:29:14 -07001424 break;
Aneesh Kumar K.Vf30c59e2014-11-05 21:57:40 +05301425 if (unlikely(pgd_huge(pgd))) {
1426 if (!gup_huge_pgd(pgd, pgdp, addr, next, write,
1427 pages, &nr))
1428 break;
1429 } else if (unlikely(is_hugepd(__hugepd(pgd_val(pgd))))) {
1430 if (!gup_huge_pd(__hugepd(pgd_val(pgd)), addr,
1431 PGDIR_SHIFT, next, write, pages, &nr))
1432 break;
1433 } else if (!gup_pud_range(pgd, addr, next, write, pages, &nr))
Steve Capper2667f502014-10-09 15:29:14 -07001434 break;
1435 } while (pgdp++, addr = next, addr != end);
1436 local_irq_restore(flags);
1437
1438 return nr;
1439}
1440
1441/**
1442 * get_user_pages_fast() - pin user pages in memory
1443 * @start: starting user address
1444 * @nr_pages: number of pages from start to pin
1445 * @write: whether pages will be written to
1446 * @pages: array that receives pointers to the pages pinned.
1447 * Should be at least nr_pages long.
1448 *
1449 * Attempt to pin user pages in memory without taking mm->mmap_sem.
1450 * If not successful, it will fall back to taking the lock and
1451 * calling get_user_pages().
1452 *
1453 * Returns number of pages pinned. This may be fewer than the number
1454 * requested. If nr_pages is 0 or negative, returns 0. If no pages
1455 * were pinned, returns -errno.
1456 */
1457int get_user_pages_fast(unsigned long start, int nr_pages, int write,
1458 struct page **pages)
1459{
1460 struct mm_struct *mm = current->mm;
1461 int nr, ret;
1462
1463 start &= PAGE_MASK;
1464 nr = __get_user_pages_fast(start, nr_pages, write, pages);
1465 ret = nr;
1466
1467 if (nr < nr_pages) {
1468 /* Try to get the remaining pages with get_user_pages */
1469 start += nr << PAGE_SHIFT;
1470 pages += nr;
1471
Andrea Arcangelia7b78072015-02-11 15:27:23 -08001472 ret = get_user_pages_unlocked(current, mm, start,
1473 nr_pages - nr, write, 0, pages);
Steve Capper2667f502014-10-09 15:29:14 -07001474
1475 /* Have to be a bit careful with return values */
1476 if (nr > 0) {
1477 if (ret < 0)
1478 ret = nr;
1479 else
1480 ret += nr;
1481 }
1482 }
1483
1484 return ret;
1485}
1486
1487#endif /* CONFIG_HAVE_GENERIC_RCU_GUP */
Dave Hansencde70142016-02-12 13:01:55 -08001488
1489long get_user_pages8(struct task_struct *tsk, struct mm_struct *mm,
1490 unsigned long start, unsigned long nr_pages,
1491 int write, int force, struct page **pages,
1492 struct vm_area_struct **vmas)
1493{
1494 WARN_ONCE(tsk != current, "get_user_pages() called on remote task");
1495 WARN_ONCE(mm != current->mm, "get_user_pages() called on remote mm");
1496
1497 return get_user_pages6(start, nr_pages, write, force, pages, vmas);
1498}
1499EXPORT_SYMBOL(get_user_pages8);
1500
1501long get_user_pages_locked8(struct task_struct *tsk, struct mm_struct *mm,
1502 unsigned long start, unsigned long nr_pages,
1503 int write, int force, struct page **pages, int *locked)
1504{
1505 WARN_ONCE(tsk != current, "get_user_pages_locked() called on remote task");
1506 WARN_ONCE(mm != current->mm, "get_user_pages_locked() called on remote mm");
1507
1508 return get_user_pages_locked6(start, nr_pages, write, force, pages, locked);
1509}
1510EXPORT_SYMBOL(get_user_pages_locked8);
1511
1512long get_user_pages_unlocked7(struct task_struct *tsk, struct mm_struct *mm,
1513 unsigned long start, unsigned long nr_pages,
1514 int write, int force, struct page **pages)
1515{
1516 WARN_ONCE(tsk != current, "get_user_pages_unlocked() called on remote task");
1517 WARN_ONCE(mm != current->mm, "get_user_pages_unlocked() called on remote mm");
1518
1519 return get_user_pages_unlocked5(start, nr_pages, write, force, pages);
1520}
1521EXPORT_SYMBOL(get_user_pages_unlocked7);
1522