xref: /openbmc/linux/mm/mremap.c (revision 09138ba68c1487a42c400485e999386a74911dbc)
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>
12ca3d76b0SJakub Matěna #include <linux/mm_inline.h>
131da177e4SLinus Torvalds #include <linux/hugetlb.h>
141da177e4SLinus Torvalds #include <linux/shm.h>
151ff82995SHugh Dickins #include <linux/ksm.h>
161da177e4SLinus Torvalds #include <linux/mman.h>
171da177e4SLinus Torvalds #include <linux/swap.h>
18c59ede7bSRandy.Dunlap #include <linux/capability.h>
191da177e4SLinus Torvalds #include <linux/fs.h>
206dec97dcSCyrill Gorcunov #include <linux/swapops.h>
211da177e4SLinus Torvalds #include <linux/highmem.h>
221da177e4SLinus Torvalds #include <linux/security.h>
231da177e4SLinus Torvalds #include <linux/syscalls.h>
24cddb8a5cSAndrea Arcangeli #include <linux/mmu_notifier.h>
252581d202SPaul McQuade #include <linux/uaccess.h>
2672f87654SPavel Emelyanov #include <linux/userfaultfd_k.h>
27ca3d76b0SJakub Matěna #include <linux/mempolicy.h>
281da177e4SLinus Torvalds 
291da177e4SLinus Torvalds #include <asm/cacheflush.h>
303bbda69cSAneesh Kumar K.V #include <asm/tlb.h>
310881ace2SAneesh Kumar K.V #include <asm/pgalloc.h>
321da177e4SLinus Torvalds 
33ba470de4SRik van Riel #include "internal.h"
34ba470de4SRik van Riel 
get_old_pud(struct mm_struct * mm,unsigned long addr)35c49dd340SKalesh Singh static pud_t *get_old_pud(struct mm_struct *mm, unsigned long addr)
361da177e4SLinus Torvalds {
371da177e4SLinus Torvalds 	pgd_t *pgd;
38c2febafcSKirill A. Shutemov 	p4d_t *p4d;
391da177e4SLinus Torvalds 	pud_t *pud;
401da177e4SLinus Torvalds 
411da177e4SLinus Torvalds 	pgd = pgd_offset(mm, addr);
421da177e4SLinus Torvalds 	if (pgd_none_or_clear_bad(pgd))
431da177e4SLinus Torvalds 		return NULL;
441da177e4SLinus Torvalds 
45c2febafcSKirill A. Shutemov 	p4d = p4d_offset(pgd, addr);
46c2febafcSKirill A. Shutemov 	if (p4d_none_or_clear_bad(p4d))
47c2febafcSKirill A. Shutemov 		return NULL;
48c2febafcSKirill A. Shutemov 
49c2febafcSKirill A. Shutemov 	pud = pud_offset(p4d, addr);
501da177e4SLinus Torvalds 	if (pud_none_or_clear_bad(pud))
511da177e4SLinus Torvalds 		return NULL;
521da177e4SLinus Torvalds 
53c49dd340SKalesh Singh 	return pud;
54c49dd340SKalesh Singh }
55c49dd340SKalesh Singh 
get_old_pmd(struct mm_struct * mm,unsigned long addr)56c49dd340SKalesh Singh static pmd_t *get_old_pmd(struct mm_struct *mm, unsigned long addr)
57c49dd340SKalesh Singh {
58c49dd340SKalesh Singh 	pud_t *pud;
59c49dd340SKalesh Singh 	pmd_t *pmd;
60c49dd340SKalesh Singh 
61c49dd340SKalesh Singh 	pud = get_old_pud(mm, addr);
62c49dd340SKalesh Singh 	if (!pud)
63c49dd340SKalesh Singh 		return NULL;
64c49dd340SKalesh Singh 
651da177e4SLinus Torvalds 	pmd = pmd_offset(pud, addr);
6637a1c49aSAndrea Arcangeli 	if (pmd_none(*pmd))
671da177e4SLinus Torvalds 		return NULL;
681da177e4SLinus Torvalds 
697be7a546SHugh Dickins 	return pmd;
701da177e4SLinus Torvalds }
711da177e4SLinus Torvalds 
alloc_new_pud(struct mm_struct * mm,struct vm_area_struct * vma,unsigned long addr)72c49dd340SKalesh Singh static pud_t *alloc_new_pud(struct mm_struct *mm, struct vm_area_struct *vma,
738ac1f832SAndrea Arcangeli 			    unsigned long addr)
741da177e4SLinus Torvalds {
751da177e4SLinus Torvalds 	pgd_t *pgd;
76c2febafcSKirill A. Shutemov 	p4d_t *p4d;
771da177e4SLinus Torvalds 
781da177e4SLinus Torvalds 	pgd = pgd_offset(mm, addr);
79c2febafcSKirill A. Shutemov 	p4d = p4d_alloc(mm, pgd, addr);
80c2febafcSKirill A. Shutemov 	if (!p4d)
81c2febafcSKirill A. Shutemov 		return NULL;
82c49dd340SKalesh Singh 
83c49dd340SKalesh Singh 	return pud_alloc(mm, p4d, addr);
84c49dd340SKalesh Singh }
85c49dd340SKalesh Singh 
alloc_new_pmd(struct mm_struct * mm,struct vm_area_struct * vma,unsigned long addr)86c49dd340SKalesh Singh static pmd_t *alloc_new_pmd(struct mm_struct *mm, struct vm_area_struct *vma,
87c49dd340SKalesh Singh 			    unsigned long addr)
88c49dd340SKalesh Singh {
89c49dd340SKalesh Singh 	pud_t *pud;
90c49dd340SKalesh Singh 	pmd_t *pmd;
91c49dd340SKalesh Singh 
92c49dd340SKalesh Singh 	pud = alloc_new_pud(mm, vma, addr);
931da177e4SLinus Torvalds 	if (!pud)
94c74df32cSHugh Dickins 		return NULL;
957be7a546SHugh Dickins 
961da177e4SLinus Torvalds 	pmd = pmd_alloc(mm, pud, addr);
9757a8f0cdSHugh Dickins 	if (!pmd)
98c74df32cSHugh Dickins 		return NULL;
997be7a546SHugh Dickins 
1008ac1f832SAndrea Arcangeli 	VM_BUG_ON(pmd_trans_huge(*pmd));
101c74df32cSHugh Dickins 
1027be7a546SHugh Dickins 	return pmd;
1031da177e4SLinus Torvalds }
1041da177e4SLinus Torvalds 
take_rmap_locks(struct vm_area_struct * vma)1051d069b7dSHugh Dickins static void take_rmap_locks(struct vm_area_struct *vma)
1061d069b7dSHugh Dickins {
1071d069b7dSHugh Dickins 	if (vma->vm_file)
1081d069b7dSHugh Dickins 		i_mmap_lock_write(vma->vm_file->f_mapping);
1091d069b7dSHugh Dickins 	if (vma->anon_vma)
1101d069b7dSHugh Dickins 		anon_vma_lock_write(vma->anon_vma);
1111d069b7dSHugh Dickins }
1121d069b7dSHugh Dickins 
drop_rmap_locks(struct vm_area_struct * vma)1131d069b7dSHugh Dickins static void drop_rmap_locks(struct vm_area_struct *vma)
1141d069b7dSHugh Dickins {
1151d069b7dSHugh Dickins 	if (vma->anon_vma)
1161d069b7dSHugh Dickins 		anon_vma_unlock_write(vma->anon_vma);
1171d069b7dSHugh Dickins 	if (vma->vm_file)
1181d069b7dSHugh Dickins 		i_mmap_unlock_write(vma->vm_file->f_mapping);
1191d069b7dSHugh Dickins }
1201d069b7dSHugh Dickins 
move_soft_dirty_pte(pte_t pte)1216dec97dcSCyrill Gorcunov static pte_t move_soft_dirty_pte(pte_t pte)
1226dec97dcSCyrill Gorcunov {
1236dec97dcSCyrill Gorcunov 	/*
1246dec97dcSCyrill Gorcunov 	 * Set soft dirty bit so we can notice
1256dec97dcSCyrill Gorcunov 	 * in userspace the ptes were moved.
1266dec97dcSCyrill Gorcunov 	 */
1276dec97dcSCyrill Gorcunov #ifdef CONFIG_MEM_SOFT_DIRTY
1286dec97dcSCyrill Gorcunov 	if (pte_present(pte))
1296dec97dcSCyrill Gorcunov 		pte = pte_mksoft_dirty(pte);
1306dec97dcSCyrill Gorcunov 	else if (is_swap_pte(pte))
1316dec97dcSCyrill Gorcunov 		pte = pte_swp_mksoft_dirty(pte);
1326dec97dcSCyrill Gorcunov #endif
1336dec97dcSCyrill Gorcunov 	return pte;
1346dec97dcSCyrill Gorcunov }
1356dec97dcSCyrill Gorcunov 
move_ptes(struct vm_area_struct * vma,pmd_t * old_pmd,unsigned long old_addr,unsigned long old_end,struct vm_area_struct * new_vma,pmd_t * new_pmd,unsigned long new_addr,bool need_rmap_locks)136a5be621eSHugh Dickins static int move_ptes(struct vm_area_struct *vma, pmd_t *old_pmd,
1377be7a546SHugh Dickins 		unsigned long old_addr, unsigned long old_end,
1387be7a546SHugh Dickins 		struct vm_area_struct *new_vma, pmd_t *new_pmd,
139eb66ae03SLinus Torvalds 		unsigned long new_addr, bool need_rmap_locks)
1401da177e4SLinus Torvalds {
1411da177e4SLinus Torvalds 	struct mm_struct *mm = vma->vm_mm;
1427be7a546SHugh Dickins 	pte_t *old_pte, *new_pte, pte;
1434c21e2f2SHugh Dickins 	spinlock_t *old_ptl, *new_ptl;
1445d190420SAaron Lu 	bool force_flush = false;
1455d190420SAaron Lu 	unsigned long len = old_end - old_addr;
146a5be621eSHugh Dickins 	int err = 0;
1471da177e4SLinus Torvalds 
1481da177e4SLinus Torvalds 	/*
149c8c06efaSDavidlohr Bueso 	 * When need_rmap_locks is true, we take the i_mmap_rwsem and anon_vma
15038a76013SMichel Lespinasse 	 * locks to ensure that rmap will always observe either the old or the
15138a76013SMichel Lespinasse 	 * new ptes. This is the easiest way to avoid races with
15238a76013SMichel Lespinasse 	 * truncate_pagecache(), page migration, etc...
15338a76013SMichel Lespinasse 	 *
15438a76013SMichel Lespinasse 	 * When need_rmap_locks is false, we use other ways to avoid
15538a76013SMichel Lespinasse 	 * such races:
15638a76013SMichel Lespinasse 	 *
15738a76013SMichel Lespinasse 	 * - During exec() shift_arg_pages(), we use a specially tagged vma
158222100eeSAnshuman Khandual 	 *   which rmap call sites look for using vma_is_temporary_stack().
15938a76013SMichel Lespinasse 	 *
16038a76013SMichel Lespinasse 	 * - During mremap(), new_vma is often known to be placed after vma
16138a76013SMichel Lespinasse 	 *   in rmap traversal order. This ensures rmap will always observe
16238a76013SMichel Lespinasse 	 *   either the old pte, or the new pte, or both (the page table locks
16338a76013SMichel Lespinasse 	 *   serialize access to individual ptes, but only rmap traversal
16438a76013SMichel Lespinasse 	 *   order guarantees that we won't miss both the old and new ptes).
1651da177e4SLinus Torvalds 	 */
1661d069b7dSHugh Dickins 	if (need_rmap_locks)
1671d069b7dSHugh Dickins 		take_rmap_locks(vma);
1681da177e4SLinus Torvalds 
1694c21e2f2SHugh Dickins 	/*
1704c21e2f2SHugh Dickins 	 * We don't have to worry about the ordering of src and dst
171c1e8d7c6SMichel Lespinasse 	 * pte locks because exclusive mmap_lock prevents deadlock.
1724c21e2f2SHugh Dickins 	 */
173c74df32cSHugh Dickins 	old_pte = pte_offset_map_lock(mm, old_pmd, old_addr, &old_ptl);
174a5be621eSHugh Dickins 	if (!old_pte) {
175a5be621eSHugh Dickins 		err = -EAGAIN;
176a5be621eSHugh Dickins 		goto out;
177a5be621eSHugh Dickins 	}
178a5be621eSHugh Dickins 	new_pte = pte_offset_map_nolock(mm, new_pmd, new_addr, &new_ptl);
179a5be621eSHugh Dickins 	if (!new_pte) {
180a5be621eSHugh Dickins 		pte_unmap_unlock(old_pte, old_ptl);
181a5be621eSHugh Dickins 		err = -EAGAIN;
182a5be621eSHugh Dickins 		goto out;
183a5be621eSHugh Dickins 	}
1844c21e2f2SHugh Dickins 	if (new_ptl != old_ptl)
185f20dc5f7SIngo Molnar 		spin_lock_nested(new_ptl, SINGLE_DEPTH_NESTING);
1863ea27719SMel Gorman 	flush_tlb_batched_pending(vma->vm_mm);
1876606c3e0SZachary Amsden 	arch_enter_lazy_mmu_mode();
1888b1f3124SNick Piggin 
1897be7a546SHugh Dickins 	for (; old_addr < old_end; old_pte++, old_addr += PAGE_SIZE,
1907be7a546SHugh Dickins 				   new_pte++, new_addr += PAGE_SIZE) {
191c33c7948SRyan Roberts 		if (pte_none(ptep_get(old_pte)))
1927be7a546SHugh Dickins 			continue;
1935d190420SAaron Lu 
1947b6efc2bSAndrea Arcangeli 		pte = ptep_get_and_clear(mm, old_addr, old_pte);
195a2ce2666SAaron Lu 		/*
196eb66ae03SLinus Torvalds 		 * If we are remapping a valid PTE, make sure
197a2ce2666SAaron Lu 		 * to flush TLB before we drop the PTL for the
198eb66ae03SLinus Torvalds 		 * PTE.
199a2ce2666SAaron Lu 		 *
200eb66ae03SLinus Torvalds 		 * NOTE! Both old and new PTL matter: the old one
201eb66ae03SLinus Torvalds 		 * for racing with page_mkclean(), the new one to
202eb66ae03SLinus Torvalds 		 * make sure the physical page stays valid until
203eb66ae03SLinus Torvalds 		 * the TLB entry for the old mapping has been
204eb66ae03SLinus Torvalds 		 * flushed.
205a2ce2666SAaron Lu 		 */
206eb66ae03SLinus Torvalds 		if (pte_present(pte))
207a2ce2666SAaron Lu 			force_flush = true;
2087be7a546SHugh Dickins 		pte = move_pte(pte, new_vma->vm_page_prot, old_addr, new_addr);
2096dec97dcSCyrill Gorcunov 		pte = move_soft_dirty_pte(pte);
2106dec97dcSCyrill Gorcunov 		set_pte_at(mm, new_addr, new_pte, pte);
2111da177e4SLinus Torvalds 	}
2127be7a546SHugh Dickins 
2136606c3e0SZachary Amsden 	arch_leave_lazy_mmu_mode();
214eb66ae03SLinus Torvalds 	if (force_flush)
215eb66ae03SLinus Torvalds 		flush_tlb_range(vma, old_end - len, old_end);
2164c21e2f2SHugh Dickins 	if (new_ptl != old_ptl)
2174c21e2f2SHugh Dickins 		spin_unlock(new_ptl);
218ece0e2b6SPeter Zijlstra 	pte_unmap(new_pte - 1);
219c74df32cSHugh Dickins 	pte_unmap_unlock(old_pte - 1, old_ptl);
220a5be621eSHugh Dickins out:
2211d069b7dSHugh Dickins 	if (need_rmap_locks)
2221d069b7dSHugh Dickins 		drop_rmap_locks(vma);
223a5be621eSHugh Dickins 	return err;
2241da177e4SLinus Torvalds }
2251da177e4SLinus Torvalds 
2263bbda69cSAneesh Kumar K.V #ifndef arch_supports_page_table_move
2273bbda69cSAneesh Kumar K.V #define arch_supports_page_table_move arch_supports_page_table_move
arch_supports_page_table_move(void)2283bbda69cSAneesh Kumar K.V static inline bool arch_supports_page_table_move(void)
2293bbda69cSAneesh Kumar K.V {
2303bbda69cSAneesh Kumar K.V 	return IS_ENABLED(CONFIG_HAVE_MOVE_PMD) ||
2313bbda69cSAneesh Kumar K.V 		IS_ENABLED(CONFIG_HAVE_MOVE_PUD);
2323bbda69cSAneesh Kumar K.V }
2333bbda69cSAneesh Kumar K.V #endif
2343bbda69cSAneesh Kumar K.V 
2352c91bd4aSJoel Fernandes (Google) #ifdef CONFIG_HAVE_MOVE_PMD
move_normal_pmd(struct vm_area_struct * vma,unsigned long old_addr,unsigned long new_addr,pmd_t * old_pmd,pmd_t * new_pmd)2362c91bd4aSJoel Fernandes (Google) static bool move_normal_pmd(struct vm_area_struct *vma, unsigned long old_addr,
237b8aa9d9dSWei Yang 		  unsigned long new_addr, pmd_t *old_pmd, pmd_t *new_pmd)
2382c91bd4aSJoel Fernandes (Google) {
2392c91bd4aSJoel Fernandes (Google) 	spinlock_t *old_ptl, *new_ptl;
2402c91bd4aSJoel Fernandes (Google) 	struct mm_struct *mm = vma->vm_mm;
241*17396e32SJann Horn 	bool res = false;
2422c91bd4aSJoel Fernandes (Google) 	pmd_t pmd;
2432c91bd4aSJoel Fernandes (Google) 
2443bbda69cSAneesh Kumar K.V 	if (!arch_supports_page_table_move())
2453bbda69cSAneesh Kumar K.V 		return false;
2462c91bd4aSJoel Fernandes (Google) 	/*
2472c91bd4aSJoel Fernandes (Google) 	 * The destination pmd shouldn't be established, free_pgtables()
248f81fdd0cSLinus Torvalds 	 * should have released it.
249f81fdd0cSLinus Torvalds 	 *
250f81fdd0cSLinus Torvalds 	 * However, there's a case during execve() where we use mremap
251f81fdd0cSLinus Torvalds 	 * to move the initial stack, and in that case the target area
252f81fdd0cSLinus Torvalds 	 * may overlap the source area (always moving down).
253f81fdd0cSLinus Torvalds 	 *
254f81fdd0cSLinus Torvalds 	 * If everything is PMD-aligned, that works fine, as moving
255f81fdd0cSLinus Torvalds 	 * each pmd down will clear the source pmd. But if we first
256f81fdd0cSLinus Torvalds 	 * have a few 4kB-only pages that get moved down, and then
257f81fdd0cSLinus Torvalds 	 * hit the "now the rest is PMD-aligned, let's do everything
258f81fdd0cSLinus Torvalds 	 * one pmd at a time", we will still have the old (now empty
259f81fdd0cSLinus Torvalds 	 * of any 4kB pages, but still there) PMD in the page table
260f81fdd0cSLinus Torvalds 	 * tree.
261f81fdd0cSLinus Torvalds 	 *
262f81fdd0cSLinus Torvalds 	 * Warn on it once - because we really should try to figure
263f81fdd0cSLinus Torvalds 	 * out how to do this better - but then say "I won't move
264f81fdd0cSLinus Torvalds 	 * this pmd".
265f81fdd0cSLinus Torvalds 	 *
266f81fdd0cSLinus Torvalds 	 * One alternative might be to just unmap the target pmd at
267f81fdd0cSLinus Torvalds 	 * this point, and verify that it really is empty. We'll see.
2682c91bd4aSJoel Fernandes (Google) 	 */
269f81fdd0cSLinus Torvalds 	if (WARN_ON_ONCE(!pmd_none(*new_pmd)))
2702c91bd4aSJoel Fernandes (Google) 		return false;
2712c91bd4aSJoel Fernandes (Google) 
2722c91bd4aSJoel Fernandes (Google) 	/*
2732c91bd4aSJoel Fernandes (Google) 	 * We don't have to worry about the ordering of src and dst
274c1e8d7c6SMichel Lespinasse 	 * ptlocks because exclusive mmap_lock prevents deadlock.
2752c91bd4aSJoel Fernandes (Google) 	 */
2762c91bd4aSJoel Fernandes (Google) 	old_ptl = pmd_lock(vma->vm_mm, old_pmd);
2772c91bd4aSJoel Fernandes (Google) 	new_ptl = pmd_lockptr(mm, new_pmd);
2782c91bd4aSJoel Fernandes (Google) 	if (new_ptl != old_ptl)
2792c91bd4aSJoel Fernandes (Google) 		spin_lock_nested(new_ptl, SINGLE_DEPTH_NESTING);
2802c91bd4aSJoel Fernandes (Google) 
2812c91bd4aSJoel Fernandes (Google) 	pmd = *old_pmd;
282*17396e32SJann Horn 
283*17396e32SJann Horn 	/* Racing with collapse? */
284*17396e32SJann Horn 	if (unlikely(!pmd_present(pmd) || pmd_leaf(pmd)))
285*17396e32SJann Horn 		goto out_unlock;
286*17396e32SJann Horn 	/* Clear the pmd */
2872c91bd4aSJoel Fernandes (Google) 	pmd_clear(old_pmd);
288*17396e32SJann Horn 	res = true;
2892c91bd4aSJoel Fernandes (Google) 
2902c91bd4aSJoel Fernandes (Google) 	VM_BUG_ON(!pmd_none(*new_pmd));
2912c91bd4aSJoel Fernandes (Google) 
2920881ace2SAneesh Kumar K.V 	pmd_populate(mm, new_pmd, pmd_pgtable(pmd));
2932c91bd4aSJoel Fernandes (Google) 	flush_tlb_range(vma, old_addr, old_addr + PMD_SIZE);
294*17396e32SJann Horn out_unlock:
2952c91bd4aSJoel Fernandes (Google) 	if (new_ptl != old_ptl)
2962c91bd4aSJoel Fernandes (Google) 		spin_unlock(new_ptl);
2972c91bd4aSJoel Fernandes (Google) 	spin_unlock(old_ptl);
2982c91bd4aSJoel Fernandes (Google) 
299*17396e32SJann Horn 	return res;
3002c91bd4aSJoel Fernandes (Google) }
301c49dd340SKalesh Singh #else
move_normal_pmd(struct vm_area_struct * vma,unsigned long old_addr,unsigned long new_addr,pmd_t * old_pmd,pmd_t * new_pmd)302c49dd340SKalesh Singh static inline bool move_normal_pmd(struct vm_area_struct *vma,
303c49dd340SKalesh Singh 		unsigned long old_addr, unsigned long new_addr, pmd_t *old_pmd,
304c49dd340SKalesh Singh 		pmd_t *new_pmd)
305c49dd340SKalesh Singh {
306c49dd340SKalesh Singh 	return false;
307c49dd340SKalesh Singh }
3082c91bd4aSJoel Fernandes (Google) #endif
3092c91bd4aSJoel Fernandes (Google) 
310d6655dffSAneesh Kumar K.V #if CONFIG_PGTABLE_LEVELS > 2 && defined(CONFIG_HAVE_MOVE_PUD)
move_normal_pud(struct vm_area_struct * vma,unsigned long old_addr,unsigned long new_addr,pud_t * old_pud,pud_t * new_pud)311c49dd340SKalesh Singh static bool move_normal_pud(struct vm_area_struct *vma, unsigned long old_addr,
312c49dd340SKalesh Singh 		  unsigned long new_addr, pud_t *old_pud, pud_t *new_pud)
313c49dd340SKalesh Singh {
314c49dd340SKalesh Singh 	spinlock_t *old_ptl, *new_ptl;
315c49dd340SKalesh Singh 	struct mm_struct *mm = vma->vm_mm;
316c49dd340SKalesh Singh 	pud_t pud;
317c49dd340SKalesh Singh 
3183bbda69cSAneesh Kumar K.V 	if (!arch_supports_page_table_move())
3193bbda69cSAneesh Kumar K.V 		return false;
320c49dd340SKalesh Singh 	/*
321c49dd340SKalesh Singh 	 * The destination pud shouldn't be established, free_pgtables()
322c49dd340SKalesh Singh 	 * should have released it.
323c49dd340SKalesh Singh 	 */
324c49dd340SKalesh Singh 	if (WARN_ON_ONCE(!pud_none(*new_pud)))
325c49dd340SKalesh Singh 		return false;
326c49dd340SKalesh Singh 
327c49dd340SKalesh Singh 	/*
328c49dd340SKalesh Singh 	 * We don't have to worry about the ordering of src and dst
329c49dd340SKalesh Singh 	 * ptlocks because exclusive mmap_lock prevents deadlock.
330c49dd340SKalesh Singh 	 */
331c49dd340SKalesh Singh 	old_ptl = pud_lock(vma->vm_mm, old_pud);
332c49dd340SKalesh Singh 	new_ptl = pud_lockptr(mm, new_pud);
333c49dd340SKalesh Singh 	if (new_ptl != old_ptl)
334c49dd340SKalesh Singh 		spin_lock_nested(new_ptl, SINGLE_DEPTH_NESTING);
335c49dd340SKalesh Singh 
336c49dd340SKalesh Singh 	/* Clear the pud */
337c49dd340SKalesh Singh 	pud = *old_pud;
338c49dd340SKalesh Singh 	pud_clear(old_pud);
339c49dd340SKalesh Singh 
340c49dd340SKalesh Singh 	VM_BUG_ON(!pud_none(*new_pud));
341c49dd340SKalesh Singh 
3420881ace2SAneesh Kumar K.V 	pud_populate(mm, new_pud, pud_pgtable(pud));
343c49dd340SKalesh Singh 	flush_tlb_range(vma, old_addr, old_addr + PUD_SIZE);
344c49dd340SKalesh Singh 	if (new_ptl != old_ptl)
345c49dd340SKalesh Singh 		spin_unlock(new_ptl);
346c49dd340SKalesh Singh 	spin_unlock(old_ptl);
347c49dd340SKalesh Singh 
348c49dd340SKalesh Singh 	return true;
349c49dd340SKalesh Singh }
350c49dd340SKalesh Singh #else
move_normal_pud(struct vm_area_struct * vma,unsigned long old_addr,unsigned long new_addr,pud_t * old_pud,pud_t * new_pud)351c49dd340SKalesh Singh static inline bool move_normal_pud(struct vm_area_struct *vma,
352c49dd340SKalesh Singh 		unsigned long old_addr, unsigned long new_addr, pud_t *old_pud,
353c49dd340SKalesh Singh 		pud_t *new_pud)
354c49dd340SKalesh Singh {
355c49dd340SKalesh Singh 	return false;
356c49dd340SKalesh Singh }
357c49dd340SKalesh Singh #endif
358c49dd340SKalesh Singh 
35954a948a1SAneesh Kumar K.V #if defined(CONFIG_TRANSPARENT_HUGEPAGE) && defined(CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD)
move_huge_pud(struct vm_area_struct * vma,unsigned long old_addr,unsigned long new_addr,pud_t * old_pud,pud_t * new_pud)3607d846db7SAneesh Kumar K.V static bool move_huge_pud(struct vm_area_struct *vma, unsigned long old_addr,
3617d846db7SAneesh Kumar K.V 			  unsigned long new_addr, pud_t *old_pud, pud_t *new_pud)
3627d846db7SAneesh Kumar K.V {
3637d846db7SAneesh Kumar K.V 	spinlock_t *old_ptl, *new_ptl;
3647d846db7SAneesh Kumar K.V 	struct mm_struct *mm = vma->vm_mm;
3657d846db7SAneesh Kumar K.V 	pud_t pud;
3667d846db7SAneesh Kumar K.V 
3677d846db7SAneesh Kumar K.V 	/*
3687d846db7SAneesh Kumar K.V 	 * The destination pud shouldn't be established, free_pgtables()
3697d846db7SAneesh Kumar K.V 	 * should have released it.
3707d846db7SAneesh Kumar K.V 	 */
3717d846db7SAneesh Kumar K.V 	if (WARN_ON_ONCE(!pud_none(*new_pud)))
3727d846db7SAneesh Kumar K.V 		return false;
3737d846db7SAneesh Kumar K.V 
3747d846db7SAneesh Kumar K.V 	/*
3757d846db7SAneesh Kumar K.V 	 * We don't have to worry about the ordering of src and dst
3767d846db7SAneesh Kumar K.V 	 * ptlocks because exclusive mmap_lock prevents deadlock.
3777d846db7SAneesh Kumar K.V 	 */
3787d846db7SAneesh Kumar K.V 	old_ptl = pud_lock(vma->vm_mm, old_pud);
3797d846db7SAneesh Kumar K.V 	new_ptl = pud_lockptr(mm, new_pud);
3807d846db7SAneesh Kumar K.V 	if (new_ptl != old_ptl)
3817d846db7SAneesh Kumar K.V 		spin_lock_nested(new_ptl, SINGLE_DEPTH_NESTING);
3827d846db7SAneesh Kumar K.V 
3837d846db7SAneesh Kumar K.V 	/* Clear the pud */
3847d846db7SAneesh Kumar K.V 	pud = *old_pud;
3857d846db7SAneesh Kumar K.V 	pud_clear(old_pud);
3867d846db7SAneesh Kumar K.V 
3877d846db7SAneesh Kumar K.V 	VM_BUG_ON(!pud_none(*new_pud));
3887d846db7SAneesh Kumar K.V 
3897d846db7SAneesh Kumar K.V 	/* Set the new pud */
3907d846db7SAneesh Kumar K.V 	/* mark soft_ditry when we add pud level soft dirty support */
3917d846db7SAneesh Kumar K.V 	set_pud_at(mm, new_addr, new_pud, pud);
3927d846db7SAneesh Kumar K.V 	flush_pud_tlb_range(vma, old_addr, old_addr + HPAGE_PUD_SIZE);
3937d846db7SAneesh Kumar K.V 	if (new_ptl != old_ptl)
3947d846db7SAneesh Kumar K.V 		spin_unlock(new_ptl);
3957d846db7SAneesh Kumar K.V 	spin_unlock(old_ptl);
3967d846db7SAneesh Kumar K.V 
3977d846db7SAneesh Kumar K.V 	return true;
3987d846db7SAneesh Kumar K.V }
3997d846db7SAneesh Kumar K.V #else
move_huge_pud(struct vm_area_struct * vma,unsigned long old_addr,unsigned long new_addr,pud_t * old_pud,pud_t * new_pud)4007d846db7SAneesh Kumar K.V static bool move_huge_pud(struct vm_area_struct *vma, unsigned long old_addr,
4017d846db7SAneesh Kumar K.V 			  unsigned long new_addr, pud_t *old_pud, pud_t *new_pud)
4027d846db7SAneesh Kumar K.V {
4037d846db7SAneesh Kumar K.V 	WARN_ON_ONCE(1);
4047d846db7SAneesh Kumar K.V 	return false;
4057d846db7SAneesh Kumar K.V 
4067d846db7SAneesh Kumar K.V }
4077d846db7SAneesh Kumar K.V #endif
4087d846db7SAneesh Kumar K.V 
409c49dd340SKalesh Singh enum pgt_entry {
410c49dd340SKalesh Singh 	NORMAL_PMD,
411c49dd340SKalesh Singh 	HPAGE_PMD,
412c49dd340SKalesh Singh 	NORMAL_PUD,
4137d846db7SAneesh Kumar K.V 	HPAGE_PUD,
414c49dd340SKalesh Singh };
415c49dd340SKalesh Singh 
416c49dd340SKalesh Singh /*
417c49dd340SKalesh Singh  * Returns an extent of the corresponding size for the pgt_entry specified if
418c49dd340SKalesh Singh  * valid. Else returns a smaller extent bounded by the end of the source and
419c49dd340SKalesh Singh  * destination pgt_entry.
420c49dd340SKalesh Singh  */
get_extent(enum pgt_entry entry,unsigned long old_addr,unsigned long old_end,unsigned long new_addr)421a30a2909SArnd Bergmann static __always_inline unsigned long get_extent(enum pgt_entry entry,
422a30a2909SArnd Bergmann 			unsigned long old_addr, unsigned long old_end,
423a30a2909SArnd Bergmann 			unsigned long new_addr)
424c49dd340SKalesh Singh {
425c49dd340SKalesh Singh 	unsigned long next, extent, mask, size;
426c49dd340SKalesh Singh 
427c49dd340SKalesh Singh 	switch (entry) {
428c49dd340SKalesh Singh 	case HPAGE_PMD:
429c49dd340SKalesh Singh 	case NORMAL_PMD:
430c49dd340SKalesh Singh 		mask = PMD_MASK;
431c49dd340SKalesh Singh 		size = PMD_SIZE;
432c49dd340SKalesh Singh 		break;
4337d846db7SAneesh Kumar K.V 	case HPAGE_PUD:
434c49dd340SKalesh Singh 	case NORMAL_PUD:
435c49dd340SKalesh Singh 		mask = PUD_MASK;
436c49dd340SKalesh Singh 		size = PUD_SIZE;
437c49dd340SKalesh Singh 		break;
438c49dd340SKalesh Singh 	default:
439c49dd340SKalesh Singh 		BUILD_BUG();
440c49dd340SKalesh Singh 		break;
441c49dd340SKalesh Singh 	}
442c49dd340SKalesh Singh 
443c49dd340SKalesh Singh 	next = (old_addr + size) & mask;
444c49dd340SKalesh Singh 	/* even if next overflowed, extent below will be ok */
445e05986eeSKalesh Singh 	extent = next - old_addr;
446e05986eeSKalesh Singh 	if (extent > old_end - old_addr)
447e05986eeSKalesh Singh 		extent = old_end - old_addr;
448c49dd340SKalesh Singh 	next = (new_addr + size) & mask;
449c49dd340SKalesh Singh 	if (extent > next - new_addr)
450c49dd340SKalesh Singh 		extent = next - new_addr;
451c49dd340SKalesh Singh 	return extent;
452c49dd340SKalesh Singh }
453c49dd340SKalesh Singh 
454c49dd340SKalesh Singh /*
455c49dd340SKalesh Singh  * Attempts to speedup the move by moving entry at the level corresponding to
456c49dd340SKalesh Singh  * pgt_entry. Returns true if the move was successful, else false.
457c49dd340SKalesh Singh  */
move_pgt_entry(enum pgt_entry entry,struct vm_area_struct * vma,unsigned long old_addr,unsigned long new_addr,void * old_entry,void * new_entry,bool need_rmap_locks)458c49dd340SKalesh Singh static bool move_pgt_entry(enum pgt_entry entry, struct vm_area_struct *vma,
459c49dd340SKalesh Singh 			unsigned long old_addr, unsigned long new_addr,
460c49dd340SKalesh Singh 			void *old_entry, void *new_entry, bool need_rmap_locks)
461c49dd340SKalesh Singh {
462c49dd340SKalesh Singh 	bool moved = false;
463c49dd340SKalesh Singh 
464c49dd340SKalesh Singh 	/* See comment in move_ptes() */
465c49dd340SKalesh Singh 	if (need_rmap_locks)
466c49dd340SKalesh Singh 		take_rmap_locks(vma);
467c49dd340SKalesh Singh 
468c49dd340SKalesh Singh 	switch (entry) {
469c49dd340SKalesh Singh 	case NORMAL_PMD:
470c49dd340SKalesh Singh 		moved = move_normal_pmd(vma, old_addr, new_addr, old_entry,
471c49dd340SKalesh Singh 					new_entry);
472c49dd340SKalesh Singh 		break;
473c49dd340SKalesh Singh 	case NORMAL_PUD:
474c49dd340SKalesh Singh 		moved = move_normal_pud(vma, old_addr, new_addr, old_entry,
475c49dd340SKalesh Singh 					new_entry);
476c49dd340SKalesh Singh 		break;
477c49dd340SKalesh Singh 	case HPAGE_PMD:
478c49dd340SKalesh Singh 		moved = IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE) &&
479c49dd340SKalesh Singh 			move_huge_pmd(vma, old_addr, new_addr, old_entry,
480c49dd340SKalesh Singh 				      new_entry);
481c49dd340SKalesh Singh 		break;
4827d846db7SAneesh Kumar K.V 	case HPAGE_PUD:
4837d846db7SAneesh Kumar K.V 		moved = IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE) &&
4847d846db7SAneesh Kumar K.V 			move_huge_pud(vma, old_addr, new_addr, old_entry,
4857d846db7SAneesh Kumar K.V 				      new_entry);
4867d846db7SAneesh Kumar K.V 		break;
4877d846db7SAneesh Kumar K.V 
488c49dd340SKalesh Singh 	default:
489c49dd340SKalesh Singh 		WARN_ON_ONCE(1);
490c49dd340SKalesh Singh 		break;
491c49dd340SKalesh Singh 	}
492c49dd340SKalesh Singh 
493c49dd340SKalesh Singh 	if (need_rmap_locks)
494c49dd340SKalesh Singh 		drop_rmap_locks(vma);
495c49dd340SKalesh Singh 
496c49dd340SKalesh Singh 	return moved;
497c49dd340SKalesh Singh }
498c49dd340SKalesh Singh 
move_page_tables(struct vm_area_struct * vma,unsigned long old_addr,struct vm_area_struct * new_vma,unsigned long new_addr,unsigned long len,bool need_rmap_locks)499b6a2fea3SOllie Wild unsigned long move_page_tables(struct vm_area_struct *vma,
5001da177e4SLinus Torvalds 		unsigned long old_addr, struct vm_area_struct *new_vma,
50138a76013SMichel Lespinasse 		unsigned long new_addr, unsigned long len,
50238a76013SMichel Lespinasse 		bool need_rmap_locks)
5031da177e4SLinus Torvalds {
504c49dd340SKalesh Singh 	unsigned long extent, old_end;
505ac46d4f3SJérôme Glisse 	struct mmu_notifier_range range;
5067be7a546SHugh Dickins 	pmd_t *old_pmd, *new_pmd;
5077d846db7SAneesh Kumar K.V 	pud_t *old_pud, *new_pud;
5081da177e4SLinus Torvalds 
50901e67e04SPaolo Bonzini 	if (!len)
51001e67e04SPaolo Bonzini 		return 0;
51101e67e04SPaolo Bonzini 
5127be7a546SHugh Dickins 	old_end = old_addr + len;
5131da177e4SLinus Torvalds 
514550a7d60SMina Almasry 	if (is_vm_hugetlb_page(vma))
515550a7d60SMina Almasry 		return move_hugetlb_page_tables(vma, new_vma, old_addr,
516550a7d60SMina Almasry 						new_addr, len);
517550a7d60SMina Almasry 
5183d0b95cdSBaolin Wang 	flush_cache_range(vma, old_addr, old_end);
5197d4a8be0SAlistair Popple 	mmu_notifier_range_init(&range, MMU_NOTIFY_UNMAP, 0, vma->vm_mm,
5206f4f13e8SJérôme Glisse 				old_addr, old_end);
521ac46d4f3SJérôme Glisse 	mmu_notifier_invalidate_range_start(&range);
5227b6efc2bSAndrea Arcangeli 
5237be7a546SHugh Dickins 	for (; old_addr < old_end; old_addr += extent, new_addr += extent) {
5241da177e4SLinus Torvalds 		cond_resched();
525c49dd340SKalesh Singh 		/*
526c49dd340SKalesh Singh 		 * If extent is PUD-sized try to speed up the move by moving at the
527c49dd340SKalesh Singh 		 * PUD level if possible.
528c49dd340SKalesh Singh 		 */
529c49dd340SKalesh Singh 		extent = get_extent(NORMAL_PUD, old_addr, old_end, new_addr);
530c49dd340SKalesh Singh 
531c49dd340SKalesh Singh 		old_pud = get_old_pud(vma->vm_mm, old_addr);
532c49dd340SKalesh Singh 		if (!old_pud)
533c49dd340SKalesh Singh 			continue;
534c49dd340SKalesh Singh 		new_pud = alloc_new_pud(vma->vm_mm, vma, new_addr);
535c49dd340SKalesh Singh 		if (!new_pud)
536c49dd340SKalesh Singh 			break;
5377d846db7SAneesh Kumar K.V 		if (pud_trans_huge(*old_pud) || pud_devmap(*old_pud)) {
5387d846db7SAneesh Kumar K.V 			if (extent == HPAGE_PUD_SIZE) {
5397d846db7SAneesh Kumar K.V 				move_pgt_entry(HPAGE_PUD, vma, old_addr, new_addr,
5407d846db7SAneesh Kumar K.V 					       old_pud, new_pud, need_rmap_locks);
5417d846db7SAneesh Kumar K.V 				/* We ignore and continue on error? */
5427d846db7SAneesh Kumar K.V 				continue;
5437d846db7SAneesh Kumar K.V 			}
5447d846db7SAneesh Kumar K.V 		} else if (IS_ENABLED(CONFIG_HAVE_MOVE_PUD) && extent == PUD_SIZE) {
5457d846db7SAneesh Kumar K.V 
546c49dd340SKalesh Singh 			if (move_pgt_entry(NORMAL_PUD, vma, old_addr, new_addr,
54797113eb3SAneesh Kumar K.V 					   old_pud, new_pud, true))
548c49dd340SKalesh Singh 				continue;
549c49dd340SKalesh Singh 		}
550c49dd340SKalesh Singh 
551c49dd340SKalesh Singh 		extent = get_extent(NORMAL_PMD, old_addr, old_end, new_addr);
5527be7a546SHugh Dickins 		old_pmd = get_old_pmd(vma->vm_mm, old_addr);
5537be7a546SHugh Dickins 		if (!old_pmd)
5547be7a546SHugh Dickins 			continue;
5558ac1f832SAndrea Arcangeli 		new_pmd = alloc_new_pmd(vma->vm_mm, vma, new_addr);
5567be7a546SHugh Dickins 		if (!new_pmd)
5577be7a546SHugh Dickins 			break;
558a5be621eSHugh Dickins again:
559c49dd340SKalesh Singh 		if (is_swap_pmd(*old_pmd) || pmd_trans_huge(*old_pmd) ||
560c49dd340SKalesh Singh 		    pmd_devmap(*old_pmd)) {
561c49dd340SKalesh Singh 			if (extent == HPAGE_PMD_SIZE &&
562c49dd340SKalesh Singh 			    move_pgt_entry(HPAGE_PMD, vma, old_addr, new_addr,
563c49dd340SKalesh Singh 					   old_pmd, new_pmd, need_rmap_locks))
56437a1c49aSAndrea Arcangeli 				continue;
5654b471e88SKirill A. Shutemov 			split_huge_pmd(vma, old_pmd, old_addr);
566c49dd340SKalesh Singh 		} else if (IS_ENABLED(CONFIG_HAVE_MOVE_PMD) &&
567c49dd340SKalesh Singh 			   extent == PMD_SIZE) {
5682c91bd4aSJoel Fernandes (Google) 			/*
5692c91bd4aSJoel Fernandes (Google) 			 * If the extent is PMD-sized, try to speed the move by
5702c91bd4aSJoel Fernandes (Google) 			 * moving at the PMD level if possible.
5712c91bd4aSJoel Fernandes (Google) 			 */
572c49dd340SKalesh Singh 			if (move_pgt_entry(NORMAL_PMD, vma, old_addr, new_addr,
57397113eb3SAneesh Kumar K.V 					   old_pmd, new_pmd, true))
5742c91bd4aSJoel Fernandes (Google) 				continue;
57537a1c49aSAndrea Arcangeli 		}
576a5be621eSHugh Dickins 		if (pmd_none(*old_pmd))
577a5be621eSHugh Dickins 			continue;
5784cf58924SJoel Fernandes (Google) 		if (pte_alloc(new_vma->vm_mm, new_pmd))
57937a1c49aSAndrea Arcangeli 			break;
580a5be621eSHugh Dickins 		if (move_ptes(vma, old_pmd, old_addr, old_addr + extent,
581a5be621eSHugh Dickins 			      new_vma, new_pmd, new_addr, need_rmap_locks) < 0)
582a5be621eSHugh Dickins 			goto again;
5831da177e4SLinus Torvalds 	}
5847b6efc2bSAndrea Arcangeli 
585ac46d4f3SJérôme Glisse 	mmu_notifier_invalidate_range_end(&range);
5867be7a546SHugh Dickins 
5877be7a546SHugh Dickins 	return len + old_addr - old_end;	/* how much done */
5881da177e4SLinus Torvalds }
5891da177e4SLinus Torvalds 
move_vma(struct vm_area_struct * vma,unsigned long old_addr,unsigned long old_len,unsigned long new_len,unsigned long new_addr,bool * locked,unsigned long flags,struct vm_userfaultfd_ctx * uf,struct list_head * uf_unmap)5901da177e4SLinus Torvalds static unsigned long move_vma(struct vm_area_struct *vma,
5911da177e4SLinus Torvalds 		unsigned long old_addr, unsigned long old_len,
59272f87654SPavel Emelyanov 		unsigned long new_len, unsigned long new_addr,
593e346b381SBrian Geffon 		bool *locked, unsigned long flags,
594e346b381SBrian Geffon 		struct vm_userfaultfd_ctx *uf, struct list_head *uf_unmap)
5951da177e4SLinus Torvalds {
596fdbef614SDmitry Safonov 	long to_account = new_len - old_len;
5971da177e4SLinus Torvalds 	struct mm_struct *mm = vma->vm_mm;
5981da177e4SLinus Torvalds 	struct vm_area_struct *new_vma;
5991da177e4SLinus Torvalds 	unsigned long vm_flags = vma->vm_flags;
6001da177e4SLinus Torvalds 	unsigned long new_pgoff;
6011da177e4SLinus Torvalds 	unsigned long moved_len;
6026b73cff2SLiam R. Howlett 	unsigned long account_start = 0;
6036b73cff2SLiam R. Howlett 	unsigned long account_end = 0;
604365e9c87SHugh Dickins 	unsigned long hiwater_vm;
60573d5e062SDmitry Safonov 	int err = 0;
60638a76013SMichel Lespinasse 	bool need_rmap_locks;
6076b73cff2SLiam R. Howlett 	struct vma_iterator vmi;
6081da177e4SLinus Torvalds 
6091da177e4SLinus Torvalds 	/*
6101da177e4SLinus Torvalds 	 * We'd prefer to avoid failure later on in do_munmap:
6111da177e4SLinus Torvalds 	 * which may split one vma into three before unmapping.
6121da177e4SLinus Torvalds 	 */
6131da177e4SLinus Torvalds 	if (mm->map_count >= sysctl_max_map_count - 3)
6141da177e4SLinus Torvalds 		return -ENOMEM;
6151da177e4SLinus Torvalds 
616fdbef614SDmitry Safonov 	if (unlikely(flags & MREMAP_DONTUNMAP))
617fdbef614SDmitry Safonov 		to_account = new_len;
618fdbef614SDmitry Safonov 
61973d5e062SDmitry Safonov 	if (vma->vm_ops && vma->vm_ops->may_split) {
62073d5e062SDmitry Safonov 		if (vma->vm_start != old_addr)
62173d5e062SDmitry Safonov 			err = vma->vm_ops->may_split(vma, old_addr);
62273d5e062SDmitry Safonov 		if (!err && vma->vm_end != old_addr + old_len)
62373d5e062SDmitry Safonov 			err = vma->vm_ops->may_split(vma, old_addr + old_len);
62473d5e062SDmitry Safonov 		if (err)
62573d5e062SDmitry Safonov 			return err;
62673d5e062SDmitry Safonov 	}
62773d5e062SDmitry Safonov 
6281ff82995SHugh Dickins 	/*
6291ff82995SHugh Dickins 	 * Advise KSM to break any KSM pages in the area to be moved:
6301ff82995SHugh Dickins 	 * it would be confusing if they were to turn up at the new
6311ff82995SHugh Dickins 	 * location, where they happen to coincide with different KSM
6321ff82995SHugh Dickins 	 * pages recently unmapped.  But leave vma->vm_flags as it was,
6331ff82995SHugh Dickins 	 * so KSM can come around to merge on vma and new_vma afterwards.
6341ff82995SHugh Dickins 	 */
6357103ad32SHugh Dickins 	err = ksm_madvise(vma, old_addr, old_addr + old_len,
6367103ad32SHugh Dickins 						MADV_UNMERGEABLE, &vm_flags);
6377103ad32SHugh Dickins 	if (err)
6387103ad32SHugh Dickins 		return err;
6391ff82995SHugh Dickins 
640fdbef614SDmitry Safonov 	if (vm_flags & VM_ACCOUNT) {
641fdbef614SDmitry Safonov 		if (security_vm_enough_memory_mm(mm, to_account >> PAGE_SHIFT))
642ad8ee77eSDmitry Safonov 			return -ENOMEM;
643ad8ee77eSDmitry Safonov 	}
644ad8ee77eSDmitry Safonov 
645d6ac235dSSuren Baghdasaryan 	vma_start_write(vma);
6461da177e4SLinus Torvalds 	new_pgoff = vma->vm_pgoff + ((old_addr - vma->vm_start) >> PAGE_SHIFT);
64738a76013SMichel Lespinasse 	new_vma = copy_vma(&vma, new_addr, new_len, new_pgoff,
64838a76013SMichel Lespinasse 			   &need_rmap_locks);
649ad8ee77eSDmitry Safonov 	if (!new_vma) {
650fdbef614SDmitry Safonov 		if (vm_flags & VM_ACCOUNT)
651fdbef614SDmitry Safonov 			vm_unacct_memory(to_account >> PAGE_SHIFT);
6521da177e4SLinus Torvalds 		return -ENOMEM;
653ad8ee77eSDmitry Safonov 	}
6541da177e4SLinus Torvalds 
65538a76013SMichel Lespinasse 	moved_len = move_page_tables(vma, old_addr, new_vma, new_addr, old_len,
65638a76013SMichel Lespinasse 				     need_rmap_locks);
6571da177e4SLinus Torvalds 	if (moved_len < old_len) {
658df1eab30SOleg Nesterov 		err = -ENOMEM;
6595477e70aSOleg Nesterov 	} else if (vma->vm_ops && vma->vm_ops->mremap) {
66014d07113SBrian Geffon 		err = vma->vm_ops->mremap(new_vma);
661df1eab30SOleg Nesterov 	}
662df1eab30SOleg Nesterov 
663df1eab30SOleg Nesterov 	if (unlikely(err)) {
6641da177e4SLinus Torvalds 		/*
6651da177e4SLinus Torvalds 		 * On error, move entries back from new area to old,
6661da177e4SLinus Torvalds 		 * which will succeed since page tables still there,
6671da177e4SLinus Torvalds 		 * and then proceed to unmap new area instead of old.
6681da177e4SLinus Torvalds 		 */
66938a76013SMichel Lespinasse 		move_page_tables(new_vma, new_addr, vma, old_addr, moved_len,
67038a76013SMichel Lespinasse 				 true);
6711da177e4SLinus Torvalds 		vma = new_vma;
6721da177e4SLinus Torvalds 		old_len = new_len;
6731da177e4SLinus Torvalds 		old_addr = new_addr;
674df1eab30SOleg Nesterov 		new_addr = err;
6754abad2caSLaurent Dufour 	} else {
67672f87654SPavel Emelyanov 		mremap_userfaultfd_prep(new_vma, uf);
6774abad2caSLaurent Dufour 	}
6781da177e4SLinus Torvalds 
679550a7d60SMina Almasry 	if (is_vm_hugetlb_page(vma)) {
680550a7d60SMina Almasry 		clear_vma_resv_huge_pages(vma);
681550a7d60SMina Almasry 	}
682550a7d60SMina Almasry 
6831da177e4SLinus Torvalds 	/* Conceal VM_ACCOUNT so old reservation is not undone */
684ad8ee77eSDmitry Safonov 	if (vm_flags & VM_ACCOUNT && !(flags & MREMAP_DONTUNMAP)) {
6851c71222eSSuren Baghdasaryan 		vm_flags_clear(vma, VM_ACCOUNT);
6866b73cff2SLiam R. Howlett 		if (vma->vm_start < old_addr)
6876b73cff2SLiam R. Howlett 			account_start = vma->vm_start;
6886b73cff2SLiam R. Howlett 		if (vma->vm_end > old_addr + old_len)
6896b73cff2SLiam R. Howlett 			account_end = vma->vm_end;
6901da177e4SLinus Torvalds 	}
6911da177e4SLinus Torvalds 
69271799062SKirill Korotaev 	/*
693365e9c87SHugh Dickins 	 * If we failed to move page tables we still do total_vm increment
694365e9c87SHugh Dickins 	 * since do_munmap() will decrement it by old_len == new_len.
695365e9c87SHugh Dickins 	 *
696365e9c87SHugh Dickins 	 * Since total_vm is about to be raised artificially high for a
697365e9c87SHugh Dickins 	 * moment, we need to restore high watermark afterwards: if stats
698365e9c87SHugh Dickins 	 * are taken meanwhile, total_vm and hiwater_vm appear too high.
699365e9c87SHugh Dickins 	 * If this were a serious issue, we'd add a flag to do_munmap().
70071799062SKirill Korotaev 	 */
701365e9c87SHugh Dickins 	hiwater_vm = mm->hiwater_vm;
70284638335SKonstantin Khlebnikov 	vm_stat_account(mm, vma->vm_flags, new_len >> PAGE_SHIFT);
70371799062SKirill Korotaev 
704d9fe4fabSToshi Kani 	/* Tell pfnmap has moved from this vma */
705d9fe4fabSToshi Kani 	if (unlikely(vma->vm_flags & VM_PFNMAP))
706d155df53SMa Wupeng 		untrack_pfn_clear(vma);
707d9fe4fabSToshi Kani 
708e346b381SBrian Geffon 	if (unlikely(!err && (flags & MREMAP_DONTUNMAP))) {
709e346b381SBrian Geffon 		/* We always clear VM_LOCKED[ONFAULT] on the old vma */
710e430a95aSSuren Baghdasaryan 		vm_flags_clear(vma, VM_LOCKED_MASK);
711e346b381SBrian Geffon 
7121583aa27SLi Xinhai 		/*
7131583aa27SLi Xinhai 		 * anon_vma links of the old vma is no longer needed after its page
7141583aa27SLi Xinhai 		 * table has been moved.
7151583aa27SLi Xinhai 		 */
7161583aa27SLi Xinhai 		if (new_vma != vma && vma->vm_start == old_addr &&
7171583aa27SLi Xinhai 			vma->vm_end == (old_addr + old_len))
7181583aa27SLi Xinhai 			unlink_anon_vmas(vma);
7191583aa27SLi Xinhai 
720e346b381SBrian Geffon 		/* Because we won't unmap we don't need to touch locked_vm */
721ad8ee77eSDmitry Safonov 		return new_addr;
722e346b381SBrian Geffon 	}
723e346b381SBrian Geffon 
7246b73cff2SLiam R. Howlett 	vma_iter_init(&vmi, mm, old_addr);
7253cec5049SLinus Torvalds 	if (do_vmi_munmap(&vmi, mm, old_addr, old_len, uf_unmap, false) < 0) {
7261da177e4SLinus Torvalds 		/* OOM: unable to split vma, just get accounts right */
727ad8ee77eSDmitry Safonov 		if (vm_flags & VM_ACCOUNT && !(flags & MREMAP_DONTUNMAP))
7285e22928aSChen Wandun 			vm_acct_memory(old_len >> PAGE_SHIFT);
7296b73cff2SLiam R. Howlett 		account_start = account_end = 0;
7301da177e4SLinus Torvalds 	}
731e346b381SBrian Geffon 
732e346b381SBrian Geffon 	if (vm_flags & VM_LOCKED) {
733e346b381SBrian Geffon 		mm->locked_vm += new_len >> PAGE_SHIFT;
734e346b381SBrian Geffon 		*locked = true;
735e346b381SBrian Geffon 	}
736ad8ee77eSDmitry Safonov 
737365e9c87SHugh Dickins 	mm->hiwater_vm = hiwater_vm;
7381da177e4SLinus Torvalds 
7391da177e4SLinus Torvalds 	/* Restore VM_ACCOUNT if one or two pieces of vma left */
7406b73cff2SLiam R. Howlett 	if (account_start) {
7416b73cff2SLiam R. Howlett 		vma = vma_prev(&vmi);
7421c71222eSSuren Baghdasaryan 		vm_flags_set(vma, VM_ACCOUNT);
7436b73cff2SLiam R. Howlett 	}
7446b73cff2SLiam R. Howlett 
7456b73cff2SLiam R. Howlett 	if (account_end) {
7466b73cff2SLiam R. Howlett 		vma = vma_next(&vmi);
7471c71222eSSuren Baghdasaryan 		vm_flags_set(vma, VM_ACCOUNT);
7481da177e4SLinus Torvalds 	}
7491da177e4SLinus Torvalds 
7501da177e4SLinus Torvalds 	return new_addr;
7511da177e4SLinus Torvalds }
7521da177e4SLinus Torvalds 
vma_to_resize(unsigned long addr,unsigned long old_len,unsigned long new_len,unsigned long flags)75354f5de70SAl Viro static struct vm_area_struct *vma_to_resize(unsigned long addr,
754fdbef614SDmitry Safonov 	unsigned long old_len, unsigned long new_len, unsigned long flags)
75554f5de70SAl Viro {
75654f5de70SAl Viro 	struct mm_struct *mm = current->mm;
7575aaf07f0SLiam Howlett 	struct vm_area_struct *vma;
7581d391686SOleg Nesterov 	unsigned long pgoff;
75954f5de70SAl Viro 
7605aaf07f0SLiam Howlett 	vma = vma_lookup(mm, addr);
7615aaf07f0SLiam Howlett 	if (!vma)
7626cd57613SDerek 		return ERR_PTR(-EFAULT);
76354f5de70SAl Viro 
764dba58d3bSMike Kravetz 	/*
765dba58d3bSMike Kravetz 	 * !old_len is a special case where an attempt is made to 'duplicate'
766dba58d3bSMike Kravetz 	 * a mapping.  This makes no sense for private mappings as it will
767dba58d3bSMike Kravetz 	 * instead create a fresh/new mapping unrelated to the original.  This
768dba58d3bSMike Kravetz 	 * is contrary to the basic idea of mremap which creates new mappings
769dba58d3bSMike Kravetz 	 * based on the original.  There are no known use cases for this
770dba58d3bSMike Kravetz 	 * behavior.  As a result, fail such attempts.
771dba58d3bSMike Kravetz 	 */
772dba58d3bSMike Kravetz 	if (!old_len && !(vma->vm_flags & (VM_SHARED | VM_MAYSHARE))) {
773dba58d3bSMike Kravetz 		pr_warn_once("%s (%d): attempted to duplicate a private mapping with mremap.  This is not supported.\n", current->comm, current->pid);
774dba58d3bSMike Kravetz 		return ERR_PTR(-EINVAL);
775dba58d3bSMike Kravetz 	}
776dba58d3bSMike Kravetz 
777a4609387SBrian Geffon 	if ((flags & MREMAP_DONTUNMAP) &&
778a4609387SBrian Geffon 			(vma->vm_flags & (VM_DONTEXPAND | VM_PFNMAP)))
779e346b381SBrian Geffon 		return ERR_PTR(-EINVAL);
780e346b381SBrian Geffon 
78154f5de70SAl Viro 	/* We can't remap across vm area boundaries */
78254f5de70SAl Viro 	if (old_len > vma->vm_end - addr)
7836cd57613SDerek 		return ERR_PTR(-EFAULT);
78454f5de70SAl Viro 
7851d391686SOleg Nesterov 	if (new_len == old_len)
7861d391686SOleg Nesterov 		return vma;
787982134baSLinus Torvalds 
7881d391686SOleg Nesterov 	/* Need to be careful about a growing mapping */
789982134baSLinus Torvalds 	pgoff = (addr - vma->vm_start) >> PAGE_SHIFT;
790982134baSLinus Torvalds 	pgoff += vma->vm_pgoff;
791982134baSLinus Torvalds 	if (pgoff + (new_len >> PAGE_SHIFT) < pgoff)
7926cd57613SDerek 		return ERR_PTR(-EINVAL);
7931d391686SOleg Nesterov 
7941d391686SOleg Nesterov 	if (vma->vm_flags & (VM_DONTEXPAND | VM_PFNMAP))
7951d391686SOleg Nesterov 		return ERR_PTR(-EFAULT);
79654f5de70SAl Viro 
797b0cc5e89SAndrew Morton 	if (!mlock_future_ok(mm, vma->vm_flags, new_len - old_len))
7986cd57613SDerek 		return ERR_PTR(-EAGAIN);
79954f5de70SAl Viro 
80084638335SKonstantin Khlebnikov 	if (!may_expand_vm(mm, vma->vm_flags,
80184638335SKonstantin Khlebnikov 				(new_len - old_len) >> PAGE_SHIFT))
8026cd57613SDerek 		return ERR_PTR(-ENOMEM);
80354f5de70SAl Viro 
80454f5de70SAl Viro 	return vma;
80554f5de70SAl Viro }
80654f5de70SAl Viro 
mremap_to(unsigned long addr,unsigned long old_len,unsigned long new_addr,unsigned long new_len,bool * locked,unsigned long flags,struct vm_userfaultfd_ctx * uf,struct list_head * uf_unmap_early,struct list_head * uf_unmap)80781909b84SMichel Lespinasse static unsigned long mremap_to(unsigned long addr, unsigned long old_len,
80872f87654SPavel Emelyanov 		unsigned long new_addr, unsigned long new_len, bool *locked,
809e346b381SBrian Geffon 		unsigned long flags, struct vm_userfaultfd_ctx *uf,
810b2282371SMike Rapoport 		struct list_head *uf_unmap_early,
811897ab3e0SMike Rapoport 		struct list_head *uf_unmap)
812ecc1a899SAl Viro {
813ecc1a899SAl Viro 	struct mm_struct *mm = current->mm;
814ecc1a899SAl Viro 	struct vm_area_struct *vma;
815ecc1a899SAl Viro 	unsigned long ret = -EINVAL;
816e346b381SBrian Geffon 	unsigned long map_flags = 0;
817ecc1a899SAl Viro 
818f19cb115SAlexander Kuleshov 	if (offset_in_page(new_addr))
819ecc1a899SAl Viro 		goto out;
820ecc1a899SAl Viro 
821ecc1a899SAl Viro 	if (new_len > TASK_SIZE || new_addr > TASK_SIZE - new_len)
822ecc1a899SAl Viro 		goto out;
823ecc1a899SAl Viro 
8249943242cSOleg Nesterov 	/* Ensure the old/new locations do not overlap */
8259943242cSOleg Nesterov 	if (addr + old_len > new_addr && new_addr + new_len > addr)
826ecc1a899SAl Viro 		goto out;
827ecc1a899SAl Viro 
828ea2c3f6fSOscar Salvador 	/*
829ea2c3f6fSOscar Salvador 	 * move_vma() need us to stay 4 maps below the threshold, otherwise
830ea2c3f6fSOscar Salvador 	 * it will bail out at the very beginning.
831ea2c3f6fSOscar Salvador 	 * That is a problem if we have already unmaped the regions here
832ea2c3f6fSOscar Salvador 	 * (new_addr, and old_addr), because userspace will not know the
833ea2c3f6fSOscar Salvador 	 * state of the vma's after it gets -ENOMEM.
834ea2c3f6fSOscar Salvador 	 * So, to avoid such scenario we can pre-compute if the whole
835ea2c3f6fSOscar Salvador 	 * operation has high chances to success map-wise.
836ea2c3f6fSOscar Salvador 	 * Worst-scenario case is when both vma's (new_addr and old_addr) get
837f0953a1bSIngo Molnar 	 * split in 3 before unmapping it.
838ea2c3f6fSOscar Salvador 	 * That means 2 more maps (1 for each) to the ones we already hold.
839ea2c3f6fSOscar Salvador 	 * Check whether current map count plus 2 still leads us to 4 maps below
840ea2c3f6fSOscar Salvador 	 * the threshold, otherwise return -ENOMEM here to be more safe.
841ea2c3f6fSOscar Salvador 	 */
842ea2c3f6fSOscar Salvador 	if ((mm->map_count + 2) >= sysctl_max_map_count - 3)
843ea2c3f6fSOscar Salvador 		return -ENOMEM;
844ea2c3f6fSOscar Salvador 
845e346b381SBrian Geffon 	if (flags & MREMAP_FIXED) {
846b2282371SMike Rapoport 		ret = do_munmap(mm, new_addr, new_len, uf_unmap_early);
847ecc1a899SAl Viro 		if (ret)
848ecc1a899SAl Viro 			goto out;
849e346b381SBrian Geffon 	}
850ecc1a899SAl Viro 
8513c9fe8b8SMiaohe Lin 	if (old_len > new_len) {
852897ab3e0SMike Rapoport 		ret = do_munmap(mm, addr+new_len, old_len - new_len, uf_unmap);
8533c9fe8b8SMiaohe Lin 		if (ret)
854ecc1a899SAl Viro 			goto out;
855ecc1a899SAl Viro 		old_len = new_len;
856ecc1a899SAl Viro 	}
857ecc1a899SAl Viro 
858fdbef614SDmitry Safonov 	vma = vma_to_resize(addr, old_len, new_len, flags);
859ecc1a899SAl Viro 	if (IS_ERR(vma)) {
860ecc1a899SAl Viro 		ret = PTR_ERR(vma);
861ecc1a899SAl Viro 		goto out;
862ecc1a899SAl Viro 	}
863ecc1a899SAl Viro 
864e346b381SBrian Geffon 	/* MREMAP_DONTUNMAP expands by old_len since old_len == new_len */
865e346b381SBrian Geffon 	if (flags & MREMAP_DONTUNMAP &&
866e346b381SBrian Geffon 		!may_expand_vm(mm, vma->vm_flags, old_len >> PAGE_SHIFT)) {
867e346b381SBrian Geffon 		ret = -ENOMEM;
868e346b381SBrian Geffon 		goto out;
869e346b381SBrian Geffon 	}
870e346b381SBrian Geffon 
871e346b381SBrian Geffon 	if (flags & MREMAP_FIXED)
872e346b381SBrian Geffon 		map_flags |= MAP_FIXED;
873e346b381SBrian Geffon 
874097eed10SAl Viro 	if (vma->vm_flags & VM_MAYSHARE)
875097eed10SAl Viro 		map_flags |= MAP_SHARED;
8769206de95SAl Viro 
877097eed10SAl Viro 	ret = get_unmapped_area(vma->vm_file, new_addr, new_len, vma->vm_pgoff +
878097eed10SAl Viro 				((addr - vma->vm_start) >> PAGE_SHIFT),
879097eed10SAl Viro 				map_flags);
880ff68dac6SGaowei Pu 	if (IS_ERR_VALUE(ret))
881fdbef614SDmitry Safonov 		goto out;
882097eed10SAl Viro 
883e346b381SBrian Geffon 	/* We got a new mapping */
884e346b381SBrian Geffon 	if (!(flags & MREMAP_FIXED))
885e346b381SBrian Geffon 		new_addr = ret;
886e346b381SBrian Geffon 
887e346b381SBrian Geffon 	ret = move_vma(vma, addr, old_len, new_len, new_addr, locked, flags, uf,
888897ab3e0SMike Rapoport 		       uf_unmap);
889e346b381SBrian Geffon 
890ecc1a899SAl Viro out:
891ecc1a899SAl Viro 	return ret;
892ecc1a899SAl Viro }
893ecc1a899SAl Viro 
vma_expandable(struct vm_area_struct * vma,unsigned long delta)8941a0ef85fSAl Viro static int vma_expandable(struct vm_area_struct *vma, unsigned long delta)
8951a0ef85fSAl Viro {
896f106af4eSAl Viro 	unsigned long end = vma->vm_end + delta;
897396a44ccSLiam R. Howlett 
8989206de95SAl Viro 	if (end < vma->vm_end) /* overflow */
8991a0ef85fSAl Viro 		return 0;
900396a44ccSLiam R. Howlett 	if (find_vma_intersection(vma->vm_mm, vma->vm_end, end))
901f106af4eSAl Viro 		return 0;
902f106af4eSAl Viro 	if (get_unmapped_area(NULL, vma->vm_start, end - vma->vm_start,
903f106af4eSAl Viro 			      0, MAP_FIXED) & ~PAGE_MASK)
904f106af4eSAl Viro 		return 0;
9051a0ef85fSAl Viro 	return 1;
9061a0ef85fSAl Viro }
9071a0ef85fSAl Viro 
9081da177e4SLinus Torvalds /*
9091da177e4SLinus Torvalds  * Expand (or shrink) an existing mapping, potentially moving it at the
9101da177e4SLinus Torvalds  * same time (controlled by the MREMAP_MAYMOVE flag and available VM space)
9111da177e4SLinus Torvalds  *
9121da177e4SLinus Torvalds  * MREMAP_FIXED option added 5-Dec-1999 by Benjamin LaHaise
9131da177e4SLinus Torvalds  * This option implies MREMAP_MAYMOVE.
9141da177e4SLinus Torvalds  */
SYSCALL_DEFINE5(mremap,unsigned long,addr,unsigned long,old_len,unsigned long,new_len,unsigned long,flags,unsigned long,new_addr)91563a81db1SAl Viro SYSCALL_DEFINE5(mremap, unsigned long, addr, unsigned long, old_len,
91663a81db1SAl Viro 		unsigned long, new_len, unsigned long, flags,
91763a81db1SAl Viro 		unsigned long, new_addr)
9181da177e4SLinus Torvalds {
919d0de32d9SHugh Dickins 	struct mm_struct *mm = current->mm;
9201da177e4SLinus Torvalds 	struct vm_area_struct *vma;
9211da177e4SLinus Torvalds 	unsigned long ret = -EINVAL;
92281909b84SMichel Lespinasse 	bool locked = false;
92372f87654SPavel Emelyanov 	struct vm_userfaultfd_ctx uf = NULL_VM_UFFD_CTX;
924b2282371SMike Rapoport 	LIST_HEAD(uf_unmap_early);
925897ab3e0SMike Rapoport 	LIST_HEAD(uf_unmap);
9261da177e4SLinus Torvalds 
927b2a84de2SWill Deacon 	/*
928b2a84de2SWill Deacon 	 * There is a deliberate asymmetry here: we strip the pointer tag
929b2a84de2SWill Deacon 	 * from the old address but leave the new address alone. This is
930b2a84de2SWill Deacon 	 * for consistency with mmap(), where we prevent the creation of
931b2a84de2SWill Deacon 	 * aliasing mappings in userspace by leaving the tag bits of the
932b2a84de2SWill Deacon 	 * mapping address intact. A non-zero tag will cause the subsequent
933b2a84de2SWill Deacon 	 * range checks to reject the address as invalid.
934b2a84de2SWill Deacon 	 *
935c3003405SJonathan Corbet 	 * See Documentation/arch/arm64/tagged-address-abi.rst for more
936c3003405SJonathan Corbet 	 * information.
937b2a84de2SWill Deacon 	 */
938057d3389SAndrey Konovalov 	addr = untagged_addr(addr);
939057d3389SAndrey Konovalov 
940e346b381SBrian Geffon 	if (flags & ~(MREMAP_FIXED | MREMAP_MAYMOVE | MREMAP_DONTUNMAP))
9419a2458a6SRasmus Villemoes 		return ret;
9429a2458a6SRasmus Villemoes 
9439a2458a6SRasmus Villemoes 	if (flags & MREMAP_FIXED && !(flags & MREMAP_MAYMOVE))
9449a2458a6SRasmus Villemoes 		return ret;
9451da177e4SLinus Torvalds 
946e346b381SBrian Geffon 	/*
947e346b381SBrian Geffon 	 * MREMAP_DONTUNMAP is always a move and it does not allow resizing
948e346b381SBrian Geffon 	 * in the process.
949e346b381SBrian Geffon 	 */
950e346b381SBrian Geffon 	if (flags & MREMAP_DONTUNMAP &&
951e346b381SBrian Geffon 			(!(flags & MREMAP_MAYMOVE) || old_len != new_len))
952e346b381SBrian Geffon 		return ret;
953e346b381SBrian Geffon 
954e346b381SBrian Geffon 
955f19cb115SAlexander Kuleshov 	if (offset_in_page(addr))
9569a2458a6SRasmus Villemoes 		return ret;
9571da177e4SLinus Torvalds 
9581da177e4SLinus Torvalds 	old_len = PAGE_ALIGN(old_len);
9591da177e4SLinus Torvalds 	new_len = PAGE_ALIGN(new_len);
9601da177e4SLinus Torvalds 
9611da177e4SLinus Torvalds 	/*
9621da177e4SLinus Torvalds 	 * We allow a zero old-len as a special case
9631da177e4SLinus Torvalds 	 * for DOS-emu "duplicate shm area" thing. But
9641da177e4SLinus Torvalds 	 * a zero new-len is nonsensical.
9651da177e4SLinus Torvalds 	 */
9661da177e4SLinus Torvalds 	if (!new_len)
9679a2458a6SRasmus Villemoes 		return ret;
9689a2458a6SRasmus Villemoes 
969d8ed45c5SMichel Lespinasse 	if (mmap_write_lock_killable(current->mm))
970dc0ef0dfSMichal Hocko 		return -EINTR;
9710e6799dbSMiaohe Lin 	vma = vma_lookup(mm, addr);
9720e6799dbSMiaohe Lin 	if (!vma) {
9737d1e6496SNiels Dossche 		ret = -EFAULT;
974550a7d60SMina Almasry 		goto out;
975550a7d60SMina Almasry 	}
976550a7d60SMina Almasry 
977550a7d60SMina Almasry 	if (is_vm_hugetlb_page(vma)) {
978550a7d60SMina Almasry 		struct hstate *h __maybe_unused = hstate_vma(vma);
979550a7d60SMina Almasry 
980550a7d60SMina Almasry 		old_len = ALIGN(old_len, huge_page_size(h));
981550a7d60SMina Almasry 		new_len = ALIGN(new_len, huge_page_size(h));
982550a7d60SMina Almasry 
983550a7d60SMina Almasry 		/* addrs must be huge page aligned */
984550a7d60SMina Almasry 		if (addr & ~huge_page_mask(h))
985550a7d60SMina Almasry 			goto out;
986550a7d60SMina Almasry 		if (new_addr & ~huge_page_mask(h))
987550a7d60SMina Almasry 			goto out;
988550a7d60SMina Almasry 
989550a7d60SMina Almasry 		/*
990550a7d60SMina Almasry 		 * Don't allow remap expansion, because the underlying hugetlb
991550a7d60SMina Almasry 		 * reservation is not yet capable to handle split reservation.
992550a7d60SMina Almasry 		 */
993550a7d60SMina Almasry 		if (new_len > old_len)
994550a7d60SMina Almasry 			goto out;
995550a7d60SMina Almasry 	}
9961da177e4SLinus Torvalds 
997e346b381SBrian Geffon 	if (flags & (MREMAP_FIXED | MREMAP_DONTUNMAP)) {
99881909b84SMichel Lespinasse 		ret = mremap_to(addr, old_len, new_addr, new_len,
999e346b381SBrian Geffon 				&locked, flags, &uf, &uf_unmap_early,
1000e346b381SBrian Geffon 				&uf_unmap);
10011da177e4SLinus Torvalds 		goto out;
10021da177e4SLinus Torvalds 	}
10031da177e4SLinus Torvalds 
10041da177e4SLinus Torvalds 	/*
10051da177e4SLinus Torvalds 	 * Always allow a shrinking remap: that just unmaps
10061da177e4SLinus Torvalds 	 * the unnecessary pages..
1007183654ceSLiam R. Howlett 	 * do_vmi_munmap does all the needed commit accounting, and
1008408579cdSLiam R. Howlett 	 * unlocks the mmap_lock if so directed.
10091da177e4SLinus Torvalds 	 */
10101da177e4SLinus Torvalds 	if (old_len >= new_len) {
1011183654ceSLiam R. Howlett 		VMA_ITERATOR(vmi, mm, addr + new_len);
101285a06835SYang Shi 
1013408579cdSLiam R. Howlett 		if (old_len == new_len) {
1014408579cdSLiam R. Howlett 			ret = addr;
10151da177e4SLinus Torvalds 			goto out;
101611f9a21aSLiam R. Howlett 		}
101711f9a21aSLiam R. Howlett 
1018408579cdSLiam R. Howlett 		ret = do_vmi_munmap(&vmi, mm, addr + new_len, old_len - new_len,
1019408579cdSLiam R. Howlett 				    &uf_unmap, true);
1020408579cdSLiam R. Howlett 		if (ret)
10211da177e4SLinus Torvalds 			goto out;
1022408579cdSLiam R. Howlett 
1023408579cdSLiam R. Howlett 		ret = addr;
1024408579cdSLiam R. Howlett 		goto out_unlocked;
10251da177e4SLinus Torvalds 	}
10261da177e4SLinus Torvalds 
10271da177e4SLinus Torvalds 	/*
1028ecc1a899SAl Viro 	 * Ok, we need to grow..
10291da177e4SLinus Torvalds 	 */
1030fdbef614SDmitry Safonov 	vma = vma_to_resize(addr, old_len, new_len, flags);
103154f5de70SAl Viro 	if (IS_ERR(vma)) {
103254f5de70SAl Viro 		ret = PTR_ERR(vma);
10331da177e4SLinus Torvalds 		goto out;
10341da177e4SLinus Torvalds 	}
10351da177e4SLinus Torvalds 
10361da177e4SLinus Torvalds 	/* old_len exactly to the end of the area..
10371da177e4SLinus Torvalds 	 */
1038ecc1a899SAl Viro 	if (old_len == vma->vm_end - addr) {
10391da177e4SLinus Torvalds 		/* can we just expand the current mapping? */
10401a0ef85fSAl Viro 		if (vma_expandable(vma, new_len - old_len)) {
1041fdbef614SDmitry Safonov 			long pages = (new_len - old_len) >> PAGE_SHIFT;
1042ca3d76b0SJakub Matěna 			unsigned long extension_start = addr + old_len;
1043ca3d76b0SJakub Matěna 			unsigned long extension_end = addr + new_len;
10446f12be79SVlastimil Babka 			pgoff_t extension_pgoff = vma->vm_pgoff +
10456f12be79SVlastimil Babka 				((extension_start - vma->vm_start) >> PAGE_SHIFT);
1046a27a11f9SLiam R. Howlett 			VMA_ITERATOR(vmi, mm, extension_start);
1047fdbef614SDmitry Safonov 
1048fdbef614SDmitry Safonov 			if (vma->vm_flags & VM_ACCOUNT) {
1049fdbef614SDmitry Safonov 				if (security_vm_enough_memory_mm(mm, pages)) {
1050fdbef614SDmitry Safonov 					ret = -ENOMEM;
1051fdbef614SDmitry Safonov 					goto out;
1052fdbef614SDmitry Safonov 				}
1053fdbef614SDmitry Safonov 			}
10541da177e4SLinus Torvalds 
1055ca3d76b0SJakub Matěna 			/*
1056d014cd7cSVlastimil Babka 			 * Function vma_merge() is called on the extension we
1057d014cd7cSVlastimil Babka 			 * are adding to the already existing vma, vma_merge()
1058d014cd7cSVlastimil Babka 			 * will merge this extension with the already existing
1059d014cd7cSVlastimil Babka 			 * vma (expand operation itself) and possibly also with
1060d014cd7cSVlastimil Babka 			 * the next vma if it becomes adjacent to the expanded
1061d014cd7cSVlastimil Babka 			 * vma and  otherwise compatible.
1062ca3d76b0SJakub Matěna 			 */
10639760ebffSLiam R. Howlett 			vma = vma_merge(&vmi, mm, vma, extension_start,
10649760ebffSLiam R. Howlett 				extension_end, vma->vm_flags, vma->anon_vma,
10659760ebffSLiam R. Howlett 				vma->vm_file, extension_pgoff, vma_policy(vma),
10669760ebffSLiam R. Howlett 				vma->vm_userfaultfd_ctx, anon_vma_name(vma));
1067ca3d76b0SJakub Matěna 			if (!vma) {
1068fdbef614SDmitry Safonov 				vm_unacct_memory(pages);
10695beb4930SRik van Riel 				ret = -ENOMEM;
10705beb4930SRik van Riel 				goto out;
10715beb4930SRik van Riel 			}
10721da177e4SLinus Torvalds 
107384638335SKonstantin Khlebnikov 			vm_stat_account(mm, vma->vm_flags, pages);
10741da177e4SLinus Torvalds 			if (vma->vm_flags & VM_LOCKED) {
1075d0de32d9SHugh Dickins 				mm->locked_vm += pages;
107681909b84SMichel Lespinasse 				locked = true;
107781909b84SMichel Lespinasse 				new_addr = addr;
10781da177e4SLinus Torvalds 			}
10791da177e4SLinus Torvalds 			ret = addr;
10801da177e4SLinus Torvalds 			goto out;
10811da177e4SLinus Torvalds 		}
10821da177e4SLinus Torvalds 	}
10831da177e4SLinus Torvalds 
10841da177e4SLinus Torvalds 	/*
10851da177e4SLinus Torvalds 	 * We weren't able to just expand or shrink the area,
10861da177e4SLinus Torvalds 	 * we need to create a new one and move it..
10871da177e4SLinus Torvalds 	 */
10881da177e4SLinus Torvalds 	ret = -ENOMEM;
10891da177e4SLinus Torvalds 	if (flags & MREMAP_MAYMOVE) {
10901da177e4SLinus Torvalds 		unsigned long map_flags = 0;
10911da177e4SLinus Torvalds 		if (vma->vm_flags & VM_MAYSHARE)
10921da177e4SLinus Torvalds 			map_flags |= MAP_SHARED;
10931da177e4SLinus Torvalds 
10941da177e4SLinus Torvalds 		new_addr = get_unmapped_area(vma->vm_file, 0, new_len,
109593587414SAl Viro 					vma->vm_pgoff +
109693587414SAl Viro 					((addr - vma->vm_start) >> PAGE_SHIFT),
109793587414SAl Viro 					map_flags);
1098ff68dac6SGaowei Pu 		if (IS_ERR_VALUE(new_addr)) {
10991da177e4SLinus Torvalds 			ret = new_addr;
1100ed032189SEric Paris 			goto out;
1101ed032189SEric Paris 		}
1102ed032189SEric Paris 
110372f87654SPavel Emelyanov 		ret = move_vma(vma, addr, old_len, new_len, new_addr,
1104e346b381SBrian Geffon 			       &locked, flags, &uf, &uf_unmap);
11051da177e4SLinus Torvalds 	}
11061da177e4SLinus Torvalds out:
1107fdbef614SDmitry Safonov 	if (offset_in_page(ret))
1108fa1f68ccSZou Wei 		locked = false;
1109d8ed45c5SMichel Lespinasse 	mmap_write_unlock(current->mm);
111081909b84SMichel Lespinasse 	if (locked && new_len > old_len)
111181909b84SMichel Lespinasse 		mm_populate(new_addr + old_len, new_len - old_len);
1112408579cdSLiam R. Howlett out_unlocked:
1113b2282371SMike Rapoport 	userfaultfd_unmap_complete(mm, &uf_unmap_early);
1114d1564926SBrian Geffon 	mremap_userfaultfd_complete(&uf, addr, ret, old_len);
1115897ab3e0SMike Rapoport 	userfaultfd_unmap_complete(mm, &uf_unmap);
11161da177e4SLinus Torvalds 	return ret;
11171da177e4SLinus Torvalds }
1118