blob: ae9a37bf13711460892584e67d02880291168f86 [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>
Nick Piggin8174c432008-07-25 19:45:24 -070012
13#include <asm/pgtable.h>
14
15static inline pte_t gup_get_pte(pte_t *ptep)
16{
17#ifndef CONFIG_X86_PAE
Christian Borntraeger14cf3d92014-11-21 16:29:40 +010018 return READ_ONCE(*ptep);
Nick Piggin8174c432008-07-25 19:45:24 -070019#else
20 /*
21 * With get_user_pages_fast, we walk down the pagetables without taking
Andy Shevchenkoab098092010-02-02 14:38:12 -080022 * any locks. For this we would like to load the pointers atomically,
Nick Piggin8174c432008-07-25 19:45:24 -070023 * but that is not possible (without expensive cmpxchg8b) on PAE. What
24 * we do have is the guarantee that a pte will only either go from not
25 * present to present, or present to not present or both -- it will not
26 * switch to a completely different present page without a TLB flush in
27 * between; something that we are blocking by holding interrupts off.
28 *
29 * Setting ptes from not present to present goes:
30 * ptep->pte_high = h;
31 * smp_wmb();
32 * ptep->pte_low = l;
33 *
34 * And present to not present goes:
35 * ptep->pte_low = 0;
36 * smp_wmb();
37 * ptep->pte_high = 0;
38 *
39 * We must ensure here that the load of pte_low sees l iff pte_high
40 * sees h. We load pte_high *after* loading pte_low, which ensures we
41 * don't see an older value of pte_high. *Then* we recheck pte_low,
42 * which ensures that we haven't picked up a changed pte high. We might
43 * have got rubbish values from pte_low and pte_high, but we are
44 * guaranteed that pte_low will not have the present bit set *unless*
45 * it is 'l'. And get_user_pages_fast only operates on present ptes, so
46 * we're safe.
47 *
48 * gup_get_pte should not be used or copied outside gup.c without being
49 * very careful -- it does not atomically load the pte or anything that
50 * is likely to be useful for you.
51 */
52 pte_t pte;
53
54retry:
55 pte.pte_low = ptep->pte_low;
56 smp_rmb();
57 pte.pte_high = ptep->pte_high;
58 smp_rmb();
59 if (unlikely(pte.pte_low != ptep->pte_low))
60 goto retry;
61
62 return pte;
63#endif
64}
65
66/*
67 * The performance critical leaf functions are made noinline otherwise gcc
68 * inlines everything into a single function which results in too much
69 * register pressure.
70 */
71static noinline int gup_pte_range(pmd_t pmd, unsigned long addr,
72 unsigned long end, int write, struct page **pages, int *nr)
73{
74 unsigned long mask;
75 pte_t *ptep;
76
77 mask = _PAGE_PRESENT|_PAGE_USER;
78 if (write)
79 mask |= _PAGE_RW;
80
81 ptep = pte_offset_map(&pmd, addr);
82 do {
83 pte_t pte = gup_get_pte(ptep);
84 struct page *page;
85
Mel Gorman2b4847e2013-12-18 17:08:32 -080086 /* Similar to the PMD case, NUMA hinting must take slow path */
Mel Gorman8a0516e2015-02-12 14:58:22 -080087 if (pte_protnone(pte)) {
Mel Gorman2b4847e2013-12-18 17:08:32 -080088 pte_unmap(ptep);
89 return 0;
90 }
91
Jan Beulich606ee442008-09-17 16:48:17 +010092 if ((pte_flags(pte) & (mask | _PAGE_SPECIAL)) != mask) {
Nick Piggin8174c432008-07-25 19:45:24 -070093 pte_unmap(ptep);
94 return 0;
95 }
96 VM_BUG_ON(!pfn_valid(pte_pfn(pte)));
97 page = pte_page(pte);
98 get_page(page);
Andrea Arcangeli8ee53822011-01-13 15:47:10 -080099 SetPageReferenced(page);
Nick Piggin8174c432008-07-25 19:45:24 -0700100 pages[*nr] = page;
101 (*nr)++;
102
103 } while (ptep++, addr += PAGE_SIZE, addr != end);
104 pte_unmap(ptep - 1);
105
106 return 1;
107}
108
109static inline void get_head_page_multiple(struct page *page, int nr)
110{
Sasha Levin309381fea2014-01-23 15:52:54 -0800111 VM_BUG_ON_PAGE(page != compound_head(page), page);
112 VM_BUG_ON_PAGE(page_count(page) == 0, page);
Nick Piggin8174c432008-07-25 19:45:24 -0700113 atomic_add(nr, &page->_count);
Andrea Arcangeli8ee53822011-01-13 15:47:10 -0800114 SetPageReferenced(page);
Nick Piggin8174c432008-07-25 19:45:24 -0700115}
116
117static noinline int gup_huge_pmd(pmd_t pmd, unsigned long addr,
118 unsigned long end, int write, struct page **pages, int *nr)
119{
120 unsigned long mask;
Nick Piggin8174c432008-07-25 19:45:24 -0700121 struct page *head, *page;
122 int refs;
123
124 mask = _PAGE_PRESENT|_PAGE_USER;
125 if (write)
126 mask |= _PAGE_RW;
Toshi Kanidaf3e352015-09-17 12:24:21 -0600127 if ((pmd_flags(pmd) & mask) != mask)
Nick Piggin8174c432008-07-25 19:45:24 -0700128 return 0;
129 /* hugepages are never "special" */
Toshi Kanidaf3e352015-09-17 12:24:21 -0600130 VM_BUG_ON(pmd_flags(pmd) & _PAGE_SPECIAL);
131 VM_BUG_ON(!pfn_valid(pmd_pfn(pmd)));
Nick Piggin8174c432008-07-25 19:45:24 -0700132
133 refs = 0;
Toshi Kanidaf3e352015-09-17 12:24:21 -0600134 head = pmd_page(pmd);
Nick Piggin652ea692008-07-25 19:45:27 -0700135 page = head + ((addr & ~PMD_MASK) >> PAGE_SHIFT);
Nick Piggin8174c432008-07-25 19:45:24 -0700136 do {
Sasha Levin309381fea2014-01-23 15:52:54 -0800137 VM_BUG_ON_PAGE(compound_head(page) != head, page);
Nick Piggin8174c432008-07-25 19:45:24 -0700138 pages[*nr] = page;
Andrea Arcangeli91807062011-01-13 15:46:32 -0800139 if (PageTail(page))
140 get_huge_page_tail(page);
Nick Piggin8174c432008-07-25 19:45:24 -0700141 (*nr)++;
142 page++;
143 refs++;
144 } while (addr += PAGE_SIZE, addr != end);
145 get_head_page_multiple(head, refs);
146
147 return 1;
148}
149
150static int gup_pmd_range(pud_t pud, unsigned long addr, unsigned long end,
151 int write, struct page **pages, int *nr)
152{
153 unsigned long next;
154 pmd_t *pmdp;
155
156 pmdp = pmd_offset(&pud, addr);
157 do {
158 pmd_t pmd = *pmdp;
159
160 next = pmd_addr_end(addr, end);
Andrea Arcangeli64cc6ae2011-01-13 15:46:42 -0800161 /*
162 * The pmd_trans_splitting() check below explains why
163 * pmdp_splitting_flush has to flush the tlb, to stop
164 * this gup-fast code from running while we set the
165 * splitting bit in the pmd. Returning zero will take
166 * the slow path that will call wait_split_huge_page()
167 * if the pmd is still in splitting state. gup-fast
168 * can't because it has irq disabled and
169 * wait_split_huge_page() would never return as the
170 * tlb flush IPI wouldn't run.
171 */
172 if (pmd_none(pmd) || pmd_trans_splitting(pmd))
Nick Piggin8174c432008-07-25 19:45:24 -0700173 return 0;
Naoya Horiguchicbef8472015-02-11 15:25:19 -0800174 if (unlikely(pmd_large(pmd) || !pmd_present(pmd))) {
Mel Gorman2b4847e2013-12-18 17:08:32 -0800175 /*
176 * NUMA hinting faults need to be handled in the GUP
177 * slowpath for accounting purposes and so that they
178 * can be serialised against THP migration.
179 */
Mel Gorman8a0516e2015-02-12 14:58:22 -0800180 if (pmd_protnone(pmd))
Mel Gorman2b4847e2013-12-18 17:08:32 -0800181 return 0;
Nick Piggin8174c432008-07-25 19:45:24 -0700182 if (!gup_huge_pmd(pmd, addr, next, write, pages, nr))
183 return 0;
184 } else {
185 if (!gup_pte_range(pmd, addr, next, write, pages, nr))
186 return 0;
187 }
188 } while (pmdp++, addr = next, addr != end);
189
190 return 1;
191}
192
Nick Piggin652ea692008-07-25 19:45:27 -0700193static noinline int gup_huge_pud(pud_t pud, unsigned long addr,
194 unsigned long end, int write, struct page **pages, int *nr)
195{
196 unsigned long mask;
Nick Piggin652ea692008-07-25 19:45:27 -0700197 struct page *head, *page;
198 int refs;
199
200 mask = _PAGE_PRESENT|_PAGE_USER;
201 if (write)
202 mask |= _PAGE_RW;
Toshi Kanidaf3e352015-09-17 12:24:21 -0600203 if ((pud_flags(pud) & mask) != mask)
Nick Piggin652ea692008-07-25 19:45:27 -0700204 return 0;
205 /* hugepages are never "special" */
Toshi Kanidaf3e352015-09-17 12:24:21 -0600206 VM_BUG_ON(pud_flags(pud) & _PAGE_SPECIAL);
207 VM_BUG_ON(!pfn_valid(pud_pfn(pud)));
Nick Piggin652ea692008-07-25 19:45:27 -0700208
209 refs = 0;
Toshi Kanidaf3e352015-09-17 12:24:21 -0600210 head = pud_page(pud);
Nick Piggin652ea692008-07-25 19:45:27 -0700211 page = head + ((addr & ~PUD_MASK) >> PAGE_SHIFT);
212 do {
Sasha Levin309381fea2014-01-23 15:52:54 -0800213 VM_BUG_ON_PAGE(compound_head(page) != head, page);
Nick Piggin652ea692008-07-25 19:45:27 -0700214 pages[*nr] = page;
Youquan Songb6999b192011-12-08 14:34:16 -0800215 if (PageTail(page))
216 get_huge_page_tail(page);
Nick Piggin652ea692008-07-25 19:45:27 -0700217 (*nr)++;
218 page++;
219 refs++;
220 } while (addr += PAGE_SIZE, addr != end);
221 get_head_page_multiple(head, refs);
222
223 return 1;
224}
225
Nick Piggin8174c432008-07-25 19:45:24 -0700226static int gup_pud_range(pgd_t pgd, unsigned long addr, unsigned long end,
227 int write, struct page **pages, int *nr)
228{
229 unsigned long next;
230 pud_t *pudp;
231
232 pudp = pud_offset(&pgd, addr);
233 do {
234 pud_t pud = *pudp;
235
236 next = pud_addr_end(addr, end);
237 if (pud_none(pud))
238 return 0;
Nick Piggin652ea692008-07-25 19:45:27 -0700239 if (unlikely(pud_large(pud))) {
240 if (!gup_huge_pud(pud, addr, next, write, pages, nr))
241 return 0;
242 } else {
243 if (!gup_pmd_range(pud, addr, next, write, pages, nr))
244 return 0;
245 }
Nick Piggin8174c432008-07-25 19:45:24 -0700246 } while (pudp++, addr = next, addr != end);
247
248 return 1;
249}
250
Peter Zijlstra465a4542009-06-15 12:31:37 +0200251/*
252 * Like get_user_pages_fast() except its IRQ-safe in that it won't fall
253 * back to the regular GUP.
254 */
255int __get_user_pages_fast(unsigned long start, int nr_pages, int write,
256 struct page **pages)
257{
258 struct mm_struct *mm = current->mm;
259 unsigned long addr, len, end;
260 unsigned long next;
261 unsigned long flags;
262 pgd_t *pgdp;
263 int nr = 0;
264
265 start &= PAGE_MASK;
266 addr = start;
267 len = (unsigned long) nr_pages << PAGE_SHIFT;
268 end = start + len;
269 if (unlikely(!access_ok(write ? VERIFY_WRITE : VERIFY_READ,
270 (void __user *)start, len)))
271 return 0;
272
273 /*
274 * XXX: batch / limit 'nr', to avoid large irq off latency
275 * needs some instrumenting to determine the common sizes used by
276 * important workloads (eg. DB2), and whether limiting the batch size
277 * will decrease performance.
278 *
279 * It seems like we're in the clear for the moment. Direct-IO is
280 * the main guy that batches up lots of get_user_pages, and even
281 * they are limited to 64-at-a-time which is not so many.
282 */
283 /*
284 * This doesn't prevent pagetable teardown, but does prevent
285 * the pagetables and pages from being freed on x86.
286 *
287 * So long as we atomically load page table pointers versus teardown
288 * (which we do on x86, with the above PAE exception), we can follow the
289 * address down to the the page and take a ref on it.
290 */
291 local_irq_save(flags);
292 pgdp = pgd_offset(mm, addr);
293 do {
294 pgd_t pgd = *pgdp;
295
296 next = pgd_addr_end(addr, end);
297 if (pgd_none(pgd))
298 break;
299 if (!gup_pud_range(pgd, addr, next, write, pages, &nr))
300 break;
301 } while (pgdp++, addr = next, addr != end);
302 local_irq_restore(flags);
303
304 return nr;
305}
306
Andy Grovera0d22f42009-04-09 16:45:29 -0700307/**
308 * get_user_pages_fast() - pin user pages in memory
309 * @start: starting user address
310 * @nr_pages: number of pages from start to pin
311 * @write: whether pages will be written to
312 * @pages: array that receives pointers to the pages pinned.
313 * Should be at least nr_pages long.
314 *
315 * Attempt to pin user pages in memory without taking mm->mmap_sem.
316 * If not successful, it will fall back to taking the lock and
317 * calling get_user_pages().
318 *
319 * Returns number of pages pinned. This may be fewer than the number
320 * requested. If nr_pages is 0 or negative, returns 0. If no pages
321 * were pinned, returns -errno.
322 */
Nick Piggin8174c432008-07-25 19:45:24 -0700323int get_user_pages_fast(unsigned long start, int nr_pages, int write,
324 struct page **pages)
325{
326 struct mm_struct *mm = current->mm;
Linus Torvalds9b790222008-07-28 17:54:21 -0700327 unsigned long addr, len, end;
Nick Piggin8174c432008-07-25 19:45:24 -0700328 unsigned long next;
329 pgd_t *pgdp;
330 int nr = 0;
331
Linus Torvalds9b790222008-07-28 17:54:21 -0700332 start &= PAGE_MASK;
333 addr = start;
334 len = (unsigned long) nr_pages << PAGE_SHIFT;
Linus Torvalds7f818902009-06-20 09:52:27 -0700335
Linus Torvalds9b790222008-07-28 17:54:21 -0700336 end = start + len;
Linus Torvalds7f818902009-06-20 09:52:27 -0700337 if (end < start)
Nick Piggin8174c432008-07-25 19:45:24 -0700338 goto slow_irqon;
339
Linus Torvalds7f818902009-06-20 09:52:27 -0700340#ifdef CONFIG_X86_64
341 if (end >> __VIRTUAL_MASK_SHIFT)
342 goto slow_irqon;
343#endif
344
Nick Piggin8174c432008-07-25 19:45:24 -0700345 /*
346 * XXX: batch / limit 'nr', to avoid large irq off latency
347 * needs some instrumenting to determine the common sizes used by
348 * important workloads (eg. DB2), and whether limiting the batch size
349 * will decrease performance.
350 *
351 * It seems like we're in the clear for the moment. Direct-IO is
352 * the main guy that batches up lots of get_user_pages, and even
353 * they are limited to 64-at-a-time which is not so many.
354 */
355 /*
356 * This doesn't prevent pagetable teardown, but does prevent
357 * the pagetables and pages from being freed on x86.
358 *
359 * So long as we atomically load page table pointers versus teardown
360 * (which we do on x86, with the above PAE exception), we can follow the
361 * address down to the the page and take a ref on it.
362 */
363 local_irq_disable();
364 pgdp = pgd_offset(mm, addr);
365 do {
366 pgd_t pgd = *pgdp;
367
368 next = pgd_addr_end(addr, end);
369 if (pgd_none(pgd))
370 goto slow;
371 if (!gup_pud_range(pgd, addr, next, write, pages, &nr))
372 goto slow;
373 } while (pgdp++, addr = next, addr != end);
374 local_irq_enable();
375
376 VM_BUG_ON(nr != (end - start) >> PAGE_SHIFT);
377 return nr;
378
379 {
380 int ret;
381
382slow:
383 local_irq_enable();
384slow_irqon:
385 /* Try to get the remaining pages with get_user_pages */
386 start += nr << PAGE_SHIFT;
387 pages += nr;
388
Andrea Arcangelia7b78072015-02-11 15:27:23 -0800389 ret = get_user_pages_unlocked(current, mm, start,
390 (end - start) >> PAGE_SHIFT,
391 write, 0, pages);
Nick Piggin8174c432008-07-25 19:45:24 -0700392
393 /* Have to be a bit careful with return values */
394 if (nr > 0) {
395 if (ret < 0)
396 ret = nr;
397 else
398 ret += nr;
399 }
400
401 return ret;
402 }
403}