xref: /linux/mm/vma.c (revision 90cb921c4d7bf92854344d3e76561f48784c613e)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 
3 /*
4  * VMA-specific functions.
5  */
6 
7 #include "vma_internal.h"
8 #include "vma.h"
9 
10 struct mmap_state {
11 	struct mm_struct *mm;
12 	struct vma_iterator *vmi;
13 
14 	unsigned long addr;
15 	unsigned long end;
16 	pgoff_t pgoff;
17 	unsigned long pglen;
18 	union {
19 		vm_flags_t vm_flags;
20 		vma_flags_t vma_flags;
21 	};
22 	struct file *file;
23 	pgprot_t page_prot;
24 
25 	/* User-defined fields, perhaps updated by .mmap_prepare(). */
26 	const struct vm_operations_struct *vm_ops;
27 	void *vm_private_data;
28 
29 	unsigned long charged;
30 
31 	struct vm_area_struct *prev;
32 	struct vm_area_struct *next;
33 
34 	/* Unmapping state. */
35 	struct vma_munmap_struct vms;
36 	struct ma_state mas_detach;
37 	struct maple_tree mt_detach;
38 
39 	/* Determine if we can check KSM flags early in mmap() logic. */
40 	bool check_ksm_early :1;
41 	/* If we map new, hold the file rmap lock on mapping. */
42 	bool hold_file_rmap_lock :1;
43 	/* If .mmap_prepare changed the file, we don't need to pin. */
44 	bool file_doesnt_need_get :1;
45 };
46 
47 #define MMAP_STATE(name, mm_, vmi_, addr_, len_, pgoff_, vma_flags_, file_) \
48 	struct mmap_state name = {					\
49 		.mm = mm_,						\
50 		.vmi = vmi_,						\
51 		.addr = addr_,						\
52 		.end = (addr_) + (len_),				\
53 		.pgoff = pgoff_,					\
54 		.pglen = PHYS_PFN(len_),				\
55 		.vma_flags = vma_flags_,				\
56 		.file = file_,						\
57 		.page_prot = vma_get_page_prot(vma_flags_),		\
58 	}
59 
60 #define VMG_MMAP_STATE(name, map_, vma_)				\
61 	struct vma_merge_struct name = {				\
62 		.mm = (map_)->mm,					\
63 		.vmi = (map_)->vmi,					\
64 		.start = (map_)->addr,					\
65 		.end = (map_)->end,					\
66 		.vma_flags = (map_)->vma_flags,				\
67 		.pgoff = (map_)->pgoff,					\
68 		.file = (map_)->file,					\
69 		.prev = (map_)->prev,					\
70 		.middle = vma_,						\
71 		.next = (vma_) ? NULL : (map_)->next,			\
72 		.state = VMA_MERGE_START,				\
73 	}
74 
75 /* Was this VMA ever forked from a parent, i.e. maybe contains CoW mappings? */
76 static bool vma_is_fork_child(struct vm_area_struct *vma)
77 {
78 	/*
79 	 * The list_is_singular() test is to avoid merging VMA cloned from
80 	 * parents. This can improve scalability caused by the anon_vma root
81 	 * lock.
82 	 */
83 	return vma && vma->anon_vma && !list_is_singular(&vma->anon_vma_chain);
84 }
85 
86 static inline bool is_mergeable_vma(struct vma_merge_struct *vmg, bool merge_next)
87 {
88 	struct vm_area_struct *vma = merge_next ? vmg->next : vmg->prev;
89 	vma_flags_t diff;
90 
91 	if (!mpol_equal(vmg->policy, vma_policy(vma)))
92 		return false;
93 
94 	diff = vma_flags_diff_pair(&vma->flags, &vmg->vma_flags);
95 	vma_flags_clear_mask(&diff, VMA_IGNORE_MERGE_FLAGS);
96 
97 	if (!vma_flags_empty(&diff))
98 		return false;
99 	if (vma->vm_file != vmg->file)
100 		return false;
101 	if (!is_mergeable_vm_userfaultfd_ctx(vma, vmg->uffd_ctx))
102 		return false;
103 	if (!anon_vma_name_eq(anon_vma_name(vma), vmg->anon_name))
104 		return false;
105 	return true;
106 }
107 
108 static bool is_mergeable_anon_vma(struct vma_merge_struct *vmg, bool merge_next)
109 {
110 	struct vm_area_struct *tgt = merge_next ? vmg->next : vmg->prev;
111 	struct vm_area_struct *src = vmg->middle; /* existing merge case. */
112 	struct anon_vma *tgt_anon = tgt->anon_vma;
113 	struct anon_vma *src_anon = vmg->anon_vma;
114 
115 	/*
116 	 * We _can_ have !src, vmg->anon_vma via copy_vma(). In this instance we
117 	 * will remove the existing VMA's anon_vma's so there's no scalability
118 	 * concerns.
119 	 */
120 	VM_WARN_ON(src && src_anon != src->anon_vma);
121 
122 	/* Case 1 - we will dup_anon_vma() from src into tgt. */
123 	if (!tgt_anon && src_anon) {
124 		struct vm_area_struct *copied_from = vmg->copied_from;
125 
126 		if (vma_is_fork_child(src))
127 			return false;
128 		if (vma_is_fork_child(copied_from))
129 			return false;
130 
131 		return true;
132 	}
133 	/* Case 2 - we will simply use tgt's anon_vma. */
134 	if (tgt_anon && !src_anon)
135 		return !vma_is_fork_child(tgt);
136 	/* Case 3 - the anon_vma's are already shared. */
137 	return src_anon == tgt_anon;
138 }
139 
140 /*
141  * init_multi_vma_prep() - Initializer for struct vma_prepare
142  * @vp: The vma_prepare struct
143  * @vma: The vma that will be altered once locked
144  * @vmg: The merge state that will be used to determine adjustment and VMA
145  *       removal.
146  */
147 static void init_multi_vma_prep(struct vma_prepare *vp,
148 				struct vm_area_struct *vma,
149 				struct vma_merge_struct *vmg)
150 {
151 	struct vm_area_struct *adjust;
152 	struct vm_area_struct **remove = &vp->remove;
153 
154 	memset(vp, 0, sizeof(struct vma_prepare));
155 	vp->vma = vma;
156 	vp->anon_vma = vma->anon_vma;
157 
158 	if (vmg && vmg->__remove_middle) {
159 		*remove = vmg->middle;
160 		remove = &vp->remove2;
161 	}
162 	if (vmg && vmg->__remove_next)
163 		*remove = vmg->next;
164 
165 	if (vmg && vmg->__adjust_middle_start)
166 		adjust = vmg->middle;
167 	else if (vmg && vmg->__adjust_next_start)
168 		adjust = vmg->next;
169 	else
170 		adjust = NULL;
171 
172 	vp->adj_next = adjust;
173 	if (!vp->anon_vma && adjust)
174 		vp->anon_vma = adjust->anon_vma;
175 
176 	VM_WARN_ON(vp->anon_vma && adjust && adjust->anon_vma &&
177 		   vp->anon_vma != adjust->anon_vma);
178 
179 	vp->file = vma->vm_file;
180 	if (vp->file)
181 		vp->mapping = vma->vm_file->f_mapping;
182 
183 	if (vmg && vmg->skip_vma_uprobe)
184 		vp->skip_vma_uprobe = true;
185 }
186 
187 /*
188  * Return true if we can merge this (vma_flags,anon_vma,file,vm_pgoff)
189  * in front of (at a lower virtual address and file offset than) the vma.
190  *
191  * We cannot merge two vmas if they have differently assigned (non-NULL)
192  * anon_vmas, nor if same anon_vma is assigned but offsets incompatible.
193  *
194  * We don't check here for the merged mmap wrapping around the end of pagecache
195  * indices (16TB on ia32) because do_mmap() does not permit mmap's which
196  * wrap, nor mmaps which cover the final page at index -1UL.
197  *
198  * We assume the vma may be removed as part of the merge.
199  */
200 static bool can_vma_merge_before(struct vma_merge_struct *vmg)
201 {
202 	pgoff_t pglen = PHYS_PFN(vmg->end - vmg->start);
203 
204 	if (is_mergeable_vma(vmg, /* merge_next = */ true) &&
205 	    is_mergeable_anon_vma(vmg, /* merge_next = */ true)) {
206 		if (vmg->next->vm_pgoff == vmg->pgoff + pglen)
207 			return true;
208 	}
209 
210 	return false;
211 }
212 
213 /*
214  * Return true if we can merge this (vma_flags,anon_vma,file,vm_pgoff)
215  * beyond (at a higher virtual address and file offset than) the vma.
216  *
217  * We cannot merge two vmas if they have differently assigned (non-NULL)
218  * anon_vmas, nor if same anon_vma is assigned but offsets incompatible.
219  *
220  * We assume that vma is not removed as part of the merge.
221  */
222 static bool can_vma_merge_after(struct vma_merge_struct *vmg)
223 {
224 	if (is_mergeable_vma(vmg, /* merge_next = */ false) &&
225 	    is_mergeable_anon_vma(vmg, /* merge_next = */ false)) {
226 		if (vmg->prev->vm_pgoff + vma_pages(vmg->prev) == vmg->pgoff)
227 			return true;
228 	}
229 	return false;
230 }
231 
232 static void __vma_link_file(struct vm_area_struct *vma,
233 			    struct address_space *mapping)
234 {
235 	if (vma_is_shared_maywrite(vma))
236 		mapping_allow_writable(mapping);
237 
238 	flush_dcache_mmap_lock(mapping);
239 	vma_interval_tree_insert(vma, &mapping->i_mmap);
240 	flush_dcache_mmap_unlock(mapping);
241 }
242 
243 /*
244  * Requires inode->i_mapping->i_mmap_rwsem
245  */
246 static void __remove_shared_vm_struct(struct vm_area_struct *vma,
247 				      struct address_space *mapping)
248 {
249 	if (vma_is_shared_maywrite(vma))
250 		mapping_unmap_writable(mapping);
251 
252 	flush_dcache_mmap_lock(mapping);
253 	vma_interval_tree_remove(vma, &mapping->i_mmap);
254 	flush_dcache_mmap_unlock(mapping);
255 }
256 
257 /*
258  * vma has some anon_vma assigned, and is already inserted on that
259  * anon_vma's interval trees.
260  *
261  * Before updating the vma's vm_start / vm_end / vm_pgoff fields, the
262  * vma must be removed from the anon_vma's interval trees using
263  * anon_vma_interval_tree_pre_update_vma().
264  *
265  * After the update, the vma will be reinserted using
266  * anon_vma_interval_tree_post_update_vma().
267  *
268  * The entire update must be protected by exclusive mmap_lock and by
269  * the root anon_vma's mutex.
270  */
271 static void
272 anon_vma_interval_tree_pre_update_vma(struct vm_area_struct *vma)
273 {
274 	struct anon_vma_chain *avc;
275 
276 	list_for_each_entry(avc, &vma->anon_vma_chain, same_vma)
277 		anon_vma_interval_tree_remove(avc, &avc->anon_vma->rb_root);
278 }
279 
280 static void
281 anon_vma_interval_tree_post_update_vma(struct vm_area_struct *vma)
282 {
283 	struct anon_vma_chain *avc;
284 
285 	list_for_each_entry(avc, &vma->anon_vma_chain, same_vma)
286 		anon_vma_interval_tree_insert(avc, &avc->anon_vma->rb_root);
287 }
288 
289 /*
290  * vma_prepare() - Helper function for handling locking VMAs prior to altering
291  * @vp: The initialized vma_prepare struct
292  */
293 static void vma_prepare(struct vma_prepare *vp)
294 {
295 	if (vp->file) {
296 		uprobe_munmap(vp->vma, vp->vma->vm_start, vp->vma->vm_end);
297 
298 		if (vp->adj_next)
299 			uprobe_munmap(vp->adj_next, vp->adj_next->vm_start,
300 				      vp->adj_next->vm_end);
301 
302 		i_mmap_lock_write(vp->mapping);
303 		if (vp->insert && vp->insert->vm_file) {
304 			/*
305 			 * Put into interval tree now, so instantiated pages
306 			 * are visible to arm/parisc __flush_dcache_page
307 			 * throughout; but we cannot insert into address
308 			 * space until vma start or end is updated.
309 			 */
310 			__vma_link_file(vp->insert,
311 					vp->insert->vm_file->f_mapping);
312 		}
313 	}
314 
315 	if (vp->anon_vma) {
316 		anon_vma_lock_write(vp->anon_vma);
317 		anon_vma_interval_tree_pre_update_vma(vp->vma);
318 		if (vp->adj_next)
319 			anon_vma_interval_tree_pre_update_vma(vp->adj_next);
320 	}
321 
322 	if (vp->file) {
323 		flush_dcache_mmap_lock(vp->mapping);
324 		vma_interval_tree_remove(vp->vma, &vp->mapping->i_mmap);
325 		if (vp->adj_next)
326 			vma_interval_tree_remove(vp->adj_next,
327 						 &vp->mapping->i_mmap);
328 	}
329 
330 }
331 
332 /*
333  * vma_complete- Helper function for handling the unlocking after altering VMAs,
334  * or for inserting a VMA.
335  *
336  * @vp: The vma_prepare struct
337  * @vmi: The vma iterator
338  * @mm: The mm_struct
339  */
340 static void vma_complete(struct vma_prepare *vp, struct vma_iterator *vmi,
341 			 struct mm_struct *mm)
342 {
343 	if (vp->file) {
344 		if (vp->adj_next)
345 			vma_interval_tree_insert(vp->adj_next,
346 						 &vp->mapping->i_mmap);
347 		vma_interval_tree_insert(vp->vma, &vp->mapping->i_mmap);
348 		flush_dcache_mmap_unlock(vp->mapping);
349 	}
350 
351 	if (vp->remove && vp->file) {
352 		__remove_shared_vm_struct(vp->remove, vp->mapping);
353 		if (vp->remove2)
354 			__remove_shared_vm_struct(vp->remove2, vp->mapping);
355 	} else if (vp->insert) {
356 		/*
357 		 * split_vma has split insert from vma, and needs
358 		 * us to insert it before dropping the locks
359 		 * (it may either follow vma or precede it).
360 		 */
361 		vma_iter_store_new(vmi, vp->insert);
362 		mm->map_count++;
363 	}
364 
365 	if (vp->anon_vma) {
366 		anon_vma_interval_tree_post_update_vma(vp->vma);
367 		if (vp->adj_next)
368 			anon_vma_interval_tree_post_update_vma(vp->adj_next);
369 		anon_vma_unlock_write(vp->anon_vma);
370 	}
371 
372 	if (vp->file) {
373 		i_mmap_unlock_write(vp->mapping);
374 
375 		if (!vp->skip_vma_uprobe) {
376 			uprobe_mmap(vp->vma);
377 
378 			if (vp->adj_next)
379 				uprobe_mmap(vp->adj_next);
380 		}
381 	}
382 
383 	if (vp->remove) {
384 again:
385 		vma_mark_detached(vp->remove);
386 		if (vp->file) {
387 			uprobe_munmap(vp->remove, vp->remove->vm_start,
388 				      vp->remove->vm_end);
389 			fput(vp->file);
390 		}
391 		if (vp->remove->anon_vma)
392 			unlink_anon_vmas(vp->remove);
393 		mm->map_count--;
394 		mpol_put(vma_policy(vp->remove));
395 		if (!vp->remove2)
396 			WARN_ON_ONCE(vp->vma->vm_end < vp->remove->vm_end);
397 		vm_area_free(vp->remove);
398 
399 		/*
400 		 * In mprotect's case 6 (see comments on vma_merge),
401 		 * we are removing both mid and next vmas
402 		 */
403 		if (vp->remove2) {
404 			vp->remove = vp->remove2;
405 			vp->remove2 = NULL;
406 			goto again;
407 		}
408 	}
409 	if (vp->insert && vp->file)
410 		uprobe_mmap(vp->insert);
411 }
412 
413 /*
414  * init_vma_prep() - Initializer wrapper for vma_prepare struct
415  * @vp: The vma_prepare struct
416  * @vma: The vma that will be altered once locked
417  */
418 static void init_vma_prep(struct vma_prepare *vp, struct vm_area_struct *vma)
419 {
420 	init_multi_vma_prep(vp, vma, NULL);
421 }
422 
423 /*
424  * Can the proposed VMA be merged with the left (previous) VMA taking into
425  * account the start position of the proposed range.
426  */
427 static bool can_vma_merge_left(struct vma_merge_struct *vmg)
428 
429 {
430 	return vmg->prev && vmg->prev->vm_end == vmg->start &&
431 		can_vma_merge_after(vmg);
432 }
433 
434 /*
435  * Can the proposed VMA be merged with the right (next) VMA taking into
436  * account the end position of the proposed range.
437  *
438  * In addition, if we can merge with the left VMA, ensure that left and right
439  * anon_vma's are also compatible.
440  */
441 static bool can_vma_merge_right(struct vma_merge_struct *vmg,
442 				bool can_merge_left)
443 {
444 	struct vm_area_struct *next = vmg->next;
445 	struct vm_area_struct *prev;
446 
447 	if (!next || vmg->end != next->vm_start || !can_vma_merge_before(vmg))
448 		return false;
449 
450 	if (!can_merge_left)
451 		return true;
452 
453 	/*
454 	 * If we can merge with prev (left) and next (right), indicating that
455 	 * each VMA's anon_vma is compatible with the proposed anon_vma, this
456 	 * does not mean prev and next are compatible with EACH OTHER.
457 	 *
458 	 * We therefore check this in addition to mergeability to either side.
459 	 */
460 	prev = vmg->prev;
461 	return !prev->anon_vma || !next->anon_vma ||
462 		prev->anon_vma == next->anon_vma;
463 }
464 
465 /*
466  * Close a vm structure and free it.
467  */
468 void remove_vma(struct vm_area_struct *vma)
469 {
470 	might_sleep();
471 	vma_close(vma);
472 	if (vma->vm_file)
473 		fput(vma->vm_file);
474 	mpol_put(vma_policy(vma));
475 	vm_area_free(vma);
476 }
477 
478 /*
479  * Get rid of page table information in the indicated region.
480  *
481  * Called with the mm semaphore held.
482  */
483 void unmap_region(struct unmap_desc *unmap)
484 {
485 	struct mm_struct *mm = unmap->first->vm_mm;
486 	struct mmu_gather tlb;
487 
488 	tlb_gather_mmu(&tlb, mm);
489 	update_hiwater_rss(mm);
490 	unmap_vmas(&tlb, unmap);
491 	mas_set(unmap->mas, unmap->tree_reset);
492 	free_pgtables(&tlb, unmap);
493 	tlb_finish_mmu(&tlb);
494 }
495 
496 /*
497  * __split_vma() bypasses sysctl_max_map_count checking.  We use this where it
498  * has already been checked or doesn't make sense to fail.
499  * VMA Iterator will point to the original VMA.
500  */
501 static __must_check int
502 __split_vma(struct vma_iterator *vmi, struct vm_area_struct *vma,
503 	    unsigned long addr, int new_below)
504 {
505 	struct vma_prepare vp;
506 	struct vm_area_struct *new;
507 	int err;
508 
509 	WARN_ON(vma->vm_start >= addr);
510 	WARN_ON(vma->vm_end <= addr);
511 
512 	if (vma->vm_ops && vma->vm_ops->may_split) {
513 		err = vma->vm_ops->may_split(vma, addr);
514 		if (err)
515 			return err;
516 	}
517 
518 	new = vm_area_dup(vma);
519 	if (!new)
520 		return -ENOMEM;
521 
522 	if (new_below) {
523 		new->vm_end = addr;
524 	} else {
525 		new->vm_start = addr;
526 		new->vm_pgoff += ((addr - vma->vm_start) >> PAGE_SHIFT);
527 	}
528 
529 	err = -ENOMEM;
530 	vma_iter_config(vmi, new->vm_start, new->vm_end);
531 	if (vma_iter_prealloc(vmi, new))
532 		goto out_free_vma;
533 
534 	err = vma_dup_policy(vma, new);
535 	if (err)
536 		goto out_free_vmi;
537 
538 	err = anon_vma_clone(new, vma, VMA_OP_SPLIT);
539 	if (err)
540 		goto out_free_mpol;
541 
542 	if (new->vm_file)
543 		get_file(new->vm_file);
544 
545 	if (new->vm_ops && new->vm_ops->open)
546 		new->vm_ops->open(new);
547 
548 	vma_start_write(vma);
549 	vma_start_write(new);
550 
551 	init_vma_prep(&vp, vma);
552 	vp.insert = new;
553 	vma_prepare(&vp);
554 
555 	/*
556 	 * Get rid of huge pages and shared page tables straddling the split
557 	 * boundary.
558 	 */
559 	vma_adjust_trans_huge(vma, vma->vm_start, addr, NULL);
560 	if (is_vm_hugetlb_page(vma))
561 		hugetlb_split(vma, addr);
562 
563 	if (new_below) {
564 		vma->vm_start = addr;
565 		vma->vm_pgoff += (addr - new->vm_start) >> PAGE_SHIFT;
566 	} else {
567 		vma->vm_end = addr;
568 	}
569 
570 	/* vma_complete stores the new vma */
571 	vma_complete(&vp, vmi, vma->vm_mm);
572 	validate_mm(vma->vm_mm);
573 
574 	/* Success. */
575 	if (new_below)
576 		vma_next(vmi);
577 	else
578 		vma_prev(vmi);
579 
580 	return 0;
581 
582 out_free_mpol:
583 	mpol_put(vma_policy(new));
584 out_free_vmi:
585 	vma_iter_free(vmi);
586 out_free_vma:
587 	vm_area_free(new);
588 	return err;
589 }
590 
591 /*
592  * Split a vma into two pieces at address 'addr', a new vma is allocated
593  * either for the first part or the tail.
594  */
595 static int split_vma(struct vma_iterator *vmi, struct vm_area_struct *vma,
596 		     unsigned long addr, int new_below)
597 {
598 	if (vma->vm_mm->map_count >= get_sysctl_max_map_count())
599 		return -ENOMEM;
600 
601 	return __split_vma(vmi, vma, addr, new_below);
602 }
603 
604 /*
605  * dup_anon_vma() - Helper function to duplicate anon_vma on VMA merge in the
606  * instance that the destination VMA has no anon_vma but the source does.
607  *
608  * @dst: The destination VMA
609  * @src: The source VMA
610  * @dup: Pointer to the destination VMA when successful.
611  *
612  * Returns: 0 on success.
613  */
614 static int dup_anon_vma(struct vm_area_struct *dst,
615 			struct vm_area_struct *src, struct vm_area_struct **dup)
616 {
617 	/*
618 	 * There are three cases to consider for correctly propagating
619 	 * anon_vma's on merge.
620 	 *
621 	 * The first is trivial - neither VMA has anon_vma, we need not do
622 	 * anything.
623 	 *
624 	 * The second where both have anon_vma is also a no-op, as they must
625 	 * then be the same, so there is simply nothing to copy.
626 	 *
627 	 * Here we cover the third - if the destination VMA has no anon_vma,
628 	 * that is it is unfaulted, we need to ensure that the newly merged
629 	 * range is referenced by the anon_vma's of the source.
630 	 */
631 	if (src->anon_vma && !dst->anon_vma) {
632 		int ret;
633 
634 		vma_assert_write_locked(dst);
635 		dst->anon_vma = src->anon_vma;
636 		ret = anon_vma_clone(dst, src, VMA_OP_MERGE_UNFAULTED);
637 		if (ret)
638 			return ret;
639 
640 		*dup = dst;
641 	}
642 
643 	return 0;
644 }
645 
646 #ifdef CONFIG_DEBUG_VM_MAPLE_TREE
647 void validate_mm(struct mm_struct *mm)
648 {
649 	int bug = 0;
650 	int i = 0;
651 	struct vm_area_struct *vma;
652 	VMA_ITERATOR(vmi, mm, 0);
653 
654 	mt_validate(&mm->mm_mt);
655 	for_each_vma(vmi, vma) {
656 #ifdef CONFIG_DEBUG_VM_RB
657 		struct anon_vma *anon_vma = vma->anon_vma;
658 		struct anon_vma_chain *avc;
659 #endif
660 		unsigned long vmi_start, vmi_end;
661 		bool warn = 0;
662 
663 		vmi_start = vma_iter_addr(&vmi);
664 		vmi_end = vma_iter_end(&vmi);
665 		if (VM_WARN_ON_ONCE_MM(vma->vm_end != vmi_end, mm))
666 			warn = 1;
667 
668 		if (VM_WARN_ON_ONCE_MM(vma->vm_start != vmi_start, mm))
669 			warn = 1;
670 
671 		if (warn) {
672 			pr_emerg("issue in %s\n", current->comm);
673 			dump_stack();
674 			dump_vma(vma);
675 			pr_emerg("tree range: %px start %lx end %lx\n", vma,
676 				 vmi_start, vmi_end - 1);
677 			vma_iter_dump_tree(&vmi);
678 		}
679 
680 #ifdef CONFIG_DEBUG_VM_RB
681 		if (anon_vma) {
682 			anon_vma_lock_read(anon_vma);
683 			list_for_each_entry(avc, &vma->anon_vma_chain, same_vma)
684 				anon_vma_interval_tree_verify(avc);
685 			anon_vma_unlock_read(anon_vma);
686 		}
687 #endif
688 		/* Check for a infinite loop */
689 		if (++i > mm->map_count + 10) {
690 			i = -1;
691 			break;
692 		}
693 	}
694 	if (i != mm->map_count) {
695 		pr_emerg("map_count %d vma iterator %d\n", mm->map_count, i);
696 		bug = 1;
697 	}
698 	VM_BUG_ON_MM(bug, mm);
699 }
700 #endif /* CONFIG_DEBUG_VM_MAPLE_TREE */
701 
702 /*
703  * Based on the vmg flag indicating whether we need to adjust the vm_start field
704  * for the middle or next VMA, we calculate what the range of the newly adjusted
705  * VMA ought to be, and set the VMA's range accordingly.
706  */
707 static void vmg_adjust_set_range(struct vma_merge_struct *vmg)
708 {
709 	struct vm_area_struct *adjust;
710 	pgoff_t pgoff;
711 
712 	if (vmg->__adjust_middle_start) {
713 		adjust = vmg->middle;
714 		pgoff = adjust->vm_pgoff + PHYS_PFN(vmg->end - adjust->vm_start);
715 	} else if (vmg->__adjust_next_start) {
716 		adjust = vmg->next;
717 		pgoff = adjust->vm_pgoff - PHYS_PFN(adjust->vm_start - vmg->end);
718 	} else {
719 		return;
720 	}
721 
722 	vma_set_range(adjust, vmg->end, adjust->vm_end, pgoff);
723 }
724 
725 /*
726  * Actually perform the VMA merge operation.
727  *
728  * IMPORTANT: We guarantee that, should vmg->give_up_on_oom is set, to not
729  * modify any VMAs or cause inconsistent state should an OOM condition arise.
730  *
731  * Returns 0 on success, or an error value on failure.
732  */
733 static int commit_merge(struct vma_merge_struct *vmg)
734 {
735 	struct vm_area_struct *vma;
736 	struct vma_prepare vp;
737 
738 	if (vmg->__adjust_next_start) {
739 		/* We manipulate middle and adjust next, which is the target. */
740 		vma = vmg->middle;
741 		vma_iter_config(vmg->vmi, vmg->end, vmg->next->vm_end);
742 	} else {
743 		vma = vmg->target;
744 		 /* Note: vma iterator must be pointing to 'start'. */
745 		vma_iter_config(vmg->vmi, vmg->start, vmg->end);
746 	}
747 
748 	init_multi_vma_prep(&vp, vma, vmg);
749 
750 	/*
751 	 * If vmg->give_up_on_oom is set, we're safe, because we don't actually
752 	 * manipulate any VMAs until we succeed at preallocation.
753 	 *
754 	 * Past this point, we will not return an error.
755 	 */
756 	if (vma_iter_prealloc(vmg->vmi, vma))
757 		return -ENOMEM;
758 
759 	vma_prepare(&vp);
760 	/*
761 	 * THP pages may need to do additional splits if we increase
762 	 * middle->vm_start.
763 	 */
764 	vma_adjust_trans_huge(vma, vmg->start, vmg->end,
765 			      vmg->__adjust_middle_start ? vmg->middle : NULL);
766 	vma_set_range(vma, vmg->start, vmg->end, vmg->pgoff);
767 	vmg_adjust_set_range(vmg);
768 	vma_iter_store_overwrite(vmg->vmi, vmg->target);
769 
770 	vma_complete(&vp, vmg->vmi, vma->vm_mm);
771 
772 	return 0;
773 }
774 
775 /* We can only remove VMAs when merging if they do not have a close hook. */
776 static bool can_merge_remove_vma(struct vm_area_struct *vma)
777 {
778 	return !vma->vm_ops || !vma->vm_ops->close;
779 }
780 
781 /*
782  * vma_merge_existing_range - Attempt to merge VMAs based on a VMA having its
783  * attributes modified.
784  *
785  * @vmg: Describes the modifications being made to a VMA and associated
786  *       metadata.
787  *
788  * When the attributes of a range within a VMA change, then it might be possible
789  * for immediately adjacent VMAs to be merged into that VMA due to having
790  * identical properties.
791  *
792  * This function checks for the existence of any such mergeable VMAs and updates
793  * the maple tree describing the @vmg->middle->vm_mm address space to account
794  * for this, as well as any VMAs shrunk/expanded/deleted as a result of this
795  * merge.
796  *
797  * As part of this operation, if a merge occurs, the @vmg object will have its
798  * vma, start, end, and pgoff fields modified to execute the merge. Subsequent
799  * calls to this function should reset these fields.
800  *
801  * Returns: The merged VMA if merge succeeds, or NULL otherwise.
802  *
803  * ASSUMPTIONS:
804  * - The caller must assign the VMA to be modified to @vmg->middle.
805  * - The caller must have set @vmg->prev to the previous VMA, if there is one.
806  * - The caller must not set @vmg->next, as we determine this.
807  * - The caller must hold a WRITE lock on the mm_struct->mmap_lock.
808  * - vmi must be positioned within [@vmg->middle->vm_start, @vmg->middle->vm_end).
809  */
810 static __must_check struct vm_area_struct *vma_merge_existing_range(
811 		struct vma_merge_struct *vmg)
812 {
813 	vma_flags_t sticky_flags = vma_flags_and_mask(&vmg->vma_flags,
814 						      VMA_STICKY_FLAGS);
815 	struct vm_area_struct *middle = vmg->middle;
816 	struct vm_area_struct *prev = vmg->prev;
817 	struct vm_area_struct *next;
818 	struct vm_area_struct *anon_dup = NULL;
819 	unsigned long start = vmg->start;
820 	unsigned long end = vmg->end;
821 	bool left_side = middle && start == middle->vm_start;
822 	bool right_side = middle && end == middle->vm_end;
823 	int err = 0;
824 	bool merge_left, merge_right, merge_both;
825 
826 	mmap_assert_write_locked(vmg->mm);
827 	VM_WARN_ON_VMG(!middle, vmg); /* We are modifying a VMA, so caller must specify. */
828 	VM_WARN_ON_VMG(vmg->next, vmg); /* We set this. */
829 	VM_WARN_ON_VMG(prev && start <= prev->vm_start, vmg);
830 	VM_WARN_ON_VMG(start >= end, vmg);
831 
832 	/*
833 	 * If middle == prev, then we are offset into a VMA. Otherwise, if we are
834 	 * not, we must span a portion of the VMA.
835 	 */
836 	VM_WARN_ON_VMG(middle &&
837 		       ((middle != prev && vmg->start != middle->vm_start) ||
838 			vmg->end > middle->vm_end), vmg);
839 	/* The vmi must be positioned within vmg->middle. */
840 	VM_WARN_ON_VMG(middle &&
841 		       !(vma_iter_addr(vmg->vmi) >= middle->vm_start &&
842 			 vma_iter_addr(vmg->vmi) < middle->vm_end), vmg);
843 	/* An existing merge can never be used by the mremap() logic. */
844 	VM_WARN_ON_VMG(vmg->copied_from, vmg);
845 
846 	vmg->state = VMA_MERGE_NOMERGE;
847 
848 	/*
849 	 * If a special mapping or if the range being modified is neither at the
850 	 * furthermost left or right side of the VMA, then we have no chance of
851 	 * merging and should abort.
852 	 */
853 	if (vma_flags_test_any_mask(&vmg->vma_flags, VMA_SPECIAL_FLAGS) ||
854 	    (!left_side && !right_side))
855 		return NULL;
856 
857 	if (left_side)
858 		merge_left = can_vma_merge_left(vmg);
859 	else
860 		merge_left = false;
861 
862 	if (right_side) {
863 		next = vmg->next = vma_iter_next_range(vmg->vmi);
864 		vma_iter_prev_range(vmg->vmi);
865 
866 		merge_right = can_vma_merge_right(vmg, merge_left);
867 	} else {
868 		merge_right = false;
869 		next = NULL;
870 	}
871 
872 	if (merge_left)		/* If merging prev, position iterator there. */
873 		vma_prev(vmg->vmi);
874 	else if (!merge_right)	/* If we have nothing to merge, abort. */
875 		return NULL;
876 
877 	merge_both = merge_left && merge_right;
878 	/* If we span the entire VMA, a merge implies it will be deleted. */
879 	vmg->__remove_middle = left_side && right_side;
880 
881 	/*
882 	 * If we need to remove middle in its entirety but are unable to do so,
883 	 * we have no sensible recourse but to abort the merge.
884 	 */
885 	if (vmg->__remove_middle && !can_merge_remove_vma(middle))
886 		return NULL;
887 
888 	/*
889 	 * If we merge both VMAs, then next is also deleted. This implies
890 	 * merge_will_delete_vma also.
891 	 */
892 	vmg->__remove_next = merge_both;
893 
894 	/*
895 	 * If we cannot delete next, then we can reduce the operation to merging
896 	 * prev and middle (thereby deleting middle).
897 	 */
898 	if (vmg->__remove_next && !can_merge_remove_vma(next)) {
899 		vmg->__remove_next = false;
900 		merge_right = false;
901 		merge_both = false;
902 	}
903 
904 	/* No matter what happens, we will be adjusting middle. */
905 	vma_start_write(middle);
906 
907 	if (merge_right) {
908 		vma_flags_t next_sticky;
909 
910 		vma_start_write(next);
911 		vmg->target = next;
912 		next_sticky = vma_flags_and_mask(&next->flags, VMA_STICKY_FLAGS);
913 		vma_flags_set_mask(&sticky_flags, next_sticky);
914 	}
915 
916 	if (merge_left) {
917 		vma_flags_t prev_sticky;
918 
919 		vma_start_write(prev);
920 		vmg->target = prev;
921 
922 		prev_sticky = vma_flags_and_mask(&prev->flags, VMA_STICKY_FLAGS);
923 		vma_flags_set_mask(&sticky_flags, prev_sticky);
924 	}
925 
926 	if (merge_both) {
927 		/*
928 		 * |<-------------------->|
929 		 * |-------********-------|
930 		 *   prev   middle   next
931 		 *  extend  delete  delete
932 		 */
933 
934 		vmg->start = prev->vm_start;
935 		vmg->end = next->vm_end;
936 		vmg->pgoff = prev->vm_pgoff;
937 
938 		/*
939 		 * We already ensured anon_vma compatibility above, so now it's
940 		 * simply a case of, if prev has no anon_vma object, which of
941 		 * next or middle contains the anon_vma we must duplicate.
942 		 */
943 		err = dup_anon_vma(prev, next->anon_vma ? next : middle,
944 				   &anon_dup);
945 	} else if (merge_left) {
946 		/*
947 		 * |<------------>|      OR
948 		 * |<----------------->|
949 		 * |-------*************
950 		 *   prev     middle
951 		 *  extend shrink/delete
952 		 */
953 
954 		vmg->start = prev->vm_start;
955 		vmg->pgoff = prev->vm_pgoff;
956 
957 		if (!vmg->__remove_middle)
958 			vmg->__adjust_middle_start = true;
959 
960 		err = dup_anon_vma(prev, middle, &anon_dup);
961 	} else { /* merge_right */
962 		/*
963 		 *     |<------------->| OR
964 		 * |<----------------->|
965 		 * *************-------|
966 		 *    middle     next
967 		 * shrink/delete extend
968 		 */
969 
970 		pgoff_t pglen = PHYS_PFN(vmg->end - vmg->start);
971 
972 		VM_WARN_ON_VMG(!merge_right, vmg);
973 		/* If we are offset into a VMA, then prev must be middle. */
974 		VM_WARN_ON_VMG(vmg->start > middle->vm_start && prev && middle != prev, vmg);
975 
976 		if (vmg->__remove_middle) {
977 			vmg->end = next->vm_end;
978 			vmg->pgoff = next->vm_pgoff - pglen;
979 		} else {
980 			/* We shrink middle and expand next. */
981 			vmg->__adjust_next_start = true;
982 			vmg->start = middle->vm_start;
983 			vmg->end = start;
984 			vmg->pgoff = middle->vm_pgoff;
985 		}
986 
987 		err = dup_anon_vma(next, middle, &anon_dup);
988 	}
989 
990 	if (err || commit_merge(vmg))
991 		goto abort;
992 
993 	vma_set_flags_mask(vmg->target, sticky_flags);
994 	khugepaged_enter_vma(vmg->target, vmg->vm_flags);
995 	vmg->state = VMA_MERGE_SUCCESS;
996 	return vmg->target;
997 
998 abort:
999 	vma_iter_set(vmg->vmi, start);
1000 	vma_iter_load(vmg->vmi);
1001 
1002 	if (anon_dup)
1003 		unlink_anon_vmas(anon_dup);
1004 
1005 	/*
1006 	 * This means we have failed to clone anon_vma's correctly, but no
1007 	 * actual changes to VMAs have occurred, so no harm no foul - if the
1008 	 * user doesn't want this reported and instead just wants to give up on
1009 	 * the merge, allow it.
1010 	 */
1011 	if (!vmg->give_up_on_oom)
1012 		vmg->state = VMA_MERGE_ERROR_NOMEM;
1013 	return NULL;
1014 }
1015 
1016 /*
1017  * vma_merge_new_range - Attempt to merge a new VMA into address space
1018  *
1019  * @vmg: Describes the VMA we are adding, in the range @vmg->start to @vmg->end
1020  *       (exclusive), which we try to merge with any adjacent VMAs if possible.
1021  *
1022  * We are about to add a VMA to the address space starting at @vmg->start and
1023  * ending at @vmg->end. There are three different possible scenarios:
1024  *
1025  * 1. There is a VMA with identical properties immediately adjacent to the
1026  *    proposed new VMA [@vmg->start, @vmg->end) either before or after it -
1027  *    EXPAND that VMA:
1028  *
1029  * Proposed:       |-----|  or  |-----|
1030  * Existing:  |----|                  |----|
1031  *
1032  * 2. There are VMAs with identical properties immediately adjacent to the
1033  *    proposed new VMA [@vmg->start, @vmg->end) both before AND after it -
1034  *    EXPAND the former and REMOVE the latter:
1035  *
1036  * Proposed:       |-----|
1037  * Existing:  |----|     |----|
1038  *
1039  * 3. There are no VMAs immediately adjacent to the proposed new VMA or those
1040  *    VMAs do not have identical attributes - NO MERGE POSSIBLE.
1041  *
1042  * In instances where we can merge, this function returns the expanded VMA which
1043  * will have its range adjusted accordingly and the underlying maple tree also
1044  * adjusted.
1045  *
1046  * Returns: In instances where no merge was possible, NULL. Otherwise, a pointer
1047  *          to the VMA we expanded.
1048  *
1049  * This function adjusts @vmg to provide @vmg->next if not already specified,
1050  * and adjusts [@vmg->start, @vmg->end) to span the expanded range.
1051  *
1052  * ASSUMPTIONS:
1053  * - The caller must hold a WRITE lock on the mm_struct->mmap_lock.
1054  * - The caller must have determined that [@vmg->start, @vmg->end) is empty,
1055      other than VMAs that will be unmapped should the operation succeed.
1056  * - The caller must have specified the previous vma in @vmg->prev.
1057  * - The caller must have specified the next vma in @vmg->next.
1058  * - The caller must have positioned the vmi at or before the gap.
1059  */
1060 struct vm_area_struct *vma_merge_new_range(struct vma_merge_struct *vmg)
1061 {
1062 	struct vm_area_struct *prev = vmg->prev;
1063 	struct vm_area_struct *next = vmg->next;
1064 	unsigned long end = vmg->end;
1065 	bool can_merge_left, can_merge_right;
1066 
1067 	mmap_assert_write_locked(vmg->mm);
1068 	VM_WARN_ON_VMG(vmg->middle, vmg);
1069 	VM_WARN_ON_VMG(vmg->target, vmg);
1070 	/* vmi must point at or before the gap. */
1071 	VM_WARN_ON_VMG(vma_iter_addr(vmg->vmi) > end, vmg);
1072 
1073 	vmg->state = VMA_MERGE_NOMERGE;
1074 
1075 	/* Special VMAs are unmergeable, also if no prev/next. */
1076 	if (vma_flags_test_any_mask(&vmg->vma_flags, VMA_SPECIAL_FLAGS) ||
1077 	    (!prev && !next))
1078 		return NULL;
1079 
1080 	can_merge_left = can_vma_merge_left(vmg);
1081 	can_merge_right = !vmg->just_expand && can_vma_merge_right(vmg, can_merge_left);
1082 
1083 	/* If we can merge with the next VMA, adjust vmg accordingly. */
1084 	if (can_merge_right) {
1085 		vmg->end = next->vm_end;
1086 		vmg->target = next;
1087 	}
1088 
1089 	/* If we can merge with the previous VMA, adjust vmg accordingly. */
1090 	if (can_merge_left) {
1091 		vmg->start = prev->vm_start;
1092 		vmg->target = prev;
1093 		vmg->pgoff = prev->vm_pgoff;
1094 
1095 		/*
1096 		 * If this merge would result in removal of the next VMA but we
1097 		 * are not permitted to do so, reduce the operation to merging
1098 		 * prev and vma.
1099 		 */
1100 		if (can_merge_right && !can_merge_remove_vma(next))
1101 			vmg->end = end;
1102 
1103 		/* In expand-only case we are already positioned at prev. */
1104 		if (!vmg->just_expand) {
1105 			/* Equivalent to going to the previous range. */
1106 			vma_prev(vmg->vmi);
1107 		}
1108 	}
1109 
1110 	/*
1111 	 * Now try to expand adjacent VMA(s). This takes care of removing the
1112 	 * following VMA if we have VMAs on both sides.
1113 	 */
1114 	if (vmg->target && !vma_expand(vmg)) {
1115 		khugepaged_enter_vma(vmg->target, vmg->vm_flags);
1116 		vmg->state = VMA_MERGE_SUCCESS;
1117 		return vmg->target;
1118 	}
1119 
1120 	return NULL;
1121 }
1122 
1123 /*
1124  * vma_merge_copied_range - Attempt to merge a VMA that is being copied by
1125  * mremap()
1126  *
1127  * @vmg: Describes the VMA we are adding, in the copied-to range @vmg->start to
1128  *       @vmg->end (exclusive), which we try to merge with any adjacent VMAs if
1129  *       possible.
1130  *
1131  * vmg->prev, next, start, end, pgoff should all be relative to the COPIED TO
1132  * range, i.e. the target range for the VMA.
1133  *
1134  * Returns: In instances where no merge was possible, NULL. Otherwise, a pointer
1135  *          to the VMA we expanded.
1136  *
1137  * ASSUMPTIONS: Same as vma_merge_new_range(), except vmg->middle must contain
1138  *              the copied-from VMA.
1139  */
1140 static struct vm_area_struct *vma_merge_copied_range(struct vma_merge_struct *vmg)
1141 {
1142 	/* We must have a copied-from VMA. */
1143 	VM_WARN_ON_VMG(!vmg->middle, vmg);
1144 
1145 	vmg->copied_from = vmg->middle;
1146 	vmg->middle = NULL;
1147 	return vma_merge_new_range(vmg);
1148 }
1149 
1150 /*
1151  * vma_expand - Expand an existing VMA
1152  *
1153  * @vmg: Describes a VMA expansion operation.
1154  *
1155  * Expand @vma to vmg->start and vmg->end.  Can expand off the start and end.
1156  * Will expand over vmg->next if it's different from vmg->target and vmg->end ==
1157  * vmg->next->vm_end.  Checking if the vmg->target can expand and merge with
1158  * vmg->next needs to be handled by the caller.
1159  *
1160  * Returns: 0 on success.
1161  *
1162  * ASSUMPTIONS:
1163  * - The caller must hold a WRITE lock on the mm_struct->mmap_lock.
1164  * - The caller must have set @vmg->target and @vmg->next.
1165  */
1166 int vma_expand(struct vma_merge_struct *vmg)
1167 {
1168 	struct vm_area_struct *anon_dup = NULL;
1169 	struct vm_area_struct *target = vmg->target;
1170 	struct vm_area_struct *next = vmg->next;
1171 	bool remove_next = false;
1172 	vma_flags_t sticky_flags =
1173 		vma_flags_and_mask(&vmg->vma_flags, VMA_STICKY_FLAGS);
1174 	vma_flags_t target_sticky;
1175 	int ret = 0;
1176 
1177 	mmap_assert_write_locked(vmg->mm);
1178 	vma_start_write(target);
1179 
1180 	target_sticky = vma_flags_and_mask(&target->flags, VMA_STICKY_FLAGS);
1181 
1182 	if (next && target != next && vmg->end == next->vm_end)
1183 		remove_next = true;
1184 
1185 	/* We must have a target. */
1186 	VM_WARN_ON_VMG(!target, vmg);
1187 	/* This should have already been checked by this point. */
1188 	VM_WARN_ON_VMG(remove_next && !can_merge_remove_vma(next), vmg);
1189 	/* Not merging but overwriting any part of next is not handled. */
1190 	VM_WARN_ON_VMG(next && !remove_next &&
1191 		       next != target && vmg->end > next->vm_start, vmg);
1192 	/* Only handles expanding. */
1193 	VM_WARN_ON_VMG(target->vm_start < vmg->start ||
1194 		       target->vm_end > vmg->end, vmg);
1195 
1196 	vma_flags_set_mask(&sticky_flags, target_sticky);
1197 
1198 	/*
1199 	 * If we are removing the next VMA or copying from a VMA
1200 	 * (e.g. mremap()'ing), we must propagate anon_vma state.
1201 	 *
1202 	 * Note that, by convention, callers ignore OOM for this case, so
1203 	 * we don't need to account for vmg->give_up_on_mm here.
1204 	 */
1205 	if (remove_next)
1206 		ret = dup_anon_vma(target, next, &anon_dup);
1207 	if (!ret && vmg->copied_from)
1208 		ret = dup_anon_vma(target, vmg->copied_from, &anon_dup);
1209 	if (ret)
1210 		return ret;
1211 
1212 	if (remove_next) {
1213 		vma_flags_t next_sticky;
1214 
1215 		vma_start_write(next);
1216 		vmg->__remove_next = true;
1217 
1218 		next_sticky = vma_flags_and_mask(&next->flags, VMA_STICKY_FLAGS);
1219 		vma_flags_set_mask(&sticky_flags, next_sticky);
1220 	}
1221 	if (commit_merge(vmg))
1222 		goto nomem;
1223 
1224 	vma_set_flags_mask(target, sticky_flags);
1225 	return 0;
1226 
1227 nomem:
1228 	if (anon_dup)
1229 		unlink_anon_vmas(anon_dup);
1230 	/*
1231 	 * If the user requests that we just give upon OOM, we are safe to do so
1232 	 * here, as commit merge provides this contract to us. Nothing has been
1233 	 * changed - no harm no foul, just don't report it.
1234 	 */
1235 	if (!vmg->give_up_on_oom)
1236 		vmg->state = VMA_MERGE_ERROR_NOMEM;
1237 	return -ENOMEM;
1238 }
1239 
1240 /*
1241  * vma_shrink() - Reduce an existing VMAs memory area
1242  * @vmi: The vma iterator
1243  * @vma: The VMA to modify
1244  * @start: The new start
1245  * @end: The new end
1246  *
1247  * Returns: 0 on success, -ENOMEM otherwise
1248  */
1249 int vma_shrink(struct vma_iterator *vmi, struct vm_area_struct *vma,
1250 	       unsigned long start, unsigned long end, pgoff_t pgoff)
1251 {
1252 	struct vma_prepare vp;
1253 
1254 	WARN_ON((vma->vm_start != start) && (vma->vm_end != end));
1255 
1256 	if (vma->vm_start < start)
1257 		vma_iter_config(vmi, vma->vm_start, start);
1258 	else
1259 		vma_iter_config(vmi, end, vma->vm_end);
1260 
1261 	if (vma_iter_prealloc(vmi, NULL))
1262 		return -ENOMEM;
1263 
1264 	vma_start_write(vma);
1265 
1266 	init_vma_prep(&vp, vma);
1267 	vma_prepare(&vp);
1268 	vma_adjust_trans_huge(vma, start, end, NULL);
1269 
1270 	vma_iter_clear(vmi);
1271 	vma_set_range(vma, start, end, pgoff);
1272 	vma_complete(&vp, vmi, vma->vm_mm);
1273 	validate_mm(vma->vm_mm);
1274 	return 0;
1275 }
1276 
1277 static inline void vms_clear_ptes(struct vma_munmap_struct *vms,
1278 		    struct ma_state *mas_detach, bool mm_wr_locked)
1279 {
1280 	struct unmap_desc unmap = {
1281 		.mas = mas_detach,
1282 		.first = vms->vma,
1283 		/* start and end may be different if there is no prev or next vma. */
1284 		.pg_start = vms->unmap_start,
1285 		.pg_end = vms->unmap_end,
1286 		.vma_start = vms->start,
1287 		.vma_end = vms->end,
1288 		/*
1289 		 * The tree limits and reset differ from the normal case since it's a
1290 		 * side-tree
1291 		 */
1292 		.tree_reset = 1,
1293 		.tree_end = vms->vma_count,
1294 		/*
1295 		 * We can free page tables without write-locking mmap_lock because VMAs
1296 		 * were isolated before we downgraded mmap_lock.
1297 		 */
1298 		.mm_wr_locked = mm_wr_locked,
1299 	};
1300 
1301 	if (!vms->clear_ptes) /* Nothing to do */
1302 		return;
1303 
1304 	mas_set(mas_detach, 1);
1305 	unmap_region(&unmap);
1306 	vms->clear_ptes = false;
1307 }
1308 
1309 static void vms_clean_up_area(struct vma_munmap_struct *vms,
1310 		struct ma_state *mas_detach)
1311 {
1312 	struct vm_area_struct *vma;
1313 
1314 	if (!vms->nr_pages)
1315 		return;
1316 
1317 	vms_clear_ptes(vms, mas_detach, true);
1318 	mas_set(mas_detach, 0);
1319 	mas_for_each(mas_detach, vma, ULONG_MAX)
1320 		vma_close(vma);
1321 }
1322 
1323 /*
1324  * vms_complete_munmap_vmas() - Finish the munmap() operation
1325  * @vms: The vma munmap struct
1326  * @mas_detach: The maple state of the detached vmas
1327  *
1328  * This updates the mm_struct, unmaps the region, frees the resources
1329  * used for the munmap() and may downgrade the lock - if requested.  Everything
1330  * needed to be done once the vma maple tree is updated.
1331  */
1332 static void vms_complete_munmap_vmas(struct vma_munmap_struct *vms,
1333 		struct ma_state *mas_detach)
1334 {
1335 	struct vm_area_struct *vma;
1336 	struct mm_struct *mm;
1337 
1338 	mm = current->mm;
1339 	mm->map_count -= vms->vma_count;
1340 	mm->locked_vm -= vms->locked_vm;
1341 	if (vms->unlock)
1342 		mmap_write_downgrade(mm);
1343 
1344 	if (!vms->nr_pages)
1345 		return;
1346 
1347 	vms_clear_ptes(vms, mas_detach, !vms->unlock);
1348 	/* Update high watermark before we lower total_vm */
1349 	update_hiwater_vm(mm);
1350 	/* Stat accounting */
1351 	WRITE_ONCE(mm->total_vm, READ_ONCE(mm->total_vm) - vms->nr_pages);
1352 	/* Paranoid bookkeeping */
1353 	VM_WARN_ON(vms->exec_vm > mm->exec_vm);
1354 	VM_WARN_ON(vms->stack_vm > mm->stack_vm);
1355 	VM_WARN_ON(vms->data_vm > mm->data_vm);
1356 	mm->exec_vm -= vms->exec_vm;
1357 	mm->stack_vm -= vms->stack_vm;
1358 	mm->data_vm -= vms->data_vm;
1359 
1360 	/* Remove and clean up vmas */
1361 	mas_set(mas_detach, 0);
1362 	mas_for_each(mas_detach, vma, ULONG_MAX)
1363 		remove_vma(vma);
1364 
1365 	vm_unacct_memory(vms->nr_accounted);
1366 	validate_mm(mm);
1367 	if (vms->unlock)
1368 		mmap_read_unlock(mm);
1369 
1370 	__mt_destroy(mas_detach->tree);
1371 }
1372 
1373 /*
1374  * reattach_vmas() - Undo any munmap work and free resources
1375  * @mas_detach: The maple state with the detached maple tree
1376  *
1377  * Reattach any detached vmas and free up the maple tree used to track the vmas.
1378  */
1379 static void reattach_vmas(struct ma_state *mas_detach)
1380 {
1381 	struct vm_area_struct *vma;
1382 
1383 	mas_set(mas_detach, 0);
1384 	mas_for_each(mas_detach, vma, ULONG_MAX)
1385 		vma_mark_attached(vma);
1386 
1387 	__mt_destroy(mas_detach->tree);
1388 }
1389 
1390 /*
1391  * vms_gather_munmap_vmas() - Put all VMAs within a range into a maple tree
1392  * for removal at a later date.  Handles splitting first and last if necessary
1393  * and marking the vmas as isolated.
1394  *
1395  * @vms: The vma munmap struct
1396  * @mas_detach: The maple state tracking the detached tree
1397  *
1398  * Return: 0 on success, error otherwise
1399  */
1400 static int vms_gather_munmap_vmas(struct vma_munmap_struct *vms,
1401 		struct ma_state *mas_detach)
1402 {
1403 	struct vm_area_struct *next = NULL;
1404 	int error;
1405 
1406 	/*
1407 	 * If we need to split any vma, do it now to save pain later.
1408 	 * Does it split the first one?
1409 	 */
1410 	if (vms->start > vms->vma->vm_start) {
1411 
1412 		/*
1413 		 * Make sure that map_count on return from munmap() will
1414 		 * not exceed its limit; but let map_count go just above
1415 		 * its limit temporarily, to help free resources as expected.
1416 		 */
1417 		if (vms->end < vms->vma->vm_end &&
1418 		    vms->vma->vm_mm->map_count >= get_sysctl_max_map_count()) {
1419 			error = -ENOMEM;
1420 			goto map_count_exceeded;
1421 		}
1422 
1423 		/* Don't bother splitting the VMA if we can't unmap it anyway */
1424 		if (vma_is_sealed(vms->vma)) {
1425 			error = -EPERM;
1426 			goto start_split_failed;
1427 		}
1428 
1429 		error = __split_vma(vms->vmi, vms->vma, vms->start, 1);
1430 		if (error)
1431 			goto start_split_failed;
1432 	}
1433 	vms->prev = vma_prev(vms->vmi);
1434 	if (vms->prev)
1435 		vms->unmap_start = vms->prev->vm_end;
1436 
1437 	/*
1438 	 * Detach a range of VMAs from the mm. Using next as a temp variable as
1439 	 * it is always overwritten.
1440 	 */
1441 	for_each_vma_range(*(vms->vmi), next, vms->end) {
1442 		long nrpages;
1443 
1444 		if (vma_is_sealed(next)) {
1445 			error = -EPERM;
1446 			goto modify_vma_failed;
1447 		}
1448 		/* Does it split the end? */
1449 		if (next->vm_end > vms->end) {
1450 			error = __split_vma(vms->vmi, next, vms->end, 0);
1451 			if (error)
1452 				goto end_split_failed;
1453 		}
1454 		vma_start_write(next);
1455 		mas_set(mas_detach, vms->vma_count++);
1456 		error = mas_store_gfp(mas_detach, next, GFP_KERNEL);
1457 		if (error)
1458 			goto munmap_gather_failed;
1459 
1460 		vma_mark_detached(next);
1461 		nrpages = vma_pages(next);
1462 
1463 		vms->nr_pages += nrpages;
1464 		if (vma_test(next, VMA_LOCKED_BIT))
1465 			vms->locked_vm += nrpages;
1466 
1467 		if (vma_test(next, VMA_ACCOUNT_BIT))
1468 			vms->nr_accounted += nrpages;
1469 
1470 		if (is_exec_mapping(next->vm_flags))
1471 			vms->exec_vm += nrpages;
1472 		else if (is_stack_mapping(next->vm_flags))
1473 			vms->stack_vm += nrpages;
1474 		else if (is_data_mapping_vma_flags(&next->flags))
1475 			vms->data_vm += nrpages;
1476 
1477 		if (vms->uf) {
1478 			/*
1479 			 * If userfaultfd_unmap_prep returns an error the vmas
1480 			 * will remain split, but userland will get a
1481 			 * highly unexpected error anyway. This is no
1482 			 * different than the case where the first of the two
1483 			 * __split_vma fails, but we don't undo the first
1484 			 * split, despite we could. This is unlikely enough
1485 			 * failure that it's not worth optimizing it for.
1486 			 */
1487 			error = userfaultfd_unmap_prep(next, vms->start,
1488 						       vms->end, vms->uf);
1489 			if (error)
1490 				goto userfaultfd_error;
1491 		}
1492 #ifdef CONFIG_DEBUG_VM_MAPLE_TREE
1493 		BUG_ON(next->vm_start < vms->start);
1494 		BUG_ON(next->vm_start > vms->end);
1495 #endif
1496 	}
1497 
1498 	vms->next = vma_next(vms->vmi);
1499 	if (vms->next)
1500 		vms->unmap_end = vms->next->vm_start;
1501 
1502 #if defined(CONFIG_DEBUG_VM_MAPLE_TREE)
1503 	/* Make sure no VMAs are about to be lost. */
1504 	{
1505 		MA_STATE(test, mas_detach->tree, 0, 0);
1506 		struct vm_area_struct *vma_mas, *vma_test;
1507 		int test_count = 0;
1508 
1509 		vma_iter_set(vms->vmi, vms->start);
1510 		rcu_read_lock();
1511 		vma_test = mas_find(&test, vms->vma_count - 1);
1512 		for_each_vma_range(*(vms->vmi), vma_mas, vms->end) {
1513 			BUG_ON(vma_mas != vma_test);
1514 			test_count++;
1515 			vma_test = mas_next(&test, vms->vma_count - 1);
1516 		}
1517 		rcu_read_unlock();
1518 		BUG_ON(vms->vma_count != test_count);
1519 	}
1520 #endif
1521 
1522 	while (vma_iter_addr(vms->vmi) > vms->start)
1523 		vma_iter_prev_range(vms->vmi);
1524 
1525 	vms->clear_ptes = true;
1526 	return 0;
1527 
1528 userfaultfd_error:
1529 munmap_gather_failed:
1530 end_split_failed:
1531 modify_vma_failed:
1532 	reattach_vmas(mas_detach);
1533 start_split_failed:
1534 map_count_exceeded:
1535 	return error;
1536 }
1537 
1538 /*
1539  * init_vma_munmap() - Initializer wrapper for vma_munmap_struct
1540  * @vms: The vma munmap struct
1541  * @vmi: The vma iterator
1542  * @vma: The first vm_area_struct to munmap
1543  * @start: The aligned start address to munmap
1544  * @end: The aligned end address to munmap
1545  * @uf: The userfaultfd list_head
1546  * @unlock: Unlock after the operation.  Only unlocked on success
1547  */
1548 static void init_vma_munmap(struct vma_munmap_struct *vms,
1549 		struct vma_iterator *vmi, struct vm_area_struct *vma,
1550 		unsigned long start, unsigned long end, struct list_head *uf,
1551 		bool unlock)
1552 {
1553 	vms->vmi = vmi;
1554 	vms->vma = vma;
1555 	if (vma) {
1556 		vms->start = start;
1557 		vms->end = end;
1558 	} else {
1559 		vms->start = vms->end = 0;
1560 	}
1561 	vms->unlock = unlock;
1562 	vms->uf = uf;
1563 	vms->vma_count = 0;
1564 	vms->nr_pages = vms->locked_vm = vms->nr_accounted = 0;
1565 	vms->exec_vm = vms->stack_vm = vms->data_vm = 0;
1566 	vms->unmap_start = FIRST_USER_ADDRESS;
1567 	vms->unmap_end = USER_PGTABLES_CEILING;
1568 	vms->clear_ptes = false;
1569 }
1570 
1571 /*
1572  * do_vmi_align_munmap() - munmap the aligned region from @start to @end.
1573  * @vmi: The vma iterator
1574  * @vma: The starting vm_area_struct
1575  * @mm: The mm_struct
1576  * @start: The aligned start address to munmap.
1577  * @end: The aligned end address to munmap.
1578  * @uf: The userfaultfd list_head
1579  * @unlock: Set to true to drop the mmap_lock.  unlocking only happens on
1580  * success.
1581  *
1582  * Return: 0 on success and drops the lock if so directed, error and leaves the
1583  * lock held otherwise.
1584  */
1585 int do_vmi_align_munmap(struct vma_iterator *vmi, struct vm_area_struct *vma,
1586 		struct mm_struct *mm, unsigned long start, unsigned long end,
1587 		struct list_head *uf, bool unlock)
1588 {
1589 	struct maple_tree mt_detach;
1590 	MA_STATE(mas_detach, &mt_detach, 0, 0);
1591 	mt_init_flags(&mt_detach, vmi->mas.tree->ma_flags & MT_FLAGS_LOCK_MASK);
1592 	mt_on_stack(mt_detach);
1593 	struct vma_munmap_struct vms;
1594 	int error;
1595 
1596 	init_vma_munmap(&vms, vmi, vma, start, end, uf, unlock);
1597 	error = vms_gather_munmap_vmas(&vms, &mas_detach);
1598 	if (error)
1599 		goto gather_failed;
1600 
1601 	error = vma_iter_clear_gfp(vmi, start, end, GFP_KERNEL);
1602 	if (error)
1603 		goto clear_tree_failed;
1604 
1605 	/* Point of no return */
1606 	vms_complete_munmap_vmas(&vms, &mas_detach);
1607 	return 0;
1608 
1609 clear_tree_failed:
1610 	reattach_vmas(&mas_detach);
1611 gather_failed:
1612 	validate_mm(mm);
1613 	return error;
1614 }
1615 
1616 /*
1617  * do_vmi_munmap() - munmap a given range.
1618  * @vmi: The vma iterator
1619  * @mm: The mm_struct
1620  * @start: The start address to munmap
1621  * @len: The length of the range to munmap
1622  * @uf: The userfaultfd list_head
1623  * @unlock: set to true if the user wants to drop the mmap_lock on success
1624  *
1625  * This function takes a @mas that is either pointing to the previous VMA or set
1626  * to MA_START and sets it up to remove the mapping(s).  The @len will be
1627  * aligned.
1628  *
1629  * Return: 0 on success and drops the lock if so directed, error and leaves the
1630  * lock held otherwise.
1631  */
1632 int do_vmi_munmap(struct vma_iterator *vmi, struct mm_struct *mm,
1633 		  unsigned long start, size_t len, struct list_head *uf,
1634 		  bool unlock)
1635 {
1636 	unsigned long end;
1637 	struct vm_area_struct *vma;
1638 
1639 	if ((offset_in_page(start)) || start > TASK_SIZE || len > TASK_SIZE-start)
1640 		return -EINVAL;
1641 
1642 	end = start + PAGE_ALIGN(len);
1643 	if (end == start)
1644 		return -EINVAL;
1645 
1646 	/* Find the first overlapping VMA */
1647 	vma = vma_find(vmi, end);
1648 	if (!vma) {
1649 		if (unlock)
1650 			mmap_write_unlock(mm);
1651 		return 0;
1652 	}
1653 
1654 	return do_vmi_align_munmap(vmi, vma, mm, start, end, uf, unlock);
1655 }
1656 
1657 /*
1658  * We are about to modify one or multiple of a VMA's flags, policy, userfaultfd
1659  * context and anonymous VMA name within the range [start, end).
1660  *
1661  * As a result, we might be able to merge the newly modified VMA range with an
1662  * adjacent VMA with identical properties.
1663  *
1664  * If no merge is possible and the range does not span the entirety of the VMA,
1665  * we then need to split the VMA to accommodate the change.
1666  *
1667  * The function returns either the merged VMA, the original VMA if a split was
1668  * required instead, or an error if the split failed.
1669  */
1670 static struct vm_area_struct *vma_modify(struct vma_merge_struct *vmg)
1671 {
1672 	struct vm_area_struct *vma = vmg->middle;
1673 	unsigned long start = vmg->start;
1674 	unsigned long end = vmg->end;
1675 	struct vm_area_struct *merged;
1676 
1677 	/* First, try to merge. */
1678 	merged = vma_merge_existing_range(vmg);
1679 	if (merged)
1680 		return merged;
1681 	if (vmg_nomem(vmg))
1682 		return ERR_PTR(-ENOMEM);
1683 
1684 	/*
1685 	 * Split can fail for reasons other than OOM, so if the user requests
1686 	 * this it's probably a mistake.
1687 	 */
1688 	VM_WARN_ON(vmg->give_up_on_oom &&
1689 		   (vma->vm_start != start || vma->vm_end != end));
1690 
1691 	/* Split any preceding portion of the VMA. */
1692 	if (vma->vm_start < start) {
1693 		int err = split_vma(vmg->vmi, vma, start, 1);
1694 
1695 		if (err)
1696 			return ERR_PTR(err);
1697 	}
1698 
1699 	/* Split any trailing portion of the VMA. */
1700 	if (vma->vm_end > end) {
1701 		int err = split_vma(vmg->vmi, vma, end, 0);
1702 
1703 		if (err)
1704 			return ERR_PTR(err);
1705 	}
1706 
1707 	return vma;
1708 }
1709 
1710 struct vm_area_struct *vma_modify_flags(struct vma_iterator *vmi,
1711 		struct vm_area_struct *prev, struct vm_area_struct *vma,
1712 		unsigned long start, unsigned long end,
1713 		vma_flags_t *vma_flags_ptr)
1714 {
1715 	VMG_VMA_STATE(vmg, vmi, prev, vma, start, end);
1716 	const vma_flags_t vma_flags = *vma_flags_ptr;
1717 	struct vm_area_struct *ret;
1718 
1719 	vmg.vma_flags = vma_flags;
1720 
1721 	ret = vma_modify(&vmg);
1722 	if (IS_ERR(ret))
1723 		return ret;
1724 
1725 	/*
1726 	 * For a merge to succeed, the flags must match those
1727 	 * requested. However, sticky flags may have been retained, so propagate
1728 	 * them to the caller.
1729 	 */
1730 	if (vmg.state == VMA_MERGE_SUCCESS)
1731 		*vma_flags_ptr = ret->flags;
1732 	return ret;
1733 }
1734 
1735 struct vm_area_struct *vma_modify_name(struct vma_iterator *vmi,
1736 		struct vm_area_struct *prev, struct vm_area_struct *vma,
1737 		unsigned long start, unsigned long end,
1738 		struct anon_vma_name *new_name)
1739 {
1740 	VMG_VMA_STATE(vmg, vmi, prev, vma, start, end);
1741 
1742 	vmg.anon_name = new_name;
1743 
1744 	return vma_modify(&vmg);
1745 }
1746 
1747 struct vm_area_struct *vma_modify_policy(struct vma_iterator *vmi,
1748 		struct vm_area_struct *prev, struct vm_area_struct *vma,
1749 		unsigned long start, unsigned long end,
1750 		struct mempolicy *new_pol)
1751 {
1752 	VMG_VMA_STATE(vmg, vmi, prev, vma, start, end);
1753 
1754 	vmg.policy = new_pol;
1755 
1756 	return vma_modify(&vmg);
1757 }
1758 
1759 struct vm_area_struct *vma_modify_flags_uffd(struct vma_iterator *vmi,
1760 		struct vm_area_struct *prev, struct vm_area_struct *vma,
1761 		unsigned long start, unsigned long end,
1762 		const vma_flags_t *vma_flags, struct vm_userfaultfd_ctx new_ctx,
1763 		bool give_up_on_oom)
1764 {
1765 	VMG_VMA_STATE(vmg, vmi, prev, vma, start, end);
1766 
1767 	vmg.vma_flags = *vma_flags;
1768 	vmg.uffd_ctx = new_ctx;
1769 	if (give_up_on_oom)
1770 		vmg.give_up_on_oom = true;
1771 
1772 	return vma_modify(&vmg);
1773 }
1774 
1775 /*
1776  * Expand vma by delta bytes, potentially merging with an immediately adjacent
1777  * VMA with identical properties.
1778  */
1779 struct vm_area_struct *vma_merge_extend(struct vma_iterator *vmi,
1780 					struct vm_area_struct *vma,
1781 					unsigned long delta)
1782 {
1783 	VMG_VMA_STATE(vmg, vmi, vma, vma, vma->vm_end, vma->vm_end + delta);
1784 
1785 	vmg.next = vma_iter_next_rewind(vmi, NULL);
1786 	vmg.middle = NULL; /* We use the VMA to populate VMG fields only. */
1787 
1788 	return vma_merge_new_range(&vmg);
1789 }
1790 
1791 void unlink_file_vma_batch_init(struct unlink_vma_file_batch *vb)
1792 {
1793 	vb->count = 0;
1794 }
1795 
1796 static void unlink_file_vma_batch_process(struct unlink_vma_file_batch *vb)
1797 {
1798 	struct address_space *mapping;
1799 	int i;
1800 
1801 	mapping = vb->vmas[0]->vm_file->f_mapping;
1802 	i_mmap_lock_write(mapping);
1803 	for (i = 0; i < vb->count; i++) {
1804 		VM_WARN_ON_ONCE(vb->vmas[i]->vm_file->f_mapping != mapping);
1805 		__remove_shared_vm_struct(vb->vmas[i], mapping);
1806 	}
1807 	i_mmap_unlock_write(mapping);
1808 
1809 	unlink_file_vma_batch_init(vb);
1810 }
1811 
1812 void unlink_file_vma_batch_add(struct unlink_vma_file_batch *vb,
1813 			       struct vm_area_struct *vma)
1814 {
1815 	if (vma->vm_file == NULL)
1816 		return;
1817 
1818 	if ((vb->count > 0 && vb->vmas[0]->vm_file != vma->vm_file) ||
1819 	    vb->count == ARRAY_SIZE(vb->vmas))
1820 		unlink_file_vma_batch_process(vb);
1821 
1822 	vb->vmas[vb->count] = vma;
1823 	vb->count++;
1824 }
1825 
1826 void unlink_file_vma_batch_final(struct unlink_vma_file_batch *vb)
1827 {
1828 	if (vb->count > 0)
1829 		unlink_file_vma_batch_process(vb);
1830 }
1831 
1832 static void vma_link_file(struct vm_area_struct *vma, bool hold_rmap_lock)
1833 {
1834 	struct file *file = vma->vm_file;
1835 	struct address_space *mapping;
1836 
1837 	if (file) {
1838 		mapping = file->f_mapping;
1839 		i_mmap_lock_write(mapping);
1840 		__vma_link_file(vma, mapping);
1841 		if (!hold_rmap_lock)
1842 			i_mmap_unlock_write(mapping);
1843 	}
1844 }
1845 
1846 static int vma_link(struct mm_struct *mm, struct vm_area_struct *vma)
1847 {
1848 	VMA_ITERATOR(vmi, mm, 0);
1849 
1850 	vma_iter_config(&vmi, vma->vm_start, vma->vm_end);
1851 	if (vma_iter_prealloc(&vmi, vma))
1852 		return -ENOMEM;
1853 
1854 	vma_start_write(vma);
1855 	vma_iter_store_new(&vmi, vma);
1856 	vma_link_file(vma, /* hold_rmap_lock= */false);
1857 	mm->map_count++;
1858 	validate_mm(mm);
1859 	return 0;
1860 }
1861 
1862 /*
1863  * Copy the vma structure to a new location in the same mm,
1864  * prior to moving page table entries, to effect an mremap move.
1865  */
1866 struct vm_area_struct *copy_vma(struct vm_area_struct **vmap,
1867 	unsigned long addr, unsigned long len, pgoff_t pgoff,
1868 	bool *need_rmap_locks)
1869 {
1870 	struct vm_area_struct *vma = *vmap;
1871 	unsigned long vma_start = vma->vm_start;
1872 	struct mm_struct *mm = vma->vm_mm;
1873 	struct vm_area_struct *new_vma;
1874 	bool faulted_in_anon_vma = true;
1875 	VMA_ITERATOR(vmi, mm, addr);
1876 	VMG_VMA_STATE(vmg, &vmi, NULL, vma, addr, addr + len);
1877 
1878 	/*
1879 	 * If anonymous vma has not yet been faulted, update new pgoff
1880 	 * to match new location, to increase its chance of merging.
1881 	 */
1882 	if (unlikely(vma_is_anonymous(vma) && !vma->anon_vma)) {
1883 		pgoff = addr >> PAGE_SHIFT;
1884 		faulted_in_anon_vma = false;
1885 	}
1886 
1887 	/*
1888 	 * If the VMA we are copying might contain a uprobe PTE, ensure
1889 	 * that we do not establish one upon merge. Otherwise, when mremap()
1890 	 * moves page tables, it will orphan the newly created PTE.
1891 	 */
1892 	if (vma->vm_file)
1893 		vmg.skip_vma_uprobe = true;
1894 
1895 	new_vma = find_vma_prev(mm, addr, &vmg.prev);
1896 	if (new_vma && new_vma->vm_start < addr + len)
1897 		return NULL;	/* should never get here */
1898 
1899 	vmg.pgoff = pgoff;
1900 	vmg.next = vma_iter_next_rewind(&vmi, NULL);
1901 	new_vma = vma_merge_copied_range(&vmg);
1902 
1903 	if (new_vma) {
1904 		/*
1905 		 * Source vma may have been merged into new_vma
1906 		 */
1907 		if (unlikely(vma_start >= new_vma->vm_start &&
1908 			     vma_start < new_vma->vm_end)) {
1909 			/*
1910 			 * The only way we can get a vma_merge with
1911 			 * self during an mremap is if the vma hasn't
1912 			 * been faulted in yet and we were allowed to
1913 			 * reset the dst vma->vm_pgoff to the
1914 			 * destination address of the mremap to allow
1915 			 * the merge to happen. mremap must change the
1916 			 * vm_pgoff linearity between src and dst vmas
1917 			 * (in turn preventing a vma_merge) to be
1918 			 * safe. It is only safe to keep the vm_pgoff
1919 			 * linear if there are no pages mapped yet.
1920 			 */
1921 			VM_BUG_ON_VMA(faulted_in_anon_vma, new_vma);
1922 			*vmap = vma = new_vma;
1923 		}
1924 		*need_rmap_locks = (new_vma->vm_pgoff <= vma->vm_pgoff);
1925 	} else {
1926 		new_vma = vm_area_dup(vma);
1927 		if (!new_vma)
1928 			goto out;
1929 		vma_set_range(new_vma, addr, addr + len, pgoff);
1930 		if (vma_dup_policy(vma, new_vma))
1931 			goto out_free_vma;
1932 		if (anon_vma_clone(new_vma, vma, VMA_OP_REMAP))
1933 			goto out_free_mempol;
1934 		if (new_vma->vm_file)
1935 			get_file(new_vma->vm_file);
1936 		if (new_vma->vm_ops && new_vma->vm_ops->open)
1937 			new_vma->vm_ops->open(new_vma);
1938 		if (vma_link(mm, new_vma))
1939 			goto out_vma_link;
1940 		*need_rmap_locks = false;
1941 	}
1942 	return new_vma;
1943 
1944 out_vma_link:
1945 	fixup_hugetlb_reservations(new_vma);
1946 	vma_close(new_vma);
1947 
1948 	if (new_vma->vm_file)
1949 		fput(new_vma->vm_file);
1950 
1951 	unlink_anon_vmas(new_vma);
1952 out_free_mempol:
1953 	mpol_put(vma_policy(new_vma));
1954 out_free_vma:
1955 	vm_area_free(new_vma);
1956 out:
1957 	return NULL;
1958 }
1959 
1960 /*
1961  * Rough compatibility check to quickly see if it's even worth looking
1962  * at sharing an anon_vma.
1963  *
1964  * They need to have the same vm_file, and the flags can only differ
1965  * in things that mprotect may change.
1966  *
1967  * NOTE! The fact that we share an anon_vma doesn't _have_ to mean that
1968  * we can merge the two vma's. For example, we refuse to merge a vma if
1969  * there is a vm_ops->close() function, because that indicates that the
1970  * driver is doing some kind of reference counting. But that doesn't
1971  * really matter for the anon_vma sharing case.
1972  */
1973 static int anon_vma_compatible(struct vm_area_struct *a, struct vm_area_struct *b)
1974 {
1975 	vma_flags_t diff = vma_flags_diff_pair(&a->flags, &b->flags);
1976 
1977 	vma_flags_clear_mask(&diff, VMA_ACCESS_FLAGS);
1978 	vma_flags_clear_mask(&diff, VMA_IGNORE_MERGE_FLAGS);
1979 
1980 	return a->vm_end == b->vm_start &&
1981 		mpol_equal(vma_policy(a), vma_policy(b)) &&
1982 		a->vm_file == b->vm_file &&
1983 		vma_flags_empty(&diff) &&
1984 		b->vm_pgoff == a->vm_pgoff + ((b->vm_start - a->vm_start) >> PAGE_SHIFT);
1985 }
1986 
1987 /*
1988  * Do some basic sanity checking to see if we can re-use the anon_vma
1989  * from 'old'. The 'a'/'b' vma's are in VM order - one of them will be
1990  * the same as 'old', the other will be the new one that is trying
1991  * to share the anon_vma.
1992  *
1993  * NOTE! This runs with mmap_lock held for reading, so it is possible that
1994  * the anon_vma of 'old' is concurrently in the process of being set up
1995  * by another page fault trying to merge _that_. But that's ok: if it
1996  * is being set up, that automatically means that it will be a singleton
1997  * acceptable for merging, so we can do all of this optimistically. But
1998  * we do that READ_ONCE() to make sure that we never re-load the pointer.
1999  *
2000  * IOW: that the "list_is_singular()" test on the anon_vma_chain only
2001  * matters for the 'stable anon_vma' case (ie the thing we want to avoid
2002  * is to return an anon_vma that is "complex" due to having gone through
2003  * a fork).
2004  *
2005  * We also make sure that the two vma's are compatible (adjacent,
2006  * and with the same memory policies). That's all stable, even with just
2007  * a read lock on the mmap_lock.
2008  */
2009 static struct anon_vma *reusable_anon_vma(struct vm_area_struct *old,
2010 					  struct vm_area_struct *a,
2011 					  struct vm_area_struct *b)
2012 {
2013 	if (anon_vma_compatible(a, b)) {
2014 		struct anon_vma *anon_vma = READ_ONCE(old->anon_vma);
2015 
2016 		if (anon_vma && list_is_singular(&old->anon_vma_chain))
2017 			return anon_vma;
2018 	}
2019 	return NULL;
2020 }
2021 
2022 /*
2023  * find_mergeable_anon_vma is used by anon_vma_prepare, to check
2024  * neighbouring vmas for a suitable anon_vma, before it goes off
2025  * to allocate a new anon_vma.  It checks because a repetitive
2026  * sequence of mprotects and faults may otherwise lead to distinct
2027  * anon_vmas being allocated, preventing vma merge in subsequent
2028  * mprotect.
2029  */
2030 struct anon_vma *find_mergeable_anon_vma(struct vm_area_struct *vma)
2031 {
2032 	struct anon_vma *anon_vma = NULL;
2033 	struct vm_area_struct *prev, *next;
2034 	VMA_ITERATOR(vmi, vma->vm_mm, vma->vm_end);
2035 
2036 	/* Try next first. */
2037 	next = vma_iter_load(&vmi);
2038 	if (next) {
2039 		anon_vma = reusable_anon_vma(next, vma, next);
2040 		if (anon_vma)
2041 			return anon_vma;
2042 	}
2043 
2044 	prev = vma_prev(&vmi);
2045 	VM_BUG_ON_VMA(prev != vma, vma);
2046 	prev = vma_prev(&vmi);
2047 	/* Try prev next. */
2048 	if (prev)
2049 		anon_vma = reusable_anon_vma(prev, prev, vma);
2050 
2051 	/*
2052 	 * We might reach here with anon_vma == NULL if we can't find
2053 	 * any reusable anon_vma.
2054 	 * There's no absolute need to look only at touching neighbours:
2055 	 * we could search further afield for "compatible" anon_vmas.
2056 	 * But it would probably just be a waste of time searching,
2057 	 * or lead to too many vmas hanging off the same anon_vma.
2058 	 * We're trying to allow mprotect remerging later on,
2059 	 * not trying to minimize memory used for anon_vmas.
2060 	 */
2061 	return anon_vma;
2062 }
2063 
2064 static bool vm_ops_needs_writenotify(const struct vm_operations_struct *vm_ops)
2065 {
2066 	return vm_ops && (vm_ops->page_mkwrite || vm_ops->pfn_mkwrite);
2067 }
2068 
2069 static bool vma_is_shared_writable(struct vm_area_struct *vma)
2070 {
2071 	return vma_test_all(vma, VMA_WRITE_BIT, VMA_SHARED_BIT);
2072 }
2073 
2074 static bool vma_fs_can_writeback(struct vm_area_struct *vma)
2075 {
2076 	/* No managed pages to writeback. */
2077 	if (vma_test(vma, VMA_PFNMAP_BIT))
2078 		return false;
2079 
2080 	return vma->vm_file && vma->vm_file->f_mapping &&
2081 		mapping_can_writeback(vma->vm_file->f_mapping);
2082 }
2083 
2084 /*
2085  * Does this VMA require the underlying folios to have their dirty state
2086  * tracked?
2087  */
2088 bool vma_needs_dirty_tracking(struct vm_area_struct *vma)
2089 {
2090 	/* Only shared, writable VMAs require dirty tracking. */
2091 	if (!vma_is_shared_writable(vma))
2092 		return false;
2093 
2094 	/* Does the filesystem need to be notified? */
2095 	if (vm_ops_needs_writenotify(vma->vm_ops))
2096 		return true;
2097 
2098 	/*
2099 	 * Even if the filesystem doesn't indicate a need for writenotify, if it
2100 	 * can writeback, dirty tracking is still required.
2101 	 */
2102 	return vma_fs_can_writeback(vma);
2103 }
2104 
2105 /*
2106  * Some shared mappings will want the pages marked read-only
2107  * to track write events. If so, we'll downgrade vm_page_prot
2108  * to the private version (using protection_map[] without the
2109  * VM_SHARED bit).
2110  */
2111 bool vma_wants_writenotify(struct vm_area_struct *vma, pgprot_t vm_page_prot)
2112 {
2113 	/* If it was private or non-writable, the write bit is already clear */
2114 	if (!vma_is_shared_writable(vma))
2115 		return false;
2116 
2117 	/* The backer wishes to know when pages are first written to? */
2118 	if (vm_ops_needs_writenotify(vma->vm_ops))
2119 		return true;
2120 
2121 	/* The open routine did something to the protections that pgprot_modify
2122 	 * won't preserve? */
2123 	if (pgprot_val(vm_page_prot) !=
2124 	    pgprot_val(vm_pgprot_modify(vm_page_prot, vma->vm_flags)))
2125 		return false;
2126 
2127 	/*
2128 	 * Do we need to track softdirty? hugetlb does not support softdirty
2129 	 * tracking yet.
2130 	 */
2131 	if (vma_soft_dirty_enabled(vma) && !is_vm_hugetlb_page(vma))
2132 		return true;
2133 
2134 	/* Do we need write faults for uffd-wp tracking? */
2135 	if (userfaultfd_wp(vma))
2136 		return true;
2137 
2138 	/* Can the mapping track the dirty pages? */
2139 	return vma_fs_can_writeback(vma);
2140 }
2141 
2142 static DEFINE_MUTEX(mm_all_locks_mutex);
2143 
2144 static void vm_lock_anon_vma(struct mm_struct *mm, struct anon_vma *anon_vma)
2145 {
2146 	if (!test_bit(0, (unsigned long *) &anon_vma->root->rb_root.rb_root.rb_node)) {
2147 		/*
2148 		 * The LSB of head.next can't change from under us
2149 		 * because we hold the mm_all_locks_mutex.
2150 		 */
2151 		down_write_nest_lock(&anon_vma->root->rwsem, &mm->mmap_lock);
2152 		/*
2153 		 * We can safely modify head.next after taking the
2154 		 * anon_vma->root->rwsem. If some other vma in this mm shares
2155 		 * the same anon_vma we won't take it again.
2156 		 *
2157 		 * No need of atomic instructions here, head.next
2158 		 * can't change from under us thanks to the
2159 		 * anon_vma->root->rwsem.
2160 		 */
2161 		if (__test_and_set_bit(0, (unsigned long *)
2162 				       &anon_vma->root->rb_root.rb_root.rb_node))
2163 			BUG();
2164 	}
2165 }
2166 
2167 static void vm_lock_mapping(struct mm_struct *mm, struct address_space *mapping)
2168 {
2169 	if (!test_bit(AS_MM_ALL_LOCKS, &mapping->flags)) {
2170 		/*
2171 		 * AS_MM_ALL_LOCKS can't change from under us because
2172 		 * we hold the mm_all_locks_mutex.
2173 		 *
2174 		 * Operations on ->flags have to be atomic because
2175 		 * even if AS_MM_ALL_LOCKS is stable thanks to the
2176 		 * mm_all_locks_mutex, there may be other cpus
2177 		 * changing other bitflags in parallel to us.
2178 		 */
2179 		if (test_and_set_bit(AS_MM_ALL_LOCKS, &mapping->flags))
2180 			BUG();
2181 		down_write_nest_lock(&mapping->i_mmap_rwsem, &mm->mmap_lock);
2182 	}
2183 }
2184 
2185 /*
2186  * This operation locks against the VM for all pte/vma/mm related
2187  * operations that could ever happen on a certain mm. This includes
2188  * vmtruncate, try_to_unmap, and all page faults.
2189  *
2190  * The caller must take the mmap_lock in write mode before calling
2191  * mm_take_all_locks(). The caller isn't allowed to release the
2192  * mmap_lock until mm_drop_all_locks() returns.
2193  *
2194  * mmap_lock in write mode is required in order to block all operations
2195  * that could modify pagetables and free pages without need of
2196  * altering the vma layout. It's also needed in write mode to avoid new
2197  * anon_vmas to be associated with existing vmas.
2198  *
2199  * A single task can't take more than one mm_take_all_locks() in a row
2200  * or it would deadlock.
2201  *
2202  * The LSB in anon_vma->rb_root.rb_node and the AS_MM_ALL_LOCKS bitflag in
2203  * mapping->flags avoid to take the same lock twice, if more than one
2204  * vma in this mm is backed by the same anon_vma or address_space.
2205  *
2206  * We take locks in following order, accordingly to comment at beginning
2207  * of mm/rmap.c:
2208  *   - all hugetlbfs_i_mmap_rwsem_key locks (aka mapping->i_mmap_rwsem for
2209  *     hugetlb mapping);
2210  *   - all vmas marked locked
2211  *   - all i_mmap_rwsem locks;
2212  *   - all anon_vma->rwseml
2213  *
2214  * We can take all locks within these types randomly because the VM code
2215  * doesn't nest them and we protected from parallel mm_take_all_locks() by
2216  * mm_all_locks_mutex.
2217  *
2218  * mm_take_all_locks() and mm_drop_all_locks are expensive operations
2219  * that may have to take thousand of locks.
2220  *
2221  * mm_take_all_locks() can fail if it's interrupted by signals.
2222  */
2223 int mm_take_all_locks(struct mm_struct *mm)
2224 {
2225 	struct vm_area_struct *vma;
2226 	struct anon_vma_chain *avc;
2227 	VMA_ITERATOR(vmi, mm, 0);
2228 
2229 	mmap_assert_write_locked(mm);
2230 
2231 	mutex_lock(&mm_all_locks_mutex);
2232 
2233 	/*
2234 	 * vma_start_write() does not have a complement in mm_drop_all_locks()
2235 	 * because vma_start_write() is always asymmetrical; it marks a VMA as
2236 	 * being written to until mmap_write_unlock() or mmap_write_downgrade()
2237 	 * is reached.
2238 	 */
2239 	for_each_vma(vmi, vma) {
2240 		if (signal_pending(current))
2241 			goto out_unlock;
2242 		vma_start_write(vma);
2243 	}
2244 
2245 	vma_iter_init(&vmi, mm, 0);
2246 	for_each_vma(vmi, vma) {
2247 		if (signal_pending(current))
2248 			goto out_unlock;
2249 		if (vma->vm_file && vma->vm_file->f_mapping &&
2250 				is_vm_hugetlb_page(vma))
2251 			vm_lock_mapping(mm, vma->vm_file->f_mapping);
2252 	}
2253 
2254 	vma_iter_init(&vmi, mm, 0);
2255 	for_each_vma(vmi, vma) {
2256 		if (signal_pending(current))
2257 			goto out_unlock;
2258 		if (vma->vm_file && vma->vm_file->f_mapping &&
2259 				!is_vm_hugetlb_page(vma))
2260 			vm_lock_mapping(mm, vma->vm_file->f_mapping);
2261 	}
2262 
2263 	vma_iter_init(&vmi, mm, 0);
2264 	for_each_vma(vmi, vma) {
2265 		if (signal_pending(current))
2266 			goto out_unlock;
2267 		if (vma->anon_vma)
2268 			list_for_each_entry(avc, &vma->anon_vma_chain, same_vma)
2269 				vm_lock_anon_vma(mm, avc->anon_vma);
2270 	}
2271 
2272 	return 0;
2273 
2274 out_unlock:
2275 	mm_drop_all_locks(mm);
2276 	return -EINTR;
2277 }
2278 
2279 static void vm_unlock_anon_vma(struct anon_vma *anon_vma)
2280 {
2281 	if (test_bit(0, (unsigned long *) &anon_vma->root->rb_root.rb_root.rb_node)) {
2282 		/*
2283 		 * The LSB of head.next can't change to 0 from under
2284 		 * us because we hold the mm_all_locks_mutex.
2285 		 *
2286 		 * We must however clear the bitflag before unlocking
2287 		 * the vma so the users using the anon_vma->rb_root will
2288 		 * never see our bitflag.
2289 		 *
2290 		 * No need of atomic instructions here, head.next
2291 		 * can't change from under us until we release the
2292 		 * anon_vma->root->rwsem.
2293 		 */
2294 		if (!__test_and_clear_bit(0, (unsigned long *)
2295 					  &anon_vma->root->rb_root.rb_root.rb_node))
2296 			BUG();
2297 		anon_vma_unlock_write(anon_vma);
2298 	}
2299 }
2300 
2301 static void vm_unlock_mapping(struct address_space *mapping)
2302 {
2303 	if (test_bit(AS_MM_ALL_LOCKS, &mapping->flags)) {
2304 		/*
2305 		 * AS_MM_ALL_LOCKS can't change to 0 from under us
2306 		 * because we hold the mm_all_locks_mutex.
2307 		 */
2308 		i_mmap_unlock_write(mapping);
2309 		if (!test_and_clear_bit(AS_MM_ALL_LOCKS,
2310 					&mapping->flags))
2311 			BUG();
2312 	}
2313 }
2314 
2315 /*
2316  * The mmap_lock cannot be released by the caller until
2317  * mm_drop_all_locks() returns.
2318  */
2319 void mm_drop_all_locks(struct mm_struct *mm)
2320 {
2321 	struct vm_area_struct *vma;
2322 	struct anon_vma_chain *avc;
2323 	VMA_ITERATOR(vmi, mm, 0);
2324 
2325 	mmap_assert_write_locked(mm);
2326 	BUG_ON(!mutex_is_locked(&mm_all_locks_mutex));
2327 
2328 	for_each_vma(vmi, vma) {
2329 		if (vma->anon_vma)
2330 			list_for_each_entry(avc, &vma->anon_vma_chain, same_vma)
2331 				vm_unlock_anon_vma(avc->anon_vma);
2332 		if (vma->vm_file && vma->vm_file->f_mapping)
2333 			vm_unlock_mapping(vma->vm_file->f_mapping);
2334 	}
2335 
2336 	mutex_unlock(&mm_all_locks_mutex);
2337 }
2338 
2339 /*
2340  * We account for memory if it's a private writeable mapping,
2341  * not hugepages and VM_NORESERVE wasn't set.
2342  */
2343 static bool accountable_mapping(struct mmap_state *map)
2344 {
2345 	const struct file *file = map->file;
2346 	vma_flags_t mask;
2347 
2348 	/*
2349 	 * hugetlb has its own accounting separate from the core VM
2350 	 * VM_HUGETLB may not be set yet so we cannot check for that flag.
2351 	 */
2352 	if (file && is_file_hugepages(file))
2353 		return false;
2354 
2355 	mask = vma_flags_and(&map->vma_flags, VMA_NORESERVE_BIT, VMA_SHARED_BIT,
2356 			     VMA_WRITE_BIT);
2357 	return vma_flags_same(&mask, VMA_WRITE_BIT);
2358 }
2359 
2360 /*
2361  * vms_abort_munmap_vmas() - Undo as much as possible from an aborted munmap()
2362  * operation.
2363  * @vms: The vma unmap structure
2364  * @mas_detach: The maple state with the detached maple tree
2365  *
2366  * Reattach any detached vmas, free up the maple tree used to track the vmas.
2367  * If that's not possible because the ptes are cleared (and vm_ops->closed() may
2368  * have been called), then a NULL is written over the vmas and the vmas are
2369  * removed (munmap() completed).
2370  */
2371 static void vms_abort_munmap_vmas(struct vma_munmap_struct *vms,
2372 		struct ma_state *mas_detach)
2373 {
2374 	struct ma_state *mas = &vms->vmi->mas;
2375 
2376 	if (!vms->nr_pages)
2377 		return;
2378 
2379 	if (vms->clear_ptes)
2380 		return reattach_vmas(mas_detach);
2381 
2382 	/*
2383 	 * Aborting cannot just call the vm_ops open() because they are often
2384 	 * not symmetrical and state data has been lost.  Resort to the old
2385 	 * failure method of leaving a gap where the MAP_FIXED mapping failed.
2386 	 */
2387 	mas_set_range(mas, vms->start, vms->end - 1);
2388 	mas_store_gfp(mas, NULL, GFP_KERNEL|__GFP_NOFAIL);
2389 	/* Clean up the insertion of the unfortunate gap */
2390 	vms_complete_munmap_vmas(vms, mas_detach);
2391 }
2392 
2393 static void update_ksm_flags(struct mmap_state *map)
2394 {
2395 	map->vma_flags = ksm_vma_flags(map->mm, map->file, map->vma_flags);
2396 }
2397 
2398 static void set_desc_from_map(struct vm_area_desc *desc,
2399 		const struct mmap_state *map)
2400 {
2401 	desc->start = map->addr;
2402 	desc->end = map->end;
2403 
2404 	desc->pgoff = map->pgoff;
2405 	desc->vm_file = map->file;
2406 	desc->vma_flags = map->vma_flags;
2407 	desc->page_prot = map->page_prot;
2408 }
2409 
2410 /*
2411  * __mmap_setup() - Prepare to gather any overlapping VMAs that need to be
2412  * unmapped once the map operation is completed, check limits, account mapping
2413  * and clean up any pre-existing VMAs.
2414  *
2415  * As a result it sets up the @map and @desc objects.
2416  *
2417  * @map: Mapping state.
2418  * @desc: VMA descriptor
2419  * @uf:  Userfaultfd context list.
2420  *
2421  * Returns: 0 on success, error code otherwise.
2422  */
2423 static int __mmap_setup(struct mmap_state *map, struct vm_area_desc *desc,
2424 			struct list_head *uf)
2425 {
2426 	int error;
2427 	struct vma_iterator *vmi = map->vmi;
2428 	struct vma_munmap_struct *vms = &map->vms;
2429 
2430 	/* Find the first overlapping VMA and initialise unmap state. */
2431 	vms->vma = vma_find(vmi, map->end);
2432 	init_vma_munmap(vms, vmi, vms->vma, map->addr, map->end, uf,
2433 			/* unlock = */ false);
2434 
2435 	/* OK, we have overlapping VMAs - prepare to unmap them. */
2436 	if (vms->vma) {
2437 		mt_init_flags(&map->mt_detach,
2438 			      vmi->mas.tree->ma_flags & MT_FLAGS_LOCK_MASK);
2439 		mt_on_stack(map->mt_detach);
2440 		mas_init(&map->mas_detach, &map->mt_detach, /* addr = */ 0);
2441 		/* Prepare to unmap any existing mapping in the area */
2442 		error = vms_gather_munmap_vmas(vms, &map->mas_detach);
2443 		if (error) {
2444 			/* On error VMAs will already have been reattached. */
2445 			vms->nr_pages = 0;
2446 			return error;
2447 		}
2448 
2449 		map->next = vms->next;
2450 		map->prev = vms->prev;
2451 	} else {
2452 		map->next = vma_iter_next_rewind(vmi, &map->prev);
2453 	}
2454 
2455 	/* Check against address space limit. */
2456 	if (!may_expand_vm(map->mm, &map->vma_flags, map->pglen - vms->nr_pages))
2457 		return -ENOMEM;
2458 
2459 	/* Private writable mapping: check memory availability. */
2460 	if (accountable_mapping(map)) {
2461 		map->charged = map->pglen;
2462 		map->charged -= vms->nr_accounted;
2463 		if (map->charged) {
2464 			error = security_vm_enough_memory_mm(map->mm, map->charged);
2465 			if (error)
2466 				return error;
2467 		}
2468 
2469 		vms->nr_accounted = 0;
2470 		vma_flags_set(&map->vma_flags, VMA_ACCOUNT_BIT);
2471 	}
2472 
2473 	/*
2474 	 * Clear PTEs while the vma is still in the tree so that rmap
2475 	 * cannot race with the freeing later in the truncate scenario.
2476 	 * This is also needed for mmap_file(), which is why vm_ops
2477 	 * close function is called.
2478 	 */
2479 	vms_clean_up_area(vms, &map->mas_detach);
2480 
2481 	set_desc_from_map(desc, map);
2482 	return 0;
2483 }
2484 
2485 
2486 static int __mmap_new_file_vma(struct mmap_state *map,
2487 			       struct vm_area_struct *vma)
2488 {
2489 	struct vma_iterator *vmi = map->vmi;
2490 	int error;
2491 
2492 	vma->vm_file = map->file;
2493 	if (!map->file_doesnt_need_get)
2494 		get_file(map->file);
2495 
2496 	if (!map->file->f_op->mmap)
2497 		return 0;
2498 
2499 	error = mmap_file(vma->vm_file, vma);
2500 	if (error) {
2501 		UNMAP_STATE(unmap, vmi, vma, vma->vm_start, vma->vm_end,
2502 			    map->prev, map->next);
2503 		fput(vma->vm_file);
2504 		vma->vm_file = NULL;
2505 
2506 		vma_iter_set(vmi, vma->vm_end);
2507 		/* Undo any partial mapping done by a device driver. */
2508 		unmap_region(&unmap);
2509 		return error;
2510 	}
2511 
2512 	/* Drivers cannot alter the address of the VMA. */
2513 	WARN_ON_ONCE(map->addr != vma->vm_start);
2514 	/*
2515 	 * Drivers should not permit writability when previously it was
2516 	 * disallowed.
2517 	 */
2518 	VM_WARN_ON_ONCE(!vma_flags_same_pair(&map->vma_flags, &vma->flags) &&
2519 			!vma_flags_test(&map->vma_flags, VMA_MAYWRITE_BIT) &&
2520 			vma_test(vma, VMA_MAYWRITE_BIT));
2521 
2522 	map->file = vma->vm_file;
2523 	map->vma_flags = vma->flags;
2524 
2525 	return 0;
2526 }
2527 
2528 /*
2529  * __mmap_new_vma() - Allocate a new VMA for the region, as merging was not
2530  * possible.
2531  *
2532  * @map:  Mapping state.
2533  * @vmap: Output pointer for the new VMA.
2534  *
2535  * Returns: Zero on success, or an error.
2536  */
2537 static int __mmap_new_vma(struct mmap_state *map, struct vm_area_struct **vmap)
2538 {
2539 	struct vma_iterator *vmi = map->vmi;
2540 	int error = 0;
2541 	struct vm_area_struct *vma;
2542 
2543 	/*
2544 	 * Determine the object being mapped and call the appropriate
2545 	 * specific mapper. the address has already been validated, but
2546 	 * not unmapped, but the maps are removed from the list.
2547 	 */
2548 	vma = vm_area_alloc(map->mm);
2549 	if (!vma)
2550 		return -ENOMEM;
2551 
2552 	vma_iter_config(vmi, map->addr, map->end);
2553 	vma_set_range(vma, map->addr, map->end, map->pgoff);
2554 	vma->flags = map->vma_flags;
2555 	vma->vm_page_prot = map->page_prot;
2556 
2557 	if (vma_iter_prealloc(vmi, vma)) {
2558 		error = -ENOMEM;
2559 		goto free_vma;
2560 	}
2561 
2562 	if (map->file)
2563 		error = __mmap_new_file_vma(map, vma);
2564 	else if (vma_flags_test(&map->vma_flags, VMA_SHARED_BIT))
2565 		error = shmem_zero_setup(vma);
2566 	else
2567 		vma_set_anonymous(vma);
2568 
2569 	if (error)
2570 		goto free_iter_vma;
2571 
2572 	if (!map->check_ksm_early) {
2573 		update_ksm_flags(map);
2574 		vma->flags = map->vma_flags;
2575 	}
2576 
2577 #ifdef CONFIG_SPARC64
2578 	/* TODO: Fix SPARC ADI! */
2579 	WARN_ON_ONCE(!arch_validate_flags(map->vm_flags));
2580 #endif
2581 
2582 	/* Lock the VMA since it is modified after insertion into VMA tree */
2583 	vma_start_write(vma);
2584 	vma_iter_store_new(vmi, vma);
2585 	map->mm->map_count++;
2586 	vma_link_file(vma, map->hold_file_rmap_lock);
2587 
2588 	/*
2589 	 * vma_merge_new_range() calls khugepaged_enter_vma() too, the below
2590 	 * call covers the non-merge case.
2591 	 */
2592 	if (!vma_is_anonymous(vma))
2593 		khugepaged_enter_vma(vma, map->vm_flags);
2594 	*vmap = vma;
2595 	return 0;
2596 
2597 free_iter_vma:
2598 	vma_iter_free(vmi);
2599 free_vma:
2600 	vm_area_free(vma);
2601 	return error;
2602 }
2603 
2604 /*
2605  * __mmap_complete() - Unmap any VMAs we overlap, account memory mapping
2606  *                     statistics, handle locking and finalise the VMA.
2607  *
2608  * @map: Mapping state.
2609  * @vma: Merged or newly allocated VMA for the mmap()'d region.
2610  */
2611 static void __mmap_complete(struct mmap_state *map, struct vm_area_struct *vma)
2612 {
2613 	struct mm_struct *mm = map->mm;
2614 
2615 	perf_event_mmap(vma);
2616 
2617 	/* Unmap any existing mapping in the area. */
2618 	vms_complete_munmap_vmas(&map->vms, &map->mas_detach);
2619 
2620 	vm_stat_account(mm, vma->vm_flags, map->pglen);
2621 	if (vma_test(vma, VMA_LOCKED_BIT)) {
2622 		if (!vma_supports_mlock(vma))
2623 			vma_clear_flags_mask(vma, VMA_LOCKED_MASK);
2624 		else
2625 			mm->locked_vm += map->pglen;
2626 	}
2627 
2628 	if (vma->vm_file)
2629 		uprobe_mmap(vma);
2630 
2631 	/*
2632 	 * New (or expanded) vma always get soft dirty status.
2633 	 * Otherwise user-space soft-dirty page tracker won't
2634 	 * be able to distinguish situation when vma area unmapped,
2635 	 * then new mapped in-place (which must be aimed as
2636 	 * a completely new data area).
2637 	 */
2638 	if (pgtable_supports_soft_dirty())
2639 		vma_set_flags(vma, VMA_SOFTDIRTY_BIT);
2640 
2641 	vma_set_page_prot(vma);
2642 }
2643 
2644 static void call_action_prepare(struct mmap_state *map,
2645 				struct vm_area_desc *desc)
2646 {
2647 	struct mmap_action *action = &desc->action;
2648 
2649 	mmap_action_prepare(action, desc);
2650 
2651 	if (action->hide_from_rmap_until_complete)
2652 		map->hold_file_rmap_lock = true;
2653 }
2654 
2655 /*
2656  * Invoke the f_op->mmap_prepare() callback for a file-backed mapping that
2657  * specifies it.
2658  *
2659  * This is called prior to any merge attempt, and updates whitelisted fields
2660  * that are permitted to be updated by the caller.
2661  *
2662  * All but user-defined fields will be pre-populated with original values.
2663  *
2664  * Returns 0 on success, or an error code otherwise.
2665  */
2666 static int call_mmap_prepare(struct mmap_state *map,
2667 		struct vm_area_desc *desc)
2668 {
2669 	int err;
2670 
2671 	/* Invoke the hook. */
2672 	err = vfs_mmap_prepare(map->file, desc);
2673 	if (err)
2674 		return err;
2675 
2676 	call_action_prepare(map, desc);
2677 
2678 	/* Update fields permitted to be changed. */
2679 	map->pgoff = desc->pgoff;
2680 	if (desc->vm_file != map->file) {
2681 		map->file_doesnt_need_get = true;
2682 		map->file = desc->vm_file;
2683 	}
2684 	map->vma_flags = desc->vma_flags;
2685 	map->page_prot = desc->page_prot;
2686 	/* User-defined fields. */
2687 	map->vm_ops = desc->vm_ops;
2688 	map->vm_private_data = desc->private_data;
2689 
2690 	return 0;
2691 }
2692 
2693 static void set_vma_user_defined_fields(struct vm_area_struct *vma,
2694 		struct mmap_state *map)
2695 {
2696 	if (map->vm_ops)
2697 		vma->vm_ops = map->vm_ops;
2698 	vma->vm_private_data = map->vm_private_data;
2699 }
2700 
2701 /*
2702  * Are we guaranteed no driver can change state such as to preclude KSM merging?
2703  * If so, let's set the KSM mergeable flag early so we don't break VMA merging.
2704  */
2705 static bool can_set_ksm_flags_early(struct mmap_state *map)
2706 {
2707 	struct file *file = map->file;
2708 
2709 	/* Anonymous mappings have no driver which can change them. */
2710 	if (!file)
2711 		return true;
2712 
2713 	/*
2714 	 * If .mmap_prepare() is specified, then the driver will have already
2715 	 * manipulated state prior to updating KSM flags. So no need to worry
2716 	 * about mmap callbacks modifying VMA flags after the KSM flag has been
2717 	 * updated here, which could otherwise affect KSM eligibility.
2718 	 */
2719 	if (file->f_op->mmap_prepare)
2720 		return true;
2721 
2722 	/* shmem is safe. */
2723 	if (shmem_file(file))
2724 		return true;
2725 
2726 	/* Any other .mmap callback is not safe. */
2727 	return false;
2728 }
2729 
2730 static int call_action_complete(struct mmap_state *map,
2731 				struct vm_area_desc *desc,
2732 				struct vm_area_struct *vma)
2733 {
2734 	struct mmap_action *action = &desc->action;
2735 	int ret;
2736 
2737 	ret = mmap_action_complete(action, vma);
2738 
2739 	/* If we held the file rmap we need to release it. */
2740 	if (map->hold_file_rmap_lock) {
2741 		struct file *file = vma->vm_file;
2742 
2743 		i_mmap_unlock_write(file->f_mapping);
2744 	}
2745 	return ret;
2746 }
2747 
2748 static unsigned long __mmap_region(struct file *file, unsigned long addr,
2749 		unsigned long len, vma_flags_t vma_flags,
2750 		unsigned long pgoff, struct list_head *uf)
2751 {
2752 	struct mm_struct *mm = current->mm;
2753 	struct vm_area_struct *vma = NULL;
2754 	bool have_mmap_prepare = file && file->f_op->mmap_prepare;
2755 	VMA_ITERATOR(vmi, mm, addr);
2756 	MMAP_STATE(map, mm, &vmi, addr, len, pgoff, vma_flags, file);
2757 	struct vm_area_desc desc = {
2758 		.mm = mm,
2759 		.file = file,
2760 		.action = {
2761 			.type = MMAP_NOTHING, /* Default to no further action. */
2762 		},
2763 	};
2764 	bool allocated_new = false;
2765 	int error;
2766 
2767 	map.check_ksm_early = can_set_ksm_flags_early(&map);
2768 
2769 	error = __mmap_setup(&map, &desc, uf);
2770 	if (!error && have_mmap_prepare)
2771 		error = call_mmap_prepare(&map, &desc);
2772 	if (error)
2773 		goto abort_munmap;
2774 
2775 	if (map.check_ksm_early)
2776 		update_ksm_flags(&map);
2777 
2778 	/* Attempt to merge with adjacent VMAs... */
2779 	if (map.prev || map.next) {
2780 		VMG_MMAP_STATE(vmg, &map, /* vma = */ NULL);
2781 
2782 		vma = vma_merge_new_range(&vmg);
2783 	}
2784 
2785 	/* ...but if we can't, allocate a new VMA. */
2786 	if (!vma) {
2787 		error = __mmap_new_vma(&map, &vma);
2788 		if (error)
2789 			goto unacct_error;
2790 		allocated_new = true;
2791 	}
2792 
2793 	if (have_mmap_prepare)
2794 		set_vma_user_defined_fields(vma, &map);
2795 
2796 	__mmap_complete(&map, vma);
2797 
2798 	if (have_mmap_prepare && allocated_new) {
2799 		error = call_action_complete(&map, &desc, vma);
2800 
2801 		if (error)
2802 			return error;
2803 	}
2804 
2805 	return addr;
2806 
2807 	/* Accounting was done by __mmap_setup(). */
2808 unacct_error:
2809 	if (map.charged)
2810 		vm_unacct_memory(map.charged);
2811 abort_munmap:
2812 	vms_abort_munmap_vmas(&map.vms, &map.mas_detach);
2813 	return error;
2814 }
2815 
2816 /**
2817  * mmap_region() - Actually perform the userland mapping of a VMA into
2818  * current->mm with known, aligned and overflow-checked @addr and @len, and
2819  * correctly determined VMA flags @vm_flags and page offset @pgoff.
2820  *
2821  * This is an internal memory management function, and should not be used
2822  * directly.
2823  *
2824  * The caller must write-lock current->mm->mmap_lock.
2825  *
2826  * @file: If a file-backed mapping, a pointer to the struct file describing the
2827  * file to be mapped, otherwise NULL.
2828  * @addr: The page-aligned address at which to perform the mapping.
2829  * @len: The page-aligned, non-zero, length of the mapping.
2830  * @vm_flags: The VMA flags which should be applied to the mapping.
2831  * @pgoff: If @file is specified, the page offset into the file, if not then
2832  * the virtual page offset in memory of the anonymous mapping.
2833  * @uf: Optionally, a pointer to a list head used for tracking userfaultfd unmap
2834  * events.
2835  *
2836  * Returns: Either an error, or the address at which the requested mapping has
2837  * been performed.
2838  */
2839 unsigned long mmap_region(struct file *file, unsigned long addr,
2840 			  unsigned long len, vm_flags_t vm_flags,
2841 			  unsigned long pgoff, struct list_head *uf)
2842 {
2843 	unsigned long ret;
2844 	bool writable_file_mapping = false;
2845 	const vma_flags_t vma_flags = legacy_to_vma_flags(vm_flags);
2846 
2847 	mmap_assert_write_locked(current->mm);
2848 
2849 	/* Check to see if MDWE is applicable. */
2850 	if (map_deny_write_exec(&vma_flags, &vma_flags))
2851 		return -EACCES;
2852 
2853 	/* Allow architectures to sanity-check the vm_flags. */
2854 	if (!arch_validate_flags(vm_flags))
2855 		return -EINVAL;
2856 
2857 	/* Map writable and ensure this isn't a sealed memfd. */
2858 	if (file && is_shared_maywrite(&vma_flags)) {
2859 		int error = mapping_map_writable(file->f_mapping);
2860 
2861 		if (error)
2862 			return error;
2863 		writable_file_mapping = true;
2864 	}
2865 
2866 	ret = __mmap_region(file, addr, len, vma_flags, pgoff, uf);
2867 
2868 	/* Clear our write mapping regardless of error. */
2869 	if (writable_file_mapping)
2870 		mapping_unmap_writable(file->f_mapping);
2871 
2872 	validate_mm(current->mm);
2873 	return ret;
2874 }
2875 
2876 /**
2877  * do_brk_flags() - Increase the brk vma if the flags match.
2878  * @vmi: The vma iterator
2879  * @addr: The start address
2880  * @len: The length of the increase
2881  * @vma: The vma,
2882  * @vma_flags: The VMA Flags
2883  *
2884  * Extend the brk VMA from addr to addr + len.  If the VMA is NULL or the flags
2885  * do not match then create a new anonymous VMA.  Eventually we may be able to
2886  * do some brk-specific accounting here.
2887  *
2888  * Returns: %0 on success, or otherwise an error.
2889  */
2890 int do_brk_flags(struct vma_iterator *vmi, struct vm_area_struct *vma,
2891 		 unsigned long addr, unsigned long len, vma_flags_t vma_flags)
2892 {
2893 	struct mm_struct *mm = current->mm;
2894 
2895 	/*
2896 	 * Check against address space limits by the changed size
2897 	 * Note: This happens *after* clearing old mappings in some code paths.
2898 	 */
2899 	vma_flags_set_mask(&vma_flags, VMA_DATA_DEFAULT_FLAGS);
2900 	vma_flags_set(&vma_flags, VMA_ACCOUNT_BIT);
2901 	vma_flags_set_mask(&vma_flags, mm->def_vma_flags);
2902 
2903 	vma_flags = ksm_vma_flags(mm, NULL, vma_flags);
2904 	if (!may_expand_vm(mm, &vma_flags, len >> PAGE_SHIFT))
2905 		return -ENOMEM;
2906 
2907 	if (mm->map_count > get_sysctl_max_map_count())
2908 		return -ENOMEM;
2909 
2910 	if (security_vm_enough_memory_mm(mm, len >> PAGE_SHIFT))
2911 		return -ENOMEM;
2912 
2913 	/*
2914 	 * Expand the existing vma if possible; Note that singular lists do not
2915 	 * occur after forking, so the expand will only happen on new VMAs.
2916 	 */
2917 	if (vma && vma->vm_end == addr) {
2918 		VMG_STATE(vmg, mm, vmi, addr, addr + len, vma_flags, PHYS_PFN(addr));
2919 
2920 		vmg.prev = vma;
2921 		/* vmi is positioned at prev, which this mode expects. */
2922 		vmg.just_expand = true;
2923 
2924 		if (vma_merge_new_range(&vmg))
2925 			goto out;
2926 		else if (vmg_nomem(&vmg))
2927 			goto unacct_fail;
2928 	}
2929 
2930 	if (vma)
2931 		vma_iter_next_range(vmi);
2932 	/* create a vma struct for an anonymous mapping */
2933 	vma = vm_area_alloc(mm);
2934 	if (!vma)
2935 		goto unacct_fail;
2936 
2937 	vma_set_anonymous(vma);
2938 	vma_set_range(vma, addr, addr + len, addr >> PAGE_SHIFT);
2939 	vma->flags = vma_flags;
2940 	vma->vm_page_prot = vm_get_page_prot(vma_flags_to_legacy(vma_flags));
2941 	vma_start_write(vma);
2942 	if (vma_iter_store_gfp(vmi, vma, GFP_KERNEL))
2943 		goto mas_store_fail;
2944 
2945 	mm->map_count++;
2946 	validate_mm(mm);
2947 out:
2948 	perf_event_mmap(vma);
2949 	mm->total_vm += len >> PAGE_SHIFT;
2950 	mm->data_vm += len >> PAGE_SHIFT;
2951 	if (vma_flags_test(&vma_flags, VMA_LOCKED_BIT))
2952 		mm->locked_vm += (len >> PAGE_SHIFT);
2953 	if (pgtable_supports_soft_dirty())
2954 		vma_set_flags(vma, VMA_SOFTDIRTY_BIT);
2955 	return 0;
2956 
2957 mas_store_fail:
2958 	vm_area_free(vma);
2959 unacct_fail:
2960 	vm_unacct_memory(len >> PAGE_SHIFT);
2961 	return -ENOMEM;
2962 }
2963 
2964 /**
2965  * unmapped_area() - Find an area between the low_limit and the high_limit with
2966  * the correct alignment and offset, all from @info. Note: current->mm is used
2967  * for the search.
2968  *
2969  * @info: The unmapped area information including the range [low_limit -
2970  * high_limit), the alignment offset and mask.
2971  *
2972  * Return: A memory address or -ENOMEM.
2973  */
2974 unsigned long unmapped_area(struct vm_unmapped_area_info *info)
2975 {
2976 	unsigned long length, gap;
2977 	unsigned long low_limit, high_limit;
2978 	struct vm_area_struct *tmp;
2979 	VMA_ITERATOR(vmi, current->mm, 0);
2980 
2981 	/* Adjust search length to account for worst case alignment overhead */
2982 	length = info->length + info->align_mask + info->start_gap;
2983 	if (length < info->length)
2984 		return -ENOMEM;
2985 
2986 	low_limit = info->low_limit;
2987 	if (low_limit < mmap_min_addr)
2988 		low_limit = mmap_min_addr;
2989 	high_limit = info->high_limit;
2990 retry:
2991 	if (vma_iter_area_lowest(&vmi, low_limit, high_limit, length))
2992 		return -ENOMEM;
2993 
2994 	/*
2995 	 * Adjust for the gap first so it doesn't interfere with the later
2996 	 * alignment. The first step is the minimum needed to fulfill the start
2997 	 * gap, the next step is the minimum to align that. It is the minimum
2998 	 * needed to fulfill both.
2999 	 */
3000 	gap = vma_iter_addr(&vmi) + info->start_gap;
3001 	gap += (info->align_offset - gap) & info->align_mask;
3002 	tmp = vma_next(&vmi);
3003 	/* Avoid prev check if possible */
3004 	if (tmp && vma_test_any_mask(tmp, VMA_STARTGAP_FLAGS)) {
3005 		if (vm_start_gap(tmp) < gap + length - 1) {
3006 			low_limit = tmp->vm_end;
3007 			vma_iter_reset(&vmi);
3008 			goto retry;
3009 		}
3010 	} else {
3011 		tmp = vma_prev(&vmi);
3012 		if (tmp && vm_end_gap(tmp) > gap) {
3013 			low_limit = vm_end_gap(tmp);
3014 			vma_iter_reset(&vmi);
3015 			goto retry;
3016 		}
3017 	}
3018 
3019 	return gap;
3020 }
3021 
3022 /**
3023  * unmapped_area_topdown() - Find an area between the low_limit and the
3024  * high_limit with the correct alignment and offset at the highest available
3025  * address, all from @info. Note: current->mm is used for the search.
3026  *
3027  * @info: The unmapped area information including the range [low_limit -
3028  * high_limit), the alignment offset and mask.
3029  *
3030  * Return: A memory address or -ENOMEM.
3031  */
3032 unsigned long unmapped_area_topdown(struct vm_unmapped_area_info *info)
3033 {
3034 	unsigned long length, gap, gap_end;
3035 	unsigned long low_limit, high_limit;
3036 	struct vm_area_struct *tmp;
3037 	VMA_ITERATOR(vmi, current->mm, 0);
3038 
3039 	/* Adjust search length to account for worst case alignment overhead */
3040 	length = info->length + info->align_mask + info->start_gap;
3041 	if (length < info->length)
3042 		return -ENOMEM;
3043 
3044 	low_limit = info->low_limit;
3045 	if (low_limit < mmap_min_addr)
3046 		low_limit = mmap_min_addr;
3047 	high_limit = info->high_limit;
3048 retry:
3049 	if (vma_iter_area_highest(&vmi, low_limit, high_limit, length))
3050 		return -ENOMEM;
3051 
3052 	gap = vma_iter_end(&vmi) - info->length;
3053 	gap -= (gap - info->align_offset) & info->align_mask;
3054 	gap_end = vma_iter_end(&vmi);
3055 	tmp = vma_next(&vmi);
3056 	 /* Avoid prev check if possible */
3057 	if (tmp && vma_test_any_mask(tmp, VMA_STARTGAP_FLAGS)) {
3058 		if (vm_start_gap(tmp) < gap_end) {
3059 			high_limit = vm_start_gap(tmp);
3060 			vma_iter_reset(&vmi);
3061 			goto retry;
3062 		}
3063 	} else {
3064 		tmp = vma_prev(&vmi);
3065 		if (tmp && vm_end_gap(tmp) > gap) {
3066 			high_limit = tmp->vm_start;
3067 			vma_iter_reset(&vmi);
3068 			goto retry;
3069 		}
3070 	}
3071 
3072 	return gap;
3073 }
3074 
3075 /*
3076  * Verify that the stack growth is acceptable and
3077  * update accounting. This is shared with both the
3078  * grow-up and grow-down cases.
3079  */
3080 static int acct_stack_growth(struct vm_area_struct *vma,
3081 			     unsigned long size, unsigned long grow)
3082 {
3083 	struct mm_struct *mm = vma->vm_mm;
3084 	unsigned long new_start;
3085 
3086 	/* address space limit tests */
3087 	if (!may_expand_vm(mm, &vma->flags, grow))
3088 		return -ENOMEM;
3089 
3090 	/* Stack limit test */
3091 	if (size > rlimit(RLIMIT_STACK))
3092 		return -ENOMEM;
3093 
3094 	/* mlock limit tests */
3095 	if (!mlock_future_ok(mm, vma_test(vma, VMA_LOCKED_BIT),
3096 			     grow << PAGE_SHIFT))
3097 		return -ENOMEM;
3098 
3099 	/* Check to ensure the stack will not grow into a hugetlb-only region */
3100 	new_start = vma->vm_end - size;
3101 #ifdef CONFIG_STACK_GROWSUP
3102 	if (vma_test(vma, VMA_GROWSUP_BIT))
3103 		new_start = vma->vm_start;
3104 #endif
3105 	if (is_hugepage_only_range(vma->vm_mm, new_start, size))
3106 		return -EFAULT;
3107 
3108 	/*
3109 	 * Overcommit..  This must be the final test, as it will
3110 	 * update security statistics.
3111 	 */
3112 	if (security_vm_enough_memory_mm(mm, grow))
3113 		return -ENOMEM;
3114 
3115 	return 0;
3116 }
3117 
3118 #ifdef CONFIG_STACK_GROWSUP
3119 /*
3120  * PA-RISC uses this for its stack.
3121  * vma is the last one with address > vma->vm_end.  Have to extend vma.
3122  */
3123 int expand_upwards(struct vm_area_struct *vma, unsigned long address)
3124 {
3125 	struct mm_struct *mm = vma->vm_mm;
3126 	struct vm_area_struct *next;
3127 	unsigned long gap_addr;
3128 	int error = 0;
3129 	VMA_ITERATOR(vmi, mm, vma->vm_start);
3130 
3131 	if (!vma_test(vma, VMA_GROWSUP_BIT))
3132 		return -EFAULT;
3133 
3134 	mmap_assert_write_locked(mm);
3135 
3136 	/* Guard against exceeding limits of the address space. */
3137 	address &= PAGE_MASK;
3138 	if (address >= (TASK_SIZE & PAGE_MASK))
3139 		return -ENOMEM;
3140 	address += PAGE_SIZE;
3141 
3142 	/* Enforce stack_guard_gap */
3143 	gap_addr = address + stack_guard_gap;
3144 
3145 	/* Guard against overflow */
3146 	if (gap_addr < address || gap_addr > TASK_SIZE)
3147 		gap_addr = TASK_SIZE;
3148 
3149 	next = find_vma_intersection(mm, vma->vm_end, gap_addr);
3150 	if (next && vma_is_accessible(next)) {
3151 		if (!vma_test(next, VMA_GROWSUP_BIT))
3152 			return -ENOMEM;
3153 		/* Check that both stack segments have the same anon_vma? */
3154 	}
3155 
3156 	if (next)
3157 		vma_iter_prev_range_limit(&vmi, address);
3158 
3159 	vma_iter_config(&vmi, vma->vm_start, address);
3160 	if (vma_iter_prealloc(&vmi, vma))
3161 		return -ENOMEM;
3162 
3163 	/* We must make sure the anon_vma is allocated. */
3164 	if (unlikely(anon_vma_prepare(vma))) {
3165 		vma_iter_free(&vmi);
3166 		return -ENOMEM;
3167 	}
3168 
3169 	/* Lock the VMA before expanding to prevent concurrent page faults */
3170 	vma_start_write(vma);
3171 	/* We update the anon VMA tree. */
3172 	anon_vma_lock_write(vma->anon_vma);
3173 
3174 	/* Somebody else might have raced and expanded it already */
3175 	if (address > vma->vm_end) {
3176 		unsigned long size, grow;
3177 
3178 		size = address - vma->vm_start;
3179 		grow = (address - vma->vm_end) >> PAGE_SHIFT;
3180 
3181 		error = -ENOMEM;
3182 		if (vma->vm_pgoff + (size >> PAGE_SHIFT) >= vma->vm_pgoff) {
3183 			error = acct_stack_growth(vma, size, grow);
3184 			if (!error) {
3185 				if (vma_test(vma, VMA_LOCKED_BIT))
3186 					mm->locked_vm += grow;
3187 				vm_stat_account(mm, vma->vm_flags, grow);
3188 				anon_vma_interval_tree_pre_update_vma(vma);
3189 				vma->vm_end = address;
3190 				/* Overwrite old entry in mtree. */
3191 				vma_iter_store_overwrite(&vmi, vma);
3192 				anon_vma_interval_tree_post_update_vma(vma);
3193 
3194 				perf_event_mmap(vma);
3195 			}
3196 		}
3197 	}
3198 	anon_vma_unlock_write(vma->anon_vma);
3199 	vma_iter_free(&vmi);
3200 	validate_mm(mm);
3201 	return error;
3202 }
3203 #endif /* CONFIG_STACK_GROWSUP */
3204 
3205 /*
3206  * vma is the first one with address < vma->vm_start.  Have to extend vma.
3207  * mmap_lock held for writing.
3208  */
3209 int expand_downwards(struct vm_area_struct *vma, unsigned long address)
3210 {
3211 	struct mm_struct *mm = vma->vm_mm;
3212 	struct vm_area_struct *prev;
3213 	int error = 0;
3214 	VMA_ITERATOR(vmi, mm, vma->vm_start);
3215 
3216 	if (!vma_test(vma, VMA_GROWSDOWN_BIT))
3217 		return -EFAULT;
3218 
3219 	mmap_assert_write_locked(mm);
3220 
3221 	address &= PAGE_MASK;
3222 	if (address < mmap_min_addr || address < FIRST_USER_ADDRESS)
3223 		return -EPERM;
3224 
3225 	/* Enforce stack_guard_gap */
3226 	prev = vma_prev(&vmi);
3227 	/* Check that both stack segments have the same anon_vma? */
3228 	if (prev) {
3229 		if (!vma_test(prev, VMA_GROWSDOWN_BIT) &&
3230 		    vma_is_accessible(prev) &&
3231 		    (address - prev->vm_end < stack_guard_gap))
3232 			return -ENOMEM;
3233 	}
3234 
3235 	if (prev)
3236 		vma_iter_next_range_limit(&vmi, vma->vm_start);
3237 
3238 	vma_iter_config(&vmi, address, vma->vm_end);
3239 	if (vma_iter_prealloc(&vmi, vma))
3240 		return -ENOMEM;
3241 
3242 	/* We must make sure the anon_vma is allocated. */
3243 	if (unlikely(anon_vma_prepare(vma))) {
3244 		vma_iter_free(&vmi);
3245 		return -ENOMEM;
3246 	}
3247 
3248 	/* Lock the VMA before expanding to prevent concurrent page faults */
3249 	vma_start_write(vma);
3250 	/* We update the anon VMA tree. */
3251 	anon_vma_lock_write(vma->anon_vma);
3252 
3253 	/* Somebody else might have raced and expanded it already */
3254 	if (address < vma->vm_start) {
3255 		unsigned long size, grow;
3256 
3257 		size = vma->vm_end - address;
3258 		grow = (vma->vm_start - address) >> PAGE_SHIFT;
3259 
3260 		error = -ENOMEM;
3261 		if (grow <= vma->vm_pgoff) {
3262 			error = acct_stack_growth(vma, size, grow);
3263 			if (!error) {
3264 				if (vma_test(vma, VMA_LOCKED_BIT))
3265 					mm->locked_vm += grow;
3266 				vm_stat_account(mm, vma->vm_flags, grow);
3267 				anon_vma_interval_tree_pre_update_vma(vma);
3268 				vma->vm_start = address;
3269 				vma->vm_pgoff -= grow;
3270 				/* Overwrite old entry in mtree. */
3271 				vma_iter_store_overwrite(&vmi, vma);
3272 				anon_vma_interval_tree_post_update_vma(vma);
3273 
3274 				perf_event_mmap(vma);
3275 			}
3276 		}
3277 	}
3278 	anon_vma_unlock_write(vma->anon_vma);
3279 	vma_iter_free(&vmi);
3280 	validate_mm(mm);
3281 	return error;
3282 }
3283 
3284 int __vm_munmap(unsigned long start, size_t len, bool unlock)
3285 {
3286 	int ret;
3287 	struct mm_struct *mm = current->mm;
3288 	LIST_HEAD(uf);
3289 	VMA_ITERATOR(vmi, mm, start);
3290 
3291 	if (mmap_write_lock_killable(mm))
3292 		return -EINTR;
3293 
3294 	ret = do_vmi_munmap(&vmi, mm, start, len, &uf, unlock);
3295 	if (ret || !unlock)
3296 		mmap_write_unlock(mm);
3297 
3298 	userfaultfd_unmap_complete(mm, &uf);
3299 	return ret;
3300 }
3301 
3302 /* Insert vm structure into process list sorted by address
3303  * and into the inode's i_mmap tree.  If vm_file is non-NULL
3304  * then i_mmap_rwsem is taken here.
3305  */
3306 int insert_vm_struct(struct mm_struct *mm, struct vm_area_struct *vma)
3307 {
3308 	unsigned long charged = vma_pages(vma);
3309 
3310 	if (find_vma_intersection(mm, vma->vm_start, vma->vm_end))
3311 		return -ENOMEM;
3312 
3313 	if (vma_test(vma, VMA_ACCOUNT_BIT) &&
3314 	     security_vm_enough_memory_mm(mm, charged))
3315 		return -ENOMEM;
3316 
3317 	/*
3318 	 * The vm_pgoff of a purely anonymous vma should be irrelevant
3319 	 * until its first write fault, when page's anon_vma and index
3320 	 * are set.  But now set the vm_pgoff it will almost certainly
3321 	 * end up with (unless mremap moves it elsewhere before that
3322 	 * first wfault), so /proc/pid/maps tells a consistent story.
3323 	 *
3324 	 * By setting it to reflect the virtual start address of the
3325 	 * vma, merges and splits can happen in a seamless way, just
3326 	 * using the existing file pgoff checks and manipulations.
3327 	 * Similarly in do_mmap and in do_brk_flags.
3328 	 */
3329 	if (vma_is_anonymous(vma)) {
3330 		BUG_ON(vma->anon_vma);
3331 		vma->vm_pgoff = vma->vm_start >> PAGE_SHIFT;
3332 	}
3333 
3334 	if (vma_link(mm, vma)) {
3335 		if (vma_test(vma, VMA_ACCOUNT_BIT))
3336 			vm_unacct_memory(charged);
3337 		return -ENOMEM;
3338 	}
3339 
3340 	return 0;
3341 }
3342 
3343 /**
3344  * vma_mmu_pagesize - Default MMU page size granularity for this VMA.
3345  * @vma: The user mapping.
3346  *
3347  * In the common case, the default page size used by the MMU matches the
3348  * default page size used by the kernel (see vma_kernel_pagesize()). On
3349  * architectures where it differs, an architecture-specific 'strong' version
3350  * of this symbol is required.
3351  *
3352  * The default MMU page size is not affected by Transparent Huge Pages
3353  * being in effect, or any usage of larger MMU page sizes (either through
3354  * architectural huge-page mappings or other explicit/implicit coalescing of
3355  * virtual ranges performed by the MMU).
3356  *
3357  * Return: The default MMU page size granularity for this VMA.
3358  */
3359 __weak unsigned long vma_mmu_pagesize(struct vm_area_struct *vma)
3360 {
3361 	return vma_kernel_pagesize(vma);
3362 }
3363