mm: mremap: downgrade mmap_sem to read when shrinking
[muen/linux.git] / mm / mremap.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  *      mm/mremap.c
4  *
5  *      (C) Copyright 1996 Linus Torvalds
6  *
7  *      Address space accounting code   <alan@lxorguk.ukuu.org.uk>
8  *      (C) Copyright 2002 Red Hat Inc, All Rights Reserved
9  */
10
11 #include <linux/mm.h>
12 #include <linux/hugetlb.h>
13 #include <linux/shm.h>
14 #include <linux/ksm.h>
15 #include <linux/mman.h>
16 #include <linux/swap.h>
17 #include <linux/capability.h>
18 #include <linux/fs.h>
19 #include <linux/swapops.h>
20 #include <linux/highmem.h>
21 #include <linux/security.h>
22 #include <linux/syscalls.h>
23 #include <linux/mmu_notifier.h>
24 #include <linux/uaccess.h>
25 #include <linux/mm-arch-hooks.h>
26 #include <linux/userfaultfd_k.h>
27
28 #include <asm/cacheflush.h>
29 #include <asm/tlbflush.h>
30
31 #include "internal.h"
32
33 static pmd_t *get_old_pmd(struct mm_struct *mm, unsigned long addr)
34 {
35         pgd_t *pgd;
36         p4d_t *p4d;
37         pud_t *pud;
38         pmd_t *pmd;
39
40         pgd = pgd_offset(mm, addr);
41         if (pgd_none_or_clear_bad(pgd))
42                 return NULL;
43
44         p4d = p4d_offset(pgd, addr);
45         if (p4d_none_or_clear_bad(p4d))
46                 return NULL;
47
48         pud = pud_offset(p4d, addr);
49         if (pud_none_or_clear_bad(pud))
50                 return NULL;
51
52         pmd = pmd_offset(pud, addr);
53         if (pmd_none(*pmd))
54                 return NULL;
55
56         return pmd;
57 }
58
59 static pmd_t *alloc_new_pmd(struct mm_struct *mm, struct vm_area_struct *vma,
60                             unsigned long addr)
61 {
62         pgd_t *pgd;
63         p4d_t *p4d;
64         pud_t *pud;
65         pmd_t *pmd;
66
67         pgd = pgd_offset(mm, addr);
68         p4d = p4d_alloc(mm, pgd, addr);
69         if (!p4d)
70                 return NULL;
71         pud = pud_alloc(mm, p4d, addr);
72         if (!pud)
73                 return NULL;
74
75         pmd = pmd_alloc(mm, pud, addr);
76         if (!pmd)
77                 return NULL;
78
79         VM_BUG_ON(pmd_trans_huge(*pmd));
80
81         return pmd;
82 }
83
84 static void take_rmap_locks(struct vm_area_struct *vma)
85 {
86         if (vma->vm_file)
87                 i_mmap_lock_write(vma->vm_file->f_mapping);
88         if (vma->anon_vma)
89                 anon_vma_lock_write(vma->anon_vma);
90 }
91
92 static void drop_rmap_locks(struct vm_area_struct *vma)
93 {
94         if (vma->anon_vma)
95                 anon_vma_unlock_write(vma->anon_vma);
96         if (vma->vm_file)
97                 i_mmap_unlock_write(vma->vm_file->f_mapping);
98 }
99
100 static pte_t move_soft_dirty_pte(pte_t pte)
101 {
102         /*
103          * Set soft dirty bit so we can notice
104          * in userspace the ptes were moved.
105          */
106 #ifdef CONFIG_MEM_SOFT_DIRTY
107         if (pte_present(pte))
108                 pte = pte_mksoft_dirty(pte);
109         else if (is_swap_pte(pte))
110                 pte = pte_swp_mksoft_dirty(pte);
111 #endif
112         return pte;
113 }
114
115 static void move_ptes(struct vm_area_struct *vma, pmd_t *old_pmd,
116                 unsigned long old_addr, unsigned long old_end,
117                 struct vm_area_struct *new_vma, pmd_t *new_pmd,
118                 unsigned long new_addr, bool need_rmap_locks)
119 {
120         struct mm_struct *mm = vma->vm_mm;
121         pte_t *old_pte, *new_pte, pte;
122         spinlock_t *old_ptl, *new_ptl;
123         bool force_flush = false;
124         unsigned long len = old_end - old_addr;
125
126         /*
127          * When need_rmap_locks is true, we take the i_mmap_rwsem and anon_vma
128          * locks to ensure that rmap will always observe either the old or the
129          * new ptes. This is the easiest way to avoid races with
130          * truncate_pagecache(), page migration, etc...
131          *
132          * When need_rmap_locks is false, we use other ways to avoid
133          * such races:
134          *
135          * - During exec() shift_arg_pages(), we use a specially tagged vma
136          *   which rmap call sites look for using is_vma_temporary_stack().
137          *
138          * - During mremap(), new_vma is often known to be placed after vma
139          *   in rmap traversal order. This ensures rmap will always observe
140          *   either the old pte, or the new pte, or both (the page table locks
141          *   serialize access to individual ptes, but only rmap traversal
142          *   order guarantees that we won't miss both the old and new ptes).
143          */
144         if (need_rmap_locks)
145                 take_rmap_locks(vma);
146
147         /*
148          * We don't have to worry about the ordering of src and dst
149          * pte locks because exclusive mmap_sem prevents deadlock.
150          */
151         old_pte = pte_offset_map_lock(mm, old_pmd, old_addr, &old_ptl);
152         new_pte = pte_offset_map(new_pmd, new_addr);
153         new_ptl = pte_lockptr(mm, new_pmd);
154         if (new_ptl != old_ptl)
155                 spin_lock_nested(new_ptl, SINGLE_DEPTH_NESTING);
156         flush_tlb_batched_pending(vma->vm_mm);
157         arch_enter_lazy_mmu_mode();
158
159         for (; old_addr < old_end; old_pte++, old_addr += PAGE_SIZE,
160                                    new_pte++, new_addr += PAGE_SIZE) {
161                 if (pte_none(*old_pte))
162                         continue;
163
164                 pte = ptep_get_and_clear(mm, old_addr, old_pte);
165                 /*
166                  * If we are remapping a valid PTE, make sure
167                  * to flush TLB before we drop the PTL for the
168                  * PTE.
169                  *
170                  * NOTE! Both old and new PTL matter: the old one
171                  * for racing with page_mkclean(), the new one to
172                  * make sure the physical page stays valid until
173                  * the TLB entry for the old mapping has been
174                  * flushed.
175                  */
176                 if (pte_present(pte))
177                         force_flush = true;
178                 pte = move_pte(pte, new_vma->vm_page_prot, old_addr, new_addr);
179                 pte = move_soft_dirty_pte(pte);
180                 set_pte_at(mm, new_addr, new_pte, pte);
181         }
182
183         arch_leave_lazy_mmu_mode();
184         if (force_flush)
185                 flush_tlb_range(vma, old_end - len, old_end);
186         if (new_ptl != old_ptl)
187                 spin_unlock(new_ptl);
188         pte_unmap(new_pte - 1);
189         pte_unmap_unlock(old_pte - 1, old_ptl);
190         if (need_rmap_locks)
191                 drop_rmap_locks(vma);
192 }
193
194 unsigned long move_page_tables(struct vm_area_struct *vma,
195                 unsigned long old_addr, struct vm_area_struct *new_vma,
196                 unsigned long new_addr, unsigned long len,
197                 bool need_rmap_locks)
198 {
199         unsigned long extent, next, old_end;
200         pmd_t *old_pmd, *new_pmd;
201         unsigned long mmun_start;       /* For mmu_notifiers */
202         unsigned long mmun_end;         /* For mmu_notifiers */
203
204         old_end = old_addr + len;
205         flush_cache_range(vma, old_addr, old_end);
206
207         mmun_start = old_addr;
208         mmun_end   = old_end;
209         mmu_notifier_invalidate_range_start(vma->vm_mm, mmun_start, mmun_end);
210
211         for (; old_addr < old_end; old_addr += extent, new_addr += extent) {
212                 cond_resched();
213                 next = (old_addr + PMD_SIZE) & PMD_MASK;
214                 /* even if next overflowed, extent below will be ok */
215                 extent = next - old_addr;
216                 if (extent > old_end - old_addr)
217                         extent = old_end - old_addr;
218                 old_pmd = get_old_pmd(vma->vm_mm, old_addr);
219                 if (!old_pmd)
220                         continue;
221                 new_pmd = alloc_new_pmd(vma->vm_mm, vma, new_addr);
222                 if (!new_pmd)
223                         break;
224                 if (is_swap_pmd(*old_pmd) || pmd_trans_huge(*old_pmd)) {
225                         if (extent == HPAGE_PMD_SIZE) {
226                                 bool moved;
227                                 /* See comment in move_ptes() */
228                                 if (need_rmap_locks)
229                                         take_rmap_locks(vma);
230                                 moved = move_huge_pmd(vma, old_addr, new_addr,
231                                                     old_end, old_pmd, new_pmd);
232                                 if (need_rmap_locks)
233                                         drop_rmap_locks(vma);
234                                 if (moved)
235                                         continue;
236                         }
237                         split_huge_pmd(vma, old_pmd, old_addr);
238                         if (pmd_trans_unstable(old_pmd))
239                                 continue;
240                 }
241                 if (pte_alloc(new_vma->vm_mm, new_pmd, new_addr))
242                         break;
243                 next = (new_addr + PMD_SIZE) & PMD_MASK;
244                 if (extent > next - new_addr)
245                         extent = next - new_addr;
246                 move_ptes(vma, old_pmd, old_addr, old_addr + extent, new_vma,
247                           new_pmd, new_addr, need_rmap_locks);
248         }
249
250         mmu_notifier_invalidate_range_end(vma->vm_mm, mmun_start, mmun_end);
251
252         return len + old_addr - old_end;        /* how much done */
253 }
254
255 static unsigned long move_vma(struct vm_area_struct *vma,
256                 unsigned long old_addr, unsigned long old_len,
257                 unsigned long new_len, unsigned long new_addr,
258                 bool *locked, struct vm_userfaultfd_ctx *uf,
259                 struct list_head *uf_unmap)
260 {
261         struct mm_struct *mm = vma->vm_mm;
262         struct vm_area_struct *new_vma;
263         unsigned long vm_flags = vma->vm_flags;
264         unsigned long new_pgoff;
265         unsigned long moved_len;
266         unsigned long excess = 0;
267         unsigned long hiwater_vm;
268         int split = 0;
269         int err;
270         bool need_rmap_locks;
271
272         /*
273          * We'd prefer to avoid failure later on in do_munmap:
274          * which may split one vma into three before unmapping.
275          */
276         if (mm->map_count >= sysctl_max_map_count - 3)
277                 return -ENOMEM;
278
279         /*
280          * Advise KSM to break any KSM pages in the area to be moved:
281          * it would be confusing if they were to turn up at the new
282          * location, where they happen to coincide with different KSM
283          * pages recently unmapped.  But leave vma->vm_flags as it was,
284          * so KSM can come around to merge on vma and new_vma afterwards.
285          */
286         err = ksm_madvise(vma, old_addr, old_addr + old_len,
287                                                 MADV_UNMERGEABLE, &vm_flags);
288         if (err)
289                 return err;
290
291         new_pgoff = vma->vm_pgoff + ((old_addr - vma->vm_start) >> PAGE_SHIFT);
292         new_vma = copy_vma(&vma, new_addr, new_len, new_pgoff,
293                            &need_rmap_locks);
294         if (!new_vma)
295                 return -ENOMEM;
296
297         moved_len = move_page_tables(vma, old_addr, new_vma, new_addr, old_len,
298                                      need_rmap_locks);
299         if (moved_len < old_len) {
300                 err = -ENOMEM;
301         } else if (vma->vm_ops && vma->vm_ops->mremap) {
302                 err = vma->vm_ops->mremap(new_vma);
303         }
304
305         if (unlikely(err)) {
306                 /*
307                  * On error, move entries back from new area to old,
308                  * which will succeed since page tables still there,
309                  * and then proceed to unmap new area instead of old.
310                  */
311                 move_page_tables(new_vma, new_addr, vma, old_addr, moved_len,
312                                  true);
313                 vma = new_vma;
314                 old_len = new_len;
315                 old_addr = new_addr;
316                 new_addr = err;
317         } else {
318                 mremap_userfaultfd_prep(new_vma, uf);
319                 arch_remap(mm, old_addr, old_addr + old_len,
320                            new_addr, new_addr + new_len);
321         }
322
323         /* Conceal VM_ACCOUNT so old reservation is not undone */
324         if (vm_flags & VM_ACCOUNT) {
325                 vma->vm_flags &= ~VM_ACCOUNT;
326                 excess = vma->vm_end - vma->vm_start - old_len;
327                 if (old_addr > vma->vm_start &&
328                     old_addr + old_len < vma->vm_end)
329                         split = 1;
330         }
331
332         /*
333          * If we failed to move page tables we still do total_vm increment
334          * since do_munmap() will decrement it by old_len == new_len.
335          *
336          * Since total_vm is about to be raised artificially high for a
337          * moment, we need to restore high watermark afterwards: if stats
338          * are taken meanwhile, total_vm and hiwater_vm appear too high.
339          * If this were a serious issue, we'd add a flag to do_munmap().
340          */
341         hiwater_vm = mm->hiwater_vm;
342         vm_stat_account(mm, vma->vm_flags, new_len >> PAGE_SHIFT);
343
344         /* Tell pfnmap has moved from this vma */
345         if (unlikely(vma->vm_flags & VM_PFNMAP))
346                 untrack_pfn_moved(vma);
347
348         if (do_munmap(mm, old_addr, old_len, uf_unmap) < 0) {
349                 /* OOM: unable to split vma, just get accounts right */
350                 vm_unacct_memory(excess >> PAGE_SHIFT);
351                 excess = 0;
352         }
353         mm->hiwater_vm = hiwater_vm;
354
355         /* Restore VM_ACCOUNT if one or two pieces of vma left */
356         if (excess) {
357                 vma->vm_flags |= VM_ACCOUNT;
358                 if (split)
359                         vma->vm_next->vm_flags |= VM_ACCOUNT;
360         }
361
362         if (vm_flags & VM_LOCKED) {
363                 mm->locked_vm += new_len >> PAGE_SHIFT;
364                 *locked = true;
365         }
366
367         return new_addr;
368 }
369
370 static struct vm_area_struct *vma_to_resize(unsigned long addr,
371         unsigned long old_len, unsigned long new_len, unsigned long *p)
372 {
373         struct mm_struct *mm = current->mm;
374         struct vm_area_struct *vma = find_vma(mm, addr);
375         unsigned long pgoff;
376
377         if (!vma || vma->vm_start > addr)
378                 return ERR_PTR(-EFAULT);
379
380         /*
381          * !old_len is a special case where an attempt is made to 'duplicate'
382          * a mapping.  This makes no sense for private mappings as it will
383          * instead create a fresh/new mapping unrelated to the original.  This
384          * is contrary to the basic idea of mremap which creates new mappings
385          * based on the original.  There are no known use cases for this
386          * behavior.  As a result, fail such attempts.
387          */
388         if (!old_len && !(vma->vm_flags & (VM_SHARED | VM_MAYSHARE))) {
389                 pr_warn_once("%s (%d): attempted to duplicate a private mapping with mremap.  This is not supported.\n", current->comm, current->pid);
390                 return ERR_PTR(-EINVAL);
391         }
392
393         if (is_vm_hugetlb_page(vma))
394                 return ERR_PTR(-EINVAL);
395
396         /* We can't remap across vm area boundaries */
397         if (old_len > vma->vm_end - addr)
398                 return ERR_PTR(-EFAULT);
399
400         if (new_len == old_len)
401                 return vma;
402
403         /* Need to be careful about a growing mapping */
404         pgoff = (addr - vma->vm_start) >> PAGE_SHIFT;
405         pgoff += vma->vm_pgoff;
406         if (pgoff + (new_len >> PAGE_SHIFT) < pgoff)
407                 return ERR_PTR(-EINVAL);
408
409         if (vma->vm_flags & (VM_DONTEXPAND | VM_PFNMAP))
410                 return ERR_PTR(-EFAULT);
411
412         if (vma->vm_flags & VM_LOCKED) {
413                 unsigned long locked, lock_limit;
414                 locked = mm->locked_vm << PAGE_SHIFT;
415                 lock_limit = rlimit(RLIMIT_MEMLOCK);
416                 locked += new_len - old_len;
417                 if (locked > lock_limit && !capable(CAP_IPC_LOCK))
418                         return ERR_PTR(-EAGAIN);
419         }
420
421         if (!may_expand_vm(mm, vma->vm_flags,
422                                 (new_len - old_len) >> PAGE_SHIFT))
423                 return ERR_PTR(-ENOMEM);
424
425         if (vma->vm_flags & VM_ACCOUNT) {
426                 unsigned long charged = (new_len - old_len) >> PAGE_SHIFT;
427                 if (security_vm_enough_memory_mm(mm, charged))
428                         return ERR_PTR(-ENOMEM);
429                 *p = charged;
430         }
431
432         return vma;
433 }
434
435 static unsigned long mremap_to(unsigned long addr, unsigned long old_len,
436                 unsigned long new_addr, unsigned long new_len, bool *locked,
437                 struct vm_userfaultfd_ctx *uf,
438                 struct list_head *uf_unmap_early,
439                 struct list_head *uf_unmap)
440 {
441         struct mm_struct *mm = current->mm;
442         struct vm_area_struct *vma;
443         unsigned long ret = -EINVAL;
444         unsigned long charged = 0;
445         unsigned long map_flags;
446
447         if (offset_in_page(new_addr))
448                 goto out;
449
450         if (new_len > TASK_SIZE || new_addr > TASK_SIZE - new_len)
451                 goto out;
452
453         /* Ensure the old/new locations do not overlap */
454         if (addr + old_len > new_addr && new_addr + new_len > addr)
455                 goto out;
456
457         ret = do_munmap(mm, new_addr, new_len, uf_unmap_early);
458         if (ret)
459                 goto out;
460
461         if (old_len >= new_len) {
462                 ret = do_munmap(mm, addr+new_len, old_len - new_len, uf_unmap);
463                 if (ret && old_len != new_len)
464                         goto out;
465                 old_len = new_len;
466         }
467
468         vma = vma_to_resize(addr, old_len, new_len, &charged);
469         if (IS_ERR(vma)) {
470                 ret = PTR_ERR(vma);
471                 goto out;
472         }
473
474         map_flags = MAP_FIXED;
475         if (vma->vm_flags & VM_MAYSHARE)
476                 map_flags |= MAP_SHARED;
477
478         ret = get_unmapped_area(vma->vm_file, new_addr, new_len, vma->vm_pgoff +
479                                 ((addr - vma->vm_start) >> PAGE_SHIFT),
480                                 map_flags);
481         if (offset_in_page(ret))
482                 goto out1;
483
484         ret = move_vma(vma, addr, old_len, new_len, new_addr, locked, uf,
485                        uf_unmap);
486         if (!(offset_in_page(ret)))
487                 goto out;
488 out1:
489         vm_unacct_memory(charged);
490
491 out:
492         return ret;
493 }
494
495 static int vma_expandable(struct vm_area_struct *vma, unsigned long delta)
496 {
497         unsigned long end = vma->vm_end + delta;
498         if (end < vma->vm_end) /* overflow */
499                 return 0;
500         if (vma->vm_next && vma->vm_next->vm_start < end) /* intersection */
501                 return 0;
502         if (get_unmapped_area(NULL, vma->vm_start, end - vma->vm_start,
503                               0, MAP_FIXED) & ~PAGE_MASK)
504                 return 0;
505         return 1;
506 }
507
508 /*
509  * Expand (or shrink) an existing mapping, potentially moving it at the
510  * same time (controlled by the MREMAP_MAYMOVE flag and available VM space)
511  *
512  * MREMAP_FIXED option added 5-Dec-1999 by Benjamin LaHaise
513  * This option implies MREMAP_MAYMOVE.
514  */
515 SYSCALL_DEFINE5(mremap, unsigned long, addr, unsigned long, old_len,
516                 unsigned long, new_len, unsigned long, flags,
517                 unsigned long, new_addr)
518 {
519         struct mm_struct *mm = current->mm;
520         struct vm_area_struct *vma;
521         unsigned long ret = -EINVAL;
522         unsigned long charged = 0;
523         bool locked = false;
524         bool downgraded = false;
525         struct vm_userfaultfd_ctx uf = NULL_VM_UFFD_CTX;
526         LIST_HEAD(uf_unmap_early);
527         LIST_HEAD(uf_unmap);
528
529         if (flags & ~(MREMAP_FIXED | MREMAP_MAYMOVE))
530                 return ret;
531
532         if (flags & MREMAP_FIXED && !(flags & MREMAP_MAYMOVE))
533                 return ret;
534
535         if (offset_in_page(addr))
536                 return ret;
537
538         old_len = PAGE_ALIGN(old_len);
539         new_len = PAGE_ALIGN(new_len);
540
541         /*
542          * We allow a zero old-len as a special case
543          * for DOS-emu "duplicate shm area" thing. But
544          * a zero new-len is nonsensical.
545          */
546         if (!new_len)
547                 return ret;
548
549         if (down_write_killable(&current->mm->mmap_sem))
550                 return -EINTR;
551
552         if (flags & MREMAP_FIXED) {
553                 ret = mremap_to(addr, old_len, new_addr, new_len,
554                                 &locked, &uf, &uf_unmap_early, &uf_unmap);
555                 goto out;
556         }
557
558         /*
559          * Always allow a shrinking remap: that just unmaps
560          * the unnecessary pages..
561          * __do_munmap does all the needed commit accounting, and
562          * downgrades mmap_sem to read if so directed.
563          */
564         if (old_len >= new_len) {
565                 int retval;
566
567                 retval = __do_munmap(mm, addr+new_len, old_len - new_len,
568                                   &uf_unmap, true);
569                 if (retval < 0 && old_len != new_len) {
570                         ret = retval;
571                         goto out;
572                 /* Returning 1 indicates mmap_sem is downgraded to read. */
573                 } else if (retval == 1)
574                         downgraded = true;
575                 ret = addr;
576                 goto out;
577         }
578
579         /*
580          * Ok, we need to grow..
581          */
582         vma = vma_to_resize(addr, old_len, new_len, &charged);
583         if (IS_ERR(vma)) {
584                 ret = PTR_ERR(vma);
585                 goto out;
586         }
587
588         /* old_len exactly to the end of the area..
589          */
590         if (old_len == vma->vm_end - addr) {
591                 /* can we just expand the current mapping? */
592                 if (vma_expandable(vma, new_len - old_len)) {
593                         int pages = (new_len - old_len) >> PAGE_SHIFT;
594
595                         if (vma_adjust(vma, vma->vm_start, addr + new_len,
596                                        vma->vm_pgoff, NULL)) {
597                                 ret = -ENOMEM;
598                                 goto out;
599                         }
600
601                         vm_stat_account(mm, vma->vm_flags, pages);
602                         if (vma->vm_flags & VM_LOCKED) {
603                                 mm->locked_vm += pages;
604                                 locked = true;
605                                 new_addr = addr;
606                         }
607                         ret = addr;
608                         goto out;
609                 }
610         }
611
612         /*
613          * We weren't able to just expand or shrink the area,
614          * we need to create a new one and move it..
615          */
616         ret = -ENOMEM;
617         if (flags & MREMAP_MAYMOVE) {
618                 unsigned long map_flags = 0;
619                 if (vma->vm_flags & VM_MAYSHARE)
620                         map_flags |= MAP_SHARED;
621
622                 new_addr = get_unmapped_area(vma->vm_file, 0, new_len,
623                                         vma->vm_pgoff +
624                                         ((addr - vma->vm_start) >> PAGE_SHIFT),
625                                         map_flags);
626                 if (offset_in_page(new_addr)) {
627                         ret = new_addr;
628                         goto out;
629                 }
630
631                 ret = move_vma(vma, addr, old_len, new_len, new_addr,
632                                &locked, &uf, &uf_unmap);
633         }
634 out:
635         if (offset_in_page(ret)) {
636                 vm_unacct_memory(charged);
637                 locked = 0;
638         }
639         if (downgraded)
640                 up_read(&current->mm->mmap_sem);
641         else
642                 up_write(&current->mm->mmap_sem);
643         if (locked && new_len > old_len)
644                 mm_populate(new_addr + old_len, new_len - old_len);
645         userfaultfd_unmap_complete(mm, &uf_unmap_early);
646         mremap_userfaultfd_complete(&uf, addr, new_addr, old_len);
647         userfaultfd_unmap_complete(mm, &uf_unmap);
648         return ret;
649 }