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>
12 #include <asm/pgtable.h>
14 static inline pte_t gup_get_pte(pte_t *ptep)
16 #ifndef CONFIG_X86_PAE
20 * With get_user_pages_fast, we walk down the pagetables without taking
21 * any locks. For this we would like to load the pointers atoimcally,
22 * but that is not possible (without expensive cmpxchg8b) on PAE. What
23 * we do have is the guarantee that a pte will only either go from not
24 * present to present, or present to not present or both -- it will not
25 * switch to a completely different present page without a TLB flush in
26 * between; something that we are blocking by holding interrupts off.
28 * Setting ptes from not present to present goes:
33 * And present to not present goes:
38 * We must ensure here that the load of pte_low sees l iff pte_high
39 * sees h. We load pte_high *after* loading pte_low, which ensures we
40 * don't see an older value of pte_high. *Then* we recheck pte_low,
41 * which ensures that we haven't picked up a changed pte high. We might
42 * have got rubbish values from pte_low and pte_high, but we are
43 * guaranteed that pte_low will not have the present bit set *unless*
44 * it is 'l'. And get_user_pages_fast only operates on present ptes, so
47 * gup_get_pte should not be used or copied outside gup.c without being
48 * very careful -- it does not atomically load the pte or anything that
49 * is likely to be useful for you.
54 pte.pte_low = ptep->pte_low;
56 pte.pte_high = ptep->pte_high;
58 if (unlikely(pte.pte_low != ptep->pte_low))
66 * The performance critical leaf functions are made noinline otherwise gcc
67 * inlines everything into a single function which results in too much
70 static noinline int gup_pte_range(pmd_t pmd, unsigned long addr,
71 unsigned long end, int write, struct page **pages, int *nr)
76 mask = _PAGE_PRESENT|_PAGE_USER;
80 ptep = pte_offset_map(&pmd, addr);
82 pte_t pte = gup_get_pte(ptep);
85 if ((pte_flags(pte) & (mask | _PAGE_SPECIAL)) != mask) {
89 VM_BUG_ON(!pfn_valid(pte_pfn(pte)));
95 } while (ptep++, addr += PAGE_SIZE, addr != end);
101 static inline void get_head_page_multiple(struct page *page, int nr)
103 VM_BUG_ON(page != compound_head(page));
104 VM_BUG_ON(page_count(page) == 0);
105 atomic_add(nr, &page->_count);
108 static noinline int gup_huge_pmd(pmd_t pmd, unsigned long addr,
109 unsigned long end, int write, struct page **pages, int *nr)
112 pte_t pte = *(pte_t *)&pmd;
113 struct page *head, *page;
116 mask = _PAGE_PRESENT|_PAGE_USER;
119 if ((pte_flags(pte) & mask) != mask)
121 /* hugepages are never "special" */
122 VM_BUG_ON(pte_flags(pte) & _PAGE_SPECIAL);
123 VM_BUG_ON(!pfn_valid(pte_pfn(pte)));
126 head = pte_page(pte);
127 page = head + ((addr & ~PMD_MASK) >> PAGE_SHIFT);
129 VM_BUG_ON(compound_head(page) != head);
134 } while (addr += PAGE_SIZE, addr != end);
135 get_head_page_multiple(head, refs);
140 static int gup_pmd_range(pud_t pud, unsigned long addr, unsigned long end,
141 int write, struct page **pages, int *nr)
146 pmdp = pmd_offset(&pud, addr);
150 next = pmd_addr_end(addr, end);
153 if (unlikely(pmd_large(pmd))) {
154 if (!gup_huge_pmd(pmd, addr, next, write, pages, nr))
157 if (!gup_pte_range(pmd, addr, next, write, pages, nr))
160 } while (pmdp++, addr = next, addr != end);
165 static noinline int gup_huge_pud(pud_t pud, unsigned long addr,
166 unsigned long end, int write, struct page **pages, int *nr)
169 pte_t pte = *(pte_t *)&pud;
170 struct page *head, *page;
173 mask = _PAGE_PRESENT|_PAGE_USER;
176 if ((pte_flags(pte) & mask) != mask)
178 /* hugepages are never "special" */
179 VM_BUG_ON(pte_flags(pte) & _PAGE_SPECIAL);
180 VM_BUG_ON(!pfn_valid(pte_pfn(pte)));
183 head = pte_page(pte);
184 page = head + ((addr & ~PUD_MASK) >> PAGE_SHIFT);
186 VM_BUG_ON(compound_head(page) != head);
191 } while (addr += PAGE_SIZE, addr != end);
192 get_head_page_multiple(head, refs);
197 static int gup_pud_range(pgd_t pgd, unsigned long addr, unsigned long end,
198 int write, struct page **pages, int *nr)
203 pudp = pud_offset(&pgd, addr);
207 next = pud_addr_end(addr, end);
210 if (unlikely(pud_large(pud))) {
211 if (!gup_huge_pud(pud, addr, next, write, pages, nr))
214 if (!gup_pmd_range(pud, addr, next, write, pages, nr))
217 } while (pudp++, addr = next, addr != end);
222 int get_user_pages_fast(unsigned long start, int nr_pages, int write,
225 struct mm_struct *mm = current->mm;
226 unsigned long addr, len, end;
233 len = (unsigned long) nr_pages << PAGE_SHIFT;
235 if (unlikely(!access_ok(write ? VERIFY_WRITE : VERIFY_READ,
240 * XXX: batch / limit 'nr', to avoid large irq off latency
241 * needs some instrumenting to determine the common sizes used by
242 * important workloads (eg. DB2), and whether limiting the batch size
243 * will decrease performance.
245 * It seems like we're in the clear for the moment. Direct-IO is
246 * the main guy that batches up lots of get_user_pages, and even
247 * they are limited to 64-at-a-time which is not so many.
250 * This doesn't prevent pagetable teardown, but does prevent
251 * the pagetables and pages from being freed on x86.
253 * So long as we atomically load page table pointers versus teardown
254 * (which we do on x86, with the above PAE exception), we can follow the
255 * address down to the the page and take a ref on it.
258 pgdp = pgd_offset(mm, addr);
262 next = pgd_addr_end(addr, end);
265 if (!gup_pud_range(pgd, addr, next, write, pages, &nr))
267 } while (pgdp++, addr = next, addr != end);
270 VM_BUG_ON(nr != (end - start) >> PAGE_SHIFT);
279 /* Try to get the remaining pages with get_user_pages */
280 start += nr << PAGE_SHIFT;
283 down_read(&mm->mmap_sem);
284 ret = get_user_pages(current, mm, start,
285 (end - start) >> PAGE_SHIFT, write, 0, pages, NULL);
286 up_read(&mm->mmap_sem);
288 /* Have to be a bit careful with return values */