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>
18 * The performance critical leaf functions are made noinline otherwise gcc
19 * inlines everything into a single function which results in too much
22 static noinline
int gup_pte_range(pmd_t pmd
, unsigned long addr
,
23 unsigned long end
, int write
, struct page
**pages
, int *nr
)
25 unsigned long mask
, result
;
28 result
= _PAGE_PRESENT
|_PAGE_USER
;
31 mask
= result
| _PAGE_SPECIAL
;
33 ptep
= pte_offset_kernel(&pmd
, addr
);
38 if ((pte_val(pte
) & mask
) != result
)
40 VM_BUG_ON(!pfn_valid(pte_pfn(pte
)));
42 if (!page_cache_get_speculative(page
))
44 if (unlikely(pte
!= *ptep
)) {
51 } while (ptep
++, addr
+= PAGE_SIZE
, addr
!= end
);
56 #ifdef CONFIG_HUGETLB_PAGE
57 static noinline
int gup_huge_pte(pte_t
*ptep
, struct hstate
*hstate
,
58 unsigned long *addr
, unsigned long end
,
59 int write
, struct page
**pages
, int *nr
)
62 unsigned long pte_end
;
63 struct page
*head
, *page
;
67 pte_end
= (*addr
+ huge_page_size(hstate
)) & huge_page_mask(hstate
);
72 mask
= _PAGE_PRESENT
|_PAGE_USER
;
75 if ((pte_val(pte
) & mask
) != mask
)
77 /* hugepages are never "special" */
78 VM_BUG_ON(!pfn_valid(pte_pfn(pte
)));
82 page
= head
+ ((*addr
& ~huge_page_mask(hstate
)) >> PAGE_SHIFT
);
84 VM_BUG_ON(compound_head(page
) != head
);
89 } while (*addr
+= PAGE_SIZE
, *addr
!= end
);
91 if (!page_cache_add_speculative(head
, refs
)) {
95 if (unlikely(pte
!= *ptep
)) {
96 /* Could be optimized better */
105 #endif /* CONFIG_HUGETLB_PAGE */
107 static int gup_pmd_range(pud_t pud
, unsigned long addr
, unsigned long end
,
108 int write
, struct page
**pages
, int *nr
)
113 pmdp
= pmd_offset(&pud
, addr
);
117 next
= pmd_addr_end(addr
, end
);
120 if (!gup_pte_range(pmd
, addr
, next
, write
, pages
, nr
))
122 } while (pmdp
++, addr
= next
, addr
!= end
);
127 static int gup_pud_range(pgd_t pgd
, unsigned long addr
, unsigned long end
,
128 int write
, struct page
**pages
, int *nr
)
133 pudp
= pud_offset(&pgd
, addr
);
137 next
= pud_addr_end(addr
, end
);
140 if (!gup_pmd_range(pud
, addr
, next
, write
, pages
, nr
))
142 } while (pudp
++, addr
= next
, addr
!= end
);
147 int get_user_pages_fast(unsigned long start
, int nr_pages
, int write
,
150 struct mm_struct
*mm
= current
->mm
;
151 unsigned long addr
, len
, end
;
157 pr_debug("%s(%lx,%x,%s)\n", __func__
, start
, nr_pages
, write
? "write" : "read");
161 len
= (unsigned long) nr_pages
<< PAGE_SHIFT
;
164 if (unlikely(!access_ok(write
? VERIFY_WRITE
: VERIFY_READ
,
168 pr_debug(" aligned: %lx .. %lx\n", start
, end
);
170 #ifdef CONFIG_HUGETLB_PAGE
171 /* We bail out on slice boundary crossing when hugetlb is
172 * enabled in order to not have to deal with two different
175 if (addr
< SLICE_LOW_TOP
) {
176 if (end
> SLICE_LOW_TOP
)
179 if (unlikely(GET_LOW_SLICE_INDEX(addr
) !=
180 GET_LOW_SLICE_INDEX(end
- 1)))
183 if (unlikely(GET_HIGH_SLICE_INDEX(addr
) !=
184 GET_HIGH_SLICE_INDEX(end
- 1)))
187 #endif /* CONFIG_HUGETLB_PAGE */
190 * XXX: batch / limit 'nr', to avoid large irq off latency
191 * needs some instrumenting to determine the common sizes used by
192 * important workloads (eg. DB2), and whether limiting the batch size
193 * will decrease performance.
195 * It seems like we're in the clear for the moment. Direct-IO is
196 * the main guy that batches up lots of get_user_pages, and even
197 * they are limited to 64-at-a-time which is not so many.
200 * This doesn't prevent pagetable teardown, but does prevent
201 * the pagetables from being freed on powerpc.
203 * So long as we atomically load page table pointers versus teardown,
204 * we can follow the address down to the the page and take a ref on it.
208 psize
= get_slice_psize(mm
, addr
);
209 shift
= mmu_psize_defs
[psize
].shift
;
211 #ifdef CONFIG_HUGETLB_PAGE
212 if (unlikely(mmu_huge_psizes
[psize
])) {
214 unsigned long a
= addr
;
215 unsigned long sz
= ((1UL) << shift
);
216 struct hstate
*hstate
= size_to_hstate(sz
);
220 * XXX: could be optimized to avoid hstate
221 * lookup entirely (just use shift)
225 VM_BUG_ON(shift
!= mmu_psize_defs
[get_slice_psize(mm
, a
)].shift
);
226 ptep
= huge_pte_offset(mm
, a
);
227 pr_debug(" %016lx: huge ptep %p\n", a
, ptep
);
228 if (!ptep
|| !gup_huge_pte(ptep
, hstate
, &a
, end
, write
, pages
,
233 #endif /* CONFIG_HUGETLB_PAGE */
235 pgdp
= pgd_offset(mm
, addr
);
239 VM_BUG_ON(shift
!= mmu_psize_defs
[get_slice_psize(mm
, addr
)].shift
);
240 pr_debug(" %016lx: normal pgd %p\n", addr
, (void *)pgd
);
241 next
= pgd_addr_end(addr
, end
);
244 if (!gup_pud_range(pgd
, addr
, next
, write
, pages
, &nr
))
246 } while (pgdp
++, addr
= next
, addr
!= end
);
250 VM_BUG_ON(nr
!= (end
- start
) >> PAGE_SHIFT
);
259 pr_debug(" slow path ! nr = %d\n", nr
);
261 /* Try to get the remaining pages with get_user_pages */
262 start
+= nr
<< PAGE_SHIFT
;
265 down_read(&mm
->mmap_sem
);
266 ret
= get_user_pages(current
, mm
, start
,
267 (end
- start
) >> PAGE_SHIFT
, write
, 0, pages
, NULL
);
268 up_read(&mm
->mmap_sem
);
270 /* Have to be a bit careful with return values */