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 | ||
6 | #include <linux/hugetlb.h> | |
7 | #include <linux/mm.h> | |
8 | #include <linux/pagemap.h> | |
9 | #include <linux/rmap.h> | |
10 | #include <linux/swap.h> | |
11 | #include <linux/swapops.h> | |
12 | ||
13 | #include "internal.h" | |
14 | ||
69e68b4f KS |
15 | static struct page *no_page_table(struct vm_area_struct *vma, |
16 | unsigned int flags) | |
4bbd4c77 | 17 | { |
69e68b4f KS |
18 | /* |
19 | * When core dumping an enormous anonymous area that nobody | |
20 | * has touched so far, we don't want to allocate unnecessary pages or | |
21 | * page tables. Return error instead of NULL to skip handle_mm_fault, | |
22 | * then get_dump_page() will return NULL to leave a hole in the dump. | |
23 | * But we can only make this optimization where a hole would surely | |
24 | * be zero-filled if handle_mm_fault() actually did handle it. | |
25 | */ | |
26 | if ((flags & FOLL_DUMP) && (!vma->vm_ops || !vma->vm_ops->fault)) | |
27 | return ERR_PTR(-EFAULT); | |
28 | return NULL; | |
29 | } | |
4bbd4c77 | 30 | |
69e68b4f KS |
31 | static struct page *follow_page_pte(struct vm_area_struct *vma, |
32 | unsigned long address, pmd_t *pmd, unsigned int flags) | |
33 | { | |
34 | struct mm_struct *mm = vma->vm_mm; | |
35 | struct page *page; | |
36 | spinlock_t *ptl; | |
37 | pte_t *ptep, pte; | |
4bbd4c77 | 38 | |
69e68b4f | 39 | retry: |
4bbd4c77 | 40 | if (unlikely(pmd_bad(*pmd))) |
69e68b4f | 41 | return no_page_table(vma, flags); |
4bbd4c77 KS |
42 | |
43 | ptep = pte_offset_map_lock(mm, pmd, address, &ptl); | |
4bbd4c77 KS |
44 | pte = *ptep; |
45 | if (!pte_present(pte)) { | |
46 | swp_entry_t entry; | |
47 | /* | |
48 | * KSM's break_ksm() relies upon recognizing a ksm page | |
49 | * even while it is being migrated, so for that case we | |
50 | * need migration_entry_wait(). | |
51 | */ | |
52 | if (likely(!(flags & FOLL_MIGRATION))) | |
53 | goto no_page; | |
54 | if (pte_none(pte) || pte_file(pte)) | |
55 | goto no_page; | |
56 | entry = pte_to_swp_entry(pte); | |
57 | if (!is_migration_entry(entry)) | |
58 | goto no_page; | |
59 | pte_unmap_unlock(ptep, ptl); | |
60 | migration_entry_wait(mm, pmd, address); | |
69e68b4f | 61 | goto retry; |
4bbd4c77 KS |
62 | } |
63 | if ((flags & FOLL_NUMA) && pte_numa(pte)) | |
64 | goto no_page; | |
69e68b4f KS |
65 | if ((flags & FOLL_WRITE) && !pte_write(pte)) { |
66 | pte_unmap_unlock(ptep, ptl); | |
67 | return NULL; | |
68 | } | |
4bbd4c77 KS |
69 | |
70 | page = vm_normal_page(vma, address, pte); | |
71 | if (unlikely(!page)) { | |
72 | if ((flags & FOLL_DUMP) || | |
73 | !is_zero_pfn(pte_pfn(pte))) | |
74 | goto bad_page; | |
75 | page = pte_page(pte); | |
76 | } | |
77 | ||
78 | if (flags & FOLL_GET) | |
79 | get_page_foll(page); | |
80 | if (flags & FOLL_TOUCH) { | |
81 | if ((flags & FOLL_WRITE) && | |
82 | !pte_dirty(pte) && !PageDirty(page)) | |
83 | set_page_dirty(page); | |
84 | /* | |
85 | * pte_mkyoung() would be more correct here, but atomic care | |
86 | * is needed to avoid losing the dirty bit: it is easier to use | |
87 | * mark_page_accessed(). | |
88 | */ | |
89 | mark_page_accessed(page); | |
90 | } | |
91 | if ((flags & FOLL_MLOCK) && (vma->vm_flags & VM_LOCKED)) { | |
92 | /* | |
93 | * The preliminary mapping check is mainly to avoid the | |
94 | * pointless overhead of lock_page on the ZERO_PAGE | |
95 | * which might bounce very badly if there is contention. | |
96 | * | |
97 | * If the page is already locked, we don't need to | |
98 | * handle it now - vmscan will handle it later if and | |
99 | * when it attempts to reclaim the page. | |
100 | */ | |
101 | if (page->mapping && trylock_page(page)) { | |
102 | lru_add_drain(); /* push cached pages to LRU */ | |
103 | /* | |
104 | * Because we lock page here, and migration is | |
105 | * blocked by the pte's page reference, and we | |
106 | * know the page is still mapped, we don't even | |
107 | * need to check for file-cache page truncation. | |
108 | */ | |
109 | mlock_vma_page(page); | |
110 | unlock_page(page); | |
111 | } | |
112 | } | |
4bbd4c77 | 113 | pte_unmap_unlock(ptep, ptl); |
4bbd4c77 | 114 | return page; |
4bbd4c77 KS |
115 | bad_page: |
116 | pte_unmap_unlock(ptep, ptl); | |
117 | return ERR_PTR(-EFAULT); | |
118 | ||
119 | no_page: | |
120 | pte_unmap_unlock(ptep, ptl); | |
121 | if (!pte_none(pte)) | |
69e68b4f KS |
122 | return NULL; |
123 | return no_page_table(vma, flags); | |
124 | } | |
125 | ||
126 | /** | |
127 | * follow_page_mask - look up a page descriptor from a user-virtual address | |
128 | * @vma: vm_area_struct mapping @address | |
129 | * @address: virtual address to look up | |
130 | * @flags: flags modifying lookup behaviour | |
131 | * @page_mask: on output, *page_mask is set according to the size of the page | |
132 | * | |
133 | * @flags can have FOLL_ flags set, defined in <linux/mm.h> | |
134 | * | |
135 | * Returns the mapped (struct page *), %NULL if no mapping exists, or | |
136 | * an error pointer if there is a mapping to something not represented | |
137 | * by a page descriptor (see also vm_normal_page()). | |
138 | */ | |
139 | struct page *follow_page_mask(struct vm_area_struct *vma, | |
140 | unsigned long address, unsigned int flags, | |
141 | unsigned int *page_mask) | |
142 | { | |
143 | pgd_t *pgd; | |
144 | pud_t *pud; | |
145 | pmd_t *pmd; | |
146 | spinlock_t *ptl; | |
147 | struct page *page; | |
148 | struct mm_struct *mm = vma->vm_mm; | |
149 | ||
150 | *page_mask = 0; | |
151 | ||
152 | page = follow_huge_addr(mm, address, flags & FOLL_WRITE); | |
153 | if (!IS_ERR(page)) { | |
154 | BUG_ON(flags & FOLL_GET); | |
4bbd4c77 | 155 | return page; |
69e68b4f | 156 | } |
4bbd4c77 | 157 | |
69e68b4f KS |
158 | pgd = pgd_offset(mm, address); |
159 | if (pgd_none(*pgd) || unlikely(pgd_bad(*pgd))) | |
160 | return no_page_table(vma, flags); | |
161 | ||
162 | pud = pud_offset(pgd, address); | |
163 | if (pud_none(*pud)) | |
164 | return no_page_table(vma, flags); | |
165 | if (pud_huge(*pud) && vma->vm_flags & VM_HUGETLB) { | |
166 | if (flags & FOLL_GET) | |
167 | return NULL; | |
168 | page = follow_huge_pud(mm, address, pud, flags & FOLL_WRITE); | |
169 | return page; | |
170 | } | |
171 | if (unlikely(pud_bad(*pud))) | |
172 | return no_page_table(vma, flags); | |
173 | ||
174 | pmd = pmd_offset(pud, address); | |
175 | if (pmd_none(*pmd)) | |
176 | return no_page_table(vma, flags); | |
177 | if (pmd_huge(*pmd) && vma->vm_flags & VM_HUGETLB) { | |
178 | page = follow_huge_pmd(mm, address, pmd, flags & FOLL_WRITE); | |
179 | if (flags & FOLL_GET) { | |
180 | /* | |
181 | * Refcount on tail pages are not well-defined and | |
182 | * shouldn't be taken. The caller should handle a NULL | |
183 | * return when trying to follow tail pages. | |
184 | */ | |
185 | if (PageHead(page)) | |
186 | get_page(page); | |
187 | else | |
188 | page = NULL; | |
189 | } | |
190 | return page; | |
191 | } | |
192 | if ((flags & FOLL_NUMA) && pmd_numa(*pmd)) | |
193 | return no_page_table(vma, flags); | |
194 | if (pmd_trans_huge(*pmd)) { | |
195 | if (flags & FOLL_SPLIT) { | |
196 | split_huge_page_pmd(vma, address, pmd); | |
197 | return follow_page_pte(vma, address, pmd, flags); | |
198 | } | |
199 | ptl = pmd_lock(mm, pmd); | |
200 | if (likely(pmd_trans_huge(*pmd))) { | |
201 | if (unlikely(pmd_trans_splitting(*pmd))) { | |
202 | spin_unlock(ptl); | |
203 | wait_split_huge_page(vma->anon_vma, pmd); | |
204 | } else { | |
205 | page = follow_trans_huge_pmd(vma, address, | |
206 | pmd, flags); | |
207 | spin_unlock(ptl); | |
208 | *page_mask = HPAGE_PMD_NR - 1; | |
209 | return page; | |
210 | } | |
211 | } else | |
212 | spin_unlock(ptl); | |
213 | } | |
214 | return follow_page_pte(vma, address, pmd, flags); | |
4bbd4c77 KS |
215 | } |
216 | ||
217 | static inline int stack_guard_page(struct vm_area_struct *vma, unsigned long addr) | |
218 | { | |
219 | return stack_guard_page_start(vma, addr) || | |
220 | stack_guard_page_end(vma, addr+PAGE_SIZE); | |
221 | } | |
222 | ||
f2b495ca KS |
223 | static int get_gate_page(struct mm_struct *mm, unsigned long address, |
224 | unsigned int gup_flags, struct vm_area_struct **vma, | |
225 | struct page **page) | |
226 | { | |
227 | pgd_t *pgd; | |
228 | pud_t *pud; | |
229 | pmd_t *pmd; | |
230 | pte_t *pte; | |
231 | int ret = -EFAULT; | |
232 | ||
233 | /* user gate pages are read-only */ | |
234 | if (gup_flags & FOLL_WRITE) | |
235 | return -EFAULT; | |
236 | if (address > TASK_SIZE) | |
237 | pgd = pgd_offset_k(address); | |
238 | else | |
239 | pgd = pgd_offset_gate(mm, address); | |
240 | BUG_ON(pgd_none(*pgd)); | |
241 | pud = pud_offset(pgd, address); | |
242 | BUG_ON(pud_none(*pud)); | |
243 | pmd = pmd_offset(pud, address); | |
244 | if (pmd_none(*pmd)) | |
245 | return -EFAULT; | |
246 | VM_BUG_ON(pmd_trans_huge(*pmd)); | |
247 | pte = pte_offset_map(pmd, address); | |
248 | if (pte_none(*pte)) | |
249 | goto unmap; | |
250 | *vma = get_gate_vma(mm); | |
251 | if (!page) | |
252 | goto out; | |
253 | *page = vm_normal_page(*vma, address, *pte); | |
254 | if (!*page) { | |
255 | if ((gup_flags & FOLL_DUMP) || !is_zero_pfn(pte_pfn(*pte))) | |
256 | goto unmap; | |
257 | *page = pte_page(*pte); | |
258 | } | |
259 | get_page(*page); | |
260 | out: | |
261 | ret = 0; | |
262 | unmap: | |
263 | pte_unmap(pte); | |
264 | return ret; | |
265 | } | |
266 | ||
4bbd4c77 KS |
267 | /** |
268 | * __get_user_pages() - pin user pages in memory | |
269 | * @tsk: task_struct of target task | |
270 | * @mm: mm_struct of target mm | |
271 | * @start: starting user address | |
272 | * @nr_pages: number of pages from start to pin | |
273 | * @gup_flags: flags modifying pin behaviour | |
274 | * @pages: array that receives pointers to the pages pinned. | |
275 | * Should be at least nr_pages long. Or NULL, if caller | |
276 | * only intends to ensure the pages are faulted in. | |
277 | * @vmas: array of pointers to vmas corresponding to each page. | |
278 | * Or NULL if the caller does not require them. | |
279 | * @nonblocking: whether waiting for disk IO or mmap_sem contention | |
280 | * | |
281 | * Returns number of pages pinned. This may be fewer than the number | |
282 | * requested. If nr_pages is 0 or negative, returns 0. If no pages | |
283 | * were pinned, returns -errno. Each page returned must be released | |
284 | * with a put_page() call when it is finished with. vmas will only | |
285 | * remain valid while mmap_sem is held. | |
286 | * | |
287 | * Must be called with mmap_sem held for read or write. | |
288 | * | |
289 | * __get_user_pages walks a process's page tables and takes a reference to | |
290 | * each struct page that each user address corresponds to at a given | |
291 | * instant. That is, it takes the page that would be accessed if a user | |
292 | * thread accesses the given user virtual address at that instant. | |
293 | * | |
294 | * This does not guarantee that the page exists in the user mappings when | |
295 | * __get_user_pages returns, and there may even be a completely different | |
296 | * page there in some cases (eg. if mmapped pagecache has been invalidated | |
297 | * and subsequently re faulted). However it does guarantee that the page | |
298 | * won't be freed completely. And mostly callers simply care that the page | |
299 | * contains data that was valid *at some point in time*. Typically, an IO | |
300 | * or similar operation cannot guarantee anything stronger anyway because | |
301 | * locks can't be held over the syscall boundary. | |
302 | * | |
303 | * If @gup_flags & FOLL_WRITE == 0, the page must not be written to. If | |
304 | * the page is written to, set_page_dirty (or set_page_dirty_lock, as | |
305 | * appropriate) must be called after the page is finished with, and | |
306 | * before put_page is called. | |
307 | * | |
308 | * If @nonblocking != NULL, __get_user_pages will not wait for disk IO | |
309 | * or mmap_sem contention, and if waiting is needed to pin all pages, | |
310 | * *@nonblocking will be set to 0. | |
311 | * | |
312 | * In most cases, get_user_pages or get_user_pages_fast should be used | |
313 | * instead of __get_user_pages. __get_user_pages should be used only if | |
314 | * you need some special @gup_flags. | |
315 | */ | |
316 | long __get_user_pages(struct task_struct *tsk, struct mm_struct *mm, | |
317 | unsigned long start, unsigned long nr_pages, | |
318 | unsigned int gup_flags, struct page **pages, | |
319 | struct vm_area_struct **vmas, int *nonblocking) | |
320 | { | |
321 | long i; | |
322 | unsigned long vm_flags; | |
323 | unsigned int page_mask; | |
324 | ||
325 | if (!nr_pages) | |
326 | return 0; | |
327 | ||
328 | VM_BUG_ON(!!pages != !!(gup_flags & FOLL_GET)); | |
329 | ||
330 | /* | |
331 | * If FOLL_FORCE is set then do not force a full fault as the hinting | |
332 | * fault information is unrelated to the reference behaviour of a task | |
333 | * using the address space | |
334 | */ | |
335 | if (!(gup_flags & FOLL_FORCE)) | |
336 | gup_flags |= FOLL_NUMA; | |
337 | ||
338 | i = 0; | |
339 | ||
340 | do { | |
341 | struct vm_area_struct *vma; | |
342 | ||
343 | vma = find_extend_vma(mm, start); | |
344 | if (!vma && in_gate_area(mm, start)) { | |
f2b495ca KS |
345 | int ret; |
346 | ret = get_gate_page(mm, start & PAGE_MASK, gup_flags, | |
347 | &vma, pages ? &pages[i] : NULL); | |
348 | if (ret) | |
4bbd4c77 | 349 | goto efault; |
4bbd4c77 KS |
350 | page_mask = 0; |
351 | goto next_page; | |
352 | } | |
353 | ||
354 | if (!vma) | |
355 | goto efault; | |
356 | vm_flags = vma->vm_flags; | |
357 | if (vm_flags & (VM_IO | VM_PFNMAP)) | |
358 | goto efault; | |
359 | ||
360 | if (gup_flags & FOLL_WRITE) { | |
361 | if (!(vm_flags & VM_WRITE)) { | |
362 | if (!(gup_flags & FOLL_FORCE)) | |
363 | goto efault; | |
364 | /* | |
365 | * We used to let the write,force case do COW | |
366 | * in a VM_MAYWRITE VM_SHARED !VM_WRITE vma, so | |
367 | * ptrace could set a breakpoint in a read-only | |
368 | * mapping of an executable, without corrupting | |
369 | * the file (yet only when that file had been | |
370 | * opened for writing!). Anon pages in shared | |
371 | * mappings are surprising: now just reject it. | |
372 | */ | |
373 | if (!is_cow_mapping(vm_flags)) { | |
374 | WARN_ON_ONCE(vm_flags & VM_MAYWRITE); | |
375 | goto efault; | |
376 | } | |
377 | } | |
378 | } else { | |
379 | if (!(vm_flags & VM_READ)) { | |
380 | if (!(gup_flags & FOLL_FORCE)) | |
381 | goto efault; | |
382 | /* | |
383 | * Is there actually any vma we can reach here | |
384 | * which does not have VM_MAYREAD set? | |
385 | */ | |
386 | if (!(vm_flags & VM_MAYREAD)) | |
387 | goto efault; | |
388 | } | |
389 | } | |
390 | ||
391 | if (is_vm_hugetlb_page(vma)) { | |
392 | i = follow_hugetlb_page(mm, vma, pages, vmas, | |
393 | &start, &nr_pages, i, gup_flags); | |
394 | continue; | |
395 | } | |
396 | ||
397 | do { | |
398 | struct page *page; | |
399 | unsigned int foll_flags = gup_flags; | |
400 | unsigned int page_increm; | |
401 | ||
402 | /* | |
403 | * If we have a pending SIGKILL, don't keep faulting | |
404 | * pages and potentially allocating memory. | |
405 | */ | |
406 | if (unlikely(fatal_signal_pending(current))) | |
407 | return i ? i : -ERESTARTSYS; | |
408 | ||
409 | cond_resched(); | |
410 | while (!(page = follow_page_mask(vma, start, | |
411 | foll_flags, &page_mask))) { | |
412 | int ret; | |
413 | unsigned int fault_flags = 0; | |
414 | ||
415 | /* For mlock, just skip the stack guard page. */ | |
416 | if (foll_flags & FOLL_MLOCK) { | |
417 | if (stack_guard_page(vma, start)) | |
418 | goto next_page; | |
419 | } | |
420 | if (foll_flags & FOLL_WRITE) | |
421 | fault_flags |= FAULT_FLAG_WRITE; | |
422 | if (nonblocking) | |
423 | fault_flags |= FAULT_FLAG_ALLOW_RETRY; | |
424 | if (foll_flags & FOLL_NOWAIT) | |
425 | fault_flags |= (FAULT_FLAG_ALLOW_RETRY | FAULT_FLAG_RETRY_NOWAIT); | |
426 | ||
427 | ret = handle_mm_fault(mm, vma, start, | |
428 | fault_flags); | |
429 | ||
430 | if (ret & VM_FAULT_ERROR) { | |
431 | if (ret & VM_FAULT_OOM) | |
432 | return i ? i : -ENOMEM; | |
433 | if (ret & (VM_FAULT_HWPOISON | | |
434 | VM_FAULT_HWPOISON_LARGE)) { | |
435 | if (i) | |
436 | return i; | |
437 | else if (gup_flags & FOLL_HWPOISON) | |
438 | return -EHWPOISON; | |
439 | else | |
440 | return -EFAULT; | |
441 | } | |
442 | if (ret & VM_FAULT_SIGBUS) | |
443 | goto efault; | |
444 | BUG(); | |
445 | } | |
446 | ||
447 | if (tsk) { | |
448 | if (ret & VM_FAULT_MAJOR) | |
449 | tsk->maj_flt++; | |
450 | else | |
451 | tsk->min_flt++; | |
452 | } | |
453 | ||
454 | if (ret & VM_FAULT_RETRY) { | |
455 | if (nonblocking) | |
456 | *nonblocking = 0; | |
457 | return i; | |
458 | } | |
459 | ||
460 | /* | |
461 | * The VM_FAULT_WRITE bit tells us that | |
462 | * do_wp_page has broken COW when necessary, | |
463 | * even if maybe_mkwrite decided not to set | |
464 | * pte_write. We can thus safely do subsequent | |
465 | * page lookups as if they were reads. But only | |
466 | * do so when looping for pte_write is futile: | |
467 | * in some cases userspace may also be wanting | |
468 | * to write to the gotten user page, which a | |
469 | * read fault here might prevent (a readonly | |
470 | * page might get reCOWed by userspace write). | |
471 | */ | |
472 | if ((ret & VM_FAULT_WRITE) && | |
473 | !(vma->vm_flags & VM_WRITE)) | |
474 | foll_flags &= ~FOLL_WRITE; | |
475 | ||
476 | cond_resched(); | |
477 | } | |
478 | if (IS_ERR(page)) | |
479 | return i ? i : PTR_ERR(page); | |
480 | if (pages) { | |
481 | pages[i] = page; | |
482 | ||
483 | flush_anon_page(vma, page, start); | |
484 | flush_dcache_page(page); | |
485 | page_mask = 0; | |
486 | } | |
487 | next_page: | |
488 | if (vmas) { | |
489 | vmas[i] = vma; | |
490 | page_mask = 0; | |
491 | } | |
492 | page_increm = 1 + (~(start >> PAGE_SHIFT) & page_mask); | |
493 | if (page_increm > nr_pages) | |
494 | page_increm = nr_pages; | |
495 | i += page_increm; | |
496 | start += page_increm * PAGE_SIZE; | |
497 | nr_pages -= page_increm; | |
498 | } while (nr_pages && start < vma->vm_end); | |
499 | } while (nr_pages); | |
500 | return i; | |
501 | efault: | |
502 | return i ? : -EFAULT; | |
503 | } | |
504 | EXPORT_SYMBOL(__get_user_pages); | |
505 | ||
506 | /* | |
507 | * fixup_user_fault() - manually resolve a user page fault | |
508 | * @tsk: the task_struct to use for page fault accounting, or | |
509 | * NULL if faults are not to be recorded. | |
510 | * @mm: mm_struct of target mm | |
511 | * @address: user address | |
512 | * @fault_flags:flags to pass down to handle_mm_fault() | |
513 | * | |
514 | * This is meant to be called in the specific scenario where for locking reasons | |
515 | * we try to access user memory in atomic context (within a pagefault_disable() | |
516 | * section), this returns -EFAULT, and we want to resolve the user fault before | |
517 | * trying again. | |
518 | * | |
519 | * Typically this is meant to be used by the futex code. | |
520 | * | |
521 | * The main difference with get_user_pages() is that this function will | |
522 | * unconditionally call handle_mm_fault() which will in turn perform all the | |
523 | * necessary SW fixup of the dirty and young bits in the PTE, while | |
524 | * handle_mm_fault() only guarantees to update these in the struct page. | |
525 | * | |
526 | * This is important for some architectures where those bits also gate the | |
527 | * access permission to the page because they are maintained in software. On | |
528 | * such architectures, gup() will not be enough to make a subsequent access | |
529 | * succeed. | |
530 | * | |
531 | * This should be called with the mm_sem held for read. | |
532 | */ | |
533 | int fixup_user_fault(struct task_struct *tsk, struct mm_struct *mm, | |
534 | unsigned long address, unsigned int fault_flags) | |
535 | { | |
536 | struct vm_area_struct *vma; | |
537 | vm_flags_t vm_flags; | |
538 | int ret; | |
539 | ||
540 | vma = find_extend_vma(mm, address); | |
541 | if (!vma || address < vma->vm_start) | |
542 | return -EFAULT; | |
543 | ||
544 | vm_flags = (fault_flags & FAULT_FLAG_WRITE) ? VM_WRITE : VM_READ; | |
545 | if (!(vm_flags & vma->vm_flags)) | |
546 | return -EFAULT; | |
547 | ||
548 | ret = handle_mm_fault(mm, vma, address, fault_flags); | |
549 | if (ret & VM_FAULT_ERROR) { | |
550 | if (ret & VM_FAULT_OOM) | |
551 | return -ENOMEM; | |
552 | if (ret & (VM_FAULT_HWPOISON | VM_FAULT_HWPOISON_LARGE)) | |
553 | return -EHWPOISON; | |
554 | if (ret & VM_FAULT_SIGBUS) | |
555 | return -EFAULT; | |
556 | BUG(); | |
557 | } | |
558 | if (tsk) { | |
559 | if (ret & VM_FAULT_MAJOR) | |
560 | tsk->maj_flt++; | |
561 | else | |
562 | tsk->min_flt++; | |
563 | } | |
564 | return 0; | |
565 | } | |
566 | ||
567 | /* | |
568 | * get_user_pages() - pin user pages in memory | |
569 | * @tsk: the task_struct to use for page fault accounting, or | |
570 | * NULL if faults are not to be recorded. | |
571 | * @mm: mm_struct of target mm | |
572 | * @start: starting user address | |
573 | * @nr_pages: number of pages from start to pin | |
574 | * @write: whether pages will be written to by the caller | |
575 | * @force: whether to force access even when user mapping is currently | |
576 | * protected (but never forces write access to shared mapping). | |
577 | * @pages: array that receives pointers to the pages pinned. | |
578 | * Should be at least nr_pages long. Or NULL, if caller | |
579 | * only intends to ensure the pages are faulted in. | |
580 | * @vmas: array of pointers to vmas corresponding to each page. | |
581 | * Or NULL if the caller does not require them. | |
582 | * | |
583 | * Returns number of pages pinned. This may be fewer than the number | |
584 | * requested. If nr_pages is 0 or negative, returns 0. If no pages | |
585 | * were pinned, returns -errno. Each page returned must be released | |
586 | * with a put_page() call when it is finished with. vmas will only | |
587 | * remain valid while mmap_sem is held. | |
588 | * | |
589 | * Must be called with mmap_sem held for read or write. | |
590 | * | |
591 | * get_user_pages walks a process's page tables and takes a reference to | |
592 | * each struct page that each user address corresponds to at a given | |
593 | * instant. That is, it takes the page that would be accessed if a user | |
594 | * thread accesses the given user virtual address at that instant. | |
595 | * | |
596 | * This does not guarantee that the page exists in the user mappings when | |
597 | * get_user_pages returns, and there may even be a completely different | |
598 | * page there in some cases (eg. if mmapped pagecache has been invalidated | |
599 | * and subsequently re faulted). However it does guarantee that the page | |
600 | * won't be freed completely. And mostly callers simply care that the page | |
601 | * contains data that was valid *at some point in time*. Typically, an IO | |
602 | * or similar operation cannot guarantee anything stronger anyway because | |
603 | * locks can't be held over the syscall boundary. | |
604 | * | |
605 | * If write=0, the page must not be written to. If the page is written to, | |
606 | * set_page_dirty (or set_page_dirty_lock, as appropriate) must be called | |
607 | * after the page is finished with, and before put_page is called. | |
608 | * | |
609 | * get_user_pages is typically used for fewer-copy IO operations, to get a | |
610 | * handle on the memory by some means other than accesses via the user virtual | |
611 | * addresses. The pages may be submitted for DMA to devices or accessed via | |
612 | * their kernel linear mapping (via the kmap APIs). Care should be taken to | |
613 | * use the correct cache flushing APIs. | |
614 | * | |
615 | * See also get_user_pages_fast, for performance critical applications. | |
616 | */ | |
617 | long get_user_pages(struct task_struct *tsk, struct mm_struct *mm, | |
618 | unsigned long start, unsigned long nr_pages, int write, | |
619 | int force, struct page **pages, struct vm_area_struct **vmas) | |
620 | { | |
621 | int flags = FOLL_TOUCH; | |
622 | ||
623 | if (pages) | |
624 | flags |= FOLL_GET; | |
625 | if (write) | |
626 | flags |= FOLL_WRITE; | |
627 | if (force) | |
628 | flags |= FOLL_FORCE; | |
629 | ||
630 | return __get_user_pages(tsk, mm, start, nr_pages, flags, pages, vmas, | |
631 | NULL); | |
632 | } | |
633 | EXPORT_SYMBOL(get_user_pages); | |
634 | ||
635 | /** | |
636 | * get_dump_page() - pin user page in memory while writing it to core dump | |
637 | * @addr: user address | |
638 | * | |
639 | * Returns struct page pointer of user page pinned for dump, | |
640 | * to be freed afterwards by page_cache_release() or put_page(). | |
641 | * | |
642 | * Returns NULL on any kind of failure - a hole must then be inserted into | |
643 | * the corefile, to preserve alignment with its headers; and also returns | |
644 | * NULL wherever the ZERO_PAGE, or an anonymous pte_none, has been found - | |
645 | * allowing a hole to be left in the corefile to save diskspace. | |
646 | * | |
647 | * Called without mmap_sem, but after all other threads have been killed. | |
648 | */ | |
649 | #ifdef CONFIG_ELF_CORE | |
650 | struct page *get_dump_page(unsigned long addr) | |
651 | { | |
652 | struct vm_area_struct *vma; | |
653 | struct page *page; | |
654 | ||
655 | if (__get_user_pages(current, current->mm, addr, 1, | |
656 | FOLL_FORCE | FOLL_DUMP | FOLL_GET, &page, &vma, | |
657 | NULL) < 1) | |
658 | return NULL; | |
659 | flush_cache_page(vma, addr, page_to_pfn(page)); | |
660 | return page; | |
661 | } | |
662 | #endif /* CONFIG_ELF_CORE */ |