xref: /linux/mm/vma.c (revision 2f1c6611b0a89afcb8641471af5f223c9caa01e0)
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 
10*2f1c6611SLorenzo Stoakes static inline bool is_mergeable_vma(struct vma_merge_struct *vmg, bool merge_next)
1149b1b8d6SLorenzo Stoakes {
12*2f1c6611SLorenzo Stoakes 	struct vm_area_struct *vma = merge_next ? vmg->next : vmg->prev;
13*2f1c6611SLorenzo Stoakes 	/*
14*2f1c6611SLorenzo Stoakes 	 * If the vma has a ->close operation then the driver probably needs to
15*2f1c6611SLorenzo Stoakes 	 * release per-vma resources, so we don't attempt to merge those if the
16*2f1c6611SLorenzo Stoakes 	 * caller indicates the current vma may be removed as part of the merge,
17*2f1c6611SLorenzo Stoakes 	 * which is the case if we are attempting to merge the next VMA into
18*2f1c6611SLorenzo Stoakes 	 * this one.
19*2f1c6611SLorenzo Stoakes 	 */
20*2f1c6611SLorenzo Stoakes 	bool may_remove_vma = merge_next;
21*2f1c6611SLorenzo Stoakes 
2249b1b8d6SLorenzo Stoakes 	/*
2349b1b8d6SLorenzo Stoakes 	 * VM_SOFTDIRTY should not prevent from VMA merging, if we
2449b1b8d6SLorenzo Stoakes 	 * match the flags but dirty bit -- the caller should mark
2549b1b8d6SLorenzo Stoakes 	 * merged VMA as dirty. If dirty bit won't be excluded from
2649b1b8d6SLorenzo Stoakes 	 * comparison, we increase pressure on the memory system forcing
2749b1b8d6SLorenzo Stoakes 	 * the kernel to generate new VMAs when old one could be
2849b1b8d6SLorenzo Stoakes 	 * extended instead.
2949b1b8d6SLorenzo Stoakes 	 */
30*2f1c6611SLorenzo Stoakes 	if ((vma->vm_flags ^ vmg->flags) & ~VM_SOFTDIRTY)
3149b1b8d6SLorenzo Stoakes 		return false;
32*2f1c6611SLorenzo Stoakes 	if (vma->vm_file != vmg->file)
3349b1b8d6SLorenzo Stoakes 		return false;
3449b1b8d6SLorenzo Stoakes 	if (may_remove_vma && vma->vm_ops && vma->vm_ops->close)
3549b1b8d6SLorenzo Stoakes 		return false;
36*2f1c6611SLorenzo Stoakes 	if (!is_mergeable_vm_userfaultfd_ctx(vma, vmg->uffd_ctx))
3749b1b8d6SLorenzo Stoakes 		return false;
38*2f1c6611SLorenzo Stoakes 	if (!anon_vma_name_eq(anon_vma_name(vma), vmg->anon_name))
3949b1b8d6SLorenzo Stoakes 		return false;
4049b1b8d6SLorenzo Stoakes 	return true;
4149b1b8d6SLorenzo Stoakes }
4249b1b8d6SLorenzo Stoakes 
4349b1b8d6SLorenzo Stoakes static inline bool is_mergeable_anon_vma(struct anon_vma *anon_vma1,
4449b1b8d6SLorenzo Stoakes 		 struct anon_vma *anon_vma2, struct vm_area_struct *vma)
4549b1b8d6SLorenzo Stoakes {
4649b1b8d6SLorenzo Stoakes 	/*
4749b1b8d6SLorenzo Stoakes 	 * The list_is_singular() test is to avoid merging VMA cloned from
4849b1b8d6SLorenzo Stoakes 	 * parents. This can improve scalability caused by anon_vma lock.
4949b1b8d6SLorenzo Stoakes 	 */
5049b1b8d6SLorenzo Stoakes 	if ((!anon_vma1 || !anon_vma2) && (!vma ||
5149b1b8d6SLorenzo Stoakes 		list_is_singular(&vma->anon_vma_chain)))
5249b1b8d6SLorenzo Stoakes 		return true;
5349b1b8d6SLorenzo Stoakes 	return anon_vma1 == anon_vma2;
5449b1b8d6SLorenzo Stoakes }
5549b1b8d6SLorenzo Stoakes 
5649b1b8d6SLorenzo Stoakes /*
5749b1b8d6SLorenzo Stoakes  * init_multi_vma_prep() - Initializer for struct vma_prepare
5849b1b8d6SLorenzo Stoakes  * @vp: The vma_prepare struct
5949b1b8d6SLorenzo Stoakes  * @vma: The vma that will be altered once locked
6049b1b8d6SLorenzo Stoakes  * @next: The next vma if it is to be adjusted
6149b1b8d6SLorenzo Stoakes  * @remove: The first vma to be removed
6249b1b8d6SLorenzo Stoakes  * @remove2: The second vma to be removed
6349b1b8d6SLorenzo Stoakes  */
6449b1b8d6SLorenzo Stoakes static void init_multi_vma_prep(struct vma_prepare *vp,
6549b1b8d6SLorenzo Stoakes 				struct vm_area_struct *vma,
6649b1b8d6SLorenzo Stoakes 				struct vm_area_struct *next,
6749b1b8d6SLorenzo Stoakes 				struct vm_area_struct *remove,
6849b1b8d6SLorenzo Stoakes 				struct vm_area_struct *remove2)
6949b1b8d6SLorenzo Stoakes {
7049b1b8d6SLorenzo Stoakes 	memset(vp, 0, sizeof(struct vma_prepare));
7149b1b8d6SLorenzo Stoakes 	vp->vma = vma;
7249b1b8d6SLorenzo Stoakes 	vp->anon_vma = vma->anon_vma;
7349b1b8d6SLorenzo Stoakes 	vp->remove = remove;
7449b1b8d6SLorenzo Stoakes 	vp->remove2 = remove2;
7549b1b8d6SLorenzo Stoakes 	vp->adj_next = next;
7649b1b8d6SLorenzo Stoakes 	if (!vp->anon_vma && next)
7749b1b8d6SLorenzo Stoakes 		vp->anon_vma = next->anon_vma;
7849b1b8d6SLorenzo Stoakes 
7949b1b8d6SLorenzo Stoakes 	vp->file = vma->vm_file;
8049b1b8d6SLorenzo Stoakes 	if (vp->file)
8149b1b8d6SLorenzo Stoakes 		vp->mapping = vma->vm_file->f_mapping;
8249b1b8d6SLorenzo Stoakes 
8349b1b8d6SLorenzo Stoakes }
8449b1b8d6SLorenzo Stoakes 
8549b1b8d6SLorenzo Stoakes /*
8649b1b8d6SLorenzo Stoakes  * Return true if we can merge this (vm_flags,anon_vma,file,vm_pgoff)
8749b1b8d6SLorenzo Stoakes  * in front of (at a lower virtual address and file offset than) the vma.
8849b1b8d6SLorenzo Stoakes  *
8949b1b8d6SLorenzo Stoakes  * We cannot merge two vmas if they have differently assigned (non-NULL)
9049b1b8d6SLorenzo Stoakes  * anon_vmas, nor if same anon_vma is assigned but offsets incompatible.
9149b1b8d6SLorenzo Stoakes  *
9249b1b8d6SLorenzo Stoakes  * We don't check here for the merged mmap wrapping around the end of pagecache
9349b1b8d6SLorenzo Stoakes  * indices (16TB on ia32) because do_mmap() does not permit mmap's which
9449b1b8d6SLorenzo Stoakes  * wrap, nor mmaps which cover the final page at index -1UL.
9549b1b8d6SLorenzo Stoakes  *
9649b1b8d6SLorenzo Stoakes  * We assume the vma may be removed as part of the merge.
9749b1b8d6SLorenzo Stoakes  */
9849b1b8d6SLorenzo Stoakes bool
99*2f1c6611SLorenzo Stoakes can_vma_merge_before(struct vma_merge_struct *vmg)
10049b1b8d6SLorenzo Stoakes {
101*2f1c6611SLorenzo Stoakes 	pgoff_t pglen = PHYS_PFN(vmg->end - vmg->start);
102*2f1c6611SLorenzo Stoakes 
103*2f1c6611SLorenzo Stoakes 	if (is_mergeable_vma(vmg, /* merge_next = */ true) &&
104*2f1c6611SLorenzo Stoakes 	    is_mergeable_anon_vma(vmg->anon_vma, vmg->next->anon_vma, vmg->next)) {
105*2f1c6611SLorenzo Stoakes 		if (vmg->next->vm_pgoff == vmg->pgoff + pglen)
10649b1b8d6SLorenzo Stoakes 			return true;
10749b1b8d6SLorenzo Stoakes 	}
108*2f1c6611SLorenzo Stoakes 
10949b1b8d6SLorenzo Stoakes 	return false;
11049b1b8d6SLorenzo Stoakes }
11149b1b8d6SLorenzo Stoakes 
11249b1b8d6SLorenzo Stoakes /*
11349b1b8d6SLorenzo Stoakes  * Return true if we can merge this (vm_flags,anon_vma,file,vm_pgoff)
11449b1b8d6SLorenzo Stoakes  * beyond (at a higher virtual address and file offset than) the vma.
11549b1b8d6SLorenzo Stoakes  *
11649b1b8d6SLorenzo Stoakes  * We cannot merge two vmas if they have differently assigned (non-NULL)
11749b1b8d6SLorenzo Stoakes  * anon_vmas, nor if same anon_vma is assigned but offsets incompatible.
11849b1b8d6SLorenzo Stoakes  *
11949b1b8d6SLorenzo Stoakes  * We assume that vma is not removed as part of the merge.
12049b1b8d6SLorenzo Stoakes  */
121*2f1c6611SLorenzo Stoakes bool can_vma_merge_after(struct vma_merge_struct *vmg)
12249b1b8d6SLorenzo Stoakes {
123*2f1c6611SLorenzo Stoakes 	if (is_mergeable_vma(vmg, /* merge_next = */ false) &&
124*2f1c6611SLorenzo Stoakes 	    is_mergeable_anon_vma(vmg->anon_vma, vmg->prev->anon_vma, vmg->prev)) {
125*2f1c6611SLorenzo Stoakes 		if (vmg->prev->vm_pgoff + vma_pages(vmg->prev) == vmg->pgoff)
12649b1b8d6SLorenzo Stoakes 			return true;
12749b1b8d6SLorenzo Stoakes 	}
12849b1b8d6SLorenzo Stoakes 	return false;
12949b1b8d6SLorenzo Stoakes }
13049b1b8d6SLorenzo Stoakes 
13149b1b8d6SLorenzo Stoakes /*
13249b1b8d6SLorenzo Stoakes  * Close a vm structure and free it.
13349b1b8d6SLorenzo Stoakes  */
134f8d112a4SLiam R. Howlett void remove_vma(struct vm_area_struct *vma, bool unreachable, bool closed)
13549b1b8d6SLorenzo Stoakes {
13649b1b8d6SLorenzo Stoakes 	might_sleep();
137f8d112a4SLiam R. Howlett 	if (!closed && vma->vm_ops && vma->vm_ops->close)
13849b1b8d6SLorenzo Stoakes 		vma->vm_ops->close(vma);
13949b1b8d6SLorenzo Stoakes 	if (vma->vm_file)
14049b1b8d6SLorenzo Stoakes 		fput(vma->vm_file);
14149b1b8d6SLorenzo Stoakes 	mpol_put(vma_policy(vma));
14249b1b8d6SLorenzo Stoakes 	if (unreachable)
14349b1b8d6SLorenzo Stoakes 		__vm_area_free(vma);
14449b1b8d6SLorenzo Stoakes 	else
14549b1b8d6SLorenzo Stoakes 		vm_area_free(vma);
14649b1b8d6SLorenzo Stoakes }
14749b1b8d6SLorenzo Stoakes 
14849b1b8d6SLorenzo Stoakes /*
14949b1b8d6SLorenzo Stoakes  * Get rid of page table information in the indicated region.
15049b1b8d6SLorenzo Stoakes  *
15149b1b8d6SLorenzo Stoakes  * Called with the mm semaphore held.
15249b1b8d6SLorenzo Stoakes  */
15394f59ea5SLiam R. Howlett void unmap_region(struct ma_state *mas, struct vm_area_struct *vma,
15494f59ea5SLiam R. Howlett 		struct vm_area_struct *prev, struct vm_area_struct *next)
15549b1b8d6SLorenzo Stoakes {
15694f59ea5SLiam R. Howlett 	struct mm_struct *mm = vma->vm_mm;
15749b1b8d6SLorenzo Stoakes 	struct mmu_gather tlb;
15849b1b8d6SLorenzo Stoakes 
15949b1b8d6SLorenzo Stoakes 	lru_add_drain();
16049b1b8d6SLorenzo Stoakes 	tlb_gather_mmu(&tlb, mm);
16149b1b8d6SLorenzo Stoakes 	update_hiwater_rss(mm);
16294f59ea5SLiam R. Howlett 	unmap_vmas(&tlb, mas, vma, vma->vm_start, vma->vm_end, vma->vm_end,
16394f59ea5SLiam R. Howlett 		   /* mm_wr_locked = */ true);
16494f59ea5SLiam R. Howlett 	mas_set(mas, vma->vm_end);
16549b1b8d6SLorenzo Stoakes 	free_pgtables(&tlb, mas, vma, prev ? prev->vm_end : FIRST_USER_ADDRESS,
16649b1b8d6SLorenzo Stoakes 		      next ? next->vm_start : USER_PGTABLES_CEILING,
16794f59ea5SLiam R. Howlett 		      /* mm_wr_locked = */ true);
16849b1b8d6SLorenzo Stoakes 	tlb_finish_mmu(&tlb);
16949b1b8d6SLorenzo Stoakes }
17049b1b8d6SLorenzo Stoakes 
17149b1b8d6SLorenzo Stoakes /*
17249b1b8d6SLorenzo Stoakes  * __split_vma() bypasses sysctl_max_map_count checking.  We use this where it
17349b1b8d6SLorenzo Stoakes  * has already been checked or doesn't make sense to fail.
174b7012d51SLiam R. Howlett  * VMA Iterator will point to the original VMA.
17549b1b8d6SLorenzo Stoakes  */
17649b1b8d6SLorenzo Stoakes static int __split_vma(struct vma_iterator *vmi, struct vm_area_struct *vma,
17749b1b8d6SLorenzo Stoakes 		       unsigned long addr, int new_below)
17849b1b8d6SLorenzo Stoakes {
17949b1b8d6SLorenzo Stoakes 	struct vma_prepare vp;
18049b1b8d6SLorenzo Stoakes 	struct vm_area_struct *new;
18149b1b8d6SLorenzo Stoakes 	int err;
18249b1b8d6SLorenzo Stoakes 
18349b1b8d6SLorenzo Stoakes 	WARN_ON(vma->vm_start >= addr);
18449b1b8d6SLorenzo Stoakes 	WARN_ON(vma->vm_end <= addr);
18549b1b8d6SLorenzo Stoakes 
18649b1b8d6SLorenzo Stoakes 	if (vma->vm_ops && vma->vm_ops->may_split) {
18749b1b8d6SLorenzo Stoakes 		err = vma->vm_ops->may_split(vma, addr);
18849b1b8d6SLorenzo Stoakes 		if (err)
18949b1b8d6SLorenzo Stoakes 			return err;
19049b1b8d6SLorenzo Stoakes 	}
19149b1b8d6SLorenzo Stoakes 
19249b1b8d6SLorenzo Stoakes 	new = vm_area_dup(vma);
19349b1b8d6SLorenzo Stoakes 	if (!new)
19449b1b8d6SLorenzo Stoakes 		return -ENOMEM;
19549b1b8d6SLorenzo Stoakes 
19649b1b8d6SLorenzo Stoakes 	if (new_below) {
19749b1b8d6SLorenzo Stoakes 		new->vm_end = addr;
19849b1b8d6SLorenzo Stoakes 	} else {
19949b1b8d6SLorenzo Stoakes 		new->vm_start = addr;
20049b1b8d6SLorenzo Stoakes 		new->vm_pgoff += ((addr - vma->vm_start) >> PAGE_SHIFT);
20149b1b8d6SLorenzo Stoakes 	}
20249b1b8d6SLorenzo Stoakes 
20349b1b8d6SLorenzo Stoakes 	err = -ENOMEM;
20449b1b8d6SLorenzo Stoakes 	vma_iter_config(vmi, new->vm_start, new->vm_end);
20549b1b8d6SLorenzo Stoakes 	if (vma_iter_prealloc(vmi, new))
20649b1b8d6SLorenzo Stoakes 		goto out_free_vma;
20749b1b8d6SLorenzo Stoakes 
20849b1b8d6SLorenzo Stoakes 	err = vma_dup_policy(vma, new);
20949b1b8d6SLorenzo Stoakes 	if (err)
21049b1b8d6SLorenzo Stoakes 		goto out_free_vmi;
21149b1b8d6SLorenzo Stoakes 
21249b1b8d6SLorenzo Stoakes 	err = anon_vma_clone(new, vma);
21349b1b8d6SLorenzo Stoakes 	if (err)
21449b1b8d6SLorenzo Stoakes 		goto out_free_mpol;
21549b1b8d6SLorenzo Stoakes 
21649b1b8d6SLorenzo Stoakes 	if (new->vm_file)
21749b1b8d6SLorenzo Stoakes 		get_file(new->vm_file);
21849b1b8d6SLorenzo Stoakes 
21949b1b8d6SLorenzo Stoakes 	if (new->vm_ops && new->vm_ops->open)
22049b1b8d6SLorenzo Stoakes 		new->vm_ops->open(new);
22149b1b8d6SLorenzo Stoakes 
22249b1b8d6SLorenzo Stoakes 	vma_start_write(vma);
22349b1b8d6SLorenzo Stoakes 	vma_start_write(new);
22449b1b8d6SLorenzo Stoakes 
22549b1b8d6SLorenzo Stoakes 	init_vma_prep(&vp, vma);
22649b1b8d6SLorenzo Stoakes 	vp.insert = new;
22749b1b8d6SLorenzo Stoakes 	vma_prepare(&vp);
22849b1b8d6SLorenzo Stoakes 	vma_adjust_trans_huge(vma, vma->vm_start, addr, 0);
22949b1b8d6SLorenzo Stoakes 
23049b1b8d6SLorenzo Stoakes 	if (new_below) {
23149b1b8d6SLorenzo Stoakes 		vma->vm_start = addr;
23249b1b8d6SLorenzo Stoakes 		vma->vm_pgoff += (addr - new->vm_start) >> PAGE_SHIFT;
23349b1b8d6SLorenzo Stoakes 	} else {
23449b1b8d6SLorenzo Stoakes 		vma->vm_end = addr;
23549b1b8d6SLorenzo Stoakes 	}
23649b1b8d6SLorenzo Stoakes 
23749b1b8d6SLorenzo Stoakes 	/* vma_complete stores the new vma */
23849b1b8d6SLorenzo Stoakes 	vma_complete(&vp, vmi, vma->vm_mm);
23989b2d2a5SLiam R. Howlett 	validate_mm(vma->vm_mm);
24049b1b8d6SLorenzo Stoakes 
24149b1b8d6SLorenzo Stoakes 	/* Success. */
24249b1b8d6SLorenzo Stoakes 	if (new_below)
24349b1b8d6SLorenzo Stoakes 		vma_next(vmi);
244b7012d51SLiam R. Howlett 	else
245b7012d51SLiam R. Howlett 		vma_prev(vmi);
246b7012d51SLiam R. Howlett 
24749b1b8d6SLorenzo Stoakes 	return 0;
24849b1b8d6SLorenzo Stoakes 
24949b1b8d6SLorenzo Stoakes out_free_mpol:
25049b1b8d6SLorenzo Stoakes 	mpol_put(vma_policy(new));
25149b1b8d6SLorenzo Stoakes out_free_vmi:
25249b1b8d6SLorenzo Stoakes 	vma_iter_free(vmi);
25349b1b8d6SLorenzo Stoakes out_free_vma:
25449b1b8d6SLorenzo Stoakes 	vm_area_free(new);
25549b1b8d6SLorenzo Stoakes 	return err;
25649b1b8d6SLorenzo Stoakes }
25749b1b8d6SLorenzo Stoakes 
25849b1b8d6SLorenzo Stoakes /*
25949b1b8d6SLorenzo Stoakes  * Split a vma into two pieces at address 'addr', a new vma is allocated
26049b1b8d6SLorenzo Stoakes  * either for the first part or the tail.
26149b1b8d6SLorenzo Stoakes  */
26249b1b8d6SLorenzo Stoakes static int split_vma(struct vma_iterator *vmi, struct vm_area_struct *vma,
26349b1b8d6SLorenzo Stoakes 		     unsigned long addr, int new_below)
26449b1b8d6SLorenzo Stoakes {
26549b1b8d6SLorenzo Stoakes 	if (vma->vm_mm->map_count >= sysctl_max_map_count)
26649b1b8d6SLorenzo Stoakes 		return -ENOMEM;
26749b1b8d6SLorenzo Stoakes 
26849b1b8d6SLorenzo Stoakes 	return __split_vma(vmi, vma, addr, new_below);
26949b1b8d6SLorenzo Stoakes }
27049b1b8d6SLorenzo Stoakes 
27149b1b8d6SLorenzo Stoakes /*
27249b1b8d6SLorenzo Stoakes  * init_vma_prep() - Initializer wrapper for vma_prepare struct
27349b1b8d6SLorenzo Stoakes  * @vp: The vma_prepare struct
27449b1b8d6SLorenzo Stoakes  * @vma: The vma that will be altered once locked
27549b1b8d6SLorenzo Stoakes  */
27649b1b8d6SLorenzo Stoakes void init_vma_prep(struct vma_prepare *vp,
27749b1b8d6SLorenzo Stoakes 		   struct vm_area_struct *vma)
27849b1b8d6SLorenzo Stoakes {
27949b1b8d6SLorenzo Stoakes 	init_multi_vma_prep(vp, vma, NULL, NULL, NULL);
28049b1b8d6SLorenzo Stoakes }
28149b1b8d6SLorenzo Stoakes 
28249b1b8d6SLorenzo Stoakes /*
28349b1b8d6SLorenzo Stoakes  * Requires inode->i_mapping->i_mmap_rwsem
28449b1b8d6SLorenzo Stoakes  */
28549b1b8d6SLorenzo Stoakes static void __remove_shared_vm_struct(struct vm_area_struct *vma,
28649b1b8d6SLorenzo Stoakes 				      struct address_space *mapping)
28749b1b8d6SLorenzo Stoakes {
28849b1b8d6SLorenzo Stoakes 	if (vma_is_shared_maywrite(vma))
28949b1b8d6SLorenzo Stoakes 		mapping_unmap_writable(mapping);
29049b1b8d6SLorenzo Stoakes 
29149b1b8d6SLorenzo Stoakes 	flush_dcache_mmap_lock(mapping);
29249b1b8d6SLorenzo Stoakes 	vma_interval_tree_remove(vma, &mapping->i_mmap);
29349b1b8d6SLorenzo Stoakes 	flush_dcache_mmap_unlock(mapping);
29449b1b8d6SLorenzo Stoakes }
29549b1b8d6SLorenzo Stoakes 
29649b1b8d6SLorenzo Stoakes /*
29749b1b8d6SLorenzo Stoakes  * vma has some anon_vma assigned, and is already inserted on that
29849b1b8d6SLorenzo Stoakes  * anon_vma's interval trees.
29949b1b8d6SLorenzo Stoakes  *
30049b1b8d6SLorenzo Stoakes  * Before updating the vma's vm_start / vm_end / vm_pgoff fields, the
30149b1b8d6SLorenzo Stoakes  * vma must be removed from the anon_vma's interval trees using
30249b1b8d6SLorenzo Stoakes  * anon_vma_interval_tree_pre_update_vma().
30349b1b8d6SLorenzo Stoakes  *
30449b1b8d6SLorenzo Stoakes  * After the update, the vma will be reinserted using
30549b1b8d6SLorenzo Stoakes  * anon_vma_interval_tree_post_update_vma().
30649b1b8d6SLorenzo Stoakes  *
30749b1b8d6SLorenzo Stoakes  * The entire update must be protected by exclusive mmap_lock and by
30849b1b8d6SLorenzo Stoakes  * the root anon_vma's mutex.
30949b1b8d6SLorenzo Stoakes  */
31049b1b8d6SLorenzo Stoakes void
31149b1b8d6SLorenzo Stoakes anon_vma_interval_tree_pre_update_vma(struct vm_area_struct *vma)
31249b1b8d6SLorenzo Stoakes {
31349b1b8d6SLorenzo Stoakes 	struct anon_vma_chain *avc;
31449b1b8d6SLorenzo Stoakes 
31549b1b8d6SLorenzo Stoakes 	list_for_each_entry(avc, &vma->anon_vma_chain, same_vma)
31649b1b8d6SLorenzo Stoakes 		anon_vma_interval_tree_remove(avc, &avc->anon_vma->rb_root);
31749b1b8d6SLorenzo Stoakes }
31849b1b8d6SLorenzo Stoakes 
31949b1b8d6SLorenzo Stoakes void
32049b1b8d6SLorenzo Stoakes anon_vma_interval_tree_post_update_vma(struct vm_area_struct *vma)
32149b1b8d6SLorenzo Stoakes {
32249b1b8d6SLorenzo Stoakes 	struct anon_vma_chain *avc;
32349b1b8d6SLorenzo Stoakes 
32449b1b8d6SLorenzo Stoakes 	list_for_each_entry(avc, &vma->anon_vma_chain, same_vma)
32549b1b8d6SLorenzo Stoakes 		anon_vma_interval_tree_insert(avc, &avc->anon_vma->rb_root);
32649b1b8d6SLorenzo Stoakes }
32749b1b8d6SLorenzo Stoakes 
32849b1b8d6SLorenzo Stoakes static void __vma_link_file(struct vm_area_struct *vma,
32949b1b8d6SLorenzo Stoakes 			    struct address_space *mapping)
33049b1b8d6SLorenzo Stoakes {
33149b1b8d6SLorenzo Stoakes 	if (vma_is_shared_maywrite(vma))
33249b1b8d6SLorenzo Stoakes 		mapping_allow_writable(mapping);
33349b1b8d6SLorenzo Stoakes 
33449b1b8d6SLorenzo Stoakes 	flush_dcache_mmap_lock(mapping);
33549b1b8d6SLorenzo Stoakes 	vma_interval_tree_insert(vma, &mapping->i_mmap);
33649b1b8d6SLorenzo Stoakes 	flush_dcache_mmap_unlock(mapping);
33749b1b8d6SLorenzo Stoakes }
33849b1b8d6SLorenzo Stoakes 
33949b1b8d6SLorenzo Stoakes /*
34049b1b8d6SLorenzo Stoakes  * vma_prepare() - Helper function for handling locking VMAs prior to altering
34149b1b8d6SLorenzo Stoakes  * @vp: The initialized vma_prepare struct
34249b1b8d6SLorenzo Stoakes  */
34349b1b8d6SLorenzo Stoakes void vma_prepare(struct vma_prepare *vp)
34449b1b8d6SLorenzo Stoakes {
34549b1b8d6SLorenzo Stoakes 	if (vp->file) {
34649b1b8d6SLorenzo Stoakes 		uprobe_munmap(vp->vma, vp->vma->vm_start, vp->vma->vm_end);
34749b1b8d6SLorenzo Stoakes 
34849b1b8d6SLorenzo Stoakes 		if (vp->adj_next)
34949b1b8d6SLorenzo Stoakes 			uprobe_munmap(vp->adj_next, vp->adj_next->vm_start,
35049b1b8d6SLorenzo Stoakes 				      vp->adj_next->vm_end);
35149b1b8d6SLorenzo Stoakes 
35249b1b8d6SLorenzo Stoakes 		i_mmap_lock_write(vp->mapping);
35349b1b8d6SLorenzo Stoakes 		if (vp->insert && vp->insert->vm_file) {
35449b1b8d6SLorenzo Stoakes 			/*
35549b1b8d6SLorenzo Stoakes 			 * Put into interval tree now, so instantiated pages
35649b1b8d6SLorenzo Stoakes 			 * are visible to arm/parisc __flush_dcache_page
35749b1b8d6SLorenzo Stoakes 			 * throughout; but we cannot insert into address
35849b1b8d6SLorenzo Stoakes 			 * space until vma start or end is updated.
35949b1b8d6SLorenzo Stoakes 			 */
36049b1b8d6SLorenzo Stoakes 			__vma_link_file(vp->insert,
36149b1b8d6SLorenzo Stoakes 					vp->insert->vm_file->f_mapping);
36249b1b8d6SLorenzo Stoakes 		}
36349b1b8d6SLorenzo Stoakes 	}
36449b1b8d6SLorenzo Stoakes 
36549b1b8d6SLorenzo Stoakes 	if (vp->anon_vma) {
36649b1b8d6SLorenzo Stoakes 		anon_vma_lock_write(vp->anon_vma);
36749b1b8d6SLorenzo Stoakes 		anon_vma_interval_tree_pre_update_vma(vp->vma);
36849b1b8d6SLorenzo Stoakes 		if (vp->adj_next)
36949b1b8d6SLorenzo Stoakes 			anon_vma_interval_tree_pre_update_vma(vp->adj_next);
37049b1b8d6SLorenzo Stoakes 	}
37149b1b8d6SLorenzo Stoakes 
37249b1b8d6SLorenzo Stoakes 	if (vp->file) {
37349b1b8d6SLorenzo Stoakes 		flush_dcache_mmap_lock(vp->mapping);
37449b1b8d6SLorenzo Stoakes 		vma_interval_tree_remove(vp->vma, &vp->mapping->i_mmap);
37549b1b8d6SLorenzo Stoakes 		if (vp->adj_next)
37649b1b8d6SLorenzo Stoakes 			vma_interval_tree_remove(vp->adj_next,
37749b1b8d6SLorenzo Stoakes 						 &vp->mapping->i_mmap);
37849b1b8d6SLorenzo Stoakes 	}
37949b1b8d6SLorenzo Stoakes 
38049b1b8d6SLorenzo Stoakes }
38149b1b8d6SLorenzo Stoakes 
38249b1b8d6SLorenzo Stoakes /*
38349b1b8d6SLorenzo Stoakes  * dup_anon_vma() - Helper function to duplicate anon_vma
38449b1b8d6SLorenzo Stoakes  * @dst: The destination VMA
38549b1b8d6SLorenzo Stoakes  * @src: The source VMA
38649b1b8d6SLorenzo Stoakes  * @dup: Pointer to the destination VMA when successful.
38749b1b8d6SLorenzo Stoakes  *
38849b1b8d6SLorenzo Stoakes  * Returns: 0 on success.
38949b1b8d6SLorenzo Stoakes  */
39049b1b8d6SLorenzo Stoakes static int dup_anon_vma(struct vm_area_struct *dst,
39149b1b8d6SLorenzo Stoakes 			struct vm_area_struct *src, struct vm_area_struct **dup)
39249b1b8d6SLorenzo Stoakes {
39349b1b8d6SLorenzo Stoakes 	/*
39449b1b8d6SLorenzo Stoakes 	 * Easily overlooked: when mprotect shifts the boundary, make sure the
39549b1b8d6SLorenzo Stoakes 	 * expanding vma has anon_vma set if the shrinking vma had, to cover any
39649b1b8d6SLorenzo Stoakes 	 * anon pages imported.
39749b1b8d6SLorenzo Stoakes 	 */
39849b1b8d6SLorenzo Stoakes 	if (src->anon_vma && !dst->anon_vma) {
39949b1b8d6SLorenzo Stoakes 		int ret;
40049b1b8d6SLorenzo Stoakes 
40149b1b8d6SLorenzo Stoakes 		vma_assert_write_locked(dst);
40249b1b8d6SLorenzo Stoakes 		dst->anon_vma = src->anon_vma;
40349b1b8d6SLorenzo Stoakes 		ret = anon_vma_clone(dst, src);
40449b1b8d6SLorenzo Stoakes 		if (ret)
40549b1b8d6SLorenzo Stoakes 			return ret;
40649b1b8d6SLorenzo Stoakes 
40749b1b8d6SLorenzo Stoakes 		*dup = dst;
40849b1b8d6SLorenzo Stoakes 	}
40949b1b8d6SLorenzo Stoakes 
41049b1b8d6SLorenzo Stoakes 	return 0;
41149b1b8d6SLorenzo Stoakes }
41249b1b8d6SLorenzo Stoakes 
41349b1b8d6SLorenzo Stoakes #ifdef CONFIG_DEBUG_VM_MAPLE_TREE
41449b1b8d6SLorenzo Stoakes void validate_mm(struct mm_struct *mm)
41549b1b8d6SLorenzo Stoakes {
41649b1b8d6SLorenzo Stoakes 	int bug = 0;
41749b1b8d6SLorenzo Stoakes 	int i = 0;
41849b1b8d6SLorenzo Stoakes 	struct vm_area_struct *vma;
41949b1b8d6SLorenzo Stoakes 	VMA_ITERATOR(vmi, mm, 0);
42049b1b8d6SLorenzo Stoakes 
42149b1b8d6SLorenzo Stoakes 	mt_validate(&mm->mm_mt);
42249b1b8d6SLorenzo Stoakes 	for_each_vma(vmi, vma) {
42349b1b8d6SLorenzo Stoakes #ifdef CONFIG_DEBUG_VM_RB
42449b1b8d6SLorenzo Stoakes 		struct anon_vma *anon_vma = vma->anon_vma;
42549b1b8d6SLorenzo Stoakes 		struct anon_vma_chain *avc;
42649b1b8d6SLorenzo Stoakes #endif
42749b1b8d6SLorenzo Stoakes 		unsigned long vmi_start, vmi_end;
42849b1b8d6SLorenzo Stoakes 		bool warn = 0;
42949b1b8d6SLorenzo Stoakes 
43049b1b8d6SLorenzo Stoakes 		vmi_start = vma_iter_addr(&vmi);
43149b1b8d6SLorenzo Stoakes 		vmi_end = vma_iter_end(&vmi);
43249b1b8d6SLorenzo Stoakes 		if (VM_WARN_ON_ONCE_MM(vma->vm_end != vmi_end, mm))
43349b1b8d6SLorenzo Stoakes 			warn = 1;
43449b1b8d6SLorenzo Stoakes 
43549b1b8d6SLorenzo Stoakes 		if (VM_WARN_ON_ONCE_MM(vma->vm_start != vmi_start, mm))
43649b1b8d6SLorenzo Stoakes 			warn = 1;
43749b1b8d6SLorenzo Stoakes 
43849b1b8d6SLorenzo Stoakes 		if (warn) {
43949b1b8d6SLorenzo Stoakes 			pr_emerg("issue in %s\n", current->comm);
44049b1b8d6SLorenzo Stoakes 			dump_stack();
44149b1b8d6SLorenzo Stoakes 			dump_vma(vma);
44249b1b8d6SLorenzo Stoakes 			pr_emerg("tree range: %px start %lx end %lx\n", vma,
44349b1b8d6SLorenzo Stoakes 				 vmi_start, vmi_end - 1);
44449b1b8d6SLorenzo Stoakes 			vma_iter_dump_tree(&vmi);
44549b1b8d6SLorenzo Stoakes 		}
44649b1b8d6SLorenzo Stoakes 
44749b1b8d6SLorenzo Stoakes #ifdef CONFIG_DEBUG_VM_RB
44849b1b8d6SLorenzo Stoakes 		if (anon_vma) {
44949b1b8d6SLorenzo Stoakes 			anon_vma_lock_read(anon_vma);
45049b1b8d6SLorenzo Stoakes 			list_for_each_entry(avc, &vma->anon_vma_chain, same_vma)
45149b1b8d6SLorenzo Stoakes 				anon_vma_interval_tree_verify(avc);
45249b1b8d6SLorenzo Stoakes 			anon_vma_unlock_read(anon_vma);
45349b1b8d6SLorenzo Stoakes 		}
45449b1b8d6SLorenzo Stoakes #endif
45549b1b8d6SLorenzo Stoakes 		i++;
45649b1b8d6SLorenzo Stoakes 	}
45749b1b8d6SLorenzo Stoakes 	if (i != mm->map_count) {
45849b1b8d6SLorenzo Stoakes 		pr_emerg("map_count %d vma iterator %d\n", mm->map_count, i);
45949b1b8d6SLorenzo Stoakes 		bug = 1;
46049b1b8d6SLorenzo Stoakes 	}
46149b1b8d6SLorenzo Stoakes 	VM_BUG_ON_MM(bug, mm);
46249b1b8d6SLorenzo Stoakes }
46349b1b8d6SLorenzo Stoakes #endif /* CONFIG_DEBUG_VM_MAPLE_TREE */
46449b1b8d6SLorenzo Stoakes 
46549b1b8d6SLorenzo Stoakes /*
46649b1b8d6SLorenzo Stoakes  * vma_expand - Expand an existing VMA
46749b1b8d6SLorenzo Stoakes  *
46849b1b8d6SLorenzo Stoakes  * @vmi: The vma iterator
46949b1b8d6SLorenzo Stoakes  * @vma: The vma to expand
47049b1b8d6SLorenzo Stoakes  * @start: The start of the vma
47149b1b8d6SLorenzo Stoakes  * @end: The exclusive end of the vma
47249b1b8d6SLorenzo Stoakes  * @pgoff: The page offset of vma
47349b1b8d6SLorenzo Stoakes  * @next: The current of next vma.
47449b1b8d6SLorenzo Stoakes  *
47549b1b8d6SLorenzo Stoakes  * Expand @vma to @start and @end.  Can expand off the start and end.  Will
47649b1b8d6SLorenzo Stoakes  * expand over @next if it's different from @vma and @end == @next->vm_end.
47749b1b8d6SLorenzo Stoakes  * Checking if the @vma can expand and merge with @next needs to be handled by
47849b1b8d6SLorenzo Stoakes  * the caller.
47949b1b8d6SLorenzo Stoakes  *
48049b1b8d6SLorenzo Stoakes  * Returns: 0 on success
48149b1b8d6SLorenzo Stoakes  */
48249b1b8d6SLorenzo Stoakes int vma_expand(struct vma_iterator *vmi, struct vm_area_struct *vma,
48349b1b8d6SLorenzo Stoakes 	       unsigned long start, unsigned long end, pgoff_t pgoff,
48449b1b8d6SLorenzo Stoakes 	       struct vm_area_struct *next)
48549b1b8d6SLorenzo Stoakes {
48649b1b8d6SLorenzo Stoakes 	struct vm_area_struct *anon_dup = NULL;
48749b1b8d6SLorenzo Stoakes 	bool remove_next = false;
48849b1b8d6SLorenzo Stoakes 	struct vma_prepare vp;
48949b1b8d6SLorenzo Stoakes 
49049b1b8d6SLorenzo Stoakes 	vma_start_write(vma);
49149b1b8d6SLorenzo Stoakes 	if (next && (vma != next) && (end == next->vm_end)) {
49249b1b8d6SLorenzo Stoakes 		int ret;
49349b1b8d6SLorenzo Stoakes 
49449b1b8d6SLorenzo Stoakes 		remove_next = true;
49549b1b8d6SLorenzo Stoakes 		vma_start_write(next);
49649b1b8d6SLorenzo Stoakes 		ret = dup_anon_vma(vma, next, &anon_dup);
49749b1b8d6SLorenzo Stoakes 		if (ret)
49849b1b8d6SLorenzo Stoakes 			return ret;
49949b1b8d6SLorenzo Stoakes 	}
50049b1b8d6SLorenzo Stoakes 
50149b1b8d6SLorenzo Stoakes 	init_multi_vma_prep(&vp, vma, NULL, remove_next ? next : NULL, NULL);
50249b1b8d6SLorenzo Stoakes 	/* Not merging but overwriting any part of next is not handled. */
50349b1b8d6SLorenzo Stoakes 	VM_WARN_ON(next && !vp.remove &&
50449b1b8d6SLorenzo Stoakes 		  next != vma && end > next->vm_start);
50549b1b8d6SLorenzo Stoakes 	/* Only handles expanding */
50649b1b8d6SLorenzo Stoakes 	VM_WARN_ON(vma->vm_start < start || vma->vm_end > end);
50749b1b8d6SLorenzo Stoakes 
50849b1b8d6SLorenzo Stoakes 	/* Note: vma iterator must be pointing to 'start' */
50949b1b8d6SLorenzo Stoakes 	vma_iter_config(vmi, start, end);
51049b1b8d6SLorenzo Stoakes 	if (vma_iter_prealloc(vmi, vma))
51149b1b8d6SLorenzo Stoakes 		goto nomem;
51249b1b8d6SLorenzo Stoakes 
51349b1b8d6SLorenzo Stoakes 	vma_prepare(&vp);
51449b1b8d6SLorenzo Stoakes 	vma_adjust_trans_huge(vma, start, end, 0);
51549b1b8d6SLorenzo Stoakes 	vma_set_range(vma, start, end, pgoff);
51649b1b8d6SLorenzo Stoakes 	vma_iter_store(vmi, vma);
51749b1b8d6SLorenzo Stoakes 
51849b1b8d6SLorenzo Stoakes 	vma_complete(&vp, vmi, vma->vm_mm);
51949b1b8d6SLorenzo Stoakes 	return 0;
52049b1b8d6SLorenzo Stoakes 
52149b1b8d6SLorenzo Stoakes nomem:
52249b1b8d6SLorenzo Stoakes 	if (anon_dup)
52349b1b8d6SLorenzo Stoakes 		unlink_anon_vmas(anon_dup);
52449b1b8d6SLorenzo Stoakes 	return -ENOMEM;
52549b1b8d6SLorenzo Stoakes }
52649b1b8d6SLorenzo Stoakes 
52749b1b8d6SLorenzo Stoakes /*
52849b1b8d6SLorenzo Stoakes  * vma_shrink() - Reduce an existing VMAs memory area
52949b1b8d6SLorenzo Stoakes  * @vmi: The vma iterator
53049b1b8d6SLorenzo Stoakes  * @vma: The VMA to modify
53149b1b8d6SLorenzo Stoakes  * @start: The new start
53249b1b8d6SLorenzo Stoakes  * @end: The new end
53349b1b8d6SLorenzo Stoakes  *
53449b1b8d6SLorenzo Stoakes  * Returns: 0 on success, -ENOMEM otherwise
53549b1b8d6SLorenzo Stoakes  */
53649b1b8d6SLorenzo Stoakes int vma_shrink(struct vma_iterator *vmi, struct vm_area_struct *vma,
53749b1b8d6SLorenzo Stoakes 	       unsigned long start, unsigned long end, pgoff_t pgoff)
53849b1b8d6SLorenzo Stoakes {
53949b1b8d6SLorenzo Stoakes 	struct vma_prepare vp;
54049b1b8d6SLorenzo Stoakes 
54149b1b8d6SLorenzo Stoakes 	WARN_ON((vma->vm_start != start) && (vma->vm_end != end));
54249b1b8d6SLorenzo Stoakes 
54349b1b8d6SLorenzo Stoakes 	if (vma->vm_start < start)
54449b1b8d6SLorenzo Stoakes 		vma_iter_config(vmi, vma->vm_start, start);
54549b1b8d6SLorenzo Stoakes 	else
54649b1b8d6SLorenzo Stoakes 		vma_iter_config(vmi, end, vma->vm_end);
54749b1b8d6SLorenzo Stoakes 
54849b1b8d6SLorenzo Stoakes 	if (vma_iter_prealloc(vmi, NULL))
54949b1b8d6SLorenzo Stoakes 		return -ENOMEM;
55049b1b8d6SLorenzo Stoakes 
55149b1b8d6SLorenzo Stoakes 	vma_start_write(vma);
55249b1b8d6SLorenzo Stoakes 
55349b1b8d6SLorenzo Stoakes 	init_vma_prep(&vp, vma);
55449b1b8d6SLorenzo Stoakes 	vma_prepare(&vp);
55549b1b8d6SLorenzo Stoakes 	vma_adjust_trans_huge(vma, start, end, 0);
55649b1b8d6SLorenzo Stoakes 
55749b1b8d6SLorenzo Stoakes 	vma_iter_clear(vmi);
55849b1b8d6SLorenzo Stoakes 	vma_set_range(vma, start, end, pgoff);
55949b1b8d6SLorenzo Stoakes 	vma_complete(&vp, vmi, vma->vm_mm);
56089b2d2a5SLiam R. Howlett 	validate_mm(vma->vm_mm);
56149b1b8d6SLorenzo Stoakes 	return 0;
56249b1b8d6SLorenzo Stoakes }
56349b1b8d6SLorenzo Stoakes 
56449b1b8d6SLorenzo Stoakes /*
56549b1b8d6SLorenzo Stoakes  * vma_complete- Helper function for handling the unlocking after altering VMAs,
56649b1b8d6SLorenzo Stoakes  * or for inserting a VMA.
56749b1b8d6SLorenzo Stoakes  *
56849b1b8d6SLorenzo Stoakes  * @vp: The vma_prepare struct
56949b1b8d6SLorenzo Stoakes  * @vmi: The vma iterator
57049b1b8d6SLorenzo Stoakes  * @mm: The mm_struct
57149b1b8d6SLorenzo Stoakes  */
57249b1b8d6SLorenzo Stoakes void vma_complete(struct vma_prepare *vp,
57349b1b8d6SLorenzo Stoakes 		  struct vma_iterator *vmi, struct mm_struct *mm)
57449b1b8d6SLorenzo Stoakes {
57549b1b8d6SLorenzo Stoakes 	if (vp->file) {
57649b1b8d6SLorenzo Stoakes 		if (vp->adj_next)
57749b1b8d6SLorenzo Stoakes 			vma_interval_tree_insert(vp->adj_next,
57849b1b8d6SLorenzo Stoakes 						 &vp->mapping->i_mmap);
57949b1b8d6SLorenzo Stoakes 		vma_interval_tree_insert(vp->vma, &vp->mapping->i_mmap);
58049b1b8d6SLorenzo Stoakes 		flush_dcache_mmap_unlock(vp->mapping);
58149b1b8d6SLorenzo Stoakes 	}
58249b1b8d6SLorenzo Stoakes 
58349b1b8d6SLorenzo Stoakes 	if (vp->remove && vp->file) {
58449b1b8d6SLorenzo Stoakes 		__remove_shared_vm_struct(vp->remove, vp->mapping);
58549b1b8d6SLorenzo Stoakes 		if (vp->remove2)
58649b1b8d6SLorenzo Stoakes 			__remove_shared_vm_struct(vp->remove2, vp->mapping);
58749b1b8d6SLorenzo Stoakes 	} else if (vp->insert) {
58849b1b8d6SLorenzo Stoakes 		/*
58949b1b8d6SLorenzo Stoakes 		 * split_vma has split insert from vma, and needs
59049b1b8d6SLorenzo Stoakes 		 * us to insert it before dropping the locks
59149b1b8d6SLorenzo Stoakes 		 * (it may either follow vma or precede it).
59249b1b8d6SLorenzo Stoakes 		 */
59349b1b8d6SLorenzo Stoakes 		vma_iter_store(vmi, vp->insert);
59449b1b8d6SLorenzo Stoakes 		mm->map_count++;
59549b1b8d6SLorenzo Stoakes 	}
59649b1b8d6SLorenzo Stoakes 
59749b1b8d6SLorenzo Stoakes 	if (vp->anon_vma) {
59849b1b8d6SLorenzo Stoakes 		anon_vma_interval_tree_post_update_vma(vp->vma);
59949b1b8d6SLorenzo Stoakes 		if (vp->adj_next)
60049b1b8d6SLorenzo Stoakes 			anon_vma_interval_tree_post_update_vma(vp->adj_next);
60149b1b8d6SLorenzo Stoakes 		anon_vma_unlock_write(vp->anon_vma);
60249b1b8d6SLorenzo Stoakes 	}
60349b1b8d6SLorenzo Stoakes 
60449b1b8d6SLorenzo Stoakes 	if (vp->file) {
60549b1b8d6SLorenzo Stoakes 		i_mmap_unlock_write(vp->mapping);
60649b1b8d6SLorenzo Stoakes 		uprobe_mmap(vp->vma);
60749b1b8d6SLorenzo Stoakes 
60849b1b8d6SLorenzo Stoakes 		if (vp->adj_next)
60949b1b8d6SLorenzo Stoakes 			uprobe_mmap(vp->adj_next);
61049b1b8d6SLorenzo Stoakes 	}
61149b1b8d6SLorenzo Stoakes 
61249b1b8d6SLorenzo Stoakes 	if (vp->remove) {
61349b1b8d6SLorenzo Stoakes again:
61449b1b8d6SLorenzo Stoakes 		vma_mark_detached(vp->remove, true);
61549b1b8d6SLorenzo Stoakes 		if (vp->file) {
61649b1b8d6SLorenzo Stoakes 			uprobe_munmap(vp->remove, vp->remove->vm_start,
61749b1b8d6SLorenzo Stoakes 				      vp->remove->vm_end);
61849b1b8d6SLorenzo Stoakes 			fput(vp->file);
61949b1b8d6SLorenzo Stoakes 		}
62049b1b8d6SLorenzo Stoakes 		if (vp->remove->anon_vma)
62149b1b8d6SLorenzo Stoakes 			anon_vma_merge(vp->vma, vp->remove);
62249b1b8d6SLorenzo Stoakes 		mm->map_count--;
62349b1b8d6SLorenzo Stoakes 		mpol_put(vma_policy(vp->remove));
62449b1b8d6SLorenzo Stoakes 		if (!vp->remove2)
62549b1b8d6SLorenzo Stoakes 			WARN_ON_ONCE(vp->vma->vm_end < vp->remove->vm_end);
62649b1b8d6SLorenzo Stoakes 		vm_area_free(vp->remove);
62749b1b8d6SLorenzo Stoakes 
62849b1b8d6SLorenzo Stoakes 		/*
62949b1b8d6SLorenzo Stoakes 		 * In mprotect's case 6 (see comments on vma_merge),
63049b1b8d6SLorenzo Stoakes 		 * we are removing both mid and next vmas
63149b1b8d6SLorenzo Stoakes 		 */
63249b1b8d6SLorenzo Stoakes 		if (vp->remove2) {
63349b1b8d6SLorenzo Stoakes 			vp->remove = vp->remove2;
63449b1b8d6SLorenzo Stoakes 			vp->remove2 = NULL;
63549b1b8d6SLorenzo Stoakes 			goto again;
63649b1b8d6SLorenzo Stoakes 		}
63749b1b8d6SLorenzo Stoakes 	}
63849b1b8d6SLorenzo Stoakes 	if (vp->insert && vp->file)
63949b1b8d6SLorenzo Stoakes 		uprobe_mmap(vp->insert);
64049b1b8d6SLorenzo Stoakes }
64149b1b8d6SLorenzo Stoakes 
642f8d112a4SLiam R. Howlett static inline void vms_clear_ptes(struct vma_munmap_struct *vms,
6439c3ebedaSLiam R. Howlett 		    struct ma_state *mas_detach, bool mm_wr_locked)
6449c3ebedaSLiam R. Howlett {
6459c3ebedaSLiam R. Howlett 	struct mmu_gather tlb;
6469c3ebedaSLiam R. Howlett 
647f8d112a4SLiam R. Howlett 	if (!vms->clear_ptes) /* Nothing to do */
648f8d112a4SLiam R. Howlett 		return;
649f8d112a4SLiam R. Howlett 
6509c3ebedaSLiam R. Howlett 	/*
6519c3ebedaSLiam R. Howlett 	 * We can free page tables without write-locking mmap_lock because VMAs
6529c3ebedaSLiam R. Howlett 	 * were isolated before we downgraded mmap_lock.
6539c3ebedaSLiam R. Howlett 	 */
6549c3ebedaSLiam R. Howlett 	mas_set(mas_detach, 1);
6559c3ebedaSLiam R. Howlett 	lru_add_drain();
65663fc66f5SLiam R. Howlett 	tlb_gather_mmu(&tlb, vms->vma->vm_mm);
65763fc66f5SLiam R. Howlett 	update_hiwater_rss(vms->vma->vm_mm);
658f8d112a4SLiam R. Howlett 	unmap_vmas(&tlb, mas_detach, vms->vma, vms->start, vms->end,
659f8d112a4SLiam R. Howlett 		   vms->vma_count, mm_wr_locked);
660f8d112a4SLiam R. Howlett 
6619c3ebedaSLiam R. Howlett 	mas_set(mas_detach, 1);
6629c3ebedaSLiam R. Howlett 	/* start and end may be different if there is no prev or next vma. */
663f8d112a4SLiam R. Howlett 	free_pgtables(&tlb, mas_detach, vms->vma, vms->unmap_start,
664f8d112a4SLiam R. Howlett 		      vms->unmap_end, mm_wr_locked);
6659c3ebedaSLiam R. Howlett 	tlb_finish_mmu(&tlb);
666f8d112a4SLiam R. Howlett 	vms->clear_ptes = false;
667f8d112a4SLiam R. Howlett }
668f8d112a4SLiam R. Howlett 
669f8d112a4SLiam R. Howlett void vms_clean_up_area(struct vma_munmap_struct *vms,
67063fc66f5SLiam R. Howlett 		struct ma_state *mas_detach)
671f8d112a4SLiam R. Howlett {
672f8d112a4SLiam R. Howlett 	struct vm_area_struct *vma;
673f8d112a4SLiam R. Howlett 
674f8d112a4SLiam R. Howlett 	if (!vms->nr_pages)
675f8d112a4SLiam R. Howlett 		return;
676f8d112a4SLiam R. Howlett 
67763fc66f5SLiam R. Howlett 	vms_clear_ptes(vms, mas_detach, true);
678f8d112a4SLiam R. Howlett 	mas_set(mas_detach, 0);
679f8d112a4SLiam R. Howlett 	mas_for_each(mas_detach, vma, ULONG_MAX)
680f8d112a4SLiam R. Howlett 		if (vma->vm_ops && vma->vm_ops->close)
681f8d112a4SLiam R. Howlett 			vma->vm_ops->close(vma);
682f8d112a4SLiam R. Howlett 	vms->closed_vm_ops = true;
6839c3ebedaSLiam R. Howlett }
6849c3ebedaSLiam R. Howlett 
68549b1b8d6SLorenzo Stoakes /*
686dba14840SLiam R. Howlett  * vms_complete_munmap_vmas() - Finish the munmap() operation
687dba14840SLiam R. Howlett  * @vms: The vma munmap struct
688dba14840SLiam R. Howlett  * @mas_detach: The maple state of the detached vmas
68901cf21e9SLiam R. Howlett  *
690dba14840SLiam R. Howlett  * This updates the mm_struct, unmaps the region, frees the resources
69101cf21e9SLiam R. Howlett  * used for the munmap() and may downgrade the lock - if requested.  Everything
69201cf21e9SLiam R. Howlett  * needed to be done once the vma maple tree is updated.
69301cf21e9SLiam R. Howlett  */
6949014b230SLiam R. Howlett void vms_complete_munmap_vmas(struct vma_munmap_struct *vms,
695dba14840SLiam R. Howlett 		struct ma_state *mas_detach)
69601cf21e9SLiam R. Howlett {
69717f1ae9bSLiam R. Howlett 	struct vm_area_struct *vma;
698dba14840SLiam R. Howlett 	struct mm_struct *mm;
69901cf21e9SLiam R. Howlett 
70063fc66f5SLiam R. Howlett 	mm = current->mm;
701dba14840SLiam R. Howlett 	mm->map_count -= vms->vma_count;
702dba14840SLiam R. Howlett 	mm->locked_vm -= vms->locked_vm;
703dba14840SLiam R. Howlett 	if (vms->unlock)
70401cf21e9SLiam R. Howlett 		mmap_write_downgrade(mm);
70501cf21e9SLiam R. Howlett 
706f8d112a4SLiam R. Howlett 	if (!vms->nr_pages)
707f8d112a4SLiam R. Howlett 		return;
708f8d112a4SLiam R. Howlett 
709f8d112a4SLiam R. Howlett 	vms_clear_ptes(vms, mas_detach, !vms->unlock);
71017f1ae9bSLiam R. Howlett 	/* Update high watermark before we lower total_vm */
71117f1ae9bSLiam R. Howlett 	update_hiwater_vm(mm);
71217f1ae9bSLiam R. Howlett 	/* Stat accounting */
71317f1ae9bSLiam R. Howlett 	WRITE_ONCE(mm->total_vm, READ_ONCE(mm->total_vm) - vms->nr_pages);
71417f1ae9bSLiam R. Howlett 	/* Paranoid bookkeeping */
71517f1ae9bSLiam R. Howlett 	VM_WARN_ON(vms->exec_vm > mm->exec_vm);
71617f1ae9bSLiam R. Howlett 	VM_WARN_ON(vms->stack_vm > mm->stack_vm);
71717f1ae9bSLiam R. Howlett 	VM_WARN_ON(vms->data_vm > mm->data_vm);
71817f1ae9bSLiam R. Howlett 	mm->exec_vm -= vms->exec_vm;
71917f1ae9bSLiam R. Howlett 	mm->stack_vm -= vms->stack_vm;
72017f1ae9bSLiam R. Howlett 	mm->data_vm -= vms->data_vm;
72117f1ae9bSLiam R. Howlett 
72217f1ae9bSLiam R. Howlett 	/* Remove and clean up vmas */
72301cf21e9SLiam R. Howlett 	mas_set(mas_detach, 0);
72417f1ae9bSLiam R. Howlett 	mas_for_each(mas_detach, vma, ULONG_MAX)
725f8d112a4SLiam R. Howlett 		remove_vma(vma, /* = */ false, vms->closed_vm_ops);
72617f1ae9bSLiam R. Howlett 
72717f1ae9bSLiam R. Howlett 	vm_unacct_memory(vms->nr_accounted);
72801cf21e9SLiam R. Howlett 	validate_mm(mm);
729dba14840SLiam R. Howlett 	if (vms->unlock)
73001cf21e9SLiam R. Howlett 		mmap_read_unlock(mm);
73101cf21e9SLiam R. Howlett 
73201cf21e9SLiam R. Howlett 	__mt_destroy(mas_detach->tree);
73301cf21e9SLiam R. Howlett }
73401cf21e9SLiam R. Howlett 
73501cf21e9SLiam R. Howlett /*
736dba14840SLiam R. Howlett  * vms_gather_munmap_vmas() - Put all VMAs within a range into a maple tree
7376898c903SLiam R. Howlett  * for removal at a later date.  Handles splitting first and last if necessary
7386898c903SLiam R. Howlett  * and marking the vmas as isolated.
7396898c903SLiam R. Howlett  *
740dba14840SLiam R. Howlett  * @vms: The vma munmap struct
7416898c903SLiam R. Howlett  * @mas_detach: The maple state tracking the detached tree
74249b1b8d6SLorenzo Stoakes  *
7436898c903SLiam R. Howlett  * Return: 0 on success, -EPERM on mseal vmas, -ENOMEM otherwise
74449b1b8d6SLorenzo Stoakes  */
7459014b230SLiam R. Howlett int vms_gather_munmap_vmas(struct vma_munmap_struct *vms,
746dba14840SLiam R. Howlett 		struct ma_state *mas_detach)
74749b1b8d6SLorenzo Stoakes {
74801cf21e9SLiam R. Howlett 	struct vm_area_struct *next = NULL;
74949b1b8d6SLorenzo Stoakes 	int error = -ENOMEM;
75049b1b8d6SLorenzo Stoakes 
75149b1b8d6SLorenzo Stoakes 	/*
75249b1b8d6SLorenzo Stoakes 	 * If we need to split any vma, do it now to save pain later.
75320831cd6SLiam R. Howlett 	 * Does it split the first one?
75449b1b8d6SLorenzo Stoakes 	 */
755dba14840SLiam R. Howlett 	if (vms->start > vms->vma->vm_start) {
75649b1b8d6SLorenzo Stoakes 
75749b1b8d6SLorenzo Stoakes 		/*
75849b1b8d6SLorenzo Stoakes 		 * Make sure that map_count on return from munmap() will
75949b1b8d6SLorenzo Stoakes 		 * not exceed its limit; but let map_count go just above
76049b1b8d6SLorenzo Stoakes 		 * its limit temporarily, to help free resources as expected.
76149b1b8d6SLorenzo Stoakes 		 */
762dba14840SLiam R. Howlett 		if (vms->end < vms->vma->vm_end &&
76363fc66f5SLiam R. Howlett 		    vms->vma->vm_mm->map_count >= sysctl_max_map_count)
76449b1b8d6SLorenzo Stoakes 			goto map_count_exceeded;
76549b1b8d6SLorenzo Stoakes 
766df2a7df9SPedro Falcato 		/* Don't bother splitting the VMA if we can't unmap it anyway */
767dba14840SLiam R. Howlett 		if (!can_modify_vma(vms->vma)) {
768df2a7df9SPedro Falcato 			error = -EPERM;
769df2a7df9SPedro Falcato 			goto start_split_failed;
770df2a7df9SPedro Falcato 		}
771df2a7df9SPedro Falcato 
772dba14840SLiam R. Howlett 		if (__split_vma(vms->vmi, vms->vma, vms->start, 1))
77349b1b8d6SLorenzo Stoakes 			goto start_split_failed;
77449b1b8d6SLorenzo Stoakes 	}
77517f1ae9bSLiam R. Howlett 	vms->prev = vma_prev(vms->vmi);
7769c3ebedaSLiam R. Howlett 	if (vms->prev)
7779c3ebedaSLiam R. Howlett 		vms->unmap_start = vms->prev->vm_end;
77849b1b8d6SLorenzo Stoakes 
77949b1b8d6SLorenzo Stoakes 	/*
78049b1b8d6SLorenzo Stoakes 	 * Detach a range of VMAs from the mm. Using next as a temp variable as
78149b1b8d6SLorenzo Stoakes 	 * it is always overwritten.
78249b1b8d6SLorenzo Stoakes 	 */
78317f1ae9bSLiam R. Howlett 	for_each_vma_range(*(vms->vmi), next, vms->end) {
78417f1ae9bSLiam R. Howlett 		long nrpages;
78517f1ae9bSLiam R. Howlett 
786df2a7df9SPedro Falcato 		if (!can_modify_vma(next)) {
787df2a7df9SPedro Falcato 			error = -EPERM;
788df2a7df9SPedro Falcato 			goto modify_vma_failed;
789df2a7df9SPedro Falcato 		}
79049b1b8d6SLorenzo Stoakes 		/* Does it split the end? */
791dba14840SLiam R. Howlett 		if (next->vm_end > vms->end) {
792dba14840SLiam R. Howlett 			if (__split_vma(vms->vmi, next, vms->end, 0))
79349b1b8d6SLorenzo Stoakes 				goto end_split_failed;
79449b1b8d6SLorenzo Stoakes 		}
79549b1b8d6SLorenzo Stoakes 		vma_start_write(next);
796dba14840SLiam R. Howlett 		mas_set(mas_detach, vms->vma_count++);
7976898c903SLiam R. Howlett 		if (mas_store_gfp(mas_detach, next, GFP_KERNEL))
79849b1b8d6SLorenzo Stoakes 			goto munmap_gather_failed;
7996898c903SLiam R. Howlett 
80049b1b8d6SLorenzo Stoakes 		vma_mark_detached(next, true);
80117f1ae9bSLiam R. Howlett 		nrpages = vma_pages(next);
80217f1ae9bSLiam R. Howlett 
80317f1ae9bSLiam R. Howlett 		vms->nr_pages += nrpages;
80449b1b8d6SLorenzo Stoakes 		if (next->vm_flags & VM_LOCKED)
80517f1ae9bSLiam R. Howlett 			vms->locked_vm += nrpages;
80617f1ae9bSLiam R. Howlett 
80717f1ae9bSLiam R. Howlett 		if (next->vm_flags & VM_ACCOUNT)
80817f1ae9bSLiam R. Howlett 			vms->nr_accounted += nrpages;
80917f1ae9bSLiam R. Howlett 
81017f1ae9bSLiam R. Howlett 		if (is_exec_mapping(next->vm_flags))
81117f1ae9bSLiam R. Howlett 			vms->exec_vm += nrpages;
81217f1ae9bSLiam R. Howlett 		else if (is_stack_mapping(next->vm_flags))
81317f1ae9bSLiam R. Howlett 			vms->stack_vm += nrpages;
81417f1ae9bSLiam R. Howlett 		else if (is_data_mapping(next->vm_flags))
81517f1ae9bSLiam R. Howlett 			vms->data_vm += nrpages;
81649b1b8d6SLorenzo Stoakes 
817dba14840SLiam R. Howlett 		if (unlikely(vms->uf)) {
81849b1b8d6SLorenzo Stoakes 			/*
81949b1b8d6SLorenzo Stoakes 			 * If userfaultfd_unmap_prep returns an error the vmas
82049b1b8d6SLorenzo Stoakes 			 * will remain split, but userland will get a
82149b1b8d6SLorenzo Stoakes 			 * highly unexpected error anyway. This is no
82249b1b8d6SLorenzo Stoakes 			 * different than the case where the first of the two
82349b1b8d6SLorenzo Stoakes 			 * __split_vma fails, but we don't undo the first
82449b1b8d6SLorenzo Stoakes 			 * split, despite we could. This is unlikely enough
82549b1b8d6SLorenzo Stoakes 			 * failure that it's not worth optimizing it for.
82649b1b8d6SLorenzo Stoakes 			 */
827dba14840SLiam R. Howlett 			if (userfaultfd_unmap_prep(next, vms->start, vms->end,
828dba14840SLiam R. Howlett 						   vms->uf))
82949b1b8d6SLorenzo Stoakes 				goto userfaultfd_error;
83049b1b8d6SLorenzo Stoakes 		}
83149b1b8d6SLorenzo Stoakes #ifdef CONFIG_DEBUG_VM_MAPLE_TREE
832dba14840SLiam R. Howlett 		BUG_ON(next->vm_start < vms->start);
833dba14840SLiam R. Howlett 		BUG_ON(next->vm_start > vms->end);
83449b1b8d6SLorenzo Stoakes #endif
83517f1ae9bSLiam R. Howlett 	}
83617f1ae9bSLiam R. Howlett 
83717f1ae9bSLiam R. Howlett 	vms->next = vma_next(vms->vmi);
8389c3ebedaSLiam R. Howlett 	if (vms->next)
8399c3ebedaSLiam R. Howlett 		vms->unmap_end = vms->next->vm_start;
84049b1b8d6SLorenzo Stoakes 
84149b1b8d6SLorenzo Stoakes #if defined(CONFIG_DEBUG_VM_MAPLE_TREE)
84249b1b8d6SLorenzo Stoakes 	/* Make sure no VMAs are about to be lost. */
84349b1b8d6SLorenzo Stoakes 	{
8446898c903SLiam R. Howlett 		MA_STATE(test, mas_detach->tree, 0, 0);
84549b1b8d6SLorenzo Stoakes 		struct vm_area_struct *vma_mas, *vma_test;
84649b1b8d6SLorenzo Stoakes 		int test_count = 0;
84749b1b8d6SLorenzo Stoakes 
848dba14840SLiam R. Howlett 		vma_iter_set(vms->vmi, vms->start);
84949b1b8d6SLorenzo Stoakes 		rcu_read_lock();
850dba14840SLiam R. Howlett 		vma_test = mas_find(&test, vms->vma_count - 1);
851dba14840SLiam R. Howlett 		for_each_vma_range(*(vms->vmi), vma_mas, vms->end) {
85249b1b8d6SLorenzo Stoakes 			BUG_ON(vma_mas != vma_test);
85349b1b8d6SLorenzo Stoakes 			test_count++;
854dba14840SLiam R. Howlett 			vma_test = mas_next(&test, vms->vma_count - 1);
85549b1b8d6SLorenzo Stoakes 		}
85649b1b8d6SLorenzo Stoakes 		rcu_read_unlock();
857dba14840SLiam R. Howlett 		BUG_ON(vms->vma_count != test_count);
85849b1b8d6SLorenzo Stoakes 	}
85949b1b8d6SLorenzo Stoakes #endif
86049b1b8d6SLorenzo Stoakes 
861dba14840SLiam R. Howlett 	while (vma_iter_addr(vms->vmi) > vms->start)
862dba14840SLiam R. Howlett 		vma_iter_prev_range(vms->vmi);
86349b1b8d6SLorenzo Stoakes 
864f8d112a4SLiam R. Howlett 	vms->clear_ptes = true;
8656898c903SLiam R. Howlett 	return 0;
8666898c903SLiam R. Howlett 
8676898c903SLiam R. Howlett userfaultfd_error:
8686898c903SLiam R. Howlett munmap_gather_failed:
8696898c903SLiam R. Howlett end_split_failed:
8706898c903SLiam R. Howlett modify_vma_failed:
8714f87153eSLiam R. Howlett 	reattach_vmas(mas_detach);
8726898c903SLiam R. Howlett start_split_failed:
8736898c903SLiam R. Howlett map_count_exceeded:
8746898c903SLiam R. Howlett 	return error;
8756898c903SLiam R. Howlett }
8766898c903SLiam R. Howlett 
8776898c903SLiam R. Howlett /*
8786898c903SLiam R. Howlett  * do_vmi_align_munmap() - munmap the aligned region from @start to @end.
8796898c903SLiam R. Howlett  * @vmi: The vma iterator
8806898c903SLiam R. Howlett  * @vma: The starting vm_area_struct
8816898c903SLiam R. Howlett  * @mm: The mm_struct
8826898c903SLiam R. Howlett  * @start: The aligned start address to munmap.
8836898c903SLiam R. Howlett  * @end: The aligned end address to munmap.
8846898c903SLiam R. Howlett  * @uf: The userfaultfd list_head
8856898c903SLiam R. Howlett  * @unlock: Set to true to drop the mmap_lock.  unlocking only happens on
8866898c903SLiam R. Howlett  * success.
8876898c903SLiam R. Howlett  *
8886898c903SLiam R. Howlett  * Return: 0 on success and drops the lock if so directed, error and leaves the
8896898c903SLiam R. Howlett  * lock held otherwise.
8906898c903SLiam R. Howlett  */
8916898c903SLiam R. Howlett int do_vmi_align_munmap(struct vma_iterator *vmi, struct vm_area_struct *vma,
8926898c903SLiam R. Howlett 		struct mm_struct *mm, unsigned long start, unsigned long end,
8936898c903SLiam R. Howlett 		struct list_head *uf, bool unlock)
8946898c903SLiam R. Howlett {
8956898c903SLiam R. Howlett 	struct maple_tree mt_detach;
8966898c903SLiam R. Howlett 	MA_STATE(mas_detach, &mt_detach, 0, 0);
8976898c903SLiam R. Howlett 	mt_init_flags(&mt_detach, vmi->mas.tree->ma_flags & MT_FLAGS_LOCK_MASK);
8986898c903SLiam R. Howlett 	mt_on_stack(mt_detach);
899dba14840SLiam R. Howlett 	struct vma_munmap_struct vms;
9006898c903SLiam R. Howlett 	int error;
9016898c903SLiam R. Howlett 
902dba14840SLiam R. Howlett 	init_vma_munmap(&vms, vmi, vma, start, end, uf, unlock);
903dba14840SLiam R. Howlett 	error = vms_gather_munmap_vmas(&vms, &mas_detach);
9046898c903SLiam R. Howlett 	if (error)
9056898c903SLiam R. Howlett 		goto gather_failed;
9066898c903SLiam R. Howlett 
90749b1b8d6SLorenzo Stoakes 	error = vma_iter_clear_gfp(vmi, start, end, GFP_KERNEL);
90849b1b8d6SLorenzo Stoakes 	if (error)
90949b1b8d6SLorenzo Stoakes 		goto clear_tree_failed;
91049b1b8d6SLorenzo Stoakes 
91149b1b8d6SLorenzo Stoakes 	/* Point of no return */
912dba14840SLiam R. Howlett 	vms_complete_munmap_vmas(&vms, &mas_detach);
91349b1b8d6SLorenzo Stoakes 	return 0;
91449b1b8d6SLorenzo Stoakes 
91549b1b8d6SLorenzo Stoakes clear_tree_failed:
9164f87153eSLiam R. Howlett 	reattach_vmas(&mas_detach);
9176898c903SLiam R. Howlett gather_failed:
91849b1b8d6SLorenzo Stoakes 	validate_mm(mm);
91949b1b8d6SLorenzo Stoakes 	return error;
92049b1b8d6SLorenzo Stoakes }
92149b1b8d6SLorenzo Stoakes 
92249b1b8d6SLorenzo Stoakes /*
92349b1b8d6SLorenzo Stoakes  * do_vmi_munmap() - munmap a given range.
92449b1b8d6SLorenzo Stoakes  * @vmi: The vma iterator
92549b1b8d6SLorenzo Stoakes  * @mm: The mm_struct
92649b1b8d6SLorenzo Stoakes  * @start: The start address to munmap
92749b1b8d6SLorenzo Stoakes  * @len: The length of the range to munmap
92849b1b8d6SLorenzo Stoakes  * @uf: The userfaultfd list_head
92949b1b8d6SLorenzo Stoakes  * @unlock: set to true if the user wants to drop the mmap_lock on success
93049b1b8d6SLorenzo Stoakes  *
93149b1b8d6SLorenzo Stoakes  * This function takes a @mas that is either pointing to the previous VMA or set
93249b1b8d6SLorenzo Stoakes  * to MA_START and sets it up to remove the mapping(s).  The @len will be
93340b88644SMichael Ellerman  * aligned.
93449b1b8d6SLorenzo Stoakes  *
93549b1b8d6SLorenzo Stoakes  * Return: 0 on success and drops the lock if so directed, error and leaves the
93649b1b8d6SLorenzo Stoakes  * lock held otherwise.
93749b1b8d6SLorenzo Stoakes  */
93849b1b8d6SLorenzo Stoakes int do_vmi_munmap(struct vma_iterator *vmi, struct mm_struct *mm,
93949b1b8d6SLorenzo Stoakes 		  unsigned long start, size_t len, struct list_head *uf,
94049b1b8d6SLorenzo Stoakes 		  bool unlock)
94149b1b8d6SLorenzo Stoakes {
94249b1b8d6SLorenzo Stoakes 	unsigned long end;
94349b1b8d6SLorenzo Stoakes 	struct vm_area_struct *vma;
94449b1b8d6SLorenzo Stoakes 
94549b1b8d6SLorenzo Stoakes 	if ((offset_in_page(start)) || start > TASK_SIZE || len > TASK_SIZE-start)
94649b1b8d6SLorenzo Stoakes 		return -EINVAL;
94749b1b8d6SLorenzo Stoakes 
94849b1b8d6SLorenzo Stoakes 	end = start + PAGE_ALIGN(len);
94949b1b8d6SLorenzo Stoakes 	if (end == start)
95049b1b8d6SLorenzo Stoakes 		return -EINVAL;
95149b1b8d6SLorenzo Stoakes 
95249b1b8d6SLorenzo Stoakes 	/* Find the first overlapping VMA */
95349b1b8d6SLorenzo Stoakes 	vma = vma_find(vmi, end);
95449b1b8d6SLorenzo Stoakes 	if (!vma) {
95549b1b8d6SLorenzo Stoakes 		if (unlock)
95649b1b8d6SLorenzo Stoakes 			mmap_write_unlock(mm);
95749b1b8d6SLorenzo Stoakes 		return 0;
95849b1b8d6SLorenzo Stoakes 	}
95949b1b8d6SLorenzo Stoakes 
96049b1b8d6SLorenzo Stoakes 	return do_vmi_align_munmap(vmi, vma, mm, start, end, uf, unlock);
96149b1b8d6SLorenzo Stoakes }
96249b1b8d6SLorenzo Stoakes 
96349b1b8d6SLorenzo Stoakes /*
96449b1b8d6SLorenzo Stoakes  * Given a mapping request (addr,end,vm_flags,file,pgoff,anon_name),
96549b1b8d6SLorenzo Stoakes  * figure out whether that can be merged with its predecessor or its
96649b1b8d6SLorenzo Stoakes  * successor.  Or both (it neatly fills a hole).
96749b1b8d6SLorenzo Stoakes  *
96849b1b8d6SLorenzo Stoakes  * In most cases - when called for mmap, brk or mremap - [addr,end) is
96949b1b8d6SLorenzo Stoakes  * certain not to be mapped by the time vma_merge is called; but when
97049b1b8d6SLorenzo Stoakes  * called for mprotect, it is certain to be already mapped (either at
97149b1b8d6SLorenzo Stoakes  * an offset within prev, or at the start of next), and the flags of
97249b1b8d6SLorenzo Stoakes  * this area are about to be changed to vm_flags - and the no-change
97349b1b8d6SLorenzo Stoakes  * case has already been eliminated.
97449b1b8d6SLorenzo Stoakes  *
97549b1b8d6SLorenzo Stoakes  * The following mprotect cases have to be considered, where **** is
97649b1b8d6SLorenzo Stoakes  * the area passed down from mprotect_fixup, never extending beyond one
97749b1b8d6SLorenzo Stoakes  * vma, PPPP is the previous vma, CCCC is a concurrent vma that starts
97849b1b8d6SLorenzo Stoakes  * at the same address as **** and is of the same or larger span, and
97949b1b8d6SLorenzo Stoakes  * NNNN the next vma after ****:
98049b1b8d6SLorenzo Stoakes  *
98149b1b8d6SLorenzo Stoakes  *     ****             ****                   ****
98249b1b8d6SLorenzo Stoakes  *    PPPPPPNNNNNN    PPPPPPNNNNNN       PPPPPPCCCCCC
98349b1b8d6SLorenzo Stoakes  *    cannot merge    might become       might become
98449b1b8d6SLorenzo Stoakes  *                    PPNNNNNNNNNN       PPPPPPPPPPCC
98549b1b8d6SLorenzo Stoakes  *    mmap, brk or    case 4 below       case 5 below
98649b1b8d6SLorenzo Stoakes  *    mremap move:
98749b1b8d6SLorenzo Stoakes  *                        ****               ****
98849b1b8d6SLorenzo Stoakes  *                    PPPP    NNNN       PPPPCCCCNNNN
98949b1b8d6SLorenzo Stoakes  *                    might become       might become
99049b1b8d6SLorenzo Stoakes  *                    PPPPPPPPPPPP 1 or  PPPPPPPPPPPP 6 or
99149b1b8d6SLorenzo Stoakes  *                    PPPPPPPPNNNN 2 or  PPPPPPPPNNNN 7 or
99249b1b8d6SLorenzo Stoakes  *                    PPPPNNNNNNNN 3     PPPPNNNNNNNN 8
99349b1b8d6SLorenzo Stoakes  *
99449b1b8d6SLorenzo Stoakes  * It is important for case 8 that the vma CCCC overlapping the
99549b1b8d6SLorenzo Stoakes  * region **** is never going to extended over NNNN. Instead NNNN must
99649b1b8d6SLorenzo Stoakes  * be extended in region **** and CCCC must be removed. This way in
99749b1b8d6SLorenzo Stoakes  * all cases where vma_merge succeeds, the moment vma_merge drops the
99849b1b8d6SLorenzo Stoakes  * rmap_locks, the properties of the merged vma will be already
99949b1b8d6SLorenzo Stoakes  * correct for the whole merged range. Some of those properties like
100049b1b8d6SLorenzo Stoakes  * vm_page_prot/vm_flags may be accessed by rmap_walks and they must
100149b1b8d6SLorenzo Stoakes  * be correct for the whole merged range immediately after the
100249b1b8d6SLorenzo Stoakes  * rmap_locks are released. Otherwise if NNNN would be removed and
100349b1b8d6SLorenzo Stoakes  * CCCC would be extended over the NNNN range, remove_migration_ptes
100449b1b8d6SLorenzo Stoakes  * or other rmap walkers (if working on addresses beyond the "end"
100549b1b8d6SLorenzo Stoakes  * parameter) may establish ptes with the wrong permissions of CCCC
100649b1b8d6SLorenzo Stoakes  * instead of the right permissions of NNNN.
100749b1b8d6SLorenzo Stoakes  *
100849b1b8d6SLorenzo Stoakes  * In the code below:
100949b1b8d6SLorenzo Stoakes  * PPPP is represented by *prev
101049b1b8d6SLorenzo Stoakes  * CCCC is represented by *curr or not represented at all (NULL)
101149b1b8d6SLorenzo Stoakes  * NNNN is represented by *next or not represented at all (NULL)
101249b1b8d6SLorenzo Stoakes  * **** is not represented - it will be merged and the vma containing the
101349b1b8d6SLorenzo Stoakes  *      area is returned, or the function will return NULL
101449b1b8d6SLorenzo Stoakes  */
1015*2f1c6611SLorenzo Stoakes static struct vm_area_struct *vma_merge(struct vma_merge_struct *vmg)
101649b1b8d6SLorenzo Stoakes {
1017*2f1c6611SLorenzo Stoakes 	struct mm_struct *mm = vmg->mm;
1018*2f1c6611SLorenzo Stoakes 	struct vm_area_struct *prev = vmg->prev;
101949b1b8d6SLorenzo Stoakes 	struct vm_area_struct *curr, *next, *res;
102049b1b8d6SLorenzo Stoakes 	struct vm_area_struct *vma, *adjust, *remove, *remove2;
102149b1b8d6SLorenzo Stoakes 	struct vm_area_struct *anon_dup = NULL;
102249b1b8d6SLorenzo Stoakes 	struct vma_prepare vp;
102349b1b8d6SLorenzo Stoakes 	pgoff_t vma_pgoff;
102449b1b8d6SLorenzo Stoakes 	int err = 0;
102549b1b8d6SLorenzo Stoakes 	bool merge_prev = false;
102649b1b8d6SLorenzo Stoakes 	bool merge_next = false;
102749b1b8d6SLorenzo Stoakes 	bool vma_expanded = false;
1028*2f1c6611SLorenzo Stoakes 	unsigned long addr = vmg->start;
1029*2f1c6611SLorenzo Stoakes 	unsigned long end = vmg->end;
103049b1b8d6SLorenzo Stoakes 	unsigned long vma_start = addr;
103149b1b8d6SLorenzo Stoakes 	unsigned long vma_end = end;
1032*2f1c6611SLorenzo Stoakes 	pgoff_t pglen = PHYS_PFN(end - addr);
103349b1b8d6SLorenzo Stoakes 	long adj_start = 0;
103449b1b8d6SLorenzo Stoakes 
103549b1b8d6SLorenzo Stoakes 	/*
103649b1b8d6SLorenzo Stoakes 	 * We later require that vma->vm_flags == vm_flags,
103749b1b8d6SLorenzo Stoakes 	 * so this tests vma->vm_flags & VM_SPECIAL, too.
103849b1b8d6SLorenzo Stoakes 	 */
1039*2f1c6611SLorenzo Stoakes 	if (vmg->flags & VM_SPECIAL)
104049b1b8d6SLorenzo Stoakes 		return NULL;
104149b1b8d6SLorenzo Stoakes 
104249b1b8d6SLorenzo Stoakes 	/* Does the input range span an existing VMA? (cases 5 - 8) */
104349b1b8d6SLorenzo Stoakes 	curr = find_vma_intersection(mm, prev ? prev->vm_end : 0, end);
104449b1b8d6SLorenzo Stoakes 
104549b1b8d6SLorenzo Stoakes 	if (!curr ||			/* cases 1 - 4 */
104649b1b8d6SLorenzo Stoakes 	    end == curr->vm_end)	/* cases 6 - 8, adjacent VMA */
1047*2f1c6611SLorenzo Stoakes 		next = vmg->next = vma_lookup(mm, end);
104849b1b8d6SLorenzo Stoakes 	else
1049*2f1c6611SLorenzo Stoakes 		next = vmg->next = NULL;	/* case 5 */
105049b1b8d6SLorenzo Stoakes 
105149b1b8d6SLorenzo Stoakes 	if (prev) {
105249b1b8d6SLorenzo Stoakes 		vma_start = prev->vm_start;
105349b1b8d6SLorenzo Stoakes 		vma_pgoff = prev->vm_pgoff;
105449b1b8d6SLorenzo Stoakes 
105549b1b8d6SLorenzo Stoakes 		/* Can we merge the predecessor? */
1056*2f1c6611SLorenzo Stoakes 		if (addr == prev->vm_end && mpol_equal(vma_policy(prev), vmg->policy)
1057*2f1c6611SLorenzo Stoakes 		    && can_vma_merge_after(vmg)) {
1058*2f1c6611SLorenzo Stoakes 
105949b1b8d6SLorenzo Stoakes 			merge_prev = true;
1060*2f1c6611SLorenzo Stoakes 			vma_prev(vmg->vmi);
106149b1b8d6SLorenzo Stoakes 		}
106249b1b8d6SLorenzo Stoakes 	}
106349b1b8d6SLorenzo Stoakes 
106449b1b8d6SLorenzo Stoakes 	/* Can we merge the successor? */
1065*2f1c6611SLorenzo Stoakes 	if (next && mpol_equal(vmg->policy, vma_policy(next)) &&
1066*2f1c6611SLorenzo Stoakes 	    can_vma_merge_before(vmg)) {
106749b1b8d6SLorenzo Stoakes 		merge_next = true;
106849b1b8d6SLorenzo Stoakes 	}
106949b1b8d6SLorenzo Stoakes 
107049b1b8d6SLorenzo Stoakes 	/* Verify some invariant that must be enforced by the caller. */
107149b1b8d6SLorenzo Stoakes 	VM_WARN_ON(prev && addr <= prev->vm_start);
107249b1b8d6SLorenzo Stoakes 	VM_WARN_ON(curr && (addr != curr->vm_start || end > curr->vm_end));
107349b1b8d6SLorenzo Stoakes 	VM_WARN_ON(addr >= end);
107449b1b8d6SLorenzo Stoakes 
107549b1b8d6SLorenzo Stoakes 	if (!merge_prev && !merge_next)
107649b1b8d6SLorenzo Stoakes 		return NULL; /* Not mergeable. */
107749b1b8d6SLorenzo Stoakes 
107849b1b8d6SLorenzo Stoakes 	if (merge_prev)
107949b1b8d6SLorenzo Stoakes 		vma_start_write(prev);
108049b1b8d6SLorenzo Stoakes 
108149b1b8d6SLorenzo Stoakes 	res = vma = prev;
108249b1b8d6SLorenzo Stoakes 	remove = remove2 = adjust = NULL;
108349b1b8d6SLorenzo Stoakes 
108449b1b8d6SLorenzo Stoakes 	/* Can we merge both the predecessor and the successor? */
108549b1b8d6SLorenzo Stoakes 	if (merge_prev && merge_next &&
108649b1b8d6SLorenzo Stoakes 	    is_mergeable_anon_vma(prev->anon_vma, next->anon_vma, NULL)) {
108749b1b8d6SLorenzo Stoakes 		vma_start_write(next);
108849b1b8d6SLorenzo Stoakes 		remove = next;				/* case 1 */
108949b1b8d6SLorenzo Stoakes 		vma_end = next->vm_end;
109049b1b8d6SLorenzo Stoakes 		err = dup_anon_vma(prev, next, &anon_dup);
109149b1b8d6SLorenzo Stoakes 		if (curr) {				/* case 6 */
109249b1b8d6SLorenzo Stoakes 			vma_start_write(curr);
109349b1b8d6SLorenzo Stoakes 			remove = curr;
109449b1b8d6SLorenzo Stoakes 			remove2 = next;
109549b1b8d6SLorenzo Stoakes 			/*
109649b1b8d6SLorenzo Stoakes 			 * Note that the dup_anon_vma below cannot overwrite err
109749b1b8d6SLorenzo Stoakes 			 * since the first caller would do nothing unless next
109849b1b8d6SLorenzo Stoakes 			 * has an anon_vma.
109949b1b8d6SLorenzo Stoakes 			 */
110049b1b8d6SLorenzo Stoakes 			if (!next->anon_vma)
110149b1b8d6SLorenzo Stoakes 				err = dup_anon_vma(prev, curr, &anon_dup);
110249b1b8d6SLorenzo Stoakes 		}
110349b1b8d6SLorenzo Stoakes 	} else if (merge_prev) {			/* case 2 */
110449b1b8d6SLorenzo Stoakes 		if (curr) {
110549b1b8d6SLorenzo Stoakes 			vma_start_write(curr);
110649b1b8d6SLorenzo Stoakes 			if (end == curr->vm_end) {	/* case 7 */
110749b1b8d6SLorenzo Stoakes 				/*
110849b1b8d6SLorenzo Stoakes 				 * can_vma_merge_after() assumed we would not be
110949b1b8d6SLorenzo Stoakes 				 * removing prev vma, so it skipped the check
111049b1b8d6SLorenzo Stoakes 				 * for vm_ops->close, but we are removing curr
111149b1b8d6SLorenzo Stoakes 				 */
111249b1b8d6SLorenzo Stoakes 				if (curr->vm_ops && curr->vm_ops->close)
111349b1b8d6SLorenzo Stoakes 					err = -EINVAL;
111449b1b8d6SLorenzo Stoakes 				remove = curr;
111549b1b8d6SLorenzo Stoakes 			} else {			/* case 5 */
111649b1b8d6SLorenzo Stoakes 				adjust = curr;
111749b1b8d6SLorenzo Stoakes 				adj_start = (end - curr->vm_start);
111849b1b8d6SLorenzo Stoakes 			}
111949b1b8d6SLorenzo Stoakes 			if (!err)
112049b1b8d6SLorenzo Stoakes 				err = dup_anon_vma(prev, curr, &anon_dup);
112149b1b8d6SLorenzo Stoakes 		}
112249b1b8d6SLorenzo Stoakes 	} else { /* merge_next */
112349b1b8d6SLorenzo Stoakes 		vma_start_write(next);
112449b1b8d6SLorenzo Stoakes 		res = next;
112549b1b8d6SLorenzo Stoakes 		if (prev && addr < prev->vm_end) {	/* case 4 */
112649b1b8d6SLorenzo Stoakes 			vma_start_write(prev);
112749b1b8d6SLorenzo Stoakes 			vma_end = addr;
112849b1b8d6SLorenzo Stoakes 			adjust = next;
112949b1b8d6SLorenzo Stoakes 			adj_start = -(prev->vm_end - addr);
113049b1b8d6SLorenzo Stoakes 			err = dup_anon_vma(next, prev, &anon_dup);
113149b1b8d6SLorenzo Stoakes 		} else {
113249b1b8d6SLorenzo Stoakes 			/*
113349b1b8d6SLorenzo Stoakes 			 * Note that cases 3 and 8 are the ONLY ones where prev
113449b1b8d6SLorenzo Stoakes 			 * is permitted to be (but is not necessarily) NULL.
113549b1b8d6SLorenzo Stoakes 			 */
113649b1b8d6SLorenzo Stoakes 			vma = next;			/* case 3 */
113749b1b8d6SLorenzo Stoakes 			vma_start = addr;
113849b1b8d6SLorenzo Stoakes 			vma_end = next->vm_end;
113949b1b8d6SLorenzo Stoakes 			vma_pgoff = next->vm_pgoff - pglen;
114049b1b8d6SLorenzo Stoakes 			if (curr) {			/* case 8 */
114149b1b8d6SLorenzo Stoakes 				vma_pgoff = curr->vm_pgoff;
114249b1b8d6SLorenzo Stoakes 				vma_start_write(curr);
114349b1b8d6SLorenzo Stoakes 				remove = curr;
114449b1b8d6SLorenzo Stoakes 				err = dup_anon_vma(next, curr, &anon_dup);
114549b1b8d6SLorenzo Stoakes 			}
114649b1b8d6SLorenzo Stoakes 		}
114749b1b8d6SLorenzo Stoakes 	}
114849b1b8d6SLorenzo Stoakes 
114949b1b8d6SLorenzo Stoakes 	/* Error in anon_vma clone. */
115049b1b8d6SLorenzo Stoakes 	if (err)
115149b1b8d6SLorenzo Stoakes 		goto anon_vma_fail;
115249b1b8d6SLorenzo Stoakes 
115349b1b8d6SLorenzo Stoakes 	if (vma_start < vma->vm_start || vma_end > vma->vm_end)
115449b1b8d6SLorenzo Stoakes 		vma_expanded = true;
115549b1b8d6SLorenzo Stoakes 
115649b1b8d6SLorenzo Stoakes 	if (vma_expanded) {
1157*2f1c6611SLorenzo Stoakes 		vma_iter_config(vmg->vmi, vma_start, vma_end);
115849b1b8d6SLorenzo Stoakes 	} else {
1159*2f1c6611SLorenzo Stoakes 		vma_iter_config(vmg->vmi, adjust->vm_start + adj_start,
116049b1b8d6SLorenzo Stoakes 				adjust->vm_end);
116149b1b8d6SLorenzo Stoakes 	}
116249b1b8d6SLorenzo Stoakes 
1163*2f1c6611SLorenzo Stoakes 	if (vma_iter_prealloc(vmg->vmi, vma))
116449b1b8d6SLorenzo Stoakes 		goto prealloc_fail;
116549b1b8d6SLorenzo Stoakes 
116649b1b8d6SLorenzo Stoakes 	init_multi_vma_prep(&vp, vma, adjust, remove, remove2);
116749b1b8d6SLorenzo Stoakes 	VM_WARN_ON(vp.anon_vma && adjust && adjust->anon_vma &&
116849b1b8d6SLorenzo Stoakes 		   vp.anon_vma != adjust->anon_vma);
116949b1b8d6SLorenzo Stoakes 
117049b1b8d6SLorenzo Stoakes 	vma_prepare(&vp);
117149b1b8d6SLorenzo Stoakes 	vma_adjust_trans_huge(vma, vma_start, vma_end, adj_start);
117249b1b8d6SLorenzo Stoakes 	vma_set_range(vma, vma_start, vma_end, vma_pgoff);
117349b1b8d6SLorenzo Stoakes 
117449b1b8d6SLorenzo Stoakes 	if (vma_expanded)
1175*2f1c6611SLorenzo Stoakes 		vma_iter_store(vmg->vmi, vma);
117649b1b8d6SLorenzo Stoakes 
117749b1b8d6SLorenzo Stoakes 	if (adj_start) {
117849b1b8d6SLorenzo Stoakes 		adjust->vm_start += adj_start;
117949b1b8d6SLorenzo Stoakes 		adjust->vm_pgoff += adj_start >> PAGE_SHIFT;
118049b1b8d6SLorenzo Stoakes 		if (adj_start < 0) {
118149b1b8d6SLorenzo Stoakes 			WARN_ON(vma_expanded);
1182*2f1c6611SLorenzo Stoakes 			vma_iter_store(vmg->vmi, next);
118349b1b8d6SLorenzo Stoakes 		}
118449b1b8d6SLorenzo Stoakes 	}
118549b1b8d6SLorenzo Stoakes 
1186*2f1c6611SLorenzo Stoakes 	vma_complete(&vp, vmg->vmi, mm);
118789b2d2a5SLiam R. Howlett 	validate_mm(mm);
1188*2f1c6611SLorenzo Stoakes 	khugepaged_enter_vma(res, vmg->flags);
118949b1b8d6SLorenzo Stoakes 	return res;
119049b1b8d6SLorenzo Stoakes 
119149b1b8d6SLorenzo Stoakes prealloc_fail:
119249b1b8d6SLorenzo Stoakes 	if (anon_dup)
119349b1b8d6SLorenzo Stoakes 		unlink_anon_vmas(anon_dup);
119449b1b8d6SLorenzo Stoakes 
119549b1b8d6SLorenzo Stoakes anon_vma_fail:
1196*2f1c6611SLorenzo Stoakes 	vma_iter_set(vmg->vmi, addr);
1197*2f1c6611SLorenzo Stoakes 	vma_iter_load(vmg->vmi);
119849b1b8d6SLorenzo Stoakes 	return NULL;
119949b1b8d6SLorenzo Stoakes }
120049b1b8d6SLorenzo Stoakes 
120149b1b8d6SLorenzo Stoakes /*
120249b1b8d6SLorenzo Stoakes  * We are about to modify one or multiple of a VMA's flags, policy, userfaultfd
120349b1b8d6SLorenzo Stoakes  * context and anonymous VMA name within the range [start, end).
120449b1b8d6SLorenzo Stoakes  *
120549b1b8d6SLorenzo Stoakes  * As a result, we might be able to merge the newly modified VMA range with an
120649b1b8d6SLorenzo Stoakes  * adjacent VMA with identical properties.
120749b1b8d6SLorenzo Stoakes  *
120849b1b8d6SLorenzo Stoakes  * If no merge is possible and the range does not span the entirety of the VMA,
120949b1b8d6SLorenzo Stoakes  * we then need to split the VMA to accommodate the change.
121049b1b8d6SLorenzo Stoakes  *
121149b1b8d6SLorenzo Stoakes  * The function returns either the merged VMA, the original VMA if a split was
121249b1b8d6SLorenzo Stoakes  * required instead, or an error if the split failed.
121349b1b8d6SLorenzo Stoakes  */
1214*2f1c6611SLorenzo Stoakes static struct vm_area_struct *vma_modify(struct vma_merge_struct *vmg)
121549b1b8d6SLorenzo Stoakes {
1216*2f1c6611SLorenzo Stoakes 	struct vm_area_struct *vma = vmg->vma;
121749b1b8d6SLorenzo Stoakes 	struct vm_area_struct *merged;
121849b1b8d6SLorenzo Stoakes 
1219*2f1c6611SLorenzo Stoakes 	/* First, try to merge. */
1220*2f1c6611SLorenzo Stoakes 	merged = vma_merge(vmg);
122149b1b8d6SLorenzo Stoakes 	if (merged)
122249b1b8d6SLorenzo Stoakes 		return merged;
122349b1b8d6SLorenzo Stoakes 
1224*2f1c6611SLorenzo Stoakes 	/* Split any preceding portion of the VMA. */
1225*2f1c6611SLorenzo Stoakes 	if (vma->vm_start < vmg->start) {
1226*2f1c6611SLorenzo Stoakes 		int err = split_vma(vmg->vmi, vma, vmg->start, 1);
122749b1b8d6SLorenzo Stoakes 
122849b1b8d6SLorenzo Stoakes 		if (err)
122949b1b8d6SLorenzo Stoakes 			return ERR_PTR(err);
123049b1b8d6SLorenzo Stoakes 	}
123149b1b8d6SLorenzo Stoakes 
1232*2f1c6611SLorenzo Stoakes 	/* Split any trailing portion of the VMA. */
1233*2f1c6611SLorenzo Stoakes 	if (vma->vm_end > vmg->end) {
1234*2f1c6611SLorenzo Stoakes 		int err = split_vma(vmg->vmi, vma, vmg->end, 0);
123549b1b8d6SLorenzo Stoakes 
123649b1b8d6SLorenzo Stoakes 		if (err)
123749b1b8d6SLorenzo Stoakes 			return ERR_PTR(err);
123849b1b8d6SLorenzo Stoakes 	}
123949b1b8d6SLorenzo Stoakes 
124049b1b8d6SLorenzo Stoakes 	return vma;
124149b1b8d6SLorenzo Stoakes }
124249b1b8d6SLorenzo Stoakes 
1243*2f1c6611SLorenzo Stoakes struct vm_area_struct *vma_modify_flags(
1244*2f1c6611SLorenzo Stoakes 	struct vma_iterator *vmi, struct vm_area_struct *prev,
1245*2f1c6611SLorenzo Stoakes 	struct vm_area_struct *vma, unsigned long start, unsigned long end,
1246*2f1c6611SLorenzo Stoakes 	unsigned long new_flags)
1247*2f1c6611SLorenzo Stoakes {
1248*2f1c6611SLorenzo Stoakes 	VMG_VMA_STATE(vmg, vmi, prev, vma, start, end);
1249*2f1c6611SLorenzo Stoakes 
1250*2f1c6611SLorenzo Stoakes 	vmg.flags = new_flags;
1251*2f1c6611SLorenzo Stoakes 
1252*2f1c6611SLorenzo Stoakes 	return vma_modify(&vmg);
1253*2f1c6611SLorenzo Stoakes }
1254*2f1c6611SLorenzo Stoakes 
1255*2f1c6611SLorenzo Stoakes struct vm_area_struct
1256*2f1c6611SLorenzo Stoakes *vma_modify_flags_name(struct vma_iterator *vmi,
1257*2f1c6611SLorenzo Stoakes 		       struct vm_area_struct *prev,
1258*2f1c6611SLorenzo Stoakes 		       struct vm_area_struct *vma,
1259*2f1c6611SLorenzo Stoakes 		       unsigned long start,
1260*2f1c6611SLorenzo Stoakes 		       unsigned long end,
1261*2f1c6611SLorenzo Stoakes 		       unsigned long new_flags,
1262*2f1c6611SLorenzo Stoakes 		       struct anon_vma_name *new_name)
1263*2f1c6611SLorenzo Stoakes {
1264*2f1c6611SLorenzo Stoakes 	VMG_VMA_STATE(vmg, vmi, prev, vma, start, end);
1265*2f1c6611SLorenzo Stoakes 
1266*2f1c6611SLorenzo Stoakes 	vmg.flags = new_flags;
1267*2f1c6611SLorenzo Stoakes 	vmg.anon_name = new_name;
1268*2f1c6611SLorenzo Stoakes 
1269*2f1c6611SLorenzo Stoakes 	return vma_modify(&vmg);
1270*2f1c6611SLorenzo Stoakes }
1271*2f1c6611SLorenzo Stoakes 
1272*2f1c6611SLorenzo Stoakes struct vm_area_struct
1273*2f1c6611SLorenzo Stoakes *vma_modify_policy(struct vma_iterator *vmi,
1274*2f1c6611SLorenzo Stoakes 		   struct vm_area_struct *prev,
1275*2f1c6611SLorenzo Stoakes 		   struct vm_area_struct *vma,
1276*2f1c6611SLorenzo Stoakes 		   unsigned long start, unsigned long end,
1277*2f1c6611SLorenzo Stoakes 		   struct mempolicy *new_pol)
1278*2f1c6611SLorenzo Stoakes {
1279*2f1c6611SLorenzo Stoakes 	VMG_VMA_STATE(vmg, vmi, prev, vma, start, end);
1280*2f1c6611SLorenzo Stoakes 
1281*2f1c6611SLorenzo Stoakes 	vmg.policy = new_pol;
1282*2f1c6611SLorenzo Stoakes 
1283*2f1c6611SLorenzo Stoakes 	return vma_modify(&vmg);
1284*2f1c6611SLorenzo Stoakes }
1285*2f1c6611SLorenzo Stoakes 
1286*2f1c6611SLorenzo Stoakes struct vm_area_struct
1287*2f1c6611SLorenzo Stoakes *vma_modify_flags_uffd(struct vma_iterator *vmi,
1288*2f1c6611SLorenzo Stoakes 		       struct vm_area_struct *prev,
1289*2f1c6611SLorenzo Stoakes 		       struct vm_area_struct *vma,
1290*2f1c6611SLorenzo Stoakes 		       unsigned long start, unsigned long end,
1291*2f1c6611SLorenzo Stoakes 		       unsigned long new_flags,
1292*2f1c6611SLorenzo Stoakes 		       struct vm_userfaultfd_ctx new_ctx)
1293*2f1c6611SLorenzo Stoakes {
1294*2f1c6611SLorenzo Stoakes 	VMG_VMA_STATE(vmg, vmi, prev, vma, start, end);
1295*2f1c6611SLorenzo Stoakes 
1296*2f1c6611SLorenzo Stoakes 	vmg.flags = new_flags;
1297*2f1c6611SLorenzo Stoakes 	vmg.uffd_ctx = new_ctx;
1298*2f1c6611SLorenzo Stoakes 
1299*2f1c6611SLorenzo Stoakes 	return vma_modify(&vmg);
1300*2f1c6611SLorenzo Stoakes }
1301*2f1c6611SLorenzo Stoakes 
130249b1b8d6SLorenzo Stoakes /*
130349b1b8d6SLorenzo Stoakes  * Attempt to merge a newly mapped VMA with those adjacent to it. The caller
130449b1b8d6SLorenzo Stoakes  * must ensure that [start, end) does not overlap any existing VMA.
130549b1b8d6SLorenzo Stoakes  */
130649b1b8d6SLorenzo Stoakes struct vm_area_struct
130749b1b8d6SLorenzo Stoakes *vma_merge_new_vma(struct vma_iterator *vmi, struct vm_area_struct *prev,
130849b1b8d6SLorenzo Stoakes 		   struct vm_area_struct *vma, unsigned long start,
130949b1b8d6SLorenzo Stoakes 		   unsigned long end, pgoff_t pgoff)
131049b1b8d6SLorenzo Stoakes {
1311*2f1c6611SLorenzo Stoakes 	VMG_VMA_STATE(vmg, vmi, prev, vma, start, end);
1312*2f1c6611SLorenzo Stoakes 
1313*2f1c6611SLorenzo Stoakes 	vmg.pgoff = pgoff;
1314*2f1c6611SLorenzo Stoakes 
1315*2f1c6611SLorenzo Stoakes 	return vma_merge(&vmg);
131649b1b8d6SLorenzo Stoakes }
131749b1b8d6SLorenzo Stoakes 
131849b1b8d6SLorenzo Stoakes /*
131949b1b8d6SLorenzo Stoakes  * Expand vma by delta bytes, potentially merging with an immediately adjacent
132049b1b8d6SLorenzo Stoakes  * VMA with identical properties.
132149b1b8d6SLorenzo Stoakes  */
132249b1b8d6SLorenzo Stoakes struct vm_area_struct *vma_merge_extend(struct vma_iterator *vmi,
132349b1b8d6SLorenzo Stoakes 					struct vm_area_struct *vma,
132449b1b8d6SLorenzo Stoakes 					unsigned long delta)
132549b1b8d6SLorenzo Stoakes {
1326*2f1c6611SLorenzo Stoakes 	VMG_VMA_STATE(vmg, vmi, vma, vma, vma->vm_end, vma->vm_end + delta);
132749b1b8d6SLorenzo Stoakes 
132849b1b8d6SLorenzo Stoakes 	/* vma is specified as prev, so case 1 or 2 will apply. */
1329*2f1c6611SLorenzo Stoakes 	return vma_merge(&vmg);
133049b1b8d6SLorenzo Stoakes }
133149b1b8d6SLorenzo Stoakes 
133249b1b8d6SLorenzo Stoakes void unlink_file_vma_batch_init(struct unlink_vma_file_batch *vb)
133349b1b8d6SLorenzo Stoakes {
133449b1b8d6SLorenzo Stoakes 	vb->count = 0;
133549b1b8d6SLorenzo Stoakes }
133649b1b8d6SLorenzo Stoakes 
133749b1b8d6SLorenzo Stoakes static void unlink_file_vma_batch_process(struct unlink_vma_file_batch *vb)
133849b1b8d6SLorenzo Stoakes {
133949b1b8d6SLorenzo Stoakes 	struct address_space *mapping;
134049b1b8d6SLorenzo Stoakes 	int i;
134149b1b8d6SLorenzo Stoakes 
134249b1b8d6SLorenzo Stoakes 	mapping = vb->vmas[0]->vm_file->f_mapping;
134349b1b8d6SLorenzo Stoakes 	i_mmap_lock_write(mapping);
134449b1b8d6SLorenzo Stoakes 	for (i = 0; i < vb->count; i++) {
134549b1b8d6SLorenzo Stoakes 		VM_WARN_ON_ONCE(vb->vmas[i]->vm_file->f_mapping != mapping);
134649b1b8d6SLorenzo Stoakes 		__remove_shared_vm_struct(vb->vmas[i], mapping);
134749b1b8d6SLorenzo Stoakes 	}
134849b1b8d6SLorenzo Stoakes 	i_mmap_unlock_write(mapping);
134949b1b8d6SLorenzo Stoakes 
135049b1b8d6SLorenzo Stoakes 	unlink_file_vma_batch_init(vb);
135149b1b8d6SLorenzo Stoakes }
135249b1b8d6SLorenzo Stoakes 
135349b1b8d6SLorenzo Stoakes void unlink_file_vma_batch_add(struct unlink_vma_file_batch *vb,
135449b1b8d6SLorenzo Stoakes 			       struct vm_area_struct *vma)
135549b1b8d6SLorenzo Stoakes {
135649b1b8d6SLorenzo Stoakes 	if (vma->vm_file == NULL)
135749b1b8d6SLorenzo Stoakes 		return;
135849b1b8d6SLorenzo Stoakes 
135949b1b8d6SLorenzo Stoakes 	if ((vb->count > 0 && vb->vmas[0]->vm_file != vma->vm_file) ||
136049b1b8d6SLorenzo Stoakes 	    vb->count == ARRAY_SIZE(vb->vmas))
136149b1b8d6SLorenzo Stoakes 		unlink_file_vma_batch_process(vb);
136249b1b8d6SLorenzo Stoakes 
136349b1b8d6SLorenzo Stoakes 	vb->vmas[vb->count] = vma;
136449b1b8d6SLorenzo Stoakes 	vb->count++;
136549b1b8d6SLorenzo Stoakes }
136649b1b8d6SLorenzo Stoakes 
136749b1b8d6SLorenzo Stoakes void unlink_file_vma_batch_final(struct unlink_vma_file_batch *vb)
136849b1b8d6SLorenzo Stoakes {
136949b1b8d6SLorenzo Stoakes 	if (vb->count > 0)
137049b1b8d6SLorenzo Stoakes 		unlink_file_vma_batch_process(vb);
137149b1b8d6SLorenzo Stoakes }
137249b1b8d6SLorenzo Stoakes 
137349b1b8d6SLorenzo Stoakes /*
137449b1b8d6SLorenzo Stoakes  * Unlink a file-based vm structure from its interval tree, to hide
137549b1b8d6SLorenzo Stoakes  * vma from rmap and vmtruncate before freeing its page tables.
137649b1b8d6SLorenzo Stoakes  */
137749b1b8d6SLorenzo Stoakes void unlink_file_vma(struct vm_area_struct *vma)
137849b1b8d6SLorenzo Stoakes {
137949b1b8d6SLorenzo Stoakes 	struct file *file = vma->vm_file;
138049b1b8d6SLorenzo Stoakes 
138149b1b8d6SLorenzo Stoakes 	if (file) {
138249b1b8d6SLorenzo Stoakes 		struct address_space *mapping = file->f_mapping;
138349b1b8d6SLorenzo Stoakes 
138449b1b8d6SLorenzo Stoakes 		i_mmap_lock_write(mapping);
138549b1b8d6SLorenzo Stoakes 		__remove_shared_vm_struct(vma, mapping);
138649b1b8d6SLorenzo Stoakes 		i_mmap_unlock_write(mapping);
138749b1b8d6SLorenzo Stoakes 	}
138849b1b8d6SLorenzo Stoakes }
138949b1b8d6SLorenzo Stoakes 
139049b1b8d6SLorenzo Stoakes void vma_link_file(struct vm_area_struct *vma)
139149b1b8d6SLorenzo Stoakes {
139249b1b8d6SLorenzo Stoakes 	struct file *file = vma->vm_file;
139349b1b8d6SLorenzo Stoakes 	struct address_space *mapping;
139449b1b8d6SLorenzo Stoakes 
139549b1b8d6SLorenzo Stoakes 	if (file) {
139649b1b8d6SLorenzo Stoakes 		mapping = file->f_mapping;
139749b1b8d6SLorenzo Stoakes 		i_mmap_lock_write(mapping);
139849b1b8d6SLorenzo Stoakes 		__vma_link_file(vma, mapping);
139949b1b8d6SLorenzo Stoakes 		i_mmap_unlock_write(mapping);
140049b1b8d6SLorenzo Stoakes 	}
140149b1b8d6SLorenzo Stoakes }
140249b1b8d6SLorenzo Stoakes 
140349b1b8d6SLorenzo Stoakes int vma_link(struct mm_struct *mm, struct vm_area_struct *vma)
140449b1b8d6SLorenzo Stoakes {
140549b1b8d6SLorenzo Stoakes 	VMA_ITERATOR(vmi, mm, 0);
140649b1b8d6SLorenzo Stoakes 
140749b1b8d6SLorenzo Stoakes 	vma_iter_config(&vmi, vma->vm_start, vma->vm_end);
140849b1b8d6SLorenzo Stoakes 	if (vma_iter_prealloc(&vmi, vma))
140949b1b8d6SLorenzo Stoakes 		return -ENOMEM;
141049b1b8d6SLorenzo Stoakes 
141149b1b8d6SLorenzo Stoakes 	vma_start_write(vma);
141249b1b8d6SLorenzo Stoakes 	vma_iter_store(&vmi, vma);
141349b1b8d6SLorenzo Stoakes 	vma_link_file(vma);
141449b1b8d6SLorenzo Stoakes 	mm->map_count++;
141549b1b8d6SLorenzo Stoakes 	validate_mm(mm);
141649b1b8d6SLorenzo Stoakes 	return 0;
141749b1b8d6SLorenzo Stoakes }
141849b1b8d6SLorenzo Stoakes 
141949b1b8d6SLorenzo Stoakes /*
142049b1b8d6SLorenzo Stoakes  * Copy the vma structure to a new location in the same mm,
142149b1b8d6SLorenzo Stoakes  * prior to moving page table entries, to effect an mremap move.
142249b1b8d6SLorenzo Stoakes  */
142349b1b8d6SLorenzo Stoakes struct vm_area_struct *copy_vma(struct vm_area_struct **vmap,
142449b1b8d6SLorenzo Stoakes 	unsigned long addr, unsigned long len, pgoff_t pgoff,
142549b1b8d6SLorenzo Stoakes 	bool *need_rmap_locks)
142649b1b8d6SLorenzo Stoakes {
142749b1b8d6SLorenzo Stoakes 	struct vm_area_struct *vma = *vmap;
142849b1b8d6SLorenzo Stoakes 	unsigned long vma_start = vma->vm_start;
142949b1b8d6SLorenzo Stoakes 	struct mm_struct *mm = vma->vm_mm;
143049b1b8d6SLorenzo Stoakes 	struct vm_area_struct *new_vma, *prev;
143149b1b8d6SLorenzo Stoakes 	bool faulted_in_anon_vma = true;
143249b1b8d6SLorenzo Stoakes 	VMA_ITERATOR(vmi, mm, addr);
143349b1b8d6SLorenzo Stoakes 
143449b1b8d6SLorenzo Stoakes 	/*
143549b1b8d6SLorenzo Stoakes 	 * If anonymous vma has not yet been faulted, update new pgoff
143649b1b8d6SLorenzo Stoakes 	 * to match new location, to increase its chance of merging.
143749b1b8d6SLorenzo Stoakes 	 */
143849b1b8d6SLorenzo Stoakes 	if (unlikely(vma_is_anonymous(vma) && !vma->anon_vma)) {
143949b1b8d6SLorenzo Stoakes 		pgoff = addr >> PAGE_SHIFT;
144049b1b8d6SLorenzo Stoakes 		faulted_in_anon_vma = false;
144149b1b8d6SLorenzo Stoakes 	}
144249b1b8d6SLorenzo Stoakes 
144349b1b8d6SLorenzo Stoakes 	new_vma = find_vma_prev(mm, addr, &prev);
144449b1b8d6SLorenzo Stoakes 	if (new_vma && new_vma->vm_start < addr + len)
144549b1b8d6SLorenzo Stoakes 		return NULL;	/* should never get here */
144649b1b8d6SLorenzo Stoakes 
144749b1b8d6SLorenzo Stoakes 	new_vma = vma_merge_new_vma(&vmi, prev, vma, addr, addr + len, pgoff);
144849b1b8d6SLorenzo Stoakes 	if (new_vma) {
144949b1b8d6SLorenzo Stoakes 		/*
145049b1b8d6SLorenzo Stoakes 		 * Source vma may have been merged into new_vma
145149b1b8d6SLorenzo Stoakes 		 */
145249b1b8d6SLorenzo Stoakes 		if (unlikely(vma_start >= new_vma->vm_start &&
145349b1b8d6SLorenzo Stoakes 			     vma_start < new_vma->vm_end)) {
145449b1b8d6SLorenzo Stoakes 			/*
145549b1b8d6SLorenzo Stoakes 			 * The only way we can get a vma_merge with
145649b1b8d6SLorenzo Stoakes 			 * self during an mremap is if the vma hasn't
145749b1b8d6SLorenzo Stoakes 			 * been faulted in yet and we were allowed to
145849b1b8d6SLorenzo Stoakes 			 * reset the dst vma->vm_pgoff to the
145949b1b8d6SLorenzo Stoakes 			 * destination address of the mremap to allow
146049b1b8d6SLorenzo Stoakes 			 * the merge to happen. mremap must change the
146149b1b8d6SLorenzo Stoakes 			 * vm_pgoff linearity between src and dst vmas
146249b1b8d6SLorenzo Stoakes 			 * (in turn preventing a vma_merge) to be
146349b1b8d6SLorenzo Stoakes 			 * safe. It is only safe to keep the vm_pgoff
146449b1b8d6SLorenzo Stoakes 			 * linear if there are no pages mapped yet.
146549b1b8d6SLorenzo Stoakes 			 */
146649b1b8d6SLorenzo Stoakes 			VM_BUG_ON_VMA(faulted_in_anon_vma, new_vma);
146749b1b8d6SLorenzo Stoakes 			*vmap = vma = new_vma;
146849b1b8d6SLorenzo Stoakes 		}
146949b1b8d6SLorenzo Stoakes 		*need_rmap_locks = (new_vma->vm_pgoff <= vma->vm_pgoff);
147049b1b8d6SLorenzo Stoakes 	} else {
147149b1b8d6SLorenzo Stoakes 		new_vma = vm_area_dup(vma);
147249b1b8d6SLorenzo Stoakes 		if (!new_vma)
147349b1b8d6SLorenzo Stoakes 			goto out;
147449b1b8d6SLorenzo Stoakes 		vma_set_range(new_vma, addr, addr + len, pgoff);
147549b1b8d6SLorenzo Stoakes 		if (vma_dup_policy(vma, new_vma))
147649b1b8d6SLorenzo Stoakes 			goto out_free_vma;
147749b1b8d6SLorenzo Stoakes 		if (anon_vma_clone(new_vma, vma))
147849b1b8d6SLorenzo Stoakes 			goto out_free_mempol;
147949b1b8d6SLorenzo Stoakes 		if (new_vma->vm_file)
148049b1b8d6SLorenzo Stoakes 			get_file(new_vma->vm_file);
148149b1b8d6SLorenzo Stoakes 		if (new_vma->vm_ops && new_vma->vm_ops->open)
148249b1b8d6SLorenzo Stoakes 			new_vma->vm_ops->open(new_vma);
148349b1b8d6SLorenzo Stoakes 		if (vma_link(mm, new_vma))
148449b1b8d6SLorenzo Stoakes 			goto out_vma_link;
148549b1b8d6SLorenzo Stoakes 		*need_rmap_locks = false;
148649b1b8d6SLorenzo Stoakes 	}
148749b1b8d6SLorenzo Stoakes 	return new_vma;
148849b1b8d6SLorenzo Stoakes 
148949b1b8d6SLorenzo Stoakes out_vma_link:
149049b1b8d6SLorenzo Stoakes 	if (new_vma->vm_ops && new_vma->vm_ops->close)
149149b1b8d6SLorenzo Stoakes 		new_vma->vm_ops->close(new_vma);
149249b1b8d6SLorenzo Stoakes 
149349b1b8d6SLorenzo Stoakes 	if (new_vma->vm_file)
149449b1b8d6SLorenzo Stoakes 		fput(new_vma->vm_file);
149549b1b8d6SLorenzo Stoakes 
149649b1b8d6SLorenzo Stoakes 	unlink_anon_vmas(new_vma);
149749b1b8d6SLorenzo Stoakes out_free_mempol:
149849b1b8d6SLorenzo Stoakes 	mpol_put(vma_policy(new_vma));
149949b1b8d6SLorenzo Stoakes out_free_vma:
150049b1b8d6SLorenzo Stoakes 	vm_area_free(new_vma);
150149b1b8d6SLorenzo Stoakes out:
150249b1b8d6SLorenzo Stoakes 	return NULL;
150349b1b8d6SLorenzo Stoakes }
150449b1b8d6SLorenzo Stoakes 
150549b1b8d6SLorenzo Stoakes /*
150649b1b8d6SLorenzo Stoakes  * Rough compatibility check to quickly see if it's even worth looking
150749b1b8d6SLorenzo Stoakes  * at sharing an anon_vma.
150849b1b8d6SLorenzo Stoakes  *
150949b1b8d6SLorenzo Stoakes  * They need to have the same vm_file, and the flags can only differ
151049b1b8d6SLorenzo Stoakes  * in things that mprotect may change.
151149b1b8d6SLorenzo Stoakes  *
151249b1b8d6SLorenzo Stoakes  * NOTE! The fact that we share an anon_vma doesn't _have_ to mean that
151349b1b8d6SLorenzo Stoakes  * we can merge the two vma's. For example, we refuse to merge a vma if
151449b1b8d6SLorenzo Stoakes  * there is a vm_ops->close() function, because that indicates that the
151549b1b8d6SLorenzo Stoakes  * driver is doing some kind of reference counting. But that doesn't
151649b1b8d6SLorenzo Stoakes  * really matter for the anon_vma sharing case.
151749b1b8d6SLorenzo Stoakes  */
151849b1b8d6SLorenzo Stoakes static int anon_vma_compatible(struct vm_area_struct *a, struct vm_area_struct *b)
151949b1b8d6SLorenzo Stoakes {
152049b1b8d6SLorenzo Stoakes 	return a->vm_end == b->vm_start &&
152149b1b8d6SLorenzo Stoakes 		mpol_equal(vma_policy(a), vma_policy(b)) &&
152249b1b8d6SLorenzo Stoakes 		a->vm_file == b->vm_file &&
152349b1b8d6SLorenzo Stoakes 		!((a->vm_flags ^ b->vm_flags) & ~(VM_ACCESS_FLAGS | VM_SOFTDIRTY)) &&
152449b1b8d6SLorenzo Stoakes 		b->vm_pgoff == a->vm_pgoff + ((b->vm_start - a->vm_start) >> PAGE_SHIFT);
152549b1b8d6SLorenzo Stoakes }
152649b1b8d6SLorenzo Stoakes 
152749b1b8d6SLorenzo Stoakes /*
152849b1b8d6SLorenzo Stoakes  * Do some basic sanity checking to see if we can re-use the anon_vma
152949b1b8d6SLorenzo Stoakes  * from 'old'. The 'a'/'b' vma's are in VM order - one of them will be
153049b1b8d6SLorenzo Stoakes  * the same as 'old', the other will be the new one that is trying
153149b1b8d6SLorenzo Stoakes  * to share the anon_vma.
153249b1b8d6SLorenzo Stoakes  *
153349b1b8d6SLorenzo Stoakes  * NOTE! This runs with mmap_lock held for reading, so it is possible that
153449b1b8d6SLorenzo Stoakes  * the anon_vma of 'old' is concurrently in the process of being set up
153549b1b8d6SLorenzo Stoakes  * by another page fault trying to merge _that_. But that's ok: if it
153649b1b8d6SLorenzo Stoakes  * is being set up, that automatically means that it will be a singleton
153749b1b8d6SLorenzo Stoakes  * acceptable for merging, so we can do all of this optimistically. But
153849b1b8d6SLorenzo Stoakes  * we do that READ_ONCE() to make sure that we never re-load the pointer.
153949b1b8d6SLorenzo Stoakes  *
154049b1b8d6SLorenzo Stoakes  * IOW: that the "list_is_singular()" test on the anon_vma_chain only
154149b1b8d6SLorenzo Stoakes  * matters for the 'stable anon_vma' case (ie the thing we want to avoid
154249b1b8d6SLorenzo Stoakes  * is to return an anon_vma that is "complex" due to having gone through
154349b1b8d6SLorenzo Stoakes  * a fork).
154449b1b8d6SLorenzo Stoakes  *
154549b1b8d6SLorenzo Stoakes  * We also make sure that the two vma's are compatible (adjacent,
154649b1b8d6SLorenzo Stoakes  * and with the same memory policies). That's all stable, even with just
154749b1b8d6SLorenzo Stoakes  * a read lock on the mmap_lock.
154849b1b8d6SLorenzo Stoakes  */
154949b1b8d6SLorenzo Stoakes static struct anon_vma *reusable_anon_vma(struct vm_area_struct *old,
155049b1b8d6SLorenzo Stoakes 					  struct vm_area_struct *a,
155149b1b8d6SLorenzo Stoakes 					  struct vm_area_struct *b)
155249b1b8d6SLorenzo Stoakes {
155349b1b8d6SLorenzo Stoakes 	if (anon_vma_compatible(a, b)) {
155449b1b8d6SLorenzo Stoakes 		struct anon_vma *anon_vma = READ_ONCE(old->anon_vma);
155549b1b8d6SLorenzo Stoakes 
155649b1b8d6SLorenzo Stoakes 		if (anon_vma && list_is_singular(&old->anon_vma_chain))
155749b1b8d6SLorenzo Stoakes 			return anon_vma;
155849b1b8d6SLorenzo Stoakes 	}
155949b1b8d6SLorenzo Stoakes 	return NULL;
156049b1b8d6SLorenzo Stoakes }
156149b1b8d6SLorenzo Stoakes 
156249b1b8d6SLorenzo Stoakes /*
156349b1b8d6SLorenzo Stoakes  * find_mergeable_anon_vma is used by anon_vma_prepare, to check
156449b1b8d6SLorenzo Stoakes  * neighbouring vmas for a suitable anon_vma, before it goes off
156549b1b8d6SLorenzo Stoakes  * to allocate a new anon_vma.  It checks because a repetitive
156649b1b8d6SLorenzo Stoakes  * sequence of mprotects and faults may otherwise lead to distinct
156749b1b8d6SLorenzo Stoakes  * anon_vmas being allocated, preventing vma merge in subsequent
156849b1b8d6SLorenzo Stoakes  * mprotect.
156949b1b8d6SLorenzo Stoakes  */
157049b1b8d6SLorenzo Stoakes struct anon_vma *find_mergeable_anon_vma(struct vm_area_struct *vma)
157149b1b8d6SLorenzo Stoakes {
157249b1b8d6SLorenzo Stoakes 	struct anon_vma *anon_vma = NULL;
157349b1b8d6SLorenzo Stoakes 	struct vm_area_struct *prev, *next;
157449b1b8d6SLorenzo Stoakes 	VMA_ITERATOR(vmi, vma->vm_mm, vma->vm_end);
157549b1b8d6SLorenzo Stoakes 
157649b1b8d6SLorenzo Stoakes 	/* Try next first. */
157749b1b8d6SLorenzo Stoakes 	next = vma_iter_load(&vmi);
157849b1b8d6SLorenzo Stoakes 	if (next) {
157949b1b8d6SLorenzo Stoakes 		anon_vma = reusable_anon_vma(next, vma, next);
158049b1b8d6SLorenzo Stoakes 		if (anon_vma)
158149b1b8d6SLorenzo Stoakes 			return anon_vma;
158249b1b8d6SLorenzo Stoakes 	}
158349b1b8d6SLorenzo Stoakes 
158449b1b8d6SLorenzo Stoakes 	prev = vma_prev(&vmi);
158549b1b8d6SLorenzo Stoakes 	VM_BUG_ON_VMA(prev != vma, vma);
158649b1b8d6SLorenzo Stoakes 	prev = vma_prev(&vmi);
158749b1b8d6SLorenzo Stoakes 	/* Try prev next. */
158849b1b8d6SLorenzo Stoakes 	if (prev)
158949b1b8d6SLorenzo Stoakes 		anon_vma = reusable_anon_vma(prev, prev, vma);
159049b1b8d6SLorenzo Stoakes 
159149b1b8d6SLorenzo Stoakes 	/*
159249b1b8d6SLorenzo Stoakes 	 * We might reach here with anon_vma == NULL if we can't find
159349b1b8d6SLorenzo Stoakes 	 * any reusable anon_vma.
159449b1b8d6SLorenzo Stoakes 	 * There's no absolute need to look only at touching neighbours:
159549b1b8d6SLorenzo Stoakes 	 * we could search further afield for "compatible" anon_vmas.
159649b1b8d6SLorenzo Stoakes 	 * But it would probably just be a waste of time searching,
159749b1b8d6SLorenzo Stoakes 	 * or lead to too many vmas hanging off the same anon_vma.
159849b1b8d6SLorenzo Stoakes 	 * We're trying to allow mprotect remerging later on,
159949b1b8d6SLorenzo Stoakes 	 * not trying to minimize memory used for anon_vmas.
160049b1b8d6SLorenzo Stoakes 	 */
160149b1b8d6SLorenzo Stoakes 	return anon_vma;
160249b1b8d6SLorenzo Stoakes }
160349b1b8d6SLorenzo Stoakes 
160449b1b8d6SLorenzo Stoakes static bool vm_ops_needs_writenotify(const struct vm_operations_struct *vm_ops)
160549b1b8d6SLorenzo Stoakes {
160649b1b8d6SLorenzo Stoakes 	return vm_ops && (vm_ops->page_mkwrite || vm_ops->pfn_mkwrite);
160749b1b8d6SLorenzo Stoakes }
160849b1b8d6SLorenzo Stoakes 
160949b1b8d6SLorenzo Stoakes static bool vma_is_shared_writable(struct vm_area_struct *vma)
161049b1b8d6SLorenzo Stoakes {
161149b1b8d6SLorenzo Stoakes 	return (vma->vm_flags & (VM_WRITE | VM_SHARED)) ==
161249b1b8d6SLorenzo Stoakes 		(VM_WRITE | VM_SHARED);
161349b1b8d6SLorenzo Stoakes }
161449b1b8d6SLorenzo Stoakes 
161549b1b8d6SLorenzo Stoakes static bool vma_fs_can_writeback(struct vm_area_struct *vma)
161649b1b8d6SLorenzo Stoakes {
161749b1b8d6SLorenzo Stoakes 	/* No managed pages to writeback. */
161849b1b8d6SLorenzo Stoakes 	if (vma->vm_flags & VM_PFNMAP)
161949b1b8d6SLorenzo Stoakes 		return false;
162049b1b8d6SLorenzo Stoakes 
162149b1b8d6SLorenzo Stoakes 	return vma->vm_file && vma->vm_file->f_mapping &&
162249b1b8d6SLorenzo Stoakes 		mapping_can_writeback(vma->vm_file->f_mapping);
162349b1b8d6SLorenzo Stoakes }
162449b1b8d6SLorenzo Stoakes 
162549b1b8d6SLorenzo Stoakes /*
162649b1b8d6SLorenzo Stoakes  * Does this VMA require the underlying folios to have their dirty state
162749b1b8d6SLorenzo Stoakes  * tracked?
162849b1b8d6SLorenzo Stoakes  */
162949b1b8d6SLorenzo Stoakes bool vma_needs_dirty_tracking(struct vm_area_struct *vma)
163049b1b8d6SLorenzo Stoakes {
163149b1b8d6SLorenzo Stoakes 	/* Only shared, writable VMAs require dirty tracking. */
163249b1b8d6SLorenzo Stoakes 	if (!vma_is_shared_writable(vma))
163349b1b8d6SLorenzo Stoakes 		return false;
163449b1b8d6SLorenzo Stoakes 
163549b1b8d6SLorenzo Stoakes 	/* Does the filesystem need to be notified? */
163649b1b8d6SLorenzo Stoakes 	if (vm_ops_needs_writenotify(vma->vm_ops))
163749b1b8d6SLorenzo Stoakes 		return true;
163849b1b8d6SLorenzo Stoakes 
163949b1b8d6SLorenzo Stoakes 	/*
164049b1b8d6SLorenzo Stoakes 	 * Even if the filesystem doesn't indicate a need for writenotify, if it
164149b1b8d6SLorenzo Stoakes 	 * can writeback, dirty tracking is still required.
164249b1b8d6SLorenzo Stoakes 	 */
164349b1b8d6SLorenzo Stoakes 	return vma_fs_can_writeback(vma);
164449b1b8d6SLorenzo Stoakes }
164549b1b8d6SLorenzo Stoakes 
164649b1b8d6SLorenzo Stoakes /*
164749b1b8d6SLorenzo Stoakes  * Some shared mappings will want the pages marked read-only
164849b1b8d6SLorenzo Stoakes  * to track write events. If so, we'll downgrade vm_page_prot
164949b1b8d6SLorenzo Stoakes  * to the private version (using protection_map[] without the
165049b1b8d6SLorenzo Stoakes  * VM_SHARED bit).
165149b1b8d6SLorenzo Stoakes  */
165249b1b8d6SLorenzo Stoakes bool vma_wants_writenotify(struct vm_area_struct *vma, pgprot_t vm_page_prot)
165349b1b8d6SLorenzo Stoakes {
165449b1b8d6SLorenzo Stoakes 	/* If it was private or non-writable, the write bit is already clear */
165549b1b8d6SLorenzo Stoakes 	if (!vma_is_shared_writable(vma))
165649b1b8d6SLorenzo Stoakes 		return false;
165749b1b8d6SLorenzo Stoakes 
165849b1b8d6SLorenzo Stoakes 	/* The backer wishes to know when pages are first written to? */
165949b1b8d6SLorenzo Stoakes 	if (vm_ops_needs_writenotify(vma->vm_ops))
166049b1b8d6SLorenzo Stoakes 		return true;
166149b1b8d6SLorenzo Stoakes 
166249b1b8d6SLorenzo Stoakes 	/* The open routine did something to the protections that pgprot_modify
166349b1b8d6SLorenzo Stoakes 	 * won't preserve? */
166449b1b8d6SLorenzo Stoakes 	if (pgprot_val(vm_page_prot) !=
166549b1b8d6SLorenzo Stoakes 	    pgprot_val(vm_pgprot_modify(vm_page_prot, vma->vm_flags)))
166649b1b8d6SLorenzo Stoakes 		return false;
166749b1b8d6SLorenzo Stoakes 
166849b1b8d6SLorenzo Stoakes 	/*
166949b1b8d6SLorenzo Stoakes 	 * Do we need to track softdirty? hugetlb does not support softdirty
167049b1b8d6SLorenzo Stoakes 	 * tracking yet.
167149b1b8d6SLorenzo Stoakes 	 */
167249b1b8d6SLorenzo Stoakes 	if (vma_soft_dirty_enabled(vma) && !is_vm_hugetlb_page(vma))
167349b1b8d6SLorenzo Stoakes 		return true;
167449b1b8d6SLorenzo Stoakes 
167549b1b8d6SLorenzo Stoakes 	/* Do we need write faults for uffd-wp tracking? */
167649b1b8d6SLorenzo Stoakes 	if (userfaultfd_wp(vma))
167749b1b8d6SLorenzo Stoakes 		return true;
167849b1b8d6SLorenzo Stoakes 
167949b1b8d6SLorenzo Stoakes 	/* Can the mapping track the dirty pages? */
168049b1b8d6SLorenzo Stoakes 	return vma_fs_can_writeback(vma);
168149b1b8d6SLorenzo Stoakes }
168249b1b8d6SLorenzo Stoakes 
168349b1b8d6SLorenzo Stoakes static DEFINE_MUTEX(mm_all_locks_mutex);
168449b1b8d6SLorenzo Stoakes 
168549b1b8d6SLorenzo Stoakes static void vm_lock_anon_vma(struct mm_struct *mm, struct anon_vma *anon_vma)
168649b1b8d6SLorenzo Stoakes {
168749b1b8d6SLorenzo Stoakes 	if (!test_bit(0, (unsigned long *) &anon_vma->root->rb_root.rb_root.rb_node)) {
168849b1b8d6SLorenzo Stoakes 		/*
168949b1b8d6SLorenzo Stoakes 		 * The LSB of head.next can't change from under us
169049b1b8d6SLorenzo Stoakes 		 * because we hold the mm_all_locks_mutex.
169149b1b8d6SLorenzo Stoakes 		 */
169249b1b8d6SLorenzo Stoakes 		down_write_nest_lock(&anon_vma->root->rwsem, &mm->mmap_lock);
169349b1b8d6SLorenzo Stoakes 		/*
169449b1b8d6SLorenzo Stoakes 		 * We can safely modify head.next after taking the
169549b1b8d6SLorenzo Stoakes 		 * anon_vma->root->rwsem. If some other vma in this mm shares
169649b1b8d6SLorenzo Stoakes 		 * the same anon_vma we won't take it again.
169749b1b8d6SLorenzo Stoakes 		 *
169849b1b8d6SLorenzo Stoakes 		 * No need of atomic instructions here, head.next
169949b1b8d6SLorenzo Stoakes 		 * can't change from under us thanks to the
170049b1b8d6SLorenzo Stoakes 		 * anon_vma->root->rwsem.
170149b1b8d6SLorenzo Stoakes 		 */
170249b1b8d6SLorenzo Stoakes 		if (__test_and_set_bit(0, (unsigned long *)
170349b1b8d6SLorenzo Stoakes 				       &anon_vma->root->rb_root.rb_root.rb_node))
170449b1b8d6SLorenzo Stoakes 			BUG();
170549b1b8d6SLorenzo Stoakes 	}
170649b1b8d6SLorenzo Stoakes }
170749b1b8d6SLorenzo Stoakes 
170849b1b8d6SLorenzo Stoakes static void vm_lock_mapping(struct mm_struct *mm, struct address_space *mapping)
170949b1b8d6SLorenzo Stoakes {
171049b1b8d6SLorenzo Stoakes 	if (!test_bit(AS_MM_ALL_LOCKS, &mapping->flags)) {
171149b1b8d6SLorenzo Stoakes 		/*
171249b1b8d6SLorenzo Stoakes 		 * AS_MM_ALL_LOCKS can't change from under us because
171349b1b8d6SLorenzo Stoakes 		 * we hold the mm_all_locks_mutex.
171449b1b8d6SLorenzo Stoakes 		 *
171549b1b8d6SLorenzo Stoakes 		 * Operations on ->flags have to be atomic because
171649b1b8d6SLorenzo Stoakes 		 * even if AS_MM_ALL_LOCKS is stable thanks to the
171749b1b8d6SLorenzo Stoakes 		 * mm_all_locks_mutex, there may be other cpus
171849b1b8d6SLorenzo Stoakes 		 * changing other bitflags in parallel to us.
171949b1b8d6SLorenzo Stoakes 		 */
172049b1b8d6SLorenzo Stoakes 		if (test_and_set_bit(AS_MM_ALL_LOCKS, &mapping->flags))
172149b1b8d6SLorenzo Stoakes 			BUG();
172249b1b8d6SLorenzo Stoakes 		down_write_nest_lock(&mapping->i_mmap_rwsem, &mm->mmap_lock);
172349b1b8d6SLorenzo Stoakes 	}
172449b1b8d6SLorenzo Stoakes }
172549b1b8d6SLorenzo Stoakes 
172649b1b8d6SLorenzo Stoakes /*
172749b1b8d6SLorenzo Stoakes  * This operation locks against the VM for all pte/vma/mm related
172849b1b8d6SLorenzo Stoakes  * operations that could ever happen on a certain mm. This includes
172949b1b8d6SLorenzo Stoakes  * vmtruncate, try_to_unmap, and all page faults.
173049b1b8d6SLorenzo Stoakes  *
173149b1b8d6SLorenzo Stoakes  * The caller must take the mmap_lock in write mode before calling
173249b1b8d6SLorenzo Stoakes  * mm_take_all_locks(). The caller isn't allowed to release the
173349b1b8d6SLorenzo Stoakes  * mmap_lock until mm_drop_all_locks() returns.
173449b1b8d6SLorenzo Stoakes  *
173549b1b8d6SLorenzo Stoakes  * mmap_lock in write mode is required in order to block all operations
173649b1b8d6SLorenzo Stoakes  * that could modify pagetables and free pages without need of
173749b1b8d6SLorenzo Stoakes  * altering the vma layout. It's also needed in write mode to avoid new
173849b1b8d6SLorenzo Stoakes  * anon_vmas to be associated with existing vmas.
173949b1b8d6SLorenzo Stoakes  *
174049b1b8d6SLorenzo Stoakes  * A single task can't take more than one mm_take_all_locks() in a row
174149b1b8d6SLorenzo Stoakes  * or it would deadlock.
174249b1b8d6SLorenzo Stoakes  *
174349b1b8d6SLorenzo Stoakes  * The LSB in anon_vma->rb_root.rb_node and the AS_MM_ALL_LOCKS bitflag in
174449b1b8d6SLorenzo Stoakes  * mapping->flags avoid to take the same lock twice, if more than one
174549b1b8d6SLorenzo Stoakes  * vma in this mm is backed by the same anon_vma or address_space.
174649b1b8d6SLorenzo Stoakes  *
174749b1b8d6SLorenzo Stoakes  * We take locks in following order, accordingly to comment at beginning
174849b1b8d6SLorenzo Stoakes  * of mm/rmap.c:
174949b1b8d6SLorenzo Stoakes  *   - all hugetlbfs_i_mmap_rwsem_key locks (aka mapping->i_mmap_rwsem for
175049b1b8d6SLorenzo Stoakes  *     hugetlb mapping);
175149b1b8d6SLorenzo Stoakes  *   - all vmas marked locked
175249b1b8d6SLorenzo Stoakes  *   - all i_mmap_rwsem locks;
175349b1b8d6SLorenzo Stoakes  *   - all anon_vma->rwseml
175449b1b8d6SLorenzo Stoakes  *
175549b1b8d6SLorenzo Stoakes  * We can take all locks within these types randomly because the VM code
175649b1b8d6SLorenzo Stoakes  * doesn't nest them and we protected from parallel mm_take_all_locks() by
175749b1b8d6SLorenzo Stoakes  * mm_all_locks_mutex.
175849b1b8d6SLorenzo Stoakes  *
175949b1b8d6SLorenzo Stoakes  * mm_take_all_locks() and mm_drop_all_locks are expensive operations
176049b1b8d6SLorenzo Stoakes  * that may have to take thousand of locks.
176149b1b8d6SLorenzo Stoakes  *
176249b1b8d6SLorenzo Stoakes  * mm_take_all_locks() can fail if it's interrupted by signals.
176349b1b8d6SLorenzo Stoakes  */
176449b1b8d6SLorenzo Stoakes int mm_take_all_locks(struct mm_struct *mm)
176549b1b8d6SLorenzo Stoakes {
176649b1b8d6SLorenzo Stoakes 	struct vm_area_struct *vma;
176749b1b8d6SLorenzo Stoakes 	struct anon_vma_chain *avc;
176849b1b8d6SLorenzo Stoakes 	VMA_ITERATOR(vmi, mm, 0);
176949b1b8d6SLorenzo Stoakes 
177049b1b8d6SLorenzo Stoakes 	mmap_assert_write_locked(mm);
177149b1b8d6SLorenzo Stoakes 
177249b1b8d6SLorenzo Stoakes 	mutex_lock(&mm_all_locks_mutex);
177349b1b8d6SLorenzo Stoakes 
177449b1b8d6SLorenzo Stoakes 	/*
177549b1b8d6SLorenzo Stoakes 	 * vma_start_write() does not have a complement in mm_drop_all_locks()
177649b1b8d6SLorenzo Stoakes 	 * because vma_start_write() is always asymmetrical; it marks a VMA as
177749b1b8d6SLorenzo Stoakes 	 * being written to until mmap_write_unlock() or mmap_write_downgrade()
177849b1b8d6SLorenzo Stoakes 	 * is reached.
177949b1b8d6SLorenzo Stoakes 	 */
178049b1b8d6SLorenzo Stoakes 	for_each_vma(vmi, vma) {
178149b1b8d6SLorenzo Stoakes 		if (signal_pending(current))
178249b1b8d6SLorenzo Stoakes 			goto out_unlock;
178349b1b8d6SLorenzo Stoakes 		vma_start_write(vma);
178449b1b8d6SLorenzo Stoakes 	}
178549b1b8d6SLorenzo Stoakes 
178649b1b8d6SLorenzo Stoakes 	vma_iter_init(&vmi, mm, 0);
178749b1b8d6SLorenzo Stoakes 	for_each_vma(vmi, vma) {
178849b1b8d6SLorenzo Stoakes 		if (signal_pending(current))
178949b1b8d6SLorenzo Stoakes 			goto out_unlock;
179049b1b8d6SLorenzo Stoakes 		if (vma->vm_file && vma->vm_file->f_mapping &&
179149b1b8d6SLorenzo Stoakes 				is_vm_hugetlb_page(vma))
179249b1b8d6SLorenzo Stoakes 			vm_lock_mapping(mm, vma->vm_file->f_mapping);
179349b1b8d6SLorenzo Stoakes 	}
179449b1b8d6SLorenzo Stoakes 
179549b1b8d6SLorenzo Stoakes 	vma_iter_init(&vmi, mm, 0);
179649b1b8d6SLorenzo Stoakes 	for_each_vma(vmi, vma) {
179749b1b8d6SLorenzo Stoakes 		if (signal_pending(current))
179849b1b8d6SLorenzo Stoakes 			goto out_unlock;
179949b1b8d6SLorenzo Stoakes 		if (vma->vm_file && vma->vm_file->f_mapping &&
180049b1b8d6SLorenzo Stoakes 				!is_vm_hugetlb_page(vma))
180149b1b8d6SLorenzo Stoakes 			vm_lock_mapping(mm, vma->vm_file->f_mapping);
180249b1b8d6SLorenzo Stoakes 	}
180349b1b8d6SLorenzo Stoakes 
180449b1b8d6SLorenzo Stoakes 	vma_iter_init(&vmi, mm, 0);
180549b1b8d6SLorenzo Stoakes 	for_each_vma(vmi, vma) {
180649b1b8d6SLorenzo Stoakes 		if (signal_pending(current))
180749b1b8d6SLorenzo Stoakes 			goto out_unlock;
180849b1b8d6SLorenzo Stoakes 		if (vma->anon_vma)
180949b1b8d6SLorenzo Stoakes 			list_for_each_entry(avc, &vma->anon_vma_chain, same_vma)
181049b1b8d6SLorenzo Stoakes 				vm_lock_anon_vma(mm, avc->anon_vma);
181149b1b8d6SLorenzo Stoakes 	}
181249b1b8d6SLorenzo Stoakes 
181349b1b8d6SLorenzo Stoakes 	return 0;
181449b1b8d6SLorenzo Stoakes 
181549b1b8d6SLorenzo Stoakes out_unlock:
181649b1b8d6SLorenzo Stoakes 	mm_drop_all_locks(mm);
181749b1b8d6SLorenzo Stoakes 	return -EINTR;
181849b1b8d6SLorenzo Stoakes }
181949b1b8d6SLorenzo Stoakes 
182049b1b8d6SLorenzo Stoakes static void vm_unlock_anon_vma(struct anon_vma *anon_vma)
182149b1b8d6SLorenzo Stoakes {
182249b1b8d6SLorenzo Stoakes 	if (test_bit(0, (unsigned long *) &anon_vma->root->rb_root.rb_root.rb_node)) {
182349b1b8d6SLorenzo Stoakes 		/*
182449b1b8d6SLorenzo Stoakes 		 * The LSB of head.next can't change to 0 from under
182549b1b8d6SLorenzo Stoakes 		 * us because we hold the mm_all_locks_mutex.
182649b1b8d6SLorenzo Stoakes 		 *
182749b1b8d6SLorenzo Stoakes 		 * We must however clear the bitflag before unlocking
182849b1b8d6SLorenzo Stoakes 		 * the vma so the users using the anon_vma->rb_root will
182949b1b8d6SLorenzo Stoakes 		 * never see our bitflag.
183049b1b8d6SLorenzo Stoakes 		 *
183149b1b8d6SLorenzo Stoakes 		 * No need of atomic instructions here, head.next
183249b1b8d6SLorenzo Stoakes 		 * can't change from under us until we release the
183349b1b8d6SLorenzo Stoakes 		 * anon_vma->root->rwsem.
183449b1b8d6SLorenzo Stoakes 		 */
183549b1b8d6SLorenzo Stoakes 		if (!__test_and_clear_bit(0, (unsigned long *)
183649b1b8d6SLorenzo Stoakes 					  &anon_vma->root->rb_root.rb_root.rb_node))
183749b1b8d6SLorenzo Stoakes 			BUG();
183849b1b8d6SLorenzo Stoakes 		anon_vma_unlock_write(anon_vma);
183949b1b8d6SLorenzo Stoakes 	}
184049b1b8d6SLorenzo Stoakes }
184149b1b8d6SLorenzo Stoakes 
184249b1b8d6SLorenzo Stoakes static void vm_unlock_mapping(struct address_space *mapping)
184349b1b8d6SLorenzo Stoakes {
184449b1b8d6SLorenzo Stoakes 	if (test_bit(AS_MM_ALL_LOCKS, &mapping->flags)) {
184549b1b8d6SLorenzo Stoakes 		/*
184649b1b8d6SLorenzo Stoakes 		 * AS_MM_ALL_LOCKS can't change to 0 from under us
184749b1b8d6SLorenzo Stoakes 		 * because we hold the mm_all_locks_mutex.
184849b1b8d6SLorenzo Stoakes 		 */
184949b1b8d6SLorenzo Stoakes 		i_mmap_unlock_write(mapping);
185049b1b8d6SLorenzo Stoakes 		if (!test_and_clear_bit(AS_MM_ALL_LOCKS,
185149b1b8d6SLorenzo Stoakes 					&mapping->flags))
185249b1b8d6SLorenzo Stoakes 			BUG();
185349b1b8d6SLorenzo Stoakes 	}
185449b1b8d6SLorenzo Stoakes }
185549b1b8d6SLorenzo Stoakes 
185649b1b8d6SLorenzo Stoakes /*
185749b1b8d6SLorenzo Stoakes  * The mmap_lock cannot be released by the caller until
185849b1b8d6SLorenzo Stoakes  * mm_drop_all_locks() returns.
185949b1b8d6SLorenzo Stoakes  */
186049b1b8d6SLorenzo Stoakes void mm_drop_all_locks(struct mm_struct *mm)
186149b1b8d6SLorenzo Stoakes {
186249b1b8d6SLorenzo Stoakes 	struct vm_area_struct *vma;
186349b1b8d6SLorenzo Stoakes 	struct anon_vma_chain *avc;
186449b1b8d6SLorenzo Stoakes 	VMA_ITERATOR(vmi, mm, 0);
186549b1b8d6SLorenzo Stoakes 
186649b1b8d6SLorenzo Stoakes 	mmap_assert_write_locked(mm);
186749b1b8d6SLorenzo Stoakes 	BUG_ON(!mutex_is_locked(&mm_all_locks_mutex));
186849b1b8d6SLorenzo Stoakes 
186949b1b8d6SLorenzo Stoakes 	for_each_vma(vmi, vma) {
187049b1b8d6SLorenzo Stoakes 		if (vma->anon_vma)
187149b1b8d6SLorenzo Stoakes 			list_for_each_entry(avc, &vma->anon_vma_chain, same_vma)
187249b1b8d6SLorenzo Stoakes 				vm_unlock_anon_vma(avc->anon_vma);
187349b1b8d6SLorenzo Stoakes 		if (vma->vm_file && vma->vm_file->f_mapping)
187449b1b8d6SLorenzo Stoakes 			vm_unlock_mapping(vma->vm_file->f_mapping);
187549b1b8d6SLorenzo Stoakes 	}
187649b1b8d6SLorenzo Stoakes 
187749b1b8d6SLorenzo Stoakes 	mutex_unlock(&mm_all_locks_mutex);
187849b1b8d6SLorenzo Stoakes }
1879