2 * Lockless get_user_pages_fast for MIPS
4 * Copyright (C) 2008 Nick Piggin
5 * Copyright (C) 2008 Novell Inc.
6 * Copyright (C) 2011 Ralf Baechle
8 #include <linux/sched.h>
10 #include <linux/vmstat.h>
11 #include <linux/highmem.h>
12 #include <linux/swap.h>
13 #include <linux/hugetlb.h>
15 #include <asm/pgtable.h>
17 static inline pte_t
gup_get_pte(pte_t
*ptep
)
19 #if defined(CONFIG_64BIT_PHYS_ADDR) && defined(CONFIG_CPU_MIPS32)
23 pte
.pte_low
= ptep
->pte_low
;
25 pte
.pte_high
= ptep
->pte_high
;
27 if (unlikely(pte
.pte_low
!= ptep
->pte_low
))
32 return ACCESS_ONCE(*ptep
);
36 static int gup_pte_range(pmd_t pmd
, unsigned long addr
, unsigned long end
,
37 int write
, struct page
**pages
, int *nr
)
39 pte_t
*ptep
= pte_offset_map(&pmd
, addr
);
41 pte_t pte
= gup_get_pte(ptep
);
44 if (!pte_present(pte
) ||
45 pte_special(pte
) || (write
&& !pte_write(pte
))) {
49 VM_BUG_ON(!pfn_valid(pte_pfn(pte
)));
52 SetPageReferenced(page
);
56 } while (ptep
++, addr
+= PAGE_SIZE
, addr
!= end
);
62 static inline void get_head_page_multiple(struct page
*page
, int nr
)
64 VM_BUG_ON(page
!= compound_head(page
));
65 VM_BUG_ON(page_count(page
) == 0);
66 atomic_add(nr
, &page
->_count
);
67 SetPageReferenced(page
);
70 static int gup_huge_pmd(pmd_t pmd
, unsigned long addr
, unsigned long end
,
71 int write
, struct page
**pages
, int *nr
)
73 pte_t pte
= *(pte_t
*)&pmd
;
74 struct page
*head
, *page
;
77 if (write
&& !pte_write(pte
))
79 /* hugepages are never "special" */
80 VM_BUG_ON(pte_special(pte
));
81 VM_BUG_ON(!pfn_valid(pte_pfn(pte
)));
85 page
= head
+ ((addr
& ~PMD_MASK
) >> PAGE_SHIFT
);
87 VM_BUG_ON(compound_head(page
) != head
);
90 get_huge_page_tail(page
);
94 } while (addr
+= PAGE_SIZE
, addr
!= end
);
96 get_head_page_multiple(head
, refs
);
100 static int gup_pmd_range(pud_t pud
, unsigned long addr
, unsigned long end
,
101 int write
, struct page
**pages
, int *nr
)
106 pmdp
= pmd_offset(&pud
, addr
);
110 next
= pmd_addr_end(addr
, end
);
112 * The pmd_trans_splitting() check below explains why
113 * pmdp_splitting_flush has to flush the tlb, to stop
114 * this gup-fast code from running while we set the
115 * splitting bit in the pmd. Returning zero will take
116 * the slow path that will call wait_split_huge_page()
117 * if the pmd is still in splitting state. gup-fast
118 * can't because it has irq disabled and
119 * wait_split_huge_page() would never return as the
120 * tlb flush IPI wouldn't run.
122 if (pmd_none(pmd
) || pmd_trans_splitting(pmd
))
124 if (unlikely(pmd_huge(pmd
))) {
125 if (!gup_huge_pmd(pmd
, addr
, next
, write
, pages
,nr
))
128 if (!gup_pte_range(pmd
, addr
, next
, write
, pages
,nr
))
131 } while (pmdp
++, addr
= next
, addr
!= end
);
136 static int gup_huge_pud(pud_t pud
, unsigned long addr
, unsigned long end
,
137 int write
, struct page
**pages
, int *nr
)
139 pte_t pte
= *(pte_t
*)&pud
;
140 struct page
*head
, *page
;
143 if (write
&& !pte_write(pte
))
145 /* hugepages are never "special" */
146 VM_BUG_ON(pte_special(pte
));
147 VM_BUG_ON(!pfn_valid(pte_pfn(pte
)));
150 head
= pte_page(pte
);
151 page
= head
+ ((addr
& ~PUD_MASK
) >> PAGE_SHIFT
);
153 VM_BUG_ON(compound_head(page
) != head
);
156 get_huge_page_tail(page
);
160 } while (addr
+= PAGE_SIZE
, addr
!= end
);
162 get_head_page_multiple(head
, refs
);
166 static int gup_pud_range(pgd_t pgd
, unsigned long addr
, unsigned long end
,
167 int write
, struct page
**pages
, int *nr
)
172 pudp
= pud_offset(&pgd
, addr
);
176 next
= pud_addr_end(addr
, end
);
179 if (unlikely(pud_huge(pud
))) {
180 if (!gup_huge_pud(pud
, addr
, next
, write
, pages
,nr
))
183 if (!gup_pmd_range(pud
, addr
, next
, write
, pages
,nr
))
186 } while (pudp
++, addr
= next
, addr
!= end
);
192 * Like get_user_pages_fast() except its IRQ-safe in that it won't fall
193 * back to the regular GUP.
195 int __get_user_pages_fast(unsigned long start
, int nr_pages
, int write
,
198 struct mm_struct
*mm
= current
->mm
;
199 unsigned long addr
, len
, end
;
207 len
= (unsigned long) nr_pages
<< PAGE_SHIFT
;
209 if (unlikely(!access_ok(write
? VERIFY_WRITE
: VERIFY_READ
,
210 (void __user
*)start
, len
)))
214 * XXX: batch / limit 'nr', to avoid large irq off latency
215 * needs some instrumenting to determine the common sizes used by
216 * important workloads (eg. DB2), and whether limiting the batch
217 * size will decrease performance.
219 * It seems like we're in the clear for the moment. Direct-IO is
220 * the main guy that batches up lots of get_user_pages, and even
221 * they are limited to 64-at-a-time which is not so many.
224 * This doesn't prevent pagetable teardown, but does prevent
225 * the pagetables and pages from being freed.
227 * So long as we atomically load page table pointers versus teardown,
228 * we can follow the address down to the page and take a ref on it.
230 local_irq_save(flags
);
231 pgdp
= pgd_offset(mm
, addr
);
235 next
= pgd_addr_end(addr
, end
);
238 if (!gup_pud_range(pgd
, addr
, next
, write
, pages
, &nr
))
240 } while (pgdp
++, addr
= next
, addr
!= end
);
241 local_irq_restore(flags
);
247 * get_user_pages_fast() - pin user pages in memory
248 * @start: starting user address
249 * @nr_pages: number of pages from start to pin
250 * @write: whether pages will be written to
251 * @pages: array that receives pointers to the pages pinned.
252 * Should be at least nr_pages long.
254 * Attempt to pin user pages in memory without taking mm->mmap_sem.
255 * If not successful, it will fall back to taking the lock and
256 * calling get_user_pages().
258 * Returns number of pages pinned. This may be fewer than the number
259 * requested. If nr_pages is 0 or negative, returns 0. If no pages
260 * were pinned, returns -errno.
262 int get_user_pages_fast(unsigned long start
, int nr_pages
, int write
,
265 struct mm_struct
*mm
= current
->mm
;
266 unsigned long addr
, len
, end
;
273 len
= (unsigned long) nr_pages
<< PAGE_SHIFT
;
279 /* XXX: batch / limit 'nr' */
281 pgdp
= pgd_offset(mm
, addr
);
285 next
= pgd_addr_end(addr
, end
);
288 if (!gup_pud_range(pgd
, addr
, next
, write
, pages
, &nr
))
290 } while (pgdp
++, addr
= next
, addr
!= end
);
293 VM_BUG_ON(nr
!= (end
- start
) >> PAGE_SHIFT
);
299 /* Try to get the remaining pages with get_user_pages */
300 start
+= nr
<< PAGE_SHIFT
;
303 down_read(&mm
->mmap_sem
);
304 ret
= get_user_pages(current
, mm
, start
,
305 (end
- start
) >> PAGE_SHIFT
,
306 write
, 0, pages
, NULL
);
307 up_read(&mm
->mmap_sem
);
309 /* Have to be a bit careful with return values */