2 * Lockless get_user_pages_fast for powerpc
4 * Copyright (C) 2008 Nick Piggin
5 * Copyright (C) 2008 Novell Inc.
9 #include <linux/sched.h>
11 #include <linux/hugetlb.h>
12 #include <linux/vmstat.h>
13 #include <linux/pagemap.h>
14 #include <linux/rwsem.h>
15 #include <asm/pgtable.h>
17 #ifdef __HAVE_ARCH_PTE_SPECIAL
20 * The performance critical leaf functions are made noinline otherwise gcc
21 * inlines everything into a single function which results in too much
24 static noinline int gup_pte_range(pmd_t pmd, unsigned long addr,
25 unsigned long end, int write, struct page **pages, int *nr)
27 unsigned long mask, result;
30 result = _PAGE_PRESENT|_PAGE_USER;
33 mask = result | _PAGE_SPECIAL;
35 ptep = pte_offset_kernel(&pmd, addr);
40 if ((pte_val(pte) & mask) != result)
42 VM_BUG_ON(!pfn_valid(pte_pfn(pte)));
44 if (!page_cache_get_speculative(page))
46 if (unlikely(pte_val(pte) != pte_val(*ptep))) {
53 } while (ptep++, addr += PAGE_SIZE, addr != end);
58 #ifdef CONFIG_HUGETLB_PAGE
59 static noinline int gup_huge_pte(pte_t *ptep, struct hstate *hstate,
60 unsigned long *addr, unsigned long end,
61 int write, struct page **pages, int *nr)
64 unsigned long pte_end;
65 struct page *head, *page;
69 pte_end = (*addr + huge_page_size(hstate)) & huge_page_mask(hstate);
74 mask = _PAGE_PRESENT|_PAGE_USER;
77 if ((pte_val(pte) & mask) != mask)
79 /* hugepages are never "special" */
80 VM_BUG_ON(!pfn_valid(pte_pfn(pte)));
84 page = head + ((*addr & ~huge_page_mask(hstate)) >> PAGE_SHIFT);
86 VM_BUG_ON(compound_head(page) != head);
91 } while (*addr += PAGE_SIZE, *addr != end);
93 if (!page_cache_add_speculative(head, refs)) {
97 if (unlikely(pte_val(pte) != pte_val(*ptep))) {
98 /* Could be optimized better */
107 #endif /* CONFIG_HUGETLB_PAGE */
109 static int gup_pmd_range(pud_t pud, unsigned long addr, unsigned long end,
110 int write, struct page **pages, int *nr)
115 pmdp = pmd_offset(&pud, addr);
119 next = pmd_addr_end(addr, end);
122 if (!gup_pte_range(pmd, addr, next, write, pages, nr))
124 } while (pmdp++, addr = next, addr != end);
129 static int gup_pud_range(pgd_t pgd, unsigned long addr, unsigned long end,
130 int write, struct page **pages, int *nr)
135 pudp = pud_offset(&pgd, addr);
139 next = pud_addr_end(addr, end);
142 if (!gup_pmd_range(pud, addr, next, write, pages, nr))
144 } while (pudp++, addr = next, addr != end);
149 int get_user_pages_fast(unsigned long start, int nr_pages, int write,
152 struct mm_struct *mm = current->mm;
153 unsigned long addr, len, end;
162 pr_debug("%s(%lx,%x,%s)\n", __func__, start, nr_pages, write ? "write" : "read");
166 len = (unsigned long) nr_pages << PAGE_SHIFT;
169 if (unlikely(!access_ok(write ? VERIFY_WRITE : VERIFY_READ,
173 pr_debug(" aligned: %lx .. %lx\n", start, end);
175 #ifdef CONFIG_HUGETLB_PAGE
176 /* We bail out on slice boundary crossing when hugetlb is
177 * enabled in order to not have to deal with two different
180 if (addr < SLICE_LOW_TOP) {
181 if (end > SLICE_LOW_TOP)
184 if (unlikely(GET_LOW_SLICE_INDEX(addr) !=
185 GET_LOW_SLICE_INDEX(end - 1)))
188 if (unlikely(GET_HIGH_SLICE_INDEX(addr) !=
189 GET_HIGH_SLICE_INDEX(end - 1)))
192 #endif /* CONFIG_HUGETLB_PAGE */
195 * XXX: batch / limit 'nr', to avoid large irq off latency
196 * needs some instrumenting to determine the common sizes used by
197 * important workloads (eg. DB2), and whether limiting the batch size
198 * will decrease performance.
200 * It seems like we're in the clear for the moment. Direct-IO is
201 * the main guy that batches up lots of get_user_pages, and even
202 * they are limited to 64-at-a-time which is not so many.
205 * This doesn't prevent pagetable teardown, but does prevent
206 * the pagetables from being freed on powerpc.
208 * So long as we atomically load page table pointers versus teardown,
209 * we can follow the address down to the the page and take a ref on it.
214 /* Those bits are related to hugetlbfs implementation and only exist
217 psize = get_slice_psize(mm, addr);
218 shift = mmu_psize_defs[psize].shift;
219 #endif /* CONFIG_PPC64 */
221 #ifdef CONFIG_HUGETLB_PAGE
222 if (unlikely(mmu_huge_psizes[psize])) {
224 unsigned long a = addr;
225 unsigned long sz = ((1UL) << shift);
226 struct hstate *hstate = size_to_hstate(sz);
230 * XXX: could be optimized to avoid hstate
231 * lookup entirely (just use shift)
235 VM_BUG_ON(shift != mmu_psize_defs[get_slice_psize(mm, a)].shift);
236 ptep = huge_pte_offset(mm, a);
237 pr_debug(" %016lx: huge ptep %p\n", a, ptep);
238 if (!ptep || !gup_huge_pte(ptep, hstate, &a, end, write, pages,
243 #endif /* CONFIG_HUGETLB_PAGE */
245 pgdp = pgd_offset(mm, addr);
250 VM_BUG_ON(shift != mmu_psize_defs[get_slice_psize(mm, addr)].shift);
252 pr_debug(" %016lx: normal pgd %p\n", addr,
253 (void *)pgd_val(pgd));
254 next = pgd_addr_end(addr, end);
257 if (!gup_pud_range(pgd, addr, next, write, pages, &nr))
259 } while (pgdp++, addr = next, addr != end);
263 VM_BUG_ON(nr != (end - start) >> PAGE_SHIFT);
272 pr_debug(" slow path ! nr = %d\n", nr);
274 /* Try to get the remaining pages with get_user_pages */
275 start += nr << PAGE_SHIFT;
278 down_read(&mm->mmap_sem);
279 ret = get_user_pages(current, mm, start,
280 (end - start) >> PAGE_SHIFT, write, 0, pages, NULL);
281 up_read(&mm->mmap_sem);
283 /* Have to be a bit careful with return values */
295 #endif /* __HAVE_ARCH_PTE_SPECIAL */