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 | 18 | #include <asm/tlbflush.h> |
4a18419f | 19 | #include <asm/tlb.h> |
c1a4de99 AA |
20 | #include "internal.h" |
21 | ||
643aa36e WY |
22 | static __always_inline |
23 | struct 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); | |
686ea6e6 | 34 | if (!range_in_vma(dst_vma, dst_start, dst_start + len)) |
643aa36e WY |
35 | return NULL; |
36 | ||
37 | /* | |
38 | * Check the vma is registered in uffd, this is required to | |
39 | * enforce the VM_MAYWRITE check done at uffd registration | |
40 | * time. | |
41 | */ | |
42 | if (!dst_vma->vm_userfaultfd_ctx.ctx) | |
43 | return NULL; | |
44 | ||
45 | return dst_vma; | |
46 | } | |
47 | ||
435cdb41 AR |
48 | /* Check if dst_addr is outside of file's size. Must be called with ptl held. */ |
49 | static bool mfill_file_over_size(struct vm_area_struct *dst_vma, | |
50 | unsigned long dst_addr) | |
51 | { | |
52 | struct inode *inode; | |
53 | pgoff_t offset, max_off; | |
54 | ||
55 | if (!dst_vma->vm_file) | |
56 | return false; | |
57 | ||
58 | inode = dst_vma->vm_file->f_inode; | |
59 | offset = linear_page_index(dst_vma, dst_addr); | |
60 | max_off = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE); | |
61 | return offset >= max_off; | |
62 | } | |
63 | ||
15313257 AR |
64 | /* |
65 | * Install PTEs, to map dst_addr (within dst_vma) to page. | |
66 | * | |
7d64ae3a AR |
67 | * This function handles both MCOPY_ATOMIC_NORMAL and _CONTINUE for both shmem |
68 | * and anon, and for both shared and private VMAs. | |
15313257 | 69 | */ |
61c50040 | 70 | int mfill_atomic_install_pte(pmd_t *dst_pmd, |
7d64ae3a AR |
71 | struct vm_area_struct *dst_vma, |
72 | unsigned long dst_addr, struct page *page, | |
d9712937 | 73 | bool newly_allocated, uffd_flags_t flags) |
15313257 AR |
74 | { |
75 | int ret; | |
61c50040 | 76 | struct mm_struct *dst_mm = dst_vma->vm_mm; |
15313257 AR |
77 | pte_t _dst_pte, *dst_pte; |
78 | bool writable = dst_vma->vm_flags & VM_WRITE; | |
79 | bool vm_shared = dst_vma->vm_flags & VM_SHARED; | |
93b0d917 | 80 | bool page_in_cache = page_mapping(page); |
15313257 | 81 | spinlock_t *ptl; |
28965f0f | 82 | struct folio *folio; |
15313257 AR |
83 | |
84 | _dst_pte = mk_pte(page, dst_vma->vm_page_prot); | |
9ae0f87d | 85 | _dst_pte = pte_mkdirty(_dst_pte); |
15313257 AR |
86 | if (page_in_cache && !vm_shared) |
87 | writable = false; | |
8ee79edf | 88 | if (writable) |
161e393c | 89 | _dst_pte = pte_mkwrite(_dst_pte, dst_vma); |
d9712937 | 90 | if (flags & MFILL_ATOMIC_WP) |
f1eb1bac | 91 | _dst_pte = pte_mkuffd_wp(_dst_pte); |
15313257 | 92 | |
3622d3cd | 93 | ret = -EAGAIN; |
15313257 | 94 | dst_pte = pte_offset_map_lock(dst_mm, dst_pmd, dst_addr, &ptl); |
3622d3cd HD |
95 | if (!dst_pte) |
96 | goto out; | |
15313257 | 97 | |
435cdb41 | 98 | if (mfill_file_over_size(dst_vma, dst_addr)) { |
15313257 | 99 | ret = -EFAULT; |
435cdb41 | 100 | goto out_unlock; |
15313257 AR |
101 | } |
102 | ||
103 | ret = -EEXIST; | |
8ee79edf PX |
104 | /* |
105 | * We allow to overwrite a pte marker: consider when both MISSING|WP | |
106 | * registered, we firstly wr-protect a none pte which has no page cache | |
107 | * page backing it, then access the page. | |
108 | */ | |
c33c7948 | 109 | if (!pte_none_mostly(ptep_get(dst_pte))) |
15313257 AR |
110 | goto out_unlock; |
111 | ||
28965f0f | 112 | folio = page_folio(page); |
cea86fe2 HD |
113 | if (page_in_cache) { |
114 | /* Usually, cache pages are already added to LRU */ | |
115 | if (newly_allocated) | |
28965f0f | 116 | folio_add_lru(folio); |
7123e19c | 117 | folio_add_file_rmap_pte(folio, page, dst_vma); |
cea86fe2 | 118 | } else { |
2853b66b | 119 | folio_add_new_anon_rmap(folio, dst_vma, dst_addr); |
28965f0f | 120 | folio_add_lru_vma(folio, dst_vma); |
cea86fe2 | 121 | } |
15313257 AR |
122 | |
123 | /* | |
124 | * Must happen after rmap, as mm_counter() checks mapping (via | |
125 | * PageAnon()), which is set by __page_set_anon_rmap(). | |
126 | */ | |
127 | inc_mm_counter(dst_mm, mm_counter(page)); | |
128 | ||
15313257 AR |
129 | set_pte_at(dst_mm, dst_addr, dst_pte, _dst_pte); |
130 | ||
131 | /* No need to invalidate - it was non-present before */ | |
132 | update_mmu_cache(dst_vma, dst_addr, dst_pte); | |
133 | ret = 0; | |
134 | out_unlock: | |
135 | pte_unmap_unlock(dst_pte, ptl); | |
3622d3cd | 136 | out: |
15313257 AR |
137 | return ret; |
138 | } | |
139 | ||
61c50040 | 140 | static int mfill_atomic_pte_copy(pmd_t *dst_pmd, |
a734991c AR |
141 | struct vm_area_struct *dst_vma, |
142 | unsigned long dst_addr, | |
143 | unsigned long src_addr, | |
d9712937 | 144 | uffd_flags_t flags, |
d7be6d7e | 145 | struct folio **foliop) |
c1a4de99 | 146 | { |
07e6d409 | 147 | void *kaddr; |
c1a4de99 | 148 | int ret; |
07e6d409 | 149 | struct folio *folio; |
c1a4de99 | 150 | |
d7be6d7e | 151 | if (!*foliop) { |
b6ebaedb | 152 | ret = -ENOMEM; |
07e6d409 Z |
153 | folio = vma_alloc_folio(GFP_HIGHUSER_MOVABLE, 0, dst_vma, |
154 | dst_addr, false); | |
155 | if (!folio) | |
b6ebaedb AA |
156 | goto out; |
157 | ||
07e6d409 | 158 | kaddr = kmap_local_folio(folio, 0); |
5521de7d IW |
159 | /* |
160 | * The read mmap_lock is held here. Despite the | |
161 | * mmap_lock being read recursive a deadlock is still | |
162 | * possible if a writer has taken a lock. For example: | |
163 | * | |
164 | * process A thread 1 takes read lock on own mmap_lock | |
165 | * process A thread 2 calls mmap, blocks taking write lock | |
166 | * process B thread 1 takes page fault, read lock on own mmap lock | |
167 | * process B thread 2 calls mmap, blocks taking write lock | |
168 | * process A thread 1 blocks taking read lock on process B | |
169 | * process B thread 1 blocks taking read lock on process A | |
170 | * | |
171 | * Disable page faults to prevent potential deadlock | |
172 | * and retry the copy outside the mmap_lock. | |
173 | */ | |
174 | pagefault_disable(); | |
07e6d409 | 175 | ret = copy_from_user(kaddr, (const void __user *) src_addr, |
b6ebaedb | 176 | PAGE_SIZE); |
5521de7d | 177 | pagefault_enable(); |
07e6d409 | 178 | kunmap_local(kaddr); |
b6ebaedb | 179 | |
c1e8d7c6 | 180 | /* fallback to copy_from_user outside mmap_lock */ |
b6ebaedb | 181 | if (unlikely(ret)) { |
9e368259 | 182 | ret = -ENOENT; |
d7be6d7e | 183 | *foliop = folio; |
b6ebaedb AA |
184 | /* don't free the page */ |
185 | goto out; | |
186 | } | |
7c25a0b8 | 187 | |
07e6d409 | 188 | flush_dcache_folio(folio); |
b6ebaedb | 189 | } else { |
d7be6d7e Z |
190 | folio = *foliop; |
191 | *foliop = NULL; | |
b6ebaedb | 192 | } |
c1a4de99 AA |
193 | |
194 | /* | |
07e6d409 | 195 | * The memory barrier inside __folio_mark_uptodate makes sure that |
f4f5329d | 196 | * preceding stores to the page contents become visible before |
c1a4de99 AA |
197 | * the set_pte_at() write. |
198 | */ | |
07e6d409 | 199 | __folio_mark_uptodate(folio); |
c1a4de99 AA |
200 | |
201 | ret = -ENOMEM; | |
07e6d409 | 202 | if (mem_cgroup_charge(folio, dst_vma->vm_mm, GFP_KERNEL)) |
c1a4de99 AA |
203 | goto out_release; |
204 | ||
61c50040 | 205 | ret = mfill_atomic_install_pte(dst_pmd, dst_vma, dst_addr, |
07e6d409 | 206 | &folio->page, true, flags); |
15313257 AR |
207 | if (ret) |
208 | goto out_release; | |
c1a4de99 AA |
209 | out: |
210 | return ret; | |
c1a4de99 | 211 | out_release: |
07e6d409 | 212 | folio_put(folio); |
c1a4de99 | 213 | goto out; |
c1a4de99 AA |
214 | } |
215 | ||
61c50040 | 216 | static int mfill_atomic_pte_zeropage(pmd_t *dst_pmd, |
a734991c AR |
217 | struct vm_area_struct *dst_vma, |
218 | unsigned long dst_addr) | |
c1a4de99 AA |
219 | { |
220 | pte_t _dst_pte, *dst_pte; | |
221 | spinlock_t *ptl; | |
222 | int ret; | |
223 | ||
224 | _dst_pte = pte_mkspecial(pfn_pte(my_zero_pfn(dst_addr), | |
225 | dst_vma->vm_page_prot)); | |
3622d3cd | 226 | ret = -EAGAIN; |
61c50040 | 227 | dst_pte = pte_offset_map_lock(dst_vma->vm_mm, dst_pmd, dst_addr, &ptl); |
3622d3cd HD |
228 | if (!dst_pte) |
229 | goto out; | |
435cdb41 | 230 | if (mfill_file_over_size(dst_vma, dst_addr)) { |
e2a50c1f | 231 | ret = -EFAULT; |
435cdb41 | 232 | goto out_unlock; |
e2a50c1f AA |
233 | } |
234 | ret = -EEXIST; | |
c33c7948 | 235 | if (!pte_none(ptep_get(dst_pte))) |
c1a4de99 | 236 | goto out_unlock; |
61c50040 | 237 | set_pte_at(dst_vma->vm_mm, dst_addr, dst_pte, _dst_pte); |
c1a4de99 AA |
238 | /* No need to invalidate - it was non-present before */ |
239 | update_mmu_cache(dst_vma, dst_addr, dst_pte); | |
240 | ret = 0; | |
241 | out_unlock: | |
242 | pte_unmap_unlock(dst_pte, ptl); | |
3622d3cd | 243 | out: |
c1a4de99 AA |
244 | return ret; |
245 | } | |
246 | ||
15313257 | 247 | /* Handles UFFDIO_CONTINUE for all shmem VMAs (shared or private). */ |
61c50040 | 248 | static int mfill_atomic_pte_continue(pmd_t *dst_pmd, |
a734991c AR |
249 | struct vm_area_struct *dst_vma, |
250 | unsigned long dst_addr, | |
d9712937 | 251 | uffd_flags_t flags) |
15313257 AR |
252 | { |
253 | struct inode *inode = file_inode(dst_vma->vm_file); | |
254 | pgoff_t pgoff = linear_page_index(dst_vma, dst_addr); | |
12acf4fb | 255 | struct folio *folio; |
15313257 AR |
256 | struct page *page; |
257 | int ret; | |
258 | ||
12acf4fb MWO |
259 | ret = shmem_get_folio(inode, pgoff, &folio, SGP_NOALLOC); |
260 | /* Our caller expects us to return -EFAULT if we failed to find folio */ | |
73f37dbc AR |
261 | if (ret == -ENOENT) |
262 | ret = -EFAULT; | |
15313257 AR |
263 | if (ret) |
264 | goto out; | |
12acf4fb | 265 | if (!folio) { |
15313257 AR |
266 | ret = -EFAULT; |
267 | goto out; | |
268 | } | |
269 | ||
12acf4fb | 270 | page = folio_file_page(folio, pgoff); |
a7605426 YS |
271 | if (PageHWPoison(page)) { |
272 | ret = -EIO; | |
273 | goto out_release; | |
274 | } | |
275 | ||
61c50040 | 276 | ret = mfill_atomic_install_pte(dst_pmd, dst_vma, dst_addr, |
d9712937 | 277 | page, false, flags); |
15313257 AR |
278 | if (ret) |
279 | goto out_release; | |
280 | ||
12acf4fb | 281 | folio_unlock(folio); |
15313257 AR |
282 | ret = 0; |
283 | out: | |
284 | return ret; | |
285 | out_release: | |
12acf4fb MWO |
286 | folio_unlock(folio); |
287 | folio_put(folio); | |
15313257 AR |
288 | goto out; |
289 | } | |
290 | ||
fc71884a AR |
291 | /* Handles UFFDIO_POISON for all non-hugetlb VMAs. */ |
292 | static int mfill_atomic_pte_poison(pmd_t *dst_pmd, | |
293 | struct vm_area_struct *dst_vma, | |
294 | unsigned long dst_addr, | |
295 | uffd_flags_t flags) | |
296 | { | |
297 | int ret; | |
298 | struct mm_struct *dst_mm = dst_vma->vm_mm; | |
299 | pte_t _dst_pte, *dst_pte; | |
300 | spinlock_t *ptl; | |
301 | ||
302 | _dst_pte = make_pte_marker(PTE_MARKER_POISONED); | |
597425df | 303 | ret = -EAGAIN; |
fc71884a | 304 | dst_pte = pte_offset_map_lock(dst_mm, dst_pmd, dst_addr, &ptl); |
597425df HD |
305 | if (!dst_pte) |
306 | goto out; | |
fc71884a AR |
307 | |
308 | if (mfill_file_over_size(dst_vma, dst_addr)) { | |
309 | ret = -EFAULT; | |
310 | goto out_unlock; | |
311 | } | |
312 | ||
313 | ret = -EEXIST; | |
314 | /* Refuse to overwrite any PTE, even a PTE marker (e.g. UFFD WP). */ | |
afccb080 | 315 | if (!pte_none(ptep_get(dst_pte))) |
fc71884a AR |
316 | goto out_unlock; |
317 | ||
318 | set_pte_at(dst_mm, dst_addr, dst_pte, _dst_pte); | |
319 | ||
320 | /* No need to invalidate - it was non-present before */ | |
321 | update_mmu_cache(dst_vma, dst_addr, dst_pte); | |
322 | ret = 0; | |
323 | out_unlock: | |
324 | pte_unmap_unlock(dst_pte, ptl); | |
597425df | 325 | out: |
fc71884a AR |
326 | return ret; |
327 | } | |
328 | ||
c1a4de99 AA |
329 | static pmd_t *mm_alloc_pmd(struct mm_struct *mm, unsigned long address) |
330 | { | |
331 | pgd_t *pgd; | |
c2febafc | 332 | p4d_t *p4d; |
c1a4de99 | 333 | pud_t *pud; |
c1a4de99 AA |
334 | |
335 | pgd = pgd_offset(mm, address); | |
c2febafc KS |
336 | p4d = p4d_alloc(mm, pgd, address); |
337 | if (!p4d) | |
338 | return NULL; | |
339 | pud = pud_alloc(mm, p4d, address); | |
340 | if (!pud) | |
341 | return NULL; | |
342 | /* | |
343 | * Note that we didn't run this because the pmd was | |
344 | * missing, the *pmd may be already established and in | |
345 | * turn it may also be a trans_huge_pmd. | |
346 | */ | |
347 | return pmd_alloc(mm, pud, address); | |
c1a4de99 AA |
348 | } |
349 | ||
60d4d2d2 MK |
350 | #ifdef CONFIG_HUGETLB_PAGE |
351 | /* | |
a734991c | 352 | * mfill_atomic processing for HUGETLB vmas. Note that this routine is |
c1e8d7c6 | 353 | * called with mmap_lock held, it will release mmap_lock before returning. |
60d4d2d2 | 354 | */ |
61c50040 | 355 | static __always_inline ssize_t mfill_atomic_hugetlb( |
60d4d2d2 MK |
356 | struct vm_area_struct *dst_vma, |
357 | unsigned long dst_start, | |
358 | unsigned long src_start, | |
359 | unsigned long len, | |
d9712937 | 360 | uffd_flags_t flags) |
60d4d2d2 | 361 | { |
61c50040 | 362 | struct mm_struct *dst_mm = dst_vma->vm_mm; |
1c9e8def | 363 | int vm_shared = dst_vma->vm_flags & VM_SHARED; |
60d4d2d2 MK |
364 | ssize_t err; |
365 | pte_t *dst_pte; | |
366 | unsigned long src_addr, dst_addr; | |
367 | long copied; | |
0169fd51 | 368 | struct folio *folio; |
60d4d2d2 MK |
369 | unsigned long vma_hpagesize; |
370 | pgoff_t idx; | |
371 | u32 hash; | |
372 | struct address_space *mapping; | |
373 | ||
374 | /* | |
375 | * There is no default zero huge page for all huge page sizes as | |
376 | * supported by hugetlb. A PMD_SIZE huge pages may exist as used | |
377 | * by THP. Since we can not reliably insert a zero page, this | |
378 | * feature is not supported. | |
379 | */ | |
8a13897f | 380 | if (uffd_flags_mode_is(flags, MFILL_ATOMIC_ZEROPAGE)) { |
d8ed45c5 | 381 | mmap_read_unlock(dst_mm); |
60d4d2d2 MK |
382 | return -EINVAL; |
383 | } | |
384 | ||
385 | src_addr = src_start; | |
386 | dst_addr = dst_start; | |
387 | copied = 0; | |
0169fd51 | 388 | folio = NULL; |
60d4d2d2 MK |
389 | vma_hpagesize = vma_kernel_pagesize(dst_vma); |
390 | ||
391 | /* | |
392 | * Validate alignment based on huge page size | |
393 | */ | |
394 | err = -EINVAL; | |
395 | if (dst_start & (vma_hpagesize - 1) || len & (vma_hpagesize - 1)) | |
396 | goto out_unlock; | |
397 | ||
398 | retry: | |
399 | /* | |
c1e8d7c6 | 400 | * On routine entry dst_vma is set. If we had to drop mmap_lock and |
60d4d2d2 MK |
401 | * retry, dst_vma will be set to NULL and we must lookup again. |
402 | */ | |
403 | if (!dst_vma) { | |
27d02568 | 404 | err = -ENOENT; |
643aa36e | 405 | dst_vma = find_dst_vma(dst_mm, dst_start, len); |
60d4d2d2 MK |
406 | if (!dst_vma || !is_vm_hugetlb_page(dst_vma)) |
407 | goto out_unlock; | |
1c9e8def | 408 | |
27d02568 MR |
409 | err = -EINVAL; |
410 | if (vma_hpagesize != vma_kernel_pagesize(dst_vma)) | |
411 | goto out_unlock; | |
412 | ||
1c9e8def | 413 | vm_shared = dst_vma->vm_flags & VM_SHARED; |
60d4d2d2 MK |
414 | } |
415 | ||
60d4d2d2 | 416 | /* |
1c9e8def | 417 | * If not shared, ensure the dst_vma has a anon_vma. |
60d4d2d2 MK |
418 | */ |
419 | err = -ENOMEM; | |
1c9e8def MK |
420 | if (!vm_shared) { |
421 | if (unlikely(anon_vma_prepare(dst_vma))) | |
422 | goto out_unlock; | |
423 | } | |
60d4d2d2 | 424 | |
60d4d2d2 | 425 | while (src_addr < src_start + len) { |
60d4d2d2 | 426 | BUG_ON(dst_addr >= dst_start + len); |
60d4d2d2 MK |
427 | |
428 | /* | |
40549ba8 MK |
429 | * Serialize via vma_lock and hugetlb_fault_mutex. |
430 | * vma_lock ensures the dst_pte remains valid even | |
431 | * in the case of shared pmds. fault mutex prevents | |
432 | * races with other faulting threads. | |
60d4d2d2 | 433 | */ |
c0d0381a | 434 | idx = linear_page_index(dst_vma, dst_addr); |
3a47c54f | 435 | mapping = dst_vma->vm_file->f_mapping; |
188b04a7 | 436 | hash = hugetlb_fault_mutex_hash(mapping, idx); |
60d4d2d2 | 437 | mutex_lock(&hugetlb_fault_mutex_table[hash]); |
40549ba8 | 438 | hugetlb_vma_lock_read(dst_vma); |
60d4d2d2 MK |
439 | |
440 | err = -ENOMEM; | |
aec44e0f | 441 | dst_pte = huge_pte_alloc(dst_mm, dst_vma, dst_addr, vma_hpagesize); |
60d4d2d2 | 442 | if (!dst_pte) { |
40549ba8 | 443 | hugetlb_vma_unlock_read(dst_vma); |
60d4d2d2 MK |
444 | mutex_unlock(&hugetlb_fault_mutex_table[hash]); |
445 | goto out_unlock; | |
446 | } | |
447 | ||
d9712937 | 448 | if (!uffd_flags_mode_is(flags, MFILL_ATOMIC_CONTINUE) && |
6041c691 | 449 | !huge_pte_none_mostly(huge_ptep_get(dst_pte))) { |
f6191471 | 450 | err = -EEXIST; |
40549ba8 | 451 | hugetlb_vma_unlock_read(dst_vma); |
60d4d2d2 MK |
452 | mutex_unlock(&hugetlb_fault_mutex_table[hash]); |
453 | goto out_unlock; | |
454 | } | |
455 | ||
d9712937 | 456 | err = hugetlb_mfill_atomic_pte(dst_pte, dst_vma, dst_addr, |
0169fd51 | 457 | src_addr, flags, &folio); |
60d4d2d2 | 458 | |
40549ba8 | 459 | hugetlb_vma_unlock_read(dst_vma); |
60d4d2d2 MK |
460 | mutex_unlock(&hugetlb_fault_mutex_table[hash]); |
461 | ||
462 | cond_resched(); | |
463 | ||
9e368259 | 464 | if (unlikely(err == -ENOENT)) { |
d8ed45c5 | 465 | mmap_read_unlock(dst_mm); |
0169fd51 | 466 | BUG_ON(!folio); |
60d4d2d2 | 467 | |
0169fd51 | 468 | err = copy_folio_from_user(folio, |
e87340ca | 469 | (const void __user *)src_addr, true); |
60d4d2d2 MK |
470 | if (unlikely(err)) { |
471 | err = -EFAULT; | |
472 | goto out; | |
473 | } | |
d8ed45c5 | 474 | mmap_read_lock(dst_mm); |
60d4d2d2 MK |
475 | |
476 | dst_vma = NULL; | |
477 | goto retry; | |
478 | } else | |
0169fd51 | 479 | BUG_ON(folio); |
60d4d2d2 MK |
480 | |
481 | if (!err) { | |
482 | dst_addr += vma_hpagesize; | |
483 | src_addr += vma_hpagesize; | |
484 | copied += vma_hpagesize; | |
485 | ||
486 | if (fatal_signal_pending(current)) | |
487 | err = -EINTR; | |
488 | } | |
489 | if (err) | |
490 | break; | |
491 | } | |
492 | ||
493 | out_unlock: | |
d8ed45c5 | 494 | mmap_read_unlock(dst_mm); |
60d4d2d2 | 495 | out: |
0169fd51 Z |
496 | if (folio) |
497 | folio_put(folio); | |
60d4d2d2 MK |
498 | BUG_ON(copied < 0); |
499 | BUG_ON(err > 0); | |
500 | BUG_ON(!copied && !err); | |
501 | return copied ? copied : err; | |
502 | } | |
503 | #else /* !CONFIG_HUGETLB_PAGE */ | |
504 | /* fail at build time if gcc attempts to use this */ | |
61c50040 AR |
505 | extern ssize_t mfill_atomic_hugetlb(struct vm_area_struct *dst_vma, |
506 | unsigned long dst_start, | |
507 | unsigned long src_start, | |
508 | unsigned long len, | |
d9712937 | 509 | uffd_flags_t flags); |
60d4d2d2 MK |
510 | #endif /* CONFIG_HUGETLB_PAGE */ |
511 | ||
61c50040 | 512 | static __always_inline ssize_t mfill_atomic_pte(pmd_t *dst_pmd, |
3217d3c7 MR |
513 | struct vm_area_struct *dst_vma, |
514 | unsigned long dst_addr, | |
515 | unsigned long src_addr, | |
d9712937 | 516 | uffd_flags_t flags, |
d7be6d7e | 517 | struct folio **foliop) |
3217d3c7 MR |
518 | { |
519 | ssize_t err; | |
520 | ||
d9712937 | 521 | if (uffd_flags_mode_is(flags, MFILL_ATOMIC_CONTINUE)) { |
61c50040 | 522 | return mfill_atomic_pte_continue(dst_pmd, dst_vma, |
d9712937 | 523 | dst_addr, flags); |
fc71884a AR |
524 | } else if (uffd_flags_mode_is(flags, MFILL_ATOMIC_POISON)) { |
525 | return mfill_atomic_pte_poison(dst_pmd, dst_vma, | |
526 | dst_addr, flags); | |
15313257 AR |
527 | } |
528 | ||
5b51072e AA |
529 | /* |
530 | * The normal page fault path for a shmem will invoke the | |
531 | * fault, fill the hole in the file and COW it right away. The | |
532 | * result generates plain anonymous memory. So when we are | |
533 | * asked to fill an hole in a MAP_PRIVATE shmem mapping, we'll | |
534 | * generate anonymous memory directly without actually filling | |
535 | * the hole. For the MAP_PRIVATE case the robustness check | |
536 | * only happens in the pagetable (to verify it's still none) | |
537 | * and not in the radix tree. | |
538 | */ | |
539 | if (!(dst_vma->vm_flags & VM_SHARED)) { | |
d9712937 | 540 | if (uffd_flags_mode_is(flags, MFILL_ATOMIC_COPY)) |
61c50040 | 541 | err = mfill_atomic_pte_copy(dst_pmd, dst_vma, |
d9712937 | 542 | dst_addr, src_addr, |
d7be6d7e | 543 | flags, foliop); |
3217d3c7 | 544 | else |
61c50040 | 545 | err = mfill_atomic_pte_zeropage(dst_pmd, |
3217d3c7 MR |
546 | dst_vma, dst_addr); |
547 | } else { | |
61c50040 | 548 | err = shmem_mfill_atomic_pte(dst_pmd, dst_vma, |
15313257 | 549 | dst_addr, src_addr, |
d7be6d7e | 550 | flags, foliop); |
3217d3c7 MR |
551 | } |
552 | ||
553 | return err; | |
554 | } | |
555 | ||
a734991c AR |
556 | static __always_inline ssize_t mfill_atomic(struct mm_struct *dst_mm, |
557 | unsigned long dst_start, | |
558 | unsigned long src_start, | |
559 | unsigned long len, | |
a734991c | 560 | atomic_t *mmap_changing, |
d9712937 | 561 | uffd_flags_t flags) |
c1a4de99 AA |
562 | { |
563 | struct vm_area_struct *dst_vma; | |
564 | ssize_t err; | |
565 | pmd_t *dst_pmd; | |
566 | unsigned long src_addr, dst_addr; | |
b6ebaedb | 567 | long copied; |
d7be6d7e | 568 | struct folio *folio; |
c1a4de99 AA |
569 | |
570 | /* | |
571 | * Sanitize the command parameters: | |
572 | */ | |
573 | BUG_ON(dst_start & ~PAGE_MASK); | |
574 | BUG_ON(len & ~PAGE_MASK); | |
575 | ||
576 | /* Does the address range wrap, or is the span zero-sized? */ | |
577 | BUG_ON(src_start + len <= src_start); | |
578 | BUG_ON(dst_start + len <= dst_start); | |
579 | ||
b6ebaedb AA |
580 | src_addr = src_start; |
581 | dst_addr = dst_start; | |
582 | copied = 0; | |
d7be6d7e | 583 | folio = NULL; |
b6ebaedb | 584 | retry: |
d8ed45c5 | 585 | mmap_read_lock(dst_mm); |
c1a4de99 | 586 | |
df2cc96e MR |
587 | /* |
588 | * If memory mappings are changing because of non-cooperative | |
589 | * operation (e.g. mremap) running in parallel, bail out and | |
590 | * request the user to retry later | |
591 | */ | |
592 | err = -EAGAIN; | |
a759a909 | 593 | if (mmap_changing && atomic_read(mmap_changing)) |
df2cc96e MR |
594 | goto out_unlock; |
595 | ||
c1a4de99 AA |
596 | /* |
597 | * Make sure the vma is not shared, that the dst range is | |
598 | * both valid and fully within a single existing vma. | |
599 | */ | |
27d02568 | 600 | err = -ENOENT; |
643aa36e | 601 | dst_vma = find_dst_vma(dst_mm, dst_start, len); |
26071ced MR |
602 | if (!dst_vma) |
603 | goto out_unlock; | |
c1a4de99 | 604 | |
27d02568 MR |
605 | err = -EINVAL; |
606 | /* | |
607 | * shmem_zero_setup is invoked in mmap for MAP_ANONYMOUS|MAP_SHARED but | |
608 | * it will overwrite vm_ops, so vma_is_anonymous must return false. | |
609 | */ | |
610 | if (WARN_ON_ONCE(vma_is_anonymous(dst_vma) && | |
611 | dst_vma->vm_flags & VM_SHARED)) | |
612 | goto out_unlock; | |
613 | ||
72981e0e AA |
614 | /* |
615 | * validate 'mode' now that we know the dst_vma: don't allow | |
616 | * a wrprotect copy if the userfaultfd didn't register as WP. | |
617 | */ | |
d9712937 | 618 | if ((flags & MFILL_ATOMIC_WP) && !(dst_vma->vm_flags & VM_UFFD_WP)) |
72981e0e AA |
619 | goto out_unlock; |
620 | ||
60d4d2d2 MK |
621 | /* |
622 | * If this is a HUGETLB vma, pass off to appropriate routine | |
623 | */ | |
624 | if (is_vm_hugetlb_page(dst_vma)) | |
61c50040 | 625 | return mfill_atomic_hugetlb(dst_vma, dst_start, |
d9712937 | 626 | src_start, len, flags); |
60d4d2d2 | 627 | |
26071ced | 628 | if (!vma_is_anonymous(dst_vma) && !vma_is_shmem(dst_vma)) |
b6ebaedb | 629 | goto out_unlock; |
d9712937 AR |
630 | if (!vma_is_shmem(dst_vma) && |
631 | uffd_flags_mode_is(flags, MFILL_ATOMIC_CONTINUE)) | |
f6191471 | 632 | goto out_unlock; |
c1a4de99 AA |
633 | |
634 | /* | |
635 | * Ensure the dst_vma has a anon_vma or this page | |
636 | * would get a NULL anon_vma when moved in the | |
637 | * dst_vma. | |
638 | */ | |
639 | err = -ENOMEM; | |
5b51072e AA |
640 | if (!(dst_vma->vm_flags & VM_SHARED) && |
641 | unlikely(anon_vma_prepare(dst_vma))) | |
b6ebaedb | 642 | goto out_unlock; |
c1a4de99 | 643 | |
b6ebaedb | 644 | while (src_addr < src_start + len) { |
c1a4de99 | 645 | pmd_t dst_pmdval; |
b6ebaedb | 646 | |
c1a4de99 | 647 | BUG_ON(dst_addr >= dst_start + len); |
b6ebaedb | 648 | |
c1a4de99 AA |
649 | dst_pmd = mm_alloc_pmd(dst_mm, dst_addr); |
650 | if (unlikely(!dst_pmd)) { | |
651 | err = -ENOMEM; | |
652 | break; | |
653 | } | |
654 | ||
dab6e717 | 655 | dst_pmdval = pmdp_get_lockless(dst_pmd); |
c1a4de99 AA |
656 | /* |
657 | * If the dst_pmd is mapped as THP don't | |
658 | * override it and just be strict. | |
659 | */ | |
660 | if (unlikely(pmd_trans_huge(dst_pmdval))) { | |
661 | err = -EEXIST; | |
662 | break; | |
663 | } | |
664 | if (unlikely(pmd_none(dst_pmdval)) && | |
4cf58924 | 665 | unlikely(__pte_alloc(dst_mm, dst_pmd))) { |
c1a4de99 AA |
666 | err = -ENOMEM; |
667 | break; | |
668 | } | |
669 | /* If an huge pmd materialized from under us fail */ | |
670 | if (unlikely(pmd_trans_huge(*dst_pmd))) { | |
671 | err = -EFAULT; | |
672 | break; | |
673 | } | |
674 | ||
675 | BUG_ON(pmd_none(*dst_pmd)); | |
676 | BUG_ON(pmd_trans_huge(*dst_pmd)); | |
677 | ||
61c50040 | 678 | err = mfill_atomic_pte(dst_pmd, dst_vma, dst_addr, |
d7be6d7e | 679 | src_addr, flags, &folio); |
c1a4de99 AA |
680 | cond_resched(); |
681 | ||
9e368259 | 682 | if (unlikely(err == -ENOENT)) { |
d7be6d7e | 683 | void *kaddr; |
b6ebaedb | 684 | |
d8ed45c5 | 685 | mmap_read_unlock(dst_mm); |
d7be6d7e | 686 | BUG_ON(!folio); |
b6ebaedb | 687 | |
d7be6d7e Z |
688 | kaddr = kmap_local_folio(folio, 0); |
689 | err = copy_from_user(kaddr, | |
b6ebaedb AA |
690 | (const void __user *) src_addr, |
691 | PAGE_SIZE); | |
d7be6d7e | 692 | kunmap_local(kaddr); |
b6ebaedb AA |
693 | if (unlikely(err)) { |
694 | err = -EFAULT; | |
695 | goto out; | |
696 | } | |
d7be6d7e | 697 | flush_dcache_folio(folio); |
b6ebaedb AA |
698 | goto retry; |
699 | } else | |
d7be6d7e | 700 | BUG_ON(folio); |
b6ebaedb | 701 | |
c1a4de99 AA |
702 | if (!err) { |
703 | dst_addr += PAGE_SIZE; | |
704 | src_addr += PAGE_SIZE; | |
705 | copied += PAGE_SIZE; | |
706 | ||
707 | if (fatal_signal_pending(current)) | |
708 | err = -EINTR; | |
709 | } | |
710 | if (err) | |
711 | break; | |
712 | } | |
713 | ||
b6ebaedb | 714 | out_unlock: |
d8ed45c5 | 715 | mmap_read_unlock(dst_mm); |
b6ebaedb | 716 | out: |
d7be6d7e Z |
717 | if (folio) |
718 | folio_put(folio); | |
c1a4de99 AA |
719 | BUG_ON(copied < 0); |
720 | BUG_ON(err > 0); | |
721 | BUG_ON(!copied && !err); | |
722 | return copied ? copied : err; | |
723 | } | |
724 | ||
a734991c AR |
725 | ssize_t mfill_atomic_copy(struct mm_struct *dst_mm, unsigned long dst_start, |
726 | unsigned long src_start, unsigned long len, | |
d9712937 | 727 | atomic_t *mmap_changing, uffd_flags_t flags) |
c1a4de99 | 728 | { |
d9712937 AR |
729 | return mfill_atomic(dst_mm, dst_start, src_start, len, mmap_changing, |
730 | uffd_flags_set_mode(flags, MFILL_ATOMIC_COPY)); | |
c1a4de99 AA |
731 | } |
732 | ||
a734991c AR |
733 | ssize_t mfill_atomic_zeropage(struct mm_struct *dst_mm, unsigned long start, |
734 | unsigned long len, atomic_t *mmap_changing) | |
c1a4de99 | 735 | { |
d9712937 AR |
736 | return mfill_atomic(dst_mm, start, 0, len, mmap_changing, |
737 | uffd_flags_set_mode(0, MFILL_ATOMIC_ZEROPAGE)); | |
f6191471 AR |
738 | } |
739 | ||
a734991c | 740 | ssize_t mfill_atomic_continue(struct mm_struct *dst_mm, unsigned long start, |
02891844 AR |
741 | unsigned long len, atomic_t *mmap_changing, |
742 | uffd_flags_t flags) | |
f6191471 | 743 | { |
d9712937 | 744 | return mfill_atomic(dst_mm, start, 0, len, mmap_changing, |
02891844 | 745 | uffd_flags_set_mode(flags, MFILL_ATOMIC_CONTINUE)); |
c1a4de99 | 746 | } |
ffd05793 | 747 | |
fc71884a AR |
748 | ssize_t mfill_atomic_poison(struct mm_struct *dst_mm, unsigned long start, |
749 | unsigned long len, atomic_t *mmap_changing, | |
750 | uffd_flags_t flags) | |
751 | { | |
752 | return mfill_atomic(dst_mm, start, 0, len, mmap_changing, | |
753 | uffd_flags_set_mode(flags, MFILL_ATOMIC_POISON)); | |
754 | } | |
755 | ||
61c50040 | 756 | long uffd_wp_range(struct vm_area_struct *dst_vma, |
f369b07c PX |
757 | unsigned long start, unsigned long len, bool enable_wp) |
758 | { | |
931298e1 | 759 | unsigned int mm_cp_flags; |
f369b07c | 760 | struct mmu_gather tlb; |
d1751118 | 761 | long ret; |
f369b07c | 762 | |
a1b92a3f MUA |
763 | VM_WARN_ONCE(start < dst_vma->vm_start || start + len > dst_vma->vm_end, |
764 | "The address range exceeds VMA boundary.\n"); | |
f369b07c | 765 | if (enable_wp) |
931298e1 | 766 | mm_cp_flags = MM_CP_UFFD_WP; |
f369b07c | 767 | else |
931298e1 | 768 | mm_cp_flags = MM_CP_UFFD_WP_RESOLVE; |
f369b07c | 769 | |
931298e1 DH |
770 | /* |
771 | * vma->vm_page_prot already reflects that uffd-wp is enabled for this | |
772 | * VMA (see userfaultfd_set_vm_flags()) and that all PTEs are supposed | |
773 | * to be write-protected as default whenever protection changes. | |
774 | * Try upgrading write permissions manually. | |
775 | */ | |
776 | if (!enable_wp && vma_wants_manual_pte_write_upgrade(dst_vma)) | |
777 | mm_cp_flags |= MM_CP_TRY_CHANGE_WRITABLE; | |
61c50040 | 778 | tlb_gather_mmu(&tlb, dst_vma->vm_mm); |
d1751118 | 779 | ret = change_protection(&tlb, dst_vma, start, start + len, mm_cp_flags); |
f369b07c | 780 | tlb_finish_mmu(&tlb); |
d1751118 PX |
781 | |
782 | return ret; | |
f369b07c PX |
783 | } |
784 | ||
ffd05793 | 785 | int mwriteprotect_range(struct mm_struct *dst_mm, unsigned long start, |
a759a909 NA |
786 | unsigned long len, bool enable_wp, |
787 | atomic_t *mmap_changing) | |
ffd05793 | 788 | { |
a1b92a3f MUA |
789 | unsigned long end = start + len; |
790 | unsigned long _start, _end; | |
ffd05793 | 791 | struct vm_area_struct *dst_vma; |
5a90d5a1 | 792 | unsigned long page_mask; |
d1751118 | 793 | long err; |
a1b92a3f | 794 | VMA_ITERATOR(vmi, dst_mm, start); |
ffd05793 SL |
795 | |
796 | /* | |
797 | * Sanitize the command parameters: | |
798 | */ | |
799 | BUG_ON(start & ~PAGE_MASK); | |
800 | BUG_ON(len & ~PAGE_MASK); | |
801 | ||
802 | /* Does the address range wrap, or is the span zero-sized? */ | |
803 | BUG_ON(start + len <= start); | |
804 | ||
d8ed45c5 | 805 | mmap_read_lock(dst_mm); |
ffd05793 SL |
806 | |
807 | /* | |
808 | * If memory mappings are changing because of non-cooperative | |
809 | * operation (e.g. mremap) running in parallel, bail out and | |
810 | * request the user to retry later | |
811 | */ | |
812 | err = -EAGAIN; | |
a759a909 | 813 | if (mmap_changing && atomic_read(mmap_changing)) |
ffd05793 SL |
814 | goto out_unlock; |
815 | ||
816 | err = -ENOENT; | |
a1b92a3f | 817 | for_each_vma_range(vmi, dst_vma, end) { |
b1f9e876 | 818 | |
a1b92a3f MUA |
819 | if (!userfaultfd_wp(dst_vma)) { |
820 | err = -ENOENT; | |
821 | break; | |
822 | } | |
ffd05793 | 823 | |
a1b92a3f MUA |
824 | if (is_vm_hugetlb_page(dst_vma)) { |
825 | err = -EINVAL; | |
826 | page_mask = vma_kernel_pagesize(dst_vma) - 1; | |
827 | if ((start & page_mask) || (len & page_mask)) | |
828 | break; | |
829 | } | |
5a90d5a1 | 830 | |
a1b92a3f MUA |
831 | _start = max(dst_vma->vm_start, start); |
832 | _end = min(dst_vma->vm_end, end); | |
d1751118 | 833 | |
61c50040 | 834 | err = uffd_wp_range(dst_vma, _start, _end - _start, enable_wp); |
ffd05793 | 835 | |
a1b92a3f MUA |
836 | /* Return 0 on success, <0 on failures */ |
837 | if (err < 0) | |
838 | break; | |
839 | err = 0; | |
840 | } | |
ffd05793 | 841 | out_unlock: |
d8ed45c5 | 842 | mmap_read_unlock(dst_mm); |
ffd05793 SL |
843 | return err; |
844 | } | |
adef4406 AA |
845 | |
846 | ||
847 | void double_pt_lock(spinlock_t *ptl1, | |
848 | spinlock_t *ptl2) | |
849 | __acquires(ptl1) | |
850 | __acquires(ptl2) | |
851 | { | |
852 | spinlock_t *ptl_tmp; | |
853 | ||
854 | if (ptl1 > ptl2) { | |
855 | /* exchange ptl1 and ptl2 */ | |
856 | ptl_tmp = ptl1; | |
857 | ptl1 = ptl2; | |
858 | ptl2 = ptl_tmp; | |
859 | } | |
860 | /* lock in virtual address order to avoid lock inversion */ | |
861 | spin_lock(ptl1); | |
862 | if (ptl1 != ptl2) | |
863 | spin_lock_nested(ptl2, SINGLE_DEPTH_NESTING); | |
864 | else | |
865 | __acquire(ptl2); | |
866 | } | |
867 | ||
868 | void double_pt_unlock(spinlock_t *ptl1, | |
869 | spinlock_t *ptl2) | |
870 | __releases(ptl1) | |
871 | __releases(ptl2) | |
872 | { | |
873 | spin_unlock(ptl1); | |
874 | if (ptl1 != ptl2) | |
875 | spin_unlock(ptl2); | |
876 | else | |
877 | __release(ptl2); | |
878 | } | |
879 | ||
880 | ||
881 | static int move_present_pte(struct mm_struct *mm, | |
882 | struct vm_area_struct *dst_vma, | |
883 | struct vm_area_struct *src_vma, | |
884 | unsigned long dst_addr, unsigned long src_addr, | |
885 | pte_t *dst_pte, pte_t *src_pte, | |
886 | pte_t orig_dst_pte, pte_t orig_src_pte, | |
887 | spinlock_t *dst_ptl, spinlock_t *src_ptl, | |
888 | struct folio *src_folio) | |
889 | { | |
890 | int err = 0; | |
891 | ||
892 | double_pt_lock(dst_ptl, src_ptl); | |
893 | ||
894 | if (!pte_same(*src_pte, orig_src_pte) || | |
895 | !pte_same(*dst_pte, orig_dst_pte)) { | |
896 | err = -EAGAIN; | |
897 | goto out; | |
898 | } | |
899 | if (folio_test_large(src_folio) || | |
900 | folio_maybe_dma_pinned(src_folio) || | |
901 | !PageAnonExclusive(&src_folio->page)) { | |
902 | err = -EBUSY; | |
903 | goto out; | |
904 | } | |
905 | ||
906 | folio_move_anon_rmap(src_folio, dst_vma); | |
907 | WRITE_ONCE(src_folio->index, linear_page_index(dst_vma, dst_addr)); | |
908 | ||
909 | orig_src_pte = ptep_clear_flush(src_vma, src_addr, src_pte); | |
910 | /* Folio got pinned from under us. Put it back and fail the move. */ | |
911 | if (folio_maybe_dma_pinned(src_folio)) { | |
912 | set_pte_at(mm, src_addr, src_pte, orig_src_pte); | |
913 | err = -EBUSY; | |
914 | goto out; | |
915 | } | |
916 | ||
917 | orig_dst_pte = mk_pte(&src_folio->page, dst_vma->vm_page_prot); | |
918 | /* Follow mremap() behavior and treat the entry dirty after the move */ | |
919 | orig_dst_pte = pte_mkwrite(pte_mkdirty(orig_dst_pte), dst_vma); | |
920 | ||
921 | set_pte_at(mm, dst_addr, dst_pte, orig_dst_pte); | |
922 | out: | |
923 | double_pt_unlock(dst_ptl, src_ptl); | |
924 | return err; | |
925 | } | |
926 | ||
927 | static int move_swap_pte(struct mm_struct *mm, | |
928 | unsigned long dst_addr, unsigned long src_addr, | |
929 | pte_t *dst_pte, pte_t *src_pte, | |
930 | pte_t orig_dst_pte, pte_t orig_src_pte, | |
931 | spinlock_t *dst_ptl, spinlock_t *src_ptl) | |
932 | { | |
933 | if (!pte_swp_exclusive(orig_src_pte)) | |
934 | return -EBUSY; | |
935 | ||
936 | double_pt_lock(dst_ptl, src_ptl); | |
937 | ||
938 | if (!pte_same(*src_pte, orig_src_pte) || | |
939 | !pte_same(*dst_pte, orig_dst_pte)) { | |
940 | double_pt_unlock(dst_ptl, src_ptl); | |
941 | return -EAGAIN; | |
942 | } | |
943 | ||
944 | orig_src_pte = ptep_get_and_clear(mm, src_addr, src_pte); | |
945 | set_pte_at(mm, dst_addr, dst_pte, orig_src_pte); | |
946 | double_pt_unlock(dst_ptl, src_ptl); | |
947 | ||
948 | return 0; | |
949 | } | |
950 | ||
951 | /* | |
952 | * The mmap_lock for reading is held by the caller. Just move the page | |
953 | * from src_pmd to dst_pmd if possible, and return true if succeeded | |
954 | * in moving the page. | |
955 | */ | |
956 | static int move_pages_pte(struct mm_struct *mm, pmd_t *dst_pmd, pmd_t *src_pmd, | |
957 | struct vm_area_struct *dst_vma, | |
958 | struct vm_area_struct *src_vma, | |
959 | unsigned long dst_addr, unsigned long src_addr, | |
960 | __u64 mode) | |
961 | { | |
962 | swp_entry_t entry; | |
963 | pte_t orig_src_pte, orig_dst_pte; | |
964 | pte_t src_folio_pte; | |
965 | spinlock_t *src_ptl, *dst_ptl; | |
966 | pte_t *src_pte = NULL; | |
967 | pte_t *dst_pte = NULL; | |
968 | ||
969 | struct folio *src_folio = NULL; | |
970 | struct anon_vma *src_anon_vma = NULL; | |
971 | struct mmu_notifier_range range; | |
972 | int err = 0; | |
973 | ||
974 | flush_cache_range(src_vma, src_addr, src_addr + PAGE_SIZE); | |
975 | mmu_notifier_range_init(&range, MMU_NOTIFY_CLEAR, 0, mm, | |
976 | src_addr, src_addr + PAGE_SIZE); | |
977 | mmu_notifier_invalidate_range_start(&range); | |
978 | retry: | |
979 | dst_pte = pte_offset_map_nolock(mm, dst_pmd, dst_addr, &dst_ptl); | |
980 | ||
981 | /* Retry if a huge pmd materialized from under us */ | |
982 | if (unlikely(!dst_pte)) { | |
983 | err = -EAGAIN; | |
984 | goto out; | |
985 | } | |
986 | ||
987 | src_pte = pte_offset_map_nolock(mm, src_pmd, src_addr, &src_ptl); | |
988 | ||
989 | /* | |
990 | * We held the mmap_lock for reading so MADV_DONTNEED | |
991 | * can zap transparent huge pages under us, or the | |
992 | * transparent huge page fault can establish new | |
993 | * transparent huge pages under us. | |
994 | */ | |
995 | if (unlikely(!src_pte)) { | |
996 | err = -EAGAIN; | |
997 | goto out; | |
998 | } | |
999 | ||
1000 | /* Sanity checks before the operation */ | |
1001 | if (WARN_ON_ONCE(pmd_none(*dst_pmd)) || WARN_ON_ONCE(pmd_none(*src_pmd)) || | |
1002 | WARN_ON_ONCE(pmd_trans_huge(*dst_pmd)) || WARN_ON_ONCE(pmd_trans_huge(*src_pmd))) { | |
1003 | err = -EINVAL; | |
1004 | goto out; | |
1005 | } | |
1006 | ||
1007 | spin_lock(dst_ptl); | |
1008 | orig_dst_pte = *dst_pte; | |
1009 | spin_unlock(dst_ptl); | |
1010 | if (!pte_none(orig_dst_pte)) { | |
1011 | err = -EEXIST; | |
1012 | goto out; | |
1013 | } | |
1014 | ||
1015 | spin_lock(src_ptl); | |
1016 | orig_src_pte = *src_pte; | |
1017 | spin_unlock(src_ptl); | |
1018 | if (pte_none(orig_src_pte)) { | |
1019 | if (!(mode & UFFDIO_MOVE_MODE_ALLOW_SRC_HOLES)) | |
1020 | err = -ENOENT; | |
1021 | else /* nothing to do to move a hole */ | |
1022 | err = 0; | |
1023 | goto out; | |
1024 | } | |
1025 | ||
1026 | /* If PTE changed after we locked the folio them start over */ | |
1027 | if (src_folio && unlikely(!pte_same(src_folio_pte, orig_src_pte))) { | |
1028 | err = -EAGAIN; | |
1029 | goto out; | |
1030 | } | |
1031 | ||
1032 | if (pte_present(orig_src_pte)) { | |
1033 | /* | |
1034 | * Pin and lock both source folio and anon_vma. Since we are in | |
1035 | * RCU read section, we can't block, so on contention have to | |
1036 | * unmap the ptes, obtain the lock and retry. | |
1037 | */ | |
1038 | if (!src_folio) { | |
1039 | struct folio *folio; | |
1040 | ||
1041 | /* | |
1042 | * Pin the page while holding the lock to be sure the | |
1043 | * page isn't freed under us | |
1044 | */ | |
1045 | spin_lock(src_ptl); | |
1046 | if (!pte_same(orig_src_pte, *src_pte)) { | |
1047 | spin_unlock(src_ptl); | |
1048 | err = -EAGAIN; | |
1049 | goto out; | |
1050 | } | |
1051 | ||
1052 | folio = vm_normal_folio(src_vma, src_addr, orig_src_pte); | |
1053 | if (!folio || !PageAnonExclusive(&folio->page)) { | |
1054 | spin_unlock(src_ptl); | |
1055 | err = -EBUSY; | |
1056 | goto out; | |
1057 | } | |
1058 | ||
1059 | folio_get(folio); | |
1060 | src_folio = folio; | |
1061 | src_folio_pte = orig_src_pte; | |
1062 | spin_unlock(src_ptl); | |
1063 | ||
1064 | if (!folio_trylock(src_folio)) { | |
1065 | pte_unmap(&orig_src_pte); | |
1066 | pte_unmap(&orig_dst_pte); | |
1067 | src_pte = dst_pte = NULL; | |
1068 | /* now we can block and wait */ | |
1069 | folio_lock(src_folio); | |
1070 | goto retry; | |
1071 | } | |
1072 | ||
1073 | if (WARN_ON_ONCE(!folio_test_anon(src_folio))) { | |
1074 | err = -EBUSY; | |
1075 | goto out; | |
1076 | } | |
1077 | } | |
1078 | ||
1079 | /* at this point we have src_folio locked */ | |
1080 | if (folio_test_large(src_folio)) { | |
1081 | err = split_folio(src_folio); | |
1082 | if (err) | |
1083 | goto out; | |
1084 | } | |
1085 | ||
1086 | if (!src_anon_vma) { | |
1087 | /* | |
1088 | * folio_referenced walks the anon_vma chain | |
1089 | * without the folio lock. Serialize against it with | |
1090 | * the anon_vma lock, the folio lock is not enough. | |
1091 | */ | |
1092 | src_anon_vma = folio_get_anon_vma(src_folio); | |
1093 | if (!src_anon_vma) { | |
1094 | /* page was unmapped from under us */ | |
1095 | err = -EAGAIN; | |
1096 | goto out; | |
1097 | } | |
1098 | if (!anon_vma_trylock_write(src_anon_vma)) { | |
1099 | pte_unmap(&orig_src_pte); | |
1100 | pte_unmap(&orig_dst_pte); | |
1101 | src_pte = dst_pte = NULL; | |
1102 | /* now we can block and wait */ | |
1103 | anon_vma_lock_write(src_anon_vma); | |
1104 | goto retry; | |
1105 | } | |
1106 | } | |
1107 | ||
1108 | err = move_present_pte(mm, dst_vma, src_vma, | |
1109 | dst_addr, src_addr, dst_pte, src_pte, | |
1110 | orig_dst_pte, orig_src_pte, | |
1111 | dst_ptl, src_ptl, src_folio); | |
1112 | } else { | |
1113 | entry = pte_to_swp_entry(orig_src_pte); | |
1114 | if (non_swap_entry(entry)) { | |
1115 | if (is_migration_entry(entry)) { | |
1116 | pte_unmap(&orig_src_pte); | |
1117 | pte_unmap(&orig_dst_pte); | |
1118 | src_pte = dst_pte = NULL; | |
1119 | migration_entry_wait(mm, src_pmd, src_addr); | |
1120 | err = -EAGAIN; | |
1121 | } else | |
1122 | err = -EFAULT; | |
1123 | goto out; | |
1124 | } | |
1125 | ||
1126 | err = move_swap_pte(mm, dst_addr, src_addr, | |
1127 | dst_pte, src_pte, | |
1128 | orig_dst_pte, orig_src_pte, | |
1129 | dst_ptl, src_ptl); | |
1130 | } | |
1131 | ||
1132 | out: | |
1133 | if (src_anon_vma) { | |
1134 | anon_vma_unlock_write(src_anon_vma); | |
1135 | put_anon_vma(src_anon_vma); | |
1136 | } | |
1137 | if (src_folio) { | |
1138 | folio_unlock(src_folio); | |
1139 | folio_put(src_folio); | |
1140 | } | |
1141 | if (dst_pte) | |
1142 | pte_unmap(dst_pte); | |
1143 | if (src_pte) | |
1144 | pte_unmap(src_pte); | |
1145 | mmu_notifier_invalidate_range_end(&range); | |
1146 | ||
1147 | return err; | |
1148 | } | |
1149 | ||
1150 | #ifdef CONFIG_TRANSPARENT_HUGEPAGE | |
1151 | static inline bool move_splits_huge_pmd(unsigned long dst_addr, | |
1152 | unsigned long src_addr, | |
1153 | unsigned long src_end) | |
1154 | { | |
1155 | return (src_addr & ~HPAGE_PMD_MASK) || (dst_addr & ~HPAGE_PMD_MASK) || | |
1156 | src_end - src_addr < HPAGE_PMD_SIZE; | |
1157 | } | |
1158 | #else | |
1159 | static inline bool move_splits_huge_pmd(unsigned long dst_addr, | |
1160 | unsigned long src_addr, | |
1161 | unsigned long src_end) | |
1162 | { | |
1163 | /* This is unreachable anyway, just to avoid warnings when HPAGE_PMD_SIZE==0 */ | |
1164 | return false; | |
1165 | } | |
1166 | #endif | |
1167 | ||
1168 | static inline bool vma_move_compatible(struct vm_area_struct *vma) | |
1169 | { | |
1170 | return !(vma->vm_flags & (VM_PFNMAP | VM_IO | VM_HUGETLB | | |
1171 | VM_MIXEDMAP | VM_SHADOW_STACK)); | |
1172 | } | |
1173 | ||
1174 | static int validate_move_areas(struct userfaultfd_ctx *ctx, | |
1175 | struct vm_area_struct *src_vma, | |
1176 | struct vm_area_struct *dst_vma) | |
1177 | { | |
1178 | /* Only allow moving if both have the same access and protection */ | |
1179 | if ((src_vma->vm_flags & VM_ACCESS_FLAGS) != (dst_vma->vm_flags & VM_ACCESS_FLAGS) || | |
1180 | pgprot_val(src_vma->vm_page_prot) != pgprot_val(dst_vma->vm_page_prot)) | |
1181 | return -EINVAL; | |
1182 | ||
1183 | /* Only allow moving if both are mlocked or both aren't */ | |
1184 | if ((src_vma->vm_flags & VM_LOCKED) != (dst_vma->vm_flags & VM_LOCKED)) | |
1185 | return -EINVAL; | |
1186 | ||
1187 | /* | |
1188 | * For now, we keep it simple and only move between writable VMAs. | |
1189 | * Access flags are equal, therefore cheching only the source is enough. | |
1190 | */ | |
1191 | if (!(src_vma->vm_flags & VM_WRITE)) | |
1192 | return -EINVAL; | |
1193 | ||
1194 | /* Check if vma flags indicate content which can be moved */ | |
1195 | if (!vma_move_compatible(src_vma) || !vma_move_compatible(dst_vma)) | |
1196 | return -EINVAL; | |
1197 | ||
1198 | /* Ensure dst_vma is registered in uffd we are operating on */ | |
1199 | if (!dst_vma->vm_userfaultfd_ctx.ctx || | |
1200 | dst_vma->vm_userfaultfd_ctx.ctx != ctx) | |
1201 | return -EINVAL; | |
1202 | ||
1203 | /* Only allow moving across anonymous vmas */ | |
1204 | if (!vma_is_anonymous(src_vma) || !vma_is_anonymous(dst_vma)) | |
1205 | return -EINVAL; | |
1206 | ||
1207 | /* | |
1208 | * Ensure the dst_vma has a anon_vma or this page | |
1209 | * would get a NULL anon_vma when moved in the | |
1210 | * dst_vma. | |
1211 | */ | |
1212 | if (unlikely(anon_vma_prepare(dst_vma))) | |
1213 | return -ENOMEM; | |
1214 | ||
1215 | return 0; | |
1216 | } | |
1217 | ||
1218 | /** | |
1219 | * move_pages - move arbitrary anonymous pages of an existing vma | |
1220 | * @ctx: pointer to the userfaultfd context | |
1221 | * @mm: the address space to move pages | |
1222 | * @dst_start: start of the destination virtual memory range | |
1223 | * @src_start: start of the source virtual memory range | |
1224 | * @len: length of the virtual memory range | |
1225 | * @mode: flags from uffdio_move.mode | |
1226 | * | |
1227 | * Must be called with mmap_lock held for read. | |
1228 | * | |
1229 | * move_pages() remaps arbitrary anonymous pages atomically in zero | |
1230 | * copy. It only works on non shared anonymous pages because those can | |
1231 | * be relocated without generating non linear anon_vmas in the rmap | |
1232 | * code. | |
1233 | * | |
1234 | * It provides a zero copy mechanism to handle userspace page faults. | |
1235 | * The source vma pages should have mapcount == 1, which can be | |
1236 | * enforced by using madvise(MADV_DONTFORK) on src vma. | |
1237 | * | |
1238 | * The thread receiving the page during the userland page fault | |
1239 | * will receive the faulting page in the source vma through the network, | |
1240 | * storage or any other I/O device (MADV_DONTFORK in the source vma | |
1241 | * avoids move_pages() to fail with -EBUSY if the process forks before | |
1242 | * move_pages() is called), then it will call move_pages() to map the | |
1243 | * page in the faulting address in the destination vma. | |
1244 | * | |
1245 | * This userfaultfd command works purely via pagetables, so it's the | |
1246 | * most efficient way to move physical non shared anonymous pages | |
1247 | * across different virtual addresses. Unlike mremap()/mmap()/munmap() | |
1248 | * it does not create any new vmas. The mapping in the destination | |
1249 | * address is atomic. | |
1250 | * | |
1251 | * It only works if the vma protection bits are identical from the | |
1252 | * source and destination vma. | |
1253 | * | |
1254 | * It can remap non shared anonymous pages within the same vma too. | |
1255 | * | |
1256 | * If the source virtual memory range has any unmapped holes, or if | |
1257 | * the destination virtual memory range is not a whole unmapped hole, | |
1258 | * move_pages() will fail respectively with -ENOENT or -EEXIST. This | |
1259 | * provides a very strict behavior to avoid any chance of memory | |
1260 | * corruption going unnoticed if there are userland race conditions. | |
1261 | * Only one thread should resolve the userland page fault at any given | |
1262 | * time for any given faulting address. This means that if two threads | |
1263 | * try to both call move_pages() on the same destination address at the | |
1264 | * same time, the second thread will get an explicit error from this | |
1265 | * command. | |
1266 | * | |
1267 | * The command retval will return "len" is successful. The command | |
1268 | * however can be interrupted by fatal signals or errors. If | |
1269 | * interrupted it will return the number of bytes successfully | |
1270 | * remapped before the interruption if any, or the negative error if | |
1271 | * none. It will never return zero. Either it will return an error or | |
1272 | * an amount of bytes successfully moved. If the retval reports a | |
1273 | * "short" remap, the move_pages() command should be repeated by | |
1274 | * userland with src+retval, dst+reval, len-retval if it wants to know | |
1275 | * about the error that interrupted it. | |
1276 | * | |
1277 | * The UFFDIO_MOVE_MODE_ALLOW_SRC_HOLES flag can be specified to | |
1278 | * prevent -ENOENT errors to materialize if there are holes in the | |
1279 | * source virtual range that is being remapped. The holes will be | |
1280 | * accounted as successfully remapped in the retval of the | |
1281 | * command. This is mostly useful to remap hugepage naturally aligned | |
1282 | * virtual regions without knowing if there are transparent hugepage | |
1283 | * in the regions or not, but preventing the risk of having to split | |
1284 | * the hugepmd during the remap. | |
1285 | * | |
1286 | * If there's any rmap walk that is taking the anon_vma locks without | |
1287 | * first obtaining the folio lock (the only current instance is | |
1288 | * folio_referenced), they will have to verify if the folio->mapping | |
1289 | * has changed after taking the anon_vma lock. If it changed they | |
1290 | * should release the lock and retry obtaining a new anon_vma, because | |
1291 | * it means the anon_vma was changed by move_pages() before the lock | |
1292 | * could be obtained. This is the only additional complexity added to | |
1293 | * the rmap code to provide this anonymous page remapping functionality. | |
1294 | */ | |
1295 | ssize_t move_pages(struct userfaultfd_ctx *ctx, struct mm_struct *mm, | |
1296 | unsigned long dst_start, unsigned long src_start, | |
1297 | unsigned long len, __u64 mode) | |
1298 | { | |
1299 | struct vm_area_struct *src_vma, *dst_vma; | |
1300 | unsigned long src_addr, dst_addr; | |
1301 | pmd_t *src_pmd, *dst_pmd; | |
1302 | long err = -EINVAL; | |
1303 | ssize_t moved = 0; | |
1304 | ||
1305 | /* Sanitize the command parameters. */ | |
1306 | if (WARN_ON_ONCE(src_start & ~PAGE_MASK) || | |
1307 | WARN_ON_ONCE(dst_start & ~PAGE_MASK) || | |
1308 | WARN_ON_ONCE(len & ~PAGE_MASK)) | |
1309 | goto out; | |
1310 | ||
1311 | /* Does the address range wrap, or is the span zero-sized? */ | |
1312 | if (WARN_ON_ONCE(src_start + len <= src_start) || | |
1313 | WARN_ON_ONCE(dst_start + len <= dst_start)) | |
1314 | goto out; | |
1315 | ||
1316 | /* | |
1317 | * Make sure the vma is not shared, that the src and dst remap | |
1318 | * ranges are both valid and fully within a single existing | |
1319 | * vma. | |
1320 | */ | |
1321 | src_vma = find_vma(mm, src_start); | |
1322 | if (!src_vma || (src_vma->vm_flags & VM_SHARED)) | |
1323 | goto out; | |
1324 | if (src_start < src_vma->vm_start || | |
1325 | src_start + len > src_vma->vm_end) | |
1326 | goto out; | |
1327 | ||
1328 | dst_vma = find_vma(mm, dst_start); | |
1329 | if (!dst_vma || (dst_vma->vm_flags & VM_SHARED)) | |
1330 | goto out; | |
1331 | if (dst_start < dst_vma->vm_start || | |
1332 | dst_start + len > dst_vma->vm_end) | |
1333 | goto out; | |
1334 | ||
1335 | err = validate_move_areas(ctx, src_vma, dst_vma); | |
1336 | if (err) | |
1337 | goto out; | |
1338 | ||
1339 | for (src_addr = src_start, dst_addr = dst_start; | |
1340 | src_addr < src_start + len;) { | |
1341 | spinlock_t *ptl; | |
1342 | pmd_t dst_pmdval; | |
1343 | unsigned long step_size; | |
1344 | ||
1345 | /* | |
1346 | * Below works because anonymous area would not have a | |
1347 | * transparent huge PUD. If file-backed support is added, | |
1348 | * that case would need to be handled here. | |
1349 | */ | |
1350 | src_pmd = mm_find_pmd(mm, src_addr); | |
1351 | if (unlikely(!src_pmd)) { | |
1352 | if (!(mode & UFFDIO_MOVE_MODE_ALLOW_SRC_HOLES)) { | |
1353 | err = -ENOENT; | |
1354 | break; | |
1355 | } | |
1356 | src_pmd = mm_alloc_pmd(mm, src_addr); | |
1357 | if (unlikely(!src_pmd)) { | |
1358 | err = -ENOMEM; | |
1359 | break; | |
1360 | } | |
1361 | } | |
1362 | dst_pmd = mm_alloc_pmd(mm, dst_addr); | |
1363 | if (unlikely(!dst_pmd)) { | |
1364 | err = -ENOMEM; | |
1365 | break; | |
1366 | } | |
1367 | ||
1368 | dst_pmdval = pmdp_get_lockless(dst_pmd); | |
1369 | /* | |
1370 | * If the dst_pmd is mapped as THP don't override it and just | |
1371 | * be strict. If dst_pmd changes into TPH after this check, the | |
1372 | * move_pages_huge_pmd() will detect the change and retry | |
1373 | * while move_pages_pte() will detect the change and fail. | |
1374 | */ | |
1375 | if (unlikely(pmd_trans_huge(dst_pmdval))) { | |
1376 | err = -EEXIST; | |
1377 | break; | |
1378 | } | |
1379 | ||
1380 | ptl = pmd_trans_huge_lock(src_pmd, src_vma); | |
1381 | if (ptl) { | |
1382 | if (pmd_devmap(*src_pmd)) { | |
1383 | spin_unlock(ptl); | |
1384 | err = -ENOENT; | |
1385 | break; | |
1386 | } | |
1387 | ||
1388 | /* Check if we can move the pmd without splitting it. */ | |
1389 | if (move_splits_huge_pmd(dst_addr, src_addr, src_start + len) || | |
1390 | !pmd_none(dst_pmdval)) { | |
1391 | struct folio *folio = pfn_folio(pmd_pfn(*src_pmd)); | |
1392 | ||
1393 | if (!folio || !PageAnonExclusive(&folio->page)) { | |
1394 | spin_unlock(ptl); | |
1395 | err = -EBUSY; | |
1396 | break; | |
1397 | } | |
1398 | ||
1399 | spin_unlock(ptl); | |
1400 | split_huge_pmd(src_vma, src_pmd, src_addr); | |
1401 | /* The folio will be split by move_pages_pte() */ | |
1402 | continue; | |
1403 | } | |
1404 | ||
1405 | err = move_pages_huge_pmd(mm, dst_pmd, src_pmd, | |
1406 | dst_pmdval, dst_vma, src_vma, | |
1407 | dst_addr, src_addr); | |
1408 | step_size = HPAGE_PMD_SIZE; | |
1409 | } else { | |
1410 | if (pmd_none(*src_pmd)) { | |
1411 | if (!(mode & UFFDIO_MOVE_MODE_ALLOW_SRC_HOLES)) { | |
1412 | err = -ENOENT; | |
1413 | break; | |
1414 | } | |
1415 | if (unlikely(__pte_alloc(mm, src_pmd))) { | |
1416 | err = -ENOMEM; | |
1417 | break; | |
1418 | } | |
1419 | } | |
1420 | ||
1421 | if (unlikely(pte_alloc(mm, dst_pmd))) { | |
1422 | err = -ENOMEM; | |
1423 | break; | |
1424 | } | |
1425 | ||
1426 | err = move_pages_pte(mm, dst_pmd, src_pmd, | |
1427 | dst_vma, src_vma, | |
1428 | dst_addr, src_addr, mode); | |
1429 | step_size = PAGE_SIZE; | |
1430 | } | |
1431 | ||
1432 | cond_resched(); | |
1433 | ||
1434 | if (fatal_signal_pending(current)) { | |
1435 | /* Do not override an error */ | |
1436 | if (!err || err == -EAGAIN) | |
1437 | err = -EINTR; | |
1438 | break; | |
1439 | } | |
1440 | ||
1441 | if (err) { | |
1442 | if (err == -EAGAIN) | |
1443 | continue; | |
1444 | break; | |
1445 | } | |
1446 | ||
1447 | /* Proceed to the next page */ | |
1448 | dst_addr += step_size; | |
1449 | src_addr += step_size; | |
1450 | moved += step_size; | |
1451 | } | |
1452 | ||
1453 | out: | |
1454 | VM_WARN_ON(moved < 0); | |
1455 | VM_WARN_ON(err > 0); | |
1456 | VM_WARN_ON(!moved && !err); | |
1457 | return moved ? moved : err; | |
1458 | } |