MIPS: eBPF: Fix icache flush end address
[linux/fpc-iii.git] / arch / mips / mm / gup.c
blob5a4875cac1ec979da60be7ed3f80379eab5d6b65
1 // SPDX-License-Identifier: GPL-2.0
2 /*
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
8 */
9 #include <linux/sched.h>
10 #include <linux/mm.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)
22 pte_t pte;
24 retry:
25 pte.pte_low = ptep->pte_low;
26 smp_rmb();
27 pte.pte_high = ptep->pte_high;
28 smp_rmb();
29 if (unlikely(pte.pte_low != ptep->pte_low))
30 goto retry;
32 return pte;
33 #else
34 return READ_ONCE(*ptep);
35 #endif
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);
42 do {
43 pte_t pte = gup_get_pte(ptep);
44 struct page *page;
46 if (!pte_present(pte) ||
47 pte_special(pte) || (write && !pte_write(pte))) {
48 pte_unmap(ptep);
49 return 0;
51 VM_BUG_ON(!pfn_valid(pte_pfn(pte)));
52 page = pte_page(pte);
53 get_page(page);
54 SetPageReferenced(page);
55 pages[*nr] = page;
56 (*nr)++;
58 } while (ptep++, addr += PAGE_SIZE, addr != end);
60 pte_unmap(ptep - 1);
61 return 1;
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;
77 int refs;
79 if (write && !pte_write(pte))
80 return 0;
81 /* hugepages are never "special" */
82 VM_BUG_ON(pte_special(pte));
83 VM_BUG_ON(!pfn_valid(pte_pfn(pte)));
85 refs = 0;
86 head = pte_page(pte);
87 page = head + ((addr & ~PMD_MASK) >> PAGE_SHIFT);
88 do {
89 VM_BUG_ON(compound_head(page) != head);
90 pages[*nr] = page;
91 (*nr)++;
92 page++;
93 refs++;
94 } while (addr += PAGE_SIZE, addr != end);
96 get_head_page_multiple(head, refs);
97 return 1;
100 static int gup_pmd_range(pud_t pud, unsigned long addr, unsigned long end,
101 int write, struct page **pages, int *nr)
103 unsigned long next;
104 pmd_t *pmdp;
106 pmdp = pmd_offset(&pud, addr);
107 do {
108 pmd_t pmd = *pmdp;
110 next = pmd_addr_end(addr, end);
111 if (pmd_none(pmd))
112 return 0;
113 if (unlikely(pmd_huge(pmd))) {
114 if (!gup_huge_pmd(pmd, addr, next, write, pages,nr))
115 return 0;
116 } else {
117 if (!gup_pte_range(pmd, addr, next, write, pages,nr))
118 return 0;
120 } while (pmdp++, addr = next, addr != end);
122 return 1;
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;
130 int refs;
132 if (write && !pte_write(pte))
133 return 0;
134 /* hugepages are never "special" */
135 VM_BUG_ON(pte_special(pte));
136 VM_BUG_ON(!pfn_valid(pte_pfn(pte)));
138 refs = 0;
139 head = pte_page(pte);
140 page = head + ((addr & ~PUD_MASK) >> PAGE_SHIFT);
141 do {
142 VM_BUG_ON(compound_head(page) != head);
143 pages[*nr] = page;
144 (*nr)++;
145 page++;
146 refs++;
147 } while (addr += PAGE_SIZE, addr != end);
149 get_head_page_multiple(head, refs);
150 return 1;
153 static int gup_pud_range(pgd_t pgd, unsigned long addr, unsigned long end,
154 int write, struct page **pages, int *nr)
156 unsigned long next;
157 pud_t *pudp;
159 pudp = pud_offset(&pgd, addr);
160 do {
161 pud_t pud = *pudp;
163 next = pud_addr_end(addr, end);
164 if (pud_none(pud))
165 return 0;
166 if (unlikely(pud_huge(pud))) {
167 if (!gup_huge_pud(pud, addr, next, write, pages,nr))
168 return 0;
169 } else {
170 if (!gup_pmd_range(pud, addr, next, write, pages,nr))
171 return 0;
173 } while (pudp++, addr = next, addr != end);
175 return 1;
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,
185 struct page **pages)
187 struct mm_struct *mm = current->mm;
188 unsigned long addr, len, end;
189 unsigned long next;
190 unsigned long flags;
191 pgd_t *pgdp;
192 int nr = 0;
194 start &= PAGE_MASK;
195 addr = start;
196 len = (unsigned long) nr_pages << PAGE_SHIFT;
197 end = start + len;
198 if (unlikely(!access_ok(write ? VERIFY_WRITE : VERIFY_READ,
199 (void __user *)start, len)))
200 return 0;
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);
221 do {
222 pgd_t pgd = *pgdp;
224 next = pgd_addr_end(addr, end);
225 if (pgd_none(pgd))
226 break;
227 if (!gup_pud_range(pgd, addr, next, write, pages, &nr))
228 break;
229 } while (pgdp++, addr = next, addr != end);
230 local_irq_restore(flags);
232 return nr;
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,
252 struct page **pages)
254 struct mm_struct *mm = current->mm;
255 unsigned long addr, len, end;
256 unsigned long next;
257 pgd_t *pgdp;
258 int ret, nr = 0;
260 start &= PAGE_MASK;
261 addr = start;
262 len = (unsigned long) nr_pages << PAGE_SHIFT;
264 end = start + len;
265 if (end < start || cpu_has_dc_aliases)
266 goto slow_irqon;
268 /* XXX: batch / limit 'nr' */
269 local_irq_disable();
270 pgdp = pgd_offset(mm, addr);
271 do {
272 pgd_t pgd = *pgdp;
274 next = pgd_addr_end(addr, end);
275 if (pgd_none(pgd))
276 goto slow;
277 if (!gup_pud_range(pgd, addr, next, write, pages, &nr))
278 goto slow;
279 } while (pgdp++, addr = next, addr != end);
280 local_irq_enable();
282 VM_BUG_ON(nr != (end - start) >> PAGE_SHIFT);
283 return nr;
284 slow:
285 local_irq_enable();
287 slow_irqon:
288 /* Try to get the remaining pages with get_user_pages */
289 start += nr << PAGE_SHIFT;
290 pages += nr;
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 */
296 if (nr > 0) {
297 if (ret < 0)
298 ret = nr;
299 else
300 ret += nr;
302 return ret;