1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0 21da177e4SLinus Torvalds /* 31da177e4SLinus Torvalds * mm/mremap.c 41da177e4SLinus Torvalds * 51da177e4SLinus Torvalds * (C) Copyright 1996 Linus Torvalds 61da177e4SLinus Torvalds * 7046c6884SAlan Cox * Address space accounting code <alan@lxorguk.ukuu.org.uk> 81da177e4SLinus Torvalds * (C) Copyright 2002 Red Hat Inc, All Rights Reserved 91da177e4SLinus Torvalds */ 101da177e4SLinus Torvalds 111da177e4SLinus Torvalds #include <linux/mm.h> 121da177e4SLinus Torvalds #include <linux/hugetlb.h> 131da177e4SLinus Torvalds #include <linux/shm.h> 141ff82995SHugh Dickins #include <linux/ksm.h> 151da177e4SLinus Torvalds #include <linux/mman.h> 161da177e4SLinus Torvalds #include <linux/swap.h> 17c59ede7bSRandy.Dunlap #include <linux/capability.h> 181da177e4SLinus Torvalds #include <linux/fs.h> 196dec97dcSCyrill Gorcunov #include <linux/swapops.h> 201da177e4SLinus Torvalds #include <linux/highmem.h> 211da177e4SLinus Torvalds #include <linux/security.h> 221da177e4SLinus Torvalds #include <linux/syscalls.h> 23cddb8a5cSAndrea Arcangeli #include <linux/mmu_notifier.h> 242581d202SPaul McQuade #include <linux/uaccess.h> 254abad2caSLaurent Dufour #include <linux/mm-arch-hooks.h> 2672f87654SPavel Emelyanov #include <linux/userfaultfd_k.h> 271da177e4SLinus Torvalds 281da177e4SLinus Torvalds #include <asm/cacheflush.h> 291da177e4SLinus Torvalds #include <asm/tlbflush.h> 301da177e4SLinus Torvalds 31ba470de4SRik van Riel #include "internal.h" 32ba470de4SRik van Riel 337be7a546SHugh Dickins static pmd_t *get_old_pmd(struct mm_struct *mm, unsigned long addr) 341da177e4SLinus Torvalds { 351da177e4SLinus Torvalds pgd_t *pgd; 36c2febafcSKirill A. Shutemov p4d_t *p4d; 371da177e4SLinus Torvalds pud_t *pud; 381da177e4SLinus Torvalds pmd_t *pmd; 391da177e4SLinus Torvalds 401da177e4SLinus Torvalds pgd = pgd_offset(mm, addr); 411da177e4SLinus Torvalds if (pgd_none_or_clear_bad(pgd)) 421da177e4SLinus Torvalds return NULL; 431da177e4SLinus Torvalds 44c2febafcSKirill A. Shutemov p4d = p4d_offset(pgd, addr); 45c2febafcSKirill A. Shutemov if (p4d_none_or_clear_bad(p4d)) 46c2febafcSKirill A. Shutemov return NULL; 47c2febafcSKirill A. Shutemov 48c2febafcSKirill A. Shutemov pud = pud_offset(p4d, addr); 491da177e4SLinus Torvalds if (pud_none_or_clear_bad(pud)) 501da177e4SLinus Torvalds return NULL; 511da177e4SLinus Torvalds 521da177e4SLinus Torvalds pmd = pmd_offset(pud, addr); 5337a1c49aSAndrea Arcangeli if (pmd_none(*pmd)) 541da177e4SLinus Torvalds return NULL; 551da177e4SLinus Torvalds 567be7a546SHugh Dickins return pmd; 571da177e4SLinus Torvalds } 581da177e4SLinus Torvalds 598ac1f832SAndrea Arcangeli static pmd_t *alloc_new_pmd(struct mm_struct *mm, struct vm_area_struct *vma, 608ac1f832SAndrea Arcangeli unsigned long addr) 611da177e4SLinus Torvalds { 621da177e4SLinus Torvalds pgd_t *pgd; 63c2febafcSKirill A. Shutemov p4d_t *p4d; 641da177e4SLinus Torvalds pud_t *pud; 65c74df32cSHugh Dickins pmd_t *pmd; 661da177e4SLinus Torvalds 671da177e4SLinus Torvalds pgd = pgd_offset(mm, addr); 68c2febafcSKirill A. Shutemov p4d = p4d_alloc(mm, pgd, addr); 69c2febafcSKirill A. Shutemov if (!p4d) 70c2febafcSKirill A. Shutemov return NULL; 71c2febafcSKirill A. Shutemov pud = pud_alloc(mm, p4d, addr); 721da177e4SLinus Torvalds if (!pud) 73c74df32cSHugh Dickins return NULL; 747be7a546SHugh Dickins 751da177e4SLinus Torvalds pmd = pmd_alloc(mm, pud, addr); 7657a8f0cdSHugh Dickins if (!pmd) 77c74df32cSHugh Dickins return NULL; 787be7a546SHugh Dickins 798ac1f832SAndrea Arcangeli VM_BUG_ON(pmd_trans_huge(*pmd)); 80c74df32cSHugh Dickins 817be7a546SHugh Dickins return pmd; 821da177e4SLinus Torvalds } 831da177e4SLinus Torvalds 841d069b7dSHugh Dickins static void take_rmap_locks(struct vm_area_struct *vma) 851d069b7dSHugh Dickins { 861d069b7dSHugh Dickins if (vma->vm_file) 871d069b7dSHugh Dickins i_mmap_lock_write(vma->vm_file->f_mapping); 881d069b7dSHugh Dickins if (vma->anon_vma) 891d069b7dSHugh Dickins anon_vma_lock_write(vma->anon_vma); 901d069b7dSHugh Dickins } 911d069b7dSHugh Dickins 921d069b7dSHugh Dickins static void drop_rmap_locks(struct vm_area_struct *vma) 931d069b7dSHugh Dickins { 941d069b7dSHugh Dickins if (vma->anon_vma) 951d069b7dSHugh Dickins anon_vma_unlock_write(vma->anon_vma); 961d069b7dSHugh Dickins if (vma->vm_file) 971d069b7dSHugh Dickins i_mmap_unlock_write(vma->vm_file->f_mapping); 981d069b7dSHugh Dickins } 991d069b7dSHugh Dickins 1006dec97dcSCyrill Gorcunov static pte_t move_soft_dirty_pte(pte_t pte) 1016dec97dcSCyrill Gorcunov { 1026dec97dcSCyrill Gorcunov /* 1036dec97dcSCyrill Gorcunov * Set soft dirty bit so we can notice 1046dec97dcSCyrill Gorcunov * in userspace the ptes were moved. 1056dec97dcSCyrill Gorcunov */ 1066dec97dcSCyrill Gorcunov #ifdef CONFIG_MEM_SOFT_DIRTY 1076dec97dcSCyrill Gorcunov if (pte_present(pte)) 1086dec97dcSCyrill Gorcunov pte = pte_mksoft_dirty(pte); 1096dec97dcSCyrill Gorcunov else if (is_swap_pte(pte)) 1106dec97dcSCyrill Gorcunov pte = pte_swp_mksoft_dirty(pte); 1116dec97dcSCyrill Gorcunov #endif 1126dec97dcSCyrill Gorcunov return pte; 1136dec97dcSCyrill Gorcunov } 1146dec97dcSCyrill Gorcunov 1157be7a546SHugh Dickins static void move_ptes(struct vm_area_struct *vma, pmd_t *old_pmd, 1167be7a546SHugh Dickins unsigned long old_addr, unsigned long old_end, 1177be7a546SHugh Dickins struct vm_area_struct *new_vma, pmd_t *new_pmd, 118eb66ae03SLinus Torvalds unsigned long new_addr, bool need_rmap_locks) 1191da177e4SLinus Torvalds { 1201da177e4SLinus Torvalds struct mm_struct *mm = vma->vm_mm; 1217be7a546SHugh Dickins pte_t *old_pte, *new_pte, pte; 1224c21e2f2SHugh Dickins spinlock_t *old_ptl, *new_ptl; 1235d190420SAaron Lu bool force_flush = false; 1245d190420SAaron Lu unsigned long len = old_end - old_addr; 1251da177e4SLinus Torvalds 1261da177e4SLinus Torvalds /* 127c8c06efaSDavidlohr Bueso * When need_rmap_locks is true, we take the i_mmap_rwsem and anon_vma 12838a76013SMichel Lespinasse * locks to ensure that rmap will always observe either the old or the 12938a76013SMichel Lespinasse * new ptes. This is the easiest way to avoid races with 13038a76013SMichel Lespinasse * truncate_pagecache(), page migration, etc... 13138a76013SMichel Lespinasse * 13238a76013SMichel Lespinasse * When need_rmap_locks is false, we use other ways to avoid 13338a76013SMichel Lespinasse * such races: 13438a76013SMichel Lespinasse * 13538a76013SMichel Lespinasse * - During exec() shift_arg_pages(), we use a specially tagged vma 136222100eeSAnshuman Khandual * which rmap call sites look for using vma_is_temporary_stack(). 13738a76013SMichel Lespinasse * 13838a76013SMichel Lespinasse * - During mremap(), new_vma is often known to be placed after vma 13938a76013SMichel Lespinasse * in rmap traversal order. This ensures rmap will always observe 14038a76013SMichel Lespinasse * either the old pte, or the new pte, or both (the page table locks 14138a76013SMichel Lespinasse * serialize access to individual ptes, but only rmap traversal 14238a76013SMichel Lespinasse * order guarantees that we won't miss both the old and new ptes). 1431da177e4SLinus Torvalds */ 1441d069b7dSHugh Dickins if (need_rmap_locks) 1451d069b7dSHugh Dickins take_rmap_locks(vma); 1461da177e4SLinus Torvalds 1474c21e2f2SHugh Dickins /* 1484c21e2f2SHugh Dickins * We don't have to worry about the ordering of src and dst 149*c1e8d7c6SMichel Lespinasse * pte locks because exclusive mmap_lock prevents deadlock. 1504c21e2f2SHugh Dickins */ 151c74df32cSHugh Dickins old_pte = pte_offset_map_lock(mm, old_pmd, old_addr, &old_ptl); 152ece0e2b6SPeter Zijlstra new_pte = pte_offset_map(new_pmd, new_addr); 1534c21e2f2SHugh Dickins new_ptl = pte_lockptr(mm, new_pmd); 1544c21e2f2SHugh Dickins if (new_ptl != old_ptl) 155f20dc5f7SIngo Molnar spin_lock_nested(new_ptl, SINGLE_DEPTH_NESTING); 1563ea27719SMel Gorman flush_tlb_batched_pending(vma->vm_mm); 1576606c3e0SZachary Amsden arch_enter_lazy_mmu_mode(); 1588b1f3124SNick Piggin 1597be7a546SHugh Dickins for (; old_addr < old_end; old_pte++, old_addr += PAGE_SIZE, 1607be7a546SHugh Dickins new_pte++, new_addr += PAGE_SIZE) { 1617be7a546SHugh Dickins if (pte_none(*old_pte)) 1627be7a546SHugh Dickins continue; 1635d190420SAaron Lu 1647b6efc2bSAndrea Arcangeli pte = ptep_get_and_clear(mm, old_addr, old_pte); 165a2ce2666SAaron Lu /* 166eb66ae03SLinus Torvalds * If we are remapping a valid PTE, make sure 167a2ce2666SAaron Lu * to flush TLB before we drop the PTL for the 168eb66ae03SLinus Torvalds * PTE. 169a2ce2666SAaron Lu * 170eb66ae03SLinus Torvalds * NOTE! Both old and new PTL matter: the old one 171eb66ae03SLinus Torvalds * for racing with page_mkclean(), the new one to 172eb66ae03SLinus Torvalds * make sure the physical page stays valid until 173eb66ae03SLinus Torvalds * the TLB entry for the old mapping has been 174eb66ae03SLinus Torvalds * flushed. 175a2ce2666SAaron Lu */ 176eb66ae03SLinus Torvalds if (pte_present(pte)) 177a2ce2666SAaron Lu force_flush = true; 1787be7a546SHugh Dickins pte = move_pte(pte, new_vma->vm_page_prot, old_addr, new_addr); 1796dec97dcSCyrill Gorcunov pte = move_soft_dirty_pte(pte); 1806dec97dcSCyrill Gorcunov set_pte_at(mm, new_addr, new_pte, pte); 1811da177e4SLinus Torvalds } 1827be7a546SHugh Dickins 1836606c3e0SZachary Amsden arch_leave_lazy_mmu_mode(); 184eb66ae03SLinus Torvalds if (force_flush) 185eb66ae03SLinus Torvalds flush_tlb_range(vma, old_end - len, old_end); 1864c21e2f2SHugh Dickins if (new_ptl != old_ptl) 1874c21e2f2SHugh Dickins spin_unlock(new_ptl); 188ece0e2b6SPeter Zijlstra pte_unmap(new_pte - 1); 189c74df32cSHugh Dickins pte_unmap_unlock(old_pte - 1, old_ptl); 1901d069b7dSHugh Dickins if (need_rmap_locks) 1911d069b7dSHugh Dickins drop_rmap_locks(vma); 1921da177e4SLinus Torvalds } 1931da177e4SLinus Torvalds 1942c91bd4aSJoel Fernandes (Google) #ifdef CONFIG_HAVE_MOVE_PMD 1952c91bd4aSJoel Fernandes (Google) static bool move_normal_pmd(struct vm_area_struct *vma, unsigned long old_addr, 1962c91bd4aSJoel Fernandes (Google) unsigned long new_addr, unsigned long old_end, 1972c91bd4aSJoel Fernandes (Google) pmd_t *old_pmd, pmd_t *new_pmd) 1982c91bd4aSJoel Fernandes (Google) { 1992c91bd4aSJoel Fernandes (Google) spinlock_t *old_ptl, *new_ptl; 2002c91bd4aSJoel Fernandes (Google) struct mm_struct *mm = vma->vm_mm; 2012c91bd4aSJoel Fernandes (Google) pmd_t pmd; 2022c91bd4aSJoel Fernandes (Google) 2032c91bd4aSJoel Fernandes (Google) if ((old_addr & ~PMD_MASK) || (new_addr & ~PMD_MASK) 2042c91bd4aSJoel Fernandes (Google) || old_end - old_addr < PMD_SIZE) 2052c91bd4aSJoel Fernandes (Google) return false; 2062c91bd4aSJoel Fernandes (Google) 2072c91bd4aSJoel Fernandes (Google) /* 2082c91bd4aSJoel Fernandes (Google) * The destination pmd shouldn't be established, free_pgtables() 2092c91bd4aSJoel Fernandes (Google) * should have release it. 2102c91bd4aSJoel Fernandes (Google) */ 2112c91bd4aSJoel Fernandes (Google) if (WARN_ON(!pmd_none(*new_pmd))) 2122c91bd4aSJoel Fernandes (Google) return false; 2132c91bd4aSJoel Fernandes (Google) 2142c91bd4aSJoel Fernandes (Google) /* 2152c91bd4aSJoel Fernandes (Google) * We don't have to worry about the ordering of src and dst 216*c1e8d7c6SMichel Lespinasse * ptlocks because exclusive mmap_lock prevents deadlock. 2172c91bd4aSJoel Fernandes (Google) */ 2182c91bd4aSJoel Fernandes (Google) old_ptl = pmd_lock(vma->vm_mm, old_pmd); 2192c91bd4aSJoel Fernandes (Google) new_ptl = pmd_lockptr(mm, new_pmd); 2202c91bd4aSJoel Fernandes (Google) if (new_ptl != old_ptl) 2212c91bd4aSJoel Fernandes (Google) spin_lock_nested(new_ptl, SINGLE_DEPTH_NESTING); 2222c91bd4aSJoel Fernandes (Google) 2232c91bd4aSJoel Fernandes (Google) /* Clear the pmd */ 2242c91bd4aSJoel Fernandes (Google) pmd = *old_pmd; 2252c91bd4aSJoel Fernandes (Google) pmd_clear(old_pmd); 2262c91bd4aSJoel Fernandes (Google) 2272c91bd4aSJoel Fernandes (Google) VM_BUG_ON(!pmd_none(*new_pmd)); 2282c91bd4aSJoel Fernandes (Google) 2292c91bd4aSJoel Fernandes (Google) /* Set the new pmd */ 2302c91bd4aSJoel Fernandes (Google) set_pmd_at(mm, new_addr, new_pmd, pmd); 2312c91bd4aSJoel Fernandes (Google) flush_tlb_range(vma, old_addr, old_addr + PMD_SIZE); 2322c91bd4aSJoel Fernandes (Google) if (new_ptl != old_ptl) 2332c91bd4aSJoel Fernandes (Google) spin_unlock(new_ptl); 2342c91bd4aSJoel Fernandes (Google) spin_unlock(old_ptl); 2352c91bd4aSJoel Fernandes (Google) 2362c91bd4aSJoel Fernandes (Google) return true; 2372c91bd4aSJoel Fernandes (Google) } 2382c91bd4aSJoel Fernandes (Google) #endif 2392c91bd4aSJoel Fernandes (Google) 240b6a2fea3SOllie Wild unsigned long move_page_tables(struct vm_area_struct *vma, 2411da177e4SLinus Torvalds unsigned long old_addr, struct vm_area_struct *new_vma, 24238a76013SMichel Lespinasse unsigned long new_addr, unsigned long len, 24338a76013SMichel Lespinasse bool need_rmap_locks) 2441da177e4SLinus Torvalds { 2457be7a546SHugh Dickins unsigned long extent, next, old_end; 246ac46d4f3SJérôme Glisse struct mmu_notifier_range range; 2477be7a546SHugh Dickins pmd_t *old_pmd, *new_pmd; 2481da177e4SLinus Torvalds 2497be7a546SHugh Dickins old_end = old_addr + len; 2507be7a546SHugh Dickins flush_cache_range(vma, old_addr, old_end); 2511da177e4SLinus Torvalds 2526f4f13e8SJérôme Glisse mmu_notifier_range_init(&range, MMU_NOTIFY_UNMAP, 0, vma, vma->vm_mm, 2536f4f13e8SJérôme Glisse old_addr, old_end); 254ac46d4f3SJérôme Glisse mmu_notifier_invalidate_range_start(&range); 2557b6efc2bSAndrea Arcangeli 2567be7a546SHugh Dickins for (; old_addr < old_end; old_addr += extent, new_addr += extent) { 2571da177e4SLinus Torvalds cond_resched(); 2587be7a546SHugh Dickins next = (old_addr + PMD_SIZE) & PMD_MASK; 259ebed4846SAndrea Arcangeli /* even if next overflowed, extent below will be ok */ 2607be7a546SHugh Dickins extent = next - old_addr; 261ebed4846SAndrea Arcangeli if (extent > old_end - old_addr) 262ebed4846SAndrea Arcangeli extent = old_end - old_addr; 2637be7a546SHugh Dickins old_pmd = get_old_pmd(vma->vm_mm, old_addr); 2647be7a546SHugh Dickins if (!old_pmd) 2657be7a546SHugh Dickins continue; 2668ac1f832SAndrea Arcangeli new_pmd = alloc_new_pmd(vma->vm_mm, vma, new_addr); 2677be7a546SHugh Dickins if (!new_pmd) 2687be7a546SHugh Dickins break; 2695bfea2d9SFan Yang if (is_swap_pmd(*old_pmd) || pmd_trans_huge(*old_pmd) || pmd_devmap(*old_pmd)) { 270dd18dbc2SKirill A. Shutemov if (extent == HPAGE_PMD_SIZE) { 2714b471e88SKirill A. Shutemov bool moved; 272dd18dbc2SKirill A. Shutemov /* See comment in move_ptes() */ 273dd18dbc2SKirill A. Shutemov if (need_rmap_locks) 2741d069b7dSHugh Dickins take_rmap_locks(vma); 275bf8616d5SHugh Dickins moved = move_huge_pmd(vma, old_addr, new_addr, 276eb66ae03SLinus Torvalds old_end, old_pmd, new_pmd); 277dd18dbc2SKirill A. Shutemov if (need_rmap_locks) 2781d069b7dSHugh Dickins drop_rmap_locks(vma); 2795d190420SAaron Lu if (moved) 28037a1c49aSAndrea Arcangeli continue; 28137a1c49aSAndrea Arcangeli } 2824b471e88SKirill A. Shutemov split_huge_pmd(vma, old_pmd, old_addr); 283337d9abfSNaoya Horiguchi if (pmd_trans_unstable(old_pmd)) 2846b9116a6SKirill A. Shutemov continue; 2852c91bd4aSJoel Fernandes (Google) } else if (extent == PMD_SIZE) { 2862c91bd4aSJoel Fernandes (Google) #ifdef CONFIG_HAVE_MOVE_PMD 2872c91bd4aSJoel Fernandes (Google) /* 2882c91bd4aSJoel Fernandes (Google) * If the extent is PMD-sized, try to speed the move by 2892c91bd4aSJoel Fernandes (Google) * moving at the PMD level if possible. 2902c91bd4aSJoel Fernandes (Google) */ 2912c91bd4aSJoel Fernandes (Google) bool moved; 2922c91bd4aSJoel Fernandes (Google) 2932c91bd4aSJoel Fernandes (Google) if (need_rmap_locks) 2942c91bd4aSJoel Fernandes (Google) take_rmap_locks(vma); 2952c91bd4aSJoel Fernandes (Google) moved = move_normal_pmd(vma, old_addr, new_addr, 2962c91bd4aSJoel Fernandes (Google) old_end, old_pmd, new_pmd); 2972c91bd4aSJoel Fernandes (Google) if (need_rmap_locks) 2982c91bd4aSJoel Fernandes (Google) drop_rmap_locks(vma); 2992c91bd4aSJoel Fernandes (Google) if (moved) 3002c91bd4aSJoel Fernandes (Google) continue; 3012c91bd4aSJoel Fernandes (Google) #endif 30237a1c49aSAndrea Arcangeli } 3032c91bd4aSJoel Fernandes (Google) 3044cf58924SJoel Fernandes (Google) if (pte_alloc(new_vma->vm_mm, new_pmd)) 30537a1c49aSAndrea Arcangeli break; 3067be7a546SHugh Dickins next = (new_addr + PMD_SIZE) & PMD_MASK; 3077be7a546SHugh Dickins if (extent > next - new_addr) 3087be7a546SHugh Dickins extent = next - new_addr; 3095d190420SAaron Lu move_ptes(vma, old_pmd, old_addr, old_addr + extent, new_vma, 310eb66ae03SLinus Torvalds new_pmd, new_addr, need_rmap_locks); 3111da177e4SLinus Torvalds } 3127b6efc2bSAndrea Arcangeli 313ac46d4f3SJérôme Glisse mmu_notifier_invalidate_range_end(&range); 3147be7a546SHugh Dickins 3157be7a546SHugh Dickins return len + old_addr - old_end; /* how much done */ 3161da177e4SLinus Torvalds } 3171da177e4SLinus Torvalds 3181da177e4SLinus Torvalds static unsigned long move_vma(struct vm_area_struct *vma, 3191da177e4SLinus Torvalds unsigned long old_addr, unsigned long old_len, 32072f87654SPavel Emelyanov unsigned long new_len, unsigned long new_addr, 321e346b381SBrian Geffon bool *locked, unsigned long flags, 322e346b381SBrian Geffon struct vm_userfaultfd_ctx *uf, struct list_head *uf_unmap) 3231da177e4SLinus Torvalds { 3241da177e4SLinus Torvalds struct mm_struct *mm = vma->vm_mm; 3251da177e4SLinus Torvalds struct vm_area_struct *new_vma; 3261da177e4SLinus Torvalds unsigned long vm_flags = vma->vm_flags; 3271da177e4SLinus Torvalds unsigned long new_pgoff; 3281da177e4SLinus Torvalds unsigned long moved_len; 3291da177e4SLinus Torvalds unsigned long excess = 0; 330365e9c87SHugh Dickins unsigned long hiwater_vm; 3311da177e4SLinus Torvalds int split = 0; 3327103ad32SHugh Dickins int err; 33338a76013SMichel Lespinasse bool need_rmap_locks; 3341da177e4SLinus Torvalds 3351da177e4SLinus Torvalds /* 3361da177e4SLinus Torvalds * We'd prefer to avoid failure later on in do_munmap: 3371da177e4SLinus Torvalds * which may split one vma into three before unmapping. 3381da177e4SLinus Torvalds */ 3391da177e4SLinus Torvalds if (mm->map_count >= sysctl_max_map_count - 3) 3401da177e4SLinus Torvalds return -ENOMEM; 3411da177e4SLinus Torvalds 3421ff82995SHugh Dickins /* 3431ff82995SHugh Dickins * Advise KSM to break any KSM pages in the area to be moved: 3441ff82995SHugh Dickins * it would be confusing if they were to turn up at the new 3451ff82995SHugh Dickins * location, where they happen to coincide with different KSM 3461ff82995SHugh Dickins * pages recently unmapped. But leave vma->vm_flags as it was, 3471ff82995SHugh Dickins * so KSM can come around to merge on vma and new_vma afterwards. 3481ff82995SHugh Dickins */ 3497103ad32SHugh Dickins err = ksm_madvise(vma, old_addr, old_addr + old_len, 3507103ad32SHugh Dickins MADV_UNMERGEABLE, &vm_flags); 3517103ad32SHugh Dickins if (err) 3527103ad32SHugh Dickins return err; 3531ff82995SHugh Dickins 3541da177e4SLinus Torvalds new_pgoff = vma->vm_pgoff + ((old_addr - vma->vm_start) >> PAGE_SHIFT); 35538a76013SMichel Lespinasse new_vma = copy_vma(&vma, new_addr, new_len, new_pgoff, 35638a76013SMichel Lespinasse &need_rmap_locks); 3571da177e4SLinus Torvalds if (!new_vma) 3581da177e4SLinus Torvalds return -ENOMEM; 3591da177e4SLinus Torvalds 36038a76013SMichel Lespinasse moved_len = move_page_tables(vma, old_addr, new_vma, new_addr, old_len, 36138a76013SMichel Lespinasse need_rmap_locks); 3621da177e4SLinus Torvalds if (moved_len < old_len) { 363df1eab30SOleg Nesterov err = -ENOMEM; 3645477e70aSOleg Nesterov } else if (vma->vm_ops && vma->vm_ops->mremap) { 3655477e70aSOleg Nesterov err = vma->vm_ops->mremap(new_vma); 366df1eab30SOleg Nesterov } 367df1eab30SOleg Nesterov 368df1eab30SOleg Nesterov if (unlikely(err)) { 3691da177e4SLinus Torvalds /* 3701da177e4SLinus Torvalds * On error, move entries back from new area to old, 3711da177e4SLinus Torvalds * which will succeed since page tables still there, 3721da177e4SLinus Torvalds * and then proceed to unmap new area instead of old. 3731da177e4SLinus Torvalds */ 37438a76013SMichel Lespinasse move_page_tables(new_vma, new_addr, vma, old_addr, moved_len, 37538a76013SMichel Lespinasse true); 3761da177e4SLinus Torvalds vma = new_vma; 3771da177e4SLinus Torvalds old_len = new_len; 3781da177e4SLinus Torvalds old_addr = new_addr; 379df1eab30SOleg Nesterov new_addr = err; 3804abad2caSLaurent Dufour } else { 38172f87654SPavel Emelyanov mremap_userfaultfd_prep(new_vma, uf); 3824abad2caSLaurent Dufour arch_remap(mm, old_addr, old_addr + old_len, 3834abad2caSLaurent Dufour new_addr, new_addr + new_len); 3844abad2caSLaurent Dufour } 3851da177e4SLinus Torvalds 3861da177e4SLinus Torvalds /* Conceal VM_ACCOUNT so old reservation is not undone */ 3871da177e4SLinus Torvalds if (vm_flags & VM_ACCOUNT) { 3881da177e4SLinus Torvalds vma->vm_flags &= ~VM_ACCOUNT; 3891da177e4SLinus Torvalds excess = vma->vm_end - vma->vm_start - old_len; 3901da177e4SLinus Torvalds if (old_addr > vma->vm_start && 3911da177e4SLinus Torvalds old_addr + old_len < vma->vm_end) 3921da177e4SLinus Torvalds split = 1; 3931da177e4SLinus Torvalds } 3941da177e4SLinus Torvalds 39571799062SKirill Korotaev /* 396365e9c87SHugh Dickins * If we failed to move page tables we still do total_vm increment 397365e9c87SHugh Dickins * since do_munmap() will decrement it by old_len == new_len. 398365e9c87SHugh Dickins * 399365e9c87SHugh Dickins * Since total_vm is about to be raised artificially high for a 400365e9c87SHugh Dickins * moment, we need to restore high watermark afterwards: if stats 401365e9c87SHugh Dickins * are taken meanwhile, total_vm and hiwater_vm appear too high. 402365e9c87SHugh Dickins * If this were a serious issue, we'd add a flag to do_munmap(). 40371799062SKirill Korotaev */ 404365e9c87SHugh Dickins hiwater_vm = mm->hiwater_vm; 40584638335SKonstantin Khlebnikov vm_stat_account(mm, vma->vm_flags, new_len >> PAGE_SHIFT); 40671799062SKirill Korotaev 407d9fe4fabSToshi Kani /* Tell pfnmap has moved from this vma */ 408d9fe4fabSToshi Kani if (unlikely(vma->vm_flags & VM_PFNMAP)) 409d9fe4fabSToshi Kani untrack_pfn_moved(vma); 410d9fe4fabSToshi Kani 411e346b381SBrian Geffon if (unlikely(!err && (flags & MREMAP_DONTUNMAP))) { 412e346b381SBrian Geffon if (vm_flags & VM_ACCOUNT) { 413e346b381SBrian Geffon /* Always put back VM_ACCOUNT since we won't unmap */ 414e346b381SBrian Geffon vma->vm_flags |= VM_ACCOUNT; 415e346b381SBrian Geffon 416dadbd85fSBrian Geffon vm_acct_memory(new_len >> PAGE_SHIFT); 417e346b381SBrian Geffon } 418e346b381SBrian Geffon 419dadbd85fSBrian Geffon /* 420dadbd85fSBrian Geffon * VMAs can actually be merged back together in copy_vma 421dadbd85fSBrian Geffon * calling merge_vma. This can happen with anonymous vmas 422dadbd85fSBrian Geffon * which have not yet been faulted, so if we were to consider 423dadbd85fSBrian Geffon * this VMA split we'll end up adding VM_ACCOUNT on the 424dadbd85fSBrian Geffon * next VMA, which is completely unrelated if this VMA 425dadbd85fSBrian Geffon * was re-merged. 426dadbd85fSBrian Geffon */ 427dadbd85fSBrian Geffon if (split && new_vma == vma) 428dadbd85fSBrian Geffon split = 0; 429dadbd85fSBrian Geffon 430e346b381SBrian Geffon /* We always clear VM_LOCKED[ONFAULT] on the old vma */ 431e346b381SBrian Geffon vma->vm_flags &= VM_LOCKED_CLEAR_MASK; 432e346b381SBrian Geffon 433e346b381SBrian Geffon /* Because we won't unmap we don't need to touch locked_vm */ 434e346b381SBrian Geffon goto out; 435e346b381SBrian Geffon } 436e346b381SBrian Geffon 437897ab3e0SMike Rapoport if (do_munmap(mm, old_addr, old_len, uf_unmap) < 0) { 4381da177e4SLinus Torvalds /* OOM: unable to split vma, just get accounts right */ 4391da177e4SLinus Torvalds vm_unacct_memory(excess >> PAGE_SHIFT); 4401da177e4SLinus Torvalds excess = 0; 4411da177e4SLinus Torvalds } 442e346b381SBrian Geffon 443e346b381SBrian Geffon if (vm_flags & VM_LOCKED) { 444e346b381SBrian Geffon mm->locked_vm += new_len >> PAGE_SHIFT; 445e346b381SBrian Geffon *locked = true; 446e346b381SBrian Geffon } 447e346b381SBrian Geffon out: 448365e9c87SHugh Dickins mm->hiwater_vm = hiwater_vm; 4491da177e4SLinus Torvalds 4501da177e4SLinus Torvalds /* Restore VM_ACCOUNT if one or two pieces of vma left */ 4511da177e4SLinus Torvalds if (excess) { 4521da177e4SLinus Torvalds vma->vm_flags |= VM_ACCOUNT; 4531da177e4SLinus Torvalds if (split) 4541da177e4SLinus Torvalds vma->vm_next->vm_flags |= VM_ACCOUNT; 4551da177e4SLinus Torvalds } 4561da177e4SLinus Torvalds 4571da177e4SLinus Torvalds return new_addr; 4581da177e4SLinus Torvalds } 4591da177e4SLinus Torvalds 46054f5de70SAl Viro static struct vm_area_struct *vma_to_resize(unsigned long addr, 461e346b381SBrian Geffon unsigned long old_len, unsigned long new_len, unsigned long flags, 462e346b381SBrian Geffon unsigned long *p) 46354f5de70SAl Viro { 46454f5de70SAl Viro struct mm_struct *mm = current->mm; 46554f5de70SAl Viro struct vm_area_struct *vma = find_vma(mm, addr); 4661d391686SOleg Nesterov unsigned long pgoff; 46754f5de70SAl Viro 46854f5de70SAl Viro if (!vma || vma->vm_start > addr) 4696cd57613SDerek return ERR_PTR(-EFAULT); 47054f5de70SAl Viro 471dba58d3bSMike Kravetz /* 472dba58d3bSMike Kravetz * !old_len is a special case where an attempt is made to 'duplicate' 473dba58d3bSMike Kravetz * a mapping. This makes no sense for private mappings as it will 474dba58d3bSMike Kravetz * instead create a fresh/new mapping unrelated to the original. This 475dba58d3bSMike Kravetz * is contrary to the basic idea of mremap which creates new mappings 476dba58d3bSMike Kravetz * based on the original. There are no known use cases for this 477dba58d3bSMike Kravetz * behavior. As a result, fail such attempts. 478dba58d3bSMike Kravetz */ 479dba58d3bSMike Kravetz if (!old_len && !(vma->vm_flags & (VM_SHARED | VM_MAYSHARE))) { 480dba58d3bSMike Kravetz pr_warn_once("%s (%d): attempted to duplicate a private mapping with mremap. This is not supported.\n", current->comm, current->pid); 481dba58d3bSMike Kravetz return ERR_PTR(-EINVAL); 482dba58d3bSMike Kravetz } 483dba58d3bSMike Kravetz 484e346b381SBrian Geffon if (flags & MREMAP_DONTUNMAP && (!vma_is_anonymous(vma) || 485e346b381SBrian Geffon vma->vm_flags & VM_SHARED)) 486e346b381SBrian Geffon return ERR_PTR(-EINVAL); 487e346b381SBrian Geffon 48854f5de70SAl Viro if (is_vm_hugetlb_page(vma)) 4896cd57613SDerek return ERR_PTR(-EINVAL); 49054f5de70SAl Viro 49154f5de70SAl Viro /* We can't remap across vm area boundaries */ 49254f5de70SAl Viro if (old_len > vma->vm_end - addr) 4936cd57613SDerek return ERR_PTR(-EFAULT); 49454f5de70SAl Viro 4951d391686SOleg Nesterov if (new_len == old_len) 4961d391686SOleg Nesterov return vma; 497982134baSLinus Torvalds 4981d391686SOleg Nesterov /* Need to be careful about a growing mapping */ 499982134baSLinus Torvalds pgoff = (addr - vma->vm_start) >> PAGE_SHIFT; 500982134baSLinus Torvalds pgoff += vma->vm_pgoff; 501982134baSLinus Torvalds if (pgoff + (new_len >> PAGE_SHIFT) < pgoff) 5026cd57613SDerek return ERR_PTR(-EINVAL); 5031d391686SOleg Nesterov 5041d391686SOleg Nesterov if (vma->vm_flags & (VM_DONTEXPAND | VM_PFNMAP)) 5051d391686SOleg Nesterov return ERR_PTR(-EFAULT); 50654f5de70SAl Viro 50754f5de70SAl Viro if (vma->vm_flags & VM_LOCKED) { 50854f5de70SAl Viro unsigned long locked, lock_limit; 50954f5de70SAl Viro locked = mm->locked_vm << PAGE_SHIFT; 51059e99e5bSJiri Slaby lock_limit = rlimit(RLIMIT_MEMLOCK); 51154f5de70SAl Viro locked += new_len - old_len; 51254f5de70SAl Viro if (locked > lock_limit && !capable(CAP_IPC_LOCK)) 5136cd57613SDerek return ERR_PTR(-EAGAIN); 51454f5de70SAl Viro } 51554f5de70SAl Viro 51684638335SKonstantin Khlebnikov if (!may_expand_vm(mm, vma->vm_flags, 51784638335SKonstantin Khlebnikov (new_len - old_len) >> PAGE_SHIFT)) 5186cd57613SDerek return ERR_PTR(-ENOMEM); 51954f5de70SAl Viro 52054f5de70SAl Viro if (vma->vm_flags & VM_ACCOUNT) { 52154f5de70SAl Viro unsigned long charged = (new_len - old_len) >> PAGE_SHIFT; 522191c5424SAl Viro if (security_vm_enough_memory_mm(mm, charged)) 5236cd57613SDerek return ERR_PTR(-ENOMEM); 52454f5de70SAl Viro *p = charged; 52554f5de70SAl Viro } 52654f5de70SAl Viro 52754f5de70SAl Viro return vma; 52854f5de70SAl Viro } 52954f5de70SAl Viro 53081909b84SMichel Lespinasse static unsigned long mremap_to(unsigned long addr, unsigned long old_len, 53172f87654SPavel Emelyanov unsigned long new_addr, unsigned long new_len, bool *locked, 532e346b381SBrian Geffon unsigned long flags, struct vm_userfaultfd_ctx *uf, 533b2282371SMike Rapoport struct list_head *uf_unmap_early, 534897ab3e0SMike Rapoport struct list_head *uf_unmap) 535ecc1a899SAl Viro { 536ecc1a899SAl Viro struct mm_struct *mm = current->mm; 537ecc1a899SAl Viro struct vm_area_struct *vma; 538ecc1a899SAl Viro unsigned long ret = -EINVAL; 539ecc1a899SAl Viro unsigned long charged = 0; 540e346b381SBrian Geffon unsigned long map_flags = 0; 541ecc1a899SAl Viro 542f19cb115SAlexander Kuleshov if (offset_in_page(new_addr)) 543ecc1a899SAl Viro goto out; 544ecc1a899SAl Viro 545ecc1a899SAl Viro if (new_len > TASK_SIZE || new_addr > TASK_SIZE - new_len) 546ecc1a899SAl Viro goto out; 547ecc1a899SAl Viro 5489943242cSOleg Nesterov /* Ensure the old/new locations do not overlap */ 5499943242cSOleg Nesterov if (addr + old_len > new_addr && new_addr + new_len > addr) 550ecc1a899SAl Viro goto out; 551ecc1a899SAl Viro 552ea2c3f6fSOscar Salvador /* 553ea2c3f6fSOscar Salvador * move_vma() need us to stay 4 maps below the threshold, otherwise 554ea2c3f6fSOscar Salvador * it will bail out at the very beginning. 555ea2c3f6fSOscar Salvador * That is a problem if we have already unmaped the regions here 556ea2c3f6fSOscar Salvador * (new_addr, and old_addr), because userspace will not know the 557ea2c3f6fSOscar Salvador * state of the vma's after it gets -ENOMEM. 558ea2c3f6fSOscar Salvador * So, to avoid such scenario we can pre-compute if the whole 559ea2c3f6fSOscar Salvador * operation has high chances to success map-wise. 560ea2c3f6fSOscar Salvador * Worst-scenario case is when both vma's (new_addr and old_addr) get 561ea2c3f6fSOscar Salvador * split in 3 before unmaping it. 562ea2c3f6fSOscar Salvador * That means 2 more maps (1 for each) to the ones we already hold. 563ea2c3f6fSOscar Salvador * Check whether current map count plus 2 still leads us to 4 maps below 564ea2c3f6fSOscar Salvador * the threshold, otherwise return -ENOMEM here to be more safe. 565ea2c3f6fSOscar Salvador */ 566ea2c3f6fSOscar Salvador if ((mm->map_count + 2) >= sysctl_max_map_count - 3) 567ea2c3f6fSOscar Salvador return -ENOMEM; 568ea2c3f6fSOscar Salvador 569e346b381SBrian Geffon if (flags & MREMAP_FIXED) { 570b2282371SMike Rapoport ret = do_munmap(mm, new_addr, new_len, uf_unmap_early); 571ecc1a899SAl Viro if (ret) 572ecc1a899SAl Viro goto out; 573e346b381SBrian Geffon } 574ecc1a899SAl Viro 575ecc1a899SAl Viro if (old_len >= new_len) { 576897ab3e0SMike Rapoport ret = do_munmap(mm, addr+new_len, old_len - new_len, uf_unmap); 577ecc1a899SAl Viro if (ret && old_len != new_len) 578ecc1a899SAl Viro goto out; 579ecc1a899SAl Viro old_len = new_len; 580ecc1a899SAl Viro } 581ecc1a899SAl Viro 582e346b381SBrian Geffon vma = vma_to_resize(addr, old_len, new_len, flags, &charged); 583ecc1a899SAl Viro if (IS_ERR(vma)) { 584ecc1a899SAl Viro ret = PTR_ERR(vma); 585ecc1a899SAl Viro goto out; 586ecc1a899SAl Viro } 587ecc1a899SAl Viro 588e346b381SBrian Geffon /* MREMAP_DONTUNMAP expands by old_len since old_len == new_len */ 589e346b381SBrian Geffon if (flags & MREMAP_DONTUNMAP && 590e346b381SBrian Geffon !may_expand_vm(mm, vma->vm_flags, old_len >> PAGE_SHIFT)) { 591e346b381SBrian Geffon ret = -ENOMEM; 592e346b381SBrian Geffon goto out; 593e346b381SBrian Geffon } 594e346b381SBrian Geffon 595e346b381SBrian Geffon if (flags & MREMAP_FIXED) 596e346b381SBrian Geffon map_flags |= MAP_FIXED; 597e346b381SBrian Geffon 598097eed10SAl Viro if (vma->vm_flags & VM_MAYSHARE) 599097eed10SAl Viro map_flags |= MAP_SHARED; 6009206de95SAl Viro 601097eed10SAl Viro ret = get_unmapped_area(vma->vm_file, new_addr, new_len, vma->vm_pgoff + 602097eed10SAl Viro ((addr - vma->vm_start) >> PAGE_SHIFT), 603097eed10SAl Viro map_flags); 604ff68dac6SGaowei Pu if (IS_ERR_VALUE(ret)) 605097eed10SAl Viro goto out1; 606097eed10SAl Viro 607e346b381SBrian Geffon /* We got a new mapping */ 608e346b381SBrian Geffon if (!(flags & MREMAP_FIXED)) 609e346b381SBrian Geffon new_addr = ret; 610e346b381SBrian Geffon 611e346b381SBrian Geffon ret = move_vma(vma, addr, old_len, new_len, new_addr, locked, flags, uf, 612897ab3e0SMike Rapoport uf_unmap); 613e346b381SBrian Geffon 614f19cb115SAlexander Kuleshov if (!(offset_in_page(ret))) 615097eed10SAl Viro goto out; 616e346b381SBrian Geffon 617097eed10SAl Viro out1: 618ecc1a899SAl Viro vm_unacct_memory(charged); 619ecc1a899SAl Viro 620ecc1a899SAl Viro out: 621ecc1a899SAl Viro return ret; 622ecc1a899SAl Viro } 623ecc1a899SAl Viro 6241a0ef85fSAl Viro static int vma_expandable(struct vm_area_struct *vma, unsigned long delta) 6251a0ef85fSAl Viro { 626f106af4eSAl Viro unsigned long end = vma->vm_end + delta; 6279206de95SAl Viro if (end < vma->vm_end) /* overflow */ 6281a0ef85fSAl Viro return 0; 6299206de95SAl Viro if (vma->vm_next && vma->vm_next->vm_start < end) /* intersection */ 630f106af4eSAl Viro return 0; 631f106af4eSAl Viro if (get_unmapped_area(NULL, vma->vm_start, end - vma->vm_start, 632f106af4eSAl Viro 0, MAP_FIXED) & ~PAGE_MASK) 633f106af4eSAl Viro return 0; 6341a0ef85fSAl Viro return 1; 6351a0ef85fSAl Viro } 6361a0ef85fSAl Viro 6371da177e4SLinus Torvalds /* 6381da177e4SLinus Torvalds * Expand (or shrink) an existing mapping, potentially moving it at the 6391da177e4SLinus Torvalds * same time (controlled by the MREMAP_MAYMOVE flag and available VM space) 6401da177e4SLinus Torvalds * 6411da177e4SLinus Torvalds * MREMAP_FIXED option added 5-Dec-1999 by Benjamin LaHaise 6421da177e4SLinus Torvalds * This option implies MREMAP_MAYMOVE. 6431da177e4SLinus Torvalds */ 64463a81db1SAl Viro SYSCALL_DEFINE5(mremap, unsigned long, addr, unsigned long, old_len, 64563a81db1SAl Viro unsigned long, new_len, unsigned long, flags, 64663a81db1SAl Viro unsigned long, new_addr) 6471da177e4SLinus Torvalds { 648d0de32d9SHugh Dickins struct mm_struct *mm = current->mm; 6491da177e4SLinus Torvalds struct vm_area_struct *vma; 6501da177e4SLinus Torvalds unsigned long ret = -EINVAL; 6511da177e4SLinus Torvalds unsigned long charged = 0; 65281909b84SMichel Lespinasse bool locked = false; 65385a06835SYang Shi bool downgraded = false; 65472f87654SPavel Emelyanov struct vm_userfaultfd_ctx uf = NULL_VM_UFFD_CTX; 655b2282371SMike Rapoport LIST_HEAD(uf_unmap_early); 656897ab3e0SMike Rapoport LIST_HEAD(uf_unmap); 6571da177e4SLinus Torvalds 658b2a84de2SWill Deacon /* 659b2a84de2SWill Deacon * There is a deliberate asymmetry here: we strip the pointer tag 660b2a84de2SWill Deacon * from the old address but leave the new address alone. This is 661b2a84de2SWill Deacon * for consistency with mmap(), where we prevent the creation of 662b2a84de2SWill Deacon * aliasing mappings in userspace by leaving the tag bits of the 663b2a84de2SWill Deacon * mapping address intact. A non-zero tag will cause the subsequent 664b2a84de2SWill Deacon * range checks to reject the address as invalid. 665b2a84de2SWill Deacon * 666b2a84de2SWill Deacon * See Documentation/arm64/tagged-address-abi.rst for more information. 667b2a84de2SWill Deacon */ 668057d3389SAndrey Konovalov addr = untagged_addr(addr); 669057d3389SAndrey Konovalov 670e346b381SBrian Geffon if (flags & ~(MREMAP_FIXED | MREMAP_MAYMOVE | MREMAP_DONTUNMAP)) 6719a2458a6SRasmus Villemoes return ret; 6729a2458a6SRasmus Villemoes 6739a2458a6SRasmus Villemoes if (flags & MREMAP_FIXED && !(flags & MREMAP_MAYMOVE)) 6749a2458a6SRasmus Villemoes return ret; 6751da177e4SLinus Torvalds 676e346b381SBrian Geffon /* 677e346b381SBrian Geffon * MREMAP_DONTUNMAP is always a move and it does not allow resizing 678e346b381SBrian Geffon * in the process. 679e346b381SBrian Geffon */ 680e346b381SBrian Geffon if (flags & MREMAP_DONTUNMAP && 681e346b381SBrian Geffon (!(flags & MREMAP_MAYMOVE) || old_len != new_len)) 682e346b381SBrian Geffon return ret; 683e346b381SBrian Geffon 684e346b381SBrian Geffon 685f19cb115SAlexander Kuleshov if (offset_in_page(addr)) 6869a2458a6SRasmus Villemoes return ret; 6871da177e4SLinus Torvalds 6881da177e4SLinus Torvalds old_len = PAGE_ALIGN(old_len); 6891da177e4SLinus Torvalds new_len = PAGE_ALIGN(new_len); 6901da177e4SLinus Torvalds 6911da177e4SLinus Torvalds /* 6921da177e4SLinus Torvalds * We allow a zero old-len as a special case 6931da177e4SLinus Torvalds * for DOS-emu "duplicate shm area" thing. But 6941da177e4SLinus Torvalds * a zero new-len is nonsensical. 6951da177e4SLinus Torvalds */ 6961da177e4SLinus Torvalds if (!new_len) 6979a2458a6SRasmus Villemoes return ret; 6989a2458a6SRasmus Villemoes 699d8ed45c5SMichel Lespinasse if (mmap_write_lock_killable(current->mm)) 700dc0ef0dfSMichal Hocko return -EINTR; 7011da177e4SLinus Torvalds 702e346b381SBrian Geffon if (flags & (MREMAP_FIXED | MREMAP_DONTUNMAP)) { 70381909b84SMichel Lespinasse ret = mremap_to(addr, old_len, new_addr, new_len, 704e346b381SBrian Geffon &locked, flags, &uf, &uf_unmap_early, 705e346b381SBrian Geffon &uf_unmap); 7061da177e4SLinus Torvalds goto out; 7071da177e4SLinus Torvalds } 7081da177e4SLinus Torvalds 7091da177e4SLinus Torvalds /* 7101da177e4SLinus Torvalds * Always allow a shrinking remap: that just unmaps 7111da177e4SLinus Torvalds * the unnecessary pages.. 71285a06835SYang Shi * __do_munmap does all the needed commit accounting, and 713*c1e8d7c6SMichel Lespinasse * downgrades mmap_lock to read if so directed. 7141da177e4SLinus Torvalds */ 7151da177e4SLinus Torvalds if (old_len >= new_len) { 71685a06835SYang Shi int retval; 71785a06835SYang Shi 71885a06835SYang Shi retval = __do_munmap(mm, addr+new_len, old_len - new_len, 71985a06835SYang Shi &uf_unmap, true); 72085a06835SYang Shi if (retval < 0 && old_len != new_len) { 72185a06835SYang Shi ret = retval; 7221da177e4SLinus Torvalds goto out; 723*c1e8d7c6SMichel Lespinasse /* Returning 1 indicates mmap_lock is downgraded to read. */ 72485a06835SYang Shi } else if (retval == 1) 72585a06835SYang Shi downgraded = true; 7261da177e4SLinus Torvalds ret = addr; 7271da177e4SLinus Torvalds goto out; 7281da177e4SLinus Torvalds } 7291da177e4SLinus Torvalds 7301da177e4SLinus Torvalds /* 731ecc1a899SAl Viro * Ok, we need to grow.. 7321da177e4SLinus Torvalds */ 733e346b381SBrian Geffon vma = vma_to_resize(addr, old_len, new_len, flags, &charged); 73454f5de70SAl Viro if (IS_ERR(vma)) { 73554f5de70SAl Viro ret = PTR_ERR(vma); 7361da177e4SLinus Torvalds goto out; 7371da177e4SLinus Torvalds } 7381da177e4SLinus Torvalds 7391da177e4SLinus Torvalds /* old_len exactly to the end of the area.. 7401da177e4SLinus Torvalds */ 741ecc1a899SAl Viro if (old_len == vma->vm_end - addr) { 7421da177e4SLinus Torvalds /* can we just expand the current mapping? */ 7431a0ef85fSAl Viro if (vma_expandable(vma, new_len - old_len)) { 7441da177e4SLinus Torvalds int pages = (new_len - old_len) >> PAGE_SHIFT; 7451da177e4SLinus Torvalds 7465beb4930SRik van Riel if (vma_adjust(vma, vma->vm_start, addr + new_len, 7475beb4930SRik van Riel vma->vm_pgoff, NULL)) { 7485beb4930SRik van Riel ret = -ENOMEM; 7495beb4930SRik van Riel goto out; 7505beb4930SRik van Riel } 7511da177e4SLinus Torvalds 75284638335SKonstantin Khlebnikov vm_stat_account(mm, vma->vm_flags, pages); 7531da177e4SLinus Torvalds if (vma->vm_flags & VM_LOCKED) { 754d0de32d9SHugh Dickins mm->locked_vm += pages; 75581909b84SMichel Lespinasse locked = true; 75681909b84SMichel Lespinasse new_addr = addr; 7571da177e4SLinus Torvalds } 7581da177e4SLinus Torvalds ret = addr; 7591da177e4SLinus Torvalds goto out; 7601da177e4SLinus Torvalds } 7611da177e4SLinus Torvalds } 7621da177e4SLinus Torvalds 7631da177e4SLinus Torvalds /* 7641da177e4SLinus Torvalds * We weren't able to just expand or shrink the area, 7651da177e4SLinus Torvalds * we need to create a new one and move it.. 7661da177e4SLinus Torvalds */ 7671da177e4SLinus Torvalds ret = -ENOMEM; 7681da177e4SLinus Torvalds if (flags & MREMAP_MAYMOVE) { 7691da177e4SLinus Torvalds unsigned long map_flags = 0; 7701da177e4SLinus Torvalds if (vma->vm_flags & VM_MAYSHARE) 7711da177e4SLinus Torvalds map_flags |= MAP_SHARED; 7721da177e4SLinus Torvalds 7731da177e4SLinus Torvalds new_addr = get_unmapped_area(vma->vm_file, 0, new_len, 77493587414SAl Viro vma->vm_pgoff + 77593587414SAl Viro ((addr - vma->vm_start) >> PAGE_SHIFT), 77693587414SAl Viro map_flags); 777ff68dac6SGaowei Pu if (IS_ERR_VALUE(new_addr)) { 7781da177e4SLinus Torvalds ret = new_addr; 779ed032189SEric Paris goto out; 780ed032189SEric Paris } 781ed032189SEric Paris 78272f87654SPavel Emelyanov ret = move_vma(vma, addr, old_len, new_len, new_addr, 783e346b381SBrian Geffon &locked, flags, &uf, &uf_unmap); 7841da177e4SLinus Torvalds } 7851da177e4SLinus Torvalds out: 786f19cb115SAlexander Kuleshov if (offset_in_page(ret)) { 7871da177e4SLinus Torvalds vm_unacct_memory(charged); 788fa1f68ccSZou Wei locked = false; 789d456fb9eSOleg Nesterov } 79085a06835SYang Shi if (downgraded) 791d8ed45c5SMichel Lespinasse mmap_read_unlock(current->mm); 79285a06835SYang Shi else 793d8ed45c5SMichel Lespinasse mmap_write_unlock(current->mm); 79481909b84SMichel Lespinasse if (locked && new_len > old_len) 79581909b84SMichel Lespinasse mm_populate(new_addr + old_len, new_len - old_len); 796b2282371SMike Rapoport userfaultfd_unmap_complete(mm, &uf_unmap_early); 797d1564926SBrian Geffon mremap_userfaultfd_complete(&uf, addr, ret, old_len); 798897ab3e0SMike Rapoport userfaultfd_unmap_complete(mm, &uf_unmap); 7991da177e4SLinus Torvalds return ret; 8001da177e4SLinus Torvalds } 801