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>
12 #include <linux/memremap.h>
14 #include <asm/pgtable.h>
16 static inline pte_t gup_get_pte(pte_t *ptep)
18 #ifndef CONFIG_X86_PAE
19 return READ_ONCE(*ptep);
22 * With get_user_pages_fast, we walk down the pagetables without taking
23 * any locks. For this we would like to load the pointers atomically,
24 * but that is not possible (without expensive cmpxchg8b) on PAE. What
25 * we do have is the guarantee that a pte will only either go from not
26 * present to present, or present to not present or both -- it will not
27 * switch to a completely different present page without a TLB flush in
28 * between; something that we are blocking by holding interrupts off.
30 * Setting ptes from not present to present goes:
35 * And present to not present goes:
40 * We must ensure here that the load of pte_low sees l iff pte_high
41 * sees h. We load pte_high *after* loading pte_low, which ensures we
42 * don't see an older value of pte_high. *Then* we recheck pte_low,
43 * which ensures that we haven't picked up a changed pte high. We might
44 * have got rubbish values from pte_low and pte_high, but we are
45 * guaranteed that pte_low will not have the present bit set *unless*
46 * it is 'l'. And get_user_pages_fast only operates on present ptes, so
49 * gup_get_pte should not be used or copied outside gup.c without being
50 * very careful -- it does not atomically load the pte or anything that
51 * is likely to be useful for you.
56 pte.pte_low = ptep->pte_low;
58 pte.pte_high = ptep->pte_high;
60 if (unlikely(pte.pte_low != ptep->pte_low))
67 static void undo_dev_pagemap(int *nr, int nr_start, struct page **pages)
69 while ((*nr) - nr_start) {
70 struct page *page = pages[--(*nr)];
72 ClearPageReferenced(page);
78 * The performance critical leaf functions are made noinline otherwise gcc
79 * inlines everything into a single function which results in too much
82 static noinline int gup_pte_range(pmd_t pmd, unsigned long addr,
83 unsigned long end, int write, struct page **pages, int *nr)
85 struct dev_pagemap *pgmap = NULL;
90 mask = _PAGE_PRESENT|_PAGE_USER;
94 ptep = pte_offset_map(&pmd, addr);
96 pte_t pte = gup_get_pte(ptep);
99 /* Similar to the PMD case, NUMA hinting must take slow path */
100 if (pte_protnone(pte)) {
105 if (pte_devmap(pte)) {
106 pgmap = get_dev_pagemap(pte_pfn(pte), pgmap);
107 if (unlikely(!pgmap)) {
108 undo_dev_pagemap(nr, nr_start, pages);
112 } else if ((pte_flags(pte) & (mask | _PAGE_SPECIAL)) != mask) {
116 VM_BUG_ON(!pfn_valid(pte_pfn(pte)));
117 page = pte_page(pte);
119 put_dev_pagemap(pgmap);
120 SetPageReferenced(page);
124 } while (ptep++, addr += PAGE_SIZE, addr != end);
130 static inline void get_head_page_multiple(struct page *page, int nr)
132 VM_BUG_ON_PAGE(page != compound_head(page), page);
133 VM_BUG_ON_PAGE(page_count(page) == 0, page);
134 atomic_add(nr, &page->_count);
135 SetPageReferenced(page);
138 static int __gup_device_huge_pmd(pmd_t pmd, unsigned long addr,
139 unsigned long end, struct page **pages, int *nr)
142 unsigned long pfn = pmd_pfn(pmd);
143 struct dev_pagemap *pgmap = NULL;
145 pfn += (addr & ~PMD_MASK) >> PAGE_SHIFT;
147 struct page *page = pfn_to_page(pfn);
149 pgmap = get_dev_pagemap(pfn, pgmap);
150 if (unlikely(!pgmap)) {
151 undo_dev_pagemap(nr, nr_start, pages);
154 SetPageReferenced(page);
157 put_dev_pagemap(pgmap);
160 } while (addr += PAGE_SIZE, addr != end);
164 static noinline int gup_huge_pmd(pmd_t pmd, unsigned long addr,
165 unsigned long end, int write, struct page **pages, int *nr)
168 struct page *head, *page;
171 mask = _PAGE_PRESENT|_PAGE_USER;
174 if ((pmd_flags(pmd) & mask) != mask)
177 VM_BUG_ON(!pfn_valid(pmd_pfn(pmd)));
179 return __gup_device_huge_pmd(pmd, addr, end, pages, nr);
181 /* hugepages are never "special" */
182 VM_BUG_ON(pmd_flags(pmd) & _PAGE_SPECIAL);
185 head = pmd_page(pmd);
186 page = head + ((addr & ~PMD_MASK) >> PAGE_SHIFT);
188 VM_BUG_ON_PAGE(compound_head(page) != head, page);
193 } while (addr += PAGE_SIZE, addr != end);
194 get_head_page_multiple(head, refs);
199 static int gup_pmd_range(pud_t pud, unsigned long addr, unsigned long end,
200 int write, struct page **pages, int *nr)
205 pmdp = pmd_offset(&pud, addr);
209 next = pmd_addr_end(addr, end);
212 if (unlikely(pmd_large(pmd) || !pmd_present(pmd))) {
214 * NUMA hinting faults need to be handled in the GUP
215 * slowpath for accounting purposes and so that they
216 * can be serialised against THP migration.
218 if (pmd_protnone(pmd))
220 if (!gup_huge_pmd(pmd, addr, next, write, pages, nr))
223 if (!gup_pte_range(pmd, addr, next, write, pages, nr))
226 } while (pmdp++, addr = next, addr != end);
231 static noinline int gup_huge_pud(pud_t pud, unsigned long addr,
232 unsigned long end, int write, struct page **pages, int *nr)
235 struct page *head, *page;
238 mask = _PAGE_PRESENT|_PAGE_USER;
241 if ((pud_flags(pud) & mask) != mask)
243 /* hugepages are never "special" */
244 VM_BUG_ON(pud_flags(pud) & _PAGE_SPECIAL);
245 VM_BUG_ON(!pfn_valid(pud_pfn(pud)));
248 head = pud_page(pud);
249 page = head + ((addr & ~PUD_MASK) >> PAGE_SHIFT);
251 VM_BUG_ON_PAGE(compound_head(page) != head, page);
256 } while (addr += PAGE_SIZE, addr != end);
257 get_head_page_multiple(head, refs);
262 static int gup_pud_range(pgd_t pgd, unsigned long addr, unsigned long end,
263 int write, struct page **pages, int *nr)
268 pudp = pud_offset(&pgd, addr);
272 next = pud_addr_end(addr, end);
275 if (unlikely(pud_large(pud))) {
276 if (!gup_huge_pud(pud, addr, next, write, pages, nr))
279 if (!gup_pmd_range(pud, addr, next, write, pages, nr))
282 } while (pudp++, addr = next, addr != end);
288 * Like get_user_pages_fast() except its IRQ-safe in that it won't fall
289 * back to the regular GUP.
291 int __get_user_pages_fast(unsigned long start, int nr_pages, int write,
294 struct mm_struct *mm = current->mm;
295 unsigned long addr, len, end;
303 len = (unsigned long) nr_pages << PAGE_SHIFT;
305 if (unlikely(!access_ok(write ? VERIFY_WRITE : VERIFY_READ,
306 (void __user *)start, len)))
310 * XXX: batch / limit 'nr', to avoid large irq off latency
311 * needs some instrumenting to determine the common sizes used by
312 * important workloads (eg. DB2), and whether limiting the batch size
313 * will decrease performance.
315 * It seems like we're in the clear for the moment. Direct-IO is
316 * the main guy that batches up lots of get_user_pages, and even
317 * they are limited to 64-at-a-time which is not so many.
320 * This doesn't prevent pagetable teardown, but does prevent
321 * the pagetables and pages from being freed on x86.
323 * So long as we atomically load page table pointers versus teardown
324 * (which we do on x86, with the above PAE exception), we can follow the
325 * address down to the the page and take a ref on it.
327 local_irq_save(flags);
328 pgdp = pgd_offset(mm, addr);
332 next = pgd_addr_end(addr, end);
335 if (!gup_pud_range(pgd, addr, next, write, pages, &nr))
337 } while (pgdp++, addr = next, addr != end);
338 local_irq_restore(flags);
344 * get_user_pages_fast() - pin user pages in memory
345 * @start: starting user address
346 * @nr_pages: number of pages from start to pin
347 * @write: whether pages will be written to
348 * @pages: array that receives pointers to the pages pinned.
349 * Should be at least nr_pages long.
351 * Attempt to pin user pages in memory without taking mm->mmap_sem.
352 * If not successful, it will fall back to taking the lock and
353 * calling get_user_pages().
355 * Returns number of pages pinned. This may be fewer than the number
356 * requested. If nr_pages is 0 or negative, returns 0. If no pages
357 * were pinned, returns -errno.
359 int get_user_pages_fast(unsigned long start, int nr_pages, int write,
362 struct mm_struct *mm = current->mm;
363 unsigned long addr, len, end;
370 len = (unsigned long) nr_pages << PAGE_SHIFT;
377 if (end >> __VIRTUAL_MASK_SHIFT)
382 * XXX: batch / limit 'nr', to avoid large irq off latency
383 * needs some instrumenting to determine the common sizes used by
384 * important workloads (eg. DB2), and whether limiting the batch size
385 * will decrease performance.
387 * It seems like we're in the clear for the moment. Direct-IO is
388 * the main guy that batches up lots of get_user_pages, and even
389 * they are limited to 64-at-a-time which is not so many.
392 * This doesn't prevent pagetable teardown, but does prevent
393 * the pagetables and pages from being freed on x86.
395 * So long as we atomically load page table pointers versus teardown
396 * (which we do on x86, with the above PAE exception), we can follow the
397 * address down to the the page and take a ref on it.
400 pgdp = pgd_offset(mm, addr);
404 next = pgd_addr_end(addr, end);
407 if (!gup_pud_range(pgd, addr, next, write, pages, &nr))
409 } while (pgdp++, addr = next, addr != end);
412 VM_BUG_ON(nr != (end - start) >> PAGE_SHIFT);
421 /* Try to get the remaining pages with get_user_pages */
422 start += nr << PAGE_SHIFT;
425 ret = get_user_pages_unlocked(current, mm, start,
426 (end - start) >> PAGE_SHIFT,
429 /* Have to be a bit careful with return values */