blob: ac6f036298cd6fba72a69e0f588a3ffbb17d6584 [file] [log] [blame]
Thomas Gleixner20c8ccb2019-06-04 10:11:32 +02001// SPDX-License-Identifier: GPL-2.0-only
Andrea Arcangelic1a4de92015-09-04 15:47:04 -07002/*
3 * mm/userfaultfd.c
4 *
5 * Copyright (C) 2015 Red Hat, Inc.
Andrea Arcangelic1a4de92015-09-04 15:47:04 -07006 */
7
8#include <linux/mm.h>
Ingo Molnar174cd4b2017-02-02 19:15:33 +01009#include <linux/sched/signal.h>
Andrea Arcangelic1a4de92015-09-04 15:47:04 -070010#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>
Mike Kravetz60d4d2d2017-02-22 15:42:55 -080016#include <linux/hugetlb.h>
Mike Rapoport26071ce2017-02-22 15:43:34 -080017#include <linux/shmem_fs.h>
Andrea Arcangelic1a4de92015-09-04 15:47:04 -070018#include <asm/tlbflush.h>
19#include "internal.h"
20
Wei Yang643aa362019-11-30 17:57:55 -080021static __always_inline
22struct 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
Axel Rasmussen15313252021-06-30 18:49:24 -070051/*
52 * Install PTEs, to map dst_addr (within dst_vma) to page.
53 *
Axel Rasmussen7d64ae32021-06-30 18:49:31 -070054 * This function handles both MCOPY_ATOMIC_NORMAL and _CONTINUE for both shmem
55 * and anon, and for both shared and private VMAs.
Axel Rasmussen15313252021-06-30 18:49:24 -070056 */
Axel Rasmussen7d64ae32021-06-30 18:49:31 -070057int 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)
Axel Rasmussen15313252021-06-30 18:49:24 -070061{
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);
Peter Xu9ae0f872021-11-05 13:38:24 -070072 _dst_pte = pte_mkdirty(_dst_pte);
Axel Rasmussen15313252021-06-30 18:49:24 -070073 if (page_in_cache && !vm_shared)
74 writable = false;
Axel Rasmussen15313252021-06-30 18:49:24 -070075 if (writable) {
76 if (wp_copy)
77 _dst_pte = pte_mkuffd_wp(_dst_pte);
78 else
79 _dst_pte = pte_mkwrite(_dst_pte);
80 }
81
82 dst_pte = pte_offset_map_lock(dst_mm, dst_pmd, dst_addr, &ptl);
83
84 if (vma_is_shmem(dst_vma)) {
85 /* serialize against truncate with the page table lock */
86 inode = dst_vma->vm_file->f_inode;
87 offset = linear_page_index(dst_vma, dst_addr);
88 max_off = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE);
89 ret = -EFAULT;
90 if (unlikely(offset >= max_off))
91 goto out_unlock;
92 }
93
94 ret = -EEXIST;
95 if (!pte_none(*dst_pte))
96 goto out_unlock;
97
98 if (page_in_cache)
99 page_add_file_rmap(page, false);
100 else
101 page_add_new_anon_rmap(page, dst_vma, dst_addr, false);
102
103 /*
104 * Must happen after rmap, as mm_counter() checks mapping (via
105 * PageAnon()), which is set by __page_set_anon_rmap().
106 */
107 inc_mm_counter(dst_mm, mm_counter(page));
108
109 if (newly_allocated)
110 lru_cache_add_inactive_or_unevictable(page, dst_vma);
111
112 set_pte_at(dst_mm, dst_addr, dst_pte, _dst_pte);
113
114 /* No need to invalidate - it was non-present before */
115 update_mmu_cache(dst_vma, dst_addr, dst_pte);
116 ret = 0;
117out_unlock:
118 pte_unmap_unlock(dst_pte, ptl);
119 return ret;
120}
121
Andrea Arcangelic1a4de92015-09-04 15:47:04 -0700122static int mcopy_atomic_pte(struct mm_struct *dst_mm,
123 pmd_t *dst_pmd,
124 struct vm_area_struct *dst_vma,
125 unsigned long dst_addr,
Andrea Arcangelib6ebaed2015-09-04 15:47:08 -0700126 unsigned long src_addr,
Andrea Arcangeli72981e02020-04-06 20:05:41 -0700127 struct page **pagep,
128 bool wp_copy)
Andrea Arcangelic1a4de92015-09-04 15:47:04 -0700129{
Andrea Arcangelic1a4de92015-09-04 15:47:04 -0700130 void *page_kaddr;
131 int ret;
Andrea Arcangelib6ebaed2015-09-04 15:47:08 -0700132 struct page *page;
Andrea Arcangelic1a4de92015-09-04 15:47:04 -0700133
Andrea Arcangelib6ebaed2015-09-04 15:47:08 -0700134 if (!*pagep) {
135 ret = -ENOMEM;
136 page = alloc_page_vma(GFP_HIGHUSER_MOVABLE, dst_vma, dst_addr);
137 if (!page)
138 goto out;
Andrea Arcangelic1a4de92015-09-04 15:47:04 -0700139
Andrea Arcangelib6ebaed2015-09-04 15:47:08 -0700140 page_kaddr = kmap_atomic(page);
141 ret = copy_from_user(page_kaddr,
142 (const void __user *) src_addr,
143 PAGE_SIZE);
144 kunmap_atomic(page_kaddr);
145
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -0700146 /* fallback to copy_from_user outside mmap_lock */
Andrea Arcangelib6ebaed2015-09-04 15:47:08 -0700147 if (unlikely(ret)) {
Andrea Arcangeli9e368252018-11-30 14:09:25 -0800148 ret = -ENOENT;
Andrea Arcangelib6ebaed2015-09-04 15:47:08 -0700149 *pagep = page;
150 /* don't free the page */
151 goto out;
152 }
153 } else {
154 page = *pagep;
155 *pagep = NULL;
156 }
Andrea Arcangelic1a4de92015-09-04 15:47:04 -0700157
158 /*
159 * The memory barrier inside __SetPageUptodate makes sure that
Wei Yangf4f53292019-11-30 17:58:17 -0800160 * preceding stores to the page contents become visible before
Andrea Arcangelic1a4de92015-09-04 15:47:04 -0700161 * the set_pte_at() write.
162 */
163 __SetPageUptodate(page);
164
165 ret = -ENOMEM;
Matthew Wilcox (Oracle)8f425e42021-06-25 09:27:04 -0400166 if (mem_cgroup_charge(page_folio(page), dst_mm, GFP_KERNEL))
Andrea Arcangelic1a4de92015-09-04 15:47:04 -0700167 goto out_release;
168
Axel Rasmussen15313252021-06-30 18:49:24 -0700169 ret = mfill_atomic_install_pte(dst_mm, dst_pmd, dst_vma, dst_addr,
170 page, true, wp_copy);
171 if (ret)
172 goto out_release;
Andrea Arcangelic1a4de92015-09-04 15:47:04 -0700173out:
174 return ret;
Andrea Arcangelic1a4de92015-09-04 15:47:04 -0700175out_release:
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300176 put_page(page);
Andrea Arcangelic1a4de92015-09-04 15:47:04 -0700177 goto out;
Andrea Arcangelic1a4de92015-09-04 15:47:04 -0700178}
179
180static int mfill_zeropage_pte(struct mm_struct *dst_mm,
181 pmd_t *dst_pmd,
182 struct vm_area_struct *dst_vma,
183 unsigned long dst_addr)
184{
185 pte_t _dst_pte, *dst_pte;
186 spinlock_t *ptl;
187 int ret;
Andrea Arcangelie2a50c12018-11-30 14:09:37 -0800188 pgoff_t offset, max_off;
189 struct inode *inode;
Andrea Arcangelic1a4de92015-09-04 15:47:04 -0700190
191 _dst_pte = pte_mkspecial(pfn_pte(my_zero_pfn(dst_addr),
192 dst_vma->vm_page_prot));
Andrea Arcangelic1a4de92015-09-04 15:47:04 -0700193 dst_pte = pte_offset_map_lock(dst_mm, dst_pmd, dst_addr, &ptl);
Andrea Arcangelie2a50c12018-11-30 14:09:37 -0800194 if (dst_vma->vm_file) {
195 /* the shmem MAP_PRIVATE case requires checking the i_size */
196 inode = dst_vma->vm_file->f_inode;
197 offset = linear_page_index(dst_vma, dst_addr);
198 max_off = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE);
199 ret = -EFAULT;
200 if (unlikely(offset >= max_off))
201 goto out_unlock;
202 }
203 ret = -EEXIST;
Andrea Arcangelic1a4de92015-09-04 15:47:04 -0700204 if (!pte_none(*dst_pte))
205 goto out_unlock;
206 set_pte_at(dst_mm, dst_addr, dst_pte, _dst_pte);
207 /* No need to invalidate - it was non-present before */
208 update_mmu_cache(dst_vma, dst_addr, dst_pte);
209 ret = 0;
210out_unlock:
211 pte_unmap_unlock(dst_pte, ptl);
212 return ret;
213}
214
Axel Rasmussen15313252021-06-30 18:49:24 -0700215/* Handles UFFDIO_CONTINUE for all shmem VMAs (shared or private). */
216static int mcontinue_atomic_pte(struct mm_struct *dst_mm,
217 pmd_t *dst_pmd,
218 struct vm_area_struct *dst_vma,
219 unsigned long dst_addr,
220 bool wp_copy)
221{
222 struct inode *inode = file_inode(dst_vma->vm_file);
223 pgoff_t pgoff = linear_page_index(dst_vma, dst_addr);
224 struct page *page;
225 int ret;
226
227 ret = shmem_getpage(inode, pgoff, &page, SGP_READ);
228 if (ret)
229 goto out;
230 if (!page) {
231 ret = -EFAULT;
232 goto out;
233 }
234
235 ret = mfill_atomic_install_pte(dst_mm, dst_pmd, dst_vma, dst_addr,
236 page, false, wp_copy);
237 if (ret)
238 goto out_release;
239
240 unlock_page(page);
241 ret = 0;
242out:
243 return ret;
244out_release:
245 unlock_page(page);
246 put_page(page);
247 goto out;
248}
249
Andrea Arcangelic1a4de92015-09-04 15:47:04 -0700250static pmd_t *mm_alloc_pmd(struct mm_struct *mm, unsigned long address)
251{
252 pgd_t *pgd;
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +0300253 p4d_t *p4d;
Andrea Arcangelic1a4de92015-09-04 15:47:04 -0700254 pud_t *pud;
Andrea Arcangelic1a4de92015-09-04 15:47:04 -0700255
256 pgd = pgd_offset(mm, address);
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +0300257 p4d = p4d_alloc(mm, pgd, address);
258 if (!p4d)
259 return NULL;
260 pud = pud_alloc(mm, p4d, address);
261 if (!pud)
262 return NULL;
263 /*
264 * Note that we didn't run this because the pmd was
265 * missing, the *pmd may be already established and in
266 * turn it may also be a trans_huge_pmd.
267 */
268 return pmd_alloc(mm, pud, address);
Andrea Arcangelic1a4de92015-09-04 15:47:04 -0700269}
270
Mike Kravetz60d4d2d2017-02-22 15:42:55 -0800271#ifdef CONFIG_HUGETLB_PAGE
272/*
273 * __mcopy_atomic processing for HUGETLB vmas. Note that this routine is
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -0700274 * called with mmap_lock held, it will release mmap_lock before returning.
Mike Kravetz60d4d2d2017-02-22 15:42:55 -0800275 */
276static __always_inline ssize_t __mcopy_atomic_hugetlb(struct mm_struct *dst_mm,
277 struct vm_area_struct *dst_vma,
278 unsigned long dst_start,
279 unsigned long src_start,
280 unsigned long len,
Axel Rasmussenf6191472021-05-04 18:35:49 -0700281 enum mcopy_atomic_mode mode)
Mike Kravetz60d4d2d2017-02-22 15:42:55 -0800282{
Mike Kravetz1c9e8de2017-02-22 15:43:43 -0800283 int vm_shared = dst_vma->vm_flags & VM_SHARED;
Mike Kravetz60d4d2d2017-02-22 15:42:55 -0800284 ssize_t err;
285 pte_t *dst_pte;
286 unsigned long src_addr, dst_addr;
287 long copied;
288 struct page *page;
Mike Kravetz60d4d2d2017-02-22 15:42:55 -0800289 unsigned long vma_hpagesize;
290 pgoff_t idx;
291 u32 hash;
292 struct address_space *mapping;
293
294 /*
295 * There is no default zero huge page for all huge page sizes as
296 * supported by hugetlb. A PMD_SIZE huge pages may exist as used
297 * by THP. Since we can not reliably insert a zero page, this
298 * feature is not supported.
299 */
Axel Rasmussenf6191472021-05-04 18:35:49 -0700300 if (mode == MCOPY_ATOMIC_ZEROPAGE) {
Michel Lespinassed8ed45c2020-06-08 21:33:25 -0700301 mmap_read_unlock(dst_mm);
Mike Kravetz60d4d2d2017-02-22 15:42:55 -0800302 return -EINVAL;
303 }
304
305 src_addr = src_start;
306 dst_addr = dst_start;
307 copied = 0;
308 page = NULL;
309 vma_hpagesize = vma_kernel_pagesize(dst_vma);
310
311 /*
312 * Validate alignment based on huge page size
313 */
314 err = -EINVAL;
315 if (dst_start & (vma_hpagesize - 1) || len & (vma_hpagesize - 1))
316 goto out_unlock;
317
318retry:
319 /*
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -0700320 * On routine entry dst_vma is set. If we had to drop mmap_lock and
Mike Kravetz60d4d2d2017-02-22 15:42:55 -0800321 * retry, dst_vma will be set to NULL and we must lookup again.
322 */
323 if (!dst_vma) {
Mike Rapoport27d02562017-02-24 14:58:28 -0800324 err = -ENOENT;
Wei Yang643aa362019-11-30 17:57:55 -0800325 dst_vma = find_dst_vma(dst_mm, dst_start, len);
Mike Kravetz60d4d2d2017-02-22 15:42:55 -0800326 if (!dst_vma || !is_vm_hugetlb_page(dst_vma))
327 goto out_unlock;
Mike Kravetz1c9e8de2017-02-22 15:43:43 -0800328
Mike Rapoport27d02562017-02-24 14:58:28 -0800329 err = -EINVAL;
330 if (vma_hpagesize != vma_kernel_pagesize(dst_vma))
331 goto out_unlock;
332
Mike Kravetz1c9e8de2017-02-22 15:43:43 -0800333 vm_shared = dst_vma->vm_flags & VM_SHARED;
Mike Kravetz60d4d2d2017-02-22 15:42:55 -0800334 }
335
Mike Kravetz60d4d2d2017-02-22 15:42:55 -0800336 /*
Mike Kravetz1c9e8de2017-02-22 15:43:43 -0800337 * If not shared, ensure the dst_vma has a anon_vma.
Mike Kravetz60d4d2d2017-02-22 15:42:55 -0800338 */
339 err = -ENOMEM;
Mike Kravetz1c9e8de2017-02-22 15:43:43 -0800340 if (!vm_shared) {
341 if (unlikely(anon_vma_prepare(dst_vma)))
342 goto out_unlock;
343 }
Mike Kravetz60d4d2d2017-02-22 15:42:55 -0800344
Mike Kravetz60d4d2d2017-02-22 15:42:55 -0800345 while (src_addr < src_start + len) {
Mike Kravetz60d4d2d2017-02-22 15:42:55 -0800346 BUG_ON(dst_addr >= dst_start + len);
Mike Kravetz60d4d2d2017-02-22 15:42:55 -0800347
348 /*
Mike Kravetzc0d03812020-04-01 21:11:05 -0700349 * Serialize via i_mmap_rwsem and hugetlb_fault_mutex.
350 * i_mmap_rwsem ensures the dst_pte remains valid even
351 * in the case of shared pmds. fault mutex prevents
352 * races with other faulting threads.
Mike Kravetz60d4d2d2017-02-22 15:42:55 -0800353 */
Mike Kravetzddeaab32019-01-08 15:23:36 -0800354 mapping = dst_vma->vm_file->f_mapping;
Mike Kravetzc0d03812020-04-01 21:11:05 -0700355 i_mmap_lock_read(mapping);
356 idx = linear_page_index(dst_vma, dst_addr);
Wei Yang188b04a2019-11-30 17:57:02 -0800357 hash = hugetlb_fault_mutex_hash(mapping, idx);
Mike Kravetz60d4d2d2017-02-22 15:42:55 -0800358 mutex_lock(&hugetlb_fault_mutex_table[hash]);
359
360 err = -ENOMEM;
Peter Xuaec44e02021-05-04 18:33:00 -0700361 dst_pte = huge_pte_alloc(dst_mm, dst_vma, dst_addr, vma_hpagesize);
Mike Kravetz60d4d2d2017-02-22 15:42:55 -0800362 if (!dst_pte) {
363 mutex_unlock(&hugetlb_fault_mutex_table[hash]);
Mike Kravetzc0d03812020-04-01 21:11:05 -0700364 i_mmap_unlock_read(mapping);
Mike Kravetz60d4d2d2017-02-22 15:42:55 -0800365 goto out_unlock;
366 }
367
Axel Rasmussenf6191472021-05-04 18:35:49 -0700368 if (mode != MCOPY_ATOMIC_CONTINUE &&
369 !huge_pte_none(huge_ptep_get(dst_pte))) {
370 err = -EEXIST;
Mike Kravetz60d4d2d2017-02-22 15:42:55 -0800371 mutex_unlock(&hugetlb_fault_mutex_table[hash]);
Mike Kravetzc0d03812020-04-01 21:11:05 -0700372 i_mmap_unlock_read(mapping);
Mike Kravetz60d4d2d2017-02-22 15:42:55 -0800373 goto out_unlock;
374 }
375
376 err = hugetlb_mcopy_atomic_pte(dst_mm, dst_pte, dst_vma,
Axel Rasmussenf6191472021-05-04 18:35:49 -0700377 dst_addr, src_addr, mode, &page);
Mike Kravetz60d4d2d2017-02-22 15:42:55 -0800378
379 mutex_unlock(&hugetlb_fault_mutex_table[hash]);
Mike Kravetzc0d03812020-04-01 21:11:05 -0700380 i_mmap_unlock_read(mapping);
Mike Kravetz60d4d2d2017-02-22 15:42:55 -0800381
382 cond_resched();
383
Andrea Arcangeli9e368252018-11-30 14:09:25 -0800384 if (unlikely(err == -ENOENT)) {
Michel Lespinassed8ed45c2020-06-08 21:33:25 -0700385 mmap_read_unlock(dst_mm);
Mike Kravetz60d4d2d2017-02-22 15:42:55 -0800386 BUG_ON(!page);
387
388 err = copy_huge_page_from_user(page,
389 (const void __user *)src_addr,
Wei Yang4fb07ee2019-11-30 17:57:49 -0800390 vma_hpagesize / PAGE_SIZE,
391 true);
Mike Kravetz60d4d2d2017-02-22 15:42:55 -0800392 if (unlikely(err)) {
393 err = -EFAULT;
394 goto out;
395 }
Michel Lespinassed8ed45c2020-06-08 21:33:25 -0700396 mmap_read_lock(dst_mm);
Mike Kravetz60d4d2d2017-02-22 15:42:55 -0800397
398 dst_vma = NULL;
399 goto retry;
400 } else
401 BUG_ON(page);
402
403 if (!err) {
404 dst_addr += vma_hpagesize;
405 src_addr += vma_hpagesize;
406 copied += vma_hpagesize;
407
408 if (fatal_signal_pending(current))
409 err = -EINTR;
410 }
411 if (err)
412 break;
413 }
414
415out_unlock:
Michel Lespinassed8ed45c2020-06-08 21:33:25 -0700416 mmap_read_unlock(dst_mm);
Mike Kravetz60d4d2d2017-02-22 15:42:55 -0800417out:
Mina Almasry8cc5fcb2021-06-30 18:48:19 -0700418 if (page)
Mike Kravetz60d4d2d2017-02-22 15:42:55 -0800419 put_page(page);
420 BUG_ON(copied < 0);
421 BUG_ON(err > 0);
422 BUG_ON(!copied && !err);
423 return copied ? copied : err;
424}
425#else /* !CONFIG_HUGETLB_PAGE */
426/* fail at build time if gcc attempts to use this */
427extern ssize_t __mcopy_atomic_hugetlb(struct mm_struct *dst_mm,
428 struct vm_area_struct *dst_vma,
429 unsigned long dst_start,
430 unsigned long src_start,
431 unsigned long len,
Axel Rasmussenf6191472021-05-04 18:35:49 -0700432 enum mcopy_atomic_mode mode);
Mike Kravetz60d4d2d2017-02-22 15:42:55 -0800433#endif /* CONFIG_HUGETLB_PAGE */
434
Mike Rapoport3217d3c2017-09-06 16:23:06 -0700435static __always_inline ssize_t mfill_atomic_pte(struct mm_struct *dst_mm,
436 pmd_t *dst_pmd,
437 struct vm_area_struct *dst_vma,
438 unsigned long dst_addr,
439 unsigned long src_addr,
440 struct page **page,
Axel Rasmussen15313252021-06-30 18:49:24 -0700441 enum mcopy_atomic_mode mode,
Andrea Arcangeli72981e02020-04-06 20:05:41 -0700442 bool wp_copy)
Mike Rapoport3217d3c2017-09-06 16:23:06 -0700443{
444 ssize_t err;
445
Axel Rasmussen15313252021-06-30 18:49:24 -0700446 if (mode == MCOPY_ATOMIC_CONTINUE) {
447 return mcontinue_atomic_pte(dst_mm, dst_pmd, dst_vma, dst_addr,
448 wp_copy);
449 }
450
Andrea Arcangeli5b510722018-11-30 14:09:28 -0800451 /*
452 * The normal page fault path for a shmem will invoke the
453 * fault, fill the hole in the file and COW it right away. The
454 * result generates plain anonymous memory. So when we are
455 * asked to fill an hole in a MAP_PRIVATE shmem mapping, we'll
456 * generate anonymous memory directly without actually filling
457 * the hole. For the MAP_PRIVATE case the robustness check
458 * only happens in the pagetable (to verify it's still none)
459 * and not in the radix tree.
460 */
461 if (!(dst_vma->vm_flags & VM_SHARED)) {
Axel Rasmussen15313252021-06-30 18:49:24 -0700462 if (mode == MCOPY_ATOMIC_NORMAL)
Mike Rapoport3217d3c2017-09-06 16:23:06 -0700463 err = mcopy_atomic_pte(dst_mm, dst_pmd, dst_vma,
Andrea Arcangeli72981e02020-04-06 20:05:41 -0700464 dst_addr, src_addr, page,
465 wp_copy);
Mike Rapoport3217d3c2017-09-06 16:23:06 -0700466 else
467 err = mfill_zeropage_pte(dst_mm, dst_pmd,
468 dst_vma, dst_addr);
469 } else {
Andrea Arcangeli72981e02020-04-06 20:05:41 -0700470 VM_WARN_ON_ONCE(wp_copy);
Axel Rasmussen3460f6e2021-06-30 18:49:17 -0700471 err = shmem_mfill_atomic_pte(dst_mm, dst_pmd, dst_vma,
Axel Rasmussen15313252021-06-30 18:49:24 -0700472 dst_addr, src_addr,
473 mode != MCOPY_ATOMIC_NORMAL,
Axel Rasmussen3460f6e2021-06-30 18:49:17 -0700474 page);
Mike Rapoport3217d3c2017-09-06 16:23:06 -0700475 }
476
477 return err;
478}
479
Andrea Arcangelic1a4de92015-09-04 15:47:04 -0700480static __always_inline ssize_t __mcopy_atomic(struct mm_struct *dst_mm,
481 unsigned long dst_start,
482 unsigned long src_start,
483 unsigned long len,
Axel Rasmussenf6191472021-05-04 18:35:49 -0700484 enum mcopy_atomic_mode mcopy_mode,
Nadav Amita759a902021-09-02 14:58:56 -0700485 atomic_t *mmap_changing,
Andrea Arcangeli72981e02020-04-06 20:05:41 -0700486 __u64 mode)
Andrea Arcangelic1a4de92015-09-04 15:47:04 -0700487{
488 struct vm_area_struct *dst_vma;
489 ssize_t err;
490 pmd_t *dst_pmd;
491 unsigned long src_addr, dst_addr;
Andrea Arcangelib6ebaed2015-09-04 15:47:08 -0700492 long copied;
493 struct page *page;
Andrea Arcangeli72981e02020-04-06 20:05:41 -0700494 bool wp_copy;
Andrea Arcangelic1a4de92015-09-04 15:47:04 -0700495
496 /*
497 * Sanitize the command parameters:
498 */
499 BUG_ON(dst_start & ~PAGE_MASK);
500 BUG_ON(len & ~PAGE_MASK);
501
502 /* Does the address range wrap, or is the span zero-sized? */
503 BUG_ON(src_start + len <= src_start);
504 BUG_ON(dst_start + len <= dst_start);
505
Andrea Arcangelib6ebaed2015-09-04 15:47:08 -0700506 src_addr = src_start;
507 dst_addr = dst_start;
508 copied = 0;
509 page = NULL;
510retry:
Michel Lespinassed8ed45c2020-06-08 21:33:25 -0700511 mmap_read_lock(dst_mm);
Andrea Arcangelic1a4de92015-09-04 15:47:04 -0700512
513 /*
Mike Rapoportdf2cc962018-06-07 17:09:25 -0700514 * If memory mappings are changing because of non-cooperative
515 * operation (e.g. mremap) running in parallel, bail out and
516 * request the user to retry later
517 */
518 err = -EAGAIN;
Nadav Amita759a902021-09-02 14:58:56 -0700519 if (mmap_changing && atomic_read(mmap_changing))
Mike Rapoportdf2cc962018-06-07 17:09:25 -0700520 goto out_unlock;
521
522 /*
Andrea Arcangelic1a4de92015-09-04 15:47:04 -0700523 * Make sure the vma is not shared, that the dst range is
524 * both valid and fully within a single existing vma.
525 */
Mike Rapoport27d02562017-02-24 14:58:28 -0800526 err = -ENOENT;
Wei Yang643aa362019-11-30 17:57:55 -0800527 dst_vma = find_dst_vma(dst_mm, dst_start, len);
Mike Rapoport26071ce2017-02-22 15:43:34 -0800528 if (!dst_vma)
529 goto out_unlock;
Mike Rapoport27d02562017-02-24 14:58:28 -0800530
531 err = -EINVAL;
532 /*
533 * shmem_zero_setup is invoked in mmap for MAP_ANONYMOUS|MAP_SHARED but
534 * it will overwrite vm_ops, so vma_is_anonymous must return false.
535 */
536 if (WARN_ON_ONCE(vma_is_anonymous(dst_vma) &&
537 dst_vma->vm_flags & VM_SHARED))
538 goto out_unlock;
539
540 /*
Andrea Arcangeli72981e02020-04-06 20:05:41 -0700541 * validate 'mode' now that we know the dst_vma: don't allow
542 * a wrprotect copy if the userfaultfd didn't register as WP.
543 */
544 wp_copy = mode & UFFDIO_COPY_MODE_WP;
545 if (wp_copy && !(dst_vma->vm_flags & VM_UFFD_WP))
546 goto out_unlock;
547
548 /*
Mike Rapoport27d02562017-02-24 14:58:28 -0800549 * If this is a HUGETLB vma, pass off to appropriate routine
550 */
551 if (is_vm_hugetlb_page(dst_vma))
552 return __mcopy_atomic_hugetlb(dst_mm, dst_vma, dst_start,
Axel Rasmussenf6191472021-05-04 18:35:49 -0700553 src_start, len, mcopy_mode);
Mike Rapoport27d02562017-02-24 14:58:28 -0800554
Mike Rapoport26071ce2017-02-22 15:43:34 -0800555 if (!vma_is_anonymous(dst_vma) && !vma_is_shmem(dst_vma))
Andrea Arcangelib6ebaed2015-09-04 15:47:08 -0700556 goto out_unlock;
Axel Rasmussen15313252021-06-30 18:49:24 -0700557 if (!vma_is_shmem(dst_vma) && mcopy_mode == MCOPY_ATOMIC_CONTINUE)
Axel Rasmussenf6191472021-05-04 18:35:49 -0700558 goto out_unlock;
Andrea Arcangelic1a4de92015-09-04 15:47:04 -0700559
560 /*
561 * Ensure the dst_vma has a anon_vma or this page
562 * would get a NULL anon_vma when moved in the
563 * dst_vma.
564 */
565 err = -ENOMEM;
Andrea Arcangeli5b510722018-11-30 14:09:28 -0800566 if (!(dst_vma->vm_flags & VM_SHARED) &&
567 unlikely(anon_vma_prepare(dst_vma)))
Andrea Arcangelib6ebaed2015-09-04 15:47:08 -0700568 goto out_unlock;
Andrea Arcangelic1a4de92015-09-04 15:47:04 -0700569
Andrea Arcangelib6ebaed2015-09-04 15:47:08 -0700570 while (src_addr < src_start + len) {
Andrea Arcangelic1a4de92015-09-04 15:47:04 -0700571 pmd_t dst_pmdval;
Andrea Arcangelib6ebaed2015-09-04 15:47:08 -0700572
Andrea Arcangelic1a4de92015-09-04 15:47:04 -0700573 BUG_ON(dst_addr >= dst_start + len);
Andrea Arcangelib6ebaed2015-09-04 15:47:08 -0700574
Andrea Arcangelic1a4de92015-09-04 15:47:04 -0700575 dst_pmd = mm_alloc_pmd(dst_mm, dst_addr);
576 if (unlikely(!dst_pmd)) {
577 err = -ENOMEM;
578 break;
579 }
580
581 dst_pmdval = pmd_read_atomic(dst_pmd);
582 /*
583 * If the dst_pmd is mapped as THP don't
584 * override it and just be strict.
585 */
586 if (unlikely(pmd_trans_huge(dst_pmdval))) {
587 err = -EEXIST;
588 break;
589 }
590 if (unlikely(pmd_none(dst_pmdval)) &&
Joel Fernandes (Google)4cf58922019-01-03 15:28:34 -0800591 unlikely(__pte_alloc(dst_mm, dst_pmd))) {
Andrea Arcangelic1a4de92015-09-04 15:47:04 -0700592 err = -ENOMEM;
593 break;
594 }
595 /* If an huge pmd materialized from under us fail */
596 if (unlikely(pmd_trans_huge(*dst_pmd))) {
597 err = -EFAULT;
598 break;
599 }
600
601 BUG_ON(pmd_none(*dst_pmd));
602 BUG_ON(pmd_trans_huge(*dst_pmd));
603
Mike Rapoport3217d3c2017-09-06 16:23:06 -0700604 err = mfill_atomic_pte(dst_mm, dst_pmd, dst_vma, dst_addr,
Axel Rasmussen15313252021-06-30 18:49:24 -0700605 src_addr, &page, mcopy_mode, wp_copy);
Andrea Arcangelic1a4de92015-09-04 15:47:04 -0700606 cond_resched();
607
Andrea Arcangeli9e368252018-11-30 14:09:25 -0800608 if (unlikely(err == -ENOENT)) {
Andrea Arcangelib6ebaed2015-09-04 15:47:08 -0700609 void *page_kaddr;
610
Michel Lespinassed8ed45c2020-06-08 21:33:25 -0700611 mmap_read_unlock(dst_mm);
Andrea Arcangelib6ebaed2015-09-04 15:47:08 -0700612 BUG_ON(!page);
613
614 page_kaddr = kmap(page);
615 err = copy_from_user(page_kaddr,
616 (const void __user *) src_addr,
617 PAGE_SIZE);
618 kunmap(page);
619 if (unlikely(err)) {
620 err = -EFAULT;
621 goto out;
622 }
623 goto retry;
624 } else
625 BUG_ON(page);
626
Andrea Arcangelic1a4de92015-09-04 15:47:04 -0700627 if (!err) {
628 dst_addr += PAGE_SIZE;
629 src_addr += PAGE_SIZE;
630 copied += PAGE_SIZE;
631
632 if (fatal_signal_pending(current))
633 err = -EINTR;
634 }
635 if (err)
636 break;
637 }
638
Andrea Arcangelib6ebaed2015-09-04 15:47:08 -0700639out_unlock:
Michel Lespinassed8ed45c2020-06-08 21:33:25 -0700640 mmap_read_unlock(dst_mm);
Andrea Arcangelib6ebaed2015-09-04 15:47:08 -0700641out:
642 if (page)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300643 put_page(page);
Andrea Arcangelic1a4de92015-09-04 15:47:04 -0700644 BUG_ON(copied < 0);
645 BUG_ON(err > 0);
646 BUG_ON(!copied && !err);
647 return copied ? copied : err;
648}
649
650ssize_t mcopy_atomic(struct mm_struct *dst_mm, unsigned long dst_start,
Mike Rapoportdf2cc962018-06-07 17:09:25 -0700651 unsigned long src_start, unsigned long len,
Nadav Amita759a902021-09-02 14:58:56 -0700652 atomic_t *mmap_changing, __u64 mode)
Andrea Arcangelic1a4de92015-09-04 15:47:04 -0700653{
Axel Rasmussenf6191472021-05-04 18:35:49 -0700654 return __mcopy_atomic(dst_mm, dst_start, src_start, len,
655 MCOPY_ATOMIC_NORMAL, mmap_changing, mode);
Andrea Arcangelic1a4de92015-09-04 15:47:04 -0700656}
657
658ssize_t mfill_zeropage(struct mm_struct *dst_mm, unsigned long start,
Nadav Amita759a902021-09-02 14:58:56 -0700659 unsigned long len, atomic_t *mmap_changing)
Andrea Arcangelic1a4de92015-09-04 15:47:04 -0700660{
Axel Rasmussenf6191472021-05-04 18:35:49 -0700661 return __mcopy_atomic(dst_mm, start, 0, len, MCOPY_ATOMIC_ZEROPAGE,
662 mmap_changing, 0);
663}
664
665ssize_t mcopy_continue(struct mm_struct *dst_mm, unsigned long start,
Nadav Amita759a902021-09-02 14:58:56 -0700666 unsigned long len, atomic_t *mmap_changing)
Axel Rasmussenf6191472021-05-04 18:35:49 -0700667{
668 return __mcopy_atomic(dst_mm, start, 0, len, MCOPY_ATOMIC_CONTINUE,
669 mmap_changing, 0);
Andrea Arcangelic1a4de92015-09-04 15:47:04 -0700670}
Shaohua Liffd05792020-04-06 20:06:09 -0700671
672int mwriteprotect_range(struct mm_struct *dst_mm, unsigned long start,
Nadav Amita759a902021-09-02 14:58:56 -0700673 unsigned long len, bool enable_wp,
674 atomic_t *mmap_changing)
Shaohua Liffd05792020-04-06 20:06:09 -0700675{
676 struct vm_area_struct *dst_vma;
677 pgprot_t newprot;
678 int err;
679
680 /*
681 * Sanitize the command parameters:
682 */
683 BUG_ON(start & ~PAGE_MASK);
684 BUG_ON(len & ~PAGE_MASK);
685
686 /* Does the address range wrap, or is the span zero-sized? */
687 BUG_ON(start + len <= start);
688
Michel Lespinassed8ed45c2020-06-08 21:33:25 -0700689 mmap_read_lock(dst_mm);
Shaohua Liffd05792020-04-06 20:06:09 -0700690
691 /*
692 * If memory mappings are changing because of non-cooperative
693 * operation (e.g. mremap) running in parallel, bail out and
694 * request the user to retry later
695 */
696 err = -EAGAIN;
Nadav Amita759a902021-09-02 14:58:56 -0700697 if (mmap_changing && atomic_read(mmap_changing))
Shaohua Liffd05792020-04-06 20:06:09 -0700698 goto out_unlock;
699
700 err = -ENOENT;
701 dst_vma = find_dst_vma(dst_mm, start, len);
702 /*
703 * Make sure the vma is not shared, that the dst range is
704 * both valid and fully within a single existing vma.
705 */
706 if (!dst_vma || (dst_vma->vm_flags & VM_SHARED))
707 goto out_unlock;
708 if (!userfaultfd_wp(dst_vma))
709 goto out_unlock;
710 if (!vma_is_anonymous(dst_vma))
711 goto out_unlock;
712
713 if (enable_wp)
714 newprot = vm_get_page_prot(dst_vma->vm_flags & ~(VM_WRITE));
715 else
716 newprot = vm_get_page_prot(dst_vma->vm_flags);
717
718 change_protection(dst_vma, start, start + len, newprot,
719 enable_wp ? MM_CP_UFFD_WP : MM_CP_UFFD_WP_RESOLVE);
720
721 err = 0;
722out_unlock:
Michel Lespinassed8ed45c2020-06-08 21:33:25 -0700723 mmap_read_unlock(dst_mm);
Shaohua Liffd05792020-04-06 20:06:09 -0700724 return err;
725}