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