blob: d7db45bdfb3bdd30d25d09b83be9ab6b9e1314c5 [file] [log] [blame]
Nick Piggin8174c432008-07-25 19:45:24 -07001/*
2 * Lockless get_user_pages_fast for x86
3 *
4 * Copyright (C) 2008 Nick Piggin
5 * Copyright (C) 2008 Novell Inc.
6 */
7#include <linux/sched.h>
8#include <linux/mm.h>
9#include <linux/vmstat.h>
10#include <linux/highmem.h>
Andrea Arcangeli8ee53822011-01-13 15:47:10 -080011#include <linux/swap.h>
Dan Williams3565fce2016-01-15 16:56:55 -080012#include <linux/memremap.h>
Nick Piggin8174c432008-07-25 19:45:24 -070013
Dave Hansen33a709b2016-02-12 13:02:19 -080014#include <asm/mmu_context.h>
Nick Piggin8174c432008-07-25 19:45:24 -070015#include <asm/pgtable.h>
16
17static inline pte_t gup_get_pte(pte_t *ptep)
18{
19#ifndef CONFIG_X86_PAE
Christian Borntraeger14cf3d92014-11-21 16:29:40 +010020 return READ_ONCE(*ptep);
Nick Piggin8174c432008-07-25 19:45:24 -070021#else
22 /*
23 * With get_user_pages_fast, we walk down the pagetables without taking
Andy Shevchenkoab098092010-02-02 14:38:12 -080024 * any locks. For this we would like to load the pointers atomically,
Nick Piggin8174c432008-07-25 19:45:24 -070025 * but that is not possible (without expensive cmpxchg8b) on PAE. What
26 * we do have is the guarantee that a pte will only either go from not
27 * present to present, or present to not present or both -- it will not
28 * switch to a completely different present page without a TLB flush in
29 * between; something that we are blocking by holding interrupts off.
30 *
31 * Setting ptes from not present to present goes:
32 * ptep->pte_high = h;
33 * smp_wmb();
34 * ptep->pte_low = l;
35 *
36 * And present to not present goes:
37 * ptep->pte_low = 0;
38 * smp_wmb();
39 * ptep->pte_high = 0;
40 *
41 * We must ensure here that the load of pte_low sees l iff pte_high
42 * sees h. We load pte_high *after* loading pte_low, which ensures we
43 * don't see an older value of pte_high. *Then* we recheck pte_low,
44 * which ensures that we haven't picked up a changed pte high. We might
45 * have got rubbish values from pte_low and pte_high, but we are
46 * guaranteed that pte_low will not have the present bit set *unless*
47 * it is 'l'. And get_user_pages_fast only operates on present ptes, so
48 * we're safe.
49 *
50 * gup_get_pte should not be used or copied outside gup.c without being
51 * very careful -- it does not atomically load the pte or anything that
52 * is likely to be useful for you.
53 */
54 pte_t pte;
55
56retry:
57 pte.pte_low = ptep->pte_low;
58 smp_rmb();
59 pte.pte_high = ptep->pte_high;
60 smp_rmb();
61 if (unlikely(pte.pte_low != ptep->pte_low))
62 goto retry;
63
64 return pte;
65#endif
66}
67
Dan Williams3565fce2016-01-15 16:56:55 -080068static void undo_dev_pagemap(int *nr, int nr_start, struct page **pages)
69{
70 while ((*nr) - nr_start) {
71 struct page *page = pages[--(*nr)];
72
73 ClearPageReferenced(page);
74 put_page(page);
75 }
76}
77
Nick Piggin8174c432008-07-25 19:45:24 -070078/*
Dave Hansen1874f682016-02-12 13:02:18 -080079 * 'pteval' can come from a pte, pmd or pud. We only check
80 * _PAGE_PRESENT, _PAGE_USER, and _PAGE_RW in here which are the
81 * same value on all 3 types.
82 */
83static inline int pte_allows_gup(unsigned long pteval, int write)
84{
85 unsigned long need_pte_bits = _PAGE_PRESENT|_PAGE_USER;
86
87 if (write)
88 need_pte_bits |= _PAGE_RW;
89
90 if ((pteval & need_pte_bits) != need_pte_bits)
91 return 0;
92
Dave Hansen33a709b2016-02-12 13:02:19 -080093 /* Check memory protection keys permissions. */
94 if (!__pkru_allows_pkey(pte_flags_pkey(pteval), write))
95 return 0;
96
Dave Hansen1874f682016-02-12 13:02:18 -080097 return 1;
98}
99
100/*
Vlastimil Babkad73af792019-08-02 18:06:14 +0200101 * Return the compund head page with ref appropriately incremented,
102 * or NULL if that failed.
103 */
104static inline struct page *try_get_compound_head(struct page *page, int refs)
105{
106 struct page *head = compound_head(page);
107 if (WARN_ON_ONCE(page_ref_count(head) < 0))
108 return NULL;
109 if (unlikely(!page_cache_add_speculative(head, refs)))
110 return NULL;
111 return head;
112}
113
114/*
Nick Piggin8174c432008-07-25 19:45:24 -0700115 * The performance critical leaf functions are made noinline otherwise gcc
116 * inlines everything into a single function which results in too much
117 * register pressure.
118 */
119static noinline int gup_pte_range(pmd_t pmd, unsigned long addr,
120 unsigned long end, int write, struct page **pages, int *nr)
121{
Dan Williams3565fce2016-01-15 16:56:55 -0800122 struct dev_pagemap *pgmap = NULL;
Dan Williams3565fce2016-01-15 16:56:55 -0800123 int nr_start = *nr;
Nick Piggin8174c432008-07-25 19:45:24 -0700124 pte_t *ptep;
125
Nick Piggin8174c432008-07-25 19:45:24 -0700126 ptep = pte_offset_map(&pmd, addr);
127 do {
128 pte_t pte = gup_get_pte(ptep);
Vlastimil Babkad73af792019-08-02 18:06:14 +0200129 struct page *head, *page;
Nick Piggin8174c432008-07-25 19:45:24 -0700130
Mel Gorman2b4847e2013-12-18 17:08:32 -0800131 /* Similar to the PMD case, NUMA hinting must take slow path */
Mel Gorman8a0516e2015-02-12 14:58:22 -0800132 if (pte_protnone(pte)) {
Mel Gorman2b4847e2013-12-18 17:08:32 -0800133 pte_unmap(ptep);
134 return 0;
135 }
136
Dan Williams91cdd9d2017-03-09 16:16:42 -0800137 if (!pte_allows_gup(pte_val(pte), write)) {
138 pte_unmap(ptep);
139 return 0;
140 }
141
Dan Williams3565fce2016-01-15 16:56:55 -0800142 if (pte_devmap(pte)) {
143 pgmap = get_dev_pagemap(pte_pfn(pte), pgmap);
144 if (unlikely(!pgmap)) {
145 undo_dev_pagemap(nr, nr_start, pages);
146 pte_unmap(ptep);
147 return 0;
148 }
Dan Williams91cdd9d2017-03-09 16:16:42 -0800149 } else if (pte_special(pte)) {
Nick Piggin8174c432008-07-25 19:45:24 -0700150 pte_unmap(ptep);
151 return 0;
152 }
153 VM_BUG_ON(!pfn_valid(pte_pfn(pte)));
Hugh Dickins457a98b2016-02-17 13:11:23 -0800154 page = pte_page(pte);
Vlastimil Babkad73af792019-08-02 18:06:14 +0200155
156 head = try_get_compound_head(page, 1);
157 if (!head) {
158 put_dev_pagemap(pgmap);
159 pte_unmap(ptep);
160 return 0;
161 }
162
163 if (unlikely(pte_val(pte) != pte_val(*ptep))) {
164 put_page(head);
165 put_dev_pagemap(pgmap);
166 pte_unmap(ptep);
167 return 0;
168 }
169
Dan Williams3565fce2016-01-15 16:56:55 -0800170 put_dev_pagemap(pgmap);
Andrea Arcangeli8ee53822011-01-13 15:47:10 -0800171 SetPageReferenced(page);
Nick Piggin8174c432008-07-25 19:45:24 -0700172 pages[*nr] = page;
173 (*nr)++;
174
175 } while (ptep++, addr += PAGE_SIZE, addr != end);
176 pte_unmap(ptep - 1);
177
178 return 1;
179}
180
181static inline void get_head_page_multiple(struct page *page, int nr)
182{
Sasha Levin309381fea2014-01-23 15:52:54 -0800183 VM_BUG_ON_PAGE(page != compound_head(page), page);
184 VM_BUG_ON_PAGE(page_count(page) == 0, page);
Joonsoo Kimfe896d12016-03-17 14:19:26 -0700185 page_ref_add(page, nr);
Andrea Arcangeli8ee53822011-01-13 15:47:10 -0800186 SetPageReferenced(page);
Nick Piggin8174c432008-07-25 19:45:24 -0700187}
188
Dan Williams3565fce2016-01-15 16:56:55 -0800189static int __gup_device_huge_pmd(pmd_t pmd, unsigned long addr,
190 unsigned long end, struct page **pages, int *nr)
191{
192 int nr_start = *nr;
193 unsigned long pfn = pmd_pfn(pmd);
194 struct dev_pagemap *pgmap = NULL;
195
196 pfn += (addr & ~PMD_MASK) >> PAGE_SHIFT;
197 do {
198 struct page *page = pfn_to_page(pfn);
199
200 pgmap = get_dev_pagemap(pfn, pgmap);
201 if (unlikely(!pgmap)) {
202 undo_dev_pagemap(nr, nr_start, pages);
203 return 0;
204 }
205 SetPageReferenced(page);
206 pages[*nr] = page;
207 get_page(page);
208 put_dev_pagemap(pgmap);
209 (*nr)++;
210 pfn++;
211 } while (addr += PAGE_SIZE, addr != end);
212 return 1;
213}
214
Nick Piggin8174c432008-07-25 19:45:24 -0700215static noinline int gup_huge_pmd(pmd_t pmd, unsigned long addr,
216 unsigned long end, int write, struct page **pages, int *nr)
217{
Nick Piggin8174c432008-07-25 19:45:24 -0700218 struct page *head, *page;
219 int refs;
220
Dave Hansen1874f682016-02-12 13:02:18 -0800221 if (!pte_allows_gup(pmd_val(pmd), write))
Nick Piggin8174c432008-07-25 19:45:24 -0700222 return 0;
Dan Williams3565fce2016-01-15 16:56:55 -0800223
224 VM_BUG_ON(!pfn_valid(pmd_pfn(pmd)));
225 if (pmd_devmap(pmd))
226 return __gup_device_huge_pmd(pmd, addr, end, pages, nr);
227
Nick Piggin8174c432008-07-25 19:45:24 -0700228 /* hugepages are never "special" */
Toshi Kanidaf3e352015-09-17 12:24:21 -0600229 VM_BUG_ON(pmd_flags(pmd) & _PAGE_SPECIAL);
Nick Piggin8174c432008-07-25 19:45:24 -0700230
231 refs = 0;
Toshi Kanidaf3e352015-09-17 12:24:21 -0600232 head = pmd_page(pmd);
Nick Piggin652ea692008-07-25 19:45:27 -0700233 page = head + ((addr & ~PMD_MASK) >> PAGE_SHIFT);
Nick Piggin8174c432008-07-25 19:45:24 -0700234 do {
Sasha Levin309381fea2014-01-23 15:52:54 -0800235 VM_BUG_ON_PAGE(compound_head(page) != head, page);
Nick Piggin8174c432008-07-25 19:45:24 -0700236 pages[*nr] = page;
237 (*nr)++;
238 page++;
239 refs++;
240 } while (addr += PAGE_SIZE, addr != end);
241 get_head_page_multiple(head, refs);
242
243 return 1;
244}
245
246static int gup_pmd_range(pud_t pud, unsigned long addr, unsigned long end,
247 int write, struct page **pages, int *nr)
248{
249 unsigned long next;
250 pmd_t *pmdp;
251
252 pmdp = pmd_offset(&pud, addr);
253 do {
254 pmd_t pmd = *pmdp;
255
256 next = pmd_addr_end(addr, end);
Kirill A. Shutemov1f196172016-01-15 16:53:35 -0800257 if (pmd_none(pmd))
Nick Piggin8174c432008-07-25 19:45:24 -0700258 return 0;
Naoya Horiguchicbef8472015-02-11 15:25:19 -0800259 if (unlikely(pmd_large(pmd) || !pmd_present(pmd))) {
Mel Gorman2b4847e2013-12-18 17:08:32 -0800260 /*
261 * NUMA hinting faults need to be handled in the GUP
262 * slowpath for accounting purposes and so that they
263 * can be serialised against THP migration.
264 */
Mel Gorman8a0516e2015-02-12 14:58:22 -0800265 if (pmd_protnone(pmd))
Mel Gorman2b4847e2013-12-18 17:08:32 -0800266 return 0;
Nick Piggin8174c432008-07-25 19:45:24 -0700267 if (!gup_huge_pmd(pmd, addr, next, write, pages, nr))
268 return 0;
269 } else {
270 if (!gup_pte_range(pmd, addr, next, write, pages, nr))
271 return 0;
272 }
273 } while (pmdp++, addr = next, addr != end);
274
275 return 1;
276}
277
Nick Piggin652ea692008-07-25 19:45:27 -0700278static noinline int gup_huge_pud(pud_t pud, unsigned long addr,
279 unsigned long end, int write, struct page **pages, int *nr)
280{
Nick Piggin652ea692008-07-25 19:45:27 -0700281 struct page *head, *page;
282 int refs;
283
Dave Hansen1874f682016-02-12 13:02:18 -0800284 if (!pte_allows_gup(pud_val(pud), write))
Nick Piggin652ea692008-07-25 19:45:27 -0700285 return 0;
286 /* hugepages are never "special" */
Toshi Kanidaf3e352015-09-17 12:24:21 -0600287 VM_BUG_ON(pud_flags(pud) & _PAGE_SPECIAL);
288 VM_BUG_ON(!pfn_valid(pud_pfn(pud)));
Nick Piggin652ea692008-07-25 19:45:27 -0700289
290 refs = 0;
Toshi Kanidaf3e352015-09-17 12:24:21 -0600291 head = pud_page(pud);
Nick Piggin652ea692008-07-25 19:45:27 -0700292 page = head + ((addr & ~PUD_MASK) >> PAGE_SHIFT);
293 do {
Sasha Levin309381fea2014-01-23 15:52:54 -0800294 VM_BUG_ON_PAGE(compound_head(page) != head, page);
Nick Piggin652ea692008-07-25 19:45:27 -0700295 pages[*nr] = page;
296 (*nr)++;
297 page++;
298 refs++;
299 } while (addr += PAGE_SIZE, addr != end);
300 get_head_page_multiple(head, refs);
301
302 return 1;
303}
304
Nick Piggin8174c432008-07-25 19:45:24 -0700305static int gup_pud_range(pgd_t pgd, unsigned long addr, unsigned long end,
306 int write, struct page **pages, int *nr)
307{
308 unsigned long next;
309 pud_t *pudp;
310
311 pudp = pud_offset(&pgd, addr);
312 do {
313 pud_t pud = *pudp;
314
315 next = pud_addr_end(addr, end);
316 if (pud_none(pud))
317 return 0;
Nick Piggin652ea692008-07-25 19:45:27 -0700318 if (unlikely(pud_large(pud))) {
319 if (!gup_huge_pud(pud, addr, next, write, pages, nr))
320 return 0;
321 } else {
322 if (!gup_pmd_range(pud, addr, next, write, pages, nr))
323 return 0;
324 }
Nick Piggin8174c432008-07-25 19:45:24 -0700325 } while (pudp++, addr = next, addr != end);
326
327 return 1;
328}
329
Peter Zijlstra465a4542009-06-15 12:31:37 +0200330/*
331 * Like get_user_pages_fast() except its IRQ-safe in that it won't fall
332 * back to the regular GUP.
333 */
334int __get_user_pages_fast(unsigned long start, int nr_pages, int write,
335 struct page **pages)
336{
337 struct mm_struct *mm = current->mm;
338 unsigned long addr, len, end;
339 unsigned long next;
340 unsigned long flags;
341 pgd_t *pgdp;
342 int nr = 0;
343
344 start &= PAGE_MASK;
345 addr = start;
346 len = (unsigned long) nr_pages << PAGE_SHIFT;
347 end = start + len;
348 if (unlikely(!access_ok(write ? VERIFY_WRITE : VERIFY_READ,
349 (void __user *)start, len)))
350 return 0;
351
352 /*
353 * XXX: batch / limit 'nr', to avoid large irq off latency
354 * needs some instrumenting to determine the common sizes used by
355 * important workloads (eg. DB2), and whether limiting the batch size
356 * will decrease performance.
357 *
358 * It seems like we're in the clear for the moment. Direct-IO is
359 * the main guy that batches up lots of get_user_pages, and even
360 * they are limited to 64-at-a-time which is not so many.
361 */
362 /*
363 * This doesn't prevent pagetable teardown, but does prevent
364 * the pagetables and pages from being freed on x86.
365 *
366 * So long as we atomically load page table pointers versus teardown
367 * (which we do on x86, with the above PAE exception), we can follow the
368 * address down to the the page and take a ref on it.
369 */
370 local_irq_save(flags);
371 pgdp = pgd_offset(mm, addr);
372 do {
373 pgd_t pgd = *pgdp;
374
375 next = pgd_addr_end(addr, end);
376 if (pgd_none(pgd))
377 break;
378 if (!gup_pud_range(pgd, addr, next, write, pages, &nr))
379 break;
380 } while (pgdp++, addr = next, addr != end);
381 local_irq_restore(flags);
382
383 return nr;
384}
385
Andy Grovera0d22f42009-04-09 16:45:29 -0700386/**
387 * get_user_pages_fast() - pin user pages in memory
388 * @start: starting user address
389 * @nr_pages: number of pages from start to pin
390 * @write: whether pages will be written to
391 * @pages: array that receives pointers to the pages pinned.
392 * Should be at least nr_pages long.
393 *
394 * Attempt to pin user pages in memory without taking mm->mmap_sem.
395 * If not successful, it will fall back to taking the lock and
396 * calling get_user_pages().
397 *
398 * Returns number of pages pinned. This may be fewer than the number
399 * requested. If nr_pages is 0 or negative, returns 0. If no pages
400 * were pinned, returns -errno.
401 */
Nick Piggin8174c432008-07-25 19:45:24 -0700402int get_user_pages_fast(unsigned long start, int nr_pages, int write,
403 struct page **pages)
404{
405 struct mm_struct *mm = current->mm;
Linus Torvalds9b790222008-07-28 17:54:21 -0700406 unsigned long addr, len, end;
Nick Piggin8174c432008-07-25 19:45:24 -0700407 unsigned long next;
408 pgd_t *pgdp;
409 int nr = 0;
410
Linus Torvalds9b790222008-07-28 17:54:21 -0700411 start &= PAGE_MASK;
412 addr = start;
413 len = (unsigned long) nr_pages << PAGE_SHIFT;
Linus Torvalds7f818902009-06-20 09:52:27 -0700414
Linus Torvalds9b790222008-07-28 17:54:21 -0700415 end = start + len;
Linus Torvalds7f818902009-06-20 09:52:27 -0700416 if (end < start)
Nick Piggin8174c432008-07-25 19:45:24 -0700417 goto slow_irqon;
418
Linus Torvalds7f818902009-06-20 09:52:27 -0700419#ifdef CONFIG_X86_64
420 if (end >> __VIRTUAL_MASK_SHIFT)
421 goto slow_irqon;
422#endif
423
Nick Piggin8174c432008-07-25 19:45:24 -0700424 /*
425 * XXX: batch / limit 'nr', to avoid large irq off latency
426 * needs some instrumenting to determine the common sizes used by
427 * important workloads (eg. DB2), and whether limiting the batch size
428 * will decrease performance.
429 *
430 * It seems like we're in the clear for the moment. Direct-IO is
431 * the main guy that batches up lots of get_user_pages, and even
432 * they are limited to 64-at-a-time which is not so many.
433 */
434 /*
435 * This doesn't prevent pagetable teardown, but does prevent
436 * the pagetables and pages from being freed on x86.
437 *
438 * So long as we atomically load page table pointers versus teardown
439 * (which we do on x86, with the above PAE exception), we can follow the
440 * address down to the the page and take a ref on it.
441 */
442 local_irq_disable();
443 pgdp = pgd_offset(mm, addr);
444 do {
445 pgd_t pgd = *pgdp;
446
447 next = pgd_addr_end(addr, end);
448 if (pgd_none(pgd))
449 goto slow;
450 if (!gup_pud_range(pgd, addr, next, write, pages, &nr))
451 goto slow;
452 } while (pgdp++, addr = next, addr != end);
453 local_irq_enable();
454
455 VM_BUG_ON(nr != (end - start) >> PAGE_SHIFT);
456 return nr;
457
458 {
459 int ret;
460
461slow:
462 local_irq_enable();
463slow_irqon:
464 /* Try to get the remaining pages with get_user_pages */
465 start += nr << PAGE_SHIFT;
466 pages += nr;
467
Dave Hansend4edcf02016-02-12 13:01:56 -0800468 ret = get_user_pages_unlocked(start,
Andrea Arcangelia7b78072015-02-11 15:27:23 -0800469 (end - start) >> PAGE_SHIFT,
Lorenzo Stoakesc1641542016-10-13 01:20:13 +0100470 pages, write ? FOLL_WRITE : 0);
Nick Piggin8174c432008-07-25 19:45:24 -0700471
472 /* Have to be a bit careful with return values */
473 if (nr > 0) {
474 if (ret < 0)
475 ret = nr;
476 else
477 ret += nr;
478 }
479
480 return ret;
481 }
482}