Commit | Line | Data |
---|---|---|
b1c10bea HD |
1 | /* |
2 | * Lockless get_user_pages_fast for MIPS | |
3 | * | |
4 | * Copyright (C) 2008 Nick Piggin | |
5 | * Copyright (C) 2008 Novell Inc. | |
6 | * Copyright (C) 2011 Ralf Baechle | |
7 | */ | |
8 | #include <linux/sched.h> | |
9 | #include <linux/mm.h> | |
10 | #include <linux/vmstat.h> | |
11 | #include <linux/highmem.h> | |
12 | #include <linux/swap.h> | |
13 | #include <linux/hugetlb.h> | |
14 | ||
5b167c12 | 15 | #include <asm/cpu-features.h> |
b1c10bea HD |
16 | #include <asm/pgtable.h> |
17 | ||
18 | static inline pte_t gup_get_pte(pte_t *ptep) | |
19 | { | |
34adb28d | 20 | #if defined(CONFIG_PHYS_ADDR_T_64BIT) && defined(CONFIG_CPU_MIPS32) |
b1c10bea HD |
21 | pte_t pte; |
22 | ||
23 | retry: | |
24 | pte.pte_low = ptep->pte_low; | |
25 | smp_rmb(); | |
26 | pte.pte_high = ptep->pte_high; | |
27 | smp_rmb(); | |
28 | if (unlikely(pte.pte_low != ptep->pte_low)) | |
29 | goto retry; | |
30 | ||
31 | return pte; | |
32 | #else | |
4218091c | 33 | return READ_ONCE(*ptep); |
b1c10bea HD |
34 | #endif |
35 | } | |
36 | ||
37 | static int gup_pte_range(pmd_t pmd, unsigned long addr, unsigned long end, | |
38 | int write, struct page **pages, int *nr) | |
39 | { | |
40 | pte_t *ptep = pte_offset_map(&pmd, addr); | |
41 | do { | |
42 | pte_t pte = gup_get_pte(ptep); | |
43 | struct page *page; | |
44 | ||
45 | if (!pte_present(pte) || | |
46 | pte_special(pte) || (write && !pte_write(pte))) { | |
47 | pte_unmap(ptep); | |
48 | return 0; | |
49 | } | |
50 | VM_BUG_ON(!pfn_valid(pte_pfn(pte))); | |
51 | page = pte_page(pte); | |
52 | get_page(page); | |
53 | SetPageReferenced(page); | |
54 | pages[*nr] = page; | |
55 | (*nr)++; | |
56 | ||
57 | } while (ptep++, addr += PAGE_SIZE, addr != end); | |
58 | ||
59 | pte_unmap(ptep - 1); | |
60 | return 1; | |
61 | } | |
62 | ||
63 | static inline void get_head_page_multiple(struct page *page, int nr) | |
64 | { | |
65 | VM_BUG_ON(page != compound_head(page)); | |
66 | VM_BUG_ON(page_count(page) == 0); | |
67 | atomic_add(nr, &page->_count); | |
68 | SetPageReferenced(page); | |
69 | } | |
70 | ||
71 | static int gup_huge_pmd(pmd_t pmd, unsigned long addr, unsigned long end, | |
72 | int write, struct page **pages, int *nr) | |
73 | { | |
74 | pte_t pte = *(pte_t *)&pmd; | |
75 | struct page *head, *page; | |
76 | int refs; | |
77 | ||
78 | if (write && !pte_write(pte)) | |
79 | return 0; | |
80 | /* hugepages are never "special" */ | |
81 | VM_BUG_ON(pte_special(pte)); | |
82 | VM_BUG_ON(!pfn_valid(pte_pfn(pte))); | |
83 | ||
84 | refs = 0; | |
85 | head = pte_page(pte); | |
86 | page = head + ((addr & ~PMD_MASK) >> PAGE_SHIFT); | |
87 | do { | |
88 | VM_BUG_ON(compound_head(page) != head); | |
89 | pages[*nr] = page; | |
b1c10bea HD |
90 | (*nr)++; |
91 | page++; | |
92 | refs++; | |
93 | } while (addr += PAGE_SIZE, addr != end); | |
94 | ||
95 | get_head_page_multiple(head, refs); | |
96 | return 1; | |
97 | } | |
98 | ||
99 | static int gup_pmd_range(pud_t pud, unsigned long addr, unsigned long end, | |
100 | int write, struct page **pages, int *nr) | |
101 | { | |
102 | unsigned long next; | |
103 | pmd_t *pmdp; | |
104 | ||
105 | pmdp = pmd_offset(&pud, addr); | |
106 | do { | |
107 | pmd_t pmd = *pmdp; | |
108 | ||
109 | next = pmd_addr_end(addr, end); | |
110 | /* | |
111 | * The pmd_trans_splitting() check below explains why | |
112 | * pmdp_splitting_flush has to flush the tlb, to stop | |
113 | * this gup-fast code from running while we set the | |
114 | * splitting bit in the pmd. Returning zero will take | |
115 | * the slow path that will call wait_split_huge_page() | |
116 | * if the pmd is still in splitting state. gup-fast | |
117 | * can't because it has irq disabled and | |
118 | * wait_split_huge_page() would never return as the | |
119 | * tlb flush IPI wouldn't run. | |
120 | */ | |
121 | if (pmd_none(pmd) || pmd_trans_splitting(pmd)) | |
122 | return 0; | |
123 | if (unlikely(pmd_huge(pmd))) { | |
124 | if (!gup_huge_pmd(pmd, addr, next, write, pages,nr)) | |
125 | return 0; | |
126 | } else { | |
127 | if (!gup_pte_range(pmd, addr, next, write, pages,nr)) | |
128 | return 0; | |
129 | } | |
130 | } while (pmdp++, addr = next, addr != end); | |
131 | ||
132 | return 1; | |
133 | } | |
134 | ||
135 | static int gup_huge_pud(pud_t pud, unsigned long addr, unsigned long end, | |
136 | int write, struct page **pages, int *nr) | |
137 | { | |
138 | pte_t pte = *(pte_t *)&pud; | |
139 | struct page *head, *page; | |
140 | int refs; | |
141 | ||
142 | if (write && !pte_write(pte)) | |
143 | return 0; | |
144 | /* hugepages are never "special" */ | |
145 | VM_BUG_ON(pte_special(pte)); | |
146 | VM_BUG_ON(!pfn_valid(pte_pfn(pte))); | |
147 | ||
148 | refs = 0; | |
149 | head = pte_page(pte); | |
150 | page = head + ((addr & ~PUD_MASK) >> PAGE_SHIFT); | |
151 | do { | |
152 | VM_BUG_ON(compound_head(page) != head); | |
153 | pages[*nr] = page; | |
154 | (*nr)++; | |
155 | page++; | |
156 | refs++; | |
157 | } while (addr += PAGE_SIZE, addr != end); | |
158 | ||
159 | get_head_page_multiple(head, refs); | |
160 | return 1; | |
161 | } | |
162 | ||
163 | static int gup_pud_range(pgd_t pgd, unsigned long addr, unsigned long end, | |
164 | int write, struct page **pages, int *nr) | |
165 | { | |
166 | unsigned long next; | |
167 | pud_t *pudp; | |
168 | ||
169 | pudp = pud_offset(&pgd, addr); | |
170 | do { | |
171 | pud_t pud = *pudp; | |
172 | ||
173 | next = pud_addr_end(addr, end); | |
174 | if (pud_none(pud)) | |
175 | return 0; | |
176 | if (unlikely(pud_huge(pud))) { | |
177 | if (!gup_huge_pud(pud, addr, next, write, pages,nr)) | |
178 | return 0; | |
179 | } else { | |
180 | if (!gup_pmd_range(pud, addr, next, write, pages,nr)) | |
181 | return 0; | |
182 | } | |
183 | } while (pudp++, addr = next, addr != end); | |
184 | ||
185 | return 1; | |
186 | } | |
187 | ||
188 | /* | |
189 | * Like get_user_pages_fast() except its IRQ-safe in that it won't fall | |
190 | * back to the regular GUP. | |
191 | */ | |
192 | int __get_user_pages_fast(unsigned long start, int nr_pages, int write, | |
193 | struct page **pages) | |
194 | { | |
195 | struct mm_struct *mm = current->mm; | |
196 | unsigned long addr, len, end; | |
197 | unsigned long next; | |
198 | unsigned long flags; | |
199 | pgd_t *pgdp; | |
200 | int nr = 0; | |
201 | ||
202 | start &= PAGE_MASK; | |
203 | addr = start; | |
204 | len = (unsigned long) nr_pages << PAGE_SHIFT; | |
205 | end = start + len; | |
206 | if (unlikely(!access_ok(write ? VERIFY_WRITE : VERIFY_READ, | |
207 | (void __user *)start, len))) | |
208 | return 0; | |
209 | ||
210 | /* | |
211 | * XXX: batch / limit 'nr', to avoid large irq off latency | |
212 | * needs some instrumenting to determine the common sizes used by | |
213 | * important workloads (eg. DB2), and whether limiting the batch | |
214 | * size will decrease performance. | |
215 | * | |
216 | * It seems like we're in the clear for the moment. Direct-IO is | |
217 | * the main guy that batches up lots of get_user_pages, and even | |
218 | * they are limited to 64-at-a-time which is not so many. | |
219 | */ | |
220 | /* | |
221 | * This doesn't prevent pagetable teardown, but does prevent | |
222 | * the pagetables and pages from being freed. | |
223 | * | |
224 | * So long as we atomically load page table pointers versus teardown, | |
225 | * we can follow the address down to the page and take a ref on it. | |
226 | */ | |
227 | local_irq_save(flags); | |
228 | pgdp = pgd_offset(mm, addr); | |
229 | do { | |
230 | pgd_t pgd = *pgdp; | |
231 | ||
232 | next = pgd_addr_end(addr, end); | |
233 | if (pgd_none(pgd)) | |
234 | break; | |
235 | if (!gup_pud_range(pgd, addr, next, write, pages, &nr)) | |
236 | break; | |
237 | } while (pgdp++, addr = next, addr != end); | |
238 | local_irq_restore(flags); | |
239 | ||
240 | return nr; | |
241 | } | |
242 | ||
243 | /** | |
244 | * get_user_pages_fast() - pin user pages in memory | |
245 | * @start: starting user address | |
246 | * @nr_pages: number of pages from start to pin | |
247 | * @write: whether pages will be written to | |
248 | * @pages: array that receives pointers to the pages pinned. | |
70342287 | 249 | * Should be at least nr_pages long. |
b1c10bea HD |
250 | * |
251 | * Attempt to pin user pages in memory without taking mm->mmap_sem. | |
252 | * If not successful, it will fall back to taking the lock and | |
253 | * calling get_user_pages(). | |
254 | * | |
255 | * Returns number of pages pinned. This may be fewer than the number | |
256 | * requested. If nr_pages is 0 or negative, returns 0. If no pages | |
257 | * were pinned, returns -errno. | |
258 | */ | |
259 | int get_user_pages_fast(unsigned long start, int nr_pages, int write, | |
260 | struct page **pages) | |
261 | { | |
262 | struct mm_struct *mm = current->mm; | |
263 | unsigned long addr, len, end; | |
264 | unsigned long next; | |
265 | pgd_t *pgdp; | |
266 | int ret, nr = 0; | |
267 | ||
268 | start &= PAGE_MASK; | |
269 | addr = start; | |
270 | len = (unsigned long) nr_pages << PAGE_SHIFT; | |
271 | ||
272 | end = start + len; | |
5b167c12 | 273 | if (end < start || cpu_has_dc_aliases) |
b1c10bea HD |
274 | goto slow_irqon; |
275 | ||
276 | /* XXX: batch / limit 'nr' */ | |
277 | local_irq_disable(); | |
278 | pgdp = pgd_offset(mm, addr); | |
279 | do { | |
280 | pgd_t pgd = *pgdp; | |
281 | ||
282 | next = pgd_addr_end(addr, end); | |
283 | if (pgd_none(pgd)) | |
284 | goto slow; | |
285 | if (!gup_pud_range(pgd, addr, next, write, pages, &nr)) | |
286 | goto slow; | |
287 | } while (pgdp++, addr = next, addr != end); | |
288 | local_irq_enable(); | |
289 | ||
290 | VM_BUG_ON(nr != (end - start) >> PAGE_SHIFT); | |
291 | return nr; | |
292 | slow: | |
293 | local_irq_enable(); | |
294 | ||
295 | slow_irqon: | |
296 | /* Try to get the remaining pages with get_user_pages */ | |
297 | start += nr << PAGE_SHIFT; | |
298 | pages += nr; | |
299 | ||
a7b78075 AA |
300 | ret = get_user_pages_unlocked(current, mm, start, |
301 | (end - start) >> PAGE_SHIFT, | |
302 | write, 0, pages); | |
b1c10bea HD |
303 | |
304 | /* Have to be a bit careful with return values */ | |
305 | if (nr > 0) { | |
306 | if (ret < 0) | |
307 | ret = nr; | |
308 | else | |
309 | ret += nr; | |
310 | } | |
311 | return ret; | |
312 | } |