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