]>
Commit | Line | Data |
---|---|---|
4bbd4c77 KS |
1 | #include <linux/kernel.h> |
2 | #include <linux/errno.h> | |
3 | #include <linux/err.h> | |
4 | #include <linux/spinlock.h> | |
5 | ||
4bbd4c77 KS |
6 | #include <linux/mm.h> |
7 | #include <linux/pagemap.h> | |
8 | #include <linux/rmap.h> | |
9 | #include <linux/swap.h> | |
10 | #include <linux/swapops.h> | |
11 | ||
2667f50e SC |
12 | #include <linux/sched.h> |
13 | #include <linux/rwsem.h> | |
f30c59e9 | 14 | #include <linux/hugetlb.h> |
1027e443 | 15 | |
2667f50e | 16 | #include <asm/pgtable.h> |
1027e443 | 17 | #include <asm/tlbflush.h> |
2667f50e | 18 | |
4bbd4c77 KS |
19 | #include "internal.h" |
20 | ||
69e68b4f KS |
21 | static struct page *no_page_table(struct vm_area_struct *vma, |
22 | unsigned int flags) | |
4bbd4c77 | 23 | { |
69e68b4f KS |
24 | /* |
25 | * When core dumping an enormous anonymous area that nobody | |
26 | * has touched so far, we don't want to allocate unnecessary pages or | |
27 | * page tables. Return error instead of NULL to skip handle_mm_fault, | |
28 | * then get_dump_page() will return NULL to leave a hole in the dump. | |
29 | * But we can only make this optimization where a hole would surely | |
30 | * be zero-filled if handle_mm_fault() actually did handle it. | |
31 | */ | |
32 | if ((flags & FOLL_DUMP) && (!vma->vm_ops || !vma->vm_ops->fault)) | |
33 | return ERR_PTR(-EFAULT); | |
34 | return NULL; | |
35 | } | |
4bbd4c77 | 36 | |
1027e443 KS |
37 | static int follow_pfn_pte(struct vm_area_struct *vma, unsigned long address, |
38 | pte_t *pte, unsigned int flags) | |
39 | { | |
40 | /* No page to get reference */ | |
41 | if (flags & FOLL_GET) | |
42 | return -EFAULT; | |
43 | ||
44 | if (flags & FOLL_TOUCH) { | |
45 | pte_t entry = *pte; | |
46 | ||
47 | if (flags & FOLL_WRITE) | |
48 | entry = pte_mkdirty(entry); | |
49 | entry = pte_mkyoung(entry); | |
50 | ||
51 | if (!pte_same(*pte, entry)) { | |
52 | set_pte_at(vma->vm_mm, address, pte, entry); | |
53 | update_mmu_cache(vma, address, pte); | |
54 | } | |
55 | } | |
56 | ||
57 | /* Proper page table entry exists, but no corresponding struct page */ | |
58 | return -EEXIST; | |
59 | } | |
60 | ||
69e68b4f KS |
61 | static struct page *follow_page_pte(struct vm_area_struct *vma, |
62 | unsigned long address, pmd_t *pmd, unsigned int flags) | |
63 | { | |
64 | struct mm_struct *mm = vma->vm_mm; | |
65 | struct page *page; | |
66 | spinlock_t *ptl; | |
67 | pte_t *ptep, pte; | |
4bbd4c77 | 68 | |
69e68b4f | 69 | retry: |
4bbd4c77 | 70 | if (unlikely(pmd_bad(*pmd))) |
69e68b4f | 71 | return no_page_table(vma, flags); |
4bbd4c77 KS |
72 | |
73 | ptep = pte_offset_map_lock(mm, pmd, address, &ptl); | |
4bbd4c77 KS |
74 | pte = *ptep; |
75 | if (!pte_present(pte)) { | |
76 | swp_entry_t entry; | |
77 | /* | |
78 | * KSM's break_ksm() relies upon recognizing a ksm page | |
79 | * even while it is being migrated, so for that case we | |
80 | * need migration_entry_wait(). | |
81 | */ | |
82 | if (likely(!(flags & FOLL_MIGRATION))) | |
83 | goto no_page; | |
0661a336 | 84 | if (pte_none(pte)) |
4bbd4c77 KS |
85 | goto no_page; |
86 | entry = pte_to_swp_entry(pte); | |
87 | if (!is_migration_entry(entry)) | |
88 | goto no_page; | |
89 | pte_unmap_unlock(ptep, ptl); | |
90 | migration_entry_wait(mm, pmd, address); | |
69e68b4f | 91 | goto retry; |
4bbd4c77 | 92 | } |
8a0516ed | 93 | if ((flags & FOLL_NUMA) && pte_protnone(pte)) |
4bbd4c77 | 94 | goto no_page; |
69e68b4f KS |
95 | if ((flags & FOLL_WRITE) && !pte_write(pte)) { |
96 | pte_unmap_unlock(ptep, ptl); | |
97 | return NULL; | |
98 | } | |
4bbd4c77 KS |
99 | |
100 | page = vm_normal_page(vma, address, pte); | |
101 | if (unlikely(!page)) { | |
1027e443 KS |
102 | if (flags & FOLL_DUMP) { |
103 | /* Avoid special (like zero) pages in core dumps */ | |
104 | page = ERR_PTR(-EFAULT); | |
105 | goto out; | |
106 | } | |
107 | ||
108 | if (is_zero_pfn(pte_pfn(pte))) { | |
109 | page = pte_page(pte); | |
110 | } else { | |
111 | int ret; | |
112 | ||
113 | ret = follow_pfn_pte(vma, address, ptep, flags); | |
114 | page = ERR_PTR(ret); | |
115 | goto out; | |
116 | } | |
4bbd4c77 KS |
117 | } |
118 | ||
6742d293 KS |
119 | if (flags & FOLL_SPLIT && PageTransCompound(page)) { |
120 | int ret; | |
121 | get_page(page); | |
122 | pte_unmap_unlock(ptep, ptl); | |
123 | lock_page(page); | |
124 | ret = split_huge_page(page); | |
125 | unlock_page(page); | |
126 | put_page(page); | |
127 | if (ret) | |
128 | return ERR_PTR(ret); | |
129 | goto retry; | |
130 | } | |
131 | ||
4bbd4c77 KS |
132 | if (flags & FOLL_GET) |
133 | get_page_foll(page); | |
134 | if (flags & FOLL_TOUCH) { | |
135 | if ((flags & FOLL_WRITE) && | |
136 | !pte_dirty(pte) && !PageDirty(page)) | |
137 | set_page_dirty(page); | |
138 | /* | |
139 | * pte_mkyoung() would be more correct here, but atomic care | |
140 | * is needed to avoid losing the dirty bit: it is easier to use | |
141 | * mark_page_accessed(). | |
142 | */ | |
143 | mark_page_accessed(page); | |
144 | } | |
de60f5f1 | 145 | if ((flags & FOLL_MLOCK) && (vma->vm_flags & VM_LOCKED)) { |
4bbd4c77 KS |
146 | /* |
147 | * The preliminary mapping check is mainly to avoid the | |
148 | * pointless overhead of lock_page on the ZERO_PAGE | |
149 | * which might bounce very badly if there is contention. | |
150 | * | |
151 | * If the page is already locked, we don't need to | |
152 | * handle it now - vmscan will handle it later if and | |
153 | * when it attempts to reclaim the page. | |
154 | */ | |
155 | if (page->mapping && trylock_page(page)) { | |
156 | lru_add_drain(); /* push cached pages to LRU */ | |
157 | /* | |
158 | * Because we lock page here, and migration is | |
159 | * blocked by the pte's page reference, and we | |
160 | * know the page is still mapped, we don't even | |
161 | * need to check for file-cache page truncation. | |
162 | */ | |
163 | mlock_vma_page(page); | |
164 | unlock_page(page); | |
165 | } | |
166 | } | |
1027e443 | 167 | out: |
4bbd4c77 | 168 | pte_unmap_unlock(ptep, ptl); |
4bbd4c77 | 169 | return page; |
4bbd4c77 KS |
170 | no_page: |
171 | pte_unmap_unlock(ptep, ptl); | |
172 | if (!pte_none(pte)) | |
69e68b4f KS |
173 | return NULL; |
174 | return no_page_table(vma, flags); | |
175 | } | |
176 | ||
177 | /** | |
178 | * follow_page_mask - look up a page descriptor from a user-virtual address | |
179 | * @vma: vm_area_struct mapping @address | |
180 | * @address: virtual address to look up | |
181 | * @flags: flags modifying lookup behaviour | |
182 | * @page_mask: on output, *page_mask is set according to the size of the page | |
183 | * | |
184 | * @flags can have FOLL_ flags set, defined in <linux/mm.h> | |
185 | * | |
186 | * Returns the mapped (struct page *), %NULL if no mapping exists, or | |
187 | * an error pointer if there is a mapping to something not represented | |
188 | * by a page descriptor (see also vm_normal_page()). | |
189 | */ | |
190 | struct page *follow_page_mask(struct vm_area_struct *vma, | |
191 | unsigned long address, unsigned int flags, | |
192 | unsigned int *page_mask) | |
193 | { | |
194 | pgd_t *pgd; | |
195 | pud_t *pud; | |
196 | pmd_t *pmd; | |
197 | spinlock_t *ptl; | |
198 | struct page *page; | |
199 | struct mm_struct *mm = vma->vm_mm; | |
200 | ||
201 | *page_mask = 0; | |
202 | ||
203 | page = follow_huge_addr(mm, address, flags & FOLL_WRITE); | |
204 | if (!IS_ERR(page)) { | |
205 | BUG_ON(flags & FOLL_GET); | |
4bbd4c77 | 206 | return page; |
69e68b4f | 207 | } |
4bbd4c77 | 208 | |
69e68b4f KS |
209 | pgd = pgd_offset(mm, address); |
210 | if (pgd_none(*pgd) || unlikely(pgd_bad(*pgd))) | |
211 | return no_page_table(vma, flags); | |
212 | ||
213 | pud = pud_offset(pgd, address); | |
214 | if (pud_none(*pud)) | |
215 | return no_page_table(vma, flags); | |
216 | if (pud_huge(*pud) && vma->vm_flags & VM_HUGETLB) { | |
e66f17ff NH |
217 | page = follow_huge_pud(mm, address, pud, flags); |
218 | if (page) | |
219 | return page; | |
220 | return no_page_table(vma, flags); | |
69e68b4f KS |
221 | } |
222 | if (unlikely(pud_bad(*pud))) | |
223 | return no_page_table(vma, flags); | |
224 | ||
225 | pmd = pmd_offset(pud, address); | |
226 | if (pmd_none(*pmd)) | |
227 | return no_page_table(vma, flags); | |
228 | if (pmd_huge(*pmd) && vma->vm_flags & VM_HUGETLB) { | |
e66f17ff NH |
229 | page = follow_huge_pmd(mm, address, pmd, flags); |
230 | if (page) | |
231 | return page; | |
232 | return no_page_table(vma, flags); | |
69e68b4f | 233 | } |
8a0516ed | 234 | if ((flags & FOLL_NUMA) && pmd_protnone(*pmd)) |
69e68b4f | 235 | return no_page_table(vma, flags); |
6742d293 KS |
236 | if (likely(!pmd_trans_huge(*pmd))) |
237 | return follow_page_pte(vma, address, pmd, flags); | |
238 | ||
239 | ptl = pmd_lock(mm, pmd); | |
240 | if (unlikely(!pmd_trans_huge(*pmd))) { | |
241 | spin_unlock(ptl); | |
242 | return follow_page_pte(vma, address, pmd, flags); | |
243 | } | |
244 | ||
245 | if (unlikely(pmd_trans_splitting(*pmd))) { | |
246 | spin_unlock(ptl); | |
247 | wait_split_huge_page(vma->anon_vma, pmd); | |
248 | return follow_page_pte(vma, address, pmd, flags); | |
249 | } | |
250 | ||
251 | if (flags & FOLL_SPLIT) { | |
252 | int ret; | |
253 | page = pmd_page(*pmd); | |
254 | if (is_huge_zero_page(page)) { | |
255 | spin_unlock(ptl); | |
256 | ret = 0; | |
69e68b4f | 257 | split_huge_page_pmd(vma, address, pmd); |
6742d293 KS |
258 | } else { |
259 | get_page(page); | |
69e68b4f | 260 | spin_unlock(ptl); |
6742d293 KS |
261 | lock_page(page); |
262 | ret = split_huge_page(page); | |
263 | unlock_page(page); | |
264 | put_page(page); | |
265 | } | |
266 | ||
267 | return ret ? ERR_PTR(ret) : | |
268 | follow_page_pte(vma, address, pmd, flags); | |
69e68b4f | 269 | } |
6742d293 KS |
270 | |
271 | page = follow_trans_huge_pmd(vma, address, pmd, flags); | |
272 | spin_unlock(ptl); | |
273 | *page_mask = HPAGE_PMD_NR - 1; | |
274 | return page; | |
4bbd4c77 KS |
275 | } |
276 | ||
f2b495ca KS |
277 | static int get_gate_page(struct mm_struct *mm, unsigned long address, |
278 | unsigned int gup_flags, struct vm_area_struct **vma, | |
279 | struct page **page) | |
280 | { | |
281 | pgd_t *pgd; | |
282 | pud_t *pud; | |
283 | pmd_t *pmd; | |
284 | pte_t *pte; | |
285 | int ret = -EFAULT; | |
286 | ||
287 | /* user gate pages are read-only */ | |
288 | if (gup_flags & FOLL_WRITE) | |
289 | return -EFAULT; | |
290 | if (address > TASK_SIZE) | |
291 | pgd = pgd_offset_k(address); | |
292 | else | |
293 | pgd = pgd_offset_gate(mm, address); | |
294 | BUG_ON(pgd_none(*pgd)); | |
295 | pud = pud_offset(pgd, address); | |
296 | BUG_ON(pud_none(*pud)); | |
297 | pmd = pmd_offset(pud, address); | |
298 | if (pmd_none(*pmd)) | |
299 | return -EFAULT; | |
300 | VM_BUG_ON(pmd_trans_huge(*pmd)); | |
301 | pte = pte_offset_map(pmd, address); | |
302 | if (pte_none(*pte)) | |
303 | goto unmap; | |
304 | *vma = get_gate_vma(mm); | |
305 | if (!page) | |
306 | goto out; | |
307 | *page = vm_normal_page(*vma, address, *pte); | |
308 | if (!*page) { | |
309 | if ((gup_flags & FOLL_DUMP) || !is_zero_pfn(pte_pfn(*pte))) | |
310 | goto unmap; | |
311 | *page = pte_page(*pte); | |
312 | } | |
313 | get_page(*page); | |
314 | out: | |
315 | ret = 0; | |
316 | unmap: | |
317 | pte_unmap(pte); | |
318 | return ret; | |
319 | } | |
320 | ||
9a95f3cf PC |
321 | /* |
322 | * mmap_sem must be held on entry. If @nonblocking != NULL and | |
323 | * *@flags does not include FOLL_NOWAIT, the mmap_sem may be released. | |
324 | * If it is, *@nonblocking will be set to 0 and -EBUSY returned. | |
325 | */ | |
16744483 KS |
326 | static int faultin_page(struct task_struct *tsk, struct vm_area_struct *vma, |
327 | unsigned long address, unsigned int *flags, int *nonblocking) | |
328 | { | |
329 | struct mm_struct *mm = vma->vm_mm; | |
330 | unsigned int fault_flags = 0; | |
331 | int ret; | |
332 | ||
de60f5f1 EM |
333 | /* mlock all present pages, but do not fault in new pages */ |
334 | if ((*flags & (FOLL_POPULATE | FOLL_MLOCK)) == FOLL_MLOCK) | |
335 | return -ENOENT; | |
84d33df2 KS |
336 | /* For mm_populate(), just skip the stack guard page. */ |
337 | if ((*flags & FOLL_POPULATE) && | |
16744483 KS |
338 | (stack_guard_page_start(vma, address) || |
339 | stack_guard_page_end(vma, address + PAGE_SIZE))) | |
340 | return -ENOENT; | |
341 | if (*flags & FOLL_WRITE) | |
342 | fault_flags |= FAULT_FLAG_WRITE; | |
343 | if (nonblocking) | |
344 | fault_flags |= FAULT_FLAG_ALLOW_RETRY; | |
345 | if (*flags & FOLL_NOWAIT) | |
346 | fault_flags |= FAULT_FLAG_ALLOW_RETRY | FAULT_FLAG_RETRY_NOWAIT; | |
234b239b ALC |
347 | if (*flags & FOLL_TRIED) { |
348 | VM_WARN_ON_ONCE(fault_flags & FAULT_FLAG_ALLOW_RETRY); | |
349 | fault_flags |= FAULT_FLAG_TRIED; | |
350 | } | |
16744483 KS |
351 | |
352 | ret = handle_mm_fault(mm, vma, address, fault_flags); | |
353 | if (ret & VM_FAULT_ERROR) { | |
354 | if (ret & VM_FAULT_OOM) | |
355 | return -ENOMEM; | |
356 | if (ret & (VM_FAULT_HWPOISON | VM_FAULT_HWPOISON_LARGE)) | |
357 | return *flags & FOLL_HWPOISON ? -EHWPOISON : -EFAULT; | |
33692f27 | 358 | if (ret & (VM_FAULT_SIGBUS | VM_FAULT_SIGSEGV)) |
16744483 KS |
359 | return -EFAULT; |
360 | BUG(); | |
361 | } | |
362 | ||
363 | if (tsk) { | |
364 | if (ret & VM_FAULT_MAJOR) | |
365 | tsk->maj_flt++; | |
366 | else | |
367 | tsk->min_flt++; | |
368 | } | |
369 | ||
370 | if (ret & VM_FAULT_RETRY) { | |
371 | if (nonblocking) | |
372 | *nonblocking = 0; | |
373 | return -EBUSY; | |
374 | } | |
375 | ||
376 | /* | |
377 | * The VM_FAULT_WRITE bit tells us that do_wp_page has broken COW when | |
378 | * necessary, even if maybe_mkwrite decided not to set pte_write. We | |
379 | * can thus safely do subsequent page lookups as if they were reads. | |
380 | * But only do so when looping for pte_write is futile: in some cases | |
381 | * userspace may also be wanting to write to the gotten user page, | |
382 | * which a read fault here might prevent (a readonly page might get | |
383 | * reCOWed by userspace write). | |
384 | */ | |
385 | if ((ret & VM_FAULT_WRITE) && !(vma->vm_flags & VM_WRITE)) | |
386 | *flags &= ~FOLL_WRITE; | |
387 | return 0; | |
388 | } | |
389 | ||
fa5bb209 KS |
390 | static int check_vma_flags(struct vm_area_struct *vma, unsigned long gup_flags) |
391 | { | |
392 | vm_flags_t vm_flags = vma->vm_flags; | |
393 | ||
394 | if (vm_flags & (VM_IO | VM_PFNMAP)) | |
395 | return -EFAULT; | |
396 | ||
397 | if (gup_flags & FOLL_WRITE) { | |
398 | if (!(vm_flags & VM_WRITE)) { | |
399 | if (!(gup_flags & FOLL_FORCE)) | |
400 | return -EFAULT; | |
401 | /* | |
402 | * We used to let the write,force case do COW in a | |
403 | * VM_MAYWRITE VM_SHARED !VM_WRITE vma, so ptrace could | |
404 | * set a breakpoint in a read-only mapping of an | |
405 | * executable, without corrupting the file (yet only | |
406 | * when that file had been opened for writing!). | |
407 | * Anon pages in shared mappings are surprising: now | |
408 | * just reject it. | |
409 | */ | |
410 | if (!is_cow_mapping(vm_flags)) { | |
411 | WARN_ON_ONCE(vm_flags & VM_MAYWRITE); | |
412 | return -EFAULT; | |
413 | } | |
414 | } | |
415 | } else if (!(vm_flags & VM_READ)) { | |
416 | if (!(gup_flags & FOLL_FORCE)) | |
417 | return -EFAULT; | |
418 | /* | |
419 | * Is there actually any vma we can reach here which does not | |
420 | * have VM_MAYREAD set? | |
421 | */ | |
422 | if (!(vm_flags & VM_MAYREAD)) | |
423 | return -EFAULT; | |
424 | } | |
425 | return 0; | |
426 | } | |
427 | ||
4bbd4c77 KS |
428 | /** |
429 | * __get_user_pages() - pin user pages in memory | |
430 | * @tsk: task_struct of target task | |
431 | * @mm: mm_struct of target mm | |
432 | * @start: starting user address | |
433 | * @nr_pages: number of pages from start to pin | |
434 | * @gup_flags: flags modifying pin behaviour | |
435 | * @pages: array that receives pointers to the pages pinned. | |
436 | * Should be at least nr_pages long. Or NULL, if caller | |
437 | * only intends to ensure the pages are faulted in. | |
438 | * @vmas: array of pointers to vmas corresponding to each page. | |
439 | * Or NULL if the caller does not require them. | |
440 | * @nonblocking: whether waiting for disk IO or mmap_sem contention | |
441 | * | |
442 | * Returns number of pages pinned. This may be fewer than the number | |
443 | * requested. If nr_pages is 0 or negative, returns 0. If no pages | |
444 | * were pinned, returns -errno. Each page returned must be released | |
445 | * with a put_page() call when it is finished with. vmas will only | |
446 | * remain valid while mmap_sem is held. | |
447 | * | |
9a95f3cf | 448 | * Must be called with mmap_sem held. It may be released. See below. |
4bbd4c77 KS |
449 | * |
450 | * __get_user_pages walks a process's page tables and takes a reference to | |
451 | * each struct page that each user address corresponds to at a given | |
452 | * instant. That is, it takes the page that would be accessed if a user | |
453 | * thread accesses the given user virtual address at that instant. | |
454 | * | |
455 | * This does not guarantee that the page exists in the user mappings when | |
456 | * __get_user_pages returns, and there may even be a completely different | |
457 | * page there in some cases (eg. if mmapped pagecache has been invalidated | |
458 | * and subsequently re faulted). However it does guarantee that the page | |
459 | * won't be freed completely. And mostly callers simply care that the page | |
460 | * contains data that was valid *at some point in time*. Typically, an IO | |
461 | * or similar operation cannot guarantee anything stronger anyway because | |
462 | * locks can't be held over the syscall boundary. | |
463 | * | |
464 | * If @gup_flags & FOLL_WRITE == 0, the page must not be written to. If | |
465 | * the page is written to, set_page_dirty (or set_page_dirty_lock, as | |
466 | * appropriate) must be called after the page is finished with, and | |
467 | * before put_page is called. | |
468 | * | |
469 | * If @nonblocking != NULL, __get_user_pages will not wait for disk IO | |
470 | * or mmap_sem contention, and if waiting is needed to pin all pages, | |
9a95f3cf PC |
471 | * *@nonblocking will be set to 0. Further, if @gup_flags does not |
472 | * include FOLL_NOWAIT, the mmap_sem will be released via up_read() in | |
473 | * this case. | |
474 | * | |
475 | * A caller using such a combination of @nonblocking and @gup_flags | |
476 | * must therefore hold the mmap_sem for reading only, and recognize | |
477 | * when it's been released. Otherwise, it must be held for either | |
478 | * reading or writing and will not be released. | |
4bbd4c77 KS |
479 | * |
480 | * In most cases, get_user_pages or get_user_pages_fast should be used | |
481 | * instead of __get_user_pages. __get_user_pages should be used only if | |
482 | * you need some special @gup_flags. | |
483 | */ | |
484 | long __get_user_pages(struct task_struct *tsk, struct mm_struct *mm, | |
485 | unsigned long start, unsigned long nr_pages, | |
486 | unsigned int gup_flags, struct page **pages, | |
487 | struct vm_area_struct **vmas, int *nonblocking) | |
488 | { | |
fa5bb209 | 489 | long i = 0; |
4bbd4c77 | 490 | unsigned int page_mask; |
fa5bb209 | 491 | struct vm_area_struct *vma = NULL; |
4bbd4c77 KS |
492 | |
493 | if (!nr_pages) | |
494 | return 0; | |
495 | ||
496 | VM_BUG_ON(!!pages != !!(gup_flags & FOLL_GET)); | |
497 | ||
498 | /* | |
499 | * If FOLL_FORCE is set then do not force a full fault as the hinting | |
500 | * fault information is unrelated to the reference behaviour of a task | |
501 | * using the address space | |
502 | */ | |
503 | if (!(gup_flags & FOLL_FORCE)) | |
504 | gup_flags |= FOLL_NUMA; | |
505 | ||
4bbd4c77 | 506 | do { |
fa5bb209 KS |
507 | struct page *page; |
508 | unsigned int foll_flags = gup_flags; | |
509 | unsigned int page_increm; | |
510 | ||
511 | /* first iteration or cross vma bound */ | |
512 | if (!vma || start >= vma->vm_end) { | |
513 | vma = find_extend_vma(mm, start); | |
514 | if (!vma && in_gate_area(mm, start)) { | |
515 | int ret; | |
516 | ret = get_gate_page(mm, start & PAGE_MASK, | |
517 | gup_flags, &vma, | |
518 | pages ? &pages[i] : NULL); | |
519 | if (ret) | |
520 | return i ? : ret; | |
521 | page_mask = 0; | |
522 | goto next_page; | |
523 | } | |
4bbd4c77 | 524 | |
fa5bb209 KS |
525 | if (!vma || check_vma_flags(vma, gup_flags)) |
526 | return i ? : -EFAULT; | |
527 | if (is_vm_hugetlb_page(vma)) { | |
528 | i = follow_hugetlb_page(mm, vma, pages, vmas, | |
529 | &start, &nr_pages, i, | |
530 | gup_flags); | |
531 | continue; | |
4bbd4c77 | 532 | } |
fa5bb209 KS |
533 | } |
534 | retry: | |
535 | /* | |
536 | * If we have a pending SIGKILL, don't keep faulting pages and | |
537 | * potentially allocating memory. | |
538 | */ | |
539 | if (unlikely(fatal_signal_pending(current))) | |
540 | return i ? i : -ERESTARTSYS; | |
541 | cond_resched(); | |
542 | page = follow_page_mask(vma, start, foll_flags, &page_mask); | |
543 | if (!page) { | |
544 | int ret; | |
545 | ret = faultin_page(tsk, vma, start, &foll_flags, | |
546 | nonblocking); | |
547 | switch (ret) { | |
548 | case 0: | |
549 | goto retry; | |
550 | case -EFAULT: | |
551 | case -ENOMEM: | |
552 | case -EHWPOISON: | |
553 | return i ? i : ret; | |
554 | case -EBUSY: | |
555 | return i; | |
556 | case -ENOENT: | |
557 | goto next_page; | |
4bbd4c77 | 558 | } |
fa5bb209 | 559 | BUG(); |
1027e443 KS |
560 | } else if (PTR_ERR(page) == -EEXIST) { |
561 | /* | |
562 | * Proper page table entry exists, but no corresponding | |
563 | * struct page. | |
564 | */ | |
565 | goto next_page; | |
566 | } else if (IS_ERR(page)) { | |
fa5bb209 | 567 | return i ? i : PTR_ERR(page); |
1027e443 | 568 | } |
fa5bb209 KS |
569 | if (pages) { |
570 | pages[i] = page; | |
571 | flush_anon_page(vma, page, start); | |
572 | flush_dcache_page(page); | |
573 | page_mask = 0; | |
4bbd4c77 | 574 | } |
4bbd4c77 | 575 | next_page: |
fa5bb209 KS |
576 | if (vmas) { |
577 | vmas[i] = vma; | |
578 | page_mask = 0; | |
579 | } | |
580 | page_increm = 1 + (~(start >> PAGE_SHIFT) & page_mask); | |
581 | if (page_increm > nr_pages) | |
582 | page_increm = nr_pages; | |
583 | i += page_increm; | |
584 | start += page_increm * PAGE_SIZE; | |
585 | nr_pages -= page_increm; | |
4bbd4c77 KS |
586 | } while (nr_pages); |
587 | return i; | |
4bbd4c77 KS |
588 | } |
589 | EXPORT_SYMBOL(__get_user_pages); | |
590 | ||
591 | /* | |
592 | * fixup_user_fault() - manually resolve a user page fault | |
593 | * @tsk: the task_struct to use for page fault accounting, or | |
594 | * NULL if faults are not to be recorded. | |
595 | * @mm: mm_struct of target mm | |
596 | * @address: user address | |
597 | * @fault_flags:flags to pass down to handle_mm_fault() | |
598 | * | |
599 | * This is meant to be called in the specific scenario where for locking reasons | |
600 | * we try to access user memory in atomic context (within a pagefault_disable() | |
601 | * section), this returns -EFAULT, and we want to resolve the user fault before | |
602 | * trying again. | |
603 | * | |
604 | * Typically this is meant to be used by the futex code. | |
605 | * | |
606 | * The main difference with get_user_pages() is that this function will | |
607 | * unconditionally call handle_mm_fault() which will in turn perform all the | |
608 | * necessary SW fixup of the dirty and young bits in the PTE, while | |
609 | * handle_mm_fault() only guarantees to update these in the struct page. | |
610 | * | |
611 | * This is important for some architectures where those bits also gate the | |
612 | * access permission to the page because they are maintained in software. On | |
613 | * such architectures, gup() will not be enough to make a subsequent access | |
614 | * succeed. | |
615 | * | |
9a95f3cf | 616 | * This has the same semantics wrt the @mm->mmap_sem as does filemap_fault(). |
4bbd4c77 KS |
617 | */ |
618 | int fixup_user_fault(struct task_struct *tsk, struct mm_struct *mm, | |
619 | unsigned long address, unsigned int fault_flags) | |
620 | { | |
621 | struct vm_area_struct *vma; | |
622 | vm_flags_t vm_flags; | |
623 | int ret; | |
624 | ||
625 | vma = find_extend_vma(mm, address); | |
626 | if (!vma || address < vma->vm_start) | |
627 | return -EFAULT; | |
628 | ||
629 | vm_flags = (fault_flags & FAULT_FLAG_WRITE) ? VM_WRITE : VM_READ; | |
630 | if (!(vm_flags & vma->vm_flags)) | |
631 | return -EFAULT; | |
632 | ||
633 | ret = handle_mm_fault(mm, vma, address, fault_flags); | |
634 | if (ret & VM_FAULT_ERROR) { | |
635 | if (ret & VM_FAULT_OOM) | |
636 | return -ENOMEM; | |
637 | if (ret & (VM_FAULT_HWPOISON | VM_FAULT_HWPOISON_LARGE)) | |
638 | return -EHWPOISON; | |
33692f27 | 639 | if (ret & (VM_FAULT_SIGBUS | VM_FAULT_SIGSEGV)) |
4bbd4c77 KS |
640 | return -EFAULT; |
641 | BUG(); | |
642 | } | |
643 | if (tsk) { | |
644 | if (ret & VM_FAULT_MAJOR) | |
645 | tsk->maj_flt++; | |
646 | else | |
647 | tsk->min_flt++; | |
648 | } | |
649 | return 0; | |
650 | } | |
651 | ||
f0818f47 AA |
652 | static __always_inline long __get_user_pages_locked(struct task_struct *tsk, |
653 | struct mm_struct *mm, | |
654 | unsigned long start, | |
655 | unsigned long nr_pages, | |
656 | int write, int force, | |
657 | struct page **pages, | |
658 | struct vm_area_struct **vmas, | |
0fd71a56 AA |
659 | int *locked, bool notify_drop, |
660 | unsigned int flags) | |
f0818f47 | 661 | { |
f0818f47 AA |
662 | long ret, pages_done; |
663 | bool lock_dropped; | |
664 | ||
665 | if (locked) { | |
666 | /* if VM_FAULT_RETRY can be returned, vmas become invalid */ | |
667 | BUG_ON(vmas); | |
668 | /* check caller initialized locked */ | |
669 | BUG_ON(*locked != 1); | |
670 | } | |
671 | ||
672 | if (pages) | |
673 | flags |= FOLL_GET; | |
674 | if (write) | |
675 | flags |= FOLL_WRITE; | |
676 | if (force) | |
677 | flags |= FOLL_FORCE; | |
678 | ||
679 | pages_done = 0; | |
680 | lock_dropped = false; | |
681 | for (;;) { | |
682 | ret = __get_user_pages(tsk, mm, start, nr_pages, flags, pages, | |
683 | vmas, locked); | |
684 | if (!locked) | |
685 | /* VM_FAULT_RETRY couldn't trigger, bypass */ | |
686 | return ret; | |
687 | ||
688 | /* VM_FAULT_RETRY cannot return errors */ | |
689 | if (!*locked) { | |
690 | BUG_ON(ret < 0); | |
691 | BUG_ON(ret >= nr_pages); | |
692 | } | |
693 | ||
694 | if (!pages) | |
695 | /* If it's a prefault don't insist harder */ | |
696 | return ret; | |
697 | ||
698 | if (ret > 0) { | |
699 | nr_pages -= ret; | |
700 | pages_done += ret; | |
701 | if (!nr_pages) | |
702 | break; | |
703 | } | |
704 | if (*locked) { | |
705 | /* VM_FAULT_RETRY didn't trigger */ | |
706 | if (!pages_done) | |
707 | pages_done = ret; | |
708 | break; | |
709 | } | |
710 | /* VM_FAULT_RETRY triggered, so seek to the faulting offset */ | |
711 | pages += ret; | |
712 | start += ret << PAGE_SHIFT; | |
713 | ||
714 | /* | |
715 | * Repeat on the address that fired VM_FAULT_RETRY | |
716 | * without FAULT_FLAG_ALLOW_RETRY but with | |
717 | * FAULT_FLAG_TRIED. | |
718 | */ | |
719 | *locked = 1; | |
720 | lock_dropped = true; | |
721 | down_read(&mm->mmap_sem); | |
722 | ret = __get_user_pages(tsk, mm, start, 1, flags | FOLL_TRIED, | |
723 | pages, NULL, NULL); | |
724 | if (ret != 1) { | |
725 | BUG_ON(ret > 1); | |
726 | if (!pages_done) | |
727 | pages_done = ret; | |
728 | break; | |
729 | } | |
730 | nr_pages--; | |
731 | pages_done++; | |
732 | if (!nr_pages) | |
733 | break; | |
734 | pages++; | |
735 | start += PAGE_SIZE; | |
736 | } | |
737 | if (notify_drop && lock_dropped && *locked) { | |
738 | /* | |
739 | * We must let the caller know we temporarily dropped the lock | |
740 | * and so the critical section protected by it was lost. | |
741 | */ | |
742 | up_read(&mm->mmap_sem); | |
743 | *locked = 0; | |
744 | } | |
745 | return pages_done; | |
746 | } | |
747 | ||
748 | /* | |
749 | * We can leverage the VM_FAULT_RETRY functionality in the page fault | |
750 | * paths better by using either get_user_pages_locked() or | |
751 | * get_user_pages_unlocked(). | |
752 | * | |
753 | * get_user_pages_locked() is suitable to replace the form: | |
754 | * | |
755 | * down_read(&mm->mmap_sem); | |
756 | * do_something() | |
757 | * get_user_pages(tsk, mm, ..., pages, NULL); | |
758 | * up_read(&mm->mmap_sem); | |
759 | * | |
760 | * to: | |
761 | * | |
762 | * int locked = 1; | |
763 | * down_read(&mm->mmap_sem); | |
764 | * do_something() | |
765 | * get_user_pages_locked(tsk, mm, ..., pages, &locked); | |
766 | * if (locked) | |
767 | * up_read(&mm->mmap_sem); | |
768 | */ | |
769 | long get_user_pages_locked(struct task_struct *tsk, struct mm_struct *mm, | |
770 | unsigned long start, unsigned long nr_pages, | |
771 | int write, int force, struct page **pages, | |
772 | int *locked) | |
773 | { | |
774 | return __get_user_pages_locked(tsk, mm, start, nr_pages, write, force, | |
0fd71a56 | 775 | pages, NULL, locked, true, FOLL_TOUCH); |
f0818f47 AA |
776 | } |
777 | EXPORT_SYMBOL(get_user_pages_locked); | |
778 | ||
0fd71a56 AA |
779 | /* |
780 | * Same as get_user_pages_unlocked(...., FOLL_TOUCH) but it allows to | |
781 | * pass additional gup_flags as last parameter (like FOLL_HWPOISON). | |
782 | * | |
783 | * NOTE: here FOLL_TOUCH is not set implicitly and must be set by the | |
784 | * caller if required (just like with __get_user_pages). "FOLL_GET", | |
785 | * "FOLL_WRITE" and "FOLL_FORCE" are set implicitly as needed | |
786 | * according to the parameters "pages", "write", "force" | |
787 | * respectively. | |
788 | */ | |
789 | __always_inline long __get_user_pages_unlocked(struct task_struct *tsk, struct mm_struct *mm, | |
790 | unsigned long start, unsigned long nr_pages, | |
791 | int write, int force, struct page **pages, | |
792 | unsigned int gup_flags) | |
793 | { | |
794 | long ret; | |
795 | int locked = 1; | |
796 | down_read(&mm->mmap_sem); | |
797 | ret = __get_user_pages_locked(tsk, mm, start, nr_pages, write, force, | |
798 | pages, NULL, &locked, false, gup_flags); | |
799 | if (locked) | |
800 | up_read(&mm->mmap_sem); | |
801 | return ret; | |
802 | } | |
803 | EXPORT_SYMBOL(__get_user_pages_unlocked); | |
804 | ||
f0818f47 AA |
805 | /* |
806 | * get_user_pages_unlocked() is suitable to replace the form: | |
807 | * | |
808 | * down_read(&mm->mmap_sem); | |
809 | * get_user_pages(tsk, mm, ..., pages, NULL); | |
810 | * up_read(&mm->mmap_sem); | |
811 | * | |
812 | * with: | |
813 | * | |
814 | * get_user_pages_unlocked(tsk, mm, ..., pages); | |
815 | * | |
816 | * It is functionally equivalent to get_user_pages_fast so | |
817 | * get_user_pages_fast should be used instead, if the two parameters | |
818 | * "tsk" and "mm" are respectively equal to current and current->mm, | |
819 | * or if "force" shall be set to 1 (get_user_pages_fast misses the | |
820 | * "force" parameter). | |
821 | */ | |
822 | long get_user_pages_unlocked(struct task_struct *tsk, struct mm_struct *mm, | |
823 | unsigned long start, unsigned long nr_pages, | |
824 | int write, int force, struct page **pages) | |
825 | { | |
0fd71a56 AA |
826 | return __get_user_pages_unlocked(tsk, mm, start, nr_pages, write, |
827 | force, pages, FOLL_TOUCH); | |
f0818f47 AA |
828 | } |
829 | EXPORT_SYMBOL(get_user_pages_unlocked); | |
830 | ||
4bbd4c77 KS |
831 | /* |
832 | * get_user_pages() - pin user pages in memory | |
833 | * @tsk: the task_struct to use for page fault accounting, or | |
834 | * NULL if faults are not to be recorded. | |
835 | * @mm: mm_struct of target mm | |
836 | * @start: starting user address | |
837 | * @nr_pages: number of pages from start to pin | |
838 | * @write: whether pages will be written to by the caller | |
839 | * @force: whether to force access even when user mapping is currently | |
840 | * protected (but never forces write access to shared mapping). | |
841 | * @pages: array that receives pointers to the pages pinned. | |
842 | * Should be at least nr_pages long. Or NULL, if caller | |
843 | * only intends to ensure the pages are faulted in. | |
844 | * @vmas: array of pointers to vmas corresponding to each page. | |
845 | * Or NULL if the caller does not require them. | |
846 | * | |
847 | * Returns number of pages pinned. This may be fewer than the number | |
848 | * requested. If nr_pages is 0 or negative, returns 0. If no pages | |
849 | * were pinned, returns -errno. Each page returned must be released | |
850 | * with a put_page() call when it is finished with. vmas will only | |
851 | * remain valid while mmap_sem is held. | |
852 | * | |
853 | * Must be called with mmap_sem held for read or write. | |
854 | * | |
855 | * get_user_pages walks a process's page tables and takes a reference to | |
856 | * each struct page that each user address corresponds to at a given | |
857 | * instant. That is, it takes the page that would be accessed if a user | |
858 | * thread accesses the given user virtual address at that instant. | |
859 | * | |
860 | * This does not guarantee that the page exists in the user mappings when | |
861 | * get_user_pages returns, and there may even be a completely different | |
862 | * page there in some cases (eg. if mmapped pagecache has been invalidated | |
863 | * and subsequently re faulted). However it does guarantee that the page | |
864 | * won't be freed completely. And mostly callers simply care that the page | |
865 | * contains data that was valid *at some point in time*. Typically, an IO | |
866 | * or similar operation cannot guarantee anything stronger anyway because | |
867 | * locks can't be held over the syscall boundary. | |
868 | * | |
869 | * If write=0, the page must not be written to. If the page is written to, | |
870 | * set_page_dirty (or set_page_dirty_lock, as appropriate) must be called | |
871 | * after the page is finished with, and before put_page is called. | |
872 | * | |
873 | * get_user_pages is typically used for fewer-copy IO operations, to get a | |
874 | * handle on the memory by some means other than accesses via the user virtual | |
875 | * addresses. The pages may be submitted for DMA to devices or accessed via | |
876 | * their kernel linear mapping (via the kmap APIs). Care should be taken to | |
877 | * use the correct cache flushing APIs. | |
878 | * | |
879 | * See also get_user_pages_fast, for performance critical applications. | |
f0818f47 AA |
880 | * |
881 | * get_user_pages should be phased out in favor of | |
882 | * get_user_pages_locked|unlocked or get_user_pages_fast. Nothing | |
883 | * should use get_user_pages because it cannot pass | |
884 | * FAULT_FLAG_ALLOW_RETRY to handle_mm_fault. | |
4bbd4c77 KS |
885 | */ |
886 | long get_user_pages(struct task_struct *tsk, struct mm_struct *mm, | |
887 | unsigned long start, unsigned long nr_pages, int write, | |
888 | int force, struct page **pages, struct vm_area_struct **vmas) | |
889 | { | |
f0818f47 | 890 | return __get_user_pages_locked(tsk, mm, start, nr_pages, write, force, |
0fd71a56 | 891 | pages, vmas, NULL, false, FOLL_TOUCH); |
4bbd4c77 KS |
892 | } |
893 | EXPORT_SYMBOL(get_user_pages); | |
894 | ||
acc3c8d1 KS |
895 | /** |
896 | * populate_vma_page_range() - populate a range of pages in the vma. | |
897 | * @vma: target vma | |
898 | * @start: start address | |
899 | * @end: end address | |
900 | * @nonblocking: | |
901 | * | |
902 | * This takes care of mlocking the pages too if VM_LOCKED is set. | |
903 | * | |
904 | * return 0 on success, negative error code on error. | |
905 | * | |
906 | * vma->vm_mm->mmap_sem must be held. | |
907 | * | |
908 | * If @nonblocking is NULL, it may be held for read or write and will | |
909 | * be unperturbed. | |
910 | * | |
911 | * If @nonblocking is non-NULL, it must held for read only and may be | |
912 | * released. If it's released, *@nonblocking will be set to 0. | |
913 | */ | |
914 | long populate_vma_page_range(struct vm_area_struct *vma, | |
915 | unsigned long start, unsigned long end, int *nonblocking) | |
916 | { | |
917 | struct mm_struct *mm = vma->vm_mm; | |
918 | unsigned long nr_pages = (end - start) / PAGE_SIZE; | |
919 | int gup_flags; | |
920 | ||
921 | VM_BUG_ON(start & ~PAGE_MASK); | |
922 | VM_BUG_ON(end & ~PAGE_MASK); | |
923 | VM_BUG_ON_VMA(start < vma->vm_start, vma); | |
924 | VM_BUG_ON_VMA(end > vma->vm_end, vma); | |
925 | VM_BUG_ON_MM(!rwsem_is_locked(&mm->mmap_sem), mm); | |
926 | ||
de60f5f1 EM |
927 | gup_flags = FOLL_TOUCH | FOLL_POPULATE | FOLL_MLOCK; |
928 | if (vma->vm_flags & VM_LOCKONFAULT) | |
929 | gup_flags &= ~FOLL_POPULATE; | |
930 | ||
acc3c8d1 KS |
931 | /* |
932 | * We want to touch writable mappings with a write fault in order | |
933 | * to break COW, except for shared mappings because these don't COW | |
934 | * and we would not want to dirty them for nothing. | |
935 | */ | |
936 | if ((vma->vm_flags & (VM_WRITE | VM_SHARED)) == VM_WRITE) | |
937 | gup_flags |= FOLL_WRITE; | |
938 | ||
939 | /* | |
940 | * We want mlock to succeed for regions that have any permissions | |
941 | * other than PROT_NONE. | |
942 | */ | |
943 | if (vma->vm_flags & (VM_READ | VM_WRITE | VM_EXEC)) | |
944 | gup_flags |= FOLL_FORCE; | |
945 | ||
946 | /* | |
947 | * We made sure addr is within a VMA, so the following will | |
948 | * not result in a stack expansion that recurses back here. | |
949 | */ | |
950 | return __get_user_pages(current, mm, start, nr_pages, gup_flags, | |
951 | NULL, NULL, nonblocking); | |
952 | } | |
953 | ||
954 | /* | |
955 | * __mm_populate - populate and/or mlock pages within a range of address space. | |
956 | * | |
957 | * This is used to implement mlock() and the MAP_POPULATE / MAP_LOCKED mmap | |
958 | * flags. VMAs must be already marked with the desired vm_flags, and | |
959 | * mmap_sem must not be held. | |
960 | */ | |
961 | int __mm_populate(unsigned long start, unsigned long len, int ignore_errors) | |
962 | { | |
963 | struct mm_struct *mm = current->mm; | |
964 | unsigned long end, nstart, nend; | |
965 | struct vm_area_struct *vma = NULL; | |
966 | int locked = 0; | |
967 | long ret = 0; | |
968 | ||
969 | VM_BUG_ON(start & ~PAGE_MASK); | |
970 | VM_BUG_ON(len != PAGE_ALIGN(len)); | |
971 | end = start + len; | |
972 | ||
973 | for (nstart = start; nstart < end; nstart = nend) { | |
974 | /* | |
975 | * We want to fault in pages for [nstart; end) address range. | |
976 | * Find first corresponding VMA. | |
977 | */ | |
978 | if (!locked) { | |
979 | locked = 1; | |
980 | down_read(&mm->mmap_sem); | |
981 | vma = find_vma(mm, nstart); | |
982 | } else if (nstart >= vma->vm_end) | |
983 | vma = vma->vm_next; | |
984 | if (!vma || vma->vm_start >= end) | |
985 | break; | |
986 | /* | |
987 | * Set [nstart; nend) to intersection of desired address | |
988 | * range with the first VMA. Also, skip undesirable VMA types. | |
989 | */ | |
990 | nend = min(end, vma->vm_end); | |
991 | if (vma->vm_flags & (VM_IO | VM_PFNMAP)) | |
992 | continue; | |
993 | if (nstart < vma->vm_start) | |
994 | nstart = vma->vm_start; | |
995 | /* | |
996 | * Now fault in a range of pages. populate_vma_page_range() | |
997 | * double checks the vma flags, so that it won't mlock pages | |
998 | * if the vma was already munlocked. | |
999 | */ | |
1000 | ret = populate_vma_page_range(vma, nstart, nend, &locked); | |
1001 | if (ret < 0) { | |
1002 | if (ignore_errors) { | |
1003 | ret = 0; | |
1004 | continue; /* continue at next VMA */ | |
1005 | } | |
1006 | break; | |
1007 | } | |
1008 | nend = nstart + ret * PAGE_SIZE; | |
1009 | ret = 0; | |
1010 | } | |
1011 | if (locked) | |
1012 | up_read(&mm->mmap_sem); | |
1013 | return ret; /* 0 or negative error code */ | |
1014 | } | |
1015 | ||
4bbd4c77 KS |
1016 | /** |
1017 | * get_dump_page() - pin user page in memory while writing it to core dump | |
1018 | * @addr: user address | |
1019 | * | |
1020 | * Returns struct page pointer of user page pinned for dump, | |
1021 | * to be freed afterwards by page_cache_release() or put_page(). | |
1022 | * | |
1023 | * Returns NULL on any kind of failure - a hole must then be inserted into | |
1024 | * the corefile, to preserve alignment with its headers; and also returns | |
1025 | * NULL wherever the ZERO_PAGE, or an anonymous pte_none, has been found - | |
1026 | * allowing a hole to be left in the corefile to save diskspace. | |
1027 | * | |
1028 | * Called without mmap_sem, but after all other threads have been killed. | |
1029 | */ | |
1030 | #ifdef CONFIG_ELF_CORE | |
1031 | struct page *get_dump_page(unsigned long addr) | |
1032 | { | |
1033 | struct vm_area_struct *vma; | |
1034 | struct page *page; | |
1035 | ||
1036 | if (__get_user_pages(current, current->mm, addr, 1, | |
1037 | FOLL_FORCE | FOLL_DUMP | FOLL_GET, &page, &vma, | |
1038 | NULL) < 1) | |
1039 | return NULL; | |
1040 | flush_cache_page(vma, addr, page_to_pfn(page)); | |
1041 | return page; | |
1042 | } | |
1043 | #endif /* CONFIG_ELF_CORE */ | |
2667f50e SC |
1044 | |
1045 | /* | |
1046 | * Generic RCU Fast GUP | |
1047 | * | |
1048 | * get_user_pages_fast attempts to pin user pages by walking the page | |
1049 | * tables directly and avoids taking locks. Thus the walker needs to be | |
1050 | * protected from page table pages being freed from under it, and should | |
1051 | * block any THP splits. | |
1052 | * | |
1053 | * One way to achieve this is to have the walker disable interrupts, and | |
1054 | * rely on IPIs from the TLB flushing code blocking before the page table | |
1055 | * pages are freed. This is unsuitable for architectures that do not need | |
1056 | * to broadcast an IPI when invalidating TLBs. | |
1057 | * | |
1058 | * Another way to achieve this is to batch up page table containing pages | |
1059 | * belonging to more than one mm_user, then rcu_sched a callback to free those | |
1060 | * pages. Disabling interrupts will allow the fast_gup walker to both block | |
1061 | * the rcu_sched callback, and an IPI that we broadcast for splitting THPs | |
1062 | * (which is a relatively rare event). The code below adopts this strategy. | |
1063 | * | |
1064 | * Before activating this code, please be aware that the following assumptions | |
1065 | * are currently made: | |
1066 | * | |
1067 | * *) HAVE_RCU_TABLE_FREE is enabled, and tlb_remove_table is used to free | |
1068 | * pages containing page tables. | |
1069 | * | |
1070 | * *) THP splits will broadcast an IPI, this can be achieved by overriding | |
1071 | * pmdp_splitting_flush. | |
1072 | * | |
1073 | * *) ptes can be read atomically by the architecture. | |
1074 | * | |
1075 | * *) access_ok is sufficient to validate userspace address ranges. | |
1076 | * | |
1077 | * The last two assumptions can be relaxed by the addition of helper functions. | |
1078 | * | |
1079 | * This code is based heavily on the PowerPC implementation by Nick Piggin. | |
1080 | */ | |
1081 | #ifdef CONFIG_HAVE_GENERIC_RCU_GUP | |
1082 | ||
1083 | #ifdef __HAVE_ARCH_PTE_SPECIAL | |
1084 | static int gup_pte_range(pmd_t pmd, unsigned long addr, unsigned long end, | |
1085 | int write, struct page **pages, int *nr) | |
1086 | { | |
1087 | pte_t *ptep, *ptem; | |
1088 | int ret = 0; | |
1089 | ||
1090 | ptem = ptep = pte_offset_map(&pmd, addr); | |
1091 | do { | |
1092 | /* | |
1093 | * In the line below we are assuming that the pte can be read | |
1094 | * atomically. If this is not the case for your architecture, | |
1095 | * please wrap this in a helper function! | |
1096 | * | |
1097 | * for an example see gup_get_pte in arch/x86/mm/gup.c | |
1098 | */ | |
9d8c47e4 | 1099 | pte_t pte = READ_ONCE(*ptep); |
7aef4172 | 1100 | struct page *head, *page; |
2667f50e SC |
1101 | |
1102 | /* | |
1103 | * Similar to the PMD case below, NUMA hinting must take slow | |
8a0516ed | 1104 | * path using the pte_protnone check. |
2667f50e SC |
1105 | */ |
1106 | if (!pte_present(pte) || pte_special(pte) || | |
8a0516ed | 1107 | pte_protnone(pte) || (write && !pte_write(pte))) |
2667f50e SC |
1108 | goto pte_unmap; |
1109 | ||
1110 | VM_BUG_ON(!pfn_valid(pte_pfn(pte))); | |
1111 | page = pte_page(pte); | |
7aef4172 | 1112 | head = compound_head(page); |
2667f50e | 1113 | |
7aef4172 | 1114 | if (!page_cache_get_speculative(head)) |
2667f50e SC |
1115 | goto pte_unmap; |
1116 | ||
1117 | if (unlikely(pte_val(pte) != pte_val(*ptep))) { | |
7aef4172 | 1118 | put_page(head); |
2667f50e SC |
1119 | goto pte_unmap; |
1120 | } | |
1121 | ||
7aef4172 | 1122 | VM_BUG_ON_PAGE(compound_head(page) != head, page); |
2667f50e SC |
1123 | pages[*nr] = page; |
1124 | (*nr)++; | |
1125 | ||
1126 | } while (ptep++, addr += PAGE_SIZE, addr != end); | |
1127 | ||
1128 | ret = 1; | |
1129 | ||
1130 | pte_unmap: | |
1131 | pte_unmap(ptem); | |
1132 | return ret; | |
1133 | } | |
1134 | #else | |
1135 | ||
1136 | /* | |
1137 | * If we can't determine whether or not a pte is special, then fail immediately | |
1138 | * for ptes. Note, we can still pin HugeTLB and THP as these are guaranteed not | |
1139 | * to be special. | |
1140 | * | |
1141 | * For a futex to be placed on a THP tail page, get_futex_key requires a | |
1142 | * __get_user_pages_fast implementation that can pin pages. Thus it's still | |
1143 | * useful to have gup_huge_pmd even if we can't operate on ptes. | |
1144 | */ | |
1145 | static int gup_pte_range(pmd_t pmd, unsigned long addr, unsigned long end, | |
1146 | int write, struct page **pages, int *nr) | |
1147 | { | |
1148 | return 0; | |
1149 | } | |
1150 | #endif /* __HAVE_ARCH_PTE_SPECIAL */ | |
1151 | ||
1152 | static int gup_huge_pmd(pmd_t orig, pmd_t *pmdp, unsigned long addr, | |
1153 | unsigned long end, int write, struct page **pages, int *nr) | |
1154 | { | |
1155 | struct page *head, *page, *tail; | |
1156 | int refs; | |
1157 | ||
1158 | if (write && !pmd_write(orig)) | |
1159 | return 0; | |
1160 | ||
1161 | refs = 0; | |
1162 | head = pmd_page(orig); | |
1163 | page = head + ((addr & ~PMD_MASK) >> PAGE_SHIFT); | |
1164 | tail = page; | |
1165 | do { | |
1166 | VM_BUG_ON_PAGE(compound_head(page) != head, page); | |
1167 | pages[*nr] = page; | |
1168 | (*nr)++; | |
1169 | page++; | |
1170 | refs++; | |
1171 | } while (addr += PAGE_SIZE, addr != end); | |
1172 | ||
1173 | if (!page_cache_add_speculative(head, refs)) { | |
1174 | *nr -= refs; | |
1175 | return 0; | |
1176 | } | |
1177 | ||
1178 | if (unlikely(pmd_val(orig) != pmd_val(*pmdp))) { | |
1179 | *nr -= refs; | |
1180 | while (refs--) | |
1181 | put_page(head); | |
1182 | return 0; | |
1183 | } | |
1184 | ||
1185 | /* | |
1186 | * Any tail pages need their mapcount reference taken before we | |
1187 | * return. (This allows the THP code to bump their ref count when | |
1188 | * they are split into base pages). | |
1189 | */ | |
1190 | while (refs--) { | |
1191 | if (PageTail(tail)) | |
1192 | get_huge_page_tail(tail); | |
1193 | tail++; | |
1194 | } | |
1195 | ||
1196 | return 1; | |
1197 | } | |
1198 | ||
1199 | static int gup_huge_pud(pud_t orig, pud_t *pudp, unsigned long addr, | |
1200 | unsigned long end, int write, struct page **pages, int *nr) | |
1201 | { | |
1202 | struct page *head, *page, *tail; | |
1203 | int refs; | |
1204 | ||
1205 | if (write && !pud_write(orig)) | |
1206 | return 0; | |
1207 | ||
1208 | refs = 0; | |
1209 | head = pud_page(orig); | |
1210 | page = head + ((addr & ~PUD_MASK) >> PAGE_SHIFT); | |
1211 | tail = page; | |
1212 | do { | |
1213 | VM_BUG_ON_PAGE(compound_head(page) != head, page); | |
1214 | pages[*nr] = page; | |
1215 | (*nr)++; | |
1216 | page++; | |
1217 | refs++; | |
1218 | } while (addr += PAGE_SIZE, addr != end); | |
1219 | ||
1220 | if (!page_cache_add_speculative(head, refs)) { | |
1221 | *nr -= refs; | |
1222 | return 0; | |
1223 | } | |
1224 | ||
1225 | if (unlikely(pud_val(orig) != pud_val(*pudp))) { | |
1226 | *nr -= refs; | |
1227 | while (refs--) | |
1228 | put_page(head); | |
1229 | return 0; | |
1230 | } | |
1231 | ||
1232 | while (refs--) { | |
1233 | if (PageTail(tail)) | |
1234 | get_huge_page_tail(tail); | |
1235 | tail++; | |
1236 | } | |
1237 | ||
1238 | return 1; | |
1239 | } | |
1240 | ||
f30c59e9 AK |
1241 | static int gup_huge_pgd(pgd_t orig, pgd_t *pgdp, unsigned long addr, |
1242 | unsigned long end, int write, | |
1243 | struct page **pages, int *nr) | |
1244 | { | |
1245 | int refs; | |
1246 | struct page *head, *page, *tail; | |
1247 | ||
1248 | if (write && !pgd_write(orig)) | |
1249 | return 0; | |
1250 | ||
1251 | refs = 0; | |
1252 | head = pgd_page(orig); | |
1253 | page = head + ((addr & ~PGDIR_MASK) >> PAGE_SHIFT); | |
1254 | tail = page; | |
1255 | do { | |
1256 | VM_BUG_ON_PAGE(compound_head(page) != head, page); | |
1257 | pages[*nr] = page; | |
1258 | (*nr)++; | |
1259 | page++; | |
1260 | refs++; | |
1261 | } while (addr += PAGE_SIZE, addr != end); | |
1262 | ||
1263 | if (!page_cache_add_speculative(head, refs)) { | |
1264 | *nr -= refs; | |
1265 | return 0; | |
1266 | } | |
1267 | ||
1268 | if (unlikely(pgd_val(orig) != pgd_val(*pgdp))) { | |
1269 | *nr -= refs; | |
1270 | while (refs--) | |
1271 | put_page(head); | |
1272 | return 0; | |
1273 | } | |
1274 | ||
1275 | while (refs--) { | |
1276 | if (PageTail(tail)) | |
1277 | get_huge_page_tail(tail); | |
1278 | tail++; | |
1279 | } | |
1280 | ||
1281 | return 1; | |
1282 | } | |
1283 | ||
2667f50e SC |
1284 | static int gup_pmd_range(pud_t pud, unsigned long addr, unsigned long end, |
1285 | int write, struct page **pages, int *nr) | |
1286 | { | |
1287 | unsigned long next; | |
1288 | pmd_t *pmdp; | |
1289 | ||
1290 | pmdp = pmd_offset(&pud, addr); | |
1291 | do { | |
38c5ce93 | 1292 | pmd_t pmd = READ_ONCE(*pmdp); |
2667f50e SC |
1293 | |
1294 | next = pmd_addr_end(addr, end); | |
1295 | if (pmd_none(pmd) || pmd_trans_splitting(pmd)) | |
1296 | return 0; | |
1297 | ||
1298 | if (unlikely(pmd_trans_huge(pmd) || pmd_huge(pmd))) { | |
1299 | /* | |
1300 | * NUMA hinting faults need to be handled in the GUP | |
1301 | * slowpath for accounting purposes and so that they | |
1302 | * can be serialised against THP migration. | |
1303 | */ | |
8a0516ed | 1304 | if (pmd_protnone(pmd)) |
2667f50e SC |
1305 | return 0; |
1306 | ||
1307 | if (!gup_huge_pmd(pmd, pmdp, addr, next, write, | |
1308 | pages, nr)) | |
1309 | return 0; | |
1310 | ||
f30c59e9 AK |
1311 | } else if (unlikely(is_hugepd(__hugepd(pmd_val(pmd))))) { |
1312 | /* | |
1313 | * architecture have different format for hugetlbfs | |
1314 | * pmd format and THP pmd format | |
1315 | */ | |
1316 | if (!gup_huge_pd(__hugepd(pmd_val(pmd)), addr, | |
1317 | PMD_SHIFT, next, write, pages, nr)) | |
1318 | return 0; | |
2667f50e SC |
1319 | } else if (!gup_pte_range(pmd, addr, next, write, pages, nr)) |
1320 | return 0; | |
1321 | } while (pmdp++, addr = next, addr != end); | |
1322 | ||
1323 | return 1; | |
1324 | } | |
1325 | ||
f30c59e9 AK |
1326 | static int gup_pud_range(pgd_t pgd, unsigned long addr, unsigned long end, |
1327 | int write, struct page **pages, int *nr) | |
2667f50e SC |
1328 | { |
1329 | unsigned long next; | |
1330 | pud_t *pudp; | |
1331 | ||
f30c59e9 | 1332 | pudp = pud_offset(&pgd, addr); |
2667f50e | 1333 | do { |
e37c6982 | 1334 | pud_t pud = READ_ONCE(*pudp); |
2667f50e SC |
1335 | |
1336 | next = pud_addr_end(addr, end); | |
1337 | if (pud_none(pud)) | |
1338 | return 0; | |
f30c59e9 | 1339 | if (unlikely(pud_huge(pud))) { |
2667f50e | 1340 | if (!gup_huge_pud(pud, pudp, addr, next, write, |
f30c59e9 AK |
1341 | pages, nr)) |
1342 | return 0; | |
1343 | } else if (unlikely(is_hugepd(__hugepd(pud_val(pud))))) { | |
1344 | if (!gup_huge_pd(__hugepd(pud_val(pud)), addr, | |
1345 | PUD_SHIFT, next, write, pages, nr)) | |
2667f50e SC |
1346 | return 0; |
1347 | } else if (!gup_pmd_range(pud, addr, next, write, pages, nr)) | |
1348 | return 0; | |
1349 | } while (pudp++, addr = next, addr != end); | |
1350 | ||
1351 | return 1; | |
1352 | } | |
1353 | ||
1354 | /* | |
1355 | * Like get_user_pages_fast() except it's IRQ-safe in that it won't fall back to | |
1356 | * the regular GUP. It will only return non-negative values. | |
1357 | */ | |
1358 | int __get_user_pages_fast(unsigned long start, int nr_pages, int write, | |
1359 | struct page **pages) | |
1360 | { | |
1361 | struct mm_struct *mm = current->mm; | |
1362 | unsigned long addr, len, end; | |
1363 | unsigned long next, flags; | |
1364 | pgd_t *pgdp; | |
1365 | int nr = 0; | |
1366 | ||
1367 | start &= PAGE_MASK; | |
1368 | addr = start; | |
1369 | len = (unsigned long) nr_pages << PAGE_SHIFT; | |
1370 | end = start + len; | |
1371 | ||
1372 | if (unlikely(!access_ok(write ? VERIFY_WRITE : VERIFY_READ, | |
1373 | start, len))) | |
1374 | return 0; | |
1375 | ||
1376 | /* | |
1377 | * Disable interrupts. We use the nested form as we can already have | |
1378 | * interrupts disabled by get_futex_key. | |
1379 | * | |
1380 | * With interrupts disabled, we block page table pages from being | |
1381 | * freed from under us. See mmu_gather_tlb in asm-generic/tlb.h | |
1382 | * for more details. | |
1383 | * | |
1384 | * We do not adopt an rcu_read_lock(.) here as we also want to | |
1385 | * block IPIs that come from THPs splitting. | |
1386 | */ | |
1387 | ||
1388 | local_irq_save(flags); | |
1389 | pgdp = pgd_offset(mm, addr); | |
1390 | do { | |
9d8c47e4 | 1391 | pgd_t pgd = READ_ONCE(*pgdp); |
f30c59e9 | 1392 | |
2667f50e | 1393 | next = pgd_addr_end(addr, end); |
f30c59e9 | 1394 | if (pgd_none(pgd)) |
2667f50e | 1395 | break; |
f30c59e9 AK |
1396 | if (unlikely(pgd_huge(pgd))) { |
1397 | if (!gup_huge_pgd(pgd, pgdp, addr, next, write, | |
1398 | pages, &nr)) | |
1399 | break; | |
1400 | } else if (unlikely(is_hugepd(__hugepd(pgd_val(pgd))))) { | |
1401 | if (!gup_huge_pd(__hugepd(pgd_val(pgd)), addr, | |
1402 | PGDIR_SHIFT, next, write, pages, &nr)) | |
1403 | break; | |
1404 | } else if (!gup_pud_range(pgd, addr, next, write, pages, &nr)) | |
2667f50e SC |
1405 | break; |
1406 | } while (pgdp++, addr = next, addr != end); | |
1407 | local_irq_restore(flags); | |
1408 | ||
1409 | return nr; | |
1410 | } | |
1411 | ||
1412 | /** | |
1413 | * get_user_pages_fast() - pin user pages in memory | |
1414 | * @start: starting user address | |
1415 | * @nr_pages: number of pages from start to pin | |
1416 | * @write: whether pages will be written to | |
1417 | * @pages: array that receives pointers to the pages pinned. | |
1418 | * Should be at least nr_pages long. | |
1419 | * | |
1420 | * Attempt to pin user pages in memory without taking mm->mmap_sem. | |
1421 | * If not successful, it will fall back to taking the lock and | |
1422 | * calling get_user_pages(). | |
1423 | * | |
1424 | * Returns number of pages pinned. This may be fewer than the number | |
1425 | * requested. If nr_pages is 0 or negative, returns 0. If no pages | |
1426 | * were pinned, returns -errno. | |
1427 | */ | |
1428 | int get_user_pages_fast(unsigned long start, int nr_pages, int write, | |
1429 | struct page **pages) | |
1430 | { | |
1431 | struct mm_struct *mm = current->mm; | |
1432 | int nr, ret; | |
1433 | ||
1434 | start &= PAGE_MASK; | |
1435 | nr = __get_user_pages_fast(start, nr_pages, write, pages); | |
1436 | ret = nr; | |
1437 | ||
1438 | if (nr < nr_pages) { | |
1439 | /* Try to get the remaining pages with get_user_pages */ | |
1440 | start += nr << PAGE_SHIFT; | |
1441 | pages += nr; | |
1442 | ||
a7b78075 AA |
1443 | ret = get_user_pages_unlocked(current, mm, start, |
1444 | nr_pages - nr, write, 0, pages); | |
2667f50e SC |
1445 | |
1446 | /* Have to be a bit careful with return values */ | |
1447 | if (nr > 0) { | |
1448 | if (ret < 0) | |
1449 | ret = nr; | |
1450 | else | |
1451 | ret += nr; | |
1452 | } | |
1453 | } | |
1454 | ||
1455 | return ret; | |
1456 | } | |
1457 | ||
1458 | #endif /* CONFIG_HAVE_GENERIC_RCU_GUP */ |