blob: f76e77a2d34b79afec5f3032366a6bd954d1aead [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>
Dan Williams3565fce2016-01-15 16:56:55 -08007#include <linux/memremap.h>
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -07008#include <linux/pagemap.h>
9#include <linux/rmap.h>
10#include <linux/swap.h>
11#include <linux/swapops.h>
12
Ingo Molnar174cd4b2017-02-02 19:15:33 +010013#include <linux/sched/signal.h>
Steve Capper2667f502014-10-09 15:29:14 -070014#include <linux/rwsem.h>
Aneesh Kumar K.Vf30c59e2014-11-05 21:57:40 +053015#include <linux/hugetlb.h>
Kirill A. Shutemov1027e442015-09-04 15:47:55 -070016
Dave Hansen33a709b2016-02-12 13:02:19 -080017#include <asm/mmu_context.h>
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
Keith Buschdf06b372018-10-26 15:10:28 -070023struct follow_page_context {
24 struct dev_pagemap *pgmap;
25 unsigned int page_mask;
26};
27
Kirill A. Shutemov69e68b42014-06-04 16:08:11 -070028static struct page *no_page_table(struct vm_area_struct *vma,
29 unsigned int flags)
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -070030{
Kirill A. Shutemov69e68b42014-06-04 16:08:11 -070031 /*
32 * When core dumping an enormous anonymous area that nobody
33 * has touched so far, we don't want to allocate unnecessary pages or
34 * page tables. Return error instead of NULL to skip handle_mm_fault,
35 * then get_dump_page() will return NULL to leave a hole in the dump.
36 * But we can only make this optimization where a hole would surely
37 * be zero-filled if handle_mm_fault() actually did handle it.
38 */
39 if ((flags & FOLL_DUMP) && (!vma->vm_ops || !vma->vm_ops->fault))
40 return ERR_PTR(-EFAULT);
41 return NULL;
42}
43
Kirill A. Shutemov1027e442015-09-04 15:47:55 -070044static int follow_pfn_pte(struct vm_area_struct *vma, unsigned long address,
45 pte_t *pte, unsigned int flags)
46{
47 /* No page to get reference */
48 if (flags & FOLL_GET)
49 return -EFAULT;
50
51 if (flags & FOLL_TOUCH) {
52 pte_t entry = *pte;
53
54 if (flags & FOLL_WRITE)
55 entry = pte_mkdirty(entry);
56 entry = pte_mkyoung(entry);
57
58 if (!pte_same(*pte, entry)) {
59 set_pte_at(vma->vm_mm, address, pte, entry);
60 update_mmu_cache(vma, address, pte);
61 }
62 }
63
64 /* Proper page table entry exists, but no corresponding struct page */
65 return -EEXIST;
66}
67
Linus Torvalds19be0ea2016-10-13 13:07:36 -070068/*
69 * FOLL_FORCE can write to even unwritable pte's, but only
70 * after we've gone through a COW cycle and they are dirty.
71 */
72static inline bool can_follow_write_pte(pte_t pte, unsigned int flags)
73{
Linus Torvaldsf6f37322017-12-15 18:53:22 -080074 return pte_write(pte) ||
Linus Torvalds19be0ea2016-10-13 13:07:36 -070075 ((flags & FOLL_FORCE) && (flags & FOLL_COW) && pte_dirty(pte));
76}
77
Kirill A. Shutemov69e68b42014-06-04 16:08:11 -070078static struct page *follow_page_pte(struct vm_area_struct *vma,
Keith Buschdf06b372018-10-26 15:10:28 -070079 unsigned long address, pmd_t *pmd, unsigned int flags,
80 struct dev_pagemap **pgmap)
Kirill A. Shutemov69e68b42014-06-04 16:08:11 -070081{
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -070082 struct mm_struct *mm = vma->vm_mm;
Kirill A. Shutemov69e68b42014-06-04 16:08:11 -070083 struct page *page;
84 spinlock_t *ptl;
85 pte_t *ptep, pte;
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -070086
Kirill A. Shutemov69e68b42014-06-04 16:08:11 -070087retry:
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -070088 if (unlikely(pmd_bad(*pmd)))
Kirill A. Shutemov69e68b42014-06-04 16:08:11 -070089 return no_page_table(vma, flags);
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -070090
91 ptep = pte_offset_map_lock(mm, pmd, address, &ptl);
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -070092 pte = *ptep;
93 if (!pte_present(pte)) {
94 swp_entry_t entry;
95 /*
96 * KSM's break_ksm() relies upon recognizing a ksm page
97 * even while it is being migrated, so for that case we
98 * need migration_entry_wait().
99 */
100 if (likely(!(flags & FOLL_MIGRATION)))
101 goto no_page;
Kirill A. Shutemov0661a332015-02-10 14:10:04 -0800102 if (pte_none(pte))
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700103 goto no_page;
104 entry = pte_to_swp_entry(pte);
105 if (!is_migration_entry(entry))
106 goto no_page;
107 pte_unmap_unlock(ptep, ptl);
108 migration_entry_wait(mm, pmd, address);
Kirill A. Shutemov69e68b42014-06-04 16:08:11 -0700109 goto retry;
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700110 }
Mel Gorman8a0516e2015-02-12 14:58:22 -0800111 if ((flags & FOLL_NUMA) && pte_protnone(pte))
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700112 goto no_page;
Linus Torvalds19be0ea2016-10-13 13:07:36 -0700113 if ((flags & FOLL_WRITE) && !can_follow_write_pte(pte, flags)) {
Kirill A. Shutemov69e68b42014-06-04 16:08:11 -0700114 pte_unmap_unlock(ptep, ptl);
115 return NULL;
116 }
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700117
118 page = vm_normal_page(vma, address, pte);
Dan Williams3565fce2016-01-15 16:56:55 -0800119 if (!page && pte_devmap(pte) && (flags & FOLL_GET)) {
120 /*
121 * Only return device mapping pages in the FOLL_GET case since
122 * they are only valid while holding the pgmap reference.
123 */
Keith Buschdf06b372018-10-26 15:10:28 -0700124 *pgmap = get_dev_pagemap(pte_pfn(pte), *pgmap);
125 if (*pgmap)
Dan Williams3565fce2016-01-15 16:56:55 -0800126 page = pte_page(pte);
127 else
128 goto no_page;
129 } else if (unlikely(!page)) {
Kirill A. Shutemov1027e442015-09-04 15:47:55 -0700130 if (flags & FOLL_DUMP) {
131 /* Avoid special (like zero) pages in core dumps */
132 page = ERR_PTR(-EFAULT);
133 goto out;
134 }
135
136 if (is_zero_pfn(pte_pfn(pte))) {
137 page = pte_page(pte);
138 } else {
139 int ret;
140
141 ret = follow_pfn_pte(vma, address, ptep, flags);
142 page = ERR_PTR(ret);
143 goto out;
144 }
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700145 }
146
Kirill A. Shutemov6742d292016-01-15 16:52:28 -0800147 if (flags & FOLL_SPLIT && PageTransCompound(page)) {
148 int ret;
149 get_page(page);
150 pte_unmap_unlock(ptep, ptl);
151 lock_page(page);
152 ret = split_huge_page(page);
153 unlock_page(page);
154 put_page(page);
155 if (ret)
156 return ERR_PTR(ret);
157 goto retry;
158 }
159
Keith Buschdf06b372018-10-26 15:10:28 -0700160 if (flags & FOLL_GET)
Kirill A. Shutemovddc58f22016-01-15 16:52:56 -0800161 get_page(page);
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700162 if (flags & FOLL_TOUCH) {
163 if ((flags & FOLL_WRITE) &&
164 !pte_dirty(pte) && !PageDirty(page))
165 set_page_dirty(page);
166 /*
167 * pte_mkyoung() would be more correct here, but atomic care
168 * is needed to avoid losing the dirty bit: it is easier to use
169 * mark_page_accessed().
170 */
171 mark_page_accessed(page);
172 }
Eric B Munsonde60f5f2015-11-05 18:51:36 -0800173 if ((flags & FOLL_MLOCK) && (vma->vm_flags & VM_LOCKED)) {
Kirill A. Shutemove90309c2016-01-15 16:54:33 -0800174 /* Do not mlock pte-mapped THP */
175 if (PageTransCompound(page))
176 goto out;
177
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700178 /*
179 * The preliminary mapping check is mainly to avoid the
180 * pointless overhead of lock_page on the ZERO_PAGE
181 * which might bounce very badly if there is contention.
182 *
183 * If the page is already locked, we don't need to
184 * handle it now - vmscan will handle it later if and
185 * when it attempts to reclaim the page.
186 */
187 if (page->mapping && trylock_page(page)) {
188 lru_add_drain(); /* push cached pages to LRU */
189 /*
190 * Because we lock page here, and migration is
191 * blocked by the pte's page reference, and we
192 * know the page is still mapped, we don't even
193 * need to check for file-cache page truncation.
194 */
195 mlock_vma_page(page);
196 unlock_page(page);
197 }
198 }
Kirill A. Shutemov1027e442015-09-04 15:47:55 -0700199out:
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700200 pte_unmap_unlock(ptep, ptl);
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700201 return page;
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700202no_page:
203 pte_unmap_unlock(ptep, ptl);
204 if (!pte_none(pte))
Kirill A. Shutemov69e68b42014-06-04 16:08:11 -0700205 return NULL;
206 return no_page_table(vma, flags);
207}
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700208
Aneesh Kumar K.V080dbb62017-07-06 15:38:44 -0700209static struct page *follow_pmd_mask(struct vm_area_struct *vma,
210 unsigned long address, pud_t *pudp,
Keith Buschdf06b372018-10-26 15:10:28 -0700211 unsigned int flags,
212 struct follow_page_context *ctx)
Kirill A. Shutemov69e68b42014-06-04 16:08:11 -0700213{
Huang Ying68827282018-06-07 17:06:34 -0700214 pmd_t *pmd, pmdval;
Kirill A. Shutemov69e68b42014-06-04 16:08:11 -0700215 spinlock_t *ptl;
216 struct page *page;
217 struct mm_struct *mm = vma->vm_mm;
218
Aneesh Kumar K.V080dbb62017-07-06 15:38:44 -0700219 pmd = pmd_offset(pudp, address);
Huang Ying68827282018-06-07 17:06:34 -0700220 /*
221 * The READ_ONCE() will stabilize the pmdval in a register or
222 * on the stack so that it will stop changing under the code.
223 */
224 pmdval = READ_ONCE(*pmd);
225 if (pmd_none(pmdval))
Kirill A. Shutemov69e68b42014-06-04 16:08:11 -0700226 return no_page_table(vma, flags);
Huang Ying68827282018-06-07 17:06:34 -0700227 if (pmd_huge(pmdval) && vma->vm_flags & VM_HUGETLB) {
Naoya Horiguchie66f17f2015-02-11 15:25:22 -0800228 page = follow_huge_pmd(mm, address, pmd, flags);
229 if (page)
230 return page;
231 return no_page_table(vma, flags);
Kirill A. Shutemov69e68b42014-06-04 16:08:11 -0700232 }
Huang Ying68827282018-06-07 17:06:34 -0700233 if (is_hugepd(__hugepd(pmd_val(pmdval)))) {
Aneesh Kumar K.V4dc71452017-07-06 15:38:56 -0700234 page = follow_huge_pd(vma, address,
Huang Ying68827282018-06-07 17:06:34 -0700235 __hugepd(pmd_val(pmdval)), flags,
Aneesh Kumar K.V4dc71452017-07-06 15:38:56 -0700236 PMD_SHIFT);
237 if (page)
238 return page;
239 return no_page_table(vma, flags);
240 }
Zi Yan84c3fc42017-09-08 16:11:01 -0700241retry:
Huang Ying68827282018-06-07 17:06:34 -0700242 if (!pmd_present(pmdval)) {
Zi Yan84c3fc42017-09-08 16:11:01 -0700243 if (likely(!(flags & FOLL_MIGRATION)))
244 return no_page_table(vma, flags);
245 VM_BUG_ON(thp_migration_supported() &&
Huang Ying68827282018-06-07 17:06:34 -0700246 !is_pmd_migration_entry(pmdval));
247 if (is_pmd_migration_entry(pmdval))
Zi Yan84c3fc42017-09-08 16:11:01 -0700248 pmd_migration_entry_wait(mm, pmd);
Huang Ying68827282018-06-07 17:06:34 -0700249 pmdval = READ_ONCE(*pmd);
250 /*
251 * MADV_DONTNEED may convert the pmd to null because
252 * mmap_sem is held in read mode
253 */
254 if (pmd_none(pmdval))
255 return no_page_table(vma, flags);
Zi Yan84c3fc42017-09-08 16:11:01 -0700256 goto retry;
257 }
Huang Ying68827282018-06-07 17:06:34 -0700258 if (pmd_devmap(pmdval)) {
Dan Williams3565fce2016-01-15 16:56:55 -0800259 ptl = pmd_lock(mm, pmd);
Keith Buschdf06b372018-10-26 15:10:28 -0700260 page = follow_devmap_pmd(vma, address, pmd, flags, &ctx->pgmap);
Dan Williams3565fce2016-01-15 16:56:55 -0800261 spin_unlock(ptl);
262 if (page)
263 return page;
264 }
Huang Ying68827282018-06-07 17:06:34 -0700265 if (likely(!pmd_trans_huge(pmdval)))
Keith Buschdf06b372018-10-26 15:10:28 -0700266 return follow_page_pte(vma, address, pmd, flags, &ctx->pgmap);
Kirill A. Shutemov6742d292016-01-15 16:52:28 -0800267
Huang Ying68827282018-06-07 17:06:34 -0700268 if ((flags & FOLL_NUMA) && pmd_protnone(pmdval))
Aneesh Kumar K.Vdb08f202017-02-24 14:59:53 -0800269 return no_page_table(vma, flags);
270
Zi Yan84c3fc42017-09-08 16:11:01 -0700271retry_locked:
Kirill A. Shutemov6742d292016-01-15 16:52:28 -0800272 ptl = pmd_lock(mm, pmd);
Huang Ying68827282018-06-07 17:06:34 -0700273 if (unlikely(pmd_none(*pmd))) {
274 spin_unlock(ptl);
275 return no_page_table(vma, flags);
276 }
Zi Yan84c3fc42017-09-08 16:11:01 -0700277 if (unlikely(!pmd_present(*pmd))) {
278 spin_unlock(ptl);
279 if (likely(!(flags & FOLL_MIGRATION)))
280 return no_page_table(vma, flags);
281 pmd_migration_entry_wait(mm, pmd);
282 goto retry_locked;
283 }
Kirill A. Shutemov6742d292016-01-15 16:52:28 -0800284 if (unlikely(!pmd_trans_huge(*pmd))) {
285 spin_unlock(ptl);
Keith Buschdf06b372018-10-26 15:10:28 -0700286 return follow_page_pte(vma, address, pmd, flags, &ctx->pgmap);
Kirill A. Shutemov69e68b42014-06-04 16:08:11 -0700287 }
Kirill A. Shutemov6742d292016-01-15 16:52:28 -0800288 if (flags & FOLL_SPLIT) {
289 int ret;
290 page = pmd_page(*pmd);
291 if (is_huge_zero_page(page)) {
292 spin_unlock(ptl);
293 ret = 0;
Kirill A. Shutemov78ddc532016-01-15 16:52:42 -0800294 split_huge_pmd(vma, pmd, address);
Naoya Horiguchi337d9ab2016-07-26 15:24:03 -0700295 if (pmd_trans_unstable(pmd))
296 ret = -EBUSY;
Kirill A. Shutemov6742d292016-01-15 16:52:28 -0800297 } else {
298 get_page(page);
299 spin_unlock(ptl);
300 lock_page(page);
301 ret = split_huge_page(page);
302 unlock_page(page);
303 put_page(page);
Kirill A. Shutemovbaa355f2016-07-26 15:25:51 -0700304 if (pmd_none(*pmd))
305 return no_page_table(vma, flags);
Kirill A. Shutemov6742d292016-01-15 16:52:28 -0800306 }
307
308 return ret ? ERR_PTR(ret) :
Keith Buschdf06b372018-10-26 15:10:28 -0700309 follow_page_pte(vma, address, pmd, flags, &ctx->pgmap);
Kirill A. Shutemov6742d292016-01-15 16:52:28 -0800310 }
Kirill A. Shutemov6742d292016-01-15 16:52:28 -0800311 page = follow_trans_huge_pmd(vma, address, pmd, flags);
312 spin_unlock(ptl);
Keith Buschdf06b372018-10-26 15:10:28 -0700313 ctx->page_mask = HPAGE_PMD_NR - 1;
Kirill A. Shutemov6742d292016-01-15 16:52:28 -0800314 return page;
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700315}
316
Aneesh Kumar K.V080dbb62017-07-06 15:38:44 -0700317static struct page *follow_pud_mask(struct vm_area_struct *vma,
318 unsigned long address, p4d_t *p4dp,
Keith Buschdf06b372018-10-26 15:10:28 -0700319 unsigned int flags,
320 struct follow_page_context *ctx)
Aneesh Kumar K.V080dbb62017-07-06 15:38:44 -0700321{
322 pud_t *pud;
323 spinlock_t *ptl;
324 struct page *page;
325 struct mm_struct *mm = vma->vm_mm;
326
327 pud = pud_offset(p4dp, address);
328 if (pud_none(*pud))
329 return no_page_table(vma, flags);
330 if (pud_huge(*pud) && vma->vm_flags & VM_HUGETLB) {
331 page = follow_huge_pud(mm, address, pud, flags);
332 if (page)
333 return page;
334 return no_page_table(vma, flags);
335 }
Aneesh Kumar K.V4dc71452017-07-06 15:38:56 -0700336 if (is_hugepd(__hugepd(pud_val(*pud)))) {
337 page = follow_huge_pd(vma, address,
338 __hugepd(pud_val(*pud)), flags,
339 PUD_SHIFT);
340 if (page)
341 return page;
342 return no_page_table(vma, flags);
343 }
Aneesh Kumar K.V080dbb62017-07-06 15:38:44 -0700344 if (pud_devmap(*pud)) {
345 ptl = pud_lock(mm, pud);
Keith Buschdf06b372018-10-26 15:10:28 -0700346 page = follow_devmap_pud(vma, address, pud, flags, &ctx->pgmap);
Aneesh Kumar K.V080dbb62017-07-06 15:38:44 -0700347 spin_unlock(ptl);
348 if (page)
349 return page;
350 }
351 if (unlikely(pud_bad(*pud)))
352 return no_page_table(vma, flags);
353
Keith Buschdf06b372018-10-26 15:10:28 -0700354 return follow_pmd_mask(vma, address, pud, flags, ctx);
Aneesh Kumar K.V080dbb62017-07-06 15:38:44 -0700355}
356
Aneesh Kumar K.V080dbb62017-07-06 15:38:44 -0700357static struct page *follow_p4d_mask(struct vm_area_struct *vma,
358 unsigned long address, pgd_t *pgdp,
Keith Buschdf06b372018-10-26 15:10:28 -0700359 unsigned int flags,
360 struct follow_page_context *ctx)
Aneesh Kumar K.V080dbb62017-07-06 15:38:44 -0700361{
362 p4d_t *p4d;
Aneesh Kumar K.V4dc71452017-07-06 15:38:56 -0700363 struct page *page;
Aneesh Kumar K.V080dbb62017-07-06 15:38:44 -0700364
365 p4d = p4d_offset(pgdp, address);
366 if (p4d_none(*p4d))
367 return no_page_table(vma, flags);
368 BUILD_BUG_ON(p4d_huge(*p4d));
369 if (unlikely(p4d_bad(*p4d)))
370 return no_page_table(vma, flags);
371
Aneesh Kumar K.V4dc71452017-07-06 15:38:56 -0700372 if (is_hugepd(__hugepd(p4d_val(*p4d)))) {
373 page = follow_huge_pd(vma, address,
374 __hugepd(p4d_val(*p4d)), flags,
375 P4D_SHIFT);
376 if (page)
377 return page;
378 return no_page_table(vma, flags);
379 }
Keith Buschdf06b372018-10-26 15:10:28 -0700380 return follow_pud_mask(vma, address, p4d, flags, ctx);
Aneesh Kumar K.V080dbb62017-07-06 15:38:44 -0700381}
382
383/**
384 * follow_page_mask - look up a page descriptor from a user-virtual address
385 * @vma: vm_area_struct mapping @address
386 * @address: virtual address to look up
387 * @flags: flags modifying lookup behaviour
388 * @page_mask: on output, *page_mask is set according to the size of the page
389 *
390 * @flags can have FOLL_ flags set, defined in <linux/mm.h>
391 *
392 * Returns the mapped (struct page *), %NULL if no mapping exists, or
393 * an error pointer if there is a mapping to something not represented
394 * by a page descriptor (see also vm_normal_page()).
395 */
396struct page *follow_page_mask(struct vm_area_struct *vma,
397 unsigned long address, unsigned int flags,
Keith Buschdf06b372018-10-26 15:10:28 -0700398 struct follow_page_context *ctx)
Aneesh Kumar K.V080dbb62017-07-06 15:38:44 -0700399{
400 pgd_t *pgd;
401 struct page *page;
402 struct mm_struct *mm = vma->vm_mm;
403
Keith Buschdf06b372018-10-26 15:10:28 -0700404 ctx->page_mask = 0;
Aneesh Kumar K.V080dbb62017-07-06 15:38:44 -0700405
406 /* make this handle hugepd */
407 page = follow_huge_addr(mm, address, flags & FOLL_WRITE);
408 if (!IS_ERR(page)) {
409 BUG_ON(flags & FOLL_GET);
410 return page;
411 }
412
413 pgd = pgd_offset(mm, address);
414
415 if (pgd_none(*pgd) || unlikely(pgd_bad(*pgd)))
416 return no_page_table(vma, flags);
417
Anshuman Khandualfaaa5b62017-07-06 15:38:50 -0700418 if (pgd_huge(*pgd)) {
419 page = follow_huge_pgd(mm, address, pgd, flags);
420 if (page)
421 return page;
422 return no_page_table(vma, flags);
423 }
Aneesh Kumar K.V4dc71452017-07-06 15:38:56 -0700424 if (is_hugepd(__hugepd(pgd_val(*pgd)))) {
425 page = follow_huge_pd(vma, address,
426 __hugepd(pgd_val(*pgd)), flags,
427 PGDIR_SHIFT);
428 if (page)
429 return page;
430 return no_page_table(vma, flags);
431 }
Anshuman Khandualfaaa5b62017-07-06 15:38:50 -0700432
Keith Buschdf06b372018-10-26 15:10:28 -0700433 return follow_p4d_mask(vma, address, pgd, flags, ctx);
434}
435
436struct page *follow_page(struct vm_area_struct *vma, unsigned long address,
437 unsigned int foll_flags)
438{
439 struct follow_page_context ctx = { NULL };
440 struct page *page;
441
442 page = follow_page_mask(vma, address, foll_flags, &ctx);
443 if (ctx.pgmap)
444 put_dev_pagemap(ctx.pgmap);
445 return page;
Aneesh Kumar K.V080dbb62017-07-06 15:38:44 -0700446}
447
Kirill A. Shutemovf2b495c2014-06-04 16:08:11 -0700448static int get_gate_page(struct mm_struct *mm, unsigned long address,
449 unsigned int gup_flags, struct vm_area_struct **vma,
450 struct page **page)
451{
452 pgd_t *pgd;
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +0300453 p4d_t *p4d;
Kirill A. Shutemovf2b495c2014-06-04 16:08:11 -0700454 pud_t *pud;
455 pmd_t *pmd;
456 pte_t *pte;
457 int ret = -EFAULT;
458
459 /* user gate pages are read-only */
460 if (gup_flags & FOLL_WRITE)
461 return -EFAULT;
462 if (address > TASK_SIZE)
463 pgd = pgd_offset_k(address);
464 else
465 pgd = pgd_offset_gate(mm, address);
466 BUG_ON(pgd_none(*pgd));
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +0300467 p4d = p4d_offset(pgd, address);
468 BUG_ON(p4d_none(*p4d));
469 pud = pud_offset(p4d, address);
Kirill A. Shutemovf2b495c2014-06-04 16:08:11 -0700470 BUG_ON(pud_none(*pud));
471 pmd = pmd_offset(pud, address);
Zi Yan84c3fc42017-09-08 16:11:01 -0700472 if (!pmd_present(*pmd))
Kirill A. Shutemovf2b495c2014-06-04 16:08:11 -0700473 return -EFAULT;
474 VM_BUG_ON(pmd_trans_huge(*pmd));
475 pte = pte_offset_map(pmd, address);
476 if (pte_none(*pte))
477 goto unmap;
478 *vma = get_gate_vma(mm);
479 if (!page)
480 goto out;
481 *page = vm_normal_page(*vma, address, *pte);
482 if (!*page) {
483 if ((gup_flags & FOLL_DUMP) || !is_zero_pfn(pte_pfn(*pte)))
484 goto unmap;
485 *page = pte_page(*pte);
Jérôme Glissedf6ad692017-09-08 16:12:24 -0700486
487 /*
488 * This should never happen (a device public page in the gate
489 * area).
490 */
491 if (is_device_public_page(*page))
492 goto unmap;
Kirill A. Shutemovf2b495c2014-06-04 16:08:11 -0700493 }
494 get_page(*page);
495out:
496 ret = 0;
497unmap:
498 pte_unmap(pte);
499 return ret;
500}
501
Paul Cassella9a95f3c2014-08-06 16:07:24 -0700502/*
503 * mmap_sem must be held on entry. If @nonblocking != NULL and
504 * *@flags does not include FOLL_NOWAIT, the mmap_sem may be released.
505 * If it is, *@nonblocking will be set to 0 and -EBUSY returned.
506 */
Kirill A. Shutemov16744482014-06-04 16:08:12 -0700507static int faultin_page(struct task_struct *tsk, struct vm_area_struct *vma,
508 unsigned long address, unsigned int *flags, int *nonblocking)
509{
Kirill A. Shutemov16744482014-06-04 16:08:12 -0700510 unsigned int fault_flags = 0;
Souptick Joarder2b740302018-08-23 17:01:36 -0700511 vm_fault_t ret;
Kirill A. Shutemov16744482014-06-04 16:08:12 -0700512
Eric B Munsonde60f5f2015-11-05 18:51:36 -0800513 /* mlock all present pages, but do not fault in new pages */
514 if ((*flags & (FOLL_POPULATE | FOLL_MLOCK)) == FOLL_MLOCK)
515 return -ENOENT;
Kirill A. Shutemov16744482014-06-04 16:08:12 -0700516 if (*flags & FOLL_WRITE)
517 fault_flags |= FAULT_FLAG_WRITE;
Dave Hansen1b2ee122016-02-12 13:02:21 -0800518 if (*flags & FOLL_REMOTE)
519 fault_flags |= FAULT_FLAG_REMOTE;
Kirill A. Shutemov16744482014-06-04 16:08:12 -0700520 if (nonblocking)
521 fault_flags |= FAULT_FLAG_ALLOW_RETRY;
522 if (*flags & FOLL_NOWAIT)
523 fault_flags |= FAULT_FLAG_ALLOW_RETRY | FAULT_FLAG_RETRY_NOWAIT;
Andres Lagar-Cavilla234b2392014-09-17 10:51:48 -0700524 if (*flags & FOLL_TRIED) {
525 VM_WARN_ON_ONCE(fault_flags & FAULT_FLAG_ALLOW_RETRY);
526 fault_flags |= FAULT_FLAG_TRIED;
527 }
Kirill A. Shutemov16744482014-06-04 16:08:12 -0700528
Kirill A. Shutemovdcddffd2016-07-26 15:25:18 -0700529 ret = handle_mm_fault(vma, address, fault_flags);
Kirill A. Shutemov16744482014-06-04 16:08:12 -0700530 if (ret & VM_FAULT_ERROR) {
James Morse9a291a72017-06-02 14:46:46 -0700531 int err = vm_fault_to_errno(ret, *flags);
532
533 if (err)
534 return err;
Kirill A. Shutemov16744482014-06-04 16:08:12 -0700535 BUG();
536 }
537
538 if (tsk) {
539 if (ret & VM_FAULT_MAJOR)
540 tsk->maj_flt++;
541 else
542 tsk->min_flt++;
543 }
544
545 if (ret & VM_FAULT_RETRY) {
Andrea Arcangeli96312e62018-03-09 15:51:06 -0800546 if (nonblocking && !(fault_flags & FAULT_FLAG_RETRY_NOWAIT))
Kirill A. Shutemov16744482014-06-04 16:08:12 -0700547 *nonblocking = 0;
548 return -EBUSY;
549 }
550
551 /*
552 * The VM_FAULT_WRITE bit tells us that do_wp_page has broken COW when
553 * necessary, even if maybe_mkwrite decided not to set pte_write. We
554 * can thus safely do subsequent page lookups as if they were reads.
555 * But only do so when looping for pte_write is futile: in some cases
556 * userspace may also be wanting to write to the gotten user page,
557 * which a read fault here might prevent (a readonly page might get
558 * reCOWed by userspace write).
559 */
560 if ((ret & VM_FAULT_WRITE) && !(vma->vm_flags & VM_WRITE))
Mario Leinweber29231172018-04-05 16:24:18 -0700561 *flags |= FOLL_COW;
Kirill A. Shutemov16744482014-06-04 16:08:12 -0700562 return 0;
563}
564
Kirill A. Shutemovfa5bb202014-06-04 16:08:13 -0700565static int check_vma_flags(struct vm_area_struct *vma, unsigned long gup_flags)
566{
567 vm_flags_t vm_flags = vma->vm_flags;
Dave Hansen1b2ee122016-02-12 13:02:21 -0800568 int write = (gup_flags & FOLL_WRITE);
569 int foreign = (gup_flags & FOLL_REMOTE);
Kirill A. Shutemovfa5bb202014-06-04 16:08:13 -0700570
571 if (vm_flags & (VM_IO | VM_PFNMAP))
572 return -EFAULT;
573
Willy Tarreau7f7ccc22018-05-11 08:11:44 +0200574 if (gup_flags & FOLL_ANON && !vma_is_anonymous(vma))
575 return -EFAULT;
576
Dave Hansen1b2ee122016-02-12 13:02:21 -0800577 if (write) {
Kirill A. Shutemovfa5bb202014-06-04 16:08:13 -0700578 if (!(vm_flags & VM_WRITE)) {
579 if (!(gup_flags & FOLL_FORCE))
580 return -EFAULT;
581 /*
582 * We used to let the write,force case do COW in a
583 * VM_MAYWRITE VM_SHARED !VM_WRITE vma, so ptrace could
584 * set a breakpoint in a read-only mapping of an
585 * executable, without corrupting the file (yet only
586 * when that file had been opened for writing!).
587 * Anon pages in shared mappings are surprising: now
588 * just reject it.
589 */
Hugh Dickins46435362016-01-30 18:03:16 -0800590 if (!is_cow_mapping(vm_flags))
Kirill A. Shutemovfa5bb202014-06-04 16:08:13 -0700591 return -EFAULT;
Kirill A. Shutemovfa5bb202014-06-04 16:08:13 -0700592 }
593 } else if (!(vm_flags & VM_READ)) {
594 if (!(gup_flags & FOLL_FORCE))
595 return -EFAULT;
596 /*
597 * Is there actually any vma we can reach here which does not
598 * have VM_MAYREAD set?
599 */
600 if (!(vm_flags & VM_MAYREAD))
601 return -EFAULT;
602 }
Dave Hansend61172b2016-02-12 13:02:24 -0800603 /*
604 * gups are always data accesses, not instruction
605 * fetches, so execute=false here
606 */
607 if (!arch_vma_access_permitted(vma, write, false, foreign))
Dave Hansen33a709b2016-02-12 13:02:19 -0800608 return -EFAULT;
Kirill A. Shutemovfa5bb202014-06-04 16:08:13 -0700609 return 0;
610}
611
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700612/**
613 * __get_user_pages() - pin user pages in memory
614 * @tsk: task_struct of target task
615 * @mm: mm_struct of target mm
616 * @start: starting user address
617 * @nr_pages: number of pages from start to pin
618 * @gup_flags: flags modifying pin behaviour
619 * @pages: array that receives pointers to the pages pinned.
620 * Should be at least nr_pages long. Or NULL, if caller
621 * only intends to ensure the pages are faulted in.
622 * @vmas: array of pointers to vmas corresponding to each page.
623 * Or NULL if the caller does not require them.
624 * @nonblocking: whether waiting for disk IO or mmap_sem contention
625 *
626 * Returns number of pages pinned. This may be fewer than the number
627 * requested. If nr_pages is 0 or negative, returns 0. If no pages
628 * were pinned, returns -errno. Each page returned must be released
629 * with a put_page() call when it is finished with. vmas will only
630 * remain valid while mmap_sem is held.
631 *
Paul Cassella9a95f3c2014-08-06 16:07:24 -0700632 * Must be called with mmap_sem held. It may be released. See below.
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700633 *
634 * __get_user_pages walks a process's page tables and takes a reference to
635 * each struct page that each user address corresponds to at a given
636 * instant. That is, it takes the page that would be accessed if a user
637 * thread accesses the given user virtual address at that instant.
638 *
639 * This does not guarantee that the page exists in the user mappings when
640 * __get_user_pages returns, and there may even be a completely different
641 * page there in some cases (eg. if mmapped pagecache has been invalidated
642 * and subsequently re faulted). However it does guarantee that the page
643 * won't be freed completely. And mostly callers simply care that the page
644 * contains data that was valid *at some point in time*. Typically, an IO
645 * or similar operation cannot guarantee anything stronger anyway because
646 * locks can't be held over the syscall boundary.
647 *
648 * If @gup_flags & FOLL_WRITE == 0, the page must not be written to. If
649 * the page is written to, set_page_dirty (or set_page_dirty_lock, as
650 * appropriate) must be called after the page is finished with, and
651 * before put_page is called.
652 *
653 * If @nonblocking != NULL, __get_user_pages will not wait for disk IO
654 * or mmap_sem contention, and if waiting is needed to pin all pages,
Paul Cassella9a95f3c2014-08-06 16:07:24 -0700655 * *@nonblocking will be set to 0. Further, if @gup_flags does not
656 * include FOLL_NOWAIT, the mmap_sem will be released via up_read() in
657 * this case.
658 *
659 * A caller using such a combination of @nonblocking and @gup_flags
660 * must therefore hold the mmap_sem for reading only, and recognize
661 * when it's been released. Otherwise, it must be held for either
662 * reading or writing and will not be released.
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700663 *
664 * In most cases, get_user_pages or get_user_pages_fast should be used
665 * instead of __get_user_pages. __get_user_pages should be used only if
666 * you need some special @gup_flags.
667 */
Lorenzo Stoakes0d731752016-10-24 10:57:25 +0100668static long __get_user_pages(struct task_struct *tsk, struct mm_struct *mm,
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700669 unsigned long start, unsigned long nr_pages,
670 unsigned int gup_flags, struct page **pages,
671 struct vm_area_struct **vmas, int *nonblocking)
672{
Keith Buschdf06b372018-10-26 15:10:28 -0700673 long ret = 0, i = 0;
Kirill A. Shutemovfa5bb202014-06-04 16:08:13 -0700674 struct vm_area_struct *vma = NULL;
Keith Buschdf06b372018-10-26 15:10:28 -0700675 struct follow_page_context ctx = { NULL };
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700676
677 if (!nr_pages)
678 return 0;
679
680 VM_BUG_ON(!!pages != !!(gup_flags & FOLL_GET));
681
682 /*
683 * If FOLL_FORCE is set then do not force a full fault as the hinting
684 * fault information is unrelated to the reference behaviour of a task
685 * using the address space
686 */
687 if (!(gup_flags & FOLL_FORCE))
688 gup_flags |= FOLL_NUMA;
689
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700690 do {
Kirill A. Shutemovfa5bb202014-06-04 16:08:13 -0700691 struct page *page;
692 unsigned int foll_flags = gup_flags;
693 unsigned int page_increm;
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700694
Kirill A. Shutemovfa5bb202014-06-04 16:08:13 -0700695 /* first iteration or cross vma bound */
696 if (!vma || start >= vma->vm_end) {
697 vma = find_extend_vma(mm, start);
698 if (!vma && in_gate_area(mm, start)) {
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700699 int ret;
Kirill A. Shutemovfa5bb202014-06-04 16:08:13 -0700700 ret = get_gate_page(mm, start & PAGE_MASK,
701 gup_flags, &vma,
702 pages ? &pages[i] : NULL);
703 if (ret)
704 return i ? : ret;
Keith Buschdf06b372018-10-26 15:10:28 -0700705 ctx.page_mask = 0;
Kirill A. Shutemovfa5bb202014-06-04 16:08:13 -0700706 goto next_page;
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700707 }
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700708
Keith Buschdf06b372018-10-26 15:10:28 -0700709 if (!vma || check_vma_flags(vma, gup_flags)) {
710 ret = -EFAULT;
711 goto out;
712 }
Kirill A. Shutemovfa5bb202014-06-04 16:08:13 -0700713 if (is_vm_hugetlb_page(vma)) {
714 i = follow_hugetlb_page(mm, vma, pages, vmas,
715 &start, &nr_pages, i,
Andrea Arcangeli87ffc112017-02-22 15:43:13 -0800716 gup_flags, nonblocking);
Kirill A. Shutemovfa5bb202014-06-04 16:08:13 -0700717 continue;
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700718 }
Kirill A. Shutemovfa5bb202014-06-04 16:08:13 -0700719 }
720retry:
721 /*
722 * If we have a pending SIGKILL, don't keep faulting pages and
723 * potentially allocating memory.
724 */
Keith Buschdf06b372018-10-26 15:10:28 -0700725 if (unlikely(fatal_signal_pending(current))) {
726 ret = -ERESTARTSYS;
727 goto out;
728 }
Kirill A. Shutemovfa5bb202014-06-04 16:08:13 -0700729 cond_resched();
Keith Buschdf06b372018-10-26 15:10:28 -0700730
731 page = follow_page_mask(vma, start, foll_flags, &ctx);
Kirill A. Shutemovfa5bb202014-06-04 16:08:13 -0700732 if (!page) {
Kirill A. Shutemovfa5bb202014-06-04 16:08:13 -0700733 ret = faultin_page(tsk, vma, start, &foll_flags,
734 nonblocking);
735 switch (ret) {
736 case 0:
737 goto retry;
Keith Buschdf06b372018-10-26 15:10:28 -0700738 case -EBUSY:
739 ret = 0;
740 /* FALLTHRU */
Kirill A. Shutemovfa5bb202014-06-04 16:08:13 -0700741 case -EFAULT:
742 case -ENOMEM:
743 case -EHWPOISON:
Keith Buschdf06b372018-10-26 15:10:28 -0700744 goto out;
Kirill A. Shutemovfa5bb202014-06-04 16:08:13 -0700745 case -ENOENT:
746 goto next_page;
747 }
748 BUG();
Kirill A. Shutemov1027e442015-09-04 15:47:55 -0700749 } else if (PTR_ERR(page) == -EEXIST) {
750 /*
751 * Proper page table entry exists, but no corresponding
752 * struct page.
753 */
754 goto next_page;
755 } else if (IS_ERR(page)) {
Keith Buschdf06b372018-10-26 15:10:28 -0700756 ret = PTR_ERR(page);
757 goto out;
Kirill A. Shutemov1027e442015-09-04 15:47:55 -0700758 }
Kirill A. Shutemovfa5bb202014-06-04 16:08:13 -0700759 if (pages) {
760 pages[i] = page;
761 flush_anon_page(vma, page, start);
762 flush_dcache_page(page);
Keith Buschdf06b372018-10-26 15:10:28 -0700763 ctx.page_mask = 0;
Kirill A. Shutemovfa5bb202014-06-04 16:08:13 -0700764 }
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700765next_page:
Kirill A. Shutemovfa5bb202014-06-04 16:08:13 -0700766 if (vmas) {
767 vmas[i] = vma;
Keith Buschdf06b372018-10-26 15:10:28 -0700768 ctx.page_mask = 0;
Kirill A. Shutemovfa5bb202014-06-04 16:08:13 -0700769 }
Keith Buschdf06b372018-10-26 15:10:28 -0700770 page_increm = 1 + (~(start >> PAGE_SHIFT) & ctx.page_mask);
Kirill A. Shutemovfa5bb202014-06-04 16:08:13 -0700771 if (page_increm > nr_pages)
772 page_increm = nr_pages;
773 i += page_increm;
774 start += page_increm * PAGE_SIZE;
775 nr_pages -= page_increm;
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700776 } while (nr_pages);
Keith Buschdf06b372018-10-26 15:10:28 -0700777out:
778 if (ctx.pgmap)
779 put_dev_pagemap(ctx.pgmap);
780 return i ? i : ret;
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700781}
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700782
Tobias Klauser771ab432016-12-12 16:41:53 -0800783static bool vma_permits_fault(struct vm_area_struct *vma,
784 unsigned int fault_flags)
Dave Hansend4925e02016-02-12 13:02:16 -0800785{
Dave Hansen1b2ee122016-02-12 13:02:21 -0800786 bool write = !!(fault_flags & FAULT_FLAG_WRITE);
787 bool foreign = !!(fault_flags & FAULT_FLAG_REMOTE);
Dave Hansen33a709b2016-02-12 13:02:19 -0800788 vm_flags_t vm_flags = write ? VM_WRITE : VM_READ;
Dave Hansend4925e02016-02-12 13:02:16 -0800789
790 if (!(vm_flags & vma->vm_flags))
791 return false;
792
Dave Hansen33a709b2016-02-12 13:02:19 -0800793 /*
794 * The architecture might have a hardware protection
Dave Hansen1b2ee122016-02-12 13:02:21 -0800795 * mechanism other than read/write that can deny access.
Dave Hansend61172b2016-02-12 13:02:24 -0800796 *
797 * gup always represents data access, not instruction
798 * fetches, so execute=false here:
Dave Hansen33a709b2016-02-12 13:02:19 -0800799 */
Dave Hansend61172b2016-02-12 13:02:24 -0800800 if (!arch_vma_access_permitted(vma, write, false, foreign))
Dave Hansen33a709b2016-02-12 13:02:19 -0800801 return false;
802
Dave Hansend4925e02016-02-12 13:02:16 -0800803 return true;
804}
805
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700806/*
807 * fixup_user_fault() - manually resolve a user page fault
808 * @tsk: the task_struct to use for page fault accounting, or
809 * NULL if faults are not to be recorded.
810 * @mm: mm_struct of target mm
811 * @address: user address
812 * @fault_flags:flags to pass down to handle_mm_fault()
Dominik Dingel4a9e1cd2016-01-15 16:57:04 -0800813 * @unlocked: did we unlock the mmap_sem while retrying, maybe NULL if caller
814 * does not allow retry
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700815 *
816 * This is meant to be called in the specific scenario where for locking reasons
817 * we try to access user memory in atomic context (within a pagefault_disable()
818 * section), this returns -EFAULT, and we want to resolve the user fault before
819 * trying again.
820 *
821 * Typically this is meant to be used by the futex code.
822 *
823 * The main difference with get_user_pages() is that this function will
824 * unconditionally call handle_mm_fault() which will in turn perform all the
825 * necessary SW fixup of the dirty and young bits in the PTE, while
Dominik Dingel4a9e1cd2016-01-15 16:57:04 -0800826 * get_user_pages() only guarantees to update these in the struct page.
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700827 *
828 * This is important for some architectures where those bits also gate the
829 * access permission to the page because they are maintained in software. On
830 * such architectures, gup() will not be enough to make a subsequent access
831 * succeed.
832 *
Dominik Dingel4a9e1cd2016-01-15 16:57:04 -0800833 * This function will not return with an unlocked mmap_sem. So it has not the
834 * same semantics wrt the @mm->mmap_sem as does filemap_fault().
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700835 */
836int fixup_user_fault(struct task_struct *tsk, struct mm_struct *mm,
Dominik Dingel4a9e1cd2016-01-15 16:57:04 -0800837 unsigned long address, unsigned int fault_flags,
838 bool *unlocked)
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700839{
840 struct vm_area_struct *vma;
Souptick Joarder2b740302018-08-23 17:01:36 -0700841 vm_fault_t ret, major = 0;
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700842
Dominik Dingel4a9e1cd2016-01-15 16:57:04 -0800843 if (unlocked)
844 fault_flags |= FAULT_FLAG_ALLOW_RETRY;
845
846retry:
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700847 vma = find_extend_vma(mm, address);
848 if (!vma || address < vma->vm_start)
849 return -EFAULT;
850
Dave Hansend4925e02016-02-12 13:02:16 -0800851 if (!vma_permits_fault(vma, fault_flags))
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700852 return -EFAULT;
853
Kirill A. Shutemovdcddffd2016-07-26 15:25:18 -0700854 ret = handle_mm_fault(vma, address, fault_flags);
Dominik Dingel4a9e1cd2016-01-15 16:57:04 -0800855 major |= ret & VM_FAULT_MAJOR;
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700856 if (ret & VM_FAULT_ERROR) {
James Morse9a291a72017-06-02 14:46:46 -0700857 int err = vm_fault_to_errno(ret, 0);
858
859 if (err)
860 return err;
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700861 BUG();
862 }
Dominik Dingel4a9e1cd2016-01-15 16:57:04 -0800863
864 if (ret & VM_FAULT_RETRY) {
865 down_read(&mm->mmap_sem);
866 if (!(fault_flags & FAULT_FLAG_TRIED)) {
867 *unlocked = true;
868 fault_flags &= ~FAULT_FLAG_ALLOW_RETRY;
869 fault_flags |= FAULT_FLAG_TRIED;
870 goto retry;
871 }
872 }
873
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700874 if (tsk) {
Dominik Dingel4a9e1cd2016-01-15 16:57:04 -0800875 if (major)
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700876 tsk->maj_flt++;
877 else
878 tsk->min_flt++;
879 }
880 return 0;
881}
Paolo Bonziniadd6a0c2016-06-07 17:51:18 +0200882EXPORT_SYMBOL_GPL(fixup_user_fault);
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700883
Andrea Arcangelif0818f42015-02-11 15:27:17 -0800884static __always_inline long __get_user_pages_locked(struct task_struct *tsk,
885 struct mm_struct *mm,
886 unsigned long start,
887 unsigned long nr_pages,
Andrea Arcangelif0818f42015-02-11 15:27:17 -0800888 struct page **pages,
889 struct vm_area_struct **vmas,
Al Viroe7167122017-11-19 11:32:05 -0500890 int *locked,
Andrea Arcangeli0fd71a52015-02-11 15:27:20 -0800891 unsigned int flags)
Andrea Arcangelif0818f42015-02-11 15:27:17 -0800892{
Andrea Arcangelif0818f42015-02-11 15:27:17 -0800893 long ret, pages_done;
894 bool lock_dropped;
895
896 if (locked) {
897 /* if VM_FAULT_RETRY can be returned, vmas become invalid */
898 BUG_ON(vmas);
899 /* check caller initialized locked */
900 BUG_ON(*locked != 1);
901 }
902
903 if (pages)
904 flags |= FOLL_GET;
Andrea Arcangelif0818f42015-02-11 15:27:17 -0800905
906 pages_done = 0;
907 lock_dropped = false;
908 for (;;) {
909 ret = __get_user_pages(tsk, mm, start, nr_pages, flags, pages,
910 vmas, locked);
911 if (!locked)
912 /* VM_FAULT_RETRY couldn't trigger, bypass */
913 return ret;
914
915 /* VM_FAULT_RETRY cannot return errors */
916 if (!*locked) {
917 BUG_ON(ret < 0);
918 BUG_ON(ret >= nr_pages);
919 }
920
921 if (!pages)
922 /* If it's a prefault don't insist harder */
923 return ret;
924
925 if (ret > 0) {
926 nr_pages -= ret;
927 pages_done += ret;
928 if (!nr_pages)
929 break;
930 }
931 if (*locked) {
Andrea Arcangeli96312e62018-03-09 15:51:06 -0800932 /*
933 * VM_FAULT_RETRY didn't trigger or it was a
934 * FOLL_NOWAIT.
935 */
Andrea Arcangelif0818f42015-02-11 15:27:17 -0800936 if (!pages_done)
937 pages_done = ret;
938 break;
939 }
940 /* VM_FAULT_RETRY triggered, so seek to the faulting offset */
941 pages += ret;
942 start += ret << PAGE_SHIFT;
943
944 /*
945 * Repeat on the address that fired VM_FAULT_RETRY
946 * without FAULT_FLAG_ALLOW_RETRY but with
947 * FAULT_FLAG_TRIED.
948 */
949 *locked = 1;
950 lock_dropped = true;
951 down_read(&mm->mmap_sem);
952 ret = __get_user_pages(tsk, mm, start, 1, flags | FOLL_TRIED,
953 pages, NULL, NULL);
954 if (ret != 1) {
955 BUG_ON(ret > 1);
956 if (!pages_done)
957 pages_done = ret;
958 break;
959 }
960 nr_pages--;
961 pages_done++;
962 if (!nr_pages)
963 break;
964 pages++;
965 start += PAGE_SIZE;
966 }
Al Viroe7167122017-11-19 11:32:05 -0500967 if (lock_dropped && *locked) {
Andrea Arcangelif0818f42015-02-11 15:27:17 -0800968 /*
969 * We must let the caller know we temporarily dropped the lock
970 * and so the critical section protected by it was lost.
971 */
972 up_read(&mm->mmap_sem);
973 *locked = 0;
974 }
975 return pages_done;
976}
977
978/*
979 * We can leverage the VM_FAULT_RETRY functionality in the page fault
980 * paths better by using either get_user_pages_locked() or
981 * get_user_pages_unlocked().
982 *
983 * get_user_pages_locked() is suitable to replace the form:
984 *
985 * down_read(&mm->mmap_sem);
986 * do_something()
987 * get_user_pages(tsk, mm, ..., pages, NULL);
988 * up_read(&mm->mmap_sem);
989 *
990 * to:
991 *
992 * int locked = 1;
993 * down_read(&mm->mmap_sem);
994 * do_something()
995 * get_user_pages_locked(tsk, mm, ..., pages, &locked);
996 * if (locked)
997 * up_read(&mm->mmap_sem);
998 */
Ingo Molnarc12d2da2016-04-04 10:24:58 +0200999long get_user_pages_locked(unsigned long start, unsigned long nr_pages,
Lorenzo Stoakes3b913172016-10-13 01:20:14 +01001000 unsigned int gup_flags, struct page **pages,
Andrea Arcangelif0818f42015-02-11 15:27:17 -08001001 int *locked)
1002{
Dave Hansencde70142016-02-12 13:01:55 -08001003 return __get_user_pages_locked(current, current->mm, start, nr_pages,
Al Viroe7167122017-11-19 11:32:05 -05001004 pages, NULL, locked,
Lorenzo Stoakes3b913172016-10-13 01:20:14 +01001005 gup_flags | FOLL_TOUCH);
Andrea Arcangelif0818f42015-02-11 15:27:17 -08001006}
Ingo Molnarc12d2da2016-04-04 10:24:58 +02001007EXPORT_SYMBOL(get_user_pages_locked);
Andrea Arcangelif0818f42015-02-11 15:27:17 -08001008
1009/*
1010 * get_user_pages_unlocked() is suitable to replace the form:
1011 *
1012 * down_read(&mm->mmap_sem);
1013 * get_user_pages(tsk, mm, ..., pages, NULL);
1014 * up_read(&mm->mmap_sem);
1015 *
1016 * with:
1017 *
1018 * get_user_pages_unlocked(tsk, mm, ..., pages);
1019 *
1020 * It is functionally equivalent to get_user_pages_fast so
Lorenzo Stoakes80a79512016-12-12 16:42:46 -08001021 * get_user_pages_fast should be used instead if specific gup_flags
1022 * (e.g. FOLL_FORCE) are not required.
Andrea Arcangelif0818f42015-02-11 15:27:17 -08001023 */
Ingo Molnarc12d2da2016-04-04 10:24:58 +02001024long get_user_pages_unlocked(unsigned long start, unsigned long nr_pages,
Lorenzo Stoakesc1641542016-10-13 01:20:13 +01001025 struct page **pages, unsigned int gup_flags)
Andrea Arcangelif0818f42015-02-11 15:27:17 -08001026{
Al Viroc803c9c2017-11-18 14:17:46 -05001027 struct mm_struct *mm = current->mm;
1028 int locked = 1;
1029 long ret;
1030
1031 down_read(&mm->mmap_sem);
1032 ret = __get_user_pages_locked(current, mm, start, nr_pages, pages, NULL,
Al Viroe7167122017-11-19 11:32:05 -05001033 &locked, gup_flags | FOLL_TOUCH);
Al Viroc803c9c2017-11-18 14:17:46 -05001034 if (locked)
1035 up_read(&mm->mmap_sem);
1036 return ret;
Andrea Arcangelif0818f42015-02-11 15:27:17 -08001037}
Ingo Molnarc12d2da2016-04-04 10:24:58 +02001038EXPORT_SYMBOL(get_user_pages_unlocked);
Andrea Arcangelif0818f42015-02-11 15:27:17 -08001039
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -07001040/*
Dave Hansen1e987792016-02-12 13:01:54 -08001041 * get_user_pages_remote() - pin user pages in memory
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -07001042 * @tsk: the task_struct to use for page fault accounting, or
1043 * NULL if faults are not to be recorded.
1044 * @mm: mm_struct of target mm
1045 * @start: starting user address
1046 * @nr_pages: number of pages from start to pin
Lorenzo Stoakes9beae1e2016-10-13 01:20:17 +01001047 * @gup_flags: flags modifying lookup behaviour
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -07001048 * @pages: array that receives pointers to the pages pinned.
1049 * Should be at least nr_pages long. Or NULL, if caller
1050 * only intends to ensure the pages are faulted in.
1051 * @vmas: array of pointers to vmas corresponding to each page.
1052 * Or NULL if the caller does not require them.
Lorenzo Stoakes5b56d492016-12-14 15:06:52 -08001053 * @locked: pointer to lock flag indicating whether lock is held and
1054 * subsequently whether VM_FAULT_RETRY functionality can be
1055 * utilised. Lock must initially be held.
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -07001056 *
1057 * Returns number of pages pinned. This may be fewer than the number
1058 * requested. If nr_pages is 0 or negative, returns 0. If no pages
1059 * were pinned, returns -errno. Each page returned must be released
1060 * with a put_page() call when it is finished with. vmas will only
1061 * remain valid while mmap_sem is held.
1062 *
1063 * Must be called with mmap_sem held for read or write.
1064 *
1065 * get_user_pages walks a process's page tables and takes a reference to
1066 * each struct page that each user address corresponds to at a given
1067 * instant. That is, it takes the page that would be accessed if a user
1068 * thread accesses the given user virtual address at that instant.
1069 *
1070 * This does not guarantee that the page exists in the user mappings when
1071 * get_user_pages returns, and there may even be a completely different
1072 * page there in some cases (eg. if mmapped pagecache has been invalidated
1073 * and subsequently re faulted). However it does guarantee that the page
1074 * won't be freed completely. And mostly callers simply care that the page
1075 * contains data that was valid *at some point in time*. Typically, an IO
1076 * or similar operation cannot guarantee anything stronger anyway because
1077 * locks can't be held over the syscall boundary.
1078 *
Lorenzo Stoakes9beae1e2016-10-13 01:20:17 +01001079 * If gup_flags & FOLL_WRITE == 0, the page must not be written to. If the page
1080 * is written to, set_page_dirty (or set_page_dirty_lock, as appropriate) must
1081 * be called after the page is finished with, and before put_page is called.
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -07001082 *
1083 * get_user_pages is typically used for fewer-copy IO operations, to get a
1084 * handle on the memory by some means other than accesses via the user virtual
1085 * addresses. The pages may be submitted for DMA to devices or accessed via
1086 * their kernel linear mapping (via the kmap APIs). Care should be taken to
1087 * use the correct cache flushing APIs.
1088 *
1089 * See also get_user_pages_fast, for performance critical applications.
Andrea Arcangelif0818f42015-02-11 15:27:17 -08001090 *
1091 * get_user_pages should be phased out in favor of
1092 * get_user_pages_locked|unlocked or get_user_pages_fast. Nothing
1093 * should use get_user_pages because it cannot pass
1094 * FAULT_FLAG_ALLOW_RETRY to handle_mm_fault.
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -07001095 */
Dave Hansen1e987792016-02-12 13:01:54 -08001096long get_user_pages_remote(struct task_struct *tsk, struct mm_struct *mm,
1097 unsigned long start, unsigned long nr_pages,
Lorenzo Stoakes9beae1e2016-10-13 01:20:17 +01001098 unsigned int gup_flags, struct page **pages,
Lorenzo Stoakes5b56d492016-12-14 15:06:52 -08001099 struct vm_area_struct **vmas, int *locked)
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -07001100{
Lorenzo Stoakes859110d2016-10-13 01:20:11 +01001101 return __get_user_pages_locked(tsk, mm, start, nr_pages, pages, vmas,
Al Viroe7167122017-11-19 11:32:05 -05001102 locked,
Lorenzo Stoakes9beae1e2016-10-13 01:20:17 +01001103 gup_flags | FOLL_TOUCH | FOLL_REMOTE);
Dave Hansen1e987792016-02-12 13:01:54 -08001104}
1105EXPORT_SYMBOL(get_user_pages_remote);
1106
1107/*
Dave Hansend4edcf02016-02-12 13:01:56 -08001108 * This is the same as get_user_pages_remote(), just with a
1109 * less-flexible calling convention where we assume that the task
Lorenzo Stoakes5b56d492016-12-14 15:06:52 -08001110 * and mm being operated on are the current task's and don't allow
1111 * passing of a locked parameter. We also obviously don't pass
1112 * FOLL_REMOTE in here.
Dave Hansen1e987792016-02-12 13:01:54 -08001113 */
Ingo Molnarc12d2da2016-04-04 10:24:58 +02001114long get_user_pages(unsigned long start, unsigned long nr_pages,
Lorenzo Stoakes768ae302016-10-13 01:20:16 +01001115 unsigned int gup_flags, struct page **pages,
Dave Hansen1e987792016-02-12 13:01:54 -08001116 struct vm_area_struct **vmas)
1117{
Dave Hansencde70142016-02-12 13:01:55 -08001118 return __get_user_pages_locked(current, current->mm, start, nr_pages,
Al Viroe7167122017-11-19 11:32:05 -05001119 pages, vmas, NULL,
Lorenzo Stoakes768ae302016-10-13 01:20:16 +01001120 gup_flags | FOLL_TOUCH);
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -07001121}
Ingo Molnarc12d2da2016-04-04 10:24:58 +02001122EXPORT_SYMBOL(get_user_pages);
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -07001123
Dan Williams2bb6d282017-11-29 16:10:35 -08001124#ifdef CONFIG_FS_DAX
1125/*
1126 * This is the same as get_user_pages() in that it assumes we are
1127 * operating on the current task's mm, but it goes further to validate
1128 * that the vmas associated with the address range are suitable for
1129 * longterm elevated page reference counts. For example, filesystem-dax
1130 * mappings are subject to the lifetime enforced by the filesystem and
1131 * we need guarantees that longterm users like RDMA and V4L2 only
1132 * establish mappings that have a kernel enforced revocation mechanism.
1133 *
1134 * "longterm" == userspace controlled elevated page count lifetime.
1135 * Contrast this to iov_iter_get_pages() usages which are transient.
1136 */
1137long get_user_pages_longterm(unsigned long start, unsigned long nr_pages,
1138 unsigned int gup_flags, struct page **pages,
1139 struct vm_area_struct **vmas_arg)
1140{
1141 struct vm_area_struct **vmas = vmas_arg;
1142 struct vm_area_struct *vma_prev = NULL;
1143 long rc, i;
1144
1145 if (!pages)
1146 return -EINVAL;
1147
1148 if (!vmas) {
1149 vmas = kcalloc(nr_pages, sizeof(struct vm_area_struct *),
1150 GFP_KERNEL);
1151 if (!vmas)
1152 return -ENOMEM;
1153 }
1154
1155 rc = get_user_pages(start, nr_pages, gup_flags, pages, vmas);
1156
1157 for (i = 0; i < rc; i++) {
1158 struct vm_area_struct *vma = vmas[i];
1159
1160 if (vma == vma_prev)
1161 continue;
1162
1163 vma_prev = vma;
1164
1165 if (vma_is_fsdax(vma))
1166 break;
1167 }
1168
1169 /*
1170 * Either get_user_pages() failed, or the vma validation
1171 * succeeded, in either case we don't need to put_page() before
1172 * returning.
1173 */
1174 if (i >= rc)
1175 goto out;
1176
1177 for (i = 0; i < rc; i++)
1178 put_page(pages[i]);
1179 rc = -EOPNOTSUPP;
1180out:
1181 if (vmas != vmas_arg)
1182 kfree(vmas);
1183 return rc;
1184}
1185EXPORT_SYMBOL(get_user_pages_longterm);
1186#endif /* CONFIG_FS_DAX */
1187
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -07001188/**
Kirill A. Shutemovacc3c8d2015-04-14 15:44:45 -07001189 * populate_vma_page_range() - populate a range of pages in the vma.
1190 * @vma: target vma
1191 * @start: start address
1192 * @end: end address
1193 * @nonblocking:
1194 *
1195 * This takes care of mlocking the pages too if VM_LOCKED is set.
1196 *
1197 * return 0 on success, negative error code on error.
1198 *
1199 * vma->vm_mm->mmap_sem must be held.
1200 *
1201 * If @nonblocking is NULL, it may be held for read or write and will
1202 * be unperturbed.
1203 *
1204 * If @nonblocking is non-NULL, it must held for read only and may be
1205 * released. If it's released, *@nonblocking will be set to 0.
1206 */
1207long populate_vma_page_range(struct vm_area_struct *vma,
1208 unsigned long start, unsigned long end, int *nonblocking)
1209{
1210 struct mm_struct *mm = vma->vm_mm;
1211 unsigned long nr_pages = (end - start) / PAGE_SIZE;
1212 int gup_flags;
1213
1214 VM_BUG_ON(start & ~PAGE_MASK);
1215 VM_BUG_ON(end & ~PAGE_MASK);
1216 VM_BUG_ON_VMA(start < vma->vm_start, vma);
1217 VM_BUG_ON_VMA(end > vma->vm_end, vma);
1218 VM_BUG_ON_MM(!rwsem_is_locked(&mm->mmap_sem), mm);
1219
Eric B Munsonde60f5f2015-11-05 18:51:36 -08001220 gup_flags = FOLL_TOUCH | FOLL_POPULATE | FOLL_MLOCK;
1221 if (vma->vm_flags & VM_LOCKONFAULT)
1222 gup_flags &= ~FOLL_POPULATE;
Kirill A. Shutemovacc3c8d2015-04-14 15:44:45 -07001223 /*
1224 * We want to touch writable mappings with a write fault in order
1225 * to break COW, except for shared mappings because these don't COW
1226 * and we would not want to dirty them for nothing.
1227 */
1228 if ((vma->vm_flags & (VM_WRITE | VM_SHARED)) == VM_WRITE)
1229 gup_flags |= FOLL_WRITE;
1230
1231 /*
1232 * We want mlock to succeed for regions that have any permissions
1233 * other than PROT_NONE.
1234 */
1235 if (vma->vm_flags & (VM_READ | VM_WRITE | VM_EXEC))
1236 gup_flags |= FOLL_FORCE;
1237
1238 /*
1239 * We made sure addr is within a VMA, so the following will
1240 * not result in a stack expansion that recurses back here.
1241 */
1242 return __get_user_pages(current, mm, start, nr_pages, gup_flags,
1243 NULL, NULL, nonblocking);
1244}
1245
1246/*
1247 * __mm_populate - populate and/or mlock pages within a range of address space.
1248 *
1249 * This is used to implement mlock() and the MAP_POPULATE / MAP_LOCKED mmap
1250 * flags. VMAs must be already marked with the desired vm_flags, and
1251 * mmap_sem must not be held.
1252 */
1253int __mm_populate(unsigned long start, unsigned long len, int ignore_errors)
1254{
1255 struct mm_struct *mm = current->mm;
1256 unsigned long end, nstart, nend;
1257 struct vm_area_struct *vma = NULL;
1258 int locked = 0;
1259 long ret = 0;
1260
Kirill A. Shutemovacc3c8d2015-04-14 15:44:45 -07001261 end = start + len;
1262
1263 for (nstart = start; nstart < end; nstart = nend) {
1264 /*
1265 * We want to fault in pages for [nstart; end) address range.
1266 * Find first corresponding VMA.
1267 */
1268 if (!locked) {
1269 locked = 1;
1270 down_read(&mm->mmap_sem);
1271 vma = find_vma(mm, nstart);
1272 } else if (nstart >= vma->vm_end)
1273 vma = vma->vm_next;
1274 if (!vma || vma->vm_start >= end)
1275 break;
1276 /*
1277 * Set [nstart; nend) to intersection of desired address
1278 * range with the first VMA. Also, skip undesirable VMA types.
1279 */
1280 nend = min(end, vma->vm_end);
1281 if (vma->vm_flags & (VM_IO | VM_PFNMAP))
1282 continue;
1283 if (nstart < vma->vm_start)
1284 nstart = vma->vm_start;
1285 /*
1286 * Now fault in a range of pages. populate_vma_page_range()
1287 * double checks the vma flags, so that it won't mlock pages
1288 * if the vma was already munlocked.
1289 */
1290 ret = populate_vma_page_range(vma, nstart, nend, &locked);
1291 if (ret < 0) {
1292 if (ignore_errors) {
1293 ret = 0;
1294 continue; /* continue at next VMA */
1295 }
1296 break;
1297 }
1298 nend = nstart + ret * PAGE_SIZE;
1299 ret = 0;
1300 }
1301 if (locked)
1302 up_read(&mm->mmap_sem);
1303 return ret; /* 0 or negative error code */
1304}
1305
1306/**
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -07001307 * get_dump_page() - pin user page in memory while writing it to core dump
1308 * @addr: user address
1309 *
1310 * Returns struct page pointer of user page pinned for dump,
Kirill A. Shutemovea1754a2016-04-01 15:29:48 +03001311 * to be freed afterwards by put_page().
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -07001312 *
1313 * Returns NULL on any kind of failure - a hole must then be inserted into
1314 * the corefile, to preserve alignment with its headers; and also returns
1315 * NULL wherever the ZERO_PAGE, or an anonymous pte_none, has been found -
1316 * allowing a hole to be left in the corefile to save diskspace.
1317 *
1318 * Called without mmap_sem, but after all other threads have been killed.
1319 */
1320#ifdef CONFIG_ELF_CORE
1321struct page *get_dump_page(unsigned long addr)
1322{
1323 struct vm_area_struct *vma;
1324 struct page *page;
1325
1326 if (__get_user_pages(current, current->mm, addr, 1,
1327 FOLL_FORCE | FOLL_DUMP | FOLL_GET, &page, &vma,
1328 NULL) < 1)
1329 return NULL;
1330 flush_cache_page(vma, addr, page_to_pfn(page));
1331 return page;
1332}
1333#endif /* CONFIG_ELF_CORE */
Steve Capper2667f502014-10-09 15:29:14 -07001334
1335/*
Kirill A. Shutemove5855132017-06-06 14:31:20 +03001336 * Generic Fast GUP
Steve Capper2667f502014-10-09 15:29:14 -07001337 *
1338 * get_user_pages_fast attempts to pin user pages by walking the page
1339 * tables directly and avoids taking locks. Thus the walker needs to be
1340 * protected from page table pages being freed from under it, and should
1341 * block any THP splits.
1342 *
1343 * One way to achieve this is to have the walker disable interrupts, and
1344 * rely on IPIs from the TLB flushing code blocking before the page table
1345 * pages are freed. This is unsuitable for architectures that do not need
1346 * to broadcast an IPI when invalidating TLBs.
1347 *
1348 * Another way to achieve this is to batch up page table containing pages
1349 * belonging to more than one mm_user, then rcu_sched a callback to free those
1350 * pages. Disabling interrupts will allow the fast_gup walker to both block
1351 * the rcu_sched callback, and an IPI that we broadcast for splitting THPs
1352 * (which is a relatively rare event). The code below adopts this strategy.
1353 *
1354 * Before activating this code, please be aware that the following assumptions
1355 * are currently made:
1356 *
Kirill A. Shutemove5855132017-06-06 14:31:20 +03001357 * *) Either HAVE_RCU_TABLE_FREE is enabled, and tlb_remove_table() is used to
1358 * free pages containing page tables or TLB flushing requires IPI broadcast.
Steve Capper2667f502014-10-09 15:29:14 -07001359 *
Steve Capper2667f502014-10-09 15:29:14 -07001360 * *) ptes can be read atomically by the architecture.
1361 *
1362 * *) access_ok is sufficient to validate userspace address ranges.
1363 *
1364 * The last two assumptions can be relaxed by the addition of helper functions.
1365 *
1366 * This code is based heavily on the PowerPC implementation by Nick Piggin.
1367 */
Kirill A. Shutemove5855132017-06-06 14:31:20 +03001368#ifdef CONFIG_HAVE_GENERIC_GUP
Steve Capper2667f502014-10-09 15:29:14 -07001369
Kirill A. Shutemov0005d202017-03-16 18:26:51 +03001370#ifndef gup_get_pte
1371/*
1372 * We assume that the PTE can be read atomically. If this is not the case for
1373 * your architecture, please provide the helper.
1374 */
1375static inline pte_t gup_get_pte(pte_t *ptep)
1376{
1377 return READ_ONCE(*ptep);
1378}
1379#endif
1380
Kirill A. Shutemovb59f65f2017-03-16 18:26:53 +03001381static void undo_dev_pagemap(int *nr, int nr_start, struct page **pages)
1382{
1383 while ((*nr) - nr_start) {
1384 struct page *page = pages[--(*nr)];
1385
1386 ClearPageReferenced(page);
1387 put_page(page);
1388 }
1389}
1390
Laurent Dufour3010a5e2018-06-07 17:06:08 -07001391#ifdef CONFIG_ARCH_HAS_PTE_SPECIAL
Steve Capper2667f502014-10-09 15:29:14 -07001392static int gup_pte_range(pmd_t pmd, unsigned long addr, unsigned long end,
1393 int write, struct page **pages, int *nr)
1394{
Kirill A. Shutemovb59f65f2017-03-16 18:26:53 +03001395 struct dev_pagemap *pgmap = NULL;
1396 int nr_start = *nr, ret = 0;
Steve Capper2667f502014-10-09 15:29:14 -07001397 pte_t *ptep, *ptem;
Steve Capper2667f502014-10-09 15:29:14 -07001398
1399 ptem = ptep = pte_offset_map(&pmd, addr);
1400 do {
Kirill A. Shutemov0005d202017-03-16 18:26:51 +03001401 pte_t pte = gup_get_pte(ptep);
Kirill A. Shutemov7aef4172016-01-15 16:52:32 -08001402 struct page *head, *page;
Steve Capper2667f502014-10-09 15:29:14 -07001403
1404 /*
1405 * Similar to the PMD case below, NUMA hinting must take slow
Mel Gorman8a0516e2015-02-12 14:58:22 -08001406 * path using the pte_protnone check.
Steve Capper2667f502014-10-09 15:29:14 -07001407 */
Kirill A. Shutemove7884f82017-03-16 18:26:50 +03001408 if (pte_protnone(pte))
1409 goto pte_unmap;
1410
1411 if (!pte_access_permitted(pte, write))
1412 goto pte_unmap;
1413
Kirill A. Shutemovb59f65f2017-03-16 18:26:53 +03001414 if (pte_devmap(pte)) {
1415 pgmap = get_dev_pagemap(pte_pfn(pte), pgmap);
1416 if (unlikely(!pgmap)) {
1417 undo_dev_pagemap(nr, nr_start, pages);
1418 goto pte_unmap;
1419 }
1420 } else if (pte_special(pte))
Steve Capper2667f502014-10-09 15:29:14 -07001421 goto pte_unmap;
1422
1423 VM_BUG_ON(!pfn_valid(pte_pfn(pte)));
1424 page = pte_page(pte);
Kirill A. Shutemov7aef4172016-01-15 16:52:32 -08001425 head = compound_head(page);
Steve Capper2667f502014-10-09 15:29:14 -07001426
Kirill A. Shutemov7aef4172016-01-15 16:52:32 -08001427 if (!page_cache_get_speculative(head))
Steve Capper2667f502014-10-09 15:29:14 -07001428 goto pte_unmap;
1429
1430 if (unlikely(pte_val(pte) != pte_val(*ptep))) {
Kirill A. Shutemov7aef4172016-01-15 16:52:32 -08001431 put_page(head);
Steve Capper2667f502014-10-09 15:29:14 -07001432 goto pte_unmap;
1433 }
1434
Kirill A. Shutemov7aef4172016-01-15 16:52:32 -08001435 VM_BUG_ON_PAGE(compound_head(page) != head, page);
Kirill A. Shutemove9348052017-03-16 18:26:52 +03001436
1437 SetPageReferenced(page);
Steve Capper2667f502014-10-09 15:29:14 -07001438 pages[*nr] = page;
1439 (*nr)++;
1440
1441 } while (ptep++, addr += PAGE_SIZE, addr != end);
1442
1443 ret = 1;
1444
1445pte_unmap:
Christoph Hellwig832d7aa2017-12-29 08:54:01 +01001446 if (pgmap)
1447 put_dev_pagemap(pgmap);
Steve Capper2667f502014-10-09 15:29:14 -07001448 pte_unmap(ptem);
1449 return ret;
1450}
1451#else
1452
1453/*
1454 * If we can't determine whether or not a pte is special, then fail immediately
1455 * for ptes. Note, we can still pin HugeTLB and THP as these are guaranteed not
1456 * to be special.
1457 *
1458 * For a futex to be placed on a THP tail page, get_futex_key requires a
1459 * __get_user_pages_fast implementation that can pin pages. Thus it's still
1460 * useful to have gup_huge_pmd even if we can't operate on ptes.
1461 */
1462static int gup_pte_range(pmd_t pmd, unsigned long addr, unsigned long end,
1463 int write, struct page **pages, int *nr)
1464{
1465 return 0;
1466}
Laurent Dufour3010a5e2018-06-07 17:06:08 -07001467#endif /* CONFIG_ARCH_HAS_PTE_SPECIAL */
Steve Capper2667f502014-10-09 15:29:14 -07001468
Oliver O'Halloran09180ca2017-09-06 16:20:58 -07001469#if defined(__HAVE_ARCH_PTE_DEVMAP) && defined(CONFIG_TRANSPARENT_HUGEPAGE)
Kirill A. Shutemovb59f65f2017-03-16 18:26:53 +03001470static int __gup_device_huge(unsigned long pfn, unsigned long addr,
1471 unsigned long end, struct page **pages, int *nr)
1472{
1473 int nr_start = *nr;
1474 struct dev_pagemap *pgmap = NULL;
1475
1476 do {
1477 struct page *page = pfn_to_page(pfn);
1478
1479 pgmap = get_dev_pagemap(pfn, pgmap);
1480 if (unlikely(!pgmap)) {
1481 undo_dev_pagemap(nr, nr_start, pages);
1482 return 0;
1483 }
1484 SetPageReferenced(page);
1485 pages[*nr] = page;
1486 get_page(page);
Kirill A. Shutemovb59f65f2017-03-16 18:26:53 +03001487 (*nr)++;
1488 pfn++;
1489 } while (addr += PAGE_SIZE, addr != end);
Christoph Hellwig832d7aa2017-12-29 08:54:01 +01001490
1491 if (pgmap)
1492 put_dev_pagemap(pgmap);
Kirill A. Shutemovb59f65f2017-03-16 18:26:53 +03001493 return 1;
1494}
1495
Dan Williamsa9b6de72018-04-19 21:32:19 -07001496static int __gup_device_huge_pmd(pmd_t orig, pmd_t *pmdp, unsigned long addr,
Kirill A. Shutemovb59f65f2017-03-16 18:26:53 +03001497 unsigned long end, struct page **pages, int *nr)
1498{
1499 unsigned long fault_pfn;
Dan Williamsa9b6de72018-04-19 21:32:19 -07001500 int nr_start = *nr;
Kirill A. Shutemovb59f65f2017-03-16 18:26:53 +03001501
Dan Williamsa9b6de72018-04-19 21:32:19 -07001502 fault_pfn = pmd_pfn(orig) + ((addr & ~PMD_MASK) >> PAGE_SHIFT);
1503 if (!__gup_device_huge(fault_pfn, addr, end, pages, nr))
1504 return 0;
1505
1506 if (unlikely(pmd_val(orig) != pmd_val(*pmdp))) {
1507 undo_dev_pagemap(nr, nr_start, pages);
1508 return 0;
1509 }
1510 return 1;
Kirill A. Shutemovb59f65f2017-03-16 18:26:53 +03001511}
1512
Dan Williamsa9b6de72018-04-19 21:32:19 -07001513static int __gup_device_huge_pud(pud_t orig, pud_t *pudp, unsigned long addr,
Kirill A. Shutemovb59f65f2017-03-16 18:26:53 +03001514 unsigned long end, struct page **pages, int *nr)
1515{
1516 unsigned long fault_pfn;
Dan Williamsa9b6de72018-04-19 21:32:19 -07001517 int nr_start = *nr;
Kirill A. Shutemovb59f65f2017-03-16 18:26:53 +03001518
Dan Williamsa9b6de72018-04-19 21:32:19 -07001519 fault_pfn = pud_pfn(orig) + ((addr & ~PUD_MASK) >> PAGE_SHIFT);
1520 if (!__gup_device_huge(fault_pfn, addr, end, pages, nr))
1521 return 0;
1522
1523 if (unlikely(pud_val(orig) != pud_val(*pudp))) {
1524 undo_dev_pagemap(nr, nr_start, pages);
1525 return 0;
1526 }
1527 return 1;
Kirill A. Shutemovb59f65f2017-03-16 18:26:53 +03001528}
1529#else
Dan Williamsa9b6de72018-04-19 21:32:19 -07001530static int __gup_device_huge_pmd(pmd_t orig, pmd_t *pmdp, unsigned long addr,
Kirill A. Shutemovb59f65f2017-03-16 18:26:53 +03001531 unsigned long end, struct page **pages, int *nr)
1532{
1533 BUILD_BUG();
1534 return 0;
1535}
1536
Dan Williamsa9b6de72018-04-19 21:32:19 -07001537static int __gup_device_huge_pud(pud_t pud, pud_t *pudp, unsigned long addr,
Kirill A. Shutemovb59f65f2017-03-16 18:26:53 +03001538 unsigned long end, struct page **pages, int *nr)
1539{
1540 BUILD_BUG();
1541 return 0;
1542}
1543#endif
1544
Steve Capper2667f502014-10-09 15:29:14 -07001545static int gup_huge_pmd(pmd_t orig, pmd_t *pmdp, unsigned long addr,
1546 unsigned long end, int write, struct page **pages, int *nr)
1547{
Kirill A. Shutemovddc58f22016-01-15 16:52:56 -08001548 struct page *head, *page;
Steve Capper2667f502014-10-09 15:29:14 -07001549 int refs;
1550
Kirill A. Shutemove7884f82017-03-16 18:26:50 +03001551 if (!pmd_access_permitted(orig, write))
Steve Capper2667f502014-10-09 15:29:14 -07001552 return 0;
1553
Kirill A. Shutemovb59f65f2017-03-16 18:26:53 +03001554 if (pmd_devmap(orig))
Dan Williamsa9b6de72018-04-19 21:32:19 -07001555 return __gup_device_huge_pmd(orig, pmdp, addr, end, pages, nr);
Kirill A. Shutemovb59f65f2017-03-16 18:26:53 +03001556
Steve Capper2667f502014-10-09 15:29:14 -07001557 refs = 0;
Punit Agrawald63206e2017-07-06 15:39:39 -07001558 page = pmd_page(orig) + ((addr & ~PMD_MASK) >> PAGE_SHIFT);
Steve Capper2667f502014-10-09 15:29:14 -07001559 do {
Steve Capper2667f502014-10-09 15:29:14 -07001560 pages[*nr] = page;
1561 (*nr)++;
1562 page++;
1563 refs++;
1564 } while (addr += PAGE_SIZE, addr != end);
1565
Punit Agrawald63206e2017-07-06 15:39:39 -07001566 head = compound_head(pmd_page(orig));
Steve Capper2667f502014-10-09 15:29:14 -07001567 if (!page_cache_add_speculative(head, refs)) {
1568 *nr -= refs;
1569 return 0;
1570 }
1571
1572 if (unlikely(pmd_val(orig) != pmd_val(*pmdp))) {
1573 *nr -= refs;
1574 while (refs--)
1575 put_page(head);
1576 return 0;
1577 }
1578
Kirill A. Shutemove9348052017-03-16 18:26:52 +03001579 SetPageReferenced(head);
Steve Capper2667f502014-10-09 15:29:14 -07001580 return 1;
1581}
1582
1583static int gup_huge_pud(pud_t orig, pud_t *pudp, unsigned long addr,
1584 unsigned long end, int write, struct page **pages, int *nr)
1585{
Kirill A. Shutemovddc58f22016-01-15 16:52:56 -08001586 struct page *head, *page;
Steve Capper2667f502014-10-09 15:29:14 -07001587 int refs;
1588
Kirill A. Shutemove7884f82017-03-16 18:26:50 +03001589 if (!pud_access_permitted(orig, write))
Steve Capper2667f502014-10-09 15:29:14 -07001590 return 0;
1591
Kirill A. Shutemovb59f65f2017-03-16 18:26:53 +03001592 if (pud_devmap(orig))
Dan Williamsa9b6de72018-04-19 21:32:19 -07001593 return __gup_device_huge_pud(orig, pudp, addr, end, pages, nr);
Kirill A. Shutemovb59f65f2017-03-16 18:26:53 +03001594
Steve Capper2667f502014-10-09 15:29:14 -07001595 refs = 0;
Punit Agrawald63206e2017-07-06 15:39:39 -07001596 page = pud_page(orig) + ((addr & ~PUD_MASK) >> PAGE_SHIFT);
Steve Capper2667f502014-10-09 15:29:14 -07001597 do {
Steve Capper2667f502014-10-09 15:29:14 -07001598 pages[*nr] = page;
1599 (*nr)++;
1600 page++;
1601 refs++;
1602 } while (addr += PAGE_SIZE, addr != end);
1603
Punit Agrawald63206e2017-07-06 15:39:39 -07001604 head = compound_head(pud_page(orig));
Steve Capper2667f502014-10-09 15:29:14 -07001605 if (!page_cache_add_speculative(head, refs)) {
1606 *nr -= refs;
1607 return 0;
1608 }
1609
1610 if (unlikely(pud_val(orig) != pud_val(*pudp))) {
1611 *nr -= refs;
1612 while (refs--)
1613 put_page(head);
1614 return 0;
1615 }
1616
Kirill A. Shutemove9348052017-03-16 18:26:52 +03001617 SetPageReferenced(head);
Steve Capper2667f502014-10-09 15:29:14 -07001618 return 1;
1619}
1620
Aneesh Kumar K.Vf30c59e2014-11-05 21:57:40 +05301621static int gup_huge_pgd(pgd_t orig, pgd_t *pgdp, unsigned long addr,
1622 unsigned long end, int write,
1623 struct page **pages, int *nr)
1624{
1625 int refs;
Kirill A. Shutemovddc58f22016-01-15 16:52:56 -08001626 struct page *head, *page;
Aneesh Kumar K.Vf30c59e2014-11-05 21:57:40 +05301627
Kirill A. Shutemove7884f82017-03-16 18:26:50 +03001628 if (!pgd_access_permitted(orig, write))
Aneesh Kumar K.Vf30c59e2014-11-05 21:57:40 +05301629 return 0;
1630
Kirill A. Shutemovb59f65f2017-03-16 18:26:53 +03001631 BUILD_BUG_ON(pgd_devmap(orig));
Aneesh Kumar K.Vf30c59e2014-11-05 21:57:40 +05301632 refs = 0;
Punit Agrawald63206e2017-07-06 15:39:39 -07001633 page = pgd_page(orig) + ((addr & ~PGDIR_MASK) >> PAGE_SHIFT);
Aneesh Kumar K.Vf30c59e2014-11-05 21:57:40 +05301634 do {
Aneesh Kumar K.Vf30c59e2014-11-05 21:57:40 +05301635 pages[*nr] = page;
1636 (*nr)++;
1637 page++;
1638 refs++;
1639 } while (addr += PAGE_SIZE, addr != end);
1640
Punit Agrawald63206e2017-07-06 15:39:39 -07001641 head = compound_head(pgd_page(orig));
Aneesh Kumar K.Vf30c59e2014-11-05 21:57:40 +05301642 if (!page_cache_add_speculative(head, refs)) {
1643 *nr -= refs;
1644 return 0;
1645 }
1646
1647 if (unlikely(pgd_val(orig) != pgd_val(*pgdp))) {
1648 *nr -= refs;
1649 while (refs--)
1650 put_page(head);
1651 return 0;
1652 }
1653
Kirill A. Shutemove9348052017-03-16 18:26:52 +03001654 SetPageReferenced(head);
Aneesh Kumar K.Vf30c59e2014-11-05 21:57:40 +05301655 return 1;
1656}
1657
Steve Capper2667f502014-10-09 15:29:14 -07001658static int gup_pmd_range(pud_t pud, unsigned long addr, unsigned long end,
1659 int write, struct page **pages, int *nr)
1660{
1661 unsigned long next;
1662 pmd_t *pmdp;
1663
1664 pmdp = pmd_offset(&pud, addr);
1665 do {
Christian Borntraeger38c5ce92015-01-06 22:54:46 +01001666 pmd_t pmd = READ_ONCE(*pmdp);
Steve Capper2667f502014-10-09 15:29:14 -07001667
1668 next = pmd_addr_end(addr, end);
Zi Yan84c3fc42017-09-08 16:11:01 -07001669 if (!pmd_present(pmd))
Steve Capper2667f502014-10-09 15:29:14 -07001670 return 0;
1671
1672 if (unlikely(pmd_trans_huge(pmd) || pmd_huge(pmd))) {
1673 /*
1674 * NUMA hinting faults need to be handled in the GUP
1675 * slowpath for accounting purposes and so that they
1676 * can be serialised against THP migration.
1677 */
Mel Gorman8a0516e2015-02-12 14:58:22 -08001678 if (pmd_protnone(pmd))
Steve Capper2667f502014-10-09 15:29:14 -07001679 return 0;
1680
1681 if (!gup_huge_pmd(pmd, pmdp, addr, next, write,
1682 pages, nr))
1683 return 0;
1684
Aneesh Kumar K.Vf30c59e2014-11-05 21:57:40 +05301685 } else if (unlikely(is_hugepd(__hugepd(pmd_val(pmd))))) {
1686 /*
1687 * architecture have different format for hugetlbfs
1688 * pmd format and THP pmd format
1689 */
1690 if (!gup_huge_pd(__hugepd(pmd_val(pmd)), addr,
1691 PMD_SHIFT, next, write, pages, nr))
1692 return 0;
Steve Capper2667f502014-10-09 15:29:14 -07001693 } else if (!gup_pte_range(pmd, addr, next, write, pages, nr))
Mario Leinweber29231172018-04-05 16:24:18 -07001694 return 0;
Steve Capper2667f502014-10-09 15:29:14 -07001695 } while (pmdp++, addr = next, addr != end);
1696
1697 return 1;
1698}
1699
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +03001700static int gup_pud_range(p4d_t p4d, unsigned long addr, unsigned long end,
Aneesh Kumar K.Vf30c59e2014-11-05 21:57:40 +05301701 int write, struct page **pages, int *nr)
Steve Capper2667f502014-10-09 15:29:14 -07001702{
1703 unsigned long next;
1704 pud_t *pudp;
1705
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +03001706 pudp = pud_offset(&p4d, addr);
Steve Capper2667f502014-10-09 15:29:14 -07001707 do {
Christian Borntraegere37c6982014-12-07 21:41:33 +01001708 pud_t pud = READ_ONCE(*pudp);
Steve Capper2667f502014-10-09 15:29:14 -07001709
1710 next = pud_addr_end(addr, end);
1711 if (pud_none(pud))
1712 return 0;
Aneesh Kumar K.Vf30c59e2014-11-05 21:57:40 +05301713 if (unlikely(pud_huge(pud))) {
Steve Capper2667f502014-10-09 15:29:14 -07001714 if (!gup_huge_pud(pud, pudp, addr, next, write,
Aneesh Kumar K.Vf30c59e2014-11-05 21:57:40 +05301715 pages, nr))
1716 return 0;
1717 } else if (unlikely(is_hugepd(__hugepd(pud_val(pud))))) {
1718 if (!gup_huge_pd(__hugepd(pud_val(pud)), addr,
1719 PUD_SHIFT, next, write, pages, nr))
Steve Capper2667f502014-10-09 15:29:14 -07001720 return 0;
1721 } else if (!gup_pmd_range(pud, addr, next, write, pages, nr))
1722 return 0;
1723 } while (pudp++, addr = next, addr != end);
1724
1725 return 1;
1726}
1727
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +03001728static int gup_p4d_range(pgd_t pgd, unsigned long addr, unsigned long end,
1729 int write, struct page **pages, int *nr)
1730{
1731 unsigned long next;
1732 p4d_t *p4dp;
1733
1734 p4dp = p4d_offset(&pgd, addr);
1735 do {
1736 p4d_t p4d = READ_ONCE(*p4dp);
1737
1738 next = p4d_addr_end(addr, end);
1739 if (p4d_none(p4d))
1740 return 0;
1741 BUILD_BUG_ON(p4d_huge(p4d));
1742 if (unlikely(is_hugepd(__hugepd(p4d_val(p4d))))) {
1743 if (!gup_huge_pd(__hugepd(p4d_val(p4d)), addr,
1744 P4D_SHIFT, next, write, pages, nr))
1745 return 0;
Kirill A. Shutemovce70df02017-03-13 08:22:13 +03001746 } else if (!gup_pud_range(p4d, addr, next, write, pages, nr))
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +03001747 return 0;
1748 } while (p4dp++, addr = next, addr != end);
1749
1750 return 1;
1751}
1752
Kirill A. Shutemov5b65c4672017-09-09 00:56:03 +03001753static void gup_pgd_range(unsigned long addr, unsigned long end,
1754 int write, struct page **pages, int *nr)
1755{
1756 unsigned long next;
1757 pgd_t *pgdp;
1758
1759 pgdp = pgd_offset(current->mm, addr);
1760 do {
1761 pgd_t pgd = READ_ONCE(*pgdp);
1762
1763 next = pgd_addr_end(addr, end);
1764 if (pgd_none(pgd))
1765 return;
1766 if (unlikely(pgd_huge(pgd))) {
1767 if (!gup_huge_pgd(pgd, pgdp, addr, next, write,
1768 pages, nr))
1769 return;
1770 } else if (unlikely(is_hugepd(__hugepd(pgd_val(pgd))))) {
1771 if (!gup_huge_pd(__hugepd(pgd_val(pgd)), addr,
1772 PGDIR_SHIFT, next, write, pages, nr))
1773 return;
1774 } else if (!gup_p4d_range(pgd, addr, next, write, pages, nr))
1775 return;
1776 } while (pgdp++, addr = next, addr != end);
1777}
1778
1779#ifndef gup_fast_permitted
1780/*
1781 * Check if it's allowed to use __get_user_pages_fast() for the range, or
1782 * we need to fall back to the slow version:
1783 */
1784bool gup_fast_permitted(unsigned long start, int nr_pages, int write)
1785{
1786 unsigned long len, end;
1787
1788 len = (unsigned long) nr_pages << PAGE_SHIFT;
1789 end = start + len;
1790 return end >= start;
1791}
1792#endif
1793
Steve Capper2667f502014-10-09 15:29:14 -07001794/*
1795 * Like get_user_pages_fast() except it's IRQ-safe in that it won't fall back to
Michael S. Tsirkind0811072018-04-13 15:35:23 -07001796 * the regular GUP.
1797 * Note a difference with get_user_pages_fast: this always returns the
1798 * number of pages pinned, 0 if no pages were pinned.
Steve Capper2667f502014-10-09 15:29:14 -07001799 */
1800int __get_user_pages_fast(unsigned long start, int nr_pages, int write,
1801 struct page **pages)
1802{
Wei Yangd4faa402018-10-26 15:07:55 -07001803 unsigned long len, end;
Kirill A. Shutemov5b65c4672017-09-09 00:56:03 +03001804 unsigned long flags;
Steve Capper2667f502014-10-09 15:29:14 -07001805 int nr = 0;
1806
1807 start &= PAGE_MASK;
Steve Capper2667f502014-10-09 15:29:14 -07001808 len = (unsigned long) nr_pages << PAGE_SHIFT;
1809 end = start + len;
1810
1811 if (unlikely(!access_ok(write ? VERIFY_WRITE : VERIFY_READ,
Arnd Bergmannaa2369f2017-05-03 14:56:12 -07001812 (void __user *)start, len)))
Steve Capper2667f502014-10-09 15:29:14 -07001813 return 0;
1814
1815 /*
1816 * Disable interrupts. We use the nested form as we can already have
1817 * interrupts disabled by get_futex_key.
1818 *
1819 * With interrupts disabled, we block page table pages from being
Fengguang Wu2ebe8222018-10-30 15:10:51 -07001820 * freed from under us. See struct mmu_table_batch comments in
1821 * include/asm-generic/tlb.h for more details.
Steve Capper2667f502014-10-09 15:29:14 -07001822 *
1823 * We do not adopt an rcu_read_lock(.) here as we also want to
1824 * block IPIs that come from THPs splitting.
1825 */
1826
Kirill A. Shutemov5b65c4672017-09-09 00:56:03 +03001827 if (gup_fast_permitted(start, nr_pages, write)) {
1828 local_irq_save(flags);
Wei Yangd4faa402018-10-26 15:07:55 -07001829 gup_pgd_range(start, end, write, pages, &nr);
Kirill A. Shutemov5b65c4672017-09-09 00:56:03 +03001830 local_irq_restore(flags);
1831 }
Steve Capper2667f502014-10-09 15:29:14 -07001832
1833 return nr;
1834}
1835
1836/**
1837 * get_user_pages_fast() - pin user pages in memory
1838 * @start: starting user address
1839 * @nr_pages: number of pages from start to pin
1840 * @write: whether pages will be written to
1841 * @pages: array that receives pointers to the pages pinned.
1842 * Should be at least nr_pages long.
1843 *
1844 * Attempt to pin user pages in memory without taking mm->mmap_sem.
1845 * If not successful, it will fall back to taking the lock and
1846 * calling get_user_pages().
1847 *
1848 * Returns number of pages pinned. This may be fewer than the number
1849 * requested. If nr_pages is 0 or negative, returns 0. If no pages
1850 * were pinned, returns -errno.
1851 */
1852int get_user_pages_fast(unsigned long start, int nr_pages, int write,
1853 struct page **pages)
1854{
Kirill A. Shutemov5b65c4672017-09-09 00:56:03 +03001855 unsigned long addr, len, end;
Kirill A. Shutemov73e10a62017-03-16 18:26:54 +03001856 int nr = 0, ret = 0;
Steve Capper2667f502014-10-09 15:29:14 -07001857
1858 start &= PAGE_MASK;
Kirill A. Shutemov5b65c4672017-09-09 00:56:03 +03001859 addr = start;
1860 len = (unsigned long) nr_pages << PAGE_SHIFT;
1861 end = start + len;
1862
Michael S. Tsirkinc61611f2018-04-13 15:35:20 -07001863 if (nr_pages <= 0)
1864 return 0;
1865
Kirill A. Shutemov5b65c4672017-09-09 00:56:03 +03001866 if (unlikely(!access_ok(write ? VERIFY_WRITE : VERIFY_READ,
1867 (void __user *)start, len)))
Michael S. Tsirkinc61611f2018-04-13 15:35:20 -07001868 return -EFAULT;
Kirill A. Shutemov73e10a62017-03-16 18:26:54 +03001869
1870 if (gup_fast_permitted(start, nr_pages, write)) {
Kirill A. Shutemov5b65c4672017-09-09 00:56:03 +03001871 local_irq_disable();
1872 gup_pgd_range(addr, end, write, pages, &nr);
1873 local_irq_enable();
Kirill A. Shutemov73e10a62017-03-16 18:26:54 +03001874 ret = nr;
1875 }
Steve Capper2667f502014-10-09 15:29:14 -07001876
1877 if (nr < nr_pages) {
1878 /* Try to get the remaining pages with get_user_pages */
1879 start += nr << PAGE_SHIFT;
1880 pages += nr;
1881
Lorenzo Stoakesc1641542016-10-13 01:20:13 +01001882 ret = get_user_pages_unlocked(start, nr_pages - nr, pages,
1883 write ? FOLL_WRITE : 0);
Steve Capper2667f502014-10-09 15:29:14 -07001884
1885 /* Have to be a bit careful with return values */
1886 if (nr > 0) {
1887 if (ret < 0)
1888 ret = nr;
1889 else
1890 ret += nr;
1891 }
1892 }
1893
1894 return ret;
1895}
1896
Kirill A. Shutemove5855132017-06-06 14:31:20 +03001897#endif /* CONFIG_HAVE_GENERIC_GUP */