x86: add tizen_qemu_x86_defconfig & tizen_qemu_x86_64_defconfig
[platform/kernel/linux-rpi.git] / mm / userfaultfd.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  *  mm/userfaultfd.c
4  *
5  *  Copyright (C) 2015  Red Hat, Inc.
6  */
7
8 #include <linux/mm.h>
9 #include <linux/sched/signal.h>
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>
16 #include <linux/hugetlb.h>
17 #include <linux/shmem_fs.h>
18 #include <asm/tlbflush.h>
19 #include "internal.h"
20
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
51 /*
52  * Install PTEs, to map dst_addr (within dst_vma) to page.
53  *
54  * This function handles both MCOPY_ATOMIC_NORMAL and _CONTINUE for both shmem
55  * and anon, and for both shared and private VMAs.
56  */
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)
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(page);
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
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,
127                             unsigned long src_addr,
128                             struct page **pagep,
129                             bool wp_copy)
130 {
131         void *page_kaddr;
132         int ret;
133         struct page *page;
134
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
147                 /* fallback to copy_from_user outside mmap_lock */
148                 if (unlikely(ret)) {
149                         ret = -ENOENT;
150                         *pagep = page;
151                         /* don't free the page */
152                         goto out;
153                 }
154
155                 flush_dcache_page(page);
156         } else {
157                 page = *pagep;
158                 *pagep = NULL;
159         }
160
161         /*
162          * The memory barrier inside __SetPageUptodate makes sure that
163          * preceding stores to the page contents become visible before
164          * the set_pte_at() write.
165          */
166         __SetPageUptodate(page);
167
168         ret = -ENOMEM;
169         if (mem_cgroup_charge(page, dst_mm, GFP_KERNEL))
170                 goto out_release;
171
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;
176 out:
177         return ret;
178 out_release:
179         put_page(page);
180         goto out;
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;
191         pgoff_t offset, max_off;
192         struct inode *inode;
193
194         _dst_pte = pte_mkspecial(pfn_pte(my_zero_pfn(dst_addr),
195                                          dst_vma->vm_page_prot));
196         dst_pte = pte_offset_map_lock(dst_mm, dst_pmd, dst_addr, &ptl);
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;
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
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
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;
234         if (ret)
235                 goto out;
236         if (!page) {
237                 ret = -EFAULT;
238                 goto out;
239         }
240
241         if (PageHWPoison(page)) {
242                 ret = -EIO;
243                 goto out_release;
244         }
245
246         ret = mfill_atomic_install_pte(dst_mm, dst_pmd, dst_vma, dst_addr,
247                                        page, false, wp_copy);
248         if (ret)
249                 goto out_release;
250
251         unlock_page(page);
252         ret = 0;
253 out:
254         return ret;
255 out_release:
256         unlock_page(page);
257         put_page(page);
258         goto out;
259 }
260
261 static pmd_t *mm_alloc_pmd(struct mm_struct *mm, unsigned long address)
262 {
263         pgd_t *pgd;
264         p4d_t *p4d;
265         pud_t *pud;
266
267         pgd = pgd_offset(mm, address);
268         p4d = p4d_alloc(mm, pgd, address);
269         if (!p4d)
270                 return NULL;
271         pud = pud_alloc(mm, p4d, address);
272         if (!pud)
273                 return NULL;
274         /*
275          * Note that we didn't run this because the pmd was
276          * missing, the *pmd may be already established and in
277          * turn it may also be a trans_huge_pmd.
278          */
279         return pmd_alloc(mm, pud, address);
280 }
281
282 #ifdef CONFIG_HUGETLB_PAGE
283 /*
284  * __mcopy_atomic processing for HUGETLB vmas.  Note that this routine is
285  * called with mmap_lock held, it will release mmap_lock before returning.
286  */
287 static __always_inline ssize_t __mcopy_atomic_hugetlb(struct mm_struct *dst_mm,
288                                               struct vm_area_struct *dst_vma,
289                                               unsigned long dst_start,
290                                               unsigned long src_start,
291                                               unsigned long len,
292                                               enum mcopy_atomic_mode mode)
293 {
294         int vm_shared = dst_vma->vm_flags & VM_SHARED;
295         ssize_t err;
296         pte_t *dst_pte;
297         unsigned long src_addr, dst_addr;
298         long copied;
299         struct page *page;
300         unsigned long vma_hpagesize;
301         pgoff_t idx;
302         u32 hash;
303         struct address_space *mapping;
304
305         /*
306          * There is no default zero huge page for all huge page sizes as
307          * supported by hugetlb.  A PMD_SIZE huge pages may exist as used
308          * by THP.  Since we can not reliably insert a zero page, this
309          * feature is not supported.
310          */
311         if (mode == MCOPY_ATOMIC_ZEROPAGE) {
312                 mmap_read_unlock(dst_mm);
313                 return -EINVAL;
314         }
315
316         src_addr = src_start;
317         dst_addr = dst_start;
318         copied = 0;
319         page = NULL;
320         vma_hpagesize = vma_kernel_pagesize(dst_vma);
321
322         /*
323          * Validate alignment based on huge page size
324          */
325         err = -EINVAL;
326         if (dst_start & (vma_hpagesize - 1) || len & (vma_hpagesize - 1))
327                 goto out_unlock;
328
329 retry:
330         /*
331          * On routine entry dst_vma is set.  If we had to drop mmap_lock and
332          * retry, dst_vma will be set to NULL and we must lookup again.
333          */
334         if (!dst_vma) {
335                 err = -ENOENT;
336                 dst_vma = find_dst_vma(dst_mm, dst_start, len);
337                 if (!dst_vma || !is_vm_hugetlb_page(dst_vma))
338                         goto out_unlock;
339
340                 err = -EINVAL;
341                 if (vma_hpagesize != vma_kernel_pagesize(dst_vma))
342                         goto out_unlock;
343
344                 vm_shared = dst_vma->vm_flags & VM_SHARED;
345         }
346
347         /*
348          * If not shared, ensure the dst_vma has a anon_vma.
349          */
350         err = -ENOMEM;
351         if (!vm_shared) {
352                 if (unlikely(anon_vma_prepare(dst_vma)))
353                         goto out_unlock;
354         }
355
356         while (src_addr < src_start + len) {
357                 BUG_ON(dst_addr >= dst_start + len);
358
359                 /*
360                  * Serialize via i_mmap_rwsem and hugetlb_fault_mutex.
361                  * i_mmap_rwsem ensures the dst_pte remains valid even
362                  * in the case of shared pmds.  fault mutex prevents
363                  * races with other faulting threads.
364                  */
365                 mapping = dst_vma->vm_file->f_mapping;
366                 i_mmap_lock_read(mapping);
367                 idx = linear_page_index(dst_vma, dst_addr);
368                 hash = hugetlb_fault_mutex_hash(mapping, idx);
369                 mutex_lock(&hugetlb_fault_mutex_table[hash]);
370
371                 err = -ENOMEM;
372                 dst_pte = huge_pte_alloc(dst_mm, dst_vma, dst_addr, vma_hpagesize);
373                 if (!dst_pte) {
374                         mutex_unlock(&hugetlb_fault_mutex_table[hash]);
375                         i_mmap_unlock_read(mapping);
376                         goto out_unlock;
377                 }
378
379                 if (mode != MCOPY_ATOMIC_CONTINUE &&
380                     !huge_pte_none(huge_ptep_get(dst_pte))) {
381                         err = -EEXIST;
382                         mutex_unlock(&hugetlb_fault_mutex_table[hash]);
383                         i_mmap_unlock_read(mapping);
384                         goto out_unlock;
385                 }
386
387                 err = hugetlb_mcopy_atomic_pte(dst_mm, dst_pte, dst_vma,
388                                                dst_addr, src_addr, mode, &page);
389
390                 mutex_unlock(&hugetlb_fault_mutex_table[hash]);
391                 i_mmap_unlock_read(mapping);
392
393                 cond_resched();
394
395                 if (unlikely(err == -ENOENT)) {
396                         mmap_read_unlock(dst_mm);
397                         BUG_ON(!page);
398
399                         err = copy_huge_page_from_user(page,
400                                                 (const void __user *)src_addr,
401                                                 vma_hpagesize / PAGE_SIZE,
402                                                 true);
403                         if (unlikely(err)) {
404                                 err = -EFAULT;
405                                 goto out;
406                         }
407                         mmap_read_lock(dst_mm);
408
409                         dst_vma = NULL;
410                         goto retry;
411                 } else
412                         BUG_ON(page);
413
414                 if (!err) {
415                         dst_addr += vma_hpagesize;
416                         src_addr += vma_hpagesize;
417                         copied += vma_hpagesize;
418
419                         if (fatal_signal_pending(current))
420                                 err = -EINTR;
421                 }
422                 if (err)
423                         break;
424         }
425
426 out_unlock:
427         mmap_read_unlock(dst_mm);
428 out:
429         if (page)
430                 put_page(page);
431         BUG_ON(copied < 0);
432         BUG_ON(err > 0);
433         BUG_ON(!copied && !err);
434         return copied ? copied : err;
435 }
436 #else /* !CONFIG_HUGETLB_PAGE */
437 /* fail at build time if gcc attempts to use this */
438 extern ssize_t __mcopy_atomic_hugetlb(struct mm_struct *dst_mm,
439                                       struct vm_area_struct *dst_vma,
440                                       unsigned long dst_start,
441                                       unsigned long src_start,
442                                       unsigned long len,
443                                       enum mcopy_atomic_mode mode);
444 #endif /* CONFIG_HUGETLB_PAGE */
445
446 static __always_inline ssize_t mfill_atomic_pte(struct mm_struct *dst_mm,
447                                                 pmd_t *dst_pmd,
448                                                 struct vm_area_struct *dst_vma,
449                                                 unsigned long dst_addr,
450                                                 unsigned long src_addr,
451                                                 struct page **page,
452                                                 enum mcopy_atomic_mode mode,
453                                                 bool wp_copy)
454 {
455         ssize_t err;
456
457         if (mode == MCOPY_ATOMIC_CONTINUE) {
458                 return mcontinue_atomic_pte(dst_mm, dst_pmd, dst_vma, dst_addr,
459                                             wp_copy);
460         }
461
462         /*
463          * The normal page fault path for a shmem will invoke the
464          * fault, fill the hole in the file and COW it right away. The
465          * result generates plain anonymous memory. So when we are
466          * asked to fill an hole in a MAP_PRIVATE shmem mapping, we'll
467          * generate anonymous memory directly without actually filling
468          * the hole. For the MAP_PRIVATE case the robustness check
469          * only happens in the pagetable (to verify it's still none)
470          * and not in the radix tree.
471          */
472         if (!(dst_vma->vm_flags & VM_SHARED)) {
473                 if (mode == MCOPY_ATOMIC_NORMAL)
474                         err = mcopy_atomic_pte(dst_mm, dst_pmd, dst_vma,
475                                                dst_addr, src_addr, page,
476                                                wp_copy);
477                 else
478                         err = mfill_zeropage_pte(dst_mm, dst_pmd,
479                                                  dst_vma, dst_addr);
480         } else {
481                 VM_WARN_ON_ONCE(wp_copy);
482                 err = shmem_mfill_atomic_pte(dst_mm, dst_pmd, dst_vma,
483                                              dst_addr, src_addr,
484                                              mode != MCOPY_ATOMIC_NORMAL,
485                                              page);
486         }
487
488         return err;
489 }
490
491 static __always_inline ssize_t __mcopy_atomic(struct mm_struct *dst_mm,
492                                               unsigned long dst_start,
493                                               unsigned long src_start,
494                                               unsigned long len,
495                                               enum mcopy_atomic_mode mcopy_mode,
496                                               atomic_t *mmap_changing,
497                                               __u64 mode)
498 {
499         struct vm_area_struct *dst_vma;
500         ssize_t err;
501         pmd_t *dst_pmd;
502         unsigned long src_addr, dst_addr;
503         long copied;
504         struct page *page;
505         bool wp_copy;
506
507         /*
508          * Sanitize the command parameters:
509          */
510         BUG_ON(dst_start & ~PAGE_MASK);
511         BUG_ON(len & ~PAGE_MASK);
512
513         /* Does the address range wrap, or is the span zero-sized? */
514         BUG_ON(src_start + len <= src_start);
515         BUG_ON(dst_start + len <= dst_start);
516
517         src_addr = src_start;
518         dst_addr = dst_start;
519         copied = 0;
520         page = NULL;
521 retry:
522         mmap_read_lock(dst_mm);
523
524         /*
525          * If memory mappings are changing because of non-cooperative
526          * operation (e.g. mremap) running in parallel, bail out and
527          * request the user to retry later
528          */
529         err = -EAGAIN;
530         if (mmap_changing && atomic_read(mmap_changing))
531                 goto out_unlock;
532
533         /*
534          * Make sure the vma is not shared, that the dst range is
535          * both valid and fully within a single existing vma.
536          */
537         err = -ENOENT;
538         dst_vma = find_dst_vma(dst_mm, dst_start, len);
539         if (!dst_vma)
540                 goto out_unlock;
541
542         err = -EINVAL;
543         /*
544          * shmem_zero_setup is invoked in mmap for MAP_ANONYMOUS|MAP_SHARED but
545          * it will overwrite vm_ops, so vma_is_anonymous must return false.
546          */
547         if (WARN_ON_ONCE(vma_is_anonymous(dst_vma) &&
548             dst_vma->vm_flags & VM_SHARED))
549                 goto out_unlock;
550
551         /*
552          * validate 'mode' now that we know the dst_vma: don't allow
553          * a wrprotect copy if the userfaultfd didn't register as WP.
554          */
555         wp_copy = mode & UFFDIO_COPY_MODE_WP;
556         if (wp_copy && !(dst_vma->vm_flags & VM_UFFD_WP))
557                 goto out_unlock;
558
559         /*
560          * If this is a HUGETLB vma, pass off to appropriate routine
561          */
562         if (is_vm_hugetlb_page(dst_vma))
563                 return  __mcopy_atomic_hugetlb(dst_mm, dst_vma, dst_start,
564                                                 src_start, len, mcopy_mode);
565
566         if (!vma_is_anonymous(dst_vma) && !vma_is_shmem(dst_vma))
567                 goto out_unlock;
568         if (!vma_is_shmem(dst_vma) && mcopy_mode == MCOPY_ATOMIC_CONTINUE)
569                 goto out_unlock;
570
571         /*
572          * Ensure the dst_vma has a anon_vma or this page
573          * would get a NULL anon_vma when moved in the
574          * dst_vma.
575          */
576         err = -ENOMEM;
577         if (!(dst_vma->vm_flags & VM_SHARED) &&
578             unlikely(anon_vma_prepare(dst_vma)))
579                 goto out_unlock;
580
581         while (src_addr < src_start + len) {
582                 pmd_t dst_pmdval;
583
584                 BUG_ON(dst_addr >= dst_start + len);
585
586                 dst_pmd = mm_alloc_pmd(dst_mm, dst_addr);
587                 if (unlikely(!dst_pmd)) {
588                         err = -ENOMEM;
589                         break;
590                 }
591
592                 dst_pmdval = pmd_read_atomic(dst_pmd);
593                 /*
594                  * If the dst_pmd is mapped as THP don't
595                  * override it and just be strict.
596                  */
597                 if (unlikely(pmd_trans_huge(dst_pmdval))) {
598                         err = -EEXIST;
599                         break;
600                 }
601                 if (unlikely(pmd_none(dst_pmdval)) &&
602                     unlikely(__pte_alloc(dst_mm, dst_pmd))) {
603                         err = -ENOMEM;
604                         break;
605                 }
606                 /* If an huge pmd materialized from under us fail */
607                 if (unlikely(pmd_trans_huge(*dst_pmd))) {
608                         err = -EFAULT;
609                         break;
610                 }
611
612                 BUG_ON(pmd_none(*dst_pmd));
613                 BUG_ON(pmd_trans_huge(*dst_pmd));
614
615                 err = mfill_atomic_pte(dst_mm, dst_pmd, dst_vma, dst_addr,
616                                        src_addr, &page, mcopy_mode, wp_copy);
617                 cond_resched();
618
619                 if (unlikely(err == -ENOENT)) {
620                         void *page_kaddr;
621
622                         mmap_read_unlock(dst_mm);
623                         BUG_ON(!page);
624
625                         page_kaddr = kmap(page);
626                         err = copy_from_user(page_kaddr,
627                                              (const void __user *) src_addr,
628                                              PAGE_SIZE);
629                         kunmap(page);
630                         if (unlikely(err)) {
631                                 err = -EFAULT;
632                                 goto out;
633                         }
634                         flush_dcache_page(page);
635                         goto retry;
636                 } else
637                         BUG_ON(page);
638
639                 if (!err) {
640                         dst_addr += PAGE_SIZE;
641                         src_addr += PAGE_SIZE;
642                         copied += PAGE_SIZE;
643
644                         if (fatal_signal_pending(current))
645                                 err = -EINTR;
646                 }
647                 if (err)
648                         break;
649         }
650
651 out_unlock:
652         mmap_read_unlock(dst_mm);
653 out:
654         if (page)
655                 put_page(page);
656         BUG_ON(copied < 0);
657         BUG_ON(err > 0);
658         BUG_ON(!copied && !err);
659         return copied ? copied : err;
660 }
661
662 ssize_t mcopy_atomic(struct mm_struct *dst_mm, unsigned long dst_start,
663                      unsigned long src_start, unsigned long len,
664                      atomic_t *mmap_changing, __u64 mode)
665 {
666         return __mcopy_atomic(dst_mm, dst_start, src_start, len,
667                               MCOPY_ATOMIC_NORMAL, mmap_changing, mode);
668 }
669
670 ssize_t mfill_zeropage(struct mm_struct *dst_mm, unsigned long start,
671                        unsigned long len, atomic_t *mmap_changing)
672 {
673         return __mcopy_atomic(dst_mm, start, 0, len, MCOPY_ATOMIC_ZEROPAGE,
674                               mmap_changing, 0);
675 }
676
677 ssize_t mcopy_continue(struct mm_struct *dst_mm, unsigned long start,
678                        unsigned long len, atomic_t *mmap_changing)
679 {
680         return __mcopy_atomic(dst_mm, start, 0, len, MCOPY_ATOMIC_CONTINUE,
681                               mmap_changing, 0);
682 }
683
684 int mwriteprotect_range(struct mm_struct *dst_mm, unsigned long start,
685                         unsigned long len, bool enable_wp,
686                         atomic_t *mmap_changing)
687 {
688         struct vm_area_struct *dst_vma;
689         pgprot_t newprot;
690         int err;
691
692         /*
693          * Sanitize the command parameters:
694          */
695         BUG_ON(start & ~PAGE_MASK);
696         BUG_ON(len & ~PAGE_MASK);
697
698         /* Does the address range wrap, or is the span zero-sized? */
699         BUG_ON(start + len <= start);
700
701         mmap_read_lock(dst_mm);
702
703         /*
704          * If memory mappings are changing because of non-cooperative
705          * operation (e.g. mremap) running in parallel, bail out and
706          * request the user to retry later
707          */
708         err = -EAGAIN;
709         if (mmap_changing && atomic_read(mmap_changing))
710                 goto out_unlock;
711
712         err = -ENOENT;
713         dst_vma = find_dst_vma(dst_mm, start, len);
714         /*
715          * Make sure the vma is not shared, that the dst range is
716          * both valid and fully within a single existing vma.
717          */
718         if (!dst_vma || (dst_vma->vm_flags & VM_SHARED))
719                 goto out_unlock;
720         if (!userfaultfd_wp(dst_vma))
721                 goto out_unlock;
722         if (!vma_is_anonymous(dst_vma))
723                 goto out_unlock;
724
725         if (enable_wp)
726                 newprot = vm_get_page_prot(dst_vma->vm_flags & ~(VM_WRITE));
727         else
728                 newprot = vm_get_page_prot(dst_vma->vm_flags);
729
730         change_protection(dst_vma, start, start + len, newprot,
731                           enable_wp ? MM_CP_UFFD_WP : MM_CP_UFFD_WP_RESOLVE);
732
733         err = 0;
734 out_unlock:
735         mmap_read_unlock(dst_mm);
736         return err;
737 }