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 13638a76013SMichel Lespinasse * which rmap call sites look for using is_vma_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 1494c21e2f2SHugh Dickins * pte locks because exclusive mmap_sem 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 194*2c91bd4aSJoel Fernandes (Google) #ifdef CONFIG_HAVE_MOVE_PMD 195*2c91bd4aSJoel Fernandes (Google) static bool move_normal_pmd(struct vm_area_struct *vma, unsigned long old_addr, 196*2c91bd4aSJoel Fernandes (Google) unsigned long new_addr, unsigned long old_end, 197*2c91bd4aSJoel Fernandes (Google) pmd_t *old_pmd, pmd_t *new_pmd) 198*2c91bd4aSJoel Fernandes (Google) { 199*2c91bd4aSJoel Fernandes (Google) spinlock_t *old_ptl, *new_ptl; 200*2c91bd4aSJoel Fernandes (Google) struct mm_struct *mm = vma->vm_mm; 201*2c91bd4aSJoel Fernandes (Google) pmd_t pmd; 202*2c91bd4aSJoel Fernandes (Google) 203*2c91bd4aSJoel Fernandes (Google) if ((old_addr & ~PMD_MASK) || (new_addr & ~PMD_MASK) 204*2c91bd4aSJoel Fernandes (Google) || old_end - old_addr < PMD_SIZE) 205*2c91bd4aSJoel Fernandes (Google) return false; 206*2c91bd4aSJoel Fernandes (Google) 207*2c91bd4aSJoel Fernandes (Google) /* 208*2c91bd4aSJoel Fernandes (Google) * The destination pmd shouldn't be established, free_pgtables() 209*2c91bd4aSJoel Fernandes (Google) * should have release it. 210*2c91bd4aSJoel Fernandes (Google) */ 211*2c91bd4aSJoel Fernandes (Google) if (WARN_ON(!pmd_none(*new_pmd))) 212*2c91bd4aSJoel Fernandes (Google) return false; 213*2c91bd4aSJoel Fernandes (Google) 214*2c91bd4aSJoel Fernandes (Google) /* 215*2c91bd4aSJoel Fernandes (Google) * We don't have to worry about the ordering of src and dst 216*2c91bd4aSJoel Fernandes (Google) * ptlocks because exclusive mmap_sem prevents deadlock. 217*2c91bd4aSJoel Fernandes (Google) */ 218*2c91bd4aSJoel Fernandes (Google) old_ptl = pmd_lock(vma->vm_mm, old_pmd); 219*2c91bd4aSJoel Fernandes (Google) new_ptl = pmd_lockptr(mm, new_pmd); 220*2c91bd4aSJoel Fernandes (Google) if (new_ptl != old_ptl) 221*2c91bd4aSJoel Fernandes (Google) spin_lock_nested(new_ptl, SINGLE_DEPTH_NESTING); 222*2c91bd4aSJoel Fernandes (Google) 223*2c91bd4aSJoel Fernandes (Google) /* Clear the pmd */ 224*2c91bd4aSJoel Fernandes (Google) pmd = *old_pmd; 225*2c91bd4aSJoel Fernandes (Google) pmd_clear(old_pmd); 226*2c91bd4aSJoel Fernandes (Google) 227*2c91bd4aSJoel Fernandes (Google) VM_BUG_ON(!pmd_none(*new_pmd)); 228*2c91bd4aSJoel Fernandes (Google) 229*2c91bd4aSJoel Fernandes (Google) /* Set the new pmd */ 230*2c91bd4aSJoel Fernandes (Google) set_pmd_at(mm, new_addr, new_pmd, pmd); 231*2c91bd4aSJoel Fernandes (Google) flush_tlb_range(vma, old_addr, old_addr + PMD_SIZE); 232*2c91bd4aSJoel Fernandes (Google) if (new_ptl != old_ptl) 233*2c91bd4aSJoel Fernandes (Google) spin_unlock(new_ptl); 234*2c91bd4aSJoel Fernandes (Google) spin_unlock(old_ptl); 235*2c91bd4aSJoel Fernandes (Google) 236*2c91bd4aSJoel Fernandes (Google) return true; 237*2c91bd4aSJoel Fernandes (Google) } 238*2c91bd4aSJoel Fernandes (Google) #endif 239*2c91bd4aSJoel 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 252ac46d4f3SJérôme Glisse mmu_notifier_range_init(&range, vma->vm_mm, old_addr, old_end); 253ac46d4f3SJérôme Glisse mmu_notifier_invalidate_range_start(&range); 2547b6efc2bSAndrea Arcangeli 2557be7a546SHugh Dickins for (; old_addr < old_end; old_addr += extent, new_addr += extent) { 2561da177e4SLinus Torvalds cond_resched(); 2577be7a546SHugh Dickins next = (old_addr + PMD_SIZE) & PMD_MASK; 258ebed4846SAndrea Arcangeli /* even if next overflowed, extent below will be ok */ 2597be7a546SHugh Dickins extent = next - old_addr; 260ebed4846SAndrea Arcangeli if (extent > old_end - old_addr) 261ebed4846SAndrea Arcangeli extent = old_end - old_addr; 2627be7a546SHugh Dickins old_pmd = get_old_pmd(vma->vm_mm, old_addr); 2637be7a546SHugh Dickins if (!old_pmd) 2647be7a546SHugh Dickins continue; 2658ac1f832SAndrea Arcangeli new_pmd = alloc_new_pmd(vma->vm_mm, vma, new_addr); 2667be7a546SHugh Dickins if (!new_pmd) 2677be7a546SHugh Dickins break; 26884c3fc4eSZi Yan if (is_swap_pmd(*old_pmd) || pmd_trans_huge(*old_pmd)) { 269dd18dbc2SKirill A. Shutemov if (extent == HPAGE_PMD_SIZE) { 2704b471e88SKirill A. Shutemov bool moved; 271dd18dbc2SKirill A. Shutemov /* See comment in move_ptes() */ 272dd18dbc2SKirill A. Shutemov if (need_rmap_locks) 2731d069b7dSHugh Dickins take_rmap_locks(vma); 274bf8616d5SHugh Dickins moved = move_huge_pmd(vma, old_addr, new_addr, 275eb66ae03SLinus Torvalds old_end, old_pmd, new_pmd); 276dd18dbc2SKirill A. Shutemov if (need_rmap_locks) 2771d069b7dSHugh Dickins drop_rmap_locks(vma); 2785d190420SAaron Lu if (moved) 27937a1c49aSAndrea Arcangeli continue; 28037a1c49aSAndrea Arcangeli } 2814b471e88SKirill A. Shutemov split_huge_pmd(vma, old_pmd, old_addr); 282337d9abfSNaoya Horiguchi if (pmd_trans_unstable(old_pmd)) 2836b9116a6SKirill A. Shutemov continue; 284*2c91bd4aSJoel Fernandes (Google) } else if (extent == PMD_SIZE) { 285*2c91bd4aSJoel Fernandes (Google) #ifdef CONFIG_HAVE_MOVE_PMD 286*2c91bd4aSJoel Fernandes (Google) /* 287*2c91bd4aSJoel Fernandes (Google) * If the extent is PMD-sized, try to speed the move by 288*2c91bd4aSJoel Fernandes (Google) * moving at the PMD level if possible. 289*2c91bd4aSJoel Fernandes (Google) */ 290*2c91bd4aSJoel Fernandes (Google) bool moved; 291*2c91bd4aSJoel Fernandes (Google) 292*2c91bd4aSJoel Fernandes (Google) if (need_rmap_locks) 293*2c91bd4aSJoel Fernandes (Google) take_rmap_locks(vma); 294*2c91bd4aSJoel Fernandes (Google) moved = move_normal_pmd(vma, old_addr, new_addr, 295*2c91bd4aSJoel Fernandes (Google) old_end, old_pmd, new_pmd); 296*2c91bd4aSJoel Fernandes (Google) if (need_rmap_locks) 297*2c91bd4aSJoel Fernandes (Google) drop_rmap_locks(vma); 298*2c91bd4aSJoel Fernandes (Google) if (moved) 299*2c91bd4aSJoel Fernandes (Google) continue; 300*2c91bd4aSJoel Fernandes (Google) #endif 30137a1c49aSAndrea Arcangeli } 302*2c91bd4aSJoel Fernandes (Google) 3034cf58924SJoel Fernandes (Google) if (pte_alloc(new_vma->vm_mm, new_pmd)) 30437a1c49aSAndrea Arcangeli break; 3057be7a546SHugh Dickins next = (new_addr + PMD_SIZE) & PMD_MASK; 3067be7a546SHugh Dickins if (extent > next - new_addr) 3077be7a546SHugh Dickins extent = next - new_addr; 3085d190420SAaron Lu move_ptes(vma, old_pmd, old_addr, old_addr + extent, new_vma, 309eb66ae03SLinus Torvalds new_pmd, new_addr, need_rmap_locks); 3101da177e4SLinus Torvalds } 3117b6efc2bSAndrea Arcangeli 312ac46d4f3SJérôme Glisse mmu_notifier_invalidate_range_end(&range); 3137be7a546SHugh Dickins 3147be7a546SHugh Dickins return len + old_addr - old_end; /* how much done */ 3151da177e4SLinus Torvalds } 3161da177e4SLinus Torvalds 3171da177e4SLinus Torvalds static unsigned long move_vma(struct vm_area_struct *vma, 3181da177e4SLinus Torvalds unsigned long old_addr, unsigned long old_len, 31972f87654SPavel Emelyanov unsigned long new_len, unsigned long new_addr, 320897ab3e0SMike Rapoport bool *locked, struct vm_userfaultfd_ctx *uf, 321897ab3e0SMike Rapoport struct list_head *uf_unmap) 3221da177e4SLinus Torvalds { 3231da177e4SLinus Torvalds struct mm_struct *mm = vma->vm_mm; 3241da177e4SLinus Torvalds struct vm_area_struct *new_vma; 3251da177e4SLinus Torvalds unsigned long vm_flags = vma->vm_flags; 3261da177e4SLinus Torvalds unsigned long new_pgoff; 3271da177e4SLinus Torvalds unsigned long moved_len; 3281da177e4SLinus Torvalds unsigned long excess = 0; 329365e9c87SHugh Dickins unsigned long hiwater_vm; 3301da177e4SLinus Torvalds int split = 0; 3317103ad32SHugh Dickins int err; 33238a76013SMichel Lespinasse bool need_rmap_locks; 3331da177e4SLinus Torvalds 3341da177e4SLinus Torvalds /* 3351da177e4SLinus Torvalds * We'd prefer to avoid failure later on in do_munmap: 3361da177e4SLinus Torvalds * which may split one vma into three before unmapping. 3371da177e4SLinus Torvalds */ 3381da177e4SLinus Torvalds if (mm->map_count >= sysctl_max_map_count - 3) 3391da177e4SLinus Torvalds return -ENOMEM; 3401da177e4SLinus Torvalds 3411ff82995SHugh Dickins /* 3421ff82995SHugh Dickins * Advise KSM to break any KSM pages in the area to be moved: 3431ff82995SHugh Dickins * it would be confusing if they were to turn up at the new 3441ff82995SHugh Dickins * location, where they happen to coincide with different KSM 3451ff82995SHugh Dickins * pages recently unmapped. But leave vma->vm_flags as it was, 3461ff82995SHugh Dickins * so KSM can come around to merge on vma and new_vma afterwards. 3471ff82995SHugh Dickins */ 3487103ad32SHugh Dickins err = ksm_madvise(vma, old_addr, old_addr + old_len, 3497103ad32SHugh Dickins MADV_UNMERGEABLE, &vm_flags); 3507103ad32SHugh Dickins if (err) 3517103ad32SHugh Dickins return err; 3521ff82995SHugh Dickins 3531da177e4SLinus Torvalds new_pgoff = vma->vm_pgoff + ((old_addr - vma->vm_start) >> PAGE_SHIFT); 35438a76013SMichel Lespinasse new_vma = copy_vma(&vma, new_addr, new_len, new_pgoff, 35538a76013SMichel Lespinasse &need_rmap_locks); 3561da177e4SLinus Torvalds if (!new_vma) 3571da177e4SLinus Torvalds return -ENOMEM; 3581da177e4SLinus Torvalds 35938a76013SMichel Lespinasse moved_len = move_page_tables(vma, old_addr, new_vma, new_addr, old_len, 36038a76013SMichel Lespinasse need_rmap_locks); 3611da177e4SLinus Torvalds if (moved_len < old_len) { 362df1eab30SOleg Nesterov err = -ENOMEM; 3635477e70aSOleg Nesterov } else if (vma->vm_ops && vma->vm_ops->mremap) { 3645477e70aSOleg Nesterov err = vma->vm_ops->mremap(new_vma); 365df1eab30SOleg Nesterov } 366df1eab30SOleg Nesterov 367df1eab30SOleg Nesterov if (unlikely(err)) { 3681da177e4SLinus Torvalds /* 3691da177e4SLinus Torvalds * On error, move entries back from new area to old, 3701da177e4SLinus Torvalds * which will succeed since page tables still there, 3711da177e4SLinus Torvalds * and then proceed to unmap new area instead of old. 3721da177e4SLinus Torvalds */ 37338a76013SMichel Lespinasse move_page_tables(new_vma, new_addr, vma, old_addr, moved_len, 37438a76013SMichel Lespinasse true); 3751da177e4SLinus Torvalds vma = new_vma; 3761da177e4SLinus Torvalds old_len = new_len; 3771da177e4SLinus Torvalds old_addr = new_addr; 378df1eab30SOleg Nesterov new_addr = err; 3794abad2caSLaurent Dufour } else { 38072f87654SPavel Emelyanov mremap_userfaultfd_prep(new_vma, uf); 3814abad2caSLaurent Dufour arch_remap(mm, old_addr, old_addr + old_len, 3824abad2caSLaurent Dufour new_addr, new_addr + new_len); 3834abad2caSLaurent Dufour } 3841da177e4SLinus Torvalds 3851da177e4SLinus Torvalds /* Conceal VM_ACCOUNT so old reservation is not undone */ 3861da177e4SLinus Torvalds if (vm_flags & VM_ACCOUNT) { 3871da177e4SLinus Torvalds vma->vm_flags &= ~VM_ACCOUNT; 3881da177e4SLinus Torvalds excess = vma->vm_end - vma->vm_start - old_len; 3891da177e4SLinus Torvalds if (old_addr > vma->vm_start && 3901da177e4SLinus Torvalds old_addr + old_len < vma->vm_end) 3911da177e4SLinus Torvalds split = 1; 3921da177e4SLinus Torvalds } 3931da177e4SLinus Torvalds 39471799062SKirill Korotaev /* 395365e9c87SHugh Dickins * If we failed to move page tables we still do total_vm increment 396365e9c87SHugh Dickins * since do_munmap() will decrement it by old_len == new_len. 397365e9c87SHugh Dickins * 398365e9c87SHugh Dickins * Since total_vm is about to be raised artificially high for a 399365e9c87SHugh Dickins * moment, we need to restore high watermark afterwards: if stats 400365e9c87SHugh Dickins * are taken meanwhile, total_vm and hiwater_vm appear too high. 401365e9c87SHugh Dickins * If this were a serious issue, we'd add a flag to do_munmap(). 40271799062SKirill Korotaev */ 403365e9c87SHugh Dickins hiwater_vm = mm->hiwater_vm; 40484638335SKonstantin Khlebnikov vm_stat_account(mm, vma->vm_flags, new_len >> PAGE_SHIFT); 40571799062SKirill Korotaev 406d9fe4fabSToshi Kani /* Tell pfnmap has moved from this vma */ 407d9fe4fabSToshi Kani if (unlikely(vma->vm_flags & VM_PFNMAP)) 408d9fe4fabSToshi Kani untrack_pfn_moved(vma); 409d9fe4fabSToshi Kani 410897ab3e0SMike Rapoport if (do_munmap(mm, old_addr, old_len, uf_unmap) < 0) { 4111da177e4SLinus Torvalds /* OOM: unable to split vma, just get accounts right */ 4121da177e4SLinus Torvalds vm_unacct_memory(excess >> PAGE_SHIFT); 4131da177e4SLinus Torvalds excess = 0; 4141da177e4SLinus Torvalds } 415365e9c87SHugh Dickins mm->hiwater_vm = hiwater_vm; 4161da177e4SLinus Torvalds 4171da177e4SLinus Torvalds /* Restore VM_ACCOUNT if one or two pieces of vma left */ 4181da177e4SLinus Torvalds if (excess) { 4191da177e4SLinus Torvalds vma->vm_flags |= VM_ACCOUNT; 4201da177e4SLinus Torvalds if (split) 4211da177e4SLinus Torvalds vma->vm_next->vm_flags |= VM_ACCOUNT; 4221da177e4SLinus Torvalds } 4231da177e4SLinus Torvalds 4241da177e4SLinus Torvalds if (vm_flags & VM_LOCKED) { 4251da177e4SLinus Torvalds mm->locked_vm += new_len >> PAGE_SHIFT; 42681909b84SMichel Lespinasse *locked = true; 4271da177e4SLinus Torvalds } 4281da177e4SLinus Torvalds 4291da177e4SLinus Torvalds return new_addr; 4301da177e4SLinus Torvalds } 4311da177e4SLinus Torvalds 43254f5de70SAl Viro static struct vm_area_struct *vma_to_resize(unsigned long addr, 43354f5de70SAl Viro unsigned long old_len, unsigned long new_len, unsigned long *p) 43454f5de70SAl Viro { 43554f5de70SAl Viro struct mm_struct *mm = current->mm; 43654f5de70SAl Viro struct vm_area_struct *vma = find_vma(mm, addr); 4371d391686SOleg Nesterov unsigned long pgoff; 43854f5de70SAl Viro 43954f5de70SAl Viro if (!vma || vma->vm_start > addr) 4406cd57613SDerek return ERR_PTR(-EFAULT); 44154f5de70SAl Viro 442dba58d3bSMike Kravetz /* 443dba58d3bSMike Kravetz * !old_len is a special case where an attempt is made to 'duplicate' 444dba58d3bSMike Kravetz * a mapping. This makes no sense for private mappings as it will 445dba58d3bSMike Kravetz * instead create a fresh/new mapping unrelated to the original. This 446dba58d3bSMike Kravetz * is contrary to the basic idea of mremap which creates new mappings 447dba58d3bSMike Kravetz * based on the original. There are no known use cases for this 448dba58d3bSMike Kravetz * behavior. As a result, fail such attempts. 449dba58d3bSMike Kravetz */ 450dba58d3bSMike Kravetz if (!old_len && !(vma->vm_flags & (VM_SHARED | VM_MAYSHARE))) { 451dba58d3bSMike Kravetz pr_warn_once("%s (%d): attempted to duplicate a private mapping with mremap. This is not supported.\n", current->comm, current->pid); 452dba58d3bSMike Kravetz return ERR_PTR(-EINVAL); 453dba58d3bSMike Kravetz } 454dba58d3bSMike Kravetz 45554f5de70SAl Viro if (is_vm_hugetlb_page(vma)) 4566cd57613SDerek return ERR_PTR(-EINVAL); 45754f5de70SAl Viro 45854f5de70SAl Viro /* We can't remap across vm area boundaries */ 45954f5de70SAl Viro if (old_len > vma->vm_end - addr) 4606cd57613SDerek return ERR_PTR(-EFAULT); 46154f5de70SAl Viro 4621d391686SOleg Nesterov if (new_len == old_len) 4631d391686SOleg Nesterov return vma; 464982134baSLinus Torvalds 4651d391686SOleg Nesterov /* Need to be careful about a growing mapping */ 466982134baSLinus Torvalds pgoff = (addr - vma->vm_start) >> PAGE_SHIFT; 467982134baSLinus Torvalds pgoff += vma->vm_pgoff; 468982134baSLinus Torvalds if (pgoff + (new_len >> PAGE_SHIFT) < pgoff) 4696cd57613SDerek return ERR_PTR(-EINVAL); 4701d391686SOleg Nesterov 4711d391686SOleg Nesterov if (vma->vm_flags & (VM_DONTEXPAND | VM_PFNMAP)) 4721d391686SOleg Nesterov return ERR_PTR(-EFAULT); 47354f5de70SAl Viro 47454f5de70SAl Viro if (vma->vm_flags & VM_LOCKED) { 47554f5de70SAl Viro unsigned long locked, lock_limit; 47654f5de70SAl Viro locked = mm->locked_vm << PAGE_SHIFT; 47759e99e5bSJiri Slaby lock_limit = rlimit(RLIMIT_MEMLOCK); 47854f5de70SAl Viro locked += new_len - old_len; 47954f5de70SAl Viro if (locked > lock_limit && !capable(CAP_IPC_LOCK)) 4806cd57613SDerek return ERR_PTR(-EAGAIN); 48154f5de70SAl Viro } 48254f5de70SAl Viro 48384638335SKonstantin Khlebnikov if (!may_expand_vm(mm, vma->vm_flags, 48484638335SKonstantin Khlebnikov (new_len - old_len) >> PAGE_SHIFT)) 4856cd57613SDerek return ERR_PTR(-ENOMEM); 48654f5de70SAl Viro 48754f5de70SAl Viro if (vma->vm_flags & VM_ACCOUNT) { 48854f5de70SAl Viro unsigned long charged = (new_len - old_len) >> PAGE_SHIFT; 489191c5424SAl Viro if (security_vm_enough_memory_mm(mm, charged)) 4906cd57613SDerek return ERR_PTR(-ENOMEM); 49154f5de70SAl Viro *p = charged; 49254f5de70SAl Viro } 49354f5de70SAl Viro 49454f5de70SAl Viro return vma; 49554f5de70SAl Viro } 49654f5de70SAl Viro 49781909b84SMichel Lespinasse static unsigned long mremap_to(unsigned long addr, unsigned long old_len, 49872f87654SPavel Emelyanov unsigned long new_addr, unsigned long new_len, bool *locked, 499897ab3e0SMike Rapoport struct vm_userfaultfd_ctx *uf, 500b2282371SMike Rapoport struct list_head *uf_unmap_early, 501897ab3e0SMike Rapoport struct list_head *uf_unmap) 502ecc1a899SAl Viro { 503ecc1a899SAl Viro struct mm_struct *mm = current->mm; 504ecc1a899SAl Viro struct vm_area_struct *vma; 505ecc1a899SAl Viro unsigned long ret = -EINVAL; 506ecc1a899SAl Viro unsigned long charged = 0; 507097eed10SAl Viro unsigned long map_flags; 508ecc1a899SAl Viro 509f19cb115SAlexander Kuleshov if (offset_in_page(new_addr)) 510ecc1a899SAl Viro goto out; 511ecc1a899SAl Viro 512ecc1a899SAl Viro if (new_len > TASK_SIZE || new_addr > TASK_SIZE - new_len) 513ecc1a899SAl Viro goto out; 514ecc1a899SAl Viro 5159943242cSOleg Nesterov /* Ensure the old/new locations do not overlap */ 5169943242cSOleg Nesterov if (addr + old_len > new_addr && new_addr + new_len > addr) 517ecc1a899SAl Viro goto out; 518ecc1a899SAl Viro 519b2282371SMike Rapoport ret = do_munmap(mm, new_addr, new_len, uf_unmap_early); 520ecc1a899SAl Viro if (ret) 521ecc1a899SAl Viro goto out; 522ecc1a899SAl Viro 523ecc1a899SAl Viro if (old_len >= new_len) { 524897ab3e0SMike Rapoport ret = do_munmap(mm, addr+new_len, old_len - new_len, uf_unmap); 525ecc1a899SAl Viro if (ret && old_len != new_len) 526ecc1a899SAl Viro goto out; 527ecc1a899SAl Viro old_len = new_len; 528ecc1a899SAl Viro } 529ecc1a899SAl Viro 530ecc1a899SAl Viro vma = vma_to_resize(addr, old_len, new_len, &charged); 531ecc1a899SAl Viro if (IS_ERR(vma)) { 532ecc1a899SAl Viro ret = PTR_ERR(vma); 533ecc1a899SAl Viro goto out; 534ecc1a899SAl Viro } 535ecc1a899SAl Viro 536097eed10SAl Viro map_flags = MAP_FIXED; 537097eed10SAl Viro if (vma->vm_flags & VM_MAYSHARE) 538097eed10SAl Viro map_flags |= MAP_SHARED; 5399206de95SAl Viro 540097eed10SAl Viro ret = get_unmapped_area(vma->vm_file, new_addr, new_len, vma->vm_pgoff + 541097eed10SAl Viro ((addr - vma->vm_start) >> PAGE_SHIFT), 542097eed10SAl Viro map_flags); 543f19cb115SAlexander Kuleshov if (offset_in_page(ret)) 544097eed10SAl Viro goto out1; 545097eed10SAl Viro 546897ab3e0SMike Rapoport ret = move_vma(vma, addr, old_len, new_len, new_addr, locked, uf, 547897ab3e0SMike Rapoport uf_unmap); 548f19cb115SAlexander Kuleshov if (!(offset_in_page(ret))) 549097eed10SAl Viro goto out; 550097eed10SAl Viro out1: 551ecc1a899SAl Viro vm_unacct_memory(charged); 552ecc1a899SAl Viro 553ecc1a899SAl Viro out: 554ecc1a899SAl Viro return ret; 555ecc1a899SAl Viro } 556ecc1a899SAl Viro 5571a0ef85fSAl Viro static int vma_expandable(struct vm_area_struct *vma, unsigned long delta) 5581a0ef85fSAl Viro { 559f106af4eSAl Viro unsigned long end = vma->vm_end + delta; 5609206de95SAl Viro if (end < vma->vm_end) /* overflow */ 5611a0ef85fSAl Viro return 0; 5629206de95SAl Viro if (vma->vm_next && vma->vm_next->vm_start < end) /* intersection */ 563f106af4eSAl Viro return 0; 564f106af4eSAl Viro if (get_unmapped_area(NULL, vma->vm_start, end - vma->vm_start, 565f106af4eSAl Viro 0, MAP_FIXED) & ~PAGE_MASK) 566f106af4eSAl Viro return 0; 5671a0ef85fSAl Viro return 1; 5681a0ef85fSAl Viro } 5691a0ef85fSAl Viro 5701da177e4SLinus Torvalds /* 5711da177e4SLinus Torvalds * Expand (or shrink) an existing mapping, potentially moving it at the 5721da177e4SLinus Torvalds * same time (controlled by the MREMAP_MAYMOVE flag and available VM space) 5731da177e4SLinus Torvalds * 5741da177e4SLinus Torvalds * MREMAP_FIXED option added 5-Dec-1999 by Benjamin LaHaise 5751da177e4SLinus Torvalds * This option implies MREMAP_MAYMOVE. 5761da177e4SLinus Torvalds */ 57763a81db1SAl Viro SYSCALL_DEFINE5(mremap, unsigned long, addr, unsigned long, old_len, 57863a81db1SAl Viro unsigned long, new_len, unsigned long, flags, 57963a81db1SAl Viro unsigned long, new_addr) 5801da177e4SLinus Torvalds { 581d0de32d9SHugh Dickins struct mm_struct *mm = current->mm; 5821da177e4SLinus Torvalds struct vm_area_struct *vma; 5831da177e4SLinus Torvalds unsigned long ret = -EINVAL; 5841da177e4SLinus Torvalds unsigned long charged = 0; 58581909b84SMichel Lespinasse bool locked = false; 58685a06835SYang Shi bool downgraded = false; 58772f87654SPavel Emelyanov struct vm_userfaultfd_ctx uf = NULL_VM_UFFD_CTX; 588b2282371SMike Rapoport LIST_HEAD(uf_unmap_early); 589897ab3e0SMike Rapoport LIST_HEAD(uf_unmap); 5901da177e4SLinus Torvalds 5911da177e4SLinus Torvalds if (flags & ~(MREMAP_FIXED | MREMAP_MAYMOVE)) 5929a2458a6SRasmus Villemoes return ret; 5939a2458a6SRasmus Villemoes 5949a2458a6SRasmus Villemoes if (flags & MREMAP_FIXED && !(flags & MREMAP_MAYMOVE)) 5959a2458a6SRasmus Villemoes return ret; 5961da177e4SLinus Torvalds 597f19cb115SAlexander Kuleshov if (offset_in_page(addr)) 5989a2458a6SRasmus Villemoes return ret; 5991da177e4SLinus Torvalds 6001da177e4SLinus Torvalds old_len = PAGE_ALIGN(old_len); 6011da177e4SLinus Torvalds new_len = PAGE_ALIGN(new_len); 6021da177e4SLinus Torvalds 6031da177e4SLinus Torvalds /* 6041da177e4SLinus Torvalds * We allow a zero old-len as a special case 6051da177e4SLinus Torvalds * for DOS-emu "duplicate shm area" thing. But 6061da177e4SLinus Torvalds * a zero new-len is nonsensical. 6071da177e4SLinus Torvalds */ 6081da177e4SLinus Torvalds if (!new_len) 6099a2458a6SRasmus Villemoes return ret; 6109a2458a6SRasmus Villemoes 611dc0ef0dfSMichal Hocko if (down_write_killable(¤t->mm->mmap_sem)) 612dc0ef0dfSMichal Hocko return -EINTR; 6131da177e4SLinus Torvalds 6141da177e4SLinus Torvalds if (flags & MREMAP_FIXED) { 61581909b84SMichel Lespinasse ret = mremap_to(addr, old_len, new_addr, new_len, 616b2282371SMike Rapoport &locked, &uf, &uf_unmap_early, &uf_unmap); 6171da177e4SLinus Torvalds goto out; 6181da177e4SLinus Torvalds } 6191da177e4SLinus Torvalds 6201da177e4SLinus Torvalds /* 6211da177e4SLinus Torvalds * Always allow a shrinking remap: that just unmaps 6221da177e4SLinus Torvalds * the unnecessary pages.. 62385a06835SYang Shi * __do_munmap does all the needed commit accounting, and 62485a06835SYang Shi * downgrades mmap_sem to read if so directed. 6251da177e4SLinus Torvalds */ 6261da177e4SLinus Torvalds if (old_len >= new_len) { 62785a06835SYang Shi int retval; 62885a06835SYang Shi 62985a06835SYang Shi retval = __do_munmap(mm, addr+new_len, old_len - new_len, 63085a06835SYang Shi &uf_unmap, true); 63185a06835SYang Shi if (retval < 0 && old_len != new_len) { 63285a06835SYang Shi ret = retval; 6331da177e4SLinus Torvalds goto out; 63485a06835SYang Shi /* Returning 1 indicates mmap_sem is downgraded to read. */ 63585a06835SYang Shi } else if (retval == 1) 63685a06835SYang Shi downgraded = true; 6371da177e4SLinus Torvalds ret = addr; 6381da177e4SLinus Torvalds goto out; 6391da177e4SLinus Torvalds } 6401da177e4SLinus Torvalds 6411da177e4SLinus Torvalds /* 642ecc1a899SAl Viro * Ok, we need to grow.. 6431da177e4SLinus Torvalds */ 64454f5de70SAl Viro vma = vma_to_resize(addr, old_len, new_len, &charged); 64554f5de70SAl Viro if (IS_ERR(vma)) { 64654f5de70SAl Viro ret = PTR_ERR(vma); 6471da177e4SLinus Torvalds goto out; 6481da177e4SLinus Torvalds } 6491da177e4SLinus Torvalds 6501da177e4SLinus Torvalds /* old_len exactly to the end of the area.. 6511da177e4SLinus Torvalds */ 652ecc1a899SAl Viro if (old_len == vma->vm_end - addr) { 6531da177e4SLinus Torvalds /* can we just expand the current mapping? */ 6541a0ef85fSAl Viro if (vma_expandable(vma, new_len - old_len)) { 6551da177e4SLinus Torvalds int pages = (new_len - old_len) >> PAGE_SHIFT; 6561da177e4SLinus Torvalds 6575beb4930SRik van Riel if (vma_adjust(vma, vma->vm_start, addr + new_len, 6585beb4930SRik van Riel vma->vm_pgoff, NULL)) { 6595beb4930SRik van Riel ret = -ENOMEM; 6605beb4930SRik van Riel goto out; 6615beb4930SRik van Riel } 6621da177e4SLinus Torvalds 66384638335SKonstantin Khlebnikov vm_stat_account(mm, vma->vm_flags, pages); 6641da177e4SLinus Torvalds if (vma->vm_flags & VM_LOCKED) { 665d0de32d9SHugh Dickins mm->locked_vm += pages; 66681909b84SMichel Lespinasse locked = true; 66781909b84SMichel Lespinasse new_addr = addr; 6681da177e4SLinus Torvalds } 6691da177e4SLinus Torvalds ret = addr; 6701da177e4SLinus Torvalds goto out; 6711da177e4SLinus Torvalds } 6721da177e4SLinus Torvalds } 6731da177e4SLinus Torvalds 6741da177e4SLinus Torvalds /* 6751da177e4SLinus Torvalds * We weren't able to just expand or shrink the area, 6761da177e4SLinus Torvalds * we need to create a new one and move it.. 6771da177e4SLinus Torvalds */ 6781da177e4SLinus Torvalds ret = -ENOMEM; 6791da177e4SLinus Torvalds if (flags & MREMAP_MAYMOVE) { 6801da177e4SLinus Torvalds unsigned long map_flags = 0; 6811da177e4SLinus Torvalds if (vma->vm_flags & VM_MAYSHARE) 6821da177e4SLinus Torvalds map_flags |= MAP_SHARED; 6831da177e4SLinus Torvalds 6841da177e4SLinus Torvalds new_addr = get_unmapped_area(vma->vm_file, 0, new_len, 68593587414SAl Viro vma->vm_pgoff + 68693587414SAl Viro ((addr - vma->vm_start) >> PAGE_SHIFT), 68793587414SAl Viro map_flags); 688f19cb115SAlexander Kuleshov if (offset_in_page(new_addr)) { 6891da177e4SLinus Torvalds ret = new_addr; 690ed032189SEric Paris goto out; 691ed032189SEric Paris } 692ed032189SEric Paris 69372f87654SPavel Emelyanov ret = move_vma(vma, addr, old_len, new_len, new_addr, 694897ab3e0SMike Rapoport &locked, &uf, &uf_unmap); 6951da177e4SLinus Torvalds } 6961da177e4SLinus Torvalds out: 697f19cb115SAlexander Kuleshov if (offset_in_page(ret)) { 6981da177e4SLinus Torvalds vm_unacct_memory(charged); 699d456fb9eSOleg Nesterov locked = 0; 700d456fb9eSOleg Nesterov } 70185a06835SYang Shi if (downgraded) 70285a06835SYang Shi up_read(¤t->mm->mmap_sem); 70385a06835SYang Shi else 7041da177e4SLinus Torvalds up_write(¤t->mm->mmap_sem); 70581909b84SMichel Lespinasse if (locked && new_len > old_len) 70681909b84SMichel Lespinasse mm_populate(new_addr + old_len, new_len - old_len); 707b2282371SMike Rapoport userfaultfd_unmap_complete(mm, &uf_unmap_early); 70890794bf1SAndrea Arcangeli mremap_userfaultfd_complete(&uf, addr, new_addr, old_len); 709897ab3e0SMike Rapoport userfaultfd_unmap_complete(mm, &uf_unmap); 7101da177e4SLinus Torvalds return ret; 7111da177e4SLinus Torvalds } 712