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