3b5032a62b0f3e41c98b98c0842500215e9e3e18
[pandora-kernel.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
13 #include <asm/pgtable.h>
14
15 static inline pte_t gup_get_pte(pte_t *ptep)
16 {
17 #ifndef CONFIG_X86_PAE
18         return ACCESS_ONCE(*ptep);
19 #else
20         /*
21          * With get_user_pages_fast, we walk down the pagetables without taking
22          * any locks.  For this we would like to load the pointers atomically,
23          * 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
54 retry:
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  */
71 static 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
86                 if ((pte_flags(pte) & (mask | _PAGE_SPECIAL)) != mask) {
87                         pte_unmap(ptep);
88                         return 0;
89                 }
90                 VM_BUG_ON(!pfn_valid(pte_pfn(pte)));
91                 page = pte_page(pte);
92                 get_page(page);
93                 SetPageReferenced(page);
94                 pages[*nr] = page;
95                 (*nr)++;
96
97         } while (ptep++, addr += PAGE_SIZE, addr != end);
98         pte_unmap(ptep - 1);
99
100         return 1;
101 }
102
103 static inline void get_head_page_multiple(struct page *page, int nr)
104 {
105         VM_BUG_ON(page != compound_head(page));
106         VM_BUG_ON(page_count(page) == 0);
107         atomic_add(nr, &page->_count);
108         SetPageReferenced(page);
109 }
110
111 static inline void get_huge_page_tail(struct page *page)
112 {
113         /*
114          * __split_huge_page_refcount() cannot run
115          * from under us.
116          */
117         VM_BUG_ON(page_mapcount(page) < 0);
118         VM_BUG_ON(atomic_read(&page->_count) != 0);
119         atomic_inc(&page->_mapcount);
120 }
121
122 static noinline int gup_huge_pmd(pmd_t pmd, unsigned long addr,
123                 unsigned long end, int write, struct page **pages, int *nr)
124 {
125         unsigned long mask;
126         pte_t pte = *(pte_t *)&pmd;
127         struct page *head, *page;
128         int refs;
129
130         mask = _PAGE_PRESENT|_PAGE_USER;
131         if (write)
132                 mask |= _PAGE_RW;
133         if ((pte_flags(pte) & mask) != mask)
134                 return 0;
135         /* hugepages are never "special" */
136         VM_BUG_ON(pte_flags(pte) & _PAGE_SPECIAL);
137         VM_BUG_ON(!pfn_valid(pte_pfn(pte)));
138
139         refs = 0;
140         head = pte_page(pte);
141         page = head + ((addr & ~PMD_MASK) >> PAGE_SHIFT);
142         do {
143                 VM_BUG_ON(compound_head(page) != head);
144                 pages[*nr] = page;
145                 if (PageTail(page))
146                         get_huge_page_tail(page);
147                 (*nr)++;
148                 page++;
149                 refs++;
150         } while (addr += PAGE_SIZE, addr != end);
151         get_head_page_multiple(head, refs);
152
153         return 1;
154 }
155
156 static int gup_pmd_range(pud_t pud, unsigned long addr, unsigned long end,
157                 int write, struct page **pages, int *nr)
158 {
159         unsigned long next;
160         pmd_t *pmdp;
161
162         pmdp = pmd_offset(&pud, addr);
163         do {
164                 pmd_t pmd = *pmdp;
165
166                 next = pmd_addr_end(addr, end);
167                 /*
168                  * The pmd_trans_splitting() check below explains why
169                  * pmdp_splitting_flush has to flush the tlb, to stop
170                  * this gup-fast code from running while we set the
171                  * splitting bit in the pmd. Returning zero will take
172                  * the slow path that will call wait_split_huge_page()
173                  * if the pmd is still in splitting state. gup-fast
174                  * can't because it has irq disabled and
175                  * wait_split_huge_page() would never return as the
176                  * tlb flush IPI wouldn't run.
177                  */
178                 if (pmd_none(pmd) || pmd_trans_splitting(pmd))
179                         return 0;
180                 if (unlikely(pmd_large(pmd))) {
181                         if (!gup_huge_pmd(pmd, addr, next, write, pages, nr))
182                                 return 0;
183                 } else {
184                         if (!gup_pte_range(pmd, addr, next, write, pages, nr))
185                                 return 0;
186                 }
187         } while (pmdp++, addr = next, addr != end);
188
189         return 1;
190 }
191
192 static noinline int gup_huge_pud(pud_t pud, unsigned long addr,
193                 unsigned long end, int write, struct page **pages, int *nr)
194 {
195         unsigned long mask;
196         pte_t pte = *(pte_t *)&pud;
197         struct page *head, *page;
198         int refs;
199
200         mask = _PAGE_PRESENT|_PAGE_USER;
201         if (write)
202                 mask |= _PAGE_RW;
203         if ((pte_flags(pte) & mask) != mask)
204                 return 0;
205         /* hugepages are never "special" */
206         VM_BUG_ON(pte_flags(pte) & _PAGE_SPECIAL);
207         VM_BUG_ON(!pfn_valid(pte_pfn(pte)));
208
209         refs = 0;
210         head = pte_page(pte);
211         page = head + ((addr & ~PUD_MASK) >> PAGE_SHIFT);
212         do {
213                 VM_BUG_ON(compound_head(page) != head);
214                 pages[*nr] = page;
215                 (*nr)++;
216                 page++;
217                 refs++;
218         } while (addr += PAGE_SIZE, addr != end);
219         get_head_page_multiple(head, refs);
220
221         return 1;
222 }
223
224 static int gup_pud_range(pgd_t pgd, unsigned long addr, unsigned long end,
225                         int write, struct page **pages, int *nr)
226 {
227         unsigned long next;
228         pud_t *pudp;
229
230         pudp = pud_offset(&pgd, addr);
231         do {
232                 pud_t pud = *pudp;
233
234                 next = pud_addr_end(addr, end);
235                 if (pud_none(pud))
236                         return 0;
237                 if (unlikely(pud_large(pud))) {
238                         if (!gup_huge_pud(pud, addr, next, write, pages, nr))
239                                 return 0;
240                 } else {
241                         if (!gup_pmd_range(pud, addr, next, write, pages, nr))
242                                 return 0;
243                 }
244         } while (pudp++, addr = next, addr != end);
245
246         return 1;
247 }
248
249 /*
250  * Like get_user_pages_fast() except its IRQ-safe in that it won't fall
251  * back to the regular GUP.
252  */
253 int __get_user_pages_fast(unsigned long start, int nr_pages, int write,
254                           struct page **pages)
255 {
256         struct mm_struct *mm = current->mm;
257         unsigned long addr, len, end;
258         unsigned long next;
259         unsigned long flags;
260         pgd_t *pgdp;
261         int nr = 0;
262
263         start &= PAGE_MASK;
264         addr = start;
265         len = (unsigned long) nr_pages << PAGE_SHIFT;
266         end = start + len;
267         if (unlikely(!access_ok(write ? VERIFY_WRITE : VERIFY_READ,
268                                         (void __user *)start, len)))
269                 return 0;
270
271         /*
272          * XXX: batch / limit 'nr', to avoid large irq off latency
273          * needs some instrumenting to determine the common sizes used by
274          * important workloads (eg. DB2), and whether limiting the batch size
275          * will decrease performance.
276          *
277          * It seems like we're in the clear for the moment. Direct-IO is
278          * the main guy that batches up lots of get_user_pages, and even
279          * they are limited to 64-at-a-time which is not so many.
280          */
281         /*
282          * This doesn't prevent pagetable teardown, but does prevent
283          * the pagetables and pages from being freed on x86.
284          *
285          * So long as we atomically load page table pointers versus teardown
286          * (which we do on x86, with the above PAE exception), we can follow the
287          * address down to the the page and take a ref on it.
288          */
289         local_irq_save(flags);
290         pgdp = pgd_offset(mm, addr);
291         do {
292                 pgd_t pgd = *pgdp;
293
294                 next = pgd_addr_end(addr, end);
295                 if (pgd_none(pgd))
296                         break;
297                 if (!gup_pud_range(pgd, addr, next, write, pages, &nr))
298                         break;
299         } while (pgdp++, addr = next, addr != end);
300         local_irq_restore(flags);
301
302         return nr;
303 }
304
305 /**
306  * get_user_pages_fast() - pin user pages in memory
307  * @start:      starting user address
308  * @nr_pages:   number of pages from start to pin
309  * @write:      whether pages will be written to
310  * @pages:      array that receives pointers to the pages pinned.
311  *              Should be at least nr_pages long.
312  *
313  * Attempt to pin user pages in memory without taking mm->mmap_sem.
314  * If not successful, it will fall back to taking the lock and
315  * calling get_user_pages().
316  *
317  * Returns number of pages pinned. This may be fewer than the number
318  * requested. If nr_pages is 0 or negative, returns 0. If no pages
319  * were pinned, returns -errno.
320  */
321 int get_user_pages_fast(unsigned long start, int nr_pages, int write,
322                         struct page **pages)
323 {
324         struct mm_struct *mm = current->mm;
325         unsigned long addr, len, end;
326         unsigned long next;
327         pgd_t *pgdp;
328         int nr = 0;
329
330         start &= PAGE_MASK;
331         addr = start;
332         len = (unsigned long) nr_pages << PAGE_SHIFT;
333
334         end = start + len;
335         if (end < start)
336                 goto slow_irqon;
337
338 #ifdef CONFIG_X86_64
339         if (end >> __VIRTUAL_MASK_SHIFT)
340                 goto slow_irqon;
341 #endif
342
343         /*
344          * XXX: batch / limit 'nr', to avoid large irq off latency
345          * needs some instrumenting to determine the common sizes used by
346          * important workloads (eg. DB2), and whether limiting the batch size
347          * will decrease performance.
348          *
349          * It seems like we're in the clear for the moment. Direct-IO is
350          * the main guy that batches up lots of get_user_pages, and even
351          * they are limited to 64-at-a-time which is not so many.
352          */
353         /*
354          * This doesn't prevent pagetable teardown, but does prevent
355          * the pagetables and pages from being freed on x86.
356          *
357          * So long as we atomically load page table pointers versus teardown
358          * (which we do on x86, with the above PAE exception), we can follow the
359          * address down to the the page and take a ref on it.
360          */
361         local_irq_disable();
362         pgdp = pgd_offset(mm, addr);
363         do {
364                 pgd_t pgd = *pgdp;
365
366                 next = pgd_addr_end(addr, end);
367                 if (pgd_none(pgd))
368                         goto slow;
369                 if (!gup_pud_range(pgd, addr, next, write, pages, &nr))
370                         goto slow;
371         } while (pgdp++, addr = next, addr != end);
372         local_irq_enable();
373
374         VM_BUG_ON(nr != (end - start) >> PAGE_SHIFT);
375         return nr;
376
377         {
378                 int ret;
379
380 slow:
381                 local_irq_enable();
382 slow_irqon:
383                 /* Try to get the remaining pages with get_user_pages */
384                 start += nr << PAGE_SHIFT;
385                 pages += nr;
386
387                 down_read(&mm->mmap_sem);
388                 ret = get_user_pages(current, mm, start,
389                         (end - start) >> PAGE_SHIFT, write, 0, pages, NULL);
390                 up_read(&mm->mmap_sem);
391
392                 /* Have to be a bit careful with return values */
393                 if (nr > 0) {
394                         if (ret < 0)
395                                 ret = nr;
396                         else
397                                 ret += nr;
398                 }
399
400                 return ret;
401         }
402 }