blob: 8cb68a50dbdf28a27dfaef6dce27e6cdb1f87881 [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
Mike Rapoport78179552018-11-16 15:08:29 -0800388 * @ctx: contains dev_pagemap for %ZONE_DEVICE memory pinning and a
389 * pointer to output page_mask
Aneesh Kumar K.V080dbb62017-07-06 15:38:44 -0700390 *
391 * @flags can have FOLL_ flags set, defined in <linux/mm.h>
392 *
Mike Rapoport78179552018-11-16 15:08:29 -0800393 * When getting pages from ZONE_DEVICE memory, the @ctx->pgmap caches
394 * the device's dev_pagemap metadata to avoid repeating expensive lookups.
395 *
396 * On output, the @ctx->page_mask is set according to the size of the page.
397 *
398 * Return: the mapped (struct page *), %NULL if no mapping exists, or
Aneesh Kumar K.V080dbb62017-07-06 15:38:44 -0700399 * an error pointer if there is a mapping to something not represented
400 * by a page descriptor (see also vm_normal_page()).
401 */
402struct page *follow_page_mask(struct vm_area_struct *vma,
403 unsigned long address, unsigned int flags,
Keith Buschdf06b372018-10-26 15:10:28 -0700404 struct follow_page_context *ctx)
Aneesh Kumar K.V080dbb62017-07-06 15:38:44 -0700405{
406 pgd_t *pgd;
407 struct page *page;
408 struct mm_struct *mm = vma->vm_mm;
409
Keith Buschdf06b372018-10-26 15:10:28 -0700410 ctx->page_mask = 0;
Aneesh Kumar K.V080dbb62017-07-06 15:38:44 -0700411
412 /* make this handle hugepd */
413 page = follow_huge_addr(mm, address, flags & FOLL_WRITE);
414 if (!IS_ERR(page)) {
415 BUG_ON(flags & FOLL_GET);
416 return page;
417 }
418
419 pgd = pgd_offset(mm, address);
420
421 if (pgd_none(*pgd) || unlikely(pgd_bad(*pgd)))
422 return no_page_table(vma, flags);
423
Anshuman Khandualfaaa5b62017-07-06 15:38:50 -0700424 if (pgd_huge(*pgd)) {
425 page = follow_huge_pgd(mm, address, pgd, flags);
426 if (page)
427 return page;
428 return no_page_table(vma, flags);
429 }
Aneesh Kumar K.V4dc71452017-07-06 15:38:56 -0700430 if (is_hugepd(__hugepd(pgd_val(*pgd)))) {
431 page = follow_huge_pd(vma, address,
432 __hugepd(pgd_val(*pgd)), flags,
433 PGDIR_SHIFT);
434 if (page)
435 return page;
436 return no_page_table(vma, flags);
437 }
Anshuman Khandualfaaa5b62017-07-06 15:38:50 -0700438
Keith Buschdf06b372018-10-26 15:10:28 -0700439 return follow_p4d_mask(vma, address, pgd, flags, ctx);
440}
441
442struct page *follow_page(struct vm_area_struct *vma, unsigned long address,
443 unsigned int foll_flags)
444{
445 struct follow_page_context ctx = { NULL };
446 struct page *page;
447
448 page = follow_page_mask(vma, address, foll_flags, &ctx);
449 if (ctx.pgmap)
450 put_dev_pagemap(ctx.pgmap);
451 return page;
Aneesh Kumar K.V080dbb62017-07-06 15:38:44 -0700452}
453
Kirill A. Shutemovf2b495c2014-06-04 16:08:11 -0700454static int get_gate_page(struct mm_struct *mm, unsigned long address,
455 unsigned int gup_flags, struct vm_area_struct **vma,
456 struct page **page)
457{
458 pgd_t *pgd;
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +0300459 p4d_t *p4d;
Kirill A. Shutemovf2b495c2014-06-04 16:08:11 -0700460 pud_t *pud;
461 pmd_t *pmd;
462 pte_t *pte;
463 int ret = -EFAULT;
464
465 /* user gate pages are read-only */
466 if (gup_flags & FOLL_WRITE)
467 return -EFAULT;
468 if (address > TASK_SIZE)
469 pgd = pgd_offset_k(address);
470 else
471 pgd = pgd_offset_gate(mm, address);
472 BUG_ON(pgd_none(*pgd));
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +0300473 p4d = p4d_offset(pgd, address);
474 BUG_ON(p4d_none(*p4d));
475 pud = pud_offset(p4d, address);
Kirill A. Shutemovf2b495c2014-06-04 16:08:11 -0700476 BUG_ON(pud_none(*pud));
477 pmd = pmd_offset(pud, address);
Zi Yan84c3fc42017-09-08 16:11:01 -0700478 if (!pmd_present(*pmd))
Kirill A. Shutemovf2b495c2014-06-04 16:08:11 -0700479 return -EFAULT;
480 VM_BUG_ON(pmd_trans_huge(*pmd));
481 pte = pte_offset_map(pmd, address);
482 if (pte_none(*pte))
483 goto unmap;
484 *vma = get_gate_vma(mm);
485 if (!page)
486 goto out;
487 *page = vm_normal_page(*vma, address, *pte);
488 if (!*page) {
489 if ((gup_flags & FOLL_DUMP) || !is_zero_pfn(pte_pfn(*pte)))
490 goto unmap;
491 *page = pte_page(*pte);
Jérôme Glissedf6ad692017-09-08 16:12:24 -0700492
493 /*
494 * This should never happen (a device public page in the gate
495 * area).
496 */
497 if (is_device_public_page(*page))
498 goto unmap;
Kirill A. Shutemovf2b495c2014-06-04 16:08:11 -0700499 }
500 get_page(*page);
501out:
502 ret = 0;
503unmap:
504 pte_unmap(pte);
505 return ret;
506}
507
Paul Cassella9a95f3c2014-08-06 16:07:24 -0700508/*
509 * mmap_sem must be held on entry. If @nonblocking != NULL and
510 * *@flags does not include FOLL_NOWAIT, the mmap_sem may be released.
511 * If it is, *@nonblocking will be set to 0 and -EBUSY returned.
512 */
Kirill A. Shutemov16744482014-06-04 16:08:12 -0700513static int faultin_page(struct task_struct *tsk, struct vm_area_struct *vma,
514 unsigned long address, unsigned int *flags, int *nonblocking)
515{
Kirill A. Shutemov16744482014-06-04 16:08:12 -0700516 unsigned int fault_flags = 0;
Souptick Joarder2b740302018-08-23 17:01:36 -0700517 vm_fault_t ret;
Kirill A. Shutemov16744482014-06-04 16:08:12 -0700518
Eric B Munsonde60f5f2015-11-05 18:51:36 -0800519 /* mlock all present pages, but do not fault in new pages */
520 if ((*flags & (FOLL_POPULATE | FOLL_MLOCK)) == FOLL_MLOCK)
521 return -ENOENT;
Kirill A. Shutemov16744482014-06-04 16:08:12 -0700522 if (*flags & FOLL_WRITE)
523 fault_flags |= FAULT_FLAG_WRITE;
Dave Hansen1b2ee122016-02-12 13:02:21 -0800524 if (*flags & FOLL_REMOTE)
525 fault_flags |= FAULT_FLAG_REMOTE;
Kirill A. Shutemov16744482014-06-04 16:08:12 -0700526 if (nonblocking)
527 fault_flags |= FAULT_FLAG_ALLOW_RETRY;
528 if (*flags & FOLL_NOWAIT)
529 fault_flags |= FAULT_FLAG_ALLOW_RETRY | FAULT_FLAG_RETRY_NOWAIT;
Andres Lagar-Cavilla234b2392014-09-17 10:51:48 -0700530 if (*flags & FOLL_TRIED) {
531 VM_WARN_ON_ONCE(fault_flags & FAULT_FLAG_ALLOW_RETRY);
532 fault_flags |= FAULT_FLAG_TRIED;
533 }
Kirill A. Shutemov16744482014-06-04 16:08:12 -0700534
Kirill A. Shutemovdcddffd2016-07-26 15:25:18 -0700535 ret = handle_mm_fault(vma, address, fault_flags);
Kirill A. Shutemov16744482014-06-04 16:08:12 -0700536 if (ret & VM_FAULT_ERROR) {
James Morse9a291a72017-06-02 14:46:46 -0700537 int err = vm_fault_to_errno(ret, *flags);
538
539 if (err)
540 return err;
Kirill A. Shutemov16744482014-06-04 16:08:12 -0700541 BUG();
542 }
543
544 if (tsk) {
545 if (ret & VM_FAULT_MAJOR)
546 tsk->maj_flt++;
547 else
548 tsk->min_flt++;
549 }
550
551 if (ret & VM_FAULT_RETRY) {
Andrea Arcangeli96312e62018-03-09 15:51:06 -0800552 if (nonblocking && !(fault_flags & FAULT_FLAG_RETRY_NOWAIT))
Kirill A. Shutemov16744482014-06-04 16:08:12 -0700553 *nonblocking = 0;
554 return -EBUSY;
555 }
556
557 /*
558 * The VM_FAULT_WRITE bit tells us that do_wp_page has broken COW when
559 * necessary, even if maybe_mkwrite decided not to set pte_write. We
560 * can thus safely do subsequent page lookups as if they were reads.
561 * But only do so when looping for pte_write is futile: in some cases
562 * userspace may also be wanting to write to the gotten user page,
563 * which a read fault here might prevent (a readonly page might get
564 * reCOWed by userspace write).
565 */
566 if ((ret & VM_FAULT_WRITE) && !(vma->vm_flags & VM_WRITE))
Mario Leinweber29231172018-04-05 16:24:18 -0700567 *flags |= FOLL_COW;
Kirill A. Shutemov16744482014-06-04 16:08:12 -0700568 return 0;
569}
570
Kirill A. Shutemovfa5bb202014-06-04 16:08:13 -0700571static int check_vma_flags(struct vm_area_struct *vma, unsigned long gup_flags)
572{
573 vm_flags_t vm_flags = vma->vm_flags;
Dave Hansen1b2ee122016-02-12 13:02:21 -0800574 int write = (gup_flags & FOLL_WRITE);
575 int foreign = (gup_flags & FOLL_REMOTE);
Kirill A. Shutemovfa5bb202014-06-04 16:08:13 -0700576
577 if (vm_flags & (VM_IO | VM_PFNMAP))
578 return -EFAULT;
579
Willy Tarreau7f7ccc22018-05-11 08:11:44 +0200580 if (gup_flags & FOLL_ANON && !vma_is_anonymous(vma))
581 return -EFAULT;
582
Dave Hansen1b2ee122016-02-12 13:02:21 -0800583 if (write) {
Kirill A. Shutemovfa5bb202014-06-04 16:08:13 -0700584 if (!(vm_flags & VM_WRITE)) {
585 if (!(gup_flags & FOLL_FORCE))
586 return -EFAULT;
587 /*
588 * We used to let the write,force case do COW in a
589 * VM_MAYWRITE VM_SHARED !VM_WRITE vma, so ptrace could
590 * set a breakpoint in a read-only mapping of an
591 * executable, without corrupting the file (yet only
592 * when that file had been opened for writing!).
593 * Anon pages in shared mappings are surprising: now
594 * just reject it.
595 */
Hugh Dickins46435362016-01-30 18:03:16 -0800596 if (!is_cow_mapping(vm_flags))
Kirill A. Shutemovfa5bb202014-06-04 16:08:13 -0700597 return -EFAULT;
Kirill A. Shutemovfa5bb202014-06-04 16:08:13 -0700598 }
599 } else if (!(vm_flags & VM_READ)) {
600 if (!(gup_flags & FOLL_FORCE))
601 return -EFAULT;
602 /*
603 * Is there actually any vma we can reach here which does not
604 * have VM_MAYREAD set?
605 */
606 if (!(vm_flags & VM_MAYREAD))
607 return -EFAULT;
608 }
Dave Hansend61172b2016-02-12 13:02:24 -0800609 /*
610 * gups are always data accesses, not instruction
611 * fetches, so execute=false here
612 */
613 if (!arch_vma_access_permitted(vma, write, false, foreign))
Dave Hansen33a709b2016-02-12 13:02:19 -0800614 return -EFAULT;
Kirill A. Shutemovfa5bb202014-06-04 16:08:13 -0700615 return 0;
616}
617
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700618/**
619 * __get_user_pages() - pin user pages in memory
620 * @tsk: task_struct of target task
621 * @mm: mm_struct of target mm
622 * @start: starting user address
623 * @nr_pages: number of pages from start to pin
624 * @gup_flags: flags modifying pin behaviour
625 * @pages: array that receives pointers to the pages pinned.
626 * Should be at least nr_pages long. Or NULL, if caller
627 * only intends to ensure the pages are faulted in.
628 * @vmas: array of pointers to vmas corresponding to each page.
629 * Or NULL if the caller does not require them.
630 * @nonblocking: whether waiting for disk IO or mmap_sem contention
631 *
632 * Returns number of pages pinned. This may be fewer than the number
633 * requested. If nr_pages is 0 or negative, returns 0. If no pages
634 * were pinned, returns -errno. Each page returned must be released
635 * with a put_page() call when it is finished with. vmas will only
636 * remain valid while mmap_sem is held.
637 *
Paul Cassella9a95f3c2014-08-06 16:07:24 -0700638 * Must be called with mmap_sem held. It may be released. See below.
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700639 *
640 * __get_user_pages walks a process's page tables and takes a reference to
641 * each struct page that each user address corresponds to at a given
642 * instant. That is, it takes the page that would be accessed if a user
643 * thread accesses the given user virtual address at that instant.
644 *
645 * This does not guarantee that the page exists in the user mappings when
646 * __get_user_pages returns, and there may even be a completely different
647 * page there in some cases (eg. if mmapped pagecache has been invalidated
648 * and subsequently re faulted). However it does guarantee that the page
649 * won't be freed completely. And mostly callers simply care that the page
650 * contains data that was valid *at some point in time*. Typically, an IO
651 * or similar operation cannot guarantee anything stronger anyway because
652 * locks can't be held over the syscall boundary.
653 *
654 * If @gup_flags & FOLL_WRITE == 0, the page must not be written to. If
655 * the page is written to, set_page_dirty (or set_page_dirty_lock, as
656 * appropriate) must be called after the page is finished with, and
657 * before put_page is called.
658 *
659 * If @nonblocking != NULL, __get_user_pages will not wait for disk IO
660 * or mmap_sem contention, and if waiting is needed to pin all pages,
Paul Cassella9a95f3c2014-08-06 16:07:24 -0700661 * *@nonblocking will be set to 0. Further, if @gup_flags does not
662 * include FOLL_NOWAIT, the mmap_sem will be released via up_read() in
663 * this case.
664 *
665 * A caller using such a combination of @nonblocking and @gup_flags
666 * must therefore hold the mmap_sem for reading only, and recognize
667 * when it's been released. Otherwise, it must be held for either
668 * reading or writing and will not be released.
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700669 *
670 * In most cases, get_user_pages or get_user_pages_fast should be used
671 * instead of __get_user_pages. __get_user_pages should be used only if
672 * you need some special @gup_flags.
673 */
Lorenzo Stoakes0d731752016-10-24 10:57:25 +0100674static long __get_user_pages(struct task_struct *tsk, struct mm_struct *mm,
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700675 unsigned long start, unsigned long nr_pages,
676 unsigned int gup_flags, struct page **pages,
677 struct vm_area_struct **vmas, int *nonblocking)
678{
Keith Buschdf06b372018-10-26 15:10:28 -0700679 long ret = 0, i = 0;
Kirill A. Shutemovfa5bb202014-06-04 16:08:13 -0700680 struct vm_area_struct *vma = NULL;
Keith Buschdf06b372018-10-26 15:10:28 -0700681 struct follow_page_context ctx = { NULL };
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700682
683 if (!nr_pages)
684 return 0;
685
686 VM_BUG_ON(!!pages != !!(gup_flags & FOLL_GET));
687
688 /*
689 * If FOLL_FORCE is set then do not force a full fault as the hinting
690 * fault information is unrelated to the reference behaviour of a task
691 * using the address space
692 */
693 if (!(gup_flags & FOLL_FORCE))
694 gup_flags |= FOLL_NUMA;
695
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700696 do {
Kirill A. Shutemovfa5bb202014-06-04 16:08:13 -0700697 struct page *page;
698 unsigned int foll_flags = gup_flags;
699 unsigned int page_increm;
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700700
Kirill A. Shutemovfa5bb202014-06-04 16:08:13 -0700701 /* first iteration or cross vma bound */
702 if (!vma || start >= vma->vm_end) {
703 vma = find_extend_vma(mm, start);
704 if (!vma && in_gate_area(mm, start)) {
Kirill A. Shutemovfa5bb202014-06-04 16:08:13 -0700705 ret = get_gate_page(mm, start & PAGE_MASK,
706 gup_flags, &vma,
707 pages ? &pages[i] : NULL);
708 if (ret)
John Hubbard08be37b2018-11-30 14:08:53 -0800709 goto out;
Keith Buschdf06b372018-10-26 15:10:28 -0700710 ctx.page_mask = 0;
Kirill A. Shutemovfa5bb202014-06-04 16:08:13 -0700711 goto next_page;
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700712 }
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700713
Keith Buschdf06b372018-10-26 15:10:28 -0700714 if (!vma || check_vma_flags(vma, gup_flags)) {
715 ret = -EFAULT;
716 goto out;
717 }
Kirill A. Shutemovfa5bb202014-06-04 16:08:13 -0700718 if (is_vm_hugetlb_page(vma)) {
719 i = follow_hugetlb_page(mm, vma, pages, vmas,
720 &start, &nr_pages, i,
Andrea Arcangeli87ffc112017-02-22 15:43:13 -0800721 gup_flags, nonblocking);
Kirill A. Shutemovfa5bb202014-06-04 16:08:13 -0700722 continue;
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700723 }
Kirill A. Shutemovfa5bb202014-06-04 16:08:13 -0700724 }
725retry:
726 /*
727 * If we have a pending SIGKILL, don't keep faulting pages and
728 * potentially allocating memory.
729 */
Keith Buschdf06b372018-10-26 15:10:28 -0700730 if (unlikely(fatal_signal_pending(current))) {
731 ret = -ERESTARTSYS;
732 goto out;
733 }
Kirill A. Shutemovfa5bb202014-06-04 16:08:13 -0700734 cond_resched();
Keith Buschdf06b372018-10-26 15:10:28 -0700735
736 page = follow_page_mask(vma, start, foll_flags, &ctx);
Kirill A. Shutemovfa5bb202014-06-04 16:08:13 -0700737 if (!page) {
Kirill A. Shutemovfa5bb202014-06-04 16:08:13 -0700738 ret = faultin_page(tsk, vma, start, &foll_flags,
739 nonblocking);
740 switch (ret) {
741 case 0:
742 goto retry;
Keith Buschdf06b372018-10-26 15:10:28 -0700743 case -EBUSY:
744 ret = 0;
745 /* FALLTHRU */
Kirill A. Shutemovfa5bb202014-06-04 16:08:13 -0700746 case -EFAULT:
747 case -ENOMEM:
748 case -EHWPOISON:
Keith Buschdf06b372018-10-26 15:10:28 -0700749 goto out;
Kirill A. Shutemovfa5bb202014-06-04 16:08:13 -0700750 case -ENOENT:
751 goto next_page;
752 }
753 BUG();
Kirill A. Shutemov1027e442015-09-04 15:47:55 -0700754 } else if (PTR_ERR(page) == -EEXIST) {
755 /*
756 * Proper page table entry exists, but no corresponding
757 * struct page.
758 */
759 goto next_page;
760 } else if (IS_ERR(page)) {
Keith Buschdf06b372018-10-26 15:10:28 -0700761 ret = PTR_ERR(page);
762 goto out;
Kirill A. Shutemov1027e442015-09-04 15:47:55 -0700763 }
Kirill A. Shutemovfa5bb202014-06-04 16:08:13 -0700764 if (pages) {
765 pages[i] = page;
766 flush_anon_page(vma, page, start);
767 flush_dcache_page(page);
Keith Buschdf06b372018-10-26 15:10:28 -0700768 ctx.page_mask = 0;
Kirill A. Shutemovfa5bb202014-06-04 16:08:13 -0700769 }
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700770next_page:
Kirill A. Shutemovfa5bb202014-06-04 16:08:13 -0700771 if (vmas) {
772 vmas[i] = vma;
Keith Buschdf06b372018-10-26 15:10:28 -0700773 ctx.page_mask = 0;
Kirill A. Shutemovfa5bb202014-06-04 16:08:13 -0700774 }
Keith Buschdf06b372018-10-26 15:10:28 -0700775 page_increm = 1 + (~(start >> PAGE_SHIFT) & ctx.page_mask);
Kirill A. Shutemovfa5bb202014-06-04 16:08:13 -0700776 if (page_increm > nr_pages)
777 page_increm = nr_pages;
778 i += page_increm;
779 start += page_increm * PAGE_SIZE;
780 nr_pages -= page_increm;
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700781 } while (nr_pages);
Keith Buschdf06b372018-10-26 15:10:28 -0700782out:
783 if (ctx.pgmap)
784 put_dev_pagemap(ctx.pgmap);
785 return i ? i : ret;
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700786}
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700787
Tobias Klauser771ab432016-12-12 16:41:53 -0800788static bool vma_permits_fault(struct vm_area_struct *vma,
789 unsigned int fault_flags)
Dave Hansend4925e02016-02-12 13:02:16 -0800790{
Dave Hansen1b2ee122016-02-12 13:02:21 -0800791 bool write = !!(fault_flags & FAULT_FLAG_WRITE);
792 bool foreign = !!(fault_flags & FAULT_FLAG_REMOTE);
Dave Hansen33a709b2016-02-12 13:02:19 -0800793 vm_flags_t vm_flags = write ? VM_WRITE : VM_READ;
Dave Hansend4925e02016-02-12 13:02:16 -0800794
795 if (!(vm_flags & vma->vm_flags))
796 return false;
797
Dave Hansen33a709b2016-02-12 13:02:19 -0800798 /*
799 * The architecture might have a hardware protection
Dave Hansen1b2ee122016-02-12 13:02:21 -0800800 * mechanism other than read/write that can deny access.
Dave Hansend61172b2016-02-12 13:02:24 -0800801 *
802 * gup always represents data access, not instruction
803 * fetches, so execute=false here:
Dave Hansen33a709b2016-02-12 13:02:19 -0800804 */
Dave Hansend61172b2016-02-12 13:02:24 -0800805 if (!arch_vma_access_permitted(vma, write, false, foreign))
Dave Hansen33a709b2016-02-12 13:02:19 -0800806 return false;
807
Dave Hansend4925e02016-02-12 13:02:16 -0800808 return true;
809}
810
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700811/*
812 * fixup_user_fault() - manually resolve a user page fault
813 * @tsk: the task_struct to use for page fault accounting, or
814 * NULL if faults are not to be recorded.
815 * @mm: mm_struct of target mm
816 * @address: user address
817 * @fault_flags:flags to pass down to handle_mm_fault()
Dominik Dingel4a9e1cd2016-01-15 16:57:04 -0800818 * @unlocked: did we unlock the mmap_sem while retrying, maybe NULL if caller
819 * does not allow retry
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700820 *
821 * This is meant to be called in the specific scenario where for locking reasons
822 * we try to access user memory in atomic context (within a pagefault_disable()
823 * section), this returns -EFAULT, and we want to resolve the user fault before
824 * trying again.
825 *
826 * Typically this is meant to be used by the futex code.
827 *
828 * The main difference with get_user_pages() is that this function will
829 * unconditionally call handle_mm_fault() which will in turn perform all the
830 * necessary SW fixup of the dirty and young bits in the PTE, while
Dominik Dingel4a9e1cd2016-01-15 16:57:04 -0800831 * get_user_pages() only guarantees to update these in the struct page.
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700832 *
833 * This is important for some architectures where those bits also gate the
834 * access permission to the page because they are maintained in software. On
835 * such architectures, gup() will not be enough to make a subsequent access
836 * succeed.
837 *
Dominik Dingel4a9e1cd2016-01-15 16:57:04 -0800838 * This function will not return with an unlocked mmap_sem. So it has not the
839 * same semantics wrt the @mm->mmap_sem as does filemap_fault().
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700840 */
841int fixup_user_fault(struct task_struct *tsk, struct mm_struct *mm,
Dominik Dingel4a9e1cd2016-01-15 16:57:04 -0800842 unsigned long address, unsigned int fault_flags,
843 bool *unlocked)
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700844{
845 struct vm_area_struct *vma;
Souptick Joarder2b740302018-08-23 17:01:36 -0700846 vm_fault_t ret, major = 0;
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700847
Dominik Dingel4a9e1cd2016-01-15 16:57:04 -0800848 if (unlocked)
849 fault_flags |= FAULT_FLAG_ALLOW_RETRY;
850
851retry:
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700852 vma = find_extend_vma(mm, address);
853 if (!vma || address < vma->vm_start)
854 return -EFAULT;
855
Dave Hansend4925e02016-02-12 13:02:16 -0800856 if (!vma_permits_fault(vma, fault_flags))
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700857 return -EFAULT;
858
Kirill A. Shutemovdcddffd2016-07-26 15:25:18 -0700859 ret = handle_mm_fault(vma, address, fault_flags);
Dominik Dingel4a9e1cd2016-01-15 16:57:04 -0800860 major |= ret & VM_FAULT_MAJOR;
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700861 if (ret & VM_FAULT_ERROR) {
James Morse9a291a72017-06-02 14:46:46 -0700862 int err = vm_fault_to_errno(ret, 0);
863
864 if (err)
865 return err;
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700866 BUG();
867 }
Dominik Dingel4a9e1cd2016-01-15 16:57:04 -0800868
869 if (ret & VM_FAULT_RETRY) {
870 down_read(&mm->mmap_sem);
871 if (!(fault_flags & FAULT_FLAG_TRIED)) {
872 *unlocked = true;
873 fault_flags &= ~FAULT_FLAG_ALLOW_RETRY;
874 fault_flags |= FAULT_FLAG_TRIED;
875 goto retry;
876 }
877 }
878
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700879 if (tsk) {
Dominik Dingel4a9e1cd2016-01-15 16:57:04 -0800880 if (major)
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700881 tsk->maj_flt++;
882 else
883 tsk->min_flt++;
884 }
885 return 0;
886}
Paolo Bonziniadd6a0c2016-06-07 17:51:18 +0200887EXPORT_SYMBOL_GPL(fixup_user_fault);
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -0700888
Andrea Arcangelif0818f42015-02-11 15:27:17 -0800889static __always_inline long __get_user_pages_locked(struct task_struct *tsk,
890 struct mm_struct *mm,
891 unsigned long start,
892 unsigned long nr_pages,
Andrea Arcangelif0818f42015-02-11 15:27:17 -0800893 struct page **pages,
894 struct vm_area_struct **vmas,
Al Viroe7167122017-11-19 11:32:05 -0500895 int *locked,
Andrea Arcangeli0fd71a52015-02-11 15:27:20 -0800896 unsigned int flags)
Andrea Arcangelif0818f42015-02-11 15:27:17 -0800897{
Andrea Arcangelif0818f42015-02-11 15:27:17 -0800898 long ret, pages_done;
899 bool lock_dropped;
900
901 if (locked) {
902 /* if VM_FAULT_RETRY can be returned, vmas become invalid */
903 BUG_ON(vmas);
904 /* check caller initialized locked */
905 BUG_ON(*locked != 1);
906 }
907
908 if (pages)
909 flags |= FOLL_GET;
Andrea Arcangelif0818f42015-02-11 15:27:17 -0800910
911 pages_done = 0;
912 lock_dropped = false;
913 for (;;) {
914 ret = __get_user_pages(tsk, mm, start, nr_pages, flags, pages,
915 vmas, locked);
916 if (!locked)
917 /* VM_FAULT_RETRY couldn't trigger, bypass */
918 return ret;
919
920 /* VM_FAULT_RETRY cannot return errors */
921 if (!*locked) {
922 BUG_ON(ret < 0);
923 BUG_ON(ret >= nr_pages);
924 }
925
926 if (!pages)
927 /* If it's a prefault don't insist harder */
928 return ret;
929
930 if (ret > 0) {
931 nr_pages -= ret;
932 pages_done += ret;
933 if (!nr_pages)
934 break;
935 }
936 if (*locked) {
Andrea Arcangeli96312e62018-03-09 15:51:06 -0800937 /*
938 * VM_FAULT_RETRY didn't trigger or it was a
939 * FOLL_NOWAIT.
940 */
Andrea Arcangelif0818f42015-02-11 15:27:17 -0800941 if (!pages_done)
942 pages_done = ret;
943 break;
944 }
945 /* VM_FAULT_RETRY triggered, so seek to the faulting offset */
946 pages += ret;
947 start += ret << PAGE_SHIFT;
948
949 /*
950 * Repeat on the address that fired VM_FAULT_RETRY
951 * without FAULT_FLAG_ALLOW_RETRY but with
952 * FAULT_FLAG_TRIED.
953 */
954 *locked = 1;
955 lock_dropped = true;
956 down_read(&mm->mmap_sem);
957 ret = __get_user_pages(tsk, mm, start, 1, flags | FOLL_TRIED,
958 pages, NULL, NULL);
959 if (ret != 1) {
960 BUG_ON(ret > 1);
961 if (!pages_done)
962 pages_done = ret;
963 break;
964 }
965 nr_pages--;
966 pages_done++;
967 if (!nr_pages)
968 break;
969 pages++;
970 start += PAGE_SIZE;
971 }
Al Viroe7167122017-11-19 11:32:05 -0500972 if (lock_dropped && *locked) {
Andrea Arcangelif0818f42015-02-11 15:27:17 -0800973 /*
974 * We must let the caller know we temporarily dropped the lock
975 * and so the critical section protected by it was lost.
976 */
977 up_read(&mm->mmap_sem);
978 *locked = 0;
979 }
980 return pages_done;
981}
982
983/*
984 * We can leverage the VM_FAULT_RETRY functionality in the page fault
985 * paths better by using either get_user_pages_locked() or
986 * get_user_pages_unlocked().
987 *
988 * get_user_pages_locked() is suitable to replace the form:
989 *
990 * down_read(&mm->mmap_sem);
991 * do_something()
992 * get_user_pages(tsk, mm, ..., pages, NULL);
993 * up_read(&mm->mmap_sem);
994 *
995 * to:
996 *
997 * int locked = 1;
998 * down_read(&mm->mmap_sem);
999 * do_something()
1000 * get_user_pages_locked(tsk, mm, ..., pages, &locked);
1001 * if (locked)
1002 * up_read(&mm->mmap_sem);
1003 */
Ingo Molnarc12d2da2016-04-04 10:24:58 +02001004long get_user_pages_locked(unsigned long start, unsigned long nr_pages,
Lorenzo Stoakes3b913172016-10-13 01:20:14 +01001005 unsigned int gup_flags, struct page **pages,
Andrea Arcangelif0818f42015-02-11 15:27:17 -08001006 int *locked)
1007{
Dave Hansencde70142016-02-12 13:01:55 -08001008 return __get_user_pages_locked(current, current->mm, start, nr_pages,
Al Viroe7167122017-11-19 11:32:05 -05001009 pages, NULL, locked,
Lorenzo Stoakes3b913172016-10-13 01:20:14 +01001010 gup_flags | FOLL_TOUCH);
Andrea Arcangelif0818f42015-02-11 15:27:17 -08001011}
Ingo Molnarc12d2da2016-04-04 10:24:58 +02001012EXPORT_SYMBOL(get_user_pages_locked);
Andrea Arcangelif0818f42015-02-11 15:27:17 -08001013
1014/*
1015 * get_user_pages_unlocked() is suitable to replace the form:
1016 *
1017 * down_read(&mm->mmap_sem);
1018 * get_user_pages(tsk, mm, ..., pages, NULL);
1019 * up_read(&mm->mmap_sem);
1020 *
1021 * with:
1022 *
1023 * get_user_pages_unlocked(tsk, mm, ..., pages);
1024 *
1025 * It is functionally equivalent to get_user_pages_fast so
Lorenzo Stoakes80a79512016-12-12 16:42:46 -08001026 * get_user_pages_fast should be used instead if specific gup_flags
1027 * (e.g. FOLL_FORCE) are not required.
Andrea Arcangelif0818f42015-02-11 15:27:17 -08001028 */
Ingo Molnarc12d2da2016-04-04 10:24:58 +02001029long get_user_pages_unlocked(unsigned long start, unsigned long nr_pages,
Lorenzo Stoakesc1641542016-10-13 01:20:13 +01001030 struct page **pages, unsigned int gup_flags)
Andrea Arcangelif0818f42015-02-11 15:27:17 -08001031{
Al Viroc803c9c2017-11-18 14:17:46 -05001032 struct mm_struct *mm = current->mm;
1033 int locked = 1;
1034 long ret;
1035
1036 down_read(&mm->mmap_sem);
1037 ret = __get_user_pages_locked(current, mm, start, nr_pages, pages, NULL,
Al Viroe7167122017-11-19 11:32:05 -05001038 &locked, gup_flags | FOLL_TOUCH);
Al Viroc803c9c2017-11-18 14:17:46 -05001039 if (locked)
1040 up_read(&mm->mmap_sem);
1041 return ret;
Andrea Arcangelif0818f42015-02-11 15:27:17 -08001042}
Ingo Molnarc12d2da2016-04-04 10:24:58 +02001043EXPORT_SYMBOL(get_user_pages_unlocked);
Andrea Arcangelif0818f42015-02-11 15:27:17 -08001044
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -07001045/*
Dave Hansen1e987792016-02-12 13:01:54 -08001046 * get_user_pages_remote() - pin user pages in memory
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -07001047 * @tsk: the task_struct to use for page fault accounting, or
1048 * NULL if faults are not to be recorded.
1049 * @mm: mm_struct of target mm
1050 * @start: starting user address
1051 * @nr_pages: number of pages from start to pin
Lorenzo Stoakes9beae1e2016-10-13 01:20:17 +01001052 * @gup_flags: flags modifying lookup behaviour
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -07001053 * @pages: array that receives pointers to the pages pinned.
1054 * Should be at least nr_pages long. Or NULL, if caller
1055 * only intends to ensure the pages are faulted in.
1056 * @vmas: array of pointers to vmas corresponding to each page.
1057 * Or NULL if the caller does not require them.
Lorenzo Stoakes5b56d492016-12-14 15:06:52 -08001058 * @locked: pointer to lock flag indicating whether lock is held and
1059 * subsequently whether VM_FAULT_RETRY functionality can be
1060 * utilised. Lock must initially be held.
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -07001061 *
1062 * Returns number of pages pinned. This may be fewer than the number
1063 * requested. If nr_pages is 0 or negative, returns 0. If no pages
1064 * were pinned, returns -errno. Each page returned must be released
1065 * with a put_page() call when it is finished with. vmas will only
1066 * remain valid while mmap_sem is held.
1067 *
1068 * Must be called with mmap_sem held for read or write.
1069 *
1070 * get_user_pages walks a process's page tables and takes a reference to
1071 * each struct page that each user address corresponds to at a given
1072 * instant. That is, it takes the page that would be accessed if a user
1073 * thread accesses the given user virtual address at that instant.
1074 *
1075 * This does not guarantee that the page exists in the user mappings when
1076 * get_user_pages returns, and there may even be a completely different
1077 * page there in some cases (eg. if mmapped pagecache has been invalidated
1078 * and subsequently re faulted). However it does guarantee that the page
1079 * won't be freed completely. And mostly callers simply care that the page
1080 * contains data that was valid *at some point in time*. Typically, an IO
1081 * or similar operation cannot guarantee anything stronger anyway because
1082 * locks can't be held over the syscall boundary.
1083 *
Lorenzo Stoakes9beae1e2016-10-13 01:20:17 +01001084 * If gup_flags & FOLL_WRITE == 0, the page must not be written to. If the page
1085 * is written to, set_page_dirty (or set_page_dirty_lock, as appropriate) must
1086 * be called after the page is finished with, and before put_page is called.
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -07001087 *
1088 * get_user_pages is typically used for fewer-copy IO operations, to get a
1089 * handle on the memory by some means other than accesses via the user virtual
1090 * addresses. The pages may be submitted for DMA to devices or accessed via
1091 * their kernel linear mapping (via the kmap APIs). Care should be taken to
1092 * use the correct cache flushing APIs.
1093 *
1094 * See also get_user_pages_fast, for performance critical applications.
Andrea Arcangelif0818f42015-02-11 15:27:17 -08001095 *
1096 * get_user_pages should be phased out in favor of
1097 * get_user_pages_locked|unlocked or get_user_pages_fast. Nothing
1098 * should use get_user_pages because it cannot pass
1099 * FAULT_FLAG_ALLOW_RETRY to handle_mm_fault.
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -07001100 */
Dave Hansen1e987792016-02-12 13:01:54 -08001101long get_user_pages_remote(struct task_struct *tsk, struct mm_struct *mm,
1102 unsigned long start, unsigned long nr_pages,
Lorenzo Stoakes9beae1e2016-10-13 01:20:17 +01001103 unsigned int gup_flags, struct page **pages,
Lorenzo Stoakes5b56d492016-12-14 15:06:52 -08001104 struct vm_area_struct **vmas, int *locked)
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -07001105{
Lorenzo Stoakes859110d2016-10-13 01:20:11 +01001106 return __get_user_pages_locked(tsk, mm, start, nr_pages, pages, vmas,
Al Viroe7167122017-11-19 11:32:05 -05001107 locked,
Lorenzo Stoakes9beae1e2016-10-13 01:20:17 +01001108 gup_flags | FOLL_TOUCH | FOLL_REMOTE);
Dave Hansen1e987792016-02-12 13:01:54 -08001109}
1110EXPORT_SYMBOL(get_user_pages_remote);
1111
1112/*
Dave Hansend4edcf02016-02-12 13:01:56 -08001113 * This is the same as get_user_pages_remote(), just with a
1114 * less-flexible calling convention where we assume that the task
Lorenzo Stoakes5b56d492016-12-14 15:06:52 -08001115 * and mm being operated on are the current task's and don't allow
1116 * passing of a locked parameter. We also obviously don't pass
1117 * FOLL_REMOTE in here.
Dave Hansen1e987792016-02-12 13:01:54 -08001118 */
Ingo Molnarc12d2da2016-04-04 10:24:58 +02001119long get_user_pages(unsigned long start, unsigned long nr_pages,
Lorenzo Stoakes768ae302016-10-13 01:20:16 +01001120 unsigned int gup_flags, struct page **pages,
Dave Hansen1e987792016-02-12 13:01:54 -08001121 struct vm_area_struct **vmas)
1122{
Dave Hansencde70142016-02-12 13:01:55 -08001123 return __get_user_pages_locked(current, current->mm, start, nr_pages,
Al Viroe7167122017-11-19 11:32:05 -05001124 pages, vmas, NULL,
Lorenzo Stoakes768ae302016-10-13 01:20:16 +01001125 gup_flags | FOLL_TOUCH);
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -07001126}
Ingo Molnarc12d2da2016-04-04 10:24:58 +02001127EXPORT_SYMBOL(get_user_pages);
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -07001128
Dan Williams2bb6d282017-11-29 16:10:35 -08001129#ifdef CONFIG_FS_DAX
1130/*
1131 * This is the same as get_user_pages() in that it assumes we are
1132 * operating on the current task's mm, but it goes further to validate
1133 * that the vmas associated with the address range are suitable for
1134 * longterm elevated page reference counts. For example, filesystem-dax
1135 * mappings are subject to the lifetime enforced by the filesystem and
1136 * we need guarantees that longterm users like RDMA and V4L2 only
1137 * establish mappings that have a kernel enforced revocation mechanism.
1138 *
1139 * "longterm" == userspace controlled elevated page count lifetime.
1140 * Contrast this to iov_iter_get_pages() usages which are transient.
1141 */
1142long get_user_pages_longterm(unsigned long start, unsigned long nr_pages,
1143 unsigned int gup_flags, struct page **pages,
1144 struct vm_area_struct **vmas_arg)
1145{
1146 struct vm_area_struct **vmas = vmas_arg;
1147 struct vm_area_struct *vma_prev = NULL;
1148 long rc, i;
1149
1150 if (!pages)
1151 return -EINVAL;
1152
1153 if (!vmas) {
1154 vmas = kcalloc(nr_pages, sizeof(struct vm_area_struct *),
1155 GFP_KERNEL);
1156 if (!vmas)
1157 return -ENOMEM;
1158 }
1159
1160 rc = get_user_pages(start, nr_pages, gup_flags, pages, vmas);
1161
1162 for (i = 0; i < rc; i++) {
1163 struct vm_area_struct *vma = vmas[i];
1164
1165 if (vma == vma_prev)
1166 continue;
1167
1168 vma_prev = vma;
1169
1170 if (vma_is_fsdax(vma))
1171 break;
1172 }
1173
1174 /*
1175 * Either get_user_pages() failed, or the vma validation
1176 * succeeded, in either case we don't need to put_page() before
1177 * returning.
1178 */
1179 if (i >= rc)
1180 goto out;
1181
1182 for (i = 0; i < rc; i++)
1183 put_page(pages[i]);
1184 rc = -EOPNOTSUPP;
1185out:
1186 if (vmas != vmas_arg)
1187 kfree(vmas);
1188 return rc;
1189}
1190EXPORT_SYMBOL(get_user_pages_longterm);
1191#endif /* CONFIG_FS_DAX */
1192
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -07001193/**
Kirill A. Shutemovacc3c8d2015-04-14 15:44:45 -07001194 * populate_vma_page_range() - populate a range of pages in the vma.
1195 * @vma: target vma
1196 * @start: start address
1197 * @end: end address
1198 * @nonblocking:
1199 *
1200 * This takes care of mlocking the pages too if VM_LOCKED is set.
1201 *
1202 * return 0 on success, negative error code on error.
1203 *
1204 * vma->vm_mm->mmap_sem must be held.
1205 *
1206 * If @nonblocking is NULL, it may be held for read or write and will
1207 * be unperturbed.
1208 *
1209 * If @nonblocking is non-NULL, it must held for read only and may be
1210 * released. If it's released, *@nonblocking will be set to 0.
1211 */
1212long populate_vma_page_range(struct vm_area_struct *vma,
1213 unsigned long start, unsigned long end, int *nonblocking)
1214{
1215 struct mm_struct *mm = vma->vm_mm;
1216 unsigned long nr_pages = (end - start) / PAGE_SIZE;
1217 int gup_flags;
1218
1219 VM_BUG_ON(start & ~PAGE_MASK);
1220 VM_BUG_ON(end & ~PAGE_MASK);
1221 VM_BUG_ON_VMA(start < vma->vm_start, vma);
1222 VM_BUG_ON_VMA(end > vma->vm_end, vma);
1223 VM_BUG_ON_MM(!rwsem_is_locked(&mm->mmap_sem), mm);
1224
Eric B Munsonde60f5f2015-11-05 18:51:36 -08001225 gup_flags = FOLL_TOUCH | FOLL_POPULATE | FOLL_MLOCK;
1226 if (vma->vm_flags & VM_LOCKONFAULT)
1227 gup_flags &= ~FOLL_POPULATE;
Kirill A. Shutemovacc3c8d2015-04-14 15:44:45 -07001228 /*
1229 * We want to touch writable mappings with a write fault in order
1230 * to break COW, except for shared mappings because these don't COW
1231 * and we would not want to dirty them for nothing.
1232 */
1233 if ((vma->vm_flags & (VM_WRITE | VM_SHARED)) == VM_WRITE)
1234 gup_flags |= FOLL_WRITE;
1235
1236 /*
1237 * We want mlock to succeed for regions that have any permissions
1238 * other than PROT_NONE.
1239 */
1240 if (vma->vm_flags & (VM_READ | VM_WRITE | VM_EXEC))
1241 gup_flags |= FOLL_FORCE;
1242
1243 /*
1244 * We made sure addr is within a VMA, so the following will
1245 * not result in a stack expansion that recurses back here.
1246 */
1247 return __get_user_pages(current, mm, start, nr_pages, gup_flags,
1248 NULL, NULL, nonblocking);
1249}
1250
1251/*
1252 * __mm_populate - populate and/or mlock pages within a range of address space.
1253 *
1254 * This is used to implement mlock() and the MAP_POPULATE / MAP_LOCKED mmap
1255 * flags. VMAs must be already marked with the desired vm_flags, and
1256 * mmap_sem must not be held.
1257 */
1258int __mm_populate(unsigned long start, unsigned long len, int ignore_errors)
1259{
1260 struct mm_struct *mm = current->mm;
1261 unsigned long end, nstart, nend;
1262 struct vm_area_struct *vma = NULL;
1263 int locked = 0;
1264 long ret = 0;
1265
Kirill A. Shutemovacc3c8d2015-04-14 15:44:45 -07001266 end = start + len;
1267
1268 for (nstart = start; nstart < end; nstart = nend) {
1269 /*
1270 * We want to fault in pages for [nstart; end) address range.
1271 * Find first corresponding VMA.
1272 */
1273 if (!locked) {
1274 locked = 1;
1275 down_read(&mm->mmap_sem);
1276 vma = find_vma(mm, nstart);
1277 } else if (nstart >= vma->vm_end)
1278 vma = vma->vm_next;
1279 if (!vma || vma->vm_start >= end)
1280 break;
1281 /*
1282 * Set [nstart; nend) to intersection of desired address
1283 * range with the first VMA. Also, skip undesirable VMA types.
1284 */
1285 nend = min(end, vma->vm_end);
1286 if (vma->vm_flags & (VM_IO | VM_PFNMAP))
1287 continue;
1288 if (nstart < vma->vm_start)
1289 nstart = vma->vm_start;
1290 /*
1291 * Now fault in a range of pages. populate_vma_page_range()
1292 * double checks the vma flags, so that it won't mlock pages
1293 * if the vma was already munlocked.
1294 */
1295 ret = populate_vma_page_range(vma, nstart, nend, &locked);
1296 if (ret < 0) {
1297 if (ignore_errors) {
1298 ret = 0;
1299 continue; /* continue at next VMA */
1300 }
1301 break;
1302 }
1303 nend = nstart + ret * PAGE_SIZE;
1304 ret = 0;
1305 }
1306 if (locked)
1307 up_read(&mm->mmap_sem);
1308 return ret; /* 0 or negative error code */
1309}
1310
1311/**
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -07001312 * get_dump_page() - pin user page in memory while writing it to core dump
1313 * @addr: user address
1314 *
1315 * Returns struct page pointer of user page pinned for dump,
Kirill A. Shutemovea1754a2016-04-01 15:29:48 +03001316 * to be freed afterwards by put_page().
Kirill A. Shutemov4bbd4c72014-06-04 16:08:10 -07001317 *
1318 * Returns NULL on any kind of failure - a hole must then be inserted into
1319 * the corefile, to preserve alignment with its headers; and also returns
1320 * NULL wherever the ZERO_PAGE, or an anonymous pte_none, has been found -
1321 * allowing a hole to be left in the corefile to save diskspace.
1322 *
1323 * Called without mmap_sem, but after all other threads have been killed.
1324 */
1325#ifdef CONFIG_ELF_CORE
1326struct page *get_dump_page(unsigned long addr)
1327{
1328 struct vm_area_struct *vma;
1329 struct page *page;
1330
1331 if (__get_user_pages(current, current->mm, addr, 1,
1332 FOLL_FORCE | FOLL_DUMP | FOLL_GET, &page, &vma,
1333 NULL) < 1)
1334 return NULL;
1335 flush_cache_page(vma, addr, page_to_pfn(page));
1336 return page;
1337}
1338#endif /* CONFIG_ELF_CORE */
Steve Capper2667f502014-10-09 15:29:14 -07001339
1340/*
Kirill A. Shutemove5855132017-06-06 14:31:20 +03001341 * Generic Fast GUP
Steve Capper2667f502014-10-09 15:29:14 -07001342 *
1343 * get_user_pages_fast attempts to pin user pages by walking the page
1344 * tables directly and avoids taking locks. Thus the walker needs to be
1345 * protected from page table pages being freed from under it, and should
1346 * block any THP splits.
1347 *
1348 * One way to achieve this is to have the walker disable interrupts, and
1349 * rely on IPIs from the TLB flushing code blocking before the page table
1350 * pages are freed. This is unsuitable for architectures that do not need
1351 * to broadcast an IPI when invalidating TLBs.
1352 *
1353 * Another way to achieve this is to batch up page table containing pages
1354 * belonging to more than one mm_user, then rcu_sched a callback to free those
1355 * pages. Disabling interrupts will allow the fast_gup walker to both block
1356 * the rcu_sched callback, and an IPI that we broadcast for splitting THPs
1357 * (which is a relatively rare event). The code below adopts this strategy.
1358 *
1359 * Before activating this code, please be aware that the following assumptions
1360 * are currently made:
1361 *
Kirill A. Shutemove5855132017-06-06 14:31:20 +03001362 * *) Either HAVE_RCU_TABLE_FREE is enabled, and tlb_remove_table() is used to
1363 * free pages containing page tables or TLB flushing requires IPI broadcast.
Steve Capper2667f502014-10-09 15:29:14 -07001364 *
Steve Capper2667f502014-10-09 15:29:14 -07001365 * *) ptes can be read atomically by the architecture.
1366 *
1367 * *) access_ok is sufficient to validate userspace address ranges.
1368 *
1369 * The last two assumptions can be relaxed by the addition of helper functions.
1370 *
1371 * This code is based heavily on the PowerPC implementation by Nick Piggin.
1372 */
Kirill A. Shutemove5855132017-06-06 14:31:20 +03001373#ifdef CONFIG_HAVE_GENERIC_GUP
Steve Capper2667f502014-10-09 15:29:14 -07001374
Kirill A. Shutemov0005d202017-03-16 18:26:51 +03001375#ifndef gup_get_pte
1376/*
1377 * We assume that the PTE can be read atomically. If this is not the case for
1378 * your architecture, please provide the helper.
1379 */
1380static inline pte_t gup_get_pte(pte_t *ptep)
1381{
1382 return READ_ONCE(*ptep);
1383}
1384#endif
1385
Kirill A. Shutemovb59f65f2017-03-16 18:26:53 +03001386static void undo_dev_pagemap(int *nr, int nr_start, struct page **pages)
1387{
1388 while ((*nr) - nr_start) {
1389 struct page *page = pages[--(*nr)];
1390
1391 ClearPageReferenced(page);
1392 put_page(page);
1393 }
1394}
1395
Laurent Dufour3010a5e2018-06-07 17:06:08 -07001396#ifdef CONFIG_ARCH_HAS_PTE_SPECIAL
Steve Capper2667f502014-10-09 15:29:14 -07001397static int gup_pte_range(pmd_t pmd, unsigned long addr, unsigned long end,
1398 int write, struct page **pages, int *nr)
1399{
Kirill A. Shutemovb59f65f2017-03-16 18:26:53 +03001400 struct dev_pagemap *pgmap = NULL;
1401 int nr_start = *nr, ret = 0;
Steve Capper2667f502014-10-09 15:29:14 -07001402 pte_t *ptep, *ptem;
Steve Capper2667f502014-10-09 15:29:14 -07001403
1404 ptem = ptep = pte_offset_map(&pmd, addr);
1405 do {
Kirill A. Shutemov0005d202017-03-16 18:26:51 +03001406 pte_t pte = gup_get_pte(ptep);
Kirill A. Shutemov7aef4172016-01-15 16:52:32 -08001407 struct page *head, *page;
Steve Capper2667f502014-10-09 15:29:14 -07001408
1409 /*
1410 * Similar to the PMD case below, NUMA hinting must take slow
Mel Gorman8a0516e2015-02-12 14:58:22 -08001411 * path using the pte_protnone check.
Steve Capper2667f502014-10-09 15:29:14 -07001412 */
Kirill A. Shutemove7884f82017-03-16 18:26:50 +03001413 if (pte_protnone(pte))
1414 goto pte_unmap;
1415
1416 if (!pte_access_permitted(pte, write))
1417 goto pte_unmap;
1418
Kirill A. Shutemovb59f65f2017-03-16 18:26:53 +03001419 if (pte_devmap(pte)) {
1420 pgmap = get_dev_pagemap(pte_pfn(pte), pgmap);
1421 if (unlikely(!pgmap)) {
1422 undo_dev_pagemap(nr, nr_start, pages);
1423 goto pte_unmap;
1424 }
1425 } else if (pte_special(pte))
Steve Capper2667f502014-10-09 15:29:14 -07001426 goto pte_unmap;
1427
1428 VM_BUG_ON(!pfn_valid(pte_pfn(pte)));
1429 page = pte_page(pte);
Kirill A. Shutemov7aef4172016-01-15 16:52:32 -08001430 head = compound_head(page);
Steve Capper2667f502014-10-09 15:29:14 -07001431
Kirill A. Shutemov7aef4172016-01-15 16:52:32 -08001432 if (!page_cache_get_speculative(head))
Steve Capper2667f502014-10-09 15:29:14 -07001433 goto pte_unmap;
1434
1435 if (unlikely(pte_val(pte) != pte_val(*ptep))) {
Kirill A. Shutemov7aef4172016-01-15 16:52:32 -08001436 put_page(head);
Steve Capper2667f502014-10-09 15:29:14 -07001437 goto pte_unmap;
1438 }
1439
Kirill A. Shutemov7aef4172016-01-15 16:52:32 -08001440 VM_BUG_ON_PAGE(compound_head(page) != head, page);
Kirill A. Shutemove9348052017-03-16 18:26:52 +03001441
1442 SetPageReferenced(page);
Steve Capper2667f502014-10-09 15:29:14 -07001443 pages[*nr] = page;
1444 (*nr)++;
1445
1446 } while (ptep++, addr += PAGE_SIZE, addr != end);
1447
1448 ret = 1;
1449
1450pte_unmap:
Christoph Hellwig832d7aa2017-12-29 08:54:01 +01001451 if (pgmap)
1452 put_dev_pagemap(pgmap);
Steve Capper2667f502014-10-09 15:29:14 -07001453 pte_unmap(ptem);
1454 return ret;
1455}
1456#else
1457
1458/*
1459 * If we can't determine whether or not a pte is special, then fail immediately
1460 * for ptes. Note, we can still pin HugeTLB and THP as these are guaranteed not
1461 * to be special.
1462 *
1463 * For a futex to be placed on a THP tail page, get_futex_key requires a
1464 * __get_user_pages_fast implementation that can pin pages. Thus it's still
1465 * useful to have gup_huge_pmd even if we can't operate on ptes.
1466 */
1467static int gup_pte_range(pmd_t pmd, unsigned long addr, unsigned long end,
1468 int write, struct page **pages, int *nr)
1469{
1470 return 0;
1471}
Laurent Dufour3010a5e2018-06-07 17:06:08 -07001472#endif /* CONFIG_ARCH_HAS_PTE_SPECIAL */
Steve Capper2667f502014-10-09 15:29:14 -07001473
Oliver O'Halloran09180ca2017-09-06 16:20:58 -07001474#if defined(__HAVE_ARCH_PTE_DEVMAP) && defined(CONFIG_TRANSPARENT_HUGEPAGE)
Kirill A. Shutemovb59f65f2017-03-16 18:26:53 +03001475static int __gup_device_huge(unsigned long pfn, unsigned long addr,
1476 unsigned long end, struct page **pages, int *nr)
1477{
1478 int nr_start = *nr;
1479 struct dev_pagemap *pgmap = NULL;
1480
1481 do {
1482 struct page *page = pfn_to_page(pfn);
1483
1484 pgmap = get_dev_pagemap(pfn, pgmap);
1485 if (unlikely(!pgmap)) {
1486 undo_dev_pagemap(nr, nr_start, pages);
1487 return 0;
1488 }
1489 SetPageReferenced(page);
1490 pages[*nr] = page;
1491 get_page(page);
Kirill A. Shutemovb59f65f2017-03-16 18:26:53 +03001492 (*nr)++;
1493 pfn++;
1494 } while (addr += PAGE_SIZE, addr != end);
Christoph Hellwig832d7aa2017-12-29 08:54:01 +01001495
1496 if (pgmap)
1497 put_dev_pagemap(pgmap);
Kirill A. Shutemovb59f65f2017-03-16 18:26:53 +03001498 return 1;
1499}
1500
Dan Williamsa9b6de72018-04-19 21:32:19 -07001501static int __gup_device_huge_pmd(pmd_t orig, pmd_t *pmdp, unsigned long addr,
Kirill A. Shutemovb59f65f2017-03-16 18:26:53 +03001502 unsigned long end, struct page **pages, int *nr)
1503{
1504 unsigned long fault_pfn;
Dan Williamsa9b6de72018-04-19 21:32:19 -07001505 int nr_start = *nr;
Kirill A. Shutemovb59f65f2017-03-16 18:26:53 +03001506
Dan Williamsa9b6de72018-04-19 21:32:19 -07001507 fault_pfn = pmd_pfn(orig) + ((addr & ~PMD_MASK) >> PAGE_SHIFT);
1508 if (!__gup_device_huge(fault_pfn, addr, end, pages, nr))
1509 return 0;
1510
1511 if (unlikely(pmd_val(orig) != pmd_val(*pmdp))) {
1512 undo_dev_pagemap(nr, nr_start, pages);
1513 return 0;
1514 }
1515 return 1;
Kirill A. Shutemovb59f65f2017-03-16 18:26:53 +03001516}
1517
Dan Williamsa9b6de72018-04-19 21:32:19 -07001518static int __gup_device_huge_pud(pud_t orig, pud_t *pudp, unsigned long addr,
Kirill A. Shutemovb59f65f2017-03-16 18:26:53 +03001519 unsigned long end, struct page **pages, int *nr)
1520{
1521 unsigned long fault_pfn;
Dan Williamsa9b6de72018-04-19 21:32:19 -07001522 int nr_start = *nr;
Kirill A. Shutemovb59f65f2017-03-16 18:26:53 +03001523
Dan Williamsa9b6de72018-04-19 21:32:19 -07001524 fault_pfn = pud_pfn(orig) + ((addr & ~PUD_MASK) >> PAGE_SHIFT);
1525 if (!__gup_device_huge(fault_pfn, addr, end, pages, nr))
1526 return 0;
1527
1528 if (unlikely(pud_val(orig) != pud_val(*pudp))) {
1529 undo_dev_pagemap(nr, nr_start, pages);
1530 return 0;
1531 }
1532 return 1;
Kirill A. Shutemovb59f65f2017-03-16 18:26:53 +03001533}
1534#else
Dan Williamsa9b6de72018-04-19 21:32:19 -07001535static int __gup_device_huge_pmd(pmd_t orig, pmd_t *pmdp, unsigned long addr,
Kirill A. Shutemovb59f65f2017-03-16 18:26:53 +03001536 unsigned long end, struct page **pages, int *nr)
1537{
1538 BUILD_BUG();
1539 return 0;
1540}
1541
Dan Williamsa9b6de72018-04-19 21:32:19 -07001542static int __gup_device_huge_pud(pud_t pud, pud_t *pudp, unsigned long addr,
Kirill A. Shutemovb59f65f2017-03-16 18:26:53 +03001543 unsigned long end, struct page **pages, int *nr)
1544{
1545 BUILD_BUG();
1546 return 0;
1547}
1548#endif
1549
Steve Capper2667f502014-10-09 15:29:14 -07001550static int gup_huge_pmd(pmd_t orig, pmd_t *pmdp, unsigned long addr,
1551 unsigned long end, int write, struct page **pages, int *nr)
1552{
Kirill A. Shutemovddc58f22016-01-15 16:52:56 -08001553 struct page *head, *page;
Steve Capper2667f502014-10-09 15:29:14 -07001554 int refs;
1555
Kirill A. Shutemove7884f82017-03-16 18:26:50 +03001556 if (!pmd_access_permitted(orig, write))
Steve Capper2667f502014-10-09 15:29:14 -07001557 return 0;
1558
Kirill A. Shutemovb59f65f2017-03-16 18:26:53 +03001559 if (pmd_devmap(orig))
Dan Williamsa9b6de72018-04-19 21:32:19 -07001560 return __gup_device_huge_pmd(orig, pmdp, addr, end, pages, nr);
Kirill A. Shutemovb59f65f2017-03-16 18:26:53 +03001561
Steve Capper2667f502014-10-09 15:29:14 -07001562 refs = 0;
Punit Agrawald63206e2017-07-06 15:39:39 -07001563 page = pmd_page(orig) + ((addr & ~PMD_MASK) >> PAGE_SHIFT);
Steve Capper2667f502014-10-09 15:29:14 -07001564 do {
Steve Capper2667f502014-10-09 15:29:14 -07001565 pages[*nr] = page;
1566 (*nr)++;
1567 page++;
1568 refs++;
1569 } while (addr += PAGE_SIZE, addr != end);
1570
Punit Agrawald63206e2017-07-06 15:39:39 -07001571 head = compound_head(pmd_page(orig));
Steve Capper2667f502014-10-09 15:29:14 -07001572 if (!page_cache_add_speculative(head, refs)) {
1573 *nr -= refs;
1574 return 0;
1575 }
1576
1577 if (unlikely(pmd_val(orig) != pmd_val(*pmdp))) {
1578 *nr -= refs;
1579 while (refs--)
1580 put_page(head);
1581 return 0;
1582 }
1583
Kirill A. Shutemove9348052017-03-16 18:26:52 +03001584 SetPageReferenced(head);
Steve Capper2667f502014-10-09 15:29:14 -07001585 return 1;
1586}
1587
1588static int gup_huge_pud(pud_t orig, pud_t *pudp, unsigned long addr,
1589 unsigned long end, int write, struct page **pages, int *nr)
1590{
Kirill A. Shutemovddc58f22016-01-15 16:52:56 -08001591 struct page *head, *page;
Steve Capper2667f502014-10-09 15:29:14 -07001592 int refs;
1593
Kirill A. Shutemove7884f82017-03-16 18:26:50 +03001594 if (!pud_access_permitted(orig, write))
Steve Capper2667f502014-10-09 15:29:14 -07001595 return 0;
1596
Kirill A. Shutemovb59f65f2017-03-16 18:26:53 +03001597 if (pud_devmap(orig))
Dan Williamsa9b6de72018-04-19 21:32:19 -07001598 return __gup_device_huge_pud(orig, pudp, addr, end, pages, nr);
Kirill A. Shutemovb59f65f2017-03-16 18:26:53 +03001599
Steve Capper2667f502014-10-09 15:29:14 -07001600 refs = 0;
Punit Agrawald63206e2017-07-06 15:39:39 -07001601 page = pud_page(orig) + ((addr & ~PUD_MASK) >> PAGE_SHIFT);
Steve Capper2667f502014-10-09 15:29:14 -07001602 do {
Steve Capper2667f502014-10-09 15:29:14 -07001603 pages[*nr] = page;
1604 (*nr)++;
1605 page++;
1606 refs++;
1607 } while (addr += PAGE_SIZE, addr != end);
1608
Punit Agrawald63206e2017-07-06 15:39:39 -07001609 head = compound_head(pud_page(orig));
Steve Capper2667f502014-10-09 15:29:14 -07001610 if (!page_cache_add_speculative(head, refs)) {
1611 *nr -= refs;
1612 return 0;
1613 }
1614
1615 if (unlikely(pud_val(orig) != pud_val(*pudp))) {
1616 *nr -= refs;
1617 while (refs--)
1618 put_page(head);
1619 return 0;
1620 }
1621
Kirill A. Shutemove9348052017-03-16 18:26:52 +03001622 SetPageReferenced(head);
Steve Capper2667f502014-10-09 15:29:14 -07001623 return 1;
1624}
1625
Aneesh Kumar K.Vf30c59e2014-11-05 21:57:40 +05301626static int gup_huge_pgd(pgd_t orig, pgd_t *pgdp, unsigned long addr,
1627 unsigned long end, int write,
1628 struct page **pages, int *nr)
1629{
1630 int refs;
Kirill A. Shutemovddc58f22016-01-15 16:52:56 -08001631 struct page *head, *page;
Aneesh Kumar K.Vf30c59e2014-11-05 21:57:40 +05301632
Kirill A. Shutemove7884f82017-03-16 18:26:50 +03001633 if (!pgd_access_permitted(orig, write))
Aneesh Kumar K.Vf30c59e2014-11-05 21:57:40 +05301634 return 0;
1635
Kirill A. Shutemovb59f65f2017-03-16 18:26:53 +03001636 BUILD_BUG_ON(pgd_devmap(orig));
Aneesh Kumar K.Vf30c59e2014-11-05 21:57:40 +05301637 refs = 0;
Punit Agrawald63206e2017-07-06 15:39:39 -07001638 page = pgd_page(orig) + ((addr & ~PGDIR_MASK) >> PAGE_SHIFT);
Aneesh Kumar K.Vf30c59e2014-11-05 21:57:40 +05301639 do {
Aneesh Kumar K.Vf30c59e2014-11-05 21:57:40 +05301640 pages[*nr] = page;
1641 (*nr)++;
1642 page++;
1643 refs++;
1644 } while (addr += PAGE_SIZE, addr != end);
1645
Punit Agrawald63206e2017-07-06 15:39:39 -07001646 head = compound_head(pgd_page(orig));
Aneesh Kumar K.Vf30c59e2014-11-05 21:57:40 +05301647 if (!page_cache_add_speculative(head, refs)) {
1648 *nr -= refs;
1649 return 0;
1650 }
1651
1652 if (unlikely(pgd_val(orig) != pgd_val(*pgdp))) {
1653 *nr -= refs;
1654 while (refs--)
1655 put_page(head);
1656 return 0;
1657 }
1658
Kirill A. Shutemove9348052017-03-16 18:26:52 +03001659 SetPageReferenced(head);
Aneesh Kumar K.Vf30c59e2014-11-05 21:57:40 +05301660 return 1;
1661}
1662
Steve Capper2667f502014-10-09 15:29:14 -07001663static int gup_pmd_range(pud_t pud, unsigned long addr, unsigned long end,
1664 int write, struct page **pages, int *nr)
1665{
1666 unsigned long next;
1667 pmd_t *pmdp;
1668
1669 pmdp = pmd_offset(&pud, addr);
1670 do {
Christian Borntraeger38c5ce92015-01-06 22:54:46 +01001671 pmd_t pmd = READ_ONCE(*pmdp);
Steve Capper2667f502014-10-09 15:29:14 -07001672
1673 next = pmd_addr_end(addr, end);
Zi Yan84c3fc42017-09-08 16:11:01 -07001674 if (!pmd_present(pmd))
Steve Capper2667f502014-10-09 15:29:14 -07001675 return 0;
1676
1677 if (unlikely(pmd_trans_huge(pmd) || pmd_huge(pmd))) {
1678 /*
1679 * NUMA hinting faults need to be handled in the GUP
1680 * slowpath for accounting purposes and so that they
1681 * can be serialised against THP migration.
1682 */
Mel Gorman8a0516e2015-02-12 14:58:22 -08001683 if (pmd_protnone(pmd))
Steve Capper2667f502014-10-09 15:29:14 -07001684 return 0;
1685
1686 if (!gup_huge_pmd(pmd, pmdp, addr, next, write,
1687 pages, nr))
1688 return 0;
1689
Aneesh Kumar K.Vf30c59e2014-11-05 21:57:40 +05301690 } else if (unlikely(is_hugepd(__hugepd(pmd_val(pmd))))) {
1691 /*
1692 * architecture have different format for hugetlbfs
1693 * pmd format and THP pmd format
1694 */
1695 if (!gup_huge_pd(__hugepd(pmd_val(pmd)), addr,
1696 PMD_SHIFT, next, write, pages, nr))
1697 return 0;
Steve Capper2667f502014-10-09 15:29:14 -07001698 } else if (!gup_pte_range(pmd, addr, next, write, pages, nr))
Mario Leinweber29231172018-04-05 16:24:18 -07001699 return 0;
Steve Capper2667f502014-10-09 15:29:14 -07001700 } while (pmdp++, addr = next, addr != end);
1701
1702 return 1;
1703}
1704
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +03001705static int gup_pud_range(p4d_t p4d, unsigned long addr, unsigned long end,
Aneesh Kumar K.Vf30c59e2014-11-05 21:57:40 +05301706 int write, struct page **pages, int *nr)
Steve Capper2667f502014-10-09 15:29:14 -07001707{
1708 unsigned long next;
1709 pud_t *pudp;
1710
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +03001711 pudp = pud_offset(&p4d, addr);
Steve Capper2667f502014-10-09 15:29:14 -07001712 do {
Christian Borntraegere37c6982014-12-07 21:41:33 +01001713 pud_t pud = READ_ONCE(*pudp);
Steve Capper2667f502014-10-09 15:29:14 -07001714
1715 next = pud_addr_end(addr, end);
1716 if (pud_none(pud))
1717 return 0;
Aneesh Kumar K.Vf30c59e2014-11-05 21:57:40 +05301718 if (unlikely(pud_huge(pud))) {
Steve Capper2667f502014-10-09 15:29:14 -07001719 if (!gup_huge_pud(pud, pudp, addr, next, write,
Aneesh Kumar K.Vf30c59e2014-11-05 21:57:40 +05301720 pages, nr))
1721 return 0;
1722 } else if (unlikely(is_hugepd(__hugepd(pud_val(pud))))) {
1723 if (!gup_huge_pd(__hugepd(pud_val(pud)), addr,
1724 PUD_SHIFT, next, write, pages, nr))
Steve Capper2667f502014-10-09 15:29:14 -07001725 return 0;
1726 } else if (!gup_pmd_range(pud, addr, next, write, pages, nr))
1727 return 0;
1728 } while (pudp++, addr = next, addr != end);
1729
1730 return 1;
1731}
1732
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +03001733static int gup_p4d_range(pgd_t pgd, unsigned long addr, unsigned long end,
1734 int write, struct page **pages, int *nr)
1735{
1736 unsigned long next;
1737 p4d_t *p4dp;
1738
1739 p4dp = p4d_offset(&pgd, addr);
1740 do {
1741 p4d_t p4d = READ_ONCE(*p4dp);
1742
1743 next = p4d_addr_end(addr, end);
1744 if (p4d_none(p4d))
1745 return 0;
1746 BUILD_BUG_ON(p4d_huge(p4d));
1747 if (unlikely(is_hugepd(__hugepd(p4d_val(p4d))))) {
1748 if (!gup_huge_pd(__hugepd(p4d_val(p4d)), addr,
1749 P4D_SHIFT, next, write, pages, nr))
1750 return 0;
Kirill A. Shutemovce70df02017-03-13 08:22:13 +03001751 } else if (!gup_pud_range(p4d, addr, next, write, pages, nr))
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +03001752 return 0;
1753 } while (p4dp++, addr = next, addr != end);
1754
1755 return 1;
1756}
1757
Kirill A. Shutemov5b65c4672017-09-09 00:56:03 +03001758static void gup_pgd_range(unsigned long addr, unsigned long end,
1759 int write, struct page **pages, int *nr)
1760{
1761 unsigned long next;
1762 pgd_t *pgdp;
1763
1764 pgdp = pgd_offset(current->mm, addr);
1765 do {
1766 pgd_t pgd = READ_ONCE(*pgdp);
1767
1768 next = pgd_addr_end(addr, end);
1769 if (pgd_none(pgd))
1770 return;
1771 if (unlikely(pgd_huge(pgd))) {
1772 if (!gup_huge_pgd(pgd, pgdp, addr, next, write,
1773 pages, nr))
1774 return;
1775 } else if (unlikely(is_hugepd(__hugepd(pgd_val(pgd))))) {
1776 if (!gup_huge_pd(__hugepd(pgd_val(pgd)), addr,
1777 PGDIR_SHIFT, next, write, pages, nr))
1778 return;
1779 } else if (!gup_p4d_range(pgd, addr, next, write, pages, nr))
1780 return;
1781 } while (pgdp++, addr = next, addr != end);
1782}
1783
1784#ifndef gup_fast_permitted
1785/*
1786 * Check if it's allowed to use __get_user_pages_fast() for the range, or
1787 * we need to fall back to the slow version:
1788 */
1789bool gup_fast_permitted(unsigned long start, int nr_pages, int write)
1790{
1791 unsigned long len, end;
1792
1793 len = (unsigned long) nr_pages << PAGE_SHIFT;
1794 end = start + len;
1795 return end >= start;
1796}
1797#endif
1798
Steve Capper2667f502014-10-09 15:29:14 -07001799/*
1800 * 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 -07001801 * the regular GUP.
1802 * Note a difference with get_user_pages_fast: this always returns the
1803 * number of pages pinned, 0 if no pages were pinned.
Steve Capper2667f502014-10-09 15:29:14 -07001804 */
1805int __get_user_pages_fast(unsigned long start, int nr_pages, int write,
1806 struct page **pages)
1807{
Wei Yangd4faa402018-10-26 15:07:55 -07001808 unsigned long len, end;
Kirill A. Shutemov5b65c4672017-09-09 00:56:03 +03001809 unsigned long flags;
Steve Capper2667f502014-10-09 15:29:14 -07001810 int nr = 0;
1811
1812 start &= PAGE_MASK;
Steve Capper2667f502014-10-09 15:29:14 -07001813 len = (unsigned long) nr_pages << PAGE_SHIFT;
1814 end = start + len;
1815
1816 if (unlikely(!access_ok(write ? VERIFY_WRITE : VERIFY_READ,
Arnd Bergmannaa2369f2017-05-03 14:56:12 -07001817 (void __user *)start, len)))
Steve Capper2667f502014-10-09 15:29:14 -07001818 return 0;
1819
1820 /*
1821 * Disable interrupts. We use the nested form as we can already have
1822 * interrupts disabled by get_futex_key.
1823 *
1824 * With interrupts disabled, we block page table pages from being
Fengguang Wu2ebe8222018-10-30 15:10:51 -07001825 * freed from under us. See struct mmu_table_batch comments in
1826 * include/asm-generic/tlb.h for more details.
Steve Capper2667f502014-10-09 15:29:14 -07001827 *
1828 * We do not adopt an rcu_read_lock(.) here as we also want to
1829 * block IPIs that come from THPs splitting.
1830 */
1831
Kirill A. Shutemov5b65c4672017-09-09 00:56:03 +03001832 if (gup_fast_permitted(start, nr_pages, write)) {
1833 local_irq_save(flags);
Wei Yangd4faa402018-10-26 15:07:55 -07001834 gup_pgd_range(start, end, write, pages, &nr);
Kirill A. Shutemov5b65c4672017-09-09 00:56:03 +03001835 local_irq_restore(flags);
1836 }
Steve Capper2667f502014-10-09 15:29:14 -07001837
1838 return nr;
1839}
1840
1841/**
1842 * get_user_pages_fast() - pin user pages in memory
1843 * @start: starting user address
1844 * @nr_pages: number of pages from start to pin
1845 * @write: whether pages will be written to
1846 * @pages: array that receives pointers to the pages pinned.
1847 * Should be at least nr_pages long.
1848 *
1849 * Attempt to pin user pages in memory without taking mm->mmap_sem.
1850 * If not successful, it will fall back to taking the lock and
1851 * calling get_user_pages().
1852 *
1853 * Returns number of pages pinned. This may be fewer than the number
1854 * requested. If nr_pages is 0 or negative, returns 0. If no pages
1855 * were pinned, returns -errno.
1856 */
1857int get_user_pages_fast(unsigned long start, int nr_pages, int write,
1858 struct page **pages)
1859{
Kirill A. Shutemov5b65c4672017-09-09 00:56:03 +03001860 unsigned long addr, len, end;
Kirill A. Shutemov73e10a62017-03-16 18:26:54 +03001861 int nr = 0, ret = 0;
Steve Capper2667f502014-10-09 15:29:14 -07001862
1863 start &= PAGE_MASK;
Kirill A. Shutemov5b65c4672017-09-09 00:56:03 +03001864 addr = start;
1865 len = (unsigned long) nr_pages << PAGE_SHIFT;
1866 end = start + len;
1867
Michael S. Tsirkinc61611f2018-04-13 15:35:20 -07001868 if (nr_pages <= 0)
1869 return 0;
1870
Kirill A. Shutemov5b65c4672017-09-09 00:56:03 +03001871 if (unlikely(!access_ok(write ? VERIFY_WRITE : VERIFY_READ,
1872 (void __user *)start, len)))
Michael S. Tsirkinc61611f2018-04-13 15:35:20 -07001873 return -EFAULT;
Kirill A. Shutemov73e10a62017-03-16 18:26:54 +03001874
1875 if (gup_fast_permitted(start, nr_pages, write)) {
Kirill A. Shutemov5b65c4672017-09-09 00:56:03 +03001876 local_irq_disable();
1877 gup_pgd_range(addr, end, write, pages, &nr);
1878 local_irq_enable();
Kirill A. Shutemov73e10a62017-03-16 18:26:54 +03001879 ret = nr;
1880 }
Steve Capper2667f502014-10-09 15:29:14 -07001881
1882 if (nr < nr_pages) {
1883 /* Try to get the remaining pages with get_user_pages */
1884 start += nr << PAGE_SHIFT;
1885 pages += nr;
1886
Lorenzo Stoakesc1641542016-10-13 01:20:13 +01001887 ret = get_user_pages_unlocked(start, nr_pages - nr, pages,
1888 write ? FOLL_WRITE : 0);
Steve Capper2667f502014-10-09 15:29:14 -07001889
1890 /* Have to be a bit careful with return values */
1891 if (nr > 0) {
1892 if (ret < 0)
1893 ret = nr;
1894 else
1895 ret += nr;
1896 }
1897 }
1898
1899 return ret;
1900}
1901
Kirill A. Shutemove5855132017-06-06 14:31:20 +03001902#endif /* CONFIG_HAVE_GENERIC_GUP */