]>
Commit | Line | Data |
---|---|---|
20c8ccb1 | 1 | // SPDX-License-Identifier: GPL-2.0-only |
c1a4de99 AA |
2 | /* |
3 | * mm/userfaultfd.c | |
4 | * | |
5 | * Copyright (C) 2015 Red Hat, Inc. | |
c1a4de99 AA |
6 | */ |
7 | ||
8 | #include <linux/mm.h> | |
174cd4b1 | 9 | #include <linux/sched/signal.h> |
c1a4de99 AA |
10 | #include <linux/pagemap.h> |
11 | #include <linux/rmap.h> | |
12 | #include <linux/swap.h> | |
13 | #include <linux/swapops.h> | |
14 | #include <linux/userfaultfd_k.h> | |
15 | #include <linux/mmu_notifier.h> | |
60d4d2d2 | 16 | #include <linux/hugetlb.h> |
26071ced | 17 | #include <linux/shmem_fs.h> |
c1a4de99 AA |
18 | #include <asm/tlbflush.h> |
19 | #include "internal.h" | |
20 | ||
643aa36e WY |
21 | static __always_inline |
22 | struct vm_area_struct *find_dst_vma(struct mm_struct *dst_mm, | |
23 | unsigned long dst_start, | |
24 | unsigned long len) | |
25 | { | |
26 | /* | |
27 | * Make sure that the dst range is both valid and fully within a | |
28 | * single existing vma. | |
29 | */ | |
30 | struct vm_area_struct *dst_vma; | |
31 | ||
32 | dst_vma = find_vma(dst_mm, dst_start); | |
33 | if (!dst_vma) | |
34 | return NULL; | |
35 | ||
36 | if (dst_start < dst_vma->vm_start || | |
37 | dst_start + len > dst_vma->vm_end) | |
38 | return NULL; | |
39 | ||
40 | /* | |
41 | * Check the vma is registered in uffd, this is required to | |
42 | * enforce the VM_MAYWRITE check done at uffd registration | |
43 | * time. | |
44 | */ | |
45 | if (!dst_vma->vm_userfaultfd_ctx.ctx) | |
46 | return NULL; | |
47 | ||
48 | return dst_vma; | |
49 | } | |
50 | ||
15313257 AR |
51 | /* |
52 | * Install PTEs, to map dst_addr (within dst_vma) to page. | |
53 | * | |
7d64ae3a AR |
54 | * This function handles both MCOPY_ATOMIC_NORMAL and _CONTINUE for both shmem |
55 | * and anon, and for both shared and private VMAs. | |
15313257 | 56 | */ |
7d64ae3a AR |
57 | int mfill_atomic_install_pte(struct mm_struct *dst_mm, pmd_t *dst_pmd, |
58 | struct vm_area_struct *dst_vma, | |
59 | unsigned long dst_addr, struct page *page, | |
60 | bool newly_allocated, bool wp_copy) | |
15313257 AR |
61 | { |
62 | int ret; | |
63 | pte_t _dst_pte, *dst_pte; | |
64 | bool writable = dst_vma->vm_flags & VM_WRITE; | |
65 | bool vm_shared = dst_vma->vm_flags & VM_SHARED; | |
66 | bool page_in_cache = page->mapping; | |
67 | spinlock_t *ptl; | |
68 | struct inode *inode; | |
69 | pgoff_t offset, max_off; | |
70 | ||
71 | _dst_pte = mk_pte(page, dst_vma->vm_page_prot); | |
72 | if (page_in_cache && !vm_shared) | |
73 | writable = false; | |
74 | if (writable || !page_in_cache) | |
75 | _dst_pte = pte_mkdirty(_dst_pte); | |
76 | if (writable) { | |
77 | if (wp_copy) | |
78 | _dst_pte = pte_mkuffd_wp(_dst_pte); | |
79 | else | |
80 | _dst_pte = pte_mkwrite(_dst_pte); | |
81 | } | |
82 | ||
83 | dst_pte = pte_offset_map_lock(dst_mm, dst_pmd, dst_addr, &ptl); | |
84 | ||
85 | if (vma_is_shmem(dst_vma)) { | |
86 | /* serialize against truncate with the page table lock */ | |
87 | inode = dst_vma->vm_file->f_inode; | |
88 | offset = linear_page_index(dst_vma, dst_addr); | |
89 | max_off = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE); | |
90 | ret = -EFAULT; | |
91 | if (unlikely(offset >= max_off)) | |
92 | goto out_unlock; | |
93 | } | |
94 | ||
95 | ret = -EEXIST; | |
96 | if (!pte_none(*dst_pte)) | |
97 | goto out_unlock; | |
98 | ||
99 | if (page_in_cache) | |
100 | page_add_file_rmap(page, false); | |
101 | else | |
102 | page_add_new_anon_rmap(page, dst_vma, dst_addr, false); | |
103 | ||
104 | /* | |
105 | * Must happen after rmap, as mm_counter() checks mapping (via | |
106 | * PageAnon()), which is set by __page_set_anon_rmap(). | |
107 | */ | |
108 | inc_mm_counter(dst_mm, mm_counter(page)); | |
109 | ||
110 | if (newly_allocated) | |
111 | lru_cache_add_inactive_or_unevictable(page, dst_vma); | |
112 | ||
113 | set_pte_at(dst_mm, dst_addr, dst_pte, _dst_pte); | |
114 | ||
115 | /* No need to invalidate - it was non-present before */ | |
116 | update_mmu_cache(dst_vma, dst_addr, dst_pte); | |
117 | ret = 0; | |
118 | out_unlock: | |
119 | pte_unmap_unlock(dst_pte, ptl); | |
120 | return ret; | |
121 | } | |
122 | ||
c1a4de99 AA |
123 | static int mcopy_atomic_pte(struct mm_struct *dst_mm, |
124 | pmd_t *dst_pmd, | |
125 | struct vm_area_struct *dst_vma, | |
126 | unsigned long dst_addr, | |
b6ebaedb | 127 | unsigned long src_addr, |
72981e0e AA |
128 | struct page **pagep, |
129 | bool wp_copy) | |
c1a4de99 | 130 | { |
c1a4de99 AA |
131 | void *page_kaddr; |
132 | int ret; | |
b6ebaedb | 133 | struct page *page; |
c1a4de99 | 134 | |
b6ebaedb AA |
135 | if (!*pagep) { |
136 | ret = -ENOMEM; | |
137 | page = alloc_page_vma(GFP_HIGHUSER_MOVABLE, dst_vma, dst_addr); | |
138 | if (!page) | |
139 | goto out; | |
140 | ||
141 | page_kaddr = kmap_atomic(page); | |
142 | ret = copy_from_user(page_kaddr, | |
143 | (const void __user *) src_addr, | |
144 | PAGE_SIZE); | |
145 | kunmap_atomic(page_kaddr); | |
146 | ||
c1e8d7c6 | 147 | /* fallback to copy_from_user outside mmap_lock */ |
b6ebaedb | 148 | if (unlikely(ret)) { |
9e368259 | 149 | ret = -ENOENT; |
b6ebaedb AA |
150 | *pagep = page; |
151 | /* don't free the page */ | |
152 | goto out; | |
153 | } | |
d0cf2e46 MS |
154 | |
155 | flush_dcache_page(page); | |
b6ebaedb AA |
156 | } else { |
157 | page = *pagep; | |
158 | *pagep = NULL; | |
159 | } | |
c1a4de99 AA |
160 | |
161 | /* | |
162 | * The memory barrier inside __SetPageUptodate makes sure that | |
f4f5329d | 163 | * preceding stores to the page contents become visible before |
c1a4de99 AA |
164 | * the set_pte_at() write. |
165 | */ | |
166 | __SetPageUptodate(page); | |
167 | ||
168 | ret = -ENOMEM; | |
d9eb1ea2 | 169 | if (mem_cgroup_charge(page, dst_mm, GFP_KERNEL)) |
c1a4de99 AA |
170 | goto out_release; |
171 | ||
15313257 AR |
172 | ret = mfill_atomic_install_pte(dst_mm, dst_pmd, dst_vma, dst_addr, |
173 | page, true, wp_copy); | |
174 | if (ret) | |
175 | goto out_release; | |
c1a4de99 AA |
176 | out: |
177 | return ret; | |
c1a4de99 | 178 | out_release: |
09cbfeaf | 179 | put_page(page); |
c1a4de99 | 180 | goto out; |
c1a4de99 AA |
181 | } |
182 | ||
183 | static int mfill_zeropage_pte(struct mm_struct *dst_mm, | |
184 | pmd_t *dst_pmd, | |
185 | struct vm_area_struct *dst_vma, | |
186 | unsigned long dst_addr) | |
187 | { | |
188 | pte_t _dst_pte, *dst_pte; | |
189 | spinlock_t *ptl; | |
190 | int ret; | |
e2a50c1f AA |
191 | pgoff_t offset, max_off; |
192 | struct inode *inode; | |
c1a4de99 AA |
193 | |
194 | _dst_pte = pte_mkspecial(pfn_pte(my_zero_pfn(dst_addr), | |
195 | dst_vma->vm_page_prot)); | |
c1a4de99 | 196 | dst_pte = pte_offset_map_lock(dst_mm, dst_pmd, dst_addr, &ptl); |
e2a50c1f AA |
197 | if (dst_vma->vm_file) { |
198 | /* the shmem MAP_PRIVATE case requires checking the i_size */ | |
199 | inode = dst_vma->vm_file->f_inode; | |
200 | offset = linear_page_index(dst_vma, dst_addr); | |
201 | max_off = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE); | |
202 | ret = -EFAULT; | |
203 | if (unlikely(offset >= max_off)) | |
204 | goto out_unlock; | |
205 | } | |
206 | ret = -EEXIST; | |
c1a4de99 AA |
207 | if (!pte_none(*dst_pte)) |
208 | goto out_unlock; | |
209 | set_pte_at(dst_mm, dst_addr, dst_pte, _dst_pte); | |
210 | /* No need to invalidate - it was non-present before */ | |
211 | update_mmu_cache(dst_vma, dst_addr, dst_pte); | |
212 | ret = 0; | |
213 | out_unlock: | |
214 | pte_unmap_unlock(dst_pte, ptl); | |
215 | return ret; | |
216 | } | |
217 | ||
15313257 AR |
218 | /* Handles UFFDIO_CONTINUE for all shmem VMAs (shared or private). */ |
219 | static int mcontinue_atomic_pte(struct mm_struct *dst_mm, | |
220 | pmd_t *dst_pmd, | |
221 | struct vm_area_struct *dst_vma, | |
222 | unsigned long dst_addr, | |
223 | bool wp_copy) | |
224 | { | |
225 | struct inode *inode = file_inode(dst_vma->vm_file); | |
226 | pgoff_t pgoff = linear_page_index(dst_vma, dst_addr); | |
227 | struct page *page; | |
228 | int ret; | |
229 | ||
aeeaed21 AR |
230 | ret = shmem_getpage(inode, pgoff, &page, SGP_NOALLOC); |
231 | /* Our caller expects us to return -EFAULT if we failed to find page. */ | |
232 | if (ret == -ENOENT) | |
233 | ret = -EFAULT; | |
15313257 AR |
234 | if (ret) |
235 | goto out; | |
236 | if (!page) { | |
237 | ret = -EFAULT; | |
238 | goto out; | |
239 | } | |
240 | ||
241 | ret = mfill_atomic_install_pte(dst_mm, dst_pmd, dst_vma, dst_addr, | |
242 | page, false, wp_copy); | |
243 | if (ret) | |
244 | goto out_release; | |
245 | ||
246 | unlock_page(page); | |
247 | ret = 0; | |
248 | out: | |
249 | return ret; | |
250 | out_release: | |
251 | unlock_page(page); | |
252 | put_page(page); | |
253 | goto out; | |
254 | } | |
255 | ||
c1a4de99 AA |
256 | static pmd_t *mm_alloc_pmd(struct mm_struct *mm, unsigned long address) |
257 | { | |
258 | pgd_t *pgd; | |
c2febafc | 259 | p4d_t *p4d; |
c1a4de99 | 260 | pud_t *pud; |
c1a4de99 AA |
261 | |
262 | pgd = pgd_offset(mm, address); | |
c2febafc KS |
263 | p4d = p4d_alloc(mm, pgd, address); |
264 | if (!p4d) | |
265 | return NULL; | |
266 | pud = pud_alloc(mm, p4d, address); | |
267 | if (!pud) | |
268 | return NULL; | |
269 | /* | |
270 | * Note that we didn't run this because the pmd was | |
271 | * missing, the *pmd may be already established and in | |
272 | * turn it may also be a trans_huge_pmd. | |
273 | */ | |
274 | return pmd_alloc(mm, pud, address); | |
c1a4de99 AA |
275 | } |
276 | ||
60d4d2d2 MK |
277 | #ifdef CONFIG_HUGETLB_PAGE |
278 | /* | |
279 | * __mcopy_atomic processing for HUGETLB vmas. Note that this routine is | |
c1e8d7c6 | 280 | * called with mmap_lock held, it will release mmap_lock before returning. |
60d4d2d2 MK |
281 | */ |
282 | static __always_inline ssize_t __mcopy_atomic_hugetlb(struct mm_struct *dst_mm, | |
283 | struct vm_area_struct *dst_vma, | |
284 | unsigned long dst_start, | |
285 | unsigned long src_start, | |
286 | unsigned long len, | |
f6191471 | 287 | enum mcopy_atomic_mode mode) |
60d4d2d2 | 288 | { |
1c9e8def | 289 | int vm_shared = dst_vma->vm_flags & VM_SHARED; |
60d4d2d2 MK |
290 | ssize_t err; |
291 | pte_t *dst_pte; | |
292 | unsigned long src_addr, dst_addr; | |
293 | long copied; | |
294 | struct page *page; | |
60d4d2d2 MK |
295 | unsigned long vma_hpagesize; |
296 | pgoff_t idx; | |
297 | u32 hash; | |
298 | struct address_space *mapping; | |
299 | ||
300 | /* | |
301 | * There is no default zero huge page for all huge page sizes as | |
302 | * supported by hugetlb. A PMD_SIZE huge pages may exist as used | |
303 | * by THP. Since we can not reliably insert a zero page, this | |
304 | * feature is not supported. | |
305 | */ | |
f6191471 | 306 | if (mode == MCOPY_ATOMIC_ZEROPAGE) { |
d8ed45c5 | 307 | mmap_read_unlock(dst_mm); |
60d4d2d2 MK |
308 | return -EINVAL; |
309 | } | |
310 | ||
311 | src_addr = src_start; | |
312 | dst_addr = dst_start; | |
313 | copied = 0; | |
314 | page = NULL; | |
315 | vma_hpagesize = vma_kernel_pagesize(dst_vma); | |
316 | ||
317 | /* | |
318 | * Validate alignment based on huge page size | |
319 | */ | |
320 | err = -EINVAL; | |
321 | if (dst_start & (vma_hpagesize - 1) || len & (vma_hpagesize - 1)) | |
322 | goto out_unlock; | |
323 | ||
324 | retry: | |
325 | /* | |
c1e8d7c6 | 326 | * On routine entry dst_vma is set. If we had to drop mmap_lock and |
60d4d2d2 MK |
327 | * retry, dst_vma will be set to NULL and we must lookup again. |
328 | */ | |
329 | if (!dst_vma) { | |
27d02568 | 330 | err = -ENOENT; |
643aa36e | 331 | dst_vma = find_dst_vma(dst_mm, dst_start, len); |
60d4d2d2 MK |
332 | if (!dst_vma || !is_vm_hugetlb_page(dst_vma)) |
333 | goto out_unlock; | |
1c9e8def | 334 | |
27d02568 MR |
335 | err = -EINVAL; |
336 | if (vma_hpagesize != vma_kernel_pagesize(dst_vma)) | |
337 | goto out_unlock; | |
338 | ||
1c9e8def | 339 | vm_shared = dst_vma->vm_flags & VM_SHARED; |
60d4d2d2 MK |
340 | } |
341 | ||
60d4d2d2 | 342 | /* |
1c9e8def | 343 | * If not shared, ensure the dst_vma has a anon_vma. |
60d4d2d2 MK |
344 | */ |
345 | err = -ENOMEM; | |
1c9e8def MK |
346 | if (!vm_shared) { |
347 | if (unlikely(anon_vma_prepare(dst_vma))) | |
348 | goto out_unlock; | |
349 | } | |
60d4d2d2 | 350 | |
60d4d2d2 | 351 | while (src_addr < src_start + len) { |
60d4d2d2 | 352 | BUG_ON(dst_addr >= dst_start + len); |
60d4d2d2 MK |
353 | |
354 | /* | |
c0d0381a MK |
355 | * Serialize via i_mmap_rwsem and hugetlb_fault_mutex. |
356 | * i_mmap_rwsem ensures the dst_pte remains valid even | |
357 | * in the case of shared pmds. fault mutex prevents | |
358 | * races with other faulting threads. | |
60d4d2d2 | 359 | */ |
ddeaab32 | 360 | mapping = dst_vma->vm_file->f_mapping; |
c0d0381a MK |
361 | i_mmap_lock_read(mapping); |
362 | idx = linear_page_index(dst_vma, dst_addr); | |
188b04a7 | 363 | hash = hugetlb_fault_mutex_hash(mapping, idx); |
60d4d2d2 MK |
364 | mutex_lock(&hugetlb_fault_mutex_table[hash]); |
365 | ||
366 | err = -ENOMEM; | |
aec44e0f | 367 | dst_pte = huge_pte_alloc(dst_mm, dst_vma, dst_addr, vma_hpagesize); |
60d4d2d2 MK |
368 | if (!dst_pte) { |
369 | mutex_unlock(&hugetlb_fault_mutex_table[hash]); | |
c0d0381a | 370 | i_mmap_unlock_read(mapping); |
60d4d2d2 MK |
371 | goto out_unlock; |
372 | } | |
373 | ||
f6191471 AR |
374 | if (mode != MCOPY_ATOMIC_CONTINUE && |
375 | !huge_pte_none(huge_ptep_get(dst_pte))) { | |
376 | err = -EEXIST; | |
60d4d2d2 | 377 | mutex_unlock(&hugetlb_fault_mutex_table[hash]); |
c0d0381a | 378 | i_mmap_unlock_read(mapping); |
60d4d2d2 MK |
379 | goto out_unlock; |
380 | } | |
381 | ||
382 | err = hugetlb_mcopy_atomic_pte(dst_mm, dst_pte, dst_vma, | |
f6191471 | 383 | dst_addr, src_addr, mode, &page); |
60d4d2d2 MK |
384 | |
385 | mutex_unlock(&hugetlb_fault_mutex_table[hash]); | |
c0d0381a | 386 | i_mmap_unlock_read(mapping); |
60d4d2d2 MK |
387 | |
388 | cond_resched(); | |
389 | ||
9e368259 | 390 | if (unlikely(err == -ENOENT)) { |
d8ed45c5 | 391 | mmap_read_unlock(dst_mm); |
60d4d2d2 MK |
392 | BUG_ON(!page); |
393 | ||
394 | err = copy_huge_page_from_user(page, | |
395 | (const void __user *)src_addr, | |
4fb07ee6 WY |
396 | vma_hpagesize / PAGE_SIZE, |
397 | true); | |
60d4d2d2 MK |
398 | if (unlikely(err)) { |
399 | err = -EFAULT; | |
400 | goto out; | |
401 | } | |
d8ed45c5 | 402 | mmap_read_lock(dst_mm); |
60d4d2d2 MK |
403 | |
404 | dst_vma = NULL; | |
405 | goto retry; | |
406 | } else | |
407 | BUG_ON(page); | |
408 | ||
409 | if (!err) { | |
410 | dst_addr += vma_hpagesize; | |
411 | src_addr += vma_hpagesize; | |
412 | copied += vma_hpagesize; | |
413 | ||
414 | if (fatal_signal_pending(current)) | |
415 | err = -EINTR; | |
416 | } | |
417 | if (err) | |
418 | break; | |
419 | } | |
420 | ||
421 | out_unlock: | |
d8ed45c5 | 422 | mmap_read_unlock(dst_mm); |
60d4d2d2 | 423 | out: |
8cc5fcbb | 424 | if (page) |
60d4d2d2 MK |
425 | put_page(page); |
426 | BUG_ON(copied < 0); | |
427 | BUG_ON(err > 0); | |
428 | BUG_ON(!copied && !err); | |
429 | return copied ? copied : err; | |
430 | } | |
431 | #else /* !CONFIG_HUGETLB_PAGE */ | |
432 | /* fail at build time if gcc attempts to use this */ | |
433 | extern ssize_t __mcopy_atomic_hugetlb(struct mm_struct *dst_mm, | |
434 | struct vm_area_struct *dst_vma, | |
435 | unsigned long dst_start, | |
436 | unsigned long src_start, | |
437 | unsigned long len, | |
f6191471 | 438 | enum mcopy_atomic_mode mode); |
60d4d2d2 MK |
439 | #endif /* CONFIG_HUGETLB_PAGE */ |
440 | ||
3217d3c7 MR |
441 | static __always_inline ssize_t mfill_atomic_pte(struct mm_struct *dst_mm, |
442 | pmd_t *dst_pmd, | |
443 | struct vm_area_struct *dst_vma, | |
444 | unsigned long dst_addr, | |
445 | unsigned long src_addr, | |
446 | struct page **page, | |
15313257 | 447 | enum mcopy_atomic_mode mode, |
72981e0e | 448 | bool wp_copy) |
3217d3c7 MR |
449 | { |
450 | ssize_t err; | |
451 | ||
15313257 AR |
452 | if (mode == MCOPY_ATOMIC_CONTINUE) { |
453 | return mcontinue_atomic_pte(dst_mm, dst_pmd, dst_vma, dst_addr, | |
454 | wp_copy); | |
455 | } | |
456 | ||
5b51072e AA |
457 | /* |
458 | * The normal page fault path for a shmem will invoke the | |
459 | * fault, fill the hole in the file and COW it right away. The | |
460 | * result generates plain anonymous memory. So when we are | |
461 | * asked to fill an hole in a MAP_PRIVATE shmem mapping, we'll | |
462 | * generate anonymous memory directly without actually filling | |
463 | * the hole. For the MAP_PRIVATE case the robustness check | |
464 | * only happens in the pagetable (to verify it's still none) | |
465 | * and not in the radix tree. | |
466 | */ | |
467 | if (!(dst_vma->vm_flags & VM_SHARED)) { | |
15313257 | 468 | if (mode == MCOPY_ATOMIC_NORMAL) |
3217d3c7 | 469 | err = mcopy_atomic_pte(dst_mm, dst_pmd, dst_vma, |
72981e0e AA |
470 | dst_addr, src_addr, page, |
471 | wp_copy); | |
3217d3c7 MR |
472 | else |
473 | err = mfill_zeropage_pte(dst_mm, dst_pmd, | |
474 | dst_vma, dst_addr); | |
475 | } else { | |
72981e0e | 476 | VM_WARN_ON_ONCE(wp_copy); |
3460f6e5 | 477 | err = shmem_mfill_atomic_pte(dst_mm, dst_pmd, dst_vma, |
15313257 AR |
478 | dst_addr, src_addr, |
479 | mode != MCOPY_ATOMIC_NORMAL, | |
3460f6e5 | 480 | page); |
3217d3c7 MR |
481 | } |
482 | ||
483 | return err; | |
484 | } | |
485 | ||
c1a4de99 AA |
486 | static __always_inline ssize_t __mcopy_atomic(struct mm_struct *dst_mm, |
487 | unsigned long dst_start, | |
488 | unsigned long src_start, | |
489 | unsigned long len, | |
f6191471 | 490 | enum mcopy_atomic_mode mcopy_mode, |
a759a909 | 491 | atomic_t *mmap_changing, |
72981e0e | 492 | __u64 mode) |
c1a4de99 AA |
493 | { |
494 | struct vm_area_struct *dst_vma; | |
495 | ssize_t err; | |
496 | pmd_t *dst_pmd; | |
497 | unsigned long src_addr, dst_addr; | |
b6ebaedb AA |
498 | long copied; |
499 | struct page *page; | |
72981e0e | 500 | bool wp_copy; |
c1a4de99 AA |
501 | |
502 | /* | |
503 | * Sanitize the command parameters: | |
504 | */ | |
505 | BUG_ON(dst_start & ~PAGE_MASK); | |
506 | BUG_ON(len & ~PAGE_MASK); | |
507 | ||
508 | /* Does the address range wrap, or is the span zero-sized? */ | |
509 | BUG_ON(src_start + len <= src_start); | |
510 | BUG_ON(dst_start + len <= dst_start); | |
511 | ||
b6ebaedb AA |
512 | src_addr = src_start; |
513 | dst_addr = dst_start; | |
514 | copied = 0; | |
515 | page = NULL; | |
516 | retry: | |
d8ed45c5 | 517 | mmap_read_lock(dst_mm); |
c1a4de99 | 518 | |
df2cc96e MR |
519 | /* |
520 | * If memory mappings are changing because of non-cooperative | |
521 | * operation (e.g. mremap) running in parallel, bail out and | |
522 | * request the user to retry later | |
523 | */ | |
524 | err = -EAGAIN; | |
a759a909 | 525 | if (mmap_changing && atomic_read(mmap_changing)) |
df2cc96e MR |
526 | goto out_unlock; |
527 | ||
c1a4de99 AA |
528 | /* |
529 | * Make sure the vma is not shared, that the dst range is | |
530 | * both valid and fully within a single existing vma. | |
531 | */ | |
27d02568 | 532 | err = -ENOENT; |
643aa36e | 533 | dst_vma = find_dst_vma(dst_mm, dst_start, len); |
26071ced MR |
534 | if (!dst_vma) |
535 | goto out_unlock; | |
c1a4de99 | 536 | |
27d02568 MR |
537 | err = -EINVAL; |
538 | /* | |
539 | * shmem_zero_setup is invoked in mmap for MAP_ANONYMOUS|MAP_SHARED but | |
540 | * it will overwrite vm_ops, so vma_is_anonymous must return false. | |
541 | */ | |
542 | if (WARN_ON_ONCE(vma_is_anonymous(dst_vma) && | |
543 | dst_vma->vm_flags & VM_SHARED)) | |
544 | goto out_unlock; | |
545 | ||
72981e0e AA |
546 | /* |
547 | * validate 'mode' now that we know the dst_vma: don't allow | |
548 | * a wrprotect copy if the userfaultfd didn't register as WP. | |
549 | */ | |
550 | wp_copy = mode & UFFDIO_COPY_MODE_WP; | |
551 | if (wp_copy && !(dst_vma->vm_flags & VM_UFFD_WP)) | |
552 | goto out_unlock; | |
553 | ||
60d4d2d2 MK |
554 | /* |
555 | * If this is a HUGETLB vma, pass off to appropriate routine | |
556 | */ | |
557 | if (is_vm_hugetlb_page(dst_vma)) | |
558 | return __mcopy_atomic_hugetlb(dst_mm, dst_vma, dst_start, | |
f6191471 | 559 | src_start, len, mcopy_mode); |
60d4d2d2 | 560 | |
26071ced | 561 | if (!vma_is_anonymous(dst_vma) && !vma_is_shmem(dst_vma)) |
b6ebaedb | 562 | goto out_unlock; |
15313257 | 563 | if (!vma_is_shmem(dst_vma) && mcopy_mode == MCOPY_ATOMIC_CONTINUE) |
f6191471 | 564 | goto out_unlock; |
c1a4de99 AA |
565 | |
566 | /* | |
567 | * Ensure the dst_vma has a anon_vma or this page | |
568 | * would get a NULL anon_vma when moved in the | |
569 | * dst_vma. | |
570 | */ | |
571 | err = -ENOMEM; | |
5b51072e AA |
572 | if (!(dst_vma->vm_flags & VM_SHARED) && |
573 | unlikely(anon_vma_prepare(dst_vma))) | |
b6ebaedb | 574 | goto out_unlock; |
c1a4de99 | 575 | |
b6ebaedb | 576 | while (src_addr < src_start + len) { |
c1a4de99 | 577 | pmd_t dst_pmdval; |
b6ebaedb | 578 | |
c1a4de99 | 579 | BUG_ON(dst_addr >= dst_start + len); |
b6ebaedb | 580 | |
c1a4de99 AA |
581 | dst_pmd = mm_alloc_pmd(dst_mm, dst_addr); |
582 | if (unlikely(!dst_pmd)) { | |
583 | err = -ENOMEM; | |
584 | break; | |
585 | } | |
586 | ||
587 | dst_pmdval = pmd_read_atomic(dst_pmd); | |
588 | /* | |
589 | * If the dst_pmd is mapped as THP don't | |
590 | * override it and just be strict. | |
591 | */ | |
592 | if (unlikely(pmd_trans_huge(dst_pmdval))) { | |
593 | err = -EEXIST; | |
594 | break; | |
595 | } | |
596 | if (unlikely(pmd_none(dst_pmdval)) && | |
4cf58924 | 597 | unlikely(__pte_alloc(dst_mm, dst_pmd))) { |
c1a4de99 AA |
598 | err = -ENOMEM; |
599 | break; | |
600 | } | |
601 | /* If an huge pmd materialized from under us fail */ | |
602 | if (unlikely(pmd_trans_huge(*dst_pmd))) { | |
603 | err = -EFAULT; | |
604 | break; | |
605 | } | |
606 | ||
607 | BUG_ON(pmd_none(*dst_pmd)); | |
608 | BUG_ON(pmd_trans_huge(*dst_pmd)); | |
609 | ||
3217d3c7 | 610 | err = mfill_atomic_pte(dst_mm, dst_pmd, dst_vma, dst_addr, |
15313257 | 611 | src_addr, &page, mcopy_mode, wp_copy); |
c1a4de99 AA |
612 | cond_resched(); |
613 | ||
9e368259 | 614 | if (unlikely(err == -ENOENT)) { |
b6ebaedb AA |
615 | void *page_kaddr; |
616 | ||
d8ed45c5 | 617 | mmap_read_unlock(dst_mm); |
b6ebaedb AA |
618 | BUG_ON(!page); |
619 | ||
620 | page_kaddr = kmap(page); | |
621 | err = copy_from_user(page_kaddr, | |
622 | (const void __user *) src_addr, | |
623 | PAGE_SIZE); | |
624 | kunmap(page); | |
625 | if (unlikely(err)) { | |
626 | err = -EFAULT; | |
627 | goto out; | |
628 | } | |
d0cf2e46 | 629 | flush_dcache_page(page); |
b6ebaedb AA |
630 | goto retry; |
631 | } else | |
632 | BUG_ON(page); | |
633 | ||
c1a4de99 AA |
634 | if (!err) { |
635 | dst_addr += PAGE_SIZE; | |
636 | src_addr += PAGE_SIZE; | |
637 | copied += PAGE_SIZE; | |
638 | ||
639 | if (fatal_signal_pending(current)) | |
640 | err = -EINTR; | |
641 | } | |
642 | if (err) | |
643 | break; | |
644 | } | |
645 | ||
b6ebaedb | 646 | out_unlock: |
d8ed45c5 | 647 | mmap_read_unlock(dst_mm); |
b6ebaedb AA |
648 | out: |
649 | if (page) | |
09cbfeaf | 650 | put_page(page); |
c1a4de99 AA |
651 | BUG_ON(copied < 0); |
652 | BUG_ON(err > 0); | |
653 | BUG_ON(!copied && !err); | |
654 | return copied ? copied : err; | |
655 | } | |
656 | ||
657 | ssize_t mcopy_atomic(struct mm_struct *dst_mm, unsigned long dst_start, | |
df2cc96e | 658 | unsigned long src_start, unsigned long len, |
a759a909 | 659 | atomic_t *mmap_changing, __u64 mode) |
c1a4de99 | 660 | { |
f6191471 AR |
661 | return __mcopy_atomic(dst_mm, dst_start, src_start, len, |
662 | MCOPY_ATOMIC_NORMAL, mmap_changing, mode); | |
c1a4de99 AA |
663 | } |
664 | ||
665 | ssize_t mfill_zeropage(struct mm_struct *dst_mm, unsigned long start, | |
a759a909 | 666 | unsigned long len, atomic_t *mmap_changing) |
c1a4de99 | 667 | { |
f6191471 AR |
668 | return __mcopy_atomic(dst_mm, start, 0, len, MCOPY_ATOMIC_ZEROPAGE, |
669 | mmap_changing, 0); | |
670 | } | |
671 | ||
672 | ssize_t mcopy_continue(struct mm_struct *dst_mm, unsigned long start, | |
a759a909 | 673 | unsigned long len, atomic_t *mmap_changing) |
f6191471 AR |
674 | { |
675 | return __mcopy_atomic(dst_mm, start, 0, len, MCOPY_ATOMIC_CONTINUE, | |
676 | mmap_changing, 0); | |
c1a4de99 | 677 | } |
ffd05793 SL |
678 | |
679 | int mwriteprotect_range(struct mm_struct *dst_mm, unsigned long start, | |
a759a909 NA |
680 | unsigned long len, bool enable_wp, |
681 | atomic_t *mmap_changing) | |
ffd05793 SL |
682 | { |
683 | struct vm_area_struct *dst_vma; | |
684 | pgprot_t newprot; | |
685 | int err; | |
686 | ||
687 | /* | |
688 | * Sanitize the command parameters: | |
689 | */ | |
690 | BUG_ON(start & ~PAGE_MASK); | |
691 | BUG_ON(len & ~PAGE_MASK); | |
692 | ||
693 | /* Does the address range wrap, or is the span zero-sized? */ | |
694 | BUG_ON(start + len <= start); | |
695 | ||
d8ed45c5 | 696 | mmap_read_lock(dst_mm); |
ffd05793 SL |
697 | |
698 | /* | |
699 | * If memory mappings are changing because of non-cooperative | |
700 | * operation (e.g. mremap) running in parallel, bail out and | |
701 | * request the user to retry later | |
702 | */ | |
703 | err = -EAGAIN; | |
a759a909 | 704 | if (mmap_changing && atomic_read(mmap_changing)) |
ffd05793 SL |
705 | goto out_unlock; |
706 | ||
707 | err = -ENOENT; | |
708 | dst_vma = find_dst_vma(dst_mm, start, len); | |
709 | /* | |
710 | * Make sure the vma is not shared, that the dst range is | |
711 | * both valid and fully within a single existing vma. | |
712 | */ | |
713 | if (!dst_vma || (dst_vma->vm_flags & VM_SHARED)) | |
714 | goto out_unlock; | |
715 | if (!userfaultfd_wp(dst_vma)) | |
716 | goto out_unlock; | |
717 | if (!vma_is_anonymous(dst_vma)) | |
718 | goto out_unlock; | |
719 | ||
720 | if (enable_wp) | |
721 | newprot = vm_get_page_prot(dst_vma->vm_flags & ~(VM_WRITE)); | |
722 | else | |
723 | newprot = vm_get_page_prot(dst_vma->vm_flags); | |
724 | ||
725 | change_protection(dst_vma, start, start + len, newprot, | |
726 | enable_wp ? MM_CP_UFFD_WP : MM_CP_UFFD_WP_RESOLVE); | |
727 | ||
728 | err = 0; | |
729 | out_unlock: | |
d8ed45c5 | 730 | mmap_read_unlock(dst_mm); |
ffd05793 SL |
731 | return err; |
732 | } |