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/cpu-features.h>
16 #include <asm/pgtable.h>
18 static inline pte_t
gup_get_pte(pte_t
*ptep
)
20 #if defined(CONFIG_PHYS_ADDR_T_64BIT) && defined(CONFIG_CPU_MIPS32)
24 pte
.pte_low
= ptep
->pte_low
;
26 pte
.pte_high
= ptep
->pte_high
;
28 if (unlikely(pte
.pte_low
!= ptep
->pte_low
))
33 return READ_ONCE(*ptep
);
37 static int gup_pte_range(pmd_t pmd
, unsigned long addr
, unsigned long end
,
38 int write
, struct page
**pages
, int *nr
)
40 pte_t
*ptep
= pte_offset_map(&pmd
, addr
);
42 pte_t pte
= gup_get_pte(ptep
);
45 if (!pte_present(pte
) ||
46 pte_special(pte
) || (write
&& !pte_write(pte
))) {
50 VM_BUG_ON(!pfn_valid(pte_pfn(pte
)));
53 SetPageReferenced(page
);
57 } while (ptep
++, addr
+= PAGE_SIZE
, addr
!= end
);
63 static inline void get_head_page_multiple(struct page
*page
, int nr
)
65 VM_BUG_ON(page
!= compound_head(page
));
66 VM_BUG_ON(page_count(page
) == 0);
67 page_ref_add(page
, nr
);
68 SetPageReferenced(page
);
71 static int gup_huge_pmd(pmd_t pmd
, unsigned long addr
, unsigned long end
,
72 int write
, struct page
**pages
, int *nr
)
74 pte_t pte
= *(pte_t
*)&pmd
;
75 struct page
*head
, *page
;
78 if (write
&& !pte_write(pte
))
80 /* hugepages are never "special" */
81 VM_BUG_ON(pte_special(pte
));
82 VM_BUG_ON(!pfn_valid(pte_pfn(pte
)));
86 page
= head
+ ((addr
& ~PMD_MASK
) >> PAGE_SHIFT
);
88 VM_BUG_ON(compound_head(page
) != head
);
93 } while (addr
+= PAGE_SIZE
, addr
!= end
);
95 get_head_page_multiple(head
, refs
);
99 static int gup_pmd_range(pud_t pud
, unsigned long addr
, unsigned long end
,
100 int write
, struct page
**pages
, int *nr
)
105 pmdp
= pmd_offset(&pud
, addr
);
109 next
= pmd_addr_end(addr
, end
);
112 if (unlikely(pmd_huge(pmd
))) {
113 if (!gup_huge_pmd(pmd
, addr
, next
, write
, pages
,nr
))
116 if (!gup_pte_range(pmd
, addr
, next
, write
, pages
,nr
))
119 } while (pmdp
++, addr
= next
, addr
!= end
);
124 static int gup_huge_pud(pud_t pud
, unsigned long addr
, unsigned long end
,
125 int write
, struct page
**pages
, int *nr
)
127 pte_t pte
= *(pte_t
*)&pud
;
128 struct page
*head
, *page
;
131 if (write
&& !pte_write(pte
))
133 /* hugepages are never "special" */
134 VM_BUG_ON(pte_special(pte
));
135 VM_BUG_ON(!pfn_valid(pte_pfn(pte
)));
138 head
= pte_page(pte
);
139 page
= head
+ ((addr
& ~PUD_MASK
) >> PAGE_SHIFT
);
141 VM_BUG_ON(compound_head(page
) != head
);
146 } while (addr
+= PAGE_SIZE
, addr
!= end
);
148 get_head_page_multiple(head
, refs
);
152 static int gup_pud_range(pgd_t pgd
, unsigned long addr
, unsigned long end
,
153 int write
, struct page
**pages
, int *nr
)
158 pudp
= pud_offset(&pgd
, addr
);
162 next
= pud_addr_end(addr
, end
);
165 if (unlikely(pud_huge(pud
))) {
166 if (!gup_huge_pud(pud
, addr
, next
, write
, pages
,nr
))
169 if (!gup_pmd_range(pud
, addr
, next
, write
, pages
,nr
))
172 } while (pudp
++, addr
= next
, addr
!= end
);
178 * Like get_user_pages_fast() except its IRQ-safe in that it won't fall
179 * back to the regular GUP.
181 int __get_user_pages_fast(unsigned long start
, int nr_pages
, int write
,
184 struct mm_struct
*mm
= current
->mm
;
185 unsigned long addr
, len
, end
;
193 len
= (unsigned long) nr_pages
<< PAGE_SHIFT
;
195 if (unlikely(!access_ok(write
? VERIFY_WRITE
: VERIFY_READ
,
196 (void __user
*)start
, len
)))
200 * XXX: batch / limit 'nr', to avoid large irq off latency
201 * needs some instrumenting to determine the common sizes used by
202 * important workloads (eg. DB2), and whether limiting the batch
203 * size will decrease performance.
205 * It seems like we're in the clear for the moment. Direct-IO is
206 * the main guy that batches up lots of get_user_pages, and even
207 * they are limited to 64-at-a-time which is not so many.
210 * This doesn't prevent pagetable teardown, but does prevent
211 * the pagetables and pages from being freed.
213 * So long as we atomically load page table pointers versus teardown,
214 * we can follow the address down to the page and take a ref on it.
216 local_irq_save(flags
);
217 pgdp
= pgd_offset(mm
, addr
);
221 next
= pgd_addr_end(addr
, end
);
224 if (!gup_pud_range(pgd
, addr
, next
, write
, pages
, &nr
))
226 } while (pgdp
++, addr
= next
, addr
!= end
);
227 local_irq_restore(flags
);
233 * get_user_pages_fast() - pin user pages in memory
234 * @start: starting user address
235 * @nr_pages: number of pages from start to pin
236 * @write: whether pages will be written to
237 * @pages: array that receives pointers to the pages pinned.
238 * Should be at least nr_pages long.
240 * Attempt to pin user pages in memory without taking mm->mmap_sem.
241 * If not successful, it will fall back to taking the lock and
242 * calling get_user_pages().
244 * Returns number of pages pinned. This may be fewer than the number
245 * requested. If nr_pages is 0 or negative, returns 0. If no pages
246 * were pinned, returns -errno.
248 int get_user_pages_fast(unsigned long start
, int nr_pages
, int write
,
251 struct mm_struct
*mm
= current
->mm
;
252 unsigned long addr
, len
, end
;
259 len
= (unsigned long) nr_pages
<< PAGE_SHIFT
;
262 if (end
< start
|| cpu_has_dc_aliases
)
265 /* XXX: batch / limit 'nr' */
267 pgdp
= pgd_offset(mm
, addr
);
271 next
= pgd_addr_end(addr
, end
);
274 if (!gup_pud_range(pgd
, addr
, next
, write
, pages
, &nr
))
276 } while (pgdp
++, addr
= next
, addr
!= end
);
279 VM_BUG_ON(nr
!= (end
- start
) >> PAGE_SHIFT
);
285 /* Try to get the remaining pages with get_user_pages */
286 start
+= nr
<< PAGE_SHIFT
;
289 ret
= get_user_pages_unlocked(start
, (end
- start
) >> PAGE_SHIFT
,
290 pages
, write
? FOLL_WRITE
: 0);
292 /* Have to be a bit careful with return values */