2 * Lockless get_user_pages_fast for x86
4 * Copyright (C) 2008 Nick Piggin
5 * Copyright (C) 2008 Novell Inc.
7 #include <linux/sched.h>
9 #include <linux/vmstat.h>
10 #include <linux/highmem.h>
11 #include <linux/swap.h>
13 #include <asm/pgtable.h>
15 static inline pte_t
gup_get_pte(pte_t
*ptep
)
17 #ifndef CONFIG_X86_PAE
18 return READ_ONCE(*ptep
);
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.
29 * Setting ptes from not present to present goes:
34 * And present to not present goes:
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
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.
55 pte
.pte_low
= ptep
->pte_low
;
57 pte
.pte_high
= ptep
->pte_high
;
59 if (unlikely(pte
.pte_low
!= ptep
->pte_low
))
67 * The performance critical leaf functions are made noinline otherwise gcc
68 * inlines everything into a single function which results in too much
71 static noinline
int gup_pte_range(pmd_t pmd
, unsigned long addr
,
72 unsigned long end
, int write
, struct page
**pages
, int *nr
)
77 mask
= _PAGE_PRESENT
|_PAGE_USER
;
81 ptep
= pte_offset_map(&pmd
, addr
);
83 pte_t pte
= gup_get_pte(ptep
);
86 /* Similar to the PMD case, NUMA hinting must take slow path */
87 if (pte_protnone(pte
)) {
92 if ((pte_flags(pte
) & (mask
| _PAGE_SPECIAL
)) != mask
) {
96 VM_BUG_ON(!pfn_valid(pte_pfn(pte
)));
99 SetPageReferenced(page
);
103 } while (ptep
++, addr
+= PAGE_SIZE
, addr
!= end
);
109 static inline void get_head_page_multiple(struct page
*page
, int nr
)
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
);
117 static noinline
int gup_huge_pmd(pmd_t pmd
, unsigned long addr
,
118 unsigned long end
, int write
, struct page
**pages
, int *nr
)
121 struct page
*head
, *page
;
124 mask
= _PAGE_PRESENT
|_PAGE_USER
;
127 if ((pmd_flags(pmd
) & mask
) != mask
)
129 /* hugepages are never "special" */
130 VM_BUG_ON(pmd_flags(pmd
) & _PAGE_SPECIAL
);
131 VM_BUG_ON(!pfn_valid(pmd_pfn(pmd
)));
134 head
= pmd_page(pmd
);
135 page
= head
+ ((addr
& ~PMD_MASK
) >> PAGE_SHIFT
);
137 VM_BUG_ON_PAGE(compound_head(page
) != head
, page
);
140 get_huge_page_tail(page
);
144 } while (addr
+= PAGE_SIZE
, addr
!= end
);
145 get_head_page_multiple(head
, refs
);
150 static int gup_pmd_range(pud_t pud
, unsigned long addr
, unsigned long end
,
151 int write
, struct page
**pages
, int *nr
)
156 pmdp
= pmd_offset(&pud
, addr
);
160 next
= pmd_addr_end(addr
, end
);
162 * The pmd_trans_splitting() check below explains why
163 * pmdp_splitting_flush has to flush the tlb, to stop
164 * this gup-fast code from running while we set the
165 * splitting bit in the pmd. Returning zero will take
166 * the slow path that will call wait_split_huge_page()
167 * if the pmd is still in splitting state. gup-fast
168 * can't because it has irq disabled and
169 * wait_split_huge_page() would never return as the
170 * tlb flush IPI wouldn't run.
172 if (pmd_none(pmd
) || pmd_trans_splitting(pmd
))
174 if (unlikely(pmd_large(pmd
) || !pmd_present(pmd
))) {
176 * NUMA hinting faults need to be handled in the GUP
177 * slowpath for accounting purposes and so that they
178 * can be serialised against THP migration.
180 if (pmd_protnone(pmd
))
182 if (!gup_huge_pmd(pmd
, addr
, next
, write
, pages
, nr
))
185 if (!gup_pte_range(pmd
, addr
, next
, write
, pages
, nr
))
188 } while (pmdp
++, addr
= next
, addr
!= end
);
193 static noinline
int gup_huge_pud(pud_t pud
, unsigned long addr
,
194 unsigned long end
, int write
, struct page
**pages
, int *nr
)
197 struct page
*head
, *page
;
200 mask
= _PAGE_PRESENT
|_PAGE_USER
;
203 if ((pud_flags(pud
) & mask
) != mask
)
205 /* hugepages are never "special" */
206 VM_BUG_ON(pud_flags(pud
) & _PAGE_SPECIAL
);
207 VM_BUG_ON(!pfn_valid(pud_pfn(pud
)));
210 head
= pud_page(pud
);
211 page
= head
+ ((addr
& ~PUD_MASK
) >> PAGE_SHIFT
);
213 VM_BUG_ON_PAGE(compound_head(page
) != head
, page
);
216 get_huge_page_tail(page
);
220 } while (addr
+= PAGE_SIZE
, addr
!= end
);
221 get_head_page_multiple(head
, refs
);
226 static int gup_pud_range(pgd_t pgd
, unsigned long addr
, unsigned long end
,
227 int write
, struct page
**pages
, int *nr
)
232 pudp
= pud_offset(&pgd
, addr
);
236 next
= pud_addr_end(addr
, end
);
239 if (unlikely(pud_large(pud
))) {
240 if (!gup_huge_pud(pud
, addr
, next
, write
, pages
, nr
))
243 if (!gup_pmd_range(pud
, addr
, next
, write
, pages
, nr
))
246 } while (pudp
++, addr
= next
, addr
!= end
);
252 * Like get_user_pages_fast() except its IRQ-safe in that it won't fall
253 * back to the regular GUP.
255 int __get_user_pages_fast(unsigned long start
, int nr_pages
, int write
,
258 struct mm_struct
*mm
= current
->mm
;
259 unsigned long addr
, len
, end
;
267 len
= (unsigned long) nr_pages
<< PAGE_SHIFT
;
269 if (unlikely(!access_ok(write
? VERIFY_WRITE
: VERIFY_READ
,
270 (void __user
*)start
, len
)))
274 * XXX: batch / limit 'nr', to avoid large irq off latency
275 * needs some instrumenting to determine the common sizes used by
276 * important workloads (eg. DB2), and whether limiting the batch size
277 * will decrease performance.
279 * It seems like we're in the clear for the moment. Direct-IO is
280 * the main guy that batches up lots of get_user_pages, and even
281 * they are limited to 64-at-a-time which is not so many.
284 * This doesn't prevent pagetable teardown, but does prevent
285 * the pagetables and pages from being freed on x86.
287 * So long as we atomically load page table pointers versus teardown
288 * (which we do on x86, with the above PAE exception), we can follow the
289 * address down to the the page and take a ref on it.
291 local_irq_save(flags
);
292 pgdp
= pgd_offset(mm
, addr
);
296 next
= pgd_addr_end(addr
, end
);
299 if (!gup_pud_range(pgd
, addr
, next
, write
, pages
, &nr
))
301 } while (pgdp
++, addr
= next
, addr
!= end
);
302 local_irq_restore(flags
);
308 * get_user_pages_fast() - pin user pages in memory
309 * @start: starting user address
310 * @nr_pages: number of pages from start to pin
311 * @write: whether pages will be written to
312 * @pages: array that receives pointers to the pages pinned.
313 * Should be at least nr_pages long.
315 * Attempt to pin user pages in memory without taking mm->mmap_sem.
316 * If not successful, it will fall back to taking the lock and
317 * calling get_user_pages().
319 * Returns number of pages pinned. This may be fewer than the number
320 * requested. If nr_pages is 0 or negative, returns 0. If no pages
321 * were pinned, returns -errno.
323 int get_user_pages_fast(unsigned long start
, int nr_pages
, int write
,
326 struct mm_struct
*mm
= current
->mm
;
327 unsigned long addr
, len
, end
;
334 len
= (unsigned long) nr_pages
<< PAGE_SHIFT
;
341 if (end
>> __VIRTUAL_MASK_SHIFT
)
346 * XXX: batch / limit 'nr', to avoid large irq off latency
347 * needs some instrumenting to determine the common sizes used by
348 * important workloads (eg. DB2), and whether limiting the batch size
349 * will decrease performance.
351 * It seems like we're in the clear for the moment. Direct-IO is
352 * the main guy that batches up lots of get_user_pages, and even
353 * they are limited to 64-at-a-time which is not so many.
356 * This doesn't prevent pagetable teardown, but does prevent
357 * the pagetables and pages from being freed on x86.
359 * So long as we atomically load page table pointers versus teardown
360 * (which we do on x86, with the above PAE exception), we can follow the
361 * address down to the the page and take a ref on it.
364 pgdp
= pgd_offset(mm
, addr
);
368 next
= pgd_addr_end(addr
, end
);
371 if (!gup_pud_range(pgd
, addr
, next
, write
, pages
, &nr
))
373 } while (pgdp
++, addr
= next
, addr
!= end
);
376 VM_BUG_ON(nr
!= (end
- start
) >> PAGE_SHIFT
);
385 /* Try to get the remaining pages with get_user_pages */
386 start
+= nr
<< PAGE_SHIFT
;
389 ret
= get_user_pages_unlocked(current
, mm
, start
,
390 (end
- start
) >> PAGE_SHIFT
,
393 /* Have to be a bit careful with return values */