xref: /linux/mm/vma.c (revision c4d91e225ff3c9821c85ac6efd8e02c0025c0190)
149b1b8d6SLorenzo Stoakes // SPDX-License-Identifier: GPL-2.0-or-later
249b1b8d6SLorenzo Stoakes 
349b1b8d6SLorenzo Stoakes /*
449b1b8d6SLorenzo Stoakes  * VMA-specific functions.
549b1b8d6SLorenzo Stoakes  */
649b1b8d6SLorenzo Stoakes 
749b1b8d6SLorenzo Stoakes #include "vma_internal.h"
849b1b8d6SLorenzo Stoakes #include "vma.h"
949b1b8d6SLorenzo Stoakes 
102f1c6611SLorenzo Stoakes static inline bool is_mergeable_vma(struct vma_merge_struct *vmg, bool merge_next)
1149b1b8d6SLorenzo Stoakes {
122f1c6611SLorenzo Stoakes 	struct vm_area_struct *vma = merge_next ? vmg->next : vmg->prev;
132f1c6611SLorenzo Stoakes 
143e01310dSLorenzo Stoakes 	if (!mpol_equal(vmg->policy, vma_policy(vma)))
153e01310dSLorenzo Stoakes 		return false;
1649b1b8d6SLorenzo Stoakes 	/*
1749b1b8d6SLorenzo Stoakes 	 * VM_SOFTDIRTY should not prevent from VMA merging, if we
1849b1b8d6SLorenzo Stoakes 	 * match the flags but dirty bit -- the caller should mark
1949b1b8d6SLorenzo Stoakes 	 * merged VMA as dirty. If dirty bit won't be excluded from
2049b1b8d6SLorenzo Stoakes 	 * comparison, we increase pressure on the memory system forcing
2149b1b8d6SLorenzo Stoakes 	 * the kernel to generate new VMAs when old one could be
2249b1b8d6SLorenzo Stoakes 	 * extended instead.
2349b1b8d6SLorenzo Stoakes 	 */
242f1c6611SLorenzo Stoakes 	if ((vma->vm_flags ^ vmg->flags) & ~VM_SOFTDIRTY)
2549b1b8d6SLorenzo Stoakes 		return false;
262f1c6611SLorenzo Stoakes 	if (vma->vm_file != vmg->file)
2749b1b8d6SLorenzo Stoakes 		return false;
282f1c6611SLorenzo Stoakes 	if (!is_mergeable_vm_userfaultfd_ctx(vma, vmg->uffd_ctx))
2949b1b8d6SLorenzo Stoakes 		return false;
302f1c6611SLorenzo Stoakes 	if (!anon_vma_name_eq(anon_vma_name(vma), vmg->anon_name))
3149b1b8d6SLorenzo Stoakes 		return false;
3249b1b8d6SLorenzo Stoakes 	return true;
3349b1b8d6SLorenzo Stoakes }
3449b1b8d6SLorenzo Stoakes 
3549b1b8d6SLorenzo Stoakes static inline bool is_mergeable_anon_vma(struct anon_vma *anon_vma1,
3649b1b8d6SLorenzo Stoakes 		 struct anon_vma *anon_vma2, struct vm_area_struct *vma)
3749b1b8d6SLorenzo Stoakes {
3849b1b8d6SLorenzo Stoakes 	/*
3949b1b8d6SLorenzo Stoakes 	 * The list_is_singular() test is to avoid merging VMA cloned from
4049b1b8d6SLorenzo Stoakes 	 * parents. This can improve scalability caused by anon_vma lock.
4149b1b8d6SLorenzo Stoakes 	 */
4249b1b8d6SLorenzo Stoakes 	if ((!anon_vma1 || !anon_vma2) && (!vma ||
4349b1b8d6SLorenzo Stoakes 		list_is_singular(&vma->anon_vma_chain)))
4449b1b8d6SLorenzo Stoakes 		return true;
4549b1b8d6SLorenzo Stoakes 	return anon_vma1 == anon_vma2;
4649b1b8d6SLorenzo Stoakes }
4749b1b8d6SLorenzo Stoakes 
48cacded5eSLorenzo Stoakes /* Are the anon_vma's belonging to each VMA compatible with one another? */
49cacded5eSLorenzo Stoakes static inline bool are_anon_vmas_compatible(struct vm_area_struct *vma1,
50cacded5eSLorenzo Stoakes 					    struct vm_area_struct *vma2)
51cacded5eSLorenzo Stoakes {
52cacded5eSLorenzo Stoakes 	return is_mergeable_anon_vma(vma1->anon_vma, vma2->anon_vma, NULL);
53cacded5eSLorenzo Stoakes }
54cacded5eSLorenzo Stoakes 
5549b1b8d6SLorenzo Stoakes /*
5649b1b8d6SLorenzo Stoakes  * init_multi_vma_prep() - Initializer for struct vma_prepare
5749b1b8d6SLorenzo Stoakes  * @vp: The vma_prepare struct
5849b1b8d6SLorenzo Stoakes  * @vma: The vma that will be altered once locked
5949b1b8d6SLorenzo Stoakes  * @next: The next vma if it is to be adjusted
6049b1b8d6SLorenzo Stoakes  * @remove: The first vma to be removed
6149b1b8d6SLorenzo Stoakes  * @remove2: The second vma to be removed
6249b1b8d6SLorenzo Stoakes  */
6349b1b8d6SLorenzo Stoakes static void init_multi_vma_prep(struct vma_prepare *vp,
6449b1b8d6SLorenzo Stoakes 				struct vm_area_struct *vma,
6549b1b8d6SLorenzo Stoakes 				struct vm_area_struct *next,
6649b1b8d6SLorenzo Stoakes 				struct vm_area_struct *remove,
6749b1b8d6SLorenzo Stoakes 				struct vm_area_struct *remove2)
6849b1b8d6SLorenzo Stoakes {
6949b1b8d6SLorenzo Stoakes 	memset(vp, 0, sizeof(struct vma_prepare));
7049b1b8d6SLorenzo Stoakes 	vp->vma = vma;
7149b1b8d6SLorenzo Stoakes 	vp->anon_vma = vma->anon_vma;
7249b1b8d6SLorenzo Stoakes 	vp->remove = remove;
7349b1b8d6SLorenzo Stoakes 	vp->remove2 = remove2;
7449b1b8d6SLorenzo Stoakes 	vp->adj_next = next;
7549b1b8d6SLorenzo Stoakes 	if (!vp->anon_vma && next)
7649b1b8d6SLorenzo Stoakes 		vp->anon_vma = next->anon_vma;
7749b1b8d6SLorenzo Stoakes 
7849b1b8d6SLorenzo Stoakes 	vp->file = vma->vm_file;
7949b1b8d6SLorenzo Stoakes 	if (vp->file)
8049b1b8d6SLorenzo Stoakes 		vp->mapping = vma->vm_file->f_mapping;
8149b1b8d6SLorenzo Stoakes 
8249b1b8d6SLorenzo Stoakes }
8349b1b8d6SLorenzo Stoakes 
8449b1b8d6SLorenzo Stoakes /*
8549b1b8d6SLorenzo Stoakes  * Return true if we can merge this (vm_flags,anon_vma,file,vm_pgoff)
8649b1b8d6SLorenzo Stoakes  * in front of (at a lower virtual address and file offset than) the vma.
8749b1b8d6SLorenzo Stoakes  *
8849b1b8d6SLorenzo Stoakes  * We cannot merge two vmas if they have differently assigned (non-NULL)
8949b1b8d6SLorenzo Stoakes  * anon_vmas, nor if same anon_vma is assigned but offsets incompatible.
9049b1b8d6SLorenzo Stoakes  *
9149b1b8d6SLorenzo Stoakes  * We don't check here for the merged mmap wrapping around the end of pagecache
9249b1b8d6SLorenzo Stoakes  * indices (16TB on ia32) because do_mmap() does not permit mmap's which
9349b1b8d6SLorenzo Stoakes  * wrap, nor mmaps which cover the final page at index -1UL.
9449b1b8d6SLorenzo Stoakes  *
9549b1b8d6SLorenzo Stoakes  * We assume the vma may be removed as part of the merge.
9649b1b8d6SLorenzo Stoakes  */
9725d3925fSLorenzo Stoakes static bool can_vma_merge_before(struct vma_merge_struct *vmg)
9849b1b8d6SLorenzo Stoakes {
992f1c6611SLorenzo Stoakes 	pgoff_t pglen = PHYS_PFN(vmg->end - vmg->start);
1002f1c6611SLorenzo Stoakes 
1012f1c6611SLorenzo Stoakes 	if (is_mergeable_vma(vmg, /* merge_next = */ true) &&
1022f1c6611SLorenzo Stoakes 	    is_mergeable_anon_vma(vmg->anon_vma, vmg->next->anon_vma, vmg->next)) {
1032f1c6611SLorenzo Stoakes 		if (vmg->next->vm_pgoff == vmg->pgoff + pglen)
10449b1b8d6SLorenzo Stoakes 			return true;
10549b1b8d6SLorenzo Stoakes 	}
1062f1c6611SLorenzo Stoakes 
10749b1b8d6SLorenzo Stoakes 	return false;
10849b1b8d6SLorenzo Stoakes }
10949b1b8d6SLorenzo Stoakes 
11049b1b8d6SLorenzo Stoakes /*
11149b1b8d6SLorenzo Stoakes  * Return true if we can merge this (vm_flags,anon_vma,file,vm_pgoff)
11249b1b8d6SLorenzo Stoakes  * beyond (at a higher virtual address and file offset than) the vma.
11349b1b8d6SLorenzo Stoakes  *
11449b1b8d6SLorenzo Stoakes  * We cannot merge two vmas if they have differently assigned (non-NULL)
11549b1b8d6SLorenzo Stoakes  * anon_vmas, nor if same anon_vma is assigned but offsets incompatible.
11649b1b8d6SLorenzo Stoakes  *
11749b1b8d6SLorenzo Stoakes  * We assume that vma is not removed as part of the merge.
11849b1b8d6SLorenzo Stoakes  */
11925d3925fSLorenzo Stoakes static bool can_vma_merge_after(struct vma_merge_struct *vmg)
12049b1b8d6SLorenzo Stoakes {
1212f1c6611SLorenzo Stoakes 	if (is_mergeable_vma(vmg, /* merge_next = */ false) &&
1222f1c6611SLorenzo Stoakes 	    is_mergeable_anon_vma(vmg->anon_vma, vmg->prev->anon_vma, vmg->prev)) {
1232f1c6611SLorenzo Stoakes 		if (vmg->prev->vm_pgoff + vma_pages(vmg->prev) == vmg->pgoff)
12449b1b8d6SLorenzo Stoakes 			return true;
12549b1b8d6SLorenzo Stoakes 	}
12649b1b8d6SLorenzo Stoakes 	return false;
12749b1b8d6SLorenzo Stoakes }
12849b1b8d6SLorenzo Stoakes 
12925d3925fSLorenzo Stoakes static void __vma_link_file(struct vm_area_struct *vma,
13025d3925fSLorenzo Stoakes 			    struct address_space *mapping)
13125d3925fSLorenzo Stoakes {
13225d3925fSLorenzo Stoakes 	if (vma_is_shared_maywrite(vma))
13325d3925fSLorenzo Stoakes 		mapping_allow_writable(mapping);
13425d3925fSLorenzo Stoakes 
13525d3925fSLorenzo Stoakes 	flush_dcache_mmap_lock(mapping);
13625d3925fSLorenzo Stoakes 	vma_interval_tree_insert(vma, &mapping->i_mmap);
13725d3925fSLorenzo Stoakes 	flush_dcache_mmap_unlock(mapping);
13825d3925fSLorenzo Stoakes }
13925d3925fSLorenzo Stoakes 
14025d3925fSLorenzo Stoakes /*
14125d3925fSLorenzo Stoakes  * Requires inode->i_mapping->i_mmap_rwsem
14225d3925fSLorenzo Stoakes  */
14325d3925fSLorenzo Stoakes static void __remove_shared_vm_struct(struct vm_area_struct *vma,
14425d3925fSLorenzo Stoakes 				      struct address_space *mapping)
14525d3925fSLorenzo Stoakes {
14625d3925fSLorenzo Stoakes 	if (vma_is_shared_maywrite(vma))
14725d3925fSLorenzo Stoakes 		mapping_unmap_writable(mapping);
14825d3925fSLorenzo Stoakes 
14925d3925fSLorenzo Stoakes 	flush_dcache_mmap_lock(mapping);
15025d3925fSLorenzo Stoakes 	vma_interval_tree_remove(vma, &mapping->i_mmap);
15125d3925fSLorenzo Stoakes 	flush_dcache_mmap_unlock(mapping);
15225d3925fSLorenzo Stoakes }
15325d3925fSLorenzo Stoakes 
15425d3925fSLorenzo Stoakes /*
15525d3925fSLorenzo Stoakes  * vma_prepare() - Helper function for handling locking VMAs prior to altering
15625d3925fSLorenzo Stoakes  * @vp: The initialized vma_prepare struct
15725d3925fSLorenzo Stoakes  */
15825d3925fSLorenzo Stoakes static void vma_prepare(struct vma_prepare *vp)
15925d3925fSLorenzo Stoakes {
16025d3925fSLorenzo Stoakes 	if (vp->file) {
16125d3925fSLorenzo Stoakes 		uprobe_munmap(vp->vma, vp->vma->vm_start, vp->vma->vm_end);
16225d3925fSLorenzo Stoakes 
16325d3925fSLorenzo Stoakes 		if (vp->adj_next)
16425d3925fSLorenzo Stoakes 			uprobe_munmap(vp->adj_next, vp->adj_next->vm_start,
16525d3925fSLorenzo Stoakes 				      vp->adj_next->vm_end);
16625d3925fSLorenzo Stoakes 
16725d3925fSLorenzo Stoakes 		i_mmap_lock_write(vp->mapping);
16825d3925fSLorenzo Stoakes 		if (vp->insert && vp->insert->vm_file) {
16925d3925fSLorenzo Stoakes 			/*
17025d3925fSLorenzo Stoakes 			 * Put into interval tree now, so instantiated pages
17125d3925fSLorenzo Stoakes 			 * are visible to arm/parisc __flush_dcache_page
17225d3925fSLorenzo Stoakes 			 * throughout; but we cannot insert into address
17325d3925fSLorenzo Stoakes 			 * space until vma start or end is updated.
17425d3925fSLorenzo Stoakes 			 */
17525d3925fSLorenzo Stoakes 			__vma_link_file(vp->insert,
17625d3925fSLorenzo Stoakes 					vp->insert->vm_file->f_mapping);
17725d3925fSLorenzo Stoakes 		}
17825d3925fSLorenzo Stoakes 	}
17925d3925fSLorenzo Stoakes 
18025d3925fSLorenzo Stoakes 	if (vp->anon_vma) {
18125d3925fSLorenzo Stoakes 		anon_vma_lock_write(vp->anon_vma);
18225d3925fSLorenzo Stoakes 		anon_vma_interval_tree_pre_update_vma(vp->vma);
18325d3925fSLorenzo Stoakes 		if (vp->adj_next)
18425d3925fSLorenzo Stoakes 			anon_vma_interval_tree_pre_update_vma(vp->adj_next);
18525d3925fSLorenzo Stoakes 	}
18625d3925fSLorenzo Stoakes 
18725d3925fSLorenzo Stoakes 	if (vp->file) {
18825d3925fSLorenzo Stoakes 		flush_dcache_mmap_lock(vp->mapping);
18925d3925fSLorenzo Stoakes 		vma_interval_tree_remove(vp->vma, &vp->mapping->i_mmap);
19025d3925fSLorenzo Stoakes 		if (vp->adj_next)
19125d3925fSLorenzo Stoakes 			vma_interval_tree_remove(vp->adj_next,
19225d3925fSLorenzo Stoakes 						 &vp->mapping->i_mmap);
19325d3925fSLorenzo Stoakes 	}
19425d3925fSLorenzo Stoakes 
19525d3925fSLorenzo Stoakes }
19625d3925fSLorenzo Stoakes 
19725d3925fSLorenzo Stoakes /*
19825d3925fSLorenzo Stoakes  * vma_complete- Helper function for handling the unlocking after altering VMAs,
19925d3925fSLorenzo Stoakes  * or for inserting a VMA.
20025d3925fSLorenzo Stoakes  *
20125d3925fSLorenzo Stoakes  * @vp: The vma_prepare struct
20225d3925fSLorenzo Stoakes  * @vmi: The vma iterator
20325d3925fSLorenzo Stoakes  * @mm: The mm_struct
20425d3925fSLorenzo Stoakes  */
20525d3925fSLorenzo Stoakes static void vma_complete(struct vma_prepare *vp, struct vma_iterator *vmi,
20625d3925fSLorenzo Stoakes 			 struct mm_struct *mm)
20725d3925fSLorenzo Stoakes {
20825d3925fSLorenzo Stoakes 	if (vp->file) {
20925d3925fSLorenzo Stoakes 		if (vp->adj_next)
21025d3925fSLorenzo Stoakes 			vma_interval_tree_insert(vp->adj_next,
21125d3925fSLorenzo Stoakes 						 &vp->mapping->i_mmap);
21225d3925fSLorenzo Stoakes 		vma_interval_tree_insert(vp->vma, &vp->mapping->i_mmap);
21325d3925fSLorenzo Stoakes 		flush_dcache_mmap_unlock(vp->mapping);
21425d3925fSLorenzo Stoakes 	}
21525d3925fSLorenzo Stoakes 
21625d3925fSLorenzo Stoakes 	if (vp->remove && vp->file) {
21725d3925fSLorenzo Stoakes 		__remove_shared_vm_struct(vp->remove, vp->mapping);
21825d3925fSLorenzo Stoakes 		if (vp->remove2)
21925d3925fSLorenzo Stoakes 			__remove_shared_vm_struct(vp->remove2, vp->mapping);
22025d3925fSLorenzo Stoakes 	} else if (vp->insert) {
22125d3925fSLorenzo Stoakes 		/*
22225d3925fSLorenzo Stoakes 		 * split_vma has split insert from vma, and needs
22325d3925fSLorenzo Stoakes 		 * us to insert it before dropping the locks
22425d3925fSLorenzo Stoakes 		 * (it may either follow vma or precede it).
22525d3925fSLorenzo Stoakes 		 */
22625d3925fSLorenzo Stoakes 		vma_iter_store(vmi, vp->insert);
22725d3925fSLorenzo Stoakes 		mm->map_count++;
22825d3925fSLorenzo Stoakes 	}
22925d3925fSLorenzo Stoakes 
23025d3925fSLorenzo Stoakes 	if (vp->anon_vma) {
23125d3925fSLorenzo Stoakes 		anon_vma_interval_tree_post_update_vma(vp->vma);
23225d3925fSLorenzo Stoakes 		if (vp->adj_next)
23325d3925fSLorenzo Stoakes 			anon_vma_interval_tree_post_update_vma(vp->adj_next);
23425d3925fSLorenzo Stoakes 		anon_vma_unlock_write(vp->anon_vma);
23525d3925fSLorenzo Stoakes 	}
23625d3925fSLorenzo Stoakes 
23725d3925fSLorenzo Stoakes 	if (vp->file) {
23825d3925fSLorenzo Stoakes 		i_mmap_unlock_write(vp->mapping);
23925d3925fSLorenzo Stoakes 		uprobe_mmap(vp->vma);
24025d3925fSLorenzo Stoakes 
24125d3925fSLorenzo Stoakes 		if (vp->adj_next)
24225d3925fSLorenzo Stoakes 			uprobe_mmap(vp->adj_next);
24325d3925fSLorenzo Stoakes 	}
24425d3925fSLorenzo Stoakes 
24525d3925fSLorenzo Stoakes 	if (vp->remove) {
24625d3925fSLorenzo Stoakes again:
24725d3925fSLorenzo Stoakes 		vma_mark_detached(vp->remove, true);
24825d3925fSLorenzo Stoakes 		if (vp->file) {
24925d3925fSLorenzo Stoakes 			uprobe_munmap(vp->remove, vp->remove->vm_start,
25025d3925fSLorenzo Stoakes 				      vp->remove->vm_end);
25125d3925fSLorenzo Stoakes 			fput(vp->file);
25225d3925fSLorenzo Stoakes 		}
25325d3925fSLorenzo Stoakes 		if (vp->remove->anon_vma)
25425d3925fSLorenzo Stoakes 			anon_vma_merge(vp->vma, vp->remove);
25525d3925fSLorenzo Stoakes 		mm->map_count--;
25625d3925fSLorenzo Stoakes 		mpol_put(vma_policy(vp->remove));
25725d3925fSLorenzo Stoakes 		if (!vp->remove2)
25825d3925fSLorenzo Stoakes 			WARN_ON_ONCE(vp->vma->vm_end < vp->remove->vm_end);
25925d3925fSLorenzo Stoakes 		vm_area_free(vp->remove);
26025d3925fSLorenzo Stoakes 
26125d3925fSLorenzo Stoakes 		/*
26225d3925fSLorenzo Stoakes 		 * In mprotect's case 6 (see comments on vma_merge),
26325d3925fSLorenzo Stoakes 		 * we are removing both mid and next vmas
26425d3925fSLorenzo Stoakes 		 */
26525d3925fSLorenzo Stoakes 		if (vp->remove2) {
26625d3925fSLorenzo Stoakes 			vp->remove = vp->remove2;
26725d3925fSLorenzo Stoakes 			vp->remove2 = NULL;
26825d3925fSLorenzo Stoakes 			goto again;
26925d3925fSLorenzo Stoakes 		}
27025d3925fSLorenzo Stoakes 	}
27125d3925fSLorenzo Stoakes 	if (vp->insert && vp->file)
27225d3925fSLorenzo Stoakes 		uprobe_mmap(vp->insert);
27325d3925fSLorenzo Stoakes }
27425d3925fSLorenzo Stoakes 
27525d3925fSLorenzo Stoakes /*
27625d3925fSLorenzo Stoakes  * init_vma_prep() - Initializer wrapper for vma_prepare struct
27725d3925fSLorenzo Stoakes  * @vp: The vma_prepare struct
27825d3925fSLorenzo Stoakes  * @vma: The vma that will be altered once locked
27925d3925fSLorenzo Stoakes  */
28025d3925fSLorenzo Stoakes static void init_vma_prep(struct vma_prepare *vp, struct vm_area_struct *vma)
28125d3925fSLorenzo Stoakes {
28225d3925fSLorenzo Stoakes 	init_multi_vma_prep(vp, vma, NULL, NULL, NULL);
28325d3925fSLorenzo Stoakes }
28425d3925fSLorenzo Stoakes 
28549b1b8d6SLorenzo Stoakes /*
286cacded5eSLorenzo Stoakes  * Can the proposed VMA be merged with the left (previous) VMA taking into
287cacded5eSLorenzo Stoakes  * account the start position of the proposed range.
288cacded5eSLorenzo Stoakes  */
289cacded5eSLorenzo Stoakes static bool can_vma_merge_left(struct vma_merge_struct *vmg)
290cacded5eSLorenzo Stoakes 
291cacded5eSLorenzo Stoakes {
292cacded5eSLorenzo Stoakes 	return vmg->prev && vmg->prev->vm_end == vmg->start &&
293cacded5eSLorenzo Stoakes 		can_vma_merge_after(vmg);
294cacded5eSLorenzo Stoakes }
295cacded5eSLorenzo Stoakes 
296cacded5eSLorenzo Stoakes /*
297cacded5eSLorenzo Stoakes  * Can the proposed VMA be merged with the right (next) VMA taking into
298cacded5eSLorenzo Stoakes  * account the end position of the proposed range.
299cacded5eSLorenzo Stoakes  *
300cacded5eSLorenzo Stoakes  * In addition, if we can merge with the left VMA, ensure that left and right
301cacded5eSLorenzo Stoakes  * anon_vma's are also compatible.
302cacded5eSLorenzo Stoakes  */
303cacded5eSLorenzo Stoakes static bool can_vma_merge_right(struct vma_merge_struct *vmg,
304cacded5eSLorenzo Stoakes 				bool can_merge_left)
305cacded5eSLorenzo Stoakes {
306cacded5eSLorenzo Stoakes 	if (!vmg->next || vmg->end != vmg->next->vm_start ||
307cacded5eSLorenzo Stoakes 	    !can_vma_merge_before(vmg))
308cacded5eSLorenzo Stoakes 		return false;
309cacded5eSLorenzo Stoakes 
310cacded5eSLorenzo Stoakes 	if (!can_merge_left)
311cacded5eSLorenzo Stoakes 		return true;
312cacded5eSLorenzo Stoakes 
313cacded5eSLorenzo Stoakes 	/*
314cacded5eSLorenzo Stoakes 	 * If we can merge with prev (left) and next (right), indicating that
315cacded5eSLorenzo Stoakes 	 * each VMA's anon_vma is compatible with the proposed anon_vma, this
316cacded5eSLorenzo Stoakes 	 * does not mean prev and next are compatible with EACH OTHER.
317cacded5eSLorenzo Stoakes 	 *
318cacded5eSLorenzo Stoakes 	 * We therefore check this in addition to mergeability to either side.
319cacded5eSLorenzo Stoakes 	 */
320cacded5eSLorenzo Stoakes 	return are_anon_vmas_compatible(vmg->prev, vmg->next);
321cacded5eSLorenzo Stoakes }
322cacded5eSLorenzo Stoakes 
323cacded5eSLorenzo Stoakes /*
32449b1b8d6SLorenzo Stoakes  * Close a vm structure and free it.
32549b1b8d6SLorenzo Stoakes  */
326f8d112a4SLiam R. Howlett void remove_vma(struct vm_area_struct *vma, bool unreachable, bool closed)
32749b1b8d6SLorenzo Stoakes {
32849b1b8d6SLorenzo Stoakes 	might_sleep();
329f8d112a4SLiam R. Howlett 	if (!closed && vma->vm_ops && vma->vm_ops->close)
33049b1b8d6SLorenzo Stoakes 		vma->vm_ops->close(vma);
33149b1b8d6SLorenzo Stoakes 	if (vma->vm_file)
33249b1b8d6SLorenzo Stoakes 		fput(vma->vm_file);
33349b1b8d6SLorenzo Stoakes 	mpol_put(vma_policy(vma));
33449b1b8d6SLorenzo Stoakes 	if (unreachable)
33549b1b8d6SLorenzo Stoakes 		__vm_area_free(vma);
33649b1b8d6SLorenzo Stoakes 	else
33749b1b8d6SLorenzo Stoakes 		vm_area_free(vma);
33849b1b8d6SLorenzo Stoakes }
33949b1b8d6SLorenzo Stoakes 
34049b1b8d6SLorenzo Stoakes /*
34149b1b8d6SLorenzo Stoakes  * Get rid of page table information in the indicated region.
34249b1b8d6SLorenzo Stoakes  *
34349b1b8d6SLorenzo Stoakes  * Called with the mm semaphore held.
34449b1b8d6SLorenzo Stoakes  */
34594f59ea5SLiam R. Howlett void unmap_region(struct ma_state *mas, struct vm_area_struct *vma,
34694f59ea5SLiam R. Howlett 		struct vm_area_struct *prev, struct vm_area_struct *next)
34749b1b8d6SLorenzo Stoakes {
34894f59ea5SLiam R. Howlett 	struct mm_struct *mm = vma->vm_mm;
34949b1b8d6SLorenzo Stoakes 	struct mmu_gather tlb;
35049b1b8d6SLorenzo Stoakes 
35149b1b8d6SLorenzo Stoakes 	lru_add_drain();
35249b1b8d6SLorenzo Stoakes 	tlb_gather_mmu(&tlb, mm);
35349b1b8d6SLorenzo Stoakes 	update_hiwater_rss(mm);
35494f59ea5SLiam R. Howlett 	unmap_vmas(&tlb, mas, vma, vma->vm_start, vma->vm_end, vma->vm_end,
35594f59ea5SLiam R. Howlett 		   /* mm_wr_locked = */ true);
35694f59ea5SLiam R. Howlett 	mas_set(mas, vma->vm_end);
35749b1b8d6SLorenzo Stoakes 	free_pgtables(&tlb, mas, vma, prev ? prev->vm_end : FIRST_USER_ADDRESS,
35849b1b8d6SLorenzo Stoakes 		      next ? next->vm_start : USER_PGTABLES_CEILING,
35994f59ea5SLiam R. Howlett 		      /* mm_wr_locked = */ true);
36049b1b8d6SLorenzo Stoakes 	tlb_finish_mmu(&tlb);
36149b1b8d6SLorenzo Stoakes }
36249b1b8d6SLorenzo Stoakes 
36349b1b8d6SLorenzo Stoakes /*
36449b1b8d6SLorenzo Stoakes  * __split_vma() bypasses sysctl_max_map_count checking.  We use this where it
36549b1b8d6SLorenzo Stoakes  * has already been checked or doesn't make sense to fail.
366b7012d51SLiam R. Howlett  * VMA Iterator will point to the original VMA.
36749b1b8d6SLorenzo Stoakes  */
36849b1b8d6SLorenzo Stoakes static int __split_vma(struct vma_iterator *vmi, struct vm_area_struct *vma,
36949b1b8d6SLorenzo Stoakes 		       unsigned long addr, int new_below)
37049b1b8d6SLorenzo Stoakes {
37149b1b8d6SLorenzo Stoakes 	struct vma_prepare vp;
37249b1b8d6SLorenzo Stoakes 	struct vm_area_struct *new;
37349b1b8d6SLorenzo Stoakes 	int err;
37449b1b8d6SLorenzo Stoakes 
37549b1b8d6SLorenzo Stoakes 	WARN_ON(vma->vm_start >= addr);
37649b1b8d6SLorenzo Stoakes 	WARN_ON(vma->vm_end <= addr);
37749b1b8d6SLorenzo Stoakes 
37849b1b8d6SLorenzo Stoakes 	if (vma->vm_ops && vma->vm_ops->may_split) {
37949b1b8d6SLorenzo Stoakes 		err = vma->vm_ops->may_split(vma, addr);
38049b1b8d6SLorenzo Stoakes 		if (err)
38149b1b8d6SLorenzo Stoakes 			return err;
38249b1b8d6SLorenzo Stoakes 	}
38349b1b8d6SLorenzo Stoakes 
38449b1b8d6SLorenzo Stoakes 	new = vm_area_dup(vma);
38549b1b8d6SLorenzo Stoakes 	if (!new)
38649b1b8d6SLorenzo Stoakes 		return -ENOMEM;
38749b1b8d6SLorenzo Stoakes 
38849b1b8d6SLorenzo Stoakes 	if (new_below) {
38949b1b8d6SLorenzo Stoakes 		new->vm_end = addr;
39049b1b8d6SLorenzo Stoakes 	} else {
39149b1b8d6SLorenzo Stoakes 		new->vm_start = addr;
39249b1b8d6SLorenzo Stoakes 		new->vm_pgoff += ((addr - vma->vm_start) >> PAGE_SHIFT);
39349b1b8d6SLorenzo Stoakes 	}
39449b1b8d6SLorenzo Stoakes 
39549b1b8d6SLorenzo Stoakes 	err = -ENOMEM;
39649b1b8d6SLorenzo Stoakes 	vma_iter_config(vmi, new->vm_start, new->vm_end);
39749b1b8d6SLorenzo Stoakes 	if (vma_iter_prealloc(vmi, new))
39849b1b8d6SLorenzo Stoakes 		goto out_free_vma;
39949b1b8d6SLorenzo Stoakes 
40049b1b8d6SLorenzo Stoakes 	err = vma_dup_policy(vma, new);
40149b1b8d6SLorenzo Stoakes 	if (err)
40249b1b8d6SLorenzo Stoakes 		goto out_free_vmi;
40349b1b8d6SLorenzo Stoakes 
40449b1b8d6SLorenzo Stoakes 	err = anon_vma_clone(new, vma);
40549b1b8d6SLorenzo Stoakes 	if (err)
40649b1b8d6SLorenzo Stoakes 		goto out_free_mpol;
40749b1b8d6SLorenzo Stoakes 
40849b1b8d6SLorenzo Stoakes 	if (new->vm_file)
40949b1b8d6SLorenzo Stoakes 		get_file(new->vm_file);
41049b1b8d6SLorenzo Stoakes 
41149b1b8d6SLorenzo Stoakes 	if (new->vm_ops && new->vm_ops->open)
41249b1b8d6SLorenzo Stoakes 		new->vm_ops->open(new);
41349b1b8d6SLorenzo Stoakes 
41449b1b8d6SLorenzo Stoakes 	vma_start_write(vma);
41549b1b8d6SLorenzo Stoakes 	vma_start_write(new);
41649b1b8d6SLorenzo Stoakes 
41749b1b8d6SLorenzo Stoakes 	init_vma_prep(&vp, vma);
41849b1b8d6SLorenzo Stoakes 	vp.insert = new;
41949b1b8d6SLorenzo Stoakes 	vma_prepare(&vp);
42049b1b8d6SLorenzo Stoakes 	vma_adjust_trans_huge(vma, vma->vm_start, addr, 0);
42149b1b8d6SLorenzo Stoakes 
42249b1b8d6SLorenzo Stoakes 	if (new_below) {
42349b1b8d6SLorenzo Stoakes 		vma->vm_start = addr;
42449b1b8d6SLorenzo Stoakes 		vma->vm_pgoff += (addr - new->vm_start) >> PAGE_SHIFT;
42549b1b8d6SLorenzo Stoakes 	} else {
42649b1b8d6SLorenzo Stoakes 		vma->vm_end = addr;
42749b1b8d6SLorenzo Stoakes 	}
42849b1b8d6SLorenzo Stoakes 
42949b1b8d6SLorenzo Stoakes 	/* vma_complete stores the new vma */
43049b1b8d6SLorenzo Stoakes 	vma_complete(&vp, vmi, vma->vm_mm);
43189b2d2a5SLiam R. Howlett 	validate_mm(vma->vm_mm);
43249b1b8d6SLorenzo Stoakes 
43349b1b8d6SLorenzo Stoakes 	/* Success. */
43449b1b8d6SLorenzo Stoakes 	if (new_below)
43549b1b8d6SLorenzo Stoakes 		vma_next(vmi);
436b7012d51SLiam R. Howlett 	else
437b7012d51SLiam R. Howlett 		vma_prev(vmi);
438b7012d51SLiam R. Howlett 
43949b1b8d6SLorenzo Stoakes 	return 0;
44049b1b8d6SLorenzo Stoakes 
44149b1b8d6SLorenzo Stoakes out_free_mpol:
44249b1b8d6SLorenzo Stoakes 	mpol_put(vma_policy(new));
44349b1b8d6SLorenzo Stoakes out_free_vmi:
44449b1b8d6SLorenzo Stoakes 	vma_iter_free(vmi);
44549b1b8d6SLorenzo Stoakes out_free_vma:
44649b1b8d6SLorenzo Stoakes 	vm_area_free(new);
44749b1b8d6SLorenzo Stoakes 	return err;
44849b1b8d6SLorenzo Stoakes }
44949b1b8d6SLorenzo Stoakes 
45049b1b8d6SLorenzo Stoakes /*
45149b1b8d6SLorenzo Stoakes  * Split a vma into two pieces at address 'addr', a new vma is allocated
45249b1b8d6SLorenzo Stoakes  * either for the first part or the tail.
45349b1b8d6SLorenzo Stoakes  */
45449b1b8d6SLorenzo Stoakes static int split_vma(struct vma_iterator *vmi, struct vm_area_struct *vma,
45549b1b8d6SLorenzo Stoakes 		     unsigned long addr, int new_below)
45649b1b8d6SLorenzo Stoakes {
45749b1b8d6SLorenzo Stoakes 	if (vma->vm_mm->map_count >= sysctl_max_map_count)
45849b1b8d6SLorenzo Stoakes 		return -ENOMEM;
45949b1b8d6SLorenzo Stoakes 
46049b1b8d6SLorenzo Stoakes 	return __split_vma(vmi, vma, addr, new_below);
46149b1b8d6SLorenzo Stoakes }
46249b1b8d6SLorenzo Stoakes 
46349b1b8d6SLorenzo Stoakes /*
46449b1b8d6SLorenzo Stoakes  * vma has some anon_vma assigned, and is already inserted on that
46549b1b8d6SLorenzo Stoakes  * anon_vma's interval trees.
46649b1b8d6SLorenzo Stoakes  *
46749b1b8d6SLorenzo Stoakes  * Before updating the vma's vm_start / vm_end / vm_pgoff fields, the
46849b1b8d6SLorenzo Stoakes  * vma must be removed from the anon_vma's interval trees using
46949b1b8d6SLorenzo Stoakes  * anon_vma_interval_tree_pre_update_vma().
47049b1b8d6SLorenzo Stoakes  *
47149b1b8d6SLorenzo Stoakes  * After the update, the vma will be reinserted using
47249b1b8d6SLorenzo Stoakes  * anon_vma_interval_tree_post_update_vma().
47349b1b8d6SLorenzo Stoakes  *
47449b1b8d6SLorenzo Stoakes  * The entire update must be protected by exclusive mmap_lock and by
47549b1b8d6SLorenzo Stoakes  * the root anon_vma's mutex.
47649b1b8d6SLorenzo Stoakes  */
47749b1b8d6SLorenzo Stoakes void
47849b1b8d6SLorenzo Stoakes anon_vma_interval_tree_pre_update_vma(struct vm_area_struct *vma)
47949b1b8d6SLorenzo Stoakes {
48049b1b8d6SLorenzo Stoakes 	struct anon_vma_chain *avc;
48149b1b8d6SLorenzo Stoakes 
48249b1b8d6SLorenzo Stoakes 	list_for_each_entry(avc, &vma->anon_vma_chain, same_vma)
48349b1b8d6SLorenzo Stoakes 		anon_vma_interval_tree_remove(avc, &avc->anon_vma->rb_root);
48449b1b8d6SLorenzo Stoakes }
48549b1b8d6SLorenzo Stoakes 
48649b1b8d6SLorenzo Stoakes void
48749b1b8d6SLorenzo Stoakes anon_vma_interval_tree_post_update_vma(struct vm_area_struct *vma)
48849b1b8d6SLorenzo Stoakes {
48949b1b8d6SLorenzo Stoakes 	struct anon_vma_chain *avc;
49049b1b8d6SLorenzo Stoakes 
49149b1b8d6SLorenzo Stoakes 	list_for_each_entry(avc, &vma->anon_vma_chain, same_vma)
49249b1b8d6SLorenzo Stoakes 		anon_vma_interval_tree_insert(avc, &avc->anon_vma->rb_root);
49349b1b8d6SLorenzo Stoakes }
49449b1b8d6SLorenzo Stoakes 
49549b1b8d6SLorenzo Stoakes /*
49649b1b8d6SLorenzo Stoakes  * dup_anon_vma() - Helper function to duplicate anon_vma
49749b1b8d6SLorenzo Stoakes  * @dst: The destination VMA
49849b1b8d6SLorenzo Stoakes  * @src: The source VMA
49949b1b8d6SLorenzo Stoakes  * @dup: Pointer to the destination VMA when successful.
50049b1b8d6SLorenzo Stoakes  *
50149b1b8d6SLorenzo Stoakes  * Returns: 0 on success.
50249b1b8d6SLorenzo Stoakes  */
50349b1b8d6SLorenzo Stoakes static int dup_anon_vma(struct vm_area_struct *dst,
50449b1b8d6SLorenzo Stoakes 			struct vm_area_struct *src, struct vm_area_struct **dup)
50549b1b8d6SLorenzo Stoakes {
50649b1b8d6SLorenzo Stoakes 	/*
50749b1b8d6SLorenzo Stoakes 	 * Easily overlooked: when mprotect shifts the boundary, make sure the
50849b1b8d6SLorenzo Stoakes 	 * expanding vma has anon_vma set if the shrinking vma had, to cover any
50949b1b8d6SLorenzo Stoakes 	 * anon pages imported.
51049b1b8d6SLorenzo Stoakes 	 */
51149b1b8d6SLorenzo Stoakes 	if (src->anon_vma && !dst->anon_vma) {
51249b1b8d6SLorenzo Stoakes 		int ret;
51349b1b8d6SLorenzo Stoakes 
51449b1b8d6SLorenzo Stoakes 		vma_assert_write_locked(dst);
51549b1b8d6SLorenzo Stoakes 		dst->anon_vma = src->anon_vma;
51649b1b8d6SLorenzo Stoakes 		ret = anon_vma_clone(dst, src);
51749b1b8d6SLorenzo Stoakes 		if (ret)
51849b1b8d6SLorenzo Stoakes 			return ret;
51949b1b8d6SLorenzo Stoakes 
52049b1b8d6SLorenzo Stoakes 		*dup = dst;
52149b1b8d6SLorenzo Stoakes 	}
52249b1b8d6SLorenzo Stoakes 
52349b1b8d6SLorenzo Stoakes 	return 0;
52449b1b8d6SLorenzo Stoakes }
52549b1b8d6SLorenzo Stoakes 
52649b1b8d6SLorenzo Stoakes #ifdef CONFIG_DEBUG_VM_MAPLE_TREE
52749b1b8d6SLorenzo Stoakes void validate_mm(struct mm_struct *mm)
52849b1b8d6SLorenzo Stoakes {
52949b1b8d6SLorenzo Stoakes 	int bug = 0;
53049b1b8d6SLorenzo Stoakes 	int i = 0;
53149b1b8d6SLorenzo Stoakes 	struct vm_area_struct *vma;
53249b1b8d6SLorenzo Stoakes 	VMA_ITERATOR(vmi, mm, 0);
53349b1b8d6SLorenzo Stoakes 
53449b1b8d6SLorenzo Stoakes 	mt_validate(&mm->mm_mt);
53549b1b8d6SLorenzo Stoakes 	for_each_vma(vmi, vma) {
53649b1b8d6SLorenzo Stoakes #ifdef CONFIG_DEBUG_VM_RB
53749b1b8d6SLorenzo Stoakes 		struct anon_vma *anon_vma = vma->anon_vma;
53849b1b8d6SLorenzo Stoakes 		struct anon_vma_chain *avc;
53949b1b8d6SLorenzo Stoakes #endif
54049b1b8d6SLorenzo Stoakes 		unsigned long vmi_start, vmi_end;
54149b1b8d6SLorenzo Stoakes 		bool warn = 0;
54249b1b8d6SLorenzo Stoakes 
54349b1b8d6SLorenzo Stoakes 		vmi_start = vma_iter_addr(&vmi);
54449b1b8d6SLorenzo Stoakes 		vmi_end = vma_iter_end(&vmi);
54549b1b8d6SLorenzo Stoakes 		if (VM_WARN_ON_ONCE_MM(vma->vm_end != vmi_end, mm))
54649b1b8d6SLorenzo Stoakes 			warn = 1;
54749b1b8d6SLorenzo Stoakes 
54849b1b8d6SLorenzo Stoakes 		if (VM_WARN_ON_ONCE_MM(vma->vm_start != vmi_start, mm))
54949b1b8d6SLorenzo Stoakes 			warn = 1;
55049b1b8d6SLorenzo Stoakes 
55149b1b8d6SLorenzo Stoakes 		if (warn) {
55249b1b8d6SLorenzo Stoakes 			pr_emerg("issue in %s\n", current->comm);
55349b1b8d6SLorenzo Stoakes 			dump_stack();
55449b1b8d6SLorenzo Stoakes 			dump_vma(vma);
55549b1b8d6SLorenzo Stoakes 			pr_emerg("tree range: %px start %lx end %lx\n", vma,
55649b1b8d6SLorenzo Stoakes 				 vmi_start, vmi_end - 1);
55749b1b8d6SLorenzo Stoakes 			vma_iter_dump_tree(&vmi);
55849b1b8d6SLorenzo Stoakes 		}
55949b1b8d6SLorenzo Stoakes 
56049b1b8d6SLorenzo Stoakes #ifdef CONFIG_DEBUG_VM_RB
56149b1b8d6SLorenzo Stoakes 		if (anon_vma) {
56249b1b8d6SLorenzo Stoakes 			anon_vma_lock_read(anon_vma);
56349b1b8d6SLorenzo Stoakes 			list_for_each_entry(avc, &vma->anon_vma_chain, same_vma)
56449b1b8d6SLorenzo Stoakes 				anon_vma_interval_tree_verify(avc);
56549b1b8d6SLorenzo Stoakes 			anon_vma_unlock_read(anon_vma);
56649b1b8d6SLorenzo Stoakes 		}
56749b1b8d6SLorenzo Stoakes #endif
56849b1b8d6SLorenzo Stoakes 		i++;
56949b1b8d6SLorenzo Stoakes 	}
57049b1b8d6SLorenzo Stoakes 	if (i != mm->map_count) {
57149b1b8d6SLorenzo Stoakes 		pr_emerg("map_count %d vma iterator %d\n", mm->map_count, i);
57249b1b8d6SLorenzo Stoakes 		bug = 1;
57349b1b8d6SLorenzo Stoakes 	}
57449b1b8d6SLorenzo Stoakes 	VM_BUG_ON_MM(bug, mm);
57549b1b8d6SLorenzo Stoakes }
57649b1b8d6SLorenzo Stoakes #endif /* CONFIG_DEBUG_VM_MAPLE_TREE */
57749b1b8d6SLorenzo Stoakes 
57865e0aa64SLorenzo Stoakes /* Actually perform the VMA merge operation. */
57965e0aa64SLorenzo Stoakes static int commit_merge(struct vma_merge_struct *vmg,
580cc8cb369SLorenzo Stoakes 			struct vm_area_struct *adjust,
581cc8cb369SLorenzo Stoakes 			struct vm_area_struct *remove,
582cc8cb369SLorenzo Stoakes 			struct vm_area_struct *remove2,
583cc8cb369SLorenzo Stoakes 			long adj_start,
584cc8cb369SLorenzo Stoakes 			bool expanded)
58565e0aa64SLorenzo Stoakes {
58665e0aa64SLorenzo Stoakes 	struct vma_prepare vp;
58765e0aa64SLorenzo Stoakes 
588cc8cb369SLorenzo Stoakes 	init_multi_vma_prep(&vp, vmg->vma, adjust, remove, remove2);
58965e0aa64SLorenzo Stoakes 
590cc8cb369SLorenzo Stoakes 	VM_WARN_ON(vp.anon_vma && adjust && adjust->anon_vma &&
591cc8cb369SLorenzo Stoakes 		   vp.anon_vma != adjust->anon_vma);
592cc8cb369SLorenzo Stoakes 
593cc8cb369SLorenzo Stoakes 	if (expanded) {
59465e0aa64SLorenzo Stoakes 		/* Note: vma iterator must be pointing to 'start'. */
59565e0aa64SLorenzo Stoakes 		vma_iter_config(vmg->vmi, vmg->start, vmg->end);
596cc8cb369SLorenzo Stoakes 	} else {
597cc8cb369SLorenzo Stoakes 		vma_iter_config(vmg->vmi, adjust->vm_start + adj_start,
598cc8cb369SLorenzo Stoakes 				adjust->vm_end);
599cc8cb369SLorenzo Stoakes 	}
60065e0aa64SLorenzo Stoakes 
60165e0aa64SLorenzo Stoakes 	if (vma_iter_prealloc(vmg->vmi, vmg->vma))
60265e0aa64SLorenzo Stoakes 		return -ENOMEM;
60365e0aa64SLorenzo Stoakes 
60465e0aa64SLorenzo Stoakes 	vma_prepare(&vp);
605cc8cb369SLorenzo Stoakes 	vma_adjust_trans_huge(vmg->vma, vmg->start, vmg->end, adj_start);
60665e0aa64SLorenzo Stoakes 	vma_set_range(vmg->vma, vmg->start, vmg->end, vmg->pgoff);
60765e0aa64SLorenzo Stoakes 
608cc8cb369SLorenzo Stoakes 	if (expanded)
60965e0aa64SLorenzo Stoakes 		vma_iter_store(vmg->vmi, vmg->vma);
61065e0aa64SLorenzo Stoakes 
611cc8cb369SLorenzo Stoakes 	if (adj_start) {
612cc8cb369SLorenzo Stoakes 		adjust->vm_start += adj_start;
613cc8cb369SLorenzo Stoakes 		adjust->vm_pgoff += PHYS_PFN(adj_start);
614cc8cb369SLorenzo Stoakes 		if (adj_start < 0) {
615cc8cb369SLorenzo Stoakes 			WARN_ON(expanded);
616cc8cb369SLorenzo Stoakes 			vma_iter_store(vmg->vmi, adjust);
617cc8cb369SLorenzo Stoakes 		}
618cc8cb369SLorenzo Stoakes 	}
619cc8cb369SLorenzo Stoakes 
62065e0aa64SLorenzo Stoakes 	vma_complete(&vp, vmg->vmi, vmg->vma->vm_mm);
62165e0aa64SLorenzo Stoakes 
62265e0aa64SLorenzo Stoakes 	return 0;
62365e0aa64SLorenzo Stoakes }
62465e0aa64SLorenzo Stoakes 
62501c373e9SLorenzo Stoakes /* We can only remove VMAs when merging if they do not have a close hook. */
62601c373e9SLorenzo Stoakes static bool can_merge_remove_vma(struct vm_area_struct *vma)
62701c373e9SLorenzo Stoakes {
62801c373e9SLorenzo Stoakes 	return !vma->vm_ops || !vma->vm_ops->close;
62901c373e9SLorenzo Stoakes }
63001c373e9SLorenzo Stoakes 
63149b1b8d6SLorenzo Stoakes /*
632cc8cb369SLorenzo Stoakes  * vma_merge_existing_range - Attempt to merge VMAs based on a VMA having its
633cc8cb369SLorenzo Stoakes  * attributes modified.
634cc8cb369SLorenzo Stoakes  *
635cc8cb369SLorenzo Stoakes  * @vmg: Describes the modifications being made to a VMA and associated
636cc8cb369SLorenzo Stoakes  *       metadata.
637cc8cb369SLorenzo Stoakes  *
638cc8cb369SLorenzo Stoakes  * When the attributes of a range within a VMA change, then it might be possible
639cc8cb369SLorenzo Stoakes  * for immediately adjacent VMAs to be merged into that VMA due to having
640cc8cb369SLorenzo Stoakes  * identical properties.
641cc8cb369SLorenzo Stoakes  *
642cc8cb369SLorenzo Stoakes  * This function checks for the existence of any such mergeable VMAs and updates
643cc8cb369SLorenzo Stoakes  * the maple tree describing the @vmg->vma->vm_mm address space to account for
644cc8cb369SLorenzo Stoakes  * this, as well as any VMAs shrunk/expanded/deleted as a result of this merge.
645cc8cb369SLorenzo Stoakes  *
646cc8cb369SLorenzo Stoakes  * As part of this operation, if a merge occurs, the @vmg object will have its
647cc8cb369SLorenzo Stoakes  * vma, start, end, and pgoff fields modified to execute the merge. Subsequent
648cc8cb369SLorenzo Stoakes  * calls to this function should reset these fields.
649cc8cb369SLorenzo Stoakes  *
650cc8cb369SLorenzo Stoakes  * Returns: The merged VMA if merge succeeds, or NULL otherwise.
651cc8cb369SLorenzo Stoakes  *
652cc8cb369SLorenzo Stoakes  * ASSUMPTIONS:
653cc8cb369SLorenzo Stoakes  * - The caller must assign the VMA to be modifed to @vmg->vma.
654cc8cb369SLorenzo Stoakes  * - The caller must have set @vmg->prev to the previous VMA, if there is one.
655cc8cb369SLorenzo Stoakes  * - The caller must not set @vmg->next, as we determine this.
656cc8cb369SLorenzo Stoakes  * - The caller must hold a WRITE lock on the mm_struct->mmap_lock.
657cc8cb369SLorenzo Stoakes  * - vmi must be positioned within [@vmg->vma->vm_start, @vmg->vma->vm_end).
658cc8cb369SLorenzo Stoakes  */
659cc8cb369SLorenzo Stoakes static struct vm_area_struct *vma_merge_existing_range(struct vma_merge_struct *vmg)
660cc8cb369SLorenzo Stoakes {
661cc8cb369SLorenzo Stoakes 	struct vm_area_struct *vma = vmg->vma;
662cc8cb369SLorenzo Stoakes 	struct vm_area_struct *prev = vmg->prev;
663cc8cb369SLorenzo Stoakes 	struct vm_area_struct *next, *res;
664cc8cb369SLorenzo Stoakes 	struct vm_area_struct *anon_dup = NULL;
665cc8cb369SLorenzo Stoakes 	struct vm_area_struct *adjust = NULL;
666cc8cb369SLorenzo Stoakes 	unsigned long start = vmg->start;
667cc8cb369SLorenzo Stoakes 	unsigned long end = vmg->end;
668cc8cb369SLorenzo Stoakes 	bool left_side = vma && start == vma->vm_start;
669cc8cb369SLorenzo Stoakes 	bool right_side = vma && end == vma->vm_end;
670cc8cb369SLorenzo Stoakes 	int err = 0;
671cc8cb369SLorenzo Stoakes 	long adj_start = 0;
672cc8cb369SLorenzo Stoakes 	bool merge_will_delete_vma, merge_will_delete_next;
673cc8cb369SLorenzo Stoakes 	bool merge_left, merge_right, merge_both;
674cc8cb369SLorenzo Stoakes 	bool expanded;
675cc8cb369SLorenzo Stoakes 
676cc8cb369SLorenzo Stoakes 	mmap_assert_write_locked(vmg->mm);
677cc8cb369SLorenzo Stoakes 	VM_WARN_ON(!vma); /* We are modifying a VMA, so caller must specify. */
678cc8cb369SLorenzo Stoakes 	VM_WARN_ON(vmg->next); /* We set this. */
679cc8cb369SLorenzo Stoakes 	VM_WARN_ON(prev && start <= prev->vm_start);
680cc8cb369SLorenzo Stoakes 	VM_WARN_ON(start >= end);
681cc8cb369SLorenzo Stoakes 	/*
682cc8cb369SLorenzo Stoakes 	 * If vma == prev, then we are offset into a VMA. Otherwise, if we are
683cc8cb369SLorenzo Stoakes 	 * not, we must span a portion of the VMA.
684cc8cb369SLorenzo Stoakes 	 */
685cc8cb369SLorenzo Stoakes 	VM_WARN_ON(vma && ((vma != prev && vmg->start != vma->vm_start) ||
686cc8cb369SLorenzo Stoakes 			   vmg->end > vma->vm_end));
687cc8cb369SLorenzo Stoakes 	/* The vmi must be positioned within vmg->vma. */
688cc8cb369SLorenzo Stoakes 	VM_WARN_ON(vma && !(vma_iter_addr(vmg->vmi) >= vma->vm_start &&
689cc8cb369SLorenzo Stoakes 			    vma_iter_addr(vmg->vmi) < vma->vm_end));
690cc8cb369SLorenzo Stoakes 
691cc8cb369SLorenzo Stoakes 	vmg->state = VMA_MERGE_NOMERGE;
692cc8cb369SLorenzo Stoakes 
693cc8cb369SLorenzo Stoakes 	/*
694cc8cb369SLorenzo Stoakes 	 * If a special mapping or if the range being modified is neither at the
695cc8cb369SLorenzo Stoakes 	 * furthermost left or right side of the VMA, then we have no chance of
696cc8cb369SLorenzo Stoakes 	 * merging and should abort.
697cc8cb369SLorenzo Stoakes 	 */
698cc8cb369SLorenzo Stoakes 	if (vmg->flags & VM_SPECIAL || (!left_side && !right_side))
699cc8cb369SLorenzo Stoakes 		return NULL;
700cc8cb369SLorenzo Stoakes 
701cc8cb369SLorenzo Stoakes 	if (left_side)
702cc8cb369SLorenzo Stoakes 		merge_left = can_vma_merge_left(vmg);
703cc8cb369SLorenzo Stoakes 	else
704cc8cb369SLorenzo Stoakes 		merge_left = false;
705cc8cb369SLorenzo Stoakes 
706cc8cb369SLorenzo Stoakes 	if (right_side) {
707cc8cb369SLorenzo Stoakes 		next = vmg->next = vma_iter_next_range(vmg->vmi);
708cc8cb369SLorenzo Stoakes 		vma_iter_prev_range(vmg->vmi);
709cc8cb369SLorenzo Stoakes 
710cc8cb369SLorenzo Stoakes 		merge_right = can_vma_merge_right(vmg, merge_left);
711cc8cb369SLorenzo Stoakes 	} else {
712cc8cb369SLorenzo Stoakes 		merge_right = false;
713cc8cb369SLorenzo Stoakes 		next = NULL;
714cc8cb369SLorenzo Stoakes 	}
715cc8cb369SLorenzo Stoakes 
716cc8cb369SLorenzo Stoakes 	if (merge_left)		/* If merging prev, position iterator there. */
717cc8cb369SLorenzo Stoakes 		vma_prev(vmg->vmi);
718cc8cb369SLorenzo Stoakes 	else if (!merge_right)	/* If we have nothing to merge, abort. */
719cc8cb369SLorenzo Stoakes 		return NULL;
720cc8cb369SLorenzo Stoakes 
721cc8cb369SLorenzo Stoakes 	merge_both = merge_left && merge_right;
722cc8cb369SLorenzo Stoakes 	/* If we span the entire VMA, a merge implies it will be deleted. */
723cc8cb369SLorenzo Stoakes 	merge_will_delete_vma = left_side && right_side;
72401c373e9SLorenzo Stoakes 
72501c373e9SLorenzo Stoakes 	/*
72601c373e9SLorenzo Stoakes 	 * If we need to remove vma in its entirety but are unable to do so,
72701c373e9SLorenzo Stoakes 	 * we have no sensible recourse but to abort the merge.
72801c373e9SLorenzo Stoakes 	 */
72901c373e9SLorenzo Stoakes 	if (merge_will_delete_vma && !can_merge_remove_vma(vma))
73001c373e9SLorenzo Stoakes 		return NULL;
73101c373e9SLorenzo Stoakes 
732cc8cb369SLorenzo Stoakes 	/*
733cc8cb369SLorenzo Stoakes 	 * If we merge both VMAs, then next is also deleted. This implies
734cc8cb369SLorenzo Stoakes 	 * merge_will_delete_vma also.
735cc8cb369SLorenzo Stoakes 	 */
736cc8cb369SLorenzo Stoakes 	merge_will_delete_next = merge_both;
737cc8cb369SLorenzo Stoakes 
73801c373e9SLorenzo Stoakes 	/*
73901c373e9SLorenzo Stoakes 	 * If we cannot delete next, then we can reduce the operation to merging
74001c373e9SLorenzo Stoakes 	 * prev and vma (thereby deleting vma).
74101c373e9SLorenzo Stoakes 	 */
74201c373e9SLorenzo Stoakes 	if (merge_will_delete_next && !can_merge_remove_vma(next)) {
74301c373e9SLorenzo Stoakes 		merge_will_delete_next = false;
74401c373e9SLorenzo Stoakes 		merge_right = false;
74501c373e9SLorenzo Stoakes 		merge_both = false;
74601c373e9SLorenzo Stoakes 	}
74701c373e9SLorenzo Stoakes 
748cc8cb369SLorenzo Stoakes 	/* No matter what happens, we will be adjusting vma. */
749cc8cb369SLorenzo Stoakes 	vma_start_write(vma);
750cc8cb369SLorenzo Stoakes 
751cc8cb369SLorenzo Stoakes 	if (merge_left)
752cc8cb369SLorenzo Stoakes 		vma_start_write(prev);
753cc8cb369SLorenzo Stoakes 
754cc8cb369SLorenzo Stoakes 	if (merge_right)
755cc8cb369SLorenzo Stoakes 		vma_start_write(next);
756cc8cb369SLorenzo Stoakes 
757cc8cb369SLorenzo Stoakes 	if (merge_both) {
758cc8cb369SLorenzo Stoakes 		/*
759cc8cb369SLorenzo Stoakes 		 *         |<----->|
760cc8cb369SLorenzo Stoakes 		 * |-------*********-------|
761cc8cb369SLorenzo Stoakes 		 *   prev     vma     next
762cc8cb369SLorenzo Stoakes 		 *  extend   delete  delete
763cc8cb369SLorenzo Stoakes 		 */
764cc8cb369SLorenzo Stoakes 
765cc8cb369SLorenzo Stoakes 		vmg->vma = prev;
766cc8cb369SLorenzo Stoakes 		vmg->start = prev->vm_start;
767cc8cb369SLorenzo Stoakes 		vmg->end = next->vm_end;
768cc8cb369SLorenzo Stoakes 		vmg->pgoff = prev->vm_pgoff;
769cc8cb369SLorenzo Stoakes 
770cc8cb369SLorenzo Stoakes 		/*
771cc8cb369SLorenzo Stoakes 		 * We already ensured anon_vma compatibility above, so now it's
772cc8cb369SLorenzo Stoakes 		 * simply a case of, if prev has no anon_vma object, which of
773cc8cb369SLorenzo Stoakes 		 * next or vma contains the anon_vma we must duplicate.
774cc8cb369SLorenzo Stoakes 		 */
775cc8cb369SLorenzo Stoakes 		err = dup_anon_vma(prev, next->anon_vma ? next : vma, &anon_dup);
776cc8cb369SLorenzo Stoakes 	} else if (merge_left) {
777cc8cb369SLorenzo Stoakes 		/*
778cc8cb369SLorenzo Stoakes 		 *         |<----->| OR
779cc8cb369SLorenzo Stoakes 		 *         |<--------->|
780cc8cb369SLorenzo Stoakes 		 * |-------*************
781cc8cb369SLorenzo Stoakes 		 *   prev       vma
782cc8cb369SLorenzo Stoakes 		 *  extend shrink/delete
783cc8cb369SLorenzo Stoakes 		 */
784cc8cb369SLorenzo Stoakes 
785cc8cb369SLorenzo Stoakes 		vmg->vma = prev;
786cc8cb369SLorenzo Stoakes 		vmg->start = prev->vm_start;
787cc8cb369SLorenzo Stoakes 		vmg->pgoff = prev->vm_pgoff;
788cc8cb369SLorenzo Stoakes 
78901c373e9SLorenzo Stoakes 		if (!merge_will_delete_vma) {
790cc8cb369SLorenzo Stoakes 			adjust = vma;
791cc8cb369SLorenzo Stoakes 			adj_start = vmg->end - vma->vm_start;
792cc8cb369SLorenzo Stoakes 		}
793cc8cb369SLorenzo Stoakes 
794cc8cb369SLorenzo Stoakes 		err = dup_anon_vma(prev, vma, &anon_dup);
795cc8cb369SLorenzo Stoakes 	} else { /* merge_right */
796cc8cb369SLorenzo Stoakes 		/*
797cc8cb369SLorenzo Stoakes 		 *     |<----->| OR
798cc8cb369SLorenzo Stoakes 		 * |<--------->|
799cc8cb369SLorenzo Stoakes 		 * *************-------|
800cc8cb369SLorenzo Stoakes 		 *      vma       next
801cc8cb369SLorenzo Stoakes 		 * shrink/delete extend
802cc8cb369SLorenzo Stoakes 		 */
803cc8cb369SLorenzo Stoakes 
804cc8cb369SLorenzo Stoakes 		pgoff_t pglen = PHYS_PFN(vmg->end - vmg->start);
805cc8cb369SLorenzo Stoakes 
806cc8cb369SLorenzo Stoakes 		VM_WARN_ON(!merge_right);
807cc8cb369SLorenzo Stoakes 		/* If we are offset into a VMA, then prev must be vma. */
808cc8cb369SLorenzo Stoakes 		VM_WARN_ON(vmg->start > vma->vm_start && prev && vma != prev);
809cc8cb369SLorenzo Stoakes 
810cc8cb369SLorenzo Stoakes 		if (merge_will_delete_vma) {
811cc8cb369SLorenzo Stoakes 			vmg->vma = next;
812cc8cb369SLorenzo Stoakes 			vmg->end = next->vm_end;
813cc8cb369SLorenzo Stoakes 			vmg->pgoff = next->vm_pgoff - pglen;
814cc8cb369SLorenzo Stoakes 		} else {
815cc8cb369SLorenzo Stoakes 			/*
816cc8cb369SLorenzo Stoakes 			 * We shrink vma and expand next.
817cc8cb369SLorenzo Stoakes 			 *
818cc8cb369SLorenzo Stoakes 			 * IMPORTANT: This is the ONLY case where the final
819cc8cb369SLorenzo Stoakes 			 * merged VMA is NOT vmg->vma, but rather vmg->next.
820cc8cb369SLorenzo Stoakes 			 */
821cc8cb369SLorenzo Stoakes 
822cc8cb369SLorenzo Stoakes 			vmg->start = vma->vm_start;
823cc8cb369SLorenzo Stoakes 			vmg->end = start;
824cc8cb369SLorenzo Stoakes 			vmg->pgoff = vma->vm_pgoff;
825cc8cb369SLorenzo Stoakes 
826cc8cb369SLorenzo Stoakes 			adjust = next;
827cc8cb369SLorenzo Stoakes 			adj_start = -(vma->vm_end - start);
828cc8cb369SLorenzo Stoakes 		}
829cc8cb369SLorenzo Stoakes 
830cc8cb369SLorenzo Stoakes 		err = dup_anon_vma(next, vma, &anon_dup);
831cc8cb369SLorenzo Stoakes 	}
832cc8cb369SLorenzo Stoakes 
833cc8cb369SLorenzo Stoakes 	if (err)
834cc8cb369SLorenzo Stoakes 		goto abort;
835cc8cb369SLorenzo Stoakes 
836cc8cb369SLorenzo Stoakes 	/*
837cc8cb369SLorenzo Stoakes 	 * In nearly all cases, we expand vmg->vma. There is one exception -
838cc8cb369SLorenzo Stoakes 	 * merge_right where we partially span the VMA. In this case we shrink
839cc8cb369SLorenzo Stoakes 	 * the end of vmg->vma and adjust the start of vmg->next accordingly.
840cc8cb369SLorenzo Stoakes 	 */
841cc8cb369SLorenzo Stoakes 	expanded = !merge_right || merge_will_delete_vma;
842cc8cb369SLorenzo Stoakes 
843cc8cb369SLorenzo Stoakes 	if (commit_merge(vmg, adjust,
844cc8cb369SLorenzo Stoakes 			 merge_will_delete_vma ? vma : NULL,
845cc8cb369SLorenzo Stoakes 			 merge_will_delete_next ? next : NULL,
846cc8cb369SLorenzo Stoakes 			 adj_start, expanded)) {
847cc8cb369SLorenzo Stoakes 		if (anon_dup)
848cc8cb369SLorenzo Stoakes 			unlink_anon_vmas(anon_dup);
849cc8cb369SLorenzo Stoakes 
850cc8cb369SLorenzo Stoakes 		vmg->state = VMA_MERGE_ERROR_NOMEM;
851cc8cb369SLorenzo Stoakes 		return NULL;
852cc8cb369SLorenzo Stoakes 	}
853cc8cb369SLorenzo Stoakes 
854cc8cb369SLorenzo Stoakes 	res = merge_left ? prev : next;
855cc8cb369SLorenzo Stoakes 	khugepaged_enter_vma(res, vmg->flags);
856cc8cb369SLorenzo Stoakes 
857cc8cb369SLorenzo Stoakes 	vmg->state = VMA_MERGE_SUCCESS;
858cc8cb369SLorenzo Stoakes 	return res;
859cc8cb369SLorenzo Stoakes 
860cc8cb369SLorenzo Stoakes abort:
861cc8cb369SLorenzo Stoakes 	vma_iter_set(vmg->vmi, start);
862cc8cb369SLorenzo Stoakes 	vma_iter_load(vmg->vmi);
863cc8cb369SLorenzo Stoakes 	vmg->state = VMA_MERGE_ERROR_NOMEM;
864cc8cb369SLorenzo Stoakes 	return NULL;
865cc8cb369SLorenzo Stoakes }
866cc8cb369SLorenzo Stoakes 
867cc8cb369SLorenzo Stoakes /*
868cacded5eSLorenzo Stoakes  * vma_merge_new_range - Attempt to merge a new VMA into address space
869cacded5eSLorenzo Stoakes  *
870cacded5eSLorenzo Stoakes  * @vmg: Describes the VMA we are adding, in the range @vmg->start to @vmg->end
871cacded5eSLorenzo Stoakes  *       (exclusive), which we try to merge with any adjacent VMAs if possible.
872cacded5eSLorenzo Stoakes  *
873cacded5eSLorenzo Stoakes  * We are about to add a VMA to the address space starting at @vmg->start and
874cacded5eSLorenzo Stoakes  * ending at @vmg->end. There are three different possible scenarios:
875cacded5eSLorenzo Stoakes  *
876cacded5eSLorenzo Stoakes  * 1. There is a VMA with identical properties immediately adjacent to the
877cacded5eSLorenzo Stoakes  *    proposed new VMA [@vmg->start, @vmg->end) either before or after it -
878cacded5eSLorenzo Stoakes  *    EXPAND that VMA:
879cacded5eSLorenzo Stoakes  *
880cacded5eSLorenzo Stoakes  * Proposed:       |-----|  or  |-----|
881cacded5eSLorenzo Stoakes  * Existing:  |----|                  |----|
882cacded5eSLorenzo Stoakes  *
883cacded5eSLorenzo Stoakes  * 2. There are VMAs with identical properties immediately adjacent to the
884cacded5eSLorenzo Stoakes  *    proposed new VMA [@vmg->start, @vmg->end) both before AND after it -
885cacded5eSLorenzo Stoakes  *    EXPAND the former and REMOVE the latter:
886cacded5eSLorenzo Stoakes  *
887cacded5eSLorenzo Stoakes  * Proposed:       |-----|
888cacded5eSLorenzo Stoakes  * Existing:  |----|     |----|
889cacded5eSLorenzo Stoakes  *
890cacded5eSLorenzo Stoakes  * 3. There are no VMAs immediately adjacent to the proposed new VMA or those
891cacded5eSLorenzo Stoakes  *    VMAs do not have identical attributes - NO MERGE POSSIBLE.
892cacded5eSLorenzo Stoakes  *
893cacded5eSLorenzo Stoakes  * In instances where we can merge, this function returns the expanded VMA which
894cacded5eSLorenzo Stoakes  * will have its range adjusted accordingly and the underlying maple tree also
895cacded5eSLorenzo Stoakes  * adjusted.
896cacded5eSLorenzo Stoakes  *
897cacded5eSLorenzo Stoakes  * Returns: In instances where no merge was possible, NULL. Otherwise, a pointer
898cacded5eSLorenzo Stoakes  *          to the VMA we expanded.
899cacded5eSLorenzo Stoakes  *
900cacded5eSLorenzo Stoakes  * This function adjusts @vmg to provide @vmg->next if not already specified,
901cacded5eSLorenzo Stoakes  * and adjusts [@vmg->start, @vmg->end) to span the expanded range.
902cacded5eSLorenzo Stoakes  *
903cacded5eSLorenzo Stoakes  * ASSUMPTIONS:
904cacded5eSLorenzo Stoakes  * - The caller must hold a WRITE lock on the mm_struct->mmap_lock.
905cacded5eSLorenzo Stoakes  * - The caller must have determined that [@vmg->start, @vmg->end) is empty,
906cacded5eSLorenzo Stoakes      other than VMAs that will be unmapped should the operation succeed.
907cacded5eSLorenzo Stoakes  * - The caller must have specified the previous vma in @vmg->prev.
908cacded5eSLorenzo Stoakes  * - The caller must have specified the next vma in @vmg->next.
909cacded5eSLorenzo Stoakes  * - The caller must have positioned the vmi at or before the gap.
910cacded5eSLorenzo Stoakes  */
911cacded5eSLorenzo Stoakes struct vm_area_struct *vma_merge_new_range(struct vma_merge_struct *vmg)
912cacded5eSLorenzo Stoakes {
913cacded5eSLorenzo Stoakes 	struct vm_area_struct *prev = vmg->prev;
914cacded5eSLorenzo Stoakes 	struct vm_area_struct *next = vmg->next;
915cacded5eSLorenzo Stoakes 	unsigned long start = vmg->start;
916cacded5eSLorenzo Stoakes 	unsigned long end = vmg->end;
917cacded5eSLorenzo Stoakes 	pgoff_t pgoff = vmg->pgoff;
918cacded5eSLorenzo Stoakes 	pgoff_t pglen = PHYS_PFN(end - start);
919cacded5eSLorenzo Stoakes 	bool can_merge_left, can_merge_right;
920*c4d91e22SLorenzo Stoakes 	bool just_expand = vmg->merge_flags & VMG_FLAG_JUST_EXPAND;
921cacded5eSLorenzo Stoakes 
922cacded5eSLorenzo Stoakes 	mmap_assert_write_locked(vmg->mm);
923cacded5eSLorenzo Stoakes 	VM_WARN_ON(vmg->vma);
924cacded5eSLorenzo Stoakes 	/* vmi must point at or before the gap. */
925cacded5eSLorenzo Stoakes 	VM_WARN_ON(vma_iter_addr(vmg->vmi) > end);
926cacded5eSLorenzo Stoakes 
927cacded5eSLorenzo Stoakes 	vmg->state = VMA_MERGE_NOMERGE;
928cacded5eSLorenzo Stoakes 
929cacded5eSLorenzo Stoakes 	/* Special VMAs are unmergeable, also if no prev/next. */
930cacded5eSLorenzo Stoakes 	if ((vmg->flags & VM_SPECIAL) || (!prev && !next))
931cacded5eSLorenzo Stoakes 		return NULL;
932cacded5eSLorenzo Stoakes 
933cacded5eSLorenzo Stoakes 	can_merge_left = can_vma_merge_left(vmg);
934*c4d91e22SLorenzo Stoakes 	can_merge_right = !just_expand && can_vma_merge_right(vmg, can_merge_left);
935cacded5eSLorenzo Stoakes 
936cacded5eSLorenzo Stoakes 	/* If we can merge with the next VMA, adjust vmg accordingly. */
937cacded5eSLorenzo Stoakes 	if (can_merge_right) {
938cacded5eSLorenzo Stoakes 		vmg->end = next->vm_end;
939cacded5eSLorenzo Stoakes 		vmg->vma = next;
940cacded5eSLorenzo Stoakes 		vmg->pgoff = next->vm_pgoff - pglen;
941cacded5eSLorenzo Stoakes 	}
942cacded5eSLorenzo Stoakes 
943cacded5eSLorenzo Stoakes 	/* If we can merge with the previous VMA, adjust vmg accordingly. */
944cacded5eSLorenzo Stoakes 	if (can_merge_left) {
945cacded5eSLorenzo Stoakes 		vmg->start = prev->vm_start;
946cacded5eSLorenzo Stoakes 		vmg->vma = prev;
947cacded5eSLorenzo Stoakes 		vmg->pgoff = prev->vm_pgoff;
948cacded5eSLorenzo Stoakes 
94901c373e9SLorenzo Stoakes 		/*
95001c373e9SLorenzo Stoakes 		 * If this merge would result in removal of the next VMA but we
95101c373e9SLorenzo Stoakes 		 * are not permitted to do so, reduce the operation to merging
95201c373e9SLorenzo Stoakes 		 * prev and vma.
95301c373e9SLorenzo Stoakes 		 */
95401c373e9SLorenzo Stoakes 		if (can_merge_right && !can_merge_remove_vma(next))
95501c373e9SLorenzo Stoakes 			vmg->end = end;
95601c373e9SLorenzo Stoakes 
957*c4d91e22SLorenzo Stoakes 		/* In expand-only case we are already positioned at prev. */
958*c4d91e22SLorenzo Stoakes 		if (!just_expand) {
959*c4d91e22SLorenzo Stoakes 			/* Equivalent to going to the previous range. */
960*c4d91e22SLorenzo Stoakes 			vma_prev(vmg->vmi);
961*c4d91e22SLorenzo Stoakes 		}
962cacded5eSLorenzo Stoakes 	}
963cacded5eSLorenzo Stoakes 
964cacded5eSLorenzo Stoakes 	/*
965cacded5eSLorenzo Stoakes 	 * Now try to expand adjacent VMA(s). This takes care of removing the
966cacded5eSLorenzo Stoakes 	 * following VMA if we have VMAs on both sides.
967cacded5eSLorenzo Stoakes 	 */
968cacded5eSLorenzo Stoakes 	if (vmg->vma && !vma_expand(vmg)) {
969cacded5eSLorenzo Stoakes 		khugepaged_enter_vma(vmg->vma, vmg->flags);
970cacded5eSLorenzo Stoakes 		vmg->state = VMA_MERGE_SUCCESS;
971cacded5eSLorenzo Stoakes 		return vmg->vma;
972cacded5eSLorenzo Stoakes 	}
973cacded5eSLorenzo Stoakes 
974cacded5eSLorenzo Stoakes 	/* If expansion failed, reset state. Allows us to retry merge later. */
975*c4d91e22SLorenzo Stoakes 	if (!just_expand) {
976cacded5eSLorenzo Stoakes 		vmg->vma = NULL;
977cacded5eSLorenzo Stoakes 		vmg->start = start;
978cacded5eSLorenzo Stoakes 		vmg->end = end;
979cacded5eSLorenzo Stoakes 		vmg->pgoff = pgoff;
980cacded5eSLorenzo Stoakes 		if (vmg->vma == prev)
981cacded5eSLorenzo Stoakes 			vma_iter_set(vmg->vmi, start);
982*c4d91e22SLorenzo Stoakes 	}
983cacded5eSLorenzo Stoakes 
984cacded5eSLorenzo Stoakes 	return NULL;
985cacded5eSLorenzo Stoakes }
986cacded5eSLorenzo Stoakes 
987cacded5eSLorenzo Stoakes /*
98849b1b8d6SLorenzo Stoakes  * vma_expand - Expand an existing VMA
98949b1b8d6SLorenzo Stoakes  *
990fc21959fSLorenzo Stoakes  * @vmg: Describes a VMA expansion operation.
99149b1b8d6SLorenzo Stoakes  *
992fc21959fSLorenzo Stoakes  * Expand @vma to vmg->start and vmg->end.  Can expand off the start and end.
993fc21959fSLorenzo Stoakes  * Will expand over vmg->next if it's different from vmg->vma and vmg->end ==
994fc21959fSLorenzo Stoakes  * vmg->next->vm_end.  Checking if the vmg->vma can expand and merge with
995fc21959fSLorenzo Stoakes  * vmg->next needs to be handled by the caller.
99649b1b8d6SLorenzo Stoakes  *
997cacded5eSLorenzo Stoakes  * Returns: 0 on success.
998cacded5eSLorenzo Stoakes  *
999cacded5eSLorenzo Stoakes  * ASSUMPTIONS:
1000cacded5eSLorenzo Stoakes  * - The caller must hold a WRITE lock on vmg->vma->mm->mmap_lock.
1001cacded5eSLorenzo Stoakes  * - The caller must have set @vmg->vma and @vmg->next.
100249b1b8d6SLorenzo Stoakes  */
1003fc21959fSLorenzo Stoakes int vma_expand(struct vma_merge_struct *vmg)
100449b1b8d6SLorenzo Stoakes {
100549b1b8d6SLorenzo Stoakes 	struct vm_area_struct *anon_dup = NULL;
100649b1b8d6SLorenzo Stoakes 	bool remove_next = false;
1007fc21959fSLorenzo Stoakes 	struct vm_area_struct *vma = vmg->vma;
1008fc21959fSLorenzo Stoakes 	struct vm_area_struct *next = vmg->next;
100949b1b8d6SLorenzo Stoakes 
1010cacded5eSLorenzo Stoakes 	mmap_assert_write_locked(vmg->mm);
1011cacded5eSLorenzo Stoakes 
101249b1b8d6SLorenzo Stoakes 	vma_start_write(vma);
1013fc21959fSLorenzo Stoakes 	if (next && (vma != next) && (vmg->end == next->vm_end)) {
101449b1b8d6SLorenzo Stoakes 		int ret;
101549b1b8d6SLorenzo Stoakes 
101649b1b8d6SLorenzo Stoakes 		remove_next = true;
101701c373e9SLorenzo Stoakes 		/* This should already have been checked by this point. */
101801c373e9SLorenzo Stoakes 		VM_WARN_ON(!can_merge_remove_vma(next));
101949b1b8d6SLorenzo Stoakes 		vma_start_write(next);
102049b1b8d6SLorenzo Stoakes 		ret = dup_anon_vma(vma, next, &anon_dup);
102149b1b8d6SLorenzo Stoakes 		if (ret)
102249b1b8d6SLorenzo Stoakes 			return ret;
102349b1b8d6SLorenzo Stoakes 	}
102449b1b8d6SLorenzo Stoakes 
102549b1b8d6SLorenzo Stoakes 	/* Not merging but overwriting any part of next is not handled. */
102665e0aa64SLorenzo Stoakes 	VM_WARN_ON(next && !remove_next &&
1027fc21959fSLorenzo Stoakes 		  next != vma && vmg->end > next->vm_start);
102849b1b8d6SLorenzo Stoakes 	/* Only handles expanding */
1029fc21959fSLorenzo Stoakes 	VM_WARN_ON(vma->vm_start < vmg->start || vma->vm_end > vmg->end);
103049b1b8d6SLorenzo Stoakes 
1031cc8cb369SLorenzo Stoakes 	if (commit_merge(vmg, NULL, remove_next ? next : NULL, NULL, 0, true))
103249b1b8d6SLorenzo Stoakes 		goto nomem;
103349b1b8d6SLorenzo Stoakes 
103449b1b8d6SLorenzo Stoakes 	return 0;
103549b1b8d6SLorenzo Stoakes 
103649b1b8d6SLorenzo Stoakes nomem:
1037cacded5eSLorenzo Stoakes 	vmg->state = VMA_MERGE_ERROR_NOMEM;
103849b1b8d6SLorenzo Stoakes 	if (anon_dup)
103949b1b8d6SLorenzo Stoakes 		unlink_anon_vmas(anon_dup);
104049b1b8d6SLorenzo Stoakes 	return -ENOMEM;
104149b1b8d6SLorenzo Stoakes }
104249b1b8d6SLorenzo Stoakes 
104349b1b8d6SLorenzo Stoakes /*
104449b1b8d6SLorenzo Stoakes  * vma_shrink() - Reduce an existing VMAs memory area
104549b1b8d6SLorenzo Stoakes  * @vmi: The vma iterator
104649b1b8d6SLorenzo Stoakes  * @vma: The VMA to modify
104749b1b8d6SLorenzo Stoakes  * @start: The new start
104849b1b8d6SLorenzo Stoakes  * @end: The new end
104949b1b8d6SLorenzo Stoakes  *
105049b1b8d6SLorenzo Stoakes  * Returns: 0 on success, -ENOMEM otherwise
105149b1b8d6SLorenzo Stoakes  */
105249b1b8d6SLorenzo Stoakes int vma_shrink(struct vma_iterator *vmi, struct vm_area_struct *vma,
105349b1b8d6SLorenzo Stoakes 	       unsigned long start, unsigned long end, pgoff_t pgoff)
105449b1b8d6SLorenzo Stoakes {
105549b1b8d6SLorenzo Stoakes 	struct vma_prepare vp;
105649b1b8d6SLorenzo Stoakes 
105749b1b8d6SLorenzo Stoakes 	WARN_ON((vma->vm_start != start) && (vma->vm_end != end));
105849b1b8d6SLorenzo Stoakes 
105949b1b8d6SLorenzo Stoakes 	if (vma->vm_start < start)
106049b1b8d6SLorenzo Stoakes 		vma_iter_config(vmi, vma->vm_start, start);
106149b1b8d6SLorenzo Stoakes 	else
106249b1b8d6SLorenzo Stoakes 		vma_iter_config(vmi, end, vma->vm_end);
106349b1b8d6SLorenzo Stoakes 
106449b1b8d6SLorenzo Stoakes 	if (vma_iter_prealloc(vmi, NULL))
106549b1b8d6SLorenzo Stoakes 		return -ENOMEM;
106649b1b8d6SLorenzo Stoakes 
106749b1b8d6SLorenzo Stoakes 	vma_start_write(vma);
106849b1b8d6SLorenzo Stoakes 
106949b1b8d6SLorenzo Stoakes 	init_vma_prep(&vp, vma);
107049b1b8d6SLorenzo Stoakes 	vma_prepare(&vp);
107149b1b8d6SLorenzo Stoakes 	vma_adjust_trans_huge(vma, start, end, 0);
107249b1b8d6SLorenzo Stoakes 
107349b1b8d6SLorenzo Stoakes 	vma_iter_clear(vmi);
107449b1b8d6SLorenzo Stoakes 	vma_set_range(vma, start, end, pgoff);
107549b1b8d6SLorenzo Stoakes 	vma_complete(&vp, vmi, vma->vm_mm);
107689b2d2a5SLiam R. Howlett 	validate_mm(vma->vm_mm);
107749b1b8d6SLorenzo Stoakes 	return 0;
107849b1b8d6SLorenzo Stoakes }
107949b1b8d6SLorenzo Stoakes 
1080f8d112a4SLiam R. Howlett static inline void vms_clear_ptes(struct vma_munmap_struct *vms,
10819c3ebedaSLiam R. Howlett 		    struct ma_state *mas_detach, bool mm_wr_locked)
10829c3ebedaSLiam R. Howlett {
10839c3ebedaSLiam R. Howlett 	struct mmu_gather tlb;
10849c3ebedaSLiam R. Howlett 
1085f8d112a4SLiam R. Howlett 	if (!vms->clear_ptes) /* Nothing to do */
1086f8d112a4SLiam R. Howlett 		return;
1087f8d112a4SLiam R. Howlett 
10889c3ebedaSLiam R. Howlett 	/*
10899c3ebedaSLiam R. Howlett 	 * We can free page tables without write-locking mmap_lock because VMAs
10909c3ebedaSLiam R. Howlett 	 * were isolated before we downgraded mmap_lock.
10919c3ebedaSLiam R. Howlett 	 */
10929c3ebedaSLiam R. Howlett 	mas_set(mas_detach, 1);
10939c3ebedaSLiam R. Howlett 	lru_add_drain();
109463fc66f5SLiam R. Howlett 	tlb_gather_mmu(&tlb, vms->vma->vm_mm);
109563fc66f5SLiam R. Howlett 	update_hiwater_rss(vms->vma->vm_mm);
1096f8d112a4SLiam R. Howlett 	unmap_vmas(&tlb, mas_detach, vms->vma, vms->start, vms->end,
1097f8d112a4SLiam R. Howlett 		   vms->vma_count, mm_wr_locked);
1098f8d112a4SLiam R. Howlett 
10999c3ebedaSLiam R. Howlett 	mas_set(mas_detach, 1);
11009c3ebedaSLiam R. Howlett 	/* start and end may be different if there is no prev or next vma. */
1101f8d112a4SLiam R. Howlett 	free_pgtables(&tlb, mas_detach, vms->vma, vms->unmap_start,
1102f8d112a4SLiam R. Howlett 		      vms->unmap_end, mm_wr_locked);
11039c3ebedaSLiam R. Howlett 	tlb_finish_mmu(&tlb);
1104f8d112a4SLiam R. Howlett 	vms->clear_ptes = false;
1105f8d112a4SLiam R. Howlett }
1106f8d112a4SLiam R. Howlett 
1107f8d112a4SLiam R. Howlett void vms_clean_up_area(struct vma_munmap_struct *vms,
110863fc66f5SLiam R. Howlett 		struct ma_state *mas_detach)
1109f8d112a4SLiam R. Howlett {
1110f8d112a4SLiam R. Howlett 	struct vm_area_struct *vma;
1111f8d112a4SLiam R. Howlett 
1112f8d112a4SLiam R. Howlett 	if (!vms->nr_pages)
1113f8d112a4SLiam R. Howlett 		return;
1114f8d112a4SLiam R. Howlett 
111563fc66f5SLiam R. Howlett 	vms_clear_ptes(vms, mas_detach, true);
1116f8d112a4SLiam R. Howlett 	mas_set(mas_detach, 0);
1117f8d112a4SLiam R. Howlett 	mas_for_each(mas_detach, vma, ULONG_MAX)
1118f8d112a4SLiam R. Howlett 		if (vma->vm_ops && vma->vm_ops->close)
1119f8d112a4SLiam R. Howlett 			vma->vm_ops->close(vma);
1120f8d112a4SLiam R. Howlett 	vms->closed_vm_ops = true;
11219c3ebedaSLiam R. Howlett }
11229c3ebedaSLiam R. Howlett 
112349b1b8d6SLorenzo Stoakes /*
1124dba14840SLiam R. Howlett  * vms_complete_munmap_vmas() - Finish the munmap() operation
1125dba14840SLiam R. Howlett  * @vms: The vma munmap struct
1126dba14840SLiam R. Howlett  * @mas_detach: The maple state of the detached vmas
112701cf21e9SLiam R. Howlett  *
1128dba14840SLiam R. Howlett  * This updates the mm_struct, unmaps the region, frees the resources
112901cf21e9SLiam R. Howlett  * used for the munmap() and may downgrade the lock - if requested.  Everything
113001cf21e9SLiam R. Howlett  * needed to be done once the vma maple tree is updated.
113101cf21e9SLiam R. Howlett  */
11329014b230SLiam R. Howlett void vms_complete_munmap_vmas(struct vma_munmap_struct *vms,
1133dba14840SLiam R. Howlett 		struct ma_state *mas_detach)
113401cf21e9SLiam R. Howlett {
113517f1ae9bSLiam R. Howlett 	struct vm_area_struct *vma;
1136dba14840SLiam R. Howlett 	struct mm_struct *mm;
113701cf21e9SLiam R. Howlett 
113863fc66f5SLiam R. Howlett 	mm = current->mm;
1139dba14840SLiam R. Howlett 	mm->map_count -= vms->vma_count;
1140dba14840SLiam R. Howlett 	mm->locked_vm -= vms->locked_vm;
1141dba14840SLiam R. Howlett 	if (vms->unlock)
114201cf21e9SLiam R. Howlett 		mmap_write_downgrade(mm);
114301cf21e9SLiam R. Howlett 
1144f8d112a4SLiam R. Howlett 	if (!vms->nr_pages)
1145f8d112a4SLiam R. Howlett 		return;
1146f8d112a4SLiam R. Howlett 
1147f8d112a4SLiam R. Howlett 	vms_clear_ptes(vms, mas_detach, !vms->unlock);
114817f1ae9bSLiam R. Howlett 	/* Update high watermark before we lower total_vm */
114917f1ae9bSLiam R. Howlett 	update_hiwater_vm(mm);
115017f1ae9bSLiam R. Howlett 	/* Stat accounting */
115117f1ae9bSLiam R. Howlett 	WRITE_ONCE(mm->total_vm, READ_ONCE(mm->total_vm) - vms->nr_pages);
115217f1ae9bSLiam R. Howlett 	/* Paranoid bookkeeping */
115317f1ae9bSLiam R. Howlett 	VM_WARN_ON(vms->exec_vm > mm->exec_vm);
115417f1ae9bSLiam R. Howlett 	VM_WARN_ON(vms->stack_vm > mm->stack_vm);
115517f1ae9bSLiam R. Howlett 	VM_WARN_ON(vms->data_vm > mm->data_vm);
115617f1ae9bSLiam R. Howlett 	mm->exec_vm -= vms->exec_vm;
115717f1ae9bSLiam R. Howlett 	mm->stack_vm -= vms->stack_vm;
115817f1ae9bSLiam R. Howlett 	mm->data_vm -= vms->data_vm;
115917f1ae9bSLiam R. Howlett 
116017f1ae9bSLiam R. Howlett 	/* Remove and clean up vmas */
116101cf21e9SLiam R. Howlett 	mas_set(mas_detach, 0);
116217f1ae9bSLiam R. Howlett 	mas_for_each(mas_detach, vma, ULONG_MAX)
1163f8d112a4SLiam R. Howlett 		remove_vma(vma, /* = */ false, vms->closed_vm_ops);
116417f1ae9bSLiam R. Howlett 
116517f1ae9bSLiam R. Howlett 	vm_unacct_memory(vms->nr_accounted);
116601cf21e9SLiam R. Howlett 	validate_mm(mm);
1167dba14840SLiam R. Howlett 	if (vms->unlock)
116801cf21e9SLiam R. Howlett 		mmap_read_unlock(mm);
116901cf21e9SLiam R. Howlett 
117001cf21e9SLiam R. Howlett 	__mt_destroy(mas_detach->tree);
117101cf21e9SLiam R. Howlett }
117201cf21e9SLiam R. Howlett 
117301cf21e9SLiam R. Howlett /*
1174dba14840SLiam R. Howlett  * vms_gather_munmap_vmas() - Put all VMAs within a range into a maple tree
11756898c903SLiam R. Howlett  * for removal at a later date.  Handles splitting first and last if necessary
11766898c903SLiam R. Howlett  * and marking the vmas as isolated.
11776898c903SLiam R. Howlett  *
1178dba14840SLiam R. Howlett  * @vms: The vma munmap struct
11796898c903SLiam R. Howlett  * @mas_detach: The maple state tracking the detached tree
118049b1b8d6SLorenzo Stoakes  *
1181659c55efSXiao Yang  * Return: 0 on success, error otherwise
118249b1b8d6SLorenzo Stoakes  */
11839014b230SLiam R. Howlett int vms_gather_munmap_vmas(struct vma_munmap_struct *vms,
1184dba14840SLiam R. Howlett 		struct ma_state *mas_detach)
118549b1b8d6SLorenzo Stoakes {
118601cf21e9SLiam R. Howlett 	struct vm_area_struct *next = NULL;
1187659c55efSXiao Yang 	int error;
118849b1b8d6SLorenzo Stoakes 
118949b1b8d6SLorenzo Stoakes 	/*
119049b1b8d6SLorenzo Stoakes 	 * If we need to split any vma, do it now to save pain later.
119120831cd6SLiam R. Howlett 	 * Does it split the first one?
119249b1b8d6SLorenzo Stoakes 	 */
1193dba14840SLiam R. Howlett 	if (vms->start > vms->vma->vm_start) {
119449b1b8d6SLorenzo Stoakes 
119549b1b8d6SLorenzo Stoakes 		/*
119649b1b8d6SLorenzo Stoakes 		 * Make sure that map_count on return from munmap() will
119749b1b8d6SLorenzo Stoakes 		 * not exceed its limit; but let map_count go just above
119849b1b8d6SLorenzo Stoakes 		 * its limit temporarily, to help free resources as expected.
119949b1b8d6SLorenzo Stoakes 		 */
1200dba14840SLiam R. Howlett 		if (vms->end < vms->vma->vm_end &&
1201659c55efSXiao Yang 		    vms->vma->vm_mm->map_count >= sysctl_max_map_count) {
1202659c55efSXiao Yang 			error = -ENOMEM;
120349b1b8d6SLorenzo Stoakes 			goto map_count_exceeded;
1204659c55efSXiao Yang 		}
120549b1b8d6SLorenzo Stoakes 
1206df2a7df9SPedro Falcato 		/* Don't bother splitting the VMA if we can't unmap it anyway */
1207dba14840SLiam R. Howlett 		if (!can_modify_vma(vms->vma)) {
1208df2a7df9SPedro Falcato 			error = -EPERM;
1209df2a7df9SPedro Falcato 			goto start_split_failed;
1210df2a7df9SPedro Falcato 		}
1211df2a7df9SPedro Falcato 
1212659c55efSXiao Yang 		error = __split_vma(vms->vmi, vms->vma, vms->start, 1);
1213659c55efSXiao Yang 		if (error)
121449b1b8d6SLorenzo Stoakes 			goto start_split_failed;
121549b1b8d6SLorenzo Stoakes 	}
121617f1ae9bSLiam R. Howlett 	vms->prev = vma_prev(vms->vmi);
12179c3ebedaSLiam R. Howlett 	if (vms->prev)
12189c3ebedaSLiam R. Howlett 		vms->unmap_start = vms->prev->vm_end;
121949b1b8d6SLorenzo Stoakes 
122049b1b8d6SLorenzo Stoakes 	/*
122149b1b8d6SLorenzo Stoakes 	 * Detach a range of VMAs from the mm. Using next as a temp variable as
122249b1b8d6SLorenzo Stoakes 	 * it is always overwritten.
122349b1b8d6SLorenzo Stoakes 	 */
122417f1ae9bSLiam R. Howlett 	for_each_vma_range(*(vms->vmi), next, vms->end) {
122517f1ae9bSLiam R. Howlett 		long nrpages;
122617f1ae9bSLiam R. Howlett 
1227df2a7df9SPedro Falcato 		if (!can_modify_vma(next)) {
1228df2a7df9SPedro Falcato 			error = -EPERM;
1229df2a7df9SPedro Falcato 			goto modify_vma_failed;
1230df2a7df9SPedro Falcato 		}
123149b1b8d6SLorenzo Stoakes 		/* Does it split the end? */
1232dba14840SLiam R. Howlett 		if (next->vm_end > vms->end) {
1233659c55efSXiao Yang 			error = __split_vma(vms->vmi, next, vms->end, 0);
1234659c55efSXiao Yang 			if (error)
123549b1b8d6SLorenzo Stoakes 				goto end_split_failed;
123649b1b8d6SLorenzo Stoakes 		}
123749b1b8d6SLorenzo Stoakes 		vma_start_write(next);
1238dba14840SLiam R. Howlett 		mas_set(mas_detach, vms->vma_count++);
1239659c55efSXiao Yang 		error = mas_store_gfp(mas_detach, next, GFP_KERNEL);
1240659c55efSXiao Yang 		if (error)
124149b1b8d6SLorenzo Stoakes 			goto munmap_gather_failed;
12426898c903SLiam R. Howlett 
124349b1b8d6SLorenzo Stoakes 		vma_mark_detached(next, true);
124417f1ae9bSLiam R. Howlett 		nrpages = vma_pages(next);
124517f1ae9bSLiam R. Howlett 
124617f1ae9bSLiam R. Howlett 		vms->nr_pages += nrpages;
124749b1b8d6SLorenzo Stoakes 		if (next->vm_flags & VM_LOCKED)
124817f1ae9bSLiam R. Howlett 			vms->locked_vm += nrpages;
124917f1ae9bSLiam R. Howlett 
125017f1ae9bSLiam R. Howlett 		if (next->vm_flags & VM_ACCOUNT)
125117f1ae9bSLiam R. Howlett 			vms->nr_accounted += nrpages;
125217f1ae9bSLiam R. Howlett 
125317f1ae9bSLiam R. Howlett 		if (is_exec_mapping(next->vm_flags))
125417f1ae9bSLiam R. Howlett 			vms->exec_vm += nrpages;
125517f1ae9bSLiam R. Howlett 		else if (is_stack_mapping(next->vm_flags))
125617f1ae9bSLiam R. Howlett 			vms->stack_vm += nrpages;
125717f1ae9bSLiam R. Howlett 		else if (is_data_mapping(next->vm_flags))
125817f1ae9bSLiam R. Howlett 			vms->data_vm += nrpages;
125949b1b8d6SLorenzo Stoakes 
1260dba14840SLiam R. Howlett 		if (unlikely(vms->uf)) {
126149b1b8d6SLorenzo Stoakes 			/*
126249b1b8d6SLorenzo Stoakes 			 * If userfaultfd_unmap_prep returns an error the vmas
126349b1b8d6SLorenzo Stoakes 			 * will remain split, but userland will get a
126449b1b8d6SLorenzo Stoakes 			 * highly unexpected error anyway. This is no
126549b1b8d6SLorenzo Stoakes 			 * different than the case where the first of the two
126649b1b8d6SLorenzo Stoakes 			 * __split_vma fails, but we don't undo the first
126749b1b8d6SLorenzo Stoakes 			 * split, despite we could. This is unlikely enough
126849b1b8d6SLorenzo Stoakes 			 * failure that it's not worth optimizing it for.
126949b1b8d6SLorenzo Stoakes 			 */
1270659c55efSXiao Yang 			error = userfaultfd_unmap_prep(next, vms->start,
1271659c55efSXiao Yang 						       vms->end, vms->uf);
1272659c55efSXiao Yang 			if (error)
127349b1b8d6SLorenzo Stoakes 				goto userfaultfd_error;
127449b1b8d6SLorenzo Stoakes 		}
127549b1b8d6SLorenzo Stoakes #ifdef CONFIG_DEBUG_VM_MAPLE_TREE
1276dba14840SLiam R. Howlett 		BUG_ON(next->vm_start < vms->start);
1277dba14840SLiam R. Howlett 		BUG_ON(next->vm_start > vms->end);
127849b1b8d6SLorenzo Stoakes #endif
127917f1ae9bSLiam R. Howlett 	}
128017f1ae9bSLiam R. Howlett 
128117f1ae9bSLiam R. Howlett 	vms->next = vma_next(vms->vmi);
12829c3ebedaSLiam R. Howlett 	if (vms->next)
12839c3ebedaSLiam R. Howlett 		vms->unmap_end = vms->next->vm_start;
128449b1b8d6SLorenzo Stoakes 
128549b1b8d6SLorenzo Stoakes #if defined(CONFIG_DEBUG_VM_MAPLE_TREE)
128649b1b8d6SLorenzo Stoakes 	/* Make sure no VMAs are about to be lost. */
128749b1b8d6SLorenzo Stoakes 	{
12886898c903SLiam R. Howlett 		MA_STATE(test, mas_detach->tree, 0, 0);
128949b1b8d6SLorenzo Stoakes 		struct vm_area_struct *vma_mas, *vma_test;
129049b1b8d6SLorenzo Stoakes 		int test_count = 0;
129149b1b8d6SLorenzo Stoakes 
1292dba14840SLiam R. Howlett 		vma_iter_set(vms->vmi, vms->start);
129349b1b8d6SLorenzo Stoakes 		rcu_read_lock();
1294dba14840SLiam R. Howlett 		vma_test = mas_find(&test, vms->vma_count - 1);
1295dba14840SLiam R. Howlett 		for_each_vma_range(*(vms->vmi), vma_mas, vms->end) {
129649b1b8d6SLorenzo Stoakes 			BUG_ON(vma_mas != vma_test);
129749b1b8d6SLorenzo Stoakes 			test_count++;
1298dba14840SLiam R. Howlett 			vma_test = mas_next(&test, vms->vma_count - 1);
129949b1b8d6SLorenzo Stoakes 		}
130049b1b8d6SLorenzo Stoakes 		rcu_read_unlock();
1301dba14840SLiam R. Howlett 		BUG_ON(vms->vma_count != test_count);
130249b1b8d6SLorenzo Stoakes 	}
130349b1b8d6SLorenzo Stoakes #endif
130449b1b8d6SLorenzo Stoakes 
1305dba14840SLiam R. Howlett 	while (vma_iter_addr(vms->vmi) > vms->start)
1306dba14840SLiam R. Howlett 		vma_iter_prev_range(vms->vmi);
130749b1b8d6SLorenzo Stoakes 
1308f8d112a4SLiam R. Howlett 	vms->clear_ptes = true;
13096898c903SLiam R. Howlett 	return 0;
13106898c903SLiam R. Howlett 
13116898c903SLiam R. Howlett userfaultfd_error:
13126898c903SLiam R. Howlett munmap_gather_failed:
13136898c903SLiam R. Howlett end_split_failed:
13146898c903SLiam R. Howlett modify_vma_failed:
13154f87153eSLiam R. Howlett 	reattach_vmas(mas_detach);
13166898c903SLiam R. Howlett start_split_failed:
13176898c903SLiam R. Howlett map_count_exceeded:
13186898c903SLiam R. Howlett 	return error;
13196898c903SLiam R. Howlett }
13206898c903SLiam R. Howlett 
13216898c903SLiam R. Howlett /*
13226898c903SLiam R. Howlett  * do_vmi_align_munmap() - munmap the aligned region from @start to @end.
13236898c903SLiam R. Howlett  * @vmi: The vma iterator
13246898c903SLiam R. Howlett  * @vma: The starting vm_area_struct
13256898c903SLiam R. Howlett  * @mm: The mm_struct
13266898c903SLiam R. Howlett  * @start: The aligned start address to munmap.
13276898c903SLiam R. Howlett  * @end: The aligned end address to munmap.
13286898c903SLiam R. Howlett  * @uf: The userfaultfd list_head
13296898c903SLiam R. Howlett  * @unlock: Set to true to drop the mmap_lock.  unlocking only happens on
13306898c903SLiam R. Howlett  * success.
13316898c903SLiam R. Howlett  *
13326898c903SLiam R. Howlett  * Return: 0 on success and drops the lock if so directed, error and leaves the
13336898c903SLiam R. Howlett  * lock held otherwise.
13346898c903SLiam R. Howlett  */
13356898c903SLiam R. Howlett int do_vmi_align_munmap(struct vma_iterator *vmi, struct vm_area_struct *vma,
13366898c903SLiam R. Howlett 		struct mm_struct *mm, unsigned long start, unsigned long end,
13376898c903SLiam R. Howlett 		struct list_head *uf, bool unlock)
13386898c903SLiam R. Howlett {
13396898c903SLiam R. Howlett 	struct maple_tree mt_detach;
13406898c903SLiam R. Howlett 	MA_STATE(mas_detach, &mt_detach, 0, 0);
13416898c903SLiam R. Howlett 	mt_init_flags(&mt_detach, vmi->mas.tree->ma_flags & MT_FLAGS_LOCK_MASK);
13426898c903SLiam R. Howlett 	mt_on_stack(mt_detach);
1343dba14840SLiam R. Howlett 	struct vma_munmap_struct vms;
13446898c903SLiam R. Howlett 	int error;
13456898c903SLiam R. Howlett 
1346dba14840SLiam R. Howlett 	init_vma_munmap(&vms, vmi, vma, start, end, uf, unlock);
1347dba14840SLiam R. Howlett 	error = vms_gather_munmap_vmas(&vms, &mas_detach);
13486898c903SLiam R. Howlett 	if (error)
13496898c903SLiam R. Howlett 		goto gather_failed;
13506898c903SLiam R. Howlett 
135149b1b8d6SLorenzo Stoakes 	error = vma_iter_clear_gfp(vmi, start, end, GFP_KERNEL);
135249b1b8d6SLorenzo Stoakes 	if (error)
135349b1b8d6SLorenzo Stoakes 		goto clear_tree_failed;
135449b1b8d6SLorenzo Stoakes 
135549b1b8d6SLorenzo Stoakes 	/* Point of no return */
1356dba14840SLiam R. Howlett 	vms_complete_munmap_vmas(&vms, &mas_detach);
135749b1b8d6SLorenzo Stoakes 	return 0;
135849b1b8d6SLorenzo Stoakes 
135949b1b8d6SLorenzo Stoakes clear_tree_failed:
13604f87153eSLiam R. Howlett 	reattach_vmas(&mas_detach);
13616898c903SLiam R. Howlett gather_failed:
136249b1b8d6SLorenzo Stoakes 	validate_mm(mm);
136349b1b8d6SLorenzo Stoakes 	return error;
136449b1b8d6SLorenzo Stoakes }
136549b1b8d6SLorenzo Stoakes 
136649b1b8d6SLorenzo Stoakes /*
136749b1b8d6SLorenzo Stoakes  * do_vmi_munmap() - munmap a given range.
136849b1b8d6SLorenzo Stoakes  * @vmi: The vma iterator
136949b1b8d6SLorenzo Stoakes  * @mm: The mm_struct
137049b1b8d6SLorenzo Stoakes  * @start: The start address to munmap
137149b1b8d6SLorenzo Stoakes  * @len: The length of the range to munmap
137249b1b8d6SLorenzo Stoakes  * @uf: The userfaultfd list_head
137349b1b8d6SLorenzo Stoakes  * @unlock: set to true if the user wants to drop the mmap_lock on success
137449b1b8d6SLorenzo Stoakes  *
137549b1b8d6SLorenzo Stoakes  * This function takes a @mas that is either pointing to the previous VMA or set
137649b1b8d6SLorenzo Stoakes  * to MA_START and sets it up to remove the mapping(s).  The @len will be
137740b88644SMichael Ellerman  * aligned.
137849b1b8d6SLorenzo Stoakes  *
137949b1b8d6SLorenzo Stoakes  * Return: 0 on success and drops the lock if so directed, error and leaves the
138049b1b8d6SLorenzo Stoakes  * lock held otherwise.
138149b1b8d6SLorenzo Stoakes  */
138249b1b8d6SLorenzo Stoakes int do_vmi_munmap(struct vma_iterator *vmi, struct mm_struct *mm,
138349b1b8d6SLorenzo Stoakes 		  unsigned long start, size_t len, struct list_head *uf,
138449b1b8d6SLorenzo Stoakes 		  bool unlock)
138549b1b8d6SLorenzo Stoakes {
138649b1b8d6SLorenzo Stoakes 	unsigned long end;
138749b1b8d6SLorenzo Stoakes 	struct vm_area_struct *vma;
138849b1b8d6SLorenzo Stoakes 
138949b1b8d6SLorenzo Stoakes 	if ((offset_in_page(start)) || start > TASK_SIZE || len > TASK_SIZE-start)
139049b1b8d6SLorenzo Stoakes 		return -EINVAL;
139149b1b8d6SLorenzo Stoakes 
139249b1b8d6SLorenzo Stoakes 	end = start + PAGE_ALIGN(len);
139349b1b8d6SLorenzo Stoakes 	if (end == start)
139449b1b8d6SLorenzo Stoakes 		return -EINVAL;
139549b1b8d6SLorenzo Stoakes 
139649b1b8d6SLorenzo Stoakes 	/* Find the first overlapping VMA */
139749b1b8d6SLorenzo Stoakes 	vma = vma_find(vmi, end);
139849b1b8d6SLorenzo Stoakes 	if (!vma) {
139949b1b8d6SLorenzo Stoakes 		if (unlock)
140049b1b8d6SLorenzo Stoakes 			mmap_write_unlock(mm);
140149b1b8d6SLorenzo Stoakes 		return 0;
140249b1b8d6SLorenzo Stoakes 	}
140349b1b8d6SLorenzo Stoakes 
140449b1b8d6SLorenzo Stoakes 	return do_vmi_align_munmap(vmi, vma, mm, start, end, uf, unlock);
140549b1b8d6SLorenzo Stoakes }
140649b1b8d6SLorenzo Stoakes 
140749b1b8d6SLorenzo Stoakes /*
140849b1b8d6SLorenzo Stoakes  * We are about to modify one or multiple of a VMA's flags, policy, userfaultfd
140949b1b8d6SLorenzo Stoakes  * context and anonymous VMA name within the range [start, end).
141049b1b8d6SLorenzo Stoakes  *
141149b1b8d6SLorenzo Stoakes  * As a result, we might be able to merge the newly modified VMA range with an
141249b1b8d6SLorenzo Stoakes  * adjacent VMA with identical properties.
141349b1b8d6SLorenzo Stoakes  *
141449b1b8d6SLorenzo Stoakes  * If no merge is possible and the range does not span the entirety of the VMA,
141549b1b8d6SLorenzo Stoakes  * we then need to split the VMA to accommodate the change.
141649b1b8d6SLorenzo Stoakes  *
141749b1b8d6SLorenzo Stoakes  * The function returns either the merged VMA, the original VMA if a split was
141849b1b8d6SLorenzo Stoakes  * required instead, or an error if the split failed.
141949b1b8d6SLorenzo Stoakes  */
14202f1c6611SLorenzo Stoakes static struct vm_area_struct *vma_modify(struct vma_merge_struct *vmg)
142149b1b8d6SLorenzo Stoakes {
14222f1c6611SLorenzo Stoakes 	struct vm_area_struct *vma = vmg->vma;
142349b1b8d6SLorenzo Stoakes 	struct vm_area_struct *merged;
142449b1b8d6SLorenzo Stoakes 
14252f1c6611SLorenzo Stoakes 	/* First, try to merge. */
1426cc8cb369SLorenzo Stoakes 	merged = vma_merge_existing_range(vmg);
142749b1b8d6SLorenzo Stoakes 	if (merged)
142849b1b8d6SLorenzo Stoakes 		return merged;
142949b1b8d6SLorenzo Stoakes 
14302f1c6611SLorenzo Stoakes 	/* Split any preceding portion of the VMA. */
14312f1c6611SLorenzo Stoakes 	if (vma->vm_start < vmg->start) {
14322f1c6611SLorenzo Stoakes 		int err = split_vma(vmg->vmi, vma, vmg->start, 1);
143349b1b8d6SLorenzo Stoakes 
143449b1b8d6SLorenzo Stoakes 		if (err)
143549b1b8d6SLorenzo Stoakes 			return ERR_PTR(err);
143649b1b8d6SLorenzo Stoakes 	}
143749b1b8d6SLorenzo Stoakes 
14382f1c6611SLorenzo Stoakes 	/* Split any trailing portion of the VMA. */
14392f1c6611SLorenzo Stoakes 	if (vma->vm_end > vmg->end) {
14402f1c6611SLorenzo Stoakes 		int err = split_vma(vmg->vmi, vma, vmg->end, 0);
144149b1b8d6SLorenzo Stoakes 
144249b1b8d6SLorenzo Stoakes 		if (err)
144349b1b8d6SLorenzo Stoakes 			return ERR_PTR(err);
144449b1b8d6SLorenzo Stoakes 	}
144549b1b8d6SLorenzo Stoakes 
144649b1b8d6SLorenzo Stoakes 	return vma;
144749b1b8d6SLorenzo Stoakes }
144849b1b8d6SLorenzo Stoakes 
14492f1c6611SLorenzo Stoakes struct vm_area_struct *vma_modify_flags(
14502f1c6611SLorenzo Stoakes 	struct vma_iterator *vmi, struct vm_area_struct *prev,
14512f1c6611SLorenzo Stoakes 	struct vm_area_struct *vma, unsigned long start, unsigned long end,
14522f1c6611SLorenzo Stoakes 	unsigned long new_flags)
14532f1c6611SLorenzo Stoakes {
14542f1c6611SLorenzo Stoakes 	VMG_VMA_STATE(vmg, vmi, prev, vma, start, end);
14552f1c6611SLorenzo Stoakes 
14562f1c6611SLorenzo Stoakes 	vmg.flags = new_flags;
14572f1c6611SLorenzo Stoakes 
14582f1c6611SLorenzo Stoakes 	return vma_modify(&vmg);
14592f1c6611SLorenzo Stoakes }
14602f1c6611SLorenzo Stoakes 
14612f1c6611SLorenzo Stoakes struct vm_area_struct
14622f1c6611SLorenzo Stoakes *vma_modify_flags_name(struct vma_iterator *vmi,
14632f1c6611SLorenzo Stoakes 		       struct vm_area_struct *prev,
14642f1c6611SLorenzo Stoakes 		       struct vm_area_struct *vma,
14652f1c6611SLorenzo Stoakes 		       unsigned long start,
14662f1c6611SLorenzo Stoakes 		       unsigned long end,
14672f1c6611SLorenzo Stoakes 		       unsigned long new_flags,
14682f1c6611SLorenzo Stoakes 		       struct anon_vma_name *new_name)
14692f1c6611SLorenzo Stoakes {
14702f1c6611SLorenzo Stoakes 	VMG_VMA_STATE(vmg, vmi, prev, vma, start, end);
14712f1c6611SLorenzo Stoakes 
14722f1c6611SLorenzo Stoakes 	vmg.flags = new_flags;
14732f1c6611SLorenzo Stoakes 	vmg.anon_name = new_name;
14742f1c6611SLorenzo Stoakes 
14752f1c6611SLorenzo Stoakes 	return vma_modify(&vmg);
14762f1c6611SLorenzo Stoakes }
14772f1c6611SLorenzo Stoakes 
14782f1c6611SLorenzo Stoakes struct vm_area_struct
14792f1c6611SLorenzo Stoakes *vma_modify_policy(struct vma_iterator *vmi,
14802f1c6611SLorenzo Stoakes 		   struct vm_area_struct *prev,
14812f1c6611SLorenzo Stoakes 		   struct vm_area_struct *vma,
14822f1c6611SLorenzo Stoakes 		   unsigned long start, unsigned long end,
14832f1c6611SLorenzo Stoakes 		   struct mempolicy *new_pol)
14842f1c6611SLorenzo Stoakes {
14852f1c6611SLorenzo Stoakes 	VMG_VMA_STATE(vmg, vmi, prev, vma, start, end);
14862f1c6611SLorenzo Stoakes 
14872f1c6611SLorenzo Stoakes 	vmg.policy = new_pol;
14882f1c6611SLorenzo Stoakes 
14892f1c6611SLorenzo Stoakes 	return vma_modify(&vmg);
14902f1c6611SLorenzo Stoakes }
14912f1c6611SLorenzo Stoakes 
14922f1c6611SLorenzo Stoakes struct vm_area_struct
14932f1c6611SLorenzo Stoakes *vma_modify_flags_uffd(struct vma_iterator *vmi,
14942f1c6611SLorenzo Stoakes 		       struct vm_area_struct *prev,
14952f1c6611SLorenzo Stoakes 		       struct vm_area_struct *vma,
14962f1c6611SLorenzo Stoakes 		       unsigned long start, unsigned long end,
14972f1c6611SLorenzo Stoakes 		       unsigned long new_flags,
14982f1c6611SLorenzo Stoakes 		       struct vm_userfaultfd_ctx new_ctx)
14992f1c6611SLorenzo Stoakes {
15002f1c6611SLorenzo Stoakes 	VMG_VMA_STATE(vmg, vmi, prev, vma, start, end);
15012f1c6611SLorenzo Stoakes 
15022f1c6611SLorenzo Stoakes 	vmg.flags = new_flags;
15032f1c6611SLorenzo Stoakes 	vmg.uffd_ctx = new_ctx;
15042f1c6611SLorenzo Stoakes 
15052f1c6611SLorenzo Stoakes 	return vma_modify(&vmg);
15062f1c6611SLorenzo Stoakes }
15072f1c6611SLorenzo Stoakes 
150849b1b8d6SLorenzo Stoakes /*
150949b1b8d6SLorenzo Stoakes  * Expand vma by delta bytes, potentially merging with an immediately adjacent
151049b1b8d6SLorenzo Stoakes  * VMA with identical properties.
151149b1b8d6SLorenzo Stoakes  */
151249b1b8d6SLorenzo Stoakes struct vm_area_struct *vma_merge_extend(struct vma_iterator *vmi,
151349b1b8d6SLorenzo Stoakes 					struct vm_area_struct *vma,
151449b1b8d6SLorenzo Stoakes 					unsigned long delta)
151549b1b8d6SLorenzo Stoakes {
15162f1c6611SLorenzo Stoakes 	VMG_VMA_STATE(vmg, vmi, vma, vma, vma->vm_end, vma->vm_end + delta);
151749b1b8d6SLorenzo Stoakes 
1518cacded5eSLorenzo Stoakes 	vmg.next = vma_iter_next_rewind(vmi, NULL);
1519cacded5eSLorenzo Stoakes 	vmg.vma = NULL; /* We use the VMA to populate VMG fields only. */
1520cacded5eSLorenzo Stoakes 
1521cacded5eSLorenzo Stoakes 	return vma_merge_new_range(&vmg);
152249b1b8d6SLorenzo Stoakes }
152349b1b8d6SLorenzo Stoakes 
152449b1b8d6SLorenzo Stoakes void unlink_file_vma_batch_init(struct unlink_vma_file_batch *vb)
152549b1b8d6SLorenzo Stoakes {
152649b1b8d6SLorenzo Stoakes 	vb->count = 0;
152749b1b8d6SLorenzo Stoakes }
152849b1b8d6SLorenzo Stoakes 
152949b1b8d6SLorenzo Stoakes static void unlink_file_vma_batch_process(struct unlink_vma_file_batch *vb)
153049b1b8d6SLorenzo Stoakes {
153149b1b8d6SLorenzo Stoakes 	struct address_space *mapping;
153249b1b8d6SLorenzo Stoakes 	int i;
153349b1b8d6SLorenzo Stoakes 
153449b1b8d6SLorenzo Stoakes 	mapping = vb->vmas[0]->vm_file->f_mapping;
153549b1b8d6SLorenzo Stoakes 	i_mmap_lock_write(mapping);
153649b1b8d6SLorenzo Stoakes 	for (i = 0; i < vb->count; i++) {
153749b1b8d6SLorenzo Stoakes 		VM_WARN_ON_ONCE(vb->vmas[i]->vm_file->f_mapping != mapping);
153849b1b8d6SLorenzo Stoakes 		__remove_shared_vm_struct(vb->vmas[i], mapping);
153949b1b8d6SLorenzo Stoakes 	}
154049b1b8d6SLorenzo Stoakes 	i_mmap_unlock_write(mapping);
154149b1b8d6SLorenzo Stoakes 
154249b1b8d6SLorenzo Stoakes 	unlink_file_vma_batch_init(vb);
154349b1b8d6SLorenzo Stoakes }
154449b1b8d6SLorenzo Stoakes 
154549b1b8d6SLorenzo Stoakes void unlink_file_vma_batch_add(struct unlink_vma_file_batch *vb,
154649b1b8d6SLorenzo Stoakes 			       struct vm_area_struct *vma)
154749b1b8d6SLorenzo Stoakes {
154849b1b8d6SLorenzo Stoakes 	if (vma->vm_file == NULL)
154949b1b8d6SLorenzo Stoakes 		return;
155049b1b8d6SLorenzo Stoakes 
155149b1b8d6SLorenzo Stoakes 	if ((vb->count > 0 && vb->vmas[0]->vm_file != vma->vm_file) ||
155249b1b8d6SLorenzo Stoakes 	    vb->count == ARRAY_SIZE(vb->vmas))
155349b1b8d6SLorenzo Stoakes 		unlink_file_vma_batch_process(vb);
155449b1b8d6SLorenzo Stoakes 
155549b1b8d6SLorenzo Stoakes 	vb->vmas[vb->count] = vma;
155649b1b8d6SLorenzo Stoakes 	vb->count++;
155749b1b8d6SLorenzo Stoakes }
155849b1b8d6SLorenzo Stoakes 
155949b1b8d6SLorenzo Stoakes void unlink_file_vma_batch_final(struct unlink_vma_file_batch *vb)
156049b1b8d6SLorenzo Stoakes {
156149b1b8d6SLorenzo Stoakes 	if (vb->count > 0)
156249b1b8d6SLorenzo Stoakes 		unlink_file_vma_batch_process(vb);
156349b1b8d6SLorenzo Stoakes }
156449b1b8d6SLorenzo Stoakes 
156549b1b8d6SLorenzo Stoakes /*
156649b1b8d6SLorenzo Stoakes  * Unlink a file-based vm structure from its interval tree, to hide
156749b1b8d6SLorenzo Stoakes  * vma from rmap and vmtruncate before freeing its page tables.
156849b1b8d6SLorenzo Stoakes  */
156949b1b8d6SLorenzo Stoakes void unlink_file_vma(struct vm_area_struct *vma)
157049b1b8d6SLorenzo Stoakes {
157149b1b8d6SLorenzo Stoakes 	struct file *file = vma->vm_file;
157249b1b8d6SLorenzo Stoakes 
157349b1b8d6SLorenzo Stoakes 	if (file) {
157449b1b8d6SLorenzo Stoakes 		struct address_space *mapping = file->f_mapping;
157549b1b8d6SLorenzo Stoakes 
157649b1b8d6SLorenzo Stoakes 		i_mmap_lock_write(mapping);
157749b1b8d6SLorenzo Stoakes 		__remove_shared_vm_struct(vma, mapping);
157849b1b8d6SLorenzo Stoakes 		i_mmap_unlock_write(mapping);
157949b1b8d6SLorenzo Stoakes 	}
158049b1b8d6SLorenzo Stoakes }
158149b1b8d6SLorenzo Stoakes 
158249b1b8d6SLorenzo Stoakes void vma_link_file(struct vm_area_struct *vma)
158349b1b8d6SLorenzo Stoakes {
158449b1b8d6SLorenzo Stoakes 	struct file *file = vma->vm_file;
158549b1b8d6SLorenzo Stoakes 	struct address_space *mapping;
158649b1b8d6SLorenzo Stoakes 
158749b1b8d6SLorenzo Stoakes 	if (file) {
158849b1b8d6SLorenzo Stoakes 		mapping = file->f_mapping;
158949b1b8d6SLorenzo Stoakes 		i_mmap_lock_write(mapping);
159049b1b8d6SLorenzo Stoakes 		__vma_link_file(vma, mapping);
159149b1b8d6SLorenzo Stoakes 		i_mmap_unlock_write(mapping);
159249b1b8d6SLorenzo Stoakes 	}
159349b1b8d6SLorenzo Stoakes }
159449b1b8d6SLorenzo Stoakes 
159549b1b8d6SLorenzo Stoakes int vma_link(struct mm_struct *mm, struct vm_area_struct *vma)
159649b1b8d6SLorenzo Stoakes {
159749b1b8d6SLorenzo Stoakes 	VMA_ITERATOR(vmi, mm, 0);
159849b1b8d6SLorenzo Stoakes 
159949b1b8d6SLorenzo Stoakes 	vma_iter_config(&vmi, vma->vm_start, vma->vm_end);
160049b1b8d6SLorenzo Stoakes 	if (vma_iter_prealloc(&vmi, vma))
160149b1b8d6SLorenzo Stoakes 		return -ENOMEM;
160249b1b8d6SLorenzo Stoakes 
160349b1b8d6SLorenzo Stoakes 	vma_start_write(vma);
160449b1b8d6SLorenzo Stoakes 	vma_iter_store(&vmi, vma);
160549b1b8d6SLorenzo Stoakes 	vma_link_file(vma);
160649b1b8d6SLorenzo Stoakes 	mm->map_count++;
160749b1b8d6SLorenzo Stoakes 	validate_mm(mm);
160849b1b8d6SLorenzo Stoakes 	return 0;
160949b1b8d6SLorenzo Stoakes }
161049b1b8d6SLorenzo Stoakes 
161149b1b8d6SLorenzo Stoakes /*
161249b1b8d6SLorenzo Stoakes  * Copy the vma structure to a new location in the same mm,
161349b1b8d6SLorenzo Stoakes  * prior to moving page table entries, to effect an mremap move.
161449b1b8d6SLorenzo Stoakes  */
161549b1b8d6SLorenzo Stoakes struct vm_area_struct *copy_vma(struct vm_area_struct **vmap,
161649b1b8d6SLorenzo Stoakes 	unsigned long addr, unsigned long len, pgoff_t pgoff,
161749b1b8d6SLorenzo Stoakes 	bool *need_rmap_locks)
161849b1b8d6SLorenzo Stoakes {
161949b1b8d6SLorenzo Stoakes 	struct vm_area_struct *vma = *vmap;
162049b1b8d6SLorenzo Stoakes 	unsigned long vma_start = vma->vm_start;
162149b1b8d6SLorenzo Stoakes 	struct mm_struct *mm = vma->vm_mm;
1622cacded5eSLorenzo Stoakes 	struct vm_area_struct *new_vma;
162349b1b8d6SLorenzo Stoakes 	bool faulted_in_anon_vma = true;
162449b1b8d6SLorenzo Stoakes 	VMA_ITERATOR(vmi, mm, addr);
1625cacded5eSLorenzo Stoakes 	VMG_VMA_STATE(vmg, &vmi, NULL, vma, addr, addr + len);
162649b1b8d6SLorenzo Stoakes 
162749b1b8d6SLorenzo Stoakes 	/*
162849b1b8d6SLorenzo Stoakes 	 * If anonymous vma has not yet been faulted, update new pgoff
162949b1b8d6SLorenzo Stoakes 	 * to match new location, to increase its chance of merging.
163049b1b8d6SLorenzo Stoakes 	 */
163149b1b8d6SLorenzo Stoakes 	if (unlikely(vma_is_anonymous(vma) && !vma->anon_vma)) {
163249b1b8d6SLorenzo Stoakes 		pgoff = addr >> PAGE_SHIFT;
163349b1b8d6SLorenzo Stoakes 		faulted_in_anon_vma = false;
163449b1b8d6SLorenzo Stoakes 	}
163549b1b8d6SLorenzo Stoakes 
1636cacded5eSLorenzo Stoakes 	new_vma = find_vma_prev(mm, addr, &vmg.prev);
163749b1b8d6SLorenzo Stoakes 	if (new_vma && new_vma->vm_start < addr + len)
163849b1b8d6SLorenzo Stoakes 		return NULL;	/* should never get here */
163949b1b8d6SLorenzo Stoakes 
1640cacded5eSLorenzo Stoakes 	vmg.vma = NULL; /* New VMA range. */
1641cacded5eSLorenzo Stoakes 	vmg.pgoff = pgoff;
1642cacded5eSLorenzo Stoakes 	vmg.next = vma_iter_next_rewind(&vmi, NULL);
1643cacded5eSLorenzo Stoakes 	new_vma = vma_merge_new_range(&vmg);
1644cacded5eSLorenzo Stoakes 
164549b1b8d6SLorenzo Stoakes 	if (new_vma) {
164649b1b8d6SLorenzo Stoakes 		/*
164749b1b8d6SLorenzo Stoakes 		 * Source vma may have been merged into new_vma
164849b1b8d6SLorenzo Stoakes 		 */
164949b1b8d6SLorenzo Stoakes 		if (unlikely(vma_start >= new_vma->vm_start &&
165049b1b8d6SLorenzo Stoakes 			     vma_start < new_vma->vm_end)) {
165149b1b8d6SLorenzo Stoakes 			/*
165249b1b8d6SLorenzo Stoakes 			 * The only way we can get a vma_merge with
165349b1b8d6SLorenzo Stoakes 			 * self during an mremap is if the vma hasn't
165449b1b8d6SLorenzo Stoakes 			 * been faulted in yet and we were allowed to
165549b1b8d6SLorenzo Stoakes 			 * reset the dst vma->vm_pgoff to the
165649b1b8d6SLorenzo Stoakes 			 * destination address of the mremap to allow
165749b1b8d6SLorenzo Stoakes 			 * the merge to happen. mremap must change the
165849b1b8d6SLorenzo Stoakes 			 * vm_pgoff linearity between src and dst vmas
165949b1b8d6SLorenzo Stoakes 			 * (in turn preventing a vma_merge) to be
166049b1b8d6SLorenzo Stoakes 			 * safe. It is only safe to keep the vm_pgoff
166149b1b8d6SLorenzo Stoakes 			 * linear if there are no pages mapped yet.
166249b1b8d6SLorenzo Stoakes 			 */
166349b1b8d6SLorenzo Stoakes 			VM_BUG_ON_VMA(faulted_in_anon_vma, new_vma);
166449b1b8d6SLorenzo Stoakes 			*vmap = vma = new_vma;
166549b1b8d6SLorenzo Stoakes 		}
166649b1b8d6SLorenzo Stoakes 		*need_rmap_locks = (new_vma->vm_pgoff <= vma->vm_pgoff);
166749b1b8d6SLorenzo Stoakes 	} else {
166849b1b8d6SLorenzo Stoakes 		new_vma = vm_area_dup(vma);
166949b1b8d6SLorenzo Stoakes 		if (!new_vma)
167049b1b8d6SLorenzo Stoakes 			goto out;
167149b1b8d6SLorenzo Stoakes 		vma_set_range(new_vma, addr, addr + len, pgoff);
167249b1b8d6SLorenzo Stoakes 		if (vma_dup_policy(vma, new_vma))
167349b1b8d6SLorenzo Stoakes 			goto out_free_vma;
167449b1b8d6SLorenzo Stoakes 		if (anon_vma_clone(new_vma, vma))
167549b1b8d6SLorenzo Stoakes 			goto out_free_mempol;
167649b1b8d6SLorenzo Stoakes 		if (new_vma->vm_file)
167749b1b8d6SLorenzo Stoakes 			get_file(new_vma->vm_file);
167849b1b8d6SLorenzo Stoakes 		if (new_vma->vm_ops && new_vma->vm_ops->open)
167949b1b8d6SLorenzo Stoakes 			new_vma->vm_ops->open(new_vma);
168049b1b8d6SLorenzo Stoakes 		if (vma_link(mm, new_vma))
168149b1b8d6SLorenzo Stoakes 			goto out_vma_link;
168249b1b8d6SLorenzo Stoakes 		*need_rmap_locks = false;
168349b1b8d6SLorenzo Stoakes 	}
168449b1b8d6SLorenzo Stoakes 	return new_vma;
168549b1b8d6SLorenzo Stoakes 
168649b1b8d6SLorenzo Stoakes out_vma_link:
168749b1b8d6SLorenzo Stoakes 	if (new_vma->vm_ops && new_vma->vm_ops->close)
168849b1b8d6SLorenzo Stoakes 		new_vma->vm_ops->close(new_vma);
168949b1b8d6SLorenzo Stoakes 
169049b1b8d6SLorenzo Stoakes 	if (new_vma->vm_file)
169149b1b8d6SLorenzo Stoakes 		fput(new_vma->vm_file);
169249b1b8d6SLorenzo Stoakes 
169349b1b8d6SLorenzo Stoakes 	unlink_anon_vmas(new_vma);
169449b1b8d6SLorenzo Stoakes out_free_mempol:
169549b1b8d6SLorenzo Stoakes 	mpol_put(vma_policy(new_vma));
169649b1b8d6SLorenzo Stoakes out_free_vma:
169749b1b8d6SLorenzo Stoakes 	vm_area_free(new_vma);
169849b1b8d6SLorenzo Stoakes out:
169949b1b8d6SLorenzo Stoakes 	return NULL;
170049b1b8d6SLorenzo Stoakes }
170149b1b8d6SLorenzo Stoakes 
170249b1b8d6SLorenzo Stoakes /*
170349b1b8d6SLorenzo Stoakes  * Rough compatibility check to quickly see if it's even worth looking
170449b1b8d6SLorenzo Stoakes  * at sharing an anon_vma.
170549b1b8d6SLorenzo Stoakes  *
170649b1b8d6SLorenzo Stoakes  * They need to have the same vm_file, and the flags can only differ
170749b1b8d6SLorenzo Stoakes  * in things that mprotect may change.
170849b1b8d6SLorenzo Stoakes  *
170949b1b8d6SLorenzo Stoakes  * NOTE! The fact that we share an anon_vma doesn't _have_ to mean that
171049b1b8d6SLorenzo Stoakes  * we can merge the two vma's. For example, we refuse to merge a vma if
171149b1b8d6SLorenzo Stoakes  * there is a vm_ops->close() function, because that indicates that the
171249b1b8d6SLorenzo Stoakes  * driver is doing some kind of reference counting. But that doesn't
171349b1b8d6SLorenzo Stoakes  * really matter for the anon_vma sharing case.
171449b1b8d6SLorenzo Stoakes  */
171549b1b8d6SLorenzo Stoakes static int anon_vma_compatible(struct vm_area_struct *a, struct vm_area_struct *b)
171649b1b8d6SLorenzo Stoakes {
171749b1b8d6SLorenzo Stoakes 	return a->vm_end == b->vm_start &&
171849b1b8d6SLorenzo Stoakes 		mpol_equal(vma_policy(a), vma_policy(b)) &&
171949b1b8d6SLorenzo Stoakes 		a->vm_file == b->vm_file &&
172049b1b8d6SLorenzo Stoakes 		!((a->vm_flags ^ b->vm_flags) & ~(VM_ACCESS_FLAGS | VM_SOFTDIRTY)) &&
172149b1b8d6SLorenzo Stoakes 		b->vm_pgoff == a->vm_pgoff + ((b->vm_start - a->vm_start) >> PAGE_SHIFT);
172249b1b8d6SLorenzo Stoakes }
172349b1b8d6SLorenzo Stoakes 
172449b1b8d6SLorenzo Stoakes /*
172549b1b8d6SLorenzo Stoakes  * Do some basic sanity checking to see if we can re-use the anon_vma
172649b1b8d6SLorenzo Stoakes  * from 'old'. The 'a'/'b' vma's are in VM order - one of them will be
172749b1b8d6SLorenzo Stoakes  * the same as 'old', the other will be the new one that is trying
172849b1b8d6SLorenzo Stoakes  * to share the anon_vma.
172949b1b8d6SLorenzo Stoakes  *
173049b1b8d6SLorenzo Stoakes  * NOTE! This runs with mmap_lock held for reading, so it is possible that
173149b1b8d6SLorenzo Stoakes  * the anon_vma of 'old' is concurrently in the process of being set up
173249b1b8d6SLorenzo Stoakes  * by another page fault trying to merge _that_. But that's ok: if it
173349b1b8d6SLorenzo Stoakes  * is being set up, that automatically means that it will be a singleton
173449b1b8d6SLorenzo Stoakes  * acceptable for merging, so we can do all of this optimistically. But
173549b1b8d6SLorenzo Stoakes  * we do that READ_ONCE() to make sure that we never re-load the pointer.
173649b1b8d6SLorenzo Stoakes  *
173749b1b8d6SLorenzo Stoakes  * IOW: that the "list_is_singular()" test on the anon_vma_chain only
173849b1b8d6SLorenzo Stoakes  * matters for the 'stable anon_vma' case (ie the thing we want to avoid
173949b1b8d6SLorenzo Stoakes  * is to return an anon_vma that is "complex" due to having gone through
174049b1b8d6SLorenzo Stoakes  * a fork).
174149b1b8d6SLorenzo Stoakes  *
174249b1b8d6SLorenzo Stoakes  * We also make sure that the two vma's are compatible (adjacent,
174349b1b8d6SLorenzo Stoakes  * and with the same memory policies). That's all stable, even with just
174449b1b8d6SLorenzo Stoakes  * a read lock on the mmap_lock.
174549b1b8d6SLorenzo Stoakes  */
174649b1b8d6SLorenzo Stoakes static struct anon_vma *reusable_anon_vma(struct vm_area_struct *old,
174749b1b8d6SLorenzo Stoakes 					  struct vm_area_struct *a,
174849b1b8d6SLorenzo Stoakes 					  struct vm_area_struct *b)
174949b1b8d6SLorenzo Stoakes {
175049b1b8d6SLorenzo Stoakes 	if (anon_vma_compatible(a, b)) {
175149b1b8d6SLorenzo Stoakes 		struct anon_vma *anon_vma = READ_ONCE(old->anon_vma);
175249b1b8d6SLorenzo Stoakes 
175349b1b8d6SLorenzo Stoakes 		if (anon_vma && list_is_singular(&old->anon_vma_chain))
175449b1b8d6SLorenzo Stoakes 			return anon_vma;
175549b1b8d6SLorenzo Stoakes 	}
175649b1b8d6SLorenzo Stoakes 	return NULL;
175749b1b8d6SLorenzo Stoakes }
175849b1b8d6SLorenzo Stoakes 
175949b1b8d6SLorenzo Stoakes /*
176049b1b8d6SLorenzo Stoakes  * find_mergeable_anon_vma is used by anon_vma_prepare, to check
176149b1b8d6SLorenzo Stoakes  * neighbouring vmas for a suitable anon_vma, before it goes off
176249b1b8d6SLorenzo Stoakes  * to allocate a new anon_vma.  It checks because a repetitive
176349b1b8d6SLorenzo Stoakes  * sequence of mprotects and faults may otherwise lead to distinct
176449b1b8d6SLorenzo Stoakes  * anon_vmas being allocated, preventing vma merge in subsequent
176549b1b8d6SLorenzo Stoakes  * mprotect.
176649b1b8d6SLorenzo Stoakes  */
176749b1b8d6SLorenzo Stoakes struct anon_vma *find_mergeable_anon_vma(struct vm_area_struct *vma)
176849b1b8d6SLorenzo Stoakes {
176949b1b8d6SLorenzo Stoakes 	struct anon_vma *anon_vma = NULL;
177049b1b8d6SLorenzo Stoakes 	struct vm_area_struct *prev, *next;
177149b1b8d6SLorenzo Stoakes 	VMA_ITERATOR(vmi, vma->vm_mm, vma->vm_end);
177249b1b8d6SLorenzo Stoakes 
177349b1b8d6SLorenzo Stoakes 	/* Try next first. */
177449b1b8d6SLorenzo Stoakes 	next = vma_iter_load(&vmi);
177549b1b8d6SLorenzo Stoakes 	if (next) {
177649b1b8d6SLorenzo Stoakes 		anon_vma = reusable_anon_vma(next, vma, next);
177749b1b8d6SLorenzo Stoakes 		if (anon_vma)
177849b1b8d6SLorenzo Stoakes 			return anon_vma;
177949b1b8d6SLorenzo Stoakes 	}
178049b1b8d6SLorenzo Stoakes 
178149b1b8d6SLorenzo Stoakes 	prev = vma_prev(&vmi);
178249b1b8d6SLorenzo Stoakes 	VM_BUG_ON_VMA(prev != vma, vma);
178349b1b8d6SLorenzo Stoakes 	prev = vma_prev(&vmi);
178449b1b8d6SLorenzo Stoakes 	/* Try prev next. */
178549b1b8d6SLorenzo Stoakes 	if (prev)
178649b1b8d6SLorenzo Stoakes 		anon_vma = reusable_anon_vma(prev, prev, vma);
178749b1b8d6SLorenzo Stoakes 
178849b1b8d6SLorenzo Stoakes 	/*
178949b1b8d6SLorenzo Stoakes 	 * We might reach here with anon_vma == NULL if we can't find
179049b1b8d6SLorenzo Stoakes 	 * any reusable anon_vma.
179149b1b8d6SLorenzo Stoakes 	 * There's no absolute need to look only at touching neighbours:
179249b1b8d6SLorenzo Stoakes 	 * we could search further afield for "compatible" anon_vmas.
179349b1b8d6SLorenzo Stoakes 	 * But it would probably just be a waste of time searching,
179449b1b8d6SLorenzo Stoakes 	 * or lead to too many vmas hanging off the same anon_vma.
179549b1b8d6SLorenzo Stoakes 	 * We're trying to allow mprotect remerging later on,
179649b1b8d6SLorenzo Stoakes 	 * not trying to minimize memory used for anon_vmas.
179749b1b8d6SLorenzo Stoakes 	 */
179849b1b8d6SLorenzo Stoakes 	return anon_vma;
179949b1b8d6SLorenzo Stoakes }
180049b1b8d6SLorenzo Stoakes 
180149b1b8d6SLorenzo Stoakes static bool vm_ops_needs_writenotify(const struct vm_operations_struct *vm_ops)
180249b1b8d6SLorenzo Stoakes {
180349b1b8d6SLorenzo Stoakes 	return vm_ops && (vm_ops->page_mkwrite || vm_ops->pfn_mkwrite);
180449b1b8d6SLorenzo Stoakes }
180549b1b8d6SLorenzo Stoakes 
180649b1b8d6SLorenzo Stoakes static bool vma_is_shared_writable(struct vm_area_struct *vma)
180749b1b8d6SLorenzo Stoakes {
180849b1b8d6SLorenzo Stoakes 	return (vma->vm_flags & (VM_WRITE | VM_SHARED)) ==
180949b1b8d6SLorenzo Stoakes 		(VM_WRITE | VM_SHARED);
181049b1b8d6SLorenzo Stoakes }
181149b1b8d6SLorenzo Stoakes 
181249b1b8d6SLorenzo Stoakes static bool vma_fs_can_writeback(struct vm_area_struct *vma)
181349b1b8d6SLorenzo Stoakes {
181449b1b8d6SLorenzo Stoakes 	/* No managed pages to writeback. */
181549b1b8d6SLorenzo Stoakes 	if (vma->vm_flags & VM_PFNMAP)
181649b1b8d6SLorenzo Stoakes 		return false;
181749b1b8d6SLorenzo Stoakes 
181849b1b8d6SLorenzo Stoakes 	return vma->vm_file && vma->vm_file->f_mapping &&
181949b1b8d6SLorenzo Stoakes 		mapping_can_writeback(vma->vm_file->f_mapping);
182049b1b8d6SLorenzo Stoakes }
182149b1b8d6SLorenzo Stoakes 
182249b1b8d6SLorenzo Stoakes /*
182349b1b8d6SLorenzo Stoakes  * Does this VMA require the underlying folios to have their dirty state
182449b1b8d6SLorenzo Stoakes  * tracked?
182549b1b8d6SLorenzo Stoakes  */
182649b1b8d6SLorenzo Stoakes bool vma_needs_dirty_tracking(struct vm_area_struct *vma)
182749b1b8d6SLorenzo Stoakes {
182849b1b8d6SLorenzo Stoakes 	/* Only shared, writable VMAs require dirty tracking. */
182949b1b8d6SLorenzo Stoakes 	if (!vma_is_shared_writable(vma))
183049b1b8d6SLorenzo Stoakes 		return false;
183149b1b8d6SLorenzo Stoakes 
183249b1b8d6SLorenzo Stoakes 	/* Does the filesystem need to be notified? */
183349b1b8d6SLorenzo Stoakes 	if (vm_ops_needs_writenotify(vma->vm_ops))
183449b1b8d6SLorenzo Stoakes 		return true;
183549b1b8d6SLorenzo Stoakes 
183649b1b8d6SLorenzo Stoakes 	/*
183749b1b8d6SLorenzo Stoakes 	 * Even if the filesystem doesn't indicate a need for writenotify, if it
183849b1b8d6SLorenzo Stoakes 	 * can writeback, dirty tracking is still required.
183949b1b8d6SLorenzo Stoakes 	 */
184049b1b8d6SLorenzo Stoakes 	return vma_fs_can_writeback(vma);
184149b1b8d6SLorenzo Stoakes }
184249b1b8d6SLorenzo Stoakes 
184349b1b8d6SLorenzo Stoakes /*
184449b1b8d6SLorenzo Stoakes  * Some shared mappings will want the pages marked read-only
184549b1b8d6SLorenzo Stoakes  * to track write events. If so, we'll downgrade vm_page_prot
184649b1b8d6SLorenzo Stoakes  * to the private version (using protection_map[] without the
184749b1b8d6SLorenzo Stoakes  * VM_SHARED bit).
184849b1b8d6SLorenzo Stoakes  */
184949b1b8d6SLorenzo Stoakes bool vma_wants_writenotify(struct vm_area_struct *vma, pgprot_t vm_page_prot)
185049b1b8d6SLorenzo Stoakes {
185149b1b8d6SLorenzo Stoakes 	/* If it was private or non-writable, the write bit is already clear */
185249b1b8d6SLorenzo Stoakes 	if (!vma_is_shared_writable(vma))
185349b1b8d6SLorenzo Stoakes 		return false;
185449b1b8d6SLorenzo Stoakes 
185549b1b8d6SLorenzo Stoakes 	/* The backer wishes to know when pages are first written to? */
185649b1b8d6SLorenzo Stoakes 	if (vm_ops_needs_writenotify(vma->vm_ops))
185749b1b8d6SLorenzo Stoakes 		return true;
185849b1b8d6SLorenzo Stoakes 
185949b1b8d6SLorenzo Stoakes 	/* The open routine did something to the protections that pgprot_modify
186049b1b8d6SLorenzo Stoakes 	 * won't preserve? */
186149b1b8d6SLorenzo Stoakes 	if (pgprot_val(vm_page_prot) !=
186249b1b8d6SLorenzo Stoakes 	    pgprot_val(vm_pgprot_modify(vm_page_prot, vma->vm_flags)))
186349b1b8d6SLorenzo Stoakes 		return false;
186449b1b8d6SLorenzo Stoakes 
186549b1b8d6SLorenzo Stoakes 	/*
186649b1b8d6SLorenzo Stoakes 	 * Do we need to track softdirty? hugetlb does not support softdirty
186749b1b8d6SLorenzo Stoakes 	 * tracking yet.
186849b1b8d6SLorenzo Stoakes 	 */
186949b1b8d6SLorenzo Stoakes 	if (vma_soft_dirty_enabled(vma) && !is_vm_hugetlb_page(vma))
187049b1b8d6SLorenzo Stoakes 		return true;
187149b1b8d6SLorenzo Stoakes 
187249b1b8d6SLorenzo Stoakes 	/* Do we need write faults for uffd-wp tracking? */
187349b1b8d6SLorenzo Stoakes 	if (userfaultfd_wp(vma))
187449b1b8d6SLorenzo Stoakes 		return true;
187549b1b8d6SLorenzo Stoakes 
187649b1b8d6SLorenzo Stoakes 	/* Can the mapping track the dirty pages? */
187749b1b8d6SLorenzo Stoakes 	return vma_fs_can_writeback(vma);
187849b1b8d6SLorenzo Stoakes }
187949b1b8d6SLorenzo Stoakes 
188049b1b8d6SLorenzo Stoakes static DEFINE_MUTEX(mm_all_locks_mutex);
188149b1b8d6SLorenzo Stoakes 
188249b1b8d6SLorenzo Stoakes static void vm_lock_anon_vma(struct mm_struct *mm, struct anon_vma *anon_vma)
188349b1b8d6SLorenzo Stoakes {
188449b1b8d6SLorenzo Stoakes 	if (!test_bit(0, (unsigned long *) &anon_vma->root->rb_root.rb_root.rb_node)) {
188549b1b8d6SLorenzo Stoakes 		/*
188649b1b8d6SLorenzo Stoakes 		 * The LSB of head.next can't change from under us
188749b1b8d6SLorenzo Stoakes 		 * because we hold the mm_all_locks_mutex.
188849b1b8d6SLorenzo Stoakes 		 */
188949b1b8d6SLorenzo Stoakes 		down_write_nest_lock(&anon_vma->root->rwsem, &mm->mmap_lock);
189049b1b8d6SLorenzo Stoakes 		/*
189149b1b8d6SLorenzo Stoakes 		 * We can safely modify head.next after taking the
189249b1b8d6SLorenzo Stoakes 		 * anon_vma->root->rwsem. If some other vma in this mm shares
189349b1b8d6SLorenzo Stoakes 		 * the same anon_vma we won't take it again.
189449b1b8d6SLorenzo Stoakes 		 *
189549b1b8d6SLorenzo Stoakes 		 * No need of atomic instructions here, head.next
189649b1b8d6SLorenzo Stoakes 		 * can't change from under us thanks to the
189749b1b8d6SLorenzo Stoakes 		 * anon_vma->root->rwsem.
189849b1b8d6SLorenzo Stoakes 		 */
189949b1b8d6SLorenzo Stoakes 		if (__test_and_set_bit(0, (unsigned long *)
190049b1b8d6SLorenzo Stoakes 				       &anon_vma->root->rb_root.rb_root.rb_node))
190149b1b8d6SLorenzo Stoakes 			BUG();
190249b1b8d6SLorenzo Stoakes 	}
190349b1b8d6SLorenzo Stoakes }
190449b1b8d6SLorenzo Stoakes 
190549b1b8d6SLorenzo Stoakes static void vm_lock_mapping(struct mm_struct *mm, struct address_space *mapping)
190649b1b8d6SLorenzo Stoakes {
190749b1b8d6SLorenzo Stoakes 	if (!test_bit(AS_MM_ALL_LOCKS, &mapping->flags)) {
190849b1b8d6SLorenzo Stoakes 		/*
190949b1b8d6SLorenzo Stoakes 		 * AS_MM_ALL_LOCKS can't change from under us because
191049b1b8d6SLorenzo Stoakes 		 * we hold the mm_all_locks_mutex.
191149b1b8d6SLorenzo Stoakes 		 *
191249b1b8d6SLorenzo Stoakes 		 * Operations on ->flags have to be atomic because
191349b1b8d6SLorenzo Stoakes 		 * even if AS_MM_ALL_LOCKS is stable thanks to the
191449b1b8d6SLorenzo Stoakes 		 * mm_all_locks_mutex, there may be other cpus
191549b1b8d6SLorenzo Stoakes 		 * changing other bitflags in parallel to us.
191649b1b8d6SLorenzo Stoakes 		 */
191749b1b8d6SLorenzo Stoakes 		if (test_and_set_bit(AS_MM_ALL_LOCKS, &mapping->flags))
191849b1b8d6SLorenzo Stoakes 			BUG();
191949b1b8d6SLorenzo Stoakes 		down_write_nest_lock(&mapping->i_mmap_rwsem, &mm->mmap_lock);
192049b1b8d6SLorenzo Stoakes 	}
192149b1b8d6SLorenzo Stoakes }
192249b1b8d6SLorenzo Stoakes 
192349b1b8d6SLorenzo Stoakes /*
192449b1b8d6SLorenzo Stoakes  * This operation locks against the VM for all pte/vma/mm related
192549b1b8d6SLorenzo Stoakes  * operations that could ever happen on a certain mm. This includes
192649b1b8d6SLorenzo Stoakes  * vmtruncate, try_to_unmap, and all page faults.
192749b1b8d6SLorenzo Stoakes  *
192849b1b8d6SLorenzo Stoakes  * The caller must take the mmap_lock in write mode before calling
192949b1b8d6SLorenzo Stoakes  * mm_take_all_locks(). The caller isn't allowed to release the
193049b1b8d6SLorenzo Stoakes  * mmap_lock until mm_drop_all_locks() returns.
193149b1b8d6SLorenzo Stoakes  *
193249b1b8d6SLorenzo Stoakes  * mmap_lock in write mode is required in order to block all operations
193349b1b8d6SLorenzo Stoakes  * that could modify pagetables and free pages without need of
193449b1b8d6SLorenzo Stoakes  * altering the vma layout. It's also needed in write mode to avoid new
193549b1b8d6SLorenzo Stoakes  * anon_vmas to be associated with existing vmas.
193649b1b8d6SLorenzo Stoakes  *
193749b1b8d6SLorenzo Stoakes  * A single task can't take more than one mm_take_all_locks() in a row
193849b1b8d6SLorenzo Stoakes  * or it would deadlock.
193949b1b8d6SLorenzo Stoakes  *
194049b1b8d6SLorenzo Stoakes  * The LSB in anon_vma->rb_root.rb_node and the AS_MM_ALL_LOCKS bitflag in
194149b1b8d6SLorenzo Stoakes  * mapping->flags avoid to take the same lock twice, if more than one
194249b1b8d6SLorenzo Stoakes  * vma in this mm is backed by the same anon_vma or address_space.
194349b1b8d6SLorenzo Stoakes  *
194449b1b8d6SLorenzo Stoakes  * We take locks in following order, accordingly to comment at beginning
194549b1b8d6SLorenzo Stoakes  * of mm/rmap.c:
194649b1b8d6SLorenzo Stoakes  *   - all hugetlbfs_i_mmap_rwsem_key locks (aka mapping->i_mmap_rwsem for
194749b1b8d6SLorenzo Stoakes  *     hugetlb mapping);
194849b1b8d6SLorenzo Stoakes  *   - all vmas marked locked
194949b1b8d6SLorenzo Stoakes  *   - all i_mmap_rwsem locks;
195049b1b8d6SLorenzo Stoakes  *   - all anon_vma->rwseml
195149b1b8d6SLorenzo Stoakes  *
195249b1b8d6SLorenzo Stoakes  * We can take all locks within these types randomly because the VM code
195349b1b8d6SLorenzo Stoakes  * doesn't nest them and we protected from parallel mm_take_all_locks() by
195449b1b8d6SLorenzo Stoakes  * mm_all_locks_mutex.
195549b1b8d6SLorenzo Stoakes  *
195649b1b8d6SLorenzo Stoakes  * mm_take_all_locks() and mm_drop_all_locks are expensive operations
195749b1b8d6SLorenzo Stoakes  * that may have to take thousand of locks.
195849b1b8d6SLorenzo Stoakes  *
195949b1b8d6SLorenzo Stoakes  * mm_take_all_locks() can fail if it's interrupted by signals.
196049b1b8d6SLorenzo Stoakes  */
196149b1b8d6SLorenzo Stoakes int mm_take_all_locks(struct mm_struct *mm)
196249b1b8d6SLorenzo Stoakes {
196349b1b8d6SLorenzo Stoakes 	struct vm_area_struct *vma;
196449b1b8d6SLorenzo Stoakes 	struct anon_vma_chain *avc;
196549b1b8d6SLorenzo Stoakes 	VMA_ITERATOR(vmi, mm, 0);
196649b1b8d6SLorenzo Stoakes 
196749b1b8d6SLorenzo Stoakes 	mmap_assert_write_locked(mm);
196849b1b8d6SLorenzo Stoakes 
196949b1b8d6SLorenzo Stoakes 	mutex_lock(&mm_all_locks_mutex);
197049b1b8d6SLorenzo Stoakes 
197149b1b8d6SLorenzo Stoakes 	/*
197249b1b8d6SLorenzo Stoakes 	 * vma_start_write() does not have a complement in mm_drop_all_locks()
197349b1b8d6SLorenzo Stoakes 	 * because vma_start_write() is always asymmetrical; it marks a VMA as
197449b1b8d6SLorenzo Stoakes 	 * being written to until mmap_write_unlock() or mmap_write_downgrade()
197549b1b8d6SLorenzo Stoakes 	 * is reached.
197649b1b8d6SLorenzo Stoakes 	 */
197749b1b8d6SLorenzo Stoakes 	for_each_vma(vmi, vma) {
197849b1b8d6SLorenzo Stoakes 		if (signal_pending(current))
197949b1b8d6SLorenzo Stoakes 			goto out_unlock;
198049b1b8d6SLorenzo Stoakes 		vma_start_write(vma);
198149b1b8d6SLorenzo Stoakes 	}
198249b1b8d6SLorenzo Stoakes 
198349b1b8d6SLorenzo Stoakes 	vma_iter_init(&vmi, mm, 0);
198449b1b8d6SLorenzo Stoakes 	for_each_vma(vmi, vma) {
198549b1b8d6SLorenzo Stoakes 		if (signal_pending(current))
198649b1b8d6SLorenzo Stoakes 			goto out_unlock;
198749b1b8d6SLorenzo Stoakes 		if (vma->vm_file && vma->vm_file->f_mapping &&
198849b1b8d6SLorenzo Stoakes 				is_vm_hugetlb_page(vma))
198949b1b8d6SLorenzo Stoakes 			vm_lock_mapping(mm, vma->vm_file->f_mapping);
199049b1b8d6SLorenzo Stoakes 	}
199149b1b8d6SLorenzo Stoakes 
199249b1b8d6SLorenzo Stoakes 	vma_iter_init(&vmi, mm, 0);
199349b1b8d6SLorenzo Stoakes 	for_each_vma(vmi, vma) {
199449b1b8d6SLorenzo Stoakes 		if (signal_pending(current))
199549b1b8d6SLorenzo Stoakes 			goto out_unlock;
199649b1b8d6SLorenzo Stoakes 		if (vma->vm_file && vma->vm_file->f_mapping &&
199749b1b8d6SLorenzo Stoakes 				!is_vm_hugetlb_page(vma))
199849b1b8d6SLorenzo Stoakes 			vm_lock_mapping(mm, vma->vm_file->f_mapping);
199949b1b8d6SLorenzo Stoakes 	}
200049b1b8d6SLorenzo Stoakes 
200149b1b8d6SLorenzo Stoakes 	vma_iter_init(&vmi, mm, 0);
200249b1b8d6SLorenzo Stoakes 	for_each_vma(vmi, vma) {
200349b1b8d6SLorenzo Stoakes 		if (signal_pending(current))
200449b1b8d6SLorenzo Stoakes 			goto out_unlock;
200549b1b8d6SLorenzo Stoakes 		if (vma->anon_vma)
200649b1b8d6SLorenzo Stoakes 			list_for_each_entry(avc, &vma->anon_vma_chain, same_vma)
200749b1b8d6SLorenzo Stoakes 				vm_lock_anon_vma(mm, avc->anon_vma);
200849b1b8d6SLorenzo Stoakes 	}
200949b1b8d6SLorenzo Stoakes 
201049b1b8d6SLorenzo Stoakes 	return 0;
201149b1b8d6SLorenzo Stoakes 
201249b1b8d6SLorenzo Stoakes out_unlock:
201349b1b8d6SLorenzo Stoakes 	mm_drop_all_locks(mm);
201449b1b8d6SLorenzo Stoakes 	return -EINTR;
201549b1b8d6SLorenzo Stoakes }
201649b1b8d6SLorenzo Stoakes 
201749b1b8d6SLorenzo Stoakes static void vm_unlock_anon_vma(struct anon_vma *anon_vma)
201849b1b8d6SLorenzo Stoakes {
201949b1b8d6SLorenzo Stoakes 	if (test_bit(0, (unsigned long *) &anon_vma->root->rb_root.rb_root.rb_node)) {
202049b1b8d6SLorenzo Stoakes 		/*
202149b1b8d6SLorenzo Stoakes 		 * The LSB of head.next can't change to 0 from under
202249b1b8d6SLorenzo Stoakes 		 * us because we hold the mm_all_locks_mutex.
202349b1b8d6SLorenzo Stoakes 		 *
202449b1b8d6SLorenzo Stoakes 		 * We must however clear the bitflag before unlocking
202549b1b8d6SLorenzo Stoakes 		 * the vma so the users using the anon_vma->rb_root will
202649b1b8d6SLorenzo Stoakes 		 * never see our bitflag.
202749b1b8d6SLorenzo Stoakes 		 *
202849b1b8d6SLorenzo Stoakes 		 * No need of atomic instructions here, head.next
202949b1b8d6SLorenzo Stoakes 		 * can't change from under us until we release the
203049b1b8d6SLorenzo Stoakes 		 * anon_vma->root->rwsem.
203149b1b8d6SLorenzo Stoakes 		 */
203249b1b8d6SLorenzo Stoakes 		if (!__test_and_clear_bit(0, (unsigned long *)
203349b1b8d6SLorenzo Stoakes 					  &anon_vma->root->rb_root.rb_root.rb_node))
203449b1b8d6SLorenzo Stoakes 			BUG();
203549b1b8d6SLorenzo Stoakes 		anon_vma_unlock_write(anon_vma);
203649b1b8d6SLorenzo Stoakes 	}
203749b1b8d6SLorenzo Stoakes }
203849b1b8d6SLorenzo Stoakes 
203949b1b8d6SLorenzo Stoakes static void vm_unlock_mapping(struct address_space *mapping)
204049b1b8d6SLorenzo Stoakes {
204149b1b8d6SLorenzo Stoakes 	if (test_bit(AS_MM_ALL_LOCKS, &mapping->flags)) {
204249b1b8d6SLorenzo Stoakes 		/*
204349b1b8d6SLorenzo Stoakes 		 * AS_MM_ALL_LOCKS can't change to 0 from under us
204449b1b8d6SLorenzo Stoakes 		 * because we hold the mm_all_locks_mutex.
204549b1b8d6SLorenzo Stoakes 		 */
204649b1b8d6SLorenzo Stoakes 		i_mmap_unlock_write(mapping);
204749b1b8d6SLorenzo Stoakes 		if (!test_and_clear_bit(AS_MM_ALL_LOCKS,
204849b1b8d6SLorenzo Stoakes 					&mapping->flags))
204949b1b8d6SLorenzo Stoakes 			BUG();
205049b1b8d6SLorenzo Stoakes 	}
205149b1b8d6SLorenzo Stoakes }
205249b1b8d6SLorenzo Stoakes 
205349b1b8d6SLorenzo Stoakes /*
205449b1b8d6SLorenzo Stoakes  * The mmap_lock cannot be released by the caller until
205549b1b8d6SLorenzo Stoakes  * mm_drop_all_locks() returns.
205649b1b8d6SLorenzo Stoakes  */
205749b1b8d6SLorenzo Stoakes void mm_drop_all_locks(struct mm_struct *mm)
205849b1b8d6SLorenzo Stoakes {
205949b1b8d6SLorenzo Stoakes 	struct vm_area_struct *vma;
206049b1b8d6SLorenzo Stoakes 	struct anon_vma_chain *avc;
206149b1b8d6SLorenzo Stoakes 	VMA_ITERATOR(vmi, mm, 0);
206249b1b8d6SLorenzo Stoakes 
206349b1b8d6SLorenzo Stoakes 	mmap_assert_write_locked(mm);
206449b1b8d6SLorenzo Stoakes 	BUG_ON(!mutex_is_locked(&mm_all_locks_mutex));
206549b1b8d6SLorenzo Stoakes 
206649b1b8d6SLorenzo Stoakes 	for_each_vma(vmi, vma) {
206749b1b8d6SLorenzo Stoakes 		if (vma->anon_vma)
206849b1b8d6SLorenzo Stoakes 			list_for_each_entry(avc, &vma->anon_vma_chain, same_vma)
206949b1b8d6SLorenzo Stoakes 				vm_unlock_anon_vma(avc->anon_vma);
207049b1b8d6SLorenzo Stoakes 		if (vma->vm_file && vma->vm_file->f_mapping)
207149b1b8d6SLorenzo Stoakes 			vm_unlock_mapping(vma->vm_file->f_mapping);
207249b1b8d6SLorenzo Stoakes 	}
207349b1b8d6SLorenzo Stoakes 
207449b1b8d6SLorenzo Stoakes 	mutex_unlock(&mm_all_locks_mutex);
207549b1b8d6SLorenzo Stoakes }
2076