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