blob: d8746684f606a6424e1c854445f779796cff3e12 [file] [log] [blame]
Nick Piggince0ad7f2008-07-30 15:23:13 +10001/*
2 * Lockless get_user_pages_fast for powerpc
3 *
4 * Copyright (C) 2008 Nick Piggin
5 * Copyright (C) 2008 Novell Inc.
6 */
7#undef DEBUG
8
9#include <linux/sched.h>
10#include <linux/mm.h>
11#include <linux/hugetlb.h>
12#include <linux/vmstat.h>
13#include <linux/pagemap.h>
14#include <linux/rwsem.h>
15#include <asm/pgtable.h>
16
Benjamin Herrenschmidt9e5efaa2009-03-10 17:24:37 +000017#ifdef __HAVE_ARCH_PTE_SPECIAL
18
Nick Piggince0ad7f2008-07-30 15:23:13 +100019/*
20 * The performance critical leaf functions are made noinline otherwise gcc
21 * inlines everything into a single function which results in too much
22 * register pressure.
23 */
24static noinline int gup_pte_range(pmd_t pmd, unsigned long addr,
25 unsigned long end, int write, struct page **pages, int *nr)
26{
27 unsigned long mask, result;
28 pte_t *ptep;
29
30 result = _PAGE_PRESENT|_PAGE_USER;
31 if (write)
32 result |= _PAGE_RW;
33 mask = result | _PAGE_SPECIAL;
34
35 ptep = pte_offset_kernel(&pmd, addr);
36 do {
Aneesh Kumar K.V7888b4d2013-06-20 14:30:23 +053037 pte_t pte = ACCESS_ONCE(*ptep);
Nick Piggince0ad7f2008-07-30 15:23:13 +100038 struct page *page;
Aneesh Kumar K.V1dc954b2014-04-02 21:37:39 +053039 /*
40 * Similar to the PMD case, NUMA hinting must take slow path
41 */
42 if (pte_numa(pte))
43 return 0;
Nick Piggince0ad7f2008-07-30 15:23:13 +100044
45 if ((pte_val(pte) & mask) != result)
46 return 0;
47 VM_BUG_ON(!pfn_valid(pte_pfn(pte)));
48 page = pte_page(pte);
49 if (!page_cache_get_speculative(page))
50 return 0;
David Gibsonf5ea64d2008-10-12 17:54:24 +000051 if (unlikely(pte_val(pte) != pte_val(*ptep))) {
Nick Piggince0ad7f2008-07-30 15:23:13 +100052 put_page(page);
53 return 0;
54 }
55 pages[*nr] = page;
56 (*nr)++;
57
58 } while (ptep++, addr += PAGE_SIZE, addr != end);
59
60 return 1;
61}
62
Nick Piggince0ad7f2008-07-30 15:23:13 +100063static int gup_pmd_range(pud_t pud, unsigned long addr, unsigned long end,
64 int write, struct page **pages, int *nr)
65{
66 unsigned long next;
67 pmd_t *pmdp;
68
69 pmdp = pmd_offset(&pud, addr);
70 do {
Aneesh Kumar K.V7888b4d2013-06-20 14:30:23 +053071 pmd_t pmd = ACCESS_ONCE(*pmdp);
Nick Piggince0ad7f2008-07-30 15:23:13 +100072
73 next = pmd_addr_end(addr, end);
Aneesh Kumar K.Vc3677142013-06-20 14:30:20 +053074 /*
75 * If we find a splitting transparent hugepage we
76 * return zero. That will result in taking the slow
77 * path which will call wait_split_huge_page()
78 * if the pmd is still in splitting state
79 */
80 if (pmd_none(pmd) || pmd_trans_splitting(pmd))
Nick Piggince0ad7f2008-07-30 15:23:13 +100081 return 0;
Aneesh Kumar K.Vc3677142013-06-20 14:30:20 +053082 if (pmd_huge(pmd) || pmd_large(pmd)) {
Aneesh Kumar K.V1dc954b2014-04-02 21:37:39 +053083 /*
84 * NUMA hinting faults need to be handled in the GUP
85 * slowpath for accounting purposes and so that they
86 * can be serialised against THP migration.
87 */
88 if (pmd_numa(pmd))
89 return 0;
90
Aneesh Kumar K.Ve2b3d202013-04-28 09:37:30 +000091 if (!gup_hugepte((pte_t *)pmdp, PMD_SIZE, addr, next,
92 write, pages, nr))
93 return 0;
94 } else if (is_hugepd(pmdp)) {
David Gibsona4fe3ce2009-10-26 19:24:31 +000095 if (!gup_hugepd((hugepd_t *)pmdp, PMD_SHIFT,
96 addr, next, write, pages, nr))
97 return 0;
98 } else if (!gup_pte_range(pmd, addr, next, write, pages, nr))
Nick Piggince0ad7f2008-07-30 15:23:13 +100099 return 0;
100 } while (pmdp++, addr = next, addr != end);
101
102 return 1;
103}
104
105static int gup_pud_range(pgd_t pgd, unsigned long addr, unsigned long end,
106 int write, struct page **pages, int *nr)
107{
108 unsigned long next;
109 pud_t *pudp;
110
111 pudp = pud_offset(&pgd, addr);
112 do {
Aneesh Kumar K.V7888b4d2013-06-20 14:30:23 +0530113 pud_t pud = ACCESS_ONCE(*pudp);
Nick Piggince0ad7f2008-07-30 15:23:13 +1000114
115 next = pud_addr_end(addr, end);
116 if (pud_none(pud))
117 return 0;
Aneesh Kumar K.Ve2b3d202013-04-28 09:37:30 +0000118 if (pud_huge(pud)) {
119 if (!gup_hugepte((pte_t *)pudp, PUD_SIZE, addr, next,
120 write, pages, nr))
121 return 0;
122 } else if (is_hugepd(pudp)) {
David Gibsona4fe3ce2009-10-26 19:24:31 +0000123 if (!gup_hugepd((hugepd_t *)pudp, PUD_SHIFT,
124 addr, next, write, pages, nr))
125 return 0;
126 } else if (!gup_pmd_range(pud, addr, next, write, pages, nr))
Nick Piggince0ad7f2008-07-30 15:23:13 +1000127 return 0;
128 } while (pudp++, addr = next, addr != end);
129
130 return 1;
131}
132
Paul Mackerras1f7bf022013-08-05 14:11:23 +1000133int __get_user_pages_fast(unsigned long start, int nr_pages, int write,
134 struct page **pages)
Nick Piggince0ad7f2008-07-30 15:23:13 +1000135{
136 struct mm_struct *mm = current->mm;
137 unsigned long addr, len, end;
138 unsigned long next;
Heiko Carstens95f715b2013-11-14 15:01:43 +1100139 unsigned long flags;
Nick Piggince0ad7f2008-07-30 15:23:13 +1000140 pgd_t *pgdp;
Benjamin Herrenschmidt9e5efaa2009-03-10 17:24:37 +0000141 int nr = 0;
Nick Piggince0ad7f2008-07-30 15:23:13 +1000142
Michael Ellerman29e5fa592009-06-17 18:13:51 +0000143 pr_devel("%s(%lx,%x,%s)\n", __func__, start, nr_pages, write ? "write" : "read");
Nick Piggince0ad7f2008-07-30 15:23:13 +1000144
145 start &= PAGE_MASK;
146 addr = start;
147 len = (unsigned long) nr_pages << PAGE_SHIFT;
148 end = start + len;
149
150 if (unlikely(!access_ok(write ? VERIFY_WRITE : VERIFY_READ,
151 start, len)))
Paul Mackerras1f7bf022013-08-05 14:11:23 +1000152 return 0;
Nick Piggince0ad7f2008-07-30 15:23:13 +1000153
Michael Ellerman29e5fa592009-06-17 18:13:51 +0000154 pr_devel(" aligned: %lx .. %lx\n", start, end);
Nick Piggince0ad7f2008-07-30 15:23:13 +1000155
Nick Piggince0ad7f2008-07-30 15:23:13 +1000156 /*
157 * XXX: batch / limit 'nr', to avoid large irq off latency
158 * needs some instrumenting to determine the common sizes used by
159 * important workloads (eg. DB2), and whether limiting the batch size
160 * will decrease performance.
161 *
162 * It seems like we're in the clear for the moment. Direct-IO is
163 * the main guy that batches up lots of get_user_pages, and even
164 * they are limited to 64-at-a-time which is not so many.
165 */
166 /*
167 * This doesn't prevent pagetable teardown, but does prevent
168 * the pagetables from being freed on powerpc.
169 *
170 * So long as we atomically load page table pointers versus teardown,
171 * we can follow the address down to the the page and take a ref on it.
172 */
Heiko Carstens95f715b2013-11-14 15:01:43 +1100173 local_irq_save(flags);
Nick Piggince0ad7f2008-07-30 15:23:13 +1000174
David Gibsona4fe3ce2009-10-26 19:24:31 +0000175 pgdp = pgd_offset(mm, addr);
176 do {
Aneesh Kumar K.V7888b4d2013-06-20 14:30:23 +0530177 pgd_t pgd = ACCESS_ONCE(*pgdp);
Nick Piggince0ad7f2008-07-30 15:23:13 +1000178
David Gibsona4fe3ce2009-10-26 19:24:31 +0000179 pr_devel(" %016lx: normal pgd %p\n", addr,
180 (void *)pgd_val(pgd));
181 next = pgd_addr_end(addr, end);
182 if (pgd_none(pgd))
Paul Mackerras1f7bf022013-08-05 14:11:23 +1000183 break;
Aneesh Kumar K.Ve2b3d202013-04-28 09:37:30 +0000184 if (pgd_huge(pgd)) {
185 if (!gup_hugepte((pte_t *)pgdp, PGDIR_SIZE, addr, next,
186 write, pages, &nr))
Paul Mackerras1f7bf022013-08-05 14:11:23 +1000187 break;
Aneesh Kumar K.Ve2b3d202013-04-28 09:37:30 +0000188 } else if (is_hugepd(pgdp)) {
David Gibsona4fe3ce2009-10-26 19:24:31 +0000189 if (!gup_hugepd((hugepd_t *)pgdp, PGDIR_SHIFT,
190 addr, next, write, pages, &nr))
Paul Mackerras1f7bf022013-08-05 14:11:23 +1000191 break;
David Gibsona4fe3ce2009-10-26 19:24:31 +0000192 } else if (!gup_pud_range(pgd, addr, next, write, pages, &nr))
Paul Mackerras1f7bf022013-08-05 14:11:23 +1000193 break;
David Gibsona4fe3ce2009-10-26 19:24:31 +0000194 } while (pgdp++, addr = next, addr != end);
Nick Piggince0ad7f2008-07-30 15:23:13 +1000195
Heiko Carstens95f715b2013-11-14 15:01:43 +1100196 local_irq_restore(flags);
Nick Piggince0ad7f2008-07-30 15:23:13 +1000197
Nick Piggince0ad7f2008-07-30 15:23:13 +1000198 return nr;
Paul Mackerras1f7bf022013-08-05 14:11:23 +1000199}
Nick Piggince0ad7f2008-07-30 15:23:13 +1000200
Paul Mackerras1f7bf022013-08-05 14:11:23 +1000201int get_user_pages_fast(unsigned long start, int nr_pages, int write,
202 struct page **pages)
203{
204 struct mm_struct *mm = current->mm;
205 int nr, ret;
Nick Piggince0ad7f2008-07-30 15:23:13 +1000206
Paul Mackerras1f7bf022013-08-05 14:11:23 +1000207 start &= PAGE_MASK;
208 nr = __get_user_pages_fast(start, nr_pages, write, pages);
209 ret = nr;
210
211 if (nr < nr_pages) {
Michael Ellerman29e5fa592009-06-17 18:13:51 +0000212 pr_devel(" slow path ! nr = %d\n", nr);
Nick Piggince0ad7f2008-07-30 15:23:13 +1000213
214 /* Try to get the remaining pages with get_user_pages */
215 start += nr << PAGE_SHIFT;
216 pages += nr;
217
218 down_read(&mm->mmap_sem);
219 ret = get_user_pages(current, mm, start,
Paul Mackerras1f7bf022013-08-05 14:11:23 +1000220 nr_pages - nr, write, 0, pages, NULL);
Nick Piggince0ad7f2008-07-30 15:23:13 +1000221 up_read(&mm->mmap_sem);
222
223 /* Have to be a bit careful with return values */
224 if (nr > 0) {
225 if (ret < 0)
226 ret = nr;
227 else
228 ret += nr;
229 }
Nick Piggince0ad7f2008-07-30 15:23:13 +1000230 }
Paul Mackerras1f7bf022013-08-05 14:11:23 +1000231
232 return ret;
Nick Piggince0ad7f2008-07-30 15:23:13 +1000233}
Benjamin Herrenschmidt9e5efaa2009-03-10 17:24:37 +0000234
235#endif /* __HAVE_ARCH_PTE_SPECIAL */