GNU Linux-libre 4.9.337-gnu1
[releases.git] / arch / x86 / mm / gup.c
1 /*
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>
11 #include <linux/swap.h>
12 #include <linux/memremap.h>
13
14 #include <asm/mmu_context.h>
15 #include <asm/pgtable.h>
16
17 static inline pte_t gup_get_pte(pte_t *ptep)
18 {
19 #ifndef CONFIG_X86_PAE
20         return READ_ONCE(*ptep);
21 #else
22         /*
23          * With get_user_pages_fast, we walk down the pagetables without taking
24          * any locks.  For this we would like to load the pointers atomically,
25          * 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
56 retry:
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
68 static 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
78 /*
79  * '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  */
83 static 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
93         /* Check memory protection keys permissions. */
94         if (!__pkru_allows_pkey(pte_flags_pkey(pteval), write))
95                 return 0;
96
97         return 1;
98 }
99
100 /*
101  * Return the compund head page with ref appropriately incremented,
102  * or NULL if that failed.
103  */
104 static 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 /*
115  * 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  */
119 static noinline int gup_pte_range(pmd_t pmd, unsigned long addr,
120                 unsigned long end, int write, struct page **pages, int *nr)
121 {
122         struct dev_pagemap *pgmap = NULL;
123         int nr_start = *nr;
124         pte_t *ptep;
125
126         ptep = pte_offset_map(&pmd, addr);
127         do {
128                 pte_t pte = gup_get_pte(ptep);
129                 struct page *head, *page;
130
131                 /* Similar to the PMD case, NUMA hinting must take slow path */
132                 if (pte_protnone(pte)) {
133                         pte_unmap(ptep);
134                         return 0;
135                 }
136
137                 if (!pte_allows_gup(pte_val(pte), write)) {
138                         pte_unmap(ptep);
139                         return 0;
140                 }
141
142                 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                         }
149                 } else if (pte_special(pte)) {
150                         pte_unmap(ptep);
151                         return 0;
152                 }
153                 VM_BUG_ON(!pfn_valid(pte_pfn(pte)));
154                 page = pte_page(pte);
155
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
170                 put_dev_pagemap(pgmap);
171                 SetPageReferenced(page);
172                 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
181 static inline void get_head_page_multiple(struct page *page, int nr)
182 {
183         VM_BUG_ON_PAGE(page != compound_head(page), page);
184         VM_BUG_ON_PAGE(page_count(page) == 0, page);
185         page_ref_add(page, nr);
186         SetPageReferenced(page);
187 }
188
189 static 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                 if (unlikely(!try_get_page(page))) {
206                         put_dev_pagemap(pgmap);
207                         return 0;
208                 }
209                 SetPageReferenced(page);
210                 pages[*nr] = page;
211                 put_dev_pagemap(pgmap);
212                 (*nr)++;
213                 pfn++;
214         } while (addr += PAGE_SIZE, addr != end);
215         return 1;
216 }
217
218 static noinline int gup_huge_pmd(pmd_t pmd, unsigned long addr,
219                 unsigned long end, int write, struct page **pages, int *nr)
220 {
221         struct page *head, *page;
222         int refs;
223
224         if (!pte_allows_gup(pmd_val(pmd), write))
225                 return 0;
226
227         VM_BUG_ON(!pfn_valid(pmd_pfn(pmd)));
228         if (pmd_devmap(pmd))
229                 return __gup_device_huge_pmd(pmd, addr, end, pages, nr);
230
231         /* hugepages are never "special" */
232         VM_BUG_ON(pmd_flags(pmd) & _PAGE_SPECIAL);
233
234         refs = 0;
235         head = pmd_page(pmd);
236         if (WARN_ON_ONCE(page_ref_count(head) <= 0))
237                 return 0;
238         page = head + ((addr & ~PMD_MASK) >> PAGE_SHIFT);
239         do {
240                 VM_BUG_ON_PAGE(compound_head(page) != head, page);
241                 pages[*nr] = page;
242                 (*nr)++;
243                 page++;
244                 refs++;
245         } while (addr += PAGE_SIZE, addr != end);
246         get_head_page_multiple(head, refs);
247
248         return 1;
249 }
250
251 static int gup_pmd_range(pud_t pud, unsigned long addr, unsigned long end,
252                 int write, struct page **pages, int *nr)
253 {
254         unsigned long next;
255         pmd_t *pmdp;
256
257         pmdp = pmd_offset(&pud, addr);
258         do {
259                 pmd_t pmd = *pmdp;
260
261                 next = pmd_addr_end(addr, end);
262                 if (pmd_none(pmd))
263                         return 0;
264                 if (unlikely(pmd_large(pmd) || !pmd_present(pmd))) {
265                         /*
266                          * NUMA hinting faults need to be handled in the GUP
267                          * slowpath for accounting purposes and so that they
268                          * can be serialised against THP migration.
269                          */
270                         if (pmd_protnone(pmd))
271                                 return 0;
272                         if (!gup_huge_pmd(pmd, addr, next, write, pages, nr))
273                                 return 0;
274                 } else {
275                         if (!gup_pte_range(pmd, addr, next, write, pages, nr))
276                                 return 0;
277                 }
278         } while (pmdp++, addr = next, addr != end);
279
280         return 1;
281 }
282
283 static noinline int gup_huge_pud(pud_t pud, unsigned long addr,
284                 unsigned long end, int write, struct page **pages, int *nr)
285 {
286         struct page *head, *page;
287         int refs;
288
289         if (!pte_allows_gup(pud_val(pud), write))
290                 return 0;
291         /* hugepages are never "special" */
292         VM_BUG_ON(pud_flags(pud) & _PAGE_SPECIAL);
293         VM_BUG_ON(!pfn_valid(pud_pfn(pud)));
294
295         refs = 0;
296         head = pud_page(pud);
297         if (WARN_ON_ONCE(page_ref_count(head) <= 0))
298                 return 0;
299         page = head + ((addr & ~PUD_MASK) >> PAGE_SHIFT);
300         do {
301                 VM_BUG_ON_PAGE(compound_head(page) != head, page);
302                 pages[*nr] = page;
303                 (*nr)++;
304                 page++;
305                 refs++;
306         } while (addr += PAGE_SIZE, addr != end);
307         get_head_page_multiple(head, refs);
308
309         return 1;
310 }
311
312 static int gup_pud_range(pgd_t pgd, unsigned long addr, unsigned long end,
313                         int write, struct page **pages, int *nr)
314 {
315         unsigned long next;
316         pud_t *pudp;
317
318         pudp = pud_offset(&pgd, addr);
319         do {
320                 pud_t pud = *pudp;
321
322                 next = pud_addr_end(addr, end);
323                 if (pud_none(pud))
324                         return 0;
325                 if (unlikely(pud_large(pud))) {
326                         if (!gup_huge_pud(pud, addr, next, write, pages, nr))
327                                 return 0;
328                 } else {
329                         if (!gup_pmd_range(pud, addr, next, write, pages, nr))
330                                 return 0;
331                 }
332         } while (pudp++, addr = next, addr != end);
333
334         return 1;
335 }
336
337 /*
338  * Like get_user_pages_fast() except its IRQ-safe in that it won't fall
339  * back to the regular GUP.
340  */
341 int __get_user_pages_fast(unsigned long start, int nr_pages, int write,
342                           struct page **pages)
343 {
344         struct mm_struct *mm = current->mm;
345         unsigned long addr, len, end;
346         unsigned long next;
347         unsigned long flags;
348         pgd_t *pgdp;
349         int nr = 0;
350
351         start &= PAGE_MASK;
352         addr = start;
353         len = (unsigned long) nr_pages << PAGE_SHIFT;
354         end = start + len;
355         if (unlikely(!access_ok(write ? VERIFY_WRITE : VERIFY_READ,
356                                         (void __user *)start, len)))
357                 return 0;
358
359         /*
360          * XXX: batch / limit 'nr', to avoid large irq off latency
361          * needs some instrumenting to determine the common sizes used by
362          * important workloads (eg. DB2), and whether limiting the batch size
363          * will decrease performance.
364          *
365          * It seems like we're in the clear for the moment. Direct-IO is
366          * the main guy that batches up lots of get_user_pages, and even
367          * they are limited to 64-at-a-time which is not so many.
368          */
369         /*
370          * This doesn't prevent pagetable teardown, but does prevent
371          * the pagetables and pages from being freed on x86.
372          *
373          * So long as we atomically load page table pointers versus teardown
374          * (which we do on x86, with the above PAE exception), we can follow the
375          * address down to the the page and take a ref on it.
376          */
377         local_irq_save(flags);
378         pgdp = pgd_offset(mm, addr);
379         do {
380                 pgd_t pgd = *pgdp;
381
382                 next = pgd_addr_end(addr, end);
383                 if (pgd_none(pgd))
384                         break;
385                 if (!gup_pud_range(pgd, addr, next, write, pages, &nr))
386                         break;
387         } while (pgdp++, addr = next, addr != end);
388         local_irq_restore(flags);
389
390         return nr;
391 }
392
393 /**
394  * get_user_pages_fast() - pin user pages in memory
395  * @start:      starting user address
396  * @nr_pages:   number of pages from start to pin
397  * @write:      whether pages will be written to
398  * @pages:      array that receives pointers to the pages pinned.
399  *              Should be at least nr_pages long.
400  *
401  * Attempt to pin user pages in memory without taking mm->mmap_sem.
402  * If not successful, it will fall back to taking the lock and
403  * calling get_user_pages().
404  *
405  * Returns number of pages pinned. This may be fewer than the number
406  * requested. If nr_pages is 0 or negative, returns 0. If no pages
407  * were pinned, returns -errno.
408  */
409 int get_user_pages_fast(unsigned long start, int nr_pages, int write,
410                         struct page **pages)
411 {
412         struct mm_struct *mm = current->mm;
413         unsigned long addr, len, end;
414         unsigned long next;
415         pgd_t *pgdp;
416         int nr = 0;
417
418         start &= PAGE_MASK;
419         addr = start;
420         len = (unsigned long) nr_pages << PAGE_SHIFT;
421
422         end = start + len;
423         if (end < start)
424                 goto slow_irqon;
425
426 #ifdef CONFIG_X86_64
427         if (end >> __VIRTUAL_MASK_SHIFT)
428                 goto slow_irqon;
429 #endif
430
431         /*
432          * XXX: batch / limit 'nr', to avoid large irq off latency
433          * needs some instrumenting to determine the common sizes used by
434          * important workloads (eg. DB2), and whether limiting the batch size
435          * will decrease performance.
436          *
437          * It seems like we're in the clear for the moment. Direct-IO is
438          * the main guy that batches up lots of get_user_pages, and even
439          * they are limited to 64-at-a-time which is not so many.
440          */
441         /*
442          * This doesn't prevent pagetable teardown, but does prevent
443          * the pagetables and pages from being freed on x86.
444          *
445          * So long as we atomically load page table pointers versus teardown
446          * (which we do on x86, with the above PAE exception), we can follow the
447          * address down to the the page and take a ref on it.
448          */
449         local_irq_disable();
450         pgdp = pgd_offset(mm, addr);
451         do {
452                 pgd_t pgd = *pgdp;
453
454                 next = pgd_addr_end(addr, end);
455                 if (pgd_none(pgd))
456                         goto slow;
457                 /*
458                  * The FAST_GUP case requires FOLL_WRITE even for pure reads,
459                  * because get_user_pages() may need to cause an early COW in
460                  * order to avoid confusing the normal COW routines. So only
461                  * targets that are already writable are safe to do by just
462                  * looking at the page tables.
463                  */
464                 if (!gup_pud_range(pgd, addr, next, 1, pages, &nr))
465                         goto slow;
466         } while (pgdp++, addr = next, addr != end);
467         local_irq_enable();
468
469         VM_BUG_ON(nr != (end - start) >> PAGE_SHIFT);
470         return nr;
471
472         {
473                 int ret;
474
475 slow:
476                 local_irq_enable();
477 slow_irqon:
478                 /* Try to get the remaining pages with get_user_pages */
479                 start += nr << PAGE_SHIFT;
480                 pages += nr;
481
482                 ret = get_user_pages_unlocked(start,
483                                               (end - start) >> PAGE_SHIFT,
484                                               pages, write ? FOLL_WRITE : 0);
485
486                 /* Have to be a bit careful with return values */
487                 if (nr > 0) {
488                         if (ret < 0)
489                                 ret = nr;
490                         else
491                                 ret += nr;
492                 }
493
494                 return ret;
495         }
496 }