1
2
3
4
5
6
7
8#include <linux/sched.h>
9#include <linux/mm.h>
10#include <linux/vmstat.h>
11#include <linux/highmem.h>
12#include <linux/swap.h>
13#include <linux/hugetlb.h>
14
15#include <asm/pgtable.h>
16
17static inline pte_t gup_get_pte(pte_t *ptep)
18{
19#if defined(CONFIG_64BIT_PHYS_ADDR) && defined(CONFIG_CPU_MIPS32)
20 pte_t pte;
21
22retry:
23 pte.pte_low = ptep->pte_low;
24 smp_rmb();
25 pte.pte_high = ptep->pte_high;
26 smp_rmb();
27 if (unlikely(pte.pte_low != ptep->pte_low))
28 goto retry;
29
30 return pte;
31#else
32 return ACCESS_ONCE(*ptep);
33#endif
34}
35
36static int gup_pte_range(pmd_t pmd, unsigned long addr, unsigned long end,
37 int write, struct page **pages, int *nr)
38{
39 pte_t *ptep = pte_offset_map(&pmd, addr);
40 do {
41 pte_t pte = gup_get_pte(ptep);
42 struct page *page;
43
44 if (!pte_present(pte) ||
45 pte_special(pte) || (write && !pte_write(pte))) {
46 pte_unmap(ptep);
47 return 0;
48 }
49 VM_BUG_ON(!pfn_valid(pte_pfn(pte)));
50 page = pte_page(pte);
51 get_page(page);
52 SetPageReferenced(page);
53 pages[*nr] = page;
54 (*nr)++;
55
56 } while (ptep++, addr += PAGE_SIZE, addr != end);
57
58 pte_unmap(ptep - 1);
59 return 1;
60}
61
62static inline void get_head_page_multiple(struct page *page, int nr)
63{
64 VM_BUG_ON(page != compound_head(page));
65 VM_BUG_ON(page_count(page) == 0);
66 page_ref_add(page, nr);
67 SetPageReferenced(page);
68}
69
70static int gup_huge_pmd(pmd_t pmd, unsigned long addr, unsigned long end,
71 int write, struct page **pages, int *nr)
72{
73 pte_t pte = *(pte_t *)&pmd;
74 struct page *head, *page;
75 int refs;
76
77 if (write && !pte_write(pte))
78 return 0;
79
80 VM_BUG_ON(pte_special(pte));
81 VM_BUG_ON(!pfn_valid(pte_pfn(pte)));
82
83 refs = 0;
84 head = pte_page(pte);
85 page = head + ((addr & ~PMD_MASK) >> PAGE_SHIFT);
86 do {
87 VM_BUG_ON(compound_head(page) != head);
88 pages[*nr] = page;
89 if (PageTail(page))
90 get_huge_page_tail(page);
91 (*nr)++;
92 page++;
93 refs++;
94 } while (addr += PAGE_SIZE, addr != end);
95
96 get_head_page_multiple(head, refs);
97 return 1;
98}
99
100static int gup_pmd_range(pud_t pud, unsigned long addr, unsigned long end,
101 int write, struct page **pages, int *nr)
102{
103 unsigned long next;
104 pmd_t *pmdp;
105
106 pmdp = pmd_offset(&pud, addr);
107 do {
108 pmd_t pmd = *pmdp;
109
110 next = pmd_addr_end(addr, end);
111
112
113
114
115
116
117
118
119
120
121
122 if (pmd_none(pmd) || pmd_trans_splitting(pmd))
123 return 0;
124 if (unlikely(pmd_huge(pmd))) {
125 if (!gup_huge_pmd(pmd, addr, next, write, pages,nr))
126 return 0;
127 } else {
128 if (!gup_pte_range(pmd, addr, next, write, pages,nr))
129 return 0;
130 }
131 } while (pmdp++, addr = next, addr != end);
132
133 return 1;
134}
135
136static int gup_huge_pud(pud_t pud, unsigned long addr, unsigned long end,
137 int write, struct page **pages, int *nr)
138{
139 pte_t pte = *(pte_t *)&pud;
140 struct page *head, *page;
141 int refs;
142
143 if (write && !pte_write(pte))
144 return 0;
145
146 VM_BUG_ON(pte_special(pte));
147 VM_BUG_ON(!pfn_valid(pte_pfn(pte)));
148
149 refs = 0;
150 head = pte_page(pte);
151 page = head + ((addr & ~PUD_MASK) >> PAGE_SHIFT);
152 do {
153 VM_BUG_ON(compound_head(page) != head);
154 pages[*nr] = page;
155 if (PageTail(page))
156 get_huge_page_tail(page);
157 (*nr)++;
158 page++;
159 refs++;
160 } while (addr += PAGE_SIZE, addr != end);
161
162 get_head_page_multiple(head, refs);
163 return 1;
164}
165
166static int gup_pud_range(pgd_t pgd, unsigned long addr, unsigned long end,
167 int write, struct page **pages, int *nr)
168{
169 unsigned long next;
170 pud_t *pudp;
171
172 pudp = pud_offset(&pgd, addr);
173 do {
174 pud_t pud = *pudp;
175
176 next = pud_addr_end(addr, end);
177 if (pud_none(pud))
178 return 0;
179 if (unlikely(pud_huge(pud))) {
180 if (!gup_huge_pud(pud, addr, next, write, pages,nr))
181 return 0;
182 } else {
183 if (!gup_pmd_range(pud, addr, next, write, pages,nr))
184 return 0;
185 }
186 } while (pudp++, addr = next, addr != end);
187
188 return 1;
189}
190
191
192
193
194
195int __get_user_pages_fast(unsigned long start, int nr_pages, int write,
196 struct page **pages)
197{
198 struct mm_struct *mm = current->mm;
199 unsigned long addr, len, end;
200 unsigned long next;
201 unsigned long flags;
202 pgd_t *pgdp;
203 int nr = 0;
204
205 start &= PAGE_MASK;
206 addr = start;
207 len = (unsigned long) nr_pages << PAGE_SHIFT;
208 end = start + len;
209 if (unlikely(!access_ok(write ? VERIFY_WRITE : VERIFY_READ,
210 (void __user *)start, len)))
211 return 0;
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230 local_irq_save(flags);
231 pgdp = pgd_offset(mm, addr);
232 do {
233 pgd_t pgd = *pgdp;
234
235 next = pgd_addr_end(addr, end);
236 if (pgd_none(pgd))
237 break;
238 if (!gup_pud_range(pgd, addr, next, write, pages, &nr))
239 break;
240 } while (pgdp++, addr = next, addr != end);
241 local_irq_restore(flags);
242
243 return nr;
244}
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262int get_user_pages_fast(unsigned long start, int nr_pages, int write,
263 struct page **pages)
264{
265 struct mm_struct *mm = current->mm;
266 unsigned long addr, len, end;
267 unsigned long next;
268 pgd_t *pgdp;
269 int ret, nr = 0;
270
271 start &= PAGE_MASK;
272 addr = start;
273 len = (unsigned long) nr_pages << PAGE_SHIFT;
274
275 end = start + len;
276 if (end < start)
277 goto slow_irqon;
278
279
280 local_irq_disable();
281 pgdp = pgd_offset(mm, addr);
282 do {
283 pgd_t pgd = *pgdp;
284
285 next = pgd_addr_end(addr, end);
286 if (pgd_none(pgd))
287 goto slow;
288 if (!gup_pud_range(pgd, addr, next, write, pages, &nr))
289 goto slow;
290 } while (pgdp++, addr = next, addr != end);
291 local_irq_enable();
292
293 VM_BUG_ON(nr != (end - start) >> PAGE_SHIFT);
294 return nr;
295slow:
296 local_irq_enable();
297
298slow_irqon:
299
300 start += nr << PAGE_SHIFT;
301 pages += nr;
302
303 ret = get_user_pages_unlocked(current, mm, start,
304 (end - start) >> PAGE_SHIFT,
305 write, 0, pages);
306
307
308 if (nr > 0) {
309 if (ret < 0)
310 ret = nr;
311 else
312 ret += nr;
313 }
314 return ret;
315}
316