blob: e95b0cb6ed8173e80ecf3ae7d8798b763b46f965 [file] [log] [blame]
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -07001#include <linux/kernel.h>
2#include <linux/errno.h>
3#include <linux/err.h>
4#include <linux/spinlock.h>
5
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -07006#include <linux/mm.h>
7#include <linux/pagemap.h>
8#include <linux/rmap.h>
9#include <linux/swap.h>
10#include <linux/swapops.h>
11
Steve Capper2667f502014-10-09 15:29:14 -070012#include <linux/sched.h>
13#include <linux/rwsem.h>
Aneesh Kumar K.Vf30c59e2014-11-05 21:57:40 +053014#include <linux/hugetlb.h>
Kirill A. Shutemov1027e442015-09-04 15:47:55 -070015
Steve Capper2667f502014-10-09 15:29:14 -070016#include <asm/pgtable.h>
Kirill A. Shutemov1027e442015-09-04 15:47:55 -070017#include <asm/tlbflush.h>
Steve Capper2667f502014-10-09 15:29:14 -070018
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -070019#include "internal.h"
20
Kirill A. Shutemov69e68b42014-06-04 16:08:11 -070021static struct page *no_page_table(struct vm_area_struct *vma,
22 unsigned int flags)
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -070023{
Kirill A. Shutemov69e68b42014-06-04 16:08:11 -070024 /*
25 * When core dumping an enormous anonymous area that nobody
26 * has touched so far, we don't want to allocate unnecessary pages or
27 * page tables. Return error instead of NULL to skip handle_mm_fault,
28 * then get_dump_page() will return NULL to leave a hole in the dump.
29 * But we can only make this optimization where a hole would surely
30 * be zero-filled if handle_mm_fault() actually did handle it.
31 */
32 if ((flags & FOLL_DUMP) && (!vma->vm_ops || !vma->vm_ops->fault))
33 return ERR_PTR(-EFAULT);
34 return NULL;
35}
36
Kirill A. Shutemov1027e442015-09-04 15:47:55 -070037static int follow_pfn_pte(struct vm_area_struct *vma, unsigned long address,
38 pte_t *pte, unsigned int flags)
39{
40 /* No page to get reference */
41 if (flags & FOLL_GET)
42 return -EFAULT;
43
44 if (flags & FOLL_TOUCH) {
45 pte_t entry = *pte;
46
47 if (flags & FOLL_WRITE)
48 entry = pte_mkdirty(entry);
49 entry = pte_mkyoung(entry);
50
51 if (!pte_same(*pte, entry)) {
52 set_pte_at(vma->vm_mm, address, pte, entry);
53 update_mmu_cache(vma, address, pte);
54 }
55 }
56
57 /* Proper page table entry exists, but no corresponding struct page */
58 return -EEXIST;
59}
60
Kirill A. Shutemov69e68b42014-06-04 16:08:11 -070061static struct page *follow_page_pte(struct vm_area_struct *vma,
62 unsigned long address, pmd_t *pmd, unsigned int flags)
63{
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -070064 struct mm_struct *mm = vma->vm_mm;
Kirill A. Shutemov69e68b42014-06-04 16:08:11 -070065 struct page *page;
66 spinlock_t *ptl;
67 pte_t *ptep, pte;
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -070068
Kirill A. Shutemov69e68b42014-06-04 16:08:11 -070069retry:
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -070070 if (unlikely(pmd_bad(*pmd)))
Kirill A. Shutemov69e68b42014-06-04 16:08:11 -070071 return no_page_table(vma, flags);
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -070072
73 ptep = pte_offset_map_lock(mm, pmd, address, &ptl);
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -070074 pte = *ptep;
75 if (!pte_present(pte)) {
76 swp_entry_t entry;
77 /*
78 * KSM's break_ksm() relies upon recognizing a ksm page
79 * even while it is being migrated, so for that case we
80 * need migration_entry_wait().
81 */
82 if (likely(!(flags & FOLL_MIGRATION)))
83 goto no_page;
Kirill A. Shutemov0661a332015-02-10 14:10:04 -080084 if (pte_none(pte))
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -070085 goto no_page;
86 entry = pte_to_swp_entry(pte);
87 if (!is_migration_entry(entry))
88 goto no_page;
89 pte_unmap_unlock(ptep, ptl);
90 migration_entry_wait(mm, pmd, address);
Kirill A. Shutemov69e68b42014-06-04 16:08:11 -070091 goto retry;
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -070092 }
Mel Gorman8a0516e2015-02-12 14:58:22 -080093 if ((flags & FOLL_NUMA) && pte_protnone(pte))
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -070094 goto no_page;
Kirill A. Shutemov69e68b42014-06-04 16:08:11 -070095 if ((flags & FOLL_WRITE) && !pte_write(pte)) {
96 pte_unmap_unlock(ptep, ptl);
97 return NULL;
98 }
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -070099
100 page = vm_normal_page(vma, address, pte);
101 if (unlikely(!page)) {
Kirill A. Shutemov1027e442015-09-04 15:47:55 -0700102 if (flags & FOLL_DUMP) {
103 /* Avoid special (like zero) pages in core dumps */
104 page = ERR_PTR(-EFAULT);
105 goto out;
106 }
107
108 if (is_zero_pfn(pte_pfn(pte))) {
109 page = pte_page(pte);
110 } else {
111 int ret;
112
113 ret = follow_pfn_pte(vma, address, ptep, flags);
114 page = ERR_PTR(ret);
115 goto out;
116 }
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700117 }
118
Kirill A. Shutemov6742d292016-01-15 16:52:28 -0800119 if (flags & FOLL_SPLIT && PageTransCompound(page)) {
120 int ret;
121 get_page(page);
122 pte_unmap_unlock(ptep, ptl);
123 lock_page(page);
124 ret = split_huge_page(page);
125 unlock_page(page);
126 put_page(page);
127 if (ret)
128 return ERR_PTR(ret);
129 goto retry;
130 }
131
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700132 if (flags & FOLL_GET)
Kirill A. Shutemovddc58f22016-01-15 16:52:56 -0800133 get_page(page);
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700134 if (flags & FOLL_TOUCH) {
135 if ((flags & FOLL_WRITE) &&
136 !pte_dirty(pte) && !PageDirty(page))
137 set_page_dirty(page);
138 /*
139 * pte_mkyoung() would be more correct here, but atomic care
140 * is needed to avoid losing the dirty bit: it is easier to use
141 * mark_page_accessed().
142 */
143 mark_page_accessed(page);
144 }
Eric B Munsonde60f5f2015-11-05 18:51:36 -0800145 if ((flags & FOLL_MLOCK) && (vma->vm_flags & VM_LOCKED)) {
Kirill A. Shutemove90309c2016-01-15 16:54:33 -0800146 /* Do not mlock pte-mapped THP */
147 if (PageTransCompound(page))
148 goto out;
149
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700150 /*
151 * The preliminary mapping check is mainly to avoid the
152 * pointless overhead of lock_page on the ZERO_PAGE
153 * which might bounce very badly if there is contention.
154 *
155 * If the page is already locked, we don't need to
156 * handle it now - vmscan will handle it later if and
157 * when it attempts to reclaim the page.
158 */
159 if (page->mapping && trylock_page(page)) {
160 lru_add_drain(); /* push cached pages to LRU */
161 /*
162 * Because we lock page here, and migration is
163 * blocked by the pte's page reference, and we
164 * know the page is still mapped, we don't even
165 * need to check for file-cache page truncation.
166 */
167 mlock_vma_page(page);
168 unlock_page(page);
169 }
170 }
Kirill A. Shutemov1027e442015-09-04 15:47:55 -0700171out:
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700172 pte_unmap_unlock(ptep, ptl);
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700173 return page;
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700174no_page:
175 pte_unmap_unlock(ptep, ptl);
176 if (!pte_none(pte))
Kirill A. Shutemov69e68b42014-06-04 16:08:11 -0700177 return NULL;
178 return no_page_table(vma, flags);
179}
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700180
Kirill A. Shutemov69e68b42014-06-04 16:08:11 -0700181/**
182 * follow_page_mask - look up a page descriptor from a user-virtual address
183 * @vma: vm_area_struct mapping @address
184 * @address: virtual address to look up
185 * @flags: flags modifying lookup behaviour
186 * @page_mask: on output, *page_mask is set according to the size of the page
187 *
188 * @flags can have FOLL_ flags set, defined in <linux/mm.h>
189 *
190 * Returns the mapped (struct page *), %NULL if no mapping exists, or
191 * an error pointer if there is a mapping to something not represented
192 * by a page descriptor (see also vm_normal_page()).
193 */
194struct page *follow_page_mask(struct vm_area_struct *vma,
195 unsigned long address, unsigned int flags,
196 unsigned int *page_mask)
197{
198 pgd_t *pgd;
199 pud_t *pud;
200 pmd_t *pmd;
201 spinlock_t *ptl;
202 struct page *page;
203 struct mm_struct *mm = vma->vm_mm;
204
205 *page_mask = 0;
206
207 page = follow_huge_addr(mm, address, flags & FOLL_WRITE);
208 if (!IS_ERR(page)) {
209 BUG_ON(flags & FOLL_GET);
210 return page;
211 }
212
213 pgd = pgd_offset(mm, address);
214 if (pgd_none(*pgd) || unlikely(pgd_bad(*pgd)))
215 return no_page_table(vma, flags);
216
217 pud = pud_offset(pgd, address);
218 if (pud_none(*pud))
219 return no_page_table(vma, flags);
220 if (pud_huge(*pud) && vma->vm_flags & VM_HUGETLB) {
Naoya Horiguchie66f17f2015-02-11 15:25:22 -0800221 page = follow_huge_pud(mm, address, pud, flags);
222 if (page)
223 return page;
224 return no_page_table(vma, flags);
Kirill A. Shutemov69e68b42014-06-04 16:08:11 -0700225 }
226 if (unlikely(pud_bad(*pud)))
227 return no_page_table(vma, flags);
228
229 pmd = pmd_offset(pud, address);
230 if (pmd_none(*pmd))
231 return no_page_table(vma, flags);
232 if (pmd_huge(*pmd) && vma->vm_flags & VM_HUGETLB) {
Naoya Horiguchie66f17f2015-02-11 15:25:22 -0800233 page = follow_huge_pmd(mm, address, pmd, flags);
234 if (page)
235 return page;
236 return no_page_table(vma, flags);
Kirill A. Shutemov69e68b42014-06-04 16:08:11 -0700237 }
Mel Gorman8a0516e2015-02-12 14:58:22 -0800238 if ((flags & FOLL_NUMA) && pmd_protnone(*pmd))
Kirill A. Shutemov69e68b42014-06-04 16:08:11 -0700239 return no_page_table(vma, flags);
Kirill A. Shutemov6742d292016-01-15 16:52:28 -0800240 if (likely(!pmd_trans_huge(*pmd)))
241 return follow_page_pte(vma, address, pmd, flags);
242
243 ptl = pmd_lock(mm, pmd);
244 if (unlikely(!pmd_trans_huge(*pmd))) {
245 spin_unlock(ptl);
246 return follow_page_pte(vma, address, pmd, flags);
Kirill A. Shutemov69e68b42014-06-04 16:08:11 -0700247 }
Kirill A. Shutemov6742d292016-01-15 16:52:28 -0800248 if (flags & FOLL_SPLIT) {
249 int ret;
250 page = pmd_page(*pmd);
251 if (is_huge_zero_page(page)) {
252 spin_unlock(ptl);
253 ret = 0;
Kirill A. Shutemov78ddc532016-01-15 16:52:42 -0800254 split_huge_pmd(vma, pmd, address);
Kirill A. Shutemov6742d292016-01-15 16:52:28 -0800255 } else {
256 get_page(page);
257 spin_unlock(ptl);
258 lock_page(page);
259 ret = split_huge_page(page);
260 unlock_page(page);
261 put_page(page);
262 }
263
264 return ret ? ERR_PTR(ret) :
265 follow_page_pte(vma, address, pmd, flags);
266 }
267
268 page = follow_trans_huge_pmd(vma, address, pmd, flags);
269 spin_unlock(ptl);
270 *page_mask = HPAGE_PMD_NR - 1;
271 return page;
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700272}
273
Kirill A. Shutemovf2b495c2014-06-04 16:08:11 -0700274static int get_gate_page(struct mm_struct *mm, unsigned long address,
275 unsigned int gup_flags, struct vm_area_struct **vma,
276 struct page **page)
277{
278 pgd_t *pgd;
279 pud_t *pud;
280 pmd_t *pmd;
281 pte_t *pte;
282 int ret = -EFAULT;
283
284 /* user gate pages are read-only */
285 if (gup_flags & FOLL_WRITE)
286 return -EFAULT;
287 if (address > TASK_SIZE)
288 pgd = pgd_offset_k(address);
289 else
290 pgd = pgd_offset_gate(mm, address);
291 BUG_ON(pgd_none(*pgd));
292 pud = pud_offset(pgd, address);
293 BUG_ON(pud_none(*pud));
294 pmd = pmd_offset(pud, address);
295 if (pmd_none(*pmd))
296 return -EFAULT;
297 VM_BUG_ON(pmd_trans_huge(*pmd));
298 pte = pte_offset_map(pmd, address);
299 if (pte_none(*pte))
300 goto unmap;
301 *vma = get_gate_vma(mm);
302 if (!page)
303 goto out;
304 *page = vm_normal_page(*vma, address, *pte);
305 if (!*page) {
306 if ((gup_flags & FOLL_DUMP) || !is_zero_pfn(pte_pfn(*pte)))
307 goto unmap;
308 *page = pte_page(*pte);
309 }
310 get_page(*page);
311out:
312 ret = 0;
313unmap:
314 pte_unmap(pte);
315 return ret;
316}
317
Paul Cassella9a95f3c2014-08-06 16:07:24 -0700318/*
319 * mmap_sem must be held on entry. If @nonblocking != NULL and
320 * *@flags does not include FOLL_NOWAIT, the mmap_sem may be released.
321 * If it is, *@nonblocking will be set to 0 and -EBUSY returned.
322 */
Kirill A. Shutemov16744482014-06-04 16:08:12 -0700323static int faultin_page(struct task_struct *tsk, struct vm_area_struct *vma,
324 unsigned long address, unsigned int *flags, int *nonblocking)
325{
326 struct mm_struct *mm = vma->vm_mm;
327 unsigned int fault_flags = 0;
328 int ret;
329
Eric B Munsonde60f5f2015-11-05 18:51:36 -0800330 /* mlock all present pages, but do not fault in new pages */
331 if ((*flags & (FOLL_POPULATE | FOLL_MLOCK)) == FOLL_MLOCK)
332 return -ENOENT;
Kirill A. Shutemov84d33df2015-04-14 15:44:37 -0700333 /* For mm_populate(), just skip the stack guard page. */
334 if ((*flags & FOLL_POPULATE) &&
Kirill A. Shutemov16744482014-06-04 16:08:12 -0700335 (stack_guard_page_start(vma, address) ||
336 stack_guard_page_end(vma, address + PAGE_SIZE)))
337 return -ENOENT;
338 if (*flags & FOLL_WRITE)
339 fault_flags |= FAULT_FLAG_WRITE;
340 if (nonblocking)
341 fault_flags |= FAULT_FLAG_ALLOW_RETRY;
342 if (*flags & FOLL_NOWAIT)
343 fault_flags |= FAULT_FLAG_ALLOW_RETRY | FAULT_FLAG_RETRY_NOWAIT;
Andres Lagar-Cavilla234b2392014-09-17 10:51:48 -0700344 if (*flags & FOLL_TRIED) {
345 VM_WARN_ON_ONCE(fault_flags & FAULT_FLAG_ALLOW_RETRY);
346 fault_flags |= FAULT_FLAG_TRIED;
347 }
Kirill A. Shutemov16744482014-06-04 16:08:12 -0700348
349 ret = handle_mm_fault(mm, vma, address, fault_flags);
350 if (ret & VM_FAULT_ERROR) {
351 if (ret & VM_FAULT_OOM)
352 return -ENOMEM;
353 if (ret & (VM_FAULT_HWPOISON | VM_FAULT_HWPOISON_LARGE))
354 return *flags & FOLL_HWPOISON ? -EHWPOISON : -EFAULT;
Linus Torvalds33692f22015-01-29 10:51:32 -0800355 if (ret & (VM_FAULT_SIGBUS | VM_FAULT_SIGSEGV))
Kirill A. Shutemov16744482014-06-04 16:08:12 -0700356 return -EFAULT;
357 BUG();
358 }
359
360 if (tsk) {
361 if (ret & VM_FAULT_MAJOR)
362 tsk->maj_flt++;
363 else
364 tsk->min_flt++;
365 }
366
367 if (ret & VM_FAULT_RETRY) {
368 if (nonblocking)
369 *nonblocking = 0;
370 return -EBUSY;
371 }
372
373 /*
374 * The VM_FAULT_WRITE bit tells us that do_wp_page has broken COW when
375 * necessary, even if maybe_mkwrite decided not to set pte_write. We
376 * can thus safely do subsequent page lookups as if they were reads.
377 * But only do so when looping for pte_write is futile: in some cases
378 * userspace may also be wanting to write to the gotten user page,
379 * which a read fault here might prevent (a readonly page might get
380 * reCOWed by userspace write).
381 */
382 if ((ret & VM_FAULT_WRITE) && !(vma->vm_flags & VM_WRITE))
383 *flags &= ~FOLL_WRITE;
384 return 0;
385}
386
Kirill A. Shutemovfa5bb202014-06-04 16:08:13 -0700387static int check_vma_flags(struct vm_area_struct *vma, unsigned long gup_flags)
388{
389 vm_flags_t vm_flags = vma->vm_flags;
390
391 if (vm_flags & (VM_IO | VM_PFNMAP))
392 return -EFAULT;
393
394 if (gup_flags & FOLL_WRITE) {
395 if (!(vm_flags & VM_WRITE)) {
396 if (!(gup_flags & FOLL_FORCE))
397 return -EFAULT;
398 /*
399 * We used to let the write,force case do COW in a
400 * VM_MAYWRITE VM_SHARED !VM_WRITE vma, so ptrace could
401 * set a breakpoint in a read-only mapping of an
402 * executable, without corrupting the file (yet only
403 * when that file had been opened for writing!).
404 * Anon pages in shared mappings are surprising: now
405 * just reject it.
406 */
407 if (!is_cow_mapping(vm_flags)) {
408 WARN_ON_ONCE(vm_flags & VM_MAYWRITE);
409 return -EFAULT;
410 }
411 }
412 } else if (!(vm_flags & VM_READ)) {
413 if (!(gup_flags & FOLL_FORCE))
414 return -EFAULT;
415 /*
416 * Is there actually any vma we can reach here which does not
417 * have VM_MAYREAD set?
418 */
419 if (!(vm_flags & VM_MAYREAD))
420 return -EFAULT;
421 }
422 return 0;
423}
424
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700425/**
426 * __get_user_pages() - pin user pages in memory
427 * @tsk: task_struct of target task
428 * @mm: mm_struct of target mm
429 * @start: starting user address
430 * @nr_pages: number of pages from start to pin
431 * @gup_flags: flags modifying pin behaviour
432 * @pages: array that receives pointers to the pages pinned.
433 * Should be at least nr_pages long. Or NULL, if caller
434 * only intends to ensure the pages are faulted in.
435 * @vmas: array of pointers to vmas corresponding to each page.
436 * Or NULL if the caller does not require them.
437 * @nonblocking: whether waiting for disk IO or mmap_sem contention
438 *
439 * Returns number of pages pinned. This may be fewer than the number
440 * requested. If nr_pages is 0 or negative, returns 0. If no pages
441 * were pinned, returns -errno. Each page returned must be released
442 * with a put_page() call when it is finished with. vmas will only
443 * remain valid while mmap_sem is held.
444 *
Paul Cassella9a95f3c2014-08-06 16:07:24 -0700445 * Must be called with mmap_sem held. It may be released. See below.
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700446 *
447 * __get_user_pages walks a process's page tables and takes a reference to
448 * each struct page that each user address corresponds to at a given
449 * instant. That is, it takes the page that would be accessed if a user
450 * thread accesses the given user virtual address at that instant.
451 *
452 * This does not guarantee that the page exists in the user mappings when
453 * __get_user_pages returns, and there may even be a completely different
454 * page there in some cases (eg. if mmapped pagecache has been invalidated
455 * and subsequently re faulted). However it does guarantee that the page
456 * won't be freed completely. And mostly callers simply care that the page
457 * contains data that was valid *at some point in time*. Typically, an IO
458 * or similar operation cannot guarantee anything stronger anyway because
459 * locks can't be held over the syscall boundary.
460 *
461 * If @gup_flags & FOLL_WRITE == 0, the page must not be written to. If
462 * the page is written to, set_page_dirty (or set_page_dirty_lock, as
463 * appropriate) must be called after the page is finished with, and
464 * before put_page is called.
465 *
466 * If @nonblocking != NULL, __get_user_pages will not wait for disk IO
467 * or mmap_sem contention, and if waiting is needed to pin all pages,
Paul Cassella9a95f3c2014-08-06 16:07:24 -0700468 * *@nonblocking will be set to 0. Further, if @gup_flags does not
469 * include FOLL_NOWAIT, the mmap_sem will be released via up_read() in
470 * this case.
471 *
472 * A caller using such a combination of @nonblocking and @gup_flags
473 * must therefore hold the mmap_sem for reading only, and recognize
474 * when it's been released. Otherwise, it must be held for either
475 * reading or writing and will not be released.
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700476 *
477 * In most cases, get_user_pages or get_user_pages_fast should be used
478 * instead of __get_user_pages. __get_user_pages should be used only if
479 * you need some special @gup_flags.
480 */
481long __get_user_pages(struct task_struct *tsk, struct mm_struct *mm,
482 unsigned long start, unsigned long nr_pages,
483 unsigned int gup_flags, struct page **pages,
484 struct vm_area_struct **vmas, int *nonblocking)
485{
Kirill A. Shutemovfa5bb202014-06-04 16:08:13 -0700486 long i = 0;
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700487 unsigned int page_mask;
Kirill A. Shutemovfa5bb202014-06-04 16:08:13 -0700488 struct vm_area_struct *vma = NULL;
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700489
490 if (!nr_pages)
491 return 0;
492
493 VM_BUG_ON(!!pages != !!(gup_flags & FOLL_GET));
494
495 /*
496 * If FOLL_FORCE is set then do not force a full fault as the hinting
497 * fault information is unrelated to the reference behaviour of a task
498 * using the address space
499 */
500 if (!(gup_flags & FOLL_FORCE))
501 gup_flags |= FOLL_NUMA;
502
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700503 do {
Kirill A. Shutemovfa5bb202014-06-04 16:08:13 -0700504 struct page *page;
505 unsigned int foll_flags = gup_flags;
506 unsigned int page_increm;
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700507
Kirill A. Shutemovfa5bb202014-06-04 16:08:13 -0700508 /* first iteration or cross vma bound */
509 if (!vma || start >= vma->vm_end) {
510 vma = find_extend_vma(mm, start);
511 if (!vma && in_gate_area(mm, start)) {
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700512 int ret;
Kirill A. Shutemovfa5bb202014-06-04 16:08:13 -0700513 ret = get_gate_page(mm, start & PAGE_MASK,
514 gup_flags, &vma,
515 pages ? &pages[i] : NULL);
516 if (ret)
517 return i ? : ret;
518 page_mask = 0;
519 goto next_page;
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700520 }
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700521
Kirill A. Shutemovfa5bb202014-06-04 16:08:13 -0700522 if (!vma || check_vma_flags(vma, gup_flags))
523 return i ? : -EFAULT;
524 if (is_vm_hugetlb_page(vma)) {
525 i = follow_hugetlb_page(mm, vma, pages, vmas,
526 &start, &nr_pages, i,
527 gup_flags);
528 continue;
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700529 }
Kirill A. Shutemovfa5bb202014-06-04 16:08:13 -0700530 }
531retry:
532 /*
533 * If we have a pending SIGKILL, don't keep faulting pages and
534 * potentially allocating memory.
535 */
536 if (unlikely(fatal_signal_pending(current)))
537 return i ? i : -ERESTARTSYS;
538 cond_resched();
539 page = follow_page_mask(vma, start, foll_flags, &page_mask);
540 if (!page) {
541 int ret;
542 ret = faultin_page(tsk, vma, start, &foll_flags,
543 nonblocking);
544 switch (ret) {
545 case 0:
546 goto retry;
547 case -EFAULT:
548 case -ENOMEM:
549 case -EHWPOISON:
550 return i ? i : ret;
551 case -EBUSY:
552 return i;
553 case -ENOENT:
554 goto next_page;
555 }
556 BUG();
Kirill A. Shutemov1027e442015-09-04 15:47:55 -0700557 } else if (PTR_ERR(page) == -EEXIST) {
558 /*
559 * Proper page table entry exists, but no corresponding
560 * struct page.
561 */
562 goto next_page;
563 } else if (IS_ERR(page)) {
Kirill A. Shutemovfa5bb202014-06-04 16:08:13 -0700564 return i ? i : PTR_ERR(page);
Kirill A. Shutemov1027e442015-09-04 15:47:55 -0700565 }
Kirill A. Shutemovfa5bb202014-06-04 16:08:13 -0700566 if (pages) {
567 pages[i] = page;
568 flush_anon_page(vma, page, start);
569 flush_dcache_page(page);
570 page_mask = 0;
571 }
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700572next_page:
Kirill A. Shutemovfa5bb202014-06-04 16:08:13 -0700573 if (vmas) {
574 vmas[i] = vma;
575 page_mask = 0;
576 }
577 page_increm = 1 + (~(start >> PAGE_SHIFT) & page_mask);
578 if (page_increm > nr_pages)
579 page_increm = nr_pages;
580 i += page_increm;
581 start += page_increm * PAGE_SIZE;
582 nr_pages -= page_increm;
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700583 } while (nr_pages);
584 return i;
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700585}
586EXPORT_SYMBOL(__get_user_pages);
587
588/*
589 * fixup_user_fault() - manually resolve a user page fault
590 * @tsk: the task_struct to use for page fault accounting, or
591 * NULL if faults are not to be recorded.
592 * @mm: mm_struct of target mm
593 * @address: user address
594 * @fault_flags:flags to pass down to handle_mm_fault()
595 *
596 * This is meant to be called in the specific scenario where for locking reasons
597 * we try to access user memory in atomic context (within a pagefault_disable()
598 * section), this returns -EFAULT, and we want to resolve the user fault before
599 * trying again.
600 *
601 * Typically this is meant to be used by the futex code.
602 *
603 * The main difference with get_user_pages() is that this function will
604 * unconditionally call handle_mm_fault() which will in turn perform all the
605 * necessary SW fixup of the dirty and young bits in the PTE, while
606 * handle_mm_fault() only guarantees to update these in the struct page.
607 *
608 * This is important for some architectures where those bits also gate the
609 * access permission to the page because they are maintained in software. On
610 * such architectures, gup() will not be enough to make a subsequent access
611 * succeed.
612 *
Paul Cassella9a95f3c2014-08-06 16:07:24 -0700613 * This has the same semantics wrt the @mm->mmap_sem as does filemap_fault().
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700614 */
615int fixup_user_fault(struct task_struct *tsk, struct mm_struct *mm,
616 unsigned long address, unsigned int fault_flags)
617{
618 struct vm_area_struct *vma;
619 vm_flags_t vm_flags;
620 int ret;
621
622 vma = find_extend_vma(mm, address);
623 if (!vma || address < vma->vm_start)
624 return -EFAULT;
625
626 vm_flags = (fault_flags & FAULT_FLAG_WRITE) ? VM_WRITE : VM_READ;
627 if (!(vm_flags & vma->vm_flags))
628 return -EFAULT;
629
630 ret = handle_mm_fault(mm, vma, address, fault_flags);
631 if (ret & VM_FAULT_ERROR) {
632 if (ret & VM_FAULT_OOM)
633 return -ENOMEM;
634 if (ret & (VM_FAULT_HWPOISON | VM_FAULT_HWPOISON_LARGE))
635 return -EHWPOISON;
Linus Torvalds33692f22015-01-29 10:51:32 -0800636 if (ret & (VM_FAULT_SIGBUS | VM_FAULT_SIGSEGV))
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700637 return -EFAULT;
638 BUG();
639 }
640 if (tsk) {
641 if (ret & VM_FAULT_MAJOR)
642 tsk->maj_flt++;
643 else
644 tsk->min_flt++;
645 }
646 return 0;
647}
648
Andrea Arcangelif0818f42015-02-11 15:27:17 -0800649static __always_inline long __get_user_pages_locked(struct task_struct *tsk,
650 struct mm_struct *mm,
651 unsigned long start,
652 unsigned long nr_pages,
653 int write, int force,
654 struct page **pages,
655 struct vm_area_struct **vmas,
Andrea Arcangeli0fd71a52015-02-11 15:27:20 -0800656 int *locked, bool notify_drop,
657 unsigned int flags)
Andrea Arcangelif0818f42015-02-11 15:27:17 -0800658{
Andrea Arcangelif0818f42015-02-11 15:27:17 -0800659 long ret, pages_done;
660 bool lock_dropped;
661
662 if (locked) {
663 /* if VM_FAULT_RETRY can be returned, vmas become invalid */
664 BUG_ON(vmas);
665 /* check caller initialized locked */
666 BUG_ON(*locked != 1);
667 }
668
669 if (pages)
670 flags |= FOLL_GET;
671 if (write)
672 flags |= FOLL_WRITE;
673 if (force)
674 flags |= FOLL_FORCE;
675
676 pages_done = 0;
677 lock_dropped = false;
678 for (;;) {
679 ret = __get_user_pages(tsk, mm, start, nr_pages, flags, pages,
680 vmas, locked);
681 if (!locked)
682 /* VM_FAULT_RETRY couldn't trigger, bypass */
683 return ret;
684
685 /* VM_FAULT_RETRY cannot return errors */
686 if (!*locked) {
687 BUG_ON(ret < 0);
688 BUG_ON(ret >= nr_pages);
689 }
690
691 if (!pages)
692 /* If it's a prefault don't insist harder */
693 return ret;
694
695 if (ret > 0) {
696 nr_pages -= ret;
697 pages_done += ret;
698 if (!nr_pages)
699 break;
700 }
701 if (*locked) {
702 /* VM_FAULT_RETRY didn't trigger */
703 if (!pages_done)
704 pages_done = ret;
705 break;
706 }
707 /* VM_FAULT_RETRY triggered, so seek to the faulting offset */
708 pages += ret;
709 start += ret << PAGE_SHIFT;
710
711 /*
712 * Repeat on the address that fired VM_FAULT_RETRY
713 * without FAULT_FLAG_ALLOW_RETRY but with
714 * FAULT_FLAG_TRIED.
715 */
716 *locked = 1;
717 lock_dropped = true;
718 down_read(&mm->mmap_sem);
719 ret = __get_user_pages(tsk, mm, start, 1, flags | FOLL_TRIED,
720 pages, NULL, NULL);
721 if (ret != 1) {
722 BUG_ON(ret > 1);
723 if (!pages_done)
724 pages_done = ret;
725 break;
726 }
727 nr_pages--;
728 pages_done++;
729 if (!nr_pages)
730 break;
731 pages++;
732 start += PAGE_SIZE;
733 }
734 if (notify_drop && lock_dropped && *locked) {
735 /*
736 * We must let the caller know we temporarily dropped the lock
737 * and so the critical section protected by it was lost.
738 */
739 up_read(&mm->mmap_sem);
740 *locked = 0;
741 }
742 return pages_done;
743}
744
745/*
746 * We can leverage the VM_FAULT_RETRY functionality in the page fault
747 * paths better by using either get_user_pages_locked() or
748 * get_user_pages_unlocked().
749 *
750 * get_user_pages_locked() is suitable to replace the form:
751 *
752 * down_read(&mm->mmap_sem);
753 * do_something()
754 * get_user_pages(tsk, mm, ..., pages, NULL);
755 * up_read(&mm->mmap_sem);
756 *
757 * to:
758 *
759 * int locked = 1;
760 * down_read(&mm->mmap_sem);
761 * do_something()
762 * get_user_pages_locked(tsk, mm, ..., pages, &locked);
763 * if (locked)
764 * up_read(&mm->mmap_sem);
765 */
766long get_user_pages_locked(struct task_struct *tsk, struct mm_struct *mm,
767 unsigned long start, unsigned long nr_pages,
768 int write, int force, struct page **pages,
769 int *locked)
770{
771 return __get_user_pages_locked(tsk, mm, start, nr_pages, write, force,
Andrea Arcangeli0fd71a52015-02-11 15:27:20 -0800772 pages, NULL, locked, true, FOLL_TOUCH);
Andrea Arcangelif0818f42015-02-11 15:27:17 -0800773}
774EXPORT_SYMBOL(get_user_pages_locked);
775
776/*
Andrea Arcangeli0fd71a52015-02-11 15:27:20 -0800777 * Same as get_user_pages_unlocked(...., FOLL_TOUCH) but it allows to
778 * pass additional gup_flags as last parameter (like FOLL_HWPOISON).
779 *
780 * NOTE: here FOLL_TOUCH is not set implicitly and must be set by the
781 * caller if required (just like with __get_user_pages). "FOLL_GET",
782 * "FOLL_WRITE" and "FOLL_FORCE" are set implicitly as needed
783 * according to the parameters "pages", "write", "force"
784 * respectively.
785 */
786__always_inline long __get_user_pages_unlocked(struct task_struct *tsk, struct mm_struct *mm,
787 unsigned long start, unsigned long nr_pages,
788 int write, int force, struct page **pages,
789 unsigned int gup_flags)
790{
791 long ret;
792 int locked = 1;
793 down_read(&mm->mmap_sem);
794 ret = __get_user_pages_locked(tsk, mm, start, nr_pages, write, force,
795 pages, NULL, &locked, false, gup_flags);
796 if (locked)
797 up_read(&mm->mmap_sem);
798 return ret;
799}
800EXPORT_SYMBOL(__get_user_pages_unlocked);
801
802/*
Andrea Arcangelif0818f42015-02-11 15:27:17 -0800803 * get_user_pages_unlocked() is suitable to replace the form:
804 *
805 * down_read(&mm->mmap_sem);
806 * get_user_pages(tsk, mm, ..., pages, NULL);
807 * up_read(&mm->mmap_sem);
808 *
809 * with:
810 *
811 * get_user_pages_unlocked(tsk, mm, ..., pages);
812 *
813 * It is functionally equivalent to get_user_pages_fast so
814 * get_user_pages_fast should be used instead, if the two parameters
815 * "tsk" and "mm" are respectively equal to current and current->mm,
816 * or if "force" shall be set to 1 (get_user_pages_fast misses the
817 * "force" parameter).
818 */
819long get_user_pages_unlocked(struct task_struct *tsk, struct mm_struct *mm,
820 unsigned long start, unsigned long nr_pages,
821 int write, int force, struct page **pages)
822{
Andrea Arcangeli0fd71a52015-02-11 15:27:20 -0800823 return __get_user_pages_unlocked(tsk, mm, start, nr_pages, write,
824 force, pages, FOLL_TOUCH);
Andrea Arcangelif0818f42015-02-11 15:27:17 -0800825}
826EXPORT_SYMBOL(get_user_pages_unlocked);
827
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700828/*
829 * get_user_pages() - pin user pages in memory
830 * @tsk: the task_struct to use for page fault accounting, or
831 * NULL if faults are not to be recorded.
832 * @mm: mm_struct of target mm
833 * @start: starting user address
834 * @nr_pages: number of pages from start to pin
835 * @write: whether pages will be written to by the caller
836 * @force: whether to force access even when user mapping is currently
837 * protected (but never forces write access to shared mapping).
838 * @pages: array that receives pointers to the pages pinned.
839 * Should be at least nr_pages long. Or NULL, if caller
840 * only intends to ensure the pages are faulted in.
841 * @vmas: array of pointers to vmas corresponding to each page.
842 * Or NULL if the caller does not require them.
843 *
844 * Returns number of pages pinned. This may be fewer than the number
845 * requested. If nr_pages is 0 or negative, returns 0. If no pages
846 * were pinned, returns -errno. Each page returned must be released
847 * with a put_page() call when it is finished with. vmas will only
848 * remain valid while mmap_sem is held.
849 *
850 * Must be called with mmap_sem held for read or write.
851 *
852 * get_user_pages walks a process's page tables and takes a reference to
853 * each struct page that each user address corresponds to at a given
854 * instant. That is, it takes the page that would be accessed if a user
855 * thread accesses the given user virtual address at that instant.
856 *
857 * This does not guarantee that the page exists in the user mappings when
858 * get_user_pages returns, and there may even be a completely different
859 * page there in some cases (eg. if mmapped pagecache has been invalidated
860 * and subsequently re faulted). However it does guarantee that the page
861 * won't be freed completely. And mostly callers simply care that the page
862 * contains data that was valid *at some point in time*. Typically, an IO
863 * or similar operation cannot guarantee anything stronger anyway because
864 * locks can't be held over the syscall boundary.
865 *
866 * If write=0, the page must not be written to. If the page is written to,
867 * set_page_dirty (or set_page_dirty_lock, as appropriate) must be called
868 * after the page is finished with, and before put_page is called.
869 *
870 * get_user_pages is typically used for fewer-copy IO operations, to get a
871 * handle on the memory by some means other than accesses via the user virtual
872 * addresses. The pages may be submitted for DMA to devices or accessed via
873 * their kernel linear mapping (via the kmap APIs). Care should be taken to
874 * use the correct cache flushing APIs.
875 *
876 * See also get_user_pages_fast, for performance critical applications.
Andrea Arcangelif0818f42015-02-11 15:27:17 -0800877 *
878 * get_user_pages should be phased out in favor of
879 * get_user_pages_locked|unlocked or get_user_pages_fast. Nothing
880 * should use get_user_pages because it cannot pass
881 * FAULT_FLAG_ALLOW_RETRY to handle_mm_fault.
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700882 */
883long get_user_pages(struct task_struct *tsk, struct mm_struct *mm,
884 unsigned long start, unsigned long nr_pages, int write,
885 int force, struct page **pages, struct vm_area_struct **vmas)
886{
Andrea Arcangelif0818f42015-02-11 15:27:17 -0800887 return __get_user_pages_locked(tsk, mm, start, nr_pages, write, force,
Andrea Arcangeli0fd71a52015-02-11 15:27:20 -0800888 pages, vmas, NULL, false, FOLL_TOUCH);
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700889}
890EXPORT_SYMBOL(get_user_pages);
891
892/**
Kirill A. Shutemovacc3c8d2015-04-14 15:44:45 -0700893 * populate_vma_page_range() - populate a range of pages in the vma.
894 * @vma: target vma
895 * @start: start address
896 * @end: end address
897 * @nonblocking:
898 *
899 * This takes care of mlocking the pages too if VM_LOCKED is set.
900 *
901 * return 0 on success, negative error code on error.
902 *
903 * vma->vm_mm->mmap_sem must be held.
904 *
905 * If @nonblocking is NULL, it may be held for read or write and will
906 * be unperturbed.
907 *
908 * If @nonblocking is non-NULL, it must held for read only and may be
909 * released. If it's released, *@nonblocking will be set to 0.
910 */
911long populate_vma_page_range(struct vm_area_struct *vma,
912 unsigned long start, unsigned long end, int *nonblocking)
913{
914 struct mm_struct *mm = vma->vm_mm;
915 unsigned long nr_pages = (end - start) / PAGE_SIZE;
916 int gup_flags;
917
918 VM_BUG_ON(start & ~PAGE_MASK);
919 VM_BUG_ON(end & ~PAGE_MASK);
920 VM_BUG_ON_VMA(start < vma->vm_start, vma);
921 VM_BUG_ON_VMA(end > vma->vm_end, vma);
922 VM_BUG_ON_MM(!rwsem_is_locked(&mm->mmap_sem), mm);
923
Eric B Munsonde60f5f2015-11-05 18:51:36 -0800924 gup_flags = FOLL_TOUCH | FOLL_POPULATE | FOLL_MLOCK;
925 if (vma->vm_flags & VM_LOCKONFAULT)
926 gup_flags &= ~FOLL_POPULATE;
Kirill A. Shutemovacc3c8d2015-04-14 15:44:45 -0700927 /*
928 * We want to touch writable mappings with a write fault in order
929 * to break COW, except for shared mappings because these don't COW
930 * and we would not want to dirty them for nothing.
931 */
932 if ((vma->vm_flags & (VM_WRITE | VM_SHARED)) == VM_WRITE)
933 gup_flags |= FOLL_WRITE;
934
935 /*
936 * We want mlock to succeed for regions that have any permissions
937 * other than PROT_NONE.
938 */
939 if (vma->vm_flags & (VM_READ | VM_WRITE | VM_EXEC))
940 gup_flags |= FOLL_FORCE;
941
942 /*
943 * We made sure addr is within a VMA, so the following will
944 * not result in a stack expansion that recurses back here.
945 */
946 return __get_user_pages(current, mm, start, nr_pages, gup_flags,
947 NULL, NULL, nonblocking);
948}
949
950/*
951 * __mm_populate - populate and/or mlock pages within a range of address space.
952 *
953 * This is used to implement mlock() and the MAP_POPULATE / MAP_LOCKED mmap
954 * flags. VMAs must be already marked with the desired vm_flags, and
955 * mmap_sem must not be held.
956 */
957int __mm_populate(unsigned long start, unsigned long len, int ignore_errors)
958{
959 struct mm_struct *mm = current->mm;
960 unsigned long end, nstart, nend;
961 struct vm_area_struct *vma = NULL;
962 int locked = 0;
963 long ret = 0;
964
965 VM_BUG_ON(start & ~PAGE_MASK);
966 VM_BUG_ON(len != PAGE_ALIGN(len));
967 end = start + len;
968
969 for (nstart = start; nstart < end; nstart = nend) {
970 /*
971 * We want to fault in pages for [nstart; end) address range.
972 * Find first corresponding VMA.
973 */
974 if (!locked) {
975 locked = 1;
976 down_read(&mm->mmap_sem);
977 vma = find_vma(mm, nstart);
978 } else if (nstart >= vma->vm_end)
979 vma = vma->vm_next;
980 if (!vma || vma->vm_start >= end)
981 break;
982 /*
983 * Set [nstart; nend) to intersection of desired address
984 * range with the first VMA. Also, skip undesirable VMA types.
985 */
986 nend = min(end, vma->vm_end);
987 if (vma->vm_flags & (VM_IO | VM_PFNMAP))
988 continue;
989 if (nstart < vma->vm_start)
990 nstart = vma->vm_start;
991 /*
992 * Now fault in a range of pages. populate_vma_page_range()
993 * double checks the vma flags, so that it won't mlock pages
994 * if the vma was already munlocked.
995 */
996 ret = populate_vma_page_range(vma, nstart, nend, &locked);
997 if (ret < 0) {
998 if (ignore_errors) {
999 ret = 0;
1000 continue; /* continue at next VMA */
1001 }
1002 break;
1003 }
1004 nend = nstart + ret * PAGE_SIZE;
1005 ret = 0;
1006 }
1007 if (locked)
1008 up_read(&mm->mmap_sem);
1009 return ret; /* 0 or negative error code */
1010}
1011
1012/**
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -07001013 * get_dump_page() - pin user page in memory while writing it to core dump
1014 * @addr: user address
1015 *
1016 * Returns struct page pointer of user page pinned for dump,
1017 * to be freed afterwards by page_cache_release() or put_page().
1018 *
1019 * Returns NULL on any kind of failure - a hole must then be inserted into
1020 * the corefile, to preserve alignment with its headers; and also returns
1021 * NULL wherever the ZERO_PAGE, or an anonymous pte_none, has been found -
1022 * allowing a hole to be left in the corefile to save diskspace.
1023 *
1024 * Called without mmap_sem, but after all other threads have been killed.
1025 */
1026#ifdef CONFIG_ELF_CORE
1027struct page *get_dump_page(unsigned long addr)
1028{
1029 struct vm_area_struct *vma;
1030 struct page *page;
1031
1032 if (__get_user_pages(current, current->mm, addr, 1,
1033 FOLL_FORCE | FOLL_DUMP | FOLL_GET, &page, &vma,
1034 NULL) < 1)
1035 return NULL;
1036 flush_cache_page(vma, addr, page_to_pfn(page));
1037 return page;
1038}
1039#endif /* CONFIG_ELF_CORE */
Steve Capper2667f502014-10-09 15:29:14 -07001040
1041/*
1042 * Generic RCU Fast GUP
1043 *
1044 * get_user_pages_fast attempts to pin user pages by walking the page
1045 * tables directly and avoids taking locks. Thus the walker needs to be
1046 * protected from page table pages being freed from under it, and should
1047 * block any THP splits.
1048 *
1049 * One way to achieve this is to have the walker disable interrupts, and
1050 * rely on IPIs from the TLB flushing code blocking before the page table
1051 * pages are freed. This is unsuitable for architectures that do not need
1052 * to broadcast an IPI when invalidating TLBs.
1053 *
1054 * Another way to achieve this is to batch up page table containing pages
1055 * belonging to more than one mm_user, then rcu_sched a callback to free those
1056 * pages. Disabling interrupts will allow the fast_gup walker to both block
1057 * the rcu_sched callback, and an IPI that we broadcast for splitting THPs
1058 * (which is a relatively rare event). The code below adopts this strategy.
1059 *
1060 * Before activating this code, please be aware that the following assumptions
1061 * are currently made:
1062 *
1063 * *) HAVE_RCU_TABLE_FREE is enabled, and tlb_remove_table is used to free
1064 * pages containing page tables.
1065 *
Steve Capper2667f502014-10-09 15:29:14 -07001066 * *) ptes can be read atomically by the architecture.
1067 *
1068 * *) access_ok is sufficient to validate userspace address ranges.
1069 *
1070 * The last two assumptions can be relaxed by the addition of helper functions.
1071 *
1072 * This code is based heavily on the PowerPC implementation by Nick Piggin.
1073 */
1074#ifdef CONFIG_HAVE_GENERIC_RCU_GUP
1075
1076#ifdef __HAVE_ARCH_PTE_SPECIAL
1077static int gup_pte_range(pmd_t pmd, unsigned long addr, unsigned long end,
1078 int write, struct page **pages, int *nr)
1079{
1080 pte_t *ptep, *ptem;
1081 int ret = 0;
1082
1083 ptem = ptep = pte_offset_map(&pmd, addr);
1084 do {
1085 /*
1086 * In the line below we are assuming that the pte can be read
1087 * atomically. If this is not the case for your architecture,
1088 * please wrap this in a helper function!
1089 *
1090 * for an example see gup_get_pte in arch/x86/mm/gup.c
1091 */
Jason Low9d8c47e2015-04-15 16:14:05 -07001092 pte_t pte = READ_ONCE(*ptep);
Kirill A. Shutemov7aef4172016-01-15 16:52:32 -08001093 struct page *head, *page;
Steve Capper2667f502014-10-09 15:29:14 -07001094
1095 /*
1096 * Similar to the PMD case below, NUMA hinting must take slow
Mel Gorman8a0516e2015-02-12 14:58:22 -08001097 * path using the pte_protnone check.
Steve Capper2667f502014-10-09 15:29:14 -07001098 */
1099 if (!pte_present(pte) || pte_special(pte) ||
Mel Gorman8a0516e2015-02-12 14:58:22 -08001100 pte_protnone(pte) || (write && !pte_write(pte)))
Steve Capper2667f502014-10-09 15:29:14 -07001101 goto pte_unmap;
1102
1103 VM_BUG_ON(!pfn_valid(pte_pfn(pte)));
1104 page = pte_page(pte);
Kirill A. Shutemov7aef4172016-01-15 16:52:32 -08001105 head = compound_head(page);
Steve Capper2667f502014-10-09 15:29:14 -07001106
Kirill A. Shutemov7aef4172016-01-15 16:52:32 -08001107 if (!page_cache_get_speculative(head))
Steve Capper2667f502014-10-09 15:29:14 -07001108 goto pte_unmap;
1109
1110 if (unlikely(pte_val(pte) != pte_val(*ptep))) {
Kirill A. Shutemov7aef4172016-01-15 16:52:32 -08001111 put_page(head);
Steve Capper2667f502014-10-09 15:29:14 -07001112 goto pte_unmap;
1113 }
1114
Kirill A. Shutemov7aef4172016-01-15 16:52:32 -08001115 VM_BUG_ON_PAGE(compound_head(page) != head, page);
Steve Capper2667f502014-10-09 15:29:14 -07001116 pages[*nr] = page;
1117 (*nr)++;
1118
1119 } while (ptep++, addr += PAGE_SIZE, addr != end);
1120
1121 ret = 1;
1122
1123pte_unmap:
1124 pte_unmap(ptem);
1125 return ret;
1126}
1127#else
1128
1129/*
1130 * If we can't determine whether or not a pte is special, then fail immediately
1131 * for ptes. Note, we can still pin HugeTLB and THP as these are guaranteed not
1132 * to be special.
1133 *
1134 * For a futex to be placed on a THP tail page, get_futex_key requires a
1135 * __get_user_pages_fast implementation that can pin pages. Thus it's still
1136 * useful to have gup_huge_pmd even if we can't operate on ptes.
1137 */
1138static int gup_pte_range(pmd_t pmd, unsigned long addr, unsigned long end,
1139 int write, struct page **pages, int *nr)
1140{
1141 return 0;
1142}
1143#endif /* __HAVE_ARCH_PTE_SPECIAL */
1144
1145static int gup_huge_pmd(pmd_t orig, pmd_t *pmdp, unsigned long addr,
1146 unsigned long end, int write, struct page **pages, int *nr)
1147{
Kirill A. Shutemovddc58f22016-01-15 16:52:56 -08001148 struct page *head, *page;
Steve Capper2667f502014-10-09 15:29:14 -07001149 int refs;
1150
1151 if (write && !pmd_write(orig))
1152 return 0;
1153
1154 refs = 0;
1155 head = pmd_page(orig);
1156 page = head + ((addr & ~PMD_MASK) >> PAGE_SHIFT);
Steve Capper2667f502014-10-09 15:29:14 -07001157 do {
1158 VM_BUG_ON_PAGE(compound_head(page) != head, page);
1159 pages[*nr] = page;
1160 (*nr)++;
1161 page++;
1162 refs++;
1163 } while (addr += PAGE_SIZE, addr != end);
1164
1165 if (!page_cache_add_speculative(head, refs)) {
1166 *nr -= refs;
1167 return 0;
1168 }
1169
1170 if (unlikely(pmd_val(orig) != pmd_val(*pmdp))) {
1171 *nr -= refs;
1172 while (refs--)
1173 put_page(head);
1174 return 0;
1175 }
1176
Steve Capper2667f502014-10-09 15:29:14 -07001177 return 1;
1178}
1179
1180static int gup_huge_pud(pud_t orig, pud_t *pudp, unsigned long addr,
1181 unsigned long end, int write, struct page **pages, int *nr)
1182{
Kirill A. Shutemovddc58f22016-01-15 16:52:56 -08001183 struct page *head, *page;
Steve Capper2667f502014-10-09 15:29:14 -07001184 int refs;
1185
1186 if (write && !pud_write(orig))
1187 return 0;
1188
1189 refs = 0;
1190 head = pud_page(orig);
1191 page = head + ((addr & ~PUD_MASK) >> PAGE_SHIFT);
Steve Capper2667f502014-10-09 15:29:14 -07001192 do {
1193 VM_BUG_ON_PAGE(compound_head(page) != head, page);
1194 pages[*nr] = page;
1195 (*nr)++;
1196 page++;
1197 refs++;
1198 } while (addr += PAGE_SIZE, addr != end);
1199
1200 if (!page_cache_add_speculative(head, refs)) {
1201 *nr -= refs;
1202 return 0;
1203 }
1204
1205 if (unlikely(pud_val(orig) != pud_val(*pudp))) {
1206 *nr -= refs;
1207 while (refs--)
1208 put_page(head);
1209 return 0;
1210 }
1211
Steve Capper2667f502014-10-09 15:29:14 -07001212 return 1;
1213}
1214
Aneesh Kumar K.Vf30c59e2014-11-05 21:57:40 +05301215static int gup_huge_pgd(pgd_t orig, pgd_t *pgdp, unsigned long addr,
1216 unsigned long end, int write,
1217 struct page **pages, int *nr)
1218{
1219 int refs;
Kirill A. Shutemovddc58f22016-01-15 16:52:56 -08001220 struct page *head, *page;
Aneesh Kumar K.Vf30c59e2014-11-05 21:57:40 +05301221
1222 if (write && !pgd_write(orig))
1223 return 0;
1224
1225 refs = 0;
1226 head = pgd_page(orig);
1227 page = head + ((addr & ~PGDIR_MASK) >> PAGE_SHIFT);
Aneesh Kumar K.Vf30c59e2014-11-05 21:57:40 +05301228 do {
1229 VM_BUG_ON_PAGE(compound_head(page) != head, page);
1230 pages[*nr] = page;
1231 (*nr)++;
1232 page++;
1233 refs++;
1234 } while (addr += PAGE_SIZE, addr != end);
1235
1236 if (!page_cache_add_speculative(head, refs)) {
1237 *nr -= refs;
1238 return 0;
1239 }
1240
1241 if (unlikely(pgd_val(orig) != pgd_val(*pgdp))) {
1242 *nr -= refs;
1243 while (refs--)
1244 put_page(head);
1245 return 0;
1246 }
1247
Aneesh Kumar K.Vf30c59e2014-11-05 21:57:40 +05301248 return 1;
1249}
1250
Steve Capper2667f502014-10-09 15:29:14 -07001251static int gup_pmd_range(pud_t pud, unsigned long addr, unsigned long end,
1252 int write, struct page **pages, int *nr)
1253{
1254 unsigned long next;
1255 pmd_t *pmdp;
1256
1257 pmdp = pmd_offset(&pud, addr);
1258 do {
Christian Borntraeger38c5ce92015-01-06 22:54:46 +01001259 pmd_t pmd = READ_ONCE(*pmdp);
Steve Capper2667f502014-10-09 15:29:14 -07001260
1261 next = pmd_addr_end(addr, end);
Kirill A. Shutemov4b471e82016-01-15 16:53:39 -08001262 if (pmd_none(pmd))
Steve Capper2667f502014-10-09 15:29:14 -07001263 return 0;
1264
1265 if (unlikely(pmd_trans_huge(pmd) || pmd_huge(pmd))) {
1266 /*
1267 * NUMA hinting faults need to be handled in the GUP
1268 * slowpath for accounting purposes and so that they
1269 * can be serialised against THP migration.
1270 */
Mel Gorman8a0516e2015-02-12 14:58:22 -08001271 if (pmd_protnone(pmd))
Steve Capper2667f502014-10-09 15:29:14 -07001272 return 0;
1273
1274 if (!gup_huge_pmd(pmd, pmdp, addr, next, write,
1275 pages, nr))
1276 return 0;
1277
Aneesh Kumar K.Vf30c59e2014-11-05 21:57:40 +05301278 } else if (unlikely(is_hugepd(__hugepd(pmd_val(pmd))))) {
1279 /*
1280 * architecture have different format for hugetlbfs
1281 * pmd format and THP pmd format
1282 */
1283 if (!gup_huge_pd(__hugepd(pmd_val(pmd)), addr,
1284 PMD_SHIFT, next, write, pages, nr))
1285 return 0;
Steve Capper2667f502014-10-09 15:29:14 -07001286 } else if (!gup_pte_range(pmd, addr, next, write, pages, nr))
1287 return 0;
1288 } while (pmdp++, addr = next, addr != end);
1289
1290 return 1;
1291}
1292
Aneesh Kumar K.Vf30c59e2014-11-05 21:57:40 +05301293static int gup_pud_range(pgd_t pgd, unsigned long addr, unsigned long end,
1294 int write, struct page **pages, int *nr)
Steve Capper2667f502014-10-09 15:29:14 -07001295{
1296 unsigned long next;
1297 pud_t *pudp;
1298
Aneesh Kumar K.Vf30c59e2014-11-05 21:57:40 +05301299 pudp = pud_offset(&pgd, addr);
Steve Capper2667f502014-10-09 15:29:14 -07001300 do {
Christian Borntraegere37c6982014-12-07 21:41:33 +01001301 pud_t pud = READ_ONCE(*pudp);
Steve Capper2667f502014-10-09 15:29:14 -07001302
1303 next = pud_addr_end(addr, end);
1304 if (pud_none(pud))
1305 return 0;
Aneesh Kumar K.Vf30c59e2014-11-05 21:57:40 +05301306 if (unlikely(pud_huge(pud))) {
Steve Capper2667f502014-10-09 15:29:14 -07001307 if (!gup_huge_pud(pud, pudp, addr, next, write,
Aneesh Kumar K.Vf30c59e2014-11-05 21:57:40 +05301308 pages, nr))
1309 return 0;
1310 } else if (unlikely(is_hugepd(__hugepd(pud_val(pud))))) {
1311 if (!gup_huge_pd(__hugepd(pud_val(pud)), addr,
1312 PUD_SHIFT, next, write, pages, nr))
Steve Capper2667f502014-10-09 15:29:14 -07001313 return 0;
1314 } else if (!gup_pmd_range(pud, addr, next, write, pages, nr))
1315 return 0;
1316 } while (pudp++, addr = next, addr != end);
1317
1318 return 1;
1319}
1320
1321/*
1322 * Like get_user_pages_fast() except it's IRQ-safe in that it won't fall back to
1323 * the regular GUP. It will only return non-negative values.
1324 */
1325int __get_user_pages_fast(unsigned long start, int nr_pages, int write,
1326 struct page **pages)
1327{
1328 struct mm_struct *mm = current->mm;
1329 unsigned long addr, len, end;
1330 unsigned long next, flags;
1331 pgd_t *pgdp;
1332 int nr = 0;
1333
1334 start &= PAGE_MASK;
1335 addr = start;
1336 len = (unsigned long) nr_pages << PAGE_SHIFT;
1337 end = start + len;
1338
1339 if (unlikely(!access_ok(write ? VERIFY_WRITE : VERIFY_READ,
1340 start, len)))
1341 return 0;
1342
1343 /*
1344 * Disable interrupts. We use the nested form as we can already have
1345 * interrupts disabled by get_futex_key.
1346 *
1347 * With interrupts disabled, we block page table pages from being
1348 * freed from under us. See mmu_gather_tlb in asm-generic/tlb.h
1349 * for more details.
1350 *
1351 * We do not adopt an rcu_read_lock(.) here as we also want to
1352 * block IPIs that come from THPs splitting.
1353 */
1354
1355 local_irq_save(flags);
1356 pgdp = pgd_offset(mm, addr);
1357 do {
Jason Low9d8c47e2015-04-15 16:14:05 -07001358 pgd_t pgd = READ_ONCE(*pgdp);
Aneesh Kumar K.Vf30c59e2014-11-05 21:57:40 +05301359
Steve Capper2667f502014-10-09 15:29:14 -07001360 next = pgd_addr_end(addr, end);
Aneesh Kumar K.Vf30c59e2014-11-05 21:57:40 +05301361 if (pgd_none(pgd))
Steve Capper2667f502014-10-09 15:29:14 -07001362 break;
Aneesh Kumar K.Vf30c59e2014-11-05 21:57:40 +05301363 if (unlikely(pgd_huge(pgd))) {
1364 if (!gup_huge_pgd(pgd, pgdp, addr, next, write,
1365 pages, &nr))
1366 break;
1367 } else if (unlikely(is_hugepd(__hugepd(pgd_val(pgd))))) {
1368 if (!gup_huge_pd(__hugepd(pgd_val(pgd)), addr,
1369 PGDIR_SHIFT, next, write, pages, &nr))
1370 break;
1371 } else if (!gup_pud_range(pgd, addr, next, write, pages, &nr))
Steve Capper2667f502014-10-09 15:29:14 -07001372 break;
1373 } while (pgdp++, addr = next, addr != end);
1374 local_irq_restore(flags);
1375
1376 return nr;
1377}
1378
1379/**
1380 * get_user_pages_fast() - pin user pages in memory
1381 * @start: starting user address
1382 * @nr_pages: number of pages from start to pin
1383 * @write: whether pages will be written to
1384 * @pages: array that receives pointers to the pages pinned.
1385 * Should be at least nr_pages long.
1386 *
1387 * Attempt to pin user pages in memory without taking mm->mmap_sem.
1388 * If not successful, it will fall back to taking the lock and
1389 * calling get_user_pages().
1390 *
1391 * Returns number of pages pinned. This may be fewer than the number
1392 * requested. If nr_pages is 0 or negative, returns 0. If no pages
1393 * were pinned, returns -errno.
1394 */
1395int get_user_pages_fast(unsigned long start, int nr_pages, int write,
1396 struct page **pages)
1397{
1398 struct mm_struct *mm = current->mm;
1399 int nr, ret;
1400
1401 start &= PAGE_MASK;
1402 nr = __get_user_pages_fast(start, nr_pages, write, pages);
1403 ret = nr;
1404
1405 if (nr < nr_pages) {
1406 /* Try to get the remaining pages with get_user_pages */
1407 start += nr << PAGE_SHIFT;
1408 pages += nr;
1409
Andrea Arcangelia7b78072015-02-11 15:27:23 -08001410 ret = get_user_pages_unlocked(current, mm, start,
1411 nr_pages - nr, write, 0, pages);
Steve Capper2667f502014-10-09 15:29:14 -07001412
1413 /* Have to be a bit careful with return values */
1414 if (nr > 0) {
1415 if (ret < 0)
1416 ret = nr;
1417 else
1418 ret += nr;
1419 }
1420 }
1421
1422 return ret;
1423}
1424
1425#endif /* CONFIG_HAVE_GENERIC_RCU_GUP */