mm: fix exec activate_mm vs TLB shootdown and lazy tlb switching race
[linux/fpc-iii.git] / arch / sparc / mm / gup.c
blob5335ba3c850ed3acdc074ffe639d3ddac101f2ad
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Lockless get_user_pages_fast for sparc, cribbed from powerpc
5 * Copyright (C) 2008 Nick Piggin
6 * Copyright (C) 2008 Novell Inc.
7 */
9 #include <linux/sched.h>
10 #include <linux/mm.h>
11 #include <linux/vmstat.h>
12 #include <linux/pagemap.h>
13 #include <linux/rwsem.h>
14 #include <asm/pgtable.h>
17 * The performance critical leaf functions are made noinline otherwise gcc
18 * inlines everything into a single function which results in too much
19 * register pressure.
21 static noinline int gup_pte_range(pmd_t pmd, unsigned long addr,
22 unsigned long end, int write, struct page **pages, int *nr)
24 unsigned long mask, result;
25 pte_t *ptep;
27 if (tlb_type == hypervisor) {
28 result = _PAGE_PRESENT_4V|_PAGE_P_4V;
29 if (write)
30 result |= _PAGE_WRITE_4V;
31 } else {
32 result = _PAGE_PRESENT_4U|_PAGE_P_4U;
33 if (write)
34 result |= _PAGE_WRITE_4U;
36 mask = result | _PAGE_SPECIAL;
38 ptep = pte_offset_kernel(&pmd, addr);
39 do {
40 struct page *page, *head;
41 pte_t pte = *ptep;
43 if ((pte_val(pte) & mask) != result)
44 return 0;
45 VM_BUG_ON(!pfn_valid(pte_pfn(pte)));
47 /* The hugepage case is simplified on sparc64 because
48 * we encode the sub-page pfn offsets into the
49 * hugepage PTEs. We could optimize this in the future
50 * use page_cache_add_speculative() for the hugepage case.
52 page = pte_page(pte);
53 head = compound_head(page);
54 if (!page_cache_get_speculative(head))
55 return 0;
56 if (unlikely(pte_val(pte) != pte_val(*ptep))) {
57 put_page(head);
58 return 0;
61 pages[*nr] = page;
62 (*nr)++;
63 } while (ptep++, addr += PAGE_SIZE, addr != end);
65 return 1;
68 static int gup_huge_pmd(pmd_t *pmdp, pmd_t pmd, unsigned long addr,
69 unsigned long end, int write, struct page **pages,
70 int *nr)
72 struct page *head, *page;
73 int refs;
75 if (!(pmd_val(pmd) & _PAGE_VALID))
76 return 0;
78 if (write && !pmd_write(pmd))
79 return 0;
81 refs = 0;
82 page = pmd_page(pmd) + ((addr & ~PMD_MASK) >> PAGE_SHIFT);
83 head = compound_head(page);
84 do {
85 VM_BUG_ON(compound_head(page) != head);
86 pages[*nr] = page;
87 (*nr)++;
88 page++;
89 refs++;
90 } while (addr += PAGE_SIZE, addr != end);
92 if (!page_cache_add_speculative(head, refs)) {
93 *nr -= refs;
94 return 0;
97 if (unlikely(pmd_val(pmd) != pmd_val(*pmdp))) {
98 *nr -= refs;
99 while (refs--)
100 put_page(head);
101 return 0;
104 return 1;
107 static int gup_huge_pud(pud_t *pudp, pud_t pud, unsigned long addr,
108 unsigned long end, int write, struct page **pages,
109 int *nr)
111 struct page *head, *page;
112 int refs;
114 if (!(pud_val(pud) & _PAGE_VALID))
115 return 0;
117 if (write && !pud_write(pud))
118 return 0;
120 refs = 0;
121 page = pud_page(pud) + ((addr & ~PUD_MASK) >> PAGE_SHIFT);
122 head = compound_head(page);
123 do {
124 VM_BUG_ON(compound_head(page) != head);
125 pages[*nr] = page;
126 (*nr)++;
127 page++;
128 refs++;
129 } while (addr += PAGE_SIZE, addr != end);
131 if (!page_cache_add_speculative(head, refs)) {
132 *nr -= refs;
133 return 0;
136 if (unlikely(pud_val(pud) != pud_val(*pudp))) {
137 *nr -= refs;
138 while (refs--)
139 put_page(head);
140 return 0;
143 return 1;
146 static int gup_pmd_range(pud_t pud, unsigned long addr, unsigned long end,
147 int write, struct page **pages, int *nr)
149 unsigned long next;
150 pmd_t *pmdp;
152 pmdp = pmd_offset(&pud, addr);
153 do {
154 pmd_t pmd = *pmdp;
156 next = pmd_addr_end(addr, end);
157 if (pmd_none(pmd))
158 return 0;
159 if (unlikely(pmd_large(pmd))) {
160 if (!gup_huge_pmd(pmdp, pmd, addr, next,
161 write, pages, nr))
162 return 0;
163 } else if (!gup_pte_range(pmd, addr, next, write,
164 pages, nr))
165 return 0;
166 } while (pmdp++, addr = next, addr != end);
168 return 1;
171 static int gup_pud_range(pgd_t pgd, unsigned long addr, unsigned long end,
172 int write, struct page **pages, int *nr)
174 unsigned long next;
175 pud_t *pudp;
177 pudp = pud_offset(&pgd, addr);
178 do {
179 pud_t pud = *pudp;
181 next = pud_addr_end(addr, end);
182 if (pud_none(pud))
183 return 0;
184 if (unlikely(pud_large(pud))) {
185 if (!gup_huge_pud(pudp, pud, addr, next,
186 write, pages, nr))
187 return 0;
188 } else if (!gup_pmd_range(pud, addr, next, write, pages, nr))
189 return 0;
190 } while (pudp++, addr = next, addr != end);
192 return 1;
195 int __get_user_pages_fast(unsigned long start, int nr_pages, int write,
196 struct page **pages)
198 struct mm_struct *mm = current->mm;
199 unsigned long addr, len, end;
200 unsigned long next, flags;
201 pgd_t *pgdp;
202 int nr = 0;
204 start &= PAGE_MASK;
205 addr = start;
206 len = (unsigned long) nr_pages << PAGE_SHIFT;
207 end = start + len;
209 local_irq_save(flags);
210 pgdp = pgd_offset(mm, addr);
211 do {
212 pgd_t pgd = *pgdp;
214 next = pgd_addr_end(addr, end);
215 if (pgd_none(pgd))
216 break;
217 if (!gup_pud_range(pgd, addr, next, write, pages, &nr))
218 break;
219 } while (pgdp++, addr = next, addr != end);
220 local_irq_restore(flags);
222 return nr;
225 int get_user_pages_fast(unsigned long start, int nr_pages, int write,
226 struct page **pages)
228 struct mm_struct *mm = current->mm;
229 unsigned long addr, len, end;
230 unsigned long next;
231 pgd_t *pgdp;
232 int nr = 0;
234 start &= PAGE_MASK;
235 addr = start;
236 len = (unsigned long) nr_pages << PAGE_SHIFT;
237 end = start + len;
240 * XXX: batch / limit 'nr', to avoid large irq off latency
241 * needs some instrumenting to determine the common sizes used by
242 * important workloads (eg. DB2), and whether limiting the batch size
243 * will decrease performance.
245 * It seems like we're in the clear for the moment. Direct-IO is
246 * the main guy that batches up lots of get_user_pages, and even
247 * they are limited to 64-at-a-time which is not so many.
250 * This doesn't prevent pagetable teardown, but does prevent
251 * the pagetables from being freed on sparc.
253 * So long as we atomically load page table pointers versus teardown,
254 * we can follow the address down to the the page and take a ref on it.
256 local_irq_disable();
258 pgdp = pgd_offset(mm, addr);
259 do {
260 pgd_t pgd = *pgdp;
262 next = pgd_addr_end(addr, end);
263 if (pgd_none(pgd))
264 goto slow;
265 if (!gup_pud_range(pgd, addr, next, write, pages, &nr))
266 goto slow;
267 } while (pgdp++, addr = next, addr != end);
269 local_irq_enable();
271 VM_BUG_ON(nr != (end - start) >> PAGE_SHIFT);
272 return nr;
275 int ret;
277 slow:
278 local_irq_enable();
280 /* Try to get the remaining pages with get_user_pages */
281 start += nr << PAGE_SHIFT;
282 pages += nr;
284 ret = get_user_pages_unlocked(start,
285 (end - start) >> PAGE_SHIFT, pages,
286 write ? FOLL_WRITE : 0);
288 /* Have to be a bit careful with return values */
289 if (nr > 0) {
290 if (ret < 0)
291 ret = nr;
292 else
293 ret += nr;
296 return ret;