]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - arch/x86/mm/gup.c
Merge branch 'akpm-current/current'
[karo-tx-linux.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 READ_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                 /* Similar to the PMD case, NUMA hinting must take slow path */
87                 if (pte_protnone(pte)) {
88                         pte_unmap(ptep);
89                         return 0;
90                 }
91
92                 if ((pte_flags(pte) & (mask | _PAGE_SPECIAL)) != mask) {
93                         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);
99                 SetPageReferenced(page);
100                 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
109 static inline void get_head_page_multiple(struct page *page, int nr)
110 {
111         VM_BUG_ON_PAGE(page != compound_head(page), page);
112         VM_BUG_ON_PAGE(page_count(page) == 0, page);
113         atomic_add(nr, &page->_count);
114         SetPageReferenced(page);
115 }
116
117 static 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;
121         struct page *head, *page;
122         int refs;
123
124         mask = _PAGE_PRESENT|_PAGE_USER;
125         if (write)
126                 mask |= _PAGE_RW;
127         if ((pmd_flags(pmd) & mask) != mask)
128                 return 0;
129         /* hugepages are never "special" */
130         VM_BUG_ON(pmd_flags(pmd) & _PAGE_SPECIAL);
131         VM_BUG_ON(!pfn_valid(pmd_pfn(pmd)));
132
133         refs = 0;
134         head = pmd_page(pmd);
135         page = head + ((addr & ~PMD_MASK) >> PAGE_SHIFT);
136         do {
137                 VM_BUG_ON_PAGE(compound_head(page) != head, page);
138                 pages[*nr] = page;
139                 (*nr)++;
140                 page++;
141                 refs++;
142         } while (addr += PAGE_SIZE, addr != end);
143         get_head_page_multiple(head, refs);
144
145         return 1;
146 }
147
148 static int gup_pmd_range(pud_t pud, unsigned long addr, unsigned long end,
149                 int write, struct page **pages, int *nr)
150 {
151         unsigned long next;
152         pmd_t *pmdp;
153
154         pmdp = pmd_offset(&pud, addr);
155         do {
156                 pmd_t pmd = *pmdp;
157
158                 next = pmd_addr_end(addr, end);
159                 if (pmd_none(pmd))
160                         return 0;
161                 if (unlikely(pmd_large(pmd) || !pmd_present(pmd))) {
162                         /*
163                          * NUMA hinting faults need to be handled in the GUP
164                          * slowpath for accounting purposes and so that they
165                          * can be serialised against THP migration.
166                          */
167                         if (pmd_protnone(pmd))
168                                 return 0;
169                         if (!gup_huge_pmd(pmd, addr, next, write, pages, nr))
170                                 return 0;
171                 } else {
172                         if (!gup_pte_range(pmd, addr, next, write, pages, nr))
173                                 return 0;
174                 }
175         } while (pmdp++, addr = next, addr != end);
176
177         return 1;
178 }
179
180 static noinline int gup_huge_pud(pud_t pud, unsigned long addr,
181                 unsigned long end, int write, struct page **pages, int *nr)
182 {
183         unsigned long mask;
184         struct page *head, *page;
185         int refs;
186
187         mask = _PAGE_PRESENT|_PAGE_USER;
188         if (write)
189                 mask |= _PAGE_RW;
190         if ((pud_flags(pud) & mask) != mask)
191                 return 0;
192         /* hugepages are never "special" */
193         VM_BUG_ON(pud_flags(pud) & _PAGE_SPECIAL);
194         VM_BUG_ON(!pfn_valid(pud_pfn(pud)));
195
196         refs = 0;
197         head = pud_page(pud);
198         page = head + ((addr & ~PUD_MASK) >> PAGE_SHIFT);
199         do {
200                 VM_BUG_ON_PAGE(compound_head(page) != head, page);
201                 pages[*nr] = page;
202                 (*nr)++;
203                 page++;
204                 refs++;
205         } while (addr += PAGE_SIZE, addr != end);
206         get_head_page_multiple(head, refs);
207
208         return 1;
209 }
210
211 static int gup_pud_range(pgd_t pgd, unsigned long addr, unsigned long end,
212                         int write, struct page **pages, int *nr)
213 {
214         unsigned long next;
215         pud_t *pudp;
216
217         pudp = pud_offset(&pgd, addr);
218         do {
219                 pud_t pud = *pudp;
220
221                 next = pud_addr_end(addr, end);
222                 if (pud_none(pud))
223                         return 0;
224                 if (unlikely(pud_large(pud))) {
225                         if (!gup_huge_pud(pud, addr, next, write, pages, nr))
226                                 return 0;
227                 } else {
228                         if (!gup_pmd_range(pud, addr, next, write, pages, nr))
229                                 return 0;
230                 }
231         } while (pudp++, addr = next, addr != end);
232
233         return 1;
234 }
235
236 /*
237  * Like get_user_pages_fast() except its IRQ-safe in that it won't fall
238  * back to the regular GUP.
239  */
240 int __get_user_pages_fast(unsigned long start, int nr_pages, int write,
241                           struct page **pages)
242 {
243         struct mm_struct *mm = current->mm;
244         unsigned long addr, len, end;
245         unsigned long next;
246         unsigned long flags;
247         pgd_t *pgdp;
248         int nr = 0;
249
250         start &= PAGE_MASK;
251         addr = start;
252         len = (unsigned long) nr_pages << PAGE_SHIFT;
253         end = start + len;
254         if (unlikely(!access_ok(write ? VERIFY_WRITE : VERIFY_READ,
255                                         (void __user *)start, len)))
256                 return 0;
257
258         /*
259          * XXX: batch / limit 'nr', to avoid large irq off latency
260          * needs some instrumenting to determine the common sizes used by
261          * important workloads (eg. DB2), and whether limiting the batch size
262          * will decrease performance.
263          *
264          * It seems like we're in the clear for the moment. Direct-IO is
265          * the main guy that batches up lots of get_user_pages, and even
266          * they are limited to 64-at-a-time which is not so many.
267          */
268         /*
269          * This doesn't prevent pagetable teardown, but does prevent
270          * the pagetables and pages from being freed on x86.
271          *
272          * So long as we atomically load page table pointers versus teardown
273          * (which we do on x86, with the above PAE exception), we can follow the
274          * address down to the the page and take a ref on it.
275          */
276         local_irq_save(flags);
277         pgdp = pgd_offset(mm, addr);
278         do {
279                 pgd_t pgd = *pgdp;
280
281                 next = pgd_addr_end(addr, end);
282                 if (pgd_none(pgd))
283                         break;
284                 if (!gup_pud_range(pgd, addr, next, write, pages, &nr))
285                         break;
286         } while (pgdp++, addr = next, addr != end);
287         local_irq_restore(flags);
288
289         return nr;
290 }
291
292 /**
293  * get_user_pages_fast() - pin user pages in memory
294  * @start:      starting user address
295  * @nr_pages:   number of pages from start to pin
296  * @write:      whether pages will be written to
297  * @pages:      array that receives pointers to the pages pinned.
298  *              Should be at least nr_pages long.
299  *
300  * Attempt to pin user pages in memory without taking mm->mmap_sem.
301  * If not successful, it will fall back to taking the lock and
302  * calling get_user_pages().
303  *
304  * Returns number of pages pinned. This may be fewer than the number
305  * requested. If nr_pages is 0 or negative, returns 0. If no pages
306  * were pinned, returns -errno.
307  */
308 int get_user_pages_fast(unsigned long start, int nr_pages, int write,
309                         struct page **pages)
310 {
311         struct mm_struct *mm = current->mm;
312         unsigned long addr, len, end;
313         unsigned long next;
314         pgd_t *pgdp;
315         int nr = 0;
316
317         start &= PAGE_MASK;
318         addr = start;
319         len = (unsigned long) nr_pages << PAGE_SHIFT;
320
321         end = start + len;
322         if (end < start)
323                 goto slow_irqon;
324
325 #ifdef CONFIG_X86_64
326         if (end >> __VIRTUAL_MASK_SHIFT)
327                 goto slow_irqon;
328 #endif
329
330         /*
331          * XXX: batch / limit 'nr', to avoid large irq off latency
332          * needs some instrumenting to determine the common sizes used by
333          * important workloads (eg. DB2), and whether limiting the batch size
334          * will decrease performance.
335          *
336          * It seems like we're in the clear for the moment. Direct-IO is
337          * the main guy that batches up lots of get_user_pages, and even
338          * they are limited to 64-at-a-time which is not so many.
339          */
340         /*
341          * This doesn't prevent pagetable teardown, but does prevent
342          * the pagetables and pages from being freed on x86.
343          *
344          * So long as we atomically load page table pointers versus teardown
345          * (which we do on x86, with the above PAE exception), we can follow the
346          * address down to the the page and take a ref on it.
347          */
348         local_irq_disable();
349         pgdp = pgd_offset(mm, addr);
350         do {
351                 pgd_t pgd = *pgdp;
352
353                 next = pgd_addr_end(addr, end);
354                 if (pgd_none(pgd))
355                         goto slow;
356                 if (!gup_pud_range(pgd, addr, next, write, pages, &nr))
357                         goto slow;
358         } while (pgdp++, addr = next, addr != end);
359         local_irq_enable();
360
361         VM_BUG_ON(nr != (end - start) >> PAGE_SHIFT);
362         return nr;
363
364         {
365                 int ret;
366
367 slow:
368                 local_irq_enable();
369 slow_irqon:
370                 /* Try to get the remaining pages with get_user_pages */
371                 start += nr << PAGE_SHIFT;
372                 pages += nr;
373
374                 ret = get_user_pages_unlocked(current, mm, start,
375                                               (end - start) >> PAGE_SHIFT,
376                                               write, 0, pages);
377
378                 /* Have to be a bit careful with return values */
379                 if (nr > 0) {
380                         if (ret < 0)
381                                 ret = nr;
382                         else
383                                 ret += nr;
384                 }
385
386                 return ret;
387         }
388 }