162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0 262306a36Sopenharmony_ci/* 362306a36Sopenharmony_ci * mm/mremap.c 462306a36Sopenharmony_ci * 562306a36Sopenharmony_ci * (C) Copyright 1996 Linus Torvalds 662306a36Sopenharmony_ci * 762306a36Sopenharmony_ci * Address space accounting code <alan@lxorguk.ukuu.org.uk> 862306a36Sopenharmony_ci * (C) Copyright 2002 Red Hat Inc, All Rights Reserved 962306a36Sopenharmony_ci */ 1062306a36Sopenharmony_ci 1162306a36Sopenharmony_ci#include <linux/mm.h> 1262306a36Sopenharmony_ci#include <linux/mm_inline.h> 1362306a36Sopenharmony_ci#include <linux/hugetlb.h> 1462306a36Sopenharmony_ci#include <linux/shm.h> 1562306a36Sopenharmony_ci#include <linux/ksm.h> 1662306a36Sopenharmony_ci#include <linux/mman.h> 1762306a36Sopenharmony_ci#include <linux/swap.h> 1862306a36Sopenharmony_ci#include <linux/capability.h> 1962306a36Sopenharmony_ci#include <linux/fs.h> 2062306a36Sopenharmony_ci#include <linux/swapops.h> 2162306a36Sopenharmony_ci#include <linux/highmem.h> 2262306a36Sopenharmony_ci#include <linux/security.h> 2362306a36Sopenharmony_ci#include <linux/syscalls.h> 2462306a36Sopenharmony_ci#include <linux/mmu_notifier.h> 2562306a36Sopenharmony_ci#include <linux/uaccess.h> 2662306a36Sopenharmony_ci#include <linux/userfaultfd_k.h> 2762306a36Sopenharmony_ci#include <linux/mempolicy.h> 2862306a36Sopenharmony_ci 2962306a36Sopenharmony_ci#include <asm/cacheflush.h> 3062306a36Sopenharmony_ci#include <asm/tlb.h> 3162306a36Sopenharmony_ci#include <asm/pgalloc.h> 3262306a36Sopenharmony_ci 3362306a36Sopenharmony_ci#include "internal.h" 3462306a36Sopenharmony_ci 3562306a36Sopenharmony_cistatic pud_t *get_old_pud(struct mm_struct *mm, unsigned long addr) 3662306a36Sopenharmony_ci{ 3762306a36Sopenharmony_ci pgd_t *pgd; 3862306a36Sopenharmony_ci p4d_t *p4d; 3962306a36Sopenharmony_ci pud_t *pud; 4062306a36Sopenharmony_ci 4162306a36Sopenharmony_ci pgd = pgd_offset(mm, addr); 4262306a36Sopenharmony_ci if (pgd_none_or_clear_bad(pgd)) 4362306a36Sopenharmony_ci return NULL; 4462306a36Sopenharmony_ci 4562306a36Sopenharmony_ci p4d = p4d_offset(pgd, addr); 4662306a36Sopenharmony_ci if (p4d_none_or_clear_bad(p4d)) 4762306a36Sopenharmony_ci return NULL; 4862306a36Sopenharmony_ci 4962306a36Sopenharmony_ci pud = pud_offset(p4d, addr); 5062306a36Sopenharmony_ci if (pud_none_or_clear_bad(pud)) 5162306a36Sopenharmony_ci return NULL; 5262306a36Sopenharmony_ci 5362306a36Sopenharmony_ci return pud; 5462306a36Sopenharmony_ci} 5562306a36Sopenharmony_ci 5662306a36Sopenharmony_cistatic pmd_t *get_old_pmd(struct mm_struct *mm, unsigned long addr) 5762306a36Sopenharmony_ci{ 5862306a36Sopenharmony_ci pud_t *pud; 5962306a36Sopenharmony_ci pmd_t *pmd; 6062306a36Sopenharmony_ci 6162306a36Sopenharmony_ci pud = get_old_pud(mm, addr); 6262306a36Sopenharmony_ci if (!pud) 6362306a36Sopenharmony_ci return NULL; 6462306a36Sopenharmony_ci 6562306a36Sopenharmony_ci pmd = pmd_offset(pud, addr); 6662306a36Sopenharmony_ci if (pmd_none(*pmd)) 6762306a36Sopenharmony_ci return NULL; 6862306a36Sopenharmony_ci 6962306a36Sopenharmony_ci return pmd; 7062306a36Sopenharmony_ci} 7162306a36Sopenharmony_ci 7262306a36Sopenharmony_cistatic pud_t *alloc_new_pud(struct mm_struct *mm, struct vm_area_struct *vma, 7362306a36Sopenharmony_ci unsigned long addr) 7462306a36Sopenharmony_ci{ 7562306a36Sopenharmony_ci pgd_t *pgd; 7662306a36Sopenharmony_ci p4d_t *p4d; 7762306a36Sopenharmony_ci 7862306a36Sopenharmony_ci pgd = pgd_offset(mm, addr); 7962306a36Sopenharmony_ci p4d = p4d_alloc(mm, pgd, addr); 8062306a36Sopenharmony_ci if (!p4d) 8162306a36Sopenharmony_ci return NULL; 8262306a36Sopenharmony_ci 8362306a36Sopenharmony_ci return pud_alloc(mm, p4d, addr); 8462306a36Sopenharmony_ci} 8562306a36Sopenharmony_ci 8662306a36Sopenharmony_cistatic pmd_t *alloc_new_pmd(struct mm_struct *mm, struct vm_area_struct *vma, 8762306a36Sopenharmony_ci unsigned long addr) 8862306a36Sopenharmony_ci{ 8962306a36Sopenharmony_ci pud_t *pud; 9062306a36Sopenharmony_ci pmd_t *pmd; 9162306a36Sopenharmony_ci 9262306a36Sopenharmony_ci pud = alloc_new_pud(mm, vma, addr); 9362306a36Sopenharmony_ci if (!pud) 9462306a36Sopenharmony_ci return NULL; 9562306a36Sopenharmony_ci 9662306a36Sopenharmony_ci pmd = pmd_alloc(mm, pud, addr); 9762306a36Sopenharmony_ci if (!pmd) 9862306a36Sopenharmony_ci return NULL; 9962306a36Sopenharmony_ci 10062306a36Sopenharmony_ci VM_BUG_ON(pmd_trans_huge(*pmd)); 10162306a36Sopenharmony_ci 10262306a36Sopenharmony_ci return pmd; 10362306a36Sopenharmony_ci} 10462306a36Sopenharmony_ci 10562306a36Sopenharmony_cistatic void take_rmap_locks(struct vm_area_struct *vma) 10662306a36Sopenharmony_ci{ 10762306a36Sopenharmony_ci if (vma->vm_file) 10862306a36Sopenharmony_ci i_mmap_lock_write(vma->vm_file->f_mapping); 10962306a36Sopenharmony_ci if (vma->anon_vma) 11062306a36Sopenharmony_ci anon_vma_lock_write(vma->anon_vma); 11162306a36Sopenharmony_ci} 11262306a36Sopenharmony_ci 11362306a36Sopenharmony_cistatic void drop_rmap_locks(struct vm_area_struct *vma) 11462306a36Sopenharmony_ci{ 11562306a36Sopenharmony_ci if (vma->anon_vma) 11662306a36Sopenharmony_ci anon_vma_unlock_write(vma->anon_vma); 11762306a36Sopenharmony_ci if (vma->vm_file) 11862306a36Sopenharmony_ci i_mmap_unlock_write(vma->vm_file->f_mapping); 11962306a36Sopenharmony_ci} 12062306a36Sopenharmony_ci 12162306a36Sopenharmony_cistatic pte_t move_soft_dirty_pte(pte_t pte) 12262306a36Sopenharmony_ci{ 12362306a36Sopenharmony_ci /* 12462306a36Sopenharmony_ci * Set soft dirty bit so we can notice 12562306a36Sopenharmony_ci * in userspace the ptes were moved. 12662306a36Sopenharmony_ci */ 12762306a36Sopenharmony_ci#ifdef CONFIG_MEM_SOFT_DIRTY 12862306a36Sopenharmony_ci if (pte_present(pte)) 12962306a36Sopenharmony_ci pte = pte_mksoft_dirty(pte); 13062306a36Sopenharmony_ci else if (is_swap_pte(pte)) 13162306a36Sopenharmony_ci pte = pte_swp_mksoft_dirty(pte); 13262306a36Sopenharmony_ci#endif 13362306a36Sopenharmony_ci return pte; 13462306a36Sopenharmony_ci} 13562306a36Sopenharmony_ci 13662306a36Sopenharmony_cistatic int move_ptes(struct vm_area_struct *vma, pmd_t *old_pmd, 13762306a36Sopenharmony_ci unsigned long old_addr, unsigned long old_end, 13862306a36Sopenharmony_ci struct vm_area_struct *new_vma, pmd_t *new_pmd, 13962306a36Sopenharmony_ci unsigned long new_addr, bool need_rmap_locks) 14062306a36Sopenharmony_ci{ 14162306a36Sopenharmony_ci struct mm_struct *mm = vma->vm_mm; 14262306a36Sopenharmony_ci pte_t *old_pte, *new_pte, pte; 14362306a36Sopenharmony_ci spinlock_t *old_ptl, *new_ptl; 14462306a36Sopenharmony_ci bool force_flush = false; 14562306a36Sopenharmony_ci unsigned long len = old_end - old_addr; 14662306a36Sopenharmony_ci int err = 0; 14762306a36Sopenharmony_ci 14862306a36Sopenharmony_ci /* 14962306a36Sopenharmony_ci * When need_rmap_locks is true, we take the i_mmap_rwsem and anon_vma 15062306a36Sopenharmony_ci * locks to ensure that rmap will always observe either the old or the 15162306a36Sopenharmony_ci * new ptes. This is the easiest way to avoid races with 15262306a36Sopenharmony_ci * truncate_pagecache(), page migration, etc... 15362306a36Sopenharmony_ci * 15462306a36Sopenharmony_ci * When need_rmap_locks is false, we use other ways to avoid 15562306a36Sopenharmony_ci * such races: 15662306a36Sopenharmony_ci * 15762306a36Sopenharmony_ci * - During exec() shift_arg_pages(), we use a specially tagged vma 15862306a36Sopenharmony_ci * which rmap call sites look for using vma_is_temporary_stack(). 15962306a36Sopenharmony_ci * 16062306a36Sopenharmony_ci * - During mremap(), new_vma is often known to be placed after vma 16162306a36Sopenharmony_ci * in rmap traversal order. This ensures rmap will always observe 16262306a36Sopenharmony_ci * either the old pte, or the new pte, or both (the page table locks 16362306a36Sopenharmony_ci * serialize access to individual ptes, but only rmap traversal 16462306a36Sopenharmony_ci * order guarantees that we won't miss both the old and new ptes). 16562306a36Sopenharmony_ci */ 16662306a36Sopenharmony_ci if (need_rmap_locks) 16762306a36Sopenharmony_ci take_rmap_locks(vma); 16862306a36Sopenharmony_ci 16962306a36Sopenharmony_ci /* 17062306a36Sopenharmony_ci * We don't have to worry about the ordering of src and dst 17162306a36Sopenharmony_ci * pte locks because exclusive mmap_lock prevents deadlock. 17262306a36Sopenharmony_ci */ 17362306a36Sopenharmony_ci old_pte = pte_offset_map_lock(mm, old_pmd, old_addr, &old_ptl); 17462306a36Sopenharmony_ci if (!old_pte) { 17562306a36Sopenharmony_ci err = -EAGAIN; 17662306a36Sopenharmony_ci goto out; 17762306a36Sopenharmony_ci } 17862306a36Sopenharmony_ci new_pte = pte_offset_map_nolock(mm, new_pmd, new_addr, &new_ptl); 17962306a36Sopenharmony_ci if (!new_pte) { 18062306a36Sopenharmony_ci pte_unmap_unlock(old_pte, old_ptl); 18162306a36Sopenharmony_ci err = -EAGAIN; 18262306a36Sopenharmony_ci goto out; 18362306a36Sopenharmony_ci } 18462306a36Sopenharmony_ci if (new_ptl != old_ptl) 18562306a36Sopenharmony_ci spin_lock_nested(new_ptl, SINGLE_DEPTH_NESTING); 18662306a36Sopenharmony_ci flush_tlb_batched_pending(vma->vm_mm); 18762306a36Sopenharmony_ci arch_enter_lazy_mmu_mode(); 18862306a36Sopenharmony_ci 18962306a36Sopenharmony_ci for (; old_addr < old_end; old_pte++, old_addr += PAGE_SIZE, 19062306a36Sopenharmony_ci new_pte++, new_addr += PAGE_SIZE) { 19162306a36Sopenharmony_ci if (pte_none(ptep_get(old_pte))) 19262306a36Sopenharmony_ci continue; 19362306a36Sopenharmony_ci 19462306a36Sopenharmony_ci pte = ptep_get_and_clear(mm, old_addr, old_pte); 19562306a36Sopenharmony_ci /* 19662306a36Sopenharmony_ci * If we are remapping a valid PTE, make sure 19762306a36Sopenharmony_ci * to flush TLB before we drop the PTL for the 19862306a36Sopenharmony_ci * PTE. 19962306a36Sopenharmony_ci * 20062306a36Sopenharmony_ci * NOTE! Both old and new PTL matter: the old one 20162306a36Sopenharmony_ci * for racing with page_mkclean(), the new one to 20262306a36Sopenharmony_ci * make sure the physical page stays valid until 20362306a36Sopenharmony_ci * the TLB entry for the old mapping has been 20462306a36Sopenharmony_ci * flushed. 20562306a36Sopenharmony_ci */ 20662306a36Sopenharmony_ci if (pte_present(pte)) 20762306a36Sopenharmony_ci force_flush = true; 20862306a36Sopenharmony_ci pte = move_pte(pte, new_vma->vm_page_prot, old_addr, new_addr); 20962306a36Sopenharmony_ci pte = move_soft_dirty_pte(pte); 21062306a36Sopenharmony_ci set_pte_at(mm, new_addr, new_pte, pte); 21162306a36Sopenharmony_ci } 21262306a36Sopenharmony_ci 21362306a36Sopenharmony_ci arch_leave_lazy_mmu_mode(); 21462306a36Sopenharmony_ci if (force_flush) 21562306a36Sopenharmony_ci flush_tlb_range(vma, old_end - len, old_end); 21662306a36Sopenharmony_ci if (new_ptl != old_ptl) 21762306a36Sopenharmony_ci spin_unlock(new_ptl); 21862306a36Sopenharmony_ci pte_unmap(new_pte - 1); 21962306a36Sopenharmony_ci pte_unmap_unlock(old_pte - 1, old_ptl); 22062306a36Sopenharmony_ciout: 22162306a36Sopenharmony_ci if (need_rmap_locks) 22262306a36Sopenharmony_ci drop_rmap_locks(vma); 22362306a36Sopenharmony_ci return err; 22462306a36Sopenharmony_ci} 22562306a36Sopenharmony_ci 22662306a36Sopenharmony_ci#ifndef arch_supports_page_table_move 22762306a36Sopenharmony_ci#define arch_supports_page_table_move arch_supports_page_table_move 22862306a36Sopenharmony_cistatic inline bool arch_supports_page_table_move(void) 22962306a36Sopenharmony_ci{ 23062306a36Sopenharmony_ci return IS_ENABLED(CONFIG_HAVE_MOVE_PMD) || 23162306a36Sopenharmony_ci IS_ENABLED(CONFIG_HAVE_MOVE_PUD); 23262306a36Sopenharmony_ci} 23362306a36Sopenharmony_ci#endif 23462306a36Sopenharmony_ci 23562306a36Sopenharmony_ci#ifdef CONFIG_HAVE_MOVE_PMD 23662306a36Sopenharmony_cistatic bool move_normal_pmd(struct vm_area_struct *vma, unsigned long old_addr, 23762306a36Sopenharmony_ci unsigned long new_addr, pmd_t *old_pmd, pmd_t *new_pmd) 23862306a36Sopenharmony_ci{ 23962306a36Sopenharmony_ci spinlock_t *old_ptl, *new_ptl; 24062306a36Sopenharmony_ci struct mm_struct *mm = vma->vm_mm; 24162306a36Sopenharmony_ci pmd_t pmd; 24262306a36Sopenharmony_ci 24362306a36Sopenharmony_ci if (!arch_supports_page_table_move()) 24462306a36Sopenharmony_ci return false; 24562306a36Sopenharmony_ci /* 24662306a36Sopenharmony_ci * The destination pmd shouldn't be established, free_pgtables() 24762306a36Sopenharmony_ci * should have released it. 24862306a36Sopenharmony_ci * 24962306a36Sopenharmony_ci * However, there's a case during execve() where we use mremap 25062306a36Sopenharmony_ci * to move the initial stack, and in that case the target area 25162306a36Sopenharmony_ci * may overlap the source area (always moving down). 25262306a36Sopenharmony_ci * 25362306a36Sopenharmony_ci * If everything is PMD-aligned, that works fine, as moving 25462306a36Sopenharmony_ci * each pmd down will clear the source pmd. But if we first 25562306a36Sopenharmony_ci * have a few 4kB-only pages that get moved down, and then 25662306a36Sopenharmony_ci * hit the "now the rest is PMD-aligned, let's do everything 25762306a36Sopenharmony_ci * one pmd at a time", we will still have the old (now empty 25862306a36Sopenharmony_ci * of any 4kB pages, but still there) PMD in the page table 25962306a36Sopenharmony_ci * tree. 26062306a36Sopenharmony_ci * 26162306a36Sopenharmony_ci * Warn on it once - because we really should try to figure 26262306a36Sopenharmony_ci * out how to do this better - but then say "I won't move 26362306a36Sopenharmony_ci * this pmd". 26462306a36Sopenharmony_ci * 26562306a36Sopenharmony_ci * One alternative might be to just unmap the target pmd at 26662306a36Sopenharmony_ci * this point, and verify that it really is empty. We'll see. 26762306a36Sopenharmony_ci */ 26862306a36Sopenharmony_ci if (WARN_ON_ONCE(!pmd_none(*new_pmd))) 26962306a36Sopenharmony_ci return false; 27062306a36Sopenharmony_ci 27162306a36Sopenharmony_ci /* 27262306a36Sopenharmony_ci * We don't have to worry about the ordering of src and dst 27362306a36Sopenharmony_ci * ptlocks because exclusive mmap_lock prevents deadlock. 27462306a36Sopenharmony_ci */ 27562306a36Sopenharmony_ci old_ptl = pmd_lock(vma->vm_mm, old_pmd); 27662306a36Sopenharmony_ci new_ptl = pmd_lockptr(mm, new_pmd); 27762306a36Sopenharmony_ci if (new_ptl != old_ptl) 27862306a36Sopenharmony_ci spin_lock_nested(new_ptl, SINGLE_DEPTH_NESTING); 27962306a36Sopenharmony_ci 28062306a36Sopenharmony_ci /* Clear the pmd */ 28162306a36Sopenharmony_ci pmd = *old_pmd; 28262306a36Sopenharmony_ci pmd_clear(old_pmd); 28362306a36Sopenharmony_ci 28462306a36Sopenharmony_ci VM_BUG_ON(!pmd_none(*new_pmd)); 28562306a36Sopenharmony_ci 28662306a36Sopenharmony_ci pmd_populate(mm, new_pmd, pmd_pgtable(pmd)); 28762306a36Sopenharmony_ci flush_tlb_range(vma, old_addr, old_addr + PMD_SIZE); 28862306a36Sopenharmony_ci if (new_ptl != old_ptl) 28962306a36Sopenharmony_ci spin_unlock(new_ptl); 29062306a36Sopenharmony_ci spin_unlock(old_ptl); 29162306a36Sopenharmony_ci 29262306a36Sopenharmony_ci return true; 29362306a36Sopenharmony_ci} 29462306a36Sopenharmony_ci#else 29562306a36Sopenharmony_cistatic inline bool move_normal_pmd(struct vm_area_struct *vma, 29662306a36Sopenharmony_ci unsigned long old_addr, unsigned long new_addr, pmd_t *old_pmd, 29762306a36Sopenharmony_ci pmd_t *new_pmd) 29862306a36Sopenharmony_ci{ 29962306a36Sopenharmony_ci return false; 30062306a36Sopenharmony_ci} 30162306a36Sopenharmony_ci#endif 30262306a36Sopenharmony_ci 30362306a36Sopenharmony_ci#if CONFIG_PGTABLE_LEVELS > 2 && defined(CONFIG_HAVE_MOVE_PUD) 30462306a36Sopenharmony_cistatic bool move_normal_pud(struct vm_area_struct *vma, unsigned long old_addr, 30562306a36Sopenharmony_ci unsigned long new_addr, pud_t *old_pud, pud_t *new_pud) 30662306a36Sopenharmony_ci{ 30762306a36Sopenharmony_ci spinlock_t *old_ptl, *new_ptl; 30862306a36Sopenharmony_ci struct mm_struct *mm = vma->vm_mm; 30962306a36Sopenharmony_ci pud_t pud; 31062306a36Sopenharmony_ci 31162306a36Sopenharmony_ci if (!arch_supports_page_table_move()) 31262306a36Sopenharmony_ci return false; 31362306a36Sopenharmony_ci /* 31462306a36Sopenharmony_ci * The destination pud shouldn't be established, free_pgtables() 31562306a36Sopenharmony_ci * should have released it. 31662306a36Sopenharmony_ci */ 31762306a36Sopenharmony_ci if (WARN_ON_ONCE(!pud_none(*new_pud))) 31862306a36Sopenharmony_ci return false; 31962306a36Sopenharmony_ci 32062306a36Sopenharmony_ci /* 32162306a36Sopenharmony_ci * We don't have to worry about the ordering of src and dst 32262306a36Sopenharmony_ci * ptlocks because exclusive mmap_lock prevents deadlock. 32362306a36Sopenharmony_ci */ 32462306a36Sopenharmony_ci old_ptl = pud_lock(vma->vm_mm, old_pud); 32562306a36Sopenharmony_ci new_ptl = pud_lockptr(mm, new_pud); 32662306a36Sopenharmony_ci if (new_ptl != old_ptl) 32762306a36Sopenharmony_ci spin_lock_nested(new_ptl, SINGLE_DEPTH_NESTING); 32862306a36Sopenharmony_ci 32962306a36Sopenharmony_ci /* Clear the pud */ 33062306a36Sopenharmony_ci pud = *old_pud; 33162306a36Sopenharmony_ci pud_clear(old_pud); 33262306a36Sopenharmony_ci 33362306a36Sopenharmony_ci VM_BUG_ON(!pud_none(*new_pud)); 33462306a36Sopenharmony_ci 33562306a36Sopenharmony_ci pud_populate(mm, new_pud, pud_pgtable(pud)); 33662306a36Sopenharmony_ci flush_tlb_range(vma, old_addr, old_addr + PUD_SIZE); 33762306a36Sopenharmony_ci if (new_ptl != old_ptl) 33862306a36Sopenharmony_ci spin_unlock(new_ptl); 33962306a36Sopenharmony_ci spin_unlock(old_ptl); 34062306a36Sopenharmony_ci 34162306a36Sopenharmony_ci return true; 34262306a36Sopenharmony_ci} 34362306a36Sopenharmony_ci#else 34462306a36Sopenharmony_cistatic inline bool move_normal_pud(struct vm_area_struct *vma, 34562306a36Sopenharmony_ci unsigned long old_addr, unsigned long new_addr, pud_t *old_pud, 34662306a36Sopenharmony_ci pud_t *new_pud) 34762306a36Sopenharmony_ci{ 34862306a36Sopenharmony_ci return false; 34962306a36Sopenharmony_ci} 35062306a36Sopenharmony_ci#endif 35162306a36Sopenharmony_ci 35262306a36Sopenharmony_ci#if defined(CONFIG_TRANSPARENT_HUGEPAGE) && defined(CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD) 35362306a36Sopenharmony_cistatic bool move_huge_pud(struct vm_area_struct *vma, unsigned long old_addr, 35462306a36Sopenharmony_ci unsigned long new_addr, pud_t *old_pud, pud_t *new_pud) 35562306a36Sopenharmony_ci{ 35662306a36Sopenharmony_ci spinlock_t *old_ptl, *new_ptl; 35762306a36Sopenharmony_ci struct mm_struct *mm = vma->vm_mm; 35862306a36Sopenharmony_ci pud_t pud; 35962306a36Sopenharmony_ci 36062306a36Sopenharmony_ci /* 36162306a36Sopenharmony_ci * The destination pud shouldn't be established, free_pgtables() 36262306a36Sopenharmony_ci * should have released it. 36362306a36Sopenharmony_ci */ 36462306a36Sopenharmony_ci if (WARN_ON_ONCE(!pud_none(*new_pud))) 36562306a36Sopenharmony_ci return false; 36662306a36Sopenharmony_ci 36762306a36Sopenharmony_ci /* 36862306a36Sopenharmony_ci * We don't have to worry about the ordering of src and dst 36962306a36Sopenharmony_ci * ptlocks because exclusive mmap_lock prevents deadlock. 37062306a36Sopenharmony_ci */ 37162306a36Sopenharmony_ci old_ptl = pud_lock(vma->vm_mm, old_pud); 37262306a36Sopenharmony_ci new_ptl = pud_lockptr(mm, new_pud); 37362306a36Sopenharmony_ci if (new_ptl != old_ptl) 37462306a36Sopenharmony_ci spin_lock_nested(new_ptl, SINGLE_DEPTH_NESTING); 37562306a36Sopenharmony_ci 37662306a36Sopenharmony_ci /* Clear the pud */ 37762306a36Sopenharmony_ci pud = *old_pud; 37862306a36Sopenharmony_ci pud_clear(old_pud); 37962306a36Sopenharmony_ci 38062306a36Sopenharmony_ci VM_BUG_ON(!pud_none(*new_pud)); 38162306a36Sopenharmony_ci 38262306a36Sopenharmony_ci /* Set the new pud */ 38362306a36Sopenharmony_ci /* mark soft_ditry when we add pud level soft dirty support */ 38462306a36Sopenharmony_ci set_pud_at(mm, new_addr, new_pud, pud); 38562306a36Sopenharmony_ci flush_pud_tlb_range(vma, old_addr, old_addr + HPAGE_PUD_SIZE); 38662306a36Sopenharmony_ci if (new_ptl != old_ptl) 38762306a36Sopenharmony_ci spin_unlock(new_ptl); 38862306a36Sopenharmony_ci spin_unlock(old_ptl); 38962306a36Sopenharmony_ci 39062306a36Sopenharmony_ci return true; 39162306a36Sopenharmony_ci} 39262306a36Sopenharmony_ci#else 39362306a36Sopenharmony_cistatic bool move_huge_pud(struct vm_area_struct *vma, unsigned long old_addr, 39462306a36Sopenharmony_ci unsigned long new_addr, pud_t *old_pud, pud_t *new_pud) 39562306a36Sopenharmony_ci{ 39662306a36Sopenharmony_ci WARN_ON_ONCE(1); 39762306a36Sopenharmony_ci return false; 39862306a36Sopenharmony_ci 39962306a36Sopenharmony_ci} 40062306a36Sopenharmony_ci#endif 40162306a36Sopenharmony_ci 40262306a36Sopenharmony_cienum pgt_entry { 40362306a36Sopenharmony_ci NORMAL_PMD, 40462306a36Sopenharmony_ci HPAGE_PMD, 40562306a36Sopenharmony_ci NORMAL_PUD, 40662306a36Sopenharmony_ci HPAGE_PUD, 40762306a36Sopenharmony_ci}; 40862306a36Sopenharmony_ci 40962306a36Sopenharmony_ci/* 41062306a36Sopenharmony_ci * Returns an extent of the corresponding size for the pgt_entry specified if 41162306a36Sopenharmony_ci * valid. Else returns a smaller extent bounded by the end of the source and 41262306a36Sopenharmony_ci * destination pgt_entry. 41362306a36Sopenharmony_ci */ 41462306a36Sopenharmony_cistatic __always_inline unsigned long get_extent(enum pgt_entry entry, 41562306a36Sopenharmony_ci unsigned long old_addr, unsigned long old_end, 41662306a36Sopenharmony_ci unsigned long new_addr) 41762306a36Sopenharmony_ci{ 41862306a36Sopenharmony_ci unsigned long next, extent, mask, size; 41962306a36Sopenharmony_ci 42062306a36Sopenharmony_ci switch (entry) { 42162306a36Sopenharmony_ci case HPAGE_PMD: 42262306a36Sopenharmony_ci case NORMAL_PMD: 42362306a36Sopenharmony_ci mask = PMD_MASK; 42462306a36Sopenharmony_ci size = PMD_SIZE; 42562306a36Sopenharmony_ci break; 42662306a36Sopenharmony_ci case HPAGE_PUD: 42762306a36Sopenharmony_ci case NORMAL_PUD: 42862306a36Sopenharmony_ci mask = PUD_MASK; 42962306a36Sopenharmony_ci size = PUD_SIZE; 43062306a36Sopenharmony_ci break; 43162306a36Sopenharmony_ci default: 43262306a36Sopenharmony_ci BUILD_BUG(); 43362306a36Sopenharmony_ci break; 43462306a36Sopenharmony_ci } 43562306a36Sopenharmony_ci 43662306a36Sopenharmony_ci next = (old_addr + size) & mask; 43762306a36Sopenharmony_ci /* even if next overflowed, extent below will be ok */ 43862306a36Sopenharmony_ci extent = next - old_addr; 43962306a36Sopenharmony_ci if (extent > old_end - old_addr) 44062306a36Sopenharmony_ci extent = old_end - old_addr; 44162306a36Sopenharmony_ci next = (new_addr + size) & mask; 44262306a36Sopenharmony_ci if (extent > next - new_addr) 44362306a36Sopenharmony_ci extent = next - new_addr; 44462306a36Sopenharmony_ci return extent; 44562306a36Sopenharmony_ci} 44662306a36Sopenharmony_ci 44762306a36Sopenharmony_ci/* 44862306a36Sopenharmony_ci * Attempts to speedup the move by moving entry at the level corresponding to 44962306a36Sopenharmony_ci * pgt_entry. Returns true if the move was successful, else false. 45062306a36Sopenharmony_ci */ 45162306a36Sopenharmony_cistatic bool move_pgt_entry(enum pgt_entry entry, struct vm_area_struct *vma, 45262306a36Sopenharmony_ci unsigned long old_addr, unsigned long new_addr, 45362306a36Sopenharmony_ci void *old_entry, void *new_entry, bool need_rmap_locks) 45462306a36Sopenharmony_ci{ 45562306a36Sopenharmony_ci bool moved = false; 45662306a36Sopenharmony_ci 45762306a36Sopenharmony_ci /* See comment in move_ptes() */ 45862306a36Sopenharmony_ci if (need_rmap_locks) 45962306a36Sopenharmony_ci take_rmap_locks(vma); 46062306a36Sopenharmony_ci 46162306a36Sopenharmony_ci switch (entry) { 46262306a36Sopenharmony_ci case NORMAL_PMD: 46362306a36Sopenharmony_ci moved = move_normal_pmd(vma, old_addr, new_addr, old_entry, 46462306a36Sopenharmony_ci new_entry); 46562306a36Sopenharmony_ci break; 46662306a36Sopenharmony_ci case NORMAL_PUD: 46762306a36Sopenharmony_ci moved = move_normal_pud(vma, old_addr, new_addr, old_entry, 46862306a36Sopenharmony_ci new_entry); 46962306a36Sopenharmony_ci break; 47062306a36Sopenharmony_ci case HPAGE_PMD: 47162306a36Sopenharmony_ci moved = IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE) && 47262306a36Sopenharmony_ci move_huge_pmd(vma, old_addr, new_addr, old_entry, 47362306a36Sopenharmony_ci new_entry); 47462306a36Sopenharmony_ci break; 47562306a36Sopenharmony_ci case HPAGE_PUD: 47662306a36Sopenharmony_ci moved = IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE) && 47762306a36Sopenharmony_ci move_huge_pud(vma, old_addr, new_addr, old_entry, 47862306a36Sopenharmony_ci new_entry); 47962306a36Sopenharmony_ci break; 48062306a36Sopenharmony_ci 48162306a36Sopenharmony_ci default: 48262306a36Sopenharmony_ci WARN_ON_ONCE(1); 48362306a36Sopenharmony_ci break; 48462306a36Sopenharmony_ci } 48562306a36Sopenharmony_ci 48662306a36Sopenharmony_ci if (need_rmap_locks) 48762306a36Sopenharmony_ci drop_rmap_locks(vma); 48862306a36Sopenharmony_ci 48962306a36Sopenharmony_ci return moved; 49062306a36Sopenharmony_ci} 49162306a36Sopenharmony_ci 49262306a36Sopenharmony_ciunsigned long move_page_tables(struct vm_area_struct *vma, 49362306a36Sopenharmony_ci unsigned long old_addr, struct vm_area_struct *new_vma, 49462306a36Sopenharmony_ci unsigned long new_addr, unsigned long len, 49562306a36Sopenharmony_ci bool need_rmap_locks) 49662306a36Sopenharmony_ci{ 49762306a36Sopenharmony_ci unsigned long extent, old_end; 49862306a36Sopenharmony_ci struct mmu_notifier_range range; 49962306a36Sopenharmony_ci pmd_t *old_pmd, *new_pmd; 50062306a36Sopenharmony_ci pud_t *old_pud, *new_pud; 50162306a36Sopenharmony_ci 50262306a36Sopenharmony_ci if (!len) 50362306a36Sopenharmony_ci return 0; 50462306a36Sopenharmony_ci 50562306a36Sopenharmony_ci old_end = old_addr + len; 50662306a36Sopenharmony_ci 50762306a36Sopenharmony_ci if (is_vm_hugetlb_page(vma)) 50862306a36Sopenharmony_ci return move_hugetlb_page_tables(vma, new_vma, old_addr, 50962306a36Sopenharmony_ci new_addr, len); 51062306a36Sopenharmony_ci 51162306a36Sopenharmony_ci flush_cache_range(vma, old_addr, old_end); 51262306a36Sopenharmony_ci mmu_notifier_range_init(&range, MMU_NOTIFY_UNMAP, 0, vma->vm_mm, 51362306a36Sopenharmony_ci old_addr, old_end); 51462306a36Sopenharmony_ci mmu_notifier_invalidate_range_start(&range); 51562306a36Sopenharmony_ci 51662306a36Sopenharmony_ci for (; old_addr < old_end; old_addr += extent, new_addr += extent) { 51762306a36Sopenharmony_ci cond_resched(); 51862306a36Sopenharmony_ci /* 51962306a36Sopenharmony_ci * If extent is PUD-sized try to speed up the move by moving at the 52062306a36Sopenharmony_ci * PUD level if possible. 52162306a36Sopenharmony_ci */ 52262306a36Sopenharmony_ci extent = get_extent(NORMAL_PUD, old_addr, old_end, new_addr); 52362306a36Sopenharmony_ci 52462306a36Sopenharmony_ci old_pud = get_old_pud(vma->vm_mm, old_addr); 52562306a36Sopenharmony_ci if (!old_pud) 52662306a36Sopenharmony_ci continue; 52762306a36Sopenharmony_ci new_pud = alloc_new_pud(vma->vm_mm, vma, new_addr); 52862306a36Sopenharmony_ci if (!new_pud) 52962306a36Sopenharmony_ci break; 53062306a36Sopenharmony_ci if (pud_trans_huge(*old_pud) || pud_devmap(*old_pud)) { 53162306a36Sopenharmony_ci if (extent == HPAGE_PUD_SIZE) { 53262306a36Sopenharmony_ci move_pgt_entry(HPAGE_PUD, vma, old_addr, new_addr, 53362306a36Sopenharmony_ci old_pud, new_pud, need_rmap_locks); 53462306a36Sopenharmony_ci /* We ignore and continue on error? */ 53562306a36Sopenharmony_ci continue; 53662306a36Sopenharmony_ci } 53762306a36Sopenharmony_ci } else if (IS_ENABLED(CONFIG_HAVE_MOVE_PUD) && extent == PUD_SIZE) { 53862306a36Sopenharmony_ci 53962306a36Sopenharmony_ci if (move_pgt_entry(NORMAL_PUD, vma, old_addr, new_addr, 54062306a36Sopenharmony_ci old_pud, new_pud, true)) 54162306a36Sopenharmony_ci continue; 54262306a36Sopenharmony_ci } 54362306a36Sopenharmony_ci 54462306a36Sopenharmony_ci extent = get_extent(NORMAL_PMD, old_addr, old_end, new_addr); 54562306a36Sopenharmony_ci old_pmd = get_old_pmd(vma->vm_mm, old_addr); 54662306a36Sopenharmony_ci if (!old_pmd) 54762306a36Sopenharmony_ci continue; 54862306a36Sopenharmony_ci new_pmd = alloc_new_pmd(vma->vm_mm, vma, new_addr); 54962306a36Sopenharmony_ci if (!new_pmd) 55062306a36Sopenharmony_ci break; 55162306a36Sopenharmony_ciagain: 55262306a36Sopenharmony_ci if (is_swap_pmd(*old_pmd) || pmd_trans_huge(*old_pmd) || 55362306a36Sopenharmony_ci pmd_devmap(*old_pmd)) { 55462306a36Sopenharmony_ci if (extent == HPAGE_PMD_SIZE && 55562306a36Sopenharmony_ci move_pgt_entry(HPAGE_PMD, vma, old_addr, new_addr, 55662306a36Sopenharmony_ci old_pmd, new_pmd, need_rmap_locks)) 55762306a36Sopenharmony_ci continue; 55862306a36Sopenharmony_ci split_huge_pmd(vma, old_pmd, old_addr); 55962306a36Sopenharmony_ci } else if (IS_ENABLED(CONFIG_HAVE_MOVE_PMD) && 56062306a36Sopenharmony_ci extent == PMD_SIZE) { 56162306a36Sopenharmony_ci /* 56262306a36Sopenharmony_ci * If the extent is PMD-sized, try to speed the move by 56362306a36Sopenharmony_ci * moving at the PMD level if possible. 56462306a36Sopenharmony_ci */ 56562306a36Sopenharmony_ci if (move_pgt_entry(NORMAL_PMD, vma, old_addr, new_addr, 56662306a36Sopenharmony_ci old_pmd, new_pmd, true)) 56762306a36Sopenharmony_ci continue; 56862306a36Sopenharmony_ci } 56962306a36Sopenharmony_ci if (pmd_none(*old_pmd)) 57062306a36Sopenharmony_ci continue; 57162306a36Sopenharmony_ci if (pte_alloc(new_vma->vm_mm, new_pmd)) 57262306a36Sopenharmony_ci break; 57362306a36Sopenharmony_ci if (move_ptes(vma, old_pmd, old_addr, old_addr + extent, 57462306a36Sopenharmony_ci new_vma, new_pmd, new_addr, need_rmap_locks) < 0) 57562306a36Sopenharmony_ci goto again; 57662306a36Sopenharmony_ci } 57762306a36Sopenharmony_ci 57862306a36Sopenharmony_ci mmu_notifier_invalidate_range_end(&range); 57962306a36Sopenharmony_ci 58062306a36Sopenharmony_ci return len + old_addr - old_end; /* how much done */ 58162306a36Sopenharmony_ci} 58262306a36Sopenharmony_ci 58362306a36Sopenharmony_cistatic unsigned long move_vma(struct vm_area_struct *vma, 58462306a36Sopenharmony_ci unsigned long old_addr, unsigned long old_len, 58562306a36Sopenharmony_ci unsigned long new_len, unsigned long new_addr, 58662306a36Sopenharmony_ci bool *locked, unsigned long flags, 58762306a36Sopenharmony_ci struct vm_userfaultfd_ctx *uf, struct list_head *uf_unmap) 58862306a36Sopenharmony_ci{ 58962306a36Sopenharmony_ci long to_account = new_len - old_len; 59062306a36Sopenharmony_ci struct mm_struct *mm = vma->vm_mm; 59162306a36Sopenharmony_ci struct vm_area_struct *new_vma; 59262306a36Sopenharmony_ci unsigned long vm_flags = vma->vm_flags; 59362306a36Sopenharmony_ci unsigned long new_pgoff; 59462306a36Sopenharmony_ci unsigned long moved_len; 59562306a36Sopenharmony_ci unsigned long account_start = 0; 59662306a36Sopenharmony_ci unsigned long account_end = 0; 59762306a36Sopenharmony_ci unsigned long hiwater_vm; 59862306a36Sopenharmony_ci int err = 0; 59962306a36Sopenharmony_ci bool need_rmap_locks; 60062306a36Sopenharmony_ci struct vma_iterator vmi; 60162306a36Sopenharmony_ci 60262306a36Sopenharmony_ci /* 60362306a36Sopenharmony_ci * We'd prefer to avoid failure later on in do_munmap: 60462306a36Sopenharmony_ci * which may split one vma into three before unmapping. 60562306a36Sopenharmony_ci */ 60662306a36Sopenharmony_ci if (mm->map_count >= sysctl_max_map_count - 3) 60762306a36Sopenharmony_ci return -ENOMEM; 60862306a36Sopenharmony_ci 60962306a36Sopenharmony_ci if (unlikely(flags & MREMAP_DONTUNMAP)) 61062306a36Sopenharmony_ci to_account = new_len; 61162306a36Sopenharmony_ci 61262306a36Sopenharmony_ci if (vma->vm_ops && vma->vm_ops->may_split) { 61362306a36Sopenharmony_ci if (vma->vm_start != old_addr) 61462306a36Sopenharmony_ci err = vma->vm_ops->may_split(vma, old_addr); 61562306a36Sopenharmony_ci if (!err && vma->vm_end != old_addr + old_len) 61662306a36Sopenharmony_ci err = vma->vm_ops->may_split(vma, old_addr + old_len); 61762306a36Sopenharmony_ci if (err) 61862306a36Sopenharmony_ci return err; 61962306a36Sopenharmony_ci } 62062306a36Sopenharmony_ci 62162306a36Sopenharmony_ci /* 62262306a36Sopenharmony_ci * Advise KSM to break any KSM pages in the area to be moved: 62362306a36Sopenharmony_ci * it would be confusing if they were to turn up at the new 62462306a36Sopenharmony_ci * location, where they happen to coincide with different KSM 62562306a36Sopenharmony_ci * pages recently unmapped. But leave vma->vm_flags as it was, 62662306a36Sopenharmony_ci * so KSM can come around to merge on vma and new_vma afterwards. 62762306a36Sopenharmony_ci */ 62862306a36Sopenharmony_ci err = ksm_madvise(vma, old_addr, old_addr + old_len, 62962306a36Sopenharmony_ci MADV_UNMERGEABLE, &vm_flags); 63062306a36Sopenharmony_ci if (err) 63162306a36Sopenharmony_ci return err; 63262306a36Sopenharmony_ci 63362306a36Sopenharmony_ci if (vm_flags & VM_ACCOUNT) { 63462306a36Sopenharmony_ci if (security_vm_enough_memory_mm(mm, to_account >> PAGE_SHIFT)) 63562306a36Sopenharmony_ci return -ENOMEM; 63662306a36Sopenharmony_ci } 63762306a36Sopenharmony_ci 63862306a36Sopenharmony_ci vma_start_write(vma); 63962306a36Sopenharmony_ci new_pgoff = vma->vm_pgoff + ((old_addr - vma->vm_start) >> PAGE_SHIFT); 64062306a36Sopenharmony_ci new_vma = copy_vma(&vma, new_addr, new_len, new_pgoff, 64162306a36Sopenharmony_ci &need_rmap_locks); 64262306a36Sopenharmony_ci if (!new_vma) { 64362306a36Sopenharmony_ci if (vm_flags & VM_ACCOUNT) 64462306a36Sopenharmony_ci vm_unacct_memory(to_account >> PAGE_SHIFT); 64562306a36Sopenharmony_ci return -ENOMEM; 64662306a36Sopenharmony_ci } 64762306a36Sopenharmony_ci 64862306a36Sopenharmony_ci moved_len = move_page_tables(vma, old_addr, new_vma, new_addr, old_len, 64962306a36Sopenharmony_ci need_rmap_locks); 65062306a36Sopenharmony_ci if (moved_len < old_len) { 65162306a36Sopenharmony_ci err = -ENOMEM; 65262306a36Sopenharmony_ci } else if (vma->vm_ops && vma->vm_ops->mremap) { 65362306a36Sopenharmony_ci err = vma->vm_ops->mremap(new_vma); 65462306a36Sopenharmony_ci } 65562306a36Sopenharmony_ci 65662306a36Sopenharmony_ci if (unlikely(err)) { 65762306a36Sopenharmony_ci /* 65862306a36Sopenharmony_ci * On error, move entries back from new area to old, 65962306a36Sopenharmony_ci * which will succeed since page tables still there, 66062306a36Sopenharmony_ci * and then proceed to unmap new area instead of old. 66162306a36Sopenharmony_ci */ 66262306a36Sopenharmony_ci move_page_tables(new_vma, new_addr, vma, old_addr, moved_len, 66362306a36Sopenharmony_ci true); 66462306a36Sopenharmony_ci vma = new_vma; 66562306a36Sopenharmony_ci old_len = new_len; 66662306a36Sopenharmony_ci old_addr = new_addr; 66762306a36Sopenharmony_ci new_addr = err; 66862306a36Sopenharmony_ci } else { 66962306a36Sopenharmony_ci mremap_userfaultfd_prep(new_vma, uf); 67062306a36Sopenharmony_ci } 67162306a36Sopenharmony_ci 67262306a36Sopenharmony_ci if (is_vm_hugetlb_page(vma)) { 67362306a36Sopenharmony_ci clear_vma_resv_huge_pages(vma); 67462306a36Sopenharmony_ci } 67562306a36Sopenharmony_ci 67662306a36Sopenharmony_ci /* Conceal VM_ACCOUNT so old reservation is not undone */ 67762306a36Sopenharmony_ci if (vm_flags & VM_ACCOUNT && !(flags & MREMAP_DONTUNMAP)) { 67862306a36Sopenharmony_ci vm_flags_clear(vma, VM_ACCOUNT); 67962306a36Sopenharmony_ci if (vma->vm_start < old_addr) 68062306a36Sopenharmony_ci account_start = vma->vm_start; 68162306a36Sopenharmony_ci if (vma->vm_end > old_addr + old_len) 68262306a36Sopenharmony_ci account_end = vma->vm_end; 68362306a36Sopenharmony_ci } 68462306a36Sopenharmony_ci 68562306a36Sopenharmony_ci /* 68662306a36Sopenharmony_ci * If we failed to move page tables we still do total_vm increment 68762306a36Sopenharmony_ci * since do_munmap() will decrement it by old_len == new_len. 68862306a36Sopenharmony_ci * 68962306a36Sopenharmony_ci * Since total_vm is about to be raised artificially high for a 69062306a36Sopenharmony_ci * moment, we need to restore high watermark afterwards: if stats 69162306a36Sopenharmony_ci * are taken meanwhile, total_vm and hiwater_vm appear too high. 69262306a36Sopenharmony_ci * If this were a serious issue, we'd add a flag to do_munmap(). 69362306a36Sopenharmony_ci */ 69462306a36Sopenharmony_ci hiwater_vm = mm->hiwater_vm; 69562306a36Sopenharmony_ci vm_stat_account(mm, vma->vm_flags, new_len >> PAGE_SHIFT); 69662306a36Sopenharmony_ci 69762306a36Sopenharmony_ci /* Tell pfnmap has moved from this vma */ 69862306a36Sopenharmony_ci if (unlikely(vma->vm_flags & VM_PFNMAP)) 69962306a36Sopenharmony_ci untrack_pfn_clear(vma); 70062306a36Sopenharmony_ci 70162306a36Sopenharmony_ci if (unlikely(!err && (flags & MREMAP_DONTUNMAP))) { 70262306a36Sopenharmony_ci /* We always clear VM_LOCKED[ONFAULT] on the old vma */ 70362306a36Sopenharmony_ci vm_flags_clear(vma, VM_LOCKED_MASK); 70462306a36Sopenharmony_ci 70562306a36Sopenharmony_ci /* 70662306a36Sopenharmony_ci * anon_vma links of the old vma is no longer needed after its page 70762306a36Sopenharmony_ci * table has been moved. 70862306a36Sopenharmony_ci */ 70962306a36Sopenharmony_ci if (new_vma != vma && vma->vm_start == old_addr && 71062306a36Sopenharmony_ci vma->vm_end == (old_addr + old_len)) 71162306a36Sopenharmony_ci unlink_anon_vmas(vma); 71262306a36Sopenharmony_ci 71362306a36Sopenharmony_ci /* Because we won't unmap we don't need to touch locked_vm */ 71462306a36Sopenharmony_ci return new_addr; 71562306a36Sopenharmony_ci } 71662306a36Sopenharmony_ci 71762306a36Sopenharmony_ci vma_iter_init(&vmi, mm, old_addr); 71862306a36Sopenharmony_ci if (do_vmi_munmap(&vmi, mm, old_addr, old_len, uf_unmap, false) < 0) { 71962306a36Sopenharmony_ci /* OOM: unable to split vma, just get accounts right */ 72062306a36Sopenharmony_ci if (vm_flags & VM_ACCOUNT && !(flags & MREMAP_DONTUNMAP)) 72162306a36Sopenharmony_ci vm_acct_memory(old_len >> PAGE_SHIFT); 72262306a36Sopenharmony_ci account_start = account_end = 0; 72362306a36Sopenharmony_ci } 72462306a36Sopenharmony_ci 72562306a36Sopenharmony_ci if (vm_flags & VM_LOCKED) { 72662306a36Sopenharmony_ci mm->locked_vm += new_len >> PAGE_SHIFT; 72762306a36Sopenharmony_ci *locked = true; 72862306a36Sopenharmony_ci } 72962306a36Sopenharmony_ci 73062306a36Sopenharmony_ci mm->hiwater_vm = hiwater_vm; 73162306a36Sopenharmony_ci 73262306a36Sopenharmony_ci /* Restore VM_ACCOUNT if one or two pieces of vma left */ 73362306a36Sopenharmony_ci if (account_start) { 73462306a36Sopenharmony_ci vma = vma_prev(&vmi); 73562306a36Sopenharmony_ci vm_flags_set(vma, VM_ACCOUNT); 73662306a36Sopenharmony_ci } 73762306a36Sopenharmony_ci 73862306a36Sopenharmony_ci if (account_end) { 73962306a36Sopenharmony_ci vma = vma_next(&vmi); 74062306a36Sopenharmony_ci vm_flags_set(vma, VM_ACCOUNT); 74162306a36Sopenharmony_ci } 74262306a36Sopenharmony_ci 74362306a36Sopenharmony_ci return new_addr; 74462306a36Sopenharmony_ci} 74562306a36Sopenharmony_ci 74662306a36Sopenharmony_cistatic struct vm_area_struct *vma_to_resize(unsigned long addr, 74762306a36Sopenharmony_ci unsigned long old_len, unsigned long new_len, unsigned long flags) 74862306a36Sopenharmony_ci{ 74962306a36Sopenharmony_ci struct mm_struct *mm = current->mm; 75062306a36Sopenharmony_ci struct vm_area_struct *vma; 75162306a36Sopenharmony_ci unsigned long pgoff; 75262306a36Sopenharmony_ci 75362306a36Sopenharmony_ci vma = vma_lookup(mm, addr); 75462306a36Sopenharmony_ci if (!vma) 75562306a36Sopenharmony_ci return ERR_PTR(-EFAULT); 75662306a36Sopenharmony_ci 75762306a36Sopenharmony_ci /* 75862306a36Sopenharmony_ci * !old_len is a special case where an attempt is made to 'duplicate' 75962306a36Sopenharmony_ci * a mapping. This makes no sense for private mappings as it will 76062306a36Sopenharmony_ci * instead create a fresh/new mapping unrelated to the original. This 76162306a36Sopenharmony_ci * is contrary to the basic idea of mremap which creates new mappings 76262306a36Sopenharmony_ci * based on the original. There are no known use cases for this 76362306a36Sopenharmony_ci * behavior. As a result, fail such attempts. 76462306a36Sopenharmony_ci */ 76562306a36Sopenharmony_ci if (!old_len && !(vma->vm_flags & (VM_SHARED | VM_MAYSHARE))) { 76662306a36Sopenharmony_ci pr_warn_once("%s (%d): attempted to duplicate a private mapping with mremap. This is not supported.\n", current->comm, current->pid); 76762306a36Sopenharmony_ci return ERR_PTR(-EINVAL); 76862306a36Sopenharmony_ci } 76962306a36Sopenharmony_ci 77062306a36Sopenharmony_ci if ((flags & MREMAP_DONTUNMAP) && 77162306a36Sopenharmony_ci (vma->vm_flags & (VM_DONTEXPAND | VM_PFNMAP))) 77262306a36Sopenharmony_ci return ERR_PTR(-EINVAL); 77362306a36Sopenharmony_ci 77462306a36Sopenharmony_ci /* We can't remap across vm area boundaries */ 77562306a36Sopenharmony_ci if (old_len > vma->vm_end - addr) 77662306a36Sopenharmony_ci return ERR_PTR(-EFAULT); 77762306a36Sopenharmony_ci 77862306a36Sopenharmony_ci if (new_len == old_len) 77962306a36Sopenharmony_ci return vma; 78062306a36Sopenharmony_ci 78162306a36Sopenharmony_ci /* Need to be careful about a growing mapping */ 78262306a36Sopenharmony_ci pgoff = (addr - vma->vm_start) >> PAGE_SHIFT; 78362306a36Sopenharmony_ci pgoff += vma->vm_pgoff; 78462306a36Sopenharmony_ci if (pgoff + (new_len >> PAGE_SHIFT) < pgoff) 78562306a36Sopenharmony_ci return ERR_PTR(-EINVAL); 78662306a36Sopenharmony_ci 78762306a36Sopenharmony_ci if (vma->vm_flags & (VM_DONTEXPAND | VM_PFNMAP)) 78862306a36Sopenharmony_ci return ERR_PTR(-EFAULT); 78962306a36Sopenharmony_ci 79062306a36Sopenharmony_ci if (!mlock_future_ok(mm, vma->vm_flags, new_len - old_len)) 79162306a36Sopenharmony_ci return ERR_PTR(-EAGAIN); 79262306a36Sopenharmony_ci 79362306a36Sopenharmony_ci if (!may_expand_vm(mm, vma->vm_flags, 79462306a36Sopenharmony_ci (new_len - old_len) >> PAGE_SHIFT)) 79562306a36Sopenharmony_ci return ERR_PTR(-ENOMEM); 79662306a36Sopenharmony_ci 79762306a36Sopenharmony_ci return vma; 79862306a36Sopenharmony_ci} 79962306a36Sopenharmony_ci 80062306a36Sopenharmony_cistatic unsigned long mremap_to(unsigned long addr, unsigned long old_len, 80162306a36Sopenharmony_ci unsigned long new_addr, unsigned long new_len, bool *locked, 80262306a36Sopenharmony_ci unsigned long flags, struct vm_userfaultfd_ctx *uf, 80362306a36Sopenharmony_ci struct list_head *uf_unmap_early, 80462306a36Sopenharmony_ci struct list_head *uf_unmap) 80562306a36Sopenharmony_ci{ 80662306a36Sopenharmony_ci struct mm_struct *mm = current->mm; 80762306a36Sopenharmony_ci struct vm_area_struct *vma; 80862306a36Sopenharmony_ci unsigned long ret = -EINVAL; 80962306a36Sopenharmony_ci unsigned long map_flags = 0; 81062306a36Sopenharmony_ci 81162306a36Sopenharmony_ci if (offset_in_page(new_addr)) 81262306a36Sopenharmony_ci goto out; 81362306a36Sopenharmony_ci 81462306a36Sopenharmony_ci if (new_len > TASK_SIZE || new_addr > TASK_SIZE - new_len) 81562306a36Sopenharmony_ci goto out; 81662306a36Sopenharmony_ci 81762306a36Sopenharmony_ci /* Ensure the old/new locations do not overlap */ 81862306a36Sopenharmony_ci if (addr + old_len > new_addr && new_addr + new_len > addr) 81962306a36Sopenharmony_ci goto out; 82062306a36Sopenharmony_ci 82162306a36Sopenharmony_ci /* 82262306a36Sopenharmony_ci * move_vma() need us to stay 4 maps below the threshold, otherwise 82362306a36Sopenharmony_ci * it will bail out at the very beginning. 82462306a36Sopenharmony_ci * That is a problem if we have already unmaped the regions here 82562306a36Sopenharmony_ci * (new_addr, and old_addr), because userspace will not know the 82662306a36Sopenharmony_ci * state of the vma's after it gets -ENOMEM. 82762306a36Sopenharmony_ci * So, to avoid such scenario we can pre-compute if the whole 82862306a36Sopenharmony_ci * operation has high chances to success map-wise. 82962306a36Sopenharmony_ci * Worst-scenario case is when both vma's (new_addr and old_addr) get 83062306a36Sopenharmony_ci * split in 3 before unmapping it. 83162306a36Sopenharmony_ci * That means 2 more maps (1 for each) to the ones we already hold. 83262306a36Sopenharmony_ci * Check whether current map count plus 2 still leads us to 4 maps below 83362306a36Sopenharmony_ci * the threshold, otherwise return -ENOMEM here to be more safe. 83462306a36Sopenharmony_ci */ 83562306a36Sopenharmony_ci if ((mm->map_count + 2) >= sysctl_max_map_count - 3) 83662306a36Sopenharmony_ci return -ENOMEM; 83762306a36Sopenharmony_ci 83862306a36Sopenharmony_ci if (flags & MREMAP_FIXED) { 83962306a36Sopenharmony_ci ret = do_munmap(mm, new_addr, new_len, uf_unmap_early); 84062306a36Sopenharmony_ci if (ret) 84162306a36Sopenharmony_ci goto out; 84262306a36Sopenharmony_ci } 84362306a36Sopenharmony_ci 84462306a36Sopenharmony_ci if (old_len > new_len) { 84562306a36Sopenharmony_ci ret = do_munmap(mm, addr+new_len, old_len - new_len, uf_unmap); 84662306a36Sopenharmony_ci if (ret) 84762306a36Sopenharmony_ci goto out; 84862306a36Sopenharmony_ci old_len = new_len; 84962306a36Sopenharmony_ci } 85062306a36Sopenharmony_ci 85162306a36Sopenharmony_ci vma = vma_to_resize(addr, old_len, new_len, flags); 85262306a36Sopenharmony_ci if (IS_ERR(vma)) { 85362306a36Sopenharmony_ci ret = PTR_ERR(vma); 85462306a36Sopenharmony_ci goto out; 85562306a36Sopenharmony_ci } 85662306a36Sopenharmony_ci 85762306a36Sopenharmony_ci /* MREMAP_DONTUNMAP expands by old_len since old_len == new_len */ 85862306a36Sopenharmony_ci if (flags & MREMAP_DONTUNMAP && 85962306a36Sopenharmony_ci !may_expand_vm(mm, vma->vm_flags, old_len >> PAGE_SHIFT)) { 86062306a36Sopenharmony_ci ret = -ENOMEM; 86162306a36Sopenharmony_ci goto out; 86262306a36Sopenharmony_ci } 86362306a36Sopenharmony_ci 86462306a36Sopenharmony_ci if (flags & MREMAP_FIXED) 86562306a36Sopenharmony_ci map_flags |= MAP_FIXED; 86662306a36Sopenharmony_ci 86762306a36Sopenharmony_ci if (vma->vm_flags & VM_MAYSHARE) 86862306a36Sopenharmony_ci map_flags |= MAP_SHARED; 86962306a36Sopenharmony_ci 87062306a36Sopenharmony_ci ret = get_unmapped_area(vma->vm_file, new_addr, new_len, vma->vm_pgoff + 87162306a36Sopenharmony_ci ((addr - vma->vm_start) >> PAGE_SHIFT), 87262306a36Sopenharmony_ci map_flags); 87362306a36Sopenharmony_ci if (IS_ERR_VALUE(ret)) 87462306a36Sopenharmony_ci goto out; 87562306a36Sopenharmony_ci 87662306a36Sopenharmony_ci /* We got a new mapping */ 87762306a36Sopenharmony_ci if (!(flags & MREMAP_FIXED)) 87862306a36Sopenharmony_ci new_addr = ret; 87962306a36Sopenharmony_ci 88062306a36Sopenharmony_ci ret = move_vma(vma, addr, old_len, new_len, new_addr, locked, flags, uf, 88162306a36Sopenharmony_ci uf_unmap); 88262306a36Sopenharmony_ci 88362306a36Sopenharmony_ciout: 88462306a36Sopenharmony_ci return ret; 88562306a36Sopenharmony_ci} 88662306a36Sopenharmony_ci 88762306a36Sopenharmony_cistatic int vma_expandable(struct vm_area_struct *vma, unsigned long delta) 88862306a36Sopenharmony_ci{ 88962306a36Sopenharmony_ci unsigned long end = vma->vm_end + delta; 89062306a36Sopenharmony_ci 89162306a36Sopenharmony_ci if (end < vma->vm_end) /* overflow */ 89262306a36Sopenharmony_ci return 0; 89362306a36Sopenharmony_ci if (find_vma_intersection(vma->vm_mm, vma->vm_end, end)) 89462306a36Sopenharmony_ci return 0; 89562306a36Sopenharmony_ci if (get_unmapped_area(NULL, vma->vm_start, end - vma->vm_start, 89662306a36Sopenharmony_ci 0, MAP_FIXED) & ~PAGE_MASK) 89762306a36Sopenharmony_ci return 0; 89862306a36Sopenharmony_ci return 1; 89962306a36Sopenharmony_ci} 90062306a36Sopenharmony_ci 90162306a36Sopenharmony_ci/* 90262306a36Sopenharmony_ci * Expand (or shrink) an existing mapping, potentially moving it at the 90362306a36Sopenharmony_ci * same time (controlled by the MREMAP_MAYMOVE flag and available VM space) 90462306a36Sopenharmony_ci * 90562306a36Sopenharmony_ci * MREMAP_FIXED option added 5-Dec-1999 by Benjamin LaHaise 90662306a36Sopenharmony_ci * This option implies MREMAP_MAYMOVE. 90762306a36Sopenharmony_ci */ 90862306a36Sopenharmony_ciSYSCALL_DEFINE5(mremap, unsigned long, addr, unsigned long, old_len, 90962306a36Sopenharmony_ci unsigned long, new_len, unsigned long, flags, 91062306a36Sopenharmony_ci unsigned long, new_addr) 91162306a36Sopenharmony_ci{ 91262306a36Sopenharmony_ci struct mm_struct *mm = current->mm; 91362306a36Sopenharmony_ci struct vm_area_struct *vma; 91462306a36Sopenharmony_ci unsigned long ret = -EINVAL; 91562306a36Sopenharmony_ci bool locked = false; 91662306a36Sopenharmony_ci struct vm_userfaultfd_ctx uf = NULL_VM_UFFD_CTX; 91762306a36Sopenharmony_ci LIST_HEAD(uf_unmap_early); 91862306a36Sopenharmony_ci LIST_HEAD(uf_unmap); 91962306a36Sopenharmony_ci 92062306a36Sopenharmony_ci /* 92162306a36Sopenharmony_ci * There is a deliberate asymmetry here: we strip the pointer tag 92262306a36Sopenharmony_ci * from the old address but leave the new address alone. This is 92362306a36Sopenharmony_ci * for consistency with mmap(), where we prevent the creation of 92462306a36Sopenharmony_ci * aliasing mappings in userspace by leaving the tag bits of the 92562306a36Sopenharmony_ci * mapping address intact. A non-zero tag will cause the subsequent 92662306a36Sopenharmony_ci * range checks to reject the address as invalid. 92762306a36Sopenharmony_ci * 92862306a36Sopenharmony_ci * See Documentation/arch/arm64/tagged-address-abi.rst for more 92962306a36Sopenharmony_ci * information. 93062306a36Sopenharmony_ci */ 93162306a36Sopenharmony_ci addr = untagged_addr(addr); 93262306a36Sopenharmony_ci 93362306a36Sopenharmony_ci if (flags & ~(MREMAP_FIXED | MREMAP_MAYMOVE | MREMAP_DONTUNMAP)) 93462306a36Sopenharmony_ci return ret; 93562306a36Sopenharmony_ci 93662306a36Sopenharmony_ci if (flags & MREMAP_FIXED && !(flags & MREMAP_MAYMOVE)) 93762306a36Sopenharmony_ci return ret; 93862306a36Sopenharmony_ci 93962306a36Sopenharmony_ci /* 94062306a36Sopenharmony_ci * MREMAP_DONTUNMAP is always a move and it does not allow resizing 94162306a36Sopenharmony_ci * in the process. 94262306a36Sopenharmony_ci */ 94362306a36Sopenharmony_ci if (flags & MREMAP_DONTUNMAP && 94462306a36Sopenharmony_ci (!(flags & MREMAP_MAYMOVE) || old_len != new_len)) 94562306a36Sopenharmony_ci return ret; 94662306a36Sopenharmony_ci 94762306a36Sopenharmony_ci 94862306a36Sopenharmony_ci if (offset_in_page(addr)) 94962306a36Sopenharmony_ci return ret; 95062306a36Sopenharmony_ci 95162306a36Sopenharmony_ci old_len = PAGE_ALIGN(old_len); 95262306a36Sopenharmony_ci new_len = PAGE_ALIGN(new_len); 95362306a36Sopenharmony_ci 95462306a36Sopenharmony_ci /* 95562306a36Sopenharmony_ci * We allow a zero old-len as a special case 95662306a36Sopenharmony_ci * for DOS-emu "duplicate shm area" thing. But 95762306a36Sopenharmony_ci * a zero new-len is nonsensical. 95862306a36Sopenharmony_ci */ 95962306a36Sopenharmony_ci if (!new_len) 96062306a36Sopenharmony_ci return ret; 96162306a36Sopenharmony_ci 96262306a36Sopenharmony_ci if (mmap_write_lock_killable(current->mm)) 96362306a36Sopenharmony_ci return -EINTR; 96462306a36Sopenharmony_ci vma = vma_lookup(mm, addr); 96562306a36Sopenharmony_ci if (!vma) { 96662306a36Sopenharmony_ci ret = -EFAULT; 96762306a36Sopenharmony_ci goto out; 96862306a36Sopenharmony_ci } 96962306a36Sopenharmony_ci 97062306a36Sopenharmony_ci if (is_vm_hugetlb_page(vma)) { 97162306a36Sopenharmony_ci struct hstate *h __maybe_unused = hstate_vma(vma); 97262306a36Sopenharmony_ci 97362306a36Sopenharmony_ci old_len = ALIGN(old_len, huge_page_size(h)); 97462306a36Sopenharmony_ci new_len = ALIGN(new_len, huge_page_size(h)); 97562306a36Sopenharmony_ci 97662306a36Sopenharmony_ci /* addrs must be huge page aligned */ 97762306a36Sopenharmony_ci if (addr & ~huge_page_mask(h)) 97862306a36Sopenharmony_ci goto out; 97962306a36Sopenharmony_ci if (new_addr & ~huge_page_mask(h)) 98062306a36Sopenharmony_ci goto out; 98162306a36Sopenharmony_ci 98262306a36Sopenharmony_ci /* 98362306a36Sopenharmony_ci * Don't allow remap expansion, because the underlying hugetlb 98462306a36Sopenharmony_ci * reservation is not yet capable to handle split reservation. 98562306a36Sopenharmony_ci */ 98662306a36Sopenharmony_ci if (new_len > old_len) 98762306a36Sopenharmony_ci goto out; 98862306a36Sopenharmony_ci } 98962306a36Sopenharmony_ci 99062306a36Sopenharmony_ci if (flags & (MREMAP_FIXED | MREMAP_DONTUNMAP)) { 99162306a36Sopenharmony_ci ret = mremap_to(addr, old_len, new_addr, new_len, 99262306a36Sopenharmony_ci &locked, flags, &uf, &uf_unmap_early, 99362306a36Sopenharmony_ci &uf_unmap); 99462306a36Sopenharmony_ci goto out; 99562306a36Sopenharmony_ci } 99662306a36Sopenharmony_ci 99762306a36Sopenharmony_ci /* 99862306a36Sopenharmony_ci * Always allow a shrinking remap: that just unmaps 99962306a36Sopenharmony_ci * the unnecessary pages.. 100062306a36Sopenharmony_ci * do_vmi_munmap does all the needed commit accounting, and 100162306a36Sopenharmony_ci * unlocks the mmap_lock if so directed. 100262306a36Sopenharmony_ci */ 100362306a36Sopenharmony_ci if (old_len >= new_len) { 100462306a36Sopenharmony_ci VMA_ITERATOR(vmi, mm, addr + new_len); 100562306a36Sopenharmony_ci 100662306a36Sopenharmony_ci if (old_len == new_len) { 100762306a36Sopenharmony_ci ret = addr; 100862306a36Sopenharmony_ci goto out; 100962306a36Sopenharmony_ci } 101062306a36Sopenharmony_ci 101162306a36Sopenharmony_ci ret = do_vmi_munmap(&vmi, mm, addr + new_len, old_len - new_len, 101262306a36Sopenharmony_ci &uf_unmap, true); 101362306a36Sopenharmony_ci if (ret) 101462306a36Sopenharmony_ci goto out; 101562306a36Sopenharmony_ci 101662306a36Sopenharmony_ci ret = addr; 101762306a36Sopenharmony_ci goto out_unlocked; 101862306a36Sopenharmony_ci } 101962306a36Sopenharmony_ci 102062306a36Sopenharmony_ci /* 102162306a36Sopenharmony_ci * Ok, we need to grow.. 102262306a36Sopenharmony_ci */ 102362306a36Sopenharmony_ci vma = vma_to_resize(addr, old_len, new_len, flags); 102462306a36Sopenharmony_ci if (IS_ERR(vma)) { 102562306a36Sopenharmony_ci ret = PTR_ERR(vma); 102662306a36Sopenharmony_ci goto out; 102762306a36Sopenharmony_ci } 102862306a36Sopenharmony_ci 102962306a36Sopenharmony_ci /* old_len exactly to the end of the area.. 103062306a36Sopenharmony_ci */ 103162306a36Sopenharmony_ci if (old_len == vma->vm_end - addr) { 103262306a36Sopenharmony_ci /* can we just expand the current mapping? */ 103362306a36Sopenharmony_ci if (vma_expandable(vma, new_len - old_len)) { 103462306a36Sopenharmony_ci long pages = (new_len - old_len) >> PAGE_SHIFT; 103562306a36Sopenharmony_ci unsigned long extension_start = addr + old_len; 103662306a36Sopenharmony_ci unsigned long extension_end = addr + new_len; 103762306a36Sopenharmony_ci pgoff_t extension_pgoff = vma->vm_pgoff + 103862306a36Sopenharmony_ci ((extension_start - vma->vm_start) >> PAGE_SHIFT); 103962306a36Sopenharmony_ci VMA_ITERATOR(vmi, mm, extension_start); 104062306a36Sopenharmony_ci 104162306a36Sopenharmony_ci if (vma->vm_flags & VM_ACCOUNT) { 104262306a36Sopenharmony_ci if (security_vm_enough_memory_mm(mm, pages)) { 104362306a36Sopenharmony_ci ret = -ENOMEM; 104462306a36Sopenharmony_ci goto out; 104562306a36Sopenharmony_ci } 104662306a36Sopenharmony_ci } 104762306a36Sopenharmony_ci 104862306a36Sopenharmony_ci /* 104962306a36Sopenharmony_ci * Function vma_merge() is called on the extension we 105062306a36Sopenharmony_ci * are adding to the already existing vma, vma_merge() 105162306a36Sopenharmony_ci * will merge this extension with the already existing 105262306a36Sopenharmony_ci * vma (expand operation itself) and possibly also with 105362306a36Sopenharmony_ci * the next vma if it becomes adjacent to the expanded 105462306a36Sopenharmony_ci * vma and otherwise compatible. 105562306a36Sopenharmony_ci */ 105662306a36Sopenharmony_ci vma = vma_merge(&vmi, mm, vma, extension_start, 105762306a36Sopenharmony_ci extension_end, vma->vm_flags, vma->anon_vma, 105862306a36Sopenharmony_ci vma->vm_file, extension_pgoff, vma_policy(vma), 105962306a36Sopenharmony_ci vma->vm_userfaultfd_ctx, anon_vma_name(vma)); 106062306a36Sopenharmony_ci if (!vma) { 106162306a36Sopenharmony_ci vm_unacct_memory(pages); 106262306a36Sopenharmony_ci ret = -ENOMEM; 106362306a36Sopenharmony_ci goto out; 106462306a36Sopenharmony_ci } 106562306a36Sopenharmony_ci 106662306a36Sopenharmony_ci vm_stat_account(mm, vma->vm_flags, pages); 106762306a36Sopenharmony_ci if (vma->vm_flags & VM_LOCKED) { 106862306a36Sopenharmony_ci mm->locked_vm += pages; 106962306a36Sopenharmony_ci locked = true; 107062306a36Sopenharmony_ci new_addr = addr; 107162306a36Sopenharmony_ci } 107262306a36Sopenharmony_ci ret = addr; 107362306a36Sopenharmony_ci goto out; 107462306a36Sopenharmony_ci } 107562306a36Sopenharmony_ci } 107662306a36Sopenharmony_ci 107762306a36Sopenharmony_ci /* 107862306a36Sopenharmony_ci * We weren't able to just expand or shrink the area, 107962306a36Sopenharmony_ci * we need to create a new one and move it.. 108062306a36Sopenharmony_ci */ 108162306a36Sopenharmony_ci ret = -ENOMEM; 108262306a36Sopenharmony_ci if (flags & MREMAP_MAYMOVE) { 108362306a36Sopenharmony_ci unsigned long map_flags = 0; 108462306a36Sopenharmony_ci if (vma->vm_flags & VM_MAYSHARE) 108562306a36Sopenharmony_ci map_flags |= MAP_SHARED; 108662306a36Sopenharmony_ci 108762306a36Sopenharmony_ci new_addr = get_unmapped_area(vma->vm_file, 0, new_len, 108862306a36Sopenharmony_ci vma->vm_pgoff + 108962306a36Sopenharmony_ci ((addr - vma->vm_start) >> PAGE_SHIFT), 109062306a36Sopenharmony_ci map_flags); 109162306a36Sopenharmony_ci if (IS_ERR_VALUE(new_addr)) { 109262306a36Sopenharmony_ci ret = new_addr; 109362306a36Sopenharmony_ci goto out; 109462306a36Sopenharmony_ci } 109562306a36Sopenharmony_ci 109662306a36Sopenharmony_ci ret = move_vma(vma, addr, old_len, new_len, new_addr, 109762306a36Sopenharmony_ci &locked, flags, &uf, &uf_unmap); 109862306a36Sopenharmony_ci } 109962306a36Sopenharmony_ciout: 110062306a36Sopenharmony_ci if (offset_in_page(ret)) 110162306a36Sopenharmony_ci locked = false; 110262306a36Sopenharmony_ci mmap_write_unlock(current->mm); 110362306a36Sopenharmony_ci if (locked && new_len > old_len) 110462306a36Sopenharmony_ci mm_populate(new_addr + old_len, new_len - old_len); 110562306a36Sopenharmony_ciout_unlocked: 110662306a36Sopenharmony_ci userfaultfd_unmap_complete(mm, &uf_unmap_early); 110762306a36Sopenharmony_ci mremap_userfaultfd_complete(&uf, addr, ret, old_len); 110862306a36Sopenharmony_ci userfaultfd_unmap_complete(mm, &uf_unmap); 110962306a36Sopenharmony_ci return ret; 111062306a36Sopenharmony_ci} 1111