blob: ce5e4545203b8054179d5a9604f2f719c8cc922e [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
14#include <asm/pgtable.h>
15
16static inline pte_t gup_get_pte(pte_t *ptep)
17{
18#ifndef CONFIG_X86_PAE
Christian Borntraeger14cf3d92014-11-21 16:29:40 +010019 return READ_ONCE(*ptep);
Nick Piggin8174c432008-07-25 19:45:24 -070020#else
21 /*
22 * With get_user_pages_fast, we walk down the pagetables without taking
Andy Shevchenkoab098092010-02-02 14:38:12 -080023 * any locks. For this we would like to load the pointers atomically,
Nick Piggin8174c432008-07-25 19:45:24 -070024 * but that is not possible (without expensive cmpxchg8b) on PAE. What
25 * we do have is the guarantee that a pte will only either go from not
26 * present to present, or present to not present or both -- it will not
27 * switch to a completely different present page without a TLB flush in
28 * between; something that we are blocking by holding interrupts off.
29 *
30 * Setting ptes from not present to present goes:
31 * ptep->pte_high = h;
32 * smp_wmb();
33 * ptep->pte_low = l;
34 *
35 * And present to not present goes:
36 * ptep->pte_low = 0;
37 * smp_wmb();
38 * ptep->pte_high = 0;
39 *
40 * We must ensure here that the load of pte_low sees l iff pte_high
41 * sees h. We load pte_high *after* loading pte_low, which ensures we
42 * don't see an older value of pte_high. *Then* we recheck pte_low,
43 * which ensures that we haven't picked up a changed pte high. We might
44 * have got rubbish values from pte_low and pte_high, but we are
45 * guaranteed that pte_low will not have the present bit set *unless*
46 * it is 'l'. And get_user_pages_fast only operates on present ptes, so
47 * we're safe.
48 *
49 * gup_get_pte should not be used or copied outside gup.c without being
50 * very careful -- it does not atomically load the pte or anything that
51 * is likely to be useful for you.
52 */
53 pte_t pte;
54
55retry:
56 pte.pte_low = ptep->pte_low;
57 smp_rmb();
58 pte.pte_high = ptep->pte_high;
59 smp_rmb();
60 if (unlikely(pte.pte_low != ptep->pte_low))
61 goto retry;
62
63 return pte;
64#endif
65}
66
Dan Williams3565fce2016-01-15 16:56:55 -080067static void undo_dev_pagemap(int *nr, int nr_start, struct page **pages)
68{
69 while ((*nr) - nr_start) {
70 struct page *page = pages[--(*nr)];
71
72 ClearPageReferenced(page);
73 put_page(page);
74 }
75}
76
Nick Piggin8174c432008-07-25 19:45:24 -070077/*
78 * The performance critical leaf functions are made noinline otherwise gcc
79 * inlines everything into a single function which results in too much
80 * register pressure.
81 */
82static noinline int gup_pte_range(pmd_t pmd, unsigned long addr,
83 unsigned long end, int write, struct page **pages, int *nr)
84{
Dan Williams3565fce2016-01-15 16:56:55 -080085 struct dev_pagemap *pgmap = NULL;
Nick Piggin8174c432008-07-25 19:45:24 -070086 unsigned long mask;
Dan Williams3565fce2016-01-15 16:56:55 -080087 int nr_start = *nr;
Nick Piggin8174c432008-07-25 19:45:24 -070088 pte_t *ptep;
89
90 mask = _PAGE_PRESENT|_PAGE_USER;
91 if (write)
92 mask |= _PAGE_RW;
93
94 ptep = pte_offset_map(&pmd, addr);
95 do {
96 pte_t pte = gup_get_pte(ptep);
97 struct page *page;
98
Mel Gorman2b4847e2013-12-18 17:08:32 -080099 /* Similar to the PMD case, NUMA hinting must take slow path */
Mel Gorman8a0516e2015-02-12 14:58:22 -0800100 if (pte_protnone(pte)) {
Mel Gorman2b4847e2013-12-18 17:08:32 -0800101 pte_unmap(ptep);
102 return 0;
103 }
104
Dan Williams3565fce2016-01-15 16:56:55 -0800105 page = pte_page(pte);
106 if (pte_devmap(pte)) {
107 pgmap = get_dev_pagemap(pte_pfn(pte), pgmap);
108 if (unlikely(!pgmap)) {
109 undo_dev_pagemap(nr, nr_start, pages);
110 pte_unmap(ptep);
111 return 0;
112 }
113 } else if ((pte_flags(pte) & (mask | _PAGE_SPECIAL)) != mask) {
Nick Piggin8174c432008-07-25 19:45:24 -0700114 pte_unmap(ptep);
115 return 0;
116 }
117 VM_BUG_ON(!pfn_valid(pte_pfn(pte)));
Nick Piggin8174c432008-07-25 19:45:24 -0700118 get_page(page);
Dan Williams3565fce2016-01-15 16:56:55 -0800119 put_dev_pagemap(pgmap);
Andrea Arcangeli8ee53822011-01-13 15:47:10 -0800120 SetPageReferenced(page);
Nick Piggin8174c432008-07-25 19:45:24 -0700121 pages[*nr] = page;
122 (*nr)++;
123
124 } while (ptep++, addr += PAGE_SIZE, addr != end);
125 pte_unmap(ptep - 1);
126
127 return 1;
128}
129
130static inline void get_head_page_multiple(struct page *page, int nr)
131{
Sasha Levin309381fea2014-01-23 15:52:54 -0800132 VM_BUG_ON_PAGE(page != compound_head(page), page);
133 VM_BUG_ON_PAGE(page_count(page) == 0, page);
Nick Piggin8174c432008-07-25 19:45:24 -0700134 atomic_add(nr, &page->_count);
Andrea Arcangeli8ee53822011-01-13 15:47:10 -0800135 SetPageReferenced(page);
Nick Piggin8174c432008-07-25 19:45:24 -0700136}
137
Dan Williams3565fce2016-01-15 16:56:55 -0800138static int __gup_device_huge_pmd(pmd_t pmd, unsigned long addr,
139 unsigned long end, struct page **pages, int *nr)
140{
141 int nr_start = *nr;
142 unsigned long pfn = pmd_pfn(pmd);
143 struct dev_pagemap *pgmap = NULL;
144
145 pfn += (addr & ~PMD_MASK) >> PAGE_SHIFT;
146 do {
147 struct page *page = pfn_to_page(pfn);
148
149 pgmap = get_dev_pagemap(pfn, pgmap);
150 if (unlikely(!pgmap)) {
151 undo_dev_pagemap(nr, nr_start, pages);
152 return 0;
153 }
154 SetPageReferenced(page);
155 pages[*nr] = page;
156 get_page(page);
157 put_dev_pagemap(pgmap);
158 (*nr)++;
159 pfn++;
160 } while (addr += PAGE_SIZE, addr != end);
161 return 1;
162}
163
Nick Piggin8174c432008-07-25 19:45:24 -0700164static noinline int gup_huge_pmd(pmd_t pmd, unsigned long addr,
165 unsigned long end, int write, struct page **pages, int *nr)
166{
167 unsigned long mask;
Nick Piggin8174c432008-07-25 19:45:24 -0700168 struct page *head, *page;
169 int refs;
170
171 mask = _PAGE_PRESENT|_PAGE_USER;
172 if (write)
173 mask |= _PAGE_RW;
Toshi Kanidaf3e352015-09-17 12:24:21 -0600174 if ((pmd_flags(pmd) & mask) != mask)
Nick Piggin8174c432008-07-25 19:45:24 -0700175 return 0;
Dan Williams3565fce2016-01-15 16:56:55 -0800176
177 VM_BUG_ON(!pfn_valid(pmd_pfn(pmd)));
178 if (pmd_devmap(pmd))
179 return __gup_device_huge_pmd(pmd, addr, end, pages, nr);
180
Nick Piggin8174c432008-07-25 19:45:24 -0700181 /* hugepages are never "special" */
Toshi Kanidaf3e352015-09-17 12:24:21 -0600182 VM_BUG_ON(pmd_flags(pmd) & _PAGE_SPECIAL);
Nick Piggin8174c432008-07-25 19:45:24 -0700183
184 refs = 0;
Toshi Kanidaf3e352015-09-17 12:24:21 -0600185 head = pmd_page(pmd);
Nick Piggin652ea692008-07-25 19:45:27 -0700186 page = head + ((addr & ~PMD_MASK) >> PAGE_SHIFT);
Nick Piggin8174c432008-07-25 19:45:24 -0700187 do {
Sasha Levin309381fea2014-01-23 15:52:54 -0800188 VM_BUG_ON_PAGE(compound_head(page) != head, page);
Nick Piggin8174c432008-07-25 19:45:24 -0700189 pages[*nr] = page;
190 (*nr)++;
191 page++;
192 refs++;
193 } while (addr += PAGE_SIZE, addr != end);
194 get_head_page_multiple(head, refs);
195
196 return 1;
197}
198
199static int gup_pmd_range(pud_t pud, unsigned long addr, unsigned long end,
200 int write, struct page **pages, int *nr)
201{
202 unsigned long next;
203 pmd_t *pmdp;
204
205 pmdp = pmd_offset(&pud, addr);
206 do {
207 pmd_t pmd = *pmdp;
208
209 next = pmd_addr_end(addr, end);
Kirill A. Shutemov1f196172016-01-15 16:53:35 -0800210 if (pmd_none(pmd))
Nick Piggin8174c432008-07-25 19:45:24 -0700211 return 0;
Naoya Horiguchicbef8472015-02-11 15:25:19 -0800212 if (unlikely(pmd_large(pmd) || !pmd_present(pmd))) {
Mel Gorman2b4847e2013-12-18 17:08:32 -0800213 /*
214 * NUMA hinting faults need to be handled in the GUP
215 * slowpath for accounting purposes and so that they
216 * can be serialised against THP migration.
217 */
Mel Gorman8a0516e2015-02-12 14:58:22 -0800218 if (pmd_protnone(pmd))
Mel Gorman2b4847e2013-12-18 17:08:32 -0800219 return 0;
Nick Piggin8174c432008-07-25 19:45:24 -0700220 if (!gup_huge_pmd(pmd, addr, next, write, pages, nr))
221 return 0;
222 } else {
223 if (!gup_pte_range(pmd, addr, next, write, pages, nr))
224 return 0;
225 }
226 } while (pmdp++, addr = next, addr != end);
227
228 return 1;
229}
230
Nick Piggin652ea692008-07-25 19:45:27 -0700231static noinline int gup_huge_pud(pud_t pud, unsigned long addr,
232 unsigned long end, int write, struct page **pages, int *nr)
233{
234 unsigned long mask;
Nick Piggin652ea692008-07-25 19:45:27 -0700235 struct page *head, *page;
236 int refs;
237
238 mask = _PAGE_PRESENT|_PAGE_USER;
239 if (write)
240 mask |= _PAGE_RW;
Toshi Kanidaf3e352015-09-17 12:24:21 -0600241 if ((pud_flags(pud) & mask) != mask)
Nick Piggin652ea692008-07-25 19:45:27 -0700242 return 0;
243 /* hugepages are never "special" */
Toshi Kanidaf3e352015-09-17 12:24:21 -0600244 VM_BUG_ON(pud_flags(pud) & _PAGE_SPECIAL);
245 VM_BUG_ON(!pfn_valid(pud_pfn(pud)));
Nick Piggin652ea692008-07-25 19:45:27 -0700246
247 refs = 0;
Toshi Kanidaf3e352015-09-17 12:24:21 -0600248 head = pud_page(pud);
Nick Piggin652ea692008-07-25 19:45:27 -0700249 page = head + ((addr & ~PUD_MASK) >> PAGE_SHIFT);
250 do {
Sasha Levin309381fea2014-01-23 15:52:54 -0800251 VM_BUG_ON_PAGE(compound_head(page) != head, page);
Nick Piggin652ea692008-07-25 19:45:27 -0700252 pages[*nr] = page;
253 (*nr)++;
254 page++;
255 refs++;
256 } while (addr += PAGE_SIZE, addr != end);
257 get_head_page_multiple(head, refs);
258
259 return 1;
260}
261
Nick Piggin8174c432008-07-25 19:45:24 -0700262static int gup_pud_range(pgd_t pgd, unsigned long addr, unsigned long end,
263 int write, struct page **pages, int *nr)
264{
265 unsigned long next;
266 pud_t *pudp;
267
268 pudp = pud_offset(&pgd, addr);
269 do {
270 pud_t pud = *pudp;
271
272 next = pud_addr_end(addr, end);
273 if (pud_none(pud))
274 return 0;
Nick Piggin652ea692008-07-25 19:45:27 -0700275 if (unlikely(pud_large(pud))) {
276 if (!gup_huge_pud(pud, addr, next, write, pages, nr))
277 return 0;
278 } else {
279 if (!gup_pmd_range(pud, addr, next, write, pages, nr))
280 return 0;
281 }
Nick Piggin8174c432008-07-25 19:45:24 -0700282 } while (pudp++, addr = next, addr != end);
283
284 return 1;
285}
286
Peter Zijlstra465a4542009-06-15 12:31:37 +0200287/*
288 * Like get_user_pages_fast() except its IRQ-safe in that it won't fall
289 * back to the regular GUP.
290 */
291int __get_user_pages_fast(unsigned long start, int nr_pages, int write,
292 struct page **pages)
293{
294 struct mm_struct *mm = current->mm;
295 unsigned long addr, len, end;
296 unsigned long next;
297 unsigned long flags;
298 pgd_t *pgdp;
299 int nr = 0;
300
301 start &= PAGE_MASK;
302 addr = start;
303 len = (unsigned long) nr_pages << PAGE_SHIFT;
304 end = start + len;
305 if (unlikely(!access_ok(write ? VERIFY_WRITE : VERIFY_READ,
306 (void __user *)start, len)))
307 return 0;
308
309 /*
310 * XXX: batch / limit 'nr', to avoid large irq off latency
311 * needs some instrumenting to determine the common sizes used by
312 * important workloads (eg. DB2), and whether limiting the batch size
313 * will decrease performance.
314 *
315 * It seems like we're in the clear for the moment. Direct-IO is
316 * the main guy that batches up lots of get_user_pages, and even
317 * they are limited to 64-at-a-time which is not so many.
318 */
319 /*
320 * This doesn't prevent pagetable teardown, but does prevent
321 * the pagetables and pages from being freed on x86.
322 *
323 * So long as we atomically load page table pointers versus teardown
324 * (which we do on x86, with the above PAE exception), we can follow the
325 * address down to the the page and take a ref on it.
326 */
327 local_irq_save(flags);
328 pgdp = pgd_offset(mm, addr);
329 do {
330 pgd_t pgd = *pgdp;
331
332 next = pgd_addr_end(addr, end);
333 if (pgd_none(pgd))
334 break;
335 if (!gup_pud_range(pgd, addr, next, write, pages, &nr))
336 break;
337 } while (pgdp++, addr = next, addr != end);
338 local_irq_restore(flags);
339
340 return nr;
341}
342
Andy Grovera0d22f42009-04-09 16:45:29 -0700343/**
344 * get_user_pages_fast() - pin user pages in memory
345 * @start: starting user address
346 * @nr_pages: number of pages from start to pin
347 * @write: whether pages will be written to
348 * @pages: array that receives pointers to the pages pinned.
349 * Should be at least nr_pages long.
350 *
351 * Attempt to pin user pages in memory without taking mm->mmap_sem.
352 * If not successful, it will fall back to taking the lock and
353 * calling get_user_pages().
354 *
355 * Returns number of pages pinned. This may be fewer than the number
356 * requested. If nr_pages is 0 or negative, returns 0. If no pages
357 * were pinned, returns -errno.
358 */
Nick Piggin8174c432008-07-25 19:45:24 -0700359int get_user_pages_fast(unsigned long start, int nr_pages, int write,
360 struct page **pages)
361{
362 struct mm_struct *mm = current->mm;
Linus Torvalds9b790222008-07-28 17:54:21 -0700363 unsigned long addr, len, end;
Nick Piggin8174c432008-07-25 19:45:24 -0700364 unsigned long next;
365 pgd_t *pgdp;
366 int nr = 0;
367
Linus Torvalds9b790222008-07-28 17:54:21 -0700368 start &= PAGE_MASK;
369 addr = start;
370 len = (unsigned long) nr_pages << PAGE_SHIFT;
Linus Torvalds7f818902009-06-20 09:52:27 -0700371
Linus Torvalds9b790222008-07-28 17:54:21 -0700372 end = start + len;
Linus Torvalds7f818902009-06-20 09:52:27 -0700373 if (end < start)
Nick Piggin8174c432008-07-25 19:45:24 -0700374 goto slow_irqon;
375
Linus Torvalds7f818902009-06-20 09:52:27 -0700376#ifdef CONFIG_X86_64
377 if (end >> __VIRTUAL_MASK_SHIFT)
378 goto slow_irqon;
379#endif
380
Nick Piggin8174c432008-07-25 19:45:24 -0700381 /*
382 * XXX: batch / limit 'nr', to avoid large irq off latency
383 * needs some instrumenting to determine the common sizes used by
384 * important workloads (eg. DB2), and whether limiting the batch size
385 * will decrease performance.
386 *
387 * It seems like we're in the clear for the moment. Direct-IO is
388 * the main guy that batches up lots of get_user_pages, and even
389 * they are limited to 64-at-a-time which is not so many.
390 */
391 /*
392 * This doesn't prevent pagetable teardown, but does prevent
393 * the pagetables and pages from being freed on x86.
394 *
395 * So long as we atomically load page table pointers versus teardown
396 * (which we do on x86, with the above PAE exception), we can follow the
397 * address down to the the page and take a ref on it.
398 */
399 local_irq_disable();
400 pgdp = pgd_offset(mm, addr);
401 do {
402 pgd_t pgd = *pgdp;
403
404 next = pgd_addr_end(addr, end);
405 if (pgd_none(pgd))
406 goto slow;
407 if (!gup_pud_range(pgd, addr, next, write, pages, &nr))
408 goto slow;
409 } while (pgdp++, addr = next, addr != end);
410 local_irq_enable();
411
412 VM_BUG_ON(nr != (end - start) >> PAGE_SHIFT);
413 return nr;
414
415 {
416 int ret;
417
418slow:
419 local_irq_enable();
420slow_irqon:
421 /* Try to get the remaining pages with get_user_pages */
422 start += nr << PAGE_SHIFT;
423 pages += nr;
424
Dave Hansend4edcf02016-02-12 13:01:56 -0800425 ret = get_user_pages_unlocked(start,
Andrea Arcangelia7b78072015-02-11 15:27:23 -0800426 (end - start) >> PAGE_SHIFT,
427 write, 0, pages);
Nick Piggin8174c432008-07-25 19:45:24 -0700428
429 /* Have to be a bit careful with return values */
430 if (nr > 0) {
431 if (ret < 0)
432 ret = nr;
433 else
434 ret += nr;
435 }
436
437 return ret;
438 }
439}