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
);
37 pte_t pte
= ACCESS_ONCE(*ptep
);
40 * Similar to the PMD case, NUMA hinting must take slow path
45 if ((pte_val(pte
) & mask
) != result
)
47 VM_BUG_ON(!pfn_valid(pte_pfn(pte
)));
49 if (!page_cache_get_speculative(page
))
51 if (unlikely(pte_val(pte
) != pte_val(*ptep
))) {
58 } while (ptep
++, addr
+= PAGE_SIZE
, addr
!= end
);
63 static int gup_pmd_range(pud_t pud
, unsigned long addr
, unsigned long end
,
64 int write
, struct page
**pages
, int *nr
)
69 pmdp
= pmd_offset(&pud
, addr
);
71 pmd_t pmd
= ACCESS_ONCE(*pmdp
);
73 next
= pmd_addr_end(addr
, end
);
75 * If we find a splitting transparent hugepage we
76 * return zero. That will result in taking the slow
77 * path which will call wait_split_huge_page()
78 * if the pmd is still in splitting state
80 if (pmd_none(pmd
) || pmd_trans_splitting(pmd
))
82 if (pmd_huge(pmd
) || pmd_large(pmd
)) {
84 * NUMA hinting faults need to be handled in the GUP
85 * slowpath for accounting purposes and so that they
86 * can be serialised against THP migration.
91 if (!gup_hugepte((pte_t
*)pmdp
, PMD_SIZE
, addr
, next
,
94 } else if (is_hugepd(pmdp
)) {
95 if (!gup_hugepd((hugepd_t
*)pmdp
, PMD_SHIFT
,
96 addr
, next
, write
, pages
, nr
))
98 } else if (!gup_pte_range(pmd
, addr
, next
, write
, pages
, nr
))
100 } while (pmdp
++, addr
= next
, addr
!= end
);
105 static int gup_pud_range(pgd_t pgd
, unsigned long addr
, unsigned long end
,
106 int write
, struct page
**pages
, int *nr
)
111 pudp
= pud_offset(&pgd
, addr
);
113 pud_t pud
= ACCESS_ONCE(*pudp
);
115 next
= pud_addr_end(addr
, end
);
119 if (!gup_hugepte((pte_t
*)pudp
, PUD_SIZE
, addr
, next
,
122 } else if (is_hugepd(pudp
)) {
123 if (!gup_hugepd((hugepd_t
*)pudp
, PUD_SHIFT
,
124 addr
, next
, write
, pages
, nr
))
126 } else if (!gup_pmd_range(pud
, addr
, next
, write
, pages
, nr
))
128 } while (pudp
++, addr
= next
, addr
!= end
);
133 int __get_user_pages_fast(unsigned long start
, int nr_pages
, int write
,
136 struct mm_struct
*mm
= current
->mm
;
137 unsigned long addr
, len
, end
;
143 pr_devel("%s(%lx,%x,%s)\n", __func__
, start
, nr_pages
, write
? "write" : "read");
147 len
= (unsigned long) nr_pages
<< PAGE_SHIFT
;
150 if (unlikely(!access_ok(write
? VERIFY_WRITE
: VERIFY_READ
,
154 pr_devel(" aligned: %lx .. %lx\n", start
, end
);
157 * XXX: batch / limit 'nr', to avoid large irq off latency
158 * needs some instrumenting to determine the common sizes used by
159 * important workloads (eg. DB2), and whether limiting the batch size
160 * will decrease performance.
162 * It seems like we're in the clear for the moment. Direct-IO is
163 * the main guy that batches up lots of get_user_pages, and even
164 * they are limited to 64-at-a-time which is not so many.
167 * This doesn't prevent pagetable teardown, but does prevent
168 * the pagetables from being freed on powerpc.
170 * So long as we atomically load page table pointers versus teardown,
171 * we can follow the address down to the the page and take a ref on it.
173 local_irq_save(flags
);
175 pgdp
= pgd_offset(mm
, addr
);
177 pgd_t pgd
= ACCESS_ONCE(*pgdp
);
179 pr_devel(" %016lx: normal pgd %p\n", addr
,
180 (void *)pgd_val(pgd
));
181 next
= pgd_addr_end(addr
, end
);
185 if (!gup_hugepte((pte_t
*)pgdp
, PGDIR_SIZE
, addr
, next
,
188 } else if (is_hugepd(pgdp
)) {
189 if (!gup_hugepd((hugepd_t
*)pgdp
, PGDIR_SHIFT
,
190 addr
, next
, write
, pages
, &nr
))
192 } else if (!gup_pud_range(pgd
, addr
, next
, write
, pages
, &nr
))
194 } while (pgdp
++, addr
= next
, addr
!= end
);
196 local_irq_restore(flags
);
201 int get_user_pages_fast(unsigned long start
, int nr_pages
, int write
,
204 struct mm_struct
*mm
= current
->mm
;
208 nr
= __get_user_pages_fast(start
, nr_pages
, write
, pages
);
212 pr_devel(" slow path ! nr = %d\n", nr
);
214 /* Try to get the remaining pages with get_user_pages */
215 start
+= nr
<< PAGE_SHIFT
;
218 down_read(&mm
->mmap_sem
);
219 ret
= get_user_pages(current
, mm
, start
,
220 nr_pages
- nr
, write
, 0, pages
, NULL
);
221 up_read(&mm
->mmap_sem
);
223 /* Have to be a bit careful with return values */
235 #endif /* __HAVE_ARCH_PTE_SPECIAL */