]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - mm/userfaultfd.c
userfaultfd: remove unnecessary WARN_ON() in __mcopy_atomic_hugetlb()
[mirror_ubuntu-jammy-kernel.git] / mm / userfaultfd.c
CommitLineData
20c8ccb1 1// SPDX-License-Identifier: GPL-2.0-only
c1a4de99
AA
2/*
3 * mm/userfaultfd.c
4 *
5 * Copyright (C) 2015 Red Hat, Inc.
c1a4de99
AA
6 */
7
8#include <linux/mm.h>
174cd4b1 9#include <linux/sched/signal.h>
c1a4de99
AA
10#include <linux/pagemap.h>
11#include <linux/rmap.h>
12#include <linux/swap.h>
13#include <linux/swapops.h>
14#include <linux/userfaultfd_k.h>
15#include <linux/mmu_notifier.h>
60d4d2d2 16#include <linux/hugetlb.h>
26071ced 17#include <linux/shmem_fs.h>
c1a4de99
AA
18#include <asm/tlbflush.h>
19#include "internal.h"
20
21static int mcopy_atomic_pte(struct mm_struct *dst_mm,
22 pmd_t *dst_pmd,
23 struct vm_area_struct *dst_vma,
24 unsigned long dst_addr,
b6ebaedb
AA
25 unsigned long src_addr,
26 struct page **pagep)
c1a4de99
AA
27{
28 struct mem_cgroup *memcg;
29 pte_t _dst_pte, *dst_pte;
30 spinlock_t *ptl;
c1a4de99
AA
31 void *page_kaddr;
32 int ret;
b6ebaedb 33 struct page *page;
e2a50c1f
AA
34 pgoff_t offset, max_off;
35 struct inode *inode;
c1a4de99 36
b6ebaedb
AA
37 if (!*pagep) {
38 ret = -ENOMEM;
39 page = alloc_page_vma(GFP_HIGHUSER_MOVABLE, dst_vma, dst_addr);
40 if (!page)
41 goto out;
42
43 page_kaddr = kmap_atomic(page);
44 ret = copy_from_user(page_kaddr,
45 (const void __user *) src_addr,
46 PAGE_SIZE);
47 kunmap_atomic(page_kaddr);
48
49 /* fallback to copy_from_user outside mmap_sem */
50 if (unlikely(ret)) {
9e368259 51 ret = -ENOENT;
b6ebaedb
AA
52 *pagep = page;
53 /* don't free the page */
54 goto out;
55 }
56 } else {
57 page = *pagep;
58 *pagep = NULL;
59 }
c1a4de99
AA
60
61 /*
62 * The memory barrier inside __SetPageUptodate makes sure that
63 * preceeding stores to the page contents become visible before
64 * the set_pte_at() write.
65 */
66 __SetPageUptodate(page);
67
68 ret = -ENOMEM;
f627c2f5 69 if (mem_cgroup_try_charge(page, dst_mm, GFP_KERNEL, &memcg, false))
c1a4de99
AA
70 goto out_release;
71
72 _dst_pte = mk_pte(page, dst_vma->vm_page_prot);
73 if (dst_vma->vm_flags & VM_WRITE)
74 _dst_pte = pte_mkwrite(pte_mkdirty(_dst_pte));
75
c1a4de99 76 dst_pte = pte_offset_map_lock(dst_mm, dst_pmd, dst_addr, &ptl);
e2a50c1f
AA
77 if (dst_vma->vm_file) {
78 /* the shmem MAP_PRIVATE case requires checking the i_size */
79 inode = dst_vma->vm_file->f_inode;
80 offset = linear_page_index(dst_vma, dst_addr);
81 max_off = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE);
82 ret = -EFAULT;
83 if (unlikely(offset >= max_off))
84 goto out_release_uncharge_unlock;
85 }
86 ret = -EEXIST;
c1a4de99
AA
87 if (!pte_none(*dst_pte))
88 goto out_release_uncharge_unlock;
89
90 inc_mm_counter(dst_mm, MM_ANONPAGES);
d281ee61 91 page_add_new_anon_rmap(page, dst_vma, dst_addr, false);
f627c2f5 92 mem_cgroup_commit_charge(page, memcg, false, false);
c1a4de99
AA
93 lru_cache_add_active_or_unevictable(page, dst_vma);
94
95 set_pte_at(dst_mm, dst_addr, dst_pte, _dst_pte);
96
97 /* No need to invalidate - it was non-present before */
98 update_mmu_cache(dst_vma, dst_addr, dst_pte);
99
100 pte_unmap_unlock(dst_pte, ptl);
101 ret = 0;
102out:
103 return ret;
104out_release_uncharge_unlock:
105 pte_unmap_unlock(dst_pte, ptl);
f627c2f5 106 mem_cgroup_cancel_charge(page, memcg, false);
c1a4de99 107out_release:
09cbfeaf 108 put_page(page);
c1a4de99 109 goto out;
c1a4de99
AA
110}
111
112static int mfill_zeropage_pte(struct mm_struct *dst_mm,
113 pmd_t *dst_pmd,
114 struct vm_area_struct *dst_vma,
115 unsigned long dst_addr)
116{
117 pte_t _dst_pte, *dst_pte;
118 spinlock_t *ptl;
119 int ret;
e2a50c1f
AA
120 pgoff_t offset, max_off;
121 struct inode *inode;
c1a4de99
AA
122
123 _dst_pte = pte_mkspecial(pfn_pte(my_zero_pfn(dst_addr),
124 dst_vma->vm_page_prot));
c1a4de99 125 dst_pte = pte_offset_map_lock(dst_mm, dst_pmd, dst_addr, &ptl);
e2a50c1f
AA
126 if (dst_vma->vm_file) {
127 /* the shmem MAP_PRIVATE case requires checking the i_size */
128 inode = dst_vma->vm_file->f_inode;
129 offset = linear_page_index(dst_vma, dst_addr);
130 max_off = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE);
131 ret = -EFAULT;
132 if (unlikely(offset >= max_off))
133 goto out_unlock;
134 }
135 ret = -EEXIST;
c1a4de99
AA
136 if (!pte_none(*dst_pte))
137 goto out_unlock;
138 set_pte_at(dst_mm, dst_addr, dst_pte, _dst_pte);
139 /* No need to invalidate - it was non-present before */
140 update_mmu_cache(dst_vma, dst_addr, dst_pte);
141 ret = 0;
142out_unlock:
143 pte_unmap_unlock(dst_pte, ptl);
144 return ret;
145}
146
147static pmd_t *mm_alloc_pmd(struct mm_struct *mm, unsigned long address)
148{
149 pgd_t *pgd;
c2febafc 150 p4d_t *p4d;
c1a4de99 151 pud_t *pud;
c1a4de99
AA
152
153 pgd = pgd_offset(mm, address);
c2febafc
KS
154 p4d = p4d_alloc(mm, pgd, address);
155 if (!p4d)
156 return NULL;
157 pud = pud_alloc(mm, p4d, address);
158 if (!pud)
159 return NULL;
160 /*
161 * Note that we didn't run this because the pmd was
162 * missing, the *pmd may be already established and in
163 * turn it may also be a trans_huge_pmd.
164 */
165 return pmd_alloc(mm, pud, address);
c1a4de99
AA
166}
167
60d4d2d2
MK
168#ifdef CONFIG_HUGETLB_PAGE
169/*
170 * __mcopy_atomic processing for HUGETLB vmas. Note that this routine is
171 * called with mmap_sem held, it will release mmap_sem before returning.
172 */
173static __always_inline ssize_t __mcopy_atomic_hugetlb(struct mm_struct *dst_mm,
174 struct vm_area_struct *dst_vma,
175 unsigned long dst_start,
176 unsigned long src_start,
177 unsigned long len,
178 bool zeropage)
179{
1c9e8def
MK
180 int vm_alloc_shared = dst_vma->vm_flags & VM_SHARED;
181 int vm_shared = dst_vma->vm_flags & VM_SHARED;
60d4d2d2
MK
182 ssize_t err;
183 pte_t *dst_pte;
184 unsigned long src_addr, dst_addr;
185 long copied;
186 struct page *page;
60d4d2d2
MK
187 unsigned long vma_hpagesize;
188 pgoff_t idx;
189 u32 hash;
190 struct address_space *mapping;
191
192 /*
193 * There is no default zero huge page for all huge page sizes as
194 * supported by hugetlb. A PMD_SIZE huge pages may exist as used
195 * by THP. Since we can not reliably insert a zero page, this
196 * feature is not supported.
197 */
198 if (zeropage) {
199 up_read(&dst_mm->mmap_sem);
200 return -EINVAL;
201 }
202
203 src_addr = src_start;
204 dst_addr = dst_start;
205 copied = 0;
206 page = NULL;
207 vma_hpagesize = vma_kernel_pagesize(dst_vma);
208
209 /*
210 * Validate alignment based on huge page size
211 */
212 err = -EINVAL;
213 if (dst_start & (vma_hpagesize - 1) || len & (vma_hpagesize - 1))
214 goto out_unlock;
215
216retry:
217 /*
218 * On routine entry dst_vma is set. If we had to drop mmap_sem and
219 * retry, dst_vma will be set to NULL and we must lookup again.
220 */
221 if (!dst_vma) {
27d02568 222 err = -ENOENT;
60d4d2d2
MK
223 dst_vma = find_vma(dst_mm, dst_start);
224 if (!dst_vma || !is_vm_hugetlb_page(dst_vma))
225 goto out_unlock;
60d4d2d2 226 /*
29ec9066
AA
227 * Check the vma is registered in uffd, this is
228 * required to enforce the VM_MAYWRITE check done at
229 * uffd registration time.
60d4d2d2 230 */
27d02568
MR
231 if (!dst_vma->vm_userfaultfd_ctx.ctx)
232 goto out_unlock;
233
60d4d2d2
MK
234 if (dst_start < dst_vma->vm_start ||
235 dst_start + len > dst_vma->vm_end)
236 goto out_unlock;
1c9e8def 237
27d02568
MR
238 err = -EINVAL;
239 if (vma_hpagesize != vma_kernel_pagesize(dst_vma))
240 goto out_unlock;
241
1c9e8def 242 vm_shared = dst_vma->vm_flags & VM_SHARED;
60d4d2d2
MK
243 }
244
60d4d2d2 245 /*
1c9e8def 246 * If not shared, ensure the dst_vma has a anon_vma.
60d4d2d2
MK
247 */
248 err = -ENOMEM;
1c9e8def
MK
249 if (!vm_shared) {
250 if (unlikely(anon_vma_prepare(dst_vma)))
251 goto out_unlock;
252 }
60d4d2d2 253
60d4d2d2
MK
254 while (src_addr < src_start + len) {
255 pte_t dst_pteval;
256
257 BUG_ON(dst_addr >= dst_start + len);
60d4d2d2
MK
258
259 /*
ddeaab32 260 * Serialize via hugetlb_fault_mutex
60d4d2d2 261 */
b43a9990 262 idx = linear_page_index(dst_vma, dst_addr);
ddeaab32 263 mapping = dst_vma->vm_file->f_mapping;
188b04a7 264 hash = hugetlb_fault_mutex_hash(mapping, idx);
60d4d2d2
MK
265 mutex_lock(&hugetlb_fault_mutex_table[hash]);
266
267 err = -ENOMEM;
4fb07ee6 268 dst_pte = huge_pte_alloc(dst_mm, dst_addr, vma_hpagesize);
60d4d2d2
MK
269 if (!dst_pte) {
270 mutex_unlock(&hugetlb_fault_mutex_table[hash]);
271 goto out_unlock;
272 }
273
274 err = -EEXIST;
275 dst_pteval = huge_ptep_get(dst_pte);
276 if (!huge_pte_none(dst_pteval)) {
277 mutex_unlock(&hugetlb_fault_mutex_table[hash]);
278 goto out_unlock;
279 }
280
281 err = hugetlb_mcopy_atomic_pte(dst_mm, dst_pte, dst_vma,
282 dst_addr, src_addr, &page);
283
284 mutex_unlock(&hugetlb_fault_mutex_table[hash]);
1c9e8def 285 vm_alloc_shared = vm_shared;
60d4d2d2
MK
286
287 cond_resched();
288
9e368259 289 if (unlikely(err == -ENOENT)) {
60d4d2d2
MK
290 up_read(&dst_mm->mmap_sem);
291 BUG_ON(!page);
292
293 err = copy_huge_page_from_user(page,
294 (const void __user *)src_addr,
4fb07ee6
WY
295 vma_hpagesize / PAGE_SIZE,
296 true);
60d4d2d2
MK
297 if (unlikely(err)) {
298 err = -EFAULT;
299 goto out;
300 }
301 down_read(&dst_mm->mmap_sem);
302
303 dst_vma = NULL;
304 goto retry;
305 } else
306 BUG_ON(page);
307
308 if (!err) {
309 dst_addr += vma_hpagesize;
310 src_addr += vma_hpagesize;
311 copied += vma_hpagesize;
312
313 if (fatal_signal_pending(current))
314 err = -EINTR;
315 }
316 if (err)
317 break;
318 }
319
320out_unlock:
321 up_read(&dst_mm->mmap_sem);
322out:
21205bf8
MK
323 if (page) {
324 /*
325 * We encountered an error and are about to free a newly
1c9e8def
MK
326 * allocated huge page.
327 *
328 * Reservation handling is very subtle, and is different for
329 * private and shared mappings. See the routine
330 * restore_reserve_on_error for details. Unfortunately, we
331 * can not call restore_reserve_on_error now as it would
332 * require holding mmap_sem.
333 *
334 * If a reservation for the page existed in the reservation
335 * map of a private mapping, the map was modified to indicate
336 * the reservation was consumed when the page was allocated.
337 * We clear the PagePrivate flag now so that the global
21205bf8
MK
338 * reserve count will not be incremented in free_huge_page.
339 * The reservation map will still indicate the reservation
340 * was consumed and possibly prevent later page allocation.
1c9e8def
MK
341 * This is better than leaking a global reservation. If no
342 * reservation existed, it is still safe to clear PagePrivate
343 * as no adjustments to reservation counts were made during
344 * allocation.
345 *
346 * The reservation map for shared mappings indicates which
347 * pages have reservations. When a huge page is allocated
348 * for an address with a reservation, no change is made to
349 * the reserve map. In this case PagePrivate will be set
350 * to indicate that the global reservation count should be
351 * incremented when the page is freed. This is the desired
352 * behavior. However, when a huge page is allocated for an
353 * address without a reservation a reservation entry is added
354 * to the reservation map, and PagePrivate will not be set.
355 * When the page is freed, the global reserve count will NOT
356 * be incremented and it will appear as though we have leaked
357 * reserved page. In this case, set PagePrivate so that the
358 * global reserve count will be incremented to match the
359 * reservation map entry which was created.
360 *
361 * Note that vm_alloc_shared is based on the flags of the vma
362 * for which the page was originally allocated. dst_vma could
363 * be different or NULL on error.
21205bf8 364 */
1c9e8def
MK
365 if (vm_alloc_shared)
366 SetPagePrivate(page);
367 else
368 ClearPagePrivate(page);
60d4d2d2 369 put_page(page);
21205bf8 370 }
60d4d2d2
MK
371 BUG_ON(copied < 0);
372 BUG_ON(err > 0);
373 BUG_ON(!copied && !err);
374 return copied ? copied : err;
375}
376#else /* !CONFIG_HUGETLB_PAGE */
377/* fail at build time if gcc attempts to use this */
378extern ssize_t __mcopy_atomic_hugetlb(struct mm_struct *dst_mm,
379 struct vm_area_struct *dst_vma,
380 unsigned long dst_start,
381 unsigned long src_start,
382 unsigned long len,
383 bool zeropage);
384#endif /* CONFIG_HUGETLB_PAGE */
385
3217d3c7
MR
386static __always_inline ssize_t mfill_atomic_pte(struct mm_struct *dst_mm,
387 pmd_t *dst_pmd,
388 struct vm_area_struct *dst_vma,
389 unsigned long dst_addr,
390 unsigned long src_addr,
391 struct page **page,
392 bool zeropage)
393{
394 ssize_t err;
395
5b51072e
AA
396 /*
397 * The normal page fault path for a shmem will invoke the
398 * fault, fill the hole in the file and COW it right away. The
399 * result generates plain anonymous memory. So when we are
400 * asked to fill an hole in a MAP_PRIVATE shmem mapping, we'll
401 * generate anonymous memory directly without actually filling
402 * the hole. For the MAP_PRIVATE case the robustness check
403 * only happens in the pagetable (to verify it's still none)
404 * and not in the radix tree.
405 */
406 if (!(dst_vma->vm_flags & VM_SHARED)) {
3217d3c7
MR
407 if (!zeropage)
408 err = mcopy_atomic_pte(dst_mm, dst_pmd, dst_vma,
409 dst_addr, src_addr, page);
410 else
411 err = mfill_zeropage_pte(dst_mm, dst_pmd,
412 dst_vma, dst_addr);
413 } else {
8fb44e54 414 if (!zeropage)
3217d3c7
MR
415 err = shmem_mcopy_atomic_pte(dst_mm, dst_pmd,
416 dst_vma, dst_addr,
417 src_addr, page);
8fb44e54
MR
418 else
419 err = shmem_mfill_zeropage_pte(dst_mm, dst_pmd,
420 dst_vma, dst_addr);
3217d3c7
MR
421 }
422
423 return err;
424}
425
c1a4de99
AA
426static __always_inline ssize_t __mcopy_atomic(struct mm_struct *dst_mm,
427 unsigned long dst_start,
428 unsigned long src_start,
429 unsigned long len,
df2cc96e
MR
430 bool zeropage,
431 bool *mmap_changing)
c1a4de99
AA
432{
433 struct vm_area_struct *dst_vma;
434 ssize_t err;
435 pmd_t *dst_pmd;
436 unsigned long src_addr, dst_addr;
b6ebaedb
AA
437 long copied;
438 struct page *page;
c1a4de99
AA
439
440 /*
441 * Sanitize the command parameters:
442 */
443 BUG_ON(dst_start & ~PAGE_MASK);
444 BUG_ON(len & ~PAGE_MASK);
445
446 /* Does the address range wrap, or is the span zero-sized? */
447 BUG_ON(src_start + len <= src_start);
448 BUG_ON(dst_start + len <= dst_start);
449
b6ebaedb
AA
450 src_addr = src_start;
451 dst_addr = dst_start;
452 copied = 0;
453 page = NULL;
454retry:
c1a4de99
AA
455 down_read(&dst_mm->mmap_sem);
456
df2cc96e
MR
457 /*
458 * If memory mappings are changing because of non-cooperative
459 * operation (e.g. mremap) running in parallel, bail out and
460 * request the user to retry later
461 */
462 err = -EAGAIN;
463 if (mmap_changing && READ_ONCE(*mmap_changing))
464 goto out_unlock;
465
c1a4de99
AA
466 /*
467 * Make sure the vma is not shared, that the dst range is
468 * both valid and fully within a single existing vma.
469 */
27d02568 470 err = -ENOENT;
c1a4de99 471 dst_vma = find_vma(dst_mm, dst_start);
26071ced
MR
472 if (!dst_vma)
473 goto out_unlock;
1c9e8def 474 /*
29ec9066
AA
475 * Check the vma is registered in uffd, this is required to
476 * enforce the VM_MAYWRITE check done at uffd registration
477 * time.
1c9e8def 478 */
27d02568 479 if (!dst_vma->vm_userfaultfd_ctx.ctx)
b6ebaedb 480 goto out_unlock;
1c9e8def 481
c1a4de99
AA
482 if (dst_start < dst_vma->vm_start ||
483 dst_start + len > dst_vma->vm_end)
b6ebaedb 484 goto out_unlock;
c1a4de99 485
27d02568
MR
486 err = -EINVAL;
487 /*
488 * shmem_zero_setup is invoked in mmap for MAP_ANONYMOUS|MAP_SHARED but
489 * it will overwrite vm_ops, so vma_is_anonymous must return false.
490 */
491 if (WARN_ON_ONCE(vma_is_anonymous(dst_vma) &&
492 dst_vma->vm_flags & VM_SHARED))
493 goto out_unlock;
494
60d4d2d2
MK
495 /*
496 * If this is a HUGETLB vma, pass off to appropriate routine
497 */
498 if (is_vm_hugetlb_page(dst_vma))
499 return __mcopy_atomic_hugetlb(dst_mm, dst_vma, dst_start,
500 src_start, len, zeropage);
501
26071ced 502 if (!vma_is_anonymous(dst_vma) && !vma_is_shmem(dst_vma))
b6ebaedb 503 goto out_unlock;
c1a4de99
AA
504
505 /*
506 * Ensure the dst_vma has a anon_vma or this page
507 * would get a NULL anon_vma when moved in the
508 * dst_vma.
509 */
510 err = -ENOMEM;
5b51072e
AA
511 if (!(dst_vma->vm_flags & VM_SHARED) &&
512 unlikely(anon_vma_prepare(dst_vma)))
b6ebaedb 513 goto out_unlock;
c1a4de99 514
b6ebaedb 515 while (src_addr < src_start + len) {
c1a4de99 516 pmd_t dst_pmdval;
b6ebaedb 517
c1a4de99 518 BUG_ON(dst_addr >= dst_start + len);
b6ebaedb 519
c1a4de99
AA
520 dst_pmd = mm_alloc_pmd(dst_mm, dst_addr);
521 if (unlikely(!dst_pmd)) {
522 err = -ENOMEM;
523 break;
524 }
525
526 dst_pmdval = pmd_read_atomic(dst_pmd);
527 /*
528 * If the dst_pmd is mapped as THP don't
529 * override it and just be strict.
530 */
531 if (unlikely(pmd_trans_huge(dst_pmdval))) {
532 err = -EEXIST;
533 break;
534 }
535 if (unlikely(pmd_none(dst_pmdval)) &&
4cf58924 536 unlikely(__pte_alloc(dst_mm, dst_pmd))) {
c1a4de99
AA
537 err = -ENOMEM;
538 break;
539 }
540 /* If an huge pmd materialized from under us fail */
541 if (unlikely(pmd_trans_huge(*dst_pmd))) {
542 err = -EFAULT;
543 break;
544 }
545
546 BUG_ON(pmd_none(*dst_pmd));
547 BUG_ON(pmd_trans_huge(*dst_pmd));
548
3217d3c7
MR
549 err = mfill_atomic_pte(dst_mm, dst_pmd, dst_vma, dst_addr,
550 src_addr, &page, zeropage);
c1a4de99
AA
551 cond_resched();
552
9e368259 553 if (unlikely(err == -ENOENT)) {
b6ebaedb
AA
554 void *page_kaddr;
555
556 up_read(&dst_mm->mmap_sem);
557 BUG_ON(!page);
558
559 page_kaddr = kmap(page);
560 err = copy_from_user(page_kaddr,
561 (const void __user *) src_addr,
562 PAGE_SIZE);
563 kunmap(page);
564 if (unlikely(err)) {
565 err = -EFAULT;
566 goto out;
567 }
568 goto retry;
569 } else
570 BUG_ON(page);
571
c1a4de99
AA
572 if (!err) {
573 dst_addr += PAGE_SIZE;
574 src_addr += PAGE_SIZE;
575 copied += PAGE_SIZE;
576
577 if (fatal_signal_pending(current))
578 err = -EINTR;
579 }
580 if (err)
581 break;
582 }
583
b6ebaedb 584out_unlock:
c1a4de99 585 up_read(&dst_mm->mmap_sem);
b6ebaedb
AA
586out:
587 if (page)
09cbfeaf 588 put_page(page);
c1a4de99
AA
589 BUG_ON(copied < 0);
590 BUG_ON(err > 0);
591 BUG_ON(!copied && !err);
592 return copied ? copied : err;
593}
594
595ssize_t mcopy_atomic(struct mm_struct *dst_mm, unsigned long dst_start,
df2cc96e
MR
596 unsigned long src_start, unsigned long len,
597 bool *mmap_changing)
c1a4de99 598{
df2cc96e
MR
599 return __mcopy_atomic(dst_mm, dst_start, src_start, len, false,
600 mmap_changing);
c1a4de99
AA
601}
602
603ssize_t mfill_zeropage(struct mm_struct *dst_mm, unsigned long start,
df2cc96e 604 unsigned long len, bool *mmap_changing)
c1a4de99 605{
df2cc96e 606 return __mcopy_atomic(dst_mm, start, 0, len, true, mmap_changing);
c1a4de99 607}