License cleanup: add SPDX GPL-2.0 license identifier to files with no license
[linux-block.git] / arch / sh / mm / gup.c
CommitLineData
b2441318 1// SPDX-License-Identifier: GPL-2.0
667b279b
PM
2/*
3 * Lockless get_user_pages_fast for SuperH
4 *
5 * Copyright (C) 2009 - 2010 Paul Mundt
6 *
7 * Cloned from the x86 and PowerPC versions, by:
8 *
9 * Copyright (C) 2008 Nick Piggin
10 * Copyright (C) 2008 Novell Inc.
11 */
12#include <linux/sched.h>
13#include <linux/mm.h>
14#include <linux/vmstat.h>
15#include <linux/highmem.h>
16#include <asm/pgtable.h>
17
18static inline pte_t gup_get_pte(pte_t *ptep)
19{
20#ifndef CONFIG_X2TLB
378af02b 21 return READ_ONCE(*ptep);
667b279b
PM
22#else
23 /*
24 * With get_user_pages_fast, we walk down the pagetables without
25 * taking any locks. For this we would like to load the pointers
26 * atomically, but that is not possible with 64-bit PTEs. What
27 * we do have is the guarantee that a pte will only either go
28 * from not present to present, or present to not present or both
29 * -- it will not switch to a completely different present page
30 * without a TLB flush in between; something that we are blocking
31 * by holding interrupts off.
32 *
33 * Setting ptes from not present to present goes:
34 * ptep->pte_high = h;
35 * smp_wmb();
36 * ptep->pte_low = l;
37 *
38 * And present to not present goes:
39 * ptep->pte_low = 0;
40 * smp_wmb();
41 * ptep->pte_high = 0;
42 *
43 * We must ensure here that the load of pte_low sees l iff pte_high
44 * sees h. We load pte_high *after* loading pte_low, which ensures we
45 * don't see an older value of pte_high. *Then* we recheck pte_low,
46 * which ensures that we haven't picked up a changed pte high. We might
47 * have got rubbish values from pte_low and pte_high, but we are
48 * guaranteed that pte_low will not have the present bit set *unless*
49 * it is 'l'. And get_user_pages_fast only operates on present ptes, so
50 * we're safe.
51 *
52 * gup_get_pte should not be used or copied outside gup.c without being
53 * very careful -- it does not atomically load the pte or anything that
54 * is likely to be useful for you.
55 */
56 pte_t pte;
57
58retry:
59 pte.pte_low = ptep->pte_low;
60 smp_rmb();
61 pte.pte_high = ptep->pte_high;
62 smp_rmb();
63 if (unlikely(pte.pte_low != ptep->pte_low))
64 goto retry;
65
66 return pte;
67#endif
68}
69
70/*
71 * The performance critical leaf functions are made noinline otherwise gcc
72 * inlines everything into a single function which results in too much
73 * register pressure.
74 */
75static noinline int gup_pte_range(pmd_t pmd, unsigned long addr,
76 unsigned long end, int write, struct page **pages, int *nr)
77{
78 u64 mask, result;
79 pte_t *ptep;
80
81#ifdef CONFIG_X2TLB
82 result = _PAGE_PRESENT | _PAGE_EXT(_PAGE_EXT_KERN_READ | _PAGE_EXT_USER_READ);
83 if (write)
84 result |= _PAGE_EXT(_PAGE_EXT_KERN_WRITE | _PAGE_EXT_USER_WRITE);
85#elif defined(CONFIG_SUPERH64)
86 result = _PAGE_PRESENT | _PAGE_USER | _PAGE_READ;
87 if (write)
88 result |= _PAGE_WRITE;
89#else
90 result = _PAGE_PRESENT | _PAGE_USER;
91 if (write)
92 result |= _PAGE_RW;
93#endif
94
95 mask = result | _PAGE_SPECIAL;
96
97 ptep = pte_offset_map(&pmd, addr);
98 do {
99 pte_t pte = gup_get_pte(ptep);
100 struct page *page;
101
102 if ((pte_val(pte) & mask) != result) {
103 pte_unmap(ptep);
104 return 0;
105 }
106 VM_BUG_ON(!pfn_valid(pte_pfn(pte)));
107 page = pte_page(pte);
108 get_page(page);
caac7e6d
SS
109 __flush_anon_page(page, addr);
110 flush_dcache_page(page);
667b279b
PM
111 pages[*nr] = page;
112 (*nr)++;
113
114 } while (ptep++, addr += PAGE_SIZE, addr != end);
115 pte_unmap(ptep - 1);
116
117 return 1;
118}
119
120static int gup_pmd_range(pud_t pud, unsigned long addr, unsigned long end,
121 int write, struct page **pages, int *nr)
122{
123 unsigned long next;
124 pmd_t *pmdp;
125
126 pmdp = pmd_offset(&pud, addr);
127 do {
128 pmd_t pmd = *pmdp;
129
130 next = pmd_addr_end(addr, end);
131 if (pmd_none(pmd))
132 return 0;
133 if (!gup_pte_range(pmd, addr, next, write, pages, nr))
134 return 0;
135 } while (pmdp++, addr = next, addr != end);
136
137 return 1;
138}
139
140static int gup_pud_range(pgd_t pgd, unsigned long addr, unsigned long end,
141 int write, struct page **pages, int *nr)
142{
143 unsigned long next;
144 pud_t *pudp;
145
146 pudp = pud_offset(&pgd, addr);
147 do {
148 pud_t pud = *pudp;
149
150 next = pud_addr_end(addr, end);
151 if (pud_none(pud))
152 return 0;
153 if (!gup_pmd_range(pud, addr, next, write, pages, nr))
154 return 0;
155 } while (pudp++, addr = next, addr != end);
156
157 return 1;
158}
159
160/*
161 * Like get_user_pages_fast() except its IRQ-safe in that it won't fall
162 * back to the regular GUP.
163 */
164int __get_user_pages_fast(unsigned long start, int nr_pages, int write,
165 struct page **pages)
166{
167 struct mm_struct *mm = current->mm;
168 unsigned long addr, len, end;
169 unsigned long next;
170 unsigned long flags;
171 pgd_t *pgdp;
172 int nr = 0;
173
174 start &= PAGE_MASK;
175 addr = start;
176 len = (unsigned long) nr_pages << PAGE_SHIFT;
177 end = start + len;
178 if (unlikely(!access_ok(write ? VERIFY_WRITE : VERIFY_READ,
179 (void __user *)start, len)))
180 return 0;
181
182 /*
183 * This doesn't prevent pagetable teardown, but does prevent
184 * the pagetables and pages from being freed.
185 */
186 local_irq_save(flags);
187 pgdp = pgd_offset(mm, addr);
188 do {
189 pgd_t pgd = *pgdp;
190
191 next = pgd_addr_end(addr, end);
192 if (pgd_none(pgd))
193 break;
194 if (!gup_pud_range(pgd, addr, next, write, pages, &nr))
195 break;
196 } while (pgdp++, addr = next, addr != end);
197 local_irq_restore(flags);
198
199 return nr;
200}
201
202/**
203 * get_user_pages_fast() - pin user pages in memory
204 * @start: starting user address
205 * @nr_pages: number of pages from start to pin
206 * @write: whether pages will be written to
207 * @pages: array that receives pointers to the pages pinned.
208 * Should be at least nr_pages long.
209 *
210 * Attempt to pin user pages in memory without taking mm->mmap_sem.
211 * If not successful, it will fall back to taking the lock and
212 * calling get_user_pages().
213 *
214 * Returns number of pages pinned. This may be fewer than the number
215 * requested. If nr_pages is 0 or negative, returns 0. If no pages
216 * were pinned, returns -errno.
217 */
218int get_user_pages_fast(unsigned long start, int nr_pages, int write,
219 struct page **pages)
220{
221 struct mm_struct *mm = current->mm;
222 unsigned long addr, len, end;
223 unsigned long next;
224 pgd_t *pgdp;
225 int nr = 0;
226
227 start &= PAGE_MASK;
228 addr = start;
229 len = (unsigned long) nr_pages << PAGE_SHIFT;
230
231 end = start + len;
232 if (end < start)
233 goto slow_irqon;
234
235 local_irq_disable();
236 pgdp = pgd_offset(mm, addr);
237 do {
238 pgd_t pgd = *pgdp;
239
240 next = pgd_addr_end(addr, end);
241 if (pgd_none(pgd))
242 goto slow;
243 if (!gup_pud_range(pgd, addr, next, write, pages, &nr))
244 goto slow;
245 } while (pgdp++, addr = next, addr != end);
246 local_irq_enable();
247
248 VM_BUG_ON(nr != (end - start) >> PAGE_SHIFT);
249 return nr;
250
251 {
252 int ret;
253
254slow:
255 local_irq_enable();
256slow_irqon:
257 /* Try to get the remaining pages with get_user_pages */
258 start += nr << PAGE_SHIFT;
259 pages += nr;
260
d4edcf0d 261 ret = get_user_pages_unlocked(start,
c164154f
LS
262 (end - start) >> PAGE_SHIFT, pages,
263 write ? FOLL_WRITE : 0);
667b279b
PM
264
265 /* Have to be a bit careful with return values */
266 if (nr > 0) {
267 if (ret < 0)
268 ret = nr;
269 else
270 ret += nr;
271 }
272
273 return ret;
274 }
275}