1 // SPDX-License-Identifier: GPL-2.0
3 * Lockless get_user_pages_fast for MIPS
5 * Copyright (C) 2008 Nick Piggin
6 * Copyright (C) 2008 Novell Inc.
7 * Copyright (C) 2011 Ralf Baechle
9 #include <linux/sched.h>
11 #include <linux/vmstat.h>
12 #include <linux/highmem.h>
13 #include <linux/swap.h>
14 #include <linux/hugetlb.h>
16 #include <asm/cpu-features.h>
17 #include <asm/pgtable.h>
19 static inline pte_t
gup_get_pte(pte_t
*ptep
)
21 #if defined(CONFIG_PHYS_ADDR_T_64BIT) && defined(CONFIG_CPU_MIPS32)
25 pte
.pte_low
= ptep
->pte_low
;
27 pte
.pte_high
= ptep
->pte_high
;
29 if (unlikely(pte
.pte_low
!= ptep
->pte_low
))
34 return READ_ONCE(*ptep
);
38 static int gup_pte_range(pmd_t pmd
, unsigned long addr
, unsigned long end
,
39 int write
, struct page
**pages
, int *nr
)
41 pte_t
*ptep
= pte_offset_map(&pmd
, addr
);
43 pte_t pte
= gup_get_pte(ptep
);
46 if (!pte_present(pte
) ||
47 pte_special(pte
) || (write
&& !pte_write(pte
))) {
51 VM_BUG_ON(!pfn_valid(pte_pfn(pte
)));
54 SetPageReferenced(page
);
58 } while (ptep
++, addr
+= PAGE_SIZE
, addr
!= end
);
64 static inline void get_head_page_multiple(struct page
*page
, int nr
)
66 VM_BUG_ON(page
!= compound_head(page
));
67 VM_BUG_ON(page_count(page
) == 0);
68 page_ref_add(page
, nr
);
69 SetPageReferenced(page
);
72 static int gup_huge_pmd(pmd_t pmd
, unsigned long addr
, unsigned long end
,
73 int write
, struct page
**pages
, int *nr
)
75 pte_t pte
= *(pte_t
*)&pmd
;
76 struct page
*head
, *page
;
79 if (write
&& !pte_write(pte
))
81 /* hugepages are never "special" */
82 VM_BUG_ON(pte_special(pte
));
83 VM_BUG_ON(!pfn_valid(pte_pfn(pte
)));
87 page
= head
+ ((addr
& ~PMD_MASK
) >> PAGE_SHIFT
);
89 VM_BUG_ON(compound_head(page
) != head
);
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
);
113 if (unlikely(pmd_huge(pmd
))) {
114 if (!gup_huge_pmd(pmd
, addr
, next
, write
, pages
,nr
))
117 if (!gup_pte_range(pmd
, addr
, next
, write
, pages
,nr
))
120 } while (pmdp
++, addr
= next
, addr
!= end
);
125 static int gup_huge_pud(pud_t pud
, unsigned long addr
, unsigned long end
,
126 int write
, struct page
**pages
, int *nr
)
128 pte_t pte
= *(pte_t
*)&pud
;
129 struct page
*head
, *page
;
132 if (write
&& !pte_write(pte
))
134 /* hugepages are never "special" */
135 VM_BUG_ON(pte_special(pte
));
136 VM_BUG_ON(!pfn_valid(pte_pfn(pte
)));
139 head
= pte_page(pte
);
140 page
= head
+ ((addr
& ~PUD_MASK
) >> PAGE_SHIFT
);
142 VM_BUG_ON(compound_head(page
) != head
);
147 } while (addr
+= PAGE_SIZE
, addr
!= end
);
149 get_head_page_multiple(head
, refs
);
153 static int gup_pud_range(pgd_t pgd
, unsigned long addr
, unsigned long end
,
154 int write
, struct page
**pages
, int *nr
)
159 pudp
= pud_offset(&pgd
, addr
);
163 next
= pud_addr_end(addr
, end
);
166 if (unlikely(pud_huge(pud
))) {
167 if (!gup_huge_pud(pud
, addr
, next
, write
, pages
,nr
))
170 if (!gup_pmd_range(pud
, addr
, next
, write
, pages
,nr
))
173 } while (pudp
++, addr
= next
, addr
!= end
);
179 * Like get_user_pages_fast() except its IRQ-safe in that it won't fall
180 * back to the regular GUP.
181 * Note a difference with get_user_pages_fast: this always returns the
182 * number of pages pinned, 0 if no pages were pinned.
184 int __get_user_pages_fast(unsigned long start
, int nr_pages
, int write
,
187 struct mm_struct
*mm
= current
->mm
;
188 unsigned long addr
, len
, end
;
196 len
= (unsigned long) nr_pages
<< PAGE_SHIFT
;
198 if (unlikely(!access_ok(write
? VERIFY_WRITE
: VERIFY_READ
,
199 (void __user
*)start
, len
)))
203 * XXX: batch / limit 'nr', to avoid large irq off latency
204 * needs some instrumenting to determine the common sizes used by
205 * important workloads (eg. DB2), and whether limiting the batch
206 * size will decrease performance.
208 * It seems like we're in the clear for the moment. Direct-IO is
209 * the main guy that batches up lots of get_user_pages, and even
210 * they are limited to 64-at-a-time which is not so many.
213 * This doesn't prevent pagetable teardown, but does prevent
214 * the pagetables and pages from being freed.
216 * So long as we atomically load page table pointers versus teardown,
217 * we can follow the address down to the page and take a ref on it.
219 local_irq_save(flags
);
220 pgdp
= pgd_offset(mm
, addr
);
224 next
= pgd_addr_end(addr
, end
);
227 if (!gup_pud_range(pgd
, addr
, next
, write
, pages
, &nr
))
229 } while (pgdp
++, addr
= next
, addr
!= end
);
230 local_irq_restore(flags
);
236 * get_user_pages_fast() - pin user pages in memory
237 * @start: starting user address
238 * @nr_pages: number of pages from start to pin
239 * @write: whether pages will be written to
240 * @pages: array that receives pointers to the pages pinned.
241 * Should be at least nr_pages long.
243 * Attempt to pin user pages in memory without taking mm->mmap_sem.
244 * If not successful, it will fall back to taking the lock and
245 * calling get_user_pages().
247 * Returns number of pages pinned. This may be fewer than the number
248 * requested. If nr_pages is 0 or negative, returns 0. If no pages
249 * were pinned, returns -errno.
251 int get_user_pages_fast(unsigned long start
, int nr_pages
, int write
,
254 struct mm_struct
*mm
= current
->mm
;
255 unsigned long addr
, len
, end
;
262 len
= (unsigned long) nr_pages
<< PAGE_SHIFT
;
265 if (end
< start
|| cpu_has_dc_aliases
)
268 /* XXX: batch / limit 'nr' */
270 pgdp
= pgd_offset(mm
, addr
);
274 next
= pgd_addr_end(addr
, end
);
277 if (!gup_pud_range(pgd
, addr
, next
, write
, pages
, &nr
))
279 } while (pgdp
++, addr
= next
, addr
!= end
);
282 VM_BUG_ON(nr
!= (end
- start
) >> PAGE_SHIFT
);
288 /* Try to get the remaining pages with get_user_pages */
289 start
+= nr
<< PAGE_SHIFT
;
292 ret
= get_user_pages_unlocked(start
, (end
- start
) >> PAGE_SHIFT
,
293 pages
, write
? FOLL_WRITE
: 0);
295 /* Have to be a bit careful with return values */